Hex Artifact Content
Not logged in

Artifact 955cc67ace8fb6226281807b542b761622253ecd:


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 37 2e 31 35 2e 20 20  ersion 3.7.15.  
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 73 69 6e 67 6c 65 20 74 72 61 6e 73 6c   a single transl
0130: 61 74 69 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20  ation.** unit.  
0140: 54 68 69 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79  This allows many
0150: 20 63 6f 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f   compilers to do
0160: 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74   optimizations t
0170: 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65  hat would not be
0180: 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 20 69 66 20  .** possible if 
0190: 74 68 65 20 66 69 6c 65 73 20 77 65 72 65 20 63  the files were c
01a0: 6f 6d 70 69 6c 65 64 20 73 65 70 61 72 61 74 65  ompiled separate
01b0: 6c 79 2e 20 20 50 65 72 66 6f 72 6d 61 6e 63 65  ly.  Performance
01c0: 20 69 6d 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a   improvements.**
01d0: 20 6f 66 20 35 25 20 6f 72 20 6d 6f 72 65 20 61   of 5% or more a
01e0: 72 65 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e  re commonly seen
01f0: 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20   when SQLite is 
0200: 63 6f 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69  compiled as a si
0210: 6e 67 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74  ngle.** translat
0220: 69 6f 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20  ion unit..**.** 
0230: 54 68 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c  This file is all
0240: 20 79 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d   you need to com
0250: 70 69 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f  pile SQLite.  To
0260: 20 75 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f   use SQLite in o
0270: 74 68 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73  ther.** programs
0280: 2c 20 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20  , you need this 
0290: 66 69 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71  file and the "sq
02a0: 6c 69 74 65 33 2e 68 22 20 68 65 61 64 65 72 20  lite3.h" header 
02b0: 66 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65  file that define
02c0: 73 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d  s.** the program
02d0: 6d 69 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74  ming interface t
02e0: 6f 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  o the SQLite lib
02f0: 72 61 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64  rary.  (If you d
0300: 6f 20 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74  o not have .** t
0310: 68 65 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68  he "sqlite3.h" h
0320: 65 61 64 65 72 20 66 69 6c 65 20 61 74 20 68 61  eader file at ha
0330: 6e 64 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e  nd, you will fin
0340: 64 20 61 20 63 6f 70 79 20 65 6d 62 65 64 64 65  d a copy embedde
0350: 64 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20  d within.** the 
0360: 74 65 78 74 20 6f 66 20 74 68 69 73 20 66 69 6c  text of this fil
0370: 65 2e 20 20 53 65 61 72 63 68 20 66 6f 72 20 22  e.  Search for "
0380: 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74  Begin file sqlit
0390: 65 33 2e 68 22 20 74 6f 20 66 69 6e 64 20 74 68  e3.h" to find th
03a0: 65 20 73 74 61 72 74 0a 2a 2a 20 6f 66 20 74 68  e start.** of th
03b0: 65 20 65 6d 62 65 64 64 65 64 20 73 71 6c 69 74  e embedded sqlit
03c0: 65 33 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65  e3.h header file
03d0: 2e 29 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f  .) Additional co
03e0: 64 65 20 66 69 6c 65 73 20 6d 61 79 20 62 65 20  de files may be 
03f0: 6e 65 65 64 65 64 0a 2a 2a 20 69 66 20 79 6f 75  needed.** if you
0400: 20 77 61 6e 74 20 61 20 77 72 61 70 70 65 72 20   want a wrapper 
0410: 74 6f 20 69 6e 74 65 72 66 61 63 65 20 53 51 4c  to interface SQL
0420: 69 74 65 20 77 69 74 68 20 79 6f 75 72 20 63 68  ite with your ch
0430: 6f 69 63 65 20 6f 66 20 70 72 6f 67 72 61 6d 6d  oice of programm
0440: 69 6e 67 0a 2a 2a 20 6c 61 6e 67 75 61 67 65 2e  ing.** language.
0450: 20 54 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68   The code for th
0460: 65 20 22 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d  e "sqlite3" comm
0470: 61 6e 64 2d 6c 69 6e 65 20 73 68 65 6c 6c 20 69  and-line shell i
0480: 73 20 61 6c 73 6f 20 69 6e 20 61 0a 2a 2a 20 73  s also in a.** s
0490: 65 70 61 72 61 74 65 20 66 69 6c 65 2e 20 54 68  eparate file. Th
04a0: 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
04b0: 20 6f 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74   only code for t
04c0: 68 65 20 63 6f 72 65 20 53 51 4c 69 74 65 20 6c  he core SQLite l
04d0: 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 64 65 66 69  ibrary..*/.#defi
04e0: 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31  ne SQLITE_CORE 1
04f0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
0500: 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 31 0a 23  AMALGAMATION 1.#
0510: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52  ifndef SQLITE_PR
0520: 49 56 41 54 45 0a 23 20 64 65 66 69 6e 65 20 53  IVATE.# define S
0530: 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74  QLITE_PRIVATE st
0540: 61 74 69 63 0a 23 65 6e 64 69 66 0a 23 69 66 6e  atic.#endif.#ifn
0550: 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23  def SQLITE_API.#
0560: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41   define SQLITE_A
0570: 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a  PI.#endif./*****
0580: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
0590: 66 69 6c 65 20 73 71 6c 69 74 65 49 6e 74 2e 68  file sqliteInt.h
05a0: 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
05b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
05c0: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
05d0: 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
05e0: 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
05f0: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
0600: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
0610: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
0620: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
0630: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
0640: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
0650: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
0660: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
0670: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
0680: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
0690: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
06a0: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
06b0: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
06c0: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
06d0: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
06e0: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
06f0: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
0700: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0710: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0720: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0730: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0740: 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 69 6e 74  .** Internal int
0750: 65 72 66 61 63 65 20 64 65 66 69 6e 69 74 69 6f  erface definitio
0760: 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a  ns for SQLite..*
0770: 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51  *.*/.#ifndef _SQ
0780: 4c 49 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69  LITEINT_H_.#defi
0790: 6e 65 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f  ne _SQLITEINT_H_
07a0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64  ../*.** These #d
07b0: 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e  efines should en
07c0: 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73  able >2GB file s
07d0: 75 70 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20  upport on POSIX 
07e0: 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c  if the.** underl
07f0: 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73  ying operating s
0800: 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69  ystem supports i
0810: 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61  t.  If the OS la
0820: 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c  cks.** large fil
0830: 65 20 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66  e support, or if
0840: 20 74 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f   the OS is windo
0850: 77 73 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64  ws, these should
0860: 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a   be no-ops..**.*
0870: 2a 20 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20  * Ticket #2739: 
0880: 20 54 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f   The _LARGEFILE_
0890: 53 4f 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73  SOURCE macro mus
08a0: 74 20 61 70 70 65 61 72 20 62 65 66 6f 72 65 20  t appear before 
08b0: 61 6e 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69  any.** system #i
08c0: 6e 63 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c  ncludes.  Hence,
08d0: 20 74 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63   this block of c
08e0: 6f 64 65 20 6d 75 73 74 20 62 65 20 74 68 65 20  ode must be the 
08f0: 76 65 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f  very first.** co
0900: 64 65 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65  de in all source
0910: 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61   files..**.** La
0920: 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74  rge file support
0930: 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64   can be disabled
0940: 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c   using the -DSQL
0950: 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20  ITE_DISABLE_LFS 
0960: 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65  switch.** on the
0970: 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e   compiler comman
0980: 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73  d line.  This is
0990: 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f   necessary if yo
09a0: 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a  u are compiling.
09b0: 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d  ** on a recent m
09c0: 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20  achine (ex: Red 
09d0: 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75  Hat 7.2) but you
09e0: 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20   want your code 
09f0: 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e  to work.** on an
0a00: 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28   older machine (
0a10: 65 78 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29  ex: Red Hat 6.0)
0a20: 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c  .  If you compil
0a30: 65 20 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32  e on Red Hat 7.2
0a40: 0a 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73  .** without this
0a50: 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20   option, LFS is 
0a60: 65 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53  enable.  But LFS
0a70: 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
0a80: 69 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a  in the kernel.**
0a90: 20 69 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c   in Red Hat 6.0,
0aa0: 20 73 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e   so the code won
0ab0: 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c  't work.  Hence,
0ac0: 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e   for maximum bin
0ad0: 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69  ary.** portabili
0ae0: 74 79 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d  ty you should om
0af0: 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69  it LFS..**.** Si
0b00: 6d 69 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f  milar is true fo
0b10: 72 20 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53  r Mac OS X.  LFS
0b20: 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74   is only support
0b30: 65 64 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39  ed on Mac OS X 9
0b40: 20 61 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23   and later..*/.#
0b50: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49  ifndef SQLITE_DI
0b60: 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69  SABLE_LFS.# defi
0b70: 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20  ne _LARGE_FILE  
0b80: 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20       1.# ifndef 
0b90: 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54  _FILE_OFFSET_BIT
0ba0: 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49  S.#   define _FI
0bb0: 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36  LE_OFFSET_BITS 6
0bc0: 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69  4.# endif.# defi
0bd0: 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f  ne _LARGEFILE_SO
0be0: 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f  URCE 1.#endif../
0bf0: 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 65  *.** Include the
0c00: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 68   configuration h
0c10: 65 61 64 65 72 20 6f 75 74 70 75 74 20 62 79 20  eader output by 
0c20: 27 63 6f 6e 66 69 67 75 72 65 27 20 69 66 20 77  'configure' if w
0c30: 65 27 72 65 20 75 73 69 6e 67 20 74 68 65 0a 2a  e're using the.*
0c40: 2a 20 61 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64  * autoconf-based
0c50: 20 62 75 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66   build.*/.#ifdef
0c60: 20 5f 48 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f   _HAVE_SQLITE_CO
0c70: 4e 46 49 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20  NFIG_H.#include 
0c80: 22 63 6f 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69  "config.h".#endi
0c90: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
0ca0: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74  ** Include sqlit
0cb0: 65 4c 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20  eLimit.h in the 
0cc0: 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
0cd0: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
0ce0: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
0cf0: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71  ** Begin file sq
0d00: 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a  liteLimit.h ****
0d10: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0d20: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0d30: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61  */./*.** 2007 Ma
0d40: 79 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  y 7.**.** The au
0d50: 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
0d60: 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
0d70: 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
0d80: 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
0d90: 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
0da0: 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
0db0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
0dc0: 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
0dd0: 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
0de0: 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
0df0: 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
0e00: 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
0e10: 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
0e20: 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
0e30: 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
0e40: 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
0e50: 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
0e60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0e70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0e80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0e90: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ea0: 2a 2a 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66  **.** .** This f
0eb0: 69 6c 65 20 64 65 66 69 6e 65 73 20 76 61 72 69  ile defines vari
0ec0: 6f 75 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68  ous limits of wh
0ed0: 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72  at SQLite can pr
0ee0: 6f 63 65 73 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ocess..*/../*.**
0ef0: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e   The maximum len
0f00: 67 74 68 20 6f 66 20 61 20 54 45 58 54 20 6f 72  gth of a TEXT or
0f10: 20 42 4c 4f 42 20 69 6e 20 62 79 74 65 73 2e 20   BLOB in bytes. 
0f20: 20 20 54 68 69 73 20 61 6c 73 6f 0a 2a 2a 20 6c    This also.** l
0f30: 69 6d 69 74 73 20 74 68 65 20 73 69 7a 65 20 6f  imits the size o
0f40: 66 20 61 20 72 6f 77 20 69 6e 20 61 20 74 61 62  f a row in a tab
0f50: 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 0a  le or index..**.
0f60: 2a 2a 20 54 68 65 20 68 61 72 64 20 6c 69 6d 69  ** The hard limi
0f70: 74 20 69 73 20 74 68 65 20 61 62 69 6c 69 74 79  t is the ability
0f80: 20 6f 66 20 61 20 33 32 2d 62 69 74 20 73 69 67   of a 32-bit sig
0f90: 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74  ned integer.** t
0fa0: 6f 20 63 6f 75 6e 74 20 74 68 65 20 73 69 7a 65  o count the size
0fb0: 3a 20 32 5e 33 31 2d 31 20 6f 72 20 32 31 34 37  : 2^31-1 or 2147
0fc0: 34 38 33 36 34 37 2e 0a 2a 2f 0a 23 69 66 6e 64  483647..*/.#ifnd
0fd0: 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  ef SQLITE_MAX_LE
0fe0: 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51  NGTH.# define SQ
0ff0: 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20  LITE_MAX_LENGTH 
1000: 31 30 30 30 30 30 30 30 30 30 0a 23 65 6e 64 69  1000000000.#endi
1010: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  f../*.** This is
1020: 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   the maximum num
1030: 62 65 72 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 20  ber of.**.**    
1040: 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74  * Columns in a t
1050: 61 62 6c 65 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c  able.**    * Col
1060: 75 6d 6e 73 20 69 6e 20 61 6e 20 69 6e 64 65 78  umns in an index
1070: 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73  .**    * Columns
1080: 20 69 6e 20 61 20 76 69 65 77 0a 2a 2a 20 20 20   in a view.**   
1090: 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20   * Terms in the 
10a0: 53 45 54 20 63 6c 61 75 73 65 20 6f 66 20 61 6e  SET clause of an
10b0: 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
10c0: 74 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20  t.**    * Terms 
10d0: 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
10e0: 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74  t of a SELECT st
10f0: 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 2a 20  atement.**    * 
1100: 54 65 72 6d 73 20 69 6e 20 74 68 65 20 47 52 4f  Terms in the GRO
1110: 55 50 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42  UP BY or ORDER B
1120: 59 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 53  Y clauses of a S
1130: 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
1140: 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69  .**    * Terms i
1150: 6e 20 74 68 65 20 56 41 4c 55 45 53 20 63 6c 61  n the VALUES cla
1160: 75 73 65 20 6f 66 20 61 6e 20 49 4e 53 45 52 54  use of an INSERT
1170: 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a   statement.**.**
1180: 20 54 68 65 20 68 61 72 64 20 75 70 70 65 72 20   The hard upper 
1190: 6c 69 6d 69 74 20 68 65 72 65 20 69 73 20 33 32  limit here is 32
11a0: 36 37 36 2e 20 20 4d 6f 73 74 20 64 61 74 61 62  676.  Most datab
11b0: 61 73 65 20 70 65 6f 70 6c 65 20 77 69 6c 6c 0a  ase people will.
11c0: 2a 2a 20 74 65 6c 6c 20 79 6f 75 20 74 68 61 74  ** tell you that
11d0: 20 69 6e 20 61 20 77 65 6c 6c 2d 6e 6f 72 6d 61   in a well-norma
11e0: 6c 69 7a 65 64 20 64 61 74 61 62 61 73 65 2c 20  lized database, 
11f0: 79 6f 75 20 75 73 75 61 6c 6c 79 20 73 68 6f 75  you usually shou
1200: 6c 64 0a 2a 2a 20 6e 6f 74 20 68 61 76 65 20 6d  ld.** not have m
1210: 6f 72 65 20 74 68 61 6e 20 61 20 64 6f 7a 65 6e  ore than a dozen
1220: 20 6f 72 20 73 6f 20 63 6f 6c 75 6d 6e 73 20 69   or so columns i
1230: 6e 20 61 6e 79 20 74 61 62 6c 65 2e 20 20 41 6e  n any table.  An
1240: 64 20 69 66 0a 2a 2a 20 74 68 61 74 20 69 73 20  d if.** that is 
1250: 74 68 65 20 63 61 73 65 2c 20 74 68 65 72 65 20  the case, there 
1260: 69 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 68  is no point in h
1270: 61 76 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aving more than 
1280: 61 20 66 65 77 0a 2a 2a 20 64 6f 7a 65 6e 20 76  a few.** dozen v
1290: 61 6c 75 65 73 20 69 6e 20 61 6e 79 20 6f 66 20  alues in any of 
12a0: 74 68 65 20 6f 74 68 65 72 20 73 69 74 75 61 74  the other situat
12b0: 69 6f 6e 73 20 64 65 73 63 72 69 62 65 64 20 61  ions described a
12c0: 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  bove..*/.#ifndef
12d0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55   SQLITE_MAX_COLU
12e0: 4d 4e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  MN.# define SQLI
12f0: 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 32 30  TE_MAX_COLUMN 20
1300: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  00.#endif../*.**
1310: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e   The maximum len
1320: 67 74 68 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  gth of a single 
1330: 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  SQL statement in
1340: 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74   bytes..**.** It
1350: 20 75 73 65 64 20 74 6f 20 62 65 20 74 68 65 20   used to be the 
1360: 63 61 73 65 20 74 68 61 74 20 73 65 74 74 69 6e  case that settin
1370: 67 20 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20  g this value to 
1380: 7a 65 72 6f 20 77 6f 75 6c 64 0a 2a 2a 20 74 75  zero would.** tu
1390: 72 6e 20 74 68 65 20 6c 69 6d 69 74 20 6f 66 66  rn the limit off
13a0: 2e 20 20 54 68 61 74 20 69 73 20 6e 6f 20 6c 6f  .  That is no lo
13b0: 6e 67 65 72 20 74 72 75 65 2e 20 20 49 74 20 69  nger true.  It i
13c0: 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 0a 2a  s not possible.*
13d0: 2a 20 74 6f 20 74 75 72 6e 20 74 68 69 73 20 6c  * to turn this l
13e0: 69 6d 69 74 20 6f 66 66 2e 0a 2a 2f 0a 23 69 66  imit off..*/.#if
13f0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
1400: 53 51 4c 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66  SQL_LENGTH.# def
1410: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53  ine SQLITE_MAX_S
1420: 51 4c 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30  QL_LENGTH 100000
1430: 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  0000.#endif../*.
1440: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 64  ** The maximum d
1450: 65 70 74 68 20 6f 66 20 61 6e 20 65 78 70 72 65  epth of an expre
1460: 73 73 69 6f 6e 20 74 72 65 65 2e 20 54 68 69 73  ssion tree. This
1470: 20 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 0a   is limited to .
1480: 2a 2a 20 73 6f 6d 65 20 65 78 74 65 6e 74 20 62  ** some extent b
1490: 79 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c  y SQLITE_MAX_SQL
14a0: 5f 4c 45 4e 47 54 48 2e 20 42 75 74 20 73 6f 6d  _LENGTH. But som
14b0: 65 74 69 6d 65 20 79 6f 75 20 6d 69 67 68 74 20  etime you might 
14c0: 0a 2a 2a 20 77 61 6e 74 20 74 6f 20 70 6c 61 63  .** want to plac
14d0: 65 20 6d 6f 72 65 20 73 65 76 65 72 65 20 6c 69  e more severe li
14e0: 6d 69 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70  mits on the comp
14f0: 6c 65 78 69 74 79 20 6f 66 20 61 6e 20 0a 2a 2a  lexity of an .**
1500: 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a   expression..**.
1510: 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20  ** A value of 0 
1520: 75 73 65 64 20 74 6f 20 6d 65 61 6e 20 74 68 61  used to mean tha
1530: 74 20 74 68 65 20 6c 69 6d 69 74 20 77 61 73 20  t the limit was 
1540: 6e 6f 74 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2a  not enforced..**
1550: 20 42 75 74 20 74 68 61 74 20 69 73 20 6e 6f 20   But that is no 
1560: 6c 6f 6e 67 65 72 20 74 72 75 65 2e 20 20 54 68  longer true.  Th
1570: 65 20 6c 69 6d 69 74 20 69 73 20 6e 6f 77 20 73  e limit is now s
1580: 74 72 69 63 74 6c 79 20 65 6e 66 6f 72 63 65 64  trictly enforced
1590: 0a 2a 2a 20 61 74 20 61 6c 6c 20 74 69 6d 65 73  .** at all times
15a0: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
15b0: 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50  ITE_MAX_EXPR_DEP
15c0: 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  TH.# define SQLI
15d0: 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
15e0: 48 20 31 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f  H 1000.#endif../
15f0: 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d  *.** The maximum
1600: 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   number of terms
1610: 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53   in a compound S
1620: 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
1630: 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e  .** The code gen
1640: 65 72 61 74 6f 72 20 66 6f 72 20 63 6f 6d 70 6f  erator for compo
1650: 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65  und SELECT state
1660: 6d 65 6e 74 73 20 64 6f 65 73 20 6f 6e 65 0a 2a  ments does one.*
1670: 2a 20 6c 65 76 65 6c 20 6f 66 20 72 65 63 75 72  * level of recur
1680: 73 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 65  sion for each te
1690: 72 6d 2e 20 20 41 20 73 74 61 63 6b 20 6f 76 65  rm.  A stack ove
16a0: 72 66 6c 6f 77 20 63 61 6e 20 72 65 73 75 6c 74  rflow can result
16b0: 0a 2a 2a 20 69 66 20 74 68 65 20 6e 75 6d 62 65  .** if the numbe
16c0: 72 20 6f 66 20 74 65 72 6d 73 20 69 73 20 74 6f  r of terms is to
16d0: 6f 20 6c 61 72 67 65 2e 20 20 49 6e 20 70 72 61  o large.  In pra
16e0: 63 74 69 63 65 2c 20 6d 6f 73 74 20 53 51 4c 0a  ctice, most SQL.
16f0: 2a 2a 20 6e 65 76 65 72 20 68 61 73 20 6d 6f 72  ** never has mor
1700: 65 20 74 68 61 6e 20 33 20 6f 72 20 34 20 74 65  e than 3 or 4 te
1710: 72 6d 73 2e 20 20 55 73 65 20 61 20 76 61 6c 75  rms.  Use a valu
1720: 65 20 6f 66 20 30 20 74 6f 20 64 69 73 61 62 6c  e of 0 to disabl
1730: 65 0a 2a 2a 20 61 6e 79 20 6c 69 6d 69 74 20 6f  e.** any limit o
1740: 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1750: 74 65 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f  terms in a compo
1760: 75 6e 74 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 23  unt SELECT..*/.#
1770: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41  ifndef SQLITE_MA
1780: 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  X_COMPOUND_SELEC
1790: 54 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  T.# define SQLIT
17a0: 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53  E_MAX_COMPOUND_S
17b0: 45 4c 45 43 54 20 35 30 30 0a 23 65 6e 64 69 66  ELECT 500.#endif
17c0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69  ../*.** The maxi
17d0: 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70  mum number of op
17e0: 63 6f 64 65 73 20 69 6e 20 61 20 56 44 42 45 20  codes in a VDBE 
17f0: 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 4e 6f 74 20  program..** Not 
1800: 63 75 72 72 65 6e 74 6c 79 20 65 6e 66 6f 72 63  currently enforc
1810: 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ed..*/.#ifndef S
1820: 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f  QLITE_MAX_VDBE_O
1830: 50 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  P.# define SQLIT
1840: 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 20 32 35  E_MAX_VDBE_OP 25
1850: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  000.#endif../*.*
1860: 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  * The maximum nu
1870: 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
1880: 73 20 74 6f 20 61 6e 20 53 51 4c 20 66 75 6e 63  s to an SQL func
1890: 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tion..*/.#ifndef
18a0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43   SQLITE_MAX_FUNC
18b0: 54 49 4f 4e 5f 41 52 47 0a 23 20 64 65 66 69 6e  TION_ARG.# defin
18c0: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e  e SQLITE_MAX_FUN
18d0: 43 54 49 4f 4e 5f 41 52 47 20 31 32 37 0a 23 65  CTION_ARG 127.#e
18e0: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
18f0: 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
1900: 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65  f in-memory page
1910: 73 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65  s to use for the
1920: 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 0a 2a   main database.*
1930: 2a 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20  * table and for 
1940: 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
1950: 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 44 45  .  The SQLITE_DE
1960: 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
1970: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1980: 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45  TE_DEFAULT_CACHE
1990: 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53  _SIZE.# define S
19a0: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41  QLITE_DEFAULT_CA
19b0: 43 48 45 5f 53 49 5a 45 20 20 32 30 30 30 0a 23  CHE_SIZE  2000.#
19c0: 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
19d0: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d  LITE_DEFAULT_TEM
19e0: 50 5f 43 41 43 48 45 5f 53 49 5a 45 0a 23 20 64  P_CACHE_SIZE.# d
19f0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46  efine SQLITE_DEF
1a00: 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f  AULT_TEMP_CACHE_
1a10: 53 49 5a 45 20 20 35 30 30 0a 23 65 6e 64 69 66  SIZE  500.#endif
1a20: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  ../*.** The defa
1a30: 75 6c 74 20 6e 75 6d 62 65 72 20 6f 66 20 66 72  ult number of fr
1a40: 61 6d 65 73 20 74 6f 20 61 63 63 75 6d 75 6c 61  ames to accumula
1a50: 74 65 20 69 6e 20 74 68 65 20 6c 6f 67 20 66 69  te in the log fi
1a60: 6c 65 20 62 65 66 6f 72 65 0a 2a 2a 20 63 68 65  le before.** che
1a70: 63 6b 70 6f 69 6e 74 69 6e 67 20 74 68 65 20 64  ckpointing the d
1a80: 61 74 61 62 61 73 65 20 69 6e 20 57 41 4c 20 6d  atabase in WAL m
1a90: 6f 64 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ode..*/.#ifndef 
1aa0: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 57  SQLITE_DEFAULT_W
1ab0: 41 4c 5f 41 55 54 4f 43 48 45 43 4b 50 4f 49 4e  AL_AUTOCHECKPOIN
1ac0: 54 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  T.# define SQLIT
1ad0: 45 5f 44 45 46 41 55 4c 54 5f 57 41 4c 5f 41 55  E_DEFAULT_WAL_AU
1ae0: 54 4f 43 48 45 43 4b 50 4f 49 4e 54 20 20 31 30  TOCHECKPOINT  10
1af0: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  00.#endif../*.**
1b00: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   The maximum num
1b10: 62 65 72 20 6f 66 20 61 74 74 61 63 68 65 64 20  ber of attached 
1b20: 64 61 74 61 62 61 73 65 73 2e 20 20 54 68 69 73  databases.  This
1b30: 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e   must be between
1b40: 20 30 0a 2a 2a 20 61 6e 64 20 36 32 2e 20 20 54   0.** and 62.  T
1b50: 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f  he upper bound o
1b60: 6e 20 36 32 20 69 73 20 62 65 63 61 75 73 65 20  n 62 is because 
1b70: 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  a 64-bit integer
1b80: 20 62 69 74 6d 61 70 0a 2a 2a 20 69 73 20 75 73   bitmap.** is us
1b90: 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f  ed internally to
1ba0: 20 74 72 61 63 6b 20 61 74 74 61 63 68 65 64 20   track attached 
1bb0: 64 61 74 61 62 61 73 65 73 2e 0a 2a 2f 0a 23 69  databases..*/.#i
1bc0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
1bd0: 5f 41 54 54 41 43 48 45 44 0a 23 20 64 65 66 69  _ATTACHED.# defi
1be0: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54  ne SQLITE_MAX_AT
1bf0: 54 41 43 48 45 44 20 31 30 0a 23 65 6e 64 69 66  TACHED 10.#endif
1c00: 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78  .../*.** The max
1c10: 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 61 20  imum value of a 
1c20: 3f 6e 6e 6e 20 77 69 6c 64 63 61 72 64 20 74 68  ?nnn wildcard th
1c30: 61 74 20 74 68 65 20 70 61 72 73 65 72 20 77 69  at the parser wi
1c40: 6c 6c 20 61 63 63 65 70 74 2e 0a 2a 2f 0a 23 69  ll accept..*/.#i
1c50: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
1c60: 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
1c70: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
1c80: 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55  _MAX_VARIABLE_NU
1c90: 4d 42 45 52 20 39 39 39 0a 23 65 6e 64 69 66 0a  MBER 999.#endif.
1ca0: 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 70 61 67 65  ./* Maximum page
1cb0: 20 73 69 7a 65 2e 20 20 54 68 65 20 75 70 70 65   size.  The uppe
1cc0: 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 20  r bound on this 
1cd0: 76 61 6c 75 65 20 69 73 20 36 35 35 33 36 2e 20  value is 65536. 
1ce0: 20 54 68 69 73 20 61 20 6c 69 6d 69 74 0a 2a 2a   This a limit.**
1cf0: 20 69 6d 70 6f 73 65 64 20 62 79 20 74 68 65 20   imposed by the 
1d00: 75 73 65 20 6f 66 20 31 36 2d 62 69 74 20 6f 66  use of 16-bit of
1d10: 66 73 65 74 73 20 77 69 74 68 69 6e 20 65 61 63  fsets within eac
1d20: 68 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 45 61  h page..**.** Ea
1d30: 72 6c 69 65 72 20 76 65 72 73 69 6f 6e 73 20 6f  rlier versions o
1d40: 66 20 53 51 4c 69 74 65 20 61 6c 6c 6f 77 65 64  f SQLite allowed
1d50: 20 74 68 65 20 75 73 65 72 20 74 6f 20 63 68 61   the user to cha
1d60: 6e 67 65 20 74 68 69 73 20 76 61 6c 75 65 20 61  nge this value a
1d70: 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 20 74 69 6d  t.** compile tim
1d80: 65 2e 20 54 68 69 73 20 69 73 20 6e 6f 20 6c 6f  e. This is no lo
1d90: 6e 67 65 72 20 70 65 72 6d 69 74 74 65 64 2c 20  nger permitted, 
1da0: 6f 6e 20 74 68 65 20 67 72 6f 75 6e 64 73 20 74  on the grounds t
1db0: 68 61 74 20 69 74 20 63 72 65 61 74 65 73 0a 2a  hat it creates.*
1dc0: 2a 20 61 20 6c 69 62 72 61 72 79 20 74 68 61 74  * a library that
1dd0: 20 69 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 20   is technically 
1de0: 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74  incompatible wit
1df0: 68 20 61 6e 20 53 51 4c 69 74 65 20 6c 69 62 72  h an SQLite libr
1e00: 61 72 79 20 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64  ary .** compiled
1e10: 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e   with a differen
1e20: 74 20 6c 69 6d 69 74 2e 20 49 66 20 61 20 70 72  t limit. If a pr
1e30: 6f 63 65 73 73 20 6f 70 65 72 61 74 69 6e 67 20  ocess operating 
1e40: 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 0a 2a  on a database .*
1e50: 2a 20 77 69 74 68 20 61 20 70 61 67 65 2d 73 69  * with a page-si
1e60: 7a 65 20 6f 66 20 36 35 35 33 36 20 62 79 74 65  ze of 65536 byte
1e70: 73 20 63 72 61 73 68 65 73 2c 20 74 68 65 6e 20  s crashes, then 
1e80: 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 53  an instance of S
1e90: 51 4c 69 74 65 20 0a 2a 2a 20 63 6f 6d 70 69 6c  QLite .** compil
1ea0: 65 64 20 77 69 74 68 20 74 68 65 20 64 65 66 61  ed with the defa
1eb0: 75 6c 74 20 70 61 67 65 2d 73 69 7a 65 20 6c 69  ult page-size li
1ec0: 6d 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  mit will not be 
1ed0: 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b  able to rollback
1ee0: 20 0a 2a 2a 20 74 68 65 20 61 62 6f 72 74 65 64   .** the aborted
1ef0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68   transaction. Th
1f00: 69 73 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f  is could lead to
1f10: 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
1f20: 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  tion..*/.#ifdef 
1f30: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
1f40: 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 4c  SIZE.# undef SQL
1f50: 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a  ITE_MAX_PAGE_SIZ
1f60: 45 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65  E.#endif.#define
1f70: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45   SQLITE_MAX_PAGE
1f80: 5f 53 49 5a 45 20 36 35 35 33 36 0a 0a 0a 2f 2a  _SIZE 65536.../*
1f90: 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20  .** The default 
1fa0: 73 69 7a 65 20 6f 66 20 61 20 64 61 74 61 62 61  size of a databa
1fb0: 73 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e  se page..*/.#ifn
1fc0: 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  def SQLITE_DEFAU
1fd0: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64  LT_PAGE_SIZE.# d
1fe0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46  efine SQLITE_DEF
1ff0: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 31  AULT_PAGE_SIZE 1
2000: 30 32 34 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  024.#endif.#if S
2010: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41  QLITE_DEFAULT_PA
2020: 47 45 5f 53 49 5a 45 3e 53 51 4c 49 54 45 5f 4d  GE_SIZE>SQLITE_M
2030: 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 75  AX_PAGE_SIZE.# u
2040: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
2050: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20  ULT_PAGE_SIZE.# 
2060: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
2070: 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20  FAULT_PAGE_SIZE 
2080: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
2090: 53 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  SIZE.#endif../*.
20a0: 2a 2a 20 4f 72 64 69 6e 61 72 69 6c 79 2c 20 69  ** Ordinarily, i
20b0: 66 20 6e 6f 20 76 61 6c 75 65 20 69 73 20 65 78  f no value is ex
20c0: 70 6c 69 63 69 74 6c 79 20 70 72 6f 76 69 64 65  plicitly provide
20d0: 64 2c 20 53 51 4c 69 74 65 20 63 72 65 61 74 65  d, SQLite create
20e0: 73 20 64 61 74 61 62 61 73 65 73 0a 2a 2a 20 77  s databases.** w
20f0: 69 74 68 20 70 61 67 65 20 73 69 7a 65 20 53 51  ith page size SQ
2100: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47  LITE_DEFAULT_PAG
2110: 45 5f 53 49 5a 45 2e 20 48 6f 77 65 76 65 72 2c  E_SIZE. However,
2120: 20 62 61 73 65 64 20 6f 6e 20 63 65 72 74 61 69   based on certai
2130: 6e 0a 2a 2a 20 64 65 76 69 63 65 20 63 68 61 72  n.** device char
2140: 61 63 74 65 72 69 73 74 69 63 73 20 28 73 65 63  acteristics (sec
2150: 74 6f 72 2d 73 69 7a 65 20 61 6e 64 20 61 74 6f  tor-size and ato
2160: 6d 69 63 20 77 72 69 74 65 28 29 20 73 75 70 70  mic write() supp
2170: 6f 72 74 29 2c 0a 2a 2a 20 53 51 4c 69 74 65 20  ort),.** SQLite 
2180: 6d 61 79 20 63 68 6f 6f 73 65 20 61 20 6c 61 72  may choose a lar
2190: 67 65 72 20 76 61 6c 75 65 2e 20 54 68 69 73 20  ger value. This 
21a0: 63 6f 6e 73 74 61 6e 74 20 69 73 20 74 68 65 20  constant is the 
21b0: 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 0a 2a 2a  maximum value.**
21c0: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 63 68 6f   SQLite will cho
21d0: 6f 73 65 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 0a  ose on its own..
21e0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
21f0: 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41  E_MAX_DEFAULT_PA
2200: 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65  GE_SIZE.# define
2210: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41   SQLITE_MAX_DEFA
2220: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 38 31  ULT_PAGE_SIZE 81
2230: 39 32 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  92.#endif.#if SQ
2240: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54  LITE_MAX_DEFAULT
2250: 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51 4c 49 54  _PAGE_SIZE>SQLIT
2260: 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a  E_MAX_PAGE_SIZE.
2270: 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  # undef SQLITE_M
2280: 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
2290: 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SIZE.# define SQ
22a0: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54  LITE_MAX_DEFAULT
22b0: 5f 50 41 47 45 5f 53 49 5a 45 20 53 51 4c 49 54  _PAGE_SIZE SQLIT
22c0: 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a  E_MAX_PAGE_SIZE.
22d0: 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d  #endif.../*.** M
22e0: 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
22f0: 20 70 61 67 65 73 20 69 6e 20 6f 6e 65 20 64 61   pages in one da
2300: 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
2310: 2a 2a 20 54 68 69 73 20 69 73 20 72 65 61 6c 6c  ** This is reall
2320: 79 20 6a 75 73 74 20 74 68 65 20 64 65 66 61 75  y just the defau
2330: 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
2340: 20 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 20   max_page_count 
2350: 70 72 61 67 6d 61 2e 0a 2a 2a 20 54 68 69 73 20  pragma..** This 
2360: 76 61 6c 75 65 20 63 61 6e 20 62 65 20 6c 6f 77  value can be low
2370: 65 72 65 64 20 28 6f 72 20 72 61 69 73 65 64 29  ered (or raised)
2380: 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73 69   at run-time usi
2390: 6e 67 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 6d  ng that the.** m
23a0: 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 20 6d 61  ax_page_count ma
23b0: 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  cro..*/.#ifndef 
23c0: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
23d0: 43 4f 55 4e 54 0a 23 20 64 65 66 69 6e 65 20 53  COUNT.# define S
23e0: 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43  QLITE_MAX_PAGE_C
23f0: 4f 55 4e 54 20 31 30 37 33 37 34 31 38 32 33 0a  OUNT 1073741823.
2400: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61  #endif../*.** Ma
2410: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 28 69 6e  ximum length (in
2420: 20 62 79 74 65 73 29 20 6f 66 20 74 68 65 20 70   bytes) of the p
2430: 61 74 74 65 72 6e 20 69 6e 20 61 20 4c 49 4b 45  attern in a LIKE
2440: 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70 65 72   or GLOB.** oper
2450: 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  ator..*/.#ifndef
2460: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45   SQLITE_MAX_LIKE
2470: 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 0a  _PATTERN_LENGTH.
2480: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
2490: 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MAX_LIKE_PATTERN
24a0: 5f 4c 45 4e 47 54 48 20 35 30 30 30 30 0a 23 65  _LENGTH 50000.#e
24b0: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69  ndif../*.** Maxi
24c0: 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 72 65 63  mum depth of rec
24d0: 75 72 73 69 6f 6e 20 66 6f 72 20 74 72 69 67 67  ursion for trigg
24e0: 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 6c  ers..**.** A val
24f0: 75 65 20 6f 66 20 31 20 6d 65 61 6e 73 20 74 68  ue of 1 means th
2500: 61 74 20 61 20 74 72 69 67 67 65 72 20 70 72 6f  at a trigger pro
2510: 67 72 61 6d 20 77 69 6c 6c 20 6e 6f 74 20 62 65  gram will not be
2520: 20 61 62 6c 65 20 74 6f 20 69 74 73 65 6c 66 0a   able to itself.
2530: 2a 2a 20 66 69 72 65 20 61 6e 79 20 74 72 69 67  ** fire any trig
2540: 67 65 72 73 2e 20 41 20 76 61 6c 75 65 20 6f 66  gers. A value of
2550: 20 30 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f   0 means that no
2560: 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
2570: 73 20 61 74 20 61 6c 6c 20 0a 2a 2a 20 6d 61 79  s at all .** may
2580: 20 62 65 20 65 78 65 63 75 74 65 64 2e 0a 2a 2f   be executed..*/
2590: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
25a0: 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 45 50 54  MAX_TRIGGER_DEPT
25b0: 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  H.# define SQLIT
25c0: 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 45  E_MAX_TRIGGER_DE
25d0: 50 54 48 20 31 30 30 30 0a 23 65 6e 64 69 66 0a  PTH 1000.#endif.
25e0: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
25f0: 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 4c 69   End of sqliteLi
2600: 6d 69 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mit.h **********
2610: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2620: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2630: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2640: 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
2650: 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
2660: 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
2670: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2680: 0a 0a 2f 2a 20 44 69 73 61 62 6c 65 20 6e 75 69  ../* Disable nui
2690: 73 61 6e 63 65 20 77 61 72 6e 69 6e 67 73 20 6f  sance warnings o
26a0: 6e 20 42 6f 72 6c 61 6e 64 20 63 6f 6d 70 69 6c  n Borland compil
26b0: 65 72 73 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e  ers */.#if defin
26c0: 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29  ed(__BORLANDC__)
26d0: 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 72  .#pragma warn -r
26e0: 63 68 20 2f 2a 20 75 6e 72 65 61 63 68 61 62 6c  ch /* unreachabl
26f0: 65 20 63 6f 64 65 20 2a 2f 0a 23 70 72 61 67 6d  e code */.#pragm
2700: 61 20 77 61 72 6e 20 2d 63 63 63 20 2f 2a 20 43  a warn -ccc /* C
2710: 6f 6e 64 69 74 69 6f 6e 20 69 73 20 61 6c 77 61  ondition is alwa
2720: 79 73 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65  ys true or false
2730: 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e   */.#pragma warn
2740: 20 2d 61 75 73 20 2f 2a 20 41 73 73 69 67 6e 65   -aus /* Assigne
2750: 64 20 76 61 6c 75 65 20 69 73 20 6e 65 76 65 72  d value is never
2760: 20 75 73 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61   used */.#pragma
2770: 20 77 61 72 6e 20 2d 63 73 75 20 2f 2a 20 43 6f   warn -csu /* Co
2780: 6d 70 61 72 69 6e 67 20 73 69 67 6e 65 64 20 61  mparing signed a
2790: 6e 64 20 75 6e 73 69 67 6e 65 64 20 2a 2f 0a 23  nd unsigned */.#
27a0: 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 73 70 61  pragma warn -spa
27b0: 20 2f 2a 20 53 75 73 70 69 63 69 6f 75 73 20 70   /* Suspicious p
27c0: 6f 69 6e 74 65 72 20 61 72 69 74 68 6d 65 74 69  ointer arithmeti
27d0: 63 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20  c */.#endif../* 
27e0: 4e 65 65 64 65 64 20 66 6f 72 20 76 61 72 69 6f  Needed for vario
27f0: 75 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 2e  us definitions..
2800: 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 47 4e  . */.#ifndef _GN
2810: 55 5f 53 4f 55 52 43 45 0a 23 20 64 65 66 69 6e  U_SOURCE.# defin
2820: 65 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0a 23 65  e _GNU_SOURCE.#e
2830: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c  ndif../*.** Incl
2840: 75 64 65 20 73 74 61 6e 64 61 72 64 20 68 65 61  ude standard hea
2850: 64 65 72 20 66 69 6c 65 73 20 61 73 20 6e 65 63  der files as nec
2860: 65 73 73 61 72 79 0a 2a 2f 0a 23 69 66 64 65 66  essary.*/.#ifdef
2870: 20 48 41 56 45 5f 53 54 44 49 4e 54 5f 48 0a 23   HAVE_STDINT_H.#
2880: 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6e 74 2e  include <stdint.
2890: 68 3e 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  h>.#endif.#ifdef
28a0: 20 48 41 56 45 5f 49 4e 54 54 59 50 45 53 5f 48   HAVE_INTTYPES_H
28b0: 0a 23 69 6e 63 6c 75 64 65 20 3c 69 6e 74 74 79  .#include <intty
28c0: 70 65 73 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f  pes.h>.#endif../
28d0: 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
28e0: 6e 67 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73  ng macros are us
28f0: 65 64 20 74 6f 20 63 61 73 74 20 70 6f 69 6e 74  ed to cast point
2900: 65 72 73 20 74 6f 20 69 6e 74 65 67 65 72 73 20  ers to integers 
2910: 61 6e 64 0a 2a 2a 20 69 6e 74 65 67 65 72 73 20  and.** integers 
2920: 74 6f 20 70 6f 69 6e 74 65 72 73 2e 20 20 54 68  to pointers.  Th
2930: 65 20 77 61 79 20 79 6f 75 20 64 6f 20 74 68 69  e way you do thi
2940: 73 20 76 61 72 69 65 73 20 66 72 6f 6d 20 6f 6e  s varies from on
2950: 65 20 63 6f 6d 70 69 6c 65 72 0a 2a 2a 20 74 6f  e compiler.** to
2960: 20 74 68 65 20 6e 65 78 74 2c 20 73 6f 20 77 65   the next, so we
2970: 20 68 61 76 65 20 64 65 76 65 6c 6f 70 65 64 20   have developed 
2980: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65  the following se
2990: 74 20 6f 66 20 23 69 66 20 73 74 61 74 65 6d 65  t of #if stateme
29a0: 6e 74 73 0a 2a 2a 20 74 6f 20 67 65 6e 65 72 61  nts.** to genera
29b0: 74 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6d  te appropriate m
29c0: 61 63 72 6f 73 20 66 6f 72 20 61 20 77 69 64 65  acros for a wide
29d0: 20 72 61 6e 67 65 20 6f 66 20 63 6f 6d 70 69 6c   range of compil
29e0: 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ers..**.** The c
29f0: 6f 72 72 65 63 74 20 22 41 4e 53 49 22 20 77 61  orrect "ANSI" wa
2a00: 79 20 74 6f 20 64 6f 20 74 68 69 73 20 69 73 20  y to do this is 
2a10: 74 6f 20 75 73 65 20 74 68 65 20 69 6e 74 70 74  to use the intpt
2a20: 72 5f 74 20 74 79 70 65 2e 20 0a 2a 2a 20 55 6e  r_t type. .** Un
2a30: 66 6f 72 74 75 6e 61 74 65 6c 79 2c 20 74 68 61  fortunately, tha
2a40: 74 20 74 79 70 65 64 65 66 20 69 73 20 6e 6f 74  t typedef is not
2a50: 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 61 6c   available on al
2a60: 6c 20 63 6f 6d 70 69 6c 65 72 73 2c 20 6f 72 0a  l compilers, or.
2a70: 2a 2a 20 69 66 20 69 74 20 69 73 20 61 76 61 69  ** if it is avai
2a80: 6c 61 62 6c 65 2c 20 69 74 20 72 65 71 75 69 72  lable, it requir
2a90: 65 73 20 61 6e 20 23 69 6e 63 6c 75 64 65 20 6f  es an #include o
2aa0: 66 20 73 70 65 63 69 66 69 63 20 68 65 61 64 65  f specific heade
2ab0: 72 73 0a 2a 2a 20 74 68 61 74 20 76 61 72 79 20  rs.** that vary 
2ac0: 66 72 6f 6d 20 6f 6e 65 20 6d 61 63 68 69 6e 65  from one machine
2ad0: 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a   to the next..**
2ae0: 0a 2a 2a 20 54 69 63 6b 65 74 20 23 33 38 36 30  .** Ticket #3860
2af0: 3a 20 20 54 68 65 20 6c 6c 76 6d 2d 67 63 63 2d  :  The llvm-gcc-
2b00: 34 2e 32 20 63 6f 6d 70 69 6c 65 72 20 66 72 6f  4.2 compiler fro
2b10: 6d 20 41 70 70 6c 65 20 63 68 6f 6b 65 73 20 6f  m Apple chokes o
2b20: 6e 0a 2a 2a 20 74 68 65 20 28 28 76 6f 69 64 2a  n.** the ((void*
2b30: 29 26 28 28 63 68 61 72 2a 29 30 29 5b 58 5d 29  )&((char*)0)[X])
2b40: 20 63 6f 6e 73 74 72 75 63 74 2e 20 20 42 75 74   construct.  But
2b50: 20 4d 53 56 43 20 63 68 6f 6b 65 73 20 6f 6e 20   MSVC chokes on 
2b60: 28 28 76 6f 69 64 2a 29 28 58 29 29 2e 0a 2a 2a  ((void*)(X))..**
2b70: 20 53 6f 20 77 65 20 68 61 76 65 20 74 6f 20 64   So we have to d
2b80: 65 66 69 6e 65 20 74 68 65 20 6d 61 63 72 6f 73  efine the macros
2b90: 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 77 61   in different wa
2ba0: 79 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  ys depending on 
2bb0: 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 2e  the.** compiler.
2bc0: 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
2bd0: 5f 5f 50 54 52 44 49 46 46 5f 54 59 50 45 5f 5f  __PTRDIFF_TYPE__
2be0: 29 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20  )  /* This case 
2bf0: 73 68 6f 75 6c 64 20 77 6f 72 6b 20 66 6f 72 20  should work for 
2c00: 47 43 43 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20  GCC */.# define 
2c10: 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54  SQLITE_INT_TO_PT
2c20: 52 28 58 29 20 20 28 28 76 6f 69 64 2a 29 28 5f  R(X)  ((void*)(_
2c30: 5f 50 54 52 44 49 46 46 5f 54 59 50 45 5f 5f 29  _PTRDIFF_TYPE__)
2c40: 28 58 29 29 0a 23 20 64 65 66 69 6e 65 20 53 51  (X)).# define SQ
2c50: 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28  LITE_PTR_TO_INT(
2c60: 58 29 20 20 28 28 69 6e 74 29 28 5f 5f 50 54 52  X)  ((int)(__PTR
2c70: 44 49 46 46 5f 54 59 50 45 5f 5f 29 28 58 29 29  DIFF_TYPE__)(X))
2c80: 0a 23 65 6c 69 66 20 21 64 65 66 69 6e 65 64 28  .#elif !defined(
2c90: 5f 5f 47 4e 55 43 5f 5f 29 20 20 20 20 20 20 20  __GNUC__)       
2ca0: 2f 2a 20 57 6f 72 6b 73 20 66 6f 72 20 63 6f 6d  /* Works for com
2cb0: 70 69 6c 65 72 73 20 6f 74 68 65 72 20 74 68 61  pilers other tha
2cc0: 6e 20 4c 4c 56 4d 20 2a 2f 0a 23 20 64 65 66 69  n LLVM */.# defi
2cd0: 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f  ne SQLITE_INT_TO
2ce0: 5f 50 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a  _PTR(X)  ((void*
2cf0: 29 26 28 28 63 68 61 72 2a 29 30 29 5b 58 5d 29  )&((char*)0)[X])
2d00: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
2d10: 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58 29 20 20  _PTR_TO_INT(X)  
2d20: 28 28 69 6e 74 29 28 28 28 63 68 61 72 2a 29 58  ((int)(((char*)X
2d30: 29 2d 28 63 68 61 72 2a 29 30 29 29 0a 23 65 6c  )-(char*)0)).#el
2d40: 69 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f  if defined(HAVE_
2d50: 53 54 44 49 4e 54 5f 48 29 20 20 20 2f 2a 20 55  STDINT_H)   /* U
2d60: 73 65 20 74 68 69 73 20 63 61 73 65 20 69 66 20  se this case if 
2d70: 77 65 20 68 61 76 65 20 41 4e 53 49 20 68 65 61  we have ANSI hea
2d80: 64 65 72 73 20 2a 2f 0a 23 20 64 65 66 69 6e 65  ders */.# define
2d90: 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50   SQLITE_INT_TO_P
2da0: 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a 29 28  TR(X)  ((void*)(
2db0: 69 6e 74 70 74 72 5f 74 29 28 58 29 29 0a 23 20  intptr_t)(X)).# 
2dc0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54  define SQLITE_PT
2dd0: 52 5f 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69  R_TO_INT(X)  ((i
2de0: 6e 74 29 28 69 6e 74 70 74 72 5f 74 29 28 58 29  nt)(intptr_t)(X)
2df0: 29 0a 23 65 6c 73 65 20 20 20 20 20 20 20 20 20  ).#else         
2e00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e10: 20 2f 2a 20 47 65 6e 65 72 61 74 65 73 20 61 20   /* Generates a 
2e20: 77 61 72 6e 69 6e 67 20 2d 20 62 75 74 20 69 74  warning - but it
2e30: 20 61 6c 77 61 79 73 20 77 6f 72 6b 73 20 2a 2f   always works */
2e40: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
2e50: 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 58 29 20 20  _INT_TO_PTR(X)  
2e60: 28 28 76 6f 69 64 2a 29 28 58 29 29 0a 23 20 64  ((void*)(X)).# d
2e70: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52  efine SQLITE_PTR
2e80: 5f 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69 6e  _TO_INT(X)  ((in
2e90: 74 29 28 58 29 29 0a 23 65 6e 64 69 66 0a 0a 2f  t)(X)).#endif../
2ea0: 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f  *.** The SQLITE_
2eb0: 54 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f  THREADSAFE macro
2ec0: 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64   must be defined
2ed0: 20 61 73 20 30 2c 20 31 2c 20 6f 72 20 32 2e 0a   as 0, 1, or 2..
2ee0: 2a 2a 20 30 20 6d 65 61 6e 73 20 6d 75 74 65 78  ** 0 means mutex
2ef0: 65 73 20 61 72 65 20 70 65 72 6d 61 6e 65 6e 74  es are permanent
2f00: 6c 79 20 64 69 73 61 62 6c 65 20 61 6e 64 20 74  ly disable and t
2f10: 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 6e 65  he library is ne
2f20: 76 65 72 0a 2a 2a 20 74 68 72 65 61 64 73 61 66  ver.** threadsaf
2f30: 65 2e 20 20 31 20 6d 65 61 6e 73 20 74 68 65 20  e.  1 means the 
2f40: 6c 69 62 72 61 72 79 20 69 73 20 73 65 72 69 61  library is seria
2f50: 6c 69 7a 65 64 20 77 68 69 63 68 20 69 73 20 74  lized which is t
2f60: 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20 6c 65  he highest.** le
2f70: 76 65 6c 20 6f 66 20 74 68 72 65 61 64 73 61 66  vel of threadsaf
2f80: 65 74 79 2e 20 20 32 20 6d 65 61 6e 73 20 74 68  ety.  2 means th
2f90: 65 20 6c 69 62 61 72 79 20 69 73 20 6d 75 6c 74  e libary is mult
2fa0: 69 74 68 72 65 61 64 65 64 20 2d 20 6d 75 6c 74  ithreaded - mult
2fb0: 69 70 6c 65 0a 2a 2a 20 74 68 72 65 61 64 73 20  iple.** threads 
2fc0: 63 61 6e 20 75 73 65 20 53 51 4c 69 74 65 20 61  can use SQLite a
2fd0: 73 20 6c 6f 6e 67 20 61 73 20 6e 6f 20 74 77 6f  s long as no two
2fe0: 20 74 68 72 65 61 64 73 20 74 72 79 20 74 6f 20   threads try to 
2ff0: 75 73 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20  use the same.** 
3000: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3010: 69 6f 6e 20 61 74 20 74 68 65 20 73 61 6d 65 20  ion at the same 
3020: 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6c 64 65  time..**.** Olde
3030: 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  r versions of SQ
3040: 4c 69 74 65 20 75 73 65 64 20 61 6e 20 6f 70 74  Lite used an opt
3050: 69 6f 6e 61 6c 20 54 48 52 45 41 44 53 41 46 45  ional THREADSAFE
3060: 20 6d 61 63 72 6f 2e 0a 2a 2a 20 57 65 20 73 75   macro..** We su
3070: 70 70 6f 72 74 20 74 68 61 74 20 66 6f 72 20 6c  pport that for l
3080: 65 67 61 63 79 2e 0a 2a 2f 0a 23 69 66 20 21 64  egacy..*/.#if !d
3090: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48  efined(SQLITE_TH
30a0: 52 45 41 44 53 41 46 45 29 0a 23 69 66 20 64 65  READSAFE).#if de
30b0: 66 69 6e 65 64 28 54 48 52 45 41 44 53 41 46 45  fined(THREADSAFE
30c0: 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ).# define SQLIT
30d0: 45 5f 54 48 52 45 41 44 53 41 46 45 20 54 48 52  E_THREADSAFE THR
30e0: 45 41 44 53 41 46 45 0a 23 65 6c 73 65 0a 23 20  EADSAFE.#else.# 
30f0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48  define SQLITE_TH
3100: 52 45 41 44 53 41 46 45 20 31 20 2f 2a 20 49 4d  READSAFE 1 /* IM
3110: 50 3a 20 52 2d 30 37 32 37 32 2d 32 32 33 30 39  P: R-07272-22309
3120: 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69   */.#endif.#endi
3130: 66 0a 0a 2f 2a 0a 2a 2a 20 50 6f 77 65 72 73 61  f../*.** Powersa
3140: 66 65 20 6f 76 65 72 77 72 69 74 65 20 69 73 20  fe overwrite is 
3150: 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 2e 20 20  on by default.  
3160: 42 75 74 20 63 61 6e 20 62 65 20 74 75 72 6e 65  But can be turne
3170: 64 20 6f 66 66 20 75 73 69 6e 67 0a 2a 2a 20 74  d off using.** t
3180: 68 65 20 2d 44 53 51 4c 49 54 45 5f 50 4f 57 45  he -DSQLITE_POWE
3190: 52 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 3d  RSAFE_OVERWRITE=
31a0: 30 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f  0 command-line o
31b0: 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ption..*/.#ifnde
31c0: 66 20 53 51 4c 49 54 45 5f 50 4f 57 45 52 53 41  f SQLITE_POWERSA
31d0: 46 45 5f 4f 56 45 52 57 52 49 54 45 0a 23 20 64  FE_OVERWRITE.# d
31e0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 4f 57  efine SQLITE_POW
31f0: 45 52 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45  ERSAFE_OVERWRITE
3200: 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   1.#endif../*.**
3210: 20 54 68 65 20 53 51 4c 49 54 45 5f 44 45 46 41   The SQLITE_DEFA
3220: 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 20 6d 61  ULT_MEMSTATUS ma
3230: 63 72 6f 20 6d 75 73 74 20 62 65 20 64 65 66 69  cro must be defi
3240: 6e 65 64 20 61 73 20 65 69 74 68 65 72 20 30 20  ned as either 0 
3250: 6f 72 20 31 2e 0a 2a 2a 20 49 74 20 64 65 74 65  or 1..** It dete
3260: 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f  rmines whether o
3270: 72 20 6e 6f 74 20 74 68 65 20 66 65 61 74 75 72  r not the featur
3280: 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20 0a 2a  es related to .*
3290: 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  * SQLITE_CONFIG_
32a0: 4d 45 4d 53 54 41 54 55 53 20 61 72 65 20 61 76  MEMSTATUS are av
32b0: 61 69 6c 61 62 6c 65 20 62 79 20 64 65 66 61 75  ailable by defau
32c0: 6c 74 20 6f 72 20 6e 6f 74 2e 20 54 68 69 73 20  lt or not. This 
32d0: 76 61 6c 75 65 20 63 61 6e 0a 2a 2a 20 62 65 20  value can.** be 
32e0: 6f 76 65 72 72 69 64 64 65 6e 20 61 74 20 72 75  overridden at ru
32f0: 6e 74 69 6d 65 20 75 73 69 6e 67 20 74 68 65 20  ntime using the 
3300: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
3310: 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 21 64 65   API..*/.#if !de
3320: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 46  fined(SQLITE_DEF
3330: 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 29 0a  AULT_MEMSTATUS).
3340: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
3350: 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55  DEFAULT_MEMSTATU
3360: 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  S 1.#endif../*.*
3370: 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66  * Exactly one of
3380: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
3390: 61 63 72 6f 73 20 6d 75 73 74 20 62 65 20 64 65  acros must be de
33a0: 66 69 6e 65 64 20 69 6e 20 6f 72 64 65 72 20 74  fined in order t
33b0: 6f 0a 2a 2a 20 73 70 65 63 69 66 79 20 77 68 69  o.** specify whi
33c0: 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ch memory alloca
33d0: 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 74  tion subsystem t
33e0: 6f 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  o use..**.**    
33f0: 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d   SQLITE_SYSTEM_M
3400: 41 4c 4c 4f 43 20 20 20 20 20 20 20 20 20 20 2f  ALLOC          /
3410: 2f 20 55 73 65 20 6e 6f 72 6d 61 6c 20 73 79 73  / Use normal sys
3420: 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 0a 2a 2a 20  tem malloc().** 
3430: 20 20 20 20 53 51 4c 49 54 45 5f 57 49 4e 33 32      SQLITE_WIN32
3440: 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 20 20 20 20  _MALLOC         
3450: 20 20 2f 2f 20 55 73 65 20 57 69 6e 33 32 20 6e    // Use Win32 n
3460: 61 74 69 76 65 20 68 65 61 70 20 41 50 49 0a 2a  ative heap API.*
3470: 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 5a 45 52  *     SQLITE_ZER
3480: 4f 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 20 20 20  O_MALLOC        
3490: 20 20 20 20 2f 2f 20 55 73 65 20 61 20 73 74 75      // Use a stu
34a0: 62 20 61 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74  b allocator that
34b0: 20 61 6c 77 61 79 73 20 66 61 69 6c 73 0a 2a 2a   always fails.**
34c0: 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 45 4d 44       SQLITE_MEMD
34d0: 45 42 55 47 20 20 20 20 20 20 20 20 20 20 20 20  EBUG            
34e0: 20 20 20 2f 2f 20 44 65 62 75 67 67 69 6e 67 20     // Debugging 
34f0: 76 65 72 73 69 6f 6e 20 6f 66 20 73 79 73 74 65  version of syste
3500: 6d 20 6d 61 6c 6c 6f 63 28 29 0a 2a 2a 0a 2a 2a  m malloc().**.**
3510: 20 4f 6e 20 57 69 6e 64 6f 77 73 2c 20 69 66 20   On Windows, if 
3520: 74 68 65 20 53 51 4c 49 54 45 5f 57 49 4e 33 32  the SQLITE_WIN32
3530: 5f 4d 41 4c 4c 4f 43 5f 56 41 4c 49 44 41 54 45  _MALLOC_VALIDATE
3540: 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65   macro is define
3550: 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 61 73 73  d and the.** ass
3560: 65 72 74 28 29 20 6d 61 63 72 6f 20 69 73 20 65  ert() macro is e
3570: 6e 61 62 6c 65 64 2c 20 65 61 63 68 20 63 61 6c  nabled, each cal
3580: 6c 20 69 6e 74 6f 20 74 68 65 20 57 69 6e 33 32  l into the Win32
3590: 20 6e 61 74 69 76 65 20 68 65 61 70 20 73 75 62   native heap sub
35a0: 73 79 73 74 65 6d 0a 2a 2a 20 77 69 6c 6c 20 63  system.** will c
35b0: 61 75 73 65 20 48 65 61 70 56 61 6c 69 64 61 74  ause HeapValidat
35c0: 65 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 2e 20  e to be called. 
35d0: 20 49 66 20 68 65 61 70 20 76 61 6c 69 64 61 74   If heap validat
35e0: 69 6f 6e 20 73 68 6f 75 6c 64 20 66 61 69 6c 2c  ion should fail,
35f0: 20 61 6e 0a 2a 2a 20 61 73 73 65 72 74 69 6f 6e   an.** assertion
3600: 20 77 69 6c 6c 20 62 65 20 74 72 69 67 67 65 72   will be trigger
3610: 65 64 2e 0a 2a 2a 0a 2a 2a 20 28 48 69 73 74 6f  ed..**.** (Histo
3620: 72 69 63 61 6c 20 6e 6f 74 65 3a 20 20 54 68 65  rical note:  The
3630: 72 65 20 75 73 65 64 20 74 6f 20 62 65 20 73 65  re used to be se
3640: 76 65 72 61 6c 20 6f 74 68 65 72 20 6f 70 74 69  veral other opti
3650: 6f 6e 73 2c 20 62 75 74 20 77 65 27 76 65 0a 2a  ons, but we've.*
3660: 2a 20 70 61 72 65 64 20 69 74 20 64 6f 77 6e 20  * pared it down 
3670: 74 6f 20 6a 75 73 74 20 74 68 65 73 65 20 74 68  to just these th
3680: 72 65 65 2e 29 0a 2a 2a 0a 2a 2a 20 49 66 20 6e  ree.).**.** If n
3690: 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f 76 65  one of the above
36a0: 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68   are defined, th
36b0: 65 6e 20 73 65 74 20 53 51 4c 49 54 45 5f 53 59  en set SQLITE_SY
36c0: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61 73 0a 2a  STEM_MALLOC as.*
36d0: 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 2a  * the default..*
36e0: 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  /.#if defined(SQ
36f0: 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c  LITE_SYSTEM_MALL
3700: 4f 43 29 20 5c 0a 20 20 2b 20 64 65 66 69 6e 65  OC) \.  + define
3710: 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 4d  d(SQLITE_WIN32_M
3720: 41 4c 4c 4f 43 29 20 5c 0a 20 20 2b 20 64 65 66  ALLOC) \.  + def
3730: 69 6e 65 64 28 53 51 4c 49 54 45 5f 5a 45 52 4f  ined(SQLITE_ZERO
3740: 5f 4d 41 4c 4c 4f 43 29 20 5c 0a 20 20 2b 20 64  _MALLOC) \.  + d
3750: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45  efined(SQLITE_ME
3760: 4d 44 45 42 55 47 29 3e 31 0a 23 20 65 72 72 6f  MDEBUG)>1.# erro
3770: 72 20 22 54 77 6f 20 6f 72 20 6d 6f 72 65 20 6f  r "Two or more o
3780: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
3790: 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e  compile-time con
37a0: 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
37b0: 6e 73 5c 0a 20 61 72 65 20 64 65 66 69 6e 65 64  ns\. are defined
37c0: 20 62 75 74 20 61 74 20 6d 6f 73 74 20 6f 6e 65   but at most one
37d0: 20 69 73 20 61 6c 6c 6f 77 65 64 3a 5c 0a 20 53   is allowed:\. S
37e0: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c  QLITE_SYSTEM_MAL
37f0: 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 57 49 4e 33  LOC, SQLITE_WIN3
3800: 32 5f 4d 41 4c 4c 4f 43 2c 20 53 51 4c 49 54 45  2_MALLOC, SQLITE
3810: 5f 4d 45 4d 44 45 42 55 47 2c 5c 0a 20 53 51 4c  _MEMDEBUG,\. SQL
3820: 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 22  ITE_ZERO_MALLOC"
3830: 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69  .#endif.#if defi
3840: 6e 65 64 28 53 51 4c 49 54 45 5f 53 59 53 54 45  ned(SQLITE_SYSTE
3850: 4d 5f 4d 41 4c 4c 4f 43 29 20 5c 0a 20 20 2b 20  M_MALLOC) \.  + 
3860: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57  defined(SQLITE_W
3870: 49 4e 33 32 5f 4d 41 4c 4c 4f 43 29 20 5c 0a 20  IN32_MALLOC) \. 
3880: 20 2b 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54   + defined(SQLIT
3890: 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 29 20 5c  E_ZERO_MALLOC) \
38a0: 0a 20 20 2b 20 64 65 66 69 6e 65 64 28 53 51 4c  .  + defined(SQL
38b0: 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 3d 3d 30  ITE_MEMDEBUG)==0
38c0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
38d0: 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 31  _SYSTEM_MALLOC 1
38e0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
38f0: 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f  f SQLITE_MALLOC_
3900: 53 4f 46 54 5f 4c 49 4d 49 54 20 69 73 20 6e 6f  SOFT_LIMIT is no
3910: 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 72 79  t zero, then try
3920: 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20   to keep the.** 
3930: 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  sizes of memory 
3940: 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 62 65 6c 6f  allocations belo
3950: 77 20 74 68 69 73 20 76 61 6c 75 65 20 77 68 65  w this value whe
3960: 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a  re possible..*/.
3970: 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
3980: 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f  ITE_MALLOC_SOFT_
3990: 4c 49 4d 49 54 29 0a 23 20 64 65 66 69 6e 65 20  LIMIT).# define 
39a0: 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f  SQLITE_MALLOC_SO
39b0: 46 54 5f 4c 49 4d 49 54 20 31 30 32 34 0a 23 65  FT_LIMIT 1024.#e
39c0: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 6e  ndif../*.** We n
39d0: 65 65 64 20 74 6f 20 64 65 66 69 6e 65 20 5f 58  eed to define _X
39e0: 4f 50 45 4e 5f 53 4f 55 52 43 45 20 61 73 20 66  OPEN_SOURCE as f
39f0: 6f 6c 6c 6f 77 73 20 69 6e 20 6f 72 64 65 72 20  ollows in order 
3a00: 74 6f 20 65 6e 61 62 6c 65 0a 2a 2a 20 72 65 63  to enable.** rec
3a10: 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 6f  ursive mutexes o
3a20: 6e 20 6d 6f 73 74 20 55 6e 69 78 20 73 79 73 74  n most Unix syst
3a30: 65 6d 73 2e 20 20 42 75 74 20 4d 61 63 20 4f 53  ems.  But Mac OS
3a40: 20 58 20 69 73 20 64 69 66 66 65 72 65 6e 74 2e   X is different.
3a50: 0a 2a 2a 20 54 68 65 20 5f 58 4f 50 45 4e 5f 53  .** The _XOPEN_S
3a60: 4f 55 52 43 45 20 64 65 66 69 6e 65 20 63 61 75  OURCE define cau
3a70: 73 65 73 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72  ses problems for
3a80: 20 4d 61 63 20 4f 53 20 58 20 77 65 20 61 72 65   Mac OS X we are
3a90: 20 74 6f 6c 64 2c 0a 2a 2a 20 73 6f 20 69 74 20   told,.** so it 
3aa0: 69 73 20 6f 6d 69 74 74 65 64 20 74 68 65 72 65  is omitted there
3ab0: 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 32  .  See ticket #2
3ac0: 36 37 33 2e 0a 2a 2a 0a 2a 2a 20 4c 61 74 65 72  673..**.** Later
3ad0: 20 77 65 20 6c 65 61 72 6e 20 74 68 61 74 20 5f   we learn that _
3ae0: 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 69 73 20  XOPEN_SOURCE is 
3af0: 70 6f 6f 72 6c 79 20 6f 72 20 69 6e 63 6f 72 72  poorly or incorr
3b00: 65 63 74 6c 79 0a 2a 2a 20 69 6d 70 6c 65 6d 65  ectly.** impleme
3b10: 6e 74 65 64 20 6f 6e 20 73 6f 6d 65 20 73 79 73  nted on some sys
3b20: 74 65 6d 73 2e 20 20 53 6f 20 77 65 20 61 76 6f  tems.  So we avo
3b30: 69 64 20 64 65 66 69 6e 69 6e 67 20 69 74 20 61  id defining it a
3b40: 74 20 61 6c 6c 0a 2a 2a 20 69 66 20 69 74 20 69  t all.** if it i
3b50: 73 20 61 6c 72 65 61 64 79 20 64 65 66 69 6e 65  s already define
3b60: 64 20 6f 72 20 69 66 20 69 74 20 69 73 20 75 6e  d or if it is un
3b70: 6e 65 65 64 65 64 20 62 65 63 61 75 73 65 20 77  needed because w
3b80: 65 20 61 72 65 0a 2a 2a 20 6e 6f 74 20 64 6f 69  e are.** not doi
3b90: 6e 67 20 61 20 74 68 72 65 61 64 73 61 66 65 20  ng a threadsafe 
3ba0: 62 75 69 6c 64 2e 20 20 54 69 63 6b 65 74 20 23  build.  Ticket #
3bb0: 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  2681..**.** See 
3bc0: 61 6c 73 6f 20 74 69 63 6b 65 74 20 23 32 37 34  also ticket #274
3bd0: 31 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  1..*/.#if !defin
3be0: 65 64 28 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45  ed(_XOPEN_SOURCE
3bf0: 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f  ) && !defined(__
3c00: 44 41 52 57 49 4e 5f 5f 29 20 26 26 20 21 64 65  DARWIN__) && !de
3c10: 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
3c20: 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41   && SQLITE_THREA
3c30: 44 53 41 46 45 0a 23 20 20 64 65 66 69 6e 65 20  DSAFE.#  define 
3c40: 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 35 30  _XOPEN_SOURCE 50
3c50: 30 20 20 2f 2a 20 4e 65 65 64 65 64 20 74 6f 20  0  /* Needed to 
3c60: 65 6e 61 62 6c 65 20 70 74 68 72 65 61 64 20 72  enable pthread r
3c70: 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73  ecursive mutexes
3c80: 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   */.#endif../*.*
3c90: 2a 20 54 68 65 20 54 43 4c 20 68 65 61 64 65 72  * The TCL header
3ca0: 73 20 61 72 65 20 6f 6e 6c 79 20 6e 65 65 64 65  s are only neede
3cb0: 64 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67  d when compiling
3cc0: 20 74 68 65 20 54 43 4c 20 62 69 6e 64 69 6e 67   the TCL binding
3cd0: 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  s..*/.#if define
3ce0: 64 28 53 51 4c 49 54 45 5f 54 43 4c 29 20 7c 7c  d(SQLITE_TCL) ||
3cf0: 20 64 65 66 69 6e 65 64 28 54 43 4c 53 48 29 0a   defined(TCLSH).
3d00: 23 20 69 6e 63 6c 75 64 65 20 3c 74 63 6c 2e 68  # include <tcl.h
3d10: 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  >.#endif../*.** 
3d20: 4e 44 45 42 55 47 20 61 6e 64 20 53 51 4c 49 54  NDEBUG and SQLIT
3d30: 45 5f 44 45 42 55 47 20 61 72 65 20 6f 70 70 6f  E_DEBUG are oppo
3d40: 73 69 74 65 73 2e 20 20 49 74 20 73 68 6f 75 6c  sites.  It shoul
3d50: 64 20 61 6c 77 61 79 73 20 62 65 20 74 72 75 65  d always be true
3d60: 20 74 68 61 74 0a 2a 2a 20 64 65 66 69 6e 65 64   that.** defined
3d70: 28 4e 44 45 42 55 47 29 3d 3d 21 64 65 66 69 6e  (NDEBUG)==!defin
3d80: 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
3d90: 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f  .  If this is no
3da0: 74 20 63 75 72 72 65 6e 74 6c 79 20 74 72 75 65  t currently true
3db0: 2c 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 74 72 75  ,.** make it tru
3dc0: 65 20 62 79 20 64 65 66 69 6e 69 6e 67 20 6f 72  e by defining or
3dd0: 20 75 6e 64 65 66 69 6e 69 6e 67 20 4e 44 45 42   undefining NDEB
3de0: 55 47 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e  UG..**.** Settin
3df0: 67 20 4e 44 45 42 55 47 20 6d 61 6b 65 73 20 74  g NDEBUG makes t
3e00: 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c 65 72 20  he code smaller 
3e10: 61 6e 64 20 72 75 6e 20 66 61 73 74 65 72 20 62  and run faster b
3e20: 79 20 64 69 73 61 62 6c 69 6e 67 20 74 68 65 0a  y disabling the.
3e30: 2a 2a 20 6e 75 6d 62 65 72 20 61 73 73 65 72 74  ** number assert
3e40: 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  () statements in
3e50: 20 74 68 65 20 63 6f 64 65 2e 20 20 53 6f 20 77   the code.  So w
3e60: 65 20 77 61 6e 74 20 74 68 65 20 64 65 66 61 75  e want the defau
3e70: 6c 74 20 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20  lt action.** to 
3e80: 62 65 20 66 6f 72 20 4e 44 45 42 55 47 20 74 6f  be for NDEBUG to
3e90: 20 62 65 20 73 65 74 20 61 6e 64 20 4e 44 45 42   be set and NDEB
3ea0: 55 47 20 74 6f 20 62 65 20 75 6e 64 65 66 69 6e  UG to be undefin
3eb0: 65 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54  ed only if SQLIT
3ec0: 45 5f 44 45 42 55 47 0a 2a 2a 20 69 73 20 73 65  E_DEBUG.** is se
3ed0: 74 2e 20 20 54 68 75 73 20 4e 44 45 42 55 47 20  t.  Thus NDEBUG 
3ee0: 62 65 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d 69  becomes an opt-i
3ef0: 6e 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 6e  n rather than an
3f00: 20 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 74   opt-out.** feat
3f10: 75 72 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ure..*/.#if !def
3f20: 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20  ined(NDEBUG) && 
3f30: 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
3f40: 44 45 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65  DEBUG) .# define
3f50: 20 4e 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66   NDEBUG 1.#endif
3f60: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 4e 44 45  .#if defined(NDE
3f70: 42 55 47 29 20 26 26 20 64 65 66 69 6e 65 64 28  BUG) && defined(
3f80: 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 23 20  SQLITE_DEBUG).# 
3f90: 75 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 65 6e  undef NDEBUG.#en
3fa0: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74  dif../*.** The t
3fb0: 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20  estcase() macro 
3fc0: 69 73 20 75 73 65 64 20 74 6f 20 61 69 64 20 69  is used to aid i
3fd0: 6e 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 69  n coverage testi
3fe0: 6e 67 2e 20 20 57 68 65 6e 20 0a 2a 2a 20 64 6f  ng.  When .** do
3ff0: 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73  ing coverage tes
4000: 74 69 6e 67 2c 20 74 68 65 20 63 6f 6e 64 69 74  ting, the condit
4010: 69 6f 6e 20 69 6e 73 69 64 65 20 74 68 65 20 61  ion inside the a
4020: 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 65  rgument to.** te
4030: 73 74 63 61 73 65 28 29 20 6d 75 73 74 20 62 65  stcase() must be
4040: 20 65 76 61 6c 75 61 74 65 64 20 62 6f 74 68 20   evaluated both 
4050: 74 72 75 65 20 61 6e 64 20 66 61 6c 73 65 20 69  true and false i
4060: 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 67 65  n order to.** ge
4070: 74 20 66 75 6c 6c 20 62 72 61 6e 63 68 20 63 6f  t full branch co
4080: 76 65 72 61 67 65 2e 20 20 54 68 65 20 74 65 73  verage.  The tes
4090: 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73  tcase() macro is
40a0: 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 74 6f 20   inserted.** to 
40b0: 68 65 6c 70 20 65 6e 73 75 72 65 20 61 64 65 71  help ensure adeq
40c0: 75 61 74 65 20 74 65 73 74 20 63 6f 76 65 72 61  uate test covera
40d0: 67 65 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65  ge in places whe
40e0: 72 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6e  re simple.** con
40f0: 64 69 74 69 6f 6e 2f 64 65 63 69 73 69 6f 6e 20  dition/decision 
4100: 63 6f 76 65 72 61 67 65 20 69 73 20 69 6e 61 64  coverage is inad
4110: 65 71 75 61 74 65 2e 20 20 46 6f 72 20 65 78 61  equate.  For exa
4120: 6d 70 6c 65 2c 20 74 65 73 74 63 61 73 65 28 29  mple, testcase()
4130: 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20  .** can be used 
4140: 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 62 6f 75  to make sure bou
4150: 6e 64 61 72 79 20 76 61 6c 75 65 73 20 61 72 65  ndary values are
4160: 20 74 65 73 74 65 64 2e 20 20 46 6f 72 0a 2a 2a   tested.  For.**
4170: 20 62 69 74 6d 61 73 6b 20 74 65 73 74 73 2c 20   bitmask tests, 
4180: 74 65 73 74 63 61 73 65 28 29 20 63 61 6e 20 62  testcase() can b
4190: 65 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73  e used to make s
41a0: 75 72 65 20 65 61 63 68 20 62 69 74 0a 2a 2a 20  ure each bit.** 
41b0: 69 73 20 73 69 67 6e 69 66 69 63 61 6e 74 20 61  is significant a
41c0: 6e 64 20 75 73 65 64 20 61 74 20 6c 65 61 73 74  nd used at least
41d0: 20 6f 6e 63 65 2e 20 20 4f 6e 20 73 77 69 74 63   once.  On switc
41e0: 68 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20  h statements.** 
41f0: 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65 20 63  where multiple c
4200: 61 73 65 73 20 67 6f 20 74 6f 20 74 68 65 20 73  ases go to the s
4210: 61 6d 65 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64  ame block of cod
4220: 65 2c 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a  e, testcase().**
4230: 20 63 61 6e 20 69 6e 73 75 72 65 20 74 68 61 74   can insure that
4240: 20 61 6c 6c 20 63 61 73 65 73 20 61 72 65 20 65   all cases are e
4250: 76 61 6c 75 61 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a  valuated..**.*/.
4260: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 4f  #ifdef SQLITE_CO
4270: 56 45 52 41 47 45 5f 54 45 53 54 0a 53 51 4c 49  VERAGE_TEST.SQLI
4280: 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
4290: 64 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67  d sqlite3Coverag
42a0: 65 28 69 6e 74 29 3b 0a 23 20 64 65 66 69 6e 65  e(int);.# define
42b0: 20 74 65 73 74 63 61 73 65 28 58 29 20 20 69 66   testcase(X)  if
42c0: 28 20 58 20 29 7b 20 73 71 6c 69 74 65 33 43 6f  ( X ){ sqlite3Co
42d0: 76 65 72 61 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29  verage(__LINE__)
42e0: 3b 20 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  ; }.#else.# defi
42f0: 6e 65 20 74 65 73 74 63 61 73 65 28 58 29 0a 23  ne testcase(X).#
4300: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
4310: 20 54 45 53 54 4f 4e 4c 59 20 6d 61 63 72 6f 20   TESTONLY macro 
4320: 69 73 20 75 73 65 64 20 74 6f 20 65 6e 63 6c 6f  is used to enclo
4330: 73 65 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c  se variable decl
4340: 61 72 61 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 6f  arations or.** o
4350: 74 68 65 72 20 62 69 74 73 20 6f 66 20 63 6f 64  ther bits of cod
4360: 65 20 74 68 61 74 20 61 72 65 20 6e 65 65 64 65  e that are neede
4370: 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 65  d to support the
4380: 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 77 69   arguments.** wi
4390: 74 68 69 6e 20 74 65 73 74 63 61 73 65 28 29 20  thin testcase() 
43a0: 61 6e 64 20 61 73 73 65 72 74 28 29 20 6d 61 63  and assert() mac
43b0: 72 6f 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ros..*/.#if !def
43c0: 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20  ined(NDEBUG) || 
43d0: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
43e0: 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a 23 20  OVERAGE_TEST).# 
43f0: 64 65 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59 28  define TESTONLY(
4400: 58 29 20 20 58 0a 23 65 6c 73 65 0a 23 20 64 65  X)  X.#else.# de
4410: 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58 29  fine TESTONLY(X)
4420: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53  .#endif../*.** S
4430: 6f 6d 65 74 69 6d 65 73 20 77 65 20 6e 65 65 64  ometimes we need
4440: 20 61 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20   a small amount 
4450: 6f 66 20 63 6f 64 65 20 73 75 63 68 20 61 73 20  of code such as 
4460: 61 20 76 61 72 69 61 62 6c 65 20 69 6e 69 74 69  a variable initi
4470: 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20  alization.** to 
4480: 73 65 74 75 70 20 66 6f 72 20 61 20 6c 61 74 65  setup for a late
4490: 72 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  r assert() state
44a0: 6d 65 6e 74 2e 20 20 57 65 20 64 6f 20 6e 6f 74  ment.  We do not
44b0: 20 77 61 6e 74 20 74 68 69 73 20 63 6f 64 65 20   want this code 
44c0: 74 6f 0a 2a 2a 20 61 70 70 65 61 72 20 77 68 65  to.** appear whe
44d0: 6e 20 61 73 73 65 72 74 28 29 20 69 73 20 64 69  n assert() is di
44e0: 73 61 62 6c 65 64 2e 20 20 54 68 65 20 66 6f 6c  sabled.  The fol
44f0: 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20  lowing macro is 
4500: 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 75 73 65  therefore.** use
4510: 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 61  d to contain tha
4520: 74 20 73 65 74 75 70 20 63 6f 64 65 2e 20 20 54  t setup code.  T
4530: 68 65 20 22 56 56 41 22 20 61 63 72 6f 6e 79 6d  he "VVA" acronym
4540: 20 73 74 61 6e 64 73 20 66 6f 72 0a 2a 2a 20 22   stands for.** "
4550: 56 65 72 69 66 69 63 61 74 69 6f 6e 2c 20 56 61  Verification, Va
4560: 6c 69 64 61 74 69 6f 6e 2c 20 61 6e 64 20 41 63  lidation, and Ac
4570: 63 72 65 64 69 74 61 74 69 6f 6e 22 2e 20 20 49  creditation".  I
4580: 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74  n other words, t
4590: 68 65 0a 2a 2a 20 63 6f 64 65 20 77 69 74 68 69  he.** code withi
45a0: 6e 20 56 56 41 5f 4f 4e 4c 59 28 29 20 77 69 6c  n VVA_ONLY() wil
45b0: 6c 20 6f 6e 6c 79 20 72 75 6e 20 64 75 72 69 6e  l only run durin
45c0: 67 20 76 65 72 69 66 69 63 61 74 69 6f 6e 20 70  g verification p
45d0: 72 6f 63 65 73 73 65 73 2e 0a 2a 2f 0a 23 69 66  rocesses..*/.#if
45e0: 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 20 64 65  ndef NDEBUG.# de
45f0: 66 69 6e 65 20 56 56 41 5f 4f 4e 4c 59 28 58 29  fine VVA_ONLY(X)
4600: 20 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69    X.#else.# defi
4610: 6e 65 20 56 56 41 5f 4f 4e 4c 59 28 58 29 0a 23  ne VVA_ONLY(X).#
4620: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
4630: 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45   ALWAYS and NEVE
4640: 52 20 6d 61 63 72 6f 73 20 73 75 72 72 6f 75 6e  R macros surroun
4650: 64 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73  d boolean expres
4660: 73 69 6f 6e 73 20 77 68 69 63 68 20 0a 2a 2a 20  sions which .** 
4670: 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20  are intended to 
4680: 61 6c 77 61 79 73 20 62 65 20 74 72 75 65 20 6f  always be true o
4690: 72 20 66 61 6c 73 65 2c 20 72 65 73 70 65 63 74  r false, respect
46a0: 69 76 65 6c 79 2e 20 20 53 75 63 68 0a 2a 2a 20  ively.  Such.** 
46b0: 65 78 70 72 65 73 73 69 6f 6e 73 20 63 6f 75 6c  expressions coul
46c0: 64 20 62 65 20 6f 6d 69 74 74 65 64 20 66 72 6f  d be omitted fro
46d0: 6d 20 74 68 65 20 63 6f 64 65 20 63 6f 6d 70 6c  m the code compl
46e0: 65 74 65 6c 79 2e 20 20 42 75 74 20 74 68 65 79  etely.  But they
46f0: 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64  .** are included
4700: 20 69 6e 20 61 20 66 65 77 20 63 61 73 65 73 20   in a few cases 
4710: 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 68 61  in order to enha
4720: 6e 63 65 20 74 68 65 20 72 65 73 69 6c 69 65 6e  nce the resilien
4730: 63 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20  ce.** of SQLite 
4740: 74 6f 20 75 6e 65 78 70 65 63 74 65 64 20 62 65  to unexpected be
4750: 68 61 76 69 6f 72 20 2d 20 74 6f 20 6d 61 6b 65  havior - to make
4760: 20 74 68 65 20 63 6f 64 65 20 22 73 65 6c 66 2d   the code "self-
4770: 68 65 61 6c 69 6e 67 22 0a 2a 2a 20 6f 72 20 22  healing".** or "
4780: 64 75 63 74 69 6c 65 22 20 72 61 74 68 65 72 20  ductile" rather 
4790: 74 68 61 6e 20 62 65 69 6e 67 20 22 62 72 69 74  than being "brit
47a0: 74 6c 65 22 20 61 6e 64 20 63 72 61 73 68 69 6e  tle" and crashin
47b0: 67 20 61 74 20 74 68 65 20 66 69 72 73 74 0a 2a  g at the first.*
47c0: 2a 20 68 69 6e 74 20 6f 66 20 75 6e 70 6c 61 6e  * hint of unplan
47d0: 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a  ned behavior..**
47e0: 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  .** In other wor
47f0: 64 73 2c 20 41 4c 57 41 59 53 20 61 6e 64 20 4e  ds, ALWAYS and N
4800: 45 56 45 52 20 61 72 65 20 61 64 64 65 64 20 66  EVER are added f
4810: 6f 72 20 64 65 66 65 6e 73 69 76 65 20 63 6f 64  or defensive cod
4820: 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 64 6f  e..**.** When do
4830: 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73  ing coverage tes
4840: 74 69 6e 67 20 41 4c 57 41 59 53 20 61 6e 64 20  ting ALWAYS and 
4850: 4e 45 56 45 52 20 61 72 65 20 68 61 72 64 2d 63  NEVER are hard-c
4860: 6f 64 65 64 20 74 6f 0a 2a 2a 20 62 65 20 74 72  oded to.** be tr
4870: 75 65 20 61 6e 64 20 66 61 6c 73 65 20 73 6f 20  ue and false so 
4880: 74 68 61 74 20 74 68 65 20 75 6e 72 65 61 63 68  that the unreach
4890: 61 62 6c 65 20 63 6f 64 65 20 74 68 65 6e 20 73  able code then s
48a0: 70 65 63 69 66 79 20 77 69 6c 6c 0a 2a 2a 20 6e  pecify will.** n
48b0: 6f 74 20 62 65 20 63 6f 75 6e 74 65 64 20 61 73  ot be counted as
48c0: 20 75 6e 74 65 73 74 65 64 20 63 6f 64 65 2e 0a   untested code..
48d0: 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
48e0: 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54  QLITE_COVERAGE_T
48f0: 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20 41 4c  EST).# define AL
4900: 57 41 59 53 28 58 29 20 20 20 20 20 20 28 31 29  WAYS(X)      (1)
4910: 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52 28  .# define NEVER(
4920: 58 29 20 20 20 20 20 20 20 28 30 29 0a 23 65 6c  X)       (0).#el
4930: 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  if !defined(NDEB
4940: 55 47 29 0a 23 20 64 65 66 69 6e 65 20 41 4c 57  UG).# define ALW
4950: 41 59 53 28 58 29 20 20 20 20 20 20 28 28 58 29  AYS(X)      ((X)
4960: 3f 31 3a 28 61 73 73 65 72 74 28 30 29 2c 30 29  ?1:(assert(0),0)
4970: 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52  ).# define NEVER
4980: 28 58 29 20 20 20 20 20 20 20 28 28 58 29 3f 28  (X)       ((X)?(
4990: 61 73 73 65 72 74 28 30 29 2c 31 29 3a 30 29 0a  assert(0),1):0).
49a0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 41  #else.# define A
49b0: 4c 57 41 59 53 28 58 29 20 20 20 20 20 20 28 58  LWAYS(X)      (X
49c0: 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52  ).# define NEVER
49d0: 28 58 29 20 20 20 20 20 20 20 28 58 29 0a 23 65  (X)       (X).#e
49e0: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
49f0: 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72  rn true (non-zer
4a00: 6f 29 20 69 66 20 74 68 65 20 69 6e 70 75 74 20  o) if the input 
4a10: 69 73 20 61 20 69 6e 74 65 67 65 72 20 74 68 61  is a integer tha
4a20: 74 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 0a 2a  t is too large.*
4a30: 2a 20 74 6f 20 66 69 74 20 69 6e 20 33 32 2d 62  * to fit in 32-b
4a40: 69 74 73 2e 20 20 54 68 69 73 20 6d 61 63 72 6f  its.  This macro
4a50: 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 20   is used inside 
4a60: 6f 66 20 76 61 72 69 6f 75 73 20 74 65 73 74 63  of various testc
4a70: 61 73 65 28 29 0a 2a 2a 20 6d 61 63 72 6f 73 20  ase().** macros 
4a80: 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 77  to verify that w
4a90: 65 20 68 61 76 65 20 74 65 73 74 65 64 20 53 51  e have tested SQ
4aa0: 4c 69 74 65 20 66 6f 72 20 6c 61 72 67 65 2d 66  Lite for large-f
4ab0: 69 6c 65 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a  ile support..*/.
4ac0: 23 64 65 66 69 6e 65 20 49 53 5f 42 49 47 5f 49  #define IS_BIG_I
4ad0: 4e 54 28 58 29 20 20 28 28 28 58 29 26 7e 28 69  NT(X)  (((X)&~(i
4ae0: 36 34 29 30 78 66 66 66 66 66 66 66 66 29 21 3d  64)0xffffffff)!=
4af0: 30 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  0)../*.** The ma
4b00: 63 72 6f 20 75 6e 6c 69 6b 65 6c 79 28 29 20 69  cro unlikely() i
4b10: 73 20 61 20 68 69 6e 74 20 74 68 61 74 20 73 75  s a hint that su
4b20: 72 72 6f 75 6e 64 73 20 61 20 62 6f 6f 6c 65 61  rrounds a boolea
4b30: 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  n.** expression 
4b40: 74 68 61 74 20 69 73 20 75 73 75 61 6c 6c 79 20  that is usually 
4b50: 66 61 6c 73 65 2e 20 20 4d 61 63 72 6f 20 6c 69  false.  Macro li
4b60: 6b 65 6c 79 28 29 20 73 75 72 72 6f 75 6e 64 73  kely() surrounds
4b70: 0a 2a 2a 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78  .** a boolean ex
4b80: 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73  pression that is
4b90: 20 75 73 75 61 6c 6c 79 20 74 72 75 65 2e 20 20   usually true.  
4ba0: 47 43 43 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a  GCC is able to.*
4bb0: 2a 20 75 73 65 20 74 68 65 73 65 20 68 69 6e 74  * use these hint
4bc0: 73 20 74 6f 20 67 65 6e 65 72 61 74 65 20 62 65  s to generate be
4bd0: 74 74 65 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74  tter code, somet
4be0: 69 6d 65 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  imes..*/.#if def
4bf0: 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26  ined(__GNUC__) &
4c00: 26 20 30 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b  & 0.# define lik
4c10: 65 6c 79 28 58 29 20 20 20 20 5f 5f 62 75 69 6c  ely(X)    __buil
4c20: 74 69 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 31  tin_expect((X),1
4c30: 29 0a 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b  ).# define unlik
4c40: 65 6c 79 28 58 29 20 20 5f 5f 62 75 69 6c 74 69  ely(X)  __builti
4c50: 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 30 29 0a  n_expect((X),0).
4c60: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 6c  #else.# define l
4c70: 69 6b 65 6c 79 28 58 29 20 20 20 20 21 21 28 58  ikely(X)    !!(X
4c80: 29 0a 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b  ).# define unlik
4c90: 65 6c 79 28 58 29 20 20 21 21 28 58 29 0a 23 65  ely(X)  !!(X).#e
4ca0: 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
4cb0: 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71  ***** Include sq
4cc0: 6c 69 74 65 33 2e 68 20 69 6e 20 74 68 65 20 6d  lite3.h in the m
4cd0: 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49  iddle of sqliteI
4ce0: 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
4cf0: 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
4d00: 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
4d10: 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a   sqlite3.h *****
4d20: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4d30: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4d40: 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
4d50: 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
4d60: 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
4d70: 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
4d80: 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
4d90: 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
4da0: 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
4db0: 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
4dc0: 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
4dd0: 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
4de0: 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
4df0: 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
4e00: 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
4e10: 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
4e20: 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
4e30: 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
4e40: 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
4e50: 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
4e60: 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
4e70: 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
4e80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e90: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4ea0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4eb0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
4ec0: 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
4ed0: 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74   defines the int
4ee0: 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20  erface that the 
4ef0: 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 0a 2a  SQLite library.*
4f00: 2a 20 70 72 65 73 65 6e 74 73 20 74 6f 20 63 6c  * presents to cl
4f10: 69 65 6e 74 20 70 72 6f 67 72 61 6d 73 2e 20 20  ient programs.  
4f20: 49 66 20 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c  If a C-function,
4f30: 20 73 74 72 75 63 74 75 72 65 2c 20 64 61 74 61   structure, data
4f40: 74 79 70 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73  type,.** or cons
4f50: 74 61 6e 74 20 64 65 66 69 6e 69 74 69 6f 6e 20  tant definition 
4f60: 64 6f 65 73 20 6e 6f 74 20 61 70 70 65 61 72 20  does not appear 
4f70: 69 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68  in this file, th
4f80: 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20  en it is.** not 
4f90: 61 20 70 75 62 6c 69 73 68 65 64 20 41 50 49 20  a published API 
4fa0: 6f 66 20 53 51 4c 69 74 65 2c 20 69 73 20 73 75  of SQLite, is su
4fb0: 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20  bject to change 
4fc0: 77 69 74 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63  without.** notic
4fd0: 65 2c 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f  e, and should no
4fe0: 74 20 62 65 20 72 65 66 65 72 65 6e 63 65 64 20  t be referenced 
4ff0: 62 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74  by programs that
5000: 20 75 73 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a   use SQLite..**.
5010: 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 64  ** Some of the d
5020: 65 66 69 6e 69 74 69 6f 6e 73 20 74 68 61 74 20  efinitions that 
5030: 61 72 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  are in this file
5040: 20 61 72 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a   are marked as.*
5050: 2a 20 22 65 78 70 65 72 69 6d 65 6e 74 61 6c 22  * "experimental"
5060: 2e 20 20 45 78 70 65 72 69 6d 65 6e 74 61 6c 20  .  Experimental 
5070: 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 6e  interfaces are n
5080: 6f 72 6d 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66  ormally new.** f
5090: 65 61 74 75 72 65 73 20 72 65 63 65 6e 74 6c 79  eatures recently
50a0: 20 61 64 64 65 64 20 74 6f 20 53 51 4c 69 74 65   added to SQLite
50b0: 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74  .  We do not ant
50c0: 69 63 69 70 61 74 65 20 63 68 61 6e 67 65 73 0a  icipate changes.
50d0: 2a 2a 20 74 6f 20 65 78 70 65 72 69 6d 65 6e 74  ** to experiment
50e0: 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 62 75  al interfaces bu
50f0: 74 20 72 65 73 65 72 76 65 20 74 68 65 20 72 69  t reserve the ri
5100: 67 68 74 20 74 6f 20 6d 61 6b 65 20 6d 69 6e 6f  ght to make mino
5110: 72 20 63 68 61 6e 67 65 73 0a 2a 2a 20 69 66 20  r changes.** if 
5120: 65 78 70 65 72 69 65 6e 63 65 20 66 72 6f 6d 20  experience from 
5130: 75 73 65 20 22 69 6e 20 74 68 65 20 77 69 6c 64  use "in the wild
5140: 22 20 73 75 67 67 65 73 74 20 73 75 63 68 20 63  " suggest such c
5150: 68 61 6e 67 65 73 20 61 72 65 20 70 72 75 64 65  hanges are prude
5160: 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 66  nt..**.** The of
5170: 66 69 63 69 61 6c 20 43 2d 6c 61 6e 67 75 61 67  ficial C-languag
5180: 65 20 41 50 49 20 64 6f 63 75 6d 65 6e 74 61 74  e API documentat
5190: 69 6f 6e 20 66 6f 72 20 53 51 4c 69 74 65 20 69  ion for SQLite i
51a0: 73 20 64 65 72 69 76 65 64 0a 2a 2a 20 66 72 6f  s derived.** fro
51b0: 6d 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68  m comments in th
51c0: 69 73 20 66 69 6c 65 2e 20 20 54 68 69 73 20 66  is file.  This f
51d0: 69 6c 65 20 69 73 20 74 68 65 20 61 75 74 68 6f  ile is the autho
51e0: 72 69 74 61 74 69 76 65 20 73 6f 75 72 63 65 0a  ritative source.
51f0: 2a 2a 20 6f 6e 20 68 6f 77 20 53 51 4c 69 74 65  ** on how SQLite
5200: 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20   interfaces are 
5210: 73 75 70 70 6f 73 65 20 74 6f 20 6f 70 65 72 61  suppose to opera
5220: 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61  te..**.** The na
5230: 6d 65 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20  me of this file 
5240: 75 6e 64 65 72 20 63 6f 6e 66 69 67 75 72 61 74  under configurat
5250: 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 20 69  ion management i
5260: 73 20 22 73 71 6c 69 74 65 2e 68 2e 69 6e 22 2e  s "sqlite.h.in".
5270: 0a 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65  .** The makefile
5280: 20 6d 61 6b 65 73 20 73 6f 6d 65 20 6d 69 6e 6f   makes some mino
5290: 72 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 69  r changes to thi
52a0: 73 20 66 69 6c 65 20 28 73 75 63 68 20 61 73 20  s file (such as 
52b0: 69 6e 73 65 72 74 69 6e 67 0a 2a 2a 20 74 68 65  inserting.** the
52c0: 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 29   version number)
52d0: 20 61 6e 64 20 63 68 61 6e 67 65 73 20 69 74 73   and changes its
52e0: 20 6e 61 6d 65 20 74 6f 20 22 73 71 6c 69 74 65   name to "sqlite
52f0: 33 2e 68 22 20 61 73 0a 2a 2a 20 70 61 72 74 20  3.h" as.** part 
5300: 6f 66 20 74 68 65 20 62 75 69 6c 64 20 70 72 6f  of the build pro
5310: 63 65 73 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  cess..*/.#ifndef
5320: 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 64 65   _SQLITE3_H_.#de
5330: 66 69 6e 65 20 5f 53 51 4c 49 54 45 33 5f 48 5f  fine _SQLITE3_H_
5340: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72  .#include <stdar
5350: 67 2e 68 3e 20 20 20 20 20 2f 2a 20 4e 65 65 64  g.h>     /* Need
5360: 65 64 20 66 6f 72 20 74 68 65 20 64 65 66 69 6e  ed for the defin
5370: 69 74 69 6f 6e 20 6f 66 20 76 61 5f 6c 69 73 74  ition of va_list
5380: 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20   */../*.** Make 
5390: 73 75 72 65 20 77 65 20 63 61 6e 20 63 61 6c 6c  sure we can call
53a0: 20 74 68 69 73 20 73 74 75 66 66 20 66 72 6f 6d   this stuff from
53b0: 20 43 2b 2b 2e 0a 2a 2f 0a 23 69 66 20 30 0a 65   C++..*/.#if 0.e
53c0: 78 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64  xtern "C" {.#end
53d0: 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74  if.../*.** Add t
53e0: 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 6f 76  he ability to ov
53f0: 65 72 72 69 64 65 20 27 65 78 74 65 72 6e 27 0a  erride 'extern'.
5400: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
5410: 45 5f 45 58 54 45 52 4e 0a 23 20 64 65 66 69 6e  E_EXTERN.# defin
5420: 65 20 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 20  e SQLITE_EXTERN 
5430: 65 78 74 65 72 6e 0a 23 65 6e 64 69 66 0a 0a 23  extern.#endif..#
5440: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 50  ifndef SQLITE_AP
5450: 49 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  I.# define SQLIT
5460: 45 5f 41 50 49 0a 23 65 6e 64 69 66 0a 0a 0a 2f  E_API.#endif.../
5470: 2a 0a 2a 2a 20 54 68 65 73 65 20 6e 6f 2d 6f 70  *.** These no-op
5480: 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73 65 64   macros are used
5490: 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 69 6e 74   in front of int
54a0: 65 72 66 61 63 65 73 20 74 6f 20 6d 61 72 6b 20  erfaces to mark 
54b0: 74 68 6f 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61  those.** interfa
54c0: 63 65 73 20 61 73 20 65 69 74 68 65 72 20 64 65  ces as either de
54d0: 70 72 65 63 61 74 65 64 20 6f 72 20 65 78 70 65  precated or expe
54e0: 72 69 6d 65 6e 74 61 6c 2e 20 20 4e 65 77 20 61  rimental.  New a
54f0: 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73  pplications.** s
5500: 68 6f 75 6c 64 20 6e 6f 74 20 75 73 65 20 64 65  hould not use de
5510: 70 72 65 63 61 74 65 64 20 69 6e 74 65 72 66 61  precated interfa
5520: 63 65 73 20 2d 20 74 68 65 79 20 61 72 65 20 73  ces - they are s
5530: 75 70 70 6f 72 74 20 66 6f 72 20 62 61 63 6b 77  upport for backw
5540: 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62  ards.** compatib
5550: 69 6c 69 74 79 20 6f 6e 6c 79 2e 20 20 41 70 70  ility only.  App
5560: 6c 69 63 61 74 69 6f 6e 20 77 72 69 74 65 72 73  lication writers
5570: 20 73 68 6f 75 6c 64 20 62 65 20 61 77 61 72 65   should be aware
5580: 20 74 68 61 74 0a 2a 2a 20 65 78 70 65 72 69 6d   that.** experim
5590: 65 6e 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73  ental interfaces
55a0: 20 61 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20   are subject to 
55b0: 63 68 61 6e 67 65 20 69 6e 20 70 6f 69 6e 74 20  change in point 
55c0: 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20  releases..**.** 
55d0: 54 68 65 73 65 20 6d 61 63 72 6f 73 20 75 73 65  These macros use
55e0: 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 6f 20  d to resolve to 
55f0: 76 61 72 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66  various kinds of
5600: 20 63 6f 6d 70 69 6c 65 72 20 6d 61 67 69 63 20   compiler magic 
5610: 74 68 61 74 0a 2a 2a 20 77 6f 75 6c 64 20 67 65  that.** would ge
5620: 6e 65 72 61 74 65 20 77 61 72 6e 69 6e 67 20 6d  nerate warning m
5630: 65 73 73 61 67 65 73 20 77 68 65 6e 20 74 68 65  essages when the
5640: 79 20 77 65 72 65 20 75 73 65 64 2e 20 20 42 75  y were used.  Bu
5650: 74 20 74 68 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c  t that.** compil
5660: 65 72 20 6d 61 67 69 63 20 65 6e 64 65 64 20 75  er magic ended u
5670: 70 20 67 65 6e 65 72 61 74 69 6e 67 20 73 75 63  p generating suc
5680: 68 20 61 20 66 6c 75 72 72 79 20 6f 66 20 62 75  h a flurry of bu
5690: 67 20 72 65 70 6f 72 74 73 0a 2a 2a 20 74 68 61  g reports.** tha
56a0: 74 20 77 65 20 68 61 76 65 20 74 61 6b 65 6e 20  t we have taken 
56b0: 69 74 20 61 6c 6c 20 6f 75 74 20 61 6e 64 20 67  it all out and g
56c0: 6f 6e 65 20 62 61 63 6b 20 74 6f 20 75 73 69 6e  one back to usin
56d0: 67 20 73 69 6d 70 6c 65 0a 2a 2a 20 6e 6f 6f 70  g simple.** noop
56e0: 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66   macros..*/.#def
56f0: 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 50 52 45  ine SQLITE_DEPRE
5700: 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20 53 51  CATED.#define SQ
5710: 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
5720: 4c 0a 0a 2f 2a 0a 2a 2a 20 45 6e 73 75 72 65 20  L../*.** Ensure 
5730: 74 68 65 73 65 20 73 79 6d 62 6f 6c 73 20 77 65  these symbols we
5740: 72 65 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 62  re not defined b
5750: 79 20 73 6f 6d 65 20 70 72 65 76 69 6f 75 73 20  y some previous 
5760: 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a  header file..*/.
5770: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45  #ifdef SQLITE_VE
5780: 52 53 49 4f 4e 0a 23 20 75 6e 64 65 66 20 53 51  RSION.# undef SQ
5790: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 0a 23 65 6e  LITE_VERSION.#en
57a0: 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
57b0: 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52  E_VERSION_NUMBER
57c0: 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f  .# undef SQLITE_
57d0: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23  VERSION_NUMBER.#
57e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  endif../*.** CAP
57f0: 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54  I3REF: Compile-T
5800: 69 6d 65 20 4c 69 62 72 61 72 79 20 56 65 72 73  ime Library Vers
5810: 69 6f 6e 20 4e 75 6d 62 65 72 73 0a 2a 2a 0a 2a  ion Numbers.**.*
5820: 2a 20 5e 28 54 68 65 20 5b 53 51 4c 49 54 45 5f  * ^(The [SQLITE_
5830: 56 45 52 53 49 4f 4e 5d 20 43 20 70 72 65 70 72  VERSION] C prepr
5840: 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 69 6e  ocessor macro in
5850: 20 74 68 65 20 73 71 6c 69 74 65 33 2e 68 20 68   the sqlite3.h h
5860: 65 61 64 65 72 0a 2a 2a 20 65 76 61 6c 75 61 74  eader.** evaluat
5870: 65 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6c  es to a string l
5880: 69 74 65 72 61 6c 20 74 68 61 74 20 69 73 20 74  iteral that is t
5890: 68 65 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f  he SQLite versio
58a0: 6e 20 69 6e 20 74 68 65 0a 2a 2a 20 66 6f 72 6d  n in the.** form
58b0: 61 74 20 22 58 2e 59 2e 5a 22 20 77 68 65 72 65  at "X.Y.Z" where
58c0: 20 58 20 69 73 20 74 68 65 20 6d 61 6a 6f 72 20   X is the major 
58d0: 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 28  version number (
58e0: 61 6c 77 61 79 73 20 33 20 66 6f 72 0a 2a 2a 20  always 3 for.** 
58f0: 53 51 4c 69 74 65 33 29 20 61 6e 64 20 59 20 69  SQLite3) and Y i
5900: 73 20 74 68 65 20 6d 69 6e 6f 72 20 76 65 72 73  s the minor vers
5910: 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 5a  ion number and Z
5920: 20 69 73 20 74 68 65 20 72 65 6c 65 61 73 65 20   is the release 
5930: 6e 75 6d 62 65 72 2e 29 5e 0a 2a 2a 20 5e 28 54  number.)^.** ^(T
5940: 68 65 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49  he [SQLITE_VERSI
5950: 4f 4e 5f 4e 55 4d 42 45 52 5d 20 43 20 70 72 65  ON_NUMBER] C pre
5960: 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20  processor macro 
5970: 72 65 73 6f 6c 76 65 73 20 74 6f 20 61 6e 20 69  resolves to an i
5980: 6e 74 65 67 65 72 0a 2a 2a 20 77 69 74 68 20 74  nteger.** with t
5990: 68 65 20 76 61 6c 75 65 20 28 58 2a 31 30 30 30  he value (X*1000
59a0: 30 30 30 20 2b 20 59 2a 31 30 30 30 20 2b 20 5a  000 + Y*1000 + Z
59b0: 29 20 77 68 65 72 65 20 58 2c 20 59 2c 20 61 6e  ) where X, Y, an
59c0: 64 20 5a 20 61 72 65 20 74 68 65 20 73 61 6d 65  d Z are the same
59d0: 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 75 73 65 64  .** numbers used
59e0: 20 69 6e 20 5b 53 51 4c 49 54 45 5f 56 45 52 53   in [SQLITE_VERS
59f0: 49 4f 4e 5d 2e 29 5e 0a 2a 2a 20 54 68 65 20 53  ION].)^.** The S
5a00: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
5a10: 4d 42 45 52 20 66 6f 72 20 61 6e 79 20 67 69 76  MBER for any giv
5a20: 65 6e 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51  en release of SQ
5a30: 4c 69 74 65 20 77 69 6c 6c 20 61 6c 73 6f 0a 2a  Lite will also.*
5a40: 2a 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e  * be larger than
5a50: 20 74 68 65 20 72 65 6c 65 61 73 65 20 66 72 6f   the release fro
5a60: 6d 20 77 68 69 63 68 20 69 74 20 69 73 20 64 65  m which it is de
5a70: 72 69 76 65 64 2e 20 20 45 69 74 68 65 72 20 59  rived.  Either Y
5a80: 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 68 65 6c 64   will.** be held
5a90: 20 63 6f 6e 73 74 61 6e 74 20 61 6e 64 20 5a 20   constant and Z 
5aa0: 77 69 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e  will be incremen
5ab0: 74 65 64 20 6f 72 20 65 6c 73 65 20 59 20 77 69  ted or else Y wi
5ac0: 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e 74 65  ll be incremente
5ad0: 64 0a 2a 2a 20 61 6e 64 20 5a 20 77 69 6c 6c 20  d.** and Z will 
5ae0: 62 65 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f  be reset to zero
5af0: 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 65  ..**.** Since ve
5b00: 72 73 69 6f 6e 20 33 2e 36 2e 31 38 2c 20 53 51  rsion 3.6.18, SQ
5b10: 4c 69 74 65 20 73 6f 75 72 63 65 20 63 6f 64 65  Lite source code
5b20: 20 68 61 73 20 62 65 65 6e 20 73 74 6f 72 65 64   has been stored
5b30: 20 69 6e 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72   in the.** <a hr
5b40: 65 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77 2e 66  ef="http://www.f
5b50: 6f 73 73 69 6c 2d 73 63 6d 2e 6f 72 67 2f 22 3e  ossil-scm.org/">
5b60: 46 6f 73 73 69 6c 20 63 6f 6e 66 69 67 75 72 61  Fossil configura
5b70: 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 0a  tion management.
5b80: 2a 2a 20 73 79 73 74 65 6d 3c 2f 61 3e 2e 20 20  ** system</a>.  
5b90: 5e 54 68 65 20 53 51 4c 49 54 45 5f 53 4f 55 52  ^The SQLITE_SOUR
5ba0: 43 45 5f 49 44 20 6d 61 63 72 6f 20 65 76 61 6c  CE_ID macro eval
5bb0: 75 61 74 65 73 20 74 6f 0a 2a 2a 20 61 20 73 74  uates to.** a st
5bc0: 72 69 6e 67 20 77 68 69 63 68 20 69 64 65 6e 74  ring which ident
5bd0: 69 66 69 65 73 20 61 20 70 61 72 74 69 63 75 6c  ifies a particul
5be0: 61 72 20 63 68 65 63 6b 2d 69 6e 20 6f 66 20 53  ar check-in of S
5bf0: 51 4c 69 74 65 0a 2a 2a 20 77 69 74 68 69 6e 20  QLite.** within 
5c00: 69 74 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  its configuratio
5c10: 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 79 73  n management sys
5c20: 74 65 6d 2e 20 20 5e 54 68 65 20 53 51 4c 49 54  tem.  ^The SQLIT
5c30: 45 5f 53 4f 55 52 43 45 5f 49 44 0a 2a 2a 20 73  E_SOURCE_ID.** s
5c40: 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 74  tring contains t
5c50: 68 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65  he date and time
5c60: 20 6f 66 20 74 68 65 20 63 68 65 63 6b 2d 69 6e   of the check-in
5c70: 20 28 55 54 43 29 20 61 6e 64 20 61 6e 20 53 48   (UTC) and an SH
5c80: 41 31 0a 2a 2a 20 68 61 73 68 20 6f 66 20 74 68  A1.** hash of th
5c90: 65 20 65 6e 74 69 72 65 20 73 6f 75 72 63 65 20  e entire source 
5ca0: 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  tree..**.** See 
5cb0: 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 6c  also: [sqlite3_l
5cc0: 69 62 76 65 72 73 69 6f 6e 28 29 5d 2c 0a 2a 2a  ibversion()],.**
5cd0: 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72   [sqlite3_libver
5ce0: 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 5d 2c 20  sion_number()], 
5cf0: 5b 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69  [sqlite3_sourcei
5d00: 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65  d()],.** [sqlite
5d10: 5f 76 65 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20  _version()] and 
5d20: 5b 73 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69  [sqlite_source_i
5d30: 64 28 29 5d 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  d()]..*/.#define
5d40: 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 20   SQLITE_VERSION 
5d50: 20 20 20 20 20 20 20 22 33 2e 37 2e 31 35 22 0a         "3.7.15".
5d60: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56  #define SQLITE_V
5d70: 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 33 30  ERSION_NUMBER 30
5d80: 30 37 30 31 35 0a 23 64 65 66 69 6e 65 20 53 51  07015.#define SQ
5d90: 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 20 20  LITE_SOURCE_ID  
5da0: 20 20 20 20 22 32 30 31 32 2d 31 32 2d 30 35 20      "2012-12-05 
5db0: 31 34 3a 33 31 3a 31 33 20 39 66 36 63 36 38 38  14:31:13 9f6c688
5dc0: 35 36 62 36 39 34 33 37 33 62 37 66 66 62 31 32  56b694373b7ffb12
5dd0: 34 61 62 64 39 39 36 65 35 31 39 62 61 35 39 32  4abd996e519ba592
5de0: 31 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  1"../*.** CAPI3R
5df0: 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 62  EF: Run-Time Lib
5e00: 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75 6d  rary Version Num
5e10: 62 65 72 73 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  bers.** KEYWORDS
5e20: 3a 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f  : sqlite3_versio
5e30: 6e 2c 20 73 71 6c 69 74 65 33 5f 73 6f 75 72 63  n, sqlite3_sourc
5e40: 65 69 64 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  eid.**.** These 
5e50: 69 6e 74 65 72 66 61 63 65 73 20 70 72 6f 76 69  interfaces provi
5e60: 64 65 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f  de the same info
5e70: 72 6d 61 74 69 6f 6e 20 61 73 20 74 68 65 20 5b  rmation as the [
5e80: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5d 2c  SQLITE_VERSION],
5e90: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 52 53  .** [SQLITE_VERS
5ea0: 49 4f 4e 5f 4e 55 4d 42 45 52 5d 2c 20 61 6e 64  ION_NUMBER], and
5eb0: 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f   [SQLITE_SOURCE_
5ec0: 49 44 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73  ID] C preprocess
5ed0: 6f 72 20 6d 61 63 72 6f 73 0a 2a 2a 20 62 75 74  or macros.** but
5ee0: 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 20   are associated 
5ef0: 77 69 74 68 20 74 68 65 20 6c 69 62 72 61 72 79  with the library
5f00: 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20   instead of the 
5f10: 68 65 61 64 65 72 20 66 69 6c 65 2e 20 20 5e 28  header file.  ^(
5f20: 43 61 75 74 69 6f 75 73 0a 2a 2a 20 70 72 6f 67  Cautious.** prog
5f30: 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 69 6e  rammers might in
5f40: 63 6c 75 64 65 20 61 73 73 65 72 74 28 29 20 73  clude assert() s
5f50: 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65  tatements in the
5f60: 69 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74  ir application t
5f70: 6f 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 61 74  o.** verify that
5f80: 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64   values returned
5f90: 20 62 79 20 74 68 65 73 65 20 69 6e 74 65 72 66   by these interf
5fa0: 61 63 65 73 20 6d 61 74 63 68 20 74 68 65 20 6d  aces match the m
5fb0: 61 63 72 6f 73 20 69 6e 0a 2a 2a 20 74 68 65 20  acros in.** the 
5fc0: 68 65 61 64 65 72 2c 20 61 6e 64 20 74 68 75 73  header, and thus
5fd0: 20 69 6e 73 75 72 65 20 74 68 61 74 20 74 68 65   insure that the
5fe0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 0a   application is.
5ff0: 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  ** compiled with
6000: 20 6d 61 74 63 68 69 6e 67 20 6c 69 62 72 61 72   matching librar
6010: 79 20 61 6e 64 20 68 65 61 64 65 72 20 66 69 6c  y and header fil
6020: 65 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b  es..**.** <block
6030: 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 61  quote><pre>.** a
6040: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6c  ssert( sqlite3_l
6050: 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
6060: 28 29 3d 3d 53 51 4c 49 54 45 5f 56 45 52 53 49  ()==SQLITE_VERSI
6070: 4f 4e 5f 4e 55 4d 42 45 52 20 29 3b 0a 2a 2a 20  ON_NUMBER );.** 
6080: 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28 73  assert( strcmp(s
6090: 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28  qlite3_sourceid(
60a0: 29 2c 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f  ),SQLITE_SOURCE_
60b0: 49 44 29 3d 3d 30 20 29 3b 0a 2a 2a 20 61 73 73  ID)==0 );.** ass
60c0: 65 72 74 28 20 73 74 72 63 6d 70 28 73 71 6c 69  ert( strcmp(sqli
60d0: 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29  te3_libversion()
60e0: 2c 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 29  ,SQLITE_VERSION)
60f0: 3d 3d 30 20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e  ==0 );.** </pre>
6100: 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 29 5e 0a  </blockquote>)^.
6110: 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74  **.** ^The sqlit
6120: 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 73 74 72  e3_version[] str
6130: 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 63 6f 6e  ing constant con
6140: 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f  tains the text o
6150: 66 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  f [SQLITE_VERSIO
6160: 4e 5d 0a 2a 2a 20 6d 61 63 72 6f 2e 20 20 5e 54  N].** macro.  ^T
6170: 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  he sqlite3_libve
6180: 72 73 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e  rsion() function
6190: 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
61a0: 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 74 6f 20  er to the.** to 
61b0: 74 68 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73  the sqlite3_vers
61c0: 69 6f 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e  ion[] string con
61d0: 73 74 61 6e 74 2e 20 20 54 68 65 20 73 71 6c 69  stant.  The sqli
61e0: 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29  te3_libversion()
61f0: 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  .** function is 
6200: 70 72 6f 76 69 64 65 64 20 66 6f 72 20 75 73 65  provided for use
6210: 20 69 6e 20 44 4c 4c 73 20 73 69 6e 63 65 20 44   in DLLs since D
6220: 4c 4c 20 75 73 65 72 73 20 75 73 75 61 6c 6c 79  LL users usually
6230: 20 64 6f 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20   do not have.** 
6240: 64 69 72 65 63 74 20 61 63 63 65 73 73 20 74 6f  direct access to
6250: 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74   string constant
6260: 73 20 77 69 74 68 69 6e 20 74 68 65 20 44 4c 4c  s within the DLL
6270: 2e 20 20 5e 54 68 65 0a 2a 2a 20 73 71 6c 69 74  .  ^The.** sqlit
6280: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  e3_libversion_nu
6290: 6d 62 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20  mber() function 
62a0: 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67  returns an integ
62b0: 65 72 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20 5b  er equal to.** [
62c0: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e  SQLITE_VERSION_N
62d0: 55 4d 42 45 52 5d 2e 20 20 5e 54 68 65 20 73 71  UMBER].  ^The sq
62e0: 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29  lite3_sourceid()
62f0: 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
6300: 73 20 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  s .** a pointer 
6310: 74 6f 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 73  to a string cons
6320: 74 61 6e 74 20 77 68 6f 73 65 20 76 61 6c 75 65  tant whose value
6330: 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
6340: 74 68 65 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  the .** [SQLITE_
6350: 53 4f 55 52 43 45 5f 49 44 5d 20 43 20 70 72 65  SOURCE_ID] C pre
6360: 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 2e  processor macro.
6370: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
6380: 20 5b 73 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e   [sqlite_version
6390: 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 5f  ()] and [sqlite_
63a0: 73 6f 75 72 63 65 5f 69 64 28 29 5d 2e 0a 2a 2f  source_id()]..*/
63b0: 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
63c0: 74 20 63 68 61 72 20 73 71 6c 69 74 65 33 5f 76  t char sqlite3_v
63d0: 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 51 4c 49 54  ersion[] = SQLIT
63e0: 45 5f 56 45 52 53 49 4f 4e 3b 0a 53 51 4c 49 54  E_VERSION;.SQLIT
63f0: 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
6400: 20 2a 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72   *sqlite3_libver
6410: 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 53 51 4c 49  sion(void);.SQLI
6420: 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
6430: 72 20 2a 73 71 6c 69 74 65 33 5f 73 6f 75 72 63  r *sqlite3_sourc
6440: 65 69 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  eid(void);.SQLIT
6450: 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
6460: 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d  3_libversion_num
6470: 62 65 72 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a  ber(void);../*.*
6480: 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d  * CAPI3REF: Run-
6490: 54 69 6d 65 20 4c 69 62 72 61 72 79 20 43 6f 6d  Time Library Com
64a0: 70 69 6c 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73  pilation Options
64b0: 20 44 69 61 67 6e 6f 73 74 69 63 73 0a 2a 2a 0a   Diagnostics.**.
64c0: 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f  ** ^The sqlite3_
64d0: 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73  compileoption_us
64e0: 65 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65  ed() function re
64f0: 74 75 72 6e 73 20 30 20 6f 72 20 31 20 0a 2a 2a  turns 0 or 1 .**
6500: 20 69 6e 64 69 63 61 74 69 6e 67 20 77 68 65 74   indicating whet
6510: 68 65 72 20 74 68 65 20 73 70 65 63 69 66 69 65  her the specifie
6520: 64 20 6f 70 74 69 6f 6e 20 77 61 73 20 64 65 66  d option was def
6530: 69 6e 65 64 20 61 74 20 0a 2a 2a 20 63 6f 6d 70  ined at .** comp
6540: 69 6c 65 20 74 69 6d 65 2e 20 20 5e 54 68 65 20  ile time.  ^The 
6550: 53 51 4c 49 54 45 5f 20 70 72 65 66 69 78 20 6d  SQLITE_ prefix m
6560: 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20 66 72  ay be omitted fr
6570: 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 70 74 69 6f  om the .** optio
6580: 6e 20 6e 61 6d 65 20 70 61 73 73 65 64 20 74 6f  n name passed to
6590: 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65   sqlite3_compile
65a0: 6f 70 74 69 6f 6e 5f 75 73 65 64 28 29 2e 20 20  option_used().  
65b0: 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69  .**.** ^The sqli
65c0: 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f  te3_compileoptio
65d0: 6e 5f 67 65 74 28 29 20 66 75 6e 63 74 69 6f 6e  n_get() function
65e0: 20 61 6c 6c 6f 77 73 20 69 74 65 72 61 74 69 6e   allows iteratin
65f0: 67 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20 6c 69  g.** over the li
6600: 73 74 20 6f 66 20 6f 70 74 69 6f 6e 73 20 74 68  st of options th
6610: 61 74 20 77 65 72 65 20 64 65 66 69 6e 65 64 20  at were defined 
6620: 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20  at compile time 
6630: 62 79 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 20  by.** returning 
6640: 74 68 65 20 4e 2d 74 68 20 63 6f 6d 70 69 6c 65  the N-th compile
6650: 20 74 69 6d 65 20 6f 70 74 69 6f 6e 20 73 74 72   time option str
6660: 69 6e 67 2e 20 20 5e 49 66 20 4e 20 69 73 20 6f  ing.  ^If N is o
6670: 75 74 20 6f 66 20 72 61 6e 67 65 2c 0a 2a 2a 20  ut of range,.** 
6680: 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f  sqlite3_compileo
6690: 70 74 69 6f 6e 5f 67 65 74 28 29 20 72 65 74 75  ption_get() retu
66a0: 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  rns a NULL point
66b0: 65 72 2e 20 20 5e 54 68 65 20 53 51 4c 49 54 45  er.  ^The SQLITE
66c0: 5f 20 0a 2a 2a 20 70 72 65 66 69 78 20 69 73 20  _ .** prefix is 
66d0: 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 61 6e 79  omitted from any
66e0: 20 73 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65   strings returne
66f0: 64 20 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33  d by .** sqlite3
6700: 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67  _compileoption_g
6710: 65 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 5e 53 75 70  et()..**.** ^Sup
6720: 70 6f 72 74 20 66 6f 72 20 74 68 65 20 64 69 61  port for the dia
6730: 67 6e 6f 73 74 69 63 20 66 75 6e 63 74 69 6f 6e  gnostic function
6740: 73 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c  s sqlite3_compil
6750: 65 6f 70 74 69 6f 6e 5f 75 73 65 64 28 29 0a 2a  eoption_used().*
6760: 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f  * and sqlite3_co
6770: 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28  mpileoption_get(
6780: 29 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64  ) may be omitted
6790: 20 62 79 20 73 70 65 63 69 66 79 69 6e 67 20 74   by specifying t
67a0: 68 65 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f  he .** [SQLITE_O
67b0: 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f  MIT_COMPILEOPTIO
67c0: 4e 5f 44 49 41 47 53 5d 20 6f 70 74 69 6f 6e 20  N_DIAGS] option 
67d0: 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e  at compile time.
67e0: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
67f0: 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 5b   SQL functions [
6800: 73 71 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 6f 70  sqlite_compileop
6810: 74 69 6f 6e 5f 75 73 65 64 28 29 5d 20 61 6e 64  tion_used()] and
6820: 0a 2a 2a 20 5b 73 71 6c 69 74 65 5f 63 6f 6d 70  .** [sqlite_comp
6830: 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28 29 5d  ileoption_get()]
6840: 20 61 6e 64 20 74 68 65 20 5b 63 6f 6d 70 69 6c   and the [compil
6850: 65 5f 6f 70 74 69 6f 6e 73 20 70 72 61 67 6d 61  e_options pragma
6860: 5d 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ]..*/.#ifndef SQ
6870: 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c  LITE_OMIT_COMPIL
6880: 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53 0a 53 51  EOPTION_DIAGS.SQ
6890: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
68a0: 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69  ite3_compileopti
68b0: 6f 6e 5f 75 73 65 64 28 63 6f 6e 73 74 20 63 68  on_used(const ch
68c0: 61 72 20 2a 7a 4f 70 74 4e 61 6d 65 29 3b 0a 53  ar *zOptName);.S
68d0: 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
68e0: 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
68f0: 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28  mpileoption_get(
6900: 69 6e 74 20 4e 29 3b 0a 23 65 6e 64 69 66 0a 0a  int N);.#endif..
6910: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
6920: 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20 54  Test To See If T
6930: 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54 68  he Library Is Th
6940: 72 65 61 64 73 61 66 65 0a 2a 2a 0a 2a 2a 20 5e  readsafe.**.** ^
6950: 54 68 65 20 73 71 6c 69 74 65 33 5f 74 68 72 65  The sqlite3_thre
6960: 61 64 73 61 66 65 28 29 20 66 75 6e 63 74 69 6f  adsafe() functio
6970: 6e 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 20 69  n returns zero i
6980: 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 0a 2a 2a  f and only if.**
6990: 20 53 51 4c 69 74 65 20 77 61 73 20 63 6f 6d 70   SQLite was comp
69a0: 69 6c 65 64 20 77 69 74 68 20 6d 75 74 65 78 69  iled with mutexi
69b0: 6e 67 20 63 6f 64 65 20 6f 6d 69 74 74 65 64 20  ng code omitted 
69c0: 64 75 65 20 74 6f 20 74 68 65 0a 2a 2a 20 5b 53  due to the.** [S
69d0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
69e0: 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f  ] compile-time o
69f0: 70 74 69 6f 6e 20 62 65 69 6e 67 20 73 65 74 20  ption being set 
6a00: 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  to 0..**.** SQLi
6a10: 74 65 20 63 61 6e 20 62 65 20 63 6f 6d 70 69 6c  te can be compil
6a20: 65 64 20 77 69 74 68 20 6f 72 20 77 69 74 68 6f  ed with or witho
6a30: 75 74 20 6d 75 74 65 78 65 73 2e 20 20 57 68 65  ut mutexes.  Whe
6a40: 6e 0a 2a 2a 20 74 68 65 20 5b 53 51 4c 49 54 45  n.** the [SQLITE
6a50: 5f 54 48 52 45 41 44 53 41 46 45 5d 20 43 20 70  _THREADSAFE] C p
6a60: 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72  reprocessor macr
6a70: 6f 20 69 73 20 31 20 6f 72 20 32 2c 20 6d 75 74  o is 1 or 2, mut
6a80: 65 78 65 73 0a 2a 2a 20 61 72 65 20 65 6e 61 62  exes.** are enab
6a90: 6c 65 64 20 61 6e 64 20 53 51 4c 69 74 65 20 69  led and SQLite i
6aa0: 73 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 57  s threadsafe.  W
6ab0: 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49  hen the.** [SQLI
6ac0: 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20 6d  TE_THREADSAFE] m
6ad0: 61 63 72 6f 20 69 73 20 30 2c 20 0a 2a 2a 20 74  acro is 0, .** t
6ae0: 68 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 6f  he mutexes are o
6af0: 6d 69 74 74 65 64 2e 20 20 57 69 74 68 6f 75 74  mitted.  Without
6b00: 20 74 68 65 20 6d 75 74 65 78 65 73 2c 20 69 74   the mutexes, it
6b10: 20 69 73 20 6e 6f 74 20 73 61 66 65 0a 2a 2a 20   is not safe.** 
6b20: 74 6f 20 75 73 65 20 53 51 4c 69 74 65 20 63 6f  to use SQLite co
6b30: 6e 63 75 72 72 65 6e 74 6c 79 20 66 72 6f 6d 20  ncurrently from 
6b40: 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 74 68  more than one th
6b50: 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 45 6e 61 62  read..**.** Enab
6b60: 6c 69 6e 67 20 6d 75 74 65 78 65 73 20 69 6e 63  ling mutexes inc
6b70: 75 72 73 20 61 20 6d 65 61 73 75 72 61 62 6c 65  urs a measurable
6b80: 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 70 65 6e   performance pen
6b90: 61 6c 74 79 2e 0a 2a 2a 20 53 6f 20 69 66 20 73  alty..** So if s
6ba0: 70 65 65 64 20 69 73 20 6f 66 20 75 74 6d 6f 73  peed is of utmos
6bb0: 74 20 69 6d 70 6f 72 74 61 6e 63 65 2c 20 69 74  t importance, it
6bc0: 20 6d 61 6b 65 73 20 73 65 6e 73 65 20 74 6f 20   makes sense to 
6bd0: 64 69 73 61 62 6c 65 0a 2a 2a 20 74 68 65 20 6d  disable.** the m
6be0: 75 74 65 78 65 73 2e 20 20 42 75 74 20 66 6f 72  utexes.  But for
6bf0: 20 6d 61 78 69 6d 75 6d 20 73 61 66 65 74 79 2c   maximum safety,
6c00: 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 20   mutexes should 
6c10: 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 20 5e  be enabled..** ^
6c20: 54 68 65 20 64 65 66 61 75 6c 74 20 62 65 68 61  The default beha
6c30: 76 69 6f 72 20 69 73 20 66 6f 72 20 6d 75 74 65  vior is for mute
6c40: 78 65 73 20 74 6f 20 62 65 20 65 6e 61 62 6c 65  xes to be enable
6c50: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  d..**.** This in
6c60: 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75  terface can be u
6c70: 73 65 64 20 62 79 20 61 6e 20 61 70 70 6c 69 63  sed by an applic
6c80: 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75  ation to make su
6c90: 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 76  re that the.** v
6ca0: 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65  ersion of SQLite
6cb0: 20 74 68 61 74 20 69 74 20 69 73 20 6c 69 6e 6b   that it is link
6cc0: 69 6e 67 20 61 67 61 69 6e 73 74 20 77 61 73 20  ing against was 
6cd0: 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a  compiled with.**
6ce0: 20 74 68 65 20 64 65 73 69 72 65 64 20 73 65 74   the desired set
6cf0: 74 69 6e 67 20 6f 66 20 74 68 65 20 5b 53 51 4c  ting of the [SQL
6d00: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20  ITE_THREADSAFE] 
6d10: 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  macro..**.** Thi
6d20: 73 20 69 6e 74 65 72 66 61 63 65 20 6f 6e 6c 79  s interface only
6d30: 20 72 65 70 6f 72 74 73 20 6f 6e 20 74 68 65 20   reports on the 
6d40: 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6d 75 74  compile-time mut
6d50: 65 78 20 73 65 74 74 69 6e 67 0a 2a 2a 20 6f 66  ex setting.** of
6d60: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52   the [SQLITE_THR
6d70: 45 41 44 53 41 46 45 5d 20 66 6c 61 67 2e 20 20  EADSAFE] flag.  
6d80: 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  If SQLite is com
6d90: 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 53 51  piled with.** SQ
6da0: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d  LITE_THREADSAFE=
6db0: 31 20 6f 72 20 3d 32 20 74 68 65 6e 20 6d 75 74  1 or =2 then mut
6dc0: 65 78 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64  exes are enabled
6dd0: 20 62 79 20 64 65 66 61 75 6c 74 20 62 75 74 0a   by default but.
6de0: 2a 2a 20 63 61 6e 20 62 65 20 66 75 6c 6c 79 20  ** can be fully 
6df0: 6f 72 20 70 61 72 74 69 61 6c 6c 79 20 64 69 73  or partially dis
6e00: 61 62 6c 65 64 20 75 73 69 6e 67 20 61 20 63 61  abled using a ca
6e10: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  ll to [sqlite3_c
6e20: 6f 6e 66 69 67 28 29 5d 0a 2a 2a 20 77 69 74 68  onfig()].** with
6e30: 20 74 68 65 20 76 65 72 62 73 20 5b 53 51 4c 49   the verbs [SQLI
6e40: 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45  TE_CONFIG_SINGLE
6e50: 54 48 52 45 41 44 5d 2c 20 5b 53 51 4c 49 54 45  THREAD], [SQLITE
6e60: 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52  _CONFIG_MULTITHR
6e70: 45 41 44 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c  EAD],.** or [SQL
6e80: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58  ITE_CONFIG_MUTEX
6e90: 5d 2e 20 20 5e 28 54 68 65 20 72 65 74 75 72 6e  ].  ^(The return
6ea0: 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a   value of the.**
6eb0: 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73   sqlite3_threads
6ec0: 61 66 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 73  afe() function s
6ed0: 68 6f 77 73 20 6f 6e 6c 79 20 74 68 65 20 63 6f  hows only the co
6ee0: 6d 70 69 6c 65 2d 74 69 6d 65 20 73 65 74 74 69  mpile-time setti
6ef0: 6e 67 20 6f 66 0a 2a 2a 20 74 68 72 65 61 64 20  ng of.** thread 
6f00: 73 61 66 65 74 79 2c 20 6e 6f 74 20 61 6e 79 20  safety, not any 
6f10: 72 75 6e 2d 74 69 6d 65 20 63 68 61 6e 67 65 73  run-time changes
6f20: 20 74 6f 20 74 68 61 74 20 73 65 74 74 69 6e 67   to that setting
6f30: 20 6d 61 64 65 20 62 79 0a 2a 2a 20 73 71 6c 69   made by.** sqli
6f40: 74 65 33 5f 63 6f 6e 66 69 67 28 29 2e 20 49 6e  te3_config(). In
6f50: 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68   other words, th
6f60: 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66  e return value f
6f70: 72 6f 6d 20 73 71 6c 69 74 65 33 5f 74 68 72 65  rom sqlite3_thre
6f80: 61 64 73 61 66 65 28 29 0a 2a 2a 20 69 73 20 75  adsafe().** is u
6f90: 6e 63 68 61 6e 67 65 64 20 62 79 20 63 61 6c 6c  nchanged by call
6fa0: 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e  s to sqlite3_con
6fb0: 66 69 67 28 29 2e 29 5e 0a 2a 2a 0a 2a 2a 20 53  fig().)^.**.** S
6fc0: 65 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e  ee the [threadin
6fd0: 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e 74  g mode] document
6fe0: 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69  ation for additi
6ff0: 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
7000: 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
7010: 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65  int sqlite3_thre
7020: 61 64 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f  adsafe(void);../
7030: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
7040: 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69  atabase Connecti
7050: 6f 6e 20 48 61 6e 64 6c 65 0a 2a 2a 20 4b 45 59  on Handle.** KEY
7060: 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61 73 65  WORDS: {database
7070: 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64 61   connection} {da
7080: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
7090: 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 6f  ns}.**.** Each o
70a0: 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62  pen SQLite datab
70b0: 61 73 65 20 69 73 20 72 65 70 72 65 73 65 6e 74  ase is represent
70c0: 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65 72 20  ed by a pointer 
70d0: 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  to an instance o
70e0: 66 0a 2a 2a 20 74 68 65 20 6f 70 61 71 75 65 20  f.** the opaque 
70f0: 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20  structure named 
7100: 22 73 71 6c 69 74 65 33 22 2e 20 20 49 74 20 69  "sqlite3".  It i
7110: 73 20 75 73 65 66 75 6c 20 74 6f 20 74 68 69 6e  s useful to thin
7120: 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 0a  k of an sqlite3.
7130: 2a 2a 20 70 6f 69 6e 74 65 72 20 61 73 20 61 6e  ** pointer as an
7140: 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 5b 73   object.  The [s
7150: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20  qlite3_open()], 
7160: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28  [sqlite3_open16(
7170: 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69  )], and.** [sqli
7180: 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69  te3_open_v2()] i
7190: 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 69 74  nterfaces are it
71a0: 73 20 63 6f 6e 73 74 72 75 63 74 6f 72 73 2c 20  s constructors, 
71b0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f  and [sqlite3_clo
71c0: 73 65 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71  se()].** and [sq
71d0: 6c 69 74 65 33 5f 63 6c 6f 73 65 5f 76 32 28 29  lite3_close_v2()
71e0: 5d 20 61 72 65 20 69 74 73 20 64 65 73 74 72 75  ] are its destru
71f0: 63 74 6f 72 73 2e 20 20 54 68 65 72 65 20 61 72  ctors.  There ar
7200: 65 20 6d 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20  e many other.** 
7210: 69 6e 74 65 72 66 61 63 65 73 20 28 73 75 63 68  interfaces (such
7220: 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   as.** [sqlite3_
7230: 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b  prepare_v2()], [
7240: 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
7250: 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 61 6e 64 0a  unction()], and.
7260: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79  ** [sqlite3_busy
7270: 5f 74 69 6d 65 6f 75 74 28 29 5d 20 74 6f 20 6e  _timeout()] to n
7280: 61 6d 65 20 62 75 74 20 74 68 72 65 65 29 20 74  ame but three) t
7290: 68 61 74 20 61 72 65 20 6d 65 74 68 6f 64 73 20  hat are methods 
72a0: 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33  on an.** sqlite3
72b0: 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65   object..*/.type
72c0: 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
72d0: 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a 2f 2a 0a  e3 sqlite3;../*.
72e0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 36 34 2d  ** CAPI3REF: 64-
72f0: 42 69 74 20 49 6e 74 65 67 65 72 20 54 79 70 65  Bit Integer Type
7300: 73 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73  s.** KEYWORDS: s
7310: 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69  qlite_int64 sqli
7320: 74 65 5f 75 69 6e 74 36 34 0a 2a 2a 0a 2a 2a 20  te_uint64.**.** 
7330: 42 65 63 61 75 73 65 20 74 68 65 72 65 20 69 73  Because there is
7340: 20 6e 6f 20 63 72 6f 73 73 2d 70 6c 61 74 66 6f   no cross-platfo
7350: 72 6d 20 77 61 79 20 74 6f 20 73 70 65 63 69 66  rm way to specif
7360: 79 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  y 64-bit integer
7370: 20 74 79 70 65 73 0a 2a 2a 20 53 51 4c 69 74 65   types.** SQLite
7380: 20 69 6e 63 6c 75 64 65 73 20 74 79 70 65 64 65   includes typede
7390: 66 73 20 66 6f 72 20 36 34 2d 62 69 74 20 73 69  fs for 64-bit si
73a0: 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67 6e 65  gned and unsigne
73b0: 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 0a 2a  d integers..**.*
73c0: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  * The sqlite3_in
73d0: 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  t64 and sqlite3_
73e0: 75 69 6e 74 36 34 20 61 72 65 20 74 68 65 20 70  uint64 are the p
73f0: 72 65 66 65 72 72 65 64 20 74 79 70 65 20 64 65  referred type de
7400: 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68  finitions..** Th
7410: 65 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 61  e sqlite_int64 a
7420: 6e 64 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  nd sqlite_uint64
7430: 20 74 79 70 65 73 20 61 72 65 20 73 75 70 70 6f   types are suppo
7440: 72 74 65 64 20 66 6f 72 20 62 61 63 6b 77 61 72  rted for backwar
7450: 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c  ds.** compatibil
7460: 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20  ity only..**.** 
7470: 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74  ^The sqlite3_int
7480: 36 34 20 61 6e 64 20 73 71 6c 69 74 65 5f 69 6e  64 and sqlite_in
7490: 74 36 34 20 74 79 70 65 73 20 63 61 6e 20 73 74  t64 types can st
74a0: 6f 72 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75  ore integer valu
74b0: 65 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 2d 39  es.** between -9
74c0: 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38  2233720368547758
74d0: 30 38 20 61 6e 64 20 2b 39 32 32 33 33 37 32 30  08 and +92233720
74e0: 33 36 38 35 34 37 37 35 38 30 37 20 69 6e 63 6c  36854775807 incl
74f0: 75 73 69 76 65 2e 20 20 5e 54 68 65 0a 2a 2a 20  usive.  ^The.** 
7500: 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 61  sqlite3_uint64 a
7510: 6e 64 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  nd sqlite_uint64
7520: 20 74 79 70 65 73 20 63 61 6e 20 73 74 6f 72 65   types can store
7530: 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20   integer values 
7540: 0a 2a 2a 20 62 65 74 77 65 65 6e 20 30 20 61 6e  .** between 0 an
7550: 64 20 2b 31 38 34 34 36 37 34 34 30 37 33 37 30  d +1844674407370
7560: 39 35 35 31 36 31 35 20 69 6e 63 6c 75 73 69 76  9551615 inclusiv
7570: 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
7580: 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a 20  ITE_INT64_TYPE. 
7590: 20 74 79 70 65 64 65 66 20 53 51 4c 49 54 45 5f   typedef SQLITE_
75a0: 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69 74  INT64_TYPE sqlit
75b0: 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64  e_int64;.  typed
75c0: 65 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c 49  ef unsigned SQLI
75d0: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71  TE_INT64_TYPE sq
75e0: 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c  lite_uint64;.#el
75f0: 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f  if defined(_MSC_
7600: 56 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  VER) || defined(
7610: 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20 20  __BORLANDC__).  
7620: 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34 20  typedef __int64 
7630: 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20  sqlite_int64;.  
7640: 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64  typedef unsigned
7650: 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f   __int64 sqlite_
7660: 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20 20  uint64;.#else.  
7670: 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f 6e  typedef long lon
7680: 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e 74  g int sqlite_int
7690: 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e  64;.  typedef un
76a0: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
76b0: 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e 74   int sqlite_uint
76c0: 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65 64  64;.#endif.typed
76d0: 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  ef sqlite_int64 
76e0: 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a 74  sqlite3_int64;.t
76f0: 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 69  ypedef sqlite_ui
7700: 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69 6e  nt64 sqlite3_uin
7710: 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63  t64;../*.** If c
7720: 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 70  ompiling for a p
7730: 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c 61  rocessor that la
7740: 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  cks floating poi
7750: 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 73  nt support,.** s
7760: 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 65  ubstitute intege
7770: 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70  r for floating-p
7780: 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  oint..*/.#ifdef 
7790: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
77a0: 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66  TING_POINT.# def
77b0: 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  ine double sqlit
77c0: 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66 0a  e3_int64.#endif.
77d0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
77e0: 20 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61 62   Closing A Datab
77f0: 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  ase Connection.*
7800: 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65  *.** ^The sqlite
7810: 33 5f 63 6c 6f 73 65 28 29 20 61 6e 64 20 73 71  3_close() and sq
7820: 6c 69 74 65 33 5f 63 6c 6f 73 65 5f 76 32 28 29  lite3_close_v2()
7830: 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 64 65   routines are de
7840: 73 74 72 75 63 74 6f 72 73 0a 2a 2a 20 66 6f 72  structors.** for
7850: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f   the [sqlite3] o
7860: 62 6a 65 63 74 2e 0a 2a 2a 20 5e 43 61 6c 6c 73  bject..** ^Calls
7870: 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73   to sqlite3_clos
7880: 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  e() and sqlite3_
7890: 63 6c 6f 73 65 5f 76 32 28 29 20 72 65 74 75 72  close_v2() retur
78a0: 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 0a 2a  n SQLITE_OK if.*
78b0: 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20  * the [sqlite3] 
78c0: 6f 62 6a 65 63 74 20 69 73 20 73 75 63 63 65 73  object is succes
78d0: 73 66 75 6c 6c 79 20 64 65 73 74 72 6f 79 65 64  sfully destroyed
78e0: 20 61 6e 64 20 61 6c 6c 20 61 73 73 6f 63 69 61   and all associa
78f0: 74 65 64 0a 2a 2a 20 72 65 73 6f 75 72 63 65 73  ted.** resources
7900: 20 61 72 65 20 64 65 61 6c 6c 6f 63 61 74 65 64   are deallocated
7910: 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 74 68 65 20  ..**.** ^If the 
7920: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
7930: 69 6f 6e 20 69 73 20 61 73 73 6f 63 69 61 74 65  ion is associate
7940: 64 20 77 69 74 68 20 75 6e 66 69 6e 61 6c 69 7a  d with unfinaliz
7950: 65 64 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73  ed prepared.** s
7960: 74 61 74 65 6d 65 6e 74 73 20 6f 72 20 75 6e 66  tatements or unf
7970: 69 6e 69 73 68 65 64 20 73 71 6c 69 74 65 33 5f  inished sqlite3_
7980: 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 73 20 74  backup objects t
7990: 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73  hen sqlite3_clos
79a0: 65 28 29 0a 2a 2a 20 77 69 6c 6c 20 6c 65 61 76  e().** will leav
79b0: 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  e the database c
79c0: 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 6e 20 61  onnection open a
79d0: 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54  nd return [SQLIT
79e0: 45 5f 42 55 53 59 5d 2e 0a 2a 2a 20 5e 49 66 20  E_BUSY]..** ^If 
79f0: 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5f 76 32  sqlite3_close_v2
7a00: 28 29 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74  () is called wit
7a10: 68 20 75 6e 66 69 6e 61 6c 69 7a 65 64 20 70 72  h unfinalized pr
7a20: 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
7a30: 73 0a 2a 2a 20 61 6e 64 20 75 6e 66 69 6e 69 73  s.** and unfinis
7a40: 68 65 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  hed sqlite3_back
7a50: 75 70 73 2c 20 74 68 65 6e 20 74 68 65 20 64 61  ups, then the da
7a60: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
7a70: 6e 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 61 6e 20  n becomes.** an 
7a80: 75 6e 75 73 61 62 6c 65 20 22 7a 6f 6d 62 69 65  unusable "zombie
7a90: 22 20 77 68 69 63 68 20 77 69 6c 6c 20 61 75 74  " which will aut
7aa0: 6f 6d 61 74 69 63 61 6c 6c 79 20 62 65 20 64 65  omatically be de
7ab0: 61 6c 6c 6f 63 61 74 65 64 20 77 68 65 6e 20 74  allocated when t
7ac0: 68 65 0a 2a 2a 20 6c 61 73 74 20 70 72 65 70 61  he.** last prepa
7ad0: 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73  red statement is
7ae0: 20 66 69 6e 61 6c 69 7a 65 64 20 6f 72 20 74 68   finalized or th
7af0: 65 20 6c 61 73 74 20 73 71 6c 69 74 65 33 5f 62  e last sqlite3_b
7b00: 61 63 6b 75 70 20 69 73 0a 2a 2a 20 66 69 6e 69  ackup is.** fini
7b10: 73 68 65 64 2e 20 20 54 68 65 20 73 71 6c 69 74  shed.  The sqlit
7b20: 65 33 5f 63 6c 6f 73 65 5f 76 32 28 29 20 69 6e  e3_close_v2() in
7b30: 74 65 72 66 61 63 65 20 69 73 20 69 6e 74 65 6e  terface is inten
7b40: 64 65 64 20 66 6f 72 20 75 73 65 20 77 69 74 68  ded for use with
7b50: 0a 2a 2a 20 68 6f 73 74 20 6c 61 6e 67 75 61 67  .** host languag
7b60: 65 73 20 74 68 61 74 20 61 72 65 20 67 61 72 62  es that are garb
7b70: 61 67 65 20 63 6f 6c 6c 65 63 74 65 64 2c 20 61  age collected, a
7b80: 6e 64 20 77 68 65 72 65 20 74 68 65 20 6f 72 64  nd where the ord
7b90: 65 72 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 64  er in which.** d
7ba0: 65 73 74 72 75 63 74 6f 72 73 20 61 72 65 20 63  estructors are c
7bb0: 61 6c 6c 65 64 20 69 73 20 61 72 62 69 74 72 61  alled is arbitra
7bc0: 72 79 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63  ry..**.** Applic
7bd0: 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 5b 73  ations should [s
7be0: 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20  qlite3_finalize 
7bf0: 7c 20 66 69 6e 61 6c 69 7a 65 5d 20 61 6c 6c 20  | finalize] all 
7c00: 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
7c10: 65 6e 74 73 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74  ents],.** [sqlit
7c20: 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 7c 20  e3_blob_close | 
7c30: 63 6c 6f 73 65 5d 20 61 6c 6c 20 5b 42 4c 4f 42  close] all [BLOB
7c40: 20 68 61 6e 64 6c 65 73 5d 2c 20 61 6e 64 20 0a   handles], and .
7c50: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b  ** [sqlite3_back
7c60: 75 70 5f 66 69 6e 69 73 68 20 7c 20 66 69 6e 69  up_finish | fini
7c70: 73 68 5d 20 61 6c 6c 20 5b 73 71 6c 69 74 65 33  sh] all [sqlite3
7c80: 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 73  _backup] objects
7c90: 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77   associated.** w
7ca0: 69 74 68 20 74 68 65 20 5b 73 71 6c 69 74 65 33  ith the [sqlite3
7cb0: 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20 74  ] object prior t
7cc0: 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  o attempting to 
7cd0: 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63 74  close the object
7ce0: 2e 20 20 5e 49 66 0a 2a 2a 20 73 71 6c 69 74 65  .  ^If.** sqlite
7cf0: 33 5f 63 6c 6f 73 65 28 29 20 69 73 20 63 61 6c  3_close() is cal
7d00: 6c 65 64 20 6f 6e 20 61 20 5b 64 61 74 61 62 61  led on a [databa
7d10: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74  se connection] t
7d20: 68 61 74 20 73 74 69 6c 6c 20 68 61 73 0a 2a 2a  hat still has.**
7d30: 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 5b 70 72   outstanding [pr
7d40: 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
7d50: 73 5d 2c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65  s], [BLOB handle
7d60: 73 5d 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 5b 73  s], and/or.** [s
7d70: 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f  qlite3_backup] o
7d80: 62 6a 65 63 74 73 20 74 68 65 6e 20 69 74 20 72  bjects then it r
7d90: 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
7da0: 20 62 75 74 20 74 68 65 20 64 65 61 6c 6c 6f 63   but the dealloc
7db0: 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 72 65 73 6f  ation.** of reso
7dc0: 75 72 63 65 73 20 69 73 20 64 65 66 65 72 72 65  urces is deferre
7dd0: 64 20 75 6e 74 69 6c 20 61 6c 6c 20 5b 70 72 65  d until all [pre
7de0: 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
7df0: 5d 2c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73  ], [BLOB handles
7e00: 5d 2c 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74  ],.** and [sqlit
7e10: 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63  e3_backup] objec
7e20: 74 73 20 61 72 65 20 61 6c 73 6f 20 64 65 73 74  ts are also dest
7e30: 72 6f 79 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66  royed..**.** ^If
7e40: 20 61 6e 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62   an [sqlite3] ob
7e50: 6a 65 63 74 20 69 73 20 64 65 73 74 72 6f 79 65  ject is destroye
7e60: 64 20 77 68 69 6c 65 20 61 20 74 72 61 6e 73 61  d while a transa
7e70: 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a 2a  ction is open,.*
7e80: 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  * the transactio
7e90: 6e 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  n is automatical
7ea0: 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a  ly rolled back..
7eb0: 2a 2a 0a 2a 2a 20 54 68 65 20 43 20 70 61 72 61  **.** The C para
7ec0: 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
7ed0: 33 5f 63 6c 6f 73 65 28 43 29 5d 20 61 6e 64 20  3_close(C)] and 
7ee0: 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5f 76  [sqlite3_close_v
7ef0: 32 28 43 29 5d 0a 2a 2a 20 6d 75 73 74 20 62 65  2(C)].** must be
7f00: 20 65 69 74 68 65 72 20 61 20 4e 55 4c 4c 0a 2a   either a NULL.*
7f10: 2a 20 70 6f 69 6e 74 65 72 20 6f 72 20 61 6e 20  * pointer or an 
7f20: 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74  [sqlite3] object
7f30: 20 70 6f 69 6e 74 65 72 20 6f 62 74 61 69 6e 65   pointer obtaine
7f40: 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74  d.** from [sqlit
7f50: 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c  e3_open()], [sql
7f60: 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20  ite3_open16()], 
7f70: 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f  or.** [sqlite3_o
7f80: 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 20 6e  pen_v2()], and n
7f90: 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6c  ot previously cl
7fa0: 6f 73 65 64 2e 0a 2a 2a 20 5e 43 61 6c 6c 69 6e  osed..** ^Callin
7fb0: 67 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28  g sqlite3_close(
7fc0: 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6c 6f  ) or sqlite3_clo
7fd0: 73 65 5f 76 32 28 29 20 77 69 74 68 20 61 20 4e  se_v2() with a N
7fe0: 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 61  ULL pointer.** a
7ff0: 72 67 75 6d 65 6e 74 20 69 73 20 61 20 68 61 72  rgument is a har
8000: 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a  mless no-op..*/.
8010: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
8020: 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c  qlite3_close(sql
8030: 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  ite3*);.SQLITE_A
8040: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
8050: 6c 6f 73 65 5f 76 32 28 73 71 6c 69 74 65 33 2a  lose_v2(sqlite3*
8060: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79  );../*.** The ty
8070: 70 65 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 63  pe for a callbac
8080: 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54  k function..** T
8090: 68 69 73 20 69 73 20 6c 65 67 61 63 79 20 61 6e  his is legacy an
80a0: 64 20 64 65 70 72 65 63 61 74 65 64 2e 20 20 49  d deprecated.  I
80b0: 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 66 6f  t is included fo
80c0: 72 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a 20  r historical.** 
80d0: 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 61 6e  compatibility an
80e0: 64 20 69 73 20 6e 6f 74 20 64 6f 63 75 6d 65 6e  d is not documen
80f0: 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ted..*/.typedef 
8100: 69 6e 74 20 28 2a 73 71 6c 69 74 65 33 5f 63 61  int (*sqlite3_ca
8110: 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e  llback)(void*,in
8120: 74 2c 63 68 61 72 2a 2a 2c 20 63 68 61 72 2a 2a  t,char**, char**
8130: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
8140: 45 46 3a 20 4f 6e 65 2d 53 74 65 70 20 51 75 65  EF: One-Step Que
8150: 72 79 20 45 78 65 63 75 74 69 6f 6e 20 49 6e 74  ry Execution Int
8160: 65 72 66 61 63 65 0a 2a 2a 0a 2a 2a 20 54 68 65  erface.**.** The
8170: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
8180: 69 6e 74 65 72 66 61 63 65 20 69 73 20 61 20 63  interface is a c
8190: 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70  onvenience wrapp
81a0: 65 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 5b 73 71  er around.** [sq
81b0: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
81c0: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74  ()], [sqlite3_st
81d0: 65 70 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69  ep()], and [sqli
81e0: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2c  te3_finalize()],
81f0: 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 6f 77 73 20  .** that allows 
8200: 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74  an application t
8210: 6f 20 72 75 6e 20 6d 75 6c 74 69 70 6c 65 20 73  o run multiple s
8220: 74 61 74 65 6d 65 6e 74 73 20 6f 66 20 53 51 4c  tatements of SQL
8230: 0a 2a 2a 20 77 69 74 68 6f 75 74 20 68 61 76 69  .** without havi
8240: 6e 67 20 74 6f 20 75 73 65 20 61 20 6c 6f 74 20  ng to use a lot 
8250: 6f 66 20 43 20 63 6f 64 65 2e 20 0a 2a 2a 0a 2a  of C code. .**.*
8260: 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 65  * ^The sqlite3_e
8270: 78 65 63 28 29 20 69 6e 74 65 72 66 61 63 65 20  xec() interface 
8280: 72 75 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72  runs zero or mor
8290: 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2c  e UTF-8 encoded,
82a0: 0a 2a 2a 20 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65  .** semicolon-se
82b0: 70 61 72 61 74 65 20 53 51 4c 20 73 74 61 74 65  parate SQL state
82c0: 6d 65 6e 74 73 20 70 61 73 73 65 64 20 69 6e 74  ments passed int
82d0: 6f 20 69 74 73 20 32 6e 64 20 61 72 67 75 6d 65  o its 2nd argume
82e0: 6e 74 2c 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f  nt,.** in the co
82f0: 6e 74 65 78 74 20 6f 66 20 74 68 65 20 5b 64 61  ntext of the [da
8300: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
8310: 6e 5d 20 70 61 73 73 65 64 20 69 6e 20 61 73 20  n] passed in as 
8320: 69 74 73 20 31 73 74 0a 2a 2a 20 61 72 67 75 6d  its 1st.** argum
8330: 65 6e 74 2e 20 20 5e 49 66 20 74 68 65 20 63 61  ent.  ^If the ca
8340: 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
8350: 6f 66 20 74 68 65 20 33 72 64 20 61 72 67 75 6d  of the 3rd argum
8360: 65 6e 74 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  ent to.** sqlite
8370: 33 5f 65 78 65 63 28 29 20 69 73 20 6e 6f 74 20  3_exec() is not 
8380: 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73  NULL, then it is
8390: 20 69 6e 76 6f 6b 65 64 20 66 6f 72 20 65 61 63   invoked for eac
83a0: 68 20 72 65 73 75 6c 74 20 72 6f 77 0a 2a 2a 20  h result row.** 
83b0: 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74 68  coming out of th
83c0: 65 20 65 76 61 6c 75 61 74 65 64 20 53 51 4c 20  e evaluated SQL 
83d0: 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 5e 54 68  statements.  ^Th
83e0: 65 20 34 74 68 20 61 72 67 75 6d 65 6e 74 20 74  e 4th argument t
83f0: 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 78 65  o.** sqlite3_exe
8400: 63 28 29 20 69 73 20 72 65 6c 61 79 65 64 20 74  c() is relayed t
8410: 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 31 73  hrough to the 1s
8420: 74 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 65 61  t argument of ea
8430: 63 68 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69  ch.** callback i
8440: 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 5e 49 66 20  nvocation.  ^If 
8450: 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 70 6f 69  the callback poi
8460: 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f  nter to sqlite3_
8470: 65 78 65 63 28 29 0a 2a 2a 20 69 73 20 4e 55 4c  exec().** is NUL
8480: 4c 2c 20 74 68 65 6e 20 6e 6f 20 63 61 6c 6c 62  L, then no callb
8490: 61 63 6b 20 69 73 20 65 76 65 72 20 69 6e 76 6f  ack is ever invo
84a0: 6b 65 64 20 61 6e 64 20 72 65 73 75 6c 74 20 72  ked and result r
84b0: 6f 77 73 20 61 72 65 0a 2a 2a 20 69 67 6e 6f 72  ows are.** ignor
84c0: 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 61 6e  ed..**.** ^If an
84d0: 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68   error occurs wh
84e0: 69 6c 65 20 65 76 61 6c 75 61 74 69 6e 67 20 74  ile evaluating t
84f0: 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
8500: 73 20 70 61 73 73 65 64 20 69 6e 74 6f 0a 2a 2a  s passed into.**
8510: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2c   sqlite3_exec(),
8520: 20 74 68 65 6e 20 65 78 65 63 75 74 69 6f 6e 20   then execution 
8530: 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  of the current s
8540: 74 61 74 65 6d 65 6e 74 20 73 74 6f 70 73 20 61  tatement stops a
8550: 6e 64 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74  nd.** subsequent
8560: 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20   statements are 
8570: 73 6b 69 70 70 65 64 2e 20 20 5e 49 66 20 74 68  skipped.  ^If th
8580: 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 5th parameter 
8590: 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  to sqlite3_exec(
85a0: 29 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  ).** is not NULL
85b0: 20 74 68 65 6e 20 61 6e 79 20 65 72 72 6f 72 20   then any error 
85c0: 6d 65 73 73 61 67 65 20 69 73 20 77 72 69 74 74  message is writt
85d0: 65 6e 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f  en into memory o
85e0: 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20  btained.** from 
85f0: 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
8600: 29 5d 20 61 6e 64 20 70 61 73 73 65 64 20 62 61  )] and passed ba
8610: 63 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 35  ck through the 5
8620: 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a  th parameter..**
8630: 20 54 6f 20 61 76 6f 69 64 20 6d 65 6d 6f 72 79   To avoid memory
8640: 20 6c 65 61 6b 73 2c 20 74 68 65 20 61 70 70 6c   leaks, the appl
8650: 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 69  ication should i
8660: 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f 66  nvoke [sqlite3_f
8670: 72 65 65 28 29 5d 0a 2a 2a 20 6f 6e 20 65 72 72  ree()].** on err
8680: 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e  or message strin
8690: 67 73 20 72 65 74 75 72 6e 65 64 20 74 68 72 6f  gs returned thro
86a0: 75 67 68 20 74 68 65 20 35 74 68 20 70 61 72 61  ugh the 5th para
86b0: 6d 65 74 65 72 20 6f 66 0a 2a 2a 20 6f 66 20 73  meter of.** of s
86c0: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 61 66  qlite3_exec() af
86d0: 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 6d 65  ter the error me
86e0: 73 73 61 67 65 20 73 74 72 69 6e 67 20 69 73 20  ssage string is 
86f0: 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65 64 65 64  no longer needed
8700: 2e 0a 2a 2a 20 5e 49 66 20 74 68 65 20 35 74 68  ..** ^If the 5th
8710: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
8720: 6c 69 74 65 33 5f 65 78 65 63 28 29 20 69 73 20  lite3_exec() is 
8730: 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 6e 6f 20  not NULL and no 
8740: 65 72 72 6f 72 73 0a 2a 2a 20 6f 63 63 75 72 2c  errors.** occur,
8750: 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 65 78   then sqlite3_ex
8760: 65 63 28 29 20 73 65 74 73 20 74 68 65 20 70 6f  ec() sets the po
8770: 69 6e 74 65 72 20 69 6e 20 69 74 73 20 35 74 68  inter in its 5th
8780: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a   parameter to.**
8790: 20 4e 55 4c 4c 20 62 65 66 6f 72 65 20 72 65 74   NULL before ret
87a0: 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5e 49  urning..**.** ^I
87b0: 66 20 61 6e 20 73 71 6c 69 74 65 33 5f 65 78 65  f an sqlite3_exe
87c0: 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  c() callback ret
87d0: 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  urns non-zero, t
87e0: 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  he sqlite3_exec(
87f0: 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74  ).** routine ret
8800: 75 72 6e 73 20 53 51 4c 49 54 45 5f 41 42 4f 52  urns SQLITE_ABOR
8810: 54 20 77 69 74 68 6f 75 74 20 69 6e 76 6f 6b 69  T without invoki
8820: 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  ng the callback 
8830: 61 67 61 69 6e 20 61 6e 64 0a 2a 2a 20 77 69 74  again and.** wit
8840: 68 6f 75 74 20 72 75 6e 6e 69 6e 67 20 61 6e 79  hout running any
8850: 20 73 75 62 73 65 71 75 65 6e 74 20 53 51 4c 20   subsequent SQL 
8860: 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  statements..**.*
8870: 2a 20 5e 54 68 65 20 32 6e 64 20 61 72 67 75 6d  * ^The 2nd argum
8880: 65 6e 74 20 74 6f 20 74 68 65 20 73 71 6c 69 74  ent to the sqlit
8890: 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 62 61  e3_exec() callba
88a0: 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74  ck function is t
88b0: 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
88c0: 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72  columns in the r
88d0: 65 73 75 6c 74 2e 20 20 5e 54 68 65 20 33 72 64  esult.  ^The 3rd
88e0: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
88f0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 0a   sqlite3_exec().
8900: 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61  ** callback is a
8910: 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74  n array of point
8920: 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20 6f  ers to strings o
8930: 62 74 61 69 6e 65 64 20 61 73 20 69 66 20 66 72  btained as if fr
8940: 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  om.** [sqlite3_c
8950: 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20 6f  olumn_text()], o
8960: 6e 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75  ne for each colu
8970: 6d 6e 2e 20 20 5e 49 66 20 61 6e 20 65 6c 65 6d  mn.  ^If an elem
8980: 65 6e 74 20 6f 66 20 61 0a 2a 2a 20 72 65 73 75  ent of a.** resu
8990: 6c 74 20 72 6f 77 20 69 73 20 4e 55 4c 4c 20 74  lt row is NULL t
89a0: 68 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f  hen the correspo
89b0: 6e 64 69 6e 67 20 73 74 72 69 6e 67 20 70 6f 69  nding string poi
89c0: 6e 74 65 72 20 66 6f 72 20 74 68 65 0a 2a 2a 20  nter for the.** 
89d0: 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63  sqlite3_exec() c
89e0: 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 4e 55 4c  allback is a NUL
89f0: 4c 20 70 6f 69 6e 74 65 72 2e 20 20 5e 54 68 65  L pointer.  ^The
8a00: 20 34 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f   4th argument to
8a10: 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   the.** sqlite3_
8a20: 65 78 65 63 28 29 20 63 61 6c 6c 62 61 63 6b 20  exec() callback 
8a30: 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70  is an array of p
8a40: 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72 69 6e  ointers to strin
8a50: 67 73 20 77 68 65 72 65 20 65 61 63 68 0a 2a 2a  gs where each.**
8a60: 20 65 6e 74 72 79 20 72 65 70 72 65 73 65 6e 74   entry represent
8a70: 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 63 6f  s the name of co
8a80: 72 72 65 73 70 6f 6e 64 69 6e 67 20 72 65 73 75  rresponding resu
8a90: 6c 74 20 63 6f 6c 75 6d 6e 20 61 73 20 6f 62 74  lt column as obt
8aa0: 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73  ained.** from [s
8ab0: 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
8ac0: 6d 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66  me()]..**.** ^If
8ad0: 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74   the 2nd paramet
8ae0: 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 78  er to sqlite3_ex
8af0: 65 63 28 29 20 69 73 20 61 20 4e 55 4c 4c 20 70  ec() is a NULL p
8b00: 6f 69 6e 74 65 72 2c 20 61 20 70 6f 69 6e 74 65  ointer, a pointe
8b10: 72 0a 2a 2a 20 74 6f 20 61 6e 20 65 6d 70 74 79  r.** to an empty
8b20: 20 73 74 72 69 6e 67 2c 20 6f 72 20 61 20 70 6f   string, or a po
8b30: 69 6e 74 65 72 20 74 68 61 74 20 63 6f 6e 74 61  inter that conta
8b40: 69 6e 73 20 6f 6e 6c 79 20 77 68 69 74 65 73 70  ins only whitesp
8b50: 61 63 65 20 61 6e 64 2f 6f 72 20 0a 2a 2a 20 53  ace and/or .** S
8b60: 51 4c 20 63 6f 6d 6d 65 6e 74 73 2c 20 74 68 65  QL comments, the
8b70: 6e 20 6e 6f 20 53 51 4c 20 73 74 61 74 65 6d 65  n no SQL stateme
8b80: 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74 65  nts are evaluate
8b90: 64 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61  d and the databa
8ba0: 73 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 68 61  se.** is not cha
8bb0: 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 74  nged..**.** Rest
8bc0: 72 69 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20  rictions:.**.** 
8bd0: 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65  <ul>.** <li> The
8be0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73   application mus
8bf0: 74 20 69 6e 73 75 72 65 20 74 68 61 74 20 74 68  t insure that th
8c00: 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 20  e 1st parameter 
8c10: 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  to sqlite3_exec(
8c20: 29 0a 2a 2a 20 20 20 20 20 20 69 73 20 61 20 76  ).**      is a v
8c30: 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 20 5b 64  alid and open [d
8c40: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
8c50: 6f 6e 5d 2e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65  on]..** <li> The
8c60: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73   application mus
8c70: 74 20 6e 6f 74 20 63 6c 6f 73 65 20 5b 64 61 74  t not close [dat
8c80: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
8c90: 5d 20 73 70 65 63 69 66 69 65 64 20 62 79 0a 2a  ] specified by.*
8ca0: 2a 20 20 20 20 20 20 74 68 65 20 31 73 74 20 70  *      the 1st p
8cb0: 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
8cc0: 74 65 33 5f 65 78 65 63 28 29 20 77 68 69 6c 65  te3_exec() while
8cd0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
8ce0: 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 20 3c  is running..** <
8cf0: 6c 69 3e 20 54 68 65 20 61 70 70 6c 69 63 61 74  li> The applicat
8d00: 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 6d 6f 64  ion must not mod
8d10: 69 66 79 20 74 68 65 20 53 51 4c 20 73 74 61 74  ify the SQL stat
8d20: 65 6d 65 6e 74 20 74 65 78 74 20 70 61 73 73 65  ement text passe
8d30: 64 20 69 6e 74 6f 0a 2a 2a 20 20 20 20 20 20 74  d into.**      t
8d40: 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72  he 2nd parameter
8d50: 20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63   of sqlite3_exec
8d60: 28 29 20 77 68 69 6c 65 20 73 71 6c 69 74 65 33  () while sqlite3
8d70: 5f 65 78 65 63 28 29 20 69 73 20 72 75 6e 6e 69  _exec() is runni
8d80: 6e 67 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2f 0a  ng..** </ul>.*/.
8d90: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
8da0: 71 6c 69 74 65 33 5f 65 78 65 63 28 0a 20 20 73  qlite3_exec(.  s
8db0: 71 6c 69 74 65 33 2a 2c 20 20 20 20 20 20 20 20  qlite3*,        
8dc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8dd0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20            /* An 
8de0: 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f  open database */
8df0: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  .  const char *s
8e00: 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ql,             
8e10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
8e20: 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c 75   SQL to be evalu
8e30: 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  ated */.  int (*
8e40: 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c  callback)(void*,
8e50: 69 6e 74 2c 63 68 61 72 2a 2a 2c 63 68 61 72 2a  int,char**,char*
8e60: 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b  *),  /* Callback
8e70: 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76   function */.  v
8e80: 6f 69 64 20 2a 2c 20 20 20 20 20 20 20 20 20 20  oid *,          
8e90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8ea0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74            /* 1st
8eb0: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 61 6c   argument to cal
8ec0: 6c 62 61 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20  lback */.  char 
8ed0: 2a 2a 65 72 72 6d 73 67 20 20 20 20 20 20 20 20  **errmsg        
8ee0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8ef0: 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d        /* Error m
8f00: 73 67 20 77 72 69 74 74 65 6e 20 68 65 72 65 20  sg written here 
8f10: 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.);../*.** CAP
8f20: 49 33 52 45 46 3a 20 52 65 73 75 6c 74 20 43 6f  I3REF: Result Co
8f30: 64 65 73 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  des.** KEYWORDS:
8f40: 20 53 51 4c 49 54 45 5f 4f 4b 20 7b 65 72 72 6f   SQLITE_OK {erro
8f50: 72 20 63 6f 64 65 7d 20 7b 65 72 72 6f 72 20 63  r code} {error c
8f60: 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44  odes}.** KEYWORD
8f70: 53 3a 20 7b 72 65 73 75 6c 74 20 63 6f 64 65 7d  S: {result code}
8f80: 20 7b 72 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a   {result codes}.
8f90: 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 53 51 4c 69 74  **.** Many SQLit
8fa0: 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75  e functions retu
8fb0: 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65  rn an integer re
8fc0: 73 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74  sult code from t
8fd0: 68 65 20 73 65 74 20 73 68 6f 77 6e 0a 2a 2a 20  he set shown.** 
8fe0: 68 65 72 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  here in order to
8ff0: 20 69 6e 64 69 63 61 74 65 20 73 75 63 63 65 73   indicate succes
9000: 73 20 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a 2a  s or failure..**
9010: 0a 2a 2a 20 4e 65 77 20 65 72 72 6f 72 20 63 6f  .** New error co
9020: 64 65 73 20 6d 61 79 20 62 65 20 61 64 64 65 64  des may be added
9030: 20 69 6e 20 66 75 74 75 72 65 20 76 65 72 73 69   in future versi
9040: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  ons of SQLite..*
9050: 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  *.** See also: [
9060: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41  SQLITE_IOERR_REA
9070: 44 20 7c 20 65 78 74 65 6e 64 65 64 20 72 65 73  D | extended res
9080: 75 6c 74 20 63 6f 64 65 73 5d 2c 0a 2a 2a 20 5b  ult codes],.** [
9090: 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 6f 6e 5f  sqlite3_vtab_on_
90a0: 63 6f 6e 66 6c 69 63 74 28 29 5d 20 5b 53 51 4c  conflict()] [SQL
90b0: 49 54 45 5f 52 4f 4c 4c 42 41 43 4b 20 7c 20 72  ITE_ROLLBACK | r
90c0: 65 73 75 6c 74 20 63 6f 64 65 73 5d 2e 0a 2a 2f  esult codes]..*/
90d0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
90e0: 4f 4b 20 20 20 20 20 20 20 20 20 20 20 30 20 20  OK           0  
90f0: 20 2f 2a 20 53 75 63 63 65 73 73 66 75 6c 20 72   /* Successful r
9100: 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20 62 65 67 69  esult */./* begi
9110: 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72 6f 72 2d 63  nning-of-error-c
9120: 6f 64 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  odes */.#define 
9130: 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 20 20 20  SQLITE_ERROR    
9140: 20 20 20 20 31 20 20 20 2f 2a 20 53 51 4c 20 65      1   /* SQL e
9150: 72 72 6f 72 20 6f 72 20 6d 69 73 73 69 6e 67 20  rror or missing 
9160: 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 66  database */.#def
9170: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 52  ine SQLITE_INTER
9180: 4e 41 4c 20 20 20 20 20 32 20 20 20 2f 2a 20 49  NAL     2   /* I
9190: 6e 74 65 72 6e 61 6c 20 6c 6f 67 69 63 20 65 72  nternal logic er
91a0: 72 6f 72 20 69 6e 20 53 51 4c 69 74 65 20 2a 2f  ror in SQLite */
91b0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
91c0: 50 45 52 4d 20 20 20 20 20 20 20 20 20 33 20 20  PERM         3  
91d0: 20 2f 2a 20 41 63 63 65 73 73 20 70 65 72 6d 69   /* Access permi
91e0: 73 73 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f 0a  ssion denied */.
91f0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
9200: 42 4f 52 54 20 20 20 20 20 20 20 20 34 20 20 20  BORT        4   
9210: 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 72 6f 75 74  /* Callback rout
9220: 69 6e 65 20 72 65 71 75 65 73 74 65 64 20 61 6e  ine requested an
9230: 20 61 62 6f 72 74 20 2a 2f 0a 23 64 65 66 69 6e   abort */.#defin
9240: 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 20 20  e SQLITE_BUSY   
9250: 20 20 20 20 20 20 35 20 20 20 2f 2a 20 54 68 65        5   /* The
9260: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
9270: 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 65 66  s locked */.#def
9280: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  ine SQLITE_LOCKE
9290: 44 20 20 20 20 20 20 20 36 20 20 20 2f 2a 20 41  D       6   /* A
92a0: 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61   table in the da
92b0: 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64  tabase is locked
92c0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
92d0: 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20  TE_NOMEM        
92e0: 37 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 28  7   /* A malloc(
92f0: 29 20 66 61 69 6c 65 64 20 2a 2f 0a 23 64 65 66  ) failed */.#def
9300: 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  ine SQLITE_READO
9310: 4e 4c 59 20 20 20 20 20 38 20 20 20 2f 2a 20 41  NLY     8   /* A
9320: 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20  ttempt to write 
9330: 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 61 62  a readonly datab
9340: 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ase */.#define S
9350: 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20  QLITE_INTERRUPT 
9360: 20 20 20 39 20 20 20 2f 2a 20 4f 70 65 72 61 74     9   /* Operat
9370: 69 6f 6e 20 74 65 72 6d 69 6e 61 74 65 64 20 62  ion terminated b
9380: 79 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  y sqlite3_interr
9390: 75 70 74 28 29 2a 2f 0a 23 64 65 66 69 6e 65 20  upt()*/.#define 
93a0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 20 20 20  SQLITE_IOERR    
93b0: 20 20 20 31 30 20 20 20 2f 2a 20 53 6f 6d 65 20     10   /* Some 
93c0: 6b 69 6e 64 20 6f 66 20 64 69 73 6b 20 49 2f 4f  kind of disk I/O
93d0: 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20   error occurred 
93e0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
93f0: 45 5f 43 4f 52 52 55 50 54 20 20 20 20 20 31 31  E_CORRUPT     11
9400: 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
9410: 73 65 20 64 69 73 6b 20 69 6d 61 67 65 20 69 73  se disk image is
9420: 20 6d 61 6c 66 6f 72 6d 65 64 20 2a 2f 0a 23 64   malformed */.#d
9430: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 54  efine SQLITE_NOT
9440: 46 4f 55 4e 44 20 20 20 20 31 32 20 20 20 2f 2a  FOUND    12   /*
9450: 20 55 6e 6b 6e 6f 77 6e 20 6f 70 63 6f 64 65 20   Unknown opcode 
9460: 69 6e 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  in sqlite3_file_
9470: 63 6f 6e 74 72 6f 6c 28 29 20 2a 2f 0a 23 64 65  control() */.#de
9480: 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4c 4c  fine SQLITE_FULL
9490: 20 20 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20          13   /* 
94a0: 49 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c 65 64  Insertion failed
94b0: 20 62 65 63 61 75 73 65 20 64 61 74 61 62 61 73   because databas
94c0: 65 20 69 73 20 66 75 6c 6c 20 2a 2f 0a 23 64 65  e is full */.#de
94d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e 54  fine SQLITE_CANT
94e0: 4f 50 45 4e 20 20 20 20 31 34 20 20 20 2f 2a 20  OPEN    14   /* 
94f0: 55 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74  Unable to open t
9500: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
9510: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
9520: 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20 31  TE_PROTOCOL    1
9530: 35 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20  5   /* Database 
9540: 6c 6f 63 6b 20 70 72 6f 74 6f 63 6f 6c 20 65 72  lock protocol er
9550: 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ror */.#define S
9560: 51 4c 49 54 45 5f 45 4d 50 54 59 20 20 20 20 20  QLITE_EMPTY     
9570: 20 20 31 36 20 20 20 2f 2a 20 44 61 74 61 62 61    16   /* Databa
9580: 73 65 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23  se is empty */.#
9590: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 43  define SQLITE_SC
95a0: 48 45 4d 41 20 20 20 20 20 20 31 37 20 20 20 2f  HEMA      17   /
95b0: 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 73  * The database s
95c0: 63 68 65 6d 61 20 63 68 61 6e 67 65 64 20 2a 2f  chema changed */
95d0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
95e0: 54 4f 4f 42 49 47 20 20 20 20 20 20 31 38 20 20  TOOBIG      18  
95f0: 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c   /* String or BL
9600: 4f 42 20 65 78 63 65 65 64 73 20 73 69 7a 65 20  OB exceeds size 
9610: 6c 69 6d 69 74 20 2a 2f 0a 23 64 65 66 69 6e 65  limit */.#define
9620: 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
9630: 4e 54 20 20 31 39 20 20 20 2f 2a 20 41 62 6f 72  NT  19   /* Abor
9640: 74 20 64 75 65 20 74 6f 20 63 6f 6e 73 74 72 61  t due to constra
9650: 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 2a 2f  int violation */
9660: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
9670: 4d 49 53 4d 41 54 43 48 20 20 20 20 32 30 20 20  MISMATCH    20  
9680: 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20 6d 69   /* Data type mi
9690: 73 6d 61 74 63 68 20 2a 2f 0a 23 64 65 66 69 6e  smatch */.#defin
96a0: 65 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20  e SQLITE_MISUSE 
96b0: 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4c 69 62       21   /* Lib
96c0: 72 61 72 79 20 75 73 65 64 20 69 6e 63 6f 72 72  rary used incorr
96d0: 65 63 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65  ectly */.#define
96e0: 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 20 20   SQLITE_NOLFS   
96f0: 20 20 20 20 32 32 20 20 20 2f 2a 20 55 73 65 73      22   /* Uses
9700: 20 4f 53 20 66 65 61 74 75 72 65 73 20 6e 6f 74   OS features not
9710: 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 68 6f   supported on ho
9720: 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  st */.#define SQ
9730: 4c 49 54 45 5f 41 55 54 48 20 20 20 20 20 20 20  LITE_AUTH       
9740: 20 32 33 20 20 20 2f 2a 20 41 75 74 68 6f 72 69   23   /* Authori
9750: 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f  zation denied */
9760: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
9770: 46 4f 52 4d 41 54 20 20 20 20 20 20 32 34 20 20  FORMAT      24  
9780: 20 2f 2a 20 41 75 78 69 6c 69 61 72 79 20 64 61   /* Auxiliary da
9790: 74 61 62 61 73 65 20 66 6f 72 6d 61 74 20 65 72  tabase format er
97a0: 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ror */.#define S
97b0: 51 4c 49 54 45 5f 52 41 4e 47 45 20 20 20 20 20  QLITE_RANGE     
97c0: 20 20 32 35 20 20 20 2f 2a 20 32 6e 64 20 70 61    25   /* 2nd pa
97d0: 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
97e0: 65 33 5f 62 69 6e 64 20 6f 75 74 20 6f 66 20 72  e3_bind out of r
97f0: 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ange */.#define 
9800: 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20 20 20  SQLITE_NOTADB   
9810: 20 20 20 32 36 20 20 20 2f 2a 20 46 69 6c 65 20     26   /* File 
9820: 6f 70 65 6e 65 64 20 74 68 61 74 20 69 73 20 6e  opened that is n
9830: 6f 74 20 61 20 64 61 74 61 62 61 73 65 20 66 69  ot a database fi
9840: 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  le */.#define SQ
9850: 4c 49 54 45 5f 52 4f 57 20 20 20 20 20 20 20 20  LITE_ROW        
9860: 20 31 30 30 20 20 2f 2a 20 73 71 6c 69 74 65 33   100  /* sqlite3
9870: 5f 73 74 65 70 28 29 20 68 61 73 20 61 6e 6f 74  _step() has anot
9880: 68 65 72 20 72 6f 77 20 72 65 61 64 79 20 2a 2f  her row ready */
9890: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
98a0: 44 4f 4e 45 20 20 20 20 20 20 20 20 31 30 31 20  DONE        101 
98b0: 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70   /* sqlite3_step
98c0: 28 29 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  () has finished 
98d0: 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 2f 2a 20  executing */./* 
98e0: 65 6e 64 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64  end-of-error-cod
98f0: 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  es */../*.** CAP
9900: 49 33 52 45 46 3a 20 45 78 74 65 6e 64 65 64 20  I3REF: Extended 
9910: 52 65 73 75 6c 74 20 43 6f 64 65 73 0a 2a 2a 20  Result Codes.** 
9920: 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 74 65 6e  KEYWORDS: {exten
9930: 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 7d 20  ded error code} 
9940: 7b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20  {extended error 
9950: 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52  codes}.** KEYWOR
9960: 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 72 65  DS: {extended re
9970: 73 75 6c 74 20 63 6f 64 65 7d 20 7b 65 78 74 65  sult code} {exte
9980: 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65  nded result code
9990: 73 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 69 74 73 20  s}.**.** In its 
99a0: 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72  default configur
99b0: 61 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 41 50  ation, SQLite AP
99c0: 49 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  I routines retur
99d0: 6e 20 6f 6e 65 20 6f 66 20 32 36 20 69 6e 74 65  n one of 26 inte
99e0: 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f  ger.** [SQLITE_O
99f0: 4b 20 7c 20 72 65 73 75 6c 74 20 63 6f 64 65 73  K | result codes
9a00: 5d 2e 20 20 48 6f 77 65 76 65 72 2c 20 65 78 70  ].  However, exp
9a10: 65 72 69 65 6e 63 65 20 68 61 73 20 73 68 6f 77  erience has show
9a20: 6e 20 74 68 61 74 20 6d 61 6e 79 20 6f 66 0a 2a  n that many of.*
9a30: 2a 20 74 68 65 73 65 20 72 65 73 75 6c 74 20 63  * these result c
9a40: 6f 64 65 73 20 61 72 65 20 74 6f 6f 20 63 6f 61  odes are too coa
9a50: 72 73 65 2d 67 72 61 69 6e 65 64 2e 20 20 54 68  rse-grained.  Th
9a60: 65 79 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64  ey do not provid
9a70: 65 20 61 73 0a 2a 2a 20 6d 75 63 68 20 69 6e 66  e as.** much inf
9a80: 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 70  ormation about p
9a90: 72 6f 62 6c 65 6d 73 20 61 73 20 70 72 6f 67 72  roblems as progr
9aa0: 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 6c 69 6b  ammers might lik
9ab0: 65 2e 20 20 49 6e 20 61 6e 20 65 66 66 6f 72 74  e.  In an effort
9ac0: 20 74 6f 0a 2a 2a 20 61 64 64 72 65 73 73 20 74   to.** address t
9ad0: 68 69 73 2c 20 6e 65 77 65 72 20 76 65 72 73 69  his, newer versi
9ae0: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 28 76  ons of SQLite (v
9af0: 65 72 73 69 6f 6e 20 33 2e 33 2e 38 20 61 6e 64  ersion 3.3.8 and
9b00: 20 6c 61 74 65 72 29 20 69 6e 63 6c 75 64 65 0a   later) include.
9b10: 2a 2a 20 73 75 70 70 6f 72 74 20 66 6f 72 20 61  ** support for a
9b20: 64 64 69 74 69 6f 6e 61 6c 20 72 65 73 75 6c 74  dditional result
9b30: 20 63 6f 64 65 73 20 74 68 61 74 20 70 72 6f 76   codes that prov
9b40: 69 64 65 20 6d 6f 72 65 20 64 65 74 61 69 6c 65  ide more detaile
9b50: 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a  d information.**
9b60: 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 20 54   about errors. T
9b70: 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75  he extended resu
9b80: 6c 74 20 63 6f 64 65 73 20 61 72 65 20 65 6e 61  lt codes are ena
9b90: 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64  bled or disabled
9ba0: 0a 2a 2a 20 6f 6e 20 61 20 70 65 72 20 64 61 74  .** on a per dat
9bb0: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
9bc0: 20 62 61 73 69 73 20 75 73 69 6e 67 20 74 68 65   basis using the
9bd0: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 74  .** [sqlite3_ext
9be0: 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64  ended_result_cod
9bf0: 65 73 28 29 5d 20 41 50 49 2e 0a 2a 2a 0a 2a 2a  es()] API..**.**
9c00: 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 76 61   Some of the ava
9c10: 69 6c 61 62 6c 65 20 65 78 74 65 6e 64 65 64 20  ilable extended 
9c20: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65  result codes are
9c30: 20 6c 69 73 74 65 64 20 68 65 72 65 2e 0a 2a 2a   listed here..**
9c40: 20 4f 6e 65 20 6d 61 79 20 65 78 70 65 63 74 20   One may expect 
9c50: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 78  the number of ex
9c60: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
9c70: 64 65 73 20 77 69 6c 6c 20 62 65 20 65 78 70 61  des will be expa
9c80: 6e 64 0a 2a 2a 20 6f 76 65 72 20 74 69 6d 65 2e  nd.** over time.
9c90: 20 20 53 6f 66 74 77 61 72 65 20 74 68 61 74 20    Software that 
9ca0: 75 73 65 73 20 65 78 74 65 6e 64 65 64 20 72 65  uses extended re
9cb0: 73 75 6c 74 20 63 6f 64 65 73 20 73 68 6f 75 6c  sult codes shoul
9cc0: 64 20 65 78 70 65 63 74 0a 2a 2a 20 74 6f 20 73  d expect.** to s
9cd0: 65 65 20 6e 65 77 20 72 65 73 75 6c 74 20 63 6f  ee new result co
9ce0: 64 65 73 20 69 6e 20 66 75 74 75 72 65 20 72 65  des in future re
9cf0: 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65  leases of SQLite
9d00: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ..**.** The SQLI
9d10: 54 45 5f 4f 4b 20 72 65 73 75 6c 74 20 63 6f 64  TE_OK result cod
9d20: 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  e will never be 
9d30: 65 78 74 65 6e 64 65 64 2e 20 20 49 74 20 77 69  extended.  It wi
9d40: 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20  ll always.** be 
9d50: 65 78 61 63 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2f  exactly zero..*/
9d60: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
9d70: 49 4f 45 52 52 5f 52 45 41 44 20 20 20 20 20 20  IOERR_READ      
9d80: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
9d90: 49 4f 45 52 52 20 7c 20 28 31 3c 3c 38 29 29 0a  IOERR | (1<<8)).
9da0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
9db0: 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20  OERR_SHORT_READ 
9dc0: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
9dd0: 4f 45 52 52 20 7c 20 28 32 3c 3c 38 29 29 0a 23  OERR | (2<<8)).#
9de0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f  define SQLITE_IO
9df0: 45 52 52 5f 57 52 49 54 45 20 20 20 20 20 20 20  ERR_WRITE       
9e00: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f        (SQLITE_IO
9e10: 45 52 52 20 7c 20 28 33 3c 3c 38 29 29 0a 23 64  ERR | (3<<8)).#d
9e20: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
9e30: 52 52 5f 46 53 59 4e 43 20 20 20 20 20 20 20 20  RR_FSYNC        
9e40: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
9e50: 52 52 20 7c 20 28 34 3c 3c 38 29 29 0a 23 64 65  RR | (4<<8)).#de
9e60: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52  fine SQLITE_IOER
9e70: 52 5f 44 49 52 5f 46 53 59 4e 43 20 20 20 20 20  R_DIR_FSYNC     
9e80: 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52      (SQLITE_IOER
9e90: 52 20 7c 20 28 35 3c 3c 38 29 29 0a 23 64 65 66  R | (5<<8)).#def
9ea0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
9eb0: 5f 54 52 55 4e 43 41 54 45 20 20 20 20 20 20 20  _TRUNCATE       
9ec0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
9ed0: 20 7c 20 28 36 3c 3c 38 29 29 0a 23 64 65 66 69   | (6<<8)).#defi
9ee0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  ne SQLITE_IOERR_
9ef0: 46 53 54 41 54 20 20 20 20 20 20 20 20 20 20 20  FSTAT           
9f00: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20    (SQLITE_IOERR 
9f10: 7c 20 28 37 3c 3c 38 29 29 0a 23 64 65 66 69 6e  | (7<<8)).#defin
9f20: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  e SQLITE_IOERR_U
9f30: 4e 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20  NLOCK           
9f40: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
9f50: 20 28 38 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65   (8<<8)).#define
9f60: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44   SQLITE_IOERR_RD
9f70: 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20 20  LOCK            
9f80: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20  (SQLITE_IOERR | 
9f90: 28 39 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  (9<<8)).#define 
9fa0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c  SQLITE_IOERR_DEL
9fb0: 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 28  ETE            (
9fc0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
9fd0: 31 30 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  10<<8)).#define 
9fe0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f  SQLITE_IOERR_BLO
9ff0: 43 4b 45 44 20 20 20 20 20 20 20 20 20 20 20 28  CKED           (
a000: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
a010: 31 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  11<<8)).#define 
a020: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
a030: 45 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 28  EM             (
a040: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
a050: 31 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  12<<8)).#define 
a060: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43  SQLITE_IOERR_ACC
a070: 45 53 53 20 20 20 20 20 20 20 20 20 20 20 20 28  ESS            (
a080: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
a090: 31 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  13<<8)).#define 
a0a0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
a0b0: 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 20 28  CKRESERVEDLOCK (
a0c0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
a0d0: 31 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  14<<8)).#define 
a0e0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43  SQLITE_IOERR_LOC
a0f0: 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28  K              (
a100: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
a110: 31 35 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  15<<8)).#define 
a120: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f  SQLITE_IOERR_CLO
a130: 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 28  SE             (
a140: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
a150: 31 36 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  16<<8)).#define 
a160: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52  SQLITE_IOERR_DIR
a170: 5f 43 4c 4f 53 45 20 20 20 20 20 20 20 20 20 28  _CLOSE         (
a180: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
a190: 31 37 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  17<<8)).#define 
a1a0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d  SQLITE_IOERR_SHM
a1b0: 4f 50 45 4e 20 20 20 20 20 20 20 20 20 20 20 28  OPEN           (
a1c0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
a1d0: 31 38 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  18<<8)).#define 
a1e0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d  SQLITE_IOERR_SHM
a1f0: 53 49 5a 45 20 20 20 20 20 20 20 20 20 20 20 28  SIZE           (
a200: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
a210: 31 39 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  19<<8)).#define 
a220: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d  SQLITE_IOERR_SHM
a230: 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20 28  LOCK           (
a240: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
a250: 32 30 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  20<<8)).#define 
a260: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d  SQLITE_IOERR_SHM
a270: 4d 41 50 20 20 20 20 20 20 20 20 20 20 20 20 28  MAP            (
a280: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
a290: 32 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  21<<8)).#define 
a2a0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 45 45  SQLITE_IOERR_SEE
a2b0: 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28  K              (
a2c0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
a2d0: 32 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  22<<8)).#define 
a2e0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c  SQLITE_IOERR_DEL
a2f0: 45 54 45 5f 4e 4f 45 4e 54 20 20 20 20 20 20 28  ETE_NOENT      (
a300: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
a310: 32 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  23<<8)).#define 
a320: 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48  SQLITE_LOCKED_SH
a330: 41 52 45 44 43 41 43 48 45 20 20 20 20 20 20 28  AREDCACHE      (
a340: 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 7c 20  SQLITE_LOCKED | 
a350: 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65   (1<<8)).#define
a360: 20 53 51 4c 49 54 45 5f 42 55 53 59 5f 52 45 43   SQLITE_BUSY_REC
a370: 4f 56 45 52 59 20 20 20 20 20 20 20 20 20 20 20  OVERY           
a380: 28 53 51 4c 49 54 45 5f 42 55 53 59 20 20 20 7c  (SQLITE_BUSY   |
a390: 20 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e    (1<<8)).#defin
a3a0: 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  e SQLITE_CANTOPE
a3b0: 4e 5f 4e 4f 54 45 4d 50 44 49 52 20 20 20 20 20  N_NOTEMPDIR     
a3c0: 20 28 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45   (SQLITE_CANTOPE
a3d0: 4e 20 7c 20 28 31 3c 3c 38 29 29 0a 23 64 65 66  N | (1<<8)).#def
a3e0: 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  ine SQLITE_CANTO
a3f0: 50 45 4e 5f 49 53 44 49 52 20 20 20 20 20 20 20  PEN_ISDIR       
a400: 20 20 20 28 53 51 4c 49 54 45 5f 43 41 4e 54 4f     (SQLITE_CANTO
a410: 50 45 4e 20 7c 20 28 32 3c 3c 38 29 29 0a 23 64  PEN | (2<<8)).#d
a420: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e  efine SQLITE_CAN
a430: 54 4f 50 45 4e 5f 46 55 4c 4c 50 41 54 48 20 20  TOPEN_FULLPATH  
a440: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 43 41 4e       (SQLITE_CAN
a450: 54 4f 50 45 4e 20 7c 20 28 33 3c 3c 38 29 29 0a  TOPEN | (3<<8)).
a460: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
a470: 4f 52 52 55 50 54 5f 56 54 41 42 20 20 20 20 20  ORRUPT_VTAB     
a480: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 43         (SQLITE_C
a490: 4f 52 52 55 50 54 20 7c 20 28 31 3c 3c 38 29 29  ORRUPT | (1<<8))
a4a0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
a4b0: 52 45 41 44 4f 4e 4c 59 5f 52 45 43 4f 56 45 52  READONLY_RECOVER
a4c0: 59 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f  Y       (SQLITE_
a4d0: 52 45 41 44 4f 4e 4c 59 20 7c 20 28 31 3c 3c 38  READONLY | (1<<8
a4e0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
a4f0: 45 5f 52 45 41 44 4f 4e 4c 59 5f 43 41 4e 54 4c  E_READONLY_CANTL
a500: 4f 43 4b 20 20 20 20 20 20 20 28 53 51 4c 49 54  OCK       (SQLIT
a510: 45 5f 52 45 41 44 4f 4e 4c 59 20 7c 20 28 32 3c  E_READONLY | (2<
a520: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
a530: 49 54 45 5f 41 42 4f 52 54 5f 52 4f 4c 4c 42 41  ITE_ABORT_ROLLBA
a540: 43 4b 20 20 20 20 20 20 20 20 20 20 28 53 51 4c  CK          (SQL
a550: 49 54 45 5f 41 42 4f 52 54 20 7c 20 28 32 3c 3c  ITE_ABORT | (2<<
a560: 38 29 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  8))../*.** CAPI3
a570: 52 45 46 3a 20 46 6c 61 67 73 20 46 6f 72 20 46  REF: Flags For F
a580: 69 6c 65 20 4f 70 65 6e 20 4f 70 65 72 61 74 69  ile Open Operati
a590: 6f 6e 73 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ons.**.** These 
a5a0: 62 69 74 20 76 61 6c 75 65 73 20 61 72 65 20 69  bit values are i
a5b0: 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
a5c0: 69 6e 20 74 68 65 0a 2a 2a 20 33 72 64 20 70 61  in the.** 3rd pa
a5d0: 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 5b  rameter to the [
a5e0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
a5f0: 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64  )] interface and
a600: 0a 2a 2a 20 69 6e 20 74 68 65 20 34 74 68 20 70  .** in the 4th p
a610: 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
a620: 5b 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70  [sqlite3_vfs.xOp
a630: 65 6e 5d 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 23  en] method..*/.#
a640: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a650: 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20  EN_READONLY     
a660: 20 20 20 20 30 78 30 30 30 30 30 30 30 31 20 20      0x00000001  
a670: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65  /* Ok for sqlite
a680: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23  3_open_v2() */.#
a690: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a6a0: 45 4e 5f 52 45 41 44 57 52 49 54 45 20 20 20 20  EN_READWRITE    
a6b0: 20 20 20 20 30 78 30 30 30 30 30 30 30 32 20 20      0x00000002  
a6c0: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65  /* Ok for sqlite
a6d0: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23  3_open_v2() */.#
a6e0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a6f0: 45 4e 5f 43 52 45 41 54 45 20 20 20 20 20 20 20  EN_CREATE       
a700: 20 20 20 20 30 78 30 30 30 30 30 30 30 34 20 20      0x00000004  
a710: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65  /* Ok for sqlite
a720: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23  3_open_v2() */.#
a730: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a740: 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
a750: 20 20 20 20 30 78 30 30 30 30 30 30 30 38 20 20      0x00000008  
a760: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23  /* VFS only */.#
a770: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a780: 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20  EN_EXCLUSIVE    
a790: 20 20 20 20 30 78 30 30 30 30 30 30 31 30 20 20      0x00000010  
a7a0: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23  /* VFS only */.#
a7b0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a7c0: 45 4e 5f 41 55 54 4f 50 52 4f 58 59 20 20 20 20  EN_AUTOPROXY    
a7d0: 20 20 20 20 30 78 30 30 30 30 30 30 32 30 20 20      0x00000020  
a7e0: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23  /* VFS only */.#
a7f0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a800: 45 4e 5f 55 52 49 20 20 20 20 20 20 20 20 20 20  EN_URI          
a810: 20 20 20 20 30 78 30 30 30 30 30 30 34 30 20 20      0x00000040  
a820: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65  /* Ok for sqlite
a830: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23  3_open_v2() */.#
a840: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a850: 45 4e 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 20  EN_MEMORY       
a860: 20 20 20 20 30 78 30 30 30 30 30 30 38 30 20 20      0x00000080  
a870: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65  /* Ok for sqlite
a880: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23  3_open_v2() */.#
a890: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a8a0: 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20 20 20  EN_MAIN_DB      
a8b0: 20 20 20 20 30 78 30 30 30 30 30 31 30 30 20 20      0x00000100  
a8c0: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23  /* VFS only */.#
a8d0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a8e0: 45 4e 5f 54 45 4d 50 5f 44 42 20 20 20 20 20 20  EN_TEMP_DB      
a8f0: 20 20 20 20 30 78 30 30 30 30 30 32 30 30 20 20      0x00000200  
a900: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23  /* VFS only */.#
a910: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a920: 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 20  EN_TRANSIENT_DB 
a930: 20 20 20 20 30 78 30 30 30 30 30 34 30 30 20 20      0x00000400  
a940: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23  /* VFS only */.#
a950: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a960: 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20  EN_MAIN_JOURNAL 
a970: 20 20 20 20 30 78 30 30 30 30 30 38 30 30 20 20      0x00000800  
a980: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23  /* VFS only */.#
a990: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a9a0: 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20  EN_TEMP_JOURNAL 
a9b0: 20 20 20 20 30 78 30 30 30 30 31 30 30 30 20 20      0x00001000  
a9c0: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23  /* VFS only */.#
a9d0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
a9e0: 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20 20  EN_SUBJOURNAL   
a9f0: 20 20 20 20 30 78 30 30 30 30 32 30 30 30 20 20      0x00002000  
aa00: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23  /* VFS only */.#
aa10: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
aa20: 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41  EN_MASTER_JOURNA
aa30: 4c 20 20 20 30 78 30 30 30 30 34 30 30 30 20 20  L   0x00004000  
aa40: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23  /* VFS only */.#
aa50: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
aa60: 45 4e 5f 4e 4f 4d 55 54 45 58 20 20 20 20 20 20  EN_NOMUTEX      
aa70: 20 20 20 20 30 78 30 30 30 30 38 30 30 30 20 20      0x00008000  
aa80: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65  /* Ok for sqlite
aa90: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23  3_open_v2() */.#
aaa0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
aab0: 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 20 20 20  EN_FULLMUTEX    
aac0: 20 20 20 20 30 78 30 30 30 31 30 30 30 30 20 20      0x00010000  
aad0: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65  /* Ok for sqlite
aae0: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23  3_open_v2() */.#
aaf0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
ab00: 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45 20 20  EN_SHAREDCACHE  
ab10: 20 20 20 20 30 78 30 30 30 32 30 30 30 30 20 20      0x00020000  
ab20: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65  /* Ok for sqlite
ab30: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23  3_open_v2() */.#
ab40: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
ab50: 45 4e 5f 50 52 49 56 41 54 45 43 41 43 48 45 20  EN_PRIVATECACHE 
ab60: 20 20 20 20 30 78 30 30 30 34 30 30 30 30 20 20      0x00040000  
ab70: 2f 2a 20 4f 6b 20 66 6f 72 20 73 71 6c 69 74 65  /* Ok for sqlite
ab80: 33 5f 6f 70 65 6e 5f 76 32 28 29 20 2a 2f 0a 23  3_open_v2() */.#
ab90: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 50  define SQLITE_OP
aba0: 45 4e 5f 57 41 4c 20 20 20 20 20 20 20 20 20 20  EN_WAL          
abb0: 20 20 20 20 30 78 30 30 30 38 30 30 30 30 20 20      0x00080000  
abc0: 2f 2a 20 56 46 53 20 6f 6e 6c 79 20 2a 2f 0a 0a  /* VFS only */..
abd0: 2f 2a 20 52 65 73 65 72 76 65 64 3a 20 20 20 20  /* Reserved:    
abe0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
abf0: 20 20 20 20 20 30 78 30 30 46 30 30 30 30 30 20       0x00F00000 
ac00: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  */../*.** CAPI3R
ac10: 45 46 3a 20 44 65 76 69 63 65 20 43 68 61 72 61  EF: Device Chara
ac20: 63 74 65 72 69 73 74 69 63 73 0a 2a 2a 0a 2a 2a  cteristics.**.**
ac30: 20 54 68 65 20 78 44 65 76 69 63 65 43 68 61 72   The xDeviceChar
ac40: 61 63 74 65 72 69 73 74 69 63 73 20 6d 65 74 68  acteristics meth
ac50: 6f 64 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  od of the [sqlit
ac60: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a  e3_io_methods].*
ac70: 2a 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 73  * object returns
ac80: 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63   an integer whic
ac90: 68 20 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66  h is a vector of
aca0: 20 74 68 65 73 65 0a 2a 2a 20 62 69 74 20 76 61   these.** bit va
acb0: 6c 75 65 73 20 65 78 70 72 65 73 73 69 6e 67 20  lues expressing 
acc0: 49 2f 4f 20 63 68 61 72 61 63 74 65 72 69 73 74  I/O characterist
acd0: 69 63 73 20 6f 66 20 74 68 65 20 6d 61 73 73 20  ics of the mass 
ace0: 73 74 6f 72 61 67 65 0a 2a 2a 20 64 65 76 69 63  storage.** devic
acf0: 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65  e that holds the
ad00: 20 66 69 6c 65 20 74 68 61 74 20 74 68 65 20 5b   file that the [
ad10: 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
ad20: 64 73 5d 0a 2a 2a 20 72 65 66 65 72 73 20 74 6f  ds].** refers to
ad30: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ..**.** The SQLI
ad40: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20  TE_IOCAP_ATOMIC 
ad50: 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74  property means t
ad60: 68 61 74 20 61 6c 6c 20 77 72 69 74 65 73 20 6f  hat all writes o
ad70: 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20 61 72  f.** any size ar
ad80: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53  e atomic.  The S
ad90: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
ada0: 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a 2a 20  ICnnn values.** 
adb0: 6d 65 61 6e 20 74 68 61 74 20 77 72 69 74 65 73  mean that writes
adc0: 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61 74 20   of blocks that 
add0: 61 72 65 20 6e 6e 6e 20 62 79 74 65 73 20 69 6e  are nnn bytes in
ade0: 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 72 65   size and.** are
adf0: 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 61   aligned to an a
ae00: 64 64 72 65 73 73 20 77 68 69 63 68 20 69 73 20  ddress which is 
ae10: 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69  an integer multi
ae20: 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72  ple of.** nnn ar
ae30: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53  e atomic.  The S
ae40: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45  QLITE_IOCAP_SAFE
ae50: 5f 41 50 50 45 4e 44 20 76 61 6c 75 65 20 6d 65  _APPEND value me
ae60: 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68 65 6e  ans.** that when
ae70: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65   data is appende
ae80: 64 20 74 6f 20 61 20 66 69 6c 65 2c 20 74 68 65  d to a file, the
ae90: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65   data is appende
aea0: 64 0a 2a 2a 20 66 69 72 73 74 20 74 68 65 6e 20  d.** first then 
aeb0: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
aec0: 66 69 6c 65 20 69 73 20 65 78 74 65 6e 64 65 64  file is extended
aed0: 2c 20 6e 65 76 65 72 20 74 68 65 20 6f 74 68 65  , never the othe
aee0: 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e  r.** way around.
aef0: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43    The SQLITE_IOC
af00: 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 70 72  AP_SEQUENTIAL pr
af10: 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61  operty means tha
af20: 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  t.** information
af30: 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64   is written to d
af40: 69 73 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20  isk in the same 
af50: 6f 72 64 65 72 20 61 73 20 63 61 6c 6c 73 0a 2a  order as calls.*
af60: 2a 20 74 6f 20 78 57 72 69 74 65 28 29 2e 20 20  * to xWrite().  
af70: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  The SQLITE_IOCAP
af80: 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57  _POWERSAFE_OVERW
af90: 52 49 54 45 20 70 72 6f 70 65 72 74 79 20 6d 65  RITE property me
afa0: 61 6e 73 20 74 68 61 74 0a 2a 2a 20 61 66 74 65  ans that.** afte
afb0: 72 20 72 65 62 6f 6f 74 20 66 6f 6c 6c 6f 77 69  r reboot followi
afc0: 6e 67 20 61 20 63 72 61 73 68 20 6f 72 20 70 6f  ng a crash or po
afd0: 77 65 72 20 6c 6f 73 73 2c 20 74 68 65 20 6f 6e  wer loss, the on
afe0: 6c 79 20 62 79 74 65 73 20 69 6e 20 61 0a 2a 2a  ly bytes in a.**
aff0: 20 66 69 6c 65 20 74 68 61 74 20 77 65 72 65 20   file that were 
b000: 77 72 69 74 74 65 6e 20 61 74 20 74 68 65 20 61  written at the a
b010: 70 70 6c 69 63 61 74 69 6f 6e 20 6c 65 76 65 6c  pplication level
b020: 20 6d 69 67 68 74 20 68 61 76 65 20 63 68 61 6e   might have chan
b030: 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20  ged.** and that 
b040: 61 64 6a 61 63 65 6e 74 20 62 79 74 65 73 2c 20  adjacent bytes, 
b050: 65 76 65 6e 20 62 79 74 65 73 20 77 69 74 68 69  even bytes withi
b060: 6e 20 74 68 65 20 73 61 6d 65 20 73 65 63 74 6f  n the same secto
b070: 72 20 61 72 65 0a 2a 2a 20 67 75 61 72 61 6e 74  r are.** guarant
b080: 65 65 64 20 74 6f 20 62 65 20 75 6e 63 68 61 6e  eed to be unchan
b090: 67 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ged..*/.#define 
b0a0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
b0b0: 4d 49 43 20 20 20 20 20 20 20 20 20 20 20 20 20  MIC             
b0c0: 20 20 20 20 30 78 30 30 30 30 30 30 30 31 0a 23      0x00000001.#
b0d0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f  define SQLITE_IO
b0e0: 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 20 20 20  CAP_ATOMIC512   
b0f0: 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30             0x000
b100: 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51  00002.#define SQ
b110: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
b120: 43 31 4b 20 20 20 20 20 20 20 20 20 20 20 20 20  C1K             
b130: 20 20 30 78 30 30 30 30 30 30 30 34 0a 23 64 65    0x00000004.#de
b140: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
b150: 50 5f 41 54 4f 4d 49 43 32 4b 20 20 20 20 20 20  P_ATOMIC2K      
b160: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
b170: 30 30 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  008.#define SQLI
b180: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34  TE_IOCAP_ATOMIC4
b190: 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  K               
b1a0: 30 78 30 30 30 30 30 30 31 30 0a 23 64 65 66 69  0x00000010.#defi
b1b0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  ne SQLITE_IOCAP_
b1c0: 41 54 4f 4d 49 43 38 4b 20 20 20 20 20 20 20 20  ATOMIC8K        
b1d0: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 32         0x0000002
b1e0: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
b1f0: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b  _IOCAP_ATOMIC16K
b200: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78                0x
b210: 30 30 30 30 30 30 34 30 0a 23 64 65 66 69 6e 65  00000040.#define
b220: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
b230: 4f 4d 49 43 33 32 4b 20 20 20 20 20 20 20 20 20  OMIC32K         
b240: 20 20 20 20 20 30 78 30 30 30 30 30 30 38 30 0a       0x00000080.
b250: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
b260: 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 20 20  OCAP_ATOMIC64K  
b270: 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30              0x00
b280: 30 30 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53  000100.#define S
b290: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45  QLITE_IOCAP_SAFE
b2a0: 5f 41 50 50 45 4e 44 20 20 20 20 20 20 20 20 20  _APPEND         
b2b0: 20 20 20 30 78 30 30 30 30 30 32 30 30 0a 23 64     0x00000200.#d
b2c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43  efine SQLITE_IOC
b2d0: 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 20 20  AP_SEQUENTIAL   
b2e0: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30            0x0000
b2f0: 30 34 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  0400.#define SQL
b300: 49 54 45 5f 49 4f 43 41 50 5f 55 4e 44 45 4c 45  ITE_IOCAP_UNDELE
b310: 54 41 42 4c 45 5f 57 48 45 4e 5f 4f 50 45 4e 20  TABLE_WHEN_OPEN 
b320: 20 30 78 30 30 30 30 30 38 30 30 0a 23 64 65 66   0x00000800.#def
b330: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  ine SQLITE_IOCAP
b340: 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57  _POWERSAFE_OVERW
b350: 52 49 54 45 20 20 20 20 30 78 30 30 30 30 31 30  RITE    0x000010
b360: 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  00../*.** CAPI3R
b370: 45 46 3a 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67  EF: File Locking
b380: 20 4c 65 76 65 6c 73 0a 2a 2a 0a 2a 2a 20 53 51   Levels.**.** SQ
b390: 4c 69 74 65 20 75 73 65 73 20 6f 6e 65 20 6f 66  Lite uses one of
b3a0: 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 76   these integer v
b3b0: 61 6c 75 65 73 20 61 73 20 74 68 65 20 73 65 63  alues as the sec
b3c0: 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  ond.** argument 
b3d0: 74 6f 20 63 61 6c 6c 73 20 69 74 20 6d 61 6b 65  to calls it make
b3e0: 73 20 74 6f 20 74 68 65 20 78 4c 6f 63 6b 28 29  s to the xLock()
b3f0: 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 6d   and xUnlock() m
b400: 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 61 6e 20  ethods.** of an 
b410: 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68  [sqlite3_io_meth
b420: 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  ods] object..*/.
b430: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
b440: 4f 43 4b 5f 4e 4f 4e 45 20 20 20 20 20 20 20 20  OCK_NONE        
b450: 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    0.#define SQLI
b460: 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 20 20  TE_LOCK_SHARED  
b470: 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
b480: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45  SQLITE_LOCK_RESE
b490: 52 56 45 44 20 20 20 20 20 20 32 0a 23 64 65 66  RVED      2.#def
b4a0: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  ine SQLITE_LOCK_
b4b0: 50 45 4e 44 49 4e 47 20 20 20 20 20 20 20 33 0a  PENDING       3.
b4c0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
b4d0: 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20  OCK_EXCLUSIVE   
b4e0: 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33    4../*.** CAPI3
b4f0: 52 45 46 3a 20 53 79 6e 63 68 72 6f 6e 69 7a 61  REF: Synchroniza
b500: 74 69 6f 6e 20 54 79 70 65 20 46 6c 61 67 73 0a  tion Type Flags.
b510: 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69 74  **.** When SQLit
b520: 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 53  e invokes the xS
b530: 79 6e 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20  ync() method of 
b540: 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69  an.** [sqlite3_i
b550: 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63  o_methods] objec
b560: 74 20 69 74 20 75 73 65 73 20 61 20 63 6f 6d 62  t it uses a comb
b570: 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68  ination of.** th
b580: 65 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75  ese integer valu
b590: 65 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  es as the second
b5a0: 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
b5b0: 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45   When the SQLITE
b5c0: 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 66  _SYNC_DATAONLY f
b5d0: 6c 61 67 20 69 73 20 75 73 65 64 2c 20 69 74 20  lag is used, it 
b5e0: 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a  means that the.*
b5f0: 2a 20 73 79 6e 63 20 6f 70 65 72 61 74 69 6f 6e  * sync operation
b600: 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 66   only needs to f
b610: 6c 75 73 68 20 64 61 74 61 20 74 6f 20 6d 61 73  lush data to mas
b620: 73 20 73 74 6f 72 61 67 65 2e 20 20 49 6e 6f 64  s storage.  Inod
b630: 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  e.** information
b640: 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 66 6c 75   need not be flu
b650: 73 68 65 64 2e 20 49 66 20 74 68 65 20 6c 6f 77  shed. If the low
b660: 65 72 20 66 6f 75 72 20 62 69 74 73 20 6f 66 20  er four bits of 
b670: 74 68 65 20 66 6c 61 67 0a 2a 2a 20 65 71 75 61  the flag.** equa
b680: 6c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f  l SQLITE_SYNC_NO
b690: 52 4d 41 4c 2c 20 74 68 61 74 20 6d 65 61 6e 73  RMAL, that means
b6a0: 20 74 6f 20 75 73 65 20 6e 6f 72 6d 61 6c 20 66   to use normal f
b6b0: 73 79 6e 63 28 29 20 73 65 6d 61 6e 74 69 63 73  sync() semantics
b6c0: 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 77 65  ..** If the lowe
b6d0: 72 20 66 6f 75 72 20 62 69 74 73 20 65 71 75 61  r four bits equa
b6e0: 6c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55  l SQLITE_SYNC_FU
b6f0: 4c 4c 2c 20 74 68 61 74 20 6d 65 61 6e 73 0a 2a  LL, that means.*
b700: 2a 20 74 6f 20 75 73 65 20 4d 61 63 20 4f 53 20  * to use Mac OS 
b710: 58 20 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63  X style fullsync
b720: 20 69 6e 73 74 65 61 64 20 6f 66 20 66 73 79 6e   instead of fsyn
b730: 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 44 6f 20 6e 6f  c()..**.** Do no
b740: 74 20 63 6f 6e 66 75 73 65 20 74 68 65 20 53 51  t confuse the SQ
b750: 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
b760: 20 61 6e 64 20 53 51 4c 49 54 45 5f 53 59 4e 43   and SQLITE_SYNC
b770: 5f 46 55 4c 4c 20 66 6c 61 67 73 0a 2a 2a 20 77  _FULL flags.** w
b780: 69 74 68 20 74 68 65 20 5b 50 52 41 47 4d 41 20  ith the [PRAGMA 
b790: 73 79 6e 63 68 72 6f 6e 6f 75 73 5d 3d 4e 4f 52  synchronous]=NOR
b7a0: 4d 41 4c 20 61 6e 64 20 5b 50 52 41 47 4d 41 20  MAL and [PRAGMA 
b7b0: 73 79 6e 63 68 72 6f 6e 6f 75 73 5d 3d 46 55 4c  synchronous]=FUL
b7c0: 4c 0a 2a 2a 20 73 65 74 74 69 6e 67 73 2e 20 20  L.** settings.  
b7d0: 54 68 65 20 5b 73 79 6e 63 68 72 6f 6e 6f 75 73  The [synchronous
b7e0: 20 70 72 61 67 6d 61 5d 20 64 65 74 65 72 6d 69   pragma] determi
b7f0: 6e 65 73 20 77 68 65 6e 20 63 61 6c 6c 73 20 74  nes when calls t
b800: 6f 20 74 68 65 0a 2a 2a 20 78 53 79 6e 63 20 56  o the.** xSync V
b810: 46 53 20 6d 65 74 68 6f 64 20 6f 63 63 75 72 20  FS method occur 
b820: 61 6e 64 20 61 70 70 6c 69 65 73 20 75 6e 69 66  and applies unif
b830: 6f 72 6d 6c 79 20 61 63 72 6f 73 73 20 61 6c 6c  ormly across all
b840: 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 2a 2a 20 54   platforms..** T
b850: 68 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e  he SQLITE_SYNC_N
b860: 4f 52 4d 41 4c 20 61 6e 64 20 53 51 4c 49 54 45  ORMAL and SQLITE
b870: 5f 53 59 4e 43 5f 46 55 4c 4c 20 66 6c 61 67 73  _SYNC_FULL flags
b880: 20 64 65 74 65 72 6d 69 6e 65 20 68 6f 77 0a 2a   determine how.*
b890: 2a 20 65 6e 65 72 67 65 74 69 63 20 6f 72 20 72  * energetic or r
b8a0: 69 67 6f 72 6f 75 73 20 6f 72 20 66 6f 72 63 65  igorous or force
b8b0: 66 75 6c 20 74 68 65 20 73 79 6e 63 20 6f 70 65  ful the sync ope
b8c0: 72 61 74 69 6f 6e 73 20 61 72 65 20 61 6e 64 0a  rations are and.
b8d0: 2a 2a 20 6f 6e 6c 79 20 6d 61 6b 65 20 61 20 64  ** only make a d
b8e0: 69 66 66 65 72 65 6e 63 65 20 6f 6e 20 4d 61 63  ifference on Mac
b8f0: 20 4f 53 58 20 66 6f 72 20 74 68 65 20 64 65 66   OSX for the def
b900: 61 75 6c 74 20 53 51 4c 69 74 65 20 63 6f 64 65  ault SQLite code
b910: 2e 0a 2a 2a 20 28 54 68 69 72 64 2d 70 61 72 74  ..** (Third-part
b920: 79 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61  y VFS implementa
b930: 74 69 6f 6e 73 20 6d 69 67 68 74 20 61 6c 73 6f  tions might also
b940: 20 6d 61 6b 65 20 74 68 65 20 64 69 73 74 69 6e   make the distin
b950: 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e  ction.** between
b960: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52   SQLITE_SYNC_NOR
b970: 4d 41 4c 20 61 6e 64 20 53 51 4c 49 54 45 5f 53  MAL and SQLITE_S
b980: 59 4e 43 5f 46 55 4c 4c 2c 20 62 75 74 20 61 6d  YNC_FULL, but am
b990: 6f 6e 67 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61  ong the.** opera
b9a0: 74 69 6e 67 20 73 79 73 74 65 6d 73 20 6e 61 74  ting systems nat
b9b0: 69 76 65 6c 79 20 73 75 70 70 6f 72 74 65 64 20  ively supported 
b9c0: 62 79 20 53 51 4c 69 74 65 2c 20 6f 6e 6c 79 20  by SQLite, only 
b9d0: 4d 61 63 20 4f 53 58 0a 2a 2a 20 63 61 72 65 73  Mac OSX.** cares
b9e0: 20 61 62 6f 75 74 20 74 68 65 20 64 69 66 66 65   about the diffe
b9f0: 72 65 6e 63 65 2e 29 0a 2a 2f 0a 23 64 65 66 69  rence.).*/.#defi
ba00: 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e  ne SQLITE_SYNC_N
ba10: 4f 52 4d 41 4c 20 20 20 20 20 20 20 20 30 78 30  ORMAL        0x0
ba20: 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 4c  0002.#define SQL
ba30: 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20 20 20  ITE_SYNC_FULL   
ba40: 20 20 20 20 20 20 20 30 78 30 30 30 30 33 0a 23         0x00003.#
ba50: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59  define SQLITE_SY
ba60: 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 20 20 20 20  NC_DATAONLY     
ba70: 20 30 78 30 30 30 31 30 0a 0a 2f 2a 0a 2a 2a 20   0x00010../*.** 
ba80: 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e 74  CAPI3REF: OS Int
ba90: 65 72 66 61 63 65 20 4f 70 65 6e 20 46 69 6c 65  erface Open File
baa0: 20 48 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 41 6e   Handle.**.** An
bab0: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20   [sqlite3_file] 
bac0: 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74  object represent
bad0: 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 69  s an open file i
bae0: 6e 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c 69 74  n the .** [sqlit
baf0: 65 33 5f 76 66 73 20 7c 20 4f 53 20 69 6e 74 65  e3_vfs | OS inte
bb00: 72 66 61 63 65 20 6c 61 79 65 72 5d 2e 20 20 49  rface layer].  I
bb10: 6e 64 69 76 69 64 75 61 6c 20 4f 53 20 69 6e 74  ndividual OS int
bb20: 65 72 66 61 63 65 0a 2a 2a 20 69 6d 70 6c 65 6d  erface.** implem
bb30: 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 0a 2a  entations will.*
bb40: 2a 20 77 61 6e 74 20 74 6f 20 73 75 62 63 6c 61  * want to subcla
bb50: 73 73 20 74 68 69 73 20 6f 62 6a 65 63 74 20 62  ss this object b
bb60: 79 20 61 70 70 65 6e 64 69 6e 67 20 61 64 64 69  y appending addi
bb70: 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 0a 2a 2a  tional fields.**
bb80: 20 66 6f 72 20 74 68 65 69 72 20 6f 77 6e 20 75   for their own u
bb90: 73 65 2e 20 20 54 68 65 20 70 4d 65 74 68 6f 64  se.  The pMethod
bba0: 73 20 65 6e 74 72 79 20 69 73 20 61 20 70 6f 69  s entry is a poi
bbb0: 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73  nter to an.** [s
bbc0: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
bbd0: 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 64  s] object that d
bbe0: 65 66 69 6e 65 73 20 6d 65 74 68 6f 64 73 20 66  efines methods f
bbf0: 6f 72 20 70 65 72 66 6f 72 6d 69 6e 67 0a 2a 2a  or performing.**
bc00: 20 49 2f 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20   I/O operations 
bc10: 6f 6e 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65  on the open file
bc20: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
bc30: 75 63 74 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  uct sqlite3_file
bc40: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 3b 0a 73   sqlite3_file;.s
bc50: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 66 69  truct sqlite3_fi
bc60: 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 74 72  le {.  const str
bc70: 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  uct sqlite3_io_m
bc80: 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73  ethods *pMethods
bc90: 3b 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f  ;  /* Methods fo
bca0: 72 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 2a  r an open file *
bcb0: 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  /.};../*.** CAPI
bcc0: 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61  3REF: OS Interfa
bcd0: 63 65 20 46 69 6c 65 20 56 69 72 74 75 61 6c 20  ce File Virtual 
bce0: 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 0a 2a  Methods Object.*
bcf0: 2a 0a 2a 2a 20 45 76 65 72 79 20 66 69 6c 65 20  *.** Every file 
bd00: 6f 70 65 6e 65 64 20 62 79 20 74 68 65 20 5b 73  opened by the [s
bd10: 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e  qlite3_vfs.xOpen
bd20: 5d 20 6d 65 74 68 6f 64 20 70 6f 70 75 6c 61 74  ] method populat
bd30: 65 73 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65  es an.** [sqlite
bd40: 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 20 28  3_file] object (
bd50: 6f 72 2c 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 6c  or, more commonl
bd60: 79 2c 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66  y, a subclass of
bd70: 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
bd80: 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 29 20 77  _file] object) w
bd90: 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ith a pointer to
bda0: 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
bdb0: 74 68 69 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20  this object..** 
bdc0: 54 68 69 73 20 6f 62 6a 65 63 74 20 64 65 66 69  This object defi
bdd0: 6e 65 73 20 74 68 65 20 6d 65 74 68 6f 64 73 20  nes the methods 
bde0: 75 73 65 64 20 74 6f 20 70 65 72 66 6f 72 6d 20  used to perform 
bdf0: 76 61 72 69 6f 75 73 20 6f 70 65 72 61 74 69 6f  various operatio
be00: 6e 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74 68  ns.** against th
be10: 65 20 6f 70 65 6e 20 66 69 6c 65 20 72 65 70 72  e open file repr
be20: 65 73 65 6e 74 65 64 20 62 79 20 74 68 65 20 5b  esented by the [
be30: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62  sqlite3_file] ob
be40: 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ject..**.** If t
be50: 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 2e  he [sqlite3_vfs.
be60: 78 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 73 65  xOpen] method se
be70: 74 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66  ts the sqlite3_f
be80: 69 6c 65 2e 70 4d 65 74 68 6f 64 73 20 65 6c 65  ile.pMethods ele
be90: 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20 61 20 6e 6f  ment .** to a no
bea0: 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  n-NULL pointer, 
beb0: 74 68 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33  then the sqlite3
bec0: 5f 69 6f 5f 6d 65 74 68 6f 64 73 2e 78 43 6c 6f  _io_methods.xClo
bed0: 73 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 6d 61 79  se method.** may
bee0: 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 76 65 6e   be invoked even
bef0: 20 69 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   if the [sqlite3
bf00: 5f 76 66 73 2e 78 4f 70 65 6e 5d 20 72 65 70 6f  _vfs.xOpen] repo
bf10: 72 74 65 64 20 74 68 61 74 20 69 74 20 66 61 69  rted that it fai
bf20: 6c 65 64 2e 20 20 54 68 65 0a 2a 2a 20 6f 6e 6c  led.  The.** onl
bf30: 79 20 77 61 79 20 74 6f 20 70 72 65 76 65 6e 74  y way to prevent
bf40: 20 61 20 63 61 6c 6c 20 74 6f 20 78 43 6c 6f 73   a call to xClos
bf50: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 66 61  e following a fa
bf60: 69 6c 65 64 20 5b 73 71 6c 69 74 65 33 5f 76 66  iled [sqlite3_vf
bf70: 73 2e 78 4f 70 65 6e 5d 0a 2a 2a 20 69 73 20 66  s.xOpen].** is f
bf80: 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  or the [sqlite3_
bf90: 76 66 73 2e 78 4f 70 65 6e 5d 20 74 6f 20 73 65  vfs.xOpen] to se
bfa0: 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69  t the sqlite3_fi
bfb0: 6c 65 2e 70 4d 65 74 68 6f 64 73 20 65 6c 65 6d  le.pMethods elem
bfc0: 65 6e 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a  ent.** to NULL..
bfd0: 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20  **.** The flags 
bfe0: 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 79 6e  argument to xSyn
bff0: 63 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20  c may be one of 
c000: 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52  [SQLITE_SYNC_NOR
c010: 4d 41 4c 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49  MAL] or.** [SQLI
c020: 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20 20  TE_SYNC_FULL].  
c030: 54 68 65 20 66 69 72 73 74 20 63 68 6f 69 63 65  The first choice
c040: 20 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 66   is the normal f
c050: 73 79 6e 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73  sync()..** The s
c060: 65 63 6f 6e 64 20 63 68 6f 69 63 65 20 69 73 20  econd choice is 
c070: 61 20 4d 61 63 20 4f 53 20 58 20 73 74 79 6c 65  a Mac OS X style
c080: 20 66 75 6c 6c 73 79 6e 63 2e 20 20 54 68 65 20   fullsync.  The 
c090: 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54  [SQLITE_SYNC_DAT
c0a0: 41 4f 4e 4c 59 5d 0a 2a 2a 20 66 6c 61 67 20 6d  AONLY].** flag m
c0b0: 61 79 20 62 65 20 4f 52 65 64 20 69 6e 20 74 6f  ay be ORed in to
c0c0: 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 6f   indicate that o
c0d0: 6e 6c 79 20 74 68 65 20 64 61 74 61 20 6f 66 20  nly the data of 
c0e0: 74 68 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20  the file.** and 
c0f0: 6e 6f 74 20 69 74 73 20 69 6e 6f 64 65 20 6e 65  not its inode ne
c100: 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 65 64  eds to be synced
c110: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65  ..**.** The inte
c120: 67 65 72 20 76 61 6c 75 65 73 20 74 6f 20 78 4c  ger values to xL
c130: 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63  ock() and xUnloc
c140: 6b 28 29 20 61 72 65 20 6f 6e 65 20 6f 66 0a 2a  k() are one of.*
c150: 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b  * <ul>.** <li> [
c160: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45  SQLITE_LOCK_NONE
c170: 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  ],.** <li> [SQLI
c180: 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c  TE_LOCK_SHARED],
c190: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
c1a0: 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c  _LOCK_RESERVED],
c1b0: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
c1c0: 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20  _LOCK_PENDING], 
c1d0: 6f 72 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  or.** <li> [SQLI
c1e0: 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56  TE_LOCK_EXCLUSIV
c1f0: 45 5d 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20  E]..** </ul>.** 
c200: 78 4c 6f 63 6b 28 29 20 69 6e 63 72 65 61 73 65  xLock() increase
c210: 73 20 74 68 65 20 6c 6f 63 6b 2e 20 78 55 6e 6c  s the lock. xUnl
c220: 6f 63 6b 28 29 20 64 65 63 72 65 61 73 65 73 20  ock() decreases 
c230: 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 65  the lock..** The
c240: 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
c250: 6f 63 6b 28 29 20 6d 65 74 68 6f 64 20 63 68 65  ock() method che
c260: 63 6b 73 20 77 68 65 74 68 65 72 20 61 6e 79 20  cks whether any 
c270: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
c280: 69 6f 6e 2c 0a 2a 2a 20 65 69 74 68 65 72 20 69  ion,.** either i
c290: 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 6f  n this process o
c2a0: 72 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20  r in some other 
c2b0: 70 72 6f 63 65 73 73 2c 20 69 73 20 68 6f 6c 64  process, is hold
c2c0: 69 6e 67 20 61 20 52 45 53 45 52 56 45 44 2c 0a  ing a RESERVED,.
c2d0: 2a 2a 20 50 45 4e 44 49 4e 47 2c 20 6f 72 20 45  ** PENDING, or E
c2e0: 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e  XCLUSIVE lock on
c2f0: 20 74 68 65 20 66 69 6c 65 2e 20 20 49 74 20 72   the file.  It r
c300: 65 74 75 72 6e 73 20 74 72 75 65 0a 2a 2a 20 69  eturns true.** i
c310: 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 65 78  f such a lock ex
c320: 69 73 74 73 20 61 6e 64 20 66 61 6c 73 65 20 6f  ists and false o
c330: 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20  therwise..**.** 
c340: 54 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c  The xFileControl
c350: 28 29 20 6d 65 74 68 6f 64 20 69 73 20 61 20 67  () method is a g
c360: 65 6e 65 72 69 63 20 69 6e 74 65 72 66 61 63 65  eneric interface
c370: 20 74 68 61 74 20 61 6c 6c 6f 77 73 20 63 75 73   that allows cus
c380: 74 6f 6d 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65  tom.** VFS imple
c390: 6d 65 6e 74 61 74 69 6f 6e 73 20 74 6f 20 64 69  mentations to di
c3a0: 72 65 63 74 6c 79 20 63 6f 6e 74 72 6f 6c 20 61  rectly control a
c3b0: 6e 20 6f 70 65 6e 20 66 69 6c 65 20 75 73 69 6e  n open file usin
c3c0: 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65  g the.** [sqlite
c3d0: 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29  3_file_control()
c3e0: 5d 20 69 6e 74 65 72 66 61 63 65 2e 20 20 54 68  ] interface.  Th
c3f0: 65 20 73 65 63 6f 6e 64 20 22 6f 70 22 20 61 72  e second "op" ar
c400: 67 75 6d 65 6e 74 20 69 73 20 61 6e 0a 2a 2a 20  gument is an.** 
c410: 69 6e 74 65 67 65 72 20 6f 70 63 6f 64 65 2e 20  integer opcode. 
c420: 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d   The third argum
c430: 65 6e 74 20 69 73 20 61 20 67 65 6e 65 72 69 63  ent is a generic
c440: 20 70 6f 69 6e 74 65 72 20 69 6e 74 65 6e 64 65   pointer intende
c450: 64 20 74 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f  d to.** point to
c460: 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68 61   a structure tha
c470: 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 72  t may contain ar
c480: 67 75 6d 65 6e 74 73 20 6f 72 20 73 70 61 63 65  guments or space
c490: 20 69 6e 20 77 68 69 63 68 20 74 6f 0a 2a 2a 20   in which to.** 
c4a0: 77 72 69 74 65 20 72 65 74 75 72 6e 20 76 61 6c  write return val
c4b0: 75 65 73 2e 20 20 50 6f 74 65 6e 74 69 61 6c 20  ues.  Potential 
c4c0: 75 73 65 73 20 66 6f 72 20 78 46 69 6c 65 43 6f  uses for xFileCo
c4d0: 6e 74 72 6f 6c 28 29 20 6d 69 67 68 74 20 62 65  ntrol() might be
c4e0: 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f  .** functions to
c4f0: 20 65 6e 61 62 6c 65 20 62 6c 6f 63 6b 69 6e 67   enable blocking
c500: 20 6c 6f 63 6b 73 20 77 69 74 68 20 74 69 6d 65   locks with time
c510: 6f 75 74 73 2c 20 74 6f 20 63 68 61 6e 67 65 20  outs, to change 
c520: 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73  the.** locking s
c530: 74 72 61 74 65 67 79 20 28 66 6f 72 20 65 78 61  trategy (for exa
c540: 6d 70 6c 65 20 74 6f 20 75 73 65 20 64 6f 74 2d  mple to use dot-
c550: 66 69 6c 65 20 6c 6f 63 6b 73 29 2c 20 74 6f 20  file locks), to 
c560: 69 6e 71 75 69 72 65 0a 2a 2a 20 61 62 6f 75 74  inquire.** about
c570: 20 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 61   the status of a
c580: 20 6c 6f 63 6b 2c 20 6f 72 20 74 6f 20 62 72 65   lock, or to bre
c590: 61 6b 20 73 74 61 6c 65 20 6c 6f 63 6b 73 2e 20  ak stale locks. 
c5a0: 20 54 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20 63   The SQLite.** c
c5b0: 6f 72 65 20 72 65 73 65 72 76 65 73 20 61 6c 6c  ore reserves all
c5c0: 20 6f 70 63 6f 64 65 73 20 6c 65 73 73 20 74 68   opcodes less th
c5d0: 61 6e 20 31 30 30 20 66 6f 72 20 69 74 73 20 6f  an 100 for its o
c5e0: 77 6e 20 75 73 65 2e 0a 2a 2a 20 41 20 5b 53 51  wn use..** A [SQ
c5f0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53  LITE_FCNTL_LOCKS
c600: 54 41 54 45 20 7c 20 6c 69 73 74 20 6f 66 20 6f  TATE | list of o
c610: 70 63 6f 64 65 73 5d 20 6c 65 73 73 20 74 68 61  pcodes] less tha
c620: 6e 20 31 30 30 20 69 73 20 61 76 61 69 6c 61 62  n 100 is availab
c630: 6c 65 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69  le..** Applicati
c640: 6f 6e 73 20 74 68 61 74 20 64 65 66 69 6e 65 20  ons that define 
c650: 61 20 63 75 73 74 6f 6d 20 78 46 69 6c 65 43 6f  a custom xFileCo
c660: 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 73 68 6f  ntrol method sho
c670: 75 6c 64 20 75 73 65 20 6f 70 63 6f 64 65 73 0a  uld use opcodes.
c680: 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  ** greater than 
c690: 31 30 30 20 74 6f 20 61 76 6f 69 64 20 63 6f 6e  100 to avoid con
c6a0: 66 6c 69 63 74 73 2e 20 20 56 46 53 20 69 6d 70  flicts.  VFS imp
c6b0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f  lementations sho
c6c0: 75 6c 64 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53  uld.** return [S
c6d0: 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 5d 20  QLITE_NOTFOUND] 
c6e0: 66 6f 72 20 66 69 6c 65 20 63 6f 6e 74 72 6f 6c  for file control
c6f0: 20 6f 70 63 6f 64 65 73 20 74 68 61 74 20 74 68   opcodes that th
c700: 65 79 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72 65 63  ey do not.** rec
c710: 6f 67 6e 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ognize..**.** Th
c720: 65 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 20  e xSectorSize() 
c730: 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 74  method returns t
c740: 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f  he sector size o
c750: 66 20 74 68 65 0a 2a 2a 20 64 65 76 69 63 65 20  f the.** device 
c760: 74 68 61 74 20 75 6e 64 65 72 6c 69 65 73 20 74  that underlies t
c770: 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 73 65  he file.  The se
c780: 63 74 6f 72 20 73 69 7a 65 20 69 73 20 74 68 65  ctor size is the
c790: 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 77 72 69 74  .** minimum writ
c7a0: 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 65  e that can be pe
c7b0: 72 66 6f 72 6d 65 64 20 77 69 74 68 6f 75 74 20  rformed without 
c7c0: 64 69 73 74 75 72 62 69 6e 67 0a 2a 2a 20 6f 74  disturbing.** ot
c7d0: 68 65 72 20 62 79 74 65 73 20 69 6e 20 74 68 65  her bytes in the
c7e0: 20 66 69 6c 65 2e 20 20 54 68 65 20 78 44 65 76   file.  The xDev
c7f0: 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
c800: 63 73 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72  cs().** method r
c810: 65 74 75 72 6e 73 20 61 20 62 69 74 20 76 65 63  eturns a bit vec
c820: 74 6f 72 20 64 65 73 63 72 69 62 69 6e 67 20 62  tor describing b
c830: 65 68 61 76 69 6f 72 73 20 6f 66 20 74 68 65 0a  ehaviors of the.
c840: 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 65  ** underlying de
c850: 76 69 63 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e  vice:.**.** <ul>
c860: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
c870: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 5d 0a 2a  _IOCAP_ATOMIC].*
c880: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49  * <li> [SQLITE_I
c890: 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 5d 0a  OCAP_ATOMIC512].
c8a0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
c8b0: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 4b 5d 0a  IOCAP_ATOMIC1K].
c8c0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
c8d0: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 32 4b 5d 0a  IOCAP_ATOMIC2K].
c8e0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
c8f0: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34 4b 5d 0a  IOCAP_ATOMIC4K].
c900: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
c910: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 38 4b 5d 0a  IOCAP_ATOMIC8K].
c920: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
c930: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b 5d  IOCAP_ATOMIC16K]
c940: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
c950: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 33 32 4b  _IOCAP_ATOMIC32K
c960: 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ].** <li> [SQLIT
c970: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34  E_IOCAP_ATOMIC64
c980: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  K].** <li> [SQLI
c990: 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50  TE_IOCAP_SAFE_AP
c9a0: 50 45 4e 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  PEND].** <li> [S
c9b0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55  QLITE_IOCAP_SEQU
c9c0: 45 4e 54 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e  ENTIAL].** </ul>
c9d0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54  .**.** The SQLIT
c9e0: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20 70  E_IOCAP_ATOMIC p
c9f0: 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68  roperty means th
ca00: 61 74 20 61 6c 6c 20 77 72 69 74 65 73 20 6f 66  at all writes of
ca10: 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20 61 72 65  .** any size are
ca20: 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 51   atomic.  The SQ
ca30: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
ca40: 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6d  Cnnn values.** m
ca50: 65 61 6e 20 74 68 61 74 20 77 72 69 74 65 73 20  ean that writes 
ca60: 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61 74 20 61  of blocks that a
ca70: 72 65 20 6e 6e 6e 20 62 79 74 65 73 20 69 6e 20  re nnn bytes in 
ca80: 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 72 65 20  size and.** are 
ca90: 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 61 64  aligned to an ad
caa0: 64 72 65 73 73 20 77 68 69 63 68 20 69 73 20 61  dress which is a
cab0: 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 70  n integer multip
cac0: 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65  le of.** nnn are
cad0: 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 51   atomic.  The SQ
cae0: 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f  LITE_IOCAP_SAFE_
caf0: 41 50 50 45 4e 44 20 76 61 6c 75 65 20 6d 65 61  APPEND value mea
cb00: 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68 65 6e 20  ns.** that when 
cb10: 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 64  data is appended
cb20: 20 74 6f 20 61 20 66 69 6c 65 2c 20 74 68 65 20   to a file, the 
cb30: 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 64  data is appended
cb40: 0a 2a 2a 20 66 69 72 73 74 20 74 68 65 6e 20 74  .** first then t
cb50: 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66  he size of the f
cb60: 69 6c 65 20 69 73 20 65 78 74 65 6e 64 65 64 2c  ile is extended,
cb70: 20 6e 65 76 65 72 20 74 68 65 20 6f 74 68 65 72   never the other
cb80: 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e 20  .** way around. 
cb90: 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41   The SQLITE_IOCA
cba0: 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 70 72 6f  P_SEQUENTIAL pro
cbb0: 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74  perty means that
cbc0: 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
cbd0: 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69  is written to di
cbe0: 73 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f  sk in the same o
cbf0: 72 64 65 72 20 61 73 20 63 61 6c 6c 73 0a 2a 2a  rder as calls.**
cc00: 20 74 6f 20 78 57 72 69 74 65 28 29 2e 0a 2a 2a   to xWrite()..**
cc10: 0a 2a 2a 20 49 66 20 78 52 65 61 64 28 29 20 72  .** If xRead() r
cc20: 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 4f  eturns SQLITE_IO
cc30: 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 69  ERR_SHORT_READ i
cc40: 74 20 6d 75 73 74 20 61 6c 73 6f 20 66 69 6c 6c  t must also fill
cc50: 0a 2a 2a 20 69 6e 20 74 68 65 20 75 6e 72 65 61  .** in the unrea
cc60: 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68  d portions of th
cc70: 65 20 62 75 66 66 65 72 20 77 69 74 68 20 7a 65  e buffer with ze
cc80: 72 6f 73 2e 20 20 41 20 56 46 53 20 74 68 61 74  ros.  A VFS that
cc90: 0a 2a 2a 20 66 61 69 6c 73 20 74 6f 20 7a 65 72  .** fails to zer
cca0: 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 61  o-fill short rea
ccb0: 64 73 20 6d 69 67 68 74 20 73 65 65 6d 20 74 6f  ds might seem to
ccc0: 20 77 6f 72 6b 2e 20 20 48 6f 77 65 76 65 72 2c   work.  However,
ccd0: 0a 2a 2a 20 66 61 69 6c 75 72 65 20 74 6f 20 7a  .** failure to z
cce0: 65 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72  ero-fill short r
ccf0: 65 61 64 73 20 77 69 6c 6c 20 65 76 65 6e 74 75  eads will eventu
cd00: 61 6c 6c 79 20 6c 65 61 64 20 74 6f 0a 2a 2a 20  ally lead to.** 
cd10: 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
cd20: 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ion..*/.typedef 
cd30: 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
cd40: 6f 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65  o_methods sqlite
cd50: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 3b 0a 73 74  3_io_methods;.st
cd60: 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  ruct sqlite3_io_
cd70: 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69 6e 74 20  methods {.  int 
cd80: 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20  iVersion;.  int 
cd90: 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74 65  (*xClose)(sqlite
cda0: 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20  3_file*);.  int 
cdb0: 28 2a 78 52 65 61 64 29 28 73 71 6c 69 74 65 33  (*xRead)(sqlite3
cdc0: 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69  _file*, void*, i
cdd0: 6e 74 20 69 41 6d 74 2c 20 73 71 6c 69 74 65 33  nt iAmt, sqlite3
cde0: 5f 69 6e 74 36 34 20 69 4f 66 73 74 29 3b 0a 20  _int64 iOfst);. 
cdf0: 20 69 6e 74 20 28 2a 78 57 72 69 74 65 29 28 73   int (*xWrite)(s
ce00: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f  qlite3_file*, co
ce10: 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69  nst void*, int i
ce20: 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  Amt, sqlite3_int
ce30: 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74  64 iOfst);.  int
ce40: 20 28 2a 78 54 72 75 6e 63 61 74 65 29 28 73 71   (*xTruncate)(sq
ce50: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c  lite3_file*, sql
ce60: 69 74 65 33 5f 69 6e 74 36 34 20 73 69 7a 65 29  ite3_int64 size)
ce70: 3b 0a 20 20 69 6e 74 20 28 2a 78 53 79 6e 63 29  ;.  int (*xSync)
ce80: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
ce90: 69 6e 74 20 66 6c 61 67 73 29 3b 0a 20 20 69 6e  int flags);.  in
cea0: 74 20 28 2a 78 46 69 6c 65 53 69 7a 65 29 28 73  t (*xFileSize)(s
ceb0: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71  qlite3_file*, sq
cec0: 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69  lite3_int64 *pSi
ced0: 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4c 6f  ze);.  int (*xLo
cee0: 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck)(sqlite3_file
cef0: 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28  *, int);.  int (
cf00: 2a 78 55 6e 6c 6f 63 6b 29 28 73 71 6c 69 74 65  *xUnlock)(sqlite
cf10: 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20  3_file*, int);. 
cf20: 20 69 6e 74 20 28 2a 78 43 68 65 63 6b 52 65 73   int (*xCheckRes
cf30: 65 72 76 65 64 4c 6f 63 6b 29 28 73 71 6c 69 74  ervedLock)(sqlit
cf40: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 2a 70  e3_file*, int *p
cf50: 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28  ResOut);.  int (
cf60: 2a 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 29 28 73  *xFileControl)(s
cf70: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
cf80: 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67  t op, void *pArg
cf90: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 74  );.  int (*xSect
cfa0: 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 5f  orSize)(sqlite3_
cfb0: 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  file*);.  int (*
cfc0: 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  xDeviceCharacter
cfd0: 69 73 74 69 63 73 29 28 73 71 6c 69 74 65 33 5f  istics)(sqlite3_
cfe0: 66 69 6c 65 2a 29 3b 0a 20 20 2f 2a 20 4d 65 74  file*);.  /* Met
cff0: 68 6f 64 73 20 61 62 6f 76 65 20 61 72 65 20 76  hods above are v
d000: 61 6c 69 64 20 66 6f 72 20 76 65 72 73 69 6f 6e  alid for version
d010: 20 31 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53   1 */.  int (*xS
d020: 68 6d 4d 61 70 29 28 73 71 6c 69 74 65 33 5f 66  hmMap)(sqlite3_f
d030: 69 6c 65 2a 2c 20 69 6e 74 20 69 50 67 2c 20 69  ile*, int iPg, i
d040: 6e 74 20 70 67 73 7a 2c 20 69 6e 74 2c 20 76 6f  nt pgsz, int, vo
d050: 69 64 20 76 6f 6c 61 74 69 6c 65 2a 2a 29 3b 0a  id volatile**);.
d060: 20 20 69 6e 74 20 28 2a 78 53 68 6d 4c 6f 63 6b    int (*xShmLock
d070: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  )(sqlite3_file*,
d080: 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 69 6e 74   int offset, int
d090: 20 6e 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a   n, int flags);.
d0a0: 20 20 76 6f 69 64 20 28 2a 78 53 68 6d 42 61 72    void (*xShmBar
d0b0: 72 69 65 72 29 28 73 71 6c 69 74 65 33 5f 66 69  rier)(sqlite3_fi
d0c0: 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53  le*);.  int (*xS
d0d0: 68 6d 55 6e 6d 61 70 29 28 73 71 6c 69 74 65 33  hmUnmap)(sqlite3
d0e0: 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 64 65 6c 65  _file*, int dele
d0f0: 74 65 46 6c 61 67 29 3b 0a 20 20 2f 2a 20 4d 65  teFlag);.  /* Me
d100: 74 68 6f 64 73 20 61 62 6f 76 65 20 61 72 65 20  thods above are 
d110: 76 61 6c 69 64 20 66 6f 72 20 76 65 72 73 69 6f  valid for versio
d120: 6e 20 32 20 2a 2f 0a 20 20 2f 2a 20 41 64 64 69  n 2 */.  /* Addi
d130: 74 69 6f 6e 61 6c 20 6d 65 74 68 6f 64 73 20 6d  tional methods m
d140: 61 79 20 62 65 20 61 64 64 65 64 20 69 6e 20 66  ay be added in f
d150: 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 2a  uture releases *
d160: 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  /.};../*.** CAPI
d170: 33 52 45 46 3a 20 53 74 61 6e 64 61 72 64 20 46  3REF: Standard F
d180: 69 6c 65 20 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f  ile Control Opco
d190: 64 65 73 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  des.**.** These 
d1a0: 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
d1b0: 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f  s are opcodes fo
d1c0: 72 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72  r the xFileContr
d1d0: 6f 6c 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20  ol method.** of 
d1e0: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  the [sqlite3_io_
d1f0: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20  methods] object 
d200: 61 6e 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c  and for the [sql
d210: 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
d220: 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63  l()].** interfac
d230: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51  e..**.** The [SQ
d240: 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53  LITE_FCNTL_LOCKS
d250: 54 41 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20  TATE] opcode is 
d260: 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69  used for debuggi
d270: 6e 67 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63  ng.  This.** opc
d280: 6f 64 65 20 63 61 75 73 65 73 20 74 68 65 20 78  ode causes the x
d290: 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68  FileControl meth
d2a0: 6f 64 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  od to write the 
d2b0: 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66  current state of
d2c0: 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e  .** the lock (on
d2d0: 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  e of [SQLITE_LOC
d2e0: 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45  K_NONE], [SQLITE
d2f0: 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a  _LOCK_SHARED],.*
d300: 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52  * [SQLITE_LOCK_R
d310: 45 53 45 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54  ESERVED], [SQLIT
d320: 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c  E_LOCK_PENDING],
d330: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b   or [SQLITE_LOCK
d340: 5f 45 58 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20  _EXCLUSIVE]).** 
d350: 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  into an integer 
d360: 74 68 61 74 20 74 68 65 20 70 41 72 67 20 61 72  that the pArg ar
d370: 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f  gument points to
d380: 2e 20 54 68 69 73 20 63 61 70 61 62 69 6c 69 74  . This capabilit
d390: 79 0a 2a 2a 20 69 73 20 75 73 65 64 20 64 75 72  y.** is used dur
d3a0: 69 6e 67 20 74 65 73 74 69 6e 67 20 61 6e 64 20  ing testing and 
d3b0: 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 62 65  only needs to be
d3c0: 20 73 75 70 70 6f 72 74 65 64 20 77 68 65 6e 20   supported when 
d3d0: 53 51 4c 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69  SQLITE_TEST.** i
d3e0: 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 3c 75  s defined..** <u
d3f0: 6c 3e 0a 2a 2a 20 3c 6c 69 3e 5b 5b 53 51 4c 49  l>.** <li>[[SQLI
d400: 54 45 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f 48 49  TE_FCNTL_SIZE_HI
d410: 4e 54 5d 5d 0a 2a 2a 20 54 68 65 20 5b 53 51 4c  NT]].** The [SQL
d420: 49 54 45 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f 48  ITE_FCNTL_SIZE_H
d430: 49 4e 54 5d 20 6f 70 63 6f 64 65 20 69 73 20 75  INT] opcode is u
d440: 73 65 64 20 62 79 20 53 51 4c 69 74 65 20 74 6f  sed by SQLite to
d450: 20 67 69 76 65 20 74 68 65 20 56 46 53 0a 2a 2a   give the VFS.**
d460: 20 6c 61 79 65 72 20 61 20 68 69 6e 74 20 6f 66   layer a hint of
d470: 20 68 6f 77 20 6c 61 72 67 65 20 74 68 65 20 64   how large the d
d480: 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 6c  atabase file wil
d490: 6c 20 67 72 6f 77 20 74 6f 20 62 65 20 64 75 72  l grow to be dur
d4a0: 69 6e 67 20 74 68 65 0a 2a 2a 20 63 75 72 72 65  ing the.** curre
d4b0: 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  nt transaction. 
d4c0: 20 54 68 69 73 20 68 69 6e 74 20 69 73 20 6e 6f   This hint is no
d4d0: 74 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  t guaranteed to 
d4e0: 62 65 20 61 63 63 75 72 61 74 65 20 62 75 74 20  be accurate but 
d4f0: 69 74 0a 2a 2a 20 69 73 20 6f 66 74 65 6e 20 63  it.** is often c
d500: 6c 6f 73 65 2e 20 20 54 68 65 20 75 6e 64 65 72  lose.  The under
d510: 6c 79 69 6e 67 20 56 46 53 20 6d 69 67 68 74 20  lying VFS might 
d520: 63 68 6f 6f 73 65 20 74 6f 20 70 72 65 61 6c 6c  choose to preall
d530: 6f 63 61 74 65 20 64 61 74 61 62 61 73 65 0a 2a  ocate database.*
d540: 2a 20 66 69 6c 65 20 73 70 61 63 65 20 62 61 73  * file space bas
d550: 65 64 20 6f 6e 20 74 68 69 73 20 68 69 6e 74 20  ed on this hint 
d560: 69 6e 20 6f 72 64 65 72 20 74 6f 20 68 65 6c 70  in order to help
d570: 20 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 64   writes to the d
d580: 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20  atabase.** file 
d590: 72 75 6e 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a  run faster..**.*
d5a0: 2a 20 3c 6c 69 3e 5b 5b 53 51 4c 49 54 45 5f 46  * <li>[[SQLITE_F
d5b0: 43 4e 54 4c 5f 43 48 55 4e 4b 5f 53 49 5a 45 5d  CNTL_CHUNK_SIZE]
d5c0: 5d 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45  ].** The [SQLITE
d5d0: 5f 46 43 4e 54 4c 5f 43 48 55 4e 4b 5f 53 49 5a  _FCNTL_CHUNK_SIZ
d5e0: 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65  E] opcode is use
d5f0: 64 20 74 6f 20 72 65 71 75 65 73 74 20 74 68 61  d to request tha
d600: 74 20 74 68 65 20 56 46 53 0a 2a 2a 20 65 78 74  t the VFS.** ext
d610: 65 6e 64 73 20 61 6e 64 20 74 72 75 6e 63 61 74  ends and truncat
d620: 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  es the database 
d630: 66 69 6c 65 20 69 6e 20 63 68 75 6e 6b 73 20 6f  file in chunks o
d640: 66 20 61 20 73 69 7a 65 20 73 70 65 63 69 66 69  f a size specifi
d650: 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 75 73 65  ed.** by the use
d660: 72 2e 20 54 68 65 20 66 6f 75 72 74 68 20 61 72  r. The fourth ar
d670: 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74  gument to [sqlit
d680: 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28  e3_file_control(
d690: 29 5d 20 73 68 6f 75 6c 64 20 0a 2a 2a 20 70 6f  )] should .** po
d6a0: 69 6e 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65  int to an intege
d6b0: 72 20 28 74 79 70 65 20 69 6e 74 29 20 63 6f 6e  r (type int) con
d6c0: 74 61 69 6e 69 6e 67 20 74 68 65 20 6e 65 77 20  taining the new 
d6d0: 63 68 75 6e 6b 2d 73 69 7a 65 20 74 6f 20 75 73  chunk-size to us
d6e0: 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6e 6f 6d  e.** for the nom
d6f0: 69 6e 61 74 65 64 20 64 61 74 61 62 61 73 65 2e  inated database.
d700: 20 41 6c 6c 6f 63 61 74 69 6e 67 20 64 61 74 61   Allocating data
d710: 62 61 73 65 20 66 69 6c 65 20 73 70 61 63 65 20  base file space 
d720: 69 6e 20 6c 61 72 67 65 0a 2a 2a 20 63 68 75 6e  in large.** chun
d730: 6b 73 20 28 73 61 79 20 31 4d 42 20 61 74 20 61  ks (say 1MB at a
d740: 20 74 69 6d 65 29 2c 20 6d 61 79 20 72 65 64 75   time), may redu
d750: 63 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 66  ce file-system f
d760: 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 61 6e 64  ragmentation and
d770: 0a 2a 2a 20 69 6d 70 72 6f 76 65 20 70 65 72 66  .** improve perf
d780: 6f 72 6d 61 6e 63 65 20 6f 6e 20 73 6f 6d 65 20  ormance on some 
d790: 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 3c  systems..**.** <
d7a0: 6c 69 3e 5b 5b 53 51 4c 49 54 45 5f 46 43 4e 54  li>[[SQLITE_FCNT
d7b0: 4c 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 5d 5d  L_FILE_POINTER]]
d7c0: 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f  .** The [SQLITE_
d7d0: 46 43 4e 54 4c 5f 46 49 4c 45 5f 50 4f 49 4e 54  FCNTL_FILE_POINT
d7e0: 45 52 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73  ER] opcode is us
d7f0: 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 70  ed to obtain a p
d800: 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65  ointer.** to the
d810: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20   [sqlite3_file] 
d820: 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65  object associate
d830: 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75  d with a particu
d840: 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  lar database.** 
d850: 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 53 65 65  connection.  See
d860: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69   the [sqlite3_fi
d870: 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 64 6f  le_control()] do
d880: 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 0a  cumentation for.
d890: 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  ** additional in
d8a0: 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  formation..**.**
d8b0: 20 3c 6c 69 3e 5b 5b 53 51 4c 49 54 45 5f 46 43   <li>[[SQLITE_FC
d8c0: 4e 54 4c 5f 53 59 4e 43 5f 4f 4d 49 54 54 45 44  NTL_SYNC_OMITTED
d8d0: 5d 5d 0a 2a 2a 20 5e 28 54 68 65 20 5b 53 51 4c  ]].** ^(The [SQL
d8e0: 49 54 45 5f 46 43 4e 54 4c 5f 53 59 4e 43 5f 4f  ITE_FCNTL_SYNC_O
d8f0: 4d 49 54 54 45 44 5d 20 6f 70 63 6f 64 65 20 69  MITTED] opcode i
d900: 73 20 67 65 6e 65 72 61 74 65 64 20 69 6e 74 65  s generated inte
d910: 72 6e 61 6c 6c 79 20 62 79 0a 2a 2a 20 53 51 4c  rnally by.** SQL
d920: 69 74 65 20 61 6e 64 20 73 65 6e 74 20 74 6f 20  ite and sent to 
d930: 61 6c 6c 20 56 46 53 65 73 20 69 6e 20 70 6c 61  all VFSes in pla
d940: 63 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20  ce of a call to 
d950: 74 68 65 20 78 53 79 6e 63 20 6d 65 74 68 6f 64  the xSync method
d960: 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 64 61 74  .** when the dat
d970: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
d980: 20 68 61 73 20 5b 50 52 41 47 4d 41 20 73 79 6e   has [PRAGMA syn
d990: 63 68 72 6f 6e 6f 75 73 5d 20 73 65 74 20 74 6f  chronous] set to
d9a0: 20 4f 46 46 2e 29 5e 0a 2a 2a 20 53 6f 6d 65 20   OFF.)^.** Some 
d9b0: 73 70 65 63 69 61 6c 69 7a 65 64 20 56 46 53 65  specialized VFSe
d9c0: 73 20 6e 65 65 64 20 74 68 69 73 20 73 69 67 6e  s need this sign
d9d0: 61 6c 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6f  al in order to o
d9e0: 70 65 72 61 74 65 20 63 6f 72 72 65 63 74 6c 79  perate correctly
d9f0: 0a 2a 2a 20 77 68 65 6e 20 5b 50 52 41 47 4d 41  .** when [PRAGMA
da00: 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 7c 20 50   synchronous | P
da10: 52 41 47 4d 41 20 73 79 6e 63 68 72 6f 6e 6f 75  RAGMA synchronou
da20: 73 3d 4f 46 46 5d 20 69 73 20 73 65 74 2c 20 62  s=OFF] is set, b
da30: 75 74 20 6d 6f 73 74 20 0a 2a 2a 20 56 46 53 65  ut most .** VFSe
da40: 73 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 68  s do not need th
da50: 69 73 20 73 69 67 6e 61 6c 20 61 6e 64 20 73 68  is signal and sh
da60: 6f 75 6c 64 20 73 69 6c 65 6e 74 6c 79 20 69 67  ould silently ig
da70: 6e 6f 72 65 20 74 68 69 73 20 6f 70 63 6f 64 65  nore this opcode
da80: 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e  ..** Application
da90: 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 63 61 6c  s should not cal
daa0: 6c 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  l [sqlite3_file_
dab0: 63 6f 6e 74 72 6f 6c 28 29 5d 20 77 69 74 68 20  control()] with 
dac0: 74 68 69 73 0a 2a 2a 20 6f 70 63 6f 64 65 20 61  this.** opcode a
dad0: 73 20 64 6f 69 6e 67 20 73 6f 20 6d 61 79 20 64  s doing so may d
dae0: 69 73 72 75 70 74 20 74 68 65 20 6f 70 65 72 61  isrupt the opera
daf0: 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 70 65 63  tion of the spec
db00: 69 61 6c 69 7a 65 64 20 56 46 53 65 73 0a 2a 2a  ialized VFSes.**
db10: 20 74 68 61 74 20 64 6f 20 72 65 71 75 69 72 65   that do require
db20: 20 69 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 3c 6c 69   it.  .**.** <li
db30: 3e 5b 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  >[[SQLITE_FCNTL_
db40: 57 49 4e 33 32 5f 41 56 5f 52 45 54 52 59 5d 5d  WIN32_AV_RETRY]]
db50: 0a 2a 2a 20 5e 54 68 65 20 5b 53 51 4c 49 54 45  .** ^The [SQLITE
db60: 5f 46 43 4e 54 4c 5f 57 49 4e 33 32 5f 41 56 5f  _FCNTL_WIN32_AV_
db70: 52 45 54 52 59 5d 20 6f 70 63 6f 64 65 20 69 73  RETRY] opcode is
db80: 20 75 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75   used to configu
db90: 72 65 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20  re automatic.** 
dba0: 72 65 74 72 79 20 63 6f 75 6e 74 73 20 61 6e 64  retry counts and
dbb0: 20 69 6e 74 65 72 76 61 6c 73 20 66 6f 72 20 63   intervals for c
dbc0: 65 72 74 61 69 6e 20 64 69 73 6b 20 49 2f 4f 20  ertain disk I/O 
dbd0: 6f 70 65 72 61 74 69 6f 6e 73 20 66 6f 72 20 74  operations for t
dbe0: 68 65 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 5b 56  he.** windows [V
dbf0: 46 53 5d 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  FS] in order to 
dc00: 70 72 6f 76 69 64 65 20 72 6f 62 75 73 74 6e 65  provide robustne
dc10: 73 73 20 69 6e 20 74 68 65 20 70 72 65 73 65 6e  ss in the presen
dc20: 63 65 20 6f 66 0a 2a 2a 20 61 6e 74 69 2d 76 69  ce of.** anti-vi
dc30: 72 75 73 20 70 72 6f 67 72 61 6d 73 2e 20 20 42  rus programs.  B
dc40: 79 20 64 65 66 61 75 6c 74 2c 20 74 68 65 20 77  y default, the w
dc50: 69 6e 64 6f 77 73 20 56 46 53 20 77 69 6c 6c 20  indows VFS will 
dc60: 72 65 74 72 79 20 66 69 6c 65 20 72 65 61 64 2c  retry file read,
dc70: 0a 2a 2a 20 66 69 6c 65 20 77 72 69 74 65 2c 20  .** file write, 
dc80: 61 6e 64 20 66 69 6c 65 20 64 65 6c 65 74 65 20  and file delete 
dc90: 6f 70 65 72 61 74 69 6f 6e 73 20 75 70 20 74 6f  operations up to
dca0: 20 31 30 20 74 69 6d 65 73 2c 20 77 69 74 68 20   10 times, with 
dcb0: 61 20 64 65 6c 61 79 0a 2a 2a 20 6f 66 20 32 35  a delay.** of 25
dcc0: 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 62 65   milliseconds be
dcd0: 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 72  fore the first r
dce0: 65 74 72 79 20 61 6e 64 20 77 69 74 68 20 74 68  etry and with th
dcf0: 65 20 64 65 6c 61 79 20 69 6e 63 72 65 61 73 69  e delay increasi
dd00: 6e 67 0a 2a 2a 20 62 79 20 61 6e 20 61 64 64 69  ng.** by an addi
dd10: 74 69 6f 6e 61 6c 20 32 35 20 6d 69 6c 6c 69 73  tional 25 millis
dd20: 65 63 6f 6e 64 73 20 77 69 74 68 20 65 61 63 68  econds with each
dd30: 20 73 75 62 73 65 71 75 65 6e 74 20 72 65 74 72   subsequent retr
dd40: 79 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63 6f  y.  This.** opco
dd50: 64 65 20 61 6c 6c 6f 77 73 20 74 68 65 73 65 20  de allows these 
dd60: 74 77 6f 20 76 61 6c 75 65 73 20 28 31 30 20 72  two values (10 r
dd70: 65 74 72 69 65 73 20 61 6e 64 20 32 35 20 6d 69  etries and 25 mi
dd80: 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 64 65  lliseconds of de
dd90: 6c 61 79 29 0a 2a 2a 20 74 6f 20 62 65 20 61 64  lay).** to be ad
dda0: 6a 75 73 74 65 64 2e 20 20 54 68 65 20 76 61 6c  justed.  The val
ddb0: 75 65 73 20 61 72 65 20 63 68 61 6e 67 65 64 20  ues are changed 
ddc0: 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65  for all database
ddd0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a 2a 20   connections.** 
dde0: 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20  within the same 
ddf0: 70 72 6f 63 65 73 73 2e 20 20 54 68 65 20 61 72  process.  The ar
de00: 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e  gument is a poin
de10: 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20  ter to an array 
de20: 6f 66 20 74 77 6f 0a 2a 2a 20 69 6e 74 65 67 65  of two.** intege
de30: 72 73 20 77 68 65 72 65 20 74 68 65 20 66 69 72  rs where the fir
de40: 73 74 20 69 6e 74 65 67 65 72 20 69 20 74 68 65  st integer i the
de50: 20 6e 65 77 20 72 65 74 72 79 20 63 6f 75 6e 74   new retry count
de60: 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 0a   and the second.
de70: 2a 2a 20 69 6e 74 65 67 65 72 20 69 73 20 74 68  ** integer is th
de80: 65 20 64 65 6c 61 79 2e 20 20 49 66 20 65 69 74  e delay.  If eit
de90: 68 65 72 20 69 6e 74 65 67 65 72 20 69 73 20 6e  her integer is n
dea0: 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 74 68  egative, then th
deb0: 65 20 73 65 74 74 69 6e 67 0a 2a 2a 20 69 73 20  e setting.** is 
dec0: 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 75 74 20  not changed but 
ded0: 69 6e 73 74 65 61 64 20 74 68 65 20 70 72 69 6f  instead the prio
dee0: 72 20 76 61 6c 75 65 20 6f 66 20 74 68 61 74 20  r value of that 
def0: 73 65 74 74 69 6e 67 20 69 73 20 77 72 69 74 74  setting is writt
df00: 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 61  en.** into the a
df10: 72 72 61 79 20 65 6e 74 72 79 2c 20 61 6c 6c 6f  rray entry, allo
df20: 77 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74  wing the current
df30: 20 72 65 74 72 79 20 73 65 74 74 69 6e 67 73 20   retry settings 
df40: 74 6f 20 62 65 0a 2a 2a 20 69 6e 74 65 72 72 6f  to be.** interro
df50: 67 61 74 65 64 2e 20 20 54 68 65 20 7a 44 62 4e  gated.  The zDbN
df60: 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 69 73  ame parameter is
df70: 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   ignored..**.** 
df80: 3c 6c 69 3e 5b 5b 53 51 4c 49 54 45 5f 46 43 4e  <li>[[SQLITE_FCN
df90: 54 4c 5f 50 45 52 53 49 53 54 5f 57 41 4c 5d 5d  TL_PERSIST_WAL]]
dfa0: 0a 2a 2a 20 5e 54 68 65 20 5b 53 51 4c 49 54 45  .** ^The [SQLITE
dfb0: 5f 46 43 4e 54 4c 5f 50 45 52 53 49 53 54 5f 57  _FCNTL_PERSIST_W
dfc0: 41 4c 5d 20 6f 70 63 6f 64 65 20 69 73 20 75 73  AL] opcode is us
dfd0: 65 64 20 74 6f 20 73 65 74 20 6f 72 20 71 75 65  ed to set or que
dfe0: 72 79 20 74 68 65 0a 2a 2a 20 70 65 72 73 69 73  ry the.** persis
dff0: 74 65 6e 74 20 5b 57 41 4c 20 7c 20 57 72 69 74  tent [WAL | Writ
e000: 65 20 41 68 65 61 64 20 4c 6f 67 5d 20 73 65 74  e Ahead Log] set
e010: 74 69 6e 67 2e 20 20 42 79 20 64 65 66 61 75 6c  ting.  By defaul
e020: 74 2c 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  t, the auxiliary
e030: 0a 2a 2a 20 77 72 69 74 65 20 61 68 65 61 64 20  .** write ahead 
e040: 6c 6f 67 20 61 6e 64 20 73 68 61 72 65 64 20 6d  log and shared m
e050: 65 6d 6f 72 79 20 66 69 6c 65 73 20 75 73 65 64  emory files used
e060: 20 66 6f 72 20 74 72 61 6e 73 61 63 74 69 6f 6e   for transaction
e070: 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 61 72 65 20   control.** are 
e080: 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65  automatically de
e090: 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 6c  leted when the l
e0a0: 61 74 65 73 74 20 63 6f 6e 6e 65 63 74 69 6f 6e  atest connection
e0b0: 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
e0c0: 0a 2a 2a 20 63 6c 6f 73 65 73 2e 20 20 53 65 74  .** closes.  Set
e0d0: 74 69 6e 67 20 70 65 72 73 69 73 74 65 6e 74 20  ting persistent 
e0e0: 57 41 4c 20 6d 6f 64 65 20 63 61 75 73 65 73 20  WAL mode causes 
e0f0: 74 68 6f 73 65 20 66 69 6c 65 73 20 74 6f 20 70  those files to p
e100: 65 72 73 69 73 74 20 61 66 74 65 72 0a 2a 2a 20  ersist after.** 
e110: 63 6c 6f 73 65 2e 20 20 50 65 72 73 69 73 74 69  close.  Persisti
e120: 6e 67 20 74 68 65 20 66 69 6c 65 73 20 69 73 20  ng the files is 
e130: 75 73 65 66 75 6c 20 77 68 65 6e 20 6f 74 68 65  useful when othe
e140: 72 20 70 72 6f 63 65 73 73 65 73 20 74 68 61 74  r processes that
e150: 20 64 6f 20 6e 6f 74 0a 2a 2a 20 68 61 76 65 20   do not.** have 
e160: 77 72 69 74 65 20 70 65 72 6d 69 73 73 69 6f 6e  write permission
e170: 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72   on the director
e180: 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  y containing the
e190: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
e1a0: 61 6e 74 0a 2a 2a 20 74 6f 20 72 65 61 64 20 74  ant.** to read t
e1b0: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
e1c0: 2c 20 61 73 20 74 68 65 20 57 41 4c 20 61 6e 64  , as the WAL and
e1d0: 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 66   shared memory f
e1e0: 69 6c 65 73 20 6d 75 73 74 20 65 78 69 73 74 0a  iles must exist.
e1f0: 2a 2a 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20  ** in order for 
e200: 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
e210: 62 65 20 72 65 61 64 61 62 6c 65 2e 20 20 54 68  be readable.  Th
e220: 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74  e fourth paramet
e230: 65 72 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65  er to.** [sqlite
e240: 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29  3_file_control()
e250: 5d 20 66 6f 72 20 74 68 69 73 20 6f 70 63 6f 64  ] for this opcod
e260: 65 20 73 68 6f 75 6c 64 20 62 65 20 61 20 70 6f  e should be a po
e270: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 74 65  inter to an inte
e280: 67 65 72 2e 0a 2a 2a 20 54 68 61 74 20 69 6e 74  ger..** That int
e290: 65 67 65 72 20 69 73 20 30 20 74 6f 20 64 69 73  eger is 0 to dis
e2a0: 61 62 6c 65 20 70 65 72 73 69 73 74 65 6e 74 20  able persistent 
e2b0: 57 41 4c 20 6d 6f 64 65 20 6f 72 20 31 20 74 6f  WAL mode or 1 to
e2c0: 20 65 6e 61 62 6c 65 20 70 65 72 73 69 73 74 65   enable persiste
e2d0: 6e 74 0a 2a 2a 20 57 41 4c 20 6d 6f 64 65 2e 20  nt.** WAL mode. 
e2e0: 20 49 66 20 74 68 65 20 69 6e 74 65 67 65 72 20   If the integer 
e2f0: 69 73 20 2d 31 2c 20 74 68 65 6e 20 69 74 20 69  is -1, then it i
e300: 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69  s overwritten wi
e310: 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a  th the current.*
e320: 2a 20 57 41 4c 20 70 65 72 73 69 73 74 65 6e 63  * WAL persistenc
e330: 65 20 73 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a  e setting..**.**
e340: 20 3c 6c 69 3e 5b 5b 53 51 4c 49 54 45 5f 46 43   <li>[[SQLITE_FC
e350: 4e 54 4c 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56  NTL_POWERSAFE_OV
e360: 45 52 57 52 49 54 45 5d 5d 0a 2a 2a 20 5e 54 68  ERWRITE]].** ^Th
e370: 65 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  e [SQLITE_FCNTL_
e380: 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 52  POWERSAFE_OVERWR
e390: 49 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 75  ITE] opcode is u
e3a0: 73 65 64 20 74 6f 20 73 65 74 20 6f 72 20 71 75  sed to set or qu
e3b0: 65 72 79 20 74 68 65 0a 2a 2a 20 70 65 72 73 69  ery the.** persi
e3c0: 73 74 65 6e 74 20 22 70 6f 77 65 72 73 61 66 65  stent "powersafe
e3d0: 2d 6f 76 65 72 77 72 69 74 65 22 20 6f 72 20 22  -overwrite" or "
e3e0: 50 53 4f 57 22 20 73 65 74 74 69 6e 67 2e 20 20  PSOW" setting.  
e3f0: 54 68 65 20 50 53 4f 57 20 73 65 74 74 69 6e 67  The PSOW setting
e400: 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 73 20 74  .** determines t
e410: 68 65 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  he [SQLITE_IOCAP
e420: 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57  _POWERSAFE_OVERW
e430: 52 49 54 45 5d 20 62 69 74 20 6f 66 20 74 68 65  RITE] bit of the
e440: 0a 2a 2a 20 78 44 65 76 69 63 65 43 68 61 72 61  .** xDeviceChara
e450: 63 74 65 72 69 73 74 69 63 73 20 6d 65 74 68 6f  cteristics metho
e460: 64 73 2e 20 54 68 65 20 66 6f 75 72 74 68 20 70  ds. The fourth p
e470: 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b  arameter to.** [
e480: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e  sqlite3_file_con
e490: 74 72 6f 6c 28 29 5d 20 66 6f 72 20 74 68 69 73  trol()] for this
e4a0: 20 6f 70 63 6f 64 65 20 73 68 6f 75 6c 64 20 62   opcode should b
e4b0: 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  e a pointer to a
e4c0: 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 54 68  n integer..** Th
e4d0: 61 74 20 69 6e 74 65 67 65 72 20 69 73 20 30 20  at integer is 0 
e4e0: 74 6f 20 64 69 73 61 62 6c 65 20 7a 65 72 6f 2d  to disable zero-
e4f0: 64 61 6d 61 67 65 20 6d 6f 64 65 20 6f 72 20 31  damage mode or 1
e500: 20 74 6f 20 65 6e 61 62 6c 65 20 7a 65 72 6f 2d   to enable zero-
e510: 64 61 6d 61 67 65 0a 2a 2a 20 6d 6f 64 65 2e 20  damage.** mode. 
e520: 20 49 66 20 74 68 65 20 69 6e 74 65 67 65 72 20   If the integer 
e530: 69 73 20 2d 31 2c 20 74 68 65 6e 20 69 74 20 69  is -1, then it i
e540: 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69  s overwritten wi
e550: 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a  th the current.*
e560: 2a 20 7a 65 72 6f 2d 64 61 6d 61 67 65 20 6d 6f  * zero-damage mo
e570: 64 65 20 73 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a  de setting..**.*
e580: 2a 20 3c 6c 69 3e 5b 5b 53 51 4c 49 54 45 5f 46  * <li>[[SQLITE_F
e590: 43 4e 54 4c 5f 4f 56 45 52 57 52 49 54 45 5d 5d  CNTL_OVERWRITE]]
e5a0: 0a 2a 2a 20 5e 54 68 65 20 5b 53 51 4c 49 54 45  .** ^The [SQLITE
e5b0: 5f 46 43 4e 54 4c 5f 4f 56 45 52 57 52 49 54 45  _FCNTL_OVERWRITE
e5c0: 5d 20 6f 70 63 6f 64 65 20 69 73 20 69 6e 76 6f  ] opcode is invo
e5d0: 6b 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 66  ked by SQLite af
e5e0: 74 65 72 20 6f 70 65 6e 69 6e 67 0a 2a 2a 20 61  ter opening.** a
e5f0: 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
e600: 6f 6e 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  on to indicate t
e610: 68 61 74 2c 20 75 6e 6c 65 73 73 20 69 74 20 69  hat, unless it i
e620: 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 66 6f  s rolled back fo
e630: 72 20 73 6f 6d 65 0a 2a 2a 20 72 65 61 73 6f 6e  r some.** reason
e640: 2c 20 74 68 65 20 65 6e 74 69 72 65 20 64 61 74  , the entire dat
e650: 61 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20  abase file will 
e660: 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 62  be overwritten b
e670: 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 0a 2a  y the current .*
e680: 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54  * transaction. T
e690: 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 56  his is used by V
e6a0: 41 43 55 55 4d 20 6f 70 65 72 61 74 69 6f 6e 73  ACUUM operations
e6b0: 2e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 5b 5b 53 51  ..**.** <li>[[SQ
e6c0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 56 46 53 4e 41  LITE_FCNTL_VFSNA
e6d0: 4d 45 5d 5d 0a 2a 2a 20 5e 54 68 65 20 5b 53 51  ME]].** ^The [SQ
e6e0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 56 46 53 4e 41  LITE_FCNTL_VFSNA
e6f0: 4d 45 5d 20 6f 70 63 6f 64 65 20 63 61 6e 20 62  ME] opcode can b
e700: 65 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e  e used to obtain
e710: 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 0a 2a 2a   the names of.**
e720: 20 61 6c 6c 20 5b 56 46 53 65 73 5d 20 69 6e 20   all [VFSes] in 
e730: 74 68 65 20 56 46 53 20 73 74 61 63 6b 2e 20 20  the VFS stack.  
e740: 54 68 65 20 6e 61 6d 65 73 20 61 72 65 20 6f 66  The names are of
e750: 20 61 6c 6c 20 56 46 53 20 73 68 69 6d 73 20 61   all VFS shims a
e760: 6e 64 20 74 68 65 0a 2a 2a 20 66 69 6e 61 6c 20  nd the.** final 
e770: 62 6f 74 74 6f 6d 2d 6c 65 76 65 6c 20 56 46 53  bottom-level VFS
e780: 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 6e 74   are written int
e790: 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
e7a0: 64 20 66 72 6f 6d 20 0a 2a 2a 20 5b 73 71 6c 69  d from .** [sqli
e7b0: 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 61 6e  te3_malloc()] an
e7c0: 64 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  d the result is 
e7d0: 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 68  stored in the ch
e7e0: 61 72 2a 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20  ar* variable.** 
e7f0: 74 68 61 74 20 74 68 65 20 66 6f 75 72 74 68 20  that the fourth 
e800: 70 61 72 61 6d 65 74 65 72 20 6f 66 20 5b 73 71  parameter of [sq
e810: 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72  lite3_file_contr
e820: 6f 6c 28 29 5d 20 70 6f 69 6e 74 73 20 74 6f 2e  ol()] points to.
e830: 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69  .** The caller i
e840: 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
e850: 72 20 66 72 65 65 69 6e 67 20 74 68 65 20 6d 65  r freeing the me
e860: 6d 6f 72 79 20 77 68 65 6e 20 64 6f 6e 65 2e 20  mory when done. 
e870: 20 41 73 20 77 69 74 68 0a 2a 2a 20 61 6c 6c 20   As with.** all 
e880: 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 61 63 74  file-control act
e890: 69 6f 6e 73 2c 20 74 68 65 72 65 20 69 73 20 6e  ions, there is n
e8a0: 6f 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74  o guarantee that
e8b0: 20 74 68 69 73 20 77 69 6c 6c 20 61 63 74 75 61   this will actua
e8c0: 6c 6c 79 0a 2a 2a 20 64 6f 20 61 6e 79 74 68 69  lly.** do anythi
e8d0: 6e 67 2e 20 20 43 61 6c 6c 65 72 73 20 73 68 6f  ng.  Callers sho
e8e0: 75 6c 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 74  uld initialize t
e8f0: 68 65 20 63 68 61 72 2a 20 76 61 72 69 61 62 6c  he char* variabl
e900: 65 20 74 6f 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70  e to a NULL.** p
e910: 6f 69 6e 74 65 72 20 69 6e 20 63 61 73 65 20 74  ointer in case t
e920: 68 69 73 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c  his file-control
e930: 20 69 73 20 6e 6f 74 20 69 6d 70 6c 65 6d 65 6e   is not implemen
e940: 74 65 64 2e 20 20 54 68 69 73 20 66 69 6c 65 2d  ted.  This file-
e950: 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 69 73 20 69 6e  control.** is in
e960: 74 65 6e 64 65 64 20 66 6f 72 20 64 69 61 67 6e  tended for diagn
e970: 6f 73 74 69 63 20 75 73 65 20 6f 6e 6c 79 2e 0a  ostic use only..
e980: 2a 2a 0a 2a 2a 20 3c 6c 69 3e 5b 5b 53 51 4c 49  **.** <li>[[SQLI
e990: 54 45 5f 46 43 4e 54 4c 5f 50 52 41 47 4d 41 5d  TE_FCNTL_PRAGMA]
e9a0: 5d 0a 2a 2a 20 5e 57 68 65 6e 65 76 65 72 20 61  ].** ^Whenever a
e9b0: 20 5b 50 52 41 47 4d 41 5d 20 73 74 61 74 65 6d   [PRAGMA] statem
e9c0: 65 6e 74 20 69 73 20 70 61 72 73 65 64 2c 20 61  ent is parsed, a
e9d0: 6e 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  n [SQLITE_FCNTL_
e9e0: 50 52 41 47 4d 41 5d 20 0a 2a 2a 20 66 69 6c 65  PRAGMA] .** file
e9f0: 20 63 6f 6e 74 72 6f 6c 20 69 73 20 73 65 6e 74   control is sent
ea00: 20 74 6f 20 74 68 65 20 6f 70 65 6e 20 5b 73 71   to the open [sq
ea10: 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65  lite3_file] obje
ea20: 63 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ct corresponding
ea30: 0a 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62  .** to the datab
ea40: 61 73 65 20 66 69 6c 65 20 74 6f 20 77 68 69 63  ase file to whic
ea50: 68 20 74 68 65 20 70 72 61 67 6d 61 20 73 74 61  h the pragma sta
ea60: 74 65 6d 65 6e 74 20 72 65 66 65 72 73 2e 20 5e  tement refers. ^
ea70: 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  The argument.** 
ea80: 74 6f 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 46  to the [SQLITE_F
ea90: 43 4e 54 4c 5f 50 52 41 47 4d 41 5d 20 66 69 6c  CNTL_PRAGMA] fil
eaa0: 65 20 63 6f 6e 74 72 6f 6c 20 69 73 20 61 6e 20  e control is an 
eab0: 61 72 72 61 79 20 6f 66 0a 2a 2a 20 70 6f 69 6e  array of.** poin
eac0: 74 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20  ters to strings 
ead0: 28 63 68 61 72 2a 2a 29 20 69 6e 20 77 68 69 63  (char**) in whic
eae0: 68 20 74 68 65 20 73 65 63 6f 6e 64 20 65 6c 65  h the second ele
eaf0: 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61  ment of the arra
eb00: 79 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d 65  y.** is the name
eb10: 20 6f 66 20 74 68 65 20 70 72 61 67 6d 61 20 61   of the pragma a
eb20: 6e 64 20 74 68 65 20 74 68 69 72 64 20 65 6c 65  nd the third ele
eb30: 6d 65 6e 74 20 69 73 20 74 68 65 20 61 72 67 75  ment is the argu
eb40: 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 70  ment to the.** p
eb50: 72 61 67 6d 61 20 6f 72 20 4e 55 4c 4c 20 69 66  ragma or NULL if
eb60: 20 74 68 65 20 70 72 61 67 6d 61 20 68 61 73 20   the pragma has 
eb70: 6e 6f 20 61 72 67 75 6d 65 6e 74 2e 20 20 5e 54  no argument.  ^T
eb80: 68 65 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61  he handler for a
eb90: 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 46 43 4e  n.** [SQLITE_FCN
eba0: 54 4c 5f 50 52 41 47 4d 41 5d 20 66 69 6c 65 20  TL_PRAGMA] file 
ebb0: 63 6f 6e 74 72 6f 6c 20 63 61 6e 20 6f 70 74 69  control can opti
ebc0: 6f 6e 61 6c 6c 79 20 6d 61 6b 65 20 74 68 65 20  onally make the 
ebd0: 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 0a 2a 2a  first element.**
ebe0: 20 6f 66 20 74 68 65 20 63 68 61 72 2a 2a 20 61   of the char** a
ebf0: 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 20 74 6f  rgument point to
ec00: 20 61 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e   a string obtain
ec10: 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
ec20: 5f 6d 70 72 69 6e 74 66 28 29 5d 0a 2a 2a 20 6f  _mprintf()].** o
ec30: 72 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74  r the equivalent
ec40: 20 61 6e 64 20 74 68 61 74 20 73 74 72 69 6e 67   and that string
ec50: 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65   will become the
ec60: 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 70   result of the p
ec70: 72 61 67 6d 61 20 6f 72 0a 2a 2a 20 74 68 65 20  ragma or.** the 
ec80: 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 66  error message if
ec90: 20 74 68 65 20 70 72 61 67 6d 61 20 66 61 69 6c   the pragma fail
eca0: 73 2e 20 5e 49 66 20 74 68 65 0a 2a 2a 20 5b 53  s. ^If the.** [S
ecb0: 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 52 41 47  QLITE_FCNTL_PRAG
ecc0: 4d 41 5d 20 66 69 6c 65 20 63 6f 6e 74 72 6f 6c  MA] file control
ecd0: 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
ece0: 5f 4e 4f 54 46 4f 55 4e 44 5d 2c 20 74 68 65 6e  _NOTFOUND], then
ecf0: 20 6e 6f 72 6d 61 6c 20 0a 2a 2a 20 5b 50 52 41   normal .** [PRA
ed00: 47 4d 41 5d 20 70 72 6f 63 65 73 73 69 6e 67 20  GMA] processing 
ed10: 63 6f 6e 74 69 6e 75 65 73 2e 20 20 5e 49 66 20  continues.  ^If 
ed20: 74 68 65 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54  the [SQLITE_FCNT
ed30: 4c 5f 50 52 41 47 4d 41 5d 0a 2a 2a 20 66 69 6c  L_PRAGMA].** fil
ed40: 65 20 63 6f 6e 74 72 6f 6c 20 72 65 74 75 72 6e  e control return
ed50: 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 74  s [SQLITE_OK], t
ed60: 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 20 61  hen the parser a
ed70: 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 0a  ssumes that the.
ed80: 2a 2a 20 56 46 53 20 68 61 73 20 68 61 6e 64 6c  ** VFS has handl
ed90: 65 64 20 74 68 65 20 50 52 41 47 4d 41 20 69 74  ed the PRAGMA it
eda0: 73 65 6c 66 20 61 6e 64 20 74 68 65 20 70 61 72  self and the par
edb0: 73 65 72 20 67 65 6e 65 72 61 74 65 73 20 61 20  ser generates a 
edc0: 6e 6f 2d 6f 70 0a 2a 2a 20 70 72 65 70 61 72 65  no-op.** prepare
edd0: 64 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 5e 49  d statement.  ^I
ede0: 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 46 43  f the [SQLITE_FC
edf0: 4e 54 4c 5f 50 52 41 47 4d 41 5d 20 66 69 6c 65  NTL_PRAGMA] file
ee00: 20 63 6f 6e 74 72 6f 6c 20 72 65 74 75 72 6e 73   control returns
ee10: 0a 2a 2a 20 61 6e 79 20 72 65 73 75 6c 74 20 63  .** any result c
ee20: 6f 64 65 20 6f 74 68 65 72 20 74 68 61 6e 20 5b  ode other than [
ee30: 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 72 20 5b 53  SQLITE_OK] or [S
ee40: 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 5d 2c  QLITE_NOTFOUND],
ee50: 20 74 68 61 74 20 6d 65 61 6e 73 0a 2a 2a 20 74   that means.** t
ee60: 68 61 74 20 74 68 65 20 56 46 53 20 65 6e 63 6f  hat the VFS enco
ee70: 75 6e 74 65 72 65 64 20 61 6e 20 65 72 72 6f 72  untered an error
ee80: 20 77 68 69 6c 65 20 68 61 6e 64 6c 69 6e 67 20   while handling 
ee90: 74 68 65 20 5b 50 52 41 47 4d 41 5d 20 61 6e 64  the [PRAGMA] and
eea0: 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 61 74   the.** compilat
eeb0: 69 6f 6e 20 6f 66 20 74 68 65 20 50 52 41 47 4d  ion of the PRAGM
eec0: 41 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20  A fails with an 
eed0: 65 72 72 6f 72 2e 20 20 5e 54 68 65 20 5b 53 51  error.  ^The [SQ
eee0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 52 41 47 4d  LITE_FCNTL_PRAGM
eef0: 41 5d 0a 2a 2a 20 66 69 6c 65 20 63 6f 6e 74 72  A].** file contr
ef00: 6f 6c 20 6f 63 63 75 72 73 20 61 74 20 74 68 65  ol occurs at the
ef10: 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 70 72   beginning of pr
ef20: 61 67 6d 61 20 73 74 61 74 65 6d 65 6e 74 20 61  agma statement a
ef30: 6e 61 6c 79 73 69 73 20 61 6e 64 20 73 6f 0a 2a  nalysis and so.*
ef40: 2a 20 69 74 20 69 73 20 61 62 6c 65 20 74 6f 20  * it is able to 
ef50: 6f 76 65 72 72 69 64 65 20 62 75 69 6c 74 2d 69  override built-i
ef60: 6e 20 5b 50 52 41 47 4d 41 5d 20 73 74 61 74 65  n [PRAGMA] state
ef70: 6d 65 6e 74 73 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a  ments..** </ul>.
ef80: 2a 2a 0a 2a 2a 20 3c 6c 69 3e 5b 5b 53 51 4c 49  **.** <li>[[SQLI
ef90: 54 45 5f 46 43 4e 54 4c 5f 42 55 53 59 48 41 4e  TE_FCNTL_BUSYHAN
efa0: 44 4c 45 52 5d 5d 0a 2a 2a 20 5e 54 68 69 73 20  DLER]].** ^This 
efb0: 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 6d 61 79  file-control may
efc0: 20 62 65 20 69 6e 76 6f 6b 65 64 20 62 79 20 53   be invoked by S
efd0: 51 4c 69 74 65 20 6f 6e 20 74 68 65 20 64 61 74  QLite on the dat
efe0: 61 62 61 73 65 20 66 69 6c 65 20 68 61 6e 64 6c  abase file handl
eff0: 65 0a 2a 2a 20 73 68 6f 72 74 6c 79 20 61 66 74  e.** shortly aft
f000: 65 72 20 69 74 20 69 73 20 6f 70 65 6e 65 64 20  er it is opened 
f010: 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 6f 76  in order to prov
f020: 69 64 65 20 61 20 63 75 73 74 6f 6d 20 56 46 53  ide a custom VFS
f030: 20 77 69 74 68 20 61 63 63 65 73 73 0a 2a 2a 20   with access.** 
f040: 74 6f 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  to the connectio
f050: 6e 73 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20  ns busy-handler 
f060: 63 61 6c 6c 62 61 63 6b 2e 20 54 68 65 20 61 72  callback. The ar
f070: 67 75 6d 65 6e 74 20 69 73 20 6f 66 20 74 79 70  gument is of typ
f080: 65 20 28 76 6f 69 64 20 2a 2a 29 0a 2a 2a 20 2d  e (void **).** -
f090: 20 61 6e 20 61 72 72 61 79 20 6f 66 20 74 77 6f   an array of two
f0a0: 20 28 76 6f 69 64 20 2a 29 20 76 61 6c 75 65 73   (void *) values
f0b0: 2e 20 54 68 65 20 66 69 72 73 74 20 28 76 6f 69  . The first (voi
f0c0: 64 20 2a 29 20 61 63 74 75 61 6c 6c 79 20 70 6f  d *) actually po
f0d0: 69 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 66 75 6e  ints.** to a fun
f0e0: 63 74 69 6f 6e 20 6f 66 20 74 79 70 65 20 28 69  ction of type (i
f0f0: 6e 74 20 28 2a 29 28 76 6f 69 64 20 2a 29 29 2e  nt (*)(void *)).
f100: 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 76   In order to inv
f110: 6f 6b 65 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  oke the connecti
f120: 6f 6e 73 0a 2a 2a 20 62 75 73 79 2d 68 61 6e 64  ons.** busy-hand
f130: 6c 65 72 2c 20 74 68 69 73 20 66 75 6e 63 74 69  ler, this functi
f140: 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 76  on should be inv
f150: 6f 6b 65 64 20 77 69 74 68 20 74 68 65 20 73 65  oked with the se
f160: 63 6f 6e 64 20 28 76 6f 69 64 20 2a 29 20 69 6e  cond (void *) in
f170: 0a 2a 2a 20 74 68 65 20 61 72 72 61 79 20 61 73  .** the array as
f180: 20 74 68 65 20 6f 6e 6c 79 20 61 72 67 75 6d 65   the only argume
f190: 6e 74 2e 20 49 66 20 69 74 20 72 65 74 75 72 6e  nt. If it return
f1a0: 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
f1b0: 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a   the operation.*
f1c0: 2a 20 73 68 6f 75 6c 64 20 62 65 20 72 65 74 72  * should be retr
f1d0: 69 65 64 2e 20 49 66 20 69 74 20 72 65 74 75 72  ied. If it retur
f1e0: 6e 73 20 7a 65 72 6f 2c 20 74 68 65 20 63 75 73  ns zero, the cus
f1f0: 74 6f 6d 20 56 46 53 20 73 68 6f 75 6c 64 20 61  tom VFS should a
f200: 62 61 6e 64 6f 6e 20 74 68 65 0a 2a 2a 20 63 75  bandon the.** cu
f210: 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 2e  rrent operation.
f220: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
f230: 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41  TE_FCNTL_LOCKSTA
f240: 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TE              
f250: 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   1.#define SQLIT
f260: 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46  E_GET_LOCKPROXYF
f270: 49 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ILE             
f280: 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
f290: 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _SET_LOCKPROXYFI
f2a0: 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 33  LE             3
f2b0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
f2c0: 4c 41 53 54 5f 45 52 52 4e 4f 20 20 20 20 20 20  LAST_ERRNO      
f2d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a                4.
f2e0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
f2f0: 43 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e 54 20 20  CNTL_SIZE_HINT  
f300: 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23               5.#
f310: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43  define SQLITE_FC
f320: 4e 54 4c 5f 43 48 55 4e 4b 5f 53 49 5a 45 20 20  NTL_CHUNK_SIZE  
f330: 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64              6.#d
f340: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e  efine SQLITE_FCN
f350: 54 4c 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20  TL_FILE_POINTER 
f360: 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65             7.#de
f370: 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54  fine SQLITE_FCNT
f380: 4c 5f 53 59 4e 43 5f 4f 4d 49 54 54 45 44 20 20  L_SYNC_OMITTED  
f390: 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66            8.#def
f3a0: 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c  ine SQLITE_FCNTL
f3b0: 5f 57 49 4e 33 32 5f 41 56 5f 52 45 54 52 59 20  _WIN32_AV_RETRY 
f3c0: 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69           9.#defi
f3d0: 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  ne SQLITE_FCNTL_
f3e0: 50 45 52 53 49 53 54 5f 57 41 4c 20 20 20 20 20  PERSIST_WAL     
f3f0: 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e         10.#defin
f400: 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4f  e SQLITE_FCNTL_O
f410: 56 45 52 57 52 49 54 45 20 20 20 20 20 20 20 20  VERWRITE        
f420: 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65        11.#define
f430: 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 56 46   SQLITE_FCNTL_VF
f440: 53 4e 41 4d 45 20 20 20 20 20 20 20 20 20 20 20  SNAME           
f450: 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20       12.#define 
f460: 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 4f 57  SQLITE_FCNTL_POW
f470: 45 52 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45  ERSAFE_OVERWRITE
f480: 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 53      13.#define S
f490: 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 52 41 47  QLITE_FCNTL_PRAG
f4a0: 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MA              
f4b0: 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 53 51     14.#define SQ
f4c0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 42 55 53 59 48  LITE_FCNTL_BUSYH
f4d0: 41 4e 44 4c 45 52 20 20 20 20 20 20 20 20 20 20  ANDLER          
f4e0: 20 20 31 35 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49    15../*.** CAPI
f4f0: 33 52 45 46 3a 20 4d 75 74 65 78 20 48 61 6e 64  3REF: Mutex Hand
f500: 6c 65 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74  le.**.** The mut
f510: 65 78 20 6d 6f 64 75 6c 65 20 77 69 74 68 69 6e  ex module within
f520: 20 53 51 4c 69 74 65 20 64 65 66 69 6e 65 73 20   SQLite defines 
f530: 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20  [sqlite3_mutex] 
f540: 74 6f 20 62 65 20 61 6e 0a 2a 2a 20 61 62 73 74  to be an.** abst
f550: 72 61 63 74 20 74 79 70 65 20 66 6f 72 20 61 20  ract type for a 
f560: 6d 75 74 65 78 20 6f 62 6a 65 63 74 2e 20 20 54  mutex object.  T
f570: 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6e  he SQLite core n
f580: 65 76 65 72 20 6c 6f 6f 6b 73 0a 2a 2a 20 61 74  ever looks.** at
f590: 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65   the internal re
f5a0: 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
f5b0: 61 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  an [sqlite3_mute
f5c0: 78 5d 2e 20 20 49 74 20 6f 6e 6c 79 0a 2a 2a 20  x].  It only.** 
f5d0: 64 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e 74  deals with point
f5e0: 65 72 73 20 74 6f 20 74 68 65 20 5b 73 71 6c 69  ers to the [sqli
f5f0: 74 65 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63  te3_mutex] objec
f600: 74 2e 0a 2a 2a 0a 2a 2a 20 4d 75 74 65 78 65 73  t..**.** Mutexes
f610: 20 61 72 65 20 63 72 65 61 74 65 64 20 75 73 69   are created usi
f620: 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  ng [sqlite3_mute
f630: 78 5f 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 74  x_alloc()]..*/.t
f640: 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
f650: 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 71 6c 69  lite3_mutex sqli
f660: 74 65 33 5f 6d 75 74 65 78 3b 0a 0a 2f 2a 0a 2a  te3_mutex;../*.*
f670: 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49  * CAPI3REF: OS I
f680: 6e 74 65 72 66 61 63 65 20 4f 62 6a 65 63 74 0a  nterface Object.
f690: 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  **.** An instanc
f6a0: 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  e of the sqlite3
f6b0: 5f 76 66 73 20 6f 62 6a 65 63 74 20 64 65 66 69  _vfs object defi
f6c0: 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63  nes the interfac
f6d0: 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65  e between.** the
f6e0: 20 53 51 4c 69 74 65 20 63 6f 72 65 20 61 6e 64   SQLite core and
f6f0: 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
f700: 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
f710: 2e 20 20 54 68 65 20 22 76 66 73 22 0a 2a 2a 20  .  The "vfs".** 
f720: 69 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  in the name of t
f730: 68 65 20 6f 62 6a 65 63 74 20 73 74 61 6e 64 73  he object stands
f740: 20 66 6f 72 20 22 76 69 72 74 75 61 6c 20 66 69   for "virtual fi
f750: 6c 65 20 73 79 73 74 65 6d 22 2e 20 20 53 65 65  le system".  See
f760: 0a 2a 2a 20 74 68 65 20 5b 56 46 53 20 7c 20 56  .** the [VFS | V
f770: 46 53 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  FS documentation
f780: 5d 20 66 6f 72 20 66 75 72 74 68 65 72 20 69 6e  ] for further in
f790: 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  formation..**.**
f7a0: 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   The value of th
f7b0: 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64  e iVersion field
f7c0: 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 20   is initially 1 
f7d0: 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72 67 65  but may be large
f7e0: 72 20 69 6e 0a 2a 2a 20 66 75 74 75 72 65 20 76  r in.** future v
f7f0: 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
f800: 65 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66  e.  Additional f
f810: 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70  ields may be app
f820: 65 6e 64 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a  ended to this.**
f830: 20 6f 62 6a 65 63 74 20 77 68 65 6e 20 74 68 65   object when the
f840: 20 69 56 65 72 73 69 6f 6e 20 76 61 6c 75 65 20   iVersion value 
f850: 69 73 20 69 6e 63 72 65 61 73 65 64 2e 20 20 4e  is increased.  N
f860: 6f 74 65 20 74 68 61 74 20 74 68 65 20 73 74 72  ote that the str
f870: 75 63 74 75 72 65 0a 2a 2a 20 6f 66 20 74 68 65  ucture.** of the
f880: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a   sqlite3_vfs obj
f890: 65 63 74 20 63 68 61 6e 67 65 73 20 69 6e 20 74  ect changes in t
f8a0: 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62  he transaction b
f8b0: 65 74 77 65 65 6e 0a 2a 2a 20 53 51 4c 69 74 65  etween.** SQLite
f8c0: 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 39 20 61   version 3.5.9 a
f8d0: 6e 64 20 33 2e 36 2e 30 20 61 6e 64 20 79 65 74  nd 3.6.0 and yet
f8e0: 20 74 68 65 20 69 56 65 72 73 69 6f 6e 20 66 69   the iVersion fi
f8f0: 65 6c 64 20 77 61 73 20 6e 6f 74 0a 2a 2a 20 6d  eld was not.** m
f900: 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  odified..**.** T
f910: 68 65 20 73 7a 4f 73 46 69 6c 65 20 66 69 65 6c  he szOsFile fiel
f920: 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66  d is the size of
f930: 20 74 68 65 20 73 75 62 63 6c 61 73 73 65 64 20   the subclassed 
f940: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 0a 2a  [sqlite3_file].*
f950: 2a 20 73 74 72 75 63 74 75 72 65 20 75 73 65 64  * structure used
f960: 20 62 79 20 74 68 69 73 20 56 46 53 2e 20 20 6d   by this VFS.  m
f970: 78 50 61 74 68 6e 61 6d 65 20 69 73 20 74 68 65  xPathname is the
f980: 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20   maximum length 
f990: 6f 66 0a 2a 2a 20 61 20 70 61 74 68 6e 61 6d 65  of.** a pathname
f9a0: 20 69 6e 20 74 68 69 73 20 56 46 53 2e 0a 2a 2a   in this VFS..**
f9b0: 0a 2a 2a 20 52 65 67 69 73 74 65 72 65 64 20 73  .** Registered s
f9c0: 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63  qlite3_vfs objec
f9d0: 74 73 20 61 72 65 20 6b 65 70 74 20 6f 6e 20 61  ts are kept on a
f9e0: 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 66 6f 72   linked list for
f9f0: 6d 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 4e  med by.** the pN
fa00: 65 78 74 20 70 6f 69 6e 74 65 72 2e 20 20 54 68  ext pointer.  Th
fa10: 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 72  e [sqlite3_vfs_r
fa20: 65 67 69 73 74 65 72 28 29 5d 0a 2a 2a 20 61 6e  egister()].** an
fa30: 64 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 75  d [sqlite3_vfs_u
fa40: 6e 72 65 67 69 73 74 65 72 28 29 5d 20 69 6e 74  nregister()] int
fa50: 65 72 66 61 63 65 73 20 6d 61 6e 61 67 65 20 74  erfaces manage t
fa60: 68 69 73 20 6c 69 73 74 0a 2a 2a 20 69 6e 20 61  his list.** in a
fa70: 20 74 68 72 65 61 64 2d 73 61 66 65 20 77 61 79   thread-safe way
fa80: 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  .  The [sqlite3_
fa90: 76 66 73 5f 66 69 6e 64 28 29 5d 20 69 6e 74 65  vfs_find()] inte
faa0: 72 66 61 63 65 0a 2a 2a 20 73 65 61 72 63 68 65  rface.** searche
fab0: 73 20 74 68 65 20 6c 69 73 74 2e 20 20 4e 65 69  s the list.  Nei
fac0: 74 68 65 72 20 74 68 65 20 61 70 70 6c 69 63 61  ther the applica
fad0: 74 69 6f 6e 20 63 6f 64 65 20 6e 6f 72 20 74 68  tion code nor th
fae0: 65 20 56 46 53 0a 2a 2a 20 69 6d 70 6c 65 6d 65  e VFS.** impleme
faf0: 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75  ntation should u
fb00: 73 65 20 74 68 65 20 70 4e 65 78 74 20 70 6f 69  se the pNext poi
fb10: 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  nter..**.** The 
fb20: 70 4e 65 78 74 20 66 69 65 6c 64 20 69 73 20 74  pNext field is t
fb30: 68 65 20 6f 6e 6c 79 20 66 69 65 6c 64 20 69 6e  he only field in
fb40: 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73   the sqlite3_vfs
fb50: 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 68  .** structure th
fb60: 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 65  at SQLite will e
fb70: 76 65 72 20 6d 6f 64 69 66 79 2e 20 20 53 51 4c  ver modify.  SQL
fb80: 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 61 63  ite will only ac
fb90: 63 65 73 73 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66  cess.** or modif
fba0: 79 20 74 68 69 73 20 66 69 65 6c 64 20 77 68 69  y this field whi
fbb0: 6c 65 20 68 6f 6c 64 69 6e 67 20 61 20 70 61 72  le holding a par
fbc0: 74 69 63 75 6c 61 72 20 73 74 61 74 69 63 20 6d  ticular static m
fbd0: 75 74 65 78 2e 0a 2a 2a 20 54 68 65 20 61 70 70  utex..** The app
fbe0: 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20  lication should 
fbf0: 6e 65 76 65 72 20 6d 6f 64 69 66 79 20 61 6e 79  never modify any
fc00: 74 68 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65  thing within the
fc10: 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20   sqlite3_vfs.** 
fc20: 6f 62 6a 65 63 74 20 6f 6e 63 65 20 74 68 65 20  object once the 
fc30: 6f 62 6a 65 63 74 20 68 61 73 20 62 65 65 6e 20  object has been 
fc40: 72 65 67 69 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a  registered..**.*
fc50: 2a 20 54 68 65 20 7a 4e 61 6d 65 20 66 69 65 6c  * The zName fiel
fc60: 64 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65  d holds the name
fc70: 20 6f 66 20 74 68 65 20 56 46 53 20 6d 6f 64 75   of the VFS modu
fc80: 6c 65 2e 20 20 54 68 65 20 6e 61 6d 65 20 6d 75  le.  The name mu
fc90: 73 74 0a 2a 2a 20 62 65 20 75 6e 69 71 75 65 20  st.** be unique 
fca0: 61 63 72 6f 73 73 20 61 6c 6c 20 56 46 53 20 6d  across all VFS m
fcb0: 6f 64 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 5b 5b  odules..**.** [[
fcc0: 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65  sqlite3_vfs.xOpe
fcd0: 6e 5d 5d 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 67  n]].** ^SQLite g
fce0: 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74  uarantees that t
fcf0: 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 70 61 72  he zFilename par
fd00: 61 6d 65 74 65 72 20 74 6f 20 78 4f 70 65 6e 0a  ameter to xOpen.
fd10: 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 4e  ** is either a N
fd20: 55 4c 4c 20 70 6f 69 6e 74 65 72 20 6f 72 20 73  ULL pointer or s
fd30: 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 0a 2a  tring obtained.*
fd40: 2a 20 66 72 6f 6d 20 78 46 75 6c 6c 50 61 74 68  * from xFullPath
fd50: 6e 61 6d 65 28 29 20 77 69 74 68 20 61 6e 20 6f  name() with an o
fd60: 70 74 69 6f 6e 61 6c 20 73 75 66 66 69 78 20 61  ptional suffix a
fd70: 64 64 65 64 2e 0a 2a 2a 20 5e 49 66 20 61 20 73  dded..** ^If a s
fd80: 75 66 66 69 78 20 69 73 20 61 64 64 65 64 20 74  uffix is added t
fd90: 6f 20 74 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20  o the zFilename 
fda0: 70 61 72 61 6d 65 74 65 72 2c 20 69 74 20 77 69  parameter, it wi
fdb0: 6c 6c 0a 2a 2a 20 63 6f 6e 73 69 73 74 20 6f 66  ll.** consist of
fdc0: 20 61 20 73 69 6e 67 6c 65 20 22 2d 22 20 63 68   a single "-" ch
fdd0: 61 72 61 63 74 65 72 20 66 6f 6c 6c 6f 77 65 64  aracter followed
fde0: 20 62 79 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e   by no more than
fdf0: 0a 2a 2a 20 31 31 20 61 6c 70 68 61 6e 75 6d 65  .** 11 alphanume
fe00: 72 69 63 20 61 6e 64 2f 6f 72 20 22 2d 22 20 63  ric and/or "-" c
fe10: 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 5e 53  haracters..** ^S
fe20: 51 4c 69 74 65 20 66 75 72 74 68 65 72 20 67 75  QLite further gu
fe30: 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a 2a  arantees that.**
fe40: 20 74 68 65 20 73 74 72 69 6e 67 20 77 69 6c 6c   the string will
fe50: 20 62 65 20 76 61 6c 69 64 20 61 6e 64 20 75 6e   be valid and un
fe60: 63 68 61 6e 67 65 64 20 75 6e 74 69 6c 20 78 43  changed until xC
fe70: 6c 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63 61 6c  lose() is.** cal
fe80: 6c 65 64 2e 20 42 65 63 61 75 73 65 20 6f 66 20  led. Because of 
fe90: 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e  the previous sen
fea0: 74 65 6e 63 65 2c 0a 2a 2a 20 74 68 65 20 5b 73  tence,.** the [s
feb0: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 61 6e  qlite3_file] can
fec0: 20 73 61 66 65 6c 79 20 73 74 6f 72 65 20 61 20   safely store a 
fed0: 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a  pointer to the.*
fee0: 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 69 74  * filename if it
fef0: 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 6d 62   needs to rememb
ff00: 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20  er the filename 
ff10: 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2e  for some reason.
ff20: 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 6c 65  .** If the zFile
ff30: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 74  name parameter t
ff40: 6f 20 78 4f 70 65 6e 20 69 73 20 61 20 4e 55 4c  o xOpen is a NUL
ff50: 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 78  L pointer then x
ff60: 4f 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 76  Open.** must inv
ff70: 65 6e 74 20 69 74 73 20 6f 77 6e 20 74 65 6d 70  ent its own temp
ff80: 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 20 74  orary name for t
ff90: 68 65 20 66 69 6c 65 2e 20 20 5e 57 68 65 6e 65  he file.  ^Whene
ffa0: 76 65 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 6c  ver the .** xFil
ffb0: 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20  ename parameter 
ffc0: 69 73 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c 20  is NULL it will 
ffd0: 61 6c 73 6f 20 62 65 20 74 68 65 20 63 61 73 65  also be the case
ffe0: 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c 61   that the.** fla
fff0: 67 73 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c  gs parameter wil
10000 6c 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54  l include [SQLIT
10010 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
10020 4c 4f 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  LOSE]..**.** The
10030 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20   flags argument 
10040 74 6f 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c 75  to xOpen() inclu
10050 64 65 73 20 61 6c 6c 20 62 69 74 73 20 73 65 74  des all bits set
10060 20 69 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67 73   in.** the flags
10070 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71   argument to [sq
10080 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d  lite3_open_v2()]
10090 2e 20 20 4f 72 20 69 66 20 5b 73 71 6c 69 74 65  .  Or if [sqlite
100a0 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72 20  3_open()].** or 
100b0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28  [sqlite3_open16(
100c0 29 5d 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e  )] is used, then
100d0 20 66 6c 61 67 73 20 69 6e 63 6c 75 64 65 73 20   flags includes 
100e0 61 74 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51 4c  at least.** [SQL
100f0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
10100 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50  TE] | [SQLITE_OP
10110 45 4e 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a 20  EN_CREATE]. .** 
10120 49 66 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e 73  If xOpen() opens
10130 20 61 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e 6c   a file read-onl
10140 79 20 74 68 65 6e 20 69 74 20 73 65 74 73 20 2a  y then it sets *
10150 70 4f 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a 20  pOutFlags to.** 
10160 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f  include [SQLITE_
10170 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e 20  OPEN_READONLY]. 
10180 20 4f 74 68 65 72 20 62 69 74 73 20 69 6e 20 2a   Other bits in *
10190 70 4f 75 74 46 6c 61 67 73 20 6d 61 79 20 62 65  pOutFlags may be
101a0 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 5e 28 53 51   set..**.** ^(SQ
101b0 4c 69 74 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61  Lite will also a
101c0 64 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  dd one of the fo
101d0 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f  llowing flags to
101e0 20 74 68 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20   the xOpen().** 
101f0 63 61 6c 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20  call, depending 
10200 6f 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 62 65  on the object be
10210 69 6e 67 20 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a  ing opened:.**.*
10220 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20  * <ul>.** <li>  
10230 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  [SQLITE_OPEN_MAI
10240 4e 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b  N_DB].** <li>  [
10250 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
10260 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69  _JOURNAL].** <li
10270 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  >  [SQLITE_OPEN_
10280 54 45 4d 50 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e  TEMP_DB].** <li>
10290 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54    [SQLITE_OPEN_T
102a0 45 4d 50 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20  EMP_JOURNAL].** 
102b0 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50  <li>  [SQLITE_OP
102c0 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 5d  EN_TRANSIENT_DB]
102d0 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54  .** <li>  [SQLIT
102e0 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41  E_OPEN_SUBJOURNA
102f0 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c  L].** <li>  [SQL
10300 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f  ITE_OPEN_MASTER_
10310 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e  JOURNAL].** <li>
10320 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 57    [SQLITE_OPEN_W
10330 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 29 5e 0a 2a  AL].** </ul>)^.*
10340 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 49 2f  *.** The file I/
10350 4f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  O implementation
10360 20 63 61 6e 20 75 73 65 20 74 68 65 20 6f 62 6a   can use the obj
10370 65 63 74 20 74 79 70 65 20 66 6c 61 67 73 20 74  ect type flags t
10380 6f 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 65 20  o.** change the 
10390 77 61 79 20 69 74 20 64 65 61 6c 73 20 77 69 74  way it deals wit
103a0 68 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 65 78  h files.  For ex
103b0 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63  ample, an applic
103c0 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 64 6f  ation.** that do
103d0 65 73 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75  es not care abou
103e0 74 20 63 72 61 73 68 20 72 65 63 6f 76 65 72 79  t crash recovery
103f0 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d 69 67   or rollback mig
10400 68 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6f  ht make.** the o
10410 70 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c  pen of a journal
10420 20 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20 20   file a no-op.  
10430 57 72 69 74 65 73 20 74 6f 20 74 68 69 73 20 6a  Writes to this j
10440 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a 2a 20  ournal would.** 
10450 61 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73 2c 20  also be no-ops, 
10460 61 6e 64 20 61 6e 79 20 61 74 74 65 6d 70 74 20  and any attempt 
10470 74 6f 20 72 65 61 64 20 74 68 65 20 6a 6f 75 72  to read the jour
10480 6e 61 6c 20 77 6f 75 6c 64 20 72 65 74 75 72 6e  nal would return
10490 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  .** SQLITE_IOERR
104a0 2e 20 20 4f 72 20 74 68 65 20 69 6d 70 6c 65 6d  .  Or the implem
104b0 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20 72  entation might r
104c0 65 63 6f 67 6e 69 7a 65 20 74 68 61 74 20 61 20  ecognize that a 
104d0 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65  database.** file
104e0 20 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67 20 70   will be doing p
104f0 61 67 65 2d 61 6c 69 67 6e 65 64 20 73 65 63 74  age-aligned sect
10500 6f 72 20 72 65 61 64 73 20 61 6e 64 20 77 72 69  or reads and wri
10510 74 65 73 20 69 6e 20 61 20 72 61 6e 64 6f 6d 0a  tes in a random.
10520 2a 2a 20 6f 72 64 65 72 20 61 6e 64 20 73 65 74  ** order and set
10530 20 75 70 20 69 74 73 20 49 2f 4f 20 73 75 62 73   up its I/O subs
10540 79 73 74 65 6d 20 61 63 63 6f 72 64 69 6e 67 6c  ystem accordingl
10550 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  y..**.** SQLite 
10560 6d 69 67 68 74 20 61 6c 73 6f 20 61 64 64 20 6f  might also add o
10570 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
10580 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68 65  ing flags to the
10590 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a 0a 2a   xOpen method:.*
105a0 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
105b0 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44  > [SQLITE_OPEN_D
105c0 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a  ELETEONCLOSE].**
105d0 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50   <li> [SQLITE_OP
105e0 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a 2a 2a  EN_EXCLUSIVE].**
105f0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65   </ul>.**.** The
10600 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45   [SQLITE_OPEN_DE
10610 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66 6c 61  LETEONCLOSE] fla
10620 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c 65  g means the file
10630 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 65   should be.** de
10640 6c 65 74 65 64 20 77 68 65 6e 20 69 74 20 69 73  leted when it is
10650 20 63 6c 6f 73 65 64 2e 20 20 5e 54 68 65 20 5b   closed.  ^The [
10660 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
10670 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 69  TEONCLOSE].** wi
10680 6c 6c 20 62 65 20 73 65 74 20 66 6f 72 20 54 45  ll be set for TE
10690 4d 50 20 64 61 74 61 62 61 73 65 73 20 61 6e 64  MP databases and
106a0 20 74 68 65 69 72 20 6a 6f 75 72 6e 61 6c 73 2c   their journals,
106b0 20 74 72 61 6e 73 69 65 6e 74 0a 2a 2a 20 64 61   transient.** da
106c0 74 61 62 61 73 65 73 2c 20 61 6e 64 20 73 75 62  tabases, and sub
106d0 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20  journals..**.** 
106e0 5e 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45  ^The [SQLITE_OPE
106f0 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20 66 6c 61  N_EXCLUSIVE] fla
10700 67 20 69 73 20 61 6c 77 61 79 73 20 75 73 65 64  g is always used
10710 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69 6f 6e 0a   in conjunction.
10720 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c  ** with the [SQL
10730 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d  ITE_OPEN_CREATE]
10740 20 66 6c 61 67 2c 20 77 68 69 63 68 20 61 72 65   flag, which are
10750 20 62 6f 74 68 20 64 69 72 65 63 74 6c 79 0a 2a   both directly.*
10760 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74 6f 20 74  * analogous to t
10770 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64 20 4f 5f  he O_EXCL and O_
10780 43 52 45 41 54 20 66 6c 61 67 73 20 6f 66 20 74  CREAT flags of t
10790 68 65 20 50 4f 53 49 58 20 6f 70 65 6e 28 29 0a  he POSIX open().
107a0 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 53 51 4c  ** API.  The SQL
107b0 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
107c0 56 45 20 66 6c 61 67 2c 20 77 68 65 6e 20 70 61  VE flag, when pa
107d0 69 72 65 64 20 77 69 74 68 20 74 68 65 20 0a 2a  ired with the .*
107e0 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52  * SQLITE_OPEN_CR
107f0 45 41 54 45 2c 20 69 73 20 75 73 65 64 20 74 6f  EATE, is used to
10800 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 66   indicate that f
10810 69 6c 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  ile should alway
10820 73 0a 2a 2a 20 62 65 20 63 72 65 61 74 65 64 2c  s.** be created,
10830 20 61 6e 64 20 74 68 61 74 20 69 74 20 69 73 20   and that it is 
10840 61 6e 20 65 72 72 6f 72 20 69 66 20 69 74 20 61  an error if it a
10850 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 0a 2a  lready exists..*
10860 2a 20 49 74 20 69 73 20 3c 69 3e 6e 6f 74 3c 2f  * It is <i>not</
10870 69 3e 20 75 73 65 64 20 74 6f 20 69 6e 64 69 63  i> used to indic
10880 61 74 65 20 74 68 65 20 66 69 6c 65 20 73 68 6f  ate the file sho
10890 75 6c 64 20 62 65 20 6f 70 65 6e 65 64 20 0a 2a  uld be opened .*
108a0 2a 20 66 6f 72 20 65 78 63 6c 75 73 69 76 65 20  * for exclusive 
108b0 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 5e 41  access..**.** ^A
108c0 74 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 6c 65  t least szOsFile
108d0 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
108e0 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62   are allocated b
108f0 79 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f 20 68  y SQLite.** to h
10900 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 74 65  old the  [sqlite
10910 33 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 75 72  3_file] structur
10920 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
10930 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e  third.** argumen
10940 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 65  t to xOpen.  The
10950 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64 6f   xOpen method do
10960 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a 2a  es not have to.*
10970 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73  * allocate the s
10980 74 72 75 63 74 75 72 65 3b 20 69 74 20 73 68 6f  tructure; it sho
10990 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69 74  uld just fill it
109a0 20 69 6e 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a   in.  Note that.
109b0 2a 2a 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74  ** the xOpen met
109c0 68 6f 64 20 6d 75 73 74 20 73 65 74 20 74 68 65  hod must set the
109d0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d   sqlite3_file.pM
109e0 65 74 68 6f 64 73 20 74 6f 20 65 69 74 68 65 72  ethods to either
109f0 0a 2a 2a 20 61 20 76 61 6c 69 64 20 5b 73 71 6c  .** a valid [sql
10a00 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d  ite3_io_methods]
10a10 20 6f 62 6a 65 63 74 20 6f 72 20 74 6f 20 4e 55   object or to NU
10a20 4c 4c 2e 20 20 78 4f 70 65 6e 20 6d 75 73 74 20  LL.  xOpen must 
10a30 64 6f 0a 2a 2a 20 74 68 69 73 20 65 76 65 6e 20  do.** this even 
10a40 69 66 20 74 68 65 20 6f 70 65 6e 20 66 61 69 6c  if the open fail
10a50 73 2e 20 20 53 51 4c 69 74 65 20 65 78 70 65 63  s.  SQLite expec
10a60 74 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69  ts that the sqli
10a70 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 6f 64  te3_file.pMethod
10a80 73 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77 69 6c  s.** element wil
10a90 6c 20 62 65 20 76 61 6c 69 64 20 61 66 74 65 72  l be valid after
10aa0 20 78 4f 70 65 6e 20 72 65 74 75 72 6e 73 20 72   xOpen returns r
10ab0 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65  egardless of the
10ac0 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 66   success.** or f
10ad0 61 69 6c 75 72 65 20 6f 66 20 74 68 65 20 78 4f  ailure of the xO
10ae0 70 65 6e 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  pen call..**.** 
10af0 5b 5b 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 41  [[sqlite3_vfs.xA
10b00 63 63 65 73 73 5d 5d 0a 2a 2a 20 5e 54 68 65 20  ccess]].** ^The 
10b10 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74  flags argument t
10b20 6f 20 78 41 63 63 65 73 73 28 29 20 6d 61 79 20  o xAccess() may 
10b30 62 65 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 53  be [SQLITE_ACCES
10b40 53 5f 45 58 49 53 54 53 5d 0a 2a 2a 20 74 6f 20  S_EXISTS].** to 
10b50 74 65 73 74 20 66 6f 72 20 74 68 65 20 65 78 69  test for the exi
10b60 73 74 65 6e 63 65 20 6f 66 20 61 20 66 69 6c 65  stence of a file
10b70 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43  , or [SQLITE_ACC
10b80 45 53 53 5f 52 45 41 44 57 52 49 54 45 5d 20 74  ESS_READWRITE] t
10b90 6f 0a 2a 2a 20 74 65 73 74 20 77 68 65 74 68 65  o.** test whethe
10ba0 72 20 61 20 66 69 6c 65 20 69 73 20 72 65 61 64  r a file is read
10bb0 61 62 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c  able and writabl
10bc0 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43  e, or [SQLITE_AC
10bd0 43 45 53 53 5f 52 45 41 44 5d 0a 2a 2a 20 74 6f  CESS_READ].** to
10be0 20 74 65 73 74 20 77 68 65 74 68 65 72 20 61 20   test whether a 
10bf0 66 69 6c 65 20 69 73 20 61 74 20 6c 65 61 73 74  file is at least
10c00 20 72 65 61 64 61 62 6c 65 2e 20 20 20 54 68 65   readable.   The
10c10 20 66 69 6c 65 20 63 61 6e 20 62 65 20 61 0a 2a   file can be a.*
10c20 2a 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a  * directory..**.
10c30 2a 2a 20 5e 53 51 4c 69 74 65 20 77 69 6c 6c 20  ** ^SQLite will 
10c40 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 74 65 20  always allocate 
10c50 61 74 20 6c 65 61 73 74 20 6d 78 50 61 74 68 6e  at least mxPathn
10c60 61 6d 65 2b 31 20 62 79 74 65 73 20 66 6f 72 20  ame+1 bytes for 
10c70 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75  the.** output bu
10c80 66 66 65 72 20 78 46 75 6c 6c 50 61 74 68 6e 61  ffer xFullPathna
10c90 6d 65 2e 20 20 54 68 65 20 65 78 61 63 74 20 73  me.  The exact s
10ca0 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75  ize of the outpu
10cb0 74 20 62 75 66 66 65 72 0a 2a 2a 20 69 73 20 61  t buffer.** is a
10cc0 6c 73 6f 20 70 61 73 73 65 64 20 61 73 20 61 20  lso passed as a 
10cd0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 6f 74  parameter to bot
10ce0 68 20 20 6d 65 74 68 6f 64 73 2e 20 49 66 20 74  h  methods. If t
10cf0 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72  he output buffer
10d00 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72 67 65  .** is not large
10d10 20 65 6e 6f 75 67 68 2c 20 5b 53 51 4c 49 54 45   enough, [SQLITE
10d20 5f 43 41 4e 54 4f 50 45 4e 5d 20 73 68 6f 75 6c  _CANTOPEN] shoul
10d30 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 53  d be returned. S
10d40 69 6e 63 65 20 74 68 69 73 20 69 73 0a 2a 2a 20  ince this is.** 
10d50 68 61 6e 64 6c 65 64 20 61 73 20 61 20 66 61 74  handled as a fat
10d60 61 6c 20 65 72 72 6f 72 20 62 79 20 53 51 4c 69  al error by SQLi
10d70 74 65 2c 20 76 66 73 20 69 6d 70 6c 65 6d 65 6e  te, vfs implemen
10d80 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 65  tations should e
10d90 6e 64 65 61 76 6f 72 0a 2a 2a 20 74 6f 20 70 72  ndeavor.** to pr
10da0 65 76 65 6e 74 20 74 68 69 73 20 62 79 20 73 65  event this by se
10db0 74 74 69 6e 67 20 6d 78 50 61 74 68 6e 61 6d 65  tting mxPathname
10dc0 20 74 6f 20 61 20 73 75 66 66 69 63 69 65 6e 74   to a sufficient
10dd0 6c 79 20 6c 61 72 67 65 20 76 61 6c 75 65 2e 0a  ly large value..
10de0 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f  **.** The xRando
10df0 6d 6e 65 73 73 28 29 2c 20 78 53 6c 65 65 70 28  mness(), xSleep(
10e00 29 2c 20 78 43 75 72 72 65 6e 74 54 69 6d 65 28  ), xCurrentTime(
10e10 29 2c 20 61 6e 64 20 78 43 75 72 72 65 6e 74 54  ), and xCurrentT
10e20 69 6d 65 49 6e 74 36 34 28 29 0a 2a 2a 20 69 6e  imeInt64().** in
10e30 74 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f 74  terfaces are not
10e40 20 73 74 72 69 63 74 6c 79 20 61 20 70 61 72 74   strictly a part
10e50 20 6f 66 20 74 68 65 20 66 69 6c 65 73 79 73 74   of the filesyst
10e60 65 6d 2c 20 62 75 74 20 74 68 65 79 20 61 72 65  em, but they are
10e70 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  .** included in 
10e80 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72  the VFS structur
10e90 65 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65  e for completene
10ea0 73 73 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64  ss..** The xRand
10eb0 6f 6d 6e 65 73 73 28 29 20 66 75 6e 63 74 69 6f  omness() functio
10ec0 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65  n attempts to re
10ed0 74 75 72 6e 20 6e 42 79 74 65 73 20 62 79 74 65  turn nBytes byte
10ee0 73 0a 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61  s.** of good-qua
10ef0 6c 69 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20  lity randomness 
10f00 69 6e 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20  into zOut.  The 
10f10 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a  return value is.
10f20 2a 2a 20 74 68 65 20 61 63 74 75 61 6c 20 6e 75  ** the actual nu
10f30 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
10f40 20 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61   randomness obta
10f50 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c  ined..** The xSl
10f60 65 65 70 28 29 20 6d 65 74 68 6f 64 20 63 61 75  eep() method cau
10f70 73 65 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  ses the calling 
10f80 74 68 72 65 61 64 20 74 6f 20 73 6c 65 65 70 20  thread to sleep 
10f90 66 6f 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20  for at.** least 
10fa0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69  the number of mi
10fb0 63 72 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 6e  croseconds given
10fc0 2e 20 20 5e 54 68 65 20 78 43 75 72 72 65 6e 74  .  ^The xCurrent
10fd0 54 69 6d 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64  Time().** method
10fe0 20 72 65 74 75 72 6e 73 20 61 20 4a 75 6c 69 61   returns a Julia
10ff0 6e 20 44 61 79 20 4e 75 6d 62 65 72 20 66 6f 72  n Day Number for
11000 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74   the current dat
11010 65 20 61 6e 64 20 74 69 6d 65 20 61 73 0a 2a 2a  e and time as.**
11020 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e   a floating poin
11030 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 5e 54 68 65  t value..** ^The
11040 20 78 43 75 72 72 65 6e 74 54 69 6d 65 49 6e 74   xCurrentTimeInt
11050 36 34 28 29 20 6d 65 74 68 6f 64 20 72 65 74 75  64() method retu
11060 72 6e 73 2c 20 61 73 20 61 6e 20 69 6e 74 65 67  rns, as an integ
11070 65 72 2c 20 74 68 65 20 4a 75 6c 69 61 6e 0a 2a  er, the Julian.*
11080 2a 20 44 61 79 20 4e 75 6d 62 65 72 20 6d 75 6c  * Day Number mul
11090 74 69 70 6c 69 65 64 20 62 79 20 38 36 34 30 30  tiplied by 86400
110a0 30 30 30 20 28 74 68 65 20 6e 75 6d 62 65 72 20  000 (the number 
110b0 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20  of milliseconds 
110c0 69 6e 20 0a 2a 2a 20 61 20 32 34 2d 68 6f 75 72  in .** a 24-hour
110d0 20 64 61 79 29 2e 20 20 0a 2a 2a 20 5e 53 51 4c   day).  .** ^SQL
110e0 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65  ite will use the
110f0 20 78 43 75 72 72 65 6e 74 54 69 6d 65 49 6e 74   xCurrentTimeInt
11100 36 34 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 67  64() method to g
11110 65 74 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a  et the current.*
11120 2a 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 20  * date and time 
11130 69 66 20 74 68 61 74 20 6d 65 74 68 6f 64 20 69  if that method i
11140 73 20 61 76 61 69 6c 61 62 6c 65 20 28 69 66 20  s available (if 
11150 69 56 65 72 73 69 6f 6e 20 69 73 20 32 20 6f 72  iVersion is 2 or
11160 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 61 6e 64   .** greater and
11170 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 6f   the function po
11180 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c  inter is not NUL
11190 4c 29 20 61 6e 64 20 77 69 6c 6c 20 66 61 6c 6c  L) and will fall
111a0 20 62 61 63 6b 0a 2a 2a 20 74 6f 20 78 43 75 72   back.** to xCur
111b0 72 65 6e 74 54 69 6d 65 28 29 20 69 66 20 78 43  rentTime() if xC
111c0 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28  urrentTimeInt64(
111d0 29 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65  ) is unavailable
111e0 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 78 53 65  ..**.** ^The xSe
111f0 74 53 79 73 74 65 6d 43 61 6c 6c 28 29 2c 20 78  tSystemCall(), x
11200 47 65 74 53 79 73 74 65 6d 43 61 6c 6c 28 29 2c  GetSystemCall(),
11210 20 61 6e 64 20 78 4e 65 73 74 53 79 73 74 65 6d   and xNestSystem
11220 43 61 6c 6c 28 29 20 69 6e 74 65 72 66 61 63 65  Call() interface
11230 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 75 73 65  s.** are not use
11240 64 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20  d by the SQLite 
11250 63 6f 72 65 2e 20 20 54 68 65 73 65 20 6f 70 74  core.  These opt
11260 69 6f 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 73  ional interfaces
11270 20 61 72 65 20 70 72 6f 76 69 64 65 64 0a 2a 2a   are provided.**
11280 20 62 79 20 73 6f 6d 65 20 56 46 53 65 73 20 74   by some VFSes t
11290 6f 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73  o facilitate tes
112a0 74 69 6e 67 20 6f 66 20 74 68 65 20 56 46 53 20  ting of the VFS 
112b0 63 6f 64 65 2e 20 42 79 20 6f 76 65 72 72 69 64  code. By overrid
112c0 69 6e 67 20 0a 2a 2a 20 73 79 73 74 65 6d 20 63  ing .** system c
112d0 61 6c 6c 73 20 77 69 74 68 20 66 75 6e 63 74 69  alls with functi
112e0 6f 6e 73 20 75 6e 64 65 72 20 69 74 73 20 63 6f  ons under its co
112f0 6e 74 72 6f 6c 2c 20 61 20 74 65 73 74 20 70 72  ntrol, a test pr
11300 6f 67 72 61 6d 20 63 61 6e 0a 2a 2a 20 73 69 6d  ogram can.** sim
11310 75 6c 61 74 65 20 66 61 75 6c 74 73 20 61 6e 64  ulate faults and
11320 20 65 72 72 6f 72 20 63 6f 6e 64 69 74 69 6f 6e   error condition
11330 73 20 74 68 61 74 20 77 6f 75 6c 64 20 6f 74 68  s that would oth
11340 65 72 77 69 73 65 20 62 65 20 64 69 66 66 69 63  erwise be diffic
11350 75 6c 74 0a 2a 2a 20 6f 72 20 69 6d 70 6f 73 73  ult.** or imposs
11360 69 62 6c 65 20 74 6f 20 69 6e 64 75 63 65 2e 20  ible to induce. 
11370 20 54 68 65 20 73 65 74 20 6f 66 20 73 79 73 74   The set of syst
11380 65 6d 20 63 61 6c 6c 73 20 74 68 61 74 20 63 61  em calls that ca
11390 6e 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 0a  n be overridden.
113a0 2a 2a 20 76 61 72 69 65 73 20 66 72 6f 6d 20 6f  ** varies from o
113b0 6e 65 20 56 46 53 20 74 6f 20 61 6e 6f 74 68 65  ne VFS to anothe
113c0 72 2c 20 61 6e 64 20 66 72 6f 6d 20 6f 6e 65 20  r, and from one 
113d0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 73  version of the s
113e0 61 6d 65 20 56 46 53 20 74 6f 20 74 68 65 0a 2a  ame VFS to the.*
113f0 2a 20 6e 65 78 74 2e 20 20 41 70 70 6c 69 63 61  * next.  Applica
11400 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 74  tions that use t
11410 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 20  hese interfaces 
11420 6d 75 73 74 20 62 65 20 70 72 65 70 61 72 65 64  must be prepared
11430 20 66 6f 72 20 61 6e 79 0a 2a 2a 20 6f 72 20 61   for any.** or a
11440 6c 6c 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65  ll of these inte
11450 72 66 61 63 65 73 20 74 6f 20 62 65 20 4e 55 4c  rfaces to be NUL
11460 4c 20 6f 72 20 66 6f 72 20 74 68 65 69 72 20 62  L or for their b
11470 65 68 61 76 69 6f 72 20 74 6f 20 63 68 61 6e 67  ehavior to chang
11480 65 0a 2a 2a 20 66 72 6f 6d 20 6f 6e 65 20 72 65  e.** from one re
11490 6c 65 61 73 65 20 74 6f 20 74 68 65 20 6e 65 78  lease to the nex
114a0 74 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  t.  Applications
114b0 20 6d 75 73 74 20 6e 6f 74 20 61 74 74 65 6d 70   must not attemp
114c0 74 20 74 6f 20 61 63 63 65 73 73 0a 2a 2a 20 61  t to access.** a
114d0 6e 79 20 6f 66 20 74 68 65 73 65 20 6d 65 74 68  ny of these meth
114e0 6f 64 73 20 69 66 20 74 68 65 20 69 56 65 72 73  ods if the iVers
114f0 69 6f 6e 20 6f 66 20 74 68 65 20 56 46 53 20 69  ion of the VFS i
11500 73 20 6c 65 73 73 20 74 68 61 6e 20 33 2e 0a 2a  s less than 3..*
11510 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
11520 20 73 71 6c 69 74 65 33 5f 76 66 73 20 73 71 6c   sqlite3_vfs sql
11530 69 74 65 33 5f 76 66 73 3b 0a 74 79 70 65 64 65  ite3_vfs;.typede
11540 66 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33  f void (*sqlite3
11550 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 28 76 6f  _syscall_ptr)(vo
11560 69 64 29 3b 0a 73 74 72 75 63 74 20 73 71 6c 69  id);.struct sqli
11570 74 65 33 5f 76 66 73 20 7b 0a 20 20 69 6e 74 20  te3_vfs {.  int 
11580 69 56 65 72 73 69 6f 6e 3b 20 20 20 20 20 20 20  iVersion;       
11590 20 20 20 20 20 2f 2a 20 53 74 72 75 63 74 75 72       /* Structur
115a0 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72  e version number
115b0 20 28 63 75 72 72 65 6e 74 6c 79 20 33 29 20 2a   (currently 3) *
115c0 2f 0a 20 20 69 6e 74 20 73 7a 4f 73 46 69 6c 65  /.  int szOsFile
115d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
115e0 53 69 7a 65 20 6f 66 20 73 75 62 63 6c 61 73 73  Size of subclass
115f0 65 64 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ed sqlite3_file 
11600 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 74 68 6e  */.  int mxPathn
11610 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ame;          /*
11620 20 4d 61 78 69 6d 75 6d 20 66 69 6c 65 20 70 61   Maximum file pa
11630 74 68 6e 61 6d 65 20 6c 65 6e 67 74 68 20 2a 2f  thname length */
11640 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
11650 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e  pNext;      /* N
11660 65 78 74 20 72 65 67 69 73 74 65 72 65 64 20 56  ext registered V
11670 46 53 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  FS */.  const ch
11680 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
11690 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73   /* Name of this
116a0 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79   virtual file sy
116b0 73 74 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  stem */.  void *
116c0 70 41 70 70 44 61 74 61 3b 20 20 20 20 20 20 20  pAppData;       
116d0 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
116e0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 70 65   application-spe
116f0 63 69 66 69 63 20 64 61 74 61 20 2a 2f 0a 20 20  cific data */.  
11700 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c  int (*xOpen)(sql
11710 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74  ite3_vfs*, const
11720 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 73 71   char *zName, sq
11730 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 20 20  lite3_file*,.   
11740 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
11750 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70 4f 75 74  flags, int *pOut
11760 46 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a  Flags);.  int (*
11770 78 44 65 6c 65 74 65 29 28 73 71 6c 69 74 65 33  xDelete)(sqlite3
11780 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61  _vfs*, const cha
11790 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 73 79  r *zName, int sy
117a0 6e 63 44 69 72 29 3b 0a 20 20 69 6e 74 20 28 2a  ncDir);.  int (*
117b0 78 41 63 63 65 73 73 29 28 73 71 6c 69 74 65 33  xAccess)(sqlite3
117c0 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61  _vfs*, const cha
117d0 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 66 6c  r *zName, int fl
117e0 61 67 73 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  ags, int *pResOu
117f0 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6c  t);.  int (*xFul
11800 6c 50 61 74 68 6e 61 6d 65 29 28 73 71 6c 69 74  lPathname)(sqlit
11810 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63  e3_vfs*, const c
11820 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20  har *zName, int 
11830 6e 4f 75 74 2c 20 63 68 61 72 20 2a 7a 4f 75 74  nOut, char *zOut
11840 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c  );.  void *(*xDl
11850 4f 70 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 66  Open)(sqlite3_vf
11860 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  s*, const char *
11870 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 76 6f  zFilename);.  vo
11880 69 64 20 28 2a 78 44 6c 45 72 72 6f 72 29 28 73  id (*xDlError)(s
11890 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74  qlite3_vfs*, int
118a0 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 45   nByte, char *zE
118b0 72 72 4d 73 67 29 3b 0a 20 20 76 6f 69 64 20 28  rrMsg);.  void (
118c0 2a 28 2a 78 44 6c 53 79 6d 29 28 73 71 6c 69 74  *(*xDlSym)(sqlit
118d0 65 33 5f 76 66 73 2a 2c 76 6f 69 64 2a 2c 20 63  e3_vfs*,void*, c
118e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62  onst char *zSymb
118f0 6f 6c 29 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f  ol))(void);.  vo
11900 69 64 20 28 2a 78 44 6c 43 6c 6f 73 65 29 28 73  id (*xDlClose)(s
11910 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 76 6f 69  qlite3_vfs*, voi
11920 64 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 61  d*);.  int (*xRa
11930 6e 64 6f 6d 6e 65 73 73 29 28 73 71 6c 69 74 65  ndomness)(sqlite
11940 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e 42 79 74  3_vfs*, int nByt
11950 65 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b 0a  e, char *zOut);.
11960 20 20 69 6e 74 20 28 2a 78 53 6c 65 65 70 29 28    int (*xSleep)(
11970 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e  sqlite3_vfs*, in
11980 74 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b  t microseconds);
11990 0a 20 20 69 6e 74 20 28 2a 78 43 75 72 72 65 6e  .  int (*xCurren
119a0 74 54 69 6d 65 29 28 73 71 6c 69 74 65 33 5f 76  tTime)(sqlite3_v
119b0 66 73 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 20  fs*, double*);. 
119c0 20 69 6e 74 20 28 2a 78 47 65 74 4c 61 73 74 45   int (*xGetLastE
119d0 72 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66  rror)(sqlite3_vf
119e0 73 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29  s*, int, char *)
119f0 3b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20  ;.  /*.  ** The 
11a00 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 20 61 72  methods above ar
11a10 65 20 69 6e 20 76 65 72 73 69 6f 6e 20 31 20 6f  e in version 1 o
11a20 66 20 74 68 65 20 73 71 6c 69 74 65 5f 76 66 73  f the sqlite_vfs
11a30 20 6f 62 6a 65 63 74 0a 20 20 2a 2a 20 64 65 66   object.  ** def
11a40 69 6e 69 74 69 6f 6e 2e 20 20 54 68 6f 73 65 20  inition.  Those 
11a50 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61 72 65 20  that follow are 
11a60 61 64 64 65 64 20 69 6e 20 76 65 72 73 69 6f 6e  added in version
11a70 20 32 20 6f 72 20 6c 61 74 65 72 0a 20 20 2a 2f   2 or later.  */
11a80 0a 20 20 69 6e 74 20 28 2a 78 43 75 72 72 65 6e  .  int (*xCurren
11a90 74 54 69 6d 65 49 6e 74 36 34 29 28 73 71 6c 69  tTimeInt64)(sqli
11aa0 74 65 33 5f 76 66 73 2a 2c 20 73 71 6c 69 74 65  te3_vfs*, sqlite
11ab0 33 5f 69 6e 74 36 34 2a 29 3b 0a 20 20 2f 2a 0a  3_int64*);.  /*.
11ac0 20 20 2a 2a 20 54 68 65 20 6d 65 74 68 6f 64 73    ** The methods
11ad0 20 61 62 6f 76 65 20 61 72 65 20 69 6e 20 76 65   above are in ve
11ae0 72 73 69 6f 6e 73 20 31 20 61 6e 64 20 32 20 6f  rsions 1 and 2 o
11af0 66 20 74 68 65 20 73 71 6c 69 74 65 5f 76 66 73  f the sqlite_vfs
11b00 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a 20 54 68   object..  ** Th
11b10 6f 73 65 20 62 65 6c 6f 77 20 61 72 65 20 66 6f  ose below are fo
11b20 72 20 76 65 72 73 69 6f 6e 20 33 20 61 6e 64 20  r version 3 and 
11b30 67 72 65 61 74 65 72 2e 0a 20 20 2a 2f 0a 20 20  greater..  */.  
11b40 69 6e 74 20 28 2a 78 53 65 74 53 79 73 74 65 6d  int (*xSetSystem
11b50 43 61 6c 6c 29 28 73 71 6c 69 74 65 33 5f 76 66  Call)(sqlite3_vf
11b60 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  s*, const char *
11b70 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 5f 73  zName, sqlite3_s
11b80 79 73 63 61 6c 6c 5f 70 74 72 29 3b 0a 20 20 73  yscall_ptr);.  s
11b90 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70  qlite3_syscall_p
11ba0 74 72 20 28 2a 78 47 65 74 53 79 73 74 65 6d 43  tr (*xGetSystemC
11bb0 61 6c 6c 29 28 73 71 6c 69 74 65 33 5f 76 66 73  all)(sqlite3_vfs
11bc0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
11bd0 4e 61 6d 65 29 3b 0a 20 20 63 6f 6e 73 74 20 63  Name);.  const c
11be0 68 61 72 20 2a 28 2a 78 4e 65 78 74 53 79 73 74  har *(*xNextSyst
11bf0 65 6d 43 61 6c 6c 29 28 73 71 6c 69 74 65 33 5f  emCall)(sqlite3_
11c00 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  vfs*, const char
11c10 20 2a 7a 4e 61 6d 65 29 3b 0a 20 20 2f 2a 0a 20   *zName);.  /*. 
11c20 20 2a 2a 20 54 68 65 20 6d 65 74 68 6f 64 73 20   ** The methods 
11c30 61 62 6f 76 65 20 61 72 65 20 69 6e 20 76 65 72  above are in ver
11c40 73 69 6f 6e 73 20 31 20 74 68 72 6f 75 67 68 20  sions 1 through 
11c50 33 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f  3 of the sqlite_
11c60 76 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a  vfs object..  **
11c70 20 4e 65 77 20 66 69 65 6c 64 73 20 6d 61 79 20   New fields may 
11c80 62 65 20 61 70 70 65 6e 64 65 64 20 69 6e 20 66  be appended in f
11c90 69 67 75 72 65 20 76 65 72 73 69 6f 6e 73 2e 20  igure versions. 
11ca0 20 54 68 65 20 69 56 65 72 73 69 6f 6e 0a 20 20   The iVersion.  
11cb0 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c 20 69 6e  ** value will in
11cc0 63 72 65 6d 65 6e 74 20 77 68 65 6e 65 76 65 72  crement whenever
11cd0 20 74 68 69 73 20 68 61 70 70 65 6e 73 2e 20 0a   this happens. .
11ce0 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43    */.};../*.** C
11cf0 41 50 49 33 52 45 46 3a 20 46 6c 61 67 73 20 66  API3REF: Flags f
11d00 6f 72 20 74 68 65 20 78 41 63 63 65 73 73 20 56  or the xAccess V
11d10 46 53 20 6d 65 74 68 6f 64 0a 2a 2a 0a 2a 2a 20  FS method.**.** 
11d20 54 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f  These integer co
11d30 6e 73 74 61 6e 74 73 20 63 61 6e 20 62 65 20 75  nstants can be u
11d40 73 65 64 20 61 73 20 74 68 65 20 74 68 69 72 64  sed as the third
11d50 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a   parameter to.**
11d60 20 74 68 65 20 78 41 63 63 65 73 73 20 6d 65 74   the xAccess met
11d70 68 6f 64 20 6f 66 20 61 6e 20 5b 73 71 6c 69 74  hod of an [sqlit
11d80 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 20  e3_vfs] object. 
11d90 20 54 68 65 79 20 64 65 74 65 72 6d 69 6e 65 0a   They determine.
11da0 2a 2a 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20  ** what kind of 
11db0 70 65 72 6d 69 73 73 69 6f 6e 73 20 74 68 65 20  permissions the 
11dc0 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 69  xAccess method i
11dd0 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a 2a  s looking for..*
11de0 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f 41 43  * With SQLITE_AC
11df0 43 45 53 53 5f 45 58 49 53 54 53 2c 20 74 68 65  CESS_EXISTS, the
11e00 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a   xAccess method.
11e10 2a 2a 20 73 69 6d 70 6c 79 20 63 68 65 63 6b 73  ** simply checks
11e20 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c   whether the fil
11e30 65 20 65 78 69 73 74 73 2e 0a 2a 2a 20 57 69 74  e exists..** Wit
11e40 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  h SQLITE_ACCESS_
11e50 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 20 78  READWRITE, the x
11e60 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a  Access method.**
11e70 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20   checks whether 
11e80 74 68 65 20 6e 61 6d 65 64 20 64 69 72 65 63 74  the named direct
11e90 6f 72 79 20 69 73 20 62 6f 74 68 20 72 65 61 64  ory is both read
11ea0 61 62 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c  able and writabl
11eb0 65 0a 2a 2a 20 28 69 6e 20 6f 74 68 65 72 20 77  e.** (in other w
11ec0 6f 72 64 73 2c 20 69 66 20 66 69 6c 65 73 20 63  ords, if files c
11ed0 61 6e 20 62 65 20 61 64 64 65 64 2c 20 72 65 6d  an be added, rem
11ee0 6f 76 65 64 2c 20 61 6e 64 20 72 65 6e 61 6d 65  oved, and rename
11ef0 64 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20  d within.** the 
11f00 64 69 72 65 63 74 6f 72 79 29 2e 0a 2a 2a 20 54  directory)..** T
11f10 68 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  he SQLITE_ACCESS
11f20 5f 52 45 41 44 57 52 49 54 45 20 63 6f 6e 73 74  _READWRITE const
11f30 61 6e 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ant is currently
11f40 20 75 73 65 64 20 6f 6e 6c 79 20 62 79 20 74 68   used only by th
11f50 65 0a 2a 2a 20 5b 74 65 6d 70 5f 73 74 6f 72 65  e.** [temp_store
11f60 5f 64 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d  _directory pragm
11f70 61 5d 2c 20 74 68 6f 75 67 68 20 74 68 69 73 20  a], though this 
11f80 63 6f 75 6c 64 20 63 68 61 6e 67 65 20 69 6e 20  could change in 
11f90 61 20 66 75 74 75 72 65 0a 2a 2a 20 72 65 6c 65  a future.** rele
11fa0 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  ase of SQLite..*
11fb0 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f 41 43  * With SQLITE_AC
11fc0 43 45 53 53 5f 52 45 41 44 2c 20 74 68 65 20 78  CESS_READ, the x
11fd0 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a  Access method.**
11fe0 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20   checks whether 
11ff0 74 68 65 20 66 69 6c 65 20 69 73 20 72 65 61 64  the file is read
12000 61 62 6c 65 2e 20 20 54 68 65 20 53 51 4c 49 54  able.  The SQLIT
12010 45 5f 41 43 43 45 53 53 5f 52 45 41 44 20 63 6f  E_ACCESS_READ co
12020 6e 73 74 61 6e 74 20 69 73 0a 2a 2a 20 63 75 72  nstant is.** cur
12030 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 2c 20 74  rently unused, t
12040 68 6f 75 67 68 20 69 74 20 6d 69 67 68 74 20 62  hough it might b
12050 65 20 75 73 65 64 20 69 6e 20 61 20 66 75 74 75  e used in a futu
12060 72 65 20 72 65 6c 65 61 73 65 20 6f 66 0a 2a 2a  re release of.**
12070 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65 66   SQLite..*/.#def
12080 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ine SQLITE_ACCES
12090 53 5f 45 58 49 53 54 53 20 20 20 20 30 0a 23 64  S_EXISTS    0.#d
120a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43  efine SQLITE_ACC
120b0 45 53 53 5f 52 45 41 44 57 52 49 54 45 20 31 20  ESS_READWRITE 1 
120c0 20 20 2f 2a 20 55 73 65 64 20 62 79 20 50 52 41    /* Used by PRA
120d0 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64  GMA temp_store_d
120e0 69 72 65 63 74 6f 72 79 20 2a 2f 0a 23 64 65 66  irectory */.#def
120f0 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ine SQLITE_ACCES
12100 53 5f 52 45 41 44 20 20 20 20 20 20 32 20 20 20  S_READ      2   
12110 2f 2a 20 55 6e 75 73 65 64 20 2a 2f 0a 0a 2f 2a  /* Unused */../*
12120 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6c  .** CAPI3REF: Fl
12130 61 67 73 20 66 6f 72 20 74 68 65 20 78 53 68 6d  ags for the xShm
12140 4c 6f 63 6b 20 56 46 53 20 6d 65 74 68 6f 64 0a  Lock VFS method.
12150 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65  **.** These inte
12160 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 64 65  ger constants de
12170 66 69 6e 65 20 74 68 65 20 76 61 72 69 6f 75 73  fine the various
12180 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69   locking operati
12190 6f 6e 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 62  ons.** allowed b
121a0 79 20 74 68 65 20 78 53 68 6d 4c 6f 63 6b 20 6d  y the xShmLock m
121b0 65 74 68 6f 64 20 6f 66 20 5b 73 71 6c 69 74 65  ethod of [sqlite
121c0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 2e 20 20  3_io_methods].  
121d0 54 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67  The.** following
121e0 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20 6c 65   are the only le
121f0 67 61 6c 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73  gal combinations
12200 20 6f 66 20 66 6c 61 67 73 20 74 6f 20 74 68 65   of flags to the
12210 0a 2a 2a 20 78 53 68 6d 4c 6f 63 6b 20 6d 65 74  .** xShmLock met
12220 68 6f 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a  hod:.**.** <ul>.
12230 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
12240 53 48 4d 5f 4c 4f 43 4b 20 7c 20 53 51 4c 49 54  SHM_LOCK | SQLIT
12250 45 5f 53 48 4d 5f 53 48 41 52 45 44 0a 2a 2a 20  E_SHM_SHARED.** 
12260 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 53 48 4d  <li>  SQLITE_SHM
12270 5f 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53  _LOCK | SQLITE_S
12280 48 4d 5f 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  HM_EXCLUSIVE.** 
12290 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 53 48 4d  <li>  SQLITE_SHM
122a0 5f 55 4e 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45  _UNLOCK | SQLITE
122b0 5f 53 48 4d 5f 53 48 41 52 45 44 0a 2a 2a 20 3c  _SHM_SHARED.** <
122c0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 53 48 4d 5f  li>  SQLITE_SHM_
122d0 55 4e 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f  UNLOCK | SQLITE_
122e0 53 48 4d 5f 45 58 43 4c 55 53 49 56 45 0a 2a 2a  SHM_EXCLUSIVE.**
122f0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 57 68 65   </ul>.**.** Whe
12300 6e 20 75 6e 6c 6f 63 6b 69 6e 67 2c 20 74 68 65  n unlocking, the
12310 20 73 61 6d 65 20 53 48 41 52 45 44 20 6f 72 20   same SHARED or 
12320 45 58 43 4c 55 53 49 56 45 20 66 6c 61 67 20 6d  EXCLUSIVE flag m
12330 75 73 74 20 62 65 20 73 75 70 70 6c 69 65 64 20  ust be supplied 
12340 61 73 0a 2a 2a 20 77 61 73 20 67 69 76 65 6e 20  as.** was given 
12350 6e 6f 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  no the correspon
12360 64 69 6e 67 20 6c 6f 63 6b 2e 20 20 0a 2a 2a 0a  ding lock.  .**.
12370 2a 2a 20 54 68 65 20 78 53 68 6d 4c 6f 63 6b 20  ** The xShmLock 
12380 6d 65 74 68 6f 64 20 63 61 6e 20 74 72 61 6e 73  method can trans
12390 69 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 75 6e  ition between un
123a0 6c 6f 63 6b 65 64 20 61 6e 64 20 53 48 41 52 45  locked and SHARE
123b0 44 20 6f 72 0a 2a 2a 20 62 65 74 77 65 65 6e 20  D or.** between 
123c0 75 6e 6c 6f 63 6b 65 64 20 61 6e 64 20 45 58 43  unlocked and EXC
123d0 4c 55 53 49 56 45 2e 20 20 49 74 20 63 61 6e 6e  LUSIVE.  It cann
123e0 6f 74 20 74 72 61 6e 73 69 74 69 6f 6e 20 62 65  ot transition be
123f0 74 77 65 65 6e 20 53 48 41 52 45 44 0a 2a 2a 20  tween SHARED.** 
12400 61 6e 64 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a  and EXCLUSIVE..*
12410 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
12420 5f 53 48 4d 5f 55 4e 4c 4f 43 4b 20 20 20 20 20  _SHM_UNLOCK     
12430 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    1.#define SQLI
12440 54 45 5f 53 48 4d 5f 4c 4f 43 4b 20 20 20 20 20  TE_SHM_LOCK     
12450 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51      2.#define SQ
12460 4c 49 54 45 5f 53 48 4d 5f 53 48 41 52 45 44 20  LITE_SHM_SHARED 
12470 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20        4.#define 
12480 53 51 4c 49 54 45 5f 53 48 4d 5f 45 58 43 4c 55  SQLITE_SHM_EXCLU
12490 53 49 56 45 20 20 20 20 38 0a 0a 2f 2a 0a 2a 2a  SIVE    8../*.**
124a0 20 43 41 50 49 33 52 45 46 3a 20 4d 61 78 69 6d   CAPI3REF: Maxim
124b0 75 6d 20 78 53 68 6d 4c 6f 63 6b 20 69 6e 64 65  um xShmLock inde
124c0 78 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 68 6d  x.**.** The xShm
124d0 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 6f 6e 20 5b  Lock method on [
124e0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
124f0 64 73 5d 20 6d 61 79 20 75 73 65 20 76 61 6c 75  ds] may use valu
12500 65 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 30 20  es.** between 0 
12510 61 6e 64 20 74 68 69 73 20 75 70 70 65 72 20 62  and this upper b
12520 6f 75 6e 64 20 61 73 20 69 74 73 20 22 6f 66 66  ound as its "off
12530 73 65 74 22 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  set" argument..*
12540 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72  * The SQLite cor
12550 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 61 74 74  e will never att
12560 65 6d 70 74 20 74 6f 20 61 63 71 75 69 72 65 20  empt to acquire 
12570 6f 72 20 72 65 6c 65 61 73 65 20 61 0a 2a 2a 20  or release a.** 
12580 6c 6f 63 6b 20 6f 75 74 73 69 64 65 20 6f 66 20  lock outside of 
12590 74 68 69 73 20 72 61 6e 67 65 0a 2a 2f 0a 23 64  this range.*/.#d
125a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 48 4d  efine SQLITE_SHM
125b0 5f 4e 4c 4f 43 4b 20 20 20 20 20 20 20 20 38 0a  _NLOCK        8.
125c0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
125d0 3a 20 49 6e 69 74 69 61 6c 69 7a 65 20 54 68 65  : Initialize The
125e0 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79 0a   SQLite Library.
125f0 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74  **.** ^The sqlit
12600 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
12610 72 6f 75 74 69 6e 65 20 69 6e 69 74 69 61 6c 69  routine initiali
12620 7a 65 73 20 74 68 65 0a 2a 2a 20 53 51 4c 69 74  zes the.** SQLit
12630 65 20 6c 69 62 72 61 72 79 2e 20 20 5e 54 68 65  e library.  ^The
12640 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
12650 6e 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64  n() routine.** d
12660 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 72  eallocates any r
12670 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 77 65  esources that we
12680 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  re allocated by 
12690 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
126a0 7a 65 28 29 2e 0a 2a 2a 20 54 68 65 73 65 20 72  ze()..** These r
126b0 6f 75 74 69 6e 65 73 20 61 72 65 20 64 65 73 69  outines are desi
126c0 67 6e 65 64 20 74 6f 20 61 69 64 20 69 6e 20 70  gned to aid in p
126d0 72 6f 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a  rocess initializ
126e0 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 68 75  ation and.** shu
126f0 74 64 6f 77 6e 20 6f 6e 20 65 6d 62 65 64 64 65  tdown on embedde
12700 64 20 73 79 73 74 65 6d 73 2e 20 20 57 6f 72 6b  d systems.  Work
12710 73 74 61 74 69 6f 6e 20 61 70 70 6c 69 63 61 74  station applicat
12720 69 6f 6e 73 20 75 73 69 6e 67 0a 2a 2a 20 53 51  ions using.** SQ
12730 4c 69 74 65 20 6e 6f 72 6d 61 6c 6c 79 20 64 6f  Lite normally do
12740 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 69 6e 76   not need to inv
12750 6f 6b 65 20 65 69 74 68 65 72 20 6f 66 20 74 68  oke either of th
12760 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  ese routines..**
12770 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71  .** A call to sq
12780 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
12790 28 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74  () is an "effect
127a0 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20  ive" call if it 
127b0 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  is.** the first 
127c0 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69  time sqlite3_ini
127d0 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76  tialize() is inv
127e0 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65 20  oked during the 
127f0 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74  lifetime of.** t
12800 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69  he process, or i
12810 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73  f it is the firs
12820 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69  t time sqlite3_i
12830 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69  nitialize() is i
12840 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77  nvoked.** follow
12850 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ing a call to sq
12860 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
12870 2e 20 20 5e 28 4f 6e 6c 79 20 61 6e 20 65 66 66  .  ^(Only an eff
12880 65 63 74 69 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f  ective call.** o
12890 66 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  f sqlite3_initia
128a0 6c 69 7a 65 28 29 20 64 6f 65 73 20 61 6e 79 20  lize() does any 
128b0 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20  initialization. 
128c0 20 41 6c 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73   All other calls
128d0 0a 2a 2a 20 61 72 65 20 68 61 72 6d 6c 65 73 73  .** are harmless
128e0 20 6e 6f 2d 6f 70 73 2e 29 5e 0a 2a 2a 0a 2a 2a   no-ops.)^.**.**
128f0 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   A call to sqlit
12900 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 69 73  e3_shutdown() is
12910 20 61 6e 20 22 65 66 66 65 63 74 69 76 65 22 20   an "effective" 
12920 63 61 6c 6c 20 69 66 20 69 74 20 69 73 20 74 68  call if it is th
12930 65 20 66 69 72 73 74 0a 2a 2a 20 63 61 6c 6c 20  e first.** call 
12940 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  to sqlite3_shutd
12950 6f 77 6e 28 29 20 73 69 6e 63 65 20 74 68 65 20  own() since the 
12960 6c 61 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 69  last sqlite3_ini
12970 74 69 61 6c 69 7a 65 28 29 2e 20 20 5e 28 4f 6e  tialize().  ^(On
12980 6c 79 0a 2a 2a 20 61 6e 20 65 66 66 65 63 74 69  ly.** an effecti
12990 76 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  ve call to sqlit
129a0 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 64 6f  e3_shutdown() do
129b0 65 73 20 61 6e 79 20 64 65 69 6e 69 74 69 61 6c  es any deinitial
129c0 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 20 41 6c 6c 20  ization..** All 
129d0 6f 74 68 65 72 20 76 61 6c 69 64 20 63 61 6c 6c  other valid call
129e0 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75  s to sqlite3_shu
129f0 74 64 6f 77 6e 28 29 20 61 72 65 20 68 61 72 6d  tdown() are harm
12a00 6c 65 73 73 20 6e 6f 2d 6f 70 73 2e 29 5e 0a 2a  less no-ops.)^.*
12a10 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
12a20 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 6e  _initialize() in
12a30 74 65 72 66 61 63 65 20 69 73 20 74 68 72 65 61  terface is threa
12a40 64 73 61 66 65 2c 20 62 75 74 20 73 71 6c 69 74  dsafe, but sqlit
12a50 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a 2a 2a  e3_shutdown().**
12a60 20 69 73 20 6e 6f 74 2e 20 20 54 68 65 20 73 71   is not.  The sq
12a70 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
12a80 20 69 6e 74 65 72 66 61 63 65 20 6d 75 73 74 20   interface must 
12a90 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 66  only be called f
12aa0 72 6f 6d 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20  rom a.** single 
12ab0 74 68 72 65 61 64 2e 20 20 41 6c 6c 20 6f 70 65  thread.  All ope
12ac0 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  n [database conn
12ad0 65 63 74 69 6f 6e 73 5d 20 6d 75 73 74 20 62 65  ections] must be
12ae0 20 63 6c 6f 73 65 64 20 61 6e 64 20 61 6c 6c 0a   closed and all.
12af0 2a 2a 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20  ** other SQLite 
12b00 72 65 73 6f 75 72 63 65 73 20 6d 75 73 74 20 62  resources must b
12b10 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 70 72  e deallocated pr
12b20 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 0a  ior to invoking.
12b30 2a 2a 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  ** sqlite3_shutd
12b40 6f 77 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f  own()..**.** Amo
12b50 6e 67 20 6f 74 68 65 72 20 74 68 69 6e 67 73 2c  ng other things,
12b60 20 5e 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61   ^sqlite3_initia
12b70 6c 69 7a 65 28 29 20 77 69 6c 6c 20 69 6e 76 6f  lize() will invo
12b80 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73  ke.** sqlite3_os
12b90 5f 69 6e 69 74 28 29 2e 20 20 53 69 6d 69 6c 61  _init().  Simila
12ba0 72 6c 79 2c 20 5e 73 71 6c 69 74 65 33 5f 73 68  rly, ^sqlite3_sh
12bb0 75 74 64 6f 77 6e 28 29 0a 2a 2a 20 77 69 6c 6c  utdown().** will
12bc0 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   invoke sqlite3_
12bd0 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20  os_end()..**.** 
12be0 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69  ^The sqlite3_ini
12bf0 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e  tialize() routin
12c00 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  e returns [SQLIT
12c10 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73  E_OK] on success
12c20 2e 0a 2a 2a 20 5e 49 66 20 66 6f 72 20 73 6f 6d  ..** ^If for som
12c30 65 20 72 65 61 73 6f 6e 2c 20 73 71 6c 69 74 65  e reason, sqlite
12c40 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69  3_initialize() i
12c50 73 20 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74  s unable to init
12c60 69 61 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 6c 69  ialize.** the li
12c70 62 72 61 72 79 20 28 70 65 72 68 61 70 73 20 69  brary (perhaps i
12c80 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61  t is unable to a
12c90 6c 6c 6f 63 61 74 65 20 61 20 6e 65 65 64 65 64  llocate a needed
12ca0 20 72 65 73 6f 75 72 63 65 20 73 75 63 68 0a 2a   resource such.*
12cb0 2a 20 61 73 20 61 20 6d 75 74 65 78 29 20 69 74  * as a mutex) it
12cc0 20 72 65 74 75 72 6e 73 20 61 6e 20 5b 65 72 72   returns an [err
12cd0 6f 72 20 63 6f 64 65 5d 20 6f 74 68 65 72 20 74  or code] other t
12ce0 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e  han [SQLITE_OK].
12cf0 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69  .**.** ^The sqli
12d00 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
12d10 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
12d20 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79  ed internally by
12d30 20 6d 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 53   many other.** S
12d40 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73  QLite interfaces
12d50 20 73 6f 20 74 68 61 74 20 61 6e 20 61 70 70 6c   so that an appl
12d60 69 63 61 74 69 6f 6e 20 75 73 75 61 6c 6c 79 20  ication usually 
12d70 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
12d80 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74  .** invoke sqlit
12d90 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
12da0 64 69 72 65 63 74 6c 79 2e 20 20 46 6f 72 20 65  directly.  For e
12db0 78 61 6d 70 6c 65 2c 20 5b 73 71 6c 69 74 65 33  xample, [sqlite3
12dc0 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c 6c  _open()].** call
12dd0 73 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  s sqlite3_initia
12de0 6c 69 7a 65 28 29 20 73 6f 20 74 68 65 20 53 51  lize() so the SQ
12df0 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c  Lite library wil
12e00 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  l be automatical
12e10 6c 79 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65  ly.** initialize
12e20 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f  d when [sqlite3_
12e30 6f 70 65 6e 28 29 5d 20 69 73 20 63 61 6c 6c 65  open()] is calle
12e40 64 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20  d if it has not 
12e50 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a  be initialized.*
12e60 2a 20 61 6c 72 65 61 64 79 2e 20 20 5e 48 6f 77  * already.  ^How
12e70 65 76 65 72 2c 20 69 66 20 53 51 4c 69 74 65 20  ever, if SQLite 
12e80 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  is compiled with
12e90 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 4d 49   the [SQLITE_OMI
12ea0 54 5f 41 55 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63  T_AUTOINIT].** c
12eb0 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69  ompile-time opti
12ec0 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 61 75 74  on, then the aut
12ed0 6f 6d 61 74 69 63 20 63 61 6c 6c 73 20 74 6f 20  omatic calls to 
12ee0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
12ef0 7a 65 28 29 0a 2a 2a 20 61 72 65 20 6f 6d 69 74  ze().** are omit
12f00 74 65 64 20 61 6e 64 20 74 68 65 20 61 70 70 6c  ted and the appl
12f10 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 63 61 6c  ication must cal
12f20 6c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  l sqlite3_initia
12f30 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c 79 0a  lize() directly.
12f40 2a 2a 20 70 72 69 6f 72 20 74 6f 20 75 73 69 6e  ** prior to usin
12f50 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51 4c 69  g any other SQLi
12f60 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20 20 46  te interface.  F
12f70 6f 72 20 6d 61 78 69 6d 75 6d 20 70 6f 72 74 61  or maximum porta
12f80 62 69 6c 69 74 79 2c 0a 2a 2a 20 69 74 20 69 73  bility,.** it is
12f90 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61   recommended tha
12fa0 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61  t applications a
12fb0 6c 77 61 79 73 20 69 6e 76 6f 6b 65 20 73 71 6c  lways invoke sql
12fc0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
12fd0 29 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 70 72  ).** directly pr
12fe0 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 61 6e 79  ior to using any
12ff0 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69 6e   other SQLite in
13000 74 65 72 66 61 63 65 2e 20 20 46 75 74 75 72 65  terface.  Future
13010 20 72 65 6c 65 61 73 65 73 0a 2a 2a 20 6f 66 20   releases.** of 
13020 53 51 4c 69 74 65 20 6d 61 79 20 72 65 71 75 69  SQLite may requi
13030 72 65 20 74 68 69 73 2e 20 20 49 6e 20 6f 74 68  re this.  In oth
13040 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 62 65  er words, the be
13050 68 61 76 69 6f 72 20 65 78 68 69 62 69 74 65 64  havior exhibited
13060 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20  .** when SQLite 
13070 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  is compiled with
13080 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55   [SQLITE_OMIT_AU
13090 54 4f 49 4e 49 54 5d 20 6d 69 67 68 74 20 62 65  TOINIT] might be
130a0 63 6f 6d 65 20 74 68 65 0a 2a 2a 20 64 65 66 61  come the.** defa
130b0 75 6c 74 20 62 65 68 61 76 69 6f 72 20 69 6e 20  ult behavior in 
130c0 73 6f 6d 65 20 66 75 74 75 72 65 20 72 65 6c 65  some future rele
130d0 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  ase of SQLite..*
130e0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
130f0 5f 6f 73 5f 69 6e 69 74 28 29 20 72 6f 75 74 69  _os_init() routi
13100 6e 65 20 64 6f 65 73 20 6f 70 65 72 61 74 69 6e  ne does operatin
13110 67 2d 73 79 73 74 65 6d 20 73 70 65 63 69 66 69  g-system specifi
13120 63 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74  c.** initializat
13130 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c 69 74  ion of the SQLit
13140 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20  e library.  The 
13150 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29  sqlite3_os_end()
13160 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 75 6e 64 6f  .** routine undo
13170 65 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66  es the effect of
13180 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
13190 28 29 2e 20 20 54 79 70 69 63 61 6c 20 74 61 73  ().  Typical tas
131a0 6b 73 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20  ks.** performed 
131b0 62 79 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  by these routine
131c0 73 20 69 6e 63 6c 75 64 65 20 61 6c 6c 6f 63 61  s include alloca
131d0 74 69 6f 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61  tion or dealloca
131e0 74 69 6f 6e 0a 2a 2a 20 6f 66 20 73 74 61 74 69  tion.** of stati
131f0 63 20 72 65 73 6f 75 72 63 65 73 2c 20 69 6e 69  c resources, ini
13200 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 67  tialization of g
13210 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2c  lobal variables,
13220 0a 2a 2a 20 73 65 74 74 69 6e 67 20 75 70 20 61  .** setting up a
13230 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65   default [sqlite
13240 33 5f 76 66 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f  3_vfs] module, o
13250 72 20 73 65 74 74 69 6e 67 20 75 70 0a 2a 2a 20  r setting up.** 
13260 61 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67  a default config
13270 75 72 61 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73  uration using [s
13280 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d  qlite3_config()]
13290 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c  ..**.** The appl
132a0 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e  ication should n
132b0 65 76 65 72 20 69 6e 76 6f 6b 65 20 65 69 74 68  ever invoke eith
132c0 65 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  er sqlite3_os_in
132d0 69 74 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74  it().** or sqlit
132e0 65 33 5f 6f 73 5f 65 6e 64 28 29 20 64 69 72 65  e3_os_end() dire
132f0 63 74 6c 79 2e 20 20 54 68 65 20 61 70 70 6c 69  ctly.  The appli
13300 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e  cation should on
13310 6c 79 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c  ly invoke.** sql
13320 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
13330 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 68  ) and sqlite3_sh
13340 75 74 64 6f 77 6e 28 29 2e 20 20 54 68 65 20 73  utdown().  The s
13350 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29  qlite3_os_init()
13360 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73  .** interface is
13370 20 63 61 6c 6c 65 64 20 61 75 74 6f 6d 61 74 69   called automati
13380 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33  cally by sqlite3
13390 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e  _initialize() an
133a0 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f  d.** sqlite3_os_
133b0 65 6e 64 28 29 20 69 73 20 63 61 6c 6c 65 64 20  end() is called 
133c0 62 79 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  by sqlite3_shutd
133d0 6f 77 6e 28 29 2e 20 20 41 70 70 72 6f 70 72 69  own().  Appropri
133e0 61 74 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ate.** implement
133f0 61 74 69 6f 6e 73 20 66 6f 72 20 73 71 6c 69 74  ations for sqlit
13400 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64  e3_os_init() and
13410 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
13420 29 0a 2a 2a 20 61 72 65 20 62 75 69 6c 74 20 69  ).** are built i
13430 6e 74 6f 20 53 51 4c 69 74 65 20 77 68 65 6e 20  nto SQLite when 
13440 69 74 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 66  it is compiled f
13450 6f 72 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73  or Unix, Windows
13460 2c 20 6f 72 20 4f 53 2f 32 2e 0a 2a 2a 20 57 68  , or OS/2..** Wh
13470 65 6e 20 5b 63 75 73 74 6f 6d 20 62 75 69 6c 64  en [custom build
13480 73 20 7c 20 62 75 69 6c 74 20 66 6f 72 20 6f 74  s | built for ot
13490 68 65 72 20 70 6c 61 74 66 6f 72 6d 73 5d 0a 2a  her platforms].*
134a0 2a 20 28 75 73 69 6e 67 20 74 68 65 20 5b 53 51  * (using the [SQ
134b0 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 31 5d  LITE_OS_OTHER=1]
134c0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a   compile-time.**
134d0 20 6f 70 74 69 6f 6e 29 20 74 68 65 20 61 70 70   option) the app
134e0 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 73 75  lication must su
134f0 70 70 6c 79 20 61 20 73 75 69 74 61 62 6c 65 20  pply a suitable 
13500 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66  implementation f
13510 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73  or.** sqlite3_os
13520 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69  _init() and sqli
13530 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 20 20 41  te3_os_end().  A
13540 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75  n application-su
13550 70 70 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d  pplied.** implem
13560 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69  entation of sqli
13570 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 6f 72  te3_os_init() or
13580 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
13590 29 0a 2a 2a 20 6d 75 73 74 20 72 65 74 75 72 6e  ).** must return
135a0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20   [SQLITE_OK] on 
135b0 73 75 63 63 65 73 73 20 61 6e 64 20 73 6f 6d 65  success and some
135c0 20 6f 74 68 65 72 20 5b 65 72 72 6f 72 20 63 6f   other [error co
135d0 64 65 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61 69 6c  de] upon.** fail
135e0 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ure..*/.SQLITE_A
135f0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
13600 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b  nitialize(void);
13610 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
13620 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
13630 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41  (void);.SQLITE_A
13640 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
13650 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51  s_init(void);.SQ
13660 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
13670 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64  ite3_os_end(void
13680 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
13690 45 46 3a 20 43 6f 6e 66 69 67 75 72 69 6e 67 20  EF: Configuring 
136a0 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62 72 61  The SQLite Libra
136b0 72 79 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  ry.**.** The sql
136c0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 6e  ite3_config() in
136d0 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20  terface is used 
136e0 74 6f 20 6d 61 6b 65 20 67 6c 6f 62 61 6c 20 63  to make global c
136f0 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a 20  onfiguration.** 
13700 63 68 61 6e 67 65 73 20 74 6f 20 53 51 4c 69 74  changes to SQLit
13710 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74 75  e in order to tu
13720 6e 65 20 53 51 4c 69 74 65 20 74 6f 20 74 68 65  ne SQLite to the
13730 20 73 70 65 63 69 66 69 63 20 6e 65 65 64 73 20   specific needs 
13740 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63  of.** the applic
13750 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61  ation.  The defa
13760 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  ult configuratio
13770 6e 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64  n is recommended
13780 20 66 6f 72 20 6d 6f 73 74 0a 2a 2a 20 61 70 70   for most.** app
13790 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 73 6f  lications and so
137a0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
137b0 20 75 73 75 61 6c 6c 79 20 6e 6f 74 20 6e 65 63   usually not nec
137c0 65 73 73 61 72 79 2e 20 20 49 74 20 69 73 0a 2a  essary.  It is.*
137d0 2a 20 70 72 6f 76 69 64 65 64 20 74 6f 20 73 75  * provided to su
137e0 70 70 6f 72 74 20 72 61 72 65 20 61 70 70 6c 69  pport rare appli
137f0 63 61 74 69 6f 6e 73 20 77 69 74 68 20 75 6e 75  cations with unu
13800 73 75 61 6c 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a  sual needs..**.*
13810 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  * The sqlite3_co
13820 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65  nfig() interface
13830 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61   is not threadsa
13840 66 65 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61  fe.  The applica
13850 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 73  tion.** must ins
13860 75 72 65 20 74 68 61 74 20 6e 6f 20 6f 74 68 65  ure that no othe
13870 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61  r SQLite interfa
13880 63 65 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 20  ces are invoked 
13890 62 79 20 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65  by other.** thre
138a0 61 64 73 20 77 68 69 6c 65 20 73 71 6c 69 74 65  ads while sqlite
138b0 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 72 75  3_config() is ru
138c0 6e 6e 69 6e 67 2e 20 20 46 75 72 74 68 65 72 6d  nning.  Furtherm
138d0 6f 72 65 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e  ore, sqlite3_con
138e0 66 69 67 28 29 0a 2a 2a 20 6d 61 79 20 6f 6e 6c  fig().** may onl
138f0 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 69  y be invoked pri
13900 6f 72 20 74 6f 20 6c 69 62 72 61 72 79 20 69 6e  or to library in
13910 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 73 69  itialization usi
13920 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69  ng.** [sqlite3_i
13930 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 6f 72 20  nitialize()] or 
13940 61 66 74 65 72 20 73 68 75 74 64 6f 77 6e 20 62  after shutdown b
13950 79 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64  y [sqlite3_shutd
13960 6f 77 6e 28 29 5d 2e 0a 2a 2a 20 5e 49 66 20 73  own()]..** ^If s
13970 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20  qlite3_config() 
13980 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
13990 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c  [sqlite3_initial
139a0 69 7a 65 28 29 5d 20 61 6e 64 20 62 65 66 6f 72  ize()] and befor
139b0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 68  e.** [sqlite3_sh
139c0 75 74 64 6f 77 6e 28 29 5d 20 74 68 65 6e 20 69  utdown()] then i
139d0 74 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51  t will return SQ
139e0 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 20  LITE_MISUSE..** 
139f0 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74  Note, however, t
13a00 68 61 74 20 5e 73 71 6c 69 74 65 33 5f 63 6f 6e  hat ^sqlite3_con
13a10 66 69 67 28 29 20 63 61 6e 20 62 65 20 63 61 6c  fig() can be cal
13a20 6c 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  led as part of t
13a30 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  he.** implementa
13a40 74 69 6f 6e 20 6f 66 20 61 6e 20 61 70 70 6c 69  tion of an appli
13a50 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 5b  cation-defined [
13a60 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28  sqlite3_os_init(
13a70 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  )]..**.** The fi
13a80 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
13a90 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
13aa0 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a   is an integer.*
13ab0 2a 20 5b 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  * [configuration
13ac0 20 6f 70 74 69 6f 6e 5d 20 74 68 61 74 20 64 65   option] that de
13ad0 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68 61 74  termines.** what
13ae0 20 70 72 6f 70 65 72 74 79 20 6f 66 20 53 51 4c   property of SQL
13af0 69 74 65 20 69 73 20 74 6f 20 62 65 20 63 6f 6e  ite is to be con
13b00 66 69 67 75 72 65 64 2e 20 20 53 75 62 73 65 71  figured.  Subseq
13b10 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 73 0a 2a  uent arguments.*
13b20 2a 20 76 61 72 79 20 64 65 70 65 6e 64 69 6e 67  * vary depending
13b30 20 6f 6e 20 74 68 65 20 5b 63 6f 6e 66 69 67 75   on the [configu
13b40 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a  ration option].*
13b50 2a 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61  * in the first a
13b60 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 5e  rgument..**.** ^
13b70 57 68 65 6e 20 61 20 63 6f 6e 66 69 67 75 72 61  When a configura
13b80 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73  tion option is s
13b90 65 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  et, sqlite3_conf
13ba0 69 67 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51  ig() returns [SQ
13bb0 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 20 5e 49 66  LITE_OK]..** ^If
13bc0 20 74 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 75   the option is u
13bd0 6e 6b 6e 6f 77 6e 20 6f 72 20 53 51 4c 69 74 65  nknown or SQLite
13be0 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 73 65   is unable to se
13bf0 74 20 74 68 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20  t the option.** 
13c00 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
13c10 65 20 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e 2d  e returns a non-
13c20 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65  zero [error code
13c30 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  ]..*/.SQLITE_API
13c40 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e   int sqlite3_con
13c50 66 69 67 28 69 6e 74 2c 20 2e 2e 2e 29 3b 0a 0a  fig(int, ...);..
13c60 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
13c70 43 6f 6e 66 69 67 75 72 65 20 64 61 74 61 62 61  Configure databa
13c80 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a  se connections.*
13c90 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
13ca0 5f 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74  _db_config() int
13cb0 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74  erface is used t
13cc0 6f 20 6d 61 6b 65 20 63 6f 6e 66 69 67 75 72 61  o make configura
13cd0 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 20  tion.** changes 
13ce0 74 6f 20 61 20 5b 64 61 74 61 62 61 73 65 20 63  to a [database c
13cf0 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65  onnection].  The
13d00 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 73 69   interface is si
13d10 6d 69 6c 61 72 20 74 6f 0a 2a 2a 20 5b 73 71 6c  milar to.** [sql
13d20 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 65  ite3_config()] e
13d30 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 63  xcept that the c
13d40 68 61 6e 67 65 73 20 61 70 70 6c 79 20 74 6f 20  hanges apply to 
13d50 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 5b 64 61 74  a single.** [dat
13d60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
13d70 5d 20 28 73 70 65 63 69 66 69 65 64 20 69 6e 20  ] (specified in 
13d80 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
13d90 6e 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  nt)..**.** The s
13da0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
13db0 6f 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e  o sqlite3_db_con
13dc0 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 20 20 69 73  fig(D,V,...)  is
13dd0 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f   the.** [SQLITE_
13de0 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49  DBCONFIG_LOOKASI
13df0 44 45 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69  DE | configurati
13e00 6f 6e 20 76 65 72 62 5d 20 2d 20 61 6e 20 69 6e  on verb] - an in
13e10 74 65 67 65 72 20 63 6f 64 65 20 0a 2a 2a 20 74  teger code .** t
13e20 68 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 68  hat indicates wh
13e30 61 74 20 61 73 70 65 63 74 20 6f 66 20 74 68 65  at aspect of the
13e40 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
13e50 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20  ction] is being 
13e60 63 6f 6e 66 69 67 75 72 65 64 2e 0a 2a 2a 20 53  configured..** S
13e70 75 62 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65  ubsequent argume
13e80 6e 74 73 20 76 61 72 79 20 64 65 70 65 6e 64 69  nts vary dependi
13e90 6e 67 20 6f 6e 20 74 68 65 20 63 6f 6e 66 69 67  ng on the config
13ea0 75 72 61 74 69 6f 6e 20 76 65 72 62 2e 0a 2a 2a  uration verb..**
13eb0 0a 2a 2a 20 5e 43 61 6c 6c 73 20 74 6f 20 73 71  .** ^Calls to sq
13ec0 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28  lite3_db_config(
13ed0 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
13ee0 4f 4b 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69  OK if and only i
13ef0 66 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 69 73  f.** the call is
13f00 20 63 6f 6e 73 69 64 65 72 65 64 20 73 75 63 63   considered succ
13f10 65 73 73 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54  essful..*/.SQLIT
13f20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
13f30 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69  3_db_config(sqli
13f40 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e  te3*, int op, ..
13f50 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
13f60 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f  REF: Memory Allo
13f70 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 0a  cation Routines.
13f80 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  **.** An instanc
13f90 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
13fa0 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74   defines the int
13fb0 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 20 53  erface between S
13fc0 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c 6f 77  QLite.** and low
13fd0 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c  -level memory al
13fe0 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65  location routine
13ff0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62  s..**.** This ob
14000 6a 65 63 74 20 69 73 20 75 73 65 64 20 69 6e 20  ject is used in 
14010 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65 20 69  only one place i
14020 6e 20 74 68 65 20 53 51 4c 69 74 65 20 69 6e 74  n the SQLite int
14030 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70 6f 69  erface..** A poi
14040 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61  nter to an insta
14050 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
14060 63 74 20 69 73 20 74 68 65 20 61 72 67 75 6d 65  ct is the argume
14070 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65  nt to.** [sqlite
14080 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68 65 6e  3_config()] when
14090 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69   the configurati
140a0 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a 20  on option is.** 
140b0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
140c0 41 4c 4c 4f 43 5d 20 6f 72 20 5b 53 51 4c 49 54  ALLOC] or [SQLIT
140d0 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c  E_CONFIG_GETMALL
140e0 4f 43 5d 2e 20 20 0a 2a 2a 20 42 79 20 63 72 65  OC].  .** By cre
140f0 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e 63  ating an instanc
14100 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
14110 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e 67 20  .** and passing 
14120 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  it to [sqlite3_c
14130 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43  onfig]([SQLITE_C
14140 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 29 0a 2a  ONFIG_MALLOC]).*
14150 2a 20 64 75 72 69 6e 67 20 63 6f 6e 66 69 67 75  * during configu
14160 72 61 74 69 6f 6e 2c 20 61 6e 20 61 70 70 6c 69  ration, an appli
14170 63 61 74 69 6f 6e 20 63 61 6e 20 73 70 65 63 69  cation can speci
14180 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76  fy an alternativ
14190 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  e.** memory allo
141a0 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
141b0 20 66 6f 72 20 53 51 4c 69 74 65 20 74 6f 20 75   for SQLite to u
141c0 73 65 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74  se for all of it
141d0 73 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 65 6d  s.** dynamic mem
141e0 6f 72 79 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a  ory needs..**.**
141f0 20 4e 6f 74 65 20 74 68 61 74 20 53 51 4c 69 74   Note that SQLit
14200 65 20 63 6f 6d 65 73 20 77 69 74 68 20 73 65 76  e comes with sev
14210 65 72 61 6c 20 5b 62 75 69 6c 74 2d 69 6e 20 6d  eral [built-in m
14220 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73  emory allocators
14230 5d 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 70 65  ].** that are pe
14240 72 66 65 63 74 6c 79 20 61 64 65 71 75 61 74 65  rfectly adequate
14250 20 66 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65   for the overwhe
14260 6c 6d 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f  lming majority o
14270 66 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a  f applications.*
14280 2a 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20  * and that this 
14290 6f 62 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75  object is only u
142a0 73 65 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20  seful to a tiny 
142b0 6d 69 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c  minority of appl
142c0 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68  ications.** with
142d0 20 73 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d   specialized mem
142e0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
142f0 65 71 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68  equirements.  Th
14300 69 73 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20  is object is.** 
14310 61 6c 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67  also used during
14320 20 74 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69   testing of SQLi
14330 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73  te in order to s
14340 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e  pecify an altern
14350 61 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20  ative.** memory 
14360 61 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73  allocator that s
14370 69 6d 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20  imulates memory 
14380 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f  out-of-memory co
14390 6e 64 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f  nditions in.** o
143a0 72 64 65 72 20 74 6f 20 76 65 72 69 66 79 20 74  rder to verify t
143b0 68 61 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76  hat SQLite recov
143c0 65 72 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66  ers gracefully f
143d0 72 6f 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64  rom such.** cond
143e0 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  itions..**.** Th
143f0 65 20 78 4d 61 6c 6c 6f 63 2c 20 78 52 65 61 6c  e xMalloc, xReal
14400 6c 6f 63 2c 20 61 6e 64 20 78 46 72 65 65 20 6d  loc, and xFree m
14410 65 74 68 6f 64 73 20 6d 75 73 74 20 77 6f 72 6b  ethods must work
14420 20 6c 69 6b 65 20 74 68 65 0a 2a 2a 20 6d 61 6c   like the.** mal
14430 6c 6f 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29  loc(), realloc()
14440 20 61 6e 64 20 66 72 65 65 28 29 20 66 75 6e 63   and free() func
14450 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 73  tions from the s
14460 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72  tandard C librar
14470 79 2e 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 67 75  y..** ^SQLite gu
14480 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 68  arantees that th
14490 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
144a0 74 20 74 6f 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63  t to.** xRealloc
144b0 20 69 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c   is always a val
144c0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61  ue returned by a
144d0 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 78   prior call to x
144e0 52 6f 75 6e 64 75 70 2e 0a 2a 2a 0a 2a 2a 20 78  Roundup..**.** x
144f0 53 69 7a 65 20 73 68 6f 75 6c 64 20 72 65 74 75  Size should retu
14500 72 6e 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64  rn the allocated
14510 20 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72   size of a memor
14520 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20  y allocation.** 
14530 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69  previously obtai
14540 6e 65 64 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63  ned from xMalloc
14550 20 6f 72 20 78 52 65 61 6c 6c 6f 63 2e 20 20 54   or xRealloc.  T
14560 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a  he allocated siz
14570 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61  e.** is always a
14580 74 20 6c 65 61 73 74 20 61 73 20 62 69 67 20 61  t least as big a
14590 73 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  s the requested 
145a0 73 69 7a 65 20 62 75 74 20 6d 61 79 20 62 65 20  size but may be 
145b0 6c 61 72 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  larger..**.** Th
145c0 65 20 78 52 6f 75 6e 64 75 70 20 6d 65 74 68 6f  e xRoundup metho
145d0 64 20 72 65 74 75 72 6e 73 20 77 68 61 74 20 77  d returns what w
145e0 6f 75 6c 64 20 62 65 20 74 68 65 20 61 6c 6c 6f  ould be the allo
145f0 63 61 74 65 64 20 73 69 7a 65 20 6f 66 0a 2a 2a  cated size of.**
14600 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
14610 74 69 6f 6e 20 67 69 76 65 6e 20 61 20 70 61 72  tion given a par
14620 74 69 63 75 6c 61 72 20 72 65 71 75 65 73 74 65  ticular requeste
14630 64 20 73 69 7a 65 2e 20 20 4d 6f 73 74 20 6d 65  d size.  Most me
14640 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f  mory.** allocato
14650 72 73 20 72 6f 75 6e 64 20 75 70 20 6d 65 6d 6f  rs round up memo
14660 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61  ry allocations a
14670 74 20 6c 65 61 73 74 20 74 6f 20 74 68 65 20 6e  t least to the n
14680 65 78 74 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20  ext multiple.** 
14690 6f 66 20 38 2e 20 20 53 6f 6d 65 20 61 6c 6c 6f  of 8.  Some allo
146a0 63 61 74 6f 72 73 20 72 6f 75 6e 64 20 75 70 20  cators round up 
146b0 74 6f 20 61 20 6c 61 72 67 65 72 20 6d 75 6c 74  to a larger mult
146c0 69 70 6c 65 20 6f 72 20 74 6f 20 61 20 70 6f 77  iple or to a pow
146d0 65 72 20 6f 66 20 32 2e 0a 2a 2a 20 45 76 65 72  er of 2..** Ever
146e0 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  y memory allocat
146f0 69 6f 6e 20 72 65 71 75 65 73 74 20 63 6f 6d 69  ion request comi
14700 6e 67 20 69 6e 20 74 68 72 6f 75 67 68 20 5b 73  ng in through [s
14710 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
14720 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  .** or [sqlite3_
14730 72 65 61 6c 6c 6f 63 28 29 5d 20 66 69 72 73 74  realloc()] first
14740 20 63 61 6c 6c 73 20 78 52 6f 75 6e 64 75 70 2e   calls xRoundup.
14750 20 20 49 66 20 78 52 6f 75 6e 64 75 70 20 72 65    If xRoundup re
14760 74 75 72 6e 73 20 30 2c 20 0a 2a 2a 20 74 68 61  turns 0, .** tha
14770 74 20 63 61 75 73 65 73 20 74 68 65 20 63 6f 72  t causes the cor
14780 72 65 73 70 6f 6e 64 69 6e 67 20 6d 65 6d 6f 72  responding memor
14790 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20  y allocation to 
147a0 66 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  fail..**.** The 
147b0 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 69 6e 69  xInit method ini
147c0 74 69 61 6c 69 7a 65 73 20 74 68 65 20 6d 65 6d  tializes the mem
147d0 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 20  ory allocator.  
147e0 28 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a  (For example,.**
147f0 20 69 74 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61   it might alloca
14800 74 65 20 61 6e 79 20 72 65 71 75 69 72 65 20 6d  te any require m
14810 75 74 65 78 65 73 20 6f 72 20 69 6e 69 74 69 61  utexes or initia
14820 6c 69 7a 65 20 69 6e 74 65 72 6e 61 6c 20 64 61  lize internal da
14830 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73  ta.** structures
14840 2e 20 20 54 68 65 20 78 53 68 75 74 64 6f 77 6e  .  The xShutdown
14850 20 6d 65 74 68 6f 64 20 69 73 20 69 6e 76 6f 6b   method is invok
14860 65 64 20 28 69 6e 64 69 72 65 63 74 6c 79 29 20  ed (indirectly) 
14870 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73  by.** [sqlite3_s
14880 68 75 74 64 6f 77 6e 28 29 5d 20 61 6e 64 20 73  hutdown()] and s
14890 68 6f 75 6c 64 20 64 65 61 6c 6c 6f 63 61 74 65  hould deallocate
148a0 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 20 61   any resources a
148b0 63 71 75 69 72 65 64 0a 2a 2a 20 62 79 20 78 49  cquired.** by xI
148c0 6e 69 74 2e 20 20 54 68 65 20 70 41 70 70 44 61  nit.  The pAppDa
148d0 74 61 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73  ta pointer is us
148e0 65 64 20 61 73 20 74 68 65 20 6f 6e 6c 79 20 70  ed as the only p
148f0 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 78  arameter to.** x
14900 49 6e 69 74 20 61 6e 64 20 78 53 68 75 74 64 6f  Init and xShutdo
14910 77 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  wn..**.** SQLite
14920 20 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c 49   holds the [SQLI
14930 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
14940 4d 41 53 54 45 52 5d 20 6d 75 74 65 78 20 77 68  MASTER] mutex wh
14950 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73 0a 2a 2a  en it invokes.**
14960 20 74 68 65 20 78 49 6e 69 74 20 6d 65 74 68 6f   the xInit metho
14970 64 2c 20 73 6f 20 74 68 65 20 78 49 6e 69 74 20  d, so the xInit 
14980 6d 65 74 68 6f 64 20 6e 65 65 64 20 6e 6f 74 20  method need not 
14990 62 65 20 74 68 72 65 61 64 73 61 66 65 2e 20 20  be threadsafe.  
149a0 54 68 65 0a 2a 2a 20 78 53 68 75 74 64 6f 77 6e  The.** xShutdown
149b0 20 6d 65 74 68 6f 64 20 69 73 20 6f 6e 6c 79 20   method is only 
149c0 63 61 6c 6c 65 64 20 66 72 6f 6d 20 5b 73 71 6c  called from [sql
149d0 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d  ite3_shutdown()]
149e0 20 73 6f 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e   so it does.** n
149f0 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 74 68  ot need to be th
14a00 72 65 61 64 73 61 66 65 20 65 69 74 68 65 72 2e  readsafe either.
14a10 20 20 46 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20    For all other 
14a20 6d 65 74 68 6f 64 73 2c 20 53 51 4c 69 74 65 0a  methods, SQLite.
14a30 2a 2a 20 68 6f 6c 64 73 20 74 68 65 20 5b 53 51  ** holds the [SQ
14a40 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
14a50 43 5f 4d 45 4d 5d 20 6d 75 74 65 78 20 61 73 20  C_MEM] mutex as 
14a60 6c 6f 6e 67 20 61 73 20 74 68 65 0a 2a 2a 20 5b  long as the.** [
14a70 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45  SQLITE_CONFIG_ME
14a80 4d 53 54 41 54 55 53 5d 20 63 6f 6e 66 69 67 75  MSTATUS] configu
14a90 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73  ration option is
14aa0 20 74 75 72 6e 65 64 20 6f 6e 20 28 77 68 69 63   turned on (whic
14ab0 68 0a 2a 2a 20 69 74 20 69 73 20 62 79 20 64 65  h.** it is by de
14ac0 66 61 75 6c 74 29 20 61 6e 64 20 73 6f 20 74 68  fault) and so th
14ad0 65 20 6d 65 74 68 6f 64 73 20 61 72 65 20 61 75  e methods are au
14ae0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 72 69  tomatically seri
14af0 61 6c 69 7a 65 64 2e 0a 2a 2a 20 48 6f 77 65 76  alized..** Howev
14b00 65 72 2c 20 69 66 20 5b 53 51 4c 49 54 45 5f 43  er, if [SQLITE_C
14b10 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 5d  ONFIG_MEMSTATUS]
14b20 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74 68   is disabled, th
14b30 65 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20  en the other.** 
14b40 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 62 65 20  methods must be 
14b50 74 68 72 65 61 64 73 61 66 65 20 6f 72 20 65 6c  threadsafe or el
14b60 73 65 20 6d 61 6b 65 20 74 68 65 69 72 20 6f 77  se make their ow
14b70 6e 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20 66  n arrangements f
14b80 6f 72 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 61 74  or.** serializat
14b90 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  ion..**.** SQLit
14ba0 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 69 6e 76  e will never inv
14bb0 6f 6b 65 20 78 49 6e 69 74 28 29 20 6d 6f 72 65  oke xInit() more
14bc0 20 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 6f   than once witho
14bd0 75 74 20 61 6e 20 69 6e 74 65 72 76 65 6e 69 6e  ut an intervenin
14be0 67 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 53 68  g.** call to xSh
14bf0 75 74 64 6f 77 6e 28 29 2e 0a 2a 2f 0a 74 79 70  utdown()..*/.typ
14c00 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
14c10 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20  te3_mem_methods 
14c20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
14c30 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69  ods;.struct sqli
14c40 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20  te3_mem_methods 
14c50 7b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 4d 61 6c  {.  void *(*xMal
14c60 6c 6f 63 29 28 69 6e 74 29 3b 20 20 20 20 20 20  loc)(int);      
14c70 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c     /* Memory all
14c80 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  ocation function
14c90 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 72   */.  void (*xFr
14ca0 65 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20  ee)(void*);     
14cb0 20 20 20 20 20 2f 2a 20 46 72 65 65 20 61 20 70       /* Free a p
14cc0 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  rior allocation 
14cd0 2a 2f 0a 20 20 76 6f 69 64 20 2a 28 2a 78 52 65  */.  void *(*xRe
14ce0 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e 74  alloc)(void*,int
14cf0 29 3b 20 20 2f 2a 20 52 65 73 69 7a 65 20 61 6e  );  /* Resize an
14d00 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20   allocation */. 
14d10 20 69 6e 74 20 28 2a 78 53 69 7a 65 29 28 76 6f   int (*xSize)(vo
14d20 69 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20  id*);           
14d30 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69  /* Return the si
14d40 7a 65 20 6f 66 20 61 6e 20 61 6c 6c 6f 63 61 74  ze of an allocat
14d50 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78  ion */.  int (*x
14d60 52 6f 75 6e 64 75 70 29 28 69 6e 74 29 3b 20 20  Roundup)(int);  
14d70 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64          /* Round
14d80 20 75 70 20 72 65 71 75 65 73 74 20 73 69 7a 65   up request size
14d90 20 74 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73   to allocation s
14da0 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78  ize */.  int (*x
14db0 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 20 20 20  Init)(void*);   
14dc0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69          /* Initi
14dd0 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79  alize the memory
14de0 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20   allocator */.  
14df0 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f 77 6e  void (*xShutdown
14e00 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 2f  )(void*);      /
14e10 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74  * Deinitialize t
14e20 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
14e30 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  tor */.  void *p
14e40 41 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20  AppData;        
14e50 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
14e60 65 6e 74 20 74 6f 20 78 49 6e 69 74 28 29 20 61  ent to xInit() a
14e70 6e 64 20 78 53 68 75 74 64 6f 77 6e 28 29 20 2a  nd xShutdown() *
14e80 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  /.};../*.** CAPI
14e90 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 61 74  3REF: Configurat
14ea0 69 6f 6e 20 4f 70 74 69 6f 6e 73 0a 2a 2a 20 4b  ion Options.** K
14eb0 45 59 57 4f 52 44 53 3a 20 7b 63 6f 6e 66 69 67  EYWORDS: {config
14ec0 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 7d 0a  uration option}.
14ed0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73  **.** These cons
14ee0 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 76  tants are the av
14ef0 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20  ailable integer 
14f00 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
14f10 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63 61  tions that.** ca
14f20 6e 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74  n be passed as t
14f30 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
14f40 74 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65  t to the [sqlite
14f50 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65  3_config()] inte
14f60 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77  rface..**.** New
14f70 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
14f80 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64  ptions may be ad
14f90 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65  ded in future re
14fa0 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65  leases of SQLite
14fb0 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f  ..** Existing co
14fc0 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
14fd0 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 64 69 73  ons might be dis
14fe0 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c  continued.  Appl
14ff0 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75  ications.** shou
15000 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72 65 74  ld check the ret
15010 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73  urn code from [s
15020 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d  qlite3_config()]
15030 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
15040 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77  at.** the call w
15050 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c  orked.  The [sql
15060 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69  ite3_config()] i
15070 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65  nterface will re
15080 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65  turn a.** non-ze
15090 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  ro [error code] 
150a0 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65  if a discontinue
150b0 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64  d or unsupported
150c0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
150d0 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f  ption.** is invo
150e0 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a  ked..**.** <dl>.
150f0 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  ** [[SQLITE_CONF
15100 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 5d  IG_SINGLETHREAD]
15110 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  ] <dt>SQLITE_CON
15120 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44  FIG_SINGLETHREAD
15130 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65  </dt>.** <dd>The
15140 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65  re are no argume
15150 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69  nts to this opti
15160 6f 6e 2e 20 20 5e 54 68 69 73 20 6f 70 74 69 6f  on.  ^This optio
15170 6e 20 73 65 74 73 20 74 68 65 0a 2a 2a 20 5b 74  n sets the.** [t
15180 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 74  hreading mode] t
15190 6f 20 53 69 6e 67 6c 65 2d 74 68 72 65 61 64 2e  o Single-thread.
151a0 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
151b0 2c 20 69 74 20 64 69 73 61 62 6c 65 73 0a 2a 2a  , it disables.**
151c0 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 61 6e   all mutexing an
151d0 64 20 70 75 74 73 20 53 51 4c 69 74 65 20 69 6e  d puts SQLite in
151e0 74 6f 20 61 20 6d 6f 64 65 20 77 68 65 72 65 20  to a mode where 
151f0 69 74 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75  it can only be u
15200 73 65 64 0a 2a 2a 20 62 79 20 61 20 73 69 6e 67  sed.** by a sing
15210 6c 65 20 74 68 72 65 61 64 2e 20 20 20 5e 49 66  le thread.   ^If
15220 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69   SQLite is compi
15230 6c 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20  led with.** the 
15240 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  [SQLITE_THREADSA
15250 46 45 20 7c 20 53 51 4c 49 54 45 5f 54 48 52 45  FE | SQLITE_THRE
15260 41 44 53 41 46 45 3d 30 5d 20 63 6f 6d 70 69 6c  ADSAFE=0] compil
15270 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 74 68  e-time option th
15280 65 6e 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20  en.** it is not 
15290 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 68 61 6e  possible to chan
152a0 67 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e  ge the [threadin
152b0 67 20 6d 6f 64 65 5d 20 66 72 6f 6d 20 69 74 73  g mode] from its
152c0 20 64 65 66 61 75 6c 74 0a 2a 2a 20 76 61 6c 75   default.** valu
152d0 65 20 6f 66 20 53 69 6e 67 6c 65 2d 74 68 72 65  e of Single-thre
152e0 61 64 20 61 6e 64 20 73 6f 20 5b 73 71 6c 69 74  ad and so [sqlit
152f0 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 69 6c  e3_config()] wil
15300 6c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 5b 53 51  l return .** [SQ
15310 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 63  LITE_ERROR] if c
15320 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 53  alled with the S
15330 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e  QLITE_CONFIG_SIN
15340 47 4c 45 54 48 52 45 41 44 0a 2a 2a 20 63 6f 6e  GLETHREAD.** con
15350 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
15360 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b  n.</dd>.**.** [[
15370 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
15380 4c 54 49 54 48 52 45 41 44 5d 5d 20 3c 64 74 3e  LTITHREAD]] <dt>
15390 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
153a0 4c 54 49 54 48 52 45 41 44 3c 2f 64 74 3e 0a 2a  LTITHREAD</dt>.*
153b0 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20  * <dd>There are 
153c0 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  no arguments to 
153d0 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 5e 54  this option.  ^T
153e0 68 69 73 20 6f 70 74 69 6f 6e 20 73 65 74 73 20  his option sets 
153f0 74 68 65 0a 2a 2a 20 5b 74 68 72 65 61 64 69 6e  the.** [threadin
15400 67 20 6d 6f 64 65 5d 20 74 6f 20 4d 75 6c 74 69  g mode] to Multi
15410 2d 74 68 72 65 61 64 2e 20 20 49 6e 20 6f 74 68  -thread.  In oth
15420 65 72 20 77 6f 72 64 73 2c 20 69 74 20 64 69 73  er words, it dis
15430 61 62 6c 65 73 0a 2a 2a 20 6d 75 74 65 78 69 6e  ables.** mutexin
15440 67 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63  g on [database c
15450 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b  onnection] and [
15460 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
15470 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20  nt] objects..** 
15480 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  The application 
15490 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
154a0 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 20 61  or serializing a
154b0 63 63 65 73 73 20 74 6f 0a 2a 2a 20 5b 64 61 74  ccess to.** [dat
154c0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
154d0 73 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64  s] and [prepared
154e0 20 73 74 61 74 65 6d 65 6e 74 73 5d 2e 20 20 42   statements].  B
154f0 75 74 20 6f 74 68 65 72 20 6d 75 74 65 78 65 73  ut other mutexes
15500 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 20  .** are enabled 
15510 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 77  so that SQLite w
15520 69 6c 6c 20 62 65 20 73 61 66 65 20 74 6f 20 75  ill be safe to u
15530 73 65 20 69 6e 20 61 20 6d 75 6c 74 69 2d 74 68  se in a multi-th
15540 72 65 61 64 65 64 0a 2a 2a 20 65 6e 76 69 72 6f  readed.** enviro
15550 6e 6d 65 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73  nment as long as
15560 20 6e 6f 20 74 77 6f 20 74 68 72 65 61 64 73 20   no two threads 
15570 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74  attempt to use t
15580 68 65 20 73 61 6d 65 0a 2a 2a 20 5b 64 61 74 61  he same.** [data
15590 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
155a0 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
155b0 65 2e 20 20 5e 49 66 20 53 51 4c 69 74 65 20 69  e.  ^If SQLite i
155c0 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a  s compiled with.
155d0 2a 2a 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54  ** the [SQLITE_T
155e0 48 52 45 41 44 53 41 46 45 20 7c 20 53 51 4c 49  HREADSAFE | SQLI
155f0 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 30 5d  TE_THREADSAFE=0]
15600 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70   compile-time op
15610 74 69 6f 6e 20 74 68 65 6e 0a 2a 2a 20 69 74 20  tion then.** it 
15620 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
15630 74 6f 20 73 65 74 20 74 68 65 20 4d 75 6c 74 69  to set the Multi
15640 2d 74 68 72 65 61 64 20 5b 74 68 72 65 61 64 69  -thread [threadi
15650 6e 67 20 6d 6f 64 65 5d 20 61 6e 64 0a 2a 2a 20  ng mode] and.** 
15660 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28  [sqlite3_config(
15670 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b  )] will return [
15680 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66  SQLITE_ERROR] if
15690 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65   called with the
156a0 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  .** SQLITE_CONFI
156b0 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 20 63 6f  G_MULTITHREAD co
156c0 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
156d0 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b  on.</dd>.**.** [
156e0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53  [SQLITE_CONFIG_S
156f0 45 52 49 41 4c 49 5a 45 44 5d 5d 20 3c 64 74 3e  ERIALIZED]] <dt>
15700 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45  SQLITE_CONFIG_SE
15710 52 49 41 4c 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a  RIALIZED</dt>.**
15720 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e   <dd>There are n
15730 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  o arguments to t
15740 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 5e 54 68  his option.  ^Th
15750 69 73 20 6f 70 74 69 6f 6e 20 73 65 74 73 20 74  is option sets t
15760 68 65 0a 2a 2a 20 5b 74 68 72 65 61 64 69 6e 67  he.** [threading
15770 20 6d 6f 64 65 5d 20 74 6f 20 53 65 72 69 61 6c   mode] to Serial
15780 69 7a 65 64 2e 20 49 6e 20 6f 74 68 65 72 20 77  ized. In other w
15790 6f 72 64 73 2c 20 74 68 69 73 20 6f 70 74 69 6f  ords, this optio
157a0 6e 20 65 6e 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c  n enables.** all
157b0 20 6d 75 74 65 78 65 73 20 69 6e 63 6c 75 64 69   mutexes includi
157c0 6e 67 20 74 68 65 20 72 65 63 75 72 73 69 76 65  ng the recursive
157d0 0a 2a 2a 20 6d 75 74 65 78 65 73 20 6f 6e 20 5b  .** mutexes on [
157e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
157f0 69 6f 6e 5d 20 61 6e 64 20 5b 70 72 65 70 61 72  ion] and [prepar
15800 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62  ed statement] ob
15810 6a 65 63 74 73 2e 0a 2a 2a 20 49 6e 20 74 68 69  jects..** In thi
15820 73 20 6d 6f 64 65 20 28 77 68 69 63 68 20 69 73  s mode (which is
15830 20 74 68 65 20 64 65 66 61 75 6c 74 20 77 68 65   the default whe
15840 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70  n SQLite is comp
15850 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 5b 53 51  iled with.** [SQ
15860 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d  LITE_THREADSAFE=
15870 31 5d 29 20 74 68 65 20 53 51 4c 69 74 65 20 6c  1]) the SQLite l
15880 69 62 72 61 72 79 20 77 69 6c 6c 20 69 74 73 65  ibrary will itse
15890 6c 66 20 73 65 72 69 61 6c 69 7a 65 20 61 63 63  lf serialize acc
158a0 65 73 73 0a 2a 2a 20 74 6f 20 5b 64 61 74 61 62  ess.** to [datab
158b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d  ase connections]
158c0 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73   and [prepared s
158d0 74 61 74 65 6d 65 6e 74 73 5d 20 73 6f 20 74 68  tatements] so th
158e0 61 74 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63  at the.** applic
158f0 61 74 69 6f 6e 20 69 73 20 66 72 65 65 20 74 6f  ation is free to
15900 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 5b 64   use the same [d
15910 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
15920 6f 6e 5d 20 6f 72 20 74 68 65 0a 2a 2a 20 73 61  on] or the.** sa
15930 6d 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  me [prepared sta
15940 74 65 6d 65 6e 74 5d 20 69 6e 20 64 69 66 66 65  tement] in diffe
15950 72 65 6e 74 20 74 68 72 65 61 64 73 20 61 74 20  rent threads at 
15960 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a  the same time..*
15970 2a 20 5e 49 66 20 53 51 4c 69 74 65 20 69 73 20  * ^If SQLite is 
15980 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a  compiled with.**
15990 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52   the [SQLITE_THR
159a0 45 41 44 53 41 46 45 20 7c 20 53 51 4c 49 54 45  EADSAFE | SQLITE
159b0 5f 54 48 52 45 41 44 53 41 46 45 3d 30 5d 20 63  _THREADSAFE=0] c
159c0 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69  ompile-time opti
159d0 6f 6e 20 74 68 65 6e 0a 2a 2a 20 69 74 20 69 73  on then.** it is
159e0 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
159f0 20 73 65 74 20 74 68 65 20 53 65 72 69 61 6c 69   set the Seriali
15a00 7a 65 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d  zed [threading m
15a10 6f 64 65 5d 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c  ode] and.** [sql
15a20 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77  ite3_config()] w
15a30 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 4c 49  ill return [SQLI
15a40 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 63 61 6c  TE_ERROR] if cal
15a50 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20  led with the.** 
15a60 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45  SQLITE_CONFIG_SE
15a70 52 49 41 4c 49 5a 45 44 20 63 6f 6e 66 69 67 75  RIALIZED configu
15a80 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 2e 3c 2f  ration option.</
15a90 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49  dd>.**.** [[SQLI
15aa0 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43  TE_CONFIG_MALLOC
15ab0 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ]] <dt>SQLITE_CO
15ac0 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c 2f 64 74 3e  NFIG_MALLOC</dt>
15ad0 0a 2a 2a 20 3c 64 64 3e 20 5e 28 54 68 69 73 20  .** <dd> ^(This 
15ae0 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73  option takes a s
15af0 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77  ingle argument w
15b00 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65  hich is a pointe
15b10 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61  r to an.** insta
15b20 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69  nce of the [sqli
15b30 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d  te3_mem_methods]
15b40 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
15b50 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66   argument specif
15b60 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69  ies.** alternati
15b70 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d  ve low-level mem
15b80 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
15b90 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75 73  outines to be us
15ba0 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  ed in place of.*
15bb0 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  * the memory all
15bc0 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73  ocation routines
15bd0 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69   built into SQLi
15be0 74 65 2e 29 5e 20 5e 53 51 4c 69 74 65 20 6d 61  te.)^ ^SQLite ma
15bf0 6b 65 73 0a 2a 2a 20 69 74 73 20 6f 77 6e 20 70  kes.** its own p
15c00 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 74  rivate copy of t
15c10 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  he content of th
15c20 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  e [sqlite3_mem_m
15c30 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72  ethods] structur
15c40 65 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20  e.** before the 
15c50 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28  [sqlite3_config(
15c60 29 5d 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e  )] call returns.
15c70 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51  </dd>.**.** [[SQ
15c80 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d  LITE_CONFIG_GETM
15c90 41 4c 4c 4f 43 5d 5d 20 3c 64 74 3e 53 51 4c 49  ALLOC]] <dt>SQLI
15ca0 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c  TE_CONFIG_GETMAL
15cb0 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  LOC</dt>.** <dd>
15cc0 20 5e 28 54 68 69 73 20 6f 70 74 69 6f 6e 20 74   ^(This option t
15cd0 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72  akes a single ar
15ce0 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20  gument which is 
15cf0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a  a pointer to an.
15d00 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ** instance of t
15d10 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  he [sqlite3_mem_
15d20 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 75  methods] structu
15d30 72 65 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65  re.  The [sqlite
15d40 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 0a 2a  3_mem_methods].*
15d50 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 66  * structure is f
15d60 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63  illed with the c
15d70 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64  urrently defined
15d80 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
15d90 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 29 5e 0a 2a  on routines.)^.*
15da0 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61  * This option ca
15db0 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65  n be used to ove
15dc0 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c  rload the defaul
15dd0 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  t memory allocat
15de0 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20  ion.** routines 
15df0 77 69 74 68 20 61 20 77 72 61 70 70 65 72 20 74  with a wrapper t
15e00 68 61 74 20 73 69 6d 75 6c 61 74 69 6f 6e 73 20  hat simulations 
15e10 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
15e20 6e 20 66 61 69 6c 75 72 65 20 6f 72 0a 2a 2a 20  n failure or.** 
15e30 74 72 61 63 6b 73 20 6d 65 6d 6f 72 79 20 75 73  tracks memory us
15e40 61 67 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  age, for example
15e50 2e 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b  . </dd>.**.** [[
15e60 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45  SQLITE_CONFIG_ME
15e70 4d 53 54 41 54 55 53 5d 5d 20 3c 64 74 3e 53 51  MSTATUS]] <dt>SQ
15e80 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53  LITE_CONFIG_MEMS
15e90 54 41 54 55 53 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  TATUS</dt>.** <d
15ea0 64 3e 20 5e 54 68 69 73 20 6f 70 74 69 6f 6e 20  d> ^This option 
15eb0 74 61 6b 65 73 20 73 69 6e 67 6c 65 20 61 72 67  takes single arg
15ec0 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20 69 6e  ument of type in
15ed0 74 2c 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  t, interpreted a
15ee0 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c  s a .** boolean,
15ef0 20 77 68 69 63 68 20 65 6e 61 62 6c 65 73 20 6f   which enables o
15f00 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63  r disables the c
15f10 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 6d 65 6d  ollection of mem
15f20 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 0a  ory allocation .
15f30 2a 2a 20 73 74 61 74 69 73 74 69 63 73 2e 20 5e  ** statistics. ^
15f40 28 57 68 65 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c  (When memory all
15f50 6f 63 61 74 69 6f 6e 20 73 74 61 74 69 73 74 69  ocation statisti
15f60 63 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 2c  cs are disabled,
15f70 20 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69   the .** followi
15f80 6e 67 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  ng SQLite interf
15f90 61 63 65 73 20 62 65 63 6f 6d 65 20 6e 6f 6e 2d  aces become non-
15fa0 6f 70 65 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a 20  operational:.** 
15fb0 20 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e    <ul>.**   <li>
15fc0 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79   [sqlite3_memory
15fd0 5f 75 73 65 64 28 29 5d 0a 2a 2a 20 20 20 3c 6c  _used()].**   <l
15fe0 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  i> [sqlite3_memo
15ff0 72 79 5f 68 69 67 68 77 61 74 65 72 28 29 5d 0a  ry_highwater()].
16000 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74  **   <li> [sqlit
16010 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d  e3_soft_heap_lim
16020 69 74 36 34 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69  it64()].**   <li
16030 3e 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75  > [sqlite3_statu
16040 73 28 29 5d 0a 2a 2a 20 20 20 3c 2f 75 6c 3e 29  s()].**   </ul>)
16050 5e 0a 2a 2a 20 5e 4d 65 6d 6f 72 79 20 61 6c 6c  ^.** ^Memory all
16060 6f 63 61 74 69 6f 6e 20 73 74 61 74 69 73 74 69  ocation statisti
16070 63 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20 62  cs are enabled b
16080 79 20 64 65 66 61 75 6c 74 20 75 6e 6c 65 73 73  y default unless
16090 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 63 6f   SQLite is.** co
160a0 6d 70 69 6c 65 64 20 77 69 74 68 20 5b 53 51 4c  mpiled with [SQL
160b0 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53  ITE_DEFAULT_MEMS
160c0 54 41 54 55 53 5d 3d 30 20 69 6e 20 77 68 69 63  TATUS]=0 in whic
160d0 68 20 63 61 73 65 20 6d 65 6d 6f 72 79 0a 2a 2a  h case memory.**
160e0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 74 61 74   allocation stat
160f0 69 73 74 69 63 73 20 61 72 65 20 64 69 73 61 62  istics are disab
16100 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 0a  led by default..
16110 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b  ** </dd>.**.** [
16120 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53  [SQLITE_CONFIG_S
16130 43 52 41 54 43 48 5d 5d 20 3c 64 74 3e 53 51 4c  CRATCH]] <dt>SQL
16140 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
16150 43 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 20  CH</dt>.** <dd> 
16160 5e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 65  ^This option spe
16170 63 69 66 69 65 73 20 61 20 73 74 61 74 69 63 20  cifies a static 
16180 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 68  memory buffer th
16190 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 75 73  at SQLite can us
161a0 65 20 66 6f 72 0a 2a 2a 20 73 63 72 61 74 63 68  e for.** scratch
161b0 20 6d 65 6d 6f 72 79 2e 20 20 54 68 65 72 65 20   memory.  There 
161c0 61 72 65 20 74 68 72 65 65 20 61 72 67 75 6d 65  are three argume
161d0 6e 74 73 3a 20 20 41 20 70 6f 69 6e 74 65 72 20  nts:  A pointer 
161e0 61 6e 20 38 2d 62 79 74 65 0a 2a 2a 20 61 6c 69  an 8-byte.** ali
161f0 67 6e 65 64 20 6d 65 6d 6f 72 79 20 62 75 66 66  gned memory buff
16200 65 72 20 66 72 6f 6d 20 77 68 69 63 68 20 74 68  er from which th
16210 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61  e scratch alloca
16220 74 69 6f 6e 73 20 77 69 6c 6c 20 62 65 0a 2a 2a  tions will be.**
16230 20 64 72 61 77 6e 2c 20 74 68 65 20 73 69 7a 65   drawn, the size
16240 20 6f 66 20 65 61 63 68 20 73 63 72 61 74 63 68   of each scratch
16250 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 73 7a 29   allocation (sz)
16260 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6d 61 78  ,.** and the max
16270 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 73  imum number of s
16280 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f  cratch allocatio
16290 6e 73 20 28 4e 29 2e 20 20 54 68 65 20 73 7a 0a  ns (N).  The sz.
162a0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74  ** argument must
162b0 20 62 65 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f   be a multiple o
162c0 66 20 31 36 2e 0a 2a 2a 20 54 68 65 20 66 69 72  f 16..** The fir
162d0 73 74 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74  st argument must
162e0 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   be a pointer to
162f0 20 61 6e 20 38 2d 62 79 74 65 20 61 6c 69 67 6e   an 8-byte align
16300 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 6f 66 20  ed buffer.** of 
16310 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 62 79  at least sz*N by
16320 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a  tes of memory..*
16330 2a 20 5e 53 51 4c 69 74 65 20 77 69 6c 6c 20 75  * ^SQLite will u
16340 73 65 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20  se no more than 
16350 74 77 6f 20 73 63 72 61 74 63 68 20 62 75 66 66  two scratch buff
16360 65 72 73 20 70 65 72 20 74 68 72 65 61 64 2e 20  ers per thread. 
16370 20 53 6f 0a 2a 2a 20 4e 20 73 68 6f 75 6c 64 20   So.** N should 
16380 62 65 20 73 65 74 20 74 6f 20 74 77 69 63 65 20  be set to twice 
16390 74 68 65 20 65 78 70 65 63 74 65 64 20 6d 61 78  the expected max
163a0 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74  imum number of t
163b0 68 72 65 61 64 73 2e 0a 2a 2a 20 5e 53 51 4c 69  hreads..** ^SQLi
163c0 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 72 65  te will never re
163d0 71 75 69 72 65 20 61 20 73 63 72 61 74 63 68 20  quire a scratch 
163e0 62 75 66 66 65 72 20 74 68 61 74 20 69 73 20 6d  buffer that is m
163f0 6f 72 65 20 74 68 61 6e 20 36 0a 2a 2a 20 74 69  ore than 6.** ti
16400 6d 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65  mes the database
16410 20 70 61 67 65 20 73 69 7a 65 2e 20 5e 49 66 20   page size. ^If 
16420 53 51 4c 69 74 65 20 6e 65 65 64 73 20 6e 65 65  SQLite needs nee
16430 64 73 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a  ds additional.**
16440 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20   scratch memory 
16450 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20 70  beyond what is p
16460 72 6f 76 69 64 65 64 20 62 79 20 74 68 69 73 20  rovided by this 
16470 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
16480 74 69 6f 6e 2c 20 74 68 65 6e 20 0a 2a 2a 20 5b  tion, then .** [
16490 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
164a0 5d 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74  ] will be used t
164b0 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d  o obtain the mem
164c0 6f 72 79 20 6e 65 65 64 65 64 2e 3c 2f 64 64 3e  ory needed.</dd>
164d0 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f  .**.** [[SQLITE_
164e0 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
164f0 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ]] <dt>SQLITE_CO
16500 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 3c 2f  NFIG_PAGECACHE</
16510 64 74 3e 0a 2a 2a 20 3c 64 64 3e 20 5e 54 68 69  dt>.** <dd> ^Thi
16520 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69  s option specifi
16530 65 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f  es a static memo
16540 72 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53  ry buffer that S
16550 51 4c 69 74 65 20 63 61 6e 20 75 73 65 20 66 6f  QLite can use fo
16560 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  r.** the databas
16570 65 20 70 61 67 65 20 63 61 63 68 65 20 77 69 74  e page cache wit
16580 68 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 61  h the default pa
16590 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65  ge cache impleme
165a0 6e 74 61 74 69 6f 6e 2e 20 20 0a 2a 2a 20 54 68  ntation.  .** Th
165b0 69 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  is configuration
165c0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75   should not be u
165d0 73 65 64 20 69 66 20 61 6e 20 61 70 70 6c 69 63  sed if an applic
165e0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 20 70 61 67  ation-define pag
165f0 65 0a 2a 2a 20 63 61 63 68 65 20 69 6d 70 6c 65  e.** cache imple
16600 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6c 6f 61  mentation is loa
16610 64 65 64 20 75 73 69 6e 67 20 74 68 65 20 53 51  ded using the SQ
16620 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43  LITE_CONFIG_PCAC
16630 48 45 32 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 54  HE2 option..** T
16640 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20 61  here are three a
16650 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73  rguments to this
16660 20 6f 70 74 69 6f 6e 3a 20 41 20 70 6f 69 6e 74   option: A point
16670 65 72 20 74 6f 20 38 2d 62 79 74 65 20 61 6c 69  er to 8-byte ali
16680 67 6e 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20  gned.** memory, 
16690 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68  the size of each
166a0 20 70 61 67 65 20 62 75 66 66 65 72 20 28 73 7a   page buffer (sz
166b0 29 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65  ), and the numbe
166c0 72 20 6f 66 20 70 61 67 65 73 20 28 4e 29 2e 0a  r of pages (N)..
166d0 2a 2a 20 54 68 65 20 73 7a 20 61 72 67 75 6d 65  ** The sz argume
166e0 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65  nt should be the
166f0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72   size of the lar
16700 67 65 73 74 20 64 61 74 61 62 61 73 65 20 70 61  gest database pa
16710 67 65 0a 2a 2a 20 28 61 20 70 6f 77 65 72 20 6f  ge.** (a power o
16720 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31  f two between 51
16730 32 20 61 6e 64 20 33 32 37 36 38 29 20 70 6c 75  2 and 32768) plu
16740 73 20 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61  s a little extra
16750 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 70 61 67   for each.** pag
16760 65 20 68 65 61 64 65 72 2e 20 20 5e 54 68 65 20  e header.  ^The 
16770 70 61 67 65 20 68 65 61 64 65 72 20 73 69 7a 65  page header size
16780 20 69 73 20 32 30 20 74 6f 20 34 30 20 62 79 74   is 20 to 40 byt
16790 65 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 0a  es depending on.
167a0 2a 2a 20 74 68 65 20 68 6f 73 74 20 61 72 63 68  ** the host arch
167b0 69 74 65 63 74 75 72 65 2e 20 20 5e 49 74 20 69  itecture.  ^It i
167c0 73 20 68 61 72 6d 6c 65 73 73 2c 20 61 70 61 72  s harmless, apar
167d0 74 20 66 72 6f 6d 20 74 68 65 20 77 61 73 74 65  t from the waste
167e0 64 20 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 6f 20  d memory,.** to 
167f0 6d 61 6b 65 20 73 7a 20 61 20 6c 69 74 74 6c 65  make sz a little
16800 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 54 68 65   too large.  The
16810 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65   first.** argume
16820 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20  nt should point 
16830 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  to an allocation
16840 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a   of at least sz*
16850 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  N bytes of memor
16860 79 2e 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 77 69  y..** ^SQLite wi
16870 6c 6c 20 75 73 65 20 74 68 65 20 6d 65 6d 6f 72  ll use the memor
16880 79 20 70 72 6f 76 69 64 65 64 20 62 79 20 74 68  y provided by th
16890 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
168a0 20 74 6f 20 73 61 74 69 73 66 79 20 69 74 73 0a   to satisfy its.
168b0 2a 2a 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20  ** memory needs 
168c0 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e 20  for the first N 
168d0 70 61 67 65 73 20 74 68 61 74 20 69 74 20 61 64  pages that it ad
168e0 64 73 20 74 6f 20 63 61 63 68 65 2e 20 20 5e 49  ds to cache.  ^I
168f0 66 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  f additional.** 
16900 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72  page cache memor
16910 79 20 69 73 20 6e 65 65 64 65 64 20 62 65 79 6f  y is needed beyo
16920 6e 64 20 77 68 61 74 20 69 73 20 70 72 6f 76 69  nd what is provi
16930 64 65 64 20 62 79 20 74 68 69 73 20 6f 70 74 69  ded by this opti
16940 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69  on, then.** SQLi
16950 74 65 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69  te goes to [sqli
16960 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f  te3_malloc()] fo
16970 72 20 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c  r the additional
16980 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a   storage space..
16990 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69  ** The pointer i
169a0 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  n the first argu
169b0 6d 65 6e 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20  ment must.** be 
169c0 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d  aligned to an 8-
169d0 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 6f 72  byte boundary or
169e0 20 73 75 62 73 65 71 75 65 6e 74 20 62 65 68 61   subsequent beha
169f0 76 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 0a 2a  vior of SQLite.*
16a00 2a 20 77 69 6c 6c 20 62 65 20 75 6e 64 65 66 69  * will be undefi
16a10 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ned.</dd>.**.** 
16a20 5b 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  [[SQLITE_CONFIG_
16a30 48 45 41 50 5d 5d 20 3c 64 74 3e 53 51 4c 49 54  HEAP]] <dt>SQLIT
16a40 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 3c 2f 64  E_CONFIG_HEAP</d
16a50 74 3e 0a 2a 2a 20 3c 64 64 3e 20 5e 54 68 69 73  t>.** <dd> ^This
16a60 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65   option specifie
16a70 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72  s a static memor
16a80 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53 51  y buffer that SQ
16a90 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a  Lite will use.**
16aa0 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20   for all of its 
16ab0 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 61  dynamic memory a
16ac0 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 20  llocation needs 
16ad0 62 65 79 6f 6e 64 20 74 68 6f 73 65 20 70 72 6f  beyond those pro
16ae0 76 69 64 65 64 0a 2a 2a 20 66 6f 72 20 62 79 20  vided.** for by 
16af0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53  [SQLITE_CONFIG_S
16b00 43 52 41 54 43 48 5d 20 61 6e 64 20 5b 53 51 4c  CRATCH] and [SQL
16b10 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43  ITE_CONFIG_PAGEC
16b20 41 43 48 45 5d 2e 0a 2a 2a 20 54 68 65 72 65 20  ACHE]..** There 
16b30 61 72 65 20 74 68 72 65 65 20 61 72 67 75 6d 65  are three argume
16b40 6e 74 73 3a 20 41 6e 20 38 2d 62 79 74 65 20 61  nts: An 8-byte a
16b50 6c 69 67 6e 65 64 20 70 6f 69 6e 74 65 72 20 74  ligned pointer t
16b60 6f 20 74 68 65 20 6d 65 6d 6f 72 79 2c 0a 2a 2a  o the memory,.**
16b70 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
16b80 79 74 65 73 20 69 6e 20 74 68 65 20 6d 65 6d 6f  ytes in the memo
16b90 72 79 20 62 75 66 66 65 72 2c 20 61 6e 64 20 74  ry buffer, and t
16ba0 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63  he minimum alloc
16bb0 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2a 20 5e  ation size..** ^
16bc0 49 66 20 74 68 65 20 66 69 72 73 74 20 70 6f 69  If the first poi
16bd0 6e 74 65 72 20 28 74 68 65 20 6d 65 6d 6f 72 79  nter (the memory
16be0 20 70 6f 69 6e 74 65 72 29 20 69 73 20 4e 55 4c   pointer) is NUL
16bf0 4c 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 72  L, then SQLite r
16c00 65 76 65 72 74 73 0a 2a 2a 20 74 6f 20 75 73 69  everts.** to usi
16c10 6e 67 20 69 74 73 20 64 65 66 61 75 6c 74 20 6d  ng its default m
16c20 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
16c30 28 74 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c  (the system mall
16c40 6f 63 28 29 20 69 6d 70 6c 65 6d 65 6e 74 61 74  oc() implementat
16c50 69 6f 6e 29 2c 0a 2a 2a 20 75 6e 64 6f 69 6e 67  ion),.** undoing
16c60 20 61 6e 79 20 70 72 69 6f 72 20 69 6e 76 6f 63   any prior invoc
16c70 61 74 69 6f 6e 20 6f 66 20 5b 53 51 4c 49 54 45  ation of [SQLITE
16c80 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2e  _CONFIG_MALLOC].
16c90 20 20 5e 49 66 20 74 68 65 0a 2a 2a 20 6d 65 6d    ^If the.** mem
16ca0 6f 72 79 20 70 6f 69 6e 74 65 72 20 69 73 20 6e  ory pointer is n
16cb0 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 65 69 74 68  ot NULL and eith
16cc0 65 72 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c  er [SQLITE_ENABL
16cd0 45 5f 4d 45 4d 53 59 53 33 5d 20 6f 72 0a 2a 2a  E_MEMSYS3] or.**
16ce0 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f   [SQLITE_ENABLE_
16cf0 4d 45 4d 53 59 53 35 5d 20 61 72 65 20 64 65 66  MEMSYS5] are def
16d00 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 61  ined, then the a
16d10 6c 74 65 72 6e 61 74 69 76 65 20 6d 65 6d 6f 72  lternative memor
16d20 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 20 69  y.** allocator i
16d30 73 20 65 6e 67 61 67 65 64 20 74 6f 20 68 61 6e  s engaged to han
16d40 64 6c 65 20 61 6c 6c 20 6f 66 20 53 51 4c 69 74  dle all of SQLit
16d50 65 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  es memory alloca
16d60 74 69 6f 6e 20 6e 65 65 64 73 2e 0a 2a 2a 20 54  tion needs..** T
16d70 68 65 20 66 69 72 73 74 20 70 6f 69 6e 74 65 72  he first pointer
16d80 20 28 74 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69   (the memory poi
16d90 6e 74 65 72 29 20 6d 75 73 74 20 62 65 20 61 6c  nter) must be al
16da0 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79  igned to an 8-by
16db0 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 20 6f  te.** boundary o
16dc0 72 20 73 75 62 73 65 71 75 65 6e 74 20 62 65 68  r subsequent beh
16dd0 61 76 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 20  avior of SQLite 
16de0 77 69 6c 6c 20 62 65 20 75 6e 64 65 66 69 6e 65  will be undefine
16df0 64 2e 0a 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75  d..** The minimu
16e00 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a  m allocation siz
16e10 65 20 69 73 20 63 61 70 70 65 64 20 61 74 20 32  e is capped at 2
16e20 2a 2a 31 32 2e 20 52 65 61 73 6f 6e 61 62 6c 65  **12. Reasonable
16e30 20 76 61 6c 75 65 73 0a 2a 2a 20 66 6f 72 20 74   values.** for t
16e40 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63  he minimum alloc
16e50 61 74 69 6f 6e 20 73 69 7a 65 20 61 72 65 20 32  ation size are 2
16e60 2a 2a 35 20 74 68 72 6f 75 67 68 20 32 2a 2a 38  **5 through 2**8
16e70 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b 53  .</dd>.**.** [[S
16e80 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54  QLITE_CONFIG_MUT
16e90 45 58 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f  EX]] <dt>SQLITE_
16ea0 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3c 2f 64 74  CONFIG_MUTEX</dt
16eb0 3e 0a 2a 2a 20 3c 64 64 3e 20 5e 28 54 68 69 73  >.** <dd> ^(This
16ec0 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20   option takes a 
16ed0 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20  single argument 
16ee0 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74  which is a point
16ef0 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74  er to an.** inst
16f00 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c  ance of the [sql
16f10 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
16f20 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20  ds] structure.  
16f30 54 68 65 20 61 72 67 75 6d 65 6e 74 20 73 70 65  The argument spe
16f40 63 69 66 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e  cifies.** altern
16f50 61 74 69 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20  ative low-level 
16f60 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 74  mutex routines t
16f70 6f 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61  o be used in pla
16f80 63 65 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20  ce.** the mutex 
16f90 72 6f 75 74 69 6e 65 73 20 62 75 69 6c 74 20 69  routines built i
16fa0 6e 74 6f 20 53 51 4c 69 74 65 2e 29 5e 20 20 5e  nto SQLite.)^  ^
16fb0 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 63  SQLite makes a c
16fc0 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f  opy of the.** co
16fd0 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 5b 73 71  ntent of the [sq
16fe0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
16ff0 6f 64 73 5d 20 73 74 72 75 63 74 75 72 65 20 62  ods] structure b
17000 65 66 6f 72 65 20 74 68 65 20 63 61 6c 6c 20 74  efore the call t
17010 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f  o.** [sqlite3_co
17020 6e 66 69 67 28 29 5d 20 72 65 74 75 72 6e 73 2e  nfig()] returns.
17030 20 5e 49 66 20 53 51 4c 69 74 65 20 69 73 20 63   ^If SQLite is c
17040 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20  ompiled with.** 
17050 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45  the [SQLITE_THRE
17060 41 44 53 41 46 45 20 7c 20 53 51 4c 49 54 45 5f  ADSAFE | SQLITE_
17070 54 48 52 45 41 44 53 41 46 45 3d 30 5d 20 63 6f  THREADSAFE=0] co
17080 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f  mpile-time optio
17090 6e 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 65 6e  n then.** the en
170a0 74 69 72 65 20 6d 75 74 65 78 69 6e 67 20 73 75  tire mutexing su
170b0 62 73 79 73 74 65 6d 20 69 73 20 6f 6d 69 74 74  bsystem is omitt
170c0 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c  ed from the buil
170d0 64 20 61 6e 64 20 68 65 6e 63 65 20 63 61 6c 6c  d and hence call
170e0 73 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  s to.** [sqlite3
170f0 5f 63 6f 6e 66 69 67 28 29 5d 20 77 69 74 68 20  _config()] with 
17100 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  the SQLITE_CONFI
17110 47 5f 4d 55 54 45 58 20 63 6f 6e 66 69 67 75 72  G_MUTEX configur
17120 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 77 69 6c  ation option wil
17130 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51 4c  l.** return [SQL
17140 49 54 45 5f 45 52 52 4f 52 5d 2e 3c 2f 64 64 3e  ITE_ERROR].</dd>
17150 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f  .**.** [[SQLITE_
17160 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 5d  CONFIG_GETMUTEX]
17170 5d 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  ] <dt>SQLITE_CON
17180 46 49 47 5f 47 45 54 4d 55 54 45 58 3c 2f 64 74  FIG_GETMUTEX</dt
17190 3e 0a 2a 2a 20 3c 64 64 3e 20 5e 28 54 68 69 73  >.** <dd> ^(This
171a0 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20   option takes a 
171b0 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20  single argument 
171c0 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74  which is a point
171d0 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74  er to an.** inst
171e0 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c  ance of the [sql
171f0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
17200 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20  ds] structure.  
17210 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  The.** [sqlite3_
17220 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a  mutex_methods].*
17230 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 66  * structure is f
17240 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63  illed with the c
17250 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64  urrently defined
17260 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e   mutex routines.
17270 29 5e 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f  )^.** This optio
17280 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  n can be used to
17290 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65   overload the de
172a0 66 61 75 6c 74 20 6d 75 74 65 78 20 61 6c 6c 6f  fault mutex allo
172b0 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e  cation.** routin
172c0 65 73 20 77 69 74 68 20 61 20 77 72 61 70 70 65  es with a wrappe
172d0 72 20 75 73 65 64 20 74 6f 20 74 72 61 63 6b 20  r used to track 
172e0 6d 75 74 65 78 20 75 73 61 67 65 20 66 6f 72 20  mutex usage for 
172f0 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70  performance.** p
17300 72 6f 66 69 6c 69 6e 67 20 6f 72 20 74 65 73 74  rofiling or test
17310 69 6e 67 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  ing, for example
17320 2e 20 20 20 5e 49 66 20 53 51 4c 69 74 65 20 69  .   ^If SQLite i
17330 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a  s compiled with.
17340 2a 2a 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54  ** the [SQLITE_T
17350 48 52 45 41 44 53 41 46 45 20 7c 20 53 51 4c 49  HREADSAFE | SQLI
17360 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 30 5d  TE_THREADSAFE=0]
17370 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70   compile-time op
17380 74 69 6f 6e 20 74 68 65 6e 0a 2a 2a 20 74 68 65  tion then.** the
17390 20 65 6e 74 69 72 65 20 6d 75 74 65 78 69 6e 67   entire mutexing
173a0 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 6f 6d   subsystem is om
173b0 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 62  itted from the b
173c0 75 69 6c 64 20 61 6e 64 20 68 65 6e 63 65 20 63  uild and hence c
173d0 61 6c 6c 73 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69  alls to.** [sqli
173e0 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 69  te3_config()] wi
173f0 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f  th the SQLITE_CO
17400 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 20 63 6f  NFIG_GETMUTEX co
17410 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
17420 6f 6e 20 77 69 6c 6c 0a 2a 2a 20 72 65 74 75 72  on will.** retur
17430 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d  n [SQLITE_ERROR]
17440 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b 53  .</dd>.**.** [[S
17450 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f  QLITE_CONFIG_LOO
17460 4b 41 53 49 44 45 5d 5d 20 3c 64 74 3e 53 51 4c  KASIDE]] <dt>SQL
17470 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41  ITE_CONFIG_LOOKA
17480 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  SIDE</dt>.** <dd
17490 3e 20 5e 28 54 68 69 73 20 6f 70 74 69 6f 6e 20  > ^(This option 
174a0 74 61 6b 65 73 20 74 77 6f 20 61 72 67 75 6d 65  takes two argume
174b0 6e 74 73 20 74 68 61 74 20 64 65 74 65 72 6d 69  nts that determi
174c0 6e 65 20 74 68 65 20 64 65 66 61 75 6c 74 0a 2a  ne the default.*
174d0 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  * memory allocat
174e0 69 6f 6e 20 66 6f 72 20 74 68 65 20 6c 6f 6f 6b  ion for the look
174f0 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  aside memory all
17500 6f 63 61 74 6f 72 20 6f 6e 20 65 61 63 68 0a 2a  ocator on each.*
17510 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  * [database conn
17520 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65 20 66 69  ection].  The fi
17530 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20  rst argument is 
17540 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65  the.** size of e
17550 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75  ach lookaside bu
17560 66 66 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68  ffer slot and th
17570 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20  e second is the 
17580 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f  number of.** slo
17590 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20  ts allocated to 
175a0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f  each database co
175b0 6e 6e 65 63 74 69 6f 6e 2e 29 5e 20 20 5e 28 54  nnection.)^  ^(T
175c0 68 69 73 20 6f 70 74 69 6f 6e 20 73 65 74 73 20  his option sets 
175d0 74 68 65 0a 2a 2a 20 3c 69 3e 64 65 66 61 75 6c  the.** <i>defaul
175e0 74 3c 2f 69 3e 20 6c 6f 6f 6b 61 73 69 64 65 20  t</i> lookaside 
175f0 73 69 7a 65 2e 20 54 68 65 20 5b 53 51 4c 49 54  size. The [SQLIT
17600 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41  E_DBCONFIG_LOOKA
17610 53 49 44 45 5d 0a 2a 2a 20 76 65 72 62 20 74 6f  SIDE].** verb to
17620 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e   [sqlite3_db_con
17630 66 69 67 28 29 5d 20 63 61 6e 20 62 65 20 75 73  fig()] can be us
17640 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65  ed to change the
17650 20 6c 6f 6f 6b 61 73 69 64 65 0a 2a 2a 20 63 6f   lookaside.** co
17660 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 6e 20 69  nfiguration on i
17670 6e 64 69 76 69 64 75 61 6c 20 63 6f 6e 6e 65 63  ndividual connec
17680 74 69 6f 6e 73 2e 29 5e 20 3c 2f 64 64 3e 0a 2a  tions.)^ </dd>.*
17690 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43 4f  *.** [[SQLITE_CO
176a0 4e 46 49 47 5f 50 43 41 43 48 45 32 5d 5d 20 3c  NFIG_PCACHE2]] <
176b0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
176c0 5f 50 43 41 43 48 45 32 3c 2f 64 74 3e 0a 2a 2a  _PCACHE2</dt>.**
176d0 20 3c 64 64 3e 20 5e 28 54 68 69 73 20 6f 70 74   <dd> ^(This opt
176e0 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ion takes a sing
176f0 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63  le argument whic
17700 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  h is a pointer t
17710 6f 0a 2a 2a 20 61 6e 20 5b 73 71 6c 69 74 65 33  o.** an [sqlite3
17720 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 32  _pcache_methods2
17730 5d 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20  ] object.  This 
17740 6f 62 6a 65 63 74 20 73 70 65 63 69 66 69 65 73  object specifies
17750 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 0a 2a   the interface.*
17760 2a 20 74 6f 20 61 20 63 75 73 74 6f 6d 20 70 61  * to a custom pa
17770 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65  ge cache impleme
17780 6e 74 61 74 69 6f 6e 2e 29 5e 20 20 5e 53 51 4c  ntation.)^  ^SQL
17790 69 74 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79  ite makes a copy
177a0 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 62 6a 65 63   of the.** objec
177b0 74 20 61 6e 64 20 75 73 65 73 20 69 74 20 66 6f  t and uses it fo
177c0 72 20 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d  r page cache mem
177d0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e  ory allocations.
177e0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51  </dd>.**.** [[SQ
177f0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50  LITE_CONFIG_GETP
17800 43 41 43 48 45 32 5d 5d 20 3c 64 74 3e 53 51 4c  CACHE2]] <dt>SQL
17810 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43  ITE_CONFIG_GETPC
17820 41 43 48 45 32 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  ACHE2</dt>.** <d
17830 64 3e 20 5e 28 54 68 69 73 20 6f 70 74 69 6f 6e  d> ^(This option
17840 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20   takes a single 
17850 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69  argument which i
17860 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
17870 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 63  n.** [sqlite3_pc
17880 61 63 68 65 5f 6d 65 74 68 6f 64 73 32 5d 20 6f  ache_methods2] o
17890 62 6a 65 63 74 2e 20 20 53 51 4c 69 74 65 20 63  bject.  SQLite c
178a0 6f 70 69 65 73 20 6f 66 20 74 68 65 20 63 75 72  opies of the cur
178b0 72 65 6e 74 0a 2a 2a 20 70 61 67 65 20 63 61 63  rent.** page cac
178c0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
178d0 6e 20 69 6e 74 6f 20 74 68 61 74 20 6f 62 6a 65  n into that obje
178e0 63 74 2e 29 5e 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a  ct.)^ </dd>.**.*
178f0 2a 20 5b 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  * [[SQLITE_CONFI
17900 47 5f 4c 4f 47 5d 5d 20 3c 64 74 3e 53 51 4c 49  G_LOG]] <dt>SQLI
17910 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 47 3c 2f 64  TE_CONFIG_LOG</d
17920 74 3e 0a 2a 2a 20 3c 64 64 3e 20 5e 54 68 65 20  t>.** <dd> ^The 
17930 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f  SQLITE_CONFIG_LO
17940 47 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 74  G option takes t
17950 77 6f 20 61 72 67 75 6d 65 6e 74 73 3a 20 61 20  wo arguments: a 
17960 70 6f 69 6e 74 65 72 20 74 6f 20 61 0a 2a 2a 20  pointer to a.** 
17970 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20  function with a 
17980 63 61 6c 6c 20 73 69 67 6e 61 74 75 72 65 20 6f  call signature o
17990 66 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c  f void(*)(void*,
179a0 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  int,const char*)
179b0 2c 20 0a 2a 2a 20 61 6e 64 20 61 20 70 6f 69 6e  , .** and a poin
179c0 74 65 72 20 74 6f 20 76 6f 69 64 2e 20 5e 49 66  ter to void. ^If
179d0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 6f   the function po
179e0 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c  inter is not NUL
179f0 4c 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e 76 6f  L, it is.** invo
17a00 6b 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  ked by [sqlite3_
17a10 6c 6f 67 28 29 5d 20 74 6f 20 70 72 6f 63 65 73  log()] to proces
17a20 73 20 65 61 63 68 20 6c 6f 67 67 69 6e 67 20 65  s each logging e
17a30 76 65 6e 74 2e 20 20 5e 49 66 20 74 68 65 0a 2a  vent.  ^If the.*
17a40 2a 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74  * function point
17a50 65 72 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20  er is NULL, the 
17a60 5b 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 5d 20  [sqlite3_log()] 
17a70 69 6e 74 65 72 66 61 63 65 20 62 65 63 6f 6d 65  interface become
17a80 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20 5e 54  s a no-op..** ^T
17a90 68 65 20 76 6f 69 64 20 70 6f 69 6e 74 65 72 20  he void pointer 
17aa0 74 68 61 74 20 69 73 20 74 68 65 20 73 65 63 6f  that is the seco
17ab0 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 53  nd argument to S
17ac0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 47  QLITE_CONFIG_LOG
17ad0 20 69 73 0a 2a 2a 20 70 61 73 73 65 64 20 74 68   is.** passed th
17ae0 72 6f 75 67 68 20 61 73 20 74 68 65 20 66 69 72  rough as the fir
17af0 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  st parameter to 
17b00 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
17b10 64 65 66 69 6e 65 64 20 6c 6f 67 67 65 72 0a 2a  defined logger.*
17b20 2a 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 6e 65  * function whene
17b30 76 65 72 20 74 68 61 74 20 66 75 6e 63 74 69 6f  ver that functio
17b40 6e 20 69 73 20 69 6e 76 6f 6b 65 64 2e 20 20 5e  n is invoked.  ^
17b50 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  The second param
17b60 65 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 6c  eter to.** the l
17b70 6f 67 67 65 72 20 66 75 6e 63 74 69 6f 6e 20 69  ogger function i
17b80 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
17b90 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
17ba0 74 6f 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  to the correspon
17bb0 64 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ding.** [sqlite3
17bc0 5f 6c 6f 67 28 29 5d 20 63 61 6c 6c 20 61 6e 64  _log()] call and
17bd0 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20   is intended to 
17be0 62 65 20 61 20 5b 72 65 73 75 6c 74 20 63 6f 64  be a [result cod
17bf0 65 5d 20 6f 72 20 61 6e 0a 2a 2a 20 5b 65 78 74  e] or an.** [ext
17c00 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64  ended result cod
17c10 65 5d 2e 20 20 5e 54 68 65 20 74 68 69 72 64 20  e].  ^The third 
17c20 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 64  parameter passed
17c30 20 74 6f 20 74 68 65 20 6c 6f 67 67 65 72 20 69   to the logger i
17c40 73 0a 2a 2a 20 6c 6f 67 20 6d 65 73 73 61 67 65  s.** log message
17c50 20 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e   after formattin
17c60 67 20 76 69 61 20 5b 73 71 6c 69 74 65 33 5f 73  g via [sqlite3_s
17c70 6e 70 72 69 6e 74 66 28 29 5d 2e 0a 2a 2a 20 54  nprintf()]..** T
17c80 68 65 20 53 51 4c 69 74 65 20 6c 6f 67 67 69 6e  he SQLite loggin
17c90 67 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6e  g interface is n
17ca0 6f 74 20 72 65 65 6e 74 72 61 6e 74 3b 20 74 68  ot reentrant; th
17cb0 65 20 6c 6f 67 67 65 72 20 66 75 6e 63 74 69 6f  e logger functio
17cc0 6e 0a 2a 2a 20 73 75 70 70 6c 69 65 64 20 62 79  n.** supplied by
17cd0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
17ce0 20 6d 75 73 74 20 6e 6f 74 20 69 6e 76 6f 6b 65   must not invoke
17cf0 20 61 6e 79 20 53 51 4c 69 74 65 20 69 6e 74 65   any SQLite inte
17d00 72 66 61 63 65 2e 0a 2a 2a 20 49 6e 20 61 20 6d  rface..** In a m
17d10 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70  ulti-threaded ap
17d20 70 6c 69 63 61 74 69 6f 6e 2c 20 74 68 65 20 61  plication, the a
17d30 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
17d40 65 64 20 6c 6f 67 67 65 72 0a 2a 2a 20 66 75 6e  ed logger.** fun
17d50 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 74 68  ction must be th
17d60 72 65 61 64 73 61 66 65 2e 20 3c 2f 64 64 3e 0a  readsafe. </dd>.
17d70 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43  **.** [[SQLITE_C
17d80 4f 4e 46 49 47 5f 55 52 49 5d 5d 20 3c 64 74 3e  ONFIG_URI]] <dt>
17d90 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 55 52  SQLITE_CONFIG_UR
17da0 49 0a 2a 2a 20 3c 64 64 3e 20 54 68 69 73 20 6f  I.** <dd> This o
17db0 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69  ption takes a si
17dc0 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 6f 66  ngle argument of
17dd0 20 74 79 70 65 20 69 6e 74 2e 20 49 66 20 6e 6f   type int. If no
17de0 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 0a 2a 2a 20  n-zero, then.** 
17df0 55 52 49 20 68 61 6e 64 6c 69 6e 67 20 69 73 20  URI handling is 
17e00 67 6c 6f 62 61 6c 6c 79 20 65 6e 61 62 6c 65 64  globally enabled
17e10 2e 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74  . If the paramet
17e20 65 72 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e  er is zero, then
17e30 20 55 52 49 20 68 61 6e 64 6c 69 6e 67 0a 2a 2a   URI handling.**
17e40 20 69 73 20 67 6c 6f 62 61 6c 6c 79 20 64 69 73   is globally dis
17e50 61 62 6c 65 64 2e 20 49 66 20 55 52 49 20 68 61  abled. If URI ha
17e60 6e 64 6c 69 6e 67 20 69 73 20 67 6c 6f 62 61 6c  ndling is global
17e70 6c 79 20 65 6e 61 62 6c 65 64 2c 20 61 6c 6c 20  ly enabled, all 
17e80 66 69 6c 65 6e 61 6d 65 73 0a 2a 2a 20 70 61 73  filenames.** pas
17e90 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  sed to [sqlite3_
17ea0 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65  open()], [sqlite
17eb0 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 5b 73  3_open_v2()], [s
17ec0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d  qlite3_open16()]
17ed0 20 6f 72 0a 2a 2a 20 73 70 65 63 69 66 69 65 64   or.** specified
17ee0 20 61 73 20 70 61 72 74 20 6f 66 20 5b 41 54 54   as part of [ATT
17ef0 41 43 48 5d 20 63 6f 6d 6d 61 6e 64 73 20 61 72  ACH] commands ar
17f00 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73  e interpreted as
17f10 20 55 52 49 73 2c 20 72 65 67 61 72 64 6c 65 73   URIs, regardles
17f20 73 0a 2a 2a 20 6f 66 20 77 68 65 74 68 65 72 20  s.** of whether 
17f30 6f 72 20 6e 6f 74 20 74 68 65 20 5b 53 51 4c 49  or not the [SQLI
17f40 54 45 5f 4f 50 45 4e 5f 55 52 49 5d 20 66 6c 61  TE_OPEN_URI] fla
17f50 67 20 69 73 20 73 65 74 20 77 68 65 6e 20 74 68  g is set when th
17f60 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  e database.** co
17f70 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e  nnection is open
17f80 65 64 2e 20 49 66 20 69 74 20 69 73 20 67 6c 6f  ed. If it is glo
17f90 62 61 6c 6c 79 20 64 69 73 61 62 6c 65 64 2c 20  bally disabled, 
17fa0 66 69 6c 65 6e 61 6d 65 73 20 61 72 65 0a 2a 2a  filenames are.**
17fb0 20 6f 6e 6c 79 20 69 6e 74 65 72 70 72 65 74 65   only interprete
17fc0 64 20 61 73 20 55 52 49 73 20 69 66 20 74 68 65  d as URIs if the
17fd0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49   SQLITE_OPEN_URI
17fe0 20 66 6c 61 67 20 69 73 20 73 65 74 20 77 68 65   flag is set whe
17ff0 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  n the.** databas
18000 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
18010 6f 70 65 6e 65 64 2e 20 42 79 20 64 65 66 61 75  opened. By defau
18020 6c 74 2c 20 55 52 49 20 68 61 6e 64 6c 69 6e 67  lt, URI handling
18030 20 69 73 20 67 6c 6f 62 61 6c 6c 79 0a 2a 2a 20   is globally.** 
18040 64 69 73 61 62 6c 65 64 2e 20 54 68 65 20 64 65  disabled. The de
18050 66 61 75 6c 74 20 76 61 6c 75 65 20 6d 61 79 20  fault value may 
18060 62 65 20 63 68 61 6e 67 65 64 20 62 79 20 63 6f  be changed by co
18070 6d 70 69 6c 69 6e 67 20 77 69 74 68 20 74 68 65  mpiling with the
18080 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 53 45 5f  .** [SQLITE_USE_
18090 55 52 49 5d 20 73 79 6d 62 6f 6c 20 64 65 66 69  URI] symbol defi
180a0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c  ned..**.** [[SQL
180b0 49 54 45 5f 43 4f 4e 46 49 47 5f 43 4f 56 45 52  ITE_CONFIG_COVER
180c0 49 4e 47 5f 49 4e 44 45 58 5f 53 43 41 4e 5d 5d  ING_INDEX_SCAN]]
180d0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46   <dt>SQLITE_CONF
180e0 49 47 5f 43 4f 56 45 52 49 4e 47 5f 49 4e 44 45  IG_COVERING_INDE
180f0 58 5f 53 43 41 4e 0a 2a 2a 20 3c 64 64 3e 20 54  X_SCAN.** <dd> T
18100 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 73 20  his option taks 
18110 61 20 73 69 6e 67 6c 65 20 69 6e 74 65 67 65 72  a single integer
18120 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20   argument which 
18130 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  is interpreted a
18140 73 0a 2a 2a 20 61 20 62 6f 6f 6c 65 61 6e 20 69  s.** a boolean i
18150 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 61 62 6c  n order to enabl
18160 65 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65  e or disable the
18170 20 75 73 65 20 6f 66 20 63 6f 76 65 72 69 6e 67   use of covering
18180 20 69 6e 64 69 63 65 73 20 66 6f 72 0a 2a 2a 20   indices for.** 
18190 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 73  full table scans
181a0 20 69 6e 20 74 68 65 20 71 75 65 72 79 20 6f 70   in the query op
181b0 74 69 6d 69 7a 65 72 2e 20 20 54 68 65 20 64 65  timizer.  The de
181c0 66 61 75 6c 74 20 73 65 74 74 69 6e 67 20 69 73  fault setting is
181d0 20 64 65 74 65 72 6d 69 6e 65 64 0a 2a 2a 20 62   determined.** b
181e0 79 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 41 4c  y the [SQLITE_AL
181f0 4c 4f 57 5f 43 4f 56 45 52 49 4e 47 5f 49 4e 44  LOW_COVERING_IND
18200 45 58 5f 53 43 41 4e 5d 20 63 6f 6d 70 69 6c 65  EX_SCAN] compile
18210 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2c 20 6f 72  -time option, or
18220 20 69 73 20 22 6f 6e 22 0a 2a 2a 20 69 66 20 74   is "on".** if t
18230 68 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  hat compile-time
18240 20 6f 70 74 69 6f 6e 20 69 73 20 6f 6d 69 74 74   option is omitt
18250 65 64 2e 0a 2a 2a 20 54 68 65 20 61 62 69 6c 69  ed..** The abili
18260 74 79 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68  ty to disable th
18270 65 20 75 73 65 20 6f 66 20 63 6f 76 65 72 69 6e  e use of coverin
18280 67 20 69 6e 64 69 63 65 73 20 66 6f 72 20 66 75  g indices for fu
18290 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 73 0a 2a  ll table scans.*
182a0 2a 20 69 73 20 62 65 63 61 75 73 65 20 73 6f 6d  * is because som
182b0 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 63 6f  e incorrectly co
182c0 64 65 64 20 6c 65 67 61 63 79 20 61 70 70 6c 69  ded legacy appli
182d0 63 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 6d 61  cations might ma
182e0 6c 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 61 6c  lfunction.** mal
182f0 66 75 6e 63 74 69 6f 6e 20 77 68 65 6e 20 74 68  function when th
18300 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  e optimization i
18310 73 20 65 6e 61 62 6c 65 64 2e 20 20 50 72 6f 76  s enabled.  Prov
18320 69 64 69 6e 67 20 74 68 65 20 61 62 69 6c 69 74  iding the abilit
18330 79 20 74 6f 0a 2a 2a 20 64 69 73 61 62 6c 65 20  y to.** disable 
18340 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  the optimization
18350 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 6c 64 65   allows the olde
18360 72 2c 20 62 75 67 67 79 20 61 70 70 6c 69 63 61  r, buggy applica
18370 74 69 6f 6e 20 63 6f 64 65 20 74 6f 20 77 6f 72  tion code to wor
18380 6b 0a 2a 2a 20 77 69 74 68 6f 75 74 20 63 68 61  k.** without cha
18390 6e 67 65 20 65 76 65 6e 20 77 69 74 68 20 6e 65  nge even with ne
183a0 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  wer versions of 
183b0 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 5b 5b  SQLite..**.** [[
183c0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43  SQLITE_CONFIG_PC
183d0 41 43 48 45 5d 5d 20 5b 5b 53 51 4c 49 54 45 5f  ACHE]] [[SQLITE_
183e0 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45  CONFIG_GETPCACHE
183f0 5d 5d 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  ]].** <dt>SQLITE
18400 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 61  _CONFIG_PCACHE a
18410 6e 64 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  nd SQLITE_CONFIG
18420 5f 47 45 54 50 43 41 43 48 45 0a 2a 2a 20 3c 64  _GETPCACHE.** <d
18430 64 3e 20 54 68 65 73 65 20 6f 70 74 69 6f 6e 73  d> These options
18440 20 61 72 65 20 6f 62 73 6f 6c 65 74 65 20 61 6e   are obsolete an
18450 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  d should not be 
18460 75 73 65 64 20 62 79 20 6e 65 77 20 63 6f 64 65  used by new code
18470 2e 0a 2a 2a 20 54 68 65 79 20 61 72 65 20 72 65  ..** They are re
18480 74 61 69 6e 65 64 20 66 6f 72 20 62 61 63 6b 77  tained for backw
18490 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69  ards compatibili
184a0 74 79 20 62 75 74 20 61 72 65 20 6e 6f 77 20 6e  ty but are now n
184b0 6f 2d 6f 70 73 2e 0a 2a 2a 20 3c 2f 64 6c 3e 0a  o-ops..** </dl>.
184c0 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 43  **.** [[SQLITE_C
184d0 4f 4e 46 49 47 5f 53 51 4c 4c 4f 47 5d 5d 0a 2a  ONFIG_SQLLOG]].*
184e0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
184f0 46 49 47 5f 53 51 4c 4c 4f 47 0a 2a 2a 20 3c 64  FIG_SQLLOG.** <d
18500 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 69 73  d>This option is
18510 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
18520 69 66 20 73 71 6c 69 74 65 20 69 73 20 63 6f 6d  if sqlite is com
18530 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a  piled with the.*
18540 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
18550 53 51 4c 4c 4f 47 20 70 72 65 2d 70 72 6f 63 65  SQLLOG pre-proce
18560 73 73 6f 72 20 6d 61 63 72 6f 20 64 65 66 69 6e  ssor macro defin
18570 65 64 2e 20 54 68 65 20 66 69 72 73 74 20 61 72  ed. The first ar
18580 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 0a 2a 2a  gument should.**
18590 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   be a pointer to
185a0 20 61 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20 74   a function of t
185b0 79 70 65 20 76 6f 69 64 28 2a 29 28 76 6f 69 64  ype void(*)(void
185c0 2a 2c 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  *,sqlite3*,const
185d0 20 63 68 61 72 2a 2c 20 69 6e 74 29 2e 0a 2a 2a   char*, int)..**
185e0 20 54 68 65 20 73 65 63 6f 6e 64 20 73 68 6f 75   The second shou
185f0 6c 64 20 62 65 20 6f 66 20 74 79 70 65 20 28 76  ld be of type (v
18600 6f 69 64 2a 29 2e 20 54 68 65 20 63 61 6c 6c 62  oid*). The callb
18610 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 62  ack is invoked b
18620 79 20 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a  y the library.**
18630 20 69 6e 20 74 68 72 65 65 20 73 65 70 61 72 61   in three separa
18640 74 65 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73  te circumstances
18650 2c 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  , identified by 
18660 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64  the value passed
18670 20 61 73 20 74 68 65 0a 2a 2a 20 66 6f 75 72 74   as the.** fourt
18680 68 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20  h parameter. If 
18690 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d  the fourth param
186a0 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20  eter is 0, then 
186b0 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
186c0 6e 65 63 74 69 6f 6e 0a 2a 2a 20 70 61 73 73 65  nection.** passe
186d0 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
186e0 61 72 67 75 6d 65 6e 74 20 68 61 73 20 6a 75 73  argument has jus
186f0 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 20 54  t been opened. T
18700 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
18710 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 61  t.** points to a
18720 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69   buffer containi
18730 6e 67 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  ng the name of t
18740 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
18750 20 66 69 6c 65 2e 20 49 66 20 74 68 65 0a 2a 2a   file. If the.**
18760 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65   fourth paramete
18770 72 20 69 73 20 31 2c 20 74 68 65 6e 20 74 68 65  r is 1, then the
18780 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74   SQL statement t
18790 68 61 74 20 74 68 65 20 74 68 69 72 64 20 70 61  hat the third pa
187a0 72 61 6d 65 74 65 72 0a 2a 2a 20 70 6f 69 6e 74  rameter.** point
187b0 73 20 74 6f 20 68 61 73 20 6a 75 73 74 20 62 65  s to has just be
187c0 65 6e 20 65 78 65 63 75 74 65 64 2e 20 4f 72 2c  en executed. Or,
187d0 20 69 66 20 74 68 65 20 66 6f 75 72 74 68 20 70   if the fourth p
187e0 61 72 61 6d 65 74 65 72 20 69 73 20 32 2c 20 74  arameter is 2, t
187f0 68 65 6e 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e 65  hen.** the conne
18800 63 74 69 6f 6e 20 62 65 69 6e 67 20 70 61 73 73  ction being pass
18810 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  ed as the second
18820 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 62 65   parameter is be
18830 69 6e 67 20 63 6c 6f 73 65 64 2e 20 54 68 65 0a  ing closed. The.
18840 2a 2a 20 74 68 69 72 64 20 70 61 72 61 6d 65 74  ** third paramet
18850 65 72 20 69 73 20 70 61 73 73 65 64 20 4e 55 4c  er is passed NUL
18860 4c 20 49 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  L In this case..
18870 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66  ** </dl>.*/.#def
18880 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ine SQLITE_CONFI
18890 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20 20  G_SINGLETHREAD  
188a0 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64 65  1  /* nil */.#de
188b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  fine SQLITE_CONF
188c0 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 20 20  IG_MULTITHREAD  
188d0 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64   2  /* nil */.#d
188e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
188f0 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 20 20  FIG_SERIALIZED  
18900 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23    3  /* nil */.#
18910 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
18920 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20 20 20  NFIG_MALLOC     
18930 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65 33     4  /* sqlite3
18940 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f  _mem_methods* */
18950 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
18960 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43  CONFIG_GETMALLOC
18970 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c 69 74       5  /* sqlit
18980 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20  e3_mem_methods* 
18990 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
189a0 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48  E_CONFIG_SCRATCH
189b0 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76 6f 69         6  /* voi
189c0 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20  d*, int sz, int 
189d0 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  N */.#define SQL
189e0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43  ITE_CONFIG_PAGEC
189f0 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a 20 76  ACHE     7  /* v
18a00 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e  oid*, int sz, in
18a10 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  t N */.#define S
18a20 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41  QLITE_CONFIG_HEA
18a30 50 20 20 20 20 20 20 20 20 20 20 38 20 20 2f 2a  P          8  /*
18a40 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42 79 74   void*, int nByt
18a50 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a 23 64  e, int min */.#d
18a60 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
18a70 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 20 20  FIG_MEMSTATUS   
18a80 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61 6e 20    9  /* boolean 
18a90 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
18aa0 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 20 20  E_CONFIG_MUTEX  
18ab0 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73 71 6c        10  /* sql
18ac0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
18ad0 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ds* */.#define S
18ae0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
18af0 4d 55 54 45 58 20 20 20 20 20 31 31 20 20 2f 2a  MUTEX     11  /*
18b00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
18b10 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a 20 70 72  ethods* */./* pr
18b20 65 76 69 6f 75 73 6c 79 20 53 51 4c 49 54 45 5f  eviously SQLITE_
18b30 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41 4c 4c 4f  CONFIG_CHUNKALLO
18b40 43 20 31 32 20 77 68 69 63 68 20 69 73 20 6e 6f  C 12 which is no
18b50 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20 0a 23 64  w unused. */ .#d
18b60 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
18b70 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20  FIG_LOOKASIDE   
18b80 20 31 33 20 20 2f 2a 20 69 6e 74 20 69 6e 74 20   13  /* int int 
18b90 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
18ba0 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20  E_CONFIG_PCACHE 
18bb0 20 20 20 20 20 20 31 34 20 20 2f 2a 20 6e 6f 2d        14  /* no-
18bc0 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  op */.#define SQ
18bd0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50  LITE_CONFIG_GETP
18be0 43 41 43 48 45 20 20 20 20 31 35 20 20 2f 2a 20  CACHE    15  /* 
18bf0 6e 6f 2d 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65  no-op */.#define
18c00 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c   SQLITE_CONFIG_L
18c10 4f 47 20 20 20 20 20 20 20 20 20 20 31 36 20 20  OG          16  
18c20 2f 2a 20 78 46 75 6e 63 2c 20 76 6f 69 64 2a 20  /* xFunc, void* 
18c30 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
18c40 45 5f 43 4f 4e 46 49 47 5f 55 52 49 20 20 20 20  E_CONFIG_URI    
18c50 20 20 20 20 20 20 31 37 20 20 2f 2a 20 69 6e 74        17  /* int
18c60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
18c70 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45  TE_CONFIG_PCACHE
18c80 32 20 20 20 20 20 20 31 38 20 20 2f 2a 20 73 71  2      18  /* sq
18c90 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
18ca0 68 6f 64 73 32 2a 20 2a 2f 0a 23 64 65 66 69 6e  hods2* */.#defin
18cb0 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
18cc0 47 45 54 50 43 41 43 48 45 32 20 20 20 31 39 20  GETPCACHE2   19 
18cd0 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63   /* sqlite3_pcac
18ce0 68 65 5f 6d 65 74 68 6f 64 73 32 2a 20 2a 2f 0a  he_methods2* */.
18cf0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
18d00 4f 4e 46 49 47 5f 43 4f 56 45 52 49 4e 47 5f 49  ONFIG_COVERING_I
18d10 4e 44 45 58 5f 53 43 41 4e 20 32 30 20 20 2f 2a  NDEX_SCAN 20  /*
18d20 20 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20   int */.#define 
18d30 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 51  SQLITE_CONFIG_SQ
18d40 4c 4c 4f 47 20 20 20 20 20 20 20 32 31 20 20 2f  LLOG       21  /
18d50 2a 20 78 53 71 6c 6c 6f 67 2c 20 76 6f 69 64 2a  * xSqllog, void*
18d60 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33   */../*.** CAPI3
18d70 52 45 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f  REF: Database Co
18d80 6e 6e 65 63 74 69 6f 6e 20 43 6f 6e 66 69 67 75  nnection Configu
18d90 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 0a 2a  ration Options.*
18da0 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74  *.** These const
18db0 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 76 61  ants are the ava
18dc0 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20 63  ilable integer c
18dd0 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
18de0 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e  ions that.** can
18df0 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68   be passed as th
18e00 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
18e10 74 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65  t to the [sqlite
18e20 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69  3_db_config()] i
18e30 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
18e40 4e 65 77 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  New configuratio
18e50 6e 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65  n options may be
18e60 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65   added in future
18e70 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c   releases of SQL
18e80 69 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67  ite..** Existing
18e90 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
18ea0 70 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20  ptions might be 
18eb0 64 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41  discontinued.  A
18ec0 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73  pplications.** s
18ed0 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20  hould check the 
18ee0 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  return code from
18ef0 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e   [sqlite3_db_con
18f00 66 69 67 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73  fig()] to make s
18f10 75 72 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  ure that.** the 
18f20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 20 20 5e 54  call worked.  ^T
18f30 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63  he [sqlite3_db_c
18f40 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61  onfig()] interfa
18f50 63 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61  ce will return a
18f60 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72  .** non-zero [er
18f70 72 6f 72 20 63 6f 64 65 5d 20 69 66 20 61 20 64  ror code] if a d
18f80 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20 75  iscontinued or u
18f90 6e 73 75 70 70 6f 72 74 65 64 20 63 6f 6e 66 69  nsupported confi
18fa0 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 0a  guration option.
18fb0 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a  ** is invoked..*
18fc0 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74  *.** <dl>.** <dt
18fd0 3e 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47  >SQLITE_DBCONFIG
18fe0 5f 4c 4f 4f 4b 41 53 49 44 45 3c 2f 64 74 3e 0a  _LOOKASIDE</dt>.
18ff0 2a 2a 20 3c 64 64 3e 20 5e 54 68 69 73 20 6f 70  ** <dd> ^This op
19000 74 69 6f 6e 20 74 61 6b 65 73 20 74 68 72 65 65  tion takes three
19010 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 72 67 75   additional argu
19020 6d 65 6e 74 73 20 74 68 61 74 20 64 65 74 65 72  ments that deter
19030 6d 69 6e 65 20 74 68 65 20 0a 2a 2a 20 5b 6c 6f  mine the .** [lo
19040 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61  okaside memory a
19050 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e 66 69 67  llocator] config
19060 75 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  uration for the 
19070 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
19080 74 69 6f 6e 5d 2e 0a 2a 2a 20 5e 54 68 65 20 66  tion]..** ^The f
19090 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 28 74  irst argument (t
190a0 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74  he third paramet
190b0 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 64  er to [sqlite3_d
190c0 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 73 20 61  b_config()] is a
190d0 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  .** pointer to a
190e0 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74   memory buffer t
190f0 6f 20 75 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73  o use for lookas
19100 69 64 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 5e  ide memory..** ^
19110 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
19120 6e 74 20 61 66 74 65 72 20 74 68 65 20 53 51 4c  nt after the SQL
19130 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f  ITE_DBCONFIG_LOO
19140 4b 41 53 49 44 45 20 76 65 72 62 0a 2a 2a 20 6d  KASIDE verb.** m
19150 61 79 20 62 65 20 4e 55 4c 4c 20 69 6e 20 77 68  ay be NULL in wh
19160 69 63 68 20 63 61 73 65 20 53 51 4c 69 74 65 20  ich case SQLite 
19170 77 69 6c 6c 20 61 6c 6c 6f 63 61 74 65 20 74 68  will allocate th
19180 65 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 62  e.** lookaside b
19190 75 66 66 65 72 20 69 74 73 65 6c 66 20 75 73 69  uffer itself usi
191a0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  ng [sqlite3_mall
191b0 6f 63 28 29 5d 2e 20 5e 54 68 65 20 73 65 63 6f  oc()]. ^The seco
191c0 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  nd argument is t
191d0 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61  he.** size of ea
191e0 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66  ch lookaside buf
191f0 66 65 72 20 73 6c 6f 74 2e 20 20 5e 54 68 65 20  fer slot.  ^The 
19200 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69  third argument i
19210 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  s the number of.
19220 2a 2a 20 73 6c 6f 74 73 2e 20 20 54 68 65 20 73  ** slots.  The s
19230 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65  ize of the buffe
19240 72 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61  r in the first a
19250 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20  rgument must be 
19260 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20  greater than.** 
19270 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
19280 70 72 6f 64 75 63 74 20 6f 66 20 74 68 65 20 73  product of the s
19290 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20  econd and third 
192a0 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 20  arguments.  The 
192b0 62 75 66 66 65 72 0a 2a 2a 20 6d 75 73 74 20 62  buffer.** must b
192c0 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20  e aligned to an 
192d0 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e  8-byte boundary.
192e0 20 20 5e 49 66 20 74 68 65 20 73 65 63 6f 6e 64    ^If the second
192f0 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20   argument to.** 
19300 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f  SQLITE_DBCONFIG_
19310 4c 4f 4f 4b 41 53 49 44 45 20 69 73 20 6e 6f 74  LOOKASIDE is not
19320 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38   a multiple of 8
19330 2c 20 69 74 20 69 73 20 69 6e 74 65 72 6e 61 6c  , it is internal
19340 6c 79 0a 2a 2a 20 72 6f 75 6e 64 65 64 20 64 6f  ly.** rounded do
19350 77 6e 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73  wn to the next s
19360 6d 61 6c 6c 65 72 20 6d 75 6c 74 69 70 6c 65 20  maller multiple 
19370 6f 66 20 38 2e 20 20 5e 28 54 68 65 20 6c 6f 6f  of 8.  ^(The loo
19380 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 0a 2a 2a  kaside memory.**
19390 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 66   configuration f
193a0 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  or a database co
193b0 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 20 6f 6e 6c  nnection can onl
193c0 79 20 62 65 20 63 68 61 6e 67 65 64 20 77 68 65  y be changed whe
193d0 6e 20 74 68 61 74 0a 2a 2a 20 63 6f 6e 6e 65 63  n that.** connec
193e0 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 75 72 72  tion is not curr
193f0 65 6e 74 6c 79 20 75 73 69 6e 67 20 6c 6f 6f 6b  ently using look
19400 61 73 69 64 65 20 6d 65 6d 6f 72 79 2c 20 6f 72  aside memory, or
19410 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 0a   in other words.
19420 2a 2a 20 77 68 65 6e 20 74 68 65 20 22 63 75 72  ** when the "cur
19430 72 65 6e 74 20 76 61 6c 75 65 22 20 72 65 74 75  rent value" retu
19440 72 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69  rned by.** [sqli
19450 74 65 33 5f 64 62 5f 73 74 61 74 75 73 5d 28 44  te3_db_status](D
19460 2c 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  ,[SQLITE_CONFIG_
19470 4c 4f 4f 4b 41 53 49 44 45 5d 2c 2e 2e 2e 29 20  LOOKASIDE],...) 
19480 69 73 20 7a 65 72 6f 2e 0a 2a 2a 20 41 6e 79 20  is zero..** Any 
19490 61 74 74 65 6d 70 74 20 74 6f 20 63 68 61 6e 67  attempt to chang
194a0 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20  e the lookaside 
194b0 6d 65 6d 6f 72 79 20 63 6f 6e 66 69 67 75 72 61  memory configura
194c0 74 69 6f 6e 20 77 68 65 6e 20 6c 6f 6f 6b 61 73  tion when lookas
194d0 69 64 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 69 73  ide.** memory is
194e0 20 69 6e 20 75 73 65 20 6c 65 61 76 65 73 20 74   in use leaves t
194f0 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  he configuration
19500 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 72   unchanged and r
19510 65 74 75 72 6e 73 20 0a 2a 2a 20 5b 53 51 4c 49  eturns .** [SQLI
19520 54 45 5f 42 55 53 59 5d 2e 29 5e 3c 2f 64 64 3e  TE_BUSY].)^</dd>
19530 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
19540 45 5f 44 42 43 4f 4e 46 49 47 5f 45 4e 41 42 4c  E_DBCONFIG_ENABL
19550 45 5f 46 4b 45 59 3c 2f 64 74 3e 0a 2a 2a 20 3c  E_FKEY</dt>.** <
19560 64 64 3e 20 5e 54 68 69 73 20 6f 70 74 69 6f 6e  dd> ^This option
19570 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e 61 62   is used to enab
19580 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68  le or disable th
19590 65 20 65 6e 66 6f 72 63 65 6d 65 6e 74 20 6f 66  e enforcement of
195a0 0a 2a 2a 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79  .** [foreign key
195b0 20 63 6f 6e 73 74 72 61 69 6e 74 73 5d 2e 20 20   constraints].  
195c0 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20  There should be 
195d0 74 77 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20 61  two additional a
195e0 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 20 54 68 65  rguments..** The
195f0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
19600 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68  is an integer wh
19610 69 63 68 20 69 73 20 30 20 74 6f 20 64 69 73 61  ich is 0 to disa
19620 62 6c 65 20 46 4b 20 65 6e 66 6f 72 63 65 6d 65  ble FK enforceme
19630 6e 74 2c 0a 2a 2a 20 70 6f 73 69 74 69 76 65 20  nt,.** positive 
19640 74 6f 20 65 6e 61 62 6c 65 20 46 4b 20 65 6e 66  to enable FK enf
19650 6f 72 63 65 6d 65 6e 74 20 6f 72 20 6e 65 67 61  orcement or nega
19660 74 69 76 65 20 74 6f 20 6c 65 61 76 65 20 46 4b  tive to leave FK
19670 20 65 6e 66 6f 72 63 65 6d 65 6e 74 0a 2a 2a 20   enforcement.** 
19680 75 6e 63 68 61 6e 67 65 64 2e 20 20 54 68 65 20  unchanged.  The 
19690 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
196a0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
196b0 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e 74 6f   an integer into
196c0 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 77 72 69   which.** is wri
196d0 74 74 65 6e 20 30 20 6f 72 20 31 20 74 6f 20 69  tten 0 or 1 to i
196e0 6e 64 69 63 61 74 65 20 77 68 65 74 68 65 72 20  ndicate whether 
196f0 46 4b 20 65 6e 66 6f 72 63 65 6d 65 6e 74 20 69  FK enforcement i
19700 73 20 6f 66 66 20 6f 72 20 6f 6e 0a 2a 2a 20 66  s off or on.** f
19710 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 63 61  ollowing this ca
19720 6c 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20  ll.  The second 
19730 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 62 65  parameter may be
19740 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
19750 20 69 6e 0a 2a 2a 20 77 68 69 63 68 20 63 61 73   in.** which cas
19760 65 20 74 68 65 20 46 4b 20 65 6e 66 6f 72 63 65  e the FK enforce
19770 6d 65 6e 74 20 73 65 74 74 69 6e 67 20 69 73 20  ment setting is 
19780 6e 6f 74 20 72 65 70 6f 72 74 65 64 20 62 61 63  not reported bac
19790 6b 2e 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  k. </dd>.**.** <
197a0 64 74 3e 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46  dt>SQLITE_DBCONF
197b0 49 47 5f 45 4e 41 42 4c 45 5f 54 52 49 47 47 45  IG_ENABLE_TRIGGE
197c0 52 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 20 5e  R</dt>.** <dd> ^
197d0 54 68 69 73 20 6f 70 74 69 6f 6e 20 69 73 20 75  This option is u
197e0 73 65 64 20 74 6f 20 65 6e 61 62 6c 65 20 6f 72  sed to enable or
197f0 20 64 69 73 61 62 6c 65 20 5b 43 52 45 41 54 45   disable [CREATE
19800 20 54 52 49 47 47 45 52 20 7c 20 74 72 69 67 67   TRIGGER | trigg
19810 65 72 73 5d 2e 0a 2a 2a 20 54 68 65 72 65 20 73  ers]..** There s
19820 68 6f 75 6c 64 20 62 65 20 74 77 6f 20 61 64 64  hould be two add
19830 69 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74  itional argument
19840 73 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  s..** The first 
19850 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 69  argument is an i
19860 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20  nteger which is 
19870 30 20 74 6f 20 64 69 73 61 62 6c 65 20 74 72 69  0 to disable tri
19880 67 67 65 72 73 2c 0a 2a 2a 20 70 6f 73 69 74 69  ggers,.** positi
19890 76 65 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 69  ve to enable tri
198a0 67 67 65 72 73 20 6f 72 20 6e 65 67 61 74 69 76  ggers or negativ
198b0 65 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20 73  e to leave the s
198c0 65 74 74 69 6e 67 20 75 6e 63 68 61 6e 67 65 64  etting unchanged
198d0 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  ..** The second 
198e0 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 70  parameter is a p
198f0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 74  ointer to an int
19900 65 67 65 72 20 69 6e 74 6f 20 77 68 69 63 68 0a  eger into which.
19910 2a 2a 20 69 73 20 77 72 69 74 74 65 6e 20 30 20  ** is written 0 
19920 6f 72 20 31 20 74 6f 20 69 6e 64 69 63 61 74 65  or 1 to indicate
19930 20 77 68 65 74 68 65 72 20 74 72 69 67 67 65 72   whether trigger
19940 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 6f  s are disabled o
19950 72 20 65 6e 61 62 6c 65 64 0a 2a 2a 20 66 6f 6c  r enabled.** fol
19960 6c 6f 77 69 6e 67 20 74 68 69 73 20 63 61 6c 6c  lowing this call
19970 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61  .  The second pa
19980 72 61 6d 65 74 65 72 20 6d 61 79 20 62 65 20 61  rameter may be a
19990 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 69   NULL pointer, i
199a0 6e 0a 2a 2a 20 77 68 69 63 68 20 63 61 73 65 20  n.** which case 
199b0 74 68 65 20 74 72 69 67 67 65 72 20 73 65 74 74  the trigger sett
199c0 69 6e 67 20 69 73 20 6e 6f 74 20 72 65 70 6f 72  ing is not repor
199d0 74 65 64 20 62 61 63 6b 2e 20 3c 2f 64 64 3e 0a  ted back. </dd>.
199e0 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23  **.** </dl>.*/.#
199f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42  define SQLITE_DB
19a00 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
19a10 20 20 20 20 20 20 20 31 30 30 31 20 20 2f 2a 20         1001  /* 
19a20 76 6f 69 64 2a 20 69 6e 74 20 69 6e 74 20 2a 2f  void* int int */
19a30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
19a40 44 42 43 4f 4e 46 49 47 5f 45 4e 41 42 4c 45 5f  DBCONFIG_ENABLE_
19a50 46 4b 45 59 20 20 20 20 20 31 30 30 32 20 20 2f  FKEY     1002  /
19a60 2a 20 69 6e 74 20 69 6e 74 2a 20 2a 2f 0a 23 64  * int int* */.#d
19a70 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 43  efine SQLITE_DBC
19a80 4f 4e 46 49 47 5f 45 4e 41 42 4c 45 5f 54 52 49  ONFIG_ENABLE_TRI
19a90 47 47 45 52 20 20 31 30 30 33 20 20 2f 2a 20 69  GGER  1003  /* i
19aa0 6e 74 20 69 6e 74 2a 20 2a 2f 0a 0a 0a 2f 2a 0a  nt int* */.../*.
19ab0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61  ** CAPI3REF: Ena
19ac0 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45  ble Or Disable E
19ad0 78 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43  xtended Result C
19ae0 6f 64 65 73 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  odes.**.** ^The 
19af0 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
19b00 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20  _result_codes() 
19b10 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20  routine enables 
19b20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a  or disables the.
19b30 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73  ** [extended res
19b40 75 6c 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75  ult codes] featu
19b50 72 65 20 6f 66 20 53 51 4c 69 74 65 2e 20 5e 54  re of SQLite. ^T
19b60 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75  he extended resu
19b70 6c 74 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 20  lt.** codes are 
19b80 64 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61  disabled by defa
19b90 75 6c 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63  ult for historic
19ba0 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  al compatibility
19bb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
19bc0 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65  int sqlite3_exte
19bd0 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65  nded_result_code
19be0 73 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20  s(sqlite3*, int 
19bf0 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  onoff);../*.** C
19c00 41 50 49 33 52 45 46 3a 20 4c 61 73 74 20 49 6e  API3REF: Last In
19c10 73 65 72 74 20 52 6f 77 69 64 0a 2a 2a 0a 2a 2a  sert Rowid.**.**
19c20 20 5e 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20   ^Each entry in 
19c30 61 6e 20 53 51 4c 69 74 65 20 74 61 62 6c 65 20  an SQLite table 
19c40 68 61 73 20 61 20 75 6e 69 71 75 65 20 36 34 2d  has a unique 64-
19c50 62 69 74 20 73 69 67 6e 65 64 0a 2a 2a 20 69 6e  bit signed.** in
19c60 74 65 67 65 72 20 6b 65 79 20 63 61 6c 6c 65 64  teger key called
19c70 20 74 68 65 20 5b 52 4f 57 49 44 20 7c 20 22 72   the [ROWID | "r
19c80 6f 77 69 64 22 5d 2e 20 5e 54 68 65 20 72 6f 77  owid"]. ^The row
19c90 69 64 20 69 73 20 61 6c 77 61 79 73 20 61 76 61  id is always ava
19ca0 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 61 6e 20  ilable.** as an 
19cb0 75 6e 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d  undeclared colum
19cc0 6e 20 6e 61 6d 65 64 20 52 4f 57 49 44 2c 20 4f  n named ROWID, O
19cd0 49 44 2c 20 6f 72 20 5f 52 4f 57 49 44 5f 20 61  ID, or _ROWID_ a
19ce0 73 20 6c 6f 6e 67 20 61 73 20 74 68 6f 73 65 0a  s long as those.
19cf0 2a 2a 20 6e 61 6d 65 73 20 61 72 65 20 6e 6f 74  ** names are not
19d00 20 61 6c 73 6f 20 75 73 65 64 20 62 79 20 65 78   also used by ex
19d10 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65  plicitly declare
19d20 64 20 63 6f 6c 75 6d 6e 73 2e 20 5e 49 66 0a 2a  d columns. ^If.*
19d30 2a 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20  * the table has 
19d40 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 79 70 65  a column of type
19d50 20 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52   [INTEGER PRIMAR
19d60 59 20 4b 45 59 5d 20 74 68 65 6e 20 74 68 61 74  Y KEY] then that
19d70 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 61 6e   column.** is an
19d80 6f 74 68 65 72 20 61 6c 69 61 73 20 66 6f 72 20  other alias for 
19d90 74 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a  the rowid..**.**
19da0 20 5e 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72   ^This routine r
19db0 65 74 75 72 6e 73 20 74 68 65 20 5b 72 6f 77 69  eturns the [rowi
19dc0 64 5d 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  d] of the most r
19dd0 65 63 65 6e 74 0a 2a 2a 20 73 75 63 63 65 73 73  ecent.** success
19de0 66 75 6c 20 5b 49 4e 53 45 52 54 5d 20 69 6e 74  ful [INSERT] int
19df0 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
19e00 72 6f 6d 20 74 68 65 20 5b 64 61 74 61 62 61 73  rom the [databas
19e10 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a  e connection].**
19e20 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72   in the first ar
19e30 67 75 6d 65 6e 74 2e 20 20 5e 41 73 20 6f 66 20  gument.  ^As of 
19e40 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33  SQLite version 3
19e50 2e 37 2e 37 2c 20 74 68 69 73 20 72 6f 75 74 69  .7.7, this routi
19e60 6e 65 73 0a 2a 2a 20 72 65 63 6f 72 64 73 20 74  nes.** records t
19e70 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72  he last insert r
19e80 6f 77 69 64 20 6f 66 20 62 6f 74 68 20 6f 72 64  owid of both ord
19e90 69 6e 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64  inary tables and
19ea0 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73   [virtual tables
19eb0 5d 2e 0a 2a 2a 20 5e 49 66 20 6e 6f 20 73 75 63  ]..** ^If no suc
19ec0 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d  cessful [INSERT]
19ed0 73 0a 2a 2a 20 68 61 76 65 20 65 76 65 72 20 6f  s.** have ever o
19ee0 63 63 75 72 72 65 64 20 6f 6e 20 74 68 61 74 20  ccurred on that 
19ef0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
19f00 69 6f 6e 2c 20 7a 65 72 6f 20 69 73 20 72 65 74  ion, zero is ret
19f10 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 28 49  urned..**.** ^(I
19f20 66 20 61 6e 20 5b 49 4e 53 45 52 54 5d 20 6f 63  f an [INSERT] oc
19f30 63 75 72 73 20 77 69 74 68 69 6e 20 61 20 74 72  curs within a tr
19f40 69 67 67 65 72 20 6f 72 20 77 69 74 68 69 6e 20  igger or within 
19f50 61 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  a [virtual table
19f60 5d 0a 2a 2a 20 6d 65 74 68 6f 64 2c 20 74 68 65  ].** method, the
19f70 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  n this routine w
19f80 69 6c 6c 20 72 65 74 75 72 6e 20 74 68 65 20 5b  ill return the [
19f90 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 69 6e  rowid] of the in
19fa0 73 65 72 74 65 64 0a 2a 2a 20 72 6f 77 20 61 73  serted.** row as
19fb0 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 72 69   long as the tri
19fc0 67 67 65 72 20 6f 72 20 76 69 72 74 75 61 6c 20  gger or virtual 
19fd0 74 61 62 6c 65 20 6d 65 74 68 6f 64 20 69 73 20  table method is 
19fe0 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 20 42 75 74 20  running..** But 
19ff0 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67 65 72  once the trigger
1a000 20 6f 72 20 76 69 72 74 75 61 6c 20 74 61 62 6c   or virtual tabl
1a010 65 20 6d 65 74 68 6f 64 20 65 6e 64 73 2c 20 74  e method ends, t
1a020 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
1a030 64 20 0a 2a 2a 20 62 79 20 74 68 69 73 20 72 6f  d .** by this ro
1a040 75 74 69 6e 65 20 72 65 76 65 72 74 73 20 74 6f  utine reverts to
1a050 20 77 68 61 74 20 69 74 20 77 61 73 20 62 65 66   what it was bef
1a060 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 72 20  ore the trigger 
1a070 6f 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61  or virtual.** ta
1a080 62 6c 65 20 6d 65 74 68 6f 64 20 62 65 67 61 6e  ble method began
1a090 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 41 6e 20 5b 49  .)^.**.** ^An [I
1a0a0 4e 53 45 52 54 5d 20 74 68 61 74 20 66 61 69 6c  NSERT] that fail
1a0b0 73 20 64 75 65 20 74 6f 20 61 20 63 6f 6e 73 74  s due to a const
1a0c0 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20  raint violation 
1a0d0 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 73 75 63 63  is not a.** succ
1a0e0 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d 20  essful [INSERT] 
1a0f0 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 63 68 61  and does not cha
1a100 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 72 65  nge the value re
1a110 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 0a 2a  turned by this.*
1a120 2a 20 72 6f 75 74 69 6e 65 2e 20 20 5e 54 68 75  * routine.  ^Thu
1a130 73 20 49 4e 53 45 52 54 20 4f 52 20 46 41 49 4c  s INSERT OR FAIL
1a140 2c 20 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f  , INSERT OR IGNO
1a150 52 45 2c 20 49 4e 53 45 52 54 20 4f 52 20 52 4f  RE, INSERT OR RO
1a160 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 6e 64 20 49  LLBACK,.** and I
1a170 4e 53 45 52 54 20 4f 52 20 41 42 4f 52 54 20 6d  NSERT OR ABORT m
1a180 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 74  ake no changes t
1a190 6f 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  o the return val
1a1a0 75 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 72 6f  ue of this.** ro
1a1b0 75 74 69 6e 65 20 77 68 65 6e 20 74 68 65 69 72  utine when their
1a1c0 20 69 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c 73   insertion fails
1a1d0 2e 20 20 5e 28 57 68 65 6e 20 49 4e 53 45 52 54  .  ^(When INSERT
1a1e0 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a 20 65   OR REPLACE.** e
1a1f0 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f 6e 73  ncounters a cons
1a200 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
1a210 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 66 61  , it does not fa
1a220 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e 53 45  il.  The.** INSE
1a230 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20  RT continues to 
1a240 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 65 72  completion after
1a250 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 20 74   deleting rows t
1a260 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 74 68  hat caused.** th
1a270 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 72 6f  e constraint pro
1a280 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 20 4f  blem so INSERT O
1a290 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c 20 61  R REPLACE will a
1a2a0 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a 2a 20  lways change.** 
1a2b0 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
1a2c0 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61   of this interfa
1a2d0 63 65 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 46 6f 72  ce.)^.**.** ^For
1a2e0 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66   the purposes of
1a2f0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61   this routine, a
1a300 6e 20 5b 49 4e 53 45 52 54 5d 20 69 73 20 63 6f  n [INSERT] is co
1a310 6e 73 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62  nsidered to.** b
1a320 65 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 65  e successful eve
1a330 6e 20 69 66 20 69 74 20 69 73 20 73 75 62 73 65  n if it is subse
1a340 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62  quently rolled b
1a350 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ack..**.** This 
1a360 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 63 63 65  function is acce
1a370 73 73 69 62 6c 65 20 74 6f 20 53 51 4c 20 73 74  ssible to SQL st
1a380 61 74 65 6d 65 6e 74 73 20 76 69 61 20 74 68 65  atements via the
1a390 0a 2a 2a 20 5b 6c 61 73 74 5f 69 6e 73 65 72 74  .** [last_insert
1a3a0 5f 72 6f 77 69 64 28 29 20 53 51 4c 20 66 75 6e  _rowid() SQL fun
1a3b0 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66  ction]..**.** If
1a3c0 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72 65   a separate thre
1a3d0 61 64 20 70 65 72 66 6f 72 6d 73 20 61 20 6e 65  ad performs a ne
1a3e0 77 20 5b 49 4e 53 45 52 54 5d 20 6f 6e 20 74 68  w [INSERT] on th
1a3f0 65 20 73 61 6d 65 0a 2a 2a 20 64 61 74 61 62 61  e same.** databa
1a400 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68  se connection wh
1a410 69 6c 65 20 74 68 65 20 5b 73 71 6c 69 74 65 33  ile the [sqlite3
1a420 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  _last_insert_row
1a430 69 64 28 29 5d 0a 2a 2a 20 66 75 6e 63 74 69 6f  id()].** functio
1a440 6e 20 69 73 20 72 75 6e 6e 69 6e 67 20 61 6e 64  n is running and
1a450 20 74 68 75 73 20 63 68 61 6e 67 65 73 20 74 68   thus changes th
1a460 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 5b 72  e last insert [r
1a470 6f 77 69 64 5d 2c 0a 2a 2a 20 74 68 65 6e 20 74  owid],.** then t
1a480 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
1a490 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 6c 61  d by [sqlite3_la
1a4a0 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
1a4b0 29 5d 20 69 73 0a 2a 2a 20 75 6e 70 72 65 64 69  )] is.** unpredi
1a4c0 63 74 61 62 6c 65 20 61 6e 64 20 6d 69 67 68 74  ctable and might
1a4d0 20 6e 6f 74 20 65 71 75 61 6c 20 65 69 74 68 65   not equal eithe
1a4e0 72 20 74 68 65 20 6f 6c 64 20 6f 72 20 74 68 65  r the old or the
1a4f0 20 6e 65 77 0a 2a 2a 20 6c 61 73 74 20 69 6e 73   new.** last ins
1a500 65 72 74 20 5b 72 6f 77 69 64 5d 2e 0a 2a 2f 0a  ert [rowid]..*/.
1a510 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
1a520 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e3_int64 sqlite3
1a530 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  _last_insert_row
1a540 69 64 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f  id(sqlite3*);../
1a550 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43  *.** CAPI3REF: C
1a560 6f 75 6e 74 20 54 68 65 20 4e 75 6d 62 65 72 20  ount The Number 
1a570 4f 66 20 52 6f 77 73 20 4d 6f 64 69 66 69 65 64  Of Rows Modified
1a580 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 66 75 6e  .**.** ^This fun
1a590 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
1a5a0 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 61 74 61  e number of data
1a5b0 62 61 73 65 20 72 6f 77 73 20 74 68 61 74 20 77  base rows that w
1a5c0 65 72 65 20 63 68 61 6e 67 65 64 0a 2a 2a 20 6f  ere changed.** o
1a5d0 72 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65  r inserted or de
1a5e0 6c 65 74 65 64 20 62 79 20 74 68 65 20 6d 6f 73  leted by the mos
1a5f0 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c  t recently compl
1a600 65 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  eted SQL stateme
1a610 6e 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 5b 64 61  nt.** on the [da
1a620 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1a630 6e 5d 20 73 70 65 63 69 66 69 65 64 20 62 79 20  n] specified by 
1a640 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  the first parame
1a650 74 65 72 2e 0a 2a 2a 20 5e 28 4f 6e 6c 79 20 63  ter..** ^(Only c
1a660 68 61 6e 67 65 73 20 74 68 61 74 20 61 72 65 20  hanges that are 
1a670 64 69 72 65 63 74 6c 79 20 73 70 65 63 69 66 69  directly specifi
1a680 65 64 20 62 79 20 74 68 65 20 5b 49 4e 53 45 52  ed by the [INSER
1a690 54 5d 2c 20 5b 55 50 44 41 54 45 5d 2c 0a 2a 2a  T], [UPDATE],.**
1a6a0 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20 73 74 61   or [DELETE] sta
1a6b0 74 65 6d 65 6e 74 20 61 72 65 20 63 6f 75 6e 74  tement are count
1a6c0 65 64 2e 20 20 41 75 78 69 6c 69 61 72 79 20 63  ed.  Auxiliary c
1a6d0 68 61 6e 67 65 73 20 63 61 75 73 65 64 20 62 79  hanges caused by
1a6e0 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 6f 72 20  .** triggers or 
1a6f0 5b 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63 74  [foreign key act
1a700 69 6f 6e 73 5d 20 61 72 65 20 6e 6f 74 20 63 6f  ions] are not co
1a710 75 6e 74 65 64 2e 29 5e 20 55 73 65 20 74 68 65  unted.)^ Use the
1a720 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74  .** [sqlite3_tot
1a730 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 66 75  al_changes()] fu
1a740 6e 63 74 69 6f 6e 20 74 6f 20 66 69 6e 64 20 74  nction to find t
1a750 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
1a760 6f 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20 69 6e  of changes.** in
1a770 63 6c 75 64 69 6e 67 20 63 68 61 6e 67 65 73 20  cluding changes 
1a780 63 61 75 73 65 64 20 62 79 20 74 72 69 67 67 65  caused by trigge
1a790 72 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b  rs and foreign k
1a7a0 65 79 20 61 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  ey actions..**.*
1a7b0 2a 20 5e 43 68 61 6e 67 65 73 20 74 6f 20 61 20  * ^Changes to a 
1a7c0 76 69 65 77 20 74 68 61 74 20 61 72 65 20 73 69  view that are si
1a7d0 6d 75 6c 61 74 65 64 20 62 79 20 61 6e 20 5b 49  mulated by an [I
1a7e0 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65  NSTEAD OF trigge
1a7f0 72 5d 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 63 6f  r].** are not co
1a800 75 6e 74 65 64 2e 20 20 4f 6e 6c 79 20 72 65 61  unted.  Only rea
1a810 6c 20 74 61 62 6c 65 20 63 68 61 6e 67 65 73 20  l table changes 
1a820 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 0a  are counted..**.
1a830 2a 2a 20 5e 28 41 20 22 72 6f 77 20 63 68 61 6e  ** ^(A "row chan
1a840 67 65 22 20 69 73 20 61 20 63 68 61 6e 67 65 20  ge" is a change 
1a850 74 6f 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  to a single row 
1a860 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  of a single tabl
1a870 65 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20 61  e.** caused by a
1a880 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 45  n INSERT, DELETE
1a890 2c 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74  , or UPDATE stat
1a8a0 65 6d 65 6e 74 2e 20 20 52 6f 77 73 20 74 68 61  ement.  Rows tha
1a8b0 74 0a 2a 2a 20 61 72 65 20 63 68 61 6e 67 65 64  t.** are changed
1a8c0 20 61 73 20 73 69 64 65 20 65 66 66 65 63 74 73   as side effects
1a8d0 20 6f 66 20 5b 52 45 50 4c 41 43 45 5d 20 63 6f   of [REPLACE] co
1a8e0 6e 73 74 72 61 69 6e 74 20 72 65 73 6f 6c 75 74  nstraint resolut
1a8f0 69 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b  ion,.** rollback
1a900 2c 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69  , ABORT processi
1a910 6e 67 2c 20 5b 44 52 4f 50 20 54 41 42 4c 45 5d  ng, [DROP TABLE]
1a920 2c 20 6f 72 20 62 79 20 61 6e 79 20 6f 74 68 65  , or by any othe
1a930 72 0a 2a 2a 20 6d 65 63 68 61 6e 69 73 6d 73 20  r.** mechanisms 
1a940 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20  do not count as 
1a950 64 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67  direct row chang
1a960 65 73 2e 29 5e 0a 2a 2a 0a 2a 2a 20 41 20 22 74  es.)^.**.** A "t
1a970 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 22 20  rigger context" 
1a980 69 73 20 61 20 73 63 6f 70 65 20 6f 66 20 65 78  is a scope of ex
1a990 65 63 75 74 69 6f 6e 20 74 68 61 74 20 62 65 67  ecution that beg
1a9a0 69 6e 73 20 61 6e 64 0a 2a 2a 20 65 6e 64 73 20  ins and.** ends 
1a9b0 77 69 74 68 20 74 68 65 20 73 63 72 69 70 74 20  with the script 
1a9c0 6f 66 20 61 20 5b 43 52 45 41 54 45 20 54 52 49  of a [CREATE TRI
1a9d0 47 47 45 52 20 7c 20 74 72 69 67 67 65 72 5d 2e  GGER | trigger].
1a9e0 20 0a 2a 2a 20 4d 6f 73 74 20 53 51 4c 20 73 74   .** Most SQL st
1a9f0 61 74 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20  atements are.** 
1aa00 65 76 61 6c 75 61 74 65 64 20 6f 75 74 73 69 64  evaluated outsid
1aa10 65 20 6f 66 20 61 6e 79 20 74 72 69 67 67 65 72  e of any trigger
1aa20 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 22  .  This is the "
1aa30 74 6f 70 20 6c 65 76 65 6c 22 0a 2a 2a 20 74 72  top level".** tr
1aa40 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 20 20  igger context.  
1aa50 49 66 20 61 20 74 72 69 67 67 65 72 20 66 69 72  If a trigger fir
1aa60 65 73 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20  es from the top 
1aa70 6c 65 76 65 6c 2c 20 61 0a 2a 2a 20 6e 65 77 20  level, a.** new 
1aa80 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 20  trigger context 
1aa90 69 73 20 65 6e 74 65 72 65 64 20 66 6f 72 20 74  is entered for t
1aaa0 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74  he duration of t
1aab0 68 61 74 20 6f 6e 65 0a 2a 2a 20 74 72 69 67 67  hat one.** trigg
1aac0 65 72 2e 20 20 53 75 62 74 72 69 67 67 65 72 73  er.  Subtriggers
1aad0 20 63 72 65 61 74 65 20 73 75 62 63 6f 6e 74 65   create subconte
1aae0 78 74 73 20 66 6f 72 20 74 68 65 69 72 20 64 75  xts for their du
1aaf0 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 5e 43  ration..**.** ^C
1ab00 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  alling [sqlite3_
1ab10 65 78 65 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69  exec()] or [sqli
1ab20 74 65 33 5f 73 74 65 70 28 29 5d 20 72 65 63 75  te3_step()] recu
1ab30 72 73 69 76 65 6c 79 20 64 6f 65 73 0a 2a 2a 20  rsively does.** 
1ab40 6e 6f 74 20 63 72 65 61 74 65 20 61 20 6e 65 77  not create a new
1ab50 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74   trigger context
1ab60 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 66 75  ..**.** ^This fu
1ab70 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
1ab80 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 72  he number of dir
1ab90 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65 73 20  ect row changes 
1aba0 69 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 72  in the.** most r
1abb0 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 55 50  ecent INSERT, UP
1abc0 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 20  DATE, or DELETE 
1abd0 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e  statement within
1abe0 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 69   the same.** tri
1abf0 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a  gger context..**
1ac00 0a 2a 2a 20 5e 54 68 75 73 2c 20 77 68 65 6e 20  .** ^Thus, when 
1ac10 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20  called from the 
1ac20 74 6f 70 20 6c 65 76 65 6c 2c 20 74 68 69 73 20  top level, this 
1ac30 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
1ac40 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
1ac50 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65  f changes in the
1ac60 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e 53   most recent INS
1ac70 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20  ERT, UPDATE, or 
1ac80 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 61  DELETE.** that a
1ac90 6c 73 6f 20 6f 63 63 75 72 72 65 64 20 61 74 20  lso occurred at 
1aca0 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2e 20 20  the top level.  
1acb0 5e 28 57 69 74 68 69 6e 20 74 68 65 20 62 6f 64  ^(Within the bod
1acc0 79 20 6f 66 20 61 20 74 72 69 67 67 65 72 2c 0a  y of a trigger,.
1acd0 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  ** the sqlite3_c
1ace0 68 61 6e 67 65 73 28 29 20 69 6e 74 65 72 66 61  hanges() interfa
1acf0 63 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64  ce can be called
1ad00 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d   to find the num
1ad10 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e 67 65  ber of.** change
1ad20 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65  s in the most re
1ad30 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64  cently completed
1ad40 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c   INSERT, UPDATE,
1ad50 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73 74   or DELETE.** st
1ad60 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 74  atement within t
1ad70 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 73  he body of the s
1ad80 61 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a 20  ame trigger..** 
1ad90 48 6f 77 65 76 65 72 2c 20 74 68 65 20 6e 75 6d  However, the num
1ada0 62 65 72 20 72 65 74 75 72 6e 65 64 20 64 6f 65  ber returned doe
1adb0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68  s not include ch
1adc0 61 6e 67 65 73 0a 2a 2a 20 63 61 75 73 65 64 20  anges.** caused 
1add0 62 79 20 73 75 62 74 72 69 67 67 65 72 73 20 73  by subtriggers s
1ade0 69 6e 63 65 20 74 68 6f 73 65 20 68 61 76 65 20  ince those have 
1adf0 74 68 65 69 72 20 6f 77 6e 20 63 6f 6e 74 65 78  their own contex
1ae00 74 2e 29 5e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  t.)^.**.** See a
1ae10 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33  lso the [sqlite3
1ae20 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29  _total_changes()
1ae30 5d 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65  ] interface, the
1ae40 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67  .** [count_chang
1ae50 65 73 20 70 72 61 67 6d 61 5d 2c 20 61 6e 64 20  es pragma], and 
1ae60 74 68 65 20 5b 63 68 61 6e 67 65 73 28 29 20 53  the [changes() S
1ae70 51 4c 20 66 75 6e 63 74 69 6f 6e 5d 2e 0a 2a 2a  QL function]..**
1ae80 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74  .** If a separat
1ae90 65 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63  e thread makes c
1aea0 68 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61  hanges on the sa
1aeb0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
1aec0 65 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20  ection.** while 
1aed0 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73  [sqlite3_changes
1aee0 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74  ()] is running t
1aef0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65  hen the value re
1af00 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 75 6e 70  turned.** is unp
1af10 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6e  redictable and n
1af20 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a  ot meaningful..*
1af30 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1af40 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
1af50 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a  (sqlite3*);../*.
1af60 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 6f 74  ** CAPI3REF: Tot
1af70 61 6c 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77  al Number Of Row
1af80 73 20 4d 6f 64 69 66 69 65 64 0a 2a 2a 0a 2a 2a  s Modified.**.**
1af90 20 5e 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20   ^This function 
1afa0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
1afb0 65 72 20 6f 66 20 72 6f 77 20 63 68 61 6e 67 65  er of row change
1afc0 73 20 63 61 75 73 65 64 20 62 79 20 5b 49 4e 53  s caused by [INS
1afd0 45 52 54 5d 2c 0a 2a 2a 20 5b 55 50 44 41 54 45  ERT],.** [UPDATE
1afe0 5d 20 6f 72 20 5b 44 45 4c 45 54 45 5d 20 73 74  ] or [DELETE] st
1aff0 61 74 65 6d 65 6e 74 73 20 73 69 6e 63 65 20 74  atements since t
1b000 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
1b010 6e 65 63 74 69 6f 6e 5d 20 77 61 73 20 6f 70 65  nection] was ope
1b020 6e 65 64 2e 0a 2a 2a 20 5e 28 54 68 65 20 63 6f  ned..** ^(The co
1b030 75 6e 74 20 72 65 74 75 72 6e 65 64 20 62 79 20  unt returned by 
1b040 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
1b050 61 6e 67 65 73 28 29 20 69 6e 63 6c 75 64 65 73  anges() includes
1b060 20 61 6c 6c 20 63 68 61 6e 67 65 73 0a 2a 2a 20   all changes.** 
1b070 66 72 6f 6d 20 61 6c 6c 20 5b 43 52 45 41 54 45  from all [CREATE
1b080 20 54 52 49 47 47 45 52 20 7c 20 74 72 69 67 67   TRIGGER | trigg
1b090 65 72 5d 20 63 6f 6e 74 65 78 74 73 20 61 6e 64  er] contexts and
1b0a0 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79   changes made by
1b0b0 0a 2a 2a 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79  .** [foreign key
1b0c0 20 61 63 74 69 6f 6e 73 5d 2e 20 48 6f 77 65 76   actions]. Howev
1b0d0 65 72 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74  er,.** the count
1b0e0 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64   does not includ
1b0f0 65 20 63 68 61 6e 67 65 73 20 75 73 65 64 20 74  e changes used t
1b100 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 5b 52 45 50  o implement [REP
1b110 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74  LACE] constraint
1b120 73 2c 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63  s,.** do rollbac
1b130 6b 73 20 6f 72 20 41 42 4f 52 54 20 70 72 6f 63  ks or ABORT proc
1b140 65 73 73 69 6e 67 2c 20 6f 72 20 5b 44 52 4f 50  essing, or [DROP
1b150 20 54 41 42 4c 45 5d 20 70 72 6f 63 65 73 73 69   TABLE] processi
1b160 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 75 6e  ng.  The.** coun
1b170 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75  t does not inclu
1b180 64 65 20 72 6f 77 73 20 6f 66 20 76 69 65 77 73  de rows of views
1b190 20 74 68 61 74 20 66 69 72 65 20 61 6e 20 5b 49   that fire an [I
1b1a0 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65  NSTEAD OF trigge
1b1b0 72 5d 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 69 66  r],.** though if
1b1c0 20 74 68 65 20 49 4e 53 54 45 41 44 20 4f 46 20   the INSTEAD OF 
1b1d0 74 72 69 67 67 65 72 20 6d 61 6b 65 73 20 63 68  trigger makes ch
1b1e0 61 6e 67 65 73 20 6f 66 20 69 74 73 20 6f 77 6e  anges of its own
1b1f0 2c 20 74 68 6f 73 65 20 63 68 61 6e 67 65 73 20  , those changes 
1b200 0a 2a 2a 20 61 72 65 20 63 6f 75 6e 74 65 64 2e  .** are counted.
1b210 29 5e 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74  )^.** ^The sqlit
1b220 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
1b230 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6e  () function coun
1b240 74 73 20 74 68 65 20 63 68 61 6e 67 65 73 20 61  ts the changes a
1b250 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 74 68 65  s soon as.** the
1b260 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
1b270 6d 61 6b 65 73 20 74 68 65 6d 20 69 73 20 63 6f  makes them is co
1b280 6d 70 6c 65 74 65 64 20 28 77 68 65 6e 20 74 68  mpleted (when th
1b290 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  e statement hand
1b2a0 6c 65 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20  le.** is passed 
1b2b0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  to [sqlite3_rese
1b2c0 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  t()] or [sqlite3
1b2d0 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 29 2e 0a 2a  _finalize()])..*
1b2e0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68  *.** See also th
1b2f0 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  e [sqlite3_chang
1b300 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2c  es()] interface,
1b310 20 74 68 65 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63   the.** [count_c
1b320 68 61 6e 67 65 73 20 70 72 61 67 6d 61 5d 2c 20  hanges pragma], 
1b330 61 6e 64 20 74 68 65 20 5b 74 6f 74 61 6c 5f 63  and the [total_c
1b340 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e  hanges() SQL fun
1b350 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66  ction]..**.** If
1b360 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72 65   a separate thre
1b370 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73  ad makes changes
1b380 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74   on the same dat
1b390 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1b3a0 0a 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c 69 74  .** while [sqlit
1b3b0 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
1b3c0 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74  ()] is running t
1b3d0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a  hen the value.**
1b3e0 20 72 65 74 75 72 6e 65 64 20 69 73 20 75 6e 70   returned is unp
1b3f0 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6e  redictable and n
1b400 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a  ot meaningful..*
1b410 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1b420 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63   sqlite3_total_c
1b430 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29  hanges(sqlite3*)
1b440 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
1b450 46 3a 20 49 6e 74 65 72 72 75 70 74 20 41 20 4c  F: Interrupt A L
1b460 6f 6e 67 2d 52 75 6e 6e 69 6e 67 20 51 75 65 72  ong-Running Quer
1b470 79 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 66 75  y.**.** ^This fu
1b480 6e 63 74 69 6f 6e 20 63 61 75 73 65 73 20 61 6e  nction causes an
1b490 79 20 70 65 6e 64 69 6e 67 20 64 61 74 61 62 61  y pending databa
1b4a0 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  se operation to 
1b4b0 61 62 6f 72 74 20 61 6e 64 0a 2a 2a 20 72 65 74  abort and.** ret
1b4c0 75 72 6e 20 61 74 20 69 74 73 20 65 61 72 6c 69  urn at its earli
1b4d0 65 73 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e  est opportunity.
1b4e0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1b4f0 20 74 79 70 69 63 61 6c 6c 79 0a 2a 2a 20 63 61   typically.** ca
1b500 6c 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65  lled in response
1b510 20 74 6f 20 61 20 75 73 65 72 20 61 63 74 69 6f   to a user actio
1b520 6e 20 73 75 63 68 20 61 73 20 70 72 65 73 73 69  n such as pressi
1b530 6e 67 20 22 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f  ng "Cancel".** o
1b540 72 20 43 74 72 6c 2d 43 20 77 68 65 72 65 20 74  r Ctrl-C where t
1b550 68 65 20 75 73 65 72 20 77 61 6e 74 73 20 61 20  he user wants a 
1b560 6c 6f 6e 67 20 71 75 65 72 79 20 6f 70 65 72 61  long query opera
1b570 74 69 6f 6e 20 74 6f 20 68 61 6c 74 0a 2a 2a 20  tion to halt.** 
1b580 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a  immediately..**.
1b590 2a 2a 20 5e 49 74 20 69 73 20 73 61 66 65 20 74  ** ^It is safe t
1b5a0 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  o call this rout
1b5b0 69 6e 65 20 66 72 6f 6d 20 61 20 74 68 72 65 61  ine from a threa
1b5c0 64 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  d different from
1b5d0 20 74 68 65 0a 2a 2a 20 74 68 72 65 61 64 20 74   the.** thread t
1b5e0 68 61 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79  hat is currently
1b5f0 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 64 61 74   running the dat
1b600 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 2e  abase operation.
1b610 20 20 42 75 74 20 69 74 0a 2a 2a 20 69 73 20 6e    But it.** is n
1b620 6f 74 20 73 61 66 65 20 74 6f 20 63 61 6c 6c 20  ot safe to call 
1b630 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74  this routine wit
1b640 68 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f  h a [database co
1b650 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61 74 0a 2a  nnection] that.*
1b660 2a 20 69 73 20 63 6c 6f 73 65 64 20 6f 72 20 6d  * is closed or m
1b670 69 67 68 74 20 63 6c 6f 73 65 20 62 65 66 6f 72  ight close befor
1b680 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  e sqlite3_interr
1b690 75 70 74 28 29 20 72 65 74 75 72 6e 73 2e 0a 2a  upt() returns..*
1b6a0 2a 0a 2a 2a 20 5e 49 66 20 61 6e 20 53 51 4c 20  *.** ^If an SQL 
1b6b0 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 76 65 72  operation is ver
1b6c0 79 20 6e 65 61 72 6c 79 20 66 69 6e 69 73 68 65  y nearly finishe
1b6d0 64 20 61 74 20 74 68 65 20 74 69 6d 65 20 77 68  d at the time wh
1b6e0 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e  en.** sqlite3_in
1b6f0 74 65 72 72 75 70 74 28 29 20 69 73 20 63 61 6c  terrupt() is cal
1b700 6c 65 64 2c 20 74 68 65 6e 20 69 74 20 6d 69 67  led, then it mig
1b710 68 74 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 6f  ht not have an o
1b720 70 70 6f 72 74 75 6e 69 74 79 0a 2a 2a 20 74 6f  pportunity.** to
1b730 20 62 65 20 69 6e 74 65 72 72 75 70 74 65 64 20   be interrupted 
1b740 61 6e 64 20 6d 69 67 68 74 20 63 6f 6e 74 69 6e  and might contin
1b750 75 65 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e  ue to completion
1b760 2e 0a 2a 2a 0a 2a 2a 20 5e 41 6e 20 53 51 4c 20  ..**.** ^An SQL 
1b770 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 69  operation that i
1b780 73 20 69 6e 74 65 72 72 75 70 74 65 64 20 77 69  s interrupted wi
1b790 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54  ll return [SQLIT
1b7a0 45 5f 49 4e 54 45 52 52 55 50 54 5d 2e 0a 2a 2a  E_INTERRUPT]..**
1b7b0 20 5e 49 66 20 74 68 65 20 69 6e 74 65 72 72 75   ^If the interru
1b7c0 70 74 65 64 20 53 51 4c 20 6f 70 65 72 61 74 69  pted SQL operati
1b7d0 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 52 54 2c  on is an INSERT,
1b7e0 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45   UPDATE, or DELE
1b7f0 54 45 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e  TE.** that is in
1b800 73 69 64 65 20 61 6e 20 65 78 70 6c 69 63 69 74  side an explicit
1b810 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68   transaction, th
1b820 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20 74 72  en the entire tr
1b830 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c  ansaction.** wil
1b840 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  l be rolled back
1b850 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a   automatically..
1b860 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74  **.** ^The sqlit
1b870 65 33 5f 69 6e 74 65 72 72 75 70 74 28 44 29 20  e3_interrupt(D) 
1b880 63 61 6c 6c 20 69 73 20 69 6e 20 65 66 66 65 63  call is in effec
1b890 74 20 75 6e 74 69 6c 20 61 6c 6c 20 63 75 72 72  t until all curr
1b8a0 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 0a 2a 2a  ently running.**
1b8b0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
1b8c0 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  on [database con
1b8d0 6e 65 63 74 69 6f 6e 5d 20 44 20 63 6f 6d 70 6c  nection] D compl
1b8e0 65 74 65 2e 20 20 5e 41 6e 79 20 6e 65 77 20 53  ete.  ^Any new S
1b8f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a  QL statements.**
1b900 20 74 68 61 74 20 61 72 65 20 73 74 61 72 74 65   that are starte
1b910 64 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69  d after the sqli
1b920 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20  te3_interrupt() 
1b930 63 61 6c 6c 20 61 6e 64 20 62 65 66 6f 72 65 20  call and before 
1b940 74 68 65 20 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20  the .** running 
1b950 73 74 61 74 65 6d 65 6e 74 73 20 72 65 61 63 68  statements reach
1b960 65 73 20 7a 65 72 6f 20 61 72 65 20 69 6e 74 65  es zero are inte
1b970 72 72 75 70 74 65 64 20 61 73 20 69 66 20 74 68  rrupted as if th
1b980 65 79 20 68 61 64 20 62 65 65 6e 0a 2a 2a 20 72  ey had been.** r
1b990 75 6e 6e 69 6e 67 20 70 72 69 6f 72 20 74 6f 20  unning prior to 
1b9a0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  the sqlite3_inte
1b9b0 72 72 75 70 74 28 29 20 63 61 6c 6c 2e 20 20 5e  rrupt() call.  ^
1b9c0 4e 65 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  New SQL statemen
1b9d0 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73  ts.** that are s
1b9e0 74 61 72 74 65 64 20 61 66 74 65 72 20 74 68 65  tarted after the
1b9f0 20 72 75 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65   running stateme
1ba00 6e 74 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73  nt count reaches
1ba10 20 7a 65 72 6f 20 61 72 65 0a 2a 2a 20 6e 6f 74   zero are.** not
1ba20 20 65 66 66 65 63 74 65 64 20 62 79 20 74 68 65   effected by the
1ba30 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
1ba40 70 74 28 29 2e 0a 2a 2a 20 5e 41 20 63 61 6c 6c  pt()..** ^A call
1ba50 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 65   to sqlite3_inte
1ba60 72 72 75 70 74 28 44 29 20 74 68 61 74 20 6f 63  rrupt(D) that oc
1ba70 63 75 72 73 20 77 68 65 6e 20 74 68 65 72 65 20  curs when there 
1ba80 61 72 65 20 6e 6f 20 72 75 6e 6e 69 6e 67 0a 2a  are no running.*
1ba90 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  * SQL statements
1baa0 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20   is a no-op and 
1bab0 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e  has no effect on
1bac0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a   SQL statements.
1bad0 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61 72  ** that are star
1bae0 74 65 64 20 61 66 74 65 72 20 74 68 65 20 73 71  ted after the sq
1baf0 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
1bb00 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a  ) call returns..
1bb10 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74  **.** If the dat
1bb20 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1bb30 20 63 6c 6f 73 65 73 20 77 68 69 6c 65 20 5b 73   closes while [s
1bb40 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1bb50 28 29 5d 0a 2a 2a 20 69 73 20 72 75 6e 6e 69 6e  ()].** is runnin
1bb60 67 20 74 68 65 6e 20 62 61 64 20 74 68 69 6e 67  g then bad thing
1bb70 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61  s will likely ha
1bb80 70 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ppen..*/.SQLITE_
1bb90 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1bba0 5f 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74  _interrupt(sqlit
1bbb0 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  e3*);../*.** CAP
1bbc0 49 33 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65  I3REF: Determine
1bbd0 20 49 66 20 41 6e 20 53 51 4c 20 53 74 61 74 65   If An SQL State
1bbe0 6d 65 6e 74 20 49 73 20 43 6f 6d 70 6c 65 74 65  ment Is Complete
1bbf0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
1bc00 74 69 6e 65 73 20 61 72 65 20 75 73 65 66 75 6c  tines are useful
1bc10 20 64 75 72 69 6e 67 20 63 6f 6d 6d 61 6e 64 2d   during command-
1bc20 6c 69 6e 65 20 69 6e 70 75 74 20 74 6f 20 64 65  line input to de
1bc30 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 0a 2a  termine if the.*
1bc40 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65  * currently ente
1bc50 72 65 64 20 74 65 78 74 20 73 65 65 6d 73 20 74  red text seems t
1bc60 6f 20 66 6f 72 6d 20 61 20 63 6f 6d 70 6c 65 74  o form a complet
1bc70 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
1bc80 6f 72 0a 2a 2a 20 69 66 20 61 64 64 69 74 69 6f  or.** if additio
1bc90 6e 61 6c 20 69 6e 70 75 74 20 69 73 20 6e 65 65  nal input is nee
1bca0 64 65 64 20 62 65 66 6f 72 65 20 73 65 6e 64 69  ded before sendi
1bcb0 6e 67 20 74 68 65 20 74 65 78 74 20 69 6e 74 6f  ng the text into
1bcc0 0a 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 70  .** SQLite for p
1bcd0 61 72 73 69 6e 67 2e 20 20 5e 54 68 65 73 65 20  arsing.  ^These 
1bce0 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
1bcf0 31 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 73  1 if the input s
1bd00 74 72 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72 73  tring.** appears
1bd10 20 74 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65 74   to be a complet
1bd20 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  e SQL statement.
1bd30 20 20 5e 41 20 73 74 61 74 65 6d 65 6e 74 20 69    ^A statement i
1bd40 73 20 6a 75 64 67 65 64 20 74 6f 20 62 65 0a 2a  s judged to be.*
1bd50 2a 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 69 74  * complete if it
1bd60 20 65 6e 64 73 20 77 69 74 68 20 61 20 73 65 6d   ends with a sem
1bd70 69 63 6f 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64  icolon token and
1bd80 20 69 73 20 6e 6f 74 20 61 20 70 72 65 66 69 78   is not a prefix
1bd90 20 6f 66 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f   of a.** well-fo
1bda0 72 6d 65 64 20 43 52 45 41 54 45 20 54 52 49 47  rmed CREATE TRIG
1bdb0 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  GER statement.  
1bdc0 5e 53 65 6d 69 63 6f 6c 6f 6e 73 20 74 68 61 74  ^Semicolons that
1bdd0 20 61 72 65 20 65 6d 62 65 64 64 65 64 20 77 69   are embedded wi
1bde0 74 68 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c  thin.** string l
1bdf0 69 74 65 72 61 6c 73 20 6f 72 20 71 75 6f 74 65  iterals or quote
1be00 64 20 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d  d identifier nam
1be10 65 73 20 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 61  es or comments a
1be20 72 65 20 6e 6f 74 0a 2a 2a 20 69 6e 64 65 70 65  re not.** indepe
1be30 6e 64 65 6e 74 20 74 6f 6b 65 6e 73 20 28 74 68  ndent tokens (th
1be40 65 79 20 61 72 65 20 70 61 72 74 20 6f 66 20 74  ey are part of t
1be50 68 65 20 74 6f 6b 65 6e 20 69 6e 20 77 68 69 63  he token in whic
1be60 68 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 65 6d  h they are.** em
1be70 62 65 64 64 65 64 29 20 61 6e 64 20 74 68 75 73  bedded) and thus
1be80 20 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73   do not count as
1be90 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 65 72   a statement ter
1bea0 6d 69 6e 61 74 6f 72 2e 20 20 5e 57 68 69 74 65  minator.  ^White
1beb0 73 70 61 63 65 0a 2a 2a 20 61 6e 64 20 63 6f 6d  space.** and com
1bec0 6d 65 6e 74 73 20 74 68 61 74 20 66 6f 6c 6c 6f  ments that follo
1bed0 77 20 74 68 65 20 66 69 6e 61 6c 20 73 65 6d 69  w the final semi
1bee0 63 6f 6c 6f 6e 20 61 72 65 20 69 67 6e 6f 72 65  colon are ignore
1bef0 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 73 65 20  d..**.** ^These 
1bf00 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
1bf10 30 20 69 66 20 74 68 65 20 73 74 61 74 65 6d 65  0 if the stateme
1bf20 6e 74 20 69 73 20 69 6e 63 6f 6d 70 6c 65 74 65  nt is incomplete
1bf30 2e 20 20 5e 49 66 20 61 0a 2a 2a 20 6d 65 6d 6f  .  ^If a.** memo
1bf40 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  ry allocation fa
1bf50 69 6c 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 45  ils, then SQLITE
1bf60 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e  _NOMEM is return
1bf70 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 73 65  ed..**.** ^These
1bf80 20 72 6f 75 74 69 6e 65 73 20 64 6f 20 6e 6f 74   routines do not
1bf90 20 70 61 72 73 65 20 74 68 65 20 53 51 4c 20 73   parse the SQL s
1bfa0 74 61 74 65 6d 65 6e 74 73 20 74 68 75 73 0a 2a  tatements thus.*
1bfb0 2a 20 77 69 6c 6c 20 6e 6f 74 20 64 65 74 65 63  * will not detec
1bfc0 74 20 73 79 6e 74 61 63 74 69 63 61 6c 6c 79 20  t syntactically 
1bfd0 69 6e 63 6f 72 72 65 63 74 20 53 51 4c 2e 0a 2a  incorrect SQL..*
1bfe0 2a 0a 2a 2a 20 5e 28 49 66 20 53 51 4c 69 74 65  *.** ^(If SQLite
1bff0 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 69 6e   has not been in
1c000 69 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20  itialized using 
1c010 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c  [sqlite3_initial
1c020 69 7a 65 28 29 5d 20 70 72 69 6f 72 20 0a 2a 2a  ize()] prior .**
1c030 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c   to invoking sql
1c040 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28  ite3_complete16(
1c050 29 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 69  ) then sqlite3_i
1c060 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69  nitialize() is i
1c070 6e 76 6f 6b 65 64 0a 2a 2a 20 61 75 74 6f 6d 61  nvoked.** automa
1c080 74 69 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74  tically by sqlit
1c090 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 2e  e3_complete16().
1c0a0 20 20 49 66 20 74 68 61 74 20 69 6e 69 74 69 61    If that initia
1c0b0 6c 69 7a 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a  lization fails,.
1c0c0 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65 74 75  ** then the retu
1c0d0 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71  rn value from sq
1c0e0 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36  lite3_complete16
1c0f0 28 29 20 77 69 6c 6c 20 62 65 20 6e 6f 6e 2d 7a  () will be non-z
1c100 65 72 6f 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73  ero.** regardles
1c110 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20  s of whether or 
1c120 6e 6f 74 20 74 68 65 20 69 6e 70 75 74 20 53 51  not the input SQ
1c130 4c 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 29 5e  L is complete.)^
1c140 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74  .**.** The input
1c150 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d   to [sqlite3_com
1c160 70 6c 65 74 65 28 29 5d 20 6d 75 73 74 20 62 65  plete()] must be
1c170 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74   a zero-terminat
1c180 65 64 0a 2a 2a 20 55 54 46 2d 38 20 73 74 72 69  ed.** UTF-8 stri
1c190 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e  ng..**.** The in
1c1a0 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  put to [sqlite3_
1c1b0 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d 20 6d 75  complete16()] mu
1c1c0 73 74 20 62 65 20 61 20 7a 65 72 6f 2d 74 65 72  st be a zero-ter
1c1d0 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 31  minated.** UTF-1
1c1e0 36 20 73 74 72 69 6e 67 20 69 6e 20 6e 61 74 69  6 string in nati
1c1f0 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a  ve byte order..*
1c200 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1c210 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
1c220 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  e(const char *sq
1c230 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  l);.SQLITE_API i
1c240 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  nt sqlite3_compl
1c250 65 74 65 31 36 28 63 6f 6e 73 74 20 76 6f 69 64  ete16(const void
1c260 20 2a 73 71 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43   *sql);../*.** C
1c270 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65  API3REF: Registe
1c280 72 20 41 20 43 61 6c 6c 62 61 63 6b 20 54 6f 20  r A Callback To 
1c290 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 5f 42 55  Handle SQLITE_BU
1c2a0 53 59 20 45 72 72 6f 72 73 0a 2a 2a 0a 2a 2a 20  SY Errors.**.** 
1c2b0 5e 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65  ^This routine se
1c2c0 74 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75  ts a callback fu
1c2d0 6e 63 74 69 6f 6e 20 74 68 61 74 20 6d 69 67 68  nction that migh
1c2e0 74 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65  t be invoked whe
1c2f0 6e 65 76 65 72 0a 2a 2a 20 61 6e 20 61 74 74 65  never.** an atte
1c300 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f  mpt is made to o
1c310 70 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 74  pen a database t
1c320 61 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65  able that anothe
1c330 72 20 74 68 72 65 61 64 0a 2a 2a 20 6f 72 20 70  r thread.** or p
1c340 72 6f 63 65 73 73 20 68 61 73 20 6c 6f 63 6b 65  rocess has locke
1c350 64 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 74 68 65  d..**.** ^If the
1c360 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69   busy callback i
1c370 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 5b 53 51  s NULL, then [SQ
1c380 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53  LITE_BUSY] or [S
1c390 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43  QLITE_IOERR_BLOC
1c3a0 4b 45 44 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72  KED].** is retur
1c3b0 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ned immediately 
1c3c0 75 70 6f 6e 20 65 6e 63 6f 75 6e 74 65 72 69 6e  upon encounterin
1c3d0 67 20 74 68 65 20 6c 6f 63 6b 2e 20 20 5e 49 66  g the lock.  ^If
1c3e0 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61   the busy callba
1c3f0 63 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c  ck.** is not NUL
1c400 4c 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c  L, then the call
1c410 62 61 63 6b 20 6d 69 67 68 74 20 62 65 20 69 6e  back might be in
1c420 76 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20 61  voked with two a
1c430 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  rguments..**.** 
1c440 5e 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d  ^The first argum
1c450 65 6e 74 20 74 6f 20 74 68 65 20 62 75 73 79 20  ent to the busy 
1c460 68 61 6e 64 6c 65 72 20 69 73 20 61 20 63 6f 70  handler is a cop
1c470 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70  y of the void* p
1c480 6f 69 6e 74 65 72 20 77 68 69 63 68 0a 2a 2a 20  ointer which.** 
1c490 69 73 20 74 68 65 20 74 68 69 72 64 20 61 72 67  is the third arg
1c4a0 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
1c4b0 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 2e  _busy_handler().
1c4c0 20 20 5e 54 68 65 20 73 65 63 6f 6e 64 20 61 72    ^The second ar
1c4d0 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65  gument to.** the
1c4e0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 63 61   busy handler ca
1c4f0 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e 75  llback is the nu
1c500 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68  mber of times th
1c510 61 74 20 74 68 65 20 62 75 73 79 20 68 61 6e 64  at the busy hand
1c520 6c 65 72 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20  ler has.** been 
1c530 69 6e 76 6f 6b 65 64 20 66 6f 72 20 74 68 69 73  invoked for this
1c540 20 6c 6f 63 6b 69 6e 67 20 65 76 65 6e 74 2e 20   locking event. 
1c550 20 5e 49 66 20 74 68 65 0a 2a 2a 20 62 75 73 79   ^If the.** busy
1c560 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
1c570 73 20 30 2c 20 74 68 65 6e 20 6e 6f 20 61 64 64  s 0, then no add
1c580 69 74 69 6f 6e 61 6c 20 61 74 74 65 6d 70 74 73  itional attempts
1c590 20 61 72 65 20 6d 61 64 65 20 74 6f 0a 2a 2a 20   are made to.** 
1c5a0 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61 62  access the datab
1c5b0 61 73 65 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f  ase and [SQLITE_
1c5c0 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45  BUSY] or [SQLITE
1c5d0 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20  _IOERR_BLOCKED] 
1c5e0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
1c5f0 5e 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  ^If the callback
1c600 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
1c610 6f 2c 20 74 68 65 6e 20 61 6e 6f 74 68 65 72 20  o, then another 
1c620 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61  attempt.** is ma
1c630 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 64  de to open the d
1c640 61 74 61 62 61 73 65 20 66 6f 72 20 72 65 61 64  atabase for read
1c650 69 6e 67 20 61 6e 64 20 74 68 65 20 63 79 63 6c  ing and the cycl
1c660 65 20 72 65 70 65 61 74 73 2e 0a 2a 2a 0a 2a 2a  e repeats..**.**
1c670 20 54 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66   The presence of
1c680 20 61 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20   a busy handler 
1c690 64 6f 65 73 20 6e 6f 74 20 67 75 61 72 61 6e 74  does not guarant
1c6a0 65 65 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20  ee that it will 
1c6b0 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68  be invoked.** wh
1c6c0 65 6e 20 74 68 65 72 65 20 69 73 20 6c 6f 63 6b  en there is lock
1c6d0 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 20 5e 49 66   contention. ^If
1c6e0 20 53 51 4c 69 74 65 20 64 65 74 65 72 6d 69 6e   SQLite determin
1c6f0 65 73 20 74 68 61 74 20 69 6e 76 6f 6b 69 6e 67  es that invoking
1c700 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 68 61 6e   the busy.** han
1c710 64 6c 65 72 20 63 6f 75 6c 64 20 72 65 73 75 6c  dler could resul
1c720 74 20 69 6e 20 61 20 64 65 61 64 6c 6f 63 6b 2c  t in a deadlock,
1c730 20 69 74 20 77 69 6c 6c 20 67 6f 20 61 68 65 61   it will go ahea
1c740 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51  d and return [SQ
1c750 4c 49 54 45 5f 42 55 53 59 5d 0a 2a 2a 20 6f 72  LITE_BUSY].** or
1c760 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42   [SQLITE_IOERR_B
1c770 4c 4f 43 4b 45 44 5d 20 69 6e 73 74 65 61 64 20  LOCKED] instead 
1c780 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20  of invoking the 
1c790 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a  busy handler..**
1c7a0 20 43 6f 6e 73 69 64 65 72 20 61 20 73 63 65 6e   Consider a scen
1c7b0 61 72 69 6f 20 77 68 65 72 65 20 6f 6e 65 20 70  ario where one p
1c7c0 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e  rocess is holdin
1c7d0 67 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 74 68  g a read lock th
1c7e0 61 74 0a 2a 2a 20 69 74 20 69 73 20 74 72 79 69  at.** it is tryi
1c7f0 6e 67 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f  ng to promote to
1c800 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b   a reserved lock
1c810 20 61 6e 64 0a 2a 2a 20 61 20 73 65 63 6f 6e 64   and.** a second
1c820 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64   process is hold
1c830 69 6e 67 20 61 20 72 65 73 65 72 76 65 64 20 6c  ing a reserved l
1c840 6f 63 6b 20 74 68 61 74 20 69 74 20 69 73 20 74  ock that it is t
1c850 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 70 72 6f 6d  rying.** to prom
1c860 6f 74 65 20 74 6f 20 61 6e 20 65 78 63 6c 75 73  ote to an exclus
1c870 69 76 65 20 6c 6f 63 6b 2e 20 20 54 68 65 20 66  ive lock.  The f
1c880 69 72 73 74 20 70 72 6f 63 65 73 73 20 63 61 6e  irst process can
1c890 6e 6f 74 20 70 72 6f 63 65 65 64 0a 2a 2a 20 62  not proceed.** b
1c8a0 65 63 61 75 73 65 20 69 74 20 69 73 20 62 6c 6f  ecause it is blo
1c8b0 63 6b 65 64 20 62 79 20 74 68 65 20 73 65 63 6f  cked by the seco
1c8c0 6e 64 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  nd and the secon
1c8d0 64 20 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74  d process cannot
1c8e0 0a 2a 2a 20 70 72 6f 63 65 65 64 20 62 65 63 61  .** proceed beca
1c8f0 75 73 65 20 69 74 20 69 73 20 62 6c 6f 63 6b 65  use it is blocke
1c900 64 20 62 79 20 74 68 65 20 66 69 72 73 74 2e 20  d by the first. 
1c910 20 49 66 20 62 6f 74 68 20 70 72 6f 63 65 73 73   If both process
1c920 65 73 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65  es.** invoke the
1c930 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2c 20   busy handlers, 
1c940 6e 65 69 74 68 65 72 20 77 69 6c 6c 20 6d 61 6b  neither will mak
1c950 65 20 61 6e 79 20 70 72 6f 67 72 65 73 73 2e 20  e any progress. 
1c960 20 54 68 65 72 65 66 6f 72 65 2c 0a 2a 2a 20 53   Therefore,.** S
1c970 51 4c 69 74 65 20 72 65 74 75 72 6e 73 20 5b 53  QLite returns [S
1c980 51 4c 49 54 45 5f 42 55 53 59 5d 20 66 6f 72 20  QLITE_BUSY] for 
1c990 74 68 65 20 66 69 72 73 74 20 70 72 6f 63 65 73  the first proces
1c9a0 73 2c 20 68 6f 70 69 6e 67 20 74 68 61 74 20 74  s, hoping that t
1c9b0 68 69 73 0a 2a 2a 20 77 69 6c 6c 20 69 6e 64 75  his.** will indu
1c9c0 63 65 20 74 68 65 20 66 69 72 73 74 20 70 72 6f  ce the first pro
1c9d0 63 65 73 73 20 74 6f 20 72 65 6c 65 61 73 65 20  cess to release 
1c9e0 69 74 73 20 72 65 61 64 20 6c 6f 63 6b 20 61 6e  its read lock an
1c9f0 64 20 61 6c 6c 6f 77 0a 2a 2a 20 74 68 65 20 73  d allow.** the s
1ca00 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 74 6f  econd process to
1ca10 20 70 72 6f 63 65 65 64 2e 0a 2a 2a 0a 2a 2a 20   proceed..**.** 
1ca20 5e 54 68 65 20 64 65 66 61 75 6c 74 20 62 75 73  ^The default bus
1ca30 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55  y callback is NU
1ca40 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 5b  LL..**.** ^The [
1ca50 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72  SQLITE_BUSY] err
1ca60 6f 72 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20  or is converted 
1ca70 74 6f 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  to [SQLITE_IOERR
1ca80 5f 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 77 68 65  _BLOCKED].** whe
1ca90 6e 20 53 51 4c 69 74 65 20 69 73 20 69 6e 20 74  n SQLite is in t
1caa0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 6c  he middle of a l
1cab0 61 72 67 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  arge transaction
1cac0 20 77 68 65 72 65 20 61 6c 6c 20 74 68 65 0a 2a   where all the.*
1cad0 2a 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 6e  * changes will n
1cae0 6f 74 20 66 69 74 20 69 6e 74 6f 20 74 68 65 20  ot fit into the 
1caf0 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e  in-memory cache.
1cb00 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a    SQLite will.**
1cb10 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20   already hold a 
1cb20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e  RESERVED lock on
1cb30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1cb40 6c 65 2c 20 62 75 74 20 69 74 20 6e 65 65 64 73  le, but it needs
1cb50 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74  .** to promote t
1cb60 68 69 73 20 6c 6f 63 6b 20 74 6f 20 45 58 43 4c  his lock to EXCL
1cb70 55 53 49 56 45 20 73 6f 20 74 68 61 74 20 69 74  USIVE so that it
1cb80 20 63 61 6e 20 73 70 69 6c 6c 20 63 61 63 68 65   can spill cache
1cb90 0a 2a 2a 20 70 61 67 65 73 20 69 6e 74 6f 20 74  .** pages into t
1cba0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1cbb0 20 77 69 74 68 6f 75 74 20 68 61 72 6d 20 74 6f   without harm to
1cbc0 20 63 6f 6e 63 75 72 72 65 6e 74 0a 2a 2a 20 72   concurrent.** r
1cbd0 65 61 64 65 72 73 2e 20 20 5e 49 66 20 69 74 20  eaders.  ^If it 
1cbe0 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 70 72 6f  is unable to pro
1cbf0 6d 6f 74 65 20 74 68 65 20 6c 6f 63 6b 2c 20 74  mote the lock, t
1cc00 68 65 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  hen the in-memor
1cc10 79 0a 2a 2a 20 63 61 63 68 65 20 77 69 6c 6c 20  y.** cache will 
1cc20 62 65 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e  be left in an in
1cc30 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65  consistent state
1cc40 20 61 6e 64 20 73 6f 20 74 68 65 20 65 72 72 6f   and so the erro
1cc50 72 0a 2a 2a 20 63 6f 64 65 20 69 73 20 70 72 6f  r.** code is pro
1cc60 6d 6f 74 65 64 20 66 72 6f 6d 20 74 68 65 20 72  moted from the r
1cc70 65 6c 61 74 69 76 65 6c 79 20 62 65 6e 69 67 6e  elatively benign
1cc80 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 74   [SQLITE_BUSY] t
1cc90 6f 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73 65  o.** the more se
1cca0 76 65 72 65 20 5b 53 51 4c 49 54 45 5f 49 4f 45  vere [SQLITE_IOE
1ccb0 52 52 5f 42 4c 4f 43 4b 45 44 5d 2e 20 20 5e 54  RR_BLOCKED].  ^T
1ccc0 68 69 73 20 65 72 72 6f 72 20 63 6f 64 65 20 70  his error code p
1ccd0 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a 20 66 6f 72 63  romotion.** forc
1cce0 65 73 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20  es an automatic 
1ccf0 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65 20  rollback of the 
1cd00 63 68 61 6e 67 65 73 2e 20 20 53 65 65 20 74 68  changes.  See th
1cd10 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 2f 63  e.** <a href="/c
1cd20 76 73 74 72 61 63 2f 77 69 6b 69 3f 70 3d 43 6f  vstrac/wiki?p=Co
1cd30 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e  rruptionFollowin
1cd40 67 42 75 73 79 45 72 72 6f 72 22 3e 0a 2a 2a 20  gBusyError">.** 
1cd50 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77  CorruptionFollow
1cd60 69 6e 67 42 75 73 79 45 72 72 6f 72 3c 2f 61 3e  ingBusyError</a>
1cd70 20 77 69 6b 69 20 70 61 67 65 20 66 6f 72 20 61   wiki page for a
1cd80 20 64 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 77   discussion of w
1cd90 68 79 0a 2a 2a 20 74 68 69 73 20 69 73 20 69 6d  hy.** this is im
1cda0 70 6f 72 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 5e  portant..**.** ^
1cdb0 28 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20  (There can only 
1cdc0 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73 79  be a single busy
1cdd0 20 68 61 6e 64 6c 65 72 20 64 65 66 69 6e 65 64   handler defined
1cde0 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 5b 64 61   for each.** [da
1cdf0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1ce00 6e 5d 2e 20 20 53 65 74 74 69 6e 67 20 61 20 6e  n].  Setting a n
1ce10 65 77 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20  ew busy handler 
1ce20 63 6c 65 61 72 73 20 61 6e 79 0a 2a 2a 20 70 72  clears any.** pr
1ce30 65 76 69 6f 75 73 6c 79 20 73 65 74 20 68 61 6e  eviously set han
1ce40 64 6c 65 72 2e 29 5e 20 20 5e 4e 6f 74 65 20 74  dler.)^  ^Note t
1ce50 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c  hat calling [sql
1ce60 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75  ite3_busy_timeou
1ce70 74 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73  t()].** will als
1ce80 6f 20 73 65 74 20 6f 72 20 63 6c 65 61 72 20 74  o set or clear t
1ce90 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e  he busy handler.
1cea0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79 20  .**.** The busy 
1ceb0 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20  callback should 
1cec0 6e 6f 74 20 74 61 6b 65 20 61 6e 79 20 61 63 74  not take any act
1ced0 69 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64 69 66  ions which modif
1cee0 79 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  y the.** databas
1cef0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61  e connection tha
1cf00 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75  t invoked the bu
1cf10 73 79 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e 79  sy handler.  Any
1cf20 20 73 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a 2a   such actions.**
1cf30 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 66   result in undef
1cf40 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a  ined behavior..*
1cf50 2a 20 0a 2a 2a 20 41 20 62 75 73 79 20 68 61 6e  * .** A busy han
1cf60 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 63 6c  dler must not cl
1cf70 6f 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ose the database
1cf80 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f   connection.** o
1cf90 72 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  r [prepared stat
1cfa0 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 6e 76 6f  ement] that invo
1cfb0 6b 65 64 20 74 68 65 20 62 75 73 79 20 68 61 6e  ked the busy han
1cfc0 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dler..*/.SQLITE_
1cfd0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1cfe0 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 73 71 6c  busy_handler(sql
1cff0 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f  ite3*, int(*)(vo
1d000 69 64 2a 2c 69 6e 74 29 2c 20 76 6f 69 64 2a 29  id*,int), void*)
1d010 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
1d020 46 3a 20 53 65 74 20 41 20 42 75 73 79 20 54 69  F: Set A Busy Ti
1d030 6d 65 6f 75 74 0a 2a 2a 0a 2a 2a 20 5e 54 68 69  meout.**.** ^Thi
1d040 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61  s routine sets a
1d050 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68   [sqlite3_busy_h
1d060 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 20 68 61  andler | busy ha
1d070 6e 64 6c 65 72 5d 20 74 68 61 74 20 73 6c 65 65  ndler] that slee
1d080 70 73 0a 2a 2a 20 66 6f 72 20 61 20 73 70 65 63  ps.** for a spec
1d090 69 66 69 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20  ified amount of 
1d0a0 74 69 6d 65 20 77 68 65 6e 20 61 20 74 61 62 6c  time when a tabl
1d0b0 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 5e 54  e is locked.  ^T
1d0c0 68 65 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 69  he handler.** wi
1d0d0 6c 6c 20 73 6c 65 65 70 20 6d 75 6c 74 69 70 6c  ll sleep multipl
1d0e0 65 20 74 69 6d 65 73 20 75 6e 74 69 6c 20 61 74  e times until at
1d0f0 20 6c 65 61 73 74 20 22 6d 73 22 20 6d 69 6c 6c   least "ms" mill
1d100 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65  iseconds of slee
1d110 70 69 6e 67 0a 2a 2a 20 68 61 76 65 20 61 63 63  ping.** have acc
1d120 75 6d 75 6c 61 74 65 64 2e 20 20 5e 41 66 74 65  umulated.  ^Afte
1d130 72 20 61 74 20 6c 65 61 73 74 20 22 6d 73 22 20  r at least "ms" 
1d140 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20  milliseconds of 
1d150 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a 20 74 68 65  sleeping,.** the
1d160 20 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73   handler returns
1d170 20 30 20 77 68 69 63 68 20 63 61 75 73 65 73 20   0 which causes 
1d180 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
1d190 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53   to return.** [S
1d1a0 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b  QLITE_BUSY] or [
1d1b0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f  SQLITE_IOERR_BLO
1d1c0 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 43 61  CKED]..**.** ^Ca
1d1d0 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69  lling this routi
1d1e0 6e 65 20 77 69 74 68 20 61 6e 20 61 72 67 75 6d  ne with an argum
1d1f0 65 6e 74 20 6c 65 73 73 20 74 68 61 6e 20 6f 72  ent less than or
1d200 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 0a 2a   equal to zero.*
1d210 2a 20 74 75 72 6e 73 20 6f 66 66 20 61 6c 6c 20  * turns off all 
1d220 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2e 0a 2a  busy handlers..*
1d230 2a 0a 2a 2a 20 5e 28 54 68 65 72 65 20 63 61 6e  *.** ^(There can
1d240 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c   only be a singl
1d250 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66  e busy handler f
1d260 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a  or a particular.
1d270 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
1d280 6e 65 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e 79  nection] any any
1d290 20 67 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 20   given moment.  
1d2a0 49 66 20 61 6e 6f 74 68 65 72 20 62 75 73 79 20  If another busy 
1d2b0 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20 64  handler.** was d
1d2c0 65 66 69 6e 65 64 20 20 28 75 73 69 6e 67 20 5b  efined  (using [
1d2d0 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e  sqlite3_busy_han
1d2e0 64 6c 65 72 28 29 5d 29 20 70 72 69 6f 72 20 74  dler()]) prior t
1d2f0 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69  o calling.** thi
1d300 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 61 74 20  s routine, that 
1d310 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c  other busy handl
1d320 65 72 20 69 73 20 63 6c 65 61 72 65 64 2e 29 5e  er is cleared.)^
1d330 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1d340 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  nt sqlite3_busy_
1d350 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 2a  timeout(sqlite3*
1d360 2c 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a  , int ms);../*.*
1d370 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 76  * CAPI3REF: Conv
1d380 65 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 73  enience Routines
1d390 20 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 65   For Running Que
1d3a0 72 69 65 73 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ries.**.** This 
1d3b0 69 73 20 61 20 6c 65 67 61 63 79 20 69 6e 74 65  is a legacy inte
1d3c0 72 66 61 63 65 20 74 68 61 74 20 69 73 20 70 72  rface that is pr
1d3d0 65 73 65 72 76 65 64 20 66 6f 72 20 62 61 63 6b  eserved for back
1d3e0 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c  wards compatibil
1d3f0 69 74 79 2e 0a 2a 2a 20 55 73 65 20 6f 66 20 74  ity..** Use of t
1d400 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73  his interface is
1d410 20 6e 6f 74 20 72 65 63 6f 6d 6d 65 6e 64 65 64   not recommended
1d420 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 69 6e 69 74 69  ..**.** Definiti
1d430 6f 6e 3a 20 41 20 3c 62 3e 72 65 73 75 6c 74 20  on: A <b>result 
1d440 74 61 62 6c 65 3c 2f 62 3e 20 69 73 20 6d 65 6d  table</b> is mem
1d450 6f 72 79 20 64 61 74 61 20 73 74 72 75 63 74 75  ory data structu
1d460 72 65 20 63 72 65 61 74 65 64 20 62 79 20 74 68  re created by th
1d470 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65  e.** [sqlite3_ge
1d480 74 5f 74 61 62 6c 65 28 29 5d 20 69 6e 74 65 72  t_table()] inter
1d490 66 61 63 65 2e 20 20 41 20 72 65 73 75 6c 74 20  face.  A result 
1d4a0 74 61 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68  table records th
1d4b0 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 20 71 75  e.** complete qu
1d4c0 65 72 79 20 72 65 73 75 6c 74 73 20 66 72 6f 6d  ery results from
1d4d0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 71 75 65   one or more que
1d4e0 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ries..**.** The 
1d4f0 74 61 62 6c 65 20 63 6f 6e 63 65 70 74 75 61 6c  table conceptual
1d500 6c 79 20 68 61 73 20 61 20 6e 75 6d 62 65 72 20  ly has a number 
1d510 6f 66 20 72 6f 77 73 20 61 6e 64 20 63 6f 6c 75  of rows and colu
1d520 6d 6e 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65  mns.  But.** the
1d530 73 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6e  se numbers are n
1d540 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 72  ot part of the r
1d550 65 73 75 6c 74 20 74 61 62 6c 65 20 69 74 73 65  esult table itse
1d560 6c 66 2e 20 20 54 68 65 73 65 0a 2a 2a 20 6e 75  lf.  These.** nu
1d570 6d 62 65 72 73 20 61 72 65 20 6f 62 74 61 69 6e  mbers are obtain
1d580 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e 20 20  ed separately.  
1d590 4c 65 74 20 4e 20 62 65 20 74 68 65 20 6e 75 6d  Let N be the num
1d5a0 62 65 72 20 6f 66 20 72 6f 77 73 0a 2a 2a 20 61  ber of rows.** a
1d5b0 6e 64 20 4d 20 62 65 20 74 68 65 20 6e 75 6d 62  nd M be the numb
1d5c0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 0a 2a  er of columns..*
1d5d0 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61  *.** A result ta
1d5e0 62 6c 65 20 69 73 20 61 6e 20 61 72 72 61 79 20  ble is an array 
1d5f0 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 7a  of pointers to z
1d600 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  ero-terminated U
1d610 54 46 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a  TF-8 strings..**
1d620 20 54 68 65 72 65 20 61 72 65 20 28 4e 2b 31 29   There are (N+1)
1d630 2a 4d 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74  *M elements in t
1d640 68 65 20 61 72 72 61 79 2e 20 20 54 68 65 20 66  he array.  The f
1d650 69 72 73 74 20 4d 20 70 6f 69 6e 74 65 72 73 20  irst M pointers 
1d660 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 7a 65 72 6f  point.** to zero
1d670 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69  -terminated stri
1d680 6e 67 73 20 74 68 61 74 20 20 63 6f 6e 74 61 69  ngs that  contai
1d690 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74  n the names of t
1d6a0 68 65 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 20 54  he columns..** T
1d6b0 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 65 6e 74  he remaining ent
1d6c0 72 69 65 73 20 61 6c 6c 20 70 6f 69 6e 74 20 74  ries all point t
1d6d0 6f 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 2e  o query results.
1d6e0 20 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 72 65    NULL values re
1d6f0 73 75 6c 74 0a 2a 2a 20 69 6e 20 4e 55 4c 4c 20  sult.** in NULL 
1d700 70 6f 69 6e 74 65 72 73 2e 20 20 41 6c 6c 20 6f  pointers.  All o
1d710 74 68 65 72 20 76 61 6c 75 65 73 20 61 72 65 20  ther values are 
1d720 69 6e 20 74 68 65 69 72 20 55 54 46 2d 38 20 7a  in their UTF-8 z
1d730 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a  ero-terminated.*
1d740 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  * string represe
1d750 6e 74 61 74 69 6f 6e 20 61 73 20 72 65 74 75 72  ntation as retur
1d760 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  ned by [sqlite3_
1d770 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2e 0a  column_text()]..
1d780 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74  **.** A result t
1d790 61 62 6c 65 20 6d 69 67 68 74 20 63 6f 6e 73 69  able might consi
1d7a0 73 74 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72  st of one or mor
1d7b0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
1d7c0 69 6f 6e 73 2e 0a 2a 2a 20 49 74 20 69 73 20 6e  ions..** It is n
1d7d0 6f 74 20 73 61 66 65 20 74 6f 20 70 61 73 73 20  ot safe to pass 
1d7e0 61 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 64  a result table d
1d7f0 69 72 65 63 74 6c 79 20 74 6f 20 5b 73 71 6c 69  irectly to [sqli
1d800 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 20  te3_free()]..** 
1d810 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 73  A result table s
1d820 68 6f 75 6c 64 20 62 65 20 64 65 61 6c 6c 6f 63  hould be dealloc
1d830 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69  ated using [sqli
1d840 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29  te3_free_table()
1d850 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 28 41 73 20 61 6e  ]..**.** ^(As an
1d860 20 65 78 61 6d 70 6c 65 20 6f 66 20 74 68 65 20   example of the 
1d870 72 65 73 75 6c 74 20 74 61 62 6c 65 20 66 6f 72  result table for
1d880 6d 61 74 2c 20 73 75 70 70 6f 73 65 20 61 20 71  mat, suppose a q
1d890 75 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a 20 69  uery result.** i
1d8a0 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s as follows:.**
1d8b0 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  .** <blockquote>
1d8c0 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20  <pre>.**        
1d8d0 4e 61 6d 65 20 20 20 20 20 20 20 20 7c 20 41 67  Name        | Ag
1d8e0 65 0a 2a 2a 20 20 20 20 20 20 20 20 2d 2d 2d 2d  e.**        ----
1d8f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d900 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 20 41 6c  ---.**        Al
1d910 69 63 65 20 20 20 20 20 20 20 7c 20 34 33 0a 2a  ice       | 43.*
1d920 2a 20 20 20 20 20 20 20 20 42 6f 62 20 20 20 20  *        Bob    
1d930 20 20 20 20 20 7c 20 32 38 0a 2a 2a 20 20 20 20       | 28.**    
1d940 20 20 20 20 43 69 6e 64 79 20 20 20 20 20 20 20      Cindy       
1d950 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f  | 21.** </pre></
1d960 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a  blockquote>.**.*
1d970 2a 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 20  * There are two 
1d980 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 29 20 61 6e  column (M==2) an
1d990 64 20 74 68 72 65 65 20 72 6f 77 73 20 28 4e 3d  d three rows (N=
1d9a0 3d 33 29 2e 20 20 54 68 75 73 20 74 68 65 0a 2a  =3).  Thus the.*
1d9b0 2a 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 68  * result table h
1d9c0 61 73 20 38 20 65 6e 74 72 69 65 73 2e 20 20 53  as 8 entries.  S
1d9d0 75 70 70 6f 73 65 20 74 68 65 20 72 65 73 75 6c  uppose the resul
1d9e0 74 20 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65  t table is store
1d9f0 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 72 61 79  d.** in an array
1da00 20 6e 61 6d 65 73 20 61 7a 52 65 73 75 6c 74 2e   names azResult.
1da10 20 20 54 68 65 6e 20 61 7a 52 65 73 75 6c 74 20    Then azResult 
1da20 68 6f 6c 64 73 20 74 68 69 73 20 63 6f 6e 74 65  holds this conte
1da30 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b  nt:.**.** <block
1da40 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20  quote><pre>.**  
1da50 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23        azResult&#
1da60 39 31 3b 30 5d 20 3d 20 22 4e 61 6d 65 22 3b 0a  91;0] = "Name";.
1da70 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75  **        azResu
1da80 6c 74 26 23 39 31 3b 31 5d 20 3d 20 22 41 67 65  lt&#91;1] = "Age
1da90 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52  ";.**        azR
1daa0 65 73 75 6c 74 26 23 39 31 3b 32 5d 20 3d 20 22  esult&#91;2] = "
1dab0 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 20 20 20 20  Alice";.**      
1dac0 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 33    azResult&#91;3
1dad0 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 20 20 20 20  ] = "43";.**    
1dae0 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31      azResult&#91
1daf0 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a 2a 2a 20  ;4] = "Bob";.** 
1db00 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26         azResult&
1db10 23 39 31 3b 35 5d 20 3d 20 22 32 38 22 3b 0a 2a  #91;5] = "28";.*
1db20 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c  *        azResul
1db30 74 26 23 39 31 3b 36 5d 20 3d 20 22 43 69 6e 64  t&#91;6] = "Cind
1db40 79 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a  y";.**        az
1db50 52 65 73 75 6c 74 26 23 39 31 3b 37 5d 20 3d 20  Result&#91;7] = 
1db60 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  "21";.** </pre><
1db70 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 29 5e 0a 2a  /blockquote>)^.*
1db80 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65  *.** ^The sqlite
1db90 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66 75  3_get_table() fu
1dba0 6e 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 73  nction evaluates
1dbb0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20   one or more.** 
1dbc0 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61  semicolon-separa
1dbd0 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ted SQL statemen
1dbe0 74 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 74  ts in the zero-t
1dbf0 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 0a  erminated UTF-8.
1dc00 2a 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74 73  ** string of its
1dc10 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61   2nd parameter a
1dc20 6e 64 20 72 65 74 75 72 6e 73 20 61 20 72 65 73  nd returns a res
1dc30 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 68 65  ult table to the
1dc40 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 76 65  .** pointer give
1dc50 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70 61 72  n in its 3rd par
1dc60 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 66  ameter..**.** Af
1dc70 74 65 72 20 74 68 65 20 61 70 70 6c 69 63 61 74  ter the applicat
1dc80 69 6f 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64  ion has finished
1dc90 20 77 69 74 68 20 74 68 65 20 72 65 73 75 6c 74   with the result
1dca0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65   from sqlite3_ge
1dcb0 74 5f 74 61 62 6c 65 28 29 2c 0a 2a 2a 20 69 74  t_table(),.** it
1dcc0 20 6d 75 73 74 20 70 61 73 73 20 74 68 65 20 72   must pass the r
1dcd0 65 73 75 6c 74 20 74 61 62 6c 65 20 70 6f 69 6e  esult table poin
1dce0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 66  ter to sqlite3_f
1dcf0 72 65 65 5f 74 61 62 6c 65 28 29 20 69 6e 20 6f  ree_table() in o
1dd00 72 64 65 72 20 74 6f 0a 2a 2a 20 72 65 6c 65 61  rder to.** relea
1dd10 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68  se the memory th
1dd20 61 74 20 77 61 73 20 6d 61 6c 6c 6f 63 65 64 2e  at was malloced.
1dd30 20 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65    Because of the
1dd40 20 77 61 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c   way the.** [sql
1dd50 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68  ite3_malloc()] h
1dd60 61 70 70 65 6e 73 20 77 69 74 68 69 6e 20 73 71  appens within sq
1dd70 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28  lite3_get_table(
1dd80 29 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a  ), the calling.*
1dd90 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20  * function must 
1dda0 6e 6f 74 20 74 72 79 20 74 6f 20 63 61 6c 6c 20  not try to call 
1ddb0 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
1ddc0 20 64 69 72 65 63 74 6c 79 2e 20 20 4f 6e 6c 79   directly.  Only
1ddd0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65  .** [sqlite3_fre
1dde0 65 5f 74 61 62 6c 65 28 29 5d 20 69 73 20 61 62  e_table()] is ab
1ddf0 6c 65 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68  le to release th
1de00 65 20 6d 65 6d 6f 72 79 20 70 72 6f 70 65 72 6c  e memory properl
1de10 79 20 61 6e 64 20 73 61 66 65 6c 79 2e 0a 2a 2a  y and safely..**
1de20 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
1de30 67 65 74 5f 74 61 62 6c 65 28 29 20 69 6e 74 65  get_table() inte
1de40 72 66 61 63 65 20 69 73 20 69 6d 70 6c 65 6d 65  rface is impleme
1de50 6e 74 65 64 20 61 73 20 61 20 77 72 61 70 70 65  nted as a wrappe
1de60 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 5b 73 71 6c  r around.** [sql
1de70 69 74 65 33 5f 65 78 65 63 28 29 5d 2e 20 20 54  ite3_exec()].  T
1de80 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  he sqlite3_get_t
1de90 61 62 6c 65 28 29 20 72 6f 75 74 69 6e 65 20 64  able() routine d
1dea0 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 63 63  oes not have acc
1deb0 65 73 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 69 6e  ess.** to any in
1dec0 74 65 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75  ternal data stru
1ded0 63 74 75 72 65 73 20 6f 66 20 53 51 4c 69 74 65  ctures of SQLite
1dee0 2e 20 20 49 74 20 75 73 65 73 20 6f 6e 6c 79 20  .  It uses only 
1def0 74 68 65 20 70 75 62 6c 69 63 0a 2a 2a 20 69 6e  the public.** in
1df00 74 65 72 66 61 63 65 20 64 65 66 69 6e 65 64 20  terface defined 
1df10 68 65 72 65 2e 20 20 41 73 20 61 20 63 6f 6e 73  here.  As a cons
1df20 65 71 75 65 6e 63 65 2c 20 65 72 72 6f 72 73 20  equence, errors 
1df30 74 68 61 74 20 6f 63 63 75 72 20 69 6e 20 74 68  that occur in th
1df40 65 0a 2a 2a 20 77 72 61 70 70 65 72 20 6c 61 79  e.** wrapper lay
1df50 65 72 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68  er outside of th
1df60 65 20 69 6e 74 65 72 6e 61 6c 20 5b 73 71 6c 69  e internal [sqli
1df70 74 65 33 5f 65 78 65 63 28 29 5d 20 63 61 6c 6c  te3_exec()] call
1df80 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 65 66 6c   are not.** refl
1df90 65 63 74 65 64 20 69 6e 20 73 75 62 73 65 71 75  ected in subsequ
1dfa0 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71  ent calls to [sq
1dfb0 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d  lite3_errcode()]
1dfc0 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   or.** [sqlite3_
1dfd0 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2f 0a 53 51  errmsg()]..*/.SQ
1dfe0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1dff0 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a  ite3_get_table(.
1e000 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
1e010 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70          /* An op
1e020 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  en database */. 
1e030 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
1e040 6c 2c 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f  l,     /* SQL to
1e050 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f   be evaluated */
1e060 0a 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a 52 65  .  char ***pazRe
1e070 73 75 6c 74 2c 20 20 20 20 2f 2a 20 52 65 73 75  sult,    /* Resu
1e080 6c 74 73 20 6f 66 20 74 68 65 20 71 75 65 72 79  lts of the query
1e090 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f 77   */.  int *pnRow
1e0a0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
1e0b0 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20  umber of result 
1e0c0 72 6f 77 73 20 77 72 69 74 74 65 6e 20 68 65 72  rows written her
1e0d0 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f  e */.  int *pnCo
1e0e0 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20  lumn,        /* 
1e0f0 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74  Number of result
1e100 20 63 6f 6c 75 6d 6e 73 20 77 72 69 74 74 65 6e   columns written
1e110 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20   here */.  char 
1e120 2a 2a 70 7a 45 72 72 6d 73 67 20 20 20 20 20 20  **pzErrmsg      
1e130 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72   /* Error msg wr
1e140 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b  itten here */.);
1e150 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1e160 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
1e170 62 6c 65 28 63 68 61 72 20 2a 2a 72 65 73 75 6c  ble(char **resul
1e180 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
1e190 52 45 46 3a 20 46 6f 72 6d 61 74 74 65 64 20 53  REF: Formatted S
1e1a0 74 72 69 6e 67 20 50 72 69 6e 74 69 6e 67 20 46  tring Printing F
1e1b0 75 6e 63 74 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 54  unctions.**.** T
1e1c0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
1e1d0 65 20 77 6f 72 6b 2d 61 6c 69 6b 65 73 20 6f 66  e work-alikes of
1e1e0 20 74 68 65 20 22 70 72 69 6e 74 66 28 29 22 20   the "printf()" 
1e1f0 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e 63 74 69  family of functi
1e200 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ons.** from the 
1e210 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61  standard C libra
1e220 72 79 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73  ry..**.** ^The s
1e230 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29  qlite3_mprintf()
1e240 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 6d 70   and sqlite3_vmp
1e250 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 73  rintf() routines
1e260 20 77 72 69 74 65 20 74 68 65 69 72 0a 2a 2a 20   write their.** 
1e270 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d 65 6d  results into mem
1e280 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
1e290 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  m [sqlite3_mallo
1e2a0 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72  c()]..** The str
1e2b0 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79  ings returned by
1e2c0 20 74 68 65 73 65 20 74 77 6f 20 72 6f 75 74 69   these two routi
1e2d0 6e 65 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a  nes should be.**
1e2e0 20 72 65 6c 65 61 73 65 64 20 62 79 20 5b 73 71   released by [sq
1e2f0 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 20 20  lite3_free()].  
1e300 5e 42 6f 74 68 20 72 6f 75 74 69 6e 65 73 20 72  ^Both routines r
1e310 65 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20  eturn a.** NULL 
1e320 70 6f 69 6e 74 65 72 20 69 66 20 5b 73 71 6c 69  pointer if [sqli
1e330 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69 73  te3_malloc()] is
1e340 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63   unable to alloc
1e350 61 74 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d 65  ate enough.** me
1e360 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65  mory to hold the
1e370 20 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e   resulting strin
1e380 67 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 73  g..**.** ^(The s
1e390 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1e3a0 29 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d  ) routine is sim
1e3b0 69 6c 61 72 20 74 6f 20 22 73 6e 70 72 69 6e 74  ilar to "snprint
1e3c0 66 28 29 22 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  f()" from.** the
1e3d0 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72   standard C libr
1e3e0 61 72 79 2e 20 20 54 68 65 20 72 65 73 75 6c 74  ary.  The result
1e3f0 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   is written into
1e400 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 73   the.** buffer s
1e410 75 70 70 6c 69 65 64 20 61 73 20 74 68 65 20 73  upplied as the s
1e420 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
1e430 77 68 6f 73 65 20 73 69 7a 65 20 69 73 20 67 69  whose size is gi
1e440 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 69  ven by.** the fi
1e450 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20 4e  rst parameter. N
1e460 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f 72 64  ote that the ord
1e470 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72  er of the.** fir
1e480 73 74 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72  st two parameter
1e490 73 20 69 73 20 72 65 76 65 72 73 65 64 20 66 72  s is reversed fr
1e4a0 6f 6d 20 73 6e 70 72 69 6e 74 66 28 29 2e 29 5e  om snprintf().)^
1e4b0 20 20 54 68 69 73 20 69 73 20 61 6e 0a 2a 2a 20    This is an.** 
1e4c0 68 69 73 74 6f 72 69 63 61 6c 20 61 63 63 69 64  historical accid
1e4d0 65 6e 74 20 74 68 61 74 20 63 61 6e 6e 6f 74 20  ent that cannot 
1e4e0 62 65 20 66 69 78 65 64 20 77 69 74 68 6f 75 74  be fixed without
1e4f0 20 62 72 65 61 6b 69 6e 67 0a 2a 2a 20 62 61 63   breaking.** bac
1e500 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69  kwards compatibi
1e510 6c 69 74 79 2e 20 20 5e 28 4e 6f 74 65 20 61 6c  lity.  ^(Note al
1e520 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f  so that sqlite3_
1e530 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 72 65  snprintf().** re
1e540 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
1e550 74 6f 20 69 74 73 20 62 75 66 66 65 72 20 69 6e  to its buffer in
1e560 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e 75 6d  stead of the num
1e570 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 61 63  ber of.** charac
1e580 74 65 72 73 20 61 63 74 75 61 6c 6c 79 20 77 72  ters actually wr
1e590 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 62  itten into the b
1e5a0 75 66 66 65 72 2e 29 5e 20 20 57 65 20 61 64 6d  uffer.)^  We adm
1e5b0 69 74 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e  it that.** the n
1e5c0 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
1e5d0 65 72 73 20 77 72 69 74 74 65 6e 20 77 6f 75 6c  ers written woul
1e5e0 64 20 62 65 20 61 20 6d 6f 72 65 20 75 73 65 66  d be a more usef
1e5f0 75 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c  ul return.** val
1e600 75 65 20 62 75 74 20 77 65 20 63 61 6e 6e 6f 74  ue but we cannot
1e610 20 63 68 61 6e 67 65 20 74 68 65 20 69 6d 70 6c   change the impl
1e620 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71  ementation of sq
1e630 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
1e640 0a 2a 2a 20 6e 6f 77 20 77 69 74 68 6f 75 74 20  .** now without 
1e650 62 72 65 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69  breaking compati
1e660 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 5e 41  bility..**.** ^A
1e670 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 62 75  s long as the bu
1e680 66 66 65 72 20 73 69 7a 65 20 69 73 20 67 72 65  ffer size is gre
1e690 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20  ater than zero, 
1e6a0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1e6b0 28 29 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 73  ().** guarantees
1e6c0 20 74 68 61 74 20 74 68 65 20 62 75 66 66 65 72   that the buffer
1e6d0 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d   is always zero-
1e6e0 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 5e 54 68  terminated.  ^Th
1e6f0 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d  e first.** param
1e700 65 74 65 72 20 22 6e 22 20 69 73 20 74 68 65 20  eter "n" is the 
1e710 74 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 74 68  total size of th
1e720 65 20 62 75 66 66 65 72 2c 20 69 6e 63 6c 75 64  e buffer, includ
1e730 69 6e 67 20 73 70 61 63 65 20 66 6f 72 0a 2a 2a  ing space for.**
1e740 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e   the zero termin
1e750 61 74 6f 72 2e 20 20 53 6f 20 74 68 65 20 6c 6f  ator.  So the lo
1e760 6e 67 65 73 74 20 73 74 72 69 6e 67 20 74 68 61  ngest string tha
1e770 74 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74  t can be complet
1e780 65 6c 79 0a 2a 2a 20 77 72 69 74 74 65 6e 20 77  ely.** written w
1e790 69 6c 6c 20 62 65 20 6e 2d 31 20 63 68 61 72 61  ill be n-1 chara
1e7a0 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68  cters..**.** ^Th
1e7b0 65 20 73 71 6c 69 74 65 33 5f 76 73 6e 70 72 69  e sqlite3_vsnpri
1e7c0 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 20 69 73  ntf() routine is
1e7d0 20 61 20 76 61 72 61 72 67 73 20 76 65 72 73 69   a varargs versi
1e7e0 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e  on of sqlite3_sn
1e7f0 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 20  printf()..**.** 
1e800 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
1e810 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20 73 6f 6d  ll implement som
1e820 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 6f 72  e additional for
1e830 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70 74 69 6f  matting.** optio
1e840 6e 73 20 74 68 61 74 20 61 72 65 20 75 73 65 66  ns that are usef
1e850 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72 75 63 74  ul for construct
1e860 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ing SQL statemen
1e870 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68  ts..** All of th
1e880 65 20 75 73 75 61 6c 20 70 72 69 6e 74 66 28 29  e usual printf()
1e890 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69   formatting opti
1e8a0 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49 6e 20 61  ons apply.  In a
1e8b0 64 64 69 74 69 6f 6e 2c 20 74 68 65 72 65 0a 2a  ddition, there.*
1e8c0 2a 20 69 73 20 61 72 65 20 22 25 71 22 2c 20 22  * is are "%q", "
1e8d0 25 51 22 2c 20 61 6e 64 20 22 25 7a 22 20 6f 70  %Q", and "%z" op
1e8e0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54  tions..**.** ^(T
1e8f0 68 65 20 25 71 20 6f 70 74 69 6f 6e 20 77 6f 72  he %q option wor
1e900 6b 73 20 6c 69 6b 65 20 25 73 20 69 6e 20 74 68  ks like %s in th
1e910 61 74 20 69 74 20 73 75 62 73 74 69 74 75 74 65  at it substitute
1e920 73 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  s a nul-terminat
1e930 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 66 72 6f  ed.** string fro
1e940 6d 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 6c  m the argument l
1e950 69 73 74 2e 20 20 42 75 74 20 25 71 20 61 6c 73  ist.  But %q als
1e960 6f 20 64 6f 75 62 6c 65 73 20 65 76 65 72 79 20  o doubles every 
1e970 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 2e 0a  '\'' character..
1e980 2a 2a 20 25 71 20 69 73 20 64 65 73 69 67 6e 65  ** %q is designe
1e990 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
1e9a0 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61   a string litera
1e9b0 6c 2e 29 5e 20 20 42 79 20 64 6f 75 62 6c 69 6e  l.)^  By doublin
1e9c0 67 20 65 61 63 68 20 27 5c 27 27 0a 2a 2a 20 63  g each '\''.** c
1e9d0 68 61 72 61 63 74 65 72 20 69 74 20 65 73 63 61  haracter it esca
1e9e0 70 65 73 20 74 68 61 74 20 63 68 61 72 61 63 74  pes that charact
1e9f0 65 72 20 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74  er and allows it
1ea00 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20   to be inserted 
1ea10 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69  into.** the stri
1ea20 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78  ng..**.** For ex
1ea30 61 6d 70 6c 65 2c 20 61 73 73 75 6d 65 20 74 68  ample, assume th
1ea40 65 20 73 74 72 69 6e 67 20 76 61 72 69 61 62 6c  e string variabl
1ea50 65 20 7a 54 65 78 74 20 63 6f 6e 74 61 69 6e 73  e zText contains
1ea60 20 74 65 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73   text as follows
1ea70 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75  :.**.** <blockqu
1ea80 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68  ote><pre>.**  ch
1ea90 61 72 20 2a 7a 54 65 78 74 20 3d 20 22 49 74 27  ar *zText = "It'
1eaa0 73 20 61 20 68 61 70 70 79 20 64 61 79 21 22 3b  s a happy day!";
1eab0 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63  .** </pre></bloc
1eac0 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e  kquote>.**.** On
1ead0 65 20 63 61 6e 20 75 73 65 20 74 68 69 73 20 74  e can use this t
1eae0 65 78 74 20 69 6e 20 61 6e 20 53 51 4c 20 73 74  ext in an SQL st
1eaf0 61 74 65 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f  atement as follo
1eb00 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b  ws:.**.** <block
1eb10 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20  quote><pre>.**  
1eb20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c  char *zSQL = sql
1eb30 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e  ite3_mprintf("IN
1eb40 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20  SERT INTO table 
1eb50 56 41 4c 55 45 53 28 27 25 71 27 29 22 2c 20 7a  VALUES('%q')", z
1eb60 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74  Text);.**  sqlit
1eb70 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c  e3_exec(db, zSQL
1eb80 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20  , 0, 0, 0);.**  
1eb90 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51  sqlite3_free(zSQ
1eba0 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  L);.** </pre></b
1ebb0 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
1ebc0 20 42 65 63 61 75 73 65 20 74 68 65 20 25 71 20   Because the %q 
1ebd0 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 69 73  format string is
1ebe0 20 75 73 65 64 2c 20 74 68 65 20 27 5c 27 27 20   used, the '\'' 
1ebf0 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a 54 65  character in zTe
1ec00 78 74 0a 2a 2a 20 69 73 20 65 73 63 61 70 65 64  xt.** is escaped
1ec10 20 61 6e 64 20 74 68 65 20 53 51 4c 20 67 65 6e   and the SQL gen
1ec20 65 72 61 74 65 64 20 69 73 20 61 73 20 66 6f 6c  erated is as fol
1ec30 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  lows:.**.** <blo
1ec40 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
1ec50 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61    INSERT INTO ta
1ec60 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49 74 27  ble1 VALUES('It'
1ec70 27 73 20 61 20 68 61 70 70 79 20 64 61 79 21 27  's a happy day!'
1ec80 29 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f  ).** </pre></blo
1ec90 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54  ckquote>.**.** T
1eca0 68 69 73 20 69 73 20 63 6f 72 72 65 63 74 2e 20  his is correct. 
1ecb0 20 48 61 64 20 77 65 20 75 73 65 64 20 25 73 20   Had we used %s 
1ecc0 69 6e 73 74 65 61 64 20 6f 66 20 25 71 2c 20 74  instead of %q, t
1ecd0 68 65 20 67 65 6e 65 72 61 74 65 64 20 53 51 4c  he generated SQL
1ece0 0a 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 6c  .** would have l
1ecf0 6f 6f 6b 65 64 20 6c 69 6b 65 20 74 68 69 73 3a  ooked like this:
1ed00 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f  .**.** <blockquo
1ed10 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53  te><pre>.**  INS
1ed20 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20  ERT INTO table1 
1ed30 56 41 4c 55 45 53 28 27 49 74 27 73 20 61 20 68  VALUES('It's a h
1ed40 61 70 70 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20  appy day!');.** 
1ed50 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f  </pre></blockquo
1ed60 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73  te>.**.** This s
1ed70 65 63 6f 6e 64 20 65 78 61 6d 70 6c 65 20 69 73  econd example is
1ed80 20 61 6e 20 53 51 4c 20 73 79 6e 74 61 78 20 65   an SQL syntax e
1ed90 72 72 6f 72 2e 20 20 41 73 20 61 20 67 65 6e 65  rror.  As a gene
1eda0 72 61 6c 20 72 75 6c 65 20 79 6f 75 20 73 68 6f  ral rule you sho
1edb0 75 6c 64 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73  uld.** always us
1edc0 65 20 25 71 20 69 6e 73 74 65 61 64 20 6f 66 20  e %q instead of 
1edd0 25 73 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e  %s when insertin
1ede0 67 20 74 65 78 74 20 69 6e 74 6f 20 61 20 73 74  g text into a st
1edf0 72 69 6e 67 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a  ring literal..**
1ee00 0a 2a 2a 20 5e 28 54 68 65 20 25 51 20 6f 70 74  .** ^(The %Q opt
1ee10 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25  ion works like %
1ee20 71 20 65 78 63 65 70 74 20 69 74 20 61 6c 73 6f  q except it also
1ee30 20 61 64 64 73 20 73 69 6e 67 6c 65 20 71 75 6f   adds single quo
1ee40 74 65 73 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68  tes around.** th
1ee50 65 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65  e outside of the
1ee60 20 74 6f 74 61 6c 20 73 74 72 69 6e 67 2e 20 20   total string.  
1ee70 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 66  Additionally, if
1ee80 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69   the parameter i
1ee90 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e  n the.** argumen
1eea0 74 20 6c 69 73 74 20 69 73 20 61 20 4e 55 4c 4c  t list is a NULL
1eeb0 20 70 6f 69 6e 74 65 72 2c 20 25 51 20 73 75 62   pointer, %Q sub
1eec0 73 74 69 74 75 74 65 73 20 74 68 65 20 74 65 78  stitutes the tex
1eed0 74 20 22 4e 55 4c 4c 22 20 28 77 69 74 68 6f 75  t "NULL" (withou
1eee0 74 0a 2a 2a 20 73 69 6e 67 6c 65 20 71 75 6f 74  t.** single quot
1eef0 65 73 29 2e 29 5e 20 20 53 6f 2c 20 66 6f 72 20  es).)^  So, for 
1ef00 65 78 61 6d 70 6c 65 2c 20 6f 6e 65 20 63 6f 75  example, one cou
1ef10 6c 64 20 73 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62  ld say:.**.** <b
1ef20 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a  lockquote><pre>.
1ef30 2a 2a 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d  **  char *zSQL =
1ef40 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
1ef50 28 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61  ("INSERT INTO ta
1ef60 62 6c 65 20 56 41 4c 55 45 53 28 25 51 29 22 2c  ble VALUES(%Q)",
1ef70 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c   zText);.**  sql
1ef80 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53  ite3_exec(db, zS
1ef90 51 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a  QL, 0, 0, 0);.**
1efa0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
1efb0 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  SQL);.** </pre><
1efc0 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a  /blockquote>.**.
1efd0 2a 2a 20 54 68 65 20 63 6f 64 65 20 61 62 6f 76  ** The code abov
1efe0 65 20 77 69 6c 6c 20 72 65 6e 64 65 72 20 61 20  e will render a 
1eff0 63 6f 72 72 65 63 74 20 53 51 4c 20 73 74 61 74  correct SQL stat
1f000 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 7a 53 51  ement in the zSQ
1f010 4c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 65 76  L.** variable ev
1f020 65 6e 20 69 66 20 74 68 65 20 7a 54 65 78 74 20  en if the zText 
1f030 76 61 72 69 61 62 6c 65 20 69 73 20 61 20 4e 55  variable is a NU
1f040 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  LL pointer..**.*
1f050 2a 20 5e 28 54 68 65 20 22 25 7a 22 20 66 6f 72  * ^(The "%z" for
1f060 6d 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e 20 77  matting option w
1f070 6f 72 6b 73 20 6c 69 6b 65 20 22 25 73 22 20 62  orks like "%s" b
1f080 75 74 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 61  ut with the.** a
1f090 64 64 69 74 69 6f 6e 20 74 68 61 74 20 61 66 74  ddition that aft
1f0a0 65 72 20 74 68 65 20 73 74 72 69 6e 67 20 68 61  er the string ha
1f0b0 73 20 62 65 65 6e 20 72 65 61 64 20 61 6e 64 20  s been read and 
1f0c0 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 74  copied into.** t
1f0d0 68 65 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69  he result, [sqli
1f0e0 74 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 63  te3_free()] is c
1f0f0 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e 70  alled on the inp
1f100 75 74 20 73 74 72 69 6e 67 2e 29 5e 0a 2a 2f 0a  ut string.)^.*/.
1f110 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
1f120 2a 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66  *sqlite3_mprintf
1f130 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e  (const char*,...
1f140 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  );.SQLITE_API ch
1f150 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72  ar *sqlite3_vmpr
1f160 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a  intf(const char*
1f170 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49  , va_list);.SQLI
1f180 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c  TE_API char *sql
1f190 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e  ite3_snprintf(in
1f1a0 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t,char*,const ch
1f1b0 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54  ar*, ...);.SQLIT
1f1c0 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69  E_API char *sqli
1f1d0 74 65 33 5f 76 73 6e 70 72 69 6e 74 66 28 69 6e  te3_vsnprintf(in
1f1e0 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t,char*,const ch
1f1f0 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 0a  ar*, va_list);..
1f200 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
1f210 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f  Memory Allocatio
1f220 6e 20 53 75 62 73 79 73 74 65 6d 0a 2a 2a 0a 2a  n Subsystem.**.*
1f230 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72  * The SQLite cor
1f240 65 20 75 73 65 73 20 74 68 65 73 65 20 74 68 72  e uses these thr
1f250 65 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20  ee routines for 
1f260 61 6c 6c 20 6f 66 20 69 74 73 20 6f 77 6e 0a 2a  all of its own.*
1f270 2a 20 69 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72  * internal memor
1f280 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65  y allocation nee
1f290 64 73 2e 20 22 43 6f 72 65 22 20 69 6e 20 74 68  ds. "Core" in th
1f2a0 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e 74 65  e previous sente
1f2b0 6e 63 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20  nce.** does not 
1f2c0 69 6e 63 6c 75 64 65 20 6f 70 65 72 61 74 69 6e  include operatin
1f2d0 67 2d 73 79 73 74 65 6d 20 73 70 65 63 69 66 69  g-system specifi
1f2e0 63 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61  c VFS implementa
1f2f0 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 57 69  tion.  The.** Wi
1f300 6e 64 6f 77 73 20 56 46 53 20 75 73 65 73 20 6e  ndows VFS uses n
1f310 61 74 69 76 65 20 6d 61 6c 6c 6f 63 28 29 20 61  ative malloc() a
1f320 6e 64 20 66 72 65 65 28 29 20 66 6f 72 20 73 6f  nd free() for so
1f330 6d 65 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a  me operations..*
1f340 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65  *.** ^The sqlite
1f350 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69  3_malloc() routi
1f360 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ne returns a poi
1f370 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f 63 6b 0a  nter to a block.
1f380 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 61 74 20  ** of memory at 
1f390 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e  least N bytes in
1f3a0 20 6c 65 6e 67 74 68 2c 20 77 68 65 72 65 20 4e   length, where N
1f3b0 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74 65   is the paramete
1f3c0 72 2e 0a 2a 2a 20 5e 49 66 20 73 71 6c 69 74 65  r..** ^If sqlite
1f3d0 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20 75 6e  3_malloc() is un
1f3e0 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e 20 73  able to obtain s
1f3f0 75 66 66 69 63 69 65 6e 74 20 66 72 65 65 0a 2a  ufficient free.*
1f400 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20 72 65 74  * memory, it ret
1f410 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  urns a NULL poin
1f420 74 65 72 2e 20 20 5e 49 66 20 74 68 65 20 70 61  ter.  ^If the pa
1f430 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a 2a 20  rameter N to.** 
1f440 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
1f450 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61   is zero or nega
1f460 74 69 76 65 20 74 68 65 6e 20 73 71 6c 69 74 65  tive then sqlite
1f470 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74 75 72  3_malloc() retur
1f480 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69  ns.** a NULL poi
1f490 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 5e 43 61 6c  nter..**.** ^Cal
1f4a0 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65  ling sqlite3_fre
1f4b0 65 28 29 20 77 69 74 68 20 61 20 70 6f 69 6e 74  e() with a point
1f4c0 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  er previously re
1f4d0 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c  turned.** by sql
1f4e0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72  ite3_malloc() or
1f4f0 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
1f500 28 29 20 72 65 6c 65 61 73 65 73 20 74 68 61 74  () releases that
1f510 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74 68   memory so.** th
1f520 61 74 20 69 74 20 6d 69 67 68 74 20 62 65 20 72  at it might be r
1f530 65 75 73 65 64 2e 20 20 5e 54 68 65 20 73 71 6c  eused.  ^The sql
1f540 69 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75 74  ite3_free() rout
1f550 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f  ine is.** a no-o
1f560 70 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20 77  p if is called w
1f570 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ith a NULL point
1f580 65 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20 4e  er.  Passing a N
1f590 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74  ULL pointer.** t
1f5a0 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  o sqlite3_free()
1f5b0 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 41   is harmless.  A
1f5c0 66 74 65 72 20 62 65 69 6e 67 20 66 72 65 65 64  fter being freed
1f5d0 2c 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75  , memory.** shou
1f5e0 6c 64 20 6e 65 69 74 68 65 72 20 62 65 20 72 65  ld neither be re
1f5f0 61 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20  ad nor written. 
1f600 20 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70 72   Even reading pr
1f610 65 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a  eviously freed.*
1f620 2a 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 72  * memory might r
1f630 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d 65  esult in a segme
1f640 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72  ntation fault or
1f650 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 72   other severe er
1f660 72 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63  ror..** Memory c
1f670 6f 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 67  orruption, a seg
1f680 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c  mentation fault,
1f690 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72 65   or other severe
1f6a0 20 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20   error.** might 
1f6b0 72 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74 65  result if sqlite
1f6c0 33 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c 6c  3_free() is call
1f6d0 65 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55  ed with a non-NU
1f6e0 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74 0a  LL pointer that.
1f6f0 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 69  ** was not obtai
1f700 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
1f710 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c  _malloc() or sql
1f720 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a  ite3_realloc()..
1f730 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 73 71 6c 69  **.** ^(The sqli
1f740 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e  te3_realloc() in
1f750 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73  terface attempts
1f760 20 74 6f 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20   to resize a.** 
1f770 70 72 69 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c  prior memory all
1f780 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 61 74  ocation to be at
1f790 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 2c 20   least N bytes, 
1f7a0 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a  where N is the.*
1f7b0 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  * second paramet
1f7c0 65 72 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20  er.  The memory 
1f7d0 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65  allocation to be
1f7e0 20 72 65 73 69 7a 65 64 20 69 73 20 74 68 65 20   resized is the 
1f7f0 66 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74  first.** paramet
1f800 65 72 2e 29 5e 20 5e 20 49 66 20 74 68 65 20 66  er.)^ ^ If the f
1f810 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74  irst parameter t
1f820 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  o sqlite3_reallo
1f830 63 28 29 0a 2a 2a 20 69 73 20 61 20 4e 55 4c 4c  c().** is a NULL
1f840 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 69 74   pointer then it
1f850 73 20 62 65 68 61 76 69 6f 72 20 69 73 20 69 64  s behavior is id
1f860 65 6e 74 69 63 61 6c 20 74 6f 20 63 61 6c 6c 69  entical to calli
1f870 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61  ng.** sqlite3_ma
1f880 6c 6c 6f 63 28 4e 29 20 77 68 65 72 65 20 4e 20  lloc(N) where N 
1f890 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  is the second pa
1f8a0 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
1f8b0 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a  e3_realloc()..**
1f8c0 20 5e 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20   ^If the second 
1f8d0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
1f8e0 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69  ite3_realloc() i
1f8f0 73 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67  s zero or.** neg
1f900 61 74 69 76 65 20 74 68 65 6e 20 74 68 65 20 62  ative then the b
1f910 65 68 61 76 69 6f 72 20 69 73 20 65 78 61 63 74  ehavior is exact
1f920 6c 79 20 74 68 65 20 73 61 6d 65 20 61 73 20 63  ly the same as c
1f930 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65  alling.** sqlite
1f940 33 5f 66 72 65 65 28 50 29 20 77 68 65 72 65 20  3_free(P) where 
1f950 50 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70  P is the first p
1f960 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
1f970 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a  te3_realloc()..*
1f980 2a 20 5e 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  * ^sqlite3_reall
1f990 6f 63 28 29 20 72 65 74 75 72 6e 73 20 61 20 70  oc() returns a p
1f9a0 6f 69 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d 6f  ointer to a memo
1f9b0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  ry allocation.**
1f9c0 20 6f 66 20 61 74 20 6c 65 61 73 74 20 4e 20 62   of at least N b
1f9d0 79 74 65 73 20 69 6e 20 73 69 7a 65 20 6f 72 20  ytes in size or 
1f9e0 4e 55 4c 4c 20 69 66 20 73 75 66 66 69 63 69 65  NULL if sufficie
1f9f0 6e 74 20 6d 65 6d 6f 72 79 20 69 73 20 75 6e 61  nt memory is una
1fa00 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 5e 49 66  vailable..** ^If
1fa10 20 4d 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f   M is the size o
1fa20 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f  f the prior allo
1fa30 63 61 74 69 6f 6e 2c 20 74 68 65 6e 20 6d 69 6e  cation, then min
1fa40 28 4e 2c 4d 29 20 62 79 74 65 73 0a 2a 2a 20 6f  (N,M) bytes.** o
1fa50 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f  f the prior allo
1fa60 63 61 74 69 6f 6e 20 61 72 65 20 63 6f 70 69 65  cation are copie
1fa70 64 20 69 6e 74 6f 20 74 68 65 20 62 65 67 69 6e  d into the begin
1fa80 6e 69 6e 67 20 6f 66 20 62 75 66 66 65 72 20 72  ning of buffer r
1fa90 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71  eturned.** by sq
1faa0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20  lite3_realloc() 
1fab0 61 6e 64 20 74 68 65 20 70 72 69 6f 72 20 61 6c  and the prior al
1fac0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65  location is free
1fad0 64 2e 0a 2a 2a 20 5e 49 66 20 73 71 6c 69 74 65  d..** ^If sqlite
1fae0 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74 75  3_realloc() retu
1faf0 72 6e 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74  rns NULL, then t
1fb00 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74  he prior allocat
1fb10 69 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 66 72  ion.** is not fr
1fb20 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  eed..**.** ^The 
1fb30 6d 65 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 20  memory returned 
1fb40 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  by sqlite3_mallo
1fb50 63 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  c() and sqlite3_
1fb60 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 73 20  realloc().** is 
1fb70 61 6c 77 61 79 73 20 61 6c 69 67 6e 65 64 20 74  always aligned t
1fb80 6f 20 61 74 20 6c 65 61 73 74 20 61 6e 20 38 20  o at least an 8 
1fb90 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2c 20 6f  byte boundary, o
1fba0 72 20 74 6f 20 61 0a 2a 2a 20 34 20 62 79 74 65  r to a.** 4 byte
1fbb0 20 62 6f 75 6e 64 61 72 79 20 69 66 20 74 68 65   boundary if the
1fbc0 20 5b 53 51 4c 49 54 45 5f 34 5f 42 59 54 45 5f   [SQLITE_4_BYTE_
1fbd0 41 4c 49 47 4e 45 44 5f 4d 41 4c 4c 4f 43 5d 20  ALIGNED_MALLOC] 
1fbe0 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20  compile-time.** 
1fbf0 6f 70 74 69 6f 6e 20 69 73 20 75 73 65 64 2e 0a  option is used..
1fc00 2a 2a 0a 2a 2a 20 49 6e 20 53 51 4c 69 74 65 20  **.** In SQLite 
1fc10 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 20 61 6e  version 3.5.0 an
1fc20 64 20 33 2e 35 2e 31 2c 20 69 74 20 77 61 73 20  d 3.5.1, it was 
1fc30 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 66 69  possible to defi
1fc40 6e 65 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45  ne.** the SQLITE
1fc50 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c  _OMIT_MEMORY_ALL
1fc60 4f 43 41 54 49 4f 4e 20 77 68 69 63 68 20 77 6f  OCATION which wo
1fc70 75 6c 64 20 63 61 75 73 65 20 74 68 65 20 62 75  uld cause the bu
1fc80 69 6c 74 2d 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d  ilt-in.** implem
1fc90 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 73  entation of thes
1fca0 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65  e routines to be
1fcb0 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 61 74 20   omitted.  That 
1fcc0 63 61 70 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73  capability.** is
1fcd0 20 6e 6f 20 6c 6f 6e 67 65 72 20 70 72 6f 76 69   no longer provi
1fce0 64 65 64 2e 20 20 4f 6e 6c 79 20 62 75 69 6c 74  ded.  Only built
1fcf0 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  -in memory alloc
1fd00 61 74 6f 72 73 20 63 61 6e 20 62 65 20 75 73 65  ators can be use
1fd10 64 2e 0a 2a 2a 0a 2a 2a 20 50 72 69 6f 72 20 74  d..**.** Prior t
1fd20 6f 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e  o SQLite version
1fd30 20 33 2e 37 2e 31 30 2c 20 74 68 65 20 57 69 6e   3.7.10, the Win
1fd40 64 6f 77 73 20 4f 53 20 69 6e 74 65 72 66 61 63  dows OS interfac
1fd50 65 20 6c 61 79 65 72 20 63 61 6c 6c 65 64 0a 2a  e layer called.*
1fd60 2a 20 74 68 65 20 73 79 73 74 65 6d 20 6d 61 6c  * the system mal
1fd70 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28 29  loc() and free()
1fd80 20 64 69 72 65 63 74 6c 79 20 77 68 65 6e 20 63   directly when c
1fd90 6f 6e 76 65 72 74 69 6e 67 0a 2a 2a 20 66 69 6c  onverting.** fil
1fda0 65 6e 61 6d 65 73 20 62 65 74 77 65 65 6e 20 74  enames between t
1fdb0 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e  he UTF-8 encodin
1fdc0 67 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65  g used by SQLite
1fdd0 0a 2a 2a 20 61 6e 64 20 77 68 61 74 65 76 65 72  .** and whatever
1fde0 20 66 69 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 69   filename encodi
1fdf0 6e 67 20 69 73 20 75 73 65 64 20 62 79 20 74 68  ng is used by th
1fe00 65 20 70 61 72 74 69 63 75 6c 61 72 20 57 69 6e  e particular Win
1fe10 64 6f 77 73 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61  dows.** installa
1fe20 74 69 6f 6e 2e 20 20 4d 65 6d 6f 72 79 20 61 6c  tion.  Memory al
1fe30 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 73 20  location errors 
1fe40 77 65 72 65 20 64 65 74 65 63 74 65 64 2c 20 62  were detected, b
1fe50 75 74 0a 2a 2a 20 74 68 65 79 20 77 65 72 65 20  ut.** they were 
1fe60 72 65 70 6f 72 74 65 64 20 62 61 63 6b 20 61 73  reported back as
1fe70 20 5b 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45   [SQLITE_CANTOPE
1fe80 4e 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45  N] or.** [SQLITE
1fe90 5f 49 4f 45 52 52 5d 20 72 61 74 68 65 72 20 74  _IOERR] rather t
1fea0 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45  han [SQLITE_NOME
1feb0 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f  M]..**.** The po
1fec0 69 6e 74 65 72 20 61 72 67 75 6d 65 6e 74 73 20  inter arguments 
1fed0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  to [sqlite3_free
1fee0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
1fef0 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 6d  _realloc()].** m
1ff00 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 55  ust be either NU
1ff10 4c 4c 20 6f 72 20 65 6c 73 65 20 70 6f 69 6e 74  LL or else point
1ff20 65 72 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ers obtained fro
1ff30 6d 20 61 20 70 72 69 6f 72 0a 2a 2a 20 69 6e 76  m a prior.** inv
1ff40 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69  ocation of [sqli
1ff50 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72  te3_malloc()] or
1ff60 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   [sqlite3_reallo
1ff70 63 28 29 5d 20 74 68 61 74 20 68 61 76 65 0a 2a  c()] that have.*
1ff80 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 72  * not yet been r
1ff90 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eleased..**.** T
1ffa0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  he application m
1ffb0 75 73 74 20 6e 6f 74 20 72 65 61 64 20 6f 72 20  ust not read or 
1ffc0 77 72 69 74 65 20 61 6e 79 20 70 61 72 74 20 6f  write any part o
1ffd0 66 0a 2a 2a 20 61 20 62 6c 6f 63 6b 20 6f 66 20  f.** a block of 
1ffe0 6d 65 6d 6f 72 79 20 61 66 74 65 72 20 69 74 20  memory after it 
1fff0 68 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73 65  has been release
20000 64 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69  d using.** [sqli
20010 74 65 33 5f 66 72 65 65 28 29 5d 20 6f 72 20 5b  te3_free()] or [
20020 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
20030 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  )]..*/.SQLITE_AP
20040 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  I void *sqlite3_
20050 6d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c  malloc(int);.SQL
20060 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
20070 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f  lite3_realloc(vo
20080 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  id*, int);.SQLIT
20090 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
200a0 65 33 5f 66 72 65 65 28 76 6f 69 64 2a 29 3b 0a  e3_free(void*);.
200b0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
200c0 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 6f   Memory Allocato
200d0 72 20 53 74 61 74 69 73 74 69 63 73 0a 2a 2a 0a  r Statistics.**.
200e0 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 76 69 64  ** SQLite provid
200f0 65 73 20 74 68 65 73 65 20 74 77 6f 20 69 6e 74  es these two int
20100 65 72 66 61 63 65 73 20 66 6f 72 20 72 65 70 6f  erfaces for repo
20110 72 74 69 6e 67 20 6f 6e 20 74 68 65 20 73 74 61  rting on the sta
20120 74 75 73 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73  tus.** of the [s
20130 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
20140 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  , [sqlite3_free(
20150 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  )], and [sqlite3
20160 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 72  _realloc()].** r
20170 6f 75 74 69 6e 65 73 2c 20 77 68 69 63 68 20 66  outines, which f
20180 6f 72 6d 20 74 68 65 20 62 75 69 6c 74 2d 69 6e  orm the built-in
20190 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
201a0 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2a  on subsystem..**
201b0 0a 2a 2a 20 5e 54 68 65 20 5b 73 71 6c 69 74 65  .** ^The [sqlite
201c0 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d  3_memory_used()]
201d0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
201e0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
201f0 79 74 65 73 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72  ytes.** of memor
20200 79 20 63 75 72 72 65 6e 74 6c 79 20 6f 75 74 73  y currently outs
20210 74 61 6e 64 69 6e 67 20 28 6d 61 6c 6c 6f 63 65  tanding (malloce
20220 64 20 62 75 74 20 6e 6f 74 20 66 72 65 65 64 29  d but not freed)
20230 2e 0a 2a 2a 20 5e 54 68 65 20 5b 73 71 6c 69 74  ..** ^The [sqlit
20240 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61  e3_memory_highwa
20250 74 65 72 28 29 5d 20 72 6f 75 74 69 6e 65 20 72  ter()] routine r
20260 65 74 75 72 6e 73 20 74 68 65 20 6d 61 78 69 6d  eturns the maxim
20270 75 6d 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 5b  um.** value of [
20280 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75  sqlite3_memory_u
20290 73 65 64 28 29 5d 20 73 69 6e 63 65 20 74 68 65  sed()] since the
202a0 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b   high-water mark
202b0 0a 2a 2a 20 77 61 73 20 6c 61 73 74 20 72 65 73  .** was last res
202c0 65 74 2e 20 20 5e 54 68 65 20 76 61 6c 75 65 73  et.  ^The values
202d0 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71   returned by [sq
202e0 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65  lite3_memory_use
202f0 64 28 29 5d 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c  d()] and.** [sql
20300 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68  ite3_memory_high
20310 77 61 74 65 72 28 29 5d 20 69 6e 63 6c 75 64 65  water()] include
20320 20 61 6e 79 20 6f 76 65 72 68 65 61 64 0a 2a 2a   any overhead.**
20330 20 61 64 64 65 64 20 62 79 20 53 51 4c 69 74 65   added by SQLite
20340 20 69 6e 20 69 74 73 20 69 6d 70 6c 65 6d 65 6e   in its implemen
20350 74 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74  tation of [sqlit
20360 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 0a 2a 2a  e3_malloc()],.**
20370 20 62 75 74 20 6e 6f 74 20 6f 76 65 72 68 65 61   but not overhea
20380 64 20 61 64 64 65 64 20 62 79 20 74 68 65 20 61  d added by the a
20390 6e 79 20 75 6e 64 65 72 6c 79 69 6e 67 20 73 79  ny underlying sy
203a0 73 74 65 6d 20 6c 69 62 72 61 72 79 0a 2a 2a 20  stem library.** 
203b0 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 5b 73  routines that [s
203c0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
203d0 20 6d 61 79 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a   may call..**.**
203e0 20 5e 54 68 65 20 6d 65 6d 6f 72 79 20 68 69 67   ^The memory hig
203f0 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 73 20  h-water mark is 
20400 72 65 73 65 74 20 74 6f 20 74 68 65 20 63 75 72  reset to the cur
20410 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 0a 2a 2a  rent value of.**
20420 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79   [sqlite3_memory
20430 5f 75 73 65 64 28 29 5d 20 69 66 20 61 6e 64 20  _used()] if and 
20440 6f 6e 6c 79 20 69 66 20 74 68 65 20 70 61 72 61  only if the para
20450 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b 73 71 6c  meter to.** [sql
20460 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68  ite3_memory_high
20470 77 61 74 65 72 28 29 5d 20 69 73 20 74 72 75 65  water()] is true
20480 2e 20 20 5e 54 68 65 20 76 61 6c 75 65 20 72 65  .  ^The value re
20490 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 5b 73 71  turned.** by [sq
204a0 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67  lite3_memory_hig
204b0 68 77 61 74 65 72 28 31 29 5d 20 69 73 20 74 68  hwater(1)] is th
204c0 65 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72  e high-water mar
204d0 6b 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 74 68  k.** prior to th
204e0 65 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49  e reset..*/.SQLI
204f0 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69  TE_API sqlite3_i
20500 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d  nt64 sqlite3_mem
20510 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 3b 0a  ory_used(void);.
20520 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
20530 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e3_int64 sqlite3
20540 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65  _memory_highwate
20550 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67 29  r(int resetFlag)
20560 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
20570 46 3a 20 50 73 65 75 64 6f 2d 52 61 6e 64 6f 6d  F: Pseudo-Random
20580 20 4e 75 6d 62 65 72 20 47 65 6e 65 72 61 74 6f   Number Generato
20590 72 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63  r.**.** SQLite c
205a0 6f 6e 74 61 69 6e 73 20 61 20 68 69 67 68 2d 71  ontains a high-q
205b0 75 61 6c 69 74 79 20 70 73 65 75 64 6f 2d 72 61  uality pseudo-ra
205c0 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65  ndom number gene
205d0 72 61 74 6f 72 20 28 50 52 4e 47 29 20 75 73 65  rator (PRNG) use
205e0 64 20 74 6f 0a 2a 2a 20 73 65 6c 65 63 74 20 72  d to.** select r
205f0 61 6e 64 6f 6d 20 5b 52 4f 57 49 44 20 7c 20 52  andom [ROWID | R
20600 4f 57 49 44 73 5d 20 77 68 65 6e 20 69 6e 73 65  OWIDs] when inse
20610 72 74 69 6e 67 20 6e 65 77 20 72 65 63 6f 72 64  rting new record
20620 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 74  s into a table t
20630 68 61 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 75  hat.** already u
20640 73 65 73 20 74 68 65 20 6c 61 72 67 65 73 74 20  ses the largest 
20650 70 6f 73 73 69 62 6c 65 20 5b 52 4f 57 49 44 5d  possible [ROWID]
20660 2e 20 20 54 68 65 20 50 52 4e 47 20 69 73 20 61  .  The PRNG is a
20670 6c 73 6f 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20  lso used for.** 
20680 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 72 61 6e  the build-in ran
20690 64 6f 6d 28 29 20 61 6e 64 20 72 61 6e 64 6f 6d  dom() and random
206a0 62 6c 6f 62 28 29 20 53 51 4c 20 66 75 6e 63 74  blob() SQL funct
206b0 69 6f 6e 73 2e 20 20 54 68 69 73 20 69 6e 74 65  ions.  This inte
206c0 72 66 61 63 65 20 61 6c 6c 6f 77 73 0a 2a 2a 20  rface allows.** 
206d0 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 6f 20  applications to 
206e0 61 63 63 65 73 73 20 74 68 65 20 73 61 6d 65 20  access the same 
206f0 50 52 4e 47 20 66 6f 72 20 6f 74 68 65 72 20 70  PRNG for other p
20700 75 72 70 6f 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 5e  urposes..**.** ^
20710 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72  A call to this r
20720 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 4e 20  outine stores N 
20730 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e  bytes of randomn
20740 65 73 73 20 69 6e 74 6f 20 62 75 66 66 65 72 20  ess into buffer 
20750 50 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69  P..**.** ^The fi
20760 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f  rst time this ro
20770 75 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65 64  utine is invoked
20780 20 28 65 69 74 68 65 72 20 69 6e 74 65 72 6e 61   (either interna
20790 6c 6c 79 20 6f 72 20 62 79 0a 2a 2a 20 74 68 65  lly or by.** the
207a0 20 61 70 70 6c 69 63 61 74 69 6f 6e 29 20 74 68   application) th
207b0 65 20 50 52 4e 47 20 69 73 20 73 65 65 64 65 64  e PRNG is seeded
207c0 20 75 73 69 6e 67 20 72 61 6e 64 6f 6d 6e 65 73   using randomnes
207d0 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  s obtained.** fr
207e0 6f 6d 20 74 68 65 20 78 52 61 6e 64 6f 6d 6e 65  om the xRandomne
207f0 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  ss method of the
20800 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65   default [sqlite
20810 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a  3_vfs] object..*
20820 2a 20 5e 4f 6e 20 61 6c 6c 20 73 75 62 73 65 71  * ^On all subseq
20830 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73  uent invocations
20840 2c 20 74 68 65 20 70 73 65 75 64 6f 2d 72 61 6e  , the pseudo-ran
20850 64 6f 6d 6e 65 73 73 20 69 73 20 67 65 6e 65 72  domness is gener
20860 61 74 65 64 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c  ated.** internal
20870 6c 79 20 61 6e 64 20 77 69 74 68 6f 75 74 20 72  ly and without r
20880 65 63 6f 75 72 73 65 20 74 6f 20 74 68 65 20 5b  ecourse to the [
20890 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 78 52 61  sqlite3_vfs] xRa
208a0 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20 6d 65 74 68  ndomness.** meth
208b0 6f 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  od..*/.SQLITE_AP
208c0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
208d0 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c  andomness(int N,
208e0 20 76 6f 69 64 20 2a 50 29 3b 0a 0a 2f 2a 0a 2a   void *P);../*.*
208f0 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70  * CAPI3REF: Comp
20900 69 6c 65 2d 54 69 6d 65 20 41 75 74 68 6f 72 69  ile-Time Authori
20910 7a 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73  zation Callbacks
20920 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 72 6f 75  .**.** ^This rou
20930 74 69 6e 65 20 72 65 67 69 73 74 65 72 73 20 61  tine registers a
20940 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  n authorizer cal
20950 6c 62 61 63 6b 20 77 69 74 68 20 61 20 70 61 72  lback with a par
20960 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61  ticular.** [data
20970 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
20980 2c 20 73 75 70 70 6c 69 65 64 20 69 6e 20 74 68  , supplied in th
20990 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
209a0 2e 0a 2a 2a 20 5e 54 68 65 20 61 75 74 68 6f 72  ..** ^The author
209b0 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73  izer callback is
209c0 20 69 6e 76 6f 6b 65 64 20 61 73 20 53 51 4c 20   invoked as SQL 
209d0 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 62  statements are b
209e0 65 69 6e 67 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a  eing compiled.**
209f0 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   by [sqlite3_pre
20a00 70 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76  pare()] or its v
20a10 61 72 69 61 6e 74 73 20 5b 73 71 6c 69 74 65 33  ariants [sqlite3
20a20 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 0a  _prepare_v2()],.
20a30 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ** [sqlite3_prep
20a40 61 72 65 31 36 28 29 5d 20 61 6e 64 20 5b 73 71  are16()] and [sq
20a50 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
20a60 76 32 28 29 5d 2e 20 20 5e 41 74 20 76 61 72 69  v2()].  ^At vari
20a70 6f 75 73 0a 2a 2a 20 70 6f 69 6e 74 73 20 64 75  ous.** points du
20a80 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61  ring the compila
20a90 74 69 6f 6e 20 70 72 6f 63 65 73 73 2c 20 61 73  tion process, as
20aa0 20 6c 6f 67 69 63 20 69 73 20 62 65 69 6e 67 20   logic is being 
20ab0 63 72 65 61 74 65 64 0a 2a 2a 20 74 6f 20 70 65  created.** to pe
20ac0 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 61 63  rform various ac
20ad0 74 69 6f 6e 73 2c 20 74 68 65 20 61 75 74 68 6f  tions, the autho
20ae0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
20af0 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20  s invoked to.** 
20b00 73 65 65 20 69 66 20 74 68 6f 73 65 20 61 63 74  see if those act
20b10 69 6f 6e 73 20 61 72 65 20 61 6c 6c 6f 77 65 64  ions are allowed
20b20 2e 20 20 5e 54 68 65 20 61 75 74 68 6f 72 69 7a  .  ^The authoriz
20b30 65 72 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75  er callback shou
20b40 6c 64 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51  ld.** return [SQ
20b50 4c 49 54 45 5f 4f 4b 5d 20 74 6f 20 61 6c 6c 6f  LITE_OK] to allo
20b60 77 20 74 68 65 20 61 63 74 69 6f 6e 2c 20 5b 53  w the action, [S
20b70 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 6f  QLITE_IGNORE] to
20b80 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 0a 2a 2a   disallow the.**
20b90 20 73 70 65 63 69 66 69 63 20 61 63 74 69 6f 6e   specific action
20ba0 20 62 75 74 20 61 6c 6c 6f 77 20 74 68 65 20 53   but allow the S
20bb0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  QL statement to 
20bc0 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65 0a 2a  continue to be.*
20bd0 2a 20 63 6f 6d 70 69 6c 65 64 2c 20 6f 72 20 5b  * compiled, or [
20be0 53 51 4c 49 54 45 5f 44 45 4e 59 5d 20 74 6f 20  SQLITE_DENY] to 
20bf0 63 61 75 73 65 20 74 68 65 20 65 6e 74 69 72 65  cause the entire
20c00 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74   SQL statement t
20c10 6f 20 62 65 0a 2a 2a 20 72 65 6a 65 63 74 65 64  o be.** rejected
20c20 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 20   with an error. 
20c30 20 5e 49 66 20 74 68 65 20 61 75 74 68 6f 72 69   ^If the authori
20c40 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  zer callback ret
20c50 75 72 6e 73 0a 2a 2a 20 61 6e 79 20 76 61 6c 75  urns.** any valu
20c60 65 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51  e other than [SQ
20c70 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 2c 20 5b 53  LITE_IGNORE], [S
20c80 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f 72 20 5b 53  QLITE_OK], or [S
20c90 51 4c 49 54 45 5f 44 45 4e 59 5d 0a 2a 2a 20 74  QLITE_DENY].** t
20ca0 68 65 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33  hen the [sqlite3
20cb0 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f  _prepare_v2()] o
20cc0 72 20 65 71 75 69 76 61 6c 65 6e 74 20 63 61 6c  r equivalent cal
20cd0 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64  l that triggered
20ce0 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a  .** the authoriz
20cf0 65 72 20 77 69 6c 6c 20 66 61 69 6c 20 77 69 74  er will fail wit
20d00 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  h an error messa
20d10 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74  ge..**.** When t
20d20 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  he callback retu
20d30 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c  rns [SQLITE_OK],
20d40 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20   that means the 
20d50 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 72 65 71  operation.** req
20d60 75 65 73 74 65 64 20 69 73 20 6f 6b 2e 20 20 5e  uested is ok.  ^
20d70 57 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63  When the callbac
20d80 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  k returns [SQLIT
20d90 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a 2a 2a 20  E_DENY], the.** 
20da0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
20db0 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69 76 61  _v2()] or equiva
20dc0 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74 20 74  lent call that t
20dd0 72 69 67 67 65 72 65 64 20 74 68 65 0a 2a 2a 20  riggered the.** 
20de0 61 75 74 68 6f 72 69 7a 65 72 20 77 69 6c 6c 20  authorizer will 
20df0 66 61 69 6c 20 77 69 74 68 20 61 6e 20 65 72 72  fail with an err
20e00 6f 72 20 6d 65 73 73 61 67 65 20 65 78 70 6c 61  or message expla
20e10 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20 61 63  ining that.** ac
20e20 63 65 73 73 20 69 73 20 64 65 6e 69 65 64 2e 20  cess is denied. 
20e30 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 72 73  .**.** ^The firs
20e40 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  t parameter to t
20e50 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  he authorizer ca
20e60 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70 79  llback is a copy
20e70 20 6f 66 20 74 68 65 20 74 68 69 72 64 0a 2a 2a   of the third.**
20e80 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
20e90 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  e sqlite3_set_au
20ea0 74 68 6f 72 69 7a 65 72 28 29 20 69 6e 74 65 72  thorizer() inter
20eb0 66 61 63 65 2e 20 5e 54 68 65 20 73 65 63 6f 6e  face. ^The secon
20ec0 64 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74  d parameter.** t
20ed0 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69  o the callback i
20ee0 73 20 61 6e 20 69 6e 74 65 67 65 72 20 5b 53 51  s an integer [SQ
20ef0 4c 49 54 45 5f 43 4f 50 59 20 7c 20 61 63 74 69  LITE_COPY | acti
20f00 6f 6e 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70  on code] that sp
20f10 65 63 69 66 69 65 73 0a 2a 2a 20 74 68 65 20 70  ecifies.** the p
20f20 61 72 74 69 63 75 6c 61 72 20 61 63 74 69 6f 6e  articular action
20f30 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65   to be authorize
20f40 64 2e 20 5e 54 68 65 20 74 68 69 72 64 20 74 68  d. ^The third th
20f50 72 6f 75 67 68 20 73 69 78 74 68 20 70 61 72 61  rough sixth para
20f60 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 74 68 65  meters.** to the
20f70 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 20 7a 65   callback are ze
20f80 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74  ro-terminated st
20f90 72 69 6e 67 73 20 74 68 61 74 20 63 6f 6e 74 61  rings that conta
20fa0 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a  in additional.**
20fb0 20 64 65 74 61 69 6c 73 20 61 62 6f 75 74 20 74   details about t
20fc0 68 65 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20  he action to be 
20fd0 61 75 74 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a  authorized..**.*
20fe0 2a 20 5e 49 66 20 74 68 65 20 61 63 74 69 6f 6e  * ^If the action
20ff0 20 63 6f 64 65 20 69 73 20 5b 53 51 4c 49 54 45   code is [SQLITE
21000 5f 52 45 41 44 5d 0a 2a 2a 20 61 6e 64 20 74 68  _READ].** and th
21010 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  e callback retur
21020 6e 73 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52  ns [SQLITE_IGNOR
21030 45 5d 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 5b  E] then the.** [
21040 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
21050 6e 74 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 73  nt] statement is
21060 20 63 6f 6e 73 74 72 75 63 74 65 64 20 74 6f 20   constructed to 
21070 73 75 62 73 74 69 74 75 74 65 0a 2a 2a 20 61 20  substitute.** a 
21080 4e 55 4c 4c 20 76 61 6c 75 65 20 69 6e 20 70 6c  NULL value in pl
21090 61 63 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ace of the table
210a0 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 77 6f 75   column that wou
210b0 6c 64 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20  ld have.** been 
210c0 72 65 61 64 20 69 66 20 5b 53 51 4c 49 54 45 5f  read if [SQLITE_
210d0 4f 4b 5d 20 68 61 64 20 62 65 65 6e 20 72 65 74  OK] had been ret
210e0 75 72 6e 65 64 2e 20 20 54 68 65 20 5b 53 51 4c  urned.  The [SQL
210f0 49 54 45 5f 49 47 4e 4f 52 45 5d 0a 2a 2a 20 72  ITE_IGNORE].** r
21100 65 74 75 72 6e 20 63 61 6e 20 62 65 20 75 73 65  eturn can be use
21110 64 20 74 6f 20 64 65 6e 79 20 61 6e 20 75 6e 74  d to deny an unt
21120 72 75 73 74 65 64 20 75 73 65 72 20 61 63 63 65  rusted user acce
21130 73 73 20 74 6f 20 69 6e 64 69 76 69 64 75 61 6c  ss to individual
21140 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 61  .** columns of a
21150 20 74 61 62 6c 65 2e 0a 2a 2a 20 5e 49 66 20 74   table..** ^If t
21160 68 65 20 61 63 74 69 6f 6e 20 63 6f 64 65 20 69  he action code i
21170 73 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45 54 45  s [SQLITE_DELETE
21180 5d 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 61  ] and the callba
21190 63 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 5b 53  ck returns.** [S
211a0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 68  QLITE_IGNORE] th
211b0 65 6e 20 74 68 65 20 5b 44 45 4c 45 54 45 5d 20  en the [DELETE] 
211c0 6f 70 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65  operation procee
211d0 64 73 20 62 75 74 20 74 68 65 0a 2a 2a 20 5b 74  ds but the.** [t
211e0 72 75 6e 63 61 74 65 20 6f 70 74 69 6d 69 7a 61  runcate optimiza
211f0 74 69 6f 6e 5d 20 69 73 20 64 69 73 61 62 6c 65  tion] is disable
21200 64 20 61 6e 64 20 61 6c 6c 20 72 6f 77 73 20 61  d and all rows a
21210 72 65 20 64 65 6c 65 74 65 64 20 69 6e 64 69 76  re deleted indiv
21220 69 64 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41  idually..**.** A
21230 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 69 73 20  n authorizer is 
21240 75 73 65 64 20 77 68 65 6e 20 5b 73 71 6c 69 74  used when [sqlit
21250 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70 72 65  e3_prepare | pre
21260 70 61 72 69 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73  paring].** SQL s
21270 74 61 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61  tatements from a
21280 6e 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72  n untrusted sour
21290 63 65 2c 20 74 6f 20 65 6e 73 75 72 65 20 74 68  ce, to ensure th
212a0 61 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  at the SQL state
212b0 6d 65 6e 74 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20  ments.** do not 
212c0 74 72 79 20 74 6f 20 61 63 63 65 73 73 20 64 61  try to access da
212d0 74 61 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20  ta they are not 
212e0 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65 65 2c 20  allowed to see, 
212f0 6f 72 20 74 68 61 74 20 74 68 65 79 20 64 6f 20  or that they do 
21300 6e 6f 74 0a 2a 2a 20 74 72 79 20 74 6f 20 65 78  not.** try to ex
21310 65 63 75 74 65 20 6d 61 6c 69 63 69 6f 75 73 20  ecute malicious 
21320 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20  statements that 
21330 64 61 6d 61 67 65 20 74 68 65 20 64 61 74 61 62  damage the datab
21340 61 73 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61  ase.  For.** exa
21350 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 61  mple, an applica
21360 74 69 6f 6e 20 6d 61 79 20 61 6c 6c 6f 77 20 61  tion may allow a
21370 20 75 73 65 72 20 74 6f 20 65 6e 74 65 72 20 61   user to enter a
21380 72 62 69 74 72 61 72 79 0a 2a 2a 20 53 51 4c 20  rbitrary.** SQL 
21390 71 75 65 72 69 65 73 20 66 6f 72 20 65 76 61 6c  queries for eval
213a0 75 61 74 69 6f 6e 20 62 79 20 61 20 64 61 74 61  uation by a data
213b0 62 61 73 65 2e 20 20 42 75 74 20 74 68 65 20 61  base.  But the a
213c0 70 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 0a  pplication does.
213d0 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 20  ** not want the 
213e0 75 73 65 72 20 74 6f 20 62 65 20 61 62 6c 65 20  user to be able 
213f0 74 6f 20 6d 61 6b 65 20 61 72 62 69 74 72 61 72  to make arbitrar
21400 79 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65  y changes to the
21410 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 41  .** database.  A
21420 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 75  n authorizer cou
21430 6c 64 20 74 68 65 6e 20 62 65 20 70 75 74 20 69  ld then be put i
21440 6e 20 70 6c 61 63 65 20 77 68 69 6c 65 20 74 68  n place while th
21450 65 0a 2a 2a 20 75 73 65 72 2d 65 6e 74 65 72 65  e.** user-entere
21460 64 20 53 51 4c 20 69 73 20 62 65 69 6e 67 20 5b  d SQL is being [
21470 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20  sqlite3_prepare 
21480 7c 20 70 72 65 70 61 72 65 64 5d 20 74 68 61 74  | prepared] that
21490 0a 2a 2a 20 64 69 73 61 6c 6c 6f 77 73 20 65 76  .** disallows ev
214a0 65 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 20  erything except 
214b0 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65  [SELECT] stateme
214c0 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69  nts..**.** Appli
214d0 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6e 65 65  cations that nee
214e0 64 20 74 6f 20 70 72 6f 63 65 73 73 20 53 51 4c  d to process SQL
214f0 20 66 72 6f 6d 20 75 6e 74 72 75 73 74 65 64 20   from untrusted 
21500 73 6f 75 72 63 65 73 0a 2a 2a 20 6d 69 67 68 74  sources.** might
21510 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 20 6c   also consider l
21520 6f 77 65 72 69 6e 67 20 72 65 73 6f 75 72 63 65  owering resource
21530 20 6c 69 6d 69 74 73 20 75 73 69 6e 67 20 5b 73   limits using [s
21540 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a  qlite3_limit()].
21550 2a 2a 20 61 6e 64 20 6c 69 6d 69 74 69 6e 67 20  ** and limiting 
21560 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 75 73  database size us
21570 69 6e 67 20 74 68 65 20 5b 6d 61 78 5f 70 61 67  ing the [max_pag
21580 65 5f 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41  e_count] [PRAGMA
21590 5d 0a 2a 2a 20 69 6e 20 61 64 64 69 74 69 6f 6e  ].** in addition
215a0 20 74 6f 20 75 73 69 6e 67 20 61 6e 20 61 75 74   to using an aut
215b0 68 6f 72 69 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 5e  horizer..**.** ^
215c0 28 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 61  (Only a single a
215d0 75 74 68 6f 72 69 7a 65 72 20 63 61 6e 20 62 65  uthorizer can be
215e0 20 69 6e 20 70 6c 61 63 65 20 6f 6e 20 61 20 64   in place on a d
215f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
21600 6f 6e 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 2e  on.** at a time.
21610 20 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 73    Each call to s
21620 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
21630 72 69 7a 65 72 20 6f 76 65 72 72 69 64 65 73 20  rizer overrides 
21640 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 20  the.** previous 
21650 63 61 6c 6c 2e 29 5e 20 20 5e 44 69 73 61 62 6c  call.)^  ^Disabl
21660 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72  e the authorizer
21670 20 62 79 20 69 6e 73 74 61 6c 6c 69 6e 67 20 61   by installing a
21680 20 4e 55 4c 4c 20 63 61 6c 6c 62 61 63 6b 2e 0a   NULL callback..
21690 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65  ** The authorize
216a0 72 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 79  r is disabled by
216b0 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20   default..**.** 
216c0 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63  The authorizer c
216d0 61 6c 6c 62 61 63 6b 20 6d 75 73 74 20 6e 6f 74  allback must not
216e0 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61   do anything tha
216f0 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a  t will modify.**
21700 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
21710 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e  nnection that in
21720 76 6f 6b 65 64 20 74 68 65 20 61 75 74 68 6f 72  voked the author
21730 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a  izer callback..*
21740 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c  * Note that [sql
21750 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
21760 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
21770 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64  step()] both mod
21780 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74  ify their.** dat
21790 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
217a0 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e  s for the meanin
217b0 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e  g of "modify" in
217c0 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e   this paragraph.
217d0 0a 2a 2a 0a 2a 2a 20 5e 57 68 65 6e 20 5b 73 71  .**.** ^When [sq
217e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
217f0 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f 20 70  ()] is used to p
21800 72 65 70 61 72 65 20 61 20 73 74 61 74 65 6d 65  repare a stateme
21810 6e 74 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65  nt, the.** state
21820 6d 65 6e 74 20 6d 69 67 68 74 20 62 65 20 72 65  ment might be re
21830 2d 70 72 65 70 61 72 65 64 20 64 75 72 69 6e 67  -prepared during
21840 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
21850 5d 20 64 75 65 20 74 6f 20 61 20 0a 2a 2a 20 73  ] due to a .** s
21860 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 20 20 48  chema change.  H
21870 65 6e 63 65 2c 20 74 68 65 20 61 70 70 6c 69 63  ence, the applic
21880 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 65 6e 73  ation should ens
21890 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ure that the.** 
218a0 63 6f 72 72 65 63 74 20 61 75 74 68 6f 72 69 7a  correct authoriz
218b0 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 6d 61  er callback rema
218c0 69 6e 73 20 69 6e 20 70 6c 61 63 65 20 64 75 72  ins in place dur
218d0 69 6e 67 20 74 68 65 20 5b 73 71 6c 69 74 65 33  ing the [sqlite3
218e0 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  _step()]..**.** 
218f0 5e 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 61  ^Note that the a
21900 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
21910 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e  ck is invoked on
21920 6c 79 20 64 75 72 69 6e 67 0a 2a 2a 20 5b 73 71  ly during.** [sq
21930 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d  lite3_prepare()]
21940 20 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73   or its variants
21950 2e 20 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e  .  Authorization
21960 20 69 73 20 6e 6f 74 0a 2a 2a 20 70 65 72 66 6f   is not.** perfo
21970 72 6d 65 64 20 64 75 72 69 6e 67 20 73 74 61 74  rmed during stat
21980 65 6d 65 6e 74 20 65 76 61 6c 75 61 74 69 6f 6e  ement evaluation
21990 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   in [sqlite3_ste
219a0 70 28 29 5d 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20  p()], unless.** 
219b0 61 73 20 73 74 61 74 65 64 20 69 6e 20 74 68 65  as stated in the
219c0 20 70 72 65 76 69 6f 75 73 20 70 61 72 61 67 72   previous paragr
219d0 61 70 68 2c 20 73 71 6c 69 74 65 33 5f 73 74 65  aph, sqlite3_ste
219e0 70 28 29 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 73  p() invokes.** s
219f0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
21a00 32 28 29 20 74 6f 20 72 65 70 72 65 70 61 72 65  2() to reprepare
21a10 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74   a statement aft
21a20 65 72 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e  er a schema chan
21a30 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ge..*/.SQLITE_AP
21a40 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65  I int sqlite3_se
21a50 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 0a 20 20  t_authorizer(.  
21a60 73 71 6c 69 74 65 33 2a 2c 0a 20 20 69 6e 74 20  sqlite3*,.  int 
21a70 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69  (*xAuth)(void*,i
21a80 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  nt,const char*,c
21a90 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
21aa0 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
21ab0 72 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73  r*),.  void *pUs
21ac0 65 72 44 61 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  erData.);../*.**
21ad0 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f   CAPI3REF: Autho
21ae0 72 69 7a 65 72 20 52 65 74 75 72 6e 20 43 6f 64  rizer Return Cod
21af0 65 73 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71  es.**.** The [sq
21b00 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
21b10 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65  izer | authorize
21b20 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  r callback funct
21b30 69 6f 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74  ion] must.** ret
21b40 75 72 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49  urn either [SQLI
21b50 54 45 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66  TE_OK] or one of
21b60 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74   these two const
21b70 61 6e 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a  ants in order.**
21b80 20 74 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74   to signal SQLit
21b90 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  e whether or not
21ba0 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70   the action is p
21bb0 65 72 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74  ermitted.  See t
21bc0 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73  he.** [sqlite3_s
21bd0 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20  et_authorizer | 
21be0 61 75 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d  authorizer docum
21bf0 65 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64  entation] for ad
21c00 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f  ditional.** info
21c10 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e  rmation..**.** N
21c20 6f 74 65 20 74 68 61 74 20 53 51 4c 49 54 45 5f  ote that SQLITE_
21c30 49 47 4e 4f 52 45 20 69 73 20 61 6c 73 6f 20 75  IGNORE is also u
21c40 73 65 64 20 61 73 20 61 20 5b 53 51 4c 49 54 45  sed as a [SQLITE
21c50 5f 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 65 74 75  _ROLLBACK | retu
21c60 72 6e 20 63 6f 64 65 5d 0a 2a 2a 20 66 72 6f 6d  rn code].** from
21c70 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 74   the [sqlite3_vt
21c80 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63 74 28 29  ab_on_conflict()
21c90 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a  ] interface..*/.
21ca0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
21cb0 45 4e 59 20 20 20 31 20 20 20 2f 2a 20 41 62 6f  ENY   1   /* Abo
21cc0 72 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  rt the SQL state
21cd0 6d 65 6e 74 20 77 69 74 68 20 61 6e 20 65 72 72  ment with an err
21ce0 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  or */.#define SQ
21cf0 4c 49 54 45 5f 49 47 4e 4f 52 45 20 32 20 20 20  LITE_IGNORE 2   
21d00 2f 2a 20 44 6f 6e 27 74 20 61 6c 6c 6f 77 20 61  /* Don't allow a
21d10 63 63 65 73 73 2c 20 62 75 74 20 64 6f 6e 27 74  ccess, but don't
21d20 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72   generate an err
21d30 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  or */../*.** CAP
21d40 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a 65  I3REF: Authorize
21d50 72 20 41 63 74 69 6f 6e 20 43 6f 64 65 73 0a 2a  r Action Codes.*
21d60 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65  *.** The [sqlite
21d70 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
21d80 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65  ()] interface re
21d90 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61  gisters a callba
21da0 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74  ck function.** t
21db0 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 74  hat is invoked t
21dc0 6f 20 61 75 74 68 6f 72 69 7a 65 20 63 65 72 74  o authorize cert
21dd0 61 69 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ain SQL statemen
21de0 74 20 61 63 74 69 6f 6e 73 2e 20 20 54 68 65 0a  t actions.  The.
21df0 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  ** second parame
21e00 74 65 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  ter to the callb
21e10 61 63 6b 20 69 73 20 61 6e 20 69 6e 74 65 67 65  ack is an intege
21e20 72 20 63 6f 64 65 20 74 68 61 74 20 73 70 65 63  r code that spec
21e30 69 66 69 65 73 0a 2a 2a 20 77 68 61 74 20 61 63  ifies.** what ac
21e40 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 61 75  tion is being au
21e50 74 68 6f 72 69 7a 65 64 2e 20 20 54 68 65 73 65  thorized.  These
21e60 20 61 72 65 20 74 68 65 20 69 6e 74 65 67 65 72   are the integer
21e70 20 61 63 74 69 6f 6e 20 63 6f 64 65 73 20 74 68   action codes th
21e80 61 74 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f 72  at.** the author
21e90 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d 61  izer callback ma
21ea0 79 20 62 65 20 70 61 73 73 65 64 2e 0a 2a 2a 0a  y be passed..**.
21eb0 2a 2a 20 54 68 65 73 65 20 61 63 74 69 6f 6e 20  ** These action 
21ec0 63 6f 64 65 20 76 61 6c 75 65 73 20 73 69 67 6e  code values sign
21ed0 69 66 79 20 77 68 61 74 20 6b 69 6e 64 20 6f 66  ify what kind of
21ee0 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 74 6f   operation is to
21ef0 20 62 65 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65   be.** authorize
21f00 64 2e 20 20 54 68 65 20 33 72 64 20 61 6e 64 20  d.  The 3rd and 
21f10 34 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74  4th parameters t
21f20 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74  o the authorizat
21f30 69 6f 6e 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20  ion.** callback 
21f40 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65  function will be
21f50 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 4e   parameters or N
21f60 55 4c 4c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  ULL depending on
21f70 20 77 68 69 63 68 20 6f 66 20 74 68 65 73 65 0a   which of these.
21f80 2a 2a 20 63 6f 64 65 73 20 69 73 20 75 73 65 64  ** codes is used
21f90 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70   as the second p
21fa0 61 72 61 6d 65 74 65 72 2e 20 20 5e 28 54 68 65  arameter.  ^(The
21fb0 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   5th parameter t
21fc0 6f 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f 72 69  o the.** authori
21fd0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  zer callback is 
21fe0 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
21ff0 64 61 74 61 62 61 73 65 20 28 22 6d 61 69 6e 22  database ("main"
22000 2c 20 22 74 65 6d 70 22 2c 0a 2a 2a 20 65 74 63  , "temp",.** etc
22010 2e 29 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65  .) if applicable
22020 2e 29 5e 20 20 5e 54 68 65 20 36 74 68 20 70 61  .)^  ^The 6th pa
22030 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61  rameter to the a
22040 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
22050 63 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d  ck.** is the nam
22060 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d 6d  e of the inner-m
22070 6f 73 74 20 74 72 69 67 67 65 72 20 6f 72 20 76  ost trigger or v
22080 69 65 77 20 74 68 61 74 20 69 73 20 72 65 73 70  iew that is resp
22090 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74  onsible for.** t
220a0 68 65 20 61 63 63 65 73 73 20 61 74 74 65 6d 70  he access attemp
220b0 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 69  t or NULL if thi
220c0 73 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 74  s access attempt
220d0 20 69 73 20 64 69 72 65 63 74 6c 79 20 66 72 6f   is directly fro
220e0 6d 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c 20 53  m.** top-level S
220f0 51 4c 20 63 6f 64 65 2e 0a 2a 2f 0a 2f 2a 2a 2a  QL code..*/./***
22100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22120 2a 2a 2a 2a 2a 2a 2a 2a 20 33 72 64 20 2a 2a 2a  ******** 3rd ***
22130 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 34 74 68 20 2a 2a  ********* 4th **
22140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69  *********/.#defi
22150 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  ne SQLITE_CREATE
22160 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20  _INDEX          
22170 31 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d  1   /* Index Nam
22180 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d  e      Table Nam
22190 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  e      */.#defin
221a0 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  e SQLITE_CREATE_
221b0 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 32  TABLE          2
221c0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
221d0 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
221e0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
221f0 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
22200 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 33 20  EMP_INDEX     3 
22210 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20    /* Index Name 
22220 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20       Table Name 
22230 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
22240 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45  SQLITE_CREATE_TE
22250 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 34 20 20  MP_TABLE     4  
22260 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20   /* Table Name  
22270 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
22280 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
22290 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
222a0 50 5f 54 52 49 47 47 45 52 20 20 20 35 20 20 20  P_TRIGGER   5   
222b0 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20  /* Trigger Name 
222c0 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20     Table Name   
222d0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
222e0 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
222f0 5f 56 49 45 57 20 20 20 20 20 20 36 20 20 20 2f  _VIEW      6   /
22300 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20  * View Name     
22310 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
22320 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
22330 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49 47 47  ITE_CREATE_TRIGG
22340 45 52 20 20 20 20 20 20 20 20 37 20 20 20 2f 2a  ER        7   /*
22350 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20   Trigger Name   
22360 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
22370 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
22380 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57 20 20  TE_CREATE_VIEW  
22390 20 20 20 20 20 20 20 20 20 38 20 20 20 2f 2a 20           8   /* 
223a0 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20  View Name       
223b0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
223c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
223d0 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20 20  E_DELETE        
223e0 20 20 20 20 20 20 20 20 39 20 20 20 2f 2a 20 54          9   /* T
223f0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e  able Name      N
22400 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
22410 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
22420 5f 44 52 4f 50 5f 49 4e 44 45 58 20 20 20 20 20  _DROP_INDEX     
22430 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20 49 6e        10   /* In
22440 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61  dex Name      Ta
22450 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f  ble Name      */
22460 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
22470 44 52 4f 50 5f 54 41 42 4c 45 20 20 20 20 20 20  DROP_TABLE      
22480 20 20 20 20 20 31 31 20 20 20 2f 2a 20 54 61 62       11   /* Tab
22490 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c  le Name      NUL
224a0 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
224b0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
224c0 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 20 20  ROP_TEMP_INDEX  
224d0 20 20 20 20 31 32 20 20 20 2f 2a 20 49 6e 64 65      12   /* Inde
224e0 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c  x Name      Tabl
224f0 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23  e Name      */.#
22500 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52  define SQLITE_DR
22510 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 20 20 20  OP_TEMP_TABLE   
22520 20 20 20 31 33 20 20 20 2f 2a 20 54 61 62 6c 65     13   /* Table
22530 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20   Name      NULL 
22540 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
22550 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f  efine SQLITE_DRO
22560 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 20 20  P_TEMP_TRIGGER  
22570 20 20 31 34 20 20 20 2f 2a 20 54 72 69 67 67 65    14   /* Trigge
22580 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20  r Name    Table 
22590 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65  Name      */.#de
225a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50  fine SQLITE_DROP
225b0 5f 54 45 4d 50 5f 56 49 45 57 20 20 20 20 20 20  _TEMP_VIEW      
225c0 20 31 35 20 20 20 2f 2a 20 56 69 65 77 20 4e 61   15   /* View Na
225d0 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  me       NULL   
225e0 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
225f0 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  ine SQLITE_DROP_
22600 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20 20  TRIGGER         
22610 31 36 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20  16   /* Trigger 
22620 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61  Name    Table Na
22630 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  me      */.#defi
22640 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56  ne SQLITE_DROP_V
22650 49 45 57 20 20 20 20 20 20 20 20 20 20 20 20 31  IEW            1
22660 37 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65  7   /* View Name
22670 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20         NULL     
22680 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
22690 65 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 20  e SQLITE_INSERT 
226a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 38                18
226b0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
226c0 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
226d0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
226e0 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41 20 20   SQLITE_PRAGMA  
226f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39 20               19 
22700 20 20 2f 2a 20 50 72 61 67 6d 61 20 4e 61 6d 65    /* Pragma Name
22710 20 20 20 20 20 31 73 74 20 61 72 67 20 6f 72 20       1st arg or 
22720 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NULL */.#define 
22730 53 51 4c 49 54 45 5f 52 45 41 44 20 20 20 20 20  SQLITE_READ     
22740 20 20 20 20 20 20 20 20 20 20 20 20 32 30 20 20              20  
22750 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20   /* Table Name  
22760 20 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20      Column Name 
22770 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
22780 51 4c 49 54 45 5f 53 45 4c 45 43 54 20 20 20 20  QLITE_SELECT    
22790 20 20 20 20 20 20 20 20 20 20 20 32 31 20 20 20             21   
227a0 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20  /* NULL         
227b0 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
227c0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
227d0 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e  LITE_TRANSACTION
227e0 20 20 20 20 20 20 20 20 20 20 32 32 20 20 20 2f            22   /
227f0 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 20 20  * Operation     
22800 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
22810 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
22820 49 54 45 5f 55 50 44 41 54 45 20 20 20 20 20 20  ITE_UPDATE      
22830 20 20 20 20 20 20 20 20 20 32 33 20 20 20 2f 2a           23   /*
22840 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
22850 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20   Column Name    
22860 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
22870 54 45 5f 41 54 54 41 43 48 20 20 20 20 20 20 20  TE_ATTACH       
22880 20 20 20 20 20 20 20 20 32 34 20 20 20 2f 2a 20          24   /* 
22890 46 69 6c 65 6e 61 6d 65 20 20 20 20 20 20 20 20  Filename        
228a0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
228b0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
228c0 45 5f 44 45 54 41 43 48 20 20 20 20 20 20 20 20  E_DETACH        
228d0 20 20 20 20 20 20 20 32 35 20 20 20 2f 2a 20 44         25   /* D
228e0 61 74 61 62 61 73 65 20 4e 61 6d 65 20 20 20 4e  atabase Name   N
228f0 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
22900 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
22910 5f 41 4c 54 45 52 5f 54 41 42 4c 45 20 20 20 20  _ALTER_TABLE    
22920 20 20 20 20 20 20 32 36 20 20 20 2f 2a 20 44 61        26   /* Da
22930 74 61 62 61 73 65 20 4e 61 6d 65 20 20 20 54 61  tabase Name   Ta
22940 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f  ble Name      */
22950 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
22960 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20 20  REINDEX         
22970 20 20 20 20 20 32 37 20 20 20 2f 2a 20 49 6e 64       27   /* Ind
22980 65 78 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c  ex Name      NUL
22990 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
229a0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
229b0 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20 20  NALYZE          
229c0 20 20 20 20 32 38 20 20 20 2f 2a 20 54 61 62 6c      28   /* Tabl
229d0 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  e Name      NULL
229e0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
229f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52  define SQLITE_CR
22a00 45 41 54 45 5f 56 54 41 42 4c 45 20 20 20 20 20  EATE_VTABLE     
22a10 20 20 20 32 39 20 20 20 2f 2a 20 54 61 62 6c 65     29   /* Table
22a20 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75 6c   Name      Modul
22a30 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64  e Name     */.#d
22a40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f  efine SQLITE_DRO
22a50 50 5f 56 54 41 42 4c 45 20 20 20 20 20 20 20 20  P_VTABLE        
22a60 20 20 33 30 20 20 20 2f 2a 20 54 61 62 6c 65 20    30   /* Table 
22a70 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75 6c 65  Name      Module
22a80 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65   Name     */.#de
22a90 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43  fine SQLITE_FUNC
22aa0 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20  TION            
22ab0 20 33 31 20 20 20 2f 2a 20 4e 55 4c 4c 20 20 20   31   /* NULL   
22ac0 20 20 20 20 20 20 20 20 20 46 75 6e 63 74 69 6f           Functio
22ad0 6e 20 4e 61 6d 65 20 20 20 2a 2f 0a 23 64 65 66  n Name   */.#def
22ae0 69 6e 65 20 53 51 4c 49 54 45 5f 53 41 56 45 50  ine SQLITE_SAVEP
22af0 4f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 20  OINT            
22b00 33 32 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f  32   /* Operatio
22b10 6e 20 20 20 20 20 20 20 53 61 76 65 70 6f 69 6e  n       Savepoin
22b20 74 20 4e 61 6d 65 20 20 2a 2f 0a 23 64 65 66 69  t Name  */.#defi
22b30 6e 65 20 53 51 4c 49 54 45 5f 43 4f 50 59 20 20  ne SQLITE_COPY  
22b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22b50 30 20 20 20 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72  0   /* No longer
22b60 20 75 73 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20   used */../*.** 
22b70 43 41 50 49 33 52 45 46 3a 20 54 72 61 63 69 6e  CAPI3REF: Tracin
22b80 67 20 41 6e 64 20 50 72 6f 66 69 6c 69 6e 67 20  g And Profiling 
22b90 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20  Functions.**.** 
22ba0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72  These routines r
22bb0 65 67 69 73 74 65 72 20 63 61 6c 6c 62 61 63 6b  egister callback
22bc0 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
22bd0 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 0a  can be used for.
22be0 2a 2a 20 74 72 61 63 69 6e 67 20 61 6e 64 20 70  ** tracing and p
22bf0 72 6f 66 69 6c 69 6e 67 20 74 68 65 20 65 78 65  rofiling the exe
22c00 63 75 74 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74  cution of SQL st
22c10 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  atements..**.** 
22c20 5e 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75  ^The callback fu
22c30 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65  nction registere
22c40 64 20 62 79 20 73 71 6c 69 74 65 33 5f 74 72 61  d by sqlite3_tra
22c50 63 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20  ce() is invoked 
22c60 61 74 0a 2a 2a 20 76 61 72 69 6f 75 73 20 74 69  at.** various ti
22c70 6d 65 73 20 77 68 65 6e 20 61 6e 20 53 51 4c 20  mes when an SQL 
22c80 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62 65 69  statement is bei
22c90 6e 67 20 72 75 6e 20 62 79 20 5b 73 71 6c 69 74  ng run by [sqlit
22ca0 65 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 5e  e3_step()]..** ^
22cb0 54 68 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63  The sqlite3_trac
22cc0 65 28 29 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  e() callback is 
22cd0 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 61 20 55  invoked with a U
22ce0 54 46 2d 38 20 72 65 6e 64 65 72 69 6e 67 20 6f  TF-8 rendering o
22cf0 66 20 74 68 65 0a 2a 2a 20 53 51 4c 20 73 74 61  f the.** SQL sta
22d00 74 65 6d 65 6e 74 20 74 65 78 74 20 61 73 20 74  tement text as t
22d10 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66 69 72  he statement fir
22d20 73 74 20 62 65 67 69 6e 73 20 65 78 65 63 75 74  st begins execut
22d30 69 6e 67 2e 0a 2a 2a 20 5e 28 41 64 64 69 74 69  ing..** ^(Additi
22d40 6f 6e 61 6c 20 73 71 6c 69 74 65 33 5f 74 72 61  onal sqlite3_tra
22d50 63 65 28 29 20 63 61 6c 6c 62 61 63 6b 73 20 6d  ce() callbacks m
22d60 69 67 68 74 20 6f 63 63 75 72 0a 2a 2a 20 61 73  ight occur.** as
22d70 20 65 61 63 68 20 74 72 69 67 67 65 72 65 64 20   each triggered 
22d80 73 75 62 70 72 6f 67 72 61 6d 20 69 73 20 65 6e  subprogram is en
22d90 74 65 72 65 64 2e 20 20 54 68 65 20 63 61 6c 6c  tered.  The call
22da0 62 61 63 6b 73 20 66 6f 72 20 74 72 69 67 67 65  backs for trigge
22db0 72 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 20  rs.** contain a 
22dc0 55 54 46 2d 38 20 53 51 4c 20 63 6f 6d 6d 65 6e  UTF-8 SQL commen
22dd0 74 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 65  t that identifie
22de0 73 20 74 68 65 20 74 72 69 67 67 65 72 2e 29 5e  s the trigger.)^
22df0 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 63 61 6c 6c  .**.** ^The call
22e00 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65  back function re
22e10 67 69 73 74 65 72 65 64 20 62 79 20 73 71 6c 69  gistered by sqli
22e20 74 65 33 5f 70 72 6f 66 69 6c 65 28 29 20 69 73  te3_profile() is
22e30 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 61 73 20 65   invoked.** as e
22e40 61 63 68 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ach SQL statemen
22e50 74 20 66 69 6e 69 73 68 65 73 2e 20 20 5e 54 68  t finishes.  ^Th
22e60 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61  e profile callba
22e70 63 6b 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74  ck contains.** t
22e80 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74  he original stat
22e90 65 6d 65 6e 74 20 74 65 78 74 20 61 6e 64 20 61  ement text and a
22ea0 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 77 61  n estimate of wa
22eb0 6c 6c 2d 63 6c 6f 63 6b 20 74 69 6d 65 0a 2a 2a  ll-clock time.**
22ec0 20 6f 66 20 68 6f 77 20 6c 6f 6e 67 20 74 68 61   of how long tha
22ed0 74 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 6f 6b  t statement took
22ee0 20 74 6f 20 72 75 6e 2e 20 20 5e 54 68 65 20 70   to run.  ^The p
22ef0 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 0a  rofile callback.
22f00 2a 2a 20 74 69 6d 65 20 69 73 20 69 6e 20 75 6e  ** time is in un
22f10 69 74 73 20 6f 66 20 6e 61 6e 6f 73 65 63 6f 6e  its of nanosecon
22f20 64 73 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20  ds, however the 
22f30 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e  current implemen
22f40 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 6e 6c  tation.** is onl
22f50 79 20 63 61 70 61 62 6c 65 20 6f 66 20 6d 69 6c  y capable of mil
22f60 6c 69 73 65 63 6f 6e 64 20 72 65 73 6f 6c 75 74  lisecond resolut
22f70 69 6f 6e 20 73 6f 20 74 68 65 20 73 69 78 20 6c  ion so the six l
22f80 65 61 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74  east significant
22f90 0a 2a 2a 20 64 69 67 69 74 73 20 69 6e 20 74 68  .** digits in th
22fa0 65 20 74 69 6d 65 20 61 72 65 20 6d 65 61 6e 69  e time are meani
22fb0 6e 67 6c 65 73 73 2e 20 20 46 75 74 75 72 65 20  ngless.  Future 
22fc0 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  versions of SQLi
22fd0 74 65 0a 2a 2a 20 6d 69 67 68 74 20 70 72 6f 76  te.** might prov
22fe0 69 64 65 20 67 72 65 61 74 65 72 20 72 65 73 6f  ide greater reso
22ff0 6c 75 74 69 6f 6e 20 6f 6e 20 74 68 65 20 70 72  lution on the pr
23000 6f 66 69 6c 65 72 20 63 61 6c 6c 62 61 63 6b 2e  ofiler callback.
23010 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33    The.** sqlite3
23020 5f 70 72 6f 66 69 6c 65 28 29 20 66 75 6e 63 74  _profile() funct
23030 69 6f 6e 20 69 73 20 63 6f 6e 73 69 64 65 72 65  ion is considere
23040 64 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61  d experimental a
23050 6e 64 20 69 73 0a 2a 2a 20 73 75 62 6a 65 63 74  nd is.** subject
23060 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 66 75   to change in fu
23070 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66  ture versions of
23080 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49   SQLite..*/.SQLI
23090 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
230a0 69 74 65 33 5f 74 72 61 63 65 28 73 71 6c 69 74  ite3_trace(sqlit
230b0 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54 72 61 63  e3*, void(*xTrac
230c0 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
230d0 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 53  har*), void*);.S
230e0 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45  QLITE_API SQLITE
230f0 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f  _EXPERIMENTAL vo
23100 69 64 20 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66  id *sqlite3_prof
23110 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c 0a 20 20  ile(sqlite3*,.  
23120 20 76 6f 69 64 28 2a 78 50 72 6f 66 69 6c 65 29   void(*xProfile)
23130 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
23140 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69 6e 74 36  r*,sqlite3_uint6
23150 34 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a  4), void*);../*.
23160 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 51 75 65  ** CAPI3REF: Que
23170 72 79 20 50 72 6f 67 72 65 73 73 20 43 61 6c 6c  ry Progress Call
23180 62 61 63 6b 73 0a 2a 2a 0a 2a 2a 20 5e 54 68 65  backs.**.** ^The
23190 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73   sqlite3_progres
231a0 73 5f 68 61 6e 64 6c 65 72 28 44 2c 4e 2c 58 2c  s_handler(D,N,X,
231b0 50 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 75  P) interface cau
231c0 73 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  ses the callback
231d0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 58 20 74  .** function X t
231e0 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 65 72  o be invoked per
231f0 69 6f 64 69 63 61 6c 6c 79 20 64 75 72 69 6e 67  iodically during
23200 20 6c 6f 6e 67 20 72 75 6e 6e 69 6e 67 20 63 61   long running ca
23210 6c 6c 73 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74  lls to.** [sqlit
23220 65 33 5f 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c  e3_exec()], [sql
23230 69 74 65 33 5f 73 74 65 70 28 29 5d 20 61 6e 64  ite3_step()] and
23240 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61   [sqlite3_get_ta
23250 62 6c 65 28 29 5d 20 66 6f 72 0a 2a 2a 20 64 61  ble()] for.** da
23260 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
23270 6e 20 44 2e 20 20 41 6e 20 65 78 61 6d 70 6c 65  n D.  An example
23280 20 75 73 65 20 66 6f 72 20 74 68 69 73 0a 2a 2a   use for this.**
23290 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 74 6f   interface is to
232a0 20 6b 65 65 70 20 61 20 47 55 49 20 75 70 64 61   keep a GUI upda
232b0 74 65 64 20 64 75 72 69 6e 67 20 61 20 6c 61 72  ted during a lar
232c0 67 65 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20  ge query..**.** 
232d0 5e 54 68 65 20 70 61 72 61 6d 65 74 65 72 20 50  ^The parameter P
232e0 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75   is passed throu
232f0 67 68 20 61 73 20 74 68 65 20 6f 6e 6c 79 20 70  gh as the only p
23300 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
23310 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  .** callback fun
23320 63 74 69 6f 6e 20 58 2e 20 20 5e 54 68 65 20 70  ction X.  ^The p
23330 61 72 61 6d 65 74 65 72 20 4e 20 69 73 20 74 68  arameter N is th
23340 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 2a 2a 20  e number of .** 
23350 5b 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65  [virtual machine
23360 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 5d 20 74   instructions] t
23370 68 61 74 20 61 72 65 20 65 76 61 6c 75 61 74 65  hat are evaluate
23380 64 20 62 65 74 77 65 65 6e 20 73 75 63 63 65 73  d between succes
23390 73 69 76 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69  sive.** invocati
233a0 6f 6e 73 20 6f 66 20 74 68 65 20 63 61 6c 6c 62  ons of the callb
233b0 61 63 6b 20 58 2e 0a 2a 2a 0a 2a 2a 20 5e 4f 6e  ack X..**.** ^On
233c0 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 67  ly a single prog
233d0 72 65 73 73 20 68 61 6e 64 6c 65 72 20 6d 61 79  ress handler may
233e0 20 62 65 20 64 65 66 69 6e 65 64 20 61 74 20 6f   be defined at o
233f0 6e 65 20 74 69 6d 65 20 70 65 72 0a 2a 2a 20 5b  ne time per.** [
23400 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
23410 69 6f 6e 5d 3b 20 73 65 74 74 69 6e 67 20 61 20  ion]; setting a 
23420 6e 65 77 20 70 72 6f 67 72 65 73 73 20 68 61 6e  new progress han
23430 64 6c 65 72 20 63 61 6e 63 65 6c 73 20 74 68 65  dler cancels the
23440 0a 2a 2a 20 6f 6c 64 20 6f 6e 65 2e 20 20 5e 53  .** old one.  ^S
23450 65 74 74 69 6e 67 20 70 61 72 61 6d 65 74 65 72  etting parameter
23460 20 58 20 74 6f 20 4e 55 4c 4c 20 64 69 73 61 62   X to NULL disab
23470 6c 65 73 20 74 68 65 20 70 72 6f 67 72 65 73 73  les the progress
23480 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 5e 54 68   handler..** ^Th
23490 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c  e progress handl
234a0 65 72 20 69 73 20 61 6c 73 6f 20 64 69 73 61 62  er is also disab
234b0 6c 65 64 20 62 79 20 73 65 74 74 69 6e 67 20 4e  led by setting N
234c0 20 74 6f 20 61 20 76 61 6c 75 65 20 6c 65 73 73   to a value less
234d0 0a 2a 2a 20 74 68 61 6e 20 31 2e 0a 2a 2a 0a 2a  .** than 1..**.*
234e0 2a 20 5e 49 66 20 74 68 65 20 70 72 6f 67 72 65  * ^If the progre
234f0 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  ss callback retu
23500 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68  rns non-zero, th
23510 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a  e operation is.*
23520 2a 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 20  * interrupted.  
23530 54 68 69 73 20 66 65 61 74 75 72 65 20 63 61 6e  This feature can
23540 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c   be used to impl
23550 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e 63  ement a.** "Canc
23560 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 20  el" button on a 
23570 47 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 61  GUI progress dia
23580 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54  log box..**.** T
23590 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64  he progress hand
235a0 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d 75 73  ler callback mus
235b0 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e  t not do anythin
235c0 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69  g that will modi
235d0 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  fy.** the databa
235e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68  se connection th
235f0 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 70  at invoked the p
23600 72 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 72 2e  rogress handler.
23610 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73  .** Note that [s
23620 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
23630 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  2()] and [sqlite
23640 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d  3_step()] both m
23650 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64  odify their.** d
23660 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
23670 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e  ons for the mean
23680 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20  ing of "modify" 
23690 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70  in this paragrap
236a0 68 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f  h..**.*/.SQLITE_
236b0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
236c0 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65  _progress_handle
236d0 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c  r(sqlite3*, int,
236e0 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20   int(*)(void*), 
236f0 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  void*);../*.** C
23700 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 69 6e 67  API3REF: Opening
23710 20 41 20 4e 65 77 20 44 61 74 61 62 61 73 65 20   A New Database 
23720 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a  Connection.**.**
23730 20 5e 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73   ^These routines
23740 20 6f 70 65 6e 20 61 6e 20 53 51 4c 69 74 65 20   open an SQLite 
23750 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 73  database file as
23760 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68   specified by th
23770 65 20 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61  e .** filename a
23780 72 67 75 6d 65 6e 74 2e 20 5e 54 68 65 20 66 69  rgument. ^The fi
23790 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20  lename argument 
237a0 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  is interpreted a
237b0 73 20 55 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73  s UTF-8 for.** s
237c0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e  qlite3_open() an
237d0 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76  d sqlite3_open_v
237e0 32 28 29 20 61 6e 64 20 61 73 20 55 54 46 2d 31  2() and as UTF-1
237f0 36 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20  6 in the native 
23800 62 79 74 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f  byte.** order fo
23810 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36  r sqlite3_open16
23820 28 29 2e 20 5e 28 41 20 5b 64 61 74 61 62 61 73  (). ^(A [databas
23830 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61  e connection] ha
23840 6e 64 6c 65 20 69 73 20 75 73 75 61 6c 6c 79 0a  ndle is usually.
23850 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a  ** returned in *
23860 70 70 44 62 2c 20 65 76 65 6e 20 69 66 20 61 6e  ppDb, even if an
23870 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 20   error occurs.  
23880 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70 74 69  The only excepti
23890 6f 6e 20 69 73 20 74 68 61 74 0a 2a 2a 20 69 66  on is that.** if
238a0 20 53 51 4c 69 74 65 20 69 73 20 75 6e 61 62 6c   SQLite is unabl
238b0 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65  e to allocate me
238c0 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65  mory to hold the
238d0 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63   [sqlite3] objec
238e0 74 2c 0a 2a 2a 20 61 20 4e 55 4c 4c 20 77 69 6c  t,.** a NULL wil
238f0 6c 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74  l be written int
23900 6f 20 2a 70 70 44 62 20 69 6e 73 74 65 61 64 20  o *ppDb instead 
23910 6f 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  of a pointer to 
23920 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 0a 2a 2a  the [sqlite3].**
23930 20 6f 62 6a 65 63 74 2e 29 5e 20 5e 28 49 66 20   object.)^ ^(If 
23940 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
23950 6f 70 65 6e 65 64 20 28 61 6e 64 2f 6f 72 20 63  opened (and/or c
23960 72 65 61 74 65 64 29 20 73 75 63 63 65 73 73 66  reated) successf
23970 75 6c 6c 79 2c 20 74 68 65 6e 0a 2a 2a 20 5b 53  ully, then.** [S
23980 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74  QLITE_OK] is ret
23990 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73  urned.  Otherwis
239a0 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  e an [error code
239b0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 29 5e  ] is returned.)^
239c0 20 5e 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65   ^The.** [sqlite
239d0 33 5f 65 72 72 6d 73 67 28 29 5d 20 6f 72 20 5b  3_errmsg()] or [
239e0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
239f0 28 29 5d 20 72 6f 75 74 69 6e 65 73 20 63 61 6e  ()] routines can
23a00 20 62 65 20 75 73 65 64 20 74 6f 20 6f 62 74 61   be used to obta
23a10 69 6e 0a 2a 2a 20 61 6e 20 45 6e 67 6c 69 73 68  in.** an English
23a20 20 6c 61 6e 67 75 61 67 65 20 64 65 73 63 72 69   language descri
23a30 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 65 72 72  ption of the err
23a40 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 66  or following a f
23a50 61 69 6c 75 72 65 20 6f 66 20 61 6e 79 0a 2a 2a  ailure of any.**
23a60 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
23a70 6f 70 65 6e 28 29 20 72 6f 75 74 69 6e 65 73 2e  open() routines.
23a80 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 64 65 66 61  .**.** ^The defa
23a90 75 6c 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72  ult encoding for
23aa0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 69   the database wi
23ab0 6c 6c 20 62 65 20 55 54 46 2d 38 20 69 66 0a 2a  ll be UTF-8 if.*
23ac0 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  * sqlite3_open()
23ad0 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e   or sqlite3_open
23ae0 5f 76 32 28 29 20 69 73 20 63 61 6c 6c 65 64 20  _v2() is called 
23af0 61 6e 64 0a 2a 2a 20 55 54 46 2d 31 36 20 69 6e  and.** UTF-16 in
23b00 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65   the native byte
23b10 20 6f 72 64 65 72 20 69 66 20 73 71 6c 69 74 65   order if sqlite
23b20 33 5f 6f 70 65 6e 31 36 28 29 20 69 73 20 75 73  3_open16() is us
23b30 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65  ed..**.** Whethe
23b40 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f  r or not an erro
23b50 72 20 6f 63 63 75 72 73 20 77 68 65 6e 20 69 74  r occurs when it
23b60 20 69 73 20 6f 70 65 6e 65 64 2c 20 72 65 73 6f   is opened, reso
23b70 75 72 63 65 73 0a 2a 2a 20 61 73 73 6f 63 69 61  urces.** associa
23b80 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61  ted with the [da
23b90 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
23ba0 6e 5d 20 68 61 6e 64 6c 65 20 73 68 6f 75 6c 64  n] handle should
23bb0 20 62 65 20 72 65 6c 65 61 73 65 64 20 62 79 0a   be released by.
23bc0 2a 2a 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f  ** passing it to
23bd0 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28   [sqlite3_close(
23be0 29 5d 20 77 68 65 6e 20 69 74 20 69 73 20 6e 6f  )] when it is no
23bf0 20 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64   longer required
23c00 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
23c10 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 6e  te3_open_v2() in
23c20 74 65 72 66 61 63 65 20 77 6f 72 6b 73 20 6c 69  terface works li
23c30 6b 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  ke sqlite3_open(
23c40 29 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74  ).** except that
23c50 20 69 74 20 61 63 63 65 70 74 73 20 74 77 6f 20   it accepts two 
23c60 61 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d  additional param
23c70 65 74 65 72 73 20 66 6f 72 20 61 64 64 69 74 69  eters for additi
23c80 6f 6e 61 6c 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20  onal control.** 
23c90 6f 76 65 72 20 74 68 65 20 6e 65 77 20 64 61 74  over the new dat
23ca0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
23cb0 2e 20 20 5e 28 54 68 65 20 66 6c 61 67 73 20 70  .  ^(The flags p
23cc0 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 73  arameter to.** s
23cd0 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
23ce0 20 63 61 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66   can take one of
23cf0 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** the followin
23d00 67 20 74 68 72 65 65 20 76 61 6c 75 65 73 2c 20  g three values, 
23d10 6f 70 74 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69  optionally combi
23d20 6e 65 64 20 77 69 74 68 20 74 68 65 20 0a 2a 2a  ned with the .**
23d30 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f   [SQLITE_OPEN_NO
23d40 4d 55 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f  MUTEX], [SQLITE_
23d50 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c  OPEN_FULLMUTEX],
23d60 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48   [SQLITE_OPEN_SH
23d70 41 52 45 44 43 41 43 48 45 5d 2c 0a 2a 2a 20 5b  AREDCACHE],.** [
23d80 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56  SQLITE_OPEN_PRIV
23d90 41 54 45 43 41 43 48 45 5d 2c 20 61 6e 64 2f 6f  ATECACHE], and/o
23da0 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55  r [SQLITE_OPEN_U
23db0 52 49 5d 20 66 6c 61 67 73 3a 29 5e 0a 2a 2a 0a  RI] flags:)^.**.
23dc0 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 5e 28 3c 64 74  ** <dl>.** ^(<dt
23dd0 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  >[SQLITE_OPEN_RE
23de0 41 44 4f 4e 4c 59 5d 3c 2f 64 74 3e 0a 2a 2a 20  ADONLY]</dt>.** 
23df0 3c 64 64 3e 54 68 65 20 64 61 74 61 62 61 73 65  <dd>The database
23e00 20 69 73 20 6f 70 65 6e 65 64 20 69 6e 20 72 65   is opened in re
23e10 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 2e 20 20 49  ad-only mode.  I
23e20 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 64  f the database d
23e30 6f 65 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61  oes not.** alrea
23e40 64 79 20 65 78 69 73 74 2c 20 61 6e 20 65 72 72  dy exist, an err
23e50 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 3c  or is returned.<
23e60 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 28 3c  /dd>)^.**.** ^(<
23e70 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  dt>[SQLITE_OPEN_
23e80 52 45 41 44 57 52 49 54 45 5d 3c 2f 64 74 3e 0a  READWRITE]</dt>.
23e90 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 61 62  ** <dd>The datab
23ea0 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f  ase is opened fo
23eb0 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72  r reading and wr
23ec0 69 74 69 6e 67 20 69 66 20 70 6f 73 73 69 62 6c  iting if possibl
23ed0 65 2c 20 6f 72 20 72 65 61 64 69 6e 67 0a 2a 2a  e, or reading.**
23ee0 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 66 69 6c   only if the fil
23ef0 65 20 69 73 20 77 72 69 74 65 20 70 72 6f 74 65  e is write prote
23f00 63 74 65 64 20 62 79 20 74 68 65 20 6f 70 65 72  cted by the oper
23f10 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 49  ating system.  I
23f20 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63 61 73 65  n either.** case
23f30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75   the database mu
23f40 73 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  st already exist
23f50 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65  , otherwise an e
23f60 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
23f70 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5e  .</dd>)^.**.** ^
23f80 28 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45  (<dt>[SQLITE_OPE
23f90 4e 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b  N_READWRITE] | [
23fa0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
23fb0 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  TE]</dt>.** <dd>
23fc0 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  The database is 
23fd0 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69  opened for readi
23fe0 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20  ng and writing, 
23ff0 61 6e 64 20 69 73 20 63 72 65 61 74 65 64 20 69  and is created i
24000 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e 6f 74  f.** it does not
24010 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e 20   already exist. 
24020 54 68 69 73 20 69 73 20 74 68 65 20 62 65 68 61  This is the beha
24030 76 69 6f 72 20 74 68 61 74 20 69 73 20 61 6c 77  vior that is alw
24040 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20  ays used for.** 
24050 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61  sqlite3_open() a
24060 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  nd sqlite3_open1
24070 36 28 29 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 20 3c  6().</dd>)^.** <
24080 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  /dl>.**.** If th
24090 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20  e 3rd parameter 
240a0 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  to sqlite3_open_
240b0 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20  v2() is not one 
240c0 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e  of the.** combin
240d0 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f  ations shown abo
240e0 76 65 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 63 6f  ve optionally co
240f0 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65  mbined with othe
24100 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45  r.** [SQLITE_OPE
24110 4e 5f 52 45 41 44 4f 4e 4c 59 20 7c 20 53 51 4c  N_READONLY | SQL
24120 49 54 45 5f 4f 50 45 4e 5f 2a 20 62 69 74 73 5d  ITE_OPEN_* bits]
24130 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68  .** then the beh
24140 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
24150 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 74 68  ed..**.** ^If th
24160 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e  e [SQLITE_OPEN_N
24170 4f 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73 20  OMUTEX] flag is 
24180 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 64 61  set, then the da
24190 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
241a0 6e 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e 20 74 68  n.** opens in th
241b0 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 20 5b  e multi-thread [
241c0 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20  threading mode] 
241d0 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 73  as long as the s
241e0 69 6e 67 6c 65 2d 74 68 72 65 61 64 0a 2a 2a 20  ingle-thread.** 
241f0 6d 6f 64 65 20 68 61 73 20 6e 6f 74 20 62 65 65  mode has not bee
24200 6e 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65  n set at compile
24210 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d 74  -time or start-t
24220 69 6d 65 2e 20 20 5e 49 66 20 74 68 65 0a 2a 2a  ime.  ^If the.**
24230 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55   [SQLITE_OPEN_FU
24240 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73  LLMUTEX] flag is
24250 20 73 65 74 20 74 68 65 6e 20 74 68 65 20 64 61   set then the da
24260 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
24270 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20 74 68  n opens.** in th
24280 65 20 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68  e serialized [th
24290 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 75 6e  reading mode] un
242a0 6c 65 73 73 20 73 69 6e 67 6c 65 2d 74 68 72 65  less single-thre
242b0 61 64 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f  ad was.** previo
242c0 75 73 6c 79 20 73 65 6c 65 63 74 65 64 20 61 74  usly selected at
242d0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72   compile-time or
242e0 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 20   start-time..** 
242f0 5e 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45  ^The [SQLITE_OPE
24300 4e 5f 53 48 41 52 45 44 43 41 43 48 45 5d 20 66  N_SHAREDCACHE] f
24310 6c 61 67 20 63 61 75 73 65 73 20 74 68 65 20 64  lag causes the d
24320 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
24330 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 6c 69 67  on to be.** elig
24340 69 62 6c 65 20 74 6f 20 75 73 65 20 5b 73 68 61  ible to use [sha
24350 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c  red cache mode],
24360 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77   regardless of w
24370 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 68  hether or not sh
24380 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 73  ared.** cache is
24390 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 20 5b   enabled using [
243a0 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73  sqlite3_enable_s
243b0 68 61 72 65 64 5f 63 61 63 68 65 28 29 5d 2e 20  hared_cache()]. 
243c0 20 5e 54 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45   ^The.** [SQLITE
243d0 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45 43 41 43  _OPEN_PRIVATECAC
243e0 48 45 5d 20 66 6c 61 67 20 63 61 75 73 65 73 20  HE] flag causes 
243f0 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
24400 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 0a 2a  nection to not.*
24410 2a 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e  * participate in
24420 20 5b 73 68 61 72 65 64 20 63 61 63 68 65 20 6d   [shared cache m
24430 6f 64 65 5d 20 65 76 65 6e 20 69 66 20 69 74 20  ode] even if it 
24440 69 73 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a  is enabled..**.*
24450 2a 20 5e 54 68 65 20 66 6f 75 72 74 68 20 70 61  * ^The fourth pa
24460 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
24470 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20  e3_open_v2() is 
24480 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a  the name of the.
24490 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d  ** [sqlite3_vfs]
244a0 20 6f 62 6a 65 63 74 20 74 68 61 74 20 64 65 66   object that def
244b0 69 6e 65 73 20 74 68 65 20 6f 70 65 72 61 74 69  ines the operati
244c0 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66  ng system interf
244d0 61 63 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  ace that.** the 
244e0 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e  new database con
244f0 6e 65 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75  nection should u
24500 73 65 2e 20 20 5e 49 66 20 74 68 65 20 66 6f 75  se.  ^If the fou
24510 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73  rth parameter is
24520 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  .** a NULL point
24530 65 72 20 74 68 65 6e 20 74 68 65 20 64 65 66 61  er then the defa
24540 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  ult [sqlite3_vfs
24550 5d 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64  ] object is used
24560 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 74 68 65 20  ..**.** ^If the 
24570 66 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65  filename is ":me
24580 6d 6f 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70  mory:", then a p
24590 72 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72  rivate, temporar
245a0 79 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  y in-memory data
245b0 62 61 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74  base.** is creat
245c0 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65  ed for the conne
245d0 63 74 69 6f 6e 2e 20 20 5e 54 68 69 73 20 69 6e  ction.  ^This in
245e0 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
245f0 20 77 69 6c 6c 20 76 61 6e 69 73 68 20 77 68 65   will vanish whe
24600 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  n.** the databas
24610 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
24620 63 6c 6f 73 65 64 2e 20 20 46 75 74 75 72 65 20  closed.  Future 
24630 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  versions of SQLi
24640 74 65 20 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65  te might.** make
24650 20 75 73 65 20 6f 66 20 61 64 64 69 74 69 6f 6e   use of addition
24660 61 6c 20 73 70 65 63 69 61 6c 20 66 69 6c 65 6e  al special filen
24670 61 6d 65 73 20 74 68 61 74 20 62 65 67 69 6e 20  ames that begin 
24680 77 69 74 68 20 74 68 65 20 22 3a 22 20 63 68 61  with the ":" cha
24690 72 61 63 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73  racter..** It is
246a0 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61   recommended tha
246b0 74 20 77 68 65 6e 20 61 20 64 61 74 61 62 61 73  t when a databas
246c0 65 20 66 69 6c 65 6e 61 6d 65 20 61 63 74 75 61  e filename actua
246d0 6c 6c 79 20 64 6f 65 73 20 62 65 67 69 6e 20 77  lly does begin w
246e0 69 74 68 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61  ith.** a ":" cha
246f0 72 61 63 74 65 72 20 79 6f 75 20 73 68 6f 75 6c  racter you shoul
24700 64 20 70 72 65 66 69 78 20 74 68 65 20 66 69 6c  d prefix the fil
24710 65 6e 61 6d 65 20 77 69 74 68 20 61 20 70 61 74  ename with a pat
24720 68 6e 61 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a  hname such as.**
24730 20 22 2e 2f 22 20 74 6f 20 61 76 6f 69 64 20 61   "./" to avoid a
24740 6d 62 69 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20  mbiguity..**.** 
24750 5e 49 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65  ^If the filename
24760 20 69 73 20 61 6e 20 65 6d 70 74 79 20 73 74 72   is an empty str
24770 69 6e 67 2c 20 74 68 65 6e 20 61 20 70 72 69 76  ing, then a priv
24780 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79 0a 2a  ate, temporary.*
24790 2a 20 6f 6e 2d 64 69 73 6b 20 64 61 74 61 62 61  * on-disk databa
247a0 73 65 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74  se will be creat
247b0 65 64 2e 20 20 5e 54 68 69 73 20 70 72 69 76 61  ed.  ^This priva
247c0 74 65 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c  te database will
247d0 20 62 65 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63   be.** automatic
247e0 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 61 73 20  ally deleted as 
247f0 73 6f 6f 6e 20 61 73 20 74 68 65 20 64 61 74 61  soon as the data
24800 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
24810 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a  is closed..**.**
24820 20 5b 5b 55 52 49 20 66 69 6c 65 6e 61 6d 65 73   [[URI filenames
24830 20 69 6e 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e   in sqlite3_open
24840 28 29 5d 5d 20 3c 68 33 3e 55 52 49 20 46 69 6c  ()]] <h3>URI Fil
24850 65 6e 61 6d 65 73 3c 2f 68 33 3e 0a 2a 2a 0a 2a  enames</h3>.**.*
24860 2a 20 5e 49 66 20 5b 55 52 49 20 66 69 6c 65 6e  * ^If [URI filen
24870 61 6d 65 5d 20 69 6e 74 65 72 70 72 65 74 61 74  ame] interpretat
24880 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20  ion is enabled, 
24890 61 6e 64 20 74 68 65 20 66 69 6c 65 6e 61 6d 65  and the filename
248a0 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 62 65 67   argument.** beg
248b0 69 6e 73 20 77 69 74 68 20 22 66 69 6c 65 3a 22  ins with "file:"
248c0 2c 20 74 68 65 6e 20 74 68 65 20 66 69 6c 65 6e  , then the filen
248d0 61 6d 65 20 69 73 20 69 6e 74 65 72 70 72 65 74  ame is interpret
248e0 65 64 20 61 73 20 61 20 55 52 49 2e 20 5e 55 52  ed as a URI. ^UR
248f0 49 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 69 6e  I.** filename in
24900 74 65 72 70 72 65 74 61 74 69 6f 6e 20 69 73 20  terpretation is 
24910 65 6e 61 62 6c 65 64 20 69 66 20 74 68 65 20 5b  enabled if the [
24920 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49 5d  SQLITE_OPEN_URI]
24930 20 66 6c 61 67 20 69 73 0a 2a 2a 20 73 65 74 20   flag is.** set 
24940 69 6e 20 74 68 65 20 66 6f 75 72 74 68 20 61 72  in the fourth ar
24950 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65  gument to sqlite
24960 33 5f 6f 70 65 6e 5f 76 32 28 29 2c 20 6f 72 20  3_open_v2(), or 
24970 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 62 65 65  if it has.** bee
24980 6e 20 65 6e 61 62 6c 65 64 20 67 6c 6f 62 61 6c  n enabled global
24990 6c 79 20 75 73 69 6e 67 20 74 68 65 20 5b 53 51  ly using the [SQ
249a0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 55 52 49 5d  LITE_CONFIG_URI]
249b0 20 6f 70 74 69 6f 6e 20 77 69 74 68 20 74 68 65   option with the
249c0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  .** [sqlite3_con
249d0 66 69 67 28 29 5d 20 6d 65 74 68 6f 64 20 6f 72  fig()] method or
249e0 20 62 79 20 74 68 65 20 5b 53 51 4c 49 54 45 5f   by the [SQLITE_
249f0 55 53 45 5f 55 52 49 5d 20 63 6f 6d 70 69 6c 65  USE_URI] compile
24a00 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2e 0a 2a 2a  -time option..**
24a10 20 41 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65   As of SQLite ve
24a20 72 73 69 6f 6e 20 33 2e 37 2e 37 2c 20 55 52 49  rsion 3.7.7, URI
24a30 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 65 72 70   filename interp
24a40 72 65 74 61 74 69 6f 6e 20 69 73 20 74 75 72 6e  retation is turn
24a50 65 64 20 6f 66 66 0a 2a 2a 20 62 79 20 64 65 66  ed off.** by def
24a60 61 75 6c 74 2c 20 62 75 74 20 66 75 74 75 72 65  ault, but future
24a70 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c   releases of SQL
24a80 69 74 65 20 6d 69 67 68 74 20 65 6e 61 62 6c 65  ite might enable
24a90 20 55 52 49 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a   URI filename.**
24aa0 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20   interpretation 
24ab0 62 79 20 64 65 66 61 75 6c 74 2e 20 20 53 65 65  by default.  See
24ac0 20 22 5b 55 52 49 20 66 69 6c 65 6e 61 6d 65 73   "[URI filenames
24ad0 5d 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  ]" for additiona
24ae0 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  l.** information
24af0 2e 0a 2a 2a 0a 2a 2a 20 55 52 49 20 66 69 6c 65  ..**.** URI file
24b00 6e 61 6d 65 73 20 61 72 65 20 70 61 72 73 65 64  names are parsed
24b10 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 52 46   according to RF
24b20 43 20 33 39 38 36 2e 20 5e 49 66 20 74 68 65 20  C 3986. ^If the 
24b30 55 52 49 20 63 6f 6e 74 61 69 6e 73 20 61 6e 0a  URI contains an.
24b40 2a 2a 20 61 75 74 68 6f 72 69 74 79 2c 20 74 68  ** authority, th
24b50 65 6e 20 69 74 20 6d 75 73 74 20 62 65 20 65 69  en it must be ei
24b60 74 68 65 72 20 61 6e 20 65 6d 70 74 79 20 73 74  ther an empty st
24b70 72 69 6e 67 20 6f 72 20 74 68 65 20 73 74 72 69  ring or the stri
24b80 6e 67 20 0a 2a 2a 20 22 6c 6f 63 61 6c 68 6f 73  ng .** "localhos
24b90 74 22 2e 20 5e 49 66 20 74 68 65 20 61 75 74 68  t". ^If the auth
24ba0 6f 72 69 74 79 20 69 73 20 6e 6f 74 20 61 6e 20  ority is not an 
24bb0 65 6d 70 74 79 20 73 74 72 69 6e 67 20 6f 72 20  empty string or 
24bc0 22 6c 6f 63 61 6c 68 6f 73 74 22 2c 20 61 6e 20  "localhost", an 
24bd0 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20 72 65 74  .** error is ret
24be0 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c  urned to the cal
24bf0 6c 65 72 2e 20 5e 54 68 65 20 66 72 61 67 6d 65  ler. ^The fragme
24c00 6e 74 20 63 6f 6d 70 6f 6e 65 6e 74 20 6f 66 20  nt component of 
24c10 61 20 55 52 49 2c 20 69 66 20 0a 2a 2a 20 70 72  a URI, if .** pr
24c20 65 73 65 6e 74 2c 20 69 73 20 69 67 6e 6f 72 65  esent, is ignore
24c30 64 2e 0a 2a 2a 0a 2a 2a 20 5e 53 51 4c 69 74 65  d..**.** ^SQLite
24c40 20 75 73 65 73 20 74 68 65 20 70 61 74 68 20 63   uses the path c
24c50 6f 6d 70 6f 6e 65 6e 74 20 6f 66 20 74 68 65 20  omponent of the 
24c60 55 52 49 20 61 73 20 74 68 65 20 6e 61 6d 65 20  URI as the name 
24c70 6f 66 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65  of the disk file
24c80 0a 2a 2a 20 77 68 69 63 68 20 63 6f 6e 74 61 69  .** which contai
24c90 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ns the database.
24ca0 20 5e 49 66 20 74 68 65 20 70 61 74 68 20 62 65   ^If the path be
24cb0 67 69 6e 73 20 77 69 74 68 20 61 20 27 2f 27 20  gins with a '/' 
24cc0 63 68 61 72 61 63 74 65 72 2c 20 0a 2a 2a 20 74  character, .** t
24cd0 68 65 6e 20 69 74 20 69 73 20 69 6e 74 65 72 70  hen it is interp
24ce0 72 65 74 65 64 20 61 73 20 61 6e 20 61 62 73 6f  reted as an abso
24cf0 6c 75 74 65 20 70 61 74 68 2e 20 5e 49 66 20 74  lute path. ^If t
24d00 68 65 20 70 61 74 68 20 64 6f 65 73 20 6e 6f 74  he path does not
24d10 20 62 65 67 69 6e 20 0a 2a 2a 20 77 69 74 68 20   begin .** with 
24d20 61 20 27 2f 27 20 28 6d 65 61 6e 69 6e 67 20 74  a '/' (meaning t
24d30 68 61 74 20 74 68 65 20 61 75 74 68 6f 72 69 74  hat the authorit
24d40 79 20 73 65 63 74 69 6f 6e 20 69 73 20 6f 6d 69  y section is omi
24d50 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 55 52  tted from the UR
24d60 49 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 70  I).** then the p
24d70 61 74 68 20 69 73 20 69 6e 74 65 72 70 72 65 74  ath is interpret
24d80 65 64 20 61 73 20 61 20 72 65 6c 61 74 69 76 65  ed as a relative
24d90 20 70 61 74 68 2e 20 0a 2a 2a 20 5e 4f 6e 20 77   path. .** ^On w
24da0 69 6e 64 6f 77 73 2c 20 74 68 65 20 66 69 72 73  indows, the firs
24db0 74 20 63 6f 6d 70 6f 6e 65 6e 74 20 6f 66 20 61  t component of a
24dc0 6e 20 61 62 73 6f 6c 75 74 65 20 70 61 74 68 20  n absolute path 
24dd0 0a 2a 2a 20 69 73 20 61 20 64 72 69 76 65 20 73  .** is a drive s
24de0 70 65 63 69 66 69 63 61 74 69 6f 6e 20 28 65 2e  pecification (e.
24df0 67 2e 20 22 43 3a 22 29 2e 0a 2a 2a 0a 2a 2a 20  g. "C:")..**.** 
24e00 5b 5b 63 6f 72 65 20 55 52 49 20 71 75 65 72 79  [[core URI query
24e10 20 70 61 72 61 6d 65 74 65 72 73 5d 5d 0a 2a 2a   parameters]].**
24e20 20 54 68 65 20 71 75 65 72 79 20 63 6f 6d 70 6f   The query compo
24e30 6e 65 6e 74 20 6f 66 20 61 20 55 52 49 20 6d 61  nent of a URI ma
24e40 79 20 63 6f 6e 74 61 69 6e 20 70 61 72 61 6d 65  y contain parame
24e50 74 65 72 73 20 74 68 61 74 20 61 72 65 20 69 6e  ters that are in
24e60 74 65 72 70 72 65 74 65 64 0a 2a 2a 20 65 69 74  terpreted.** eit
24e70 68 65 72 20 62 79 20 53 51 4c 69 74 65 20 69 74  her by SQLite it
24e80 73 65 6c 66 2c 20 6f 72 20 62 79 20 61 20 5b 56  self, or by a [V
24e90 46 53 20 7c 20 63 75 73 74 6f 6d 20 56 46 53 20  FS | custom VFS 
24ea0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 5d 2e  implementation].
24eb0 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 74 65 72  .** SQLite inter
24ec0 70 72 65 74 73 20 74 68 65 20 66 6f 6c 6c 6f 77  prets the follow
24ed0 69 6e 67 20 74 68 72 65 65 20 71 75 65 72 79 20  ing three query 
24ee0 70 61 72 61 6d 65 74 65 72 73 3a 0a 2a 2a 0a 2a  parameters:.**.*
24ef0 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e  * <ul>.**   <li>
24f00 20 3c 62 3e 76 66 73 3c 2f 62 3e 3a 20 5e 54 68   <b>vfs</b>: ^Th
24f10 65 20 22 76 66 73 22 20 70 61 72 61 6d 65 74 65  e "vfs" paramete
24f20 72 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f  r may be used to
24f30 20 73 70 65 63 69 66 79 20 74 68 65 20 6e 61 6d   specify the nam
24f40 65 20 6f 66 0a 2a 2a 20 20 20 20 20 61 20 56 46  e of.**     a VF
24f50 53 20 6f 62 6a 65 63 74 20 74 68 61 74 20 70 72  S object that pr
24f60 6f 76 69 64 65 73 20 74 68 65 20 6f 70 65 72 61  ovides the opera
24f70 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65  ting system inte
24f80 72 66 61 63 65 20 74 68 61 74 20 73 68 6f 75 6c  rface that shoul
24f90 64 0a 2a 2a 20 20 20 20 20 62 65 20 75 73 65 64  d.**     be used
24fa0 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64   to access the d
24fb0 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20  atabase file on 
24fc0 64 69 73 6b 2e 20 5e 49 66 20 74 68 69 73 20 6f  disk. ^If this o
24fd0 70 74 69 6f 6e 20 69 73 20 73 65 74 20 74 6f 0a  ption is set to.
24fe0 2a 2a 20 20 20 20 20 61 6e 20 65 6d 70 74 79 20  **     an empty 
24ff0 73 74 72 69 6e 67 20 74 68 65 20 64 65 66 61 75  string the defau
25000 6c 74 20 56 46 53 20 6f 62 6a 65 63 74 20 69 73  lt VFS object is
25010 20 75 73 65 64 2e 20 5e 53 70 65 63 69 66 79 69   used. ^Specifyi
25020 6e 67 20 61 6e 20 75 6e 6b 6e 6f 77 6e 0a 2a 2a  ng an unknown.**
25030 20 20 20 20 20 56 46 53 20 69 73 20 61 6e 20 65       VFS is an e
25040 72 72 6f 72 2e 20 5e 49 66 20 73 71 6c 69 74 65  rror. ^If sqlite
25050 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 75  3_open_v2() is u
25060 73 65 64 20 61 6e 64 20 74 68 65 20 76 66 73 20  sed and the vfs 
25070 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20  option is.**    
25080 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 74   present, then t
25090 68 65 20 56 46 53 20 73 70 65 63 69 66 69 65 64  he VFS specified
250a0 20 62 79 20 74 68 65 20 6f 70 74 69 6f 6e 20 74   by the option t
250b0 61 6b 65 73 20 70 72 65 63 65 64 65 6e 63 65 20  akes precedence 
250c0 6f 76 65 72 0a 2a 2a 20 20 20 20 20 74 68 65 20  over.**     the 
250d0 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20  value passed as 
250e0 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d  the fourth param
250f0 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f  eter to sqlite3_
25100 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a 2a 2a  open_v2()..**.**
25110 20 20 20 3c 6c 69 3e 20 3c 62 3e 6d 6f 64 65 3c     <li> <b>mode<
25120 2f 62 3e 3a 20 5e 28 54 68 65 20 6d 6f 64 65 20  /b>: ^(The mode 
25130 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 62 65  parameter may be
25140 20 73 65 74 20 74 6f 20 65 69 74 68 65 72 20 22   set to either "
25150 72 6f 22 2c 20 22 72 77 22 2c 0a 2a 2a 20 20 20  ro", "rw",.**   
25160 20 20 22 72 77 63 22 2c 20 6f 72 20 22 6d 65 6d    "rwc", or "mem
25170 6f 72 79 22 2e 20 41 74 74 65 6d 70 74 69 6e 67  ory". Attempting
25180 20 74 6f 20 73 65 74 20 69 74 20 74 6f 20 61 6e   to set it to an
25190 79 20 6f 74 68 65 72 20 76 61 6c 75 65 20 69 73  y other value is
251a0 0a 2a 2a 20 20 20 20 20 61 6e 20 65 72 72 6f 72  .**     an error
251b0 29 5e 2e 20 0a 2a 2a 20 20 20 20 20 5e 49 66 20  )^. .**     ^If 
251c0 22 72 6f 22 20 69 73 20 73 70 65 63 69 66 69 65  "ro" is specifie
251d0 64 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61  d, then the data
251e0 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66  base is opened f
251f0 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 0a 2a 2a  or read-only .**
25200 20 20 20 20 20 61 63 63 65 73 73 2c 20 6a 75 73       access, jus
25210 74 20 61 73 20 69 66 20 74 68 65 20 5b 53 51 4c  t as if the [SQL
25220 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
25230 59 5d 20 66 6c 61 67 20 68 61 64 20 62 65 65 6e  Y] flag had been
25240 20 73 65 74 20 69 6e 20 74 68 65 20 0a 2a 2a 20   set in the .** 
25250 20 20 20 20 74 68 69 72 64 20 61 72 67 75 6d 65      third argume
25260 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70  nt to sqlite3_op
25270 65 6e 5f 76 32 28 29 2e 20 5e 49 66 20 74 68 65  en_v2(). ^If the
25280 20 6d 6f 64 65 20 6f 70 74 69 6f 6e 20 69 73 20   mode option is 
25290 73 65 74 20 74 6f 20 0a 2a 2a 20 20 20 20 20 22  set to .**     "
252a0 72 77 22 2c 20 74 68 65 6e 20 74 68 65 20 64 61  rw", then the da
252b0 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64  tabase is opened
252c0 20 66 6f 72 20 72 65 61 64 2d 77 72 69 74 65 20   for read-write 
252d0 28 62 75 74 20 6e 6f 74 20 63 72 65 61 74 65 29  (but not create)
252e0 20 0a 2a 2a 20 20 20 20 20 61 63 63 65 73 73 2c   .**     access,
252f0 20 61 73 20 69 66 20 53 51 4c 49 54 45 5f 4f 50   as if SQLITE_OP
25300 45 4e 5f 52 45 41 44 57 52 49 54 45 20 28 62 75  EN_READWRITE (bu
25310 74 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4f 50 45  t not SQLITE_OPE
25320 4e 5f 43 52 45 41 54 45 29 20 68 61 64 20 0a 2a  N_CREATE) had .*
25330 2a 20 20 20 20 20 62 65 65 6e 20 73 65 74 2e 20  *     been set. 
25340 5e 56 61 6c 75 65 20 22 72 77 63 22 20 69 73 20  ^Value "rwc" is 
25350 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 73 65  equivalent to se
25360 74 74 69 6e 67 20 62 6f 74 68 20 0a 2a 2a 20 20  tting both .**  
25370 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52     SQLITE_OPEN_R
25380 45 41 44 57 52 49 54 45 20 61 6e 64 20 53 51 4c  EADWRITE and SQL
25390 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 2e  ITE_OPEN_CREATE.
253a0 20 20 5e 49 66 20 74 68 65 20 6d 6f 64 65 20 6f    ^If the mode o
253b0 70 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 20  ption is.**     
253c0 73 65 74 20 74 6f 20 22 6d 65 6d 6f 72 79 22 20  set to "memory" 
253d0 74 68 65 6e 20 61 20 70 75 72 65 20 5b 69 6e 2d  then a pure [in-
253e0 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 5d  memory database]
253f0 20 74 68 61 74 20 6e 65 76 65 72 20 72 65 61 64   that never read
25400 73 0a 2a 2a 20 20 20 20 20 6f 72 20 77 72 69 74  s.**     or writ
25410 65 73 20 66 72 6f 6d 20 64 69 73 6b 20 69 73 20  es from disk is 
25420 75 73 65 64 2e 20 5e 49 74 20 69 73 20 61 6e 20  used. ^It is an 
25430 65 72 72 6f 72 20 74 6f 20 73 70 65 63 69 66 79  error to specify
25440 20 61 20 76 61 6c 75 65 20 66 6f 72 0a 2a 2a 20   a value for.** 
25450 20 20 20 20 74 68 65 20 6d 6f 64 65 20 70 61 72      the mode par
25460 61 6d 65 74 65 72 20 74 68 61 74 20 69 73 20 6c  ameter that is l
25470 65 73 73 20 72 65 73 74 72 69 63 74 69 76 65 20  ess restrictive 
25480 74 68 61 6e 20 74 68 61 74 20 73 70 65 63 69 66  than that specif
25490 69 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 74 68  ied by.**     th
254a0 65 20 66 6c 61 67 73 20 70 61 73 73 65 64 20 69  e flags passed i
254b0 6e 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61  n the third para
254c0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33  meter to sqlite3
254d0 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a 2a  _open_v2()..**.*
254e0 2a 20 20 20 3c 6c 69 3e 20 3c 62 3e 63 61 63 68  *   <li> <b>cach
254f0 65 3c 2f 62 3e 3a 20 5e 54 68 65 20 63 61 63 68  e</b>: ^The cach
25500 65 20 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20  e parameter may 
25510 62 65 20 73 65 74 20 74 6f 20 65 69 74 68 65 72  be set to either
25520 20 22 73 68 61 72 65 64 22 20 6f 72 0a 2a 2a 20   "shared" or.** 
25530 20 20 20 20 22 70 72 69 76 61 74 65 22 2e 20 5e      "private". ^
25540 53 65 74 74 69 6e 67 20 69 74 20 74 6f 20 22 73  Setting it to "s
25550 68 61 72 65 64 22 20 69 73 20 65 71 75 69 76 61  hared" is equiva
25560 6c 65 6e 74 20 74 6f 20 73 65 74 74 69 6e 67 20  lent to setting 
25570 74 68 65 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54  the.**     SQLIT
25580 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43  E_OPEN_SHAREDCAC
25590 48 45 20 62 69 74 20 69 6e 20 74 68 65 20 66 6c  HE bit in the fl
255a0 61 67 73 20 61 72 67 75 6d 65 6e 74 20 70 61 73  ags argument pas
255b0 73 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 73 71  sed to.**     sq
255c0 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e  lite3_open_v2().
255d0 20 5e 53 65 74 74 69 6e 67 20 74 68 65 20 63 61   ^Setting the ca
255e0 63 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f  che parameter to
255f0 20 22 70 72 69 76 61 74 65 22 20 69 73 20 0a 2a   "private" is .*
25600 2a 20 20 20 20 20 65 71 75 69 76 61 6c 65 6e 74  *     equivalent
25610 20 74 6f 20 73 65 74 74 69 6e 67 20 74 68 65 20   to setting the 
25620 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56  SQLITE_OPEN_PRIV
25630 41 54 45 43 41 43 48 45 20 62 69 74 2e 0a 2a 2a  ATECACHE bit..**
25640 20 20 20 20 20 5e 49 66 20 73 71 6c 69 74 65 33       ^If sqlite3
25650 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 75 73  _open_v2() is us
25660 65 64 20 61 6e 64 20 74 68 65 20 22 63 61 63 68  ed and the "cach
25670 65 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  e" parameter is 
25680 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 20 20  present in.**   
25690 20 20 61 20 55 52 49 20 66 69 6c 65 6e 61 6d 65    a URI filename
256a0 2c 20 69 74 73 20 76 61 6c 75 65 20 6f 76 65 72  , its value over
256b0 72 69 64 65 73 20 61 6e 79 20 62 65 68 61 76 69  rides any behavi
256c0 6f 75 72 20 72 65 71 75 65 73 74 65 64 20 62 79  our requested by
256d0 20 73 65 74 74 69 6e 67 0a 2a 2a 20 20 20 20 20   setting.**     
256e0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56  SQLITE_OPEN_PRIV
256f0 41 54 45 43 41 43 48 45 20 6f 72 20 53 51 4c 49  ATECACHE or SQLI
25700 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41  TE_OPEN_SHAREDCA
25710 43 48 45 20 66 6c 61 67 2e 0a 2a 2a 20 3c 2f 75  CHE flag..** </u
25720 6c 3e 0a 2a 2a 0a 2a 2a 20 5e 53 70 65 63 69 66  l>.**.** ^Specif
25730 79 69 6e 67 20 61 6e 20 75 6e 6b 6e 6f 77 6e 20  ying an unknown 
25740 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68 65  parameter in the
25750 20 71 75 65 72 79 20 63 6f 6d 70 6f 6e 65 6e 74   query component
25760 20 6f 66 20 61 20 55 52 49 20 69 73 20 6e 6f 74   of a URI is not
25770 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 2e 20 20 46   an.** error.  F
25780 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f  uture versions o
25790 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 20 75  f SQLite might u
257a0 6e 64 65 72 73 74 61 6e 64 20 61 64 64 69 74 69  nderstand additi
257b0 6f 6e 61 6c 20 71 75 65 72 79 0a 2a 2a 20 70 61  onal query.** pa
257c0 72 61 6d 65 74 65 72 73 2e 20 20 53 65 65 20 22  rameters.  See "
257d0 5b 71 75 65 72 79 20 70 61 72 61 6d 65 74 65 72  [query parameter
257e0 73 20 77 69 74 68 20 73 70 65 63 69 61 6c 20 6d  s with special m
257f0 65 61 6e 69 6e 67 20 74 6f 20 53 51 4c 69 74 65  eaning to SQLite
25800 5d 22 20 66 6f 72 0a 2a 2a 20 61 64 64 69 74 69  ]" for.** additi
25810 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
25820 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 55 52 49 20 66 69  ..**.** [[URI fi
25830 6c 65 6e 61 6d 65 20 65 78 61 6d 70 6c 65 73 5d  lename examples]
25840 5d 20 3c 68 33 3e 55 52 49 20 66 69 6c 65 6e 61  ] <h3>URI filena
25850 6d 65 20 65 78 61 6d 70 6c 65 73 3c 2f 68 33 3e  me examples</h3>
25860 0a 2a 2a 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f  .**.** <table bo
25870 72 64 65 72 3d 22 31 22 20 61 6c 69 67 6e 3d 63  rder="1" align=c
25880 65 6e 74 65 72 20 63 65 6c 6c 70 61 64 64 69 6e  enter cellpaddin
25890 67 3d 35 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e  g=5>.** <tr><th>
258a0 20 55 52 49 20 66 69 6c 65 6e 61 6d 65 73 20 3c   URI filenames <
258b0 74 68 3e 20 52 65 73 75 6c 74 73 0a 2a 2a 20 3c  th> Results.** <
258c0 74 72 3e 3c 74 64 3e 20 66 69 6c 65 3a 64 61 74  tr><td> file:dat
258d0 61 2e 64 62 20 3c 74 64 3e 20 0a 2a 2a 20 20 20  a.db <td> .**   
258e0 20 20 20 20 20 20 20 4f 70 65 6e 20 74 68 65 20         Open the 
258f0 66 69 6c 65 20 22 64 61 74 61 2e 64 62 22 20 69  file "data.db" i
25900 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 69  n the current di
25910 72 65 63 74 6f 72 79 2e 0a 2a 2a 20 3c 74 72 3e  rectory..** <tr>
25920 3c 74 64 3e 20 66 69 6c 65 3a 2f 68 6f 6d 65 2f  <td> file:/home/
25930 66 72 65 64 2f 64 61 74 61 2e 64 62 3c 62 72 3e  fred/data.db<br>
25940 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c  .**          fil
25950 65 3a 2f 2f 2f 68 6f 6d 65 2f 66 72 65 64 2f 64  e:///home/fred/d
25960 61 74 61 2e 64 62 20 3c 62 72 3e 20 0a 2a 2a 20  ata.db <br> .** 
25970 20 20 20 20 20 20 20 20 20 66 69 6c 65 3a 2f 2f           file://
25980 6c 6f 63 61 6c 68 6f 73 74 2f 68 6f 6d 65 2f 66  localhost/home/f
25990 72 65 64 2f 64 61 74 61 2e 64 62 20 3c 62 72 3e  red/data.db <br>
259a0 20 3c 74 64 3e 20 0a 2a 2a 20 20 20 20 20 20 20   <td> .**       
259b0 20 20 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61     Open the data
259c0 62 61 73 65 20 66 69 6c 65 20 22 2f 68 6f 6d 65  base file "/home
259d0 2f 66 72 65 64 2f 64 61 74 61 2e 64 62 22 2e 0a  /fred/data.db"..
259e0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 66 69 6c 65  ** <tr><td> file
259f0 3a 2f 2f 64 61 72 6b 73 74 61 72 2f 68 6f 6d 65  ://darkstar/home
25a00 2f 66 72 65 64 2f 64 61 74 61 2e 64 62 20 3c 74  /fred/data.db <t
25a10 64 3e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  d> .**          
25a20 41 6e 20 65 72 72 6f 72 2e 20 22 64 61 72 6b 73  An error. "darks
25a30 74 61 72 22 20 69 73 20 6e 6f 74 20 61 20 72 65  tar" is not a re
25a40 63 6f 67 6e 69 7a 65 64 20 61 75 74 68 6f 72 69  cognized authori
25a50 74 79 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 20 73  ty..** <tr><td s
25a60 74 79 6c 65 3d 22 77 68 69 74 65 2d 73 70 61 63  tyle="white-spac
25a70 65 3a 6e 6f 77 72 61 70 22 3e 20 0a 2a 2a 20 20  e:nowrap"> .**  
25a80 20 20 20 20 20 20 20 20 66 69 6c 65 3a 2f 2f 2f          file:///
25a90 43 3a 2f 44 6f 63 75 6d 65 6e 74 73 25 32 30 61  C:/Documents%20a
25aa0 6e 64 25 32 30 53 65 74 74 69 6e 67 73 2f 66 72  nd%20Settings/fr
25ab0 65 64 2f 44 65 73 6b 74 6f 70 2f 64 61 74 61 2e  ed/Desktop/data.
25ac0 64 62 0a 2a 2a 20 20 20 20 20 3c 74 64 3e 20 57  db.**     <td> W
25ad0 69 6e 64 6f 77 73 20 6f 6e 6c 79 3a 20 4f 70 65  indows only: Ope
25ae0 6e 20 74 68 65 20 66 69 6c 65 20 22 64 61 74 61  n the file "data
25af0 2e 64 62 22 20 6f 6e 20 66 72 65 64 27 73 20 64  .db" on fred's d
25b00 65 73 6b 74 6f 70 20 6f 6e 20 64 72 69 76 65 0a  esktop on drive.
25b10 2a 2a 20 20 20 20 20 20 20 20 20 20 43 3a 2e 20  **          C:. 
25b20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 25 32  Note that the %2
25b30 30 20 65 73 63 61 70 69 6e 67 20 69 6e 20 74 68  0 escaping in th
25b40 69 73 20 65 78 61 6d 70 6c 65 20 69 73 20 6e 6f  is example is no
25b50 74 20 73 74 72 69 63 74 6c 79 20 0a 2a 2a 20 20  t strictly .**  
25b60 20 20 20 20 20 20 20 20 6e 65 63 65 73 73 61 72          necessar
25b70 79 20 2d 20 73 70 61 63 65 20 63 68 61 72 61 63  y - space charac
25b80 74 65 72 73 20 63 61 6e 20 62 65 20 75 73 65 64  ters can be used
25b90 20 6c 69 74 65 72 61 6c 6c 79 0a 2a 2a 20 20 20   literally.**   
25ba0 20 20 20 20 20 20 20 69 6e 20 55 52 49 20 66 69         in URI fi
25bb0 6c 65 6e 61 6d 65 73 2e 0a 2a 2a 20 3c 74 72 3e  lenames..** <tr>
25bc0 3c 74 64 3e 20 66 69 6c 65 3a 64 61 74 61 2e 64  <td> file:data.d
25bd0 62 3f 6d 6f 64 65 3d 72 6f 26 63 61 63 68 65 3d  b?mode=ro&cache=
25be0 70 72 69 76 61 74 65 20 3c 74 64 3e 20 0a 2a 2a  private <td> .**
25bf0 20 20 20 20 20 20 20 20 20 20 4f 70 65 6e 20 66            Open f
25c00 69 6c 65 20 22 64 61 74 61 2e 64 62 22 20 69 6e  ile "data.db" in
25c10 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 69 72   the current dir
25c20 65 63 74 6f 72 79 20 66 6f 72 20 72 65 61 64 2d  ectory for read-
25c30 6f 6e 6c 79 20 61 63 63 65 73 73 2e 0a 2a 2a 20  only access..** 
25c40 20 20 20 20 20 20 20 20 20 52 65 67 61 72 64 6c           Regardl
25c50 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f  ess of whether o
25c60 72 20 6e 6f 74 20 73 68 61 72 65 64 2d 63 61 63  r not shared-cac
25c70 68 65 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 6c  he mode is enabl
25c80 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20  ed by.**        
25c90 20 20 64 65 66 61 75 6c 74 2c 20 75 73 65 20 61    default, use a
25ca0 20 70 72 69 76 61 74 65 20 63 61 63 68 65 2e 0a   private cache..
25cb0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 66 69 6c 65  ** <tr><td> file
25cc0 3a 2f 68 6f 6d 65 2f 66 72 65 64 2f 64 61 74 61  :/home/fred/data
25cd0 2e 64 62 3f 76 66 73 3d 75 6e 69 78 2d 6e 6f 6c  .db?vfs=unix-nol
25ce0 6f 63 6b 20 3c 74 64 3e 0a 2a 2a 20 20 20 20 20  ock <td>.**     
25cf0 20 20 20 20 20 4f 70 65 6e 20 66 69 6c 65 20 22       Open file "
25d00 2f 68 6f 6d 65 2f 66 72 65 64 2f 64 61 74 61 2e  /home/fred/data.
25d10 64 62 22 2e 20 55 73 65 20 74 68 65 20 73 70 65  db". Use the spe
25d20 63 69 61 6c 20 56 46 53 20 22 75 6e 69 78 2d 6e  cial VFS "unix-n
25d30 6f 6c 6f 63 6b 22 2e 0a 2a 2a 20 3c 74 72 3e 3c  olock"..** <tr><
25d40 74 64 3e 20 66 69 6c 65 3a 64 61 74 61 2e 64 62  td> file:data.db
25d50 3f 6d 6f 64 65 3d 72 65 61 64 6f 6e 6c 79 20 3c  ?mode=readonly <
25d60 74 64 3e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  td> .**         
25d70 20 41 6e 20 65 72 72 6f 72 2e 20 22 72 65 61 64   An error. "read
25d80 6f 6e 6c 79 22 20 69 73 20 6e 6f 74 20 61 20 76  only" is not a v
25d90 61 6c 69 64 20 6f 70 74 69 6f 6e 20 66 6f 72 20  alid option for 
25da0 74 68 65 20 22 6d 6f 64 65 22 20 70 61 72 61 6d  the "mode" param
25db0 65 74 65 72 2e 0a 2a 2a 20 3c 2f 74 61 62 6c 65  eter..** </table
25dc0 3e 0a 2a 2a 0a 2a 2a 20 5e 55 52 49 20 68 65 78  >.**.** ^URI hex
25dd0 61 64 65 63 69 6d 61 6c 20 65 73 63 61 70 65 20  adecimal escape 
25de0 73 65 71 75 65 6e 63 65 73 20 28 25 48 48 29 20  sequences (%HH) 
25df0 61 72 65 20 73 75 70 70 6f 72 74 65 64 20 77 69  are supported wi
25e00 74 68 69 6e 20 74 68 65 20 70 61 74 68 20 61 6e  thin the path an
25e10 64 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6d 70 6f  d.** query compo
25e20 6e 65 6e 74 73 20 6f 66 20 61 20 55 52 49 2e 20  nents of a URI. 
25e30 41 20 68 65 78 61 64 65 63 69 6d 61 6c 20 65 73  A hexadecimal es
25e40 63 61 70 65 20 73 65 71 75 65 6e 63 65 20 63 6f  cape sequence co
25e50 6e 73 69 73 74 73 20 6f 66 20 61 0a 2a 2a 20 70  nsists of a.** p
25e60 65 72 63 65 6e 74 20 73 69 67 6e 20 2d 20 22 25  ercent sign - "%
25e70 22 20 2d 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  " - followed by 
25e80 65 78 61 63 74 6c 79 20 74 77 6f 20 68 65 78 61  exactly two hexa
25e90 64 65 63 69 6d 61 6c 20 64 69 67 69 74 73 20 0a  decimal digits .
25ea0 2a 2a 20 73 70 65 63 69 66 79 69 6e 67 20 61 6e  ** specifying an
25eb0 20 6f 63 74 65 74 20 76 61 6c 75 65 2e 20 5e 42   octet value. ^B
25ec0 65 66 6f 72 65 20 74 68 65 20 70 61 74 68 20 6f  efore the path o
25ed0 72 20 71 75 65 72 79 20 63 6f 6d 70 6f 6e 65 6e  r query componen
25ee0 74 73 20 6f 66 20 61 0a 2a 2a 20 55 52 49 20 66  ts of a.** URI f
25ef0 69 6c 65 6e 61 6d 65 20 61 72 65 20 69 6e 74 65  ilename are inte
25f00 72 70 72 65 74 65 64 2c 20 74 68 65 79 20 61 72  rpreted, they ar
25f10 65 20 65 6e 63 6f 64 65 64 20 75 73 69 6e 67 20  e encoded using 
25f20 55 54 46 2d 38 20 61 6e 64 20 61 6c 6c 20 0a 2a  UTF-8 and all .*
25f30 2a 20 68 65 78 61 64 65 63 69 6d 61 6c 20 65 73  * hexadecimal es
25f40 63 61 70 65 20 73 65 71 75 65 6e 63 65 73 20 72  cape sequences r
25f50 65 70 6c 61 63 65 64 20 62 79 20 61 20 73 69 6e  eplaced by a sin
25f60 67 6c 65 20 62 79 74 65 20 63 6f 6e 74 61 69 6e  gle byte contain
25f70 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65  ing the.** corre
25f80 73 70 6f 6e 64 69 6e 67 20 6f 63 74 65 74 2e 20  sponding octet. 
25f90 49 66 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  If this process 
25fa0 67 65 6e 65 72 61 74 65 73 20 61 6e 20 69 6e 76  generates an inv
25fb0 61 6c 69 64 20 55 54 46 2d 38 20 65 6e 63 6f 64  alid UTF-8 encod
25fc0 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75  ing,.** the resu
25fd0 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65  lts are undefine
25fe0 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 65  d..**.** <b>Note
25ff0 20 74 6f 20 57 69 6e 64 6f 77 73 20 75 73 65 72   to Windows user
26000 73 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e 63 6f  s:</b>  The enco
26010 64 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68  ding used for th
26020 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d  e filename argum
26030 65 6e 74 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65  ent.** of sqlite
26040 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c  3_open() and sql
26050 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 6d  ite3_open_v2() m
26060 75 73 74 20 62 65 20 55 54 46 2d 38 2c 20 6e 6f  ust be UTF-8, no
26070 74 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 63 6f  t whatever.** co
26080 64 65 70 61 67 65 20 69 73 20 63 75 72 72 65 6e  depage is curren
26090 74 6c 79 20 64 65 66 69 6e 65 64 2e 20 20 46 69  tly defined.  Fi
260a0 6c 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e 69  lenames containi
260b0 6e 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e 61 6c  ng international
260c0 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 6d  .** characters m
260d0 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64  ust be converted
260e0 20 74 6f 20 55 54 46 2d 38 20 70 72 69 6f 72 20   to UTF-8 prior 
260f0 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65 6d 20  to passing them 
26100 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  into.** sqlite3_
26110 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 65  open() or sqlite
26120 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a  3_open_v2()..**.
26130 2a 2a 20 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69  ** <b>Note to Wi
26140 6e 64 6f 77 73 20 52 75 6e 74 69 6d 65 20 75 73  ndows Runtime us
26150 65 72 73 3a 3c 2f 62 3e 20 20 54 68 65 20 74 65  ers:</b>  The te
26160 6d 70 6f 72 61 72 79 20 64 69 72 65 63 74 6f 72  mporary director
26170 79 20 6d 75 73 74 20 62 65 20 73 65 74 0a 2a 2a  y must be set.**
26180 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e   prior to callin
26190 67 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  g sqlite3_open()
261a0 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e   or sqlite3_open
261b0 5f 76 32 28 29 2e 20 20 4f 74 68 65 72 77 69 73  _v2().  Otherwis
261c0 65 2c 20 76 61 72 69 6f 75 73 0a 2a 2a 20 66 65  e, various.** fe
261d0 61 74 75 72 65 73 20 74 68 61 74 20 72 65 71 75  atures that requ
261e0 69 72 65 20 74 68 65 20 75 73 65 20 6f 66 20 74  ire the use of t
261f0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 6d  emporary files m
26200 61 79 20 66 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 53  ay fail..**.** S
26210 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65  ee also: [sqlite
26220 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
26230 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
26240 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  int sqlite3_open
26250 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
26260 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44  filename,   /* D
26270 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65  atabase filename
26280 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71   (UTF-8) */.  sq
26290 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 20 20 20  lite3 **ppDb    
262a0 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51        /* OUT: SQ
262b0 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a  Lite db handle *
262c0 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  /.);.SQLITE_API 
262d0 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  int sqlite3_open
262e0 31 36 28 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  16(.  const void
262f0 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a   *filename,   /*
26300 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61   Database filena
26310 6d 65 20 28 55 54 46 2d 31 36 29 20 2a 2f 0a 20  me (UTF-16) */. 
26320 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20   sqlite3 **ppDb 
26330 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
26340 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c   SQLite db handl
26350 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  e */.);.SQLITE_A
26360 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
26370 70 65 6e 5f 76 32 28 0a 20 20 63 6f 6e 73 74 20  pen_v2(.  const 
26380 63 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20  char *filename, 
26390 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69    /* Database fi
263a0 6c 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a  lename (UTF-8) *
263b0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70  /.  sqlite3 **pp
263c0 44 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  Db,         /* O
263d0 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61  UT: SQLite db ha
263e0 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  ndle */.  int fl
263f0 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
26400 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20    /* Flags */.  
26410 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73  const char *zVfs
26420 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
26430 6f 66 20 56 46 53 20 6d 6f 64 75 6c 65 20 74 6f  of VFS module to
26440 20 75 73 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a   use */.);../*.*
26450 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61  * CAPI3REF: Obta
26460 69 6e 20 56 61 6c 75 65 73 20 46 6f 72 20 55 52  in Values For UR
26470 49 20 50 61 72 61 6d 65 74 65 72 73 0a 2a 2a 0a  I Parameters.**.
26480 2a 2a 20 54 68 65 73 65 20 61 72 65 20 75 74 69  ** These are uti
26490 6c 69 74 79 20 72 6f 75 74 69 6e 65 73 2c 20 75  lity routines, u
264a0 73 65 66 75 6c 20 74 6f 20 56 46 53 20 69 6d 70  seful to VFS imp
264b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2c 20 74 68  lementations, th
264c0 61 74 20 63 68 65 63 6b 0a 2a 2a 20 74 6f 20 73  at check.** to s
264d0 65 65 20 69 66 20 61 20 64 61 74 61 62 61 73 65  ee if a database
264e0 20 66 69 6c 65 20 77 61 73 20 61 20 55 52 49 20   file was a URI 
264f0 74 68 61 74 20 63 6f 6e 74 61 69 6e 65 64 20 61  that contained a
26500 20 73 70 65 63 69 66 69 63 20 71 75 65 72 79 20   specific query 
26510 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2c 20 61  .** parameter, a
26520 6e 64 20 69 66 20 73 6f 20 6f 62 74 61 69 6e 73  nd if so obtains
26530 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
26540 61 74 20 71 75 65 72 79 20 70 61 72 61 6d 65 74  at query paramet
26550 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 46 20 69  er..**.** If F i
26560 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  s the database f
26570 69 6c 65 6e 61 6d 65 20 70 6f 69 6e 74 65 72 20  ilename pointer 
26580 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20  passed into the 
26590 78 4f 70 65 6e 28 29 20 6d 65 74 68 6f 64 20 6f  xOpen() method o
265a0 66 20 0a 2a 2a 20 61 20 56 46 53 20 69 6d 70 6c  f .** a VFS impl
265b0 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68 65 6e 20  ementation when 
265c0 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  the flags parame
265d0 74 65 72 20 74 6f 20 78 4f 70 65 6e 28 29 20 68  ter to xOpen() h
265e0 61 73 20 6f 6e 65 20 6f 72 20 0a 2a 2a 20 6d 6f  as one or .** mo
265f0 72 65 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54  re of the [SQLIT
26600 45 5f 4f 50 45 4e 5f 55 52 49 5d 20 6f 72 20 5b  E_OPEN_URI] or [
26610 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
26620 5f 44 42 5d 20 62 69 74 73 20 73 65 74 20 61 6e  _DB] bits set an
26630 64 0a 2a 2a 20 50 20 69 73 20 74 68 65 20 6e 61  d.** P is the na
26640 6d 65 20 6f 66 20 74 68 65 20 71 75 65 72 79 20  me of the query 
26650 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 0a  parameter, then.
26660 2a 2a 20 73 71 6c 69 74 65 33 5f 75 72 69 5f 70  ** sqlite3_uri_p
26670 61 72 61 6d 65 74 65 72 28 46 2c 50 29 20 72 65  arameter(F,P) re
26680 74 75 72 6e 73 20 74 68 65 20 76 61 6c 75 65 20  turns the value 
26690 6f 66 20 74 68 65 20 50 0a 2a 2a 20 70 61 72 61  of the P.** para
266a0 6d 65 74 65 72 20 69 66 20 69 74 20 65 78 69 73  meter if it exis
266b0 74 73 20 6f 72 20 61 20 4e 55 4c 4c 20 70 6f 69  ts or a NULL poi
266c0 6e 74 65 72 20 69 66 20 50 20 64 6f 65 73 20 6e  nter if P does n
266d0 6f 74 20 61 70 70 65 61 72 20 61 73 20 61 20 0a  ot appear as a .
266e0 2a 2a 20 71 75 65 72 79 20 70 61 72 61 6d 65 74  ** query paramet
266f0 65 72 20 6f 6e 20 46 2e 20 20 49 66 20 50 20 69  er on F.  If P i
26700 73 20 61 20 71 75 65 72 79 20 70 61 72 61 6d 65  s a query parame
26710 74 65 72 20 6f 66 20 46 0a 2a 2a 20 68 61 73 20  ter of F.** has 
26720 6e 6f 20 65 78 70 6c 69 63 69 74 20 76 61 6c 75  no explicit valu
26730 65 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f  e, then sqlite3_
26740 75 72 69 5f 70 61 72 61 6d 65 74 65 72 28 46 2c  uri_parameter(F,
26750 50 29 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 20  P) returns.** a 
26760 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 6d  pointer to an em
26770 70 74 79 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a  pty string..**.*
26780 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 75 72  * The sqlite3_ur
26790 69 5f 62 6f 6f 6c 65 61 6e 28 46 2c 50 2c 42 29  i_boolean(F,P,B)
267a0 20 72 6f 75 74 69 6e 65 20 61 73 73 75 6d 65 73   routine assumes
267b0 20 74 68 61 74 20 50 20 69 73 20 61 20 62 6f 6f   that P is a boo
267c0 6c 65 61 6e 0a 2a 2a 20 70 61 72 61 6d 65 74 65  lean.** paramete
267d0 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 72  r and returns tr
267e0 75 65 20 28 31 29 20 6f 72 20 66 61 6c 73 65 20  ue (1) or false 
267f0 28 30 29 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  (0) according to
26800 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66   the value.** of
26810 20 50 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33   P.  The sqlite3
26820 5f 75 72 69 5f 62 6f 6f 6c 65 61 6e 28 46 2c 50  _uri_boolean(F,P
26830 2c 42 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75  ,B) routine retu
26840 72 6e 73 20 74 72 75 65 20 28 31 29 20 69 66 20  rns true (1) if 
26850 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20  the.** value of 
26860 71 75 65 72 79 20 70 61 72 61 6d 65 74 65 72 20  query parameter 
26870 50 20 69 73 20 6f 6e 65 20 6f 66 20 22 79 65 73  P is one of "yes
26880 22 2c 20 22 74 72 75 65 22 2c 20 6f 72 20 22 6f  ", "true", or "o
26890 6e 22 20 69 6e 20 61 6e 79 0a 2a 2a 20 63 61 73  n" in any.** cas
268a0 65 20 6f 72 20 69 66 20 74 68 65 20 76 61 6c 75  e or if the valu
268b0 65 20 62 65 67 69 6e 73 20 77 69 74 68 20 61 20  e begins with a 
268c0 6e 6f 6e 2d 7a 65 72 6f 20 6e 75 6d 62 65 72 2e  non-zero number.
268d0 20 20 54 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65    The .** sqlite
268e0 33 5f 75 72 69 5f 62 6f 6f 6c 65 61 6e 28 46 2c  3_uri_boolean(F,
268f0 50 2c 42 29 20 72 6f 75 74 69 6e 65 73 20 72 65  P,B) routines re
26900 74 75 72 6e 73 20 66 61 6c 73 65 20 28 30 29 20  turns false (0) 
26910 69 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a  if the value of.
26920 2a 2a 20 71 75 65 72 79 20 70 61 72 61 6d 65 74  ** query paramet
26930 65 72 20 50 20 69 73 20 6f 6e 65 20 6f 66 20 22  er P is one of "
26940 6e 6f 22 2c 20 22 66 61 6c 73 65 22 2c 20 6f 72  no", "false", or
26950 20 22 6f 66 66 22 20 69 6e 20 61 6e 79 20 63 61   "off" in any ca
26960 73 65 20 6f 72 0a 2a 2a 20 69 66 20 74 68 65 20  se or.** if the 
26970 76 61 6c 75 65 20 62 65 67 69 6e 73 20 77 69 74  value begins wit
26980 68 20 61 20 6e 75 6d 65 72 69 63 20 7a 65 72 6f  h a numeric zero
26990 2e 20 20 49 66 20 50 20 69 73 20 6e 6f 74 20 61  .  If P is not a
269a0 20 71 75 65 72 79 0a 2a 2a 20 70 61 72 61 6d 65   query.** parame
269b0 74 65 72 20 6f 6e 20 46 20 6f 72 20 69 66 20 74  ter on F or if t
269c0 68 65 20 76 61 6c 75 65 20 6f 66 20 50 20 69 73  he value of P is
269d0 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
269e0 61 6e 79 20 6f 66 20 74 68 65 0a 2a 2a 20 61 62  any of the.** ab
269f0 6f 76 65 2c 20 74 68 65 6e 20 73 71 6c 69 74 65  ove, then sqlite
26a00 33 5f 75 72 69 5f 62 6f 6f 6c 65 61 6e 28 46 2c  3_uri_boolean(F,
26a10 50 2c 42 29 20 72 65 74 75 72 6e 73 20 28 42 21  P,B) returns (B!
26a20 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  =0)..**.** The s
26a30 71 6c 69 74 65 33 5f 75 72 69 5f 69 6e 74 36 34  qlite3_uri_int64
26a40 28 46 2c 50 2c 44 29 20 72 6f 75 74 69 6e 65 20  (F,P,D) routine 
26a50 63 6f 6e 76 65 72 74 73 20 74 68 65 20 76 61 6c  converts the val
26a60 75 65 20 6f 66 20 50 20 69 6e 74 6f 20 61 0a 2a  ue of P into a.*
26a70 2a 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  * 64-bit signed 
26a80 69 6e 74 65 67 65 72 20 61 6e 64 20 72 65 74 75  integer and retu
26a90 72 6e 73 20 74 68 61 74 20 69 6e 74 65 67 65 72  rns that integer
26aa0 2c 20 6f 72 20 44 20 69 66 20 50 20 64 6f 65 73  , or D if P does
26ab0 20 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 2e 20 20   not.** exist.  
26ac0 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  If the value of 
26ad0 50 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 6f  P is something o
26ae0 74 68 65 72 20 74 68 61 6e 20 61 6e 20 69 6e 74  ther than an int
26af0 65 67 65 72 2c 20 74 68 65 6e 0a 2a 2a 20 7a 65  eger, then.** ze
26b00 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  ro is returned..
26b10 2a 2a 20 0a 2a 2a 20 49 66 20 46 20 69 73 20 61  ** .** If F is a
26b20 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74   NULL pointer, t
26b30 68 65 6e 20 73 71 6c 69 74 65 33 5f 75 72 69 5f  hen sqlite3_uri_
26b40 70 61 72 61 6d 65 74 65 72 28 46 2c 50 29 20 72  parameter(F,P) r
26b50 65 74 75 72 6e 73 20 4e 55 4c 4c 20 61 6e 64 0a  eturns NULL and.
26b60 2a 2a 20 73 71 6c 69 74 65 33 5f 75 72 69 5f 62  ** sqlite3_uri_b
26b70 6f 6f 6c 65 61 6e 28 46 2c 50 2c 42 29 20 72 65  oolean(F,P,B) re
26b80 74 75 72 6e 73 20 42 2e 20 20 49 66 20 46 20 69  turns B.  If F i
26b90 73 20 6e 6f 74 20 61 20 4e 55 4c 4c 20 70 6f 69  s not a NULL poi
26ba0 6e 74 65 72 20 61 6e 64 0a 2a 2a 20 69 73 20 6e  nter and.** is n
26bb0 6f 74 20 61 20 64 61 74 61 62 61 73 65 20 66 69  ot a database fi
26bc0 6c 65 20 70 61 74 68 6e 61 6d 65 20 70 6f 69 6e  le pathname poin
26bd0 74 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20  ter that SQLite 
26be0 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20  passed into the 
26bf0 78 4f 70 65 6e 0a 2a 2a 20 56 46 53 20 6d 65 74  xOpen.** VFS met
26c00 68 6f 64 2c 20 74 68 65 6e 20 74 68 65 20 62 65  hod, then the be
26c10 68 61 76 69 6f 72 20 6f 66 20 74 68 69 73 20 72  havior of this r
26c20 6f 75 74 69 6e 65 20 69 73 20 75 6e 64 65 66 69  outine is undefi
26c30 6e 65 64 20 61 6e 64 20 70 72 6f 62 61 62 6c 79  ned and probably
26c40 0a 2a 2a 20 75 6e 64 65 73 69 72 61 62 6c 65 2e  .** undesirable.
26c50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
26c60 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
26c70 65 33 5f 75 72 69 5f 70 61 72 61 6d 65 74 65 72  e3_uri_parameter
26c80 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69  (const char *zFi
26c90 6c 65 6e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68  lename, const ch
26ca0 61 72 20 2a 7a 50 61 72 61 6d 29 3b 0a 53 51 4c  ar *zParam);.SQL
26cb0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
26cc0 74 65 33 5f 75 72 69 5f 62 6f 6f 6c 65 61 6e 28  te3_uri_boolean(
26cd0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
26ce0 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
26cf0 50 61 72 61 6d 2c 20 69 6e 74 20 62 44 65 66 61  Param, int bDefa
26d00 75 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ult);.SQLITE_API
26d10 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
26d20 71 6c 69 74 65 33 5f 75 72 69 5f 69 6e 74 36 34  qlite3_uri_int64
26d30 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f  (const char*, co
26d40 6e 73 74 20 63 68 61 72 2a 2c 20 73 71 6c 69 74  nst char*, sqlit
26d50 65 33 5f 69 6e 74 36 34 29 3b 0a 0a 0a 2f 2a 0a  e3_int64);.../*.
26d60 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 72 72  ** CAPI3REF: Err
26d70 6f 72 20 43 6f 64 65 73 20 41 6e 64 20 4d 65 73  or Codes And Mes
26d80 73 61 67 65 73 0a 2a 2a 0a 2a 2a 20 5e 54 68 65  sages.**.** ^The
26d90 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65   sqlite3_errcode
26da0 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
26db0 75 72 6e 73 20 74 68 65 20 6e 75 6d 65 72 69 63  urns the numeric
26dc0 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 6f   [result code] o
26dd0 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72  r.** [extended r
26de0 65 73 75 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20  esult code] for 
26df0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
26e00 66 61 69 6c 65 64 20 73 71 6c 69 74 65 33 5f 2a  failed sqlite3_*
26e10 20 41 50 49 20 63 61 6c 6c 0a 2a 2a 20 61 73 73   API call.** ass
26e20 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 5b  ociated with a [
26e30 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
26e40 69 6f 6e 5d 2e 20 49 66 20 61 20 70 72 69 6f 72  ion]. If a prior
26e50 20 41 50 49 20 63 61 6c 6c 20 66 61 69 6c 65 64   API call failed
26e60 0a 2a 2a 20 62 75 74 20 74 68 65 20 6d 6f 73 74  .** but the most
26e70 20 72 65 63 65 6e 74 20 41 50 49 20 63 61 6c 6c   recent API call
26e80 20 73 75 63 63 65 65 64 65 64 2c 20 74 68 65 20   succeeded, the 
26e90 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f  return value fro
26ea0 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72  m.** sqlite3_err
26eb0 63 6f 64 65 28 29 20 69 73 20 75 6e 64 65 66 69  code() is undefi
26ec0 6e 65 64 2e 20 20 5e 54 68 65 20 73 71 6c 69 74  ned.  ^The sqlit
26ed0 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63  e3_extended_errc
26ee0 6f 64 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61  ode().** interfa
26ef0 63 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 65  ce is the same e
26f00 78 63 65 70 74 20 74 68 61 74 20 69 74 20 61 6c  xcept that it al
26f10 77 61 79 73 20 72 65 74 75 72 6e 73 20 74 68 65  ways returns the
26f20 20 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72   .** [extended r
26f30 65 73 75 6c 74 20 63 6f 64 65 5d 20 65 76 65 6e  esult code] even
26f40 20 77 68 65 6e 20 65 78 74 65 6e 64 65 64 20 72   when extended r
26f50 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 0a  esult codes are.
26f60 2a 2a 20 64 69 73 61 62 6c 65 64 2e 0a 2a 2a 0a  ** disabled..**.
26f70 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f  ** ^The sqlite3_
26f80 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 6c  errmsg() and sql
26f90 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 20  ite3_errmsg16() 
26fa0 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 2d 6c  return English-l
26fb0 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65 78 74 20  anguage.** text 
26fc0 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74  that describes t
26fd0 68 65 20 65 72 72 6f 72 2c 20 61 73 20 65 69 74  he error, as eit
26fe0 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55 54 46  her UTF-8 or UTF
26ff0 2d 31 36 20 72 65 73 70 65 63 74 69 76 65 6c 79  -16 respectively
27000 2e 0a 2a 2a 20 5e 28 4d 65 6d 6f 72 79 20 74 6f  ..** ^(Memory to
27010 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20   hold the error 
27020 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 69  message string i
27030 73 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 6e  s managed intern
27040 61 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70 70  ally..** The app
27050 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  lication does no
27060 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20  t need to worry 
27070 61 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74 68  about freeing th
27080 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f 77  e result..** How
27090 65 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20  ever, the error 
270a0 73 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65 20  string might be 
270b0 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 64  overwritten or d
270c0 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a  eallocated by.**
270d0 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
270e0 73 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69 74  s to other SQLit
270f0 65 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63  e interface func
27100 74 69 6f 6e 73 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e  tions.)^.**.** ^
27110 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 73  The sqlite3_errs
27120 74 72 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  tr() interface r
27130 65 74 75 72 6e 73 20 74 68 65 20 45 6e 67 6c 69  eturns the Engli
27140 73 68 2d 6c 61 6e 67 75 61 67 65 20 74 65 78 74  sh-language text
27150 0a 2a 2a 20 74 68 61 74 20 64 65 73 63 72 69 62  .** that describ
27160 65 73 20 74 68 65 20 5b 72 65 73 75 6c 74 20 63  es the [result c
27170 6f 64 65 5d 2c 20 61 73 20 55 54 46 2d 38 2e 0a  ode], as UTF-8..
27180 2a 2a 20 5e 28 4d 65 6d 6f 72 79 20 74 6f 20 68  ** ^(Memory to h
27190 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 6d 65  old the error me
271a0 73 73 61 67 65 20 73 74 72 69 6e 67 20 69 73 20  ssage string is 
271b0 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 6e 61 6c  managed internal
271c0 6c 79 0a 2a 2a 20 61 6e 64 20 6d 75 73 74 20 6e  ly.** and must n
271d0 6f 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74  ot be freed by t
271e0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 29 5e  he application)^
271f0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65  ..**.** When the
27200 20 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72   serialized [thr
27210 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20  eading mode] is 
27220 69 6e 20 75 73 65 2c 20 69 74 20 6d 69 67 68 74  in use, it might
27230 20 62 65 20 74 68 65 0a 2a 2a 20 63 61 73 65 20   be the.** case 
27240 74 68 61 74 20 61 20 73 65 63 6f 6e 64 20 65 72  that a second er
27250 72 6f 72 20 6f 63 63 75 72 73 20 6f 6e 20 61 20  ror occurs on a 
27260 73 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20  separate thread 
27270 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68  in between.** th
27280 65 20 74 69 6d 65 20 6f 66 20 74 68 65 20 66 69  e time of the fi
27290 72 73 74 20 65 72 72 6f 72 20 61 6e 64 20 74 68  rst error and th
272a0 65 20 63 61 6c 6c 20 74 6f 20 74 68 65 73 65 20  e call to these 
272b0 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 57  interfaces..** W
272c0 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73  hen that happens
272d0 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 65 72 72  , the second err
272e0 6f 72 20 77 69 6c 6c 20 62 65 20 72 65 70 6f 72  or will be repor
272f0 74 65 64 20 73 69 6e 63 65 20 74 68 65 73 65 0a  ted since these.
27300 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 61 6c  ** interfaces al
27310 77 61 79 73 20 72 65 70 6f 72 74 20 74 68 65 20  ways report the 
27320 6d 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73 75  most recent resu
27330 6c 74 2e 20 20 54 6f 20 61 76 6f 69 64 0a 2a 2a  lt.  To avoid.**
27340 20 74 68 69 73 2c 20 65 61 63 68 20 74 68 72 65   this, each thre
27350 61 64 20 63 61 6e 20 6f 62 74 61 69 6e 20 65 78  ad can obtain ex
27360 63 6c 75 73 69 76 65 20 75 73 65 20 6f 66 20 74  clusive use of t
27370 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
27380 6e 65 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 62 79  nection] D.** by
27390 20 69 6e 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74   invoking [sqlit
273a0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 5d 28  e3_mutex_enter](
273b0 5b 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65  [sqlite3_db_mute
273c0 78 5d 28 44 29 29 20 62 65 66 6f 72 65 20 62 65  x](D)) before be
273d0 67 69 6e 6e 69 6e 67 0a 2a 2a 20 74 6f 20 75 73  ginning.** to us
273e0 65 20 44 20 61 6e 64 20 69 6e 76 6f 6b 69 6e 67  e D and invoking
273f0 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   [sqlite3_mutex_
27400 6c 65 61 76 65 5d 28 5b 73 71 6c 69 74 65 33 5f  leave]([sqlite3_
27410 64 62 5f 6d 75 74 65 78 5d 28 44 29 29 20 61 66  db_mutex](D)) af
27420 74 65 72 0a 2a 2a 20 61 6c 6c 20 63 61 6c 6c 73  ter.** all calls
27430 20 74 6f 20 74 68 65 20 69 6e 74 65 72 66 61 63   to the interfac
27440 65 73 20 6c 69 73 74 65 64 20 68 65 72 65 20 61  es listed here a
27450 72 65 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a  re completed..**
27460 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 66  .** If an interf
27470 61 63 65 20 66 61 69 6c 73 20 77 69 74 68 20 53  ace fails with S
27480 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 74 68  QLITE_MISUSE, th
27490 61 74 20 6d 65 61 6e 73 20 74 68 65 20 69 6e 74  at means the int
274a0 65 72 66 61 63 65 0a 2a 2a 20 77 61 73 20 69 6e  erface.** was in
274b0 76 6f 6b 65 64 20 69 6e 63 6f 72 72 65 63 74 6c  voked incorrectl
274c0 79 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61  y by the applica
274d0 74 69 6f 6e 2e 20 20 49 6e 20 74 68 61 74 20 63  tion.  In that c
274e0 61 73 65 2c 20 74 68 65 0a 2a 2a 20 65 72 72 6f  ase, the.** erro
274f0 72 20 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61  r code and messa
27500 67 65 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f  ge may or may no
27510 74 20 62 65 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c  t be set..*/.SQL
27520 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
27530 74 65 33 5f 65 72 72 63 6f 64 65 28 73 71 6c 69  te3_errcode(sqli
27540 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45  te3 *db);.SQLITE
27550 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
27560 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64  _extended_errcod
27570 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a  e(sqlite3 *db);.
27580 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
27590 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 65   char *sqlite3_e
275a0 72 72 6d 73 67 28 73 71 6c 69 74 65 33 2a 29 3b  rrmsg(sqlite3*);
275b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
275c0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
275d0 65 72 72 6d 73 67 31 36 28 73 71 6c 69 74 65 33  errmsg16(sqlite3
275e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  *);.SQLITE_API c
275f0 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
27600 65 33 5f 65 72 72 73 74 72 28 69 6e 74 29 3b 0a  e3_errstr(int);.
27610 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
27620 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 4f   SQL Statement O
27630 62 6a 65 63 74 0a 2a 2a 20 4b 45 59 57 4f 52 44  bject.** KEYWORD
27640 53 3a 20 7b 70 72 65 70 61 72 65 64 20 73 74 61  S: {prepared sta
27650 74 65 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72 65  tement} {prepare
27660 64 20 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a  d statements}.**
27670 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
27680 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72  of this object r
27690 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67  epresents a sing
276a0 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  le SQL statement
276b0 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74  ..** This object
276c0 20 69 73 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e   is variously kn
276d0 6f 77 6e 20 61 73 20 61 20 22 70 72 65 70 61 72  own as a "prepar
276e0 65 64 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72  ed statement" or
276f0 20 61 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20   a.** "compiled 
27700 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 22 20 6f  SQL statement" o
27710 72 20 73 69 6d 70 6c 79 20 61 73 20 61 20 22 73  r simply as a "s
27720 74 61 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a  tatement"..**.**
27730 20 54 68 65 20 6c 69 66 65 20 6f 66 20 61 20 73   The life of a s
27740 74 61 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20  tatement object 
27750 67 6f 65 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c  goes something l
27760 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
27770 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65  <ol>.** <li> Cre
27780 61 74 65 20 74 68 65 20 6f 62 6a 65 63 74 20 75  ate the object u
27790 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72  sing [sqlite3_pr
277a0 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 61  epare_v2()] or a
277b0 20 72 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20   related.**     
277c0 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c   function..** <l
277d0 69 3e 20 42 69 6e 64 20 76 61 6c 75 65 73 20 74  i> Bind values t
277e0 6f 20 5b 68 6f 73 74 20 70 61 72 61 6d 65 74 65  o [host paramete
277f0 72 73 5d 20 75 73 69 6e 67 20 74 68 65 20 73 71  rs] using the sq
27800 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a  lite3_bind_*().*
27810 2a 20 20 20 20 20 20 69 6e 74 65 72 66 61 63 65  *      interface
27820 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74  s..** <li> Run t
27830 68 65 20 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e  he SQL by callin
27840 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  g [sqlite3_step(
27850 29 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74  )] one or more t
27860 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65  imes..** <li> Re
27870 73 65 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e  set the statemen
27880 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  t using [sqlite3
27890 5f 72 65 73 65 74 28 29 5d 20 74 68 65 6e 20 67  _reset()] then g
278a0 6f 20 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74  o back.**      t
278b0 6f 20 73 74 65 70 20 32 2e 20 20 44 6f 20 74 68  o step 2.  Do th
278c0 69 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  is zero or more 
278d0 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44  times..** <li> D
278e0 65 73 74 72 6f 79 20 74 68 65 20 6f 62 6a 65 63  estroy the objec
278f0 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  t using [sqlite3
27900 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a  _finalize()]..**
27910 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66   </ol>.**.** Ref
27920 65 72 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74  er to documentat
27930 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 61  ion on individua
27940 6c 20 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 20  l methods above 
27950 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a  for additional.*
27960 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  * information..*
27970 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
27980 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 73 71   sqlite3_stmt sq
27990 6c 69 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a  lite3_stmt;../*.
279a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e  ** CAPI3REF: Run
279b0 2d 74 69 6d 65 20 4c 69 6d 69 74 73 0a 2a 2a 0a  -time Limits.**.
279c0 2a 2a 20 5e 28 54 68 69 73 20 69 6e 74 65 72 66  ** ^(This interf
279d0 61 63 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 73  ace allows the s
279e0 69 7a 65 20 6f 66 20 76 61 72 69 6f 75 73 20 63  ize of various c
279f0 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20  onstructs to be 
27a00 6c 69 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20  limited.** on a 
27a10 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f  connection by co
27a20 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 2e 20  nnection basis. 
27a30 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d   The first param
27a40 65 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b  eter is the.** [
27a50 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
27a60 69 6f 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74  ion] whose limit
27a70 20 69 73 20 74 6f 20 62 65 20 73 65 74 20 6f 72   is to be set or
27a80 20 71 75 65 72 69 65 64 2e 20 20 54 68 65 0a 2a   queried.  The.*
27a90 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  * second paramet
27aa0 65 72 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65  er is one of the
27ab0 20 5b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69   [limit categori
27ac0 65 73 5d 20 74 68 61 74 20 64 65 66 69 6e 65 20  es] that define 
27ad0 61 0a 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f  a.** class of co
27ae0 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 73  nstructs to be s
27af0 69 7a 65 20 6c 69 6d 69 74 65 64 2e 20 20 54 68  ize limited.  Th
27b00 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65  e third paramete
27b10 72 20 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20  r is the.** new 
27b20 6c 69 6d 69 74 20 66 6f 72 20 74 68 61 74 20 63  limit for that c
27b30 6f 6e 73 74 72 75 63 74 2e 29 5e 0a 2a 2a 0a 2a  onstruct.)^.**.*
27b40 2a 20 5e 49 66 20 74 68 65 20 6e 65 77 20 6c 69  * ^If the new li
27b50 6d 69 74 20 69 73 20 61 20 6e 65 67 61 74 69 76  mit is a negativ
27b60 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c 69  e number, the li
27b70 6d 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64  mit is unchanged
27b80 2e 0a 2a 2a 20 5e 28 46 6f 72 20 65 61 63 68 20  ..** ^(For each 
27b90 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 20 53  limit category S
27ba0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 3c 69 3e 4e  QLITE_LIMIT_<i>N
27bb0 41 4d 45 3c 2f 69 3e 20 74 68 65 72 65 20 69 73  AME</i> there is
27bc0 20 61 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c   a .** [limits |
27bd0 20 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e   hard upper boun
27be0 64 5d 0a 2a 2a 20 73 65 74 20 61 74 20 63 6f 6d  d].** set at com
27bf0 70 69 6c 65 2d 74 69 6d 65 20 62 79 20 61 20 43  pile-time by a C
27c00 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61   preprocessor ma
27c10 63 72 6f 20 63 61 6c 6c 65 64 0a 2a 2a 20 5b 6c  cro called.** [l
27c20 69 6d 69 74 73 20 7c 20 53 51 4c 49 54 45 5f 4d  imits | SQLITE_M
27c30 41 58 5f 3c 69 3e 4e 41 4d 45 3c 2f 69 3e 5d 2e  AX_<i>NAME</i>].
27c40 0a 2a 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54  .** (The "_LIMIT
27c50 5f 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69  _" in the name i
27c60 73 20 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d  s changed to "_M
27c70 41 58 5f 22 2e 29 29 5e 0a 2a 2a 20 5e 41 74 74  AX_".))^.** ^Att
27c80 65 6d 70 74 73 20 74 6f 20 69 6e 63 72 65 61 73  empts to increas
27c90 65 20 61 20 6c 69 6d 69 74 20 61 62 6f 76 65 20  e a limit above 
27ca0 69 74 73 20 68 61 72 64 20 75 70 70 65 72 20 62  its hard upper b
27cb0 6f 75 6e 64 20 61 72 65 0a 2a 2a 20 73 69 6c 65  ound are.** sile
27cc0 6e 74 6c 79 20 74 72 75 6e 63 61 74 65 64 20 74  ntly truncated t
27cd0 6f 20 74 68 65 20 68 61 72 64 20 75 70 70 65 72  o the hard upper
27ce0 20 62 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 5e 52   bound..**.** ^R
27cf0 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65  egardless of whe
27d00 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
27d10 6c 69 6d 69 74 20 77 61 73 20 63 68 61 6e 67 65  limit was change
27d20 64 2c 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c 69  d, the .** [sqli
27d30 74 65 33 5f 6c 69 6d 69 74 28 29 5d 20 69 6e 74  te3_limit()] int
27d40 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74  erface returns t
27d50 68 65 20 70 72 69 6f 72 20 76 61 6c 75 65 20 6f  he prior value o
27d60 66 20 74 68 65 20 6c 69 6d 69 74 2e 0a 2a 2a 20  f the limit..** 
27d70 5e 48 65 6e 63 65 2c 20 74 6f 20 66 69 6e 64 20  ^Hence, to find 
27d80 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  the current valu
27d90 65 20 6f 66 20 61 20 6c 69 6d 69 74 20 77 69 74  e of a limit wit
27da0 68 6f 75 74 20 63 68 61 6e 67 69 6e 67 20 69 74  hout changing it
27db0 2c 0a 2a 2a 20 73 69 6d 70 6c 79 20 69 6e 76 6f  ,.** simply invo
27dc0 6b 65 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  ke this interfac
27dd0 65 20 77 69 74 68 20 74 68 65 20 74 68 69 72 64  e with the third
27de0 20 70 61 72 61 6d 65 74 65 72 20 73 65 74 20 74   parameter set t
27df0 6f 20 2d 31 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 2d  o -1..**.** Run-
27e00 74 69 6d 65 20 6c 69 6d 69 74 73 20 61 72 65 20  time limits are 
27e10 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65  intended for use
27e20 20 69 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73   in applications
27e30 20 74 68 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20   that manage.** 
27e40 62 6f 74 68 20 74 68 65 69 72 20 6f 77 6e 20 69  both their own i
27e50 6e 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65  nternal database
27e60 20 61 6e 64 20 61 6c 73 6f 20 64 61 74 61 62 61   and also databa
27e70 73 65 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e  ses that are con
27e80 74 72 6f 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e  trolled.** by un
27e90 74 72 75 73 74 65 64 20 65 78 74 65 72 6e 61 6c  trusted external
27ea0 20 73 6f 75 72 63 65 73 2e 20 20 41 6e 20 65 78   sources.  An ex
27eb0 61 6d 70 6c 65 20 61 70 70 6c 69 63 61 74 69 6f  ample applicatio
27ec0 6e 20 6d 69 67 68 74 20 62 65 20 61 0a 2a 2a 20  n might be a.** 
27ed0 77 65 62 20 62 72 6f 77 73 65 72 20 74 68 61 74  web browser that
27ee0 20 68 61 73 20 69 74 73 20 6f 77 6e 20 64 61 74   has its own dat
27ef0 61 62 61 73 65 73 20 66 6f 72 20 73 74 6f 72 69  abases for stori
27f00 6e 67 20 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a  ng history and.*
27f10 2a 20 73 65 70 61 72 61 74 65 20 64 61 74 61 62  * separate datab
27f20 61 73 65 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20  ases controlled 
27f30 62 79 20 4a 61 76 61 53 63 72 69 70 74 20 61 70  by JavaScript ap
27f40 70 6c 69 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c  plications downl
27f50 6f 61 64 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65  oaded.** off the
27f60 20 49 6e 74 65 72 6e 65 74 2e 20 20 54 68 65 20   Internet.  The 
27f70 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73  internal databas
27f80 65 73 20 63 61 6e 20 62 65 20 67 69 76 65 6e 20  es can be given 
27f90 74 68 65 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65  the.** large, de
27fa0 66 61 75 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44  fault limits.  D
27fb0 61 74 61 62 61 73 65 73 20 6d 61 6e 61 67 65 64  atabases managed
27fc0 20 62 79 20 65 78 74 65 72 6e 61 6c 20 73 6f 75   by external sou
27fd0 72 63 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67  rces can.** be g
27fe0 69 76 65 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65  iven much smalle
27ff0 72 20 6c 69 6d 69 74 73 20 64 65 73 69 67 6e 65  r limits designe
28000 64 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 64  d to prevent a d
28010 65 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65  enial of service
28020 0a 2a 2a 20 61 74 74 61 63 6b 2e 20 20 44 65 76  .** attack.  Dev
28030 65 6c 6f 70 65 72 73 20 6d 69 67 68 74 20 61 6c  elopers might al
28040 73 6f 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74  so want to use t
28050 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f  he [sqlite3_set_
28060 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a  authorizer()].**
28070 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 66 75   interface to fu
28080 72 74 68 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e  rther control un
28090 74 72 75 73 74 65 64 20 53 51 4c 2e 20 20 54 68  trusted SQL.  Th
280a0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  e size of the da
280b0 74 61 62 61 73 65 0a 2a 2a 20 63 72 65 61 74 65  tabase.** create
280c0 64 20 62 79 20 61 6e 20 75 6e 74 72 75 73 74 65  d by an untruste
280d0 64 20 73 63 72 69 70 74 20 63 61 6e 20 62 65 20  d script can be 
280e0 63 6f 6e 74 61 69 6e 65 64 20 75 73 69 6e 67 20  contained using 
280f0 74 68 65 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65  the.** [max_page
28100 5f 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d  _count] [PRAGMA]
28110 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d  ..**.** New run-
28120 74 69 6d 65 20 6c 69 6d 69 74 20 63 61 74 65 67  time limit categ
28130 6f 72 69 65 73 20 6d 61 79 20 62 65 20 61 64 64  ories may be add
28140 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c  ed in future rel
28150 65 61 73 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  eases..*/.SQLITE
28160 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
28170 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 2a 2c  _limit(sqlite3*,
28180 20 69 6e 74 20 69 64 2c 20 69 6e 74 20 6e 65 77   int id, int new
28190 56 61 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  Val);../*.** CAP
281a0 49 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20  I3REF: Run-Time 
281b0 4c 69 6d 69 74 20 43 61 74 65 67 6f 72 69 65 73  Limit Categories
281c0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 6c  .** KEYWORDS: {l
281d0 69 6d 69 74 20 63 61 74 65 67 6f 72 79 7d 20 7b  imit category} {
281e0 2a 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65  *limit categorie
281f0 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63  s}.**.** These c
28200 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e 65 20  onstants define 
28210 76 61 72 69 6f 75 73 20 70 65 72 66 6f 72 6d 61  various performa
28220 6e 63 65 20 6c 69 6d 69 74 73 0a 2a 2a 20 74 68  nce limits.** th
28230 61 74 20 63 61 6e 20 62 65 20 6c 6f 77 65 72 65  at can be lowere
28240 64 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73  d at run-time us
28250 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d  ing [sqlite3_lim
28260 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 79  it()]..** The sy
28270 6e 6f 70 73 69 73 20 6f 66 20 74 68 65 20 6d 65  nopsis of the me
28280 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61  anings of the va
28290 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 69 73 20  rious limits is 
282a0 73 68 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a 2a 20  shown below..** 
282b0 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  Additional infor
282c0 6d 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61  mation is availa
282d0 62 6c 65 20 61 74 20 5b 6c 69 6d 69 74 73 20 7c  ble at [limits |
282e0 20 4c 69 6d 69 74 73 20 69 6e 20 53 51 4c 69 74   Limits in SQLit
282f0 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a  e]..**.** <dl>.*
28300 2a 20 5b 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  * [[SQLITE_LIMIT
28310 5f 4c 45 4e 47 54 48 5d 5d 20 5e 28 3c 64 74 3e  _LENGTH]] ^(<dt>
28320 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
28330 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  GTH</dt>.** <dd>
28340 54 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65  The maximum size
28350 20 6f 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f   of any string o
28360 72 20 42 4c 4f 42 20 6f 72 20 74 61 62 6c 65 20  r BLOB or table 
28370 72 6f 77 2c 20 69 6e 20 62 79 74 65 73 2e 3c 64  row, in bytes.<d
28380 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c  d>)^.**.** [[SQL
28390 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45  ITE_LIMIT_SQL_LE
283a0 4e 47 54 48 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c  NGTH]] ^(<dt>SQL
283b0 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45  ITE_LIMIT_SQL_LE
283c0 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  NGTH</dt>.** <dd
283d0 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e  >The maximum len
283e0 67 74 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74  gth of an SQL st
283f0 61 74 65 6d 65 6e 74 2c 20 69 6e 20 62 79 74 65  atement, in byte
28400 73 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20  s.</dd>)^.**.** 
28410 5b 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43  [[SQLITE_LIMIT_C
28420 4f 4c 55 4d 4e 5d 5d 20 5e 28 3c 64 74 3e 53 51  OLUMN]] ^(<dt>SQ
28430 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d  LITE_LIMIT_COLUM
28440 4e 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  N</dt>.** <dd>Th
28450 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
28460 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61   of columns in a
28470 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f   table definitio
28480 6e 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 72  n or in the.** r
28490 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 5b  esult set of a [
284a0 53 45 4c 45 43 54 5d 20 6f 72 20 74 68 65 20 6d  SELECT] or the m
284b0 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
284c0 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 20 69   columns in an i
284d0 6e 64 65 78 0a 2a 2a 20 6f 72 20 69 6e 20 61 6e  ndex.** or in an
284e0 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f   ORDER BY or GRO
284f0 55 50 20 42 59 20 63 6c 61 75 73 65 2e 3c 2f 64  UP BY clause.</d
28500 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c  d>)^.**.** [[SQL
28510 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44  ITE_LIMIT_EXPR_D
28520 45 50 54 48 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c  EPTH]] ^(<dt>SQL
28530 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44  ITE_LIMIT_EXPR_D
28540 45 50 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  EPTH</dt>.** <dd
28550 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70  >The maximum dep
28560 74 68 20 6f 66 20 74 68 65 20 70 61 72 73 65 20  th of the parse 
28570 74 72 65 65 20 6f 6e 20 61 6e 79 20 65 78 70 72  tree on any expr
28580 65 73 73 69 6f 6e 2e 3c 2f 64 64 3e 29 5e 0a 2a  ession.</dd>)^.*
28590 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 4c 49  *.** [[SQLITE_LI
285a0 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
285b0 45 43 54 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49  ECT]] ^(<dt>SQLI
285c0 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e  TE_LIMIT_COMPOUN
285d0 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a 2a 2a  D_SELECT</dt>.**
285e0 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d   <dd>The maximum
285f0 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   number of terms
28600 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53   in a compound S
28610 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
28620 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b  </dd>)^.**.** [[
28630 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 42  SQLITE_LIMIT_VDB
28640 45 5f 4f 50 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c  E_OP]] ^(<dt>SQL
28650 49 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f 4f  ITE_LIMIT_VDBE_O
28660 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  P</dt>.** <dd>Th
28670 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
28680 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73   of instructions
28690 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20 6d 61   in a virtual ma
286a0 63 68 69 6e 65 20 70 72 6f 67 72 61 6d 0a 2a 2a  chine program.**
286b0 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
286c0 6e 74 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  nt an SQL statem
286d0 65 6e 74 2e 20 20 54 68 69 73 20 6c 69 6d 69 74  ent.  This limit
286e0 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
286f0 79 0a 2a 2a 20 65 6e 66 6f 72 63 65 64 2c 20 74  y.** enforced, t
28700 68 6f 75 67 68 20 74 68 61 74 20 6d 69 67 68 74  hough that might
28710 20 62 65 20 61 64 64 65 64 20 69 6e 20 73 6f 6d   be added in som
28720 65 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  e future release
28730 20 6f 66 0a 2a 2a 20 53 51 4c 69 74 65 2e 3c 2f   of.** SQLite.</
28740 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51  dd>)^.**.** [[SQ
28750 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54  LITE_LIMIT_FUNCT
28760 49 4f 4e 5f 41 52 47 5d 5d 20 5e 28 3c 64 74 3e  ION_ARG]] ^(<dt>
28770 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e  SQLITE_LIMIT_FUN
28780 43 54 49 4f 4e 5f 41 52 47 3c 2f 64 74 3e 0a 2a  CTION_ARG</dt>.*
28790 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
287a0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  m number of argu
287b0 6d 65 6e 74 73 20 6f 6e 20 61 20 66 75 6e 63 74  ments on a funct
287c0 69 6f 6e 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a  ion.</dd>)^.**.*
287d0 2a 20 5b 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  * [[SQLITE_LIMIT
287e0 5f 41 54 54 41 43 48 45 44 5d 5d 20 5e 28 3c 64  _ATTACHED]] ^(<d
287f0 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41  t>SQLITE_LIMIT_A
28800 54 54 41 43 48 45 44 3c 2f 64 74 3e 0a 2a 2a 20  TTACHED</dt>.** 
28810 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20  <dd>The maximum 
28820 6e 75 6d 62 65 72 20 6f 66 20 5b 41 54 54 41 43  number of [ATTAC
28830 48 20 7c 20 61 74 74 61 63 68 65 64 20 64 61 74  H | attached dat
28840 61 62 61 73 65 73 5d 2e 29 5e 3c 2f 64 64 3e 0a  abases].)^</dd>.
28850 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 4c  **.** [[SQLITE_L
28860 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52  IMIT_LIKE_PATTER
28870 4e 5f 4c 45 4e 47 54 48 5d 5d 0a 2a 2a 20 5e 28  N_LENGTH]].** ^(
28880 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
28890 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45  _LIKE_PATTERN_LE
288a0 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  NGTH</dt>.** <dd
288b0 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e  >The maximum len
288c0 67 74 68 20 6f 66 20 74 68 65 20 70 61 74 74 65  gth of the patte
288d0 72 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  rn argument to t
288e0 68 65 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20  he [LIKE] or.** 
288f0 5b 47 4c 4f 42 5d 20 6f 70 65 72 61 74 6f 72 73  [GLOB] operators
28900 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b  .</dd>)^.**.** [
28910 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41  [SQLITE_LIMIT_VA
28920 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 5d 0a  RIABLE_NUMBER]].
28930 2a 2a 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f  ** ^(<dt>SQLITE_
28940 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e  LIMIT_VARIABLE_N
28950 55 4d 42 45 52 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  UMBER</dt>.** <d
28960 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 69 6e  d>The maximum in
28970 64 65 78 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e  dex number of an
28980 79 20 5b 70 61 72 61 6d 65 74 65 72 5d 20 69 6e  y [parameter] in
28990 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
289a0 74 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c  t.)^.**.** [[SQL
289b0 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45  ITE_LIMIT_TRIGGE
289c0 52 5f 44 45 50 54 48 5d 5d 20 5e 28 3c 64 74 3e  R_DEPTH]] ^(<dt>
289d0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49  SQLITE_LIMIT_TRI
289e0 47 47 45 52 5f 44 45 50 54 48 3c 2f 64 74 3e 0a  GGER_DEPTH</dt>.
289f0 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d  ** <dd>The maxim
28a00 75 6d 20 64 65 70 74 68 20 6f 66 20 72 65 63 75  um depth of recu
28a10 72 73 69 6f 6e 20 66 6f 72 20 74 72 69 67 67 65  rsion for trigge
28a20 72 73 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 20 3c 2f  rs.</dd>)^.** </
28a30 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  dl>.*/.#define S
28a40 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
28a50 54 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TH              
28a60 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20        0.#define 
28a70 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c  SQLITE_LIMIT_SQL
28a80 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20 20 20  _LENGTH         
28a90 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65         1.#define
28aa0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f   SQLITE_LIMIT_CO
28ab0 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20  LUMN            
28ac0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e          2.#defin
28ad0 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45  e SQLITE_LIMIT_E
28ae0 58 50 52 5f 44 45 50 54 48 20 20 20 20 20 20 20  XPR_DEPTH       
28af0 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69           3.#defi
28b00 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
28b10 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20  COMPOUND_SELECT 
28b20 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66            4.#def
28b30 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54  ine SQLITE_LIMIT
28b40 5f 56 44 42 45 5f 4f 50 20 20 20 20 20 20 20 20  _VDBE_OP        
28b50 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65             5.#de
28b60 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49  fine SQLITE_LIMI
28b70 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20  T_FUNCTION_ARG  
28b80 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64              6.#d
28b90 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d  efine SQLITE_LIM
28ba0 49 54 5f 41 54 54 41 43 48 45 44 20 20 20 20 20  IT_ATTACHED     
28bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23               7.#
28bc0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49  define SQLITE_LI
28bd0 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MIT_LIKE_PATTERN
28be0 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20 38 0a  _LENGTH       8.
28bf0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
28c00 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55  IMIT_VARIABLE_NU
28c10 4d 42 45 52 20 20 20 20 20 20 20 20 20 20 20 39  MBER           9
28c20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
28c30 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45  LIMIT_TRIGGER_DE
28c40 50 54 48 20 20 20 20 20 20 20 20 20 20 20 20 31  PTH            1
28c50 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  0../*.** CAPI3RE
28c60 46 3a 20 43 6f 6d 70 69 6c 69 6e 67 20 41 6e 20  F: Compiling An 
28c70 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 0a 2a 2a  SQL Statement.**
28c80 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20   KEYWORDS: {SQL 
28c90 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c  statement compil
28ca0 65 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78 65  er}.**.** To exe
28cb0 63 75 74 65 20 61 6e 20 53 51 4c 20 71 75 65 72  cute an SQL quer
28cc0 79 2c 20 69 74 20 6d 75 73 74 20 66 69 72 73 74  y, it must first
28cd0 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e 74   be compiled int
28ce0 6f 20 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a 2a  o a byte-code.**
28cf0 20 70 72 6f 67 72 61 6d 20 75 73 69 6e 67 20 6f   program using o
28d00 6e 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74  ne of these rout
28d10 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ines..**.** The 
28d20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20  first argument, 
28d30 22 64 62 22 2c 20 69 73 20 61 20 5b 64 61 74 61  "db", is a [data
28d40 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
28d50 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61   obtained from a
28d60 0a 2a 2a 20 70 72 69 6f 72 20 73 75 63 63 65 73  .** prior succes
28d70 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71  sful call to [sq
28d80 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b  lite3_open()], [
28d90 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
28da0 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65  )] or.** [sqlite
28db0 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 20 20 54 68  3_open16()].  Th
28dc0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
28dd0 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 68  ction must not h
28de0 61 76 65 20 62 65 65 6e 20 63 6c 6f 73 65 64 2e  ave been closed.
28df0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e  .**.** The secon
28e00 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 7a 53 71  d argument, "zSq
28e10 6c 22 2c 20 69 73 20 74 68 65 20 73 74 61 74 65  l", is the state
28e20 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70 69  ment to be compi
28e30 6c 65 64 2c 20 65 6e 63 6f 64 65 64 0a 2a 2a 20  led, encoded.** 
28e40 61 73 20 65 69 74 68 65 72 20 55 54 46 2d 38 20  as either UTF-8 
28e50 6f 72 20 55 54 46 2d 31 36 2e 20 20 54 68 65 20  or UTF-16.  The 
28e60 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
28e70 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72  ) and sqlite3_pr
28e80 65 70 61 72 65 5f 76 32 28 29 0a 2a 2a 20 69 6e  epare_v2().** in
28e90 74 65 72 66 61 63 65 73 20 75 73 65 20 55 54 46  terfaces use UTF
28ea0 2d 38 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  -8, and sqlite3_
28eb0 70 72 65 70 61 72 65 31 36 28 29 20 61 6e 64 20  prepare16() and 
28ec0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
28ed0 36 5f 76 32 28 29 0a 2a 2a 20 75 73 65 20 55 54  6_v2().** use UT
28ee0 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20  F-16..**.** ^If 
28ef0 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d 65  the nByte argume
28f00 6e 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  nt is less than 
28f10 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71 6c 20  zero, then zSql 
28f20 69 73 20 72 65 61 64 20 75 70 20 74 6f 20 74 68  is read up to th
28f30 65 0a 2a 2a 20 66 69 72 73 74 20 7a 65 72 6f 20  e.** first zero 
28f40 74 65 72 6d 69 6e 61 74 6f 72 2e 20 5e 49 66 20  terminator. ^If 
28f50 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 67  nByte is non-neg
28f60 61 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20 69  ative, then it i
28f70 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a  s the maximum.**
28f80 20 6e 75 6d 62 65 72 20 6f 66 20 20 62 79 74 65   number of  byte
28f90 73 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c  s read from zSql
28fa0 2e 20 20 5e 57 68 65 6e 20 6e 42 79 74 65 20 69  .  ^When nByte i
28fb0 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20  s non-negative, 
28fc0 74 68 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69  the.** zSql stri
28fd0 6e 67 20 65 6e 64 73 20 61 74 20 65 69 74 68 65  ng ends at eithe
28fe0 72 20 74 68 65 20 66 69 72 73 74 20 27 5c 30 30  r the first '\00
28ff0 30 27 20 6f 72 20 27 5c 75 30 30 30 30 27 20 63  0' or '\u0000' c
29000 68 61 72 61 63 74 65 72 20 6f 72 0a 2a 2a 20 74  haracter or.** t
29010 68 65 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65  he nByte-th byte
29020 2c 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65  , whichever come
29030 73 20 66 69 72 73 74 2e 20 49 66 20 74 68 65 20  s first. If the 
29040 63 61 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20  caller knows.** 
29050 74 68 61 74 20 74 68 65 20 73 75 70 70 6c 69 65  that the supplie
29060 64 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d  d string is nul-
29070 74 65 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e  terminated, then
29080 20 74 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c   there is a smal
29090 6c 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65  l.** performance
290a0 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65   advantage to be
290b0 20 67 61 69 6e 65 64 20 62 79 20 70 61 73 73 69   gained by passi
290c0 6e 67 20 61 6e 20 6e 42 79 74 65 20 70 61 72 61  ng an nByte para
290d0 6d 65 74 65 72 20 74 68 61 74 0a 2a 2a 20 69 73  meter that.** is
290e0 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75   equal to the nu
290f0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
29100 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e   the input strin
29110 67 20 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f  g <i>including</
29120 69 3e 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d 74 65  i>.** the nul-te
29130 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 73 20 61  rminator bytes a
29140 73 20 74 68 69 73 20 73 61 76 65 73 20 53 51 4c  s this saves SQL
29150 69 74 65 20 66 72 6f 6d 20 68 61 76 69 6e 67 20  ite from having 
29160 74 6f 0a 2a 2a 20 6d 61 6b 65 20 61 20 63 6f 70  to.** make a cop
29170 79 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 73  y of the input s
29180 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66  tring..**.** ^If
29190 20 70 7a 54 61 69 6c 20 69 73 20 6e 6f 74 20 4e   pzTail is not N
291a0 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 54 61 69 6c  ULL then *pzTail
291b0 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e   is made to poin
291c0 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62  t to the first b
291d0 79 74 65 0a 2a 2a 20 70 61 73 74 20 74 68 65 20  yte.** past the 
291e0 65 6e 64 20 6f 66 20 74 68 65 20 66 69 72 73 74  end of the first
291f0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69   SQL statement i
29200 6e 20 7a 53 71 6c 2e 20 20 54 68 65 73 65 20 72  n zSql.  These r
29210 6f 75 74 69 6e 65 73 20 6f 6e 6c 79 0a 2a 2a 20  outines only.** 
29220 63 6f 6d 70 69 6c 65 20 74 68 65 20 66 69 72 73  compile the firs
29230 74 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a  t statement in z
29240 53 71 6c 2c 20 73 6f 20 2a 70 7a 54 61 69 6c 20  Sql, so *pzTail 
29250 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67  is left pointing
29260 20 74 6f 0a 2a 2a 20 77 68 61 74 20 72 65 6d 61   to.** what rema
29270 69 6e 73 20 75 6e 63 6f 6d 70 69 6c 65 64 2e 0a  ins uncompiled..
29280 2a 2a 0a 2a 2a 20 5e 2a 70 70 53 74 6d 74 20 69  **.** ^*ppStmt i
29290 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
292a0 74 6f 20 61 20 63 6f 6d 70 69 6c 65 64 20 5b 70  to a compiled [p
292b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
292c0 74 5d 20 74 68 61 74 20 63 61 6e 20 62 65 0a 2a  t] that can be.*
292d0 2a 20 65 78 65 63 75 74 65 64 20 75 73 69 6e 67  * executed using
292e0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
292f0 5d 2e 20 20 5e 49 66 20 74 68 65 72 65 20 69 73  ].  ^If there is
29300 20 61 6e 20 65 72 72 6f 72 2c 20 2a 70 70 53 74   an error, *ppSt
29310 6d 74 20 69 73 20 73 65 74 0a 2a 2a 20 74 6f 20  mt is set.** to 
29320 4e 55 4c 4c 2e 20 20 5e 49 66 20 74 68 65 20 69  NULL.  ^If the i
29330 6e 70 75 74 20 74 65 78 74 20 63 6f 6e 74 61 69  nput text contai
29340 6e 73 20 6e 6f 20 53 51 4c 20 28 69 66 20 74 68  ns no SQL (if th
29350 65 20 69 6e 70 75 74 20 69 73 20 61 6e 20 65 6d  e input is an em
29360 70 74 79 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 72  pty.** string or
29370 20 61 20 63 6f 6d 6d 65 6e 74 29 20 74 68 65 6e   a comment) then
29380 20 2a 70 70 53 74 6d 74 20 69 73 20 73 65 74 20   *ppStmt is set 
29390 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 54 68 65 20  to NULL..** The 
293a0 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72  calling procedur
293b0 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  e is responsible
293c0 20 66 6f 72 20 64 65 6c 65 74 69 6e 67 20 74 68   for deleting th
293d0 65 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 53 51  e compiled.** SQ
293e0 4c 20 73 74 61 74 65 6d 65 6e 74 20 75 73 69 6e  L statement usin
293f0 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  g [sqlite3_final
29400 69 7a 65 28 29 5d 20 61 66 74 65 72 20 69 74 20  ize()] after it 
29410 68 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 74  has finished wit
29420 68 20 69 74 2e 0a 2a 2a 20 70 70 53 74 6d 74 20  h it..** ppStmt 
29430 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e  may not be NULL.
29440 0a 2a 2a 0a 2a 2a 20 5e 4f 6e 20 73 75 63 63 65  .**.** ^On succe
29450 73 73 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ss, the sqlite3_
29460 70 72 65 70 61 72 65 28 29 20 66 61 6d 69 6c 79  prepare() family
29470 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 72 65 74   of routines ret
29480 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 3b  urn [SQLITE_OK];
29490 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 61 6e  .** otherwise an
294a0 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73   [error code] is
294b0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
294c0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65   The sqlite3_pre
294d0 70 61 72 65 5f 76 32 28 29 20 61 6e 64 20 73 71  pare_v2() and sq
294e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
294f0 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 73 20  v2() interfaces 
29500 61 72 65 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64  are.** recommend
29510 65 64 20 66 6f 72 20 61 6c 6c 20 6e 65 77 20 70  ed for all new p
29520 72 6f 67 72 61 6d 73 2e 20 54 68 65 20 74 77 6f  rograms. The two
29530 20 6f 6c 64 65 72 20 69 6e 74 65 72 66 61 63 65   older interface
29540 73 20 61 72 65 20 72 65 74 61 69 6e 65 64 0a 2a  s are retained.*
29550 2a 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 20  * for backwards 
29560 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2c 20 62  compatibility, b
29570 75 74 20 74 68 65 69 72 20 75 73 65 20 69 73 20  ut their use is 
29580 64 69 73 63 6f 75 72 61 67 65 64 2e 0a 2a 2a 20  discouraged..** 
29590 5e 49 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74  ^In the "v2" int
295a0 65 72 66 61 63 65 73 2c 20 74 68 65 20 70 72 65  erfaces, the pre
295b0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a  pared statement.
295c0 2a 2a 20 74 68 61 74 20 69 73 20 72 65 74 75 72  ** that is retur
295d0 6e 65 64 20 28 74 68 65 20 5b 73 71 6c 69 74 65  ned (the [sqlite
295e0 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 29 20  3_stmt] object) 
295f0 63 6f 6e 74 61 69 6e 73 20 61 20 63 6f 70 79 20  contains a copy 
29600 6f 66 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e  of the.** origin
29610 61 6c 20 53 51 4c 20 74 65 78 74 2e 20 54 68 69  al SQL text. Thi
29620 73 20 63 61 75 73 65 73 20 74 68 65 20 5b 73 71  s causes the [sq
29630 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 69 6e  lite3_step()] in
29640 74 65 72 66 61 63 65 20 74 6f 0a 2a 2a 20 62 65  terface to.** be
29650 68 61 76 65 20 64 69 66 66 65 72 65 6e 74 6c 79  have differently
29660 20 69 6e 20 74 68 72 65 65 20 77 61 79 73 3a 0a   in three ways:.
29670 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c  **.** <ol>.** <l
29680 69 3e 0a 2a 2a 20 5e 49 66 20 74 68 65 20 64 61  i>.** ^If the da
29690 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 63 68  tabase schema ch
296a0 61 6e 67 65 73 2c 20 69 6e 73 74 65 61 64 20 6f  anges, instead o
296b0 66 20 72 65 74 75 72 6e 69 6e 67 20 5b 53 51 4c  f returning [SQL
296c0 49 54 45 5f 53 43 48 45 4d 41 5d 20 61 73 20 69  ITE_SCHEMA] as i
296d0 74 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 64  t.** always used
296e0 20 74 6f 20 64 6f 2c 20 5b 73 71 6c 69 74 65 33   to do, [sqlite3
296f0 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 61 75  _step()] will au
29700 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 6f  tomatically reco
29710 6d 70 69 6c 65 20 74 68 65 20 53 51 4c 0a 2a 2a  mpile the SQL.**
29720 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74   statement and t
29730 72 79 20 74 6f 20 72 75 6e 20 69 74 20 61 67 61  ry to run it aga
29740 69 6e 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 0a  in..** </li>.**.
29750 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 5e 57 68 65 6e  ** <li>.** ^When
29760 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
29770 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  , [sqlite3_step(
29780 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 6f  )] will return o
29790 6e 65 20 6f 66 20 74 68 65 20 64 65 74 61 69 6c  ne of the detail
297a0 65 64 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64  ed.** [error cod
297b0 65 73 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64  es] or [extended
297c0 20 65 72 72 6f 72 20 63 6f 64 65 73 5d 2e 20 20   error codes].  
297d0 5e 54 68 65 20 6c 65 67 61 63 79 20 62 65 68 61  ^The legacy beha
297e0 76 69 6f 72 20 77 61 73 20 74 68 61 74 0a 2a 2a  vior was that.**
297f0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
29800 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 72 65 74  ] would only ret
29810 75 72 6e 20 61 20 67 65 6e 65 72 69 63 20 5b 53  urn a generic [S
29820 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 72 65 73  QLITE_ERROR] res
29830 75 6c 74 20 63 6f 64 65 0a 2a 2a 20 61 6e 64 20  ult code.** and 
29840 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
29850 77 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 6d 61  would have to ma
29860 6b 65 20 61 20 73 65 63 6f 6e 64 20 63 61 6c 6c  ke a second call
29870 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73   to [sqlite3_res
29880 65 74 28 29 5d 0a 2a 2a 20 69 6e 20 6f 72 64 65  et()].** in orde
29890 72 20 74 6f 20 66 69 6e 64 20 74 68 65 20 75 6e  r to find the un
298a0 64 65 72 6c 79 69 6e 67 20 63 61 75 73 65 20 6f  derlying cause o
298b0 66 20 74 68 65 20 70 72 6f 62 6c 65 6d 2e 20 57  f the problem. W
298c0 69 74 68 20 74 68 65 20 22 76 32 22 20 70 72 65  ith the "v2" pre
298d0 70 61 72 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63  pare.** interfac
298e0 65 73 2c 20 74 68 65 20 75 6e 64 65 72 6c 79 69  es, the underlyi
298f0 6e 67 20 72 65 61 73 6f 6e 20 66 6f 72 20 74 68  ng reason for th
29900 65 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  e error is retur
29910 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e  ned immediately.
29920 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20  .** </li>.**.** 
29930 3c 6c 69 3e 0a 2a 2a 20 5e 49 66 20 74 68 65 20  <li>.** ^If the 
29940 73 70 65 63 69 66 69 63 20 76 61 6c 75 65 20 62  specific value b
29950 6f 75 6e 64 20 74 6f 20 5b 70 61 72 61 6d 65 74  ound to [paramet
29960 65 72 20 7c 20 68 6f 73 74 20 70 61 72 61 6d 65  er | host parame
29970 74 65 72 5d 20 69 6e 20 74 68 65 20 0a 2a 2a 20  ter] in the .** 
29980 57 48 45 52 45 20 63 6c 61 75 73 65 20 6d 69 67  WHERE clause mig
29990 68 74 20 69 6e 66 6c 75 65 6e 63 65 20 74 68 65  ht influence the
299a0 20 63 68 6f 69 63 65 20 6f 66 20 71 75 65 72 79   choice of query
299b0 20 70 6c 61 6e 20 66 6f 72 20 61 20 73 74 61 74   plan for a stat
299c0 65 6d 65 6e 74 2c 0a 2a 2a 20 74 68 65 6e 20 74  ement,.** then t
299d0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c  he statement wil
299e0 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  l be automatical
299f0 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 2c 20 61  ly recompiled, a
29a00 73 20 69 66 20 74 68 65 72 65 20 68 61 64 20 62  s if there had b
29a10 65 65 6e 20 0a 2a 2a 20 61 20 73 63 68 65 6d 61  een .** a schema
29a20 20 63 68 61 6e 67 65 2c 20 6f 6e 20 74 68 65 20   change, on the 
29a30 66 69 72 73 74 20 20 5b 73 71 6c 69 74 65 33 5f  first  [sqlite3_
29a40 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 66 6f 6c  step()] call fol
29a50 6c 6f 77 69 6e 67 20 61 6e 79 20 63 68 61 6e 67  lowing any chang
29a60 65 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73 71 6c  e.** to the [sql
29a70 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20 7c  ite3_bind_text |
29a80 20 62 69 6e 64 69 6e 67 73 5d 20 6f 66 20 74 68   bindings] of th
29a90 61 74 20 5b 70 61 72 61 6d 65 74 65 72 5d 2e 20  at [parameter]. 
29aa0 0a 2a 2a 20 5e 54 68 65 20 73 70 65 63 69 66 69  .** ^The specifi
29ab0 63 20 76 61 6c 75 65 20 6f 66 20 57 48 45 52 45  c value of WHERE
29ac0 2d 63 6c 61 75 73 65 20 5b 70 61 72 61 6d 65 74  -clause [paramet
29ad0 65 72 5d 20 6d 69 67 68 74 20 69 6e 66 6c 75 65  er] might influe
29ae0 6e 63 65 20 74 68 65 20 0a 2a 2a 20 63 68 6f 69  nce the .** choi
29af0 63 65 20 6f 66 20 71 75 65 72 79 20 70 6c 61 6e  ce of query plan
29b00 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   if the paramete
29b10 72 20 69 73 20 74 68 65 20 6c 65 66 74 2d 68 61  r is the left-ha
29b20 6e 64 20 73 69 64 65 20 6f 66 20 61 20 5b 4c 49  nd side of a [LI
29b30 4b 45 5d 0a 2a 2a 20 6f 72 20 5b 47 4c 4f 42 5d  KE].** or [GLOB]
29b40 20 6f 70 65 72 61 74 6f 72 20 6f 72 20 69 66 20   operator or if 
29b50 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73  the parameter is
29b60 20 63 6f 6d 70 61 72 65 64 20 74 6f 20 61 6e 20   compared to an 
29b70 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 0a 2a  indexed column.*
29b80 2a 20 61 6e 64 20 74 68 65 20 5b 53 51 4c 49 54  * and the [SQLIT
29b90 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 5d 20  E_ENABLE_STAT3] 
29ba0 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74  compile-time opt
29bb0 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a  ion is enabled..
29bc0 2a 2a 20 74 68 65 20 0a 2a 2a 20 3c 2f 6c 69 3e  ** the .** </li>
29bd0 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2f 0a 53 51 4c  .** </ol>.*/.SQL
29be0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
29bf0 74 65 33 5f 70 72 65 70 61 72 65 28 0a 20 20 73  te3_prepare(.  s
29c00 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
29c10 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
29c20 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63  se handle */.  c
29c30 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c  onst char *zSql,
29c40 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74         /* SQL st
29c50 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 38 20 65  atement, UTF-8 e
29c60 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ncoded */.  int 
29c70 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20  nByte,          
29c80 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c      /* Maximum l
29c90 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e  ength of zSql in
29ca0 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c   bytes. */.  sql
29cb0 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
29cc0 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61  mt,  /* OUT: Sta
29cd0 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f  tement handle */
29ce0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
29cf0 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55  pzTail     /* OU
29d00 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e  T: Pointer to un
29d10 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20  used portion of 
29d20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54  zSql */.);.SQLIT
29d30 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
29d40 33 5f 70 72 65 70 61 72 65 5f 76 32 28 0a 20 20  3_prepare_v2(.  
29d50 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
29d60 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
29d70 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ase handle */.  
29d80 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
29d90 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73  ,       /* SQL s
29da0 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 38 20  tatement, UTF-8 
29db0 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74  encoded */.  int
29dc0 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20   nByte,         
29dd0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
29de0 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69  length of zSql i
29df0 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71  n bytes. */.  sq
29e00 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
29e10 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74  tmt,  /* OUT: St
29e20 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a  atement handle *
29e30 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
29e40 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f  *pzTail     /* O
29e50 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75  UT: Pointer to u
29e60 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66  nused portion of
29e70 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49   zSql */.);.SQLI
29e80 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
29e90 65 33 5f 70 72 65 70 61 72 65 31 36 28 0a 20 20  e3_prepare16(.  
29ea0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
29eb0 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
29ec0 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ase handle */.  
29ed0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c  const void *zSql
29ee0 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73  ,       /* SQL s
29ef0 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36  tatement, UTF-16
29f00 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e   encoded */.  in
29f10 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20  t nByte,        
29f20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
29f30 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20   length of zSql 
29f40 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73  in bytes. */.  s
29f50 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
29f60 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53  Stmt,  /* OUT: S
29f70 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20  tatement handle 
29f80 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
29f90 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20  **pzTail     /* 
29fa0 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20  OUT: Pointer to 
29fb0 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f  unused portion o
29fc0 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c  f zSql */.);.SQL
29fd0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
29fe0 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
29ff0 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
2a000 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
2a010 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
2a020 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
2a030 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53  zSql,       /* S
2a040 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54  QL statement, UT
2a050 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f 0a  F-16 encoded */.
2a060 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
2a070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
2a080 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a  imum length of z
2a090 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  Sql in bytes. */
2a0a0 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2a0b0 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55  **ppStmt,  /* OU
2a0c0 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e  T: Statement han
2a0d0 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  dle */.  const v
2a0e0 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  oid **pzTail    
2a0f0 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72   /* OUT: Pointer
2a100 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69   to unused porti
2a110 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b  on of zSql */.);
2a120 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
2a130 3a 20 52 65 74 72 69 65 76 69 6e 67 20 53 74 61  : Retrieving Sta
2a140 74 65 6d 65 6e 74 20 53 51 4c 0a 2a 2a 0a 2a 2a  tement SQL.**.**
2a150 20 5e 54 68 69 73 20 69 6e 74 65 72 66 61 63 65   ^This interface
2a160 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
2a170 72 65 74 72 69 65 76 65 20 61 20 73 61 76 65 64  retrieve a saved
2a180 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6f 72 69   copy of the ori
2a190 67 69 6e 61 6c 0a 2a 2a 20 53 51 4c 20 74 65 78  ginal.** SQL tex
2a1a0 74 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65  t used to create
2a1b0 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61   a [prepared sta
2a1c0 74 65 6d 65 6e 74 5d 20 69 66 20 74 68 61 74 20  tement] if that 
2a1d0 73 74 61 74 65 6d 65 6e 74 20 77 61 73 0a 2a 2a  statement was.**
2a1e0 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20   compiled using 
2a1f0 65 69 74 68 65 72 20 5b 73 71 6c 69 74 65 33 5f  either [sqlite3_
2a200 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72  prepare_v2()] or
2a210 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
2a220 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2f 0a 53 51  e16_v2()]..*/.SQ
2a230 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
2a240 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c  har *sqlite3_sql
2a250 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
2a260 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  Stmt);../*.** CA
2a270 50 49 33 52 45 46 3a 20 44 65 74 65 72 6d 69 6e  PI3REF: Determin
2a280 65 20 49 66 20 41 6e 20 53 51 4c 20 53 74 61 74  e If An SQL Stat
2a290 65 6d 65 6e 74 20 57 72 69 74 65 73 20 54 68 65  ement Writes The
2a2a0 20 44 61 74 61 62 61 73 65 0a 2a 2a 0a 2a 2a 20   Database.**.** 
2a2b0 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d  ^The sqlite3_stm
2a2c0 74 5f 72 65 61 64 6f 6e 6c 79 28 58 29 20 69 6e  t_readonly(X) in
2a2d0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
2a2e0 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20  true (non-zero) 
2a2f0 69 66 0a 2a 2a 20 61 6e 64 20 6f 6e 6c 79 20 69  if.** and only i
2a300 66 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20  f the [prepared 
2a310 73 74 61 74 65 6d 65 6e 74 5d 20 58 20 6d 61 6b  statement] X mak
2a320 65 73 20 6e 6f 20 64 69 72 65 63 74 20 63 68 61  es no direct cha
2a330 6e 67 65 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63  nges to.** the c
2a340 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 64 61  ontent of the da
2a350 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
2a360 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 61 70  ** Note that [ap
2a370 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2a380 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d  d SQL functions]
2a390 20 6f 72 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20   or.** [virtual 
2a3a0 74 61 62 6c 65 73 5d 20 6d 69 67 68 74 20 63 68  tables] might ch
2a3b0 61 6e 67 65 20 74 68 65 20 64 61 74 61 62 61 73  ange the databas
2a3c0 65 20 69 6e 64 69 72 65 63 74 6c 79 20 61 73 20  e indirectly as 
2a3d0 61 20 73 69 64 65 20 65 66 66 65 63 74 2e 20 20  a side effect.  
2a3e0 0a 2a 2a 20 5e 28 46 6f 72 20 65 78 61 6d 70 6c  .** ^(For exampl
2a3f0 65 2c 20 69 66 20 61 6e 20 61 70 70 6c 69 63 61  e, if an applica
2a400 74 69 6f 6e 20 64 65 66 69 6e 65 73 20 61 20 66  tion defines a f
2a410 75 6e 63 74 69 6f 6e 20 22 65 76 61 6c 28 29 22  unction "eval()"
2a420 20 74 68 61 74 20 0a 2a 2a 20 63 61 6c 6c 73 20   that .** calls 
2a430 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d  [sqlite3_exec()]
2a440 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f  , then the follo
2a450 77 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d 65  wing SQL stateme
2a460 6e 74 20 77 6f 75 6c 64 0a 2a 2a 20 63 68 61 6e  nt would.** chan
2a470 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ge the database 
2a480 66 69 6c 65 20 74 68 72 6f 75 67 68 20 73 69 64  file through sid
2a490 65 2d 65 66 66 65 63 74 73 3a 0a 2a 2a 0a 2a 2a  e-effects:.**.**
2a4a0 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72   <blockquote><pr
2a4b0 65 3e 0a 2a 2a 20 20 20 20 53 45 4c 45 43 54 20  e>.**    SELECT 
2a4c0 65 76 61 6c 28 27 44 45 4c 45 54 45 20 46 52 4f  eval('DELETE FRO
2a4d0 4d 20 74 31 27 29 20 46 52 4f 4d 20 74 32 3b 0a  M t1') FROM t2;.
2a4e0 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b  ** </pre></block
2a4f0 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 42 75 74  quote>.**.** But
2a500 20 62 65 63 61 75 73 65 20 74 68 65 20 5b 53 45   because the [SE
2a510 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 20  LECT] statement 
2a520 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20  does not change 
2a530 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
2a540 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2c 20 73  e.** directly, s
2a550 71 6c 69 74 65 33 5f 73 74 6d 74 5f 72 65 61 64  qlite3_stmt_read
2a560 6f 6e 6c 79 28 29 20 77 6f 75 6c 64 20 73 74 69  only() would sti
2a570 6c 6c 20 72 65 74 75 72 6e 20 74 72 75 65 2e 29  ll return true.)
2a580 5e 0a 2a 2a 0a 2a 2a 20 5e 54 72 61 6e 73 61 63  ^.**.** ^Transac
2a590 74 69 6f 6e 20 63 6f 6e 74 72 6f 6c 20 73 74 61  tion control sta
2a5a0 74 65 6d 65 6e 74 73 20 73 75 63 68 20 61 73 20  tements such as 
2a5b0 5b 42 45 47 49 4e 5d 2c 20 5b 43 4f 4d 4d 49 54  [BEGIN], [COMMIT
2a5c0 5d 2c 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2c 0a 2a  ], [ROLLBACK],.*
2a5d0 2a 20 5b 53 41 56 45 50 4f 49 4e 54 5d 2c 20 61  * [SAVEPOINT], a
2a5e0 6e 64 20 5b 52 45 4c 45 41 53 45 5d 20 63 61 75  nd [RELEASE] cau
2a5f0 73 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f  se sqlite3_stmt_
2a600 72 65 61 64 6f 6e 6c 79 28 29 20 74 6f 20 72 65  readonly() to re
2a610 74 75 72 6e 20 74 72 75 65 2c 0a 2a 2a 20 73 69  turn true,.** si
2a620 6e 63 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e  nce the statemen
2a630 74 73 20 74 68 65 6d 73 65 6c 76 65 73 20 64 6f  ts themselves do
2a640 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f   not actually mo
2a650 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73  dify the databas
2a660 65 20 62 75 74 0a 2a 2a 20 72 61 74 68 65 72 20  e but.** rather 
2a670 74 68 65 79 20 63 6f 6e 74 72 6f 6c 20 74 68 65  they control the
2a680 20 74 69 6d 69 6e 67 20 6f 66 20 77 68 65 6e 20   timing of when 
2a690 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73  other statements
2a6a0 20 6d 6f 64 69 66 79 20 74 68 65 20 0a 2a 2a 20   modify the .** 
2a6b0 64 61 74 61 62 61 73 65 2e 20 20 5e 54 68 65 20  database.  ^The 
2a6c0 5b 41 54 54 41 43 48 5d 20 61 6e 64 20 5b 44 45  [ATTACH] and [DE
2a6d0 54 41 43 48 5d 20 73 74 61 74 65 6d 65 6e 74 73  TACH] statements
2a6e0 20 61 6c 73 6f 20 63 61 75 73 65 0a 2a 2a 20 73   also cause.** s
2a6f0 71 6c 69 74 65 33 5f 73 74 6d 74 5f 72 65 61 64  qlite3_stmt_read
2a700 6f 6e 6c 79 28 29 20 74 6f 20 72 65 74 75 72 6e  only() to return
2a710 20 74 72 75 65 20 73 69 6e 63 65 2c 20 77 68 69   true since, whi
2a720 6c 65 20 74 68 6f 73 65 20 73 74 61 74 65 6d 65  le those stateme
2a730 6e 74 73 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68  nts.** change th
2a740 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  e configuration 
2a750 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  of a database co
2a760 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 79 20 64  nnection, they d
2a770 6f 20 6e 6f 74 20 6d 61 6b 65 20 0a 2a 2a 20 63  o not make .** c
2a780 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 63 6f  hanges to the co
2a790 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 64 61 74  ntent of the dat
2a7a0 61 62 61 73 65 20 66 69 6c 65 73 20 6f 6e 20 64  abase files on d
2a7b0 69 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  isk..*/.SQLITE_A
2a7c0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
2a7d0 74 6d 74 5f 72 65 61 64 6f 6e 6c 79 28 73 71 6c  tmt_readonly(sql
2a7e0 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
2a7f0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
2a800 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 20 49 66  EF: Determine If
2a810 20 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74   A Prepared Stat
2a820 65 6d 65 6e 74 20 48 61 73 20 42 65 65 6e 20 52  ement Has Been R
2a830 65 73 65 74 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  eset.**.** ^The 
2a840 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 62 75 73  sqlite3_stmt_bus
2a850 79 28 53 29 20 69 6e 74 65 72 66 61 63 65 20 72  y(S) interface r
2a860 65 74 75 72 6e 73 20 74 72 75 65 20 28 6e 6f 6e  eturns true (non
2a870 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 0a 2a 2a  -zero) if the.**
2a880 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
2a890 6d 65 6e 74 5d 20 53 20 68 61 73 20 62 65 65 6e  ment] S has been
2a8a0 20 73 74 65 70 70 65 64 20 61 74 20 6c 65 61 73   stepped at leas
2a8b0 74 20 6f 6e 63 65 20 75 73 69 6e 67 20 0a 2a 2a  t once using .**
2a8c0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53   [sqlite3_step(S
2a8d0 29 5d 20 62 75 74 20 68 61 73 20 6e 6f 74 20 72  )] but has not r
2a8e0 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e  un to completion
2a8f0 20 61 6e 64 2f 6f 72 20 68 61 73 20 6e 6f 74 20   and/or has not 
2a900 0a 2a 2a 20 62 65 65 6e 20 72 65 73 65 74 20 75  .** been reset u
2a910 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65  sing [sqlite3_re
2a920 73 65 74 28 53 29 5d 2e 20 20 5e 54 68 65 20 73  set(S)].  ^The s
2a930 71 6c 69 74 65 33 5f 73 74 6d 74 5f 62 75 73 79  qlite3_stmt_busy
2a940 28 53 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  (S).** interface
2a950 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 20 69   returns false i
2a960 66 20 53 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f  f S is a NULL po
2a970 69 6e 74 65 72 2e 20 20 49 66 20 53 20 69 73 20  inter.  If S is 
2a980 6e 6f 74 20 61 20 0a 2a 2a 20 4e 55 4c 4c 20 70  not a .** NULL p
2a990 6f 69 6e 74 65 72 20 61 6e 64 20 69 73 20 6e 6f  ointer and is no
2a9a0 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  t a pointer to a
2a9b0 20 76 61 6c 69 64 20 5b 70 72 65 70 61 72 65 64   valid [prepared
2a9c0 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 6f   statement].** o
2a9d0 62 6a 65 63 74 2c 20 74 68 65 6e 20 74 68 65 20  bject, then the 
2a9e0 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65  behavior is unde
2a9f0 66 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61 62  fined and probab
2aa00 6c 79 20 75 6e 64 65 73 69 72 61 62 6c 65 2e 0a  ly undesirable..
2aa10 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
2aa20 66 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 64  face can be used
2aa30 20 69 6e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20   in combination 
2aa40 5b 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74  [sqlite3_next_st
2aa50 6d 74 28 29 5d 0a 2a 2a 20 74 6f 20 6c 6f 63 61  mt()].** to loca
2aa60 74 65 20 61 6c 6c 20 70 72 65 70 61 72 65 64 20  te all prepared 
2aa70 73 74 61 74 65 6d 65 6e 74 73 20 61 73 73 6f 63  statements assoc
2aa80 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74  iated with a dat
2aa90 61 62 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63  abase .** connec
2aaa0 74 69 6f 6e 20 74 68 61 74 20 61 72 65 20 69 6e  tion that are in
2aab0 20 6e 65 65 64 20 6f 66 20 62 65 69 6e 67 20 72   need of being r
2aac0 65 73 65 74 2e 20 20 54 68 69 73 20 63 61 6e 20  eset.  This can 
2aad0 62 65 20 75 73 65 64 2c 0a 2a 2a 20 66 6f 72 20  be used,.** for 
2aae0 65 78 61 6d 70 6c 65 2c 20 69 6e 20 64 69 61 67  example, in diag
2aaf0 6e 6f 73 74 69 63 20 72 6f 75 74 69 6e 65 73 20  nostic routines 
2ab00 74 6f 20 73 65 61 72 63 68 20 66 6f 72 20 70 72  to search for pr
2ab10 65 70 61 72 65 64 20 0a 2a 2a 20 73 74 61 74 65  epared .** state
2ab20 6d 65 6e 74 73 20 74 68 61 74 20 61 72 65 20 68  ments that are h
2ab30 6f 6c 64 69 6e 67 20 61 20 74 72 61 6e 73 61 63  olding a transac
2ab40 74 69 6f 6e 20 6f 70 65 6e 2e 0a 2a 2f 0a 53 51  tion open..*/.SQ
2ab50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2ab60 69 74 65 33 5f 73 74 6d 74 5f 62 75 73 79 28 73  ite3_stmt_busy(s
2ab70 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a  qlite3_stmt*);..
2ab80 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2ab90 44 79 6e 61 6d 69 63 61 6c 6c 79 20 54 79 70 65  Dynamically Type
2aba0 64 20 56 61 6c 75 65 20 4f 62 6a 65 63 74 0a 2a  d Value Object.*
2abb0 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 6f  * KEYWORDS: {pro
2abc0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
2abd0 61 6c 75 65 7d 20 7b 75 6e 70 72 6f 74 65 63 74  alue} {unprotect
2abe0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
2abf0 7d 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75  }.**.** SQLite u
2ac00 73 65 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ses the sqlite3_
2ac10 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 74 6f 20  value object to 
2ac20 72 65 70 72 65 73 65 6e 74 20 61 6c 6c 20 76 61  represent all va
2ac30 6c 75 65 73 0a 2a 2a 20 74 68 61 74 20 63 61 6e  lues.** that can
2ac40 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20   be stored in a 
2ac50 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 20  database table. 
2ac60 53 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e 61  SQLite uses dyna
2ac70 6d 69 63 20 74 79 70 69 6e 67 0a 2a 2a 20 66 6f  mic typing.** fo
2ac80 72 20 74 68 65 20 76 61 6c 75 65 73 20 69 74 20  r the values it 
2ac90 73 74 6f 72 65 73 2e 20 20 5e 56 61 6c 75 65 73  stores.  ^Values
2aca0 20 73 74 6f 72 65 64 20 69 6e 20 73 71 6c 69 74   stored in sqlit
2acb0 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73  e3_value objects
2acc0 0a 2a 2a 20 63 61 6e 20 62 65 20 69 6e 74 65 67  .** can be integ
2acd0 65 72 73 2c 20 66 6c 6f 61 74 69 6e 67 20 70 6f  ers, floating po
2ace0 69 6e 74 20 76 61 6c 75 65 73 2c 20 73 74 72 69  int values, stri
2acf0 6e 67 73 2c 20 42 4c 4f 42 73 2c 20 6f 72 20 4e  ngs, BLOBs, or N
2ad00 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 73 71  ULL..**.** An sq
2ad10 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
2ad20 63 74 20 6d 61 79 20 62 65 20 65 69 74 68 65 72  ct may be either
2ad30 20 22 70 72 6f 74 65 63 74 65 64 22 20 6f 72 20   "protected" or 
2ad40 22 75 6e 70 72 6f 74 65 63 74 65 64 22 2e 0a 2a  "unprotected"..*
2ad50 2a 20 53 6f 6d 65 20 69 6e 74 65 72 66 61 63 65  * Some interface
2ad60 73 20 72 65 71 75 69 72 65 20 61 20 70 72 6f 74  s require a prot
2ad70 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
2ad80 6c 75 65 2e 20 20 4f 74 68 65 72 20 69 6e 74 65  lue.  Other inte
2ad90 72 66 61 63 65 73 0a 2a 2a 20 77 69 6c 6c 20 61  rfaces.** will a
2ada0 63 63 65 70 74 20 65 69 74 68 65 72 20 61 20 70  ccept either a p
2adb0 72 6f 74 65 63 74 65 64 20 6f 72 20 61 6e 20 75  rotected or an u
2adc0 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  nprotected sqlit
2add0 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a 20 45 76 65  e3_value..** Eve
2ade0 72 79 20 69 6e 74 65 72 66 61 63 65 20 74 68 61  ry interface tha
2adf0 74 20 61 63 63 65 70 74 73 20 73 71 6c 69 74 65  t accepts sqlite
2ae00 33 5f 76 61 6c 75 65 20 61 72 67 75 6d 65 6e 74  3_value argument
2ae10 73 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 77  s specifies.** w
2ae20 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 69 74  hether or not it
2ae30 20 72 65 71 75 69 72 65 73 20 61 20 70 72 6f 74   requires a prot
2ae40 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
2ae50 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  lue..**.** The t
2ae60 65 72 6d 73 20 22 70 72 6f 74 65 63 74 65 64 22  erms "protected"
2ae70 20 61 6e 64 20 22 75 6e 70 72 6f 74 65 63 74 65   and "unprotecte
2ae80 64 22 20 72 65 66 65 72 20 74 6f 20 77 68 65 74  d" refer to whet
2ae90 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 61 20  her or not.** a 
2aea0 6d 75 74 65 78 20 69 73 20 68 65 6c 64 2e 20 20  mutex is held.  
2aeb0 41 6e 20 69 6e 74 65 72 6e 61 6c 20 6d 75 74 65  An internal mute
2aec0 78 20 69 73 20 68 65 6c 64 20 66 6f 72 20 61 20  x is held for a 
2aed0 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c  protected.** sql
2aee0 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
2aef0 74 20 62 75 74 20 6e 6f 20 6d 75 74 65 78 20 69  t but no mutex i
2af00 73 20 68 65 6c 64 20 66 6f 72 20 61 6e 20 75 6e  s held for an un
2af10 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c  protected.** sql
2af20 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
2af30 74 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69 73  t.  If SQLite is
2af40 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 62 65 20   compiled to be 
2af50 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 0a  single-threaded.
2af60 2a 2a 20 28 77 69 74 68 20 5b 53 51 4c 49 54 45  ** (with [SQLITE
2af70 5f 54 48 52 45 41 44 53 41 46 45 3d 30 5d 20 61  _THREADSAFE=0] a
2af80 6e 64 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33  nd with [sqlite3
2af90 5f 74 68 72 65 61 64 73 61 66 65 28 29 5d 20 72  _threadsafe()] r
2afa0 65 74 75 72 6e 69 6e 67 20 30 29 0a 2a 2a 20 6f  eturning 0).** o
2afb0 72 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 72  r if SQLite is r
2afc0 75 6e 20 69 6e 20 6f 6e 65 20 6f 66 20 72 65 64  un in one of red
2afd0 75 63 65 64 20 6d 75 74 65 78 20 6d 6f 64 65 73  uced mutex modes
2afe0 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e   .** [SQLITE_CON
2aff0 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44  FIG_SINGLETHREAD
2b000 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  ] or [SQLITE_CON
2b010 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d  FIG_MULTITHREAD]
2b020 0a 2a 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69  .** then there i
2b030 73 20 6e 6f 20 64 69 73 74 69 6e 63 74 69 6f 6e  s no distinction
2b040 20 62 65 74 77 65 65 6e 20 70 72 6f 74 65 63 74   between protect
2b050 65 64 20 61 6e 64 20 75 6e 70 72 6f 74 65 63 74  ed and unprotect
2b060 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61  ed.** sqlite3_va
2b070 6c 75 65 20 6f 62 6a 65 63 74 73 20 61 6e 64 20  lue objects and 
2b080 74 68 65 79 20 63 61 6e 20 62 65 20 75 73 65 64  they can be used
2b090 20 69 6e 74 65 72 63 68 61 6e 67 65 61 62 6c 79   interchangeably
2b0a0 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 66  .  However,.** f
2b0b0 6f 72 20 6d 61 78 69 6d 75 6d 20 63 6f 64 65 20  or maximum code 
2b0c0 70 6f 72 74 61 62 69 6c 69 74 79 20 69 74 20 69  portability it i
2b0d0 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68  s recommended th
2b0e0 61 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a  at applications.
2b0f0 2a 2a 20 73 74 69 6c 6c 20 6d 61 6b 65 20 74 68  ** still make th
2b100 65 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65  e distinction be
2b110 74 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20  tween protected 
2b120 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a  and unprotected.
2b130 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ** sqlite3_value
2b140 20 6f 62 6a 65 63 74 73 20 65 76 65 6e 20 77 68   objects even wh
2b150 65 6e 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20  en not strictly 
2b160 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  required..**.** 
2b170 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  ^The sqlite3_val
2b180 75 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20  ue objects that 
2b190 61 72 65 20 70 61 73 73 65 64 20 61 73 20 70 61  are passed as pa
2b1a0 72 61 6d 65 74 65 72 73 20 69 6e 74 6f 20 74 68  rameters into th
2b1b0 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  e.** implementat
2b1c0 69 6f 6e 20 6f 66 20 5b 61 70 70 6c 69 63 61 74  ion of [applicat
2b1d0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20  ion-defined SQL 
2b1e0 66 75 6e 63 74 69 6f 6e 73 5d 20 61 72 65 20 70  functions] are p
2b1f0 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 5e 54 68  rotected..** ^Th
2b200 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  e sqlite3_value 
2b210 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20  object returned 
2b220 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  by.** [sqlite3_c
2b230 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69  olumn_value()] i
2b240 73 20 75 6e 70 72 6f 74 65 63 74 65 64 2e 0a 2a  s unprotected..*
2b250 2a 20 55 6e 70 72 6f 74 65 63 74 65 64 20 73 71  * Unprotected sq
2b260 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
2b270 63 74 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20  cts may only be 
2b280 75 73 65 64 20 77 69 74 68 0a 2a 2a 20 5b 73 71  used with.** [sq
2b290 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
2b2a0 75 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  ue()] and [sqlit
2b2b0 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d  e3_bind_value()]
2b2c0 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65  ..** The [sqlite
2b2d0 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 7c 20 73  3_value_blob | s
2b2e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
2b2f0 65 28 29 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a  e()] family of.*
2b300 2a 20 69 6e 74 65 72 66 61 63 65 73 20 72 65 71  * interfaces req
2b310 75 69 72 65 20 70 72 6f 74 65 63 74 65 64 20 73  uire protected s
2b320 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
2b330 65 63 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ects..*/.typedef
2b340 20 73 74 72 75 63 74 20 4d 65 6d 20 73 71 6c 69   struct Mem sqli
2b350 74 65 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a  te3_value;../*.*
2b360 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20  * CAPI3REF: SQL 
2b370 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74  Function Context
2b380 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68   Object.**.** Th
2b390 65 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69  e context in whi
2b3a0 63 68 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  ch an SQL functi
2b3b0 6f 6e 20 65 78 65 63 75 74 65 73 20 69 73 20 73  on executes is s
2b3c0 74 6f 72 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 73  tored in an.** s
2b3d0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f  qlite3_context o
2b3e0 62 6a 65 63 74 2e 20 20 5e 41 20 70 6f 69 6e 74  bject.  ^A point
2b3f0 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33  er to an sqlite3
2b400 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 0a  _context object.
2b410 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 66 69 72  ** is always fir
2b420 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  st parameter to 
2b430 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66  [application-def
2b440 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f  ined SQL functio
2b450 6e 73 5d 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c  ns]..** The appl
2b460 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2b470 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  SQL function imp
2b480 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c  lementation will
2b490 20 70 61 73 73 20 74 68 69 73 0a 2a 2a 20 70 6f   pass this.** po
2b4a0 69 6e 74 65 72 20 74 68 72 6f 75 67 68 20 69 6e  inter through in
2b4b0 74 6f 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c  to calls to [sql
2b4c0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
2b4d0 7c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  | sqlite3_result
2b4e0 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ()],.** [sqlite3
2b4f0 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
2b500 78 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  xt()], [sqlite3_
2b510 75 73 65 72 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a  user_data()],.**
2b520 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   [sqlite3_contex
2b530 74 5f 64 62 5f 68 61 6e 64 6c 65 28 29 5d 2c 20  t_db_handle()], 
2b540 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78  [sqlite3_get_aux
2b550 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f  data()],.** and/
2b560 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f  or [sqlite3_set_
2b570 61 75 78 64 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74  auxdata()]..*/.t
2b580 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
2b590 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 71  lite3_context sq
2b5a0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a  lite3_context;..
2b5b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2b5c0 42 69 6e 64 69 6e 67 20 56 61 6c 75 65 73 20 54  Binding Values T
2b5d0 6f 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65  o Prepared State
2b5e0 6d 65 6e 74 73 0a 2a 2a 20 4b 45 59 57 4f 52 44  ments.** KEYWORD
2b5f0 53 3a 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74  S: {host paramet
2b600 65 72 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65  er} {host parame
2b610 74 65 72 73 7d 20 7b 68 6f 73 74 20 70 61 72 61  ters} {host para
2b620 6d 65 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20 4b  meter name}.** K
2b630 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 70 61  EYWORDS: {SQL pa
2b640 72 61 6d 65 74 65 72 7d 20 7b 53 51 4c 20 70 61  rameter} {SQL pa
2b650 72 61 6d 65 74 65 72 73 7d 20 7b 70 61 72 61 6d  rameters} {param
2b660 65 74 65 72 20 62 69 6e 64 69 6e 67 7d 0a 2a 2a  eter binding}.**
2b670 0a 2a 2a 20 5e 28 49 6e 20 74 68 65 20 53 51 4c  .** ^(In the SQL
2b680 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74 20   statement text 
2b690 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65  input to [sqlite
2b6a0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
2b6b0 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73  and its variants
2b6c0 2c 0a 2a 2a 20 6c 69 74 65 72 61 6c 73 20 6d 61  ,.** literals ma
2b6d0 79 20 62 65 20 72 65 70 6c 61 63 65 64 20 62 79  y be replaced by
2b6e0 20 61 20 5b 70 61 72 61 6d 65 74 65 72 5d 20 74   a [parameter] t
2b6f0 68 61 74 20 6d 61 74 63 68 65 73 20 6f 6e 65 20  hat matches one 
2b700 6f 66 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  of following.** 
2b710 74 65 6d 70 6c 61 74 65 73 3a 0a 2a 2a 0a 2a 2a  templates:.**.**
2b720 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 3f   <ul>.** <li>  ?
2b730 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a 2a  .** <li>  ?NNN.*
2b740 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a 2a 20  * <li>  :VVV.** 
2b750 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a 20 3c 6c  <li>  @VVV.** <l
2b760 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c 2f 75 6c  i>  $VVV.** </ul
2b770 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 74  >.**.** In the t
2b780 65 6d 70 6c 61 74 65 73 20 61 62 6f 76 65 2c 20  emplates above, 
2b790 4e 4e 4e 20 72 65 70 72 65 73 65 6e 74 73 20 61  NNN represents a
2b7a0 6e 20 69 6e 74 65 67 65 72 20 6c 69 74 65 72 61  n integer litera
2b7b0 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56 56 20 72 65  l,.** and VVV re
2b7c0 70 72 65 73 65 6e 74 73 20 61 6e 20 61 6c 70 68  presents an alph
2b7d0 61 6e 75 6d 65 72 69 63 20 69 64 65 6e 74 69 66  anumeric identif
2b7e0 69 65 72 2e 29 5e 20 20 5e 54 68 65 20 76 61 6c  ier.)^  ^The val
2b7f0 75 65 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20  ues of these.** 
2b800 70 61 72 61 6d 65 74 65 72 73 20 28 61 6c 73 6f  parameters (also
2b810 20 63 61 6c 6c 65 64 20 22 68 6f 73 74 20 70 61   called "host pa
2b820 72 61 6d 65 74 65 72 20 6e 61 6d 65 73 22 20 6f  rameter names" o
2b830 72 20 22 53 51 4c 20 70 61 72 61 6d 65 74 65 72  r "SQL parameter
2b840 73 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65  s").** can be se
2b850 74 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69  t using the sqli
2b860 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75  te3_bind_*() rou
2b870 74 69 6e 65 73 20 64 65 66 69 6e 65 64 20 68 65  tines defined he
2b880 72 65 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66  re..**.** ^The f
2b890 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
2b8a0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e   the sqlite3_bin
2b8b0 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 69  d_*() routines i
2b8c0 73 20 61 6c 77 61 79 73 0a 2a 2a 20 61 20 70 6f  s always.** a po
2b8d0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71  inter to the [sq
2b8e0 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65  lite3_stmt] obje
2b8f0 63 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d  ct returned from
2b900 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
2b910 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 74  pare_v2()] or it
2b920 73 20 76 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a  s variants..**.*
2b930 2a 20 5e 54 68 65 20 73 65 63 6f 6e 64 20 61 72  * ^The second ar
2b940 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e  gument is the in
2b950 64 65 78 20 6f 66 20 74 68 65 20 53 51 4c 20 70  dex of the SQL p
2b960 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20 73  arameter to be s
2b970 65 74 2e 0a 2a 2a 20 5e 54 68 65 20 6c 65 66 74  et..** ^The left
2b980 6d 6f 73 74 20 53 51 4c 20 70 61 72 61 6d 65 74  most SQL paramet
2b990 65 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20  er has an index 
2b9a0 6f 66 20 31 2e 20 20 5e 57 68 65 6e 20 74 68 65  of 1.  ^When the
2b9b0 20 73 61 6d 65 20 6e 61 6d 65 64 0a 2a 2a 20 53   same named.** S
2b9c0 51 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  QL parameter is 
2b9d0 75 73 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f  used more than o
2b9e0 6e 63 65 2c 20 73 65 63 6f 6e 64 20 61 6e 64 20  nce, second and 
2b9f0 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 6f 63  subsequent.** oc
2ba00 63 75 72 72 65 6e 63 65 73 20 68 61 76 65 20 74  currences have t
2ba10 68 65 20 73 61 6d 65 20 69 6e 64 65 78 20 61 73  he same index as
2ba20 20 74 68 65 20 66 69 72 73 74 20 6f 63 63 75 72   the first occur
2ba30 72 65 6e 63 65 2e 0a 2a 2a 20 5e 54 68 65 20 69  rence..** ^The i
2ba40 6e 64 65 78 20 66 6f 72 20 6e 61 6d 65 64 20 70  ndex for named p
2ba50 61 72 61 6d 65 74 65 72 73 20 63 61 6e 20 62 65  arameters can be
2ba60 20 6c 6f 6f 6b 65 64 20 75 70 20 75 73 69 6e 67   looked up using
2ba70 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
2ba80 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
2ba90 69 6e 64 65 78 28 29 5d 20 41 50 49 20 69 66 20  index()] API if 
2baa0 64 65 73 69 72 65 64 2e 20 20 5e 54 68 65 20 69  desired.  ^The i
2bab0 6e 64 65 78 0a 2a 2a 20 66 6f 72 20 22 3f 4e 4e  ndex.** for "?NN
2bac0 4e 22 20 70 61 72 61 6d 65 74 65 72 73 20 69 73  N" parameters is
2bad0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4e 4e   the value of NN
2bae0 4e 2e 0a 2a 2a 20 5e 54 68 65 20 4e 4e 4e 20 76  N..** ^The NNN v
2baf0 61 6c 75 65 20 6d 75 73 74 20 62 65 20 62 65 74  alue must be bet
2bb00 77 65 65 6e 20 31 20 61 6e 64 20 74 68 65 20 5b  ween 1 and the [
2bb10 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d  sqlite3_limit()]
2bb20 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 5b 53  .** parameter [S
2bb30 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49  QLITE_LIMIT_VARI
2bb40 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 28 64 65  ABLE_NUMBER] (de
2bb50 66 61 75 6c 74 20 76 61 6c 75 65 3a 20 39 39 39  fault value: 999
2bb60 29 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 74 68  )..**.** ^The th
2bb70 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ird argument is 
2bb80 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62 69 6e  the value to bin
2bb90 64 20 74 6f 20 74 68 65 20 70 61 72 61 6d 65 74  d to the paramet
2bba0 65 72 2e 0a 2a 2a 0a 2a 2a 20 5e 28 49 6e 20 74  er..**.** ^(In t
2bbb0 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 74 68  hose routines th
2bbc0 61 74 20 68 61 76 65 20 61 20 66 6f 75 72 74 68  at have a fourth
2bbd0 20 61 72 67 75 6d 65 6e 74 2c 20 69 74 73 20 76   argument, its v
2bbe0 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 6e  alue is the.** n
2bbf0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
2bc00 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e  n the parameter.
2bc10 20 20 54 6f 20 62 65 20 63 6c 65 61 72 3a 20 74    To be clear: t
2bc20 68 65 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a  he value is the.
2bc30 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 3c 75 3e  ** number of <u>
2bc40 62 79 74 65 73 3c 2f 75 3e 20 69 6e 20 74 68 65  bytes</u> in the
2bc50 20 76 61 6c 75 65 2c 20 6e 6f 74 20 74 68 65 20   value, not the 
2bc60 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
2bc70 74 65 72 73 2e 29 5e 0a 2a 2a 20 5e 49 66 20 74  ters.)^.** ^If t
2bc80 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65  he fourth parame
2bc90 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 62  ter to sqlite3_b
2bca0 69 6e 64 5f 74 65 78 74 28 29 20 6f 72 20 73 71  ind_text() or sq
2bcb0 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31  lite3_bind_text1
2bcc0 36 28 29 0a 2a 2a 20 69 73 20 6e 65 67 61 74 69  6().** is negati
2bcd0 76 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 65 6e  ve, then the len
2bce0 67 74 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e  gth of the strin
2bcf0 67 20 69 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62  g is.** the numb
2bd00 65 72 20 6f 66 20 62 79 74 65 73 20 75 70 20 74  er of bytes up t
2bd10 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f  o the first zero
2bd20 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 20   terminator..** 
2bd30 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61  If the fourth pa
2bd40 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
2bd50 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 29 20 69  e3_bind_blob() i
2bd60 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e  s negative, then
2bd70 0a 2a 2a 20 74 68 65 20 62 65 68 61 76 69 6f 72  .** the behavior
2bd80 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
2bd90 2a 20 49 66 20 61 20 6e 6f 6e 2d 6e 65 67 61 74  * If a non-negat
2bda0 69 76 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d  ive fourth param
2bdb0 65 74 65 72 20 69 73 20 70 72 6f 76 69 64 65 64  eter is provided
2bdc0 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64   to sqlite3_bind
2bdd0 5f 74 65 78 74 28 29 0a 2a 2a 20 6f 72 20 73 71  _text().** or sq
2bde0 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31  lite3_bind_text1
2bdf0 36 28 29 20 74 68 65 6e 20 74 68 61 74 20 70 61  6() then that pa
2be00 72 61 6d 65 74 65 72 20 6d 75 73 74 20 62 65 20  rameter must be 
2be10 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 0a  the byte offset.
2be20 2a 2a 20 77 68 65 72 65 20 74 68 65 20 4e 55 4c  ** where the NUL
2be30 20 74 65 72 6d 69 6e 61 74 6f 72 20 77 6f 75 6c   terminator woul
2be40 64 20 6f 63 63 75 72 20 61 73 73 75 6d 69 6e 67  d occur assuming
2be50 20 74 68 65 20 73 74 72 69 6e 67 20 77 65 72 65   the string were
2be60 20 4e 55 4c 0a 2a 2a 20 74 65 72 6d 69 6e 61 74   NUL.** terminat
2be70 65 64 2e 20 20 49 66 20 61 6e 79 20 4e 55 4c 20  ed.  If any NUL 
2be80 63 68 61 72 61 63 74 65 72 73 20 6f 63 63 75 72  characters occur
2be90 20 61 74 20 62 79 74 65 20 6f 66 66 73 65 74 73   at byte offsets
2bea0 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74   less than .** t
2beb0 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
2bec0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
2bed0 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
2bee0 69 6e 67 20 73 74 72 69 6e 67 20 76 61 6c 75 65  ing string value
2bef0 20 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 61 69 6e   will.** contain
2bf00 20 65 6d 62 65 64 64 65 64 20 4e 55 4c 73 2e 20   embedded NULs. 
2bf10 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65   The result of e
2bf20 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 76 6f 6c  xpressions invol
2bf30 76 69 6e 67 20 73 74 72 69 6e 67 73 0a 2a 2a 20  ving strings.** 
2bf40 77 69 74 68 20 65 6d 62 65 64 64 65 64 20 4e 55  with embedded NU
2bf50 4c 73 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  Ls is undefined.
2bf60 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 66 74  .**.** ^The fift
2bf70 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71  h argument to sq
2bf80 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28  lite3_bind_blob(
2bf90 29 2c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ), sqlite3_bind_
2bfa0 74 65 78 74 28 29 2c 20 61 6e 64 0a 2a 2a 20 73  text(), and.** s
2bfb0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
2bfc0 31 36 28 29 20 69 73 20 61 20 64 65 73 74 72 75  16() is a destru
2bfd0 63 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 73  ctor used to dis
2bfe0 70 6f 73 65 20 6f 66 20 74 68 65 20 42 4c 4f 42  pose of the BLOB
2bff0 20 6f 72 0a 2a 2a 20 73 74 72 69 6e 67 20 61 66   or.** string af
2c000 74 65 72 20 53 51 4c 69 74 65 20 68 61 73 20 66  ter SQLite has f
2c010 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74 2e  inished with it.
2c020 20 20 5e 54 68 65 20 64 65 73 74 72 75 63 74 6f    ^The destructo
2c030 72 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 74  r is called.** t
2c040 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65  o dispose of the
2c050 20 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 20   BLOB or string 
2c060 65 76 65 6e 20 69 66 20 74 68 65 20 63 61 6c 6c  even if the call
2c070 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64   to sqlite3_bind
2c080 5f 62 6c 6f 62 28 29 2c 0a 2a 2a 20 73 71 6c 69  _blob(),.** sqli
2c090 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 29 2c  te3_bind_text(),
2c0a0 20 6f 72 20 73 71 6c 69 74 65 33 5f 62 69 6e 64   or sqlite3_bind
2c0b0 5f 74 65 78 74 31 36 28 29 20 66 61 69 6c 73 2e  _text16() fails.
2c0c0 20 20 0a 2a 2a 20 5e 49 66 20 74 68 65 20 66 69    .** ^If the fi
2c0d0 66 74 68 20 61 72 67 75 6d 65 6e 74 20 69 73 0a  fth argument is.
2c0e0 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 76  ** the special v
2c0f0 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 53 54 41  alue [SQLITE_STA
2c100 54 49 43 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74  TIC], then SQLit
2c110 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74  e assumes that t
2c120 68 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  he.** informatio
2c130 6e 20 69 73 20 69 6e 20 73 74 61 74 69 63 2c 20  n is in static, 
2c140 75 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20  unmanaged space 
2c150 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65  and does not nee
2c160 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a  d to be freed..*
2c170 2a 20 5e 49 66 20 74 68 65 20 66 69 66 74 68 20  * ^If the fifth 
2c180 61 72 67 75 6d 65 6e 74 20 68 61 73 20 74 68 65  argument has the
2c190 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 54   value [SQLITE_T
2c1a0 52 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65 6e 0a  RANSIENT], then.
2c1b0 2a 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20  ** SQLite makes 
2c1c0 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20  its own private 
2c1d0 63 6f 70 79 20 6f 66 20 74 68 65 20 64 61 74 61  copy of the data
2c1e0 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65   immediately, be
2c1f0 66 6f 72 65 0a 2a 2a 20 74 68 65 20 73 71 6c 69  fore.** the sqli
2c200 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75  te3_bind_*() rou
2c210 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a  tine returns..**
2c220 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33  .** ^The sqlite3
2c230 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29  _bind_zeroblob()
2c240 20 72 6f 75 74 69 6e 65 20 62 69 6e 64 73 20 61   routine binds a
2c250 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20   BLOB of length 
2c260 4e 20 74 68 61 74 0a 2a 2a 20 69 73 20 66 69 6c  N that.** is fil
2c270 6c 65 64 20 77 69 74 68 20 7a 65 72 6f 65 73 2e  led with zeroes.
2c280 20 20 5e 41 20 7a 65 72 6f 62 6c 6f 62 20 75 73    ^A zeroblob us
2c290 65 73 20 61 20 66 69 78 65 64 20 61 6d 6f 75 6e  es a fixed amoun
2c2a0 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28  t of memory.** (
2c2b0 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20  just an integer 
2c2c0 74 6f 20 68 6f 6c 64 20 69 74 73 20 73 69 7a 65  to hold its size
2c2d0 29 20 77 68 69 6c 65 20 69 74 20 69 73 20 62 65  ) while it is be
2c2e0 69 6e 67 20 70 72 6f 63 65 73 73 65 64 2e 0a 2a  ing processed..*
2c2f0 2a 20 5a 65 72 6f 62 6c 6f 62 73 20 61 72 65 20  * Zeroblobs are 
2c300 69 6e 74 65 6e 64 65 64 20 74 6f 20 73 65 72 76  intended to serv
2c310 65 20 61 73 20 70 6c 61 63 65 68 6f 6c 64 65 72  e as placeholder
2c320 73 20 66 6f 72 20 42 4c 4f 42 73 20 77 68 6f 73  s for BLOBs whos
2c330 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 69 73 20  e.** content is 
2c340 6c 61 74 65 72 20 77 72 69 74 74 65 6e 20 75 73  later written us
2c350 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ing.** [sqlite3_
2c360 62 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72  blob_open | incr
2c370 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f  emental BLOB I/O
2c380 5d 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 5e  ] routines..** ^
2c390 41 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65  A negative value
2c3a0 20 66 6f 72 20 74 68 65 20 7a 65 72 6f 62 6c 6f   for the zeroblo
2c3b0 62 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 7a  b results in a z
2c3c0 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 2e  ero-length BLOB.
2c3d0 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 61 6e 79 20 6f  .**.** ^If any o
2c3e0 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69  f the sqlite3_bi
2c3f0 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20  nd_*() routines 
2c400 61 72 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20  are called with 
2c410 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a  a NULL pointer.*
2c420 2a 20 66 6f 72 20 74 68 65 20 5b 70 72 65 70 61  * for the [prepa
2c430 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f  red statement] o
2c440 72 20 77 69 74 68 20 61 20 70 72 65 70 61 72 65  r with a prepare
2c450 64 20 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20  d statement for 
2c460 77 68 69 63 68 0a 2a 2a 20 5b 73 71 6c 69 74 65  which.** [sqlite
2c470 33 5f 73 74 65 70 28 29 5d 20 68 61 73 20 62 65  3_step()] has be
2c480 65 6e 20 63 61 6c 6c 65 64 20 6d 6f 72 65 20 72  en called more r
2c490 65 63 65 6e 74 6c 79 20 74 68 61 6e 20 5b 73 71  ecently than [sq
2c4a0 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 2c 0a  lite3_reset()],.
2c4b0 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c  ** then the call
2c4c0 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51   will return [SQ
2c4d0 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e 20 20 49  LITE_MISUSE].  I
2c4e0 66 20 61 6e 79 20 73 71 6c 69 74 65 33 5f 62 69  f any sqlite3_bi
2c4f0 6e 64 5f 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65  nd_().** routine
2c500 20 69 73 20 70 61 73 73 65 64 20 61 20 5b 70 72   is passed a [pr
2c510 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2c520 5d 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20  ] that has been 
2c530 66 69 6e 61 6c 69 7a 65 64 2c 20 74 68 65 0a 2a  finalized, the.*
2c540 2a 20 72 65 73 75 6c 74 20 69 73 20 75 6e 64 65  * result is unde
2c550 66 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61 62  fined and probab
2c560 6c 79 20 68 61 72 6d 66 75 6c 2e 0a 2a 2a 0a 2a  ly harmful..**.*
2c570 2a 20 5e 42 69 6e 64 69 6e 67 73 20 61 72 65 20  * ^Bindings are 
2c580 6e 6f 74 20 63 6c 65 61 72 65 64 20 62 79 20 74  not cleared by t
2c590 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  he [sqlite3_rese
2c5a0 74 28 29 5d 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a  t()] routine..**
2c5b0 20 5e 55 6e 62 6f 75 6e 64 20 70 61 72 61 6d 65   ^Unbound parame
2c5c0 74 65 72 73 20 61 72 65 20 69 6e 74 65 72 70 72  ters are interpr
2c5d0 65 74 65 64 20 61 73 20 4e 55 4c 4c 2e 0a 2a 2a  eted as NULL..**
2c5e0 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33  .** ^The sqlite3
2c5f0 5f 62 69 6e 64 5f 2a 20 72 6f 75 74 69 6e 65 73  _bind_* routines
2c600 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
2c610 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 6f  OK] on success o
2c620 72 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63  r an.** [error c
2c630 6f 64 65 5d 20 69 66 20 61 6e 79 74 68 69 6e 67  ode] if anything
2c640 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 20   goes wrong..** 
2c650 5e 5b 53 51 4c 49 54 45 5f 52 41 4e 47 45 5d 20  ^[SQLITE_RANGE] 
2c660 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
2c670 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20  he parameter.** 
2c680 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f 66 20  index is out of 
2c690 72 61 6e 67 65 2e 20 20 5e 5b 53 51 4c 49 54 45  range.  ^[SQLITE
2c6a0 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74 75 72  _NOMEM] is retur
2c6b0 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20  ned if malloc() 
2c6c0 66 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  fails..**.** See
2c6d0 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f   also: [sqlite3_
2c6e0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63  bind_parameter_c
2c6f0 6f 75 6e 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  ount()],.** [sql
2c700 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
2c710 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64  ter_name()], and
2c720 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70   [sqlite3_bind_p
2c730 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29  arameter_index()
2c740 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  ]..*/.SQLITE_API
2c750 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
2c760 64 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73  d_blob(sqlite3_s
2c770 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  tmt*, int, const
2c780 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 2c 20 76   void*, int n, v
2c790 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
2c7a0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2c7b0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62  qlite3_bind_doub
2c7c0 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  le(sqlite3_stmt*
2c7d0 2c 20 69 6e 74 2c 20 64 6f 75 62 6c 65 29 3b 0a  , int, double);.
2c7e0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2c7f0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28  qlite3_bind_int(
2c800 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
2c810 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
2c820 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2c830 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 71 6c 69  _bind_int64(sqli
2c840 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20  te3_stmt*, int, 
2c850 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a  sqlite3_int64);.
2c860 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2c870 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c  qlite3_bind_null
2c880 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
2c890 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
2c8a0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
2c8b0 64 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73  d_text(sqlite3_s
2c8c0 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  tmt*, int, const
2c8d0 20 63 68 61 72 2a 2c 20 69 6e 74 20 6e 2c 20 76   char*, int n, v
2c8e0 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
2c8f0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2c900 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
2c910 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  16(sqlite3_stmt*
2c920 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69  , int, const voi
2c930 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29  d*, int, void(*)
2c940 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45  (void*));.SQLITE
2c950 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2c960 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69  _bind_value(sqli
2c970 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20  te3_stmt*, int, 
2c980 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61  const sqlite3_va
2c990 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
2c9a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
2c9b0 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69  nd_zeroblob(sqli
2c9c0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20  te3_stmt*, int, 
2c9d0 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  int n);../*.** C
2c9e0 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20  API3REF: Number 
2c9f0 4f 66 20 53 51 4c 20 50 61 72 61 6d 65 74 65 72  Of SQL Parameter
2ca00 73 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 72 6f  s.**.** ^This ro
2ca10 75 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73 65  utine can be use
2ca20 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75  d to find the nu
2ca30 6d 62 65 72 20 6f 66 20 5b 53 51 4c 20 70 61 72  mber of [SQL par
2ca40 61 6d 65 74 65 72 73 5d 0a 2a 2a 20 69 6e 20 61  ameters].** in a
2ca50 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
2ca60 6d 65 6e 74 5d 2e 20 20 53 51 4c 20 70 61 72 61  ment].  SQL para
2ca70 6d 65 74 65 72 73 20 61 72 65 20 74 6f 6b 65 6e  meters are token
2ca80 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d  s of the.** form
2ca90 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 3a   "?", "?NNN", ":
2caa0 41 41 41 22 2c 20 22 24 41 41 41 22 2c 20 6f 72  AAA", "$AAA", or
2cab0 20 22 40 41 41 41 22 20 74 68 61 74 20 73 65 72   "@AAA" that ser
2cac0 76 65 20 61 73 0a 2a 2a 20 70 6c 61 63 65 68 6f  ve as.** placeho
2cad0 6c 64 65 72 73 20 66 6f 72 20 76 61 6c 75 65 73  lders for values
2cae0 20 74 68 61 74 20 61 72 65 20 5b 73 71 6c 69 74   that are [sqlit
2caf0 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62  e3_bind_blob | b
2cb00 6f 75 6e 64 5d 0a 2a 2a 20 74 6f 20 74 68 65 20  ound].** to the 
2cb10 70 61 72 61 6d 65 74 65 72 73 20 61 74 20 61 20  parameters at a 
2cb20 6c 61 74 65 72 20 74 69 6d 65 2e 0a 2a 2a 0a 2a  later time..**.*
2cb30 2a 20 5e 28 54 68 69 73 20 72 6f 75 74 69 6e 65  * ^(This routine
2cb40 20 61 63 74 75 61 6c 6c 79 20 72 65 74 75 72 6e   actually return
2cb50 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  s the index of t
2cb60 68 65 20 6c 61 72 67 65 73 74 20 28 72 69 67 68  he largest (righ
2cb70 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 61 6d 65  tmost).** parame
2cb80 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20 66 6f 72  ter. For all for
2cb90 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e 4e 2c 20  ms except ?NNN, 
2cba0 74 68 69 73 20 77 69 6c 6c 20 63 6f 72 72 65 73  this will corres
2cbb0 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a 2a 20 6e  pond to the.** n
2cbc0 75 6d 62 65 72 20 6f 66 20 75 6e 69 71 75 65 20  umber of unique 
2cbd0 70 61 72 61 6d 65 74 65 72 73 2e 20 20 49 66 20  parameters.  If 
2cbe0 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68  parameters of th
2cbf0 65 20 3f 4e 4e 4e 20 66 6f 72 6d 20 61 72 65 20  e ?NNN form are 
2cc00 75 73 65 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d  used,.** there m
2cc10 61 79 20 62 65 20 67 61 70 73 20 69 6e 20 74 68  ay be gaps in th
2cc20 65 20 6c 69 73 74 2e 29 5e 0a 2a 2a 0a 2a 2a 20  e list.)^.**.** 
2cc30 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74  See also: [sqlit
2cc40 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c  e3_bind_blob|sql
2cc50 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a  ite3_bind()],.**
2cc60 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70   [sqlite3_bind_p
2cc70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d  arameter_name()]
2cc80 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65  , and.** [sqlite
2cc90 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
2cca0 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2f 0a 53 51  _index()]..*/.SQ
2ccb0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2ccc0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
2ccd0 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65  ter_count(sqlite
2cce0 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  3_stmt*);../*.**
2ccf0 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20   CAPI3REF: Name 
2cd00 4f 66 20 41 20 48 6f 73 74 20 50 61 72 61 6d 65  Of A Host Parame
2cd10 74 65 72 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73  ter.**.** ^The s
2cd20 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
2cd30 6d 65 74 65 72 5f 6e 61 6d 65 28 50 2c 4e 29 20  meter_name(P,N) 
2cd40 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
2cd50 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66  s.** the name of
2cd60 20 74 68 65 20 4e 2d 74 68 20 5b 53 51 4c 20 70   the N-th [SQL p
2cd70 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 74 68 65  arameter] in the
2cd80 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
2cd90 6d 65 6e 74 5d 20 50 2e 0a 2a 2a 20 5e 28 53 51  ment] P..** ^(SQ
2cda0 4c 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20  L parameters of 
2cdb0 74 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20  the form "?NNN" 
2cdc0 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22 40 41  or ":AAA" or "@A
2cdd0 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a 2a 2a  AA" or "$AAA".**
2cde0 20 68 61 76 65 20 61 20 6e 61 6d 65 20 77 68 69   have a name whi
2cdf0 63 68 20 69 73 20 74 68 65 20 73 74 72 69 6e 67  ch is the string
2ce00 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 41   "?NNN" or ":AAA
2ce10 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72 20 22  " or "@AAA" or "
2ce20 24 41 41 41 22 0a 2a 2a 20 72 65 73 70 65 63 74  $AAA".** respect
2ce30 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68  ively..** In oth
2ce40 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 69 6e  er words, the in
2ce50 69 74 69 61 6c 20 22 3a 22 20 6f 72 20 22 24 22  itial ":" or "$"
2ce60 20 6f 72 20 22 40 22 20 6f 72 20 22 3f 22 0a 2a   or "@" or "?".*
2ce70 2a 20 69 73 20 69 6e 63 6c 75 64 65 64 20 61 73  * is included as
2ce80 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d   part of the nam
2ce90 65 2e 29 5e 0a 2a 2a 20 5e 50 61 72 61 6d 65 74  e.)^.** ^Paramet
2cea0 65 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ers of the form 
2ceb0 22 3f 22 20 77 69 74 68 6f 75 74 20 61 20 66 6f  "?" without a fo
2cec0 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 67 65 72 20  llowing integer 
2ced0 68 61 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20  have no name.** 
2cee0 61 6e 64 20 61 72 65 20 72 65 66 65 72 72 65 64  and are referred
2cef0 20 74 6f 20 61 73 20 22 6e 61 6d 65 6c 65 73 73   to as "nameless
2cf00 22 20 6f 72 20 22 61 6e 6f 6e 79 6d 6f 75 73 20  " or "anonymous 
2cf10 70 61 72 61 6d 65 74 65 72 73 22 2e 0a 2a 2a 0a  parameters"..**.
2cf20 2a 2a 20 5e 54 68 65 20 66 69 72 73 74 20 68 6f  ** ^The first ho
2cf30 73 74 20 70 61 72 61 6d 65 74 65 72 20 68 61 73  st parameter has
2cf40 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2c 20   an index of 1, 
2cf50 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66  not 0..**.** ^If
2cf60 20 74 68 65 20 76 61 6c 75 65 20 4e 20 69 73 20   the value N is 
2cf70 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 20  out of range or 
2cf80 69 66 20 74 68 65 20 4e 2d 74 68 20 70 61 72 61  if the N-th para
2cf90 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e 61 6d 65  meter is.** name
2cfa0 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55 4c 4c 20  less, then NULL 
2cfb0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 5e 54  is returned.  ^T
2cfc0 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
2cfd0 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20  ng is.** always 
2cfe0 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e  in UTF-8 encodin
2cff0 67 20 65 76 65 6e 20 69 66 20 74 68 65 20 6e 61  g even if the na
2d000 6d 65 64 20 70 61 72 61 6d 65 74 65 72 20 77 61  med parameter wa
2d010 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20  s.** originally 
2d020 73 70 65 63 69 66 69 65 64 20 61 73 20 55 54 46  specified as UTF
2d030 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f  -16 in [sqlite3_
2d040 70 72 65 70 61 72 65 31 36 28 29 5d 20 6f 72 0a  prepare16()] or.
2d050 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ** [sqlite3_prep
2d060 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a  are16_v2()]..**.
2d070 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71  ** See also: [sq
2d080 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c  lite3_bind_blob|
2d090 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c  sqlite3_bind()],
2d0a0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  .** [sqlite3_bin
2d0b0 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e  d_parameter_coun
2d0c0 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71  t()], and.** [sq
2d0d0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
2d0e0 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a  eter_index()]..*
2d0f0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
2d100 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
2d110 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
2d120 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
2d130 74 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a  t*, int);../*.**
2d140 20 43 41 50 49 33 52 45 46 3a 20 49 6e 64 65 78   CAPI3REF: Index
2d150 20 4f 66 20 41 20 50 61 72 61 6d 65 74 65 72 20   Of A Parameter 
2d160 57 69 74 68 20 41 20 47 69 76 65 6e 20 4e 61 6d  With A Given Nam
2d170 65 0a 2a 2a 0a 2a 2a 20 5e 52 65 74 75 72 6e 20  e.**.** ^Return 
2d180 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20  the index of an 
2d190 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69  SQL parameter gi
2d1a0 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 5e  ven its name.  ^
2d1b0 54 68 65 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c  The.** index val
2d1c0 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 73  ue returned is s
2d1d0 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20  uitable for use 
2d1e0 61 73 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a  as the second.**
2d1f0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
2d200 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
2d210 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d  |sqlite3_bind()]
2d220 2e 20 20 5e 41 20 7a 65 72 6f 0a 2a 2a 20 69 73  .  ^A zero.** is
2d230 20 72 65 74 75 72 6e 65 64 20 69 66 20 6e 6f 20   returned if no 
2d240 6d 61 74 63 68 69 6e 67 20 70 61 72 61 6d 65 74  matching paramet
2d250 65 72 20 69 73 20 66 6f 75 6e 64 2e 20 20 5e 54  er is found.  ^T
2d260 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20  he parameter.** 
2d270 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 67 69 76  name must be giv
2d280 65 6e 20 69 6e 20 55 54 46 2d 38 20 65 76 65 6e  en in UTF-8 even
2d290 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   if the original
2d2a0 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 77 61   statement.** wa
2d2b0 73 20 70 72 65 70 61 72 65 64 20 66 72 6f 6d 20  s prepared from 
2d2c0 55 54 46 2d 31 36 20 74 65 78 74 20 75 73 69 6e  UTF-16 text usin
2d2d0 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  g [sqlite3_prepa
2d2e0 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a  re16_v2()]..**.*
2d2f0 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c  * See also: [sql
2d300 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73  ite3_bind_blob|s
2d310 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a  qlite3_bind()],.
2d320 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  ** [sqlite3_bind
2d330 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74  _parameter_count
2d340 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c  ()], and.** [sql
2d350 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
2d360 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2f  ter_index()]..*/
2d370 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2d380 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
2d390 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 73 71 6c  ameter_index(sql
2d3a0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 63 6f 6e 73  ite3_stmt*, cons
2d3b0 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 3b 0a  t char *zName);.
2d3c0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
2d3d0 20 52 65 73 65 74 20 41 6c 6c 20 42 69 6e 64 69   Reset All Bindi
2d3e0 6e 67 73 20 4f 6e 20 41 20 50 72 65 70 61 72 65  ngs On A Prepare
2d3f0 64 20 53 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a  d Statement.**.*
2d400 2a 20 5e 43 6f 6e 74 72 61 72 79 20 74 6f 20 74  * ^Contrary to t
2d410 68 65 20 69 6e 74 75 69 74 69 6f 6e 20 6f 66 20  he intuition of 
2d420 6d 61 6e 79 2c 20 5b 73 71 6c 69 74 65 33 5f 72  many, [sqlite3_r
2d430 65 73 65 74 28 29 5d 20 64 6f 65 73 20 6e 6f 74  eset()] does not
2d440 20 72 65 73 65 74 0a 2a 2a 20 74 68 65 20 5b 73   reset.** the [s
2d450 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
2d460 20 7c 20 62 69 6e 64 69 6e 67 73 5d 20 6f 6e 20   | bindings] on 
2d470 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  a [prepared stat
2d480 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 5e 55 73 65 20  ement]..** ^Use 
2d490 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20  this routine to 
2d4a0 72 65 73 65 74 20 61 6c 6c 20 68 6f 73 74 20 70  reset all host p
2d4b0 61 72 61 6d 65 74 65 72 73 20 74 6f 20 4e 55 4c  arameters to NUL
2d4c0 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  L..*/.SQLITE_API
2d4d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65   int sqlite3_cle
2d4e0 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69  ar_bindings(sqli
2d4f0 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a  te3_stmt*);../*.
2d500 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d  ** CAPI3REF: Num
2d510 62 65 72 20 4f 66 20 43 6f 6c 75 6d 6e 73 20 49  ber Of Columns I
2d520 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 0a 2a  n A Result Set.*
2d530 2a 0a 2a 2a 20 5e 52 65 74 75 72 6e 20 74 68 65  *.** ^Return the
2d540 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
2d550 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ns in the result
2d560 20 73 65 74 20 72 65 74 75 72 6e 65 64 20 62 79   set returned by
2d570 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65   the.** [prepare
2d580 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 5e 54  d statement]. ^T
2d590 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
2d5a0 72 6e 73 20 30 20 69 66 20 70 53 74 6d 74 20 69  rns 0 if pStmt i
2d5b0 73 20 61 6e 20 53 51 4c 0a 2a 2a 20 73 74 61 74  s an SQL.** stat
2d5c0 65 6d 65 6e 74 20 74 68 61 74 20 64 6f 65 73 20  ement that does 
2d5d0 6e 6f 74 20 72 65 74 75 72 6e 20 64 61 74 61 20  not return data 
2d5e0 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e 20  (for example an 
2d5f0 5b 55 50 44 41 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a  [UPDATE])..**.**
2d600 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69   See also: [sqli
2d610 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 29  te3_data_count()
2d620 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
2d630 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  int sqlite3_colu
2d640 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33  mn_count(sqlite3
2d650 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a  _stmt *pStmt);..
2d660 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2d670 43 6f 6c 75 6d 6e 20 4e 61 6d 65 73 20 49 6e 20  Column Names In 
2d680 41 20 52 65 73 75 6c 74 20 53 65 74 0a 2a 2a 0a  A Result Set.**.
2d690 2a 2a 20 5e 54 68 65 73 65 20 72 6f 75 74 69 6e  ** ^These routin
2d6a0 65 73 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61  es return the na
2d6b0 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61  me assigned to a
2d6c0 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75   particular colu
2d6d0 6d 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73  mn.** in the res
2d6e0 75 6c 74 20 73 65 74 20 6f 66 20 61 20 5b 53 45  ult set of a [SE
2d6f0 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 2e  LECT] statement.
2d700 20 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 63    ^The sqlite3_c
2d710 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 0a 2a 2a 20  olumn_name().** 
2d720 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
2d730 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
2d740 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
2d750 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a   UTF-8 string.**
2d760 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c   and sqlite3_col
2d770 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20 72 65 74  umn_name16() ret
2d780 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
2d790 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  o a zero-termina
2d7a0 74 65 64 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74  ted.** UTF-16 st
2d7b0 72 69 6e 67 2e 20 20 5e 54 68 65 20 66 69 72 73  ring.  ^The firs
2d7c0 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  t parameter is t
2d7d0 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
2d7e0 74 65 6d 65 6e 74 5d 0a 2a 2a 20 74 68 61 74 20  tement].** that 
2d7f0 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 5b  implements the [
2d800 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e  SELECT] statemen
2d810 74 2e 20 5e 54 68 65 20 73 65 63 6f 6e 64 20 70  t. ^The second p
2d820 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 0a  arameter is the.
2d830 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  ** column number
2d840 2e 20 20 5e 54 68 65 20 6c 65 66 74 6d 6f 73 74  .  ^The leftmost
2d850 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 75 6d 62 65   column is numbe
2d860 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  r 0..**.** ^The 
2d870 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
2d880 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64  pointer is valid
2d890 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 74 68   until either th
2d8a0 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  e [prepared stat
2d8b0 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73 20 64 65 73  ement].** is des
2d8c0 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74  troyed by [sqlit
2d8d0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f  e3_finalize()] o
2d8e0 72 20 75 6e 74 69 6c 20 74 68 65 20 73 74 61 74  r until the stat
2d8f0 65 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d 61 74  ement is automat
2d900 69 63 61 6c 6c 79 0a 2a 2a 20 72 65 70 72 65 70  ically.** reprep
2d910 61 72 65 64 20 62 79 20 74 68 65 20 66 69 72 73  ared by the firs
2d920 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  t call to [sqlit
2d930 65 33 5f 73 74 65 70 28 29 5d 20 66 6f 72 20 61  e3_step()] for a
2d940 20 70 61 72 74 69 63 75 6c 61 72 20 72 75 6e 0a   particular run.
2d950 2a 2a 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20  ** or until the 
2d960 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  next call to.** 
2d970 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
2d980 61 6d 65 28 29 20 6f 72 20 73 71 6c 69 74 65 33  ame() or sqlite3
2d990 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29  _column_name16()
2d9a0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 63 6f 6c   on the same col
2d9b0 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 73  umn..**.** ^If s
2d9c0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
2d9d0 66 61 69 6c 73 20 64 75 72 69 6e 67 20 74 68 65  fails during the
2d9e0 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 65   processing of e
2d9f0 69 74 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a  ither routine.**
2da00 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 64 75   (for example du
2da10 72 69 6e 67 20 61 20 63 6f 6e 76 65 72 73 69 6f  ring a conversio
2da20 6e 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20  n from UTF-8 to 
2da30 55 54 46 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a  UTF-16) then a.*
2da40 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69  * NULL pointer i
2da50 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
2da60 2a 20 5e 54 68 65 20 6e 61 6d 65 20 6f 66 20 61  * ^The name of a
2da70 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69   result column i
2da80 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  s the value of t
2da90 68 65 20 22 41 53 22 20 63 6c 61 75 73 65 20 66  he "AS" clause f
2daa0 6f 72 0a 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d  or.** that colum
2dab0 6e 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 61  n, if there is a
2dac0 6e 20 41 53 20 63 6c 61 75 73 65 2e 20 20 49 66  n AS clause.  If
2dad0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 41 53 20   there is no AS 
2dae0 63 6c 61 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74  clause.** then t
2daf0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 63  he name of the c
2db00 6f 6c 75 6d 6e 20 69 73 20 75 6e 73 70 65 63 69  olumn is unspeci
2db10 66 69 65 64 20 61 6e 64 20 6d 61 79 20 63 68 61  fied and may cha
2db20 6e 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20  nge from.** one 
2db30 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 74  release of SQLit
2db40 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a  e to the next..*
2db50 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
2db60 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
2db70 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c  _column_name(sql
2db80 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
2db90 4e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  N);.SQLITE_API c
2dba0 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
2dbb0 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  e3_column_name16
2dbc0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
2dbd0 69 6e 74 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  int N);../*.** C
2dbe0 41 50 49 33 52 45 46 3a 20 53 6f 75 72 63 65 20  API3REF: Source 
2dbf0 4f 66 20 44 61 74 61 20 49 6e 20 41 20 51 75 65  Of Data In A Que
2dc00 72 79 20 52 65 73 75 6c 74 0a 2a 2a 0a 2a 2a 20  ry Result.**.** 
2dc10 5e 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20  ^These routines 
2dc20 70 72 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20  provide a means 
2dc30 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
2dc40 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65   database, table
2dc50 2c 20 61 6e 64 0a 2a 2a 20 74 61 62 6c 65 20 63  , and.** table c
2dc60 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 74 68  olumn that is th
2dc70 65 20 6f 72 69 67 69 6e 20 6f 66 20 61 20 70 61  e origin of a pa
2dc80 72 74 69 63 75 6c 61 72 20 72 65 73 75 6c 74 20  rticular result 
2dc90 63 6f 6c 75 6d 6e 20 69 6e 0a 2a 2a 20 5b 53 45  column in.** [SE
2dca0 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 2e  LECT] statement.
2dcb0 0a 2a 2a 20 5e 54 68 65 20 6e 61 6d 65 20 6f 66  .** ^The name of
2dcc0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72   the database or
2dcd0 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e   table or column
2dce0 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64   can be returned
2dcf0 20 61 73 0a 2a 2a 20 65 69 74 68 65 72 20 61 20   as.** either a 
2dd00 55 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20  UTF-8 or UTF-16 
2dd10 73 74 72 69 6e 67 2e 20 20 5e 54 68 65 20 5f 64  string.  ^The _d
2dd20 61 74 61 62 61 73 65 5f 20 72 6f 75 74 69 6e 65  atabase_ routine
2dd30 73 20 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20  s return.** the 
2dd40 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 74  database name, t
2dd50 68 65 20 5f 74 61 62 6c 65 5f 20 72 6f 75 74 69  he _table_ routi
2dd60 6e 65 73 20 72 65 74 75 72 6e 20 74 68 65 20 74  nes return the t
2dd70 61 62 6c 65 20 6e 61 6d 65 2c 20 61 6e 64 0a 2a  able name, and.*
2dd80 2a 20 74 68 65 20 6f 72 69 67 69 6e 5f 20 72 6f  * the origin_ ro
2dd90 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68  utines return th
2dda0 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a  e column name..*
2ddb0 2a 20 5e 54 68 65 20 72 65 74 75 72 6e 65 64 20  * ^The returned 
2ddc0 73 74 72 69 6e 67 20 69 73 20 76 61 6c 69 64 20  string is valid 
2ddd0 75 6e 74 69 6c 20 74 68 65 20 5b 70 72 65 70 61  until the [prepa
2dde0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69  red statement] i
2ddf0 73 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a 20 75  s destroyed.** u
2de00 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69  sing [sqlite3_fi
2de10 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74  nalize()] or unt
2de20 69 6c 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  il the statement
2de30 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
2de40 79 0a 2a 2a 20 72 65 70 72 65 70 61 72 65 64 20  y.** reprepared 
2de50 62 79 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  by the first cal
2de60 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  l to [sqlite3_st
2de70 65 70 28 29 5d 20 66 6f 72 20 61 20 70 61 72 74  ep()] for a part
2de80 69 63 75 6c 61 72 20 72 75 6e 0a 2a 2a 20 6f 72  icular run.** or
2de90 20 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20   until the same 
2dea0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72  information is r
2deb0 65 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69  equested.** agai
2dec0 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74  n in a different
2ded0 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a   encoding..**.**
2dee0 20 5e 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75   ^The names retu
2def0 72 6e 65 64 20 61 72 65 20 74 68 65 20 6f 72 69  rned are the ori
2df00 67 69 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64  ginal un-aliased
2df10 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a   names of the.**
2df20 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65   database, table
2df30 2c 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a  , and column..**
2df40 0a 2a 2a 20 5e 54 68 65 20 66 69 72 73 74 20 61  .** ^The first a
2df50 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 73 65  rgument to these
2df60 20 69 6e 74 65 72 66 61 63 65 73 20 69 73 20 61   interfaces is a
2df70 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
2df80 6d 65 6e 74 5d 2e 0a 2a 2a 20 5e 54 68 65 73 65  ment]..** ^These
2df90 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72   functions retur
2dfa0 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  n information ab
2dfb0 6f 75 74 20 74 68 65 20 4e 74 68 20 72 65 73 75  out the Nth resu
2dfc0 6c 74 20 63 6f 6c 75 6d 6e 20 72 65 74 75 72 6e  lt column return
2dfd0 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 73 74 61  ed by.** the sta
2dfe0 74 65 6d 65 6e 74 2c 20 77 68 65 72 65 20 4e 20  tement, where N 
2dff0 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 66 75  is the second fu
2e000 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e  nction argument.
2e010 0a 2a 2a 20 5e 54 68 65 20 6c 65 66 74 2d 6d 6f  .** ^The left-mo
2e020 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 63 6f 6c  st column is col
2e030 75 6d 6e 20 30 20 66 6f 72 20 74 68 65 73 65 20  umn 0 for these 
2e040 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20  routines..**.** 
2e050 5e 49 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75  ^If the Nth colu
2e060 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  mn returned by t
2e070 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
2e080 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72  an expression or
2e090 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64  .** subquery and
2e0a0 20 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e   is not a column
2e0b0 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c   value, then all
2e0c0 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69   of these functi
2e0d0 6f 6e 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55  ons return.** NU
2e0e0 4c 4c 2e 20 20 5e 54 68 65 73 65 20 72 6f 75 74  LL.  ^These rout
2e0f0 69 6e 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 72  ine might also r
2e100 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20  eturn NULL if a 
2e110 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
2e120 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72  n error.** occur
2e130 73 2e 20 20 5e 4f 74 68 65 72 77 69 73 65 2c 20  s.  ^Otherwise, 
2e140 74 68 65 79 20 72 65 74 75 72 6e 20 74 68 65 20  they return the 
2e150 6e 61 6d 65 20 6f 66 20 74 68 65 20 61 74 74 61  name of the atta
2e160 63 68 65 64 20 64 61 74 61 62 61 73 65 2c 20 74  ched database, t
2e170 61 62 6c 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6c 75  able,.** or colu
2e180 6d 6e 20 74 68 61 74 20 71 75 65 72 79 20 72 65  mn that query re
2e190 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 77 61 73 20  sult column was 
2e1a0 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 2e 0a  extracted from..
2e1b0 2a 2a 0a 2a 2a 20 5e 41 73 20 77 69 74 68 20 61  **.** ^As with a
2e1c0 6c 6c 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20  ll other SQLite 
2e1d0 41 50 49 73 2c 20 74 68 6f 73 65 20 77 68 6f 73  APIs, those whos
2e1e0 65 20 6e 61 6d 65 73 20 65 6e 64 20 77 69 74 68  e names end with
2e1f0 20 22 31 36 22 20 72 65 74 75 72 6e 0a 2a 2a 20   "16" return.** 
2e200 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 73  UTF-16 encoded s
2e210 74 72 69 6e 67 73 20 61 6e 64 20 74 68 65 20 6f  trings and the o
2e220 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 72  ther functions r
2e230 65 74 75 72 6e 20 55 54 46 2d 38 2e 0a 2a 2a 0a  eturn UTF-8..**.
2e240 2a 2a 20 5e 54 68 65 73 65 20 41 50 49 73 20 61  ** ^These APIs a
2e250 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  re only availabl
2e260 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79  e if the library
2e270 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69   was compiled wi
2e280 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54  th the.** [SQLIT
2e290 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f  E_ENABLE_COLUMN_
2e2a0 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65 70  METADATA] C-prep
2e2b0 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c 2e  rocessor symbol.
2e2c0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 77 6f 20 6f 72  .**.** If two or
2e2d0 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 61   more threads ca
2e2e0 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f  ll one or more o
2e2f0 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  f these routines
2e300 20 61 67 61 69 6e 73 74 20 74 68 65 20 73 61 6d   against the sam
2e310 65 0a 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74  e.** prepared st
2e320 61 74 65 6d 65 6e 74 20 61 6e 64 20 63 6f 6c 75  atement and colu
2e330 6d 6e 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  mn at the same t
2e340 69 6d 65 20 74 68 65 6e 20 74 68 65 20 72 65 73  ime then the res
2e350 75 6c 74 73 20 61 72 65 0a 2a 2a 20 75 6e 64 65  ults are.** unde
2e360 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  fined..**.** If 
2e370 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 65  two or more thre
2e380 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20  ads call one or 
2e390 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  more.** [sqlite3
2e3a0 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
2e3b0 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e 20 6d  _name | column m
2e3c0 65 74 61 64 61 74 61 20 69 6e 74 65 72 66 61 63  etadata interfac
2e3d0 65 73 5d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73  es].** for the s
2e3e0 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  ame [prepared st
2e3f0 61 74 65 6d 65 6e 74 5d 20 61 6e 64 20 72 65 73  atement] and res
2e400 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61 74  ult column.** at
2e410 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74   the same time t
2e420 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20  hen the results 
2e430 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a  are undefined..*
2e440 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
2e450 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
2e460 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
2e470 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
2e480 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  mt*,int);.SQLITE
2e490 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
2e4a0 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
2e4b0 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28  database_name16(
2e4c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
2e4d0 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  t);.SQLITE_API c
2e4e0 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
2e4f0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
2e500 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
2e510 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t*,int);.SQLITE_
2e520 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
2e530 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
2e540 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69  able_name16(sqli
2e550 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
2e560 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2e570 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
2e580 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
2e590 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
2e5a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
2e5b0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
2e5c0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
2e5d0 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65  in_name16(sqlite
2e5e0 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f  3_stmt*,int);../
2e5f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
2e600 65 63 6c 61 72 65 64 20 44 61 74 61 74 79 70 65  eclared Datatype
2e610 20 4f 66 20 41 20 51 75 65 72 79 20 52 65 73 75   Of A Query Resu
2e620 6c 74 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 66  lt.**.** ^(The f
2e630 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69  irst parameter i
2e640 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74  s a [prepared st
2e650 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20  atement]..** If 
2e660 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69  this statement i
2e670 73 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61  s a [SELECT] sta
2e680 74 65 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 4e  tement and the N
2e690 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  th column of the
2e6a0 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 72 65 73  .** returned res
2e6b0 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61 74 20  ult set of that 
2e6c0 5b 53 45 4c 45 43 54 5d 20 69 73 20 61 20 74 61  [SELECT] is a ta
2e6d0 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e 6f 74 20  ble column (not 
2e6e0 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  an.** expression
2e6f0 20 6f 72 20 73 75 62 71 75 65 72 79 29 20 74 68   or subquery) th
2e700 65 6e 20 74 68 65 20 64 65 63 6c 61 72 65 64 20  en the declared 
2e710 74 79 70 65 20 6f 66 20 74 68 65 20 74 61 62 6c  type of the tabl
2e720 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 72  e.** column is r
2e730 65 74 75 72 6e 65 64 2e 29 5e 20 20 5e 49 66 20  eturned.)^  ^If 
2e740 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f  the Nth column o
2e750 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  f the result set
2e760 20 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73   is an.** expres
2e770 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79  sion or subquery
2e780 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f  , then a NULL po
2e790 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65  inter is returne
2e7a0 64 2e 0a 2a 2a 20 5e 54 68 65 20 72 65 74 75 72  d..** ^The retur
2e7b0 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c  ned string is al
2e7c0 77 61 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64  ways UTF-8 encod
2e7d0 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 28 46 6f 72 20  ed..**.** ^(For 
2e7e0 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e 20 74  example, given t
2e7f0 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
2e800 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45  ma:.**.** CREATE
2e810 20 54 41 42 4c 45 20 74 31 28 63 31 20 56 41 52   TABLE t1(c1 VAR
2e820 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20 61 6e 64  IANT);.**.** and
2e830 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
2e840 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 63  tatement to be c
2e850 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a 20 53  ompiled:.**.** S
2e860 45 4c 45 43 54 20 63 31 20 2b 20 31 2c 20 63 31  ELECT c1 + 1, c1
2e870 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a 2a 20   FROM t1;.**.** 
2e880 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 75  this routine wou
2e890 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 73 74  ld return the st
2e8a0 72 69 6e 67 20 22 56 41 52 49 41 4e 54 22 20 66  ring "VARIANT" f
2e8b0 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 72 65  or the second re
2e8c0 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 28  sult.** column (
2e8d0 69 3d 3d 31 29 2c 20 61 6e 64 20 61 20 4e 55 4c  i==1), and a NUL
2e8e0 4c 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68  L pointer for th
2e8f0 65 20 66 69 72 73 74 20 72 65 73 75 6c 74 20 63  e first result c
2e900 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 2e 29 5e 0a  olumn (i==0).)^.
2e910 2a 2a 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 75 73  **.** ^SQLite us
2e920 65 73 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d 74  es dynamic run-t
2e930 69 6d 65 20 74 79 70 69 6e 67 2e 20 20 5e 53 6f  ime typing.  ^So
2e940 20 6a 75 73 74 20 62 65 63 61 75 73 65 20 61 20   just because a 
2e950 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63  column.** is dec
2e960 6c 61 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e  lared to contain
2e970 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 79   a particular ty
2e980 70 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e  pe does not mean
2e990 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 74   that the.** dat
2e9a0 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 61 74  a stored in that
2e9b0 20 63 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68   column is of th
2e9c0 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 2e  e declared type.
2e9d0 20 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73    SQLite is.** s
2e9e0 74 72 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62  trongly typed, b
2e9f0 75 74 20 74 68 65 20 74 79 70 69 6e 67 20 69 73  ut the typing is
2ea00 20 64 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61   dynamic not sta
2ea10 74 69 63 2e 20 20 5e 54 79 70 65 0a 2a 2a 20 69  tic.  ^Type.** i
2ea20 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
2ea30 68 20 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c  h individual val
2ea40 75 65 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68  ues, not with th
2ea50 65 20 63 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20  e containers.** 
2ea60 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f  used to hold tho
2ea70 73 65 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51  se values..*/.SQ
2ea80 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
2ea90 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  har *sqlite3_col
2eaa0 75 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c  umn_decltype(sql
2eab0 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
2eac0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
2ead0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
2eae0 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31  column_decltype1
2eaf0 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  6(sqlite3_stmt*,
2eb00 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  int);../*.** CAP
2eb10 49 33 52 45 46 3a 20 45 76 61 6c 75 61 74 65 20  I3REF: Evaluate 
2eb20 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74  An SQL Statement
2eb30 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20 5b  .**.** After a [
2eb40 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2eb50 6e 74 5d 20 68 61 73 20 62 65 65 6e 20 70 72 65  nt] has been pre
2eb60 70 61 72 65 64 20 75 73 69 6e 67 20 65 69 74 68  pared using eith
2eb70 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70  er.** [sqlite3_p
2eb80 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20  repare_v2()] or 
2eb90 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
2eba0 31 36 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20  16_v2()] or one 
2ebb0 6f 66 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a  of the legacy.**
2ebc0 20 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c   interfaces [sql
2ebd0 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20  ite3_prepare()] 
2ebe0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  or [sqlite3_prep
2ebf0 61 72 65 31 36 28 29 5d 2c 20 74 68 69 73 20 66  are16()], this f
2ec00 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20  unction.** must 
2ec10 62 65 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72  be called one or
2ec20 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 65   more times to e
2ec30 76 61 6c 75 61 74 65 20 74 68 65 20 73 74 61 74  valuate the stat
2ec40 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ement..**.** The
2ec50 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20   details of the 
2ec60 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20  behavior of the 
2ec70 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69  sqlite3_step() i
2ec80 6e 74 65 72 66 61 63 65 20 64 65 70 65 6e 64 0a  nterface depend.
2ec90 2a 2a 20 6f 6e 20 77 68 65 74 68 65 72 20 74 68  ** on whether th
2eca0 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20  e statement was 
2ecb0 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 74  prepared using t
2ecc0 68 65 20 6e 65 77 65 72 20 22 76 32 22 20 69 6e  he newer "v2" in
2ecd0 74 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71 6c 69  terface.** [sqli
2ece0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
2ecf0 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70  ] and [sqlite3_p
2ed00 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f  repare16_v2()] o
2ed10 72 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 67 61  r the older lega
2ed20 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  cy.** interface 
2ed30 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
2ed40 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
2ed50 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2e 20 20  _prepare16()].  
2ed60 54 68 65 20 75 73 65 20 6f 66 20 74 68 65 0a 2a  The use of the.*
2ed70 2a 20 6e 65 77 20 22 76 32 22 20 69 6e 74 65 72  * new "v2" inter
2ed80 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e  face is recommen
2ed90 64 65 64 20 66 6f 72 20 6e 65 77 20 61 70 70 6c  ded for new appl
2eda0 69 63 61 74 69 6f 6e 73 20 62 75 74 20 74 68 65  ications but the
2edb0 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72   legacy.** inter
2edc0 66 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e  face will contin
2edd0 75 65 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74  ue to be support
2ede0 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 49 6e 20 74 68  ed..**.** ^In th
2edf0 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61  e legacy interfa
2ee00 63 65 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76  ce, the return v
2ee10 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 65 69 74  alue will be eit
2ee20 68 65 72 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  her [SQLITE_BUSY
2ee30 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f  ],.** [SQLITE_DO
2ee40 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57  NE], [SQLITE_ROW
2ee50 5d 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  ], [SQLITE_ERROR
2ee60 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49  ], or [SQLITE_MI
2ee70 53 55 53 45 5d 2e 0a 2a 2a 20 5e 57 69 74 68 20  SUSE]..** ^With 
2ee80 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61  the "v2" interfa
2ee90 63 65 2c 20 61 6e 79 20 6f 66 20 74 68 65 20 6f  ce, any of the o
2eea0 74 68 65 72 20 5b 72 65 73 75 6c 74 20 63 6f 64  ther [result cod
2eeb0 65 73 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e  es] or.** [exten
2eec0 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73  ded result codes
2eed0 5d 20 6d 69 67 68 74 20 62 65 20 72 65 74 75 72  ] might be retur
2eee0 6e 65 64 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a 0a  ned as well..**.
2eef0 2a 2a 20 5e 5b 53 51 4c 49 54 45 5f 42 55 53 59  ** ^[SQLITE_BUSY
2ef00 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  ] means that the
2ef10 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65   database engine
2ef20 20 77 61 73 20 75 6e 61 62 6c 65 20 74 6f 20 61   was unable to a
2ef30 63 71 75 69 72 65 20 74 68 65 0a 2a 2a 20 64 61  cquire the.** da
2ef40 74 61 62 61 73 65 20 6c 6f 63 6b 73 20 69 74 20  tabase locks it 
2ef50 6e 65 65 64 73 20 74 6f 20 64 6f 20 69 74 73 20  needs to do its 
2ef60 6a 6f 62 2e 20 20 5e 49 66 20 74 68 65 20 73 74  job.  ^If the st
2ef70 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b 43 4f  atement is a [CO
2ef80 4d 4d 49 54 5d 0a 2a 2a 20 6f 72 20 6f 63 63 75  MMIT].** or occu
2ef90 72 73 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e  rs outside of an
2efa0 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61   explicit transa
2efb0 63 74 69 6f 6e 2c 20 74 68 65 6e 20 79 6f 75 20  ction, then you 
2efc0 63 61 6e 20 72 65 74 72 79 20 74 68 65 0a 2a 2a  can retry the.**
2efd0 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 66 20   statement.  If 
2efe0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
2eff0 20 6e 6f 74 20 61 20 5b 43 4f 4d 4d 49 54 5d 20   not a [COMMIT] 
2f000 61 6e 64 20 6f 63 63 75 72 73 20 77 69 74 68 69  and occurs withi
2f010 6e 20 61 6e 0a 2a 2a 20 65 78 70 6c 69 63 69 74  n an.** explicit
2f020 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 68 65   transaction the
2f030 6e 20 79 6f 75 20 73 68 6f 75 6c 64 20 72 6f 6c  n you should rol
2f040 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61  lback the transa
2f050 63 74 69 6f 6e 20 62 65 66 6f 72 65 0a 2a 2a 20  ction before.** 
2f060 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2a 0a 2a  continuing..**.*
2f070 2a 20 5e 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d  * ^[SQLITE_DONE]
2f080 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
2f090 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 66 69  statement has fi
2f0a0 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e 67  nished executing
2f0b0 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79  .** successfully
2f0c0 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
2f0d0 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ) should not be 
2f0e0 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 20  called again on 
2f0f0 74 68 69 73 20 76 69 72 74 75 61 6c 0a 2a 2a 20  this virtual.** 
2f100 6d 61 63 68 69 6e 65 20 77 69 74 68 6f 75 74 20  machine without 
2f110 66 69 72 73 74 20 63 61 6c 6c 69 6e 67 20 5b 73  first calling [s
2f120 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20  qlite3_reset()] 
2f130 74 6f 20 72 65 73 65 74 20 74 68 65 20 76 69 72  to reset the vir
2f140 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 20  tual.** machine 
2f150 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74  back to its init
2f160 69 61 6c 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a  ial state..**.**
2f170 20 5e 49 66 20 74 68 65 20 53 51 4c 20 73 74 61   ^If the SQL sta
2f180 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 78 65  tement being exe
2f190 63 75 74 65 64 20 72 65 74 75 72 6e 73 20 61 6e  cuted returns an
2f1a0 79 20 64 61 74 61 2c 20 74 68 65 6e 20 5b 53 51  y data, then [SQ
2f1b0 4c 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20 69 73 20  LITE_ROW].** is 
2f1c0 72 65 74 75 72 6e 65 64 20 65 61 63 68 20 74 69  returned each ti
2f1d0 6d 65 20 61 20 6e 65 77 20 72 6f 77 20 6f 66 20  me a new row of 
2f1e0 64 61 74 61 20 69 73 20 72 65 61 64 79 20 66 6f  data is ready fo
2f1f0 72 20 70 72 6f 63 65 73 73 69 6e 67 20 62 79 20  r processing by 
2f200 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20 54  the.** caller. T
2f210 68 65 20 76 61 6c 75 65 73 20 6d 61 79 20 62 65  he values may be
2f220 20 61 63 63 65 73 73 65 64 20 75 73 69 6e 67 20   accessed using 
2f230 74 68 65 20 5b 63 6f 6c 75 6d 6e 20 61 63 63 65  the [column acce
2f240 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a  ss functions]..*
2f250 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  * sqlite3_step()
2f260 20 69 73 20 63 61 6c 6c 65 64 20 61 67 61 69 6e   is called again
2f270 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65   to retrieve the
2f280 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 64 61 74   next row of dat
2f290 61 2e 0a 2a 2a 0a 2a 2a 20 5e 5b 53 51 4c 49 54  a..**.** ^[SQLIT
2f2a0 45 5f 45 52 52 4f 52 5d 20 6d 65 61 6e 73 20 74  E_ERROR] means t
2f2b0 68 61 74 20 61 20 72 75 6e 2d 74 69 6d 65 20 65  hat a run-time e
2f2c0 72 72 6f 72 20 28 73 75 63 68 20 61 73 20 61 20  rror (such as a 
2f2d0 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76 69  constraint.** vi
2f2e0 6f 6c 61 74 69 6f 6e 29 20 68 61 73 20 6f 63 63  olation) has occ
2f2f0 75 72 72 65 64 2e 20 20 73 71 6c 69 74 65 33 5f  urred.  sqlite3_
2f300 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f  step() should no
2f310 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69  t be called agai
2f320 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d 2e 20  n on.** the VM. 
2f330 4d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  More information
2f340 20 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 62 79   may be found by
2f350 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65   calling [sqlite
2f360 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 20  3_errmsg()]..** 
2f370 5e 57 69 74 68 20 74 68 65 20 6c 65 67 61 63 79  ^With the legacy
2f380 20 69 6e 74 65 72 66 61 63 65 2c 20 61 20 6d 6f   interface, a mo
2f390 72 65 20 73 70 65 63 69 66 69 63 20 65 72 72 6f  re specific erro
2f3a0 72 20 63 6f 64 65 20 28 66 6f 72 20 65 78 61 6d  r code (for exam
2f3b0 70 6c 65 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  ple,.** [SQLITE_
2f3c0 49 4e 54 45 52 52 55 50 54 5d 2c 20 5b 53 51 4c  INTERRUPT], [SQL
2f3d0 49 54 45 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 51  ITE_SCHEMA], [SQ
2f3e0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5d 2c 20 61  LITE_CORRUPT], a
2f3f0 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a 2a 2a 20  nd so forth).** 
2f400 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 20  can be obtained 
2f410 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  by calling [sqli
2f420 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 6e 20  te3_reset()] on 
2f430 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64  the.** [prepared
2f440 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 5e 49   statement].  ^I
2f450 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72  n the "v2" inter
2f460 66 61 63 65 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72  face,.** the mor
2f470 65 20 73 70 65 63 69 66 69 63 20 65 72 72 6f 72  e specific error
2f480 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
2f490 64 20 64 69 72 65 63 74 6c 79 20 62 79 20 73 71  d directly by sq
2f4a0 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2a  lite3_step()..**
2f4b0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55  .** [SQLITE_MISU
2f4c0 53 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74  SE] means that t
2f4d0 68 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  he this routine 
2f4e0 77 61 73 20 63 61 6c 6c 65 64 20 69 6e 61 70 70  was called inapp
2f4f0 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 20 50  ropriately..** P
2f500 65 72 68 61 70 73 20 69 74 20 77 61 73 20 63 61  erhaps it was ca
2f510 6c 6c 65 64 20 6f 6e 20 61 20 5b 70 72 65 70 61  lled on a [prepa
2f520 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74  red statement] t
2f530 68 61 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61  hat has.** alrea
2f540 64 79 20 62 65 65 6e 20 5b 73 71 6c 69 74 65 33  dy been [sqlite3
2f550 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61  _finalize | fina
2f560 6c 69 7a 65 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65  lized] or on one
2f570 20 74 68 61 74 20 68 61 64 0a 2a 2a 20 70 72 65   that had.** pre
2f580 76 69 6f 75 73 6c 79 20 72 65 74 75 72 6e 65 64  viously returned
2f590 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
2f5a0 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d  or [SQLITE_DONE]
2f5b0 2e 20 20 4f 72 20 69 74 20 63 6f 75 6c 64 0a 2a  .  Or it could.*
2f5c0 2a 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68  * be the case th
2f5d0 61 74 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  at the same data
2f5e0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2f5f0 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 62 79  is being used by
2f600 20 74 77 6f 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20   two or.** more 
2f610 74 68 72 65 61 64 73 20 61 74 20 74 68 65 20 73  threads at the s
2f620 61 6d 65 20 6d 6f 6d 65 6e 74 20 69 6e 20 74 69  ame moment in ti
2f630 6d 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6c  me..**.** For al
2f640 6c 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  l versions of SQ
2f650 4c 69 74 65 20 75 70 20 74 6f 20 61 6e 64 20 69  Lite up to and i
2f660 6e 63 6c 75 64 69 6e 67 20 33 2e 36 2e 32 33 2e  ncluding 3.6.23.
2f670 31 2c 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  1, a call to.** 
2f680 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
2f690 5d 20 77 61 73 20 72 65 71 75 69 72 65 64 20 61  ] was required a
2f6a0 66 74 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65  fter sqlite3_ste
2f6b0 70 28 29 20 72 65 74 75 72 6e 65 64 20 61 6e 79  p() returned any
2f6c0 74 68 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 20 74  thing.** other t
2f6d0 68 61 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d  han [SQLITE_ROW]
2f6e0 20 62 65 66 6f 72 65 20 61 6e 79 20 73 75 62 73   before any subs
2f6f0 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f  equent invocatio
2f700 6e 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  n of.** sqlite3_
2f710 73 74 65 70 28 29 2e 20 20 46 61 69 6c 75 72 65  step().  Failure
2f720 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 70 72   to reset the pr
2f730 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2f740 20 75 73 69 6e 67 20 0a 2a 2a 20 5b 73 71 6c 69   using .** [sqli
2f750 74 65 33 5f 72 65 73 65 74 28 29 5d 20 77 6f 75  te3_reset()] wou
2f760 6c 64 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20  ld result in an 
2f770 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20  [SQLITE_MISUSE] 
2f780 72 65 74 75 72 6e 20 66 72 6f 6d 0a 2a 2a 20 73  return from.** s
2f790 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20  qlite3_step().  
2f7a0 42 75 74 20 61 66 74 65 72 20 76 65 72 73 69 6f  But after versio
2f7b0 6e 20 33 2e 36 2e 32 33 2e 31 2c 20 73 71 6c 69  n 3.6.23.1, sqli
2f7c0 74 65 33 5f 73 74 65 70 28 29 20 62 65 67 61 6e  te3_step() began
2f7d0 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c  .** calling [sql
2f7e0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 61 75  ite3_reset()] au
2f7f0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 20 74  tomatically in t
2f800 68 69 73 20 63 69 72 63 75 6d 73 74 61 6e 63 65  his circumstance
2f810 20 72 61 74 68 65 72 0a 2a 2a 20 74 68 61 6e 20   rather.** than 
2f820 72 65 74 75 72 6e 69 6e 67 20 5b 53 51 4c 49 54  returning [SQLIT
2f830 45 5f 4d 49 53 55 53 45 5d 2e 20 20 54 68 69 73  E_MISUSE].  This
2f840 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72   is not consider
2f850 65 64 20 61 20 63 6f 6d 70 61 74 69 62 69 6c 69  ed a compatibili
2f860 74 79 0a 2a 2a 20 62 72 65 61 6b 20 62 65 63 61  ty.** break beca
2f870 75 73 65 20 61 6e 79 20 61 70 70 6c 69 63 61 74  use any applicat
2f880 69 6f 6e 20 74 68 61 74 20 65 76 65 72 20 72 65  ion that ever re
2f890 63 65 69 76 65 73 20 61 6e 20 53 51 4c 49 54 45  ceives an SQLITE
2f8a0 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 0a 2a 2a  _MISUSE error.**
2f8b0 20 69 73 20 62 72 6f 6b 65 6e 20 62 79 20 64 65   is broken by de
2f8c0 66 69 6e 69 74 69 6f 6e 2e 20 20 54 68 65 20 5b  finition.  The [
2f8d0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
2f8e0 52 45 53 45 54 5d 20 63 6f 6d 70 69 6c 65 2d 74  RESET] compile-t
2f8f0 69 6d 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 63 61  ime option.** ca
2f900 6e 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 73  n be used to res
2f910 74 6f 72 65 20 74 68 65 20 6c 65 67 61 63 79 20  tore the legacy 
2f920 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20  behavior..**.** 
2f930 3c 62 3e 47 6f 6f 66 79 20 49 6e 74 65 72 66 61  <b>Goofy Interfa
2f940 63 65 20 41 6c 65 72 74 3a 3c 2f 62 3e 20 49 6e  ce Alert:</b> In
2f950 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65   the legacy inte
2f960 72 66 61 63 65 2c 20 74 68 65 20 73 71 6c 69 74  rface, the sqlit
2f970 65 33 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 49  e3_step().** API
2f980 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20   always returns 
2f990 61 20 67 65 6e 65 72 69 63 20 65 72 72 6f 72 20  a generic error 
2f9a0 63 6f 64 65 2c 20 5b 53 51 4c 49 54 45 5f 45 52  code, [SQLITE_ER
2f9b0 52 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69 6e 67 20  ROR], following 
2f9c0 61 6e 79 0a 2a 2a 20 65 72 72 6f 72 20 6f 74 68  any.** error oth
2f9d0 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f  er than [SQLITE_
2f9e0 42 55 53 59 5d 20 61 6e 64 20 5b 53 51 4c 49 54  BUSY] and [SQLIT
2f9f0 45 5f 4d 49 53 55 53 45 5d 2e 20 20 59 6f 75 20  E_MISUSE].  You 
2fa00 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 5b 73 71  must call.** [sq
2fa10 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f  lite3_reset()] o
2fa20 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  r [sqlite3_final
2fa30 69 7a 65 28 29 5d 20 69 6e 20 6f 72 64 65 72 20  ize()] in order 
2fa40 74 6f 20 66 69 6e 64 20 6f 6e 65 20 6f 66 20 74  to find one of t
2fa50 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 5b  he.** specific [
2fa60 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 74 68 61  error codes] tha
2fa70 74 20 62 65 74 74 65 72 20 64 65 73 63 72 69 62  t better describ
2fa80 65 73 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2a  es the error..**
2fa90 20 57 65 20 61 64 6d 69 74 20 74 68 61 74 20 74   We admit that t
2faa0 68 69 73 20 69 73 20 61 20 67 6f 6f 66 79 20 64  his is a goofy d
2fab0 65 73 69 67 6e 2e 20 20 54 68 65 20 70 72 6f 62  esign.  The prob
2fac0 6c 65 6d 20 68 61 73 20 62 65 65 6e 20 66 69 78  lem has been fix
2fad0 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 22  ed.** with the "
2fae0 76 32 22 20 69 6e 74 65 72 66 61 63 65 2e 20 20  v2" interface.  
2faf0 49 66 20 79 6f 75 20 70 72 65 70 61 72 65 20 61  If you prepare a
2fb00 6c 6c 20 6f 66 20 79 6f 75 72 20 53 51 4c 20 73  ll of your SQL s
2fb10 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 75 73 69  tatements.** usi
2fb20 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74  ng either [sqlit
2fb30 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
2fb40 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   or [sqlite3_pre
2fb50 70 61 72 65 31 36 5f 76 32 28 29 5d 20 69 6e 73  pare16_v2()] ins
2fb60 74 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6c  tead.** of the l
2fb70 65 67 61 63 79 20 5b 73 71 6c 69 74 65 33 5f 70  egacy [sqlite3_p
2fb80 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b 73  repare()] and [s
2fb90 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
2fba0 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 2c 0a  ()] interfaces,.
2fbb0 2a 2a 20 74 68 65 6e 20 74 68 65 20 6d 6f 72 65  ** then the more
2fbc0 20 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72   specific [error
2fbd0 20 63 6f 64 65 73 5d 20 61 72 65 20 72 65 74 75   codes] are retu
2fbe0 72 6e 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a  rned directly.**
2fbf0 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70   by sqlite3_step
2fc00 28 29 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20  ().  The use of 
2fc10 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61  the "v2" interfa
2fc20 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65  ce is recommende
2fc30 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
2fc40 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65   int sqlite3_ste
2fc50 70 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  p(sqlite3_stmt*)
2fc60 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
2fc70 46 3a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  F: Number of col
2fc80 75 6d 6e 73 20 69 6e 20 61 20 72 65 73 75 6c 74  umns in a result
2fc90 20 73 65 74 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20   set.**.** ^The 
2fca0 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75  sqlite3_data_cou
2fcb0 6e 74 28 50 29 20 69 6e 74 65 72 66 61 63 65 20  nt(P) interface 
2fcc0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
2fcd0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
2fce0 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20   the.** current 
2fcf0 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c  row of the resul
2fd00 74 20 73 65 74 20 6f 66 20 5b 70 72 65 70 61 72  t set of [prepar
2fd10 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 50 2e  ed statement] P.
2fd20 0a 2a 2a 20 5e 49 66 20 70 72 65 70 61 72 65 64  .** ^If prepared
2fd30 20 73 74 61 74 65 6d 65 6e 74 20 50 20 64 6f 65   statement P doe
2fd40 73 20 6e 6f 74 20 68 61 76 65 20 72 65 73 75 6c  s not have resul
2fd50 74 73 20 72 65 61 64 79 20 74 6f 20 72 65 74 75  ts ready to retu
2fd60 72 6e 0a 2a 2a 20 28 76 69 61 20 63 61 6c 6c 73  rn.** (via calls
2fd70 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33   to the [sqlite3
2fd80 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 20 7c 20 73 71  _column_int | sq
2fd90 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 2a 28 29  lite3_column_*()
2fda0 5d 20 6f 66 0a 2a 2a 20 69 6e 74 65 72 66 61 63  ] of.** interfac
2fdb0 65 73 29 20 74 68 65 6e 20 73 71 6c 69 74 65 33  es) then sqlite3
2fdc0 5f 64 61 74 61 5f 63 6f 75 6e 74 28 50 29 20 72  _data_count(P) r
2fdd0 65 74 75 72 6e 73 20 30 2e 0a 2a 2a 20 5e 54 68  eturns 0..** ^Th
2fde0 65 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63  e sqlite3_data_c
2fdf0 6f 75 6e 74 28 50 29 20 72 6f 75 74 69 6e 65 20  ount(P) routine 
2fe00 61 6c 73 6f 20 72 65 74 75 72 6e 73 20 30 20 69  also returns 0 i
2fe10 66 20 50 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f  f P is a NULL po
2fe20 69 6e 74 65 72 2e 0a 2a 2a 20 5e 54 68 65 20 73  inter..** ^The s
2fe30 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e  qlite3_data_coun
2fe40 74 28 50 29 20 72 6f 75 74 69 6e 65 20 72 65 74  t(P) routine ret
2fe50 75 72 6e 73 20 30 20 69 66 20 74 68 65 20 70 72  urns 0 if the pr
2fe60 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 0a 2a  evious call to.*
2fe70 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 5d  * [sqlite3_step]
2fe80 28 50 29 20 72 65 74 75 72 6e 65 64 20 5b 53 51  (P) returned [SQ
2fe90 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 20 5e 54 68  LITE_DONE].  ^Th
2fea0 65 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63  e sqlite3_data_c
2feb0 6f 75 6e 74 28 50 29 0a 2a 2a 20 77 69 6c 6c 20  ount(P).** will 
2fec0 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  return non-zero 
2fed0 69 66 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c  if previous call
2fee0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   to [sqlite3_ste
2fef0 70 5d 28 50 29 20 72 65 74 75 72 6e 65 64 0a 2a  p](P) returned.*
2ff00 2a 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c 20  * [SQLITE_ROW], 
2ff10 65 78 63 65 70 74 20 69 6e 20 74 68 65 20 63 61  except in the ca
2ff20 73 65 20 6f 66 20 74 68 65 20 5b 50 52 41 47 4d  se of the [PRAGM
2ff30 41 20 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 76 61  A incremental_va
2ff40 63 75 75 6d 5d 0a 2a 2a 20 77 68 65 72 65 20 69  cuum].** where i
2ff50 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73  t always returns
2ff60 20 7a 65 72 6f 20 73 69 6e 63 65 20 65 61 63 68   zero since each
2ff70 20 73 74 65 70 20 6f 66 20 74 68 61 74 20 6d 75   step of that mu
2ff80 6c 74 69 2d 73 74 65 70 0a 2a 2a 20 70 72 61 67  lti-step.** prag
2ff90 6d 61 20 72 65 74 75 72 6e 73 20 30 20 63 6f 6c  ma returns 0 col
2ffa0 75 6d 6e 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a  umns of data..**
2ffb0 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
2ffc0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f  qlite3_column_co
2ffd0 75 6e 74 28 29 5d 0a 2a 2f 0a 53 51 4c 49 54 45  unt()].*/.SQLITE
2ffe0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2fff0 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69  _data_count(sqli
30000 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
30010 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
30020 46 3a 20 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44  F: Fundamental D
30030 61 74 61 74 79 70 65 73 0a 2a 2a 20 4b 45 59 57  atatypes.** KEYW
30040 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 54 45 58  ORDS: SQLITE_TEX
30050 54 0a 2a 2a 0a 2a 2a 20 5e 28 45 76 65 72 79 20  T.**.** ^(Every 
30060 76 61 6c 75 65 20 69 6e 20 53 51 4c 69 74 65 20  value in SQLite 
30070 68 61 73 20 6f 6e 65 20 6f 66 20 66 69 76 65 20  has one of five 
30080 66 75 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74 61  fundamental data
30090 74 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  types:.**.** <ul
300a0 3e 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74  >.** <li> 64-bit
300b0 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a   signed integer.
300c0 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 49  ** <li> 64-bit I
300d0 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  EEE floating poi
300e0 6e 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c 69  nt number.** <li
300f0 3e 20 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69 3e  > string.** <li>
30100 20 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e 55   BLOB.** <li> NU
30110 4c 4c 0a 2a 2a 20 3c 2f 75 6c 3e 29 5e 0a 2a 2a  LL.** </ul>)^.**
30120 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61  .** These consta
30130 6e 74 73 20 61 72 65 20 63 6f 64 65 73 20 66 6f  nts are codes fo
30140 72 20 65 61 63 68 20 6f 66 20 74 68 6f 73 65 20  r each of those 
30150 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  types..**.** Not
30160 65 20 74 68 61 74 20 74 68 65 20 53 51 4c 49 54  e that the SQLIT
30170 45 5f 54 45 58 54 20 63 6f 6e 73 74 61 6e 74 20  E_TEXT constant 
30180 77 61 73 20 61 6c 73 6f 20 75 73 65 64 20 69 6e  was also used in
30190 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20   SQLite version 
301a0 32 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6d 70 6c  2.** for a compl
301b0 65 74 65 6c 79 20 64 69 66 66 65 72 65 6e 74 20  etely different 
301c0 6d 65 61 6e 69 6e 67 2e 20 20 53 6f 66 74 77 61  meaning.  Softwa
301d0 72 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67  re that links ag
301e0 61 69 6e 73 74 20 62 6f 74 68 0a 2a 2a 20 53 51  ainst both.** SQ
301f0 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 32 20 61  Lite version 2 a
30200 6e 64 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f  nd SQLite versio
30210 6e 20 33 20 73 68 6f 75 6c 64 20 75 73 65 20 53  n 3 should use S
30220 51 4c 49 54 45 33 5f 54 45 58 54 2c 20 6e 6f 74  QLITE3_TEXT, not
30230 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45 58 54 2e  .** SQLITE_TEXT.
30240 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
30250 54 45 5f 49 4e 54 45 47 45 52 20 20 31 0a 23 64  TE_INTEGER  1.#d
30260 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 4c 4f  efine SQLITE_FLO
30270 41 54 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20  AT    2.#define 
30280 53 51 4c 49 54 45 5f 42 4c 4f 42 20 20 20 20 20  SQLITE_BLOB     
30290 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  4.#define SQLITE
302a0 5f 4e 55 4c 4c 20 20 20 20 20 35 0a 23 69 66 64  _NULL     5.#ifd
302b0 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23  ef SQLITE_TEXT.#
302c0 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45   undef SQLITE_TE
302d0 58 54 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  XT.#else.# defin
302e0 65 20 53 51 4c 49 54 45 5f 54 45 58 54 20 20 20  e SQLITE_TEXT   
302f0 20 20 33 0a 23 65 6e 64 69 66 0a 23 64 65 66 69    3.#endif.#defi
30300 6e 65 20 53 51 4c 49 54 45 33 5f 54 45 58 54 20  ne SQLITE3_TEXT 
30310 20 20 20 20 33 0a 0a 2f 2a 0a 2a 2a 20 43 41 50      3../*.** CAP
30320 49 33 52 45 46 3a 20 52 65 73 75 6c 74 20 56 61  I3REF: Result Va
30330 6c 75 65 73 20 46 72 6f 6d 20 41 20 51 75 65 72  lues From A Quer
30340 79 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  y.** KEYWORDS: {
30350 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75  column access fu
30360 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54  nctions}.**.** T
30370 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 66 6f  hese routines fo
30380 72 6d 20 74 68 65 20 22 72 65 73 75 6c 74 20 73  rm the "result s
30390 65 74 22 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  et" interface..*
303a0 2a 0a 2a 2a 20 5e 54 68 65 73 65 20 72 6f 75 74  *.** ^These rout
303b0 69 6e 65 73 20 72 65 74 75 72 6e 20 69 6e 66 6f  ines return info
303c0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 20  rmation about a 
303d0 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66  single column of
303e0 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20   the current.** 
303f0 72 65 73 75 6c 74 20 72 6f 77 20 6f 66 20 61 20  result row of a 
30400 71 75 65 72 79 2e 20 20 5e 49 6e 20 65 76 65 72  query.  ^In ever
30410 79 20 63 61 73 65 20 74 68 65 20 66 69 72 73 74  y case the first
30420 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70   argument is a p
30430 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65  ointer.** to the
30440 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
30450 6d 65 6e 74 5d 20 74 68 61 74 20 69 73 20 62 65  ment] that is be
30460 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 28 74  ing evaluated (t
30470 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74  he [sqlite3_stmt
30480 2a 5d 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 72  *].** that was r
30490 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b 73 71  eturned from [sq
304a0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
304b0 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 69 74  ()] or one of it
304c0 73 20 76 61 72 69 61 6e 74 73 29 0a 2a 2a 20 61  s variants).** a
304d0 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  nd the second ar
304e0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e  gument is the in
304f0 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  dex of the colum
30500 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e 66 6f  n for which info
30510 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c  rmation.** shoul
30520 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 5e  d be returned. ^
30530 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c  The leftmost col
30540 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c  umn of the resul
30550 74 20 73 65 74 20 68 61 73 20 74 68 65 20 69 6e  t set has the in
30560 64 65 78 20 30 2e 0a 2a 2a 20 5e 54 68 65 20 6e  dex 0..** ^The n
30570 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
30580 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 63   in the result c
30590 61 6e 20 62 65 20 64 65 74 65 72 6d 69 6e 65 64  an be determined
305a0 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74   using.** [sqlit
305b0 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28  e3_column_count(
305c0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  )]..**.** If the
305d0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 64   SQL statement d
305e0 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c  oes not currentl
305f0 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c  y point to a val
30600 69 64 20 72 6f 77 2c 20 6f 72 20 69 66 20 74 68  id row, or if th
30610 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65  e.** column inde
30620 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  x is out of rang
30630 65 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  e, the result is
30640 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 54   undefined..** T
30650 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 61  hese routines ma
30660 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64  y only be called
30670 20 77 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 72   when the most r
30680 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a  ecent call to.**
30690 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
306a0 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b  ] has returned [
306b0 53 51 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 20  SQLITE_ROW] and 
306c0 6e 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69  neither.** [sqli
306d0 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f 72  te3_reset()] nor
306e0 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
306f0 7a 65 28 29 5d 20 68 61 76 65 20 62 65 65 6e 20  ze()] have been 
30700 63 61 6c 6c 65 64 20 73 75 62 73 65 71 75 65 6e  called subsequen
30710 74 6c 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f  tly..** If any o
30720 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  f these routines
30730 20 61 72 65 20 63 61 6c 6c 65 64 20 61 66 74 65   are called afte
30740 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  r [sqlite3_reset
30750 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74  ()] or.** [sqlit
30760 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f  e3_finalize()] o
30770 72 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33  r after [sqlite3
30780 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 74  _step()] has ret
30790 75 72 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 69  urned.** somethi
307a0 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53  ng other than [S
307b0 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20  QLITE_ROW], the 
307c0 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65  results are unde
307d0 66 69 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 71  fined..** If [sq
307e0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72  lite3_step()] or
307f0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
30800 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66  )] or [sqlite3_f
30810 69 6e 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 72  inalize()].** ar
30820 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20  e called from a 
30830 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64  different thread
30840 20 77 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 68   while any of th
30850 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20  ese routines.** 
30860 61 72 65 20 70 65 6e 64 69 6e 67 2c 20 74 68 65  are pending, the
30870 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 72  n the results ar
30880 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  e undefined..**.
30890 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f  ** ^The sqlite3_
308a0 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f  column_type() ro
308b0 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68  utine returns th
308c0 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54  e.** [SQLITE_INT
308d0 45 47 45 52 20 7c 20 64 61 74 61 74 79 70 65 20  EGER | datatype 
308e0 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 69 6e  code] for the in
308f0 69 74 69 61 6c 20 64 61 74 61 20 74 79 70 65 0a  itial data type.
30900 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ** of the result
30910 20 63 6f 6c 75 6d 6e 2e 20 20 5e 54 68 65 20 72   column.  ^The r
30920 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73  eturned value is
30930 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f   one of [SQLITE_
30940 49 4e 54 45 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51  INTEGER],.** [SQ
30950 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51  LITE_FLOAT], [SQ
30960 4c 49 54 45 5f 54 45 58 54 5d 2c 20 5b 53 51 4c  LITE_TEXT], [SQL
30970 49 54 45 5f 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53  ITE_BLOB], or [S
30980 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68  QLITE_NULL].  Th
30990 65 20 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72  e value.** retur
309a0 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63  ned by sqlite3_c
309b0 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 20  olumn_type() is 
309c0 6f 6e 6c 79 20 6d 65 61 6e 69 6e 67 66 75 6c 20  only meaningful 
309d0 69 66 20 6e 6f 20 74 79 70 65 0a 2a 2a 20 63 6f  if no type.** co
309e0 6e 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 6f  nversions have o
309f0 63 63 75 72 72 65 64 20 61 73 20 64 65 73 63 72  ccurred as descr
30a00 69 62 65 64 20 62 65 6c 6f 77 2e 20 20 41 66 74  ibed below.  Aft
30a10 65 72 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72  er a type conver
30a20 73 69 6f 6e 2c 0a 2a 2a 20 74 68 65 20 76 61 6c  sion,.** the val
30a30 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ue returned by s
30a40 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
30a50 70 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65  pe() is undefine
30a60 64 2e 20 20 46 75 74 75 72 65 0a 2a 2a 20 76 65  d.  Future.** ve
30a70 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
30a80 20 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65 20   may change the 
30a90 62 65 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69  behavior of sqli
30aa0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28  te3_column_type(
30ab0 29 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61  ).** following a
30ac0 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e   type conversion
30ad0 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20 74 68 65 20  ..**.** ^If the 
30ae0 72 65 73 75 6c 74 20 69 73 20 61 20 42 4c 4f 42  result is a BLOB
30af0 20 6f 72 20 55 54 46 2d 38 20 73 74 72 69 6e 67   or UTF-8 string
30b00 20 74 68 65 6e 20 74 68 65 20 73 71 6c 69 74 65   then the sqlite
30b10 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29  3_column_bytes()
30b20 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75  .** routine retu
30b30 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
30b40 66 20 62 79 74 65 73 20 69 6e 20 74 68 61 74 20  f bytes in that 
30b50 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a  BLOB or string..
30b60 2a 2a 20 5e 49 66 20 74 68 65 20 72 65 73 75 6c  ** ^If the resul
30b70 74 20 69 73 20 61 20 55 54 46 2d 31 36 20 73 74  t is a UTF-16 st
30b80 72 69 6e 67 2c 20 74 68 65 6e 20 73 71 6c 69 74  ring, then sqlit
30b90 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
30ba0 29 20 63 6f 6e 76 65 72 74 73 0a 2a 2a 20 74 68  ) converts.** th
30bb0 65 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d  e string to UTF-
30bc0 38 20 61 6e 64 20 74 68 65 6e 20 72 65 74 75 72  8 and then retur
30bd0 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
30be0 20 62 79 74 65 73 2e 0a 2a 2a 20 5e 49 66 20 74   bytes..** ^If t
30bf0 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 6e  he result is a n
30c00 75 6d 65 72 69 63 20 76 61 6c 75 65 20 74 68 65  umeric value the
30c10 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  n sqlite3_column
30c20 5f 62 79 74 65 73 28 29 20 75 73 65 73 0a 2a 2a  _bytes() uses.**
30c30 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e   [sqlite3_snprin
30c40 74 66 28 29 5d 20 74 6f 20 63 6f 6e 76 65 72 74  tf()] to convert
30c50 20 74 68 61 74 20 76 61 6c 75 65 20 74 6f 20 61   that value to a
30c60 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 61 6e   UTF-8 string an
30c70 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65  d returns.** the
30c80 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
30c90 20 69 6e 20 74 68 61 74 20 73 74 72 69 6e 67 2e   in that string.
30ca0 0a 2a 2a 20 5e 49 66 20 74 68 65 20 72 65 73 75  .** ^If the resu
30cb0 6c 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  lt is NULL, then
30cc0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
30cd0 62 79 74 65 73 28 29 20 72 65 74 75 72 6e 73 20  bytes() returns 
30ce0 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20  zero..**.** ^If 
30cf0 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20  the result is a 
30d00 42 4c 4f 42 20 6f 72 20 55 54 46 2d 31 36 20 73  BLOB or UTF-16 s
30d10 74 72 69 6e 67 20 74 68 65 6e 20 74 68 65 20 73  tring then the s
30d20 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
30d30 74 65 73 31 36 28 29 0a 2a 2a 20 72 6f 75 74 69  tes16().** routi
30d40 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  ne returns the n
30d50 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
30d60 6e 20 74 68 61 74 20 42 4c 4f 42 20 6f 72 20 73  n that BLOB or s
30d70 74 72 69 6e 67 2e 0a 2a 2a 20 5e 49 66 20 74 68  tring..** ^If th
30d80 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 55 54  e result is a UT
30d90 46 2d 38 20 73 74 72 69 6e 67 2c 20 74 68 65 6e  F-8 string, then
30da0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
30db0 62 79 74 65 73 31 36 28 29 20 63 6f 6e 76 65 72  bytes16() conver
30dc0 74 73 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67  ts.** the string
30dd0 20 74 6f 20 55 54 46 2d 31 36 20 61 6e 64 20 74   to UTF-16 and t
30de0 68 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20  hen returns the 
30df0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 2e  number of bytes.
30e00 0a 2a 2a 20 5e 49 66 20 74 68 65 20 72 65 73 75  .** ^If the resu
30e10 6c 74 20 69 73 20 61 20 6e 75 6d 65 72 69 63 20  lt is a numeric 
30e20 76 61 6c 75 65 20 74 68 65 6e 20 73 71 6c 69 74  value then sqlit
30e30 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31  e3_column_bytes1
30e40 36 28 29 20 75 73 65 73 0a 2a 2a 20 5b 73 71 6c  6() uses.** [sql
30e50 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d  ite3_snprintf()]
30e60 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 61 74   to convert that
30e70 20 76 61 6c 75 65 20 74 6f 20 61 20 55 54 46 2d   value to a UTF-
30e80 31 36 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65  16 string and re
30e90 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d  turns.** the num
30ea0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
30eb0 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20  that string..** 
30ec0 5e 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69  ^If the result i
30ed0 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 71 6c  s NULL, then sql
30ee0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
30ef0 73 31 36 28 29 20 72 65 74 75 72 6e 73 20 7a 65  s16() returns ze
30f00 72 6f 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 76  ro..**.** ^The v
30f10 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 62  alues returned b
30f20 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  y [sqlite3_colum
30f30 6e 5f 62 79 74 65 73 28 29 5d 20 61 6e 64 20 0a  n_bytes()] and .
30f40 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ** [sqlite3_colu
30f50 6d 6e 5f 62 79 74 65 73 31 36 28 29 5d 20 64 6f  mn_bytes16()] do
30f60 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 65   not include the
30f70 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
30f80 73 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20  s at the end.** 
30f90 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20  of the string.  
30fa0 5e 46 6f 72 20 63 6c 61 72 69 74 79 3a 20 74 68  ^For clarity: th
30fb0 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65  e values returne
30fc0 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  d by.** [sqlite3
30fd0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 5d  _column_bytes()]
30fe0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f   and [sqlite3_co
30ff0 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 5d 20  lumn_bytes16()] 
31000 61 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  are the number o
31010 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 68  f.** bytes in th
31020 65 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20 74 68  e string, not th
31030 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  e number of char
31040 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 5e 53  acters..**.** ^S
31050 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20  trings returned 
31060 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  by sqlite3_colum
31070 6e 5f 74 65 78 74 28 29 20 61 6e 64 20 73 71 6c  n_text() and sql
31080 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
31090 31 36 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 65 6d  16(),.** even em
310a0 70 74 79 20 73 74 72 69 6e 67 73 2c 20 61 72 65  pty strings, are
310b0 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65 72   always zero-ter
310c0 6d 69 6e 61 74 65 64 2e 20 20 5e 54 68 65 20 72  minated.  ^The r
310d0 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 66  eturn.** value f
310e0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  rom sqlite3_colu
310f0 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72 20 61 20  mn_blob() for a 
31100 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42  zero-length BLOB
31110 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   is a NULL point
31120 65 72 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 6f  er..**.** ^The o
31130 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62  bject returned b
31140 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  y [sqlite3_colum
31150 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 61 6e  n_value()] is an
31160 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64  .** [unprotected
31170 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20   sqlite3_value] 
31180 6f 62 6a 65 63 74 2e 20 20 41 6e 20 75 6e 70 72  object.  An unpr
31190 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
311a0 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2a 20  value object.** 
311b0 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64  may only be used
311c0 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 62   with [sqlite3_b
311d0 69 6e 64 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64  ind_value()] and
311e0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
311f0 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 49 66  _value()]..** If
31200 20 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65   the [unprotecte
31210 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
31220 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64   object returned
31230 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   by.** [sqlite3_
31240 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20  column_value()] 
31250 69 73 20 75 73 65 64 20 69 6e 20 61 6e 79 20 6f  is used in any o
31260 74 68 65 72 20 77 61 79 2c 20 69 6e 63 6c 75 64  ther way, includ
31270 69 6e 67 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20  ing calls.** to 
31280 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73  routines like [s
31290 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
312a0 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61  ()], [sqlite3_va
312b0 6c 75 65 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20  lue_text()],.** 
312c0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  or [sqlite3_valu
312d0 65 5f 62 79 74 65 73 28 29 5d 2c 20 74 68 65 6e  e_bytes()], then
312e0 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73   the behavior is
312f0 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a   undefined..**.*
31300 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
31310 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e 76   attempt to conv
31320 65 72 74 20 74 68 65 20 76 61 6c 75 65 20 77 68  ert the value wh
31330 65 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e  ere appropriate.
31340 20 20 5e 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c    ^For.** exampl
31350 65 2c 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e  e, if the intern
31360 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  al representatio
31370 6e 20 69 73 20 46 4c 4f 41 54 20 61 6e 64 20 61  n is FLOAT and a
31380 20 74 65 78 74 20 72 65 73 75 6c 74 0a 2a 2a 20   text result.** 
31390 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 5b 73  is requested, [s
313a0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
313b0 29 5d 20 69 73 20 75 73 65 64 20 69 6e 74 65 72  )] is used inter
313c0 6e 61 6c 6c 79 20 74 6f 20 70 65 72 66 6f 72 6d  nally to perform
313d0 20 74 68 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69   the.** conversi
313e0 6f 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  on automatically
313f0 2e 20 20 5e 28 54 68 65 20 66 6f 6c 6c 6f 77 69  .  ^(The followi
31400 6e 67 20 74 61 62 6c 65 20 64 65 74 61 69 6c 73  ng table details
31410 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73   the conversions
31420 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 61 70 70  .** that are app
31430 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  lied:.**.** <blo
31440 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62  ckquote>.** <tab
31450 6c 65 20 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a  le border="1">.*
31460 2a 20 3c 74 72 3e 3c 74 68 3e 20 49 6e 74 65 72  * <tr><th> Inter
31470 6e 61 6c 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e  nal<br>Type <th>
31480 20 52 65 71 75 65 73 74 65 64 3c 62 72 3e 54 79   Requested<br>Ty
31490 70 65 20 3c 74 68 3e 20 20 43 6f 6e 76 65 72 73  pe <th>  Convers
314a0 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74  ion.**.** <tr><t
314b0 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e  d>  NULL    <td>
314c0 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20   INTEGER   <td> 
314d0 52 65 73 75 6c 74 20 69 73 20 30 0a 2a 2a 20 3c  Result is 0.** <
314e0 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20  tr><td>  NULL   
314f0 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20   <td>  FLOAT    
31500 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30  <td> Result is 0
31510 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  .0.** <tr><td>  
31520 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 54  NULL    <td>   T
31530 45 58 54 20 20 20 20 3c 74 64 3e 20 52 65 73 75  EXT    <td> Resu
31540 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74  lt is NULL point
31550 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  er.** <tr><td>  
31560 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20 42  NULL    <td>   B
31570 4c 4f 42 20 20 20 20 3c 74 64 3e 20 52 65 73 75  LOB    <td> Resu
31580 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74  lt is NULL point
31590 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49  er.** <tr><td> I
315a0 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 46 4c  NTEGER  <td>  FL
315b0 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76  OAT    <td> Conv
315c0 65 72 74 20 66 72 6f 6d 20 69 6e 74 65 67 65 72  ert from integer
315d0 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72   to float.** <tr
315e0 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c  ><td> INTEGER  <
315f0 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74  td>   TEXT    <t
31600 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 69  d> ASCII renderi
31610 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ng of the intege
31620 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e  r.** <tr><td> IN
31630 54 45 47 45 52 20 20 3c 74 64 3e 20 20 20 42 4c  TEGER  <td>   BL
31640 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 20  OB    <td> Same 
31650 61 73 20 49 4e 54 45 47 45 52 2d 3e 54 45 58 54  as INTEGER->TEXT
31660 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c  .** <tr><td>  FL
31670 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e 54 45 47  OAT   <td> INTEG
31680 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72  ER   <td> Conver
31690 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20 74 6f 20  t from float to 
316a0 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c  integer.** <tr><
316b0 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64  td>  FLOAT   <td
316c0 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e  >   TEXT    <td>
316d0 20 41 53 43 49 49 20 72 65 6e 64 65 72 69 6e 67   ASCII rendering
316e0 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 0a 2a 2a   of the float.**
316f0 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54   <tr><td>  FLOAT
31700 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20     <td>   BLOB  
31710 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20 46    <td> Same as F
31720 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74  LOAT->TEXT.** <t
31730 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20  r><td>  TEXT    
31740 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c  <td> INTEGER   <
31750 74 64 3e 20 55 73 65 20 61 74 6f 69 28 29 0a 2a  td> Use atoi().*
31760 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54  * <tr><td>  TEXT
31770 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20      <td>  FLOAT 
31780 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f 66     <td> Use atof
31790 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  ().** <tr><td>  
317a0 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 20 42  TEXT    <td>   B
317b0 4c 4f 42 20 20 20 20 3c 74 64 3e 20 4e 6f 20 63  LOB    <td> No c
317c0 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64  hange.** <tr><td
317d0 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20  >  BLOB    <td> 
317e0 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43  INTEGER   <td> C
317f0 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 54 20 74  onvert to TEXT t
31800 68 65 6e 20 75 73 65 20 61 74 6f 69 28 29 0a 2a  hen use atoi().*
31810 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42  * <tr><td>  BLOB
31820 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20      <td>  FLOAT 
31830 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20     <td> Convert 
31840 74 6f 20 54 45 58 54 20 74 68 65 6e 20 75 73 65  to TEXT then use
31850 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c   atof().** <tr><
31860 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64  td>  BLOB    <td
31870 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e  >   TEXT    <td>
31880 20 41 64 64 20 61 20 7a 65 72 6f 20 74 65 72 6d   Add a zero term
31890 69 6e 61 74 6f 72 20 69 66 20 6e 65 65 64 65 64  inator if needed
318a0 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20  .** </table>.** 
318b0 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 29 5e 0a  </blockquote>)^.
318c0 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20  **.** The table 
318d0 61 62 6f 76 65 20 6d 61 6b 65 73 20 72 65 66 65  above makes refe
318e0 72 65 6e 63 65 20 74 6f 20 73 74 61 6e 64 61 72  rence to standar
318f0 64 20 43 20 6c 69 62 72 61 72 79 20 66 75 6e 63  d C library func
31900 74 69 6f 6e 73 20 61 74 6f 69 28 29 0a 2a 2a 20  tions atoi().** 
31910 61 6e 64 20 61 74 6f 66 28 29 2e 20 20 53 51 4c  and atof().  SQL
31920 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 61  ite does not rea
31930 6c 6c 79 20 75 73 65 20 74 68 65 73 65 20 66 75  lly use these fu
31940 6e 63 74 69 6f 6e 73 2e 20 20 49 74 20 68 61 73  nctions.  It has
31950 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 65 71 75 69   its.** own equi
31960 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c 20  valent internal 
31970 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 20 61  routines.  The a
31980 74 6f 69 28 29 20 61 6e 64 20 61 74 6f 66 28 29  toi() and atof()
31990 20 6e 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75 73   names are.** us
319a0 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ed in the table 
319b0 66 6f 72 20 62 72 65 76 69 74 79 20 61 6e 64 20  for brevity and 
319c0 62 65 63 61 75 73 65 20 74 68 65 79 20 61 72 65  because they are
319d0 20 66 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f 73   familiar to mos
319e0 74 0a 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d 65  t.** C programme
319f0 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  rs..**.** Note t
31a00 68 61 74 20 77 68 65 6e 20 74 79 70 65 20 63 6f  hat when type co
31a10 6e 76 65 72 73 69 6f 6e 73 20 6f 63 63 75 72 2c  nversions occur,
31a20 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e   pointers return
31a30 65 64 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20 63  ed by prior.** c
31a40 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  alls to sqlite3_
31a50 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 73  column_blob(), s
31a60 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
31a70 78 74 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20  xt(), and/or.** 
31a80 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
31a90 65 78 74 31 36 28 29 20 6d 61 79 20 62 65 20 69  ext16() may be i
31aa0 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 20 54  nvalidated..** T
31ab0 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  ype conversions 
31ac0 61 6e 64 20 70 6f 69 6e 74 65 72 20 69 6e 76 61  and pointer inva
31ad0 6c 69 64 61 74 69 6f 6e 73 20 6d 69 67 68 74 20  lidations might 
31ae0 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65 20  occur.** in the 
31af0 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 65 73 3a  following cases:
31b00 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  .**.** <ul>.** <
31b10 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20  li> The initial 
31b20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20 42 4c 4f  content is a BLO
31b30 42 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f  B and sqlite3_co
31b40 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 2a  lumn_text() or.*
31b50 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  *      sqlite3_c
31b60 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69  olumn_text16() i
31b70 73 20 63 61 6c 6c 65 64 2e 20 20 41 20 7a 65 72  s called.  A zer
31b80 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 6d 69 67  o-terminator mig
31b90 68 74 0a 2a 2a 20 20 20 20 20 20 6e 65 65 64 20  ht.**      need 
31ba0 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74  to be added to t
31bb0 68 65 20 73 74 72 69 6e 67 2e 3c 2f 6c 69 3e 0a  he string.</li>.
31bc0 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74  ** <li> The init
31bd0 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55  ial content is U
31be0 54 46 2d 38 20 74 65 78 74 20 61 6e 64 20 73 71  TF-8 text and sq
31bf0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
31c00 65 73 31 36 28 29 20 6f 72 0a 2a 2a 20 20 20 20  es16() or.**    
31c10 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
31c20 5f 74 65 78 74 31 36 28 29 20 69 73 20 63 61 6c  _text16() is cal
31c30 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e  led.  The conten
31c40 74 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72  t must be conver
31c50 74 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20 55  ted.**      to U
31c60 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c  TF-16.</li>.** <
31c70 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20  li> The initial 
31c80 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 31  content is UTF-1
31c90 36 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69 74  6 text and sqlit
31ca0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
31cb0 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c  ) or.**      sql
31cc0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
31cd0 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54  () is called.  T
31ce0 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20  he content must 
31cf0 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20  be converted.** 
31d00 20 20 20 20 20 74 6f 20 55 54 46 2d 38 2e 3c 2f       to UTF-8.</
31d10 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a  li>.** </ul>.**.
31d20 2a 2a 20 5e 43 6f 6e 76 65 72 73 69 6f 6e 73 20  ** ^Conversions 
31d30 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 62 65  between UTF-16be
31d40 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 20 61 72   and UTF-16le ar
31d50 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 20 69 6e  e always done in
31d60 20 70 6c 61 63 65 20 61 6e 64 20 64 6f 0a 2a 2a   place and do.**
31d70 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20   not invalidate 
31d80 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 2c  a prior pointer,
31d90 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75 72 73   though of cours
31da0 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
31db0 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 74   the buffer.** t
31dc0 68 61 74 20 74 68 65 20 70 72 69 6f 72 20 70 6f  hat the prior po
31dd0 69 6e 74 65 72 20 72 65 66 65 72 65 6e 63 65 73  inter references
31de0 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20   will have been 
31df0 6d 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 65 72  modified.  Other
31e00 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e   kinds.** of con
31e10 76 65 72 73 69 6f 6e 20 61 72 65 20 64 6f 6e 65  version are done
31e20 20 69 6e 20 70 6c 61 63 65 20 77 68 65 6e 20 69   in place when i
31e30 74 20 69 73 20 70 6f 73 73 69 62 6c 65 2c 20 62  t is possible, b
31e40 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 74 68 65  ut sometimes the
31e50 79 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70 6f 73  y.** are not pos
31e60 73 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 68 6f  sible and in tho
31e70 73 65 20 63 61 73 65 73 20 70 72 69 6f 72 20 70  se cases prior p
31e80 6f 69 6e 74 65 72 73 20 61 72 65 20 69 6e 76 61  ointers are inva
31e90 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  lidated..**.** T
31ea0 68 65 20 73 61 66 65 73 74 20 61 6e 64 20 65 61  he safest and ea
31eb0 73 69 65 73 74 20 74 6f 20 72 65 6d 65 6d 62 65  siest to remembe
31ec0 72 20 70 6f 6c 69 63 79 20 69 73 20 74 6f 20 69  r policy is to i
31ed0 6e 76 6f 6b 65 20 74 68 65 73 65 20 72 6f 75 74  nvoke these rout
31ee0 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f  ines.** in one o
31ef0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
31f00 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e  ways:.**.** <ul>
31f10 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33  .**  <li>sqlite3
31f20 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 66  _column_text() f
31f30 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74  ollowed by sqlit
31f40 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
31f50 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73  )</li>.**  <li>s
31f60 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
31f70 6f 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79  ob() followed by
31f80 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
31f90 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20  bytes()</li>.** 
31fa0 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c   <li>sqlite3_col
31fb0 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 6f 6c  umn_text16() fol
31fc0 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33  lowed by sqlite3
31fd0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28  _column_bytes16(
31fe0 29 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a  )</li>.** </ul>.
31ff0 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77  **.** In other w
32000 6f 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 6c 64  ords, you should
32010 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 63 6f   call sqlite3_co
32020 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a 20  lumn_text(),.** 
32030 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
32040 6c 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69 74 65  lob(), or sqlite
32050 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
32060 29 20 66 69 72 73 74 20 74 6f 20 66 6f 72 63 65  ) first to force
32070 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69   the result.** i
32080 6e 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 20  nto the desired 
32090 66 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 6e 76  format, then inv
320a0 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  oke sqlite3_colu
320b0 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a  mn_bytes() or.**
320c0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
320d0 62 79 74 65 73 31 36 28 29 20 74 6f 20 66 69 6e  bytes16() to fin
320e0 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  d the size of th
320f0 65 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 6e 6f  e result.  Do no
32100 74 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a 20 74  t mix calls.** t
32110 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  o sqlite3_column
32120 5f 74 65 78 74 28 29 20 6f 72 20 73 71 6c 69 74  _text() or sqlit
32130 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29  e3_column_blob()
32140 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 0a 2a   with calls to.*
32150 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
32160 5f 62 79 74 65 73 31 36 28 29 2c 20 61 6e 64 20  _bytes16(), and 
32170 64 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73  do not mix calls
32180 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   to sqlite3_colu
32190 6d 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 77  mn_text16().** w
321a0 69 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  ith calls to sql
321b0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
321c0 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  s()..**.** ^The 
321d0 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65  pointers returne
321e0 64 20 61 72 65 20 76 61 6c 69 64 20 75 6e 74 69  d are valid unti
321f0 6c 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73  l a type convers
32200 69 6f 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a 2a  ion occurs as.**
32210 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65   described above
32220 2c 20 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69  , or until [sqli
32230 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b  te3_step()] or [
32240 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
32250 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   or.** [sqlite3_
32260 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63  finalize()] is c
32270 61 6c 6c 65 64 2e 20 20 5e 54 68 65 20 6d 65 6d  alled.  ^The mem
32280 6f 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74  ory space used t
32290 6f 20 68 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a  o hold strings.*
322a0 2a 20 61 6e 64 20 42 4c 4f 42 73 20 69 73 20 66  * and BLOBs is f
322b0 72 65 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c  reed automatical
322c0 6c 79 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f  ly.  Do <b>not</
322d0 62 3e 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e  b> pass the poin
322e0 74 65 72 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a  ters returned.**
322f0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
32300 5f 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74  _blob()], [sqlit
32310 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
32320 5d 2c 20 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20  ], etc. into.** 
32330 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
32340 2e 0a 2a 2a 0a 2a 2a 20 5e 28 49 66 20 61 20 6d  ..**.** ^(If a m
32350 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
32360 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75   error occurs du
32370 72 69 6e 67 20 74 68 65 20 65 76 61 6c 75 61 74  ring the evaluat
32380 69 6f 6e 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 66  ion of any.** of
32390 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2c   these routines,
323a0 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65   a default value
323b0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54   is returned.  T
323c0 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
323d0 0a 2a 2a 20 69 73 20 65 69 74 68 65 72 20 74 68  .** is either th
323e0 65 20 69 6e 74 65 67 65 72 20 30 2c 20 74 68 65  e integer 0, the
323f0 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
32400 6e 75 6d 62 65 72 20 30 2e 30 2c 20 6f 72 20 61  number 0.0, or a
32410 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72   NULL.** pointer
32420 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 61  .  Subsequent ca
32430 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
32440 65 72 72 63 6f 64 65 28 29 5d 20 77 69 6c 6c 20  errcode()] will 
32450 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54  return.** [SQLIT
32460 45 5f 4e 4f 4d 45 4d 5d 2e 29 5e 0a 2a 2f 0a 53  E_NOMEM].)^.*/.S
32470 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
32480 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
32490 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65  lumn_blob(sqlite
324a0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f  3_stmt*, int iCo
324b0 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  l);.SQLITE_API i
324c0 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  nt sqlite3_colum
324d0 6e 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f  n_bytes(sqlite3_
324e0 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
324f0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
32500 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
32510 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f  bytes16(sqlite3_
32520 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
32530 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75  ;.SQLITE_API dou
32540 62 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ble sqlite3_colu
32550 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65  mn_double(sqlite
32560 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f  3_stmt*, int iCo
32570 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  l);.SQLITE_API i
32580 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  nt sqlite3_colum
32590 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74  n_int(sqlite3_st
325a0 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a  mt*, int iCol);.
325b0 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
325c0 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e3_int64 sqlite3
325d0 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71  _column_int64(sq
325e0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
325f0 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41   iCol);.SQLITE_A
32600 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  PI const unsigne
32610 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  d char *sqlite3_
32620 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69  column_text(sqli
32630 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69  te3_stmt*, int i
32640 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  Col);.SQLITE_API
32650 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
32660 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
32670 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  16(sqlite3_stmt*
32680 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c  , int iCol);.SQL
32690 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
326a0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28  te3_column_type(
326b0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
326c0 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45  nt iCol);.SQLITE
326d0 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c  _API sqlite3_val
326e0 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ue *sqlite3_colu
326f0 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33  mn_value(sqlite3
32700 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
32710 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
32720 45 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72  EF: Destroy A Pr
32730 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74  epared Statement
32740 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 5e 54   Object.**.** ^T
32750 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  he sqlite3_final
32760 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ize() function i
32770 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 65 6c 65  s called to dele
32780 74 65 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  te a [prepared s
32790 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 5e 49  tatement]..** ^I
327a0 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
327b0 74 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20  t evaluation of 
327c0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 65 6e  the statement en
327d0 63 6f 75 6e 74 65 72 65 64 20 6e 6f 20 65 72 72  countered no err
327e0 6f 72 73 0a 2a 2a 20 6f 72 20 69 66 20 74 68 65  ors.** or if the
327f0 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 65   statement is ne
32800 76 65 72 20 62 65 65 6e 20 65 76 61 6c 75 61 74  ver been evaluat
32810 65 64 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33  ed, then sqlite3
32820 5f 66 69 6e 61 6c 69 7a 65 28 29 20 72 65 74 75  _finalize() retu
32830 72 6e 73 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  rns.** SQLITE_OK
32840 2e 20 20 5e 49 66 20 74 68 65 20 6d 6f 73 74 20  .  ^If the most 
32850 72 65 63 65 6e 74 20 65 76 61 6c 75 61 74 69 6f  recent evaluatio
32860 6e 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20 53  n of statement S
32870 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 0a 2a 2a   failed, then.**
32880 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
32890 65 28 53 29 20 72 65 74 75 72 6e 73 20 74 68 65  e(S) returns the
328a0 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65 72   appropriate [er
328b0 72 6f 72 20 63 6f 64 65 5d 20 6f 72 0a 2a 2a 20  ror code] or.** 
328c0 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20  [extended error 
328d0 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68  code]..**.** ^Th
328e0 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  e sqlite3_finali
328f0 7a 65 28 53 29 20 72 6f 75 74 69 6e 65 20 63 61  ze(S) routine ca
32900 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61  n be called at a
32910 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 0a  ny point during.
32920 2a 2a 20 74 68 65 20 6c 69 66 65 20 63 79 63 6c  ** the life cycl
32930 65 20 6f 66 20 5b 70 72 65 70 61 72 65 64 20 73  e of [prepared s
32940 74 61 74 65 6d 65 6e 74 5d 20 53 3a 0a 2a 2a 20  tatement] S:.** 
32950 62 65 66 6f 72 65 20 73 74 61 74 65 6d 65 6e 74  before statement
32960 20 53 20 69 73 20 65 76 65 72 20 65 76 61 6c 75   S is ever evalu
32970 61 74 65 64 2c 20 61 66 74 65 72 0a 2a 2a 20 6f  ated, after.** o
32980 6e 65 20 6f 72 20 6d 6f 72 65 20 63 61 6c 6c 73  ne or more calls
32990 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73   to [sqlite3_res
329a0 65 74 28 29 5d 2c 20 6f 72 20 61 66 74 65 72 20  et()], or after 
329b0 61 6e 79 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 5b  any call.** to [
329c0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
329d0 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68  regardless of wh
329e0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
329f0 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 0a 2a   statement has.*
32a00 2a 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63  * completed exec
32a10 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 5e 49 6e  ution..**.** ^In
32a20 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 66  voking sqlite3_f
32a30 69 6e 61 6c 69 7a 65 28 29 20 6f 6e 20 61 20 4e  inalize() on a N
32a40 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 61  ULL pointer is a
32a50 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e   harmless no-op.
32a60 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69  .**.** The appli
32a70 63 61 74 69 6f 6e 20 6d 75 73 74 20 66 69 6e 61  cation must fina
32a80 6c 69 7a 65 20 65 76 65 72 79 20 5b 70 72 65 70  lize every [prep
32a90 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
32aa0 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69  in order to avoi
32ab0 64 0a 2a 2a 20 72 65 73 6f 75 72 63 65 20 6c 65  d.** resource le
32ac0 61 6b 73 2e 20 20 49 74 20 69 73 20 61 20 67 72  aks.  It is a gr
32ad0 69 65 76 6f 75 73 20 65 72 72 6f 72 20 66 6f 72  ievous error for
32ae0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
32af0 20 74 6f 20 74 72 79 20 74 6f 20 75 73 65 0a 2a   to try to use.*
32b00 2a 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61  * a prepared sta
32b10 74 65 6d 65 6e 74 20 61 66 74 65 72 20 69 74 20  tement after it 
32b20 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a  has been finaliz
32b30 65 64 2e 20 20 41 6e 79 20 75 73 65 20 6f 66 20  ed.  Any use of 
32b40 61 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74  a prepared.** st
32b50 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 69 74  atement after it
32b60 20 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 69   has been finali
32b70 7a 65 64 20 63 61 6e 20 72 65 73 75 6c 74 20 69  zed can result i
32b80 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 0a  n undefined and.
32b90 2a 2a 20 75 6e 64 65 73 69 72 61 62 6c 65 20 62  ** undesirable b
32ba0 65 68 61 76 69 6f 72 20 73 75 63 68 20 61 73 20  ehavior such as 
32bb0 73 65 67 66 61 75 6c 74 73 20 61 6e 64 20 68 65  segfaults and he
32bc0 61 70 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a  ap corruption..*
32bd0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
32be0 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
32bf0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
32c00 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  pStmt);../*.** C
32c10 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41  API3REF: Reset A
32c20 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d   Prepared Statem
32c30 65 6e 74 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a  ent Object.**.**
32c40 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
32c50 65 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73  et() function is
32c60 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 73 65 74   called to reset
32c70 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61   a [prepared sta
32c80 74 65 6d 65 6e 74 5d 0a 2a 2a 20 6f 62 6a 65 63  tement].** objec
32c90 74 20 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e  t back to its in
32ca0 69 74 69 61 6c 20 73 74 61 74 65 2c 20 72 65 61  itial state, rea
32cb0 64 79 20 74 6f 20 62 65 20 72 65 2d 65 78 65 63  dy to be re-exec
32cc0 75 74 65 64 2e 0a 2a 2a 20 5e 41 6e 79 20 53 51  uted..** ^Any SQ
32cd0 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61 72 69  L statement vari
32ce0 61 62 6c 65 73 20 74 68 61 74 20 68 61 64 20 76  ables that had v
32cf0 61 6c 75 65 73 20 62 6f 75 6e 64 20 74 6f 20 74  alues bound to t
32d00 68 65 6d 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65  hem using.** the
32d10 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62   [sqlite3_bind_b
32d20 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f 62 69  lob | sqlite3_bi
32d30 6e 64 5f 2a 28 29 20 41 50 49 5d 20 72 65 74 61  nd_*() API] reta
32d40 69 6e 20 74 68 65 69 72 20 76 61 6c 75 65 73 2e  in their values.
32d50 0a 2a 2a 20 55 73 65 20 5b 73 71 6c 69 74 65 33  .** Use [sqlite3
32d60 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28  _clear_bindings(
32d70 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65 20  )] to reset the 
32d80 62 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20  bindings..**.** 
32d90 5e 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65  ^The [sqlite3_re
32da0 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63  set(S)] interfac
32db0 65 20 72 65 73 65 74 73 20 74 68 65 20 5b 70 72  e resets the [pr
32dc0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
32dd0 5d 20 53 0a 2a 2a 20 62 61 63 6b 20 74 6f 20 74  ] S.** back to t
32de0 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
32df0 69 74 73 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a  its program..**.
32e00 2a 2a 20 5e 49 66 20 74 68 65 20 6d 6f 73 74 20  ** ^If the most 
32e10 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b  recent call to [
32e20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d  sqlite3_step(S)]
32e30 20 66 6f 72 20 74 68 65 0a 2a 2a 20 5b 70 72 65   for the.** [pre
32e40 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
32e50 20 53 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c   S returned [SQL
32e60 49 54 45 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 4c  ITE_ROW] or [SQL
32e70 49 54 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20 6f 72  ITE_DONE],.** or
32e80 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   if [sqlite3_ste
32e90 70 28 53 29 5d 20 68 61 73 20 6e 65 76 65 72 20  p(S)] has never 
32ea0 62 65 66 6f 72 65 20 62 65 65 6e 20 63 61 6c 6c  before been call
32eb0 65 64 20 6f 6e 20 53 2c 0a 2a 2a 20 74 68 65 6e  ed on S,.** then
32ec0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
32ed0 53 29 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  S)] returns [SQL
32ee0 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 5e  ITE_OK]..**.** ^
32ef0 49 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  If the most rece
32f00 6e 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  nt call to [sqli
32f10 74 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72  te3_step(S)] for
32f20 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65   the.** [prepare
32f30 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 69  d statement] S i
32f40 6e 64 69 63 61 74 65 64 20 61 6e 20 65 72 72 6f  ndicated an erro
32f50 72 2c 20 74 68 65 6e 0a 2a 2a 20 5b 73 71 6c 69  r, then.** [sqli
32f60 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20 72 65  te3_reset(S)] re
32f70 74 75 72 6e 73 20 61 6e 20 61 70 70 72 6f 70 72  turns an appropr
32f80 69 61 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65  iate [error code
32f90 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 5b 73  ]..**.** ^The [s
32fa0 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d  qlite3_reset(S)]
32fb0 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65 73 20   interface does 
32fc0 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 76  not change the v
32fd0 61 6c 75 65 73 0a 2a 2a 20 6f 66 20 61 6e 79 20  alues.** of any 
32fe0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c  [sqlite3_bind_bl
32ff0 6f 62 7c 62 69 6e 64 69 6e 67 73 5d 20 6f 6e 20  ob|bindings] on 
33000 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  the [prepared st
33010 61 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53  atement] S..*/.S
33020 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
33030 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69  lite3_reset(sqli
33040 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
33050 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
33060 46 3a 20 43 72 65 61 74 65 20 4f 72 20 52 65 64  F: Create Or Red
33070 65 66 69 6e 65 20 53 51 4c 20 46 75 6e 63 74 69  efine SQL Functi
33080 6f 6e 73 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  ons.** KEYWORDS:
33090 20 7b 66 75 6e 63 74 69 6f 6e 20 63 72 65 61 74   {function creat
330a0 69 6f 6e 20 72 6f 75 74 69 6e 65 73 7d 0a 2a 2a  ion routines}.**
330b0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c   KEYWORDS: {appl
330c0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
330d0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 7d 0a 2a 2a  SQL function}.**
330e0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c   KEYWORDS: {appl
330f0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
33100 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a  SQL functions}.*
33110 2a 0a 2a 2a 20 5e 54 68 65 73 65 20 66 75 6e 63  *.** ^These func
33120 74 69 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76  tions (collectiv
33130 65 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75  ely known as "fu
33140 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20  nction creation 
33150 72 6f 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72  routines").** ar
33160 65 20 75 73 65 64 20 74 6f 20 61 64 64 20 53 51  e used to add SQ
33170 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61  L functions or a
33180 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 6f 20  ggregates or to 
33190 72 65 64 65 66 69 6e 65 20 74 68 65 20 62 65 68  redefine the beh
331a0 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73  avior.** of exis
331b0 74 69 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 6f  ting SQL functio
331c0 6e 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 73  ns or aggregates
331d0 2e 20 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66  .  The only diff
331e0 65 72 65 6e 63 65 73 20 62 65 74 77 65 65 6e 0a  erences between.
331f0 2a 2a 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  ** these routine
33200 73 20 61 72 65 20 74 68 65 20 74 65 78 74 20 65  s are the text e
33210 6e 63 6f 64 69 6e 67 20 65 78 70 65 63 74 65 64  ncoding expected
33220 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 65 63 6f   for.** the seco
33230 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28 74 68  nd parameter (th
33240 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75  e name of the fu
33250 6e 63 74 69 6f 6e 20 62 65 69 6e 67 20 63 72 65  nction being cre
33260 61 74 65 64 29 0a 2a 2a 20 61 6e 64 20 74 68 65  ated).** and the
33270 20 70 72 65 73 65 6e 63 65 20 6f 72 20 61 62 73   presence or abs
33280 65 6e 63 65 20 6f 66 20 61 20 64 65 73 74 72 75  ence of a destru
33290 63 74 6f 72 20 63 61 6c 6c 62 61 63 6b 20 66 6f  ctor callback fo
332a0 72 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61  r.** the applica
332b0 74 69 6f 6e 20 64 61 74 61 20 70 6f 69 6e 74 65  tion data pointe
332c0 72 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69  r..**.** ^The fi
332d0 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  rst parameter is
332e0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
332f0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f 20 77 68  onnection] to wh
33300 69 63 68 20 74 68 65 20 53 51 4c 0a 2a 2a 20 66  ich the SQL.** f
33310 75 6e 63 74 69 6f 6e 20 69 73 20 74 6f 20 62 65  unction is to be
33320 20 61 64 64 65 64 2e 20 20 5e 49 66 20 61 6e 20   added.  ^If an 
33330 61 70 70 6c 69 63 61 74 69 6f 6e 20 75 73 65 73  application uses
33340 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64   more than one d
33350 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65  atabase.** conne
33360 63 74 69 6f 6e 20 74 68 65 6e 20 61 70 70 6c 69  ction then appli
33370 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53  cation-defined S
33380 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73  QL functions mus
33390 74 20 62 65 20 61 64 64 65 64 0a 2a 2a 20 74 6f  t be added.** to
333a0 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63   each database c
333b0 6f 6e 6e 65 63 74 69 6f 6e 20 73 65 70 61 72 61  onnection separa
333c0 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65  tely..**.** ^The
333d0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
333e0 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  r is the name of
333f0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
33400 6e 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20  n to be created 
33410 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e  or.** redefined.
33420 20 20 5e 54 68 65 20 6c 65 6e 67 74 68 20 6f 66    ^The length of
33430 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d   the name is lim
33440 69 74 65 64 20 74 6f 20 32 35 35 20 62 79 74 65  ited to 255 byte
33450 73 20 69 6e 20 61 20 55 54 46 2d 38 0a 2a 2a 20  s in a UTF-8.** 
33460 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20  representation, 
33470 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65  exclusive of the
33480 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72   zero-terminator
33490 2e 20 20 5e 4e 6f 74 65 20 74 68 61 74 20 74 68  .  ^Note that th
334a0 65 20 6e 61 6d 65 0a 2a 2a 20 6c 65 6e 67 74 68  e name.** length
334b0 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 55 54 46   limit is in UTF
334c0 2d 38 20 62 79 74 65 73 2c 20 6e 6f 74 20 63 68  -8 bytes, not ch
334d0 61 72 61 63 74 65 72 73 20 6e 6f 72 20 55 54 46  aracters nor UTF
334e0 2d 31 36 20 62 79 74 65 73 2e 20 20 0a 2a 2a 20  -16 bytes.  .** 
334f0 5e 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20  ^Any attempt to 
33500 63 72 65 61 74 65 20 61 20 66 75 6e 63 74 69 6f  create a functio
33510 6e 20 77 69 74 68 20 61 20 6c 6f 6e 67 65 72 20  n with a longer 
33520 6e 61 6d 65 0a 2a 2a 20 77 69 6c 6c 20 72 65 73  name.** will res
33530 75 6c 74 20 69 6e 20 5b 53 51 4c 49 54 45 5f 4d  ult in [SQLITE_M
33540 49 53 55 53 45 5d 20 62 65 69 6e 67 20 72 65 74  ISUSE] being ret
33550 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68  urned..**.** ^Th
33560 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65  e third paramete
33570 72 20 28 6e 41 72 67 29 0a 2a 2a 20 69 73 20 74  r (nArg).** is t
33580 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  he number of arg
33590 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 20  uments that the 
335a0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a  SQL function or.
335b0 2a 2a 20 61 67 67 72 65 67 61 74 65 20 74 61 6b  ** aggregate tak
335c0 65 73 2e 20 5e 49 66 20 74 68 69 73 20 70 61 72  es. ^If this par
335d0 61 6d 65 74 65 72 20 69 73 20 2d 31 2c 20 74 68  ameter is -1, th
335e0 65 6e 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  en the SQL funct
335f0 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67  ion or.** aggreg
33600 61 74 65 20 6d 61 79 20 74 61 6b 65 20 61 6e 79  ate may take any
33610 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
33620 65 6e 74 73 20 62 65 74 77 65 65 6e 20 30 20 61  ents between 0 a
33630 6e 64 20 74 68 65 20 6c 69 6d 69 74 0a 2a 2a 20  nd the limit.** 
33640 73 65 74 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  set by [sqlite3_
33650 6c 69 6d 69 74 5d 28 5b 53 51 4c 49 54 45 5f 4c  limit]([SQLITE_L
33660 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52  IMIT_FUNCTION_AR
33670 47 5d 29 2e 20 20 49 66 20 74 68 65 20 74 68 69  G]).  If the thi
33680 72 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  rd.** parameter 
33690 69 73 20 6c 65 73 73 20 74 68 61 6e 20 2d 31 20  is less than -1 
336a0 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  or greater than 
336b0 31 32 37 20 74 68 65 6e 20 74 68 65 20 62 65 68  127 then the beh
336c0 61 76 69 6f 72 20 69 73 0a 2a 2a 20 75 6e 64 65  avior is.** unde
336d0 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68  fined..**.** ^Th
336e0 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74  e fourth paramet
336f0 65 72 2c 20 65 54 65 78 74 52 65 70 2c 20 73 70  er, eTextRep, sp
33700 65 63 69 66 69 65 73 20 77 68 61 74 0a 2a 2a 20  ecifies what.** 
33710 5b 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 20 74  [SQLITE_UTF8 | t
33720 65 78 74 20 65 6e 63 6f 64 69 6e 67 5d 20 74 68  ext encoding] th
33730 69 73 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  is SQL function 
33740 70 72 65 66 65 72 73 20 66 6f 72 0a 2a 2a 20 69  prefers for.** i
33750 74 73 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20  ts parameters.  
33760 45 76 65 72 79 20 53 51 4c 20 66 75 6e 63 74 69  Every SQL functi
33770 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  on implementatio
33780 6e 20 6d 75 73 74 20 62 65 20 61 62 6c 65 20 74  n must be able t
33790 6f 20 77 6f 72 6b 0a 2a 2a 20 77 69 74 68 20 55  o work.** with U
337a0 54 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c 20  TF-8, UTF-16le, 
337b0 6f 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42 75  or UTF-16be.  Bu
337c0 74 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74  t some implement
337d0 61 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a 2a  ations may be.**
337e0 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20   more efficient 
337f0 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69 6e  with one encodin
33800 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e 20  g than another. 
33810 20 5e 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e   ^An application
33820 20 6d 61 79 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73   may.** invoke s
33830 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
33840 6e 63 74 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69  nction() or sqli
33850 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
33860 69 6f 6e 31 36 28 29 20 6d 75 6c 74 69 70 6c 65  ion16() multiple
33870 0a 2a 2a 20 74 69 6d 65 73 20 77 69 74 68 20 74  .** times with t
33880 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e  he same function
33890 20 62 75 74 20 77 69 74 68 20 64 69 66 66 65 72   but with differ
338a0 65 6e 74 20 76 61 6c 75 65 73 20 6f 66 20 65 54  ent values of eT
338b0 65 78 74 52 65 70 2e 0a 2a 2a 20 5e 57 68 65 6e  extRep..** ^When
338c0 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d   multiple implem
338d0 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  entations of the
338e0 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61   same function a
338f0 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 53 51  re available, SQ
33900 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 70 69 63  Lite.** will pic
33910 6b 20 74 68 65 20 6f 6e 65 20 74 68 61 74 20 69  k the one that i
33920 6e 76 6f 6c 76 65 73 20 74 68 65 20 6c 65 61 73  nvolves the leas
33930 74 20 61 6d 6f 75 6e 74 20 6f 66 20 64 61 74 61  t amount of data
33940 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20   conversion..** 
33950 49 66 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79  If there is only
33960 20 61 20 73 69 6e 67 6c 65 20 69 6d 70 6c 65 6d   a single implem
33970 65 6e 74 61 74 69 6f 6e 20 77 68 69 63 68 20 64  entation which d
33980 6f 65 73 20 6e 6f 74 20 63 61 72 65 20 77 68 61  oes not care wha
33990 74 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69  t text.** encodi
339a0 6e 67 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e  ng is used, then
339b0 20 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75   the fourth argu
339c0 6d 65 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 5b  ment should be [
339d0 53 51 4c 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a  SQLITE_ANY]..**.
339e0 2a 2a 20 5e 28 54 68 65 20 66 69 66 74 68 20 70  ** ^(The fifth p
339f0 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 61  arameter is an a
33a00 72 62 69 74 72 61 72 79 20 70 6f 69 6e 74 65 72  rbitrary pointer
33a10 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  .  The implement
33a20 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20  ation of the.** 
33a30 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 67 61 69  function can gai
33a40 6e 20 61 63 63 65 73 73 20 74 6f 20 74 68 69 73  n access to this
33a50 20 70 6f 69 6e 74 65 72 20 75 73 69 6e 67 20 5b   pointer using [
33a60 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
33a70 61 28 29 5d 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 54  a()].)^.**.** ^T
33a80 68 65 20 73 69 78 74 68 2c 20 73 65 76 65 6e 74  he sixth, sevent
33a90 68 20 61 6e 64 20 65 69 67 68 74 68 20 70 61 72  h and eighth par
33aa0 61 6d 65 74 65 72 73 2c 20 78 46 75 6e 63 2c 20  ameters, xFunc, 
33ab0 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 6c  xStep and xFinal
33ac0 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e 74 65 72  , are.** pointer
33ad0 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61 67 65 20  s to C-language 
33ae0 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
33af0 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c  mplement the SQL
33b00 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20   function or.** 
33b10 61 67 67 72 65 67 61 74 65 2e 20 5e 41 20 73 63  aggregate. ^A sc
33b20 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f  alar SQL functio
33b30 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 6d  n requires an im
33b40 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
33b50 74 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63 61 6c  the xFunc.** cal
33b60 6c 62 61 63 6b 20 6f 6e 6c 79 3b 20 4e 55 4c 4c  lback only; NULL
33b70 20 70 6f 69 6e 74 65 72 73 20 6d 75 73 74 20 62   pointers must b
33b80 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
33b90 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 6c  xStep and xFinal
33ba0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 2e 20  .** parameters. 
33bb0 5e 41 6e 20 61 67 67 72 65 67 61 74 65 20 53 51  ^An aggregate SQ
33bc0 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69  L function requi
33bd0 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74  res an implement
33be0 61 74 69 6f 6e 20 6f 66 20 78 53 74 65 70 0a 2a  ation of xStep.*
33bf0 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 61 6e 64  * and xFinal and
33c00 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 6d 75   NULL pointer mu
33c10 73 74 20 62 65 20 70 61 73 73 65 64 20 66 6f 72  st be passed for
33c20 20 78 46 75 6e 63 2e 20 5e 54 6f 20 64 65 6c 65   xFunc. ^To dele
33c30 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a  te an existing.*
33c40 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f  * SQL function o
33c50 72 20 61 67 67 72 65 67 61 74 65 2c 20 70 61 73  r aggregate, pas
33c60 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20  s NULL pointers 
33c70 66 6f 72 20 61 6c 6c 20 74 68 72 65 65 20 66 75  for all three fu
33c80 6e 63 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c 62 61  nction.** callba
33c90 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 5e 28 49 66 20  cks..**.** ^(If 
33ca0 74 68 65 20 6e 69 6e 74 68 20 70 61 72 61 6d 65  the ninth parame
33cb0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 63  ter to sqlite3_c
33cc0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 5f 76  reate_function_v
33cd0 32 28 29 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c  2() is not NULL,
33ce0 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 64  .** then it is d
33cf0 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68  estructor for th
33d00 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 61  e application da
33d10 74 61 20 70 6f 69 6e 74 65 72 2e 20 0a 2a 2a 20  ta pointer. .** 
33d20 54 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 69  The destructor i
33d30 73 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 74  s invoked when t
33d40 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64  he function is d
33d50 65 6c 65 74 65 64 2c 20 65 69 74 68 65 72 20 62  eleted, either b
33d60 79 20 62 65 69 6e 67 0a 2a 2a 20 6f 76 65 72 6c  y being.** overl
33d70 6f 61 64 65 64 20 6f 72 20 77 68 65 6e 20 74 68  oaded or when th
33d80 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
33d90 63 74 69 6f 6e 20 63 6c 6f 73 65 73 2e 29 5e 0a  ction closes.)^.
33da0 2a 2a 20 5e 54 68 65 20 64 65 73 74 72 75 63 74  ** ^The destruct
33db0 6f 72 20 69 73 20 61 6c 73 6f 20 69 6e 76 6f 6b  or is also invok
33dc0 65 64 20 69 66 20 74 68 65 20 63 61 6c 6c 20 74  ed if the call t
33dd0 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 72 65  o.** sqlite3_cre
33de0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 5f 76 32 28  ate_function_v2(
33df0 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 5e 57 68 65  ) fails..** ^Whe
33e00 6e 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  n the destructor
33e10 20 63 61 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65   callback of the
33e20 20 74 65 6e 74 68 20 70 61 72 61 6d 65 74 65 72   tenth parameter
33e30 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 69 74 0a   is invoked, it.
33e40 2a 2a 20 69 73 20 70 61 73 73 65 64 20 61 20 73  ** is passed a s
33e50 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77  ingle argument w
33e60 68 69 63 68 20 69 73 20 61 20 63 6f 70 79 20 6f  hich is a copy o
33e70 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  f the applicatio
33e80 6e 20 64 61 74 61 20 0a 2a 2a 20 70 6f 69 6e 74  n data .** point
33e90 65 72 20 77 68 69 63 68 20 77 61 73 20 74 68 65  er which was the
33ea0 20 66 69 66 74 68 20 70 61 72 61 6d 65 74 65 72   fifth parameter
33eb0 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 72 65 61   to sqlite3_crea
33ec0 74 65 5f 66 75 6e 63 74 69 6f 6e 5f 76 32 28 29  te_function_v2()
33ed0 2e 0a 2a 2a 0a 2a 2a 20 5e 49 74 20 69 73 20 70  ..**.** ^It is p
33ee0 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 67 69  ermitted to regi
33ef0 73 74 65 72 20 6d 75 6c 74 69 70 6c 65 20 69 6d  ster multiple im
33f00 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
33f10 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66 75 6e   the same.** fun
33f20 63 74 69 6f 6e 73 20 77 69 74 68 20 74 68 65 20  ctions with the 
33f30 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 69  same name but wi
33f40 74 68 20 65 69 74 68 65 72 20 64 69 66 66 65 72  th either differ
33f50 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 0a 2a  ing numbers of.*
33f60 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 64  * arguments or d
33f70 69 66 66 65 72 69 6e 67 20 70 72 65 66 65 72 72  iffering preferr
33f80 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  ed text encoding
33f90 73 2e 20 20 5e 53 51 4c 69 74 65 20 77 69 6c 6c  s.  ^SQLite will
33fa0 20 75 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70 6c   use.** the impl
33fb0 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20  ementation that 
33fc0 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74  most closely mat
33fd0 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e 20  ches the way in 
33fe0 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c  which the.** SQL
33ff0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
34000 64 2e 20 20 5e 41 20 66 75 6e 63 74 69 6f 6e 20  d.  ^A function 
34010 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77  implementation w
34020 69 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69  ith a non-negati
34030 76 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d  ve.** nArg param
34040 65 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72  eter is a better
34050 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75   match than a fu
34060 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
34070 61 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20  ation with.** a 
34080 6e 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20  negative nArg.  
34090 5e 41 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72  ^A function wher
340a0 65 20 74 68 65 20 70 72 65 66 65 72 72 65 64 20  e the preferred 
340b0 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a  text encoding.**
340c0 20 6d 61 74 63 68 65 73 20 74 68 65 20 64 61 74   matches the dat
340d0 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69  abase encoding i
340e0 73 20 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61  s a better.** ma
340f0 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74  tch than a funct
34100 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e  ion where the en
34110 63 6f 64 69 6e 67 20 69 73 20 64 69 66 66 65 72  coding is differ
34120 65 6e 74 2e 20 20 0a 2a 2a 20 5e 41 20 66 75 6e  ent.  .** ^A fun
34130 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20  ction where the 
34140 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65  encoding differe
34150 6e 63 65 20 69 73 20 62 65 74 77 65 65 6e 20 55  nce is between U
34160 54 46 31 36 6c 65 20 61 6e 64 20 55 54 46 31 36  TF16le and UTF16
34170 62 65 0a 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65  be.** is a close
34180 72 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66  r match than a f
34190 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68  unction where th
341a0 65 20 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65  e encoding diffe
341b0 72 65 6e 63 65 20 69 73 0a 2a 2a 20 62 65 74 77  rence is.** betw
341c0 65 65 6e 20 55 54 46 38 20 61 6e 64 20 55 54 46  een UTF8 and UTF
341d0 31 36 2e 0a 2a 2a 0a 2a 2a 20 5e 42 75 69 6c 74  16..**.** ^Built
341e0 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61  -in functions ma
341f0 79 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 20  y be overloaded 
34200 62 79 20 6e 65 77 20 61 70 70 6c 69 63 61 74 69  by new applicati
34210 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
34220 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 5e 41 6e 20  ions..**.** ^An 
34230 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
34240 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ned function is 
34250 70 65 72 6d 69 74 74 65 64 20 74 6f 20 63 61 6c  permitted to cal
34260 6c 20 6f 74 68 65 72 0a 2a 2a 20 53 51 4c 69 74  l other.** SQLit
34270 65 20 69 6e 74 65 72 66 61 63 65 73 2e 20 20 48  e interfaces.  H
34280 6f 77 65 76 65 72 2c 20 73 75 63 68 20 63 61 6c  owever, such cal
34290 6c 73 20 6d 75 73 74 20 6e 6f 74 0a 2a 2a 20 63  ls must not.** c
342a0 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62 61 73  lose the databas
342b0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6e 6f 72  e connection nor
342c0 20 66 69 6e 61 6c 69 7a 65 20 6f 72 20 72 65 73   finalize or res
342d0 65 74 20 74 68 65 20 70 72 65 70 61 72 65 64 0a  et the prepared.
342e0 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  ** statement in 
342f0 77 68 69 63 68 20 74 68 65 20 66 75 6e 63 74 69  which the functi
34300 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a  on is running..*
34310 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
34320 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
34330 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69  function(.  sqli
34340 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74  te3 *db,.  const
34350 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e   char *zFunction
34360 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67  Name,.  int nArg
34370 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70  ,.  int eTextRep
34380 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a  ,.  void *pApp,.
34390 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28    void (*xFunc)(
343a0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
343b0 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
343c0 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  ue**),.  void (*
343d0 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63  xStep)(sqlite3_c
343e0 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
343f0 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20  te3_value**),.  
34400 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73  void (*xFinal)(s
34410 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
34420 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .);.SQLITE_API i
34430 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
34440 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20  e_function16(.  
34450 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63  sqlite3 *db,.  c
34460 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63  onst void *zFunc
34470 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  tionName,.  int 
34480 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78  nArg,.  int eTex
34490 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41  tRep,.  void *pA
344a0 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75  pp,.  void (*xFu
344b0 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
344c0 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
344d0 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69  _value**),.  voi
344e0 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74  d (*xStep)(sqlit
344f0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
34500 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
34510 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61  ,.  void (*xFina
34520 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  l)(sqlite3_conte
34530 78 74 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  xt*).);.SQLITE_A
34540 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
34550 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 5f 76  reate_function_v
34560 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  2(.  sqlite3 *db
34570 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
34580 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20  zFunctionName,. 
34590 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74   int nArg,.  int
345a0 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69   eTextRep,.  voi
345b0 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20  d *pApp,.  void 
345c0 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33  (*xFunc)(sqlite3
345d0 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
345e0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a  lite3_value**),.
345f0 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28    void (*xStep)(
34600 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
34610 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
34620 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  ue**),.  void (*
34630 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f  xFinal)(sqlite3_
34640 63 6f 6e 74 65 78 74 2a 29 2c 0a 20 20 76 6f 69  context*),.  voi
34650 64 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69  d(*xDestroy)(voi
34660 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  d*).);../*.** CA
34670 50 49 33 52 45 46 3a 20 54 65 78 74 20 45 6e 63  PI3REF: Text Enc
34680 6f 64 69 6e 67 73 0a 2a 2a 0a 2a 2a 20 54 68 65  odings.**.** The
34690 73 65 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69  se constant defi
346a0 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73  ne integer codes
346b0 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 20   that represent 
346c0 74 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74  the various.** t
346d0 65 78 74 20 65 6e 63 6f 64 69 6e 67 73 20 73 75  ext encodings su
346e0 70 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74  pported by SQLit
346f0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  e..*/.#define SQ
34700 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20  LITE_UTF8       
34710 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51      1.#define SQ
34720 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20  LITE_UTF16LE    
34730 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51      2.#define SQ
34740 4c 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20  LITE_UTF16BE    
34750 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51      3.#define SQ
34760 4c 49 54 45 5f 55 54 46 31 36 20 20 20 20 20 20  LITE_UTF16      
34770 20 20 20 20 34 20 20 20 20 2f 2a 20 55 73 65 20      4    /* Use 
34780 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65  native byte orde
34790 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  r */.#define SQL
347a0 49 54 45 5f 41 4e 59 20 20 20 20 20 20 20 20 20  ITE_ANY         
347b0 20 20 20 35 20 20 20 20 2f 2a 20 73 71 6c 69 74     5    /* sqlit
347c0 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
347d0 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  on only */.#defi
347e0 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f  ne SQLITE_UTF16_
347f0 41 4c 49 47 4e 45 44 20 20 38 20 20 20 20 2f 2a  ALIGNED  8    /*
34800 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
34810 63 6f 6c 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a  collation only *
34820 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  /../*.** CAPI3RE
34830 46 3a 20 44 65 70 72 65 63 61 74 65 64 20 46 75  F: Deprecated Fu
34840 6e 63 74 69 6f 6e 73 0a 2a 2a 20 44 45 50 52 45  nctions.** DEPRE
34850 43 41 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73  CATED.**.** Thes
34860 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  e functions are 
34870 5b 64 65 70 72 65 63 61 74 65 64 5d 2e 20 20 49  [deprecated].  I
34880 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74  n order to maint
34890 61 69 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73  ain.** backwards
348a0 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77   compatibility w
348b0 69 74 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20  ith older code, 
348c0 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  these functions 
348d0 63 6f 6e 74 69 6e 75 65 20 0a 2a 2a 20 74 6f 20  continue .** to 
348e0 62 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 48  be supported.  H
348f0 6f 77 65 76 65 72 2c 20 6e 65 77 20 61 70 70 6c  owever, new appl
34900 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20  ications should 
34910 61 76 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73 65  avoid.** the use
34920 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69   of these functi
34930 6f 6e 73 2e 20 20 54 6f 20 68 65 6c 70 20 65 6e  ons.  To help en
34940 63 6f 75 72 61 67 65 20 70 65 6f 70 6c 65 20 74  courage people t
34950 6f 20 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e 67  o avoid.** using
34960 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   these functions
34970 2c 20 77 65 20 61 72 65 20 6e 6f 74 20 67 6f 69  , we are not goi
34980 6e 67 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20 77  ng to tell you w
34990 68 61 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a  hat they do..*/.
349a0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
349b0 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 53  MIT_DEPRECATED.S
349c0 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45  QLITE_API SQLITE
349d0 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20  _DEPRECATED int 
349e0 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
349f0 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f  e_count(sqlite3_
34a00 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54  context*);.SQLIT
34a10 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50  E_API SQLITE_DEP
34a20 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69  RECATED int sqli
34a30 74 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69  te3_expired(sqli
34a40 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49  te3_stmt*);.SQLI
34a50 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45  TE_API SQLITE_DE
34a60 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c  PRECATED int sql
34a70 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69  ite3_transfer_bi
34a80 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73  ndings(sqlite3_s
34a90 74 6d 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74  tmt*, sqlite3_st
34aa0 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  mt*);.SQLITE_API
34ab0 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54   SQLITE_DEPRECAT
34ac0 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67  ED int sqlite3_g
34ad0 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f  lobal_recover(vo
34ae0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  id);.SQLITE_API 
34af0 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45  SQLITE_DEPRECATE
34b00 44 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74  D void sqlite3_t
34b10 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f  hread_cleanup(vo
34b20 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  id);.SQLITE_API 
34b30 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45  SQLITE_DEPRECATE
34b40 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65  D int sqlite3_me
34b50 6d 6f 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28  mory_alarm(void(
34b60 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  *)(void*,sqlite3
34b70 5f 69 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64  _int64,int),void
34b80 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29  *,sqlite3_int64)
34b90 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
34ba0 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e  CAPI3REF: Obtain
34bb0 69 6e 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e  ing SQL Function
34bc0 20 50 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65   Parameter Value
34bd0 73 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d 6c 61  s.**.** The C-la
34be0 6e 67 75 61 67 65 20 69 6d 70 6c 65 6d 65 6e 74  nguage implement
34bf0 61 74 69 6f 6e 20 6f 66 20 53 51 4c 20 66 75 6e  ation of SQL fun
34c00 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 65  ctions and aggre
34c10 67 61 74 65 73 20 75 73 65 73 0a 2a 2a 20 74 68  gates uses.** th
34c20 69 73 20 73 65 74 20 6f 66 20 69 6e 74 65 72 66  is set of interf
34c30 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 20  ace routines to 
34c40 61 63 63 65 73 73 20 74 68 65 20 70 61 72 61 6d  access the param
34c50 65 74 65 72 20 76 61 6c 75 65 73 20 6f 6e 0a 2a  eter values on.*
34c60 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 6f  * the function o
34c70 72 20 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a  r aggregate..**.
34c80 2a 2a 20 54 68 65 20 78 46 75 6e 63 20 28 66 6f  ** The xFunc (fo
34c90 72 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  r scalar functio
34ca0 6e 73 29 20 6f 72 20 78 53 74 65 70 20 28 66 6f  ns) or xStep (fo
34cb0 72 20 61 67 67 72 65 67 61 74 65 73 29 20 70 61  r aggregates) pa
34cc0 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 5b  rameters.** to [
34cd0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
34ce0 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b  unction()] and [
34cf0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
34d00 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20  unction16()].** 
34d10 64 65 66 69 6e 65 20 63 61 6c 6c 62 61 63 6b 73  define callbacks
34d20 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
34d30 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
34d40 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 65 73  s and aggregates
34d50 2e 0a 2a 2a 20 54 68 65 20 33 72 64 20 70 61 72  ..** The 3rd par
34d60 61 6d 65 74 65 72 20 74 6f 20 74 68 65 73 65 20  ameter to these 
34d70 63 61 6c 6c 62 61 63 6b 73 20 69 73 20 61 6e 20  callbacks is an 
34d80 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72  array of pointer
34d90 73 20 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65 63 74  s to.** [protect
34da0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
34db0 5d 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 72  ] objects.  Ther
34dc0 65 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69 74 65  e is one [sqlite
34dd0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20  3_value] object 
34de0 66 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61 72 61  for.** each para
34df0 6d 65 74 65 72 20 74 6f 20 74 68 65 20 53 51 4c  meter to the SQL
34e00 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73   function.  Thes
34e10 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75  e routines are u
34e20 73 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72 61 63  sed to.** extrac
34e30 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68  t values from th
34e40 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e [sqlite3_value
34e50 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a  ] objects..**.**
34e60 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
34e70 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68 20 5b  work only with [
34e80 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
34e90 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73  3_value] objects
34ea0 2e 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70 74  ..** Any attempt
34eb0 20 74 6f 20 75 73 65 20 74 68 65 73 65 20 72 6f   to use these ro
34ec0 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b 75 6e  utines on an [un
34ed0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
34ee0 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62 6a 65  3_value].** obje
34ef0 63 74 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e  ct results in un
34f00 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72  defined behavior
34f10 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 73 65 20 72  ..**.** ^These r
34f20 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6a 75 73  outines work jus
34f30 74 20 6c 69 6b 65 20 74 68 65 20 63 6f 72 72 65  t like the corre
34f40 73 70 6f 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d 6e  sponding [column
34f50 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e   access function
34f60 73 5d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61  s].** except tha
34f70 74 20 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  t  these routine
34f80 73 20 74 61 6b 65 20 61 20 73 69 6e 67 6c 65 20  s take a single 
34f90 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  [protected sqlit
34fa0 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
34fb0 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 73 74  .** pointer inst
34fc0 65 61 64 20 6f 66 20 61 20 5b 73 71 6c 69 74 65  ead of a [sqlite
34fd0 33 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e 74 65 72  3_stmt*] pointer
34fe0 20 61 6e 64 20 61 6e 20 69 6e 74 65 67 65 72 20   and an integer 
34ff0 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a  column number..*
35000 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65  *.** ^The sqlite
35010 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29  3_value_text16()
35020 20 69 6e 74 65 72 66 61 63 65 20 65 78 74 72 61   interface extra
35030 63 74 73 20 61 20 55 54 46 2d 31 36 20 73 74 72  cts a UTF-16 str
35040 69 6e 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61  ing.** in the na
35050 74 69 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20  tive byte-order 
35060 6f 66 20 74 68 65 20 68 6f 73 74 20 6d 61 63 68  of the host mach
35070 69 6e 65 2e 20 20 5e 54 68 65 0a 2a 2a 20 73 71  ine.  ^The.** sq
35080 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
35090 31 36 62 65 28 29 20 61 6e 64 20 73 71 6c 69 74  16be() and sqlit
350a0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c  e3_value_text16l
350b0 65 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a  e() interfaces.*
350c0 2a 20 65 78 74 72 61 63 74 20 55 54 46 2d 31 36  * extract UTF-16
350d0 20 73 74 72 69 6e 67 73 20 61 73 20 62 69 67 2d   strings as big-
350e0 65 6e 64 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c  endian and littl
350f0 65 2d 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74  e-endian respect
35100 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54  ively..**.** ^(T
35110 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  he sqlite3_value
35120 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 29 20  _numeric_type() 
35130 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70  interface attemp
35140 74 73 20 74 6f 20 61 70 70 6c 79 0a 2a 2a 20 6e  ts to apply.** n
35150 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74 79 20  umeric affinity 
35160 74 6f 20 74 68 65 20 76 61 6c 75 65 2e 20 20 54  to the value.  T
35170 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 61  his means that a
35180 6e 20 61 74 74 65 6d 70 74 20 69 73 0a 2a 2a 20  n attempt is.** 
35190 6d 61 64 65 20 74 6f 20 63 6f 6e 76 65 72 74 20  made to convert 
351a0 74 68 65 20 76 61 6c 75 65 20 74 6f 20 61 6e 20  the value to an 
351b0 69 6e 74 65 67 65 72 20 6f 72 20 66 6c 6f 61 74  integer or float
351c0 69 6e 67 20 70 6f 69 6e 74 2e 20 20 49 66 0a 2a  ing point.  If.*
351d0 2a 20 73 75 63 68 20 61 20 63 6f 6e 76 65 72 73  * such a convers
351e0 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 20  ion is possible 
351f0 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20  without loss of 
35200 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 69 6e 20  information (in 
35210 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20  other.** words, 
35220 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  if the value is 
35230 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 6c 6f  a string that lo
35240 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65  oks like a numbe
35250 72 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63  r).** then the c
35260 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 65 72  onversion is per
35270 66 6f 72 6d 65 64 2e 20 20 4f 74 68 65 72 77 69  formed.  Otherwi
35280 73 65 20 6e 6f 20 63 6f 6e 76 65 72 73 69 6f 6e  se no conversion
35290 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 54 68 65 20   occurs..** The 
352a0 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20  [SQLITE_INTEGER 
352b0 7c 20 64 61 74 61 74 79 70 65 5d 20 61 66 74 65  | datatype] afte
352c0 72 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  r conversion is 
352d0 72 65 74 75 72 6e 65 64 2e 29 5e 0a 2a 2a 0a 2a  returned.)^.**.*
352e0 2a 20 50 6c 65 61 73 65 20 70 61 79 20 70 61 72  * Please pay par
352f0 74 69 63 75 6c 61 72 20 61 74 74 65 6e 74 69 6f  ticular attentio
35300 6e 20 74 6f 20 74 68 65 20 66 61 63 74 20 74 68  n to the fact th
35310 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 72  at the pointer r
35320 65 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20  eturned.** from 
35330 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  [sqlite3_value_b
35340 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  lob()], [sqlite3
35350 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 20  _value_text()], 
35360 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76  or.** [sqlite3_v
35370 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 20 63  alue_text16()] c
35380 61 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65  an be invalidate
35390 64 20 62 79 20 61 20 73 75 62 73 65 71 75 65 6e  d by a subsequen
353a0 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71  t call to.** [sq
353b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
353c0 73 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76  s()], [sqlite3_v
353d0 61 6c 75 65 5f 62 79 74 65 73 31 36 28 29 5d 2c  alue_bytes16()],
353e0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
353f0 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b  text()],.** or [
35400 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
35410 78 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54  xt16()]..**.** T
35420 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 75  hese routines mu
35430 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f  st be called fro
35440 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  m the same threa
35450 64 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 4c 20  d as.** the SQL 
35460 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73 75  function that su
35470 70 70 6c 69 65 64 20 74 68 65 20 5b 73 71 6c 69  pplied the [sqli
35480 74 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61 72 61  te3_value*] para
35490 6d 65 74 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54  meters..*/.SQLIT
354a0 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
354b0 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   *sqlite3_value_
354c0 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61 6c  blob(sqlite3_val
354d0 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
354e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
354f0 75 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33  ue_bytes(sqlite3
35500 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45  _value*);.SQLITE
35510 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
35520 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 73  _value_bytes16(s
35530 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
35540 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c  SQLITE_API doubl
35550 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
35560 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 76  double(sqlite3_v
35570 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  alue*);.SQLITE_A
35580 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76  PI int sqlite3_v
35590 61 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 33  alue_int(sqlite3
355a0 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45  _value*);.SQLITE
355b0 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74  _API sqlite3_int
355c0 36 34 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  64 sqlite3_value
355d0 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76  _int64(sqlite3_v
355e0 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  alue*);.SQLITE_A
355f0 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  PI const unsigne
35600 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  d char *sqlite3_
35610 76 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74  value_text(sqlit
35620 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49  e3_value*);.SQLI
35630 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
35640 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  d *sqlite3_value
35650 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f  _text16(sqlite3_
35660 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
35670 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
35680 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
35690 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 76  xt16le(sqlite3_v
356a0 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  alue*);.SQLITE_A
356b0 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
356c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
356d0 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f 76 61  t16be(sqlite3_va
356e0 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
356f0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  I int sqlite3_va
35700 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65 33  lue_type(sqlite3
35710 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45  _value*);.SQLITE
35720 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
35730 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74  _value_numeric_t
35740 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ype(sqlite3_valu
35750 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  e*);../*.** CAPI
35760 33 52 45 46 3a 20 4f 62 74 61 69 6e 20 41 67 67  3REF: Obtain Agg
35770 72 65 67 61 74 65 20 46 75 6e 63 74 69 6f 6e 20  regate Function 
35780 43 6f 6e 74 65 78 74 0a 2a 2a 0a 2a 2a 20 49 6d  Context.**.** Im
35790 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
357a0 20 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66   aggregate SQL f
357b0 75 6e 63 74 69 6f 6e 73 20 75 73 65 20 74 68 69  unctions use thi
357c0 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20  s.** routine to 
357d0 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  allocate memory 
357e0 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 69  for storing thei
357f0 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 5e  r state..**.** ^
35800 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74  The first time t
35810 68 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65  he sqlite3_aggre
35820 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43 2c 4e  gate_context(C,N
35830 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  ) routine is cal
35840 6c 65 64 20 0a 2a 2a 20 66 6f 72 20 61 20 70 61  led .** for a pa
35850 72 74 69 63 75 6c 61 72 20 61 67 67 72 65 67 61  rticular aggrega
35860 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51 4c  te function, SQL
35870 69 74 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 73  ite.** allocates
35880 20 4e 20 6f 66 20 6d 65 6d 6f 72 79 2c 20 7a 65   N of memory, ze
35890 72 6f 65 73 20 6f 75 74 20 74 68 61 74 20 6d 65  roes out that me
358a0 6d 6f 72 79 2c 20 61 6e 64 20 72 65 74 75 72 6e  mory, and return
358b0 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74  s a pointer.** t
358c0 6f 20 74 68 65 20 6e 65 77 20 6d 65 6d 6f 72 79  o the new memory
358d0 2e 20 5e 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64  . ^On second and
358e0 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
358f0 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  s to.** sqlite3_
35900 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
35910 74 28 29 20 66 6f 72 20 74 68 65 20 73 61 6d 65  t() for the same
35920 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
35930 69 6f 6e 20 69 6e 73 74 61 6e 63 65 2c 0a 2a 2a  ion instance,.**
35940 20 74 68 65 20 73 61 6d 65 20 62 75 66 66 65 72   the same buffer
35950 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 53   is returned.  S
35960 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
35970 5f 63 6f 6e 74 65 78 74 28 29 20 69 73 20 6e 6f  _context() is no
35980 72 6d 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 65 64  rmally.** called
35990 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69   once for each i
359a0 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65  nvocation of the
359b0 20 78 53 74 65 70 20 63 61 6c 6c 62 61 63 6b 20   xStep callback 
359c0 61 6e 64 20 74 68 65 6e 20 6f 6e 65 0a 2a 2a 20  and then one.** 
359d0 6c 61 73 74 20 74 69 6d 65 20 77 68 65 6e 20 74  last time when t
359e0 68 65 20 78 46 69 6e 61 6c 20 63 61 6c 6c 62 61  he xFinal callba
359f0 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 2e 20 20  ck is invoked.  
35a00 5e 28 57 68 65 6e 20 6e 6f 20 72 6f 77 73 20 6d  ^(When no rows m
35a10 61 74 63 68 0a 2a 2a 20 61 6e 20 61 67 67 72 65  atch.** an aggre
35a20 67 61 74 65 20 71 75 65 72 79 2c 20 74 68 65 20  gate query, the 
35a30 78 53 74 65 70 28 29 20 63 61 6c 6c 62 61 63 6b  xStep() callback
35a40 20 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74   of the aggregat
35a50 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6d  e function.** im
35a60 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
35a70 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 61 6e 64  never called and
35a80 20 78 46 69 6e 61 6c 28 29 20 69 73 20 63 61 6c   xFinal() is cal
35a90 6c 65 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65  led exactly once
35aa0 2e 0a 2a 2a 20 49 6e 20 74 68 6f 73 65 20 63 61  ..** In those ca
35ab0 73 65 73 2c 20 73 71 6c 69 74 65 33 5f 61 67 67  ses, sqlite3_agg
35ac0 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29  regate_context()
35ad0 20 6d 69 67 68 74 20 62 65 20 63 61 6c 6c 65 64   might be called
35ae0 20 66 6f 72 20 74 68 65 0a 2a 2a 20 66 69 72 73   for the.** firs
35af0 74 20 74 69 6d 65 20 66 72 6f 6d 20 77 69 74 68  t time from with
35b00 69 6e 20 78 46 69 6e 61 6c 28 29 2e 29 5e 0a 2a  in xFinal().)^.*
35b10 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65  *.** ^The sqlite
35b20 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
35b30 65 78 74 28 43 2c 4e 29 20 72 6f 75 74 69 6e 65  ext(C,N) routine
35b40 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20   returns a NULL 
35b50 70 6f 69 6e 74 65 72 20 69 66 20 4e 20 69 73 0a  pointer if N is.
35b60 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  ** less than or 
35b70 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 20 6f 72  equal to zero or
35b80 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   if a memory all
35b90 6f 63 61 74 65 20 65 72 72 6f 72 20 6f 63 63 75  ocate error occu
35ba0 72 73 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20  rs..**.** ^(The 
35bb0 61 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20  amount of space 
35bc0 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c  allocated by sql
35bd0 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
35be0 6f 6e 74 65 78 74 28 43 2c 4e 29 20 69 73 0a 2a  ontext(C,N) is.*
35bf0 2a 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20  * determined by 
35c00 74 68 65 20 4e 20 70 61 72 61 6d 65 74 65 72 20  the N parameter 
35c10 6f 6e 20 66 69 72 73 74 20 73 75 63 63 65 73 73  on first success
35c20 66 75 6c 20 63 61 6c 6c 2e 20 20 43 68 61 6e 67  ful call.  Chang
35c30 69 6e 67 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65  ing the.** value
35c40 20 6f 66 20 4e 20 69 6e 20 73 75 62 73 65 71 75   of N in subsequ
35c50 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ent call to sqli
35c60 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
35c70 6e 74 65 78 74 28 29 20 77 69 74 68 69 6e 0a 2a  ntext() within.*
35c80 2a 20 74 68 65 20 73 61 6d 65 20 61 67 67 72 65  * the same aggre
35c90 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e  gate function in
35ca0 73 74 61 6e 63 65 20 77 69 6c 6c 20 6e 6f 74 20  stance will not 
35cb0 72 65 73 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72  resize the memor
35cc0 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  y.** allocation.
35cd0 29 5e 0a 2a 2a 0a 2a 2a 20 5e 53 51 4c 69 74 65  )^.**.** ^SQLite
35ce0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 66   automatically f
35cf0 72 65 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20  rees the memory 
35d00 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a  allocated by .**
35d10 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
35d20 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 77 68 65  te_context() whe
35d30 6e 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  n the aggregate 
35d40 71 75 65 72 79 20 63 6f 6e 63 6c 75 64 65 73 2e  query concludes.
35d50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
35d60 20 70 61 72 61 6d 65 74 65 72 20 6d 75 73 74 20   parameter must 
35d70 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  be a copy of the
35d80 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  .** [sqlite3_con
35d90 74 65 78 74 20 7c 20 53 51 4c 20 66 75 6e 63 74  text | SQL funct
35da0 69 6f 6e 20 63 6f 6e 74 65 78 74 5d 20 74 68 61  ion context] tha
35db0 74 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70  t is the first p
35dc0 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74  arameter.** to t
35dd0 68 65 20 78 53 74 65 70 20 6f 72 20 78 46 69 6e  he xStep or xFin
35de0 61 6c 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74  al callback rout
35df0 69 6e 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ine that impleme
35e00 6e 74 73 20 74 68 65 20 61 67 67 72 65 67 61 74  nts the aggregat
35e10 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  e.** function..*
35e20 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
35e30 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
35e40 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74   from the same t
35e50 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a  hread in which.*
35e60 2a 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  * the aggregate 
35e70 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  SQL function is 
35e80 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49  running..*/.SQLI
35e90 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
35ea0 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
35eb0 6f 6e 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63  ontext(sqlite3_c
35ec0 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42 79  ontext*, int nBy
35ed0 74 65 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  tes);../*.** CAP
35ee0 49 33 52 45 46 3a 20 55 73 65 72 20 44 61 74 61  I3REF: User Data
35ef0 20 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 0a 2a   For Functions.*
35f00 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65  *.** ^The sqlite
35f10 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 69 6e  3_user_data() in
35f20 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
35f30 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65  a copy of.** the
35f40 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 77 61   pointer that wa
35f50 73 20 74 68 65 20 70 55 73 65 72 44 61 74 61 20  s the pUserData 
35f60 70 61 72 61 6d 65 74 65 72 20 28 74 68 65 20 35  parameter (the 5
35f70 74 68 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a  th parameter).**
35f80 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
35f90 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
35fa0 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69  ()].** and [sqli
35fb0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
35fc0 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e 65  ion16()] routine
35fd0 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 6c  s that originall
35fe0 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 20  y.** registered 
35ff0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
36000 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
36010 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
36020 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c  tine must be cal
36030 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d  led from the sam
36040 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63  e thread in whic
36050 68 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61  h.** the applica
36060 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
36070 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67  ction is running
36080 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
36090 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 73  void *sqlite3_us
360a0 65 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33 5f  er_data(sqlite3_
360b0 63 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a  context*);../*.*
360c0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61  * CAPI3REF: Data
360d0 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20  base Connection 
360e0 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a  For Functions.**
360f0 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33  .** ^The sqlite3
36100 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
36110 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  le() interface r
36120 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66  eturns a copy of
36130 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20  .** the pointer 
36140 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  to the [database
36150 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68   connection] (th
36160 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29  e 1st parameter)
36170 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69  .** of the [sqli
36180 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
36190 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73  ion()].** and [s
361a0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
361b0 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74  nction16()] rout
361c0 69 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e  ines that origin
361d0 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72  ally.** register
361e0 65 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  ed the applicati
361f0 6f 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on defined funct
36200 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ion..*/.SQLITE_A
36210 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69  PI sqlite3 *sqli
36220 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
36230 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f  andle(sqlite3_co
36240 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  ntext*);../*.** 
36250 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69  CAPI3REF: Functi
36260 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74  on Auxiliary Dat
36270 61 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  a.**.** The foll
36280 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 74 69  owing two functi
36290 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65 64 20  ons may be used 
362a0 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20 66 75  by scalar SQL fu
362b0 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 61 73  nctions to.** as
362c0 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61 74 61  sociate metadata
362d0 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20 76   with argument v
362e0 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20 73 61  alues. If the sa
362f0 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 73  me value is pass
36300 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69 70 6c  ed to.** multipl
36310 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66  e invocations of
36320 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20 66 75   the same SQL fu
36330 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20 71 75  nction during qu
36340 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c 20 75  ery execution, u
36350 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63 69 72  nder.** some cir
36360 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65 20 61  cumstances the a
36370 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61 64 61  ssociated metada
36380 74 61 20 6d 61 79 20 62 65 20 70 72 65 73 65 72  ta may be preser
36390 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a 2a 2a  ved. This may.**
363a0 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20 65 78   be used, for ex
363b0 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20 61 20  ample, to add a 
363c0 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73 73 69  regular-expressi
363d0 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63 61 6c  on matching scal
363e0 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20  ar.** function. 
363f0 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76 65 72  The compiled ver
36400 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 67 75  sion of the regu
36410 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69  lar expression i
36420 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 6d  s stored as.** m
36430 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 61 74  etadata associat
36440 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 20  ed with the SQL 
36450 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20  value passed as 
36460 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72  the regular expr
36470 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74 65 72  ession.** patter
36480 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c 65 64  n.  The compiled
36490 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73   regular express
364a0 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75 73 65  ion can be reuse
364b0 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a  d on multiple.**
364c0 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66 20   invocations of 
364d0 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f  the same functio
364e0 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f 72  n so that the or
364f0 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e 20 73  iginal pattern s
36500 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20 6e 6f  tring.** does no
36510 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65 63  t need to be rec
36520 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63 68 20  ompiled on each 
36530 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  invocation..**.*
36540 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 67  * ^The sqlite3_g
36550 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e 74  et_auxdata() int
36560 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
36570 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
36580 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73 6f  metadata.** asso
36590 63 69 61 74 65 64 20 62 79 20 74 68 65 20 73 71  ciated by the sq
365a0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74  lite3_set_auxdat
365b0 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  a() function wit
365c0 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65  h the Nth argume
365d0 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 74  nt.** value to t
365e0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  he application-d
365f0 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e  efined function.
36600 20 5e 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61   ^If no metadata
36610 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a   has been ever.*
36620 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74  * been set for t
36630 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20  he Nth argument 
36640 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c  of the function,
36650 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 65   or if the corre
36660 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63  sponding.** func
36670 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68  tion parameter h
36680 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65  as changed since
36690 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77   the meta-data w
366a0 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20  as set,.** then 
366b0 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
366c0 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 20  ata() returns a 
366d0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  NULL pointer..**
366e0 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33  .** ^The sqlite3
366f0 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69  _set_auxdata() i
36700 6e 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74  nterface saves t
36710 68 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70  he metadata.** p
36720 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73  ointed to by its
36730 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61   3rd parameter a
36740 73 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66  s the metadata f
36750 6f 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61  or the N-th.** a
36760 72 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61  rgument of the a
36770 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
36780 65 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75  ed function.  Su
36790 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c  bsequent.** call
367a0 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74  s to sqlite3_get
367b0 5f 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74  _auxdata() might
367c0 20 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74   return this dat
367d0 61 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20  a, if it has.** 
367e0 6e 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79  not been destroy
367f0 65 64 2e 0a 2a 2a 20 5e 49 66 20 69 74 20 69 73  ed..** ^If it is
36800 20 6e 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74   not NULL, SQLit
36810 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68  e will invoke th
36820 65 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20  e destructor.** 
36830 66 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62  function given b
36840 79 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65  y the 4th parame
36850 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73  ter to sqlite3_s
36860 65 74 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a  et_auxdata() on.
36870 2a 2a 20 74 68 65 20 6d 65 74 61 64 61 74 61 20  ** the metadata 
36880 77 68 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70  when the corresp
36890 6f 6e 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  onding function 
368a0 70 61 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65  parameter change
368b0 73 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65  s.** or when the
368c0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63   SQL statement c
368d0 6f 6d 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65  ompletes, whiche
368e0 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e  ver comes first.
368f0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73  .**.** SQLite is
36900 20 66 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68   free to call th
36910 65 20 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64  e destructor and
36920 20 64 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f   drop metadata o
36930 6e 20 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74  n any.** paramet
36940 65 72 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69  er of any functi
36950 6f 6e 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20  on at any time. 
36960 20 5e 54 68 65 20 6f 6e 6c 79 20 67 75 61 72 61   ^The only guara
36970 6e 74 65 65 20 69 73 20 74 68 61 74 0a 2a 2a 20  ntee is that.** 
36980 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 77  the destructor w
36990 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 65  ill be called be
369a0 66 6f 72 65 20 74 68 65 20 6d 65 74 61 64 61 74  fore the metadat
369b0 61 20 69 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a  a is dropped..**
369c0 0a 2a 2a 20 5e 28 49 6e 20 70 72 61 63 74 69 63  .** ^(In practic
369d0 65 2c 20 6d 65 74 61 64 61 74 61 20 69 73 20 70  e, metadata is p
369e0 72 65 73 65 72 76 65 64 20 62 65 74 77 65 65 6e  reserved between
369f0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20   function calls 
36a00 66 6f 72 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  for.** expressio
36a10 6e 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 73  ns that are cons
36a20 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20  tant at compile 
36a30 74 69 6d 65 2e 20 54 68 69 73 20 69 6e 63 6c 75  time. This inclu
36a40 64 65 73 20 6c 69 74 65 72 61 6c 0a 2a 2a 20 76  des literal.** v
36a50 61 6c 75 65 73 20 61 6e 64 20 5b 70 61 72 61 6d  alues and [param
36a60 65 74 65 72 73 5d 2e 29 5e 0a 2a 2a 0a 2a 2a 20  eters].)^.**.** 
36a70 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d  These routines m
36a80 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72  ust be called fr
36a90 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  om the same thre
36aa0 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74  ad in which.** t
36ab0 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  he SQL function 
36ac0 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 53  is running..*/.S
36ad0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
36ae0 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
36af0 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ata(sqlite3_cont
36b00 65 78 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51  ext*, int N);.SQ
36b10 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
36b20 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74  lite3_set_auxdat
36b30 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  a(sqlite3_contex
36b40 74 2a 2c 20 69 6e 74 20 4e 2c 20 76 6f 69 64 2a  t*, int N, void*
36b50 2c 20 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a  , void (*)(void*
36b60 29 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ));.../*.** CAPI
36b70 33 52 45 46 3a 20 43 6f 6e 73 74 61 6e 74 73 20  3REF: Constants 
36b80 44 65 66 69 6e 69 6e 67 20 53 70 65 63 69 61 6c  Defining Special
36b90 20 44 65 73 74 72 75 63 74 6f 72 20 42 65 68 61   Destructor Beha
36ba0 76 69 6f 72 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  vior.**.** These
36bb0 20 61 72 65 20 73 70 65 63 69 61 6c 20 76 61 6c   are special val
36bc0 75 65 73 20 66 6f 72 20 74 68 65 20 64 65 73 74  ues for the dest
36bd0 72 75 63 74 6f 72 20 74 68 61 74 20 69 73 20 70  ructor that is p
36be0 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 0a  assed in as the.
36bf0 2a 2a 20 66 69 6e 61 6c 20 61 72 67 75 6d 65 6e  ** final argumen
36c00 74 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69  t to routines li
36c10 6b 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75  ke [sqlite3_resu
36c20 6c 74 5f 62 6c 6f 62 28 29 5d 2e 20 20 5e 49 66  lt_blob()].  ^If
36c30 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 0a   the destructor.
36c40 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53  ** argument is S
36c50 51 4c 49 54 45 5f 53 54 41 54 49 43 2c 20 69 74  QLITE_STATIC, it
36c60 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
36c70 63 6f 6e 74 65 6e 74 20 70 6f 69 6e 74 65 72 20  content pointer 
36c80 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61  is constant.** a
36c90 6e 64 20 77 69 6c 6c 20 6e 65 76 65 72 20 63 68  nd will never ch
36ca0 61 6e 67 65 2e 20 20 49 74 20 64 6f 65 73 20 6e  ange.  It does n
36cb0 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 64 65  ot need to be de
36cc0 73 74 72 6f 79 65 64 2e 20 20 5e 54 68 65 0a 2a  stroyed.  ^The.*
36cd0 2a 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  * SQLITE_TRANSIE
36ce0 4e 54 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 74  NT value means t
36cf0 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  hat the content 
36d00 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 68 61 6e  will likely chan
36d10 67 65 20 69 6e 0a 2a 2a 20 74 68 65 20 6e 65 61  ge in.** the nea
36d20 72 20 66 75 74 75 72 65 20 61 6e 64 20 74 68 61  r future and tha
36d30 74 20 53 51 4c 69 74 65 20 73 68 6f 75 6c 64 20  t SQLite should 
36d40 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 70 72 69  make its own pri
36d50 76 61 74 65 20 63 6f 70 79 20 6f 66 0a 2a 2a 20  vate copy of.** 
36d60 74 68 65 20 63 6f 6e 74 65 6e 74 20 62 65 66 6f  the content befo
36d70 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a  re returning..**
36d80 0a 2a 2a 20 54 68 65 20 74 79 70 65 64 65 66 20  .** The typedef 
36d90 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20  is necessary to 
36da0 77 6f 72 6b 20 61 72 6f 75 6e 64 20 70 72 6f 62  work around prob
36db0 6c 65 6d 73 20 69 6e 20 63 65 72 74 61 69 6e 0a  lems in certain.
36dc0 2a 2a 20 43 2b 2b 20 63 6f 6d 70 69 6c 65 72 73  ** C++ compilers
36dd0 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 32  .  See ticket #2
36de0 31 39 31 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  191..*/.typedef 
36df0 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 5f 64  void (*sqlite3_d
36e00 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 28  estructor_type)(
36e10 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e 65 20  void*);.#define 
36e20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20 20 20  SQLITE_STATIC   
36e30 20 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73     ((sqlite3_des
36e40 74 72 75 63 74 6f 72 5f 74 79 70 65 29 30 29 0a  tructor_type)0).
36e50 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54  #define SQLITE_T
36e60 52 41 4e 53 49 45 4e 54 20 20 20 28 28 73 71 6c  RANSIENT   ((sql
36e70 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f  ite3_destructor_
36e80 74 79 70 65 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20  type)-1)../*.** 
36e90 43 41 50 49 33 52 45 46 3a 20 53 65 74 74 69 6e  CAPI3REF: Settin
36ea0 67 20 54 68 65 20 52 65 73 75 6c 74 20 4f 66 20  g The Result Of 
36eb0 41 6e 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 0a  An SQL Function.
36ec0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
36ed0 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 62 79  ines are used by
36ee0 20 74 68 65 20 78 46 75 6e 63 20 6f 72 20 78 46   the xFunc or xF
36ef0 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 74  inal callbacks t
36f00 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  hat.** implement
36f10 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61   SQL functions a
36f20 6e 64 20 61 67 67 72 65 67 61 74 65 73 2e 20 20  nd aggregates.  
36f30 53 65 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  See.** [sqlite3_
36f40 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
36f50 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
36f60 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
36f70 36 28 29 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 69  6()].** for addi
36f80 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
36f90 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  on..**.** These 
36fa0 66 75 6e 63 74 69 6f 6e 73 20 77 6f 72 6b 20 76  functions work v
36fb0 65 72 79 20 6d 75 63 68 20 6c 69 6b 65 20 74 68  ery much like th
36fc0 65 20 5b 70 61 72 61 6d 65 74 65 72 20 62 69 6e  e [parameter bin
36fd0 64 69 6e 67 5d 20 66 61 6d 69 6c 79 20 6f 66 0a  ding] family of.
36fe0 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65  ** functions use
36ff0 64 20 74 6f 20 62 69 6e 64 20 76 61 6c 75 65 73  d to bind values
37000 20 74 6f 20 68 6f 73 74 20 70 61 72 61 6d 65 74   to host paramet
37010 65 72 73 20 69 6e 20 70 72 65 70 61 72 65 64 20  ers in prepared 
37020 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 52  statements..** R
37030 65 66 65 72 20 74 6f 20 74 68 65 20 5b 53 51 4c  efer to the [SQL
37040 20 70 61 72 61 6d 65 74 65 72 5d 20 64 6f 63 75   parameter] docu
37050 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64  mentation for ad
37060 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
37070 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65  tion..**.** ^The
37080 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
37090 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65  blob() interface
370a0 20 73 65 74 73 20 74 68 65 20 72 65 73 75 6c 74   sets the result
370b0 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c   from.** an appl
370c0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
370d0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 74  function to be t
370e0 68 65 20 42 4c 4f 42 20 77 68 6f 73 65 20 63 6f  he BLOB whose co
370f0 6e 74 65 6e 74 20 69 73 20 70 6f 69 6e 74 65 64  ntent is pointed
37100 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65 20 73 65  .** to by the se
37110 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61  cond parameter a
37120 6e 64 20 77 68 69 63 68 20 69 73 20 4e 20 62 79  nd which is N by
37130 74 65 73 20 6c 6f 6e 67 20 77 68 65 72 65 20 4e  tes long where N
37140 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64   is the.** third
37150 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a   parameter..**.*
37160 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72  * ^The sqlite3_r
37170 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29  esult_zeroblob()
37180 20 69 6e 74 65 72 66 61 63 65 73 20 73 65 74 20   interfaces set 
37190 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a  the result of.**
371a0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
371b0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
371c0 6e 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 63  n to be a BLOB c
371d0 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65  ontaining all ze
371e0 72 6f 0a 2a 2a 20 62 79 74 65 73 20 61 6e 64 20  ro.** bytes and 
371f0 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c  N bytes in size,
37200 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20   where N is the 
37210 76 61 6c 75 65 20 6f 66 20 74 68 65 20 32 6e 64  value of the 2nd
37220 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a   parameter..**.*
37230 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72  * ^The sqlite3_r
37240 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 29 20 69  esult_double() i
37250 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68  nterface sets th
37260 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a  e result from.**
37270 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   an application-
37280 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
37290 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e   to be a floatin
372a0 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 73 70  g point value sp
372b0 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 69 74  ecified.** by it
372c0 73 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a  s 2nd argument..
372d0 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74  **.** ^The sqlit
372e0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
372f0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65  ) and sqlite3_re
37300 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 66  sult_error16() f
37310 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 63 61 75 73  unctions.** caus
37320 65 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 65  e the implemente
37330 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74  d SQL function t
37340 6f 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70  o throw an excep
37350 74 69 6f 6e 2e 0a 2a 2a 20 5e 53 51 4c 69 74 65  tion..** ^SQLite
37360 20 75 73 65 73 20 74 68 65 20 73 74 72 69 6e 67   uses the string
37370 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74   pointed to by t
37380 68 65 0a 2a 2a 20 32 6e 64 20 70 61 72 61 6d 65  he.** 2nd parame
37390 74 65 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 72  ter of sqlite3_r
373a0 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 6f 72  esult_error() or
373b0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
373c0 65 72 72 6f 72 31 36 28 29 0a 2a 2a 20 61 73 20  error16().** as 
373d0 74 68 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65  the text of an e
373e0 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 20 20 5e  rror message.  ^
373f0 53 51 4c 69 74 65 20 69 6e 74 65 72 70 72 65 74  SQLite interpret
37400 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d  s the error.** m
37410 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 66 72  essage string fr
37420 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  om sqlite3_resul
37430 74 5f 65 72 72 6f 72 28 29 20 61 73 20 55 54 46  t_error() as UTF
37440 2d 38 2e 20 5e 53 51 4c 69 74 65 0a 2a 2a 20 69  -8. ^SQLite.** i
37450 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 73 74  nterprets the st
37460 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74 65  ring from sqlite
37470 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
37480 28 29 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20  () as UTF-16 in 
37490 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74 65 20 6f  native.** byte o
374a0 72 64 65 72 2e 20 20 5e 49 66 20 74 68 65 20 74  rder.  ^If the t
374b0 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74  hird parameter t
374c0 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  o sqlite3_result
374d0 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 73  _error().** or s
374e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
374f0 72 6f 72 31 36 28 29 20 69 73 20 6e 65 67 61 74  ror16() is negat
37500 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 20  ive then SQLite 
37510 74 61 6b 65 73 20 61 73 20 74 68 65 20 65 72 72  takes as the err
37520 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 61 6c  or.** message al
37530 6c 20 74 65 78 74 20 75 70 20 74 68 72 6f 75 67  l text up throug
37540 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f  h the first zero
37550 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 5e   character..** ^
37560 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61 72  If the third par
37570 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65  ameter to sqlite
37580 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29  3_result_error()
37590 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72   or.** sqlite3_r
375a0 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20  esult_error16() 
375b0 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20  is non-negative 
375c0 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65  then SQLite take
375d0 73 20 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20 62  s that many.** b
375e0 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63  ytes (not charac
375f0 74 65 72 73 29 20 66 72 6f 6d 20 74 68 65 20 32  ters) from the 2
37600 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20  nd parameter as 
37610 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
37620 65 2e 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74  e..** ^The sqlit
37630 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
37640 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65  ) and sqlite3_re
37650 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a  sult_error16().*
37660 2a 20 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20  * routines make 
37670 61 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f  a private copy o
37680 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73  f the error mess
37690 61 67 65 20 74 65 78 74 20 62 65 66 6f 72 65 0a  age text before.
376a0 2a 2a 20 74 68 65 79 20 72 65 74 75 72 6e 2e 20  ** they return. 
376b0 20 48 65 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c   Hence, the call
376c0 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e  ing function can
376d0 20 64 65 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a   deallocate or.*
376e0 2a 20 6d 6f 64 69 66 79 20 74 68 65 20 74 65 78  * modify the tex
376f0 74 20 61 66 74 65 72 20 74 68 65 79 20 72 65 74  t after they ret
37700 75 72 6e 20 77 69 74 68 6f 75 74 20 68 61 72 6d  urn without harm
37710 2e 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65  ..** ^The sqlite
37720 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63  3_result_error_c
37730 6f 64 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 63  ode() function c
37740 68 61 6e 67 65 73 20 74 68 65 20 65 72 72 6f 72  hanges the error
37750 20 63 6f 64 65 0a 2a 2a 20 72 65 74 75 72 6e 65   code.** returne
37760 64 20 62 79 20 53 51 4c 69 74 65 20 61 73 20 61  d by SQLite as a
37770 20 72 65 73 75 6c 74 20 6f 66 20 61 6e 20 65 72   result of an er
37780 72 6f 72 20 69 6e 20 61 20 66 75 6e 63 74 69 6f  ror in a functio
37790 6e 2e 20 20 5e 42 79 20 64 65 66 61 75 6c 74 2c  n.  ^By default,
377a0 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 63 6f  .** the error co
377b0 64 65 20 69 73 20 53 51 4c 49 54 45 5f 45 52 52  de is SQLITE_ERR
377c0 4f 52 2e 20 20 5e 41 20 73 75 62 73 65 71 75 65  OR.  ^A subseque
377d0 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  nt call to sqlit
377e0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
377f0 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f  ).** or sqlite3_
37800 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29  result_error16()
37810 20 72 65 73 65 74 73 20 74 68 65 20 65 72 72 6f   resets the erro
37820 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54 45  r code to SQLITE
37830 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 5e 54  _ERROR..**.** ^T
37840 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
37850 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 29  t_error_toobig()
37860 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 73 65   interface cause
37870 73 20 53 51 4c 69 74 65 20 74 6f 20 74 68 72 6f  s SQLite to thro
37880 77 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 69 6e  w an.** error in
37890 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 61 20  dicating that a 
378a0 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 69  string or BLOB i
378b0 73 20 74 6f 6f 20 6c 6f 6e 67 20 74 6f 20 72 65  s too long to re
378c0 70 72 65 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 5e  present..**.** ^
378d0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
378e0 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 29  lt_error_nomem()
378f0 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 73 65   interface cause
37900 73 20 53 51 4c 69 74 65 20 74 6f 20 74 68 72 6f  s SQLite to thro
37910 77 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 69 6e  w an.** error in
37920 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 61 20  dicating that a 
37930 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
37940 6e 20 66 61 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20  n failed..**.** 
37950 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73  ^The sqlite3_res
37960 75 6c 74 5f 69 6e 74 28 29 20 69 6e 74 65 72 66  ult_int() interf
37970 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74  ace sets the ret
37980 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  urn value.** of 
37990 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
379a0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
379b0 20 74 6f 20 62 65 20 74 68 65 20 33 32 2d 62 69   to be the 32-bi
379c0 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
379d0 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 65 6e 20  .** value given 
379e0 69 6e 20 74 68 65 20 32 6e 64 20 61 72 67 75 6d  in the 2nd argum
379f0 65 6e 74 2e 0a 2a 2a 20 5e 54 68 65 20 73 71 6c  ent..** ^The sql
37a00 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
37a10 34 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65  4() interface se
37a20 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ts the return va
37a30 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70  lue.** of the ap
37a40 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
37a50 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  d function to be
37a60 20 74 68 65 20 36 34 2d 62 69 74 20 73 69 67 6e   the 64-bit sign
37a70 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61  ed integer.** va
37a80 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65  lue given in the
37a90 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a   2nd argument..*
37aa0 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65  *.** ^The sqlite
37ab0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 29 20  3_result_null() 
37ac0 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74  interface sets t
37ad0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
37ae0 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63  ** of the applic
37af0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
37b00 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 4e 55 4c  nction to be NUL
37b10 4c 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71  L..**.** ^The sq
37b20 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
37b30 74 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73  t(), sqlite3_res
37b40 75 6c 74 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a  ult_text16(),.**
37b50 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
37b60 74 65 78 74 31 36 6c 65 28 29 2c 20 61 6e 64 20  text16le(), and 
37b70 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
37b80 65 78 74 31 36 62 65 28 29 20 69 6e 74 65 72 66  ext16be() interf
37b90 61 63 65 73 0a 2a 2a 20 73 65 74 20 74 68 65 20  aces.** set the 
37ba0 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
37bb0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
37bc0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
37bd0 20 74 6f 20 62 65 0a 2a 2a 20 61 20 74 65 78 74   to be.** a text
37be0 20 73 74 72 69 6e 67 20 77 68 69 63 68 20 69 73   string which is
37bf0 20 72 65 70 72 65 73 65 6e 74 65 64 20 61 73 20   represented as 
37c00 55 54 46 2d 38 2c 20 55 54 46 2d 31 36 20 6e 61  UTF-8, UTF-16 na
37c10 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2c  tive byte order,
37c20 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 6c  .** UTF-16 littl
37c30 65 20 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46  e endian, or UTF
37c40 2d 31 36 20 62 69 67 20 65 6e 64 69 61 6e 2c 20  -16 big endian, 
37c50 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a  respectively..**
37c60 20 5e 53 51 4c 69 74 65 20 74 61 6b 65 73 20 74   ^SQLite takes t
37c70 68 65 20 74 65 78 74 20 72 65 73 75 6c 74 20 66  he text result f
37c80 72 6f 6d 20 74 68 65 20 61 70 70 6c 69 63 61 74  rom the applicat
37c90 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  ion from.** the 
37ca0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66  2nd parameter of
37cb0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   the sqlite3_res
37cc0 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66  ult_text* interf
37cd0 61 63 65 73 2e 0a 2a 2a 20 5e 49 66 20 74 68 65  aces..** ^If the
37ce0 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74   3rd parameter t
37cf0 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65  o the sqlite3_re
37d00 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72  sult_text* inter
37d10 66 61 63 65 73 0a 2a 2a 20 69 73 20 6e 65 67 61  faces.** is nega
37d20 74 69 76 65 2c 20 74 68 65 6e 20 53 51 4c 69 74  tive, then SQLit
37d30 65 20 74 61 6b 65 73 20 72 65 73 75 6c 74 20 74  e takes result t
37d40 65 78 74 20 66 72 6f 6d 20 74 68 65 20 32 6e 64  ext from the 2nd
37d50 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 68   parameter.** th
37d60 72 6f 75 67 68 20 74 68 65 20 66 69 72 73 74 20  rough the first 
37d70 7a 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e 0a  zero character..
37d80 2a 2a 20 5e 49 66 20 74 68 65 20 33 72 64 20 70  ** ^If the 3rd p
37d90 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
37da0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
37db0 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a  ext* interfaces.
37dc0 2a 2a 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69  ** is non-negati
37dd0 76 65 2c 20 74 68 65 6e 20 61 73 20 6d 61 6e 79  ve, then as many
37de0 20 62 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72   bytes (not char
37df0 61 63 74 65 72 73 29 20 6f 66 20 74 68 65 20 74  acters) of the t
37e00 65 78 74 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74  ext.** pointed t
37e10 6f 20 62 79 20 74 68 65 20 32 6e 64 20 70 61 72  o by the 2nd par
37e20 61 6d 65 74 65 72 20 61 72 65 20 74 61 6b 65 6e  ameter are taken
37e30 20 61 73 20 74 68 65 20 61 70 70 6c 69 63 61 74   as the applicat
37e40 69 6f 6e 2d 64 65 66 69 6e 65 64 0a 2a 2a 20 66  ion-defined.** f
37e50 75 6e 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 20  unction result. 
37e60 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61   If the 3rd para
37e70 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 6e 65 67  meter is non-neg
37e80 61 74 69 76 65 2c 20 74 68 65 6e 20 69 74 0a 2a  ative, then it.*
37e90 2a 20 6d 75 73 74 20 62 65 20 74 68 65 20 62 79  * must be the by
37ea0 74 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74  te offset into t
37eb0 68 65 20 73 74 72 69 6e 67 20 77 68 65 72 65 20  he string where 
37ec0 74 68 65 20 4e 55 4c 20 74 65 72 6d 69 6e 61 74  the NUL terminat
37ed0 6f 72 20 77 6f 75 6c 64 0a 2a 2a 20 61 70 70 65  or would.** appe
37ee0 61 72 20 69 66 20 74 68 65 20 73 74 72 69 6e 67  ar if the string
37ef0 20 77 68 65 72 65 20 4e 55 4c 20 74 65 72 6d 69   where NUL termi
37f00 6e 61 74 65 64 2e 20 20 49 66 20 61 6e 79 20 4e  nated.  If any N
37f10 55 4c 20 63 68 61 72 61 63 74 65 72 73 20 6f 63  UL characters oc
37f20 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65 20 73 74  cur.** in the st
37f30 72 69 6e 67 20 61 74 20 61 20 62 79 74 65 20 6f  ring at a byte o
37f40 66 66 73 65 74 20 74 68 61 74 20 69 73 20 6c 65  ffset that is le
37f50 73 73 20 74 68 61 6e 20 74 68 65 20 76 61 6c 75  ss than the valu
37f60 65 20 6f 66 20 74 68 65 20 33 72 64 0a 2a 2a 20  e of the 3rd.** 
37f70 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 20  parameter, then 
37f80 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 73 74  the resulting st
37f90 72 69 6e 67 20 77 69 6c 6c 20 63 6f 6e 74 61 69  ring will contai
37fa0 6e 20 65 6d 62 65 64 64 65 64 20 4e 55 4c 73 20  n embedded NULs 
37fb0 61 6e 64 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c  and the.** resul
37fc0 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73  t of expressions
37fd0 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e 20 73 74   operating on st
37fe0 72 69 6e 67 73 20 77 69 74 68 20 65 6d 62 65 64  rings with embed
37ff0 64 65 64 20 4e 55 4c 73 20 69 73 20 75 6e 64 65  ded NULs is unde
38000 66 69 6e 65 64 2e 0a 2a 2a 20 5e 49 66 20 74 68  fined..** ^If th
38010 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 4th parameter 
38020 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  to the sqlite3_r
38030 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65  esult_text* inte
38040 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c  rfaces.** or sql
38050 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
38060 20 69 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70   is a non-NULL p
38070 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c  ointer, then SQL
38080 69 74 65 20 63 61 6c 6c 73 20 74 68 61 74 0a 2a  ite calls that.*
38090 2a 20 66 75 6e 63 74 69 6f 6e 20 61 73 20 74 68  * function as th
380a0 65 20 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20  e destructor on 
380b0 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42  the text or BLOB
380c0 20 72 65 73 75 6c 74 20 77 68 65 6e 20 69 74 20   result when it 
380d0 68 61 73 0a 2a 2a 20 66 69 6e 69 73 68 65 64 20  has.** finished 
380e0 75 73 69 6e 67 20 74 68 61 74 20 72 65 73 75 6c  using that resul
380f0 74 2e 0a 2a 2a 20 5e 49 66 20 74 68 65 20 34 74  t..** ^If the 4t
38100 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  h parameter to t
38110 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
38120 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63  t_text* interfac
38130 65 73 20 6f 72 20 74 6f 0a 2a 2a 20 73 71 6c 69  es or to.** sqli
38140 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
38150 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63  is the special c
38160 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 53  onstant SQLITE_S
38170 54 41 54 49 43 2c 20 74 68 65 6e 20 53 51 4c 69  TATIC, then SQLi
38180 74 65 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68  te.** assumes th
38190 61 74 20 74 68 65 20 74 65 78 74 20 6f 72 20 42  at the text or B
381a0 4c 4f 42 20 72 65 73 75 6c 74 20 69 73 20 69 6e  LOB result is in
381b0 20 63 6f 6e 73 74 61 6e 74 20 73 70 61 63 65 20   constant space 
381c0 61 6e 64 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20  and does not.** 
381d0 63 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74  copy the content
381e0 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   of the paramete
381f0 72 20 6e 6f 72 20 63 61 6c 6c 20 61 20 64 65 73  r nor call a des
38200 74 72 75 63 74 6f 72 20 6f 6e 20 74 68 65 20 63  tructor on the c
38210 6f 6e 74 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 69  ontent.** when i
38220 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 75  t has finished u
38230 73 69 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74  sing that result
38240 2e 0a 2a 2a 20 5e 49 66 20 74 68 65 20 34 74 68  ..** ^If the 4th
38250 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
38260 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
38270 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65  _text* interface
38280 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f  s.** or sqlite3_
38290 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 74  result_blob is t
382a0 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e 73 74  he special const
382b0 61 6e 74 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  ant SQLITE_TRANS
382c0 49 45 4e 54 0a 2a 2a 20 74 68 65 6e 20 53 51 4c  IENT.** then SQL
382d0 69 74 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79  ite makes a copy
382e0 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 69   of the result i
382f0 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e  nto space obtain
38300 65 64 20 66 72 6f 6d 0a 2a 2a 20 66 72 6f 6d 20  ed from.** from 
38310 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
38320 29 5d 20 62 65 66 6f 72 65 20 69 74 20 72 65 74  )] before it ret
38330 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65  urns..**.** ^The
38340 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
38350 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66 61 63  value() interfac
38360 65 20 73 65 74 73 20 74 68 65 20 72 65 73 75 6c  e sets the resul
38370 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c  t of.** the appl
38380 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
38390 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61  function to be a
383a0 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20 5b 75 6e   copy the.** [un
383b0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
383c0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20  3_value] object 
383d0 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
383e0 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20   2nd parameter. 
383f0 20 5e 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33   ^The.** sqlite3
38400 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 20  _result_value() 
38410 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 20  interface makes 
38420 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 5b 73  a copy of the [s
38430 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a  qlite3_value].**
38440 20 73 6f 20 74 68 61 74 20 74 68 65 20 5b 73 71   so that the [sq
38450 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 73 70 65  lite3_value] spe
38460 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 70 61  cified in the pa
38470 72 61 6d 65 74 65 72 20 6d 61 79 20 63 68 61 6e  rameter may chan
38480 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64 65 61 6c  ge or.** be deal
38490 6c 6f 63 61 74 65 64 20 61 66 74 65 72 20 73 71  located after sq
384a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
384b0 75 65 28 29 20 72 65 74 75 72 6e 73 20 77 69 74  ue() returns wit
384c0 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a 20 5e 41  hout harm..** ^A
384d0 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69   [protected sqli
384e0 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63  te3_value] objec
384f0 74 20 6d 61 79 20 61 6c 77 61 79 73 20 62 65 20  t may always be 
38500 75 73 65 64 20 77 68 65 72 65 20 61 6e 0a 2a 2a  used where an.**
38510 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71   [unprotected sq
38520 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
38530 65 63 74 20 69 73 20 72 65 71 75 69 72 65 64 2c  ect is required,
38540 20 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20 6b 69   so either.** ki
38550 6e 64 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 76  nd of [sqlite3_v
38560 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63 61 6e  alue] object can
38570 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74 68   be used with th
38580 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  is interface..**
38590 0a 2a 2a 20 49 66 20 74 68 65 73 65 20 72 6f 75  .** If these rou
385a0 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 64  tines are called
385b0 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
385c0 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61   different threa
385d0 64 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 6f 6e  d.** than the on
385e0 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
385f0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
38600 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 68  ined function th
38610 61 74 20 72 65 63 65 69 76 65 64 0a 2a 2a 20 74  at received.** t
38620 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74  he [sqlite3_cont
38630 65 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20 74 68  ext] pointer, th
38640 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e  e results are un
38650 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  defined..*/.SQLI
38660 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
38670 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28  te3_result_blob(
38680 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
38690 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69  , const void*, i
386a0 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64  nt, void(*)(void
386b0 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  *));.SQLITE_API 
386c0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
386d0 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74  ult_double(sqlit
386e0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 6f 75  e3_context*, dou
386f0 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ble);.SQLITE_API
38700 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
38710 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69 74  sult_error(sqlit
38720 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  e3_context*, con
38730 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a  st char*, int);.
38740 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
38750 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
38760 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f 63  rror16(sqlite3_c
38770 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76  ontext*, const v
38780 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  oid*, int);.SQLI
38790 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
387a0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
387b0 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33 5f  _toobig(sqlite3_
387c0 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54  context*);.SQLIT
387d0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
387e0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
387f0 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63 6f  nomem(sqlite3_co
38800 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  ntext*);.SQLITE_
38810 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
38820 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f  _result_error_co
38830 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  de(sqlite3_conte
38840 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  xt*, int);.SQLIT
38850 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
38860 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73 71  e3_result_int(sq
38870 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
38880 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
38890 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
388a0 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69 74  sult_int64(sqlit
388b0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71 6c  e3_context*, sql
388c0 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c  ite3_int64);.SQL
388d0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
388e0 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
388f0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
38900 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  *);.SQLITE_API v
38910 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
38920 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f  lt_text(sqlite3_
38930 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20  context*, const 
38940 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f 69 64  char*, int, void
38950 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c  (*)(void*));.SQL
38960 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
38970 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
38980 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  16(sqlite3_conte
38990 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  xt*, const void*
389a0 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76  , int, void(*)(v
389b0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41  oid*));.SQLITE_A
389c0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
389d0 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 28  result_text16le(
389e0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
389f0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69  , const void*, i
38a00 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  nt,void(*)(void*
38a10 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  ));.SQLITE_API v
38a20 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
38a30 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69  lt_text16be(sqli
38a40 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f  te3_context*, co
38a50 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 76  nst void*, int,v
38a60 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
38a70 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
38a80 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
38a90 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  alue(sqlite3_con
38aa0 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f 76  text*, sqlite3_v
38ab0 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  alue*);.SQLITE_A
38ac0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
38ad0 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28  result_zeroblob(
38ae0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
38af0 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a  , int n);../*.**
38b00 20 43 41 50 49 33 52 45 46 3a 20 44 65 66 69 6e   CAPI3REF: Defin
38b10 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e 67 20  e New Collating 
38b20 53 65 71 75 65 6e 63 65 73 0a 2a 2a 0a 2a 2a 20  Sequences.**.** 
38b30 5e 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73  ^These functions
38b40 20 61 64 64 2c 20 72 65 6d 6f 76 65 2c 20 6f 72   add, remove, or
38b50 20 6d 6f 64 69 66 79 20 61 20 5b 63 6f 6c 6c 61   modify a [colla
38b60 74 69 6f 6e 5d 20 61 73 73 6f 63 69 61 74 65 64  tion] associated
38b70 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 64 61  .** with the [da
38b80 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
38b90 6e 5d 20 73 70 65 63 69 66 69 65 64 20 61 73 20  n] specified as 
38ba0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
38bb0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 6e  nt..**.** ^The n
38bc0 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61  ame of the colla
38bd0 74 69 6f 6e 20 69 73 20 61 20 55 54 46 2d 38 20  tion is a UTF-8 
38be0 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 73 71  string.** for sq
38bf0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
38c00 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73 71 6c  lation() and sql
38c10 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
38c20 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20 61 6e  ation_v2().** an
38c30 64 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e  d a UTF-16 strin
38c40 67 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65  g in native byte
38c50 20 6f 72 64 65 72 20 66 6f 72 20 73 71 6c 69 74   order for sqlit
38c60 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
38c70 69 6f 6e 31 36 28 29 2e 0a 2a 2a 20 5e 43 6f 6c  ion16()..** ^Col
38c80 6c 61 74 69 6f 6e 20 6e 61 6d 65 73 20 74 68 61  lation names tha
38c90 74 20 63 6f 6d 70 61 72 65 20 65 71 75 61 6c 20  t compare equal 
38ca0 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 5b 73 71  according to [sq
38cb0 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 29  lite3_strnicmp()
38cc0 5d 20 61 72 65 0a 2a 2a 20 63 6f 6e 73 69 64 65  ] are.** conside
38cd0 72 65 64 20 74 6f 20 62 65 20 74 68 65 20 73 61  red to be the sa
38ce0 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 5e  me name..**.** ^
38cf0 28 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d  (The third argum
38d00 65 6e 74 20 28 65 54 65 78 74 52 65 70 29 20 6d  ent (eTextRep) m
38d10 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 74 68  ust be one of th
38d20 65 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 20  e constants:.** 
38d30 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51  <ul>.** <li> [SQ
38d40 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a 20 3c  LITE_UTF8],.** <
38d50 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 55 54 46 31  li> [SQLITE_UTF1
38d60 36 4c 45 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  6LE],.** <li> [S
38d70 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c 0a  QLITE_UTF16BE],.
38d80 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
38d90 55 54 46 31 36 5d 2c 20 6f 72 0a 2a 2a 20 3c 6c  UTF16], or.** <l
38da0 69 3e 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36  i> [SQLITE_UTF16
38db0 5f 41 4c 49 47 4e 45 44 5d 2e 0a 2a 2a 20 3c 2f  _ALIGNED]..** </
38dc0 75 6c 3e 29 5e 0a 2a 2a 20 5e 54 68 65 20 65 54  ul>)^.** ^The eT
38dd0 65 78 74 52 65 70 20 61 72 67 75 6d 65 6e 74 20  extRep argument 
38de0 64 65 74 65 72 6d 69 6e 65 73 20 74 68 65 20 65  determines the e
38df0 6e 63 6f 64 69 6e 67 20 6f 66 20 73 74 72 69 6e  ncoding of strin
38e00 67 73 20 70 61 73 73 65 64 0a 2a 2a 20 74 6f 20  gs passed.** to 
38e10 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75  the collating fu
38e20 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2c  nction callback,
38e30 20 78 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 5e   xCallback..** ^
38e40 54 68 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31  The [SQLITE_UTF1
38e50 36 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 55  6] and [SQLITE_U
38e60 54 46 31 36 5f 41 4c 49 47 4e 45 44 5d 20 76 61  TF16_ALIGNED] va
38e70 6c 75 65 73 20 66 6f 72 20 65 54 65 78 74 52 65  lues for eTextRe
38e80 70 0a 2a 2a 20 66 6f 72 63 65 20 73 74 72 69 6e  p.** force strin
38e90 67 73 20 74 6f 20 62 65 20 55 54 46 31 36 20 77  gs to be UTF16 w
38ea0 69 74 68 20 6e 61 74 69 76 65 20 62 79 74 65 20  ith native byte 
38eb0 6f 72 64 65 72 2e 0a 2a 2a 20 5e 54 68 65 20 5b  order..** ^The [
38ec0 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49  SQLITE_UTF16_ALI
38ed0 47 4e 45 44 5d 20 76 61 6c 75 65 20 66 6f 72 20  GNED] value for 
38ee0 65 54 65 78 74 52 65 70 20 66 6f 72 63 65 73 20  eTextRep forces 
38ef0 73 74 72 69 6e 67 73 20 74 6f 20 62 65 67 69 6e  strings to begin
38f00 0a 2a 2a 20 6f 6e 20 61 6e 20 65 76 65 6e 20 62  .** on an even b
38f10 79 74 65 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a  yte address..**.
38f20 2a 2a 20 5e 54 68 65 20 66 6f 75 72 74 68 20 61  ** ^The fourth a
38f30 72 67 75 6d 65 6e 74 2c 20 70 41 72 67 2c 20 69  rgument, pArg, i
38f40 73 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  s an application
38f50 20 64 61 74 61 20 70 6f 69 6e 74 65 72 20 74 68   data pointer th
38f60 61 74 20 69 73 20 70 61 73 73 65 64 0a 2a 2a 20  at is passed.** 
38f70 74 68 72 6f 75 67 68 20 61 73 20 74 68 65 20 66  through as the f
38f80 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
38f90 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66   the collating f
38fa0 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b  unction callback
38fb0 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 66  ..**.** ^The fif
38fc0 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 78 43 61  th argument, xCa
38fd0 6c 6c 62 61 63 6b 2c 20 69 73 20 61 20 70 6f 69  llback, is a poi
38fe0 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6c 6c  nter to the coll
38ff0 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a  ating function..
39000 2a 2a 20 5e 4d 75 6c 74 69 70 6c 65 20 63 6f 6c  ** ^Multiple col
39010 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73  lating functions
39020 20 63 61 6e 20 62 65 20 72 65 67 69 73 74 65 72   can be register
39030 65 64 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  ed using the sam
39040 65 20 6e 61 6d 65 20 62 75 74 0a 2a 2a 20 77 69  e name but.** wi
39050 74 68 20 64 69 66 66 65 72 65 6e 74 20 65 54 65  th different eTe
39060 78 74 52 65 70 20 70 61 72 61 6d 65 74 65 72 73  xtRep parameters
39070 20 61 6e 64 20 53 51 4c 69 74 65 20 77 69 6c 6c   and SQLite will
39080 20 75 73 65 20 77 68 69 63 68 65 76 65 72 0a 2a   use whichever.*
39090 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69  * function requi
390a0 72 65 73 20 74 68 65 20 6c 65 61 73 74 20 61 6d  res the least am
390b0 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 74 72 61  ount of data tra
390c0 6e 73 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20  nsformation..** 
390d0 5e 49 66 20 74 68 65 20 78 43 61 6c 6c 62 61 63  ^If the xCallbac
390e0 6b 20 61 72 67 75 6d 65 6e 74 20 69 73 20 4e 55  k argument is NU
390f0 4c 4c 20 74 68 65 6e 20 74 68 65 20 63 6f 6c 6c  LL then the coll
39100 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69  ating function i
39110 73 0a 2a 2a 20 64 65 6c 65 74 65 64 2e 20 20 5e  s.** deleted.  ^
39120 57 68 65 6e 20 61 6c 6c 20 63 6f 6c 6c 61 74 69  When all collati
39130 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 68 61 76  ng functions hav
39140 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6e 61 6d  ing the same nam
39150 65 20 61 72 65 20 64 65 6c 65 74 65 64 2c 0a 2a  e are deleted,.*
39160 2a 20 74 68 61 74 20 63 6f 6c 6c 61 74 69 6f 6e  * that collation
39170 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 75 73   is no longer us
39180 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65  able..**.** ^The
39190 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74   collating funct
391a0 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  ion callback is 
391b0 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 61 20 63  invoked with a c
391c0 6f 70 79 20 6f 66 20 74 68 65 20 70 41 72 67 20  opy of the pArg 
391d0 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  .** application 
391e0 64 61 74 61 20 70 6f 69 6e 74 65 72 20 61 6e 64  data pointer and
391f0 20 77 69 74 68 20 74 77 6f 20 73 74 72 69 6e 67   with two string
39200 73 20 69 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e  s in the encodin
39210 67 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62  g specified.** b
39220 79 20 74 68 65 20 65 54 65 78 74 52 65 70 20 61  y the eTextRep a
39230 72 67 75 6d 65 6e 74 2e 20 20 54 68 65 20 63 6f  rgument.  The co
39240 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
39250 20 6d 75 73 74 20 72 65 74 75 72 6e 20 61 6e 0a   must return an.
39260 2a 2a 20 69 6e 74 65 67 65 72 20 74 68 61 74 20  ** integer that 
39270 69 73 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72  is negative, zer
39280 6f 2c 20 6f 72 20 70 6f 73 69 74 69 76 65 0a 2a  o, or positive.*
39290 2a 20 69 66 20 74 68 65 20 66 69 72 73 74 20 73  * if the first s
392a0 74 72 69 6e 67 20 69 73 20 6c 65 73 73 20 74 68  tring is less th
392b0 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72  an, equal to, or
392c0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
392d0 65 20 73 65 63 6f 6e 64 2c 0a 2a 2a 20 72 65 73  e second,.** res
392e0 70 65 63 74 69 76 65 6c 79 2e 20 20 41 20 63 6f  pectively.  A co
392f0 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
39300 20 6d 75 73 74 20 61 6c 77 61 79 73 20 72 65 74   must always ret
39310 75 72 6e 20 74 68 65 20 73 61 6d 65 20 61 6e 73  urn the same ans
39320 77 65 72 0a 2a 2a 20 67 69 76 65 6e 20 74 68 65  wer.** given the
39330 20 73 61 6d 65 20 69 6e 70 75 74 73 2e 20 20 49   same inputs.  I
39340 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63 6f  f two or more co
39350 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
39360 73 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64  s are registered
39370 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61 6d 65 20  .** to the same 
39380 63 6f 6c 6c 61 74 69 6f 6e 20 6e 61 6d 65 20 28  collation name (
39390 75 73 69 6e 67 20 64 69 66 66 65 72 65 6e 74 20  using different 
393a0 65 54 65 78 74 52 65 70 20 76 61 6c 75 65 73 29  eTextRep values)
393b0 20 74 68 65 6e 20 61 6c 6c 0a 2a 2a 20 6d 75 73   then all.** mus
393c0 74 20 67 69 76 65 20 61 6e 20 65 71 75 69 76 61  t give an equiva
393d0 6c 65 6e 74 20 61 6e 73 77 65 72 20 77 68 65 6e  lent answer when
393e0 20 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 65 71   invoked with eq
393f0 75 69 76 61 6c 65 6e 74 20 73 74 72 69 6e 67 73  uivalent strings
39400 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69  ..** The collati
39410 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74  ng function must
39420 20 6f 62 65 79 20 74 68 65 20 66 6f 6c 6c 6f 77   obey the follow
39430 69 6e 67 20 70 72 6f 70 65 72 74 69 65 73 20 66  ing properties f
39440 6f 72 20 61 6c 6c 0a 2a 2a 20 73 74 72 69 6e 67  or all.** string
39450 73 20 41 2c 20 42 2c 20 61 6e 64 20 43 3a 0a 2a  s A, B, and C:.*
39460 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ol>.** <li
39470 3e 20 49 66 20 41 3d 3d 42 20 74 68 65 6e 20 42  > If A==B then B
39480 3d 3d 41 2e 0a 2a 2a 20 3c 6c 69 3e 20 49 66 20  ==A..** <li> If 
39490 41 3d 3d 42 20 61 6e 64 20 42 3d 3d 43 20 74 68  A==B and B==C th
394a0 65 6e 20 41 3d 3d 43 2e 0a 2a 2a 20 3c 6c 69 3e  en A==C..** <li>
394b0 20 49 66 20 41 26 6c 74 3b 42 20 54 48 45 4e 20   If A&lt;B THEN 
394c0 42 26 67 74 3b 41 2e 0a 2a 2a 20 3c 6c 69 3e 20  B&gt;A..** <li> 
394d0 49 66 20 41 26 6c 74 3b 42 20 61 6e 64 20 42 26  If A&lt;B and B&
394e0 6c 74 3b 43 20 74 68 65 6e 20 41 26 6c 74 3b 43  lt;C then A&lt;C
394f0 2e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a  ..** </ol>.**.**
39500 20 49 66 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20   If a collating 
39510 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 73 20 61  function fails a
39520 6e 79 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20  ny of the above 
39530 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 6e 64 20  constraints and 
39540 74 68 61 74 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e  that.** collatin
39550 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 20 72  g function is  r
39560 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 75 73  egistered and us
39570 65 64 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68  ed, then the beh
39580 61 76 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 0a  avior of SQLite.
39590 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  ** is undefined.
395a0 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69  .**.** ^The sqli
395b0 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
395c0 74 69 6f 6e 5f 76 32 28 29 20 77 6f 72 6b 73 20  tion_v2() works 
395d0 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f 63 72 65  like sqlite3_cre
395e0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 0a  ate_collation().
395f0 2a 2a 20 77 69 74 68 20 74 68 65 20 61 64 64 69  ** with the addi
39600 74 69 6f 6e 20 74 68 61 74 20 74 68 65 20 78 44  tion that the xD
39610 65 73 74 72 6f 79 20 63 61 6c 6c 62 61 63 6b 20  estroy callback 
39620 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 70 41  is invoked on pA
39630 72 67 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 63  rg when.** the c
39640 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f  ollating functio
39650 6e 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a  n is deleted..**
39660 20 5e 43 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63   ^Collating func
39670 74 69 6f 6e 73 20 61 72 65 20 64 65 6c 65 74 65  tions are delete
39680 64 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20  d when they are 
39690 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 6c 61  overridden by la
396a0 74 65 72 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20  ter.** calls to 
396b0 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 72  the collation cr
396c0 65 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73  eation functions
396d0 20 6f 72 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20   or when the.** 
396e0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
396f0 74 69 6f 6e 5d 20 69 73 20 63 6c 6f 73 65 64 20  tion] is closed 
39700 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63  using [sqlite3_c
39710 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 5e  lose()]..**.** ^
39720 54 68 65 20 78 44 65 73 74 72 6f 79 20 63 61 6c  The xDestroy cal
39730 6c 62 61 63 6b 20 69 73 20 3c 75 3e 6e 6f 74 3c  lback is <u>not<
39740 2f 75 3e 20 63 61 6c 6c 65 64 20 69 66 20 74 68  /u> called if th
39750 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 72  e .** sqlite3_cr
39760 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
39770 32 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 61 69  2() function fai
39780 6c 73 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e  ls.  Application
39790 73 20 74 68 61 74 20 69 6e 76 6f 6b 65 0a 2a 2a  s that invoke.**
397a0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
397b0 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 20 77  collation_v2() w
397c0 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 78  ith a non-NULL x
397d0 44 65 73 74 72 6f 79 20 61 72 67 75 6d 65 6e 74  Destroy argument
397e0 20 73 68 6f 75 6c 64 20 0a 2a 2a 20 63 68 65 63   should .** chec
397f0 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64  k the return cod
39800 65 20 61 6e 64 20 64 69 73 70 6f 73 65 20 6f 66  e and dispose of
39810 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
39820 20 64 61 74 61 20 70 6f 69 6e 74 65 72 0a 2a 2a   data pointer.**
39830 20 74 68 65 6d 73 65 6c 76 65 73 20 72 61 74 68   themselves rath
39840 65 72 20 74 68 61 6e 20 65 78 70 65 63 74 69 6e  er than expectin
39850 67 20 53 51 4c 69 74 65 20 74 6f 20 64 65 61 6c  g SQLite to deal
39860 20 77 69 74 68 20 69 74 20 66 6f 72 20 74 68 65   with it for the
39870 6d 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 69  m..** This is di
39880 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 65 76 65  fferent from eve
39890 72 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20  ry other SQLite 
398a0 69 6e 74 65 72 66 61 63 65 2e 20 20 54 68 65 20  interface.  The 
398b0 69 6e 63 6f 6e 73 69 73 74 65 6e 63 79 20 0a 2a  inconsistency .*
398c0 2a 20 69 73 20 75 6e 66 6f 72 74 75 6e 61 74 65  * is unfortunate
398d0 20 62 75 74 20 63 61 6e 6e 6f 74 20 62 65 20 63   but cannot be c
398e0 68 61 6e 67 65 64 20 77 69 74 68 6f 75 74 20 62  hanged without b
398f0 72 65 61 6b 69 6e 67 20 62 61 63 6b 77 61 72 64  reaking backward
39900 73 20 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c  s .** compatibil
39910 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ity..**.** See a
39920 6c 73 6f 3a 20 20 5b 73 71 6c 69 74 65 33 5f 63  lso:  [sqlite3_c
39930 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28  ollation_needed(
39940 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
39950 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
39960 31 36 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  16()]..*/.SQLITE
39970 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
39980 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
39990 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a  n(.  sqlite3*, .
399a0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
399b0 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78  ame, .  int eTex
399c0 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 20 2a 70  tRep, .  void *p
399d0 41 72 67 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d  Arg,.  int(*xCom
399e0 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  pare)(void*,int,
399f0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
39a00 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a  const void*).);.
39a10 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
39a20 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
39a30 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a 20 20 73 71  llation_v2(.  sq
39a40 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74  lite3*, .  const
39a50 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20   char *zName, . 
39a60 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a   int eTextRep, .
39a70 20 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20    void *pArg,.  
39a80 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76  int(*xCompare)(v
39a90 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
39aa0 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
39ab0 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78  oid*),.  void(*x
39ac0 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a  Destroy)(void*).
39ad0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
39ae0 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
39af0 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20  _collation16(.  
39b00 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e  sqlite3*, .  con
39b10 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a  st void *zName,.
39b20 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20    int eTextRep, 
39b30 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20  .  void *pArg,. 
39b40 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28   int(*xCompare)(
39b50 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
39b60 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
39b70 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  void*).);../*.**
39b80 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6c 6c 61   CAPI3REF: Colla
39b90 74 69 6f 6e 20 4e 65 65 64 65 64 20 43 61 6c 6c  tion Needed Call
39ba0 62 61 63 6b 73 0a 2a 2a 0a 2a 2a 20 5e 54 6f 20  backs.**.** ^To 
39bb0 61 76 6f 69 64 20 68 61 76 69 6e 67 20 74 6f 20  avoid having to 
39bc0 72 65 67 69 73 74 65 72 20 61 6c 6c 20 63 6f 6c  register all col
39bd0 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73  lation sequences
39be0 20 62 65 66 6f 72 65 20 61 20 64 61 74 61 62 61   before a databa
39bf0 73 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65  se.** can be use
39c00 64 2c 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c  d, a single call
39c10 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 6d 61  back function ma
39c20 79 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20  y be registered 
39c30 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b 64 61 74  with the.** [dat
39c40 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
39c50 5d 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20  ] to be invoked 
39c60 77 68 65 6e 65 76 65 72 20 61 6e 20 75 6e 64 65  whenever an unde
39c70 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 0a  fined collation.
39c80 2a 2a 20 73 65 71 75 65 6e 63 65 20 69 73 20 72  ** sequence is r
39c90 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e  equired..**.** ^
39ca0 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  If the function 
39cb0 69 73 20 72 65 67 69 73 74 65 72 65 64 20 75 73  is registered us
39cc0 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ing the sqlite3_
39cd0 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
39ce0 28 29 20 41 50 49 2c 0a 2a 2a 20 74 68 65 6e 20  () API,.** then 
39cf0 69 74 20 69 73 20 70 61 73 73 65 64 20 74 68 65  it is passed the
39d00 20 6e 61 6d 65 73 20 6f 66 20 75 6e 64 65 66 69   names of undefi
39d10 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ned collation se
39d20 71 75 65 6e 63 65 73 20 61 73 20 73 74 72 69 6e  quences as strin
39d30 67 73 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e  gs.** encoded in
39d40 20 55 54 46 2d 38 2e 20 5e 49 66 20 73 71 6c 69   UTF-8. ^If sqli
39d50 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  te3_collation_ne
39d60 65 64 65 64 31 36 28 29 20 69 73 20 75 73 65 64  eded16() is used
39d70 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 61  ,.** the names a
39d80 72 65 20 70 61 73 73 65 64 20 61 73 20 55 54 46  re passed as UTF
39d90 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 20 6e  -16 in machine n
39da0 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
39db0 2e 0a 2a 2a 20 5e 41 20 63 61 6c 6c 20 74 6f 20  ..** ^A call to 
39dc0 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20  either function 
39dd0 72 65 70 6c 61 63 65 73 20 74 68 65 20 65 78 69  replaces the exi
39de0 73 74 69 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e 2d  sting collation-
39df0 6e 65 65 64 65 64 20 63 61 6c 6c 62 61 63 6b 2e  needed callback.
39e00 0a 2a 2a 0a 2a 2a 20 5e 28 57 68 65 6e 20 74 68  .**.** ^(When th
39e10 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e  e callback is in
39e20 76 6f 6b 65 64 2c 20 74 68 65 20 66 69 72 73 74  voked, the first
39e30 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64   argument passed
39e40 20 69 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66   is a copy.** of
39e50 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
39e60 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f  ment to sqlite3_
39e70 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
39e80 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33  () or.** sqlite3
39e90 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
39ea0 64 31 36 28 29 2e 20 20 54 68 65 20 73 65 63 6f  d16().  The seco
39eb0 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  nd argument is t
39ec0 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  he database.** c
39ed0 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20  onnection.  The 
39ee0 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69  third argument i
39ef0 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45  s one of [SQLITE
39f00 5f 55 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f  _UTF8], [SQLITE_
39f10 55 54 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20  UTF16BE],.** or 
39f20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d  [SQLITE_UTF16LE]
39f30 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65  , indicating the
39f40 20 6d 6f 73 74 20 64 65 73 69 72 61 62 6c 65 20   most desirable 
39f50 66 6f 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c  form of the coll
39f60 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63  ation.** sequenc
39f70 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69  e function requi
39f80 72 65 64 2e 20 20 54 68 65 20 66 6f 75 72 74 68  red.  The fourth
39f90 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
39fa0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a  e name of the.**
39fb0 20 72 65 71 75 69 72 65 64 20 63 6f 6c 6c 61 74   required collat
39fc0 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 29 5e 0a  ion sequence.)^.
39fd0 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61  **.** The callba
39fe0 63 6b 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  ck function shou
39ff0 6c 64 20 72 65 67 69 73 74 65 72 20 74 68 65 20  ld register the 
3a000 64 65 73 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f  desired collatio
3a010 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69  n using.** [sqli
3a020 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
3a030 74 69 6f 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65  tion()], [sqlite
3a040 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
3a050 6f 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b  on16()], or.** [
3a060 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
3a070 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a  ollation_v2()]..
3a080 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
3a090 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  t sqlite3_collat
3a0a0 69 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 71  ion_needed(.  sq
3a0b0 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a  lite3*, .  void*
3a0c0 2c 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69  , .  void(*)(voi
3a0d0 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20  d*,sqlite3*,int 
3a0e0 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63  eTextRep,const c
3a0f0 68 61 72 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f  har*).);.SQLITE_
3a100 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
3a110 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
3a120 31 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20  16(.  sqlite3*, 
3a130 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64  .  void*,.  void
3a140 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  (*)(void*,sqlite
3a150 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
3a160 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a  const void*).);.
3a170 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48  .#ifdef SQLITE_H
3a180 41 53 5f 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53  AS_CODEC./*.** S
3a190 70 65 63 69 66 79 20 74 68 65 20 6b 65 79 20 66  pecify the key f
3a1a0 6f 72 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20  or an encrypted 
3a1b0 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20  database.  This 
3a1c0 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62  routine should b
3a1d0 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 69 67 68  e.** called righ
3a1e0 74 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f  t after sqlite3_
3a1f0 6f 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  open()..**.** Th
3a200 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d  e code to implem
3a210 65 6e 74 20 74 68 69 73 20 41 50 49 20 69 73 20  ent this API is 
3a220 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e  not available in
3a230 20 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65   the public rele
3a240 61 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65  ase.** of SQLite
3a250 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3a260 69 6e 74 20 73 71 6c 69 74 65 33 5f 6b 65 79 28  int sqlite3_key(
3a270 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
3a280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a290 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f    /* Database to
3a2a0 20 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20   be rekeyed */. 
3a2b0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
3a2c0 79 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20  y, int nKey     
3a2d0 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 29 3b  /* The key */.);
3a2e0 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
3a2f0 68 65 20 6b 65 79 20 6f 6e 20 61 6e 20 6f 70 65  he key on an ope
3a300 6e 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20  n database.  If 
3a310 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61  the current data
3a320 62 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65  base is not.** e
3a330 6e 63 72 79 70 74 65 64 2c 20 74 68 69 73 20 72  ncrypted, this r
3a340 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 6e 63 72  outine will encr
3a350 79 70 74 20 69 74 2e 20 20 49 66 20 70 4e 65 77  ypt it.  If pNew
3a360 3d 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20  ==0 or nNew==0, 
3a370 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
3a380 69 73 20 64 65 63 72 79 70 74 65 64 2e 0a 2a 2a  is decrypted..**
3a390 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20  .** The code to 
3a3a0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41  implement this A
3a3b0 50 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61  PI is not availa
3a3c0 62 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69  ble in the publi
3a3d0 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20  c release.** of 
3a3e0 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54  SQLite..*/.SQLIT
3a3f0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3a400 33 5f 72 65 6b 65 79 28 0a 20 20 73 71 6c 69 74  3_rekey(.  sqlit
3a410 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
3a420 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
3a430 61 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65  abase to be reke
3a440 79 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  yed */.  const v
3a450 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  oid *pKey, int n
3a460 4b 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6e  Key     /* The n
3a470 65 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a  ew key */.);../*
3a480 0a 2a 2a 20 53 70 65 63 69 66 79 20 74 68 65 20  .** Specify the 
3a490 61 63 74 69 76 61 74 69 6f 6e 20 6b 65 79 20 66  activation key f
3a4a0 6f 72 20 61 20 53 45 45 20 64 61 74 61 62 61 73  or a SEE databas
3a4b0 65 2e 20 20 55 6e 6c 65 73 73 20 0a 2a 2a 20 61  e.  Unless .** a
3a4c0 63 74 69 76 61 74 65 64 2c 20 6e 6f 6e 65 20 6f  ctivated, none o
3a4d0 66 20 74 68 65 20 53 45 45 20 72 6f 75 74 69 6e  f the SEE routin
3a4e0 65 73 20 77 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f  es will work..*/
3a4f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
3a500 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61 74   sqlite3_activat
3a510 65 5f 73 65 65 28 0a 20 20 63 6f 6e 73 74 20 63  e_see(.  const c
3a520 68 61 72 20 2a 7a 50 61 73 73 50 68 72 61 73 65  har *zPassPhrase
3a530 20 20 20 20 20 20 20 20 2f 2a 20 41 63 74 69 76          /* Activ
3a540 61 74 69 6f 6e 20 70 68 72 61 73 65 20 2a 2f 0a  ation phrase */.
3a550 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65  );.#endif..#ifde
3a560 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
3a570 43 45 52 4f 44 0a 2f 2a 0a 2a 2a 20 53 70 65 63  CEROD./*.** Spec
3a580 69 66 79 20 74 68 65 20 61 63 74 69 76 61 74 69  ify the activati
3a590 6f 6e 20 6b 65 79 20 66 6f 72 20 61 20 43 45 52  on key for a CER
3a5a0 4f 44 20 64 61 74 61 62 61 73 65 2e 20 20 55 6e  OD database.  Un
3a5b0 6c 65 73 73 20 0a 2a 2a 20 61 63 74 69 76 61 74  less .** activat
3a5c0 65 64 2c 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20  ed, none of the 
3a5d0 43 45 52 4f 44 20 72 6f 75 74 69 6e 65 73 20 77  CEROD routines w
3a5e0 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f 0a 53 51 4c  ill work..*/.SQL
3a5f0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
3a600 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 63 65  ite3_activate_ce
3a610 72 6f 64 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  rod(.  const cha
3a620 72 20 2a 7a 50 61 73 73 50 68 72 61 73 65 20 20  r *zPassPhrase  
3a630 20 20 20 20 20 20 2f 2a 20 41 63 74 69 76 61 74        /* Activat
3a640 69 6f 6e 20 70 68 72 61 73 65 20 2a 2f 0a 29 3b  ion phrase */.);
3a650 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
3a660 41 50 49 33 52 45 46 3a 20 53 75 73 70 65 6e 64  API3REF: Suspend
3a670 20 45 78 65 63 75 74 69 6f 6e 20 46 6f 72 20 41   Execution For A
3a680 20 53 68 6f 72 74 20 54 69 6d 65 0a 2a 2a 0a 2a   Short Time.**.*
3a690 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6c  * The sqlite3_sl
3a6a0 65 65 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 63  eep() function c
3a6b0 61 75 73 65 73 20 74 68 65 20 63 75 72 72 65 6e  auses the curren
3a6c0 74 20 74 68 72 65 61 64 20 74 6f 20 73 75 73 70  t thread to susp
3a6d0 65 6e 64 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a  end execution.**
3a6e0 20 66 6f 72 20 61 74 20 6c 65 61 73 74 20 61 20   for at least a 
3a6f0 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73  number of millis
3a700 65 63 6f 6e 64 73 20 73 70 65 63 69 66 69 65 64  econds specified
3a710 20 69 6e 20 69 74 73 20 70 61 72 61 6d 65 74 65   in its paramete
3a720 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  r..**.** If the 
3a730 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
3a740 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72   does not suppor
3a750 74 20 73 6c 65 65 70 20 72 65 71 75 65 73 74 73  t sleep requests
3a760 20 77 69 74 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65   with.** millise
3a770 63 6f 6e 64 20 74 69 6d 65 20 72 65 73 6f 6c 75  cond time resolu
3a780 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74  tion, then the t
3a790 69 6d 65 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e  ime will be roun
3a7a0 64 65 64 20 75 70 20 74 6f 0a 2a 2a 20 74 68 65  ded up to.** the
3a7b0 20 6e 65 61 72 65 73 74 20 73 65 63 6f 6e 64 2e   nearest second.
3a7c0 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d   The number of m
3a7d0 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73  illiseconds of s
3a7e0 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a  leep actually.**
3a7f0 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20   requested from 
3a800 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
3a810 73 74 65 6d 20 69 73 20 72 65 74 75 72 6e 65 64  stem is returned
3a820 2e 0a 2a 2a 0a 2a 2a 20 5e 53 51 4c 69 74 65 20  ..**.** ^SQLite 
3a830 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 69 73 20  implements this 
3a840 69 6e 74 65 72 66 61 63 65 20 62 79 20 63 61 6c  interface by cal
3a850 6c 69 6e 67 20 74 68 65 20 78 53 6c 65 65 70 28  ling the xSleep(
3a860 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74  ).** method of t
3a870 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69  he default [sqli
3a880 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e  te3_vfs] object.
3a890 20 20 49 66 20 74 68 65 20 78 53 6c 65 65 70 28    If the xSleep(
3a8a0 29 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74  ) method.** of t
3a8b0 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69  he default VFS i
3a8c0 73 20 6e 6f 74 20 69 6d 70 6c 65 6d 65 6e 74 65  s not implemente
3a8d0 64 20 63 6f 72 72 65 63 74 6c 79 2c 20 6f 72 20  d correctly, or 
3a8e0 6e 6f 74 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  not implemented 
3a8f0 61 74 0a 2a 2a 20 61 6c 6c 2c 20 74 68 65 6e 20  at.** all, then 
3a900 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20  the behavior of 
3a910 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 29 20  sqlite3_sleep() 
3a920 6d 61 79 20 64 65 76 69 61 74 65 20 66 72 6f 6d  may deviate from
3a930 20 74 68 65 20 64 65 73 63 72 69 70 74 69 6f 6e   the description
3a940 0a 2a 2a 20 69 6e 20 74 68 65 20 70 72 65 76 69  .** in the previ
3a950 6f 75 73 20 70 61 72 61 67 72 61 70 68 73 2e 0a  ous paragraphs..
3a960 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
3a970 74 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28  t sqlite3_sleep(
3a980 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  int);../*.** CAP
3a990 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 54  I3REF: Name Of T
3a9a0 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c 64 69 6e  he Folder Holdin
3a9b0 67 20 54 65 6d 70 6f 72 61 72 79 20 46 69 6c 65  g Temporary File
3a9c0 73 0a 2a 2a 0a 2a 2a 20 5e 28 49 66 20 74 68 69  s.**.** ^(If thi
3a9d0 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  s global variabl
3a9e0 65 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69  e is made to poi
3a9f0 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77  nt to a string w
3aa00 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 6e  hich is.** the n
3aa10 61 6d 65 20 6f 66 20 61 20 66 6f 6c 64 65 72 20  ame of a folder 
3aa20 28 61 2e 6b 2e 61 2e 20 64 69 72 65 63 74 6f 72  (a.k.a. director
3aa30 79 29 2c 20 74 68 65 6e 20 61 6c 6c 20 74 65 6d  y), then all tem
3aa40 70 6f 72 61 72 79 20 66 69 6c 65 73 0a 2a 2a 20  porary files.** 
3aa50 63 72 65 61 74 65 64 20 62 79 20 53 51 4c 69 74  created by SQLit
3aa60 65 20 77 68 65 6e 20 75 73 69 6e 67 20 61 20 62  e when using a b
3aa70 75 69 6c 74 2d 69 6e 20 5b 73 71 6c 69 74 65 33  uilt-in [sqlite3
3aa80 5f 76 66 73 20 7c 20 56 46 53 5d 0a 2a 2a 20 77  _vfs | VFS].** w
3aa90 69 6c 6c 20 62 65 20 70 6c 61 63 65 64 20 69 6e  ill be placed in
3aaa0 20 74 68 61 74 20 64 69 72 65 63 74 6f 72 79 2e   that directory.
3aab0 29 5e 20 20 5e 49 66 20 74 68 69 73 20 76 61 72  )^  ^If this var
3aac0 69 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 4e 55  iable.** is a NU
3aad0 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e  LL pointer, then
3aae0 20 53 51 4c 69 74 65 20 70 65 72 66 6f 72 6d 73   SQLite performs
3aaf0 20 61 20 73 65 61 72 63 68 20 66 6f 72 20 61 6e   a search for an
3ab00 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20   appropriate.** 
3ab10 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64  temporary file d
3ab20 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20  irectory..**.** 
3ab30 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74  It is not safe t
3ab40 6f 20 72 65 61 64 20 6f 72 20 6d 6f 64 69 66 79  o read or modify
3ab50 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69   this variable i
3ab60 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 0a  n more than one.
3ab70 2a 2a 20 74 68 72 65 61 64 20 61 74 20 61 20 74  ** thread at a t
3ab80 69 6d 65 2e 20 20 49 74 20 69 73 20 6e 6f 74 20  ime.  It is not 
3ab90 73 61 66 65 20 74 6f 20 72 65 61 64 20 6f 72 20  safe to read or 
3aba0 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61 72 69  modify this vari
3abb0 61 62 6c 65 0a 2a 2a 20 69 66 20 61 20 5b 64 61  able.** if a [da
3abc0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3abd0 6e 5d 20 69 73 20 62 65 69 6e 67 20 75 73 65 64  n] is being used
3abe0 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
3abf0 65 20 69 6e 20 61 20 73 65 70 61 72 61 74 65 0a  e in a separate.
3ac00 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 20 49 74  ** thread..** It
3ac10 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 68 61   is intended tha
3ac20 74 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20  t this variable 
3ac30 62 65 20 73 65 74 20 6f 6e 63 65 0a 2a 2a 20 61  be set once.** a
3ac40 73 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73  s part of proces
3ac50 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  s initialization
3ac60 20 61 6e 64 20 62 65 66 6f 72 65 20 61 6e 79 20   and before any 
3ac70 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65  SQLite interface
3ac80 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 68 61 76  .** routines hav
3ac90 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 61 6e  e been called an
3aca0 64 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69  d that this vari
3acb0 61 62 6c 65 20 72 65 6d 61 69 6e 20 75 6e 63 68  able remain unch
3acc0 61 6e 67 65 64 0a 2a 2a 20 74 68 65 72 65 61 66  anged.** thereaf
3acd0 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  ter..**.** ^The 
3ace0 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65  [temp_store_dire
3acf0 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 6d 61  ctory pragma] ma
3ad00 79 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61  y modify this va
3ad10 72 69 61 62 6c 65 20 61 6e 64 20 63 61 75 73 65  riable and cause
3ad20 0a 2a 2a 20 69 74 20 74 6f 20 70 6f 69 6e 74 20  .** it to point 
3ad30 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
3ad40 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
3ad50 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 5e 46 75 72 74  _malloc].  ^Furt
3ad60 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74 68 65 20  hermore,.** the 
3ad70 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65  [temp_store_dire
3ad80 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 61 6c  ctory pragma] al
3ad90 77 61 79 73 20 61 73 73 75 6d 65 73 20 74 68 61  ways assumes tha
3ada0 74 20 61 6e 79 20 73 74 72 69 6e 67 0a 2a 2a 20  t any string.** 
3adb0 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61 62  that this variab
3adc0 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20  le points to is 
3add0 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f  held in memory o
3ade0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 0a 2a 2a  btained from .**
3adf0 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
3ae00 5d 20 61 6e 64 20 74 68 65 20 70 72 61 67 6d 61  ] and the pragma
3ae10 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 6f 20   may attempt to 
3ae20 66 72 65 65 20 74 68 61 74 20 6d 65 6d 6f 72 79  free that memory
3ae30 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  .** using [sqlit
3ae40 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20 48 65 6e  e3_free]..** Hen
3ae50 63 65 2c 20 69 66 20 74 68 69 73 20 76 61 72 69  ce, if this vari
3ae60 61 62 6c 65 20 69 73 20 6d 6f 64 69 66 69 65 64  able is modified
3ae70 20 64 69 72 65 63 74 6c 79 2c 20 65 69 74 68 65   directly, eithe
3ae80 72 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a 2a  r it should be.*
3ae90 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f 72 20 6d  * made NULL or m
3aea0 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  ade to point to 
3aeb0 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
3aec0 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61  from [sqlite3_ma
3aed0 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65 6c 73 65  lloc].** or else
3aee0 20 74 68 65 20 75 73 65 20 6f 66 20 74 68 65 20   the use of the 
3aef0 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65  [temp_store_dire
3af00 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 73 68  ctory pragma] sh
3af10 6f 75 6c 64 20 62 65 20 61 76 6f 69 64 65 64 2e  ould be avoided.
3af20 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 65 20 74  .**.** <b>Note t
3af30 6f 20 57 69 6e 64 6f 77 73 20 52 75 6e 74 69 6d  o Windows Runtim
3af40 65 20 75 73 65 72 73 3a 3c 2f 62 3e 20 20 54 68  e users:</b>  Th
3af50 65 20 74 65 6d 70 6f 72 61 72 79 20 64 69 72 65  e temporary dire
3af60 63 74 6f 72 79 20 6d 75 73 74 20 62 65 20 73 65  ctory must be se
3af70 74 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 63 61  t.** prior to ca
3af80 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f  lling [sqlite3_o
3af90 70 65 6e 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  pen] or [sqlite3
3afa0 5f 6f 70 65 6e 5f 76 32 5d 2e 20 20 4f 74 68 65  _open_v2].  Othe
3afb0 72 77 69 73 65 2c 20 76 61 72 69 6f 75 73 0a 2a  rwise, various.*
3afc0 2a 20 66 65 61 74 75 72 65 73 20 74 68 61 74 20  * features that 
3afd0 72 65 71 75 69 72 65 20 74 68 65 20 75 73 65 20  require the use 
3afe0 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  of temporary fil
3aff0 65 73 20 6d 61 79 20 66 61 69 6c 2e 20 20 48 65  es may fail.  He
3b000 72 65 20 69 73 20 61 6e 0a 2a 2a 20 65 78 61 6d  re is an.** exam
3b010 70 6c 65 20 6f 66 20 68 6f 77 20 74 6f 20 64 6f  ple of how to do
3b020 20 74 68 69 73 20 75 73 69 6e 67 20 43 2b 2b 20   this using C++ 
3b030 77 69 74 68 20 74 68 65 20 57 69 6e 64 6f 77 73  with the Windows
3b040 20 52 75 6e 74 69 6d 65 3a 0a 2a 2a 0a 2a 2a 20   Runtime:.**.** 
3b050 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65  <blockquote><pre
3b060 3e 0a 2a 2a 20 4c 50 43 57 53 54 52 20 7a 50 61  >.** LPCWSTR zPa
3b070 74 68 20 3d 20 57 69 6e 64 6f 77 73 3a 3a 53 74  th = Windows::St
3b080 6f 72 61 67 65 3a 3a 41 70 70 6c 69 63 61 74 69  orage::Applicati
3b090 6f 6e 44 61 74 61 3a 3a 43 75 72 72 65 6e 74 2d  onData::Current-
3b0a0 3e 0a 2a 2a 20 26 6e 62 73 70 3b 20 20 20 20 20  >.** &nbsp;     
3b0b0 54 65 6d 70 6f 72 61 72 79 46 6f 6c 64 65 72 2d  TemporaryFolder-
3b0c0 3e 50 61 74 68 2d 3e 44 61 74 61 28 29 3b 0a 2a  >Path->Data();.*
3b0d0 2a 20 63 68 61 72 20 7a 50 61 74 68 42 75 66 26  * char zPathBuf&
3b0e0 23 39 31 3b 4d 41 58 5f 50 41 54 48 20 2b 20 31  #91;MAX_PATH + 1
3b0f0 26 23 39 33 3b 3b 0a 2a 2a 20 6d 65 6d 73 65 74  &#93;;.** memset
3b100 28 7a 50 61 74 68 42 75 66 2c 20 30 2c 20 73 69  (zPathBuf, 0, si
3b110 7a 65 6f 66 28 7a 50 61 74 68 42 75 66 29 29 3b  zeof(zPathBuf));
3b120 0a 2a 2a 20 57 69 64 65 43 68 61 72 54 6f 4d 75  .** WideCharToMu
3b130 6c 74 69 42 79 74 65 28 43 50 5f 55 54 46 38 2c  ltiByte(CP_UTF8,
3b140 20 30 2c 20 7a 50 61 74 68 2c 20 2d 31 2c 20 7a   0, zPath, -1, z
3b150 50 61 74 68 42 75 66 2c 20 73 69 7a 65 6f 66 28  PathBuf, sizeof(
3b160 7a 50 61 74 68 42 75 66 29 2c 0a 2a 2a 20 26 6e  zPathBuf),.** &n
3b170 62 73 70 3b 20 20 20 20 20 4e 55 4c 4c 2c 20 4e  bsp;     NULL, N
3b180 55 4c 4c 29 3b 0a 2a 2a 20 73 71 6c 69 74 65 33  ULL);.** sqlite3
3b190 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20  _temp_directory 
3b1a0 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
3b1b0 66 28 22 25 73 22 2c 20 7a 50 61 74 68 42 75 66  f("%s", zPathBuf
3b1c0 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c  );.** </pre></bl
3b1d0 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2f 0a 53 51 4c  ockquote>.*/.SQL
3b1e0 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71  ITE_API char *sq
3b1f0 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
3b200 74 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  tory;../*.** CAP
3b210 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 54  I3REF: Name Of T
3b220 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c 64 69 6e  he Folder Holdin
3b230 67 20 44 61 74 61 62 61 73 65 20 46 69 6c 65 73  g Database Files
3b240 0a 2a 2a 0a 2a 2a 20 5e 28 49 66 20 74 68 69 73  .**.** ^(If this
3b250 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
3b260 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e   is made to poin
3b270 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77 68  t to a string wh
3b280 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 6e 61  ich is.** the na
3b290 6d 65 20 6f 66 20 61 20 66 6f 6c 64 65 72 20 28  me of a folder (
3b2a0 61 2e 6b 2e 61 2e 20 64 69 72 65 63 74 6f 72 79  a.k.a. directory
3b2b0 29 2c 20 74 68 65 6e 20 61 6c 6c 20 64 61 74 61  ), then all data
3b2c0 62 61 73 65 20 66 69 6c 65 73 0a 2a 2a 20 73 70  base files.** sp
3b2d0 65 63 69 66 69 65 64 20 77 69 74 68 20 61 20 72  ecified with a r
3b2e0 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65  elative pathname
3b2f0 20 61 6e 64 20 63 72 65 61 74 65 64 20 6f 72 20   and created or 
3b300 61 63 63 65 73 73 65 64 20 62 79 0a 2a 2a 20 53  accessed by.** S
3b310 51 4c 69 74 65 20 77 68 65 6e 20 75 73 69 6e 67  QLite when using
3b320 20 61 20 62 75 69 6c 74 2d 69 6e 20 77 69 6e 64   a built-in wind
3b330 6f 77 73 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  ows [sqlite3_vfs
3b340 20 7c 20 56 46 53 5d 20 77 69 6c 6c 20 62 65 20   | VFS] will be 
3b350 61 73 73 75 6d 65 64 0a 2a 2a 20 74 6f 20 62 65  assumed.** to be
3b360 20 72 65 6c 61 74 69 76 65 20 74 6f 20 74 68 61   relative to tha
3b370 74 20 64 69 72 65 63 74 6f 72 79 2e 29 5e 20 5e  t directory.)^ ^
3b380 49 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  If this variable
3b390 20 69 73 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f   is a NULL.** po
3b3a0 69 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69  inter, then SQLi
3b3b0 74 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  te assumes that 
3b3c0 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  all database fil
3b3d0 65 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  es specified.** 
3b3e0 77 69 74 68 20 61 20 72 65 6c 61 74 69 76 65 20  with a relative 
3b3f0 70 61 74 68 6e 61 6d 65 20 61 72 65 20 72 65 6c  pathname are rel
3b400 61 74 69 76 65 20 74 6f 20 74 68 65 20 63 75 72  ative to the cur
3b410 72 65 6e 74 20 64 69 72 65 63 74 6f 72 79 0a 2a  rent directory.*
3b420 2a 20 66 6f 72 20 74 68 65 20 70 72 6f 63 65 73  * for the proces
3b430 73 2e 20 20 4f 6e 6c 79 20 74 68 65 20 77 69 6e  s.  Only the win
3b440 64 6f 77 73 20 56 46 53 20 6d 61 6b 65 73 20 75  dows VFS makes u
3b450 73 65 20 6f 66 20 74 68 69 73 20 67 6c 6f 62 61  se of this globa
3b460 6c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 3b 20 69  l.** variable; i
3b470 74 20 69 73 20 69 67 6e 6f 72 65 64 20 62 79 20  t is ignored by 
3b480 74 68 65 20 75 6e 69 78 20 56 46 53 2e 0a 2a 2a  the unix VFS..**
3b490 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 65  .** Changing the
3b4a0 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 76   value of this v
3b4b0 61 72 69 61 62 6c 65 20 77 68 69 6c 65 20 61 20  ariable while a 
3b4c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3b4d0 69 6f 6e 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 63  ion is.** open c
3b4e0 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 61 20 63  an result in a c
3b4f0 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 2e  orrupt database.
3b500 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74  .**.** It is not
3b510 20 73 61 66 65 20 74 6f 20 72 65 61 64 20 6f 72   safe to read or
3b520 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61 72   modify this var
3b530 69 61 62 6c 65 20 69 6e 20 6d 6f 72 65 20 74 68  iable in more th
3b540 61 6e 20 6f 6e 65 0a 2a 2a 20 74 68 72 65 61 64  an one.** thread
3b550 20 61 74 20 61 20 74 69 6d 65 2e 20 20 49 74 20   at a time.  It 
3b560 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 72  is not safe to r
3b570 65 61 64 20 6f 72 20 6d 6f 64 69 66 79 20 74 68  ead or modify th
3b580 69 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69  is variable.** i
3b590 66 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f  f a [database co
3b5a0 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 65 69  nnection] is bei
3b5b0 6e 67 20 75 73 65 64 20 61 74 20 74 68 65 20 73  ng used at the s
3b5c0 61 6d 65 20 74 69 6d 65 20 69 6e 20 61 20 73 65  ame time in a se
3b5d0 70 61 72 61 74 65 0a 2a 2a 20 74 68 72 65 61 64  parate.** thread
3b5e0 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6e 74 65 6e  ..** It is inten
3b5f0 64 65 64 20 74 68 61 74 20 74 68 69 73 20 76 61  ded that this va
3b600 72 69 61 62 6c 65 20 62 65 20 73 65 74 20 6f 6e  riable be set on
3b610 63 65 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66  ce.** as part of
3b620 20 70 72 6f 63 65 73 73 20 69 6e 69 74 69 61 6c   process initial
3b630 69 7a 61 74 69 6f 6e 20 61 6e 64 20 62 65 66 6f  ization and befo
3b640 72 65 20 61 6e 79 20 53 51 4c 69 74 65 20 69 6e  re any SQLite in
3b650 74 65 72 66 61 63 65 0a 2a 2a 20 72 6f 75 74 69  terface.** routi
3b660 6e 65 73 20 68 61 76 65 20 62 65 65 6e 20 63 61  nes have been ca
3b670 6c 6c 65 64 20 61 6e 64 20 74 68 61 74 20 74 68  lled and that th
3b680 69 73 20 76 61 72 69 61 62 6c 65 20 72 65 6d 61  is variable rema
3b690 69 6e 20 75 6e 63 68 61 6e 67 65 64 0a 2a 2a 20  in unchanged.** 
3b6a0 74 68 65 72 65 61 66 74 65 72 2e 0a 2a 2a 0a 2a  thereafter..**.*
3b6b0 2a 20 5e 54 68 65 20 5b 64 61 74 61 5f 73 74 6f  * ^The [data_sto
3b6c0 72 65 5f 64 69 72 65 63 74 6f 72 79 20 70 72 61  re_directory pra
3b6d0 67 6d 61 5d 20 6d 61 79 20 6d 6f 64 69 66 79 20  gma] may modify 
3b6e0 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 61 6e  this variable an
3b6f0 64 20 63 61 75 73 65 0a 2a 2a 20 69 74 20 74 6f  d cause.** it to
3b700 20 70 6f 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79   point to memory
3b710 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b   obtained from [
3b720 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 5d 2e  sqlite3_malloc].
3b730 20 20 5e 46 75 72 74 68 65 72 6d 6f 72 65 2c 0a    ^Furthermore,.
3b740 2a 2a 20 74 68 65 20 5b 64 61 74 61 5f 73 74 6f  ** the [data_sto
3b750 72 65 5f 64 69 72 65 63 74 6f 72 79 20 70 72 61  re_directory pra
3b760 67 6d 61 5d 20 61 6c 77 61 79 73 20 61 73 73 75  gma] always assu
3b770 6d 65 73 20 74 68 61 74 20 61 6e 79 20 73 74 72  mes that any str
3b780 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74 68 69 73  ing.** that this
3b790 20 76 61 72 69 61 62 6c 65 20 70 6f 69 6e 74 73   variable points
3b7a0 20 74 6f 20 69 73 20 68 65 6c 64 20 69 6e 20 6d   to is held in m
3b7b0 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
3b7c0 72 6f 6d 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  rom .** [sqlite3
3b7d0 5f 6d 61 6c 6c 6f 63 5d 20 61 6e 64 20 74 68 65  _malloc] and the
3b7e0 20 70 72 61 67 6d 61 20 6d 61 79 20 61 74 74 65   pragma may atte
3b7f0 6d 70 74 20 74 6f 20 66 72 65 65 20 74 68 61 74  mpt to free that
3b800 20 6d 65 6d 6f 72 79 0a 2a 2a 20 75 73 69 6e 67   memory.** using
3b810 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5d 2e   [sqlite3_free].
3b820 0a 2a 2a 20 48 65 6e 63 65 2c 20 69 66 20 74 68  .** Hence, if th
3b830 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 6d  is variable is m
3b840 6f 64 69 66 69 65 64 20 64 69 72 65 63 74 6c 79  odified directly
3b850 2c 20 65 69 74 68 65 72 20 69 74 20 73 68 6f 75  , either it shou
3b860 6c 64 20 62 65 0a 2a 2a 20 6d 61 64 65 20 4e 55  ld be.** made NU
3b870 4c 4c 20 6f 72 20 6d 61 64 65 20 74 6f 20 70 6f  LL or made to po
3b880 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  int to memory ob
3b890 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c  tained from [sql
3b8a0 69 74 65 33 5f 6d 61 6c 6c 6f 63 5d 0a 2a 2a 20  ite3_malloc].** 
3b8b0 6f 72 20 65 6c 73 65 20 74 68 65 20 75 73 65 20  or else the use 
3b8c0 6f 66 20 74 68 65 20 5b 64 61 74 61 5f 73 74 6f  of the [data_sto
3b8d0 72 65 5f 64 69 72 65 63 74 6f 72 79 20 70 72 61  re_directory pra
3b8e0 67 6d 61 5d 20 73 68 6f 75 6c 64 20 62 65 20 61  gma] should be a
3b8f0 76 6f 69 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  voided..*/.SQLIT
3b900 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69  E_API char *sqli
3b910 74 65 33 5f 64 61 74 61 5f 64 69 72 65 63 74 6f  te3_data_directo
3b920 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  ry;../*.** CAPI3
3b930 52 45 46 3a 20 54 65 73 74 20 46 6f 72 20 41 75  REF: Test For Au
3b940 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f 64 65 0a 2a  to-Commit Mode.*
3b950 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 75 74  * KEYWORDS: {aut
3b960 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 7d 0a 2a 2a  ocommit mode}.**
3b970 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33  .** ^The sqlite3
3b980 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28  _get_autocommit(
3b990 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ) interface retu
3b9a0 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a  rns non-zero or.
3b9b0 2a 2a 20 7a 65 72 6f 20 69 66 20 74 68 65 20 67  ** zero if the g
3b9c0 69 76 65 6e 20 64 61 74 61 62 61 73 65 20 63 6f  iven database co
3b9d0 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 72 20 69  nnection is or i
3b9e0 73 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f 6d  s not in autocom
3b9f0 6d 69 74 20 6d 6f 64 65 2c 0a 2a 2a 20 72 65 73  mit mode,.** res
3ba00 70 65 63 74 69 76 65 6c 79 2e 20 20 5e 41 75 74  pectively.  ^Aut
3ba10 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20  ocommit mode is 
3ba20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a  on by default..*
3ba30 2a 20 5e 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  * ^Autocommit mo
3ba40 64 65 20 69 73 20 64 69 73 61 62 6c 65 64 20 62  de is disabled b
3ba50 79 20 61 20 5b 42 45 47 49 4e 5d 20 73 74 61 74  y a [BEGIN] stat
3ba60 65 6d 65 6e 74 2e 0a 2a 2a 20 5e 41 75 74 6f 63  ement..** ^Autoc
3ba70 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 72 65  ommit mode is re
3ba80 2d 65 6e 61 62 6c 65 64 20 62 79 20 61 20 5b 43  -enabled by a [C
3ba90 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c 4c 42  OMMIT] or [ROLLB
3baa0 41 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63  ACK]..**.** If c
3bab0 65 72 74 61 69 6e 20 6b 69 6e 64 73 20 6f 66 20  ertain kinds of 
3bac0 65 72 72 6f 72 73 20 6f 63 63 75 72 20 6f 6e 20  errors occur on 
3bad0 61 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68  a statement with
3bae0 69 6e 20 61 20 6d 75 6c 74 69 2d 73 74 61 74 65  in a multi-state
3baf0 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74  ment.** transact
3bb00 69 6f 6e 20 28 65 72 72 6f 72 73 20 69 6e 63 6c  ion (errors incl
3bb10 75 64 69 6e 67 20 5b 53 51 4c 49 54 45 5f 46 55  uding [SQLITE_FU
3bb20 4c 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 4f 45  LL], [SQLITE_IOE
3bb30 52 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  RR],.** [SQLITE_
3bb40 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f  NOMEM], [SQLITE_
3bb50 42 55 53 59 5d 2c 20 61 6e 64 20 5b 53 51 4c 49  BUSY], and [SQLI
3bb60 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 29 20 74  TE_INTERRUPT]) t
3bb70 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73  hen the.** trans
3bb80 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 62 65 20  action might be 
3bb90 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 6f  rolled back auto
3bba0 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 20  matically.  The 
3bbb0 6f 6e 6c 79 20 77 61 79 20 74 6f 0a 2a 2a 20 66  only way to.** f
3bbc0 69 6e 64 20 6f 75 74 20 77 68 65 74 68 65 72 20  ind out whether 
3bbd0 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63  SQLite automatic
3bbe0 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b  ally rolled back
3bbf0 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
3bc00 20 61 66 74 65 72 0a 2a 2a 20 61 6e 20 65 72 72   after.** an err
3bc10 6f 72 20 69 73 20 74 6f 20 75 73 65 20 74 68 69  or is to use thi
3bc20 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  s function..**.*
3bc30 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72  * If another thr
3bc40 65 61 64 20 63 68 61 6e 67 65 73 20 74 68 65 20  ead changes the 
3bc50 61 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 75  autocommit statu
3bc60 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  s of the databas
3bc70 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  e.** connection 
3bc80 77 68 69 6c 65 20 74 68 69 73 20 72 6f 75 74 69  while this routi
3bc90 6e 65 20 69 73 20 72 75 6e 6e 69 6e 67 2c 20 74  ne is running, t
3bca0 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76  hen the return v
3bcb0 61 6c 75 65 0a 2a 2a 20 69 73 20 75 6e 64 65 66  alue.** is undef
3bcc0 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ined..*/.SQLITE_
3bcd0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
3bce0 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 73  get_autocommit(s
3bcf0 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  qlite3*);../*.**
3bd00 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64 20   CAPI3REF: Find 
3bd10 54 68 65 20 44 61 74 61 62 61 73 65 20 48 61 6e  The Database Han
3bd20 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72 65  dle Of A Prepare
3bd30 64 20 53 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a  d Statement.**.*
3bd40 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 64  * ^The sqlite3_d
3bd50 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72 66 61  b_handle interfa
3bd60 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 5b  ce returns the [
3bd70 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3bd80 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 74  ion] handle.** t
3bd90 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65 70 61  o which a [prepa
3bda0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 62  red statement] b
3bdb0 65 6c 6f 6e 67 73 2e 20 20 5e 54 68 65 20 5b 64  elongs.  ^The [d
3bdc0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3bdd0 6f 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  on].** returned 
3bde0 62 79 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61  by sqlite3_db_ha
3bdf0 6e 64 6c 65 20 69 73 20 74 68 65 20 73 61 6d 65  ndle is the same
3be00 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
3be10 63 74 69 6f 6e 5d 0a 2a 2a 20 74 68 61 74 20 77  ction].** that w
3be20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
3be30 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20  ument.** to the 
3be40 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
3be50 5f 76 32 28 29 5d 20 63 61 6c 6c 20 28 6f 72 20  _v2()] call (or 
3be60 69 74 73 20 76 61 72 69 61 6e 74 73 29 20 74 68  its variants) th
3be70 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 0a 2a  at was used to.*
3be80 2a 20 63 72 65 61 74 65 20 74 68 65 20 73 74 61  * create the sta
3be90 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 66 69  tement in the fi
3bea0 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2f 0a 53 51  rst place..*/.SQ
3beb0 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
3bec0 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e   *sqlite3_db_han
3bed0 64 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  dle(sqlite3_stmt
3bee0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
3bef0 52 45 46 3a 20 52 65 74 75 72 6e 20 54 68 65 20  REF: Return The 
3bf00 46 69 6c 65 6e 61 6d 65 20 46 6f 72 20 41 20 44  Filename For A D
3bf10 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69  atabase Connecti
3bf20 6f 6e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71  on.**.** ^The sq
3bf30 6c 69 74 65 33 5f 64 62 5f 66 69 6c 65 6e 61 6d  lite3_db_filenam
3bf40 65 28 44 2c 4e 29 20 69 6e 74 65 72 66 61 63 65  e(D,N) interface
3bf50 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
3bf60 65 72 20 74 6f 20 61 20 66 69 6c 65 6e 61 6d 65  er to a filename
3bf70 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77  .** associated w
3bf80 69 74 68 20 64 61 74 61 62 61 73 65 20 4e 20 6f  ith database N o
3bf90 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e 20  f connection D. 
3bfa0 20 5e 54 68 65 20 6d 61 69 6e 20 64 61 74 61 62   ^The main datab
3bfb0 61 73 65 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20  ase file.** has 
3bfc0 74 68 65 20 6e 61 6d 65 20 22 6d 61 69 6e 22 2e  the name "main".
3bfd0 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f    If there is no
3bfe0 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
3bff0 73 65 20 4e 20 6f 6e 20 74 68 65 20 64 61 74 61  se N on the data
3c000 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69  base.** connecti
3c010 6f 6e 20 44 2c 20 6f 72 20 69 66 20 64 61 74 61  on D, or if data
3c020 62 61 73 65 20 4e 20 69 73 20 61 20 74 65 6d 70  base N is a temp
3c030 6f 72 61 72 79 20 6f 72 20 69 6e 2d 6d 65 6d 6f  orary or in-memo
3c040 72 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  ry database, the
3c050 6e 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  n.** a NULL poin
3c060 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ter is returned.
3c070 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 6c 65  .**.** ^The file
3c080 6e 61 6d 65 20 72 65 74 75 72 6e 65 64 20 62 79  name returned by
3c090 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
3c0a0 73 20 74 68 65 20 6f 75 74 70 75 74 20 6f 66 20  s the output of 
3c0b0 74 68 65 0a 2a 2a 20 78 46 75 6c 6c 50 61 74 68  the.** xFullPath
3c0c0 6e 61 6d 65 20 6d 65 74 68 6f 64 20 6f 66 20 74  name method of t
3c0d0 68 65 20 5b 56 46 53 5d 2e 20 20 5e 49 6e 20 6f  he [VFS].  ^In o
3c0e0 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20  ther words, the 
3c0f0 66 69 6c 65 6e 61 6d 65 0a 2a 2a 20 77 69 6c 6c  filename.** will
3c100 20 62 65 20 61 6e 20 61 62 73 6f 6c 75 74 65 20   be an absolute 
3c110 70 61 74 68 6e 61 6d 65 2c 20 65 76 65 6e 20 69  pathname, even i
3c120 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 75  f the filename u
3c130 73 65 64 0a 2a 2a 20 74 6f 20 6f 70 65 6e 20 74  sed.** to open t
3c140 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 69 67  he database orig
3c150 69 6e 61 6c 6c 79 20 77 61 73 20 61 20 55 52 49  inally was a URI
3c160 20 6f 72 20 72 65 6c 61 74 69 76 65 20 70 61 74   or relative pat
3c170 68 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  hname..*/.SQLITE
3c180 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
3c190 2a 73 71 6c 69 74 65 33 5f 64 62 5f 66 69 6c 65  *sqlite3_db_file
3c1a0 6e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a 64 62  name(sqlite3 *db
3c1b0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  , const char *zD
3c1c0 62 4e 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  bName);../*.** C
3c1d0 41 50 49 33 52 45 46 3a 20 44 65 74 65 72 6d 69  API3REF: Determi
3c1e0 6e 65 20 69 66 20 61 20 64 61 74 61 62 61 73 65  ne if a database
3c1f0 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 0a 2a 2a   is read-only.**
3c200 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33  .** ^The sqlite3
3c210 5f 64 62 5f 72 65 61 64 6f 6e 6c 79 28 44 2c 4e  _db_readonly(D,N
3c220 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ) interface retu
3c230 72 6e 73 20 31 20 69 66 20 74 68 65 20 64 61 74  rns 1 if the dat
3c240 61 62 61 73 65 20 4e 0a 2a 2a 20 6f 66 20 63 6f  abase N.** of co
3c250 6e 6e 65 63 74 69 6f 6e 20 44 20 69 73 20 72 65  nnection D is re
3c260 61 64 2d 6f 6e 6c 79 2c 20 30 20 69 66 20 69 74  ad-only, 0 if it
3c270 20 69 73 20 72 65 61 64 2f 77 72 69 74 65 2c 20   is read/write, 
3c280 6f 72 20 2d 31 20 69 66 20 4e 20 69 73 20 6e 6f  or -1 if N is no
3c290 74 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66  t.** the name of
3c2a0 20 61 20 64 61 74 61 62 61 73 65 20 6f 6e 20 63   a database on c
3c2b0 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e 0a 2a 2f 0a  onnection D..*/.
3c2c0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3c2d0 71 6c 69 74 65 33 5f 64 62 5f 72 65 61 64 6f 6e  qlite3_db_readon
3c2e0 6c 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ly(sqlite3 *db, 
3c2f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e  const char *zDbN
3c300 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ame);../*.** CAP
3c310 49 33 52 45 46 3a 20 46 69 6e 64 20 74 68 65 20  I3REF: Find the 
3c320 6e 65 78 74 20 70 72 65 70 61 72 65 64 20 73 74  next prepared st
3c330 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 5e 54  atement.**.** ^T
3c340 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 72 65  his interface re
3c350 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
3c360 74 6f 20 74 68 65 20 6e 65 78 74 20 5b 70 72 65  to the next [pre
3c370 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
3c380 20 61 66 74 65 72 0a 2a 2a 20 70 53 74 6d 74 20   after.** pStmt 
3c390 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
3c3a0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
3c3b0 6e 6e 65 63 74 69 6f 6e 5d 20 70 44 62 2e 20 20  nnection] pDb.  
3c3c0 5e 49 66 20 70 53 74 6d 74 20 69 73 20 4e 55 4c  ^If pStmt is NUL
3c3d0 4c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 69  L.** then this i
3c3e0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
3c3f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
3c400 65 20 66 69 72 73 74 20 70 72 65 70 61 72 65 64  e first prepared
3c410 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 73   statement.** as
3c420 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
3c430 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
3c440 63 74 69 6f 6e 20 70 44 62 2e 20 20 5e 49 66 20  ction pDb.  ^If 
3c450 6e 6f 20 70 72 65 70 61 72 65 64 20 73 74 61 74  no prepared stat
3c460 65 6d 65 6e 74 0a 2a 2a 20 73 61 74 69 73 66 69  ement.** satisfi
3c470 65 73 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  es the condition
3c480 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  s of this routin
3c490 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55  e, it returns NU
3c4a0 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 64  LL..**.** The [d
3c4b0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3c4c0 6f 6e 5d 20 70 6f 69 6e 74 65 72 20 44 20 69 6e  on] pointer D in
3c4d0 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73   a call to.** [s
3c4e0 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74  qlite3_next_stmt
3c4f0 28 44 2c 53 29 5d 20 6d 75 73 74 20 72 65 66 65  (D,S)] must refe
3c500 72 20 74 6f 20 61 6e 20 6f 70 65 6e 20 64 61 74  r to an open dat
3c510 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
3c520 69 6f 6e 20 61 6e 64 20 69 6e 20 70 61 72 74 69  ion and in parti
3c530 63 75 6c 61 72 20 6d 75 73 74 20 6e 6f 74 20 62  cular must not b
3c540 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  e a NULL pointer
3c550 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3c560 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71  sqlite3_stmt *sq
3c570 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28  lite3_next_stmt(
3c580 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71  sqlite3 *pDb, sq
3c590 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
3c5a0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
3c5b0 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64 20  REF: Commit And 
3c5c0 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69 63  Rollback Notific
3c5d0 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 0a  ation Callbacks.
3c5e0 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74  **.** ^The sqlit
3c5f0 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29  e3_commit_hook()
3c600 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73   interface regis
3c610 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a  ters a callback.
3c620 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ** function to b
3c630 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76  e invoked whenev
3c640 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  er a transaction
3c650 20 69 73 20 5b 43 4f 4d 4d 49 54 20 7c 20 63 6f   is [COMMIT | co
3c660 6d 6d 69 74 74 65 64 5d 2e 0a 2a 2a 20 5e 41 6e  mmitted]..** ^An
3c670 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62  y callback set b
3c680 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c  y a previous cal
3c690 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d  l to sqlite3_com
3c6a0 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f  mit_hook().** fo
3c6b0 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  r the same datab
3c6c0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
3c6d0 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a  s overridden..**
3c6e0 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72 6f   ^The sqlite3_ro
3c6f0 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 20 69 6e  llback_hook() in
3c700 74 65 72 66 61 63 65 20 72 65 67 69 73 74 65 72  terface register
3c710 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20  s a callback.** 
3c720 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69  function to be i
3c730 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20  nvoked whenever 
3c740 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
3c750 20 5b 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 6f 6c   [ROLLBACK | rol
3c760 6c 65 64 20 62 61 63 6b 5d 2e 0a 2a 2a 20 5e 41  led back]..** ^A
3c770 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20  ny callback set 
3c780 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61  by a previous ca
3c790 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 6f  ll to sqlite3_ro
3c7a0 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 0a 2a 2a  llback_hook().**
3c7b0 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 64 61   for the same da
3c7c0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3c7d0 6e 20 69 73 20 6f 76 65 72 72 69 64 64 65 6e 2e  n is overridden.
3c7e0 0a 2a 2a 20 5e 54 68 65 20 70 41 72 67 20 61 72  .** ^The pArg ar
3c7f0 67 75 6d 65 6e 74 20 69 73 20 70 61 73 73 65 64  gument is passed
3c800 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
3c810 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 5e 49 66  callback..** ^If
3c820 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e   the callback on
3c830 20 61 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 66   a commit hook f
3c840 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
3c850 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65  non-zero,.** the
3c860 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 69 73 20  n the commit is 
3c870 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61  converted into a
3c880 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a   rollback..**.**
3c890 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f   ^The sqlite3_co
3c8a0 6d 6d 69 74 5f 68 6f 6f 6b 28 44 2c 43 2c 50 29  mmit_hook(D,C,P)
3c8b0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 6f 6c   and sqlite3_rol
3c8c0 6c 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c 43 2c 50  lback_hook(D,C,P
3c8d0 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 72  ) functions.** r
3c8e0 65 74 75 72 6e 20 74 68 65 20 50 20 61 72 67 75  eturn the P argu
3c8f0 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 72  ment from the pr
3c900 65 76 69 6f 75 73 20 63 61 6c 6c 20 6f 66 20 74  evious call of t
3c910 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e  he same function
3c920 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  .** on the same 
3c930 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
3c940 74 69 6f 6e 5d 20 44 2c 20 6f 72 20 4e 55 4c 4c  tion] D, or NULL
3c950 20 66 6f 72 0a 2a 2a 20 74 68 65 20 66 69 72 73   for.** the firs
3c960 74 20 63 61 6c 6c 20 66 6f 72 20 65 61 63 68 20  t call for each 
3c970 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 44 2e 0a 2a  function on D..*
3c980 2a 0a 2a 2a 20 54 68 65 20 63 6f 6d 6d 69 74 20  *.** The commit 
3c990 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f  and rollback hoo
3c9a0 6b 20 63 61 6c 6c 62 61 63 6b 73 20 61 72 65 20  k callbacks are 
3c9b0 6e 6f 74 20 72 65 65 6e 74 72 61 6e 74 2e 0a 2a  not reentrant..*
3c9c0 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 69  * The callback i
3c9d0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75  mplementation mu
3c9e0 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69  st not do anythi
3c9f0 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64  ng that will mod
3ca00 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  ify.** the datab
3ca10 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
3ca20 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20  hat invoked the 
3ca30 63 61 6c 6c 62 61 63 6b 2e 20 20 41 6e 79 20 61  callback.  Any a
3ca40 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d 6f 64  ctions.** to mod
3ca50 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73 65  ify the database
3ca60 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74   connection must
3ca70 20 62 65 20 64 65 66 65 72 72 65 64 20 75 6e 74   be deferred unt
3ca80 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a 2a 20  il after the.** 
3ca90 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 74 68  completion of th
3caa0 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  e [sqlite3_step(
3cab0 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69  )] call that tri
3cac0 67 67 65 72 65 64 20 74 68 65 20 63 6f 6d 6d 69  ggered the commi
3cad0 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62 61 63 6b  t.** or rollback
3cae0 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20 66 69 72   hook in the fir
3caf0 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 20 4e 6f 74  st place..** Not
3cb00 65 20 74 68 61 74 20 72 75 6e 6e 69 6e 67 20 61  e that running a
3cb10 6e 79 20 6f 74 68 65 72 20 53 51 4c 20 73 74 61  ny other SQL sta
3cb20 74 65 6d 65 6e 74 73 2c 20 69 6e 63 6c 75 64 69  tements, includi
3cb30 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ng SELECT statem
3cb40 65 6e 74 73 2c 0a 2a 2a 20 6f 72 20 6d 65 72 65  ents,.** or mere
3cb50 6c 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  ly calling [sqli
3cb60 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
3cb70 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73  ] and [sqlite3_s
3cb80 74 65 70 28 29 5d 20 77 69 6c 6c 20 6d 6f 64 69  tep()] will modi
3cb90 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  fy.** the databa
3cba0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66  se connections f
3cbb0 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f  or the meaning o
3cbc0 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68  f "modify" in th
3cbd0 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a  is paragraph..**
3cbe0 0a 2a 2a 20 5e 52 65 67 69 73 74 65 72 69 6e 67  .** ^Registering
3cbf0 20 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 6f 6e   a NULL function
3cc00 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 61   disables the ca
3cc10 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 5e 57  llback..**.** ^W
3cc20 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 68  hen the commit h
3cc30 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75  ook callback rou
3cc40 74 69 6e 65 20 72 65 74 75 72 6e 73 20 7a 65 72  tine returns zer
3cc50 6f 2c 20 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 0a  o, the [COMMIT].
3cc60 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20  ** operation is 
3cc70 61 6c 6c 6f 77 65 64 20 74 6f 20 63 6f 6e 74 69  allowed to conti
3cc80 6e 75 65 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 5e  nue normally.  ^
3cc90 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f  If the commit ho
3cca0 6f 6b 0a 2a 2a 20 72 65 74 75 72 6e 73 20 6e 6f  ok.** returns no
3ccb0 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  n-zero, then the
3ccc0 20 5b 43 4f 4d 4d 49 54 5d 20 69 73 20 63 6f 6e   [COMMIT] is con
3ccd0 76 65 72 74 65 64 20 69 6e 74 6f 20 61 20 5b 52  verted into a [R
3cce0 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a 20 5e 54 68  OLLBACK]..** ^Th
3ccf0 65 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20  e rollback hook 
3cd00 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 61 20  is invoked on a 
3cd10 72 6f 6c 6c 62 61 63 6b 20 74 68 61 74 20 72 65  rollback that re
3cd20 73 75 6c 74 73 20 66 72 6f 6d 20 61 20 63 6f 6d  sults from a com
3cd30 6d 69 74 0a 2a 2a 20 68 6f 6f 6b 20 72 65 74 75  mit.** hook retu
3cd40 72 6e 69 6e 67 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  rning non-zero, 
3cd50 6a 75 73 74 20 61 73 20 69 74 20 77 6f 75 6c 64  just as it would
3cd60 20 62 65 20 77 69 74 68 20 61 6e 79 20 6f 74 68   be with any oth
3cd70 65 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a  er rollback..**.
3cd80 2a 2a 20 5e 46 6f 72 20 74 68 65 20 70 75 72 70  ** ^For the purp
3cd90 6f 73 65 73 20 6f 66 20 74 68 69 73 20 41 50 49  oses of this API
3cda0 2c 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  , a transaction 
3cdb0 69 73 20 73 61 69 64 20 74 6f 20 68 61 76 65 20  is said to have 
3cdc0 62 65 65 6e 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62  been.** rolled b
3cdd0 61 63 6b 20 69 66 20 61 6e 20 65 78 70 6c 69 63  ack if an explic
3cde0 69 74 20 22 52 4f 4c 4c 42 41 43 4b 22 20 73 74  it "ROLLBACK" st
3cdf0 61 74 65 6d 65 6e 74 20 69 73 20 65 78 65 63 75  atement is execu
3ce00 74 65 64 2c 20 6f 72 0a 2a 2a 20 61 6e 20 65 72  ted, or.** an er
3ce10 72 6f 72 20 6f 72 20 63 6f 6e 73 74 72 61 69 6e  ror or constrain
3ce20 74 20 63 61 75 73 65 73 20 61 6e 20 69 6d 70 6c  t causes an impl
3ce30 69 63 69 74 20 72 6f 6c 6c 62 61 63 6b 20 74 6f  icit rollback to
3ce40 20 6f 63 63 75 72 2e 0a 2a 2a 20 5e 54 68 65 20   occur..** ^The 
3ce50 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63  rollback callbac
3ce60 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64  k is not invoked
3ce70 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   if a transactio
3ce80 6e 20 69 73 0a 2a 2a 20 61 75 74 6f 6d 61 74 69  n is.** automati
3ce90 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63  cally rolled bac
3cea0 6b 20 62 65 63 61 75 73 65 20 74 68 65 20 64 61  k because the da
3ceb0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3cec0 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a  n is closed..**.
3ced0 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20  ** See also the 
3cee0 5b 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f  [sqlite3_update_
3cef0 68 6f 6f 6b 28 29 5d 20 69 6e 74 65 72 66 61 63  hook()] interfac
3cf00 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
3cf10 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63   void *sqlite3_c
3cf20 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 73 71 6c 69 74  ommit_hook(sqlit
3cf30 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64  e3*, int(*)(void
3cf40 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49  *), void*);.SQLI
3cf50 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
3cf60 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f  ite3_rollback_ho
3cf70 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69  ok(sqlite3*, voi
3cf80 64 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f  d(*)(void *), vo
3cf90 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  id*);../*.** CAP
3cfa0 49 33 52 45 46 3a 20 44 61 74 61 20 43 68 61 6e  I3REF: Data Chan
3cfb0 67 65 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20  ge Notification 
3cfc0 43 61 6c 6c 62 61 63 6b 73 0a 2a 2a 0a 2a 2a 20  Callbacks.**.** 
3cfd0 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 75 70 64  ^The sqlite3_upd
3cfe0 61 74 65 5f 68 6f 6f 6b 28 29 20 69 6e 74 65 72  ate_hook() inter
3cff0 66 61 63 65 20 72 65 67 69 73 74 65 72 73 20 61  face registers a
3d000 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
3d010 6f 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b  on.** with the [
3d020 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3d030 69 6f 6e 5d 20 69 64 65 6e 74 69 66 69 65 64 20  ion] identified 
3d040 62 79 20 74 68 65 20 66 69 72 73 74 20 61 72 67  by the first arg
3d050 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 62 65 20 69  ument.** to be i
3d060 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20  nvoked whenever 
3d070 61 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64  a row is updated
3d080 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65  , inserted or de
3d090 6c 65 74 65 64 2e 0a 2a 2a 20 5e 41 6e 79 20 63  leted..** ^Any c
3d0a0 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20 61  allback set by a
3d0b0 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74   previous call t
3d0c0 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a  o this function.
3d0d0 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20  ** for the same 
3d0e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3d0f0 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 64 65  ion is overridde
3d100 6e 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 65  n..**.** ^The se
3d110 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
3d120 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
3d130 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e  e function to in
3d140 76 6f 6b 65 20 77 68 65 6e 20 61 0a 2a 2a 20 72  voke when a.** r
3d150 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c 20 69  ow is updated, i
3d160 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74  nserted or delet
3d170 65 64 2e 0a 2a 2a 20 5e 54 68 65 20 66 69 72 73  ed..** ^The firs
3d180 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
3d190 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20  e callback is a 
3d1a0 63 6f 70 79 20 6f 66 20 74 68 65 20 74 68 69 72  copy of the thir
3d1b0 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f  d argument.** to
3d1c0 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f   sqlite3_update_
3d1d0 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 5e 54 68 65 20  hook()..** ^The 
3d1e0 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61 63 6b 20  second callback 
3d1f0 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 65 20  argument is one 
3d200 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53 45 52  of [SQLITE_INSER
3d210 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45  T], [SQLITE_DELE
3d220 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49  TE],.** or [SQLI
3d230 54 45 5f 55 50 44 41 54 45 5d 2c 20 64 65 70 65  TE_UPDATE], depe
3d240 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 65  nding on the ope
3d250 72 61 74 69 6f 6e 20 74 68 61 74 20 63 61 75 73  ration that caus
3d260 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a  ed the callback.
3d270 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64  ** to be invoked
3d280 2e 0a 2a 2a 20 5e 54 68 65 20 74 68 69 72 64 20  ..** ^The third 
3d290 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 6d  and fourth argum
3d2a0 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c 6c  ents to the call
3d2b0 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f 69  back contain poi
3d2c0 6e 74 65 72 73 20 74 6f 20 74 68 65 0a 2a 2a 20  nters to the.** 
3d2d0 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 61 62  database and tab
3d2e0 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69 6e 69  le name containi
3d2f0 6e 67 20 74 68 65 20 61 66 66 65 63 74 65 64 20  ng the affected 
3d300 72 6f 77 2e 0a 2a 2a 20 5e 54 68 65 20 66 69 6e  row..** ^The fin
3d310 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 61  al callback para
3d320 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 72 6f  meter is the [ro
3d330 77 69 64 5d 20 6f 66 20 74 68 65 20 72 6f 77 2e  wid] of the row.
3d340 0a 2a 2a 20 5e 49 6e 20 74 68 65 20 63 61 73 65  .** ^In the case
3d350 20 6f 66 20 61 6e 20 75 70 64 61 74 65 2c 20 74   of an update, t
3d360 68 69 73 20 69 73 20 74 68 65 20 5b 72 6f 77 69  his is the [rowi
3d370 64 5d 20 61 66 74 65 72 20 74 68 65 20 75 70 64  d] after the upd
3d380 61 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e  ate takes place.
3d390 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 75 70 64  .**.** ^(The upd
3d3a0 61 74 65 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20  ate hook is not 
3d3b0 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74  invoked when int
3d3c0 65 72 6e 61 6c 20 73 79 73 74 65 6d 20 74 61 62  ernal system tab
3d3d0 6c 65 73 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66  les are.** modif
3d3e0 69 65 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65  ied (i.e. sqlite
3d3f0 5f 6d 61 73 74 65 72 20 61 6e 64 20 73 71 6c 69  _master and sqli
3d400 74 65 5f 73 65 71 75 65 6e 63 65 29 2e 29 5e 0a  te_sequence).)^.
3d410 2a 2a 0a 2a 2a 20 5e 49 6e 20 74 68 65 20 63 75  **.** ^In the cu
3d420 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61  rrent implementa
3d430 74 69 6f 6e 2c 20 74 68 65 20 75 70 64 61 74 65  tion, the update
3d440 20 68 6f 6f 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20   hook.** is not 
3d450 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 64 75 70  invoked when dup
3d460 6c 69 63 61 74 69 6f 6e 20 72 6f 77 73 20 61 72  lication rows ar
3d470 65 20 64 65 6c 65 74 65 64 20 62 65 63 61 75 73  e deleted becaus
3d480 65 20 6f 66 20 61 6e 0a 2a 2a 20 5b 4f 4e 20 43  e of an.** [ON C
3d490 4f 4e 46 4c 49 43 54 20 7c 20 4f 4e 20 43 4f 4e  ONFLICT | ON CON
3d4a0 46 4c 49 43 54 20 52 45 50 4c 41 43 45 5d 20 63  FLICT REPLACE] c
3d4b0 6c 61 75 73 65 2e 20 20 5e 4e 6f 72 20 69 73 20  lause.  ^Nor is 
3d4c0 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 0a  the update hook.
3d4d0 2a 2a 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20  ** invoked when 
3d4e0 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65 64  rows are deleted
3d4f0 20 75 73 69 6e 67 20 74 68 65 20 5b 74 72 75 6e   using the [trun
3d500 63 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f  cate optimizatio
3d510 6e 5d 2e 0a 2a 2a 20 54 68 65 20 65 78 63 65 70  n]..** The excep
3d520 74 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 6e  tions defined in
3d530 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 20   this paragraph 
3d540 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20  might change in 
3d550 61 20 66 75 74 75 72 65 0a 2a 2a 20 72 65 6c 65  a future.** rele
3d560 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  ase of SQLite..*
3d570 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20  *.** The update 
3d580 68 6f 6f 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74  hook implementat
3d590 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20  ion must not do 
3d5a0 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69  anything that wi
3d5b0 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65  ll modify.** the
3d5c0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
3d5d0 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65  tion that invoke
3d5e0 64 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f  d the update hoo
3d5f0 6b 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 0a  k.  Any actions.
3d600 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65  ** to modify the
3d610 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
3d620 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 66  tion must be def
3d630 65 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 65  erred until afte
3d640 72 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74  r the.** complet
3d650 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69  ion of the [sqli
3d660 74 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c 6c  te3_step()] call
3d670 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64 20   that triggered 
3d680 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e  the update hook.
3d690 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73  .** Note that [s
3d6a0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
3d6b0 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  2()] and [sqlite
3d6c0 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d  3_step()] both m
3d6d0 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64  odify their.** d
3d6e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3d6f0 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e  ons for the mean
3d700 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20  ing of "modify" 
3d710 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70  in this paragrap
3d720 68 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71  h..**.** ^The sq
3d730 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f  lite3_update_hoo
3d740 6b 28 44 2c 43 2c 50 29 20 66 75 6e 63 74 69 6f  k(D,C,P) functio
3d750 6e 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74 68 65  n.** returns the
3d760 20 50 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d   P argument from
3d770 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 61   the previous ca
3d780 6c 6c 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 61 6d  ll.** on the sam
3d790 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
3d7a0 65 63 74 69 6f 6e 5d 20 44 2c 20 6f 72 20 4e 55  ection] D, or NU
3d7b0 4c 4c 20 66 6f 72 0a 2a 2a 20 74 68 65 20 66 69  LL for.** the fi
3d7c0 72 73 74 20 63 61 6c 6c 20 6f 6e 20 44 2e 0a 2a  rst call on D..*
3d7d0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68  *.** See also th
3d7e0 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69  e [sqlite3_commi
3d7f0 74 5f 68 6f 6f 6b 28 29 5d 20 61 6e 64 20 5b 73  t_hook()] and [s
3d800 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f  qlite3_rollback_
3d810 68 6f 6f 6b 28 29 5d 0a 2a 2a 20 69 6e 74 65 72  hook()].** inter
3d820 66 61 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  faces..*/.SQLITE
3d830 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
3d840 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 0a  e3_update_hook(.
3d850 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76    sqlite3*, .  v
3d860 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 2c 69 6e  oid(*)(void *,in
3d870 74 20 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c  t ,char const *,
3d880 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c  char const *,sql
3d890 69 74 65 33 5f 69 6e 74 36 34 29 2c 0a 20 20 76  ite3_int64),.  v
3d8a0 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  oid*.);../*.** C
3d8b0 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20  API3REF: Enable 
3d8c0 4f 72 20 44 69 73 61 62 6c 65 20 53 68 61 72 65  Or Disable Share
3d8d0 64 20 50 61 67 65 72 20 43 61 63 68 65 0a 2a 2a  d Pager Cache.**
3d8e0 0a 2a 2a 20 5e 28 54 68 69 73 20 72 6f 75 74 69  .** ^(This routi
3d8f0 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69  ne enables or di
3d900 73 61 62 6c 65 73 20 74 68 65 20 73 68 61 72 69  sables the shari
3d910 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ng of the databa
3d920 73 65 20 63 61 63 68 65 0a 2a 2a 20 61 6e 64 20  se cache.** and 
3d930 73 63 68 65 6d 61 20 64 61 74 61 20 73 74 72 75  schema data stru
3d940 63 74 75 72 65 73 20 62 65 74 77 65 65 6e 20 5b  ctures between [
3d950 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3d960 69 6f 6e 20 7c 20 63 6f 6e 6e 65 63 74 69 6f 6e  ion | connection
3d970 73 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61 6d  s].** to the sam
3d980 65 20 64 61 74 61 62 61 73 65 2e 20 53 68 61 72  e database. Shar
3d990 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 69  ing is enabled i
3d9a0 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69  f the argument i
3d9b0 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 64 69  s true.** and di
3d9c0 73 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72  sabled if the ar
3d9d0 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65 2e  gument is false.
3d9e0 29 5e 0a 2a 2a 0a 2a 2a 20 5e 43 61 63 68 65 20  )^.**.** ^Cache 
3d9f0 73 68 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c  sharing is enabl
3da00 65 64 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20  ed and disabled 
3da10 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 20 70 72  for an entire pr
3da20 6f 63 65 73 73 2e 0a 2a 2a 20 54 68 69 73 20 69  ocess..** This i
3da30 73 20 61 20 63 68 61 6e 67 65 20 61 73 20 6f 66  s a change as of
3da40 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20   SQLite version 
3da50 33 2e 35 2e 30 2e 20 49 6e 20 70 72 69 6f 72 20  3.5.0. In prior 
3da60 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  versions of SQLi
3da70 74 65 2c 0a 2a 2a 20 73 68 61 72 69 6e 67 20 77  te,.** sharing w
3da80 61 73 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69  as enabled or di
3da90 73 61 62 6c 65 64 20 66 6f 72 20 65 61 63 68 20  sabled for each 
3daa0 74 68 72 65 61 64 20 73 65 70 61 72 61 74 65 6c  thread separatel
3dab0 79 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 63  y..**.** ^(The c
3dac0 61 63 68 65 20 73 68 61 72 69 6e 67 20 6d 6f 64  ache sharing mod
3dad0 65 20 73 65 74 20 62 79 20 74 68 69 73 20 69 6e  e set by this in
3dae0 74 65 72 66 61 63 65 20 65 66 66 65 63 74 73 20  terface effects 
3daf0 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a 2a  all subsequent.*
3db00 2a 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69  * calls to [sqli
3db10 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71  te3_open()], [sq
3db20 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d  lite3_open_v2()]
3db30 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6f  , and [sqlite3_o
3db40 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20 45 78 69  pen16()]..** Exi
3db50 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 63  sting database c
3db60 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 6f 6e 74 69  onnections conti
3db70 6e 75 65 20 75 73 65 20 74 68 65 20 73 68 61 72  nue use the shar
3db80 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74 68 61 74  ing mode.** that
3db90 20 77 61 73 20 69 6e 20 65 66 66 65 63 74 20 61   was in effect a
3dba0 74 20 74 68 65 20 74 69 6d 65 20 74 68 65 79 20  t the time they 
3dbb0 77 65 72 65 20 6f 70 65 6e 65 64 2e 29 5e 0a 2a  were opened.)^.*
3dbc0 2a 0a 2a 2a 20 5e 28 54 68 69 73 20 72 6f 75 74  *.** ^(This rout
3dbd0 69 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  ine returns [SQL
3dbe0 49 54 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72 65  ITE_OK] if share
3dbf0 64 20 63 61 63 68 65 20 77 61 73 20 65 6e 61 62  d cache was enab
3dc00 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 0a  led or disabled.
3dc10 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e  ** successfully.
3dc20 20 20 41 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65    An [error code
3dc30 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 74  ] is returned ot
3dc40 68 65 72 77 69 73 65 2e 29 5e 0a 2a 2a 0a 2a 2a  herwise.)^.**.**
3dc50 20 5e 53 68 61 72 65 64 20 63 61 63 68 65 20 69   ^Shared cache i
3dc60 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 64 65  s disabled by de
3dc70 66 61 75 6c 74 2e 20 42 75 74 20 74 68 69 73 20  fault. But this 
3dc80 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 0a  might change in.
3dc90 2a 2a 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  ** future releas
3dca0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 41  es of SQLite.  A
3dcb0 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74  pplications that
3dcc0 20 63 61 72 65 20 61 62 6f 75 74 20 73 68 61 72   care about shar
3dcd0 65 64 0a 2a 2a 20 63 61 63 68 65 20 73 65 74 74  ed.** cache sett
3dce0 69 6e 67 20 73 68 6f 75 6c 64 20 73 65 74 20 69  ing should set i
3dcf0 74 20 65 78 70 6c 69 63 69 74 6c 79 2e 0a 2a 2a  t explicitly..**
3dd00 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
3dd10 63 65 20 69 73 20 74 68 72 65 61 64 73 61 66 65  ce is threadsafe
3dd20 20 6f 6e 20 70 72 6f 63 65 73 73 6f 72 73 20 77   on processors w
3dd30 68 65 72 65 20 77 72 69 74 69 6e 67 20 61 0a 2a  here writing a.*
3dd40 2a 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  * 32-bit integer
3dd50 20 69 73 20 61 74 6f 6d 69 63 2e 0a 2a 2a 0a 2a   is atomic..**.*
3dd60 2a 20 53 65 65 20 41 6c 73 6f 3a 20 20 5b 53 51  * See Also:  [SQ
3dd70 4c 69 74 65 20 53 68 61 72 65 64 2d 43 61 63 68  Lite Shared-Cach
3dd80 65 20 4d 6f 64 65 5d 0a 2a 2f 0a 53 51 4c 49 54  e Mode].*/.SQLIT
3dd90 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3dda0 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f  3_enable_shared_
3ddb0 63 61 63 68 65 28 69 6e 74 29 3b 0a 0a 2f 2a 0a  cache(int);../*.
3ddc0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 74 74  ** CAPI3REF: Att
3ddd0 65 6d 70 74 20 54 6f 20 46 72 65 65 20 48 65 61  empt To Free Hea
3dde0 70 20 4d 65 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 5e  p Memory.**.** ^
3ddf0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65  The sqlite3_rele
3de00 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 69 6e 74  ase_memory() int
3de10 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20  erface attempts 
3de20 74 6f 20 66 72 65 65 20 4e 20 62 79 74 65 73 0a  to free N bytes.
3de30 2a 2a 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72  ** of heap memor
3de40 79 20 62 79 20 64 65 61 6c 6c 6f 63 61 74 69 6e  y by deallocatin
3de50 67 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20  g non-essential 
3de60 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
3de70 6e 73 0a 2a 2a 20 68 65 6c 64 20 62 79 20 74 68  ns.** held by th
3de80 65 20 64 61 74 61 62 61 73 65 20 6c 69 62 72 61  e database libra
3de90 72 79 2e 20 20 20 4d 65 6d 6f 72 79 20 75 73 65  ry.   Memory use
3dea0 64 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 62  d to cache datab
3deb0 61 73 65 0a 2a 2a 20 70 61 67 65 73 20 74 6f 20  ase.** pages to 
3dec0 69 6d 70 72 6f 76 65 20 70 65 72 66 6f 72 6d 61  improve performa
3ded0 6e 63 65 20 69 73 20 61 6e 20 65 78 61 6d 70 6c  nce is an exampl
3dee0 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69  e of non-essenti
3def0 61 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 5e 73  al memory..** ^s
3df00 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
3df10 65 6d 6f 72 79 28 29 20 72 65 74 75 72 6e 73 20  emory() returns 
3df20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
3df30 74 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72 65  tes actually fre
3df40 65 64 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 69 67  ed,.** which mig
3df50 68 74 20 62 65 20 6d 6f 72 65 20 6f 72 20 6c 65  ht be more or le
3df60 73 73 20 74 68 61 6e 20 74 68 65 20 61 6d 6f 75  ss than the amou
3df70 6e 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a 2a  nt requested..**
3df80 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65   ^The sqlite3_re
3df90 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 72  lease_memory() r
3dfa0 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
3dfb0 70 20 72 65 74 75 72 6e 69 6e 67 20 7a 65 72 6f  p returning zero
3dfc0 0a 2a 2a 20 69 66 20 53 51 4c 69 74 65 20 69 73  .** if SQLite is
3dfd0 20 6e 6f 74 20 63 6f 6d 70 69 6c 65 64 20 77 69   not compiled wi
3dfe0 74 68 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c  th [SQLITE_ENABL
3dff0 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d  E_MEMORY_MANAGEM
3e000 45 4e 54 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  ENT]..**.** See 
3e010 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 64  also: [sqlite3_d
3e020 62 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  b_release_memory
3e030 28 29 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ()].*/.SQLITE_AP
3e040 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65  I int sqlite3_re
3e050 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74  lease_memory(int
3e060 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
3e070 45 46 3a 20 46 72 65 65 20 4d 65 6d 6f 72 79 20  EF: Free Memory 
3e080 55 73 65 64 20 42 79 20 41 20 44 61 74 61 62 61  Used By A Databa
3e090 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  se Connection.**
3e0a0 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33  .** ^The sqlite3
3e0b0 5f 64 62 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  _db_release_memo
3e0c0 72 79 28 44 29 20 69 6e 74 65 72 66 61 63 65 20  ry(D) interface 
3e0d0 61 74 74 65 6d 70 74 73 20 74 6f 20 66 72 65 65  attempts to free
3e0e0 20 61 73 20 6d 75 63 68 20 68 65 61 70 0a 2a 2a   as much heap.**
3e0f0 20 6d 65 6d 6f 72 79 20 61 73 20 70 6f 73 73 69   memory as possi
3e100 62 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73  ble from databas
3e110 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e 20  e connection D. 
3e120 55 6e 6c 69 6b 65 20 74 68 65 0a 2a 2a 20 5b 73  Unlike the.** [s
3e130 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
3e140 65 6d 6f 72 79 28 29 5d 20 69 6e 74 65 72 66 61  emory()] interfa
3e150 63 65 2c 20 74 68 69 73 20 69 6e 74 65 72 66 61  ce, this interfa
3e160 63 65 20 69 73 20 65 66 66 65 63 74 20 65 76 65  ce is effect eve
3e170 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 65 6e 20 5b  n.** when then [
3e180 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
3e190 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 5d  MORY_MANAGEMENT]
3e1a0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70   compile-time op
3e1b0 74 69 6f 6e 20 69 73 0a 2a 2a 20 6f 6d 69 74 74  tion is.** omitt
3e1c0 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  ed..**.** See al
3e1d0 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c  so: [sqlite3_rel
3e1e0 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 0a 2a  ease_memory()].*
3e1f0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
3e200 20 73 71 6c 69 74 65 33 5f 64 62 5f 72 65 6c 65   sqlite3_db_rele
3e210 61 73 65 5f 6d 65 6d 6f 72 79 28 73 71 6c 69 74  ase_memory(sqlit
3e220 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  e3*);../*.** CAP
3e230 49 33 52 45 46 3a 20 49 6d 70 6f 73 65 20 41 20  I3REF: Impose A 
3e240 4c 69 6d 69 74 20 4f 6e 20 48 65 61 70 20 53 69  Limit On Heap Si
3e250 7a 65 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71  ze.**.** ^The sq
3e260 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f  lite3_soft_heap_
3e270 6c 69 6d 69 74 36 34 28 29 20 69 6e 74 65 72 66  limit64() interf
3e280 61 63 65 20 73 65 74 73 20 61 6e 64 2f 6f 72 20  ace sets and/or 
3e290 71 75 65 72 69 65 73 20 74 68 65 0a 2a 2a 20 73  queries the.** s
3e2a0 6f 66 74 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65  oft limit on the
3e2b0 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 61 70 20   amount of heap 
3e2c0 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 61 79 20  memory that may 
3e2d0 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  be allocated by 
3e2e0 53 51 4c 69 74 65 2e 0a 2a 2a 20 5e 53 51 4c 69  SQLite..** ^SQLi
3e2f0 74 65 20 73 74 72 69 76 65 73 20 74 6f 20 6b 65  te strives to ke
3e300 65 70 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 75  ep heap memory u
3e310 74 69 6c 69 7a 61 74 69 6f 6e 20 62 65 6c 6f 77  tilization below
3e320 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 0a 2a   the soft heap.*
3e330 2a 20 6c 69 6d 69 74 20 62 79 20 72 65 64 75 63  * limit by reduc
3e340 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ing the number o
3e350 66 20 70 61 67 65 73 20 68 65 6c 64 20 69 6e 20  f pages held in 
3e360 74 68 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a  the page cache.*
3e370 2a 20 61 73 20 68 65 61 70 20 6d 65 6d 6f 72 79  * as heap memory
3e380 20 75 73 61 67 65 73 20 61 70 70 72 6f 61 63 68   usages approach
3e390 65 73 20 74 68 65 20 6c 69 6d 69 74 2e 0a 2a 2a  es the limit..**
3e3a0 20 5e 54 68 65 20 73 6f 66 74 20 68 65 61 70 20   ^The soft heap 
3e3b0 6c 69 6d 69 74 20 69 73 20 22 73 6f 66 74 22 20  limit is "soft" 
3e3c0 62 65 63 61 75 73 65 20 65 76 65 6e 20 74 68 6f  because even tho
3e3d0 75 67 68 20 53 51 4c 69 74 65 20 73 74 72 69 76  ugh SQLite striv
3e3e0 65 73 20 74 6f 20 73 74 61 79 0a 2a 2a 20 62 65  es to stay.** be
3e3f0 6c 6f 77 20 74 68 65 20 6c 69 6d 69 74 2c 20 69  low the limit, i
3e400 74 20 77 69 6c 6c 20 65 78 63 65 65 64 20 74 68  t will exceed th
3e410 65 20 6c 69 6d 69 74 20 72 61 74 68 65 72 20 74  e limit rather t
3e420 68 61 6e 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20  han generate.** 
3e430 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  an [SQLITE_NOMEM
3e440 5d 20 65 72 72 6f 72 2e 20 20 49 6e 20 6f 74 68  ] error.  In oth
3e450 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 73 6f  er words, the so
3e460 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 0a 2a  ft heap limit .*
3e470 2a 20 69 73 20 61 64 76 69 73 6f 72 79 20 6f 6e  * is advisory on
3e480 6c 79 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 72  ly..**.** ^The r
3e490 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d  eturn value from
3e4a0 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65   sqlite3_soft_he
3e4b0 61 70 5f 6c 69 6d 69 74 36 34 28 29 20 69 73 20  ap_limit64() is 
3e4c0 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74  the size of.** t
3e4d0 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  he soft heap lim
3e4e0 69 74 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  it prior to the 
3e4f0 63 61 6c 6c 2c 20 6f 72 20 6e 65 67 61 74 69 76  call, or negativ
3e500 65 20 69 6e 20 74 68 65 20 63 61 73 65 20 6f 66  e in the case of
3e510 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 2e 20 20 5e   an.** error.  ^
3e520 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  If the argument 
3e530 4e 20 69 73 20 6e 65 67 61 74 69 76 65 0a 2a 2a  N is negative.**
3e540 20 74 68 65 6e 20 6e 6f 20 63 68 61 6e 67 65 20   then no change 
3e550 69 73 20 6d 61 64 65 20 74 6f 20 74 68 65 20 73  is made to the s
3e560 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 2e 20  oft heap limit. 
3e570 20 48 65 6e 63 65 2c 20 74 68 65 20 63 75 72 72   Hence, the curr
3e580 65 6e 74 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74  ent.** size of t
3e590 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  he soft heap lim
3e5a0 69 74 20 63 61 6e 20 62 65 20 64 65 74 65 72 6d  it can be determ
3e5b0 69 6e 65 64 20 62 79 20 69 6e 76 6f 6b 69 6e 67  ined by invoking
3e5c0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 6f 66 74  .** sqlite3_soft
3e5d0 5f 68 65 61 70 5f 6c 69 6d 69 74 36 34 28 29 20  _heap_limit64() 
3e5e0 77 69 74 68 20 61 20 6e 65 67 61 74 69 76 65 20  with a negative 
3e5f0 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
3e600 5e 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74  ^If the argument
3e610 20 4e 20 69 73 20 7a 65 72 6f 20 74 68 65 6e 20   N is zero then 
3e620 74 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69  the soft heap li
3e630 6d 69 74 20 69 73 20 64 69 73 61 62 6c 65 64 2e  mit is disabled.
3e640 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 73 6f 66  .**.** ^(The sof
3e650 74 20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20  t heap limit is 
3e660 6e 6f 74 20 65 6e 66 6f 72 63 65 64 20 69 6e 20  not enforced in 
3e670 74 68 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c  the current impl
3e680 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 66  ementation.** if
3e690 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20   one or more of 
3e6a0 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 64 69 74  following condit
3e6b0 69 6f 6e 73 20 61 72 65 20 74 72 75 65 3a 0a 2a  ions are true:.*
3e6c0 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
3e6d0 3e 20 54 68 65 20 73 6f 66 74 20 68 65 61 70 20  > The soft heap 
3e6e0 6c 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20  limit is set to 
3e6f0 7a 65 72 6f 2e 0a 2a 2a 20 3c 6c 69 3e 20 4d 65  zero..** <li> Me
3e700 6d 6f 72 79 20 61 63 63 6f 75 6e 74 69 6e 67 20  mory accounting 
3e710 69 73 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e  is disabled usin
3e720 67 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  g a combination 
3e730 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 5b  of the.**      [
3e740 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28  sqlite3_config](
3e750 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
3e760 45 4d 53 54 41 54 55 53 5d 2c 2e 2e 2e 29 20 73  EMSTATUS],...) s
3e770 74 61 72 74 2d 74 69 6d 65 20 6f 70 74 69 6f 6e  tart-time option
3e780 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 74 68 65   and.**      the
3e790 20 5b 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54   [SQLITE_DEFAULT
3e7a0 5f 4d 45 4d 53 54 41 54 55 53 5d 20 63 6f 6d 70  _MEMSTATUS] comp
3e7b0 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 2e  ile-time option.
3e7c0 0a 2a 2a 20 3c 6c 69 3e 20 41 6e 20 61 6c 74 65  .** <li> An alte
3e7d0 72 6e 61 74 69 76 65 20 70 61 67 65 20 63 61 63  rnative page cac
3e7e0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
3e7f0 6e 20 69 73 20 73 70 65 63 69 66 69 65 64 20 75  n is specified u
3e800 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 5b 73 71  sing.**      [sq
3e810 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53  lite3_config]([S
3e820 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41  QLITE_CONFIG_PCA
3e830 43 48 45 32 5d 2c 2e 2e 2e 29 2e 0a 2a 2a 20 3c  CHE2],...)..** <
3e840 6c 69 3e 20 54 68 65 20 70 61 67 65 20 63 61 63  li> The page cac
3e850 68 65 20 61 6c 6c 6f 63 61 74 65 73 20 66 72 6f  he allocates fro
3e860 6d 20 69 74 73 20 6f 77 6e 20 6d 65 6d 6f 72 79  m its own memory
3e870 20 70 6f 6f 6c 20 73 75 70 70 6c 69 65 64 0a 2a   pool supplied.*
3e880 2a 20 20 20 20 20 20 62 79 20 5b 73 71 6c 69 74  *      by [sqlit
3e890 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49  e3_config]([SQLI
3e8a0 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41  TE_CONFIG_PAGECA
3e8b0 43 48 45 5d 2c 2e 2e 2e 29 20 72 61 74 68 65 72  CHE],...) rather
3e8c0 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 66 72   than.**      fr
3e8d0 6f 6d 20 74 68 65 20 68 65 61 70 2e 0a 2a 2a 20  om the heap..** 
3e8e0 3c 2f 75 6c 3e 29 5e 0a 2a 2a 0a 2a 2a 20 42 65  </ul>)^.**.** Be
3e8f0 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 4c  ginning with SQL
3e900 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 37 2e  ite version 3.7.
3e910 33 2c 20 74 68 65 20 73 6f 66 74 20 68 65 61 70  3, the soft heap
3e920 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f 72 63   limit is enforc
3e930 65 64 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73 73  ed.** regardless
3e940 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e   of whether or n
3e950 6f 74 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 45  ot the [SQLITE_E
3e960 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e  NABLE_MEMORY_MAN
3e970 41 47 45 4d 45 4e 54 5d 0a 2a 2a 20 63 6f 6d 70  AGEMENT].** comp
3e980 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20  ile-time option 
3e990 69 73 20 69 6e 76 6f 6b 65 64 2e 20 20 57 69 74  is invoked.  Wit
3e9a0 68 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  h [SQLITE_ENABLE
3e9b0 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
3e9c0 4e 54 5d 2c 0a 2a 2a 20 74 68 65 20 73 6f 66 74  NT],.** the soft
3e9d0 20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20 65   heap limit is e
3e9e0 6e 66 6f 72 63 65 64 20 6f 6e 20 65 76 65 72 79  nforced on every
3e9f0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
3ea00 6f 6e 2e 20 20 57 69 74 68 6f 75 74 0a 2a 2a 20  on.  Without.** 
3ea10 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d  [SQLITE_ENABLE_M
3ea20 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
3ea30 5d 2c 20 74 68 65 20 73 6f 66 74 20 68 65 61 70  ], the soft heap
3ea40 20 6c 69 6d 69 74 20 69 73 20 6f 6e 6c 79 20 65   limit is only e
3ea50 6e 66 6f 72 63 65 64 0a 2a 2a 20 77 68 65 6e 20  nforced.** when 
3ea60 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61  memory is alloca
3ea70 74 65 64 20 62 79 20 74 68 65 20 70 61 67 65 20  ted by the page 
3ea80 63 61 63 68 65 2e 20 20 54 65 73 74 69 6e 67 20  cache.  Testing 
3ea90 73 75 67 67 65 73 74 73 20 74 68 61 74 20 62 65  suggests that be
3eaa0 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 70 61 67  cause.** the pag
3eab0 65 20 63 61 63 68 65 20 69 73 20 74 68 65 20 70  e cache is the p
3eac0 72 65 64 6f 6d 69 6e 61 74 65 20 6d 65 6d 6f 72  redominate memor
3ead0 79 20 75 73 65 72 20 69 6e 20 53 51 4c 69 74 65  y user in SQLite
3eae0 2c 20 6d 6f 73 74 0a 2a 2a 20 61 70 70 6c 69 63  , most.** applic
3eaf0 61 74 69 6f 6e 73 20 77 69 6c 6c 20 61 63 68 69  ations will achi
3eb00 65 76 65 20 61 64 65 71 75 61 74 65 20 73 6f 66  eve adequate sof
3eb10 74 20 68 65 61 70 20 6c 69 6d 69 74 20 65 6e 66  t heap limit enf
3eb20 6f 72 63 65 6d 65 6e 74 20 77 69 74 68 6f 75 74  orcement without
3eb30 0a 2a 2a 20 74 68 65 20 75 73 65 20 6f 66 20 5b  .** the use of [
3eb40 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
3eb50 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 5d  MORY_MANAGEMENT]
3eb60 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 69 72 63  ..**.** The circ
3eb70 75 6d 73 74 61 6e 63 65 73 20 75 6e 64 65 72 20  umstances under 
3eb80 77 68 69 63 68 20 53 51 4c 69 74 65 20 77 69 6c  which SQLite wil
3eb90 6c 20 65 6e 66 6f 72 63 65 20 74 68 65 20 73 6f  l enforce the so
3eba0 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 6d 61  ft heap limit ma
3ebb0 79 0a 2a 2a 20 63 68 61 6e 67 65 73 20 69 6e 20  y.** changes in 
3ebc0 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20  future releases 
3ebd0 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51  of SQLite..*/.SQ
3ebe0 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
3ebf0 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 73  _int64 sqlite3_s
3ec00 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 36 34  oft_heap_limit64
3ec10 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 4e  (sqlite3_int64 N
3ec20 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
3ec30 45 46 3a 20 44 65 70 72 65 63 61 74 65 64 20 53  EF: Deprecated S
3ec40 6f 66 74 20 48 65 61 70 20 4c 69 6d 69 74 20 49  oft Heap Limit I
3ec50 6e 74 65 72 66 61 63 65 0a 2a 2a 20 44 45 50 52  nterface.** DEPR
3ec60 45 43 41 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 69  ECATED.**.** Thi
3ec70 73 20 69 73 20 61 20 64 65 70 72 65 63 61 74 65  s is a deprecate
3ec80 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  d version of the
3ec90 20 5b 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68   [sqlite3_soft_h
3eca0 65 61 70 5f 6c 69 6d 69 74 36 34 28 29 5d 0a 2a  eap_limit64()].*
3ecb0 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 20 54 68  * interface.  Th
3ecc0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 70 72  is routine is pr
3ecd0 6f 76 69 64 65 64 20 66 6f 72 20 68 69 73 74 6f  ovided for histo
3ece0 72 69 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c  rical compatibil
3ecf0 69 74 79 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 41 6c  ity.** only.  Al
3ed00 6c 20 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f  l new applicatio
3ed10 6e 73 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68  ns should use th
3ed20 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 6f  e.** [sqlite3_so
3ed30 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 36 34 28  ft_heap_limit64(
3ed40 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 61 74  )] interface rat
3ed50 68 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e  her than this on
3ed60 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
3ed70 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54   SQLITE_DEPRECAT
3ed80 45 44 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  ED void sqlite3_
3ed90 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28  soft_heap_limit(
3eda0 69 6e 74 20 4e 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20  int N);.../*.** 
3edb0 43 41 50 49 33 52 45 46 3a 20 45 78 74 72 61 63  CAPI3REF: Extrac
3edc0 74 20 4d 65 74 61 64 61 74 61 20 41 62 6f 75 74  t Metadata About
3edd0 20 41 20 43 6f 6c 75 6d 6e 20 4f 66 20 41 20 54   A Column Of A T
3ede0 61 62 6c 65 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73  able.**.** ^This
3edf0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
3ee00 20 6d 65 74 61 64 61 74 61 20 61 62 6f 75 74 20   metadata about 
3ee10 61 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d  a specific colum
3ee20 6e 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 0a  n of a specific.
3ee30 2a 2a 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  ** database tabl
3ee40 65 20 61 63 63 65 73 73 69 62 6c 65 20 75 73 69  e accessible usi
3ee50 6e 67 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  ng the [database
3ee60 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e   connection] han
3ee70 64 6c 65 0a 2a 2a 20 70 61 73 73 65 64 20 61 73  dle.** passed as
3ee80 20 74 68 65 20 66 69 72 73 74 20 66 75 6e 63 74   the first funct
3ee90 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  ion argument..**
3eea0 0a 2a 2a 20 5e 54 68 65 20 63 6f 6c 75 6d 6e 20  .** ^The column 
3eeb0 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  is identified by
3eec0 20 74 68 65 20 73 65 63 6f 6e 64 2c 20 74 68 69   the second, thi
3eed0 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61  rd and fourth pa
3eee0 72 61 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 74  rameters to.** t
3eef0 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 5e 54  his function. ^T
3ef00 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
3ef10 74 65 72 20 69 73 20 65 69 74 68 65 72 20 74 68  ter is either th
3ef20 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  e name of the da
3ef30 74 61 62 61 73 65 0a 2a 2a 20 28 69 2e 65 2e 20  tabase.** (i.e. 
3ef40 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22 2c 20  "main", "temp", 
3ef50 6f 72 20 61 6e 20 61 74 74 61 63 68 65 64 20 64  or an attached d
3ef60 61 74 61 62 61 73 65 29 20 63 6f 6e 74 61 69 6e  atabase) contain
3ef70 69 6e 67 20 74 68 65 20 73 70 65 63 69 66 69 65  ing the specifie
3ef80 64 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 4e 55  d.** table or NU
3ef90 4c 4c 2e 20 5e 49 66 20 69 74 20 69 73 20 4e 55  LL. ^If it is NU
3efa0 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61 74 74  LL, then all att
3efb0 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20  ached databases 
3efc0 61 72 65 20 73 65 61 72 63 68 65 64 0a 2a 2a 20  are searched.** 
3efd0 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 75 73  for the table us
3efe0 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61 6c 67  ing the same alg
3eff0 6f 72 69 74 68 6d 20 75 73 65 64 20 62 79 20 74  orithm used by t
3f000 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 67 69  he database engi
3f010 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c 76 65  ne to.** resolve
3f020 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62   unqualified tab
3f030 6c 65 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a  le references..*
3f040 2a 0a 2a 2a 20 5e 54 68 65 20 74 68 69 72 64 20  *.** ^The third 
3f050 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d  and fourth param
3f060 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66 75  eters to this fu
3f070 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74  nction are the t
3f080 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a  able and column.
3f090 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64  ** name of the d
3f0a0 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c 20 72  esired column, r
3f0b0 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e 65 69  espectively. Nei
3f0c0 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 70 61  ther of these pa
3f0d0 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 79 20  rameters.** may 
3f0e0 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 5e  be NULL..**.** ^
3f0f0 4d 65 74 61 64 61 74 61 20 69 73 20 72 65 74 75  Metadata is retu
3f100 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 20  rned by writing 
3f110 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f  to the memory lo
3f120 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 61  cations passed a
3f130 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e 64  s the 5th.** and
3f140 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61   subsequent para
3f150 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66  meters to this f
3f160 75 6e 63 74 69 6f 6e 2e 20 5e 41 6e 79 20 6f 66  unction. ^Any of
3f170 20 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73   these arguments
3f180 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c   may be.** NULL,
3f190 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74   in which case t
3f1a0 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
3f1b0 20 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61   element of meta
3f1c0 64 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e  data is omitted.
3f1d0 0a 2a 2a 0a 2a 2a 20 5e 28 3c 62 6c 6f 63 6b 71  .**.** ^(<blockq
3f1e0 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20  uote>.** <table 
3f1f0 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c  border="1">.** <
3f200 74 72 3e 3c 74 68 3e 20 50 61 72 61 6d 65 74 65  tr><th> Paramete
3f210 72 20 3c 74 68 3e 20 4f 75 74 70 75 74 3c 62 72  r <th> Output<br
3f220 3e 54 79 70 65 20 3c 74 68 3e 20 20 44 65 73 63  >Type <th>  Desc
3f230 72 69 70 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74  ription.**.** <t
3f240 72 3e 3c 74 64 3e 20 35 74 68 20 3c 74 64 3e 20  r><td> 5th <td> 
3f250 63 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e  const char* <td>
3f260 20 44 61 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74   Data type.** <t
3f270 72 3e 3c 74 64 3e 20 36 74 68 20 3c 74 64 3e 20  r><td> 6th <td> 
3f280 63 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e  const char* <td>
3f290 20 4e 61 6d 65 20 6f 66 20 64 65 66 61 75 6c 74   Name of default
3f2a0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
3f2b0 6e 63 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  nce.** <tr><td> 
3f2c0 37 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20  7th <td> int    
3f2d0 20 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69       <td> True i
3f2e0 66 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e  f column has a N
3f2f0 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69  OT NULL constrai
3f300 6e 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38  nt.** <tr><td> 8
3f310 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20  th <td> int     
3f320 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66      <td> True if
3f330 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20   column is part 
3f340 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b  of the PRIMARY K
3f350 45 59 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39  EY.** <tr><td> 9
3f360 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20  th <td> int     
3f370 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66      <td> True if
3f380 20 63 6f 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f   column is [AUTO
3f390 49 4e 43 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f  INCREMENT].** </
3f3a0 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63  table>.** </bloc
3f3b0 6b 71 75 6f 74 65 3e 29 5e 0a 2a 2a 0a 2a 2a 20  kquote>)^.**.** 
3f3c0 5e 54 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e  ^The memory poin
3f3d0 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 63 68  ted to by the ch
3f3e0 61 72 61 63 74 65 72 20 70 6f 69 6e 74 65 72 73  aracter pointers
3f3f0 20 72 65 74 75 72 6e 65 64 20 66 6f 72 20 74 68   returned for th
3f400 65 0a 2a 2a 20 64 65 63 6c 61 72 61 74 69 6f 6e  e.** declaration
3f410 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74   type and collat
3f420 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20  ion sequence is 
3f430 76 61 6c 69 64 20 6f 6e 6c 79 20 75 6e 74 69 6c  valid only until
3f440 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 63 61 6c   the next.** cal
3f450 6c 20 74 6f 20 61 6e 79 20 53 51 4c 69 74 65 20  l to any SQLite 
3f460 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  API function..**
3f470 0a 2a 2a 20 5e 49 66 20 74 68 65 20 73 70 65 63  .** ^If the spec
3f480 69 66 69 65 64 20 74 61 62 6c 65 20 69 73 20 61  ified table is a
3f490 63 74 75 61 6c 6c 79 20 61 20 76 69 65 77 2c 20  ctually a view, 
3f4a0 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  an [error code] 
3f4b0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
3f4c0 2a 2a 20 5e 49 66 20 74 68 65 20 73 70 65 63 69  ** ^If the speci
3f4d0 66 69 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 22  fied column is "
3f4e0 72 6f 77 69 64 22 2c 20 22 6f 69 64 22 20 6f 72  rowid", "oid" or
3f4f0 20 22 5f 72 6f 77 69 64 5f 22 20 61 6e 64 20 61   "_rowid_" and a
3f500 6e 0a 2a 2a 20 5b 49 4e 54 45 47 45 52 20 50 52  n.** [INTEGER PR
3f510 49 4d 41 52 59 20 4b 45 59 5d 20 63 6f 6c 75 6d  IMARY KEY] colum
3f520 6e 20 68 61 73 20 62 65 65 6e 20 65 78 70 6c 69  n has been expli
3f530 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 2c 20  citly declared, 
3f540 74 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0a  then the output.
3f550 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72  ** parameters ar
3f560 65 20 73 65 74 20 66 6f 72 20 74 68 65 20 65 78  e set for the ex
3f570 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65  plicitly declare
3f580 64 20 63 6f 6c 75 6d 6e 2e 20 5e 28 49 66 20 74  d column. ^(If t
3f590 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 65 78  here is no.** ex
3f5a0 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65  plicitly declare
3f5b0 64 20 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41  d [INTEGER PRIMA
3f5c0 52 59 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e 2c 20  RY KEY] column, 
3f5d0 74 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0a  then the output.
3f5e0 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72  ** parameters ar
3f5f0 65 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73  e set as follows
3f600 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a  :.**.** <pre>.**
3f610 20 20 20 20 20 64 61 74 61 20 74 79 70 65 3a 20       data type: 
3f620 22 49 4e 54 45 47 45 52 22 0a 2a 2a 20 20 20 20  "INTEGER".**    
3f630 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
3f640 6e 63 65 3a 20 22 42 49 4e 41 52 59 22 0a 2a 2a  nce: "BINARY".**
3f650 20 20 20 20 20 6e 6f 74 20 6e 75 6c 6c 3a 20 30       not null: 0
3f660 0a 2a 2a 20 20 20 20 20 70 72 69 6d 61 72 79 20  .**     primary 
3f670 6b 65 79 3a 20 31 0a 2a 2a 20 20 20 20 20 61 75  key: 1.**     au
3f680 74 6f 20 69 6e 63 72 65 6d 65 6e 74 3a 20 30 0a  to increment: 0.
3f690 2a 2a 20 3c 2f 70 72 65 3e 29 5e 0a 2a 2a 0a 2a  ** </pre>)^.**.*
3f6a0 2a 20 5e 28 54 68 69 73 20 66 75 6e 63 74 69 6f  * ^(This functio
3f6b0 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e 65 20 6f  n may load one o
3f6c0 72 20 6d 6f 72 65 20 73 63 68 65 6d 61 73 20 66  r more schemas f
3f6d0 72 6f 6d 20 64 61 74 61 62 61 73 65 20 66 69 6c  rom database fil
3f6e0 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20 65 72 72  es. If an.** err
3f6f0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
3f700 20 74 68 69 73 20 70 72 6f 63 65 73 73 2c 20 6f   this process, o
3f710 72 20 69 66 20 74 68 65 20 72 65 71 75 65 73 74  r if the request
3f720 65 64 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75  ed table or colu
3f730 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20  mn.** cannot be 
3f740 66 6f 75 6e 64 2c 20 61 6e 20 5b 65 72 72 6f 72  found, an [error
3f750 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e   code] is return
3f760 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20  ed and an error 
3f770 6d 65 73 73 61 67 65 20 6c 65 66 74 0a 2a 2a 20  message left.** 
3f780 69 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  in the [database
3f790 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 6f   connection] (to
3f7a0 20 62 65 20 72 65 74 72 69 65 76 65 64 20 75 73   be retrieved us
3f7b0 69 6e 67 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  ing sqlite3_errm
3f7c0 73 67 28 29 29 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e  sg()).)^.**.** ^
3f7d0 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79  This API is only
3f7e0 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68   available if th
3f7f0 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f  e library was co
3f800 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a  mpiled with the.
3f810 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ** [SQLITE_ENABL
3f820 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54  E_COLUMN_METADAT
3f830 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f  A] C-preprocesso
3f840 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64  r symbol defined
3f850 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3f860 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c  int sqlite3_tabl
3f870 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74  e_column_metadat
3f880 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  a(.  sqlite3 *db
3f890 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3f8a0 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68   /* Connection h
3f8b0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
3f8c0 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20   char *zDbName, 
3f8d0 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
3f8e0 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20  se name or NULL 
3f8f0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
3f900 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20  *zTableName,    
3f910 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a   /* Table name *
3f920 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
3f930 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20  zColumnName,    
3f940 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a  /* Column name *
3f950 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  /.  char const *
3f960 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20  *pzDataType,    
3f970 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61  /* OUTPUT: Decla
3f980 72 65 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f  red data type */
3f990 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a  .  char const **
3f9a0 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f  pzCollSeq,     /
3f9b0 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74  * OUTPUT: Collat
3f9c0 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d  ion sequence nam
3f9d0 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74  e */.  int *pNot
3f9e0 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20  Null,           
3f9f0 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72     /* OUTPUT: Tr
3fa00 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63  ue if NOT NULL c
3fa10 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 73  onstraint exists
3fa20 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d   */.  int *pPrim
3fa30 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20  aryKey,         
3fa40 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75    /* OUTPUT: Tru
3fa50 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74  e if column part
3fa60 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20   of PK */.  int 
3fa70 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20  *pAutoinc       
3fa80 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55          /* OUTPU
3fa90 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d  T: True if colum
3faa0 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d  n is auto-increm
3fab0 65 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  ent */.);../*.**
3fac0 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 20   CAPI3REF: Load 
3fad0 41 6e 20 45 78 74 65 6e 73 69 6f 6e 0a 2a 2a 0a  An Extension.**.
3fae0 2a 2a 20 5e 54 68 69 73 20 69 6e 74 65 72 66 61  ** ^This interfa
3faf0 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 51 4c 69  ce loads an SQLi
3fb00 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62  te extension lib
3fb10 72 61 72 79 20 66 72 6f 6d 20 74 68 65 20 6e 61  rary from the na
3fb20 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  med file..**.** 
3fb30 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f 61  ^The sqlite3_loa
3fb40 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69 6e  d_extension() in
3fb50 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73  terface attempts
3fb60 20 74 6f 20 6c 6f 61 64 20 61 6e 0a 2a 2a 20 53   to load an.** S
3fb70 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20  QLite extension 
3fb80 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 65  library containe
3fb90 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 7a 46  d in the file zF
3fba0 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  ile..**.** ^The 
3fbb0 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73 20 7a  entry point is z
3fbc0 50 72 6f 63 2e 0a 2a 2a 20 5e 7a 50 72 6f 63 20  Proc..** ^zProc 
3fbd0 6d 61 79 20 62 65 20 30 2c 20 69 6e 20 77 68 69  may be 0, in whi
3fbe0 63 68 20 63 61 73 65 20 74 68 65 20 6e 61 6d 65  ch case the name
3fbf0 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 70 6f   of the entry po
3fc00 69 6e 74 0a 2a 2a 20 64 65 66 61 75 6c 74 73 20  int.** defaults 
3fc10 74 6f 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65  to "sqlite3_exte
3fc20 6e 73 69 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 20  nsion_init"..** 
3fc30 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f 61  ^The sqlite3_loa
3fc40 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69 6e  d_extension() in
3fc50 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 0a  terface returns.
3fc60 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f  ** [SQLITE_OK] o
3fc70 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 5b 53  n success and [S
3fc80 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 20  QLITE_ERROR] if 
3fc90 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77  something goes w
3fca0 72 6f 6e 67 2e 0a 2a 2a 20 5e 49 66 20 61 6e 20  rong..** ^If an 
3fcb0 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 64  error occurs and
3fcc0 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e 6f 74   pzErrMsg is not
3fcd0 20 30 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20   0, then the.** 
3fce0 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78  [sqlite3_load_ex
3fcf0 74 65 6e 73 69 6f 6e 28 29 5d 20 69 6e 74 65 72  tension()] inter
3fd00 66 61 63 65 20 73 68 61 6c 6c 20 61 74 74 65 6d  face shall attem
3fd10 70 74 20 74 6f 0a 2a 2a 20 66 69 6c 6c 20 2a 70  pt to.** fill *p
3fd20 7a 45 72 72 4d 73 67 20 77 69 74 68 20 65 72 72  zErrMsg with err
3fd30 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20  or message text 
3fd40 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79  stored in memory
3fd50 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  .** obtained fro
3fd60 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  m [sqlite3_mallo
3fd70 63 28 29 5d 2e 20 54 68 65 20 63 61 6c 6c 69 6e  c()]. The callin
3fd80 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 68  g function.** sh
3fd90 6f 75 6c 64 20 66 72 65 65 20 74 68 69 73 20 6d  ould free this m
3fda0 65 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67  emory by calling
3fdb0 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
3fdc0 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 45 78 74 65 6e 73  ]..**.** ^Extens
3fdd0 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6d 75 73 74  ion loading must
3fde0 20 62 65 20 65 6e 61 62 6c 65 64 20 75 73 69 6e   be enabled usin
3fdf0 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 6e  g.** [sqlite3_en
3fe00 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73  able_load_extens
3fe10 69 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f 20  ion()] prior to 
3fe20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 41 50 49  calling this API
3fe30 2c 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 61  ,.** otherwise a
3fe40 6e 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20  n error will be 
3fe50 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
3fe60 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 6c 6f  See also the [lo
3fe70 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 53  ad_extension() S
3fe80 51 4c 20 66 75 6e 63 74 69 6f 6e 5d 2e 0a 2a 2f  QL function]..*/
3fe90 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
3fea0 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74  sqlite3_load_ext
3feb0 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  ension(.  sqlite
3fec0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
3fed0 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65  /* Load the exte
3fee0 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20  nsion into this 
3fef0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3ff00 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ion */.  const c
3ff10 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f  har *zFile,    /
3ff20 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68  * Name of the sh
3ff30 61 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e  ared library con
3ff40 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f  taining extensio
3ff50 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  n */.  const cha
3ff60 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20  r *zProc,    /* 
3ff70 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 44 65  Entry point.  De
3ff80 72 69 76 65 64 20 66 72 6f 6d 20 7a 46 69 6c 65  rived from zFile
3ff90 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72 20   if 0 */.  char 
3ffa0 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20  **pzErrMsg      
3ffb0 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d 65   /* Put error me
3ffc0 73 73 61 67 65 20 68 65 72 65 20 69 66 20 6e 6f  ssage here if no
3ffd0 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  t 0 */.);../*.**
3ffe0 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c   CAPI3REF: Enabl
3fff0 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74  e Or Disable Ext
40000 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 0a 2a  ension Loading.*
40010 2a 0a 2a 2a 20 5e 53 6f 20 61 73 20 6e 6f 74 20  *.** ^So as not 
40020 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 69 74 79  to open security
40030 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65 72 20   holes in older 
40040 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  applications tha
40050 74 20 61 72 65 0a 2a 2a 20 75 6e 70 72 65 70 61  t are.** unprepa
40060 72 65 64 20 74 6f 20 64 65 61 6c 20 77 69 74 68  red to deal with
40070 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69   extension loadi
40080 6e 67 2c 20 61 6e 64 20 61 73 20 61 20 6d 65 61  ng, and as a mea
40090 6e 73 20 6f 66 20 64 69 73 61 62 6c 69 6e 67 0a  ns of disabling.
400a0 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61  ** extension loa
400b0 64 69 6e 67 20 77 68 69 6c 65 20 65 76 61 6c 75  ding while evalu
400c0 61 74 69 6e 67 20 75 73 65 72 2d 65 6e 74 65 72  ating user-enter
400d0 65 64 20 53 51 4c 2c 20 74 68 65 20 66 6f 6c 6c  ed SQL, the foll
400e0 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20 69 73 20  owing API.** is 
400f0 70 72 6f 76 69 64 65 64 20 74 6f 20 74 75 72 6e  provided to turn
40100 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 6f   the [sqlite3_lo
40110 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20  ad_extension()] 
40120 6d 65 63 68 61 6e 69 73 6d 20 6f 6e 20 61 6e 64  mechanism on and
40130 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 5e 45 78 74   off..**.** ^Ext
40140 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69  ension loading i
40150 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74  s off by default
40160 2e 20 53 65 65 20 74 69 63 6b 65 74 20 23 31 38  . See ticket #18
40170 36 33 2e 0a 2a 2a 20 5e 43 61 6c 6c 20 74 68 65  63..** ^Call the
40180 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
40190 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29  load_extension()
401a0 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 6f 6e   routine with on
401b0 6f 66 66 3d 3d 31 0a 2a 2a 20 74 6f 20 74 75 72  off==1.** to tur
401c0 6e 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64  n extension load
401d0 69 6e 67 20 6f 6e 20 61 6e 64 20 63 61 6c 6c 20  ing on and call 
401e0 69 74 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 30  it with onoff==0
401f0 20 74 6f 20 74 75 72 6e 0a 2a 2a 20 69 74 20 62   to turn.** it b
40200 61 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a 2a  ack off again..*
40210 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
40220 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
40230 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73  load_extension(s
40240 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
40250 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  onoff);../*.** C
40260 41 50 49 33 52 45 46 3a 20 41 75 74 6f 6d 61 74  API3REF: Automat
40270 69 63 61 6c 6c 79 20 4c 6f 61 64 20 53 74 61 74  ically Load Stat
40280 69 63 61 6c 6c 79 20 4c 69 6e 6b 65 64 20 45 78  ically Linked Ex
40290 74 65 6e 73 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 5e  tensions.**.** ^
402a0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 63  This interface c
402b0 61 75 73 65 73 20 74 68 65 20 78 45 6e 74 72 79  auses the xEntry
402c0 50 6f 69 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e  Point() function
402d0 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 66   to be invoked f
402e0 6f 72 0a 2a 2a 20 65 61 63 68 20 6e 65 77 20 5b  or.** each new [
402f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
40300 69 6f 6e 5d 20 74 68 61 74 20 69 73 20 63 72 65  ion] that is cre
40310 61 74 65 64 2e 20 20 54 68 65 20 69 64 65 61 20  ated.  The idea 
40320 68 65 72 65 20 69 73 20 74 68 61 74 0a 2a 2a 20  here is that.** 
40330 78 45 6e 74 72 79 50 6f 69 6e 74 28 29 20 69 73  xEntryPoint() is
40340 20 74 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74   the entry point
40350 20 66 6f 72 20 61 20 73 74 61 74 69 63 61 6c 6c   for a staticall
40360 79 20 6c 69 6e 6b 65 64 20 53 51 4c 69 74 65 20  y linked SQLite 
40370 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 74 68 61  extension.** tha
40380 74 20 69 73 20 74 6f 20 62 65 20 61 75 74 6f 6d  t is to be autom
40390 61 74 69 63 61 6c 6c 79 20 6c 6f 61 64 65 64 20  atically loaded 
403a0 69 6e 74 6f 20 61 6c 6c 20 6e 65 77 20 64 61 74  into all new dat
403b0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
403c0 73 2e 0a 2a 2a 0a 2a 2a 20 5e 28 45 76 65 6e 20  s..**.** ^(Even 
403d0 74 68 6f 75 67 68 20 74 68 65 20 66 75 6e 63 74  though the funct
403e0 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 20 73 68  ion prototype sh
403f0 6f 77 73 20 74 68 61 74 20 78 45 6e 74 72 79 50  ows that xEntryP
40400 6f 69 6e 74 28 29 20 74 61 6b 65 73 0a 2a 2a 20  oint() takes.** 
40410 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64  no arguments and
40420 20 72 65 74 75 72 6e 73 20 76 6f 69 64 2c 20 53   returns void, S
40430 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 78 45  QLite invokes xE
40440 6e 74 72 79 50 6f 69 6e 74 28 29 20 77 69 74 68  ntryPoint() with
40450 20 74 68 72 65 65 0a 2a 2a 20 61 72 67 75 6d 65   three.** argume
40460 6e 74 73 20 61 6e 64 20 65 78 70 65 63 74 73 20  nts and expects 
40470 61 6e 64 20 69 6e 74 65 67 65 72 20 72 65 73 75  and integer resu
40480 6c 74 20 61 73 20 69 66 20 74 68 65 20 73 69 67  lt as if the sig
40490 6e 61 74 75 72 65 20 6f 66 20 74 68 65 0a 2a 2a  nature of the.**
404a0 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 77 68 65   entry point whe
404b0 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  re as follows:.*
404c0 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
404d0 3e 3c 70 72 65 3e 0a 2a 2a 20 26 6e 62 73 70 3b  ><pre>.** &nbsp;
404e0 20 20 69 6e 74 20 78 45 6e 74 72 79 50 6f 69 6e    int xEntryPoin
404f0 74 28 0a 2a 2a 20 26 6e 62 73 70 3b 20 20 20 20  t(.** &nbsp;    
40500 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 2a 2a 20  sqlite3 *db,.** 
40510 26 6e 62 73 70 3b 20 20 20 20 63 6f 6e 73 74 20  &nbsp;    const 
40520 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c  char **pzErrMsg,
40530 0a 2a 2a 20 26 6e 62 73 70 3b 20 20 20 20 63 6f  .** &nbsp;    co
40540 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74  nst struct sqlit
40550 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20  e3_api_routines 
40560 2a 70 54 68 75 6e 6b 0a 2a 2a 20 26 6e 62 73 70  *pThunk.** &nbsp
40570 3b 20 20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  ;  );.** </pre><
40580 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 29 5e 0a 2a  /blockquote>)^.*
40590 2a 0a 2a 2a 20 49 66 20 74 68 65 20 78 45 6e 74  *.** If the xEnt
405a0 72 79 50 6f 69 6e 74 20 72 6f 75 74 69 6e 65 20  ryPoint routine 
405b0 65 6e 63 6f 75 6e 74 65 72 73 20 61 6e 20 65 72  encounters an er
405c0 72 6f 72 2c 20 69 74 20 73 68 6f 75 6c 64 20 6d  ror, it should m
405d0 61 6b 65 20 2a 70 7a 45 72 72 4d 73 67 0a 2a 2a  ake *pzErrMsg.**
405e0 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 61 70 70   point to an app
405f0 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d  ropriate error m
40600 65 73 73 61 67 65 20 28 6f 62 74 61 69 6e 65 64  essage (obtained
40610 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d   from [sqlite3_m
40620 70 72 69 6e 74 66 28 29 5d 29 0a 2a 2a 20 61 6e  printf()]).** an
40630 64 20 72 65 74 75 72 6e 20 61 6e 20 61 70 70 72  d return an appr
40640 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72 20 63  opriate [error c
40650 6f 64 65 5d 2e 20 20 5e 53 51 4c 69 74 65 20 65  ode].  ^SQLite e
40660 6e 73 75 72 65 73 20 74 68 61 74 20 2a 70 7a 45  nsures that *pzE
40670 72 72 4d 73 67 0a 2a 2a 20 69 73 20 4e 55 4c 4c  rrMsg.** is NULL
40680 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20   before calling 
40690 74 68 65 20 78 45 6e 74 72 79 50 6f 69 6e 74 28  the xEntryPoint(
406a0 29 2e 20 20 5e 53 51 4c 69 74 65 20 77 69 6c 6c  ).  ^SQLite will
406b0 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b 73 71 6c 69   invoke.** [sqli
406c0 74 65 33 5f 66 72 65 65 28 29 5d 20 6f 6e 20 2a  te3_free()] on *
406d0 70 7a 45 72 72 4d 73 67 20 61 66 74 65 72 20 78  pzErrMsg after x
406e0 45 6e 74 72 79 50 6f 69 6e 74 28 29 20 72 65 74  EntryPoint() ret
406f0 75 72 6e 73 2e 20 20 5e 49 66 20 61 6e 79 0a 2a  urns.  ^If any.*
40700 2a 20 78 45 6e 74 72 79 50 6f 69 6e 74 28 29 20  * xEntryPoint() 
40710 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72  returns an error
40720 2c 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f  , the [sqlite3_o
40730 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  pen()], [sqlite3
40740 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 6f  _open16()],.** o
40750 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  r [sqlite3_open_
40760 76 32 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20  v2()] call that 
40770 70 72 6f 76 6f 6b 65 64 20 74 68 65 20 78 45 6e  provoked the xEn
40780 74 72 79 50 6f 69 6e 74 28 29 20 77 69 6c 6c 20  tryPoint() will 
40790 66 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 5e 43 61 6c  fail..**.** ^Cal
407a0 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 61 75 74  ling sqlite3_aut
407b0 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 58 29 20 77  o_extension(X) w
407c0 69 74 68 20 61 6e 20 65 6e 74 72 79 20 70 6f 69  ith an entry poi
407d0 6e 74 20 58 20 74 68 61 74 20 69 73 20 61 6c 72  nt X that is alr
407e0 65 61 64 79 0a 2a 2a 20 6f 6e 20 74 68 65 20 6c  eady.** on the l
407f0 69 73 74 20 6f 66 20 61 75 74 6f 6d 61 74 69 63  ist of automatic
40800 20 65 78 74 65 6e 73 69 6f 6e 73 20 69 73 20 61   extensions is a
40810 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e   harmless no-op.
40820 20 5e 4e 6f 20 65 6e 74 72 79 20 70 6f 69 6e 74   ^No entry point
40830 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c  .** will be call
40840 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ed more than onc
40850 65 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62  e for each datab
40860 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
40870 68 61 74 20 69 73 20 6f 70 65 6e 65 64 2e 0a 2a  hat is opened..*
40880 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  *.** See also: [
40890 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75  sqlite3_reset_au
408a0 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 2e  to_extension()].
408b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
408c0 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f  nt sqlite3_auto_
408d0 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28  extension(void (
408e0 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 76 6f  *xEntryPoint)(vo
408f0 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  id));../*.** CAP
40900 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 75 74  I3REF: Reset Aut
40910 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f 6e  omatic Extension
40920 20 4c 6f 61 64 69 6e 67 0a 2a 2a 0a 2a 2a 20 5e   Loading.**.** ^
40930 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 64  This interface d
40940 69 73 61 62 6c 65 73 20 61 6c 6c 20 61 75 74 6f  isables all auto
40950 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73  matic extensions
40960 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 72   previously.** r
40970 65 67 69 73 74 65 72 65 64 20 75 73 69 6e 67 20  egistered using 
40980 5b 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78  [sqlite3_auto_ex
40990 74 65 6e 73 69 6f 6e 28 29 5d 2e 0a 2a 2f 0a 53  tension()]..*/.S
409a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
409b0 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74  qlite3_reset_aut
409c0 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64  o_extension(void
409d0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e  );../*.** The in
409e0 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76  terface to the v
409f0 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63  irtual-table mec
40a00 68 61 6e 69 73 6d 20 69 73 20 63 75 72 72 65 6e  hanism is curren
40a10 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a  tly considered.*
40a20 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d 65  * to be experime
40a30 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 72  ntal.  The inter
40a40 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e 67  face might chang
40a50 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c  e in incompatibl
40a60 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68  e ways..** If th
40a70 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20  is is a problem 
40a80 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20  for you, do not 
40a90 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 63  use the interfac
40aa0 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a  e at this time..
40ab0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76  **.** When the v
40ac0 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63  irtual-table mec
40ad0 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65  hanism stabilize
40ae0 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61  s, we will decla
40af0 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66  re the.** interf
40b00 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 6f  ace fixed, suppo
40b10 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 65  rt it indefinite
40b20 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 74  ly, and remove t
40b30 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a  his comment..*/.
40b40 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65  ./*.** Structure
40b50 73 20 75 73 65 64 20 62 79 20 74 68 65 20 76 69  s used by the vi
40b60 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74 65  rtual table inte
40b70 72 66 61 63 65 0a 2a 2f 0a 74 79 70 65 64 65 66  rface.*/.typedef
40b80 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
40b90 76 74 61 62 20 73 71 6c 69 74 65 33 5f 76 74 61  vtab sqlite3_vta
40ba0 62 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  b;.typedef struc
40bb0 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
40bc0 69 6e 66 6f 20 73 71 6c 69 74 65 33 5f 69 6e 64  info sqlite3_ind
40bd0 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 65 64 65 66  ex_info;.typedef
40be0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
40bf0 76 74 61 62 5f 63 75 72 73 6f 72 20 73 71 6c 69  vtab_cursor sqli
40c00 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 3b  te3_vtab_cursor;
40c10 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
40c20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 73  sqlite3_module s
40c30 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 3b 0a 0a  qlite3_module;..
40c40 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
40c50 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 4f 62  Virtual Table Ob
40c60 6a 65 63 74 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  ject.** KEYWORDS
40c70 3a 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  : sqlite3_module
40c80 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   {virtual table 
40c90 6d 6f 64 75 6c 65 7d 0a 2a 2a 0a 2a 2a 20 54 68  module}.**.** Th
40ca0 69 73 20 73 74 72 75 63 74 75 72 65 2c 20 73 6f  is structure, so
40cb0 6d 65 74 69 6d 65 73 20 63 61 6c 6c 65 64 20 61  metimes called a
40cc0 20 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   "virtual table 
40cd0 6d 6f 64 75 6c 65 22 2c 20 0a 2a 2a 20 64 65 66  module", .** def
40ce0 69 6e 65 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  ines the impleme
40cf0 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 5b 76 69  ntation of a [vi
40d00 72 74 75 61 6c 20 74 61 62 6c 65 73 5d 2e 20 20  rtual tables].  
40d10 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75  .** This structu
40d20 72 65 20 63 6f 6e 73 69 73 74 73 20 6d 6f 73 74  re consists most
40d30 6c 79 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f  ly of methods fo
40d40 72 20 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a  r the module..**
40d50 0a 2a 2a 20 5e 41 20 76 69 72 74 75 61 6c 20 74  .** ^A virtual t
40d60 61 62 6c 65 20 6d 6f 64 75 6c 65 20 69 73 20 63  able module is c
40d70 72 65 61 74 65 64 20 62 79 20 66 69 6c 6c 69 6e  reated by fillin
40d80 67 20 69 6e 20 61 20 70 65 72 73 69 73 74 65 6e  g in a persisten
40d90 74 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66  t.** instance of
40da0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
40db0 61 6e 64 20 70 61 73 73 69 6e 67 20 61 20 70 6f  and passing a po
40dc0 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 69 6e  inter to that in
40dd0 73 74 61 6e 63 65 0a 2a 2a 20 74 6f 20 5b 73 71  stance.** to [sq
40de0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
40df0 75 6c 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  ule()] or [sqlit
40e00 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
40e10 5f 76 32 28 29 5d 2e 0a 2a 2a 20 5e 54 68 65 20  _v2()]..** ^The 
40e20 72 65 67 69 73 74 72 61 74 69 6f 6e 20 72 65 6d  registration rem
40e30 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 69 6c  ains valid until
40e40 20 69 74 20 69 73 20 72 65 70 6c 61 63 65 64 20   it is replaced 
40e50 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a  by a different.*
40e60 2a 20 6d 6f 64 75 6c 65 20 6f 72 20 75 6e 74 69  * module or unti
40e70 6c 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  l the [database 
40e80 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 63 6c 6f 73  connection] clos
40e90 65 73 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74  es.  The content
40ea0 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75  .** of this stru
40eb0 63 74 75 72 65 20 6d 75 73 74 20 6e 6f 74 20 63  cture must not c
40ec0 68 61 6e 67 65 20 77 68 69 6c 65 20 69 74 20 69  hange while it i
40ed0 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74  s registered wit
40ee0 68 0a 2a 2a 20 61 6e 79 20 64 61 74 61 62 61 73  h.** any databas
40ef0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  e connection..*/
40f00 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
40f10 6d 6f 64 75 6c 65 20 7b 0a 20 20 69 6e 74 20 69  module {.  int i
40f20 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 28  Version;.  int (
40f30 2a 78 43 72 65 61 74 65 29 28 73 71 6c 69 74 65  *xCreate)(sqlite
40f40 33 2a 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a  3*, void *pAux,.
40f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
40f60 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63  nt argc, const c
40f70 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c  har *const*argv,
40f80 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
40f90 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70  sqlite3_vtab **p
40fa0 70 56 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a  pVTab, char**);.
40fb0 20 20 69 6e 74 20 28 2a 78 43 6f 6e 6e 65 63 74    int (*xConnect
40fc0 29 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64  )(sqlite3*, void
40fd0 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20   *pAux,.        
40fe0 20 20 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c         int argc,
40ff0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
41000 73 74 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20  st*argv,.       
41010 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
41020 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 63  vtab **ppVTab, c
41030 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  har**);.  int (*
41040 78 42 65 73 74 49 6e 64 65 78 29 28 73 71 6c 69  xBestIndex)(sqli
41050 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c  te3_vtab *pVTab,
41060 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
41070 6e 66 6f 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78  nfo*);.  int (*x
41080 44 69 73 63 6f 6e 6e 65 63 74 29 28 73 71 6c 69  Disconnect)(sqli
41090 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29  te3_vtab *pVTab)
410a0 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 73 74 72  ;.  int (*xDestr
410b0 6f 79 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  oy)(sqlite3_vtab
410c0 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20   *pVTab);.  int 
410d0 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74 65 33  (*xOpen)(sqlite3
410e0 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71  _vtab *pVTab, sq
410f0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
41100 72 20 2a 2a 70 70 43 75 72 73 6f 72 29 3b 0a 20  r **ppCursor);. 
41110 20 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28 73   int (*xClose)(s
41120 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
41130 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46  or*);.  int (*xF
41140 69 6c 74 65 72 29 28 73 71 6c 69 74 65 33 5f 76  ilter)(sqlite3_v
41150 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 69 6e 74  tab_cursor*, int
41160 20 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74 20 63   idxNum, const c
41170 68 61 72 20 2a 69 64 78 53 74 72 2c 0a 20 20 20  har *idxStr,.   
41180 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
41190 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
411a0 61 6c 75 65 20 2a 2a 61 72 67 76 29 3b 0a 20 20  alue **argv);.  
411b0 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 73 71 6c  int (*xNext)(sql
411c0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
411d0 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 45 6f 66  *);.  int (*xEof
411e0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  )(sqlite3_vtab_c
411f0 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28  ursor*);.  int (
41200 2a 78 43 6f 6c 75 6d 6e 29 28 73 71 6c 69 74 65  *xColumn)(sqlite
41210 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20  3_vtab_cursor*, 
41220 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
41230 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a  , int);.  int (*
41240 78 52 6f 77 69 64 29 28 73 71 6c 69 74 65 33 5f  xRowid)(sqlite3_
41250 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 73 71  vtab_cursor*, sq
41260 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 52 6f  lite3_int64 *pRo
41270 77 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55  wid);.  int (*xU
41280 70 64 61 74 65 29 28 73 71 6c 69 74 65 33 5f 76  pdate)(sqlite3_v
41290 74 61 62 20 2a 2c 20 69 6e 74 2c 20 73 71 6c 69  tab *, int, sqli
412a0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 2c 20 73 71  te3_value **, sq
412b0 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 29 3b 0a  lite3_int64 *);.
412c0 20 20 69 6e 74 20 28 2a 78 42 65 67 69 6e 29 28    int (*xBegin)(
412d0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
412e0 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53  Tab);.  int (*xS
412f0 79 6e 63 29 28 73 71 6c 69 74 65 33 5f 76 74 61  ync)(sqlite3_vta
41300 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74  b *pVTab);.  int
41310 20 28 2a 78 43 6f 6d 6d 69 74 29 28 73 71 6c 69   (*xCommit)(sqli
41320 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29  te3_vtab *pVTab)
41330 3b 0a 20 20 69 6e 74 20 28 2a 78 52 6f 6c 6c 62  ;.  int (*xRollb
41340 61 63 6b 29 28 73 71 6c 69 74 65 33 5f 76 74 61  ack)(sqlite3_vta
41350 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74  b *pVTab);.  int
41360 20 28 2a 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e   (*xFindFunction
41370 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
41380 70 56 74 61 62 2c 20 69 6e 74 20 6e 41 72 67 2c  pVtab, int nArg,
41390 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
413a0 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  me,.            
413b0 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 20             void 
413c0 28 2a 2a 70 78 46 75 6e 63 29 28 73 71 6c 69 74  (**pxFunc)(sqlit
413d0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
413e0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
413f0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
41400 20 20 20 20 20 20 20 20 20 76 6f 69 64 20 2a 2a           void **
41410 70 70 41 72 67 29 3b 0a 20 20 69 6e 74 20 28 2a  ppArg);.  int (*
41420 78 52 65 6e 61 6d 65 29 28 73 71 6c 69 74 65 33  xRename)(sqlite3
41430 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20 63 6f  _vtab *pVtab, co
41440 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65 77 29 3b  nst char *zNew);
41450 0a 20 20 2f 2a 20 54 68 65 20 6d 65 74 68 6f 64  .  /* The method
41460 73 20 61 62 6f 76 65 20 61 72 65 20 69 6e 20 76  s above are in v
41470 65 72 73 69 6f 6e 20 31 20 6f 66 20 74 68 65 20  ersion 1 of the 
41480 73 71 6c 69 74 65 5f 6d 6f 64 75 6c 65 20 6f 62  sqlite_module ob
41490 6a 65 63 74 2e 20 54 68 6f 73 65 20 0a 20 20 2a  ject. Those .  *
414a0 2a 20 62 65 6c 6f 77 20 61 72 65 20 66 6f 72 20  * below are for 
414b0 76 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 67 72  version 2 and gr
414c0 65 61 74 65 72 2e 20 2a 2f 0a 20 20 69 6e 74 20  eater. */.  int 
414d0 28 2a 78 53 61 76 65 70 6f 69 6e 74 29 28 73 71  (*xSavepoint)(sq
414e0 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61  lite3_vtab *pVTa
414f0 62 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28  b, int);.  int (
41500 2a 78 52 65 6c 65 61 73 65 29 28 73 71 6c 69 74  *xRelease)(sqlit
41510 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20  e3_vtab *pVTab, 
41520 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52  int);.  int (*xR
41530 6f 6c 6c 62 61 63 6b 54 6f 29 28 73 71 6c 69 74  ollbackTo)(sqlit
41540 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20  e3_vtab *pVTab, 
41550 69 6e 74 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  int);.};../*.** 
41560 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61  CAPI3REF: Virtua
41570 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67  l Table Indexing
41580 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   Information.** 
41590 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65  KEYWORDS: sqlite
415a0 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a 0a  3_index_info.**.
415b0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69  ** The sqlite3_i
415c0 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74  ndex_info struct
415d0 75 72 65 20 61 6e 64 20 69 74 73 20 73 75 62 73  ure and its subs
415e0 74 72 75 63 74 75 72 65 73 20 69 73 20 75 73 65  tructures is use
415f0 64 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20  d as part.** of 
41600 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62  the [virtual tab
41610 6c 65 5d 20 69 6e 74 65 72 66 61 63 65 20 74 6f  le] interface to
41620 0a 2a 2a 20 70 61 73 73 20 69 6e 66 6f 72 6d 61  .** pass informa
41630 74 69 6f 6e 20 69 6e 74 6f 20 61 6e 64 20 72 65  tion into and re
41640 63 65 69 76 65 20 74 68 65 20 72 65 70 6c 79 20  ceive the reply 
41650 66 72 6f 6d 20 74 68 65 20 5b 78 42 65 73 74 49  from the [xBestI
41660 6e 64 65 78 5d 0a 2a 2a 20 6d 65 74 68 6f 64 20  ndex].** method 
41670 6f 66 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61  of a [virtual ta
41680 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 54 68  ble module].  Th
41690 65 20 66 69 65 6c 64 73 20 75 6e 64 65 72 20 2a  e fields under *
416a0 2a 49 6e 70 75 74 73 2a 2a 20 61 72 65 20 74 68  *Inputs** are th
416b0 65 0a 2a 2a 20 69 6e 70 75 74 73 20 74 6f 20 78  e.** inputs to x
416c0 42 65 73 74 49 6e 64 65 78 20 61 6e 64 20 61 72  BestIndex and ar
416d0 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 78 42  e read-only.  xB
416e0 65 73 74 49 6e 64 65 78 20 69 6e 73 65 72 74 73  estIndex inserts
416f0 20 69 74 73 0a 2a 2a 20 72 65 73 75 6c 74 73 20   its.** results 
41700 69 6e 74 6f 20 74 68 65 20 2a 2a 4f 75 74 70 75  into the **Outpu
41710 74 73 2a 2a 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a  ts** fields..**.
41720 2a 2a 20 5e 28 54 68 65 20 61 43 6f 6e 73 74 72  ** ^(The aConstr
41730 61 69 6e 74 5b 5d 20 61 72 72 61 79 20 72 65 63  aint[] array rec
41740 6f 72 64 73 20 57 48 45 52 45 20 63 6c 61 75 73  ords WHERE claus
41750 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 66  e constraints of
41760 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a   the form:.**.**
41770 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 63 6f 6c   <blockquote>col
41780 75 6d 6e 20 4f 50 20 65 78 70 72 3c 2f 62 6c 6f  umn OP expr</blo
41790 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 77  ckquote>.**.** w
417a0 68 65 72 65 20 4f 50 20 69 73 20 3d 2c 20 26 6c  here OP is =, &l
417b0 74 3b 2c 20 26 6c 74 3b 3d 2c 20 26 67 74 3b 2c  t;, &lt;=, &gt;,
417c0 20 6f 72 20 26 67 74 3b 3d 2e 29 5e 20 20 5e 28   or &gt;=.)^  ^(
417d0 54 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 6f  The particular o
417e0 70 65 72 61 74 6f 72 20 69 73 0a 2a 2a 20 73 74  perator is.** st
417f0 6f 72 65 64 20 69 6e 20 61 43 6f 6e 73 74 72 61  ored in aConstra
41800 69 6e 74 5b 5d 2e 6f 70 20 75 73 69 6e 67 20 6f  int[].op using o
41810 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 53 51  ne of the.** [SQ
41820 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
41830 52 41 49 4e 54 5f 45 51 20 7c 20 53 51 4c 49 54  RAINT_EQ | SQLIT
41840 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
41850 4e 54 5f 20 76 61 6c 75 65 73 5d 2e 29 5e 0a 2a  NT_ values].)^.*
41860 2a 20 5e 28 54 68 65 20 69 6e 64 65 78 20 6f 66  * ^(The index of
41870 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 73   the column is s
41880 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 61 43 6f 6e  tored in.** aCon
41890 73 74 72 61 69 6e 74 5b 5d 2e 69 43 6f 6c 75 6d  straint[].iColum
418a0 6e 2e 29 5e 20 20 5e 28 61 43 6f 6e 73 74 72 61  n.)^  ^(aConstra
418b0 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 69 73 20  int[].usable is 
418c0 54 52 55 45 20 69 66 20 74 68 65 0a 2a 2a 20 65  TRUE if the.** e
418d0 78 70 72 20 6f 6e 20 74 68 65 20 72 69 67 68 74  xpr on the right
418e0 2d 68 61 6e 64 20 73 69 64 65 20 63 61 6e 20 62  -hand side can b
418f0 65 20 65 76 61 6c 75 61 74 65 64 20 28 61 6e 64  e evaluated (and
41900 20 74 68 75 73 20 74 68 65 20 63 6f 6e 73 74 72   thus the constr
41910 61 69 6e 74 0a 2a 2a 20 69 73 20 75 73 61 62 6c  aint.** is usabl
41920 65 29 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20  e) and false if 
41930 69 74 20 63 61 6e 6e 6f 74 2e 29 5e 0a 2a 2a 0a  it cannot.)^.**.
41940 2a 2a 20 5e 54 68 65 20 6f 70 74 69 6d 69 7a 65  ** ^The optimize
41950 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  r automatically 
41960 69 6e 76 65 72 74 73 20 74 65 72 6d 73 20 6f 66  inverts terms of
41970 20 74 68 65 20 66 6f 72 6d 20 22 65 78 70 72 20   the form "expr 
41980 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a 20 61 6e  OP column".** an
41990 64 20 6d 61 6b 65 73 20 6f 74 68 65 72 20 73 69  d makes other si
419a0 6d 70 6c 69 66 69 63 61 74 69 6f 6e 73 20 74 6f  mplifications to
419b0 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
419c0 65 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 74 20  e in an attempt 
419d0 74 6f 0a 2a 2a 20 67 65 74 20 61 73 20 6d 61 6e  to.** get as man
419e0 79 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  y WHERE clause t
419f0 65 72 6d 73 20 69 6e 74 6f 20 74 68 65 20 66 6f  erms into the fo
41a00 72 6d 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 61  rm shown above a
41a10 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20 5e  s possible..** ^
41a20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  The aConstraint[
41a30 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20 72 65 70  ] array only rep
41a40 6f 72 74 73 20 57 48 45 52 45 20 63 6c 61 75 73  orts WHERE claus
41a50 65 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 65  e terms that are
41a60 0a 2a 2a 20 72 65 6c 65 76 61 6e 74 20 74 6f 20  .** relevant to 
41a70 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76  the particular v
41a80 69 72 74 75 61 6c 20 74 61 62 6c 65 20 62 65 69  irtual table bei
41a90 6e 67 20 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a  ng queried..**.*
41aa0 2a 20 5e 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  * ^Information a
41ab0 62 6f 75 74 20 74 68 65 20 4f 52 44 45 52 20 42  bout the ORDER B
41ac0 59 20 63 6c 61 75 73 65 20 69 73 20 73 74 6f 72  Y clause is stor
41ad0 65 64 20 69 6e 20 61 4f 72 64 65 72 42 79 5b 5d  ed in aOrderBy[]
41ae0 2e 0a 2a 2a 20 5e 45 61 63 68 20 74 65 72 6d 20  ..** ^Each term 
41af0 6f 66 20 61 4f 72 64 65 72 42 79 20 72 65 63 6f  of aOrderBy reco
41b00 72 64 73 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20  rds a column of 
41b10 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
41b20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b  use..**.** The [
41b30 78 42 65 73 74 49 6e 64 65 78 5d 20 6d 65 74 68  xBestIndex] meth
41b40 6f 64 20 6d 75 73 74 20 66 69 6c 6c 20 61 43 6f  od must fill aCo
41b50 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 20  nstraintUsage[] 
41b60 77 69 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  with information
41b70 0a 2a 2a 20 61 62 6f 75 74 20 77 68 61 74 20 70  .** about what p
41b80 61 72 61 6d 65 74 65 72 73 20 74 6f 20 70 61 73  arameters to pas
41b90 73 20 74 6f 20 78 46 69 6c 74 65 72 2e 20 20 5e  s to xFilter.  ^
41ba0 49 66 20 61 72 67 76 49 6e 64 65 78 3e 30 20 74  If argvIndex>0 t
41bb0 68 65 6e 0a 2a 2a 20 74 68 65 20 72 69 67 68 74  hen.** the right
41bc0 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68  -hand side of th
41bd0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
41be0 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 69 73  aConstraint[] is
41bf0 20 65 76 61 6c 75 61 74 65 64 0a 2a 2a 20 61 6e   evaluated.** an
41c00 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 61 72  d becomes the ar
41c10 67 76 49 6e 64 65 78 2d 74 68 20 65 6e 74 72 79  gvIndex-th entry
41c20 20 69 6e 20 61 72 67 76 2e 20 20 5e 28 49 66 20   in argv.  ^(If 
41c30 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
41c40 5b 5d 2e 6f 6d 69 74 0a 2a 2a 20 69 73 20 74 72  [].omit.** is tr
41c50 75 65 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e  ue, then the con
41c60 73 74 72 61 69 6e 74 20 69 73 20 61 73 73 75 6d  straint is assum
41c70 65 64 20 74 6f 20 62 65 20 66 75 6c 6c 79 20 68  ed to be fully h
41c80 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a  andled by the.**
41c90 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 61   virtual table a
41ca0 6e 64 20 69 73 20 6e 6f 74 20 63 68 65 63 6b 65  nd is not checke
41cb0 64 20 61 67 61 69 6e 20 62 79 20 53 51 4c 69 74  d again by SQLit
41cc0 65 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  e.)^.**.** ^The 
41cd0 69 64 78 4e 75 6d 20 61 6e 64 20 69 64 78 50 74  idxNum and idxPt
41ce0 72 20 76 61 6c 75 65 73 20 61 72 65 20 72 65 63  r values are rec
41cf0 6f 72 64 65 64 20 61 6e 64 20 70 61 73 73 65 64  orded and passed
41d00 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 5b 78 46   into the.** [xF
41d10 69 6c 74 65 72 5d 20 6d 65 74 68 6f 64 2e 0a 2a  ilter] method..*
41d20 2a 20 5e 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  * ^[sqlite3_free
41d30 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f 20 66  ()] is used to f
41d40 72 65 65 20 69 64 78 50 74 72 20 69 66 20 61 6e  ree idxPtr if an
41d50 64 20 6f 6e 6c 79 20 69 66 0a 2a 2a 20 6e 65 65  d only if.** nee
41d60 64 54 6f 46 72 65 65 49 64 78 50 74 72 20 69 73  dToFreeIdxPtr is
41d70 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68   true..**.** ^Th
41d80 65 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65  e orderByConsume
41d90 64 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 75 74  d means that out
41da0 70 75 74 20 66 72 6f 6d 20 5b 78 46 69 6c 74 65  put from [xFilte
41db0 72 5d 2f 5b 78 4e 65 78 74 5d 20 77 69 6c 6c 20  r]/[xNext] will 
41dc0 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 74 68 65 20  occur in.** the 
41dd0 63 6f 72 72 65 63 74 20 6f 72 64 65 72 20 74 6f  correct order to
41de0 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52 44   satisfy the ORD
41df0 45 52 20 42 59 20 63 6c 61 75 73 65 20 73 6f 20  ER BY clause so 
41e00 74 68 61 74 20 6e 6f 20 73 65 70 61 72 61 74 65  that no separate
41e10 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 73 74 65 70  .** sorting step
41e20 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a   is required..**
41e30 0a 2a 2a 20 5e 54 68 65 20 65 73 74 69 6d 61 74  .** ^The estimat
41e40 65 64 43 6f 73 74 20 76 61 6c 75 65 20 69 73 20  edCost value is 
41e50 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74  an estimate of t
41e60 68 65 20 63 6f 73 74 20 6f 66 20 64 6f 69 6e 67  he cost of doing
41e70 20 74 68 65 0a 2a 2a 20 70 61 72 74 69 63 75 6c   the.** particul
41e80 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 41 20 66 75  ar lookup.  A fu
41e90 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20 74 61 62  ll scan of a tab
41ea0 6c 65 20 77 69 74 68 20 4e 20 65 6e 74 72 69 65  le with N entrie
41eb0 73 20 73 68 6f 75 6c 64 20 68 61 76 65 0a 2a 2a  s should have.**
41ec0 20 61 20 63 6f 73 74 20 6f 66 20 4e 2e 20 20 41   a cost of N.  A
41ed0 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 20 6f   binary search o
41ee0 66 20 61 20 74 61 62 6c 65 20 6f 66 20 4e 20 65  f a table of N e
41ef0 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61  ntries should ha
41f00 76 65 20 61 0a 2a 2a 20 63 6f 73 74 20 6f 66 20  ve a.** cost of 
41f10 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 20 6c 6f  approximately lo
41f20 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  g(N)..*/.struct 
41f30 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
41f40 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e 70 75 74 73  fo {.  /* Inputs
41f50 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6e 73 74   */.  int nConst
41f60 72 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  raint;          
41f70 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
41f80 74 72 69 65 73 20 69 6e 20 61 43 6f 6e 73 74 72  tries in aConstr
41f90 61 69 6e 74 20 2a 2f 0a 20 20 73 74 72 75 63 74  aint */.  struct
41fa0 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
41fb0 6f 6e 73 74 72 61 69 6e 74 20 7b 0a 20 20 20 20  onstraint {.    
41fc0 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20   int iColumn;   
41fd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
41fe0 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74 2d 68 61 6e  lumn on left-han
41ff0 64 20 73 69 64 65 20 6f 66 20 63 6f 6e 73 74 72  d side of constr
42000 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 75 6e 73  aint */.     uns
42010 69 67 6e 65 64 20 63 68 61 72 20 6f 70 3b 20 20  igned char op;  
42020 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72         /* Constr
42030 61 69 6e 74 20 6f 70 65 72 61 74 6f 72 20 2a 2f  aint operator */
42040 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  .     unsigned c
42050 68 61 72 20 75 73 61 62 6c 65 3b 20 20 20 20 20  har usable;     
42060 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
42070 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 75 73  constraint is us
42080 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 69 6e 74  able */.     int
42090 20 69 54 65 72 6d 4f 66 66 73 65 74 3b 20 20 20   iTermOffset;   
420a0 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 69         /* Used i
420b0 6e 74 65 72 6e 61 6c 6c 79 20 2d 20 78 42 65 73  nternally - xBes
420c0 74 49 6e 64 65 78 20 73 68 6f 75 6c 64 20 69 67  tIndex should ig
420d0 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f  nore */.  } *aCo
420e0 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 20  nstraint;       
420f0 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6f 66       /* Table of
42100 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f   WHERE clause co
42110 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69  nstraints */.  i
42120 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20 20 20 20  nt nOrderBy;    
42130 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
42140 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ber of terms in 
42150 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
42160 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  use */.  struct 
42170 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72  sqlite3_index_or
42180 64 65 72 62 79 20 7b 0a 20 20 20 20 20 69 6e 74  derby {.     int
42190 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   iColumn;       
421a0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e         /* Column
421b0 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20   number */.     
421c0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 65  unsigned char de
421d0 73 63 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75  sc;       /* Tru
421e0 65 20 66 6f 72 20 44 45 53 43 2e 20 20 46 61 6c  e for DESC.  Fal
421f0 73 65 20 66 6f 72 20 41 53 43 2e 20 2a 2f 0a 20  se for ASC. */. 
42200 20 7d 20 2a 61 4f 72 64 65 72 42 79 3b 20 20 20   } *aOrderBy;   
42210 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
42220 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
42230 73 65 20 2a 2f 0a 20 20 2f 2a 20 4f 75 74 70 75  se */.  /* Outpu
42240 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73  ts */.  struct s
42250 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
42260 73 74 72 61 69 6e 74 5f 75 73 61 67 65 20 7b 0a  straint_usage {.
42270 20 20 20 20 69 6e 74 20 61 72 67 76 49 6e 64 65      int argvInde
42280 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  x;           /* 
42290 69 66 20 3e 30 2c 20 63 6f 6e 73 74 72 61 69 6e  if >0, constrain
422a0 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 72 67  t is part of arg
422b0 76 20 74 6f 20 78 46 69 6c 74 65 72 20 2a 2f 0a  v to xFilter */.
422c0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
422d0 72 20 6f 6d 69 74 3b 20 20 20 20 20 20 2f 2a 20  r omit;      /* 
422e0 44 6f 20 6e 6f 74 20 63 6f 64 65 20 61 20 74 65  Do not code a te
422f0 73 74 20 66 6f 72 20 74 68 69 73 20 63 6f 6e 73  st for this cons
42300 74 72 61 69 6e 74 20 2a 2f 0a 20 20 7d 20 2a 61  traint */.  } *a
42310 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 3b  ConstraintUsage;
42320 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 3b 20 20  .  int idxNum;  
42330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42340 20 4e 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20   Number used to 
42350 69 64 65 6e 74 69 66 79 20 74 68 65 20 69 6e 64  identify the ind
42360 65 78 20 2a 2f 0a 20 20 63 68 61 72 20 2a 69 64  ex */.  char *id
42370 78 53 74 72 3b 20 20 20 20 20 20 20 20 20 20 20  xStr;           
42380 20 20 20 2f 2a 20 53 74 72 69 6e 67 2c 20 70 6f     /* String, po
42390 73 73 69 62 6c 79 20 6f 62 74 61 69 6e 65 64 20  ssibly obtained 
423a0 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
423b0 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 65  loc */.  int nee
423c0 64 54 6f 46 72 65 65 49 64 78 53 74 72 3b 20 20  dToFreeIdxStr;  
423d0 20 20 20 20 2f 2a 20 46 72 65 65 20 69 64 78 53      /* Free idxS
423e0 74 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  tr using sqlite3
423f0 5f 66 72 65 65 28 29 20 69 66 20 74 72 75 65 20  _free() if true 
42400 2a 2f 0a 20 20 69 6e 74 20 6f 72 64 65 72 42 79  */.  int orderBy
42410 43 6f 6e 73 75 6d 65 64 3b 20 20 20 20 20 20 20  Consumed;       
42420 2f 2a 20 54 72 75 65 20 69 66 20 6f 75 74 70 75  /* True if outpu
42430 74 20 69 73 20 61 6c 72 65 61 64 79 20 6f 72 64  t is already ord
42440 65 72 65 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  ered */.  double
42450 20 65 73 74 69 6d 61 74 65 64 43 6f 73 74 3b 20   estimatedCost; 
42460 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 65       /* Estimate
42470 64 20 63 6f 73 74 20 6f 66 20 75 73 69 6e 67 20  d cost of using 
42480 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b  this index */.};
42490 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
424a0 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20  : Virtual Table 
424b0 43 6f 6e 73 74 72 61 69 6e 74 20 4f 70 65 72 61  Constraint Opera
424c0 74 6f 72 20 43 6f 64 65 73 0a 2a 2a 0a 2a 2a 20  tor Codes.**.** 
424d0 54 68 65 73 65 20 6d 61 63 72 6f 73 20 64 65 66  These macros def
424e0 69 6e 65 64 20 74 68 65 20 61 6c 6c 6f 77 65 64  ined the allowed
424f0 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 0a   values for the.
42500 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6e 64 65  ** [sqlite3_inde
42510 78 5f 69 6e 66 6f 5d 2e 61 43 6f 6e 73 74 72 61  x_info].aConstra
42520 69 6e 74 5b 5d 2e 6f 70 20 66 69 65 6c 64 2e 20  int[].op field. 
42530 20 45 61 63 68 20 76 61 6c 75 65 20 72 65 70 72   Each value repr
42540 65 73 65 6e 74 73 0a 2a 2a 20 61 6e 20 6f 70 65  esents.** an ope
42550 72 61 74 6f 72 20 74 68 61 74 20 69 73 20 70 61  rator that is pa
42560 72 74 20 6f 66 20 61 20 63 6f 6e 73 74 72 61 69  rt of a constrai
42570 6e 74 20 74 65 72 6d 20 69 6e 20 74 68 65 20 77  nt term in the w
42580 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 0a 2a  HERE clause of.*
42590 2a 20 61 20 71 75 65 72 79 20 74 68 61 74 20 75  * a query that u
425a0 73 65 73 20 61 20 5b 76 69 72 74 75 61 6c 20 74  ses a [virtual t
425b0 61 62 6c 65 5d 2e 0a 2a 2f 0a 23 64 65 66 69 6e  able]..*/.#defin
425c0 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  e SQLITE_INDEX_C
425d0 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 20 20 20  ONSTRAINT_EQ    
425e0 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
425f0 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
42600 54 5f 47 54 20 20 20 20 34 0a 23 64 65 66 69 6e  T_GT    4.#defin
42610 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  e SQLITE_INDEX_C
42620 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 20 20 20 20  ONSTRAINT_LE    
42630 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  8.#define SQLITE
42640 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
42650 54 5f 4c 54 20 20 20 20 31 36 0a 23 64 65 66 69  T_LT    16.#defi
42660 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  ne SQLITE_INDEX_
42670 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 20 20 20  CONSTRAINT_GE   
42680 20 33 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   32.#define SQLI
42690 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
426a0 49 4e 54 5f 4d 41 54 43 48 20 36 34 0a 0a 2f 2a  INT_MATCH 64../*
426b0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65  .** CAPI3REF: Re
426c0 67 69 73 74 65 72 20 41 20 56 69 72 74 75 61 6c  gister A Virtual
426d0 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74   Table Implement
426e0 61 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65  ation.**.** ^The
426f0 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  se routines are 
42700 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72  used to register
42710 20 61 20 6e 65 77 20 5b 76 69 72 74 75 61 6c 20   a new [virtual 
42720 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 6e 61  table module] na
42730 6d 65 2e 0a 2a 2a 20 5e 4d 6f 64 75 6c 65 20 6e  me..** ^Module n
42740 61 6d 65 73 20 6d 75 73 74 20 62 65 20 72 65 67  ames must be reg
42750 69 73 74 65 72 65 64 20 62 65 66 6f 72 65 0a 2a  istered before.*
42760 2a 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77  * creating a new
42770 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d   [virtual table]
42780 20 75 73 69 6e 67 20 74 68 65 20 6d 6f 64 75 6c   using the modul
42790 65 20 61 6e 64 20 62 65 66 6f 72 65 20 75 73 69  e and before usi
427a0 6e 67 20 61 0a 2a 2a 20 70 72 65 65 78 69 73 74  ng a.** preexist
427b0 69 6e 67 20 5b 76 69 72 74 75 61 6c 20 74 61 62  ing [virtual tab
427c0 6c 65 5d 20 66 6f 72 20 74 68 65 20 6d 6f 64 75  le] for the modu
427d0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 6d  le..**.** ^The m
427e0 6f 64 75 6c 65 20 6e 61 6d 65 20 69 73 20 72 65  odule name is re
427f0 67 69 73 74 65 72 65 64 20 6f 6e 20 74 68 65 20  gistered on the 
42800 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
42810 74 69 6f 6e 5d 20 73 70 65 63 69 66 69 65 64 0a  tion] specified.
42820 2a 2a 20 62 79 20 74 68 65 20 66 69 72 73 74 20  ** by the first 
42830 70 61 72 61 6d 65 74 65 72 2e 20 20 5e 54 68 65  parameter.  ^The
42840 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64   name of the mod
42850 75 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 20  ule is given by 
42860 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64 20 70  the .** second p
42870 61 72 61 6d 65 74 65 72 2e 20 20 5e 54 68 65 20  arameter.  ^The 
42880 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20  third parameter 
42890 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a  is a pointer to.
428a0 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ** the implement
428b0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 76 69  ation of the [vi
428c0 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
428d0 6c 65 5d 2e 20 20 20 5e 54 68 65 20 66 6f 75 72  le].   ^The four
428e0 74 68 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  th.** parameter 
428f0 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20  is an arbitrary 
42900 63 6c 69 65 6e 74 20 64 61 74 61 20 70 6f 69 6e  client data poin
42910 74 65 72 20 74 68 61 74 20 69 73 20 70 61 73 73  ter that is pass
42920 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 6e  ed through.** in
42930 74 6f 20 74 68 65 20 5b 78 43 72 65 61 74 65 5d  to the [xCreate]
42940 20 61 6e 64 20 5b 78 43 6f 6e 6e 65 63 74 5d 20   and [xConnect] 
42950 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 76  methods of the v
42960 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
42970 75 6c 65 0a 2a 2a 20 77 68 65 6e 20 61 20 6e 65  ule.** when a ne
42980 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  w virtual table 
42990 69 73 20 62 65 20 62 65 69 6e 67 20 63 72 65 61  is be being crea
429a0 74 65 64 20 6f 72 20 72 65 69 6e 69 74 69 61 6c  ted or reinitial
429b0 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65  ized..**.** ^The
429c0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
429d0 6d 6f 64 75 6c 65 5f 76 32 28 29 20 69 6e 74 65  module_v2() inte
429e0 72 66 61 63 65 20 68 61 73 20 61 20 66 69 66 74  rface has a fift
429f0 68 20 70 61 72 61 6d 65 74 65 72 20 77 68 69 63  h parameter whic
42a00 68 0a 2a 2a 20 69 73 20 61 20 70 6f 69 6e 74 65  h.** is a pointe
42a10 72 20 74 6f 20 61 20 64 65 73 74 72 75 63 74 6f  r to a destructo
42a20 72 20 66 6f 72 20 74 68 65 20 70 43 6c 69 65 6e  r for the pClien
42a30 74 44 61 74 61 2e 20 20 5e 53 51 4c 69 74 65 20  tData.  ^SQLite 
42a40 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74  will.** invoke t
42a50 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75  he destructor fu
42a60 6e 63 74 69 6f 6e 20 28 69 66 20 69 74 20 69 73  nction (if it is
42a70 20 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 65 6e 20   not NULL) when 
42a80 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20 6c 6f 6e  SQLite.** no lon
42a90 67 65 72 20 6e 65 65 64 73 20 74 68 65 20 70 43  ger needs the pC
42aa0 6c 69 65 6e 74 44 61 74 61 20 70 6f 69 6e 74 65  lientData pointe
42ab0 72 2e 20 20 5e 54 68 65 20 64 65 73 74 72 75 63  r.  ^The destruc
42ac0 74 6f 72 20 77 69 6c 6c 20 61 6c 73 6f 0a 2a 2a  tor will also.**
42ad0 20 62 65 20 69 6e 76 6f 6b 65 64 20 69 66 20 74   be invoked if t
42ae0 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  he call to sqlit
42af0 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
42b00 5f 76 32 28 29 20 66 61 69 6c 73 2e 0a 2a 2a 20  _v2() fails..** 
42b10 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65  ^The sqlite3_cre
42b20 61 74 65 5f 6d 6f 64 75 6c 65 28 29 0a 2a 2a 20  ate_module().** 
42b30 69 6e 74 65 72 66 61 63 65 20 69 73 20 65 71 75  interface is equ
42b40 69 76 61 6c 65 6e 74 20 74 6f 20 73 71 6c 69 74  ivalent to sqlit
42b50 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
42b60 5f 76 32 28 29 20 77 69 74 68 20 61 20 4e 55 4c  _v2() with a NUL
42b70 4c 0a 2a 2a 20 64 65 73 74 72 75 63 74 6f 72 2e  L.** destructor.
42b80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
42b90 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
42ba0 65 5f 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69  e_module(.  sqli
42bb0 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
42bc0 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65         /* SQLite
42bd0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72   connection to r
42be0 65 67 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77  egister module w
42bf0 69 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ith */.  const c
42c00 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20  har *zName,     
42c10 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
42c20 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63  he module */.  c
42c30 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
42c40 75 6c 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74  ule *p,   /* Met
42c50 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64  hods for the mod
42c60 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ule */.  void *p
42c70 43 6c 69 65 6e 74 44 61 74 61 20 20 20 20 20 20  ClientData      
42c80 20 20 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61      /* Client da
42c90 74 61 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78  ta for xCreate/x
42ca0 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 53 51  Connect */.);.SQ
42cb0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
42cc0 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
42cd0 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33  le_v2(.  sqlite3
42ce0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
42cf0 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f      /* SQLite co
42d00 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 69  nnection to regi
42d10 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 68  ster module with
42d20 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
42d30 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
42d40 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
42d50 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  module */.  cons
42d60 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
42d70 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64   *p,   /* Method
42d80 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65  s for the module
42d90 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69   */.  void *pCli
42da0 65 6e 74 44 61 74 61 2c 20 20 20 20 20 20 20 20  entData,        
42db0 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 20   /* Client data 
42dc0 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e  for xCreate/xCon
42dd0 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 28 2a  nect */.  void(*
42de0 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29  xDestroy)(void*)
42df0 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64       /* Module d
42e00 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69  estructor functi
42e10 6f 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  on */.);../*.** 
42e20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61  CAPI3REF: Virtua
42e30 6c 20 54 61 62 6c 65 20 49 6e 73 74 61 6e 63 65  l Table Instance
42e40 20 4f 62 6a 65 63 74 0a 2a 2a 20 4b 45 59 57 4f   Object.** KEYWO
42e50 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 61  RDS: sqlite3_vta
42e60 62 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 5b 76  b.**.** Every [v
42e70 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
42e80 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ule] implementat
42e90 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62 63 6c  ion uses a subcl
42ea0 61 73 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20 6f  ass.** of this o
42eb0 62 6a 65 63 74 20 74 6f 20 64 65 73 63 72 69 62  bject to describ
42ec0 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69  e a particular i
42ed0 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68  nstance.** of th
42ee0 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  e [virtual table
42ef0 5d 2e 20 20 45 61 63 68 20 73 75 62 63 6c 61 73  ].  Each subclas
42f00 73 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69  s will.** be tai
42f10 6c 6f 72 65 64 20 74 6f 20 74 68 65 20 73 70 65  lored to the spe
42f20 63 69 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74  cific needs of t
42f30 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d  he module implem
42f40 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65  entation..** The
42f50 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73   purpose of this
42f60 20 73 75 70 65 72 63 6c 61 73 73 20 69 73 20 74   superclass is t
42f70 6f 20 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e  o define certain
42f80 20 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65   fields that are
42f90 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c  .** common to al
42fa0 6c 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  l module impleme
42fb0 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  ntations..**.** 
42fc0 5e 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20  ^Virtual tables 
42fd0 6d 65 74 68 6f 64 73 20 63 61 6e 20 73 65 74 20  methods can set 
42fe0 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
42ff0 20 62 79 20 61 73 73 69 67 6e 69 6e 67 20 61 0a   by assigning a.
43000 2a 2a 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e  ** string obtain
43010 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
43020 5f 6d 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a  _mprintf()] to z
43030 45 72 72 4d 73 67 2e 20 20 54 68 65 20 6d 65 74  ErrMsg.  The met
43040 68 6f 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61  hod should.** ta
43050 6b 65 20 63 61 72 65 20 74 68 61 74 20 61 6e 79  ke care that any
43060 20 70 72 69 6f 72 20 73 74 72 69 6e 67 20 69 73   prior string is
43070 20 66 72 65 65 64 20 62 79 20 61 20 63 61 6c 6c   freed by a call
43080 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65   to [sqlite3_fre
43090 65 28 29 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f  e()].** prior to
430a0 20 61 73 73 69 67 6e 69 6e 67 20 61 20 6e 65 77   assigning a new
430b0 20 73 74 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d   string to zErrM
430c0 73 67 2e 20 20 5e 41 66 74 65 72 20 74 68 65 20  sg.  ^After the 
430d0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a  error message.**
430e0 20 69 73 20 64 65 6c 69 76 65 72 65 64 20 75 70   is delivered up
430f0 20 74 6f 20 74 68 65 20 63 6c 69 65 6e 74 20 61   to the client a
43100 70 70 6c 69 63 61 74 69 6f 6e 2c 20 74 68 65 20  pplication, the 
43110 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 61  string will be a
43120 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20  utomatically.** 
43130 66 72 65 65 64 20 62 79 20 73 71 6c 69 74 65 33  freed by sqlite3
43140 5f 66 72 65 65 28 29 20 61 6e 64 20 74 68 65 20  _free() and the 
43150 7a 45 72 72 4d 73 67 20 66 69 65 6c 64 20 77 69  zErrMsg field wi
43160 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e 0a 2a 2f  ll be zeroed..*/
43170 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
43180 76 74 61 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73  vtab {.  const s
43190 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
431a0 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20  Module;  /* The 
431b0 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68 69 73 20  module for this 
431c0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
431d0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
431e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
431f0 20 20 20 2f 2a 20 4e 4f 20 4c 4f 4e 47 45 52 20     /* NO LONGER 
43200 55 53 45 44 20 2a 2f 0a 20 20 63 68 61 72 20 2a  USED */.  char *
43210 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20  zErrMsg;        
43220 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72            /* Err
43230 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20  or message from 
43240 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
43250 29 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61  ) */.  /* Virtua
43260 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  l table implemen
43270 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70  tations will typ
43280 69 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74  ically add addit
43290 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a  ional fields */.
432a0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  };../*.** CAPI3R
432b0 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c  EF: Virtual Tabl
432c0 65 20 43 75 72 73 6f 72 20 4f 62 6a 65 63 74 0a  e Cursor Object.
432d0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c  ** KEYWORDS: sql
432e0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
432f0 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   {virtual table 
43300 63 75 72 73 6f 72 7d 0a 2a 2a 0a 2a 2a 20 45 76  cursor}.**.** Ev
43310 65 72 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62  ery [virtual tab
43320 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65  le module] imple
43330 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61  mentation uses a
43340 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65   subclass of the
43350 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  .** following st
43360 72 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 72  ructure to descr
43370 69 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 74  ibe cursors that
43380 20 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 0a   point into the.
43390 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c  ** [virtual tabl
433a0 65 5d 20 61 6e 64 20 61 72 65 20 75 73 65 64 0a  e] and are used.
433b0 2a 2a 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75  ** to loop throu
433c0 67 68 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  gh the virtual t
433d0 61 62 6c 65 2e 20 20 43 75 72 73 6f 72 73 20 61  able.  Cursors a
433e0 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  re created using
433f0 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
43400 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c 20  _module.xOpen | 
43410 78 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f 66  xOpen] method of
43420 20 74 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64 20   the module and 
43430 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a  are destroyed.**
43440 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33   by the [sqlite3
43450 5f 6d 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20 7c  _module.xClose |
43460 20 78 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64 2e   xClose] method.
43470 20 20 43 75 72 73 6f 72 73 20 61 72 65 20 75 73    Cursors are us
43480 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78 46  ed.** by the [xF
43490 69 6c 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c  ilter], [xNext],
434a0 20 5b 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d   [xEof], [xColum
434b0 6e 5d 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64 5d  n], and [xRowid]
434c0 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74   methods.** of t
434d0 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68  he module.  Each
434e0 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
434f0 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69  tation will defi
43500 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  ne.** the conten
43510 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74  t of a cursor st
43520 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20  ructure to suit 
43530 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a  its own needs..*
43540 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63  *.** This superc
43550 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f  lass exists in o
43560 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66  rder to define f
43570 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72  ields of the cur
43580 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20  sor that.** are 
43590 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d  common to all im
435a0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a  plementations..*
435b0 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
435c0 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20  _vtab_cursor {. 
435d0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
435e0 56 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 69  Vtab;      /* Vi
435f0 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 74  rtual table of t
43600 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  his cursor */.  
43610 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
43620 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
43630 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20   will typically 
43640 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66  add additional f
43650 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ields */.};../*.
43660 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 63  ** CAPI3REF: Dec
43670 6c 61 72 65 20 54 68 65 20 53 63 68 65 6d 61 20  lare The Schema 
43680 4f 66 20 41 20 56 69 72 74 75 61 6c 20 54 61 62  Of A Virtual Tab
43690 6c 65 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 5b 78  le.**.** ^The [x
436a0 43 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f  Create] and [xCo
436b0 6e 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f  nnect] methods o
436c0 66 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20  f a.** [virtual 
436d0 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 61  table module] ca
436e0 6c 6c 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  ll this interfac
436f0 65 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 65 20  e.** to declare 
43700 74 68 65 20 66 6f 72 6d 61 74 20 28 74 68 65 20  the format (the 
43710 6e 61 6d 65 73 20 61 6e 64 20 64 61 74 61 74 79  names and dataty
43720 70 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  pes of the colum
43730 6e 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69  ns) of.** the vi
43740 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 68 65  rtual tables the
43750 79 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a  y implement..*/.
43760 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
43770 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76  qlite3_declare_v
43780 74 61 62 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f  tab(sqlite3*, co
43790 6e 73 74 20 63 68 61 72 20 2a 7a 53 51 4c 29 3b  nst char *zSQL);
437a0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
437b0 3a 20 4f 76 65 72 6c 6f 61 64 20 41 20 46 75 6e  : Overload A Fun
437c0 63 74 69 6f 6e 20 46 6f 72 20 41 20 56 69 72 74  ction For A Virt
437d0 75 61 6c 20 54 61 62 6c 65 0a 2a 2a 0a 2a 2a 20  ual Table.**.** 
437e0 5e 28 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73  ^(Virtual tables
437f0 20 63 61 6e 20 70 72 6f 76 69 64 65 20 61 6c 74   can provide alt
43800 65 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d 65  ernative impleme
43810 6e 74 61 74 69 6f 6e 73 20 6f 66 20 66 75 6e 63  ntations of func
43820 74 69 6f 6e 73 0a 2a 2a 20 75 73 69 6e 67 20 74  tions.** using t
43830 68 65 20 5b 78 46 69 6e 64 46 75 6e 63 74 69 6f  he [xFindFunctio
43840 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  n] method of the
43850 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   [virtual table 
43860 6d 6f 64 75 6c 65 5d 2e 20 20 0a 2a 2a 20 42 75  module].  .** Bu
43870 74 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e  t global version
43880 73 20 6f 66 20 74 68 6f 73 65 20 66 75 6e 63 74  s of those funct
43890 69 6f 6e 73 0a 2a 2a 20 6d 75 73 74 20 65 78 69  ions.** must exi
438a0 73 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62  st in order to b
438b0 65 20 6f 76 65 72 6c 6f 61 64 65 64 2e 29 5e 0a  e overloaded.)^.
438c0 2a 2a 0a 2a 2a 20 5e 28 54 68 69 73 20 41 50 49  **.** ^(This API
438d0 20 6d 61 6b 65 73 20 73 75 72 65 20 61 20 67 6c   makes sure a gl
438e0 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f 66 20  obal version of 
438f0 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  a function with 
43900 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20  a particular.** 
43910 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 72 20  name and number 
43920 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 65 78  of parameters ex
43930 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 75 63  ists.  If no suc
43940 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74  h function exist
43950 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 73  s.** before this
43960 20 41 50 49 20 69 73 20 63 61 6c 6c 65 64 2c 20   API is called, 
43970 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 69  a new function i
43980 73 20 63 72 65 61 74 65 64 2e 29 5e 20 20 5e 54  s created.)^  ^T
43990 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
439a0 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20  n.** of the new 
439b0 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20  function always 
439c0 63 61 75 73 65 73 20 61 6e 20 65 78 63 65 70 74  causes an except
439d0 69 6f 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 6e  ion to be thrown
439e0 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77  .  So.** the new
439f0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74   function is not
43a00 20 67 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 69   good for anythi
43a10 6e 67 20 62 79 20 69 74 73 65 6c 66 2e 20 20 49  ng by itself.  I
43a20 74 73 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f  ts only.** purpo
43a30 73 65 20 69 73 20 74 6f 20 62 65 20 61 20 70 6c  se is to be a pl
43a40 61 63 65 68 6f 6c 64 65 72 20 66 75 6e 63 74 69  aceholder functi
43a50 6f 6e 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f  on that can be o
43a60 76 65 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20  verloaded.** by 
43a70 61 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  a [virtual table
43a80 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  ]..*/.SQLITE_API
43a90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65   int sqlite3_ove
43aa0 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 73  rload_function(s
43ab0 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63  qlite3*, const c
43ac0 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20  har *zFuncName, 
43ad0 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a 0a 2a  int nArg);../*.*
43ae0 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20  * The interface 
43af0 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74  to the virtual-t
43b00 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 64  able mechanism d
43b10 65 66 69 6e 65 64 20 61 62 6f 76 65 20 28 62 61  efined above (ba
43b20 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61 20 63 6f  ck up.** to a co
43b30 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c 79  mment remarkably
43b40 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73   similar to this
43b50 20 6f 6e 65 29 20 69 73 20 63 75 72 72 65 6e 74   one) is current
43b60 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a  ly considered.**
43b70 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e   to be experimen
43b80 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66  tal.  The interf
43b90 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65  ace might change
43ba0 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65   in incompatible
43bb0 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 69   ways..** If thi
43bc0 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66  s is a problem f
43bd0 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75  or you, do not u
43be0 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  se the interface
43bf0 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a   at this time..*
43c00 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 69  *.** When the vi
43c10 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68  rtual-table mech
43c20 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 73  anism stabilizes
43c30 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 72  , we will declar
43c40 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 61  e the.** interfa
43c50 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 6f 72  ce fixed, suppor
43c60 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 65 6c  t it indefinitel
43c70 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68  y, and remove th
43c80 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a 0a  is comment..*/..
43c90 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
43ca0 41 20 48 61 6e 64 6c 65 20 54 6f 20 41 6e 20 4f  A Handle To An O
43cb0 70 65 6e 20 42 4c 4f 42 0a 2a 2a 20 4b 45 59 57  pen BLOB.** KEYW
43cc0 4f 52 44 53 3a 20 7b 42 4c 4f 42 20 68 61 6e 64  ORDS: {BLOB hand
43cd0 6c 65 7d 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65  le} {BLOB handle
43ce0 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  s}.**.** An inst
43cf0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a  ance of this obj
43d00 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 20 61  ect represents a
43d10 6e 20 6f 70 65 6e 20 42 4c 4f 42 20 6f 6e 20 77  n open BLOB on w
43d20 68 69 63 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  hich.** [sqlite3
43d30 5f 62 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63  _blob_open | inc
43d40 72 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f  remental BLOB I/
43d50 4f 5d 20 63 61 6e 20 62 65 20 70 65 72 66 6f 72  O] can be perfor
43d60 6d 65 64 2e 0a 2a 2a 20 5e 4f 62 6a 65 63 74 73  med..** ^Objects
43d70 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 61 72   of this type ar
43d80 65 20 63 72 65 61 74 65 64 20 62 79 20 5b 73 71  e created by [sq
43d90 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28  lite3_blob_open(
43da0 29 5d 0a 2a 2a 20 61 6e 64 20 64 65 73 74 72 6f  )].** and destro
43db0 79 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  yed by [sqlite3_
43dc0 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a  blob_close()]..*
43dd0 2a 20 5e 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  * ^The [sqlite3_
43de0 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61 6e 64  blob_read()] and
43df0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77   [sqlite3_blob_w
43e00 72 69 74 65 28 29 5d 20 69 6e 74 65 72 66 61 63  rite()] interfac
43e10 65 73 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65  es.** can be use
43e20 64 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69  d to read or wri
43e30 74 65 20 73 6d 61 6c 6c 20 73 75 62 73 65 63 74  te small subsect
43e40 69 6f 6e 73 20 6f 66 20 74 68 65 20 42 4c 4f 42  ions of the BLOB
43e50 2e 0a 2a 2a 20 5e 54 68 65 20 5b 73 71 6c 69 74  ..** ^The [sqlit
43e60 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 29 5d  e3_blob_bytes()]
43e70 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
43e80 6e 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  ns the size of t
43e90 68 65 20 42 4c 4f 42 20 69 6e 20 62 79 74 65 73  he BLOB in bytes
43ea0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
43eb0 75 63 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  uct sqlite3_blob
43ec0 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 3b 0a 0a   sqlite3_blob;..
43ed0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
43ee0 4f 70 65 6e 20 41 20 42 4c 4f 42 20 46 6f 72 20  Open A BLOB For 
43ef0 49 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 0a  Incremental I/O.
43f00 2a 2a 0a 2a 2a 20 5e 28 54 68 69 73 20 69 6e 74  **.** ^(This int
43f10 65 72 66 61 63 65 73 20 6f 70 65 6e 73 20 61 20  erfaces opens a 
43f20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 20 7c 20 68  [BLOB handle | h
43f30 61 6e 64 6c 65 5d 20 74 6f 20 74 68 65 20 42 4c  andle] to the BL
43f40 4f 42 20 6c 6f 63 61 74 65 64 0a 2a 2a 20 69 6e  OB located.** in
43f50 20 72 6f 77 20 69 52 6f 77 2c 20 63 6f 6c 75 6d   row iRow, colum
43f60 6e 20 7a 43 6f 6c 75 6d 6e 2c 20 74 61 62 6c 65  n zColumn, table
43f70 20 7a 54 61 62 6c 65 20 69 6e 20 64 61 74 61 62   zTable in datab
43f80 61 73 65 20 7a 44 62 3b 0a 2a 2a 20 69 6e 20 6f  ase zDb;.** in o
43f90 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20  ther words, the 
43fa0 73 61 6d 65 20 42 4c 4f 42 20 74 68 61 74 20 77  same BLOB that w
43fb0 6f 75 6c 64 20 62 65 20 73 65 6c 65 63 74 65 64  ould be selected
43fc0 20 62 79 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e   by:.**.** <pre>
43fd0 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 7a  .**     SELECT z
43fe0 43 6f 6c 75 6d 6e 20 46 52 4f 4d 20 7a 44 62 2e  Column FROM zDb.
43ff0 7a 54 61 62 6c 65 20 57 48 45 52 45 20 5b 72 6f  zTable WHERE [ro
44000 77 69 64 5d 20 3d 20 69 52 6f 77 3b 0a 2a 2a 20  wid] = iRow;.** 
44010 3c 2f 70 72 65 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5e  </pre>)^.**.** ^
44020 49 66 20 74 68 65 20 66 6c 61 67 73 20 70 61 72  If the flags par
44030 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65  ameter is non-ze
44040 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 42 4c 4f  ro, then the BLO
44050 42 20 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 20  B is opened for 
44060 72 65 61 64 0a 2a 2a 20 61 6e 64 20 77 72 69 74  read.** and writ
44070 65 20 61 63 63 65 73 73 2e 20 5e 49 66 20 69 74  e access. ^If it
44080 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 20 42 4c   is zero, the BL
44090 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66 6f 72  OB is opened for
440a0 20 72 65 61 64 20 61 63 63 65 73 73 2e 0a 2a 2a   read access..**
440b0 20 5e 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73   ^It is not poss
440c0 69 62 6c 65 20 74 6f 20 6f 70 65 6e 20 61 20 63  ible to open a c
440d0 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 70 61  olumn that is pa
440e0 72 74 20 6f 66 20 61 6e 20 69 6e 64 65 78 20 6f  rt of an index o
440f0 72 20 70 72 69 6d 61 72 79 20 0a 2a 2a 20 6b 65  r primary .** ke
44100 79 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 5e  y for writing. ^
44110 49 66 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79 20  If [foreign key 
44120 63 6f 6e 73 74 72 61 69 6e 74 73 5d 20 61 72 65  constraints] are
44130 20 65 6e 61 62 6c 65 64 2c 20 69 74 20 69 73 20   enabled, it is 
44140 0a 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65  .** not possible
44150 20 74 6f 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d   to open a colum
44160 6e 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f  n that is part o
44170 66 20 61 20 5b 63 68 69 6c 64 20 6b 65 79 5d 20  f a [child key] 
44180 66 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a  for writing..**.
44190 2a 2a 20 5e 4e 6f 74 65 20 74 68 61 74 20 74 68  ** ^Note that th
441a0 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  e database name 
441b0 69 73 20 6e 6f 74 20 74 68 65 20 66 69 6c 65 6e  is not the filen
441c0 61 6d 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ame that contain
441d0 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  s.** the databas
441e0 65 20 62 75 74 20 72 61 74 68 65 72 20 74 68 65  e but rather the
441f0 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 6f   symbolic name o
44200 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  f the database t
44210 68 61 74 0a 2a 2a 20 61 70 70 65 61 72 73 20 61  hat.** appears a
44220 66 74 65 72 20 74 68 65 20 41 53 20 6b 65 79 77  fter the AS keyw
44230 6f 72 64 20 77 68 65 6e 20 74 68 65 20 64 61 74  ord when the dat
44240 61 62 61 73 65 20 69 73 20 63 6f 6e 6e 65 63 74  abase is connect
44250 65 64 20 75 73 69 6e 67 20 5b 41 54 54 41 43 48  ed using [ATTACH
44260 5d 2e 0a 2a 2a 20 5e 46 6f 72 20 74 68 65 20 6d  ]..** ^For the m
44270 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
44280 65 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  e, the database 
44290 6e 61 6d 65 20 69 73 20 22 6d 61 69 6e 22 2e 0a  name is "main"..
442a0 2a 2a 20 5e 46 6f 72 20 54 45 4d 50 20 74 61 62  ** ^For TEMP tab
442b0 6c 65 73 2c 20 74 68 65 20 64 61 74 61 62 61 73  les, the databas
442c0 65 20 6e 61 6d 65 20 69 73 20 22 74 65 6d 70 22  e name is "temp"
442d0 2e 0a 2a 2a 0a 2a 2a 20 5e 28 4f 6e 20 73 75 63  ..**.** ^(On suc
442e0 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b  cess, [SQLITE_OK
442f0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  ] is returned an
44300 64 20 74 68 65 20 6e 65 77 20 5b 42 4c 4f 42 20  d the new [BLOB 
44310 68 61 6e 64 6c 65 5d 20 69 73 20 77 72 69 74 74  handle] is writt
44320 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f 62  en.** to *ppBlob
44330 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 5b  . Otherwise an [
44340 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72  error code] is r
44350 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 42  eturned and *ppB
44360 6c 6f 62 20 69 73 20 73 65 74 0a 2a 2a 20 74 6f  lob is set.** to
44370 20 62 65 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74   be a null point
44380 65 72 2e 29 5e 0a 2a 2a 20 5e 54 68 69 73 20 66  er.)^.** ^This f
44390 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65  unction sets the
443a0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
443b0 63 74 69 6f 6e 5d 20 65 72 72 6f 72 20 63 6f 64  ction] error cod
443c0 65 20 61 6e 64 20 6d 65 73 73 61 67 65 0a 2a 2a  e and message.**
443d0 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 20   accessible via 
443e0 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65  [sqlite3_errcode
443f0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
44400 5f 65 72 72 6d 73 67 28 29 5d 20 61 6e 64 20 72  _errmsg()] and r
44410 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74 69  elated.** functi
44420 6f 6e 73 2e 20 5e 4e 6f 74 65 20 74 68 61 74 20  ons. ^Note that 
44430 74 68 65 20 2a 70 70 42 6c 6f 62 20 76 61 72 69  the *ppBlob vari
44440 61 62 6c 65 20 69 73 20 61 6c 77 61 79 73 20 69  able is always i
44450 6e 69 74 69 61 6c 69 7a 65 64 20 69 6e 20 61 0a  nitialized in a.
44460 2a 2a 20 77 61 79 20 74 68 61 74 20 6d 61 6b 65  ** way that make
44470 73 20 69 74 20 73 61 66 65 20 74 6f 20 69 6e 76  s it safe to inv
44480 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  oke [sqlite3_blo
44490 62 5f 63 6c 6f 73 65 28 29 5d 20 6f 6e 20 2a 70  b_close()] on *p
444a0 70 42 6c 6f 62 0a 2a 2a 20 72 65 67 61 72 64 6c  pBlob.** regardl
444b0 65 73 73 20 6f 66 20 74 68 65 20 73 75 63 63 65  ess of the succe
444c0 73 73 20 6f 72 20 66 61 69 6c 75 72 65 20 6f 66  ss or failure of
444d0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
444e0 2a 0a 2a 2a 20 5e 28 49 66 20 74 68 65 20 72 6f  *.** ^(If the ro
444f0 77 20 74 68 61 74 20 61 20 42 4c 4f 42 20 68 61  w that a BLOB ha
44500 6e 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69  ndle points to i
44510 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e  s modified by an
44520 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b 44  .** [UPDATE], [D
44530 45 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b 4f  ELETE], or by [O
44540 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65  N CONFLICT] side
44550 2d 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 65 6e  -effects.** then
44560 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65   the BLOB handle
44570 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65   is marked as "e
44580 78 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69 73  xpired"..** This
44590 20 69 73 20 74 72 75 65 20 69 66 20 61 6e 79 20   is true if any 
445a0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 6f  column of the ro
445b0 77 20 69 73 20 63 68 61 6e 67 65 64 2c 20 65 76  w is changed, ev
445c0 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f  en a column.** o
445d0 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e  ther than the on
445e0 65 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c  e the BLOB handl
445f0 65 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 29 5e 0a  e is open on.)^.
44600 2a 2a 20 5e 43 61 6c 6c 73 20 74 6f 20 5b 73 71  ** ^Calls to [sq
44610 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28  lite3_blob_read(
44620 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
44630 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 66 6f  blob_write()] fo
44640 72 0a 2a 2a 20 61 6e 20 65 78 70 69 72 65 64 20  r.** an expired 
44650 42 4c 4f 42 20 68 61 6e 64 6c 65 20 66 61 69 6c  BLOB handle fail
44660 20 77 69 74 68 20 61 20 72 65 74 75 72 6e 20 63   with a return c
44670 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41  ode of [SQLITE_A
44680 42 4f 52 54 5d 2e 0a 2a 2a 20 5e 28 43 68 61 6e  BORT]..** ^(Chan
44690 67 65 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  ges written into
446a0 20 61 20 42 4c 4f 42 20 70 72 69 6f 72 20 74 6f   a BLOB prior to
446b0 20 74 68 65 20 42 4c 4f 42 20 65 78 70 69 72 69   the BLOB expiri
446c0 6e 67 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 6f  ng are not.** ro
446d0 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 65  lled back by the
446e0 20 65 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74   expiration of t
446f0 68 65 20 42 4c 4f 42 2e 20 20 53 75 63 68 20 63  he BLOB.  Such c
44700 68 61 6e 67 65 73 20 77 69 6c 6c 20 65 76 65 6e  hanges will even
44710 74 75 61 6c 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74  tually.** commit
44720 20 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 74   if the transact
44730 69 6f 6e 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f  ion continues to
44740 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 29 5e 0a 2a   completion.)^.*
44750 2a 0a 2a 2a 20 5e 55 73 65 20 74 68 65 20 5b 73  *.** ^Use the [s
44760 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65  qlite3_blob_byte
44770 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 74  s()] interface t
44780 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
44790 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6f  size of.** the o
447a0 70 65 6e 65 64 20 62 6c 6f 62 2e 20 20 5e 54 68  pened blob.  ^Th
447b0 65 20 73 69 7a 65 20 6f 66 20 61 20 62 6c 6f 62  e size of a blob
447c0 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e   may not be chan
447d0 67 65 64 20 62 79 20 74 68 69 73 0a 2a 2a 20 69  ged by this.** i
447e0 6e 74 65 72 66 61 63 65 2e 20 20 55 73 65 20 74  nterface.  Use t
447f0 68 65 20 5b 55 50 44 41 54 45 5d 20 53 51 4c 20  he [UPDATE] SQL 
44800 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 68 61 6e 67  command to chang
44810 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 0a  e the size of a.
44820 2a 2a 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 5e  ** blob..**.** ^
44830 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  The [sqlite3_bin
44840 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20 61 6e  d_zeroblob()] an
44850 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  d [sqlite3_resul
44860 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20 69 6e  t_zeroblob()] in
44870 74 65 72 66 61 63 65 73 0a 2a 2a 20 61 6e 64 20  terfaces.** and 
44880 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 5b 7a 65  the built-in [ze
44890 72 6f 62 6c 6f 62 5d 20 53 51 4c 20 66 75 6e 63  roblob] SQL func
448a0 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64  tion can be used
448b0 2c 20 69 66 20 64 65 73 69 72 65 64 2c 0a 2a 2a  , if desired,.**
448c0 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 65 6d   to create an em
448d0 70 74 79 2c 20 7a 65 72 6f 2d 66 69 6c 6c 65 64  pty, zero-filled
448e0 20 62 6c 6f 62 20 69 6e 20 77 68 69 63 68 20 74   blob in which t
448f0 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20  o read or write 
44900 75 73 69 6e 67 0a 2a 2a 20 74 68 69 73 20 69 6e  using.** this in
44910 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54  terface..**.** T
44920 6f 20 61 76 6f 69 64 20 61 20 72 65 73 6f 75 72  o avoid a resour
44930 63 65 20 6c 65 61 6b 2c 20 65 76 65 72 79 20 6f  ce leak, every o
44940 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65  pen [BLOB handle
44950 5d 20 73 68 6f 75 6c 64 20 65 76 65 6e 74 75 61  ] should eventua
44960 6c 6c 79 0a 2a 2a 20 62 65 20 72 65 6c 65 61 73  lly.** be releas
44970 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20  ed by a call to 
44980 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c  [sqlite3_blob_cl
44990 6f 73 65 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54  ose()]..*/.SQLIT
449a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
449b0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73  3_blob_open(.  s
449c0 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 74  qlite3*,.  const
449d0 20 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63 6f   char *zDb,.  co
449e0 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
449f0 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
44a00 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 74  zColumn,.  sqlit
44a10 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20  e3_int64 iRow,. 
44a20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73 71   int flags,.  sq
44a30 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42  lite3_blob **ppB
44a40 6c 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  lob.);../*.** CA
44a50 50 49 33 52 45 46 3a 20 4d 6f 76 65 20 61 20 42  PI3REF: Move a B
44a60 4c 4f 42 20 48 61 6e 64 6c 65 20 74 6f 20 61 20  LOB Handle to a 
44a70 4e 65 77 20 52 6f 77 0a 2a 2a 0a 2a 2a 20 5e 54  New Row.**.** ^T
44a80 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
44a90 75 73 65 64 20 74 6f 20 6d 6f 76 65 20 61 6e 20  used to move an 
44aa0 65 78 69 73 74 69 6e 67 20 62 6c 6f 62 20 68 61  existing blob ha
44ab0 6e 64 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20  ndle so that it 
44ac0 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 64  points.** to a d
44ad0 69 66 66 65 72 65 6e 74 20 72 6f 77 20 6f 66 20  ifferent row of 
44ae0 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
44af0 65 20 74 61 62 6c 65 2e 20 5e 54 68 65 20 6e 65  e table. ^The ne
44b00 77 20 72 6f 77 20 69 73 20 69 64 65 6e 74 69 66  w row is identif
44b10 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 72 6f  ied.** by the ro
44b20 77 69 64 20 76 61 6c 75 65 20 70 61 73 73 65 64  wid value passed
44b30 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
44b40 72 67 75 6d 65 6e 74 2e 20 4f 6e 6c 79 20 74 68  rgument. Only th
44b50 65 20 72 6f 77 20 63 61 6e 20 62 65 0a 2a 2a 20  e row can be.** 
44b60 63 68 61 6e 67 65 64 2e 20 5e 54 68 65 20 64 61  changed. ^The da
44b70 74 61 62 61 73 65 2c 20 74 61 62 6c 65 20 61 6e  tabase, table an
44b80 64 20 63 6f 6c 75 6d 6e 20 6f 6e 20 77 68 69 63  d column on whic
44b90 68 20 74 68 65 20 62 6c 6f 62 20 68 61 6e 64 6c  h the blob handl
44ba0 65 20 69 73 20 6f 70 65 6e 0a 2a 2a 20 72 65 6d  e is open.** rem
44bb0 61 69 6e 20 74 68 65 20 73 61 6d 65 2e 20 4d 6f  ain the same. Mo
44bc0 76 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67  ving an existing
44bd0 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 74 6f 20   blob handle to 
44be0 61 20 6e 65 77 20 72 6f 77 20 63 61 6e 20 62 65  a new row can be
44bf0 0a 2a 2a 20 66 61 73 74 65 72 20 74 68 61 6e 20  .** faster than 
44c00 63 6c 6f 73 69 6e 67 20 74 68 65 20 65 78 69 73  closing the exis
44c10 74 69 6e 67 20 68 61 6e 64 6c 65 20 61 6e 64 20  ting handle and 
44c20 6f 70 65 6e 69 6e 67 20 61 20 6e 65 77 20 6f 6e  opening a new on
44c30 65 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65 20 6e  e..**.** ^(The n
44c40 65 77 20 72 6f 77 20 6d 75 73 74 20 6d 65 65 74  ew row must meet
44c50 20 74 68 65 20 73 61 6d 65 20 63 72 69 74 65 72   the same criter
44c60 69 61 20 61 73 20 66 6f 72 20 5b 73 71 6c 69 74  ia as for [sqlit
44c70 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20  e3_blob_open()] 
44c80 2d 0a 2a 2a 20 69 74 20 6d 75 73 74 20 65 78 69  -.** it must exi
44c90 73 74 20 61 6e 64 20 74 68 65 72 65 20 6d 75 73  st and there mus
44ca0 74 20 62 65 20 65 69 74 68 65 72 20 61 20 62 6c  t be either a bl
44cb0 6f 62 20 6f 72 20 74 65 78 74 20 76 61 6c 75 65  ob or text value
44cc0 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 74 68   stored in.** th
44cd0 65 20 6e 6f 6d 69 6e 61 74 65 64 20 63 6f 6c 75  e nominated colu
44ce0 6d 6e 2e 29 5e 20 5e 49 66 20 74 68 65 20 6e 65  mn.)^ ^If the ne
44cf0 77 20 72 6f 77 20 69 73 20 6e 6f 74 20 70 72 65  w row is not pre
44d00 73 65 6e 74 20 69 6e 20 74 68 65 20 74 61 62 6c  sent in the tabl
44d10 65 2c 20 6f 72 20 69 66 0a 2a 2a 20 69 74 20 64  e, or if.** it d
44d20 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20  oes not contain 
44d30 61 20 62 6c 6f 62 20 6f 72 20 74 65 78 74 20 76  a blob or text v
44d40 61 6c 75 65 2c 20 6f 72 20 69 66 20 61 6e 6f 74  alue, or if anot
44d50 68 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73  her error occurs
44d60 2c 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 65  , an.** SQLite e
44d70 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
44d80 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 62 6c  urned and the bl
44d90 6f 62 20 68 61 6e 64 6c 65 20 69 73 20 63 6f 6e  ob handle is con
44da0 73 69 64 65 72 65 64 20 61 62 6f 72 74 65 64 2e  sidered aborted.
44db0 0a 2a 2a 20 5e 41 6c 6c 20 73 75 62 73 65 71 75  .** ^All subsequ
44dc0 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71  ent calls to [sq
44dd0 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28  lite3_blob_read(
44de0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  )], [sqlite3_blo
44df0 62 5f 77 72 69 74 65 28 29 5d 20 6f 72 0a 2a 2a  b_write()] or.**
44e00 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72   [sqlite3_blob_r
44e10 65 6f 70 65 6e 28 29 5d 20 6f 6e 20 61 6e 20 61  eopen()] on an a
44e20 62 6f 72 74 65 64 20 62 6c 6f 62 20 68 61 6e 64  borted blob hand
44e30 6c 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72  le immediately r
44e40 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f  eturn.** SQLITE_
44e50 41 42 4f 52 54 2e 20 5e 43 61 6c 6c 69 6e 67 20  ABORT. ^Calling 
44e60 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79  [sqlite3_blob_by
44e70 74 65 73 28 29 5d 20 6f 6e 20 61 6e 20 61 62 6f  tes()] on an abo
44e80 72 74 65 64 20 62 6c 6f 62 20 68 61 6e 64 6c 65  rted blob handle
44e90 0a 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72  .** always retur
44ea0 6e 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 5e  ns zero..**.** ^
44eb0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65  This function se
44ec0 74 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ts the database 
44ed0 68 61 6e 64 6c 65 20 65 72 72 6f 72 20 63 6f 64  handle error cod
44ee0 65 20 61 6e 64 20 6d 65 73 73 61 67 65 2e 0a 2a  e and message..*
44ef0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  /.SQLITE_API SQL
44f00 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
44f10 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f   int sqlite3_blo
44f20 62 5f 72 65 6f 70 65 6e 28 73 71 6c 69 74 65 33  b_reopen(sqlite3
44f30 5f 62 6c 6f 62 20 2a 2c 20 73 71 6c 69 74 65 33  _blob *, sqlite3
44f40 5f 69 6e 74 36 34 29 3b 0a 0a 2f 2a 0a 2a 2a 20  _int64);../*.** 
44f50 43 41 50 49 33 52 45 46 3a 20 43 6c 6f 73 65 20  CAPI3REF: Close 
44f60 41 20 42 4c 4f 42 20 48 61 6e 64 6c 65 0a 2a 2a  A BLOB Handle.**
44f70 0a 2a 2a 20 5e 43 6c 6f 73 65 73 20 61 6e 20 6f  .** ^Closes an o
44f80 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65  pen [BLOB handle
44f90 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 43 6c 6f 73 69 6e  ]..**.** ^Closin
44fa0 67 20 61 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63  g a BLOB shall c
44fb0 61 75 73 65 20 74 68 65 20 63 75 72 72 65 6e 74  ause the current
44fc0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20   transaction to 
44fd0 63 6f 6d 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65  commit.** if the
44fe0 72 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20  re are no other 
44ff0 42 4c 4f 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e  BLOBs, no pendin
45000 67 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  g prepared state
45010 6d 65 6e 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a  ments, and the.*
45020 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
45030 63 74 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74  ction is in [aut
45040 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a  ocommit mode]..*
45050 2a 20 5e 49 66 20 61 6e 79 20 77 72 69 74 65 73  * ^If any writes
45060 20 77 65 72 65 20 6d 61 64 65 20 74 6f 20 74 68   were made to th
45070 65 20 42 4c 4f 42 2c 20 74 68 65 79 20 6d 69 67  e BLOB, they mig
45080 68 74 20 62 65 20 68 65 6c 64 20 69 6e 20 63 61  ht be held in ca
45090 63 68 65 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65  che.** until the
450a0 20 63 6c 6f 73 65 20 6f 70 65 72 61 74 69 6f 6e   close operation
450b0 20 69 66 20 74 68 65 79 20 77 69 6c 6c 20 66 69   if they will fi
450c0 74 2e 0a 2a 2a 0a 2a 2a 20 5e 28 43 6c 6f 73 69  t..**.** ^(Closi
450d0 6e 67 20 74 68 65 20 42 4c 4f 42 20 6f 66 74 65  ng the BLOB ofte
450e0 6e 20 66 6f 72 63 65 73 20 74 68 65 20 63 68 61  n forces the cha
450f0 6e 67 65 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 64  nges.** out to d
45100 69 73 6b 20 61 6e 64 20 73 6f 20 69 66 20 61 6e  isk and so if an
45110 79 20 49 2f 4f 20 65 72 72 6f 72 73 20 6f 63 63  y I/O errors occ
45120 75 72 2c 20 74 68 65 79 20 77 69 6c 6c 20 6c 69  ur, they will li
45130 6b 65 6c 79 20 6f 63 63 75 72 0a 2a 2a 20 61 74  kely occur.** at
45140 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e 20 74   the time when t
45150 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65  he BLOB is close
45160 64 2e 20 20 41 6e 79 20 65 72 72 6f 72 73 20 74  d.  Any errors t
45170 68 61 74 20 6f 63 63 75 72 20 64 75 72 69 6e 67  hat occur during
45180 0a 2a 2a 20 63 6c 6f 73 69 6e 67 20 61 72 65 20  .** closing are 
45190 72 65 70 6f 72 74 65 64 20 61 73 20 61 20 6e 6f  reported as a no
451a0 6e 2d 7a 65 72 6f 20 72 65 74 75 72 6e 20 76 61  n-zero return va
451b0 6c 75 65 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 28 54  lue.)^.**.** ^(T
451c0 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65  he BLOB is close
451d0 64 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c  d unconditionall
451e0 79 2e 20 20 45 76 65 6e 20 69 66 20 74 68 69 73  y.  Even if this
451f0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
45200 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  .** an error cod
45210 65 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 20 73  e, the BLOB is s
45220 74 69 6c 6c 20 63 6c 6f 73 65 64 2e 29 5e 0a 2a  till closed.)^.*
45230 2a 0a 2a 2a 20 5e 43 61 6c 6c 69 6e 67 20 74 68  *.** ^Calling th
45240 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20  is routine with 
45250 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 20 28  a null pointer (
45260 73 75 63 68 20 61 73 20 77 6f 75 6c 64 20 62 65  such as would be
45270 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20   returned.** by 
45280 61 20 66 61 69 6c 65 64 20 63 61 6c 6c 20 74 6f  a failed call to
45290 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
452a0 70 65 6e 28 29 5d 29 20 69 73 20 61 20 68 61 72  pen()]) is a har
452b0 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a  mless no-op..*/.
452c0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
452d0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73  qlite3_blob_clos
452e0 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a  e(sqlite3_blob *
452f0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
45300 45 46 3a 20 52 65 74 75 72 6e 20 54 68 65 20 53  EF: Return The S
45310 69 7a 65 20 4f 66 20 41 6e 20 4f 70 65 6e 20 42  ize Of An Open B
45320 4c 4f 42 0a 2a 2a 0a 2a 2a 20 5e 52 65 74 75 72  LOB.**.** ^Retur
45330 6e 73 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62  ns the size in b
45340 79 74 65 73 20 6f 66 20 74 68 65 20 42 4c 4f 42  ytes of the BLOB
45350 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 20   accessible via 
45360 74 68 65 20 0a 2a 2a 20 73 75 63 63 65 73 73 66  the .** successf
45370 75 6c 6c 79 20 6f 70 65 6e 65 64 20 5b 42 4c 4f  ully opened [BLO
45380 42 20 68 61 6e 64 6c 65 5d 20 69 6e 20 69 74 73  B handle] in its
45390 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e 74 2e 20   only argument. 
453a0 20 5e 54 68 65 0a 2a 2a 20 69 6e 63 72 65 6d 65   ^The.** increme
453b0 6e 74 61 6c 20 62 6c 6f 62 20 49 2f 4f 20 72 6f  ntal blob I/O ro
453c0 75 74 69 6e 65 73 20 63 61 6e 20 6f 6e 6c 79 20  utines can only 
453d0 72 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74  read or overwrit
453e0 69 6e 67 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20  ing existing.** 
453f0 62 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 20 74 68  blob content; th
45400 65 79 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65  ey cannot change
45410 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 62   the size of a b
45420 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  lob..**.** This 
45430 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72  routine only wor
45440 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20 68 61  ks on a [BLOB ha
45450 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 61 73 20  ndle] which has 
45460 62 65 65 6e 20 63 72 65 61 74 65 64 0a 2a 2a 20  been created.** 
45470 62 79 20 61 20 70 72 69 6f 72 20 73 75 63 63 65  by a prior succe
45480 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73  ssful call to [s
45490 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
454a0 28 29 5d 20 61 6e 64 20 77 68 69 63 68 20 68 61  ()] and which ha
454b0 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e 20 63 6c  s not.** been cl
454c0 6f 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  osed by [sqlite3
454d0 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 20  _blob_close()]. 
454e0 20 50 61 73 73 69 6e 67 20 61 6e 79 20 6f 74 68   Passing any oth
454f0 65 72 20 70 6f 69 6e 74 65 72 20 69 6e 0a 2a 2a  er pointer in.**
45500 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
45510 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65   results in unde
45520 66 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61 62  fined and probab
45530 6c 79 20 75 6e 64 65 73 69 72 61 62 6c 65 20 62  ly undesirable b
45540 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49  ehavior..*/.SQLI
45550 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
45560 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 73 71  e3_blob_bytes(sq
45570 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a 0a  lite3_blob *);..
45580 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
45590 52 65 61 64 20 44 61 74 61 20 46 72 6f 6d 20 41  Read Data From A
455a0 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61   BLOB Incrementa
455b0 6c 6c 79 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 69 73  lly.**.** ^(This
455c0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
455d0 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 20 66  d to read data f
455e0 72 6f 6d 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f  rom an open [BLO
455f0 42 20 68 61 6e 64 6c 65 5d 20 69 6e 74 6f 20 61  B handle] into a
45600 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c  .** caller-suppl
45610 69 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62 79  ied buffer. N by
45620 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20  tes of data are 
45630 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 75 66 66  copied into buff
45640 65 72 20 5a 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  er Z.** from the
45650 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72   open BLOB, star
45660 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20 69  ting at offset i
45670 4f 66 66 73 65 74 2e 29 5e 0a 2a 2a 0a 2a 2a 20  Offset.)^.**.** 
45680 5e 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66 73  ^If offset iOffs
45690 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  et is less than 
456a0 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65  N bytes from the
456b0 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f 42   end of the BLOB
456c0 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52  ,.** [SQLITE_ERR
456d0 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20  OR] is returned 
456e0 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72  and no data is r
456f0 65 61 64 2e 20 20 5e 49 66 20 4e 20 6f 72 20 69  ead.  ^If N or i
45700 4f 66 66 73 65 74 20 69 73 0a 2a 2a 20 6c 65 73  Offset is.** les
45710 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 5b 53 51  s than zero, [SQ
45720 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72  LITE_ERROR] is r
45730 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64  eturned and no d
45740 61 74 61 20 69 73 20 72 65 61 64 2e 0a 2a 2a 20  ata is read..** 
45750 5e 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65  ^The size of the
45760 20 62 6c 6f 62 20 28 61 6e 64 20 68 65 6e 63 65   blob (and hence
45770 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c   the maximum val
45780 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 74 29  ue of N+iOffset)
45790 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 65 72  .** can be deter
457a0 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20  mined using the 
457b0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79  [sqlite3_blob_by
457c0 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65  tes()] interface
457d0 2e 0a 2a 2a 0a 2a 2a 20 5e 41 6e 20 61 74 74 65  ..**.** ^An atte
457e0 6d 70 74 20 74 6f 20 72 65 61 64 20 66 72 6f 6d  mpt to read from
457f0 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c 4f   an expired [BLO
45800 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20  B handle] fails 
45810 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72  with an.** error
45820 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45   code of [SQLITE
45830 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 5e  _ABORT]..**.** ^
45840 28 4f 6e 20 73 75 63 63 65 73 73 2c 20 73 71 6c  (On success, sql
45850 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29  ite3_blob_read()
45860 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
45870 4f 4b 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  OK..** Otherwise
45880 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  , an [error code
45890 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 65  ] or an [extende
458a0 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73  d error code] is
458b0 20 72 65 74 75 72 6e 65 64 2e 29 5e 0a 2a 2a 0a   returned.)^.**.
458c0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
458d0 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20  only works on a 
458e0 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68  [BLOB handle] wh
458f0 69 63 68 20 68 61 73 20 62 65 65 6e 20 63 72 65  ich has been cre
45900 61 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69  ated.** by a pri
45910 6f 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 61  or successful ca
45920 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62  ll to [sqlite3_b
45930 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20  lob_open()] and 
45940 77 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a  which has not.**
45950 20 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20   been closed by 
45960 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c  [sqlite3_blob_cl
45970 6f 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67  ose()].  Passing
45980 20 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74   any other point
45990 65 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73  er in.** to this
459a0 20 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73   routine results
459b0 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e   in undefined an
459c0 64 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73  d probably undes
459d0 69 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e  irable behavior.
459e0 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
459f0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77   [sqlite3_blob_w
45a00 72 69 74 65 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49  rite()]..*/.SQLI
45a10 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
45a20 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c  e3_blob_read(sql
45a30 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 76 6f 69  ite3_blob *, voi
45a40 64 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 69 6e 74  d *Z, int N, int
45a50 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a   iOffset);../*.*
45a60 2a 20 43 41 50 49 33 52 45 46 3a 20 57 72 69 74  * CAPI3REF: Writ
45a70 65 20 44 61 74 61 20 49 6e 74 6f 20 41 20 42 4c  e Data Into A BL
45a80 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79  OB Incrementally
45a90 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 66 75 6e  .**.** ^This fun
45aa0 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ction is used to
45ab0 20 77 72 69 74 65 20 64 61 74 61 20 69 6e 74 6f   write data into
45ac0 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68   an open [BLOB h
45ad0 61 6e 64 6c 65 5d 20 66 72 6f 6d 20 61 0a 2a 2a  andle] from a.**
45ae0 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64   caller-supplied
45af0 20 62 75 66 66 65 72 2e 20 5e 4e 20 62 79 74 65   buffer. ^N byte
45b00 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f  s of data are co
45b10 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75  pied from the bu
45b20 66 66 65 72 20 5a 0a 2a 2a 20 69 6e 74 6f 20 74  ffer Z.** into t
45b30 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74  he open BLOB, st
45b40 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74  arting at offset
45b50 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20   iOffset..**.** 
45b60 5e 49 66 20 74 68 65 20 5b 42 4c 4f 42 20 68 61  ^If the [BLOB ha
45b70 6e 64 6c 65 5d 20 70 61 73 73 65 64 20 61 73 20  ndle] passed as 
45b80 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
45b90 6e 74 20 77 61 73 20 6e 6f 74 20 6f 70 65 6e 65  nt was not opene
45ba0 64 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67  d for.** writing
45bb0 20 28 74 68 65 20 66 6c 61 67 73 20 70 61 72 61   (the flags para
45bc0 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
45bd0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 77  3_blob_open()] w
45be0 61 73 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74 68 69  as zero),.** thi
45bf0 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
45c00 6e 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f  ns [SQLITE_READO
45c10 4e 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 69  NLY]..**.** ^Thi
45c20 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f  s function may o
45c30 6e 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 63  nly modify the c
45c40 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 42  ontents of the B
45c50 4c 4f 42 3b 20 69 74 20 69 73 0a 2a 2a 20 6e 6f  LOB; it is.** no
45c60 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e  t possible to in
45c70 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20  crease the size 
45c80 6f 66 20 61 20 42 4c 4f 42 20 75 73 69 6e 67 20  of a BLOB using 
45c90 74 68 69 73 20 41 50 49 2e 0a 2a 2a 20 5e 49 66  this API..** ^If
45ca0 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 20   offset iOffset 
45cb0 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62  is less than N b
45cc0 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e  ytes from the en
45cd0 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a  d of the BLOB,.*
45ce0 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d  * [SQLITE_ERROR]
45cf0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
45d00 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69 74   no data is writ
45d10 74 65 6e 2e 20 20 5e 49 66 20 4e 20 69 73 0a 2a  ten.  ^If N is.*
45d20 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f  * less than zero
45d30 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
45d40 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
45d50 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74  no data is writt
45d60 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20  en..** The size 
45d70 6f 66 20 74 68 65 20 42 4c 4f 42 20 28 61 6e 64  of the BLOB (and
45d80 20 68 65 6e 63 65 20 74 68 65 20 6d 61 78 69 6d   hence the maxim
45d90 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e 2b 69 4f  um value of N+iO
45da0 66 66 73 65 74 29 0a 2a 2a 20 63 61 6e 20 62 65  ffset).** can be
45db0 20 64 65 74 65 72 6d 69 6e 65 64 20 75 73 69 6e   determined usin
45dc0 67 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62  g the [sqlite3_b
45dd0 6c 6f 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74  lob_bytes()] int
45de0 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 5e 41  erface..**.** ^A
45df0 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69  n attempt to wri
45e00 74 65 20 74 6f 20 61 6e 20 65 78 70 69 72 65 64  te to an expired
45e10 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66   [BLOB handle] f
45e20 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20  ails with an.** 
45e30 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53  error code of [S
45e40 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 20 20 5e  QLITE_ABORT].  ^
45e50 57 72 69 74 65 73 20 74 6f 20 74 68 65 20 42 4c  Writes to the BL
45e60 4f 42 20 74 68 61 74 20 6f 63 63 75 72 72 65 64  OB that occurred
45e70 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 5b  .** before the [
45e80 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65 78 70  BLOB handle] exp
45e90 69 72 65 64 20 61 72 65 20 6e 6f 74 20 72 6f 6c  ired are not rol
45ea0 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 65 0a  led back by the.
45eb0 2a 2a 20 65 78 70 69 72 61 74 69 6f 6e 20 6f 66  ** expiration of
45ec0 20 74 68 65 20 68 61 6e 64 6c 65 2c 20 74 68 6f   the handle, tho
45ed0 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20 74 68  ugh of course th
45ee0 6f 73 65 20 63 68 61 6e 67 65 73 20 6d 69 67 68  ose changes migh
45ef0 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f  t.** have been o
45f00 76 65 72 77 72 69 74 74 65 6e 20 62 79 20 74 68  verwritten by th
45f10 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  e statement that
45f20 20 65 78 70 69 72 65 64 20 74 68 65 20 42 4c 4f   expired the BLO
45f30 42 20 68 61 6e 64 6c 65 0a 2a 2a 20 6f 72 20 62  B handle.** or b
45f40 79 20 6f 74 68 65 72 20 69 6e 64 65 70 65 6e 64  y other independ
45f50 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  ent statements..
45f60 2a 2a 0a 2a 2a 20 5e 28 4f 6e 20 73 75 63 63 65  **.** ^(On succe
45f70 73 73 2c 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ss, sqlite3_blob
45f80 5f 77 72 69 74 65 28 29 20 72 65 74 75 72 6e 73  _write() returns
45f90 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 4f   SQLITE_OK..** O
45fa0 74 68 65 72 77 69 73 65 2c 20 61 6e 20 20 5b 65  therwise, an  [e
45fb0 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e  rror code] or an
45fc0 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72   [extended error
45fd0 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e   code] is return
45fe0 65 64 2e 29 5e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ed.)^.**.** This
45ff0 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f   routine only wo
46000 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20 68  rks on a [BLOB h
46010 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 61 73  andle] which has
46020 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a 2a 2a   been created.**
46030 20 62 79 20 61 20 70 72 69 6f 72 20 73 75 63 63   by a prior succ
46040 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b  essful call to [
46050 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
46060 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68 20 68  n()] and which h
46070 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e 20 63  as not.** been c
46080 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65  losed by [sqlite
46090 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e  3_blob_close()].
460a0 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20 6f 74    Passing any ot
460b0 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e 0a 2a  her pointer in.*
460c0 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  * to this routin
460d0 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64  e results in und
460e0 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61  efined and proba
460f0 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c 65 20  bly undesirable 
46100 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20  behavior..**.** 
46110 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74  See also: [sqlit
46120 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 2e  e3_blob_read()].
46130 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
46140 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  nt sqlite3_blob_
46150 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c  write(sqlite3_bl
46160 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ob *, const void
46170 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20   *z, int n, int 
46180 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a  iOffset);../*.**
46190 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75   CAPI3REF: Virtu
461a0 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d 20 4f  al File System O
461b0 62 6a 65 63 74 73 0a 2a 2a 0a 2a 2a 20 41 20 76  bjects.**.** A v
461c0 69 72 74 75 61 6c 20 66 69 6c 65 73 79 73 74 65  irtual filesyste
461d0 6d 20 28 56 46 53 29 20 69 73 20 61 6e 20 5b 73  m (VFS) is an [s
461e0 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
461f0 63 74 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74  ct.** that SQLit
46200 65 20 75 73 65 73 20 74 6f 20 69 6e 74 65 72 61  e uses to intera
46210 63 74 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 75  ct.** with the u
46220 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74  nderlying operat
46230 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 4d 6f 73  ing system.  Mos
46240 74 20 53 51 4c 69 74 65 20 62 75 69 6c 64 73 20  t SQLite builds 
46250 63 6f 6d 65 20 77 69 74 68 20 61 0a 2a 2a 20 73  come with a.** s
46260 69 6e 67 6c 65 20 64 65 66 61 75 6c 74 20 56 46  ingle default VF
46270 53 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70  S that is approp
46280 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 68 6f  riate for the ho
46290 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a 2a 2a 20  st computer..** 
462a0 4e 65 77 20 56 46 53 65 73 20 63 61 6e 20 62 65  New VFSes can be
462b0 20 72 65 67 69 73 74 65 72 65 64 20 61 6e 64 20   registered and 
462c0 65 78 69 73 74 69 6e 67 20 56 46 53 65 73 20 63  existing VFSes c
462d0 61 6e 20 62 65 20 75 6e 72 65 67 69 73 74 65 72  an be unregister
462e0 65 64 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ed..** The follo
462f0 77 69 6e 67 20 69 6e 74 65 72 66 61 63 65 73 20  wing interfaces 
46300 61 72 65 20 70 72 6f 76 69 64 65 64 2e 0a 2a 2a  are provided..**
46310 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33  .** ^The sqlite3
46320 5f 76 66 73 5f 66 69 6e 64 28 29 20 69 6e 74 65  _vfs_find() inte
46330 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20  rface returns a 
46340 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 56 46 53  pointer to a VFS
46350 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e   given its name.
46360 0a 2a 2a 20 5e 4e 61 6d 65 73 20 61 72 65 20 63  .** ^Names are c
46370 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a  ase sensitive..*
46380 2a 20 5e 4e 61 6d 65 73 20 61 72 65 20 7a 65 72  * ^Names are zer
46390 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46  o-terminated UTF
463a0 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 5e  -8 strings..** ^
463b0 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  If there is no m
463c0 61 74 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69  atch, a NULL poi
463d0 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64  nter is returned
463e0 2e 0a 2a 2a 20 5e 49 66 20 7a 56 66 73 4e 61 6d  ..** ^If zVfsNam
463f0 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74  e is NULL then t
46400 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69  he default VFS i
46410 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
46420 2a 20 5e 4e 65 77 20 56 46 53 65 73 20 61 72 65  * ^New VFSes are
46430 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68   registered with
46440 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67   sqlite3_vfs_reg
46450 69 73 74 65 72 28 29 2e 0a 2a 2a 20 5e 45 61 63  ister()..** ^Eac
46460 68 20 6e 65 77 20 56 46 53 20 62 65 63 6f 6d 65  h new VFS become
46470 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 56 46  s the default VF
46480 53 20 69 66 20 74 68 65 20 6d 61 6b 65 44 66 6c  S if the makeDfl
46490 74 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a  t flag is set..*
464a0 2a 20 5e 54 68 65 20 73 61 6d 65 20 56 46 53 20  * ^The same VFS 
464b0 63 61 6e 20 62 65 20 72 65 67 69 73 74 65 72 65  can be registere
464c0 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73  d multiple times
464d0 20 77 69 74 68 6f 75 74 20 69 6e 6a 75 72 79 2e   without injury.
464e0 0a 2a 2a 20 5e 54 6f 20 6d 61 6b 65 20 61 6e 20  .** ^To make an 
464f0 65 78 69 73 74 69 6e 67 20 56 46 53 20 69 6e 74  existing VFS int
46500 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 56 46  o the default VF
46510 53 2c 20 72 65 67 69 73 74 65 72 20 69 74 20 61  S, register it a
46520 67 61 69 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65  gain.** with the
46530 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 73   makeDflt flag s
46540 65 74 2e 20 20 49 66 20 74 77 6f 20 64 69 66 66  et.  If two diff
46550 65 72 65 6e 74 20 56 46 53 65 73 20 77 69 74 68  erent VFSes with
46560 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6e 61 6d   the.** same nam
46570 65 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64  e are registered
46580 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  , the behavior i
46590 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49 66  s undefined.  If
465a0 20 61 0a 2a 2a 20 56 46 53 20 69 73 20 72 65 67   a.** VFS is reg
465b0 69 73 74 65 72 65 64 20 77 69 74 68 20 61 20 6e  istered with a n
465c0 61 6d 65 20 74 68 61 74 20 69 73 20 4e 55 4c 4c  ame that is NULL
465d0 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73 74 72   or an empty str
465e0 69 6e 67 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ing,.** then the
465f0 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64   behavior is und
46600 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 5e 55  efined..**.** ^U
46610 6e 72 65 67 69 73 74 65 72 20 61 20 56 46 53 20  nregister a VFS 
46620 77 69 74 68 20 74 68 65 20 73 71 6c 69 74 65 33  with the sqlite3
46630 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28  _vfs_unregister(
46640 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20  ) interface..** 
46650 5e 28 49 66 20 74 68 65 20 64 65 66 61 75 6c 74  ^(If the default
46660 20 56 46 53 20 69 73 20 75 6e 72 65 67 69 73 74   VFS is unregist
46670 65 72 65 64 2c 20 61 6e 6f 74 68 65 72 20 56 46  ered, another VF
46680 53 20 69 73 20 63 68 6f 73 65 6e 20 61 73 0a 2a  S is chosen as.*
46690 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20  * the default.  
466a0 54 68 65 20 63 68 6f 69 63 65 20 66 6f 72 20 74  The choice for t
466b0 68 65 20 6e 65 77 20 56 46 53 20 69 73 20 61 72  he new VFS is ar
466c0 62 69 74 72 61 72 79 2e 29 5e 0a 2a 2f 0a 53 51  bitrary.)^.*/.SQ
466d0 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
466e0 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66  _vfs *sqlite3_vf
466f0 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61  s_find(const cha
46700 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a 53 51  r *zVfsName);.SQ
46710 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
46720 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65  ite3_vfs_registe
46730 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20  r(sqlite3_vfs*, 
46740 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b 0a 53  int makeDflt);.S
46750 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
46760 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69  lite3_vfs_unregi
46770 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73  ster(sqlite3_vfs
46780 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
46790 52 45 46 3a 20 4d 75 74 65 78 65 73 0a 2a 2a 0a  REF: Mutexes.**.
467a0 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f  ** The SQLite co
467b0 72 65 20 75 73 65 73 20 74 68 65 73 65 20 72 6f  re uses these ro
467c0 75 74 69 6e 65 73 20 66 6f 72 20 74 68 72 65 61  utines for threa
467d0 64 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e 69 7a 61  d.** synchroniza
467e0 74 69 6f 6e 2e 20 54 68 6f 75 67 68 20 74 68 65  tion. Though the
467f0 79 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66  y are intended f
46800 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75  or internal.** u
46810 73 65 20 62 79 20 53 51 4c 69 74 65 2c 20 63 6f  se by SQLite, co
46820 64 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67  de that links ag
46830 61 69 6e 73 74 20 53 51 4c 69 74 65 20 69 73 0a  ainst SQLite is.
46840 2a 2a 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20  ** permitted to 
46850 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65  use any of these
46860 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a   routines..**.**
46870 20 54 68 65 20 53 51 4c 69 74 65 20 73 6f 75 72   The SQLite sour
46880 63 65 20 63 6f 64 65 20 63 6f 6e 74 61 69 6e 73  ce code contains
46890 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d   multiple implem
468a0 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20  entations.** of 
468b0 74 68 65 73 65 20 6d 75 74 65 78 20 72 6f 75 74  these mutex rout
468c0 69 6e 65 73 2e 20 20 41 6e 20 61 70 70 72 6f 70  ines.  An approp
468d0 72 69 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61  riate implementa
468e0 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 65 6c 65 63  tion.** is selec
468f0 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ted automaticall
46900 79 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  y at compile-tim
46910 65 2e 20 20 5e 28 54 68 65 20 66 6f 6c 6c 6f 77  e.  ^(The follow
46920 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ing.** implement
46930 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61 69 6c  ations are avail
46940 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51 4c 69  able in the SQLi
46950 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20 3c  te core:.**.** <
46960 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51  ul>.** <li>   SQ
46970 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45  LITE_MUTEX_PTHRE
46980 41 44 53 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51  ADS.** <li>   SQ
46990 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 0a 2a  LITE_MUTEX_W32.*
469a0 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f  * <li>   SQLITE_
469b0 4d 55 54 45 58 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f  MUTEX_NOOP.** </
469c0 75 6c 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65  ul>)^.**.** ^The
469d0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f   SQLITE_MUTEX_NO
469e0 4f 50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  OP implementatio
469f0 6e 20 69 73 20 61 20 73 65 74 20 6f 66 20 72 6f  n is a set of ro
46a00 75 74 69 6e 65 73 0a 2a 2a 20 74 68 61 74 20 64  utines.** that d
46a10 6f 65 73 20 6e 6f 20 72 65 61 6c 20 6c 6f 63 6b  oes no real lock
46a20 69 6e 67 20 61 6e 64 20 69 73 20 61 70 70 72 6f  ing and is appro
46a30 70 72 69 61 74 65 20 66 6f 72 20 75 73 65 20 69  priate for use i
46a40 6e 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 2d 74 68  n.** a single-th
46a50 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69  readed applicati
46a60 6f 6e 2e 20 20 5e 54 68 65 20 53 51 4c 49 54 45  on.  ^The SQLITE
46a70 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 20  _MUTEX_PTHREADS 
46a80 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55  and.** SQLITE_MU
46a90 54 45 58 5f 57 33 32 20 69 6d 70 6c 65 6d 65 6e  TEX_W32 implemen
46aa0 74 61 74 69 6f 6e 73 20 61 72 65 20 61 70 70 72  tations are appr
46ab0 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73 65 20  opriate for use 
46ac0 6f 6e 20 55 6e 69 78 0a 2a 2a 20 61 6e 64 20 57  on Unix.** and W
46ad0 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 5e 28  indows..**.** ^(
46ae0 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  If SQLite is com
46af0 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53  piled with the S
46b00 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44  QLITE_MUTEX_APPD
46b10 45 46 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a  EF preprocessor.
46b20 2a 2a 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64  ** macro defined
46b30 20 28 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45   (with "-DSQLITE
46b40 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22  _MUTEX_APPDEF=1"
46b50 29 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78  ), then no mutex
46b60 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
46b70 6f 6e 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77  on is included w
46b80 69 74 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e  ith the library.
46b90 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
46ba0 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e  e.** application
46bb0 20 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 63   must supply a c
46bc0 75 73 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c  ustom mutex impl
46bd0 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67  ementation using
46be0 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f   the.** [SQLITE_
46bf0 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70  CONFIG_MUTEX] op
46c00 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
46c10 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e  te3_config() fun
46c20 63 74 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20  ction.** before 
46c30 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f  calling sqlite3_
46c40 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20  initialize() or 
46c50 61 6e 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63  any other public
46c60 20 73 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e   sqlite3_.** fun
46c70 63 74 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73  ction that calls
46c80 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
46c90 69 7a 65 28 29 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e  ize().)^.**.** ^
46ca0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
46cb0 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e  x_alloc() routin
46cc0 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65  e allocates a ne
46cd0 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72  w.** mutex and r
46ce0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
46cf0 20 74 6f 20 69 74 2e 20 5e 49 66 20 69 74 20 72   to it. ^If it r
46d00 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74  eturns NULL.** t
46d10 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61  hat means that a
46d20 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74   mutex could not
46d30 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20   be allocated.  
46d40 5e 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20  ^SQLite.** will 
46d50 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b  unwind its stack
46d60 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65   and return an e
46d70 72 72 6f 72 2e 20 20 5e 28 54 68 65 20 61 72 67  rror.  ^(The arg
46d80 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69  ument.** to sqli
46d90 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
46da0 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73  ) is one of thes
46db0 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61  e integer consta
46dc0 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a  nts:.**.** <ul>.
46dd0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
46de0 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c  MUTEX_FAST.** <l
46df0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
46e00 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 3c 6c  _RECURSIVE.** <l
46e10 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
46e20 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a  _STATIC_MASTER.*
46e30 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
46e40 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 0a  UTEX_STATIC_MEM.
46e50 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
46e60 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
46e70 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  2.** <li>  SQLIT
46e80 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50  E_MUTEX_STATIC_P
46e90 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  RNG.** <li>  SQL
46ea0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
46eb0 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  _LRU.** <li>  SQ
46ec0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
46ed0 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c 3e 29  C_LRU2.** </ul>)
46ee0 5e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 72  ^.**.** ^The fir
46ef0 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73  st two constants
46f00 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46   (SQLITE_MUTEX_F
46f10 41 53 54 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d  AST and SQLITE_M
46f20 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 0a  UTEX_RECURSIVE).
46f30 2a 2a 20 63 61 75 73 65 20 73 71 6c 69 74 65 33  ** cause sqlite3
46f40 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74  _mutex_alloc() t
46f50 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65  o create.** a ne
46f60 77 20 6d 75 74 65 78 2e 20 20 5e 54 68 65 20 6e  w mutex.  ^The n
46f70 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 75  ew mutex is recu
46f80 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54  rsive when SQLIT
46f90 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
46fa0 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74  E.** is used but
46fb0 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
46fc0 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f   so when SQLITE_
46fd0 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 73  MUTEX_FAST is us
46fe0 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78  ed..** The mutex
46ff0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
47000 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
47010 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74   make a distinct
47020 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53  ion.** between S
47030 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
47040 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45  RSIVE and SQLITE
47050 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69  _MUTEX_FAST if i
47060 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61  t does.** not wa
47070 6e 74 20 74 6f 2e 20 20 5e 53 51 4c 69 74 65 20  nt to.  ^SQLite 
47080 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73  will only reques
47090 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75  t a recursive mu
470a0 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20  tex in.** cases 
470b0 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20  where it really 
470c0 6e 65 65 64 73 20 6f 6e 65 2e 20 20 5e 49 66 20  needs one.  ^If 
470d0 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63  a faster non-rec
470e0 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20  ursive mutex.** 
470f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
47100 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74  s available on t
47110 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d  he host platform
47120 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73  , the mutex subs
47130 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72  ystem.** might r
47140 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74  eturn such a mut
47150 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74  ex in response t
47160 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  o SQLITE_MUTEX_F
47170 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  AST..**.** ^The 
47180 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61  other allowed pa
47190 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69  rameters to sqli
471a0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
471b0 29 20 28 61 6e 79 74 68 69 6e 67 20 6f 74 68 65  ) (anything othe
471c0 72 0a 2a 2a 20 74 68 61 6e 20 53 51 4c 49 54 45  r.** than SQLITE
471d0 5f 4d 55 54 45 58 5f 46 41 53 54 20 61 6e 64 20  _MUTEX_FAST and 
471e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
471f0 55 52 53 49 56 45 29 20 65 61 63 68 20 72 65 74  URSIVE) each ret
47200 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  urn.** a pointer
47210 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 72 65   to a static pre
47220 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20  existing mutex. 
47230 20 5e 53 69 78 20 73 74 61 74 69 63 20 6d 75 74   ^Six static mut
47240 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64  exes are.** used
47250 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20   by the current 
47260 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74  version of SQLit
47270 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69  e.  Future versi
47280 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a  ons of SQLite.**
47290 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 6f   may add additio
472a0 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78  nal static mutex
472b0 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65  es.  Static mute
472c0 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65  xes are for inte
472d0 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53  rnal.** use by S
472e0 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70  QLite only.  App
472f0 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75  lications that u
47300 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65  se SQLite mutexe
47310 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20  s should.** use 
47320 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63  only the dynamic
47330 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65   mutexes returne
47340 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45  d by SQLITE_MUTE
47350 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c  X_FAST or.** SQL
47360 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
47370 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 5e 4e 6f 74 65  IVE..**.** ^Note
47380 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20   that if one of 
47390 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  the dynamic mute
473a0 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51  x parameters (SQ
473b0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a  LITE_MUTEX_FAST.
473c0 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54  ** or SQLITE_MUT
473d0 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73  EX_RECURSIVE) is
473e0 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74   used then sqlit
473f0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
47400 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69  .** returns a di
47410 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e  fferent mutex on
47420 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 5e 42   every call.  ^B
47430 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 69  ut for the stati
47440 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73  c.** mutex types
47450 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78  , the same mutex
47460 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20   is returned on 
47470 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20  every call that 
47480 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20  has.** the same 
47490 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a  type number..**.
474a0 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f  ** ^The sqlite3_
474b0 6d 75 74 65 78 5f 66 72 65 65 28 29 20 72 6f 75  mutex_free() rou
474c0 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73  tine deallocates
474d0 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a   a previously.**
474e0 20 61 6c 6c 6f 63 61 74 65 64 20 64 79 6e 61 6d   allocated dynam
474f0 69 63 20 6d 75 74 65 78 2e 20 20 5e 53 51 4c 69  ic mutex.  ^SQLi
47500 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f  te is careful to
47510 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72   deallocate ever
47520 79 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 75 74  y.** dynamic mut
47530 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63  ex that it alloc
47540 61 74 65 73 2e 20 20 54 68 65 20 64 79 6e 61 6d  ates.  The dynam
47550 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73 74 20  ic mutexes must 
47560 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75 73 65  not be in.** use
47570 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 64   when they are d
47580 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 41 74 74  eallocated.  Att
47590 65 6d 70 74 69 6e 67 20 74 6f 20 64 65 61 6c 6c  empting to deall
475a0 6f 63 61 74 65 20 61 20 73 74 61 74 69 63 0a 2a  ocate a static.*
475b0 2a 20 6d 75 74 65 78 20 72 65 73 75 6c 74 73 20  * mutex results 
475c0 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68  in undefined beh
475d0 61 76 69 6f 72 2e 20 20 5e 53 51 4c 69 74 65 20  avior.  ^SQLite 
475e0 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74 65  never deallocate
475f0 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d 75  s.** a static mu
47600 74 65 78 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  tex..**.** ^The 
47610 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
47620 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ter() and sqlite
47630 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f  3_mutex_try() ro
47640 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a  utines attempt.*
47650 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74  * to enter a mut
47660 65 78 2e 20 20 5e 49 66 20 61 6e 6f 74 68 65 72  ex.  ^If another
47670 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61   thread is alrea
47680 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75  dy within the mu
47690 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  tex,.** sqlite3_
476a0 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69  mutex_enter() wi
476b0 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c  ll block and sql
476c0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
476d0 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20   will return.** 
476e0 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 5e 54  SQLITE_BUSY.  ^T
476f0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
47700 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65  _try() interface
47710 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
47720 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f 6e 20 73 75 63  _OK].** upon suc
47730 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20  cessful entry.  
47740 5e 28 4d 75 74 65 78 65 73 20 63 72 65 61 74 65  ^(Mutexes create
47750 64 20 75 73 69 6e 67 0a 2a 2a 20 53 51 4c 49 54  d using.** SQLIT
47760 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
47770 45 20 63 61 6e 20 62 65 20 65 6e 74 65 72 65 64  E can be entered
47780 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20   multiple times 
47790 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
477a0 61 64 2e 0a 2a 2a 20 49 6e 20 73 75 63 68 20 63  ad..** In such c
477b0 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74  ases the,.** mut
477c0 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65  ex must be exite
477d0 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65  d an equal numbe
477e0 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72  r of times befor
477f0 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  e another thread
47800 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 29 5e  .** can enter.)^
47810 20 20 5e 28 49 66 20 74 68 65 20 73 61 6d 65 20    ^(If the same 
47820 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20  thread tries to 
47830 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 0a  enter any other.
47840 2a 2a 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78  ** kind of mutex
47850 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c   more than once,
47860 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73   the behavior is
47870 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 53   undefined..** S
47880 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72  QLite will never
47890 20 65 78 68 69 62 69 74 0a 2a 2a 20 73 75 63 68   exhibit.** such
478a0 20 62 65 68 61 76 69 6f 72 20 69 6e 20 69 74 73   behavior in its
478b0 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d 75 74 65   own use of mute
478c0 78 65 73 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 28 53  xes.)^.**.** ^(S
478d0 6f 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72  ome systems (for
478e0 20 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77   example, Window
478f0 73 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70  s 95) do not sup
47900 70 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74 69  port the operati
47910 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65  on.** implemente
47920 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74  d by sqlite3_mut
47930 65 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68  ex_try().  On th
47940 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c  ose systems, sql
47950 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
47960 0a 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20  .** will always 
47970 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
47980 53 59 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20  SY.  The SQLite 
47990 63 6f 72 65 20 6f 6e 6c 79 20 65 76 65 72 20 75  core only ever u
479a0 73 65 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  ses.** sqlite3_m
479b0 75 74 65 78 5f 74 72 79 28 29 20 61 73 20 61 6e  utex_try() as an
479c0 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 73 6f   optimization so
479d0 20 74 68 69 73 20 69 73 20 61 63 63 65 70 74 61   this is accepta
479e0 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 29 5e 0a  ble behavior.)^.
479f0 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74  **.** ^The sqlit
47a00 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
47a10 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61   routine exits a
47a20 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a   mutex that was.
47a30 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e  ** previously en
47a40 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d  tered by the sam
47a50 65 20 74 68 72 65 61 64 2e 20 20 20 5e 28 54 68  e thread.   ^(Th
47a60 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73  e behavior.** is
47a70 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68   undefined if th
47a80 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63  e mutex is not c
47a90 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64  urrently entered
47aa0 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69   by the.** calli
47ab0 6e 67 20 74 68 72 65 61 64 20 6f 72 20 69 73 20  ng thread or is 
47ac0 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c  not currently al
47ad0 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65  located.  SQLite
47ae0 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64   will.** never d
47af0 6f 20 65 69 74 68 65 72 2e 29 5e 0a 2a 2a 0a 2a  o either.)^.**.*
47b00 2a 20 5e 49 66 20 74 68 65 20 61 72 67 75 6d 65  * ^If the argume
47b10 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  nt to sqlite3_mu
47b20 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c  tex_enter(), sql
47b30 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
47b40 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  , or.** sqlite3_
47b50 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73  mutex_leave() is
47b60 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
47b70 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20   then all three 
47b80 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61  routines.** beha
47b90 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a  ve as no-ops..**
47ba0 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
47bb0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
47bc0 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  d()] and [sqlite
47bd0 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
47be0 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  )]..*/.SQLITE_AP
47bf0 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  I sqlite3_mutex 
47c00 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  *sqlite3_mutex_a
47c10 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54  lloc(int);.SQLIT
47c20 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
47c30 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71  e3_mutex_free(sq
47c40 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53  lite3_mutex*);.S
47c50 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
47c60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
47c70 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  er(sqlite3_mutex
47c80 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
47c90 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nt sqlite3_mutex
47ca0 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74  _try(sqlite3_mut
47cb0 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ex*);.SQLITE_API
47cc0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75   void sqlite3_mu
47cd0 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
47ce0 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a  3_mutex*);../*.*
47cf0 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65  * CAPI3REF: Mute
47d00 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74  x Methods Object
47d10 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  .**.** An instan
47d20 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
47d30 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 65  ture defines the
47d40 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69   low-level routi
47d50 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 61  nes.** used to a
47d60 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73 65 20  llocate and use 
47d70 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 55  mutexes..**.** U
47d80 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65 66 61  sually, the defa
47d90 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d  ult mutex implem
47da0 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76 69 64  entations provid
47db0 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 72 65  ed by SQLite are
47dc0 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 2c 20  .** sufficient, 
47dd0 68 6f 77 65 76 65 72 20 74 68 65 20 75 73 65 72  however the user
47de0 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f 6e 20   has the option 
47df0 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e 67 20  of substituting 
47e00 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d 70 6c  a custom.** impl
47e10 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 73  ementation for s
47e20 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70 6c 6f  pecialized deplo
47e30 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74 65 6d  yments or system
47e40 73 20 66 6f 72 20 77 68 69 63 68 20 53 51 4c 69  s for which SQLi
47e50 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 70  te.** does not p
47e60 72 6f 76 69 64 65 20 61 20 73 75 69 74 61 62 6c  rovide a suitabl
47e70 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
47e80 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20  . In this case, 
47e90 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72 65 61  the user.** crea
47ea0 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61 74 65  tes and populate
47eb0 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
47ec0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
47ed0 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20 73 71  to pass.** to sq
47ee0 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 61  lite3_config() a
47ef0 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 5b 53  long with the [S
47f00 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54  QLITE_CONFIG_MUT
47f10 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 41  EX] option..** A
47f20 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 6e 20  dditionally, an 
47f30 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
47f40 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 62   structure can b
47f50 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a 2a 20  e used as an.** 
47f60 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20  output variable 
47f70 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20 74 68  when querying th
47f80 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74 68 65  e system for the
47f90 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 0a 2a   current mutex.*
47fa0 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
47fb0 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53 51 4c  , using the [SQL
47fc0 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55  ITE_CONFIG_GETMU
47fd0 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 0a  TEX] option..**.
47fe0 2a 2a 20 5e 54 68 65 20 78 4d 75 74 65 78 49 6e  ** ^The xMutexIn
47ff0 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e 65  it method define
48000 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63 74  d by this struct
48010 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 61  ure is invoked a
48020 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 73  s.** part of sys
48030 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  tem initializati
48040 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  on by the sqlite
48050 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66  3_initialize() f
48060 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 5e 54 68 65  unction..** ^The
48070 20 78 4d 75 74 65 78 49 6e 69 74 20 72 6f 75 74   xMutexInit rout
48080 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ine is called by
48090 20 53 51 4c 69 74 65 20 65 78 61 63 74 6c 79 20   SQLite exactly 
480a0 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 0a 2a 2a  once for each.**
480b0 20 65 66 66 65 63 74 69 76 65 20 63 61 6c 6c 20   effective call 
480c0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74  to [sqlite3_init
480d0 69 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a  ialize()]..**.**
480e0 20 5e 54 68 65 20 78 4d 75 74 65 78 45 6e 64 20   ^The xMutexEnd 
480f0 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64 20 62  method defined b
48100 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  y this structure
48110 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 0a 2a   is invoked as.*
48120 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74 65 6d  * part of system
48130 20 73 68 75 74 64 6f 77 6e 20 62 79 20 74 68 65   shutdown by the
48140 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
48150 6e 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68  n() function. Th
48160 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  e.** implementat
48170 69 6f 6e 20 6f 66 20 74 68 69 73 20 6d 65 74 68  ion of this meth
48180 6f 64 20 69 73 20 65 78 70 65 63 74 65 64 20 74  od is expected t
48190 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 6f 75  o release all ou
481a0 74 73 74 61 6e 64 69 6e 67 0a 2a 2a 20 72 65 73  tstanding.** res
481b0 6f 75 72 63 65 73 20 6f 62 74 61 69 6e 65 64 20  ources obtained 
481c0 62 79 20 74 68 65 20 6d 75 74 65 78 20 6d 65 74  by the mutex met
481d0 68 6f 64 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  hods implementat
481e0 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c 79 0a  ion, especially.
481f0 2a 2a 20 74 68 6f 73 65 20 6f 62 74 61 69 6e 65  ** those obtaine
48200 64 20 62 79 20 74 68 65 20 78 4d 75 74 65 78 49  d by the xMutexI
48210 6e 69 74 20 6d 65 74 68 6f 64 2e 20 20 5e 54 68  nit method.  ^Th
48220 65 20 78 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a  e xMutexEnd().**
48230 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 69 6e   interface is in
48240 76 6f 6b 65 64 20 65 78 61 63 74 6c 79 20 6f 6e  voked exactly on
48250 63 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c 6c  ce for each call
48260 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 68 75   to [sqlite3_shu
48270 74 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  tdown()]..**.** 
48280 5e 28 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20  ^(The remaining 
48290 73 65 76 65 6e 20 6d 65 74 68 6f 64 73 20 64 65  seven methods de
482a0 66 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74  fined by this st
482b0 72 75 63 74 75 72 65 20 28 78 4d 75 74 65 78 41  ructure (xMutexA
482c0 6c 6c 6f 63 2c 0a 2a 2a 20 78 4d 75 74 65 78 46  lloc,.** xMutexF
482d0 72 65 65 2c 20 78 4d 75 74 65 78 45 6e 74 65 72  ree, xMutexEnter
482e0 2c 20 78 4d 75 74 65 78 54 72 79 2c 20 78 4d 75  , xMutexTry, xMu
482f0 74 65 78 4c 65 61 76 65 2c 20 78 4d 75 74 65 78  texLeave, xMutex
48300 48 65 6c 64 20 61 6e 64 0a 2a 2a 20 78 4d 75 74  Held and.** xMut
48310 65 78 4e 6f 74 68 65 6c 64 29 20 69 6d 70 6c 65  exNotheld) imple
48320 6d 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ment the followi
48330 6e 67 20 69 6e 74 65 72 66 61 63 65 73 20 28 72  ng interfaces (r
48340 65 73 70 65 63 74 69 76 65 6c 79 29 3a 0a 2a 2a  espectively):.**
48350 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c  .** <ul>.**   <l
48360 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  i>  [sqlite3_mut
48370 65 78 5f 61 6c 6c 6f 63 28 29 5d 20 3c 2f 6c 69  ex_alloc()] </li
48380 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71  >.**   <li>  [sq
48390 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65  lite3_mutex_free
483a0 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c  ()] </li>.**   <
483b0 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75  li>  [sqlite3_mu
483c0 74 65 78 5f 65 6e 74 65 72 28 29 5d 20 3c 2f 6c  tex_enter()] </l
483d0 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73  i>.**   <li>  [s
483e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
483f0 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c  ()] </li>.**   <
48400 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75  li>  [sqlite3_mu
48410 74 65 78 5f 6c 65 61 76 65 28 29 5d 20 3c 2f 6c  tex_leave()] </l
48420 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73  i>.**   <li>  [s
48430 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
48440 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20  d()] </li>.**   
48450 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d  <li>  [sqlite3_m
48460 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 20  utex_notheld()] 
48470 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 29 5e  </li>.** </ul>)^
48480 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20  .**.** The only 
48490 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 74 68  difference is th
484a0 61 74 20 74 68 65 20 70 75 62 6c 69 63 20 73 71  at the public sq
484b0 6c 69 74 65 33 5f 58 58 58 20 66 75 6e 63 74 69  lite3_XXX functi
484c0 6f 6e 73 20 65 6e 75 6d 65 72 61 74 65 64 0a 2a  ons enumerated.*
484d0 2a 20 61 62 6f 76 65 20 73 69 6c 65 6e 74 6c 79  * above silently
484e0 20 69 67 6e 6f 72 65 20 61 6e 79 20 69 6e 76 6f   ignore any invo
484f0 63 61 74 69 6f 6e 73 20 74 68 61 74 20 70 61 73  cations that pas
48500 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  s a NULL pointer
48510 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 61   instead.** of a
48520 20 76 61 6c 69 64 20 6d 75 74 65 78 20 68 61 6e   valid mutex han
48530 64 6c 65 2e 20 54 68 65 20 69 6d 70 6c 65 6d 65  dle. The impleme
48540 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  ntations of the 
48550 6d 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 0a  methods defined.
48560 2a 2a 20 62 79 20 74 68 69 73 20 73 74 72 75 63  ** by this struc
48570 74 75 72 65 20 61 72 65 20 6e 6f 74 20 72 65 71  ture are not req
48580 75 69 72 65 64 20 74 6f 20 68 61 6e 64 6c 65 20  uired to handle 
48590 74 68 69 73 20 63 61 73 65 2c 20 74 68 65 20 72  this case, the r
485a0 65 73 75 6c 74 73 0a 2a 2a 20 6f 66 20 70 61 73  esults.** of pas
485b0 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  sing a NULL poin
485c0 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61  ter instead of a
485d0 20 76 61 6c 69 64 20 6d 75 74 65 78 20 68 61 6e   valid mutex han
485e0 64 6c 65 20 61 72 65 20 75 6e 64 65 66 69 6e 65  dle are undefine
485f0 64 0a 2a 2a 20 28 69 2e 65 2e 20 69 74 20 69 73  d.** (i.e. it is
48600 20 61 63 63 65 70 74 61 62 6c 65 20 74 6f 20 70   acceptable to p
48610 72 6f 76 69 64 65 20 61 6e 20 69 6d 70 6c 65 6d  rovide an implem
48620 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 73 65  entation that se
48630 67 66 61 75 6c 74 73 20 69 66 0a 2a 2a 20 69 74  gfaults if.** it
48640 20 69 73 20 70 61 73 73 65 64 20 61 20 4e 55 4c   is passed a NUL
48650 4c 20 70 6f 69 6e 74 65 72 29 2e 0a 2a 2a 0a 2a  L pointer)..**.*
48660 2a 20 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74  * The xMutexInit
48670 28 29 20 6d 65 74 68 6f 64 20 6d 75 73 74 20 62  () method must b
48680 65 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 5e  e threadsafe.  ^
48690 49 74 20 6d 75 73 74 20 62 65 20 68 61 72 6d 6c  It must be harml
486a0 65 73 73 20 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65  ess to.** invoke
486b0 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 75   xMutexInit() mu
486c0 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74  ltiple times wit
486d0 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f  hin the same pro
486e0 63 65 73 73 20 61 6e 64 20 77 69 74 68 6f 75 74  cess and without
486f0 0a 2a 2a 20 69 6e 74 65 72 76 65 6e 69 6e 67 20  .** intervening 
48700 63 61 6c 6c 73 20 74 6f 20 78 4d 75 74 65 78 45  calls to xMutexE
48710 6e 64 28 29 2e 20 20 53 65 63 6f 6e 64 20 61 6e  nd().  Second an
48720 64 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  d subsequent cal
48730 6c 73 20 74 6f 0a 2a 2a 20 78 4d 75 74 65 78 49  ls to.** xMutexI
48740 6e 69 74 28 29 20 6d 75 73 74 20 62 65 20 6e 6f  nit() must be no
48750 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 5e 78 4d 75  -ops..**.** ^xMu
48760 74 65 78 49 6e 69 74 28 29 20 6d 75 73 74 20 6e  texInit() must n
48770 6f 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 65  ot use SQLite me
48780 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
48790 28 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  ([sqlite3_malloc
487a0 28 29 5d 0a 2a 2a 20 61 6e 64 20 69 74 73 20 61  ()].** and its a
487b0 73 73 6f 63 69 61 74 65 73 29 2e 20 20 5e 53 69  ssociates).  ^Si
487c0 6d 69 6c 61 72 6c 79 2c 20 78 4d 75 74 65 78 41  milarly, xMutexA
487d0 6c 6c 6f 63 28 29 20 6d 75 73 74 20 6e 6f 74 20  lloc() must not 
487e0 75 73 65 20 53 51 4c 69 74 65 20 6d 65 6d 6f 72  use SQLite memor
487f0 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  y.** allocation 
48800 66 6f 72 20 61 20 73 74 61 74 69 63 20 6d 75 74  for a static mut
48810 65 78 2e 20 20 5e 48 6f 77 65 76 65 72 20 78 4d  ex.  ^However xM
48820 75 74 65 78 41 6c 6c 6f 63 28 29 20 6d 61 79 20  utexAlloc() may 
48830 75 73 65 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 65  use SQLite.** me
48840 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
48850 66 6f 72 20 61 20 66 61 73 74 20 6f 72 20 72 65  for a fast or re
48860 63 75 72 73 69 76 65 20 6d 75 74 65 78 2e 0a 2a  cursive mutex..*
48870 2a 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 77 69 6c  *.** ^SQLite wil
48880 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 4d 75  l invoke the xMu
48890 74 65 78 45 6e 64 28 29 20 6d 65 74 68 6f 64 20  texEnd() method 
488a0 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68  when [sqlite3_sh
488b0 75 74 64 6f 77 6e 28 29 5d 20 69 73 0a 2a 2a 20  utdown()] is.** 
488c0 63 61 6c 6c 65 64 2c 20 62 75 74 20 6f 6e 6c 79  called, but only
488d0 20 69 66 20 74 68 65 20 70 72 69 6f 72 20 63 61   if the prior ca
488e0 6c 6c 20 74 6f 20 78 4d 75 74 65 78 49 6e 69 74  ll to xMutexInit
488f0 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49 54 45   returned SQLITE
48900 5f 4f 4b 2e 0a 2a 2a 20 49 66 20 78 4d 75 74 65  _OK..** If xMute
48910 78 49 6e 69 74 20 66 61 69 6c 73 20 69 6e 20 61  xInit fails in a
48920 6e 79 20 77 61 79 2c 20 69 74 20 69 73 20 65 78  ny way, it is ex
48930 70 65 63 74 65 64 20 74 6f 20 63 6c 65 61 6e 20  pected to clean 
48940 75 70 20 61 66 74 65 72 20 69 74 73 65 6c 66 0a  up after itself.
48950 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74 75  ** prior to retu
48960 72 6e 69 6e 67 2e 0a 2a 2f 0a 74 79 70 65 64 65  rning..*/.typede
48970 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
48980 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73  _mutex_methods s
48990 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
489a0 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c  hods;.struct sql
489b0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
489c0 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a 78 4d 75  ds {.  int (*xMu
489d0 74 65 78 49 6e 69 74 29 28 76 6f 69 64 29 3b 0a  texInit)(void);.
489e0 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 45 6e    int (*xMutexEn
489f0 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71 6c 69  d)(void);.  sqli
48a00 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 78 4d 75  te3_mutex *(*xMu
48a10 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a  texAlloc)(int);.
48a20 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 46    void (*xMutexF
48a30 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 74  ree)(sqlite3_mut
48a40 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a  ex *);.  void (*
48a50 78 4d 75 74 65 78 45 6e 74 65 72 29 28 73 71 6c  xMutexEnter)(sql
48a60 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20  ite3_mutex *);. 
48a70 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 54 72 79   int (*xMutexTry
48a80 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  )(sqlite3_mutex 
48a90 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75  *);.  void (*xMu
48aa0 74 65 78 4c 65 61 76 65 29 28 73 71 6c 69 74 65  texLeave)(sqlite
48ab0 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e  3_mutex *);.  in
48ac0 74 20 28 2a 78 4d 75 74 65 78 48 65 6c 64 29 28  t (*xMutexHeld)(
48ad0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29  sqlite3_mutex *)
48ae0 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78  ;.  int (*xMutex
48af0 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69 74 65 33  Notheld)(sqlite3
48b00 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 2f  _mutex *);.};../
48b10 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d  *.** CAPI3REF: M
48b20 75 74 65 78 20 56 65 72 69 66 69 63 61 74 69 6f  utex Verificatio
48b30 6e 20 52 6f 75 74 69 6e 65 73 0a 2a 2a 0a 2a 2a  n Routines.**.**
48b40 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
48b50 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71  ex_held() and sq
48b60 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
48b70 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 73 0a 2a  eld() routines.*
48b80 2a 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66  * are intended f
48b90 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 61 73  or use inside as
48ba0 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
48bb0 73 2e 20 20 5e 54 68 65 20 53 51 4c 69 74 65 20  s.  ^The SQLite 
48bc0 63 6f 72 65 0a 2a 2a 20 6e 65 76 65 72 20 75 73  core.** never us
48bd0 65 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  es these routine
48be0 73 20 65 78 63 65 70 74 20 69 6e 73 69 64 65 20  s except inside 
48bf0 61 6e 20 61 73 73 65 72 74 28 29 20 61 6e 64 20  an assert() and 
48c00 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20  applications.** 
48c10 61 72 65 20 61 64 76 69 73 65 64 20 74 6f 20 66  are advised to f
48c20 6f 6c 6c 6f 77 20 74 68 65 20 6c 65 61 64 20 6f  ollow the lead o
48c30 66 20 74 68 65 20 63 6f 72 65 2e 20 20 5e 54 68  f the core.  ^Th
48c40 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e  e SQLite core on
48c50 6c 79 0a 2a 2a 20 70 72 6f 76 69 64 65 73 20 69  ly.** provides i
48c60 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66  mplementations f
48c70 6f 72 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  or these routine
48c80 73 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d  s when it is com
48c90 70 69 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 68  piled.** with th
48ca0 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 66  e SQLITE_DEBUG f
48cb0 6c 61 67 2e 20 20 5e 45 78 74 65 72 6e 61 6c 20  lag.  ^External 
48cc0 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61  mutex implementa
48cd0 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c  tions.** are onl
48ce0 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 70 72  y required to pr
48cf0 6f 76 69 64 65 20 74 68 65 73 65 20 72 6f 75 74  ovide these rout
48d00 69 6e 65 73 20 69 66 20 53 51 4c 49 54 45 5f 44  ines if SQLITE_D
48d10 45 42 55 47 20 69 73 0a 2a 2a 20 64 65 66 69 6e  EBUG is.** defin
48d20 65 64 20 61 6e 64 20 69 66 20 4e 44 45 42 55 47  ed and if NDEBUG
48d30 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e   is not defined.
48d40 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 73 65 20 72 6f  .**.** ^These ro
48d50 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 72 65  utines should re
48d60 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
48d70 20 6d 75 74 65 78 20 69 6e 20 74 68 65 69 72 20   mutex in their 
48d80 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 68  argument.** is h
48d90 65 6c 64 20 6f 72 20 6e 6f 74 20 68 65 6c 64 2c  eld or not held,
48da0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2c 20 62   respectively, b
48db0 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68  y the calling th
48dc0 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65  read..**.** ^The
48dd0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
48de0 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20  is not required 
48df0 74 6f 20 70 72 6f 76 69 64 65 20 76 65 72 73 69  to provide versi
48e00 6f 6e 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20  ons of these.** 
48e10 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 63  routines that ac
48e20 74 75 61 6c 6c 79 20 77 6f 72 6b 2e 20 49 66 20  tually work. If 
48e30 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
48e40 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76  on does not prov
48e50 69 64 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76  ide working.** v
48e60 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 73 65  ersions of these
48e70 20 72 6f 75 74 69 6e 65 73 2c 20 69 74 20 73 68   routines, it sh
48e80 6f 75 6c 64 20 61 74 20 6c 65 61 73 74 20 70 72  ould at least pr
48e90 6f 76 69 64 65 20 73 74 75 62 73 20 74 68 61 74  ovide stubs that
48ea0 20 61 6c 77 61 79 73 0a 2a 2a 20 72 65 74 75 72   always.** retur
48eb0 6e 20 74 72 75 65 20 73 6f 20 74 68 61 74 20 6f  n true so that o
48ec0 6e 65 20 64 6f 65 73 20 6e 6f 74 20 67 65 74 20  ne does not get 
48ed0 73 70 75 72 69 6f 75 73 20 61 73 73 65 72 74 69  spurious asserti
48ee0 6f 6e 20 66 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a  on failures..**.
48ef0 2a 2a 20 5e 49 66 20 74 68 65 20 61 72 67 75 6d  ** ^If the argum
48f00 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  ent to sqlite3_m
48f10 75 74 65 78 5f 68 65 6c 64 28 29 20 69 73 20 61  utex_held() is a
48f20 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68   NULL pointer th
48f30 65 6e 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69 6e  en.** the routin
48f40 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20  e should return 
48f50 31 2e 20 20 20 54 68 69 73 20 73 65 65 6d 73 20  1.   This seems 
48f60 63 6f 75 6e 74 65 72 2d 69 6e 74 75 69 74 69 76  counter-intuitiv
48f70 65 20 73 69 6e 63 65 0a 2a 2a 20 63 6c 65 61 72  e since.** clear
48f80 6c 79 20 74 68 65 20 6d 75 74 65 78 20 63 61 6e  ly the mutex can
48f90 6e 6f 74 20 62 65 20 68 65 6c 64 20 69 66 20 69  not be held if i
48fa0 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  t does not exist
48fb0 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 20 72 65  .  But.** the re
48fc0 61 73 6f 6e 20 74 68 65 20 6d 75 74 65 78 20 64  ason the mutex d
48fd0 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 73  oes not exist is
48fe0 20 62 65 63 61 75 73 65 20 74 68 65 20 62 75 69   because the bui
48ff0 6c 64 20 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69  ld is not.** usi
49000 6e 67 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64  ng mutexes.  And
49010 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20   we do not want 
49020 74 68 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e  the assert() con
49030 74 61 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63  taining the.** c
49040 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  all to sqlite3_m
49050 75 74 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66  utex_held() to f
49060 61 69 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65  ail, so a non-ze
49070 72 6f 20 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20  ro return is.** 
49080 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
49090 74 68 69 6e 67 20 74 6f 20 64 6f 2e 20 20 5e 54  thing to do.  ^T
490a0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
490b0 5f 6e 6f 74 68 65 6c 64 28 29 0a 2a 2a 20 69 6e  _notheld().** in
490c0 74 65 72 66 61 63 65 20 73 68 6f 75 6c 64 20 61  terface should a
490d0 6c 73 6f 20 72 65 74 75 72 6e 20 31 20 77 68 65  lso return 1 whe
490e0 6e 20 67 69 76 65 6e 20 61 20 4e 55 4c 4c 20 70  n given a NULL p
490f0 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 23 69 66 6e 64  ointer..*/.#ifnd
49100 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45  ef NDEBUG.SQLITE
49110 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
49120 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69  _mutex_held(sqli
49130 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c  te3_mutex*);.SQL
49140 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
49150 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
49160 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a  d(sqlite3_mutex*
49170 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  );.#endif../*.**
49180 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78   CAPI3REF: Mutex
49190 20 54 79 70 65 73 0a 2a 2a 0a 2a 2a 20 54 68 65   Types.**.** The
491a0 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   [sqlite3_mutex_
491b0 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 65 72 66 61  alloc()] interfa
491c0 63 65 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c  ce takes a singl
491d0 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68  e argument.** wh
491e0 69 63 68 20 69 73 20 6f 6e 65 20 6f 66 20 74 68  ich is one of th
491f0 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73  ese integer cons
49200 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tants..**.** The
49210 20 73 65 74 20 6f 66 20 73 74 61 74 69 63 20 6d   set of static m
49220 75 74 65 78 65 73 20 6d 61 79 20 63 68 61 6e 67  utexes may chang
49230 65 20 66 72 6f 6d 20 6f 6e 65 20 53 51 4c 69 74  e from one SQLit
49240 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74 68 65  e release to the
49250 0a 2a 2a 20 6e 65 78 74 2e 20 20 41 70 70 6c 69  .** next.  Appli
49260 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 65  cations that ove
49270 72 72 69 64 65 20 74 68 65 20 62 75 69 6c 74 2d  rride the built-
49280 69 6e 20 6d 75 74 65 78 20 6c 6f 67 69 63 20 6d  in mutex logic m
49290 75 73 74 20 62 65 0a 2a 2a 20 70 72 65 70 61 72  ust be.** prepar
492a0 65 64 20 74 6f 20 61 63 63 6f 6d 6d 6f 64 61 74  ed to accommodat
492b0 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61  e additional sta
492c0 74 69 63 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a  tic mutexes..*/.
492d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
492e0 55 54 45 58 5f 46 41 53 54 20 20 20 20 20 20 20  UTEX_FAST       
492f0 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20        0.#define 
49300 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
49310 55 52 53 49 56 45 20 20 20 20 20 20 20 20 31 0a  URSIVE        1.
49320 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
49330 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
49340 45 52 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20  ER    2.#define 
49350 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
49360 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 33 20  TIC_MEM       3 
49370 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   /* sqlite3_mall
49380 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  oc() */.#define 
49390 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
493a0 54 49 43 5f 4d 45 4d 32 20 20 20 20 20 20 34 20  TIC_MEM2      4 
493b0 20 2f 2a 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a   /* NOT USED */.
493c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
493d0 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e  UTEX_STATIC_OPEN
493e0 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69        4  /* sqli
493f0 74 65 33 42 74 72 65 65 4f 70 65 6e 28 29 20 2a  te3BtreeOpen() *
49400 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
49410 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52  _MUTEX_STATIC_PR
49420 4e 47 20 20 20 20 20 20 35 20 20 2f 2a 20 73 71  NG      5  /* sq
49430 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 28 29 20 2a  lite3_random() *
49440 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
49450 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
49460 55 20 20 20 20 20 20 20 36 20 20 2f 2a 20 6c 72  U       6  /* lr
49470 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 23  u page list */.#
49480 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55  define SQLITE_MU
49490 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20  TEX_STATIC_LRU2 
494a0 20 20 20 20 20 37 20 20 2f 2a 20 4e 4f 54 20 55       7  /* NOT U
494b0 53 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  SED */.#define S
494c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
494d0 49 43 5f 50 4d 45 4d 20 20 20 20 20 20 37 20 20  IC_PMEM      7  
494e0 2f 2a 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61  /* sqlite3PageMa
494f0 6c 6c 6f 63 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  lloc() */../*.**
49500 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 72 69   CAPI3REF: Retri
49510 65 76 65 20 74 68 65 20 6d 75 74 65 78 20 66 6f  eve the mutex fo
49520 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  r a database con
49530 6e 65 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 5e 54  nection.**.** ^T
49540 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 72 65  his interface re
49550 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
49560 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  the [sqlite3_mut
49570 65 78 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20  ex] object that 
49580 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 65 73 20 61  .** serializes a
49590 63 63 65 73 73 20 74 6f 20 74 68 65 20 5b 64 61  ccess to the [da
495a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
495b0 6e 5d 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20  n] given in the 
495c0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 65 6e  argument.** when
495d0 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20   the [threading 
495e0 6d 6f 64 65 5d 20 69 73 20 53 65 72 69 61 6c 69  mode] is Seriali
495f0 7a 65 64 2e 0a 2a 2a 20 5e 49 66 20 74 68 65 20  zed..** ^If the 
49600 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d  [threading mode]
49610 20 69 73 20 53 69 6e 67 6c 65 2d 74 68 72 65 61   is Single-threa
49620 64 20 6f 72 20 4d 75 6c 74 69 2d 74 68 72 65 61  d or Multi-threa
49630 64 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72  d then this.** r
49640 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61  outine returns a
49650 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a   NULL pointer..*
49660 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
49670 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69  ite3_mutex *sqli
49680 74 65 33 5f 64 62 5f 6d 75 74 65 78 28 73 71 6c  te3_db_mutex(sql
49690 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ite3*);../*.** C
496a0 41 50 49 33 52 45 46 3a 20 4c 6f 77 2d 4c 65 76  API3REF: Low-Lev
496b0 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f 66 20 44 61  el Control Of Da
496c0 74 61 62 61 73 65 20 46 69 6c 65 73 0a 2a 2a 0a  tabase Files.**.
496d0 2a 2a 20 5e 54 68 65 20 5b 73 71 6c 69 74 65 33  ** ^The [sqlite3
496e0 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d  _file_control()]
496f0 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73   interface makes
49700 20 61 20 64 69 72 65 63 74 20 63 61 6c 6c 20 74   a direct call t
49710 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 65 43 6f  o the.** xFileCo
49720 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 66 6f 72  ntrol method for
49730 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f   the [sqlite3_io
49740 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74  _methods] object
49750 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77   associated.** w
49760 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72  ith a particular
49770 20 64 61 74 61 62 61 73 65 20 69 64 65 6e 74 69   database identi
49780 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f  fied by the seco
49790 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20 5e 54 68  nd argument. ^Th
497a0 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65  e.** name of the
497b0 20 64 61 74 61 62 61 73 65 20 69 73 20 22 6d 61   database is "ma
497c0 69 6e 22 20 66 6f 72 20 74 68 65 20 6d 61 69 6e  in" for the main
497d0 20 64 61 74 61 62 61 73 65 20 6f 72 20 22 74 65   database or "te
497e0 6d 70 22 20 66 6f 72 20 74 68 65 0a 2a 2a 20 54  mp" for the.** T
497f0 45 4d 50 20 64 61 74 61 62 61 73 65 2c 20 6f 72  EMP database, or
49800 20 74 68 65 20 6e 61 6d 65 20 74 68 61 74 20 61   the name that a
49810 70 70 65 61 72 73 20 61 66 74 65 72 20 74 68 65  ppears after the
49820 20 41 53 20 6b 65 79 77 6f 72 64 20 66 6f 72 0a   AS keyword for.
49830 2a 2a 20 64 61 74 61 62 61 73 65 73 20 74 68 61  ** databases tha
49840 74 20 61 72 65 20 61 64 64 65 64 20 75 73 69 6e  t are added usin
49850 67 20 74 68 65 20 5b 41 54 54 41 43 48 5d 20 53  g the [ATTACH] S
49860 51 4c 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 20 5e  QL command..** ^
49870 41 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 63  A NULL pointer c
49880 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c  an be used in pl
49890 61 63 65 20 6f 66 20 22 6d 61 69 6e 22 20 74 6f  ace of "main" to
498a0 20 72 65 66 65 72 20 74 6f 20 74 68 65 0a 2a 2a   refer to the.**
498b0 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
498c0 69 6c 65 2e 0a 2a 2a 20 5e 54 68 65 20 74 68 69  ile..** ^The thi
498d0 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61  rd and fourth pa
498e0 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73  rameters to this
498f0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 72 65 20   routine.** are 
49900 70 61 73 73 65 64 20 64 69 72 65 63 74 6c 79 20  passed directly 
49910 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73  through to the s
49920 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20  econd and third 
49930 70 61 72 61 6d 65 74 65 72 73 20 6f 66 0a 2a 2a  parameters of.**
49940 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f   the xFileContro
49950 6c 20 6d 65 74 68 6f 64 2e 20 20 5e 54 68 65 20  l method.  ^The 
49960 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
49970 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c  the xFileControl
49980 0a 2a 2a 20 6d 65 74 68 6f 64 20 62 65 63 6f 6d  .** method becom
49990 65 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  es the return va
499a0 6c 75 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74  lue of this rout
499b0 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  ine..**.** ^The 
499c0 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 46 49 4c  SQLITE_FCNTL_FIL
499d0 45 5f 50 4f 49 4e 54 45 52 20 76 61 6c 75 65 20  E_POINTER value 
499e0 66 6f 72 20 74 68 65 20 6f 70 20 70 61 72 61 6d  for the op param
499f0 65 74 65 72 20 63 61 75 73 65 73 0a 2a 2a 20 61  eter causes.** a
49a00 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
49a10 75 6e 64 65 72 6c 79 69 6e 67 20 5b 73 71 6c 69  underlying [sqli
49a20 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74  te3_file] object
49a30 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69   to be written i
49a40 6e 74 6f 0a 2a 2a 20 74 68 65 20 73 70 61 63 65  nto.** the space
49a50 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74   pointed to by t
49a60 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72  he 4th parameter
49a70 2e 20 20 5e 54 68 65 20 53 51 4c 49 54 45 5f 46  .  ^The SQLITE_F
49a80 43 4e 54 4c 5f 46 49 4c 45 5f 50 4f 49 4e 54 45  CNTL_FILE_POINTE
49a90 52 0a 2a 2a 20 63 61 73 65 20 69 73 20 61 20 73  R.** case is a s
49aa0 68 6f 72 74 2d 63 69 72 63 75 69 74 20 70 61 74  hort-circuit pat
49ab0 68 20 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74  h which does not
49ac0 20 61 63 74 75 61 6c 6c 79 20 69 6e 76 6f 6b 65   actually invoke
49ad0 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69   the.** underlyi
49ae0 6e 67 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  ng sqlite3_io_me
49af0 74 68 6f 64 73 2e 78 46 69 6c 65 43 6f 6e 74 72  thods.xFileContr
49b00 6f 6c 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a  ol method..**.**
49b10 20 5e 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20   ^If the second 
49b20 70 61 72 61 6d 65 74 65 72 20 28 7a 44 62 4e 61  parameter (zDbNa
49b30 6d 65 29 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74  me) does not mat
49b40 63 68 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  ch the name of a
49b50 6e 79 0a 2a 2a 20 6f 70 65 6e 20 64 61 74 61 62  ny.** open datab
49b60 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53  ase file, then S
49b70 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72  QLITE_ERROR is r
49b80 65 74 75 72 6e 65 64 2e 20 20 5e 54 68 69 73 20  eturned.  ^This 
49b90 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73  error.** code is
49ba0 20 6e 6f 74 20 72 65 6d 65 6d 62 65 72 65 64 20   not remembered 
49bb0 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  and will not be 
49bc0 72 65 63 61 6c 6c 65 64 20 62 79 20 5b 73 71 6c  recalled by [sql
49bd0 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 0a  ite3_errcode()].
49be0 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65  ** or [sqlite3_e
49bf0 72 72 6d 73 67 28 29 5d 2e 20 20 54 68 65 20 75  rrmsg()].  The u
49c00 6e 64 65 72 6c 79 69 6e 67 20 78 46 69 6c 65 43  nderlying xFileC
49c10 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 6d 69  ontrol method mi
49c20 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 72 65 74 75  ght.** also retu
49c30 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e  rn SQLITE_ERROR.
49c40 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 77 61    There is no wa
49c50 79 20 74 6f 20 64 69 73 74 69 6e 67 75 69 73 68  y to distinguish
49c60 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 6e 20 69   between.** an i
49c70 6e 63 6f 72 72 65 63 74 20 7a 44 62 4e 61 6d 65  ncorrect zDbName
49c80 20 61 6e 64 20 61 6e 20 53 51 4c 49 54 45 5f 45   and an SQLITE_E
49c90 52 52 4f 52 20 72 65 74 75 72 6e 20 66 72 6f 6d  RROR return from
49ca0 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a   the underlying.
49cb0 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  ** xFileControl 
49cc0 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 53 65  method..**.** Se
49cd0 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f  e also: [SQLITE_
49ce0 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d  FCNTL_LOCKSTATE]
49cf0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
49d00 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  nt sqlite3_file_
49d10 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 2a  control(sqlite3*
49d20 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  , const char *zD
49d30 62 4e 61 6d 65 2c 20 69 6e 74 20 6f 70 2c 20 76  bName, int op, v
49d40 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  oid*);../*.** CA
49d50 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e 67 20  PI3REF: Testing 
49d60 49 6e 74 65 72 66 61 63 65 0a 2a 2a 0a 2a 2a 20  Interface.**.** 
49d70 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73  ^The sqlite3_tes
49d80 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65  t_control() inte
49d90 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f  rface is used to
49da0 20 72 65 61 64 20 6f 75 74 20 69 6e 74 65 72 6e   read out intern
49db0 61 6c 0a 2a 2a 20 73 74 61 74 65 20 6f 66 20 53  al.** state of S
49dc0 51 4c 69 74 65 20 61 6e 64 20 74 6f 20 69 6e 6a  QLite and to inj
49dd0 65 63 74 20 66 61 75 6c 74 73 20 69 6e 74 6f 20  ect faults into 
49de0 53 51 4c 69 74 65 20 66 6f 72 20 74 65 73 74 69  SQLite for testi
49df0 6e 67 0a 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20  ng.** purposes. 
49e00 20 5e 54 68 65 20 66 69 72 73 74 20 70 61 72 61   ^The first para
49e10 6d 65 74 65 72 20 69 73 20 61 6e 20 6f 70 65 72  meter is an oper
49e20 61 74 69 6f 6e 20 63 6f 64 65 20 74 68 61 74 20  ation code that 
49e30 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 74 68  determines.** th
49e40 65 20 6e 75 6d 62 65 72 2c 20 6d 65 61 6e 69 6e  e number, meanin
49e50 67 2c 20 61 6e 64 20 6f 70 65 72 61 74 69 6f 6e  g, and operation
49e60 20 6f 66 20 61 6c 6c 20 73 75 62 73 65 71 75 65   of all subseque
49e70 6e 74 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 2a  nt parameters..*
49e80 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
49e90 61 63 65 20 69 73 20 6e 6f 74 20 66 6f 72 20 75  ace is not for u
49ea0 73 65 20 62 79 20 61 70 70 6c 69 63 61 74 69 6f  se by applicatio
49eb0 6e 73 2e 20 20 49 74 20 65 78 69 73 74 73 20 73  ns.  It exists s
49ec0 6f 6c 65 6c 79 0a 2a 2a 20 66 6f 72 20 76 65 72  olely.** for ver
49ed0 69 66 79 69 6e 67 20 74 68 65 20 63 6f 72 72 65  ifying the corre
49ee0 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20  ct operation of 
49ef0 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
49f00 72 79 2e 20 20 44 65 70 65 6e 64 69 6e 67 0a 2a  ry.  Depending.*
49f10 2a 20 6f 6e 20 68 6f 77 20 74 68 65 20 53 51 4c  * on how the SQL
49f20 69 74 65 20 6c 69 62 72 61 72 79 20 69 73 20 63  ite library is c
49f30 6f 6d 70 69 6c 65 64 2c 20 74 68 69 73 20 69 6e  ompiled, this in
49f40 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 6e 6f  terface might no
49f50 74 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54  t exist..**.** T
49f60 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68  he details of th
49f70 65 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65  e operation code
49f80 73 2c 20 74 68 65 69 72 20 6d 65 61 6e 69 6e 67  s, their meaning
49f90 73 2c 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  s, the parameter
49fa0 73 0a 2a 2a 20 74 68 65 79 20 74 61 6b 65 2c 20  s.** they take, 
49fb0 61 6e 64 20 77 68 61 74 20 74 68 65 79 20 64 6f  and what they do
49fc0 20 61 72 65 20 61 6c 6c 20 73 75 62 6a 65 63 74   are all subject
49fd0 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f   to change witho
49fe0 75 74 20 6e 6f 74 69 63 65 2e 0a 2a 2a 20 55 6e  ut notice..** Un
49ff0 6c 69 6b 65 20 6d 6f 73 74 20 6f 66 20 74 68 65  like most of the
4a000 20 53 51 4c 69 74 65 20 41 50 49 2c 20 74 68 69   SQLite API, thi
4a010 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f  s function is no
4a020 74 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 0a  t guaranteed to.
4a030 2a 2a 20 6f 70 65 72 61 74 65 20 63 6f 6e 73 69  ** operate consi
4a040 73 74 65 6e 74 6c 79 20 66 72 6f 6d 20 6f 6e 65  stently from one
4a050 20 72 65 6c 65 61 73 65 20 74 6f 20 74 68 65 20   release to the 
4a060 6e 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  next..*/.SQLITE_
4a070 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
4a080 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74  test_control(int
4a090 20 6f 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a   op, ...);../*.*
4a0a0 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 73 74  * CAPI3REF: Test
4a0b0 69 6e 67 20 49 6e 74 65 72 66 61 63 65 20 4f 70  ing Interface Op
4a0c0 65 72 61 74 69 6f 6e 20 43 6f 64 65 73 0a 2a 2a  eration Codes.**
4a0d0 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61  .** These consta
4a0e0 6e 74 73 20 61 72 65 20 74 68 65 20 76 61 6c 69  nts are the vali
4a0f0 64 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65  d operation code
4a100 20 70 61 72 61 6d 65 74 65 72 73 20 75 73 65 64   parameters used
4a110 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74  .** as the first
4a120 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71   argument to [sq
4a130 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72  lite3_test_contr
4a140 6f 6c 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ol()]..**.** The
4a150 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 6e  se parameters an
4a160 64 20 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73  d their meanings
4a170 20 61 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20   are subject to 
4a180 63 68 61 6e 67 65 0a 2a 2a 20 77 69 74 68 6f 75  change.** withou
4a190 74 20 6e 6f 74 69 63 65 2e 20 20 54 68 65 73 65  t notice.  These
4a1a0 20 76 61 6c 75 65 73 20 61 72 65 20 66 6f 72 20   values are for 
4a1b0 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73  testing purposes
4a1c0 20 6f 6e 6c 79 2e 0a 2a 2a 20 41 70 70 6c 69 63   only..** Applic
4a1d0 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f  ations should no
4a1e0 74 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65  t use any of the
4a1f0 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72  se parameters or
4a200 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
4a210 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d  _test_control()]
4a220 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23   interface..*/.#
4a230 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
4a240 53 54 43 54 52 4c 5f 46 49 52 53 54 20 20 20 20  STCTRL_FIRST    
4a250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a260 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  5.#define SQLITE
4a270 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 53  _TESTCTRL_PRNG_S
4a280 41 56 45 20 20 20 20 20 20 20 20 20 20 20 20 20  AVE             
4a290 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c     5.#define SQL
4a2a0 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e  ITE_TESTCTRL_PRN
4a2b0 47 5f 52 45 53 54 4f 52 45 20 20 20 20 20 20 20  G_RESTORE       
4a2c0 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20        6.#define 
4a2d0 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
4a2e0 50 52 4e 47 5f 52 45 53 45 54 20 20 20 20 20 20  PRNG_RESET      
4a2f0 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69           7.#defi
4a300 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
4a310 52 4c 5f 42 49 54 56 45 43 5f 54 45 53 54 20 20  RL_BITVEC_TEST  
4a320 20 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 64              8.#d
4a330 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53  efine SQLITE_TES
4a340 54 43 54 52 4c 5f 46 41 55 4c 54 5f 49 4e 53 54  TCTRL_FAULT_INST
4a350 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 39  ALL            9
4a360 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
4a370 54 45 53 54 43 54 52 4c 5f 42 45 4e 49 47 4e 5f  TESTCTRL_BENIGN_
4a380 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 20 20 20 20  MALLOC_HOOKS    
4a390 20 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   10.#define SQLI
4a3a0 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 4e 44  TE_TESTCTRL_PEND
4a3b0 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 20 20  ING_BYTE        
4a3c0 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65 20 53      11.#define S
4a3d0 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41  QLITE_TESTCTRL_A
4a3e0 53 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20  SSERT           
4a3f0 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e         12.#defin
4a400 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
4a410 4c 5f 41 4c 57 41 59 53 20 20 20 20 20 20 20 20  L_ALWAYS        
4a420 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65            13.#de
4a430 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54  fine SQLITE_TEST
4a440 43 54 52 4c 5f 52 45 53 45 52 56 45 20 20 20 20  CTRL_RESERVE    
4a450 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 0a               14.
4a460 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54  #define SQLITE_T
4a470 45 53 54 43 54 52 4c 5f 4f 50 54 49 4d 49 5a 41  ESTCTRL_OPTIMIZA
4a480 54 49 4f 4e 53 20 20 20 20 20 20 20 20 20 20 20  TIONS           
4a490 31 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  15.#define SQLIT
4a4a0 45 5f 54 45 53 54 43 54 52 4c 5f 49 53 4b 45 59  E_TESTCTRL_ISKEY
4a4b0 57 4f 52 44 20 20 20 20 20 20 20 20 20 20 20 20  WORD            
4a4c0 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 53 51     16.#define SQ
4a4d0 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 53 43  LITE_TESTCTRL_SC
4a4e0 52 41 54 43 48 4d 41 4c 4c 4f 43 20 20 20 20 20  RATCHMALLOC     
4a4f0 20 20 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65        17.#define
4a500 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
4a510 5f 4c 4f 43 41 4c 54 49 4d 45 5f 46 41 55 4c 54  _LOCALTIME_FAULT
4a520 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65 66           18.#def
4a530 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ine SQLITE_TESTC
4a540 54 52 4c 5f 45 58 50 4c 41 49 4e 5f 53 54 4d 54  TRL_EXPLAIN_STMT
4a550 20 20 20 20 20 20 20 20 20 20 20 20 31 39 0a 23              19.#
4a560 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
4a570 53 54 43 54 52 4c 5f 4c 41 53 54 20 20 20 20 20  STCTRL_LAST     
4a580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4a590 39 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  9../*.** CAPI3RE
4a5a0 46 3a 20 53 51 4c 69 74 65 20 52 75 6e 74 69 6d  F: SQLite Runtim
4a5b0 65 20 53 74 61 74 75 73 0a 2a 2a 0a 2a 2a 20 5e  e Status.**.** ^
4a5c0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69  This interface i
4a5d0 73 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65  s used to retrie
4a5e0 76 65 20 72 75 6e 74 69 6d 65 20 73 74 61 74 75  ve runtime statu
4a5f0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a  s information.**
4a600 20 61 62 6f 75 74 20 74 68 65 20 70 65 72 66 6f   about the perfo
4a610 72 6d 61 6e 63 65 20 6f 66 20 53 51 4c 69 74 65  rmance of SQLite
4a620 2c 20 61 6e 64 20 6f 70 74 69 6f 6e 61 6c 6c 79  , and optionally
4a630 20 74 6f 20 72 65 73 65 74 20 76 61 72 69 6f 75   to reset variou
4a640 73 0a 2a 2a 20 68 69 67 68 77 61 74 65 72 20 6d  s.** highwater m
4a650 61 72 6b 73 2e 20 20 5e 54 68 65 20 66 69 72 73  arks.  ^The firs
4a660 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e  t argument is an
4a670 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f   integer code fo
4a680 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69  r.** the specifi
4a690 63 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 6d  c parameter to m
4a6a0 65 61 73 75 72 65 2e 20 20 5e 28 52 65 63 6f 67  easure.  ^(Recog
4a6b0 6e 69 7a 65 64 20 69 6e 74 65 67 65 72 20 63 6f  nized integer co
4a6c0 64 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20 74 68  des.** are of th
4a6d0 65 20 66 6f 72 6d 20 5b 73 74 61 74 75 73 20 70  e form [status p
4a6e0 61 72 61 6d 65 74 65 72 73 20 7c 20 53 51 4c 49  arameters | SQLI
4a6f0 54 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e 29  TE_STATUS_...].)
4a700 5e 0a 2a 2a 20 5e 54 68 65 20 63 75 72 72 65 6e  ^.** ^The curren
4a710 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70  t value of the p
4a720 61 72 61 6d 65 74 65 72 20 69 73 20 72 65 74 75  arameter is retu
4a730 72 6e 65 64 20 69 6e 74 6f 20 2a 70 43 75 72 72  rned into *pCurr
4a740 65 6e 74 2e 0a 2a 2a 20 5e 54 68 65 20 68 69 67  ent..** ^The hig
4a750 68 65 73 74 20 72 65 63 6f 72 64 65 64 20 76 61  hest recorded va
4a760 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  lue is returned 
4a770 69 6e 20 2a 70 48 69 67 68 77 61 74 65 72 2e 20  in *pHighwater. 
4a780 20 5e 49 66 20 74 68 65 0a 2a 2a 20 72 65 73 65   ^If the.** rese
4a790 74 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74  tFlag is true, t
4a7a0 68 65 6e 20 74 68 65 20 68 69 67 68 65 73 74 20  hen the highest 
4a7b0 72 65 63 6f 72 64 20 76 61 6c 75 65 20 69 73 20  record value is 
4a7c0 72 65 73 65 74 20 61 66 74 65 72 0a 2a 2a 20 2a  reset after.** *
4a7d0 70 48 69 67 68 77 61 74 65 72 20 69 73 20 77 72  pHighwater is wr
4a7e0 69 74 74 65 6e 2e 20 20 5e 28 53 6f 6d 65 20 70  itten.  ^(Some p
4a7f0 61 72 61 6d 65 74 65 72 73 20 64 6f 20 6e 6f 74  arameters do not
4a800 20 72 65 63 6f 72 64 20 74 68 65 20 68 69 67 68   record the high
4a810 65 73 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46  est.** value.  F
4a820 6f 72 20 74 68 6f 73 65 20 70 61 72 61 6d 65 74  or those paramet
4a830 65 72 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 69  ers.** nothing i
4a840 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a  s written into *
4a850 70 48 69 67 68 77 61 74 65 72 20 61 6e 64 20 74  pHighwater and t
4a860 68 65 20 72 65 73 65 74 46 6c 61 67 20 69 73 20  he resetFlag is 
4a870 69 67 6e 6f 72 65 64 2e 29 5e 0a 2a 2a 20 5e 28  ignored.)^.** ^(
4a880 4f 74 68 65 72 20 70 61 72 61 6d 65 74 65 72 73  Other parameters
4a890 20 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 68 65   record only the
4a8a0 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20   highwater mark 
4a8b0 61 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 72 72  and not the curr
4a8c0 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46  ent.** value.  F
4a8d0 6f 72 20 74 68 65 73 65 20 6c 61 74 74 65 72 20  or these latter 
4a8e0 70 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 68 69  parameters nothi
4a8f0 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  ng is written in
4a900 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 29 5e 0a  to *pCurrent.)^.
4a910 2a 2a 0a 2a 2a 20 5e 54 68 65 20 73 71 6c 69 74  **.** ^The sqlit
4a920 65 33 5f 73 74 61 74 75 73 28 29 20 72 6f 75 74  e3_status() rout
4a930 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ine returns SQLI
4a940 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
4a950 20 61 6e 64 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65   and a.** non-ze
4a960 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  ro [error code] 
4a970 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a  on failure..**.*
4a980 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
4a990 73 20 74 68 72 65 61 64 73 61 66 65 20 62 75 74  s threadsafe but
4a9a0 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2e 20   is not atomic. 
4a9b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
4a9c0 6e 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 77  n be.** called w
4a9d0 68 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 61  hile other threa
4a9e0 64 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 74  ds are running t
4a9f0 68 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 65  he same or diffe
4aa00 72 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20 69  rent SQLite.** i
4aa10 6e 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65  nterfaces.  Howe
4aa20 76 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 72  ver the values r
4aa30 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 72  eturned in *pCur
4aa40 72 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48 69  rent and.** *pHi
4aa50 67 68 77 61 74 65 72 20 72 65 66 6c 65 63 74 20  ghwater reflect 
4aa60 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 53 51  the status of SQ
4aa70 4c 69 74 65 20 61 74 20 64 69 66 66 65 72 65 6e  Lite at differen
4aa80 74 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d 65  t points in time
4aa90 0a 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70 6f  .** and it is po
4aaa0 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74  ssible that anot
4aab0 68 65 72 20 74 68 72 65 61 64 20 6d 69 67 68 74  her thread might
4aac0 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 72 61   change the para
4aad0 6d 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74 77  meter.** in betw
4aae0 65 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77 68  een the times wh
4aaf0 65 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64  en *pCurrent and
4ab00 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 72 65   *pHighwater are
4ab10 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20   written..**.** 
4ab20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74  See also: [sqlit
4ab30 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 0a  e3_db_status()].
4ab40 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
4ab50 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73  t sqlite3_status
4ab60 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43  (int op, int *pC
4ab70 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69  urrent, int *pHi
4ab80 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73  ghwater, int res
4ab90 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f 2a 0a 2a 2a  etFlag);.../*.**
4aba0 20 43 41 50 49 33 52 45 46 3a 20 53 74 61 74 75   CAPI3REF: Statu
4abb0 73 20 50 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  s Parameters.** 
4abc0 4b 45 59 57 4f 52 44 53 3a 20 7b 73 74 61 74 75  KEYWORDS: {statu
4abd0 73 20 70 61 72 61 6d 65 74 65 72 73 7d 0a 2a 2a  s parameters}.**
4abe0 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65  .** These intege
4abf0 72 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 73 69  r constants desi
4ac00 67 6e 61 74 65 20 76 61 72 69 6f 75 73 20 72 75  gnate various ru
4ac10 6e 2d 74 69 6d 65 20 73 74 61 74 75 73 20 70 61  n-time status pa
4ac20 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 61 74  rameters.** that
4ac30 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64   can be returned
4ac40 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 61   by [sqlite3_sta
4ac50 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64  tus()]..**.** <d
4ac60 6c 3e 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53  l>.** [[SQLITE_S
4ac70 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45  TATUS_MEMORY_USE
4ac80 44 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45  D]] ^(<dt>SQLITE
4ac90 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55  _STATUS_MEMORY_U
4aca0 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  SED</dt>.** <dd>
4acb0 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69  This parameter i
4acc0 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 61 6d  s the current am
4acd0 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63  ount of memory c
4ace0 68 65 63 6b 65 64 20 6f 75 74 0a 2a 2a 20 75 73  hecked out.** us
4acf0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  ing [sqlite3_mal
4ad00 6c 6f 63 28 29 5d 2c 20 65 69 74 68 65 72 20 64  loc()], either d
4ad10 69 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72  irectly or indir
4ad20 65 63 74 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 66  ectly.  The.** f
4ad30 69 67 75 72 65 20 69 6e 63 6c 75 64 65 73 20 63  igure includes c
4ad40 61 6c 6c 73 20 6d 61 64 65 20 74 6f 20 5b 73 71  alls made to [sq
4ad50 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20  lite3_malloc()] 
4ad60 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  by the applicati
4ad70 6f 6e 0a 2a 2a 20 61 6e 64 20 69 6e 74 65 72 6e  on.** and intern
4ad80 61 6c 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20  al memory usage 
4ad90 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  by the SQLite li
4ada0 62 72 61 72 79 2e 20 20 53 63 72 61 74 63 68 20  brary.  Scratch 
4adb0 6d 65 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 74 72 6f  memory.** contro
4adc0 6c 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f  lled by [SQLITE_
4add0 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20  CONFIG_SCRATCH] 
4ade0 61 6e 64 20 61 75 78 69 6c 69 61 72 79 20 70 61  and auxiliary pa
4adf0 67 65 2d 63 61 63 68 65 0a 2a 2a 20 6d 65 6d 6f  ge-cache.** memo
4ae00 72 79 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79  ry controlled by
4ae10 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
4ae20 50 41 47 45 43 41 43 48 45 5d 20 69 73 20 6e 6f  PAGECACHE] is no
4ae30 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 2a 2a  t included in.**
4ae40 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 2e   this parameter.
4ae50 20 20 54 68 65 20 61 6d 6f 75 6e 74 20 72 65 74    The amount ret
4ae60 75 72 6e 65 64 20 69 73 20 74 68 65 20 73 75 6d  urned is the sum
4ae70 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   of the allocati
4ae80 6f 6e 0a 2a 2a 20 73 69 7a 65 73 20 61 73 20 72  on.** sizes as r
4ae90 65 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 78  eported by the x
4aea0 53 69 7a 65 20 6d 65 74 68 6f 64 20 69 6e 20 5b  Size method in [
4aeb0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
4aec0 6f 64 73 5d 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a  ods].</dd>)^.**.
4aed0 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53 54 41 54  ** [[SQLITE_STAT
4aee0 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 5d 5d  US_MALLOC_SIZE]]
4aef0 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54   ^(<dt>SQLITE_ST
4af00 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45  ATUS_MALLOC_SIZE
4af10 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
4af20 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f  s parameter reco
4af30 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 20  rds the largest 
4af40 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
4af50 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e  n request.** han
4af60 64 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ded to [sqlite3_
4af70 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71  malloc()] or [sq
4af80 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d  lite3_realloc()]
4af90 20 28 6f 72 20 74 68 65 69 72 0a 2a 2a 20 69 6e   (or their.** in
4afa0 74 65 72 6e 61 6c 20 65 71 75 69 76 61 6c 65 6e  ternal equivalen
4afb0 74 73 29 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76  ts).  Only the v
4afc0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e  alue returned in
4afd0 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61   the.** *pHighwa
4afe0 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ter parameter to
4aff0 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73   [sqlite3_status
4b000 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65  ()] is of intere
4b010 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c  st.  .** The val
4b020 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  ue written into 
4b030 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61  the *pCurrent pa
4b040 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66  rameter is undef
4b050 69 6e 65 64 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a  ined.</dd>)^.**.
4b060 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53 54 41 54  ** [[SQLITE_STAT
4b070 55 53 5f 4d 41 4c 4c 4f 43 5f 43 4f 55 4e 54 5d  US_MALLOC_COUNT]
4b080 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 53  ] ^(<dt>SQLITE_S
4b090 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 43 4f 55  TATUS_MALLOC_COU
4b0a0 4e 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  NT</dt>.** <dd>T
4b0b0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
4b0c0 63 6f 72 64 73 20 74 68 65 20 6e 75 6d 62 65 72  cords the number
4b0d0 20 6f 66 20 73 65 70 61 72 61 74 65 20 6d 65 6d   of separate mem
4b0e0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a  ory allocations.
4b0f0 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65  ** currently che
4b100 63 6b 65 64 20 6f 75 74 2e 3c 2f 64 64 3e 29 5e  cked out.</dd>)^
4b110 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f  .**.** [[SQLITE_
4b120 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45  STATUS_PAGECACHE
4b130 5f 55 53 45 44 5d 5d 20 5e 28 3c 64 74 3e 53 51  _USED]] ^(<dt>SQ
4b140 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
4b150 43 41 43 48 45 5f 55 53 45 44 3c 2f 64 74 3e 0a  CACHE_USED</dt>.
4b160 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61  ** <dd>This para
4b170 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68  meter returns th
4b180 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
4b190 73 20 75 73 65 64 20 6f 75 74 20 6f 66 20 74 68  s used out of th
4b1a0 65 0a 2a 2a 20 5b 70 61 67 65 63 61 63 68 65 20  e.** [pagecache 
4b1b0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
4b1c0 5d 20 74 68 61 74 20 77 61 73 20 63 6f 6e 66 69  ] that was confi
4b1d0 67 75 72 65 64 20 75 73 69 6e 67 20 0a 2a 2a 20  gured using .** 
4b1e0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50  [SQLITE_CONFIG_P
4b1f0 41 47 45 43 41 43 48 45 5d 2e 20 20 54 68 65 0a  AGECACHE].  The.
4b200 2a 2a 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  ** value returne
4b210 64 20 69 73 20 69 6e 20 70 61 67 65 73 2c 20 6e  d is in pages, n
4b220 6f 74 20 69 6e 20 62 79 74 65 73 2e 3c 2f 64 64  ot in bytes.</dd
4b230 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49  >)^.**.** [[SQLI
4b240 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41  TE_STATUS_PAGECA
4b250 43 48 45 5f 4f 56 45 52 46 4c 4f 57 5d 5d 20 0a  CHE_OVERFLOW]] .
4b260 2a 2a 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f  ** ^(<dt>SQLITE_
4b270 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45  STATUS_PAGECACHE
4b280 5f 4f 56 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a  _OVERFLOW</dt>.*
4b290 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d  * <dd>This param
4b2a0 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65  eter returns the
4b2b0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
4b2c0 20 6f 66 20 70 61 67 65 20 63 61 63 68 65 0a 2a   of page cache.*
4b2d0 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69  * allocation whi
4b2e0 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ch could not be 
4b2f0 73 61 74 69 73 66 69 65 64 20 62 79 20 74 68 65  satisfied by the
4b300 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
4b310 50 41 47 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75  PAGECACHE].** bu
4b320 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66  ffer and where f
4b330 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f  orced to overflo
4b340 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61  w to [sqlite3_ma
4b350 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a  lloc()].  The.**
4b360 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20   returned value 
4b370 69 6e 63 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74  includes allocat
4b380 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 66 6c  ions that overfl
4b390 6f 77 65 64 20 62 65 63 61 75 73 65 20 74 68 65  owed because the
4b3a0 79 0a 2a 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c  y.** where too l
4b3b0 61 72 67 65 20 28 74 68 65 79 20 77 65 72 65 20  arge (they were 
4b3c0 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
4b3d0 22 73 7a 22 20 70 61 72 61 6d 65 74 65 72 20 74  "sz" parameter t
4b3e0 6f 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  o.** [SQLITE_CON
4b3f0 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 29 20  FIG_PAGECACHE]) 
4b400 61 6e 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  and allocations 
4b410 74 68 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20  that overflowed 
4b420 62 65 63 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70  because.** no sp
4b430 61 63 65 20 77 61 73 20 6c 65 66 74 20 69 6e 20  ace was left in 
4b440 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 3c  the page cache.<
4b450 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53  /dd>)^.**.** [[S
4b460 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47  QLITE_STATUS_PAG
4b470 45 43 41 43 48 45 5f 53 49 5a 45 5d 5d 20 5e 28  ECACHE_SIZE]] ^(
4b480 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55  <dt>SQLITE_STATU
4b490 53 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45  S_PAGECACHE_SIZE
4b4a0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
4b4b0 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f  s parameter reco
4b4c0 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 20  rds the largest 
4b4d0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
4b4e0 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e  n request.** han
4b4f0 64 65 64 20 74 6f 20 5b 70 61 67 65 63 61 63 68  ded to [pagecach
4b500 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
4b510 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76  or].  Only the v
4b520 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e  alue returned in
4b530 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61   the.** *pHighwa
4b540 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ter parameter to
4b550 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73   [sqlite3_status
4b560 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65  ()] is of intere
4b570 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c  st.  .** The val
4b580 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  ue written into 
4b590 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61  the *pCurrent pa
4b5a0 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66  rameter is undef
4b5b0 69 6e 65 64 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a  ined.</dd>)^.**.
4b5c0 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53 54 41 54  ** [[SQLITE_STAT
4b5d0 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 5d  US_SCRATCH_USED]
4b5e0 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 53  ] ^(<dt>SQLITE_S
4b5f0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53  TATUS_SCRATCH_US
4b600 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  ED</dt>.** <dd>T
4b610 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
4b620 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
4b630 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20   of allocations 
4b640 75 73 65 64 20 6f 75 74 20 6f 66 20 74 68 65 0a  used out of the.
4b650 2a 2a 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f  ** [scratch memo
4b660 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f  ry allocator] co
4b670 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 0a 2a  nfigured using.*
4b680 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  * [SQLITE_CONFIG
4b690 5f 53 43 52 41 54 43 48 5d 2e 20 20 54 68 65 20  _SCRATCH].  The 
4b6a0 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69  value returned i
4b6b0 73 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  s in allocations
4b6c0 2c 20 6e 6f 74 0a 2a 2a 20 69 6e 20 62 79 74 65  , not.** in byte
4b6d0 73 2e 20 20 53 69 6e 63 65 20 61 20 73 69 6e 67  s.  Since a sing
4b6e0 6c 65 20 74 68 72 65 61 64 20 6d 61 79 20 6f 6e  le thread may on
4b6f0 6c 79 20 68 61 76 65 20 6f 6e 65 20 73 63 72 61  ly have one scra
4b700 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  tch allocation.*
4b710 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74  * outstanding at
4b720 20 74 69 6d 65 2c 20 74 68 69 73 20 70 61 72 61   time, this para
4b730 6d 65 74 65 72 20 61 6c 73 6f 20 72 65 70 6f 72  meter also repor
4b740 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ts the number of
4b750 20 74 68 72 65 61 64 73 0a 2a 2a 20 75 73 69 6e   threads.** usin
4b760 67 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79  g scratch memory
4b770 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
4b780 65 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20  e.</dd>)^.**.** 
4b790 5b 5b 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  [[SQLITE_STATUS_
4b7a0 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57  SCRATCH_OVERFLOW
4b7b0 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f  ]] ^(<dt>SQLITE_
4b7c0 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f  STATUS_SCRATCH_O
4b7d0 56 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20  VERFLOW</dt>.** 
4b7e0 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74  <dd>This paramet
4b7f0 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  er returns the n
4b800 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
4b810 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79  f scratch memory
4b820 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77  .** allocation w
4b830 68 69 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62  hich could not b
4b840 65 20 73 61 74 69 73 66 69 65 64 20 62 79 20 74  e satisfied by t
4b850 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  he [SQLITE_CONFI
4b860 47 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62 75  G_SCRATCH].** bu
4b870 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66  ffer and where f
4b880 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f  orced to overflo
4b890 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61  w to [sqlite3_ma
4b8a0 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76 61  lloc()].  The va
4b8b0 6c 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  lues.** returned
4b8c0 20 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c 6f   include overflo
4b8d0 77 73 20 62 65 63 61 75 73 65 20 74 68 65 20 72  ws because the r
4b8e0 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74  equested allocat
4b8f0 69 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c  ion was too.** l
4b900 61 72 67 65 72 20 28 74 68 61 74 20 69 73 2c 20  arger (that is, 
4b910 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75  because the requ
4b920 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e  ested allocation
4b930 20 77 61 73 20 6c 61 72 67 65 72 20 74 68 61 6e   was larger than
4b940 20 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61 72   the.** "sz" par
4b950 61 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49 54  ameter to [SQLIT
4b960 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48  E_CONFIG_SCRATCH
4b970 5d 29 20 61 6e 64 20 62 65 63 61 75 73 65 20 6e  ]) and because n
4b980 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72  o scratch buffer
4b990 0a 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20 61  .** slots were a
4b9a0 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64  vailable..** </d
4b9b0 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c  d>)^.**.** [[SQL
4b9c0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54  ITE_STATUS_SCRAT
4b9d0 43 48 5f 53 49 5a 45 5d 5d 20 5e 28 3c 64 74 3e  CH_SIZE]] ^(<dt>
4b9e0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43  SQLITE_STATUS_SC
4b9f0 52 41 54 43 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a  RATCH_SIZE</dt>.
4ba00 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61  ** <dd>This para
4ba10 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68  meter records th
4ba20 65 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79  e largest memory
4ba30 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75   allocation requ
4ba40 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f  est.** handed to
4ba50 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79   [scratch memory
4ba60 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e   allocator].  On
4ba70 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  ly the value ret
4ba80 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20  urned in the.** 
4ba90 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72 61  *pHighwater para
4baa0 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
4bab0 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20 6f  3_status()] is o
4bac0 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a  f interest.  .**
4bad0 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74 74   The value writt
4bae0 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 75  en into the *pCu
4baf0 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20  rrent parameter 
4bb00 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64  is undefined.</d
4bb10 64 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c  d>)^.**.** [[SQL
4bb20 49 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45  ITE_STATUS_PARSE
4bb30 52 5f 53 54 41 43 4b 5d 5d 20 5e 28 3c 64 74 3e  R_STACK]] ^(<dt>
4bb40 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
4bb50 52 53 45 52 5f 53 54 41 43 4b 3c 2f 64 74 3e 0a  RSER_STACK</dt>.
4bb60 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61  ** <dd>This para
4bb70 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68  meter records th
4bb80 65 20 64 65 65 70 65 73 74 20 70 61 72 73 65 72  e deepest parser
4bb90 20 73 74 61 63 6b 2e 20 20 49 74 20 69 73 20 6f   stack.  It is o
4bba0 6e 6c 79 0a 2a 2a 20 6d 65 61 6e 69 6e 67 66 75  nly.** meaningfu
4bbb0 6c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63  l if SQLite is c
4bbc0 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b 59 59  ompiled with [YY
4bbd0 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50  TRACKMAXSTACKDEP
4bbe0 54 48 5d 2e 3c 2f 64 64 3e 29 5e 0a 2a 2a 20 3c  TH].</dd>)^.** <
4bbf0 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 73  /dl>.**.** New s
4bc00 74 61 74 75 73 20 70 61 72 61 6d 65 74 65 72 73  tatus parameters
4bc10 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 66 72   may be added fr
4bc20 6f 6d 20 74 69 6d 65 20 74 6f 20 74 69 6d 65 2e  om time to time.
4bc30 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
4bc40 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59  TE_STATUS_MEMORY
4bc50 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 20 30  _USED          0
4bc60 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
4bc70 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45  STATUS_PAGECACHE
4bc80 5f 55 53 45 44 20 20 20 20 20 20 20 31 0a 23 64  _USED       1.#d
4bc90 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41  efine SQLITE_STA
4bca0 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56  TUS_PAGECACHE_OV
4bcb0 45 52 46 4c 4f 57 20 20 20 32 0a 23 64 65 66 69  ERFLOW   2.#defi
4bcc0 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53  ne SQLITE_STATUS
4bcd0 5f 53 43 52 41 54 43 48 5f 55 53 45 44 20 20 20  _SCRATCH_USED   
4bce0 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20        3.#define 
4bcf0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43  SQLITE_STATUS_SC
4bd00 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 20 20  RATCH_OVERFLOW  
4bd10 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c     4.#define SQL
4bd20 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f  ITE_STATUS_MALLO
4bd30 43 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 20  C_SIZE          
4bd40 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  5.#define SQLITE
4bd50 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53  _STATUS_PARSER_S
4bd60 54 41 43 4b 20 20 20 20 20 20 20 20 20 36 0a 23  TACK         6.#
4bd70 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
4bd80 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 53  ATUS_PAGECACHE_S
4bd90 49 5a 45 20 20 20 20 20 20 20 37 0a 23 64 65 66  IZE       7.#def
4bda0 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55  ine SQLITE_STATU
4bdb0 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 20 20  S_SCRATCH_SIZE  
4bdc0 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65         8.#define
4bdd0 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d   SQLITE_STATUS_M
4bde0 41 4c 4c 4f 43 5f 43 4f 55 4e 54 20 20 20 20 20  ALLOC_COUNT     
4bdf0 20 20 20 20 39 0a 0a 2f 2a 0a 2a 2a 20 43 41 50      9../*.** CAP
4be00 49 33 52 45 46 3a 20 44 61 74 61 62 61 73 65 20  I3REF: Database 
4be10 43 6f 6e 6e 65 63 74 69 6f 6e 20 53 74 61 74 75  Connection Statu
4be20 73 0a 2a 2a 0a 2a 2a 20 5e 54 68 69 73 20 69 6e  s.**.** ^This in
4be30 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20  terface is used 
4be40 74 6f 20 72 65 74 72 69 65 76 65 20 72 75 6e 74  to retrieve runt
4be50 69 6d 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72  ime status infor
4be60 6d 61 74 69 6f 6e 20 0a 2a 2a 20 61 62 6f 75 74  mation .** about
4be70 20 61 20 73 69 6e 67 6c 65 20 5b 64 61 74 61 62   a single [datab
4be80 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e  ase connection].
4be90 20 20 5e 54 68 65 20 66 69 72 73 74 20 61 72 67    ^The first arg
4bea0 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20  ument is the.** 
4beb0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
4bec0 69 6f 6e 20 6f 62 6a 65 63 74 20 74 6f 20 62 65  ion object to be
4bed0 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 20   interrogated.  
4bee0 5e 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75  ^The second argu
4bef0 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e  ment.** is an in
4bf00 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2c 20  teger constant, 
4bf10 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 73  taken from the s
4bf20 65 74 20 6f 66 0a 2a 2a 20 5b 53 51 4c 49 54 45  et of.** [SQLITE
4bf30 5f 44 42 53 54 41 54 55 53 20 6f 70 74 69 6f 6e  _DBSTATUS option
4bf40 73 5d 2c 20 74 68 61 74 0a 2a 2a 20 64 65 74 65  s], that.** dete
4bf50 72 6d 69 6e 65 73 20 74 68 65 20 70 61 72 61 6d  rmines the param
4bf60 65 74 65 72 20 74 6f 20 69 6e 74 65 72 72 6f 67  eter to interrog
4bf70 61 74 65 2e 20 20 54 68 65 20 73 65 74 20 6f 66  ate.  The set of
4bf80 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 42 53   .** [SQLITE_DBS
4bf90 54 41 54 55 53 20 6f 70 74 69 6f 6e 73 5d 20 69  TATUS options] i
4bfa0 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 74 6f 20 67  s likely.** to g
4bfb0 72 6f 77 20 69 6e 20 66 75 74 75 72 65 20 72 65  row in future re
4bfc0 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65  leases of SQLite
4bfd0 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 63 75 72  ..**.** ^The cur
4bfe0 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68  rent value of th
4bff0 65 20 72 65 71 75 65 73 74 65 64 20 70 61 72 61  e requested para
4c000 6d 65 74 65 72 20 69 73 20 77 72 69 74 74 65 6e  meter is written
4c010 20 69 6e 74 6f 20 2a 70 43 75 72 0a 2a 2a 20 61   into *pCur.** a
4c020 6e 64 20 74 68 65 20 68 69 67 68 65 73 74 20 69  nd the highest i
4c030 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 61 6c  nstantaneous val
4c040 75 65 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  ue is written in
4c050 74 6f 20 2a 70 48 69 77 74 72 2e 20 20 5e 49 66  to *pHiwtr.  ^If
4c060 0a 2a 2a 20 74 68 65 20 72 65 73 65 74 46 6c 67  .** the resetFlg
4c070 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
4c080 68 65 20 68 69 67 68 65 73 74 20 69 6e 73 74 61  he highest insta
4c090 6e 74 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69  ntaneous value i
4c0a0 73 0a 2a 2a 20 72 65 73 65 74 20 62 61 63 6b 20  s.** reset back 
4c0b0 64 6f 77 6e 20 74 6f 20 74 68 65 20 63 75 72 72  down to the curr
4c0c0 65 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  ent value..**.**
4c0d0 20 5e 54 68 65 20 73 71 6c 69 74 65 33 5f 64 62   ^The sqlite3_db
4c0e0 5f 73 74 61 74 75 73 28 29 20 72 6f 75 74 69 6e  _status() routin
4c0f0 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  e returns SQLITE
4c100 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61  _OK on success a
4c110 6e 64 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f  nd a.** non-zero
4c120 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e   [error code] on
4c130 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20   failure..**.** 
4c140 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74  See also: [sqlit
4c150 65 33 5f 73 74 61 74 75 73 28 29 5d 20 61 6e 64  e3_status()] and
4c160 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73   [sqlite3_stmt_s
4c170 74 61 74 75 73 28 29 5d 2e 0a 2a 2f 0a 53 51 4c  tatus()]..*/.SQL
4c180 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
4c190 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71  te3_db_status(sq
4c1a0 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20  lite3*, int op, 
4c1b0 69 6e 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  int *pCur, int *
4c1c0 70 48 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65  pHiwtr, int rese
4c1d0 74 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  tFlg);../*.** CA
4c1e0 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 50  PI3REF: Status P
4c1f0 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61  arameters for da
4c200 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
4c210 6e 73 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  ns.** KEYWORDS: 
4c220 7b 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53  {SQLITE_DBSTATUS
4c230 20 6f 70 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20   options}.**.** 
4c240 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20  These constants 
4c250 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c  are the availabl
4c260 65 20 69 6e 74 65 67 65 72 20 22 76 65 72 62 73  e integer "verbs
4c270 22 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 61  " that can be pa
4c280 73 73 65 64 20 61 73 0a 2a 2a 20 74 68 65 20 73  ssed as.** the s
4c290 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
4c2a0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64  o the [sqlite3_d
4c2b0 62 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65  b_status()] inte
4c2c0 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77  rface..**.** New
4c2d0 20 76 65 72 62 73 20 6d 61 79 20 62 65 20 61 64   verbs may be ad
4c2e0 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65  ded in future re
4c2f0 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65  leases of SQLite
4c300 2e 20 45 78 69 73 74 69 6e 67 20 76 65 72 62 73  . Existing verbs
4c310 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 64 69 73  .** might be dis
4c320 63 6f 6e 74 69 6e 75 65 64 2e 20 41 70 70 6c 69  continued. Appli
4c330 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 63  cations should c
4c340 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20  heck the return 
4c350 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 5b 73 71  code from.** [sq
4c360 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28  lite3_db_status(
4c370 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  )] to make sure 
4c380 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20 77 6f  that the call wo
4c390 72 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 5b 73 71  rked..** The [sq
4c3a0 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28  lite3_db_status(
4c3b0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c  )] interface wil
4c3c0 6c 20 72 65 74 75 72 6e 20 61 20 6e 6f 6e 2d 7a  l return a non-z
4c3d0 65 72 6f 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a  ero error code.*
4c3e0 2a 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e  * if a discontin
4c3f0 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74  ued or unsupport
4c400 65 64 20 76 65 72 62 20 69 73 20 69 6e 76 6f 6b  ed verb is invok
4c410 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a  ed..**.** <dl>.*
4c420 2a 20 5b 5b 53 51 4c 49 54 45 5f 44 42 53 54 41  * [[SQLITE_DBSTA
4c430 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53  TUS_LOOKASIDE_US
4c440 45 44 5d 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54  ED]] ^(<dt>SQLIT
4c450 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41  E_DBSTATUS_LOOKA
4c460 53 49 44 45 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a  SIDE_USED</dt>.*
4c470 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d  * <dd>This param
4c480 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65  eter returns the
4c490 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 6f 6b 61   number of looka
4c4a0 73 69 64 65 20 6d 65 6d 6f 72 79 20 73 6c 6f 74  side memory slot
4c4b0 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 63  s currently.** c
4c4c0 68 65 63 6b 65 64 20 6f 75 74 2e 3c 2f 64 64 3e  hecked out.</dd>
4c4d0 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54  )^.**.** [[SQLIT
4c4e0 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41  E_DBSTATUS_LOOKA
4c4f0 53 49 44 45 5f 48 49 54 5d 5d 20 5e 28 3c 64 74  SIDE_HIT]] ^(<dt
4c500 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53  >SQLITE_DBSTATUS
4c510 5f 4c 4f 4f 4b 41 53 49 44 45 5f 48 49 54 3c 2f  _LOOKASIDE_HIT</
4c520 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
4c530 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e  parameter return
4c540 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6d 61 6c  s the number mal
4c550 6c 6f 63 20 61 74 74 65 6d 70 74 73 20 74 68 61  loc attempts tha
4c560 74 20 77 65 72 65 20 0a 2a 2a 20 73 61 74 69 73  t were .** satis
4c570 66 69 65 64 20 75 73 69 6e 67 20 6c 6f 6f 6b 61  fied using looka
4c580 73 69 64 65 20 6d 65 6d 6f 72 79 2e 20 4f 6e 6c  side memory. Onl
4c590 79 20 74 68 65 20 68 69 67 68 2d 77 61 74 65 72  y the high-water
4c5a0 20 76 61 6c 75 65 20 69 73 20 6d 65 61 6e 69 6e   value is meanin
4c5b0 67 66 75 6c 3b 0a 2a 2a 20 74 68 65 20 63 75 72  gful;.** the cur
4c5c0 72 65 6e 74 20 76 61 6c 75 65 20 69 73 20 61 6c  rent value is al
4c5d0 77 61 79 73 20 7a 65 72 6f 2e 29 5e 0a 2a 2a 0a  ways zero.)^.**.
4c5e0 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 44 42 53 54  ** [[SQLITE_DBST
4c5f0 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 4d  ATUS_LOOKASIDE_M
4c600 49 53 53 5f 53 49 5a 45 5d 5d 0a 2a 2a 20 5e 28  ISS_SIZE]].** ^(
4c610 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42 53 54 41  <dt>SQLITE_DBSTA
4c620 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 4d 49  TUS_LOOKASIDE_MI
4c630 53 53 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20  SS_SIZE</dt>.** 
4c640 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74  <dd>This paramet
4c650 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  er returns the n
4c660 75 6d 62 65 72 20 6d 61 6c 6c 6f 63 20 61 74 74  umber malloc att
4c670 65 6d 70 74 73 20 74 68 61 74 20 6d 69 67 68 74  empts that might
4c680 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 73 61   have.** been sa
4c690 74 69 73 66 69 65 64 20 75 73 69 6e 67 20 6c 6f  tisfied using lo
4c6a0 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 62  okaside memory b
4c6b0 75 74 20 66 61 69 6c 65 64 20 64 75 65 20 74 6f  ut failed due to
4c6c0 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 0a 2a   the amount of.*
4c6d0 2a 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 73 74  * memory request
4c6e0 65 64 20 62 65 69 6e 67 20 6c 61 72 67 65 72 20  ed being larger 
4c6f0 74 68 61 6e 20 74 68 65 20 6c 6f 6f 6b 61 73 69  than the lookasi
4c700 64 65 20 73 6c 6f 74 20 73 69 7a 65 2e 0a 2a 2a  de slot size..**
4c710 20 4f 6e 6c 79 20 74 68 65 20 68 69 67 68 2d 77   Only the high-w
4c720 61 74 65 72 20 76 61 6c 75 65 20 69 73 20 6d 65  ater value is me
4c730 61 6e 69 6e 67 66 75 6c 3b 0a 2a 2a 20 74 68 65  aningful;.** the
4c740 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 69   current value i
4c750 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2e 29 5e  s always zero.)^
4c760 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f  .**.** [[SQLITE_
4c770 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49  DBSTATUS_LOOKASI
4c780 44 45 5f 4d 49 53 53 5f 46 55 4c 4c 5d 5d 0a 2a  DE_MISS_FULL]].*
4c790 2a 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 44  * ^(<dt>SQLITE_D
4c7a0 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44  BSTATUS_LOOKASID
4c7b0 45 5f 4d 49 53 53 5f 46 55 4c 4c 3c 2f 64 74 3e  E_MISS_FULL</dt>
4c7c0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72  .** <dd>This par
4c7d0 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74  ameter returns t
4c7e0 68 65 20 6e 75 6d 62 65 72 20 6d 61 6c 6c 6f 63  he number malloc
4c7f0 20 61 74 74 65 6d 70 74 73 20 74 68 61 74 20 6d   attempts that m
4c800 69 67 68 74 20 68 61 76 65 0a 2a 2a 20 62 65 65  ight have.** bee
4c810 6e 20 73 61 74 69 73 66 69 65 64 20 75 73 69 6e  n satisfied usin
4c820 67 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f  g lookaside memo
4c830 72 79 20 62 75 74 20 66 61 69 6c 65 64 20 64 75  ry but failed du
4c840 65 20 74 6f 20 61 6c 6c 20 6c 6f 6f 6b 61 73 69  e to all lookasi
4c850 64 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 72  de.** memory alr
4c860 65 61 64 79 20 62 65 69 6e 67 20 69 6e 20 75 73  eady being in us
4c870 65 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 68  e..** Only the h
4c880 69 67 68 2d 77 61 74 65 72 20 76 61 6c 75 65 20  igh-water value 
4c890 69 73 20 6d 65 61 6e 69 6e 67 66 75 6c 3b 0a 2a  is meaningful;.*
4c8a0 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61  * the current va
4c8b0 6c 75 65 20 69 73 20 61 6c 77 61 79 73 20 7a 65  lue is always ze
4c8c0 72 6f 2e 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51  ro.)^.**.** [[SQ
4c8d0 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41  LITE_DBSTATUS_CA
4c8e0 43 48 45 5f 55 53 45 44 5d 5d 20 5e 28 3c 64 74  CHE_USED]] ^(<dt
4c8f0 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53  >SQLITE_DBSTATUS
4c900 5f 43 41 43 48 45 5f 55 53 45 44 3c 2f 64 74 3e  _CACHE_USED</dt>
4c910 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72  .** <dd>This par
4c920 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74  ameter returns t
4c930 68 65 20 61 70 70 72 6f 78 69 6d 61 74 65 20 6e  he approximate n
4c940 75 6d 62 65 72 20 6f 66 20 6f 66 20 62 79 74 65  umber of of byte
4c950 73 20 6f 66 20 68 65 61 70 0a 2a 2a 20 6d 65 6d  s of heap.** mem
4c960 6f 72 79 20 75 73 65 64 20 62 79 20 61 6c 6c 20  ory used by all 
4c970 70 61 67 65 72 20 63 61 63 68 65 73 20 61 73 73  pager caches ass
4c980 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
4c990 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
4c9a0 74 69 6f 6e 2e 29 5e 0a 2a 2a 20 5e 54 68 65 20  tion.)^.** ^The 
4c9b0 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61  highwater mark a
4c9c0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 53  ssociated with S
4c9d0 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43  QLITE_DBSTATUS_C
4c9e0 41 43 48 45 5f 55 53 45 44 20 69 73 20 61 6c 77  ACHE_USED is alw
4c9f0 61 79 73 20 30 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 53  ays 0..**.** [[S
4ca00 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 53  QLITE_DBSTATUS_S
4ca10 43 48 45 4d 41 5f 55 53 45 44 5d 5d 20 5e 28 3c  CHEMA_USED]] ^(<
4ca20 64 74 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54  dt>SQLITE_DBSTAT
4ca30 55 53 5f 53 43 48 45 4d 41 5f 55 53 45 44 3c 2f  US_SCHEMA_USED</
4ca40 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
4ca50 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e  parameter return
4ca60 73 20 74 68 65 20 61 70 70 72 6f 78 69 6d 61 74  s the approximat
4ca70 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 66 20 62  e number of of b
4ca80 79 74 65 73 20 6f 66 20 68 65 61 70 0a 2a 2a 20  ytes of heap.** 
4ca90 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 73  memory used to s
4caa0 74 6f 72 65 20 74 68 65 20 73 63 68 65 6d 61 20  tore the schema 
4cab0 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65  for all database
4cac0 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20  s associated.** 
4cad0 77 69 74 68 20 74 68 65 20 63 6f 6e 6e 65 63 74  with the connect
4cae0 69 6f 6e 20 2d 20 6d 61 69 6e 2c 20 74 65 6d 70  ion - main, temp
4caf0 2c 20 61 6e 64 20 61 6e 79 20 5b 41 54 54 41 43  , and any [ATTAC
4cb00 48 5d 2d 65 64 20 64 61 74 61 62 61 73 65 73 2e  H]-ed databases.
4cb10 29 5e 20 0a 2a 2a 20 5e 54 68 65 20 66 75 6c 6c  )^ .** ^The full
4cb20 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72   amount of memor
4cb30 79 20 75 73 65 64 20 62 79 20 74 68 65 20 73 63  y used by the sc
4cb40 68 65 6d 61 73 20 69 73 20 72 65 70 6f 72 74 65  hemas is reporte
4cb50 64 2c 20 65 76 65 6e 20 69 66 20 74 68 65 0a 2a  d, even if the.*
4cb60 2a 20 73 63 68 65 6d 61 20 6d 65 6d 6f 72 79 20  * schema memory 
4cb70 69 73 20 73 68 61 72 65 64 20 77 69 74 68 20 6f  is shared with o
4cb80 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f  ther database co
4cb90 6e 6e 65 63 74 69 6f 6e 73 20 64 75 65 20 74 6f  nnections due to
4cba0 0a 2a 2a 20 5b 73 68 61 72 65 64 20 63 61 63 68  .** [shared cach
4cbb0 65 20 6d 6f 64 65 5d 20 62 65 69 6e 67 20 65 6e  e mode] being en
4cbc0 61 62 6c 65 64 2e 0a 2a 2a 20 5e 54 68 65 20 68  abled..** ^The h
4cbd0 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61 73  ighwater mark as
4cbe0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 53 51  sociated with SQ
4cbf0 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 53 43  LITE_DBSTATUS_SC
4cc00 48 45 4d 41 5f 55 53 45 44 20 69 73 20 61 6c 77  HEMA_USED is alw
4cc10 61 79 73 20 30 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 53  ays 0..**.** [[S
4cc20 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 53  QLITE_DBSTATUS_S
4cc30 54 4d 54 5f 55 53 45 44 5d 5d 20 5e 28 3c 64 74  TMT_USED]] ^(<dt
4cc40 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53  >SQLITE_DBSTATUS
4cc50 5f 53 54 4d 54 5f 55 53 45 44 3c 2f 64 74 3e 0a  _STMT_USED</dt>.
4cc60 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61  ** <dd>This para
4cc70 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68  meter returns th
4cc80 65 20 61 70 70 72 6f 78 69 6d 61 74 65 20 6e 75  e approximate nu
4cc90 6d 62 65 72 20 6f 66 20 6f 66 20 62 79 74 65 73  mber of of bytes
4cca0 20 6f 66 20 68 65 61 70 0a 2a 2a 20 61 6e 64 20   of heap.** and 
4ccb0 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79  lookaside memory
4ccc0 20 75 73 65 64 20 62 79 20 61 6c 6c 20 70 72 65   used by all pre
4ccd0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
4cce0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
4ccf0 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
4cd00 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 29 5e 0a 2a   connection.)^.*
4cd10 2a 20 5e 54 68 65 20 68 69 67 68 77 61 74 65 72  * ^The highwater
4cd20 20 6d 61 72 6b 20 61 73 73 6f 63 69 61 74 65 64   mark associated
4cd30 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 42 53   with SQLITE_DBS
4cd40 54 41 54 55 53 5f 53 54 4d 54 5f 55 53 45 44 20  TATUS_STMT_USED 
4cd50 69 73 20 61 6c 77 61 79 73 20 30 2e 0a 2a 2a 20  is always 0..** 
4cd60 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b 5b 53 51  </dd>.**.** [[SQ
4cd70 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41  LITE_DBSTATUS_CA
4cd80 43 48 45 5f 48 49 54 5d 5d 20 5e 28 3c 64 74 3e  CHE_HIT]] ^(<dt>
4cd90 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
4cda0 43 41 43 48 45 5f 48 49 54 3c 2f 64 74 3e 0a 2a  CACHE_HIT</dt>.*
4cdb0 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d  * <dd>This param
4cdc0 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65  eter returns the
4cdd0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 72   number of pager
4cde0 20 63 61 63 68 65 20 68 69 74 73 20 74 68 61 74   cache hits that
4cdf0 20 68 61 76 65 0a 2a 2a 20 6f 63 63 75 72 72 65   have.** occurre
4ce00 64 2e 29 5e 20 5e 54 68 65 20 68 69 67 68 77 61  d.)^ ^The highwa
4ce10 74 65 72 20 6d 61 72 6b 20 61 73 73 6f 63 69 61  ter mark associa
4ce20 74 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f  ted with SQLITE_
4ce30 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f 48  DBSTATUS_CACHE_H
4ce40 49 54 20 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73  IT .** is always
4ce50 20 30 2e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a   0..** </dd>.**.
4ce60 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 44 42 53 54  ** [[SQLITE_DBST
4ce70 41 54 55 53 5f 43 41 43 48 45 5f 4d 49 53 53 5d  ATUS_CACHE_MISS]
4ce80 5d 20 5e 28 3c 64 74 3e 53 51 4c 49 54 45 5f 44  ] ^(<dt>SQLITE_D
4ce90 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f 4d 49  BSTATUS_CACHE_MI
4cea0 53 53 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  SS</dt>.** <dd>T
4ceb0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
4cec0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
4ced0 20 6f 66 20 70 61 67 65 72 20 63 61 63 68 65 20   of pager cache 
4cee0 6d 69 73 73 65 73 20 74 68 61 74 20 68 61 76 65  misses that have
4cef0 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2e 29 5e 20  .** occurred.)^ 
4cf00 5e 54 68 65 20 68 69 67 68 77 61 74 65 72 20 6d  ^The highwater m
4cf10 61 72 6b 20 61 73 73 6f 63 69 61 74 65 64 20 77  ark associated w
4cf20 69 74 68 20 53 51 4c 49 54 45 5f 44 42 53 54 41  ith SQLITE_DBSTA
4cf30 54 55 53 5f 43 41 43 48 45 5f 4d 49 53 53 20 0a  TUS_CACHE_MISS .
4cf40 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 30 2e 0a  ** is always 0..
4cf50 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 5b  ** </dd>.**.** [
4cf60 5b 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53  [SQLITE_DBSTATUS
4cf70 5f 43 41 43 48 45 5f 57 52 49 54 45 5d 5d 20 5e  _CACHE_WRITE]] ^
4cf80 28 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42 53 54  (<dt>SQLITE_DBST
4cf90 41 54 55 53 5f 43 41 43 48 45 5f 57 52 49 54 45  ATUS_CACHE_WRITE
4cfa0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
4cfb0 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75  s parameter retu
4cfc0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
4cfd0 66 20 64 69 72 74 79 20 63 61 63 68 65 20 65 6e  f dirty cache en
4cfe0 74 72 69 65 73 20 74 68 61 74 20 68 61 76 65 0a  tries that have.
4cff0 2a 2a 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  ** been written 
4d000 74 6f 20 64 69 73 6b 2e 20 53 70 65 63 69 66 69  to disk. Specifi
4d010 63 61 6c 6c 79 2c 20 74 68 65 20 6e 75 6d 62 65  cally, the numbe
4d020 72 20 6f 66 20 70 61 67 65 73 20 77 72 69 74 74  r of pages writt
4d030 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 77 61 6c  en to the.** wal
4d040 20 66 69 6c 65 20 69 6e 20 77 61 6c 20 6d 6f 64   file in wal mod
4d050 65 20 64 61 74 61 62 61 73 65 73 2c 20 6f 72 20  e databases, or 
4d060 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
4d070 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74  ges written to t
4d080 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66  he.** database f
4d090 69 6c 65 20 69 6e 20 72 6f 6c 6c 62 61 63 6b 20  ile in rollback 
4d0a0 6d 6f 64 65 20 64 61 74 61 62 61 73 65 73 2e 20  mode databases. 
4d0b0 41 6e 79 20 70 61 67 65 73 20 77 72 69 74 74 65  Any pages writte
4d0c0 6e 20 61 73 20 70 61 72 74 20 6f 66 0a 2a 2a 20  n as part of.** 
4d0d0 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c  transaction roll
4d0e0 62 61 63 6b 20 6f 72 20 64 61 74 61 62 61 73 65  back or database
4d0f0 20 72 65 63 6f 76 65 72 79 20 6f 70 65 72 61 74   recovery operat
4d100 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 69 6e 63  ions are not inc
4d110 6c 75 64 65 64 2e 0a 2a 2a 20 49 66 20 61 6e 20  luded..** If an 
4d120 49 4f 20 6f 72 20 6f 74 68 65 72 20 65 72 72 6f  IO or other erro
4d130 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 77  r occurs while w
4d140 72 69 74 69 6e 67 20 61 20 70 61 67 65 20 74 6f  riting a page to
4d150 20 64 69 73 6b 2c 20 74 68 65 20 65 66 66 65 63   disk, the effec
4d160 74 0a 2a 2a 20 6f 6e 20 73 75 62 73 65 71 75 65  t.** on subseque
4d170 6e 74 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54  nt SQLITE_DBSTAT
4d180 55 53 5f 43 41 43 48 45 5f 57 52 49 54 45 20 72  US_CACHE_WRITE r
4d190 65 71 75 65 73 74 73 20 69 73 20 75 6e 64 65 66  equests is undef
4d1a0 69 6e 65 64 2e 29 5e 20 5e 54 68 65 0a 2a 2a 20  ined.)^ ^The.** 
4d1b0 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61  highwater mark a
4d1c0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 53  ssociated with S
4d1d0 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43  QLITE_DBSTATUS_C
4d1e0 41 43 48 45 5f 57 52 49 54 45 20 69 73 20 61 6c  ACHE_WRITE is al
4d1f0 77 61 79 73 20 30 2e 0a 2a 2a 20 3c 2f 64 64 3e  ways 0..** </dd>
4d200 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65  .** </dl>.*/.#de
4d210 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54  fine SQLITE_DBST
4d220 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55  ATUS_LOOKASIDE_U
4d230 53 45 44 20 20 20 20 20 20 20 30 0a 23 64 65 66  SED       0.#def
4d240 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41  ine SQLITE_DBSTA
4d250 54 55 53 5f 43 41 43 48 45 5f 55 53 45 44 20 20  TUS_CACHE_USED  
4d260 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69           1.#defi
4d270 6e 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54  ne SQLITE_DBSTAT
4d280 55 53 5f 53 43 48 45 4d 41 5f 55 53 45 44 20 20  US_SCHEMA_USED  
4d290 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e          2.#defin
4d2a0 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55  e SQLITE_DBSTATU
4d2b0 53 5f 53 54 4d 54 5f 55 53 45 44 20 20 20 20 20  S_STMT_USED     
4d2c0 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65         3.#define
4d2d0 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53   SQLITE_DBSTATUS
4d2e0 5f 4c 4f 4f 4b 41 53 49 44 45 5f 48 49 54 20 20  _LOOKASIDE_HIT  
4d2f0 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20        4.#define 
4d300 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
4d310 4c 4f 4f 4b 41 53 49 44 45 5f 4d 49 53 53 5f 53  LOOKASIDE_MISS_S
4d320 49 5a 45 20 20 35 0a 23 64 65 66 69 6e 65 20 53  IZE  5.#define S
4d330 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c  QLITE_DBSTATUS_L
4d340 4f 4f 4b 41 53 49 44 45 5f 4d 49 53 53 5f 46 55  OOKASIDE_MISS_FU
4d350 4c 4c 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51  LL  6.#define SQ
4d360 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41  LITE_DBSTATUS_CA
4d370 43 48 45 5f 48 49 54 20 20 20 20 20 20 20 20 20  CHE_HIT         
4d380 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c     7.#define SQL
4d390 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43  ITE_DBSTATUS_CAC
4d3a0 48 45 5f 4d 49 53 53 20 20 20 20 20 20 20 20 20  HE_MISS         
4d3b0 20 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    8.#define SQLI
4d3c0 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43 48  TE_DBSTATUS_CACH
4d3d0 45 5f 57 52 49 54 45 20 20 20 20 20 20 20 20 20  E_WRITE         
4d3e0 20 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   9.#define SQLIT
4d3f0 45 5f 44 42 53 54 41 54 55 53 5f 4d 41 58 20 20  E_DBSTATUS_MAX  
4d400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d410 39 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 64  9   /* Largest d
4d420 65 66 69 6e 65 64 20 44 42 53 54 41 54 55 53 20  efined DBSTATUS 
4d430 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  */.../*.** CAPI3
4d440 52 45 46 3a 20 50 72 65 70 61 72 65 64 20 53 74  REF: Prepared St
4d450 61 74 65 6d 65 6e 74 20 53 74 61 74 75 73 0a 2a  atement Status.*
4d460 2a 0a 2a 2a 20 5e 28 45 61 63 68 20 70 72 65 70  *.** ^(Each prep
4d470 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d  ared statement m
4d480 61 69 6e 74 61 69 6e 73 20 76 61 72 69 6f 75 73  aintains various
4d490 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 54 4d 54  .** [SQLITE_STMT
4d4a0 53 54 41 54 55 53 20 63 6f 75 6e 74 65 72 73 5d  STATUS counters]
4d4b0 20 74 68 61 74 20 6d 65 61 73 75 72 65 20 74 68   that measure th
4d4c0 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 74  e number.** of t
4d4d0 69 6d 65 73 20 69 74 20 68 61 73 20 70 65 72 66  imes it has perf
4d4e0 6f 72 6d 65 64 20 73 70 65 63 69 66 69 63 20 6f  ormed specific o
4d4f0 70 65 72 61 74 69 6f 6e 73 2e 29 5e 20 20 54 68  perations.)^  Th
4d500 65 73 65 20 63 6f 75 6e 74 65 72 73 20 63 61 6e  ese counters can
4d510 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 6d  .** be used to m
4d520 6f 6e 69 74 6f 72 20 74 68 65 20 70 65 72 66 6f  onitor the perfo
4d530 72 6d 61 6e 63 65 20 63 68 61 72 61 63 74 65 72  rmance character
4d540 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 70 72  istics of the pr
4d550 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d  epared.** statem
4d560 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70  ents.  For examp
4d570 6c 65 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 65  le, if the numbe
4d580 72 20 6f 66 20 74 61 62 6c 65 20 73 74 65 70 73  r of table steps
4d590 20 67 72 65 61 74 6c 79 20 65 78 63 65 65 64 73   greatly exceeds
4d5a0 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
4d5b0 66 20 74 61 62 6c 65 20 73 65 61 72 63 68 65 73  f table searches
4d5c0 20 6f 72 20 72 65 73 75 6c 74 20 72 6f 77 73 2c   or result rows,
4d5d0 20 74 68 61 74 20 77 6f 75 6c 64 20 74 65 6e 64   that would tend
4d5e0 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20   to indicate.** 
4d5f0 74 68 61 74 20 74 68 65 20 70 72 65 70 61 72 65  that the prepare
4d600 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 75  d statement is u
4d610 73 69 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 6c  sing a full tabl
4d620 65 20 73 63 61 6e 20 72 61 74 68 65 72 20 74 68  e scan rather th
4d630 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20  an.** an index. 
4d640 20 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 69 73 20 69   .**.** ^(This i
4d650 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64  nterface is used
4d660 20 74 6f 20 72 65 74 72 69 65 76 65 20 61 6e 64   to retrieve and
4d670 20 72 65 73 65 74 20 63 6f 75 6e 74 65 72 20 76   reset counter v
4d680 61 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20  alues from.** a 
4d690 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
4d6a0 65 6e 74 5d 2e 20 20 54 68 65 20 66 69 72 73 74  ent].  The first
4d6b0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
4d6c0 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
4d6d0 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f  ent.** object to
4d6e0 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64   be interrogated
4d6f0 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  .  The second ar
4d700 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20  gument.** is an 
4d710 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72  integer code for
4d720 20 61 20 73 70 65 63 69 66 69 63 20 5b 53 51 4c   a specific [SQL
4d730 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 20 63  ITE_STMTSTATUS c
4d740 6f 75 6e 74 65 72 5d 0a 2a 2a 20 74 6f 20 62 65  ounter].** to be
4d750 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 29 5e   interrogated.)^
4d760 0a 2a 2a 20 5e 54 68 65 20 63 75 72 72 65 6e 74  .** ^The current
4d770 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65   value of the re
4d780 71 75 65 73 74 65 64 20 63 6f 75 6e 74 65 72 20  quested counter 
4d790 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
4d7a0 5e 49 66 20 74 68 65 20 72 65 73 65 74 46 6c 67  ^If the resetFlg
4d7b0 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
4d7c0 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 72 65  he counter is re
4d7d0 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 74 65  set to zero afte
4d7e0 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66  r this.** interf
4d7f0 61 63 65 20 63 61 6c 6c 20 72 65 74 75 72 6e 73  ace call returns
4d800 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
4d810 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75  : [sqlite3_statu
4d820 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  s()] and [sqlite
4d830 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e 0a  3_db_status()]..
4d840 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
4d850 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73  t sqlite3_stmt_s
4d860 74 61 74 75 73 28 73 71 6c 69 74 65 33 5f 73 74  tatus(sqlite3_st
4d870 6d 74 2a 2c 20 69 6e 74 20 6f 70 2c 69 6e 74 20  mt*, int op,int 
4d880 72 65 73 65 74 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a  resetFlg);../*.*
4d890 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 61 74  * CAPI3REF: Stat
4d8a0 75 73 20 50 61 72 61 6d 65 74 65 72 73 20 66 6f  us Parameters fo
4d8b0 72 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  r prepared state
4d8c0 6d 65 6e 74 73 0a 2a 2a 20 4b 45 59 57 4f 52 44  ments.** KEYWORD
4d8d0 53 3a 20 7b 53 51 4c 49 54 45 5f 53 54 4d 54 53  S: {SQLITE_STMTS
4d8e0 54 41 54 55 53 20 63 6f 75 6e 74 65 72 7d 20 7b  TATUS counter} {
4d8f0 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55  SQLITE_STMTSTATU
4d900 53 20 63 6f 75 6e 74 65 72 73 7d 0a 2a 2a 0a 2a  S counters}.**.*
4d910 2a 20 54 68 65 73 65 20 70 72 65 70 72 6f 63 65  * These preproce
4d920 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 65 66 69  ssor macros defi
4d930 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73  ne integer codes
4d940 20 74 68 61 74 20 6e 61 6d 65 20 63 6f 75 6e 74   that name count
4d950 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61 73 73  er.** values ass
4d960 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
4d970 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73   [sqlite3_stmt_s
4d980 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61  tatus()] interfa
4d990 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61 6e 69  ce..** The meani
4d9a0 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72 69 6f  ngs of the vario
4d9b0 75 73 20 63 6f 75 6e 74 65 72 73 20 61 72 65 20  us counters are 
4d9c0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
4d9d0 2a 20 3c 64 6c 3e 0a 2a 2a 20 5b 5b 53 51 4c 49  * <dl>.** [[SQLI
4d9e0 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55  TE_STMTSTATUS_FU
4d9f0 4c 4c 53 43 41 4e 5f 53 54 45 50 5d 5d 20 3c 64  LLSCAN_STEP]] <d
4da00 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41  t>SQLITE_STMTSTA
4da10 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45  TUS_FULLSCAN_STE
4da20 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 5e 54  P</dt>.** <dd>^T
4da30 68 69 73 20 69 73 20 74 68 65 20 6e 75 6d 62 65  his is the numbe
4da40 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20  r of times that 
4da50 53 51 4c 69 74 65 20 68 61 73 20 73 74 65 70 70  SQLite has stepp
4da60 65 64 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a  ed forward in.**
4da70 20 61 20 74 61 62 6c 65 20 61 73 20 70 61 72 74   a table as part
4da80 20 6f 66 20 61 20 66 75 6c 6c 20 74 61 62 6c 65   of a full table
4da90 20 73 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75   scan.  Large nu
4daa0 6d 62 65 72 73 20 66 6f 72 20 74 68 69 73 20 63  mbers for this c
4dab0 6f 75 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e  ounter.** may in
4dac0 64 69 63 61 74 65 20 6f 70 70 6f 72 74 75 6e 69  dicate opportuni
4dad0 74 69 65 73 20 66 6f 72 20 70 65 72 66 6f 72 6d  ties for perform
4dae0 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74  ance improvement
4daf0 20 74 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72   through .** car
4db00 65 66 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 69  eful use of indi
4db10 63 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ces.</dd>.**.** 
4db20 5b 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41  [[SQLITE_STMTSTA
4db30 54 55 53 5f 53 4f 52 54 5d 5d 20 3c 64 74 3e 53  TUS_SORT]] <dt>S
4db40 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
4db50 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  _SORT</dt>.** <d
4db60 64 3e 5e 54 68 69 73 20 69 73 20 74 68 65 20 6e  d>^This is the n
4db70 75 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 6f 70  umber of sort op
4db80 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20 68 61  erations that ha
4db90 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20  ve occurred..** 
4dba0 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  A non-zero value
4dbb0 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 65 72   in this counter
4dbc0 20 6d 61 79 20 69 6e 64 69 63 61 74 65 20 61 6e   may indicate an
4dbd0 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 0a   opportunity to.
4dbe0 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 70  ** improvement p
4dbf0 65 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f 75  erformance throu
4dc00 67 68 20 63 61 72 65 66 75 6c 20 75 73 65 20 6f  gh careful use o
4dc10 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0a  f indices.</dd>.
4dc20 2a 2a 0a 2a 2a 20 5b 5b 53 51 4c 49 54 45 5f 53  **.** [[SQLITE_S
4dc30 54 4d 54 53 54 41 54 55 53 5f 41 55 54 4f 49 4e  TMTSTATUS_AUTOIN
4dc40 44 45 58 5d 5d 20 3c 64 74 3e 53 51 4c 49 54 45  DEX]] <dt>SQLITE
4dc50 5f 53 54 4d 54 53 54 41 54 55 53 5f 41 55 54 4f  _STMTSTATUS_AUTO
4dc60 49 4e 44 45 58 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  INDEX</dt>.** <d
4dc70 64 3e 5e 54 68 69 73 20 69 73 20 74 68 65 20 6e  d>^This is the n
4dc80 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e  umber of rows in
4dc90 73 65 72 74 65 64 20 69 6e 74 6f 20 74 72 61 6e  serted into tran
4dca0 73 69 65 6e 74 20 69 6e 64 69 63 65 73 20 74 68  sient indices th
4dcb0 61 74 0a 2a 2a 20 77 65 72 65 20 63 72 65 61 74  at.** were creat
4dcc0 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ed automatically
4dcd0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 68 65 6c   in order to hel
4dce0 70 20 6a 6f 69 6e 73 20 72 75 6e 20 66 61 73 74  p joins run fast
4dcf0 65 72 2e 0a 2a 2a 20 41 20 6e 6f 6e 2d 7a 65 72  er..** A non-zer
4dd00 6f 20 76 61 6c 75 65 20 69 6e 20 74 68 69 73 20  o value in this 
4dd10 63 6f 75 6e 74 65 72 20 6d 61 79 20 69 6e 64 69  counter may indi
4dd20 63 61 74 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e  cate an opportun
4dd30 69 74 79 20 74 6f 0a 2a 2a 20 69 6d 70 72 6f 76  ity to.** improv
4dd40 65 6d 65 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63  ement performanc
4dd50 65 20 62 79 20 61 64 64 69 6e 67 20 70 65 72 6d  e by adding perm
4dd60 61 6e 65 6e 74 20 69 6e 64 69 63 65 73 20 74 68  anent indices th
4dd70 61 74 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e 65 65  at do not.** nee
4dd80 64 20 74 6f 20 62 65 20 72 65 69 6e 69 74 69 61  d to be reinitia
4dd90 6c 69 7a 65 64 20 65 61 63 68 20 74 69 6d 65 20  lized each time 
4dda0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
4ddb0 20 72 75 6e 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f   run.</dd>.** </
4ddc0 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  dl>.*/.#define S
4ddd0 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
4dde0 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 20 20  _FULLSCAN_STEP  
4ddf0 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c     1.#define SQL
4de00 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53  ITE_STMTSTATUS_S
4de10 4f 52 54 20 20 20 20 20 20 20 20 20 20 20 20 20  ORT             
4de20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   2.#define SQLIT
4de30 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 41 55 54  E_STMTSTATUS_AUT
4de40 4f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 33  OINDEX         3
4de50 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
4de60 3a 20 43 75 73 74 6f 6d 20 50 61 67 65 20 43 61  : Custom Page Ca
4de70 63 68 65 20 4f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a  che Object.**.**
4de80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   The sqlite3_pca
4de90 63 68 65 20 74 79 70 65 20 69 73 20 6f 70 61 71  che type is opaq
4dea0 75 65 2e 20 20 49 74 20 69 73 20 69 6d 70 6c 65  ue.  It is imple
4deb0 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20 74 68 65  mented by.** the
4dec0 20 70 6c 75 67 67 61 62 6c 65 20 6d 6f 64 75 6c   pluggable modul
4ded0 65 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63  e.  The SQLite c
4dee0 6f 72 65 20 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c  ore has no knowl
4def0 65 64 67 65 20 6f 66 0a 2a 2a 20 69 74 73 20 73  edge of.** its s
4df00 69 7a 65 20 6f 72 20 69 6e 74 65 72 6e 61 6c 20  ize or internal 
4df10 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 6e 65  structure and ne
4df20 76 65 72 20 64 65 61 6c 73 20 77 69 74 68 20 74  ver deals with t
4df30 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63  he.** sqlite3_pc
4df40 61 63 68 65 20 6f 62 6a 65 63 74 20 65 78 63 65  ache object exce
4df50 70 74 20 62 79 20 68 6f 6c 64 69 6e 67 20 61 6e  pt by holding an
4df60 64 20 70 61 73 73 69 6e 67 20 70 6f 69 6e 74 65  d passing pointe
4df70 72 73 0a 2a 2a 20 74 6f 20 74 68 65 20 6f 62 6a  rs.** to the obj
4df80 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b  ect..**.** See [
4df90 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d  sqlite3_pcache_m
4dfa0 65 74 68 6f 64 73 32 5d 20 66 6f 72 20 61 64 64  ethods2] for add
4dfb0 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
4dfc0 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ion..*/.typedef 
4dfd0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70  struct sqlite3_p
4dfe0 63 61 63 68 65 20 73 71 6c 69 74 65 33 5f 70 63  cache sqlite3_pc
4dff0 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ache;../*.** CAP
4e000 49 33 52 45 46 3a 20 43 75 73 74 6f 6d 20 50 61  I3REF: Custom Pa
4e010 67 65 20 43 61 63 68 65 20 4f 62 6a 65 63 74 0a  ge Cache Object.
4e020 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
4e030 33 5f 70 63 61 63 68 65 5f 70 61 67 65 20 6f 62  3_pcache_page ob
4e040 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 20  ject represents 
4e050 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 69 6e  a single page in
4e060 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 63 61 63   the.** page cac
4e070 68 65 2e 20 20 54 68 65 20 70 61 67 65 20 63 61  he.  The page ca
4e080 63 68 65 20 77 69 6c 6c 20 61 6c 6c 6f 63 61 74  che will allocat
4e090 65 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74  e instances of t
4e0a0 68 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 20  his.** object.  
4e0b0 56 61 72 69 6f 75 73 20 6d 65 74 68 6f 64 73 20  Various methods 
4e0c0 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68  of the page cach
4e0d0 65 20 75 73 65 20 70 6f 69 6e 74 65 72 73 20 74  e use pointers t
4e0e0 6f 20 69 6e 73 74 61 6e 63 65 73 0a 2a 2a 20 6f  o instances.** o
4e0f0 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 61 73  f this object as
4e100 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 61   parameters or a
4e110 73 20 74 68 65 69 72 20 72 65 74 75 72 6e 20 76  s their return v
4e120 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  alue..**.** See 
4e130 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f  [sqlite3_pcache_
4e140 6d 65 74 68 6f 64 73 32 5d 20 66 6f 72 20 61 64  methods2] for ad
4e150 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
4e160 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  tion..*/.typedef
4e170 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
4e180 70 63 61 63 68 65 5f 70 61 67 65 20 73 71 6c 69  pcache_page sqli
4e190 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67 65 3b  te3_pcache_page;
4e1a0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
4e1b0 70 63 61 63 68 65 5f 70 61 67 65 20 7b 0a 20 20  pcache_page {.  
4e1c0 76 6f 69 64 20 2a 70 42 75 66 3b 20 20 20 20 20  void *pBuf;     
4e1d0 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e     /* The conten
4e1e0 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f  t of the page */
4e1f0 0a 20 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b  .  void *pExtra;
4e200 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 69        /* Extra i
4e210 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 73 6f 63  nformation assoc
4e220 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 70  iated with the p
4e230 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  age */.};../*.**
4e240 20 43 41 50 49 33 52 45 46 3a 20 41 70 70 6c 69   CAPI3REF: Appli
4e250 63 61 74 69 6f 6e 20 44 65 66 69 6e 65 64 20 50  cation Defined P
4e260 61 67 65 20 43 61 63 68 65 2e 0a 2a 2a 20 4b 45  age Cache..** KE
4e270 59 57 4f 52 44 53 3a 20 7b 70 61 67 65 20 63 61  YWORDS: {page ca
4e280 63 68 65 7d 0a 2a 2a 0a 2a 2a 20 5e 28 54 68 65  che}.**.** ^(The
4e290 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67   [sqlite3_config
4e2a0 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ]([SQLITE_CONFIG
4e2b0 5f 50 43 41 43 48 45 32 5d 2c 20 2e 2e 2e 29 20  _PCACHE2], ...) 
4e2c0 69 6e 74 65 72 66 61 63 65 20 63 61 6e 0a 2a 2a  interface can.**
4e2d0 20 72 65 67 69 73 74 65 72 20 61 6e 20 61 6c 74   register an alt
4e2e0 65 72 6e 61 74 69 76 65 20 70 61 67 65 20 63 61  ernative page ca
4e2f0 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  che implementati
4e300 6f 6e 20 62 79 20 70 61 73 73 69 6e 67 20 69 6e  on by passing in
4e310 20 61 6e 20 0a 2a 2a 20 69 6e 73 74 61 6e 63 65   an .** instance
4e320 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
4e330 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 32 20  pcache_methods2 
4e340 73 74 72 75 63 74 75 72 65 2e 29 5e 0a 2a 2a 20  structure.)^.** 
4e350 49 6e 20 6d 61 6e 79 20 61 70 70 6c 69 63 61 74  In many applicat
4e360 69 6f 6e 73 2c 20 6d 6f 73 74 20 6f 66 20 74 68  ions, most of th
4e370 65 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 61 6c  e heap memory al
4e380 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a 20 53  located by .** S
4e390 51 4c 69 74 65 20 69 73 20 75 73 65 64 20 66 6f  QLite is used fo
4e3a0 72 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  r the page cache
4e3b0 2e 0a 2a 2a 20 42 79 20 69 6d 70 6c 65 6d 65 6e  ..** By implemen
4e3c0 74 69 6e 67 20 61 20 0a 2a 2a 20 63 75 73 74 6f  ting a .** custo
4e3d0 6d 20 70 61 67 65 20 63 61 63 68 65 20 75 73 69  m page cache usi
4e3e0 6e 67 20 74 68 69 73 20 41 50 49 2c 20 61 6e 20  ng this API, an 
4e3f0 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20  application can 
4e400 62 65 74 74 65 72 20 63 6f 6e 74 72 6f 6c 0a 2a  better control.*
4e410 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  * the amount of 
4e420 6d 65 6d 6f 72 79 20 63 6f 6e 73 75 6d 65 64 20  memory consumed 
4e430 62 79 20 53 51 4c 69 74 65 2c 20 74 68 65 20 77  by SQLite, the w
4e440 61 79 20 69 6e 20 77 68 69 63 68 20 0a 2a 2a 20  ay in which .** 
4e450 74 68 61 74 20 6d 65 6d 6f 72 79 20 69 73 20 61  that memory is a
4e460 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 72 65 6c  llocated and rel
4e470 65 61 73 65 64 2c 20 61 6e 64 20 74 68 65 20 70  eased, and the p
4e480 6f 6c 69 63 69 65 73 20 75 73 65 64 20 74 6f 20  olicies used to 
4e490 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 65 78  .** determine ex
4e4a0 61 63 74 6c 79 20 77 68 69 63 68 20 70 61 72 74  actly which part
4e4b0 73 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20  s of a database 
4e4c0 66 69 6c 65 20 61 72 65 20 63 61 63 68 65 64 20  file are cached 
4e4d0 61 6e 64 20 66 6f 72 20 0a 2a 2a 20 68 6f 77 20  and for .** how 
4e4e0 6c 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  long..**.** The 
4e4f0 61 6c 74 65 72 6e 61 74 69 76 65 20 70 61 67 65  alternative page
4e500 20 63 61 63 68 65 20 6d 65 63 68 61 6e 69 73 6d   cache mechanism
4e510 20 69 73 20 61 6e 0a 2a 2a 20 65 78 74 72 65 6d   is an.** extrem
4e520 65 20 6d 65 61 73 75 72 65 20 74 68 61 74 20 69  e measure that i
4e530 73 20 6f 6e 6c 79 20 6e 65 65 64 65 64 20 62 79  s only needed by
4e540 20 74 68 65 20 6d 6f 73 74 20 64 65 6d 61 6e 64   the most demand
4e550 69 6e 67 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  ing applications
4e560 2e 0a 2a 2a 20 54 68 65 20 62 75 69 6c 74 2d 69  ..** The built-i
4e570 6e 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20  n page cache is 
4e580 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20  recommended for 
4e590 6d 6f 73 74 20 75 73 65 73 2e 0a 2a 2a 0a 2a 2a  most uses..**.**
4e5a0 20 5e 28 54 68 65 20 63 6f 6e 74 65 6e 74 73 20   ^(The contents 
4e5b0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70  of the sqlite3_p
4e5c0 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 32 20 73  cache_methods2 s
4e5d0 74 72 75 63 74 75 72 65 20 61 72 65 20 63 6f 70  tructure are cop
4e5e0 69 65 64 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 74  ied to an.** int
4e5f0 65 72 6e 61 6c 20 62 75 66 66 65 72 20 62 79 20  ernal buffer by 
4e600 53 51 4c 69 74 65 20 77 69 74 68 69 6e 20 74 68  SQLite within th
4e610 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  e call to [sqlit
4e620 65 33 5f 63 6f 6e 66 69 67 5d 2e 20 20 48 65 6e  e3_config].  Hen
4e630 63 65 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63  ce.** the applic
4e640 61 74 69 6f 6e 20 6d 61 79 20 64 69 73 63 61 72  ation may discar
4e650 64 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  d the parameter 
4e660 61 66 74 65 72 20 74 68 65 20 63 61 6c 6c 20 74  after the call t
4e670 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f  o.** [sqlite3_co
4e680 6e 66 69 67 28 29 5d 20 72 65 74 75 72 6e 73 2e  nfig()] returns.
4e690 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 74 68 65 20 78  )^.**.** [[the x
4e6a0 49 6e 69 74 28 29 20 70 61 67 65 20 63 61 63 68  Init() page cach
4e6b0 65 20 6d 65 74 68 6f 64 5d 5d 0a 2a 2a 20 5e 28  e method]].** ^(
4e6c0 54 68 65 20 78 49 6e 69 74 28 29 20 6d 65 74 68  The xInit() meth
4e6d0 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63  od is called onc
4e6e0 65 20 66 6f 72 20 65 61 63 68 20 65 66 66 65 63  e for each effec
4e6f0 74 69 76 65 20 0a 2a 2a 20 63 61 6c 6c 20 74 6f  tive .** call to
4e700 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61   [sqlite3_initia
4e710 6c 69 7a 65 28 29 5d 29 5e 0a 2a 2a 20 28 75 73  lize()])^.** (us
4e720 75 61 6c 6c 79 20 6f 6e 6c 79 20 6f 6e 63 65 20  ually only once 
4e730 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74  during the lifet
4e740 69 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 63 65  ime of the proce
4e750 73 73 29 2e 20 5e 28 54 68 65 20 78 49 6e 69 74  ss). ^(The xInit
4e760 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 69 73 20  ().** method is 
4e770 70 61 73 73 65 64 20 61 20 63 6f 70 79 20 6f 66  passed a copy of
4e780 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
4e790 63 68 65 5f 6d 65 74 68 6f 64 73 32 2e 70 41 72  che_methods2.pAr
4e7a0 67 20 76 61 6c 75 65 2e 29 5e 0a 2a 2a 20 54 68  g value.)^.** Th
4e7b0 65 20 69 6e 74 65 6e 74 20 6f 66 20 74 68 65 20  e intent of the 
4e7c0 78 49 6e 69 74 28 29 20 6d 65 74 68 6f 64 20 69  xInit() method i
4e7d0 73 20 74 6f 20 73 65 74 20 75 70 20 67 6c 6f 62  s to set up glob
4e7e0 61 6c 20 64 61 74 61 20 73 74 72 75 63 74 75 72  al data structur
4e7f0 65 73 20 0a 2a 2a 20 72 65 71 75 69 72 65 64 20  es .** required 
4e800 62 79 20 74 68 65 20 63 75 73 74 6f 6d 20 70 61  by the custom pa
4e810 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65  ge cache impleme
4e820 6e 74 61 74 69 6f 6e 2e 20 0a 2a 2a 20 5e 28 49  ntation. .** ^(I
4e830 66 20 74 68 65 20 78 49 6e 69 74 28 29 20 6d 65  f the xInit() me
4e840 74 68 6f 64 20 69 73 20 4e 55 4c 4c 2c 20 74 68  thod is NULL, th
4e850 65 6e 20 74 68 65 20 0a 2a 2a 20 62 75 69 6c 74  en the .** built
4e860 2d 69 6e 20 64 65 66 61 75 6c 74 20 70 61 67 65  -in default page
4e870 20 63 61 63 68 65 20 69 73 20 75 73 65 64 20 69   cache is used i
4e880 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 61 70  nstead of the ap
4e890 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65  plication define
4e8a0 64 0a 2a 2a 20 70 61 67 65 20 63 61 63 68 65 2e  d.** page cache.
4e8b0 29 5e 0a 2a 2a 0a 2a 2a 20 5b 5b 74 68 65 20 78  )^.**.** [[the x
4e8c0 53 68 75 74 64 6f 77 6e 28 29 20 70 61 67 65 20  Shutdown() page 
4e8d0 63 61 63 68 65 20 6d 65 74 68 6f 64 5d 5d 0a 2a  cache method]].*
4e8e0 2a 20 5e 54 68 65 20 78 53 68 75 74 64 6f 77 6e  * ^The xShutdown
4e8f0 28 29 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c  () method is cal
4e900 6c 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  led by [sqlite3_
4e910 73 68 75 74 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 20  shutdown()]..** 
4e920 49 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  It can be used t
4e930 6f 20 63 6c 65 61 6e 20 75 70 20 0a 2a 2a 20 61  o clean up .** a
4e940 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72  ny outstanding r
4e950 65 73 6f 75 72 63 65 73 20 62 65 66 6f 72 65 20  esources before 
4e960 70 72 6f 63 65 73 73 20 73 68 75 74 64 6f 77 6e  process shutdown
4e970 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a  , if required..*
4e980 2a 20 5e 54 68 65 20 78 53 68 75 74 64 6f 77 6e  * ^The xShutdown
4e990 28 29 20 6d 65 74 68 6f 64 20 6d 61 79 20 62 65  () method may be
4e9a0 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 5e 53 51   NULL..**.** ^SQ
4e9b0 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  Lite automatical
4e9c0 6c 79 20 73 65 72 69 61 6c 69 7a 65 73 20 63 61  ly serializes ca
4e9d0 6c 6c 73 20 74 6f 20 74 68 65 20 78 49 6e 69 74  lls to the xInit
4e9e0 20 6d 65 74 68 6f 64 2c 0a 2a 2a 20 73 6f 20 74   method,.** so t
4e9f0 68 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20  he xInit method 
4ea00 6e 65 65 64 20 6e 6f 74 20 62 65 20 74 68 72 65  need not be thre
4ea10 61 64 73 61 66 65 2e 20 20 5e 54 68 65 0a 2a 2a  adsafe.  ^The.**
4ea20 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f   xShutdown metho
4ea30 64 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64  d is only called
4ea40 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 73   from [sqlite3_s
4ea50 68 75 74 64 6f 77 6e 28 29 5d 20 73 6f 20 69 74  hutdown()] so it
4ea60 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65   does.** not nee
4ea70 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 61  d to be threadsa
4ea80 66 65 20 65 69 74 68 65 72 2e 20 20 41 6c 6c 20  fe either.  All 
4ea90 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 20 6d 75  other methods mu
4eaa0 73 74 20 62 65 20 74 68 72 65 61 64 73 61 66 65  st be threadsafe
4eab0 0a 2a 2a 20 69 6e 20 6d 75 6c 74 69 74 68 72 65  .** in multithre
4eac0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e  aded application
4ead0 73 2e 0a 2a 2a 0a 2a 2a 20 5e 53 51 4c 69 74 65  s..**.** ^SQLite
4eae0 20 77 69 6c 6c 20 6e 65 76 65 72 20 69 6e 76 6f   will never invo
4eaf0 6b 65 20 78 49 6e 69 74 28 29 20 6d 6f 72 65 20  ke xInit() more 
4eb00 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 6f 75  than once withou
4eb10 74 20 61 6e 20 69 6e 74 65 72 76 65 6e 69 6e 67  t an intervening
4eb20 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 53 68 75  .** call to xShu
4eb30 74 64 6f 77 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b  tdown()..**.** [
4eb40 5b 74 68 65 20 78 43 72 65 61 74 65 28 29 20 70  [the xCreate() p
4eb50 61 67 65 20 63 61 63 68 65 20 6d 65 74 68 6f 64  age cache method
4eb60 73 5d 5d 0a 2a 2a 20 5e 53 51 4c 69 74 65 20 69  s]].** ^SQLite i
4eb70 6e 76 6f 6b 65 73 20 74 68 65 20 78 43 72 65 61  nvokes the xCrea
4eb80 74 65 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 63  te() method to c
4eb90 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20 63  onstruct a new c
4eba0 61 63 68 65 20 69 6e 73 74 61 6e 63 65 2e 0a 2a  ache instance..*
4ebb0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 74 79  * SQLite will ty
4ebc0 70 69 63 61 6c 6c 79 20 63 72 65 61 74 65 20 6f  pically create o
4ebd0 6e 65 20 63 61 63 68 65 20 69 6e 73 74 61 6e 63  ne cache instanc
4ebe0 65 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20  e for each open 
4ebf0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a  database file,.*
4ec00 2a 20 74 68 6f 75 67 68 20 74 68 69 73 20 69 73  * though this is
4ec10 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 2e   not guaranteed.
4ec20 20 5e 54 68 65 0a 2a 2a 20 66 69 72 73 74 20 70   ^The.** first p
4ec30 61 72 61 6d 65 74 65 72 2c 20 73 7a 50 61 67 65  arameter, szPage
4ec40 2c 20 69 73 20 74 68 65 20 73 69 7a 65 20 69 6e  , is the size in
4ec50 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 61   bytes of the pa
4ec60 67 65 73 20 74 68 61 74 20 6d 75 73 74 0a 2a 2a  ges that must.**
4ec70 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79   be allocated by
4ec80 20 74 68 65 20 63 61 63 68 65 2e 20 20 5e 73 7a   the cache.  ^sz
4ec90 50 61 67 65 20 77 69 6c 6c 20 61 6c 77 61 79 73  Page will always
4eca0 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e   a power of two.
4ecb0 20 20 5e 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64    ^The.** second
4ecc0 20 70 61 72 61 6d 65 74 65 72 20 73 7a 45 78 74   parameter szExt
4ecd0 72 61 20 69 73 20 61 20 6e 75 6d 62 65 72 20 6f  ra is a number o
4ece0 66 20 62 79 74 65 73 20 6f 66 20 65 78 74 72 61  f bytes of extra
4ecf0 20 73 74 6f 72 61 67 65 20 0a 2a 2a 20 61 73 73   storage .** ass
4ed00 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63  ociated with eac
4ed10 68 20 70 61 67 65 20 63 61 63 68 65 20 65 6e 74  h page cache ent
4ed20 72 79 2e 20 20 5e 54 68 65 20 73 7a 45 78 74 72  ry.  ^The szExtr
4ed30 61 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c 6c  a parameter will
4ed40 0a 2a 2a 20 61 20 6e 75 6d 62 65 72 20 6c 65 73  .** a number les
4ed50 73 20 74 68 61 6e 20 32 35 30 2e 20 20 53 51 4c  s than 250.  SQL
4ed60 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65  ite will use the
4ed70 0a 2a 2a 20 65 78 74 72 61 20 73 7a 45 78 74 72  .** extra szExtr
4ed80 61 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20  a bytes on each 
4ed90 70 61 67 65 20 74 6f 20 73 74 6f 72 65 20 6d 65  page to store me
4eda0 74 61 64 61 74 61 20 61 62 6f 75 74 20 74 68 65  tadata about the
4edb0 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 64   underlying.** d
4edc0 61 74 61 62 61 73 65 20 70 61 67 65 20 6f 6e 20  atabase page on 
4edd0 64 69 73 6b 2e 20 20 54 68 65 20 76 61 6c 75 65  disk.  The value
4ede0 20 70 61 73 73 65 64 20 69 6e 74 6f 20 73 7a 45   passed into szE
4edf0 78 74 72 61 20 64 65 70 65 6e 64 73 0a 2a 2a 20  xtra depends.** 
4ee00 6f 6e 20 74 68 65 20 53 51 4c 69 74 65 20 76 65  on the SQLite ve
4ee10 72 73 69 6f 6e 2c 20 74 68 65 20 74 61 72 67 65  rsion, the targe
4ee20 74 20 70 6c 61 74 66 6f 72 6d 2c 20 61 6e 64 20  t platform, and 
4ee30 68 6f 77 20 53 51 4c 69 74 65 20 77 61 73 20 63  how SQLite was c
4ee40 6f 6d 70 69 6c 65 64 2e 0a 2a 2a 20 5e 54 68 65  ompiled..** ^The
4ee50 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20   third argument 
4ee60 74 6f 20 78 43 72 65 61 74 65 28 29 2c 20 62 50  to xCreate(), bP
4ee70 75 72 67 65 61 62 6c 65 2c 20 69 73 20 74 72 75  urgeable, is tru
4ee80 65 20 69 66 20 74 68 65 20 63 61 63 68 65 20 62  e if the cache b
4ee90 65 69 6e 67 0a 2a 2a 20 63 72 65 61 74 65 64 20  eing.** created 
4eea0 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20  will be used to 
4eeb0 63 61 63 68 65 20 64 61 74 61 62 61 73 65 20 70  cache database p
4eec0 61 67 65 73 20 6f 66 20 61 20 66 69 6c 65 20 73  ages of a file s
4eed0 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 2c 20 6f  tored on disk, o
4eee0 72 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74  r.** false if it
4eef0 20 69 73 20 75 73 65 64 20 66 6f 72 20 61 6e 20   is used for an 
4ef00 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
4ef10 73 65 2e 20 54 68 65 20 63 61 63 68 65 20 69 6d  se. The cache im
4ef20 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20  plementation.** 
4ef30 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f  does not have to
4ef40 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 73 70 65   do anything spe
4ef50 63 69 61 6c 20 62 61 73 65 64 20 77 69 74 68 20  cial based with 
4ef60 74 68 65 20 76 61 6c 75 65 20 6f 66 20 62 50 75  the value of bPu
4ef70 72 67 65 61 62 6c 65 3b 0a 2a 2a 20 69 74 20 69  rgeable;.** it i
4ef80 73 20 70 75 72 65 6c 79 20 61 64 76 69 73 6f 72  s purely advisor
4ef90 79 2e 20 20 5e 4f 6e 20 61 20 63 61 63 68 65 20  y.  ^On a cache 
4efa0 77 68 65 72 65 20 62 50 75 72 67 65 61 62 6c 65  where bPurgeable
4efb0 20 69 73 20 66 61 6c 73 65 2c 20 53 51 4c 69 74   is false, SQLit
4efc0 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20  e will.** never 
4efd0 69 6e 76 6f 6b 65 20 78 55 6e 70 69 6e 28 29 20  invoke xUnpin() 
4efe0 65 78 63 65 70 74 20 74 6f 20 64 65 6c 69 62 65  except to delibe
4eff0 72 61 74 65 6c 79 20 64 65 6c 65 74 65 20 61 20  rately delete a 
4f000 70 61 67 65 2e 0a 2a 2a 20 5e 49 6e 20 6f 74 68  page..** ^In oth
4f010 65 72 20 77 6f 72 64 73 2c 20 63 61 6c 6c 73 20  er words, calls 
4f020 74 6f 20 78 55 6e 70 69 6e 28 29 20 6f 6e 20 61  to xUnpin() on a
4f030 20 63 61 63 68 65 20 77 69 74 68 20 62 50 75 72   cache with bPur
4f040 67 65 61 62 6c 65 20 73 65 74 20 74 6f 0a 2a 2a  geable set to.**
4f050 20 66 61 6c 73 65 20 77 69 6c 6c 20 61 6c 77 61   false will alwa
4f060 79 73 20 68 61 76 65 20 74 68 65 20 22 64 69 73  ys have the "dis
4f070 63 61 72 64 22 20 66 6c 61 67 20 73 65 74 20 74  card" flag set t
4f080 6f 20 74 72 75 65 2e 20 20 0a 2a 2a 20 5e 48 65  o true.  .** ^He
4f090 6e 63 65 2c 20 61 20 63 61 63 68 65 20 63 72 65  nce, a cache cre
4f0a0 61 74 65 64 20 77 69 74 68 20 62 50 75 72 67 65  ated with bPurge
4f0b0 61 62 6c 65 20 66 61 6c 73 65 20 77 69 6c 6c 0a  able false will.
4f0c0 2a 2a 20 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e  ** never contain
4f0d0 20 61 6e 79 20 75 6e 70 69 6e 6e 65 64 20 70 61   any unpinned pa
4f0e0 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 74 68 65  ges..**.** [[the
4f0f0 20 78 43 61 63 68 65 73 69 7a 65 28 29 20 70 61   xCachesize() pa
4f100 67 65 20 63 61 63 68 65 20 6d 65 74 68 6f 64 5d  ge cache method]
4f110 5d 0a 2a 2a 20 5e 28 54 68 65 20 78 43 61 63 68  ].** ^(The xCach
4f120 65 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 6d  esize() method m
4f130 61 79 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20  ay be called at 
4f140 61 6e 79 20 74 69 6d 65 20 62 79 20 53 51 4c 69  any time by SQLi
4f150 74 65 20 74 6f 20 73 65 74 20 74 68 65 0a 2a 2a  te to set the.**
4f160 20 73 75 67 67 65 73 74 65 64 20 6d 61 78 69 6d   suggested maxim
4f170 75 6d 20 63 61 63 68 65 2d 73 69 7a 65 20 28 6e  um cache-size (n
4f180 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73  umber of pages s
4f190 74 6f 72 65 64 20 62 79 29 20 74 68 65 20 63 61  tored by) the ca
4f1a0 63 68 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20  che.** instance 
4f1b0 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
4f1c0 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68  rst argument. Th
4f1d0 69 73 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  is is the value 
4f1e0 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67  configured using
4f1f0 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 22  .** the SQLite "
4f200 5b 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73 69  [PRAGMA cache_si
4f210 7a 65 5d 22 20 63 6f 6d 6d 61 6e 64 2e 29 5e 20  ze]" command.)^ 
4f220 20 41 73 20 77 69 74 68 20 74 68 65 20 62 50 75   As with the bPu
4f230 72 67 65 61 62 6c 65 0a 2a 2a 20 70 61 72 61 6d  rgeable.** param
4f240 65 74 65 72 2c 20 74 68 65 20 69 6d 70 6c 65 6d  eter, the implem
4f250 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20  entation is not 
4f260 72 65 71 75 69 72 65 64 20 74 6f 20 64 6f 20 61  required to do a
4f270 6e 79 74 68 69 6e 67 20 77 69 74 68 20 74 68 69  nything with thi
4f280 73 0a 2a 2a 20 76 61 6c 75 65 3b 20 69 74 20 69  s.** value; it i
4f290 73 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e  s advisory only.
4f2a0 0a 2a 2a 0a 2a 2a 20 5b 5b 74 68 65 20 78 50 61  .**.** [[the xPa
4f2b0 67 65 63 6f 75 6e 74 28 29 20 70 61 67 65 20 63  gecount() page c
4f2c0 61 63 68 65 20 6d 65 74 68 6f 64 73 5d 5d 0a 2a  ache methods]].*
4f2d0 2a 20 54 68 65 20 78 50 61 67 65 63 6f 75 6e 74  * The xPagecount
4f2e0 28 29 20 6d 65 74 68 6f 64 20 6d 75 73 74 20 72  () method must r
4f2f0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
4f300 20 6f 66 20 70 61 67 65 73 20 63 75 72 72 65 6e   of pages curren
4f310 74 6c 79 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e  tly.** stored in
4f320 20 74 68 65 20 63 61 63 68 65 2c 20 62 6f 74 68   the cache, both
4f330 20 70 69 6e 6e 65 64 20 61 6e 64 20 75 6e 70 69   pinned and unpi
4f340 6e 6e 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 5b 5b 74  nned..** .** [[t
4f350 68 65 20 78 46 65 74 63 68 28 29 20 70 61 67 65  he xFetch() page
4f360 20 63 61 63 68 65 20 6d 65 74 68 6f 64 73 5d 5d   cache methods]]
4f370 0a 2a 2a 20 54 68 65 20 78 46 65 74 63 68 28 29  .** The xFetch()
4f380 20 6d 65 74 68 6f 64 20 6c 6f 63 61 74 65 73 20   method locates 
4f390 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61  a page in the ca
4f3a0 63 68 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20  che and returns 
4f3b0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 0a 2a 2a  a pointer to .**
4f3c0 20 61 6e 20 73 71 6c 69 74 65 33 5f 70 63 61 63   an sqlite3_pcac
4f3d0 68 65 5f 70 61 67 65 20 6f 62 6a 65 63 74 20 61  he_page object a
4f3e0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
4f3f0 68 61 74 20 70 61 67 65 2c 20 6f 72 20 61 20 4e  hat page, or a N
4f400 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20  ULL pointer..** 
4f410 54 68 65 20 70 42 75 66 20 65 6c 65 6d 65 6e 74  The pBuf element
4f420 20 6f 66 20 74 68 65 20 72 65 74 75 72 6e 65 64   of the returned
4f430 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f   sqlite3_pcache_
4f440 70 61 67 65 20 6f 62 6a 65 63 74 20 77 69 6c 6c  page object will
4f450 20 62 65 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72   be a.** pointer
4f460 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20   to a buffer of 
4f470 73 7a 50 61 67 65 20 62 79 74 65 73 20 75 73 65  szPage bytes use
4f480 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63  d to store the c
4f490 6f 6e 74 65 6e 74 20 6f 66 20 61 20 0a 2a 2a 20  ontent of a .** 
4f4a0 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20  single database 
4f4b0 70 61 67 65 2e 20 20 54 68 65 20 70 45 78 74 72  page.  The pExtr
4f4c0 61 20 65 6c 65 6d 65 6e 74 20 6f 66 20 73 71 6c  a element of sql
4f4d0 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67 65  ite3_pcache_page
4f4e0 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 61 20 70 6f   will be.** a po
4f4f0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 7a 45  inter to the szE
4f500 78 74 72 61 20 62 79 74 65 73 20 6f 66 20 65 78  xtra bytes of ex
4f510 74 72 61 20 73 74 6f 72 61 67 65 20 74 68 61 74  tra storage that
4f520 20 53 51 4c 69 74 65 20 68 61 73 20 72 65 71 75   SQLite has requ
4f530 65 73 74 65 64 0a 2a 2a 20 66 6f 72 20 65 61 63  ested.** for eac
4f540 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70  h entry in the p
4f550 61 67 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a  age cache..**.**
4f560 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20   The page to be 
4f570 66 65 74 63 68 65 64 20 69 73 20 64 65 74 65 72  fetched is deter
4f580 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6b 65 79  mined by the key
4f590 2e 20 5e 54 68 65 20 6d 69 6e 69 6d 75 6d 20 6b  . ^The minimum k
4f5a0 65 79 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 31  ey value.** is 1
4f5b0 2e 20 20 41 66 74 65 72 20 69 74 20 68 61 73 20  .  After it has 
4f5c0 62 65 65 6e 20 72 65 74 72 69 65 76 65 64 20 75  been retrieved u
4f5d0 73 69 6e 67 20 78 46 65 74 63 68 2c 20 74 68 65  sing xFetch, the
4f5e0 20 70 61 67 65 20 69 73 20 63 6f 6e 73 69 64 65   page is conside
4f5f0 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20 22 70 69  red.** to be "pi
4f600 6e 6e 65 64 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  nned"..**.** If 
4f610 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61  the requested pa
4f620 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  ge is already in
4f630 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2c   the page cache,
4f640 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 63   then the page c
4f650 61 63 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  ache.** implemen
4f660 74 61 74 69 6f 6e 20 6d 75 73 74 20 72 65 74 75  tation must retu
4f670 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
4f680 74 68 65 20 70 61 67 65 20 62 75 66 66 65 72 20  the page buffer 
4f690 77 69 74 68 20 69 74 73 20 63 6f 6e 74 65 6e 74  with its content
4f6a0 0a 2a 2a 20 69 6e 74 61 63 74 2e 20 20 49 66 20  .** intact.  If 
4f6b0 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61  the requested pa
4f6c0 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64  ge is not alread
4f6d0 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20  y in the cache, 
4f6e0 74 68 65 6e 20 74 68 65 0a 2a 2a 20 63 61 63 68  then the.** cach
4f6f0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
4f700 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20   should use the 
4f710 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65  value of the cre
4f720 61 74 65 46 6c 61 67 0a 2a 2a 20 70 61 72 61 6d  ateFlag.** param
4f730 65 74 65 72 20 74 6f 20 68 65 6c 70 20 69 74 20  eter to help it 
4f740 64 65 74 65 72 6d 69 6e 65 64 20 77 68 61 74 20  determined what 
4f750 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 3a 0a  action to take:.
4f760 2a 2a 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72  **.** <table bor
4f770 64 65 72 3d 31 20 77 69 64 74 68 3d 38 35 25 20  der=1 width=85% 
4f780 61 6c 69 67 6e 3d 63 65 6e 74 65 72 3e 0a 2a 2a  align=center>.**
4f790 20 3c 74 72 3e 3c 74 68 3e 20 63 72 65 61 74 65   <tr><th> create
4f7a0 46 6c 61 67 20 3c 74 68 3e 20 42 65 68 61 76 69  Flag <th> Behavi
4f7b0 6f 75 72 20 77 68 65 6e 20 70 61 67 65 20 69 73  our when page is
4f7c0 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20   not already in 
4f7d0 63 61 63 68 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64  cache.** <tr><td
4f7e0 3e 20 30 20 3c 74 64 3e 20 44 6f 20 6e 6f 74 20  > 0 <td> Do not 
4f7f0 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  allocate a new p
4f800 61 67 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c  age.  Return NUL
4f810 4c 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 31  L..** <tr><td> 1
4f820 20 3c 74 64 3e 20 41 6c 6c 6f 63 61 74 65 20 61   <td> Allocate a
4f830 20 6e 65 77 20 70 61 67 65 20 69 66 20 69 74 20   new page if it 
4f840 65 61 73 79 20 61 6e 64 20 63 6f 6e 76 65 6e 69  easy and conveni
4f850 65 6e 74 20 74 6f 20 64 6f 20 73 6f 2e 0a 2a 2a  ent to do so..**
4f860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f870 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
4f880 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 3c 74 72 3e 3c  n NULL..** <tr><
4f890 74 64 3e 20 32 20 3c 74 64 3e 20 4d 61 6b 65 20  td> 2 <td> Make 
4f8a0 65 76 65 72 79 20 65 66 66 6f 72 74 20 74 6f 20  every effort to 
4f8b0 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  allocate a new p
4f8c0 61 67 65 2e 20 20 4f 6e 6c 79 20 72 65 74 75 72  age.  Only retur
4f8d0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  n.**            
4f8e0 20 20 20 20 20 4e 55 4c 4c 20 69 66 20 61 6c 6c       NULL if all
4f8f0 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 70 61  ocating a new pa
4f900 67 65 20 69 73 20 65 66 66 65 63 74 69 76 65 6c  ge is effectivel
4f910 79 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a  y impossible..**
4f920 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20   </table>.**.** 
4f930 5e 28 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f  ^(SQLite will no
4f940 72 6d 61 6c 6c 79 20 69 6e 76 6f 6b 65 20 78 46  rmally invoke xF
4f950 65 74 63 68 28 29 20 77 69 74 68 20 61 20 63 72  etch() with a cr
4f960 65 61 74 65 46 6c 61 67 20 6f 66 20 30 20 6f 72  eateFlag of 0 or
4f970 20 31 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77   1.  SQLite.** w
4f980 69 6c 6c 20 6f 6e 6c 79 20 75 73 65 20 61 20 63  ill only use a c
4f990 72 65 61 74 65 46 6c 61 67 20 6f 66 20 32 20 61  reateFlag of 2 a
4f9a0 66 74 65 72 20 61 20 70 72 69 6f 72 20 63 61 6c  fter a prior cal
4f9b0 6c 20 77 69 74 68 20 61 20 63 72 65 61 74 65 46  l with a createF
4f9c0 6c 61 67 20 6f 66 20 31 0a 2a 2a 20 66 61 69 6c  lag of 1.** fail
4f9d0 65 64 2e 29 5e 20 20 49 6e 20 62 65 74 77 65 65  ed.)^  In betwee
4f9e0 6e 20 74 68 65 20 74 6f 20 78 46 65 74 63 68 28  n the to xFetch(
4f9f0 29 20 63 61 6c 6c 73 2c 20 53 51 4c 69 74 65 20  ) calls, SQLite 
4fa00 6d 61 79 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74  may.** attempt t
4fa10 6f 20 75 6e 70 69 6e 20 6f 6e 65 20 6f 72 20 6d  o unpin one or m
4fa20 6f 72 65 20 63 61 63 68 65 20 70 61 67 65 73 20  ore cache pages 
4fa30 62 79 20 73 70 69 6c 6c 69 6e 67 20 74 68 65 20  by spilling the 
4fa40 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 70 69  content of.** pi
4fa50 6e 6e 65 64 20 70 61 67 65 73 20 74 6f 20 64 69  nned pages to di
4fa60 73 6b 20 61 6e 64 20 73 79 6e 63 68 69 6e 67 20  sk and synching 
4fa70 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
4fa80 73 74 65 6d 20 64 69 73 6b 20 63 61 63 68 65 2e  stem disk cache.
4fa90 0a 2a 2a 0a 2a 2a 20 5b 5b 74 68 65 20 78 55 6e  .**.** [[the xUn
4faa0 70 69 6e 28 29 20 70 61 67 65 20 63 61 63 68 65  pin() page cache
4fab0 20 6d 65 74 68 6f 64 5d 5d 0a 2a 2a 20 5e 78 55   method]].** ^xU
4fac0 6e 70 69 6e 28 29 20 69 73 20 63 61 6c 6c 65 64  npin() is called
4fad0 20 62 79 20 53 51 4c 69 74 65 20 77 69 74 68 20   by SQLite with 
4fae0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 63  a pointer to a c
4faf0 75 72 72 65 6e 74 6c 79 20 70 69 6e 6e 65 64 20  urrently pinned 
4fb00 70 61 67 65 0a 2a 2a 20 61 73 20 69 74 73 20 73  page.** as its s
4fb10 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20  econd argument. 
4fb20 20 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61   If the third pa
4fb30 72 61 6d 65 74 65 72 2c 20 64 69 73 63 61 72 64  rameter, discard
4fb40 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a  , is non-zero,.*
4fb50 2a 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20  * then the page 
4fb60 6d 75 73 74 20 62 65 20 65 76 69 63 74 65 64 20  must be evicted 
4fb70 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 2e 0a  from the cache..
4fb80 2a 2a 20 5e 49 66 20 74 68 65 20 64 69 73 63 61  ** ^If the disca
4fb90 72 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a  rd parameter is.
4fba0 2a 2a 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  ** zero, then th
4fbb0 65 20 70 61 67 65 20 6d 61 79 20 62 65 20 64 69  e page may be di
4fbc0 73 63 61 72 64 65 64 20 6f 72 20 72 65 74 61 69  scarded or retai
4fbd0 6e 65 64 20 61 74 20 74 68 65 20 64 69 73 63 72  ned at the discr
4fbe0 65 74 69 6f 6e 20 6f 66 0a 2a 2a 20 70 61 67 65  etion of.** page
4fbf0 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74   cache implement
4fc00 61 74 69 6f 6e 2e 20 5e 54 68 65 20 70 61 67 65  ation. ^The page
4fc10 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74   cache implement
4fc20 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79 20 63 68 6f  ation.** may cho
4fc30 6f 73 65 20 74 6f 20 65 76 69 63 74 20 75 6e 70  ose to evict unp
4fc40 69 6e 6e 65 64 20 70 61 67 65 73 20 61 74 20 61  inned pages at a
4fc50 6e 79 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54  ny time..**.** T
4fc60 68 65 20 63 61 63 68 65 20 6d 75 73 74 20 6e 6f  he cache must no
4fc70 74 20 70 65 72 66 6f 72 6d 20 61 6e 79 20 72 65  t perform any re
4fc80 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67  ference counting
4fc90 2e 20 41 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 63  . A single .** c
4fca0 61 6c 6c 20 74 6f 20 78 55 6e 70 69 6e 28 29 20  all to xUnpin() 
4fcb0 75 6e 70 69 6e 73 20 74 68 65 20 70 61 67 65 20  unpins the page 
4fcc0 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68  regardless of th
4fcd0 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f  e number of prio
4fce0 72 20 63 61 6c 6c 73 20 0a 2a 2a 20 74 6f 20 78  r calls .** to x
4fcf0 46 65 74 63 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b  Fetch()..**.** [
4fd00 5b 74 68 65 20 78 52 65 6b 65 79 28 29 20 70 61  [the xRekey() pa
4fd10 67 65 20 63 61 63 68 65 20 6d 65 74 68 6f 64 73  ge cache methods
4fd20 5d 5d 0a 2a 2a 20 54 68 65 20 78 52 65 6b 65 79  ]].** The xRekey
4fd30 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75 73 65  () method is use
4fd40 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20  d to change the 
4fd50 6b 65 79 20 76 61 6c 75 65 20 61 73 73 6f 63 69  key value associ
4fd60 61 74 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a  ated with the.**
4fd70 20 70 61 67 65 20 70 61 73 73 65 64 20 61 73 20   page passed as 
4fd80 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
4fd90 65 6e 74 2e 20 49 66 20 74 68 65 20 63 61 63 68  ent. If the cach
4fda0 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  e.** previously 
4fdb0 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74 72  contains an entr
4fdc0 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  y associated wit
4fdd0 68 20 6e 65 77 4b 65 79 2c 20 69 74 20 6d 75 73  h newKey, it mus
4fde0 74 20 62 65 0a 2a 2a 20 64 69 73 63 61 72 64 65  t be.** discarde
4fdf0 64 2e 20 5e 41 6e 79 20 70 72 69 6f 72 20 63 61  d. ^Any prior ca
4fe00 63 68 65 20 65 6e 74 72 79 20 61 73 73 6f 63 69  che entry associ
4fe10 61 74 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79  ated with newKey
4fe20 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e   is guaranteed n
4fe30 6f 74 0a 2a 2a 20 74 6f 20 62 65 20 70 69 6e 6e  ot.** to be pinn
4fe40 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53  ed..**.** When S
4fe50 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 65 20  QLite calls the 
4fe60 78 54 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68  xTruncate() meth
4fe70 6f 64 2c 20 74 68 65 20 63 61 63 68 65 20 6d 75  od, the cache mu
4fe80 73 74 20 64 69 73 63 61 72 64 20 61 6c 6c 0a 2a  st discard all.*
4fe90 2a 20 65 78 69 73 74 69 6e 67 20 63 61 63 68 65  * existing cache
4fea0 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 70 61   entries with pa
4feb0 67 65 20 6e 75 6d 62 65 72 73 20 28 6b 65 79 73  ge numbers (keys
4fec0 29 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  ) greater than o
4fed0 72 20 65 71 75 61 6c 0a 2a 2a 20 74 6f 20 74 68  r equal.** to th
4fee0 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69  e value of the i
4fef0 4c 69 6d 69 74 20 70 61 72 61 6d 65 74 65 72 20  Limit parameter 
4ff00 70 61 73 73 65 64 20 74 6f 20 78 54 72 75 6e 63  passed to xTrunc
4ff10 61 74 65 28 29 2e 20 49 66 20 61 6e 79 0a 2a 2a  ate(). If any.**
4ff20 20 6f 66 20 74 68 65 73 65 20 70 61 67 65 73 20   of these pages 
4ff30 61 72 65 20 70 69 6e 6e 65 64 2c 20 74 68 65 79  are pinned, they
4ff40 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20   are implicitly 
4ff50 75 6e 70 69 6e 6e 65 64 2c 20 6d 65 61 6e 69 6e  unpinned, meanin
4ff60 67 20 74 68 61 74 0a 2a 2a 20 74 68 65 79 20 63  g that.** they c
4ff70 61 6e 20 62 65 20 73 61 66 65 6c 79 20 64 69 73  an be safely dis
4ff80 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 5b 5b  carded..**.** [[
4ff90 74 68 65 20 78 44 65 73 74 72 6f 79 28 29 20 70  the xDestroy() p
4ffa0 61 67 65 20 63 61 63 68 65 20 6d 65 74 68 6f 64  age cache method
4ffb0 5d 5d 0a 2a 2a 20 5e 54 68 65 20 78 44 65 73 74  ]].** ^The xDest
4ffc0 72 6f 79 28 29 20 6d 65 74 68 6f 64 20 69 73 20  roy() method is 
4ffd0 75 73 65 64 20 74 6f 20 64 65 6c 65 74 65 20 61  used to delete a
4ffe0 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65 64   cache allocated
4fff0 20 62 79 20 78 43 72 65 61 74 65 28 29 2e 0a 2a   by xCreate()..*
50000 2a 20 41 6c 6c 20 72 65 73 6f 75 72 63 65 73 20  * All resources 
50010 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
50020 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 61  the specified ca
50030 63 68 65 20 73 68 6f 75 6c 64 20 62 65 20 66 72  che should be fr
50040 65 65 64 2e 20 5e 41 66 74 65 72 0a 2a 2a 20 63  eed. ^After.** c
50050 61 6c 6c 69 6e 67 20 74 68 65 20 78 44 65 73 74  alling the xDest
50060 72 6f 79 28 29 20 6d 65 74 68 6f 64 2c 20 53 51  roy() method, SQ
50070 4c 69 74 65 20 63 6f 6e 73 69 64 65 72 73 20 74  Lite considers t
50080 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63  he [sqlite3_pcac
50090 68 65 2a 5d 0a 2a 2a 20 68 61 6e 64 6c 65 20 69  he*].** handle i
500a0 6e 76 61 6c 69 64 2c 20 61 6e 64 20 77 69 6c 6c  nvalid, and will
500b0 20 6e 6f 74 20 75 73 65 20 69 74 20 77 69 74 68   not use it with
500c0 20 61 6e 79 20 6f 74 68 65 72 20 73 71 6c 69 74   any other sqlit
500d0 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
500e0 73 32 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e  s2.** functions.
500f0 0a 2a 2a 0a 2a 2a 20 5b 5b 74 68 65 20 78 53 68  .**.** [[the xSh
50100 72 69 6e 6b 28 29 20 70 61 67 65 20 63 61 63 68  rink() page cach
50110 65 20 6d 65 74 68 6f 64 5d 5d 0a 2a 2a 20 5e 53  e method]].** ^S
50120 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68  QLite invokes th
50130 65 20 78 53 68 72 69 6e 6b 28 29 20 6d 65 74 68  e xShrink() meth
50140 6f 64 20 77 68 65 6e 20 69 74 20 77 61 6e 74 73  od when it wants
50150 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20   the page cache 
50160 74 6f 0a 2a 2a 20 66 72 65 65 20 75 70 20 61 73  to.** free up as
50170 20 6d 75 63 68 20 6f 66 20 68 65 61 70 20 6d 65   much of heap me
50180 6d 6f 72 79 20 61 73 20 70 6f 73 73 69 62 6c 65  mory as possible
50190 2e 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68  .  The page cach
501a0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
501b0 0a 2a 2a 20 69 73 20 6e 6f 74 20 6f 62 6c 69 67  .** is not oblig
501c0 61 74 65 64 20 74 6f 20 66 72 65 65 20 61 6e 79  ated to free any
501d0 20 6d 65 6d 6f 72 79 2c 20 62 75 74 20 77 65 6c   memory, but wel
501e0 6c 2d 62 65 68 61 76 65 64 20 69 6d 70 6c 65 6d  l-behaved implem
501f0 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64  entations should
50200 0a 2a 2a 20 64 6f 20 74 68 65 69 72 20 62 65 73  .** do their bes
50210 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  t..*/.typedef st
50220 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 61  ruct sqlite3_pca
50230 63 68 65 5f 6d 65 74 68 6f 64 73 32 20 73 71 6c  che_methods2 sql
50240 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
50250 6f 64 73 32 3b 0a 73 74 72 75 63 74 20 73 71 6c  ods2;.struct sql
50260 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
50270 6f 64 73 32 20 7b 0a 20 20 69 6e 74 20 69 56 65  ods2 {.  int iVe
50280 72 73 69 6f 6e 3b 0a 20 20 76 6f 69 64 20 2a 70  rsion;.  void *p
50290 41 72 67 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e  Arg;.  int (*xIn
502a0 69 74 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f  it)(void*);.  vo
502b0 69 64 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28  id (*xShutdown)(
502c0 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65  void*);.  sqlite
502d0 33 5f 70 63 61 63 68 65 20 2a 28 2a 78 43 72 65  3_pcache *(*xCre
502e0 61 74 65 29 28 69 6e 74 20 73 7a 50 61 67 65 2c  ate)(int szPage,
502f0 20 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 69 6e   int szExtra, in
50300 74 20 62 50 75 72 67 65 61 62 6c 65 29 3b 0a 20  t bPurgeable);. 
50310 20 76 6f 69 64 20 28 2a 78 43 61 63 68 65 73 69   void (*xCachesi
50320 7a 65 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63  ze)(sqlite3_pcac
50330 68 65 2a 2c 20 69 6e 74 20 6e 43 61 63 68 65 73  he*, int nCaches
50340 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 50  ize);.  int (*xP
50350 61 67 65 63 6f 75 6e 74 29 28 73 71 6c 69 74 65  agecount)(sqlite
50360 33 5f 70 63 61 63 68 65 2a 29 3b 0a 20 20 73 71  3_pcache*);.  sq
50370 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67  lite3_pcache_pag
50380 65 20 2a 28 2a 78 46 65 74 63 68 29 28 73 71 6c  e *(*xFetch)(sql
50390 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e  ite3_pcache*, un
503a0 73 69 67 6e 65 64 20 6b 65 79 2c 20 69 6e 74 20  signed key, int 
503b0 63 72 65 61 74 65 46 6c 61 67 29 3b 0a 20 20 76  createFlag);.  v
503c0 6f 69 64 20 28 2a 78 55 6e 70 69 6e 29 28 73 71  oid (*xUnpin)(sq
503d0 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 73  lite3_pcache*, s
503e0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61  qlite3_pcache_pa
503f0 67 65 2a 2c 20 69 6e 74 20 64 69 73 63 61 72 64  ge*, int discard
50400 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 6b  );.  void (*xRek
50410 65 79 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63  ey)(sqlite3_pcac
50420 68 65 2a 2c 20 73 71 6c 69 74 65 33 5f 70 63 61  he*, sqlite3_pca
50430 63 68 65 5f 70 61 67 65 2a 2c 20 0a 20 20 20 20  che_page*, .    
50440 20 20 75 6e 73 69 67 6e 65 64 20 6f 6c 64 4b 65    unsigned oldKe
50450 79 2c 20 75 6e 73 69 67 6e 65 64 20 6e 65 77 4b  y, unsigned newK
50460 65 79 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 54  ey);.  void (*xT
50470 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33  runcate)(sqlite3
50480 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e  _pcache*, unsign
50490 65 64 20 69 4c 69 6d 69 74 29 3b 0a 20 20 76 6f  ed iLimit);.  vo
504a0 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 73  id (*xDestroy)(s
504b0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 29 3b  qlite3_pcache*);
504c0 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 72 69 6e  .  void (*xShrin
504d0 6b 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68  k)(sqlite3_pcach
504e0 65 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  e*);.};../*.** T
504f0 68 69 73 20 69 73 20 74 68 65 20 6f 62 73 6f 6c  his is the obsol
50500 65 74 65 20 70 63 61 63 68 65 5f 6d 65 74 68 6f  ete pcache_metho
50510 64 73 20 6f 62 6a 65 63 74 20 74 68 61 74 20 68  ds object that h
50520 61 73 20 6e 6f 77 20 62 65 65 6e 20 72 65 70 6c  as now been repl
50530 61 63 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74  aced.** by sqlit
50540 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
50550 73 32 2e 20 20 54 68 69 73 20 6f 62 6a 65 63 74  s2.  This object
50560 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 20   is not used by 
50570 53 51 4c 69 74 65 2e 20 20 49 74 20 69 73 0a 2a  SQLite.  It is.*
50580 2a 20 72 65 74 61 69 6e 65 64 20 69 6e 20 74 68  * retained in th
50590 65 20 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f  e header file fo
505a0 72 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  r backwards comp
505b0 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a  atibility only..
505c0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
505d0 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  t sqlite3_pcache
505e0 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33  _methods sqlite3
505f0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3b  _pcache_methods;
50600 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
50610 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 7b  pcache_methods {
50620 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20  .  void *pArg;. 
50630 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 76 6f   int (*xInit)(vo
50640 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  id*);.  void (*x
50650 53 68 75 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29  Shutdown)(void*)
50660 3b 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63  ;.  sqlite3_pcac
50670 68 65 20 2a 28 2a 78 43 72 65 61 74 65 29 28 69  he *(*xCreate)(i
50680 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62  nt szPage, int b
50690 50 75 72 67 65 61 62 6c 65 29 3b 0a 20 20 76 6f  Purgeable);.  vo
506a0 69 64 20 28 2a 78 43 61 63 68 65 73 69 7a 65 29  id (*xCachesize)
506b0 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
506c0 2c 20 69 6e 74 20 6e 43 61 63 68 65 73 69 7a 65  , int nCachesize
506d0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 50 61 67 65  );.  int (*xPage
506e0 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 70  count)(sqlite3_p
506f0 63 61 63 68 65 2a 29 3b 0a 20 20 76 6f 69 64 20  cache*);.  void 
50700 2a 28 2a 78 46 65 74 63 68 29 28 73 71 6c 69 74  *(*xFetch)(sqlit
50710 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69  e3_pcache*, unsi
50720 67 6e 65 64 20 6b 65 79 2c 20 69 6e 74 20 63 72  gned key, int cr
50730 65 61 74 65 46 6c 61 67 29 3b 0a 20 20 76 6f 69  eateFlag);.  voi
50740 64 20 28 2a 78 55 6e 70 69 6e 29 28 73 71 6c 69  d (*xUnpin)(sqli
50750 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69  te3_pcache*, voi
50760 64 2a 2c 20 69 6e 74 20 64 69 73 63 61 72 64 29  d*, int discard)
50770 3b 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 6b 65  ;.  void (*xReke
50780 79 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68  y)(sqlite3_pcach
50790 65 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67  e*, void*, unsig
507a0 6e 65 64 20 6f 6c 64 4b 65 79 2c 20 75 6e 73 69  ned oldKey, unsi
507b0 67 6e 65 64 20 6e 65 77 4b 65 79 29 3b 0a 20 20  gned newKey);.  
507c0 76 6f 69 64 20 28 2a 78 54 72 75 6e 63 61 74 65  void (*xTruncate
507d0 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  )(sqlite3_pcache
507e0 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69 4c 69 6d  *, unsigned iLim
507f0 69 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44  it);.  void (*xD
50800 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f  estroy)(sqlite3_
50810 70 63 61 63 68 65 2a 29 3b 0a 7d 3b 0a 0a 0a 2f  pcache*);.};.../
50820 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  *.** CAPI3REF: O
50830 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 4f 62 6a  nline Backup Obj
50840 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ect.**.** The sq
50850 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a  lite3_backup obj
50860 65 63 74 20 72 65 63 6f 72 64 73 20 73 74 61 74  ect records stat
50870 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  e information ab
50880 6f 75 74 20 61 6e 20 6f 6e 67 6f 69 6e 67 0a 2a  out an ongoing.*
50890 2a 20 6f 6e 6c 69 6e 65 20 62 61 63 6b 75 70 20  * online backup 
508a0 6f 70 65 72 61 74 69 6f 6e 2e 20 20 5e 54 68 65  operation.  ^The
508b0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
508c0 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74 65  object is create
508d0 64 20 62 79 0a 2a 2a 20 61 20 63 61 6c 6c 20 74  d by.** a call t
508e0 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  o [sqlite3_backu
508f0 70 5f 69 6e 69 74 28 29 5d 20 61 6e 64 20 69 73  p_init()] and is
50900 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 61 20   destroyed by a 
50910 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69  call to.** [sqli
50920 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73  te3_backup_finis
50930 68 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  h()]..**.** See 
50940 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65  Also: [Using the
50950 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 42   SQLite Online B
50960 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2f 0a 74 79  ackup API].*/.ty
50970 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
50980 69 74 65 33 5f 62 61 63 6b 75 70 20 73 71 6c 69  ite3_backup sqli
50990 74 65 33 5f 62 61 63 6b 75 70 3b 0a 0a 2f 2a 0a  te3_backup;../*.
509a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 6e 6c  ** CAPI3REF: Onl
509b0 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49 2e 0a  ine Backup API..
509c0 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 63 6b 75 70  **.** The backup
509d0 20 41 50 49 20 63 6f 70 69 65 73 20 74 68 65 20   API copies the 
509e0 63 6f 6e 74 65 6e 74 20 6f 66 20 6f 6e 65 20 64  content of one d
509f0 61 74 61 62 61 73 65 20 69 6e 74 6f 20 61 6e 6f  atabase into ano
50a00 74 68 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 75  ther..** It is u
50a10 73 65 66 75 6c 20 65 69 74 68 65 72 20 66 6f 72  seful either for
50a20 20 63 72 65 61 74 69 6e 67 20 62 61 63 6b 75 70   creating backup
50a30 73 20 6f 66 20 64 61 74 61 62 61 73 65 73 20 6f  s of databases o
50a40 72 0a 2a 2a 20 66 6f 72 20 63 6f 70 79 69 6e 67  r.** for copying
50a50 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
50a60 61 73 65 73 20 74 6f 20 6f 72 20 66 72 6f 6d 20  ases to or from 
50a70 70 65 72 73 69 73 74 65 6e 74 20 66 69 6c 65 73  persistent files
50a80 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73  . .**.** See Als
50a90 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 51  o: [Using the SQ
50aa0 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 42 61 63 6b  Lite Online Back
50ab0 75 70 20 41 50 49 5d 0a 2a 2a 0a 2a 2a 20 5e 53  up API].**.** ^S
50ac0 51 4c 69 74 65 20 68 6f 6c 64 73 20 61 20 77 72  QLite holds a wr
50ad0 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite transaction 
50ae0 6f 70 65 6e 20 6f 6e 20 74 68 65 20 64 65 73 74  open on the dest
50af0 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
50b00 20 66 69 6c 65 0a 2a 2a 20 66 6f 72 20 74 68 65   file.** for the
50b10 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 65   duration of the
50b20 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f   backup operatio
50b30 6e 2e 0a 2a 2a 20 5e 54 68 65 20 73 6f 75 72 63  n..** ^The sourc
50b40 65 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65  e database is re
50b50 61 64 2d 6c 6f 63 6b 65 64 20 6f 6e 6c 79 20 77  ad-locked only w
50b60 68 69 6c 65 20 69 74 20 69 73 20 62 65 69 6e 67  hile it is being
50b70 20 72 65 61 64 3b 0a 2a 2a 20 69 74 20 69 73 20   read;.** it is 
50b80 6e 6f 74 20 6c 6f 63 6b 65 64 20 63 6f 6e 74 69  not locked conti
50b90 6e 75 6f 75 73 6c 79 20 66 6f 72 20 74 68 65 20  nuously for the 
50ba0 65 6e 74 69 72 65 20 62 61 63 6b 75 70 20 6f 70  entire backup op
50bb0 65 72 61 74 69 6f 6e 2e 0a 2a 2a 20 5e 54 68 75  eration..** ^Thu
50bc0 73 2c 20 74 68 65 20 62 61 63 6b 75 70 20 6d 61  s, the backup ma
50bd0 79 20 62 65 20 70 65 72 66 6f 72 6d 65 64 20 6f  y be performed o
50be0 6e 20 61 20 6c 69 76 65 20 73 6f 75 72 63 65 20  n a live source 
50bf0 64 61 74 61 62 61 73 65 20 77 69 74 68 6f 75 74  database without
50c00 0a 2a 2a 20 70 72 65 76 65 6e 74 69 6e 67 20 6f  .** preventing o
50c10 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f  ther database co
50c20 6e 6e 65 63 74 69 6f 6e 73 20 66 72 6f 6d 0a 2a  nnections from.*
50c30 2a 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69  * reading or wri
50c40 74 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72  ting to the sour
50c50 63 65 20 64 61 74 61 62 61 73 65 20 77 68 69 6c  ce database whil
50c60 65 20 74 68 65 20 62 61 63 6b 75 70 20 69 73 20  e the backup is 
50c70 75 6e 64 65 72 77 61 79 2e 0a 2a 2a 20 0a 2a 2a  underway..** .**
50c80 20 5e 28 54 6f 20 70 65 72 66 6f 72 6d 20 61 20   ^(To perform a 
50c90 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
50ca0 3a 20 0a 2a 2a 20 20 20 3c 6f 6c 3e 0a 2a 2a 20  : .**   <ol>.** 
50cb0 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74      <li><b>sqlit
50cc0 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29  e3_backup_init()
50cd0 3c 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64 20 6f  </b> is called o
50ce0 6e 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a  nce to initializ
50cf0 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  e the.**        
50d00 20 62 61 63 6b 75 70 2c 20 0a 2a 2a 20 20 20 20   backup, .**    
50d10 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f   <li><b>sqlite3_
50d20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62  backup_step()</b
50d30 3e 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 65 20  > is called one 
50d40 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f  or more times to
50d50 20 74 72 61 6e 73 66 65 72 20 0a 2a 2a 20 20 20   transfer .**   
50d60 20 20 20 20 20 20 74 68 65 20 64 61 74 61 20 62        the data b
50d70 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20 64  etween the two d
50d80 61 74 61 62 61 73 65 73 2c 20 61 6e 64 20 66 69  atabases, and fi
50d90 6e 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 3c 6c 69  nally.**     <li
50da0 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b  ><b>sqlite3_back
50db0 75 70 5f 66 69 6e 69 73 68 28 29 3c 2f 62 3e 20  up_finish()</b> 
50dc0 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 6c  is called to rel
50dd0 65 61 73 65 20 61 6c 6c 20 72 65 73 6f 75 72 63  ease all resourc
50de0 65 73 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 61  es .**         a
50df0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
50e00 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74  he backup operat
50e10 69 6f 6e 2e 20 0a 2a 2a 20 20 20 3c 2f 6f 6c 3e  ion. .**   </ol>
50e20 29 5e 0a 2a 2a 20 54 68 65 72 65 20 73 68 6f 75  )^.** There shou
50e30 6c 64 20 62 65 20 65 78 61 63 74 6c 79 20 6f 6e  ld be exactly on
50e40 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  e call to sqlite
50e50 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
50e60 29 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 73 75  ) for each.** su
50e70 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
50e80 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
50e90 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b 5b  init()..**.** [[
50ea0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69  sqlite3_backup_i
50eb0 6e 69 74 28 29 5d 5d 20 3c 62 3e 73 71 6c 69 74  nit()]] <b>sqlit
50ec0 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29  e3_backup_init()
50ed0 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  </b>.**.** ^The 
50ee0 44 20 61 6e 64 20 4e 20 61 72 67 75 6d 65 6e 74  D and N argument
50ef0 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  s to sqlite3_bac
50f00 6b 75 70 5f 69 6e 69 74 28 44 2c 4e 2c 53 2c 4d  kup_init(D,N,S,M
50f10 29 20 61 72 65 20 74 68 65 20 0a 2a 2a 20 5b 64  ) are the .** [d
50f20 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
50f30 6f 6e 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77  on] associated w
50f40 69 74 68 20 74 68 65 20 64 65 73 74 69 6e 61 74  ith the destinat
50f50 69 6f 6e 20 64 61 74 61 62 61 73 65 20 0a 2a 2a  ion database .**
50f60 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73   and the databas
50f70 65 20 6e 61 6d 65 2c 20 72 65 73 70 65 63 74 69  e name, respecti
50f80 76 65 6c 79 2e 0a 2a 2a 20 5e 54 68 65 20 64 61  vely..** ^The da
50f90 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22  tabase name is "
50fa0 6d 61 69 6e 22 20 66 6f 72 20 74 68 65 20 6d 61  main" for the ma
50fb0 69 6e 20 64 61 74 61 62 61 73 65 2c 20 22 74 65  in database, "te
50fc0 6d 70 22 20 66 6f 72 20 74 68 65 0a 2a 2a 20 74  mp" for the.** t
50fd0 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
50fe0 65 2c 20 6f 72 20 74 68 65 20 6e 61 6d 65 20 73  e, or the name s
50ff0 70 65 63 69 66 69 65 64 20 61 66 74 65 72 20 74  pecified after t
51000 68 65 20 41 53 20 6b 65 79 77 6f 72 64 20 69 6e  he AS keyword in
51010 0a 2a 2a 20 61 6e 20 5b 41 54 54 41 43 48 5d 20  .** an [ATTACH] 
51020 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 61 6e  statement for an
51030 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
51040 73 65 2e 0a 2a 2a 20 5e 54 68 65 20 53 20 61 6e  se..** ^The S an
51050 64 20 4d 20 61 72 67 75 6d 65 6e 74 73 20 70 61  d M arguments pa
51060 73 73 65 64 20 74 6f 20 0a 2a 2a 20 73 71 6c 69  ssed to .** sqli
51070 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28  te3_backup_init(
51080 44 2c 4e 2c 53 2c 4d 29 20 69 64 65 6e 74 69 66  D,N,S,M) identif
51090 79 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  y the [database 
510a0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 61  connection].** a
510b0 6e 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  nd database name
510c0 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 20 64   of the source d
510d0 61 74 61 62 61 73 65 2c 20 72 65 73 70 65 63 74  atabase, respect
510e0 69 76 65 6c 79 2e 0a 2a 2a 20 5e 54 68 65 20 73  ively..** ^The s
510f0 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e  ource and destin
51100 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 20  ation [database 
51110 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 28 70 61  connections] (pa
51120 72 61 6d 65 74 65 72 73 20 53 20 61 6e 64 20 44  rameters S and D
51130 29 0a 2a 2a 20 6d 75 73 74 20 62 65 20 64 69 66  ).** must be dif
51140 66 65 72 65 6e 74 20 6f 72 20 65 6c 73 65 20 73  ferent or else s
51150 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e  qlite3_backup_in
51160 69 74 28 44 2c 4e 2c 53 2c 4d 29 20 77 69 6c 6c  it(D,N,S,M) will
51170 20 66 61 69 6c 20 77 69 74 68 0a 2a 2a 20 61 6e   fail with.** an
51180 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 5e 49   error..**.** ^I
51190 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
511a0 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 33  s within sqlite3
511b0 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 44 2c 4e  _backup_init(D,N
511c0 2c 53 2c 4d 29 2c 20 74 68 65 6e 20 4e 55 4c 4c  ,S,M), then NULL
511d0 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20   is.** returned 
511e0 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  and an error cod
511f0 65 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73  e and error mess
51200 61 67 65 20 61 72 65 20 73 74 6f 72 65 64 20 69  age are stored i
51210 6e 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61  n the.** destina
51220 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63  tion [database c
51230 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a  onnection] D..**
51240 20 5e 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65   ^The error code
51250 20 61 6e 64 20 6d 65 73 73 61 67 65 20 66 6f 72   and message for
51260 20 74 68 65 20 66 61 69 6c 65 64 20 63 61 6c 6c   the failed call
51270 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
51280 75 70 5f 69 6e 69 74 28 29 0a 2a 2a 20 63 61 6e  up_init().** can
51290 20 62 65 20 72 65 74 72 69 65 76 65 64 20 75 73   be retrieved us
512a0 69 6e 67 20 74 68 65 20 5b 73 71 6c 69 74 65 33  ing the [sqlite3
512b0 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 5b 73 71  _errcode()], [sq
512c0 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c  lite3_errmsg()],
512d0 20 61 6e 64 2f 6f 72 0a 2a 2a 20 5b 73 71 6c 69   and/or.** [sqli
512e0 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 20  te3_errmsg16()] 
512f0 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 5e 41  functions..** ^A
51300 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c   successful call
51310 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
51320 75 70 5f 69 6e 69 74 28 29 20 72 65 74 75 72 6e  up_init() return
51330 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
51340 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 61  n.** [sqlite3_ba
51350 63 6b 75 70 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a  ckup] object..**
51360 20 5e 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62   ^The [sqlite3_b
51370 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 6d 61  ackup] object ma
51380 79 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74  y be used with t
51390 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  he sqlite3_backu
513a0 70 5f 73 74 65 70 28 29 20 61 6e 64 0a 2a 2a 20  p_step() and.** 
513b0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66  sqlite3_backup_f
513c0 69 6e 69 73 68 28 29 20 66 75 6e 63 74 69 6f 6e  inish() function
513d0 73 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 65  s to perform the
513e0 20 73 70 65 63 69 66 69 65 64 20 62 61 63 6b 75   specified backu
513f0 70 20 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e  p .** operation.
51400 0a 2a 2a 0a 2a 2a 20 5b 5b 73 71 6c 69 74 65 33  .**.** [[sqlite3
51410 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 5d 5d  _backup_step()]]
51420 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b   <b>sqlite3_back
51430 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 0a 2a 2a  up_step()</b>.**
51440 0a 2a 2a 20 5e 46 75 6e 63 74 69 6f 6e 20 73 71  .** ^Function sq
51450 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
51460 70 28 42 2c 4e 29 20 77 69 6c 6c 20 63 6f 70 79  p(B,N) will copy
51470 20 75 70 20 74 6f 20 4e 20 70 61 67 65 73 20 62   up to N pages b
51480 65 74 77 65 65 6e 20 0a 2a 2a 20 74 68 65 20 73  etween .** the s
51490 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e  ource and destin
514a0 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 73 20  ation databases 
514b0 73 70 65 63 69 66 69 65 64 20 62 79 20 5b 73 71  specified by [sq
514c0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62  lite3_backup] ob
514d0 6a 65 63 74 20 42 2e 0a 2a 2a 20 5e 49 66 20 4e  ject B..** ^If N
514e0 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 61 6c   is negative, al
514f0 6c 20 72 65 6d 61 69 6e 69 6e 67 20 73 6f 75 72  l remaining sour
51500 63 65 20 70 61 67 65 73 20 61 72 65 20 63 6f 70  ce pages are cop
51510 69 65 64 2e 20 0a 2a 2a 20 5e 49 66 20 73 71 6c  ied. .** ^If sql
51520 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
51530 28 42 2c 4e 29 20 73 75 63 63 65 73 73 66 75 6c  (B,N) successful
51540 6c 79 20 63 6f 70 69 65 73 20 4e 20 70 61 67 65  ly copies N page
51550 73 20 61 6e 64 20 74 68 65 72 65 0a 2a 2a 20 61  s and there.** a
51560 72 65 20 73 74 69 6c 6c 20 6d 6f 72 65 20 70 61  re still more pa
51570 67 65 73 20 74 6f 20 62 65 20 63 6f 70 69 65 64  ges to be copied
51580 2c 20 74 68 65 6e 20 74 68 65 20 66 75 6e 63 74  , then the funct
51590 69 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  ion returns [SQL
515a0 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 20 5e 49 66 20  ITE_OK]..** ^If 
515b0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
515c0 74 65 70 28 42 2c 4e 29 20 73 75 63 63 65 73 73  tep(B,N) success
515d0 66 75 6c 6c 79 20 66 69 6e 69 73 68 65 73 20 63  fully finishes c
515e0 6f 70 79 69 6e 67 20 61 6c 6c 20 70 61 67 65 73  opying all pages
515f0 0a 2a 2a 20 66 72 6f 6d 20 73 6f 75 72 63 65 20  .** from source 
51600 74 6f 20 64 65 73 74 69 6e 61 74 69 6f 6e 2c 20  to destination, 
51610 74 68 65 6e 20 69 74 20 72 65 74 75 72 6e 73 20  then it returns 
51620 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 0a 2a  [SQLITE_DONE]..*
51630 2a 20 5e 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  * ^If an error o
51640 63 63 75 72 73 20 77 68 69 6c 65 20 72 75 6e 6e  ccurs while runn
51650 69 6e 67 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  ing sqlite3_back
51660 75 70 5f 73 74 65 70 28 42 2c 4e 29 2c 0a 2a 2a  up_step(B,N),.**
51670 20 74 68 65 6e 20 61 6e 20 5b 65 72 72 6f 72 20   then an [error 
51680 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65  code] is returne
51690 64 2e 20 5e 41 73 20 77 65 6c 6c 20 61 73 20 5b  d. ^As well as [
516a0 53 51 4c 49 54 45 5f 4f 4b 5d 20 61 6e 64 0a 2a  SQLITE_OK] and.*
516b0 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c  * [SQLITE_DONE],
516c0 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
516d0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
516e0 20 6d 61 79 20 72 65 74 75 72 6e 20 5b 53 51 4c   may return [SQL
516f0 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 2c 0a 2a  ITE_READONLY],.*
51700 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d  * [SQLITE_NOMEM]
51710 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c  , [SQLITE_BUSY],
51720 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d   [SQLITE_LOCKED]
51730 2c 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49  , or an.** [SQLI
51740 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20  TE_IOERR_ACCESS 
51750 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58  | SQLITE_IOERR_X
51760 58 58 5d 20 65 78 74 65 6e 64 65 64 20 65 72 72  XX] extended err
51770 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 5e  or code..**.** ^
51780 28 54 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63  (The sqlite3_bac
51790 6b 75 70 5f 73 74 65 70 28 29 20 6d 69 67 68 74  kup_step() might
517a0 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
517b0 52 45 41 44 4f 4e 4c 59 5d 20 69 66 0a 2a 2a 20  READONLY] if.** 
517c0 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 74 68 65  <ol>.** <li> the
517d0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
517e0 61 62 61 73 65 20 77 61 73 20 6f 70 65 6e 65 64  abase was opened
517f0 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 6f 72 0a 2a   read-only, or.*
51800 2a 20 3c 6c 69 3e 20 74 68 65 20 64 65 73 74 69  * <li> the desti
51810 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
51820 69 73 20 75 73 69 6e 67 20 77 72 69 74 65 2d 61  is using write-a
51830 68 65 61 64 2d 6c 6f 67 20 6a 6f 75 72 6e 61 6c  head-log journal
51840 69 6e 67 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64  ing.** and the d
51850 65 73 74 69 6e 61 74 69 6f 6e 20 61 6e 64 20 73  estination and s
51860 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a 65 73  ource page sizes
51870 20 64 69 66 66 65 72 2c 20 6f 72 0a 2a 2a 20 3c   differ, or.** <
51880 6c 69 3e 20 74 68 65 20 64 65 73 74 69 6e 61 74  li> the destinat
51890 69 6f 6e 20 64 61 74 61 62 61 73 65 20 69 73 20  ion database is 
518a0 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  an in-memory dat
518b0 61 62 61 73 65 20 61 6e 64 20 74 68 65 0a 2a 2a  abase and the.**
518c0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 61 6e 64   destination and
518d0 20 73 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a   source page siz
518e0 65 73 20 64 69 66 66 65 72 2e 0a 2a 2a 20 3c 2f  es differ..** </
518f0 6f 6c 3e 29 5e 0a 2a 2a 0a 2a 2a 20 5e 49 66 20  ol>)^.**.** ^If 
51900 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
51910 74 65 70 28 29 20 63 61 6e 6e 6f 74 20 6f 62 74  tep() cannot obt
51920 61 69 6e 20 61 20 72 65 71 75 69 72 65 64 20 66  ain a required f
51930 69 6c 65 2d 73 79 73 74 65 6d 20 6c 6f 63 6b 2c  ile-system lock,
51940 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 73 71   then.** the [sq
51950 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c  lite3_busy_handl
51960 65 72 20 7c 20 62 75 73 79 2d 68 61 6e 64 6c 65  er | busy-handle
51970 72 20 66 75 6e 63 74 69 6f 6e 5d 0a 2a 2a 20 69  r function].** i
51980 73 20 69 6e 76 6f 6b 65 64 20 28 69 66 20 6f 6e  s invoked (if on
51990 65 20 69 73 20 73 70 65 63 69 66 69 65 64 29 2e  e is specified).
519a0 20 5e 49 66 20 74 68 65 20 0a 2a 2a 20 62 75 73   ^If the .** bus
519b0 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e  y-handler return
519c0 73 20 6e 6f 6e 2d 7a 65 72 6f 20 62 65 66 6f 72  s non-zero befor
519d0 65 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 76  e the lock is av
519e0 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 0a 2a  ailable, then .*
519f0 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  * [SQLITE_BUSY] 
51a00 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  is returned to t
51a10 68 65 20 63 61 6c 6c 65 72 2e 20 5e 49 6e 20 74  he caller. ^In t
51a20 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c  his case the cal
51a30 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  l to.** sqlite3_
51a40 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61  backup_step() ca
51a50 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74  n be retried lat
51a60 65 72 2e 20 5e 49 66 20 74 68 65 20 73 6f 75 72  er. ^If the sour
51a70 63 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20  ce.** [database 
51a80 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 69  connection].** i
51a90 73 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20  s being used to 
51aa0 77 72 69 74 65 20 74 6f 20 74 68 65 20 73 6f 75  write to the sou
51ab0 72 63 65 20 64 61 74 61 62 61 73 65 20 77 68 65  rce database whe
51ac0 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  n sqlite3_backup
51ad0 5f 73 74 65 70 28 29 0a 2a 2a 20 69 73 20 63 61  _step().** is ca
51ae0 6c 6c 65 64 2c 20 74 68 65 6e 20 5b 53 51 4c 49  lled, then [SQLI
51af0 54 45 5f 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65  TE_LOCKED] is re
51b00 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65  turned immediate
51b10 6c 79 2e 20 5e 41 67 61 69 6e 2c 20 69 6e 20 74  ly. ^Again, in t
51b20 68 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 65 20  his.** case the 
51b30 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
51b40 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61  backup_step() ca
51b50 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74  n be retried lat
51b60 65 72 20 6f 6e 2e 20 5e 28 49 66 0a 2a 2a 20 5b  er on. ^(If.** [
51b70 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43  SQLITE_IOERR_ACC
51b80 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45  ESS | SQLITE_IOE
51b90 52 52 5f 58 58 58 5d 2c 20 5b 53 51 4c 49 54 45  RR_XXX], [SQLITE
51ba0 5f 4e 4f 4d 45 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b  _NOMEM], or.** [
51bb0 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d  SQLITE_READONLY]
51bc0 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68   is returned, th
51bd0 65 6e 20 0a 2a 2a 20 74 68 65 72 65 20 69 73 20  en .** there is 
51be0 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 72 65 74 72  no point in retr
51bf0 79 69 6e 67 20 74 68 65 20 63 61 6c 6c 20 74 6f  ying the call to
51c00 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
51c10 73 74 65 70 28 29 2e 20 54 68 65 73 65 20 0a 2a  step(). These .*
51c20 2a 20 65 72 72 6f 72 73 20 61 72 65 20 63 6f 6e  * errors are con
51c30 73 69 64 65 72 65 64 20 66 61 74 61 6c 2e 29 5e  sidered fatal.)^
51c40 20 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f    The applicatio
51c50 6e 20 6d 75 73 74 20 61 63 63 65 70 74 20 0a 2a  n must accept .*
51c60 2a 20 74 68 61 74 20 74 68 65 20 62 61 63 6b 75  * that the backu
51c70 70 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 73 20  p operation has 
51c80 66 61 69 6c 65 64 20 61 6e 64 20 70 61 73 73 20  failed and pass 
51c90 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61  the backup opera
51ca0 74 69 6f 6e 20 68 61 6e 64 6c 65 20 0a 2a 2a 20  tion handle .** 
51cb0 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62  to the sqlite3_b
51cc0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 74  ackup_finish() t
51cd0 6f 20 72 65 6c 65 61 73 65 20 61 73 73 6f 63 69  o release associ
51ce0 61 74 65 64 20 72 65 73 6f 75 72 63 65 73 2e 0a  ated resources..
51cf0 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 72 73 74  **.** ^The first
51d00 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
51d10 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6f  _backup_step() o
51d20 62 74 61 69 6e 73 20 61 6e 20 65 78 63 6c 75 73  btains an exclus
51d30 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 74  ive lock.** on t
51d40 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 66  he destination f
51d50 69 6c 65 2e 20 5e 54 68 65 20 65 78 63 6c 75 73  ile. ^The exclus
51d60 69 76 65 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20  ive lock is not 
51d70 72 65 6c 65 61 73 65 64 20 75 6e 74 69 6c 20 65  released until e
51d80 69 74 68 65 72 20 0a 2a 2a 20 73 71 6c 69 74 65  ither .** sqlite
51d90 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
51da0 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 72 20 74  ) is called or t
51db0 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74  he backup operat
51dc0 69 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 20  ion is complete 
51dd0 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  .** and sqlite3_
51de0 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 72 65  backup_step() re
51df0 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 4f  turns [SQLITE_DO
51e00 4e 45 5d 2e 20 20 5e 45 76 65 72 79 20 63 61 6c  NE].  ^Every cal
51e10 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  l to.** sqlite3_
51e20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6f 62  backup_step() ob
51e30 74 61 69 6e 73 20 61 20 5b 73 68 61 72 65 64 20  tains a [shared 
51e40 6c 6f 63 6b 5d 20 6f 6e 20 74 68 65 20 73 6f 75  lock] on the sou
51e50 72 63 65 20 64 61 74 61 62 61 73 65 20 74 68 61  rce database tha
51e60 74 0a 2a 2a 20 6c 61 73 74 73 20 66 6f 72 20 74  t.** lasts for t
51e70 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74  he duration of t
51e80 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  he sqlite3_backu
51e90 70 5f 73 74 65 70 28 29 20 63 61 6c 6c 2e 0a 2a  p_step() call..*
51ea0 2a 20 5e 42 65 63 61 75 73 65 20 74 68 65 20 73  * ^Because the s
51eb0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69  ource database i
51ec0 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 62 65 74  s not locked bet
51ed0 77 65 65 6e 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a  ween calls to.**
51ee0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
51ef0 73 74 65 70 28 29 2c 20 74 68 65 20 73 6f 75 72  step(), the sour
51f00 63 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20  ce database may 
51f10 62 65 20 6d 6f 64 69 66 69 65 64 20 6d 69 64 2d  be modified mid-
51f20 77 61 79 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74  way.** through t
51f30 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73  he backup proces
51f40 73 2e 20 20 5e 49 66 20 74 68 65 20 73 6f 75 72  s.  ^If the sour
51f50 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6d  ce database is m
51f60 6f 64 69 66 69 65 64 20 62 79 20 61 6e 0a 2a 2a  odified by an.**
51f70 20 65 78 74 65 72 6e 61 6c 20 70 72 6f 63 65 73   external proces
51f80 73 20 6f 72 20 76 69 61 20 61 20 64 61 74 61 62  s or via a datab
51f90 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f  ase connection o
51fa0 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e  ther than the on
51fb0 65 20 62 65 69 6e 67 0a 2a 2a 20 75 73 65 64 20  e being.** used 
51fc0 62 79 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70  by the backup op
51fd0 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68  eration, then th
51fe0 65 20 62 61 63 6b 75 70 20 77 69 6c 6c 20 62 65  e backup will be
51ff0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a   automatically.*
52000 2a 20 72 65 73 74 61 72 74 65 64 20 62 79 20 74  * restarted by t
52010 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20  he next call to 
52020 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
52030 74 65 70 28 29 2e 20 5e 49 66 20 74 68 65 20 73  tep(). ^If the s
52040 6f 75 72 63 65 20 0a 2a 2a 20 64 61 74 61 62 61  ource .** databa
52050 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62  se is modified b
52060 79 20 74 68 65 20 75 73 69 6e 67 20 74 68 65 20  y the using the 
52070 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
52080 6e 6e 65 63 74 69 6f 6e 20 61 73 20 69 73 20 75  nnection as is u
52090 73 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 62 61  sed.** by the ba
520a0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20  ckup operation, 
520b0 74 68 65 6e 20 74 68 65 20 62 61 63 6b 75 70 20  then the backup 
520c0 64 61 74 61 62 61 73 65 20 69 73 20 61 75 74 6f  database is auto
520d0 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 75 70 64  matically.** upd
520e0 61 74 65 64 20 61 74 20 74 68 65 20 73 61 6d 65  ated at the same
520f0 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 73   time..**.** [[s
52100 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
52110 6e 69 73 68 28 29 5d 5d 20 3c 62 3e 73 71 6c 69  nish()]] <b>sqli
52120 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73  te3_backup_finis
52130 68 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68  h()</b>.**.** Wh
52140 65 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  en sqlite3_backu
52150 70 5f 73 74 65 70 28 29 20 68 61 73 20 72 65 74  p_step() has ret
52160 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 44 4f  urned [SQLITE_DO
52170 4e 45 5d 2c 20 6f 72 20 77 68 65 6e 20 74 68 65  NE], or when the
52180 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e   .** application
52190 20 77 69 73 68 65 73 20 74 6f 20 61 62 61 6e 64   wishes to aband
521a0 6f 6e 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70  on the backup op
521b0 65 72 61 74 69 6f 6e 2c 20 74 68 65 20 61 70 70  eration, the app
521c0 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75  lication.** shou
521d0 6c 64 20 64 65 73 74 72 6f 79 20 74 68 65 20 5b  ld destroy the [
521e0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 20  sqlite3_backup] 
521f0 62 79 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f  by passing it to
52200 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
52210 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 20 5e 54 68  finish()..** ^Th
52220 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  e sqlite3_backup
52230 5f 66 69 6e 69 73 68 28 29 20 69 6e 74 65 72 66  _finish() interf
52240 61 63 65 73 20 72 65 6c 65 61 73 65 73 20 61 6c  aces releases al
52250 6c 0a 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 61  l.** resources a
52260 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
52270 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b  he [sqlite3_back
52280 75 70 5d 20 6f 62 6a 65 63 74 2e 20 0a 2a 2a 20  up] object. .** 
52290 5e 49 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  ^If sqlite3_back
522a0 75 70 5f 73 74 65 70 28 29 20 68 61 73 20 6e 6f  up_step() has no
522b0 74 20 79 65 74 20 72 65 74 75 72 6e 65 64 20 5b  t yet returned [
522c0 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 74 68  SQLITE_DONE], th
522d0 65 6e 20 61 6e 79 0a 2a 2a 20 61 63 74 69 76 65  en any.** active
522e0 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
522f0 6f 6e 20 6f 6e 20 74 68 65 20 64 65 73 74 69 6e  on on the destin
52300 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 69  ation database i
52310 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a  s rolled back..*
52320 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62  * The [sqlite3_b
52330 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73  ackup] object is
52340 20 69 6e 76 61 6c 69 64 0a 2a 2a 20 61 6e 64 20   invalid.** and 
52350 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20  may not be used 
52360 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c  following a call
52370 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
52380 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a  up_finish()..**.
52390 2a 2a 20 5e 54 68 65 20 76 61 6c 75 65 20 72 65  ** ^The value re
523a0 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
523b0 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 20  3_backup_finish 
523c0 69 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69  is [SQLITE_OK] i
523d0 66 20 6e 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  f no.** sqlite3_
523e0 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 65 72  backup_step() er
523f0 72 6f 72 73 20 6f 63 63 75 72 72 65 64 2c 20 72  rors occurred, r
52400 65 67 61 72 64 6c 65 73 73 20 6f 72 20 77 68 65  egardless or whe
52410 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 73  ther or not.** s
52420 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
52430 65 70 28 29 20 63 6f 6d 70 6c 65 74 65 64 2e 0a  ep() completed..
52440 2a 2a 20 5e 49 66 20 61 6e 20 6f 75 74 2d 6f 66  ** ^If an out-of
52450 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 69 74 69 6f  -memory conditio
52460 6e 20 6f 72 20 49 4f 20 65 72 72 6f 72 20 6f 63  n or IO error oc
52470 63 75 72 72 65 64 20 64 75 72 69 6e 67 20 61 6e  curred during an
52480 79 20 70 72 69 6f 72 0a 2a 2a 20 73 71 6c 69 74  y prior.** sqlit
52490 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
524a0 20 63 61 6c 6c 20 6f 6e 20 74 68 65 20 73 61 6d   call on the sam
524b0 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  e [sqlite3_backu
524c0 70 5d 20 6f 62 6a 65 63 74 2c 20 74 68 65 6e 0a  p] object, then.
524d0 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
524e0 70 5f 66 69 6e 69 73 68 28 29 20 72 65 74 75 72  p_finish() retur
524f0 6e 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  ns the correspon
52500 64 69 6e 67 20 5b 65 72 72 6f 72 20 63 6f 64 65  ding [error code
52510 5d 2e 0a 2a 2a 0a 2a 2a 20 5e 41 20 72 65 74 75  ]..**.** ^A retu
52520 72 6e 20 6f 66 20 5b 53 51 4c 49 54 45 5f 42 55  rn of [SQLITE_BU
52530 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c  SY] or [SQLITE_L
52540 4f 43 4b 45 44 5d 20 66 72 6f 6d 20 73 71 6c 69  OCKED] from sqli
52550 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
52560 29 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 20 70 65  ).** is not a pe
52570 72 6d 61 6e 65 6e 74 20 65 72 72 6f 72 20 61 6e  rmanent error an
52580 64 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 63  d does not affec
52590 74 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  t the return val
525a0 75 65 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33  ue of.** sqlite3
525b0 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29  _backup_finish()
525c0 2e 0a 2a 2a 0a 2a 2a 20 5b 5b 73 71 6c 69 74 65  ..**.** [[sqlite
525d0 33 5f 62 61 63 6b 75 70 5f 5f 72 65 6d 61 69 6e  3_backup__remain
525e0 69 6e 67 28 29 5d 5d 20 5b 5b 73 71 6c 69 74 65  ing()]] [[sqlite
525f0 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75  3_backup_pagecou
52600 6e 74 28 29 5d 5d 0a 2a 2a 20 3c 62 3e 73 71 6c  nt()]].** <b>sql
52610 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61  ite3_backup_rema
52620 69 6e 69 6e 67 28 29 20 61 6e 64 20 73 71 6c 69  ining() and sqli
52630 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63  te3_backup_pagec
52640 6f 75 6e 74 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a  ount()</b>.**.**
52650 20 5e 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 73   ^Each call to s
52660 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
52670 65 70 28 29 20 73 65 74 73 20 74 77 6f 20 76 61  ep() sets two va
52680 6c 75 65 73 20 69 6e 73 69 64 65 0a 2a 2a 20 74  lues inside.** t
52690 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b  he [sqlite3_back
526a0 75 70 5d 20 6f 62 6a 65 63 74 3a 20 74 68 65 20  up] object: the 
526b0 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
526c0 73 74 69 6c 6c 20 74 6f 20 62 65 20 62 61 63 6b  still to be back
526d0 65 64 0a 2a 2a 20 75 70 20 61 6e 64 20 74 68 65  ed.** up and the
526e0 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
526f0 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f   pages in the so
52700 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69  urce database fi
52710 6c 65 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  le..** The sqlit
52720 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e  e3_backup_remain
52730 69 6e 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ing() and sqlite
52740 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75  3_backup_pagecou
52750 6e 74 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a  nt() interfaces.
52760 2a 2a 20 72 65 74 72 69 65 76 65 20 74 68 65 73  ** retrieve thes
52770 65 20 74 77 6f 20 76 61 6c 75 65 73 2c 20 72 65  e two values, re
52780 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a  spectively..**.*
52790 2a 20 5e 54 68 65 20 76 61 6c 75 65 73 20 72 65  * ^The values re
527a0 74 75 72 6e 65 64 20 62 79 20 74 68 65 73 65 20  turned by these 
527b0 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6f 6e  functions are on
527c0 6c 79 20 75 70 64 61 74 65 64 20 62 79 0a 2a 2a  ly updated by.**
527d0 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
527e0 73 74 65 70 28 29 2e 20 5e 49 66 20 74 68 65 20  step(). ^If the 
527f0 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20  source database 
52800 69 73 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69  is modified duri
52810 6e 67 20 61 20 62 61 63 6b 75 70 0a 2a 2a 20 6f  ng a backup.** o
52820 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74  peration, then t
52830 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e 6f  he values are no
52840 74 20 75 70 64 61 74 65 64 20 74 6f 20 61 63 63  t updated to acc
52850 6f 75 6e 74 20 66 6f 72 20 61 6e 79 20 65 78 74  ount for any ext
52860 72 61 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 74  ra.** pages that
52870 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64 61   need to be upda
52880 74 65 64 20 6f 72 20 74 68 65 20 73 69 7a 65 20  ted or the size 
52890 6f 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 61  of the source da
528a0 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 63  tabase file.** c
528b0 68 61 6e 67 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 3c  hanging..**.** <
528c0 62 3e 43 6f 6e 63 75 72 72 65 6e 74 20 55 73 61  b>Concurrent Usa
528d0 67 65 20 6f 66 20 44 61 74 61 62 61 73 65 20 48  ge of Database H
528e0 61 6e 64 6c 65 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a  andles</b>.**.**
528f0 20 5e 54 68 65 20 73 6f 75 72 63 65 20 5b 64 61   ^The source [da
52900 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
52910 6e 5d 20 6d 61 79 20 62 65 20 75 73 65 64 20 62  n] may be used b
52920 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  y the applicatio
52930 6e 20 66 6f 72 20 6f 74 68 65 72 0a 2a 2a 20 70  n for other.** p
52940 75 72 70 6f 73 65 73 20 77 68 69 6c 65 20 61 20  urposes while a 
52950 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
52960 20 69 73 20 75 6e 64 65 72 77 61 79 20 6f 72 20   is underway or 
52970 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65  being initialize
52980 64 2e 0a 2a 2a 20 5e 49 66 20 53 51 4c 69 74 65  d..** ^If SQLite
52990 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 61 6e 64   is compiled and
529a0 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 73   configured to s
529b0 75 70 70 6f 72 74 20 74 68 72 65 61 64 73 61 66  upport threadsaf
529c0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  e database.** co
529d0 6e 6e 65 63 74 69 6f 6e 73 2c 20 74 68 65 6e 20  nnections, then 
529e0 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
529f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d  ase connection m
52a00 61 79 20 62 65 20 75 73 65 64 20 63 6f 6e 63 75  ay be used concu
52a10 72 72 65 6e 74 6c 79 0a 2a 2a 20 66 72 6f 6d 20  rrently.** from 
52a20 77 69 74 68 69 6e 20 6f 74 68 65 72 20 74 68 72  within other thr
52a30 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 48 6f 77 65  eads..**.** Howe
52a40 76 65 72 2c 20 74 68 65 20 61 70 70 6c 69 63 61  ver, the applica
52a50 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61 6e  tion must guaran
52a60 74 65 65 20 74 68 61 74 20 74 68 65 20 64 65 73  tee that the des
52a70 74 69 6e 61 74 69 6f 6e 20 0a 2a 2a 20 5b 64 61  tination .** [da
52a80 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
52a90 6e 5d 20 69 73 20 6e 6f 74 20 70 61 73 73 65 64  n] is not passed
52aa0 20 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 41 50   to any other AP
52ab0 49 20 28 62 79 20 61 6e 79 20 74 68 72 65 61 64  I (by any thread
52ac0 29 20 61 66 74 65 72 20 0a 2a 2a 20 73 71 6c 69  ) after .** sqli
52ad0 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28  te3_backup_init(
52ae0 29 20 69 73 20 63 61 6c 6c 65 64 20 61 6e 64 20  ) is called and 
52af0 62 65 66 6f 72 65 20 74 68 65 20 63 6f 72 72 65  before the corre
52b00 73 70 6f 6e 64 69 6e 67 20 63 61 6c 6c 20 74 6f  sponding call to
52b10 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  .** sqlite3_back
52b20 75 70 5f 66 69 6e 69 73 68 28 29 2e 20 20 53 51  up_finish().  SQ
52b30 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 63 75  Lite does not cu
52b40 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 20 74 6f  rrently check to
52b50 20 73 65 65 0a 2a 2a 20 69 66 20 74 68 65 20 61   see.** if the a
52b60 70 70 6c 69 63 61 74 69 6f 6e 20 69 6e 63 6f 72  pplication incor
52b70 72 65 63 74 6c 79 20 61 63 63 65 73 73 65 73 20  rectly accesses 
52b80 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
52b90 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
52ba0 74 69 6f 6e 5d 0a 2a 2a 20 61 6e 64 20 73 6f 20  tion].** and so 
52bb0 6e 6f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  no error code is
52bc0 20 72 65 70 6f 72 74 65 64 2c 20 62 75 74 20 74   reported, but t
52bd0 68 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 6d 61  he operations ma
52be0 79 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 0a 2a 2a  y malfunction.**
52bf0 20 6e 65 76 65 72 74 68 65 6c 65 73 73 2e 20 20   nevertheless.  
52c00 55 73 65 20 6f 66 20 74 68 65 20 64 65 73 74 69  Use of the desti
52c10 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
52c20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65  connection while
52c30 20 61 0a 2a 2a 20 62 61 63 6b 75 70 20 69 73 20   a.** backup is 
52c40 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 69 67 68  in progress migh
52c50 74 20 61 6c 73 6f 20 61 6c 73 6f 20 63 61 75 73  t also also caus
52c60 65 20 61 20 6d 75 74 65 78 20 64 65 61 64 6c 6f  e a mutex deadlo
52c70 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 75 6e  ck..**.** If run
52c80 6e 69 6e 67 20 69 6e 20 5b 73 68 61 72 65 64 20  ning in [shared 
52c90 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 74 68 65  cache mode], the
52ca0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73   application mus
52cb0 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74  t.** guarantee t
52cc0 68 61 74 20 74 68 65 20 73 68 61 72 65 64 20 63  hat the shared c
52cd0 61 63 68 65 20 75 73 65 64 20 62 79 20 74 68 65  ache used by the
52ce0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
52cf0 61 62 61 73 65 0a 2a 2a 20 69 73 20 6e 6f 74 20  abase.** is not 
52d00 61 63 63 65 73 73 65 64 20 77 68 69 6c 65 20 74  accessed while t
52d10 68 65 20 62 61 63 6b 75 70 20 69 73 20 72 75 6e  he backup is run
52d20 6e 69 6e 67 2e 20 49 6e 20 70 72 61 63 74 69 63  ning. In practic
52d30 65 20 74 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20  e this means.** 
52d40 74 68 61 74 20 74 68 65 20 61 70 70 6c 69 63 61  that the applica
52d50 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61 6e  tion must guaran
52d60 74 65 65 20 74 68 61 74 20 74 68 65 20 64 69 73  tee that the dis
52d70 6b 20 66 69 6c 65 20 62 65 69 6e 67 20 0a 2a 2a  k file being .**
52d80 20 62 61 63 6b 65 64 20 75 70 20 74 6f 20 69 73   backed up to is
52d90 20 6e 6f 74 20 61 63 63 65 73 73 65 64 20 62 79   not accessed by
52da0 20 61 6e 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   any connection 
52db0 77 69 74 68 69 6e 20 74 68 65 20 70 72 6f 63 65  within the proce
52dc0 73 73 2c 0a 2a 2a 20 6e 6f 74 20 6a 75 73 74 20  ss,.** not just 
52dd0 74 68 65 20 73 70 65 63 69 66 69 63 20 63 6f 6e  the specific con
52de0 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 77 61 73  nection that was
52df0 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74   passed to sqlit
52e00 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29  e3_backup_init()
52e10 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c  ..**.** The [sql
52e20 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a  ite3_backup] obj
52e30 65 63 74 20 69 74 73 65 6c 66 20 69 73 20 70 61  ect itself is pa
52e40 72 74 69 61 6c 6c 79 20 74 68 72 65 61 64 73 61  rtially threadsa
52e50 66 65 2e 20 4d 75 6c 74 69 70 6c 65 20 0a 2a 2a  fe. Multiple .**
52e60 20 74 68 72 65 61 64 73 20 6d 61 79 20 73 61 66   threads may saf
52e70 65 6c 79 20 6d 61 6b 65 20 6d 75 6c 74 69 70 6c  ely make multipl
52e80 65 20 63 6f 6e 63 75 72 72 65 6e 74 20 63 61 6c  e concurrent cal
52e90 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61  ls to sqlite3_ba
52ea0 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2a 20  ckup_step()..** 
52eb0 48 6f 77 65 76 65 72 2c 20 74 68 65 20 73 71 6c  However, the sql
52ec0 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61  ite3_backup_rema
52ed0 69 6e 69 6e 67 28 29 20 61 6e 64 20 73 71 6c 69  ining() and sqli
52ee0 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63  te3_backup_pagec
52ef0 6f 75 6e 74 28 29 0a 2a 2a 20 41 50 49 73 20 61  ount().** APIs a
52f00 72 65 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20  re not strictly 
52f10 73 70 65 61 6b 69 6e 67 20 74 68 72 65 61 64 73  speaking threads
52f20 61 66 65 2e 20 49 66 20 74 68 65 79 20 61 72 65  afe. If they are
52f30 20 69 6e 76 6f 6b 65 64 20 61 74 20 74 68 65 0a   invoked at the.
52f40 2a 2a 20 73 61 6d 65 20 74 69 6d 65 20 61 73 20  ** same time as 
52f50 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69  another thread i
52f60 73 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74  s invoking sqlit
52f70 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
52f80 20 69 74 20 69 73 0a 2a 2a 20 70 6f 73 73 69 62   it is.** possib
52f90 6c 65 20 74 68 61 74 20 74 68 65 79 20 72 65 74  le that they ret
52fa0 75 72 6e 20 69 6e 76 61 6c 69 64 20 76 61 6c 75  urn invalid valu
52fb0 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  es..*/.SQLITE_AP
52fc0 49 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  I sqlite3_backup
52fd0 20 2a 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70   *sqlite3_backup
52fe0 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33  _init(.  sqlite3
52ff0 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 20 20   *pDest,        
53000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53010 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 64  /* Destination d
53020 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
53030 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
53040 7a 44 65 73 74 4e 61 6d 65 2c 20 20 20 20 20 20  zDestName,      
53050 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
53060 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61  stination databa
53070 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 71 6c  se name */.  sql
53080 69 74 65 33 20 2a 70 53 6f 75 72 63 65 2c 20 20  ite3 *pSource,  
53090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
530a0 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61      /* Source da
530b0 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f  tabase handle */
530c0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
530d0 53 6f 75 72 63 65 4e 61 6d 65 20 20 20 20 20 20  SourceName      
530e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75            /* Sou
530f0 72 63 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  rce database nam
53100 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  e */.);.SQLITE_A
53110 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
53120 61 63 6b 75 70 5f 73 74 65 70 28 73 71 6c 69 74  ackup_step(sqlit
53130 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 69 6e  e3_backup *p, in
53140 74 20 6e 50 61 67 65 29 3b 0a 53 51 4c 49 54 45  t nPage);.SQLITE
53150 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
53160 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 73  _backup_finish(s
53170 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70  qlite3_backup *p
53180 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
53190 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  t sqlite3_backup
531a0 5f 72 65 6d 61 69 6e 69 6e 67 28 73 71 6c 69 74  _remaining(sqlit
531b0 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 53  e3_backup *p);.S
531c0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
531d0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67  lite3_backup_pag
531e0 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62  ecount(sqlite3_b
531f0 61 63 6b 75 70 20 2a 70 29 3b 0a 0a 2f 2a 0a 2a  ackup *p);../*.*
53200 2a 20 43 41 50 49 33 52 45 46 3a 20 55 6e 6c 6f  * CAPI3REF: Unlo
53210 63 6b 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 0a  ck Notification.
53220 2a 2a 0a 2a 2a 20 5e 57 68 65 6e 20 72 75 6e 6e  **.** ^When runn
53230 69 6e 67 20 69 6e 20 73 68 61 72 65 64 2d 63 61  ing in shared-ca
53240 63 68 65 20 6d 6f 64 65 2c 20 61 20 64 61 74 61  che mode, a data
53250 62 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 6d  base operation m
53260 61 79 20 66 61 69 6c 20 77 69 74 68 0a 2a 2a 20  ay fail with.** 
53270 61 6e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  an [SQLITE_LOCKE
53280 44 5d 20 65 72 72 6f 72 20 69 66 20 74 68 65 20  D] error if the 
53290 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 6f  required locks o
532a0 6e 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63  n the shared-cac
532b0 68 65 20 6f 72 0a 2a 2a 20 69 6e 64 69 76 69 64  he or.** individ
532c0 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68 69  ual tables withi
532d0 6e 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63  n the shared-cac
532e0 68 65 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74  he cannot be obt
532f0 61 69 6e 65 64 2e 20 53 65 65 0a 2a 2a 20 5b 53  ained. See.** [S
53300 51 4c 69 74 65 20 53 68 61 72 65 64 2d 43 61 63  QLite Shared-Cac
53310 68 65 20 4d 6f 64 65 5d 20 66 6f 72 20 61 20 64  he Mode] for a d
53320 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 73 68  escription of sh
53330 61 72 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b 69  ared-cache locki
53340 6e 67 2e 20 0a 2a 2a 20 5e 54 68 69 73 20 41 50  ng. .** ^This AP
53350 49 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f  I may be used to
53360 20 72 65 67 69 73 74 65 72 20 61 20 63 61 6c 6c   register a call
53370 62 61 63 6b 20 74 68 61 74 20 53 51 4c 69 74 65  back that SQLite
53380 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 0a 2a 2a   will invoke .**
53390 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 6e 65 63   when the connec
533a0 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 68  tion currently h
533b0 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 71 75 69  olding the requi
533c0 72 65 64 20 6c 6f 63 6b 20 72 65 6c 69 6e 71 75  red lock relinqu
533d0 69 73 68 65 73 20 69 74 2e 0a 2a 2a 20 5e 54 68  ishes it..** ^Th
533e0 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20 61  is API is only a
533f0 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20  vailable if the 
53400 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70  library was comp
53410 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a  iled with the.**
53420 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f   [SQLITE_ENABLE_
53430 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 5d 20 43  UNLOCK_NOTIFY] C
53440 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73 79  -preprocessor sy
53450 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2a  mbol defined..**
53460 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55  .** See Also: [U
53470 73 69 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20  sing the SQLite 
53480 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69 63 61 74  Unlock Notificat
53490 69 6f 6e 20 46 65 61 74 75 72 65 5d 2e 0a 2a 2a  ion Feature]..**
534a0 0a 2a 2a 20 5e 53 68 61 72 65 64 2d 63 61 63 68  .** ^Shared-cach
534b0 65 20 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c 65  e locks are rele
534c0 61 73 65 64 20 77 68 65 6e 20 61 20 64 61 74 61  ased when a data
534d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
534e0 63 6f 6e 63 6c 75 64 65 73 0a 2a 2a 20 69 74 73  concludes.** its
534f0 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
53500 74 69 6f 6e 2c 20 65 69 74 68 65 72 20 62 79 20  tion, either by 
53510 63 6f 6d 6d 69 74 74 69 6e 67 20 69 74 20 6f 72  committing it or
53520 20 72 6f 6c 6c 69 6e 67 20 69 74 20 62 61 63 6b   rolling it back
53530 2e 20 0a 2a 2a 0a 2a 2a 20 5e 57 68 65 6e 20 61  . .**.** ^When a
53540 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 6b 6e 6f   connection (kno
53550 77 6e 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 65  wn as the blocke
53560 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 66 61  d connection) fa
53570 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 0a  ils to obtain a.
53580 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  ** shared-cache 
53590 6c 6f 63 6b 20 61 6e 64 20 53 51 4c 49 54 45 5f  lock and SQLITE_
535a0 4c 4f 43 4b 45 44 20 69 73 20 72 65 74 75 72 6e  LOCKED is return
535b0 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  ed to the caller
535c0 2c 20 74 68 65 0a 2a 2a 20 69 64 65 6e 74 69 74  , the.** identit
535d0 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  y of the databas
535e0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 74 68  e connection (th
535f0 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65  e blocking conne
53600 63 74 69 6f 6e 29 20 74 68 61 74 0a 2a 2a 20 68  ction) that.** h
53610 61 73 20 6c 6f 63 6b 65 64 20 74 68 65 20 72 65  as locked the re
53620 71 75 69 72 65 64 20 72 65 73 6f 75 72 63 65 20  quired resource 
53630 69 73 20 73 74 6f 72 65 64 20 69 6e 74 65 72 6e  is stored intern
53640 61 6c 6c 79 2e 20 5e 41 66 74 65 72 20 61 6e 20  ally. ^After an 
53650 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  .** application 
53660 72 65 63 65 69 76 65 73 20 61 6e 20 53 51 4c 49  receives an SQLI
53670 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f 72 2c  TE_LOCKED error,
53680 20 69 74 20 6d 61 79 20 63 61 6c 6c 20 74 68 65   it may call the
53690 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f  .** sqlite3_unlo
536a0 63 6b 5f 6e 6f 74 69 66 79 28 29 20 6d 65 74 68  ck_notify() meth
536b0 6f 64 20 77 69 74 68 20 74 68 65 20 62 6c 6f 63  od with the bloc
536c0 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  ked connection h
536d0 61 6e 64 6c 65 20 61 73 20 0a 2a 2a 20 74 68 65  andle as .** the
536e0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
536f0 74 6f 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  to register for 
53700 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20  a callback that 
53710 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 0a  will be invoked.
53720 2a 2a 20 77 68 65 6e 20 74 68 65 20 62 6c 6f 63  ** when the bloc
53730 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  king connections
53740 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
53750 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65  tion is conclude
53760 64 2e 20 5e 54 68 65 0a 2a 2a 20 63 61 6c 6c 62  d. ^The.** callb
53770 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 66  ack is invoked f
53780 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 5b  rom within the [
53790 73 71 6c 69 74 65 33 5f 73 74 65 70 5d 20 6f 72  sqlite3_step] or
537a0 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5d   [sqlite3_close]
537b0 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74 20 63 6f  .** call that co
537c0 6e 63 6c 75 64 65 73 20 74 68 65 20 62 6c 6f 63  ncludes the bloc
537d0 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  king connections
537e0 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a   transaction..**
537f0 0a 2a 2a 20 5e 28 49 66 20 73 71 6c 69 74 65 33  .** ^(If sqlite3
53800 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
53810 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 61 20   is called in a 
53820 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61  multi-threaded a
53830 70 70 6c 69 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74  pplication,.** t
53840 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65  here is a chance
53850 20 74 68 61 74 20 74 68 65 20 62 6c 6f 63 6b 69   that the blocki
53860 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69  ng connection wi
53870 6c 6c 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a  ll have already.
53880 2a 2a 20 63 6f 6e 63 6c 75 64 65 64 20 69 74 73  ** concluded its
53890 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 79 20   transaction by 
538a0 74 68 65 20 74 69 6d 65 20 73 71 6c 69 74 65 33  the time sqlite3
538b0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
538c0 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20   is invoked..** 
538d0 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c  If this happens,
538e0 20 74 68 65 6e 20 74 68 65 20 73 70 65 63 69 66   then the specif
538f0 69 65 64 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  ied callback is 
53900 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74  invoked immediat
53910 65 6c 79 2c 0a 2a 2a 20 66 72 6f 6d 20 77 69 74  ely,.** from wit
53920 68 69 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  hin the call to 
53930 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e  sqlite3_unlock_n
53940 6f 74 69 66 79 28 29 2e 29 5e 0a 2a 2a 0a 2a 2a  otify().)^.**.**
53950 20 5e 49 66 20 74 68 65 20 62 6c 6f 63 6b 65 64   ^If the blocked
53960 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 61   connection is a
53970 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f 62 74  ttempting to obt
53980 61 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ain a write-lock
53990 20 6f 6e 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d   on a.** shared-
539a0 63 61 63 68 65 20 74 61 62 6c 65 2c 20 61 6e 64  cache table, and
539b0 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f   more than one o
539c0 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ther connection 
539d0 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 73 0a  currently holds.
539e0 2a 2a 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f  ** a read-lock o
539f0 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  n the same table
53a00 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 61 72  , then SQLite ar
53a10 62 69 74 72 61 72 69 6c 79 20 73 65 6c 65 63 74  bitrarily select
53a20 73 20 6f 6e 65 20 6f 66 20 0a 2a 2a 20 74 68 65  s one of .** the
53a30 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f   other connectio
53a40 6e 73 20 74 6f 20 75 73 65 20 61 73 20 74 68 65  ns to use as the
53a50 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63   blocking connec
53a60 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 5e 28 54 68  tion..**.** ^(Th
53a70 65 72 65 20 6d 61 79 20 62 65 20 61 74 20 6d 6f  ere may be at mo
53a80 73 74 20 6f 6e 65 20 75 6e 6c 6f 63 6b 2d 6e 6f  st one unlock-no
53a90 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 72 65  tify callback re
53aa0 67 69 73 74 65 72 65 64 20 62 79 20 61 20 0a 2a  gistered by a .*
53ab0 2a 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  * blocked connec
53ac0 74 69 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65 33  tion. If sqlite3
53ad0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
53ae0 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
53af0 74 68 65 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20 63  the.** blocked c
53b00 6f 6e 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61 64  onnection alread
53b10 79 20 68 61 73 20 61 20 72 65 67 69 73 74 65 72  y has a register
53b20 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  ed unlock-notify
53b30 20 63 61 6c 6c 62 61 63 6b 2c 0a 2a 2a 20 74 68   callback,.** th
53b40 65 6e 20 74 68 65 20 6e 65 77 20 63 61 6c 6c 62  en the new callb
53b50 61 63 6b 20 72 65 70 6c 61 63 65 73 20 74 68 65  ack replaces the
53b60 20 6f 6c 64 2e 29 5e 20 5e 49 66 20 73 71 6c 69   old.)^ ^If sqli
53b70 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
53b80 79 28 29 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64  y() is.** called
53b90 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69   with a NULL poi
53ba0 6e 74 65 72 20 61 73 20 69 74 73 20 73 65 63 6f  nter as its seco
53bb0 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 74 68 65  nd argument, the
53bc0 6e 20 61 6e 79 20 65 78 69 73 74 69 6e 67 0a 2a  n any existing.*
53bd0 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  * unlock-notify 
53be0 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 61 6e 63  callback is canc
53bf0 65 6c 65 64 2e 20 5e 54 68 65 20 62 6c 6f 63 6b  eled. ^The block
53c00 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 0a  ed connections .
53c10 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  ** unlock-notify
53c20 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61 6c   callback may al
53c30 73 6f 20 62 65 20 63 61 6e 63 65 6c 65 64 20 62  so be canceled b
53c40 79 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 62 6c  y closing the bl
53c50 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 6e 65 63 74  ocked.** connect
53c60 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ion using [sqlit
53c70 65 33 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a  e3_close()]..**.
53c80 2a 2a 20 54 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f  ** The unlock-no
53c90 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73  tify callback is
53ca0 20 6e 6f 74 20 72 65 65 6e 74 72 61 6e 74 2e 20   not reentrant. 
53cb0 49 66 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f  If an applicatio
53cc0 6e 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 61 6e 79  n invokes.** any
53cd0 20 73 71 6c 69 74 65 33 5f 78 78 78 20 41 50 49   sqlite3_xxx API
53ce0 20 66 75 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20   functions from 
53cf0 77 69 74 68 69 6e 20 61 6e 20 75 6e 6c 6f 63 6b  within an unlock
53d00 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
53d10 2c 20 61 0a 2a 2a 20 63 72 61 73 68 20 6f 72 20  , a.** crash or 
53d20 64 65 61 64 6c 6f 63 6b 20 6d 61 79 20 62 65 20  deadlock may be 
53d30 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a  the result..**.*
53d40 2a 20 5e 55 6e 6c 65 73 73 20 64 65 61 64 6c 6f  * ^Unless deadlo
53d50 63 6b 20 69 73 20 64 65 74 65 63 74 65 64 20 28  ck is detected (
53d60 73 65 65 20 62 65 6c 6f 77 29 2c 20 73 71 6c 69  see below), sqli
53d70 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
53d80 79 28 29 20 61 6c 77 61 79 73 0a 2a 2a 20 72 65  y() always.** re
53d90 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e  turns SQLITE_OK.
53da0 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 61 6c 6c 62 61  .**.** <b>Callba
53db0 63 6b 20 49 6e 76 6f 63 61 74 69 6f 6e 20 44 65  ck Invocation De
53dc0 74 61 69 6c 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20  tails</b>.**.** 
53dd0 57 68 65 6e 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e  When an unlock-n
53de0 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69  otify callback i
53df0 73 20 72 65 67 69 73 74 65 72 65 64 2c 20 74 68  s registered, th
53e00 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 70 72  e application pr
53e10 6f 76 69 64 65 73 20 61 20 0a 2a 2a 20 73 69 6e  ovides a .** sin
53e20 67 6c 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65  gle void* pointe
53e30 72 20 74 68 61 74 20 69 73 20 70 61 73 73 65 64  r that is passed
53e40 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   to the callback
53e50 20 77 68 65 6e 20 69 74 20 69 73 20 69 6e 76 6f   when it is invo
53e60 6b 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  ked..** However,
53e70 20 74 68 65 20 73 69 67 6e 61 74 75 72 65 20 6f   the signature o
53e80 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  f the callback f
53e90 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 77 73 20 53  unction allows S
53ea0 51 4c 69 74 65 20 74 6f 20 70 61 73 73 0a 2a 2a  QLite to pass.**
53eb0 20 69 74 20 61 6e 20 61 72 72 61 79 20 6f 66 20   it an array of 
53ec0 76 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20 70 6f  void* context po
53ed0 69 6e 74 65 72 73 2e 20 54 68 65 20 66 69 72 73  inters. The firs
53ee0 74 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65  t argument passe
53ef0 64 20 74 6f 0a 2a 2a 20 61 6e 20 75 6e 6c 6f 63  d to.** an unloc
53f00 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
53f10 6b 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  k is a pointer t
53f20 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 76 6f  o an array of vo
53f30 69 64 2a 20 70 6f 69 6e 74 65 72 73 2c 0a 2a 2a  id* pointers,.**
53f40 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20   and the second 
53f50 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
53f60 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
53f70 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  array..**.** Whe
53f80 6e 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e  n a blocking con
53f90 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63  nections transac
53fa0 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65  tion is conclude
53fb0 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62 65 0a  d, there may be.
53fc0 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  ** more than one
53fd0 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
53fe0 69 6f 6e 20 74 68 61 74 20 68 61 73 20 72 65 67  ion that has reg
53ff0 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75  istered for an u
54000 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20  nlock-notify.** 
54010 63 61 6c 6c 62 61 63 6b 2e 20 5e 49 66 20 74 77  callback. ^If tw
54020 6f 20 6f 72 20 6d 6f 72 65 20 73 75 63 68 20 62  o or more such b
54030 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f  locked connectio
54040 6e 73 20 68 61 76 65 20 73 70 65 63 69 66 69 65  ns have specifie
54050 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 61  d the.** same ca
54060 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2c  llback function,
54070 20 74 68 65 6e 20 69 6e 73 74 65 61 64 20 6f 66   then instead of
54080 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 61   invoking the ca
54090 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a  llback function.
540a0 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ** multiple time
540b0 73 2c 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64  s, it is invoked
540c0 20 6f 6e 63 65 20 77 69 74 68 20 74 68 65 20 73   once with the s
540d0 65 74 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e 74  et of void* cont
540e0 65 78 74 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20  ext pointers.** 
540f0 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
54100 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
54110 69 6f 6e 73 20 62 75 6e 64 6c 65 64 20 74 6f 67  ions bundled tog
54120 65 74 68 65 72 20 69 6e 74 6f 20 61 6e 20 61 72  ether into an ar
54130 72 61 79 2e 0a 2a 2a 20 54 68 69 73 20 67 69 76  ray..** This giv
54140 65 73 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  es the applicati
54150 6f 6e 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74  on an opportunit
54160 79 20 74 6f 20 70 72 69 6f 72 69 74 69 7a 65 20  y to prioritize 
54170 61 6e 79 20 61 63 74 69 6f 6e 73 20 0a 2a 2a 20  any actions .** 
54180 72 65 6c 61 74 65 64 20 74 6f 20 74 68 65 20 73  related to the s
54190 65 74 20 6f 66 20 75 6e 62 6c 6f 63 6b 65 64 20  et of unblocked 
541a0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
541b0 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 44  ions..**.** <b>D
541c0 65 61 64 6c 6f 63 6b 20 44 65 74 65 63 74 69 6f  eadlock Detectio
541d0 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 41 73 73 75  n</b>.**.** Assu
541e0 6d 69 6e 67 20 74 68 61 74 20 61 66 74 65 72 20  ming that after 
541f0 72 65 67 69 73 74 65 72 69 6e 67 20 66 6f 72 20  registering for 
54200 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  an unlock-notify
54210 20 63 61 6c 6c 62 61 63 6b 20 61 20 0a 2a 2a 20   callback a .** 
54220 64 61 74 61 62 61 73 65 20 77 61 69 74 73 20 66  database waits f
54230 6f 72 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  or the callback 
54240 74 6f 20 62 65 20 69 73 73 75 65 64 20 62 65 66  to be issued bef
54250 6f 72 65 20 74 61 6b 69 6e 67 20 61 6e 79 20 66  ore taking any f
54260 75 72 74 68 65 72 0a 2a 2a 20 61 63 74 69 6f 6e  urther.** action
54270 20 28 61 20 72 65 61 73 6f 6e 61 62 6c 65 20 61   (a reasonable a
54280 73 73 75 6d 70 74 69 6f 6e 29 2c 20 74 68 65 6e  ssumption), then
54290 20 75 73 69 6e 67 20 74 68 69 73 20 41 50 49 20   using this API 
542a0 6d 61 79 20 63 61 75 73 65 20 74 68 65 0a 2a 2a  may cause the.**
542b0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20   application to 
542c0 64 65 61 64 6c 6f 63 6b 2e 20 46 6f 72 20 65 78  deadlock. For ex
542d0 61 6d 70 6c 65 2c 20 69 66 20 63 6f 6e 6e 65 63  ample, if connec
542e0 74 69 6f 6e 20 58 20 69 73 20 77 61 69 74 69 6e  tion X is waitin
542f0 67 20 66 6f 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74  g for.** connect
54300 69 6f 6e 20 59 27 73 20 74 72 61 6e 73 61 63 74  ion Y's transact
54310 69 6f 6e 20 74 6f 20 62 65 20 63 6f 6e 63 6c 75  ion to be conclu
54320 64 65 64 2c 20 61 6e 64 20 73 69 6d 69 6c 61 72  ded, and similar
54330 6c 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  ly connection.**
54340 20 59 20 69 73 20 77 61 69 74 69 6e 67 20 6f 6e   Y is waiting on
54350 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 58 27 73 20   connection X's 
54360 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65  transaction, the
54370 6e 20 6e 65 69 74 68 65 72 20 63 6f 6e 6e 65 63  n neither connec
54380 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 70 72 6f  tion.** will pro
54390 63 65 65 64 20 61 6e 64 20 74 68 65 20 73 79 73  ceed and the sys
543a0 74 65 6d 20 6d 61 79 20 72 65 6d 61 69 6e 20 64  tem may remain d
543b0 65 61 64 6c 6f 63 6b 65 64 20 69 6e 64 65 66 69  eadlocked indefi
543c0 6e 69 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 6f  nitely..**.** To
543d0 20 61 76 6f 69 64 20 74 68 69 73 20 73 63 65 6e   avoid this scen
543e0 61 72 69 6f 2c 20 74 68 65 20 73 71 6c 69 74 65  ario, the sqlite
543f0 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28  3_unlock_notify(
54400 29 20 70 65 72 66 6f 72 6d 73 20 64 65 61 64 6c  ) performs deadl
54410 6f 63 6b 0a 2a 2a 20 64 65 74 65 63 74 69 6f 6e  ock.** detection
54420 2e 20 5e 49 66 20 61 20 67 69 76 65 6e 20 63 61  . ^If a given ca
54430 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e  ll to sqlite3_un
54440 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 77 6f  lock_notify() wo
54450 75 6c 64 20 70 75 74 20 74 68 65 0a 2a 2a 20 73  uld put the.** s
54460 79 73 74 65 6d 20 69 6e 20 61 20 64 65 61 64 6c  ystem in a deadl
54470 6f 63 6b 65 64 20 73 74 61 74 65 2c 20 74 68 65  ocked state, the
54480 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  n SQLITE_LOCKED 
54490 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
544a0 6e 6f 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  no.** unlock-not
544b0 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  ify callback is 
544c0 72 65 67 69 73 74 65 72 65 64 2e 20 54 68 65 20  registered. The 
544d0 73 79 73 74 65 6d 20 69 73 20 73 61 69 64 20 74  system is said t
544e0 6f 20 62 65 20 69 6e 0a 2a 2a 20 61 20 64 65 61  o be in.** a dea
544f0 64 6c 6f 63 6b 65 64 20 73 74 61 74 65 20 69 66  dlocked state if
54500 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 41 20 68 61   connection A ha
54510 73 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72  s registered for
54520 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
54530 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e  y.** callback on
54540 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20   the conclusion 
54550 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 42 27  of connection B'
54560 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61  s transaction, a
54570 6e 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  nd connection.**
54580 20 42 20 68 61 73 20 69 74 73 65 6c 66 20 72 65   B has itself re
54590 67 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20  gistered for an 
545a0 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
545b0 6c 6c 62 61 63 6b 20 77 68 65 6e 20 63 6f 6e 6e  llback when conn
545c0 65 63 74 69 6f 6e 0a 2a 2a 20 41 27 73 20 74 72  ection.** A's tr
545d0 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e  ansaction is con
545e0 63 6c 75 64 65 64 2e 20 5e 49 6e 64 69 72 65 63  cluded. ^Indirec
545f0 74 20 64 65 61 64 6c 6f 63 6b 20 69 73 20 61 6c  t deadlock is al
54600 73 6f 20 64 65 74 65 63 74 65 64 2c 20 73 6f 0a  so detected, so.
54610 2a 2a 20 74 68 65 20 73 79 73 74 65 6d 20 69 73  ** the system is
54620 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65 64   also considered
54630 20 74 6f 20 62 65 20 64 65 61 64 6c 6f 63 6b 65   to be deadlocke
54640 64 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  d if connection 
54650 42 20 68 61 73 0a 2a 2a 20 72 65 67 69 73 74 65  B has.** registe
54660 72 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63  red for an unloc
54670 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
54680 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c 75 73  k on the conclus
54690 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f  ion of connectio
546a0 6e 0a 2a 2a 20 43 27 73 20 74 72 61 6e 73 61 63  n.** C's transac
546b0 74 69 6f 6e 2c 20 77 68 65 72 65 20 63 6f 6e 6e  tion, where conn
546c0 65 63 74 69 6f 6e 20 43 20 69 73 20 77 61 69 74  ection C is wait
546d0 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f  ing on connectio
546e0 6e 20 41 2e 20 5e 41 6e 79 0a 2a 2a 20 6e 75 6d  n A. ^Any.** num
546f0 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66  ber of levels of
54700 20 69 6e 64 69 72 65 63 74 69 6f 6e 20 61 72 65   indirection are
54710 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a 0a 2a 2a 20   allowed..**.** 
54720 3c 62 3e 54 68 65 20 22 44 52 4f 50 20 54 41 42  <b>The "DROP TAB
54730 4c 45 22 20 45 78 63 65 70 74 69 6f 6e 3c 2f 62  LE" Exception</b
54740 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 63  >.**.** When a c
54750 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
54760 73 74 65 70 28 29 5d 20 72 65 74 75 72 6e 73 20  step()] returns 
54770 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c 20 69  SQLITE_LOCKED, i
54780 74 20 69 73 20 61 6c 6d 6f 73 74 20 0a 2a 2a 20  t is almost .** 
54790 61 6c 77 61 79 73 20 61 70 70 72 6f 70 72 69 61  always appropria
547a0 74 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74  te to call sqlit
547b0 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79  e3_unlock_notify
547c0 28 29 2e 20 54 68 65 72 65 20 69 73 20 68 6f 77  (). There is how
547d0 65 76 65 72 2c 0a 2a 2a 20 6f 6e 65 20 65 78 63  ever,.** one exc
547e0 65 70 74 69 6f 6e 2e 20 57 68 65 6e 20 65 78 65  eption. When exe
547f0 63 75 74 69 6e 67 20 61 20 22 44 52 4f 50 20 54  cuting a "DROP T
54800 41 42 4c 45 22 20 6f 72 20 22 44 52 4f 50 20 49  ABLE" or "DROP I
54810 4e 44 45 58 22 20 73 74 61 74 65 6d 65 6e 74 2c  NDEX" statement,
54820 0a 2a 2a 20 53 51 4c 69 74 65 20 63 68 65 63 6b  .** SQLite check
54830 73 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61  s if there are a
54840 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65  ny currently exe
54850 63 75 74 69 6e 67 20 53 45 4c 45 43 54 20 73 74  cuting SELECT st
54860 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74  atements.** that
54870 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 65 20 73   belong to the s
54880 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  ame connection. 
54890 49 66 20 74 68 65 72 65 20 61 72 65 2c 20 53 51  If there are, SQ
548a0 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73 0a 2a  LITE_LOCKED is.*
548b0 2a 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74  * returned. In t
548c0 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20 69  his case there i
548d0 73 20 6e 6f 20 22 62 6c 6f 63 6b 69 6e 67 20 63  s no "blocking c
548e0 6f 6e 6e 65 63 74 69 6f 6e 22 2c 20 73 6f 20 69  onnection", so i
548f0 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c 69 74  nvoking.** sqlit
54900 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79  e3_unlock_notify
54910 28 29 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68  () results in th
54920 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  e unlock-notify 
54930 63 61 6c 6c 62 61 63 6b 20 62 65 69 6e 67 0a 2a  callback being.*
54940 2a 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69  * invoked immedi
54950 61 74 65 6c 79 2e 20 49 66 20 74 68 65 20 61 70  ately. If the ap
54960 70 6c 69 63 61 74 69 6f 6e 20 74 68 65 6e 20 72  plication then r
54970 65 2d 61 74 74 65 6d 70 74 73 20 74 68 65 20 22  e-attempts the "
54980 44 52 4f 50 20 54 41 42 4c 45 22 0a 2a 2a 20 6f  DROP TABLE".** o
54990 72 20 22 44 52 4f 50 20 49 4e 44 45 58 22 20 71  r "DROP INDEX" q
549a0 75 65 72 79 2c 20 61 6e 20 69 6e 66 69 6e 69 74  uery, an infinit
549b0 65 20 6c 6f 6f 70 20 6d 69 67 68 74 20 62 65 20  e loop might be 
549c0 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a  the result..**.*
549d0 2a 20 4f 6e 65 20 77 61 79 20 61 72 6f 75 6e 64  * One way around
549e0 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 69 73   this problem is
549f0 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 65 78   to check the ex
54a00 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
54a10 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79  e returned.** by
54a20 20 61 6e 20 73 71 6c 69 74 65 33 5f 73 74 65 70   an sqlite3_step
54a30 28 29 20 63 61 6c 6c 2e 20 5e 28 49 66 20 74 68  () call. ^(If th
54a40 65 72 65 20 69 73 20 61 20 62 6c 6f 63 6b 69 6e  ere is a blockin
54a50 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68  g connection, th
54a60 65 6e 20 74 68 65 0a 2a 2a 20 65 78 74 65 6e 64  en the.** extend
54a70 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  ed error code is
54a80 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c   set to SQLITE_L
54a90 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48  OCKED_SHAREDCACH
54aa0 45 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e  E. Otherwise, in
54ab0 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20  .** the special 
54ac0 22 44 52 4f 50 20 54 41 42 4c 45 2f 49 4e 44 45  "DROP TABLE/INDE
54ad0 58 22 20 63 61 73 65 2c 20 74 68 65 20 65 78 74  X" case, the ext
54ae0 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65  ended error code
54af0 20 69 73 20 6a 75 73 74 20 0a 2a 2a 20 53 51 4c   is just .** SQL
54b00 49 54 45 5f 4c 4f 43 4b 45 44 2e 29 5e 0a 2a 2f  ITE_LOCKED.)^.*/
54b10 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
54b20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e  sqlite3_unlock_n
54b30 6f 74 69 66 79 28 0a 20 20 73 71 6c 69 74 65 33  otify(.  sqlite3
54b40 20 2a 70 42 6c 6f 63 6b 65 64 2c 20 20 20 20 20   *pBlocked,     
54b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54b60 20 20 20 20 20 2f 2a 20 57 61 69 74 69 6e 67 20       /* Waiting 
54b70 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
54b80 76 6f 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28  void (*xNotify)(
54b90 76 6f 69 64 20 2a 2a 61 70 41 72 67 2c 20 69 6e  void **apArg, in
54ba0 74 20 6e 41 72 67 29 2c 20 20 20 20 2f 2a 20 43  t nArg),    /* C
54bb0 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
54bc0 20 74 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20   to invoke */.  
54bd0 76 6f 69 64 20 2a 70 4e 6f 74 69 66 79 41 72 67  void *pNotifyArg
54be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54bf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
54c00 72 67 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20  rgument to pass 
54c10 74 6f 20 78 4e 6f 74 69 66 79 20 2a 2f 0a 29 3b  to xNotify */.);
54c20 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  .../*.** CAPI3RE
54c30 46 3a 20 53 74 72 69 6e 67 20 43 6f 6d 70 61 72  F: String Compar
54c40 69 73 6f 6e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  ison.**.** ^The 
54c50 5b 73 71 6c 69 74 65 33 5f 73 74 72 69 63 6d 70  [sqlite3_stricmp
54c60 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
54c70 5f 73 74 72 6e 69 63 6d 70 28 29 5d 20 41 50 49  _strnicmp()] API
54c80 73 20 61 6c 6c 6f 77 20 61 70 70 6c 69 63 61 74  s allow applicat
54c90 69 6f 6e 73 0a 2a 2a 20 61 6e 64 20 65 78 74 65  ions.** and exte
54ca0 6e 73 69 6f 6e 73 20 74 6f 20 63 6f 6d 70 61 72  nsions to compar
54cb0 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  e the contents o
54cc0 66 20 74 77 6f 20 62 75 66 66 65 72 73 20 63 6f  f two buffers co
54cd0 6e 74 61 69 6e 69 6e 67 20 55 54 46 2d 38 0a 2a  ntaining UTF-8.*
54ce0 2a 20 73 74 72 69 6e 67 73 20 69 6e 20 61 20 63  * strings in a c
54cf0 61 73 65 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20  ase-independent 
54d00 66 61 73 68 69 6f 6e 2c 20 75 73 69 6e 67 20 74  fashion, using t
54d10 68 65 20 73 61 6d 65 20 64 65 66 69 6e 69 74 69  he same definiti
54d20 6f 6e 20 6f 66 20 22 63 61 73 65 0a 2a 2a 20 69  on of "case.** i
54d30 6e 64 65 70 65 6e 64 65 6e 63 65 22 20 74 68 61  ndependence" tha
54d40 74 20 53 51 4c 69 74 65 20 75 73 65 73 20 69 6e  t SQLite uses in
54d50 74 65 72 6e 61 6c 6c 79 20 77 68 65 6e 20 63 6f  ternally when co
54d60 6d 70 61 72 69 6e 67 20 69 64 65 6e 74 69 66 69  mparing identifi
54d70 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ers..*/.SQLITE_A
54d80 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
54d90 74 72 69 63 6d 70 28 63 6f 6e 73 74 20 63 68 61  tricmp(const cha
54da0 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  r *, const char 
54db0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
54dc0 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69  nt sqlite3_strni
54dd0 63 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  cmp(const char *
54de0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
54df0 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  int);../*.** CAP
54e00 49 33 52 45 46 3a 20 45 72 72 6f 72 20 4c 6f 67  I3REF: Error Log
54e10 67 69 6e 67 20 49 6e 74 65 72 66 61 63 65 0a 2a  ging Interface.*
54e20 2a 0a 2a 2a 20 5e 54 68 65 20 5b 73 71 6c 69 74  *.** ^The [sqlit
54e30 65 33 5f 6c 6f 67 28 29 5d 20 69 6e 74 65 72 66  e3_log()] interf
54e40 61 63 65 20 77 72 69 74 65 73 20 61 20 6d 65 73  ace writes a mes
54e50 73 61 67 65 20 69 6e 74 6f 20 74 68 65 20 65 72  sage into the er
54e60 72 6f 72 20 6c 6f 67 0a 2a 2a 20 65 73 74 61 62  ror log.** estab
54e70 6c 69 73 68 65 64 20 62 79 20 74 68 65 20 5b 53  lished by the [S
54e80 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 47  QLITE_CONFIG_LOG
54e90 5d 20 6f 70 74 69 6f 6e 20 74 6f 20 5b 73 71 6c  ] option to [sql
54ea0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a  ite3_config()]..
54eb0 2a 2a 20 5e 49 66 20 6c 6f 67 67 69 6e 67 20 69  ** ^If logging i
54ec0 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 7a  s enabled, the z
54ed0 46 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 61 6e  Format string an
54ee0 64 20 73 75 62 73 65 71 75 65 6e 74 20 61 72 67  d subsequent arg
54ef0 75 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 75 73  uments are.** us
54f00 65 64 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33  ed with [sqlite3
54f10 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 74 6f 20  _snprintf()] to 
54f20 67 65 6e 65 72 61 74 65 20 74 68 65 20 66 69 6e  generate the fin
54f30 61 6c 20 6f 75 74 70 75 74 20 73 74 72 69 6e 67  al output string
54f40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
54f50 74 65 33 5f 6c 6f 67 28 29 20 69 6e 74 65 72 66  te3_log() interf
54f60 61 63 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20  ace is intended 
54f70 66 6f 72 20 75 73 65 20 62 79 20 65 78 74 65 6e  for use by exten
54f80 73 69 6f 6e 73 20 73 75 63 68 20 61 73 0a 2a 2a  sions such as.**
54f90 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2c   virtual tables,
54fa0 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74   collating funct
54fb0 69 6f 6e 73 2c 20 61 6e 64 20 53 51 4c 20 66 75  ions, and SQL fu
54fc0 6e 63 74 69 6f 6e 73 2e 20 20 57 68 69 6c 65 20  nctions.  While 
54fd0 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 74 68  there is.** noth
54fe0 69 6e 67 20 74 6f 20 70 72 65 76 65 6e 74 20 61  ing to prevent a
54ff0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 72  n application fr
55000 6f 6d 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  om calling sqlit
55010 65 33 5f 6c 6f 67 28 29 2c 20 64 6f 69 6e 67 20  e3_log(), doing 
55020 73 6f 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65  so.** is conside
55030 72 65 64 20 62 61 64 20 66 6f 72 6d 2e 0a 2a 2a  red bad form..**
55040 0a 2a 2a 20 54 68 65 20 7a 46 6f 72 6d 61 74 20  .** The zFormat 
55050 73 74 72 69 6e 67 20 6d 75 73 74 20 6e 6f 74 20  string must not 
55060 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54  be NULL..**.** T
55070 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b  o avoid deadlock
55080 73 20 61 6e 64 20 6f 74 68 65 72 20 74 68 72 65  s and other thre
55090 61 64 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2c 20  ading problems, 
550a0 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28  the sqlite3_log(
550b0 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 69 6c  ) routine.** wil
550c0 6c 20 6e 6f 74 20 75 73 65 20 64 79 6e 61 6d 69  l not use dynami
550d0 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  cally allocated 
550e0 6d 65 6d 6f 72 79 2e 20 20 54 68 65 20 6c 6f 67  memory.  The log
550f0 20 6d 65 73 73 61 67 65 20 69 73 20 73 74 6f 72   message is stor
55100 65 64 20 69 6e 0a 2a 2a 20 61 20 66 69 78 65 64  ed in.** a fixed
55110 2d 6c 65 6e 67 74 68 20 62 75 66 66 65 72 20 6f  -length buffer o
55120 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 49 66  n the stack.  If
55130 20 74 68 65 20 6c 6f 67 20 6d 65 73 73 61 67 65   the log message
55140 20 69 73 20 6c 6f 6e 67 65 72 20 74 68 61 6e 0a   is longer than.
55150 2a 2a 20 61 20 66 65 77 20 68 75 6e 64 72 65 64  ** a few hundred
55160 20 63 68 61 72 61 63 74 65 72 73 2c 20 69 74 20   characters, it 
55170 77 69 6c 6c 20 62 65 20 74 72 75 6e 63 61 74 65  will be truncate
55180 64 20 74 6f 20 74 68 65 20 6c 65 6e 67 74 68 20  d to the length 
55190 6f 66 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72  of the.** buffer
551a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
551b0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6c 6f 67  void sqlite3_log
551c0 28 69 6e 74 20 69 45 72 72 43 6f 64 65 2c 20 63  (int iErrCode, c
551d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
551e0 61 74 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a  at, ...);../*.**
551f0 20 43 41 50 49 33 52 45 46 3a 20 57 72 69 74 65   CAPI3REF: Write
55200 2d 41 68 65 61 64 20 4c 6f 67 20 43 6f 6d 6d 69  -Ahead Log Commi
55210 74 20 48 6f 6f 6b 0a 2a 2a 0a 2a 2a 20 5e 54 68  t Hook.**.** ^Th
55220 65 20 5b 73 71 6c 69 74 65 33 5f 77 61 6c 5f 68  e [sqlite3_wal_h
55230 6f 6f 6b 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20  ook()] function 
55240 69 73 20 75 73 65 64 20 74 6f 20 72 65 67 69 73  is used to regis
55250 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74  ter a callback t
55260 68 61 74 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 69  hat.** will be i
55270 6e 76 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65  nvoked each time
55280 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e   a database conn
55290 65 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 64  ection commits d
552a0 61 74 61 20 74 6f 20 61 0a 2a 2a 20 5b 77 72 69  ata to a.** [wri
552b0 74 65 2d 61 68 65 61 64 20 6c 6f 67 5d 20 28 69  te-ahead log] (i
552c0 2e 65 2e 20 77 68 65 6e 65 76 65 72 20 61 20 74  .e. whenever a t
552d0 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
552e0 6d 6d 69 74 74 65 64 20 69 6e 0a 2a 2a 20 5b 6a  mmitted in.** [j
552f0 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 7c 20 6a 6f  ournal_mode | jo
55300 75 72 6e 61 6c 5f 6d 6f 64 65 3d 57 41 4c 20 6d  urnal_mode=WAL m
55310 6f 64 65 5d 29 2e 20 0a 2a 2a 0a 2a 2a 20 5e 54  ode]). .**.** ^T
55320 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69  he callback is i
55330 6e 76 6f 6b 65 64 20 62 79 20 53 51 4c 69 74 65  nvoked by SQLite
55340 20 61 66 74 65 72 20 74 68 65 20 63 6f 6d 6d 69   after the commi
55350 74 20 68 61 73 20 74 61 6b 65 6e 20 70 6c 61 63  t has taken plac
55360 65 20 61 6e 64 20 0a 2a 2a 20 74 68 65 20 61 73  e and .** the as
55370 73 6f 63 69 61 74 65 64 20 77 72 69 74 65 2d 6c  sociated write-l
55380 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
55390 61 73 65 20 72 65 6c 65 61 73 65 64 2c 20 73 6f  ase released, so
553a0 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
553b0 69 6f 6e 20 0a 2a 2a 20 6d 61 79 20 72 65 61 64  ion .** may read
553c0 2c 20 77 72 69 74 65 20 6f 72 20 5b 63 68 65 63  , write or [chec
553d0 6b 70 6f 69 6e 74 5d 20 74 68 65 20 64 61 74 61  kpoint] the data
553e0 62 61 73 65 20 61 73 20 72 65 71 75 69 72 65 64  base as required
553f0 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 66 69 72  ..**.** ^The fir
55400 73 74 20 70 61 72 61 6d 65 74 65 72 20 70 61 73  st parameter pas
55410 73 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  sed to the callb
55420 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77 68 65  ack function whe
55430 6e 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 0a  n it is invoked.
55440 2a 2a 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20  ** is a copy of 
55450 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65  the third parame
55460 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 73 71  ter passed to sq
55470 6c 69 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28 29  lite3_wal_hook()
55480 20 77 68 65 6e 0a 2a 2a 20 72 65 67 69 73 74 65   when.** registe
55490 72 69 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63  ring the callbac
554a0 6b 2e 20 5e 54 68 65 20 73 65 63 6f 6e 64 20 69  k. ^The second i
554b0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
554c0 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  database handle.
554d0 0a 2a 2a 20 5e 54 68 65 20 74 68 69 72 64 20 70  .** ^The third p
554e0 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20  arameter is the 
554f0 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
55500 62 61 73 65 20 74 68 61 74 20 77 61 73 20 77 72  base that was wr
55510 69 74 74 65 6e 20 74 6f 20 2d 0a 2a 2a 20 65 69  itten to -.** ei
55520 74 68 65 72 20 22 6d 61 69 6e 22 20 6f 72 20 74  ther "main" or t
55530 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 5b 41  he name of an [A
55540 54 54 41 43 48 5d 2d 65 64 20 64 61 74 61 62 61  TTACH]-ed databa
55550 73 65 2e 20 5e 54 68 65 20 66 6f 75 72 74 68 20  se. ^The fourth 
55560 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20  parameter.** is 
55570 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
55580 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  ges currently in
55590 20 74 68 65 20 77 72 69 74 65 2d 61 68 65 61 64   the write-ahead
555a0 20 6c 6f 67 20 66 69 6c 65 2c 0a 2a 2a 20 69 6e   log file,.** in
555b0 63 6c 75 64 69 6e 67 20 74 68 6f 73 65 20 74 68  cluding those th
555c0 61 74 20 77 65 72 65 20 6a 75 73 74 20 63 6f 6d  at were just com
555d0 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  mitted..**.** Th
555e0 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
555f0 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 72 6d 61  ion should norma
55600 6c 6c 79 20 72 65 74 75 72 6e 20 5b 53 51 4c 49  lly return [SQLI
55610 54 45 5f 4f 4b 5d 2e 20 20 5e 49 66 20 61 6e 20  TE_OK].  ^If an 
55620 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73  error.** code is
55630 20 72 65 74 75 72 6e 65 64 2c 20 74 68 61 74 20   returned, that 
55640 65 72 72 6f 72 20 77 69 6c 6c 20 70 72 6f 70 61  error will propa
55650 67 61 74 65 20 62 61 63 6b 20 75 70 20 74 68 72  gate back up thr
55660 6f 75 67 68 20 74 68 65 0a 2a 2a 20 53 51 4c 69  ough the.** SQLi
55670 74 65 20 63 6f 64 65 20 62 61 73 65 20 74 6f 20  te code base to 
55680 63 61 75 73 65 20 74 68 65 20 73 74 61 74 65 6d  cause the statem
55690 65 6e 74 20 74 68 61 74 20 70 72 6f 76 6f 6b 65  ent that provoke
556a0 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a  d the callback.*
556b0 2a 20 74 6f 20 72 65 70 6f 72 74 20 61 6e 20 65  * to report an e
556c0 72 72 6f 72 2c 20 74 68 6f 75 67 68 20 74 68 65  rror, though the
556d0 20 63 6f 6d 6d 69 74 20 77 69 6c 6c 20 68 61 76   commit will hav
556e0 65 20 73 74 69 6c 6c 20 6f 63 63 75 72 72 65 64  e still occurred
556f0 2e 20 49 66 20 74 68 65 0a 2a 2a 20 63 61 6c 6c  . If the.** call
55700 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51  back returns [SQ
55710 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20 5b 53 51  LITE_ROW] or [SQ
55720 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 6f 72 20 69  LITE_DONE], or i
55730 66 20 69 74 20 72 65 74 75 72 6e 73 20 61 20 76  f it returns a v
55740 61 6c 75 65 0a 2a 2a 20 74 68 61 74 20 64 6f 65  alue.** that doe
55750 73 20 6e 6f 74 20 63 6f 72 72 65 73 70 6f 6e 64  s not correspond
55760 20 74 6f 20 61 6e 79 20 76 61 6c 69 64 20 53 51   to any valid SQ
55770 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2c  Lite error code,
55780 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20   the results.** 
55790 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a  are undefined..*
557a0 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61  *.** A single da
557b0 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6d 61  tabase handle ma
557c0 79 20 68 61 76 65 20 61 74 20 6d 6f 73 74 20 61  y have at most a
557d0 20 73 69 6e 67 6c 65 20 77 72 69 74 65 2d 61 68   single write-ah
557e0 65 61 64 20 6c 6f 67 20 63 61 6c 6c 62 61 63 6b  ead log callback
557f0 20 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 20   .** registered 
55800 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 5e 43 61  at one time. ^Ca
55810 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 77  lling [sqlite3_w
55820 61 6c 5f 68 6f 6f 6b 28 29 5d 20 72 65 70 6c 61  al_hook()] repla
55830 63 65 73 20 61 6e 79 0a 2a 2a 20 70 72 65 76 69  ces any.** previ
55840 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64  ously registered
55850 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67   write-ahead log
55860 20 63 61 6c 6c 62 61 63 6b 2e 20 5e 4e 6f 74 65   callback. ^Note
55870 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 5b 73 71   that the.** [sq
55880 6c 69 74 65 33 5f 77 61 6c 5f 61 75 74 6f 63 68  lite3_wal_autoch
55890 65 63 6b 70 6f 69 6e 74 28 29 5d 20 69 6e 74 65  eckpoint()] inte
558a0 72 66 61 63 65 20 61 6e 64 20 74 68 65 0a 2a 2a  rface and the.**
558b0 20 5b 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70   [wal_autocheckp
558c0 6f 69 6e 74 20 70 72 61 67 6d 61 5d 20 62 6f 74  oint pragma] bot
558d0 68 20 69 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65  h invoke [sqlite
558e0 33 5f 77 61 6c 5f 68 6f 6f 6b 28 29 5d 20 61 6e  3_wal_hook()] an
558f0 64 20 77 69 6c 6c 0a 2a 2a 20 74 68 6f 73 65 20  d will.** those 
55900 6f 76 65 72 77 72 69 74 65 20 61 6e 79 20 70 72  overwrite any pr
55910 69 6f 72 20 5b 73 71 6c 69 74 65 33 5f 77 61 6c  ior [sqlite3_wal
55920 5f 68 6f 6f 6b 28 29 5d 20 73 65 74 74 69 6e 67  _hook()] setting
55930 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
55940 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 77   void *sqlite3_w
55950 61 6c 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74  al_hook(.  sqlit
55960 65 33 2a 2c 20 0a 20 20 69 6e 74 28 2a 29 28 76  e3*, .  int(*)(v
55970 6f 69 64 20 2a 2c 73 71 6c 69 74 65 33 2a 2c 63  oid *,sqlite3*,c
55980 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 2c  onst char*,int),
55990 0a 20 20 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a  .  void*.);../*.
559a0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e  ** CAPI3REF: Con
559b0 66 69 67 75 72 65 20 61 6e 20 61 75 74 6f 2d 63  figure an auto-c
559c0 68 65 63 6b 70 6f 69 6e 74 0a 2a 2a 0a 2a 2a 20  heckpoint.**.** 
559d0 5e 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 77 61  ^The [sqlite3_wa
559e0 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69 6e 74  l_autocheckpoint
559f0 28 44 2c 4e 29 5d 20 69 73 20 61 20 77 72 61 70  (D,N)] is a wrap
55a00 70 65 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 5b 73  per around.** [s
55a10 71 6c 69 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28  qlite3_wal_hook(
55a20 29 5d 20 74 68 61 74 20 63 61 75 73 65 73 20 61  )] that causes a
55a30 6e 79 20 64 61 74 61 62 61 73 65 20 6f 6e 20 5b  ny database on [
55a40 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
55a50 69 6f 6e 5d 20 44 0a 2a 2a 20 74 6f 20 61 75 74  ion] D.** to aut
55a60 6f 6d 61 74 69 63 61 6c 6c 79 20 5b 63 68 65 63  omatically [chec
55a70 6b 70 6f 69 6e 74 5d 0a 2a 2a 20 61 66 74 65 72  kpoint].** after
55a80 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72   committing a tr
55a90 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65  ansaction if the
55aa0 72 65 20 61 72 65 20 4e 20 6f 72 0a 2a 2a 20 6d  re are N or.** m
55ab0 6f 72 65 20 66 72 61 6d 65 73 20 69 6e 20 74 68  ore frames in th
55ac0 65 20 5b 77 72 69 74 65 2d 61 68 65 61 64 20 6c  e [write-ahead l
55ad0 6f 67 5d 20 66 69 6c 65 2e 20 20 5e 50 61 73 73  og] file.  ^Pass
55ae0 69 6e 67 20 7a 65 72 6f 20 6f 72 20 0a 2a 2a 20  ing zero or .** 
55af0 61 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65  a negative value
55b00 20 61 73 20 74 68 65 20 6e 46 72 61 6d 65 20 70   as the nFrame p
55b10 61 72 61 6d 65 74 65 72 20 64 69 73 61 62 6c 65  arameter disable
55b20 73 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 63  s automatic.** c
55b30 68 65 63 6b 70 6f 69 6e 74 73 20 65 6e 74 69 72  heckpoints entir
55b40 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20  ely..**.** ^The 
55b50 63 61 6c 6c 62 61 63 6b 20 72 65 67 69 73 74 65  callback registe
55b60 72 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63  red by this func
55b70 74 69 6f 6e 20 72 65 70 6c 61 63 65 73 20 61 6e  tion replaces an
55b80 79 20 65 78 69 73 74 69 6e 67 20 63 61 6c 6c 62  y existing callb
55b90 61 63 6b 0a 2a 2a 20 72 65 67 69 73 74 65 72 65  ack.** registere
55ba0 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  d using [sqlite3
55bb0 5f 77 61 6c 5f 68 6f 6f 6b 28 29 5d 2e 20 20 5e  _wal_hook()].  ^
55bc0 4c 69 6b 65 77 69 73 65 2c 20 72 65 67 69 73 74  Likewise, regist
55bd0 65 72 69 6e 67 20 61 20 63 61 6c 6c 62 61 63 6b  ering a callback
55be0 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  .** using [sqlit
55bf0 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28 29 5d 20 64  e3_wal_hook()] d
55c00 69 73 61 62 6c 65 73 20 74 68 65 20 61 75 74 6f  isables the auto
55c10 6d 61 74 69 63 20 63 68 65 63 6b 70 6f 69 6e 74  matic checkpoint
55c20 20 6d 65 63 68 61 6e 69 73 6d 0a 2a 2a 20 63 6f   mechanism.** co
55c30 6e 66 69 67 75 72 65 64 20 62 79 20 74 68 69 73  nfigured by this
55c40 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
55c50 20 5e 54 68 65 20 5b 77 61 6c 5f 61 75 74 6f 63   ^The [wal_autoc
55c60 68 65 63 6b 70 6f 69 6e 74 20 70 72 61 67 6d 61  heckpoint pragma
55c70 5d 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  ] can be used to
55c80 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 69 6e 74   invoke this int
55c90 65 72 66 61 63 65 0a 2a 2a 20 66 72 6f 6d 20 53  erface.** from S
55ca0 51 4c 2e 0a 2a 2a 0a 2a 2a 20 5e 45 76 65 72 79  QL..**.** ^Every
55cb0 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20 63   new [database c
55cc0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 64 65 66 61 75  onnection] defau
55cd0 6c 74 73 20 74 6f 20 68 61 76 69 6e 67 20 74 68  lts to having th
55ce0 65 20 61 75 74 6f 2d 63 68 65 63 6b 70 6f 69 6e  e auto-checkpoin
55cf0 74 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 77 69 74  t.** enabled wit
55d00 68 20 61 20 74 68 72 65 73 68 6f 6c 64 20 6f 66  h a threshold of
55d10 20 31 30 30 30 20 6f 72 20 5b 53 51 4c 49 54 45   1000 or [SQLITE
55d20 5f 44 45 46 41 55 4c 54 5f 57 41 4c 5f 41 55 54  _DEFAULT_WAL_AUT
55d30 4f 43 48 45 43 4b 50 4f 49 4e 54 5d 0a 2a 2a 20  OCHECKPOINT].** 
55d40 70 61 67 65 73 2e 20 20 54 68 65 20 75 73 65 20  pages.  The use 
55d50 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  of this interfac
55d60 65 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 6e 65 63  e.** is only nec
55d70 65 73 73 61 72 79 20 69 66 20 74 68 65 20 64 65  essary if the de
55d80 66 61 75 6c 74 20 73 65 74 74 69 6e 67 20 69 73  fault setting is
55d90 20 66 6f 75 6e 64 20 74 6f 20 62 65 20 73 75 62   found to be sub
55da0 6f 70 74 69 6d 61 6c 0a 2a 2a 20 66 6f 72 20 61  optimal.** for a
55db0 20 70 61 72 74 69 63 75 6c 61 72 20 61 70 70 6c   particular appl
55dc0 69 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  ication..*/.SQLI
55dd0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
55de0 65 33 5f 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b  e3_wal_autocheck
55df0 70 6f 69 6e 74 28 73 71 6c 69 74 65 33 20 2a 64  point(sqlite3 *d
55e00 62 2c 20 69 6e 74 20 4e 29 3b 0a 0a 2f 2a 0a 2a  b, int N);../*.*
55e10 2a 20 43 41 50 49 33 52 45 46 3a 20 43 68 65 63  * CAPI3REF: Chec
55e20 6b 70 6f 69 6e 74 20 61 20 64 61 74 61 62 61 73  kpoint a databas
55e30 65 0a 2a 2a 0a 2a 2a 20 5e 54 68 65 20 5b 73 71  e.**.** ^The [sq
55e40 6c 69 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70  lite3_wal_checkp
55e50 6f 69 6e 74 28 44 2c 58 29 5d 20 69 6e 74 65 72  oint(D,X)] inter
55e60 66 61 63 65 20 63 61 75 73 65 73 20 64 61 74 61  face causes data
55e70 62 61 73 65 20 6e 61 6d 65 64 20 58 0a 2a 2a 20  base named X.** 
55e80 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  on [database con
55e90 6e 65 63 74 69 6f 6e 5d 20 44 20 74 6f 20 62 65  nection] D to be
55ea0 20 5b 63 68 65 63 6b 70 6f 69 6e 74 65 64 5d 2e   [checkpointed].
55eb0 20 20 5e 49 66 20 58 20 69 73 20 4e 55 4c 4c 20    ^If X is NULL 
55ec0 6f 72 20 61 6e 0a 2a 2a 20 65 6d 70 74 79 20 73  or an.** empty s
55ed0 74 72 69 6e 67 2c 20 74 68 65 6e 20 61 20 63 68  tring, then a ch
55ee0 65 63 6b 70 6f 69 6e 74 20 69 73 20 72 75 6e 20  eckpoint is run 
55ef0 6f 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  on all databases
55f00 20 6f 66 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f   of.** connectio
55f10 6e 20 44 2e 20 20 5e 49 66 20 74 68 65 20 64 61  n D.  ^If the da
55f20 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
55f30 6e 20 44 20 69 73 20 6e 6f 74 20 69 6e 0a 2a 2a  n D is not in.**
55f40 20 5b 57 41 4c 20 7c 20 77 72 69 74 65 2d 61 68   [WAL | write-ah
55f50 65 61 64 20 6c 6f 67 20 6d 6f 64 65 5d 20 74 68  ead log mode] th
55f60 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  en this interfac
55f70 65 20 69 73 20 61 20 68 61 72 6d 6c 65 73 73 20  e is a harmless 
55f80 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 5e 54 68  no-op..**.** ^Th
55f90 65 20 5b 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e  e [wal_checkpoin
55fa0 74 20 70 72 61 67 6d 61 5d 20 63 61 6e 20 62 65  t pragma] can be
55fb0 20 75 73 65 64 20 74 6f 20 69 6e 76 6f 6b 65 20   used to invoke 
55fc0 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 0a 2a  this interface.*
55fd0 2a 20 66 72 6f 6d 20 53 51 4c 2e 20 20 5e 54 68  * from SQL.  ^Th
55fe0 65 20 5b 73 71 6c 69 74 65 33 5f 77 61 6c 5f 61  e [sqlite3_wal_a
55ff0 75 74 6f 63 68 65 63 6b 70 6f 69 6e 74 28 29 5d  utocheckpoint()]
56000 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20 74   interface and t
56010 68 65 0a 2a 2a 20 5b 77 61 6c 5f 61 75 74 6f 63  he.** [wal_autoc
56020 68 65 63 6b 70 6f 69 6e 74 20 70 72 61 67 6d 61  heckpoint pragma
56030 5d 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  ] can be used to
56040 20 63 61 75 73 65 20 74 68 69 73 20 69 6e 74 65   cause this inte
56050 72 66 61 63 65 20 74 6f 20 62 65 0a 2a 2a 20 72  rface to be.** r
56060 75 6e 20 77 68 65 6e 65 76 65 72 20 74 68 65 20  un whenever the 
56070 57 41 4c 20 72 65 61 63 68 65 73 20 61 20 63 65  WAL reaches a ce
56080 72 74 61 69 6e 20 73 69 7a 65 20 74 68 72 65 73  rtain size thres
56090 68 6f 6c 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  hold..**.** See 
560a0 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 77  also: [sqlite3_w
560b0 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 5f 76 32  al_checkpoint_v2
560c0 28 29 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ()].*/.SQLITE_AP
560d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 77 61  I int sqlite3_wa
560e0 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 28 73 71 6c  l_checkpoint(sql
560f0 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
56100 63 68 61 72 20 2a 7a 44 62 29 3b 0a 0a 2f 2a 0a  char *zDb);../*.
56110 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 68 65  ** CAPI3REF: Che
56120 63 6b 70 6f 69 6e 74 20 61 20 64 61 74 61 62 61  ckpoint a databa
56130 73 65 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 61 20 63  se.**.** Run a c
56140 68 65 63 6b 70 6f 69 6e 74 20 6f 70 65 72 61 74  heckpoint operat
56150 69 6f 6e 20 6f 6e 20 57 41 4c 20 64 61 74 61 62  ion on WAL datab
56160 61 73 65 20 7a 44 62 20 61 74 74 61 63 68 65 64  ase zDb attached
56170 20 74 6f 20 64 61 74 61 62 61 73 65 20 0a 2a 2a   to database .**
56180 20 68 61 6e 64 6c 65 20 64 62 2e 20 54 68 65 20   handle db. The 
56190 73 70 65 63 69 66 69 63 20 6f 70 65 72 61 74 69  specific operati
561a0 6f 6e 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  on is determined
561b0 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 6f 66   by the value of
561c0 20 74 68 65 20 0a 2a 2a 20 65 4d 6f 64 65 20 70   the .** eMode p
561d0 61 72 61 6d 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20  arameter:.**.** 
561e0 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  <dl>.** <dt>SQLI
561f0 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 50 41  TE_CHECKPOINT_PA
56200 53 53 49 56 45 3c 64 64 3e 0a 2a 2a 20 20 20 43  SSIVE<dd>.**   C
56210 68 65 63 6b 70 6f 69 6e 74 20 61 73 20 6d 61 6e  heckpoint as man
56220 79 20 66 72 61 6d 65 73 20 61 73 20 70 6f 73 73  y frames as poss
56230 69 62 6c 65 20 77 69 74 68 6f 75 74 20 77 61 69  ible without wai
56240 74 69 6e 67 20 66 6f 72 20 61 6e 79 20 64 61 74  ting for any dat
56250 61 62 61 73 65 20 0a 2a 2a 20 20 20 72 65 61 64  abase .**   read
56260 65 72 73 20 6f 72 20 77 72 69 74 65 72 73 20 74  ers or writers t
56270 6f 20 66 69 6e 69 73 68 2e 20 53 79 6e 63 20 74  o finish. Sync t
56280 68 65 20 64 62 20 66 69 6c 65 20 69 66 20 61 6c  he db file if al
56290 6c 20 66 72 61 6d 65 73 20 69 6e 20 74 68 65 20  l frames in the 
562a0 6c 6f 67 0a 2a 2a 20 20 20 61 72 65 20 63 68 65  log.**   are che
562b0 63 6b 70 6f 69 6e 74 65 64 2e 20 54 68 69 73 20  ckpointed. This 
562c0 6d 6f 64 65 20 69 73 20 74 68 65 20 73 61 6d 65  mode is the same
562d0 20 61 73 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20   as calling .** 
562e0 20 20 73 71 6c 69 74 65 33 5f 77 61 6c 5f 63 68    sqlite3_wal_ch
562f0 65 63 6b 70 6f 69 6e 74 28 29 2e 20 54 68 65 20  eckpoint(). The 
56300 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c  busy-handler cal
56310 6c 62 61 63 6b 20 69 73 20 6e 65 76 65 72 20 69  lback is never i
56320 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64  nvoked..**.** <d
56330 74 3e 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f  t>SQLITE_CHECKPO
56340 49 4e 54 5f 46 55 4c 4c 3c 64 64 3e 0a 2a 2a 20  INT_FULL<dd>.** 
56350 20 20 54 68 69 73 20 6d 6f 64 65 20 62 6c 6f 63    This mode bloc
56360 6b 73 20 28 63 61 6c 6c 73 20 74 68 65 20 62 75  ks (calls the bu
56370 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62  sy-handler callb
56380 61 63 6b 29 20 75 6e 74 69 6c 20 74 68 65 72 65  ack) until there
56390 20 69 73 20 6e 6f 0a 2a 2a 20 20 20 64 61 74 61   is no.**   data
563a0 62 61 73 65 20 77 72 69 74 65 72 20 61 6e 64 20  base writer and 
563b0 61 6c 6c 20 72 65 61 64 65 72 73 20 61 72 65 20  all readers are 
563c0 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65  reading from the
563d0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 64 61 74   most recent dat
563e0 61 62 61 73 65 0a 2a 2a 20 20 20 73 6e 61 70 73  abase.**   snaps
563f0 68 6f 74 2e 20 49 74 20 74 68 65 6e 20 63 68 65  hot. It then che
56400 63 6b 70 6f 69 6e 74 73 20 61 6c 6c 20 66 72 61  ckpoints all fra
56410 6d 65 73 20 69 6e 20 74 68 65 20 6c 6f 67 20 66  mes in the log f
56420 69 6c 65 20 61 6e 64 20 73 79 6e 63 73 20 74 68  ile and syncs th
56430 65 0a 2a 2a 20 20 20 64 61 74 61 62 61 73 65 20  e.**   database 
56440 66 69 6c 65 2e 20 54 68 69 73 20 63 61 6c 6c 20  file. This call 
56450 62 6c 6f 63 6b 73 20 64 61 74 61 62 61 73 65 20  blocks database 
56460 77 72 69 74 65 72 73 20 77 68 69 6c 65 20 69 74  writers while it
56470 20 69 73 20 72 75 6e 6e 69 6e 67 2c 0a 2a 2a 20   is running,.** 
56480 20 20 62 75 74 20 6e 6f 74 20 64 61 74 61 62 61    but not databa
56490 73 65 20 72 65 61 64 65 72 73 2e 0a 2a 2a 0a 2a  se readers..**.*
564a0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 48 45  * <dt>SQLITE_CHE
564b0 43 4b 50 4f 49 4e 54 5f 52 45 53 54 41 52 54 3c  CKPOINT_RESTART<
564c0 64 64 3e 0a 2a 2a 20 20 20 54 68 69 73 20 6d 6f  dd>.**   This mo
564d0 64 65 20 77 6f 72 6b 73 20 74 68 65 20 73 61 6d  de works the sam
564e0 65 20 77 61 79 20 61 73 20 53 51 4c 49 54 45 5f  e way as SQLITE_
564f0 43 48 45 43 4b 50 4f 49 4e 54 5f 46 55 4c 4c 2c  CHECKPOINT_FULL,
56500 20 65 78 63 65 70 74 20 61 66 74 65 72 20 0a 2a   except after .*
56510 2a 20 20 20 63 68 65 63 6b 70 6f 69 6e 74 69 6e  *   checkpointin
56520 67 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 20 69  g the log file i
56530 74 20 62 6c 6f 63 6b 73 20 28 63 61 6c 6c 73 20  t blocks (calls 
56540 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72  the busy-handler
56550 20 63 61 6c 6c 62 61 63 6b 29 0a 2a 2a 20 20 20   callback).**   
56560 75 6e 74 69 6c 20 61 6c 6c 20 72 65 61 64 65 72  until all reader
56570 73 20 61 72 65 20 72 65 61 64 69 6e 67 20 66 72  s are reading fr
56580 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
56590 66 69 6c 65 20 6f 6e 6c 79 2e 20 54 68 69 73 20  file only. This 
565a0 65 6e 73 75 72 65 73 20 0a 2a 2a 20 20 20 74 68  ensures .**   th
565b0 61 74 20 74 68 65 20 6e 65 78 74 20 63 6c 69 65  at the next clie
565c0 6e 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74  nt to write to t
565d0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
565e0 20 72 65 73 74 61 72 74 73 20 74 68 65 20 6c 6f   restarts the lo
565f0 67 20 66 69 6c 65 20 0a 2a 2a 20 20 20 66 72 6f  g file .**   fro
56600 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 2e  m the beginning.
56610 20 54 68 69 73 20 63 61 6c 6c 20 62 6c 6f 63 6b   This call block
56620 73 20 64 61 74 61 62 61 73 65 20 77 72 69 74 65  s database write
56630 72 73 20 77 68 69 6c 65 20 69 74 20 69 73 20 72  rs while it is r
56640 75 6e 6e 69 6e 67 2c 0a 2a 2a 20 20 20 62 75 74  unning,.**   but
56650 20 6e 6f 74 20 64 61 74 61 62 61 73 65 20 72 65   not database re
56660 61 64 65 72 73 2e 0a 2a 2a 20 3c 2f 64 6c 3e 0a  aders..** </dl>.
56670 2a 2a 0a 2a 2a 20 49 66 20 70 6e 4c 6f 67 20 69  **.** If pnLog i
56680 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
56690 20 2a 70 6e 4c 6f 67 20 69 73 20 73 65 74 20 74   *pnLog is set t
566a0 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  o the total numb
566b0 65 72 20 6f 66 20 66 72 61 6d 65 73 20 69 6e 0a  er of frames in.
566c0 2a 2a 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 20  ** the log file 
566d0 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
566e0 2e 20 49 66 20 70 6e 43 6b 70 74 20 69 73 20 6e  . If pnCkpt is n
566f0 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 2a 70  ot NULL, then *p
56700 6e 43 6b 70 74 20 69 73 20 73 65 74 20 74 6f 0a  nCkpt is set to.
56710 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  ** the total num
56720 62 65 72 20 6f 66 20 63 68 65 63 6b 70 6f 69 6e  ber of checkpoin
56730 74 65 64 20 66 72 61 6d 65 73 20 28 69 6e 63 6c  ted frames (incl
56740 75 64 69 6e 67 20 61 6e 79 20 74 68 61 74 20 77  uding any that w
56750 65 72 65 20 61 6c 72 65 61 64 79 0a 2a 2a 20 63  ere already.** c
56760 68 65 63 6b 70 6f 69 6e 74 65 64 20 77 68 65 6e  heckpointed when
56770 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
56780 73 20 63 61 6c 6c 65 64 29 2e 20 2a 70 6e 4c 6f  s called). *pnLo
56790 67 20 61 6e 64 20 2a 70 6e 43 6b 70 74 20 6d 61  g and *pnCkpt ma
567a0 79 20 62 65 0a 2a 2a 20 70 6f 70 75 6c 61 74 65  y be.** populate
567b0 64 20 65 76 65 6e 20 69 66 20 73 71 6c 69 74 65  d even if sqlite
567c0 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74  3_wal_checkpoint
567d0 5f 76 32 28 29 20 72 65 74 75 72 6e 73 20 6f 74  _v2() returns ot
567e0 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  her than SQLITE_
567f0 4f 4b 2e 0a 2a 2a 20 49 66 20 6e 6f 20 76 61 6c  OK..** If no val
56800 75 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c  ues are availabl
56810 65 20 62 65 63 61 75 73 65 20 6f 66 20 61 6e 20  e because of an 
56820 65 72 72 6f 72 2c 20 74 68 65 79 20 61 72 65 20  error, they are 
56830 62 6f 74 68 20 73 65 74 20 74 6f 20 2d 31 0a 2a  both set to -1.*
56840 2a 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  * before returni
56850 6e 67 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74  ng to communicat
56860 65 20 74 68 69 73 20 74 6f 20 74 68 65 20 63 61  e this to the ca
56870 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20  ller..**.** All 
56880 63 61 6c 6c 73 20 6f 62 74 61 69 6e 20 61 6e 20  calls obtain an 
56890 65 78 63 6c 75 73 69 76 65 20 22 63 68 65 63 6b  exclusive "check
568a0 70 6f 69 6e 74 22 20 6c 6f 63 6b 20 6f 6e 20 74  point" lock on t
568b0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
568c0 2e 20 49 66 0a 2a 2a 20 61 6e 79 20 6f 74 68 65  . If.** any othe
568d0 72 20 70 72 6f 63 65 73 73 20 69 73 20 72 75 6e  r process is run
568e0 6e 69 6e 67 20 61 20 63 68 65 63 6b 70 6f 69 6e  ning a checkpoin
568f0 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 74 20 74  t operation at t
56900 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 74 68  he same time, th
56910 65 20 0a 2a 2a 20 6c 6f 63 6b 20 63 61 6e 6e 6f  e .** lock canno
56920 74 20 62 65 20 6f 62 74 61 69 6e 65 64 20 61 6e  t be obtained an
56930 64 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73  d SQLITE_BUSY is
56940 20 72 65 74 75 72 6e 65 64 2e 20 45 76 65 6e 20   returned. Even 
56950 69 66 20 74 68 65 72 65 20 69 73 20 61 20 0a 2a  if there is a .*
56960 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63  * busy-handler c
56970 6f 6e 66 69 67 75 72 65 64 2c 20 69 74 20 77 69  onfigured, it wi
56980 6c 6c 20 6e 6f 74 20 62 65 20 69 6e 76 6f 6b 65  ll not be invoke
56990 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  d in this case..
569a0 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45  **.** The SQLITE
569b0 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 46 55 4c 4c  _CHECKPOINT_FULL
569c0 20 61 6e 64 20 52 45 53 54 41 52 54 20 6d 6f 64   and RESTART mod
569d0 65 73 20 61 6c 73 6f 20 6f 62 74 61 69 6e 20 74  es also obtain t
569e0 68 65 20 65 78 63 6c 75 73 69 76 65 20 0a 2a 2a  he exclusive .**
569f0 20 22 77 72 69 74 65 72 22 20 6c 6f 63 6b 20 6f   "writer" lock o
56a00 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
56a10 69 6c 65 2e 20 49 66 20 74 68 65 20 77 72 69 74  ile. If the writ
56a20 65 72 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 62  er lock cannot b
56a30 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 69 6d  e obtained.** im
56a40 6d 65 64 69 61 74 65 6c 79 2c 20 61 6e 64 20 61  mediately, and a
56a50 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 73   busy-handler is
56a60 20 63 6f 6e 66 69 67 75 72 65 64 2c 20 69 74 20   configured, it 
56a70 69 73 20 69 6e 76 6f 6b 65 64 20 61 6e 64 20 74  is invoked and t
56a80 68 65 20 77 72 69 74 65 72 0a 2a 2a 20 6c 6f 63  he writer.** loc
56a90 6b 20 72 65 74 72 69 65 64 20 75 6e 74 69 6c 20  k retried until 
56aa0 65 69 74 68 65 72 20 74 68 65 20 62 75 73 79 2d  either the busy-
56ab0 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 20  handler returns 
56ac0 30 20 6f 72 20 74 68 65 20 6c 6f 63 6b 20 69 73  0 or the lock is
56ad0 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79  .** successfully
56ae0 20 6f 62 74 61 69 6e 65 64 2e 20 54 68 65 20 62   obtained. The b
56af0 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 73 20 61  usy-handler is a
56b00 6c 73 6f 20 69 6e 76 6f 6b 65 64 20 77 68 69 6c  lso invoked whil
56b10 65 20 77 61 69 74 69 6e 67 20 66 6f 72 0a 2a 2a  e waiting for.**
56b20 20 64 61 74 61 62 61 73 65 20 72 65 61 64 65 72   database reader
56b30 73 20 61 73 20 64 65 73 63 72 69 62 65 64 20 61  s as described a
56b40 62 6f 76 65 2e 20 49 66 20 74 68 65 20 62 75 73  bove. If the bus
56b50 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75 72 6e  y-handler return
56b60 73 20 30 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68  s 0 before.** th
56b70 65 20 77 72 69 74 65 72 20 6c 6f 63 6b 20 69 73  e writer lock is
56b80 20 6f 62 74 61 69 6e 65 64 20 6f 72 20 77 68 69   obtained or whi
56b90 6c 65 20 77 61 69 74 69 6e 67 20 66 6f 72 20 64  le waiting for d
56ba0 61 74 61 62 61 73 65 20 72 65 61 64 65 72 73 2c  atabase readers,
56bb0 20 74 68 65 0a 2a 2a 20 63 68 65 63 6b 70 6f 69   the.** checkpoi
56bc0 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 70 72 6f  nt operation pro
56bd0 63 65 65 64 73 20 66 72 6f 6d 20 74 68 61 74 20  ceeds from that 
56be0 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 73 61 6d  point in the sam
56bf0 65 20 77 61 79 20 61 73 20 0a 2a 2a 20 53 51 4c  e way as .** SQL
56c00 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 50  ITE_CHECKPOINT_P
56c10 41 53 53 49 56 45 20 2d 20 63 68 65 63 6b 70 6f  ASSIVE - checkpo
56c20 69 6e 74 69 6e 67 20 61 73 20 6d 61 6e 79 20 66  inting as many f
56c30 72 61 6d 65 73 20 61 73 20 70 6f 73 73 69 62 6c  rames as possibl
56c40 65 20 0a 2a 2a 20 77 69 74 68 6f 75 74 20 62 6c  e .** without bl
56c50 6f 63 6b 69 6e 67 20 61 6e 79 20 66 75 72 74 68  ocking any furth
56c60 65 72 2e 20 53 51 4c 49 54 45 5f 42 55 53 59 20  er. SQLITE_BUSY 
56c70 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74  is returned in t
56c80 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  his case..**.** 
56c90 49 66 20 70 61 72 61 6d 65 74 65 72 20 7a 44 62  If parameter zDb
56ca0 20 69 73 20 4e 55 4c 4c 20 6f 72 20 70 6f 69 6e   is NULL or poin
56cb0 74 73 20 74 6f 20 61 20 7a 65 72 6f 20 6c 65 6e  ts to a zero len
56cc0 67 74 68 20 73 74 72 69 6e 67 2c 20 74 68 65 6e  gth string, then
56cd0 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 65   the.** specifie
56ce0 64 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61  d operation is a
56cf0 74 74 65 6d 70 74 65 64 20 6f 6e 20 61 6c 6c 20  ttempted on all 
56d00 57 41 4c 20 64 61 74 61 62 61 73 65 73 2e 20 49  WAL databases. I
56d10 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 0a  n this case the.
56d20 2a 2a 20 76 61 6c 75 65 73 20 77 72 69 74 74 65  ** values writte
56d30 6e 20 74 6f 20 6f 75 74 70 75 74 20 70 61 72 61  n to output para
56d40 6d 65 74 65 72 73 20 2a 70 6e 4c 6f 67 20 61 6e  meters *pnLog an
56d50 64 20 2a 70 6e 43 6b 70 74 20 61 72 65 20 75 6e  d *pnCkpt are un
56d60 64 65 66 69 6e 65 64 2e 20 49 66 20 0a 2a 2a 20  defined. If .** 
56d70 61 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 65  an SQLITE_BUSY e
56d80 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65  rror is encounte
56d90 72 65 64 20 77 68 65 6e 20 70 72 6f 63 65 73 73  red when process
56da0 69 6e 67 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ing one or more 
56db0 6f 66 20 74 68 65 20 0a 2a 2a 20 61 74 74 61 63  of the .** attac
56dc0 68 65 64 20 57 41 4c 20 64 61 74 61 62 61 73 65  hed WAL database
56dd0 73 2c 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  s, the operation
56de0 20 69 73 20 73 74 69 6c 6c 20 61 74 74 65 6d 70   is still attemp
56df0 74 65 64 20 6f 6e 20 61 6e 79 20 72 65 6d 61 69  ted on any remai
56e00 6e 69 6e 67 20 0a 2a 2a 20 61 74 74 61 63 68 65  ning .** attache
56e10 64 20 64 61 74 61 62 61 73 65 73 20 61 6e 64 20  d databases and 
56e20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72  SQLITE_BUSY is r
56e30 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63  eturned to the c
56e40 61 6c 6c 65 72 2e 20 49 66 20 61 6e 79 20 6f 74  aller. If any ot
56e50 68 65 72 20 0a 2a 2a 20 65 72 72 6f 72 20 6f 63  her .** error oc
56e60 63 75 72 73 20 77 68 69 6c 65 20 70 72 6f 63 65  curs while proce
56e70 73 73 69 6e 67 20 61 6e 20 61 74 74 61 63 68 65  ssing an attache
56e80 64 20 64 61 74 61 62 61 73 65 2c 20 70 72 6f 63  d database, proc
56e90 65 73 73 69 6e 67 20 69 73 20 61 62 61 6e 64 6f  essing is abando
56ea0 6e 65 64 20 0a 2a 2a 20 61 6e 64 20 74 68 65 20  ned .** and the 
56eb0 65 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72  error code retur
56ec0 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ned to the calle
56ed0 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49  r immediately. I
56ee0 66 20 6e 6f 20 65 72 72 6f 72 20 0a 2a 2a 20 28  f no error .** (
56ef0 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 72 20 6f  SQLITE_BUSY or o
56f00 74 68 65 72 77 69 73 65 29 20 69 73 20 65 6e 63  therwise) is enc
56f10 6f 75 6e 74 65 72 65 64 20 77 68 69 6c 65 20 70  ountered while p
56f20 72 6f 63 65 73 73 69 6e 67 20 74 68 65 20 61 74  rocessing the at
56f30 74 61 63 68 65 64 20 0a 2a 2a 20 64 61 74 61 62  tached .** datab
56f40 61 73 65 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  ases, SQLITE_OK 
56f50 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
56f60 2a 2a 20 49 66 20 64 61 74 61 62 61 73 65 20 7a  ** If database z
56f70 44 62 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  Db is the name o
56f80 66 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61  f an attached da
56f90 74 61 62 61 73 65 20 74 68 61 74 20 69 73 20 6e  tabase that is n
56fa0 6f 74 20 69 6e 20 57 41 4c 0a 2a 2a 20 6d 6f 64  ot in WAL.** mod
56fb0 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  e, SQLITE_OK is 
56fc0 72 65 74 75 72 6e 65 64 20 61 6e 64 20 62 6f 74  returned and bot
56fd0 68 20 2a 70 6e 4c 6f 67 20 61 6e 64 20 2a 70 6e  h *pnLog and *pn
56fe0 43 6b 70 74 20 73 65 74 20 74 6f 20 2d 31 2e 20  Ckpt set to -1. 
56ff0 49 66 0a 2a 2a 20 7a 44 62 20 69 73 20 6e 6f 74  If.** zDb is not
57000 20 4e 55 4c 4c 20 28 6f 72 20 61 20 7a 65 72 6f   NULL (or a zero
57010 20 6c 65 6e 67 74 68 20 73 74 72 69 6e 67 29 20   length string) 
57020 61 6e 64 20 69 73 20 6e 6f 74 20 74 68 65 20 6e  and is not the n
57030 61 6d 65 20 6f 66 20 61 6e 79 0a 2a 2a 20 61 74  ame of any.** at
57040 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 2c  tached database,
57050 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73   SQLITE_ERROR is
57060 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
57070 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49   caller..*/.SQLI
57080 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
57090 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e  e3_wal_checkpoin
570a0 74 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20  t_v2(.  sqlite3 
570b0 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
570c0 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
570d0 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ase handle */.  
570e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c  const char *zDb,
570f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57100 2f 2a 20 4e 61 6d 65 20 6f 66 20 61 74 74 61 63  /* Name of attac
57110 68 65 64 20 64 61 74 61 62 61 73 65 20 28 6f 72  hed database (or
57120 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 69 6e 74 20   NULL) */.  int 
57130 65 4d 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20  eMode,          
57140 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
57150 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54  QLITE_CHECKPOINT
57160 5f 2a 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e  _* value */.  in
57170 74 20 2a 70 6e 4c 6f 67 2c 20 20 20 20 20 20 20  t *pnLog,       
57180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
57190 20 4f 55 54 3a 20 53 69 7a 65 20 6f 66 20 57 41   OUT: Size of WA
571a0 4c 20 6c 6f 67 20 69 6e 20 66 72 61 6d 65 73 20  L log in frames 
571b0 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6b 70 74  */.  int *pnCkpt
571c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
571d0 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74       /* OUT: Tot
571e0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 61  al number of fra
571f0 6d 65 73 20 63 68 65 63 6b 70 6f 69 6e 74 65 64  mes checkpointed
57200 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   */.);../*.** CA
57210 50 49 33 52 45 46 3a 20 43 68 65 63 6b 70 6f 69  PI3REF: Checkpoi
57220 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 70 61 72  nt operation par
57230 61 6d 65 74 65 72 73 0a 2a 2a 0a 2a 2a 20 54 68  ameters.**.** Th
57240 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 63 61  ese constants ca
57250 6e 20 62 65 20 75 73 65 64 20 61 73 20 74 68 65  n be used as the
57260 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74   3rd parameter t
57270 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 77 61  o.** [sqlite3_wa
57280 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 5f 76 32 28  l_checkpoint_v2(
57290 29 5d 2e 20 20 53 65 65 20 74 68 65 20 5b 73 71  )].  See the [sq
572a0 6c 69 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70  lite3_wal_checkp
572b0 6f 69 6e 74 5f 76 32 28 29 5d 0a 2a 2a 20 64 6f  oint_v2()].** do
572c0 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  cumentation for 
572d0 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
572e0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
572f0 20 6d 65 61 6e 69 6e 67 20 61 6e 64 20 75 73 65   meaning and use
57300 20 6f 66 0a 2a 2a 20 65 61 63 68 20 6f 66 20 74   of.** each of t
57310 68 65 73 65 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a  hese values..*/.
57320 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
57330 48 45 43 4b 50 4f 49 4e 54 5f 50 41 53 53 49 56  HECKPOINT_PASSIV
57340 45 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  E 0.#define SQLI
57350 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 46 55  TE_CHECKPOINT_FU
57360 4c 4c 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20  LL    1.#define 
57370 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e  SQLITE_CHECKPOIN
57380 54 5f 52 45 53 54 41 52 54 20 32 0a 0a 2f 2a 0a  T_RESTART 2../*.
57390 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72  ** CAPI3REF: Vir
573a0 74 75 61 6c 20 54 61 62 6c 65 20 49 6e 74 65 72  tual Table Inter
573b0 66 61 63 65 20 43 6f 6e 66 69 67 75 72 61 74 69  face Configurati
573c0 6f 6e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  on.**.** This fu
573d0 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61  nction may be ca
573e0 6c 6c 65 64 20 62 79 20 65 69 74 68 65 72 20 74  lled by either t
573f0 68 65 20 5b 78 43 6f 6e 6e 65 63 74 5d 20 6f 72  he [xConnect] or
57400 20 5b 78 43 72 65 61 74 65 5d 20 6d 65 74 68 6f   [xCreate] metho
57410 64 0a 2a 2a 20 6f 66 20 61 20 5b 76 69 72 74 75  d.** of a [virtu
57420 61 6c 20 74 61 62 6c 65 5d 20 69 6d 70 6c 65 6d  al table] implem
57430 65 6e 74 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 66  entation to conf
57440 69 67 75 72 65 0a 2a 2a 20 76 61 72 69 6f 75 73  igure.** various
57450 20 66 61 63 65 74 73 20 6f 66 20 74 68 65 20 76   facets of the v
57460 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74  irtual table int
57470 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  erface..**.** If
57480 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   this interface 
57490 69 73 20 69 6e 76 6f 6b 65 64 20 6f 75 74 73 69  is invoked outsi
574a0 64 65 20 74 68 65 20 63 6f 6e 74 65 78 74 20 6f  de the context o
574b0 66 20 61 6e 20 78 43 6f 6e 6e 65 63 74 20 6f 72  f an xConnect or
574c0 0a 2a 2a 20 78 43 72 65 61 74 65 20 76 69 72 74  .** xCreate virt
574d0 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64  ual table method
574e0 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69   then the behavi
574f0 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  or is undefined.
57500 0a 2a 2a 0a 2a 2a 20 41 74 20 70 72 65 73 65 6e  .**.** At presen
57510 74 2c 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79  t, there is only
57520 20 6f 6e 65 20 6f 70 74 69 6f 6e 20 74 68 61 74   one option that
57530 20 6d 61 79 20 62 65 20 63 6f 6e 66 69 67 75 72   may be configur
57540 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 69 73  ed using.** this
57550 20 66 75 6e 63 74 69 6f 6e 2e 20 28 53 65 65 20   function. (See 
57560 5b 53 51 4c 49 54 45 5f 56 54 41 42 5f 43 4f 4e  [SQLITE_VTAB_CON
57570 53 54 52 41 49 4e 54 5f 53 55 50 50 4f 52 54 5d  STRAINT_SUPPORT]
57580 2e 29 20 20 46 75 72 74 68 65 72 20 6f 70 74 69  .)  Further opti
57590 6f 6e 73 0a 2a 2a 20 6d 61 79 20 62 65 20 61 64  ons.** may be ad
575a0 64 65 64 20 69 6e 20 74 68 65 20 66 75 74 75 72  ded in the futur
575b0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
575c0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 74 61   int sqlite3_vta
575d0 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74 65 33  b_config(sqlite3
575e0 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b  *, int op, ...);
575f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
57600 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20  : Virtual Table 
57610 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70  Configuration Op
57620 74 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 54 68 65 73  tions.**.** Thes
57630 65 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20  e macros define 
57640 74 68 65 20 76 61 72 69 6f 75 73 20 6f 70 74 69  the various opti
57650 6f 6e 73 20 74 6f 20 74 68 65 0a 2a 2a 20 5b 73  ons to the.** [s
57660 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 6f 6e 66  qlite3_vtab_conf
57670 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  ig()] interface 
57680 74 68 61 74 20 5b 76 69 72 74 75 61 6c 20 74 61  that [virtual ta
57690 62 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ble] implementat
576a0 69 6f 6e 73 0a 2a 2a 20 63 61 6e 20 75 73 65 20  ions.** can use 
576b0 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 61 6e 64  to customize and
576c0 20 6f 70 74 69 6d 69 7a 65 20 74 68 65 69 72 20   optimize their 
576d0 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20  behavior..**.** 
576e0 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  <dl>.** <dt>SQLI
576f0 54 45 5f 56 54 41 42 5f 43 4f 4e 53 54 52 41 49  TE_VTAB_CONSTRAI
57700 4e 54 5f 53 55 50 50 4f 52 54 0a 2a 2a 20 3c 64  NT_SUPPORT.** <d
57710 64 3e 43 61 6c 6c 73 20 6f 66 20 74 68 65 20 66  d>Calls of the f
57720 6f 72 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  orm.** [sqlite3_
57730 76 74 61 62 5f 63 6f 6e 66 69 67 5d 28 64 62 2c  vtab_config](db,
57740 53 51 4c 49 54 45 5f 56 54 41 42 5f 43 4f 4e 53  SQLITE_VTAB_CONS
57750 54 52 41 49 4e 54 5f 53 55 50 50 4f 52 54 2c 58  TRAINT_SUPPORT,X
57760 29 20 61 72 65 20 73 75 70 70 6f 72 74 65 64 2c  ) are supported,
57770 0a 2a 2a 20 77 68 65 72 65 20 58 20 69 73 20 61  .** where X is a
57780 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20 58  n integer.  If X
57790 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74   is zero, then t
577a0 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c  he [virtual tabl
577b0 65 5d 20 77 68 6f 73 65 0a 2a 2a 20 5b 78 43 72  e] whose.** [xCr
577c0 65 61 74 65 5d 20 6f 72 20 5b 78 43 6f 6e 6e 65  eate] or [xConne
577d0 63 74 5d 20 6d 65 74 68 6f 64 20 69 6e 76 6f 6b  ct] method invok
577e0 65 64 20 5b 73 71 6c 69 74 65 33 5f 76 74 61 62  ed [sqlite3_vtab
577f0 5f 63 6f 6e 66 69 67 28 29 5d 20 64 6f 65 73 20  _config()] does 
57800 6e 6f 74 0a 2a 2a 20 73 75 70 70 6f 72 74 20 63  not.** support c
57810 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 49 6e 20  onstraints.  In 
57820 74 68 69 73 20 63 6f 6e 66 69 67 75 72 61 74 69  this configurati
57830 6f 6e 20 28 77 68 69 63 68 20 69 73 20 74 68 65  on (which is the
57840 20 64 65 66 61 75 6c 74 29 20 69 66 0a 2a 2a 20   default) if.** 
57850 61 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 5b 78  a call to the [x
57860 55 70 64 61 74 65 5d 20 6d 65 74 68 6f 64 20 72  Update] method r
57870 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 43  eturns [SQLITE_C
57880 4f 4e 53 54 52 41 49 4e 54 5d 2c 20 74 68 65 6e  ONSTRAINT], then
57890 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20 73   the entire.** s
578a0 74 61 74 65 6d 65 6e 74 20 69 73 20 72 6f 6c 6c  tatement is roll
578b0 65 64 20 62 61 63 6b 20 61 73 20 69 66 20 5b 4f  ed back as if [O
578c0 4e 20 43 4f 4e 46 4c 49 43 54 20 7c 20 4f 52 20  N CONFLICT | OR 
578d0 41 42 4f 52 54 5d 20 68 61 64 20 62 65 65 6e 0a  ABORT] had been.
578e0 2a 2a 20 73 70 65 63 69 66 69 65 64 20 61 73 20  ** specified as 
578f0 70 61 72 74 20 6f 66 20 74 68 65 20 75 73 65 72  part of the user
57900 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c  s SQL statement,
57910 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74   regardless of t
57920 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 4f 4e 20  he actual.** ON 
57930 43 4f 4e 46 4c 49 43 54 20 6d 6f 64 65 20 73 70  CONFLICT mode sp
57940 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  ecified..**.** I
57950 66 20 58 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  f X is non-zero,
57960 20 74 68 65 6e 20 74 68 65 20 76 69 72 74 75 61   then the virtua
57970 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  l table implemen
57980 74 61 74 69 6f 6e 20 67 75 61 72 61 6e 74 65 65  tation guarantee
57990 73 0a 2a 2a 20 74 68 61 74 20 69 66 20 5b 78 55  s.** that if [xU
579a0 70 64 61 74 65 5d 20 72 65 74 75 72 6e 73 20 5b  pdate] returns [
579b0 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
579c0 54 5d 2c 20 69 74 20 77 69 6c 6c 20 64 6f 20 73  T], it will do s
579d0 6f 20 62 65 66 6f 72 65 0a 2a 2a 20 61 6e 79 20  o before.** any 
579e0 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 74 6f  modifications to
579f0 20 69 6e 74 65 72 6e 61 6c 20 6f 72 20 70 65 72   internal or per
57a00 73 69 73 74 65 6e 74 20 64 61 74 61 20 73 74 72  sistent data str
57a10 75 63 74 75 72 65 73 20 68 61 76 65 20 62 65 65  uctures have bee
57a20 6e 20 6d 61 64 65 2e 0a 2a 2a 20 49 66 20 74 68  n made..** If th
57a30 65 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d 20  e [ON CONFLICT] 
57a40 6d 6f 64 65 20 69 73 20 41 42 4f 52 54 2c 20 46  mode is ABORT, F
57a50 41 49 4c 2c 20 49 47 4e 4f 52 45 20 6f 72 20 52  AIL, IGNORE or R
57a60 4f 4c 4c 42 41 43 4b 2c 20 53 51 4c 69 74 65 20  OLLBACK, SQLite 
57a70 0a 2a 2a 20 69 73 20 61 62 6c 65 20 74 6f 20 72  .** is able to r
57a80 6f 6c 6c 20 62 61 63 6b 20 61 20 73 74 61 74 65  oll back a state
57a90 6d 65 6e 74 20 6f 72 20 64 61 74 61 62 61 73 65  ment or database
57aa0 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e   transaction, an
57ab0 64 20 61 62 61 6e 64 6f 6e 0a 2a 2a 20 6f 72 20  d abandon.** or 
57ac0 63 6f 6e 74 69 6e 75 65 20 70 72 6f 63 65 73 73  continue process
57ad0 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20  ing the current 
57ae0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 73  SQL statement as
57af0 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 0a 2a   appropriate. .*
57b00 2a 20 49 66 20 74 68 65 20 4f 4e 20 43 4f 4e 46  * If the ON CONF
57b10 4c 49 43 54 20 6d 6f 64 65 20 69 73 20 52 45 50  LICT mode is REP
57b20 4c 41 43 45 20 61 6e 64 20 74 68 65 20 5b 78 55  LACE and the [xU
57b30 70 64 61 74 65 5d 20 6d 65 74 68 6f 64 20 72 65  pdate] method re
57b40 74 75 72 6e 73 0a 2a 2a 20 5b 53 51 4c 49 54 45  turns.** [SQLITE
57b50 5f 43 4f 4e 53 54 52 41 49 4e 54 5d 2c 20 53 51  _CONSTRAINT], SQ
57b60 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 68 69  Lite handles thi
57b70 73 20 61 73 20 69 66 20 74 68 65 20 4f 4e 20 43  s as if the ON C
57b80 4f 4e 46 4c 49 43 54 20 6d 6f 64 65 0a 2a 2a 20  ONFLICT mode.** 
57b90 68 61 64 20 62 65 65 6e 20 41 42 4f 52 54 2e 0a  had been ABORT..
57ba0 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61  **.** Virtual ta
57bb0 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
57bc0 6f 6e 73 20 74 68 61 74 20 61 72 65 20 72 65 71  ons that are req
57bd0 75 69 72 65 64 20 74 6f 20 68 61 6e 64 6c 65 20  uired to handle 
57be0 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a 20 6d 75  OR REPLACE.** mu
57bf0 73 74 20 64 6f 20 73 6f 20 77 69 74 68 69 6e 20  st do so within 
57c00 74 68 65 20 5b 78 55 70 64 61 74 65 5d 20 6d 65  the [xUpdate] me
57c10 74 68 6f 64 2e 20 49 66 20 61 20 63 61 6c 6c 20  thod. If a call 
57c20 74 6f 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c 69  to the .** [sqli
57c30 74 65 33 5f 76 74 61 62 5f 6f 6e 5f 63 6f 6e 66  te3_vtab_on_conf
57c40 6c 69 63 74 28 29 5d 20 66 75 6e 63 74 69 6f 6e  lict()] function
57c50 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
57c60 74 68 65 20 63 75 72 72 65 6e 74 20 4f 4e 20 0a  the current ON .
57c70 2a 2a 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69  ** CONFLICT poli
57c80 63 79 20 69 73 20 52 45 50 4c 41 43 45 2c 20 74  cy is REPLACE, t
57c90 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
57ca0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
57cb0 73 68 6f 75 6c 64 20 0a 2a 2a 20 73 69 6c 65 6e  should .** silen
57cc0 74 6c 79 20 72 65 70 6c 61 63 65 20 74 68 65 20  tly replace the 
57cd0 61 70 70 72 6f 70 72 69 61 74 65 20 72 6f 77 73  appropriate rows
57ce0 20 77 69 74 68 69 6e 20 74 68 65 20 78 55 70 64   within the xUpd
57cf0 61 74 65 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64  ate callback and
57d00 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54  .** return SQLIT
57d10 45 5f 4f 4b 2e 20 4f 72 2c 20 69 66 20 74 68 69  E_OK. Or, if thi
57d20 73 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  s is not possibl
57d30 65 2c 20 69 74 20 6d 61 79 20 72 65 74 75 72 6e  e, it may return
57d40 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54  .** SQLITE_CONST
57d50 52 41 49 4e 54 2c 20 69 6e 20 77 68 69 63 68 20  RAINT, in which 
57d60 63 61 73 65 20 53 51 4c 69 74 65 20 66 61 6c 6c  case SQLite fall
57d70 73 20 62 61 63 6b 20 74 6f 20 4f 52 20 41 42 4f  s back to OR ABO
57d80 52 54 20 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e  RT .** constrain
57d90 74 20 68 61 6e 64 6c 69 6e 67 2e 0a 2a 2a 20 3c  t handling..** <
57da0 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  /dl>.*/.#define 
57db0 53 51 4c 49 54 45 5f 56 54 41 42 5f 43 4f 4e 53  SQLITE_VTAB_CONS
57dc0 54 52 41 49 4e 54 5f 53 55 50 50 4f 52 54 20 31  TRAINT_SUPPORT 1
57dd0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
57de0 3a 20 44 65 74 65 72 6d 69 6e 65 20 54 68 65 20  : Determine The 
57df0 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 43 6f  Virtual Table Co
57e00 6e 66 6c 69 63 74 20 50 6f 6c 69 63 79 0a 2a 2a  nflict Policy.**
57e10 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
57e20 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63 61  n may only be ca
57e30 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
57e40 20 61 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 5b   a call to the [
57e50 78 55 70 64 61 74 65 5d 20 6d 65 74 68 6f 64 0a  xUpdate] method.
57e60 2a 2a 20 6f 66 20 61 20 5b 76 69 72 74 75 61 6c  ** of a [virtual
57e70 20 74 61 62 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e   table] implemen
57e80 74 61 74 69 6f 6e 20 66 6f 72 20 61 6e 20 49 4e  tation for an IN
57e90 53 45 52 54 20 6f 72 20 55 50 44 41 54 45 20 6f  SERT or UPDATE o
57ea0 70 65 72 61 74 69 6f 6e 2e 20 5e 54 68 65 0a 2a  peration. ^The.*
57eb0 2a 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  * value returned
57ec0 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49   is one of [SQLI
57ed0 54 45 5f 52 4f 4c 4c 42 41 43 4b 5d 2c 20 5b 53  TE_ROLLBACK], [S
57ee0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 2c 20 5b  QLITE_IGNORE], [
57ef0 53 51 4c 49 54 45 5f 46 41 49 4c 5d 2c 0a 2a 2a  SQLITE_FAIL],.**
57f00 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2c   [SQLITE_ABORT],
57f10 20 6f 72 20 5b 53 51 4c 49 54 45 5f 52 45 50 4c   or [SQLITE_REPL
57f20 41 43 45 5d 2c 20 61 63 63 6f 72 64 69 6e 67 20  ACE], according 
57f30 74 6f 20 74 68 65 20 5b 4f 4e 20 43 4f 4e 46 4c  to the [ON CONFL
57f40 49 43 54 5d 20 6d 6f 64 65 0a 2a 2a 20 6f 66 20  ICT] mode.** of 
57f50 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
57f60 74 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64  t that triggered
57f70 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 68 65   the call to the
57f80 20 5b 78 55 70 64 61 74 65 5d 20 6d 65 74 68 6f   [xUpdate] metho
57f90 64 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 76 69 72  d of the.** [vir
57fa0 74 75 61 6c 20 74 61 62 6c 65 5d 2e 0a 2a 2f 0a  tual table]..*/.
57fb0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
57fc0 71 6c 69 74 65 33 5f 76 74 61 62 5f 6f 6e 5f 63  qlite3_vtab_on_c
57fd0 6f 6e 66 6c 69 63 74 28 73 71 6c 69 74 65 33 20  onflict(sqlite3 
57fe0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
57ff0 52 45 46 3a 20 43 6f 6e 66 6c 69 63 74 20 72 65  REF: Conflict re
58000 73 6f 6c 75 74 69 6f 6e 20 6d 6f 64 65 73 0a 2a  solution modes.*
58010 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74  *.** These const
58020 61 6e 74 73 20 61 72 65 20 72 65 74 75 72 6e 65  ants are returne
58030 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 76 74  d by [sqlite3_vt
58040 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63 74 28 29  ab_on_conflict()
58050 5d 20 74 6f 0a 2a 2a 20 69 6e 66 6f 72 6d 20 61  ] to.** inform a
58060 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d   [virtual table]
58070 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
58080 77 68 61 74 20 74 68 65 20 5b 4f 4e 20 43 4f 4e  what the [ON CON
58090 46 4c 49 43 54 5d 20 6d 6f 64 65 0a 2a 2a 20 69  FLICT] mode.** i
580a0 73 20 66 6f 72 20 74 68 65 20 53 51 4c 20 73 74  s for the SQL st
580b0 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 76  atement being ev
580c0 61 6c 75 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e  aluated..**.** N
580d0 6f 74 65 20 74 68 61 74 20 74 68 65 20 5b 53 51  ote that the [SQ
580e0 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 63 6f 6e  LITE_IGNORE] con
580f0 73 74 61 6e 74 20 69 73 20 61 6c 73 6f 20 75 73  stant is also us
58100 65 64 20 61 73 20 61 20 70 6f 74 65 6e 74 69 61  ed as a potentia
58110 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75  l.** return valu
58120 65 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69  e from the [sqli
58130 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
58140 65 72 28 29 5d 20 63 61 6c 6c 62 61 63 6b 20 61  er()] callback a
58150 6e 64 20 74 68 61 74 0a 2a 2a 20 5b 53 51 4c 49  nd that.** [SQLI
58160 54 45 5f 41 42 4f 52 54 5d 20 69 73 20 61 6c 73  TE_ABORT] is als
58170 6f 20 61 20 5b 72 65 73 75 6c 74 20 63 6f 64 65  o a [result code
58180 5d 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ]..*/.#define SQ
58190 4c 49 54 45 5f 52 4f 4c 4c 42 41 43 4b 20 31 0a  LITE_ROLLBACK 1.
581a0 2f 2a 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54  /* #define SQLIT
581b0 45 5f 49 47 4e 4f 52 45 20 32 20 2f 2f 20 41 6c  E_IGNORE 2 // Al
581c0 73 6f 20 75 73 65 64 20 62 79 20 73 71 6c 69 74  so used by sqlit
581d0 65 33 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 20  e3_authorizer() 
581e0 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 23 64 65 66  callback */.#def
581f0 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 49 4c 20  ine SQLITE_FAIL 
58200 20 20 20 20 33 0a 2f 2a 20 23 64 65 66 69 6e 65      3./* #define
58210 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 34 20   SQLITE_ABORT 4 
58220 20 2f 2f 20 41 6c 73 6f 20 61 6e 20 65 72 72 6f   // Also an erro
58230 72 20 63 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e  r code */.#defin
58240 65 20 53 51 4c 49 54 45 5f 52 45 50 4c 41 43 45  e SQLITE_REPLACE
58250 20 20 35 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64    5..../*.** Und
58260 6f 20 74 68 65 20 68 61 63 6b 20 74 68 61 74 20  o the hack that 
58270 63 6f 6e 76 65 72 74 73 20 66 6c 6f 61 74 69 6e  converts floatin
58280 67 20 70 6f 69 6e 74 20 74 79 70 65 73 20 74 6f  g point types to
58290 20 69 6e 74 65 67 65 72 20 66 6f 72 0a 2a 2a 20   integer for.** 
582a0 62 75 69 6c 64 73 20 6f 6e 20 70 72 6f 63 65 73  builds on proces
582b0 73 6f 72 73 20 77 69 74 68 6f 75 74 20 66 6c 6f  sors without flo
582c0 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70  ating point supp
582d0 6f 72 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ort..*/.#ifdef S
582e0 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
582f0 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 75 6e 64 65  ING_POINT.# unde
58300 66 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a  f double.#endif.
58310 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 45 6e 64  .#if 0.}  /* End
58320 20 6f 66 20 74 68 65 20 27 65 78 74 65 72 6e 20   of the 'extern 
58330 22 43 22 27 20 62 6c 6f 63 6b 20 2a 2f 0a 23 65  "C"' block */.#e
58340 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  ndif.#endif../*.
58350 2a 2a 20 32 30 31 30 20 41 75 67 75 73 74 20 33  ** 2010 August 3
58360 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  0.**.** The auth
58370 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
58380 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
58390 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
583a0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
583b0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
583c0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
583d0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
583e0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
583f0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
58400 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
58410 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
58420 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
58430 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
58440 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
58450 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
58460 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
58470 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
58480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
584a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
584b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
584c0 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 53 51  .*/..#ifndef _SQ
584d0 4c 49 54 45 33 52 54 52 45 45 5f 48 5f 0a 23 64  LITE3RTREE_H_.#d
584e0 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 33 52 54  efine _SQLITE3RT
584f0 52 45 45 5f 48 5f 0a 0a 0a 23 69 66 20 30 0a 65  REE_H_...#if 0.e
58500 78 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64  xtern "C" {.#end
58510 69 66 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75  if..typedef stru
58520 63 74 20 73 71 6c 69 74 65 33 5f 72 74 72 65 65  ct sqlite3_rtree
58530 5f 67 65 6f 6d 65 74 72 79 20 73 71 6c 69 74 65  _geometry sqlite
58540 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 79  3_rtree_geometry
58550 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  ;../*.** Registe
58560 72 20 61 20 67 65 6f 6d 65 74 72 79 20 63 61 6c  r a geometry cal
58570 6c 62 61 63 6b 20 6e 61 6d 65 64 20 7a 47 65 6f  lback named zGeo
58580 6d 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73  m that can be us
58590 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e  ed as part of an
585a0 0a 2a 2a 20 52 2d 54 72 65 65 20 67 65 6f 6d 65  .** R-Tree geome
585b0 74 72 79 20 71 75 65 72 79 20 61 73 20 66 6f 6c  try query as fol
585c0 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45  lows:.**.**   SE
585d0 4c 45 43 54 20 2e 2e 2e 20 46 52 4f 4d 20 3c 72  LECT ... FROM <r
585e0 74 72 65 65 3e 20 57 48 45 52 45 20 3c 72 74 72  tree> WHERE <rtr
585f0 65 65 20 63 6f 6c 3e 20 4d 41 54 43 48 20 24 7a  ee col> MATCH $z
58600 47 65 6f 6d 28 2e 2e 2e 20 70 61 72 61 6d 73 20  Geom(... params 
58610 2e 2e 2e 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ...).*/.SQLITE_A
58620 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72  PI int sqlite3_r
58630 74 72 65 65 5f 67 65 6f 6d 65 74 72 79 5f 63 61  tree_geometry_ca
58640 6c 6c 62 61 63 6b 28 0a 20 20 73 71 6c 69 74 65  llback(.  sqlite
58650 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63  3 *db,.  const c
58660 68 61 72 20 2a 7a 47 65 6f 6d 2c 0a 23 69 66 64  har *zGeom,.#ifd
58670 65 66 20 53 51 4c 49 54 45 5f 52 54 52 45 45 5f  ef SQLITE_RTREE_
58680 49 4e 54 5f 4f 4e 4c 59 0a 20 20 69 6e 74 20 28  INT_ONLY.  int (
58690 2a 78 47 65 6f 6d 29 28 73 71 6c 69 74 65 33 5f  *xGeom)(sqlite3_
586a0 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 79 2a 2c  rtree_geometry*,
586b0 20 69 6e 74 20 6e 2c 20 73 71 6c 69 74 65 33 5f   int n, sqlite3_
586c0 69 6e 74 36 34 20 2a 61 2c 20 69 6e 74 20 2a 70  int64 *a, int *p
586d0 52 65 73 29 2c 0a 23 65 6c 73 65 0a 20 20 69 6e  Res),.#else.  in
586e0 74 20 28 2a 78 47 65 6f 6d 29 28 73 71 6c 69 74  t (*xGeom)(sqlit
586f0 65 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72  e3_rtree_geometr
58700 79 2a 2c 20 69 6e 74 20 6e 2c 20 64 6f 75 62 6c  y*, int n, doubl
58710 65 20 2a 61 2c 20 69 6e 74 20 2a 70 52 65 73 29  e *a, int *pRes)
58720 2c 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 20  ,.#endif.  void 
58730 2a 70 43 6f 6e 74 65 78 74 0a 29 3b 0a 0a 0a 2f  *pContext.);.../
58740 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74  *.** A pointer t
58750 6f 20 61 20 73 74 72 75 63 74 75 72 65 20 6f 66  o a structure of
58760 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
58770 79 70 65 20 69 73 20 70 61 73 73 65 64 20 61 73  ype is passed as
58780 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72   the first.** ar
58790 67 75 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 62 61  gument to callba
587a0 63 6b 73 20 72 65 67 69 73 74 65 72 65 64 20 75  cks registered u
587b0 73 69 6e 67 20 72 74 72 65 65 5f 67 65 6f 6d 65  sing rtree_geome
587c0 74 72 79 5f 63 61 6c 6c 62 61 63 6b 28 29 2e 0a  try_callback()..
587d0 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
587e0 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 79  3_rtree_geometry
587f0 20 7b 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6e 74   {.  void *pCont
58800 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ext;            
58810 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20       /* Copy of 
58820 70 43 6f 6e 74 65 78 74 20 70 61 73 73 65 64 20  pContext passed 
58830 74 6f 20 73 5f 72 5f 67 5f 63 28 29 20 2a 2f 0a  to s_r_g_c() */.
58840 20 20 69 6e 74 20 6e 50 61 72 61 6d 3b 20 20 20    int nParam;   
58850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58860 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72 72    /* Size of arr
58870 61 79 20 61 50 61 72 61 6d 5b 5d 20 2a 2f 0a 20  ay aParam[] */. 
58880 20 64 6f 75 62 6c 65 20 2a 61 50 61 72 61 6d 3b   double *aParam;
58890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
588a0 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 73 20 70   /* Parameters p
588b0 61 73 73 65 64 20 74 6f 20 53 51 4c 20 67 65 6f  assed to SQL geo
588c0 6d 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  m function */.  
588d0 76 6f 69 64 20 2a 70 55 73 65 72 3b 20 20 20 20  void *pUser;    
588e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
588f0 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 69 6d 70 6c  /* Callback impl
58900 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 72 20  ementation user 
58910 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 28  data */.  void (
58920 2a 78 44 65 6c 55 73 65 72 29 28 76 6f 69 64 20  *xDelUser)(void 
58930 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43 61 6c  *);       /* Cal
58940 6c 65 64 20 62 79 20 53 51 4c 69 74 65 20 74 6f  led by SQLite to
58950 20 63 6c 65 61 6e 20 75 70 20 70 55 73 65 72 20   clean up pUser 
58960 2a 2f 0a 7d 3b 0a 0a 0a 23 69 66 20 30 0a 7d 20  */.};...#if 0.} 
58970 20 2f 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 27   /* end of the '
58980 65 78 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 63  extern "C"' bloc
58990 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 23 65 6e  k */.#endif..#en
589a0 64 69 66 20 20 2f 2a 20 69 66 6e 64 65 66 20 5f  dif  /* ifndef _
589b0 53 51 4c 49 54 45 33 52 54 52 45 45 5f 48 5f 20  SQLITE3RTREE_H_ 
589c0 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */.../**********
589d0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69  **** End of sqli
589e0 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  te3.h **********
589f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58a10 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
58a20 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
58a30 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
58a40 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  f in sqliteInt.h
58a50 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
58a60 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
58a70 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61 73  **** Include has
58a80 68 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  h.h in the middl
58a90 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68  e of sqliteInt.h
58aa0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
58ab0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
58ac0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
58ad0 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  hash.h *********
58ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58b00 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
58b10 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a  September 22.**.
58b20 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
58b30 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
58b40 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
58b50 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
58b60 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
58b70 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
58b80 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
58b90 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
58ba0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
58bb0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
58bc0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
58bd0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
58be0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
58bf0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
58c00 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
58c10 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
58c20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
58c30 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
58c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 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 2a 2a  ****************
58c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
58c80 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65  his is the heade
58c90 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67  r file for the g
58ca0 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c  eneric hash-tabl
58cb0 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a  e implemenation.
58cc0 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74  ** used in SQLit
58cd0 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53  e..*/.#ifndef _S
58ce0 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 0a 23 64  QLITE_HASH_H_.#d
58cf0 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 48 41  efine _SQLITE_HA
58d00 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72  SH_H_../* Forwar
58d10 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f  d declarations o
58d20 66 20 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f  f structures. */
58d30 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
58d40 48 61 73 68 20 48 61 73 68 3b 0a 74 79 70 65 64  Hash Hash;.typed
58d50 65 66 20 73 74 72 75 63 74 20 48 61 73 68 45 6c  ef struct HashEl
58d60 65 6d 20 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a  em HashElem;../*
58d70 20 41 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68   A complete hash
58d80 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73   table is an ins
58d90 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
58da0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
58db0 2e 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 6e 61  ..** The interna
58dc0 6c 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ls of this struc
58dd0 74 75 72 65 20 61 72 65 20 69 6e 74 65 6e 64 65  ture are intende
58de0 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 2d  d to be opaque -
58df0 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65  - client.** code
58e00 20 73 68 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65   should not atte
58e10 6d 70 74 20 74 6f 20 61 63 63 65 73 73 20 6f 72  mpt to access or
58e20 20 6d 6f 64 69 66 79 20 74 68 65 20 66 69 65 6c   modify the fiel
58e30 64 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ds of this struc
58e40 74 75 72 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79  ture.** directly
58e50 2e 20 20 43 68 61 6e 67 65 20 74 68 69 73 20 73  .  Change this s
58e60 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 20 62 79  tructure only by
58e70 20 75 73 69 6e 67 20 74 68 65 20 72 6f 75 74 69   using the routi
58e80 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f  nes below..** Ho
58e90 77 65 76 65 72 2c 20 73 6f 6d 65 20 6f 66 20 74  wever, some of t
58ea0 68 65 20 22 70 72 6f 63 65 64 75 72 65 73 22 20  he "procedures" 
58eb0 61 6e 64 20 22 66 75 6e 63 74 69 6f 6e 73 22 20  and "functions" 
58ec0 66 6f 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e  for modifying an
58ed0 64 0a 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74  d.** accessing t
58ee0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72  his structure ar
58ef0 65 20 72 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c  e really macros,
58f00 20 73 6f 20 77 65 20 63 61 6e 27 74 20 72 65 61   so we can't rea
58f10 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73  lly make.** this
58f20 20 73 74 72 75 63 74 75 72 65 20 6f 70 61 71 75   structure opaqu
58f30 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65 6c 65  e..**.** All ele
58f40 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 68 61 73  ments of the has
58f50 68 20 74 61 62 6c 65 20 61 72 65 20 6f 6e 20 61  h table are on a
58f60 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c   single doubly-l
58f70 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 20 48  inked list..** H
58f80 61 73 68 2e 66 69 72 73 74 20 70 6f 69 6e 74 73  ash.first points
58f90 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20   to the head of 
58fa0 74 68 69 73 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a  this list..**.**
58fb0 20 54 68 65 72 65 20 61 72 65 20 48 61 73 68 2e   There are Hash.
58fc0 68 74 73 69 7a 65 20 62 75 63 6b 65 74 73 2e 20  htsize buckets. 
58fd0 20 45 61 63 68 20 62 75 63 6b 65 74 20 70 6f 69   Each bucket poi
58fe0 6e 74 73 20 74 6f 20 61 20 73 70 6f 74 20 69 6e  nts to a spot in
58ff0 0a 2a 2a 20 74 68 65 20 67 6c 6f 62 61 6c 20 64  .** the global d
59000 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73  oubly-linked lis
59010 74 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 73  t.  The contents
59020 20 6f 66 20 74 68 65 20 62 75 63 6b 65 74 20 61   of the bucket a
59030 72 65 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e  re the.** elemen
59040 74 20 70 6f 69 6e 74 65 64 20 74 6f 20 70 6c 75  t pointed to plu
59050 73 20 74 68 65 20 6e 65 78 74 20 5f 68 74 2e 63  s the next _ht.c
59060 6f 75 6e 74 2d 31 20 65 6c 65 6d 65 6e 74 73 20  ount-1 elements 
59070 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a  in the list..**.
59080 2a 2a 20 48 61 73 68 2e 68 74 73 69 7a 65 20 61  ** Hash.htsize a
59090 6e 64 20 48 61 73 68 2e 68 74 20 6d 61 79 20 62  nd Hash.ht may b
590a0 65 20 7a 65 72 6f 2e 20 20 49 6e 20 74 68 61 74  e zero.  In that
590b0 20 63 61 73 65 20 6c 6f 6f 6b 75 70 20 69 73 20   case lookup is 
590c0 64 6f 6e 65 0a 2a 2a 20 62 79 20 61 20 6c 69 6e  done.** by a lin
590d0 65 61 72 20 73 65 61 72 63 68 20 6f 66 20 74 68  ear search of th
590e0 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74 2e 20 20  e global list.  
590f0 46 6f 72 20 73 6d 61 6c 6c 20 74 61 62 6c 65 73  For small tables
59100 2c 20 74 68 65 20 0a 2a 2a 20 48 61 73 68 2e 68  , the .** Hash.h
59110 74 20 74 61 62 6c 65 20 69 73 20 6e 65 76 65 72  t table is never
59120 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 63 61 75   allocated becau
59130 73 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20  se if there are 
59140 66 65 77 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20  few elements.** 
59150 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 69 74  in the table, it
59160 20 69 73 20 66 61 73 74 65 72 20 74 6f 20 64 6f   is faster to do
59170 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68   a linear search
59180 20 74 68 61 6e 20 74 6f 20 6d 61 6e 61 67 65 0a   than to manage.
59190 2a 2a 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  ** the hash tabl
591a0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73  e..*/.struct Has
591b0 68 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  h {.  unsigned i
591c0 6e 74 20 68 74 73 69 7a 65 3b 20 20 20 20 20 20  nt htsize;      
591d0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 75 63  /* Number of buc
591e0 6b 65 74 73 20 69 6e 20 74 68 65 20 68 61 73 68  kets in the hash
591f0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 6e 73 69   table */.  unsi
59200 67 6e 65 64 20 69 6e 74 20 63 6f 75 6e 74 3b 20  gned int count; 
59210 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
59220 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  of entries in th
59230 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61  is table */.  Ha
59240 73 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b 20 20  shElem *first;  
59250 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
59260 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
59270 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 73  the array */.  s
59280 74 72 75 63 74 20 5f 68 74 20 7b 20 20 20 20 20  truct _ht {     
59290 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20           /* the 
592a0 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
592b0 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20    int count;    
592c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
592d0 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
592e0 73 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68  s with this hash
592f0 20 2a 2f 0a 20 20 20 20 48 61 73 68 45 6c 65 6d   */.    HashElem
59300 20 2a 63 68 61 69 6e 3b 20 20 20 20 20 20 20 20   *chain;        
59310 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
59320 20 66 69 72 73 74 20 65 6e 74 72 79 20 77 69 74   first entry wit
59330 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20  h this hash */. 
59340 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45   } *ht;.};../* E
59350 61 63 68 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74  ach element in t
59360 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73  he hash table is
59370 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
59380 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a  the following .*
59390 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 6c  * structure.  Al
593a0 6c 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 73  l elements are s
593b0 74 6f 72 65 64 20 6f 6e 20 61 20 73 69 6e 67 6c  tored on a singl
593c0 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20  e doubly-linked 
593d0 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69  list..**.** Agai
593e0 6e 2c 20 74 68 69 73 20 73 74 72 75 63 74 75 72  n, this structur
593f0 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f  e is intended to
59400 20 62 65 20 6f 70 61 71 75 65 2c 20 62 75 74 20   be opaque, but 
59410 69 74 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 0a  it can't really.
59420 2a 2a 20 62 65 20 6f 70 61 71 75 65 20 62 65 63  ** be opaque bec
59430 61 75 73 65 20 69 74 20 69 73 20 75 73 65 64 20  ause it is used 
59440 62 79 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74  by macros..*/.st
59450 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 7b 0a  ruct HashElem {.
59460 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74    HashElem *next
59470 2c 20 2a 70 72 65 76 3b 20 20 20 20 20 20 20 2f  , *prev;       /
59480 2a 20 4e 65 78 74 20 61 6e 64 20 70 72 65 76 69  * Next and previ
59490 6f 75 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20  ous elements in 
594a0 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76  the table */.  v
594b0 6f 69 64 20 2a 64 61 74 61 3b 20 20 20 20 20 20  oid *data;      
594c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
594d0 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  ata associated w
594e0 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74  ith this element
594f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
59500 20 2a 70 4b 65 79 3b 20 69 6e 74 20 6e 4b 65 79   *pKey; int nKey
59510 3b 20 20 2f 2a 20 4b 65 79 20 61 73 73 6f 63 69  ;  /* Key associ
59520 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65  ated with this e
59530 6c 65 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  lement */.};../*
59540 0a 2a 2a 20 41 63 63 65 73 73 20 72 6f 75 74 69  .** Access routi
59550 6e 65 73 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c  nes.  To delete,
59560 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 70   insert a NULL p
59570 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  ointer..*/.SQLIT
59580 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
59590 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 48  qlite3HashInit(H
595a0 61 73 68 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ash*);.SQLITE_PR
595b0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
595c0 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 48 61  te3HashInsert(Ha
595d0 73 68 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  sh*, const char 
595e0 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c  *pKey, int nKey,
595f0 20 76 6f 69 64 20 2a 70 44 61 74 61 29 3b 0a 53   void *pData);.S
59600 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
59610 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 46  id *sqlite3HashF
59620 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 2a 2c  ind(const Hash*,
59630 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65   const char *pKe
59640 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 51  y, int nKey);.SQ
59650 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
59660 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65  d sqlite3HashCle
59670 61 72 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a  ar(Hash*);../*.*
59680 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f 6f  * Macros for loo
59690 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c  ping over all el
596a0 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73 68  ements of a hash
596b0 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64 69   table.  The idi
596c0 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 68  om is.** like th
596d0 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61 73 68  is:.**.**   Hash
596e0 20 68 3b 0a 2a 2a 20 20 20 48 61 73 68 45 6c 65   h;.**   HashEle
596f0 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a  m *p;.**   ....*
59700 2a 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65  *   for(p=sqlite
59710 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20 70  HashFirst(&h); p
59720 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  ; p=sqliteHashNe
59730 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53  xt(p)){.**     S
59740 6f 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70 44  omeStructure *pD
59750 61 74 61 20 3d 20 73 71 6c 69 74 65 48 61 73 68  ata = sqliteHash
59760 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20 20  Data(p);.**     
59770 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20  // do something 
59780 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20 20 20  with pData.**   
59790 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c  }.*/.#define sql
597a0 69 74 65 48 61 73 68 46 69 72 73 74 28 48 29 20  iteHashFirst(H) 
597b0 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 64   ((H)->first).#d
597c0 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68  efine sqliteHash
597d0 4e 65 78 74 28 45 29 20 20 20 28 28 45 29 2d 3e  Next(E)   ((E)->
597e0 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20 73 71  next).#define sq
597f0 6c 69 74 65 48 61 73 68 44 61 74 61 28 45 29 20  liteHashData(E) 
59800 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a 2f 2a    ((E)->data)./*
59810 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48   #define sqliteH
59820 61 73 68 4b 65 79 28 45 29 20 20 20 20 28 28 45  ashKey(E)    ((E
59830 29 2d 3e 70 4b 65 79 29 20 2f 2f 20 4e 4f 54 20  )->pKey) // NOT 
59840 55 53 45 44 20 2a 2f 0a 2f 2a 20 23 64 65 66 69  USED */./* #defi
59850 6e 65 20 73 71 6c 69 74 65 48 61 73 68 4b 65 79  ne sqliteHashKey
59860 73 69 7a 65 28 45 29 20 28 28 45 29 2d 3e 6e 4b  size(E) ((E)->nK
59870 65 79 29 20 20 2f 2f 20 4e 4f 54 20 55 53 45 44  ey)  // NOT USED
59880 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65   */../*.** Numbe
59890 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
598a0 61 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a  a hash table.*/.
598b0 2f 2a 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  /* #define sqlit
598c0 65 48 61 73 68 43 6f 75 6e 74 28 48 29 20 20 28  eHashCount(H)  (
598d0 28 48 29 2d 3e 63 6f 75 6e 74 29 20 2f 2f 20 4e  (H)->count) // N
598e0 4f 54 20 55 53 45 44 20 2a 2f 0a 0a 23 65 6e 64  OT USED */..#end
598f0 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 48 41  if /* _SQLITE_HA
59900 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  SH_H_ */../*****
59910 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
59920 20 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a   hash.h ********
59930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59950 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
59960 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
59970 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
59980 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
59990 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
599a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
599b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
599c0 65 20 70 61 72 73 65 2e 68 20 69 6e 20 74 68 65  e parse.h in the
599d0 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74   middle of sqlit
599e0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
599f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
59a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
59a10 66 69 6c 65 20 70 61 72 73 65 2e 68 20 2a 2a 2a  file parse.h ***
59a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59a40 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e  ********/.#defin
59a50 65 20 54 4b 5f 53 45 4d 49 20 20 20 20 20 20 20  e TK_SEMI       
59a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59a70 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 54       1.#define T
59a80 4b 5f 45 58 50 4c 41 49 4e 20 20 20 20 20 20 20  K_EXPLAIN       
59a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59aa0 20 20 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 51    2.#define TK_Q
59ab0 55 45 52 59 20 20 20 20 20 20 20 20 20 20 20 20  UERY            
59ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
59ad0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c 41 4e  .#define TK_PLAN
59ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59af0 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 23 64              4.#d
59b00 65 66 69 6e 65 20 54 4b 5f 42 45 47 49 4e 20 20  efine TK_BEGIN  
59b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59b20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69           5.#defi
59b30 6e 65 20 54 4b 5f 54 52 41 4e 53 41 43 54 49 4f  ne TK_TRANSACTIO
59b40 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
59b50 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20        6.#define 
59b60 54 4b 5f 44 45 46 45 52 52 45 44 20 20 20 20 20  TK_DEFERRED     
59b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59b80 20 20 20 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f     7.#define TK_
59b90 49 4d 4d 45 44 49 41 54 45 20 20 20 20 20 20 20  IMMEDIATE       
59ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59bb0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 43  8.#define TK_EXC
59bc0 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 20 20  LUSIVE          
59bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 0a 23               9.#
59be0 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 49 54  define TK_COMMIT
59bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59c00 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 65 66           10.#def
59c10 69 6e 65 20 54 4b 5f 45 4e 44 20 20 20 20 20 20  ine TK_END      
59c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59c30 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65        11.#define
59c40 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 20 20 20 20   TK_ROLLBACK    
59c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59c60 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 54 4b     12.#define TK
59c70 5f 53 41 56 45 50 4f 49 4e 54 20 20 20 20 20 20  _SAVEPOINT      
59c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59c90 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45  13.#define TK_RE
59ca0 4c 45 41 53 45 20 20 20 20 20 20 20 20 20 20 20  LEASE           
59cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 0a               14.
59cc0 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 20 20 20  #define TK_TO   
59cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59ce0 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 64 65            15.#de
59cf0 66 69 6e 65 20 54 4b 5f 54 41 42 4c 45 20 20 20  fine TK_TABLE   
59d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59d10 20 20 20 20 20 20 20 31 36 0a 23 64 65 66 69 6e         16.#defin
59d20 65 20 54 4b 5f 43 52 45 41 54 45 20 20 20 20 20  e TK_CREATE     
59d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59d40 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65 20 54      17.#define T
59d50 4b 5f 49 46 20 20 20 20 20 20 20 20 20 20 20 20  K_IF            
59d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59d70 20 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e   18.#define TK_N
59d80 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OT              
59d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39                19
59da0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 49 53  .#define TK_EXIS
59db0 54 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TS              
59dc0 20 20 20 20 20 20 20 20 20 20 20 32 30 0a 23 64             20.#d
59dd0 65 66 69 6e 65 20 54 4b 5f 54 45 4d 50 20 20 20  efine TK_TEMP   
59de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59df0 20 20 20 20 20 20 20 20 32 31 0a 23 64 65 66 69          21.#defi
59e00 6e 65 20 54 4b 5f 4c 50 20 20 20 20 20 20 20 20  ne TK_LP        
59e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59e20 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20       22.#define 
59e30 54 4b 5f 52 50 20 20 20 20 20 20 20 20 20 20 20  TK_RP           
59e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59e50 20 20 32 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f    23.#define TK_
59e60 41 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AS              
59e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
59e80 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d  4.#define TK_COM
59e90 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MA              
59ea0 20 20 20 20 20 20 20 20 20 20 20 20 32 35 0a 23              25.#
59eb0 64 65 66 69 6e 65 20 54 4b 5f 49 44 20 20 20 20  define TK_ID    
59ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59ed0 20 20 20 20 20 20 20 20 20 32 36 0a 23 64 65 66           26.#def
59ee0 69 6e 65 20 54 4b 5f 49 4e 44 45 58 45 44 20 20  ine TK_INDEXED  
59ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59f00 20 20 20 20 20 20 32 37 0a 23 64 65 66 69 6e 65        27.#define
59f10 20 54 4b 5f 41 42 4f 52 54 20 20 20 20 20 20 20   TK_ABORT       
59f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59f30 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20 54 4b     28.#define TK
59f40 5f 41 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20  _ACTION         
59f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59f60 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 46  29.#define TK_AF
59f70 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  TER             
59f80 20 20 20 20 20 20 20 20 20 20 20 20 20 33 30 0a               30.
59f90 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 41 4c 59  #define TK_ANALY
59fa0 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ZE              
59fb0 20 20 20 20 20 20 20 20 20 20 33 31 0a 23 64 65            31.#de
59fc0 66 69 6e 65 20 54 4b 5f 41 53 43 20 20 20 20 20  fine TK_ASC     
59fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59fe0 20 20 20 20 20 20 20 33 32 0a 23 64 65 66 69 6e         32.#defin
59ff0 65 20 54 4b 5f 41 54 54 41 43 48 20 20 20 20 20  e TK_ATTACH     
5a000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a010 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20 54      33.#define T
5a020 4b 5f 42 45 46 4f 52 45 20 20 20 20 20 20 20 20  K_BEFORE        
5a030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a040 20 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42   34.#define TK_B
5a050 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Y               
5a060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 35                35
5a070 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 43  .#define TK_CASC
5a080 41 44 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ADE             
5a090 20 20 20 20 20 20 20 20 20 20 20 33 36 0a 23 64             36.#d
5a0a0 65 66 69 6e 65 20 54 4b 5f 43 41 53 54 20 20 20  efine TK_CAST   
5a0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a0c0 20 20 20 20 20 20 20 20 33 37 0a 23 64 65 66 69          37.#defi
5a0d0 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 20 20  ne TK_COLUMNKW  
5a0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a0f0 20 20 20 20 20 33 38 0a 23 64 65 66 69 6e 65 20       38.#define 
5a100 54 4b 5f 43 4f 4e 46 4c 49 43 54 20 20 20 20 20  TK_CONFLICT     
5a110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a120 20 20 33 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f    39.#define TK_
5a130 44 41 54 41 42 41 53 45 20 20 20 20 20 20 20 20  DATABASE        
5a140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
5a150 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 53  0.#define TK_DES
5a160 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  C               
5a170 20 20 20 20 20 20 20 20 20 20 20 20 34 31 0a 23              41.#
5a180 64 65 66 69 6e 65 20 54 4b 5f 44 45 54 41 43 48  define TK_DETACH
5a190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a1a0 20 20 20 20 20 20 20 20 20 34 32 0a 23 64 65 66           42.#def
5a1b0 69 6e 65 20 54 4b 5f 45 41 43 48 20 20 20 20 20  ine TK_EACH     
5a1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a1d0 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e 65        43.#define
5a1e0 20 54 4b 5f 46 41 49 4c 20 20 20 20 20 20 20 20   TK_FAIL        
5a1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a200 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20 54 4b     44.#define TK
5a210 5f 46 4f 52 20 20 20 20 20 20 20 20 20 20 20 20  _FOR            
5a220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a230 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 47  45.#define TK_IG
5a240 4e 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20  NORE            
5a250 20 20 20 20 20 20 20 20 20 20 20 20 20 34 36 0a               46.
5a260 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 49 54 49  #define TK_INITI
5a270 41 4c 4c 59 20 20 20 20 20 20 20 20 20 20 20 20  ALLY            
5a280 20 20 20 20 20 20 20 20 20 20 34 37 0a 23 64 65            47.#de
5a290 66 69 6e 65 20 54 4b 5f 49 4e 53 54 45 41 44 20  fine TK_INSTEAD 
5a2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a2b0 20 20 20 20 20 20 20 34 38 0a 23 64 65 66 69 6e         48.#defin
5a2c0 65 20 54 4b 5f 4c 49 4b 45 5f 4b 57 20 20 20 20  e TK_LIKE_KW    
5a2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a2e0 20 20 20 20 34 39 0a 23 64 65 66 69 6e 65 20 54      49.#define T
5a2f0 4b 5f 4d 41 54 43 48 20 20 20 20 20 20 20 20 20  K_MATCH         
5a300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a310 20 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e   50.#define TK_N
5a320 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  O               
5a330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31                51
5a340 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4b 45 59 20  .#define TK_KEY 
5a350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a360 20 20 20 20 20 20 20 20 20 20 20 35 32 0a 23 64             52.#d
5a370 65 66 69 6e 65 20 54 4b 5f 4f 46 20 20 20 20 20  efine TK_OF     
5a380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a390 20 20 20 20 20 20 20 20 35 33 0a 23 64 65 66 69          53.#defi
5a3a0 6e 65 20 54 4b 5f 4f 46 46 53 45 54 20 20 20 20  ne TK_OFFSET    
5a3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a3c0 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20       54.#define 
5a3d0 54 4b 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20  TK_PRAGMA       
5a3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a3f0 20 20 35 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f    55.#define TK_
5a400 52 41 49 53 45 20 20 20 20 20 20 20 20 20 20 20  RAISE           
5a410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
5a420 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 50  6.#define TK_REP
5a430 4c 41 43 45 20 20 20 20 20 20 20 20 20 20 20 20  LACE            
5a440 20 20 20 20 20 20 20 20 20 20 20 20 35 37 0a 23              57.#
5a450 64 65 66 69 6e 65 20 54 4b 5f 52 45 53 54 52 49  define TK_RESTRI
5a460 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CT              
5a470 20 20 20 20 20 20 20 20 20 35 38 0a 23 64 65 66           58.#def
5a480 69 6e 65 20 54 4b 5f 52 4f 57 20 20 20 20 20 20  ine TK_ROW      
5a490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a4a0 20 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65        59.#define
5a4b0 20 54 4b 5f 54 52 49 47 47 45 52 20 20 20 20 20   TK_TRIGGER     
5a4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a4d0 20 20 20 36 30 0a 23 64 65 66 69 6e 65 20 54 4b     60.#define TK
5a4e0 5f 56 41 43 55 55 4d 20 20 20 20 20 20 20 20 20  _VACUUM         
5a4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a500 36 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 49  61.#define TK_VI
5a510 45 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EW              
5a520 20 20 20 20 20 20 20 20 20 20 20 20 20 36 32 0a               62.
5a530 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 52 54 55  #define TK_VIRTU
5a540 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AL              
5a550 20 20 20 20 20 20 20 20 20 20 36 33 0a 23 64 65            63.#de
5a560 66 69 6e 65 20 54 4b 5f 52 45 49 4e 44 45 58 20  fine TK_REINDEX 
5a570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a580 20 20 20 20 20 20 20 36 34 0a 23 64 65 66 69 6e         64.#defin
5a590 65 20 54 4b 5f 52 45 4e 41 4d 45 20 20 20 20 20  e TK_RENAME     
5a5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a5b0 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65 20 54      65.#define T
5a5c0 4b 5f 43 54 49 4d 45 5f 4b 57 20 20 20 20 20 20  K_CTIME_KW      
5a5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a5e0 20 36 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41   66.#define TK_A
5a5f0 4e 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NY              
5a600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 37                67
5a610 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 20 20  .#define TK_OR  
5a620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a630 20 20 20 20 20 20 20 20 20 20 20 36 38 0a 23 64             68.#d
5a640 65 66 69 6e 65 20 54 4b 5f 41 4e 44 20 20 20 20  efine TK_AND    
5a650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a660 20 20 20 20 20 20 20 20 36 39 0a 23 64 65 66 69          69.#defi
5a670 6e 65 20 54 4b 5f 49 53 20 20 20 20 20 20 20 20  ne TK_IS        
5a680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a690 20 20 20 20 20 37 30 0a 23 64 65 66 69 6e 65 20       70.#define 
5a6a0 54 4b 5f 42 45 54 57 45 45 4e 20 20 20 20 20 20  TK_BETWEEN      
5a6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a6c0 20 20 37 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f    71.#define TK_
5a6d0 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  IN              
5a6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
5a6f0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e  2.#define TK_ISN
5a700 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  ULL             
5a710 20 20 20 20 20 20 20 20 20 20 20 20 37 33 0a 23              73.#
5a720 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 4e 55 4c  define TK_NOTNUL
5a730 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
5a740 20 20 20 20 20 20 20 20 20 37 34 0a 23 64 65 66           74.#def
5a750 69 6e 65 20 54 4b 5f 4e 45 20 20 20 20 20 20 20  ine TK_NE       
5a760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a770 20 20 20 20 20 20 37 35 0a 23 64 65 66 69 6e 65        75.#define
5a780 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20 20 20   TK_EQ          
5a790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a7a0 20 20 20 37 36 0a 23 64 65 66 69 6e 65 20 54 4b     76.#define TK
5a7b0 5f 47 54 20 20 20 20 20 20 20 20 20 20 20 20 20  _GT             
5a7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a7d0 37 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 45  77.#define TK_LE
5a7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 38 0a               78.
5a800 23 64 65 66 69 6e 65 20 54 4b 5f 4c 54 20 20 20  #define TK_LT   
5a810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a820 20 20 20 20 20 20 20 20 20 20 37 39 0a 23 64 65            79.#de
5a830 66 69 6e 65 20 54 4b 5f 47 45 20 20 20 20 20 20  fine TK_GE      
5a840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a850 20 20 20 20 20 20 20 38 30 0a 23 64 65 66 69 6e         80.#defin
5a860 65 20 54 4b 5f 45 53 43 41 50 45 20 20 20 20 20  e TK_ESCAPE     
5a870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a880 20 20 20 20 38 31 0a 23 64 65 66 69 6e 65 20 54      81.#define T
5a890 4b 5f 42 49 54 41 4e 44 20 20 20 20 20 20 20 20  K_BITAND        
5a8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a8b0 20 38 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42   82.#define TK_B
5a8c0 49 54 4f 52 20 20 20 20 20 20 20 20 20 20 20 20  ITOR            
5a8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 33                83
5a8e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 53 48 49  .#define TK_LSHI
5a8f0 46 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  FT              
5a900 20 20 20 20 20 20 20 20 20 20 20 38 34 0a 23 64             84.#d
5a910 65 66 69 6e 65 20 54 4b 5f 52 53 48 49 46 54 20  efine TK_RSHIFT 
5a920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a930 20 20 20 20 20 20 20 20 38 35 0a 23 64 65 66 69          85.#defi
5a940 6e 65 20 54 4b 5f 50 4c 55 53 20 20 20 20 20 20  ne TK_PLUS      
5a950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a960 20 20 20 20 20 38 36 0a 23 64 65 66 69 6e 65 20       86.#define 
5a970 54 4b 5f 4d 49 4e 55 53 20 20 20 20 20 20 20 20  TK_MINUS        
5a980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a990 20 20 38 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f    87.#define TK_
5a9a0 53 54 41 52 20 20 20 20 20 20 20 20 20 20 20 20  STAR            
5a9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
5a9c0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 4c 41  8.#define TK_SLA
5a9d0 53 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20  SH              
5a9e0 20 20 20 20 20 20 20 20 20 20 20 20 38 39 0a 23              89.#
5a9f0 64 65 66 69 6e 65 20 54 4b 5f 52 45 4d 20 20 20  define TK_REM   
5aa00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aa10 20 20 20 20 20 20 20 20 20 39 30 0a 23 64 65 66           90.#def
5aa20 69 6e 65 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20  ine TK_CONCAT   
5aa30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aa40 20 20 20 20 20 20 39 31 0a 23 64 65 66 69 6e 65        91.#define
5aa50 20 54 4b 5f 43 4f 4c 4c 41 54 45 20 20 20 20 20   TK_COLLATE     
5aa60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aa70 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 54 4b     92.#define TK
5aa80 5f 42 49 54 4e 4f 54 20 20 20 20 20 20 20 20 20  _BITNOT         
5aa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aaa0 39 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54  93.#define TK_ST
5aab0 52 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20  RING            
5aac0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 34 0a               94.
5aad0 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 5f  #define TK_JOIN_
5aae0 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20  KW              
5aaf0 20 20 20 20 20 20 20 20 20 20 39 35 0a 23 64 65            95.#de
5ab00 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 52 41 49  fine TK_CONSTRAI
5ab10 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NT              
5ab20 20 20 20 20 20 20 20 39 36 0a 23 64 65 66 69 6e         96.#defin
5ab30 65 20 54 4b 5f 44 45 46 41 55 4c 54 20 20 20 20  e TK_DEFAULT    
5ab40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ab50 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65 20 54      97.#define T
5ab60 4b 5f 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20  K_NULL          
5ab70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ab80 20 39 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50   98.#define TK_P
5ab90 52 49 4d 41 52 59 20 20 20 20 20 20 20 20 20 20  RIMARY          
5aba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 39                99
5abb0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 51  .#define TK_UNIQ
5abc0 55 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  UE              
5abd0 20 20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23             100.#
5abe0 64 65 66 69 6e 65 20 54 4b 5f 43 48 45 43 4b 20  define TK_CHECK 
5abf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ac00 20 20 20 20 20 20 20 20 20 31 30 31 0a 23 64 65           101.#de
5ac10 66 69 6e 65 20 54 4b 5f 52 45 46 45 52 45 4e 43  fine TK_REFERENC
5ac20 45 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ES              
5ac30 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 66 69         102.#defi
5ac40 6e 65 20 54 4b 5f 41 55 54 4f 49 4e 43 52 20 20  ne TK_AUTOINCR  
5ac50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ac60 20 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65       103.#define
5ac70 20 54 4b 5f 4f 4e 20 20 20 20 20 20 20 20 20 20   TK_ON          
5ac80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ac90 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65 20 54     104.#define T
5aca0 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 20  K_INSERT        
5acb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5acc0 20 31 30 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f   105.#define TK_
5acd0 44 45 4c 45 54 45 20 20 20 20 20 20 20 20 20 20  DELETE          
5ace0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
5acf0 30 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 50  06.#define TK_UP
5ad00 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20  DATE            
5ad10 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37               107
5ad20 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 54 20  .#define TK_SET 
5ad30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ad40 20 20 20 20 20 20 20 20 20 20 20 31 30 38 0a 23             108.#
5ad50 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52  define TK_DEFERR
5ad60 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  ABLE            
5ad70 20 20 20 20 20 20 20 20 20 31 30 39 0a 23 64 65           109.#de
5ad80 66 69 6e 65 20 54 4b 5f 46 4f 52 45 49 47 4e 20  fine TK_FOREIGN 
5ad90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ada0 20 20 20 20 20 20 20 31 31 30 0a 23 64 65 66 69         110.#defi
5adb0 6e 65 20 54 4b 5f 44 52 4f 50 20 20 20 20 20 20  ne TK_DROP      
5adc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5add0 20 20 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65       111.#define
5ade0 20 54 4b 5f 55 4e 49 4f 4e 20 20 20 20 20 20 20   TK_UNION       
5adf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ae00 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 20 54     112.#define T
5ae10 4b 5f 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20  K_ALL           
5ae20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ae30 20 31 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f   113.#define TK_
5ae40 45 58 43 45 50 54 20 20 20 20 20 20 20 20 20 20  EXCEPT          
5ae50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
5ae60 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e  14.#define TK_IN
5ae70 54 45 52 53 45 43 54 20 20 20 20 20 20 20 20 20  TERSECT         
5ae80 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 35               115
5ae90 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 4c 45  .#define TK_SELE
5aea0 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CT              
5aeb0 20 20 20 20 20 20 20 20 20 20 20 31 31 36 0a 23             116.#
5aec0 64 65 66 69 6e 65 20 54 4b 5f 44 49 53 54 49 4e  define TK_DISTIN
5aed0 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CT              
5aee0 20 20 20 20 20 20 20 20 20 31 31 37 0a 23 64 65           117.#de
5aef0 66 69 6e 65 20 54 4b 5f 44 4f 54 20 20 20 20 20  fine TK_DOT     
5af00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5af10 20 20 20 20 20 20 20 31 31 38 0a 23 64 65 66 69         118.#defi
5af20 6e 65 20 54 4b 5f 46 52 4f 4d 20 20 20 20 20 20  ne TK_FROM      
5af30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5af40 20 20 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65       119.#define
5af50 20 54 4b 5f 4a 4f 49 4e 20 20 20 20 20 20 20 20   TK_JOIN        
5af60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5af70 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65 20 54     120.#define T
5af80 4b 5f 55 53 49 4e 47 20 20 20 20 20 20 20 20 20  K_USING         
5af90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5afa0 20 31 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f   121.#define TK_
5afb0 4f 52 44 45 52 20 20 20 20 20 20 20 20 20 20 20  ORDER           
5afc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
5afd0 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 52  22.#define TK_GR
5afe0 4f 55 50 20 20 20 20 20 20 20 20 20 20 20 20 20  OUP             
5aff0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 33               123
5b000 0a 23 64 65 66 69 6e 65 20 54 4b 5f 48 41 56 49  .#define TK_HAVI
5b010 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NG              
5b020 20 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23             124.#
5b030 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4d 49 54 20  define TK_LIMIT 
5b040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b050 20 20 20 20 20 20 20 20 20 31 32 35 0a 23 64 65           125.#de
5b060 66 69 6e 65 20 54 4b 5f 57 48 45 52 45 20 20 20  fine TK_WHERE   
5b070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b080 20 20 20 20 20 20 20 31 32 36 0a 23 64 65 66 69         126.#defi
5b090 6e 65 20 54 4b 5f 49 4e 54 4f 20 20 20 20 20 20  ne TK_INTO      
5b0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b0b0 20 20 20 20 20 31 32 37 0a 23 64 65 66 69 6e 65       127.#define
5b0c0 20 54 4b 5f 56 41 4c 55 45 53 20 20 20 20 20 20   TK_VALUES      
5b0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b0e0 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 54     128.#define T
5b0f0 4b 5f 49 4e 54 45 47 45 52 20 20 20 20 20 20 20  K_INTEGER       
5b100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b110 20 31 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f   129.#define TK_
5b120 46 4c 4f 41 54 20 20 20 20 20 20 20 20 20 20 20  FLOAT           
5b130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
5b140 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 4c  30.#define TK_BL
5b150 4f 42 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OB              
5b160 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 31               131
5b170 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 47 49  .#define TK_REGI
5b180 53 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20  STER            
5b190 20 20 20 20 20 20 20 20 20 20 20 31 33 32 0a 23             132.#
5b1a0 64 65 66 69 6e 65 20 54 4b 5f 56 41 52 49 41 42  define TK_VARIAB
5b1b0 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LE              
5b1c0 20 20 20 20 20 20 20 20 20 31 33 33 0a 23 64 65           133.#de
5b1d0 66 69 6e 65 20 54 4b 5f 43 41 53 45 20 20 20 20  fine TK_CASE    
5b1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b1f0 20 20 20 20 20 20 20 31 33 34 0a 23 64 65 66 69         134.#defi
5b200 6e 65 20 54 4b 5f 57 48 45 4e 20 20 20 20 20 20  ne TK_WHEN      
5b210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b220 20 20 20 20 20 31 33 35 0a 23 64 65 66 69 6e 65       135.#define
5b230 20 54 4b 5f 54 48 45 4e 20 20 20 20 20 20 20 20   TK_THEN        
5b240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b250 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20 54     136.#define T
5b260 4b 5f 45 4c 53 45 20 20 20 20 20 20 20 20 20 20  K_ELSE          
5b270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b280 20 31 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f   137.#define TK_
5b290 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20  INDEX           
5b2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
5b2b0 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4c  38.#define TK_AL
5b2c0 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  TER             
5b2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 39               139
5b2e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 44 44 20  .#define TK_ADD 
5b2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b300 20 20 20 20 20 20 20 20 20 20 20 31 34 30 0a 23             140.#
5b310 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 54 45 58  define TK_TO_TEX
5b320 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
5b330 20 20 20 20 20 20 20 20 20 31 34 31 0a 23 64 65           141.#de
5b340 66 69 6e 65 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20  fine TK_TO_BLOB 
5b350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b360 20 20 20 20 20 20 20 31 34 32 0a 23 64 65 66 69         142.#defi
5b370 6e 65 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43  ne TK_TO_NUMERIC
5b380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b390 20 20 20 20 20 31 34 33 0a 23 64 65 66 69 6e 65       143.#define
5b3a0 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 20 20 20   TK_TO_INT      
5b3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b3c0 20 20 20 31 34 34 0a 23 64 65 66 69 6e 65 20 54     144.#define T
5b3d0 4b 5f 54 4f 5f 52 45 41 4c 20 20 20 20 20 20 20  K_TO_REAL       
5b3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b3f0 20 31 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f   145.#define TK_
5b400 49 53 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20  ISNOT           
5b410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
5b420 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e  46.#define TK_EN
5b430 44 5f 4f 46 5f 46 49 4c 45 20 20 20 20 20 20 20  D_OF_FILE       
5b440 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 37               147
5b450 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4c 4c 45  .#define TK_ILLE
5b460 47 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  GAL             
5b470 20 20 20 20 20 20 20 20 20 20 20 31 34 38 0a 23             148.#
5b480 64 65 66 69 6e 65 20 54 4b 5f 53 50 41 43 45 20  define TK_SPACE 
5b490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b4a0 20 20 20 20 20 20 20 20 20 31 34 39 0a 23 64 65           149.#de
5b4b0 66 69 6e 65 20 54 4b 5f 55 4e 43 4c 4f 53 45 44  fine TK_UNCLOSED
5b4c0 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20  _STRING         
5b4d0 20 20 20 20 20 20 20 31 35 30 0a 23 64 65 66 69         150.#defi
5b4e0 6e 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 20  ne TK_FUNCTION  
5b4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b500 20 20 20 20 20 31 35 31 0a 23 64 65 66 69 6e 65       151.#define
5b510 20 54 4b 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20   TK_COLUMN      
5b520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b530 20 20 20 31 35 32 0a 23 64 65 66 69 6e 65 20 54     152.#define T
5b540 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 20  K_AGG_FUNCTION  
5b550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b560 20 31 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f   153.#define TK_
5b570 41 47 47 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20  AGG_COLUMN      
5b580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
5b590 35 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f  54.#define TK_CO
5b5a0 4e 53 54 5f 46 55 4e 43 20 20 20 20 20 20 20 20  NST_FUNC        
5b5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 35               155
5b5c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4d 49 4e  .#define TK_UMIN
5b5d0 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20  US              
5b5e0 20 20 20 20 20 20 20 20 20 20 20 31 35 36 0a 23             156.#
5b5f0 64 65 66 69 6e 65 20 54 4b 5f 55 50 4c 55 53 20  define TK_UPLUS 
5b600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b610 20 20 20 20 20 20 20 20 20 31 35 37 0a 0a 2f 2a           157../*
5b620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
5b630 64 20 6f 66 20 70 61 72 73 65 2e 68 20 2a 2a 2a  d of parse.h ***
5b640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5b650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5b660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
5b670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
5b680 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
5b690 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71  e left off in sq
5b6a0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
5b6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69  ************/.#i
5b6c0 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e  nclude <stdio.h>
5b6d0 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69  .#include <stdli
5b6e0 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73  b.h>.#include <s
5b6f0 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64  tring.h>.#includ
5b700 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 6e  e <assert.h>.#in
5b710 63 6c 75 64 65 20 3c 73 74 64 64 65 66 2e 68 3e  clude <stddef.h>
5b720 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69  ../*.** If compi
5b730 6c 69 6e 67 20 66 6f 72 20 61 20 70 72 6f 63 65  ling for a proce
5b740 73 73 6f 72 20 74 68 61 74 20 6c 61 63 6b 73 20  ssor that lacks 
5b750 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73  floating point s
5b760 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 62 73 74  upport,.** subst
5b770 69 74 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f  itute integer fo
5b780 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74  r floating-point
5b790 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
5b7a0 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
5b7b0 50 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65 20 64  POINT.# define d
5b7c0 6f 75 62 6c 65 20 73 71 6c 69 74 65 5f 69 6e 74  ouble sqlite_int
5b7d0 36 34 0a 23 20 64 65 66 69 6e 65 20 66 6c 6f 61  64.# define floa
5b7e0 74 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23  t sqlite_int64.#
5b7f0 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42   define LONGDOUB
5b800 4c 45 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 69  LE_TYPE sqlite_i
5b810 6e 74 36 34 0a 23 20 69 66 6e 64 65 66 20 53 51  nt64.# ifndef SQ
5b820 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 20  LITE_BIG_DBL.#  
5b830 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42   define SQLITE_B
5b840 49 47 5f 44 42 4c 20 28 28 28 73 71 6c 69 74 65  IG_DBL (((sqlite
5b850 33 5f 69 6e 74 36 34 29 31 29 3c 3c 35 30 29 0a  3_int64)1)<<50).
5b860 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65  # endif.# define
5b870 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54   SQLITE_OMIT_DAT
5b880 45 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a 23 20  ETIME_FUNCS 1.# 
5b890 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d  define SQLITE_OM
5b8a0 49 54 5f 54 52 41 43 45 20 31 0a 23 20 75 6e 64  IT_TRACE 1.# und
5b8b0 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f  ef SQLITE_MIXED_
5b8c0 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f  ENDIAN_64BIT_FLO
5b8d0 41 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54  AT.# undef SQLIT
5b8e0 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 65 6e  E_HAVE_ISNAN.#en
5b8f0 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
5b900 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66  TE_BIG_DBL.# def
5b910 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44  ine SQLITE_BIG_D
5b920 42 4c 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66  BL (1e99).#endif
5b930 0a 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d  ../*.** OMIT_TEM
5b940 50 44 42 20 69 73 20 73 65 74 20 74 6f 20 31 20  PDB is set to 1 
5b950 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  if SQLITE_OMIT_T
5b960 45 4d 50 44 42 20 69 73 20 64 65 66 69 6e 65 64  EMPDB is defined
5b970 2c 20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77  , or 0.** afterw
5b980 61 72 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73  ard. Having this
5b990 20 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73   macro allows us
5b9a0 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 43 20   to cause the C 
5b9b0 63 6f 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20  compiler .** to 
5b9c0 6f 6d 69 74 20 63 6f 64 65 20 75 73 65 64 20 62  omit code used b
5b9d0 79 20 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69  y TEMP tables wi
5b9e0 74 68 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e  thout messy #ifn
5b9f0 64 65 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  def statements..
5ba00 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
5ba10 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65  _OMIT_TEMPDB.#de
5ba20 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42  fine OMIT_TEMPDB
5ba30 20 31 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65   1.#else.#define
5ba40 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23   OMIT_TEMPDB 0.#
5ba50 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
5ba60 20 22 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e   "file format" n
5ba70 75 6d 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65  umber is an inte
5ba80 67 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72  ger that is incr
5ba90 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72  emented whenever
5baa0 0a 2a 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76  .** the VDBE-lev
5bab0 65 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63  el file format c
5bac0 68 61 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c  hanges.  The fol
5bad0 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65  lowing macros de
5bae0 66 69 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20  fine the.** the 
5baf0 64 65 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72  default file for
5bb00 6d 61 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61  mat for new data
5bb10 62 61 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61  bases and the ma
5bb20 78 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61  ximum file forma
5bb30 74 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69  t.** that the li
5bb40 62 72 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a  brary can read..
5bb50 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5bb60 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41  E_MAX_FILE_FORMA
5bb70 54 20 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  T 4.#ifndef SQLI
5bb80 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f  TE_DEFAULT_FILE_
5bb90 46 4f 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20  FORMAT.# define 
5bba0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
5bbb0 49 4c 45 5f 46 4f 52 4d 41 54 20 34 0a 23 65 6e  ILE_FORMAT 4.#en
5bbc0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72  dif../*.** Deter
5bbd0 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74 72 69  mine whether tri
5bbe0 67 67 65 72 73 20 61 72 65 20 72 65 63 75 72 73  ggers are recurs
5bbf0 69 76 65 20 62 79 20 64 65 66 61 75 6c 74 2e 20  ive by default. 
5bc00 20 54 68 69 73 20 63 61 6e 20 62 65 0a 2a 2a 20   This can be.** 
5bc10 63 68 61 6e 67 65 64 20 61 74 20 72 75 6e 2d 74  changed at run-t
5bc20 69 6d 65 20 75 73 69 6e 67 20 61 20 70 72 61 67  ime using a prag
5bc30 6d 61 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ma..*/.#ifndef S
5bc40 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 52 45  QLITE_DEFAULT_RE
5bc50 43 55 52 53 49 56 45 5f 54 52 49 47 47 45 52 53  CURSIVE_TRIGGERS
5bc60 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
5bc70 5f 44 45 46 41 55 4c 54 5f 52 45 43 55 52 53 49  _DEFAULT_RECURSI
5bc80 56 45 5f 54 52 49 47 47 45 52 53 20 30 0a 23 65  VE_TRIGGERS 0.#e
5bc90 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76  ndif../*.** Prov
5bca0 69 64 65 20 61 20 64 65 66 61 75 6c 74 20 76 61  ide a default va
5bcb0 6c 75 65 20 66 6f 72 20 53 51 4c 49 54 45 5f 54  lue for SQLITE_T
5bcc0 45 4d 50 5f 53 54 4f 52 45 20 69 6e 20 63 61 73  EMP_STORE in cas
5bcd0 65 20 69 74 20 69 73 20 6e 6f 74 20 73 70 65 63  e it is not spec
5bce0 69 66 69 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20  ified.** on the 
5bcf0 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a  command-line.*/.
5bd00 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54  #ifndef SQLITE_T
5bd10 45 4d 50 5f 53 54 4f 52 45 0a 23 20 64 65 66 69  EMP_STORE.# defi
5bd20 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53  ne SQLITE_TEMP_S
5bd30 54 4f 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f  TORE 1.#endif../
5bd40 2a 0a 2a 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f  *.** GCC does no
5bd50 74 20 64 65 66 69 6e 65 20 74 68 65 20 6f 66 66  t define the off
5bd60 73 65 74 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f  setof() macro so
5bd70 20 77 65 27 6c 6c 20 68 61 76 65 20 74 6f 20 64   we'll have to d
5bd80 6f 20 69 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65  o it.** ourselve
5bd90 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66  s..*/.#ifndef of
5bda0 66 73 65 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f  fsetof.#define o
5bdb0 66 66 73 65 74 6f 66 28 53 54 52 55 43 54 55 52  ffsetof(STRUCTUR
5bdc0 45 2c 46 49 45 4c 44 29 20 28 28 69 6e 74 29 28  E,FIELD) ((int)(
5bdd0 28 63 68 61 72 2a 29 26 28 28 53 54 52 55 43 54  (char*)&((STRUCT
5bde0 55 52 45 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29  URE*)0)->FIELD))
5bdf0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
5be00 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
5be10 68 69 73 20 6d 61 63 68 69 6e 65 20 75 73 65 73  his machine uses
5be20 20 45 42 43 44 49 43 2e 20 20 28 59 65 73 2c 20   EBCDIC.  (Yes, 
5be30 62 65 6c 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a  believe it or.**
5be40 20 6e 6f 74 2c 20 74 68 65 72 65 20 61 72 65 20   not, there are 
5be50 73 74 69 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f  still machines o
5be60 75 74 20 74 68 65 72 65 20 74 68 61 74 20 75 73  ut there that us
5be70 65 20 45 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69  e EBCDIC.).*/.#i
5be80 66 20 27 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a  f 'A' == '\301'.
5be90 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
5bea0 45 42 43 44 49 43 20 31 0a 23 65 6c 73 65 0a 23  EBCDIC 1.#else.#
5beb0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41   define SQLITE_A
5bec0 53 43 49 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f  SCII 1.#endif../
5bed0 2a 0a 2a 2a 20 49 6e 74 65 67 65 72 73 20 6f 66  *.** Integers of
5bee0 20 6b 6e 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54   known sizes.  T
5bef0 68 65 73 65 20 74 79 70 65 64 65 66 73 20 6d 69  hese typedefs mi
5bf00 67 68 74 20 63 68 61 6e 67 65 20 66 6f 72 20 61  ght change for a
5bf10 72 63 68 69 74 65 63 74 75 72 65 73 0a 2a 2a 20  rchitectures.** 
5bf20 77 68 65 72 65 20 74 68 65 20 73 69 7a 65 73 20  where the sizes 
5bf30 76 65 72 79 2e 20 20 50 72 65 70 72 6f 63 65 73  very.  Preproces
5bf40 73 6f 72 20 6d 61 63 72 6f 73 20 61 72 65 20 61  sor macros are a
5bf50 76 61 69 6c 61 62 6c 65 20 73 6f 20 74 68 61 74  vailable so that
5bf60 20 74 68 65 0a 2a 2a 20 74 79 70 65 73 20 63 61   the.** types ca
5bf70 6e 20 62 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c  n be convenientl
5bf80 79 20 72 65 64 65 66 69 6e 65 64 20 61 74 20 63  y redefined at c
5bf90 6f 6d 70 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69  ompile-type.  Li
5bfa0 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
5bfb0 20 20 20 20 20 20 20 63 63 20 27 2d 44 55 49 4e         cc '-DUIN
5bfc0 54 50 54 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c  TPTR_TYPE=long l
5bfd0 6f 6e 67 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a  ong int' ....*/.
5bfe0 23 69 66 6e 64 65 66 20 55 49 4e 54 33 32 5f 54  #ifndef UINT32_T
5bff0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45  YPE.# ifdef HAVE
5c000 5f 55 49 4e 54 33 32 5f 54 0a 23 20 20 64 65 66  _UINT32_T.#  def
5c010 69 6e 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20  ine UINT32_TYPE 
5c020 75 69 6e 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a  uint32_t.# else.
5c030 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 33 32  #  define UINT32
5c040 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 69  _TYPE unsigned i
5c050 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  nt.# endif.#endi
5c060 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 31 36  f.#ifndef UINT16
5c070 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41  _TYPE.# ifdef HA
5c080 56 45 5f 55 49 4e 54 31 36 5f 54 0a 23 20 20 64  VE_UINT16_T.#  d
5c090 65 66 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50  efine UINT16_TYP
5c0a0 45 20 75 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73  E uint16_t.# els
5c0b0 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54  e.#  define UINT
5c0c0 31 36 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64  16_TYPE unsigned
5c0d0 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64   short int.# end
5c0e0 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  if.#endif.#ifnde
5c0f0 66 20 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 69  f INT16_TYPE.# i
5c100 66 64 65 66 20 48 41 56 45 5f 49 4e 54 31 36 5f  fdef HAVE_INT16_
5c110 54 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 31  T.#  define INT1
5c120 36 5f 54 59 50 45 20 69 6e 74 31 36 5f 74 0a 23  6_TYPE int16_t.#
5c130 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20   else.#  define 
5c140 49 4e 54 31 36 5f 54 59 50 45 20 73 68 6f 72 74  INT16_TYPE short
5c150 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e   int.# endif.#en
5c160 64 69 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54  dif.#ifndef UINT
5c170 38 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48  8_TYPE.# ifdef H
5c180 41 56 45 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64  AVE_UINT8_T.#  d
5c190 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 50 45  efine UINT8_TYPE
5c1a0 20 75 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a   uint8_t.# else.
5c1b0 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 38 5f  #  define UINT8_
5c1c0 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 63 68  TYPE unsigned ch
5c1d0 61 72 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  ar.# endif.#endi
5c1e0 66 0a 23 69 66 6e 64 65 66 20 49 4e 54 38 5f 54  f.#ifndef INT8_T
5c1f0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45  YPE.# ifdef HAVE
5c200 5f 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 69 6e  _INT8_T.#  defin
5c210 65 20 49 4e 54 38 5f 54 59 50 45 20 69 6e 74 38  e INT8_TYPE int8
5c220 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66  _t.# else.#  def
5c230 69 6e 65 20 49 4e 54 38 5f 54 59 50 45 20 73 69  ine INT8_TYPE si
5c240 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e 64 69  gned char.# endi
5c250 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  f.#endif.#ifndef
5c260 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45   LONGDOUBLE_TYPE
5c270 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f  .# define LONGDO
5c280 55 42 4c 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64  UBLE_TYPE long d
5c290 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70  ouble.#endif.typ
5c2a0 65 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36  edef sqlite_int6
5c2b0 34 20 69 36 34 3b 20 20 20 20 20 20 20 20 20 20  4 i64;          
5c2c0 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 8-byte signed
5c2d0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
5c2e0 64 65 66 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  def sqlite_uint6
5c2f0 34 20 75 36 34 3b 20 20 20 20 20 20 20 20 20 2f  4 u64;         /
5c300 2a 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65  * 8-byte unsigne
5c310 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70  d integer */.typ
5c320 65 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45  edef UINT32_TYPE
5c330 20 75 33 32 3b 20 20 20 20 20 20 20 20 20 20 20   u32;           
5c340 2f 2a 20 34 2d 62 79 74 65 20 75 6e 73 69 67 6e  /* 4-byte unsign
5c350 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79  ed integer */.ty
5c360 70 65 64 65 66 20 55 49 4e 54 31 36 5f 54 59 50  pedef UINT16_TYP
5c370 45 20 75 31 36 3b 20 20 20 20 20 20 20 20 20 20  E u16;          
5c380 20 2f 2a 20 32 2d 62 79 74 65 20 75 6e 73 69 67   /* 2-byte unsig
5c390 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74  ned integer */.t
5c3a0 79 70 65 64 65 66 20 49 4e 54 31 36 5f 54 59 50  ypedef INT16_TYP
5c3b0 45 20 69 31 36 3b 20 20 20 20 20 20 20 20 20 20  E i16;          
5c3c0 20 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e    /* 2-byte sign
5c3d0 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79  ed integer */.ty
5c3e0 70 65 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45  pedef UINT8_TYPE
5c3f0 20 75 38 3b 20 20 20 20 20 20 20 20 20 20 20 20   u8;            
5c400 20 2f 2a 20 31 2d 62 79 74 65 20 75 6e 73 69 67   /* 1-byte unsig
5c410 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74  ned integer */.t
5c420 79 70 65 64 65 66 20 49 4e 54 38 5f 54 59 50 45  ypedef INT8_TYPE
5c430 20 69 38 3b 20 20 20 20 20 20 20 20 20 20 20 20   i8;            
5c440 20 20 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e    /* 1-byte sign
5c450 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f  ed integer */../
5c460 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f  *.** SQLITE_MAX_
5c470 55 33 32 20 69 73 20 61 20 75 36 34 20 63 6f 6e  U32 is a u64 con
5c480 73 74 61 6e 74 20 74 68 61 74 20 69 73 20 74 68  stant that is th
5c490 65 20 6d 61 78 69 6d 75 6d 20 75 36 34 20 76 61  e maximum u64 va
5c4a0 6c 75 65 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20  lue.** that can 
5c4b0 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 75  be stored in a u
5c4c0 33 32 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20  32 without loss 
5c4d0 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20 76 61  of data.  The va
5c4e0 6c 75 65 0a 2a 2a 20 69 73 20 30 78 30 30 30 30  lue.** is 0x0000
5c4f0 30 30 30 30 66 66 66 66 66 66 66 66 2e 20 20 42  0000ffffffff.  B
5c500 75 74 20 62 65 63 61 75 73 65 20 6f 66 20 71 75  ut because of qu
5c510 69 72 6b 73 20 6f 66 20 73 6f 6d 65 20 63 6f 6d  irks of some com
5c520 70 69 6c 65 72 73 2c 20 77 65 0a 2a 2a 20 68 61  pilers, we.** ha
5c530 76 65 20 74 6f 20 73 70 65 63 69 66 79 20 74 68  ve to specify th
5c540 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 6c  e value in the l
5c550 65 73 73 20 69 6e 74 75 69 74 69 76 65 20 6d 61  ess intuitive ma
5c560 6e 6e 65 72 20 73 68 6f 77 6e 3a 0a 2a 2f 0a 23  nner shown:.*/.#
5c570 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
5c580 58 5f 55 33 32 20 20 28 28 28 28 75 36 34 29 31  X_U32  ((((u64)1
5c590 29 3c 3c 33 32 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a  )<<32)-1)../*.**
5c5a0 20 54 68 65 20 64 61 74 61 74 79 70 65 20 75 73   The datatype us
5c5b0 65 64 20 74 6f 20 73 74 6f 72 65 20 65 73 74 69  ed to store esti
5c5c0 6d 61 74 65 73 20 6f 66 20 74 68 65 20 6e 75 6d  mates of the num
5c5d0 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 61  ber of rows in a
5c5e0 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  .** table or ind
5c5f0 65 78 2e 20 20 54 68 69 73 20 69 73 20 61 6e 20  ex.  This is an 
5c600 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  unsigned integer
5c610 20 74 79 70 65 2e 20 20 46 6f 72 20 39 39 2e 39   type.  For 99.9
5c620 25 20 6f 66 0a 2a 2a 20 74 68 65 20 77 6f 72 6c  % of.** the worl
5c630 64 2c 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65  d, a 32-bit inte
5c640 67 65 72 20 69 73 20 73 75 66 66 69 63 69 65 6e  ger is sufficien
5c650 74 2e 20 20 42 75 74 20 61 20 36 34 2d 62 69 74  t.  But a 64-bit
5c660 20 69 6e 74 65 67 65 72 0a 2a 2a 20 63 61 6e 20   integer.** can 
5c670 62 65 20 75 73 65 64 20 61 74 20 63 6f 6d 70 69  be used at compi
5c680 6c 65 2d 74 69 6d 65 20 69 66 20 64 65 73 69 72  le-time if desir
5c690 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ed..*/.#ifdef SQ
5c6a0 4c 49 54 45 5f 36 34 42 49 54 5f 53 54 41 54 53  LITE_64BIT_STATS
5c6b0 0a 20 74 79 70 65 64 65 66 20 75 36 34 20 74 52  . typedef u64 tR
5c6c0 6f 77 63 6e 74 3b 20 20 20 20 2f 2a 20 36 34 2d  owcnt;    /* 64-
5c6d0 62 69 74 20 6f 6e 6c 79 20 69 66 20 72 65 71 75  bit only if requ
5c6e0 65 73 74 65 64 20 61 74 20 63 6f 6d 70 69 6c 65  ested at compile
5c6f0 2d 74 69 6d 65 20 2a 2f 0a 23 65 6c 73 65 0a 20  -time */.#else. 
5c700 74 79 70 65 64 65 66 20 75 33 32 20 74 52 6f 77  typedef u32 tRow
5c710 63 6e 74 3b 20 20 20 20 2f 2a 20 33 32 2d 62 69  cnt;    /* 32-bi
5c720 74 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74  t is the default
5c730 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   */.#endif../*.*
5c740 2a 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65  * Macros to dete
5c750 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74 68  rmine whether th
5c760 65 20 6d 61 63 68 69 6e 65 20 69 73 20 62 69 67  e machine is big
5c770 20 6f 72 20 6c 69 74 74 6c 65 20 65 6e 64 69 61   or little endia
5c780 6e 2c 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20  n,.** evaluated 
5c790 61 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23  at runtime..*/.#
5c7a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41  ifdef SQLITE_AMA
5c7b0 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45  LGAMATION.SQLITE
5c7c0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69  _PRIVATE const i
5c7d0 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20  nt sqlite3one = 
5c7e0 31 3b 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f  1;.#else.SQLITE_
5c7f0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e  PRIVATE const in
5c800 74 20 73 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 65  t sqlite3one;.#e
5c810 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64  ndif.#if defined
5c820 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65  (i386) || define
5c830 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64  d(__i386__) || d
5c840 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 5c  efined(_M_IX86)\
5c850 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
5c860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
5c870 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36   defined(__x86_6
5c880 34 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  4) || defined(__
5c890 78 38 36 5f 36 34 5f 5f 29 0a 23 20 64 65 66 69  x86_64__).# defi
5c8a0 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44  ne SQLITE_BIGEND
5c8b0 49 41 4e 20 20 20 20 30 0a 23 20 64 65 66 69 6e  IAN    0.# defin
5c8c0 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45  e SQLITE_LITTLEE
5c8d0 4e 44 49 41 4e 20 31 0a 23 20 64 65 66 69 6e 65  NDIAN 1.# define
5c8e0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
5c8f0 49 56 45 20 20 53 51 4c 49 54 45 5f 55 54 46 31  IVE  SQLITE_UTF1
5c900 36 4c 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  6LE.#else.# defi
5c910 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44  ne SQLITE_BIGEND
5c920 49 41 4e 20 20 20 20 28 2a 28 63 68 61 72 20 2a  IAN    (*(char *
5c930 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d  )(&sqlite3one)==
5c940 30 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  0).# define SQLI
5c950 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20  TE_LITTLEENDIAN 
5c960 28 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69  (*(char *)(&sqli
5c970 74 65 33 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 65  te3one)==1).# de
5c980 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31  fine SQLITE_UTF1
5c990 36 4e 41 54 49 56 45 20 28 53 51 4c 49 54 45 5f  6NATIVE (SQLITE_
5c9a0 42 49 47 45 4e 44 49 41 4e 3f 53 51 4c 49 54 45  BIGENDIAN?SQLITE
5c9b0 5f 55 54 46 31 36 42 45 3a 53 51 4c 49 54 45 5f  _UTF16BE:SQLITE_
5c9c0 55 54 46 31 36 4c 45 29 0a 23 65 6e 64 69 66 0a  UTF16LE).#endif.
5c9d0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 73  ./*.** Constants
5c9e0 20 66 6f 72 20 74 68 65 20 6c 61 72 67 65 73 74   for the largest
5c9f0 20 61 6e 64 20 73 6d 61 6c 6c 65 73 74 20 70 6f   and smallest po
5ca00 73 73 69 62 6c 65 20 36 34 2d 62 69 74 20 73 69  ssible 64-bit si
5ca10 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a  gned integers..*
5ca20 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 61  * These macros a
5ca30 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20 77  re designed to w
5ca40 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e  ork correctly on
5ca50 20 62 6f 74 68 20 33 32 2d 62 69 74 20 61 6e 64   both 32-bit and
5ca60 20 36 34 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70 69   64-bit.** compi
5ca70 6c 65 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  lers..*/.#define
5ca80 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 20 20   LARGEST_INT64  
5ca90 28 30 78 66 66 66 66 66 66 66 66 7c 28 28 28 69  (0xffffffff|(((i
5caa0 36 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c  64)0x7fffffff)<<
5cab0 33 32 29 29 0a 23 64 65 66 69 6e 65 20 53 4d 41  32)).#define SMA
5cac0 4c 4c 45 53 54 5f 49 4e 54 36 34 20 28 28 28 69  LLEST_INT64 (((i
5cad0 36 34 29 2d 31 29 20 2d 20 4c 41 52 47 45 53 54  64)-1) - LARGEST
5cae0 5f 49 4e 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20  _INT64)../* .** 
5caf0 52 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d 62 65  Round up a numbe
5cb00 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c 61  r to the next la
5cb10 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66  rger multiple of
5cb20 20 38 2e 20 20 54 68 69 73 20 69 73 20 75 73 65   8.  This is use
5cb30 64 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38 2d  d.** to force 8-
5cb40 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f  byte alignment o
5cb50 6e 20 36 34 2d 62 69 74 20 61 72 63 68 69 74 65  n 64-bit archite
5cb60 63 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 69  ctures..*/.#defi
5cb70 6e 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20 20  ne ROUND8(x)    
5cb80 20 28 28 28 78 29 2b 37 29 26 7e 37 29 0a 0a 2f   (((x)+7)&~7)../
5cb90 2a 0a 2a 2a 20 52 6f 75 6e 64 20 64 6f 77 6e 20  *.** Round down 
5cba0 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 6d  to the nearest m
5cbb0 75 6c 74 69 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a  ultiple of 8.*/.
5cbc0 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 44 4f 57  #define ROUNDDOW
5cbd0 4e 38 28 78 29 20 28 28 78 29 26 7e 37 29 0a 0a  N8(x) ((x)&~7)..
5cbe0 2f 2a 0a 2a 2a 20 41 73 73 65 72 74 20 74 68 61  /*.** Assert tha
5cbf0 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 58 20  t the pointer X 
5cc00 69 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e  is aligned to an
5cc10 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79   8-byte boundary
5cc20 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61 63 72 6f  .  This.** macro
5cc30 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 77 69   is used only wi
5cc40 74 68 69 6e 20 61 73 73 65 72 74 28 29 20 74 6f  thin assert() to
5cc50 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65   verify that the
5cc60 20 63 6f 64 65 20 67 65 74 73 0a 2a 2a 20 61 6c   code gets.** al
5cc70 6c 20 61 6c 69 67 6e 6d 65 6e 74 20 72 65 73 74  l alignment rest
5cc80 72 69 63 74 69 6f 6e 73 20 63 6f 72 72 65 63 74  rictions correct
5cc90 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c 20  ..**.** Except, 
5cca0 69 66 20 53 51 4c 49 54 45 5f 34 5f 42 59 54 45  if SQLITE_4_BYTE
5ccb0 5f 41 4c 49 47 4e 45 44 5f 4d 41 4c 4c 4f 43 20  _ALIGNED_MALLOC 
5ccc0 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e  is defined, then
5ccd0 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69   the.** underlyi
5cce0 6e 67 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c  ng malloc() impl
5ccf0 65 6d 65 6e 74 69 6f 6e 20 6d 69 67 68 74 20 72  emention might r
5cd00 65 74 75 72 6e 20 75 73 20 34 2d 62 79 74 65 20  eturn us 4-byte 
5cd10 61 6c 69 67 6e 65 64 0a 2a 2a 20 70 6f 69 6e 74  aligned.** point
5cd20 65 72 73 2e 20 20 49 6e 20 74 68 61 74 20 63 61  ers.  In that ca
5cd30 73 65 2c 20 6f 6e 6c 79 20 76 65 72 69 66 79 20  se, only verify 
5cd40 34 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74  4-byte alignment
5cd50 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
5cd60 54 45 5f 34 5f 42 59 54 45 5f 41 4c 49 47 4e 45  TE_4_BYTE_ALIGNE
5cd70 44 5f 4d 41 4c 4c 4f 43 0a 23 20 64 65 66 69 6e  D_MALLOC.# defin
5cd80 65 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  e EIGHT_BYTE_ALI
5cd90 47 4e 4d 45 4e 54 28 58 29 20 20 20 28 28 28 28  GNMENT(X)   ((((
5cda0 63 68 61 72 2a 29 28 58 29 20 2d 20 28 63 68 61  char*)(X) - (cha
5cdb0 72 2a 29 30 29 26 33 29 3d 3d 30 29 0a 23 65 6c  r*)0)&3)==0).#el
5cdc0 73 65 0a 23 20 64 65 66 69 6e 65 20 45 49 47 48  se.# define EIGH
5cdd0 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54  T_BYTE_ALIGNMENT
5cde0 28 58 29 20 20 20 28 28 28 28 63 68 61 72 2a 29  (X)   ((((char*)
5cdf0 28 58 29 20 2d 20 28 63 68 61 72 2a 29 30 29 26  (X) - (char*)0)&
5ce00 37 29 3d 3d 30 29 0a 23 65 6e 64 69 66 0a 0a 0a  7)==0).#endif...
5ce10 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
5ce20 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
5ce30 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ng structure is 
5ce40 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  used to store th
5ce50 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a  e busy-handler.*
5ce60 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61  * callback for a
5ce70 20 67 69 76 65 6e 20 73 71 6c 69 74 65 20 68 61   given sqlite ha
5ce80 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ndle. .**.** The
5ce90 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64   sqlite.busyHand
5cea0 6c 65 72 20 6d 65 6d 62 65 72 20 6f 66 20 74 68  ler member of th
5ceb0 65 20 73 71 6c 69 74 65 20 73 74 72 75 63 74 20  e sqlite struct 
5cec0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 62 75 73  contains the bus
5ced0 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f  y.** callback fo
5cee0 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  r the database h
5cef0 61 6e 64 6c 65 2e 20 45 61 63 68 20 70 61 67 65  andle. Each page
5cf00 72 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68 65  r opened via the
5cf10 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c   sqlite.** handl
5cf20 65 20 69 73 20 70 61 73 73 65 64 20 61 20 70 6f  e is passed a po
5cf30 69 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65 2e  inter to sqlite.
5cf40 62 75 73 79 48 61 6e 64 6c 65 72 2e 20 54 68 65  busyHandler. The
5cf50 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a   busy-handler.**
5cf60 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 75 72   callback is cur
5cf70 72 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20 6f  rently invoked o
5cf80 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  nly from within 
5cf90 70 61 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79 70 65  pager.c..*/.type
5cfa0 64 65 66 20 73 74 72 75 63 74 20 42 75 73 79 48  def struct BusyH
5cfb0 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e 64 6c  andler BusyHandl
5cfc0 65 72 3b 0a 73 74 72 75 63 74 20 42 75 73 79 48  er;.struct BusyH
5cfd0 61 6e 64 6c 65 72 20 7b 0a 20 20 69 6e 74 20 28  andler {.  int (
5cfe0 2a 78 46 75 6e 63 29 28 76 6f 69 64 20 2a 2c 69  *xFunc)(void *,i
5cff0 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 62 75 73  nt);  /* The bus
5d000 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20  y callback */.  
5d010 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20 20 20  void *pArg;     
5d020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
5d030 72 73 74 20 61 72 67 20 74 6f 20 62 75 73 79 20  rst arg to busy 
5d040 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e  callback */.  in
5d050 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20 20  t nBusy;        
5d060 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72           /* Incr
5d070 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63  emented with eac
5d080 68 20 62 75 73 79 20 63 61 6c 6c 20 2a 2f 0a 7d  h busy call */.}
5d090 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66  ;../*.** Name of
5d0a0 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 61   the master data
5d0b0 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54 68 65  base table.  The
5d0c0 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65   master database
5d0d0 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20 73   table.** is a s
5d0e0 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74 68 61  pecial table tha
5d0f0 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65  t holds the name
5d100 73 20 61 6e 64 20 61 74 74 72 69 62 75 74 65 73  s and attributes
5d110 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65 72 20   of all.** user 
5d120 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63  tables and indic
5d130 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  es..*/.#define M
5d140 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20 20 20  ASTER_NAME      
5d150 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22   "sqlite_master"
5d160 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d 41  .#define TEMP_MA
5d170 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c 69  STER_NAME  "sqli
5d180 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 0a  te_temp_master".
5d190 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 2d  ./*.** The root-
5d1a0 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73 74  page of the mast
5d1b0 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  er database tabl
5d1c0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41  e..*/.#define MA
5d1d0 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20 20 20  STER_ROOT       
5d1e0 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d  1../*.** The nam
5d1f0 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20  e of the schema 
5d200 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  table..*/.#defin
5d210 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 78  e SCHEMA_TABLE(x
5d220 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d 50 44  )  ((!OMIT_TEMPD
5d230 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d 50 5f  B)&&(x==1)?TEMP_
5d240 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41 53 54  MASTER_NAME:MAST
5d250 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20  ER_NAME)../*.** 
5d260 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6d 61  A convenience ma
5d270 63 72 6f 20 74 68 61 74 20 72 65 74 75 72 6e 73  cro that returns
5d280 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
5d290 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 61 6e  lements in.** an
5d2a0 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65 66 69   array..*/.#defi
5d2b0 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58 29 20  ne ArraySize(X) 
5d2c0 20 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66     ((int)(sizeof
5d2d0 28 58 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d 29  (X)/sizeof(X[0])
5d2e0 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  ))../*.** The fo
5d2f0 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 61 73  llowing value as
5d300 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 6d 65   a destructor me
5d310 61 6e 73 20 74 6f 20 75 73 65 20 73 71 6c 69 74  ans to use sqlit
5d320 65 33 44 62 46 72 65 65 28 29 2e 0a 2a 2a 20 54  e3DbFree()..** T
5d330 68 65 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  he sqlite3DbFree
5d340 28 29 20 72 6f 75 74 69 6e 65 20 72 65 71 75 69  () routine requi
5d350 72 65 73 20 74 77 6f 20 70 61 72 61 6d 65 74 65  res two paramete
5d360 72 73 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68  rs instead of th
5d370 65 20 0a 2a 2a 20 6f 6e 65 20 70 61 72 61 6d 65  e .** one parame
5d380 74 65 72 20 74 68 61 74 20 64 65 73 74 72 75 63  ter that destruc
5d390 74 6f 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 77 61  tors normally wa
5d3a0 6e 74 2e 20 20 53 6f 20 77 65 20 68 61 76 65 20  nt.  So we have 
5d3b0 74 6f 20 69 6e 74 72 6f 64 75 63 65 20 0a 2a 2a  to introduce .**
5d3c0 20 74 68 69 73 20 6d 61 67 69 63 20 76 61 6c 75   this magic valu
5d3d0 65 20 74 68 61 74 20 74 68 65 20 63 6f 64 65 20  e that the code 
5d3e0 6b 6e 6f 77 73 20 74 6f 20 68 61 6e 64 6c 65 20  knows to handle 
5d3f0 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 41 6e  differently.  An
5d400 79 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 77 69  y .** pointer wi
5d410 6c 6c 20 77 6f 72 6b 20 68 65 72 65 20 61 73 20  ll work here as 
5d420 6c 6f 6e 67 20 61 73 20 69 74 20 69 73 20 64 69  long as it is di
5d430 73 74 69 6e 63 74 20 66 72 6f 6d 20 53 51 4c 49  stinct from SQLI
5d440 54 45 5f 53 54 41 54 49 43 0a 2a 2a 20 61 6e 64  TE_STATIC.** and
5d450 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
5d460 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  T..*/.#define SQ
5d470 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 20 20 28  LITE_DYNAMIC   (
5d480 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63  (sqlite3_destruc
5d490 74 6f 72 5f 74 79 70 65 29 73 71 6c 69 74 65 33  tor_type)sqlite3
5d4a0 4d 61 6c 6c 6f 63 53 69 7a 65 29 0a 0a 2f 2a 0a  MallocSize)../*.
5d4b0 2a 2a 20 57 68 65 6e 20 53 51 4c 49 54 45 5f 4f  ** When SQLITE_O
5d4c0 4d 49 54 5f 57 53 44 20 69 73 20 64 65 66 69 6e  MIT_WSD is defin
5d4d0 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  ed, it means tha
5d4e0 74 20 74 68 65 20 74 61 72 67 65 74 20 70 6c 61  t the target pla
5d4f0 74 66 6f 72 6d 20 64 6f 65 73 0a 2a 2a 20 6e 6f  tform does.** no
5d500 74 20 73 75 70 70 6f 72 74 20 57 72 69 74 61 62  t support Writab
5d510 6c 65 20 53 74 61 74 69 63 20 44 61 74 61 20 28  le Static Data (
5d520 57 53 44 29 20 73 75 63 68 20 61 73 20 67 6c 6f  WSD) such as glo
5d530 62 61 6c 20 61 6e 64 20 73 74 61 74 69 63 20 76  bal and static v
5d540 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 20 41 6c 6c  ariables..** All
5d550 20 76 61 72 69 61 62 6c 65 73 20 6d 75 73 74 20   variables must 
5d560 65 69 74 68 65 72 20 62 65 20 6f 6e 20 74 68 65  either be on the
5d570 20 73 74 61 63 6b 20 6f 72 20 64 79 6e 61 6d 69   stack or dynami
5d580 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  cally allocated 
5d590 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 68 65 61 70  from.** the heap
5d5a0 2e 20 20 57 68 65 6e 20 57 53 44 20 69 73 20 75  .  When WSD is u
5d5b0 6e 73 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20  nsupported, the 
5d5c0 76 61 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61  variable declara
5d5d0 74 69 6f 6e 73 20 73 63 61 74 74 65 72 65 64 0a  tions scattered.
5d5e0 2a 2a 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68  ** throughout th
5d5f0 65 20 53 51 4c 69 74 65 20 63 6f 64 65 20 6d 75  e SQLite code mu
5d600 73 74 20 62 65 63 6f 6d 65 20 63 6f 6e 73 74 61  st become consta
5d610 6e 74 73 20 69 6e 73 74 65 61 64 2e 20 20 54 68  nts instead.  Th
5d620 65 20 53 51 4c 49 54 45 5f 57 53 44 0a 2a 2a 20  e SQLITE_WSD.** 
5d630 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 66 6f  macro is used fo
5d640 72 20 74 68 69 73 20 70 75 72 70 6f 73 65 2e 20  r this purpose. 
5d650 20 41 6e 64 20 69 6e 73 74 65 61 64 20 6f 66 20   And instead of 
5d660 72 65 66 65 72 65 6e 63 69 6e 67 20 74 68 65 20  referencing the 
5d670 76 61 72 69 61 62 6c 65 0a 2a 2a 20 64 69 72 65  variable.** dire
5d680 63 74 6c 79 2c 20 77 65 20 75 73 65 20 69 74 73  ctly, we use its
5d690 20 63 6f 6e 73 74 61 6e 74 20 61 73 20 61 20 6b   constant as a k
5d6a0 65 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65  ey to lookup the
5d6b0 20 72 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61   run-time alloca
5d6c0 74 65 64 0a 2a 2a 20 62 75 66 66 65 72 20 74 68  ted.** buffer th
5d6d0 61 74 20 68 6f 6c 64 73 20 72 65 61 6c 20 76 61  at holds real va
5d6e0 72 69 61 62 6c 65 2e 20 20 54 68 65 20 63 6f 6e  riable.  The con
5d6f0 73 74 61 6e 74 20 69 73 20 61 6c 73 6f 20 74 68  stant is also th
5d700 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 0a 2a 2a  e initializer.**
5d710 20 66 6f 72 20 74 68 65 20 72 75 6e 2d 74 69 6d   for the run-tim
5d720 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66  e allocated buff
5d730 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65  er..**.** In the
5d740 20 75 73 75 61 6c 20 63 61 73 65 20 77 68 65 72   usual case wher
5d750 65 20 57 53 44 20 69 73 20 73 75 70 70 6f 72 74  e WSD is support
5d760 65 64 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 57  ed, the SQLITE_W
5d770 53 44 20 61 6e 64 20 47 4c 4f 42 41 4c 0a 2a 2a  SD and GLOBAL.**
5d780 20 6d 61 63 72 6f 73 20 62 65 63 6f 6d 65 20 6e   macros become n
5d790 6f 2d 6f 70 73 20 61 6e 64 20 68 61 76 65 20 7a  o-ops and have z
5d7a0 65 72 6f 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  ero performance 
5d7b0 69 6d 70 61 63 74 2e 0a 2a 2f 0a 23 69 66 64 65  impact..*/.#ifde
5d7c0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53  f SQLITE_OMIT_WS
5d7d0 44 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49  D.  #define SQLI
5d7e0 54 45 5f 57 53 44 20 63 6f 6e 73 74 0a 20 20 23  TE_WSD const.  #
5d7f0 64 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c  define GLOBAL(t,
5d800 76 29 20 28 2a 28 74 2a 29 73 71 6c 69 74 65 33  v) (*(t*)sqlite3
5d810 5f 77 73 64 5f 66 69 6e 64 28 28 76 6f 69 64 2a  _wsd_find((void*
5d820 29 26 28 76 29 2c 20 73 69 7a 65 6f 66 28 76 29  )&(v), sizeof(v)
5d830 29 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  )).  #define sql
5d840 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
5d850 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 53   GLOBAL(struct S
5d860 71 6c 69 74 65 33 43 6f 6e 66 69 67 2c 20 73 71  qlite3Config, sq
5d870 6c 69 74 65 33 43 6f 6e 66 69 67 29 0a 53 51 4c  lite3Config).SQL
5d880 49 54 45 5f 41 50 49 20 20 20 69 6e 74 20 73 71  ITE_API   int sq
5d890 6c 69 74 65 33 5f 77 73 64 5f 69 6e 69 74 28 69  lite3_wsd_init(i
5d8a0 6e 74 20 4e 2c 20 69 6e 74 20 4a 29 3b 0a 53 51  nt N, int J);.SQ
5d8b0 4c 49 54 45 5f 41 50 49 20 20 20 76 6f 69 64 20  LITE_API   void 
5d8c0 2a 73 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e  *sqlite3_wsd_fin
5d8d0 64 28 76 6f 69 64 20 2a 4b 2c 20 69 6e 74 20 4c  d(void *K, int L
5d8e0 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69  );.#else.  #defi
5d8f0 6e 65 20 53 51 4c 49 54 45 5f 57 53 44 20 0a 20  ne SQLITE_WSD . 
5d900 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28   #define GLOBAL(
5d910 74 2c 76 29 20 76 0a 20 20 23 64 65 66 69 6e 65  t,v) v.  #define
5d920 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
5d930 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66  nfig sqlite3Conf
5d940 69 67 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  ig.#endif../*.**
5d950 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   The following m
5d960 61 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 74  acros are used t
5d970 6f 20 73 75 70 70 72 65 73 73 20 63 6f 6d 70 69  o suppress compi
5d980 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 61 6e 64  ler warnings and
5d990 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 63   to.** make it c
5d9a0 6c 65 61 72 20 74 6f 20 68 75 6d 61 6e 20 72 65  lear to human re
5d9b0 61 64 65 72 73 20 77 68 65 6e 20 61 20 66 75 6e  aders when a fun
5d9c0 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20  ction parameter 
5d9d0 69 73 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20  is deliberately 
5d9e0 0a 2a 2a 20 6c 65 66 74 20 75 6e 75 73 65 64 20  .** left unused 
5d9f0 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20  within the body 
5da00 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 54  of a function. T
5da10 68 69 73 20 75 73 75 61 6c 6c 79 20 68 61 70 70  his usually happ
5da20 65 6e 73 20 77 68 65 6e 0a 2a 2a 20 61 20 66 75  ens when.** a fu
5da30 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
5da40 20 76 69 61 20 61 20 66 75 6e 63 74 69 6f 6e 20   via a function 
5da50 70 6f 69 6e 74 65 72 2e 20 46 6f 72 20 65 78 61  pointer. For exa
5da60 6d 70 6c 65 20 74 68 65 20 0a 2a 2a 20 69 6d 70  mple the .** imp
5da70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  lementation of a
5da80 6e 20 53 51 4c 20 61 67 67 72 65 67 61 74 65 20  n SQL aggregate 
5da90 73 74 65 70 20 63 61 6c 6c 62 61 63 6b 20 6d 61  step callback ma
5daa0 79 20 6e 6f 74 20 75 73 65 20 74 68 65 0a 2a 2a  y not use the.**
5dab0 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63   parameter indic
5dac0 61 74 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72  ating the number
5dad0 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 70 61   of arguments pa
5dae0 73 73 65 64 20 74 6f 20 74 68 65 20 61 67 67 72  ssed to the aggr
5daf0 65 67 61 74 65 2c 0a 2a 2a 20 69 66 20 69 74 20  egate,.** if it 
5db00 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 69 73 20  knows that this 
5db10 69 73 20 65 6e 66 6f 72 63 65 64 20 65 6c 73 65  is enforced else
5db20 77 68 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  where..**.** Whe
5db30 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 61 72  n a function par
5db40 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73  ameter is not us
5db50 65 64 20 61 74 20 61 6c 6c 20 77 69 74 68 69 6e  ed at all within
5db60 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 66   the body of a f
5db70 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 69 74 20 69  unction,.** it i
5db80 73 20 67 65 6e 65 72 61 6c 6c 79 20 6e 61 6d 65  s generally name
5db90 64 20 22 4e 6f 74 55 73 65 64 22 20 6f 72 20 22  d "NotUsed" or "
5dba0 4e 6f 74 55 73 65 64 32 22 20 74 6f 20 6d 61 6b  NotUsed2" to mak
5dbb0 65 20 74 68 69 6e 67 73 20 65 76 65 6e 20 63 6c  e things even cl
5dbc0 65 61 72 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65  earer..** Howeve
5dbd0 72 2c 20 74 68 65 73 65 20 6d 61 63 72 6f 73 20  r, these macros 
5dbe0 6d 61 79 20 61 6c 73 6f 20 62 65 20 75 73 65 64  may also be used
5dbf0 20 74 6f 20 73 75 70 70 72 65 73 73 20 77 61 72   to suppress war
5dc00 6e 69 6e 67 73 20 72 65 6c 61 74 65 64 20 74 6f  nings related to
5dc10 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 74  .** parameters t
5dc20 68 61 74 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e  hat may or may n
5dc30 6f 74 20 62 65 20 75 73 65 64 20 64 65 70 65 6e  ot be used depen
5dc40 64 69 6e 67 20 6f 6e 20 63 6f 6d 70 69 6c 61 74  ding on compilat
5dc50 69 6f 6e 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 20  ion options..** 
5dc60 46 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 6f 73  For example thos
5dc70 65 20 70 61 72 61 6d 65 74 65 72 73 20 6f 6e 6c  e parameters onl
5dc80 79 20 75 73 65 64 20 69 6e 20 61 73 73 65 72 74  y used in assert
5dc90 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49  () statements. I
5dca0 6e 20 74 68 65 73 65 0a 2a 2a 20 63 61 73 65 73  n these.** cases
5dcb0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20   the parameters 
5dcc0 61 72 65 20 6e 61 6d 65 64 20 61 73 20 70 65 72  are named as per
5dcd0 20 74 68 65 20 75 73 75 61 6c 20 63 6f 6e 76 65   the usual conve
5dce0 6e 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69  ntions..*/.#defi
5dcf0 6e 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  ne UNUSED_PARAME
5dd00 54 45 52 28 78 29 20 28 76 6f 69 64 29 28 78 29  TER(x) (void)(x)
5dd10 0a 23 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f  .#define UNUSED_
5dd20 50 41 52 41 4d 45 54 45 52 32 28 78 2c 79 29 20  PARAMETER2(x,y) 
5dd30 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
5dd40 28 78 29 2c 55 4e 55 53 45 44 5f 50 41 52 41 4d  (x),UNUSED_PARAM
5dd50 45 54 45 52 28 79 29 0a 0a 2f 2a 0a 2a 2a 20 46  ETER(y)../*.** F
5dd60 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65  orward reference
5dd70 73 20 74 6f 20 73 74 72 75 63 74 75 72 65 73 0a  s to structures.
5dd80 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
5dd90 74 20 41 67 67 49 6e 66 6f 20 41 67 67 49 6e 66  t AggInfo AggInf
5dda0 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  o;.typedef struc
5ddb0 74 20 41 75 74 68 43 6f 6e 74 65 78 74 20 41 75  t AuthContext Au
5ddc0 74 68 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64  thContext;.typed
5ddd0 65 66 20 73 74 72 75 63 74 20 41 75 74 6f 69 6e  ef struct Autoin
5dde0 63 49 6e 66 6f 20 41 75 74 6f 69 6e 63 49 6e 66  cInfo AutoincInf
5ddf0 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  o;.typedef struc
5de00 74 20 42 69 74 76 65 63 20 42 69 74 76 65 63 3b  t Bitvec Bitvec;
5de10 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
5de20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65 71 3b  CollSeq CollSeq;
5de30 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
5de40 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b 0a 74  Column Column;.t
5de50 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44 62  ypedef struct Db
5de60 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73 74 72   Db;.typedef str
5de70 75 63 74 20 53 63 68 65 6d 61 20 53 63 68 65 6d  uct Schema Schem
5de80 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  a;.typedef struc
5de90 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74 79 70  t Expr Expr;.typ
5dea0 65 64 65 66 20 73 74 72 75 63 74 20 45 78 70 72  edef struct Expr
5deb0 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b 0a 74  List ExprList;.t
5dec0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78  ypedef struct Ex
5ded0 70 72 53 70 61 6e 20 45 78 70 72 53 70 61 6e 3b  prSpan ExprSpan;
5dee0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
5def0 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 65 64  FKey FKey;.typed
5df00 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 65  ef struct FuncDe
5df10 73 74 72 75 63 74 6f 72 20 46 75 6e 63 44 65 73  structor FuncDes
5df20 74 72 75 63 74 6f 72 3b 0a 74 79 70 65 64 65 66  tructor;.typedef
5df30 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 20   struct FuncDef 
5df40 46 75 6e 63 44 65 66 3b 0a 74 79 70 65 64 65 66  FuncDef;.typedef
5df50 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 48   struct FuncDefH
5df60 61 73 68 20 46 75 6e 63 44 65 66 48 61 73 68 3b  ash FuncDefHash;
5df70 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
5df80 49 64 4c 69 73 74 20 49 64 4c 69 73 74 3b 0a 74  IdList IdList;.t
5df90 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e  ypedef struct In
5dfa0 64 65 78 20 49 6e 64 65 78 3b 0a 74 79 70 65 64  dex Index;.typed
5dfb0 65 66 20 73 74 72 75 63 74 20 49 6e 64 65 78 53  ef struct IndexS
5dfc0 61 6d 70 6c 65 20 49 6e 64 65 78 53 61 6d 70 6c  ample IndexSampl
5dfd0 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
5dfe0 74 20 4b 65 79 43 6c 61 73 73 20 4b 65 79 43 6c  t KeyClass KeyCl
5dff0 61 73 73 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ass;.typedef str
5e000 75 63 74 20 4b 65 79 49 6e 66 6f 20 4b 65 79 49  uct KeyInfo KeyI
5e010 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72  nfo;.typedef str
5e020 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 20 4c 6f  uct Lookaside Lo
5e030 6f 6b 61 73 69 64 65 3b 0a 74 79 70 65 64 65 66  okaside;.typedef
5e040 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64   struct Lookasid
5e050 65 53 6c 6f 74 20 4c 6f 6f 6b 61 73 69 64 65 53  eSlot LookasideS
5e060 6c 6f 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  lot;.typedef str
5e070 75 63 74 20 4d 6f 64 75 6c 65 20 4d 6f 64 75 6c  uct Module Modul
5e080 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
5e090 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 4e 61  t NameContext Na
5e0a0 6d 65 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64  meContext;.typed
5e0b0 65 66 20 73 74 72 75 63 74 20 50 61 72 73 65 20  ef struct Parse 
5e0c0 50 61 72 73 65 3b 0a 74 79 70 65 64 65 66 20 73  Parse;.typedef s
5e0d0 74 72 75 63 74 20 52 6f 77 53 65 74 20 52 6f 77  truct RowSet Row
5e0e0 53 65 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  Set;.typedef str
5e0f0 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20 53 61  uct Savepoint Sa
5e100 76 65 70 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66  vepoint;.typedef
5e110 20 73 74 72 75 63 74 20 53 65 6c 65 63 74 20 53   struct Select S
5e120 65 6c 65 63 74 3b 0a 74 79 70 65 64 65 66 20 73  elect;.typedef s
5e130 74 72 75 63 74 20 53 65 6c 65 63 74 44 65 73 74  truct SelectDest
5e140 20 53 65 6c 65 63 74 44 65 73 74 3b 0a 74 79 70   SelectDest;.typ
5e150 65 64 65 66 20 73 74 72 75 63 74 20 53 72 63 4c  edef struct SrcL
5e160 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 79 70  ist SrcList;.typ
5e170 65 64 65 66 20 73 74 72 75 63 74 20 53 74 72 41  edef struct StrA
5e180 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b 0a 74  ccum StrAccum;.t
5e190 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61  ypedef struct Ta
5e1a0 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 65 64  ble Table;.typed
5e1b0 65 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 4c  ef struct TableL
5e1c0 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b 0a 74  ock TableLock;.t
5e1d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 6f  ypedef struct To
5e1e0 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 65 64  ken Token;.typed
5e1f0 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65  ef struct Trigge
5e200 72 20 54 72 69 67 67 65 72 3b 0a 74 79 70 65 64  r Trigger;.typed
5e210 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65  ef struct Trigge
5e220 72 50 72 67 20 54 72 69 67 67 65 72 50 72 67 3b  rPrg TriggerPrg;
5e230 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
5e240 54 72 69 67 67 65 72 53 74 65 70 20 54 72 69 67  TriggerStep Trig
5e250 67 65 72 53 74 65 70 3b 0a 74 79 70 65 64 65 66  gerStep;.typedef
5e260 20 73 74 72 75 63 74 20 55 6e 70 61 63 6b 65 64   struct Unpacked
5e270 52 65 63 6f 72 64 20 55 6e 70 61 63 6b 65 64 52  Record UnpackedR
5e280 65 63 6f 72 64 3b 0a 74 79 70 65 64 65 66 20 73  ecord;.typedef s
5e290 74 72 75 63 74 20 56 54 61 62 6c 65 20 56 54 61  truct VTable VTa
5e2a0 62 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ble;.typedef str
5e2b0 75 63 74 20 56 74 61 62 43 74 78 20 56 74 61 62  uct VtabCtx Vtab
5e2c0 43 74 78 3b 0a 74 79 70 65 64 65 66 20 73 74 72  Ctx;.typedef str
5e2d0 75 63 74 20 57 61 6c 6b 65 72 20 57 61 6c 6b 65  uct Walker Walke
5e2e0 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  r;.typedef struc
5e2f0 74 20 57 68 65 72 65 50 6c 61 6e 20 57 68 65 72  t WherePlan Wher
5e300 65 50 6c 61 6e 3b 0a 74 79 70 65 64 65 66 20 73  ePlan;.typedef s
5e310 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f 20  truct WhereInfo 
5e320 57 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70 65 64  WhereInfo;.typed
5e330 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4c  ef struct WhereL
5e340 65 76 65 6c 20 57 68 65 72 65 4c 65 76 65 6c 3b  evel WhereLevel;
5e350 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f  ../*.** Defer so
5e360 75 72 63 69 6e 67 20 76 64 62 65 2e 68 20 61 6e  urcing vdbe.h an
5e370 64 20 62 74 72 65 65 2e 68 20 75 6e 74 69 6c 20  d btree.h until 
5e380 61 66 74 65 72 20 74 68 65 20 22 75 38 22 20 61  after the "u8" a
5e390 6e 64 20 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64  nd .** "BusyHand
5e3a0 6c 65 72 22 20 74 79 70 65 64 65 66 73 2e 20 76  ler" typedefs. v
5e3b0 64 62 65 2e 68 20 61 6c 73 6f 20 72 65 71 75 69  dbe.h also requi
5e3c0 72 65 73 20 61 20 66 65 77 20 6f 66 20 74 68 65  res a few of the
5e3d0 20 6f 70 61 71 75 65 0a 2a 2a 20 70 6f 69 6e 74   opaque.** point
5e3e0 65 72 20 74 79 70 65 73 20 28 69 2e 65 2e 20 46  er types (i.e. F
5e3f0 75 6e 63 44 65 66 29 20 64 65 66 69 6e 65 64 20  uncDef) defined 
5e400 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a  above..*/./*****
5e410 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
5e420 65 20 62 74 72 65 65 2e 68 20 69 6e 20 74 68 65  e btree.h in the
5e430 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74   middle of sqlit
5e440 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
5e450 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
5e460 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
5e470 66 69 6c 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a  file btree.h ***
5e480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e4a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
5e4b0 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
5e4c0 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
5e4d0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
5e4e0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
5e4f0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
5e500 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
5e510 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
5e520 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
5e530 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
5e540 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
5e550 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
5e560 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
5e570 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
5e580 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
5e590 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
5e5a0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
5e5b0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
5e5c0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
5e5d0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
5e5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e620 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
5e630 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65  file defines the
5e640 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20   interface that 
5e650 74 68 65 20 73 71 6c 69 74 65 20 42 2d 54 72 65  the sqlite B-Tre
5e660 65 20 66 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73  e file.** subsys
5e670 74 65 6d 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e  tem.  See commen
5e680 74 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65  ts in the source
5e690 20 63 6f 64 65 20 66 6f 72 20 61 20 64 65 74 61   code for a deta
5e6a0 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e  iled description
5e6b0 0a 2a 2a 20 6f 66 20 77 68 61 74 20 65 61 63 68  .** of what each
5e6c0 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69   interface routi
5e6d0 6e 65 20 64 6f 65 73 2e 0a 2a 2f 0a 23 69 66 6e  ne does..*/.#ifn
5e6e0 64 65 66 20 5f 42 54 52 45 45 5f 48 5f 0a 23 64  def _BTREE_H_.#d
5e6f0 65 66 69 6e 65 20 5f 42 54 52 45 45 5f 48 5f 0a  efine _BTREE_H_.
5e700 0a 2f 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 64  ./* TODO: This d
5e710 65 66 69 6e 69 74 69 6f 6e 20 69 73 20 6a 75 73  efinition is jus
5e720 74 20 69 6e 63 6c 75 64 65 64 20 73 6f 20 6f 74  t included so ot
5e730 68 65 72 20 6d 6f 64 75 6c 65 73 20 63 6f 6d 70  her modules comp
5e740 69 6c 65 2e 20 49 74 0a 2a 2a 20 6e 65 65 64 73  ile. It.** needs
5e750 20 74 6f 20 62 65 20 72 65 76 69 73 69 74 65 64   to be revisited
5e760 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
5e770 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41  ITE_N_BTREE_META
5e780 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 64 65   10../*.** If de
5e790 66 69 6e 65 64 20 61 73 20 6e 6f 6e 2d 7a 65 72  fined as non-zer
5e7a0 6f 2c 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69  o, auto-vacuum i
5e7b0 73 20 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66  s enabled by def
5e7c0 61 75 6c 74 2e 20 4f 74 68 65 72 77 69 73 65 0a  ault. Otherwise.
5e7d0 2a 2a 20 69 74 20 6d 75 73 74 20 62 65 20 74 75  ** it must be tu
5e7e0 72 6e 65 64 20 6f 6e 20 66 6f 72 20 65 61 63 68  rned on for each
5e7f0 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20   database using 
5e800 22 50 52 41 47 4d 41 20 61 75 74 6f 5f 76 61 63  "PRAGMA auto_vac
5e810 75 75 6d 20 3d 20 31 22 2e 0a 2a 2f 0a 23 69 66  uum = 1"..*/.#if
5e820 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
5e830 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  ULT_AUTOVACUUM. 
5e840 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f   #define SQLITE_
5e850 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55  DEFAULT_AUTOVACU
5e860 55 4d 20 30 0a 23 65 6e 64 69 66 0a 0a 23 64 65  UM 0.#endif..#de
5e870 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56  fine BTREE_AUTOV
5e880 41 43 55 55 4d 5f 4e 4f 4e 45 20 30 20 20 20 20  ACUUM_NONE 0    
5e890 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f      /* Do not do
5e8a0 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a   auto-vacuum */.
5e8b0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55  #define BTREE_AU
5e8c0 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 20 31 20  TOVACUUM_FULL 1 
5e8d0 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 66 75 6c         /* Do ful
5e8e0 6c 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f  l auto-vacuum */
5e8f0 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41  .#define BTREE_A
5e900 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 20 32  UTOVACUUM_INCR 2
5e910 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65          /* Incre
5e920 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 2a 2f  mental vacuum */
5e930 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20  ../*.** Forward 
5e940 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20  declarations of 
5e950 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70  structure.*/.typ
5e960 65 64 65 66 20 73 74 72 75 63 74 20 42 74 72 65  edef struct Btre
5e970 65 20 42 74 72 65 65 3b 0a 74 79 70 65 64 65 66  e Btree;.typedef
5e980 20 73 74 72 75 63 74 20 42 74 43 75 72 73 6f 72   struct BtCursor
5e990 20 42 74 43 75 72 73 6f 72 3b 0a 74 79 70 65 64   BtCursor;.typed
5e9a0 65 66 20 73 74 72 75 63 74 20 42 74 53 68 61 72  ef struct BtShar
5e9b0 65 64 20 42 74 53 68 61 72 65 64 3b 0a 0a 0a 53  ed BtShared;...S
5e9c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5e9d0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70  t sqlite3BtreeOp
5e9e0 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  en(.  sqlite3_vf
5e9f0 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 2f  s *pVfs,       /
5ea00 2a 20 56 46 53 20 74 6f 20 75 73 65 20 77 69 74  * VFS to use wit
5ea10 68 20 74 68 69 73 20 62 2d 74 72 65 65 20 2a 2f  h this b-tree */
5ea20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
5ea30 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e  Filename,   /* N
5ea40 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ame of database 
5ea50 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a  file to open */.
5ea60 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
5ea70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73             /* As
5ea80 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73  sociated databas
5ea90 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
5eaa0 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65    Btree **ppBtre
5eab0 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  e,         /* Re
5eac0 74 75 72 6e 20 6f 70 65 6e 20 42 74 72 65 65 2a  turn open Btree*
5ead0 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66   here */.  int f
5eae0 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
5eaf0 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a      /* Flags */.
5eb00 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20    int vfsFlags  
5eb10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
5eb20 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75  ags passed throu
5eb30 67 68 20 74 6f 20 56 46 53 20 6f 70 65 6e 20 2a  gh to VFS open *
5eb40 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61  /.);../* The fla
5eb50 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  gs parameter to 
5eb60 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e  sqlite3BtreeOpen
5eb70 20 63 61 6e 20 62 65 20 74 68 65 20 62 69 74 77   can be the bitw
5eb80 69 73 65 20 6f 72 20 6f 66 20 74 68 65 0a 2a 2a  ise or of the.**
5eb90 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65   following value
5eba0 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20  s..**.** NOTE:  
5ebb0 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73  These values mus
5ebc0 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72  t match the corr
5ebd0 65 73 70 6f 6e 64 69 6e 67 20 50 41 47 45 52 5f  esponding PAGER_
5ebe0 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 70 61   values in.** pa
5ebf0 67 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ger.h..*/.#defin
5ec00 65 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55  e BTREE_OMIT_JOU
5ec10 52 4e 41 4c 20 20 31 20 20 2f 2a 20 44 6f 20 6e  RNAL  1  /* Do n
5ec20 6f 74 20 63 72 65 61 74 65 20 6f 72 20 75 73 65  ot create or use
5ec30 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72   a rollback jour
5ec40 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  nal */.#define B
5ec50 54 52 45 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20  TREE_MEMORY     
5ec60 20 20 20 32 20 20 2f 2a 20 54 68 69 73 20 69 73     2  /* This is
5ec70 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 44 42   an in-memory DB
5ec80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45   */.#define BTRE
5ec90 45 5f 53 49 4e 47 4c 45 20 20 20 20 20 20 20 20  E_SINGLE        
5eca0 34 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 63  4  /* The file c
5ecb0 6f 6e 74 61 69 6e 73 20 61 74 20 6d 6f 73 74 20  ontains at most 
5ecc0 31 20 62 2d 74 72 65 65 20 2a 2f 0a 23 64 65 66  1 b-tree */.#def
5ecd0 69 6e 65 20 42 54 52 45 45 5f 55 4e 4f 52 44 45  ine BTREE_UNORDE
5ece0 52 45 44 20 20 20 20 20 38 20 20 2f 2a 20 55 73  RED     8  /* Us
5ecf0 65 20 6f 66 20 61 20 68 61 73 68 20 69 6d 70 6c  e of a hash impl
5ed00 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 4f 4b  ementation is OK
5ed10 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
5ed20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
5ed30 74 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 2a  treeClose(Btree*
5ed40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5ed50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
5ed60 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 42  eeSetCacheSize(B
5ed70 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  tree*,int);.SQLI
5ed80 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5ed90 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61  qlite3BtreeSetSa
5eda0 66 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 2a  fetyLevel(Btree*
5edb0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  ,int,int,int);.S
5edc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5edd0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 79  t sqlite3BtreeSy
5ede0 6e 63 44 69 73 61 62 6c 65 64 28 42 74 72 65 65  ncDisabled(Btree
5edf0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
5ee00 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
5ee10 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42  reeSetPageSize(B
5ee20 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 50 61  tree *p, int nPa
5ee30 67 65 73 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73  gesize, int nRes
5ee40 65 72 76 65 2c 20 69 6e 74 20 65 46 69 78 29 3b  erve, int eFix);
5ee50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5ee60 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
5ee70 47 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65  GetPageSize(Btre
5ee80 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
5ee90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
5eea0 74 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74  treeMaxPageCount
5eeb0 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51  (Btree*,int);.SQ
5eec0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32  LITE_PRIVATE u32
5eed0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73   sqlite3BtreeLas
5eee0 74 50 61 67 65 28 42 74 72 65 65 2a 29 3b 0a 53  tPage(Btree*);.S
5eef0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5ef00 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  t sqlite3BtreeSe
5ef10 63 75 72 65 44 65 6c 65 74 65 28 42 74 72 65 65  cureDelete(Btree
5ef20 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
5ef30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5ef40 65 33 42 74 72 65 65 47 65 74 52 65 73 65 72 76  e3BtreeGetReserv
5ef50 65 28 42 74 72 65 65 2a 29 3b 0a 23 69 66 20 64  e(Btree*);.#if d
5ef60 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41  efined(SQLITE_HA
5ef70 53 5f 43 4f 44 45 43 29 20 7c 7c 20 64 65 66 69  S_CODEC) || defi
5ef80 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
5ef90 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
5efa0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
5efb0 65 47 65 74 52 65 73 65 72 76 65 4e 6f 4d 75 74  eGetReserveNoMut
5efc0 65 78 28 42 74 72 65 65 20 2a 70 29 3b 0a 23 65  ex(Btree *p);.#e
5efd0 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
5efe0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
5eff0 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 75  treeSetAutoVacuu
5f000 6d 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 29 3b  m(Btree *, int);
5f010 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5f020 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
5f030 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74  GetAutoVacuum(Bt
5f040 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ree *);.SQLITE_P
5f050 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5f060 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e  e3BtreeBeginTran
5f070 73 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53  s(Btree*,int);.S
5f080 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5f090 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
5f0a0 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72  mmitPhaseOne(Btr
5f0b0 65 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ee*, const char 
5f0c0 2a 7a 4d 61 73 74 65 72 29 3b 0a 53 51 4c 49 54  *zMaster);.SQLIT
5f0d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5f0e0 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
5f0f0 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 2a 2c  PhaseTwo(Btree*,
5f100 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
5f110 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5f120 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72  3BtreeCommit(Btr
5f130 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
5f140 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5f150 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74  BtreeRollback(Bt
5f160 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ree*,int);.SQLIT
5f170 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5f180 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 53  lite3BtreeBeginS
5f190 74 6d 74 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b  tmt(Btree*,int);
5f1a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5f1b0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
5f1c0 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65  CreateTable(Btre
5f1d0 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 20 66 6c  e*, int*, int fl
5f1e0 61 67 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ags);.SQLITE_PRI
5f1f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5f200 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 42  BtreeIsInTrans(B
5f210 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tree*);.SQLITE_P
5f220 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5f230 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54  e3BtreeIsInReadT
5f240 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53 51  rans(Btree*);.SQ
5f250 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5f260 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
5f270 6e 42 61 63 6b 75 70 28 42 74 72 65 65 2a 29 3b  nBackup(Btree*);
5f280 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5f290 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72  void *sqlite3Btr
5f2a0 65 65 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a  eeSchema(Btree *
5f2b0 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76  , int, void(*)(v
5f2c0 6f 69 64 20 2a 29 29 3b 0a 53 51 4c 49 54 45 5f  oid *));.SQLITE_
5f2d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
5f2e0 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f  te3BtreeSchemaLo
5f2f0 63 6b 65 64 28 42 74 72 65 65 20 2a 70 42 74 72  cked(Btree *pBtr
5f300 65 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ee);.SQLITE_PRIV
5f310 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
5f320 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74  treeLockTable(Bt
5f330 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74  ree *pBtree, int
5f340 20 69 54 61 62 2c 20 75 38 20 69 73 57 72 69 74   iTab, u8 isWrit
5f350 65 4c 6f 63 6b 29 3b 0a 53 51 4c 49 54 45 5f 50  eLock);.SQLITE_P
5f360 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5f370 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74  e3BtreeSavepoint
5f380 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 69  (Btree *, int, i
5f390 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  nt);..SQLITE_PRI
5f3a0 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
5f3b0 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74  *sqlite3BtreeGet
5f3c0 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 20 2a  Filename(Btree *
5f3d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5f3e0 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  E const char *sq
5f3f0 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75  lite3BtreeGetJou
5f400 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a  rnalname(Btree *
5f410 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5f420 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
5f430 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65  eeCopyFile(Btree
5f440 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 0a 53   *, Btree *);..S
5f450 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5f460 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e  t sqlite3BtreeIn
5f470 63 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a  crVacuum(Btree *
5f480 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73  );../* The flags
5f490 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
5f4a0 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65  lite3BtreeCreate
5f4b0 54 61 62 6c 65 20 63 61 6e 20 62 65 20 74 68 65  Table can be the
5f4c0 20 62 69 74 77 69 73 65 20 4f 52 0a 2a 2a 20 6f   bitwise OR.** o
5f4d0 66 20 74 68 65 20 66 6c 61 67 73 20 73 68 6f 77  f the flags show
5f4e0 6e 20 62 65 6c 6f 77 2e 0a 2a 2a 0a 2a 2a 20 45  n below..**.** E
5f4f0 76 65 72 79 20 53 51 4c 69 74 65 20 74 61 62 6c  very SQLite tabl
5f500 65 20 6d 75 73 74 20 68 61 76 65 20 65 69 74 68  e must have eith
5f510 65 72 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 20  er BTREE_INTKEY 
5f520 6f 72 20 42 54 52 45 45 5f 42 4c 4f 42 4b 45 59  or BTREE_BLOBKEY
5f530 20 73 65 74 2e 0a 2a 2a 20 57 69 74 68 20 42 54   set..** With BT
5f540 52 45 45 5f 49 4e 54 4b 45 59 2c 20 74 68 65 20  REE_INTKEY, the 
5f550 74 61 62 6c 65 20 6b 65 79 20 69 73 20 61 20 36  table key is a 6
5f560 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 61 6e  4-bit integer an
5f570 64 20 61 72 62 69 74 72 61 72 79 20 64 61 74 61  d arbitrary data
5f580 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 69 6e  .** is stored in
5f590 20 74 68 65 20 6c 65 61 76 65 73 2e 20 20 28 42   the leaves.  (B
5f5a0 54 52 45 45 5f 49 4e 54 4b 45 59 20 69 73 20 75  TREE_INTKEY is u
5f5b0 73 65 64 20 66 6f 72 20 53 51 4c 20 74 61 62 6c  sed for SQL tabl
5f5c0 65 73 2e 29 20 20 57 69 74 68 0a 2a 2a 20 42 54  es.)  With.** BT
5f5d0 52 45 45 5f 42 4c 4f 42 4b 45 59 2c 20 74 68 65  REE_BLOBKEY, the
5f5e0 20 6b 65 79 20 69 73 20 61 6e 20 61 72 62 69 74   key is an arbit
5f5f0 72 61 72 79 20 42 4c 4f 42 20 61 6e 64 20 6e 6f  rary BLOB and no
5f600 20 63 6f 6e 74 65 6e 74 20 69 73 20 73 74 6f 72   content is stor
5f610 65 64 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20 2d  ed.** anywhere -
5f620 20 74 68 65 20 6b 65 79 20 69 73 20 74 68 65 20   the key is the 
5f630 63 6f 6e 74 65 6e 74 2e 20 20 28 42 54 52 45 45  content.  (BTREE
5f640 5f 42 4c 4f 42 4b 45 59 20 69 73 20 75 73 65 64  _BLOBKEY is used
5f650 20 66 6f 72 20 53 51 4c 0a 2a 2a 20 69 6e 64 69   for SQL.** indi
5f660 63 65 73 2e 29 0a 2a 2f 0a 23 64 65 66 69 6e 65  ces.).*/.#define
5f670 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 20 20 20   BTREE_INTKEY   
5f680 20 20 31 20 20 20 20 2f 2a 20 54 61 62 6c 65 20    1    /* Table 
5f690 68 61 73 20 6f 6e 6c 79 20 36 34 2d 62 69 74 20  has only 64-bit 
5f6a0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 6b  signed integer k
5f6b0 65 79 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  eys */.#define B
5f6c0 54 52 45 45 5f 42 4c 4f 42 4b 45 59 20 20 20 20  TREE_BLOBKEY    
5f6d0 32 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61  2    /* Table ha
5f6e0 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f  s keys only - no
5f6f0 20 64 61 74 61 20 2a 2f 0a 0a 53 51 4c 49 54 45   data */..SQLITE
5f700 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
5f710 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62  ite3BtreeDropTab
5f720 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20  le(Btree*, int, 
5f730 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
5f740 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5f750 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65  3BtreeClearTable
5f760 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e  (Btree*, int, in
5f770 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
5f780 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5f790 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73  BtreeTripAllCurs
5f7a0 6f 72 73 28 42 74 72 65 65 2a 2c 20 69 6e 74 29  ors(Btree*, int)
5f7b0 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
5f7c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
5f7d0 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65  reeGetMeta(Btree
5f7e0 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 69 64   *pBtree, int id
5f7f0 78 2c 20 75 33 32 20 2a 70 56 61 6c 75 65 29 3b  x, u32 *pValue);
5f800 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5f810 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
5f820 55 70 64 61 74 65 4d 65 74 61 28 42 74 72 65 65  UpdateMeta(Btree
5f830 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20  *, int idx, u32 
5f840 76 61 6c 75 65 29 3b 0a 0a 53 51 4c 49 54 45 5f  value);..SQLITE_
5f850 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
5f860 74 65 33 42 74 72 65 65 4e 65 77 44 62 28 42 74  te3BtreeNewDb(Bt
5f870 72 65 65 20 2a 70 29 3b 0a 0a 2f 2a 0a 2a 2a 20  ree *p);../*.** 
5f880 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  The second param
5f890 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42  eter to sqlite3B
5f8a0 74 72 65 65 47 65 74 4d 65 74 61 20 6f 72 20 73  treeGetMeta or s
5f8b0 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74  qlite3BtreeUpdat
5f8c0 65 4d 65 74 61 0a 2a 2a 20 73 68 6f 75 6c 64 20  eMeta.** should 
5f8d0 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  be one of the fo
5f8e0 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 20  llowing values. 
5f8f0 54 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75  The integer valu
5f900 65 73 20 61 72 65 20 61 73 73 69 67 6e 65 64 20  es are assigned 
5f910 0a 2a 2a 20 74 6f 20 63 6f 6e 73 74 61 6e 74 73  .** to constants
5f920 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f 66 66   so that the off
5f930 73 65 74 20 6f 66 20 74 68 65 20 63 6f 72 72 65  set of the corre
5f940 73 70 6f 6e 64 69 6e 67 20 66 69 65 6c 64 20 69  sponding field i
5f950 6e 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 64  n an.** SQLite d
5f960 61 74 61 62 61 73 65 20 68 65 61 64 65 72 20 6d  atabase header m
5f970 61 79 20 62 65 20 66 6f 75 6e 64 20 75 73 69 6e  ay be found usin
5f980 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  g the following 
5f990 66 6f 72 6d 75 6c 61 3a 0a 2a 2a 0a 2a 2a 20 20  formula:.**.**  
5f9a0 20 6f 66 66 73 65 74 20 3d 20 33 36 20 2b 20 28   offset = 36 + (
5f9b0 69 64 78 20 2a 20 34 29 0a 2a 2a 0a 2a 2a 20 46  idx * 4).**.** F
5f9c0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20  or example, the 
5f9d0 66 72 65 65 2d 70 61 67 65 2d 63 6f 75 6e 74 20  free-page-count 
5f9e0 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 65 64  field is located
5f9f0 20 61 74 20 62 79 74 65 20 6f 66 66 73 65 74 20   at byte offset 
5fa00 33 36 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 74  36 of.** the dat
5fa10 61 62 61 73 65 20 66 69 6c 65 20 68 65 61 64 65  abase file heade
5fa20 72 2e 20 54 68 65 20 69 6e 63 72 2d 76 61 63 75  r. The incr-vacu
5fa30 75 6d 2d 66 6c 61 67 20 66 69 65 6c 64 20 69 73  um-flag field is
5fa40 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 62   located at.** b
5fa50 79 74 65 20 6f 66 66 73 65 74 20 36 34 20 28 3d  yte offset 64 (=
5fa60 3d 20 33 36 2b 34 2a 37 29 2e 0a 2a 2f 0a 23 64  = 36+4*7)..*/.#d
5fa70 65 66 69 6e 65 20 42 54 52 45 45 5f 46 52 45 45  efine BTREE_FREE
5fa80 5f 50 41 47 45 5f 43 4f 55 4e 54 20 20 20 20 20  _PAGE_COUNT     
5fa90 30 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  0.#define BTREE_
5faa0 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20 20  SCHEMA_VERSION  
5fab0 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 42 54      1.#define BT
5fac0 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20  REE_FILE_FORMAT 
5fad0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e          2.#defin
5fae0 65 20 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f  e BTREE_DEFAULT_
5faf0 43 41 43 48 45 5f 53 49 5a 45 20 20 33 0a 23 64  CACHE_SIZE  3.#d
5fb00 65 66 69 6e 65 20 42 54 52 45 45 5f 4c 41 52 47  efine BTREE_LARG
5fb10 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 20 20  EST_ROOT_PAGE   
5fb20 34 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  4.#define BTREE_
5fb30 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 20 20 20  TEXT_ENCODING   
5fb40 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 42 54      5.#define BT
5fb50 52 45 45 5f 55 53 45 52 5f 56 45 52 53 49 4f 4e  REE_USER_VERSION
5fb60 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e          6.#defin
5fb70 65 20 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 43  e BTREE_INCR_VAC
5fb80 55 55 4d 20 20 20 20 20 20 20 20 20 37 0a 0a 2f  UUM         7../
5fb90 2a 0a 2a 2a 20 56 61 6c 75 65 73 20 74 68 61 74  *.** Values that
5fba0 20 6d 61 79 20 62 65 20 4f 52 27 64 20 74 6f 67   may be OR'd tog
5fbb0 65 74 68 65 72 20 74 6f 20 66 6f 72 6d 20 74 68  ether to form th
5fbc0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
5fbd0 74 20 6f 66 20 61 6e 0a 2a 2a 20 73 71 6c 69 74  t of an.** sqlit
5fbe0 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 69 6e  e3BtreeCursorHin
5fbf0 74 73 28 29 20 63 61 6c 6c 2e 0a 2a 2f 0a 23 64  ts() call..*/.#d
5fc00 65 66 69 6e 65 20 42 54 52 45 45 5f 42 55 4c 4b  efine BTREE_BULK
5fc10 4c 4f 41 44 20 30 78 30 30 30 30 30 30 30 31 0a  LOAD 0x00000001.
5fc20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5fc30 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
5fc40 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 2a  Cursor(.  Btree*
5fc50 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
5fc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5fc70 2a 20 42 54 72 65 65 20 63 6f 6e 74 61 69 6e 69  * BTree containi
5fc80 6e 67 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e  ng table to open
5fc90 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
5fca0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
5fcb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
5fcc0 64 65 78 20 6f 66 20 72 6f 6f 74 20 70 61 67 65  dex of root page
5fcd0 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67   */.  int wrFlag
5fce0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
5fcf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20             /* 1 
5fd00 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 20 30 20  for writing.  0 
5fd10 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f  for read-only */
5fd20 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66  .  struct KeyInf
5fd30 6f 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  o*,             
5fd40 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
5fd50 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 6f 6d   argument to com
5fd60 70 61 72 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  pare function */
5fd70 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
5fd80 72 73 6f 72 20 20 20 20 20 20 20 20 20 20 20 20  rsor            
5fd90 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
5fda0 20 74 6f 20 77 72 69 74 65 20 63 75 72 73 6f 72   to write cursor
5fdb0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 3b   structure */.);
5fdc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5fdd0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
5fde0 43 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 29  CursorSize(void)
5fdf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5fe00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
5fe10 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 42 74 43  eeCursorZero(BtC
5fe20 75 72 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 45  ursor*);..SQLITE
5fe30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
5fe40 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75  ite3BtreeCloseCu
5fe50 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b  rsor(BtCursor*);
5fe60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5fe70 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
5fe80 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a  MovetoUnpacked(.
5fe90 20 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55    BtCursor*,.  U
5fea0 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70  npackedRecord *p
5feb0 55 6e 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e 74  UnKey,.  i64 int
5fec0 4b 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c  Key,.  int bias,
5fed0 0a 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a  .  int *pRes.);.
5fee0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5fef0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
5ff00 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74  ursorHasMoved(Bt
5ff10 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a  Cursor*, int*);.
5ff20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5ff30 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
5ff40 65 6c 65 74 65 28 42 74 43 75 72 73 6f 72 2a 29  elete(BtCursor*)
5ff50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5ff60 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
5ff70 65 49 6e 73 65 72 74 28 42 74 43 75 72 73 6f 72  eInsert(BtCursor
5ff80 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  *, const void *p
5ff90 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20  Key, i64 nKey,. 
5ffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ffb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ffc0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61   const void *pDa
5ffd0 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20  ta, int nData,. 
5ffe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5fff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60000 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20   int nZero, int 
60010 62 69 61 73 2c 20 69 6e 74 20 73 65 65 6b 52 65  bias, int seekRe
60020 73 75 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  sult);.SQLITE_PR
60030 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
60040 33 42 74 72 65 65 46 69 72 73 74 28 42 74 43 75  3BtreeFirst(BtCu
60050 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73  rsor*, int *pRes
60060 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
60070 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
60080 65 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 2a  eeLast(BtCursor*
60090 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51  , int *pRes);.SQ
600a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
600b0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78   sqlite3BtreeNex
600c0 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74  t(BtCursor*, int
600d0 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f   *pRes);.SQLITE_
600e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
600f0 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75  te3BtreeEof(BtCu
60100 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  rsor*);.SQLITE_P
60110 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
60120 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28  e3BtreePrevious(
60130 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a  BtCursor*, int *
60140 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pRes);.SQLITE_PR
60150 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
60160 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74  3BtreeKeySize(Bt
60170 43 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70 53  Cursor*, i64 *pS
60180 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ize);.SQLITE_PRI
60190 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
601a0 42 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f  BtreeKey(BtCurso
601b0 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20  r*, u32 offset, 
601c0 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b  u32 amt, void*);
601d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
601e0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
601f0 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68  te3BtreeKeyFetch
60200 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20  (BtCursor*, int 
60210 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *pAmt);.SQLITE_P
60220 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69  RIVATE const voi
60230 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44  d *sqlite3BtreeD
60240 61 74 61 46 65 74 63 68 28 42 74 43 75 72 73 6f  ataFetch(BtCurso
60250 72 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a  r*, int *pAmt);.
60260 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
60270 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
60280 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72  ataSize(BtCursor
60290 2a 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a  *, u32 *pSize);.
602a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
602b0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
602c0 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75  ata(BtCursor*, u
602d0 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61  32 offset, u32 a
602e0 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49  mt, void*);.SQLI
602f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
60300 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43  sqlite3BtreeSetC
60310 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72  achedRowid(BtCur
60320 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e  sor*, sqlite3_in
60330 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  t64);.SQLITE_PRI
60340 56 41 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74  VATE sqlite3_int
60350 36 34 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  64 sqlite3BtreeG
60360 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 42 74  etCachedRowid(Bt
60370 43 75 72 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54  Cursor*);..SQLIT
60380 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
60390 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65  sqlite3BtreeInte
603a0 67 72 69 74 79 43 68 65 63 6b 28 42 74 72 65 65  grityCheck(Btree
603b0 2a 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 69  *, int *aRoot, i
603c0 6e 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 69  nt nRoot, int, i
603d0 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
603e0 56 41 54 45 20 73 74 72 75 63 74 20 50 61 67 65  VATE struct Page
603f0 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50  r *sqlite3BtreeP
60400 61 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a 53  ager(Btree*);..S
60410 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
60420 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75  t sqlite3BtreePu
60430 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c  tData(BtCursor*,
60440 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32   u32 offset, u32
60450 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51   amt, void*);.SQ
60460 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
60470 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 61  d sqlite3BtreeCa
60480 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75  cheOverflow(BtCu
60490 72 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  rsor *);.SQLITE_
604a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
604b0 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75  ite3BtreeClearCu
604c0 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 29  rsor(BtCursor *)
604d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
604e0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
604f0 65 53 65 74 56 65 72 73 69 6f 6e 28 42 74 72 65  eSetVersion(Btre
60500 65 20 2a 70 42 74 2c 20 69 6e 74 20 69 56 65 72  e *pBt, int iVer
60510 73 69 6f 6e 29 3b 0a 53 51 4c 49 54 45 5f 50 52  sion);.SQLITE_PR
60520 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
60530 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 69 6e  e3BtreeCursorHin
60540 74 73 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 75  ts(BtCursor *, u
60550 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d 61 73 6b  nsigned int mask
60560 29 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  );..#ifndef NDEB
60570 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
60580 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
60590 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28  eeCursorIsValid(
605a0 42 74 43 75 72 73 6f 72 2a 29 3b 0a 23 65 6e 64  BtCursor*);.#end
605b0 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
605c0 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55  TE_OMIT_BTREECOU
605d0 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  NT.SQLITE_PRIVAT
605e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
605f0 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f 72  eeCount(BtCursor
60600 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 23 65 6e 64   *, i64 *);.#end
60610 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
60620 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  E_TEST.SQLITE_PR
60630 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
60640 33 42 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f  3BtreeCursorInfo
60650 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a  (BtCursor*, int*
60660 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
60670 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
60680 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69  te3BtreeCursorLi
60690 73 74 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64  st(Btree*);.#end
606a0 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
606b0 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 53 51 4c 49  TE_OMIT_WAL.SQLI
606c0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
606d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 68 65   sqlite3BtreeChe
606e0 63 6b 70 6f 69 6e 74 28 42 74 72 65 65 2a 2c 20  ckpoint(Btree*, 
606f0 69 6e 74 2c 20 69 6e 74 20 2a 2c 20 69 6e 74 20  int, int *, int 
60700 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  *);.#endif../*.*
60710 2a 20 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20  * If we are not 
60720 75 73 69 6e 67 20 73 68 61 72 65 64 20 63 61 63  using shared cac
60730 68 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69  he, then there i
60740 73 20 6e 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20  s no need to.** 
60750 75 73 65 20 6d 75 74 65 78 65 73 20 74 6f 20 61  use mutexes to a
60760 63 63 65 73 73 20 74 68 65 20 42 74 53 68 61 72  ccess the BtShar
60770 65 64 20 73 74 72 75 63 74 75 72 65 73 2e 20 20  ed structures.  
60780 53 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 45  So make the.** E
60790 6e 74 65 72 20 61 6e 64 20 4c 65 61 76 65 20 70  nter and Leave p
607a0 72 6f 63 65 64 75 72 65 73 20 6e 6f 2d 6f 70 73  rocedures no-ops
607b0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
607c0 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
607d0 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49  CACHE.SQLITE_PRI
607e0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
607f0 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 74  te3BtreeEnter(Bt
60800 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
60810 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
60820 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c  ite3BtreeEnterAl
60830 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c  l(sqlite3*);.#el
60840 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  se.# define sqli
60850 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 58 29  te3BtreeEnter(X)
60860 20 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74   .# define sqlit
60870 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
60880 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21  X).#endif..#if !
60890 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
608a0 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
608b0 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45  ) && SQLITE_THRE
608c0 41 44 53 41 46 45 0a 53 51 4c 49 54 45 5f 50 52  ADSAFE.SQLITE_PR
608d0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
608e0 74 65 33 42 74 72 65 65 53 68 61 72 61 62 6c 65  te3BtreeSharable
608f0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
60900 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
60910 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
60920 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  e(Btree*);.SQLIT
60930 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
60940 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
60950 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f  erCursor(BtCurso
60960 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
60970 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
60980 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73  e3BtreeLeaveCurs
60990 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53  or(BtCursor*);.S
609a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
609b0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
609c0 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65  eLeaveAll(sqlite
609d0 33 2a 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  3*);.#ifndef NDE
609e0 42 55 47 0a 20 20 2f 2a 20 54 68 65 73 65 20 72  BUG.  /* These r
609f0 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
60a00 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29   inside assert()
60a10 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79   statements only
60a20 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  . */.SQLITE_PRIV
60a30 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
60a40 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
60a50 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
60a60 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
60a70 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
60a80 41 6c 6c 4d 75 74 65 78 65 73 28 73 71 6c 69 74  AllMutexes(sqlit
60a90 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e3*);.SQLITE_PRI
60aa0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
60ab0 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c  e3SchemaMutexHel
60ac0 64 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 53  d(sqlite3*,int,S
60ad0 63 68 65 6d 61 2a 29 3b 0a 23 65 6e 64 69 66 0a  chema*);.#endif.
60ae0 23 65 6c 73 65 0a 0a 23 20 64 65 66 69 6e 65 20  #else..# define 
60af0 73 71 6c 69 74 65 33 42 74 72 65 65 53 68 61 72  sqlite3BtreeShar
60b00 61 62 6c 65 28 58 29 20 30 0a 23 20 64 65 66 69  able(X) 0.# defi
60b10 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  ne sqlite3BtreeL
60b20 65 61 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65  eave(X).# define
60b30 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
60b40 65 72 43 75 72 73 6f 72 28 58 29 0a 23 20 64 65  erCursor(X).# de
60b50 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65  fine sqlite3Btre
60b60 65 4c 65 61 76 65 43 75 72 73 6f 72 28 58 29 0a  eLeaveCursor(X).
60b70 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
60b80 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 58 29  BtreeLeaveAll(X)
60b90 0a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ..# define sqlit
60ba0 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
60bb0 78 28 58 29 20 31 0a 23 20 64 65 66 69 6e 65 20  x(X) 1.# define 
60bc0 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
60bd0 73 41 6c 6c 4d 75 74 65 78 65 73 28 58 29 20 31  sAllMutexes(X) 1
60be0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
60bf0 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64  3SchemaMutexHeld
60c00 28 58 2c 59 2c 5a 29 20 31 0a 23 65 6e 64 69 66  (X,Y,Z) 1.#endif
60c10 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42 54  ...#endif /* _BT
60c20 52 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  REE_H_ */../****
60c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
60c40 66 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a  f btree.h ******
60c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
60c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
60c90 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
60ca0 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74  eft off in sqlit
60cb0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
60cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
60cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
60ce0 64 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68 65  de vdbe.h in the
60cf0 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74   middle of sqlit
60d00 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
60d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
60d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
60d30 20 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a 2a   file vdbe.h ***
60d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
60d70 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
60d80 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
60d90 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
60da0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
60db0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
60dc0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
60dd0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
60de0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
60df0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
60e00 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
60e10 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
60e20 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
60e30 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
60e40 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
60e50 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
60e60 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
60e70 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
60e80 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
60e90 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
60ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
60ee0 2a 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c 65  *.** Header file
60ef0 20 66 6f 72 20 74 68 65 20 56 69 72 74 75 61 6c   for the Virtual
60f00 20 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e 65   DataBase Engine
60f10 20 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54 68   (VDBE).**.** Th
60f20 69 73 20 68 65 61 64 65 72 20 64 65 66 69 6e 65  is header define
60f30 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20  s the interface 
60f40 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 64  to the virtual d
60f50 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a 2a  atabase engine.*
60f60 2a 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65 20  * or VDBE.  The 
60f70 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73 20  VDBE implements 
60f80 61 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63 68  an abstract mach
60f90 69 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61 0a  ine that runs a.
60fa0 2a 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72 61  ** simple progra
60fb0 6d 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64 20  m to access and 
60fc0 6d 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65 72  modify the under
60fd0 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e 0a  lying database..
60fe0 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49  */.#ifndef _SQLI
60ff0 54 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66 69  TE_VDBE_H_.#defi
61000 6e 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f  ne _SQLITE_VDBE_
61010 48 5f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c  H_./* #include <
61020 73 74 64 69 6f 2e 68 3e 20 2a 2f 0a 0a 2f 2a 0a  stdio.h> */../*.
61030 2a 2a 20 41 20 73 69 6e 67 6c 65 20 56 44 42 45  ** A single VDBE
61040 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74   is an opaque st
61050 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 56  ructure named "V
61060 64 62 65 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74  dbe".  Only rout
61070 69 6e 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73  ines.** in the s
61080 6f 75 72 63 65 20 66 69 6c 65 20 73 71 6c 69 74  ource file sqlit
61090 65 56 64 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f  eVdbe.c are allo
610a0 77 65 64 20 74 6f 20 73 65 65 20 74 68 65 20 69  wed to see the i
610b0 6e 73 69 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69  nsides.** of thi
610c0 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  s structure..*/.
610d0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56  typedef struct V
610e0 64 62 65 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a  dbe Vdbe;../*.**
610f0 20 54 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68   The names of th
61100 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65  e following type
61110 73 20 64 65 63 6c 61 72 65 64 20 69 6e 20 76 64  s declared in vd
61120 62 65 49 6e 74 2e 68 20 61 72 65 20 72 65 71 75  beInt.h are requ
61130 69 72 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20  ired.** for the 
61140 56 64 62 65 4f 70 20 64 65 66 69 6e 69 74 69 6f  VdbeOp definitio
61150 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  n..*/.typedef st
61160 72 75 63 74 20 56 64 62 65 46 75 6e 63 20 56 64  ruct VdbeFunc Vd
61170 62 65 46 75 6e 63 3b 0a 74 79 70 65 64 65 66 20  beFunc;.typedef 
61180 73 74 72 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a  struct Mem Mem;.
61190 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
611a0 75 62 50 72 6f 67 72 61 6d 20 53 75 62 50 72 6f  ubProgram SubPro
611b0 67 72 61 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73  gram;../*.** A s
611c0 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f  ingle instructio
611d0 6e 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  n of the virtual
611e0 20 6d 61 63 68 69 6e 65 20 68 61 73 20 61 6e 20   machine has an 
611f0 6f 70 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61 73  opcode.** and as
61200 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20 6f   many as three o
61210 70 65 72 61 6e 64 73 2e 20 20 54 68 65 20 69 6e  perands.  The in
61220 73 74 72 75 63 74 69 6f 6e 20 69 73 20 72 65 63  struction is rec
61230 6f 72 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20 69  orded.** as an i
61240 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
61250 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
61260 72 65 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64  re:.*/.struct Vd
61270 62 65 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63 6f  beOp {.  u8 opco
61280 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  de;          /* 
61290 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 74  What operation t
612a0 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 73  o perform */.  s
612b0 69 67 6e 65 64 20 63 68 61 72 20 70 34 74 79 70  igned char p4typ
612c0 65 3b 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65  e; /* One of the
612d0 20 50 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e 74   P4_xxx constant
612e0 73 20 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75 38  s for p4 */.  u8
612f0 20 6f 70 66 6c 61 67 73 3b 20 20 20 20 20 20 20   opflags;       
61300 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 74 68 65    /* Mask of the
61310 20 4f 50 46 4c 47 5f 2a 20 66 6c 61 67 73 20 69   OPFLG_* flags i
61320 6e 20 6f 70 63 6f 64 65 73 2e 68 20 2a 2f 0a 20  n opcodes.h */. 
61330 20 75 38 20 70 35 3b 20 20 20 20 20 20 20 20 20   u8 p5;         
61340 20 20 20 20 20 2f 2a 20 46 69 66 74 68 20 70 61       /* Fifth pa
61350 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 75 6e  rameter is an un
61360 73 69 67 6e 65 64 20 63 68 61 72 61 63 74 65 72  signed character
61370 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20   */.  int p1;   
61380 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
61390 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  st operand */.  
613a0 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 20  int p2;         
613b0 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61      /* Second pa
613c0 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 74  rameter (often t
613d0 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74  he jump destinat
613e0 69 6f 6e 29 20 2a 2f 0a 20 20 69 6e 74 20 70 33  ion) */.  int p3
613f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
61400 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d   The third param
61410 65 74 65 72 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20  eter */.  union 
61420 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  {             /*
61430 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65   fourth paramete
61440 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 20  r */.    int i; 
61450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61460 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65  /* Integer value
61470 20 69 66 20 70 34 74 79 70 65 3d 3d 50 34 5f 49   if p4type==P4_I
61480 4e 54 33 32 20 2a 2f 0a 20 20 20 20 76 6f 69 64  NT32 */.    void
61490 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   *p;            
614a0 20 20 20 2f 2a 20 47 65 6e 65 72 69 63 20 70 6f     /* Generic po
614b0 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 63 68 61  inter */.    cha
614c0 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20  r *z;           
614d0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
614e0 6f 20 64 61 74 61 20 66 6f 72 20 73 74 72 69 6e  o data for strin
614f0 67 20 28 63 68 61 72 20 61 72 72 61 79 29 20 74  g (char array) t
61500 79 70 65 73 20 2a 2f 0a 20 20 20 20 69 36 34 20  ypes */.    i64 
61510 2a 70 49 36 34 3b 20 20 20 20 20 20 20 20 20 20  *pI64;          
61520 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20     /* Used when 
61530 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e 54  p4type is P4_INT
61540 36 34 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65  64 */.    double
61550 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20 20 20   *pReal;        
61560 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34   /* Used when p4
61570 74 79 70 65 20 69 73 20 50 34 5f 52 45 41 4c 20  type is P4_REAL 
61580 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a  */.    FuncDef *
61590 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 2f 2a  pFunc;        /*
615a0 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70   Used when p4typ
615b0 65 20 69 73 20 50 34 5f 46 55 4e 43 44 45 46 20  e is P4_FUNCDEF 
615c0 2a 2f 0a 20 20 20 20 56 64 62 65 46 75 6e 63 20  */.    VdbeFunc 
615d0 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 20 2f 2a  *pVdbeFunc;   /*
615e0 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70   Used when p4typ
615f0 65 20 69 73 20 50 34 5f 56 44 42 45 46 55 4e 43  e is P4_VDBEFUNC
61600 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20   */.    CollSeq 
61610 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 2f  *pColl;        /
61620 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79  * Used when p4ty
61630 70 65 20 69 73 20 50 34 5f 43 4f 4c 4c 53 45 51  pe is P4_COLLSEQ
61640 20 2a 2f 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65   */.    Mem *pMe
61650 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  m;             /
61660 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79  * Used when p4ty
61670 70 65 20 69 73 20 50 34 5f 4d 45 4d 20 2a 2f 0a  pe is P4_MEM */.
61680 20 20 20 20 56 54 61 62 6c 65 20 2a 70 56 74 61      VTable *pVta
61690 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73  b;         /* Us
616a0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
616b0 73 20 50 34 5f 56 54 41 42 20 2a 2f 0a 20 20 20  s P4_VTAB */.   
616c0 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
616d0 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20  fo;     /* Used 
616e0 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50  when p4type is P
616f0 34 5f 4b 45 59 49 4e 46 4f 20 2a 2f 0a 20 20 20  4_KEYINFO */.   
61700 20 69 6e 74 20 2a 61 69 3b 20 20 20 20 20 20 20   int *ai;       
61710 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
61720 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50  when p4type is P
61730 34 5f 49 4e 54 41 52 52 41 59 20 2a 2f 0a 20 20  4_INTARRAY */.  
61740 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 50    SubProgram *pP
61750 72 6f 67 72 61 6d 3b 20 20 2f 2a 20 55 73 65 64  rogram;  /* Used
61760 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20   when p4type is 
61770 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 2a 2f  P4_SUBPROGRAM */
61780 0a 20 20 20 20 69 6e 74 20 28 2a 78 41 64 76 61  .    int (*xAdva
61790 6e 63 65 29 28 42 74 43 75 72 73 6f 72 20 2a 2c  nce)(BtCursor *,
617a0 20 69 6e 74 20 2a 29 3b 0a 20 20 7d 20 70 34 3b   int *);.  } p4;
617b0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
617c0 45 42 55 47 0a 20 20 63 68 61 72 20 2a 7a 43 6f  EBUG.  char *zCo
617d0 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20  mment;          
617e0 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f 20 69 6d  /* Comment to im
617f0 70 72 6f 76 65 20 72 65 61 64 61 62 69 6c 69 74  prove readabilit
61800 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64  y */.#endif.#ifd
61810 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a  ef VDBE_PROFILE.
61820 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20    int cnt;      
61830 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
61840 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68  mber of times th
61850 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77  is instruction w
61860 61 73 20 65 78 65 63 75 74 65 64 20 2a 2f 0a 20  as executed */. 
61870 20 75 36 34 20 63 79 63 6c 65 73 3b 20 20 20 20   u64 cycles;    
61880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74            /* Tot
61890 61 6c 20 74 69 6d 65 20 73 70 65 6e 74 20 65 78  al time spent ex
618a0 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e 73  ecuting this ins
618b0 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64  truction */.#end
618c0 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74  if.};.typedef st
618d0 72 75 63 74 20 56 64 62 65 4f 70 20 56 64 62 65  ruct VdbeOp Vdbe
618e0 4f 70 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 75  Op;.../*.** A su
618f0 62 2d 72 6f 75 74 69 6e 65 20 75 73 65 64 20 74  b-routine used t
61900 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 74 72  o implement a tr
61910 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a 2a  igger program..*
61920 2f 0a 73 74 72 75 63 74 20 53 75 62 50 72 6f 67  /.struct SubProg
61930 72 61 6d 20 7b 0a 20 20 56 64 62 65 4f 70 20 2a  ram {.  VdbeOp *
61940 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  aOp;            
61950 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f        /* Array o
61960 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 73 75  f opcodes for su
61970 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69  b-program */.  i
61980 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 20 20  nt nOp;         
61990 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
619a0 45 6c 65 6d 65 6e 74 73 20 69 6e 20 61 4f 70 5b  Elements in aOp[
619b0 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b  ] */.  int nMem;
619c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
619d0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
619e0 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 72  f memory cells r
619f0 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74  equired */.  int
61a00 20 6e 43 73 72 3b 20 20 20 20 20 20 20 20 20 20   nCsr;          
61a10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
61a20 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20  mber of cursors 
61a30 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e  required */.  in
61a40 74 20 6e 4f 6e 63 65 3b 20 20 20 20 20 20 20 20  t nOnce;        
61a50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
61a60 75 6d 62 65 72 20 6f 66 20 4f 50 5f 4f 6e 63 65  umber of OP_Once
61a70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f   instructions */
61a80 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e 3b 20  .  void *token; 
61a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61aa0 20 2f 2a 20 69 64 20 74 68 61 74 20 6d 61 79 20   /* id that may 
61ab0 62 65 20 75 73 65 64 20 74 6f 20 72 65 63 75 72  be used to recur
61ac0 73 69 76 65 20 74 72 69 67 67 65 72 73 20 2a 2f  sive triggers */
61ad0 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70  .  SubProgram *p
61ae0 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20  Next;           
61af0 20 2f 2a 20 4e 65 78 74 20 73 75 62 2d 70 72 6f   /* Next sub-pro
61b00 67 72 61 6d 20 61 6c 72 65 61 64 79 20 76 69 73  gram already vis
61b10 69 74 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ited */.};../*.*
61b20 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 65 72 73  * A smaller vers
61b30 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 20 75 73  ion of VdbeOp us
61b40 65 64 20 66 6f 72 20 74 68 65 20 56 64 62 65 41  ed for the VdbeA
61b50 64 64 4f 70 4c 69 73 74 28 29 20 66 75 6e 63 74  ddOpList() funct
61b60 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 69  ion because.** i
61b70 74 20 74 61 6b 65 73 20 75 70 20 6c 65 73 73 20  t takes up less 
61b80 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  space..*/.struct
61b90 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20 20   VdbeOpList {.  
61ba0 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 20  u8 opcode;      
61bb0 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 72      /* What oper
61bc0 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 6d  ation to perform
61bd0 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61   */.  signed cha
61be0 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 46 69 72  r p1;     /* Fir
61bf0 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  st operand */.  
61c00 73 69 67 6e 65 64 20 63 68 61 72 20 70 32 3b 20  signed char p2; 
61c10 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61      /* Second pa
61c20 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 74  rameter (often t
61c30 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74  he jump destinat
61c40 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 6e 65 64  ion) */.  signed
61c50 20 63 68 61 72 20 70 33 3b 20 20 20 20 20 2f 2a   char p3;     /*
61c60 20 54 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   Third parameter
61c70 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73   */.};.typedef s
61c80 74 72 75 63 74 20 56 64 62 65 4f 70 4c 69 73 74  truct VdbeOpList
61c90 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f 2a   VdbeOpList;../*
61ca0 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
61cb0 65 73 20 6f 66 20 56 64 62 65 4f 70 2e 70 34 74  es of VdbeOp.p4t
61cc0 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ype.*/.#define P
61cd0 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 30 20 20  4_NOTUSED    0  
61ce0 20 2f 2a 20 54 68 65 20 50 34 20 70 61 72 61 6d   /* The P4 param
61cf0 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64  eter is not used
61d00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 44   */.#define P4_D
61d10 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 20 2f 2a  YNAMIC  (-1)  /*
61d20 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   Pointer to a st
61d30 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72  ring obtained fr
61d40 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  om sqliteMalloc(
61d50 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  ) */.#define P4_
61d60 53 54 41 54 49 43 20 20 20 28 2d 32 29 20 20 2f  STATIC   (-2)  /
61d70 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  * Pointer to a s
61d80 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a  tatic string */.
61d90 23 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c 53  #define P4_COLLS
61da0 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 50 34 20  EQ  (-4)  /* P4 
61db0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
61dc0 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74  a CollSeq struct
61dd0 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ure */.#define P
61de0 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35 29 20  4_FUNCDEF  (-5) 
61df0 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e   /* P4 is a poin
61e00 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44 65 66  ter to a FuncDef
61e10 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64   structure */.#d
61e20 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f  efine P4_KEYINFO
61e30 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20 69 73    (-6)  /* P4 is
61e40 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
61e50 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
61e60 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  e */.#define P4_
61e70 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20 20 2f  VDBEFUNC (-7)  /
61e80 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
61e90 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e 63 20  r to a VdbeFunc 
61ea0 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65  structure */.#de
61eb0 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 20 20 20  fine P4_MEM     
61ec0 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 69 73 20   (-8)  /* P4 is 
61ed0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d  a pointer to a M
61ee0 65 6d 2a 20 20 20 20 73 74 72 75 63 74 75 72 65  em*    structure
61ef0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 54   */.#define P4_T
61f00 52 41 4e 53 49 45 4e 54 20 20 30 20 20 20 2f 2a  RANSIENT  0   /*
61f10 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
61f20 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20   to a transient 
61f30 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e  string */.#defin
61f40 65 20 50 34 5f 56 54 41 42 20 20 20 20 20 28 2d  e P4_VTAB     (-
61f50 31 30 29 20 2f 2a 20 50 34 20 69 73 20 61 20 70  10) /* P4 is a p
61f60 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c  ointer to an sql
61f70 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74  ite3_vtab struct
61f80 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ure */.#define P
61f90 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d 31 31 29  4_MPRINTF  (-11)
61fa0 20 2f 2a 20 50 34 20 69 73 20 61 20 73 74 72 69   /* P4 is a stri
61fb0 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ng obtained from
61fc0 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
61fd0 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  () */.#define P4
61fe0 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 32 29 20  _REAL     (-12) 
61ff0 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62 69  /* P4 is a 64-bi
62000 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  t floating point
62010 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e   value */.#defin
62020 65 20 50 34 5f 49 4e 54 36 34 20 20 20 20 28 2d  e P4_INT64    (-
62030 31 33 29 20 2f 2a 20 50 34 20 69 73 20 61 20 36  13) /* P4 is a 6
62040 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  4-bit signed int
62050 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20  eger */.#define 
62060 50 34 5f 49 4e 54 33 32 20 20 20 20 28 2d 31 34  P4_INT32    (-14
62070 29 20 2f 2a 20 50 34 20 69 73 20 61 20 33 32 2d  ) /* P4 is a 32-
62080 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
62090 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  er */.#define P4
620a0 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 35 29 20  _INTARRAY (-15) 
620b0 2f 2a 20 50 34 20 69 73 20 61 20 76 65 63 74 6f  /* P4 is a vecto
620c0 72 20 6f 66 20 33 32 2d 62 69 74 20 69 6e 74 65  r of 32-bit inte
620d0 67 65 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  gers */.#define 
620e0 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 20 28  P4_SUBPROGRAM  (
620f0 2d 31 38 29 20 2f 2a 20 50 34 20 69 73 20 61 20  -18) /* P4 is a 
62100 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 75 62  pointer to a Sub
62110 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75 72  Program structur
62120 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  e */.#define P4_
62130 41 44 56 41 4e 43 45 20 20 28 2d 31 39 29 20 2f  ADVANCE  (-19) /
62140 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
62150 72 20 74 6f 20 42 74 72 65 65 4e 65 78 74 28 29  r to BtreeNext()
62160 20 6f 72 20 42 74 72 65 65 50 72 65 76 28 29 20   or BtreePrev() 
62170 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61 64 64 69  */../* When addi
62180 6e 67 20 61 20 50 34 20 61 72 67 75 6d 65 6e 74  ng a P4 argument
62190 20 75 73 69 6e 67 20 50 34 5f 4b 45 59 49 4e 46   using P4_KEYINF
621a0 4f 2c 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  O, a copy of the
621b0 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
621c0 72 65 0a 2a 2a 20 69 73 20 6d 61 64 65 2e 20 20  re.** is made.  
621d0 54 68 61 74 20 63 6f 70 79 20 69 73 20 66 72 65  That copy is fre
621e0 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65  ed when the Vdbe
621f0 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20   is finalized.  
62200 42 75 74 20 69 66 20 74 68 65 0a 2a 2a 20 61 72  But if the.** ar
62210 67 75 6d 65 6e 74 20 69 73 20 50 34 5f 4b 45 59  gument is P4_KEY
62220 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 74 68  INFO_HANDOFF, th
62230 65 20 70 61 73 73 65 64 20 69 6e 20 70 6f 69 6e  e passed in poin
62240 74 65 72 20 69 73 20 75 73 65 64 2e 20 20 49 74  ter is used.  It
62250 20 73 74 69 6c 6c 0a 2a 2a 20 67 65 74 73 20 66   still.** gets f
62260 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64  reed when the Vd
62270 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 20  be is finalized 
62280 73 6f 20 69 74 20 73 74 69 6c 6c 20 73 68 6f 75  so it still shou
62290 6c 64 20 62 65 20 6f 62 74 61 69 6e 65 64 0a 2a  ld be obtained.*
622a0 2a 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20  * from a single 
622b0 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20  sqliteMalloc(). 
622c0 20 42 75 74 20 6e 6f 20 63 6f 70 79 20 69 73 20   But no copy is 
622d0 6d 61 64 65 20 61 6e 64 20 74 68 65 20 63 61 6c  made and the cal
622e0 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ling.** function
622f0 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 74 72   should *not* tr
62300 79 20 74 6f 20 66 72 65 65 20 74 68 65 20 4b 65  y to free the Ke
62310 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e  yInfo..*/.#defin
62320 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  e P4_KEYINFO_HAN
62330 44 4f 46 46 20 28 2d 31 36 29 0a 23 64 65 66 69  DOFF (-16).#defi
62340 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54  ne P4_KEYINFO_ST
62350 41 54 49 43 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a  ATIC  (-17)../*.
62360 2a 2a 20 54 68 65 20 56 64 62 65 2e 61 43 6f 6c  ** The Vdbe.aCol
62370 4e 61 6d 65 20 61 72 72 61 79 20 63 6f 6e 74 61  Name array conta
62380 69 6e 73 20 35 6e 20 4d 65 6d 20 73 74 72 75 63  ins 5n Mem struc
62390 74 75 72 65 73 2c 20 77 68 65 72 65 20 6e 20 69  tures, where n i
623a0 73 20 74 68 65 20 0a 2a 2a 20 6e 75 6d 62 65 72  s the .** number
623b0 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64   of columns of d
623c0 61 74 61 20 72 65 74 75 72 6e 65 64 20 62 79 20  ata returned by 
623d0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  the statement..*
623e0 2f 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  /.#define COLNAM
623f0 45 5f 4e 41 4d 45 20 20 20 20 20 30 0a 23 64 65  E_NAME     0.#de
62400 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43  fine COLNAME_DEC
62410 4c 54 59 50 45 20 31 0a 23 64 65 66 69 6e 65 20  LTYPE 1.#define 
62420 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45  COLNAME_DATABASE
62430 20 32 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41   2.#define COLNA
62440 4d 45 5f 54 41 42 4c 45 20 20 20 20 33 0a 23 64  ME_TABLE    3.#d
62450 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f  efine COLNAME_CO
62460 4c 55 4d 4e 20 20 20 34 0a 23 69 66 64 65 66 20  LUMN   4.#ifdef 
62470 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f  SQLITE_ENABLE_CO
62480 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20  LUMN_METADATA.# 
62490 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e  define COLNAME_N
624a0 20 20 20 20 20 20 20 20 35 20 20 20 20 20 20 2f          5      /
624b0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 43 4f 4c 4e  * Number of COLN
624c0 41 4d 45 5f 78 78 78 20 73 79 6d 62 6f 6c 73 20  AME_xxx symbols 
624d0 2a 2f 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66  */.#else.# ifdef
624e0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43   SQLITE_OMIT_DEC
624f0 4c 54 59 50 45 0a 23 20 20 20 64 65 66 69 6e 65  LTYPE.#   define
62500 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20   COLNAME_N      
62510 31 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20  1      /* Store 
62520 6f 6e 6c 79 20 74 68 65 20 6e 61 6d 65 20 2a 2f  only the name */
62530 0a 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69  .# else.#   defi
62540 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20  ne COLNAME_N    
62550 20 20 32 20 20 20 20 20 20 2f 2a 20 53 74 6f 72    2      /* Stor
62560 65 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 64  e the name and d
62570 65 63 6c 74 79 70 65 20 2a 2f 0a 23 20 65 6e 64  ecltype */.# end
62580 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  if.#endif../*.**
62590 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   The following m
625a0 61 63 72 6f 20 63 6f 6e 76 65 72 74 73 20 61 20  acro converts a 
625b0 72 65 6c 61 74 69 76 65 20 61 64 64 72 65 73 73  relative address
625c0 20 69 6e 20 74 68 65 20 70 32 20 66 69 65 6c 64   in the p2 field
625d0 0a 2a 2a 20 6f 66 20 61 20 56 64 62 65 4f 70 20  .** of a VdbeOp 
625e0 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 61  structure into a
625f0 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72   negative number
62600 20 73 6f 20 74 68 61 74 20 0a 2a 2a 20 73 71 6c   so that .** sql
62610 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73  ite3VdbeAddOpLis
62620 74 28 29 20 6b 6e 6f 77 73 20 74 68 61 74 20 74  t() knows that t
62630 68 65 20 61 64 64 72 65 73 73 20 69 73 20 72 65  he address is re
62640 6c 61 74 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67  lative.  Calling
62650 0a 2a 2a 20 74 68 65 20 6d 61 63 72 6f 20 61 67  .** the macro ag
62660 61 69 6e 20 72 65 73 74 6f 72 65 73 20 74 68 65  ain restores the
62670 20 61 64 64 72 65 73 73 2e 0a 2a 2f 0a 23 64 65   address..*/.#de
62680 66 69 6e 65 20 41 44 44 52 28 58 29 20 20 28 2d  fine ADDR(X)  (-
62690 31 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  1-(X))../*.** Th
626a0 65 20 6d 61 6b 65 66 69 6c 65 20 73 63 61 6e 73  e makefile scans
626b0 20 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72   the vdbe.c sour
626c0 63 65 20 66 69 6c 65 20 61 6e 64 20 63 72 65 61  ce file and crea
626d0 74 65 73 20 74 68 65 20 22 6f 70 63 6f 64 65 73  tes the "opcodes
626e0 2e 68 22 0a 2a 2a 20 68 65 61 64 65 72 20 66 69  .h".** header fi
626f0 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  le that defines 
62700 61 20 6e 75 6d 62 65 72 20 66 6f 72 20 65 61 63  a number for eac
62710 68 20 6f 70 63 6f 64 65 20 75 73 65 64 20 62 79  h opcode used by
62720 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a   the VDBE..*/./*
62730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
62740 63 6c 75 64 65 20 6f 70 63 6f 64 65 73 2e 68 20  clude opcodes.h 
62750 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
62760 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a   vdbe.h ********
62770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
62780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
62790 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73  gin file opcodes
627a0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
627b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
627c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
627d0 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67   Automatically g
627e0 65 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f  enerated.  Do no
627f0 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65  t edit */./* See
62800 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61   the mkopcodeh.a
62810 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65  wk script for de
62820 74 61 69 6c 73 20 2a 2f 0a 23 64 65 66 69 6e 65  tails */.#define
62830 20 4f 50 5f 47 6f 74 6f 20 20 20 20 20 20 20 20   OP_Goto        
62840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62850 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69           1.#defi
62860 6e 65 20 4f 50 5f 47 6f 73 75 62 20 20 20 20 20  ne OP_Gosub     
62870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62880 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65             2.#de
62890 66 69 6e 65 20 4f 50 5f 52 65 74 75 72 6e 20 20  fine OP_Return  
628a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
628b0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23               3.#
628c0 64 65 66 69 6e 65 20 4f 50 5f 59 69 65 6c 64 20  define OP_Yield 
628d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
628e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
628f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74  .#define OP_Halt
62900 49 66 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20  IfNull          
62910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62920 20 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 61   5.#define OP_Ha
62930 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  lt              
62940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62950 20 20 20 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f     6.#define OP_
62960 49 6e 74 65 67 65 72 20 20 20 20 20 20 20 20 20  Integer         
62970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62980 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 4f       7.#define O
62990 50 5f 49 6e 74 36 34 20 20 20 20 20 20 20 20 20  P_Int64         
629a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
629b0 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65         8.#define
629c0 20 4f 50 5f 52 65 61 6c 20 20 20 20 20 20 20 20   OP_Real        
629d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
629e0 20 20 20 20 20 20 20 31 33 30 20 20 20 2f 2a 20         130   /* 
629f0 73 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54  same as TK_FLOAT
62a00 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f      */.#define O
62a10 50 5f 53 74 72 69 6e 67 38 20 20 20 20 20 20 20  P_String8       
62a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62a30 20 20 20 20 20 20 39 34 20 20 20 2f 2a 20 73 61        94   /* sa
62a40 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 20  me as TK_STRING 
62a50 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
62a60 53 74 72 69 6e 67 20 20 20 20 20 20 20 20 20 20  String          
62a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62a80 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 4f       9.#define O
62a90 50 5f 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20  P_Null          
62aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62ab0 20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 65        10.#define
62ac0 20 4f 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20   OP_Blob        
62ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62ae0 20 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69          11.#defi
62af0 6e 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 20  ne OP_Variable  
62b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62b10 20 20 20 20 20 20 20 20 20 20 31 32 0a 23 64 65            12.#de
62b20 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 20 20 20 20  fine OP_Move    
62b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62b40 20 20 20 20 20 20 20 20 20 20 20 20 31 33 0a 23              13.#
62b50 64 65 66 69 6e 65 20 4f 50 5f 43 6f 70 79 20 20  define OP_Copy  
62b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
62b80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 43 6f 70  .#define OP_SCop
62b90 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
62ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62bb0 31 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65  15.#define OP_Re
62bc0 73 75 6c 74 52 6f 77 20 20 20 20 20 20 20 20 20  sultRow         
62bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62be0 20 20 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f    16.#define OP_
62bf0 43 6f 6e 63 61 74 20 20 20 20 20 20 20 20 20 20  Concat          
62c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62c10 20 20 20 20 39 31 20 20 20 2f 2a 20 73 61 6d 65      91   /* same
62c20 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20   as TK_CONCAT   
62c30 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64  */.#define OP_Ad
62c40 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
62c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62c60 20 20 38 36 20 20 20 2f 2a 20 73 61 6d 65 20 61    86   /* same a
62c70 73 20 54 4b 5f 50 4c 55 53 20 20 20 20 20 2a 2f  s TK_PLUS     */
62c80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 75 62 74  .#define OP_Subt
62c90 72 61 63 74 20 20 20 20 20 20 20 20 20 20 20 20  ract            
62ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62cb0 38 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  87   /* same as 
62cc0 54 4b 5f 4d 49 4e 55 53 20 20 20 20 2a 2f 0a 23  TK_MINUS    */.#
62cd0 64 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70  define OP_Multip
62ce0 6c 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ly              
62cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38                88
62d00 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
62d10 5f 53 54 41 52 20 20 20 20 20 2a 2f 0a 23 64 65  _STAR     */.#de
62d20 66 69 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20  fine OP_Divide  
62d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62d40 20 20 20 20 20 20 20 20 20 20 20 20 38 39 20 20              89  
62d50 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53   /* same as TK_S
62d60 4c 41 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69  LASH    */.#defi
62d70 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20  ne OP_Remainder 
62d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62d90 20 20 20 20 20 20 20 20 20 20 39 30 20 20 20 2f            90   /
62da0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 4d  * same as TK_REM
62db0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
62dc0 20 4f 50 5f 43 6f 6c 6c 53 65 71 20 20 20 20 20   OP_CollSeq     
62dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62de0 20 20 20 20 20 20 20 20 31 37 0a 23 64 65 66 69          17.#defi
62df0 6e 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 20  ne OP_Function  
62e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62e10 20 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65            18.#de
62e20 66 69 6e 65 20 4f 50 5f 42 69 74 41 6e 64 20 20  fine OP_BitAnd  
62e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62e40 20 20 20 20 20 20 20 20 20 20 20 20 38 32 20 20              82  
62e50 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42   /* same as TK_B
62e60 49 54 41 4e 44 20 20 20 2a 2f 0a 23 64 65 66 69  ITAND   */.#defi
62e70 6e 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 20 20  ne OP_BitOr     
62e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62e90 20 20 20 20 20 20 20 20 20 20 38 33 20 20 20 2f            83   /
62ea0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54  * same as TK_BIT
62eb0 4f 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  OR    */.#define
62ec0 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 20 20   OP_ShiftLeft   
62ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62ee0 20 20 20 20 20 20 20 20 38 34 20 20 20 2f 2a 20          84   /* 
62ef0 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46  same as TK_LSHIF
62f00 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  T   */.#define O
62f10 50 5f 53 68 69 66 74 52 69 67 68 74 20 20 20 20  P_ShiftRight    
62f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62f30 20 20 20 20 20 20 38 35 20 20 20 2f 2a 20 73 61        85   /* sa
62f40 6d 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 20  me as TK_RSHIFT 
62f50 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
62f60 41 64 64 49 6d 6d 20 20 20 20 20 20 20 20 20 20  AddImm          
62f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62f80 20 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 4f      20.#define O
62f90 50 5f 4d 75 73 74 42 65 49 6e 74 20 20 20 20 20  P_MustBeInt     
62fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62fb0 20 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65        21.#define
62fc0 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79   OP_RealAffinity
62fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62fe0 20 20 20 20 20 20 20 20 32 32 0a 23 64 65 66 69          22.#defi
62ff0 6e 65 20 4f 50 5f 54 6f 54 65 78 74 20 20 20 20  ne OP_ToText    
63000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63010 20 20 20 20 20 20 20 20 20 31 34 31 20 20 20 2f           141   /
63020 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
63030 54 45 58 54 20 20 2a 2f 0a 23 64 65 66 69 6e 65  TEXT  */.#define
63040 20 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 20 20   OP_ToBlob      
63050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63060 20 20 20 20 20 20 20 31 34 32 20 20 20 2f 2a 20         142   /* 
63070 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c  same as TK_TO_BL
63080 4f 42 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  OB  */.#define O
63090 50 5f 54 6f 4e 75 6d 65 72 69 63 20 20 20 20 20  P_ToNumeric     
630a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
630b0 20 20 20 20 20 31 34 33 20 20 20 2f 2a 20 73 61       143   /* sa
630c0 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45  me as TK_TO_NUME
630d0 52 49 43 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  RIC*/.#define OP
630e0 5f 54 6f 49 6e 74 20 20 20 20 20 20 20 20 20 20  _ToInt          
630f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63100 20 20 20 20 31 34 34 20 20 20 2f 2a 20 73 61 6d      144   /* sam
63110 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 20 20  e as TK_TO_INT  
63120 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54   */.#define OP_T
63130 6f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20  oReal           
63140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63150 20 20 31 34 35 20 20 20 2f 2a 20 73 61 6d 65 20    145   /* same 
63160 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 2a  as TK_TO_REAL  *
63170 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 71 20  /.#define OP_Eq 
63180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
631a0 20 37 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   76   /* same as
631b0 20 54 4b 5f 45 51 20 20 20 20 20 20 20 2a 2f 0a   TK_EQ       */.
631c0 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65 20 20 20  #define OP_Ne   
631d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
631e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
631f0 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  5   /* same as T
63200 4b 5f 4e 45 20 20 20 20 20 20 20 2a 2f 0a 23 64  K_NE       */.#d
63210 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20 20  efine OP_Lt     
63220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63230 20 20 20 20 20 20 20 20 20 20 20 20 20 37 39 20               79 
63240 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
63250 4c 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  LT       */.#def
63260 69 6e 65 20 4f 50 5f 4c 65 20 20 20 20 20 20 20  ine OP_Le       
63270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63280 20 20 20 20 20 20 20 20 20 20 20 37 38 20 20 20             78   
63290 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45  /* same as TK_LE
632a0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
632b0 65 20 4f 50 5f 47 74 20 20 20 20 20 20 20 20 20  e OP_Gt         
632c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
632d0 20 20 20 20 20 20 20 20 20 37 37 20 20 20 2f 2a           77   /*
632e0 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 20 20   same as TK_GT  
632f0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
63300 4f 50 5f 47 65 20 20 20 20 20 20 20 20 20 20 20  OP_Ge           
63310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63320 20 20 20 20 20 20 20 38 30 20 20 20 2f 2a 20 73         80   /* s
63330 61 6d 65 20 61 73 20 54 4b 5f 47 45 20 20 20 20  ame as TK_GE    
63340 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
63350 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 20 20 20  _Permutation    
63360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63370 20 20 20 20 20 32 33 0a 23 64 65 66 69 6e 65 20       23.#define 
63380 4f 50 5f 43 6f 6d 70 61 72 65 20 20 20 20 20 20  OP_Compare      
63390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
633a0 20 20 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e         24.#defin
633b0 65 20 4f 50 5f 4a 75 6d 70 20 20 20 20 20 20 20  e OP_Jump       
633c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
633d0 20 20 20 20 20 20 20 20 20 32 35 0a 23 64 65 66           25.#def
633e0 69 6e 65 20 4f 50 5f 41 6e 64 20 20 20 20 20 20  ine OP_And      
633f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63400 20 20 20 20 20 20 20 20 20 20 20 36 39 20 20 20             69   
63410 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e  /* same as TK_AN
63420 44 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  D      */.#defin
63430 65 20 4f 50 5f 4f 72 20 20 20 20 20 20 20 20 20  e OP_Or         
63440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63450 20 20 20 20 20 20 20 20 20 36 38 20 20 20 2f 2a           68   /*
63460 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 20 20   same as TK_OR  
63470 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
63480 4f 50 5f 4e 6f 74 20 20 20 20 20 20 20 20 20 20  OP_Not          
63490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
634a0 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 73         19   /* s
634b0 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 20 20 20  ame as TK_NOT   
634c0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
634d0 5f 42 69 74 4e 6f 74 20 20 20 20 20 20 20 20 20  _BitNot         
634e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
634f0 20 20 20 20 20 39 33 20 20 20 2f 2a 20 73 61 6d       93   /* sam
63500 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 20 20  e as TK_BITNOT  
63510 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f   */.#define OP_O
63520 6e 63 65 20 20 20 20 20 20 20 20 20 20 20 20 20  nce             
63530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63540 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 4f 50     26.#define OP
63550 5f 49 66 20 20 20 20 20 20 20 20 20 20 20 20 20  _If             
63560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63570 20 20 20 20 20 32 37 0a 23 64 65 66 69 6e 65 20       27.#define 
63580 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20 20 20 20  OP_IfNot        
63590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
635a0 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 69 6e         28.#defin
635b0 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20 20 20  e OP_IsNull     
635c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
635d0 20 20 20 20 20 20 20 20 20 37 33 20 20 20 2f 2a           73   /*
635e0 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55   same as TK_ISNU
635f0 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  LL   */.#define 
63600 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20 20 20  OP_NotNull      
63610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63620 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a 20 73         74   /* s
63630 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c  ame as TK_NOTNUL
63640 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  L  */.#define OP
63650 5f 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 20 20  _Column         
63660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63670 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65 20       29.#define 
63680 4f 50 5f 41 66 66 69 6e 69 74 79 20 20 20 20 20  OP_Affinity     
63690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
636a0 20 20 20 20 20 20 20 33 30 0a 23 64 65 66 69 6e         30.#defin
636b0 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20  e OP_MakeRecord 
636c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
636d0 20 20 20 20 20 20 20 20 20 33 31 0a 23 64 65 66           31.#def
636e0 69 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 20 20 20  ine OP_Count    
636f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63700 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 23 64             32.#d
63710 65 66 69 6e 65 20 4f 50 5f 53 61 76 65 70 6f 69  efine OP_Savepoi
63720 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
63730 20 20 20 20 20 20 20 20 20 20 20 20 20 33 33 0a               33.
63740 23 64 65 66 69 6e 65 20 4f 50 5f 41 75 74 6f 43  #define OP_AutoC
63750 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 20 20  ommit           
63760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
63770 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 72 61  4.#define OP_Tra
63780 6e 73 61 63 74 69 6f 6e 20 20 20 20 20 20 20 20  nsaction        
63790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
637a0 20 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52   35.#define OP_R
637b0 65 61 64 43 6f 6f 6b 69 65 20 20 20 20 20 20 20  eadCookie       
637c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
637d0 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 4f 50     36.#define OP
637e0 5f 53 65 74 43 6f 6f 6b 69 65 20 20 20 20 20 20  _SetCookie      
637f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63800 20 20 20 20 20 33 37 0a 23 64 65 66 69 6e 65 20       37.#define 
63810 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20  OP_VerifyCookie 
63820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63830 20 20 20 20 20 20 20 33 38 0a 23 64 65 66 69 6e         38.#defin
63840 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 20 20  e OP_OpenRead   
63850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63860 20 20 20 20 20 20 20 20 20 33 39 0a 23 64 65 66           39.#def
63870 69 6e 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  ine OP_OpenWrite
63880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63890 20 20 20 20 20 20 20 20 20 20 20 34 30 0a 23 64             40.#d
638a0 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 41 75 74  efine OP_OpenAut
638b0 6f 69 6e 64 65 78 20 20 20 20 20 20 20 20 20 20  oindex          
638c0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 31 0a               41.
638d0 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 45  #define OP_OpenE
638e0 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 20  phemeral        
638f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
63900 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72  2.#define OP_Sor
63910 74 65 72 4f 70 65 6e 20 20 20 20 20 20 20 20 20  terOpen         
63920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63930 20 34 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f   43.#define OP_O
63940 70 65 6e 50 73 65 75 64 6f 20 20 20 20 20 20 20  penPseudo       
63950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63960 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20 4f 50     44.#define OP
63970 5f 43 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20  _Close          
63980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63990 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65 20       45.#define 
639a0 4f 50 5f 53 65 65 6b 4c 74 20 20 20 20 20 20 20  OP_SeekLt       
639b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
639c0 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e         46.#defin
639d0 65 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 20  e OP_SeekLe     
639e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
639f0 20 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 66           47.#def
63a00 69 6e 65 20 4f 50 5f 53 65 65 6b 47 65 20 20 20  ine OP_SeekGe   
63a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63a20 20 20 20 20 20 20 20 20 20 20 20 34 38 0a 23 64             48.#d
63a30 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 74 20  efine OP_SeekGt 
63a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63a50 20 20 20 20 20 20 20 20 20 20 20 20 20 34 39 0a               49.
63a60 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 20  #define OP_Seek 
63a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
63a90 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74  0.#define OP_Not
63aa0 46 6f 75 6e 64 20 20 20 20 20 20 20 20 20 20 20  Found           
63ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63ac0 20 35 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46   51.#define OP_F
63ad0 6f 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 20  ound            
63ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63af0 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20 4f 50     52.#define OP
63b00 5f 49 73 55 6e 69 71 75 65 20 20 20 20 20 20 20  _IsUnique       
63b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63b20 20 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20       53.#define 
63b30 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 20 20 20  OP_NotExists    
63b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63b50 20 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e         54.#defin
63b60 65 20 4f 50 5f 53 65 71 75 65 6e 63 65 20 20 20  e OP_Sequence   
63b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63b80 20 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66           55.#def
63b90 69 6e 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20  ine OP_NewRowid 
63ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63bb0 20 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 64             56.#d
63bc0 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74 20  efine OP_Insert 
63bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63be0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 37 0a               57.
63bf0 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72  #define OP_Inser
63c00 74 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  tInt            
63c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
63c20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 6c  8.#define OP_Del
63c30 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ete             
63c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63c50 20 35 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52   59.#define OP_R
63c60 65 73 65 74 43 6f 75 6e 74 20 20 20 20 20 20 20  esetCount       
63c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63c80 20 20 20 36 30 0a 23 64 65 66 69 6e 65 20 4f 50     60.#define OP
63c90 5f 53 6f 72 74 65 72 43 6f 6d 70 61 72 65 20 20  _SorterCompare  
63ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63cb0 20 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65 20       61.#define 
63cc0 4f 50 5f 53 6f 72 74 65 72 44 61 74 61 20 20 20  OP_SorterData   
63cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63ce0 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e         62.#defin
63cf0 65 20 4f 50 5f 52 6f 77 4b 65 79 20 20 20 20 20  e OP_RowKey     
63d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63d10 20 20 20 20 20 20 20 20 20 36 33 0a 23 64 65 66           63.#def
63d20 69 6e 65 20 4f 50 5f 52 6f 77 44 61 74 61 20 20  ine OP_RowData  
63d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63d40 20 20 20 20 20 20 20 20 20 20 20 36 34 0a 23 64             64.#d
63d50 65 66 69 6e 65 20 4f 50 5f 52 6f 77 69 64 20 20  efine OP_Rowid  
63d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63d70 20 20 20 20 20 20 20 20 20 20 20 20 20 36 35 0a               65.
63d80 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75 6c 6c 52  #define OP_NullR
63d90 6f 77 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ow              
63da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
63db0 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 61 73  6.#define OP_Las
63dc0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
63dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63de0 20 36 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53   67.#define OP_S
63df0 6f 72 74 65 72 53 6f 72 74 20 20 20 20 20 20 20  orterSort       
63e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63e10 20 20 20 37 30 0a 23 64 65 66 69 6e 65 20 4f 50     70.#define OP
63e20 5f 53 6f 72 74 20 20 20 20 20 20 20 20 20 20 20  _Sort           
63e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63e40 20 20 20 20 20 37 31 0a 23 64 65 66 69 6e 65 20       71.#define 
63e50 4f 50 5f 52 65 77 69 6e 64 20 20 20 20 20 20 20  OP_Rewind       
63e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63e70 20 20 20 20 20 20 20 37 32 0a 23 64 65 66 69 6e         72.#defin
63e80 65 20 4f 50 5f 53 6f 72 74 65 72 4e 65 78 74 20  e OP_SorterNext 
63e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63ea0 20 20 20 20 20 20 20 20 20 38 31 0a 23 64 65 66           81.#def
63eb0 69 6e 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20  ine OP_Prev     
63ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63ed0 20 20 20 20 20 20 20 20 20 20 20 39 32 0a 23 64             92.#d
63ee0 65 66 69 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20  efine OP_Next   
63ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63f00 20 20 20 20 20 20 20 20 20 20 20 20 20 39 35 0a               95.
63f10 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 65  #define OP_Sorte
63f20 72 49 6e 73 65 72 74 20 20 20 20 20 20 20 20 20  rInsert         
63f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
63f40 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78  6.#define OP_Idx
63f50 49 6e 73 65 72 74 20 20 20 20 20 20 20 20 20 20  Insert          
63f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63f70 20 39 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49   97.#define OP_I
63f80 64 78 44 65 6c 65 74 65 20 20 20 20 20 20 20 20  dxDelete        
63f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63fa0 20 20 20 39 38 0a 23 64 65 66 69 6e 65 20 4f 50     98.#define OP
63fb0 5f 49 64 78 52 6f 77 69 64 20 20 20 20 20 20 20  _IdxRowid       
63fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63fd0 20 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20       99.#define 
63fe0 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20  OP_IdxLT        
63ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64000 20 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e        100.#defin
64010 65 20 4f 50 5f 49 64 78 47 45 20 20 20 20 20 20  e OP_IdxGE      
64020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64030 20 20 20 20 20 20 20 20 31 30 31 0a 23 64 65 66          101.#def
64040 69 6e 65 20 4f 50 5f 44 65 73 74 72 6f 79 20 20  ine OP_Destroy  
64050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64060 20 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64            102.#d
64070 65 66 69 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20  efine OP_Clear  
64080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64090 20 20 20 20 20 20 20 20 20 20 20 20 31 30 33 0a              103.
640a0 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 74  #define OP_Creat
640b0 65 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 20  eIndex          
640c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
640d0 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 65  4.#define OP_Cre
640e0 61 74 65 54 61 62 6c 65 20 20 20 20 20 20 20 20  ateTable        
640f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64100 31 30 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50  105.#define OP_P
64110 61 72 73 65 53 63 68 65 6d 61 20 20 20 20 20 20  arseSchema      
64120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64130 20 20 31 30 36 0a 23 64 65 66 69 6e 65 20 4f 50    106.#define OP
64140 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 20 20  _LoadAnalysis   
64150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64160 20 20 20 20 31 30 37 0a 23 64 65 66 69 6e 65 20      107.#define 
64170 4f 50 5f 44 72 6f 70 54 61 62 6c 65 20 20 20 20  OP_DropTable    
64180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64190 20 20 20 20 20 20 31 30 38 0a 23 64 65 66 69 6e        108.#defin
641a0 65 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 20 20  e OP_DropIndex  
641b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
641c0 20 20 20 20 20 20 20 20 31 30 39 0a 23 64 65 66          109.#def
641d0 69 6e 65 20 4f 50 5f 44 72 6f 70 54 72 69 67 67  ine OP_DropTrigg
641e0 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  er              
641f0 20 20 20 20 20 20 20 20 20 20 31 31 30 0a 23 64            110.#d
64200 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69  efine OP_Integri
64210 74 79 43 6b 20 20 20 20 20 20 20 20 20 20 20 20  tyCk            
64220 20 20 20 20 20 20 20 20 20 20 20 20 31 31 31 0a              111.
64230 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 53 65  #define OP_RowSe
64240 74 41 64 64 20 20 20 20 20 20 20 20 20 20 20 20  tAdd            
64250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
64260 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77  2.#define OP_Row
64270 53 65 74 52 65 61 64 20 20 20 20 20 20 20 20 20  SetRead         
64280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64290 31 31 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52  113.#define OP_R
642a0 6f 77 53 65 74 54 65 73 74 20 20 20 20 20 20 20  owSetTest       
642b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
642c0 20 20 31 31 34 0a 23 64 65 66 69 6e 65 20 4f 50    114.#define OP
642d0 5f 50 72 6f 67 72 61 6d 20 20 20 20 20 20 20 20  _Program        
642e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
642f0 20 20 20 20 31 31 35 0a 23 64 65 66 69 6e 65 20      115.#define 
64300 4f 50 5f 50 61 72 61 6d 20 20 20 20 20 20 20 20  OP_Param        
64310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64320 20 20 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e        116.#defin
64330 65 20 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 20 20  e OP_FkCounter  
64340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64350 20 20 20 20 20 20 20 20 31 31 37 0a 23 64 65 66          117.#def
64360 69 6e 65 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 20  ine OP_FkIfZero 
64370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64380 20 20 20 20 20 20 20 20 20 20 31 31 38 0a 23 64            118.#d
64390 65 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61 78 20  efine OP_MemMax 
643a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
643b0 20 20 20 20 20 20 20 20 20 20 20 20 31 31 39 0a              119.
643c0 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 50 6f 73  #define OP_IfPos
643d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
643e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
643f0 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 4e  0.#define OP_IfN
64400 65 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  eg              
64410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64420 31 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49  121.#define OP_I
64430 66 5a 65 72 6f 20 20 20 20 20 20 20 20 20 20 20  fZero           
64440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64450 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20 4f 50    122.#define OP
64460 5f 41 67 67 53 74 65 70 20 20 20 20 20 20 20 20  _AggStep        
64470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64480 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20      123.#define 
64490 4f 50 5f 41 67 67 46 69 6e 61 6c 20 20 20 20 20  OP_AggFinal     
644a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
644b0 20 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e        124.#defin
644c0 65 20 4f 50 5f 43 68 65 63 6b 70 6f 69 6e 74 20  e OP_Checkpoint 
644d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
644e0 20 20 20 20 20 20 20 20 31 32 35 0a 23 64 65 66          125.#def
644f0 69 6e 65 20 4f 50 5f 4a 6f 75 72 6e 61 6c 4d 6f  ine OP_JournalMo
64500 64 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  de              
64510 20 20 20 20 20 20 20 20 20 20 31 32 36 0a 23 64            126.#d
64520 65 66 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d 20  efine OP_Vacuum 
64530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64540 20 20 20 20 20 20 20 20 20 20 20 20 31 32 37 0a              127.
64550 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 63 72 56  #define OP_IncrV
64560 61 63 75 75 6d 20 20 20 20 20 20 20 20 20 20 20  acuum           
64570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
64580 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 78 70  8.#define OP_Exp
64590 69 72 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ire             
645a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
645b0 31 32 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54  129.#define OP_T
645c0 61 62 6c 65 4c 6f 63 6b 20 20 20 20 20 20 20 20  ableLock        
645d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
645e0 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 4f 50    131.#define OP
645f0 5f 56 42 65 67 69 6e 20 20 20 20 20 20 20 20 20  _VBegin         
64600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64610 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 20      132.#define 
64620 4f 50 5f 56 43 72 65 61 74 65 20 20 20 20 20 20  OP_VCreate      
64630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64640 20 20 20 20 20 20 31 33 33 0a 23 64 65 66 69 6e        133.#defin
64650 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20 20  e OP_VDestroy   
64660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64670 20 20 20 20 20 20 20 20 31 33 34 0a 23 64 65 66          134.#def
64680 69 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20 20 20  ine OP_VOpen    
64690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
646a0 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 64            135.#d
646b0 65 66 69 6e 65 20 4f 50 5f 56 46 69 6c 74 65 72  efine OP_VFilter
646c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
646d0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36 0a              136.
646e0 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 6f 6c 75  #define OP_VColu
646f0 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  mn              
64700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
64710 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 4e 65  7.#define OP_VNe
64720 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  xt              
64730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64740 31 33 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56  138.#define OP_V
64750 52 65 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20  Rename          
64760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64770 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 4f 50    139.#define OP
64780 5f 56 55 70 64 61 74 65 20 20 20 20 20 20 20 20  _VUpdate        
64790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
647a0 20 20 20 20 31 34 30 0a 23 64 65 66 69 6e 65 20      140.#define 
647b0 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 20 20 20  OP_Pagecount    
647c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
647d0 20 20 20 20 20 20 31 34 36 0a 23 64 65 66 69 6e        146.#defin
647e0 65 20 4f 50 5f 4d 61 78 50 67 63 6e 74 20 20 20  e OP_MaxPgcnt   
647f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64800 20 20 20 20 20 20 20 20 31 34 37 0a 23 64 65 66          147.#def
64810 69 6e 65 20 4f 50 5f 54 72 61 63 65 20 20 20 20  ine OP_Trace    
64820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64830 20 20 20 20 20 20 20 20 20 20 31 34 38 0a 23 64            148.#d
64840 65 66 69 6e 65 20 4f 50 5f 4e 6f 6f 70 20 20 20  efine OP_Noop   
64850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64860 20 20 20 20 20 20 20 20 20 20 20 20 31 34 39 0a              149.
64870 23 64 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61  #define OP_Expla
64880 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  in              
64890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35                15
648a0 30 0a 0a 0a 2f 2a 20 50 72 6f 70 65 72 74 69 65  0.../* Propertie
648b0 73 20 73 75 63 68 20 61 73 20 22 6f 75 74 32 22  s such as "out2"
648c0 20 6f 72 20 22 6a 75 6d 70 22 20 74 68 61 74 20   or "jump" that 
648d0 61 72 65 20 73 70 65 63 69 66 69 65 64 20 69 6e  are specified in
648e0 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c  .** comments fol
648f0 6c 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73 65  lowing the "case
64900 22 20 66 6f 72 20 65 61 63 68 20 6f 70 63 6f 64  " for each opcod
64910 65 20 69 6e 20 74 68 65 20 76 64 62 65 2e 63 0a  e in the vdbe.c.
64920 2a 2a 20 61 72 65 20 65 6e 63 6f 64 65 64 20 69  ** are encoded i
64930 6e 74 6f 20 62 69 74 76 65 63 74 6f 72 73 20 61  nto bitvectors a
64940 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2f 0a 23 64  s follows:.*/.#d
64950 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4a 55 4d 50  efine OPFLG_JUMP
64960 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30              0x00
64970 30 31 20 20 2f 2a 20 6a 75 6d 70 3a 20 20 50 32  01  /* jump:  P2
64980 20 68 6f 6c 64 73 20 6a 6d 70 20 74 61 72 67 65   holds jmp targe
64990 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  t */.#define OPF
649a0 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41  LG_OUT2_PRERELEA
649b0 53 45 20 30 78 30 30 30 32 20 20 2f 2a 20 6f 75  SE 0x0002  /* ou
649c0 74 32 2d 70 72 65 72 65 6c 65 61 73 65 3a 20 2a  t2-prerelease: *
649d0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f  /.#define OPFLG_
649e0 49 4e 31 20 20 20 20 20 20 20 20 20 20 20 20 20  IN1             
649f0 30 78 30 30 30 34 20 20 2f 2a 20 69 6e 31 3a 20  0x0004  /* in1: 
64a00 20 20 50 31 20 69 73 20 61 6e 20 69 6e 70 75 74    P1 is an input
64a10 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c   */.#define OPFL
64a20 47 5f 49 4e 32 20 20 20 20 20 20 20 20 20 20 20  G_IN2           
64a30 20 20 30 78 30 30 30 38 20 20 2f 2a 20 69 6e 32    0x0008  /* in2
64a40 3a 20 20 20 50 32 20 69 73 20 61 6e 20 69 6e 70  :   P2 is an inp
64a50 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  ut */.#define OP
64a60 46 4c 47 5f 49 4e 33 20 20 20 20 20 20 20 20 20  FLG_IN3         
64a70 20 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 69      0x0010  /* i
64a80 6e 33 3a 20 20 20 50 33 20 69 73 20 61 6e 20 69  n3:   P3 is an i
64a90 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  nput */.#define 
64aa0 4f 50 46 4c 47 5f 4f 55 54 32 20 20 20 20 20 20  OPFLG_OUT2      
64ab0 20 20 20 20 20 20 30 78 30 30 32 30 20 20 2f 2a        0x0020  /*
64ac0 20 6f 75 74 32 3a 20 20 50 32 20 69 73 20 61 6e   out2:  P2 is an
64ad0 20 6f 75 74 70 75 74 20 2a 2f 0a 23 64 65 66 69   output */.#defi
64ae0 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 33 20 20 20  ne OPFLG_OUT3   
64af0 20 20 20 20 20 20 20 20 20 30 78 30 30 34 30 20           0x0040 
64b00 20 2f 2a 20 6f 75 74 33 3a 20 20 50 33 20 69 73   /* out3:  P3 is
64b10 20 61 6e 20 6f 75 74 70 75 74 20 2a 2f 0a 23 64   an output */.#d
64b20 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 49 54  efine OPFLG_INIT
64b30 49 41 4c 49 5a 45 52 20 7b 5c 0a 2f 2a 20 20 20  IALIZER {\./*   
64b40 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c  0 */ 0x00, 0x01,
64b50 20 30 78 30 31 2c 20 30 78 30 34 2c 20 30 78 30   0x01, 0x04, 0x0
64b60 34 2c 20 30 78 31 30 2c 20 30 78 30 30 2c 20 30  4, 0x10, 0x00, 0
64b70 78 30 32 2c 5c 0a 2f 2a 20 20 20 38 20 2a 2f 20  x02,\./*   8 */ 
64b80 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
64b90 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
64ba0 30 30 2c 20 30 78 30 30 2c 20 30 78 32 34 2c 5c  00, 0x00, 0x24,\
64bb0 0a 2f 2a 20 20 31 36 20 2a 2f 20 30 78 30 30 2c  ./*  16 */ 0x00,
64bc0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 32   0x00, 0x00, 0x2
64bd0 34 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30  4, 0x04, 0x05, 0
64be0 78 30 34 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20  x04, 0x00,\./*  
64bf0 32 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31  24 */ 0x00, 0x01
64c00 2c 20 30 78 30 31 2c 20 30 78 30 35 2c 20 30 78  , 0x01, 0x05, 0x
64c10 30 35 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  05, 0x00, 0x00, 
64c20 30 78 30 30 2c 5c 0a 2f 2a 20 20 33 32 20 2a 2f  0x00,\./*  32 */
64c30 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30   0x02, 0x00, 0x0
64c40 30 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30  0, 0x00, 0x02, 0
64c50 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x10, 0x00, 0x00,
64c60 5c 0a 2f 2a 20 20 34 30 20 2a 2f 20 30 78 30 30  \./*  40 */ 0x00
64c70 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
64c80 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
64c90 30 78 31 31 2c 20 30 78 31 31 2c 5c 0a 2f 2a 20  0x11, 0x11,\./* 
64ca0 20 34 38 20 2a 2f 20 30 78 31 31 2c 20 30 78 31   48 */ 0x11, 0x1
64cb0 31 2c 20 30 78 30 38 2c 20 30 78 31 31 2c 20 30  1, 0x08, 0x11, 0
64cc0 78 31 31 2c 20 30 78 31 31 2c 20 30 78 31 31 2c  x11, 0x11, 0x11,
64cd0 20 30 78 30 32 2c 5c 0a 2f 2a 20 20 35 36 20 2a   0x02,\./*  56 *
64ce0 2f 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78  / 0x02, 0x00, 0x
64cf0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
64d00 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
64d10 2c 5c 0a 2f 2a 20 20 36 34 20 2a 2f 20 30 78 30  ,\./*  64 */ 0x0
64d20 30 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30  0, 0x02, 0x00, 0
64d30 78 30 31 2c 20 30 78 34 63 2c 20 30 78 34 63 2c  x01, 0x4c, 0x4c,
64d40 20 30 78 30 31 2c 20 30 78 30 31 2c 5c 0a 2f 2a   0x01, 0x01,\./*
64d50 20 20 37 32 20 2a 2f 20 30 78 30 31 2c 20 30 78    72 */ 0x01, 0x
64d60 30 35 2c 20 30 78 30 35 2c 20 30 78 31 35 2c 20  05, 0x05, 0x15, 
64d70 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35  0x15, 0x15, 0x15
64d80 2c 20 30 78 31 35 2c 5c 0a 2f 2a 20 20 38 30 20  , 0x15,\./*  80 
64d90 2a 2f 20 30 78 31 35 2c 20 30 78 30 31 2c 20 30  */ 0x15, 0x01, 0
64da0 78 34 63 2c 20 30 78 34 63 2c 20 30 78 34 63 2c  x4c, 0x4c, 0x4c,
64db0 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 34   0x4c, 0x4c, 0x4
64dc0 63 2c 5c 0a 2f 2a 20 20 38 38 20 2a 2f 20 30 78  c,\./*  88 */ 0x
64dd0 34 63 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 20  4c, 0x4c, 0x4c, 
64de0 30 78 34 63 2c 20 30 78 30 31 2c 20 30 78 32 34  0x4c, 0x01, 0x24
64df0 2c 20 30 78 30 32 2c 20 30 78 30 31 2c 5c 0a 2f  , 0x02, 0x01,\./
64e00 2a 20 20 39 36 20 2a 2f 20 30 78 30 38 2c 20 30  *  96 */ 0x08, 0
64e10 78 30 38 2c 20 30 78 30 30 2c 20 30 78 30 32 2c  x08, 0x00, 0x02,
64e20 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30   0x01, 0x01, 0x0
64e30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 31 30 34  2, 0x00,\./* 104
64e40 20 2a 2f 20 30 78 30 32 2c 20 30 78 30 32 2c 20   */ 0x02, 0x02, 
64e50 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
64e60 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
64e70 30 30 2c 5c 0a 2f 2a 20 31 31 32 20 2a 2f 20 30  00,\./* 112 */ 0
64e80 78 30 63 2c 20 30 78 34 35 2c 20 30 78 31 35 2c  x0c, 0x45, 0x15,
64e90 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30   0x01, 0x02, 0x0
64ea0 30 2c 20 30 78 30 31 2c 20 30 78 30 38 2c 5c 0a  0, 0x01, 0x08,\.
64eb0 2f 2a 20 31 32 30 20 2a 2f 20 30 78 30 35 2c 20  /* 120 */ 0x05, 
64ec0 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 30 30  0x05, 0x05, 0x00
64ed0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
64ee0 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 31 32  02, 0x00,\./* 12
64ef0 38 20 2a 2f 20 30 78 30 31 2c 20 30 78 30 30 2c  8 */ 0x01, 0x00,
64f00 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30   0x02, 0x00, 0x0
64f10 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
64f20 78 30 30 2c 5c 0a 2f 2a 20 31 33 36 20 2a 2f 20  x00,\./* 136 */ 
64f30 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 31  0x01, 0x00, 0x01
64f40 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
64f50 30 34 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 5c  04, 0x04, 0x04,\
64f60 0a 2f 2a 20 31 34 34 20 2a 2f 20 30 78 30 34 2c  ./* 144 */ 0x04,
64f70 20 30 78 30 34 2c 20 30 78 30 32 2c 20 30 78 30   0x04, 0x02, 0x0
64f80 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  2, 0x00, 0x00, 0
64f90 78 30 30 2c 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  x00,}../********
64fa0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70  ****** End of op
64fb0 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  codes.h ********
64fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64fe0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
64ff0 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
65000 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
65010 6f 66 66 20 69 6e 20 76 64 62 65 2e 68 20 2a 2a  off in vdbe.h **
65020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
65030 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72  *****/../*.** Pr
65040 6f 74 6f 74 79 70 65 73 20 66 6f 72 20 74 68 65  ototypes for the
65050 20 56 44 42 45 20 69 6e 74 65 72 66 61 63 65 2e   VDBE interface.
65060 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f    See comments o
65070 6e 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  n the implementa
65080 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 20 64 65  tion.** for a de
65090 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 77 68 61  scription of wha
650a0 74 20 65 61 63 68 20 6f 66 20 74 68 65 73 65 20  t each of these 
650b0 72 6f 75 74 69 6e 65 73 20 64 6f 65 73 2e 0a 2a  routines does..*
650c0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
650d0 20 56 64 62 65 20 2a 73 71 6c 69 74 65 33 56 64   Vdbe *sqlite3Vd
650e0 62 65 43 72 65 61 74 65 28 73 71 6c 69 74 65 33  beCreate(sqlite3
650f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
65100 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
65110 62 65 41 64 64 4f 70 30 28 56 64 62 65 2a 2c 69  beAddOp0(Vdbe*,i
65120 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
65130 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
65140 64 62 65 41 64 64 4f 70 31 28 56 64 62 65 2a 2c  dbeAddOp1(Vdbe*,
65150 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
65160 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
65170 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 56  ite3VdbeAddOp2(V
65180 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  dbe*,int,int,int
65190 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
651a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
651b0 65 41 64 64 4f 70 33 28 56 64 62 65 2a 2c 69 6e  eAddOp3(Vdbe*,in
651c0 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a  t,int,int,int);.
651d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
651e0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
651f0 64 4f 70 34 28 56 64 62 65 2a 2c 69 6e 74 2c 69  dOp4(Vdbe*,int,i
65200 6e 74 2c 69 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74  nt,int,int,const
65210 20 63 68 61 72 20 2a 7a 50 34 2c 69 6e 74 29 3b   char *zP4,int);
65220 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
65230 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
65240 64 64 4f 70 34 49 6e 74 28 56 64 62 65 2a 2c 69  ddOp4Int(Vdbe*,i
65250 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69  nt,int,int,int,i
65260 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
65270 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
65280 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62  dbeAddOpList(Vdb
65290 65 2a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62  e*, int nOp, Vdb
652a0 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61  eOpList const *a
652b0 4f 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  Op);.SQLITE_PRIV
652c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
652d0 56 64 62 65 41 64 64 50 61 72 73 65 53 63 68 65  VdbeAddParseSche
652e0 6d 61 4f 70 28 56 64 62 65 2a 2c 69 6e 74 2c 63  maOp(Vdbe*,int,c
652f0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
65300 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
65310 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 56  e3VdbeChangeP1(V
65320 64 62 65 2a 2c 20 75 33 32 20 61 64 64 72 2c 20  dbe*, u32 addr, 
65330 69 6e 74 20 50 31 29 3b 0a 53 51 4c 49 54 45 5f  int P1);.SQLITE_
65340 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
65350 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
65360 28 56 64 62 65 2a 2c 20 75 33 32 20 61 64 64 72  (Vdbe*, u32 addr
65370 2c 20 69 6e 74 20 50 32 29 3b 0a 53 51 4c 49 54  , int P2);.SQLIT
65380 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
65390 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
653a0 50 33 28 56 64 62 65 2a 2c 20 75 33 32 20 61 64  P3(Vdbe*, u32 ad
653b0 64 72 2c 20 69 6e 74 20 50 33 29 3b 0a 53 51 4c  dr, int P3);.SQL
653c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
653d0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
653e0 67 65 50 35 28 56 64 62 65 2a 2c 20 75 38 20 50  geP5(Vdbe*, u8 P
653f0 35 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  5);.SQLITE_PRIVA
65400 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
65410 64 62 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65  dbeJumpHere(Vdbe
65420 2a 2c 20 69 6e 74 20 61 64 64 72 29 3b 0a 53 51  *, int addr);.SQ
65430 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
65440 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  d sqlite3VdbeCha
65450 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c  ngeToNoop(Vdbe*,
65460 20 69 6e 74 20 61 64 64 72 29 3b 0a 53 51 4c 49   int addr);.SQLI
65470 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
65480 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
65490 65 50 34 28 56 64 62 65 2a 2c 20 69 6e 74 20 61  eP4(Vdbe*, int a
654a0 64 64 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ddr, const char 
654b0 2a 7a 50 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51  *zP4, int N);.SQ
654c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
654d0 64 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65  d sqlite3VdbeUse
654e0 73 42 74 72 65 65 28 56 64 62 65 2a 2c 20 69 6e  sBtree(Vdbe*, in
654f0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
65500 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74  TE VdbeOp *sqlit
65510 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 65  e3VdbeGetOp(Vdbe
65520 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
65530 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
65540 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
65550 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
65560 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
65570 69 74 65 33 56 64 62 65 52 75 6e 4f 6e 6c 79 4f  ite3VdbeRunOnlyO
65580 6e 63 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  nce(Vdbe*);.SQLI
65590 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
655a0 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
655b0 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  e(Vdbe*);.SQLITE
655c0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
655d0 6c 69 74 65 33 56 64 62 65 43 6c 65 61 72 4f 62  lite3VdbeClearOb
655e0 6a 65 63 74 28 73 71 6c 69 74 65 33 2a 2c 56 64  ject(sqlite3*,Vd
655f0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
65600 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
65610 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56  3VdbeMakeReady(V
65620 64 62 65 2a 2c 50 61 72 73 65 2a 29 3b 0a 53 51  dbe*,Parse*);.SQ
65630 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
65640 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61   sqlite3VdbeFina
65650 6c 69 7a 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c  lize(Vdbe*);.SQL
65660 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
65670 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
65680 6c 76 65 4c 61 62 65 6c 28 56 64 62 65 2a 2c 20  lveLabel(Vdbe*, 
65690 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
656a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
656b0 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
656c0 56 64 62 65 2a 29 3b 0a 23 69 66 64 65 66 20 53  Vdbe*);.#ifdef S
656d0 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49  QLITE_DEBUG.SQLI
656e0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
656f0 20 73 71 6c 69 74 65 33 56 64 62 65 41 73 73 65   sqlite3VdbeAsse
65700 72 74 4d 61 79 41 62 6f 72 74 28 56 64 62 65 20  rtMayAbort(Vdbe 
65710 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
65720 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
65730 71 6c 69 74 65 33 56 64 62 65 54 72 61 63 65 28  qlite3VdbeTrace(
65740 56 64 62 65 2a 2c 46 49 4c 45 2a 29 3b 0a 23 65  Vdbe*,FILE*);.#e
65750 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
65760 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
65770 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73  VdbeResetStepRes
65780 75 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  ult(Vdbe*);.SQLI
65790 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
657a0 73 71 6c 69 74 65 33 56 64 62 65 52 65 77 69 6e  sqlite3VdbeRewin
657b0 64 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  d(Vdbe*);.SQLITE
657c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
657d0 69 74 65 33 56 64 62 65 52 65 73 65 74 28 56 64  ite3VdbeReset(Vd
657e0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
657f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
65800 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
65810 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  Vdbe*,int);.SQLI
65820 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
65830 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
65840 4e 61 6d 65 28 56 64 62 65 2a 2c 20 69 6e 74 2c  Name(Vdbe*, int,
65850 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72   int, const char
65860 20 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64   *, void(*)(void
65870 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  *));.SQLITE_PRIV
65880 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
65890 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73  VdbeCountChanges
658a0 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
658b0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 20  PRIVATE sqlite3 
658c0 2a 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 56  *sqlite3VdbeDb(V
658d0 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
658e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
658f0 65 33 56 64 62 65 53 65 74 53 71 6c 28 56 64 62  e3VdbeSetSql(Vdb
65900 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  e*, const char *
65910 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 29 3b 0a  z, int n, int);.
65920 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
65930 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53  oid sqlite3VdbeS
65940 77 61 70 28 56 64 62 65 2a 2c 56 64 62 65 2a 29  wap(Vdbe*,Vdbe*)
65950 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
65960 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33   VdbeOp *sqlite3
65970 56 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79 28  VdbeTakeOpArray(
65980 56 64 62 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74  Vdbe*, int*, int
65990 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
659a0 54 45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  TE sqlite3_value
659b0 20 2a 73 71 6c 69 74 65 33 56 64 62 65 47 65 74   *sqlite3VdbeGet
659c0 56 61 6c 75 65 28 56 64 62 65 2a 2c 20 69 6e 74  Value(Vdbe*, int
659d0 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52  , u8);.SQLITE_PR
659e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
659f0 65 33 56 64 62 65 53 65 74 56 61 72 6d 61 73 6b  e3VdbeSetVarmask
65a00 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 23 69  (Vdbe*, int);.#i
65a10 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
65a20 54 5f 54 52 41 43 45 0a 53 51 4c 49 54 45 5f 50  T_TRACE.SQLITE_P
65a30 52 49 56 41 54 45 20 20 20 63 68 61 72 20 2a 73  RIVATE   char *s
65a40 71 6c 69 74 65 33 56 64 62 65 45 78 70 61 6e 64  qlite3VdbeExpand
65a50 53 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74  Sql(Vdbe*, const
65a60 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a   char*);.#endif.
65a70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
65a80 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
65a90 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 4b 65 79  RecordUnpack(Key
65aa0 49 6e 66 6f 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  Info*,int,const 
65ab0 76 6f 69 64 2a 2c 55 6e 70 61 63 6b 65 64 52 65  void*,UnpackedRe
65ac0 63 6f 72 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  cord*);.SQLITE_P
65ad0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
65ae0 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70  e3VdbeRecordComp
65af0 61 72 65 28 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  are(int,const vo
65b00 69 64 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f  id*,UnpackedReco
65b10 72 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  rd*);.SQLITE_PRI
65b20 56 41 54 45 20 55 6e 70 61 63 6b 65 64 52 65 63  VATE UnpackedRec
65b30 6f 72 64 20 2a 73 71 6c 69 74 65 33 56 64 62 65  ord *sqlite3Vdbe
65b40 41 6c 6c 6f 63 55 6e 70 61 63 6b 65 64 52 65 63  AllocUnpackedRec
65b50 6f 72 64 28 4b 65 79 49 6e 66 6f 20 2a 2c 20 63  ord(KeyInfo *, c
65b60 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72  har *, int, char
65b70 20 2a 2a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53   **);..#ifndef S
65b80 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
65b90 45 52 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ER.SQLITE_PRIVAT
65ba0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
65bb0 62 65 4c 69 6e 6b 53 75 62 50 72 6f 67 72 61 6d  beLinkSubProgram
65bc0 28 56 64 62 65 20 2a 2c 20 53 75 62 50 72 6f 67  (Vdbe *, SubProg
65bd0 72 61 6d 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a  ram *);.#endif..
65be0 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
65bf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
65c00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
65c10 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20  eComment(Vdbe*, 
65c20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e  const char*, ...
65c30 29 3b 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65  );.# define Vdbe
65c40 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69  Comment(X)  sqli
65c50 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 58  te3VdbeComment X
65c60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
65c70 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64    void sqlite3Vd
65c80 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64  beNoopComment(Vd
65c90 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  be*, const char*
65ca0 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65  , ...);.# define
65cb0 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
65cc0 28 58 29 20 20 73 71 6c 69 74 65 33 56 64 62 65  (X)  sqlite3Vdbe
65cd0 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23 65  NoopComment X.#e
65ce0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 64 62  lse.# define Vdb
65cf0 65 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64 65  eComment(X).# de
65d00 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d  fine VdbeNoopCom
65d10 6d 65 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a  ment(X).#endif..
65d20 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
65d30 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76  ******* End of v
65d40 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dbe.h **********
65d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
65d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
65d70 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
65d80 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
65d90 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
65da0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
65db0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
65dc0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
65dd0 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
65de0 70 61 67 65 72 2e 68 20 69 6e 20 74 68 65 20 6d  pager.h in the m
65df0 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49  iddle of sqliteI
65e00 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
65e10 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
65e20 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
65e30 6c 65 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a  le pager.h *****
65e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
65e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
65e60 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
65e70 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
65e80 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
65e90 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
65ea0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
65eb0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
65ec0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
65ed0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
65ee0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
65ef0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
65f00 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
65f10 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
65f20 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
65f30 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
65f40 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
65f50 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
65f60 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
65f70 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
65f80 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
65f90 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
65fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
65fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
65fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
65fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
65fe0 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69  * This header fi
65ff0 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69  le defines the i
66000 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68  nterface that th
66010 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 61  e sqlite page ca
66020 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d  che.** subsystem
66030 2e 20 20 54 68 65 20 70 61 67 65 20 63 61 63 68  .  The page cach
66040 65 20 73 75 62 73 79 73 74 65 6d 20 72 65 61 64  e subsystem read
66050 73 20 61 6e 64 20 77 72 69 74 65 73 20 61 20 66  s and writes a f
66060 69 6c 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 74  ile a page.** at
66070 20 61 20 74 69 6d 65 20 61 6e 64 20 70 72 6f 76   a time and prov
66080 69 64 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 66  ides a journal f
66090 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a  or rollback..*/.
660a0 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 45 52 5f  .#ifndef _PAGER_
660b0 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 41 47 45  H_.#define _PAGE
660c0 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61  R_H_../*.** Defa
660d0 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65  ult maximum size
660e0 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20   for persistent 
660f0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 20 41  journal files. A
66100 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a 20 76 61   negative .** va
66110 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 6c 69 6d  lue means no lim
66120 69 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 6d  it. This value m
66130 61 79 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e  ay be overridden
66140 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 73   using the .** s
66150 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
66160 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 20 41 50  alSizeLimit() AP
66170 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 50 52 41  I. See also "PRA
66180 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65  GMA journal_size
66190 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 69 66 6e  _limit"..*/.#ifn
661a0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  def SQLITE_DEFAU
661b0 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f  LT_JOURNAL_SIZE_
661c0 4c 49 4d 49 54 0a 20 20 23 64 65 66 69 6e 65 20  LIMIT.  #define 
661d0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a  SQLITE_DEFAULT_J
661e0 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49  OURNAL_SIZE_LIMI
661f0 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  T -1.#endif../*.
66200 2a 2a 20 54 68 65 20 74 79 70 65 20 75 73 65 64  ** The type used
66210 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20   to represent a 
66220 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 68  page number.  Th
66230 65 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20  e first page in 
66240 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 63 61 6c  a file.** is cal
66250 6c 65 64 20 70 61 67 65 20 31 2e 20 20 30 20 69  led page 1.  0 i
66260 73 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73  s used to repres
66270 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 67 65 22  ent "not a page"
66280 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 33 32  ..*/.typedef u32
66290 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61   Pgno;../*.** Ea
662a0 63 68 20 6f 70 65 6e 20 66 69 6c 65 20 69 73 20  ch open file is 
662b0 6d 61 6e 61 67 65 64 20 62 79 20 61 20 73 65 70  managed by a sep
662c0 61 72 61 74 65 20 69 6e 73 74 61 6e 63 65 20 6f  arate instance o
662d0 66 20 74 68 65 20 22 50 61 67 65 72 22 20 73 74  f the "Pager" st
662e0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65  ructure..*/.type
662f0 64 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72  def struct Pager
66300 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 48   Pager;../*.** H
66310 61 6e 64 6c 65 20 74 79 70 65 20 66 6f 72 20 70  andle type for p
66320 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ages..*/.typedef
66330 20 73 74 72 75 63 74 20 50 67 48 64 72 20 44 62   struct PgHdr Db
66340 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 50 61 67  Page;../*.** Pag
66350 65 20 6e 75 6d 62 65 72 20 50 41 47 45 52 5f 4d  e number PAGER_M
66360 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 72 20  J_PGNO is never 
66370 75 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 69 74  used in an SQLit
66380 65 20 64 61 74 61 62 61 73 65 20 28 69 74 20 69  e database (it i
66390 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 66 6f  s.** reserved fo
663a0 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 6e 64  r working around
663b0 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 69 78   a windows/posix
663c0 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79   incompatibility
663d0 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64  ). It is.** used
663e0 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
663f0 74 6f 20 73 69 67 6e 69 66 79 20 74 68 61 74 20  to signify that 
66400 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66  the remainder of
66410 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
66420 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 65 64  e .** is devoted
66430 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 6d 61   to storing a ma
66440 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster journal nam
66450 65 20 2d 20 74 68 65 72 65 20 61 72 65 20 6e 6f  e - there are no
66460 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 0a 2a   more pages to.*
66470 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 65 65  * roll back. See
66480 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 66 75   comments for fu
66490 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 73 74  nction writeMast
664a0 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69 6e 20 70  erJournal() in p
664b0 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f 72 20 64  ager.c .** for d
664c0 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69  etails..*/.#defi
664d0 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  ne PAGER_MJ_PGNO
664e0 28 78 29 20 28 28 50 67 6e 6f 29 28 28 50 45 4e  (x) ((Pgno)((PEN
664f0 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 2d 3e  DING_BYTE/((x)->
66500 70 61 67 65 53 69 7a 65 29 29 2b 31 29 29 0a 0a  pageSize))+1))..
66510 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
66520 6c 75 65 73 20 66 6f 72 20 74 68 65 20 66 6c 61  lues for the fla
66530 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  gs parameter to 
66540 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
66550 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20  ()..**.** NOTE: 
66560 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73  These values mus
66570 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72  t match the corr
66580 65 73 70 6f 6e 64 69 6e 67 20 42 54 52 45 45 5f  esponding BTREE_
66590 20 76 61 6c 75 65 73 20 69 6e 20 62 74 72 65 65   values in btree
665a0 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  .h..*/.#define P
665b0 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41  AGER_OMIT_JOURNA
665c0 4c 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20  L  0x0001    /* 
665d0 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 72 6f 6c  Do not use a rol
665e0 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f  lback journal */
665f0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4d  .#define PAGER_M
66600 45 4d 4f 52 59 20 20 20 20 20 20 20 20 30 78 30  EMORY        0x0
66610 30 30 32 20 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d  002    /* In-mem
66620 6f 72 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  ory database */.
66630 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c  ./*.** Valid val
66640 75 65 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f  ues for the seco
66650 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73  nd argument to s
66660 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69  qlite3PagerLocki
66670 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65  ngMode()..*/.#de
66680 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49  fine PAGER_LOCKI
66690 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20  NGMODE_QUERY    
666a0 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47    -1.#define PAG
666b0 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e  ER_LOCKINGMODE_N
666c0 4f 52 4d 41 4c 20 20 20 20 20 20 30 0a 23 64 65  ORMAL      0.#de
666d0 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49  fine PAGER_LOCKI
666e0 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
666f0 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 65     1../*.** Nume
66700 72 69 63 20 63 6f 6e 73 74 61 6e 74 73 20 74 68  ric constants th
66710 61 74 20 65 6e 63 6f 64 65 20 74 68 65 20 6a 6f  at encode the jo
66720 75 72 6e 61 6c 6d 6f 64 65 2e 20 20 0a 2a 2f 0a  urnalmode.  .*/.
66730 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f  #define PAGER_JO
66740 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20  URNALMODE_QUERY 
66750 20 20 20 20 28 2d 31 29 20 20 2f 2a 20 51 75 65      (-1)  /* Que
66760 72 79 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ry the value of 
66770 6a 6f 75 72 6e 61 6c 6d 6f 64 65 20 2a 2f 0a 23  journalmode */.#
66780 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55  define PAGER_JOU
66790 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20  RNALMODE_DELETE 
667a0 20 20 20 20 20 30 20 20 20 2f 2a 20 43 6f 6d 6d       0   /* Comm
667b0 69 74 20 62 79 20 64 65 6c 65 74 69 6e 67 20 6a  it by deleting j
667c0 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 23  ournal file */.#
667d0 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55  define PAGER_JOU
667e0 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54  RNALMODE_PERSIST
667f0 20 20 20 20 20 31 20 20 20 2f 2a 20 43 6f 6d 6d       1   /* Comm
66800 69 74 20 62 79 20 7a 65 72 6f 69 6e 67 20 6a 6f  it by zeroing jo
66810 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a  urnal header */.
66820 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f  #define PAGER_JO
66830 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 20 20  URNALMODE_OFF   
66840 20 20 20 20 20 20 32 20 20 20 2f 2a 20 4a 6f 75        2   /* Jou
66850 72 6e 61 6c 20 6f 6d 69 74 74 65 64 2e 20 20 2a  rnal omitted.  *
66860 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  /.#define PAGER_
66870 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e  JOURNALMODE_TRUN
66880 43 41 54 45 20 20 20 20 33 20 20 20 2f 2a 20 43  CATE    3   /* C
66890 6f 6d 6d 69 74 20 62 79 20 74 72 75 6e 63 61 74  ommit by truncat
668a0 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23  ing journal */.#
668b0 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55  define PAGER_JOU
668c0 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20  RNALMODE_MEMORY 
668d0 20 20 20 20 20 34 20 20 20 2f 2a 20 49 6e 2d 6d       4   /* In-m
668e0 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69  emory journal fi
668f0 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  le */.#define PA
66900 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
66910 57 41 4c 20 20 20 20 20 20 20 20 20 35 20 20 20  WAL         5   
66920 2f 2a 20 55 73 65 20 77 72 69 74 65 2d 61 68 65  /* Use write-ahe
66930 61 64 20 6c 6f 67 67 69 6e 67 20 2a 2f 0a 0a 2f  ad logging */../
66940 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 64  *.** The remaind
66950 65 72 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20  er of this file 
66960 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 65 63  contains the dec
66970 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  larations of the
66980 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74 68   functions.** th
66990 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20 50  at make up the P
669a0 61 67 65 72 20 73 75 62 2d 73 79 73 74 65 6d 20  ager sub-system 
669b0 41 50 49 2e 20 53 65 65 20 73 6f 75 72 63 65 20  API. See source 
669c0 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f  code comments fo
669d0 72 20 0a 2a 2a 20 61 20 64 65 74 61 69 6c 65 64  r .** a detailed
669e0 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20   description of 
669f0 65 61 63 68 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f  each routine..*/
66a00 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 20 63 6c  ../* Open and cl
66a10 6f 73 65 20 61 20 50 61 67 65 72 20 63 6f 6e 6e  ose a Pager conn
66a20 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a 53 51 4c 49  ection. */ .SQLI
66a30 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
66a40 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28  qlite3PagerOpen(
66a50 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  .  sqlite3_vfs*,
66a60 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67  .  Pager **ppPag
66a70 65 72 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  er,.  const char
66a80 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20 69 6e 74 2c  *,.  int,.  int,
66a90 0a 20 20 69 6e 74 2c 0a 20 20 76 6f 69 64 28 2a  .  int,.  void(*
66aa0 29 28 44 62 50 61 67 65 2a 29 0a 29 3b 0a 53 51  )(DbPage*).);.SQ
66ab0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
66ac0 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f   sqlite3PagerClo
66ad0 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  se(Pager *pPager
66ae0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
66af0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
66b00 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72  erReadFileheader
66b10 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20 75 6e  (Pager*, int, un
66b20 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 0a  signed char*);..
66b30 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 65  /* Functions use
66b40 64 20 74 6f 20 63 6f 6e 66 69 67 75 72 65 20 61  d to configure a
66b50 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 2a   Pager object. *
66b60 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
66b70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
66b80 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72  erSetBusyhandler
66b90 28 50 61 67 65 72 2a 2c 20 69 6e 74 28 2a 29 28  (Pager*, int(*)(
66ba0 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 20 2a 29  void *), void *)
66bb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
66bc0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
66bd0 72 53 65 74 50 61 67 65 73 69 7a 65 28 50 61 67  rSetPagesize(Pag
66be0 65 72 2a 2c 20 75 33 32 2a 2c 20 69 6e 74 29 3b  er*, u32*, int);
66bf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
66c00 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
66c10 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67  MaxPageCount(Pag
66c20 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  er*, int);.SQLIT
66c30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
66c40 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61  qlite3PagerSetCa
66c50 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a 2c 20  chesize(Pager*, 
66c60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
66c70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
66c80 33 50 61 67 65 72 53 68 72 69 6e 6b 28 50 61 67  3PagerShrink(Pag
66c90 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
66ca0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
66cb0 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c  3PagerSetSafetyL
66cc0 65 76 65 6c 28 50 61 67 65 72 2a 2c 69 6e 74 2c  evel(Pager*,int,
66cd0 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
66ce0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
66cf0 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67  ite3PagerLocking
66d00 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e  Mode(Pager *, in
66d10 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
66d20 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
66d30 67 65 72 53 65 74 4a 6f 75 72 6e 61 6c 4d 6f 64  gerSetJournalMod
66d40 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b  e(Pager *, int);
66d50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
66d60 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
66d70 47 65 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50  GetJournalMode(P
66d80 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
66d90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
66da0 65 33 50 61 67 65 72 4f 6b 54 6f 43 68 61 6e 67  e3PagerOkToChang
66db0 65 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67  eJournalMode(Pag
66dc0 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
66dd0 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33  VATE i64 sqlite3
66de0 50 61 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65  PagerJournalSize
66df0 4c 69 6d 69 74 28 50 61 67 65 72 20 2a 2c 20 69  Limit(Pager *, i
66e00 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  64);.SQLITE_PRIV
66e10 41 54 45 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  ATE sqlite3_back
66e20 75 70 20 2a 2a 73 71 6c 69 74 65 33 50 61 67 65  up **sqlite3Page
66e30 72 42 61 63 6b 75 70 50 74 72 28 50 61 67 65 72  rBackupPtr(Pager
66e40 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e  *);../* Function
66e50 73 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e  s used to obtain
66e60 20 61 6e 64 20 72 65 6c 65 61 73 65 20 70 61 67   and release pag
66e70 65 20 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f  e references. */
66e80 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45   .SQLITE_PRIVATE
66e90 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
66ea0 72 41 63 71 75 69 72 65 28 50 61 67 65 72 20 2a  rAcquire(Pager *
66eb0 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e  pPager, Pgno pgn
66ec0 6f 2c 20 44 62 50 61 67 65 20 2a 2a 70 70 50 61  o, DbPage **ppPa
66ed0 67 65 2c 20 69 6e 74 20 63 6c 72 46 6c 61 67 29  ge, int clrFlag)
66ee0 3b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ;.#define sqlite
66ef0 33 50 61 67 65 72 47 65 74 28 41 2c 42 2c 43 29  3PagerGet(A,B,C)
66f00 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71   sqlite3PagerAcq
66f10 75 69 72 65 28 41 2c 42 2c 43 2c 30 29 0a 53 51  uire(A,B,C,0).SQ
66f20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 44 62 50  LITE_PRIVATE DbP
66f30 61 67 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65  age *sqlite3Page
66f40 72 4c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70  rLookup(Pager *p
66f50 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f  Pager, Pgno pgno
66f60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
66f70 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
66f80 67 65 72 52 65 66 28 44 62 50 61 67 65 2a 29 3b  gerRef(DbPage*);
66f90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
66fa0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
66fb0 72 55 6e 72 65 66 28 44 62 50 61 67 65 2a 29 3b  rUnref(DbPage*);
66fc0 0a 0a 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 73 20  ../* Operations 
66fd0 6f 6e 20 70 61 67 65 20 72 65 66 65 72 65 6e 63  on page referenc
66fe0 65 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es. */.SQLITE_PR
66ff0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
67000 33 50 61 67 65 72 57 72 69 74 65 28 44 62 50 61  3PagerWrite(DbPa
67010 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ge*);.SQLITE_PRI
67020 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
67030 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28  3PagerDontWrite(
67040 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45  DbPage*);.SQLITE
67050 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
67060 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67  ite3PagerMovepag
67070 65 28 50 61 67 65 72 2a 2c 44 62 50 61 67 65 2a  e(Pager*,DbPage*
67080 2c 50 67 6e 6f 2c 69 6e 74 29 3b 0a 53 51 4c 49  ,Pgno,int);.SQLI
67090 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
670a0 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52  qlite3PagerPageR
670b0 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65 2a 29  efcount(DbPage*)
670c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
670d0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61   void *sqlite3Pa
670e0 67 65 72 47 65 74 44 61 74 61 28 44 62 50 61 67  gerGetData(DbPag
670f0 65 20 2a 29 3b 20 0a 53 51 4c 49 54 45 5f 50 52  e *); .SQLITE_PR
67100 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
67110 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61  te3PagerGetExtra
67120 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 0a 2f 2a  (DbPage *); ../*
67130 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20   Functions used 
67140 74 6f 20 6d 61 6e 61 67 65 20 70 61 67 65 72 20  to manage pager 
67150 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 6e 64  transactions and
67160 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 2a 2f 0a   savepoints. */.
67170 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
67180 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
67190 50 61 67 65 63 6f 75 6e 74 28 50 61 67 65 72 2a  Pagecount(Pager*
671a0 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  , int*);.SQLITE_
671b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
671c0 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 50 61  te3PagerBegin(Pa
671d0 67 65 72 2a 2c 20 69 6e 74 20 65 78 46 6c 61 67  ger*, int exFlag
671e0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
671f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
67200 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61  e3PagerCommitPha
67210 73 65 4f 6e 65 28 50 61 67 65 72 2a 2c 63 6f 6e  seOne(Pager*,con
67220 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72  st char *zMaster
67230 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
67240 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
67250 65 33 50 61 67 65 72 45 78 63 6c 75 73 69 76 65  e3PagerExclusive
67260 4c 6f 63 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51  Lock(Pager*);.SQ
67270 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
67280 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e   sqlite3PagerSyn
67290 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  c(Pager *pPager)
672a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
672b0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
672c0 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  rCommitPhaseTwo(
672d0 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
672e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
672f0 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
67300 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
67310 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
67320 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 76  ite3PagerOpenSav
67330 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50  epoint(Pager *pP
67340 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 51  ager, int n);.SQ
67350 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
67360 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76   sqlite3PagerSav
67370 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50  epoint(Pager *pP
67380 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  ager, int op, in
67390 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 53  t iSavepoint);.S
673a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
673b0 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 68  t sqlite3PagerSh
673c0 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a  aredLock(Pager *
673d0 70 50 61 67 65 72 29 3b 0a 0a 23 69 66 6e 64 65  pPager);..#ifnde
673e0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41  f SQLITE_OMIT_WA
673f0 4c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  L.SQLITE_PRIVATE
67400 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61     int sqlite3Pa
67410 67 65 72 43 68 65 63 6b 70 6f 69 6e 74 28 50 61  gerCheckpoint(Pa
67420 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
67430 2c 20 69 6e 74 2a 2c 20 69 6e 74 2a 29 3b 0a 53  , int*, int*);.S
67440 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
67450 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
67460 57 61 6c 53 75 70 70 6f 72 74 65 64 28 50 61 67  WalSupported(Pag
67470 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 53 51 4c  er *pPager);.SQL
67480 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
67490 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 61  t sqlite3PagerWa
674a0 6c 43 61 6c 6c 62 61 63 6b 28 50 61 67 65 72 20  lCallback(Pager 
674b0 2a 70 50 61 67 65 72 29 3b 0a 53 51 4c 49 54 45  *pPager);.SQLITE
674c0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
674d0 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 57  qlite3PagerOpenW
674e0 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  al(Pager *pPager
674f0 2c 20 69 6e 74 20 2a 70 69 73 4f 70 65 6e 29 3b  , int *pisOpen);
67500 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
67510 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67    int sqlite3Pag
67520 65 72 43 6c 6f 73 65 57 61 6c 28 50 61 67 65 72  erCloseWal(Pager
67530 20 2a 70 50 61 67 65 72 29 3b 0a 23 65 6e 64 69   *pPager);.#endi
67540 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
67550 5f 45 4e 41 42 4c 45 5f 5a 49 50 56 46 53 0a 53  _ENABLE_ZIPVFS.S
67560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
67570 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
67580 57 61 6c 46 72 61 6d 65 73 69 7a 65 28 50 61 67  WalFramesize(Pag
67590 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 23 65 6e  er *pPager);.#en
675a0 64 69 66 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e  dif../* Function
675b0 73 20 75 73 65 64 20 74 6f 20 71 75 65 72 79 20  s used to query 
675c0 70 61 67 65 72 20 73 74 61 74 65 20 61 6e 64 20  pager state and 
675d0 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e 20 2a  configuration. *
675e0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
675f0 20 75 38 20 73 71 6c 69 74 65 33 50 61 67 65 72   u8 sqlite3Pager
67600 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67 65 72  Isreadonly(Pager
67610 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
67620 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
67630 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61 67 65  gerRefcount(Page
67640 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
67650 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
67660 61 67 65 72 4d 65 6d 55 73 65 64 28 50 61 67 65  agerMemUsed(Page
67670 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
67680 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
67690 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65  sqlite3PagerFile
676a0 6e 61 6d 65 28 50 61 67 65 72 2a 2c 20 69 6e 74  name(Pager*, int
676b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
676c0 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  E const sqlite3_
676d0 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65  vfs *sqlite3Page
676e0 72 56 66 73 28 50 61 67 65 72 2a 29 3b 0a 53 51  rVfs(Pager*);.SQ
676f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
67700 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74  ite3_file *sqlit
67710 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67 65  e3PagerFile(Page
67720 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
67730 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
67740 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72  sqlite3PagerJour
67750 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b  nalname(Pager*);
67760 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
67770 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
67780 4e 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b 0a  Nosync(Pager*);.
67790 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
677a0 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65  oid *sqlite3Page
677b0 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 72  rTempSpace(Pager
677c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
677d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
677e0 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72  gerIsMemdb(Pager
677f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
67800 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
67810 61 67 65 72 43 61 63 68 65 53 74 61 74 28 50 61  agerCacheStat(Pa
67820 67 65 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  ger *, int, int,
67830 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f   int *);.SQLITE_
67840 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
67850 69 74 65 33 50 61 67 65 72 43 6c 65 61 72 43 61  ite3PagerClearCa
67860 63 68 65 28 50 61 67 65 72 20 2a 29 3b 0a 53 51  che(Pager *);.SQ
67870 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
67880 20 73 71 6c 69 74 65 33 53 65 63 74 6f 72 53 69   sqlite3SectorSi
67890 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ze(sqlite3_file 
678a0 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e  *);../* Function
678b0 73 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63 61  s used to trunca
678c0 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  te the database 
678d0 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f  file. */.SQLITE_
678e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
678f0 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
67900 65 49 6d 61 67 65 28 50 61 67 65 72 2a 2c 50 67  eImage(Pager*,Pg
67910 6e 6f 29 3b 0a 0a 23 69 66 20 64 65 66 69 6e 65  no);..#if define
67920 64 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  d(SQLITE_HAS_COD
67930 45 43 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  EC) && !defined(
67940 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 29  SQLITE_OMIT_WAL)
67950 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
67960 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
67970 65 72 43 6f 64 65 63 28 44 62 50 61 67 65 20 2a  erCodec(DbPage *
67980 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 75  );.#endif../* Fu
67990 6e 63 74 69 6f 6e 73 20 74 6f 20 73 75 70 70 6f  nctions to suppo
679a0 72 74 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64  rt testing and d
679b0 65 62 75 67 67 69 6e 67 2e 20 2a 2f 0a 23 69 66  ebugging. */.#if
679c0 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
679d0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
679e0 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54 45  ITE_TEST).SQLITE
679f0 5f 50 52 49 56 41 54 45 20 20 20 50 67 6e 6f 20  _PRIVATE   Pgno 
67a00 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
67a10 6e 75 6d 62 65 72 28 44 62 50 61 67 65 2a 29 3b  number(DbPage*);
67a20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
67a30 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67    int sqlite3Pag
67a40 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 44 62  erIswriteable(Db
67a50 50 61 67 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 23  Page*);.#endif.#
67a60 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
67a70 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
67a80 20 20 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 50     int *sqlite3P
67a90 61 67 65 72 53 74 61 74 73 28 50 61 67 65 72 2a  agerStats(Pager*
67aa0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
67ab0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
67ac0 50 61 67 65 72 52 65 66 64 75 6d 70 28 50 61 67  PagerRefdump(Pag
67ad0 65 72 2a 29 3b 0a 20 20 76 6f 69 64 20 64 69 73  er*);.  void dis
67ae0 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69  able_simulated_i
67af0 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a  o_errors(void);.
67b00 20 20 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69    void enable_si
67b10 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
67b20 73 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 23  s(void);.#else.#
67b30 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f   define disable_
67b40 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
67b50 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65  ors().# define e
67b60 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  nable_simulated_
67b70 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64  io_errors().#end
67b80 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50  if..#endif /* _P
67b90 41 47 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a  AGER_H_ */../***
67ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
67bb0 6f 66 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a  of pager.h *****
67bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
67bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
67c00 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
67c10 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
67c20 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
67c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
67c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
67c50 75 64 65 20 70 63 61 63 68 65 2e 68 20 69 6e 20  ude pcache.h in 
67c60 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71  the middle of sq
67c70 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
67c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
67c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
67ca0 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e 68 20  n file pcache.h 
67cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
67ce0 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30 35  * 2008 August 05
67cf0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
67d00 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
67d10 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
67d20 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
67d30 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
67d40 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
67d50 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
67d60 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
67d70 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
67d80 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
67d90 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
67da0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
67db0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
67dc0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
67dd0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
67de0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
67df0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
67e00 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
67e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
67e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
67e50 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
67e60 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20  ile defines the 
67e70 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74  interface that t
67e80 68 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63  he sqlite page c
67e90 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65  ache.** subsyste
67ea0 6d 2e 20 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  m. .*/..#ifndef 
67eb0 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79 70 65  _PCACHE_H_..type
67ec0 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72  def struct PgHdr
67ed0 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65 66 20   PgHdr;.typedef 
67ee0 73 74 72 75 63 74 20 50 43 61 63 68 65 20 50 43  struct PCache PC
67ef0 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76 65  ache;../*.** Eve
67f00 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63  ry page in the c
67f10 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c  ache is controll
67f20 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63  ed by an instanc
67f30 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
67f40 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  ng.** structure.
67f50 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 64 72  .*/.struct PgHdr
67f60 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61   {.  sqlite3_pca
67f70 63 68 65 5f 70 61 67 65 20 2a 70 50 61 67 65 3b  che_page *pPage;
67f80 20 20 20 20 2f 2a 20 50 63 61 63 68 65 20 6f 62      /* Pcache ob
67f90 6a 65 63 74 20 70 61 67 65 20 68 61 6e 64 6c 65  ject page handle
67fa0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 44 61 74   */.  void *pDat
67fb0 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a;              
67fc0 20 20 20 20 20 2f 2a 20 50 61 67 65 20 64 61 74       /* Page dat
67fd0 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 78  a */.  void *pEx
67fe0 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  tra;            
67ff0 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 63        /* Extra c
68000 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 50 67 48 64  ontent */.  PgHd
68010 72 20 2a 70 44 69 72 74 79 3b 20 20 20 20 20 20  r *pDirty;      
68020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
68030 61 6e 73 69 65 6e 74 20 6c 69 73 74 20 6f 66 20  ansient list of 
68040 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 20  dirty pages */. 
68050 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20   Pager *pPager; 
68060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68070 2f 2a 20 54 68 65 20 70 61 67 65 72 20 74 68 69  /* The pager thi
68080 73 20 70 61 67 65 20 69 73 20 70 61 72 74 20 6f  s page is part o
68090 66 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  f */.  Pgno pgno
680a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
680b0 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
680c0 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 70 61  mber for this pa
680d0 67 65 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  ge */.#ifdef SQL
680e0 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a  ITE_CHECK_PAGES.
680f0 20 20 75 33 32 20 70 61 67 65 48 61 73 68 3b 20    u32 pageHash; 
68100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68110 20 2f 2a 20 48 61 73 68 20 6f 66 20 70 61 67 65   /* Hash of page
68120 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 23 65 6e 64   content */.#end
68130 69 66 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20  if.  u16 flags; 
68140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68150 20 20 20 20 2f 2a 20 50 47 48 44 52 20 66 6c 61      /* PGHDR fla
68160 67 73 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77  gs defined below
68170 20 2a 2f 0a 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a   */..  /********
68180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
68190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
681a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
681b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20  **************. 
681c0 20 2a 2a 20 45 6c 65 6d 65 6e 74 73 20 61 62 6f   ** Elements abo
681d0 76 65 20 61 72 65 20 70 75 62 6c 69 63 2e 20 20  ve are public.  
681e0 41 6c 6c 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  All that follows
681f0 20 69 73 20 70 72 69 76 61 74 65 20 74 6f 20 70   is private to p
68200 63 61 63 68 65 2e 63 0a 20 20 2a 2a 20 61 6e 64  cache.c.  ** and
68210 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61   should not be a
68220 63 63 65 73 73 65 64 20 62 79 20 6f 74 68 65 72  ccessed by other
68230 20 6d 6f 64 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20   modules..  */. 
68240 20 69 31 36 20 6e 52 65 66 3b 20 20 20 20 20 20   i16 nRef;      
68250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68260 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65  /* Number of use
68270 72 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20  rs of this page 
68280 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61  */.  PCache *pCa
68290 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  che;            
682a0 20 20 20 20 2f 2a 20 43 61 63 68 65 20 74 68 61      /* Cache tha
682b0 74 20 6f 77 6e 73 20 74 68 69 73 20 70 61 67 65  t owns this page
682c0 20 2a 2f 0a 0a 20 20 50 67 48 64 72 20 2a 70 44   */..  PgHdr *pD
682d0 69 72 74 79 4e 65 78 74 3b 20 20 20 20 20 20 20  irtyNext;       
682e0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6c        /* Next el
682f0 65 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20 6f 66  ement in list of
68300 20 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a   dirty pages */.
68310 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 50    PgHdr *pDirtyP
68320 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  rev;            
68330 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 65 6c 65   /* Previous ele
68340 6d 65 6e 74 20 69 6e 20 6c 69 73 74 20 6f 66 20  ment in list of 
68350 64 69 72 74 79 20 70 61 67 65 73 20 2a 2f 0a 7d  dirty pages */.}
68360 3b 0a 0a 2f 2a 20 42 69 74 20 76 61 6c 75 65 73  ;../* Bit values
68370 20 66 6f 72 20 50 67 48 64 72 2e 66 6c 61 67 73   for PgHdr.flags
68380 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44   */.#define PGHD
68390 52 5f 44 49 52 54 59 20 20 20 20 20 20 20 20 20  R_DIRTY         
683a0 20 20 20 20 30 78 30 30 32 20 20 2f 2a 20 50 61      0x002  /* Pa
683b0 67 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 2a  ge has changed *
683c0 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f  /.#define PGHDR_
683d0 4e 45 45 44 5f 53 59 4e 43 20 20 20 20 20 20 20  NEED_SYNC       
683e0 20 20 30 78 30 30 34 20 20 2f 2a 20 46 73 79 6e    0x004  /* Fsyn
683f0 63 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  c the rollback j
68400 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 0a 20 20  ournal before.  
68410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68430 20 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20       ** writing 
68440 74 68 69 73 20 70 61 67 65 20 74 6f 20 74 68 65  this page to the
68450 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65   database */.#de
68460 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f  fine PGHDR_NEED_
68470 52 45 41 44 20 20 20 20 20 20 20 20 20 30 78 30  READ         0x0
68480 30 38 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 69  08  /* Content i
68490 73 20 75 6e 72 65 61 64 20 2a 2f 0a 23 64 65 66  s unread */.#def
684a0 69 6e 65 20 50 47 48 44 52 5f 52 45 55 53 45 5f  ine PGHDR_REUSE_
684b0 55 4e 4c 49 4b 45 4c 59 20 20 20 20 30 78 30 31  UNLIKELY    0x01
684c0 30 20 20 2f 2a 20 41 20 68 69 6e 74 20 74 68 61  0  /* A hint tha
684d0 74 20 72 65 75 73 65 20 69 73 20 75 6e 6c 69 6b  t reuse is unlik
684e0 65 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ely */.#define P
684f0 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20  GHDR_DONT_WRITE 
68500 20 20 20 20 20 20 20 30 78 30 32 30 20 20 2f 2a         0x020  /*
68510 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 63 6f   Do not write co
68520 6e 74 65 6e 74 20 74 6f 20 64 69 73 6b 20 2a 2f  ntent to disk */
68530 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  ../* Initialize 
68540 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 65  and shutdown the
68550 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73   page cache subs
68560 79 73 74 65 6d 20 2a 2f 0a 53 51 4c 49 54 45 5f  ystem */.SQLITE_
68570 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
68580 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c  te3PcacheInitial
68590 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  ize(void);.SQLIT
685a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
685b0 71 6c 69 74 65 33 50 63 61 63 68 65 53 68 75 74  qlite3PcacheShut
685c0 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20  down(void);../* 
685d0 50 61 67 65 20 63 61 63 68 65 20 62 75 66 66 65  Page cache buffe
685e0 72 20 6d 61 6e 61 67 65 6d 65 6e 74 3a 0a 2a 2a  r management:.**
685f0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
68600 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 49 54 45  implement SQLITE
68610 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
68620 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  E..*/.SQLITE_PRI
68630 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
68640 33 50 43 61 63 68 65 42 75 66 66 65 72 53 65 74  3PCacheBufferSet
68650 75 70 28 76 6f 69 64 20 2a 2c 20 69 6e 74 20 73  up(void *, int s
68660 7a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 20 43  z, int n);../* C
68670 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65  reate a new page
68680 72 20 63 61 63 68 65 2e 0a 2a 2a 20 55 6e 64 65  r cache..** Unde
68690 72 20 6d 65 6d 6f 72 79 20 73 74 72 65 73 73 2c  r memory stress,
686a0 20 69 6e 76 6f 6b 65 20 78 53 74 72 65 73 73 20   invoke xStress 
686b0 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70  to try to make p
686c0 61 67 65 73 20 63 6c 65 61 6e 2e 0a 2a 2a 20 4f  ages clean..** O
686d0 6e 6c 79 20 63 6c 65 61 6e 20 61 6e 64 20 75 6e  nly clean and un
686e0 70 69 6e 6e 65 64 20 70 61 67 65 73 20 63 61 6e  pinned pages can
686f0 20 62 65 20 72 65 63 6c 61 69 6d 65 64 2e 0a 2a   be reclaimed..*
68700 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
68710 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
68720 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e 74 20 73  cheOpen(.  int s
68730 7a 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20  zPage,          
68740 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
68750 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20  e of every page 
68760 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61  */.  int szExtra
68770 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
68780 20 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61      /* Extra spa
68790 63 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ce associated wi
687a0 74 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  th each page */.
687b0 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65    int bPurgeable
687c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
687d0 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67 65   /* True if page
687e0 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67  s are on backing
687f0 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20   store */.  int 
68800 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64 2a  (*xStress)(void*
68810 2c 20 50 67 48 64 72 2a 29 2c 20 2f 2a 20 43 61  , PgHdr*), /* Ca
68820 6c 6c 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b  ll to try to mak
68830 65 20 70 61 67 65 73 20 63 6c 65 61 6e 20 2a 2f  e pages clean */
68840 0a 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73  .  void *pStress
68850 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
68860 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
68870 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 20 50 43   xStress */.  PC
68880 61 63 68 65 20 2a 70 54 6f 49 6e 69 74 20 20 20  ache *pToInit   
68890 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
688a0 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73 70 61  Preallocated spa
688b0 63 65 20 66 6f 72 20 74 68 65 20 50 43 61 63 68  ce for the PCach
688c0 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 4d 6f 64 69  e */.);../* Modi
688d0 66 79 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  fy the page-size
688e0 20 61 66 74 65 72 20 74 68 65 20 63 61 63 68 65   after the cache
688f0 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65   has been create
68900 64 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d. */.SQLITE_PRI
68910 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
68920 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69  3PcacheSetPageSi
68930 7a 65 28 50 43 61 63 68 65 20 2a 2c 20 69 6e 74  ze(PCache *, int
68940 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68  );../* Return th
68950 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  e size in bytes 
68960 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65  of a PCache obje
68970 63 74 2e 20 20 55 73 65 64 20 74 6f 20 70 72 65  ct.  Used to pre
68980 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 73 74 6f 72  allocate.** stor
68990 61 67 65 20 73 70 61 63 65 2e 0a 2a 2f 0a 53 51  age space..*/.SQ
689a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
689b0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 69   sqlite3PcacheSi
689c0 7a 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 4f 6e  ze(void);../* On
689d0 65 20 72 65 6c 65 61 73 65 20 70 65 72 20 73 75  e release per su
689e0 63 63 65 73 73 66 75 6c 20 66 65 74 63 68 2e 20  ccessful fetch. 
689f0 20 50 61 67 65 20 69 73 20 70 69 6e 6e 65 64 20   Page is pinned 
68a00 75 6e 74 69 6c 20 72 65 6c 65 61 73 65 64 2e 0a  until released..
68a10 2a 2a 20 52 65 66 65 72 65 6e 63 65 20 63 6f 75  ** Reference cou
68a20 6e 74 65 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45  nted. .*/.SQLITE
68a30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
68a40 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28  ite3PcacheFetch(
68a50 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 2c 20 69  PCache*, Pgno, i
68a60 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c 20 50  nt createFlag, P
68a70 67 48 64 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  gHdr**);.SQLITE_
68a80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
68a90 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73  ite3PcacheReleas
68aa0 65 28 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49  e(PgHdr*);..SQLI
68ab0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
68ac0 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f  sqlite3PcacheDro
68ad0 70 28 50 67 48 64 72 2a 29 3b 20 20 20 20 20 20  p(PgHdr*);      
68ae0 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 70 61 67     /* Remove pag
68af0 65 20 66 72 6f 6d 20 63 61 63 68 65 20 2a 2f 0a  e from cache */.
68b00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
68b10 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
68b20 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72  eMakeDirty(PgHdr
68b30 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73  *);    /* Make s
68b40 75 72 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b  ure page is mark
68b50 65 64 20 64 69 72 74 79 20 2a 2f 0a 53 51 4c 49  ed dirty */.SQLI
68b60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
68b70 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
68b80 65 43 6c 65 61 6e 28 50 67 48 64 72 2a 29 3b 20  eClean(PgHdr*); 
68b90 20 20 20 2f 2a 20 4d 61 72 6b 20 61 20 73 69 6e     /* Mark a sin
68ba0 67 6c 65 20 70 61 67 65 20 61 73 20 63 6c 65 61  gle page as clea
68bb0 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  n */.SQLITE_PRIV
68bc0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
68bd0 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50  PcacheCleanAll(P
68be0 43 61 63 68 65 2a 29 3b 20 20 20 20 2f 2a 20 4d  Cache*);    /* M
68bf0 61 72 6b 20 61 6c 6c 20 64 69 72 74 79 20 6c 69  ark all dirty li
68c00 73 74 20 70 61 67 65 73 20 61 73 20 63 6c 65 61  st pages as clea
68c10 6e 20 2a 2f 0a 0a 2f 2a 20 43 68 61 6e 67 65 20  n */../* Change 
68c20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20  a page number.  
68c30 55 73 65 64 20 62 79 20 69 6e 63 72 2d 76 61 63  Used by incr-vac
68c40 75 75 6d 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  uum. */.SQLITE_P
68c50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
68c60 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 50 67  te3PcacheMove(Pg
68c70 48 64 72 2a 2c 20 50 67 6e 6f 29 3b 0a 0a 2f 2a  Hdr*, Pgno);../*
68c80 20 52 65 6d 6f 76 65 20 61 6c 6c 20 70 61 67 65   Remove all page
68c90 73 20 77 69 74 68 20 70 67 6e 6f 3e 78 2e 20 20  s with pgno>x.  
68ca0 52 65 73 65 74 20 74 68 65 20 63 61 63 68 65 20  Reset the cache 
68cb0 69 66 20 78 3d 3d 30 20 2a 2f 0a 53 51 4c 49 54  if x==0 */.SQLIT
68cc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
68cd0 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e  qlite3PcacheTrun
68ce0 63 61 74 65 28 50 43 61 63 68 65 2a 2c 20 50 67  cate(PCache*, Pg
68cf0 6e 6f 20 78 29 3b 0a 0a 2f 2a 20 47 65 74 20 61  no x);../* Get a
68d00 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72   list of all dir
68d10 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  ty pages in the 
68d20 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79  cache, sorted by
68d30 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a   page number */.
68d40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50  SQLITE_PRIVATE P
68d50 67 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61  gHdr *sqlite3Pca
68d60 63 68 65 44 69 72 74 79 4c 69 73 74 28 50 43 61  cheDirtyList(PCa
68d70 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52 65 73 65 74  che*);../* Reset
68d80 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65 20 63   and close the c
68d90 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 53  ache object */.S
68da0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
68db0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
68dc0 43 6c 6f 73 65 28 50 43 61 63 68 65 2a 29 3b 0a  Close(PCache*);.
68dd0 0a 2f 2a 20 43 6c 65 61 72 20 66 6c 61 67 73 20  ./* Clear flags 
68de0 66 72 6f 6d 20 70 61 67 65 73 20 6f 66 20 74 68  from pages of th
68df0 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a  e page cache */.
68e00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
68e10 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
68e20 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28  eClearSyncFlags(
68e30 50 43 61 63 68 65 20 2a 29 3b 0a 0a 2f 2a 20 44  PCache *);../* D
68e40 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65  iscard the conte
68e50 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65  nts of the cache
68e60 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
68e70 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
68e80 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63 68  cacheClear(PCach
68e90 65 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20  e*);../* Return 
68ea0 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
68eb0 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   of outstanding 
68ec0 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 20  page references 
68ed0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
68ee0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
68ef0 63 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63  cheRefCount(PCac
68f00 68 65 2a 29 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d  he*);../* Increm
68f10 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63  ent the referenc
68f20 65 20 63 6f 75 6e 74 20 6f 66 20 61 6e 20 65 78  e count of an ex
68f30 69 73 74 69 6e 67 20 70 61 67 65 20 2a 2f 0a 53  isting page */.S
68f40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
68f50 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
68f60 52 65 66 28 50 67 48 64 72 2a 29 3b 0a 0a 53 51  Ref(PgHdr*);..SQ
68f70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
68f80 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61   sqlite3PcachePa
68f90 67 65 52 65 66 63 6f 75 6e 74 28 50 67 48 64 72  geRefcount(PgHdr
68fa0 2a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74  *);../* Return t
68fb0 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
68fc0 6f 66 20 70 61 67 65 73 20 73 74 6f 72 65 64 20  of pages stored 
68fd0 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a  in the cache */.
68fe0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
68ff0 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
69000 50 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65  Pagecount(PCache
69010 2a 29 3b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  *);..#if defined
69020 28 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41  (SQLITE_CHECK_PA
69030 47 45 53 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  GES) || defined(
69040 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a  SQLITE_DEBUG)./*
69050 20 49 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   Iterate through
69060 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73   all dirty pages
69070 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65   currently store
69080 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 20  d in the cache. 
69090 54 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63  This.** interfac
690a0 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  e is only availa
690b0 62 6c 65 20 69 66 20 53 51 4c 49 54 45 5f 43 48  ble if SQLITE_CH
690c0 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65 66  ECK_PAGES is def
690d0 69 6e 65 64 20 77 68 65 6e 20 74 68 65 20 0a 2a  ined when the .*
690e0 2a 20 6c 69 62 72 61 72 79 20 69 73 20 62 75 69  * library is bui
690f0 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  lt..*/.SQLITE_PR
69100 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
69110 65 33 50 63 61 63 68 65 49 74 65 72 61 74 65 44  e3PcacheIterateD
69120 69 72 74 79 28 50 43 61 63 68 65 20 2a 70 43 61  irty(PCache *pCa
69130 63 68 65 2c 20 76 6f 69 64 20 28 2a 78 49 74 65  che, void (*xIte
69140 72 29 28 50 67 48 64 72 20 2a 29 29 3b 0a 23 65  r)(PgHdr *));.#e
69150 6e 64 69 66 0a 0a 2f 2a 20 53 65 74 20 61 6e 64  ndif../* Set and
69160 20 67 65 74 20 74 68 65 20 73 75 67 67 65 73 74   get the suggest
69170 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 66 6f  ed cache-size fo
69180 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  r the specified 
69190 70 61 67 65 72 2d 63 61 63 68 65 2e 0a 2a 2a 0a  pager-cache..**.
691a0 2a 2a 20 49 66 20 6e 6f 20 67 6c 6f 62 61 6c 20  ** If no global 
691b0 6d 61 78 69 6d 75 6d 20 69 73 20 63 6f 6e 66 69  maximum is confi
691c0 67 75 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20  gured, then the 
691d0 73 79 73 74 65 6d 20 61 74 74 65 6d 70 74 73 20  system attempts 
691e0 74 6f 20 6c 69 6d 69 74 0a 2a 2a 20 74 68 65 20  to limit.** the 
691f0 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
69200 70 61 67 65 73 20 63 61 63 68 65 64 20 62 79 20  pages cached by 
69210 70 75 72 67 65 61 62 6c 65 20 70 61 67 65 72 2d  purgeable pager-
69220 63 61 63 68 65 73 20 74 6f 20 74 68 65 20 73 75  caches to the su
69230 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 73 75 67 67  m.** of the sugg
69240 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65  ested cache-size
69250 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
69260 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
69270 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65 73  3PcacheSetCaches
69280 69 7a 65 28 50 43 61 63 68 65 20 2a 2c 20 69 6e  ize(PCache *, in
69290 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  t);.#ifdef SQLIT
692a0 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  E_TEST.SQLITE_PR
692b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
692c0 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65 73  3PcacheGetCaches
692d0 69 7a 65 28 50 43 61 63 68 65 20 2a 29 3b 0a 23  ize(PCache *);.#
692e0 65 6e 64 69 66 0a 0a 2f 2a 20 46 72 65 65 20 75  endif../* Free u
692f0 70 20 61 73 20 6d 75 63 68 20 6d 65 6d 6f 72 79  p as much memory
69300 20 61 73 20 70 6f 73 73 69 62 6c 65 20 66 72 6f   as possible fro
69310 6d 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  m the page cache
69320 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
69330 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
69340 63 61 63 68 65 53 68 72 69 6e 6b 28 50 43 61 63  cacheShrink(PCac
69350 68 65 2a 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  he*);..#ifdef SQ
69360 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f  LITE_ENABLE_MEMO
69370 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a  RY_MANAGEMENT./*
69380 20 54 72 79 20 74 6f 20 72 65 74 75 72 6e 20 6d   Try to return m
69390 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 74 68  emory used by th
693a0 65 20 70 63 61 63 68 65 20 6d 6f 64 75 6c 65 20  e pcache module 
693b0 74 6f 20 74 68 65 20 6d 61 69 6e 20 6d 65 6d 6f  to the main memo
693c0 72 79 20 68 65 61 70 20 2a 2f 0a 53 51 4c 49 54  ry heap */.SQLIT
693d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
693e0 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61  lite3PcacheRelea
693f0 73 65 4d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 23  seMemory(int);.#
69400 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
69410 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
69420 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
69430 6c 69 74 65 33 50 63 61 63 68 65 53 74 61 74 73  lite3PcacheStats
69440 28 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c  (int*,int*,int*,
69450 69 6e 74 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 53  int*);.#endif..S
69460 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
69470 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 65  id sqlite3PCache
69480 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29  SetDefault(void)
69490 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50 43  ;..#endif /* _PC
694a0 41 43 48 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a  ACHE_H_ */../***
694b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
694c0 6f 66 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a  of pcache.h ****
694d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
694e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
694f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
69500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
69510 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
69520 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
69530 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
69540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a  **********/../**
69550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
69560 6c 75 64 65 20 6f 73 2e 68 20 69 6e 20 74 68 65  lude os.h in the
69570 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74   middle of sqlit
69580 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
69590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
695a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
695b0 69 6e 20 66 69 6c 65 20 6f 73 2e 68 20 2a 2a 2a  in file os.h ***
695c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
695d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
695e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
695f0 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
69600 72 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 16.**.** The a
69610 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
69620 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
69630 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
69640 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
69650 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
69660 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
69670 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
69680 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
69690 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
696a0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
696b0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
696c0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
696d0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
696e0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
696f0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
69700 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
69710 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
69720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
69730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
69740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
69750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
69760 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
69770 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20  his header file 
69780 28 74 6f 67 65 74 68 65 72 20 77 69 74 68 20 69  (together with i
69790 73 20 63 6f 6d 70 61 6e 69 6f 6e 20 43 20 73 6f  s companion C so
697a0 75 72 63 65 2d 63 6f 64 65 20 66 69 6c 65 0a 2a  urce-code file.*
697b0 2a 20 22 6f 73 2e 63 22 29 20 61 74 74 65 6d 70  * "os.c") attemp
697c0 74 20 74 6f 20 61 62 73 74 72 61 63 74 20 74 68  t to abstract th
697d0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65  e underlying ope
697e0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73 6f  rating system so
697f0 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 53 51 4c   that.** the SQL
69800 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c  ite library will
69810 20 77 6f 72 6b 20 6f 6e 20 62 6f 74 68 20 50 4f   work on both PO
69820 53 49 58 20 61 6e 64 20 77 69 6e 64 6f 77 73 20  SIX and windows 
69830 73 79 73 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54  systems..**.** T
69840 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20  his header file 
69850 69 73 20 23 69 6e 63 6c 75 64 65 2d 65 64 20 62  is #include-ed b
69860 79 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 61 6e  y sqliteInt.h an
69870 64 20 74 68 75 73 20 65 6e 64 73 20 75 70 0a 2a  d thus ends up.*
69880 2a 20 62 65 69 6e 67 20 69 6e 63 6c 75 64 65 64  * being included
69890 20 62 79 20 65 76 65 72 79 20 73 6f 75 72 63 65   by every source
698a0 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65   file..*/.#ifnde
698b0 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a  f _SQLITE_OS_H_.
698c0 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f  #define _SQLITE_
698d0 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46 69 67  OS_H_../*.** Fig
698e0 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 61 72  ure out if we ar
698f0 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 55  e dealing with U
69900 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 6f 72  nix, Windows, or
69910 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 6f   some other.** o
69920 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e  perating system.
69930 20 20 41 66 74 65 72 20 74 68 65 20 66 6f 6c 6c    After the foll
69940 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 70  owing block of p
69950 72 65 70 72 6f 63 65 73 73 20 6d 61 63 72 6f 73  reprocess macros
69960 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 51 4c 49  ,.** all of SQLI
69970 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 51 4c 49  TE_OS_UNIX, SQLI
69980 54 45 5f 4f 53 5f 57 49 4e 2c 20 61 6e 64 20 53  TE_OS_WIN, and S
69990 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20 0a  QLITE_OS_OTHER .
699a0 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 64 20  ** will defined 
699b0 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72 20 30  to either 1 or 0
699c0 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f  .  One of the fo
699d0 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 20 54  ur will be 1.  T
699e0 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 68 72  he other .** thr
699f0 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a 2a 2f  ee will be 0..*/
69a00 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
69a10 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20  ITE_OS_OTHER).# 
69a20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48  if SQLITE_OS_OTH
69a30 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 66 20  ER==1.#   undef 
69a40 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23  SQLITE_OS_UNIX.#
69a50 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
69a60 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 75  _OS_UNIX 0.#   u
69a70 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  ndef SQLITE_OS_W
69a80 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51  IN.#   define SQ
69a90 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20  LITE_OS_WIN 0.# 
69aa0 65 6c 73 65 0a 23 20 20 20 75 6e 64 65 66 20 53  else.#   undef S
69ab0 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 0a 23  QLITE_OS_OTHER.#
69ac0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69   endif.#endif.#i
69ad0 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
69ae0 45 5f 4f 53 5f 55 4e 49 58 29 20 26 26 20 21 64  E_OS_UNIX) && !d
69af0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53  efined(SQLITE_OS
69b00 5f 4f 54 48 45 52 29 0a 23 20 64 65 66 69 6e 65  _OTHER).# define
69b10 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52   SQLITE_OS_OTHER
69b20 20 30 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49   0.# ifndef SQLI
69b30 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 69 66  TE_OS_WIN.#   if
69b40 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 29   defined(_WIN32)
69b50 20 7c 7c 20 64 65 66 69 6e 65 64 28 57 49 4e 33   || defined(WIN3
69b60 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  2) || defined(__
69b70 43 59 47 57 49 4e 5f 5f 29 20 7c 7c 20 64 65 66  CYGWIN__) || def
69b80 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 33 32 5f 5f  ined(__MINGW32__
69b90 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 42  ) || defined(__B
69ba0 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 20 20 20 20  ORLANDC__).#    
69bb0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
69bc0 53 5f 57 49 4e 20 31 0a 23 20 20 20 20 20 64 65  S_WIN 1.#     de
69bd0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55  fine SQLITE_OS_U
69be0 4e 49 58 20 30 0a 23 20 20 20 65 6c 73 65 0a 23  NIX 0.#   else.#
69bf0 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49       define SQLI
69c00 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20  TE_OS_WIN 0.#   
69c10 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
69c20 4f 53 5f 55 4e 49 58 20 31 0a 23 20 20 65 6e 64  OS_UNIX 1.#  end
69c30 69 66 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66  if.# else.#  def
69c40 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e  ine SQLITE_OS_UN
69c50 49 58 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6c  IX 0.# endif.#el
69c60 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49  se.# ifndef SQLI
69c70 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 65 66  TE_OS_WIN.#  def
69c80 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  ine SQLITE_OS_WI
69c90 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64  N 0.# endif.#end
69ca0 69 66 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  if..#if SQLITE_O
69cb0 53 5f 57 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20  S_WIN.# include 
69cc0 3c 77 69 6e 64 6f 77 73 2e 68 3e 0a 23 65 6e 64  <windows.h>.#end
69cd0 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d  if../*.** Determ
69ce0 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 64 65  ine if we are de
69cf0 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64 6f  aling with Windo
69d00 77 73 20 4e 54 2e 0a 2a 2a 0a 2a 2a 20 57 65 20  ws NT..**.** We 
69d10 6f 75 67 68 74 20 74 6f 20 62 65 20 61 62 6c 65  ought to be able
69d20 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66   to determine if
69d30 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e   we are compilin
69d40 67 20 66 6f 72 20 77 69 6e 39 38 20 6f 72 20 77  g for win98 or w
69d50 69 6e 4e 54 0a 2a 2a 20 75 73 69 6e 67 20 74 68  inNT.** using th
69d60 65 20 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 20 6d  e _WIN32_WINNT m
69d70 61 63 72 6f 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  acro as follows:
69d80 0a 2a 2a 0a 2a 2a 20 23 69 66 20 64 65 66 69 6e  .**.** #if defin
69d90 65 64 28 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 29  ed(_WIN32_WINNT)
69da0 0a 2a 2a 20 23 20 64 65 66 69 6e 65 20 53 51 4c  .** # define SQL
69db0 49 54 45 5f 4f 53 5f 57 49 4e 4e 54 20 31 0a 2a  ITE_OS_WINNT 1.*
69dc0 2a 20 23 65 6c 73 65 0a 2a 2a 20 23 20 64 65 66  * #else.** # def
69dd0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  ine SQLITE_OS_WI
69de0 4e 4e 54 20 30 0a 2a 2a 20 23 65 6e 64 69 66 0a  NNT 0.** #endif.
69df0 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 76  **.** However, v
69e00 73 32 30 30 35 20 64 6f 65 73 20 6e 6f 74 20 73  s2005 does not s
69e10 65 74 20 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 20  et _WIN32_WINNT 
69e20 62 79 20 64 65 66 61 75 6c 74 2c 20 61 73 20 69  by default, as i
69e30 74 20 6f 75 67 68 74 20 74 6f 2c 0a 2a 2a 20 73  t ought to,.** s
69e40 6f 20 74 68 65 20 61 62 6f 76 65 20 74 65 73 74  o the above test
69e50 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 2e 20   does not work. 
69e60 20 57 65 27 6c 6c 20 6a 75 73 74 20 61 73 73 75   We'll just assu
69e70 6d 65 20 74 68 61 74 20 65 76 65 72 79 74 68 69  me that everythi
69e80 6e 67 20 69 73 0a 2a 2a 20 77 69 6e 4e 54 20 75  ng is.** winNT u
69e90 6e 6c 65 73 73 20 74 68 65 20 70 72 6f 67 72 61  nless the progra
69ea0 6d 6d 65 72 20 65 78 70 6c 69 63 69 74 6c 79 20  mmer explicitly 
69eb0 73 61 79 73 20 6f 74 68 65 72 77 69 73 65 20 62  says otherwise b
69ec0 79 20 73 65 74 74 69 6e 67 0a 2a 2a 20 53 51 4c  y setting.** SQL
69ed0 49 54 45 5f 4f 53 5f 57 49 4e 4e 54 20 74 6f 20  ITE_OS_WINNT to 
69ee0 30 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  0..*/.#if SQLITE
69ef0 5f 4f 53 5f 57 49 4e 20 26 26 20 21 64 65 66 69  _OS_WIN && !defi
69f00 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 57 49  ned(SQLITE_OS_WI
69f10 4e 4e 54 29 0a 23 20 64 65 66 69 6e 65 20 53 51  NNT).# define SQ
69f20 4c 49 54 45 5f 4f 53 5f 57 49 4e 4e 54 20 31 0a  LITE_OS_WINNT 1.
69f30 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65  #endif../*.** De
69f40 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 61 72  termine if we ar
69f50 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 57  e dealing with W
69f60 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 63 68  indowsCE - which
69f70 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72   has a much.** r
69f80 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23  educed API..*/.#
69f90 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33  if defined(_WIN3
69fa0 32 5f 57 43 45 29 0a 23 20 64 65 66 69 6e 65 20  2_WCE).# define 
69fb0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20  SQLITE_OS_WINCE 
69fc0 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  1.#else.# define
69fd0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
69fe0 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   0.#endif../*.**
69ff0 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65   Determine if we
6a000 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
6a010 68 20 57 69 6e 52 54 2c 20 77 68 69 63 68 20 70  h WinRT, which p
6a020 72 6f 76 69 64 65 73 20 6f 6e 6c 79 20 61 20 73  rovides only a s
6a030 75 62 73 65 74 20 6f 66 0a 2a 2a 20 74 68 65 20  ubset of.** the 
6a040 66 75 6c 6c 20 57 69 6e 33 32 20 41 50 49 2e 0a  full Win32 API..
6a050 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
6a060 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 29  SQLITE_OS_WINRT)
6a070 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
6a080 5f 4f 53 5f 57 49 4e 52 54 20 30 0a 23 65 6e 64  _OS_WINRT 0.#end
6a090 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 63  if../*.** When c
6a0a0 6f 6d 70 69 6c 65 64 20 66 6f 72 20 57 69 6e 43  ompiled for WinC
6a0b0 45 20 6f 72 20 57 69 6e 52 54 2c 20 74 68 65 72  E or WinRT, ther
6a0c0 65 20 69 73 20 6e 6f 20 63 6f 6e 63 65 70 74 20  e is no concept 
6a0d0 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a  of the current.*
6a0e0 2a 20 64 69 72 65 63 74 6f 72 79 2e 0a 20 2a 2f  * directory.. */
6a0f0 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f  .#if !SQLITE_OS_
6a100 57 49 4e 43 45 20 26 26 20 21 53 51 4c 49 54 45  WINCE && !SQLITE
6a110 5f 4f 53 5f 57 49 4e 52 54 0a 23 20 64 65 66 69  _OS_WINRT.# defi
6a120 6e 65 20 53 51 4c 49 54 45 5f 43 55 52 44 49 52  ne SQLITE_CURDIR
6a130 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49 66   1.#endif../* If
6a140 20 74 68 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e   the SET_FULLSYN
6a150 43 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74 20 64  C macro is not d
6a160 65 66 69 6e 65 64 20 61 62 6f 76 65 2c 20 74 68  efined above, th
6a170 65 6e 20 6d 61 6b 65 20 69 74 0a 2a 2a 20 61 20  en make it.** a 
6a180 6e 6f 2d 6f 70 0a 2a 2f 0a 23 69 66 6e 64 65 66  no-op.*/.#ifndef
6a190 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 0a 23 20   SET_FULLSYNC.# 
6a1a0 64 65 66 69 6e 65 20 53 45 54 5f 46 55 4c 4c 53  define SET_FULLS
6a1b0 59 4e 43 28 78 2c 79 29 0a 23 65 6e 64 69 66 0a  YNC(x,y).#endif.
6a1c0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75  ./*.** The defau
6a1d0 6c 74 20 73 69 7a 65 20 6f 66 20 61 20 64 69 73  lt size of a dis
6a1e0 6b 20 73 65 63 74 6f 72 0a 2a 2f 0a 23 69 66 6e  k sector.*/.#ifn
6a1f0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  def SQLITE_DEFAU
6a200 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a 23  LT_SECTOR_SIZE.#
6a210 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44   define SQLITE_D
6a220 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49  EFAULT_SECTOR_SI
6a230 5a 45 20 34 30 39 36 0a 23 65 6e 64 69 66 0a 0a  ZE 4096.#endif..
6a240 2f 2a 0a 2a 2a 20 54 65 6d 70 6f 72 61 72 79 20  /*.** Temporary 
6a250 66 69 6c 65 73 20 61 72 65 20 6e 61 6d 65 64 20  files are named 
6a260 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 74 68  starting with th
6a270 69 73 20 70 72 65 66 69 78 20 66 6f 6c 6c 6f 77  is prefix follow
6a280 65 64 20 62 79 20 31 36 20 72 61 6e 64 6f 6d 0a  ed by 16 random.
6a290 2a 2a 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20  ** alphanumeric 
6a2a0 63 68 61 72 61 63 74 65 72 73 2c 20 61 6e 64 20  characters, and 
6a2b0 6e 6f 20 66 69 6c 65 20 65 78 74 65 6e 73 69 6f  no file extensio
6a2c0 6e 2e 20 54 68 65 79 20 61 72 65 20 73 74 6f 72  n. They are stor
6a2d0 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 4f 53 27  ed in the.** OS'
6a2e0 73 20 73 74 61 6e 64 61 72 64 20 74 65 6d 70 6f  s standard tempo
6a2f0 72 61 72 79 20 66 69 6c 65 20 64 69 72 65 63 74  rary file direct
6a300 6f 72 79 2c 20 61 6e 64 20 61 72 65 20 64 65 6c  ory, and are del
6a310 65 74 65 64 20 70 72 69 6f 72 20 74 6f 20 65 78  eted prior to ex
6a320 69 74 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65  it..** If sqlite
6a330 20 69 73 20 62 65 69 6e 67 20 65 6d 62 65 64 64   is being embedd
6a340 65 64 20 69 6e 20 61 6e 6f 74 68 65 72 20 70 72  ed in another pr
6a350 6f 67 72 61 6d 2c 20 79 6f 75 20 6d 61 79 20 77  ogram, you may w
6a360 69 73 68 20 74 6f 20 63 68 61 6e 67 65 20 74 68  ish to change th
6a370 65 0a 2a 2a 20 70 72 65 66 69 78 20 74 6f 20 72  e.** prefix to r
6a380 65 66 6c 65 63 74 20 79 6f 75 72 20 70 72 6f 67  eflect your prog
6a390 72 61 6d 27 73 20 6e 61 6d 65 2c 20 73 6f 20 74  ram's name, so t
6a3a0 68 61 74 20 69 66 20 79 6f 75 72 20 70 72 6f 67  hat if your prog
6a3b0 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 70 72 65  ram exits.** pre
6a3c0 6d 61 74 75 72 65 6c 79 2c 20 6f 6c 64 20 74 65  maturely, old te
6a3d0 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 63 61  mporary files ca
6a3e0 6e 20 62 65 20 65 61 73 69 6c 79 20 69 64 65 6e  n be easily iden
6a3f0 74 69 66 69 65 64 2e 20 54 68 69 73 20 63 61 6e  tified. This can
6a400 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 75 73 69 6e   be done.** usin
6a410 67 20 2d 44 53 51 4c 49 54 45 5f 54 45 4d 50 5f  g -DSQLITE_TEMP_
6a420 46 49 4c 45 5f 50 52 45 46 49 58 3d 6d 79 70 72  FILE_PREFIX=mypr
6a430 65 66 69 78 5f 20 6f 6e 20 74 68 65 20 63 6f 6d  efix_ on the com
6a440 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69  piler command li
6a450 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 36 2d 31  ne..**.** 2006-1
6a460 30 2d 33 31 3a 20 20 54 68 65 20 64 65 66 61 75  0-31:  The defau
6a470 6c 74 20 70 72 65 66 69 78 20 75 73 65 64 20 74  lt prefix used t
6a480 6f 20 62 65 20 22 73 71 6c 69 74 65 5f 22 2e 20  o be "sqlite_". 
6a490 20 42 75 74 20 74 68 65 6e 0a 2a 2a 20 4d 63 61   But then.** Mca
6a4a0 66 65 65 20 73 74 61 72 74 65 64 20 75 73 69 6e  fee started usin
6a4b0 67 20 53 51 4c 69 74 65 20 69 6e 20 74 68 65 69  g SQLite in thei
6a4c0 72 20 61 6e 74 69 2d 76 69 72 75 73 20 70 72 6f  r anti-virus pro
6a4d0 64 75 63 74 20 61 6e 64 20 69 74 0a 2a 2a 20 73  duct and it.** s
6a4e0 74 61 72 74 65 64 20 70 75 74 74 69 6e 67 20 66  tarted putting f
6a4f0 69 6c 65 73 20 77 69 74 68 20 74 68 65 20 22 73  iles with the "s
6a500 71 6c 69 74 65 22 20 6e 61 6d 65 20 69 6e 20 74  qlite" name in t
6a510 68 65 20 63 3a 2f 74 65 6d 70 20 66 6f 6c 64 65  he c:/temp folde
6a520 72 2e 0a 2a 2a 20 54 68 69 73 20 61 6e 6e 6f 79  r..** This annoy
6a530 65 64 20 6d 61 6e 79 20 77 69 6e 64 6f 77 73 20  ed many windows 
6a540 75 73 65 72 73 2e 20 20 54 68 6f 73 65 20 75 73  users.  Those us
6a550 65 72 73 20 77 6f 75 6c 64 20 74 68 65 6e 20 64  ers would then d
6a560 6f 20 61 20 0a 2a 2a 20 47 6f 6f 67 6c 65 20 73  o a .** Google s
6a570 65 61 72 63 68 20 66 6f 72 20 22 73 71 6c 69 74  earch for "sqlit
6a580 65 22 2c 20 66 69 6e 64 20 74 68 65 20 74 65 6c  e", find the tel
6a590 65 70 68 6f 6e 65 20 6e 75 6d 62 65 72 73 20 6f  ephone numbers o
6a5a0 66 20 74 68 65 0a 2a 2a 20 64 65 76 65 6c 6f 70  f the.** develop
6a5b0 65 72 73 20 61 6e 64 20 63 61 6c 6c 20 74 6f 20  ers and call to 
6a5c0 77 61 6b 65 20 74 68 65 6d 20 75 70 20 61 74 20  wake them up at 
6a5d0 6e 69 67 68 74 20 61 6e 64 20 63 6f 6d 70 6c 61  night and compla
6a5e0 69 6e 2e 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20  in..** For this 
6a5f0 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 65 66 61  reason, the defa
6a600 75 6c 74 20 6e 61 6d 65 20 70 72 65 66 69 78 20  ult name prefix 
6a610 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 62 65  is changed to be
6a620 20 22 73 71 6c 69 74 65 22 20 0a 2a 2a 20 73 70   "sqlite" .** sp
6a630 65 6c 6c 65 64 20 62 61 63 6b 77 61 72 64 73 2e  elled backwards.
6a640 20 20 53 6f 20 74 68 65 20 74 65 6d 70 20 66 69    So the temp fi
6a650 6c 65 73 20 61 72 65 20 73 74 69 6c 6c 20 69 64  les are still id
6a660 65 6e 74 69 66 69 65 64 2c 20 62 75 74 0a 2a 2a  entified, but.**
6a670 20 61 6e 79 62 6f 64 79 20 73 6d 61 72 74 20 65   anybody smart e
6a680 6e 6f 75 67 68 20 74 6f 20 66 69 67 75 72 65 20  nough to figure 
6a690 6f 75 74 20 74 68 65 20 63 6f 64 65 20 69 73 20  out the code is 
6a6a0 61 6c 73 6f 20 6c 69 6b 65 6c 79 20 73 6d 61 72  also likely smar
6a6b0 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 6b  t.** enough to k
6a6c0 6e 6f 77 20 74 68 61 74 20 63 61 6c 6c 69 6e 67  now that calling
6a6d0 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 20 77   the developer w
6a6e0 69 6c 6c 20 6e 6f 74 20 68 65 6c 70 20 67 65 74  ill not help get
6a6f0 20 72 69 64 0a 2a 2a 20 6f 66 20 74 68 65 20 66   rid.** of the f
6a700 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ile..*/.#ifndef 
6a710 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45  SQLITE_TEMP_FILE
6a720 5f 50 52 45 46 49 58 0a 23 20 64 65 66 69 6e 65  _PREFIX.# define
6a730 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c   SQLITE_TEMP_FIL
6a740 45 5f 50 52 45 46 49 58 20 22 65 74 69 6c 71 73  E_PREFIX "etilqs
6a750 5f 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  _".#endif../*.**
6a760 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   The following v
6a770 61 6c 75 65 73 20 6d 61 79 20 62 65 20 70 61 73  alues may be pas
6a780 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
6a790 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a  d argument to.**
6a7a0 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29   sqlite3OsLock()
6a7b0 2e 20 54 68 65 20 76 61 72 69 6f 75 73 20 6c 6f  . The various lo
6a7c0 63 6b 73 20 65 78 68 69 62 69 74 20 74 68 65 20  cks exhibit the 
6a7d0 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 6d 61 6e 74  following semant
6a7e0 69 63 73 3a 0a 2a 2a 0a 2a 2a 20 53 48 41 52 45  ics:.**.** SHARE
6a7f0 44 3a 20 20 20 20 41 6e 79 20 6e 75 6d 62 65 72  D:    Any number
6a800 20 6f 66 20 70 72 6f 63 65 73 73 65 73 20 6d 61   of processes ma
6a810 79 20 68 6f 6c 64 20 61 20 53 48 41 52 45 44 20  y hold a SHARED 
6a820 6c 6f 63 6b 20 73 69 6d 75 6c 74 61 6e 65 6f 75  lock simultaneou
6a830 73 6c 79 2e 0a 2a 2a 20 52 45 53 45 52 56 45 44  sly..** RESERVED
6a840 3a 20 20 41 20 73 69 6e 67 6c 65 20 70 72 6f 63  :  A single proc
6a850 65 73 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 52  ess may hold a R
6a860 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20  ESERVED lock on 
6a870 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20  a file at.**    
6a880 20 20 20 20 20 20 20 20 61 6e 79 20 74 69 6d 65          any time
6a890 2e 20 4f 74 68 65 72 20 70 72 6f 63 65 73 73 65  . Other processe
6a8a0 73 20 6d 61 79 20 68 6f 6c 64 20 61 6e 64 20 6f  s may hold and o
6a8b0 62 74 61 69 6e 20 6e 65 77 20 53 48 41 52 45 44  btain new SHARED
6a8c0 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 50 45 4e 44 49   locks..** PENDI
6a8d0 4e 47 3a 20 20 20 41 20 73 69 6e 67 6c 65 20 70  NG:   A single p
6a8e0 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c 64 20  rocess may hold 
6a8f0 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f  a PENDING lock o
6a900 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20 20  n a file at.**  
6a910 20 20 20 20 20 20 20 20 20 20 61 6e 79 20 6f 6e            any on
6a920 65 20 74 69 6d 65 2e 20 45 78 69 73 74 69 6e 67  e time. Existing
6a930 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61   SHARED locks ma
6a940 79 20 70 65 72 73 69 73 74 2c 20 62 75 74 20 6e  y persist, but n
6a950 6f 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20  o new.**        
6a960 20 20 20 20 53 48 41 52 45 44 20 6c 6f 63 6b 73      SHARED locks
6a970 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64   may be obtained
6a980 20 62 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73   by other proces
6a990 73 65 73 2e 0a 2a 2a 20 45 58 43 4c 55 53 49 56  ses..** EXCLUSIV
6a9a0 45 3a 20 41 6e 20 45 58 43 4c 55 53 49 56 45 20  E: An EXCLUSIVE 
6a9b0 6c 6f 63 6b 20 70 72 65 63 6c 75 64 65 73 20 61  lock precludes a
6a9c0 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 2e 0a  ll other locks..
6a9d0 2a 2a 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 4c 4f  **.** PENDING_LO
6a9e0 43 4b 20 6d 61 79 20 6e 6f 74 20 62 65 20 70 61  CK may not be pa
6a9f0 73 73 65 64 20 64 69 72 65 63 74 6c 79 20 74 6f  ssed directly to
6aa00 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29   sqlite3OsLock()
6aa10 2e 20 49 6e 73 74 65 61 64 2c 20 61 0a 2a 2a 20  . Instead, a.** 
6aa20 70 72 6f 63 65 73 73 20 74 68 61 74 20 72 65 71  process that req
6aa30 75 65 73 74 73 20 61 6e 20 45 58 43 4c 55 53 49  uests an EXCLUSI
6aa40 56 45 20 6c 6f 63 6b 20 6d 61 79 20 61 63 74 75  VE lock may actu
6aa50 61 6c 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 45  ally obtain a PE
6aa60 4e 44 49 4e 47 0a 2a 2a 20 6c 6f 63 6b 2e 20 54  NDING.** lock. T
6aa70 68 69 73 20 63 61 6e 20 62 65 20 75 70 67 72 61  his can be upgra
6aa80 64 65 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53  ded to an EXCLUS
6aa90 49 56 45 20 6c 6f 63 6b 20 62 79 20 61 20 73 75  IVE lock by a su
6aaa0 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f  bsequent call to
6aab0 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  .** sqlite3OsLoc
6aac0 6b 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  k()..*/.#define 
6aad0 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20  NO_LOCK         
6aae0 30 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 44  0.#define SHARED
6aaf0 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 66  _LOCK     1.#def
6ab00 69 6e 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  ine RESERVED_LOC
6ab10 4b 20 20 20 32 0a 23 64 65 66 69 6e 65 20 50 45  K   2.#define PE
6ab20 4e 44 49 4e 47 5f 4c 4f 43 4b 20 20 20 20 33 0a  NDING_LOCK    3.
6ab30 23 64 65 66 69 6e 65 20 45 58 43 4c 55 53 49 56  #define EXCLUSIV
6ab40 45 5f 4c 4f 43 4b 20 20 34 0a 0a 2f 2a 0a 2a 2a  E_LOCK  4../*.**
6ab50 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 4e 6f   File Locking No
6ab60 74 65 73 3a 20 20 28 4d 6f 73 74 6c 79 20 61 62  tes:  (Mostly ab
6ab70 6f 75 74 20 77 69 6e 64 6f 77 73 20 62 75 74 20  out windows but 
6ab80 61 6c 73 6f 20 73 6f 6d 65 20 69 6e 66 6f 20 66  also some info f
6ab90 6f 72 20 55 6e 69 78 29 0a 2a 2a 0a 2a 2a 20 57  or Unix).**.** W
6aba0 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 4c 6f 63  e cannot use Loc
6abb0 6b 46 69 6c 65 45 78 28 29 20 6f 72 20 55 6e 6c  kFileEx() or Unl
6abc0 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f 6e 20 57  ockFileEx() on W
6abd0 69 6e 39 35 2f 39 38 2f 4d 45 20 62 65 63 61 75  in95/98/ME becau
6abe0 73 65 0a 2a 2a 20 74 68 6f 73 65 20 66 75 6e 63  se.** those func
6abf0 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 76  tions are not av
6ac00 61 69 6c 61 62 6c 65 2e 20 20 53 6f 20 77 65 20  ailable.  So we 
6ac10 75 73 65 20 6f 6e 6c 79 20 4c 6f 63 6b 46 69 6c  use only LockFil
6ac20 65 28 29 20 61 6e 64 0a 2a 2a 20 55 6e 6c 6f 63  e() and.** Unloc
6ac30 6b 46 69 6c 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 4c  kFile()..**.** L
6ac40 6f 63 6b 46 69 6c 65 28 29 20 70 72 65 76 65 6e  ockFile() preven
6ac50 74 73 20 6e 6f 74 20 6a 75 73 74 20 77 72 69 74  ts not just writ
6ac60 69 6e 67 20 62 75 74 20 61 6c 73 6f 20 72 65 61  ing but also rea
6ac70 64 69 6e 67 20 62 79 20 6f 74 68 65 72 20 70 72  ding by other pr
6ac80 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 41 20 53 48  ocesses..** A SH
6ac90 41 52 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74  ARED_LOCK is obt
6aca0 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67  ained by locking
6acb0 20 61 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d   a single random
6acc0 6c 79 2d 63 68 6f 73 65 6e 20 0a 2a 2a 20 62 79  ly-chosen .** by
6acd0 74 65 20 6f 75 74 20 6f 66 20 61 20 73 70 65 63  te out of a spec
6ace0 69 66 69 63 20 72 61 6e 67 65 20 6f 66 20 62 79  ific range of by
6acf0 74 65 73 2e 20 54 68 65 20 6c 6f 63 6b 20 62 79  tes. The lock by
6ad00 74 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 61  te is obtained a
6ad10 74 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 73 6f 20  t .** random so 
6ad20 74 77 6f 20 73 65 70 61 72 61 74 65 20 72 65 61  two separate rea
6ad30 64 65 72 73 20 63 61 6e 20 70 72 6f 62 61 62 6c  ders can probabl
6ad40 79 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c  y access the fil
6ad50 65 20 61 74 20 74 68 65 20 0a 2a 2a 20 73 61 6d  e at the .** sam
6ad60 65 20 74 69 6d 65 2c 20 75 6e 6c 65 73 73 20 74  e time, unless t
6ad70 68 65 79 20 61 72 65 20 75 6e 6c 75 63 6b 79 20  hey are unlucky 
6ad80 61 6e 64 20 63 68 6f 6f 73 65 20 74 68 65 20 73  and choose the s
6ad90 61 6d 65 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a  ame lock byte..*
6ada0 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 45 5f 4c  * An EXCLUSIVE_L
6adb0 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  OCK is obtained 
6adc0 62 79 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 62  by locking all b
6add0 79 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67  ytes in the rang
6ade0 65 2e 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20  e..** There can 
6adf0 6f 6e 6c 79 20 62 65 20 6f 6e 65 20 77 72 69 74  only be one writ
6ae00 65 72 2e 20 20 41 20 52 45 53 45 52 56 45 44 5f  er.  A RESERVED_
6ae10 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64  LOCK is obtained
6ae20 20 62 79 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 61   by locking.** a
6ae30 20 73 69 6e 67 6c 65 20 62 79 74 65 20 6f 66 20   single byte of 
6ae40 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 69 73  the file that is
6ae50 20 64 65 73 69 67 6e 61 74 65 64 20 61 73 20 74   designated as t
6ae60 68 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b  he reserved lock
6ae70 20 62 79 74 65 2e 0a 2a 2a 20 41 20 50 45 4e 44   byte..** A PEND
6ae80 49 4e 47 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61  ING_LOCK is obta
6ae90 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20  ined by locking 
6aea0 61 20 64 65 73 69 67 6e 61 74 65 64 20 62 79 74  a designated byt
6aeb0 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
6aec0 0a 2a 2a 20 74 68 65 20 52 45 53 45 52 56 45 44  .** the RESERVED
6aed0 5f 4c 4f 43 4b 20 62 79 74 65 2e 0a 2a 2a 0a 2a  _LOCK byte..**.*
6aee0 2a 20 4f 6e 20 57 69 6e 4e 54 2f 32 4b 2f 58 50  * On WinNT/2K/XP
6aef0 20 73 79 73 74 65 6d 73 2c 20 4c 6f 63 6b 46 69   systems, LockFi
6af00 6c 65 45 78 28 29 20 61 6e 64 20 55 6e 6c 6f 63  leEx() and Unloc
6af10 6b 46 69 6c 65 45 78 28 29 20 61 72 65 20 61 76  kFileEx() are av
6af20 61 69 6c 61 62 6c 65 2c 0a 2a 2a 20 77 68 69 63  ailable,.** whic
6af30 68 20 6d 65 61 6e 73 20 77 65 20 63 61 6e 20 75  h means we can u
6af40 73 65 20 72 65 61 64 65 72 2f 77 72 69 74 65 72  se reader/writer
6af50 20 6c 6f 63 6b 73 2e 20 20 57 68 65 6e 20 72 65   locks.  When re
6af60 61 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b  ader/writer lock
6af70 73 0a 2a 2a 20 61 72 65 20 75 73 65 64 2c 20 74  s.** are used, t
6af80 68 65 20 6c 6f 63 6b 20 69 73 20 70 6c 61 63 65  he lock is place
6af90 64 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 72 61  d on the same ra
6afa0 6e 67 65 20 6f 66 20 62 79 74 65 73 20 74 68 61  nge of bytes tha
6afb0 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 66 6f 72  t is used.** for
6afc0 20 70 72 6f 62 61 62 69 6c 69 73 74 69 63 20 6c   probabilistic l
6afd0 6f 63 6b 69 6e 67 20 69 6e 20 57 69 6e 39 35 2f  ocking in Win95/
6afe0 39 38 2f 4d 45 2e 20 20 48 65 6e 63 65 2c 20 74  98/ME.  Hence, t
6aff0 68 65 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d  he locking schem
6b000 65 0a 2a 2a 20 77 69 6c 6c 20 73 75 70 70 6f 72  e.** will suppor
6b010 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69  t two or more Wi
6b020 6e 39 35 20 72 65 61 64 65 72 73 20 6f 72 20 74  n95 readers or t
6b030 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69 6e 4e 54  wo or more WinNT
6b040 20 72 65 61 64 65 72 73 2e 0a 2a 2a 20 42 75 74   readers..** But
6b050 20 61 20 73 69 6e 67 6c 65 20 57 69 6e 39 35 20   a single Win95 
6b060 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b  reader will lock
6b070 20 6f 75 74 20 61 6c 6c 20 57 69 6e 4e 54 20 72   out all WinNT r
6b080 65 61 64 65 72 73 20 61 6e 64 20 61 20 73 69 6e  eaders and a sin
6b090 67 6c 65 0a 2a 2a 20 57 69 6e 4e 54 20 72 65 61  gle.** WinNT rea
6b0a0 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75  der will lock ou
6b0b0 74 20 61 6c 6c 20 6f 74 68 65 72 20 57 69 6e 39  t all other Win9
6b0c0 35 20 72 65 61 64 65 72 73 2e 0a 2a 2a 0a 2a 2a  5 readers..**.**
6b0d0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 23   The following #
6b0e0 64 65 66 69 6e 65 73 20 73 70 65 63 69 66 79 20  defines specify 
6b0f0 74 68 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74  the range of byt
6b100 65 73 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b  es used for lock
6b110 69 6e 67 2e 0a 2a 2a 20 53 48 41 52 45 44 5f 53  ing..** SHARED_S
6b120 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d 62 65  IZE is the numbe
6b130 72 20 6f 66 20 62 79 74 65 73 20 61 76 61 69 6c  r of bytes avail
6b140 61 62 6c 65 20 69 6e 20 74 68 65 20 70 6f 6f 6c  able in the pool
6b150 20 66 72 6f 6d 20 77 68 69 63 68 0a 2a 2a 20 61   from which.** a
6b160 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 69 73 20   random byte is 
6b170 73 65 6c 65 63 74 65 64 20 66 6f 72 20 61 20 73  selected for a s
6b180 68 61 72 65 64 20 6c 6f 63 6b 2e 20 20 54 68 65  hared lock.  The
6b190 20 70 6f 6f 6c 20 6f 66 20 62 79 74 65 73 20 66   pool of bytes f
6b1a0 6f 72 0a 2a 2a 20 73 68 61 72 65 64 20 6c 6f 63  or.** shared loc
6b1b0 6b 73 20 62 65 67 69 6e 73 20 61 74 20 53 48 41  ks begins at SHA
6b1c0 52 45 44 5f 46 49 52 53 54 2e 20 0a 2a 2a 0a 2a  RED_FIRST. .**.*
6b1d0 2a 20 54 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69  * The same locki
6b1e0 6e 67 20 73 74 72 61 74 65 67 79 20 61 6e 64 0a  ng strategy and.
6b1f0 2a 2a 20 62 79 74 65 20 72 61 6e 67 65 73 20 61  ** byte ranges a
6b200 72 65 20 75 73 65 64 20 66 6f 72 20 55 6e 69 78  re used for Unix
6b210 2e 20 20 54 68 69 73 20 6c 65 61 76 65 73 20 6f  .  This leaves o
6b220 70 65 6e 20 74 68 65 20 70 6f 73 73 69 62 6c 69  pen the possibli
6b230 74 79 20 6f 66 20 68 61 76 69 6e 67 0a 2a 2a 20  ty of having.** 
6b240 63 6c 69 65 6e 74 73 20 6f 6e 20 77 69 6e 39 35  clients on win95
6b250 2c 20 77 69 6e 4e 54 2c 20 61 6e 64 20 75 6e 69  , winNT, and uni
6b260 78 20 61 6c 6c 20 74 61 6c 6b 69 6e 67 20 74 6f  x all talking to
6b270 20 74 68 65 20 73 61 6d 65 20 73 68 61 72 65 64   the same shared
6b280 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 61 6c 6c   file.** and all
6b290 20 6c 6f 63 6b 69 6e 67 20 63 6f 72 72 65 63 74   locking correct
6b2a0 6c 79 2e 20 20 54 6f 20 64 6f 20 73 6f 20 77 6f  ly.  To do so wo
6b2b0 75 6c 64 20 72 65 71 75 69 72 65 20 74 68 61 74  uld require that
6b2c0 20 73 61 6d 62 61 20 28 6f 72 20 77 68 61 74 65   samba (or whate
6b2d0 76 65 72 0a 2a 2a 20 74 6f 6f 6c 20 69 73 20 62  ver.** tool is b
6b2e0 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20 66 69  eing used for fi
6b2f0 6c 65 20 73 68 61 72 69 6e 67 29 20 69 6d 70 6c  le sharing) impl
6b300 65 6d 65 6e 74 73 20 6c 6f 63 6b 73 20 63 6f 72  ements locks cor
6b310 72 65 63 74 6c 79 20 62 65 74 77 65 65 6e 0a 2a  rectly between.*
6b320 2a 20 77 69 6e 64 6f 77 73 20 61 6e 64 20 75 6e  * windows and un
6b330 69 78 2e 20 20 49 27 6d 20 67 75 65 73 73 69 6e  ix.  I'm guessin
6b340 67 20 74 68 61 74 20 69 73 6e 27 74 20 6c 69 6b  g that isn't lik
6b350 65 6c 79 20 74 6f 20 68 61 70 70 65 6e 2c 20 62  ely to happen, b
6b360 75 74 20 62 79 0a 2a 2a 20 75 73 69 6e 67 20 74  ut by.** using t
6b370 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20  he same locking 
6b380 72 61 6e 67 65 20 77 65 20 61 72 65 20 61 74 20  range we are at 
6b390 6c 65 61 73 74 20 6f 70 65 6e 20 74 6f 20 74 68  least open to th
6b3a0 65 20 70 6f 73 73 69 62 69 6c 69 74 79 2e 0a 2a  e possibility..*
6b3b0 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e 67 20 69 6e 20  *.** Locking in 
6b3c0 77 69 6e 64 6f 77 73 20 69 73 20 6d 61 6e 64 69  windows is mandi
6b3d0 74 6f 72 79 2e 20 20 46 6f 72 20 74 68 69 73 20  tory.  For this 
6b3e0 72 65 61 73 6f 6e 2c 20 77 65 20 63 61 6e 6e 6f  reason, we canno
6b3f0 74 20 73 74 6f 72 65 0a 2a 2a 20 61 63 74 75 61  t store.** actua
6b400 6c 20 64 61 74 61 20 69 6e 20 74 68 65 20 62 79  l data in the by
6b410 74 65 73 20 75 73 65 64 20 66 6f 72 20 6c 6f 63  tes used for loc
6b420 6b 69 6e 67 2e 20 20 54 68 65 20 70 61 67 65 72  king.  The pager
6b430 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65 73   never allocates
6b440 0a 2a 2a 20 74 68 65 20 70 61 67 65 73 20 69 6e  .** the pages in
6b450 76 6f 6c 76 65 64 20 69 6e 20 6c 6f 63 6b 69 6e  volved in lockin
6b460 67 20 74 68 65 72 65 66 6f 72 65 2e 20 20 53 48  g therefore.  SH
6b470 41 52 45 44 5f 53 49 5a 45 20 69 73 20 73 65 6c  ARED_SIZE is sel
6b480 65 63 74 65 64 20 73 6f 0a 2a 2a 20 74 68 61 74  ected so.** that
6b490 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 6c 6c 20   all locks will 
6b4a0 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20  fit on a single 
6b4b0 70 61 67 65 20 65 76 65 6e 20 61 74 20 74 68 65  page even at the
6b4c0 20 6d 69 6e 69 6d 75 6d 20 70 61 67 65 20 73 69   minimum page si
6b4d0 7a 65 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 42  ze..** PENDING_B
6b4e0 59 54 45 20 64 65 66 69 6e 65 73 20 74 68 65 20  YTE defines the 
6b4f0 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
6b500 20 6c 6f 63 6b 73 2e 20 20 42 79 20 64 65 66 61   locks.  By defa
6b510 75 6c 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  ult PENDING_BYTE
6b520 0a 2a 2a 20 69 73 20 73 65 74 20 68 69 67 68 20  .** is set high 
6b530 73 6f 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74  so that we don't
6b540 20 68 61 76 65 20 74 6f 20 61 6c 6c 6f 63 61 74   have to allocat
6b550 65 20 61 6e 20 75 6e 75 73 65 64 20 70 61 67 65  e an unused page
6b560 20 65 78 63 65 70 74 0a 2a 2a 20 66 6f 72 20 76   except.** for v
6b570 65 72 79 20 6c 61 72 67 65 20 64 61 74 61 62 61  ery large databa
6b580 73 65 73 2e 20 20 42 75 74 20 6f 6e 65 20 73 68  ses.  But one sh
6b590 6f 75 6c 64 20 74 65 73 74 20 74 68 65 20 70 61  ould test the pa
6b5a0 67 65 20 73 6b 69 70 70 69 6e 67 20 6c 6f 67 69  ge skipping logi
6b5b0 63 20 0a 2a 2a 20 62 79 20 73 65 74 74 69 6e 67  c .** by setting
6b5c0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6c 6f   PENDING_BYTE lo
6b5d0 77 20 61 6e 64 20 72 75 6e 6e 69 6e 67 20 74 68  w and running th
6b5e0 65 20 65 6e 74 69 72 65 20 72 65 67 72 65 73 73  e entire regress
6b5f0 69 6f 6e 20 73 75 69 74 65 2e 0a 2a 2a 0a 2a 2a  ion suite..**.**
6b600 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 76 61   Changing the va
6b610 6c 75 65 20 6f 66 20 50 45 4e 44 49 4e 47 5f 42  lue of PENDING_B
6b620 59 54 45 20 72 65 73 75 6c 74 73 20 69 6e 20 61  YTE results in a
6b630 20 73 75 62 74 6c 79 20 69 6e 63 6f 6d 70 61 74   subtly incompat
6b640 69 62 6c 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72  ible.** file for
6b650 6d 61 74 2e 20 20 44 65 70 65 6e 64 69 6e 67 20  mat.  Depending 
6b660 6f 6e 20 68 6f 77 20 69 74 20 69 73 20 63 68 61  on how it is cha
6b670 6e 67 65 64 2c 20 79 6f 75 20 6d 69 67 68 74 20  nged, you might 
6b680 6e 6f 74 20 6e 6f 74 69 63 65 0a 2a 2a 20 74 68  not notice.** th
6b690 65 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74  e incompatibilit
6b6a0 79 20 72 69 67 68 74 20 61 77 61 79 2c 20 65 76  y right away, ev
6b6b0 65 6e 20 72 75 6e 6e 69 6e 67 20 61 20 66 75 6c  en running a ful
6b6c0 6c 20 72 65 67 72 65 73 73 69 6f 6e 20 74 65 73  l regression tes
6b6d0 74 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c  t..** The defaul
6b6e0 74 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 50 45  t location of PE
6b6f0 4e 44 49 4e 47 5f 42 59 54 45 20 69 73 20 74 68  NDING_BYTE is th
6b700 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73  e first byte pas
6b710 74 20 74 68 65 0a 2a 2a 20 31 47 42 20 62 6f 75  t the.** 1GB bou
6b720 6e 64 61 72 79 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66  ndary..**.*/.#if
6b730 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
6b740 57 53 44 0a 23 20 64 65 66 69 6e 65 20 50 45 4e  WSD.# define PEN
6b750 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 28 30  DING_BYTE     (0
6b760 78 34 30 30 30 30 30 30 30 29 0a 23 65 6c 73 65  x40000000).#else
6b770 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e  .# define PENDIN
6b780 47 5f 42 59 54 45 20 20 20 20 20 20 73 71 6c 69  G_BYTE      sqli
6b790 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 0a 23  te3PendingByte.#
6b7a0 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 52 45  endif.#define RE
6b7b0 53 45 52 56 45 44 5f 42 59 54 45 20 20 20 20 20  SERVED_BYTE     
6b7c0 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 29  (PENDING_BYTE+1)
6b7d0 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f  .#define SHARED_
6b7e0 46 49 52 53 54 20 20 20 20 20 20 28 50 45 4e 44  FIRST      (PEND
6b7f0 49 4e 47 5f 42 59 54 45 2b 32 29 0a 23 64 65 66  ING_BYTE+2).#def
6b800 69 6e 65 20 53 48 41 52 45 44 5f 53 49 5a 45 20  ine SHARED_SIZE 
6b810 20 20 20 20 20 20 35 31 30 0a 0a 2f 2a 0a 2a 2a        510../*.**
6b820 20 57 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20   Wrapper around 
6b830 4f 53 20 73 70 65 63 69 66 69 63 20 73 71 6c 69  OS specific sqli
6b840 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 66 75  te3_os_init() fu
6b850 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  nction..*/.SQLIT
6b860 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6b870 6c 69 74 65 33 4f 73 49 6e 69 74 28 76 6f 69 64  lite3OsInit(void
6b880 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74  );../* .** Funct
6b890 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73 73 69  ions for accessi
6b8a0 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ng sqlite3_file 
6b8b0 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49  methods .*/.SQLI
6b8c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6b8d0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71  qlite3OsClose(sq
6b8e0 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 53 51  lite3_file*);.SQ
6b8f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6b900 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73   sqlite3OsRead(s
6b910 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f  qlite3_file*, vo
6b920 69 64 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36  id*, int amt, i6
6b930 34 20 6f 66 66 73 65 74 29 3b 0a 53 51 4c 49 54  4 offset);.SQLIT
6b940 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6b950 6c 69 74 65 33 4f 73 57 72 69 74 65 28 73 71 6c  lite3OsWrite(sql
6b960 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73  ite3_file*, cons
6b970 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d 74  t void*, int amt
6b980 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a 53  , i64 offset);.S
6b990 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6b9a0 74 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  t sqlite3OsTrunc
6b9b0 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ate(sqlite3_file
6b9c0 2a 2c 20 69 36 34 20 73 69 7a 65 29 3b 0a 53 51  *, i64 size);.SQ
6b9d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6b9e0 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73   sqlite3OsSync(s
6b9f0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
6ba00 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6ba10 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
6ba20 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33  FileSize(sqlite3
6ba30 5f 66 69 6c 65 2a 2c 20 69 36 34 20 2a 70 53 69  _file*, i64 *pSi
6ba40 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ze);.SQLITE_PRIV
6ba50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
6ba60 73 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  sLock(sqlite3_fi
6ba70 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  le*, int);.SQLIT
6ba80 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6ba90 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71  lite3OsUnlock(sq
6baa0 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
6bab0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6bac0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43  E int sqlite3OsC
6bad0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
6bae0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
6baf0 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29  d, int *pResOut)
6bb00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6bb10 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69   int sqlite3OsFi
6bb20 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  leControl(sqlite
6bb30 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64  3_file*,int,void
6bb40 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6bb50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f  TE void sqlite3O
6bb60 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 48 69 6e 74  sFileControlHint
6bb70 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69  (sqlite3_file*,i
6bb80 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69  nt,void*);.#defi
6bb90 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  ne SQLITE_FCNTL_
6bba0 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 30 78 63  DB_UNCHANGED 0xc
6bbb0 61 30 39 33 66 61 30 0a 53 51 4c 49 54 45 5f 50  a093fa0.SQLITE_P
6bbc0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6bbd0 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73  e3OsSectorSize(s
6bbe0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
6bbf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6bc00 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65   int sqlite3OsDe
6bc10 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
6bc20 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ics(sqlite3_file
6bc30 20 2a 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52   *id);.SQLITE_PR
6bc40 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6bc50 33 4f 73 53 68 6d 4d 61 70 28 73 71 6c 69 74 65  3OsShmMap(sqlite
6bc60 33 5f 66 69 6c 65 20 2a 2c 69 6e 74 2c 69 6e 74  3_file *,int,int
6bc70 2c 69 6e 74 2c 76 6f 69 64 20 76 6f 6c 61 74 69  ,int,void volati
6bc80 6c 65 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  le **);.SQLITE_P
6bc90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6bca0 65 33 4f 73 53 68 6d 4c 6f 63 6b 28 73 71 6c 69  e3OsShmLock(sqli
6bcb0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
6bcc0 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  t, int, int);.SQ
6bcd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6bce0 64 20 73 71 6c 69 74 65 33 4f 73 53 68 6d 42 61  d sqlite3OsShmBa
6bcf0 72 72 69 65 72 28 73 71 6c 69 74 65 33 5f 66 69  rrier(sqlite3_fi
6bd00 6c 65 20 2a 69 64 29 3b 0a 53 51 4c 49 54 45 5f  le *id);.SQLITE_
6bd10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6bd20 74 65 33 4f 73 53 68 6d 55 6e 6d 61 70 28 73 71  te3OsShmUnmap(sq
6bd30 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
6bd40 69 6e 74 29 3b 0a 0a 0a 2f 2a 20 0a 2a 2a 20 46  int);.../* .** F
6bd50 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 63  unctions for acc
6bd60 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 76  essing sqlite3_v
6bd70 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53  fs methods .*/.S
6bd80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6bd90 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  t sqlite3OsOpen(
6bda0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63  sqlite3_vfs *, c
6bdb0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c  onst char *, sql
6bdc0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2c  ite3_file*, int,
6bdd0 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f   int *);.SQLITE_
6bde0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6bdf0 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69  te3OsDelete(sqli
6be00 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74  te3_vfs *, const
6be10 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53   char *, int);.S
6be20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6be30 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  t sqlite3OsAcces
6be40 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  s(sqlite3_vfs *,
6be50 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69   const char *, i
6be60 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  nt, int *pResOut
6be70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6be80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46  E int sqlite3OsF
6be90 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71 6c 69  ullPathname(sqli
6bea0 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74  te3_vfs *, const
6beb0 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68   char *, int, ch
6bec0 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53  ar *);.#ifndef S
6bed0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
6bee0 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45  EXTENSION.SQLITE
6bef0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
6bf00 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73  qlite3OsDlOpen(s
6bf10 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f  qlite3_vfs *, co
6bf20 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c  nst char *);.SQL
6bf30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6bf40 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f   sqlite3OsDlErro
6bf50 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  r(sqlite3_vfs *,
6bf60 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53   int, char *);.S
6bf70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6bf80 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 6c  id (*sqlite3OsDl
6bf90 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20  Sym(sqlite3_vfs 
6bfa0 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 74  *, void *, const
6bfb0 20 63 68 61 72 20 2a 29 29 28 76 6f 69 64 29 3b   char *))(void);
6bfc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6bfd0 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c  void sqlite3OsDl
6bfe0 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66  Close(sqlite3_vf
6bff0 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a 23 65  s *, void *);.#e
6c000 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
6c010 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
6c020 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ON */.SQLITE_PRI
6c030 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6c040 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c  OsRandomness(sql
6c050 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c  ite3_vfs *, int,
6c060 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45   char *);.SQLITE
6c070 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6c080 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69  ite3OsSleep(sqli
6c090 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 29 3b  te3_vfs *, int);
6c0a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6c0b0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72  int sqlite3OsCur
6c0c0 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 73 71  rentTimeInt64(sq
6c0d0 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 73 71 6c  lite3_vfs *, sql
6c0e0 69 74 65 33 5f 69 6e 74 36 34 2a 29 3b 0a 0a 2f  ite3_int64*);../
6c0f0 2a 0a 2a 2a 20 43 6f 6e 76 65 6e 69 65 6e 63 65  *.** Convenience
6c100 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 6f   functions for o
6c110 70 65 6e 69 6e 67 20 61 6e 64 20 63 6c 6f 73 69  pening and closi
6c120 6e 67 20 66 69 6c 65 73 20 75 73 69 6e 67 20 0a  ng files using .
6c130 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ** sqlite3_mallo
6c140 63 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 70  c() to obtain sp
6c150 61 63 65 20 66 6f 72 20 74 68 65 20 66 69 6c 65  ace for the file
6c160 2d 68 61 6e 64 6c 65 20 73 74 72 75 63 74 75 72  -handle structur
6c170 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
6c180 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6c190 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 73 71 6c  OsOpenMalloc(sql
6c1a0 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73  ite3_vfs *, cons
6c1b0 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65  t char *, sqlite
6c1c0 33 5f 66 69 6c 65 20 2a 2a 2c 20 69 6e 74 2c 69  3_file **, int,i
6c1d0 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
6c1e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6c1f0 4f 73 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69  OsCloseFree(sqli
6c200 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 65  te3_file *);..#e
6c210 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f  ndif /* _SQLITE_
6c220 4f 53 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  OS_H_ */../*****
6c230 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
6c240 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   os.h **********
6c250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6c260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6c270 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
6c280 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
6c290 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
6c2a0 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
6c2b0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
6c2c0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
6c2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
6c2e0 65 20 6d 75 74 65 78 2e 68 20 69 6e 20 74 68 65  e mutex.h in the
6c2f0 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74   middle of sqlit
6c300 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
6c310 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
6c320 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
6c330 66 69 6c 65 20 6d 75 74 65 78 2e 68 20 2a 2a 2a  file mutex.h ***
6c340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6c350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6c360 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
6c370 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a  2007 August 28.*
6c380 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
6c390 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
6c3a0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
6c3b0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
6c3c0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
6c3d0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
6c3e0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
6c3f0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
6c400 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
6c410 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
6c420 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
6c430 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
6c440 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
6c450 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
6c460 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
6c470 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
6c480 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
6c490 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
6c4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6c4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6c4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6c4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
6c4e0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
6c4f0 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6d 6d 6f  ntains the commo
6c500 6e 20 68 65 61 64 65 72 20 66 6f 72 20 61 6c 6c  n header for all
6c510 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74   mutex implement
6c520 61 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73  ations..** The s
6c530 71 6c 69 74 65 49 6e 74 2e 68 20 68 65 61 64 65  qliteInt.h heade
6c540 72 20 23 69 6e 63 6c 75 64 65 73 20 74 68 69 73  r #includes this
6c550 20 66 69 6c 65 20 73 6f 20 74 68 61 74 20 69 74   file so that it
6c560 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a   is available.**
6c570 20 74 6f 20 61 6c 6c 20 73 6f 75 72 63 65 20 66   to all source f
6c580 69 6c 65 73 2e 20 20 57 65 20 62 72 65 61 6b 20  iles.  We break 
6c590 69 74 20 6f 75 74 20 69 6e 20 61 6e 20 65 66 66  it out in an eff
6c5a0 6f 72 74 20 74 6f 20 6b 65 65 70 20 74 68 65 20  ort to keep the 
6c5b0 63 6f 64 65 0a 2a 2a 20 62 65 74 74 65 72 20 6f  code.** better o
6c5c0 72 67 61 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20  rganized..**.** 
6c5d0 4e 4f 54 45 3a 20 20 73 6f 75 72 63 65 20 66 69  NOTE:  source fi
6c5e0 6c 65 73 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a  les should *not*
6c5f0 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73 20 68   #include this h
6c600 65 61 64 65 72 20 66 69 6c 65 20 64 69 72 65 63  eader file direc
6c610 74 6c 79 2e 0a 2a 2a 20 53 6f 75 72 63 65 20 66  tly..** Source f
6c620 69 6c 65 73 20 73 68 6f 75 6c 64 20 23 69 6e 63  iles should #inc
6c630 6c 75 64 65 20 74 68 65 20 73 71 6c 69 74 65 49  lude the sqliteI
6c640 6e 74 2e 68 20 66 69 6c 65 20 61 6e 64 20 6c 65  nt.h file and le
6c650 74 20 74 68 61 74 20 66 69 6c 65 0a 2a 2a 20 69  t that file.** i
6c660 6e 63 6c 75 64 65 20 74 68 69 73 20 6f 6e 65 20  nclude this one 
6c670 69 6e 64 69 72 65 63 74 6c 79 2e 0a 2a 2f 0a 0a  indirectly..*/..
6c680 0a 2f 2a 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75  ./*.** Figure ou
6c690 74 20 77 68 61 74 20 76 65 72 73 69 6f 6e 20 6f  t what version o
6c6a0 66 20 74 68 65 20 63 6f 64 65 20 74 6f 20 75 73  f the code to us
6c6b0 65 2e 20 20 54 68 65 20 63 68 6f 69 63 65 73 20  e.  The choices 
6c6c0 61 72 65 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49  are.**.**   SQLI
6c6d0 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 20 20 20  TE_MUTEX_OMIT   
6c6e0 20 20 20 20 20 20 4e 6f 20 6d 75 74 65 78 20 6c        No mutex l
6c6f0 6f 67 69 63 2e 20 20 4e 6f 74 20 65 76 65 6e 20  ogic.  Not even 
6c700 73 74 75 62 73 2e 20 20 54 68 65 0a 2a 2a 20 20  stubs.  The.**  
6c710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c720 20 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 78             mutex
6c730 65 73 20 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20  es implemention 
6c740 63 61 6e 6e 6f 74 20 62 65 20 6f 76 65 72 72 69  cannot be overri
6c750 64 64 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  dden.**         
6c760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c770 20 20 20 20 61 74 20 73 74 61 72 74 2d 74 69 6d      at start-tim
6c780 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54  e..**.**   SQLIT
6c790 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20  E_MUTEX_NOOP    
6c7a0 20 20 20 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d       For single-
6c7b0 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61  threaded applica
6c7c0 74 69 6f 6e 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20  tions.  No.**   
6c7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c7e0 20 20 20 20 20 20 20 20 20 20 6d 75 74 75 61 6c            mutual
6c7f0 20 65 78 63 6c 75 73 69 6f 6e 20 69 73 20 70 72   exclusion is pr
6c800 6f 76 69 64 65 64 2e 20 20 42 75 74 20 74 68 69  ovided.  But thi
6c810 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
6c820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c830 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
6c840 63 61 6e 20 62 65 20 6f 76 65 72 72 69 64 64 65  can be overridde
6c850 6e 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  n at.**         
6c860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c870 20 20 20 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a      start-time..
6c880 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d  **.**   SQLITE_M
6c890 55 54 45 58 5f 50 54 48 52 45 41 44 53 20 20 20  UTEX_PTHREADS   
6c8a0 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65    For multi-thre
6c8b0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e  aded application
6c8c0 73 20 6f 6e 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a  s on Unix..**.**
6c8d0 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f     SQLITE_MUTEX_
6c8e0 57 33 32 20 20 20 20 20 20 20 20 20 20 46 6f 72  W32          For
6c8f0 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20   multi-threaded 
6c900 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20  applications on 
6c910 57 69 6e 33 32 2e 0a 2a 2f 0a 23 69 66 20 21 53  Win32..*/.#if !S
6c920 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
6c930 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
6c940 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 23 65 6e 64  _MUTEX_OMIT.#end
6c950 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  if.#if SQLITE_TH
6c960 52 45 41 44 53 41 46 45 20 26 26 20 21 64 65 66  READSAFE && !def
6c970 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45  ined(SQLITE_MUTE
6c980 58 5f 4e 4f 4f 50 29 0a 23 20 20 69 66 20 53 51  X_NOOP).#  if SQ
6c990 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23 20 20  LITE_OS_UNIX.#  
6c9a0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
6c9b0 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 0a 23  MUTEX_PTHREADS.#
6c9c0 20 20 65 6c 69 66 20 53 51 4c 49 54 45 5f 4f 53    elif SQLITE_OS
6c9d0 5f 57 49 4e 0a 23 20 20 20 20 64 65 66 69 6e 65  _WIN.#    define
6c9e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33   SQLITE_MUTEX_W3
6c9f0 32 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64  2.#  else.#    d
6ca00 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54  efine SQLITE_MUT
6ca10 45 58 5f 4e 4f 4f 50 0a 23 20 20 65 6e 64 69 66  EX_NOOP.#  endif
6ca20 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
6ca30 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49  SQLITE_MUTEX_OMI
6ca40 54 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20  T./*.** If this 
6ca50 69 73 20 61 20 6e 6f 2d 6f 70 20 69 6d 70 6c 65  is a no-op imple
6ca60 6d 65 6e 74 61 74 69 6f 6e 2c 20 69 6d 70 6c 65  mentation, imple
6ca70 6d 65 6e 74 20 65 76 65 72 79 74 68 69 6e 67 20  ment everything 
6ca80 61 73 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64  as macros..*/.#d
6ca90 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75  efine sqlite3_mu
6caa0 74 65 78 5f 61 6c 6c 6f 63 28 58 29 20 20 20 20  tex_alloc(X)    
6cab0 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a  ((sqlite3_mutex*
6cac0 29 38 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  )8).#define sqli
6cad0 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 58  te3_mutex_free(X
6cae0 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ).#define sqlite
6caf0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 58 29  3_mutex_enter(X)
6cb00 20 20 20 20 0a 23 64 65 66 69 6e 65 20 73 71 6c      .#define sql
6cb10 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 58  ite3_mutex_try(X
6cb20 29 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b  )      SQLITE_OK
6cb30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
6cb40 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 58 29 20  _mutex_leave(X) 
6cb50 20 20 20 0a 23 64 65 66 69 6e 65 20 73 71 6c 69     .#define sqli
6cb60 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 58  te3_mutex_held(X
6cb70 29 20 20 20 20 20 28 28 76 6f 69 64 29 28 58 29  )     ((void)(X)
6cb80 2c 31 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ,1).#define sqli
6cb90 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
6cba0 64 28 58 29 20 20 28 28 76 6f 69 64 29 28 58 29  d(X)  ((void)(X)
6cbb0 2c 31 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ,1).#define sqli
6cbc0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 58 29  te3MutexAlloc(X)
6cbd0 20 20 20 20 20 20 28 28 73 71 6c 69 74 65 33 5f        ((sqlite3_
6cbe0 6d 75 74 65 78 2a 29 38 29 0a 23 64 65 66 69 6e  mutex*)8).#defin
6cbf0 65 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 6e  e sqlite3MutexIn
6cc00 69 74 28 29 20 20 20 20 20 20 20 20 53 51 4c 49  it()        SQLI
6cc10 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71  TE_OK.#define sq
6cc20 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28 29 0a  lite3MutexEnd().
6cc30 23 64 65 66 69 6e 65 20 4d 55 54 45 58 5f 4c 4f  #define MUTEX_LO
6cc40 47 49 43 28 58 29 0a 23 65 6c 73 65 0a 23 64 65  GIC(X).#else.#de
6cc50 66 69 6e 65 20 4d 55 54 45 58 5f 4c 4f 47 49 43  fine MUTEX_LOGIC
6cc60 28 58 29 20 20 20 20 20 20 20 20 20 20 20 20 58  (X)            X
6cc70 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e  .#endif /* defin
6cc80 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ed(SQLITE_MUTEX_
6cc90 4f 4d 49 54 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  OMIT) */../*****
6cca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
6ccb0 20 6d 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a   mutex.h *******
6ccc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6ccd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
6cce0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
6ccf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
6cd00 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
6cd10 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
6cd20 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
6cd30 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a  ********/.../*.*
6cd40 2a 20 45 61 63 68 20 64 61 74 61 62 61 73 65 20  * Each database 
6cd50 66 69 6c 65 20 74 6f 20 62 65 20 61 63 63 65 73  file to be acces
6cd60 73 65 64 20 62 79 20 74 68 65 20 73 79 73 74 65  sed by the syste
6cd70 6d 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  m is an instance
6cd80 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  .** of the follo
6cd90 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 20  wing structure. 
6cda0 20 54 68 65 72 65 20 61 72 65 20 6e 6f 72 6d 61   There are norma
6cdb0 6c 6c 79 20 74 77 6f 20 6f 66 20 74 68 65 73 65  lly two of these
6cdc0 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 69   structures.** i
6cdd0 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61 44 62  n the sqlite.aDb
6cde0 5b 5d 20 61 72 72 61 79 2e 20 20 61 44 62 5b 30  [] array.  aDb[0
6cdf0 5d 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61  ] is the main da
6ce00 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 0a  tabase file and.
6ce10 2a 2a 20 61 44 62 5b 31 5d 20 69 73 20 74 68 65  ** aDb[1] is the
6ce20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75   database file u
6ce30 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 65 6d 70  sed to hold temp
6ce40 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 41  orary tables.  A
6ce50 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 61 74  dditional.** dat
6ce60 61 62 61 73 65 73 20 6d 61 79 20 62 65 20 61 74  abases may be at
6ce70 74 61 63 68 65 64 2e 0a 2a 2f 0a 73 74 72 75 63  tached..*/.struc
6ce80 74 20 44 62 20 7b 0a 20 20 63 68 61 72 20 2a 7a  t Db {.  char *z
6ce90 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f 2a  Name;         /*
6cea0 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 64 61   Name of this da
6ceb0 74 61 62 61 73 65 20 2a 2f 0a 20 20 42 74 72 65  tabase */.  Btre
6cec0 65 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20  e *pBt;         
6ced0 20 2f 2a 20 54 68 65 20 42 2a 54 72 65 65 20 73   /* The B*Tree s
6cee0 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 69  tructure for thi
6cef0 73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  s database file 
6cf00 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 3b  */.  u8 inTrans;
6cf10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 3a 20            /* 0: 
6cf20 6e 6f 74 20 77 72 69 74 61 62 6c 65 2e 20 20 31  not writable.  1
6cf30 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  : Transaction.  
6cf40 32 3a 20 43 68 65 63 6b 70 6f 69 6e 74 20 2a 2f  2: Checkpoint */
6cf50 0a 20 20 75 38 20 73 61 66 65 74 79 5f 6c 65 76  .  u8 safety_lev
6cf60 65 6c 3b 20 20 20 20 20 2f 2a 20 48 6f 77 20 61  el;     /* How a
6cf70 67 67 72 65 73 73 69 76 65 20 61 74 20 73 79 6e  ggressive at syn
6cf80 63 69 6e 67 20 64 61 74 61 20 74 6f 20 64 69 73  cing data to dis
6cf90 6b 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70  k */.  Schema *p
6cfa0 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 50  Schema;     /* P
6cfb0 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 62 61  ointer to databa
6cfc0 73 65 20 73 63 68 65 6d 61 20 28 70 6f 73 73 69  se schema (possi
6cfd0 62 6c 79 20 73 68 61 72 65 64 29 20 2a 2f 0a 7d  bly shared) */.}
6cfe0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  ;../*.** An inst
6cff0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
6d000 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
6d010 73 74 6f 72 65 73 20 61 20 64 61 74 61 62 61 73  stores a databas
6d020 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20  e schema..**.** 
6d030 4d 6f 73 74 20 53 63 68 65 6d 61 20 6f 62 6a 65  Most Schema obje
6d040 63 74 73 20 61 72 65 20 61 73 73 6f 63 69 61 74  cts are associat
6d050 65 64 20 77 69 74 68 20 61 20 42 74 72 65 65 2e  ed with a Btree.
6d060 20 20 54 68 65 20 65 78 63 65 70 74 69 6f 6e 20    The exception 
6d070 69 73 0a 2a 2a 20 74 68 65 20 53 63 68 65 6d 61  is.** the Schema
6d080 20 66 6f 72 20 74 68 65 20 54 45 4d 50 20 64 61   for the TEMP da
6d090 74 61 62 61 65 73 20 28 73 71 6c 69 74 65 33 2e  tabaes (sqlite3.
6d0a0 61 44 62 5b 31 5d 29 20 77 68 69 63 68 20 69 73  aDb[1]) which is
6d0b0 20 66 72 65 65 2d 73 74 61 6e 64 69 6e 67 2e 0a   free-standing..
6d0c0 2a 2a 20 49 6e 20 73 68 61 72 65 64 20 63 61 63  ** In shared cac
6d0d0 68 65 20 6d 6f 64 65 2c 20 61 20 73 69 6e 67 6c  he mode, a singl
6d0e0 65 20 53 63 68 65 6d 61 20 6f 62 6a 65 63 74 20  e Schema object 
6d0f0 63 61 6e 20 62 65 20 73 68 61 72 65 64 20 62 79  can be shared by
6d100 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 42 74 72   multiple.** Btr
6d110 65 65 73 20 74 68 61 74 20 72 65 66 65 72 20 74  ees that refer t
6d120 6f 20 74 68 65 20 73 61 6d 65 20 75 6e 64 65 72  o the same under
6d130 6c 79 69 6e 67 20 42 74 53 68 61 72 65 64 20 6f  lying BtShared o
6d140 62 6a 65 63 74 2e 0a 2a 2a 20 0a 2a 2a 20 53 63  bject..** .** Sc
6d150 68 65 6d 61 20 6f 62 6a 65 63 74 73 20 61 72 65  hema objects are
6d160 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64   automatically d
6d170 65 61 6c 6c 6f 63 61 74 65 64 20 77 68 65 6e 20  eallocated when 
6d180 74 68 65 20 6c 61 73 74 20 42 74 72 65 65 20 74  the last Btree t
6d190 68 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65  hat.** reference
6d1a0 73 20 74 68 65 6d 20 69 73 20 64 65 73 74 72 6f  s them is destro
6d1b0 79 65 64 2e 20 20 20 54 68 65 20 54 45 4d 50 20  yed.   The TEMP 
6d1c0 53 63 68 65 6d 61 20 69 73 20 6d 61 6e 75 61 6c  Schema is manual
6d1d0 6c 79 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 73  ly freed by.** s
6d1e0 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a  qlite3_close()..
6d1f0 2a 0a 2a 2a 20 41 20 74 68 72 65 61 64 20 6d 75  *.** A thread mu
6d200 73 74 20 62 65 20 68 6f 6c 64 69 6e 67 20 61 20  st be holding a 
6d210 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 63 6f 72  mutex on the cor
6d220 72 65 73 70 6f 6e 64 69 6e 67 20 42 74 72 65 65  responding Btree
6d230 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20   in order.** to 
6d240 61 63 63 65 73 73 20 53 63 68 65 6d 61 20 63 6f  access Schema co
6d250 6e 74 65 6e 74 2e 20 20 54 68 69 73 20 69 6d 70  ntent.  This imp
6d260 6c 69 65 73 20 74 68 61 74 20 74 68 65 20 74 68  lies that the th
6d270 72 65 61 64 20 6d 75 73 74 20 61 6c 73 6f 20 62  read must also b
6d280 65 0a 2a 2a 20 68 6f 6c 64 69 6e 67 20 61 20 6d  e.** holding a m
6d290 75 74 65 78 20 6f 6e 20 74 68 65 20 73 71 6c 69  utex on the sqli
6d2a0 74 65 33 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  te3 connection p
6d2b0 6f 69 6e 74 65 72 20 74 68 61 74 20 6f 77 6e 73  ointer that owns
6d2c0 20 74 68 65 20 42 74 72 65 65 2e 0a 2a 2a 20 46   the Btree..** F
6d2d0 6f 72 20 61 20 54 45 4d 50 20 53 63 68 65 6d 61  or a TEMP Schema
6d2e0 2c 20 6f 6e 6c 79 20 74 68 65 20 63 6f 6e 6e 65  , only the conne
6d2f0 63 74 69 6f 6e 20 6d 75 74 65 78 20 69 73 20 72  ction mutex is r
6d300 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 72 75  equired..*/.stru
6d310 63 74 20 53 63 68 65 6d 61 20 7b 0a 20 20 69 6e  ct Schema {.  in
6d320 74 20 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b  t schema_cookie;
6d330 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 73     /* Database s
6d340 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 6e 75  chema version nu
6d350 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 66 69  mber for this fi
6d360 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 47 65 6e  le */.  int iGen
6d370 65 72 61 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20  eration;     /* 
6d380 47 65 6e 65 72 61 74 69 6f 6e 20 63 6f 75 6e 74  Generation count
6d390 65 72 2e 20 20 49 6e 63 72 65 6d 65 6e 74 65 64  er.  Incremented
6d3a0 20 77 69 74 68 20 65 61 63 68 20 63 68 61 6e 67   with each chang
6d3b0 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 62 6c 48  e */.  Hash tblH
6d3c0 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 41  ash;        /* A
6d3d0 6c 6c 20 74 61 62 6c 65 73 20 69 6e 64 65 78 65  ll tables indexe
6d3e0 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48  d by name */.  H
6d3f0 61 73 68 20 69 64 78 48 61 73 68 3b 20 20 20 20  ash idxHash;    
6d400 20 20 20 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65      /* All (name
6d410 64 29 20 69 6e 64 69 63 65 73 20 69 6e 64 65 78  d) indices index
6d420 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20  ed by name */.  
6d430 48 61 73 68 20 74 72 69 67 48 61 73 68 3b 20 20  Hash trigHash;  
6d440 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67       /* All trig
6d450 67 65 72 73 20 69 6e 64 65 78 65 64 20 62 79 20  gers indexed by 
6d460 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 66  name */.  Hash f
6d470 6b 65 79 48 61 73 68 3b 20 20 20 20 20 20 20 2f  keyHash;       /
6d480 2a 20 41 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65  * All foreign ke
6d490 79 73 20 62 79 20 72 65 66 65 72 65 6e 63 65 64  ys by referenced
6d4a0 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20   table name */. 
6d4b0 20 54 61 62 6c 65 20 2a 70 53 65 71 54 61 62 3b   Table *pSeqTab;
6d4c0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c        /* The sql
6d4d0 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62  ite_sequence tab
6d4e0 6c 65 20 75 73 65 64 20 62 79 20 41 55 54 4f 49  le used by AUTOI
6d4f0 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 75 38  NCREMENT */.  u8
6d500 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20   file_format;   
6d510 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 66 6f 72     /* Schema for
6d520 6d 61 74 20 76 65 72 73 69 6f 6e 20 66 6f 72 20  mat version for 
6d530 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75  this file */.  u
6d540 38 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20  8 enc;          
6d550 20 20 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f      /* Text enco
6d560 64 69 6e 67 20 75 73 65 64 20 62 79 20 74 68 69  ding used by thi
6d570 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  s database */.  
6d580 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20  u16 flags;      
6d590 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73       /* Flags as
6d5a0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
6d5b0 69 73 20 73 63 68 65 6d 61 20 2a 2f 0a 20 20 69  is schema */.  i
6d5c0 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 3b 20 20  nt cache_size;  
6d5d0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6d5e0 20 70 61 67 65 73 20 74 6f 20 75 73 65 20 69 6e   pages to use in
6d5f0 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 7d 3b   the cache */.};
6d600 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61  ../*.** These ma
6d610 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64  cros can be used
6d620 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 6f   to test, set, o
6d630 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e 20  r clear bits in 
6d640 74 68 65 20 0a 2a 2a 20 44 62 2e 70 53 63 68 65  the .** Db.pSche
6d650 6d 61 2d 3e 66 6c 61 67 73 20 66 69 65 6c 64 2e  ma->flags field.
6d660 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 62 48 61  .*/.#define DbHa
6d670 73 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29  sProperty(D,I,P)
6d680 20 20 20 20 20 28 28 28 44 29 2d 3e 61 44 62 5b       (((D)->aDb[
6d690 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67  I].pSchema->flag
6d6a0 73 26 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65  s&(P))==(P)).#de
6d6b0 66 69 6e 65 20 44 62 48 61 73 41 6e 79 50 72 6f  fine DbHasAnyPro
6d6c0 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 28 28  perty(D,I,P)  ((
6d6d0 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68  (D)->aDb[I].pSch
6d6e0 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 21  ema->flags&(P))!
6d6f0 3d 30 29 0a 23 64 65 66 69 6e 65 20 44 62 53 65  =0).#define DbSe
6d700 74 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29  tProperty(D,I,P)
6d710 20 20 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d       (D)->aDb[I]
6d720 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 7c  .pSchema->flags|
6d730 3d 28 50 29 0a 23 64 65 66 69 6e 65 20 44 62 43  =(P).#define DbC
6d740 6c 65 61 72 50 72 6f 70 65 72 74 79 28 44 2c 49  learProperty(D,I
6d750 2c 50 29 20 20 20 28 44 29 2d 3e 61 44 62 5b 49  ,P)   (D)->aDb[I
6d760 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73  ].pSchema->flags
6d770 26 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c  &=~(P)../*.** Al
6d780 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72  lowed values for
6d790 20 74 68 65 20 44 42 2e 70 53 63 68 65 6d 61 2d   the DB.pSchema-
6d7a0 3e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a  >flags field..**
6d7b0 0a 2a 2a 20 54 68 65 20 44 42 5f 53 63 68 65 6d  .** The DB_Schem
6d7c0 61 4c 6f 61 64 65 64 20 66 6c 61 67 20 69 73 20  aLoaded flag is 
6d7d0 73 65 74 20 61 66 74 65 72 20 74 68 65 20 64 61  set after the da
6d7e0 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61  tabase schema ha
6d7f0 73 20 62 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69  s been.** read i
6d800 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73  nto internal has
6d810 68 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  h tables..**.** 
6d820 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20  DB_UnresetViews 
6d830 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f  means that one o
6d840 72 20 6d 6f 72 65 20 76 69 65 77 73 20 68 61 76  r more views hav
6d850 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74  e column names t
6d860 68 61 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e  hat.** have been
6d870 20 66 69 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66   filled out.  If
6d880 20 74 68 65 20 73 63 68 65 6d 61 20 63 68 61 6e   the schema chan
6d890 67 65 73 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d  ges, these colum
6d8a0 6e 20 6e 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a  n names might.**
6d8b0 20 63 68 61 6e 67 65 73 20 61 6e 64 20 73 6f 20   changes and so 
6d8c0 74 68 65 20 76 69 65 77 20 77 69 6c 6c 20 6e 65  the view will ne
6d8d0 65 64 20 74 6f 20 62 65 20 72 65 73 65 74 2e 0a  ed to be reset..
6d8e0 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 53 63  */.#define DB_Sc
6d8f0 68 65 6d 61 4c 6f 61 64 65 64 20 20 20 20 30 78  hemaLoaded    0x
6d900 30 30 30 31 20 20 2f 2a 20 54 68 65 20 73 63 68  0001  /* The sch
6d910 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61  ema has been loa
6d920 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44  ded */.#define D
6d930 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 20  B_UnresetViews  
6d940 20 20 30 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d    0x0002  /* Som
6d950 65 20 76 69 65 77 73 20 68 61 76 65 20 64 65 66  e views have def
6d960 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  ined column name
6d970 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f  s */.#define DB_
6d980 45 6d 70 74 79 20 20 20 20 20 20 20 20 20 20 20  Empty           
6d990 30 78 30 30 30 34 20 20 2f 2a 20 54 68 65 20 66  0x0004  /* The f
6d9a0 69 6c 65 20 69 73 20 65 6d 70 74 79 20 28 6c 65  ile is empty (le
6d9b0 6e 67 74 68 20 30 20 62 79 74 65 73 29 20 2a 2f  ngth 0 bytes) */
6d9c0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62  ../*.** The numb
6d9d0 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20  er of different 
6d9e0 6b 69 6e 64 73 20 6f 66 20 74 68 69 6e 67 73 20  kinds of things 
6d9f0 74 68 61 74 20 63 61 6e 20 62 65 20 6c 69 6d 69  that can be limi
6da00 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65  ted.** using the
6da10 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29   sqlite3_limit()
6da20 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23   interface..*/.#
6da30 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f  define SQLITE_N_
6da40 4c 49 4d 49 54 20 28 53 51 4c 49 54 45 5f 4c 49  LIMIT (SQLITE_LI
6da50 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54  MIT_TRIGGER_DEPT
6da60 48 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b  H+1)../*.** Look
6da70 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69 73 20  aside malloc is 
6da80 61 20 73 65 74 20 6f 66 20 66 69 78 65 64 2d 73  a set of fixed-s
6da90 69 7a 65 20 62 75 66 66 65 72 73 20 74 68 61 74  ize buffers that
6daa0 20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20   can be used.** 
6dab0 74 6f 20 73 61 74 69 73 66 79 20 73 6d 61 6c 6c  to satisfy small
6dac0 20 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d 6f 72   transient memor
6dad0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71  y allocation req
6dae0 75 65 73 74 73 20 66 6f 72 20 6f 62 6a 65 63 74  uests for object
6daf0 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20  s.** associated 
6db00 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61  with a particula
6db10 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
6db20 63 74 69 6f 6e 2e 20 20 54 68 65 20 75 73 65 20  ction.  The use 
6db30 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20  of.** lookaside 
6db40 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65 73 20  malloc provides 
6db50 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20 70 65  a significant pe
6db60 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61 6e 63  rformance enhanc
6db70 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72 6f 78  ement.** (approx
6db80 20 31 30 25 29 20 62 79 20 61 76 6f 69 64 69 6e   10%) by avoidin
6db90 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c 6c 6f  g numerous mallo
6dba0 63 2f 66 72 65 65 20 72 65 71 75 65 73 74 73 20  c/free requests 
6dbb0 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a  while parsing.**
6dbc0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e   SQL statements.
6dbd0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f 6b 61  .**.** The Looka
6dbe0 73 69 64 65 20 73 74 72 75 63 74 75 72 65 20 68  side structure h
6dbf0 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 61 74 69  olds configurati
6dc00 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  on information a
6dc10 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b  bout the.** look
6dc20 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73 75 62  aside malloc sub
6dc30 73 79 73 74 65 6d 2e 20 20 45 61 63 68 20 61 76  system.  Each av
6dc40 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 61  ailable memory a
6dc50 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a 2a 20  llocation in.** 
6dc60 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 73 75  the lookaside su
6dc70 62 73 79 73 74 65 6d 20 69 73 20 73 74 6f 72 65  bsystem is store
6dc80 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69  d on a linked li
6dc90 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64 65 53  st of LookasideS
6dca0 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 0a  lot.** objects..
6dcb0 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20  **.** Lookaside 
6dcc0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20  allocations are 
6dcd0 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 66 6f 72  only allowed for
6dce0 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72   objects that ar
6dcf0 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20  e associated.** 
6dd00 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61  with a particula
6dd10 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
6dd20 63 74 69 6f 6e 2e 20 20 48 65 6e 63 65 2c 20 73  ction.  Hence, s
6dd30 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f  chema informatio
6dd40 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 73  n cannot.** be s
6dd50 74 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b 61 73 69  tored in lookasi
6dd60 64 65 20 62 65 63 61 75 73 65 20 69 6e 20 73 68  de because in sh
6dd70 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 20  ared cache mode 
6dd80 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66 6f 72  the schema infor
6dd90 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 68 61  mation.** is sha
6dda0 72 65 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20  red by multiple 
6ddb0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
6ddc0 69 6f 6e 73 2e 20 20 54 68 65 72 65 66 6f 72 65  ions.  Therefore
6ddd0 2c 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a  , while parsing.
6dde0 2a 2a 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d  ** schema inform
6ddf0 61 74 69 6f 6e 2c 20 74 68 65 20 4c 6f 6f 6b 61  ation, the Looka
6de00 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 66 6c  side.bEnabled fl
6de10 61 67 20 69 73 20 63 6c 65 61 72 65 64 20 73 6f  ag is cleared so
6de20 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b 61 73 69   that.** lookasi
6de30 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61  de allocations a
6de40 72 65 20 6e 6f 74 20 75 73 65 64 20 74 6f 20 63  re not used to c
6de50 6f 6e 73 74 72 75 63 74 20 74 68 65 20 73 63 68  onstruct the sch
6de60 65 6d 61 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a  ema objects..*/.
6de70 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65  struct Lookaside
6de80 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20 20 20 20   {.  u16 sz;    
6de90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6dea0 53 69 7a 65 20 6f 66 20 65 61 63 68 20 62 75 66  Size of each buf
6deb0 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  fer in bytes */.
6dec0 20 20 75 38 20 62 45 6e 61 62 6c 65 64 3b 20 20    u8 bEnabled;  
6ded0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c            /* Fal
6dee0 73 65 20 74 6f 20 64 69 73 61 62 6c 65 20 6e 65  se to disable ne
6def0 77 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f  w lookaside allo
6df00 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 38 20  cations */.  u8 
6df10 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 20 20 20  bMalloced;      
6df20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
6df30 70 53 74 61 72 74 20 6f 62 74 61 69 6e 65 64 20  pStart obtained 
6df40 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
6df50 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e  loc() */.  int n
6df60 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  Out;            
6df70 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
6df80 62 75 66 66 65 72 73 20 63 75 72 72 65 6e 74 6c  buffers currentl
6df90 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f  y checked out */
6dfa0 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b 20 20 20  .  int mxOut;   
6dfb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 69             /* Hi
6dfc0 67 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72  ghwater mark for
6dfd0 20 6e 4f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 61   nOut */.  int a
6dfe0 6e 53 74 61 74 5b 33 5d 3b 20 20 20 20 20 20 20  nStat[3];       
6dff0 20 20 20 2f 2a 20 30 3a 20 68 69 74 73 2e 20 20     /* 0: hits.  
6e000 31 3a 20 73 69 7a 65 20 6d 69 73 73 65 73 2e 20  1: size misses. 
6e010 20 32 3a 20 66 75 6c 6c 20 6d 69 73 73 65 73 20   2: full misses 
6e020 2a 2f 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c  */.  LookasideSl
6e030 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 2f 2a 20  ot *pFree;   /* 
6e040 4c 69 73 74 20 6f 66 20 61 76 61 69 6c 61 62 6c  List of availabl
6e050 65 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 20 76  e buffers */.  v
6e060 6f 69 64 20 2a 70 53 74 61 72 74 3b 20 20 20 20  oid *pStart;    
6e070 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
6e080 62 79 74 65 20 6f 66 20 61 76 61 69 6c 61 62 6c  byte of availabl
6e090 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 2a  e memory space *
6e0a0 2f 0a 20 20 76 6f 69 64 20 2a 70 45 6e 64 3b 20  /.  void *pEnd; 
6e0b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
6e0c0 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 65  irst byte past e
6e0d0 6e 64 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20  nd of available 
6e0e0 73 70 61 63 65 20 2a 2f 0a 7d 3b 0a 73 74 72 75  space */.};.stru
6e0f0 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74  ct LookasideSlot
6e100 20 7b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c   {.  LookasideSl
6e110 6f 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f 2a  ot *pNext;    /*
6e120 20 4e 65 78 74 20 62 75 66 66 65 72 20 69 6e 20   Next buffer in 
6e130 74 68 65 20 6c 69 73 74 20 6f 66 20 66 72 65 65  the list of free
6e140 20 62 75 66 66 65 72 73 20 2a 2f 0a 7d 3b 0a 0a   buffers */.};..
6e150 2f 2a 0a 2a 2a 20 41 20 68 61 73 68 20 74 61 62  /*.** A hash tab
6e160 6c 65 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20  le for function 
6e170 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 0a  definitions..**.
6e180 2a 2a 20 48 61 73 68 20 65 61 63 68 20 46 75 6e  ** Hash each Fun
6e190 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 69  cDef structure i
6e1a0 6e 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 46  nto one of the F
6e1b0 75 6e 63 44 65 66 48 61 73 68 2e 61 5b 5d 20 73  uncDefHash.a[] s
6e1c0 6c 6f 74 73 2e 0a 2a 2a 20 43 6f 6c 6c 69 73 69  lots..** Collisi
6e1d0 6f 6e 73 20 61 72 65 20 6f 6e 20 74 68 65 20 46  ons are on the F
6e1e0 75 6e 63 44 65 66 2e 70 48 61 73 68 20 63 68 61  uncDef.pHash cha
6e1f0 69 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75  in..*/.struct Fu
6e200 6e 63 44 65 66 48 61 73 68 20 7b 0a 20 20 46 75  ncDefHash {.  Fu
6e210 6e 63 44 65 66 20 2a 61 5b 32 33 5d 3b 20 20 20  ncDef *a[23];   
6e220 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c      /* Hash tabl
6e230 65 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20  e for functions 
6e240 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  */.};../*.** Eac
6e250 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  h database conne
6e260 63 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73 74  ction is an inst
6e270 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
6e280 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
6e290 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74  .*/.struct sqlit
6e2a0 65 33 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  e3 {.  sqlite3_v
6e2b0 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20  fs *pVfs;       
6e2c0 20 20 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72       /* OS Inter
6e2d0 66 61 63 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  face */.  struct
6e2e0 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 20 20   Vdbe *pVdbe;   
6e2f0 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
6e300 6f 66 20 61 63 74 69 76 65 20 76 69 72 74 75 61  of active virtua
6e310 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f 0a 20 20  l machines */.  
6e320 43 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43 6f  CollSeq *pDfltCo
6e330 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ll;           /*
6e340 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c   The default col
6e350 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
6e360 28 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20 73 71  (BINARY) */.  sq
6e370 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
6e380 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  ex;         /* C
6e390 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 74 65 78 20  onnection mutex 
6e3a0 2a 2f 0a 20 20 44 62 20 2a 61 44 62 3b 20 20 20  */.  Db *aDb;   
6e3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e3c0 20 20 20 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e     /* All backen
6e3d0 64 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b  ds */.  int nDb;
6e3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e3f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6e400 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 75 72 72  of backends curr
6e410 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a  ently in use */.
6e420 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20    int flags;    
6e430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e440 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 65 6f 75 73  /* Miscellaneous
6e450 20 66 6c 61 67 73 2e 20 53 65 65 20 62 65 6c 6f   flags. See belo
6e460 77 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52  w */.  i64 lastR
6e470 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 20  owid;           
6e480 20 20 20 20 20 2f 2a 20 52 4f 57 49 44 20 6f 66       /* ROWID of
6e490 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 73   most recent ins
6e4a0 65 72 74 20 28 73 65 65 20 61 62 6f 76 65 29 20  ert (see above) 
6e4b0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  */.  unsigned in
6e4c0 74 20 6f 70 65 6e 46 6c 61 67 73 3b 20 20 20 20  t openFlags;    
6e4d0 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73     /* Flags pass
6e4e0 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66  ed to sqlite3_vf
6e4f0 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69  s.xOpen() */.  i
6e500 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20  nt errCode;     
6e510 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6e520 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f  Most recent erro
6e530 72 20 63 6f 64 65 20 28 53 51 4c 49 54 45 5f 2a  r code (SQLITE_*
6e540 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 4d 61  ) */.  int errMa
6e550 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sk;             
6e560 20 20 20 20 20 2f 2a 20 26 20 72 65 73 75 6c 74       /* & result
6e570 20 63 6f 64 65 73 20 77 69 74 68 20 74 68 69 73   codes with this
6e580 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
6e590 67 20 2a 2f 0a 20 20 75 31 36 20 64 62 4f 70 74  g */.  u16 dbOpt
6e5a0 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  Flags;          
6e5b0 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 6f       /* Flags to
6e5c0 20 65 6e 61 62 6c 65 2f 64 69 73 61 62 6c 65 20   enable/disable 
6e5d0 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 2a 2f  optimizations */
6e5e0 0a 20 20 75 38 20 61 75 74 6f 43 6f 6d 6d 69 74  .  u8 autoCommit
6e5f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6e600 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d   /* The auto-com
6e610 6d 69 74 20 66 6c 61 67 2e 20 2a 2f 0a 20 20 75  mit flag. */.  u
6e620 38 20 74 65 6d 70 5f 73 74 6f 72 65 3b 20 20 20  8 temp_store;   
6e630 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6e640 31 3a 20 66 69 6c 65 20 32 3a 20 6d 65 6d 6f 72  1: file 2: memor
6e650 79 20 30 3a 20 64 65 66 61 75 6c 74 20 2a 2f 0a  y 0: default */.
6e660 20 20 75 38 20 6d 61 6c 6c 6f 63 46 61 69 6c 65    u8 mallocFaile
6e670 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
6e680 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 68 61  /* True if we ha
6e690 76 65 20 73 65 65 6e 20 61 20 6d 61 6c 6c 6f 63  ve seen a malloc
6e6a0 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20 75 38   failure */.  u8
6e6b0 20 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 3b 20 20   dfltLockMode;  
6e6c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
6e6d0 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 2d 6d  efault locking-m
6e6e0 6f 64 65 20 66 6f 72 20 61 74 74 61 63 68 65 64  ode for attached
6e6f0 20 64 62 73 20 2a 2f 0a 20 20 73 69 67 6e 65 64   dbs */.  signed
6e700 20 63 68 61 72 20 6e 65 78 74 41 75 74 6f 76 61   char nextAutova
6e710 63 3b 20 20 20 20 20 20 2f 2a 20 41 75 74 6f 76  c;      /* Autov
6e720 61 63 20 73 65 74 74 69 6e 67 20 61 66 74 65 72  ac setting after
6e730 20 56 41 43 55 55 4d 20 69 66 20 3e 3d 30 20 2a   VACUUM if >=0 *
6e740 2f 0a 20 20 75 38 20 73 75 70 70 72 65 73 73 45  /.  u8 suppressE
6e750 72 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rr;             
6e760 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 69 73 73 75    /* Do not issu
6e770 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  e error messages
6e780 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 38   if true */.  u8
6e790 20 76 74 61 62 4f 6e 43 6f 6e 66 6c 69 63 74 3b   vtabOnConflict;
6e7a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
6e7b0 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 66  alue to return f
6e7c0 6f 72 20 73 33 5f 76 74 61 62 5f 6f 6e 5f 63 6f  or s3_vtab_on_co
6e7d0 6e 66 6c 69 63 74 28 29 20 2a 2f 0a 20 20 75 38  nflict() */.  u8
6e7e0 20 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61   isTransactionSa
6e7f0 76 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 54  vepoint;    /* T
6e800 72 75 65 20 69 66 20 74 68 65 20 6f 75 74 65 72  rue if the outer
6e810 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 20 69  most savepoint i
6e820 73 20 61 20 54 53 20 2a 2f 0a 20 20 69 6e 74 20  s a TS */.  int 
6e830 6e 65 78 74 50 61 67 65 73 69 7a 65 3b 20 20 20  nextPagesize;   
6e840 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
6e850 65 73 69 7a 65 20 61 66 74 65 72 20 56 41 43 55  esize after VACU
6e860 55 4d 20 69 66 20 3e 30 20 2a 2f 0a 20 20 75 33  UM if >0 */.  u3
6e870 32 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20  2 magic;        
6e880 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
6e890 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20  agic number for 
6e8a0 64 65 74 65 63 74 20 6c 69 62 72 61 72 79 20 6d  detect library m
6e8b0 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  isuse */.  int n
6e8c0 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20  Change;         
6e8d0 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
6e8e0 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  e returned by sq
6e8f0 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20  lite3_changes() 
6e900 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 61 6c 43  */.  int nTotalC
6e910 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20  hange;          
6e920 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75     /* Value retu
6e930 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
6e940 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20  total_changes() 
6e950 2a 2f 0a 20 20 69 6e 74 20 61 4c 69 6d 69 74 5b  */.  int aLimit[
6e960 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 5d 3b  SQLITE_N_LIMIT];
6e970 20 20 20 2f 2a 20 4c 69 6d 69 74 73 20 2a 2f 0a     /* Limits */.
6e980 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
6e990 49 6e 69 74 49 6e 66 6f 20 7b 20 20 20 20 20 20  InitInfo {      
6e9a0 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75  /* Information u
6e9b0 73 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69  sed during initi
6e9c0 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 20 20  alization */.   
6e9d0 20 69 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20   int newTnum;   
6e9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6e9f0 52 6f 6f 74 70 61 67 65 20 6f 66 20 74 61 62 6c  Rootpage of tabl
6ea00 65 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69  e being initiali
6ea10 7a 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 69 44  zed */.    u8 iD
6ea20 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
6ea30 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20         /* Which 
6ea40 64 62 20 66 69 6c 65 20 69 73 20 62 65 69 6e 67  db file is being
6ea50 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
6ea60 20 20 20 20 75 38 20 62 75 73 79 3b 20 20 20 20      u8 busy;    
6ea70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ea80 2f 2a 20 54 52 55 45 20 69 66 20 63 75 72 72 65  /* TRUE if curre
6ea90 6e 74 6c 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e  ntly initializin
6eaa0 67 20 2a 2f 0a 20 20 20 20 75 38 20 6f 72 70 68  g */.    u8 orph
6eab0 61 6e 54 72 69 67 67 65 72 3b 20 20 20 20 20 20  anTrigger;      
6eac0 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 73 74 61       /* Last sta
6ead0 74 65 6d 65 6e 74 20 69 73 20 6f 72 70 68 61 6e  tement is orphan
6eae0 65 64 20 54 45 4d 50 20 74 72 69 67 67 65 72 20  ed TEMP trigger 
6eaf0 2a 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a 20 20 69  */.  } init;.  i
6eb00 6e 74 20 61 63 74 69 76 65 56 64 62 65 43 6e 74  nt activeVdbeCnt
6eb10 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
6eb20 4e 75 6d 62 65 72 20 6f 66 20 56 44 42 45 73 20  Number of VDBEs 
6eb30 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74  currently execut
6eb40 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 77 72 69  ing */.  int wri
6eb50 74 65 56 64 62 65 43 6e 74 3b 20 20 20 20 20 20  teVdbeCnt;      
6eb60 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6eb70 20 6f 66 20 61 63 74 69 76 65 20 56 44 42 45 73   of active VDBEs
6eb80 20 74 68 61 74 20 61 72 65 20 77 72 69 74 69 6e   that are writin
6eb90 67 20 2a 2f 0a 20 20 69 6e 74 20 76 64 62 65 45  g */.  int vdbeE
6eba0 78 65 63 43 6e 74 3b 20 20 20 20 20 20 20 20 20  xecCnt;         
6ebb0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6ebc0 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20 74  f nested calls t
6ebd0 6f 20 56 64 62 65 45 78 65 63 28 29 20 2a 2f 0a  o VdbeExec() */.
6ebe0 20 20 69 6e 74 20 6e 45 78 74 65 6e 73 69 6f 6e    int nExtension
6ebf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6ec00 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 61  /* Number of loa
6ec10 64 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 2a  ded extensions *
6ec20 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 45 78 74 65  /.  void **aExte
6ec30 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20  nsion;          
6ec40 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 68    /* Array of sh
6ec50 61 72 65 64 20 6c 69 62 72 61 72 79 20 68 61 6e  ared library han
6ec60 64 6c 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28  dles */.  void (
6ec70 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63  *xTrace)(void*,c
6ec80 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20 20 20 20  onst char*);    
6ec90 20 20 20 20 2f 2a 20 54 72 61 63 65 20 66 75 6e      /* Trace fun
6eca0 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  ction */.  void 
6ecb0 2a 70 54 72 61 63 65 41 72 67 3b 20 20 20 20 20  *pTraceArg;     
6ecc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ecd0 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
6ece0 20 74 6f 20 74 68 65 20 74 72 61 63 65 20 66 75   to the trace fu
6ecf0 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64  nction */.  void
6ed00 20 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f 69   (*xProfile)(voi
6ed10 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75  d*,const char*,u
6ed20 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c 69  64);  /* Profili
6ed30 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  ng function */. 
6ed40 20 76 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65 41   void *pProfileA
6ed50 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
6ed60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
6ed70 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69 6c  gument to profil
6ed80 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  e function */.  
6ed90 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72 67  void *pCommitArg
6eda0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6edb0 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
6edc0 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b   xCommitCallback
6edd0 28 29 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20 28  () */   .  int (
6ede0 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b  *xCommitCallback
6edf0 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a 20  )(void*);    /* 
6ee00 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79  Invoked at every
6ee10 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f   commit. */.  vo
6ee20 69 64 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72 67  id *pRollbackArg
6ee30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6ee40 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78  /* Argument to x
6ee50 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b  RollbackCallback
6ee60 28 29 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64 20  () */   .  void 
6ee70 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62  (*xRollbackCallb
6ee80 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20  ack)(void*); /* 
6ee90 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79  Invoked at every
6eea0 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f   commit. */.  vo
6eeb0 69 64 20 2a 70 55 70 64 61 74 65 41 72 67 3b 0a  id *pUpdateArg;.
6eec0 20 20 76 6f 69 64 20 28 2a 78 55 70 64 61 74 65    void (*xUpdate
6eed0 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c  Callback)(void*,
6eee0 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  int, const char*
6eef0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c  ,const char*,sql
6ef00 69 74 65 5f 69 6e 74 36 34 29 3b 0a 23 69 66 6e  ite_int64);.#ifn
6ef10 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
6ef20 57 41 4c 0a 20 20 69 6e 74 20 28 2a 78 57 61 6c  WAL.  int (*xWal
6ef30 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a  Callback)(void *
6ef40 2c 20 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e  , sqlite3 *, con
6ef50 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b  st char *, int);
6ef60 0a 20 20 76 6f 69 64 20 2a 70 57 61 6c 41 72 67  .  void *pWalArg
6ef70 3b 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 28  ;.#endif.  void(
6ef80 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f  *xCollNeeded)(vo
6ef90 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74  id*,sqlite3*,int
6efa0 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20   eTextRep,const 
6efb0 63 68 61 72 2a 29 3b 0a 20 20 76 6f 69 64 28 2a  char*);.  void(*
6efc0 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28 76  xCollNeeded16)(v
6efd0 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e  oid*,sqlite3*,in
6efe0 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74  t eTextRep,const
6eff0 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20   void*);.  void 
6f000 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b  *pCollNeededArg;
6f010 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
6f020 20 2a 70 45 72 72 3b 20 20 20 20 20 20 20 20 20   *pErr;         
6f030 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20   /* Most recent 
6f040 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f  error message */
6f050 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
6f060 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6f070 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20   /* Most recent 
6f080 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55  error message (U
6f090 54 46 2d 38 20 65 6e 63 6f 64 65 64 29 20 2a 2f  TF-8 encoded) */
6f0a0 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
6f0b0 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  16;             
6f0c0 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20   /* Most recent 
6f0d0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55  error message (U
6f0e0 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 29 20 2a  TF-16 encoded) *
6f0f0 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20  /.  union {.    
6f100 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 73 49  volatile int isI
6f110 6e 74 65 72 72 75 70 74 65 64 3b 20 2f 2a 20 54  nterrupted; /* T
6f120 72 75 65 20 69 66 20 73 71 6c 69 74 65 33 5f 69  rue if sqlite3_i
6f130 6e 74 65 72 72 75 70 74 20 68 61 73 20 62 65 65  nterrupt has bee
6f140 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 20 20  n called */.    
6f150 64 6f 75 62 6c 65 20 6e 6f 74 55 73 65 64 31 3b  double notUsed1;
6f160 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
6f170 70 61 63 65 72 20 2a 2f 0a 20 20 7d 20 75 31 3b  pacer */.  } u1;
6f180 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f 6f  .  Lookaside loo
6f190 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20  kaside;         
6f1a0 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61   /* Lookaside ma
6f1b0 6c 6c 6f 63 20 63 6f 6e 66 69 67 75 72 61 74 69  lloc configurati
6f1c0 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  on */.#ifndef SQ
6f1d0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
6f1e0 49 5a 41 54 49 4f 4e 0a 20 20 69 6e 74 20 28 2a  IZATION.  int (*
6f1f0 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74  xAuth)(void*,int
6f200 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
6f210 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
6f220 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
6f230 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
6f240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6f250 20 20 20 2f 2a 20 41 63 63 65 73 73 20 61 75 74     /* Access aut
6f260 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74  horization funct
6f270 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ion */.  void *p
6f280 41 75 74 68 41 72 67 3b 20 20 20 20 20 20 20 20  AuthArg;        
6f290 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72         /* 1st ar
6f2a0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 63  gument to the ac
6f2b0 63 65 73 73 20 61 75 74 68 20 66 75 6e 63 74 69  cess auth functi
6f2c0 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66  on */.#endif.#if
6f2d0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
6f2e0 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41  _PROGRESS_CALLBA
6f2f0 43 4b 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f 67  CK.  int (*xProg
6f300 72 65 73 73 29 28 76 6f 69 64 20 2a 29 3b 20 20  ress)(void *);  
6f310 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 65     /* The progre
6f320 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  ss callback */. 
6f330 20 76 6f 69 64 20 2a 70 50 72 6f 67 72 65 73 73   void *pProgress
6f340 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Arg;           /
6f350 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
6f360 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
6f370 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72  ack */.  int nPr
6f380 6f 67 72 65 73 73 4f 70 73 3b 20 20 20 20 20 20  ogressOps;      
6f390 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6f3a0 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20   of opcodes for 
6f3b0 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
6f3c0 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e  k */.#endif.#ifn
6f3d0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
6f3e0 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69  VIRTUALTABLE.  i
6f3f0 6e 74 20 6e 56 54 72 61 6e 73 3b 20 20 20 20 20  nt nVTrans;     
6f400 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6f410 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f  Allocated size o
6f420 66 20 61 56 54 72 61 6e 73 20 2a 2f 0a 20 20 48  f aVTrans */.  H
6f430 61 73 68 20 61 4d 6f 64 75 6c 65 3b 20 20 20 20  ash aModule;    
6f440 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6f450 70 6f 70 75 6c 61 74 65 64 20 62 79 20 73 71 6c  populated by sql
6f460 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
6f470 6c 65 28 29 20 2a 2f 0a 20 20 56 74 61 62 43 74  le() */.  VtabCt
6f480 78 20 2a 70 56 74 61 62 43 74 78 3b 20 20 20 20  x *pVtabCtx;    
6f490 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65          /* Conte
6f4a0 78 74 20 66 6f 72 20 61 63 74 69 76 65 20 76 74  xt for active vt
6f4b0 61 62 20 63 6f 6e 6e 65 63 74 2f 63 72 65 61 74  ab connect/creat
6f4c0 65 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 2a  e */.  VTable **
6f4d0 61 56 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20  aVTrans;        
6f4e0 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20       /* Virtual 
6f4f0 74 61 62 6c 65 73 20 77 69 74 68 20 6f 70 65 6e  tables with open
6f500 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 2a 2f   transactions */
6f510 0a 20 20 56 54 61 62 6c 65 20 2a 70 44 69 73 63  .  VTable *pDisc
6f520 6f 6e 6e 65 63 74 3b 20 20 20 20 2f 2a 20 44 69  onnect;    /* Di
6f530 73 63 6f 6e 6e 65 63 74 20 74 68 65 73 65 20 69  sconnect these i
6f540 6e 20 6e 65 78 74 20 73 71 6c 69 74 65 33 5f 70  n next sqlite3_p
6f550 72 65 70 61 72 65 28 29 20 2a 2f 0a 23 65 6e 64  repare() */.#end
6f560 69 66 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68  if.  FuncDefHash
6f570 20 61 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20   aFunc;         
6f580 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65     /* Hash table
6f590 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66   of connection f
6f5a0 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 48 61  unctions */.  Ha
6f5b0 73 68 20 61 43 6f 6c 6c 53 65 71 3b 20 20 20 20  sh aCollSeq;    
6f5c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
6f5d0 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  ll collating seq
6f5e0 75 65 6e 63 65 73 20 2a 2f 0a 20 20 42 75 73 79  uences */.  Busy
6f5f0 48 61 6e 64 6c 65 72 20 62 75 73 79 48 61 6e 64  Handler busyHand
6f600 6c 65 72 3b 20 20 20 20 20 20 2f 2a 20 42 75 73  ler;      /* Bus
6f610 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20  y callback */.  
6f620 44 62 20 61 44 62 53 74 61 74 69 63 5b 32 5d 3b  Db aDbStatic[2];
6f630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6f640 20 53 74 61 74 69 63 20 73 70 61 63 65 20 66 6f   Static space fo
6f650 72 20 74 68 65 20 32 20 64 65 66 61 75 6c 74 20  r the 2 default 
6f660 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 20 20 53 61  backends */.  Sa
6f670 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f  vepoint *pSavepo
6f680 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c  int;        /* L
6f690 69 73 74 20 6f 66 20 61 63 74 69 76 65 20 73 61  ist of active sa
6f6a0 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e  vepoints */.  in
6f6b0 74 20 62 75 73 79 54 69 6d 65 6f 75 74 3b 20 20  t busyTimeout;  
6f6c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
6f6d0 75 73 79 20 68 61 6e 64 6c 65 72 20 74 69 6d 65  usy handler time
6f6e0 6f 75 74 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a  out, in msec */.
6f6f0 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74    int nSavepoint
6f700 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6f710 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e  /* Number of non
6f720 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76  -transaction sav
6f730 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74  epoints */.  int
6f740 20 6e 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 20   nStatement;    
6f750 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
6f760 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 73  mber of nested s
6f770 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63  tatement-transac
6f780 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 69 36 34 20  tions  */.  i64 
6f790 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 20 20  nDeferredCons;  
6f7a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 74            /* Net
6f7b0 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72   deferred constr
6f7c0 61 69 6e 74 73 20 74 68 69 73 20 74 72 61 6e 73  aints this trans
6f7d0 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 69 6e 74  action. */.  int
6f7e0 20 2a 70 6e 42 79 74 65 73 46 72 65 65 64 3b 20   *pnBytesFreed; 
6f7f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
6f800 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 6e 63 72 65   not NULL, incre
6f810 6d 65 6e 74 20 74 68 69 73 20 69 6e 20 44 62 46  ment this in DbF
6f820 72 65 65 28 29 20 2a 2f 0a 0a 23 69 66 64 65 66  ree() */..#ifdef
6f830 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55   SQLITE_ENABLE_U
6f840 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20 20 2f  NLOCK_NOTIFY.  /
6f850 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
6f860 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 61 6c  variables are al
6f870 6c 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 74  l protected by t
6f880 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52  he STATIC_MASTER
6f890 20 0a 20 20 2a 2a 20 6d 75 74 65 78 2c 20 6e 6f   .  ** mutex, no
6f8a0 74 20 62 79 20 73 71 6c 69 74 65 33 2e 6d 75 74  t by sqlite3.mut
6f8b0 65 78 2e 20 54 68 65 79 20 61 72 65 20 75 73 65  ex. They are use
6f8c0 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6e 6f 74  d by code in not
6f8d0 69 66 79 2e 63 2e 20 0a 20 20 2a 2a 0a 20 20 2a  ify.c. .  **.  *
6f8e0 2a 20 57 68 65 6e 20 58 2e 70 55 6e 6c 6f 63 6b  * When X.pUnlock
6f8f0 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 74  Connection==Y, t
6f900 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 58  hat means that X
6f910 20 69 73 20 77 61 69 74 69 6e 67 20 66 6f 72 20   is waiting for 
6f920 59 20 74 6f 0a 20 20 2a 2a 20 75 6e 6c 6f 63 6b  Y to.  ** unlock
6f930 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20   so that it can 
6f940 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2a 0a 20 20  proceed..  **.  
6f950 2a 2a 20 57 68 65 6e 20 58 2e 70 42 6c 6f 63 6b  ** When X.pBlock
6f960 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59  ingConnection==Y
6f970 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  , that means tha
6f980 74 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74  t something that
6f990 20 58 20 74 72 69 65 64 0a 20 20 2a 2a 20 74 72   X tried.  ** tr
6f9a0 69 65 64 20 74 6f 20 64 6f 20 72 65 63 65 6e 74  ied to do recent
6f9b0 6c 79 20 66 61 69 6c 65 64 20 77 69 74 68 20 61  ly failed with a
6f9c0 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  n SQLITE_LOCKED 
6f9d0 65 72 72 6f 72 20 64 75 65 20 74 6f 20 6c 6f 63  error due to loc
6f9e0 6b 73 0a 20 20 2a 2a 20 68 65 6c 64 20 62 79 20  ks.  ** held by 
6f9f0 59 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  Y..  */.  sqlite
6fa00 33 20 2a 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e  3 *pBlockingConn
6fa10 65 63 74 69 6f 6e 3b 20 2f 2a 20 43 6f 6e 6e 65  ection; /* Conne
6fa20 63 74 69 6f 6e 20 74 68 61 74 20 63 61 75 73 65  ction that cause
6fa30 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  d SQLITE_LOCKED 
6fa40 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 55  */.  sqlite3 *pU
6fa50 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3b  nlockConnection;
6fa60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
6fa70 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 77 61 74 63  nnection to watc
6fa80 68 20 66 6f 72 20 75 6e 6c 6f 63 6b 20 2a 2f 0a  h for unlock */.
6fa90 20 20 76 6f 69 64 20 2a 70 55 6e 6c 6f 63 6b 41    void *pUnlockA
6faa0 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
6fab0 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
6fac0 65 6e 74 20 74 6f 20 78 55 6e 6c 6f 63 6b 4e 6f  ent to xUnlockNo
6fad0 74 69 66 79 20 2a 2f 0a 20 20 76 6f 69 64 20 28  tify */.  void (
6fae0 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 29 28  *xUnlockNotify)(
6faf0 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 3b 20 20  void **, int);  
6fb00 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f 74 69 66 79  /* Unlock notify
6fb10 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 73   callback */.  s
6fb20 71 6c 69 74 65 33 20 2a 70 4e 65 78 74 42 6c 6f  qlite3 *pNextBlo
6fb30 63 6b 65 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  cked;        /* 
6fb40 4e 65 78 74 20 69 6e 20 6c 69 73 74 20 6f 66 20  Next in list of 
6fb50 61 6c 6c 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e  all blocked conn
6fb60 65 63 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69  ections */.#endi
6fb70 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61  f.};../*.** A ma
6fb80 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65 72 20  cro to discover 
6fb90 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  the encoding of 
6fba0 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 23  a database..*/.#
6fbb0 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29 20 28  define ENC(db) (
6fbc0 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53 63  (db)->aDb[0].pSc
6fbd0 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a  hema->enc)../*.*
6fbe0 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65  * Possible value
6fbf0 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65  s for the sqlite
6fc00 33 2e 66 6c 61 67 73 2e 0a 2a 2f 0a 23 64 65 66  3.flags..*/.#def
6fc10 69 6e 65 20 53 51 4c 49 54 45 5f 56 64 62 65 54  ine SQLITE_VdbeT
6fc20 72 61 63 65 20 20 20 20 20 20 30 78 30 30 30 30  race      0x0000
6fc30 30 30 30 31 20 20 2f 2a 20 54 72 75 65 20 74 6f  0001  /* True to
6fc40 20 74 72 61 63 65 20 56 44 42 45 20 65 78 65 63   trace VDBE exec
6fc50 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  ution */.#define
6fc60 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68   SQLITE_InternCh
6fc70 61 6e 67 65 73 20 20 30 78 30 30 30 30 30 30 30  anges  0x0000000
6fc80 32 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74 65  2  /* Uncommitte
6fc90 64 20 48 61 73 68 20 74 61 62 6c 65 20 63 68 61  d Hash table cha
6fca0 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  nges */.#define 
6fcb0 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61  SQLITE_FullColNa
6fcc0 6d 65 73 20 20 20 30 78 30 30 30 30 30 30 30 34  mes   0x00000004
6fcd0 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20 63    /* Show full c
6fce0 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20 53  olumn names on S
6fcf0 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69 6e 65  ELECT */.#define
6fd00 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c   SQLITE_ShortCol
6fd10 4e 61 6d 65 73 20 20 30 78 30 30 30 30 30 30 30  Names  0x0000000
6fd20 38 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 72 74  8  /* Show short
6fd30 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 2a   columns names *
6fd40 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
6fd50 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20 20  _CountRows      
6fd60 30 78 30 30 30 30 30 30 31 30 20 20 2f 2a 20 43  0x00000010  /* C
6fd70 6f 75 6e 74 20 72 6f 77 73 20 63 68 61 6e 67 65  ount rows change
6fd80 64 20 62 79 20 49 4e 53 45 52 54 2c 20 2a 2f 0a  d by INSERT, */.
6fd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6fda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6fdb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 44            /*   D
6fdc0 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 45  ELETE, or UPDATE
6fdd0 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20   and return */. 
6fde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6fdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6fe00 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 74 68           /*   th
6fe10 65 20 63 6f 75 6e 74 20 75 73 69 6e 67 20 61 20  e count using a 
6fe20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64 65  callback. */.#de
6fe30 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75 6c 6c  fine SQLITE_Null
6fe40 43 61 6c 6c 62 61 63 6b 20 20 20 30 78 30 30 30  Callback   0x000
6fe50 30 30 30 32 30 20 20 2f 2a 20 49 6e 76 6f 6b 65  00020  /* Invoke
6fe60 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e   the callback on
6fe70 63 65 20 69 66 20 74 68 65 20 2a 2f 0a 20 20 20  ce if the */.   
6fe80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6fe90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6fea0 20 20 20 20 20 20 20 2f 2a 20 20 20 72 65 73 75         /*   resu
6feb0 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 20  lt set is empty 
6fec0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
6fed0 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20 20  E_SqlTrace      
6fee0 20 30 78 30 30 30 30 30 30 34 30 20 20 2f 2a 20   0x00000040  /* 
6fef0 44 65 62 75 67 20 70 72 69 6e 74 20 53 51 4c 20  Debug print SQL 
6ff00 61 73 20 69 74 20 65 78 65 63 75 74 65 73 20 2a  as it executes *
6ff10 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
6ff20 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20 20  _VdbeListing    
6ff30 30 78 30 30 30 30 30 30 38 30 20 20 2f 2a 20 44  0x00000080  /* D
6ff40 65 62 75 67 20 6c 69 73 74 69 6e 67 73 20 6f 66  ebug listings of
6ff50 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20 2a   VDBE programs *
6ff60 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
6ff70 5f 57 72 69 74 65 53 63 68 65 6d 61 20 20 20 20  _WriteSchema    
6ff80 30 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 4f  0x00000100  /* O
6ff90 4b 20 74 6f 20 75 70 64 61 74 65 20 53 51 4c 49  K to update SQLI
6ffa0 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 20 20 20  TE_MASTER */.   
6ffb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ffc0 20 20 20 20 20 20 2f 2a 20 20 20 30 78 30 30 30        /*   0x000
6ffd0 30 30 32 30 30 20 20 55 6e 75 73 65 64 20 2a 2f  00200  Unused */
6ffe0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
6fff0 49 67 6e 6f 72 65 43 68 65 63 6b 73 20 20 20 30  IgnoreChecks   0
70000 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 44 6f  x00000400  /* Do
70010 20 6e 6f 74 20 65 6e 66 6f 72 63 65 20 63 68 65   not enforce che
70020 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a  ck constraints *
70030 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
70040 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64  _ReadUncommitted
70050 20 30 78 30 30 30 30 38 30 30 20 20 2f 2a 20 46   0x0000800  /* F
70060 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  or shared-cache 
70070 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  mode */.#define 
70080 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c  SQLITE_LegacyFil
70090 65 46 6d 74 20 20 30 78 30 30 30 30 31 30 30 30  eFmt  0x00001000
700a0 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20    /* Create new 
700b0 64 61 74 61 62 61 73 65 73 20 69 6e 20 66 6f 72  databases in for
700c0 6d 61 74 20 31 20 2a 2f 0a 23 64 65 66 69 6e 65  mat 1 */.#define
700d0 20 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e   SQLITE_FullFSyn
700e0 63 20 20 20 20 20 20 30 78 30 30 30 30 32 30 30  c      0x0000200
700f0 30 20 20 2f 2a 20 55 73 65 20 66 75 6c 6c 20 66  0  /* Use full f
70100 73 79 6e 63 20 6f 6e 20 74 68 65 20 62 61 63 6b  sync on the back
70110 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  end */.#define S
70120 51 4c 49 54 45 5f 43 6b 70 74 46 75 6c 6c 46 53  QLITE_CkptFullFS
70130 79 6e 63 20 20 30 78 30 30 30 30 34 30 30 30 20  ync  0x00004000 
70140 20 2f 2a 20 55 73 65 20 66 75 6c 6c 20 66 73 79   /* Use full fsy
70150 6e 63 20 66 6f 72 20 63 68 65 63 6b 70 6f 69 6e  nc for checkpoin
70160 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  t */.#define SQL
70170 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65  ITE_RecoveryMode
70180 20 20 20 30 78 30 30 30 30 38 30 30 30 20 20 2f     0x00008000  /
70190 2a 20 49 67 6e 6f 72 65 20 73 63 68 65 6d 61 20  * Ignore schema 
701a0 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e  errors */.#defin
701b0 65 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73 65  e SQLITE_Reverse
701c0 4f 72 64 65 72 20 20 20 30 78 30 30 30 31 30 30  Order   0x000100
701d0 30 30 20 20 2f 2a 20 52 65 76 65 72 73 65 20 75  00  /* Reverse u
701e0 6e 6f 72 64 65 72 65 64 20 53 45 4c 45 43 54 73  nordered SELECTs
701f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
70200 54 45 5f 52 65 63 54 72 69 67 67 65 72 73 20 20  TE_RecTriggers  
70210 20 20 30 78 30 30 30 32 30 30 30 30 20 20 2f 2a    0x00020000  /*
70220 20 45 6e 61 62 6c 65 20 72 65 63 75 72 73 69 76   Enable recursiv
70230 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 64  e triggers */.#d
70240 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 6f 72  efine SQLITE_For
70250 65 69 67 6e 4b 65 79 73 20 20 20 20 30 78 30 30  eignKeys    0x00
70260 30 34 30 30 30 30 20 20 2f 2a 20 45 6e 66 6f 72  040000  /* Enfor
70270 63 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  ce foreign key c
70280 6f 6e 73 74 72 61 69 6e 74 73 20 20 2a 2f 0a 23  onstraints  */.#
70290 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 75  define SQLITE_Au
702a0 74 6f 49 6e 64 65 78 20 20 20 20 20 20 30 78 30  toIndex      0x0
702b0 30 30 38 30 30 30 30 20 20 2f 2a 20 45 6e 61 62  0080000  /* Enab
702c0 6c 65 20 61 75 74 6f 6d 61 74 69 63 20 69 6e 64  le automatic ind
702d0 65 78 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  exes */.#define 
702e0 53 51 4c 49 54 45 5f 50 72 65 66 65 72 42 75 69  SQLITE_PreferBui
702f0 6c 74 69 6e 20 20 30 78 30 30 31 30 30 30 30 30  ltin  0x00100000
70300 20 20 2f 2a 20 50 72 65 66 65 72 65 6e 63 65 20    /* Preference 
70310 74 6f 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63  to built-in func
70320 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
70330 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ITE_LoadExtensio
70340 6e 20 20 30 78 30 30 32 30 30 30 30 30 20 20 2f  n  0x00200000  /
70350 2a 20 45 6e 61 62 6c 65 20 6c 6f 61 64 5f 65 78  * Enable load_ex
70360 74 65 6e 73 69 6f 6e 20 2a 2f 0a 23 64 65 66 69  tension */.#defi
70370 6e 65 20 53 51 4c 49 54 45 5f 45 6e 61 62 6c 65  ne SQLITE_Enable
70380 54 72 69 67 67 65 72 20 20 30 78 30 30 34 30 30  Trigger  0x00400
70390 30 30 30 20 20 2f 2a 20 54 72 75 65 20 74 6f 20  000  /* True to 
703a0 65 6e 61 62 6c 65 20 74 72 69 67 67 65 72 73 20  enable triggers 
703b0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 73 20 6f  */../*.** Bits o
703c0 66 20 74 68 65 20 73 71 6c 69 74 65 33 2e 64 62  f the sqlite3.db
703d0 4f 70 74 46 6c 61 67 73 20 66 69 65 6c 64 20 74  OptFlags field t
703e0 68 61 74 20 61 72 65 20 75 73 65 64 20 62 79 20  hat are used by 
703f0 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74  the.** sqlite3_t
70400 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49  est_control(SQLI
70410 54 45 5f 54 45 53 54 43 54 52 4c 5f 4f 50 54 49  TE_TESTCTRL_OPTI
70420 4d 49 5a 41 54 49 4f 4e 53 2c 2e 2e 2e 29 20 69  MIZATIONS,...) i
70430 6e 74 65 72 66 61 63 65 20 74 6f 0a 2a 2a 20 73  nterface to.** s
70440 65 6c 65 63 74 69 76 65 6c 79 20 64 69 73 61 62  electively disab
70450 6c 65 20 76 61 72 69 6f 75 73 20 6f 70 74 69 6d  le various optim
70460 69 7a 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65  izations..*/.#de
70470 66 69 6e 65 20 53 51 4c 49 54 45 5f 51 75 65 72  fine SQLITE_Quer
70480 79 46 6c 61 74 74 65 6e 65 72 20 30 78 30 30 30  yFlattener 0x000
70490 31 20 20 20 2f 2a 20 51 75 65 72 79 20 66 6c 61  1   /* Query fla
704a0 74 74 65 6e 69 6e 67 20 2a 2f 0a 23 64 65 66 69  ttening */.#defi
704b0 6e 65 20 53 51 4c 49 54 45 5f 43 6f 6c 75 6d 6e  ne SQLITE_Column
704c0 43 61 63 68 65 20 20 20 20 30 78 30 30 30 32 20  Cache    0x0002 
704d0 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 63 61 63 68    /* Column cach
704e0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
704f0 49 54 45 5f 47 72 6f 75 70 42 79 4f 72 64 65 72  ITE_GroupByOrder
70500 20 20 20 30 78 30 30 30 34 20 20 20 2f 2a 20 47     0x0004   /* G
70510 52 4f 55 50 42 59 20 63 6f 76 65 72 20 6f 66 20  ROUPBY cover of 
70520 4f 52 44 45 52 42 59 20 2a 2f 0a 23 64 65 66 69  ORDERBY */.#defi
70530 6e 65 20 53 51 4c 49 54 45 5f 46 61 63 74 6f 72  ne SQLITE_Factor
70540 4f 75 74 43 6f 6e 73 74 20 30 78 30 30 30 38 20  OutConst 0x0008 
70550 20 20 2f 2a 20 43 6f 6e 73 74 61 6e 74 20 66 61    /* Constant fa
70560 63 74 6f 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69  ctoring */.#defi
70570 6e 65 20 53 51 4c 49 54 45 5f 49 64 78 52 65 61  ne SQLITE_IdxRea
70580 6c 41 73 49 6e 74 20 20 20 30 78 30 30 31 30 20  lAsInt   0x0010 
70590 20 20 2f 2a 20 53 74 6f 72 65 20 52 45 41 4c 20    /* Store REAL 
705a0 61 73 20 49 4e 54 20 69 6e 20 69 6e 64 69 63 65  as INT in indice
705b0 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
705c0 49 54 45 5f 44 69 73 74 69 6e 63 74 4f 70 74 20  ITE_DistinctOpt 
705d0 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a 20 44     0x0020   /* D
705e0 49 53 54 49 4e 43 54 20 75 73 69 6e 67 20 69 6e  ISTINCT using in
705f0 64 65 78 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65  dexes */.#define
70600 20 53 51 4c 49 54 45 5f 43 6f 76 65 72 49 64 78   SQLITE_CoverIdx
70610 53 63 61 6e 20 20 20 30 78 30 30 34 30 20 20 20  Scan   0x0040   
70620 2f 2a 20 43 6f 76 65 72 69 6e 67 20 69 6e 64 65  /* Covering inde
70630 78 20 73 63 61 6e 73 20 2a 2f 0a 23 64 65 66 69  x scans */.#defi
70640 6e 65 20 53 51 4c 49 54 45 5f 4f 72 64 65 72 42  ne SQLITE_OrderB
70650 79 49 64 78 4a 6f 69 6e 20 30 78 30 30 38 30 20  yIdxJoin 0x0080 
70660 20 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 6f 66    /* ORDER BY of
70670 20 6a 6f 69 6e 73 20 76 69 61 20 69 6e 64 65 78   joins via index
70680 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
70690 54 45 5f 53 75 62 71 43 6f 72 6f 75 74 69 6e 65  TE_SubqCoroutine
706a0 20 20 30 78 30 31 30 30 20 20 20 2f 2a 20 45 76    0x0100   /* Ev
706b0 61 6c 75 61 74 65 20 73 75 62 71 75 65 72 69 65  aluate subquerie
706c0 73 20 61 73 20 63 6f 72 6f 75 74 69 6e 65 73 20  s as coroutines 
706d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
706e0 45 5f 41 6c 6c 4f 70 74 73 20 20 20 20 20 20 20  E_AllOpts       
706f0 20 30 78 66 66 66 66 20 20 20 2f 2a 20 41 6c 6c   0xffff   /* All
70700 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 2a   optimizations *
70710 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20  /../*.** Macros 
70720 66 6f 72 20 74 65 73 74 69 6e 67 20 77 68 65 74  for testing whet
70730 68 65 72 20 6f 72 20 6e 6f 74 20 6f 70 74 69 6d  her or not optim
70740 69 7a 61 74 69 6f 6e 73 20 61 72 65 20 65 6e 61  izations are ena
70750 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64  bled or disabled
70760 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
70770 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
70780 5f 54 45 53 54 0a 23 64 65 66 69 6e 65 20 4f 70  _TEST.#define Op
70790 74 69 6d 69 7a 61 74 69 6f 6e 44 69 73 61 62 6c  timizationDisabl
707a0 65 64 28 64 62 2c 20 6d 61 73 6b 29 20 20 28 28  ed(db, mask)  ((
707b0 28 64 62 29 2d 3e 64 62 4f 70 74 46 6c 61 67 73  (db)->dbOptFlags
707c0 26 28 6d 61 73 6b 29 29 21 3d 30 29 0a 23 64 65  &(mask))!=0).#de
707d0 66 69 6e 65 20 4f 70 74 69 6d 69 7a 61 74 69 6f  fine Optimizatio
707e0 6e 45 6e 61 62 6c 65 64 28 64 62 2c 20 6d 61 73  nEnabled(db, mas
707f0 6b 29 20 20 20 28 28 28 64 62 29 2d 3e 64 62 4f  k)   (((db)->dbO
70800 70 74 46 6c 61 67 73 26 28 6d 61 73 6b 29 29 3d  ptFlags&(mask))=
70810 3d 30 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  =0).#else.#defin
70820 65 20 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 44 69  e OptimizationDi
70830 73 61 62 6c 65 64 28 64 62 2c 20 6d 61 73 6b 29  sabled(db, mask)
70840 20 20 30 0a 23 64 65 66 69 6e 65 20 4f 70 74 69    0.#define Opti
70850 6d 69 7a 61 74 69 6f 6e 45 6e 61 62 6c 65 64 28  mizationEnabled(
70860 64 62 2c 20 6d 61 73 6b 29 20 20 20 31 0a 23 65  db, mask)   1.#e
70870 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73  ndif../*.** Poss
70880 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20  ible values for 
70890 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 63  the sqlite.magic
708a0 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68 65 20 6e   field..** The n
708b0 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74 61 69  umbers are obtai
708c0 6e 65 64 20 61 74 20 72 61 6e 64 6f 6d 20 61 6e  ned at random an
708d0 64 20 68 61 76 65 20 6e 6f 20 73 70 65 63 69 61  d have no specia
708e0 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f 74 68 65 72  l meaning, other
708f0 0a 2a 2a 20 74 68 61 6e 20 62 65 69 6e 67 20 64  .** than being d
70900 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 6f 6e 65  istinct from one
70910 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 23 64 65   another..*/.#de
70920 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49  fine SQLITE_MAGI
70930 43 5f 4f 50 45 4e 20 20 20 20 20 30 78 61 30 32  C_OPEN     0xa02
70940 39 61 36 39 37 20 20 2f 2a 20 44 61 74 61 62 61  9a697  /* Databa
70950 73 65 20 69 73 20 6f 70 65 6e 20 2a 2f 0a 23 64  se is open */.#d
70960 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47  efine SQLITE_MAG
70970 49 43 5f 43 4c 4f 53 45 44 20 20 20 30 78 39 66  IC_CLOSED   0x9f
70980 33 63 32 64 33 33 20 20 2f 2a 20 44 61 74 61 62  3c2d33  /* Datab
70990 61 73 65 20 69 73 20 63 6c 6f 73 65 64 20 2a 2f  ase is closed */
709a0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
709b0 4d 41 47 49 43 5f 53 49 43 4b 20 20 20 20 20 30  MAGIC_SICK     0
709c0 78 34 62 37 37 31 32 39 30 20 20 2f 2a 20 45 72  x4b771290  /* Er
709d0 72 6f 72 20 61 6e 64 20 61 77 61 69 74 69 6e 67  ror and awaiting
709e0 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 65 66 69 6e   close */.#defin
709f0 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42  e SQLITE_MAGIC_B
70a00 55 53 59 20 20 20 20 20 30 78 66 30 33 62 37 39  USY     0xf03b79
70a10 30 36 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20  06  /* Database 
70a20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65  currently in use
70a30 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
70a40 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 20 20  TE_MAGIC_ERROR  
70a50 20 20 30 78 62 35 33 35 37 39 33 30 20 20 2f 2a    0xb5357930  /*
70a60 20 41 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53   An SQLITE_MISUS
70a70 45 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64  E error occurred
70a80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
70a90 54 45 5f 4d 41 47 49 43 5f 5a 4f 4d 42 49 45 20  TE_MAGIC_ZOMBIE 
70aa0 20 20 30 78 36 34 63 66 66 63 37 66 20 20 2f 2a    0x64cffc7f  /*
70ab0 20 43 6c 6f 73 65 20 77 69 74 68 20 6c 61 73 74   Close with last
70ac0 20 73 74 61 74 65 6d 65 6e 74 20 63 6c 6f 73 65   statement close
70ad0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20   */../*.** Each 
70ae0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  SQL function is 
70af0 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 69 6e  defined by an in
70b00 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
70b10 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63  llowing.** struc
70b20 74 75 72 65 2e 20 20 41 20 70 6f 69 6e 74 65 72  ture.  A pointer
70b30 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75   to this structu
70b40 72 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  re is stored in 
70b50 74 68 65 20 73 71 6c 69 74 65 2e 61 46 75 6e 63  the sqlite.aFunc
70b60 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 20  .** hash table. 
70b70 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 66   When multiple f
70b80 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 74 68  unctions have th
70b90 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 74 68 65  e same name, the
70ba0 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20 70   hash table.** p
70bb0 6f 69 6e 74 73 20 74 6f 20 61 20 6c 69 6e 6b 65  oints to a linke
70bc0 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 65 20  d list of these 
70bd0 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 73  structures..*/.s
70be0 74 72 75 63 74 20 46 75 6e 63 44 65 66 20 7b 0a  truct FuncDef {.
70bf0 20 20 69 31 36 20 6e 41 72 67 3b 20 20 20 20 20    i16 nArg;     
70c00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
70c10 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20   of arguments.  
70c20 2d 31 20 6d 65 61 6e 73 20 75 6e 6c 69 6d 69 74  -1 means unlimit
70c30 65 64 20 2a 2f 0a 20 20 75 38 20 69 50 72 65 66  ed */.  u8 iPref
70c40 45 6e 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Enc;         /* 
70c50 50 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65  Preferred text e
70c60 6e 63 6f 64 69 6e 67 20 28 53 51 4c 49 54 45 5f  ncoding (SQLITE_
70c70 55 54 46 38 2c 20 31 36 4c 45 2c 20 31 36 42 45  UTF8, 16LE, 16BE
70c80 29 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67 73 3b  ) */.  u8 flags;
70c90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
70ca0 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  ome combination 
70cb0 6f 66 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 2a  of SQLITE_FUNC_*
70cc0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 73 65   */.  void *pUse
70cd0 72 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 55 73  rData;     /* Us
70ce0 65 72 20 64 61 74 61 20 70 61 72 61 6d 65 74 65  er data paramete
70cf0 72 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a  r */.  FuncDef *
70d00 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e  pNext;      /* N
70d10 65 78 74 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  ext function wit
70d20 68 20 73 61 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20  h same name */. 
70d30 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73   void (*xFunc)(s
70d40 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
70d50 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
70d60 65 2a 2a 29 3b 20 2f 2a 20 52 65 67 75 6c 61 72  e**); /* Regular
70d70 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76   function */.  v
70d80 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c  oid (*xStep)(sql
70d90 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
70da0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
70db0 2a 29 3b 20 2f 2a 20 41 67 67 72 65 67 61 74 65  *); /* Aggregate
70dc0 20 73 74 65 70 20 2a 2f 0a 20 20 76 6f 69 64 20   step */.  void 
70dd0 28 2a 78 46 69 6e 61 6c 69 7a 65 29 28 73 71 6c  (*xFinalize)(sql
70de0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 20  ite3_context*); 
70df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
70e00 2a 20 41 67 67 72 65 67 61 74 65 20 66 69 6e 61  * Aggregate fina
70e10 6c 69 7a 65 72 20 2a 2f 0a 20 20 63 68 61 72 20  lizer */.  char 
70e20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
70e30 2f 2a 20 53 51 4c 20 6e 61 6d 65 20 6f 66 20 74  /* SQL name of t
70e40 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a  he function. */.
70e50 20 20 46 75 6e 63 44 65 66 20 2a 70 48 61 73 68    FuncDef *pHash
70e60 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 77  ;      /* Next w
70e70 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20  ith a different 
70e80 6e 61 6d 65 20 62 75 74 20 74 68 65 20 73 61 6d  name but the sam
70e90 65 20 68 61 73 68 20 2a 2f 0a 20 20 46 75 6e 63  e hash */.  Func
70ea0 44 65 73 74 72 75 63 74 6f 72 20 2a 70 44 65 73  Destructor *pDes
70eb0 74 72 75 63 74 6f 72 3b 20 20 20 2f 2a 20 52 65  tructor;   /* Re
70ec0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 20  ference counted 
70ed0 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74  destructor funct
70ee0 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ion */.};../*.**
70ef0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
70f00 65 6e 63 61 70 73 75 6c 61 74 65 73 20 61 20 75  encapsulates a u
70f10 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 73  ser-function des
70f20 74 72 75 63 74 6f 72 20 63 61 6c 6c 62 61 63 6b  tructor callback
70f30 20 28 61 73 0a 2a 2a 20 63 6f 6e 66 69 67 75 72   (as.** configur
70f40 65 64 20 75 73 69 6e 67 20 63 72 65 61 74 65 5f  ed using create_
70f50 66 75 6e 63 74 69 6f 6e 5f 76 32 28 29 29 20 61  function_v2()) a
70f60 6e 64 20 61 20 72 65 66 65 72 65 6e 63 65 20 63  nd a reference c
70f70 6f 75 6e 74 65 72 2e 20 57 68 65 6e 0a 2a 2a 20  ounter. When.** 
70f80 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 5f  create_function_
70f90 76 32 28 29 20 69 73 20 63 61 6c 6c 65 64 20 74  v2() is called t
70fa0 6f 20 63 72 65 61 74 65 20 61 20 66 75 6e 63 74  o create a funct
70fb0 69 6f 6e 20 77 69 74 68 20 61 20 64 65 73 74 72  ion with a destr
70fc0 75 63 74 6f 72 2c 0a 2a 2a 20 61 20 73 69 6e 67  uctor,.** a sing
70fd0 6c 65 20 6f 62 6a 65 63 74 20 6f 66 20 74 68 69  le object of thi
70fe0 73 20 74 79 70 65 20 69 73 20 61 6c 6c 6f 63 61  s type is alloca
70ff0 74 65 64 2e 20 46 75 6e 63 44 65 73 74 72 75 63  ted. FuncDestruc
71000 74 6f 72 2e 6e 52 65 66 20 69 73 20 73 65 74 20  tor.nRef is set 
71010 74 6f 20 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  to .** the numbe
71020 72 20 6f 66 20 46 75 6e 63 44 65 66 20 6f 62 6a  r of FuncDef obj
71030 65 63 74 73 20 63 72 65 61 74 65 64 20 28 65 69  ects created (ei
71040 74 68 65 72 20 31 20 6f 72 20 33 2c 20 64 65 70  ther 1 or 3, dep
71050 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65  ending on whethe
71060 72 0a 2a 2a 20 6f 72 20 6e 6f 74 20 74 68 65 20  r.** or not the 
71070 73 70 65 63 69 66 69 65 64 20 65 6e 63 6f 64 69  specified encodi
71080 6e 67 20 69 73 20 53 51 4c 49 54 45 5f 41 4e 59  ng is SQLITE_ANY
71090 29 2e 20 54 68 65 20 46 75 6e 63 44 65 66 2e 70  ). The FuncDef.p
710a0 44 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 6d 65  Destructor.** me
710b0 6d 62 65 72 20 6f 66 20 65 61 63 68 20 6f 66 20  mber of each of 
710c0 74 68 65 20 6e 65 77 20 46 75 6e 63 44 65 66 20  the new FuncDef 
710d0 6f 62 6a 65 63 74 73 20 69 73 20 73 65 74 20 74  objects is set t
710e0 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 61  o point to the a
710f0 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 46 75 6e 63  llocated.** Func
71100 44 65 73 74 72 75 63 74 6f 72 2e 0a 2a 2a 0a 2a  Destructor..**.*
71110 2a 20 54 68 65 72 65 61 66 74 65 72 2c 20 77 68  * Thereafter, wh
71120 65 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 46 75  en one of the Fu
71130 6e 63 44 65 66 20 6f 62 6a 65 63 74 73 20 69 73  ncDef objects is
71140 20 64 65 6c 65 74 65 64 2c 20 74 68 65 20 72 65   deleted, the re
71150 66 65 72 65 6e 63 65 0a 2a 2a 20 63 6f 75 6e 74  ference.** count
71160 20 6f 6e 20 74 68 69 73 20 6f 62 6a 65 63 74 20   on this object 
71170 69 73 20 64 65 63 72 65 6d 65 6e 74 65 64 2e 20  is decremented. 
71180 57 68 65 6e 20 69 74 20 72 65 61 63 68 65 73 20  When it reaches 
71190 30 2c 20 74 68 65 20 64 65 73 74 72 75 63 74 6f  0, the destructo
711a0 72 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 20  r.** is invoked 
711b0 61 6e 64 20 74 68 65 20 46 75 6e 63 44 65 73 74  and the FuncDest
711c0 72 75 63 74 6f 72 20 73 74 72 75 63 74 75 72 65  ructor structure
711d0 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 72 75 63   freed..*/.struc
711e0 74 20 46 75 6e 63 44 65 73 74 72 75 63 74 6f 72  t FuncDestructor
711f0 20 7b 0a 20 20 69 6e 74 20 6e 52 65 66 3b 0a 20   {.  int nRef;. 
71200 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79   void (*xDestroy
71210 29 28 76 6f 69 64 20 2a 29 3b 0a 20 20 76 6f 69  )(void *);.  voi
71220 64 20 2a 70 55 73 65 72 44 61 74 61 3b 0a 7d 3b  d *pUserData;.};
71230 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65  ../*.** Possible
71240 20 76 61 6c 75 65 73 20 66 6f 72 20 46 75 6e 63   values for Func
71250 44 65 66 2e 66 6c 61 67 73 2e 20 20 4e 6f 74 65  Def.flags.  Note
71260 20 74 68 61 74 20 74 68 65 20 5f 4c 45 4e 47 54   that the _LENGT
71270 48 20 61 6e 64 20 5f 54 59 50 45 4f 46 0a 2a 2a  H and _TYPEOF.**
71280 20 76 61 6c 75 65 73 20 6d 75 73 74 20 63 6f 72   values must cor
71290 72 65 73 70 6f 6e 64 20 74 6f 20 4f 50 46 4c 41  respond to OPFLA
712a0 47 5f 4c 45 4e 47 54 48 41 52 47 20 61 6e 64 20  G_LENGTHARG and 
712b0 4f 50 46 4c 41 47 5f 54 59 50 45 4f 46 41 52 47  OPFLAG_TYPEOFARG
712c0 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20  .  There.** are 
712d0 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
712e0 6e 74 73 20 69 6e 20 74 68 65 20 63 6f 64 65 20  nts in the code 
712f0 74 6f 20 76 65 72 69 66 79 20 74 68 69 73 2e 0a  to verify this..
71300 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
71310 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 20 20 20 20  E_FUNC_LIKE     
71320 30 78 30 31 20 2f 2a 20 43 61 6e 64 69 64 61 74  0x01 /* Candidat
71330 65 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f  e for the LIKE o
71340 70 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a 23  ptimization */.#
71350 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55  define SQLITE_FU
71360 4e 43 5f 43 41 53 45 20 20 20 20 20 30 78 30 32  NC_CASE     0x02
71370 20 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69 74 69   /* Case-sensiti
71380 76 65 20 4c 49 4b 45 2d 74 79 70 65 20 66 75 6e  ve LIKE-type fun
71390 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  ction */.#define
713a0 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48   SQLITE_FUNC_EPH
713b0 45 4d 20 20 20 20 30 78 30 34 20 2f 2a 20 45 70  EM    0x04 /* Ep
713c0 68 65 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74 65  hemeral.  Delete
713d0 20 77 69 74 68 20 56 44 42 45 20 2a 2f 0a 23 64   with VDBE */.#d
713e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e  efine SQLITE_FUN
713f0 43 5f 4e 45 45 44 43 4f 4c 4c 20 30 78 30 38 20  C_NEEDCOLL 0x08 
71400 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e  /* sqlite3GetFun
71410 63 43 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68 74  cCollSeq() might
71420 20 62 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 23 64   be called */.#d
71430 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e  efine SQLITE_FUN
71440 43 5f 43 4f 55 4e 54 20 20 20 20 30 78 31 30 20  C_COUNT    0x10 
71450 2f 2a 20 42 75 69 6c 74 2d 69 6e 20 63 6f 75 6e  /* Built-in coun
71460 74 28 2a 29 20 61 67 67 72 65 67 61 74 65 20 2a  t(*) aggregate *
71470 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
71480 5f 46 55 4e 43 5f 43 4f 41 4c 45 53 43 45 20 30  _FUNC_COALESCE 0
71490 78 32 30 20 2f 2a 20 42 75 69 6c 74 2d 69 6e 20  x20 /* Built-in 
714a0 63 6f 61 6c 65 73 63 65 28 29 20 6f 72 20 69 66  coalesce() or if
714b0 6e 75 6c 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20  null() function 
714c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
714d0 45 5f 46 55 4e 43 5f 4c 45 4e 47 54 48 20 20 20  E_FUNC_LENGTH   
714e0 30 78 34 30 20 2f 2a 20 42 75 69 6c 74 2d 69 6e  0x40 /* Built-in
714f0 20 6c 65 6e 67 74 68 28 29 20 66 75 6e 63 74 69   length() functi
71500 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  on */.#define SQ
71510 4c 49 54 45 5f 46 55 4e 43 5f 54 59 50 45 4f 46  LITE_FUNC_TYPEOF
71520 20 20 20 30 78 38 30 20 2f 2a 20 42 75 69 6c 74     0x80 /* Built
71530 2d 69 6e 20 74 79 70 65 6f 66 28 29 20 66 75 6e  -in typeof() fun
71540 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ction */../*.** 
71550 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  The following th
71560 72 65 65 20 6d 61 63 72 6f 73 2c 20 46 55 4e 43  ree macros, FUNC
71570 54 49 4f 4e 28 29 2c 20 4c 49 4b 45 46 55 4e 43  TION(), LIKEFUNC
71580 28 29 20 61 6e 64 20 41 47 47 52 45 47 41 54 45  () and AGGREGATE
71590 28 29 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 74  () are.** used t
715a0 6f 20 63 72 65 61 74 65 20 74 68 65 20 69 6e 69  o create the ini
715b0 74 69 61 6c 69 7a 65 72 73 20 66 6f 72 20 74 68  tializers for th
715c0 65 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74  e FuncDef struct
715d0 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 55  ures..**.**   FU
715e0 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41  NCTION(zName, nA
715f0 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78  rg, iArg, bNC, x
71600 46 75 6e 63 29 0a 2a 2a 20 20 20 20 20 55 73 65  Func).**     Use
71610 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 63  d to create a sc
71620 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65  alar function de
71630 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75  finition of a fu
71640 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a  nction zName .**
71650 20 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 65 64       implemented
71660 20 62 79 20 43 20 66 75 6e 63 74 69 6f 6e 20 78   by C function x
71670 46 75 6e 63 20 74 68 61 74 20 61 63 63 65 70 74  Func that accept
71680 73 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73  s nArg arguments
71690 2e 20 54 68 65 0a 2a 2a 20 20 20 20 20 76 61 6c  . The.**     val
716a0 75 65 20 70 61 73 73 65 64 20 61 73 20 69 41 72  ue passed as iAr
716b0 67 20 69 73 20 63 61 73 74 20 74 6f 20 61 20 28  g is cast to a (
716c0 76 6f 69 64 2a 29 20 61 6e 64 20 6d 61 64 65 20  void*) and made 
716d0 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 20 20 20  available.**    
716e0 20 61 73 20 74 68 65 20 75 73 65 72 2d 64 61 74   as the user-dat
716f0 61 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  a (sqlite3_user_
71700 64 61 74 61 28 29 29 20 66 6f 72 20 74 68 65 20  data()) for the 
71710 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 0a 2a 2a  function. If .**
71720 20 20 20 20 20 61 72 67 75 6d 65 6e 74 20 62 4e       argument bN
71730 43 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  C is true, then 
71740 74 68 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  the SQLITE_FUNC_
71750 4e 45 45 44 43 4f 4c 4c 20 66 6c 61 67 20 69 73  NEEDCOLL flag is
71760 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 41 47   set..**.**   AG
71770 47 52 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e  GREGATE(zName, n
71780 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20  Arg, iArg, bNC, 
71790 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 0a 2a  xStep, xFinal).*
717a0 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72  *     Used to cr
717b0 65 61 74 65 20 61 6e 20 61 67 67 72 65 67 61 74  eate an aggregat
717c0 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  e function defin
717d0 69 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65  ition implemente
717e0 64 20 62 79 0a 2a 2a 20 20 20 20 20 74 68 65 20  d by.**     the 
717f0 43 20 66 75 6e 63 74 69 6f 6e 73 20 78 53 74 65  C functions xSte
71800 70 20 61 6e 64 20 78 46 69 6e 61 6c 2e 20 54 68  p and xFinal. Th
71810 65 20 66 69 72 73 74 20 66 6f 75 72 20 70 61 72  e first four par
71820 61 6d 65 74 65 72 73 0a 2a 2a 20 20 20 20 20 61  ameters.**     a
71830 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 69  re interpreted i
71840 6e 20 74 68 65 20 73 61 6d 65 20 77 61 79 20 61  n the same way a
71850 73 20 74 68 65 20 66 69 72 73 74 20 34 20 70 61  s the first 4 pa
71860 72 61 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 20  rameters to.**  
71870 20 20 20 46 55 4e 43 54 49 4f 4e 28 29 2e 0a 2a     FUNCTION()..*
71880 2a 0a 2a 2a 20 20 20 4c 49 4b 45 46 55 4e 43 28  *.**   LIKEFUNC(
71890 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72  zName, nArg, pAr
718a0 67 2c 20 66 6c 61 67 73 29 0a 2a 2a 20 20 20 20  g, flags).**    
718b0 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   Used to create 
718c0 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  a scalar functio
718d0 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  n definition of 
718e0 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65  a function zName
718f0 20 0a 2a 2a 20 20 20 20 20 74 68 61 74 20 61 63   .**     that ac
71900 63 65 70 74 73 20 6e 41 72 67 20 61 72 67 75 6d  cepts nArg argum
71910 65 6e 74 73 20 61 6e 64 20 69 73 20 69 6d 70 6c  ents and is impl
71920 65 6d 65 6e 74 65 64 20 62 79 20 61 20 63 61 6c  emented by a cal
71930 6c 20 74 6f 20 43 20 0a 2a 2a 20 20 20 20 20 66  l to C .**     f
71940 75 6e 63 74 69 6f 6e 20 6c 69 6b 65 46 75 6e 63  unction likeFunc
71950 2e 20 41 72 67 75 6d 65 6e 74 20 70 41 72 67 20  . Argument pArg 
71960 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f  is cast to a (vo
71970 69 64 20 2a 29 20 61 6e 64 20 6d 61 64 65 0a 2a  id *) and made.*
71980 2a 20 20 20 20 20 61 76 61 69 6c 61 62 6c 65 20  *     available 
71990 61 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  as the function 
719a0 75 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74  user-data (sqlit
719b0 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29 2e  e3_user_data()).
719c0 20 54 68 65 0a 2a 2a 20 20 20 20 20 46 75 6e 63   The.**     Func
719d0 44 65 66 2e 66 6c 61 67 73 20 76 61 72 69 61 62  Def.flags variab
719e0 6c 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  le is set to the
719f0 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73   value passed as
71a00 20 74 68 65 20 66 6c 61 67 73 0a 2a 2a 20 20 20   the flags.**   
71a10 20 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a    parameter..*/.
71a20 23 64 65 66 69 6e 65 20 46 55 4e 43 54 49 4f 4e  #define FUNCTION
71a30 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41  (zName, nArg, iA
71a40 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20  rg, bNC, xFunc) 
71a50 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54  \.  {nArg, SQLIT
71a60 45 5f 55 54 46 38 2c 20 28 62 4e 43 2a 53 51 4c  E_UTF8, (bNC*SQL
71a70 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c  ITE_FUNC_NEEDCOL
71a80 4c 29 2c 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f  L), \.   SQLITE_
71a90 49 4e 54 5f 54 4f 5f 50 54 52 28 69 41 72 67 29  INT_TO_PTR(iArg)
71aa0 2c 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30  , 0, xFunc, 0, 0
71ab0 2c 20 23 7a 4e 61 6d 65 2c 20 30 2c 20 30 7d 0a  , #zName, 0, 0}.
71ac0 23 64 65 66 69 6e 65 20 46 55 4e 43 54 49 4f 4e  #define FUNCTION
71ad0 32 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69  2(zName, nArg, i
71ae0 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 2c  Arg, bNC, xFunc,
71af0 20 65 78 74 72 61 46 6c 61 67 73 29 20 5c 0a 20   extraFlags) \. 
71b00 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55   {nArg, SQLITE_U
71b10 54 46 38 2c 20 28 62 4e 43 2a 53 51 4c 49 54 45  TF8, (bNC*SQLITE
71b20 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 29 7c  _FUNC_NEEDCOLL)|
71b30 65 78 74 72 61 46 6c 61 67 73 2c 20 5c 0a 20 20  extraFlags, \.  
71b40 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50   SQLITE_INT_TO_P
71b50 54 52 28 69 41 72 67 29 2c 20 30 2c 20 78 46 75  TR(iArg), 0, xFu
71b60 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65  nc, 0, 0, #zName
71b70 2c 20 30 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20  , 0, 0}.#define 
71b80 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e 61  STR_FUNCTION(zNa
71b90 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20  me, nArg, pArg, 
71ba0 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20  bNC, xFunc) \.  
71bb0 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54  {nArg, SQLITE_UT
71bc0 46 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45 5f 46  F8, bNC*SQLITE_F
71bd0 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a  UNC_NEEDCOLL, \.
71be0 20 20 20 70 41 72 67 2c 20 30 2c 20 78 46 75 6e     pArg, 0, xFun
71bf0 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c  c, 0, 0, #zName,
71c00 20 30 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 4c   0, 0}.#define L
71c10 49 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e  IKEFUNC(zName, n
71c20 41 72 67 2c 20 61 72 67 2c 20 66 6c 61 67 73 29  Arg, arg, flags)
71c30 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49   \.  {nArg, SQLI
71c40 54 45 5f 55 54 46 38 2c 20 66 6c 61 67 73 2c 20  TE_UTF8, flags, 
71c50 28 76 6f 69 64 20 2a 29 61 72 67 2c 20 30 2c 20  (void *)arg, 0, 
71c60 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20  likeFunc, 0, 0, 
71c70 23 7a 4e 61 6d 65 2c 20 30 2c 20 30 7d 0a 23 64  #zName, 0, 0}.#d
71c80 65 66 69 6e 65 20 41 47 47 52 45 47 41 54 45 28  efine AGGREGATE(
71c90 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67  zName, nArg, arg
71ca0 2c 20 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69  , nc, xStep, xFi
71cb0 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20  nal) \.  {nArg, 
71cc0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 6e 63 2a  SQLITE_UTF8, nc*
71cd0 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44  SQLITE_FUNC_NEED
71ce0 43 4f 4c 4c 2c 20 5c 0a 20 20 20 53 51 4c 49 54  COLL, \.   SQLIT
71cf0 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 61 72 67  E_INT_TO_PTR(arg
71d00 29 2c 20 30 2c 20 30 2c 20 78 53 74 65 70 2c 78  ), 0, 0, xStep,x
71d10 46 69 6e 61 6c 2c 23 7a 4e 61 6d 65 2c 30 2c 30  Final,#zName,0,0
71d20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 75 72  }../*.** All cur
71d30 72 65 6e 74 20 73 61 76 65 70 6f 69 6e 74 73 20  rent savepoints 
71d40 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20  are stored in a 
71d50 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 74 61 72  linked list star
71d60 74 69 6e 67 20 61 74 0a 2a 2a 20 73 71 6c 69 74  ting at.** sqlit
71d70 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e 20 54  e3.pSavepoint. T
71d80 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74  he first element
71d90 20 69 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20   in the list is 
71da0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
71db0 79 0a 2a 2a 20 6f 70 65 6e 65 64 20 73 61 76 65  y.** opened save
71dc0 70 6f 69 6e 74 2e 20 53 61 76 65 70 6f 69 6e 74  point. Savepoint
71dd0 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74  s are added to t
71de0 68 65 20 6c 69 73 74 20 62 79 20 74 68 65 20 76  he list by the v
71df0 64 62 65 0a 2a 2a 20 4f 50 5f 53 61 76 65 70 6f  dbe.** OP_Savepo
71e00 69 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  int instruction.
71e10 0a 2a 2f 0a 73 74 72 75 63 74 20 53 61 76 65 70  .*/.struct Savep
71e20 6f 69 6e 74 20 7b 0a 20 20 63 68 61 72 20 2a 7a  oint {.  char *z
71e30 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
71e40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
71e50 53 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 20 28  Savepoint name (
71e60 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 29 20  nul-terminated) 
71e70 2a 2f 0a 20 20 69 36 34 20 6e 44 65 66 65 72 72  */.  i64 nDeferr
71e80 65 64 43 6f 6e 73 3b 20 20 20 20 20 20 20 20 20  edCons;         
71e90 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
71ea0 65 72 20 6f 66 20 64 65 66 65 72 72 65 64 20 66  er of deferred f
71eb0 6b 20 76 69 6f 6c 61 74 69 6f 6e 73 20 2a 2f 0a  k violations */.
71ec0 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65    Savepoint *pNe
71ed0 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  xt;             
71ee0 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20        /* Parent 
71ef0 73 61 76 65 70 6f 69 6e 74 20 28 69 66 20 61 6e  savepoint (if an
71f00 79 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  y) */.};../*.** 
71f10 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72  The following ar
71f20 65 20 75 73 65 64 20 61 73 20 74 68 65 20 73 65  e used as the se
71f30 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74  cond parameter t
71f40 6f 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69  o sqlite3Savepoi
71f50 6e 74 28 29 2c 0a 2a 2a 20 61 6e 64 20 61 73 20  nt(),.** and as 
71f60 74 68 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20  the P1 argument 
71f70 74 6f 20 74 68 65 20 4f 50 5f 53 61 76 65 70 6f  to the OP_Savepo
71f80 69 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  int instruction.
71f90 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 41 56 45  .*/.#define SAVE
71fa0 50 4f 49 4e 54 5f 42 45 47 49 4e 20 20 20 20 20  POINT_BEGIN     
71fb0 20 30 0a 23 64 65 66 69 6e 65 20 53 41 56 45 50   0.#define SAVEP
71fc0 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 20 20 20  OINT_RELEASE    
71fd0 31 0a 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f  1.#define SAVEPO
71fe0 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 20 20 32  INT_ROLLBACK   2
71ff0 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51  .../*.** Each SQ
72000 4c 69 74 65 20 6d 6f 64 75 6c 65 20 28 76 69 72  Lite module (vir
72010 74 75 61 6c 20 74 61 62 6c 65 20 64 65 66 69 6e  tual table defin
72020 69 74 69 6f 6e 29 20 69 73 20 64 65 66 69 6e 65  ition) is define
72030 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61  d by an.** insta
72040 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
72050 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2c 20  wing structure, 
72060 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71  stored in the sq
72070 6c 69 74 65 33 2e 61 4d 6f 64 75 6c 65 0a 2a 2a  lite3.aModule.**
72080 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a   hash table..*/.
72090 73 74 72 75 63 74 20 4d 6f 64 75 6c 65 20 7b 0a  struct Module {.
720a0 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
720b0 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
720c0 20 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61         /* Callba
720d0 63 6b 20 70 6f 69 6e 74 65 72 73 20 2a 2f 0a 20  ck pointers */. 
720e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
720f0 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
72100 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 70 61        /* Name pa
72110 73 73 65 64 20 74 6f 20 63 72 65 61 74 65 5f 6d  ssed to create_m
72120 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 76 6f 69  odule() */.  voi
72130 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20  d *pAux;        
72140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72150 20 20 2f 2a 20 70 41 75 78 20 70 61 73 73 65 64    /* pAux passed
72160 20 74 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c   to create_modul
72170 65 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  e() */.  void (*
72180 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a  xDestroy)(void *
72190 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  );            /*
721a0 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74   Module destruct
721b0 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 7d  or function */.}
721c0 3b 0a 0a 2f 2a 0a 2a 2a 20 69 6e 66 6f 72 6d 61  ;../*.** informa
721d0 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20  tion about each 
721e0 63 6f 6c 75 6d 6e 20 6f 66 20 61 6e 20 53 51 4c  column of an SQL
721f0 20 74 61 62 6c 65 20 69 73 20 68 65 6c 64 20 69   table is held i
72200 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a  n an instance.**
72210 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
72220 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f  re..*/.struct Co
72230 6c 75 6d 6e 20 7b 0a 20 20 63 68 61 72 20 2a 7a  lumn {.  char *z
72240 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d  Name;     /* Nam
72250 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e  e of this column
72260 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 66 6c   */.  Expr *pDfl
72270 74 3b 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c  t;     /* Defaul
72280 74 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20  t value of this 
72290 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72  column */.  char
722a0 20 2a 7a 44 66 6c 74 3b 20 20 20 20 20 2f 2a 20   *zDflt;     /* 
722b0 4f 72 69 67 69 6e 61 6c 20 74 65 78 74 20 6f 66  Original text of
722c0 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c   the default val
722d0 75 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54  ue */.  char *zT
722e0 79 70 65 3b 20 20 20 20 20 2f 2a 20 44 61 74 61  ype;     /* Data
722f0 20 74 79 70 65 20 66 6f 72 20 74 68 69 73 20 63   type for this c
72300 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20  olumn */.  char 
72310 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a 20 43  *zColl;     /* C
72320 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
72330 65 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 75 73 65  e.  If NULL, use
72340 20 74 68 65 20 64 65 66 61 75 6c 74 20 2a 2f 0a   the default */.
72350 20 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20    u8 notNull;   
72360 20 20 20 2f 2a 20 41 6e 20 4f 45 5f 20 63 6f 64     /* An OE_ cod
72370 65 20 66 6f 72 20 68 61 6e 64 6c 69 6e 67 20 61  e for handling a
72380 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72   NOT NULL constr
72390 61 69 6e 74 20 2a 2f 0a 20 20 63 68 61 72 20 61  aint */.  char a
723a0 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 4f 6e  ffinity;   /* On
723b0 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f  e of the SQLITE_
723c0 41 46 46 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 2a  AFF_... values *
723d0 2f 0a 20 20 75 31 36 20 63 6f 6c 46 6c 61 67 73  /.  u16 colFlags
723e0 3b 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 20  ;    /* Boolean 
723f0 70 72 6f 70 65 72 74 69 65 73 2e 20 20 53 65 65  properties.  See
72400 20 43 4f 4c 46 4c 41 47 5f 20 64 65 66 69 6e 65   COLFLAG_ define
72410 73 20 62 65 6c 6f 77 20 2a 2f 0a 7d 3b 0a 0a 2f  s below */.};../
72420 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
72430 20 66 6f 72 20 43 6f 6c 75 6d 6e 2e 63 6f 6c 46   for Column.colF
72440 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65  lags:.*/.#define
72450 20 43 4f 4c 46 4c 41 47 5f 50 52 49 4d 4b 45 59   COLFLAG_PRIMKEY
72460 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20 43    0x0001    /* C
72470 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66  olumn is part of
72480 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
72490 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4f 4c 46   */.#define COLF
724a0 4c 41 47 5f 48 49 44 44 45 4e 20 20 20 30 78 30  LAG_HIDDEN   0x0
724b0 30 30 32 20 20 20 20 2f 2a 20 41 20 68 69 64 64  002    /* A hidd
724c0 65 6e 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76  en column in a v
724d0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
724e0 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c 61 74  ./*.** A "Collat
724f0 69 6e 67 20 53 65 71 75 65 6e 63 65 22 20 69 73  ing Sequence" is
72500 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 69   defined by an i
72510 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
72520 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75  ollowing.** stru
72530 63 74 75 72 65 2e 20 43 6f 6e 63 65 70 74 75 61  cture. Conceptua
72540 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69 6e 67  lly, a collating
72550 20 73 65 71 75 65 6e 63 65 20 63 6f 6e 73 69 73   sequence consis
72560 74 73 20 6f 66 20 61 20 6e 61 6d 65 20 61 6e 64  ts of a name and
72570 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e  .** a comparison
72580 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64 65   routine that de
72590 66 69 6e 65 73 20 74 68 65 20 6f 72 64 65 72 20  fines the order 
725a0 6f 66 20 74 68 61 74 20 73 65 71 75 65 6e 63 65  of that sequence
725b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61  ..**.** There ma
725c0 79 20 74 77 6f 20 73 65 70 61 72 61 74 65 20 69  y two separate i
725d0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
725e0 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  f the collation 
725f0 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a 2a 2a  function, one.**
72600 20 74 68 61 74 20 70 72 6f 63 65 73 73 65 73 20   that processes 
72610 74 65 78 74 20 69 6e 20 55 54 46 2d 38 20 65 6e  text in UTF-8 en
72620 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65 71 2e  coding (CollSeq.
72630 78 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74 68 65  xCmp) and anothe
72640 72 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63 65 73  r that.** proces
72650 73 65 73 20 74 65 78 74 20 65 6e 63 6f 64 65 64  ses text encoded
72660 20 69 6e 20 55 54 46 2d 31 36 20 28 43 6f 6c 6c   in UTF-16 (Coll
72670 53 65 71 2e 78 43 6d 70 31 36 29 2c 20 75 73 69  Seq.xCmp16), usi
72680 6e 67 20 74 68 65 20 6d 61 63 68 69 6e 65 0a 2a  ng the machine.*
72690 2a 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72  * native byte or
726a0 64 65 72 2e 20 57 68 65 6e 20 61 20 63 6f 6c 6c  der. When a coll
726b0 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69  ation sequence i
726c0 73 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c 69 74  s invoked, SQLit
726d0 65 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74 68 65  e selects.** the
726e0 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20 77 69   version that wi
726f0 6c 6c 20 72 65 71 75 69 72 65 20 74 68 65 20 6c  ll require the l
72700 65 61 73 74 20 65 78 70 65 6e 73 69 76 65 20 65  east expensive e
72710 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61 6e 73  ncoding.** trans
72720 6c 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e 79 2e  lations, if any.
72730 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c 6c 53  .**.** The CollS
72740 65 71 2e 70 55 73 65 72 20 6d 65 6d 62 65 72 20  eq.pUser member 
72750 76 61 72 69 61 62 6c 65 20 69 73 20 61 6e 20 65  variable is an e
72760 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 74  xtra parameter t
72770 68 61 74 20 70 61 73 73 65 64 20 69 6e 0a 2a 2a  hat passed in.**
72780 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
72790 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 55 54  gument to the UT
727a0 46 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66  F-8 comparison f
727b0 75 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e 0a 2a  unction, xCmp..*
727c0 2a 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 31  * CollSeq.pUser1
727d0 36 20 69 73 20 74 68 65 20 65 71 75 69 76 61 6c  6 is the equival
727e0 65 6e 74 20 66 6f 72 20 74 68 65 20 55 54 46 2d  ent for the UTF-
727f0 31 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75  16 comparison fu
72800 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d 70 31  nction,.** xCmp1
72810 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f 74 68  6..**.** If both
72820 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20 61 6e   CollSeq.xCmp an
72830 64 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36  d CollSeq.xCmp16
72840 20 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20 6d 65   are NULL, it me
72850 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ans that the.** 
72860 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
72870 63 65 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  ce is undefined.
72880 20 20 49 6e 64 69 63 65 73 20 62 75 69 6c 74 20    Indices built 
72890 6f 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a  on an undefined.
728a0 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  ** collating seq
728b0 75 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20 62 65  uence may not be
728c0 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e   read or written
728d0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 6c  ..*/.struct Coll
728e0 53 65 71 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e  Seq {.  char *zN
728f0 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ame;          /*
72900 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c   Name of the col
72910 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2c  lating sequence,
72920 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a   UTF-8 encoded *
72930 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20  /.  u8 enc;     
72940 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
72950 74 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e 64 6c  t encoding handl
72960 65 64 20 62 79 20 78 43 6d 70 28 29 20 2a 2f 0a  ed by xCmp() */.
72970 20 20 76 6f 69 64 20 2a 70 55 73 65 72 3b 20 20    void *pUser;  
72980 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
72990 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6d   argument to xCm
729a0 70 28 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78  p() */.  int (*x
729b0 43 6d 70 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20  Cmp)(void*,int, 
729c0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74  const void*, int
729d0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a  , const void*);.
729e0 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
729f0 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72  oid*);  /* Destr
72a00 75 63 74 6f 72 20 66 6f 72 20 70 55 73 65 72 20  uctor for pUser 
72a10 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73  */.};../*.** A s
72a20 6f 72 74 20 6f 72 64 65 72 20 63 61 6e 20 62 65  ort order can be
72a30 20 65 69 74 68 65 72 20 41 53 43 20 6f 72 20 44   either ASC or D
72a40 45 53 43 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ESC..*/.#define 
72a50 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 20 20  SQLITE_SO_ASC   
72a60 20 20 20 20 30 20 20 2f 2a 20 53 6f 72 74 20 69      0  /* Sort i
72a70 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  n ascending orde
72a80 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  r */.#define SQL
72a90 49 54 45 5f 53 4f 5f 44 45 53 43 20 20 20 20 20  ITE_SO_DESC     
72aa0 20 31 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61   1  /* Sort in a
72ab0 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a  scending order *
72ac0 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20  /../*.** Column 
72ad0 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 2e 0a  affinity types..
72ae0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 75 73 65 64  **.** These used
72af0 20 74 6f 20 68 61 76 65 20 6d 6e 65 6d 6f 6e 69   to have mnemoni
72b00 63 20 6e 61 6d 65 20 6c 69 6b 65 20 27 69 27 20  c name like 'i' 
72b10 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 49  for SQLITE_AFF_I
72b20 4e 54 45 47 45 52 20 61 6e 64 0a 2a 2a 20 27 74  NTEGER and.** 't
72b30 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46  ' for SQLITE_AFF
72b40 5f 54 45 58 54 2e 20 20 42 75 74 20 77 65 20 63  _TEXT.  But we c
72b50 61 6e 20 73 61 76 65 20 61 20 6c 69 74 74 6c 65  an save a little
72b60 20 73 70 61 63 65 20 61 6e 64 20 69 6d 70 72 6f   space and impro
72b70 76 65 0a 2a 2a 20 74 68 65 20 73 70 65 65 64 20  ve.** the speed 
72b80 61 20 6c 69 74 74 6c 65 20 62 79 20 6e 75 6d 62  a little by numb
72b90 65 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65 73  ering the values
72ba0 20 63 6f 6e 73 65 63 75 74 69 76 65 6c 79 2e 20   consecutively. 
72bb0 20 0a 2a 2a 0a 2a 2a 20 42 75 74 20 72 61 74 68   .**.** But rath
72bc0 65 72 20 74 68 61 6e 20 73 74 61 72 74 20 77 69  er than start wi
72bd0 74 68 20 30 20 6f 72 20 31 2c 20 77 65 20 62 65  th 0 or 1, we be
72be0 67 69 6e 20 77 69 74 68 20 27 61 27 2e 20 20 54  gin with 'a'.  T
72bf0 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 68 65 6e  hat way,.** when
72c00 20 6d 75 6c 74 69 70 6c 65 20 61 66 66 69 6e 69   multiple affini
72c10 74 79 20 74 79 70 65 73 20 61 72 65 20 63 6f 6e  ty types are con
72c20 63 61 74 65 6e 61 74 65 64 20 69 6e 74 6f 20 61  catenated into a
72c30 20 73 74 72 69 6e 67 20 61 6e 64 0a 2a 2a 20 75   string and.** u
72c40 73 65 64 20 61 73 20 74 68 65 20 50 34 20 6f 70  sed as the P4 op
72c50 65 72 61 6e 64 2c 20 74 68 65 79 20 77 69 6c 6c  erand, they will
72c60 20 62 65 20 6d 6f 72 65 20 72 65 61 64 61 62 6c   be more readabl
72c70 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 61 6c  e..**.** Note al
72c80 73 6f 20 74 68 61 74 20 74 68 65 20 6e 75 6d 65  so that the nume
72c90 72 69 63 20 74 79 70 65 73 20 61 72 65 20 67 72  ric types are gr
72ca0 6f 75 70 65 64 20 74 6f 67 65 74 68 65 72 20 73  ouped together s
72cb0 6f 20 74 68 61 74 20 74 65 73 74 69 6e 67 0a 2a  o that testing.*
72cc0 2a 20 66 6f 72 20 61 20 6e 75 6d 65 72 69 63 20  * for a numeric 
72cd0 74 79 70 65 20 69 73 20 61 20 73 69 6e 67 6c 65  type is a single
72ce0 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a   comparison..*/.
72cf0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
72d00 46 46 5f 54 45 58 54 20 20 20 20 20 27 61 27 0a  FF_TEXT     'a'.
72d10 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
72d20 46 46 5f 4e 4f 4e 45 20 20 20 20 20 27 62 27 0a  FF_NONE     'b'.
72d30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
72d40 46 46 5f 4e 55 4d 45 52 49 43 20 20 27 63 27 0a  FF_NUMERIC  'c'.
72d50 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
72d60 46 46 5f 49 4e 54 45 47 45 52 20 20 27 64 27 0a  FF_INTEGER  'd'.
72d70 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
72d80 46 46 5f 52 45 41 4c 20 20 20 20 20 27 65 27 0a  FF_REAL     'e'.
72d90 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
72da0 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74  IsNumericAffinit
72db0 79 28 58 29 20 20 28 28 58 29 3e 3d 53 51 4c 49  y(X)  ((X)>=SQLI
72dc0 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 29 0a  TE_AFF_NUMERIC).
72dd0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54  ./*.** The SQLIT
72de0 45 5f 41 46 46 5f 4d 41 53 4b 20 76 61 6c 75 65  E_AFF_MASK value
72df0 73 20 6d 61 73 6b 73 20 6f 66 66 20 74 68 65 20  s masks off the 
72e00 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74 73  significant bits
72e10 20 6f 66 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69   of an.** affini
72e20 74 79 20 76 61 6c 75 65 2e 20 0a 2a 2f 0a 23 64  ty value. .*/.#d
72e30 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46  efine SQLITE_AFF
72e40 5f 4d 41 53 4b 20 20 20 20 20 30 78 36 37 0a 0a  _MASK     0x67..
72e50 2f 2a 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c  /*.** Additional
72e60 20 62 69 74 20 76 61 6c 75 65 73 20 74 68 61 74   bit values that
72e70 20 63 61 6e 20 62 65 20 4f 52 65 64 20 77 69 74   can be ORed wit
72e80 68 20 61 6e 20 61 66 66 69 6e 69 74 79 20 77 69  h an affinity wi
72e90 74 68 6f 75 74 0a 2a 2a 20 63 68 61 6e 67 69 6e  thout.** changin
72ea0 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 2e 0a  g the affinity..
72eb0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
72ec0 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 20 20 30  E_JUMPIFNULL   0
72ed0 78 30 38 20 20 2f 2a 20 6a 75 6d 70 73 20 69 66  x08  /* jumps if
72ee0 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20   either operand 
72ef0 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69  is NULL */.#defi
72f00 6e 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50  ne SQLITE_STOREP
72f10 32 20 20 20 20 20 20 30 78 31 30 20 20 2f 2a 20  2      0x10  /* 
72f20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20  Store result in 
72f30 72 65 67 5b 50 32 5d 20 72 61 74 68 65 72 20 74  reg[P2] rather t
72f40 68 61 6e 20 6a 75 6d 70 20 2a 2f 0a 23 64 65 66  han jump */.#def
72f50 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45  ine SQLITE_NULLE
72f60 51 20 20 20 20 20 20 20 30 78 38 30 20 20 2f 2a  Q       0x80  /*
72f70 20 4e 55 4c 4c 3d 4e 55 4c 4c 20 2a 2f 0a 0a 2f   NULL=NULL */../
72f80 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 20 6f  *.** An object o
72f90 66 20 74 68 69 73 20 74 79 70 65 20 69 73 20 63  f this type is c
72fa0 72 65 61 74 65 64 20 66 6f 72 20 65 61 63 68 20  reated for each 
72fb0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70 72  virtual table pr
72fc0 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 74 68 65 20  esent in.** the 
72fd0 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e  database schema.
72fe0 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64   .**.** If the d
72ff0 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69  atabase schema i
73000 73 20 73 68 61 72 65 64 2c 20 74 68 65 6e 20 74  s shared, then t
73010 68 65 72 65 20 69 73 20 6f 6e 65 20 69 6e 73 74  here is one inst
73020 61 6e 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20  ance of this.** 
73030 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 65 61  structure for ea
73040 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ch database conn
73050 65 63 74 69 6f 6e 20 28 73 71 6c 69 74 65 33 2a  ection (sqlite3*
73060 29 20 74 68 61 74 20 75 73 65 73 20 74 68 65 20  ) that uses the 
73070 73 68 61 72 65 64 0a 2a 2a 20 73 63 68 65 6d 61  shared.** schema
73080 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73  . This is becaus
73090 65 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20  e each database 
730a0 63 6f 6e 6e 65 63 74 69 6f 6e 20 72 65 71 75 69  connection requi
730b0 72 65 73 20 69 74 73 20 6f 77 6e 20 75 6e 69 71  res its own uniq
730c0 75 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f  ue.** instance o
730d0 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74  f the sqlite3_vt
730e0 61 62 2a 20 68 61 6e 64 6c 65 20 75 73 65 64 20  ab* handle used 
730f0 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 76 69  to access the vi
73100 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20  rtual table .** 
73110 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20  implementation. 
73120 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 20 68 61  sqlite3_vtab* ha
73130 6e 64 6c 65 73 20 63 61 6e 20 6e 6f 74 20 62 65  ndles can not be
73140 20 73 68 61 72 65 64 20 62 65 74 77 65 65 6e 20   shared between 
73150 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
73160 6e 65 63 74 69 6f 6e 73 2c 20 65 76 65 6e 20 77  nections, even w
73170 68 65 6e 20 74 68 65 20 72 65 73 74 20 6f 66 20  hen the rest of 
73180 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61  the in-memory da
73190 74 61 62 61 73 65 20 0a 2a 2a 20 73 63 68 65 6d  tabase .** schem
731a0 61 20 69 73 20 73 68 61 72 65 64 2c 20 61 73 20  a is shared, as 
731b0 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
731c0 6f 6e 20 6f 66 74 65 6e 20 73 74 6f 72 65 73 20  on often stores 
731d0 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
731e0 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c  connection handl
731f0 65 20 70 61 73 73 65 64 20 74 6f 20 69 74 20 76  e passed to it v
73200 69 61 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 28  ia the xConnect(
73210 29 20 6f 72 20 78 43 72 65 61 74 65 28 29 20 6d  ) or xCreate() m
73220 65 74 68 6f 64 0a 2a 2a 20 64 75 72 69 6e 67 20  ethod.** during 
73230 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69  initialization i
73240 6e 74 65 72 6e 61 6c 6c 79 2e 20 54 68 69 73 20  nternally. This 
73250 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
73260 69 6f 6e 20 68 61 6e 64 6c 65 20 6d 61 79 0a 2a  ion handle may.*
73270 2a 20 74 68 65 6e 20 62 65 20 75 73 65 64 20 62  * then be used b
73280 79 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  y the virtual ta
73290 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
732a0 6f 6e 20 74 6f 20 61 63 63 65 73 73 20 72 65 61  on to access rea
732b0 6c 20 74 61 62 6c 65 73 20 0a 2a 2a 20 77 69 74  l tables .** wit
732c0 68 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hin the database
732d0 2e 20 53 6f 20 74 68 61 74 20 74 68 65 79 20 61  . So that they a
732e0 70 70 65 61 72 20 61 73 20 70 61 72 74 20 6f 66  ppear as part of
732f0 20 74 68 65 20 63 61 6c 6c 65 72 73 20 0a 2a 2a   the callers .**
73300 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68   transaction, th
73310 65 73 65 20 61 63 63 65 73 73 65 73 20 6e 65 65  ese accesses nee
73320 64 20 74 6f 20 62 65 20 6d 61 64 65 20 76 69 61  d to be made via
73330 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
73340 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  se .** connectio
73350 6e 20 61 73 20 74 68 61 74 20 75 73 65 64 20 74  n as that used t
73360 6f 20 65 78 65 63 75 74 65 20 53 51 4c 20 6f 70  o execute SQL op
73370 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20  erations on the 
73380 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
73390 2a 0a 2a 2a 20 41 6c 6c 20 56 54 61 62 6c 65 20  *.** All VTable 
733a0 6f 62 6a 65 63 74 73 20 74 68 61 74 20 63 6f 72  objects that cor
733b0 72 65 73 70 6f 6e 64 20 74 6f 20 61 20 73 69 6e  respond to a sin
733c0 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 61 20 73  gle table in a s
733d0 68 61 72 65 64 0a 2a 2a 20 64 61 74 61 62 61 73  hared.** databas
733e0 65 20 73 63 68 65 6d 61 20 61 72 65 20 69 6e 69  e schema are ini
733f0 74 69 61 6c 6c 79 20 73 74 6f 72 65 64 20 69 6e  tially stored in
73400 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 70   a linked-list p
73410 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20  ointed to by.** 
73420 74 68 65 20 54 61 62 6c 65 2e 70 56 54 61 62 6c  the Table.pVTabl
73430 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c  e member variabl
73440 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70  e of the corresp
73450 6f 6e 64 69 6e 67 20 54 61 62 6c 65 20 6f 62 6a  onding Table obj
73460 65 63 74 2e 0a 2a 2a 20 57 68 65 6e 20 61 6e 20  ect..** When an 
73470 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
73480 29 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72  ) operation is r
73490 65 71 75 69 72 65 64 20 74 6f 20 61 63 63 65 73  equired to acces
734a0 73 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a  s the virtual.**
734b0 20 74 61 62 6c 65 2c 20 69 74 20 73 65 61 72 63   table, it searc
734c0 68 65 73 20 74 68 65 20 6c 69 73 74 20 66 6f 72  hes the list for
734d0 20 74 68 65 20 56 54 61 62 6c 65 20 74 68 61 74   the VTable that
734e0 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20   corresponds to 
734f0 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
73500 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 6f 69 6e 67  connection doing
73510 20 74 68 65 20 70 72 65 70 61 72 69 6e 67 20 73   the preparing s
73520 6f 20 61 73 20 74 6f 20 75 73 65 20 74 68 65 20  o as to use the 
73530 63 6f 72 72 65 63 74 0a 2a 2a 20 73 71 6c 69 74  correct.** sqlit
73540 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 20  e3_vtab* handle 
73550 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20  in the compiled 
73560 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  query..**.** Whe
73570 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 54  n an in-memory T
73580 61 62 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 64  able object is d
73590 65 6c 65 74 65 64 20 28 66 6f 72 20 65 78 61 6d  eleted (for exam
735a0 70 6c 65 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20  ple when the.** 
735b0 73 63 68 65 6d 61 20 69 73 20 62 65 69 6e 67 20  schema is being 
735c0 72 65 6c 6f 61 64 65 64 20 66 6f 72 20 73 6f 6d  reloaded for som
735d0 65 20 72 65 61 73 6f 6e 29 2c 20 74 68 65 20 56  e reason), the V
735e0 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 61 72  Table objects ar
735f0 65 20 6e 6f 74 20 0a 2a 2a 20 64 65 6c 65 74 65  e not .** delete
73600 64 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65  d and the sqlite
73610 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 73 20  3_vtab* handles 
73620 61 72 65 20 6e 6f 74 20 78 44 69 73 63 6f 6e 6e  are not xDisconn
73630 65 63 74 28 29 65 64 20 0a 2a 2a 20 69 6d 6d 65  ect()ed .** imme
73640 64 69 61 74 65 6c 79 2e 20 49 6e 73 74 65 61 64  diately. Instead
73650 2c 20 74 68 65 79 20 61 72 65 20 6d 6f 76 65 64  , they are moved
73660 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 2e   from the Table.
73670 70 56 54 61 62 6c 65 20 6c 69 73 74 20 74 6f 0a  pVTable list to.
73680 2a 2a 20 61 6e 6f 74 68 65 72 20 6c 69 6e 6b 65  ** another linke
73690 64 20 6c 69 73 74 20 68 65 61 64 65 64 20 62 79  d list headed by
736a0 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69   the sqlite3.pDi
736b0 73 63 6f 6e 6e 65 63 74 20 6d 65 6d 62 65 72 20  sconnect member 
736c0 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73  of the.** corres
736d0 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 20  ponding sqlite3 
736e0 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 79 20  structure. They 
736f0 61 72 65 20 74 68 65 6e 20 64 65 6c 65 74 65 64  are then deleted
73700 2f 78 44 69 73 63 6f 6e 6e 65 63 74 65 64 20 0a  /xDisconnected .
73710 2a 2a 20 6e 65 78 74 20 74 69 6d 65 20 61 20 73  ** next time a s
73720 74 61 74 65 6d 65 6e 74 20 69 73 20 70 72 65 70  tatement is prep
73730 61 72 65 64 20 75 73 69 6e 67 20 73 61 69 64 20  ared using said 
73740 73 71 6c 69 74 65 33 2a 2e 20 54 68 69 73 20 69  sqlite3*. This i
73750 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 61 76 6f  s done.** to avo
73760 69 64 20 64 65 61 64 6c 6f 63 6b 20 69 73 73 75  id deadlock issu
73770 65 73 20 69 6e 76 6f 6c 76 69 6e 67 20 6d 75 6c  es involving mul
73780 74 69 70 6c 65 20 73 71 6c 69 74 65 33 2e 6d 75  tiple sqlite3.mu
73790 74 65 78 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 20  tex mutexes..** 
737a0 52 65 66 65 72 20 74 6f 20 63 6f 6d 6d 65 6e 74  Refer to comment
737b0 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e  s above function
737c0 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f   sqlite3VtabUnlo
737d0 63 6b 4c 69 73 74 28 29 20 66 6f 72 20 61 6e 0a  ckList() for an.
737e0 2a 2a 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 61  ** explanation a
737f0 73 20 74 6f 20 77 68 79 20 69 74 20 69 73 20 73  s to why it is s
73800 61 66 65 20 74 6f 20 61 64 64 20 61 6e 20 65 6e  afe to add an en
73810 74 72 79 20 74 6f 20 61 6e 20 73 71 6c 69 74 65  try to an sqlite
73820 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 0a 2a 2a  3.pDisconnect.**
73830 20 6c 69 73 74 20 77 69 74 68 6f 75 74 20 68 6f   list without ho
73840 6c 64 69 6e 67 20 74 68 65 20 63 6f 72 72 65 73  lding the corres
73850 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 2e  ponding sqlite3.
73860 6d 75 74 65 78 20 6d 75 74 65 78 2e 0a 2a 2a 0a  mutex mutex..**.
73870 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 66 6f  ** The memory fo
73880 72 20 6f 62 6a 65 63 74 73 20 6f 66 20 74 68 69  r objects of thi
73890 73 20 74 79 70 65 20 69 73 20 61 6c 77 61 79 73  s type is always
738a0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a   allocated by .*
738b0 2a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  * sqlite3DbMallo
738c0 63 28 29 2c 20 75 73 69 6e 67 20 74 68 65 20 63  c(), using the c
738d0 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65  onnection handle
738e0 20 73 74 6f 72 65 64 20 69 6e 20 56 54 61 62 6c   stored in VTabl
738f0 65 2e 64 62 20 61 73 20 0a 2a 2a 20 74 68 65 20  e.db as .** the 
73900 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a  first argument..
73910 2a 2f 0a 73 74 72 75 63 74 20 56 54 61 62 6c 65  */.struct VTable
73920 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   {.  sqlite3 *db
73930 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
73940 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
73950 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64  ction associated
73960 20 77 69 74 68 20 74 68 69 73 20 74 61 62 6c 65   with this table
73970 20 2a 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d   */.  Module *pM
73980 6f 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  od;             
73990 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d 6f  /* Pointer to mo
739a0 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  dule implementat
739b0 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ion */.  sqlite3
739c0 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20  _vtab *pVtab;   
739d0 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
739e0 20 76 74 61 62 20 69 6e 73 74 61 6e 63 65 20 2a   vtab instance *
739f0 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20  /.  int nRef;   
73a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
73a10 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74   Number of point
73a20 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75  ers to this stru
73a30 63 74 75 72 65 20 2a 2f 0a 20 20 75 38 20 62 43  cture */.  u8 bC
73a40 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20  onstraint;      
73a50 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
73a60 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20  constraints are 
73a70 73 75 70 70 6f 72 74 65 64 20 2a 2f 0a 20 20 69  supported */.  i
73a80 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 3b 20 20  nt iSavepoint;  
73a90 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74           /* Dept
73aa0 68 20 6f 66 20 74 68 65 20 53 41 56 45 50 4f 49  h of the SAVEPOI
73ab0 4e 54 20 73 74 61 63 6b 20 2a 2f 0a 20 20 56 54  NT stack */.  VT
73ac0 61 62 6c 65 20 2a 70 4e 65 78 74 3b 20 20 20 20  able *pNext;    
73ad0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
73ae0 69 6e 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28  in linked list (
73af0 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 7d 3b  see above) */.};
73b00 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c  ../*.** Each SQL
73b10 20 74 61 62 6c 65 20 69 73 20 72 65 70 72 65 73   table is repres
73b20 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20  ented in memory 
73b30 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  by an instance o
73b40 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69  f the.** followi
73b50 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
73b60 0a 2a 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d 65 20  .** Table.zName 
73b70 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
73b80 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 63  he table.  The c
73b90 61 73 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69  ase of the origi
73ba0 6e 61 6c 0a 2a 2a 20 43 52 45 41 54 45 20 54 41  nal.** CREATE TA
73bb0 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73  BLE statement is
73bc0 20 73 74 6f 72 65 64 2c 20 62 75 74 20 63 61 73   stored, but cas
73bd0 65 20 69 73 20 6e 6f 74 20 73 69 67 6e 69 66 69  e is not signifi
73be0 63 61 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70  cant for.** comp
73bf0 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54  arisons..**.** T
73c00 61 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 68 65  able.nCol is the
73c10 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
73c20 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65  ns in this table
73c30 2e 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20 69 73  .  Table.aCol is
73c40 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f   a.** pointer to
73c50 20 61 6e 20 61 72 72 61 79 20 6f 66 20 43 6f 6c   an array of Col
73c60 75 6d 6e 20 73 74 72 75 63 74 75 72 65 73 2c 20  umn structures, 
73c70 6f 6e 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c  one for each col
73c80 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  umn..**.** If th
73c90 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49  e table has an I
73ca0 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
73cb0 45 59 2c 20 74 68 65 6e 20 54 61 62 6c 65 2e 69  EY, then Table.i
73cc0 50 4b 65 79 20 69 73 20 74 68 65 20 69 6e 64 65  PKey is the inde
73cd0 78 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75  x of.** the colu
73ce0 6d 6e 20 74 68 61 74 20 69 73 20 74 68 61 74 20  mn that is that 
73cf0 6b 65 79 2e 20 20 20 4f 74 68 65 72 77 69 73 65  key.   Otherwise
73d00 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20   Table.iPKey is 
73d10 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 0a  negative.  Note.
73d20 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61 74 61  ** that the data
73d30 74 79 70 65 20 6f 66 20 74 68 65 20 50 52 49 4d  type of the PRIM
73d40 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20  ARY KEY must be 
73d50 49 4e 54 45 47 45 52 20 66 6f 72 20 74 68 69 73  INTEGER for this
73d60 20 66 69 65 6c 64 20 74 6f 0a 2a 2a 20 62 65 20   field to.** be 
73d70 73 65 74 2e 20 20 41 6e 20 49 4e 54 45 47 45 52  set.  An INTEGER
73d80 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20   PRIMARY KEY is 
73d90 75 73 65 64 20 61 73 20 74 68 65 20 72 6f 77 69  used as the rowi
73da0 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f  d for each row o
73db0 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20  f.** the table. 
73dc0 20 49 66 20 61 20 74 61 62 6c 65 20 68 61 73 20   If a table has 
73dd0 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  no INTEGER PRIMA
73de0 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 61 20 72  RY KEY, then a r
73df0 61 6e 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a 20 69  andom rowid.** i
73e00 73 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20  s generated for 
73e10 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65 20  each row of the 
73e20 74 61 62 6c 65 2e 20 20 54 46 5f 48 61 73 50 72  table.  TF_HasPr
73e30 69 6d 61 72 79 4b 65 79 20 69 73 20 73 65 74 20  imaryKey is set 
73e40 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20  if.** the table 
73e50 68 61 73 20 61 6e 79 20 50 52 49 4d 41 52 59 20  has any PRIMARY 
73e60 4b 45 59 2c 20 49 4e 54 45 47 45 52 20 6f 72 20  KEY, INTEGER or 
73e70 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a  otherwise..**.**
73e80 20 54 61 62 6c 65 2e 74 6e 75 6d 20 69 73 20 74   Table.tnum is t
73e90 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66  he page number f
73ea0 6f 72 20 74 68 65 20 72 6f 6f 74 20 42 54 72 65  or the root BTre
73eb0 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 74 61  e page of the ta
73ec0 62 6c 65 20 69 6e 20 74 68 65 0a 2a 2a 20 64 61  ble in the.** da
73ed0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66  tabase file.  If
73ee0 20 54 61 62 6c 65 2e 69 44 62 20 69 73 20 74 68   Table.iDb is th
73ef0 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64  e index of the d
73f00 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 62 61  atabase table ba
73f10 63 6b 65 6e 64 0a 2a 2a 20 69 6e 20 73 71 6c 69  ckend.** in sqli
73f20 74 65 2e 61 44 62 5b 5d 2e 20 20 30 20 69 73 20  te.aDb[].  0 is 
73f30 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74  for the main dat
73f40 61 62 61 73 65 20 61 6e 64 20 31 20 69 73 20 66  abase and 1 is f
73f50 6f 72 20 74 68 65 20 66 69 6c 65 20 74 68 61 74  or the file that
73f60 0a 2a 2a 20 68 6f 6c 64 73 20 74 65 6d 70 6f 72  .** holds tempor
73f70 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 20 69  ary tables and i
73f80 6e 64 69 63 65 73 2e 20 20 49 66 20 54 46 5f 45  ndices.  If TF_E
73f90 70 68 65 6d 65 72 61 6c 20 69 73 20 73 65 74 0a  phemeral is set.
73fa0 2a 2a 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c  ** then the tabl
73fb0 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61  e is stored in a
73fc0 20 66 69 6c 65 20 74 68 61 74 20 69 73 20 61 75   file that is au
73fd0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65  tomatically dele
73fe0 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  ted.** when the 
73ff0 56 44 42 45 20 63 75 72 73 6f 72 20 74 6f 20 74  VDBE cursor to t
74000 68 65 20 74 61 62 6c 65 20 69 73 20 63 6c 6f 73  he table is clos
74010 65 64 2e 20 20 49 6e 20 74 68 69 73 20 63 61 73  ed.  In this cas
74020 65 20 54 61 62 6c 65 2e 74 6e 75 6d 20 0a 2a 2a  e Table.tnum .**
74030 20 72 65 66 65 72 73 20 56 44 42 45 20 63 75 72   refers VDBE cur
74040 73 6f 72 20 6e 75 6d 62 65 72 20 74 68 61 74 20  sor number that 
74050 68 6f 6c 64 73 20 74 68 65 20 74 61 62 6c 65 20  holds the table 
74060 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f 20 74 68 65  open, not to the
74070 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 6e 75   root.** page nu
74080 6d 62 65 72 2e 20 20 54 72 61 6e 73 69 65 6e 74  mber.  Transient
74090 20 74 61 62 6c 65 73 20 61 72 65 20 75 73 65 64   tables are used
740a0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73   to hold the res
740b0 75 6c 74 73 20 6f 66 20 61 0a 2a 2a 20 73 75 62  ults of a.** sub
740c0 2d 71 75 65 72 79 20 74 68 61 74 20 61 70 70 65  -query that appe
740d0 61 72 73 20 69 6e 73 74 65 61 64 20 6f 66 20 61  ars instead of a
740e0 20 72 65 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65   real table name
740f0 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
74100 75 73 65 20 0a 2a 2a 20 6f 66 20 61 20 53 45 4c  use .** of a SEL
74110 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ECT statement..*
74120 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 20 7b  /.struct Table {
74130 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
74140 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
74150 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  of the table or 
74160 76 69 65 77 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e  view */.  Column
74170 20 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f   *aCol;        /
74180 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  * Information ab
74190 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  out each column 
741a0 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  */.  Index *pInd
741b0 65 78 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 73  ex;       /* Lis
741c0 74 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65 73  t of SQL indexes
741d0 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20   on this table. 
741e0 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
741f0 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 4e 55 4c  lect;     /* NUL
74200 4c 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 50  L for tables.  P
74210 6f 69 6e 74 73 20 74 6f 20 64 65 66 69 6e 69 74  oints to definit
74220 69 6f 6e 20 69 66 20 61 20 76 69 65 77 2e 20 2a  ion if a view. *
74230 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b  /.  FKey *pFKey;
74240 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b           /* Link
74250 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 66  ed list of all f
74260 6f 72 65 69 67 6e 20 6b 65 79 73 20 69 6e 20 74  oreign keys in t
74270 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63  his table */.  c
74280 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20  har *zColAff;   
74290 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64 65      /* String de
742a0 66 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69 6e  fining the affin
742b0 69 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c 75  ity of each colu
742c0 6d 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  mn */.#ifndef SQ
742d0 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a  LITE_OMIT_CHECK.
742e0 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 65    ExprList *pChe
742f0 63 6b 3b 20 20 20 20 2f 2a 20 41 6c 6c 20 43 48  ck;    /* All CH
74300 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  ECK constraints 
74310 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 74 52 6f 77  */.#endif.  tRow
74320 63 6e 74 20 6e 52 6f 77 45 73 74 3b 20 20 20 20  cnt nRowEst;    
74330 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 72 6f   /* Estimated ro
74340 77 73 20 69 6e 20 74 61 62 6c 65 20 2d 20 66 72  ws in table - fr
74350 6f 6d 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  om sqlite_stat1 
74360 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 74  table */.  int t
74370 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  num;            
74380 2f 2a 20 52 6f 6f 74 20 42 54 72 65 65 20 6e 6f  /* Root BTree no
74390 64 65 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c  de for this tabl
743a0 65 20 28 73 65 65 20 6e 6f 74 65 20 61 62 6f 76  e (see note abov
743b0 65 29 20 2a 2f 0a 20 20 69 31 36 20 69 50 4b 65  e) */.  i16 iPKe
743c0 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y;           /* 
743d0 49 66 20 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c  If not negative,
743e0 20 75 73 65 20 61 43 6f 6c 5b 69 50 4b 65 79 5d   use aCol[iPKey]
743f0 20 61 73 20 74 68 65 20 70 72 69 6d 61 72 79 20   as the primary 
74400 6b 65 79 20 2a 2f 0a 20 20 69 31 36 20 6e 43 6f  key */.  i16 nCo
74410 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  l;            /*
74420 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
74430 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65  ns in this table
74440 20 2a 2f 0a 20 20 75 31 36 20 6e 52 65 66 3b 20   */.  u16 nRef; 
74450 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
74460 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73  mber of pointers
74470 20 74 6f 20 74 68 69 73 20 54 61 62 6c 65 20 2a   to this Table *
74480 2f 0a 20 20 75 38 20 74 61 62 46 6c 61 67 73 3b  /.  u8 tabFlags;
74490 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
744a0 20 6f 66 20 54 46 5f 2a 20 76 61 6c 75 65 73 20   of TF_* values 
744b0 2a 2f 0a 20 20 75 38 20 6b 65 79 43 6f 6e 66 3b  */.  u8 keyConf;
744c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61            /* Wha
744d0 74 20 74 6f 20 64 6f 20 69 6e 20 63 61 73 65 20  t to do in case 
744e0 6f 66 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f  of uniqueness co
744f0 6e 66 6c 69 63 74 20 6f 6e 20 69 50 4b 65 79 20  nflict on iPKey 
74500 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
74510 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c  E_OMIT_ALTERTABL
74520 45 0a 20 20 69 6e 74 20 61 64 64 43 6f 6c 4f 66  E.  int addColOf
74530 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73  fset;    /* Offs
74540 65 74 20 69 6e 20 43 52 45 41 54 45 20 54 41 42  et in CREATE TAB
74550 4c 45 20 73 74 6d 74 20 74 6f 20 61 64 64 20 61  LE stmt to add a
74560 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23   new column */.#
74570 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
74580 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
74590 4c 54 41 42 4c 45 0a 20 20 69 6e 74 20 6e 4d 6f  LTABLE.  int nMo
745a0 64 75 6c 65 41 72 67 3b 20 20 20 20 20 20 2f 2a  duleArg;      /*
745b0 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   Number of argum
745c0 65 6e 74 73 20 74 6f 20 74 68 65 20 6d 6f 64 75  ents to the modu
745d0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61  le */.  char **a
745e0 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 20 2f 2a 20  zModuleArg;  /* 
745f0 54 65 78 74 20 6f 66 20 61 6c 6c 20 6d 6f 64 75  Text of all modu
74600 6c 65 20 61 72 67 73 2e 20 5b 30 5d 20 69 73 20  le args. [0] is 
74610 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20  module name */. 
74620 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65   VTable *pVTable
74630 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  ;     /* List of
74640 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 2e   VTable objects.
74650 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 54 72 69   */.#endif.  Tri
74660 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20  gger *pTrigger; 
74670 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69    /* List of tri
74680 67 67 65 72 73 20 73 74 6f 72 65 64 20 69 6e 20  ggers stored in 
74690 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 53 63 68  pSchema */.  Sch
746a0 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20  ema *pSchema;   
746b0 20 20 2f 2a 20 53 63 68 65 6d 61 20 74 68 61 74    /* Schema that
746c0 20 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 74   contains this t
746d0 61 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20  able */.  Table 
746e0 2a 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 20 20 2f  *pNextZombie;  /
746f0 2a 20 4e 65 78 74 20 6f 6e 20 74 68 65 20 50 61  * Next on the Pa
74700 72 73 65 2e 70 5a 6f 6d 62 69 65 54 61 62 20 6c  rse.pZombieTab l
74710 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ist */.};../*.**
74720 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20   Allowed values 
74730 66 6f 72 20 54 61 62 65 2e 74 61 62 46 6c 61 67  for Tabe.tabFlag
74740 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46  s..*/.#define TF
74750 5f 52 65 61 64 6f 6e 6c 79 20 20 20 20 20 20 20  _Readonly       
74760 20 30 78 30 31 20 20 20 20 2f 2a 20 52 65 61 64   0x01    /* Read
74770 2d 6f 6e 6c 79 20 73 79 73 74 65 6d 20 74 61 62  -only system tab
74780 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46  le */.#define TF
74790 5f 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20  _Ephemeral      
747a0 20 30 78 30 32 20 20 20 20 2f 2a 20 41 6e 20 65   0x02    /* An e
747b0 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 2a  phemeral table *
747c0 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 48 61 73  /.#define TF_Has
747d0 50 72 69 6d 61 72 79 4b 65 79 20 20 20 30 78 30  PrimaryKey   0x0
747e0 34 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61  4    /* Table ha
747f0 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20  s a primary key 
74800 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 41 75  */.#define TF_Au
74810 74 6f 69 6e 63 72 65 6d 65 6e 74 20 20 20 30 78  toincrement   0x
74820 30 38 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72  08    /* Integer
74830 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20   primary key is 
74840 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 2a 2f  autoincrement */
74850 0a 23 64 65 66 69 6e 65 20 54 46 5f 56 69 72 74  .#define TF_Virt
74860 75 61 6c 20 20 20 20 20 20 20 20 20 30 78 31 30  ual         0x10
74870 20 20 20 20 2f 2a 20 49 73 20 61 20 76 69 72 74      /* Is a virt
74880 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 0a 0a 2f  ual table */.../
74890 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73 65 65  *.** Test to see
748a0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
748b0 61 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72  a table is a vir
748c0 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69  tual table.  Thi
748d0 73 20 69 73 0a 2a 2a 20 64 6f 6e 65 20 61 73 20  s is.** done as 
748e0 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20  a macro so that 
748f0 69 74 20 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d  it will be optim
74900 69 7a 65 64 20 6f 75 74 20 77 68 65 6e 20 76 69  ized out when vi
74910 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 73  rtual.** table s
74920 75 70 70 6f 72 74 20 69 73 20 6f 6d 69 74 74 65  upport is omitte
74930 64 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64  d from the build
74940 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
74950 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
74960 54 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20  TABLE.#  define 
74970 49 73 56 69 72 74 75 61 6c 28 58 29 20 20 20 20  IsVirtual(X)    
74980 20 20 28 28 28 58 29 2d 3e 74 61 62 46 6c 61 67    (((X)->tabFlag
74990 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21  s & TF_Virtual)!
749a0 3d 30 29 0a 23 20 20 64 65 66 69 6e 65 20 49 73  =0).#  define Is
749b0 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20  HiddenColumn(X) 
749c0 28 28 28 58 29 2d 3e 63 6f 6c 46 6c 61 67 73 20  (((X)->colFlags 
749d0 26 20 43 4f 4c 46 4c 41 47 5f 48 49 44 44 45 4e  & COLFLAG_HIDDEN
749e0 29 21 3d 30 29 0a 23 65 6c 73 65 0a 23 20 20 64  )!=0).#else.#  d
749f0 65 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c 28  efine IsVirtual(
74a00 58 29 20 20 20 20 20 20 30 0a 23 20 20 64 65 66  X)      0.#  def
74a10 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c 75  ine IsHiddenColu
74a20 6d 6e 28 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a  mn(X) 0.#endif..
74a30 2f 2a 0a 2a 2a 20 45 61 63 68 20 66 6f 72 65 69  /*.** Each forei
74a40 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
74a50 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  t is an instance
74a60 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
74a70 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  g structure..**.
74a80 2a 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b 65 79  ** A foreign key
74a90 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77   is associated w
74aa0 69 74 68 20 74 77 6f 20 74 61 62 6c 65 73 2e 20  ith two tables. 
74ab0 20 54 68 65 20 22 66 72 6f 6d 22 20 74 61 62 6c   The "from" tabl
74ac0 65 20 69 73 0a 2a 2a 20 74 68 65 20 74 61 62 6c  e is.** the tabl
74ad0 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
74ae0 74 68 65 20 52 45 46 45 52 45 4e 43 45 53 20 63  the REFERENCES c
74af0 6c 61 75 73 65 20 74 68 61 74 20 63 72 65 61 74  lause that creat
74b00 65 73 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a  es the foreign.*
74b10 2a 20 6b 65 79 2e 20 20 54 68 65 20 22 74 6f 22  * key.  The "to"
74b20 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 74 61   table is the ta
74b30 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61 6d 65  ble that is name
74b40 64 20 69 6e 20 74 68 65 20 52 45 46 45 52 45 4e  d in the REFEREN
74b50 43 45 53 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 43  CES clause..** C
74b60 6f 6e 73 69 64 65 72 20 74 68 69 73 20 65 78 61  onsider this exa
74b70 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  mple:.**.**     
74b80 43 52 45 41 54 45 20 54 41 42 4c 45 20 65 78 31  CREATE TABLE ex1
74b90 28 0a 2a 2a 20 20 20 20 20 20 20 61 20 49 4e 54  (.**       a INT
74ba0 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
74bb0 2c 0a 2a 2a 20 20 20 20 20 20 20 62 20 49 4e 54  ,.**       b INT
74bc0 45 47 45 52 20 43 4f 4e 53 54 52 41 49 4e 54 20  EGER CONSTRAINT 
74bd0 66 6b 31 20 52 45 46 45 52 45 4e 43 45 53 20 65  fk1 REFERENCES e
74be0 78 32 28 78 29 0a 2a 2a 20 20 20 20 20 29 3b 0a  x2(x).**     );.
74bf0 2a 2a 0a 2a 2a 20 46 6f 72 20 66 6f 72 65 69 67  **.** For foreig
74c00 6e 20 6b 65 79 20 22 66 6b 31 22 2c 20 74 68 65  n key "fk1", the
74c10 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 22   from-table is "
74c20 65 78 31 22 20 61 6e 64 20 74 68 65 20 74 6f 2d  ex1" and the to-
74c30 74 61 62 6c 65 20 69 73 20 22 65 78 32 22 2e 0a  table is "ex2"..
74c40 2a 2a 0a 2a 2a 20 45 61 63 68 20 52 45 46 45 52  **.** Each REFER
74c50 45 4e 43 45 53 20 63 6c 61 75 73 65 20 67 65 6e  ENCES clause gen
74c60 65 72 61 74 65 73 20 61 6e 20 69 6e 73 74 61 6e  erates an instan
74c70 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
74c80 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  ing structure.**
74c90 20 77 68 69 63 68 20 69 73 20 61 74 74 61 63 68   which is attach
74ca0 65 64 20 74 6f 20 74 68 65 20 66 72 6f 6d 2d 74  ed to the from-t
74cb0 61 62 6c 65 2e 20 20 54 68 65 20 74 6f 2d 74 61  able.  The to-ta
74cc0 62 6c 65 20 6e 65 65 64 20 6e 6f 74 20 65 78 69  ble need not exi
74cd0 73 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 66  st when.** the f
74ce0 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 63 72 65  rom-table is cre
74cf0 61 74 65 64 2e 20 20 54 68 65 20 65 78 69 73 74  ated.  The exist
74d00 65 6e 63 65 20 6f 66 20 74 68 65 20 74 6f 2d 74  ence of the to-t
74d10 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 68 65 63  able is not chec
74d20 6b 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46  ked..*/.struct F
74d30 4b 65 79 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70  Key {.  Table *p
74d40 46 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54 61 62  From;     /* Tab
74d50 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
74d60 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61  e REFERENCES cla
74d70 75 73 65 20 28 61 6b 61 3a 20 43 68 69 6c 64 29  use (aka: Child)
74d80 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78   */.  FKey *pNex
74d90 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20  tFrom;  /* Next 
74da0 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 70  foreign key in p
74db0 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a  From */.  char *
74dc0 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  zTo;        /* N
74dd0 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 61  ame of table tha
74de0 74 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 73  t the key points
74df0 20 74 6f 20 28 61 6b 61 3a 20 50 61 72 65 6e 74   to (aka: Parent
74e00 29 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65  ) */.  FKey *pNe
74e10 78 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 74  xtTo;    /* Next
74e20 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20   foreign key on 
74e30 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f 20  table named zTo 
74e40 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 50 72 65 76  */.  FKey *pPrev
74e50 54 6f 3b 20 20 20 20 2f 2a 20 50 72 65 76 69 6f  To;    /* Previo
74e60 75 73 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f  us foreign key o
74e70 6e 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54  n table named zT
74e80 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  o */.  int nCol;
74e90 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
74ea0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
74eb0 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 2f   this key */.  /
74ec0 2a 20 45 56 3a 20 52 2d 33 30 33 32 33 2d 32 31  * EV: R-30323-21
74ed0 39 31 37 20 2a 2f 0a 20 20 75 38 20 69 73 44 65  917 */.  u8 isDe
74ee0 66 65 72 72 65 64 3b 20 20 20 20 2f 2a 20 54 72  ferred;    /* Tr
74ef0 75 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e 74  ue if constraint
74f00 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64 65 66   checking is def
74f10 65 72 72 65 64 20 74 69 6c 6c 20 43 4f 4d 4d 49  erred till COMMI
74f20 54 20 2a 2f 0a 20 20 75 38 20 61 41 63 74 69 6f  T */.  u8 aActio
74f30 6e 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 2f  n[2];          /
74f40 2a 20 4f 4e 20 44 45 4c 45 54 45 20 61 6e 64 20  * ON DELETE and 
74f50 4f 4e 20 55 50 44 41 54 45 20 61 63 74 69 6f 6e  ON UPDATE action
74f60 73 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 20  s, respectively 
74f70 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 61 70  */.  Trigger *ap
74f80 54 72 69 67 67 65 72 5b 32 5d 3b 20 20 2f 2a 20  Trigger[2];  /* 
74f90 54 72 69 67 67 65 72 73 20 66 6f 72 20 61 41 63  Triggers for aAc
74fa0 74 69 6f 6e 5b 5d 20 61 63 74 69 6f 6e 73 20 2a  tion[] actions *
74fb0 2f 0a 20 20 73 74 72 75 63 74 20 73 43 6f 6c 4d  /.  struct sColM
74fc0 61 70 20 7b 20 20 2f 2a 20 4d 61 70 70 69 6e 67  ap {  /* Mapping
74fd0 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70   of columns in p
74fe0 46 72 6f 6d 20 74 6f 20 63 6f 6c 75 6d 6e 73 20  From to columns 
74ff0 69 6e 20 7a 54 6f 20 2a 2f 0a 20 20 20 20 69 6e  in zTo */.    in
75000 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 20  t iFrom;        
75010 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c   /* Index of col
75020 75 6d 6e 20 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a  umn in pFrom */.
75030 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20      char *zCol; 
75040 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
75050 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 7a 54 6f 2e  f column in zTo.
75060 20 20 49 66 20 30 20 75 73 65 20 50 52 49 4d 41    If 0 use PRIMA
75070 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 20 61 43  RY KEY */.  } aC
75080 6f 6c 5b 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a  ol[1];        /*
75090 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65   One entry for e
750a0 61 63 68 20 6f 66 20 6e 43 6f 6c 20 63 6f 6c 75  ach of nCol colu
750b0 6d 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  mn s */.};../*.*
750c0 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f 72 74  * SQLite support
750d0 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65 6e 74  s many different
750e0 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 65   ways to resolve
750f0 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a   a constraint.**
75100 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43   error.  ROLLBAC
75110 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61  K processing mea
75120 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73 74 72  ns that a constr
75130 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a  aint violation.*
75140 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f 70 65  * causes the ope
75150 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73  ration in proces
75160 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f  s to fail and fo
75170 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72  r the current tr
75180 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20  ansaction.** to 
75190 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  be rolled back. 
751a0 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e   ABORT processin
751b0 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72  g means the oper
751c0 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73  ation in process
751d0 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 6e  .** fails and an
751e0 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20  y prior changes 
751f0 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70  from that one op
75200 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61 63 6b  eration are back
75210 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74  ed out,.** but t
75220 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
75230 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63  s not rolled bac
75240 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 73 73  k.  FAIL process
75250 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a  ing means that.*
75260 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  * the operation 
75270 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74 6f 70  in progress stop
75280 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e  s and returns an
75290 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 75   error code.  Bu
752a0 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67  t prior.** chang
752b0 65 73 20 64 75 65 20 74 6f 20 74 68 65 20 73 61  es due to the sa
752c0 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65  me operation are
752d0 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 20   not backed out 
752e0 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a  and no rollback.
752f0 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f  ** occurs.  IGNO
75300 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  RE means that th
75310 65 20 70 61 72 74 69 63 75 6c 61 72 20 72 6f 77  e particular row
75320 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65   that caused the
75330 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65   constraint.** e
75340 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 65  rror is not inse
75350 72 74 65 64 20 6f 72 20 75 70 64 61 74 65 64 2e  rted or updated.
75360 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e    Processing con
75370 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72  tinues and no er
75380 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  ror.** is return
75390 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 61  ed.  REPLACE mea
753a0 6e 73 20 74 68 61 74 20 70 72 65 65 78 69 73 74  ns that preexist
753b0 69 6e 67 20 64 61 74 61 62 61 73 65 20 72 6f 77  ing database row
753c0 73 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a  s that caused.**
753d0 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72   a UNIQUE constr
753e0 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61  aint violation a
753f0 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 68  re removed so th
75400 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73 65 72  at the new inser
75410 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 63  t or.** update c
75420 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 72 6f  an proceed.  Pro
75430 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65  cessing continue
75440 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69  s and no error i
75450 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a  s reported..**.*
75460 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45 54 4e  * RESTRICT, SETN
75470 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 45  ULL, and CASCADE
75480 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f   actions apply o
75490 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b  nly to foreign k
754a0 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54  eys..** RESTRICT
754b0 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
754c0 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 49  ABORT for IMMEDI
754d0 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 73  ATE foreign keys
754e0 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65   and the.** same
754f0 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72   as ROLLBACK for
75500 20 44 45 46 45 52 52 45 44 20 6b 65 79 73 2e 20   DEFERRED keys. 
75510 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74   SETNULL means t
75520 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e 0a  hat the foreign.
75530 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20 74 6f  ** key is set to
75540 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 20   NULL.  CASCADE 
75550 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44 45 4c  means that a DEL
75560 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 6f 66  ETE or UPDATE of
75570 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63   the.** referenc
75580 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20  ed table row is 
75590 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74 6f 20  propagated into 
755a0 74 68 65 20 72 6f 77 20 74 68 61 74 20 68 6f 6c  the row that hol
755b0 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67  ds the.** foreig
755c0 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68  n key..** .** Th
755d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62  e following symb
755e0 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72 65 20  olic values are 
755f0 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77  used to record w
75600 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20  hich type.** of 
75610 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a  action to take..
75620 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f  */.#define OE_No
75630 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20 54 68  ne     0   /* Th
75640 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72  ere is no constr
75650 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f  aint to check */
75660 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c  .#define OE_Roll
75670 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c  back 1   /* Fail
75680 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61   the operation a
75690 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  nd rollback the 
756a0 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23  transaction */.#
756b0 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20  define OE_Abort 
756c0 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f     2   /* Back o
756d0 75 74 20 63 68 61 6e 67 65 73 20 62 75 74 20 64  ut changes but d
756e0 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72  o no rollback tr
756f0 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65  ansaction */.#de
75700 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 20  fine OE_Fail    
75710 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 65   3   /* Stop the
75720 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 20 6c   operation but l
75730 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 63  eave all prior c
75740 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e  hanges */.#defin
75750 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 20  e OE_Ignore   4 
75760 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20    /* Ignore the 
75770 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f  error. Do not do
75780 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55   the INSERT or U
75790 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65  PDATE */.#define
757a0 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 20 20   OE_Replace  5  
757b0 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 73 74   /* Delete exist
757c0 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 65 6e  ing record, then
757d0 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50   do INSERT or UP
757e0 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65  DATE */..#define
757f0 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36 20 20   OE_Restrict 6  
75800 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72   /* OE_Abort for
75810 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52   IMMEDIATE, OE_R
75820 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 45  ollback for DEFE
75830 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20  RRED */.#define 
75840 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 20  OE_SetNull  7   
75850 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69  /* Set the forei
75860 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20  gn key value to 
75870 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NULL */.#define 
75880 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20 20 20  OE_SetDflt  8   
75890 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69  /* Set the forei
758a0 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20  gn key value to 
758b0 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23  its default */.#
758c0 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 64  define OE_Cascad
758d0 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63 61 64  e  9   /* Cascad
758e0 65 20 74 68 65 20 63 68 61 6e 67 65 73 20 2a 2f  e the changes */
758f0 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 66  ..#define OE_Def
75900 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f 20  ault  99  /* Do 
75910 77 68 61 74 65 76 65 72 20 74 68 65 20 64 65 66  whatever the def
75920 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 2a  ault action is *
75930 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  /.../*.** An ins
75940 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
75950 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
75960 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68   is passed as th
75970 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d  e first.** argum
75980 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 56 64  ent to sqlite3Vd
75990 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e 64  beKeyCompare and
759a0 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 74   is used to cont
759b0 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70  rol the .** comp
759c0 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 77  arison of the tw
759d0 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f  o index keys..*/
759e0 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20  .struct KeyInfo 
759f0 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
75a00 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
75a10 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
75a20 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20  on */.  u8 enc; 
75a30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
75a40 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f  ext encoding - o
75a50 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45  ne of the SQLITE
75a60 5f 55 54 46 2a 20 76 61 6c 75 65 73 20 2a 2f 0a  _UTF* values */.
75a70 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20    u16 nField;   
75a80 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
75a90 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43  of entries in aC
75aa0 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61  oll[] */.  u8 *a
75ab0 53 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f  SortOrder;     /
75ac0 2a 20 53 6f 72 74 20 6f 72 64 65 72 20 66 6f 72  * Sort order for
75ad0 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 20 20 4d   each column.  M
75ae0 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20  ay be NULL */.  
75af0 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c 5b 31  CollSeq *aColl[1
75b00 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67  ];  /* Collating
75b10 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 61   sequence for ea
75b20 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 6b  ch term of the k
75b30 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ey */.};../*.** 
75b40 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
75b50 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
75b60 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66  ucture holds inf
75b70 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61  ormation about a
75b80 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 6e 64 65 78  .** single index
75b90 20 72 65 63 6f 72 64 20 74 68 61 74 20 68 61 73   record that has
75ba0 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 61   already been pa
75bb0 72 73 65 64 20 6f 75 74 20 69 6e 74 6f 20 69 6e  rsed out into in
75bc0 64 69 76 69 64 75 61 6c 0a 2a 2a 20 76 61 6c 75  dividual.** valu
75bd0 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 63 6f  es..**.** A reco
75be0 72 64 20 69 73 20 61 6e 20 6f 62 6a 65 63 74 20  rd is an object 
75bf0 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 6f 6e  that contains on
75c00 65 20 6f 72 20 6d 6f 72 65 20 66 69 65 6c 64 73  e or more fields
75c10 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 52 65 63   of data..** Rec
75c20 6f 72 64 73 20 61 72 65 20 75 73 65 64 20 74 6f  ords are used to
75c30 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6e 74 65   store the conte
75c40 6e 74 20 6f 66 20 61 20 74 61 62 6c 65 20 72 6f  nt of a table ro
75c50 77 20 61 6e 64 20 74 6f 20 73 74 6f 72 65 0a 2a  w and to store.*
75c60 2a 20 74 68 65 20 6b 65 79 20 6f 66 20 61 6e 20  * the key of an 
75c70 69 6e 64 65 78 2e 20 20 41 20 62 6c 6f 62 20 65  index.  A blob e
75c80 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72 65 63  ncoding of a rec
75c90 6f 72 64 20 69 73 20 63 72 65 61 74 65 64 20 62  ord is created b
75ca0 79 0a 2a 2a 20 74 68 65 20 4f 50 5f 4d 61 6b 65  y.** the OP_Make
75cb0 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66  Record opcode of
75cc0 20 74 68 65 20 56 44 42 45 20 61 6e 64 20 69 73   the VDBE and is
75cd0 20 64 69 73 61 73 73 65 6d 62 6c 65 64 20 62 79   disassembled by
75ce0 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 6f 6c 75 6d   the.** OP_Colum
75cf0 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  n opcode..**.** 
75d00 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68  This structure h
75d10 6f 6c 64 73 20 61 20 72 65 63 6f 72 64 20 74 68  olds a record th
75d20 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  at has already b
75d30 65 65 6e 20 64 69 73 61 73 73 65 6d 62 6c 65 64  een disassembled
75d40 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 20 63 6f 6e  .** into its con
75d50 73 74 69 74 75 65 6e 74 20 66 69 65 6c 64 73 2e  stituent fields.
75d60 0a 2a 2f 0a 73 74 72 75 63 74 20 55 6e 70 61 63  .*/.struct Unpac
75d70 6b 65 64 52 65 63 6f 72 64 20 7b 0a 20 20 4b 65  kedRecord {.  Ke
75d80 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b  yInfo *pKeyInfo;
75d90 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 61    /* Collation a
75da0 6e 64 20 73 6f 72 74 2d 6f 72 64 65 72 20 69 6e  nd sort-order in
75db0 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 75  formation */.  u
75dc0 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  16 nField;      
75dd0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
75de0 65 6e 74 72 69 65 73 20 69 6e 20 61 70 4d 65 6d  entries in apMem
75df0 5b 5d 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67 73  [] */.  u8 flags
75e00 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ;           /* B
75e10 6f 6f 6c 65 61 6e 20 73 65 74 74 69 6e 67 73 2e  oolean settings.
75e20 20 20 55 4e 50 41 43 4b 45 44 5f 2e 2e 2e 20 62    UNPACKED_... b
75e30 65 6c 6f 77 20 2a 2f 0a 20 20 69 36 34 20 72 6f  elow */.  i64 ro
75e40 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  wid;          /*
75e50 20 55 73 65 64 20 62 79 20 55 4e 50 41 43 4b 45   Used by UNPACKE
75e60 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 20  D_PREFIX_SEARCH 
75e70 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20  */.  Mem *aMem; 
75e80 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
75e90 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  es */.};../*.** 
75ea0 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f  Allowed values o
75eb0 66 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  f UnpackedRecord
75ec0 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e  .flags.*/.#defin
75ed0 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b  e UNPACKED_INCRK
75ee0 45 59 20 20 20 20 20 20 20 30 78 30 31 20 20 2f  EY       0x01  /
75ef0 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79 20  * Make this key 
75f00 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65  an epsilon large
75f10 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50  r */.#define UNP
75f20 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54  ACKED_PREFIX_MAT
75f30 43 48 20 20 30 78 30 32 20 20 2f 2a 20 41 20 70  CH  0x02  /* A p
75f40 72 65 66 69 78 20 6d 61 74 63 68 20 69 73 20 63  refix match is c
75f50 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a  onsidered OK */.
75f60 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44  #define UNPACKED
75f70 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 30  _PREFIX_SEARCH 0
75f80 78 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 66  x04  /* Ignore f
75f90 69 6e 61 6c 20 28 72 6f 77 69 64 29 20 66 69 65  inal (rowid) fie
75fa0 6c 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  ld */../*.** Eac
75fb0 68 20 53 51 4c 20 69 6e 64 65 78 20 69 73 20 72  h SQL index is r
75fc0 65 70 72 65 73 65 6e 74 65 64 20 69 6e 20 6d 65  epresented in me
75fd0 6d 6f 72 79 20 62 79 20 61 6e 0a 2a 2a 20 69 6e  mory by an.** in
75fe0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
75ff0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
76000 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c  e..**.** The col
76010 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c  umns of the tabl
76020 65 20 74 68 61 74 20 61 72 65 20 74 6f 20 62 65  e that are to be
76030 20 69 6e 64 65 78 65 64 20 61 72 65 20 64 65 73   indexed are des
76040 63 72 69 62 65 64 0a 2a 2a 20 62 79 20 74 68 65  cribed.** by the
76050 20 61 69 43 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c   aiColumn[] fiel
76060 64 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  d of this struct
76070 75 72 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  ure.  For exampl
76080 65 2c 20 73 75 70 70 6f 73 65 0a 2a 2a 20 77 65  e, suppose.** we
76090 20 68 61 76 65 20 74 68 65 20 66 6f 6c 6c 6f 77   have the follow
760a0 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 69 6e  ing table and in
760b0 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43  dex:.**.**     C
760c0 52 45 41 54 45 20 54 41 42 4c 45 20 45 78 31 28  REATE TABLE Ex1(
760d0 63 31 20 69 6e 74 2c 20 63 32 20 69 6e 74 2c 20  c1 int, c2 int, 
760e0 63 33 20 74 65 78 74 29 3b 0a 2a 2a 20 20 20 20  c3 text);.**    
760f0 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 45 78   CREATE INDEX Ex
76100 32 20 4f 4e 20 45 78 31 28 63 33 2c 63 31 29 3b  2 ON Ex1(c3,c1);
76110 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 54 61  .**.** In the Ta
76120 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 64 65  ble structure de
76130 73 63 72 69 62 69 6e 67 20 45 78 31 2c 20 6e 43  scribing Ex1, nC
76140 6f 6c 3d 3d 33 20 62 65 63 61 75 73 65 20 74 68  ol==3 because th
76150 65 72 65 20 61 72 65 0a 2a 2a 20 74 68 72 65 65  ere are.** three
76160 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
76170 74 61 62 6c 65 2e 20 20 49 6e 20 74 68 65 20 49  table.  In the I
76180 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 64  ndex structure d
76190 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20 45 78 32  escribing.** Ex2
761a0 2c 20 6e 43 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e  , nColumn==2 sin
761b0 63 65 20 32 20 6f 66 20 74 68 65 20 33 20 63 6f  ce 2 of the 3 co
761c0 6c 75 6d 6e 73 20 6f 66 20 45 78 31 20 61 72 65  lumns of Ex1 are
761d0 20 69 6e 64 65 78 65 64 2e 0a 2a 2a 20 54 68 65   indexed..** The
761e0 20 76 61 6c 75 65 20 6f 66 20 61 69 43 6f 6c 75   value of aiColu
761f0 6d 6e 20 69 73 20 7b 32 2c 20 30 7d 2e 20 20 61  mn is {2, 0}.  a
76200 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65  iColumn[0]==2 be
76210 63 61 75 73 65 20 74 68 65 20 0a 2a 2a 20 66 69  cause the .** fi
76220 72 73 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65  rst column to be
76230 20 69 6e 64 65 78 65 64 20 28 63 33 29 20 68 61   indexed (c3) ha
76240 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 32 20  s an index of 2 
76250 69 6e 20 45 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a  in Ex1.aCol[]..*
76260 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 63 6f 6c  * The second col
76270 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 65 78 65  umn to be indexe
76280 64 20 28 63 31 29 20 68 61 73 20 61 6e 20 69 6e  d (c1) has an in
76290 64 65 78 20 6f 66 20 30 20 69 6e 0a 2a 2a 20 45  dex of 0 in.** E
762a0 78 31 2e 61 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65  x1.aCol[], hence
762b0 20 45 78 32 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d   Ex2.aiColumn[1]
762c0 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49  ==0..**.** The I
762d0 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 66 69 65  ndex.onError fie
762e0 6c 64 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  ld determines wh
762f0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
76300 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 73   indexed columns
76310 0a 2a 2a 20 6d 75 73 74 20 62 65 20 75 6e 69 71  .** must be uniq
76320 75 65 20 61 6e 64 20 77 68 61 74 20 74 6f 20 64  ue and what to d
76330 6f 20 69 66 20 74 68 65 79 20 61 72 65 20 6e 6f  o if they are no
76340 74 2e 20 20 57 68 65 6e 20 49 6e 64 65 78 2e 6f  t.  When Index.o
76350 6e 45 72 72 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a  nError=OE_None,.
76360 2a 2a 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73  ** it means this
76370 20 69 73 20 6e 6f 74 20 61 20 75 6e 69 71 75 65   is not a unique
76380 20 69 6e 64 65 78 2e 20 20 4f 74 68 65 72 77 69   index.  Otherwi
76390 73 65 20 69 74 20 69 73 20 61 20 75 6e 69 71 75  se it is a uniqu
763a0 65 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 74  e index.** and t
763b0 68 65 20 76 61 6c 75 65 20 6f 66 20 49 6e 64 65  he value of Inde
763c0 78 2e 6f 6e 45 72 72 6f 72 20 69 6e 64 69 63 61  x.onError indica
763d0 74 65 20 74 68 65 20 77 68 69 63 68 20 63 6f 6e  te the which con
763e0 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e  flict resolution
763f0 20 0a 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 20 74   .** algorithm t
76400 6f 20 65 6d 70 6c 6f 79 20 77 68 65 6e 65 76 65  o employ wheneve
76410 72 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20  r an attempt is 
76420 6d 61 64 65 20 74 6f 20 69 6e 73 65 72 74 20 61  made to insert a
76430 20 6e 6f 6e 2d 75 6e 69 71 75 65 0a 2a 2a 20 65   non-unique.** e
76440 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63  lement..*/.struc
76450 74 20 49 6e 64 65 78 20 7b 0a 20 20 63 68 61 72  t Index {.  char
76460 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20   *zName;     /* 
76470 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 69 6e 64  Name of this ind
76480 65 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43  ex */.  int *aiC
76490 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 69 63  olumn;   /* Whic
764a0 68 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73  h columns are us
764b0 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78  ed by this index
764c0 2e 20 20 31 73 74 20 69 73 20 30 20 2a 2f 0a 20  .  1st is 0 */. 
764d0 20 74 52 6f 77 63 6e 74 20 2a 61 69 52 6f 77 45   tRowcnt *aiRowE
764e0 73 74 3b 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66  st; /* Result of
764f0 20 41 4e 41 4c 59 5a 45 3a 20 45 73 74 2e 20 72   ANALYZE: Est. r
76500 6f 77 73 20 73 65 6c 65 63 74 65 64 20 62 79 20  ows selected by 
76510 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  each column */. 
76520 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20   Table *pTable; 
76530 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 61 62    /* The SQL tab
76540 6c 65 20 62 65 69 6e 67 20 69 6e 64 65 78 65 64  le being indexed
76550 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c   */.  char *zCol
76560 41 66 66 3b 20 20 20 2f 2a 20 53 74 72 69 6e 67  Aff;   /* String
76570 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 61 66   defining the af
76580 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68 20 63  finity of each c
76590 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78  olumn */.  Index
765a0 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 54   *pNext;    /* T
765b0 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20 61 73  he next index as
765c0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
765d0 65 20 73 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a  e same table */.
765e0 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
765f0 61 3b 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e  a; /* Schema con
76600 74 61 69 6e 69 6e 67 20 74 68 69 73 20 69 6e 64  taining this ind
76610 65 78 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72  ex */.  u8 *aSor
76620 74 4f 72 64 65 72 3b 20 20 2f 2a 20 41 72 72 61  tOrder;  /* Arra
76630 79 20 6f 66 20 73 69 7a 65 20 49 6e 64 65 78 2e  y of size Index.
76640 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75 65 3d 3d 44  nColumn. True==D
76650 45 53 43 2c 20 46 61 6c 73 65 3d 3d 41 53 43 20  ESC, False==ASC 
76660 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f  */.  char **azCo
76670 6c 6c 3b 20 20 20 2f 2a 20 41 72 72 61 79 20 6f  ll;   /* Array o
76680 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  f collation sequ
76690 65 6e 63 65 20 6e 61 6d 65 73 20 66 6f 72 20 69  ence names for i
766a0 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  ndex */.  int nC
766b0 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 4e 75  olumn;     /* Nu
766c0 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
766d0 69 6e 20 74 68 65 20 74 61 62 6c 65 20 75 73 65  in the table use
766e0 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 20  d by this index 
766f0 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20  */.  int tnum;  
76700 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f        /* Page co
76710 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 20 6f 66  ntaining root of
76720 20 74 68 69 73 20 69 6e 64 65 78 20 69 6e 20 64   this index in d
76730 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
76740 20 20 75 38 20 6f 6e 45 72 72 6f 72 3b 20 20 20    u8 onError;   
76750 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c 20     /* OE_Abort, 
76760 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f 52 65  OE_Ignore, OE_Re
76770 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f 6e  place, or OE_Non
76780 65 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f 49 6e  e */.  u8 autoIn
76790 64 65 78 3b 20 20 20 20 2f 2a 20 54 72 75 65 20  dex;    /* True 
767a0 69 66 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  if is automatica
767b0 6c 6c 79 20 63 72 65 61 74 65 64 20 28 65 78 3a  lly created (ex:
767c0 20 62 79 20 55 4e 49 51 55 45 29 20 2a 2f 0a 20   by UNIQUE) */. 
767d0 20 75 38 20 62 55 6e 6f 72 64 65 72 65 64 3b 20   u8 bUnordered; 
767e0 20 20 2f 2a 20 55 73 65 20 74 68 69 73 20 69 6e    /* Use this in
767f0 64 65 78 20 66 6f 72 20 3d 3d 20 6f 72 20 49 4e  dex for == or IN
76800 20 71 75 65 72 69 65 73 20 6f 6e 6c 79 20 2a 2f   queries only */
76810 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
76820 4e 41 42 4c 45 5f 53 54 41 54 33 0a 20 20 69 6e  NABLE_STAT3.  in
76830 74 20 6e 53 61 6d 70 6c 65 3b 20 20 20 20 20 20  t nSample;      
76840 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
76850 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20   of elements in 
76860 61 53 61 6d 70 6c 65 5b 5d 20 2a 2f 0a 20 20 74  aSample[] */.  t
76870 52 6f 77 63 6e 74 20 61 76 67 45 71 3b 20 20 20  Rowcnt avgEq;   
76880 20 20 20 20 20 20 20 20 2f 2a 20 41 76 65 72 61          /* Avera
76890 67 65 20 6e 45 71 20 76 61 6c 75 65 20 66 6f 72  ge nEq value for
768a0 20 6b 65 79 20 76 61 6c 75 65 73 20 6e 6f 74 20   key values not 
768b0 69 6e 20 61 53 61 6d 70 6c 65 20 2a 2f 0a 20 20  in aSample */.  
768c0 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a 61 53 61  IndexSample *aSa
768d0 6d 70 6c 65 3b 20 20 20 20 2f 2a 20 53 61 6d 70  mple;    /* Samp
768e0 6c 65 73 20 6f 66 20 74 68 65 20 6c 65 66 74 2d  les of the left-
768f0 6d 6f 73 74 20 6b 65 79 20 2a 2f 0a 23 65 6e 64  most key */.#end
76900 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  if.};../*.** Eac
76910 68 20 73 61 6d 70 6c 65 20 73 74 6f 72 65 64 20  h sample stored 
76920 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  in the sqlite_st
76930 61 74 33 20 74 61 62 6c 65 20 69 73 20 72 65 70  at3 table is rep
76940 72 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f  resented in memo
76950 72 79 20 0a 2a 2a 20 75 73 69 6e 67 20 61 20 73  ry .** using a s
76960 74 72 75 63 74 75 72 65 20 6f 66 20 74 68 69 73  tructure of this
76970 20 74 79 70 65 2e 20 20 53 65 65 20 64 6f 63 75   type.  See docu
76980 6d 65 6e 74 61 74 69 6f 6e 20 61 74 20 74 68 65  mentation at the
76990 20 74 6f 70 20 6f 66 20 74 68 65 0a 2a 2a 20 61   top of the.** a
769a0 6e 61 6c 79 7a 65 2e 63 20 73 6f 75 72 63 65 20  nalyze.c source 
769b0 66 69 6c 65 20 66 6f 72 20 61 64 64 69 74 69 6f  file for additio
769c0 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
769d0 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64 65 78  .*/.struct Index
769e0 53 61 6d 70 6c 65 20 7b 0a 20 20 75 6e 69 6f 6e  Sample {.  union
769f0 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20   {.    char *z; 
76a00 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
76a10 69 66 20 65 54 79 70 65 20 69 73 20 53 51 4c 49  if eType is SQLI
76a20 54 45 5f 54 45 58 54 20 6f 72 20 53 51 4c 49 54  TE_TEXT or SQLIT
76a30 45 5f 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 64 6f  E_BLOB */.    do
76a40 75 62 6c 65 20 72 3b 20 20 20 20 20 20 20 2f 2a  uble r;       /*
76a50 20 56 61 6c 75 65 20 69 66 20 65 54 79 70 65 20   Value if eType 
76a60 69 73 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20  is SQLITE_FLOAT 
76a70 2a 2f 0a 20 20 20 20 69 36 34 20 69 3b 20 20 20  */.    i64 i;   
76a80 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
76a90 69 66 20 65 54 79 70 65 20 69 73 20 53 51 4c 49  if eType is SQLI
76aa0 54 45 5f 49 4e 54 45 47 45 52 20 2a 2f 0a 20 20  TE_INTEGER */.  
76ab0 7d 20 75 3b 0a 20 20 75 38 20 65 54 79 70 65 3b  } u;.  u8 eType;
76ac0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49           /* SQLI
76ad0 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f  TE_NULL, SQLITE_
76ae0 49 4e 54 45 47 45 52 20 2e 2e 2e 20 65 74 63 2e  INTEGER ... etc.
76af0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b   */.  int nByte;
76b00 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
76b10 69 6e 20 62 79 74 65 20 6f 66 20 74 65 78 74 20  in byte of text 
76b20 6f 72 20 62 6c 6f 62 2e 20 2a 2f 0a 20 20 74 52  or blob. */.  tR
76b30 6f 77 63 6e 74 20 6e 45 71 3b 20 20 20 20 20 20  owcnt nEq;      
76b40 2f 2a 20 45 73 74 2e 20 6e 75 6d 62 65 72 20 6f  /* Est. number o
76b50 66 20 72 6f 77 73 20 77 68 65 72 65 20 74 68 65  f rows where the
76b60 20 6b 65 79 20 65 71 75 61 6c 73 20 74 68 69 73   key equals this
76b70 20 73 61 6d 70 6c 65 20 2a 2f 0a 20 20 74 52 6f   sample */.  tRo
76b80 77 63 6e 74 20 6e 4c 74 3b 20 20 20 20 20 20 2f  wcnt nLt;      /
76b90 2a 20 45 73 74 2e 20 6e 75 6d 62 65 72 20 6f 66  * Est. number of
76ba0 20 72 6f 77 73 20 77 68 65 72 65 20 6b 65 79 20   rows where key 
76bb0 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 69  is less than thi
76bc0 73 20 73 61 6d 70 6c 65 20 2a 2f 0a 20 20 74 52  s sample */.  tR
76bd0 6f 77 63 6e 74 20 6e 44 4c 74 3b 20 20 20 20 20  owcnt nDLt;     
76be0 2f 2a 20 45 73 74 2e 20 6e 75 6d 62 65 72 20 6f  /* Est. number o
76bf0 66 20 64 69 73 74 69 6e 63 74 20 6b 65 79 73 20  f distinct keys 
76c00 6c 65 73 73 20 74 68 61 6e 20 74 68 69 73 20 73  less than this s
76c10 61 6d 70 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ample */.};../*.
76c20 2a 2a 20 45 61 63 68 20 74 6f 6b 65 6e 20 63 6f  ** Each token co
76c30 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74 68 65 20  ming out of the 
76c40 6c 65 78 65 72 20 69 73 20 61 6e 20 69 6e 73 74  lexer is an inst
76c50 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20  ance of.** this 
76c60 73 74 72 75 63 74 75 72 65 2e 20 20 54 6f 6b 65  structure.  Toke
76c70 6e 73 20 61 72 65 20 61 6c 73 6f 20 75 73 65 64  ns are also used
76c80 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65   as part of an e
76c90 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  xpression..**.**
76ca0 20 4e 6f 74 65 20 69 66 20 54 6f 6b 65 6e 2e 7a   Note if Token.z
76cb0 3d 3d 30 20 74 68 65 6e 20 54 6f 6b 65 6e 2e 64  ==0 then Token.d
76cc0 79 6e 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 61  yn and Token.n a
76cd0 72 65 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64  re undefined and
76ce0 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20  .** may contain 
76cf0 72 61 6e 64 6f 6d 20 76 61 6c 75 65 73 2e 20 20  random values.  
76d00 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 20  Do not make any 
76d10 61 73 73 75 6d 70 74 69 6f 6e 73 20 61 62 6f 75  assumptions abou
76d20 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a 20 61  t Token.dyn.** a
76d30 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 68 65 6e 20  nd Token.n when 
76d40 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f 0a 73  Token.z==0..*/.s
76d50 74 72 75 63 74 20 54 6f 6b 65 6e 20 7b 0a 20 20  truct Token {.  
76d60 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 20 20  const char *z;  
76d70 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68     /* Text of th
76d80 65 20 74 6f 6b 65 6e 2e 20 20 4e 6f 74 20 4e 55  e token.  Not NU
76d90 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64 21 20 2a  LL-terminated! *
76da0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
76db0 20 6e 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72   n;    /* Number
76dc0 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69   of characters i
76dd0 6e 20 74 68 69 73 20 74 6f 6b 65 6e 20 2a 2f 0a  n this token */.
76de0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
76df0 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
76e00 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
76e10 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65   information nee
76e20 64 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 0a  ded to generate.
76e30 2a 2a 20 63 6f 64 65 20 66 6f 72 20 61 20 53 45  ** code for a SE
76e40 4c 45 43 54 20 74 68 61 74 20 63 6f 6e 74 61 69  LECT that contai
76e50 6e 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  ns aggregate fun
76e60 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  ctions..**.** If
76e70 20 45 78 70 72 2e 6f 70 3d 3d 54 4b 5f 41 47 47   Expr.op==TK_AGG
76e80 5f 43 4f 4c 55 4d 4e 20 6f 72 20 54 4b 5f 41 47  _COLUMN or TK_AG
76e90 47 5f 46 55 4e 43 54 49 4f 4e 20 74 68 65 6e 20  G_FUNCTION then 
76ea0 45 78 70 72 2e 70 41 67 67 49 6e 66 6f 20 69 73  Expr.pAggInfo is
76eb0 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f   a.** pointer to
76ec0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e   this structure.
76ed0 20 20 54 68 65 20 45 78 70 72 2e 69 43 6f 6c 75    The Expr.iColu
76ee0 6d 6e 20 66 69 65 6c 64 20 69 73 20 74 68 65 20  mn field is the 
76ef0 69 6e 64 65 78 20 69 6e 0a 2a 2a 20 41 67 67 49  index in.** AggI
76f00 6e 66 6f 2e 61 43 6f 6c 5b 5d 20 6f 72 20 41 67  nfo.aCol[] or Ag
76f10 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 5d 20 6f 66  gInfo.aFunc[] of
76f20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65   information nee
76f30 64 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 0a  ded to generate.
76f40 2a 2a 20 63 6f 64 65 20 66 6f 72 20 74 68 61 74  ** code for that
76f50 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 67 67   node..**.** Agg
76f60 49 6e 66 6f 2e 70 47 72 6f 75 70 42 79 20 61 6e  Info.pGroupBy an
76f70 64 20 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 2e  d AggInfo.aFunc.
76f80 70 45 78 70 72 20 70 6f 69 6e 74 20 74 6f 20 66  pExpr point to f
76f90 69 65 6c 64 73 20 77 69 74 68 69 6e 20 74 68 65  ields within the
76fa0 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53 65 6c  .** original Sel
76fb0 65 63 74 20 73 74 72 75 63 74 75 72 65 20 74 68  ect structure th
76fc0 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65  at describes the
76fd0 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
76fe0 74 2e 20 20 54 68 65 73 65 0a 2a 2a 20 66 69 65  t.  These.** fie
76ff0 6c 64 73 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20  lds do not need 
77000 74 6f 20 62 65 20 66 72 65 65 64 20 77 68 65 6e  to be freed when
77010 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68   deallocating th
77020 65 20 41 67 67 49 6e 66 6f 20 73 74 72 75 63 74  e AggInfo struct
77030 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41  ure..*/.struct A
77040 67 67 49 6e 66 6f 20 7b 0a 20 20 75 38 20 64 69  ggInfo {.  u8 di
77050 72 65 63 74 4d 6f 64 65 3b 20 20 20 20 20 20 20  rectMode;       
77060 20 20 20 2f 2a 20 44 69 72 65 63 74 20 72 65 6e     /* Direct ren
77070 64 65 72 69 6e 67 20 6d 6f 64 65 20 6d 65 61 6e  dering mode mean
77080 73 20 74 61 6b 65 20 64 61 74 61 20 64 69 72 65  s take data dire
77090 63 74 6c 79 0a 20 20 20 20 20 20 20 20 20 20 20  ctly.           
770a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
770b0 2a 20 66 72 6f 6d 20 73 6f 75 72 63 65 20 74 61  * from source ta
770c0 62 6c 65 73 20 72 61 74 68 65 72 20 74 68 61 6e  bles rather than
770d0 20 66 72 6f 6d 20 61 63 63 75 6d 75 6c 61 74 6f   from accumulato
770e0 72 73 20 2a 2f 0a 20 20 75 38 20 75 73 65 53 6f  rs */.  u8 useSo
770f0 72 74 69 6e 67 49 64 78 3b 20 20 20 20 20 20 20  rtingIdx;       
77100 2f 2a 20 49 6e 20 64 69 72 65 63 74 20 6d 6f 64  /* In direct mod
77110 65 2c 20 72 65 66 65 72 65 6e 63 65 20 74 68 65  e, reference the
77120 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 72   sorting index r
77130 61 74 68 65 72 0a 20 20 20 20 20 20 20 20 20 20  ather.          
77140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77150 2a 2a 20 74 68 61 6e 20 74 68 65 20 73 6f 75 72  ** than the sour
77160 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  ce table */.  in
77170 74 20 73 6f 72 74 69 6e 67 49 64 78 3b 20 20 20  t sortingIdx;   
77180 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
77190 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f  number of the so
771a0 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20  rting index */. 
771b0 20 69 6e 74 20 73 6f 72 74 69 6e 67 49 64 78 50   int sortingIdxP
771c0 54 61 62 3b 20 20 20 20 20 2f 2a 20 43 75 72 73  Tab;     /* Curs
771d0 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 70 73 65  or number of pse
771e0 75 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 69  udo-table */.  i
771f0 6e 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d  nt nSortingColum
77200 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  n;     /* Number
77210 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
77220 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  he sorting index
77230 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
77240 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a  pGroupBy;     /*
77250 20 54 68 65 20 67 72 6f 75 70 20 62 79 20 63 6c   The group by cl
77260 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  ause */.  struct
77270 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 7b 20 20   AggInfo_col {  
77280 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 63 6f    /* For each co
77290 6c 75 6d 6e 20 75 73 65 64 20 69 6e 20 73 6f 75  lumn used in sou
772a0 72 63 65 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20  rce tables */.  
772b0 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
772c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f             /* So
772d0 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  urce table */.  
772e0 20 20 69 6e 74 20 69 54 61 62 6c 65 3b 20 20 20    int iTable;   
772f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
77300 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74  rsor number of t
77310 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20  he source table 
77320 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75  */.    int iColu
77330 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
77340 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* Column number
77350 20 77 69 74 68 69 6e 20 74 68 65 20 73 6f 75 72   within the sour
77360 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  ce table */.    
77370 69 6e 74 20 69 53 6f 72 74 65 72 43 6f 6c 75 6d  int iSorterColum
77380 6e 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75  n;       /* Colu
77390 6d 6e 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65  mn number in the
773a0 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a   sorting index *
773b0 2f 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20  /.    int iMem; 
773c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
773d0 2a 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  * Memory locatio
773e0 6e 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61  n that acts as a
773f0 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20  ccumulator */.  
77400 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20    Expr *pExpr;  
77410 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
77420 65 20 6f 72 69 67 69 6e 61 6c 20 65 78 70 72 65  e original expre
77430 73 73 69 6f 6e 20 2a 2f 0a 20 20 7d 20 2a 61 43  ssion */.  } *aC
77440 6f 6c 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d  ol;.  int nColum
77450 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  n;            /*
77460 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 64 20   Number of used 
77470 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 5b  entries in aCol[
77480 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75  ] */.  int nAccu
77490 6d 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f  mulator;       /
774a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
774b0 6d 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74 68  mns that show th
774c0 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74  rough to the out
774d0 70 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20  put..           
774e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
774f0 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c  * Additional col
77500 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e  umns are used on
77510 6c 79 20 61 73 20 70 61 72 61 6d 65 74 65 72 73  ly as parameters
77520 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20   to.            
77530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
77540 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
77550 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  ions */.  struct
77560 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20   AggInfo_func { 
77570 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67    /* For each ag
77580 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
77590 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45   */.    Expr *pE
775a0 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
775b0 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65   /* Expression e
775c0 6e 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63  ncoding the func
775d0 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63  tion */.    Func
775e0 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20  Def *pFunc;     
775f0 20 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72       /* The aggr
77600 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69  egate function i
77610 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f  mplementation */
77620 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20  .    int iMem;  
77630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
77640 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   Memory location
77650 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63   that acts as ac
77660 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20  cumulator */.   
77670 20 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20   int iDistinct; 
77680 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68            /* Eph
77690 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65  emeral table use
776a0 64 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53  d to enforce DIS
776b0 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46  TINCT */.  } *aF
776c0 75 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63  unc;.  int nFunc
776d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
776e0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
776f0 69 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a  ies in aFunc[] *
77700 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  /.};../*.** The 
77710 64 61 74 61 74 79 70 65 20 79 6e 56 61 72 20 69  datatype ynVar i
77720 73 20 61 20 73 69 67 6e 65 64 20 69 6e 74 65 67  s a signed integ
77730 65 72 2c 20 65 69 74 68 65 72 20 31 36 2d 62 69  er, either 16-bi
77740 74 20 6f 72 20 33 32 2d 62 69 74 2e 0a 2a 2a 20  t or 32-bit..** 
77750 55 73 75 61 6c 6c 79 20 69 74 20 69 73 20 31 36  Usually it is 16
77760 2d 62 69 74 73 2e 20 20 42 75 74 20 69 66 20 53  -bits.  But if S
77770 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42  QLITE_MAX_VARIAB
77780 4c 45 5f 4e 55 4d 42 45 52 20 69 73 20 67 72 65  LE_NUMBER is gre
77790 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 33 32 37  ater.** than 327
777a0 36 37 20 77 65 20 68 61 76 65 20 74 6f 20 6d 61  67 we have to ma
777b0 6b 65 20 69 74 20 33 32 2d 62 69 74 2e 20 20 31  ke it 32-bit.  1
777c0 36 2d 62 69 74 20 69 73 20 70 72 65 66 65 72 72  6-bit is preferr
777d0 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 69 74  ed because.** it
777e0 20 75 73 65 73 20 6c 65 73 73 20 6d 65 6d 6f 72   uses less memor
777f0 79 20 69 6e 20 74 68 65 20 45 78 70 72 20 6f 62  y in the Expr ob
77800 6a 65 63 74 2c 20 77 68 69 63 68 20 69 73 20 61  ject, which is a
77810 20 62 69 67 20 6d 65 6d 6f 72 79 20 75 73 65 72   big memory user
77820 0a 2a 2a 20 69 6e 20 73 79 73 74 65 6d 73 20 77  .** in systems w
77830 69 74 68 20 6c 6f 74 73 20 6f 66 20 70 72 65 70  ith lots of prep
77840 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e  ared statements.
77850 20 20 41 6e 64 20 66 65 77 20 61 70 70 6c 69 63    And few applic
77860 61 74 69 6f 6e 73 0a 2a 2a 20 6e 65 65 64 20 6d  ations.** need m
77870 6f 72 65 20 74 68 61 6e 20 61 62 6f 75 74 20 31  ore than about 1
77880 30 20 6f 72 20 32 30 20 76 61 72 69 61 62 6c 65  0 or 20 variable
77890 73 2e 20 20 42 75 74 20 73 6f 6d 65 20 65 78 74  s.  But some ext
778a0 72 65 6d 65 20 75 73 65 72 73 20 77 61 6e 74 0a  reme users want.
778b0 2a 2a 20 74 6f 20 68 61 76 65 20 70 72 65 70 61  ** to have prepa
778c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 77  red statements w
778d0 69 74 68 20 6f 76 65 72 20 33 32 37 36 37 20 76  ith over 32767 v
778e0 61 72 69 61 62 6c 65 73 2c 20 61 6e 64 20 66 6f  ariables, and fo
778f0 72 20 74 68 65 6d 0a 2a 2a 20 74 68 65 20 6f 70  r them.** the op
77900 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c  tion is availabl
77910 65 20 28 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  e (at compile-ti
77920 6d 65 29 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  me)..*/.#if SQLI
77930 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f  TE_MAX_VARIABLE_
77940 4e 55 4d 42 45 52 3c 3d 33 32 37 36 37 0a 74 79  NUMBER<=32767.ty
77950 70 65 64 65 66 20 69 31 36 20 79 6e 56 61 72 3b  pedef i16 ynVar;
77960 0a 23 65 6c 73 65 0a 74 79 70 65 64 65 66 20 69  .#else.typedef i
77970 6e 74 20 79 6e 56 61 72 3b 0a 23 65 6e 64 69 66  nt ynVar;.#endif
77980 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6e 6f 64  ../*.** Each nod
77990 65 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69  e of an expressi
779a0 6f 6e 20 69 6e 20 74 68 65 20 70 61 72 73 65 20  on in the parse 
779b0 74 72 65 65 20 69 73 20 61 6e 20 69 6e 73 74 61  tree is an insta
779c0 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73  nce.** of this s
779d0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
779e0 45 78 70 72 2e 6f 70 20 69 73 20 74 68 65 20 6f  Expr.op is the o
779f0 70 63 6f 64 65 2e 20 54 68 65 20 69 6e 74 65 67  pcode. The integ
77a00 65 72 20 70 61 72 73 65 72 20 74 6f 6b 65 6e 20  er parser token 
77a10 63 6f 64 65 73 20 61 72 65 20 72 65 75 73 65 64  codes are reused
77a20 0a 2a 2a 20 61 73 20 6f 70 63 6f 64 65 73 20 68  .** as opcodes h
77a30 65 72 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  ere. For example
77a40 2c 20 74 68 65 20 70 61 72 73 65 72 20 64 65 66  , the parser def
77a50 69 6e 65 73 20 54 4b 5f 47 45 20 74 6f 20 62 65  ines TK_GE to be
77a60 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 63   an integer.** c
77a70 6f 64 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67  ode representing
77a80 20 74 68 65 20 22 3e 3d 22 20 6f 70 65 72 61 74   the ">=" operat
77a90 6f 72 2e 20 54 68 69 73 20 73 61 6d 65 20 69 6e  or. This same in
77aa0 74 65 67 65 72 20 63 6f 64 65 20 69 73 20 72 65  teger code is re
77ab0 75 73 65 64 0a 2a 2a 20 74 6f 20 72 65 70 72 65  used.** to repre
77ac0 73 65 6e 74 20 74 68 65 20 67 72 65 61 74 65 72  sent the greater
77ad0 2d 74 68 61 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74  -than-or-equal-t
77ae0 6f 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 74 68  o operator in th
77af0 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  e expression.** 
77b00 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  tree..**.** If t
77b10 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
77b20 20 61 6e 20 53 51 4c 20 6c 69 74 65 72 61 6c 20   an SQL literal 
77b30 28 54 4b 5f 49 4e 54 45 47 45 52 2c 20 54 4b 5f  (TK_INTEGER, TK_
77b40 46 4c 4f 41 54 2c 20 54 4b 5f 42 4c 4f 42 2c 20  FLOAT, TK_BLOB, 
77b50 0a 2a 2a 20 6f 72 20 54 4b 5f 53 54 52 49 4e 47  .** or TK_STRING
77b60 29 2c 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b  ), then Expr.tok
77b70 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  en contains the 
77b80 74 65 78 74 20 6f 66 20 74 68 65 20 53 51 4c 20  text of the SQL 
77b90 6c 69 74 65 72 61 6c 2e 20 49 66 0a 2a 2a 20 74  literal. If.** t
77ba0 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
77bb0 20 61 20 76 61 72 69 61 62 6c 65 20 28 54 4b 5f   a variable (TK_
77bc0 56 41 52 49 41 42 4c 45 29 2c 20 74 68 65 6e 20  VARIABLE), then 
77bd0 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61  Expr.token conta
77be0 69 6e 73 20 74 68 65 20 0a 2a 2a 20 76 61 72 69  ins the .** vari
77bf0 61 62 6c 65 20 6e 61 6d 65 2e 20 46 69 6e 61 6c  able name. Final
77c00 6c 79 2c 20 69 66 20 74 68 65 20 65 78 70 72 65  ly, if the expre
77c10 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20  ssion is an SQL 
77c20 66 75 6e 63 74 69 6f 6e 20 28 54 4b 5f 46 55 4e  function (TK_FUN
77c30 43 54 49 4f 4e 29 2c 0a 2a 2a 20 74 68 65 6e 20  CTION),.** then 
77c40 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61  Expr.token conta
77c50 69 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  ins the name of 
77c60 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  the function..**
77c70 0a 2a 2a 20 45 78 70 72 2e 70 52 69 67 68 74 20  .** Expr.pRight 
77c80 61 6e 64 20 45 78 70 72 2e 70 4c 65 66 74 20 61  and Expr.pLeft a
77c90 72 65 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20  re the left and 
77ca0 72 69 67 68 74 20 73 75 62 65 78 70 72 65 73 73  right subexpress
77cb0 69 6f 6e 73 20 6f 66 20 61 0a 2a 2a 20 62 69 6e  ions of a.** bin
77cc0 61 72 79 20 6f 70 65 72 61 74 6f 72 2e 20 45 69  ary operator. Ei
77cd0 74 68 65 72 20 6f 72 20 62 6f 74 68 20 6d 61 79  ther or both may
77ce0 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   be NULL..**.** 
77cf0 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 69 73 20  Expr.x.pList is 
77d00 61 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65  a list of argume
77d10 6e 74 73 20 69 66 20 74 68 65 20 65 78 70 72 65  nts if the expre
77d20 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20  ssion is an SQL 
77d30 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 61 20 43  function,.** a C
77d40 41 53 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6f  ASE expression o
77d50 72 20 61 6e 20 49 4e 20 65 78 70 72 65 73 73 69  r an IN expressi
77d60 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  on of the form "
77d70 3c 6c 68 73 3e 20 49 4e 20 28 3c 79 3e 2c 20 3c  <lhs> IN (<y>, <
77d80 7a 3e 2e 2e 2e 29 22 2e 0a 2a 2a 20 45 78 70 72  z>...)"..** Expr
77d90 2e 78 2e 70 53 65 6c 65 63 74 20 69 73 20 75 73  .x.pSelect is us
77da0 65 64 20 69 66 20 74 68 65 20 65 78 70 72 65 73  ed if the expres
77db0 73 69 6f 6e 20 69 73 20 61 20 73 75 62 2d 73 65  sion is a sub-se
77dc0 6c 65 63 74 20 6f 72 20 61 6e 20 65 78 70 72 65  lect or an expre
77dd0 73 73 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20  ssion of.** the 
77de0 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28  form "<lhs> IN (
77df0 53 45 4c 45 43 54 20 2e 2e 2e 29 22 2e 20 49 66  SELECT ...)". If
77e00 20 74 68 65 20 45 50 5f 78 49 73 53 65 6c 65 63   the EP_xIsSelec
77e10 74 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20  t bit is set in 
77e20 74 68 65 0a 2a 2a 20 45 78 70 72 2e 66 6c 61 67  the.** Expr.flag
77e30 73 20 6d 61 73 6b 2c 20 74 68 65 6e 20 45 78 70  s mask, then Exp
77e40 72 2e 78 2e 70 53 65 6c 65 63 74 20 69 73 20 76  r.x.pSelect is v
77e50 61 6c 69 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  alid. Otherwise,
77e60 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 69 73   Expr.x.pList is
77e70 20 0a 2a 2a 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a   .** valid..**.*
77e80 2a 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  * An expression 
77e90 6f 66 20 74 68 65 20 66 6f 72 6d 20 49 44 20 6f  of the form ID o
77ea0 72 20 49 44 2e 49 44 20 72 65 66 65 72 73 20 74  r ID.ID refers t
77eb0 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20  o a column in a 
77ec0 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f 72 20 73 75  table..** For su
77ed0 63 68 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20  ch expressions, 
77ee0 45 78 70 72 2e 6f 70 20 69 73 20 73 65 74 20 74  Expr.op is set t
77ef0 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20  o TK_COLUMN and 
77f00 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73 0a 2a  Expr.iTable is.*
77f10 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20 63 75  * the integer cu
77f20 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 61  rsor number of a
77f30 20 56 44 42 45 20 63 75 72 73 6f 72 20 70 6f 69   VDBE cursor poi
77f40 6e 74 69 6e 67 20 74 6f 20 74 68 61 74 20 74 61  nting to that ta
77f50 62 6c 65 20 61 6e 64 0a 2a 2a 20 45 78 70 72 2e  ble and.** Expr.
77f60 69 43 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 63  iColumn is the c
77f70 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72  olumn number for
77f80 20 74 68 65 20 73 70 65 63 69 66 69 63 20 63 6f   the specific co
77f90 6c 75 6d 6e 2e 20 20 49 66 20 74 68 65 0a 2a 2a  lumn.  If the.**
77fa0 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 75   expression is u
77fb0 73 65 64 20 61 73 20 61 20 72 65 73 75 6c 74 20  sed as a result 
77fc0 69 6e 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  in an aggregate 
77fd0 53 45 4c 45 43 54 2c 20 74 68 65 6e 20 74 68 65  SELECT, then the
77fe0 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 61 6c 73  .** value is als
77ff0 6f 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  o stored in the 
78000 45 78 70 72 2e 69 41 67 67 20 63 6f 6c 75 6d 6e  Expr.iAgg column
78010 20 69 6e 20 74 68 65 20 61 67 67 72 65 67 61 74   in the aggregat
78020 65 20 73 6f 20 74 68 61 74 0a 2a 2a 20 69 74 20  e so that.** it 
78030 63 61 6e 20 62 65 20 61 63 63 65 73 73 65 64 20  can be accessed 
78040 61 66 74 65 72 20 61 6c 6c 20 61 67 67 72 65 67  after all aggreg
78050 61 74 65 73 20 61 72 65 20 63 6f 6d 70 75 74 65  ates are compute
78060 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
78070 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e  expression is an
78080 20 75 6e 62 6f 75 6e 64 20 76 61 72 69 61 62 6c   unbound variabl
78090 65 20 6d 61 72 6b 65 72 20 28 61 20 71 75 65 73  e marker (a ques
780a0 74 69 6f 6e 20 6d 61 72 6b 20 0a 2a 2a 20 63 68  tion mark .** ch
780b0 61 72 61 63 74 65 72 20 27 3f 27 20 69 6e 20 74  aracter '?' in t
780c0 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 29  he original SQL)
780d0 20 74 68 65 6e 20 74 68 65 20 45 78 70 72 2e 69   then the Expr.i
780e0 54 61 62 6c 65 20 68 6f 6c 64 73 20 74 68 65 20  Table holds the 
780f0 69 6e 64 65 78 20 0a 2a 2a 20 6e 75 6d 62 65 72  index .** number
78100 20 66 6f 72 20 74 68 61 74 20 76 61 72 69 61 62   for that variab
78110 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  le..**.** If the
78120 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
78130 20 73 75 62 71 75 65 72 79 20 74 68 65 6e 20 45   subquery then E
78140 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 68 6f 6c 64  xpr.iColumn hold
78150 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20  s an integer.** 
78160 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20  register number 
78170 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72  containing the r
78180 65 73 75 6c 74 20 6f 66 20 74 68 65 20 73 75 62  esult of the sub
78190 71 75 65 72 79 2e 20 20 49 66 20 74 68 65 0a 2a  query.  If the.*
781a0 2a 20 73 75 62 71 75 65 72 79 20 67 69 76 65 73  * subquery gives
781b0 20 61 20 63 6f 6e 73 74 61 6e 74 20 72 65 73 75   a constant resu
781c0 6c 74 2c 20 74 68 65 6e 20 69 54 61 62 6c 65 20  lt, then iTable 
781d0 69 73 20 2d 31 2e 20 20 49 66 20 74 68 65 20 73  is -1.  If the s
781e0 75 62 71 75 65 72 79 0a 2a 2a 20 67 69 76 65 73  ubquery.** gives
781f0 20 61 20 64 69 66 66 65 72 65 6e 74 20 61 6e 73   a different ans
78200 77 65 72 20 61 74 20 64 69 66 66 65 72 65 6e 74  wer at different
78210 20 74 69 6d 65 73 20 64 75 72 69 6e 67 20 73 74   times during st
78220 61 74 65 6d 65 6e 74 20 70 72 6f 63 65 73 73 69  atement processi
78230 6e 67 0a 2a 2a 20 74 68 65 6e 20 69 54 61 62 6c  ng.** then iTabl
78240 65 20 69 73 20 74 68 65 20 61 64 64 72 65 73 73  e is the address
78250 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e 65   of a subroutine
78260 20 74 68 61 74 20 63 6f 6d 70 75 74 65 73 20 74   that computes t
78270 68 65 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a  he subquery..**.
78280 2a 2a 20 49 66 20 74 68 65 20 45 78 70 72 20 69  ** If the Expr i
78290 73 20 6f 66 20 74 79 70 65 20 4f 50 5f 43 6f 6c  s of type OP_Col
782a0 75 6d 6e 2c 20 61 6e 64 20 74 68 65 20 74 61 62  umn, and the tab
782b0 6c 65 20 69 74 20 69 73 20 73 65 6c 65 63 74 69  le it is selecti
782c0 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69 73 20 61 20  ng from.** is a 
782d0 64 69 73 6b 20 74 61 62 6c 65 20 6f 72 20 74 68  disk table or th
782e0 65 20 22 6f 6c 64 2e 2a 22 20 70 73 65 75 64 6f  e "old.*" pseudo
782f0 2d 74 61 62 6c 65 2c 20 74 68 65 6e 20 70 54 61  -table, then pTa
78300 62 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a  b points to the.
78310 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
78320 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f   table definitio
78330 6e 2e 0a 2a 2a 0a 2a 2a 20 41 4c 4c 4f 43 41 54  n..**.** ALLOCAT
78340 49 4f 4e 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a  ION NOTES:.**.**
78350 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 63 61   Expr objects ca
78360 6e 20 75 73 65 20 61 20 6c 6f 74 20 6f 66 20 6d  n use a lot of m
78370 65 6d 6f 72 79 20 73 70 61 63 65 20 69 6e 20 64  emory space in d
78380 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20  atabase schema. 
78390 20 54 6f 0a 2a 2a 20 68 65 6c 70 20 72 65 64 75   To.** help redu
783a0 63 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72  ce memory requir
783b0 65 6d 65 6e 74 73 2c 20 73 6f 6d 65 74 69 6d 65  ements, sometime
783c0 73 20 61 6e 20 45 78 70 72 20 6f 62 6a 65 63 74  s an Expr object
783d0 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72 75 6e   will be.** trun
783e0 63 61 74 65 64 2e 20 20 41 6e 64 20 74 6f 20 72  cated.  And to r
783f0 65 64 75 63 65 20 74 68 65 20 6e 75 6d 62 65 72  educe the number
78400 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63   of memory alloc
78410 61 74 69 6f 6e 73 2c 20 73 6f 6d 65 74 69 6d 65  ations, sometime
78420 73 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65  s.** two or more
78430 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 77 69   Expr objects wi
78440 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20  ll be stored in 
78450 61 20 73 69 6e 67 6c 65 20 6d 65 6d 6f 72 79 20  a single memory 
78460 61 6c 6c 6f 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74  allocation,.** t
78470 6f 67 65 74 68 65 72 20 77 69 74 68 20 45 78 70  ogether with Exp
78480 72 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 73  r.zToken strings
78490 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45  ..**.** If the E
784a0 50 5f 52 65 64 75 63 65 64 20 61 6e 64 20 45 50  P_Reduced and EP
784b0 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 73  _TokenOnly flags
784c0 20 61 72 65 20 73 65 74 20 77 68 65 6e 0a 2a 2a   are set when.**
784d0 20 61 6e 20 45 78 70 72 20 6f 62 6a 65 63 74 20   an Expr object 
784e0 69 73 20 74 72 75 6e 63 61 74 65 64 2e 20 20 57  is truncated.  W
784f0 68 65 6e 20 45 50 5f 52 65 64 75 63 65 64 20 69  hen EP_Reduced i
78500 73 20 73 65 74 2c 20 74 68 65 6e 20 61 6c 6c 0a  s set, then all.
78510 2a 2a 20 74 68 65 20 63 68 69 6c 64 20 45 78 70  ** the child Exp
78520 72 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65  r objects in the
78530 20 45 78 70 72 2e 70 4c 65 66 74 20 61 6e 64 20   Expr.pLeft and 
78540 45 78 70 72 2e 70 52 69 67 68 74 20 73 75 62 74  Expr.pRight subt
78550 72 65 65 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 74  rees.** are cont
78560 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65  ained within the
78570 20 73 61 6d 65 20 6d 65 6d 6f 72 79 20 61 6c 6c   same memory all
78580 6f 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 65 2c 20  ocation.  Note, 
78590 68 6f 77 65 76 65 72 2c 20 74 68 61 74 0a 2a 2a  however, that.**
785a0 20 74 68 65 20 73 75 62 74 72 65 65 73 20 69 6e   the subtrees in
785b0 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 6f 72   Expr.x.pList or
785c0 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 20   Expr.x.pSelect 
785d0 61 72 65 20 61 6c 77 61 79 73 20 73 65 70 61 72  are always separ
785e0 61 74 65 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  ately.** allocat
785f0 65 64 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f  ed, regardless o
78600 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  f whether or not
78610 20 45 50 5f 52 65 64 75 63 65 64 20 69 73 20 73   EP_Reduced is s
78620 65 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78  et..*/.struct Ex
78630 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b 20 20 20  pr {.  u8 op;   
78640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
78650 20 4f 70 65 72 61 74 69 6f 6e 20 70 65 72 66 6f   Operation perfo
78660 72 6d 65 64 20 62 79 20 74 68 69 73 20 6e 6f 64  rmed by this nod
78670 65 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69  e */.  char affi
78680 6e 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a  nity;         /*
78690 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66   The affinity of
786a0 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30   the column or 0
786b0 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e   if not a column
786c0 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b   */.  u16 flags;
786d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
786e0 56 61 72 69 6f 75 73 20 66 6c 61 67 73 2e 20 20  Various flags.  
786f0 45 50 5f 2a 20 53 65 65 20 62 65 6c 6f 77 20 2a  EP_* See below *
78700 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20  /.  union {.    
78710 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20  char *zToken;   
78720 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20         /* Token 
78730 76 61 6c 75 65 2e 20 5a 65 72 6f 20 74 65 72 6d  value. Zero term
78740 69 6e 61 74 65 64 20 61 6e 64 20 64 65 71 75 6f  inated and dequo
78750 74 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ted */.    int i
78760 56 61 6c 75 65 3b 20 20 20 20 20 20 20 20 20 20  Value;          
78770 20 20 2f 2a 20 4e 6f 6e 2d 6e 65 67 61 74 69 76    /* Non-negativ
78780 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  e integer value 
78790 69 66 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 2a  if EP_IntValue *
787a0 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 49  /.  } u;..  /* I
787b0 66 20 74 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e  f the EP_TokenOn
787c0 6c 79 20 66 6c 61 67 20 69 73 20 73 65 74 20 69  ly flag is set i
787d0 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73  n the Expr.flags
787e0 20 6d 61 73 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20   mask, then no. 
787f0 20 2a 2a 20 73 70 61 63 65 20 69 73 20 61 6c 6c   ** space is all
78800 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 66  ocated for the f
78810 69 65 6c 64 73 20 62 65 6c 6f 77 20 74 68 69 73  ields below this
78820 20 70 6f 69 6e 74 2e 20 41 6e 20 61 74 74 65 6d   point. An attem
78830 70 74 20 74 6f 0a 20 20 2a 2a 20 61 63 63 65 73  pt to.  ** acces
78840 73 20 74 68 65 6d 20 77 69 6c 6c 20 72 65 73 75  s them will resu
78850 6c 74 20 69 6e 20 61 20 73 65 67 66 61 75 6c 74  lt in a segfault
78860 20 6f 72 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e   or malfunction.
78870 20 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   .  ************
78880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
788a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
788b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 45 78  *********/..  Ex
788c0 70 72 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20  pr *pLeft;      
788d0 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 75 62       /* Left sub
788e0 6e 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a  node */.  Expr *
788f0 70 52 69 67 68 74 3b 20 20 20 20 20 20 20 20 20  pRight;         
78900 20 2f 2a 20 52 69 67 68 74 20 73 75 62 6e 6f 64   /* Right subnod
78910 65 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20  e */.  union {. 
78920 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69     ExprList *pLi
78930 73 74 3b 20 20 20 20 20 2f 2a 20 46 75 6e 63 74  st;     /* Funct
78940 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 6f 72  ion arguments or
78950 20 69 6e 20 22 3c 65 78 70 72 3e 20 49 4e 20 28   in "<expr> IN (
78960 3c 65 78 70 72 2d 6c 69 73 74 29 22 20 2a 2f 0a  <expr-list)" */.
78970 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c      Select *pSel
78980 65 63 74 3b 20 20 20 20 20 2f 2a 20 55 73 65 64  ect;     /* Used
78990 20 66 6f 72 20 73 75 62 2d 73 65 6c 65 63 74 73   for sub-selects
789a0 20 61 6e 64 20 22 3c 65 78 70 72 3e 20 49 4e 20   and "<expr> IN 
789b0 28 3c 73 65 6c 65 63 74 3e 29 22 20 2a 2f 0a 20  (<select>)" */. 
789c0 20 7d 20 78 3b 0a 20 20 43 6f 6c 6c 53 65 71 20   } x;.  CollSeq 
789d0 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 2f  *pColl;        /
789e0 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  * The collation 
789f0 74 79 70 65 20 6f 66 20 74 68 65 20 63 6f 6c 75  type of the colu
78a00 6d 6e 20 6f 72 20 30 20 2a 2f 0a 0a 20 20 2f 2a  mn or 0 */..  /*
78a10 20 49 66 20 74 68 65 20 45 50 5f 52 65 64 75 63   If the EP_Reduc
78a20 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 69  ed flag is set i
78a30 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73  n the Expr.flags
78a40 20 6d 61 73 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20   mask, then no. 
78a50 20 2a 2a 20 73 70 61 63 65 20 69 73 20 61 6c 6c   ** space is all
78a60 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 66  ocated for the f
78a70 69 65 6c 64 73 20 62 65 6c 6f 77 20 74 68 69 73  ields below this
78a80 20 70 6f 69 6e 74 2e 20 41 6e 20 61 74 74 65 6d   point. An attem
78a90 70 74 20 74 6f 0a 20 20 2a 2a 20 61 63 63 65 73  pt to.  ** acces
78aa0 73 20 74 68 65 6d 20 77 69 6c 6c 20 72 65 73 75  s them will resu
78ab0 6c 74 20 69 6e 20 61 20 73 65 67 66 61 75 6c 74  lt in a segfault
78ac0 20 6f 72 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e   or malfunction.
78ad0 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .  *************
78ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78b10 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 53  ********/..#if S
78b20 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
78b30 45 50 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65  EPTH>0.  int nHe
78b40 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20  ight;           
78b50 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 74 68 65  /* Height of the
78b60 20 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20   tree headed by 
78b70 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e  this node */.#en
78b80 64 69 66 0a 20 20 69 6e 74 20 69 54 61 62 6c 65  dif.  int iTable
78b90 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
78ba0 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 75 72 73 6f  TK_COLUMN: curso
78bb0 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c  r number of tabl
78bc0 65 20 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e  e holding column
78bd0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
78be0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f            ** TK_
78bf0 52 45 47 49 53 54 45 52 3a 20 72 65 67 69 73 74  REGISTER: regist
78c00 65 72 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20  er number.      
78c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78c20 20 20 20 2a 2a 20 54 4b 5f 54 52 49 47 47 45 52     ** TK_TRIGGER
78c30 3a 20 31 20 2d 3e 20 6e 65 77 2c 20 30 20 2d 3e  : 1 -> new, 0 ->
78c40 20 6f 6c 64 20 2a 2f 0a 20 20 79 6e 56 61 72 20   old */.  ynVar 
78c50 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  iColumn;        
78c60 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63   /* TK_COLUMN: c
78c70 6f 6c 75 6d 6e 20 69 6e 64 65 78 2e 20 20 2d 31  olumn index.  -1
78c80 20 66 6f 72 20 72 6f 77 69 64 2e 0a 20 20 20 20   for rowid..    
78c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78ca0 20 20 20 20 20 2a 2a 20 54 4b 5f 56 41 52 49 41       ** TK_VARIA
78cb0 42 4c 45 3a 20 76 61 72 69 61 62 6c 65 20 6e 75  BLE: variable nu
78cc0 6d 62 65 72 20 28 61 6c 77 61 79 73 20 3e 3d 20  mber (always >= 
78cd0 31 29 2e 20 2a 2f 0a 20 20 69 31 36 20 69 41 67  1). */.  i16 iAg
78ce0 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
78cf0 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69  /* Which entry i
78d00 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c  n pAggInfo->aCol
78d10 5b 5d 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20  [] or ->aFunc[] 
78d20 2a 2f 0a 20 20 69 31 36 20 69 52 69 67 68 74 4a  */.  i16 iRightJ
78d30 6f 69 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49  oinTable;   /* I
78d40 66 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74  f EP_FromJoin, t
78d50 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f  he right table o
78d60 66 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20  f the join */.  
78d70 75 38 20 66 6c 61 67 73 32 3b 20 20 20 20 20 20  u8 flags2;      
78d80 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64         /* Second
78d90 20 73 65 74 20 6f 66 20 66 6c 61 67 73 2e 20 20   set of flags.  
78da0 45 50 32 5f 2e 2e 2e 20 2a 2f 0a 20 20 75 38 20  EP2_... */.  u8 
78db0 6f 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20  op2;            
78dc0 20 20 20 20 2f 2a 20 54 4b 5f 52 45 47 49 53 54      /* TK_REGIST
78dd0 45 52 3a 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c  ER: original val
78de0 75 65 20 6f 66 20 45 78 70 72 2e 6f 70 0a 20 20  ue of Expr.op.  
78df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78e00 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f 43 4f 4c         ** TK_COL
78e10 55 4d 4e 3a 20 74 68 65 20 76 61 6c 75 65 20 6f  UMN: the value o
78e20 66 20 70 35 20 66 6f 72 20 4f 50 5f 43 6f 6c 75  f p5 for OP_Colu
78e30 6d 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  mn.             
78e40 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54              ** T
78e50 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20  K_AGG_FUNCTION: 
78e60 6e 65 73 74 69 6e 67 20 64 65 70 74 68 20 2a 2f  nesting depth */
78e70 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67  .  AggInfo *pAgg
78e80 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73 65  Info;     /* Use
78e90 64 20 62 79 20 54 4b 5f 41 47 47 5f 43 4f 4c 55  d by TK_AGG_COLU
78ea0 4d 4e 20 61 6e 64 20 54 4b 5f 41 47 47 5f 46 55  MN and TK_AGG_FU
78eb0 4e 43 54 49 4f 4e 20 2a 2f 0a 20 20 54 61 62 6c  NCTION */.  Tabl
78ec0 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
78ed0 20 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72 20     /* Table for 
78ee0 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 73  TK_COLUMN expres
78ef0 73 69 6f 6e 73 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  sions. */.};../*
78f00 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
78f10 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e 69 6e  g are the meanin
78f20 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68  gs of bits in th
78f30 65 20 45 78 70 72 2e 66 6c 61 67 73 20 66 69 65  e Expr.flags fie
78f40 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45  ld..*/.#define E
78f50 50 5f 46 72 6f 6d 4a 6f 69 6e 20 20 20 30 78 30  P_FromJoin   0x0
78f60 30 30 31 20 20 2f 2a 20 4f 72 69 67 69 6e 61 74  001  /* Originat
78f70 65 64 20 69 6e 20 4f 4e 20 6f 72 20 55 53 49 4e  ed in ON or USIN
78f80 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f  G clause of a jo
78f90 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  in */.#define EP
78fa0 5f 41 67 67 20 20 20 20 20 20 20 20 30 78 30 30  _Agg        0x00
78fb0 30 32 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 20  02  /* Contains 
78fc0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 61 67 67 72  one or more aggr
78fd0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
78fe0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 52 65  */.#define EP_Re
78ff0 73 6f 6c 76 65 64 20 20 20 30 78 30 30 30 34 20  solved   0x0004 
79000 20 2f 2a 20 49 44 73 20 68 61 76 65 20 62 65 65   /* IDs have bee
79010 6e 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 43 4f  n resolved to CO
79020 4c 55 4d 4e 73 20 2a 2f 0a 23 64 65 66 69 6e 65  LUMNs */.#define
79030 20 45 50 5f 45 72 72 6f 72 20 20 20 20 20 20 30   EP_Error      0
79040 78 30 30 30 38 20 20 2f 2a 20 45 78 70 72 65 73  x0008  /* Expres
79050 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 6f 6e  sion contains on
79060 65 20 6f 72 20 6d 6f 72 65 20 65 72 72 6f 72 73  e or more errors
79070 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44   */.#define EP_D
79080 69 73 74 69 6e 63 74 20 20 20 30 78 30 30 31 30  istinct   0x0010
79090 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66    /* Aggregate f
790a0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 44 49 53  unction with DIS
790b0 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 2a 2f  TINCT keyword */
790c0 0a 23 64 65 66 69 6e 65 20 45 50 5f 56 61 72 53  .#define EP_VarS
790d0 65 6c 65 63 74 20 20 30 78 30 30 32 30 20 20 2f  elect  0x0020  /
790e0 2a 20 70 53 65 6c 65 63 74 20 69 73 20 63 6f 72  * pSelect is cor
790f0 72 65 6c 61 74 65 64 2c 20 6e 6f 74 20 63 6f 6e  related, not con
79100 73 74 61 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65  stant */.#define
79110 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 20 20 30   EP_DblQuoted  0
79120 78 30 30 34 30 20 20 2f 2a 20 74 6f 6b 65 6e 2e  x0040  /* token.
79130 7a 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79  z was originally
79140 20 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a 23 64 65   in "..." */.#de
79150 66 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 6e  fine EP_InfixFun
79160 63 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 72  c  0x0080  /* Tr
79170 75 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 20  ue for an infix 
79180 66 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20  function: LIKE, 
79190 47 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65  GLOB, etc */.#de
791a0 66 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61  fine EP_ExpColla
791b0 74 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 6f  te 0x0100  /* Co
791c0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
791d0 20 73 70 65 63 69 66 69 65 64 20 65 78 70 6c 69   specified expli
791e0 63 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65  citly */.#define
791f0 20 45 50 5f 46 69 78 65 64 44 65 73 74 20 20 30   EP_FixedDest  0
79200 78 30 32 30 30 20 20 2f 2a 20 52 65 73 75 6c 74  x0200  /* Result
79210 20 6e 65 65 64 65 64 20 69 6e 20 61 20 73 70 65   needed in a spe
79220 63 69 66 69 63 20 72 65 67 69 73 74 65 72 20 2a  cific register *
79230 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 49 6e 74  /.#define EP_Int
79240 56 61 6c 75 65 20 20 20 30 78 30 34 30 30 20 20  Value   0x0400  
79250 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65  /* Integer value
79260 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 75 2e   contained in u.
79270 69 56 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e  iValue */.#defin
79280 65 20 45 50 5f 78 49 73 53 65 6c 65 63 74 20 20  e EP_xIsSelect  
79290 30 78 30 38 30 30 20 20 2f 2a 20 78 2e 70 53 65  0x0800  /* x.pSe
792a0 6c 65 63 74 20 69 73 20 76 61 6c 69 64 20 28 6f  lect is valid (o
792b0 74 68 65 72 77 69 73 65 20 78 2e 70 4c 69 73 74  therwise x.pList
792c0 20 69 73 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20   is) */.#define 
792d0 45 50 5f 48 69 6e 74 20 20 20 20 20 20 20 30 78  EP_Hint       0x
792e0 31 30 30 30 20 20 2f 2a 20 4e 6f 74 20 75 73 65  1000  /* Not use
792f0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  d */.#define EP_
79300 52 65 64 75 63 65 64 20 20 20 20 30 78 32 30 30  Reduced    0x200
79310 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75 63  0  /* Expr struc
79320 74 20 69 73 20 45 58 50 52 5f 52 45 44 55 43 45  t is EXPR_REDUCE
79330 44 53 49 5a 45 20 62 79 74 65 73 20 6f 6e 6c 79  DSIZE bytes only
79340 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 54   */.#define EP_T
79350 6f 6b 65 6e 4f 6e 6c 79 20 20 30 78 34 30 30 30  okenOnly  0x4000
79360 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75 63 74    /* Expr struct
79370 20 69 73 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e   is EXPR_TOKENON
79380 4c 59 53 49 5a 45 20 62 79 74 65 73 20 6f 6e 6c  LYSIZE bytes onl
79390 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  y */.#define EP_
793a0 53 74 61 74 69 63 20 20 20 20 20 30 78 38 30 30  Static     0x800
793b0 30 20 20 2f 2a 20 48 65 6c 64 20 69 6e 20 6d 65  0  /* Held in me
793c0 6d 6f 72 79 20 6e 6f 74 20 6f 62 74 61 69 6e 65  mory not obtaine
793d0 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 20  d from malloc() 
793e0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  */../*.** The fo
793f0 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65 20  llowing are the 
79400 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 69 74 73  meanings of bits
79410 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61   in the Expr.fla
79420 67 73 32 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64  gs2 field..*/.#d
79430 65 66 69 6e 65 20 45 50 32 5f 4d 61 6c 6c 6f 63  efine EP2_Malloc
79440 65 64 54 6f 6b 65 6e 20 20 30 78 30 30 30 31 20  edToken  0x0001 
79450 20 2f 2a 20 4e 65 65 64 20 74 6f 20 73 71 6c 69   /* Need to sqli
79460 74 65 33 44 62 46 72 65 65 28 29 20 45 78 70 72  te3DbFree() Expr
79470 2e 7a 54 6f 6b 65 6e 20 2a 2f 0a 23 64 65 66 69  .zToken */.#defi
79480 6e 65 20 45 50 32 5f 49 72 72 65 64 75 63 69 62  ne EP2_Irreducib
79490 6c 65 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a  le    0x0002  /*
794a0 20 43 61 6e 6e 6f 74 20 45 58 50 52 44 55 50 5f   Cannot EXPRDUP_
794b0 52 45 44 55 43 45 20 74 68 69 73 20 45 78 70 72  REDUCE this Expr
794c0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70   */../*.** The p
794d0 73 65 75 64 6f 2d 72 6f 75 74 69 6e 65 20 73 71  seudo-routine sq
794e0 6c 69 74 65 33 45 78 70 72 53 65 74 49 72 72 65  lite3ExprSetIrre
794f0 64 75 63 69 62 6c 65 20 73 65 74 73 20 74 68 65  ducible sets the
79500 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 6c 65   EP2_Irreducible
79510 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 61 6e 20 65  .** flag on an e
79520 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74  xpression struct
79530 75 72 65 2e 20 20 54 68 69 73 20 66 6c 61 67 20  ure.  This flag 
79540 69 73 20 75 73 65 64 20 66 6f 72 20 56 56 26 41  is used for VV&A
79550 20 6f 6e 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 72   only.  The.** r
79560 6f 75 74 69 6e 65 20 69 73 20 69 6d 70 6c 65 6d  outine is implem
79570 65 6e 74 65 64 20 61 73 20 61 20 6d 61 63 72 6f  ented as a macro
79580 20 74 68 61 74 20 6f 6e 6c 79 20 77 6f 72 6b 73   that only works
79590 20 77 68 65 6e 20 69 6e 20 64 65 62 75 67 67 69   when in debuggi
795a0 6e 67 20 6d 6f 64 65 2c 0a 2a 2a 20 73 6f 20 61  ng mode,.** so a
795b0 73 20 6e 6f 74 20 74 6f 20 62 75 72 64 65 6e 20  s not to burden 
795c0 70 72 6f 64 75 63 74 69 6f 6e 20 63 6f 64 65 2e  production code.
795d0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
795e0 45 5f 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65  E_DEBUG.# define
795f0 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69   ExprSetIrreduci
79600 62 6c 65 28 58 29 20 20 28 58 29 2d 3e 66 6c 61  ble(X)  (X)->fla
79610 67 73 32 20 7c 3d 20 45 50 32 5f 49 72 72 65 64  gs2 |= EP2_Irred
79620 75 63 69 62 6c 65 0a 23 65 6c 73 65 0a 23 20 64  ucible.#else.# d
79630 65 66 69 6e 65 20 45 78 70 72 53 65 74 49 72 72  efine ExprSetIrr
79640 65 64 75 63 69 62 6c 65 28 58 29 0a 23 65 6e 64  educible(X).#end
79650 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20  if../*.** These 
79660 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73  macros can be us
79670 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c  ed to test, set,
79680 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69   or clear bits i
79690 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e 66  n the .** Expr.f
796a0 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23  lags field..*/.#
796b0 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 50 72  define ExprHasPr
796c0 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20  operty(E,P)     
796d0 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29  (((E)->flags&(P)
796e0 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20  )==(P)).#define 
796f0 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
79700 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d 3e  ty(E,P)  (((E)->
79710 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23  flags&(P))!=0).#
79720 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 50 72  define ExprSetPr
79730 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20  operty(E,P)     
79740 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a  (E)->flags|=(P).
79750 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65 61  #define ExprClea
79760 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20  rProperty(E,P)  
79770 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 50   (E)->flags&=~(P
79780 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20  )../*.** Macros 
79790 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
797a0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
797b0 20 72 65 71 75 69 72 65 64 20 62 79 20 61 20 6e   required by a n
797c0 6f 72 6d 61 6c 20 45 78 70 72 20 0a 2a 2a 20 73  ormal Expr .** s
797d0 74 72 75 63 74 2c 20 61 6e 20 45 78 70 72 20 73  truct, an Expr s
797e0 74 72 75 63 74 20 77 69 74 68 20 74 68 65 20 45  truct with the E
797f0 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20 73  P_Reduced flag s
79800 65 74 20 69 6e 20 45 78 70 72 2e 66 6c 61 67 73  et in Expr.flags
79810 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 45 78 70 72   .** and an Expr
79820 20 73 74 72 75 63 74 20 77 69 74 68 20 74 68 65   struct with the
79830 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c   EP_TokenOnly fl
79840 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69  ag set..*/.#defi
79850 6e 65 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45  ne EXPR_FULLSIZE
79860 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f             sizeo
79870 66 28 45 78 70 72 29 20 20 20 20 20 20 20 20 20  f(Expr)         
79880 20 20 2f 2a 20 46 75 6c 6c 20 73 69 7a 65 20 2a    /* Full size *
79890 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 52  /.#define EXPR_R
798a0 45 44 55 43 45 44 53 49 5a 45 20 20 20 20 20 20  EDUCEDSIZE      
798b0 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c    offsetof(Expr,
798c0 69 54 61 62 6c 65 29 20 20 2f 2a 20 43 6f 6d 6d  iTable)  /* Comm
798d0 6f 6e 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 23  on features */.#
798e0 64 65 66 69 6e 65 20 45 58 50 52 5f 54 4f 4b 45  define EXPR_TOKE
798f0 4e 4f 4e 4c 59 53 49 5a 45 20 20 20 20 20 20 6f  NONLYSIZE      o
79900 66 66 73 65 74 6f 66 28 45 78 70 72 2c 70 4c 65  ffsetof(Expr,pLe
79910 66 74 29 20 20 20 2f 2a 20 46 65 77 65 72 20 66  ft)   /* Fewer f
79920 65 61 74 75 72 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a  eatures */../*.*
79930 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74  * Flags passed t
79940 6f 20 74 68 65 20 73 71 6c 69 74 65 33 45 78 70  o the sqlite3Exp
79950 72 44 75 70 28 29 20 66 75 6e 63 74 69 6f 6e 2e  rDup() function.
79960 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72 20   See the header 
79970 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62 6f 76  comment .** abov
79980 65 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  e sqlite3ExprDup
79990 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a  () for details..
799a0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 44  */.#define EXPRD
799b0 55 50 5f 52 45 44 55 43 45 20 20 20 20 20 20 20  UP_REDUCE       
799c0 20 20 30 78 30 30 30 31 20 20 2f 2a 20 55 73 65    0x0001  /* Use
799d0 64 20 72 65 64 75 63 65 64 2d 73 69 7a 65 20 45  d reduced-size E
799e0 78 70 72 20 6e 6f 64 65 73 20 2a 2f 0a 0a 2f 2a  xpr nodes */../*
799f0 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 65 78  .** A list of ex
79a00 70 72 65 73 73 69 6f 6e 73 2e 20 20 45 61 63 68  pressions.  Each
79a10 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 79 20   expression may 
79a20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68 61 76 65 20  optionally have 
79a30 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20 41 6e 20 65  a.** name.  An e
79a40 78 70 72 2f 6e 61 6d 65 20 63 6f 6d 62 69 6e 61  xpr/name combina
79a50 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64  tion can be used
79a60 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79 73   in several ways
79a70 2c 20 73 75 63 68 0a 2a 2a 20 61 73 20 74 68 65  , such.** as the
79a80 20 6c 69 73 74 20 6f 66 20 22 65 78 70 72 20 41   list of "expr A
79a90 53 20 49 44 22 20 66 69 65 6c 64 73 20 66 6f 6c  S ID" fields fol
79aa0 6c 6f 77 69 6e 67 20 61 20 22 53 45 4c 45 43 54  lowing a "SELECT
79ab0 22 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 6c  " or in the.** l
79ac0 69 73 74 20 6f 66 20 22 49 44 20 3d 20 65 78 70  ist of "ID = exp
79ad0 72 22 20 69 74 65 6d 73 20 69 6e 20 61 6e 20 55  r" items in an U
79ae0 50 44 41 54 45 2e 20 20 41 20 6c 69 73 74 20 6f  PDATE.  A list o
79af0 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61  f expressions ca
79b00 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 65  n.** also be use
79b10 64 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e  d as the argumen
79b20 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2c  t to a function,
79b30 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74   in which case t
79b40 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a 2a 20 66 69  he a.zName.** fi
79b50 65 6c 64 20 69 73 20 6e 6f 74 20 75 73 65 64 2e  eld is not used.
79b60 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70 72 4c  .*/.struct ExprL
79b70 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78 70  ist {.  int nExp
79b80 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  r;             /
79b90 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 70 72  * Number of expr
79ba0 65 73 73 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6c  essions on the l
79bb0 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 45 43  ist */.  int iEC
79bc0 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20  ursor;          
79bd0 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20 61  /* VDBE Cursor a
79be0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
79bf0 68 69 73 20 45 78 70 72 4c 69 73 74 20 2a 2f 0a  his ExprList */.
79c00 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
79c10 74 5f 69 74 65 6d 20 7b 20 2f 2a 20 46 6f 72 20  t_item { /* For 
79c20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20  each expression 
79c30 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20  in the list */. 
79c40 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20     Expr *pExpr; 
79c50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
79c60 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73   list of express
79c70 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72  ions */.    char
79c80 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
79c90 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f     /* Token asso
79ca0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
79cb0 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
79cc0 20 20 20 63 68 61 72 20 2a 7a 53 70 61 6e 3b 20     char *zSpan; 
79cd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69            /* Ori
79ce0 67 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68  ginal text of th
79cf0 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  e expression */.
79d00 20 20 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72      u8 sortOrder
79d10 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20  ;          /* 1 
79d20 66 6f 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f  for DESC or 0 fo
79d30 72 20 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20  r ASC */.    u8 
79d40 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20  done;           
79d50 20 20 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f      /* A flag to
79d60 20 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70   indicate when p
79d70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e  rocessing is fin
79d80 69 73 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36  ished */.    u16
79d90 20 69 4f 72 64 65 72 42 79 43 6f 6c 3b 20 20 20   iOrderByCol;   
79da0 20 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52      /* For ORDER
79db0 20 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62   BY, column numb
79dc0 65 72 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74  er in result set
79dd0 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69   */.    u16 iAli
79de0 61 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  as;            /
79df0 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72  * Index into Par
79e00 73 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20  se.aAlias[] for 
79e10 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b  zName */.  } *a;
79e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
79e30 20 20 2f 2a 20 41 6c 6c 6f 63 20 61 20 70 6f 77    /* Alloc a pow
79e40 65 72 20 6f 66 20 74 77 6f 20 67 72 65 61 74 65  er of two greate
79e50 72 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 6e 45  r or equal to nE
79e60 78 70 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  xpr */.};../*.**
79e70 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
79e80 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
79e90 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61  s used by the pa
79ea0 72 73 65 72 20 74 6f 20 72 65 63 6f 72 64 20 62  rser to record b
79eb0 6f 74 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65  oth.** the parse
79ec0 20 74 72 65 65 20 66 6f 72 20 61 6e 20 65 78 70   tree for an exp
79ed0 72 65 73 73 69 6f 6e 20 61 6e 64 20 74 68 65 20  ression and the 
79ee0 73 70 61 6e 20 6f 66 20 69 6e 70 75 74 20 74 65  span of input te
79ef0 78 74 20 66 6f 72 20 61 6e 0a 2a 2a 20 65 78 70  xt for an.** exp
79f00 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75  ression..*/.stru
79f10 63 74 20 45 78 70 72 53 70 61 6e 20 7b 0a 20 20  ct ExprSpan {.  
79f20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20  Expr *pExpr;    
79f30 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
79f40 72 65 73 73 69 6f 6e 20 70 61 72 73 65 20 74 72  ression parse tr
79f50 65 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ee */.  const ch
79f60 61 72 20 2a 7a 53 74 61 72 74 3b 20 20 20 2f 2a  ar *zStart;   /*
79f70 20 46 69 72 73 74 20 63 68 61 72 61 63 74 65 72   First character
79f80 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20 2a   of input text *
79f90 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
79fa0 7a 45 6e 64 3b 20 20 20 20 20 2f 2a 20 4f 6e 65  zEnd;     /* One
79fb0 20 63 68 61 72 61 63 74 65 72 20 70 61 73 74 20   character past 
79fc0 74 68 65 20 65 6e 64 20 6f 66 20 69 6e 70 75 74  the end of input
79fd0 20 74 65 78 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   text */.};../*.
79fe0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
79ff0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
7a000 20 63 61 6e 20 68 6f 6c 64 20 61 20 73 69 6d 70   can hold a simp
7a010 6c 65 20 6c 69 73 74 20 6f 66 20 69 64 65 6e 74  le list of ident
7a020 69 66 69 65 72 73 2c 0a 2a 2a 20 73 75 63 68 20  ifiers,.** such 
7a030 61 73 20 74 68 65 20 6c 69 73 74 20 22 61 2c 62  as the list "a,b
7a040 2c 63 22 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  ,c" in the follo
7a050 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 3a  wing statements:
7a060 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 49 4e 53 45  .**.**      INSE
7a070 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29  RT INTO t(a,b,c)
7a080 20 56 41 4c 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20   VALUES ...;.** 
7a090 20 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45       CREATE INDE
7a0a0 58 20 69 64 78 20 4f 4e 20 74 28 61 2c 62 2c 63  X idx ON t(a,b,c
7a0b0 29 3b 0a 2a 2a 20 20 20 20 20 20 43 52 45 41 54  );.**      CREAT
7a0c0 45 20 54 52 49 47 47 45 52 20 74 72 69 67 20 42  E TRIGGER trig B
7a0d0 45 46 4f 52 45 20 55 50 44 41 54 45 20 4f 4e 20  EFORE UPDATE ON 
7a0e0 74 28 61 2c 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a  t(a,b,c) ...;.**
7a0f0 0a 2a 2a 20 54 68 65 20 49 64 4c 69 73 74 2e 61  .** The IdList.a
7a100 2e 69 64 78 20 66 69 65 6c 64 20 69 73 20 75 73  .idx field is us
7a110 65 64 20 77 68 65 6e 20 74 68 65 20 49 64 4c 69  ed when the IdLi
7a120 73 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68  st represents th
7a130 65 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c  e list of.** col
7a140 75 6d 6e 20 6e 61 6d 65 73 20 61 66 74 65 72 20  umn names after 
7a150 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20  a table name in 
7a160 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  an INSERT statem
7a170 65 6e 74 2e 20 20 49 6e 20 74 68 65 20 73 74 61  ent.  In the sta
7a180 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20  tement.**.**    
7a190 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 28 61   INSERT INTO t(a
7a1a0 2c 62 2c 63 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20  ,b,c) ....**.** 
7a1b0 49 66 20 22 61 22 20 69 73 20 74 68 65 20 6b 2d  If "a" is the k-
7a1c0 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62  th column of tab
7a1d0 6c 65 20 22 74 22 2c 20 74 68 65 6e 20 49 64 4c  le "t", then IdL
7a1e0 69 73 74 2e 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e  ist.a[0].idx==k.
7a1f0 0a 2a 2f 0a 73 74 72 75 63 74 20 49 64 4c 69 73  .*/.struct IdLis
7a200 74 20 7b 0a 20 20 73 74 72 75 63 74 20 49 64 4c  t {.  struct IdL
7a210 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 63  ist_item {.    c
7a220 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
7a230 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
7a240 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 20 20  identifier */.  
7a250 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20    int idx;      
7a260 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20      /* Index in 
7a270 73 6f 6d 65 20 54 61 62 6c 65 2e 61 43 6f 6c 5b  some Table.aCol[
7a280 5d 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6e 61  ] of a column na
7a290 6d 65 64 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d  med zName */.  }
7a2a0 20 2a 61 3b 0a 20 20 69 6e 74 20 6e 49 64 3b 20   *a;.  int nId; 
7a2b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
7a2c0 72 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72 73  r of identifiers
7a2d0 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a   on the list */.
7a2e0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69  };../*.** The bi
7a2f0 74 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20 64  tmask datatype d
7a300 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20  efined below is 
7a310 75 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73  used for various
7a320 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a   optimizations..
7a330 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74  **.** Changing t
7a340 68 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69  his from a 64-bi
7a350 74 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79  t to a 32-bit ty
7a360 70 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75  pe limits the nu
7a370 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65  mber of.** table
7a380 73 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33  s in a join to 3
7a390 32 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e  2 instead of 64.
7a3a0 20 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65    But it also re
7a3b0 64 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a  duces the size.*
7a3c0 2a 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79  * of the library
7a3d0 20 62 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e   by 738 bytes on
7a3e0 20 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65   ix86..*/.typede
7a3f0 66 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a  f u64 Bitmask;..
7a400 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72  /*.** The number
7a410 20 6f 66 20 62 69 74 73 20 69 6e 20 61 20 42 69   of bits in a Bi
7a420 74 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65  tmask.  "BMS" me
7a430 61 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a  ans "BitMask Siz
7a440 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42  e"..*/.#define B
7a450 4d 53 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f  MS  ((int)(sizeo
7a460 66 28 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a  f(Bitmask)*8))..
7a470 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
7a480 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65  ing structure de
7a490 73 63 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d  scribes the FROM
7a4a0 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c   clause of a SEL
7a4b0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ECT statement..*
7a4c0 2a 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20  * Each table or 
7a4d0 73 75 62 71 75 65 72 79 20 69 6e 20 74 68 65 20  subquery in the 
7a4e0 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61  FROM clause is a
7a4f0 20 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e   separate elemen
7a500 74 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c  t of.** the SrcL
7a510 69 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a  ist.a[] array..*
7a520 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64  *.** With the ad
7a530 64 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70  dition of multip
7a540 6c 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70  le database supp
7a550 6f 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ort, the followi
7a560 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  ng structure.** 
7a570 63 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64  can also be used
7a580 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70   to describe a p
7a590 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20  articular table 
7a5a0 73 75 63 68 20 61 73 20 74 68 65 20 74 61 62 6c  such as the tabl
7a5b0 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64  e that.** is mod
7a5c0 69 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45  ified by an INSE
7a5d0 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55  RT, DELETE, or U
7a5e0 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e  PDATE statement.
7a5f0 20 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51    In standard SQ
7a600 4c 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62  L,.** such a tab
7a610 6c 65 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d  le must be a sim
7a620 70 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42  ple name: ID.  B
7a630 75 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68  ut in SQLite, th
7a640 65 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e  e table can.** n
7a650 6f 77 20 62 65 20 69 64 65 6e 74 69 66 69 65 64  ow be identified
7a660 20 62 79 20 61 20 64 61 74 61 62 61 73 65 20 6e   by a database n
7a670 61 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e  ame, a dot, then
7a680 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a   the table name:
7a690 20 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68   ID.ID..**.** Th
7a6a0 65 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74  e jointype start
7a6b0 73 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68  s out showing th
7a6c0 65 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77  e join type betw
7a6d0 65 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20  een the current 
7a6e0 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65  table.** and the
7a6f0 20 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74   next table on t
7a700 68 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61  he list.  The pa
7a710 72 73 65 72 20 62 75 69 6c 64 73 20 74 68 65 20  rser builds the 
7a720 6c 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a  list this way..*
7a730 2a 20 42 75 74 20 73 71 6c 69 74 65 33 53 72 63  * But sqlite3Src
7a740 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70  ListShiftJoinTyp
7a750 65 28 29 20 6c 61 74 65 72 20 73 68 69 66 74 73  e() later shifts
7a760 20 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73   the jointypes s
7a770 6f 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a  o that each.** j
7a780 6f 69 6e 74 79 70 65 20 65 78 70 72 65 73 73 65  ointype expresse
7a790 73 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65  s the join betwe
7a7a0 65 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  en the table and
7a7b0 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 61   the previous ta
7a7c0 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  ble..**.** In th
7a7d0 65 20 63 6f 6c 55 73 65 64 20 66 69 65 6c 64 2c  e colUsed field,
7a7e0 20 74 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20   the high-order 
7a7f0 62 69 74 20 28 62 69 74 20 36 33 29 20 69 73 20  bit (bit 63) is 
7a800 73 65 74 20 69 66 20 74 68 65 20 74 61 62 6c 65  set if the table
7a810 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72  .** contains mor
7a820 65 20 74 68 61 6e 20 36 33 20 63 6f 6c 75 6d 6e  e than 63 column
7a830 73 20 61 6e 64 20 74 68 65 20 36 34 2d 74 68 20  s and the 64-th 
7a840 6f 72 20 6c 61 74 65 72 20 63 6f 6c 75 6d 6e 20  or later column 
7a850 69 73 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 72 75  is used..*/.stru
7a860 63 74 20 53 72 63 4c 69 73 74 20 7b 0a 20 20 69  ct SrcList {.  i
7a870 31 36 20 6e 53 72 63 3b 20 20 20 20 20 20 20 20  16 nSrc;        
7a880 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62  /* Number of tab
7a890 6c 65 73 20 6f 72 20 73 75 62 71 75 65 72 69 65  les or subquerie
7a8a0 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  s in the FROM cl
7a8b0 61 75 73 65 20 2a 2f 0a 20 20 69 31 36 20 6e 41  ause */.  i16 nA
7a8c0 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 4e 75  lloc;      /* Nu
7a8d0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
7a8e0 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 5b 5d  allocated in a[]
7a8f0 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 73 74 72 75   below */.  stru
7a900 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
7a910 7b 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53  {.    Schema *pS
7a920 63 68 65 6d 61 3b 20 20 2f 2a 20 53 63 68 65 6d  chema;  /* Schem
7a930 61 20 74 6f 20 77 68 69 63 68 20 74 68 69 73 20  a to which this 
7a940 69 74 65 6d 20 69 73 20 66 69 78 65 64 20 2a 2f  item is fixed */
7a950 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 61 74 61  .    char *zData
7a960 62 61 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f  base;  /* Name o
7a970 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69  f database holdi
7a980 6e 67 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  ng this table */
7a990 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  .    char *zName
7a9a0 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  ;      /* Name o
7a9b0 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  f the table */. 
7a9c0 20 20 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 3b     char *zAlias;
7a9d0 20 20 20 20 20 2f 2a 20 54 68 65 20 22 42 22 20       /* The "B" 
7a9e0 70 61 72 74 20 6f 66 20 61 20 22 41 20 41 53 20  part of a "A AS 
7a9f0 42 22 20 70 68 72 61 73 65 2e 20 20 7a 4e 61 6d  B" phrase.  zNam
7aa00 65 20 69 73 20 74 68 65 20 22 41 22 20 2a 2f 0a  e is the "A" */.
7aa10 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b      Table *pTab;
7aa20 20 20 20 20 20 20 2f 2a 20 41 6e 20 53 51 4c 20        /* An SQL 
7aa30 74 61 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64  table correspond
7aa40 69 6e 67 20 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a  ing to zName */.
7aa50 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c      Select *pSel
7aa60 65 63 74 3b 20 20 2f 2a 20 41 20 53 45 4c 45 43  ect;  /* A SELEC
7aa70 54 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64  T statement used
7aa80 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74   in place of a t
7aa90 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  able name */.   
7aaa0 20 69 6e 74 20 61 64 64 72 46 69 6c 6c 53 75 62   int addrFillSub
7aab0 3b 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66  ;  /* Address of
7aac0 20 73 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 6d   subroutine to m
7aad0 61 6e 69 66 65 73 74 20 61 20 73 75 62 71 75 65  anifest a subque
7aae0 72 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65  ry */.    int re
7aaf0 67 52 65 74 75 72 6e 3b 20 20 20 20 2f 2a 20 52  gReturn;    /* R
7ab00 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
7ab10 72 65 74 75 72 6e 20 61 64 64 72 65 73 73 20 6f  return address o
7ab20 66 20 61 64 64 72 46 69 6c 6c 53 75 62 20 2a 2f  f addrFillSub */
7ab30 0a 20 20 20 20 75 38 20 6a 6f 69 6e 74 79 70 65  .    u8 jointype
7ab40 3b 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f  ;      /* Type o
7ab50 66 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e 20 74  f join between t
7ab60 68 69 73 20 61 62 6c 65 20 61 6e 64 20 74 68 65  his able and the
7ab70 20 70 72 65 76 69 6f 75 73 20 2a 2f 0a 20 20 20   previous */.   
7ab80 20 75 6e 73 69 67 6e 65 64 20 6e 6f 74 49 6e 64   unsigned notInd
7ab90 65 78 65 64 20 3a 31 3b 20 20 20 20 2f 2a 20 54  exed :1;    /* T
7aba0 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20  rue if there is 
7abb0 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c  a NOT INDEXED cl
7abc0 61 75 73 65 20 2a 2f 0a 20 20 20 20 75 6e 73 69  ause */.    unsi
7abd0 67 6e 65 64 20 69 73 43 6f 72 72 65 6c 61 74 65  gned isCorrelate
7abe0 64 20 3a 31 3b 20 20 2f 2a 20 54 72 75 65 20 69  d :1;  /* True i
7abf0 66 20 73 75 62 2d 71 75 65 72 79 20 69 73 20 63  f sub-query is c
7ac00 6f 72 72 65 6c 61 74 65 64 20 2a 2f 0a 20 20 20  orrelated */.   
7ac10 20 75 6e 73 69 67 6e 65 64 20 76 69 61 43 6f 72   unsigned viaCor
7ac20 6f 75 74 69 6e 65 20 3a 31 3b 20 20 2f 2a 20 49  outine :1;  /* I
7ac30 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20  mplemented as a 
7ac40 63 6f 2d 72 6f 75 74 69 6e 65 20 2a 2f 0a 23 69  co-routine */.#i
7ac50 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
7ac60 54 5f 45 58 50 4c 41 49 4e 0a 20 20 20 20 75 38  T_EXPLAIN.    u8
7ac70 20 69 53 65 6c 65 63 74 49 64 3b 20 20 20 20 20   iSelectId;     
7ac80 2f 2a 20 49 66 20 70 53 65 6c 65 63 74 21 3d 30  /* If pSelect!=0
7ac90 2c 20 74 68 65 20 69 64 20 6f 66 20 74 68 65 20  , the id of the 
7aca0 73 75 62 2d 73 65 6c 65 63 74 20 69 6e 20 45 51  sub-select in EQ
7acb0 50 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 20 20  P */.#endif.    
7acc0 69 6e 74 20 69 43 75 72 73 6f 72 3b 20 20 20 20  int iCursor;    
7acd0 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75    /* The VDBE cu
7ace0 72 73 6f 72 20 6e 75 6d 62 65 72 20 75 73 65 64  rsor number used
7acf0 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 73 20   to access this 
7ad00 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70  table */.    Exp
7ad10 72 20 2a 70 4f 6e 3b 20 20 20 20 20 20 20 20 2f  r *pOn;        /
7ad20 2a 20 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 20  * The ON clause 
7ad30 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20  of a join */.   
7ad40 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 3b   IdList *pUsing;
7ad50 20 20 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20     /* The USING 
7ad60 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e  clause of a join
7ad70 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b 20   */.    Bitmask 
7ad80 63 6f 6c 55 73 65 64 3b 20 20 2f 2a 20 42 69 74  colUsed;  /* Bit
7ad90 20 4e 20 28 31 3c 3c 4e 29 20 73 65 74 20 69 66   N (1<<N) set if
7ada0 20 63 6f 6c 75 6d 6e 20 4e 20 6f 66 20 70 54 61   column N of pTa
7adb0 62 20 69 73 20 75 73 65 64 20 2a 2f 0a 20 20 20  b is used */.   
7adc0 20 63 68 61 72 20 2a 7a 49 6e 64 65 78 3b 20 20   char *zIndex;  
7add0 20 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72     /* Identifier
7ade0 20 66 72 6f 6d 20 22 49 4e 44 45 58 45 44 20 42   from "INDEXED B
7adf0 59 20 3c 7a 49 6e 64 65 78 3e 22 20 63 6c 61 75  Y <zIndex>" clau
7ae00 73 65 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20  se */.    Index 
7ae10 2a 70 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 49  *pIndex;    /* I
7ae20 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 63  ndex structure c
7ae30 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
7ae40 7a 49 6e 64 65 78 2c 20 69 66 20 61 6e 79 20 2a  zIndex, if any *
7ae50 2f 0a 20 20 7d 20 61 5b 31 5d 3b 20 20 20 20 20  /.  } a[1];     
7ae60 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65          /* One e
7ae70 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 69 64  ntry for each id
7ae80 65 6e 74 69 66 69 65 72 20 6f 6e 20 74 68 65 20  entifier on the 
7ae90 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  list */.};../*.*
7aea0 2a 20 50 65 72 6d 69 74 74 65 64 20 76 61 6c 75  * Permitted valu
7aeb0 65 73 20 6f 66 20 74 68 65 20 53 72 63 4c 69 73  es of the SrcLis
7aec0 74 2e 61 2e 6a 6f 69 6e 74 79 70 65 20 66 69 65  t.a.jointype fie
7aed0 6c 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54  ld.*/.#define JT
7aee0 5f 49 4e 4e 45 52 20 20 20 20 20 30 78 30 30 30  _INNER     0x000
7aef0 31 20 20 20 20 2f 2a 20 41 6e 79 20 6b 69 6e 64  1    /* Any kind
7af00 20 6f 66 20 69 6e 6e 65 72 20 6f 72 20 63 72 6f   of inner or cro
7af10 73 73 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69  ss join */.#defi
7af20 6e 65 20 4a 54 5f 43 52 4f 53 53 20 20 20 20 20  ne JT_CROSS     
7af30 30 78 30 30 30 32 20 20 20 20 2f 2a 20 45 78 70  0x0002    /* Exp
7af40 6c 69 63 69 74 20 75 73 65 20 6f 66 20 74 68 65  licit use of the
7af50 20 43 52 4f 53 53 20 6b 65 79 77 6f 72 64 20 2a   CROSS keyword *
7af60 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4e 41 54  /.#define JT_NAT
7af70 55 52 41 4c 20 20 20 30 78 30 30 30 34 20 20 20  URAL   0x0004   
7af80 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 22   /* True for a "
7af90 6e 61 74 75 72 61 6c 22 20 6a 6f 69 6e 20 2a 2f  natural" join */
7afa0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4c 45 46 54  .#define JT_LEFT
7afb0 20 20 20 20 20 20 30 78 30 30 30 38 20 20 20 20        0x0008    
7afc0 2f 2a 20 4c 65 66 74 20 6f 75 74 65 72 20 6a 6f  /* Left outer jo
7afd0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54  in */.#define JT
7afe0 5f 52 49 47 48 54 20 20 20 20 20 30 78 30 30 31  _RIGHT     0x001
7aff0 30 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 75  0    /* Right ou
7b000 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66  ter join */.#def
7b010 69 6e 65 20 4a 54 5f 4f 55 54 45 52 20 20 20 20  ine JT_OUTER    
7b020 20 30 78 30 30 32 30 20 20 20 20 2f 2a 20 54 68   0x0020    /* Th
7b030 65 20 22 4f 55 54 45 52 22 20 6b 65 79 77 6f 72  e "OUTER" keywor
7b040 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a  d is present */.
7b050 23 64 65 66 69 6e 65 20 4a 54 5f 45 52 52 4f 52  #define JT_ERROR
7b060 20 20 20 20 20 30 78 30 30 34 30 20 20 20 20 2f       0x0040    /
7b070 2a 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73  * unknown or uns
7b080 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79  upported join ty
7b090 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20  pe */.../*.** A 
7b0a0 57 68 65 72 65 50 6c 61 6e 20 6f 62 6a 65 63 74  WherePlan object
7b0b0 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69   holds informati
7b0c0 6f 6e 20 74 68 61 74 20 64 65 73 63 72 69 62 65  on that describe
7b0d0 73 20 61 20 6c 6f 6f 6b 75 70 0a 2a 2a 20 73 74  s a lookup.** st
7b0e0 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  rategy..**.** Th
7b0f0 69 73 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 74  is object is int
7b100 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71  ended to be opaq
7b110 75 65 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68  ue outside of th
7b120 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65  e where.c module
7b130 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6e 63 6c 75  ..** It is inclu
7b140 64 65 64 20 68 65 72 65 20 6f 6e 6c 79 20 73 6f  ded here only so
7b150 20 74 68 61 74 20 74 68 61 74 20 63 6f 6d 70 69   that that compi
7b160 6c 65 72 20 77 69 6c 6c 20 6b 6e 6f 77 20 68 6f  ler will know ho
7b170 77 20 62 69 67 20 69 74 0a 2a 2a 20 69 73 2e 20  w big it.** is. 
7b180 20 4e 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 65   None of the fie
7b190 6c 64 73 20 69 6e 20 74 68 69 73 20 6f 62 6a 65  lds in this obje
7b1a0 63 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65  ct should be use
7b1b0 64 20 6f 75 74 73 69 64 65 20 6f 66 0a 2a 2a 20  d outside of.** 
7b1c0 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75  the where.c modu
7b1d0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e  le..**.** Within
7b1e0 20 74 68 65 20 75 6e 69 6f 6e 2c 20 70 49 64 78   the union, pIdx
7b1f0 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68   is only used wh
7b200 65 6e 20 77 73 46 6c 61 67 73 26 57 48 45 52 45  en wsFlags&WHERE
7b210 5f 49 4e 44 45 58 45 44 20 69 73 20 74 72 75 65  _INDEXED is true
7b220 2e 0a 2a 2a 20 70 54 65 72 6d 20 69 73 20 6f 6e  ..** pTerm is on
7b230 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46  ly used when wsF
7b240 6c 61 67 73 26 57 48 45 52 45 5f 4d 55 4c 54 49  lags&WHERE_MULTI
7b250 5f 4f 52 20 69 73 20 74 72 75 65 2e 20 20 41 6e  _OR is true.  An
7b260 64 20 70 56 74 61 62 49 64 78 0a 2a 2a 20 69 73  d pVtabIdx.** is
7b270 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20   only used when 
7b280 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 56 49  wsFlags&WHERE_VI
7b290 52 54 55 41 4c 54 41 42 4c 45 20 69 73 20 74 72  RTUALTABLE is tr
7b2a0 75 65 2e 20 20 49 74 20 69 73 20 6e 65 76 65 72  ue.  It is never
7b2b0 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61   the.** case tha
7b2c0 74 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20  t more than one 
7b2d0 6f 66 20 74 68 65 73 65 20 63 6f 6e 64 69 74 69  of these conditi
7b2e0 6f 6e 73 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a  ons is true..*/.
7b2f0 73 74 72 75 63 74 20 57 68 65 72 65 50 6c 61 6e  struct WherePlan
7b300 20 7b 0a 20 20 75 33 32 20 77 73 46 6c 61 67 73   {.  u32 wsFlags
7b310 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
7b320 20 20 20 20 2f 2a 20 57 48 45 52 45 5f 2a 20 66      /* WHERE_* f
7b330 6c 61 67 73 20 74 68 61 74 20 64 65 73 63 72 69  lags that descri
7b340 62 65 20 74 68 65 20 73 74 72 61 74 65 67 79 20  be the strategy 
7b350 2a 2f 0a 20 20 75 31 36 20 6e 45 71 3b 20 20 20  */.  u16 nEq;   
7b360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7b370 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
7b380 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 20   == constraints 
7b390 2a 2f 0a 20 20 75 31 36 20 6e 4f 42 53 61 74 3b  */.  u16 nOBSat;
7b3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7b3b0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
7b3c0 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20   ORDER BY terms 
7b3d0 73 61 74 69 73 66 69 65 64 20 2a 2f 0a 20 20 64  satisfied */.  d
7b3e0 6f 75 62 6c 65 20 6e 52 6f 77 3b 20 20 20 20 20  ouble nRow;     
7b3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
7b400 20 45 73 74 69 6d 61 74 65 64 20 6e 75 6d 62 65   Estimated numbe
7b410 72 20 6f 66 20 72 6f 77 73 20 28 66 6f 72 20 45  r of rows (for E
7b420 51 50 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b  QP) */.  union {
7b430 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78  .    Index *pIdx
7b440 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
7b450 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 77 68 65      /* Index whe
7b460 6e 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 20  n WHERE_INDEXED 
7b470 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 20 20 73  is true */.    s
7b480 74 72 75 63 74 20 57 68 65 72 65 54 65 72 6d 20  truct WhereTerm 
7b490 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 2f 2a  *pTerm;       /*
7b4a0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
7b4b0 72 6d 20 66 6f 72 20 4f 52 2d 73 65 61 72 63 68  rm for OR-search
7b4c0 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   */.    sqlite3_
7b4d0 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 56 74 61  index_info *pVta
7b4e0 62 49 64 78 3b 20 20 2f 2a 20 56 69 72 74 75 61  bIdx;  /* Virtua
7b4f0 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 20 74 6f  l table index to
7b500 20 75 73 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d   use */.  } u;.}
7b510 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 61 63  ;../*.** For eac
7b520 68 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 69 6e  h nested loop in
7b530 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20   a WHERE clause 
7b540 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
7b550 74 68 65 20 57 68 65 72 65 49 6e 66 6f 0a 2a 2a  the WhereInfo.**
7b560 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
7b570 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 69 6e 73  ins a single ins
7b580 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
7b590 72 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 73  ructure.  This s
7b5a0 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 69  tructure.** is i
7b5b0 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 70 72  ntended to be pr
7b5c0 69 76 61 74 65 20 74 6f 20 74 68 65 20 77 68 65  ivate to the whe
7b5d0 72 65 2e 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20  re.c module and 
7b5e0 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
7b5f0 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66   access or modif
7b600 69 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64  ied by other mod
7b610 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ules..**.** The 
7b620 70 49 64 78 49 6e 66 6f 20 66 69 65 6c 64 20 69  pIdxInfo field i
7b630 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 70  s used to help p
7b640 69 63 6b 20 74 68 65 20 62 65 73 74 20 69 6e 64  ick the best ind
7b650 65 78 20 6f 6e 20 61 0a 2a 2a 20 76 69 72 74 75  ex on a.** virtu
7b660 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 70  al table.  The p
7b670 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20  IdxInfo pointer 
7b680 63 6f 6e 74 61 69 6e 73 20 69 6e 64 65 78 69 6e  contains indexin
7b690 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  g.** information
7b6a0 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61   for the i-th ta
7b6b0 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ble in the FROM 
7b6c0 63 6c 61 75 73 65 20 62 65 66 6f 72 65 20 72 65  clause before re
7b6d0 6f 72 64 65 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c  ordering..** All
7b6e0 20 74 68 65 20 70 49 64 78 49 6e 66 6f 20 70 6f   the pIdxInfo po
7b6f0 69 6e 74 65 72 73 20 61 72 65 20 66 72 65 65 64  inters are freed
7b700 20 62 79 20 77 68 65 72 65 49 6e 66 6f 46 72 65   by whereInfoFre
7b710 65 28 29 20 69 6e 20 77 68 65 72 65 2e 63 2e 0a  e() in where.c..
7b720 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 69 6e 66  ** All other inf
7b730 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20  ormation in the 
7b740 69 2d 74 68 20 57 68 65 72 65 4c 65 76 65 6c 20  i-th WhereLevel 
7b750 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 69  object for the i
7b760 2d 74 68 20 74 61 62 6c 65 0a 2a 2a 20 61 66 74  -th table.** aft
7b770 65 72 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  er FROM clause o
7b780 72 64 65 72 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75  rdering..*/.stru
7b790 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a  ct WhereLevel {.
7b7a0 20 20 57 68 65 72 65 50 6c 61 6e 20 70 6c 61 6e    WherePlan plan
7b7b0 3b 20 20 20 20 20 20 20 2f 2a 20 71 75 65 72 79  ;       /* query
7b7c0 20 70 6c 61 6e 20 66 6f 72 20 74 68 69 73 20 65   plan for this e
7b7d0 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 46 52  lement of the FR
7b7e0 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  OM clause */.  i
7b7f0 6e 74 20 69 4c 65 66 74 4a 6f 69 6e 3b 20 20 20  nt iLeftJoin;   
7b800 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
7b810 65 6c 6c 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  ell used to impl
7b820 65 6d 65 6e 74 20 4c 45 46 54 20 4f 55 54 45 52  ement LEFT OUTER
7b830 20 4a 4f 49 4e 20 2a 2f 0a 20 20 69 6e 74 20 69   JOIN */.  int i
7b840 54 61 62 43 75 72 3b 20 20 20 20 20 20 20 20 20  TabCur;         
7b850 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72   /* The VDBE cur
7b860 73 6f 72 20 75 73 65 64 20 74 6f 20 61 63 63 65  sor used to acce
7b870 73 73 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  ss the table */.
7b880 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20    int iIdxCur;  
7b890 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
7b8a0 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20  DBE cursor used 
7b8b0 74 6f 20 61 63 63 65 73 73 20 70 49 64 78 20 2a  to access pIdx *
7b8c0 2f 0a 20 20 69 6e 74 20 61 64 64 72 42 72 6b 3b  /.  int addrBrk;
7b8d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d            /* Jum
7b8e0 70 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20  p here to break 
7b8f0 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  out of the loop 
7b900 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4e 78 74  */.  int addrNxt
7b910 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75  ;          /* Ju
7b920 6d 70 20 68 65 72 65 20 74 6f 20 73 74 61 72 74  mp here to start
7b930 20 74 68 65 20 6e 65 78 74 20 49 4e 20 63 6f 6d   the next IN com
7b940 62 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  bination */.  in
7b950 74 20 61 64 64 72 43 6f 6e 74 3b 20 20 20 20 20  t addrCont;     
7b960 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
7b970 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74   to continue wit
7b980 68 20 74 68 65 20 6e 65 78 74 20 6c 6f 6f 70 20  h the next loop 
7b990 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61  cycle */.  int a
7b9a0 64 64 72 46 69 72 73 74 3b 20 20 20 20 20 20 20  ddrFirst;       
7b9b0 20 2f 2a 20 46 69 72 73 74 20 69 6e 73 74 72 75   /* First instru
7b9c0 63 74 69 6f 6e 20 6f 66 20 69 6e 74 65 72 69 6f  ction of interio
7b9d0 72 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  r of the loop */
7b9e0 0a 20 20 75 38 20 69 46 72 6f 6d 3b 20 20 20 20  .  u8 iFrom;    
7b9f0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63           /* Whic
7ba00 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 46  h entry in the F
7ba10 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ROM clause */.  
7ba20 75 38 20 6f 70 2c 20 70 35 3b 20 20 20 20 20 20  u8 op, p5;      
7ba30 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20        /* Opcode 
7ba40 61 6e 64 20 50 35 20 6f 66 20 74 68 65 20 6f 70  and P5 of the op
7ba50 63 6f 64 65 20 74 68 61 74 20 65 6e 64 73 20 74  code that ends t
7ba60 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  he loop */.  int
7ba70 20 70 31 2c 20 70 32 3b 20 20 20 20 20 20 20 20   p1, p2;        
7ba80 20 20 20 2f 2a 20 4f 70 65 72 61 6e 64 73 20 6f     /* Operands o
7ba90 66 20 74 68 65 20 6f 70 63 6f 64 65 20 75 73 65  f the opcode use
7baa0 64 20 74 6f 20 65 6e 64 73 20 74 68 65 20 6c 6f  d to ends the lo
7bab0 6f 70 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20  op */.  union { 
7bac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
7bad0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61   Information tha
7bae0 74 20 64 65 70 65 6e 64 73 20 6f 6e 20 70 6c 61  t depends on pla
7baf0 6e 2e 77 73 46 6c 61 67 73 20 2a 2f 0a 20 20 20  n.wsFlags */.   
7bb00 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 20 20   struct {.      
7bb10 69 6e 74 20 6e 49 6e 3b 20 20 20 20 20 20 20 20  int nIn;        
7bb20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
7bb30 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 49  of entries in aI
7bb40 6e 4c 6f 6f 70 5b 5d 20 2a 2f 0a 20 20 20 20 20  nLoop[] */.     
7bb50 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 7b   struct InLoop {
7bb60 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 75  .        int iCu
7bb70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
7bb80 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73  /* The VDBE curs
7bb90 6f 72 20 75 73 65 64 20 62 79 20 74 68 69 73 20  or used by this 
7bba0 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  IN operator */. 
7bbb0 20 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 49         int addrI
7bbc0 6e 54 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a  nTop;         /*
7bbd0 20 54 6f 70 20 6f 66 20 74 68 65 20 49 4e 20 6c   Top of the IN l
7bbe0 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 20 7d 20 2a  oop */.      } *
7bbf0 61 49 6e 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20  aInLoop;        
7bc00 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
7bc10 6e 20 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73  n about each nes
7bc20 74 65 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 20  ted IN operator 
7bc30 2a 2f 0a 20 20 20 20 7d 20 69 6e 3b 20 20 20 20  */.    } in;    
7bc40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
7bc50 55 73 65 64 20 77 68 65 6e 20 70 6c 61 6e 2e 77  Used when plan.w
7bc60 73 46 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 5f  sFlags&WHERE_IN_
7bc70 41 42 4c 45 20 2a 2f 0a 20 20 20 20 49 6e 64 65  ABLE */.    Inde
7bc80 78 20 2a 70 43 6f 76 69 64 78 3b 20 20 20 20 20  x *pCovidx;     
7bc90 20 20 2f 2a 20 50 6f 73 73 69 62 6c 65 20 63 6f    /* Possible co
7bca0 76 65 72 69 6e 67 20 69 6e 64 65 78 20 66 6f 72  vering index for
7bcb0 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20   WHERE_MULTI_OR 
7bcc0 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 64 6f 75 62  */.  } u;.  doub
7bcd0 6c 65 20 72 4f 70 74 43 6f 73 74 3b 20 20 20 20  le rOptCost;    
7bce0 20 20 2f 2a 20 22 4f 70 74 69 6d 61 6c 22 20 63    /* "Optimal" c
7bcf0 6f 73 74 20 66 6f 72 20 74 68 69 73 20 6c 65 76  ost for this lev
7bd00 65 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20  el */..  /* The 
7bd10 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20  following field 
7bd20 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61  is really not pa
7bd30 72 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  rt of the curren
7bd40 74 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20  t level.  But.  
7bd50 2a 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61  ** we need a pla
7bd60 63 65 20 74 6f 20 63 61 63 68 65 20 76 69 72 74  ce to cache virt
7bd70 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 20  ual table index 
7bd80 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
7bd90 65 61 63 68 0a 20 20 2a 2a 20 76 69 72 74 75 61  each.  ** virtua
7bda0 6c 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46  l table in the F
7bdb0 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e 64 20 74  ROM clause and t
7bdc0 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20 73 74  he WhereLevel st
7bdd0 72 75 63 74 75 72 65 20 69 73 0a 20 20 2a 2a 20  ructure is.  ** 
7bde0 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61  a convenient pla
7bdf0 63 65 20 73 69 6e 63 65 20 74 68 65 72 65 20 69  ce since there i
7be00 73 20 6f 6e 65 20 57 68 65 72 65 4c 65 76 65 6c  s one WhereLevel
7be10 20 66 6f 72 20 65 61 63 68 20 46 52 4f 4d 20 63   for each FROM c
7be20 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c 65 6d 65  lause.  ** eleme
7be30 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  nt..  */.  sqlit
7be40 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
7be50 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e 64  IdxInfo;  /* Ind
7be60 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d 74 68  ex info for n-th
7be70 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f   source table */
7be80 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73  .};../*.** Flags
7be90 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
7bea0 20 74 68 65 20 77 63 74 72 6c 46 6c 61 67 73 20   the wctrlFlags 
7beb0 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c  parameter of sql
7bec0 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
7bed0 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72  .** and the Wher
7bee0 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c 61 67 73  eInfo.wctrlFlags
7bef0 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66   member..*/.#def
7bf00 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42  ine WHERE_ORDERB
7bf10 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30 30 30  Y_NORMAL   0x000
7bf20 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23 64  0 /* No-op */.#d
7bf30 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45  efine WHERE_ORDE
7bf40 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30 78 30  RBY_MIN      0x0
7bf50 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42 59 20  001 /* ORDER BY 
7bf60 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d  processing for m
7bf70 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65  in() func */.#de
7bf80 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52  fine WHERE_ORDER
7bf90 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78 30 30  BY_MAX      0x00
7bfa0 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 70  02 /* ORDER BY p
7bfb0 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d 61  rocessing for ma
7bfc0 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66  x() func */.#def
7bfd0 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50 41 53  ine WHERE_ONEPAS
7bfe0 53 5f 44 45 53 49 52 45 44 20 20 30 78 30 30 30  S_DESIRED  0x000
7bff0 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64 6f 20  4 /* Want to do 
7c000 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54 45 2f  one-pass UPDATE/
7c010 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66 69 6e  DELETE */.#defin
7c020 65 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54  e WHERE_DUPLICAT
7c030 45 53 5f 4f 4b 20 20 20 20 30 78 30 30 30 38 20  ES_OK    0x0008 
7c040 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75 72 6e 20  /* Ok to return 
7c050 61 20 72 6f 77 20 6d 6f 72 65 20 74 68 61 6e 20  a row more than 
7c060 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  once */.#define 
7c070 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 5f  WHERE_OMIT_OPEN_
7c080 43 4c 4f 53 45 20 20 30 78 30 30 31 30 20 2f 2a  CLOSE  0x0010 /*
7c090 20 54 61 62 6c 65 20 63 75 72 73 6f 72 73 20 61   Table cursors a
7c0a0 72 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20  re already open 
7c0b0 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
7c0c0 5f 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 20 20  _FORCE_TABLE    
7c0d0 20 20 30 78 30 30 32 30 20 2f 2a 20 44 6f 20 6e    0x0020 /* Do n
7c0e0 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2d  ot use an index-
7c0f0 6f 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f 0a 23  only search */.#
7c100 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45  define WHERE_ONE
7c110 54 41 42 4c 45 5f 4f 4e 4c 59 20 20 20 20 30 78  TABLE_ONLY    0x
7c120 30 30 34 30 20 2f 2a 20 4f 6e 6c 79 20 63 6f 64  0040 /* Only cod
7c130 65 20 74 68 65 20 31 73 74 20 74 61 62 6c 65 20  e the 1st table 
7c140 69 6e 20 70 54 61 62 4c 69 73 74 20 2a 2f 0a 23  in pTabList */.#
7c150 64 65 66 69 6e 65 20 57 48 45 52 45 5f 41 4e 44  define WHERE_AND
7c160 5f 4f 4e 4c 59 20 20 20 20 20 20 20 20 20 30 78  _ONLY         0x
7c170 30 30 38 30 20 2f 2a 20 44 6f 6e 27 74 20 75 73  0080 /* Don't us
7c180 65 20 69 6e 64 69 63 65 73 20 66 6f 72 20 4f 52  e indices for OR
7c190 20 74 65 72 6d 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a   terms */../*.**
7c1a0 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
7c1b0 65 20 70 72 6f 63 65 73 73 69 6e 67 20 72 6f 75  e processing rou
7c1c0 74 69 6e 65 20 68 61 73 20 74 77 6f 20 68 61 6c  tine has two hal
7c1d0 76 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66 69 72  ves.  The.** fir
7c1e0 73 74 20 70 61 72 74 20 64 6f 65 73 20 74 68 65  st part does the
7c1f0 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 57 48   start of the WH
7c200 45 52 45 20 6c 6f 6f 70 20 61 6e 64 20 74 68 65  ERE loop and the
7c210 20 73 65 63 6f 6e 64 0a 2a 2a 20 68 61 6c 66 20   second.** half 
7c220 64 6f 65 73 20 74 68 65 20 74 61 69 6c 20 6f 66  does the tail of
7c230 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 2e   the WHERE loop.
7c240 20 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66    An instance of
7c250 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75  .** this structu
7c260 72 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 62  re is returned b
7c270 79 20 74 68 65 20 66 69 72 73 74 20 68 61 6c 66  y the first half
7c280 20 61 6e 64 20 70 61 73 73 65 64 0a 2a 2a 20 69   and passed.** i
7c290 6e 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 68  nto the second h
7c2a0 61 6c 66 20 74 6f 20 67 69 76 65 20 73 6f 6d 65  alf to give some
7c2b0 20 63 6f 6e 74 69 6e 75 69 74 79 2e 0a 2a 2f 0a   continuity..*/.
7c2c0 73 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f  struct WhereInfo
7c2d0 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72   {.  Parse *pPar
7c2e0 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  se;            /
7c2f0 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
7c300 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
7c310 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
7c320 73 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20  st *pTabList;   
7c330 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
7c340 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f  tables in the jo
7c350 69 6e 20 2a 2f 0a 20 20 75 31 36 20 6e 4f 42 53  in */.  u16 nOBS
7c360 61 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  at;             
7c370 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4f    /* Number of O
7c380 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 73 61  RDER BY terms sa
7c390 74 69 73 66 69 65 64 20 62 79 20 69 6e 64 69 63  tisfied by indic
7c3a0 65 73 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72  es */.  u16 wctr
7c3b0 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  lFlags;         
7c3c0 20 20 2f 2a 20 46 6c 61 67 73 20 6f 72 69 67 69    /* Flags origi
7c3d0 6e 61 6c 6c 79 20 70 61 73 73 65 64 20 74 6f 20  nally passed to 
7c3e0 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
7c3f0 6e 28 29 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e  n() */.  u8 okOn
7c400 65 50 61 73 73 3b 20 20 20 20 20 20 20 20 20 20  ePass;          
7c410 20 20 20 2f 2a 20 4f 6b 20 74 6f 20 75 73 65 20     /* Ok to use 
7c420 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74  one-pass algorit
7c430 68 6d 20 66 6f 72 20 55 50 44 41 54 45 2f 44 45  hm for UPDATE/DE
7c440 4c 45 54 45 20 2a 2f 0a 20 20 75 38 20 75 6e 74  LETE */.  u8 unt
7c450 65 73 74 65 64 54 65 72 6d 73 3b 20 20 20 20 20  estedTerms;     
7c460 20 20 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c 20 57      /* Not all W
7c470 48 45 52 45 20 74 65 72 6d 73 20 72 65 73 6f 6c  HERE terms resol
7c480 76 65 64 20 62 79 20 6f 75 74 65 72 20 6c 6f 6f  ved by outer loo
7c490 70 20 2a 2f 0a 20 20 75 38 20 65 44 69 73 74 69  p */.  u8 eDisti
7c4a0 6e 63 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  nct;            
7c4b0 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 57   /* One of the W
7c4c0 48 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 2a 20  HERE_DISTINCT_* 
7c4d0 76 61 6c 75 65 73 20 62 65 6c 6f 77 20 2a 2f 0a  values below */.
7c4e0 20 20 69 6e 74 20 69 54 6f 70 3b 20 20 20 20 20    int iTop;     
7c4f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
7c500 68 65 20 76 65 72 79 20 62 65 67 69 6e 6e 69 6e  he very beginnin
7c510 67 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c  g of the WHERE l
7c520 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  oop */.  int iCo
7c530 6e 74 69 6e 75 65 3b 20 20 20 20 20 20 20 20 20  ntinue;         
7c540 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20     /* Jump here 
7c550 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68  to continue with
7c560 20 6e 65 78 74 20 72 65 63 6f 72 64 20 2a 2f 0a   next record */.
7c570 20 20 69 6e 74 20 69 42 72 65 61 6b 3b 20 20 20    int iBreak;   
7c580 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a              /* J
7c590 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65 61  ump here to brea
7c5a0 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f  k out of the loo
7c5b0 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 65 76 65  p */.  int nLeve
7c5c0 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
7c5d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65   /* Number of ne
7c5e0 73 74 65 64 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73  sted loop */.  s
7c5f0 74 72 75 63 74 20 57 68 65 72 65 43 6c 61 75 73  truct WhereClaus
7c600 65 20 2a 70 57 43 3b 20 20 2f 2a 20 44 65 63 6f  e *pWC;  /* Deco
7c610 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65  mposition of the
7c620 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
7c630 0a 20 20 64 6f 75 62 6c 65 20 73 61 76 65 64 4e  .  double savedN
7c640 51 75 65 72 79 4c 6f 6f 70 3b 20 20 20 2f 2a 20  QueryLoop;   /* 
7c650 70 50 61 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f  pParse->nQueryLo
7c660 6f 70 20 6f 75 74 73 69 64 65 20 74 68 65 20 57  op outside the W
7c670 48 45 52 45 20 6c 6f 6f 70 20 2a 2f 0a 20 20 64  HERE loop */.  d
7c680 6f 75 62 6c 65 20 6e 52 6f 77 4f 75 74 3b 20 20  ouble nRowOut;  
7c690 20 20 20 20 20 20 20 20 20 2f 2a 20 45 73 74 69           /* Esti
7c6a0 6d 61 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20  mated number of 
7c6b0 6f 75 74 70 75 74 20 72 6f 77 73 20 2a 2f 0a 20  output rows */. 
7c6c0 20 57 68 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d   WhereLevel a[1]
7c6d0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  ;          /* In
7c6e0 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
7c6f0 65 61 63 68 20 6e 65 73 74 20 6c 6f 6f 70 20 69  each nest loop i
7c700 6e 20 57 48 45 52 45 20 2a 2f 0a 7d 3b 0a 0a 2f  n WHERE */.};../
7c710 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
7c720 20 66 6f 72 20 57 68 65 72 65 49 6e 66 6f 2e 65   for WhereInfo.e
7c730 44 69 73 74 69 6e 63 74 20 61 6e 64 20 44 69 73  Distinct and Dis
7c740 74 69 6e 63 74 43 74 78 2e 65 54 6e 63 74 54 79  tinctCtx.eTnctTy
7c750 70 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  pe */.#define WH
7c760 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 4e 4f 4f  ERE_DISTINCT_NOO
7c770 50 20 20 20 20 20 20 30 20 20 2f 2a 20 44 49 53  P      0  /* DIS
7c780 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 6e 6f  TINCT keyword no
7c790 74 20 75 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e  t used */.#defin
7c7a0 65 20 57 48 45 52 45 5f 44 49 53 54 49 4e 43 54  e WHERE_DISTINCT
7c7b0 5f 55 4e 49 51 55 45 20 20 20 20 31 20 20 2f 2a  _UNIQUE    1  /*
7c7c0 20 4e 6f 20 64 75 70 6c 69 63 61 74 65 73 20 2a   No duplicates *
7c7d0 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
7c7e0 44 49 53 54 49 4e 43 54 5f 4f 52 44 45 52 45 44  DISTINCT_ORDERED
7c7f0 20 20 20 32 20 20 2f 2a 20 41 6c 6c 20 64 75 70     2  /* All dup
7c800 6c 69 63 61 74 65 73 20 61 72 65 20 61 64 6a 61  licates are adja
7c810 63 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  cent */.#define 
7c820 57 48 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 55  WHERE_DISTINCT_U
7c830 4e 4f 52 44 45 52 45 44 20 33 20 20 2f 2a 20 44  NORDERED 3  /* D
7c840 75 70 6c 69 63 61 74 65 73 20 61 72 65 20 73 63  uplicates are sc
7c850 61 74 74 65 72 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a  attered */../*.*
7c860 2a 20 41 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  * A NameContext 
7c870 64 65 66 69 6e 65 73 20 61 20 63 6f 6e 74 65 78  defines a contex
7c880 74 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65  t in which to re
7c890 73 6f 6c 76 65 20 74 61 62 6c 65 20 61 6e 64 20  solve table and 
7c8a0 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e  column.** names.
7c8b0 20 20 54 68 65 20 63 6f 6e 74 65 78 74 20 63 6f    The context co
7c8c0 6e 73 69 73 74 73 20 6f 66 20 61 20 6c 69 73 74  nsists of a list
7c8d0 20 6f 66 20 74 61 62 6c 65 73 20 28 74 68 65 20   of tables (the 
7c8e0 70 53 72 63 4c 69 73 74 29 20 66 69 65 6c 64 20  pSrcList) field 
7c8f0 61 6e 64 0a 2a 2a 20 61 20 6c 69 73 74 20 6f 66  and.** a list of
7c900 20 6e 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f   named expressio
7c910 6e 20 28 70 45 4c 69 73 74 29 2e 20 20 54 68 65  n (pEList).  The
7c920 20 6e 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f   named expressio
7c930 6e 20 6c 69 73 74 20 6d 61 79 0a 2a 2a 20 62 65  n list may.** be
7c940 20 4e 55 4c 4c 2e 20 20 54 68 65 20 70 53 72 63   NULL.  The pSrc
7c950 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20   corresponds to 
7c960 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
7c970 6f 66 20 61 20 53 45 4c 45 43 54 20 6f 72 0a 2a  of a SELECT or.*
7c980 2a 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 62  * to the table b
7c990 65 69 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e  eing operated on
7c9a0 20 62 79 20 49 4e 53 45 52 54 2c 20 55 50 44 41   by INSERT, UPDA
7c9b0 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 2e 20 20  TE, or DELETE.  
7c9c0 54 68 65 0a 2a 2a 20 70 45 4c 69 73 74 20 63 6f  The.** pEList co
7c9d0 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65  rresponds to the
7c9e0 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61   result set of a
7c9f0 20 53 45 4c 45 43 54 20 61 6e 64 20 69 73 20 4e   SELECT and is N
7ca00 55 4c 4c 20 66 6f 72 0a 2a 2a 20 6f 74 68 65 72  ULL for.** other
7ca10 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a   statements..**.
7ca20 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 73 20  ** NameContexts 
7ca30 63 61 6e 20 62 65 20 6e 65 73 74 65 64 2e 20 20  can be nested.  
7ca40 57 68 65 6e 20 72 65 73 6f 6c 76 69 6e 67 20 6e  When resolving n
7ca50 61 6d 65 73 2c 20 74 68 65 20 69 6e 6e 65 72 2d  ames, the inner-
7ca60 6d 6f 73 74 20 0a 2a 2a 20 63 6f 6e 74 65 78 74  most .** context
7ca70 20 69 73 20 73 65 61 72 63 68 65 64 20 66 69 72   is searched fir
7ca80 73 74 2e 20 20 49 66 20 6e 6f 20 6d 61 74 63 68  st.  If no match
7ca90 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e   is found, the n
7caa0 65 78 74 20 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e  ext outer.** con
7cab0 74 65 78 74 20 69 73 20 63 68 65 63 6b 65 64 2e  text is checked.
7cac0 20 20 49 66 20 74 68 65 72 65 20 69 73 20 73 74    If there is st
7cad0 69 6c 6c 20 6e 6f 20 6d 61 74 63 68 2c 20 74 68  ill no match, th
7cae0 65 20 6e 65 78 74 20 63 6f 6e 74 65 78 74 0a 2a  e next context.*
7caf0 2a 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 54  * is checked.  T
7cb00 68 69 73 20 70 72 6f 63 65 73 73 20 63 6f 6e 74  his process cont
7cb10 69 6e 75 65 73 20 75 6e 74 69 6c 20 65 69 74 68  inues until eith
7cb20 65 72 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f  er a match is fo
7cb30 75 6e 64 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f  und.** or all co
7cb40 6e 74 65 78 74 73 20 61 72 65 20 63 68 65 63 6b  ntexts are check
7cb50 2e 20 20 57 68 65 6e 20 61 20 6d 61 74 63 68 20  .  When a match 
7cb60 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 52  is found, the nR
7cb70 65 66 20 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a 20  ef member of.** 
7cb80 74 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 74  the context cont
7cb90 61 69 6e 69 6e 67 20 74 68 65 20 6d 61 74 63 68  aining the match
7cba0 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e   is incremented.
7cbb0 20 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 75 62   .**.** Each sub
7cbc0 71 75 65 72 79 20 67 65 74 73 20 61 20 6e 65 77  query gets a new
7cbd0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 20 20 54   NameContext.  T
7cbe0 68 65 20 70 4e 65 78 74 20 66 69 65 6c 64 20 70  he pNext field p
7cbf0 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20  oints to the.** 
7cc00 4e 61 6d 65 43 6f 6e 74 65 78 74 20 69 6e 20 74  NameContext in t
7cc10 68 65 20 70 61 72 65 6e 74 20 71 75 65 72 79 2e  he parent query.
7cc20 20 20 54 68 75 73 20 74 68 65 20 70 72 6f 63 65    Thus the proce
7cc30 73 73 20 6f 66 20 73 63 61 6e 6e 69 6e 67 20 74  ss of scanning t
7cc40 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78  he.** NameContex
7cc50 74 20 6c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e  t list correspon
7cc60 64 73 20 74 6f 20 73 65 61 72 63 68 69 6e 67 20  ds to searching 
7cc70 74 68 72 6f 75 67 68 20 73 75 63 63 65 73 73 69  through successi
7cc80 76 65 6c 79 20 6f 75 74 65 72 0a 2a 2a 20 73 75  vely outer.** su
7cc90 62 71 75 65 72 69 65 73 20 6c 6f 6f 6b 69 6e 67  bqueries looking
7cca0 20 66 6f 72 20 61 20 6d 61 74 63 68 2e 0a 2a 2f   for a match..*/
7ccb0 0a 73 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74  .struct NameCont
7ccc0 65 78 74 20 7b 0a 20 20 50 61 72 73 65 20 2a 70  ext {.  Parse *p
7ccd0 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f 2a 20  Parse;       /* 
7cce0 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20  The parser */.  
7ccf0 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73  SrcList *pSrcLis
7cd00 74 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d  t;   /* One or m
7cd10 6f 72 65 20 74 61 62 6c 65 73 20 75 73 65 64 20  ore tables used 
7cd20 74 6f 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73  to resolve names
7cd30 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
7cd40 70 45 4c 69 73 74 3b 20 20 20 20 2f 2a 20 4f 70  pEList;    /* Op
7cd50 74 69 6f 6e 61 6c 20 6c 69 73 74 20 6f 66 20 6e  tional list of n
7cd60 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 73  amed expressions
7cd70 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70   */.  AggInfo *p
7cd80 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e  AggInfo;   /* In
7cd90 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
7cda0 61 67 67 72 65 67 61 74 65 73 20 61 74 20 74 68  aggregates at th
7cdb0 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61  is level */.  Na
7cdc0 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74  meContext *pNext
7cdd0 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72  ;  /* Next outer
7cde0 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20   name context.  
7cdf0 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f  NULL for outermo
7ce00 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66  st */.  int nRef
7ce10 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
7ce20 4e 75 6d 62 65 72 20 6f 66 20 6e 61 6d 65 73 20  Number of names 
7ce30 72 65 73 6f 6c 76 65 64 20 62 79 20 74 68 69 73  resolved by this
7ce40 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e   context */.  in
7ce50 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20  t nErr;         
7ce60 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
7ce70 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72  errors encounter
7ce80 65 64 20 77 68 69 6c 65 20 72 65 73 6f 6c 76 69  ed while resolvi
7ce90 6e 67 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 75 38  ng names */.  u8
7cea0 20 6e 63 46 6c 61 67 73 3b 20 20 20 20 20 20 20   ncFlags;       
7ceb0 20 20 20 2f 2a 20 5a 65 72 6f 20 6f 72 20 6d 6f     /* Zero or mo
7cec0 72 65 20 4e 43 5f 2a 20 66 6c 61 67 73 20 64 65  re NC_* flags de
7ced0 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 7d  fined below */.}
7cee0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  ;../*.** Allowed
7cef0 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20   values for the 
7cf00 4e 61 6d 65 43 6f 6e 74 65 78 74 2c 20 6e 63 46  NameContext, ncF
7cf10 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23  lags field..*/.#
7cf20 64 65 66 69 6e 65 20 4e 43 5f 41 6c 6c 6f 77 41  define NC_AllowA
7cf30 67 67 20 20 30 78 30 31 20 20 20 20 2f 2a 20 41  gg  0x01    /* A
7cf40 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
7cf50 6e 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 68  ns are allowed h
7cf60 65 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4e  ere */.#define N
7cf70 43 5f 48 61 73 41 67 67 20 20 20 20 30 78 30 32  C_HasAgg    0x02
7cf80 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f      /* One or mo
7cf90 72 65 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  re aggregate fun
7cfa0 63 74 69 6f 6e 73 20 73 65 65 6e 20 2a 2f 0a 23  ctions seen */.#
7cfb0 64 65 66 69 6e 65 20 4e 43 5f 49 73 43 68 65 63  define NC_IsChec
7cfc0 6b 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54  k   0x04    /* T
7cfd0 72 75 65 20 69 66 20 72 65 73 6f 6c 76 69 6e 67  rue if resolving
7cfe0 20 6e 61 6d 65 73 20 69 6e 20 61 20 43 48 45 43   names in a CHEC
7cff0 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a  K constraint */.
7d000 23 64 65 66 69 6e 65 20 4e 43 5f 49 6e 41 67 67  #define NC_InAgg
7d010 46 75 6e 63 20 30 78 30 38 20 20 20 20 2f 2a 20  Func 0x08    /* 
7d020 54 72 75 65 20 69 66 20 61 6e 61 6c 79 7a 69 6e  True if analyzin
7d030 67 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 61  g arguments to a
7d040 6e 20 61 67 67 20 66 75 6e 63 20 2a 2f 0a 0a 2f  n agg func */../
7d050 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
7d060 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
7d070 67 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  g structure cont
7d080 61 69 6e 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61  ains all informa
7d090 74 69 6f 6e 0a 2a 2a 20 6e 65 65 64 65 64 20 74  tion.** needed t
7d0a0 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20  o generate code 
7d0b0 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 53 45 4c  for a single SEL
7d0c0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ECT statement..*
7d0d0 2a 0a 2a 2a 20 6e 4c 69 6d 69 74 20 69 73 20 73  *.** nLimit is s
7d0e0 65 74 20 74 6f 20 2d 31 20 69 66 20 74 68 65 72  et to -1 if ther
7d0f0 65 20 69 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c  e is no LIMIT cl
7d100 61 75 73 65 2e 20 20 6e 4f 66 66 73 65 74 20 69  ause.  nOffset i
7d110 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 49  s set to 0..** I
7d120 66 20 74 68 65 72 65 20 69 73 20 61 20 4c 49 4d  f there is a LIM
7d130 49 54 20 63 6c 61 75 73 65 2c 20 74 68 65 20 70  IT clause, the p
7d140 61 72 73 65 72 20 73 65 74 73 20 6e 4c 69 6d 69  arser sets nLimi
7d150 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f  t to the value o
7d160 66 20 74 68 65 0a 2a 2a 20 6c 69 6d 69 74 20 61  f the.** limit a
7d170 6e 64 20 6e 4f 66 66 73 65 74 20 74 6f 20 74 68  nd nOffset to th
7d180 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f  e value of the o
7d190 66 66 73 65 74 20 28 6f 72 20 30 20 69 66 20 74  ffset (or 0 if t
7d1a0 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f  here is not.** o
7d1b0 66 66 73 65 74 29 2e 20 20 42 75 74 20 6c 61 74  ffset).  But lat
7d1c0 65 72 20 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e  er on, nLimit an
7d1d0 64 20 6e 4f 66 66 73 65 74 20 62 65 63 6f 6d 65  d nOffset become
7d1e0 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61   the memory loca
7d1f0 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20  tions.** in the 
7d200 56 44 42 45 20 74 68 61 74 20 72 65 63 6f 72 64  VDBE that record
7d210 20 74 68 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f   the limit and o
7d220 66 66 73 65 74 20 63 6f 75 6e 74 65 72 73 2e 0a  ffset counters..
7d230 2a 2a 0a 2a 2a 20 61 64 64 72 4f 70 65 6e 45 70  **.** addrOpenEp
7d240 68 6d 5b 5d 20 65 6e 74 72 69 65 73 20 63 6f 6e  hm[] entries con
7d250 74 61 69 6e 20 74 68 65 20 61 64 64 72 65 73 73  tain the address
7d260 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d   of OP_OpenEphem
7d270 65 72 61 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a  eral opcodes..**
7d280 20 54 68 65 73 65 20 61 64 64 72 65 73 73 65 73   These addresses
7d290 20 6d 75 73 74 20 62 65 20 73 74 6f 72 65 64 20   must be stored 
7d2a0 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 67  so that we can g
7d2b0 6f 20 62 61 63 6b 20 61 6e 64 20 66 69 6c 6c 20  o back and fill 
7d2c0 69 6e 0a 2a 2a 20 74 68 65 20 50 34 5f 4b 45 59  in.** the P4_KEY
7d2d0 49 4e 46 4f 20 61 6e 64 20 50 32 20 70 61 72 61  INFO and P2 para
7d2e0 6d 65 74 65 72 73 20 6c 61 74 65 72 2e 20 20 4e  meters later.  N
7d2f0 65 69 74 68 65 72 20 74 68 65 20 4b 65 79 49 6e  either the KeyIn
7d300 66 6f 20 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e 75  fo nor.** the nu
7d310 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
7d320 69 6e 20 50 32 20 63 61 6e 20 62 65 20 63 6f 6d  in P2 can be com
7d330 70 75 74 65 64 20 61 74 20 74 68 65 20 73 61 6d  puted at the sam
7d340 65 20 74 69 6d 65 0a 2a 2a 20 61 73 20 74 68 65  e time.** as the
7d350 20 4f 50 5f 4f 70 65 6e 45 70 68 6d 20 69 6e 73   OP_OpenEphm ins
7d360 74 72 75 63 74 69 6f 6e 20 69 73 20 63 6f 64 65  truction is code
7d370 64 20 62 65 63 61 75 73 65 20 6e 6f 74 0a 2a 2a  d because not.**
7d380 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74   enough informat
7d390 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63 6f  ion about the co
7d3a0 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 69 73 20  mpound query is 
7d3b0 6b 6e 6f 77 6e 20 61 74 20 74 68 61 74 20 70 6f  known at that po
7d3c0 69 6e 74 2e 0a 2a 2a 20 54 68 65 20 4b 65 79 49  int..** The KeyI
7d3d0 6e 66 6f 20 66 6f 72 20 61 64 64 72 4f 70 65 6e  nfo for addrOpen
7d3e0 54 72 61 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d 20  Tran[0] and [1] 
7d3f0 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69  contains collati
7d400 6e 67 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20  ng sequences.** 
7d410 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20 73  for the result s
7d420 65 74 2e 20 20 54 68 65 20 4b 65 79 49 6e 66 6f  et.  The KeyInfo
7d430 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 45 70 68   for addrOpenEph
7d440 6d 5b 32 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f  m[2] contains co
7d450 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65  llating.** seque
7d460 6e 63 65 73 20 66 6f 72 20 74 68 65 20 4f 52 44  nces for the ORD
7d470 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2f  ER BY clause..*/
7d480 0a 73 74 72 75 63 74 20 53 65 6c 65 63 74 20 7b  .struct Select {
7d490 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
7d4a0 69 73 74 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  ist;      /* The
7d4b0 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 72   fields of the r
7d4c0 65 73 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6f 70  esult */.  u8 op
7d4d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
7d4e0 20 20 2f 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f    /* One of: TK_
7d4f0 55 4e 49 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f  UNION TK_ALL TK_
7d500 49 4e 54 45 52 53 45 43 54 20 54 4b 5f 45 58 43  INTERSECT TK_EXC
7d510 45 50 54 20 2a 2f 0a 20 20 75 31 36 20 73 65 6c  EPT */.  u16 sel
7d520 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  Flags;          
7d530 2f 2a 20 56 61 72 69 6f 75 73 20 53 46 5f 2a 20  /* Various SF_* 
7d540 76 61 6c 75 65 73 20 2a 2f 0a 20 20 69 6e 74 20  values */.  int 
7d550 69 4c 69 6d 69 74 2c 20 69 4f 66 66 73 65 74 3b  iLimit, iOffset;
7d560 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 67     /* Memory reg
7d570 69 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20 4c  isters holding L
7d580 49 4d 49 54 20 26 20 4f 46 46 53 45 54 20 63 6f  IMIT & OFFSET co
7d590 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20  unters */.  int 
7d5a0 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 33 5d 3b  addrOpenEphm[3];
7d5b0 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e 45 70 68     /* OP_OpenEph
7d5c0 65 6d 20 6f 70 63 6f 64 65 73 20 72 65 6c 61 74  em opcodes relat
7d5d0 65 64 20 74 6f 20 74 68 69 73 20 73 65 6c 65 63  ed to this selec
7d5e0 74 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 6e 53  t */.  double nS
7d5f0 65 6c 65 63 74 52 6f 77 3b 20 20 20 20 20 2f 2a  electRow;     /*
7d600 20 45 73 74 69 6d 61 74 65 64 20 6e 75 6d 62 65   Estimated numbe
7d610 72 20 6f 66 20 72 65 73 75 6c 74 20 72 6f 77 73  r of result rows
7d620 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
7d630 53 72 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Src;         /* 
7d640 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  The FROM clause 
7d650 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
7d660 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  e;          /* T
7d670 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
7d680 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
7d690 47 72 6f 75 70 42 79 3b 20 20 20 20 2f 2a 20 54  GroupBy;    /* T
7d6a0 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  he GROUP BY clau
7d6b0 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48  se */.  Expr *pH
7d6c0 61 76 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f  aving;         /
7d6d0 2a 20 54 68 65 20 48 41 56 49 4e 47 20 63 6c 61  * The HAVING cla
7d6e0 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  use */.  ExprLis
7d6f0 74 20 2a 70 4f 72 64 65 72 42 79 3b 20 20 20 20  t *pOrderBy;    
7d700 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
7d710 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65  clause */.  Sele
7d720 63 74 20 2a 70 50 72 69 6f 72 3b 20 20 20 20 20  ct *pPrior;     
7d730 20 20 20 2f 2a 20 50 72 69 6f 72 20 73 65 6c 65     /* Prior sele
7d740 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64  ct in a compound
7d750 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e   select statemen
7d760 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  t */.  Select *p
7d770 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Next;         /*
7d780 20 4e 65 78 74 20 73 65 6c 65 63 74 20 74 6f 20   Next select to 
7d790 74 68 65 20 6c 65 66 74 20 69 6e 20 61 20 63 6f  the left in a co
7d7a0 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 53 65 6c 65  mpound */.  Sele
7d7b0 63 74 20 2a 70 52 69 67 68 74 6d 6f 73 74 3b 20  ct *pRightmost; 
7d7c0 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74     /* Right-most
7d7d0 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d   select in a com
7d7e0 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 74 61  pound select sta
7d7f0 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72  tement */.  Expr
7d800 20 2a 70 4c 69 6d 69 74 3b 20 20 20 20 20 20 20   *pLimit;       
7d810 20 20 20 2f 2a 20 4c 49 4d 49 54 20 65 78 70 72     /* LIMIT expr
7d820 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61  ession. NULL mea
7d830 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a  ns not used. */.
7d840 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74 3b    Expr *pOffset;
7d850 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 46 46 53           /* OFFS
7d860 45 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e  ET expression. N
7d870 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73  ULL means not us
7d880 65 64 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ed. */.};../*.**
7d890 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20   Allowed values 
7d8a0 66 6f 72 20 53 65 6c 65 63 74 2e 73 65 6c 46 6c  for Select.selFl
7d8b0 61 67 73 2e 20 20 54 68 65 20 22 53 46 22 20 70  ags.  The "SF" p
7d8c0 72 65 66 69 78 20 73 74 61 6e 64 73 20 66 6f 72  refix stands for
7d8d0 0a 2a 2a 20 22 53 65 6c 65 63 74 20 46 6c 61 67  .** "Select Flag
7d8e0 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46  "..*/.#define SF
7d8f0 5f 44 69 73 74 69 6e 63 74 20 20 20 20 20 20 20  _Distinct       
7d900 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 75 74 70   0x0001  /* Outp
7d910 75 74 20 73 68 6f 75 6c 64 20 62 65 20 44 49 53  ut should be DIS
7d920 54 49 4e 43 54 20 2a 2f 0a 23 64 65 66 69 6e 65  TINCT */.#define
7d930 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 20 20 20   SF_Resolved    
7d940 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 49      0x0002  /* I
7d950 64 65 6e 74 69 66 69 65 72 73 20 68 61 76 65 20  dentifiers have 
7d960 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 2a 2f  been resolved */
7d970 0a 23 64 65 66 69 6e 65 20 53 46 5f 41 67 67 72  .#define SF_Aggr
7d980 65 67 61 74 65 20 20 20 20 20 20 20 30 78 30 30  egate       0x00
7d990 30 34 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 20  04  /* Contains 
7d9a0 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
7d9b0 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ons */.#define S
7d9c0 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 20  F_UsesEphemeral 
7d9d0 20 20 30 78 30 30 30 38 20 20 2f 2a 20 55 73 65    0x0008  /* Use
7d9e0 73 20 74 68 65 20 4f 70 65 6e 45 70 68 65 6d 65  s the OpenEpheme
7d9f0 72 61 6c 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 64  ral opcode */.#d
7da00 65 66 69 6e 65 20 53 46 5f 45 78 70 61 6e 64 65  efine SF_Expande
7da10 64 20 20 20 20 20 20 20 20 30 78 30 30 31 30 20  d        0x0010 
7da20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63   /* sqlite3Selec
7da30 74 45 78 70 61 6e 64 28 29 20 63 61 6c 6c 65 64  tExpand() called
7da40 20 6f 6e 20 74 68 69 73 20 2a 2f 0a 23 64 65 66   on this */.#def
7da50 69 6e 65 20 53 46 5f 48 61 73 54 79 70 65 49 6e  ine SF_HasTypeIn
7da60 66 6f 20 20 20 20 20 30 78 30 30 32 30 20 20 2f  fo     0x0020  /
7da70 2a 20 46 52 4f 4d 20 73 75 62 71 75 65 72 69 65  * FROM subquerie
7da80 73 20 68 61 76 65 20 54 61 62 6c 65 20 6d 65 74  s have Table met
7da90 61 64 61 74 61 20 2a 2f 0a 23 64 65 66 69 6e 65  adata */.#define
7daa0 20 53 46 5f 55 73 65 53 6f 72 74 65 72 20 20 20   SF_UseSorter   
7dab0 20 20 20 20 30 78 30 30 34 30 20 20 2f 2a 20 53      0x0040  /* S
7dac0 6f 72 74 20 75 73 69 6e 67 20 61 20 73 6f 72 74  ort using a sort
7dad0 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46  er */.#define SF
7dae0 5f 56 61 6c 75 65 73 20 20 20 20 20 20 20 20 20  _Values         
7daf0 20 30 78 30 30 38 30 20 20 2f 2a 20 53 79 6e 74   0x0080  /* Synt
7db00 68 65 73 69 7a 65 64 20 66 72 6f 6d 20 56 41 4c  hesized from VAL
7db10 55 45 53 20 63 6c 61 75 73 65 20 2a 2f 0a 23 64  UES clause */.#d
7db20 65 66 69 6e 65 20 53 46 5f 4d 61 74 65 72 69 61  efine SF_Materia
7db30 6c 69 7a 65 20 20 20 20 20 30 78 30 31 30 30 20  lize     0x0100 
7db40 20 2f 2a 20 46 6f 72 63 65 20 6d 61 74 65 72 69   /* Force materi
7db50 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 76 69 65  alization of vie
7db60 77 73 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  ws */.../*.** Th
7db70 65 20 72 65 73 75 6c 74 73 20 6f 66 20 61 20 73  e results of a s
7db80 65 6c 65 63 74 20 63 61 6e 20 62 65 20 64 69 73  elect can be dis
7db90 74 72 69 62 75 74 65 64 20 69 6e 20 73 65 76 65  tributed in seve
7dba0 72 61 6c 20 77 61 79 73 2e 20 20 54 68 65 0a 2a  ral ways.  The.*
7dbb0 2a 20 22 53 52 54 22 20 70 72 65 66 69 78 20 6d  * "SRT" prefix m
7dbc0 65 61 6e 73 20 22 53 45 4c 45 43 54 20 52 65 73  eans "SELECT Res
7dbd0 75 6c 74 20 54 79 70 65 22 2e 0a 2a 2f 0a 23 64  ult Type"..*/.#d
7dbe0 65 66 69 6e 65 20 53 52 54 5f 55 6e 69 6f 6e 20  efine SRT_Union 
7dbf0 20 20 20 20 20 20 20 31 20 20 2f 2a 20 53 74 6f         1  /* Sto
7dc00 72 65 20 72 65 73 75 6c 74 20 61 73 20 6b 65 79  re result as key
7dc10 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20 2a 2f  s in an index */
7dc20 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 78 63  .#define SRT_Exc
7dc30 65 70 74 20 20 20 20 20 20 20 32 20 20 2f 2a 20  ept       2  /* 
7dc40 52 65 6d 6f 76 65 20 72 65 73 75 6c 74 20 66 72  Remove result fr
7dc50 6f 6d 20 61 20 55 4e 49 4f 4e 20 69 6e 64 65 78  om a UNION index
7dc60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f   */.#define SRT_
7dc70 45 78 69 73 74 73 20 20 20 20 20 20 20 33 20 20  Exists       3  
7dc80 2f 2a 20 53 74 6f 72 65 20 31 20 69 66 20 74 68  /* Store 1 if th
7dc90 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f 74 20  e result is not 
7dca0 65 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65  empty */.#define
7dcb0 20 53 52 54 5f 44 69 73 63 61 72 64 20 20 20 20   SRT_Discard    
7dcc0 20 20 34 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73    4  /* Do not s
7dcd0 61 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ave the results 
7dce0 61 6e 79 77 68 65 72 65 20 2a 2f 0a 0a 2f 2a 20  anywhere */../* 
7dcf0 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
7dd00 75 73 65 20 69 73 20 69 67 6e 6f 72 65 64 20 66  use is ignored f
7dd10 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62  or all of the ab
7dd20 6f 76 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 49  ove */.#define I
7dd30 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62 79 28  gnorableOrderby(
7dd40 58 29 20 28 28 58 2d 3e 65 44 65 73 74 29 3c 3d  X) ((X->eDest)<=
7dd50 53 52 54 5f 44 69 73 63 61 72 64 29 0a 0a 23 64  SRT_Discard)..#d
7dd60 65 66 69 6e 65 20 53 52 54 5f 4f 75 74 70 75 74  efine SRT_Output
7dd70 20 20 20 20 20 20 20 35 20 20 2f 2a 20 4f 75 74         5  /* Out
7dd80 70 75 74 20 65 61 63 68 20 72 6f 77 20 6f 66 20  put each row of 
7dd90 72 65 73 75 6c 74 20 2a 2f 0a 23 64 65 66 69 6e  result */.#defin
7dda0 65 20 53 52 54 5f 4d 65 6d 20 20 20 20 20 20 20  e SRT_Mem       
7ddb0 20 20 20 36 20 20 2f 2a 20 53 74 6f 72 65 20 72     6  /* Store r
7ddc0 65 73 75 6c 74 20 69 6e 20 61 20 6d 65 6d 6f 72  esult in a memor
7ddd0 79 20 63 65 6c 6c 20 2a 2f 0a 23 64 65 66 69 6e  y cell */.#defin
7dde0 65 20 53 52 54 5f 53 65 74 20 20 20 20 20 20 20  e SRT_Set       
7ddf0 20 20 20 37 20 20 2f 2a 20 53 74 6f 72 65 20 72     7  /* Store r
7de00 65 73 75 6c 74 73 20 61 73 20 6b 65 79 73 20 69  esults as keys i
7de10 6e 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64  n an index */.#d
7de20 65 66 69 6e 65 20 53 52 54 5f 54 61 62 6c 65 20  efine SRT_Table 
7de30 20 20 20 20 20 20 20 38 20 20 2f 2a 20 53 74 6f         8  /* Sto
7de40 72 65 20 72 65 73 75 6c 74 20 61 73 20 64 61 74  re result as dat
7de50 61 20 77 69 74 68 20 61 6e 20 61 75 74 6f 6d 61  a with an automa
7de60 74 69 63 20 72 6f 77 69 64 20 2a 2f 0a 23 64 65  tic rowid */.#de
7de70 66 69 6e 65 20 53 52 54 5f 45 70 68 65 6d 54 61  fine SRT_EphemTa
7de80 62 20 20 20 20 20 39 20 20 2f 2a 20 43 72 65 61  b     9  /* Crea
7de90 74 65 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62  te transient tab
7dea0 20 61 6e 64 20 73 74 6f 72 65 20 6c 69 6b 65 20   and store like 
7deb0 53 52 54 5f 54 61 62 6c 65 20 2a 2f 0a 23 64 65  SRT_Table */.#de
7dec0 66 69 6e 65 20 53 52 54 5f 43 6f 72 6f 75 74 69  fine SRT_Corouti
7ded0 6e 65 20 20 20 31 30 20 20 2f 2a 20 47 65 6e 65  ne   10  /* Gene
7dee0 72 61 74 65 20 61 20 73 69 6e 67 6c 65 20 72 6f  rate a single ro
7def0 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 0a  w of result */..
7df00 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
7df10 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
7df20 20 64 65 73 63 72 69 62 65 73 20 77 68 65 72 65   describes where
7df30 20 74 6f 20 70 75 74 20 6f 66 20 74 68 65 20 72   to put of the r
7df40 65 73 75 6c 74 73 20 6f 66 0a 2a 2a 20 61 20 53  esults of.** a S
7df50 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
7df60 0a 2a 2f 0a 73 74 72 75 63 74 20 53 65 6c 65 63  .*/.struct Selec
7df70 74 44 65 73 74 20 7b 0a 20 20 75 38 20 65 44 65  tDest {.  u8 eDe
7df80 73 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 48  st;         /* H
7df90 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66  ow to dispose of
7dfa0 20 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20 4f   the results.  O
7dfb0 6e 20 6f 66 20 53 52 54 5f 2a 20 61 62 6f 76 65  n of SRT_* above
7dfc0 2e 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 53  . */.  char affS
7dfd0 64 73 74 3b 20 20 20 20 20 2f 2a 20 41 66 66 69  dst;     /* Affi
7dfe0 6e 69 74 79 20 75 73 65 64 20 77 68 65 6e 20 65  nity used when e
7dff0 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f  Dest==SRT_Set */
7e000 0a 20 20 69 6e 74 20 69 53 44 50 61 72 6d 3b 20  .  int iSDParm; 
7e010 20 20 20 20 20 2f 2a 20 41 20 70 61 72 61 6d 65       /* A parame
7e020 74 65 72 20 75 73 65 64 20 62 79 20 74 68 65 20  ter used by the 
7e030 65 44 65 73 74 20 64 69 73 70 6f 73 61 6c 20 6d  eDest disposal m
7e040 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69  ethod */.  int i
7e050 53 64 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  Sdst;        /* 
7e060 42 61 73 65 20 72 65 67 69 73 74 65 72 20 77 68  Base register wh
7e070 65 72 65 20 72 65 73 75 6c 74 73 20 61 72 65 20  ere results are 
7e080 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74  written */.  int
7e090 20 6e 53 64 73 74 3b 20 20 20 20 20 20 20 20 2f   nSdst;        /
7e0a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69  * Number of regi
7e0b0 73 74 65 72 73 20 61 6c 6c 6f 63 61 74 65 64 20  sters allocated 
7e0c0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 75 72  */.};../*.** Dur
7e0d0 69 6e 67 20 63 6f 64 65 20 67 65 6e 65 72 61 74  ing code generat
7e0e0 69 6f 6e 20 6f 66 20 73 74 61 74 65 6d 65 6e 74  ion of statement
7e0f0 73 20 74 68 61 74 20 64 6f 20 69 6e 73 65 72 74  s that do insert
7e100 73 20 69 6e 74 6f 20 41 55 54 4f 49 4e 43 52 45  s into AUTOINCRE
7e110 4d 45 4e 54 20 0a 2a 2a 20 74 61 62 6c 65 73 2c  MENT .** tables,
7e120 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   the following i
7e130 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 74  nformation is at
7e140 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 54 61  tached to the Ta
7e150 62 6c 65 2e 75 2e 61 75 74 6f 49 6e 63 2e 70 0a  ble.u.autoInc.p.
7e160 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66 20 65 61  ** pointer of ea
7e170 63 68 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  ch autoincrement
7e180 20 74 61 62 6c 65 20 74 6f 20 72 65 63 6f 72 64   table to record
7e190 20 73 6f 6d 65 20 73 69 64 65 20 69 6e 66 6f 72   some side infor
7e1a0 6d 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74  mation that.** t
7e1b0 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f  he code generato
7e1c0 72 20 6e 65 65 64 73 2e 20 20 57 65 20 68 61 76  r needs.  We hav
7e1d0 65 20 74 6f 20 6b 65 65 70 20 70 65 72 2d 74 61  e to keep per-ta
7e1e0 62 6c 65 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e  ble autoincremen
7e1f0 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  t.** information
7e200 20 69 6e 20 63 61 73 65 20 69 6e 73 65 72 74 73   in case inserts
7e210 20 61 72 65 20 64 6f 77 6e 20 77 69 74 68 69 6e   are down within
7e220 20 74 72 69 67 67 65 72 73 2e 20 20 54 72 69 67   triggers.  Trig
7e230 67 65 72 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e  gers do not.** n
7e240 6f 72 6d 61 6c 6c 79 20 63 6f 6f 72 64 69 6e 61  ormally coordina
7e250 74 65 20 74 68 65 69 72 20 61 63 74 69 76 69 74  te their activit
7e260 69 65 73 2c 20 62 75 74 20 77 65 20 64 6f 20 6e  ies, but we do n
7e270 65 65 64 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74  eed to coordinat
7e280 65 20 74 68 65 0a 2a 2a 20 6c 6f 61 64 69 6e 67  e the.** loading
7e290 20 61 6e 64 20 73 61 76 69 6e 67 20 6f 66 20 61   and saving of a
7e2a0 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 69 6e 66  utoincrement inf
7e2b0 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72  ormation..*/.str
7e2c0 75 63 74 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20  uct AutoincInfo 
7e2d0 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20  {.  AutoincInfo 
7e2e0 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78  *pNext;   /* Nex
7e2f0 74 20 69 6e 66 6f 20 62 6c 6f 63 6b 20 69 6e 20  t info block in 
7e300 61 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61  a list of them a
7e310 6c 6c 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  ll */.  Table *p
7e320 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Tab;          /*
7e330 20 54 61 62 6c 65 20 74 68 69 73 20 69 6e 66 6f   Table this info
7e340 20 62 6c 6f 63 6b 20 72 65 66 65 72 73 20 74 6f   block refers to
7e350 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
7e360 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
7e370 6e 64 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e  ndex in sqlite3.
7e380 61 44 62 5b 5d 20 6f 66 20 64 61 74 61 62 61 73  aDb[] of databas
7e390 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a  e holding pTab *
7e3a0 2f 0a 20 20 69 6e 74 20 72 65 67 43 74 72 3b 20  /.  int regCtr; 
7e3b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
7e3c0 6f 72 79 20 72 65 67 69 73 74 65 72 20 68 6f 6c  ory register hol
7e3d0 64 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 63  ding the rowid c
7e3e0 6f 75 6e 74 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  ounter */.};../*
7e3f0 0a 2a 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20  .** Size of the 
7e400 63 6f 6c 75 6d 6e 20 63 61 63 68 65 0a 2a 2f 0a  column cache.*/.
7e410 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e  #ifndef SQLITE_N
7e420 5f 43 4f 4c 43 41 43 48 45 0a 23 20 64 65 66 69  _COLCACHE.# defi
7e430 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43  ne SQLITE_N_COLC
7e440 41 43 48 45 20 31 30 0a 23 65 6e 64 69 66 0a 0a  ACHE 10.#endif..
7e450 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 6f  /*.** At least o
7e460 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ne instance of t
7e470 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
7e480 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65  ucture is create
7e490 64 20 66 6f 72 20 65 61 63 68 20 0a 2a 2a 20 74  d for each .** t
7e4a0 72 69 67 67 65 72 20 74 68 61 74 20 6d 61 79 20  rigger that may 
7e4b0 62 65 20 66 69 72 65 64 20 77 68 69 6c 65 20 70  be fired while p
7e4c0 61 72 73 69 6e 67 20 61 6e 20 49 4e 53 45 52 54  arsing an INSERT
7e4d0 2c 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45  , UPDATE or DELE
7e4e0 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e  TE.** statement.
7e4f0 20 41 6c 6c 20 73 75 63 68 20 6f 62 6a 65 63 74   All such object
7e500 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  s are stored in 
7e510 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  the linked list 
7e520 68 65 61 64 65 64 20 61 74 0a 2a 2a 20 50 61 72  headed at.** Par
7e530 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 61  se.pTriggerPrg a
7e540 6e 64 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20  nd deleted once 
7e550 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c  statement compil
7e560 61 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a  ation has been.*
7e570 2a 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a  * completed..**.
7e580 2a 2a 20 41 20 56 64 62 65 20 73 75 62 2d 70 72  ** A Vdbe sub-pr
7e590 6f 67 72 61 6d 20 74 68 61 74 20 69 6d 70 6c 65  ogram that imple
7e5a0 6d 65 6e 74 73 20 74 68 65 20 62 6f 64 79 20 61  ments the body a
7e5b0 6e 64 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f  nd WHEN clause o
7e5c0 66 20 74 72 69 67 67 65 72 0a 2a 2a 20 54 72 69  f trigger.** Tri
7e5d0 67 67 65 72 50 72 67 2e 70 54 72 69 67 67 65 72  ggerPrg.pTrigger
7e5e0 2c 20 61 73 73 75 6d 69 6e 67 20 61 20 64 65 66  , assuming a def
7e5f0 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54  ault ON CONFLICT
7e600 20 63 6c 61 75 73 65 20 6f 66 0a 2a 2a 20 54 72   clause of.** Tr
7e610 69 67 67 65 72 50 72 67 2e 6f 72 63 6f 6e 66 2c  iggerPrg.orconf,
7e620 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68   is stored in th
7e630 65 20 54 72 69 67 67 65 72 50 72 67 2e 70 50 72  e TriggerPrg.pPr
7e640 6f 67 72 61 6d 20 76 61 72 69 61 62 6c 65 2e 0a  ogram variable..
7e650 2a 2a 20 54 68 65 20 50 61 72 73 65 2e 70 54 72  ** The Parse.pTr
7e660 69 67 67 65 72 50 72 67 20 6c 69 73 74 20 6e 65  iggerPrg list ne
7e670 76 65 72 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f  ver contains two
7e680 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68   entries with th
7e690 65 20 73 61 6d 65 0a 2a 2a 20 76 61 6c 75 65 73  e same.** values
7e6a0 20 66 6f 72 20 62 6f 74 68 20 70 54 72 69 67 67   for both pTrigg
7e6b0 65 72 20 61 6e 64 20 6f 72 63 6f 6e 66 2e 0a 2a  er and orconf..*
7e6c0 2a 0a 2a 2a 20 54 68 65 20 54 72 69 67 67 65 72  *.** The Trigger
7e6d0 50 72 67 2e 61 43 6f 6c 6d 61 73 6b 5b 30 5d 20  Prg.aColmask[0] 
7e6e0 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20  variable is set 
7e6f0 74 6f 20 61 20 6d 61 73 6b 20 6f 66 20 6f 6c 64  to a mask of old
7e700 2e 2a 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 61 63  .* columns.** ac
7e710 63 65 73 73 65 64 20 28 6f 72 20 73 65 74 20 74  cessed (or set t
7e720 6f 20 30 20 66 6f 72 20 74 72 69 67 67 65 72 73  o 0 for triggers
7e730 20 66 69 72 65 64 20 61 73 20 61 20 72 65 73 75   fired as a resu
7e740 6c 74 20 6f 66 20 49 4e 53 45 52 54 20 0a 2a 2a  lt of INSERT .**
7e750 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 20 53 69   statements). Si
7e760 6d 69 6c 61 72 6c 79 2c 20 74 68 65 20 54 72 69  milarly, the Tri
7e770 67 67 65 72 50 72 67 2e 61 43 6f 6c 6d 61 73 6b  ggerPrg.aColmask
7e780 5b 31 5d 20 76 61 72 69 61 62 6c 65 20 69 73 20  [1] variable is 
7e790 73 65 74 20 74 6f 0a 2a 2a 20 61 20 6d 61 73 6b  set to.** a mask
7e7a0 20 6f 66 20 6e 65 77 2e 2a 20 63 6f 6c 75 6d 6e   of new.* column
7e7b0 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 72  s used by the pr
7e7c0 6f 67 72 61 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74  ogram..*/.struct
7e7d0 20 54 72 69 67 67 65 72 50 72 67 20 7b 0a 20 20   TriggerPrg {.  
7e7e0 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
7e7f0 72 3b 20 20 20 20 20 20 2f 2a 20 54 72 69 67 67  r;      /* Trigg
7e800 65 72 20 74 68 69 73 20 70 72 6f 67 72 61 6d 20  er this program 
7e810 77 61 73 20 63 6f 64 65 64 20 66 72 6f 6d 20 2a  was coded from *
7e820 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a  /.  TriggerPrg *
7e830 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e  pNext;      /* N
7e840 65 78 74 20 65 6e 74 72 79 20 69 6e 20 50 61 72  ext entry in Par
7e850 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 6c  se.pTriggerPrg l
7e860 69 73 74 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67  ist */.  SubProg
7e870 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20  ram *pProgram;  
7e880 20 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6d 70 6c   /* Program impl
7e890 65 6d 65 6e 74 69 6e 67 20 70 54 72 69 67 67 65  ementing pTrigge
7e8a0 72 2f 6f 72 63 6f 6e 66 20 2a 2f 0a 20 20 69 6e  r/orconf */.  in
7e8b0 74 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20  t orconf;       
7e8c0 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
7e8d0 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c   ON CONFLICT pol
7e8e0 69 63 79 20 2a 2f 0a 20 20 75 33 32 20 61 43 6f  icy */.  u32 aCo
7e8f0 6c 6d 61 73 6b 5b 32 5d 3b 20 20 20 20 20 20 20  lmask[2];       
7e900 20 2f 2a 20 4d 61 73 6b 73 20 6f 66 20 6f 6c 64   /* Masks of old
7e910 2e 2a 2c 20 6e 65 77 2e 2a 20 63 6f 6c 75 6d 6e  .*, new.* column
7e920 73 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 7d 3b  s accessed */.};
7e930 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 79 44 62 4d  ../*.** The yDbM
7e940 61 73 6b 20 64 61 74 61 74 79 70 65 20 66 6f 72  ask datatype for
7e950 20 74 68 65 20 62 69 74 6d 61 73 6b 20 6f 66 20   the bitmask of 
7e960 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74  all attached dat
7e970 61 62 61 73 65 73 2e 0a 2a 2f 0a 23 69 66 20 53  abases..*/.#if S
7e980 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
7e990 45 44 3e 33 30 0a 20 20 74 79 70 65 64 65 66 20  ED>30.  typedef 
7e9a0 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 79  sqlite3_uint64 y
7e9b0 44 62 4d 61 73 6b 3b 0a 23 65 6c 73 65 0a 20 20  DbMask;.#else.  
7e9c0 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64  typedef unsigned
7e9d0 20 69 6e 74 20 79 44 62 4d 61 73 6b 3b 0a 23 65   int yDbMask;.#e
7e9e0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53  ndif../*.** An S
7e9f0 51 4c 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78  QL parser contex
7ea00 74 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 74 68  t.  A copy of th
7ea10 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
7ea20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a 2a  passed through.*
7ea30 2a 20 74 68 65 20 70 61 72 73 65 72 20 61 6e 64  * the parser and
7ea40 20 64 6f 77 6e 20 69 6e 74 6f 20 61 6c 6c 20 74   down into all t
7ea50 68 65 20 70 61 72 73 65 72 20 61 63 74 69 6f 6e  he parser action
7ea60 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65   routine in orde
7ea70 72 20 74 6f 0a 2a 2a 20 63 61 72 72 79 20 61 72  r to.** carry ar
7ea80 6f 75 6e 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ound information
7ea90 20 74 68 61 74 20 69 73 20 67 6c 6f 62 61 6c 20   that is global 
7eaa0 74 6f 20 74 68 65 20 65 6e 74 69 72 65 20 70 61  to the entire pa
7eab0 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  rse..**.** The s
7eac0 74 72 75 63 74 75 72 65 20 69 73 20 64 69 76 69  tructure is divi
7ead0 64 65 64 20 69 6e 74 6f 20 74 77 6f 20 70 61 72  ded into two par
7eae0 74 73 2e 20 20 57 68 65 6e 20 74 68 65 20 70 61  ts.  When the pa
7eaf0 72 73 65 72 20 61 6e 64 20 63 6f 64 65 0a 2a 2a  rser and code.**
7eb00 20 67 65 6e 65 72 61 74 65 20 63 61 6c 6c 20 74   generate call t
7eb10 68 65 6d 73 65 6c 76 65 73 20 72 65 63 75 72 73  hemselves recurs
7eb20 69 76 65 6c 79 2c 20 74 68 65 20 66 69 72 73 74  ively, the first
7eb30 20 70 61 72 74 20 6f 66 20 74 68 65 20 73 74 72   part of the str
7eb40 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 63 6f 6e  ucture.** is con
7eb50 73 74 61 6e 74 20 62 75 74 20 74 68 65 20 73 65  stant but the se
7eb60 63 6f 6e 64 20 70 61 72 74 20 69 73 20 72 65 73  cond part is res
7eb70 65 74 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  et at the beginn
7eb80 69 6e 67 20 61 6e 64 20 65 6e 64 20 6f 66 0a 2a  ing and end of.*
7eb90 2a 20 65 61 63 68 20 72 65 63 75 72 73 69 6f 6e  * each recursion
7eba0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 54 61 62  ..**.** The nTab
7ebb0 6c 65 4c 6f 63 6b 20 61 6e 64 20 61 54 61 62 6c  leLock and aTabl
7ebc0 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65 73 20  eLock variables 
7ebd0 61 72 65 20 6f 6e 6c 79 20 75 73 65 64 20 69 66  are only used if
7ebe0 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68   the shared-cach
7ebf0 65 20 0a 2a 2a 20 66 65 61 74 75 72 65 20 69 73  e .** feature is
7ec00 20 65 6e 61 62 6c 65 64 20 28 69 66 20 73 71 6c   enabled (if sql
7ec10 69 74 65 33 54 73 64 28 29 2d 3e 75 73 65 53 68  ite3Tsd()->useSh
7ec20 61 72 65 64 44 61 74 61 20 69 73 20 74 72 75 65  aredData is true
7ec30 29 2e 20 54 68 65 79 20 61 72 65 0a 2a 2a 20 75  ). They are.** u
7ec40 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  sed to store the
7ec50 20 73 65 74 20 6f 66 20 74 61 62 6c 65 2d 6c 6f   set of table-lo
7ec60 63 6b 73 20 72 65 71 75 69 72 65 64 20 62 79 20  cks required by 
7ec70 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 62 65  the statement be
7ec80 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2e  ing.** compiled.
7ec90 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65   Function sqlite
7eca0 33 54 61 62 6c 65 4c 6f 63 6b 28 29 20 69 73 20  3TableLock() is 
7ecb0 75 73 65 64 20 74 6f 20 61 64 64 20 65 6e 74 72  used to add entr
7ecc0 69 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 6c 69  ies to the.** li
7ecd0 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61  st..*/.struct Pa
7ece0 72 73 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20  rse {.  sqlite3 
7ecf0 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  *db;         /* 
7ed00 54 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  The main databas
7ed10 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
7ed20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20   char *zErrMsg; 
7ed30 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f        /* An erro
7ed40 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 56  r message */.  V
7ed50 64 62 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20  dbe *pVdbe;     
7ed60 20 20 20 20 2f 2a 20 41 6e 20 65 6e 67 69 6e 65      /* An engine
7ed70 20 66 6f 72 20 65 78 65 63 75 74 69 6e 67 20 64   for executing d
7ed80 61 74 61 62 61 73 65 20 62 79 74 65 63 6f 64 65  atabase bytecode
7ed90 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
7eda0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
7edb0 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65  turn code from e
7edc0 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 75 38  xecution */.  u8
7edd0 20 63 6f 6c 4e 61 6d 65 73 53 65 74 3b 20 20 20   colNamesSet;   
7ede0 20 20 20 2f 2a 20 54 52 55 45 20 61 66 74 65 72     /* TRUE after
7edf0 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61 6d 65 20 68   OP_ColumnName h
7ee00 61 73 20 62 65 65 6e 20 69 73 73 75 65 64 20 74  as been issued t
7ee10 6f 20 70 56 64 62 65 20 2a 2f 0a 20 20 75 38 20  o pVdbe */.  u8 
7ee20 63 68 65 63 6b 53 63 68 65 6d 61 3b 20 20 20 20  checkSchema;    
7ee30 20 20 2f 2a 20 43 61 75 73 65 73 20 73 63 68 65    /* Causes sche
7ee40 6d 61 20 63 6f 6f 6b 69 65 20 63 68 65 63 6b 20  ma cookie check 
7ee50 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 2a  after an error *
7ee60 2f 0a 20 20 75 38 20 6e 65 73 74 65 64 3b 20 20  /.  u8 nested;  
7ee70 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
7ee80 65 72 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c  er of nested cal
7ee90 6c 73 20 74 6f 20 74 68 65 20 70 61 72 73 65 72  ls to the parser
7eea0 2f 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20  /code generator 
7eeb0 2a 2f 0a 20 20 75 38 20 6e 54 65 6d 70 52 65 67  */.  u8 nTempReg
7eec0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
7eed0 62 65 72 20 6f 66 20 74 65 6d 70 6f 72 61 72 79  ber of temporary
7eee0 20 72 65 67 69 73 74 65 72 73 20 69 6e 20 61 54   registers in aT
7eef0 65 6d 70 52 65 67 5b 5d 20 2a 2f 0a 20 20 75 38  empReg[] */.  u8
7ef00 20 6e 54 65 6d 70 49 6e 55 73 65 3b 20 20 20 20   nTempInUse;    
7ef10 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
7ef20 61 54 65 6d 70 52 65 67 5b 5d 20 63 75 72 72 65  aTempReg[] curre
7ef30 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74  ntly checked out
7ef40 20 2a 2f 0a 20 20 75 38 20 6e 43 6f 6c 43 61 63   */.  u8 nColCac
7ef50 68 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  he;        /* Nu
7ef60 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
7ef70 69 6e 20 61 43 6f 6c 43 61 63 68 65 5b 5d 20 2a  in aColCache[] *
7ef80 2f 0a 20 20 75 38 20 69 43 6f 6c 43 61 63 68 65  /.  u8 iColCache
7ef90 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74  ;        /* Next
7efa0 20 65 6e 74 72 79 20 69 6e 20 61 43 6f 6c 43 61   entry in aColCa
7efb0 63 68 65 5b 5d 20 74 6f 20 72 65 70 6c 61 63 65  che[] to replace
7efc0 20 2a 2f 0a 20 20 75 38 20 69 73 4d 75 6c 74 69   */.  u8 isMulti
7efd0 57 72 69 74 65 3b 20 20 20 20 20 2f 2a 20 54 72  Write;     /* Tr
7efe0 75 65 20 69 66 20 73 74 61 74 65 6d 65 6e 74 20  ue if statement 
7eff0 6d 61 79 20 6d 6f 64 69 66 79 2f 69 6e 73 65 72  may modify/inser
7f000 74 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 20  t multiple rows 
7f010 2a 2f 0a 20 20 75 38 20 6d 61 79 41 62 6f 72 74  */.  u8 mayAbort
7f020 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
7f030 65 20 69 66 20 73 74 61 74 65 6d 65 6e 74 20 6d  e if statement m
7f040 61 79 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52  ay throw an ABOR
7f050 54 20 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20  T exception */. 
7f060 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38 5d   int aTempReg[8]
7f070 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67  ;     /* Holding
7f080 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f 72   area for tempor
7f090 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a 2f  ary registers */
7f0a0 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65 67  .  int nRangeReg
7f0b0 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20  ;       /* Size 
7f0c0 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  of the temporary
7f0d0 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b 20   register block 
7f0e0 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65 52  */.  int iRangeR
7f0f0 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72  eg;       /* Fir
7f100 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 74  st register in t
7f110 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65  emporary registe
7f120 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74  r block */.  int
7f130 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 20   nErr;          
7f140 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
7f150 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20 20  rrors seen */.  
7f160 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20 20  int nTab;       
7f170 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
7f180 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c  f previously all
7f190 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72 73  ocated VDBE curs
7f1a0 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65  ors */.  int nMe
7f1b0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  m;            /*
7f1c0 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72   Number of memor
7f1d0 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f 20  y cells used so 
7f1e0 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65  far */.  int nSe
7f1f0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
7f200 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73 20   Number of sets 
7f210 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20  used so far */. 
7f220 20 69 6e 74 20 6e 4f 6e 63 65 3b 20 20 20 20 20   int nOnce;     
7f230 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
7f240 6f 66 20 4f 50 5f 4f 6e 63 65 20 69 6e 73 74 72  of OP_Once instr
7f250 75 63 74 69 6f 6e 73 20 73 6f 20 66 61 72 20 2a  uctions so far *
7f260 2f 0a 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20  /.  int ckBase; 
7f270 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65           /* Base
7f280 20 72 65 67 69 73 74 65 72 20 6f 66 20 64 61 74   register of dat
7f290 61 20 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63  a during check c
7f2a0 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
7f2b0 69 6e 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b  int iCacheLevel;
7f2c0 20 20 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65       /* ColCache
7f2d0 20 76 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c   valid when aCol
7f2e0 43 61 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d  Cache[].iLevel<=
7f2f0 69 43 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20  iCacheLevel */. 
7f300 20 69 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20   int iCacheCnt; 
7f310 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72        /* Counter
7f320 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74   used to generat
7f330 65 20 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72  e aColCache[].lr
7f340 75 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 73 74  u values */.  st
7f350 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 7b  ruct yColCache {
7f360 0a 20 20 20 20 69 6e 74 20 69 54 61 62 6c 65 3b  .    int iTable;
7f370 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
7f380 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ble cursor numbe
7f390 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f  r */.    int iCo
7f3a0 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f  lumn;          /
7f3b0 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 6e  * Table column n
7f3c0 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 75 38 20  umber */.    u8 
7f3d0 74 65 6d 70 52 65 67 3b 20 20 20 20 20 20 20 20  tempReg;        
7f3e0 20 20 20 2f 2a 20 69 52 65 67 20 69 73 20 61 20     /* iReg is a 
7f3f0 74 65 6d 70 20 72 65 67 69 73 74 65 72 20 74 68  temp register th
7f400 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66  at needs to be f
7f410 72 65 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  reed */.    int 
7f420 69 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20  iLevel;         
7f430 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 6c 65 76    /* Nesting lev
7f440 65 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 52  el */.    int iR
7f450 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  eg;             
7f460 2f 2a 20 52 65 67 20 77 69 74 68 20 76 61 6c 75  /* Reg with valu
7f470 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e  e of this column
7f480 2e 20 30 20 6d 65 61 6e 73 20 6e 6f 6e 65 2e 20  . 0 means none. 
7f490 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 75 3b 20  */.    int lru; 
7f4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
7f4b0 4c 65 61 73 74 20 72 65 63 65 6e 74 6c 79 20 75  Least recently u
7f4c0 73 65 64 20 65 6e 74 72 79 20 68 61 73 20 74 68  sed entry has th
7f4d0 65 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c 75 65  e smallest value
7f4e0 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43 61 63 68   */.  } aColCach
7f4f0 65 5b 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41  e[SQLITE_N_COLCA
7f500 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e 65 20 66 6f  CHE];  /* One fo
7f510 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 63 61  r each column ca
7f520 63 68 65 20 65 6e 74 72 79 20 2a 2f 0a 20 20 79  che entry */.  y
7f530 44 62 4d 61 73 6b 20 77 72 69 74 65 4d 61 73 6b  DbMask writeMask
7f540 3b 20 20 20 2f 2a 20 53 74 61 72 74 20 61 20 77  ;   /* Start a w
7f550 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
7f560 20 6f 6e 20 74 68 65 73 65 20 64 61 74 61 62 61   on these databa
7f570 73 65 73 20 2a 2f 0a 20 20 79 44 62 4d 61 73 6b  ses */.  yDbMask
7f580 20 63 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 2f 2a   cookieMask;  /*
7f590 20 42 69 74 6d 61 73 6b 20 6f 66 20 73 63 68 65   Bitmask of sche
7f5a0 6d 61 20 76 65 72 69 66 69 65 64 20 64 61 74 61  ma verified data
7f5b0 62 61 73 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63  bases */.  int c
7f5c0 6f 6f 6b 69 65 47 6f 74 6f 3b 20 20 20 20 20 20  ookieGoto;      
7f5d0 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50  /* Address of OP
7f5e0 5f 47 6f 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20  _Goto to cookie 
7f5f0 76 65 72 69 66 69 65 72 20 73 75 62 72 6f 75 74  verifier subrout
7f600 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f  ine */.  int coo
7f610 6b 69 65 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f  kieValue[SQLITE_
7f620 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b  MAX_ATTACHED+2];
7f630 20 20 2f 2a 20 56 61 6c 75 65 73 20 6f 66 20 63    /* Values of c
7f640 6f 6f 6b 69 65 73 20 74 6f 20 76 65 72 69 66 79  ookies to verify
7f650 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77   */.  int regRow
7f660 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65  id;        /* Re
7f670 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72  gister holding r
7f680 6f 77 69 64 20 6f 66 20 43 52 45 41 54 45 20 54  owid of CREATE T
7f690 41 42 4c 45 20 65 6e 74 72 79 20 2a 2f 0a 20 20  ABLE entry */.  
7f6a0 69 6e 74 20 72 65 67 52 6f 6f 74 3b 20 20 20 20  int regRoot;    
7f6b0 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
7f6c0 20 68 6f 6c 64 69 6e 67 20 72 6f 6f 74 20 70 61   holding root pa
7f6d0 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 6e 65  ge number for ne
7f6e0 77 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 69  w objects */.  i
7f6f0 6e 74 20 6e 4d 61 78 41 72 67 3b 20 20 20 20 20  nt nMaxArg;     
7f700 20 20 20 20 2f 2a 20 4d 61 78 20 61 72 67 73 20      /* Max args 
7f710 70 61 73 73 65 64 20 74 6f 20 75 73 65 72 20 66  passed to user f
7f720 75 6e 63 74 69 6f 6e 20 62 79 20 73 75 62 2d 70  unction by sub-p
7f730 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 54 6f 6b 65  rogram */.  Toke
7f740 6e 20 63 6f 6e 73 74 72 61 69 6e 74 4e 61 6d 65  n constraintName
7f750 3b 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20  ;/* Name of the 
7f760 63 6f 6e 73 74 72 61 69 6e 74 20 63 75 72 72 65  constraint curre
7f770 6e 74 6c 79 20 62 65 69 6e 67 20 70 61 72 73 65  ntly being parse
7f780 64 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  d */.#ifndef SQL
7f790 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
7f7a0 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 54 61 62  CACHE.  int nTab
7f7b0 6c 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f  leLock;        /
7f7c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b  * Number of lock
7f7d0 73 20 69 6e 20 61 54 61 62 6c 65 4c 6f 63 6b 20  s in aTableLock 
7f7e0 2a 2f 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a  */.  TableLock *
7f7f0 61 54 61 62 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52  aTableLock; /* R
7f800 65 71 75 69 72 65 64 20 74 61 62 6c 65 20 6c 6f  equired table lo
7f810 63 6b 73 20 66 6f 72 20 73 68 61 72 65 64 2d 63  cks for shared-c
7f820 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e  ache mode */.#en
7f830 64 69 66 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66  dif.  AutoincInf
7f840 6f 20 2a 70 41 69 6e 63 3b 20 20 2f 2a 20 49 6e  o *pAinc;  /* In
7f850 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
7f860 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 63 6f  AUTOINCREMENT co
7f870 75 6e 74 65 72 73 20 2a 2f 0a 0a 20 20 2f 2a 20  unters */..  /* 
7f880 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64  Information used
7f890 20 77 68 69 6c 65 20 63 6f 64 69 6e 67 20 74 72   while coding tr
7f8a0 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 20  igger programs. 
7f8b0 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70  */.  Parse *pTop
7f8c0 6c 65 76 65 6c 3b 20 20 20 20 2f 2a 20 50 61 72  level;    /* Par
7f8d0 73 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72  se structure for
7f8e0 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20 28 6f   main program (o
7f8f0 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 54 61 62  r NULL) */.  Tab
7f900 6c 65 20 2a 70 54 72 69 67 67 65 72 54 61 62 3b  le *pTriggerTab;
7f910 20 20 2f 2a 20 54 61 62 6c 65 20 74 72 69 67 67    /* Table trigg
7f920 65 72 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f  ers are being co
7f930 64 65 64 20 66 6f 72 20 2a 2f 0a 20 20 64 6f 75  ded for */.  dou
7f940 62 6c 65 20 6e 51 75 65 72 79 4c 6f 6f 70 3b 20  ble nQueryLoop; 
7f950 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 6e    /* Estimated n
7f960 75 6d 62 65 72 20 6f 66 20 69 74 65 72 61 74 69  umber of iterati
7f970 6f 6e 73 20 6f 66 20 61 20 71 75 65 72 79 20 2a  ons of a query *
7f980 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61 73 6b 3b  /.  u32 oldmask;
7f990 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
7f9a0 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e   of old.* column
7f9b0 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a  s referenced */.
7f9c0 20 20 75 33 32 20 6e 65 77 6d 61 73 6b 3b 20 20    u32 newmask;  
7f9d0 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
7f9e0 66 20 6e 65 77 2e 2a 20 63 6f 6c 75 6d 6e 73 20  f new.* columns 
7f9f0 72 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20  referenced */.  
7fa00 75 38 20 65 54 72 69 67 67 65 72 4f 70 3b 20 20  u8 eTriggerOp;  
7fa10 20 20 20 20 20 2f 2a 20 54 4b 5f 55 50 44 41 54       /* TK_UPDAT
7fa20 45 2c 20 54 4b 5f 49 4e 53 45 52 54 20 6f 72 20  E, TK_INSERT or 
7fa30 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75  TK_DELETE */.  u
7fa40 38 20 65 4f 72 63 6f 6e 66 3b 20 20 20 20 20 20  8 eOrconf;      
7fa50 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f      /* Default O
7fa60 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63  N CONFLICT polic
7fa70 79 20 66 6f 72 20 74 72 69 67 67 65 72 20 73 74  y for trigger st
7fa80 65 70 73 20 2a 2f 0a 20 20 75 38 20 64 69 73 61  eps */.  u8 disa
7fa90 62 6c 65 54 72 69 67 67 65 72 73 3b 20 20 2f 2a  bleTriggers;  /*
7faa0 20 54 72 75 65 20 74 6f 20 64 69 73 61 62 6c 65   True to disable
7fab0 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 0a 20 20   triggers */..  
7fac0 2f 2a 20 41 62 6f 76 65 20 69 73 20 63 6f 6e 73  /* Above is cons
7fad0 74 61 6e 74 20 62 65 74 77 65 65 6e 20 72 65 63  tant between rec
7fae0 75 72 73 69 6f 6e 73 2e 20 20 42 65 6c 6f 77 20  ursions.  Below 
7faf0 69 73 20 72 65 73 65 74 20 62 65 66 6f 72 65 20  is reset before 
7fb00 61 6e 64 20 61 66 74 65 72 0a 20 20 2a 2a 20 65  and after.  ** e
7fb10 61 63 68 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f  ach recursion */
7fb20 0a 0a 20 20 69 6e 74 20 6e 56 61 72 3b 20 20 20  ..  int nVar;   
7fb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
7fb40 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27 20 76   Number of '?' v
7fb50 61 72 69 61 62 6c 65 73 20 73 65 65 6e 20 69 6e  ariables seen in
7fb60 20 74 68 65 20 53 51 4c 20 73 6f 20 66 61 72 20   the SQL so far 
7fb70 2a 2f 0a 20 20 69 6e 74 20 6e 7a 56 61 72 3b 20  */.  int nzVar; 
7fb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
7fb90 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 76 61 69  * Number of avai
7fba0 6c 61 62 6c 65 20 73 6c 6f 74 73 20 69 6e 20 61  lable slots in a
7fbb0 7a 56 61 72 5b 5d 20 2a 2f 0a 20 20 75 38 20 65  zVar[] */.  u8 e
7fbc0 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 20 20 20  xplain;         
7fbd0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
7fbe0 20 74 68 65 20 45 58 50 4c 41 49 4e 20 66 6c 61   the EXPLAIN fla
7fbf0 67 20 69 73 20 66 6f 75 6e 64 20 6f 6e 20 74 68  g is found on th
7fc00 65 20 71 75 65 72 79 20 2a 2f 0a 23 69 66 6e 64  e query */.#ifnd
7fc10 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
7fc20 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 75 38  IRTUALTABLE.  u8
7fc30 20 64 65 63 6c 61 72 65 56 74 61 62 3b 20 20 20   declareVtab;   
7fc40 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
7fc50 69 66 20 69 6e 73 69 64 65 20 73 71 6c 69 74 65  if inside sqlite
7fc60 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29  3_declare_vtab()
7fc70 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 74 61 62 4c   */.  int nVtabL
7fc80 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ock;            
7fc90 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 69 72  /* Number of vir
7fca0 74 75 61 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c  tual tables to l
7fcb0 6f 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  ock */.#endif.  
7fcc0 69 6e 74 20 6e 41 6c 69 61 73 3b 20 20 20 20 20  int nAlias;     
7fcd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
7fce0 62 65 72 20 6f 66 20 61 6c 69 61 73 65 64 20 72  ber of aliased r
7fcf0 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e  esult set column
7fd00 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 65 69 67  s */.  int nHeig
7fd10 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ht;             
7fd20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74   /* Expression t
7fd30 72 65 65 20 68 65 69 67 68 74 20 6f 66 20 63 75  ree height of cu
7fd40 72 72 65 6e 74 20 73 75 62 2d 73 65 6c 65 63 74  rrent sub-select
7fd50 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
7fd60 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a  TE_OMIT_EXPLAIN.
7fd70 20 20 69 6e 74 20 69 53 65 6c 65 63 74 49 64 3b    int iSelectId;
7fd80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
7fd90 44 20 6f 66 20 63 75 72 72 65 6e 74 20 73 65 6c  D of current sel
7fda0 65 63 74 20 66 6f 72 20 45 58 50 4c 41 49 4e 20  ect for EXPLAIN 
7fdb0 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20  output */.  int 
7fdc0 69 4e 65 78 74 53 65 6c 65 63 74 49 64 3b 20 20  iNextSelectId;  
7fdd0 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 61 76        /* Next av
7fde0 61 69 6c 61 62 6c 65 20 73 65 6c 65 63 74 20 49  ailable select I
7fdf0 44 20 66 6f 72 20 45 58 50 4c 41 49 4e 20 6f 75  D for EXPLAIN ou
7fe00 74 70 75 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  tput */.#endif. 
7fe10 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20 20   char **azVar;  
7fe20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
7fe30 69 6e 74 65 72 73 20 74 6f 20 6e 61 6d 65 73 20  inters to names 
7fe40 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f  of parameters */
7fe50 0a 20 20 56 64 62 65 20 2a 70 52 65 70 72 65 70  .  Vdbe *pReprep
7fe60 61 72 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  are;         /* 
7fe70 56 4d 20 62 65 69 6e 67 20 72 65 70 72 65 70 61  VM being reprepa
7fe80 72 65 64 20 28 73 71 6c 69 74 65 33 52 65 70 72  red (sqlite3Repr
7fe90 65 70 61 72 65 28 29 29 20 2a 2f 0a 20 20 69 6e  epare()) */.  in
7fea0 74 20 2a 61 41 6c 69 61 73 3b 20 20 20 20 20 20  t *aAlias;      
7feb0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
7fec0 74 65 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64  ter used to hold
7fed0 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20   aliased result 
7fee0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
7fef0 2a 7a 54 61 69 6c 3b 20 20 20 20 20 20 20 20 2f  *zTail;        /
7ff00 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20 70  * All SQL text p
7ff10 61 73 74 20 74 68 65 20 6c 61 73 74 20 73 65 6d  ast the last sem
7ff20 69 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a 2f  icolon parsed */
7ff30 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 54 61  .  Table *pNewTa
7ff40 62 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ble;         /* 
7ff50 41 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6f  A table being co
7ff60 6e 73 74 72 75 63 74 65 64 20 62 79 20 43 52 45  nstructed by CRE
7ff70 41 54 45 20 54 41 42 4c 45 20 2a 2f 0a 20 20 54  ATE TABLE */.  T
7ff80 72 69 67 67 65 72 20 2a 70 4e 65 77 54 72 69 67  rigger *pNewTrig
7ff90 67 65 72 3b 20 20 20 20 20 2f 2a 20 54 72 69 67  ger;     /* Trig
7ffa0 67 65 72 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  ger under constr
7ffb0 75 63 74 20 62 79 20 61 20 43 52 45 41 54 45 20  uct by a CREATE 
7ffc0 54 52 49 47 47 45 52 20 2a 2f 0a 20 20 63 6f 6e  TRIGGER */.  con
7ffd0 73 74 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f  st char *zAuthCo
7ffe0 6e 74 65 78 74 3b 20 2f 2a 20 54 68 65 20 36 74  ntext; /* The 6t
7fff0 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 64  h parameter to d
80000 62 2d 3e 78 41 75 74 68 20 63 61 6c 6c 62 61 63  b->xAuth callbac
80010 6b 73 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4e  ks */.  Token sN
80020 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20  ameToken;       
80030 20 20 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68 20    /* Token with 
80040 75 6e 71 75 61 6c 69 66 69 65 64 20 73 63 68 65  unqualified sche
80050 6d 61 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ma object name *
80060 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74 54  /.  Token sLastT
80070 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  oken;         /*
80080 20 54 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20   The last token 
80090 70 61 72 73 65 64 20 2a 2f 0a 23 69 66 6e 64 65  parsed */.#ifnde
800a0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
800b0 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 54 6f 6b  RTUALTABLE.  Tok
800c0 65 6e 20 73 41 72 67 3b 20 20 20 20 20 20 20 20  en sArg;        
800d0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65         /* Comple
800e0 74 65 20 74 65 78 74 20 6f 66 20 61 20 6d 6f 64  te text of a mod
800f0 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a  ule argument */.
80100 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 62    Table **apVtab
80110 4c 6f 63 6b 3b 20 20 20 20 20 20 20 2f 2a 20 50  Lock;       /* P
80120 6f 69 6e 74 65 72 20 74 6f 20 76 69 72 74 75 61  ointer to virtua
80130 6c 20 74 61 62 6c 65 73 20 6e 65 65 64 69 6e 67  l tables needing
80140 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65 6e 64   locking */.#end
80150 69 66 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f 6d  if.  Table *pZom
80160 62 69 65 54 61 62 3b 20 20 20 20 20 20 20 20 2f  bieTab;        /
80170 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c 65 20  * List of Table 
80180 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c 65 74  objects to delet
80190 65 20 61 66 74 65 72 20 63 6f 64 65 20 67 65 6e  e after code gen
801a0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67   */.  TriggerPrg
801b0 20 2a 70 54 72 69 67 67 65 72 50 72 67 3b 20 20   *pTriggerPrg;  
801c0 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f  /* Linked list o
801d0 66 20 63 6f 64 65 64 20 74 72 69 67 67 65 72 73  f coded triggers
801e0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65   */.};../*.** Re
801f0 74 75 72 6e 20 74 72 75 65 20 69 66 20 63 75 72  turn true if cur
80200 72 65 6e 74 6c 79 20 69 6e 73 69 64 65 20 61 6e  rently inside an
80210 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
80220 5f 76 74 61 62 28 29 20 63 61 6c 6c 2e 0a 2a 2f  _vtab() call..*/
80230 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
80240 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
80250 0a 20 20 23 64 65 66 69 6e 65 20 49 4e 5f 44 45  .  #define IN_DE
80260 43 4c 41 52 45 5f 56 54 41 42 20 30 0a 23 65 6c  CLARE_VTAB 0.#el
80270 73 65 0a 20 20 23 64 65 66 69 6e 65 20 49 4e 5f  se.  #define IN_
80280 44 45 43 4c 41 52 45 5f 56 54 41 42 20 28 70 50  DECLARE_VTAB (pP
80290 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56 74 61  arse->declareVta
802a0 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  b).#endif../*.**
802b0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
802c0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
802d0 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 64  ructure can be d
802e0 65 63 6c 61 72 65 64 20 6f 6e 20 61 20 73 74 61  eclared on a sta
802f0 63 6b 20 61 6e 64 20 75 73 65 64 0a 2a 2a 20 74  ck and used.** t
80300 6f 20 73 61 76 65 20 74 68 65 20 50 61 72 73 65  o save the Parse
80310 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 76 61  .zAuthContext va
80320 6c 75 65 20 73 6f 20 74 68 61 74 20 69 74 20 63  lue so that it c
80330 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 6c  an be restored l
80340 61 74 65 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ater..*/.struct 
80350 41 75 74 68 43 6f 6e 74 65 78 74 20 7b 0a 20 20  AuthContext {.  
80360 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 75 74  const char *zAut
80370 68 43 6f 6e 74 65 78 74 3b 20 20 20 2f 2a 20 50  hContext;   /* P
80380 75 74 20 73 61 76 65 64 20 50 61 72 73 65 2e 7a  ut saved Parse.z
80390 41 75 74 68 43 6f 6e 74 65 78 74 20 68 65 72 65  AuthContext here
803a0 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61   */.  Parse *pPa
803b0 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  rse;            
803c0 20 20 2f 2a 20 54 68 65 20 50 61 72 73 65 20 73    /* The Parse s
803d0 74 72 75 63 74 75 72 65 20 2a 2f 0a 7d 3b 0a 0a  tructure */.};..
803e0 2f 2a 0a 2a 2a 20 42 69 74 66 69 65 6c 64 20 66  /*.** Bitfield f
803f0 6c 61 67 73 20 66 6f 72 20 50 35 20 76 61 6c 75  lags for P5 valu
80400 65 20 69 6e 20 76 61 72 69 6f 75 73 20 6f 70 63  e in various opc
80410 6f 64 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  odes..*/.#define
80420 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20   OPFLAG_NCHANGE 
80430 20 20 20 20 20 20 30 78 30 31 20 20 20 20 2f 2a        0x01    /*
80440 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20 64   Set to update d
80450 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a 23 64  b->nChange */.#d
80460 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c 41 53  efine OPFLAG_LAS
80470 54 52 4f 57 49 44 20 20 20 20 20 30 78 30 32 20  TROWID     0x02 
80480 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75 70 64     /* Set to upd
80490 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69  ate db->lastRowi
804a0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  d */.#define OPF
804b0 4c 41 47 5f 49 53 55 50 44 41 54 45 20 20 20 20  LAG_ISUPDATE    
804c0 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54 68 69    0x04    /* Thi
804d0 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 20 61  s OP_Insert is a
804e0 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a 2f 0a  n sql UPDATE */.
804f0 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 41  #define OPFLAG_A
80500 50 50 45 4e 44 20 20 20 20 20 20 20 20 30 78 30  PPEND        0x0
80510 38 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20  8    /* This is 
80520 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20  likely to be an 
80530 61 70 70 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e  append */.#defin
80540 65 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b  e OPFLAG_USESEEK
80550 52 45 53 55 4c 54 20 30 78 31 30 20 20 20 20 2f  RESULT 0x10    /
80560 2a 20 54 72 79 20 74 6f 20 61 76 6f 69 64 20 61  * Try to avoid a
80570 20 73 65 65 6b 20 69 6e 20 42 74 72 65 65 49 6e   seek in BtreeIn
80580 73 65 72 74 28 29 20 2a 2f 0a 23 64 65 66 69 6e  sert() */.#defin
80590 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41  e OPFLAG_CLEARCA
805a0 43 48 45 20 20 20 20 30 78 32 30 20 20 20 20 2f  CHE    0x20    /
805b0 2a 20 43 6c 65 61 72 20 70 73 65 75 64 6f 2d 74  * Clear pseudo-t
805c0 61 62 6c 65 20 63 61 63 68 65 20 69 6e 20 4f 50  able cache in OP
805d0 5f 43 6f 6c 75 6d 6e 20 2a 2f 0a 23 64 65 66 69  _Column */.#defi
805e0 6e 65 20 4f 50 46 4c 41 47 5f 4c 45 4e 47 54 48  ne OPFLAG_LENGTH
805f0 41 52 47 20 20 20 20 20 30 78 34 30 20 20 20 20  ARG     0x40    
80600 2f 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 6e 6c  /* OP_Column onl
80610 79 20 75 73 65 64 20 66 6f 72 20 6c 65 6e 67 74  y used for lengt
80620 68 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  h() */.#define O
80630 50 46 4c 41 47 5f 54 59 50 45 4f 46 41 52 47 20  PFLAG_TYPEOFARG 
80640 20 20 20 20 30 78 38 30 20 20 20 20 2f 2a 20 4f      0x80    /* O
80650 50 5f 43 6f 6c 75 6d 6e 20 6f 6e 6c 79 20 75 73  P_Column only us
80660 65 64 20 66 6f 72 20 74 79 70 65 6f 66 28 29 20  ed for typeof() 
80670 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41  */.#define OPFLA
80680 47 5f 42 55 4c 4b 43 53 52 20 20 20 20 20 20 20  G_BULKCSR       
80690 30 78 30 31 20 20 20 20 2f 2a 20 4f 50 5f 4f 70  0x01    /* OP_Op
806a0 65 6e 2a 2a 20 75 73 65 64 20 74 6f 20 6f 70 65  en** used to ope
806b0 6e 20 62 75 6c 6b 20 63 75 72 73 6f 72 20 2a 2f  n bulk cursor */
806c0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f  .#define OPFLAG_
806d0 50 32 49 53 52 45 47 20 20 20 20 20 20 20 30 78  P2ISREG       0x
806e0 30 32 20 20 20 20 2f 2a 20 50 32 20 74 6f 20 4f  02    /* P2 to O
806f0 50 5f 4f 70 65 6e 2a 2a 20 69 73 20 61 20 72 65  P_Open** is a re
80700 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 2a 2f  gister number */
80710 0a 0a 2f 2a 0a 20 2a 20 45 61 63 68 20 74 72 69  ../*. * Each tri
80720 67 67 65 72 20 70 72 65 73 65 6e 74 20 69 6e 20  gger present in 
80730 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
80740 65 6d 61 20 69 73 20 73 74 6f 72 65 64 20 61 73  ema is stored as
80750 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a   an instance of.
80760 20 2a 20 73 74 72 75 63 74 20 54 72 69 67 67 65   * struct Trigge
80770 72 2e 20 0a 20 2a 0a 20 2a 20 50 6f 69 6e 74 65  r. . *. * Pointe
80780 72 73 20 74 6f 20 69 6e 73 74 61 6e 63 65 73 20  rs to instances 
80790 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65  of struct Trigge
807a0 72 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  r are stored in 
807b0 74 77 6f 20 77 61 79 73 2e 0a 20 2a 20 31 2e 20  two ways.. * 1. 
807c0 49 6e 20 74 68 65 20 22 74 72 69 67 48 61 73 68  In the "trigHash
807d0 22 20 68 61 73 68 20 74 61 62 6c 65 20 28 70 61  " hash table (pa
807e0 72 74 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  rt of the sqlite
807f0 33 2a 20 74 68 61 74 20 72 65 70 72 65 73 65 6e  3* that represen
80800 74 73 20 74 68 65 20 0a 20 2a 20 20 20 20 64 61  ts the . *    da
80810 74 61 62 61 73 65 29 2e 20 54 68 69 73 20 61 6c  tabase). This al
80820 6c 6f 77 73 20 54 72 69 67 67 65 72 20 73 74 72  lows Trigger str
80830 75 63 74 75 72 65 73 20 74 6f 20 62 65 20 72 65  uctures to be re
80840 74 72 69 65 76 65 64 20 62 79 20 6e 61 6d 65 2e  trieved by name.
80850 0a 20 2a 20 32 2e 20 41 6c 6c 20 74 72 69 67 67  . * 2. All trigg
80860 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ers associated w
80870 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74 61 62  ith a single tab
80880 6c 65 20 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 64  le form a linked
80890 20 6c 69 73 74 2c 20 75 73 69 6e 67 20 74 68 65   list, using the
808a0 0a 20 2a 20 20 20 20 70 4e 65 78 74 20 6d 65 6d  . *    pNext mem
808b0 62 65 72 20 6f 66 20 73 74 72 75 63 74 20 54 72  ber of struct Tr
808c0 69 67 67 65 72 2e 20 41 20 70 6f 69 6e 74 65 72  igger. A pointer
808d0 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6c   to the first el
808e0 65 6d 65 6e 74 20 6f 66 20 74 68 65 0a 20 2a 20  ement of the. * 
808f0 20 20 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69     linked list i
80900 73 20 73 74 6f 72 65 64 20 61 73 20 74 68 65 20  s stored as the 
80910 22 70 54 72 69 67 67 65 72 22 20 6d 65 6d 62 65  "pTrigger" membe
80920 72 20 6f 66 20 74 68 65 20 61 73 73 6f 63 69 61  r of the associa
80930 74 65 64 0a 20 2a 20 20 20 20 73 74 72 75 63 74  ted. *    struct
80940 20 54 61 62 6c 65 2e 0a 20 2a 0a 20 2a 20 54 68   Table.. *. * Th
80950 65 20 22 73 74 65 70 5f 6c 69 73 74 22 20 6d 65  e "step_list" me
80960 6d 62 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74  mber points to t
80970 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74  he first element
80980 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   of a linked lis
80990 74 0a 20 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20  t. * containing 
809a0 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
809b0 74 73 20 73 70 65 63 69 66 69 65 64 20 61 73 20  ts specified as 
809c0 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67  the trigger prog
809d0 72 61 6d 2e 0a 20 2a 2f 0a 73 74 72 75 63 74 20  ram.. */.struct 
809e0 54 72 69 67 67 65 72 20 7b 0a 20 20 63 68 61 72  Trigger {.  char
809f0 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
80a00 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20      /* The name 
80a10 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20 20  of the trigger  
80a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80a30 20 20 20 20 20 20 2a 2f 0a 20 20 63 68 61 72 20        */.  char 
80a40 2a 74 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20  *table;         
80a50 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
80a60 6f 72 20 76 69 65 77 20 74 6f 20 77 68 69 63 68  or view to which
80a70 20 74 68 65 20 74 72 69 67 67 65 72 20 61 70 70   the trigger app
80a80 6c 69 65 73 20 2a 2f 0a 20 20 75 38 20 6f 70 3b  lies */.  u8 op;
80a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80aa0 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44    /* One of TK_D
80ab0 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41 54 45  ELETE, TK_UPDATE
80ac0 2c 20 54 4b 5f 49 4e 53 45 52 54 20 20 20 20 20  , TK_INSERT     
80ad0 20 20 20 20 2a 2f 0a 20 20 75 38 20 74 72 5f 74      */.  u8 tr_t
80ae0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
80af0 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 52 49 47 47   /* One of TRIGG
80b00 45 52 5f 42 45 46 4f 52 45 2c 20 54 52 49 47 47  ER_BEFORE, TRIGG
80b10 45 52 5f 41 46 54 45 52 20 2a 2f 0a 20 20 45 78  ER_AFTER */.  Ex
80b20 70 72 20 2a 70 57 68 65 6e 3b 20 20 20 20 20 20  pr *pWhen;      
80b30 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
80b40 4e 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  N clause of the 
80b50 65 78 70 72 65 73 73 69 6f 6e 20 28 6d 61 79 20  expression (may 
80b60 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 49 64  be NULL) */.  Id
80b70 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 73 3b 20  List *pColumns; 
80b80 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73        /* If this
80b90 20 69 73 20 61 6e 20 55 50 44 41 54 45 20 4f 46   is an UPDATE OF
80ba0 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 74   <column-list> t
80bb0 72 69 67 67 65 72 2c 0a 20 20 20 20 20 20 20 20  rigger,.        
80bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80bd0 20 20 20 20 20 74 68 65 20 3c 63 6f 6c 75 6d 6e       the <column
80be0 2d 6c 69 73 74 3e 20 69 73 20 73 74 6f 72 65 64  -list> is stored
80bf0 20 68 65 72 65 20 2a 2f 0a 20 20 53 63 68 65 6d   here */.  Schem
80c00 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20  a *pSchema;     
80c10 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e     /* Schema con
80c20 74 61 69 6e 69 6e 67 20 74 68 65 20 74 72 69 67  taining the trig
80c30 67 65 72 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20  ger */.  Schema 
80c40 2a 70 54 61 62 53 63 68 65 6d 61 3b 20 20 20 20  *pTabSchema;    
80c50 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61   /* Schema conta
80c60 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20  ining the table 
80c70 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  */.  TriggerStep
80c80 20 2a 73 74 65 70 5f 6c 69 73 74 3b 20 2f 2a 20   *step_list; /* 
80c90 4c 69 6e 6b 20 6c 69 73 74 20 6f 66 20 74 72 69  Link list of tri
80ca0 67 67 65 72 20 70 72 6f 67 72 61 6d 20 73 74 65  gger program ste
80cb0 70 73 20 20 20 20 20 20 20 20 20 20 20 20 20 2a  ps             *
80cc0 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4e 65  /.  Trigger *pNe
80cd0 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  xt;         /* N
80ce0 65 78 74 20 74 72 69 67 67 65 72 20 61 73 73 6f  ext trigger asso
80cf0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
80d00 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  table */.};../*.
80d10 2a 2a 20 41 20 74 72 69 67 67 65 72 20 69 73 20  ** A trigger is 
80d20 65 69 74 68 65 72 20 61 20 42 45 46 4f 52 45 20  either a BEFORE 
80d30 6f 72 20 61 6e 20 41 46 54 45 52 20 74 72 69 67  or an AFTER trig
80d40 67 65 72 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  ger.  The follow
80d50 69 6e 67 20 63 6f 6e 73 74 61 6e 74 73 0a 2a 2a  ing constants.**
80d60 20 64 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68   determine which
80d70 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  . .**.** If ther
80d80 65 20 61 72 65 20 6d 75 6c 74 69 70 6c 65 20 74  e are multiple t
80d90 72 69 67 67 65 72 73 2c 20 79 6f 75 20 6d 69 67  riggers, you mig
80da0 68 74 20 6f 66 20 73 6f 6d 65 20 42 45 46 4f 52  ht of some BEFOR
80db0 45 20 61 6e 64 20 73 6f 6d 65 20 41 46 54 45 52  E and some AFTER
80dc0 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 20 63 61 73  ..** In that cas
80dd0 65 73 2c 20 74 68 65 20 63 6f 6e 73 74 61 6e 74  es, the constant
80de0 73 20 62 65 6c 6f 77 20 63 61 6e 20 62 65 20 4f  s below can be O
80df0 52 65 64 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2f  Red together..*/
80e00 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 45 52  .#define TRIGGER
80e10 5f 42 45 46 4f 52 45 20 20 31 0a 23 64 65 66 69  _BEFORE  1.#defi
80e20 6e 65 20 54 52 49 47 47 45 52 5f 41 46 54 45 52  ne TRIGGER_AFTER
80e30 20 20 20 32 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69     2../*. * An i
80e40 6e 73 74 61 6e 63 65 20 6f 66 20 73 74 72 75 63  nstance of struc
80e50 74 20 54 72 69 67 67 65 72 53 74 65 70 20 69 73  t TriggerStep is
80e60 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61   used to store a
80e70 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 74   single SQL stat
80e80 65 6d 65 6e 74 0a 20 2a 20 74 68 61 74 20 69 73  ement. * that is
80e90 20 61 20 70 61 72 74 20 6f 66 20 61 20 74 72 69   a part of a tri
80ea0 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 20 0a 20  gger-program. . 
80eb0 2a 0a 20 2a 20 49 6e 73 74 61 6e 63 65 73 20 6f  *. * Instances o
80ec0 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72  f struct Trigger
80ed0 53 74 65 70 20 61 72 65 20 73 74 6f 72 65 64 20  Step are stored 
80ee0 69 6e 20 61 20 73 69 6e 67 6c 79 20 6c 69 6e 6b  in a singly link
80ef0 65 64 20 6c 69 73 74 20 28 6c 69 6e 6b 65 64 0a  ed list (linked.
80f00 20 2a 20 75 73 69 6e 67 20 74 68 65 20 22 70 4e   * using the "pN
80f10 65 78 74 22 20 6d 65 6d 62 65 72 29 20 72 65 66  ext" member) ref
80f20 65 72 65 6e 63 65 64 20 62 79 20 74 68 65 20 22  erenced by the "
80f30 73 74 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65  step_list" membe
80f40 72 20 6f 66 20 74 68 65 20 0a 20 2a 20 61 73 73  r of the . * ass
80f50 6f 63 69 61 74 65 64 20 73 74 72 75 63 74 20 54  ociated struct T
80f60 72 69 67 67 65 72 20 69 6e 73 74 61 6e 63 65 2e  rigger instance.
80f70 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   The first eleme
80f80 6e 74 20 6f 66 20 74 68 65 20 6c 69 6e 6b 65 64  nt of the linked
80f90 20 6c 69 73 74 20 69 73 0a 20 2a 20 74 68 65 20   list is. * the 
80fa0 66 69 72 73 74 20 73 74 65 70 20 6f 66 20 74 68  first step of th
80fb0 65 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61  e trigger-progra
80fc0 6d 2e 0a 20 2a 20 0a 20 2a 20 54 68 65 20 22 6f  m.. * . * The "o
80fd0 70 22 20 6d 65 6d 62 65 72 20 69 6e 64 69 63 61  p" member indica
80fe0 74 65 73 20 77 68 65 74 68 65 72 20 74 68 69 73  tes whether this
80ff0 20 69 73 20 61 20 22 44 45 4c 45 54 45 22 2c 20   is a "DELETE", 
81000 22 49 4e 53 45 52 54 22 2c 20 22 55 50 44 41 54  "INSERT", "UPDAT
81010 45 22 20 6f 72 0a 20 2a 20 22 53 45 4c 45 43 54  E" or. * "SELECT
81020 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65  " statement. The
81030 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65   meanings of the
81040 20 6f 74 68 65 72 20 6d 65 6d 62 65 72 73 20 69   other members i
81050 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20  s determined by 
81060 74 68 65 20 0a 20 2a 20 76 61 6c 75 65 20 6f 66  the . * value of
81070 20 22 6f 70 22 20 61 73 20 66 6f 6c 6c 6f 77 73   "op" as follows
81080 3a 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54  :. *. * (op == T
81090 4b 5f 49 4e 53 45 52 54 29 0a 20 2a 20 6f 72 63  K_INSERT). * orc
810a0 6f 6e 66 20 20 20 20 2d 3e 20 73 74 6f 72 65 73  onf    -> stores
810b0 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54   the ON CONFLICT
810c0 20 61 6c 67 6f 72 69 74 68 6d 0a 20 2a 20 70 53   algorithm. * pS
810d0 65 6c 65 63 74 20 20 20 2d 3e 20 49 66 20 74 68  elect   -> If th
810e0 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20  is is an INSERT 
810f0 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 20  INTO ... SELECT 
81100 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 74  ... statement, t
81110 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 20 20  hen. *          
81120 20 20 20 20 74 68 69 73 20 73 74 6f 72 65 73 20      this stores 
81130 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
81140 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
81150 74 2e 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c  t. Otherwise NUL
81160 4c 2e 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20  L.. * target    
81170 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69  -> A token holdi
81180 6e 67 20 74 68 65 20 71 75 6f 74 65 64 20 6e 61  ng the quoted na
81190 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
811a0 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e 0a  to insert into..
811b0 20 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20   * pExprList -> 
811c0 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e  If this is an IN
811d0 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 56 41  SERT INTO ... VA
811e0 4c 55 45 53 20 2e 2e 2e 20 73 74 61 74 65 6d 65  LUES ... stateme
811f0 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 20  nt, then. *     
81200 20 20 20 20 20 20 20 20 20 74 68 69 73 20 73 74           this st
81210 6f 72 65 73 20 76 61 6c 75 65 73 20 74 6f 20 62  ores values to b
81220 65 20 69 6e 73 65 72 74 65 64 2e 20 4f 74 68 65  e inserted. Othe
81230 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70  rwise NULL.. * p
81240 49 64 4c 69 73 74 20 20 20 2d 3e 20 49 66 20 74  IdList   -> If t
81250 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54  his is an INSERT
81260 20 49 4e 54 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75   INTO ... (<colu
81270 6d 6e 2d 6e 61 6d 65 73 3e 29 20 56 41 4c 55 45  mn-names>) VALUE
81280 53 20 2e 2e 2e 20 0a 20 2a 20 20 20 20 20 20 20  S ... . *       
81290 20 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74         statement
812a0 2c 20 74 68 65 6e 20 74 68 69 73 20 73 74 6f 72  , then this stor
812b0 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61  es the column-na
812c0 6d 65 73 20 74 6f 20 62 65 0a 20 2a 20 20 20 20  mes to be. *    
812d0 20 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74            insert
812e0 65 64 20 69 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28  ed into.. *. * (
812f0 6f 70 20 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29  op == TK_DELETE)
81300 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e  . * target    ->
81310 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67   A token holding
81320 20 74 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65   the quoted name
81330 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
81340 20 64 65 6c 65 74 65 20 66 72 6f 6d 2e 0a 20 2a   delete from.. *
81350 20 70 57 68 65 72 65 20 20 20 20 2d 3e 20 54 68   pWhere    -> Th
81360 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f  e WHERE clause o
81370 66 20 74 68 65 20 44 45 4c 45 54 45 20 73 74 61  f the DELETE sta
81380 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69 73  tement if one is
81390 20 73 70 65 63 69 66 69 65 64 2e 0a 20 2a 20 20   specified.. *  
813a0 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65              Othe
813b0 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 0a  rwise NULL.. * .
813c0 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 55 50 44   * (op == TK_UPD
813d0 41 54 45 29 0a 20 2a 20 74 61 72 67 65 74 20 20  ATE). * target  
813e0 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c    -> A token hol
813f0 64 69 6e 67 20 74 68 65 20 71 75 6f 74 65 64 20  ding the quoted 
81400 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
81410 65 20 74 6f 20 75 70 64 61 74 65 20 72 6f 77 73  e to update rows
81420 20 6f 66 2e 0a 20 2a 20 70 57 68 65 72 65 20 20   of.. * pWhere  
81430 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63    -> The WHERE c
81440 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44  lause of the UPD
81450 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66  ATE statement if
81460 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65   one is specifie
81470 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  d.. *           
81480 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c     Otherwise NUL
81490 4c 2e 0a 20 2a 20 70 45 78 70 72 4c 69 73 74 20  L.. * pExprList 
814a0 2d 3e 20 41 20 6c 69 73 74 20 6f 66 20 74 68 65  -> A list of the
814b0 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 75 70 64 61   columns to upda
814c0 74 65 20 61 6e 64 20 74 68 65 20 65 78 70 72 65  te and the expre
814d0 73 73 69 6f 6e 73 20 74 6f 20 75 70 64 61 74 65  ssions to update
814e0 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  . *             
814f0 20 74 68 65 6d 20 74 6f 2e 20 53 65 65 20 73 71   them to. See sq
81500 6c 69 74 65 33 55 70 64 61 74 65 28 29 20 64 6f  lite3Update() do
81510 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 22  cumentation of "
81520 70 43 68 61 6e 67 65 73 22 0a 20 2a 20 20 20 20  pChanges". *    
81530 20 20 20 20 20 20 20 20 20 20 61 72 67 75 6d 65            argume
81540 6e 74 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74 72 75  nt.. * . */.stru
81550 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20 7b  ct TriggerStep {
81560 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20  .  u8 op;       
81570 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
81580 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f  f TK_DELETE, TK_
81590 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52  UPDATE, TK_INSER
815a0 54 2c 20 54 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a  T, TK_SELECT */.
815b0 20 20 75 38 20 6f 72 63 6f 6e 66 3b 20 20 20 20    u8 orconf;    
815c0 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f 52 6f 6c         /* OE_Rol
815d0 6c 62 61 63 6b 20 65 74 63 2e 20 2a 2f 0a 20 20  lback etc. */.  
815e0 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b 20  Trigger *pTrig; 
815f0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 69 67       /* The trig
81600 67 65 72 20 74 68 61 74 20 74 68 69 73 20 73 74  ger that this st
81610 65 70 20 69 73 20 61 20 70 61 72 74 20 6f 66 20  ep is a part of 
81620 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
81630 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 53 45 4c  lect;     /* SEL
81640 45 43 54 20 73 74 61 74 6d 65 6e 74 20 6f 72 20  ECT statment or 
81650 52 48 53 20 6f 66 20 49 4e 53 45 52 54 20 49 4e  RHS of INSERT IN
81660 54 4f 20 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e  TO .. SELECT ...
81670 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74 61 72 67   */.  Token targ
81680 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 61  et;        /* Ta
81690 72 67 65 74 20 74 61 62 6c 65 20 66 6f 72 20 44  rget table for D
816a0 45 4c 45 54 45 2c 20 55 50 44 41 54 45 2c 20 49  ELETE, UPDATE, I
816b0 4e 53 45 52 54 20 2a 2f 0a 20 20 45 78 70 72 20  NSERT */.  Expr 
816c0 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 20  *pWhere;        
816d0 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
816e0 75 73 65 20 66 6f 72 20 44 45 4c 45 54 45 20 6f  use for DELETE o
816f0 72 20 55 50 44 41 54 45 20 73 74 65 70 73 20 2a  r UPDATE steps *
81700 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
81710 78 70 72 4c 69 73 74 3b 20 2f 2a 20 53 45 54 20  xprList; /* SET 
81720 63 6c 61 75 73 65 20 66 6f 72 20 55 50 44 41 54  clause for UPDAT
81730 45 2e 20 20 56 41 4c 55 45 53 20 63 6c 61 75 73  E.  VALUES claus
81740 65 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f 0a  e for INSERT */.
81750 20 20 49 64 4c 69 73 74 20 2a 70 49 64 4c 69 73    IdList *pIdLis
81760 74 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e  t;     /* Column
81770 20 6e 61 6d 65 73 20 66 6f 72 20 49 4e 53 45 52   names for INSER
81780 54 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74  T */.  TriggerSt
81790 65 70 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e  ep *pNext;  /* N
817a0 65 78 74 20 69 6e 20 74 68 65 20 6c 69 6e 6b 2d  ext in the link-
817b0 6c 69 73 74 20 2a 2f 0a 20 20 54 72 69 67 67 65  list */.  Trigge
817c0 72 53 74 65 70 20 2a 70 4c 61 73 74 3b 20 20 2f  rStep *pLast;  /
817d0 2a 20 4c 61 73 74 20 65 6c 65 6d 65 6e 74 20 69  * Last element i
817e0 6e 20 6c 69 6e 6b 2d 6c 69 73 74 2e 20 56 61 6c  n link-list. Val
817f0 69 64 20 66 6f 72 20 31 73 74 20 65 6c 65 6d 20  id for 1st elem 
81800 6f 6e 6c 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  only */.};../*.*
81810 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
81820 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
81830 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75  ns information u
81840 73 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74  sed by the sqlit
81850 65 46 69 78 2e 2e 2e 0a 2a 2a 20 72 6f 75 74 69  eFix....** routi
81860 6e 65 73 20 61 73 20 74 68 65 79 20 77 61 6c 6b  nes as they walk
81870 20 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20   the parse tree 
81880 74 6f 20 6d 61 6b 65 20 64 61 74 61 62 61 73 65  to make database
81890 20 72 65 66 65 72 65 6e 63 65 73 0a 2a 2a 20 65   references.** e
818a0 78 70 6c 69 63 69 74 2e 20 20 0a 2a 2f 0a 74 79  xplicit.  .*/.ty
818b0 70 65 64 65 66 20 73 74 72 75 63 74 20 44 62 46  pedef struct DbF
818c0 69 78 65 72 20 44 62 46 69 78 65 72 3b 0a 73 74  ixer DbFixer;.st
818d0 72 75 63 74 20 44 62 46 69 78 65 72 20 7b 0a 20  ruct DbFixer {. 
818e0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20   Parse *pParse; 
818f0 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
81900 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 45 72  ing context.  Er
81910 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 72 69  ror messages wri
81920 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 53  tten here */.  S
81930 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20  chema *pSchema; 
81940 20 20 20 2f 2a 20 46 69 78 20 69 74 65 6d 73 20     /* Fix items 
81950 74 6f 20 74 68 69 73 20 73 63 68 65 6d 61 20 2a  to this schema *
81960 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
81970 7a 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 20  zDb;    /* Make 
81980 73 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74 73  sure all objects
81990 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69   are contained i
819a0 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  n this database 
819b0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
819c0 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 70 65  *zType;  /* Type
819d0 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e 65   of the containe
819e0 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72 72  r - used for err
819f0 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20  or messages */. 
81a00 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 4e   const Token *pN
81a10 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ame; /* Name of 
81a20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d 20  the container - 
81a30 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d  used for error m
81a40 65 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f  essages */.};../
81a50 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 65 64  *.** An objected
81a60 20 75 73 65 64 20 74 6f 20 61 63 63 75 6d 75 6c   used to accumul
81a70 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 20  ate the text of 
81a80 61 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 77  a string where w
81a90 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 63 65  e.** do not nece
81aa0 73 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68 6f 77  ssarily know how
81ab0 20 62 69 67 20 74 68 65 20 73 74 72 69 6e 67 20   big the string 
81ac0 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 20 65  will be in the e
81ad0 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 74  nd..*/.struct St
81ae0 72 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c 69 74  rAccum {.  sqlit
81af0 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
81b00 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 74 61  /* Optional data
81b10 62 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 69  base for lookasi
81b20 64 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 4c 4c  de.  Can be NULL
81b30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 61 73   */.  char *zBas
81b40 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20  e;         /* A 
81b50 62 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  base allocation.
81b60 20 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c 6f    Not from mallo
81b70 63 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54  c. */.  char *zT
81b80 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ext;         /* 
81b90 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c 6c 65  The string colle
81ba0 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20  cted so far */. 
81bb0 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 20 20   int  nChar;    
81bc0 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
81bd0 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 73 6f  of the string so
81be0 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e   far */.  int  n
81bf0 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 2f  Alloc;         /
81c00 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63  * Amount of spac
81c10 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 7a  e allocated in z
81c20 54 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 20 6d  Text */.  int  m
81c30 78 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f  xAlloc;        /
81c40 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65  * Maximum allowe
81c50 64 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 20  d string length 
81c60 2a 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c 6f 63  */.  u8   malloc
81c70 46 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42 65 63  Failed;   /* Bec
81c80 6f 6d 65 73 20 74 72 75 65 20 69 66 20 61 6e 79  omes true if any
81c90 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
81ca0 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 75 38  on fails */.  u8
81cb0 20 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20 20 20     useMalloc;   
81cc0 20 20 20 2f 2a 20 30 3a 20 6e 6f 6e 65 2c 20 20     /* 0: none,  
81cd0 31 3a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  1: sqlite3DbMall
81ce0 6f 63 2c 20 20 32 3a 20 73 71 6c 69 74 65 33 5f  oc,  2: sqlite3_
81cf0 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20 20  malloc */.  u8  
81d00 20 74 6f 6f 42 69 67 3b 20 20 20 20 20 20 20 20   tooBig;        
81d10 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65   /* Becomes true
81d20 20 69 66 20 73 74 72 69 6e 67 20 73 69 7a 65 20   if string size 
81d30 65 78 63 65 65 64 73 20 6c 69 6d 69 74 73 20 2a  exceeds limits *
81d40 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70 6f  /.};../*.** A po
81d50 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74  inter to this st
81d60 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20  ructure is used 
81d70 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 69  to communicate i
81d80 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 72  nformation.** fr
81d90 6f 6d 20 73 71 6c 69 74 65 33 49 6e 69 74 20 61  om sqlite3Init a
81da0 6e 64 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  nd OP_ParseSchem
81db0 61 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74  a into the sqlit
81dc0 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e 0a  e3InitCallback..
81dd0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
81de0 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  t {.  sqlite3 *d
81df0 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  b;        /* The
81e00 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20   database being 
81e10 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20  initialized */. 
81e20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
81e30 3b 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65  ;    /* Error me
81e40 73 73 61 67 65 20 73 74 6f 72 65 64 20 68 65 72  ssage stored her
81e50 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  e */.  int iDb; 
81e60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20             /* 0 
81e70 66 6f 72 20 6d 61 69 6e 20 64 61 74 61 62 61 73  for main databas
81e80 65 2e 20 20 31 20 66 6f 72 20 54 45 4d 50 2c 20  e.  1 for TEMP, 
81e90 32 2e 2e 20 66 6f 72 20 41 54 54 41 43 48 65 64  2.. for ATTACHed
81ea0 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
81eb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
81ec0 75 6c 74 20 63 6f 64 65 20 73 74 6f 72 65 64 20  ult code stored 
81ed0 68 65 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44 61  here */.} InitDa
81ee0 74 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63  ta;../*.** Struc
81ef0 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ture containing 
81f00 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61  global configura
81f10 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 20 74 68  tion data for th
81f20 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
81f30 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72  ..**.** This str
81f40 75 63 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e 74  ucture also cont
81f50 61 69 6e 73 20 73 6f 6d 65 20 73 74 61 74 65 20  ains some state 
81f60 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
81f70 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f  struct Sqlite3Co
81f80 6e 66 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d 65  nfig {.  int bMe
81f90 6d 73 74 61 74 3b 20 20 20 20 20 20 20 20 20 20  mstat;          
81fa0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
81fb0 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65 6d  ue to enable mem
81fc0 6f 72 79 20 73 74 61 74 75 73 20 2a 2f 0a 20 20  ory status */.  
81fd0 69 6e 74 20 62 43 6f 72 65 4d 75 74 65 78 3b 20  int bCoreMutex; 
81fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
81ff0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61    /* True to ena
82000 62 6c 65 20 63 6f 72 65 20 6d 75 74 65 78 69 6e  ble core mutexin
82010 67 20 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c 6c  g */.  int bFull
82020 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20  Mutex;          
82030 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
82040 20 74 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c 20   to enable full 
82050 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69 6e  mutexing */.  in
82060 74 20 62 4f 70 65 6e 55 72 69 3b 20 20 20 20 20  t bOpenUri;     
82070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82080 2f 2a 20 54 72 75 65 20 74 6f 20 69 6e 74 65 72  /* True to inter
82090 70 72 65 74 20 66 69 6c 65 6e 61 6d 65 73 20 61  pret filenames a
820a0 73 20 55 52 49 73 20 2a 2f 0a 20 20 69 6e 74 20  s URIs */.  int 
820b0 62 55 73 65 43 69 73 3b 20 20 20 20 20 20 20 20  bUseCis;        
820c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
820d0 20 55 73 65 20 63 6f 76 65 72 69 6e 67 20 69 6e   Use covering in
820e0 64 69 63 65 73 20 66 6f 72 20 66 75 6c 6c 2d 73  dices for full-s
820f0 63 61 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 78  cans */.  int mx
82100 53 74 72 6c 65 6e 3b 20 20 20 20 20 20 20 20 20  Strlen;         
82110 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
82120 61 78 69 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65  aximum string le
82130 6e 67 74 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  ngth */.  int sz
82140 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20  Lookaside;      
82150 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
82160 65 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65  efault lookaside
82170 20 62 75 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a   buffer size */.
82180 20 20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65    int nLookaside
82190 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
821a0 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c      /* Default l
821b0 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20  ookaside buffer 
821c0 63 6f 75 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74  count */.  sqlit
821d0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d  e3_mem_methods m
821e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
821f0 4c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  Low-level memory
82200 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65   allocation inte
82210 72 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74  rface */.  sqlit
82220 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
82230 20 6d 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20   mutex;      /* 
82240 4c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20  Low-level mutex 
82250 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73  interface */.  s
82260 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
82270 74 68 6f 64 73 32 20 70 63 61 63 68 65 32 3b 20  thods2 pcache2; 
82280 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 70 61   /* Low-level pa
82290 67 65 2d 63 61 63 68 65 20 69 6e 74 65 72 66 61  ge-cache interfa
822a0 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 48  ce */.  void *pH
822b0 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  eap;            
822c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61            /* Hea
822d0 70 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20  p storage space 
822e0 2a 2f 0a 20 20 69 6e 74 20 6e 48 65 61 70 3b 20  */.  int nHeap; 
822f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82300 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
82310 66 20 70 48 65 61 70 5b 5d 20 2a 2f 0a 20 20 69  f pHeap[] */.  i
82320 6e 74 20 6d 6e 52 65 71 2c 20 6d 78 52 65 71 3b  nt mnReq, mxReq;
82330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82340 20 2f 2a 20 4d 69 6e 20 61 6e 64 20 6d 61 78 20   /* Min and max 
82350 68 65 61 70 20 72 65 71 75 65 73 74 73 20 73 69  heap requests si
82360 7a 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  zes */.  void *p
82370 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20  Scratch;        
82380 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63             /* Sc
82390 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 2a 2f 0a  ratch memory */.
823a0 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b    int szScratch;
823b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
823c0 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65      /* Size of e
823d0 61 63 68 20 73 63 72 61 74 63 68 20 62 75 66 66  ach scratch buff
823e0 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 63 72  er */.  int nScr
823f0 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20  atch;           
82400 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
82410 62 65 72 20 6f 66 20 73 63 72 61 74 63 68 20 62  ber of scratch b
82420 75 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64  uffers */.  void
82430 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20   *pPage;        
82440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
82450 20 50 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f   Page cache memo
82460 72 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61  ry */.  int szPa
82470 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
82480 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
82490 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 69  e of each page i
824a0 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69  n pPage[] */.  i
824b0 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20  nt nPage;       
824c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
824d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
824e0 67 65 73 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a  ges in pPage[] *
824f0 2f 0a 20 20 69 6e 74 20 6d 78 50 61 72 73 65 72  /.  int mxParser
82500 53 74 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20  Stack;          
82510 20 20 20 20 20 20 2f 2a 20 6d 61 78 69 6d 75 6d        /* maximum
82520 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61   depth of the pa
82530 72 73 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20  rser stack */.  
82540 69 6e 74 20 73 68 61 72 65 64 43 61 63 68 65 45  int sharedCacheE
82550 6e 61 62 6c 65 64 3b 20 20 20 20 20 20 20 20 20  nabled;         
82560 20 20 2f 2a 20 74 72 75 65 20 69 66 20 73 68 61    /* true if sha
82570 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 65  red-cache mode e
82580 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 2f 2a 20 54  nabled */.  /* T
82590 68 65 20 61 62 6f 76 65 20 6d 69 67 68 74 20 62  he above might b
825a0 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f  e initialized to
825b0 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54 68 65 20   non-zero.  The 
825c0 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 65 64 20 74  following need t
825d0 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 69 6e  o always.  ** in
825e0 69 74 69 61 6c 6c 79 20 62 65 20 7a 65 72 6f 2c  itially be zero,
825f0 20 68 6f 77 65 76 65 72 2e 20 2a 2f 0a 20 20 69   however. */.  i
82600 6e 74 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20  nt isInit;      
82610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82620 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 69   /* True after i
82630 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 68 61  nitialization ha
82640 73 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20  s finished */.  
82650 69 6e 74 20 69 6e 50 72 6f 67 72 65 73 73 3b 20  int inProgress; 
82660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82670 20 20 2f 2a 20 54 72 75 65 20 77 68 69 6c 65 20    /* True while 
82680 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69  initialization i
82690 6e 20 70 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20  n progress */.  
826a0 69 6e 74 20 69 73 4d 75 74 65 78 49 6e 69 74 3b  int isMutexInit;
826b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
826c0 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20    /* True after 
826d0 6d 75 74 65 78 65 73 20 61 72 65 20 69 6e 69 74  mutexes are init
826e0 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74  ialized */.  int
826f0 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20   isMallocInit;  
82700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
82710 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c  * True after mal
82720 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  loc is initializ
82730 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 73 50 43  ed */.  int isPC
82740 61 63 68 65 49 6e 69 74 3b 20 20 20 20 20 20 20  acheInit;       
82750 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
82760 65 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63 20 69  e after malloc i
82770 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f  s initialized */
82780 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
82790 20 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20 20 20   *pInitMutex;   
827a0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73       /* Mutex us
827b0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e  ed by sqlite3_in
827c0 69 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a 20 20  itialize() */.  
827d0 69 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75 74 65  int nRefInitMute
827e0 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
827f0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75    /* Number of u
82800 73 65 72 73 20 6f 66 20 70 49 6e 69 74 4d 75 74  sers of pInitMut
82810 65 78 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  ex */.  void (*x
82820 4c 6f 67 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  Log)(void*,int,c
82830 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20 2f 2a 20  onst char*); /* 
82840 46 75 6e 63 74 69 6f 6e 20 66 6f 72 20 6c 6f 67  Function for log
82850 67 69 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  ging */.  void *
82860 70 4c 6f 67 41 72 67 3b 20 20 20 20 20 20 20 20  pLogArg;        
82870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
82880 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74  * First argument
82890 20 74 6f 20 78 4c 6f 67 28 29 20 2a 2f 0a 20 20   to xLog() */.  
828a0 69 6e 74 20 62 4c 6f 63 61 6c 74 69 6d 65 46 61  int bLocaltimeFa
828b0 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ult;            
828c0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 66 61 69    /* True to fai
828d0 6c 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 63 61  l localtime() ca
828e0 6c 6c 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 51  lls */.#ifdef SQ
828f0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 51 4c 4c  LITE_ENABLE_SQLL
82900 4f 47 0a 20 20 76 6f 69 64 28 2a 78 53 71 6c 6c  OG.  void(*xSqll
82910 6f 67 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  og)(void*,sqlite
82920 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  3*,const char*, 
82930 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 2a 70 53  int);.  void *pS
82940 71 6c 6c 6f 67 41 72 67 3b 0a 23 65 6e 64 69 66  qllogArg;.#endif
82950 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 65  .};../*.** Conte
82960 78 74 20 70 6f 69 6e 74 65 72 20 70 61 73 73 65  xt pointer passe
82970 64 20 64 6f 77 6e 20 74 68 72 6f 75 67 68 20 74  d down through t
82980 68 65 20 74 72 65 65 2d 77 61 6c 6b 2e 0a 2a 2f  he tree-walk..*/
82990 0a 73 74 72 75 63 74 20 57 61 6c 6b 65 72 20 7b  .struct Walker {
829a0 0a 20 20 69 6e 74 20 28 2a 78 45 78 70 72 43 61  .  int (*xExprCa
829b0 6c 6c 62 61 63 6b 29 28 57 61 6c 6b 65 72 2a 2c  llback)(Walker*,
829c0 20 45 78 70 72 2a 29 3b 20 20 20 20 20 2f 2a 20   Expr*);     /* 
829d0 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 65 78 70  Callback for exp
829e0 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  ressions */.  in
829f0 74 20 28 2a 78 53 65 6c 65 63 74 43 61 6c 6c 62  t (*xSelectCallb
82a00 61 63 6b 29 28 57 61 6c 6b 65 72 2a 2c 53 65 6c  ack)(Walker*,Sel
82a10 65 63 74 2a 29 3b 20 20 2f 2a 20 43 61 6c 6c 62  ect*);  /* Callb
82a20 61 63 6b 20 66 6f 72 20 53 45 4c 45 43 54 73 20  ack for SELECTs 
82a30 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  */.  Parse *pPar
82a40 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  se;             
82a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
82a60 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74  * Parser context
82a70 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 77 61 6c 6b  .  */.  int walk
82a80 65 72 44 65 70 74 68 3b 20 20 20 20 20 20 20 20  erDepth;        
82a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82aa0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
82ab0 75 62 71 75 65 72 69 65 73 20 2a 2f 0a 20 20 75  ubqueries */.  u
82ac0 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20  nion {          
82ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82ae0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72           /* Extr
82af0 61 20 64 61 74 61 20 66 6f 72 20 63 61 6c 6c 62  a data for callb
82b00 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43  ack */.    NameC
82b10 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 20 20 20  ontext *pNC;    
82b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82b30 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 20        /* Naming 
82b40 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 69  context */.    i
82b50 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
82b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82b70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74            /* Int
82b80 65 67 65 72 20 76 61 6c 75 65 20 2a 2f 0a 20 20  eger value */.  
82b90 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c    SrcList *pSrcL
82ba0 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ist;            
82bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
82bc0 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
82bd0 20 20 20 73 74 72 75 63 74 20 53 72 63 43 6f 75     struct SrcCou
82be0 6e 74 20 2a 70 53 72 63 43 6f 75 6e 74 3b 20 20  nt *pSrcCount;  
82bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
82c00 20 43 6f 75 6e 74 69 6e 67 20 63 6f 6c 75 6d 6e   Counting column
82c10 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 20   references */. 
82c20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72   } u;.};../* For
82c30 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
82c40 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  s */.SQLITE_PRIV
82c50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
82c60 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c  alkExpr(Walker*,
82c70 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
82c80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
82c90 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28  te3WalkExprList(
82ca0 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c 69 73  Walker*, ExprLis
82cb0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
82cc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
82cd0 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 72  alkSelect(Walker
82ce0 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  *, Select*);.SQL
82cf0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
82d00 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
82d10 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 53  tExpr(Walker*, S
82d20 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
82d30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
82d40 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f  te3WalkSelectFro
82d50 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63  m(Walker*, Selec
82d60 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  t*);../*.** Retu
82d70 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65  rn code from the
82d80 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b   parse-tree walk
82d90 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 61  ing primitives a
82da0 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 6c 6c  nd their.** call
82db0 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  backs..*/.#defin
82dc0 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20  e WRC_Continue  
82dd0 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69 6e 75    0   /* Continu
82de0 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68 69 6c  e down into chil
82df0 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  dren */.#define 
82e00 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 20 20  WRC_Prune       
82e10 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68 69 6c  1   /* Omit chil
82e20 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69 6e 75  dren but continu
82e30 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c 69 6e  e walking siblin
82e40 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52  gs */.#define WR
82e50 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 32 20  C_Abort       2 
82e60 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74 68 65    /* Abandon the
82e70 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f   tree walk */../
82e80 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 7a 49  *.** Assuming zI
82e90 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  n points to the 
82ea0 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 20  first byte of a 
82eb0 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2c  UTF-8 character,
82ec0 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 6e 20  .** advance zIn 
82ed0 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
82ee0 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68  first byte of th
82ef0 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63 68 61  e next UTF-8 cha
82f00 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69  racter..*/.#defi
82f10 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55  ne SQLITE_SKIP_U
82f20 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 20 20  TF8(zIn) {      
82f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82f40 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e    \.  if( (*(zIn
82f50 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 20 20  ++))>=0xc0 ){   
82f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82f70 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
82f80 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 26 20   while( (*zIn & 
82f90 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 7a  0xc0)==0x80 ){ z
82fa0 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 20 20  In++; }         
82fb0 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20      \.  }       
82fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
82fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d               \.}
82ff0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ../*.** The SQLI
83000 54 45 5f 2a 5f 42 4b 50 54 20 6d 61 63 72 6f 73  TE_*_BKPT macros
83010 20 61 72 65 20 73 75 62 73 74 69 74 75 74 65 73   are substitutes
83020 20 66 6f 72 20 74 68 65 20 65 72 72 6f 72 20 63   for the error c
83030 6f 64 65 73 20 77 69 74 68 0a 2a 2a 20 74 68 65  odes with.** the
83040 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77   same name but w
83050 69 74 68 6f 75 74 20 74 68 65 20 5f 42 4b 50 54  ithout the _BKPT
83060 20 73 75 66 66 69 78 2e 20 20 54 68 65 73 65 20   suffix.  These 
83070 6d 61 63 72 6f 73 20 69 6e 76 6f 6b 65 0a 2a 2a  macros invoke.**
83080 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 72   routines that r
83090 65 70 6f 72 74 20 74 68 65 20 6c 69 6e 65 2d 6e  eport the line-n
830a0 75 6d 62 65 72 20 6f 6e 20 77 68 69 63 68 20 74  umber on which t
830b0 68 65 20 65 72 72 6f 72 20 6f 72 69 67 69 6e 61  he error origina
830c0 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c  ted.** using sql
830d0 69 74 65 33 5f 6c 6f 67 28 29 2e 20 20 54 68 65  ite3_log().  The
830e0 20 72 6f 75 74 69 6e 65 73 20 61 6c 73 6f 20 70   routines also p
830f0 72 6f 76 69 64 65 20 61 20 63 6f 6e 76 65 6e 69  rovide a conveni
83100 65 6e 74 20 70 6c 61 63 65 0a 2a 2a 20 74 6f 20  ent place.** to 
83110 73 65 74 20 61 20 64 65 62 75 67 67 65 72 20 62  set a debugger b
83120 72 65 61 6b 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51  reakpoint..*/.SQ
83130 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
83140 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 45   sqlite3CorruptE
83150 72 72 6f 72 28 69 6e 74 29 3b 0a 53 51 4c 49 54  rror(int);.SQLIT
83160 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
83170 6c 69 74 65 33 4d 69 73 75 73 65 45 72 72 6f 72  lite3MisuseError
83180 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  (int);.SQLITE_PR
83190 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
831a0 33 43 61 6e 74 6f 70 65 6e 45 72 72 6f 72 28 69  3CantopenError(i
831b0 6e 74 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c  nt);.#define SQL
831c0 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
831d0 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 45   sqlite3CorruptE
831e0 72 72 6f 72 28 5f 5f 4c 49 4e 45 5f 5f 29 0a 23  rror(__LINE__).#
831f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49  define SQLITE_MI
83200 53 55 53 45 5f 42 4b 50 54 20 73 71 6c 69 74 65  SUSE_BKPT sqlite
83210 33 4d 69 73 75 73 65 45 72 72 6f 72 28 5f 5f 4c  3MisuseError(__L
83220 49 4e 45 5f 5f 29 0a 23 64 65 66 69 6e 65 20 53  INE__).#define S
83230 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5f 42  QLITE_CANTOPEN_B
83240 4b 50 54 20 73 71 6c 69 74 65 33 43 61 6e 74 6f  KPT sqlite3Canto
83250 70 65 6e 45 72 72 6f 72 28 5f 5f 4c 49 4e 45 5f  penError(__LINE_
83260 5f 29 0a 0a 0a 2f 2a 0a 2a 2a 20 46 54 53 34 20  _).../*.** FTS4 
83270 69 73 20 72 65 61 6c 6c 79 20 61 6e 20 65 78 74  is really an ext
83280 65 6e 73 69 6f 6e 20 66 6f 72 20 46 54 53 33 2e  ension for FTS3.
83290 20 20 49 74 20 69 73 20 65 6e 61 62 6c 65 64 20    It is enabled 
832a0 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c  using the.** SQL
832b0 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20  ITE_ENABLE_FTS3 
832c0 6d 61 63 72 6f 2e 20 20 42 75 74 20 74 6f 20 61  macro.  But to a
832d0 76 6f 69 64 20 63 6f 6e 66 75 73 69 6f 6e 20 77  void confusion w
832e0 65 20 61 6c 73 6f 20 61 6c 6c 0a 2a 2a 20 74 68  e also all.** th
832f0 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  e SQLITE_ENABLE_
83300 46 54 53 34 20 6d 61 63 72 6f 20 74 6f 20 73 65  FTS4 macro to se
83310 72 76 65 20 61 73 20 61 6e 20 61 6c 69 73 73 65  rve as an alisse
83320 20 66 6f 72 20 53 51 4c 49 54 45 5f 45 4e 41 42   for SQLITE_ENAB
83330 4c 45 5f 46 54 53 33 2e 0a 2a 2f 0a 23 69 66 20  LE_FTS3..*/.#if 
83340 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
83350 4e 41 42 4c 45 5f 46 54 53 34 29 20 26 26 20 21  NABLE_FTS4) && !
83360 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
83370 4e 41 42 4c 45 5f 46 54 53 33 29 0a 23 20 64 65  NABLE_FTS3).# de
83380 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  fine SQLITE_ENAB
83390 4c 45 5f 46 54 53 33 0a 23 65 6e 64 69 66 0a 0a  LE_FTS3.#endif..
833a0 2f 2a 0a 2a 2a 20 54 68 65 20 63 74 79 70 65 2e  /*.** The ctype.
833b0 68 20 68 65 61 64 65 72 20 69 73 20 6e 65 65 64  h header is need
833c0 65 64 20 66 6f 72 20 6e 6f 6e 2d 41 53 43 49 49  ed for non-ASCII
833d0 20 73 79 73 74 65 6d 73 2e 20 20 49 74 20 69 73   systems.  It is
833e0 20 61 6c 73 6f 0a 2a 2a 20 6e 65 65 64 65 64 20   also.** needed 
833f0 62 79 20 46 54 53 33 20 77 68 65 6e 20 46 54 53  by FTS3 when FTS
83400 33 20 69 73 20 69 6e 63 6c 75 64 65 64 20 69 6e  3 is included in
83410 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f   the amalgamatio
83420 6e 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  n..*/.#if !defin
83430 65 64 28 53 51 4c 49 54 45 5f 41 53 43 49 49 29  ed(SQLITE_ASCII)
83440 20 7c 7c 20 5c 0a 20 20 20 20 28 64 65 66 69 6e   || \.    (defin
83450 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
83460 5f 46 54 53 33 29 20 26 26 20 64 65 66 69 6e 65  _FTS3) && define
83470 64 28 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d  d(SQLITE_AMALGAM
83480 41 54 49 4f 4e 29 29 0a 23 20 69 6e 63 6c 75 64  ATION)).# includ
83490 65 20 3c 63 74 79 70 65 2e 68 3e 0a 23 65 6e 64  e <ctype.h>.#end
834a0 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  if../*.** The fo
834b0 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d  llowing macros m
834c0 69 6d 69 63 20 74 68 65 20 73 74 61 6e 64 61 72  imic the standar
834d0 64 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69  d library functi
834e0 6f 6e 73 20 74 6f 75 70 70 65 72 28 29 2c 0a 2a  ons toupper(),.*
834f0 2a 20 69 73 73 70 61 63 65 28 29 2c 20 69 73 61  * isspace(), isa
83500 6c 6e 75 6d 28 29 2c 20 69 73 64 69 67 69 74 28  lnum(), isdigit(
83510 29 20 61 6e 64 20 69 73 78 64 69 67 69 74 28 29  ) and isxdigit()
83520 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20  , respectively. 
83530 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 20 76 65  The.** sqlite ve
83540 72 73 69 6f 6e 73 20 6f 6e 6c 79 20 77 6f 72 6b  rsions only work
83550 20 66 6f 72 20 41 53 43 49 49 20 63 68 61 72 61   for ASCII chara
83560 63 74 65 72 73 2c 20 72 65 67 61 72 64 6c 65 73  cters, regardles
83570 73 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a  s of locale..*/.
83580 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53  #ifdef SQLITE_AS
83590 43 49 49 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  CII.# define sql
835a0 69 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20  ite3Toupper(x)  
835b0 28 28 78 29 26 7e 28 73 71 6c 69 74 65 33 43 74  ((x)&~(sqlite3Ct
835c0 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64  ypeMap[(unsigned
835d0 20 63 68 61 72 29 28 78 29 5d 26 30 78 32 30 29   char)(x)]&0x20)
835e0 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
835f0 65 33 49 73 73 70 61 63 65 28 78 29 20 20 20 28  e3Isspace(x)   (
83600 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b  sqlite3CtypeMap[
83610 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28  (unsigned char)(
83620 78 29 5d 26 30 78 30 31 29 0a 23 20 64 65 66 69  x)]&0x01).# defi
83630 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75  ne sqlite3Isalnu
83640 6d 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43  m(x)   (sqlite3C
83650 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65  typeMap[(unsigne
83660 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 36  d char)(x)]&0x06
83670 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
83680 65 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 28  e3Isalpha(x)   (
83690 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b  sqlite3CtypeMap[
836a0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28  (unsigned char)(
836b0 78 29 5d 26 30 78 30 32 29 0a 23 20 64 65 66 69  x)]&0x02).# defi
836c0 6e 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69  ne sqlite3Isdigi
836d0 74 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43  t(x)   (sqlite3C
836e0 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65  typeMap[(unsigne
836f0 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 34  d char)(x)]&0x04
83700 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
83710 65 33 49 73 78 64 69 67 69 74 28 78 29 20 20 28  e3Isxdigit(x)  (
83720 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b  sqlite3CtypeMap[
83730 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28  (unsigned char)(
83740 78 29 5d 26 30 78 30 38 29 0a 23 20 64 65 66 69  x)]&0x08).# defi
83750 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65  ne sqlite3Tolowe
83760 72 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 55  r(x)   (sqlite3U
83770 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73  pperToLower[(uns
83780 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 29  igned char)(x)])
83790 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
837a0 73 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 78  sqlite3Toupper(x
837b0 29 20 20 20 74 6f 75 70 70 65 72 28 28 75 6e 73  )   toupper((uns
837c0 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a  igned char)(x)).
837d0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
837e0 49 73 73 70 61 63 65 28 78 29 20 20 20 69 73 73  Isspace(x)   iss
837f0 70 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63  pace((unsigned c
83800 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e  har)(x)).# defin
83810 65 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d  e sqlite3Isalnum
83820 28 78 29 20 20 20 69 73 61 6c 6e 75 6d 28 28 75  (x)   isalnum((u
83830 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29  nsigned char)(x)
83840 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
83850 65 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 69  e3Isalpha(x)   i
83860 73 61 6c 70 68 61 28 28 75 6e 73 69 67 6e 65 64  salpha((unsigned
83870 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66   char)(x)).# def
83880 69 6e 65 20 73 71 6c 69 74 65 33 49 73 64 69 67  ine sqlite3Isdig
83890 69 74 28 78 29 20 20 20 69 73 64 69 67 69 74 28  it(x)   isdigit(
838a0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28  (unsigned char)(
838b0 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  x)).# define sql
838c0 69 74 65 33 49 73 78 64 69 67 69 74 28 78 29 20  ite3Isxdigit(x) 
838d0 20 69 73 78 64 69 67 69 74 28 28 75 6e 73 69 67   isxdigit((unsig
838e0 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20  ned char)(x)).# 
838f0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f  define sqlite3To
83900 6c 6f 77 65 72 28 78 29 20 20 20 74 6f 6c 6f 77  lower(x)   tolow
83910 65 72 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  er((unsigned cha
83920 72 29 28 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f  r)(x)).#endif../
83930 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75  *.** Internal fu
83940 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65  nction prototype
83950 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c  s.*/.#define sql
83960 69 74 65 33 53 74 72 49 43 6d 70 20 73 71 6c 69  ite3StrICmp sqli
83970 74 65 33 5f 73 74 72 69 63 6d 70 0a 53 51 4c 49  te3_stricmp.SQLI
83980 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
83990 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63  qlite3Strlen30(c
839a0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 64 65  onst char*);.#de
839b0 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 72 4e  fine sqlite3StrN
839c0 49 43 6d 70 20 73 71 6c 69 74 65 33 5f 73 74 72  ICmp sqlite3_str
839d0 6e 69 63 6d 70 0a 0a 53 51 4c 49 54 45 5f 50 52  nicmp..SQLITE_PR
839e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
839f0 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64  3MallocInit(void
83a00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
83a10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61  E void sqlite3Ma
83a20 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b 0a 53  llocEnd(void);.S
83a30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
83a40 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  id *sqlite3Mallo
83a50 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  c(int);.SQLITE_P
83a60 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
83a70 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69  ite3MallocZero(i
83a80 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
83a90 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
83aa0 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71  3DbMallocZero(sq
83ab0 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51  lite3*, int);.SQ
83ac0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
83ad0 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  d *sqlite3DbMall
83ae0 6f 63 52 61 77 28 73 71 6c 69 74 65 33 2a 2c 20  ocRaw(sqlite3*, 
83af0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
83b00 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
83b10 65 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 74  e3DbStrDup(sqlit
83b20 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  e3*,const char*)
83b30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
83b40 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62   char *sqlite3Db
83b50 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 2a  StrNDup(sqlite3*
83b60 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e  ,const char*, in
83b70 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
83b80 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
83b90 52 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69  Realloc(void*, i
83ba0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
83bb0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
83bc0 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  3DbReallocOrFree
83bd0 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64  (sqlite3 *, void
83be0 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   *, int);.SQLITE
83bf0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
83c00 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28  qlite3DbRealloc(
83c10 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20  sqlite3 *, void 
83c20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
83c30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
83c40 69 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74  ite3DbFree(sqlit
83c50 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c  e3*, void*);.SQL
83c60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
83c70 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a  sqlite3MallocSiz
83c80 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  e(void*);.SQLITE
83c90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
83ca0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65  ite3DbMallocSize
83cb0 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a  (sqlite3*, void*
83cc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
83cd0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 53  E void *sqlite3S
83ce0 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74  cratchMalloc(int
83cf0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
83d00 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63  E void sqlite3Sc
83d10 72 61 74 63 68 46 72 65 65 28 76 6f 69 64 2a 29  ratchFree(void*)
83d20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
83d30 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61   void *sqlite3Pa
83d40 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53  geMalloc(int);.S
83d50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
83d60 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 46 72  id sqlite3PageFr
83d70 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54  ee(void*);.SQLIT
83d80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
83d90 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61  qlite3MemSetDefa
83da0 75 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  ult(void);.SQLIT
83db0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
83dc0 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c  qlite3BenignMall
83dd0 6f 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29  ocHooks(void (*)
83de0 28 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 2a 29  (void), void (*)
83df0 28 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f  (void));.SQLITE_
83e00 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
83e10 74 65 33 48 65 61 70 4e 65 61 72 6c 79 46 75 6c  te3HeapNearlyFul
83e20 6c 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20  l(void);../*.** 
83e30 4f 6e 20 73 79 73 74 65 6d 73 20 77 69 74 68 20  On systems with 
83e40 61 6d 70 6c 65 20 73 74 61 63 6b 20 73 70 61 63  ample stack spac
83e50 65 20 61 6e 64 20 74 68 61 74 20 73 75 70 70 6f  e and that suppo
83e60 72 74 20 61 6c 6c 6f 63 61 28 29 2c 20 6d 61 6b  rt alloca(), mak
83e70 65 0a 2a 2a 20 75 73 65 20 6f 66 20 61 6c 6c 6f  e.** use of allo
83e80 63 61 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 73  ca() to obtain s
83e90 70 61 63 65 20 66 6f 72 20 6c 61 72 67 65 20 61  pace for large a
83ea0 75 74 6f 6d 61 74 69 63 20 6f 62 6a 65 63 74 73  utomatic objects
83eb0 2e 20 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a  .  By default,.*
83ec0 2a 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66  * obtain space f
83ed0 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a  rom malloc()..**
83ee0 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61 28 29  .** The alloca()
83ef0 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 72   routine never r
83f00 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 20 20 54 68  eturns NULL.  Th
83f10 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20 63 6f  is will cause co
83f20 64 65 20 70 61 74 68 73 0a 2a 2a 20 74 68 61 74  de paths.** that
83f30 20 64 65 61 6c 20 77 69 74 68 20 73 71 6c 69 74   deal with sqlit
83f40 65 33 53 74 61 63 6b 41 6c 6c 6f 63 28 29 20 66  e3StackAlloc() f
83f50 61 69 6c 75 72 65 73 20 74 6f 20 62 65 20 75 6e  ailures to be un
83f60 72 65 61 63 68 61 62 6c 65 2e 0a 2a 2f 0a 23 69  reachable..*/.#i
83f70 66 64 65 66 20 53 51 4c 49 54 45 5f 55 53 45 5f  fdef SQLITE_USE_
83f80 41 4c 4c 4f 43 41 0a 23 20 64 65 66 69 6e 65 20  ALLOCA.# define 
83f90 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f  sqlite3StackAllo
83fa0 63 52 61 77 28 44 2c 4e 29 20 20 20 61 6c 6c 6f  cRaw(D,N)   allo
83fb0 63 61 28 4e 29 0a 23 20 64 65 66 69 6e 65 20 73  ca(N).# define s
83fc0 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63  qlite3StackAlloc
83fd0 5a 65 72 6f 28 44 2c 4e 29 20 20 6d 65 6d 73 65  Zero(D,N)  memse
83fe0 74 28 61 6c 6c 6f 63 61 28 4e 29 2c 20 30 2c 20  t(alloca(N), 0, 
83ff0 4e 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  N).# define sqli
84000 74 65 33 53 74 61 63 6b 46 72 65 65 28 44 2c 50  te3StackFree(D,P
84010 29 20 20 20 20 20 20 20 0a 23 65 6c 73 65 0a 23  )       .#else.#
84020 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53   define sqlite3S
84030 74 61 63 6b 41 6c 6c 6f 63 52 61 77 28 44 2c 4e  tackAllocRaw(D,N
84040 29 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  )   sqlite3DbMal
84050 6c 6f 63 52 61 77 28 44 2c 4e 29 0a 23 20 64 65  locRaw(D,N).# de
84060 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63  fine sqlite3Stac
84070 6b 41 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20  kAllocZero(D,N) 
84080 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
84090 5a 65 72 6f 28 44 2c 4e 29 0a 23 20 64 65 66 69  Zero(D,N).# defi
840a0 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46  ne sqlite3StackF
840b0 72 65 65 28 44 2c 50 29 20 20 20 20 20 20 20 73  ree(D,P)       s
840c0 71 6c 69 74 65 33 44 62 46 72 65 65 28 44 2c 50  qlite3DbFree(D,P
840d0 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ).#endif..#ifdef
840e0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
840f0 45 4d 53 59 53 33 0a 53 51 4c 49 54 45 5f 50 52  EMSYS3.SQLITE_PR
84100 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69  IVATE const sqli
84110 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20  te3_mem_methods 
84120 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65  *sqlite3MemGetMe
84130 6d 73 79 73 33 28 76 6f 69 64 29 3b 0a 23 65 6e  msys3(void);.#en
84140 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
84150 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35  E_ENABLE_MEMSYS5
84160 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
84170 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65  const sqlite3_me
84180 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74  m_methods *sqlit
84190 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28  e3MemGetMemsys5(
841a0 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a  void);.#endif...
841b0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
841c0 55 54 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54 45  UTEX_OMIT.SQLITE
841d0 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74  _PRIVATE   sqlit
841e0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
841f0 20 63 6f 6e 73 74 20 2a 73 71 6c 69 74 65 33 44   const *sqlite3D
84200 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64  efaultMutex(void
84210 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
84220 45 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  E   sqlite3_mute
84230 78 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20  x_methods const 
84240 2a 73 71 6c 69 74 65 33 4e 6f 6f 70 4d 75 74 65  *sqlite3NoopMute
84250 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  x(void);.SQLITE_
84260 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65  PRIVATE   sqlite
84270 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33  3_mutex *sqlite3
84280 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 29 3b  MutexAlloc(int);
84290 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
842a0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74    int sqlite3Mut
842b0 65 78 49 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51  exInit(void);.SQ
842c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
842d0 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45  nt sqlite3MutexE
842e0 6e 64 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66  nd(void);.#endif
842f0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
84300 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74   int sqlite3Stat
84310 75 73 56 61 6c 75 65 28 69 6e 74 29 3b 0a 53 51  usValue(int);.SQ
84320 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
84330 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41  d sqlite3StatusA
84340 64 64 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  dd(int, int);.SQ
84350 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
84360 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53  d sqlite3StatusS
84370 65 74 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 23  et(int, int);..#
84380 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
84390 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
843a0 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
843b0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73     int sqlite3Is
843c0 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a 23 65 6c  NaN(double);.#el
843d0 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  se.# define sqli
843e0 74 65 33 49 73 4e 61 4e 28 58 29 20 20 30 0a 23  te3IsNaN(X)  0.#
843f0 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
84400 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
84410 65 33 56 58 50 72 69 6e 74 66 28 53 74 72 41 63  e3VXPrintf(StrAc
84420 63 75 6d 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  cum*, int, const
84430 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29   char*, va_list)
84440 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
84450 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 53 51 4c 49  _OMIT_TRACE.SQLI
84460 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
84470 73 71 6c 69 74 65 33 58 50 72 69 6e 74 66 28 53  sqlite3XPrintf(S
84480 74 72 41 63 63 75 6d 2a 2c 20 63 6f 6e 73 74 20  trAccum*, const 
84490 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 65 6e  char*, ...);.#en
844a0 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
844b0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
844c0 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 2a  MPrintf(sqlite3*
844d0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e  ,const char*, ..
844e0 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .);.SQLITE_PRIVA
844f0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
84500 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33  VMPrintf(sqlite3
84510 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76  *,const char*, v
84520 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f  a_list);.SQLITE_
84530 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
84540 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 73 71  lite3MAppendf(sq
84550 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2c 63 6f 6e  lite3*,char*,con
84560 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23  st char*,...);.#
84570 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
84580 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e  E_TEST) || defin
84590 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
845a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
845b0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65    void sqlite3De
845c0 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73 74 20  bugPrintf(const 
845d0 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 65 6e  char*, ...);.#en
845e0 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28  dif.#if defined(
845f0 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 53 51 4c  SQLITE_TEST).SQL
84600 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
84610 69 64 20 2a 73 71 6c 69 74 65 33 54 65 73 74 54  id *sqlite3TestT
84620 65 78 74 54 6f 50 74 72 28 63 6f 6e 73 74 20 63  extToPtr(const c
84630 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f  har*);.#endif../
84640 2a 20 4f 75 74 70 75 74 20 66 6f 72 6d 61 74 74  * Output formatt
84650 69 6e 67 20 66 6f 72 20 53 51 4c 49 54 45 5f 54  ing for SQLITE_T
84660 45 53 54 43 54 52 4c 5f 45 58 50 4c 41 49 4e 20  ESTCTRL_EXPLAIN 
84670 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
84680 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 54 52 45  QLITE_ENABLE_TRE
84690 45 5f 45 58 50 4c 41 49 4e 29 0a 53 51 4c 49 54  E_EXPLAIN).SQLIT
846a0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
846b0 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 42   sqlite3ExplainB
846c0 65 67 69 6e 28 56 64 62 65 2a 29 3b 0a 53 51 4c  egin(Vdbe*);.SQL
846d0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
846e0 69 64 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69  id sqlite3Explai
846f0 6e 50 72 69 6e 74 66 28 56 64 62 65 2a 2c 20 63  nPrintf(Vdbe*, c
84700 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
84710 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
84720 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45     void sqlite3E
84730 78 70 6c 61 69 6e 4e 4c 28 56 64 62 65 2a 29 3b  xplainNL(Vdbe*);
84740 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
84750 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78    void sqlite3Ex
84760 70 6c 61 69 6e 50 75 73 68 28 56 64 62 65 2a 29  plainPush(Vdbe*)
84770 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
84780 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45     void sqlite3E
84790 78 70 6c 61 69 6e 50 6f 70 28 56 64 62 65 2a 29  xplainPop(Vdbe*)
847a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
847b0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45     void sqlite3E
847c0 78 70 6c 61 69 6e 46 69 6e 69 73 68 28 56 64 62  xplainFinish(Vdb
847d0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
847e0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
847f0 65 33 45 78 70 6c 61 69 6e 53 65 6c 65 63 74 28  e3ExplainSelect(
84800 56 64 62 65 2a 2c 20 53 65 6c 65 63 74 2a 29 3b  Vdbe*, Select*);
84810 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
84820 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78    void sqlite3Ex
84830 70 6c 61 69 6e 45 78 70 72 28 56 64 62 65 2a 2c  plainExpr(Vdbe*,
84840 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
84850 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
84860 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70  qlite3ExplainExp
84870 72 4c 69 73 74 28 56 64 62 65 2a 2c 20 45 78 70  rList(Vdbe*, Exp
84880 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  rList*);.SQLITE_
84890 50 52 49 56 41 54 45 20 20 20 63 6f 6e 73 74 20  PRIVATE   const 
848a0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 64 62  char *sqlite3Vdb
848b0 65 45 78 70 6c 61 6e 61 74 69 6f 6e 28 56 64 62  eExplanation(Vdb
848c0 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  e*);.#else.# def
848d0 69 6e 65 20 73 71 6c 69 74 65 33 45 78 70 6c 61  ine sqlite3Expla
848e0 69 6e 42 65 67 69 6e 28 58 29 0a 23 20 64 65 66  inBegin(X).# def
848f0 69 6e 65 20 73 71 6c 69 74 65 33 45 78 70 6c 61  ine sqlite3Expla
84900 69 6e 53 65 6c 65 63 74 28 41 2c 42 29 0a 23 20  inSelect(A,B).# 
84910 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 78  define sqlite3Ex
84920 70 6c 61 69 6e 45 78 70 72 28 41 2c 42 29 0a 23  plainExpr(A,B).#
84930 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45   define sqlite3E
84940 78 70 6c 61 69 6e 45 78 70 72 4c 69 73 74 28 41  xplainExprList(A
84950 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ,B).# define sql
84960 69 74 65 33 45 78 70 6c 61 69 6e 46 69 6e 69 73  ite3ExplainFinis
84970 68 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71  h(X).# define sq
84980 6c 69 74 65 33 56 64 62 65 45 78 70 6c 61 6e 61  lite3VdbeExplana
84990 74 69 6f 6e 28 58 29 20 30 0a 23 65 6e 64 69 66  tion(X) 0.#endif
849a0 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ...SQLITE_PRIVAT
849b0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
849c0 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c  tString(char **,
849d0 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74   sqlite3*, const
849e0 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51   char*, ...);.SQ
849f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
84a00 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73  d sqlite3ErrorMs
84a10 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20  g(Parse*, const 
84a20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c  char*, ...);.SQL
84a30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
84a40 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63  sqlite3Dequote(c
84a50 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
84a60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
84a70 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e  3KeywordCode(con
84a80 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
84a90 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
84aa0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
84ab0 74 65 33 52 75 6e 50 61 72 73 65 72 28 50 61 72  te3RunParser(Par
84ac0 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  se*, const char*
84ad0 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49  , char **);.SQLI
84ae0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
84af0 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64  sqlite3FinishCod
84b00 69 6e 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  ing(Parse*);.SQL
84b10 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
84b20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
84b30 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54  g(Parse*);.SQLIT
84b40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
84b50 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
84b60 70 52 65 67 28 50 61 72 73 65 2a 2c 69 6e 74 29  pReg(Parse*,int)
84b70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
84b80 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54   int sqlite3GetT
84b90 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 2a 2c  empRange(Parse*,
84ba0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
84bb0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
84bc0 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
84bd0 65 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74  e(Parse*,int,int
84be0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
84bf0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c  E void sqlite3Cl
84c00 65 61 72 54 65 6d 70 52 65 67 43 61 63 68 65 28  earTempRegCache(
84c10 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
84c20 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
84c30 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 73  lite3ExprAlloc(s
84c40 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73  qlite3*,int,cons
84c50 74 20 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53  t Token*,int);.S
84c60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
84c70 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28  pr *sqlite3Expr(
84c80 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e  sqlite3*,int,con
84c90 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
84ca0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
84cb0 71 6c 69 74 65 33 45 78 70 72 41 74 74 61 63 68  qlite3ExprAttach
84cc0 53 75 62 74 72 65 65 73 28 73 71 6c 69 74 65 33  Subtrees(sqlite3
84cd0 2a 2c 45 78 70 72 2a 2c 45 78 70 72 2a 2c 45 78  *,Expr*,Expr*,Ex
84ce0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
84cf0 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
84d00 65 33 50 45 78 70 72 28 50 61 72 73 65 2a 2c 20  e3PExpr(Parse*, 
84d10 69 6e 74 2c 20 45 78 70 72 2a 2c 20 45 78 70 72  int, Expr*, Expr
84d20 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29  *, const Token*)
84d30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
84d40 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
84d50 70 72 41 6e 64 28 73 71 6c 69 74 65 33 2a 2c 45  prAnd(sqlite3*,E
84d60 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  xpr*, Expr*);.SQ
84d70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
84d80 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 46 75  r *sqlite3ExprFu
84d90 6e 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 45 78  nction(Parse*,Ex
84da0 70 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29  prList*, Token*)
84db0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
84dc0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
84dd0 72 41 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72  rAssignVarNumber
84de0 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b  (Parse*, Expr*);
84df0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
84e00 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
84e10 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c  Delete(sqlite3*,
84e20 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
84e30 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74  PRIVATE ExprList
84e40 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73   *sqlite3ExprLis
84e50 74 41 70 70 65 6e 64 28 50 61 72 73 65 2a 2c 45  tAppend(Parse*,E
84e60 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 2a 29 3b  xprList*,Expr*);
84e70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
84e80 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
84e90 4c 69 73 74 53 65 74 4e 61 6d 65 28 50 61 72 73  ListSetName(Pars
84ea0 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 54 6f 6b  e*,ExprList*,Tok
84eb0 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  en*,int);.SQLITE
84ec0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
84ed0 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74  lite3ExprListSet
84ee0 53 70 61 6e 28 50 61 72 73 65 2a 2c 45 78 70 72  Span(Parse*,Expr
84ef0 4c 69 73 74 2a 2c 45 78 70 72 53 70 61 6e 2a 29  List*,ExprSpan*)
84f00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
84f10 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
84f20 72 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69  rListDelete(sqli
84f30 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29  te3*, ExprList*)
84f40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
84f50 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74   int sqlite3Init
84f60 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 61 72 2a  (sqlite3*, char*
84f70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
84f80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e  TE int sqlite3In
84f90 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 2a  itCallback(void*
84fa0 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 63  , int, char**, c
84fb0 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  har**);.SQLITE_P
84fc0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
84fd0 74 65 33 50 72 61 67 6d 61 28 50 61 72 73 65 2a  te3Pragma(Parse*
84fe0 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54  ,Token*,Token*,T
84ff0 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  oken*,int);.SQLI
85000 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
85010 73 71 6c 69 74 65 33 52 65 73 65 74 41 6c 6c 53  sqlite3ResetAllS
85020 63 68 65 6d 61 73 4f 66 43 6f 6e 6e 65 63 74 69  chemasOfConnecti
85030 6f 6e 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  on(sqlite3*);.SQ
85040 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
85050 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 4f 6e  d sqlite3ResetOn
85060 65 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33 2a  eSchema(sqlite3*
85070 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
85080 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
85090 65 33 43 6f 6c 6c 61 70 73 65 44 61 74 61 62 61  e3CollapseDataba
850a0 73 65 41 72 72 61 79 28 73 71 6c 69 74 65 33 2a  seArray(sqlite3*
850b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
850c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65  E void sqlite3Be
850d0 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c  ginParse(Parse*,
850e0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
850f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
85100 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43  3CommitInternalC
85110 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29  hanges(sqlite3*)
85120 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
85130 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 52   Table *sqlite3R
85140 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74  esultSetOfSelect
85150 28 50 61 72 73 65 2a 2c 53 65 6c 65 63 74 2a 29  (Parse*,Select*)
85160 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
85170 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65   void sqlite3Ope
85180 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 50 61 72  nMasterTable(Par
85190 73 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  se *, int);.SQLI
851a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
851b0 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
851c0 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c  e(Parse*,Token*,
851d0 54 6f 6b 65 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 69  Token*,int,int,i
851e0 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  nt,int);.SQLITE_
851f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
85200 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28 50 61  ite3AddColumn(Pa
85210 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51  rse*,Token*);.SQ
85220 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
85230 64 20 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e  d sqlite3AddNotN
85240 75 6c 6c 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  ull(Parse*, int)
85250 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
85260 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
85270 50 72 69 6d 61 72 79 4b 65 79 28 50 61 72 73 65  PrimaryKey(Parse
85280 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e  *, ExprList*, in
85290 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  t, int, int);.SQ
852a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
852b0 64 20 73 71 6c 69 74 65 33 41 64 64 43 68 65 63  d sqlite3AddChec
852c0 6b 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 72 73  kConstraint(Pars
852d0 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49  e*, Expr*);.SQLI
852e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
852f0 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
85300 54 79 70 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65  Type(Parse*,Toke
85310 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
85320 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
85330 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28  AddDefaultValue(
85340 50 61 72 73 65 2a 2c 45 78 70 72 53 70 61 6e 2a  Parse*,ExprSpan*
85350 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
85360 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
85370 64 43 6f 6c 6c 61 74 65 54 79 70 65 28 50 61 72  dCollateType(Par
85380 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  se*, Token*);.SQ
85390 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
853a0 64 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c  d sqlite3EndTabl
853b0 65 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c  e(Parse*,Token*,
853c0 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a 29 3b  Token*,Select*);
853d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
853e0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 72 73 65  int sqlite3Parse
853f0 55 72 69 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  Uri(const char*,
85400 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 73 69  const char*,unsi
85410 67 6e 65 64 20 69 6e 74 2a 2c 0a 20 20 20 20 20  gned int*,.     
85420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
85430 71 6c 69 74 65 33 5f 76 66 73 2a 2a 2c 63 68 61  qlite3_vfs**,cha
85440 72 2a 2a 2c 63 68 61 72 20 2a 2a 29 3b 0a 53 51  r**,char **);.SQ
85450 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 74 72  LITE_PRIVATE Btr
85460 65 65 20 2a 73 71 6c 69 74 65 33 44 62 4e 61 6d  ee *sqlite3DbNam
85470 65 54 6f 42 74 72 65 65 28 73 71 6c 69 74 65 33  eToBtree(sqlite3
85480 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  *,const char*);.
85490 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
854a0 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 4f 6e  nt sqlite3CodeOn
854b0 63 65 28 50 61 72 73 65 20 2a 29 3b 0a 0a 53 51  ce(Parse *);..SQ
854c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 69 74  LITE_PRIVATE Bit
854d0 76 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76  vec *sqlite3Bitv
854e0 65 63 43 72 65 61 74 65 28 75 33 32 29 3b 0a 53  ecCreate(u32);.S
854f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
85500 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54  t sqlite3BitvecT
85510 65 73 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32  est(Bitvec*, u32
85520 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
85530 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74  E int sqlite3Bit
85540 76 65 63 53 65 74 28 42 69 74 76 65 63 2a 2c 20  vecSet(Bitvec*, 
85550 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  u32);.SQLITE_PRI
85560 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
85570 33 42 69 74 76 65 63 43 6c 65 61 72 28 42 69 74  3BitvecClear(Bit
85580 76 65 63 2a 2c 20 75 33 32 2c 20 76 6f 69 64 2a  vec*, u32, void*
85590 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
855a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69  E void sqlite3Bi
855b0 74 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76  tvecDestroy(Bitv
855c0 65 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ec*);.SQLITE_PRI
855d0 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
855e0 42 69 74 76 65 63 53 69 7a 65 28 42 69 74 76 65  BitvecSize(Bitve
855f0 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  c*);.SQLITE_PRIV
85600 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
85610 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74  itvecBuiltinTest
85620 28 69 6e 74 2c 69 6e 74 2a 29 3b 0a 0a 53 51 4c  (int,int*);..SQL
85630 49 54 45 5f 50 52 49 56 41 54 45 20 52 6f 77 53  ITE_PRIVATE RowS
85640 65 74 20 2a 73 71 6c 69 74 65 33 52 6f 77 53 65  et *sqlite3RowSe
85650 74 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20  tInit(sqlite3*, 
85660 76 6f 69 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20  void*, unsigned 
85670 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
85680 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
85690 33 52 6f 77 53 65 74 43 6c 65 61 72 28 52 6f 77  3RowSetClear(Row
856a0 53 65 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Set*);.SQLITE_PR
856b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
856c0 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52  e3RowSetInsert(R
856d0 6f 77 53 65 74 2a 2c 20 69 36 34 29 3b 0a 53 51  owSet*, i64);.SQ
856e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
856f0 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65   sqlite3RowSetTe
85700 73 74 28 52 6f 77 53 65 74 2a 2c 20 75 38 20 69  st(RowSet*, u8 i
85710 42 61 74 63 68 2c 20 69 36 34 29 3b 0a 53 51 4c  Batch, i64);.SQL
85720 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
85730 73 71 6c 69 74 65 33 52 6f 77 53 65 74 4e 65 78  sqlite3RowSetNex
85740 74 28 52 6f 77 53 65 74 2a 2c 20 69 36 34 2a 29  t(RowSet*, i64*)
85750 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
85760 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72  E void sqlite3Cr
85770 65 61 74 65 56 69 65 77 28 50 61 72 73 65 2a 2c  eateView(Parse*,
85780 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f  Token*,Token*,To
85790 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74  ken*,Select*,int
857a0 2c 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66  ,int);..#if !def
857b0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
857c0 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e  _VIEW) || !defin
857d0 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
857e0 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 53 51 4c  IRTUALTABLE).SQL
857f0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
85800 74 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  t sqlite3ViewGet
85810 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73  ColumnNames(Pars
85820 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23 65 6c 73  e*,Table*);.#els
85830 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  e.# define sqlit
85840 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
85850 61 6d 65 73 28 41 2c 42 29 20 30 0a 23 65 6e 64  ames(A,B) 0.#end
85860 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  if..SQLITE_PRIVA
85870 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
85880 72 6f 70 54 61 62 6c 65 28 50 61 72 73 65 2a 2c  ropTable(Parse*,
85890 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20   SrcList*, int, 
858a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
858b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
858c0 33 43 6f 64 65 44 72 6f 70 54 61 62 6c 65 28 50  3CodeDropTable(P
858d0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
858e0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
858f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
85900 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
85910 28 73 71 6c 69 74 65 33 2a 2c 20 54 61 62 6c 65  (sqlite3*, Table
85920 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  *);.#ifndef SQLI
85930 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
85940 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49  EMENT.SQLITE_PRI
85950 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
85960 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  te3Autoincrement
85970 42 65 67 69 6e 28 50 61 72 73 65 20 2a 70 50 61  Begin(Parse *pPa
85980 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  rse);.SQLITE_PRI
85990 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
859a0 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  te3Autoincrement
859b0 45 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72 73  End(Parse *pPars
859c0 65 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  e);.#else.# defi
859d0 6e 65 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e  ne sqlite3Autoin
859e0 63 72 65 6d 65 6e 74 42 65 67 69 6e 28 58 29 0a  crementBegin(X).
859f0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
85a00 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64  AutoincrementEnd
85a10 28 58 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  (X).#endif.SQLIT
85a20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
85a30 6c 69 74 65 33 43 6f 64 65 43 6f 72 6f 75 74 69  lite3CodeCorouti
85a40 6e 65 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63  ne(Parse*, Selec
85a50 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74 2a 29  t*, SelectDest*)
85a60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
85a70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 73   void sqlite3Ins
85a80 65 72 74 28 50 61 72 73 65 2a 2c 20 53 72 63 4c  ert(Parse*, SrcL
85a90 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c  ist*, ExprList*,
85aa0 20 53 65 6c 65 63 74 2a 2c 20 49 64 4c 69 73 74   Select*, IdList
85ab0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
85ac0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
85ad0 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61  lite3ArrayAlloca
85ae0 74 65 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64  te(sqlite3*,void
85af0 2a 2c 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 29  *,int,int*,int*)
85b00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
85b10 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33   IdList *sqlite3
85b20 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c  IdListAppend(sql
85b30 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20  ite3*, IdList*, 
85b40 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
85b50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
85b60 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49  te3IdListIndex(I
85b70 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61  dList*,const cha
85b80 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
85b90 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
85ba0 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72  ite3SrcListEnlar
85bb0 67 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63  ge(sqlite3*, Src
85bc0 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  List*, int, int)
85bd0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
85be0 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65   SrcList *sqlite
85bf0 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 73  3SrcListAppend(s
85c00 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74  qlite3*, SrcList
85c10 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
85c20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
85c30 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69  TE SrcList *sqli
85c40 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
85c50 46 72 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c  FromTerm(Parse*,
85c60 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e   SrcList*, Token
85c70 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20  *, Token*,.     
85c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
85c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
85ca0 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a   Token*, Select*
85cb0 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a  , Expr*, IdList*
85cc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
85cd0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72  E void sqlite3Sr
85ce0 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 50  cListIndexedBy(P
85cf0 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20  arse *, SrcList 
85d00 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c  *, Token *);.SQL
85d10 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
85d20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79  sqlite3IndexedBy
85d30 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c 20  Lookup(Parse *, 
85d40 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
85d50 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tem *);.SQLITE_P
85d60 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
85d70 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a  te3SrcListShiftJ
85d80 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a  oinType(SrcList*
85d90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
85da0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72  E void sqlite3Sr
85db0 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f  cListAssignCurso
85dc0 72 73 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  rs(Parse*, SrcLi
85dd0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
85de0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
85df0 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71  3IdListDelete(sq
85e00 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29  lite3*, IdList*)
85e10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
85e20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63   void sqlite3Src
85e30 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74  ListDelete(sqlit
85e40 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a  e3*, SrcList*);.
85e50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49  SQLITE_PRIVATE I
85e60 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 43 72 65  ndex *sqlite3Cre
85e70 61 74 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c  ateIndex(Parse*,
85e80 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72  Token*,Token*,Sr
85e90 63 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a  cList*,ExprList*
85ea0 2c 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20  ,int,Token*,.   
85eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
85ec0 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74       Token*, int
85ed0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
85ee0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
85ef0 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72  te3DropIndex(Par
85f00 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69  se*, SrcList*, i
85f10 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
85f20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
85f30 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65  elect(Parse*, Se
85f40 6c 65 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73  lect*, SelectDes
85f50 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
85f60 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69  ATE Select *sqli
85f70 74 65 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72  te3SelectNew(Par
85f80 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72  se*,ExprList*,Sr
85f90 63 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70  cList*,Expr*,Exp
85fa0 72 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20  rList*,.        
85fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
85fc0 20 45 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a   Expr*,ExprList*
85fd0 2c 69 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a  ,int,Expr*,Expr*
85fe0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
85ff0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
86000 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74  lectDelete(sqlit
86010 65 33 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53  e3*, Select*);.S
86020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61  QLITE_PRIVATE Ta
86030 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c  ble *sqlite3SrcL
86040 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a  istLookup(Parse*
86050 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c  , SrcList*);.SQL
86060 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
86070 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c  sqlite3IsReadOnl
86080 79 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a  y(Parse*, Table*
86090 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
860a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
860b0 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72  te3OpenTable(Par
860c0 73 65 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69  se*, int iCur, i
860d0 6e 74 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20  nt iDb, Table*, 
860e0 69 6e 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65  int);.#if define
860f0 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
86100 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49  UPDATE_DELETE_LI
86110 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64  MIT) && !defined
86120 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42  (SQLITE_OMIT_SUB
86130 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52  QUERY).SQLITE_PR
86140 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
86150 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61  te3LimitWhere(Pa
86160 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a  rse *, SrcList *
86170 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69  , Expr *, ExprLi
86180 73 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78  st *, Expr *, Ex
86190 70 72 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23  pr *, char *);.#
861a0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49  endif.SQLITE_PRI
861b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
861c0 33 44 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73  3DeleteFrom(Pars
861d0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78  e*, SrcList*, Ex
861e0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
861f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
86200 33 55 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20  3Update(Parse*, 
86210 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69  SrcList*, ExprLi
86220 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29  st*, Expr*, int)
86230 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
86240 20 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69   WhereInfo *sqli
86250 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 50 61  te3WhereBegin(Pa
86260 72 73 65 2a 2c 53 72 63 4c 69 73 74 2a 2c 45 78  rse*,SrcList*,Ex
86270 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 45 78  pr*,ExprList*,Ex
86280 70 72 4c 69 73 74 2a 2c 75 31 36 2c 69 6e 74 29  prList*,u16,int)
86290 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
862a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65   void sqlite3Whe
862b0 72 65 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 2a  reEnd(WhereInfo*
862c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
862d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
862e0 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 50  rCodeGetColumn(P
862f0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
86300 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 75 38  nt, int, int, u8
86310 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
86320 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
86330 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 4f  prCodeGetColumnO
86340 66 54 61 62 6c 65 28 56 64 62 65 2a 2c 20 54 61  fTable(Vdbe*, Ta
86350 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  ble*, int, int, 
86360 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
86370 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
86380 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 50 61  3ExprCodeMove(Pa
86390 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  rse*, int, int, 
863a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
863b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
863c0 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 65 28  3ExprCacheStore(
863d0 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Parse*, int, int
863e0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
863f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
86400 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68  te3ExprCachePush
86410 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45  (Parse*);.SQLITE
86420 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
86430 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f  lite3ExprCachePo
86440 70 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a  p(Parse*, int);.
86450 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
86460 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
86470 61 63 68 65 52 65 6d 6f 76 65 28 50 61 72 73 65  acheRemove(Parse
86480 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  *, int, int);.SQ
86490 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
864a0 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63  d sqlite3ExprCac
864b0 68 65 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b  heClear(Parse*);
864c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
864d0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
864e0 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
864f0 6e 67 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c  nge(Parse*, int,
86500 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
86510 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
86520 33 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 2a  3ExprCode(Parse*
86530 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53  , Expr*, int);.S
86540 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
86550 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
86560 65 54 65 6d 70 28 50 61 72 73 65 2a 2c 20 45 78  eTemp(Parse*, Ex
86570 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  pr*, int*);.SQLI
86580 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
86590 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
865a0 72 67 65 74 28 50 61 72 73 65 2a 2c 20 45 78 70  rget(Parse*, Exp
865b0 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  r*, int);.SQLITE
865c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
865d0 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43  ite3ExprCodeAndC
865e0 61 63 68 65 28 50 61 72 73 65 2a 2c 20 45 78 70  ache(Parse*, Exp
865f0 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  r*, int);.SQLITE
86600 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
86610 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 6e  lite3ExprCodeCon
86620 73 74 61 6e 74 73 28 50 61 72 73 65 2a 2c 20 45  stants(Parse*, E
86630 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
86640 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
86650 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
86660 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69  t(Parse*, ExprLi
86670 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  st*, int, int);.
86680 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
86690 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49  oid sqlite3ExprI
866a0 66 54 72 75 65 28 50 61 72 73 65 2a 2c 20 45 78  fTrue(Parse*, Ex
866b0 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  pr*, int, int);.
866c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
866d0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49  oid sqlite3ExprI
866e0 66 46 61 6c 73 65 28 50 61 72 73 65 2a 2c 20 45  fFalse(Parse*, E
866f0 78 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  xpr*, int, int);
86700 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
86710 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 46 69  Table *sqlite3Fi
86720 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a  ndTable(sqlite3*
86730 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f  ,const char*, co
86740 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
86750 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65  TE_PRIVATE Table
86760 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54   *sqlite3LocateT
86770 61 62 6c 65 28 50 61 72 73 65 2a 2c 69 6e 74 20  able(Parse*,int 
86780 69 73 56 69 65 77 2c 63 6f 6e 73 74 20 63 68 61  isView,const cha
86790 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29  r*, const char*)
867a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
867b0 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c   Table *sqlite3L
867c0 6f 63 61 74 65 54 61 62 6c 65 49 74 65 6d 28 50  ocateTableItem(P
867d0 61 72 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 77  arse*,int isView
867e0 2c 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f  ,struct SrcList_
867f0 69 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  item *);.SQLITE_
86800 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73  PRIVATE Index *s
86810 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
86820 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
86830 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  har*, const char
86840 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
86850 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
86860 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61  nlinkAndDeleteTa
86870 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  ble(sqlite3*,int
86880 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53  ,const char*);.S
86890 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
868a0 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  id sqlite3Unlink
868b0 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 73  AndDeleteIndex(s
868c0 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73  qlite3*,int,cons
868d0 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
868e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
868f0 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72 73  lite3Vacuum(Pars
86900 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
86910 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
86920 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a 2c  unVacuum(char**,
86930 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49   sqlite3*);.SQLI
86940 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
86950 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d  *sqlite3NameFrom
86960 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c 20  Token(sqlite3*, 
86970 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
86980 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
86990 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45  te3ExprCompare(E
869a0 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  xpr*, Expr*);.SQ
869b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
869c0 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
869d0 43 6f 6d 70 61 72 65 28 45 78 70 72 4c 69 73 74  Compare(ExprList
869e0 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53  *, ExprList*);.S
869f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
86a00 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e  id sqlite3ExprAn
86a10 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28  alyzeAggregates(
86a20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78  NameContext*, Ex
86a30 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
86a40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
86a50 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c  3ExprAnalyzeAggL
86a60 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a  ist(NameContext*
86a70 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c  ,ExprList*);.SQL
86a80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
86a90 73 71 6c 69 74 65 33 46 75 6e 63 74 69 6f 6e 55  sqlite3FunctionU
86aa0 73 65 73 54 68 69 73 53 72 63 28 45 78 70 72 2a  sesThisSrc(Expr*
86ab0 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c  , SrcList*);.SQL
86ac0 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65  ITE_PRIVATE Vdbe
86ad0 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65   *sqlite3GetVdbe
86ae0 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45  (Parse*);.SQLITE
86af0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
86b00 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61  lite3PrngSaveSta
86b10 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  te(void);.SQLITE
86b20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
86b30 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65  lite3PrngRestore
86b40 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c  State(void);.SQL
86b50 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
86b60 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65   sqlite3PrngRese
86b70 74 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51  tState(void);.SQ
86b80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
86b90 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63  d sqlite3Rollbac
86ba0 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a 2c 69 6e  kAll(sqlite3*,in
86bb0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
86bc0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
86bd0 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28  odeVerifySchema(
86be0 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51  Parse*, int);.SQ
86bf0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
86c00 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72  d sqlite3CodeVer
86c10 69 66 79 4e 61 6d 65 64 53 63 68 65 6d 61 28 50  ifyNamedSchema(P
86c20 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  arse*, const cha
86c30 72 20 2a 7a 44 62 29 3b 0a 53 51 4c 49 54 45 5f  r *zDb);.SQLITE_
86c40 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
86c50 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63  ite3BeginTransac
86c60 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74  tion(Parse*, int
86c70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
86c80 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
86c90 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28  mmitTransaction(
86ca0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
86cb0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
86cc0 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e  ite3RollbackTran
86cd0 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 3b  saction(Parse*);
86ce0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
86cf0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 65  void sqlite3Save
86d00 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 6e  point(Parse*, in
86d10 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  t, Token*);.SQLI
86d20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
86d30 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65  sqlite3CloseSave
86d40 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20 2a  points(sqlite3 *
86d50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
86d60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4c 65  E void sqlite3Le
86d70 61 76 65 4d 75 74 65 78 41 6e 64 43 6c 6f 73 65  aveMutexAndClose
86d80 5a 6f 6d 62 69 65 28 73 71 6c 69 74 65 33 2a 29  Zombie(sqlite3*)
86d90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
86da0 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
86db0 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72 2a  IsConstant(Expr*
86dc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
86dd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
86de0 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f  rIsConstantNotJo
86df0 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  in(Expr*);.SQLIT
86e00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
86e10 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
86e20 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78  antOrFunction(Ex
86e30 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
86e40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
86e50 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 78  ExprIsInteger(Ex
86e60 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  pr*, int*);.SQLI
86e70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
86e80 71 6c 69 74 65 33 45 78 70 72 43 61 6e 42 65 4e  qlite3ExprCanBeN
86e90 75 6c 6c 28 63 6f 6e 73 74 20 45 78 70 72 2a 29  ull(const Expr*)
86ea0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
86eb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
86ec0 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28  rCodeIsNullJump(
86ed0 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 45 78 70  Vdbe*, const Exp
86ee0 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  r*, int, int);.S
86ef0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
86f00 74 20 73 71 6c 69 74 65 33 45 78 70 72 4e 65 65  t sqlite3ExprNee
86f10 64 73 4e 6f 41 66 66 69 6e 69 74 79 43 68 61 6e  dsNoAffinityChan
86f20 67 65 28 63 6f 6e 73 74 20 45 78 70 72 2a 2c 20  ge(const Expr*, 
86f30 63 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  char);.SQLITE_PR
86f40 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
86f50 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 20 63  3IsRowid(const c
86f60 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
86f70 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
86f80 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c  e3GenerateRowDel
86f90 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61 62 6c  ete(Parse*, Tabl
86fa0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  e*, int, int, in
86fb0 74 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69 6e  t, Trigger *, in
86fc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
86fd0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47  TE void sqlite3G
86fe0 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44  enerateRowIndexD
86ff0 65 6c 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61  elete(Parse*, Ta
87000 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29  ble*, int, int*)
87010 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
87020 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 6e 65   int sqlite3Gene
87030 72 61 74 65 49 6e 64 65 78 4b 65 79 28 50 61 72  rateIndexKey(Par
87040 73 65 2a 2c 20 49 6e 64 65 78 2a 2c 20 69 6e 74  se*, Index*, int
87050 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
87060 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
87070 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
87080 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73  ConstraintChecks
87090 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 2c 69  (Parse*,Table*,i
870a0 6e 74 2c 69 6e 74 2c 0a 20 20 20 20 20 20 20 20  nt,int,.        
870b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
870c0 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
870d0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e  *,int,int,int,in
870e0 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  t,int*);.SQLITE_
870f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
87100 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65  ite3CompleteInse
87110 72 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 54 61  rtion(Parse*, Ta
87120 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  ble*, int, int, 
87130 69 6e 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  int*, int, int, 
87140 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
87150 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
87160 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69  OpenTableAndIndi
87170 63 65 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c  ces(Parse*, Tabl
87180 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  e*, int, int);.S
87190 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
871a0 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57  id sqlite3BeginW
871b0 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 50 61  riteOperation(Pa
871c0 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  rse*, int, int);
871d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
871e0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 75 6c 74  void sqlite3Mult
871f0 69 57 72 69 74 65 28 50 61 72 73 65 2a 29 3b 0a  iWrite(Parse*);.
87200 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
87210 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 79 41 62  oid sqlite3MayAb
87220 6f 72 74 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  ort(Parse*);.SQL
87230 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
87240 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73   sqlite3HaltCons
87250 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 69  traint(Parse*, i
87260 6e 74 2c 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b  nt, char*, int);
87270 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
87280 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70  Expr *sqlite3Exp
87290 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 78  rDup(sqlite3*,Ex
872a0 70 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  pr*,int);.SQLITE
872b0 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73  _PRIVATE ExprLis
872c0 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69  t *sqlite3ExprLi
872d0 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45  stDup(sqlite3*,E
872e0 78 70 72 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53  xprList*,int);.S
872f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72  QLITE_PRIVATE Sr
87300 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72  cList *sqlite3Sr
87310 63 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33  cListDup(sqlite3
87320 2a 2c 53 72 63 4c 69 73 74 2a 2c 69 6e 74 29 3b  *,SrcList*,int);
87330 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
87340 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49  IdList *sqlite3I
87350 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33  dListDup(sqlite3
87360 2a 2c 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49  *,IdList*);.SQLI
87370 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63  TE_PRIVATE Selec
87380 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74  t *sqlite3Select
87390 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c  Dup(sqlite3*,Sel
873a0 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ect*,int);.SQLIT
873b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
873c0 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73  qlite3FuncDefIns
873d0 65 72 74 28 46 75 6e 63 44 65 66 48 61 73 68 2a  ert(FuncDefHash*
873e0 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c  , FuncDef*);.SQL
873f0 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63  ITE_PRIVATE Func
87400 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64  Def *sqlite3Find
87410 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33  Function(sqlite3
87420 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  *,const char*,in
87430 74 2c 69 6e 74 2c 75 38 2c 75 38 29 3b 0a 53 51  t,int,u8,u8);.SQ
87440 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
87450 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65  d sqlite3Registe
87460 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e  rBuiltinFunction
87470 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  s(sqlite3*);.SQL
87480 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
87490 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
874a0 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e  DateTimeFunction
874b0 73 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  s(void);.SQLITE_
874c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
874d0 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62  ite3RegisterGlob
874e0 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64  alFunctions(void
874f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
87500 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66  E int sqlite3Saf
87510 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74  etyCheckOk(sqlit
87520 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e3*);.SQLITE_PRI
87530 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
87540 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f  SafetyCheckSickO
87550 72 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  rOk(sqlite3*);.S
87560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
87570 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65  id sqlite3Change
87580 43 6f 6f 6b 69 65 28 50 61 72 73 65 2a 2c 20 69  Cookie(Parse*, i
87590 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e  nt);..#if !defin
875a0 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
875b0 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 64  IEW) && !defined
875c0 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
875d0 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 49  GGER).SQLITE_PRI
875e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
875f0 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77  3MaterializeView
87600 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
87610 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 23 65   Expr*, int);.#e
87620 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
87630 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
87640 52 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  R.SQLITE_PRIVATE
87650 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
87660 65 67 69 6e 54 72 69 67 67 65 72 28 50 61 72 73  eginTrigger(Pars
87670 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  e*, Token*,Token
87680 2a 2c 69 6e 74 2c 69 6e 74 2c 49 64 4c 69 73 74  *,int,int,IdList
87690 2a 2c 53 72 63 4c 69 73 74 2a 2c 0a 20 20 20 20  *,SrcList*,.    
876a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
876b0 20 20 20 20 20 20 20 45 78 70 72 2a 2c 69 6e 74         Expr*,int
876c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
876d0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
876e0 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 67  lite3FinishTrigg
876f0 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67 67  er(Parse*, Trigg
87700 65 72 53 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a 29  erStep*, Token*)
87710 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
87720 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44     void sqlite3D
87730 72 6f 70 54 72 69 67 67 65 72 28 50 61 72 73 65  ropTrigger(Parse
87740 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74  *, SrcList*, int
87750 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
87760 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
87770 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 50  DropTriggerPtr(P
87780 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 2a 29  arse*, Trigger*)
87790 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
877a0 20 20 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69     Trigger *sqli
877b0 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74  te3TriggersExist
877c0 28 50 61 72 73 65 20 2a 2c 20 54 61 62 6c 65 2a  (Parse *, Table*
877d0 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a  , int, ExprList*
877e0 2c 20 69 6e 74 20 2a 70 4d 61 73 6b 29 3b 0a 53  , int *pMask);.S
877f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
87800 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33  Trigger *sqlite3
87810 54 72 69 67 67 65 72 4c 69 73 74 28 50 61 72 73  TriggerList(Pars
87820 65 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53  e *, Table *);.S
87830 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
87840 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65  void sqlite3Code
87850 52 6f 77 54 72 69 67 67 65 72 28 50 61 72 73 65  RowTrigger(Parse
87860 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69 6e  *, Trigger *, in
87870 74 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e  t, ExprList*, in
87880 74 2c 20 54 61 62 6c 65 20 2a 2c 0a 20 20 20 20  t, Table *,.    
87890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
878a0 20 20 20 20 20 20 20 20 69 6e 74 2c 20 69 6e 74          int, int
878b0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
878c0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
878d0 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67  lite3CodeRowTrig
878e0 67 65 72 44 69 72 65 63 74 28 50 61 72 73 65 20  gerDirect(Parse 
878f0 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 54 61  *, Trigger *, Ta
87900 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  ble *, int, int,
87910 20 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 73 71   int);.  void sq
87920 6c 69 74 65 56 69 65 77 54 72 69 67 67 65 72 73  liteViewTriggers
87930 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
87940 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70   Expr*, int, Exp
87950 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  rList*);.SQLITE_
87960 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
87970 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
87980 67 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 2a  gerStep(sqlite3*
87990 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b  , TriggerStep*);
879a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
879b0 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73    TriggerStep *s
879c0 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c  qlite3TriggerSel
879d0 65 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a  ectStep(sqlite3*
879e0 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54  ,Select*);.SQLIT
879f0 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67  E_PRIVATE   Trig
87a00 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33  gerStep *sqlite3
87a10 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65  TriggerInsertSte
87a20 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e  p(sqlite3*,Token
87a30 2a 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 20  *, IdList*,.    
87a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
87a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
87a60 20 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 65      ExprList*,Se
87a70 6c 65 63 74 2a 2c 75 38 29 3b 0a 53 51 4c 49 54  lect*,u8);.SQLIT
87a80 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67  E_PRIVATE   Trig
87a90 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33  gerStep *sqlite3
87aa0 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65  TriggerUpdateSte
87ab0 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e  p(sqlite3*,Token
87ac0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70  *,ExprList*, Exp
87ad0 72 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f  r*, u8);.SQLITE_
87ae0 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65  PRIVATE   Trigge
87af0 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
87b00 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28  iggerDeleteStep(
87b10 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c  sqlite3*,Token*,
87b20 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
87b30 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
87b40 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
87b50 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54 72  ger(sqlite3*, Tr
87b60 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  igger*);.SQLITE_
87b70 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
87b80 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
87b90 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c  eleteTrigger(sql
87ba0 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  ite3*,int,const 
87bb0 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
87bc0 52 49 56 41 54 45 20 20 20 75 33 32 20 73 71 6c  RIVATE   u32 sql
87bd0 69 74 65 33 54 72 69 67 67 65 72 43 6f 6c 6d 61  ite3TriggerColma
87be0 73 6b 28 50 61 72 73 65 2a 2c 54 72 69 67 67 65  sk(Parse*,Trigge
87bf0 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 6e 74  r*,ExprList*,int
87c00 2c 69 6e 74 2c 54 61 62 6c 65 2a 2c 69 6e 74 29  ,int,Table*,int)
87c10 3b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ;.# define sqlit
87c20 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28  e3ParseToplevel(
87c30 70 29 20 28 28 70 29 2d 3e 70 54 6f 70 6c 65 76  p) ((p)->pToplev
87c40 65 6c 20 3f 20 28 70 29 2d 3e 70 54 6f 70 6c 65  el ? (p)->pTople
87c50 76 65 6c 20 3a 20 28 70 29 29 0a 23 65 6c 73 65  vel : (p)).#else
87c60 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
87c70 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 42  3TriggersExist(B
87c80 2c 43 2c 44 2c 45 2c 46 29 20 30 0a 23 20 64 65  ,C,D,E,F) 0.# de
87c90 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 6c 65  fine sqlite3Dele
87ca0 74 65 54 72 69 67 67 65 72 28 41 2c 42 29 0a 23  teTrigger(A,B).#
87cb0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44   define sqlite3D
87cc0 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 41 2c  ropTriggerPtr(A,
87cd0 42 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  B).# define sqli
87ce0 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65  te3UnlinkAndDele
87cf0 74 65 54 72 69 67 67 65 72 28 41 2c 42 2c 43 29  teTrigger(A,B,C)
87d00 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
87d10 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
87d20 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c  A,B,C,D,E,F,G,H,
87d30 49 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  I).# define sqli
87d40 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
87d50 72 44 69 72 65 63 74 28 41 2c 42 2c 43 2c 44 2c  rDirect(A,B,C,D,
87d60 45 2c 46 29 0a 23 20 64 65 66 69 6e 65 20 73 71  E,F).# define sq
87d70 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 74  lite3TriggerList
87d80 28 58 2c 20 59 29 20 30 0a 23 20 64 65 66 69 6e  (X, Y) 0.# defin
87d90 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f  e sqlite3ParseTo
87da0 70 6c 65 76 65 6c 28 70 29 20 70 0a 23 20 64 65  plevel(p) p.# de
87db0 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67  fine sqlite3Trig
87dc0 67 65 72 43 6f 6c 6d 61 73 6b 28 41 2c 42 2c 43  gerColmask(A,B,C
87dd0 2c 44 2c 45 2c 46 2c 47 29 20 30 0a 23 65 6e 64  ,D,E,F,G) 0.#end
87de0 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  if..SQLITE_PRIVA
87df0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f  TE int sqlite3Jo
87e00 69 6e 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54  inType(Parse*, T
87e10 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54  oken*, Token*, T
87e20 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
87e30 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
87e40 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e  te3CreateForeign
87e50 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72  Key(Parse*, Expr
87e60 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 45  List*, Token*, E
87e70 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a  xprList*, int);.
87e80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
87e90 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 65 72  oid sqlite3Defer
87ea0 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73 65  ForeignKey(Parse
87eb0 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 6e 64 65 66  *, int);.#ifndef
87ec0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
87ed0 48 4f 52 49 5a 41 54 49 4f 4e 0a 53 51 4c 49 54  HORIZATION.SQLIT
87ee0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
87ef0 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64   sqlite3AuthRead
87f00 28 50 61 72 73 65 2a 2c 45 78 70 72 2a 2c 53 63  (Parse*,Expr*,Sc
87f10 68 65 6d 61 2a 2c 53 72 63 4c 69 73 74 2a 29 3b  hema*,SrcList*);
87f20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
87f30 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74    int sqlite3Aut
87f40 68 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 69 6e  hCheck(Parse*,in
87f50 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  t, const char*, 
87f60 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e  const char*, con
87f70 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
87f80 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
87f90 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
87fa0 65 78 74 50 75 73 68 28 50 61 72 73 65 2a 2c 20  extPush(Parse*, 
87fb0 41 75 74 68 43 6f 6e 74 65 78 74 2a 2c 20 63 6f  AuthContext*, co
87fc0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
87fd0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
87fe0 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e  d sqlite3AuthCon
87ff0 74 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74  textPop(AuthCont
88000 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ext*);.SQLITE_PR
88010 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
88020 74 65 33 41 75 74 68 52 65 61 64 43 6f 6c 28 50  te3AuthReadCol(P
88030 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  arse*, const cha
88040 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  r *, const char 
88050 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23  *, int);.#else.#
88060 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41   define sqlite3A
88070 75 74 68 52 65 61 64 28 61 2c 62 2c 63 2c 64 29  uthRead(a,b,c,d)
88080 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
88090 33 41 75 74 68 43 68 65 63 6b 28 61 2c 62 2c 63  3AuthCheck(a,b,c
880a0 2c 64 2c 65 29 20 20 20 20 53 51 4c 49 54 45 5f  ,d,e)    SQLITE_
880b0 4f 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  OK.# define sqli
880c0 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75  te3AuthContextPu
880d0 73 68 28 61 2c 62 2c 63 29 0a 23 20 64 65 66 69  sh(a,b,c).# defi
880e0 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f  ne sqlite3AuthCo
880f0 6e 74 65 78 74 50 6f 70 28 61 29 20 20 28 28 76  ntextPop(a)  ((v
88100 6f 69 64 29 28 61 29 29 0a 23 65 6e 64 69 66 0a  oid)(a)).#endif.
88110 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
88120 6f 69 64 20 73 71 6c 69 74 65 33 41 74 74 61 63  oid sqlite3Attac
88130 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c  h(Parse*, Expr*,
88140 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a   Expr*, Expr*);.
88150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
88160 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74 61 63  oid sqlite3Detac
88170 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29  h(Parse*, Expr*)
88180 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
88190 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 49   int sqlite3FixI
881a0 6e 69 74 28 44 62 46 69 78 65 72 2a 2c 20 50 61  nit(DbFixer*, Pa
881b0 72 73 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  rse*, int, const
881c0 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f   char*, const To
881d0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
881e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
881f0 33 46 69 78 53 72 63 4c 69 73 74 28 44 62 46 69  3FixSrcList(DbFi
88200 78 65 72 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b  xer*, SrcList*);
88210 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
88220 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 65  int sqlite3FixSe
88230 6c 65 63 74 28 44 62 46 69 78 65 72 2a 2c 20 53  lect(DbFixer*, S
88240 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
88250 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
88260 74 65 33 46 69 78 45 78 70 72 28 44 62 46 69 78  te3FixExpr(DbFix
88270 65 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  er*, Expr*);.SQL
88280 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
88290 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69  sqlite3FixExprLi
882a0 73 74 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70  st(DbFixer*, Exp
882b0 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  rList*);.SQLITE_
882c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
882d0 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65  te3FixTriggerSte
882e0 70 28 44 62 46 69 78 65 72 2a 2c 20 54 72 69 67  p(DbFixer*, Trig
882f0 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54  gerStep*);.SQLIT
88300 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
88310 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20  lite3AtoF(const 
88320 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a  char *z, double*
88330 2c 20 69 6e 74 2c 20 75 38 29 3b 0a 53 51 4c 49  , int, u8);.SQLI
88340 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
88350 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 63  qlite3GetInt32(c
88360 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
88370 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
88380 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74  TE int sqlite3At
88390 6f 69 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  oi(const char*);
883a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
883b0 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 31 36  int sqlite3Utf16
883c0 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f  ByteLen(const vo
883d0 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  id *pData, int n
883e0 43 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Char);.SQLITE_PR
883f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
88400 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e  3Utf8CharLen(con
88410 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
88420 69 6e 74 20 6e 42 79 74 65 29 3b 0a 53 51 4c 49  int nByte);.SQLI
88430 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73  TE_PRIVATE u32 s
88440 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 63  qlite3Utf8Read(c
88450 6f 6e 73 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a  onst u8**);../*.
88460 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 72  ** Routines to r
88470 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 76 61  ead and write va
88480 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e  riable-length in
88490 74 65 67 65 72 73 2e 20 20 54 68 65 73 65 20 75  tegers.  These u
884a0 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 64 65 66  sed to.** be def
884b0 69 6e 65 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 75  ined locally, bu
884c0 74 20 6e 6f 77 20 77 65 20 75 73 65 20 74 68 65  t now we use the
884d0 20 76 61 72 69 6e 74 20 72 6f 75 74 69 6e 65 73   varint routines
884e0 20 69 6e 20 74 68 65 20 75 74 69 6c 2e 63 0a 2a   in the util.c.*
884f0 2a 20 66 69 6c 65 2e 20 20 43 6f 64 65 20 73 68  * file.  Code sh
88500 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43  ould use the MAC
88510 52 4f 20 66 6f 72 6d 73 20 62 65 6c 6f 77 2c 20  RO forms below, 
88520 61 73 20 74 68 65 20 56 61 72 69 6e 74 33 32 20  as the Varint32 
88530 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20  versions.** are 
88540 63 6f 64 65 64 20 74 6f 20 61 73 73 75 6d 65 20  coded to assume 
88550 74 68 65 20 73 69 6e 67 6c 65 20 62 79 74 65 20  the single byte 
88560 63 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20  case is already 
88570 68 61 6e 64 6c 65 64 20 28 77 68 69 63 68 20 0a  handled (which .
88580 2a 2a 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72  ** the MACRO for
88590 6d 20 64 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c 49  m does)..*/.SQLI
885a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
885b0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28  qlite3PutVarint(
885c0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20  unsigned char*, 
885d0 75 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  u64);.SQLITE_PRI
885e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
885f0 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69  PutVarint32(unsi
88600 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 29  gned char*, u32)
88610 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
88620 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61   u8 sqlite3GetVa
88630 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67  rint(const unsig
88640 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 36 34 20  ned char *, u64 
88650 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
88660 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74  TE u8 sqlite3Get
88670 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75  Varint32(const u
88680 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20  nsigned char *, 
88690 75 33 32 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  u32 *);.SQLITE_P
886a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
886b0 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20  e3VarintLen(u64 
886c0 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68  v);../*.** The h
886d0 65 61 64 65 72 20 6f 66 20 61 20 72 65 63 6f 72  eader of a recor
886e0 64 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20  d consists of a 
886f0 73 65 71 75 65 6e 63 65 20 76 61 72 69 61 62 6c  sequence variabl
88700 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
88710 73 2e 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65  s..** These inte
88720 67 65 72 73 20 61 72 65 20 61 6c 6d 6f 73 74 20  gers are almost 
88730 61 6c 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e 64  always small and
88740 20 61 72 65 20 65 6e 63 6f 64 65 64 20 61 73 20   are encoded as 
88750 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a  a single byte..*
88760 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
88770 6d 61 63 72 6f 73 20 74 61 6b 65 20 61 64 76 61  macros take adva
88780 6e 74 61 67 65 20 74 68 69 73 20 66 61 63 74 20  ntage this fact 
88790 74 6f 20 70 72 6f 76 69 64 65 20 61 20 66 61 73  to provide a fas
887a0 74 20 65 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64 20  t encode.** and 
887b0 64 65 63 6f 64 65 20 6f 66 20 74 68 65 20 69 6e  decode of the in
887c0 74 65 67 65 72 73 20 69 6e 20 61 20 72 65 63 6f  tegers in a reco
887d0 72 64 20 68 65 61 64 65 72 2e 20 20 49 74 20 69  rd header.  It i
887e0 73 20 66 61 73 74 65 72 20 66 6f 72 20 74 68 65  s faster for the
887f0 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20   common.** case 
88800 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 67 65  where the intege
88810 72 20 69 73 20 61 20 73 69 6e 67 6c 65 20 62 79  r is a single by
88820 74 65 2e 20 20 49 74 20 69 73 20 61 20 6c 69 74  te.  It is a lit
88830 74 6c 65 20 73 6c 6f 77 65 72 20 77 68 65 6e 20  tle slower when 
88840 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69  the.** integer i
88850 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 79  s two or more by
88860 74 65 73 2e 20 20 42 75 74 20 6f 76 65 72 61 6c  tes.  But overal
88870 6c 20 69 74 20 69 73 20 66 61 73 74 65 72 2e 0a  l it is faster..
88880 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
88890 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ing expressions 
888a0 61 72 65 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a  are equivalent:.
888b0 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71  **.**     x = sq
888c0 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32  lite3GetVarint32
888d0 28 20 41 2c 20 26 42 20 29 3b 0a 2a 2a 20 20 20  ( A, &B );.**   
888e0 20 20 78 20 3d 20 73 71 6c 69 74 65 33 50 75 74    x = sqlite3Put
888f0 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29  Varint32( A, B )
88900 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20  ;.**.**     x = 
88910 67 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20  getVarint32( A, 
88920 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20  B );.**     x = 
88930 70 75 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20  putVarint32( A, 
88940 42 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69  B );.**.*/.#defi
88950 6e 65 20 67 65 74 56 61 72 69 6e 74 33 32 28 41  ne getVarint32(A
88960 2c 42 29 20 20 28 75 38 29 28 28 2a 28 41 29 3c  ,B)  (u8)((*(A)<
88970 28 75 38 29 30 78 38 30 29 20 3f 20 28 28 42 29  (u8)0x80) ? ((B)
88980 20 3d 20 28 75 33 32 29 2a 28 41 29 29 2c 31 20   = (u32)*(A)),1 
88990 3a 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69  : sqlite3GetVari
889a0 6e 74 33 32 28 28 41 29 2c 20 28 75 33 32 20 2a  nt32((A), (u32 *
889b0 29 26 28 42 29 29 29 0a 23 64 65 66 69 6e 65 20  )&(B))).#define 
889c0 70 75 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29  putVarint32(A,B)
889d0 20 20 28 75 38 29 28 28 28 75 33 32 29 28 42 29    (u8)(((u32)(B)
889e0 3c 28 75 33 32 29 30 78 38 30 29 20 3f 20 28 2a  <(u32)0x80) ? (*
889f0 28 41 29 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  (A) = (unsigned 
88a00 63 68 61 72 29 28 42 29 29 2c 31 20 3a 20 73 71  char)(B)),1 : sq
88a10 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32  lite3PutVarint32
88a20 28 28 41 29 2c 20 28 42 29 29 29 0a 23 64 65 66  ((A), (B))).#def
88a30 69 6e 65 20 67 65 74 56 61 72 69 6e 74 20 20 20  ine getVarint   
88a40 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
88a50 74 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61 72  t.#define putVar
88a60 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33 50 75  int    sqlite3Pu
88a70 74 56 61 72 69 6e 74 0a 0a 0a 53 51 4c 49 54 45  tVarint...SQLITE
88a80 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
88a90 68 61 72 20 2a 73 71 6c 69 74 65 33 49 6e 64 65  har *sqlite3Inde
88aa0 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62  xAffinityStr(Vdb
88ab0 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53  e *, Index *);.S
88ac0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
88ad0 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41  id sqlite3TableA
88ae0 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 65 20  ffinityStr(Vdbe 
88af0 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c  *, Table *);.SQL
88b00 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
88b10 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41   sqlite3CompareA
88b20 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45  ffinity(Expr *pE
88b30 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 29 3b  xpr, char aff2);
88b40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
88b50 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78  int sqlite3Index
88b60 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 20  AffinityOk(Expr 
88b70 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64 78  *pExpr, char idx
88b80 5f 61 66 66 69 6e 69 74 79 29 3b 0a 53 51 4c 49  _affinity);.SQLI
88b90 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
88ba0 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e  sqlite3ExprAffin
88bb0 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29  ity(Expr *pExpr)
88bc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
88bd0 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 69   int sqlite3Atoi
88be0 36 34 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  64(const char*, 
88bf0 69 36 34 2a 2c 20 69 6e 74 2c 20 75 38 29 3b 0a  i64*, int, u8);.
88c00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
88c10 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72  oid sqlite3Error
88c20 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20  (sqlite3*, int, 
88c30 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29  const char*,...)
88c40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
88c50 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65   void *sqlite3He
88c60 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 2a  xToBlob(sqlite3*
88c70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  , const char *z,
88c80 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f   int n);.SQLITE_
88c90 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74  PRIVATE u8 sqlit
88ca0 65 33 48 65 78 54 6f 49 6e 74 28 69 6e 74 20 68  e3HexToInt(int h
88cb0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
88cc0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 77 6f  E int sqlite3Two
88cd0 50 61 72 74 4e 61 6d 65 28 50 61 72 73 65 20 2a  PartName(Parse *
88ce0 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e  , Token *, Token
88cf0 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2a 29 3b 0a 53   *, Token **);.S
88d00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
88d10 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
88d20 33 45 72 72 53 74 72 28 69 6e 74 29 3b 0a 53 51  3ErrStr(int);.SQ
88d30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
88d40 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
88d50 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ma(Parse *pParse
88d60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
88d70 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  E CollSeq *sqlit
88d80 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 73 71  e3FindCollSeq(sq
88d90 6c 69 74 65 33 2a 2c 75 38 20 65 6e 63 2c 20 63  lite3*,u8 enc, c
88da0 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b  onst char*,int);
88db0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
88dc0 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
88dd0 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61  LocateCollSeq(Pa
88de0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e  rse *pParse, con
88df0 73 74 20 63 68 61 72 2a 7a 4e 61 6d 65 29 3b 0a  st char*zName);.
88e00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
88e10 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45  ollSeq *sqlite3E
88e20 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65  xprCollSeq(Parse
88e30 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
88e40 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50  pExpr);.SQLITE_P
88e50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
88e60 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28  ite3ExprSetColl(
88e70 45 78 70 72 2a 2c 20 43 6f 6c 6c 53 65 71 2a 29  Expr*, CollSeq*)
88e80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
88e90 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
88ea0 70 72 53 65 74 43 6f 6c 6c 42 79 54 6f 6b 65 6e  prSetCollByToken
88eb0 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
88ec0 45 78 70 72 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  Expr*, Token*);.
88ed0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
88ee0 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43  nt sqlite3CheckC
88ef0 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20  ollSeq(Parse *, 
88f00 43 6f 6c 6c 53 65 71 20 2a 29 3b 0a 53 51 4c 49  CollSeq *);.SQLI
88f10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
88f20 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63  qlite3CheckObjec
88f30 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 63  tName(Parse *, c
88f40 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51  onst char *);.SQ
88f50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
88f60 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  d sqlite3VdbeSet
88f70 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20  Changes(sqlite3 
88f80 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
88f90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
88fa0 74 65 33 41 64 64 49 6e 74 36 34 28 69 36 34 2a  te3AddInt64(i64*
88fb0 2c 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,i64);.SQLITE_PR
88fc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
88fd0 33 53 75 62 49 6e 74 36 34 28 69 36 34 2a 2c 69  3SubInt64(i64*,i
88fe0 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  64);.SQLITE_PRIV
88ff0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
89000 75 6c 49 6e 74 36 34 28 69 36 34 2a 2c 69 36 34  ulInt64(i64*,i64
89010 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
89020 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 62 73  E int sqlite3Abs
89030 49 6e 74 33 32 28 69 6e 74 29 3b 0a 23 69 66 64  Int32(int);.#ifd
89040 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
89050 5f 38 5f 33 5f 4e 41 4d 45 53 0a 53 51 4c 49 54  _8_3_NAMES.SQLIT
89060 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
89070 71 6c 69 74 65 33 46 69 6c 65 53 75 66 66 69 78  qlite3FileSuffix
89080 33 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63  3(const char*, c
89090 68 61 72 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64  har*);.#else.# d
890a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 69 6c  efine sqlite3Fil
890b0 65 53 75 66 66 69 78 33 28 58 2c 59 29 0a 23 65  eSuffix3(X,Y).#e
890c0 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
890d0 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65  ATE u8 sqlite3Ge
890e0 74 42 6f 6f 6c 65 61 6e 28 63 6f 6e 73 74 20 63  tBoolean(const c
890f0 68 61 72 20 2a 7a 2c 69 6e 74 29 3b 0a 0a 53 51  har *z,int);..SQ
89100 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
89110 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
89120 56 61 6c 75 65 54 65 78 74 28 73 71 6c 69 74 65  ValueText(sqlite
89130 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53  3_value*, u8);.S
89140 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
89150 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79  t sqlite3ValueBy
89160 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  tes(sqlite3_valu
89170 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f  e*, u8);.SQLITE_
89180 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
89190 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
891a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20  sqlite3_value*, 
891b0 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  int, const void 
891c0 2a 2c 75 38 2c 20 0a 20 20 20 20 20 20 20 20 20  *,u8, .         
891d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76                 v
891e0 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
891f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
89200 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65  oid sqlite3Value
89210 46 72 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  Free(sqlite3_val
89220 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ue*);.SQLITE_PRI
89230 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61 6c  VATE sqlite3_val
89240 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65  ue *sqlite3Value
89250 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 29 3b 0a  New(sqlite3 *);.
89260 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
89270 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 31  har *sqlite3Utf1
89280 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 2c 20  6to8(sqlite3 *, 
89290 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74  const void*, int
892a0 2c 20 75 38 29 3b 0a 23 69 66 64 65 66 20 53 51  , u8);.#ifdef SQ
892b0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54  LITE_ENABLE_STAT
892c0 33 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  3.SQLITE_PRIVATE
892d0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74   char *sqlite3Ut
892e0 66 38 74 6f 31 36 28 73 71 6c 69 74 65 33 20 2a  f8to16(sqlite3 *
892f0 2c 20 75 38 2c 20 63 68 61 72 20 2a 2c 20 69 6e  , u8, char *, in
89300 74 2c 20 69 6e 74 20 2a 29 3b 0a 23 65 6e 64 69  t, int *);.#endi
89310 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f.SQLITE_PRIVATE
89320 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75   int sqlite3Valu
89330 65 46 72 6f 6d 45 78 70 72 28 73 71 6c 69 74 65  eFromExpr(sqlite
89340 33 20 2a 2c 20 45 78 70 72 20 2a 2c 20 75 38 2c  3 *, Expr *, u8,
89350 20 75 38 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c   u8, sqlite3_val
89360 75 65 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ue **);.SQLITE_P
89370 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
89380 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66  te3ValueApplyAff
89390 69 6e 69 74 79 28 73 71 6c 69 74 65 33 5f 76 61  inity(sqlite3_va
893a0 6c 75 65 20 2a 2c 20 75 38 2c 20 75 38 29 3b 0a  lue *, u8, u8);.
893b0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41  #ifndef SQLITE_A
893c0 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49  MALGAMATION.SQLI
893d0 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
893e0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73   unsigned char s
893f0 71 6c 69 74 65 33 4f 70 63 6f 64 65 50 72 6f 70  qlite3OpcodeProp
89400 65 72 74 79 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50  erty[];.SQLITE_P
89410 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73  RIVATE const uns
89420 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74  igned char sqlit
89430 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d  e3UpperToLower[]
89440 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
89450 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
89460 63 68 61 72 20 73 71 6c 69 74 65 33 43 74 79 70  char sqlite3Ctyp
89470 65 4d 61 70 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50  eMap[];.SQLITE_P
89480 52 49 56 41 54 45 20 63 6f 6e 73 74 20 54 6f 6b  RIVATE const Tok
89490 65 6e 20 73 71 6c 69 74 65 33 49 6e 74 54 6f 6b  en sqlite3IntTok
894a0 65 6e 73 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52  ens[];.SQLITE_PR
894b0 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44  IVATE SQLITE_WSD
894c0 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43   struct Sqlite3C
894d0 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e  onfig sqlite3Con
894e0 66 69 67 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  fig;.SQLITE_PRIV
894f0 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 46  ATE SQLITE_WSD F
89500 75 6e 63 44 65 66 48 61 73 68 20 73 71 6c 69 74  uncDefHash sqlit
89510 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e  e3GlobalFunction
89520 73 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  s;.#ifndef SQLIT
89530 45 5f 4f 4d 49 54 5f 57 53 44 0a 53 51 4c 49 54  E_OMIT_WSD.SQLIT
89540 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
89550 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65  lite3PendingByte
89560 3b 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  ;.#endif.#endif.
89570 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
89580 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50  oid sqlite3RootP
89590 61 67 65 4d 6f 76 65 64 28 73 71 6c 69 74 65 33  ageMoved(sqlite3
895a0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
895b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
895c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
895d0 69 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 54 6f  index(Parse*, To
895e0 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53  ken*, Token*);.S
895f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
89600 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46  id sqlite3AlterF
89610 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a  unctions(void);.
89620 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
89630 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72  oid sqlite3Alter
89640 52 65 6e 61 6d 65 54 61 62 6c 65 28 50 61 72 73  RenameTable(Pars
89650 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f  e*, SrcList*, To
89660 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
89670 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
89680 33 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20  3GetToken(const 
89690 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c  unsigned char *,
896a0 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f   int *);.SQLITE_
896b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
896c0 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
896d0 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68  Parse*, const ch
896e0 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54  ar*, ...);.SQLIT
896f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
89700 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
89710 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 73  aredStatements(s
89720 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
89730 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
89740 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63  ite3CodeSubselec
89750 74 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20  t(Parse *, Expr 
89760 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  *, int, int);.SQ
89770 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
89780 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50  d sqlite3SelectP
89790 72 65 70 28 50 61 72 73 65 2a 2c 20 53 65 6c 65  rep(Parse*, Sele
897a0 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74  ct*, NameContext
897b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
897c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65  TE int sqlite3Re
897d0 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 4e  solveExprNames(N
897e0 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70  ameContext*, Exp
897f0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
89800 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
89810 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d  ResolveSelectNam
89820 65 73 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63  es(Parse*, Selec
89830 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a  t*, NameContext*
89840 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
89850 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73  E int sqlite3Res
89860 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79  olveOrderGroupBy
89870 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a  (Parse*, Select*
89880 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e  , ExprList*, con
89890 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
898a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
898b0 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61  qlite3ColumnDefa
898c0 75 6c 74 28 56 64 62 65 20 2a 2c 20 54 61 62 6c  ult(Vdbe *, Tabl
898d0 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  e *, int, int);.
898e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
898f0 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72  oid sqlite3Alter
89900 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28  FinishAddColumn(
89910 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a  Parse *, Token *
89920 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
89930 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c  E void sqlite3Al
89940 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d  terBeginAddColum
89950 6e 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69  n(Parse *, SrcLi
89960 73 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  st *);.SQLITE_PR
89970 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73  IVATE CollSeq *s
89980 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71  qlite3GetCollSeq
89990 28 50 61 72 73 65 2a 2c 20 75 38 2c 20 43 6f 6c  (Parse*, u8, Col
899a0 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 74 20 63 68  lSeq *, const ch
899b0 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
899c0 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65  VATE char sqlite
899d0 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 63 6f  3AffinityType(co
899e0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
899f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
89a00 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 50  sqlite3Analyze(P
89a10 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54  arse*, Token*, T
89a20 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
89a30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
89a40 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64  e3InvokeBusyHand
89a50 6c 65 72 28 42 75 73 79 48 61 6e 64 6c 65 72 2a  ler(BusyHandler*
89a60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
89a70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e  E int sqlite3Fin
89a80 64 44 62 28 73 71 6c 69 74 65 33 2a 2c 20 54 6f  dDb(sqlite3*, To
89a90 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
89aa0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
89ab0 33 46 69 6e 64 44 62 4e 61 6d 65 28 73 71 6c 69  3FindDbName(sqli
89ac0 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  te3 *, const cha
89ad0 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  r *);.SQLITE_PRI
89ae0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
89af0 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 73 71 6c  AnalysisLoad(sql
89b00 69 74 65 33 2a 2c 69 6e 74 20 69 44 42 29 3b 0a  ite3*,int iDB);.
89b10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
89b20 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74  oid sqlite3Delet
89b30 65 49 6e 64 65 78 53 61 6d 70 6c 65 73 28 73 71  eIndexSamples(sq
89b40 6c 69 74 65 33 2a 2c 49 6e 64 65 78 2a 29 3b 0a  lite3*,Index*);.
89b50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
89b60 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 61 75  oid sqlite3Defau
89b70 6c 74 52 6f 77 45 73 74 28 49 6e 64 65 78 2a 29  ltRowEst(Index*)
89b80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
89b90 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67   void sqlite3Reg
89ba0 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f  isterLikeFunctio
89bb0 6e 73 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ns(sqlite3*, int
89bc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
89bd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4c  E int sqlite3IsL
89be0 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69  ikeFunction(sqli
89bf0 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 2a 2c  te3*,Expr*,int*,
89c00 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
89c10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
89c20 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f  te3MinimumFileFo
89c30 72 6d 61 74 28 50 61 72 73 65 2a 2c 20 69 6e 74  rmat(Parse*, int
89c40 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
89c50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
89c60 74 65 33 53 63 68 65 6d 61 43 6c 65 61 72 28 76  te3SchemaClear(v
89c70 6f 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oid *);.SQLITE_P
89c80 52 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a 73  RIVATE Schema *s
89c90 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28  qlite3SchemaGet(
89ca0 73 71 6c 69 74 65 33 20 2a 2c 20 42 74 72 65 65  sqlite3 *, Btree
89cb0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
89cc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
89cd0 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c  chemaToIndex(sql
89ce0 69 74 65 33 20 2a 64 62 2c 20 53 63 68 65 6d 61  ite3 *db, Schema
89cf0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
89d00 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c  ATE KeyInfo *sql
89d10 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
89d20 28 50 61 72 73 65 20 2a 2c 20 49 6e 64 65 78 20  (Parse *, Index 
89d30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
89d40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 72  TE int sqlite3Cr
89d50 65 61 74 65 46 75 6e 63 28 73 71 6c 69 74 65 33  eateFunc(sqlite3
89d60 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
89d70 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64  , int, int, void
89d80 20 2a 2c 20 0a 20 20 76 6f 69 64 20 28 2a 29 28   *, .  void (*)(
89d90 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
89da0 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
89db0 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28  ue **),.  void (
89dc0 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  *)(sqlite3_conte
89dd0 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
89de0 76 61 6c 75 65 20 2a 2a 29 2c 20 76 6f 69 64 20  value **), void 
89df0 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (*)(sqlite3_cont
89e00 65 78 74 2a 29 2c 0a 20 20 46 75 6e 63 44 65 73  ext*),.  FuncDes
89e10 74 72 75 63 74 6f 72 20 2a 70 44 65 73 74 72 75  tructor *pDestru
89e20 63 74 6f 72 0a 29 3b 0a 53 51 4c 49 54 45 5f 50  ctor.);.SQLITE_P
89e30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
89e40 65 33 41 70 69 45 78 69 74 28 73 71 6c 69 74 65  e3ApiExit(sqlite
89e50 33 20 2a 64 62 2c 20 69 6e 74 29 3b 0a 53 51 4c  3 *db, int);.SQL
89e60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
89e70 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44  sqlite3OpenTempD
89e80 61 74 61 62 61 73 65 28 50 61 72 73 65 20 2a 29  atabase(Parse *)
89e90 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
89ea0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74  E void sqlite3St
89eb0 72 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41 63  rAccumInit(StrAc
89ec0 63 75 6d 2a 2c 20 63 68 61 72 2a 2c 20 69 6e 74  cum*, char*, int
89ed0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
89ee0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
89ef0 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
89f00 64 28 53 74 72 41 63 63 75 6d 2a 2c 63 6f 6e 73  d(StrAccum*,cons
89f10 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51  t char*,int);.SQ
89f20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
89f30 64 20 73 71 6c 69 74 65 33 41 70 70 65 6e 64 53  d sqlite3AppendS
89f40 70 61 63 65 28 53 74 72 41 63 63 75 6d 2a 2c 69  pace(StrAccum*,i
89f50 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
89f60 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
89f70 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28  3StrAccumFinish(
89f80 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49  StrAccum*);.SQLI
89f90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
89fa0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52  sqlite3StrAccumR
89fb0 65 73 65 74 28 53 74 72 41 63 63 75 6d 2a 29 3b  eset(StrAccum*);
89fc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
89fd0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65  void sqlite3Sele
89fe0 63 74 44 65 73 74 49 6e 69 74 28 53 65 6c 65 63  ctDestInit(Selec
89ff0 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b  tDest*,int,int);
8a000 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8a010 45 78 70 72 20 2a 73 71 6c 69 74 65 33 43 72 65  Expr *sqlite3Cre
8a020 61 74 65 43 6f 6c 75 6d 6e 45 78 70 72 28 73 71  ateColumnExpr(sq
8a030 6c 69 74 65 33 20 2a 2c 20 53 72 63 4c 69 73 74  lite3 *, SrcList
8a040 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a   *, int, int);..
8a050 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
8a060 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75  oid sqlite3Backu
8a070 70 52 65 73 74 61 72 74 28 73 71 6c 69 74 65 33  pRestart(sqlite3
8a080 5f 62 61 63 6b 75 70 20 2a 29 3b 0a 53 51 4c 49  _backup *);.SQLI
8a090 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
8a0a0 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64  sqlite3BackupUpd
8a0b0 61 74 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  ate(sqlite3_back
8a0c0 75 70 20 2a 2c 20 50 67 6e 6f 2c 20 63 6f 6e 73  up *, Pgno, cons
8a0d0 74 20 75 38 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t u8 *);../*.** 
8a0e0 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f  The interface to
8a0f0 20 74 68 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72   the LEMON-gener
8a100 61 74 65 64 20 70 61 72 73 65 72 0a 2a 2f 0a 53  ated parser.*/.S
8a110 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
8a120 69 64 20 2a 73 71 6c 69 74 65 33 50 61 72 73 65  id *sqlite3Parse
8a130 72 41 6c 6c 6f 63 28 76 6f 69 64 2a 28 2a 29 28  rAlloc(void*(*)(
8a140 73 69 7a 65 5f 74 29 29 3b 0a 53 51 4c 49 54 45  size_t));.SQLITE
8a150 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
8a160 6c 69 74 65 33 50 61 72 73 65 72 46 72 65 65 28  lite3ParserFree(
8a170 76 6f 69 64 2a 2c 20 76 6f 69 64 28 2a 29 28 76  void*, void(*)(v
8a180 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50  oid*));.SQLITE_P
8a190 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
8a1a0 74 65 33 50 61 72 73 65 72 28 76 6f 69 64 2a 2c  te3Parser(void*,
8a1b0 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c 20 50 61 72   int, Token, Par
8a1c0 73 65 2a 29 3b 0a 23 69 66 64 65 66 20 59 59 54  se*);.#ifdef YYT
8a1d0 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54  RACKMAXSTACKDEPT
8a1e0 48 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  H.SQLITE_PRIVATE
8a1f0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61     int sqlite3Pa
8a200 72 73 65 72 53 74 61 63 6b 50 65 61 6b 28 76 6f  rserStackPeak(vo
8a210 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51  id*);.#endif..SQ
8a220 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
8a230 64 20 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61  d sqlite3AutoLoa
8a240 64 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69  dExtensions(sqli
8a250 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53  te3*);.#ifndef S
8a260 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
8a270 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45  EXTENSION.SQLITE
8a280 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
8a290 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65  sqlite3CloseExte
8a2a0 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29  nsions(sqlite3*)
8a2b0 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ;.#else.# define
8a2c0 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74   sqlite3CloseExt
8a2d0 65 6e 73 69 6f 6e 73 28 58 29 0a 23 65 6e 64 69  ensions(X).#endi
8a2e0 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
8a2f0 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
8a300 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  CHE.SQLITE_PRIVA
8a310 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
8a320 33 54 61 62 6c 65 4c 6f 63 6b 28 50 61 72 73 65  3TableLock(Parse
8a330 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 75 38   *, int, int, u8
8a340 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b  , const char *);
8a350 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
8a360 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
8a370 6b 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e  k(v,w,x,y,z).#en
8a380 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
8a390 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50  TE_TEST.SQLITE_P
8a3a0 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
8a3b0 69 74 65 33 55 74 66 38 54 6f 38 28 75 6e 73 69  ite3Utf8To8(unsi
8a3c0 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 23 65 6e  gned char*);.#en
8a3d0 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
8a3e0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
8a3f0 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 73  ABLE.#  define s
8a400 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28  qlite3VtabClear(
8a410 59 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c  Y).#  define sql
8a420 69 74 65 33 56 74 61 62 53 79 6e 63 28 58 2c 59  ite3VtabSync(X,Y
8a430 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 20 64  ) SQLITE_OK.#  d
8a440 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61  efine sqlite3Vta
8a450 62 52 6f 6c 6c 62 61 63 6b 28 58 29 0a 23 20 20  bRollback(X).#  
8a460 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74  define sqlite3Vt
8a470 61 62 43 6f 6d 6d 69 74 28 58 29 0a 23 20 20 64  abCommit(X).#  d
8a480 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61  efine sqlite3Vta
8a490 62 49 6e 53 79 6e 63 28 64 62 29 20 30 0a 23 20  bInSync(db) 0.# 
8a4a0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56   define sqlite3V
8a4b0 74 61 62 4c 6f 63 6b 28 58 29 20 0a 23 20 20 64  tabLock(X) .#  d
8a4c0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61  efine sqlite3Vta
8a4d0 62 55 6e 6c 6f 63 6b 28 58 29 0a 23 20 20 64 65  bUnlock(X).#  de
8a4e0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62  fine sqlite3Vtab
8a4f0 55 6e 6c 6f 63 6b 4c 69 73 74 28 58 29 0a 23 20  UnlockList(X).# 
8a500 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56   define sqlite3V
8a510 74 61 62 53 61 76 65 70 6f 69 6e 74 28 58 2c 20  tabSavepoint(X, 
8a520 59 2c 20 5a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a  Y, Z) SQLITE_OK.
8a530 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  #  define sqlite
8a540 33 47 65 74 56 54 61 62 6c 65 28 58 2c 59 29 20  3GetVTable(X,Y) 
8a550 20 28 28 56 54 61 62 6c 65 2a 29 30 29 0a 23 65   ((VTable*)0).#e
8a560 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  lse.SQLITE_PRIVA
8a570 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74  TE    void sqlit
8a580 65 33 56 74 61 62 43 6c 65 61 72 28 73 71 6c 69  e3VtabClear(sqli
8a590 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 2a 29  te3 *db, Table*)
8a5a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
8a5b0 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33      void sqlite3
8a5c0 56 74 61 62 44 69 73 63 6f 6e 6e 65 63 74 28 73  VtabDisconnect(s
8a5d0 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c  qlite3 *db, Tabl
8a5e0 65 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52  e *p);.SQLITE_PR
8a5f0 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c  IVATE    int sql
8a600 69 74 65 33 56 74 61 62 53 79 6e 63 28 73 71 6c  ite3VtabSync(sql
8a610 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a  ite3 *db, char *
8a620 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
8a630 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65  TE    int sqlite
8a640 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71  3VtabRollback(sq
8a650 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49  lite3 *db);.SQLI
8a660 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e  TE_PRIVATE    in
8a670 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d  t sqlite3VtabCom
8a680 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29  mit(sqlite3 *db)
8a690 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
8a6a0 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33      void sqlite3
8a6b0 56 74 61 62 4c 6f 63 6b 28 56 54 61 62 6c 65 20  VtabLock(VTable 
8a6c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
8a6d0 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74  TE    void sqlit
8a6e0 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 56 54 61  e3VtabUnlock(VTa
8a6f0 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ble *);.SQLITE_P
8a700 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73  RIVATE    void s
8a710 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
8a720 4c 69 73 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a  List(sqlite3*);.
8a730 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
8a740 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61    int sqlite3Vta
8a750 62 53 61 76 65 70 6f 69 6e 74 28 73 71 6c 69 74  bSavepoint(sqlit
8a760 65 33 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  e3 *, int, int);
8a770 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8a780 20 20 20 56 54 61 62 6c 65 20 2a 73 71 6c 69 74     VTable *sqlit
8a790 65 33 47 65 74 56 54 61 62 6c 65 28 73 71 6c 69  e3GetVTable(sqli
8a7a0 74 65 33 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 23  te3*, Table*);.#
8a7b0 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
8a7c0 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 28  VtabInSync(db) (
8a7d0 28 64 62 29 2d 3e 6e 56 54 72 61 6e 73 3e 30 20  (db)->nVTrans>0 
8a7e0 26 26 20 28 64 62 29 2d 3e 61 56 54 72 61 6e 73  && (db)->aVTrans
8a7f0 3d 3d 30 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49  ==0).#endif.SQLI
8a800 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
8a810 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57  sqlite3VtabMakeW
8a820 72 69 74 61 62 6c 65 28 50 61 72 73 65 2a 2c 54  ritable(Parse*,T
8a830 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  able*);.SQLITE_P
8a840 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
8a850 74 65 33 56 74 61 62 42 65 67 69 6e 50 61 72 73  te3VtabBeginPars
8a860 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a  e(Parse*, Token*
8a870 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a  , Token*, Token*
8a880 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
8a890 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
8a8a0 74 65 33 56 74 61 62 46 69 6e 69 73 68 50 61 72  te3VtabFinishPar
8a8b0 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e  se(Parse*, Token
8a8c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
8a8d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
8a8e0 74 61 62 41 72 67 49 6e 69 74 28 50 61 72 73 65  tabArgInit(Parse
8a8f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
8a900 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
8a910 74 61 62 41 72 67 45 78 74 65 6e 64 28 50 61 72  tabArgExtend(Par
8a920 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  se*, Token*);.SQ
8a930 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
8a940 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c   sqlite3VtabCall
8a950 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a 2c  Create(sqlite3*,
8a960 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72   int, const char
8a970 20 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51   *, char **);.SQ
8a980 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
8a990 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c   sqlite3VtabCall
8a9a0 43 6f 6e 6e 65 63 74 28 50 61 72 73 65 2a 2c 20  Connect(Parse*, 
8a9b0 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Table*);.SQLITE_
8a9c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
8a9d0 74 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72  te3VtabCallDestr
8a9e0 6f 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  oy(sqlite3*, int
8a9f0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b  , const char *);
8aa00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8aa10 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 42  int sqlite3VtabB
8aa20 65 67 69 6e 28 73 71 6c 69 74 65 33 20 2a 2c 20  egin(sqlite3 *, 
8aa30 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54  VTable *);.SQLIT
8aa40 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65  E_PRIVATE FuncDe
8aa50 66 20 2a 73 71 6c 69 74 65 33 56 74 61 62 4f 76  f *sqlite3VtabOv
8aa60 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 73  erloadFunction(s
8aa70 71 6c 69 74 65 33 20 2a 2c 46 75 6e 63 44 65 66  qlite3 *,FuncDef
8aa80 2a 2c 20 69 6e 74 20 6e 41 72 67 2c 20 45 78 70  *, int nArg, Exp
8aa90 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
8aaa0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
8aab0 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28  InvalidFunction(
8aac0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
8aad0 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
8aae0 75 65 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ue**);.SQLITE_PR
8aaf0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
8ab00 33 56 64 62 65 50 61 72 61 6d 65 74 65 72 49 6e  3VdbeParameterIn
8ab10 64 65 78 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74  dex(Vdbe*, const
8ab20 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51   char*, int);.SQ
8ab30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
8ab40 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65 72   sqlite3Transfer
8ab50 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33  Bindings(sqlite3
8ab60 5f 73 74 6d 74 20 2a 2c 20 73 71 6c 69 74 65 33  _stmt *, sqlite3
8ab70 5f 73 74 6d 74 20 2a 29 3b 0a 53 51 4c 49 54 45  _stmt *);.SQLITE
8ab80 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
8ab90 69 74 65 33 52 65 70 72 65 70 61 72 65 28 56 64  ite3Reprepare(Vd
8aba0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
8abb0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
8abc0 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65  3ExprListCheckLe
8abd0 6e 67 74 68 28 50 61 72 73 65 2a 2c 20 45 78 70  ngth(Parse*, Exp
8abe0 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63 68  rList*, const ch
8abf0 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
8ac00 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71  VATE CollSeq *sq
8ac10 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61  lite3BinaryCompa
8ac20 72 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20  reCollSeq(Parse 
8ac30 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 20  *, Expr *, Expr 
8ac40 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
8ac50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 65  TE int sqlite3Te
8ac60 6d 70 49 6e 4d 65 6d 6f 72 79 28 63 6f 6e 73 74  mpInMemory(const
8ac70 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49   sqlite3*);.SQLI
8ac80 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
8ac90 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4a 6f   char *sqlite3Jo
8aca0 75 72 6e 61 6c 4d 6f 64 65 6e 61 6d 65 28 69 6e  urnalModename(in
8acb0 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  t);.#ifndef SQLI
8acc0 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 53 51 4c 49  TE_OMIT_WAL.SQLI
8acd0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
8ace0 20 73 71 6c 69 74 65 33 43 68 65 63 6b 70 6f 69   sqlite3Checkpoi
8acf0 6e 74 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  nt(sqlite3*, int
8ad00 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 69 6e 74  , int, int*, int
8ad10 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
8ad20 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
8ad30 57 61 6c 44 65 66 61 75 6c 74 48 6f 6f 6b 28 76  WalDefaultHook(v
8ad40 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 63 6f  oid*,sqlite3*,co
8ad50 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a  nst char*,int);.
8ad60 23 65 6e 64 69 66 0a 0a 2f 2a 20 44 65 63 6c 61  #endif../* Decla
8ad70 72 61 74 69 6f 6e 73 20 66 6f 72 20 66 75 6e 63  rations for func
8ad80 74 69 6f 6e 73 20 69 6e 20 66 6b 65 79 2e 63 2e  tions in fkey.c.
8ad90 20 41 6c 6c 20 6f 66 20 74 68 65 73 65 20 61 72   All of these ar
8ada0 65 20 72 65 70 6c 61 63 65 64 20 62 79 0a 2a 2a  e replaced by.**
8adb0 20 6e 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 69 66   no-op macros if
8adc0 20 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45   OMIT_FOREIGN_KE
8add0 59 20 69 73 20 64 65 66 69 6e 65 64 2e 20 49 6e  Y is defined. In
8ade0 20 74 68 69 73 20 63 61 73 65 20 6e 6f 20 66 6f   this case no fo
8adf0 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 20 66 75 6e  reign.** key fun
8ae00 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 61 76  ctionality is av
8ae10 61 69 6c 61 62 6c 65 2e 20 49 66 20 4f 4d 49 54  ailable. If OMIT
8ae20 5f 54 52 49 47 47 45 52 20 69 73 20 64 65 66 69  _TRIGGER is defi
8ae30 6e 65 64 20 62 75 74 0a 2a 2a 20 4f 4d 49 54 5f  ned but.** OMIT_
8ae40 46 4f 52 45 49 47 4e 5f 4b 45 59 20 69 73 20 6e  FOREIGN_KEY is n
8ae50 6f 74 2c 20 6f 6e 6c 79 20 73 6f 6d 65 20 6f 66  ot, only some of
8ae60 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 61   the functions a
8ae70 72 65 20 6e 6f 2d 6f 70 65 64 2e 20 49 6e 0a 2a  re no-oped. In.*
8ae80 2a 20 74 68 69 73 20 63 61 73 65 20 66 6f 72 65  * this case fore
8ae90 69 67 6e 20 6b 65 79 73 20 61 72 65 20 70 61 72  ign keys are par
8aea0 73 65 64 2c 20 62 75 74 20 6e 6f 20 6f 74 68 65  sed, but no othe
8aeb0 72 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20  r functionality 
8aec0 69 73 20 0a 2a 2a 20 70 72 6f 76 69 64 65 64 20  is .** provided 
8aed0 28 65 6e 66 6f 72 63 65 6d 65 6e 74 20 6f 66 20  (enforcement of 
8aee0 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 72  FK constraints r
8aef0 65 71 75 69 72 65 73 20 74 68 65 20 74 72 69 67  equires the trig
8af00 67 65 72 73 20 73 75 62 2d 73 79 73 74 65 6d 29  gers sub-system)
8af10 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  ..*/.#if !define
8af20 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  d(SQLITE_OMIT_FO
8af30 52 45 49 47 4e 5f 4b 45 59 29 20 26 26 20 21 64  REIGN_KEY) && !d
8af40 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
8af50 49 54 5f 54 52 49 47 47 45 52 29 0a 53 51 4c 49  IT_TRIGGER).SQLI
8af60 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
8af70 64 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b  d sqlite3FkCheck
8af80 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
8af90 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
8afa0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
8afb0 64 20 73 71 6c 69 74 65 33 46 6b 44 72 6f 70 54  d sqlite3FkDropT
8afc0 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63  able(Parse*, Src
8afd0 4c 69 73 74 20 2a 2c 20 54 61 62 6c 65 2a 29 3b  List *, Table*);
8afe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8aff0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b    void sqlite3Fk
8b000 41 63 74 69 6f 6e 73 28 50 61 72 73 65 2a 2c 20  Actions(Parse*, 
8b010 54 61 62 6c 65 2a 2c 20 45 78 70 72 4c 69 73 74  Table*, ExprList
8b020 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
8b030 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
8b040 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28  lite3FkRequired(
8b050 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  Parse*, Table*, 
8b060 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  int*, int);.SQLI
8b070 54 45 5f 50 52 49 56 41 54 45 20 20 20 75 33 32  TE_PRIVATE   u32
8b080 20 73 71 6c 69 74 65 33 46 6b 4f 6c 64 6d 61 73   sqlite3FkOldmas
8b090 6b 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a  k(Parse*, Table*
8b0a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
8b0b0 45 20 20 20 46 4b 65 79 20 2a 73 71 6c 69 74 65  E   FKey *sqlite
8b0c0 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 54 61  3FkReferences(Ta
8b0d0 62 6c 65 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20  ble *);.#else.  
8b0e0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46  #define sqlite3F
8b0f0 6b 41 63 74 69 6f 6e 73 28 61 2c 62 2c 63 2c 64  kActions(a,b,c,d
8b100 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  ).  #define sqli
8b110 74 65 33 46 6b 43 68 65 63 6b 28 61 2c 62 2c 63  te3FkCheck(a,b,c
8b120 2c 64 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71  ,d).  #define sq
8b130 6c 69 74 65 33 46 6b 44 72 6f 70 54 61 62 6c 65  lite3FkDropTable
8b140 28 61 2c 62 2c 63 29 0a 20 20 23 64 65 66 69 6e  (a,b,c).  #defin
8b150 65 20 73 71 6c 69 74 65 33 46 6b 4f 6c 64 6d 61  e sqlite3FkOldma
8b160 73 6b 28 61 2c 62 29 20 20 20 20 20 20 30 0a 20  sk(a,b)      0. 
8b170 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
8b180 46 6b 52 65 71 75 69 72 65 64 28 61 2c 62 2c 63  FkRequired(a,b,c
8b190 2c 64 29 20 30 0a 23 65 6e 64 69 66 0a 23 69 66  ,d) 0.#endif.#if
8b1a0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
8b1b0 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 53 51 4c  _FOREIGN_KEY.SQL
8b1c0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
8b1d0 69 64 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65  id sqlite3FkDele
8b1e0 74 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 54 61  te(sqlite3 *, Ta
8b1f0 62 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23  ble*);.#else.  #
8b200 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b  define sqlite3Fk
8b210 44 65 6c 65 74 65 28 61 2c 62 29 0a 23 65 6e 64  Delete(a,b).#end
8b220 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 76 61 69 6c  if.../*.** Avail
8b230 61 62 6c 65 20 66 61 75 6c 74 20 69 6e 6a 65 63  able fault injec
8b240 74 6f 72 73 2e 20 20 53 68 6f 75 6c 64 20 62 65  tors.  Should be
8b250 20 6e 75 6d 62 65 72 65 64 20 62 65 67 69 6e 6e   numbered beginn
8b260 69 6e 67 20 77 69 74 68 20 30 2e 0a 2a 2f 0a 23  ing with 0..*/.#
8b270 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 41  define SQLITE_FA
8b280 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c  ULTINJECTOR_MALL
8b290 4f 43 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65  OC     0.#define
8b2a0 20 53 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a   SQLITE_FAULTINJ
8b2b0 45 43 54 4f 52 5f 43 4f 55 4e 54 20 20 20 20 20  ECTOR_COUNT     
8b2c0 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e   1../*.** The in
8b2d0 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 63  terface to the c
8b2e0 6f 64 65 20 69 6e 20 66 61 75 6c 74 2e 63 20 75  ode in fault.c u
8b2f0 73 65 64 20 66 6f 72 20 69 64 65 6e 74 69 66 79  sed for identify
8b300 69 6e 67 20 22 62 65 6e 69 67 6e 22 0a 2a 2a 20  ing "benign".** 
8b310 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 2e  malloc failures.
8b320 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 70 72   This is only pr
8b330 65 73 65 6e 74 20 69 66 20 53 51 4c 49 54 45 5f  esent if SQLITE_
8b340 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53  OMIT_BUILTIN_TES
8b350 54 0a 2a 2a 20 69 73 20 6e 6f 74 20 64 65 66 69  T.** is not defi
8b360 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ned..*/.#ifndef 
8b370 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
8b380 54 49 4e 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  TIN_TEST.SQLITE_
8b390 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
8b3a0 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
8b3b0 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 53  nMalloc(void);.S
8b3c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
8b3d0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42  void sqlite3EndB
8b3e0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64  enignMalloc(void
8b3f0 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69  );.#else.  #defi
8b400 6e 65 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42  ne sqlite3BeginB
8b410 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 20 20  enignMalloc().  
8b420 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45  #define sqlite3E
8b430 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
8b440 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65  .#endif..#define
8b450 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20   IN_INDEX_ROWID 
8b460 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66            1.#def
8b470 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48  ine IN_INDEX_EPH
8b480 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 23               2.#
8b490 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f  define IN_INDEX_
8b4a0 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20  INDEX           
8b4b0 33 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  3.SQLITE_PRIVATE
8b4c0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64   int sqlite3Find
8b4d0 49 6e 49 6e 64 65 78 28 50 61 72 73 65 20 2a 2c  InIndex(Parse *,
8b4e0 20 45 78 70 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a   Expr *, int*);.
8b4f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
8b500 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49  NABLE_ATOMIC_WRI
8b510 54 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  TE.SQLITE_PRIVAT
8b520 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  E   int sqlite3J
8b530 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74  ournalOpen(sqlit
8b540 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20  e3_vfs *, const 
8b550 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f  char *, sqlite3_
8b560 66 69 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74  file *, int, int
8b570 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
8b580 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  E   int sqlite3J
8b590 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74  ournalSize(sqlit
8b5a0 65 33 5f 76 66 73 20 2a 29 3b 0a 53 51 4c 49 54  e3_vfs *);.SQLIT
8b5b0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
8b5c0 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72  sqlite3JournalCr
8b5d0 65 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  eate(sqlite3_fil
8b5e0 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e *);.SQLITE_PRI
8b5f0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
8b600 65 33 4a 6f 75 72 6e 61 6c 45 78 69 73 74 73 28  e3JournalExists(
8b610 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 29  sqlite3_file *p)
8b620 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e  ;.#else.  #defin
8b630 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  e sqlite3Journal
8b640 53 69 7a 65 28 70 56 66 73 29 20 28 28 70 56 66  Size(pVfs) ((pVf
8b650 73 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a 20 20  s)->szOsFile).  
8b660 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4a  #define sqlite3J
8b670 6f 75 72 6e 61 6c 45 78 69 73 74 73 28 70 29 20  ournalExists(p) 
8b680 31 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45  1.#endif..SQLITE
8b690 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
8b6a0 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f  lite3MemJournalO
8b6b0 70 65 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  pen(sqlite3_file
8b6c0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
8b6d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
8b6e0 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 76 6f  emJournalSize(vo
8b6f0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
8b700 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
8b710 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69  sMemJournal(sqli
8b720 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69  te3_file *);..#i
8b730 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  f SQLITE_MAX_EXP
8b740 52 5f 44 45 50 54 48 3e 30 0a 53 51 4c 49 54 45  R_DEPTH>0.SQLITE
8b750 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
8b760 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
8b770 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72  ight(Parse *pPar
8b780 73 65 2c 20 45 78 70 72 20 2a 70 29 3b 0a 53 51  se, Expr *p);.SQ
8b790 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
8b7a0 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  nt sqlite3Select
8b7b0 45 78 70 72 48 65 69 67 68 74 28 53 65 6c 65 63  ExprHeight(Selec
8b7c0 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  t *);.SQLITE_PRI
8b7d0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
8b7e0 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68  e3ExprCheckHeigh
8b7f0 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a  t(Parse*, int);.
8b800 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
8b810 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
8b820 69 67 68 74 28 78 2c 79 29 0a 20 20 23 64 65 66  ight(x,y).  #def
8b830 69 6e 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ine sqlite3Selec
8b840 74 45 78 70 72 48 65 69 67 68 74 28 78 29 20 30  tExprHeight(x) 0
8b850 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
8b860 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68  e3ExprCheckHeigh
8b870 74 28 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53  t(x,y).#endif..S
8b880 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33  QLITE_PRIVATE u3
8b890 32 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74  2 sqlite3Get4byt
8b8a0 65 28 63 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51  e(const u8*);.SQ
8b8b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
8b8c0 64 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74  d sqlite3Put4byt
8b8d0 65 28 75 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69  e(u8*, u32);..#i
8b8e0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
8b8f0 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59  LE_UNLOCK_NOTIFY
8b900 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8b910 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f    void sqlite3Co
8b920 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28  nnectionBlocked(
8b930 73 71 6c 69 74 65 33 20 2a 2c 20 73 71 6c 69 74  sqlite3 *, sqlit
8b940 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  e3 *);.SQLITE_PR
8b950 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
8b960 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e  ite3ConnectionUn
8b970 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a  locked(sqlite3 *
8b980 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  db);.SQLITE_PRIV
8b990 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
8b9a0 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73  e3ConnectionClos
8b9b0 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b  ed(sqlite3 *db);
8b9c0 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
8b9d0 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69   sqlite3Connecti
8b9e0 6f 6e 42 6c 6f 63 6b 65 64 28 78 2c 79 29 0a 20  onBlocked(x,y). 
8b9f0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
8ba00 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b  ConnectionUnlock
8ba10 65 64 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20  ed(x).  #define 
8ba20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f  sqlite3Connectio
8ba30 6e 43 6c 6f 73 65 64 28 78 29 0a 23 65 6e 64 69  nClosed(x).#endi
8ba40 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
8ba50 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52  _DEBUG.SQLITE_PR
8ba60 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
8ba70 69 74 65 33 50 61 72 73 65 72 54 72 61 63 65 28  ite3ParserTrace(
8ba80 46 49 4c 45 2a 2c 20 63 68 61 72 20 2a 29 3b 0a  FILE*, char *);.
8ba90 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66  #endif../*.** If
8baa0 20 74 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42   the SQLITE_ENAB
8bab0 4c 45 20 49 4f 54 52 41 43 45 20 65 78 69 73 74  LE IOTRACE exist
8bac0 73 20 74 68 65 6e 20 74 68 65 20 67 6c 6f 62 61  s then the globa
8bad0 6c 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 73 71  l variable.** sq
8bae0 6c 69 74 65 33 49 6f 54 72 61 63 65 20 69 73 20  lite3IoTrace is 
8baf0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70  a pointer to a p
8bb00 72 69 6e 74 66 2d 6c 69 6b 65 20 72 6f 75 74 69  rintf-like routi
8bb10 6e 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70 72  ne used to.** pr
8bb20 69 6e 74 20 49 2f 4f 20 74 72 61 63 69 6e 67 20  int I/O tracing 
8bb30 6d 65 73 73 61 67 65 73 2e 20 0a 2a 2f 0a 23 69  messages. .*/.#i
8bb40 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
8bb50 4c 45 5f 49 4f 54 52 41 43 45 0a 23 20 64 65 66  LE_IOTRACE.# def
8bb60 69 6e 65 20 49 4f 54 52 41 43 45 28 41 29 20 20  ine IOTRACE(A)  
8bb70 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61  if( sqlite3IoTra
8bb80 63 65 20 29 7b 20 73 71 6c 69 74 65 33 49 6f 54  ce ){ sqlite3IoT
8bb90 72 61 63 65 20 41 3b 20 7d 0a 53 51 4c 49 54 45  race A; }.SQLITE
8bba0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
8bbb0 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61  sqlite3VdbeIOTra
8bbc0 63 65 53 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51  ceSql(Vdbe*);.SQ
8bbd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
8bbe0 64 20 28 2a 73 71 6c 69 74 65 33 49 6f 54 72 61  d (*sqlite3IoTra
8bbf0 63 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ce)(const char*,
8bc00 2e 2e 2e 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65  ...);.#else.# de
8bc10 66 69 6e 65 20 49 4f 54 52 41 43 45 28 41 29 0a  fine IOTRACE(A).
8bc20 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
8bc30 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 58  VdbeIOTraceSql(X
8bc40 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
8bc50 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
8bc60 72 65 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  re available for
8bc70 20 74 68 65 20 6d 65 6d 32 2e 63 20 64 65 62 75   the mem2.c debu
8bc80 67 67 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c  gging memory all
8bc90 6f 63 61 74 6f 72 0a 2a 2a 20 6f 6e 6c 79 2e 20  ocator.** only. 
8bca0 20 54 68 65 79 20 61 72 65 20 75 73 65 64 20 74   They are used t
8bcb0 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 64 69  o verify that di
8bcc0 66 66 65 72 65 6e 74 20 22 74 79 70 65 73 22 20  fferent "types" 
8bcd0 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c  of memory.** all
8bce0 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 70 72 6f  ocations are pro
8bcf0 70 65 72 6c 79 20 74 72 61 63 6b 65 64 20 62 79  perly tracked by
8bd00 20 74 68 65 20 73 79 73 74 65 6d 2e 0a 2a 2a 0a   the system..**.
8bd10 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62  ** sqlite3Memdeb
8bd20 75 67 53 65 74 54 79 70 65 28 29 20 73 65 74 73  ugSetType() sets
8bd30 20 74 68 65 20 22 74 79 70 65 22 20 6f 66 20 61   the "type" of a
8bd40 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20  n allocation to 
8bd50 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 4d 45  one of.** the ME
8bd60 4d 54 59 50 45 5f 2a 20 6d 61 63 72 6f 73 20 64  MTYPE_* macros d
8bd70 65 66 69 6e 65 64 20 62 65 6c 6f 77 2e 20 20 54  efined below.  T
8bd80 68 65 20 74 79 70 65 20 6d 75 73 74 20 62 65 20  he type must be 
8bd90 61 20 62 69 74 6d 61 73 6b 20 77 69 74 68 0a 2a  a bitmask with.*
8bda0 2a 20 61 20 73 69 6e 67 6c 65 20 62 69 74 20 73  * a single bit s
8bdb0 65 74 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65  et..**.** sqlite
8bdc0 33 4d 65 6d 64 65 62 75 67 48 61 73 54 79 70 65  3MemdebugHasType
8bdd0 28 29 20 72 65 74 75 72 6e 73 20 74 72 75 65 20  () returns true 
8bde0 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 62 69  if any of the bi
8bdf0 74 73 20 69 6e 20 69 74 73 20 73 65 63 6f 6e 64  ts in its second
8be00 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 61 74  .** argument mat
8be10 63 68 20 74 68 65 20 74 79 70 65 20 73 65 74 20  ch the type set 
8be20 62 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  by the previous 
8be30 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53  sqlite3MemdebugS
8be40 65 74 54 79 70 65 28 29 2e 0a 2a 2a 20 73 71 6c  etType()..** sql
8be50 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54  ite3MemdebugHasT
8be60 79 70 65 28 29 20 69 73 20 69 6e 74 65 6e 64 65  ype() is intende
8be70 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
8be80 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
8be90 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69  ents..**.** sqli
8bea0 74 65 33 4d 65 6d 64 65 62 75 67 4e 6f 54 79 70  te3MemdebugNoTyp
8beb0 65 28 29 20 72 65 74 75 72 6e 73 20 74 72 75 65  e() returns true
8bec0 20 69 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20   if none of the 
8bed0 62 69 74 73 20 69 6e 20 69 74 73 20 73 65 63 6f  bits in its seco
8bee0 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d  nd.** argument m
8bef0 61 74 63 68 20 74 68 65 20 74 79 70 65 20 73 65  atch the type se
8bf00 74 20 62 79 20 74 68 65 20 70 72 65 76 69 6f 75  t by the previou
8bf10 73 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  s sqlite3Memdebu
8bf20 67 53 65 74 54 79 70 65 28 29 2e 0a 2a 2a 0a 2a  gSetType()..**.*
8bf30 2a 20 50 65 72 68 61 70 73 20 74 68 65 20 6d 6f  * Perhaps the mo
8bf40 73 74 20 69 6d 70 6f 72 74 61 6e 74 20 70 6f 69  st important poi
8bf50 6e 74 20 69 73 20 74 68 65 20 64 69 66 66 65 72  nt is the differ
8bf60 65 6e 63 65 20 62 65 74 77 65 65 6e 20 4d 45 4d  ence between MEM
8bf70 54 59 50 45 5f 48 45 41 50 0a 2a 2a 20 61 6e 64  TYPE_HEAP.** and
8bf80 20 4d 45 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49   MEMTYPE_LOOKASI
8bf90 44 45 2e 20 20 49 66 20 61 6e 20 61 6c 6c 6f 63  DE.  If an alloc
8bfa0 61 74 69 6f 6e 20 69 73 20 4d 45 4d 54 59 50 45  ation is MEMTYPE
8bfb0 5f 4c 4f 4f 4b 41 53 49 44 45 2c 20 74 68 61 74  _LOOKASIDE, that
8bfc0 20 6d 65 61 6e 73 0a 2a 2a 20 69 74 20 6d 69 67   means.** it mig
8bfd0 68 74 20 68 61 76 65 20 62 65 65 6e 20 61 6c 6c  ht have been all
8bfe0 6f 63 61 74 65 64 20 62 79 20 6c 6f 6f 6b 61 73  ocated by lookas
8bff0 69 64 65 2c 20 65 78 63 65 70 74 20 74 68 65 20  ide, except the 
8c000 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 0a 2a  allocation was.*
8c010 2a 20 74 6f 6f 20 6c 61 72 67 65 20 6f 72 20 6c  * too large or l
8c020 6f 6f 6b 61 73 69 64 65 20 77 61 73 20 61 6c 72  ookaside was alr
8c030 65 61 64 79 20 66 75 6c 6c 2e 20 20 49 74 20 69  eady full.  It i
8c040 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 76  s important to v
8c050 65 72 69 66 79 0a 2a 2a 20 74 68 61 74 20 61 6c  erify.** that al
8c060 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6d  locations that m
8c070 69 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 73  ight have been s
8c080 61 74 69 73 66 69 65 64 20 62 79 20 6c 6f 6f 6b  atisfied by look
8c090 61 73 69 64 65 20 61 72 65 20 6e 6f 74 0a 2a 2a  aside are not.**
8c0a0 20 70 61 73 73 65 64 20 62 61 63 6b 20 74 6f 20   passed back to 
8c0b0 6e 6f 6e 2d 6c 6f 6f 6b 61 73 69 64 65 20 66 72  non-lookaside fr
8c0c0 65 65 28 29 20 72 6f 75 74 69 6e 65 73 2e 20 20  ee() routines.  
8c0d0 41 73 73 65 72 74 73 20 73 75 63 68 20 61 73 20  Asserts such as 
8c0e0 74 68 65 0a 2a 2a 20 65 78 61 6d 70 6c 65 20 61  the.** example a
8c0f0 62 6f 76 65 20 61 72 65 20 70 6c 61 63 65 64 20  bove are placed 
8c100 6f 6e 20 74 68 65 20 6e 6f 6e 2d 6c 6f 6f 6b 61  on the non-looka
8c110 73 69 64 65 20 66 72 65 65 28 29 20 72 6f 75 74  side free() rout
8c120 69 6e 65 73 20 74 6f 20 76 65 72 69 66 79 0a 2a  ines to verify.*
8c130 2a 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e  * this constrain
8c140 74 2e 20 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66  t. .**.** All of
8c150 20 74 68 69 73 20 69 73 20 6e 6f 2d 6f 70 20 66   this is no-op f
8c160 6f 72 20 61 20 70 72 6f 64 75 63 74 69 6f 6e 20  or a production 
8c170 62 75 69 6c 64 2e 20 20 49 74 20 6f 6e 6c 79 20  build.  It only 
8c180 63 6f 6d 65 73 20 69 6e 74 6f 0a 2a 2a 20 70 6c  comes into.** pl
8c190 61 79 20 77 68 65 6e 20 74 68 65 20 53 51 4c 49  ay when the SQLI
8c1a0 54 45 5f 4d 45 4d 44 45 42 55 47 20 63 6f 6d 70  TE_MEMDEBUG comp
8c1b0 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20  ile-time option 
8c1c0 69 73 20 75 73 65 64 2e 0a 2a 2f 0a 23 69 66 64  is used..*/.#ifd
8c1d0 65 66 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42  ef SQLITE_MEMDEB
8c1e0 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
8c1f0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
8c200 4d 65 6d 64 65 62 75 67 53 65 74 54 79 70 65 28  MemdebugSetType(
8c210 76 6f 69 64 2a 2c 75 38 29 3b 0a 53 51 4c 49 54  void*,u8);.SQLIT
8c220 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
8c230 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 48  sqlite3MemdebugH
8c240 61 73 54 79 70 65 28 76 6f 69 64 2a 2c 75 38 29  asType(void*,u8)
8c250 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
8c260 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65     int sqlite3Me
8c270 6d 64 65 62 75 67 4e 6f 54 79 70 65 28 76 6f 69  mdebugNoType(voi
8c280 64 2a 2c 75 38 29 3b 0a 23 65 6c 73 65 0a 23 20  d*,u8);.#else.# 
8c290 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 65  define sqlite3Me
8c2a0 6d 64 65 62 75 67 53 65 74 54 79 70 65 28 58 2c  mdebugSetType(X,
8c2b0 59 29 20 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a  Y)  /* no-op */.
8c2c0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
8c2d0 4d 65 6d 64 65 62 75 67 48 61 73 54 79 70 65 28  MemdebugHasType(
8c2e0 58 2c 59 29 20 20 31 0a 23 20 64 65 66 69 6e 65  X,Y)  1.# define
8c2f0 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67   sqlite3Memdebug
8c300 4e 6f 54 79 70 65 28 58 2c 59 29 20 20 20 31 0a  NoType(X,Y)   1.
8c310 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 4d  #endif.#define M
8c320 45 4d 54 59 50 45 5f 48 45 41 50 20 20 20 20 20  EMTYPE_HEAP     
8c330 20 20 30 78 30 31 20 20 2f 2a 20 47 65 6e 65 72    0x01  /* Gener
8c340 61 6c 20 68 65 61 70 20 61 6c 6c 6f 63 61 74 69  al heap allocati
8c350 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  ons */.#define M
8c360 45 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45  EMTYPE_LOOKASIDE
8c370 20 20 30 78 30 32 20 20 2f 2a 20 4d 69 67 68 74    0x02  /* Might
8c380 20 68 61 76 65 20 62 65 65 6e 20 6c 6f 6f 6b 61   have been looka
8c390 73 69 64 65 20 6d 65 6d 6f 72 79 20 2a 2f 0a 23  side memory */.#
8c3a0 64 65 66 69 6e 65 20 4d 45 4d 54 59 50 45 5f 53  define MEMTYPE_S
8c3b0 43 52 41 54 43 48 20 20 20 20 30 78 30 34 20 20  CRATCH    0x04  
8c3c0 2f 2a 20 53 63 72 61 74 63 68 20 61 6c 6c 6f 63  /* Scratch alloc
8c3d0 61 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e  ations */.#defin
8c3e0 65 20 4d 45 4d 54 59 50 45 5f 50 43 41 43 48 45  e MEMTYPE_PCACHE
8c3f0 20 20 20 20 20 30 78 30 38 20 20 2f 2a 20 50 61       0x08  /* Pa
8c400 67 65 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 74  ge cache allocat
8c410 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ions */.#define 
8c420 4d 45 4d 54 59 50 45 5f 44 42 20 20 20 20 20 20  MEMTYPE_DB      
8c430 20 20 20 30 78 31 30 20 20 2f 2a 20 55 73 65 73     0x10  /* Uses
8c440 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
8c450 2c 20 6e 6f 74 20 73 71 6c 69 74 65 5f 6d 61 6c  , not sqlite_mal
8c460 6c 6f 63 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f  loc */..#endif /
8c470 2a 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f 20  * _SQLITEINT_H_ 
8c480 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
8c490 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74  *** End of sqlit
8c4a0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
8c4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c4d0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
8c4e0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 67  *** Begin file g
8c4f0 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  lobal.c ********
8c500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c520 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a  **/./*.** 2008 J
8c530 75 6e 65 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65  une 13.**.** The
8c540 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
8c550 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
8c560 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
8c570 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
8c580 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
8c590 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
8c5a0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
8c5b0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
8c5c0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
8c5d0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
8c5e0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
8c5f0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
8c600 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
8c610 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
8c620 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
8c630 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
8c640 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
8c650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c690 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
8c6a0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 64   file contains d
8c6b0 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 67 6c  efinitions of gl
8c6c0 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 61  obal variables a
8c6d0 6e 64 20 63 6f 6e 74 61 6e 74 73 2e 0a 2a 2f 0a  nd contants..*/.
8c6e0 0a 2f 2a 20 41 6e 20 61 72 72 61 79 20 74 6f 20  ./* An array to 
8c6f0 6d 61 70 20 61 6c 6c 20 75 70 70 65 72 2d 63 61  map all upper-ca
8c700 73 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  se characters in
8c710 74 6f 20 74 68 65 69 72 20 63 6f 72 72 65 73 70  to their corresp
8c720 6f 6e 64 69 6e 67 0a 2a 2a 20 6c 6f 77 65 72 2d  onding.** lower-
8c730 63 61 73 65 20 63 68 61 72 61 63 74 65 72 2e 20  case character. 
8c740 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6f 6e  .**.** SQLite on
8c750 6c 79 20 63 6f 6e 73 69 64 65 72 73 20 55 53 2d  ly considers US-
8c760 41 53 43 49 49 20 28 6f 72 20 45 42 43 44 49 43  ASCII (or EBCDIC
8c770 29 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 57  ) characters.  W
8c780 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20 68 61 6e 64  e do not.** hand
8c790 6c 65 20 63 61 73 65 20 63 6f 6e 76 65 72 73 69  le case conversi
8c7a0 6f 6e 73 20 66 6f 72 20 74 68 65 20 55 54 46 20  ons for the UTF 
8c7b0 63 68 61 72 61 63 74 65 72 20 73 65 74 20 73 69  character set si
8c7c0 6e 63 65 20 74 68 65 20 74 61 62 6c 65 73 0a 2a  nce the tables.*
8c7d0 2a 20 69 6e 76 6f 6c 76 65 64 20 61 72 65 20 6e  * involved are n
8c7e0 65 61 72 6c 79 20 61 73 20 62 69 67 20 6f 72 20  early as big or 
8c7f0 62 69 67 67 65 72 20 74 68 61 6e 20 53 51 4c 69  bigger than SQLi
8c800 74 65 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51  te itself..*/.SQ
8c810 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
8c820 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
8c830 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c   sqlite3UpperToL
8c840 6f 77 65 72 5b 5d 20 3d 20 7b 0a 23 69 66 64 65  ower[] = {.#ifde
8c850 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 20  f SQLITE_ASCII. 
8c860 20 20 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 20       0,  1,  2, 
8c870 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 20   3,  4,  5,  6, 
8c880 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 20   7,  8,  9, 10, 
8c890 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 20  11, 12, 13, 14, 
8c8a0 31 35 2c 20 31 36 2c 20 31 37 2c 0a 20 20 20 20  15, 16, 17,.    
8c8b0 20 31 38 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c   18, 19, 20, 21,
8c8c0 20 32 32 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c   22, 23, 24, 25,
8c8d0 20 32 36 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c   26, 27, 28, 29,
8c8e0 20 33 30 2c 20 33 31 2c 20 33 32 2c 20 33 33 2c   30, 31, 32, 33,
8c8f0 20 33 34 2c 20 33 35 2c 0a 20 20 20 20 20 33 36   34, 35,.     36
8c900 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30  , 37, 38, 39, 40
8c910 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34  , 41, 42, 43, 44
8c920 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c 20 34 38  , 45, 46, 47, 48
8c930 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 32  , 49, 50, 51, 52
8c940 2c 20 35 33 2c 0a 20 20 20 20 20 35 34 2c 20 35  , 53,.     54, 5
8c950 35 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35  5, 56, 57, 58, 5
8c960 39 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36  9, 60, 61, 62, 6
8c970 33 2c 20 36 34 2c 20 39 37 2c 20 39 38 2c 20 39  3, 64, 97, 98, 9
8c980 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30  9,100,101,102,10
8c990 33 2c 0a 20 20 20 20 31 30 34 2c 31 30 35 2c 31  3,.    104,105,1
8c9a0 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31  06,107,108,109,1
8c9b0 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31  10,111,112,113,1
8c9c0 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31  14,115,116,117,1
8c9d0 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 0a  18,119,120,121,.
8c9e0 20 20 20 20 31 32 32 2c 20 39 31 2c 20 39 32 2c      122, 91, 92,
8c9f0 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 39 36 2c   93, 94, 95, 96,
8ca00 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c   97, 98, 99,100,
8ca10 31 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c  101,102,103,104,
8ca20 31 30 35 2c 31 30 36 2c 31 30 37 2c 0a 20 20 20  105,106,107,.   
8ca30 20 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31   108,109,110,111
8ca40 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 35  ,112,113,114,115
8ca50 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39  ,116,117,118,119
8ca60 2c 31 32 30 2c 31 32 31 2c 31 32 32 2c 31 32 33  ,120,121,122,123
8ca70 2c 31 32 34 2c 31 32 35 2c 0a 20 20 20 20 31 32  ,124,125,.    12
8ca80 36 2c 31 32 37 2c 31 32 38 2c 31 32 39 2c 31 33  6,127,128,129,13
8ca90 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33  0,131,132,133,13
8caa0 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33  4,135,136,137,13
8cab0 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34  8,139,140,141,14
8cac0 32 2c 31 34 33 2c 0a 20 20 20 20 31 34 34 2c 31  2,143,.    144,1
8cad0 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31  45,146,147,148,1
8cae0 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31  49,150,151,152,1
8caf0 35 33 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c 31  53,154,155,156,1
8cb00 35 37 2c 31 35 38 2c 31 35 39 2c 31 36 30 2c 31  57,158,159,160,1
8cb10 36 31 2c 0a 20 20 20 20 31 36 32 2c 31 36 33 2c  61,.    162,163,
8cb20 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c  164,165,166,167,
8cb30 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c  168,169,170,171,
8cb40 31 37 32 2c 31 37 33 2c 31 37 34 2c 31 37 35 2c  172,173,174,175,
8cb50 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c  176,177,178,179,
8cb60 0a 20 20 20 20 31 38 30 2c 31 38 31 2c 31 38 32  .    180,181,182
8cb70 2c 31 38 33 2c 31 38 34 2c 31 38 35 2c 31 38 36  ,183,184,185,186
8cb80 2c 31 38 37 2c 31 38 38 2c 31 38 39 2c 31 39 30  ,187,188,189,190
8cb90 2c 31 39 31 2c 31 39 32 2c 31 39 33 2c 31 39 34  ,191,192,193,194
8cba0 2c 31 39 35 2c 31 39 36 2c 31 39 37 2c 0a 20 20  ,195,196,197,.  
8cbb0 20 20 31 39 38 2c 31 39 39 2c 32 30 30 2c 32 30    198,199,200,20
8cbc0 31 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30  1,202,203,204,20
8cbd0 35 2c 32 30 36 2c 32 30 37 2c 32 30 38 2c 32 30  5,206,207,208,20
8cbe0 39 2c 32 31 30 2c 32 31 31 2c 32 31 32 2c 32 31  9,210,211,212,21
8cbf0 33 2c 32 31 34 2c 32 31 35 2c 0a 20 20 20 20 32  3,214,215,.    2
8cc00 31 36 2c 32 31 37 2c 32 31 38 2c 32 31 39 2c 32  16,217,218,219,2
8cc10 32 30 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 32  20,221,222,223,2
8cc20 32 34 2c 32 32 35 2c 32 32 36 2c 32 32 37 2c 32  24,225,226,227,2
8cc30 32 38 2c 32 32 39 2c 32 33 30 2c 32 33 31 2c 32  28,229,230,231,2
8cc40 33 32 2c 32 33 33 2c 0a 20 20 20 20 32 33 34 2c  32,233,.    234,
8cc50 32 33 35 2c 32 33 36 2c 32 33 37 2c 32 33 38 2c  235,236,237,238,
8cc60 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c  239,240,241,242,
8cc70 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c  243,244,245,246,
8cc80 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 35 30 2c  247,248,249,250,
8cc90 32 35 31 2c 0a 20 20 20 20 32 35 32 2c 32 35 33  251,.    252,253
8cca0 2c 32 35 34 2c 32 35 35 0a 23 65 6e 64 69 66 0a  ,254,255.#endif.
8ccb0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42  #ifdef SQLITE_EB
8ccc0 43 44 49 43 0a 20 20 20 20 20 20 30 2c 20 20 31  CDIC.      0,  1
8ccd0 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 20 35  ,  2,  3,  4,  5
8cce0 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 20 39  ,  6,  7,  8,  9
8ccf0 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 31 33  , 10, 11, 12, 13
8cd00 2c 20 31 34 2c 20 31 35 2c 20 2f 2a 20 30 78 20  , 14, 15, /* 0x 
8cd10 2a 2f 0a 20 20 20 20 20 31 36 2c 20 31 37 2c 20  */.     16, 17, 
8cd20 31 38 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20  18, 19, 20, 21, 
8cd30 32 32 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20  22, 23, 24, 25, 
8cd40 32 36 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20  26, 27, 28, 29, 
8cd50 33 30 2c 20 33 31 2c 20 2f 2a 20 31 78 20 2a 2f  30, 31, /* 1x */
8cd60 0a 20 20 20 20 20 33 32 2c 20 33 33 2c 20 33 34  .     32, 33, 34
8cd70 2c 20 33 35 2c 20 33 36 2c 20 33 37 2c 20 33 38  , 35, 36, 37, 38
8cd80 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32  , 39, 40, 41, 42
8cd90 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36  , 43, 44, 45, 46
8cda0 2c 20 34 37 2c 20 2f 2a 20 32 78 20 2a 2f 0a 20  , 47, /* 2x */. 
8cdb0 20 20 20 20 34 38 2c 20 34 39 2c 20 35 30 2c 20      48, 49, 50, 
8cdc0 35 31 2c 20 35 32 2c 20 35 33 2c 20 35 34 2c 20  51, 52, 53, 54, 
8cdd0 35 35 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 20  55, 56, 57, 58, 
8cde0 35 39 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 20  59, 60, 61, 62, 
8cdf0 36 33 2c 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20  63, /* 3x */.   
8ce00 20 20 36 34 2c 20 36 35 2c 20 36 36 2c 20 36 37    64, 65, 66, 67
8ce10 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 31  , 68, 69, 70, 71
8ce20 2c 20 37 32 2c 20 37 33 2c 20 37 34 2c 20 37 35  , 72, 73, 74, 75
8ce30 2c 20 37 36 2c 20 37 37 2c 20 37 38 2c 20 37 39  , 76, 77, 78, 79
8ce40 2c 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 20  , /* 4x */.     
8ce50 38 30 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c 20  80, 81, 82, 83, 
8ce60 38 34 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c 20  84, 85, 86, 87, 
8ce70 38 38 2c 20 38 39 2c 20 39 30 2c 20 39 31 2c 20  88, 89, 90, 91, 
8ce80 39 32 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c 20  92, 93, 94, 95, 
8ce90 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 20 39 36  /* 5x */.     96
8cea0 2c 20 39 37 2c 20 36 36 2c 20 36 37 2c 20 36 38  , 97, 66, 67, 68
8ceb0 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 32  , 69, 70, 71, 72
8cec0 2c 20 37 33 2c 31 30 36 2c 31 30 37 2c 31 30 38  , 73,106,107,108
8ced0 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 20 2f 2a  ,109,110,111, /*
8cee0 20 36 78 20 2a 2f 0a 20 20 20 20 31 31 32 2c 20   6x */.    112, 
8cef0 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 20  81, 82, 83, 84, 
8cf00 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 20  85, 86, 87, 88, 
8cf10 38 39 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 31  89,122,123,124,1
8cf20 32 35 2c 31 32 36 2c 31 32 37 2c 20 2f 2a 20 37  25,126,127, /* 7
8cf30 78 20 2a 2f 0a 20 20 20 20 31 32 38 2c 31 32 39  x */.    128,129
8cf40 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33  ,130,131,132,133
8cf50 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37  ,134,135,136,137
8cf60 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c 31 34 31  ,138,139,140,141
8cf70 2c 31 34 32 2c 31 34 33 2c 20 2f 2a 20 38 78 20  ,142,143, /* 8x 
8cf80 2a 2f 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 31  */.    144,145,1
8cf90 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31  46,147,148,149,1
8cfa0 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 31  50,151,152,153,1
8cfb0 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 31  54,155,156,157,1
8cfc0 35 36 2c 31 35 39 2c 20 2f 2a 20 39 78 20 2a 2f  56,159, /* 9x */
8cfd0 0a 20 20 20 20 31 36 30 2c 31 36 31 2c 31 36 32  .    160,161,162
8cfe0 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36  ,163,164,165,166
8cff0 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 31 37 30  ,167,168,169,170
8d000 2c 31 37 31 2c 31 34 30 2c 31 34 31 2c 31 34 32  ,171,140,141,142
8d010 2c 31 37 35 2c 20 2f 2a 20 41 78 20 2a 2f 0a 20  ,175, /* Ax */. 
8d020 20 20 20 31 37 36 2c 31 37 37 2c 31 37 38 2c 31     176,177,178,1
8d030 37 39 2c 31 38 30 2c 31 38 31 2c 31 38 32 2c 31  79,180,181,182,1
8d040 38 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31  83,184,185,186,1
8d050 38 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31  87,188,189,190,1
8d060 39 31 2c 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20  91, /* Bx */.   
8d070 20 31 39 32 2c 31 32 39 2c 31 33 30 2c 31 33 31   192,129,130,131
8d080 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35  ,132,133,134,135
8d090 2c 31 33 36 2c 31 33 37 2c 32 30 32 2c 32 30 33  ,136,137,202,203
8d0a0 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37  ,204,205,206,207
8d0b0 2c 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 20 32  , /* Cx */.    2
8d0c0 30 38 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31  08,145,146,147,1
8d0d0 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31  48,149,150,151,1
8d0e0 35 32 2c 31 35 33 2c 32 31 38 2c 32 31 39 2c 32  52,153,218,219,2
8d0f0 32 30 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 20  20,221,222,223, 
8d100 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20 32 32 34  /* Dx */.    224
8d110 2c 32 32 35 2c 31 36 32 2c 31 36 33 2c 31 36 34  ,225,162,163,164
8d120 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38  ,165,166,167,168
8d130 2c 31 36 39 2c 32 33 32 2c 32 30 33 2c 32 30 34  ,169,232,203,204
8d140 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a  ,205,206,207, /*
8d150 20 45 78 20 2a 2f 0a 20 20 20 20 32 33 39 2c 32   Ex */.    239,2
8d160 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c 32  40,241,242,243,2
8d170 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c 32  44,245,246,247,2
8d180 34 38 2c 32 34 39 2c 32 31 39 2c 32 32 30 2c 32  48,249,219,220,2
8d190 32 31 2c 32 32 32 2c 32 35 35 2c 20 2f 2a 20 46  21,222,255, /* F
8d1a0 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a  x */.#endif.};..
8d1b0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
8d1c0 69 6e 67 20 32 35 36 20 62 79 74 65 20 6c 6f 6f  ing 256 byte loo
8d1d0 6b 75 70 20 74 61 62 6c 65 20 69 73 20 75 73 65  kup table is use
8d1e0 64 20 74 6f 20 73 75 70 70 6f 72 74 20 53 51 4c  d to support SQL
8d1f0 69 74 65 73 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a  ites built-in.**
8d200 20 65 71 75 69 76 61 6c 65 6e 74 73 20 74 6f 20   equivalents to 
8d210 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
8d220 61 6e 64 61 72 64 20 6c 69 62 72 61 72 79 20 66  andard library f
8d230 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20  unctions:.**.** 
8d240 20 20 69 73 73 70 61 63 65 28 29 20 20 20 20 20    isspace()     
8d250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8d260 20 20 20 30 78 30 31 0a 2a 2a 20 20 20 69 73 61     0x01.**   isa
8d270 6c 70 68 61 28 29 20 20 20 20 20 20 20 20 20 20  lpha()          
8d280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78                0x
8d290 30 32 0a 2a 2a 20 20 20 69 73 64 69 67 69 74 28  02.**   isdigit(
8d2a0 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
8d2b0 20 20 20 20 20 20 20 20 20 30 78 30 34 0a 2a 2a           0x04.**
8d2c0 20 20 20 69 73 61 6c 6e 75 6d 28 29 20 20 20 20     isalnum()    
8d2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8d2e0 20 20 20 20 30 78 30 36 0a 2a 2a 20 20 20 69 73      0x06.**   is
8d2f0 78 64 69 67 69 74 28 29 20 20 20 20 20 20 20 20  xdigit()        
8d300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
8d310 78 30 38 0a 2a 2a 20 20 20 74 6f 75 70 70 65 72  x08.**   toupper
8d320 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ()              
8d330 20 20 20 20 20 20 20 20 20 20 30 78 32 30 0a 2a            0x20.*
8d340 2a 20 20 20 53 51 4c 69 74 65 20 69 64 65 6e 74  *   SQLite ident
8d350 69 66 69 65 72 20 63 68 61 72 61 63 74 65 72 20  ifier character 
8d360 20 20 20 20 20 30 78 34 30 0a 2a 2a 0a 2a 2a 20       0x40.**.** 
8d370 42 69 74 20 30 78 32 30 20 69 73 20 73 65 74 20  Bit 0x20 is set 
8d380 69 66 20 74 68 65 20 6d 61 70 70 65 64 20 63 68  if the mapped ch
8d390 61 72 61 63 74 65 72 20 72 65 71 75 69 72 65 73  aracter requires
8d3a0 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 6f 20   translation to 
8d3b0 75 70 70 65 72 0a 2a 2a 20 63 61 73 65 2e 20 69  upper.** case. i
8d3c0 2e 65 2e 20 69 66 20 74 68 65 20 63 68 61 72 61  .e. if the chara
8d3d0 63 74 65 72 20 69 73 20 61 20 6c 6f 77 65 72 2d  cter is a lower-
8d3e0 63 61 73 65 20 41 53 43 49 49 20 63 68 61 72 61  case ASCII chara
8d3f0 63 74 65 72 2e 0a 2a 2a 20 49 66 20 78 20 69 73  cter..** If x is
8d400 20 61 20 6c 6f 77 65 72 2d 63 61 73 65 20 41 53   a lower-case AS
8d410 43 49 49 20 63 68 61 72 61 63 74 65 72 2c 20 74  CII character, t
8d420 68 65 6e 20 69 74 73 20 75 70 70 65 72 2d 63 61  hen its upper-ca
8d430 73 65 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a  se equivalent.**
8d440 20 69 73 20 28 78 20 2d 20 30 78 32 30 29 2e 20   is (x - 0x20). 
8d450 54 68 65 72 65 66 6f 72 65 20 74 6f 75 70 70 65  Therefore touppe
8d460 72 28 29 20 63 61 6e 20 62 65 20 69 6d 70 6c 65  r() can be imple
8d470 6d 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a 2a 2a  mented as:.**.**
8d480 20 20 20 28 78 20 26 20 7e 28 6d 61 70 5b 78 5d     (x & ~(map[x]
8d490 26 30 78 32 30 29 29 0a 2a 2a 0a 2a 2a 20 53 74  &0x20)).**.** St
8d4a0 61 6e 64 61 72 64 20 66 75 6e 63 74 69 6f 6e 20  andard function 
8d4b0 74 6f 6c 6f 77 65 72 28 29 20 69 73 20 69 6d 70  tolower() is imp
8d4c0 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 74  lemented using t
8d4d0 68 65 20 73 71 6c 69 74 65 33 55 70 70 65 72 54  he sqlite3UpperT
8d4e0 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a 20 61 72 72 61  oLower[].** arra
8d4f0 79 2e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 20  y. tolower() is 
8d500 75 73 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e 20  used more often 
8d510 74 68 61 6e 20 74 6f 75 70 70 65 72 28 29 20 62  than toupper() b
8d520 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  y SQLite..**.** 
8d530 42 69 74 20 30 78 34 30 20 69 73 20 73 65 74 20  Bit 0x40 is set 
8d540 69 66 20 74 68 65 20 63 68 61 72 61 63 74 65 72  if the character
8d550 20 6e 6f 6e 2d 61 6c 70 68 61 6e 75 6d 65 72 69   non-alphanumeri
8d560 63 20 61 6e 64 20 63 61 6e 20 62 65 20 75 73 65  c and can be use
8d570 64 20 69 6e 20 61 6e 20 0a 2a 2a 20 53 51 4c 69  d in an .** SQLi
8d580 74 65 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20  te identifier.  
8d590 49 64 65 6e 74 69 66 69 65 72 73 20 61 72 65 20  Identifiers are 
8d5a0 61 6c 70 68 61 6e 75 6d 65 72 69 63 73 2c 20 22  alphanumerics, "
8d5b0 5f 22 2c 20 22 24 22 2c 20 61 6e 64 20 61 6e 79  _", "$", and any
8d5c0 0a 2a 2a 20 6e 6f 6e 2d 41 53 43 49 49 20 55 54  .** non-ASCII UT
8d5d0 46 20 63 68 61 72 61 63 74 65 72 2e 20 48 65 6e  F character. Hen
8d5e0 63 65 20 74 68 65 20 74 65 73 74 20 66 6f 72 20  ce the test for 
8d5f0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61  whether or not a
8d600 20 63 68 61 72 61 63 74 65 72 20 69 73 0a 2a 2a   character is.**
8d610 20 70 61 72 74 20 6f 66 20 61 6e 20 69 64 65 6e   part of an iden
8d620 74 69 66 69 65 72 20 69 73 20 30 78 34 36 2e 0a  tifier is 0x46..
8d630 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 27 73 20 76  **.** SQLite's v
8d640 65 72 73 69 6f 6e 73 20 61 72 65 20 69 64 65 6e  ersions are iden
8d650 74 69 63 61 6c 20 74 6f 20 74 68 65 20 73 74 61  tical to the sta
8d660 6e 64 61 72 64 20 76 65 72 73 69 6f 6e 73 20 61  ndard versions a
8d670 73 73 75 6d 69 6e 67 20 61 0a 2a 2a 20 6c 6f 63  ssuming a.** loc
8d680 61 6c 65 20 6f 66 20 22 43 22 2e 20 54 68 65 79  ale of "C". They
8d690 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64   are implemented
8d6a0 20 61 73 20 6d 61 63 72 6f 73 20 69 6e 20 73 71   as macros in sq
8d6b0 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 23 69  liteInt.h..*/.#i
8d6c0 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49  fdef SQLITE_ASCI
8d6d0 49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  I.SQLITE_PRIVATE
8d6e0 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
8d6f0 63 68 61 72 20 73 71 6c 69 74 65 33 43 74 79 70  char sqlite3Ctyp
8d700 65 4d 61 70 5b 32 35 36 5d 20 3d 20 7b 0a 20 20  eMap[256] = {.  
8d710 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
8d720 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
8d730 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
8d740 20 2f 2a 20 30 30 2e 2e 30 37 20 20 20 20 2e 2e   /* 00..07    ..
8d750 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30  ...... */.  0x00
8d760 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78  , 0x01, 0x01, 0x
8d770 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20  01, 0x01, 0x01, 
8d780 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
8d790 30 38 2e 2e 30 66 20 20 20 20 2e 2e 2e 2e 2e 2e  08..0f    ......
8d7a0 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  .. */.  0x00, 0x
8d7b0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
8d7c0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
8d7d0 2c 20 30 78 30 30 2c 20 20 2f 2a 20 31 30 2e 2e  , 0x00,  /* 10..
8d7e0 31 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  17    ........ *
8d7f0 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20  /.  0x00, 0x00, 
8d800 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
8d810 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
8d820 30 30 2c 20 20 2f 2a 20 31 38 2e 2e 31 66 20 20  00,  /* 18..1f  
8d830 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20    ........ */.  
8d840 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30  0x01, 0x00, 0x00
8d850 2c 20 30 78 30 30 2c 20 30 78 34 30 2c 20 30 78  , 0x00, 0x40, 0x
8d860 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
8d870 20 2f 2a 20 32 30 2e 2e 32 37 20 20 20 20 20 21   /* 20..27     !
8d880 22 23 24 25 26 27 20 2a 2f 0a 20 20 30 78 30 30  "#$%&' */.  0x00
8d890 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
8d8a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
8d8b0 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
8d8c0 32 38 2e 2e 32 66 20 20 20 20 28 29 2a 2b 2c 2d  28..2f    ()*+,-
8d8d0 2e 2f 20 2a 2f 0a 20 20 30 78 30 63 2c 20 30 78  ./ */.  0x0c, 0x
8d8e0 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20  0c, 0x0c, 0x0c, 
8d8f0 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63  0x0c, 0x0c, 0x0c
8d900 2c 20 30 78 30 63 2c 20 20 2f 2a 20 33 30 2e 2e  , 0x0c,  /* 30..
8d910 33 37 20 20 20 20 30 31 32 33 34 35 36 37 20 2a  37    01234567 *
8d920 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c 20  /.  0x0c, 0x0c, 
8d930 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
8d940 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
8d950 30 30 2c 20 20 2f 2a 20 33 38 2e 2e 33 66 20 20  00,  /* 38..3f  
8d960 20 20 38 39 3a 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20    89:;<=>? */.. 
8d970 20 30 78 30 30 2c 20 30 78 30 61 2c 20 30 78 30   0x00, 0x0a, 0x0
8d980 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30  a, 0x0a, 0x0a, 0
8d990 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 32 2c  x0a, 0x0a, 0x02,
8d9a0 20 20 2f 2a 20 34 30 2e 2e 34 37 20 20 20 20 40    /* 40..47    @
8d9b0 41 42 43 44 45 46 47 20 2a 2f 0a 20 20 30 78 30  ABCDEFG */.  0x0
8d9c0 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30  2, 0x02, 0x02, 0
8d9d0 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c  x02, 0x02, 0x02,
8d9e0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 20 2f 2a   0x02, 0x02,  /*
8d9f0 20 34 38 2e 2e 34 66 20 20 20 20 48 49 4a 4b 4c   48..4f    HIJKL
8da00 4d 4e 4f 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30  MNO */.  0x02, 0
8da10 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c  x02, 0x02, 0x02,
8da20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
8da30 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 35 30 2e  2, 0x02,  /* 50.
8da40 2e 35 37 20 20 20 20 50 51 52 53 54 55 56 57 20  .57    PQRSTUVW 
8da50 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c  */.  0x02, 0x02,
8da60 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30   0x02, 0x00, 0x0
8da70 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
8da80 78 34 30 2c 20 20 2f 2a 20 35 38 2e 2e 35 66 20  x40,  /* 58..5f 
8da90 20 20 20 58 59 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20     XYZ[\]^_ */. 
8daa0 20 30 78 30 30 2c 20 30 78 32 61 2c 20 30 78 32   0x00, 0x2a, 0x2
8dab0 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30  a, 0x2a, 0x2a, 0
8dac0 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 32 2c  x2a, 0x2a, 0x22,
8dad0 20 20 2f 2a 20 36 30 2e 2e 36 37 20 20 20 20 60    /* 60..67    `
8dae0 61 62 63 64 65 66 67 20 2a 2f 0a 20 20 30 78 32  abcdefg */.  0x2
8daf0 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30  2, 0x22, 0x22, 0
8db00 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c  x22, 0x22, 0x22,
8db10 20 30 78 32 32 2c 20 30 78 32 32 2c 20 20 2f 2a   0x22, 0x22,  /*
8db20 20 36 38 2e 2e 36 66 20 20 20 20 68 69 6a 6b 6c   68..6f    hijkl
8db30 6d 6e 6f 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30  mno */.  0x22, 0
8db40 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c  x22, 0x22, 0x22,
8db50 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32   0x22, 0x22, 0x2
8db60 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 37 30 2e  2, 0x22,  /* 70.
8db70 2e 37 37 20 20 20 20 70 71 72 73 74 75 76 77 20  .77    pqrstuvw 
8db80 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c  */.  0x22, 0x22,
8db90 20 30 78 32 32 2c 20 30 78 30 30 2c 20 30 78 30   0x22, 0x00, 0x0
8dba0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
8dbb0 78 30 30 2c 20 20 2f 2a 20 37 38 2e 2e 37 66 20  x00,  /* 78..7f 
8dbc0 20 20 20 78 79 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a     xyz{|}~. */..
8dbd0 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78    0x40, 0x40, 0x
8dbe0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
8dbf0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
8dc00 2c 20 20 2f 2a 20 38 30 2e 2e 38 37 20 20 20 20  ,  /* 80..87    
8dc10 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78  ........ */.  0x
8dc20 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
8dc30 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
8dc40 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f  , 0x40, 0x40,  /
8dc50 2a 20 38 38 2e 2e 38 66 20 20 20 20 2e 2e 2e 2e  * 88..8f    ....
8dc60 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20  .... */.  0x40, 
8dc70 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
8dc80 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
8dc90 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 39 30  40, 0x40,  /* 90
8dca0 2e 2e 39 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..97    ........
8dcb0 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30   */.  0x40, 0x40
8dcc0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
8dcd0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
8dce0 30 78 34 30 2c 20 20 2f 2a 20 39 38 2e 2e 39 66  0x40,  /* 98..9f
8dcf0 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
8dd00 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78    0x40, 0x40, 0x
8dd10 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
8dd20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
8dd30 2c 20 20 2f 2a 20 61 30 2e 2e 61 37 20 20 20 20  ,  /* a0..a7    
8dd40 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78  ........ */.  0x
8dd50 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
8dd60 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
8dd70 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f  , 0x40, 0x40,  /
8dd80 2a 20 61 38 2e 2e 61 66 20 20 20 20 2e 2e 2e 2e  * a8..af    ....
8dd90 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20  .... */.  0x40, 
8dda0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
8ddb0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
8ddc0 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 62 30  40, 0x40,  /* b0
8ddd0 2e 2e 62 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..b7    ........
8dde0 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30   */.  0x40, 0x40
8ddf0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
8de00 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
8de10 30 78 34 30 2c 20 20 2f 2a 20 62 38 2e 2e 62 66  0x40,  /* b8..bf
8de20 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
8de30 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  .  0x40, 0x40, 0
8de40 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
8de50 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8de60 30 2c 20 20 2f 2a 20 63 30 2e 2e 63 37 20 20 20  0,  /* c0..c7   
8de70 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
8de80 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
8de90 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8dea0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20  0, 0x40, 0x40,  
8deb0 2f 2a 20 63 38 2e 2e 63 66 20 20 20 20 2e 2e 2e  /* c8..cf    ...
8dec0 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c  ..... */.  0x40,
8ded0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8dee0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
8def0 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 64  x40, 0x40,  /* d
8df00 30 2e 2e 64 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  0..d7    .......
8df10 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34  . */.  0x40, 0x4
8df20 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
8df30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
8df40 20 30 78 34 30 2c 20 20 2f 2a 20 64 38 2e 2e 64   0x40,  /* d8..d
8df50 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  f    ........ */
8df60 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  .  0x40, 0x40, 0
8df70 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
8df80 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8df90 30 2c 20 20 2f 2a 20 65 30 2e 2e 65 37 20 20 20  0,  /* e0..e7   
8dfa0 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
8dfb0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
8dfc0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8dfd0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20  0, 0x40, 0x40,  
8dfe0 2f 2a 20 65 38 2e 2e 65 66 20 20 20 20 2e 2e 2e  /* e8..ef    ...
8dff0 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c  ..... */.  0x40,
8e000 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
8e010 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
8e020 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 66  x40, 0x40,  /* f
8e030 30 2e 2e 66 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  0..f7    .......
8e040 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34  . */.  0x40, 0x4
8e050 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
8e060 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
8e070 20 30 78 34 30 20 20 20 2f 2a 20 66 38 2e 2e 66   0x40   /* f8..f
8e080 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  f    ........ */
8e090 0a 7d 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  .};.#endif..#ifn
8e0a0 64 65 66 20 53 51 4c 49 54 45 5f 55 53 45 5f 55  def SQLITE_USE_U
8e0b0 52 49 0a 23 20 64 65 66 69 6e 65 20 20 53 51 4c  RI.# define  SQL
8e0c0 49 54 45 5f 55 53 45 5f 55 52 49 20 30 0a 23 65  ITE_USE_URI 0.#e
8e0d0 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
8e0e0 4c 49 54 45 5f 41 4c 4c 4f 57 5f 43 4f 56 45 52  LITE_ALLOW_COVER
8e0f0 49 4e 47 5f 49 4e 44 45 58 5f 53 43 41 4e 0a 23  ING_INDEX_SCAN.#
8e100 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41   define SQLITE_A
8e110 4c 4c 4f 57 5f 43 4f 56 45 52 49 4e 47 5f 49 4e  LLOW_COVERING_IN
8e120 44 45 58 5f 53 43 41 4e 20 31 0a 23 65 6e 64 69  DEX_SCAN 1.#endi
8e130 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  f../*.** The fol
8e140 6c 6f 77 69 6e 67 20 73 69 6e 67 6c 65 74 6f 6e  lowing singleton
8e150 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 67 6c   contains the gl
8e160 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69  obal configurati
8e170 6f 6e 20 66 6f 72 0a 2a 2a 20 74 68 65 20 53 51  on for.** the SQ
8e180 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f  Lite library..*/
8e190 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8e1a0 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63  SQLITE_WSD struc
8e1b0 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20  t Sqlite3Config 
8e1c0 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 3d 20  sqlite3Config = 
8e1d0 7b 0a 20 20 20 53 51 4c 49 54 45 5f 44 45 46 41  {.   SQLITE_DEFA
8e1e0 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 2c 20 20  ULT_MEMSTATUS,  
8e1f0 2f 2a 20 62 4d 65 6d 73 74 61 74 20 2a 2f 0a 20  /* bMemstat */. 
8e200 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20    1,            
8e210 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
8e220 62 43 6f 72 65 4d 75 74 65 78 20 2a 2f 0a 20 20  bCoreMutex */.  
8e230 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
8e240 46 45 3d 3d 31 2c 20 20 20 20 20 20 2f 2a 20 62  FE==1,      /* b
8e250 46 75 6c 6c 4d 75 74 65 78 20 2a 2f 0a 20 20 20  FullMutex */.   
8e260 53 51 4c 49 54 45 5f 55 53 45 5f 55 52 49 2c 20  SQLITE_USE_URI, 
8e270 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 62 4f             /* bO
8e280 70 65 6e 55 72 69 20 2a 2f 0a 20 20 20 53 51 4c  penUri */.   SQL
8e290 49 54 45 5f 41 4c 4c 4f 57 5f 43 4f 56 45 52 49  ITE_ALLOW_COVERI
8e2a0 4e 47 5f 49 4e 44 45 58 5f 53 43 41 4e 2c 20 20  NG_INDEX_SCAN,  
8e2b0 20 2f 2a 20 62 55 73 65 43 69 73 20 2a 2f 0a 20   /* bUseCis */. 
8e2c0 20 20 30 78 37 66 66 66 66 66 66 65 2c 20 20 20    0x7ffffffe,   
8e2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
8e2e0 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a 20 20 20 31  mxStrlen */.   1
8e2f0 32 38 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  28,             
8e300 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 4c            /* szL
8e310 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 35  ookaside */.   5
8e320 30 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  00,             
8e330 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 4c 6f            /* nLo
8e340 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 7b 30  okaside */.   {0
8e350 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c  ,0,0,0,0,0,0,0},
8e360 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 20 2a 2f           /* m */
8e370 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30  .   {0,0,0,0,0,0
8e380 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 20 20 20 2f  ,0,0,0},       /
8e390 2a 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 7b 30  * mutex */.   {0
8e3a0 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30  ,0,0,0,0,0,0,0,0
8e3b0 2c 30 2c 30 2c 30 2c 30 7d 2c 2f 2a 20 70 63 61  ,0,0,0,0},/* pca
8e3c0 63 68 65 32 20 2a 2f 0a 20 20 20 28 76 6f 69 64  che2 */.   (void
8e3d0 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  *)0,            
8e3e0 20 20 20 20 20 20 2f 2a 20 70 48 65 61 70 20 2a        /* pHeap *
8e3f0 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  /.   0,         
8e400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e410 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20 20 20 30  /* nHeap */.   0
8e420 2c 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  , 0,            
8e430 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 6e 48            /* mnH
8e440 65 61 70 2c 20 6d 78 48 65 61 70 20 2a 2f 0a 20  eap, mxHeap */. 
8e450 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 20 20    (void*)0,     
8e460 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
8e470 70 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30  pScratch */.   0
8e480 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
8e490 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 53            /* szS
8e4a0 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30 2c 20  cratch */.   0, 
8e4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e4c0 20 20 20 20 20 20 20 20 2f 2a 20 6e 53 63 72 61          /* nScra
8e4d0 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a  tch */.   (void*
8e4e0 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  )0,             
8e4f0 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 2a 2f       /* pPage */
8e500 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  .   0,          
8e510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
8e520 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20 20 20 30  * szPage */.   0
8e530 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
8e540 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 50 61            /* nPa
8e550 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20  ge */.   0,     
8e560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e570 20 20 20 20 2f 2a 20 6d 78 50 61 72 73 65 72 53      /* mxParserS
8e580 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c 20 20 20  tack */.   0,   
8e590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e5a0 20 20 20 20 20 20 2f 2a 20 73 68 61 72 65 64 43        /* sharedC
8e5b0 61 63 68 65 45 6e 61 62 6c 65 64 20 2a 2f 0a 20  acheEnabled */. 
8e5c0 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65 73    /* All the res
8e5d0 74 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20  t should always 
8e5e0 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74  be initialized t
8e5f0 6f 20 7a 65 72 6f 20 2a 2f 0a 20 20 20 30 2c 20  o zero */.   0, 
8e600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e610 20 20 20 20 20 20 20 20 2f 2a 20 69 73 49 6e 69          /* isIni
8e620 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20  t */.   0,      
8e630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e640 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72 65 73 73     /* inProgress
8e650 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20   */.   0,       
8e660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e670 20 20 2f 2a 20 69 73 4d 75 74 65 78 49 6e 69 74    /* isMutexInit
8e680 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20   */.   0,       
8e690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e6a0 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63 49 6e 69    /* isMallocIni
8e6b0 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20  t */.   0,      
8e6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e6d0 20 20 20 2f 2a 20 69 73 50 43 61 63 68 65 49 6e     /* isPCacheIn
8e6e0 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20  it */.   0,     
8e6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e700 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d 75 74 65      /* pInitMute
8e710 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20  x */.   0,      
8e720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e730 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69 74 4d 75     /* nRefInitMu
8e740 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  tex */.   0,    
8e750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e760 20 20 20 20 20 2f 2a 20 78 4c 6f 67 20 2a 2f 0a       /* xLog */.
8e770 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
8e780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
8e790 20 70 4c 6f 67 41 72 67 20 2a 2f 0a 20 20 20 30   pLogArg */.   0
8e7a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
8e7b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 62 4c 6f            /* bLo
8e7c0 63 61 6c 74 69 6d 65 46 61 75 6c 74 20 2a 2f 0a  caltimeFault */.
8e7d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
8e7e0 41 42 4c 45 5f 53 51 4c 4c 4f 47 0a 20 20 20 30  ABLE_SQLLOG.   0
8e7f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
8e800 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 71            /* xSq
8e810 6c 6c 6f 67 20 2a 2f 0a 20 20 20 30 20 20 20 20  llog */.   0    
8e820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8e830 20 20 20 20 20 20 2f 2a 20 70 53 71 6c 6c 6f 67        /* pSqllog
8e840 41 72 67 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b  Arg */.#endif.};
8e850 0a 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 74 61  .../*.** Hash ta
8e860 62 6c 65 20 66 6f 72 20 67 6c 6f 62 61 6c 20 66  ble for global f
8e870 75 6e 63 74 69 6f 6e 73 20 2d 20 66 75 6e 63 74  unctions - funct
8e880 69 6f 6e 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61  ions common to a
8e890 6c 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  ll.** database c
8e8a0 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 41 66 74  onnections.  Aft
8e8b0 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  er initializatio
8e8c0 6e 2c 20 74 68 69 73 20 74 61 62 6c 65 20 69 73  n, this table is
8e8d0 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 2a  .** read-only..*
8e8e0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
8e8f0 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63   SQLITE_WSD Func
8e900 44 65 66 48 61 73 68 20 73 71 6c 69 74 65 33 47  DefHash sqlite3G
8e910 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a  lobalFunctions;.
8e920 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 20  ./*.** Constant 
8e930 74 6f 6b 65 6e 73 20 66 6f 72 20 76 61 6c 75 65  tokens for value
8e940 73 20 30 20 61 6e 64 20 31 2e 0a 2a 2f 0a 53 51  s 0 and 1..*/.SQ
8e950 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
8e960 73 74 20 54 6f 6b 65 6e 20 73 71 6c 69 74 65 33  st Token sqlite3
8e970 49 6e 74 54 6f 6b 65 6e 73 5b 5d 20 3d 20 7b 0a  IntTokens[] = {.
8e980 20 20 20 7b 20 22 30 22 2c 20 31 20 7d 2c 0a 20     { "0", 1 },. 
8e990 20 20 7b 20 22 31 22 2c 20 31 20 7d 0a 7d 3b 0a    { "1", 1 }.};.
8e9a0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75  ../*.** The valu
8e9b0 65 20 6f 66 20 74 68 65 20 22 70 65 6e 64 69 6e  e of the "pendin
8e9c0 67 22 20 62 79 74 65 20 6d 75 73 74 20 62 65 20  g" byte must be 
8e9d0 30 78 34 30 30 30 30 30 30 30 20 28 31 20 62 79  0x40000000 (1 by
8e9e0 74 65 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31  te past the.** 1
8e9f0 2d 67 69 62 61 62 79 74 65 20 62 6f 75 6e 64 61  -gibabyte bounda
8ea00 72 79 29 20 69 6e 20 61 20 63 6f 6d 70 61 74 69  ry) in a compati
8ea10 62 6c 65 20 64 61 74 61 62 61 73 65 2e 20 20 53  ble database.  S
8ea20 51 4c 69 74 65 20 6e 65 76 65 72 20 75 73 65 73  QLite never uses
8ea30 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
8ea40 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61   page that conta
8ea50 69 6e 73 20 74 68 65 20 70 65 6e 64 69 6e 67 20  ins the pending 
8ea60 62 79 74 65 2e 20 20 49 74 20 6e 65 76 65 72 20  byte.  It never 
8ea70 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 72  attempts.** to r
8ea80 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 68 61  ead or write tha
8ea90 74 20 70 61 67 65 2e 20 20 54 68 65 20 70 65 6e  t page.  The pen
8eaa0 64 69 6e 67 20 62 79 74 65 20 70 61 67 65 20 69  ding byte page i
8eab0 73 20 73 65 74 20 61 73 73 69 67 6e 0a 2a 2a 20  s set assign.** 
8eac0 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 56  for use by the V
8ead0 46 53 20 6c 61 79 65 72 73 20 61 73 20 73 70 61  FS layers as spa
8eae0 63 65 20 66 6f 72 20 6d 61 6e 61 67 69 6e 67 20  ce for managing 
8eaf0 66 69 6c 65 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a  file locks..**.*
8eb00 2a 20 44 75 72 69 6e 67 20 74 65 73 74 69 6e 67  * During testing
8eb10 2c 20 69 74 20 69 73 20 6f 66 74 65 6e 20 64 65  , it is often de
8eb20 73 69 72 61 62 6c 65 20 74 6f 20 6d 6f 76 65 20  sirable to move 
8eb30 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65  the pending byte
8eb40 20 74 6f 0a 2a 2a 20 61 20 64 69 66 66 65 72 65   to.** a differe
8eb50 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74  nt position in t
8eb60 68 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 61  he file.  This a
8eb70 6c 6c 6f 77 73 20 63 6f 64 65 20 74 68 61 74 20  llows code that 
8eb80 68 61 73 20 74 6f 0a 2a 2a 20 64 65 61 6c 20 77  has to.** deal w
8eb90 69 74 68 20 74 68 65 20 70 65 6e 64 69 6e 67 20  ith the pending 
8eba0 62 79 74 65 20 74 6f 20 72 75 6e 20 6f 6e 20 66  byte to run on f
8ebb0 69 6c 65 73 20 74 68 61 74 20 61 72 65 20 6d 75  iles that are mu
8ebc0 63 68 20 73 6d 61 6c 6c 65 72 0a 2a 2a 20 74 68  ch smaller.** th
8ebd0 61 6e 20 31 20 47 69 42 2e 20 20 54 68 65 20 73  an 1 GiB.  The s
8ebe0 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
8ebf0 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20  rol() interface 
8ec00 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 0a 2a  can be used to.*
8ec10 2a 20 6d 6f 76 65 20 74 68 65 20 70 65 6e 64 69  * move the pendi
8ec20 6e 67 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 49  ng byte..**.** I
8ec30 4d 50 4f 52 54 41 4e 54 3a 20 20 43 68 61 6e 67  MPORTANT:  Chang
8ec40 69 6e 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20  ing the pending 
8ec50 62 79 74 65 20 74 6f 20 61 6e 79 20 76 61 6c 75  byte to any valu
8ec60 65 20 6f 74 68 65 72 20 74 68 61 6e 0a 2a 2a 20  e other than.** 
8ec70 30 78 34 30 30 30 30 30 30 30 20 72 65 73 75 6c  0x40000000 resul
8ec80 74 73 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61  ts in an incompa
8ec90 74 69 62 6c 65 20 64 61 74 61 62 61 73 65 20 66  tible database f
8eca0 69 6c 65 20 66 6f 72 6d 61 74 21 0a 2a 2a 20 43  ile format!.** C
8ecb0 68 61 6e 67 69 6e 67 20 74 68 65 20 70 65 6e 64  hanging the pend
8ecc0 69 6e 67 20 62 79 74 65 20 64 75 72 69 6e 67 20  ing byte during 
8ecd0 6f 70 65 72 61 74 69 6e 67 20 72 65 73 75 6c 74  operating result
8ece0 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a  s in undefined.*
8ecf0 2a 20 61 6e 64 20 64 69 6c 65 74 65 72 69 6f 75  * and dileteriou
8ed00 73 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 23  s behavior..*/.#
8ed10 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
8ed20 49 54 5f 57 53 44 0a 53 51 4c 49 54 45 5f 50 52  IT_WSD.SQLITE_PR
8ed30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
8ed40 33 50 65 6e 64 69 6e 67 42 79 74 65 20 3d 20 30  3PendingByte = 0
8ed50 78 34 30 30 30 30 30 30 30 3b 0a 23 65 6e 64 69  x40000000;.#endi
8ed60 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 70 65 72 74  f../*.** Propert
8ed70 69 65 73 20 6f 66 20 6f 70 63 6f 64 65 73 2e 20  ies of opcodes. 
8ed80 20 54 68 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49   The OPFLG_INITI
8ed90 41 4c 49 5a 45 52 20 6d 61 63 72 6f 20 69 73 0a  ALIZER macro is.
8eda0 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 6d 6b  ** created by mk
8edb0 6f 70 63 6f 64 65 68 2e 61 77 6b 20 64 75 72 69  opcodeh.awk duri
8edc0 6e 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e 20  ng compilation. 
8edd0 20 44 61 74 61 20 69 73 20 6f 62 74 61 69 6e 65   Data is obtaine
8ede0 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 63 6f  d.** from the co
8edf0 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e 67  mments following
8ee00 20 74 68 65 20 22 63 61 73 65 20 4f 50 5f 78 78   the "case OP_xx
8ee10 78 78 3a 22 20 73 74 61 74 65 6d 65 6e 74 73 20  xx:" statements 
8ee20 69 6e 0a 2a 2a 20 74 68 65 20 76 64 62 65 2e 63  in.** the vdbe.c
8ee30 20 66 69 6c 65 2e 20 20 0a 2a 2f 0a 53 51 4c 49   file.  .*/.SQLI
8ee40 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
8ee50 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73   unsigned char s
8ee60 71 6c 69 74 65 33 4f 70 63 6f 64 65 50 72 6f 70  qlite3OpcodeProp
8ee70 65 72 74 79 5b 5d 20 3d 20 4f 50 46 4c 47 5f 49  erty[] = OPFLG_I
8ee80 4e 49 54 49 41 4c 49 5a 45 52 3b 0a 0a 2f 2a 2a  NITIALIZER;../**
8ee90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
8eea0 20 6f 66 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a   of global.c ***
8eeb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8eec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8eed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
8eee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
8eef0 69 6e 20 66 69 6c 65 20 63 74 69 6d 65 2e 63 20  in file ctime.c 
8ef00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ef10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ef20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
8ef30 2a 2a 20 32 30 31 30 20 46 65 62 72 75 61 72 79  ** 2010 February
8ef40 20 32 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   23.**.** The au
8ef50 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
8ef60 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
8ef70 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
8ef80 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
8ef90 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
8efa0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
8efb0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
8efc0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
8efd0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
8efe0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
8eff0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
8f000 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
8f010 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
8f020 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
8f030 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
8f040 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
8f050 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
8f060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8f070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8f080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8f090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8f0a0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
8f0b0 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 72 6f  le implements ro
8f0c0 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 72  utines used to r
8f0d0 65 70 6f 72 74 20 77 68 61 74 20 63 6f 6d 70 69  eport what compi
8f0e0 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 73 0a  le-time options.
8f0f0 2a 2a 20 53 51 4c 69 74 65 20 77 61 73 20 62 75  ** SQLite was bu
8f100 69 6c 74 20 77 69 74 68 2e 0a 2a 2f 0a 0a 23 69  ilt with..*/..#i
8f110 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
8f120 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f  T_COMPILEOPTION_
8f130 44 49 41 47 53 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e  DIAGS.../*.** An
8f140 20 61 72 72 61 79 20 6f 66 20 6e 61 6d 65 73 20   array of names 
8f150 6f 66 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 2d 74  of all compile-t
8f160 69 6d 65 20 6f 70 74 69 6f 6e 73 2e 20 20 54 68  ime options.  Th
8f170 69 73 20 61 72 72 61 79 20 73 68 6f 75 6c 64 20  is array should 
8f180 0a 2a 2a 20 62 65 20 73 6f 72 74 65 64 20 41 2d  .** be sorted A-
8f190 5a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 72  Z..**.** This ar
8f1a0 72 61 79 20 6c 6f 6f 6b 73 20 6c 61 72 67 65 2c  ray looks large,
8f1b0 20 62 75 74 20 69 6e 20 61 20 74 79 70 69 63 61   but in a typica
8f1c0 6c 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e 20 61  l installation a
8f1d0 63 74 75 61 6c 6c 79 20 75 73 65 73 0a 2a 2a 20  ctually uses.** 
8f1e0 6f 6e 6c 79 20 61 20 68 61 6e 64 66 75 6c 20 6f  only a handful o
8f1f0 66 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f  f compile-time o
8f200 70 74 69 6f 6e 73 2c 20 73 6f 20 6d 6f 73 74 20  ptions, so most 
8f210 74 69 6d 65 73 20 74 68 69 73 20 61 72 72 61 79  times this array
8f220 20 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72   is usually.** r
8f230 61 74 68 65 72 20 73 68 6f 72 74 20 61 6e 64 20  ather short and 
8f240 75 73 65 73 20 6c 69 74 74 6c 65 20 6d 65 6d 6f  uses little memo
8f250 72 79 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61  ry space..*/.sta
8f260 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
8f270 20 63 6f 6e 73 74 20 61 7a 43 6f 6d 70 69 6c 65   const azCompile
8f280 4f 70 74 5b 5d 20 3d 20 7b 0a 0a 2f 2a 20 54 68  Opt[] = {../* Th
8f290 65 73 65 20 6d 61 63 72 6f 73 20 61 72 65 20 70  ese macros are p
8f2a0 72 6f 76 69 64 65 64 20 74 6f 20 22 73 74 72 69  rovided to "stri
8f2b0 6e 67 69 66 79 22 20 74 68 65 20 76 61 6c 75 65  ngify" the value
8f2c0 20 6f 66 20 74 68 65 20 64 65 66 69 6e 65 0a 2a   of the define.*
8f2d0 2a 20 66 6f 72 20 74 68 6f 73 65 20 6f 70 74 69  * for those opti
8f2e0 6f 6e 73 20 69 6e 20 77 68 69 63 68 20 74 68 65  ons in which the
8f2f0 20 76 61 6c 75 65 20 69 73 20 6d 65 61 6e 69 6e   value is meanin
8f300 67 66 75 6c 2e 20 2a 2f 0a 23 64 65 66 69 6e 65  gful. */.#define
8f310 20 43 54 49 4d 45 4f 50 54 5f 56 41 4c 5f 28 6f   CTIMEOPT_VAL_(o
8f320 70 74 29 20 23 6f 70 74 0a 23 64 65 66 69 6e 65  pt) #opt.#define
8f330 20 43 54 49 4d 45 4f 50 54 5f 56 41 4c 28 6f 70   CTIMEOPT_VAL(op
8f340 74 29 20 43 54 49 4d 45 4f 50 54 5f 56 41 4c 5f  t) CTIMEOPT_VAL_
8f350 28 6f 70 74 29 0a 0a 23 69 66 64 65 66 20 53 51  (opt)..#ifdef SQ
8f360 4c 49 54 45 5f 33 32 42 49 54 5f 52 4f 57 49 44  LITE_32BIT_ROWID
8f370 0a 20 20 22 33 32 42 49 54 5f 52 4f 57 49 44 22  .  "32BIT_ROWID"
8f380 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ,.#endif.#ifdef 
8f390 53 51 4c 49 54 45 5f 34 5f 42 59 54 45 5f 41 4c  SQLITE_4_BYTE_AL
8f3a0 49 47 4e 45 44 5f 4d 41 4c 4c 4f 43 0a 20 20 22  IGNED_MALLOC.  "
8f3b0 34 5f 42 59 54 45 5f 41 4c 49 47 4e 45 44 5f 4d  4_BYTE_ALIGNED_M
8f3c0 41 4c 4c 4f 43 22 2c 0a 23 65 6e 64 69 66 0a 23  ALLOC",.#endif.#
8f3d0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 41 53  ifdef SQLITE_CAS
8f3e0 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45  E_SENSITIVE_LIKE
8f3f0 0a 20 20 22 43 41 53 45 5f 53 45 4e 53 49 54 49  .  "CASE_SENSITI
8f400 56 45 5f 4c 49 4b 45 22 2c 0a 23 65 6e 64 69 66  VE_LIKE",.#endif
8f410 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
8f420 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 22 43 48  HECK_PAGES.  "CH
8f430 45 43 4b 5f 50 41 47 45 53 22 2c 0a 23 65 6e 64  ECK_PAGES",.#end
8f440 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
8f450 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 0a 20  _COVERAGE_TEST. 
8f460 20 22 43 4f 56 45 52 41 47 45 5f 54 45 53 54 22   "COVERAGE_TEST"
8f470 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ,.#endif.#ifdef 
8f480 53 51 4c 49 54 45 5f 43 55 52 44 49 52 0a 20 20  SQLITE_CURDIR.  
8f490 22 43 55 52 44 49 52 22 2c 0a 23 65 6e 64 69 66  "CURDIR",.#endif
8f4a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
8f4b0 45 42 55 47 0a 20 20 22 44 45 42 55 47 22 2c 0a  EBUG.  "DEBUG",.
8f4c0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
8f4d0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43  LITE_DEFAULT_LOC
8f4e0 4b 49 4e 47 5f 4d 4f 44 45 0a 20 20 22 44 45 46  KING_MODE.  "DEF
8f4f0 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44  AULT_LOCKING_MOD
8f500 45 3d 22 20 43 54 49 4d 45 4f 50 54 5f 56 41 4c  E=" CTIMEOPT_VAL
8f510 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f  (SQLITE_DEFAULT_
8f520 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 29 2c 0a 23  LOCKING_MODE),.#
8f530 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
8f540 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 53  ITE_DISABLE_DIRS
8f550 59 4e 43 0a 20 20 22 44 49 53 41 42 4c 45 5f 44  YNC.  "DISABLE_D
8f560 49 52 53 59 4e 43 22 2c 0a 23 65 6e 64 69 66 0a  IRSYNC",.#endif.
8f570 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49  #ifdef SQLITE_DI
8f580 53 41 42 4c 45 5f 4c 46 53 0a 20 20 22 44 49 53  SABLE_LFS.  "DIS
8f590 41 42 4c 45 5f 4c 46 53 22 2c 0a 23 65 6e 64 69  ABLE_LFS",.#endi
8f5a0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
8f5b0 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52  ENABLE_ATOMIC_WR
8f5c0 49 54 45 0a 20 20 22 45 4e 41 42 4c 45 5f 41 54  ITE.  "ENABLE_AT
8f5d0 4f 4d 49 43 5f 57 52 49 54 45 22 2c 0a 23 65 6e  OMIC_WRITE",.#en
8f5e0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
8f5f0 45 5f 45 4e 41 42 4c 45 5f 43 45 52 4f 44 0a 20  E_ENABLE_CEROD. 
8f600 20 22 45 4e 41 42 4c 45 5f 43 45 52 4f 44 22 2c   "ENABLE_CEROD",
8f610 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
8f620 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
8f630 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 20 20 22  UMN_METADATA.  "
8f640 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
8f650 54 41 44 41 54 41 22 2c 0a 23 65 6e 64 69 66 0a  TADATA",.#endif.
8f660 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
8f670 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41  ABLE_EXPENSIVE_A
8f680 53 53 45 52 54 0a 20 20 22 45 4e 41 42 4c 45 5f  SSERT.  "ENABLE_
8f690 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54  EXPENSIVE_ASSERT
8f6a0 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  ",.#endif.#ifdef
8f6b0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46   SQLITE_ENABLE_F
8f6c0 54 53 31 0a 20 20 22 45 4e 41 42 4c 45 5f 46 54  TS1.  "ENABLE_FT
8f6d0 53 31 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64  S1",.#endif.#ifd
8f6e0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
8f6f0 5f 46 54 53 32 0a 20 20 22 45 4e 41 42 4c 45 5f  _FTS2.  "ENABLE_
8f700 46 54 53 32 22 2c 0a 23 65 6e 64 69 66 0a 23 69  FTS2",.#endif.#i
8f710 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
8f720 4c 45 5f 46 54 53 33 0a 20 20 22 45 4e 41 42 4c  LE_FTS3.  "ENABL
8f730 45 5f 46 54 53 33 22 2c 0a 23 65 6e 64 69 66 0a  E_FTS3",.#endif.
8f740 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
8f750 41 42 4c 45 5f 46 54 53 33 5f 50 41 52 45 4e 54  ABLE_FTS3_PARENT
8f760 48 45 53 49 53 0a 20 20 22 45 4e 41 42 4c 45 5f  HESIS.  "ENABLE_
8f770 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53 49 53  FTS3_PARENTHESIS
8f780 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  ",.#endif.#ifdef
8f790 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46   SQLITE_ENABLE_F
8f7a0 54 53 34 0a 20 20 22 45 4e 41 42 4c 45 5f 46 54  TS4.  "ENABLE_FT
8f7b0 53 34 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64  S4",.#endif.#ifd
8f7c0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
8f7d0 5f 49 43 55 0a 20 20 22 45 4e 41 42 4c 45 5f 49  _ICU.  "ENABLE_I
8f7e0 43 55 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64  CU",.#endif.#ifd
8f7f0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
8f800 5f 49 4f 54 52 41 43 45 0a 20 20 22 45 4e 41 42  _IOTRACE.  "ENAB
8f810 4c 45 5f 49 4f 54 52 41 43 45 22 2c 0a 23 65 6e  LE_IOTRACE",.#en
8f820 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
8f830 45 5f 45 4e 41 42 4c 45 5f 4c 4f 41 44 5f 45 58  E_ENABLE_LOAD_EX
8f840 54 45 4e 53 49 4f 4e 0a 20 20 22 45 4e 41 42 4c  TENSION.  "ENABL
8f850 45 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  E_LOAD_EXTENSION
8f860 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  ",.#endif.#ifdef
8f870 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
8f880 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 22  OCKING_STYLE.  "
8f890 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
8f8a0 54 59 4c 45 3d 22 20 43 54 49 4d 45 4f 50 54 5f  TYLE=" CTIMEOPT_
8f8b0 56 41 4c 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  VAL(SQLITE_ENABL
8f8c0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29  E_LOCKING_STYLE)
8f8d0 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ,.#endif.#ifdef 
8f8e0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
8f8f0 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a  MORY_MANAGEMENT.
8f900 20 20 22 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59    "ENABLE_MEMORY
8f910 5f 4d 41 4e 41 47 45 4d 45 4e 54 22 2c 0a 23 65  _MANAGEMENT",.#e
8f920 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
8f930 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
8f940 33 0a 20 20 22 45 4e 41 42 4c 45 5f 4d 45 4d 53  3.  "ENABLE_MEMS
8f950 59 53 33 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66  YS3",.#endif.#if
8f960 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
8f970 45 5f 4d 45 4d 53 59 53 35 0a 20 20 22 45 4e 41  E_MEMSYS5.  "ENA
8f980 42 4c 45 5f 4d 45 4d 53 59 53 35 22 2c 0a 23 65  BLE_MEMSYS5",.#e
8f990 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
8f9a0 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49  TE_ENABLE_OVERSI
8f9b0 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 0a 20 20  ZE_CELL_CHECK.  
8f9c0 22 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45  "ENABLE_OVERSIZE
8f9d0 5f 43 45 4c 4c 5f 43 48 45 43 4b 22 2c 0a 23 65  _CELL_CHECK",.#e
8f9e0 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
8f9f0 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45 0a  TE_ENABLE_RTREE.
8fa00 20 20 22 45 4e 41 42 4c 45 5f 52 54 52 45 45 22    "ENABLE_RTREE"
8fa10 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ,.#endif.#ifdef 
8fa20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
8fa30 41 54 33 0a 20 20 22 45 4e 41 42 4c 45 5f 53 54  AT3.  "ENABLE_ST
8fa40 41 54 33 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66  AT3",.#endif.#if
8fa50 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
8fa60 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a  E_UNLOCK_NOTIFY.
8fa70 20 20 22 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b    "ENABLE_UNLOCK
8fa80 5f 4e 4f 54 49 46 59 22 2c 0a 23 65 6e 64 69 66  _NOTIFY",.#endif
8fa90 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
8faa0 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c  NABLE_UPDATE_DEL
8fab0 45 54 45 5f 4c 49 4d 49 54 0a 20 20 22 45 4e 41  ETE_LIMIT.  "ENA
8fac0 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54  BLE_UPDATE_DELET
8fad0 45 5f 4c 49 4d 49 54 22 2c 0a 23 65 6e 64 69 66  E_LIMIT",.#endif
8fae0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48  .#ifdef SQLITE_H
8faf0 41 53 5f 43 4f 44 45 43 0a 20 20 22 48 41 53 5f  AS_CODEC.  "HAS_
8fb00 43 4f 44 45 43 22 2c 0a 23 65 6e 64 69 66 0a 23  CODEC",.#endif.#
8fb10 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 56  ifdef SQLITE_HAV
8fb20 45 5f 49 53 4e 41 4e 0a 20 20 22 48 41 56 45 5f  E_ISNAN.  "HAVE_
8fb30 49 53 4e 41 4e 22 2c 0a 23 65 6e 64 69 66 0a 23  ISNAN",.#endif.#
8fb40 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d  ifdef SQLITE_HOM
8fb50 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45  EGROWN_RECURSIVE
8fb60 5f 4d 55 54 45 58 0a 20 20 22 48 4f 4d 45 47 52  _MUTEX.  "HOMEGR
8fb70 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55  OWN_RECURSIVE_MU
8fb80 54 45 58 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66  TEX",.#endif.#if
8fb90 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52  def SQLITE_IGNOR
8fba0 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52  E_AFP_LOCK_ERROR
8fbb0 53 0a 20 20 22 49 47 4e 4f 52 45 5f 41 46 50 5f  S.  "IGNORE_AFP_
8fbc0 4c 4f 43 4b 5f 45 52 52 4f 52 53 22 2c 0a 23 65  LOCK_ERRORS",.#e
8fbd0 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
8fbe0 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f  TE_IGNORE_FLOCK_
8fbf0 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 22 49  LOCK_ERRORS.  "I
8fc00 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b  GNORE_FLOCK_LOCK
8fc10 5f 45 52 52 4f 52 53 22 2c 0a 23 65 6e 64 69 66  _ERRORS",.#endif
8fc20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49  .#ifdef SQLITE_I
8fc30 4e 54 36 34 5f 54 59 50 45 0a 20 20 22 49 4e 54  NT64_TYPE.  "INT
8fc40 36 34 5f 54 59 50 45 22 2c 0a 23 65 6e 64 69 66  64_TYPE",.#endif
8fc50 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4c  .#ifdef SQLITE_L
8fc60 4f 43 4b 5f 54 52 41 43 45 0a 20 20 22 4c 4f 43  OCK_TRACE.  "LOC
8fc70 4b 5f 54 52 41 43 45 22 2c 0a 23 65 6e 64 69 66  K_TRACE",.#endif
8fc80 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d  .#ifdef SQLITE_M
8fc90 41 58 5f 53 43 48 45 4d 41 5f 52 45 54 52 59 0a  AX_SCHEMA_RETRY.
8fca0 20 20 22 4d 41 58 5f 53 43 48 45 4d 41 5f 52 45    "MAX_SCHEMA_RE
8fcb0 54 52 59 3d 22 20 43 54 49 4d 45 4f 50 54 5f 56  TRY=" CTIMEOPT_V
8fcc0 41 4c 28 53 51 4c 49 54 45 5f 4d 41 58 5f 53 43  AL(SQLITE_MAX_SC
8fcd0 48 45 4d 41 5f 52 45 54 52 59 29 2c 0a 23 65 6e  HEMA_RETRY),.#en
8fce0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
8fcf0 45 5f 4d 45 4d 44 45 42 55 47 0a 20 20 22 4d 45  E_MEMDEBUG.  "ME
8fd00 4d 44 45 42 55 47 22 2c 0a 23 65 6e 64 69 66 0a  MDEBUG",.#endif.
8fd10 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 49  #ifdef SQLITE_MI
8fd20 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54  XED_ENDIAN_64BIT
8fd30 5f 46 4c 4f 41 54 0a 20 20 22 4d 49 58 45 44 5f  _FLOAT.  "MIXED_
8fd40 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f  ENDIAN_64BIT_FLO
8fd50 41 54 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64  AT",.#endif.#ifd
8fd60 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e  ef SQLITE_NO_SYN
8fd70 43 0a 20 20 22 4e 4f 5f 53 59 4e 43 22 2c 0a 23  C.  "NO_SYNC",.#
8fd80 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
8fd90 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41  ITE_OMIT_ALTERTA
8fda0 42 4c 45 0a 20 20 22 4f 4d 49 54 5f 41 4c 54 45  BLE.  "OMIT_ALTE
8fdb0 52 54 41 42 4c 45 22 2c 0a 23 65 6e 64 69 66 0a  RTABLE",.#endif.
8fdc0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
8fdd0 49 54 5f 41 4e 41 4c 59 5a 45 0a 20 20 22 4f 4d  IT_ANALYZE.  "OM
8fde0 49 54 5f 41 4e 41 4c 59 5a 45 22 2c 0a 23 65 6e  IT_ANALYZE",.#en
8fdf0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
8fe00 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 0a 20 20  E_OMIT_ATTACH.  
8fe10 22 4f 4d 49 54 5f 41 54 54 41 43 48 22 2c 0a 23  "OMIT_ATTACH",.#
8fe20 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
8fe30 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
8fe40 5a 41 54 49 4f 4e 0a 20 20 22 4f 4d 49 54 5f 41  ZATION.  "OMIT_A
8fe50 55 54 48 4f 52 49 5a 41 54 49 4f 4e 22 2c 0a 23  UTHORIZATION",.#
8fe60 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
8fe70 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
8fe80 52 45 4d 45 4e 54 0a 20 20 22 4f 4d 49 54 5f 41  REMENT.  "OMIT_A
8fe90 55 54 4f 49 4e 43 52 45 4d 45 4e 54 22 2c 0a 23  UTOINCREMENT",.#
8fea0 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
8feb0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
8fec0 54 0a 20 20 22 4f 4d 49 54 5f 41 55 54 4f 49 4e  T.  "OMIT_AUTOIN
8fed0 49 54 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64  IT",.#endif.#ifd
8fee0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
8fef0 55 54 4f 4d 41 54 49 43 5f 49 4e 44 45 58 0a 20  UTOMATIC_INDEX. 
8ff00 20 22 4f 4d 49 54 5f 41 55 54 4f 4d 41 54 49 43   "OMIT_AUTOMATIC
8ff10 5f 49 4e 44 45 58 22 2c 0a 23 65 6e 64 69 66 0a  _INDEX",.#endif.
8ff20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
8ff30 49 54 5f 41 55 54 4f 52 45 53 45 54 0a 20 20 22  IT_AUTORESET.  "
8ff40 4f 4d 49 54 5f 41 55 54 4f 52 45 53 45 54 22 2c  OMIT_AUTORESET",
8ff50 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
8ff60 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
8ff70 41 43 55 55 4d 0a 20 20 22 4f 4d 49 54 5f 41 55  ACUUM.  "OMIT_AU
8ff80 54 4f 56 41 43 55 55 4d 22 2c 0a 23 65 6e 64 69  TOVACUUM",.#endi
8ff90 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
8ffa0 4f 4d 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54  OMIT_BETWEEN_OPT
8ffb0 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 22 4f 4d 49  IMIZATION.  "OMI
8ffc0 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49 4d 49  T_BETWEEN_OPTIMI
8ffd0 5a 41 54 49 4f 4e 22 2c 0a 23 65 6e 64 69 66 0a  ZATION",.#endif.
8ffe0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
8fff0 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a  IT_BLOB_LITERAL.
90000 20 20 22 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54    "OMIT_BLOB_LIT
90010 45 52 41 4c 22 2c 0a 23 65 6e 64 69 66 0a 23 69  ERAL",.#endif.#i
90020 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
90030 5f 42 54 52 45 45 43 4f 55 4e 54 0a 20 20 22 4f  _BTREECOUNT.  "O
90040 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 22 2c  MIT_BTREECOUNT",
90050 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
90060 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54  QLITE_OMIT_BUILT
90070 49 4e 5f 54 45 53 54 0a 20 20 22 4f 4d 49 54 5f  IN_TEST.  "OMIT_
90080 42 55 49 4c 54 49 4e 5f 54 45 53 54 22 2c 0a 23  BUILTIN_TEST",.#
90090 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
900a0 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20  ITE_OMIT_CAST.  
900b0 22 4f 4d 49 54 5f 43 41 53 54 22 2c 0a 23 65 6e  "OMIT_CAST",.#en
900c0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
900d0 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 22  E_OMIT_CHECK.  "
900e0 4f 4d 49 54 5f 43 48 45 43 4b 22 2c 0a 23 65 6e  OMIT_CHECK",.#en
900f0 64 69 66 0a 2f 2a 20 2f 2f 20 72 65 64 75 6e 64  dif./* // redund
90100 61 6e 74 0a 2a 2a 20 23 69 66 64 65 66 20 53 51  ant.** #ifdef SQ
90110 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c  LITE_OMIT_COMPIL
90120 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53 0a 2a 2a  EOPTION_DIAGS.**
90130 20 20 20 22 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45     "OMIT_COMPILE
90140 4f 50 54 49 4f 4e 5f 44 49 41 47 53 22 2c 0a 2a  OPTION_DIAGS",.*
90150 2a 20 23 65 6e 64 69 66 0a 2a 2f 0a 23 69 66 64  * #endif.*/.#ifd
90160 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
90170 4f 4d 50 4c 45 54 45 0a 20 20 22 4f 4d 49 54 5f  OMPLETE.  "OMIT_
90180 43 4f 4d 50 4c 45 54 45 22 2c 0a 23 65 6e 64 69  COMPLETE",.#endi
90190 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
901a0 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  OMIT_COMPOUND_SE
901b0 4c 45 43 54 0a 20 20 22 4f 4d 49 54 5f 43 4f 4d  LECT.  "OMIT_COM
901c0 50 4f 55 4e 44 5f 53 45 4c 45 43 54 22 2c 0a 23  POUND_SELECT",.#
901d0 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
901e0 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d  ITE_OMIT_DATETIM
901f0 45 5f 46 55 4e 43 53 0a 20 20 22 4f 4d 49 54 5f  E_FUNCS.  "OMIT_
90200 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 22 2c  DATETIME_FUNCS",
90210 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
90220 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54  QLITE_OMIT_DECLT
90230 59 50 45 0a 20 20 22 4f 4d 49 54 5f 44 45 43 4c  YPE.  "OMIT_DECL
90240 54 59 50 45 22 2c 0a 23 65 6e 64 69 66 0a 23 69  TYPE",.#endif.#i
90250 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
90260 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20 22 4f  _DEPRECATED.  "O
90270 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 22 2c  MIT_DEPRECATED",
90280 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
90290 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
902a0 4f 0a 20 20 22 4f 4d 49 54 5f 44 49 53 4b 49 4f  O.  "OMIT_DISKIO
902b0 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  ",.#endif.#ifdef
902c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50   SQLITE_OMIT_EXP
902d0 4c 41 49 4e 0a 20 20 22 4f 4d 49 54 5f 45 58 50  LAIN.  "OMIT_EXP
902e0 4c 41 49 4e 22 2c 0a 23 65 6e 64 69 66 0a 23 69  LAIN",.#endif.#i
902f0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
90300 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 0a 20 20  _FLAG_PRAGMAS.  
90310 22 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d  "OMIT_FLAG_PRAGM
90320 41 53 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64  AS",.#endif.#ifd
90330 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
90340 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
90350 22 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  "OMIT_FLOATING_P
90360 4f 49 4e 54 22 2c 0a 23 65 6e 64 69 66 0a 23 69  OINT",.#endif.#i
90370 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
90380 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 22  _FOREIGN_KEY.  "
90390 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
903a0 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  ",.#endif.#ifdef
903b0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54   SQLITE_OMIT_GET
903c0 5f 54 41 42 4c 45 0a 20 20 22 4f 4d 49 54 5f 47  _TABLE.  "OMIT_G
903d0 45 54 5f 54 41 42 4c 45 22 2c 0a 23 65 6e 64 69  ET_TABLE",.#endi
903e0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
903f0 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20  OMIT_INCRBLOB.  
90400 22 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 22 2c  "OMIT_INCRBLOB",
90410 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
90420 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
90430 52 49 54 59 5f 43 48 45 43 4b 0a 20 20 22 4f 4d  RITY_CHECK.  "OM
90440 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
90450 43 4b 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64  CK",.#endif.#ifd
90460 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
90470 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  IKE_OPTIMIZATION
90480 0a 20 20 22 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50  .  "OMIT_LIKE_OP
90490 54 49 4d 49 5a 41 54 49 4f 4e 22 2c 0a 23 65 6e  TIMIZATION",.#en
904a0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
904b0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
904c0 4e 53 49 4f 4e 0a 20 20 22 4f 4d 49 54 5f 4c 4f  NSION.  "OMIT_LO
904d0 41 44 5f 45 58 54 45 4e 53 49 4f 4e 22 2c 0a 23  AD_EXTENSION",.#
904e0 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
904f0 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49  ITE_OMIT_LOCALTI
90500 4d 45 0a 20 20 22 4f 4d 49 54 5f 4c 4f 43 41 4c  ME.  "OMIT_LOCAL
90510 54 49 4d 45 22 2c 0a 23 65 6e 64 69 66 0a 23 69  TIME",.#endif.#i
90520 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
90530 5f 4c 4f 4f 4b 41 53 49 44 45 0a 20 20 22 4f 4d  _LOOKASIDE.  "OM
90540 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 22 2c 0a 23  IT_LOOKASIDE",.#
90550 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
90560 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44  ITE_OMIT_MEMORYD
90570 42 0a 20 20 22 4f 4d 49 54 5f 4d 45 4d 4f 52 59  B.  "OMIT_MEMORY
90580 44 42 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64  DB",.#endif.#ifd
90590 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d  ef SQLITE_OMIT_M
905a0 45 52 47 45 5f 53 4f 52 54 0a 20 20 22 4f 4d 49  ERGE_SORT.  "OMI
905b0 54 5f 4d 45 52 47 45 5f 53 4f 52 54 22 2c 0a 23  T_MERGE_SORT",.#
905c0 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
905d0 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49  ITE_OMIT_OR_OPTI
905e0 4d 49 5a 41 54 49 4f 4e 0a 20 20 22 4f 4d 49 54  MIZATION.  "OMIT
905f0 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  _OR_OPTIMIZATION
90600 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  ",.#endif.#ifdef
90610 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
90620 45 52 5f 50 52 41 47 4d 41 53 0a 20 20 22 4f 4d  ER_PRAGMAS.  "OM
90630 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
90640 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  ",.#endif.#ifdef
90650 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 41   SQLITE_OMIT_PRA
90660 47 4d 41 0a 20 20 22 4f 4d 49 54 5f 50 52 41 47  GMA.  "OMIT_PRAG
90670 4d 41 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64  MA",.#endif.#ifd
90680 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ef SQLITE_OMIT_P
90690 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b  ROGRESS_CALLBACK
906a0 0a 20 20 22 4f 4d 49 54 5f 50 52 4f 47 52 45 53  .  "OMIT_PROGRES
906b0 53 5f 43 41 4c 4c 42 41 43 4b 22 2c 0a 23 65 6e  S_CALLBACK",.#en
906c0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
906d0 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41  E_OMIT_QUICKBALA
906e0 4e 43 45 0a 20 20 22 4f 4d 49 54 5f 51 55 49 43  NCE.  "OMIT_QUIC
906f0 4b 42 41 4c 41 4e 43 45 22 2c 0a 23 65 6e 64 69  KBALANCE",.#endi
90700 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
90710 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 20 20 22  OMIT_REINDEX.  "
90720 4f 4d 49 54 5f 52 45 49 4e 44 45 58 22 2c 0a 23  OMIT_REINDEX",.#
90730 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
90740 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f  ITE_OMIT_SCHEMA_
90750 50 52 41 47 4d 41 53 0a 20 20 22 4f 4d 49 54 5f  PRAGMAS.  "OMIT_
90760 53 43 48 45 4d 41 5f 50 52 41 47 4d 41 53 22 2c  SCHEMA_PRAGMAS",
90770 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
90780 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d  QLITE_OMIT_SCHEM
90790 41 5f 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41  A_VERSION_PRAGMA
907a0 53 0a 20 20 22 4f 4d 49 54 5f 53 43 48 45 4d 41  S.  "OMIT_SCHEMA
907b0 5f 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53  _VERSION_PRAGMAS
907c0 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  ",.#endif.#ifdef
907d0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
907e0 52 45 44 5f 43 41 43 48 45 0a 20 20 22 4f 4d 49  RED_CACHE.  "OMI
907f0 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 22 2c  T_SHARED_CACHE",
90800 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
90810 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
90820 45 52 59 0a 20 20 22 4f 4d 49 54 5f 53 55 42 51  ERY.  "OMIT_SUBQ
90830 55 45 52 59 22 2c 0a 23 65 6e 64 69 66 0a 23 69  UERY",.#endif.#i
90840 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
90850 5f 54 43 4c 5f 56 41 52 49 41 42 4c 45 0a 20 20  _TCL_VARIABLE.  
90860 22 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49 41 42  "OMIT_TCL_VARIAB
90870 4c 45 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64  LE",.#endif.#ifd
90880 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
90890 45 4d 50 44 42 0a 20 20 22 4f 4d 49 54 5f 54 45  EMPDB.  "OMIT_TE
908a0 4d 50 44 42 22 2c 0a 23 65 6e 64 69 66 0a 23 69  MPDB",.#endif.#i
908b0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
908c0 5f 54 52 41 43 45 0a 20 20 22 4f 4d 49 54 5f 54  _TRACE.  "OMIT_T
908d0 52 41 43 45 22 2c 0a 23 65 6e 64 69 66 0a 23 69  RACE",.#endif.#i
908e0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
908f0 5f 54 52 49 47 47 45 52 0a 20 20 22 4f 4d 49 54  _TRIGGER.  "OMIT
90900 5f 54 52 49 47 47 45 52 22 2c 0a 23 65 6e 64 69  _TRIGGER",.#endi
90910 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
90920 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50  OMIT_TRUNCATE_OP
90930 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 22 4f 4d  TIMIZATION.  "OM
90940 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49  IT_TRUNCATE_OPTI
90950 4d 49 5a 41 54 49 4f 4e 22 2c 0a 23 65 6e 64 69  MIZATION",.#endi
90960 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
90970 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 22 4f 4d  OMIT_UTF16.  "OM
90980 49 54 5f 55 54 46 31 36 22 2c 0a 23 65 6e 64 69  IT_UTF16",.#endi
90990 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
909a0 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 20 20 22 4f  OMIT_VACUUM.  "O
909b0 4d 49 54 5f 56 41 43 55 55 4d 22 2c 0a 23 65 6e  MIT_VACUUM",.#en
909c0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
909d0 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 22 4f  E_OMIT_VIEW.  "O
909e0 4d 49 54 5f 56 49 45 57 22 2c 0a 23 65 6e 64 69  MIT_VIEW",.#endi
909f0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
90a00 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
90a10 45 0a 20 20 22 4f 4d 49 54 5f 56 49 52 54 55 41  E.  "OMIT_VIRTUA
90a20 4c 54 41 42 4c 45 22 2c 0a 23 65 6e 64 69 66 0a  LTABLE",.#endif.
90a30 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
90a40 49 54 5f 57 41 4c 0a 20 20 22 4f 4d 49 54 5f 57  IT_WAL.  "OMIT_W
90a50 41 4c 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64  AL",.#endif.#ifd
90a60 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57  ef SQLITE_OMIT_W
90a70 53 44 0a 20 20 22 4f 4d 49 54 5f 57 53 44 22 2c  SD.  "OMIT_WSD",
90a80 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
90a90 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f  QLITE_OMIT_XFER_
90aa0 4f 50 54 0a 20 20 22 4f 4d 49 54 5f 58 46 45 52  OPT.  "OMIT_XFER
90ab0 5f 4f 50 54 22 2c 0a 23 65 6e 64 69 66 0a 23 69  _OPT",.#endif.#i
90ac0 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46  fdef SQLITE_PERF
90ad0 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 20 20  ORMANCE_TRACE.  
90ae0 22 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41  "PERFORMANCE_TRA
90af0 43 45 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64  CE",.#endif.#ifd
90b00 65 66 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f  ef SQLITE_PROXY_
90b10 44 45 42 55 47 0a 20 20 22 50 52 4f 58 59 5f 44  DEBUG.  "PROXY_D
90b20 45 42 55 47 22 2c 0a 23 65 6e 64 69 66 0a 23 69  EBUG",.#endif.#i
90b30 66 64 65 66 20 53 51 4c 49 54 45 5f 52 54 52 45  fdef SQLITE_RTRE
90b40 45 5f 49 4e 54 5f 4f 4e 4c 59 0a 20 20 22 52 54  E_INT_ONLY.  "RT
90b50 52 45 45 5f 49 4e 54 5f 4f 4e 4c 59 22 2c 0a 23  REE_INT_ONLY",.#
90b60 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
90b70 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54  ITE_SECURE_DELET
90b80 45 0a 20 20 22 53 45 43 55 52 45 5f 44 45 4c 45  E.  "SECURE_DELE
90b90 54 45 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64  TE",.#endif.#ifd
90ba0 65 66 20 53 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f  ef SQLITE_SMALL_
90bb0 53 54 41 43 4b 0a 20 20 22 53 4d 41 4c 4c 5f 53  STACK.  "SMALL_S
90bc0 54 41 43 4b 22 2c 0a 23 65 6e 64 69 66 0a 23 69  TACK",.#endif.#i
90bd0 66 64 65 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e  fdef SQLITE_SOUN
90be0 44 45 58 0a 20 20 22 53 4f 55 4e 44 45 58 22 2c  DEX.  "SOUNDEX",
90bf0 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
90c00 51 4c 49 54 45 5f 54 43 4c 0a 20 20 22 54 43 4c  QLITE_TCL.  "TCL
90c10 22 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  ",.#endif.#ifdef
90c20 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
90c30 52 45 0a 20 20 22 54 45 4d 50 5f 53 54 4f 52 45  RE.  "TEMP_STORE
90c40 3d 22 20 43 54 49 4d 45 4f 50 54 5f 56 41 4c 28  =" CTIMEOPT_VAL(
90c50 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52  SQLITE_TEMP_STOR
90c60 45 29 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  E),.#endif.#ifde
90c70 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
90c80 22 54 45 53 54 22 2c 0a 23 65 6e 64 69 66 0a 23  "TEST",.#endif.#
90c90 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 48 52  ifdef SQLITE_THR
90ca0 45 41 44 53 41 46 45 0a 20 20 22 54 48 52 45 41  EADSAFE.  "THREA
90cb0 44 53 41 46 45 3d 22 20 43 54 49 4d 45 4f 50 54  DSAFE=" CTIMEOPT
90cc0 5f 56 41 4c 28 53 51 4c 49 54 45 5f 54 48 52 45  _VAL(SQLITE_THRE
90cd0 41 44 53 41 46 45 29 2c 0a 23 65 6e 64 69 66 0a  ADSAFE),.#endif.
90ce0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 55 53  #ifdef SQLITE_US
90cf0 45 5f 41 4c 4c 4f 43 41 0a 20 20 22 55 53 45 5f  E_ALLOCA.  "USE_
90d00 41 4c 4c 4f 43 41 22 2c 0a 23 65 6e 64 69 66 0a  ALLOCA",.#endif.
90d10 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 5a 45  #ifdef SQLITE_ZE
90d20 52 4f 5f 4d 41 4c 4c 4f 43 0a 20 20 22 5a 45 52  RO_MALLOC.  "ZER
90d30 4f 5f 4d 41 4c 4c 4f 43 22 0a 23 65 6e 64 69 66  O_MALLOC".#endif
90d40 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  .};../*.** Given
90d50 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63   the name of a c
90d60 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69  ompile-time opti
90d70 6f 6e 2c 20 72 65 74 75 72 6e 20 74 72 75 65 20  on, return true 
90d80 69 66 20 74 68 61 74 20 6f 70 74 69 6f 6e 0a 2a  if that option.*
90d90 2a 20 77 61 73 20 75 73 65 64 20 61 6e 64 20 66  * was used and f
90da0 61 6c 73 65 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a  alse if not..**.
90db0 2a 2a 20 54 68 65 20 6e 61 6d 65 20 63 61 6e 20  ** The name can 
90dc0 6f 70 74 69 6f 6e 61 6c 6c 79 20 62 65 67 69 6e  optionally begin
90dd0 20 77 69 74 68 20 22 53 51 4c 49 54 45 5f 22 20   with "SQLITE_" 
90de0 62 75 74 20 74 68 65 20 22 53 51 4c 49 54 45 5f  but the "SQLITE_
90df0 22 20 70 72 65 66 69 78 0a 2a 2a 20 69 73 20 6e  " prefix.** is n
90e00 6f 74 20 72 65 71 75 69 72 65 64 20 66 6f 72 20  ot required for 
90e10 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49  a match..*/.SQLI
90e20 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
90e30 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e  e3_compileoption
90e40 5f 75 73 65 64 28 63 6f 6e 73 74 20 63 68 61 72  _used(const char
90e50 20 2a 7a 4f 70 74 4e 61 6d 65 29 7b 0a 20 20 69   *zOptName){.  i
90e60 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20 73  nt i, n;.  if( s
90e70 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a  qlite3StrNICmp(z
90e80 4f 70 74 4e 61 6d 65 2c 20 22 53 51 4c 49 54 45  OptName, "SQLITE
90e90 5f 22 2c 20 37 29 3d 3d 30 20 29 20 7a 4f 70 74  _", 7)==0 ) zOpt
90ea0 4e 61 6d 65 20 2b 3d 20 37 3b 0a 20 20 6e 20 3d  Name += 7;.  n =
90eb0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
90ec0 28 7a 4f 70 74 4e 61 6d 65 29 3b 0a 0a 20 20 2f  (zOptName);..  /
90ed0 2a 20 53 69 6e 63 65 20 41 72 72 61 79 53 69 7a  * Since ArraySiz
90ee0 65 28 61 7a 43 6f 6d 70 69 6c 65 4f 70 74 29 20  e(azCompileOpt) 
90ef0 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 69 6e 20 73  is normally in s
90f00 69 6e 67 6c 65 20 64 69 67 69 74 73 2c 20 61 0a  ingle digits, a.
90f10 20 20 2a 2a 20 6c 69 6e 65 61 72 20 73 65 61 72    ** linear sear
90f20 63 68 20 69 73 20 61 64 65 71 75 61 74 65 2e 20  ch is adequate. 
90f30 20 4e 6f 20 6e 65 65 64 20 66 6f 72 20 61 20 62   No need for a b
90f40 69 6e 61 72 79 20 73 65 61 72 63 68 2e 20 2a 2f  inary search. */
90f50 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72  .  for(i=0; i<Ar
90f60 72 61 79 53 69 7a 65 28 61 7a 43 6f 6d 70 69 6c  raySize(azCompil
90f70 65 4f 70 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  eOpt); i++){.   
90f80 20 69 66 28 20 20 20 28 73 71 6c 69 74 65 33 53   if(   (sqlite3S
90f90 74 72 4e 49 43 6d 70 28 7a 4f 70 74 4e 61 6d 65  trNICmp(zOptName
90fa0 2c 20 61 7a 43 6f 6d 70 69 6c 65 4f 70 74 5b 69  , azCompileOpt[i
90fb0 5d 2c 20 6e 29 3d 3d 30 29 0a 20 20 20 20 20 20  ], n)==0).      
90fc0 20 26 26 20 28 20 28 61 7a 43 6f 6d 70 69 6c 65   && ( (azCompile
90fd0 4f 70 74 5b 69 5d 5b 6e 5d 3d 3d 30 29 20 7c 7c  Opt[i][n]==0) ||
90fe0 20 28 61 7a 43 6f 6d 70 69 6c 65 4f 70 74 5b 69   (azCompileOpt[i
90ff0 5d 5b 6e 5d 3d 3d 27 3d 27 29 20 29 20 29 20 72  ][n]=='=') ) ) r
91000 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72  eturn 1;.  }.  r
91010 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
91020 2a 20 52 65 74 75 72 6e 20 74 68 65 20 4e 2d 74  * Return the N-t
91030 68 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f  h compile-time o
91040 70 74 69 6f 6e 20 73 74 72 69 6e 67 2e 20 20 49  ption string.  I
91050 66 20 4e 20 69 73 20 6f 75 74 20 6f 66 20 72 61  f N is out of ra
91060 6e 67 65 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 61  nge,.** return a
91070 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a   NULL pointer..*
91080 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
91090 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
910a0 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67  _compileoption_g
910b0 65 74 28 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28  et(int N){.  if(
910c0 20 4e 3e 3d 30 20 26 26 20 4e 3c 41 72 72 61 79   N>=0 && N<Array
910d0 53 69 7a 65 28 61 7a 43 6f 6d 70 69 6c 65 4f 70  Size(azCompileOp
910e0 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  t) ){.    return
910f0 20 61 7a 43 6f 6d 70 69 6c 65 4f 70 74 5b 4e 5d   azCompileOpt[N]
91100 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
91110 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
91120 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49  QLITE_OMIT_COMPI
91130 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53 20 2a  LEOPTION_DIAGS *
91140 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
91150 2a 2a 20 45 6e 64 20 6f 66 20 63 74 69 6d 65 2e  ** End of ctime.
91160 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
91170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91190 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
911a0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 74  ** Begin file st
911b0 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  atus.c *********
911c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
911d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
911e0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75  */./*.** 2008 Ju
911f0 6e 65 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ne 18.**.** The 
91200 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
91210 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
91220 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
91230 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
91240 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
91250 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
91260 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
91270 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
91280 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
91290 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
912a0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
912b0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
912c0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
912d0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
912e0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
912f0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
91300 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
91310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91350 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
91360 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  module implement
91370 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74  s the sqlite3_st
91380 61 74 75 73 28 29 20 69 6e 74 65 72 66 61 63 65  atus() interface
91390 20 61 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a 20   and related.** 
913a0 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a  functionality..*
913b0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
913c0 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 49 6e  * Include vdbeIn
913d0 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  t.h in the middl
913e0 65 20 6f 66 20 73 74 61 74 75 73 2e 63 20 2a 2a  e of status.c **
913f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91400 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
91410 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62  * Begin file vdb
91420 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
91430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91450 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70  /./*.** 2003 Sep
91460 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54  tember 6.**.** T
91470 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
91480 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
91490 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
914a0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
914b0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
914c0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
914d0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
914e0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
914f0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
91500 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
91510 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
91520 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
91530 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
91540 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
91550 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
91560 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
91570 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
91580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
915a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
915b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
915c0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
915d0 69 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69  is the header fi
915e0 6c 65 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74 69  le for informati
915f0 6f 6e 20 74 68 61 74 20 69 73 20 70 72 69 76 61  on that is priva
91600 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44 42  te to the.** VDB
91610 45 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61  E.  This informa
91620 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c  tion used to all
91630 20 62 65 20 61 74 20 74 68 65 20 74 6f 70 20 6f   be at the top o
91640 66 20 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20  f the single.** 
91650 73 6f 75 72 63 65 20 63 6f 64 65 20 66 69 6c 65  source code file
91660 20 22 76 64 62 65 2e 63 22 2e 20 20 57 68 65 6e   "vdbe.c".  When
91670 20 74 68 61 74 20 66 69 6c 65 20 62 65 63 61 6d   that file becam
91680 65 20 74 6f 6f 20 62 69 67 20 28 6f 76 65 72 0a  e too big (over.
91690 2a 2a 20 36 30 30 30 20 6c 69 6e 65 73 20 6c 6f  ** 6000 lines lo
916a0 6e 67 29 20 69 74 20 77 61 73 20 73 70 6c 69 74  ng) it was split
916b0 20 75 70 20 69 6e 74 6f 20 73 65 76 65 72 61 6c   up into several
916c0 20 73 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20 61   smaller files a
916d0 6e 64 0a 2a 2a 20 74 68 69 73 20 68 65 61 64 65  nd.** this heade
916e0 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61  r information wa
916f0 73 20 66 61 63 74 6f 72 65 64 20 6f 75 74 2e 0a  s factored out..
91700 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 56 44 42 45  */.#ifndef _VDBE
91710 49 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  INT_H_.#define _
91720 56 44 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a  VDBEINT_H_../*.*
91730 2a 20 53 51 4c 20 69 73 20 74 72 61 6e 73 6c 61  * SQL is transla
91740 74 65 64 20 69 6e 74 6f 20 61 20 73 65 71 75 65  ted into a seque
91750 6e 63 65 20 6f 66 20 69 6e 73 74 72 75 63 74 69  nce of instructi
91760 6f 6e 73 20 74 6f 20 62 65 0a 2a 2a 20 65 78 65  ons to be.** exe
91770 63 75 74 65 64 20 62 79 20 61 20 76 69 72 74 75  cuted by a virtu
91780 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 45 61 63  al machine.  Eac
91790 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73  h instruction is
917a0 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20   an instance.** 
917b0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
917c0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74   structure..*/.t
917d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64  ypedef struct Vd
917e0 62 65 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20  beOp Op;../*.** 
917f0 42 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 73 0a 2a  Boolean values.*
91800 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e  /.typedef unsign
91810 65 64 20 63 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f  ed char Bool;../
91820 2a 20 4f 70 61 71 75 65 20 74 79 70 65 20 75 73  * Opaque type us
91830 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 76 64  ed by code in vd
91840 62 65 73 6f 72 74 2e 63 20 2a 2f 0a 74 79 70 65  besort.c */.type
91850 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 53  def struct VdbeS
91860 6f 72 74 65 72 20 56 64 62 65 53 6f 72 74 65 72  orter VdbeSorter
91870 3b 0a 0a 2f 2a 20 4f 70 61 71 75 65 20 74 79 70  ;../* Opaque typ
91880 65 20 75 73 65 64 20 62 79 20 74 68 65 20 65 78  e used by the ex
91890 70 6c 61 69 6e 65 72 20 2a 2f 0a 74 79 70 65 64  plainer */.typed
918a0 65 66 20 73 74 72 75 63 74 20 45 78 70 6c 61 69  ef struct Explai
918b0 6e 20 45 78 70 6c 61 69 6e 3b 0a 0a 2f 2a 0a 2a  n Explain;../*.*
918c0 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 61 20  * A cursor is a 
918d0 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61 20 73  pointer into a s
918e0 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69 74 68  ingle BTree with
918f0 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69  in a database fi
91900 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f  le..** The curso
91910 72 20 63 61 6e 20 73 65 65 6b 20 74 6f 20 61 20  r can seek to a 
91920 42 54 72 65 65 20 65 6e 74 72 79 20 77 69 74 68  BTree entry with
91930 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6b 65   a particular ke
91940 79 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 6f 76  y, or.** loop ov
91950 65 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 6f  er all entries o
91960 66 20 74 68 65 20 42 74 72 65 65 2e 20 20 59 6f  f the Btree.  Yo
91970 75 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73 65 72  u can also inser
91980 74 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a 20 65  t new BTree.** e
91990 6e 74 72 69 65 73 20 6f 72 20 72 65 74 72 69 65  ntries or retrie
919a0 76 65 20 74 68 65 20 6b 65 79 20 6f 72 20 64 61  ve the key or da
919b0 74 61 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72  ta from the entr
919c0 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  y that the curso
919d0 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c  r.** is currentl
919e0 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a  y pointing to..*
919f0 2a 20 0a 2a 2a 20 45 76 65 72 79 20 63 75 72 73  * .** Every curs
91a00 6f 72 20 74 68 61 74 20 74 68 65 20 76 69 72 74  or that the virt
91a10 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 20  ual machine has 
91a20 6f 70 65 6e 20 69 73 20 72 65 70 72 65 73 65 6e  open is represen
91a30 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73  ted by an.** ins
91a40 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
91a50 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
91a60 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65  ..*/.struct Vdbe
91a70 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 43 75 72  Cursor {.  BtCur
91a80 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 20 20 20  sor *pCursor;   
91a90 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 73   /* The cursor s
91aa0 74 72 75 63 74 75 72 65 20 6f 66 20 74 68 65 20  tructure of the 
91ab0 62 61 63 6b 65 6e 64 20 2a 2f 0a 20 20 42 74 72  backend */.  Btr
91ac0 65 65 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20  ee *pBt;        
91ad0 20 20 20 2f 2a 20 53 65 70 61 72 61 74 65 20 66     /* Separate f
91ae0 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70  ile holding temp
91af0 6f 72 61 72 79 20 74 61 62 6c 65 20 2a 2f 0a 20  orary table */. 
91b00 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
91b10 66 6f 3b 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61  fo;    /* Info a
91b20 62 6f 75 74 20 69 6e 64 65 78 20 6b 65 79 73 20  bout index keys 
91b30 6e 65 65 64 65 64 20 62 79 20 69 6e 64 65 78 20  needed by index 
91b40 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74  cursors */.  int
91b50 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
91b60 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63     /* Index of c
91b70 75 72 73 6f 72 20 64 61 74 61 62 61 73 65 20 69  ursor database i
91b80 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 28 6f 72 20  n db->aDb[] (or 
91b90 2d 31 29 20 2a 2f 0a 20 20 69 6e 74 20 70 73 65  -1) */.  int pse
91ba0 75 64 6f 54 61 62 6c 65 52 65 67 3b 20 20 20 2f  udoTableReg;   /
91bb0 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
91bc0 6e 67 20 70 73 65 75 64 6f 74 61 62 6c 65 20 63  ng pseudotable c
91bd0 6f 6e 74 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74  ontent. */.  int
91be0 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20   nField;        
91bf0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
91c00 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 68 65  fields in the he
91c10 61 64 65 72 20 2a 2f 0a 20 20 42 6f 6f 6c 20 7a  ader */.  Bool z
91c20 65 72 6f 65 64 3b 20 20 20 20 20 20 20 20 20 20  eroed;          
91c30 2f 2a 20 54 72 75 65 20 69 66 20 7a 65 72 6f 65  /* True if zeroe
91c40 64 20 6f 75 74 20 61 6e 64 20 72 65 61 64 79 20  d out and ready 
91c50 66 6f 72 20 72 65 75 73 65 20 2a 2f 0a 20 20 42  for reuse */.  B
91c60 6f 6f 6c 20 72 6f 77 69 64 49 73 56 61 6c 69 64  ool rowidIsValid
91c70 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  ;    /* True if 
91c80 6c 61 73 74 52 6f 77 69 64 20 69 73 20 76 61 6c  lastRowid is val
91c90 69 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 61 74 46  id */.  Bool atF
91ca0 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  irst;         /*
91cb0 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74 69 6e   True if pointin
91cc0 67 20 74 6f 20 66 69 72 73 74 20 65 6e 74 72 79  g to first entry
91cd0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 75 73 65 52 61   */.  Bool useRa
91ce0 6e 64 6f 6d 52 6f 77 69 64 3b 20 20 2f 2a 20 47  ndomRowid;  /* G
91cf0 65 6e 65 72 61 74 65 20 6e 65 77 20 72 65 63 6f  enerate new reco
91d00 72 64 20 6e 75 6d 62 65 72 73 20 73 65 6d 69 2d  rd numbers semi-
91d10 72 61 6e 64 6f 6d 6c 79 20 2a 2f 0a 20 20 42 6f  randomly */.  Bo
91d20 6f 6c 20 6e 75 6c 6c 52 6f 77 3b 20 20 20 20 20  ol nullRow;     
91d30 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
91d40 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f 77  ointing to a row
91d50 20 77 69 74 68 20 6e 6f 20 64 61 74 61 20 2a 2f   with no data */
91d60 0a 20 20 42 6f 6f 6c 20 64 65 66 65 72 72 65 64  .  Bool deferred
91d70 4d 6f 76 65 74 6f 3b 20 20 2f 2a 20 41 20 63 61  Moveto;  /* A ca
91d80 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  ll to sqlite3Btr
91d90 65 65 4d 6f 76 65 74 6f 28 29 20 69 73 20 6e 65  eeMoveto() is ne
91da0 65 64 65 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69  eded */.  Bool i
91db0 73 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20  sTable;         
91dc0 2f 2a 20 54 72 75 65 20 69 66 20 61 20 74 61 62  /* True if a tab
91dd0 6c 65 20 72 65 71 75 69 72 69 6e 67 20 69 6e 74  le requiring int
91de0 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a 20 20 42  eger keys */.  B
91df0 6f 6f 6c 20 69 73 49 6e 64 65 78 3b 20 20 20 20  ool isIndex;    
91e00 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
91e10 61 6e 20 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e  an index contain
91e20 69 6e 67 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20  ing keys only - 
91e30 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 42 6f 6f  no data */.  Boo
91e40 6c 20 69 73 4f 72 64 65 72 65 64 3b 20 20 20 20  l isOrdered;    
91e50 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
91e60 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 74 61 62  e underlying tab
91e70 6c 65 20 69 73 20 42 54 52 45 45 5f 55 4e 4f 52  le is BTREE_UNOR
91e80 44 45 52 45 44 20 2a 2f 0a 20 20 42 6f 6f 6c 20  DERED */.  Bool 
91e90 69 73 53 6f 72 74 65 72 3b 20 20 20 20 20 20 20  isSorter;       
91ea0 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6e 65   /* True if a ne
91eb0 77 2d 73 74 79 6c 65 20 73 6f 72 74 65 72 20 2a  w-style sorter *
91ec0 2f 0a 20 20 42 6f 6f 6c 20 6d 75 6c 74 69 50 73  /.  Bool multiPs
91ed0 65 75 64 6f 3b 20 20 20 20 20 2f 2a 20 4d 75 6c  eudo;     /* Mul
91ee0 74 69 2d 72 65 67 69 73 74 65 72 20 70 73 65 75  ti-register pseu
91ef0 64 6f 2d 63 75 72 73 6f 72 20 2a 2f 0a 20 20 73  do-cursor */.  s
91f00 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
91f10 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b  or *pVtabCursor;
91f20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
91f30 66 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 61  for a virtual ta
91f40 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ble */.  const s
91f50 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
91f60 4d 6f 64 75 6c 65 3b 20 20 20 20 20 2f 2a 20 4d  Module;     /* M
91f70 6f 64 75 6c 65 20 66 6f 72 20 63 75 72 73 6f 72  odule for cursor
91f80 20 70 56 74 61 62 43 75 72 73 6f 72 20 2a 2f 0a   pVtabCursor */.
91f90 20 20 69 36 34 20 73 65 71 43 6f 75 6e 74 3b 20    i64 seqCount; 
91fa0 20 20 20 20 20 20 20 20 2f 2a 20 53 65 71 75 65          /* Seque
91fb0 6e 63 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  nce counter */. 
91fc0 20 69 36 34 20 6d 6f 76 65 74 6f 54 61 72 67 65   i64 movetoTarge
91fd0 74 3b 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65  t;     /* Argume
91fe0 6e 74 20 74 6f 20 74 68 65 20 64 65 66 65 72 72  nt to the deferr
91ff0 65 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  ed sqlite3BtreeM
92000 6f 76 65 74 6f 28 29 20 2a 2f 0a 20 20 69 36 34  oveto() */.  i64
92010 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20   lastRowid;     
92020 20 20 20 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64     /* Last rowid
92030 20 66 72 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20   from a Next or 
92040 4e 65 78 74 49 64 78 20 6f 70 65 72 61 74 69 6f  NextIdx operatio
92050 6e 20 2a 2f 0a 20 20 56 64 62 65 53 6f 72 74 65  n */.  VdbeSorte
92060 72 20 2a 70 53 6f 72 74 65 72 3b 20 20 2f 2a 20  r *pSorter;  /* 
92070 53 6f 72 74 65 72 20 6f 62 6a 65 63 74 20 66 6f  Sorter object fo
92080 72 20 4f 50 5f 53 6f 72 74 65 72 4f 70 65 6e 20  r OP_SorterOpen 
92090 63 75 72 73 6f 72 73 20 2a 2f 0a 0a 20 20 2f 2a  cursors */..  /*
920a0 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 20   Result of last 
920b0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
920c0 74 6f 28 29 20 64 6f 6e 65 20 62 79 20 61 6e 20  to() done by an 
920d0 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72 20  OP_NotExists or 
920e0 0a 20 20 2a 2a 20 4f 50 5f 49 73 55 6e 69 71 75  .  ** OP_IsUniqu
920f0 65 20 6f 70 63 6f 64 65 20 6f 6e 20 74 68 69 73  e opcode on this
92100 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 69 6e   cursor. */.  in
92110 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 0a 0a 20  t seekResult;.. 
92120 20 2f 2a 20 43 61 63 68 65 64 20 69 6e 66 6f 72   /* Cached infor
92130 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
92140 20 68 65 61 64 65 72 20 66 6f 72 20 74 68 65 20   header for the 
92150 64 61 74 61 20 72 65 63 6f 72 64 20 74 68 61 74  data record that
92160 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 73 6f 72   the.  ** cursor
92170 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
92180 69 6e 74 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c 79  inting to.  Only
92190 20 76 61 6c 69 64 20 69 66 20 63 61 63 68 65 53   valid if cacheS
921a0 74 61 74 75 73 20 6d 61 74 63 68 65 73 0a 20 20  tatus matches.  
921b0 2a 2a 20 56 64 62 65 2e 63 61 63 68 65 43 74 72  ** Vdbe.cacheCtr
921c0 2e 20 20 56 64 62 65 2e 63 61 63 68 65 43 74 72  .  Vdbe.cacheCtr
921d0 20 77 69 6c 6c 20 6e 65 76 65 72 20 74 61 6b 65   will never take
921e0 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66   on the value of
921f0 0a 20 20 2a 2a 20 43 41 43 48 45 5f 53 54 41 4c  .  ** CACHE_STAL
92200 45 20 61 6e 64 20 73 6f 20 73 65 74 74 69 6e 67  E and so setting
92210 20 63 61 63 68 65 53 74 61 74 75 73 3d 43 41 43   cacheStatus=CAC
92220 48 45 5f 53 54 41 4c 45 20 67 75 61 72 61 6e 74  HE_STALE guarant
92230 65 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 74 68  ees that.  ** th
92240 65 20 63 61 63 68 65 20 69 73 20 6f 75 74 20 6f  e cache is out o
92250 66 20 64 61 74 65 2e 0a 20 20 2a 2a 0a 20 20 2a  f date..  **.  *
92260 2a 20 61 52 6f 77 20 6d 69 67 68 74 20 70 6f 69  * aRow might poi
92270 6e 74 20 74 6f 20 28 65 70 68 65 6d 65 72 61 6c  nt to (ephemeral
92280 29 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 63  ) data for the c
92290 75 72 72 65 6e 74 20 72 6f 77 2c 20 6f 72 20 69  urrent row, or i
922a0 74 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65 20  t might.  ** be 
922b0 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 75 33 32  NULL..  */.  u32
922c0 20 63 61 63 68 65 53 74 61 74 75 73 3b 20 20 20   cacheStatus;   
922d0 20 20 20 2f 2a 20 43 61 63 68 65 20 69 73 20 76     /* Cache is v
922e0 61 6c 69 64 20 69 66 20 74 68 69 73 20 6d 61 74  alid if this mat
922f0 63 68 65 73 20 56 64 62 65 2e 63 61 63 68 65 43  ches Vdbe.cacheC
92300 74 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 79 6c  tr */.  int payl
92310 6f 61 64 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a  oadSize;      /*
92320 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
92330 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 65   bytes in the re
92340 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61  cord */.  u32 *a
92350 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  Type;           
92360 2f 2a 20 54 79 70 65 20 76 61 6c 75 65 73 20 66  /* Type values f
92370 6f 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69  or all entries i
92380 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a  n the record */.
92390 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20    u32 *aOffset; 
923a0 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65          /* Cache
923b0 64 20 6f 66 66 73 65 74 73 20 74 6f 20 74 68 65  d offsets to the
923c0 20 73 74 61 72 74 20 6f 66 20 65 61 63 68 20 63   start of each c
923d0 6f 6c 75 6d 6e 73 20 64 61 74 61 20 2a 2f 0a 20  olumns data */. 
923e0 20 75 38 20 2a 61 52 6f 77 3b 20 20 20 20 20 20   u8 *aRow;      
923f0 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66         /* Data f
92400 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  or the current r
92410 6f 77 2c 20 69 66 20 61 6c 6c 20 6f 6e 20 6f 6e  ow, if all on on
92420 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74 79 70  e page */.};.typ
92430 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65  edef struct Vdbe
92440 43 75 72 73 6f 72 20 56 64 62 65 43 75 72 73 6f  Cursor VdbeCurso
92450 72 3b 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61  r;../*.** When a
92460 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20   sub-program is 
92470 65 78 65 63 75 74 65 64 20 28 4f 50 5f 50 72 6f  executed (OP_Pro
92480 67 72 61 6d 29 2c 20 61 20 73 74 72 75 63 74 75  gram), a structu
92490 72 65 20 6f 66 20 74 68 69 73 20 74 79 70 65 0a  re of this type.
924a0 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  ** is allocated 
924b0 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 75 72  to store the cur
924c0 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68  rent value of th
924d0 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65  e program counte
924e0 72 2c 20 61 73 0a 2a 2a 20 77 65 6c 6c 20 61 73  r, as.** well as
924f0 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 65 6d   the current mem
92500 6f 72 79 20 63 65 6c 6c 20 61 72 72 61 79 20 61  ory cell array a
92510 6e 64 20 76 61 72 69 6f 75 73 20 6f 74 68 65 72  nd various other
92520 20 66 72 61 6d 65 20 73 70 65 63 69 66 69 63 0a   frame specific.
92530 2a 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64  ** values stored
92540 20 69 6e 20 74 68 65 20 56 64 62 65 20 73 74 72   in the Vdbe str
92550 75 63 74 2e 20 57 68 65 6e 20 74 68 65 20 73 75  uct. When the su
92560 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 66 69 6e  b-program is fin
92570 69 73 68 65 64 2c 20 0a 2a 2a 20 74 68 65 73 65  ished, .** these
92580 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f 70 69   values are copi
92590 65 64 20 62 61 63 6b 20 74 6f 20 74 68 65 20 56  ed back to the V
925a0 64 62 65 20 66 72 6f 6d 20 74 68 65 20 56 64 62  dbe from the Vdb
925b0 65 46 72 61 6d 65 20 73 74 72 75 63 74 75 72 65  eFrame structure
925c0 2c 0a 2a 2a 20 72 65 73 74 6f 72 69 6e 67 20 74  ,.** restoring t
925d0 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20  he state of the 
925e0 56 4d 20 74 6f 20 61 73 20 69 74 20 77 61 73 20  VM to as it was 
925f0 62 65 66 6f 72 65 20 74 68 65 20 73 75 62 2d 70  before the sub-p
92600 72 6f 67 72 61 6d 0a 2a 2a 20 62 65 67 61 6e 20  rogram.** began 
92610 65 78 65 63 75 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a  executing..**.**
92620 20 54 68 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20   The memory for 
92630 61 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65  a VdbeFrame obje
92640 63 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  ct is allocated 
92650 61 6e 64 20 6d 61 6e 61 67 65 64 20 62 79 20 61  and managed by a
92660 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63 65 6c 6c 20   memory.** cell 
92670 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 28 63  in the parent (c
92680 61 6c 6c 69 6e 67 29 20 66 72 61 6d 65 2e 20 57  alling) frame. W
92690 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 63  hen the memory c
926a0 65 6c 6c 20 69 73 20 64 65 6c 65 74 65 64 20 6f  ell is deleted o
926b0 72 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e  r.** overwritten
926c0 2c 20 74 68 65 20 56 64 62 65 46 72 61 6d 65 20  , the VdbeFrame 
926d0 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 66 72  object is not fr
926e0 65 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e  eed immediately.
926f0 20 49 6e 73 74 65 61 64 2c 20 69 74 0a 2a 2a 20   Instead, it.** 
92700 69 73 20 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74  is linked into t
92710 68 65 20 56 64 62 65 2e 70 44 65 6c 46 72 61 6d  he Vdbe.pDelFram
92720 65 20 6c 69 73 74 2e 20 54 68 65 20 63 6f 6e 74  e list. The cont
92730 65 6e 74 73 20 6f 66 20 74 68 65 20 56 64 62 65  ents of the Vdbe
92740 2e 70 44 65 6c 46 72 61 6d 65 0a 2a 2a 20 6c 69  .pDelFrame.** li
92750 73 74 20 69 73 20 64 65 6c 65 74 65 64 20 77 68  st is deleted wh
92760 65 6e 20 74 68 65 20 56 4d 20 69 73 20 72 65 73  en the VM is res
92770 65 74 20 69 6e 20 56 64 62 65 48 61 6c 74 28 29  et in VdbeHalt()
92780 2e 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72  . The reason for
92790 20 64 6f 69 6e 67 0a 2a 2a 20 74 68 69 73 20 69   doing.** this i
927a0 6e 73 74 65 61 64 20 6f 66 20 64 65 6c 65 74 69  nstead of deleti
927b0 6e 67 20 74 68 65 20 56 64 62 65 46 72 61 6d 65  ng the VdbeFrame
927c0 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 69 73 20   immediately is 
927d0 74 6f 20 61 76 6f 69 64 20 72 65 63 75 72 73 69  to avoid recursi
927e0 76 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73  ve.** calls to s
927f0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
92800 65 61 73 65 28 29 20 77 68 65 6e 20 74 68 65 20  ease() when the 
92810 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 62 65 6c  memory cells bel
92820 6f 6e 67 69 6e 67 20 74 6f 20 74 68 65 0a 2a 2a  onging to the.**
92830 20 63 68 69 6c 64 20 66 72 61 6d 65 20 61 72 65   child frame are
92840 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a   released..**.**
92850 20 54 68 65 20 63 75 72 72 65 6e 74 6c 79 20 65   The currently e
92860 78 65 63 75 74 69 6e 67 20 66 72 61 6d 65 20 69  xecuting frame i
92870 73 20 73 74 6f 72 65 64 20 69 6e 20 56 64 62 65  s stored in Vdbe
92880 2e 70 46 72 61 6d 65 2e 20 56 64 62 65 2e 70 46  .pFrame. Vdbe.pF
92890 72 61 6d 65 20 69 73 0a 2a 2a 20 73 65 74 20 74  rame is.** set t
928a0 6f 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 63 75  o NULL if the cu
928b0 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e  rrently executin
928c0 67 20 66 72 61 6d 65 20 69 73 20 74 68 65 20 6d  g frame is the m
928d0 61 69 6e 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a  ain program..*/.
928e0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56  typedef struct V
928f0 64 62 65 46 72 61 6d 65 20 56 64 62 65 46 72 61  dbeFrame VdbeFra
92900 6d 65 3b 0a 73 74 72 75 63 74 20 56 64 62 65 46  me;.struct VdbeF
92910 72 61 6d 65 20 7b 0a 20 20 56 64 62 65 20 2a 76  rame {.  Vdbe *v
92920 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
92930 20 2f 2a 20 56 4d 20 74 68 69 73 20 66 72 61 6d   /* VM this fram
92940 65 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a  e belongs to */.
92950 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 50 61    VdbeFrame *pPa
92960 72 65 6e 74 3b 20 20 20 20 20 2f 2a 20 50 61 72  rent;     /* Par
92970 65 6e 74 20 6f 66 20 74 68 69 73 20 66 72 61 6d  ent of this fram
92980 65 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 70 61  e, or NULL if pa
92990 72 65 6e 74 20 69 73 20 6d 61 69 6e 20 2a 2f 0a  rent is main */.
929a0 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20    Op *aOp;      
929b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f            /* Pro
929c0 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e  gram instruction
929d0 73 20 66 6f 72 20 70 61 72 65 6e 74 20 66 72 61  s for parent fra
929e0 6d 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65  me */.  Mem *aMe
929f0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
92a00 2f 2a 20 41 72 72 61 79 20 6f 66 20 6d 65 6d 6f  /* Array of memo
92a10 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 70 61 72  ry cells for par
92a20 65 6e 74 20 66 72 61 6d 65 20 2a 2f 0a 20 20 75  ent frame */.  u
92a30 38 20 2a 61 4f 6e 63 65 46 6c 61 67 3b 20 20 20  8 *aOnceFlag;   
92a40 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
92a50 6f 66 20 4f 50 5f 4f 6e 63 65 20 66 6c 61 67 73  of OP_Once flags
92a60 20 66 6f 72 20 70 61 72 65 6e 74 20 66 72 61 6d   for parent fram
92a70 65 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f  e */.  VdbeCurso
92a80 72 20 2a 2a 61 70 43 73 72 3b 20 20 20 20 20 2f  r **apCsr;     /
92a90 2a 20 41 72 72 61 79 20 6f 66 20 56 64 62 65 20  * Array of Vdbe 
92aa0 63 75 72 73 6f 72 73 20 66 6f 72 20 70 61 72 65  cursors for pare
92ab0 6e 74 20 66 72 61 6d 65 20 2a 2f 0a 20 20 76 6f  nt frame */.  vo
92ac0 69 64 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20  id *token;      
92ad0 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66        /* Copy of
92ae0 20 53 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b 65   SubProgram.toke
92af0 6e 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52  n */.  i64 lastR
92b00 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f  owid;          /
92b10 2a 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f  * Last insert ro
92b20 77 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73  wid (sqlite3.las
92b30 74 52 6f 77 69 64 29 20 2a 2f 0a 20 20 75 31 36  tRowid) */.  u16
92b40 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20   nCursor;       
92b50 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
92b60 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 70 43  f entries in apC
92b70 73 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20  sr */.  int pc; 
92b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
92b90 2f 2a 20 50 72 6f 67 72 61 6d 20 43 6f 75 6e 74  /* Program Count
92ba0 65 72 20 69 6e 20 70 61 72 65 6e 74 20 28 63 61  er in parent (ca
92bb0 6c 6c 69 6e 67 29 20 66 72 61 6d 65 20 2a 2f 0a  lling) frame */.
92bc0 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20    int nOp;      
92bd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
92be0 65 20 6f 66 20 61 4f 70 20 61 72 72 61 79 20 2a  e of aOp array *
92bf0 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20  /.  int nMem;   
92c00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
92c10 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
92c20 20 69 6e 20 61 4d 65 6d 20 2a 2f 0a 20 20 69 6e   in aMem */.  in
92c30 74 20 6e 4f 6e 63 65 46 6c 61 67 3b 20 20 20 20  t nOnceFlag;    
92c40 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
92c50 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 4f  of entries in aO
92c60 6e 63 65 46 6c 61 67 20 2a 2f 0a 20 20 69 6e 74  nceFlag */.  int
92c70 20 6e 43 68 69 6c 64 4d 65 6d 3b 20 20 20 20 20   nChildMem;     
92c80 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
92c90 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66  f memory cells f
92ca0 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65 20 2a  or child frame *
92cb0 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 43 73  /.  int nChildCs
92cc0 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  r;          /* N
92cd0 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73  umber of cursors
92ce0 20 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65   for child frame
92cf0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67   */.  int nChang
92d00 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
92d10 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67   Statement chang
92d20 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e 67 65  es (Vdbe.nChange
92d30 73 29 20 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 23 64  s)     */.};..#d
92d40 65 66 69 6e 65 20 56 64 62 65 46 72 61 6d 65 4d  efine VdbeFrameM
92d50 65 6d 28 70 29 20 28 28 4d 65 6d 20 2a 29 26 28  em(p) ((Mem *)&(
92d60 28 75 38 20 2a 29 70 29 5b 52 4f 55 4e 44 38 28  (u8 *)p)[ROUND8(
92d70 73 69 7a 65 6f 66 28 56 64 62 65 46 72 61 6d 65  sizeof(VdbeFrame
92d80 29 29 5d 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 61  ))])../*.** A va
92d90 6c 75 65 20 66 6f 72 20 56 64 62 65 43 75 72 73  lue for VdbeCurs
92da0 6f 72 2e 63 61 63 68 65 56 61 6c 69 64 20 74 68  or.cacheValid th
92db0 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 61 63  at means the cac
92dc0 68 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 76  he is always inv
92dd0 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  alid..*/.#define
92de0 20 43 41 43 48 45 5f 53 54 41 4c 45 20 30 0a 0a   CACHE_STALE 0..
92df0 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 6c 79  /*.** Internally
92e00 2c 20 74 68 65 20 76 64 62 65 20 6d 61 6e 69 70  , the vdbe manip
92e10 75 6c 61 74 65 73 20 6e 65 61 72 6c 79 20 61 6c  ulates nearly al
92e20 6c 20 53 51 4c 20 76 61 6c 75 65 73 20 61 73 20  l SQL values as 
92e30 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  Mem.** structure
92e40 73 2e 20 45 61 63 68 20 4d 65 6d 20 73 74 72 75  s. Each Mem stru
92e50 63 74 20 6d 61 79 20 63 61 63 68 65 20 6d 75 6c  ct may cache mul
92e60 74 69 70 6c 65 20 72 65 70 72 65 73 65 6e 74 61  tiple representa
92e70 74 69 6f 6e 73 20 28 73 74 72 69 6e 67 2c 0a 2a  tions (string,.*
92e80 2a 20 69 6e 74 65 67 65 72 20 65 74 63 2e 29 20  * integer etc.) 
92e90 6f 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75  of the same valu
92ea0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d  e..*/.struct Mem
92eb0 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   {.  sqlite3 *db
92ec0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
92ed0 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62  associated datab
92ee0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
92ef0 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20  /.  char *z;    
92f00 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e          /* Strin
92f10 67 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 65 20  g or BLOB value 
92f20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 20  */.  double r;  
92f30 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 6c           /* Real
92f40 20 76 61 6c 75 65 20 2a 2f 0a 20 20 75 6e 69 6f   value */.  unio
92f50 6e 20 7b 0a 20 20 20 20 69 36 34 20 69 3b 20 20  n {.    i64 i;  
92f60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
92f70 6e 74 65 67 65 72 20 76 61 6c 75 65 20 75 73 65  nteger value use
92f80 64 20 77 68 65 6e 20 4d 45 4d 5f 49 6e 74 20 69  d when MEM_Int i
92f90 73 20 73 65 74 20 69 6e 20 66 6c 61 67 73 20 2a  s set in flags *
92fa0 2f 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b  /.    int nZero;
92fb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
92fc0 64 20 77 68 65 6e 20 62 69 74 20 4d 45 4d 5f 5a  d when bit MEM_Z
92fd0 65 72 6f 20 69 73 20 73 65 74 20 69 6e 20 66 6c  ero is set in fl
92fe0 61 67 73 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44  ags */.    FuncD
92ff0 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20 20 2f  ef *pDef;      /
93000 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e  * Used only when
93010 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67 67 20   flags==MEM_Agg 
93020 2a 2f 0a 20 20 20 20 52 6f 77 53 65 74 20 2a 70  */.    RowSet *p
93030 52 6f 77 53 65 74 3b 20 20 20 20 2f 2a 20 55 73  RowSet;    /* Us
93040 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c 61  ed only when fla
93050 67 73 3d 3d 4d 45 4d 5f 52 6f 77 53 65 74 20 2a  gs==MEM_RowSet *
93060 2f 0a 20 20 20 20 56 64 62 65 46 72 61 6d 65 20  /.    VdbeFrame 
93070 2a 70 46 72 61 6d 65 3b 20 20 2f 2a 20 55 73 65  *pFrame;  /* Use
93080 64 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45  d when flags==ME
93090 4d 5f 46 72 61 6d 65 20 2a 2f 0a 20 20 7d 20 75  M_Frame */.  } u
930a0 3b 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20  ;.  int n;      
930b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
930c0 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
930d0 69 6e 20 73 74 72 69 6e 67 20 76 61 6c 75 65 2c  in string value,
930e0 20 65 78 63 6c 75 64 69 6e 67 20 27 5c 30 27 20   excluding '\0' 
930f0 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20  */.  u16 flags; 
93100 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65           /* Some
93110 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20   combination of 
93120 4d 45 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d 5f 53 74  MEM_Null, MEM_St
93130 72 2c 20 4d 45 4d 5f 44 79 6e 2c 20 65 74 63 2e  r, MEM_Dyn, etc.
93140 20 2a 2f 0a 20 20 75 38 20 20 74 79 70 65 3b 20   */.  u8  type; 
93150 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
93160 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c   of SQLITE_NULL,
93170 20 53 51 4c 49 54 45 5f 54 45 58 54 2c 20 53 51   SQLITE_TEXT, SQ
93180 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c 20 65 74  LITE_INTEGER, et
93190 63 20 2a 2f 0a 20 20 75 38 20 20 65 6e 63 3b 20  c */.  u8  enc; 
931a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51             /* SQ
931b0 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
931c0 45 5f 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54  E_UTF16BE, SQLIT
931d0 45 5f 55 54 46 31 36 4c 45 20 2a 2f 0a 23 69 66  E_UTF16LE */.#if
931e0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
931f0 0a 20 20 4d 65 6d 20 2a 70 53 63 6f 70 79 46 72  .  Mem *pScopyFr
93200 6f 6d 3b 20 20 20 20 2f 2a 20 54 68 69 73 20 4d  om;    /* This M
93210 65 6d 20 69 73 20 61 20 73 68 61 6c 6c 6f 77 20  em is a shallow 
93220 63 6f 70 79 20 6f 66 20 70 53 63 6f 70 79 46 72  copy of pScopyFr
93230 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 46  om */.  void *pF
93240 69 6c 6c 65 72 3b 20 20 20 20 20 20 2f 2a 20 53  iller;      /* S
93250 6f 20 74 68 61 74 20 73 69 7a 65 6f 66 28 4d 65  o that sizeof(Me
93260 6d 29 20 69 73 20 61 20 6d 75 6c 74 69 70 6c 65  m) is a multiple
93270 20 6f 66 20 38 20 2a 2f 0a 23 65 6e 64 69 66 0a   of 8 */.#endif.
93280 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
93290 6f 69 64 20 2a 29 3b 20 20 2f 2a 20 49 66 20 6e  oid *);  /* If n
932a0 6f 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c 20 74 68  ot null, call th
932b0 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64  is function to d
932c0 65 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a 2f 0a 20  elete Mem.z */. 
932d0 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20   char *zMalloc; 
932e0 20 20 20 20 20 2f 2a 20 44 79 6e 61 6d 69 63 20       /* Dynamic 
932f0 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64  buffer allocated
93300 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   by sqlite3_mall
93310 6f 63 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 4f  oc() */.};../* O
93320 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
93330 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67  e following flag
93340 73 20 61 72 65 20 73 65 74 20 74 6f 20 69 6e 64  s are set to ind
93350 69 63 61 74 65 20 74 68 65 20 76 61 6c 69 64 4f  icate the validO
93360 4b 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74  K.** representat
93370 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 6c 75  ions of the valu
93380 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
93390 4d 65 6d 20 73 74 72 75 63 74 2e 0a 2a 2a 0a 2a  Mem struct..**.*
933a0 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 4e 75 6c  * If the MEM_Nul
933b0 6c 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74  l flag is set, t
933c0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73  hen the value is
933d0 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c   an SQL NULL val
933e0 75 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 65 72 20  ue..** No other 
933f0 66 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65 74  flags may be set
93400 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a   in this case..*
93410 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f  *.** If the MEM_
93420 53 74 72 20 66 6c 61 67 20 69 73 20 73 65 74 20  Str flag is set 
93430 74 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74  then Mem.z point
93440 73 20 61 74 20 61 20 73 74 72 69 6e 67 20 72 65  s at a string re
93450 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a  presentation..**
93460 20 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69 73   Usually this is
93470 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20   encoded in the 
93480 73 61 6d 65 20 75 6e 69 63 6f 64 65 20 65 6e 63  same unicode enc
93490 6f 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61 69  oding as the mai
934a0 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 28 73  n.** database (s
934b0 65 65 20 62 65 6c 6f 77 20 66 6f 72 20 65 78 63  ee below for exc
934c0 65 70 74 69 6f 6e 73 29 2e 20 49 66 20 74 68 65  eptions). If the
934d0 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20 69   MEM_Term flag i
934e0 73 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 2c 20 74  s also.** set, t
934f0 68 65 6e 20 74 68 65 20 73 74 72 69 6e 67 20 69  hen the string i
93500 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64  s nul terminated
93510 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20 61 6e  . The MEM_Int an
93520 64 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a 2a 20 66  d MEM_Real .** f
93530 6c 61 67 73 20 6d 61 79 20 63 6f 65 78 69 73 74  lags may coexist
93540 20 77 69 74 68 20 74 68 65 20 4d 45 4d 5f 53 74   with the MEM_St
93550 72 20 66 6c 61 67 2e 0a 2a 2f 0a 23 64 65 66 69  r flag..*/.#defi
93560 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 20  ne MEM_Null     
93570 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61 6c   0x0001   /* Val
93580 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64  ue is NULL */.#d
93590 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 20  efine MEM_Str   
935a0 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a 20      0x0002   /* 
935b0 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e  Value is a strin
935c0 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  g */.#define MEM
935d0 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30 30  _Int       0x000
935e0 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20  4   /* Value is 
935f0 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64  an integer */.#d
93600 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 20  efine MEM_Real  
93610 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a 20      0x0008   /* 
93620 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c 20  Value is a real 
93630 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 6e  number */.#defin
93640 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 20  e MEM_Blob      
93650 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 75  0x0010   /* Valu
93660 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a 23  e is a BLOB */.#
93670 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53 65  define MEM_RowSe
93680 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a  t    0x0020   /*
93690 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77 53   Value is a RowS
936a0 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 65  et object */.#de
936b0 66 69 6e 65 20 4d 45 4d 5f 46 72 61 6d 65 20 20  fine MEM_Frame  
936c0 20 20 20 30 78 30 30 34 30 20 20 20 2f 2a 20 56     0x0040   /* V
936d0 61 6c 75 65 20 69 73 20 61 20 56 64 62 65 46 72  alue is a VdbeFr
936e0 61 6d 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64  ame object */.#d
936f0 65 66 69 6e 65 20 4d 45 4d 5f 49 6e 76 61 6c 69  efine MEM_Invali
93700 64 20 20 20 30 78 30 30 38 30 20 20 20 2f 2a 20  d   0x0080   /* 
93710 56 61 6c 75 65 20 69 73 20 75 6e 64 65 66 69 6e  Value is undefin
93720 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  ed */.#define ME
93730 4d 5f 43 6c 65 61 72 65 64 20 20 20 30 78 30 31  M_Cleared   0x01
93740 30 30 20 20 20 2f 2a 20 4e 55 4c 4c 20 73 65 74  00   /* NULL set
93750 20 62 79 20 4f 50 5f 4e 75 6c 6c 2c 20 6e 6f 74   by OP_Null, not
93760 20 66 72 6f 6d 20 64 61 74 61 20 2a 2f 0a 23 64   from data */.#d
93770 65 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d 61  efine MEM_TypeMa
93780 73 6b 20 20 30 78 30 31 66 66 20 20 20 2f 2a 20  sk  0x01ff   /* 
93790 4d 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69 74  Mask of type bit
937a0 73 20 2a 2f 0a 0a 0a 2f 2a 20 57 68 65 6e 65 76  s */.../* Whenev
937b0 65 72 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20  er Mem contains 
937c0 61 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f  a valid string o
937d0 72 20 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74  r blob represent
937e0 61 74 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a  ation, one of.**
937f0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
93800 6c 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 74  lags must be set
93810 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
93820 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d  e memory managem
93830 65 6e 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f  ent.** policy fo
93840 72 20 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45  r Mem.z.  The ME
93850 4d 5f 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c  M_Term flag tell
93860 73 20 75 73 20 77 68 65 74 68 65 72 20 6f 72 20  s us whether or 
93870 6e 6f 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e  not the.** strin
93880 67 20 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30  g is \000 or \u0
93890 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a  000 terminated.*
938a0 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65  /.#define MEM_Te
938b0 72 6d 20 20 20 20 20 20 30 78 30 32 30 30 20 20  rm      0x0200  
938c0 20 2f 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69   /* String rep i
938d0 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64  s nul terminated
938e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f   */.#define MEM_
938f0 44 79 6e 20 20 20 20 20 20 20 30 78 30 34 30 30  Dyn       0x0400
93900 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61     /* Need to ca
93910 6c 6c 20 73 71 6c 69 74 65 46 72 65 65 28 29 20  ll sqliteFree() 
93920 6f 6e 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66  on Mem.z */.#def
93930 69 6e 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20  ine MEM_Static  
93940 20 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65    0x0800   /* Me
93950 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  m.z points to a 
93960 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f  static string */
93970 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68  .#define MEM_Eph
93980 65 6d 20 20 20 20 20 30 78 31 30 30 30 20 20 20  em     0x1000   
93990 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20  /* Mem.z points 
939a0 74 6f 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20  to an ephemeral 
939b0 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e  string */.#defin
939c0 65 20 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20  e MEM_Agg       
939d0 30 78 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e  0x2000   /* Mem.
939e0 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61  z points to an a
939f0 67 67 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74  gg function cont
93a00 65 78 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  ext */.#define M
93a10 45 4d 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34  EM_Zero      0x4
93a20 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63  000   /* Mem.i c
93a30 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66  ontains count of
93a40 20 30 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20   0s appended to 
93a50 62 6c 6f 62 20 2a 2f 0a 23 69 66 64 65 66 20 53  blob */.#ifdef S
93a60 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
93a70 4c 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45 4d  LOB.  #undef MEM
93a80 5f 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65 20  _Zero.  #define 
93a90 4d 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30 0a  MEM_Zero 0x0000.
93aa0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6c  #endif../*.** Cl
93ab0 65 61 72 20 61 6e 79 20 65 78 69 73 74 69 6e 67  ear any existing
93ac0 20 74 79 70 65 20 66 6c 61 67 73 20 66 72 6f 6d   type flags from
93ad0 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70 6c 61   a Mem and repla
93ae0 63 65 20 74 68 65 6d 20 77 69 74 68 20 66 0a 2a  ce them with f.*
93af0 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53 65 74  /.#define MemSet
93b00 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29 20 5c  TypeFlag(p, f) \
93b10 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67 73 20  .   ((p)->flags 
93b20 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 7e 28  = ((p)->flags&~(
93b30 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d 45 4d  MEM_TypeMask|MEM
93b40 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 2f 2a 0a 2a  _Zero))|f)../*.*
93b50 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
93b60 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69   a memory cell i
93b70 73 20 6e 6f 74 20 6d 61 72 6b 65 64 20 61 73 20  s not marked as 
93b80 69 6e 76 61 6c 69 64 2e 20 20 54 68 69 73 20 6d  invalid.  This m
93b90 61 63 72 6f 0a 2a 2a 20 69 73 20 66 6f 72 20 75  acro.** is for u
93ba0 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  se inside assert
93bb0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e  () statements on
93bc0 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ly..*/.#ifdef SQ
93bd0 4c 49 54 45 5f 44 45 42 55 47 0a 23 64 65 66 69  LITE_DEBUG.#defi
93be0 6e 65 20 6d 65 6d 49 73 56 61 6c 69 64 28 4d 29  ne memIsValid(M)
93bf0 20 20 28 28 4d 29 2d 3e 66 6c 61 67 73 20 26 20    ((M)->flags & 
93c00 4d 45 4d 5f 49 6e 76 61 6c 69 64 29 3d 3d 30 0a  MEM_Invalid)==0.
93c10 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 41 20 56 64  #endif.../* A Vd
93c20 62 65 46 75 6e 63 20 69 73 20 6a 75 73 74 20 61  beFunc is just a
93c30 20 46 75 6e 63 44 65 66 20 28 64 65 66 69 6e 65   FuncDef (define
93c40 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  d in sqliteInt.h
93c50 29 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a  ) that contains.
93c60 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  ** additional in
93c70 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
93c80 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d  auxiliary inform
93c90 61 74 69 6f 6e 20 62 6f 75 6e 64 20 74 6f 20 61  ation bound to a
93ca0 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 6f 66 20 74  rguments.** of t
93cb0 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  he function.  Th
93cc0 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d  is is used to im
93cd0 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69  plement the sqli
93ce0 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
93cf0 29 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33  ).** and sqlite3
93d00 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41  _set_auxdata() A
93d10 50 49 73 2e 20 20 54 68 65 20 22 61 75 78 64 61  PIs.  The "auxda
93d20 74 61 22 20 69 73 20 73 6f 6d 65 20 61 75 78 69  ta" is some auxi
93d30 6c 69 61 72 79 20 64 61 74 61 0a 2a 2a 20 74 68  liary data.** th
93d40 61 74 20 63 61 6e 20 62 65 20 61 73 73 6f 63 69  at can be associ
93d50 61 74 65 64 20 77 69 74 68 20 61 20 63 6f 6e 73  ated with a cons
93d60 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  tant argument to
93d70 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68   a function.  Th
93d80 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 66 75 6e  is.** allows fun
93d90 63 74 69 6f 6e 73 20 73 75 63 68 20 61 73 20 22  ctions such as "
93da0 72 65 67 65 78 70 22 20 74 6f 20 63 6f 6d 70 69  regexp" to compi
93db0 6c 65 20 74 68 65 69 72 20 63 6f 6e 73 74 61 6e  le their constan
93dc0 74 20 72 65 67 75 6c 61 72 0a 2a 2a 20 65 78 70  t regular.** exp
93dd0 72 65 73 73 69 6f 6e 20 61 72 67 75 6d 65 6e 74  ression argument
93de0 20 6f 6e 63 65 20 61 6e 64 20 72 65 75 73 65 64   once and reused
93df0 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 63 6f   the compiled co
93e00 64 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 0a  de for multiple.
93e10 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a  ** invocations..
93e20 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 46 75  */.struct VdbeFu
93e30 6e 63 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  nc {.  FuncDef *
93e40 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20  pFunc;          
93e50 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 66 69       /* The defi
93e60 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 75  nition of the fu
93e70 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nction */.  int 
93e80 6e 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20  nAux;           
93e90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
93ea0 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61  ber of entries a
93eb0 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 70 41  llocated for apA
93ec0 75 78 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74  ux[] */.  struct
93ed0 20 41 75 78 44 61 74 61 20 7b 0a 20 20 20 20 76   AuxData {.    v
93ee0 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20  oid *pAux;      
93ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
93f00 41 75 78 20 64 61 74 61 20 66 6f 72 20 74 68 65  Aux data for the
93f10 20 69 2d 74 68 20 61 72 67 75 6d 65 6e 74 20 2a   i-th argument *
93f20 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 44 65  /.    void (*xDe
93f30 6c 65 74 65 29 28 76 6f 69 64 20 2a 29 3b 20 20  lete)(void *);  
93f40 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f      /* Destructo
93f50 72 20 66 6f 72 20 74 68 65 20 61 75 78 20 64 61  r for the aux da
93f60 74 61 20 2a 2f 0a 20 20 7d 20 61 70 41 75 78 5b  ta */.  } apAux[
93f70 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  1];             
93f80 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f        /* One slo
93f90 74 20 66 6f 72 20 65 61 63 68 20 66 75 6e 63 74  t for each funct
93fa0 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a  ion argument */.
93fb0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 63  };../*.** The "c
93fc0 6f 6e 74 65 78 74 22 20 61 72 67 75 6d 65 6e 74  ontext" argument
93fd0 20 66 6f 72 20 61 20 69 6e 73 74 61 6c 6c 61 62   for a installab
93fe0 6c 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20  le function.  A 
93ff0 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a  pointer to an.**
94000 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
94010 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 74  s structure is t
94020 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
94030 74 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65  t to the routine
94040 73 20 75 73 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d  s used.** implem
94050 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63  ent the SQL func
94060 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tions..**.** The
94070 72 65 20 69 73 20 61 20 74 79 70 65 64 65 66 20  re is a typedef 
94080 66 6f 72 20 74 68 69 73 20 73 74 72 75 63 74 75  for this structu
94090 72 65 20 69 6e 20 73 71 6c 69 74 65 2e 68 2e 20  re in sqlite.h. 
940a0 20 53 6f 20 61 6c 6c 20 72 6f 75 74 69 6e 65 73   So all routines
940b0 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 65 20 70 75  ,.** even the pu
940c0 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74  blic interface t
940d0 6f 20 53 51 4c 69 74 65 2c 20 63 61 6e 20 75 73  o SQLite, can us
940e0 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  e a pointer to t
940f0 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  his structure..*
94100 2a 20 42 75 74 20 74 68 69 73 20 66 69 6c 65 20  * But this file 
94110 69 73 20 74 68 65 20 6f 6e 6c 79 20 70 6c 61 63  is the only plac
94120 65 20 77 68 65 72 65 20 74 68 65 20 69 6e 74 65  e where the inte
94130 72 6e 61 6c 20 64 65 74 61 69 6c 73 20 6f 66 20  rnal details of 
94140 74 68 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72  this.** structur
94150 65 20 61 72 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a  e are known..**.
94160 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
94170 65 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73  e is defined ins
94180 69 64 65 20 6f 66 20 76 64 62 65 49 6e 74 2e 68  ide of vdbeInt.h
94190 20 62 65 63 61 75 73 65 20 69 74 20 75 73 65 73   because it uses
941a0 20 73 75 62 73 74 72 75 63 74 75 72 65 73 0a 2a   substructures.*
941b0 2a 20 28 4d 65 6d 29 20 77 68 69 63 68 20 61 72  * (Mem) which ar
941c0 65 20 6f 6e 6c 79 20 64 65 66 69 6e 65 64 20 74  e only defined t
941d0 68 65 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  here..*/.struct 
941e0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
941f0 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75  {.  FuncDef *pFu
94200 6e 63 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  nc;       /* Poi
94210 6e 74 65 72 20 74 6f 20 66 75 6e 63 74 69 6f 6e  nter to function
94220 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d   information.  M
94230 55 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a  UST BE FIRST */.
94240 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62    VdbeFunc *pVdb
94250 65 46 75 6e 63 3b 20 20 2f 2a 20 41 75 78 69 6c  eFunc;  /* Auxil
94260 61 72 79 20 64 61 74 61 2c 20 69 66 20 63 72 65  ary data, if cre
94270 61 74 65 64 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73  ated. */.  Mem s
94280 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
94290 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76   /* The return v
942a0 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 68  alue is stored h
942b0 65 72 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d  ere */.  Mem *pM
942c0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  em;            /
942d0 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73  * Memory cell us
942e0 65 64 20 74 6f 20 73 74 6f 72 65 20 61 67 67 72  ed to store aggr
942f0 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20 2a 2f  egate context */
94300 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
94310 6c 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c  l;       /* Coll
94320 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a  ating sequence *
94330 2f 0a 20 20 69 6e 74 20 69 73 45 72 72 6f 72 3b  /.  int isError;
94340 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72            /* Err
94350 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64  or code returned
94360 20 62 79 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   by the function
94370 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 46  . */.  int skipF
94380 6c 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  lag;         /* 
94390 53 6b 69 70 20 73 6b 69 70 20 61 63 63 75 6d 75  Skip skip accumu
943a0 6c 61 74 6f 72 20 6c 6f 61 64 69 6e 67 20 69 66  lator loading if
943b0 20 74 72 75 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   true */.};../*.
943c0 2a 2a 20 41 6e 20 45 78 70 6c 61 69 6e 20 6f 62  ** An Explain ob
943d0 6a 65 63 74 20 61 63 63 75 6d 75 6c 61 74 65 73  ject accumulates
943e0 20 69 6e 64 65 6e 74 65 64 20 6f 75 74 70 75 74   indented output
943f0 20 77 68 69 63 68 20 69 73 20 68 65 6c 70 66 75   which is helpfu
94400 6c 0a 2a 2a 20 69 6e 20 64 65 73 63 72 69 62 69  l.** in describi
94410 6e 67 20 72 65 63 75 72 73 69 76 65 20 64 61 74  ng recursive dat
94420 61 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  a structures..*/
94430 0a 73 74 72 75 63 74 20 45 78 70 6c 61 69 6e 20  .struct Explain 
94440 7b 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b  {.  Vdbe *pVdbe;
94450 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63 68         /* Attach
94460 20 74 68 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e   the explanation
94470 20 74 6f 20 74 68 69 73 20 56 64 62 65 20 2a 2f   to this Vdbe */
94480 0a 20 20 53 74 72 41 63 63 75 6d 20 73 74 72 3b  .  StrAccum str;
94490 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72        /* The str
944a0 69 6e 67 20 62 65 69 6e 67 20 61 63 63 75 6d 75  ing being accumu
944b0 6c 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  lated */.  int n
944c0 49 6e 64 65 6e 74 3b 20 20 20 20 20 20 20 2f 2a  Indent;       /*
944d0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
944e0 6e 74 73 20 69 6e 20 61 49 6e 64 65 6e 74 20 2a  nts in aIndent *
944f0 2f 0a 20 20 75 31 36 20 61 49 6e 64 65 6e 74 5b  /.  u16 aIndent[
94500 31 30 30 5d 3b 20 20 2f 2a 20 4c 65 76 65 6c 73  100];  /* Levels
94510 20 6f 66 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20   of indentation 
94520 2a 2f 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b  */.  char zBase[
94530 31 30 30 5d 3b 20 20 20 2f 2a 20 49 6e 69 74 69  100];   /* Initi
94540 61 6c 20 73 70 61 63 65 20 2a 2f 0a 7d 3b 0a 0a  al space */.};..
94550 2f 2a 20 41 20 62 69 74 66 69 65 6c 64 20 74 79  /* A bitfield ty
94560 70 65 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64  pe for use insid
94570 65 20 6f 66 20 73 74 72 75 63 74 75 72 65 73 2e  e of structures.
94580 20 20 41 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 20    Always follow 
94590 77 69 74 68 20 3a 4e 20 77 68 65 72 65 0a 2a 2a  with :N where.**
945a0 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72   N is the number
945b0 20 6f 66 20 62 69 74 73 2e 0a 2a 2f 0a 74 79 70   of bits..*/.typ
945c0 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 62 66  edef unsigned bf
945d0 74 3b 20 20 2f 2a 20 42 69 74 20 46 69 65 6c 64  t;  /* Bit Field
945e0 20 54 79 70 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20   Type */../*.** 
945f0 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
94600 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  he virtual machi
94610 6e 65 2e 20 20 54 68 69 73 20 73 74 72 75 63 74  ne.  This struct
94620 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ure contains the
94630 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73 74 61   complete.** sta
94640 74 65 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  te of the virtua
94650 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a  l machine..**.**
94660 20 54 68 65 20 22 73 71 6c 69 74 65 33 5f 73 74   The "sqlite3_st
94670 6d 74 22 20 73 74 72 75 63 74 75 72 65 20 70 6f  mt" structure po
94680 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 72 65  inter that is re
94690 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
946a0 33 5f 70 72 65 70 61 72 65 28 29 0a 2a 2a 20 69  3_prepare().** i
946b0 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74  s really a point
946c0 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63  er to an instanc
946d0 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
946e0 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56  ure..**.** The V
946f0 64 62 65 2e 69 6e 56 74 61 62 4d 65 74 68 6f 64  dbe.inVtabMethod
94700 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74   variable is set
94710 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72   to non-zero for
94720 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66   the duration of
94730 0a 2a 2a 20 61 6e 79 20 76 69 72 74 75 61 6c 20  .** any virtual 
94740 74 61 62 6c 65 20 6d 65 74 68 6f 64 20 69 6e 76  table method inv
94750 6f 63 61 74 69 6f 6e 73 20 6d 61 64 65 20 62 79  ocations made by
94760 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61   the vdbe progra
94770 6d 2e 20 49 74 20 69 73 0a 2a 2a 20 73 65 74 20  m. It is.** set 
94780 74 6f 20 32 20 66 6f 72 20 78 44 65 73 74 72 6f  to 2 for xDestro
94790 79 20 6d 65 74 68 6f 64 20 63 61 6c 6c 73 20 61  y method calls a
947a0 6e 64 20 31 20 66 6f 72 20 61 6c 6c 20 6f 74 68  nd 1 for all oth
947b0 65 72 20 6d 65 74 68 6f 64 73 2e 20 54 68 69 73  er methods. This
947c0 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 73 20  .** variable is 
947d0 75 73 65 64 20 66 6f 72 20 74 77 6f 20 70 75 72  used for two pur
947e0 70 6f 73 65 73 3a 20 74 6f 20 61 6c 6c 6f 77 20  poses: to allow 
947f0 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 73  xDestroy methods
94800 20 74 6f 20 65 78 65 63 75 74 65 0a 2a 2a 20 22   to execute.** "
94810 44 52 4f 50 20 54 41 42 4c 45 22 20 73 74 61 74  DROP TABLE" stat
94820 65 6d 65 6e 74 73 20 61 6e 64 20 74 6f 20 70 72  ements and to pr
94830 65 76 65 6e 74 20 73 6f 6d 65 20 6e 61 73 74 79  event some nasty
94840 20 73 69 64 65 20 65 66 66 65 63 74 73 20 6f 66   side effects of
94850 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  .** malloc failu
94860 72 65 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69  re when SQLite i
94870 73 20 69 6e 76 6f 6b 65 64 20 72 65 63 75 72 73  s invoked recurs
94880 69 76 65 6c 79 20 62 79 20 61 20 76 69 72 74 75  ively by a virtu
94890 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 6d 65 74  al table .** met
948a0 68 6f 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  hod function..*/
948b0 0a 73 74 72 75 63 74 20 56 64 62 65 20 7b 0a 20  .struct Vdbe {. 
948c0 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
948d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
948e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
948f0 69 6f 6e 20 74 68 61 74 20 6f 77 6e 73 20 74 68  ion that owns th
94900 69 73 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  is statement */.
94910 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20    Op *aOp;      
94920 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61            /* Spa
94930 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 76  ce to hold the v
94940 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 27 73  irtual machine's
94950 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 4d 65   program */.  Me
94960 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20  m *aMem;        
94970 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 65 6d        /* The mem
94980 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f  ory locations */
94990 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 20  .  Mem **apArg; 
949a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
949b0 67 75 6d 65 6e 74 73 20 74 6f 20 63 75 72 72 65  guments to curre
949c0 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 75  ntly executing u
949d0 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ser function */.
949e0 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e 61 6d 65 3b    Mem *aColName;
949f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
94a00 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72 65 74  umn names to ret
94a10 75 72 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52  urn */.  Mem *pR
94a20 65 73 75 6c 74 53 65 74 3b 20 20 20 20 20 20 20  esultSet;       
94a30 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61   /* Pointer to a
94a40 6e 20 61 72 72 61 79 20 6f 66 20 72 65 73 75 6c  n array of resul
94a50 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d  ts */.  int nMem
94a60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
94a70 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d  /* Number of mem
94a80 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 63 75  ory locations cu
94a90 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65  rrently allocate
94aa0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b 20  d */.  int nOp; 
94ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
94ac0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74  * Number of inst
94ad0 72 75 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20  ructions in the 
94ae0 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74  program */.  int
94af0 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20   nOpAlloc;      
94b00 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
94b10 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65  f slots allocate
94b20 64 20 66 6f 72 20 61 4f 70 5b 5d 20 2a 2f 0a 20  d for aOp[] */. 
94b30 20 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20 20   int nLabel;    
94b40 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
94b50 65 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65  er of labels use
94b60 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c 61 62  d */.  int *aLab
94b70 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  el;            /
94b80 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  * Space to hold 
94b90 74 68 65 20 6c 61 62 65 6c 73 20 2a 2f 0a 20 20  the labels */.  
94ba0 75 31 36 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b 20  u16 nResColumn; 
94bb0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
94bc0 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
94bd0 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72  one row of the r
94be0 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 75  esult set */.  u
94bf0 31 36 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20  16 nCursor;     
94c00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
94c10 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 43   of slots in apC
94c20 73 72 5b 5d 20 2a 2f 0a 20 20 75 33 32 20 6d 61  sr[] */.  u32 ma
94c30 67 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  gic;            
94c40 20 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65    /* Magic numbe
94c50 72 20 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65  r for sanity che
94c60 63 6b 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20  cking */.  char 
94c70 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20  *zErrMsg;       
94c80 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73     /* Error mess
94c90 61 67 65 20 77 72 69 74 74 65 6e 20 68 65 72 65  age written here
94ca0 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 50 72 65   */.  Vdbe *pPre
94cb0 76 2c 2a 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a  v,*pNext;     /*
94cc0 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   Linked list of 
94cd0 56 44 42 45 73 20 77 69 74 68 20 74 68 65 20 73  VDBEs with the s
94ce0 61 6d 65 20 56 64 62 65 2e 64 62 20 2a 2f 0a 20  ame Vdbe.db */. 
94cf0 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70   VdbeCursor **ap
94d00 43 73 72 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20  Csr;     /* One 
94d10 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 69 73 20  element of this 
94d20 61 72 72 61 79 20 66 6f 72 20 65 61 63 68 20 6f  array for each o
94d30 70 65 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  pen cursor */.  
94d40 4d 65 6d 20 2a 61 56 61 72 3b 20 20 20 20 20 20  Mem *aVar;      
94d50 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
94d60 73 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72  s for the OP_Var
94d70 69 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f  iable opcode. */
94d80 0a 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b  .  char **azVar;
94d90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
94da0 6d 65 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20  me of variables 
94db0 2a 2f 0a 20 20 79 6e 56 61 72 20 6e 56 61 72 3b  */.  ynVar nVar;
94dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
94dd0 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
94de0 73 20 69 6e 20 61 56 61 72 5b 5d 20 2a 2f 0a 20  s in aVar[] */. 
94df0 20 79 6e 56 61 72 20 6e 7a 56 61 72 3b 20 20 20   ynVar nzVar;   
94e00 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
94e10 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
94e20 20 61 7a 56 61 72 5b 5d 20 2a 2f 0a 20 20 75 33   azVar[] */.  u3
94e30 32 20 63 61 63 68 65 43 74 72 3b 20 20 20 20 20  2 cacheCtr;     
94e40 20 20 20 20 20 20 2f 2a 20 56 64 62 65 43 75 72        /* VdbeCur
94e50 73 6f 72 20 72 6f 77 20 63 61 63 68 65 20 67 65  sor row cache ge
94e60 6e 65 72 61 74 69 6f 6e 20 63 6f 75 6e 74 65 72  neration counter
94e70 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20   */.  int pc;   
94e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
94e90 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75   The program cou
94ea0 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63  nter */.  int rc
94eb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
94ec0 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65    /* Value to re
94ed0 74 75 72 6e 20 2a 2f 0a 20 20 75 38 20 65 72 72  turn */.  u8 err
94ee0 6f 72 41 63 74 69 6f 6e 3b 20 20 20 20 20 20 20  orAction;       
94ef0 20 20 2f 2a 20 52 65 63 6f 76 65 72 79 20 61 63    /* Recovery ac
94f00 74 69 6f 6e 20 74 6f 20 64 6f 20 69 6e 20 63 61  tion to do in ca
94f10 73 65 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 2a  se of an error *
94f20 2f 0a 20 20 75 38 20 6d 69 6e 57 72 69 74 65 46  /.  u8 minWriteF
94f30 69 6c 65 46 6f 72 6d 61 74 3b 20 20 2f 2a 20 4d  ileFormat;  /* M
94f40 69 6e 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d  inimum file form
94f50 61 74 20 66 6f 72 20 77 72 69 74 61 62 6c 65 20  at for writable 
94f60 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 2a  database files *
94f70 2f 0a 20 20 62 66 74 20 65 78 70 6c 61 69 6e 3a  /.  bft explain:
94f80 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  2;          /* T
94f90 72 75 65 20 69 66 20 45 58 50 4c 41 49 4e 20 70  rue if EXPLAIN p
94fa0 72 65 73 65 6e 74 20 6f 6e 20 53 51 4c 20 63 6f  resent on SQL co
94fb0 6d 6d 61 6e 64 20 2a 2f 0a 20 20 62 66 74 20 69  mmand */.  bft i
94fc0 6e 56 74 61 62 4d 65 74 68 6f 64 3a 32 3b 20 20  nVtabMethod:2;  
94fd0 20 20 20 2f 2a 20 53 65 65 20 63 6f 6d 6d 65 6e     /* See commen
94fe0 74 73 20 61 62 6f 76 65 20 2a 2f 0a 20 20 62 66  ts above */.  bf
94ff0 74 20 63 68 61 6e 67 65 43 6e 74 4f 6e 3a 31 3b  t changeCntOn:1;
95000 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
95010 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e   update the chan
95020 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  ge-counter */.  
95030 62 66 74 20 65 78 70 69 72 65 64 3a 31 3b 20 20  bft expired:1;  
95040 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
95050 69 66 20 74 68 65 20 56 4d 20 6e 65 65 64 73 20  if the VM needs 
95060 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64  to be recompiled
95070 20 2a 2f 0a 20 20 62 66 74 20 72 75 6e 4f 6e 6c   */.  bft runOnl
95080 79 4f 6e 63 65 3a 31 3b 20 20 20 20 20 20 2f 2a  yOnce:1;      /*
95090 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 65   Automatically e
950a0 78 70 69 72 65 20 6f 6e 20 72 65 73 65 74 20 2a  xpire on reset *
950b0 2f 0a 20 20 62 66 74 20 75 73 65 73 53 74 6d 74  /.  bft usesStmt
950c0 4a 6f 75 72 6e 61 6c 3a 31 3b 20 20 2f 2a 20 54  Journal:1;  /* T
950d0 72 75 65 20 69 66 20 75 73 65 73 20 61 20 73 74  rue if uses a st
950e0 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
950f0 2a 2f 0a 20 20 62 66 74 20 72 65 61 64 4f 6e 6c  */.  bft readOnl
95100 79 3a 31 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  y:1;         /* 
95110 54 72 75 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e  True for read-on
95120 6c 79 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f  ly statements */
95130 0a 20 20 62 66 74 20 69 73 50 72 65 70 61 72 65  .  bft isPrepare
95140 56 32 3a 31 3b 20 20 20 20 20 20 2f 2a 20 54 72  V2:1;      /* Tr
95150 75 65 20 69 66 20 70 72 65 70 61 72 65 64 20 77  ue if prepared w
95160 69 74 68 20 70 72 65 70 61 72 65 5f 76 32 28 29  ith prepare_v2()
95170 20 2a 2f 0a 20 20 62 66 74 20 64 6f 69 6e 67 52   */.  bft doingR
95180 65 72 75 6e 3a 31 3b 20 20 20 20 20 20 20 2f 2a  erun:1;       /*
95190 20 54 72 75 65 20 69 66 20 72 65 72 75 6e 6e 69   True if rerunni
951a0 6e 67 20 61 66 74 65 72 20 61 6e 20 61 75 74 6f  ng after an auto
951b0 2d 72 65 70 72 65 70 61 72 65 20 2a 2f 0a 20 20  -reprepare */.  
951c0 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20  int nChange;    
951d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
951e0 72 20 6f 66 20 64 62 20 63 68 61 6e 67 65 73 20  r of db changes 
951f0 6d 61 64 65 20 73 69 6e 63 65 20 6c 61 73 74 20  made since last 
95200 72 65 73 65 74 20 2a 2f 0a 20 20 79 44 62 4d 61  reset */.  yDbMa
95210 73 6b 20 62 74 72 65 65 4d 61 73 6b 3b 20 20 20  sk btreeMask;   
95220 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66     /* Bitmask of
95230 20 64 62 2d 3e 61 44 62 5b 5d 20 65 6e 74 72 69   db->aDb[] entri
95240 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f  es referenced */
95250 0a 20 20 79 44 62 4d 61 73 6b 20 6c 6f 63 6b 4d  .  yDbMask lockM
95260 61 73 6b 3b 20 20 20 20 20 20 20 2f 2a 20 53 75  ask;       /* Su
95270 62 73 65 74 20 6f 66 20 62 74 72 65 65 4d 61 73  bset of btreeMas
95280 6b 20 74 68 61 74 20 72 65 71 75 69 72 65 73 20  k that requires 
95290 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20  a lock */.  int 
952a0 69 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 20  iStatement;     
952b0 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74      /* Statement
952c0 20 6e 75 6d 62 65 72 20 28 6f 72 20 30 20 69 66   number (or 0 if
952d0 20 68 61 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20   has not opened 
952e0 73 74 6d 74 29 20 2a 2f 0a 20 20 69 6e 74 20 61  stmt) */.  int a
952f0 43 6f 75 6e 74 65 72 5b 33 5d 3b 20 20 20 20 20  Counter[3];     
95300 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 73 20 75     /* Counters u
95310 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 73  sed by sqlite3_s
95320 74 6d 74 5f 73 74 61 74 75 73 28 29 20 2a 2f 0a  tmt_status() */.
95330 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
95340 4d 49 54 5f 54 52 41 43 45 0a 20 20 69 36 34 20  MIT_TRACE.  i64 
95350 73 74 61 72 74 54 69 6d 65 3b 20 20 20 20 20 20  startTime;      
95360 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68 65 6e      /* Time when
95370 20 71 75 65 72 79 20 73 74 61 72 74 65 64 20 2d   query started -
95380 20 75 73 65 64 20 66 6f 72 20 70 72 6f 66 69 6c   used for profil
95390 69 6e 67 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  ing */.#endif.  
953a0 69 36 34 20 6e 46 6b 43 6f 6e 73 74 72 61 69 6e  i64 nFkConstrain
953b0 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  t;      /* Numbe
953c0 72 20 6f 66 20 69 6d 6d 2e 20 46 4b 20 63 6f 6e  r of imm. FK con
953d0 73 74 72 61 69 6e 74 73 20 74 68 69 73 20 56 4d  straints this VM
953e0 20 2a 2f 0a 20 20 69 36 34 20 6e 53 74 6d 74 44   */.  i64 nStmtD
953f0 65 66 43 6f 6e 73 3b 20 20 20 20 20 20 20 2f 2a  efCons;       /*
95400 20 4e 75 6d 62 65 72 20 6f 66 20 64 65 66 2e 20   Number of def. 
95410 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 68 65 6e  constraints when
95420 20 73 74 6d 74 20 73 74 61 72 74 65 64 20 2a 2f   stmt started */
95430 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20  .  char *zSql;  
95440 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
95450 78 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74  xt of the SQL st
95460 61 74 65 6d 65 6e 74 20 74 68 61 74 20 67 65 6e  atement that gen
95470 65 72 61 74 65 64 20 74 68 69 73 20 2a 2f 0a 20  erated this */. 
95480 20 76 6f 69 64 20 2a 70 46 72 65 65 3b 20 20 20   void *pFree;   
95490 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65           /* Free
954a0 20 74 68 69 73 20 77 68 65 6e 20 64 65 6c 65 74   this when delet
954b0 69 6e 67 20 74 68 65 20 76 64 62 65 20 2a 2f 0a  ing the vdbe */.
954c0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
954d0 42 55 47 0a 20 20 46 49 4c 45 20 2a 74 72 61 63  BUG.  FILE *trac
954e0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
954f0 20 57 72 69 74 65 20 61 6e 20 65 78 65 63 75 74   Write an execut
95500 69 6f 6e 20 74 72 61 63 65 20 68 65 72 65 2c 20  ion trace here, 
95510 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 23  if not NULL */.#
95520 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
95530 49 54 45 5f 45 4e 41 42 4c 45 5f 54 52 45 45 5f  ITE_ENABLE_TREE_
95540 45 58 50 4c 41 49 4e 0a 20 20 45 78 70 6c 61 69  EXPLAIN.  Explai
95550 6e 20 2a 70 45 78 70 6c 61 69 6e 3b 20 20 20 20  n *pExplain;    
95560 20 20 2f 2a 20 54 68 65 20 65 78 70 6c 61 69 6e    /* The explain
95570 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45  er */.  char *zE
95580 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 20 20 20  xplain;         
95590 2f 2a 20 45 78 70 6c 61 6e 61 74 69 6f 6e 20 6f  /* Explanation o
955a0 66 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  f data structure
955b0 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 56 64  s */.#endif.  Vd
955c0 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b  beFrame *pFrame;
955d0 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20        /* Parent 
955e0 66 72 61 6d 65 20 2a 2f 0a 20 20 56 64 62 65 46  frame */.  VdbeF
955f0 72 61 6d 65 20 2a 70 44 65 6c 46 72 61 6d 65 3b  rame *pDelFrame;
95600 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 72     /* List of fr
95610 61 6d 65 20 6f 62 6a 65 63 74 73 20 74 6f 20 66  ame objects to f
95620 72 65 65 20 6f 6e 20 56 4d 20 72 65 73 65 74 20  ree on VM reset 
95630 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 61 6d 65 3b  */.  int nFrame;
95640 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
95650 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73  Number of frames
95660 20 69 6e 20 70 46 72 61 6d 65 20 6c 69 73 74 20   in pFrame list 
95670 2a 2f 0a 20 20 75 33 32 20 65 78 70 6d 61 73 6b  */.  u32 expmask
95680 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
95690 42 69 6e 64 69 6e 67 20 74 6f 20 74 68 65 73 65  Binding to these
956a0 20 76 61 72 73 20 69 6e 76 61 6c 69 64 61 74 65   vars invalidate
956b0 73 20 56 4d 20 2a 2f 0a 20 20 53 75 62 50 72 6f  s VM */.  SubPro
956c0 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20  gram *pProgram; 
956d0 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74    /* Linked list
956e0 20 6f 66 20 61 6c 6c 20 73 75 62 2d 70 72 6f 67   of all sub-prog
956f0 72 61 6d 73 20 75 73 65 64 20 62 79 20 56 4d 20  rams used by VM 
95700 2a 2f 0a 20 20 69 6e 74 20 6e 4f 6e 63 65 46 6c  */.  int nOnceFl
95710 61 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ag;          /* 
95720 53 69 7a 65 20 6f 66 20 61 72 72 61 79 20 61 4f  Size of array aO
95730 6e 63 65 46 6c 61 67 5b 5d 20 2a 2f 0a 20 20 75  nceFlag[] */.  u
95740 38 20 2a 61 4f 6e 63 65 46 6c 61 67 3b 20 20 20  8 *aOnceFlag;   
95750 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
95760 66 6f 72 20 4f 50 5f 4f 6e 63 65 20 2a 2f 0a 7d  for OP_Once */.}
95770 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  ;../*.** The fol
95780 6c 6f 77 69 6e 67 20 61 72 65 20 61 6c 6c 6f 77  lowing are allow
95790 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 56 64  ed values for Vd
957a0 62 65 2e 6d 61 67 69 63 0a 2a 2f 0a 23 64 65 66  be.magic.*/.#def
957b0 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 49  ine VDBE_MAGIC_I
957c0 4e 49 54 20 20 20 20 20 30 78 32 36 62 63 65 61  NIT     0x26bcea
957d0 61 35 20 20 20 20 2f 2a 20 42 75 69 6c 64 69 6e  a5    /* Buildin
957e0 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d  g a VDBE program
957f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45   */.#define VDBE
95800 5f 4d 41 47 49 43 5f 52 55 4e 20 20 20 20 20 20  _MAGIC_RUN      
95810 30 78 62 64 66 32 30 64 61 33 20 20 20 20 2f 2a  0xbdf20da3    /*
95820 20 56 44 42 45 20 69 73 20 72 65 61 64 79 20 74   VDBE is ready t
95830 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 23 64 65  o execute */.#de
95840 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f  fine VDBE_MAGIC_
95850 48 41 4c 54 20 20 20 20 20 30 78 35 31 39 63 32  HALT     0x519c2
95860 39 37 33 20 20 20 20 2f 2a 20 56 44 42 45 20 68  973    /* VDBE h
95870 61 73 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65  as completed exe
95880 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  cution */.#defin
95890 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45 41  e VDBE_MAGIC_DEA
958a0 44 20 20 20 20 20 30 78 62 36 30 36 63 33 63 38  D     0xb606c3c8
958b0 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20      /* The VDBE 
958c0 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63  has been dealloc
958d0 61 74 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46  ated */../*.** F
958e0 75 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70  unction prototyp
958f0 65 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  es.*/.SQLITE_PRI
95900 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
95910 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28  3VdbeFreeCursor(
95920 56 64 62 65 20 2a 2c 20 56 64 62 65 43 75 72 73  Vdbe *, VdbeCurs
95930 6f 72 2a 29 3b 0a 76 6f 69 64 20 73 71 6c 69 74  or*);.void sqlit
95940 65 56 64 62 65 50 6f 70 53 74 61 63 6b 28 56 64  eVdbePopStack(Vd
95950 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  be*,int);.SQLITE
95960 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
95970 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f  ite3VdbeCursorMo
95980 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 2a  veto(VdbeCursor*
95990 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  );.#if defined(S
959a0 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c 20  QLITE_DEBUG) || 
959b0 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f  defined(VDBE_PRO
959c0 46 49 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 49  FILE).SQLITE_PRI
959d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
959e0 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c  3VdbePrintOp(FIL
959f0 45 2a 2c 20 69 6e 74 2c 20 4f 70 2a 29 3b 0a 23  E*, int, Op*);.#
95a00 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49  endif.SQLITE_PRI
95a10 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
95a20 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
95a30 6e 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50  n(u32);.SQLITE_P
95a40 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74  RIVATE u32 sqlit
95a50 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
95a60 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  (Mem*, int);.SQL
95a70 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
95a80 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
95a90 6c 50 75 74 28 75 6e 73 69 67 6e 65 64 20 63 68  lPut(unsigned ch
95aa0 61 72 2a 2c 20 69 6e 74 2c 20 4d 65 6d 2a 2c 20  ar*, int, Mem*, 
95ab0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
95ac0 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
95ad0 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 63 6f  VdbeSerialGet(co
95ae0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
95af0 72 2a 2c 20 75 33 32 2c 20 4d 65 6d 2a 29 3b 0a  r*, u32, Mem*);.
95b00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
95b10 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44  oid sqlite3VdbeD
95b20 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 64 62  eleteAuxData(Vdb
95b30 65 46 75 6e 63 2a 2c 20 69 6e 74 29 3b 0a 0a 69  eFunc*, int);..i
95b40 6e 74 20 73 71 6c 69 74 65 32 42 74 72 65 65 4b  nt sqlite2BtreeK
95b50 65 79 43 6f 6d 70 61 72 65 28 42 74 43 75 72 73  eyCompare(BtCurs
95b60 6f 72 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  or *, const void
95b70 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e   *, int, int, in
95b80 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  t *);.SQLITE_PRI
95b90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
95ba0 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72  VdbeIdxKeyCompar
95bb0 65 28 56 64 62 65 43 75 72 73 6f 72 2a 2c 55 6e  e(VdbeCursor*,Un
95bc0 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 2c 69 6e  packedRecord*,in
95bd0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
95be0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
95bf0 64 62 65 49 64 78 52 6f 77 69 64 28 73 71 6c 69  dbeIdxRowid(sqli
95c00 74 65 33 2a 2c 20 42 74 43 75 72 73 6f 72 20 2a  te3*, BtCursor *
95c10 2c 20 69 36 34 20 2a 29 3b 0a 53 51 4c 49 54 45  , i64 *);.SQLITE
95c20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
95c30 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63  ite3MemCompare(c
95c40 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74  onst Mem*, const
95c50 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 43 6f 6c   Mem*, const Col
95c60 6c 53 65 71 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  lSeq*);.SQLITE_P
95c70 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
95c80 65 33 56 64 62 65 45 78 65 63 28 56 64 62 65 2a  e3VdbeExec(Vdbe*
95c90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
95ca0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
95cb0 65 4c 69 73 74 28 56 64 62 65 2a 29 3b 0a 53 51  eList(Vdbe*);.SQ
95cc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
95cd0 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74   sqlite3VdbeHalt
95ce0 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
95cf0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
95d00 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
95d10 6f 64 69 6e 67 28 4d 65 6d 20 2a 2c 20 69 6e 74  oding(Mem *, int
95d20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
95d30 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
95d40 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 2a 29  eMemTooBig(Mem*)
95d50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
95d60 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
95d70 4d 65 6d 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f  MemCopy(Mem*, co
95d80 6e 73 74 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  nst Mem*);.SQLIT
95d90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
95da0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
95db0 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 2a 2c 20 63  llowCopy(Mem*, c
95dc0 6f 6e 73 74 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b  onst Mem*, int);
95dd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
95de0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
95df0 4d 65 6d 4d 6f 76 65 28 4d 65 6d 2a 2c 20 4d 65  MemMove(Mem*, Me
95e00 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
95e10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
95e20 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61  dbeMemNulTermina
95e30 74 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  te(Mem*);.SQLITE
95e40 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
95e50 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
95e60 72 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 63 68  r(Mem*, const ch
95e70 61 72 2a 2c 20 69 6e 74 2c 20 75 38 2c 20 76 6f  ar*, int, u8, vo
95e80 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53  id(*)(void*));.S
95e90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
95ea0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
95eb0 6d 53 65 74 49 6e 74 36 34 28 4d 65 6d 2a 2c 20  mSetInt64(Mem*, 
95ec0 69 36 34 29 3b 0a 23 69 66 64 65 66 20 53 51 4c  i64);.#ifdef SQL
95ed0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
95ee0 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65  G_POINT.# define
95ef0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
95f00 65 74 44 6f 75 62 6c 65 20 73 71 6c 69 74 65 33  etDouble sqlite3
95f10 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 0a  VdbeMemSetInt64.
95f20 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49  #else.SQLITE_PRI
95f30 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
95f40 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75  te3VdbeMemSetDou
95f50 62 6c 65 28 4d 65 6d 2a 2c 20 64 6f 75 62 6c 65  ble(Mem*, double
95f60 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45  );.#endif.SQLITE
95f70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
95f80 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e  lite3VdbeMemSetN
95f90 75 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  ull(Mem*);.SQLIT
95fa0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
95fb0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
95fc0 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e  ZeroBlob(Mem*,in
95fd0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
95fe0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
95ff0 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28  dbeMemSetRowSet(
96000 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
96010 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
96020 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
96030 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c  eable(Mem*);.SQL
96040 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
96050 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74  sqlite3VdbeMemSt
96060 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c 20 69 6e  ringify(Mem*, in
96070 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
96080 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64  TE i64 sqlite3Vd
96090 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 2a 29  beIntValue(Mem*)
960a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
960b0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
960c0 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 4d 65  MemIntegerify(Me
960d0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
960e0 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  ATE double sqlit
960f0 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28  e3VdbeRealValue(
96100 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
96110 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
96120 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 66  e3VdbeIntegerAff
96130 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c  inity(Mem*);.SQL
96140 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
96150 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
96160 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c  alify(Mem*);.SQL
96170 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
96180 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75  sqlite3VdbeMemNu
96190 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51  merify(Mem*);.SQ
961a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
961b0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
961c0 72 6f 6d 42 74 72 65 65 28 42 74 43 75 72 73 6f  romBtree(BtCurso
961d0 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d  r*,int,int,int,M
961e0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
961f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
96200 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
96210 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f  Mem *p);.SQLITE_
96220 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
96230 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
96240 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a  seExternal(Mem *
96250 70 29 3b 0a 23 64 65 66 69 6e 65 20 56 64 62 65  p);.#define Vdbe
96260 4d 65 6d 52 65 6c 65 61 73 65 28 58 29 20 20 5c  MemRelease(X)  \
96270 0a 20 20 69 66 28 28 58 29 2d 3e 66 6c 61 67 73  .  if((X)->flags
96280 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79  &(MEM_Agg|MEM_Dy
96290 6e 7c 4d 45 4d 5f 52 6f 77 53 65 74 7c 4d 45 4d  n|MEM_RowSet|MEM
962a0 5f 46 72 61 6d 65 29 29 20 5c 0a 20 20 20 20 73  _Frame)) \.    s
962b0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
962c0 65 61 73 65 45 78 74 65 72 6e 61 6c 28 58 29 3b  easeExternal(X);
962d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
962e0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
962f0 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 6d 2a 2c  emFinalize(Mem*,
96300 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49   FuncDef*);.SQLI
96310 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
96320 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4f 70   char *sqlite3Op
96330 63 6f 64 65 4e 61 6d 65 28 69 6e 74 29 3b 0a 53  codeName(int);.S
96340 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
96350 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
96360 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  Grow(Mem *pMem, 
96370 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65  int n, int prese
96380 72 76 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  rve);.SQLITE_PRI
96390 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
963a0 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65  VdbeCloseStateme
963b0 6e 74 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b  nt(Vdbe *, int);
963c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
963d0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
963e0 46 72 61 6d 65 44 65 6c 65 74 65 28 56 64 62 65  FrameDelete(Vdbe
963f0 46 72 61 6d 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Frame*);.SQLITE_
96400 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
96410 74 65 33 56 64 62 65 46 72 61 6d 65 52 65 73 74  te3VdbeFrameRest
96420 6f 72 65 28 56 64 62 65 46 72 61 6d 65 20 2a 29  ore(VdbeFrame *)
96430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
96440 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
96450 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 4d 65  eMemStoreType(Me
96460 6d 20 2a 70 4d 65 6d 29 3b 0a 53 51 4c 49 54 45  m *pMem);.SQLITE
96470 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
96480 69 74 65 33 56 64 62 65 54 72 61 6e 73 66 65 72  ite3VdbeTransfer
96490 45 72 72 6f 72 28 56 64 62 65 20 2a 70 29 3b 0a  Error(Vdbe *p);.
964a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
964b0 4d 49 54 5f 4d 45 52 47 45 5f 53 4f 52 54 0a 23  MIT_MERGE_SORT.#
964c0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56   define sqlite3V
964d0 64 62 65 53 6f 72 74 65 72 49 6e 69 74 28 59 2c  dbeSorterInit(Y,
964e0 5a 29 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f  Z)      SQLITE_O
964f0 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  K.# define sqlit
96500 65 33 56 64 62 65 53 6f 72 74 65 72 57 72 69 74  e3VdbeSorterWrit
96510 65 28 58 2c 59 2c 5a 29 20 20 20 53 51 4c 49 54  e(X,Y,Z)   SQLIT
96520 45 5f 4f 4b 0a 23 20 64 65 66 69 6e 65 20 73 71  E_OK.# define sq
96530 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 43  lite3VdbeSorterC
96540 6c 6f 73 65 28 59 2c 5a 29 0a 23 20 64 65 66 69  lose(Y,Z).# defi
96550 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f  ne sqlite3VdbeSo
96560 72 74 65 72 52 6f 77 6b 65 79 28 59 2c 5a 29 20  rterRowkey(Y,Z) 
96570 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64     SQLITE_OK.# d
96580 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62  efine sqlite3Vdb
96590 65 53 6f 72 74 65 72 52 65 77 69 6e 64 28 58 2c  eSorterRewind(X,
965a0 59 2c 5a 29 20 20 53 51 4c 49 54 45 5f 4f 4b 0a  Y,Z)  SQLITE_OK.
965b0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
965c0 56 64 62 65 53 6f 72 74 65 72 4e 65 78 74 28 58  VdbeSorterNext(X
965d0 2c 59 2c 5a 29 20 20 20 20 53 51 4c 49 54 45 5f  ,Y,Z)    SQLITE_
965e0 4f 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  OK.# define sqli
965f0 74 65 33 56 64 62 65 53 6f 72 74 65 72 43 6f 6d  te3VdbeSorterCom
96600 70 61 72 65 28 58 2c 59 2c 5a 29 20 53 51 4c 49  pare(X,Y,Z) SQLI
96610 54 45 5f 4f 4b 0a 23 65 6c 73 65 0a 53 51 4c 49  TE_OK.#else.SQLI
96620 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
96630 71 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72  qlite3VdbeSorter
96640 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 2c 20  Init(sqlite3 *, 
96650 56 64 62 65 43 75 72 73 6f 72 20 2a 29 3b 0a 53  VdbeCursor *);.S
96660 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
96670 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f  id sqlite3VdbeSo
96680 72 74 65 72 43 6c 6f 73 65 28 73 71 6c 69 74 65  rterClose(sqlite
96690 33 20 2a 2c 20 56 64 62 65 43 75 72 73 6f 72 20  3 *, VdbeCursor 
966a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
966b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
966c0 62 65 53 6f 72 74 65 72 52 6f 77 6b 65 79 28 63  beSorterRowkey(c
966d0 6f 6e 73 74 20 56 64 62 65 43 75 72 73 6f 72 20  onst VdbeCursor 
966e0 2a 2c 20 4d 65 6d 20 2a 29 3b 0a 53 51 4c 49 54  *, Mem *);.SQLIT
966f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
96700 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 4e  lite3VdbeSorterN
96710 65 78 74 28 73 71 6c 69 74 65 33 20 2a 2c 20 63  ext(sqlite3 *, c
96720 6f 6e 73 74 20 56 64 62 65 43 75 72 73 6f 72 20  onst VdbeCursor 
96730 2a 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54  *, int *);.SQLIT
96740 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
96750 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 52  lite3VdbeSorterR
96760 65 77 69 6e 64 28 73 71 6c 69 74 65 33 20 2a 2c  ewind(sqlite3 *,
96770 20 63 6f 6e 73 74 20 56 64 62 65 43 75 72 73 6f   const VdbeCurso
96780 72 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c  r *, int *);.SQL
96790 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
967a0 73 71 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65  sqlite3VdbeSorte
967b0 72 57 72 69 74 65 28 73 71 6c 69 74 65 33 20 2a  rWrite(sqlite3 *
967c0 2c 20 63 6f 6e 73 74 20 56 64 62 65 43 75 72 73  , const VdbeCurs
967d0 6f 72 20 2a 2c 20 4d 65 6d 20 2a 29 3b 0a 53 51  or *, Mem *);.SQ
967e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
967f0 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f 72 74   sqlite3VdbeSort
96800 65 72 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20  erCompare(const 
96810 56 64 62 65 43 75 72 73 6f 72 20 2a 2c 20 4d 65  VdbeCursor *, Me
96820 6d 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a 23 65 6e  m *, int *);.#en
96830 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  dif..#if !define
96840 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  d(SQLITE_OMIT_SH
96850 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 53  ARED_CACHE) && S
96860 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
96870 3e 30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  >0.SQLITE_PRIVAT
96880 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
96890 56 64 62 65 45 6e 74 65 72 28 56 64 62 65 2a 29  VdbeEnter(Vdbe*)
968a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
968b0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56     void sqlite3V
968c0 64 62 65 4c 65 61 76 65 28 56 64 62 65 2a 29 3b  dbeLeave(Vdbe*);
968d0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
968e0 73 71 6c 69 74 65 33 56 64 62 65 45 6e 74 65 72  sqlite3VdbeEnter
968f0 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  (X).# define sql
96900 69 74 65 33 56 64 62 65 4c 65 61 76 65 28 58 29  ite3VdbeLeave(X)
96910 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
96920 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
96930 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
96940 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 41   sqlite3VdbeMemA
96950 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 56 64 62  boutToChange(Vdb
96960 65 2a 2c 4d 65 6d 2a 29 3b 0a 23 65 6e 64 69 66  e*,Mem*);.#endif
96970 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
96980 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
96990 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  Y.SQLITE_PRIVATE
969a0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
969b0 43 68 65 63 6b 46 6b 28 56 64 62 65 20 2a 2c 20  CheckFk(Vdbe *, 
969c0 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65  int);.#else.# de
969d0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65  fine sqlite3Vdbe
969e0 43 68 65 63 6b 46 6b 28 70 2c 69 29 20 30 0a 23  CheckFk(p,i) 0.#
969f0 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
96a00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
96a10 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74  3VdbeMemTranslat
96a20 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 66  e(Mem*, u8);.#if
96a30 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
96a40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
96a50 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64    void sqlite3Vd
96a60 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65 2a  bePrintSql(Vdbe*
96a70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
96a80 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
96a90 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69  VdbeMemPrettyPri
96aa0 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68  nt(Mem *pMem, ch
96ab0 61 72 20 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 69  ar *zBuf);.#endi
96ac0 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f.SQLITE_PRIVATE
96ad0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
96ae0 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d  MemHandleBom(Mem
96af0 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64 65   *pMem);..#ifnde
96b00 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
96b10 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52  CRBLOB.SQLITE_PR
96b20 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
96b30 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64  te3VdbeMemExpand
96b40 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a 20 20 23  Blob(Mem *);.  #
96b50 64 65 66 69 6e 65 20 45 78 70 61 6e 64 42 6c 6f  define ExpandBlo
96b60 62 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67  b(P) (((P)->flag
96b70 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69  s&MEM_Zero)?sqli
96b80 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64  te3VdbeMemExpand
96b90 42 6c 6f 62 28 50 29 3a 30 29 0a 23 65 6c 73 65  Blob(P):0).#else
96ba0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
96bb0 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42  e3VdbeMemExpandB
96bc0 6c 6f 62 28 78 29 20 53 51 4c 49 54 45 5f 4f 4b  lob(x) SQLITE_OK
96bd0 0a 20 20 23 64 65 66 69 6e 65 20 45 78 70 61 6e  .  #define Expan
96be0 64 42 6c 6f 62 28 50 29 20 53 51 4c 49 54 45 5f  dBlob(P) SQLITE_
96bf0 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69  OK.#endif..#endi
96c00 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 56  f /* !defined(_V
96c10 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a 2f  DBEINT_H_) */../
96c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
96c30 6e 64 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 20  nd of vdbeInt.h 
96c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
96c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
96c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
96c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
96c80 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
96c90 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73  we left off in s
96ca0 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  tatus.c ********
96cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
96cc0 2f 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 73 20  /*.** Variables 
96cd0 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 63 6f  in which to reco
96ce0 72 64 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d  rd status inform
96cf0 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65  ation..*/.typede
96d00 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
96d10 53 74 61 74 54 79 70 65 20 73 71 6c 69 74 65 33  StatType sqlite3
96d20 53 74 61 74 54 79 70 65 3b 0a 73 74 61 74 69 63  StatType;.static
96d30 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75   SQLITE_WSD stru
96d40 63 74 20 73 71 6c 69 74 65 33 53 74 61 74 54 79  ct sqlite3StatTy
96d50 70 65 20 7b 0a 20 20 69 6e 74 20 6e 6f 77 56 61  pe {.  int nowVa
96d60 6c 75 65 5b 31 30 5d 3b 20 20 20 20 20 20 20 20  lue[10];        
96d70 20 2f 2a 20 43 75 72 72 65 6e 74 20 76 61 6c 75   /* Current valu
96d80 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 56 61 6c  e */.  int mxVal
96d90 75 65 5b 31 30 5d 3b 20 20 20 20 20 20 20 20 20  ue[10];         
96da0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61 6c 75   /* Maximum valu
96db0 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 53 74  e */.} sqlite3St
96dc0 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b 30 2c  at = { {0,}, {0,
96dd0 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20 22 77  } };.../* The "w
96de0 73 64 53 74 61 74 22 20 6d 61 63 72 6f 20 77 69  sdStat" macro wi
96df0 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68  ll resolve to th
96e00 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61  e status informa
96e10 74 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20 76 65  tion.** state ve
96e20 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62  ctor.  If writab
96e30 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69  le static data i
96e40 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e  s unsupported on
96e50 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20   the target,.** 
96e60 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74  we have to locat
96e70 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74  e the state vect
96e80 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20  or at run-time. 
96e90 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d   In the more com
96ea0 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72  mon.** case wher
96eb0 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69  e writable stati
96ec0 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f 72  c data is suppor
96ed0 74 65 64 2c 20 77 73 64 53 74 61 74 20 63 61 6e  ted, wsdStat can
96ee0 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a   refer directly.
96ef0 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74  ** to the "sqlit
96f00 65 33 53 74 61 74 22 20 73 74 61 74 65 20 76 65  e3Stat" state ve
96f10 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62  ctor declared ab
96f20 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ove..*/.#ifdef S
96f30 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23  QLITE_OMIT_WSD.#
96f40 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 49   define wsdStatI
96f50 6e 69 74 20 20 73 71 6c 69 74 65 33 53 74 61 74  nit  sqlite3Stat
96f60 54 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f 42 41  Type *x = &GLOBA
96f70 4c 28 73 71 6c 69 74 65 33 53 74 61 74 54 79 70  L(sqlite3StatTyp
96f80 65 2c 73 71 6c 69 74 65 33 53 74 61 74 29 0a 23  e,sqlite3Stat).#
96f90 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 20   define wsdStat 
96fa0 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66  x[0].#else.# def
96fb0 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69 74 0a  ine wsdStatInit.
96fc0 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74  # define wsdStat
96fd0 20 73 71 6c 69 74 65 33 53 74 61 74 0a 23 65 6e   sqlite3Stat.#en
96fe0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  dif../*.** Retur
96ff0 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61  n the current va
97000 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20  lue of a status 
97010 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 53 51  parameter..*/.SQ
97020 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
97030 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61   sqlite3StatusVa
97040 6c 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20 20 77  lue(int op){.  w
97050 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73  sdStatInit;.  as
97060 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f  sert( op>=0 && o
97070 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53  p<ArraySize(wsdS
97080 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b  tat.nowValue) );
97090 0a 20 20 72 65 74 75 72 6e 20 77 73 64 53 74 61  .  return wsdSta
970a0 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a  t.nowValue[op];.
970b0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e 20 74  }../*.** Add N t
970c0 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61  o the value of a
970d0 20 73 74 61 74 75 73 20 72 65 63 6f 72 64 2e 20   status record. 
970e0 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   It is assumed t
970f0 68 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65  hat the.** calle
97100 72 20 68 6f 6c 64 73 20 61 70 70 72 6f 70 72 69  r holds appropri
97110 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 53 51  ate locks..*/.SQ
97120 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
97130 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41  d sqlite3StatusA
97140 64 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 4e  dd(int op, int N
97150 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74  ){.  wsdStatInit
97160 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d  ;.  assert( op>=
97170 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a  0 && op<ArraySiz
97180 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  e(wsdStat.nowVal
97190 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74 61 74  ue) );.  wsdStat
971a0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 2b 3d  .nowValue[op] +=
971b0 20 4e 3b 0a 20 20 69 66 28 20 77 73 64 53 74 61   N;.  if( wsdSta
971c0 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e 77  t.nowValue[op]>w
971d0 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f  sdStat.mxValue[o
971e0 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61  p] ){.    wsdSta
971f0 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20  t.mxValue[op] = 
97200 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65  wsdStat.nowValue
97210 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  [op];.  }.}../*.
97220 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c 75 65  ** Set the value
97230 20 6f 66 20 61 20 73 74 61 74 75 73 20 74 6f 20   of a status to 
97240 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  X..*/.SQLITE_PRI
97250 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
97260 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 20 6f  3StatusSet(int o
97270 70 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77 73 64  p, int X){.  wsd
97280 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65  StatInit;.  asse
97290 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c  rt( op>=0 && op<
972a0 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 61  ArraySize(wsdSta
972b0 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20  t.nowValue) );. 
972c0 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75   wsdStat.nowValu
972d0 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69 66 28  e[op] = X;.  if(
972e0 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75   wsdStat.nowValu
972f0 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78  e[op]>wsdStat.mx
97300 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20  Value[op] ){.   
97310 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65   wsdStat.mxValue
97320 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e  [op] = wsdStat.n
97330 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d  owValue[op];.  }
97340 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20  .}../*.** Query 
97350 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69  status informati
97360 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  on..**.** This i
97370 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73  mplementation as
97380 73 75 6d 65 73 20 74 68 61 74 20 72 65 61 64 69  sumes that readi
97390 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 61 6e  ng or writing an
973a0 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32 2d 62   aligned.** 32-b
973b0 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 61 6e  it integer is an
973c0 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 69 6f   atomic operatio
973d0 6e 2e 20 20 49 66 20 74 68 61 74 20 61 73 73 75  n.  If that assu
973e0 6d 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20 74 72  mption is not tr
973f0 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73  ue,.** then this
97400 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 20   routine is not 
97410 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53  threadsafe..*/.S
97420 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
97430 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 6e 74  lite3_status(int
97440 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 72 65   op, int *pCurre
97450 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 77 61  nt, int *pHighwa
97460 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 46 6c  ter, int resetFl
97470 61 67 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e  ag){.  wsdStatIn
97480 69 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30 20 7c  it;.  if( op<0 |
97490 7c 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a 65 28  | op>=ArraySize(
974a0 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65  wsdStat.nowValue
974b0 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
974c0 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b  SQLITE_MISUSE_BK
974d0 50 54 3b 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72  PT;.  }.  *pCurr
974e0 65 6e 74 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f  ent = wsdStat.no
974f0 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70  wValue[op];.  *p
97500 48 69 67 68 77 61 74 65 72 20 3d 20 77 73 64 53  Highwater = wsdS
97510 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b  tat.mxValue[op];
97520 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c 61 67  .  if( resetFlag
97530 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e   ){.    wsdStat.
97540 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73  mxValue[op] = ws
97550 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f  dStat.nowValue[o
97560 70 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  p];.  }.  return
97570 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
97580 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74 75  *.** Query statu
97590 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  s information fo
975a0 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62  r a single datab
975b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  ase connection.*
975c0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
975d0 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74   sqlite3_db_stat
975e0 75 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  us(.  sqlite3 *d
975f0 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  b,          /* T
97600 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
97610 65 63 74 69 6f 6e 20 77 68 6f 73 65 20 73 74 61  ection whose sta
97620 74 75 73 20 69 73 20 64 65 73 69 72 65 64 20 2a  tus is desired *
97630 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20  /.  int op,     
97640 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
97650 74 75 73 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e  tus verb */.  in
97660 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 20 20 20  t *pCurrent,    
97670 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 75 72      /* Write cur
97680 72 65 6e 74 20 76 61 6c 75 65 20 68 65 72 65 20  rent value here 
97690 2a 2f 0a 20 20 69 6e 74 20 2a 70 48 69 67 68 77  */.  int *pHighw
976a0 61 74 65 72 2c 20 20 20 20 20 20 2f 2a 20 57 72  ater,      /* Wr
976b0 69 74 65 20 68 69 67 68 2d 77 61 74 65 72 20 6d  ite high-water m
976c0 61 72 6b 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  ark here */.  in
976d0 74 20 72 65 73 65 74 46 6c 61 67 20 20 20 20 20  t resetFlag     
976e0 20 20 20 20 2f 2a 20 52 65 73 65 74 20 68 69 67      /* Reset hig
976f0 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 66 20  h-water mark if 
97700 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  true */.){.  int
97710 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
97720 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
97730 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d  e */.  sqlite3_m
97740 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
97750 75 74 65 78 29 3b 0a 20 20 73 77 69 74 63 68 28  utex);.  switch(
97760 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20   op ){.    case 
97770 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
97780 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a 20  LOOKASIDE_USED: 
97790 7b 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65 6e  {.      *pCurren
977a0 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  t = db->lookasid
977b0 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a 70  e.nOut;.      *p
977c0 48 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d 3e  Highwater = db->
977d0 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 3b  lookaside.mxOut;
977e0 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 65 74  .      if( reset
977f0 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20  Flag ){.        
97800 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78  db->lookaside.mx
97810 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73  Out = db->lookas
97820 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20  ide.nOut;.      
97830 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
97840 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 53     }..    case S
97850 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c  QLITE_DBSTATUS_L
97860 4f 4f 4b 41 53 49 44 45 5f 48 49 54 3a 0a 20 20  OOKASIDE_HIT:.  
97870 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42    case SQLITE_DB
97880 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45  STATUS_LOOKASIDE
97890 5f 4d 49 53 53 5f 53 49 5a 45 3a 0a 20 20 20 20  _MISS_SIZE:.    
978a0 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54  case SQLITE_DBST
978b0 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 4d  ATUS_LOOKASIDE_M
978c0 49 53 53 5f 46 55 4c 4c 3a 20 7b 0a 20 20 20 20  ISS_FULL: {.    
978d0 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
978e0 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
978f0 4c 4f 4f 4b 41 53 49 44 45 5f 48 49 54 20 29 3b  LOOKASIDE_HIT );
97900 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
97910 20 6f 70 3d 3d 53 51 4c 49 54 45 5f 44 42 53 54   op==SQLITE_DBST
97920 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 4d  ATUS_LOOKASIDE_M
97930 49 53 53 5f 53 49 5a 45 20 29 3b 0a 20 20 20 20  ISS_SIZE );.    
97940 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
97950 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
97960 4c 4f 4f 4b 41 53 49 44 45 5f 4d 49 53 53 5f 46  LOOKASIDE_MISS_F
97970 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 61 73 73  ULL );.      ass
97980 65 72 74 28 20 28 6f 70 2d 53 51 4c 49 54 45 5f  ert( (op-SQLITE_
97990 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49  DBSTATUS_LOOKASI
979a0 44 45 5f 48 49 54 29 3e 3d 30 20 29 3b 0a 20 20  DE_HIT)>=0 );.  
979b0 20 20 20 20 61 73 73 65 72 74 28 20 28 6f 70 2d      assert( (op-
979c0 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
979d0 4c 4f 4f 4b 41 53 49 44 45 5f 48 49 54 29 3c 33  LOOKASIDE_HIT)<3
979e0 20 29 3b 0a 20 20 20 20 20 20 2a 70 43 75 72 72   );.      *pCurr
979f0 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 2a  ent = 0;.      *
97a00 70 48 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d  pHighwater = db-
97a10 3e 6c 6f 6f 6b 61 73 69 64 65 2e 61 6e 53 74 61  >lookaside.anSta
97a20 74 5b 6f 70 20 2d 20 53 51 4c 49 54 45 5f 44 42  t[op - SQLITE_DB
97a30 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45  STATUS_LOOKASIDE
97a40 5f 48 49 54 5d 3b 0a 20 20 20 20 20 20 69 66 28  _HIT];.      if(
97a50 20 72 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 20   resetFlag ){.  
97a60 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73        db->lookas
97a70 69 64 65 2e 61 6e 53 74 61 74 5b 6f 70 20 2d 20  ide.anStat[op - 
97a80 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
97a90 4c 4f 4f 4b 41 53 49 44 45 5f 48 49 54 5d 20 3d  LOOKASIDE_HIT] =
97aa0 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
97ab0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
97ac0 20 20 20 20 2f 2a 20 0a 20 20 20 20 2a 2a 20 52      /* .    ** R
97ad0 65 74 75 72 6e 20 61 6e 20 61 70 70 72 6f 78 69  eturn an approxi
97ae0 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 61  mation for the a
97af0 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20  mount of memory 
97b00 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 0a 20  currently used. 
97b10 20 20 20 2a 2a 20 62 79 20 61 6c 6c 20 70 61 67     ** by all pag
97b20 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ers associated w
97b30 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 64 61  ith the given da
97b40 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
97b50 6e 2e 20 20 54 68 65 0a 20 20 20 20 2a 2a 20 68  n.  The.    ** h
97b60 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 69 73  ighwater mark is
97b70 20 6d 65 61 6e 69 6e 67 6c 65 73 73 20 61 6e 64   meaningless and
97b80 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20   is returned as 
97b90 7a 65 72 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  zero..    */.   
97ba0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53   case SQLITE_DBS
97bb0 54 41 54 55 53 5f 43 41 43 48 45 5f 55 53 45 44  TATUS_CACHE_USED
97bc0 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 6f  : {.      int to
97bd0 74 61 6c 55 73 65 64 20 3d 20 30 3b 0a 20 20 20  talUsed = 0;.   
97be0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
97bf0 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
97c00 72 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20  rAll(db);.      
97c10 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
97c20 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
97c30 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64    Btree *pBt = d
97c40 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
97c50 20 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29         if( pBt )
97c60 7b 0a 20 20 20 20 20 20 20 20 20 20 50 61 67 65  {.          Page
97c70 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69  r *pPager = sqli
97c80 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 42  te3BtreePager(pB
97c90 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 74 6f  t);.          to
97ca0 74 61 6c 55 73 65 64 20 2b 3d 20 73 71 6c 69 74  talUsed += sqlit
97cb0 65 33 50 61 67 65 72 4d 65 6d 55 73 65 64 28 70  e3PagerMemUsed(p
97cc0 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20  Pager);.        
97cd0 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
97ce0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
97cf0 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20  eAll(db);.      
97d00 2a 70 43 75 72 72 65 6e 74 20 3d 20 74 6f 74 61  *pCurrent = tota
97d10 6c 55 73 65 64 3b 0a 20 20 20 20 20 20 2a 70 48  lUsed;.      *pH
97d20 69 67 68 77 61 74 65 72 20 3d 20 30 3b 0a 20 20  ighwater = 0;.  
97d30 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
97d40 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
97d50 2a 70 43 75 72 72 65 6e 74 20 67 65 74 73 20 61  *pCurrent gets a
97d60 6e 20 61 63 63 75 72 61 74 65 20 65 73 74 69 6d  n accurate estim
97d70 61 74 65 20 6f 66 20 74 68 65 20 61 6d 6f 75 6e  ate of the amoun
97d80 74 20 6f 66 20 6d 65 6d 6f 72 79 20 75 73 65 64  t of memory used
97d90 0a 20 20 20 20 2a 2a 20 74 6f 20 73 74 6f 72 65  .    ** to store
97da0 20 74 68 65 20 73 63 68 65 6d 61 20 66 6f 72 20   the schema for 
97db0 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 28 6d  all databases (m
97dc0 61 69 6e 2c 20 74 65 6d 70 2c 20 61 6e 64 20 61  ain, temp, and a
97dd0 6e 79 20 41 54 54 41 43 48 65 64 0a 20 20 20 20  ny ATTACHed.    
97de0 2a 2a 20 64 61 74 61 62 61 73 65 73 2e 20 20 2a  ** databases.  *
97df0 70 48 69 67 68 77 61 74 65 72 20 69 73 20 73 65  pHighwater is se
97e00 74 20 74 6f 20 7a 65 72 6f 2e 0a 20 20 20 20 2a  t to zero..    *
97e10 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  /.    case SQLIT
97e20 45 5f 44 42 53 54 41 54 55 53 5f 53 43 48 45 4d  E_DBSTATUS_SCHEM
97e30 41 5f 55 53 45 44 3a 20 7b 0a 20 20 20 20 20 20  A_USED: {.      
97e40 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
97e50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
97e60 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74  sed to iterate t
97e70 68 72 6f 75 67 68 20 73 63 68 65 6d 61 73 20 2a  hrough schemas *
97e80 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74  /.      int nByt
97e90 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
97ea0 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 61      /* Used to a
97eb0 63 63 75 6d 75 6c 61 74 65 20 72 65 74 75 72 6e  ccumulate return
97ec0 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 20 20 20   value */..     
97ed0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
97ee0 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20  erAll(db);.     
97ef0 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 65   db->pnBytesFree
97f00 64 20 3d 20 26 6e 42 79 74 65 3b 0a 20 20 20 20  d = &nByte;.    
97f10 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
97f20 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
97f30 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68      Schema *pSch
97f40 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  ema = db->aDb[i]
97f50 2e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20  .pSchema;.      
97f60 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 53 63    if( ALWAYS(pSc
97f70 68 65 6d 61 21 3d 30 29 20 29 7b 0a 20 20 20 20  hema!=0) ){.    
97f80 20 20 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a        HashElem *
97f90 70 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 6e 42  p;..          nB
97fa0 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 47 6c  yte += sqlite3Gl
97fb0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f  obalConfig.m.xRo
97fc0 75 6e 64 75 70 28 73 69 7a 65 6f 66 28 48 61 73  undup(sizeof(Has
97fd0 68 45 6c 65 6d 29 29 20 2a 20 28 0a 20 20 20 20  hElem)) * (.    
97fe0 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65 6d            pSchem
97ff0 61 2d 3e 74 62 6c 48 61 73 68 2e 63 6f 75 6e 74  a->tblHash.count
98000 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2b 20   .            + 
98010 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73  pSchema->trigHas
98020 68 2e 63 6f 75 6e 74 0a 20 20 20 20 20 20 20 20  h.count.        
98030 20 20 20 20 2b 20 70 53 63 68 65 6d 61 2d 3e 69      + pSchema->i
98040 64 78 48 61 73 68 2e 63 6f 75 6e 74 0a 20 20 20  dxHash.count.   
98050 20 20 20 20 20 20 20 20 20 2b 20 70 53 63 68 65           + pSche
98060 6d 61 2d 3e 66 6b 65 79 48 61 73 68 2e 63 6f 75  ma->fkeyHash.cou
98070 6e 74 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a  nt.          );.
98080 20 20 20 20 20 20 20 20 20 20 6e 42 79 74 65 20            nByte 
98090 2b 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  += sqlite3Malloc
980a0 53 69 7a 65 28 70 53 63 68 65 6d 61 2d 3e 74 62  Size(pSchema->tb
980b0 6c 48 61 73 68 2e 68 74 29 3b 0a 20 20 20 20 20  lHash.ht);.     
980c0 20 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 71       nByte += sq
980d0 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28  lite3MallocSize(
980e0 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73  pSchema->trigHas
980f0 68 2e 68 74 29 3b 0a 20 20 20 20 20 20 20 20 20  h.ht);.         
98100 20 6e 42 79 74 65 20 2b 3d 20 73 71 6c 69 74 65   nByte += sqlite
98110 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 53 63 68  3MallocSize(pSch
98120 65 6d 61 2d 3e 69 64 78 48 61 73 68 2e 68 74 29  ema->idxHash.ht)
98130 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 42 79 74  ;.          nByt
98140 65 20 2b 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  e += sqlite3Mall
98150 6f 63 53 69 7a 65 28 70 53 63 68 65 6d 61 2d 3e  ocSize(pSchema->
98160 66 6b 65 79 48 61 73 68 2e 68 74 29 3b 0a 0a 20  fkeyHash.ht);.. 
98170 20 20 20 20 20 20 20 20 20 66 6f 72 28 70 3d 73           for(p=s
98180 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
98190 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73  pSchema->trigHas
981a0 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48  h); p; p=sqliteH
981b0 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20  ashNext(p)){.   
981c0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
981d0 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 64 62  DeleteTrigger(db
981e0 2c 20 28 54 72 69 67 67 65 72 2a 29 73 71 6c 69  , (Trigger*)sqli
981f0 74 65 48 61 73 68 44 61 74 61 28 70 29 29 3b 0a  teHashData(p));.
98200 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
98210 20 20 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69        for(p=sqli
98220 74 65 48 61 73 68 46 69 72 73 74 28 26 70 53 63  teHashFirst(&pSc
98230 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20  hema->tblHash); 
98240 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e  p; p=sqliteHashN
98250 65 78 74 28 70 29 29 7b 0a 20 20 20 20 20 20 20  ext(p)){.       
98260 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65       sqlite3Dele
98270 74 65 54 61 62 6c 65 28 64 62 2c 20 28 54 61 62  teTable(db, (Tab
98280 6c 65 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44  le *)sqliteHashD
98290 61 74 61 28 70 29 29 3b 0a 20 20 20 20 20 20 20  ata(p));.       
982a0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
982b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 62 2d       }.      db-
982c0 3e 70 6e 42 79 74 65 73 46 72 65 65 64 20 3d 20  >pnBytesFreed = 
982d0 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  0;.      sqlite3
982e0 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62  BtreeLeaveAll(db
982f0 29 3b 0a 0a 20 20 20 20 20 20 2a 70 48 69 67 68  );..      *pHigh
98300 77 61 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 20  water = 0;.     
98310 20 2a 70 43 75 72 72 65 6e 74 20 3d 20 6e 42 79   *pCurrent = nBy
98320 74 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  te;.      break;
98330 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20  .    }..    /*. 
98340 20 20 20 2a 2a 20 2a 70 43 75 72 72 65 6e 74 20     ** *pCurrent 
98350 67 65 74 73 20 61 6e 20 61 63 63 75 72 61 74 65  gets an accurate
98360 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65   estimate of the
98370 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72   amount of memor
98380 79 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 74 6f  y used.    ** to
98390 20 73 74 6f 72 65 20 61 6c 6c 20 70 72 65 70 61   store all prepa
983a0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  red statements..
983b0 20 20 20 20 2a 2a 20 2a 70 48 69 67 68 77 61 74      ** *pHighwat
983c0 65 72 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72  er is set to zer
983d0 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  o..    */.    ca
983e0 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54  se SQLITE_DBSTAT
983f0 55 53 5f 53 54 4d 54 5f 55 53 45 44 3a 20 7b 0a  US_STMT_USED: {.
98400 20 20 20 20 20 20 73 74 72 75 63 74 20 56 64 62        struct Vdb
98410 65 20 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20  e *pVdbe;       
98420 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65    /* Used to ite
98430 72 61 74 65 20 74 68 72 6f 75 67 68 20 56 4d 73  rate through VMs
98440 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 42   */.      int nB
98450 79 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  yte = 0;        
98460 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
98470 20 61 63 63 75 6d 75 6c 61 74 65 20 72 65 74 75   accumulate retu
98480 72 6e 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 20  rn value */..   
98490 20 20 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72     db->pnBytesFr
984a0 65 65 64 20 3d 20 26 6e 42 79 74 65 3b 0a 20 20  eed = &nByte;.  
984b0 20 20 20 20 66 6f 72 28 70 56 64 62 65 3d 64 62      for(pVdbe=db
984c0 2d 3e 70 56 64 62 65 3b 20 70 56 64 62 65 3b 20  ->pVdbe; pVdbe; 
984d0 70 56 64 62 65 3d 70 56 64 62 65 2d 3e 70 4e 65  pVdbe=pVdbe->pNe
984e0 78 74 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  xt){.        sql
984f0 69 74 65 33 56 64 62 65 43 6c 65 61 72 4f 62 6a  ite3VdbeClearObj
98500 65 63 74 28 64 62 2c 20 70 56 64 62 65 29 3b 0a  ect(db, pVdbe);.
98510 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
98520 62 46 72 65 65 28 64 62 2c 20 70 56 64 62 65 29  bFree(db, pVdbe)
98530 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
98540 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 65 64  db->pnBytesFreed
98550 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2a 70 48   = 0;..      *pH
98560 69 67 68 77 61 74 65 72 20 3d 20 30 3b 0a 20 20  ighwater = 0;.  
98570 20 20 20 20 2a 70 43 75 72 72 65 6e 74 20 3d 20      *pCurrent = 
98580 6e 42 79 74 65 3b 0a 0a 20 20 20 20 20 20 62 72  nByte;..      br
98590 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
985a0 2f 2a 0a 20 20 20 20 2a 2a 20 53 65 74 20 2a 70  /*.    ** Set *p
985b0 43 75 72 72 65 6e 74 20 74 6f 20 74 68 65 20 74  Current to the t
985c0 6f 74 61 6c 20 63 61 63 68 65 20 68 69 74 73 20  otal cache hits 
985d0 6f 72 20 6d 69 73 73 65 73 20 65 6e 63 6f 75 6e  or misses encoun
985e0 74 65 72 65 64 20 62 79 20 61 6c 6c 0a 20 20 20  tered by all.   
985f0 20 2a 2a 20 70 61 67 65 72 73 20 74 68 65 20 64   ** pagers the d
98600 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 69  atabase handle i
98610 73 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f 2e 20  s connected to. 
98620 2a 70 48 69 67 68 77 61 74 65 72 20 69 73 20 61  *pHighwater is a
98630 6c 77 61 79 73 20 73 65 74 20 0a 20 20 20 20 2a  lways set .    *
98640 2a 20 74 6f 20 7a 65 72 6f 2e 0a 20 20 20 20 2a  * to zero..    *
98650 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  /.    case SQLIT
98660 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43 48 45  E_DBSTATUS_CACHE
98670 5f 48 49 54 3a 0a 20 20 20 20 63 61 73 65 20 53  _HIT:.    case S
98680 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43  QLITE_DBSTATUS_C
98690 41 43 48 45 5f 4d 49 53 53 3a 0a 20 20 20 20 63  ACHE_MISS:.    c
986a0 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41  ase SQLITE_DBSTA
986b0 54 55 53 5f 43 41 43 48 45 5f 57 52 49 54 45 3a  TUS_CACHE_WRITE:
986c0 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20  {.      int i;. 
986d0 20 20 20 20 20 69 6e 74 20 6e 52 65 74 20 3d 20       int nRet = 
986e0 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  0;.      assert(
986f0 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53   SQLITE_DBSTATUS
98700 5f 43 41 43 48 45 5f 4d 49 53 53 3d 3d 53 51 4c  _CACHE_MISS==SQL
98710 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43  ITE_DBSTATUS_CAC
98720 48 45 5f 48 49 54 2b 31 20 29 3b 0a 20 20 20 20  HE_HIT+1 );.    
98730 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45    assert( SQLITE
98740 5f 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f  _DBSTATUS_CACHE_
98750 57 52 49 54 45 3d 3d 53 51 4c 49 54 45 5f 44 42  WRITE==SQLITE_DB
98760 53 54 41 54 55 53 5f 43 41 43 48 45 5f 48 49 54  STATUS_CACHE_HIT
98770 2b 32 20 29 3b 0a 0a 20 20 20 20 20 20 66 6f 72  +2 );..      for
98780 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
98790 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   i++){.        i
987a0 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  f( db->aDb[i].pB
987b0 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50  t ){.          P
987c0 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73  ager *pPager = s
987d0 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
987e0 28 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29  (db->aDb[i].pBt)
987f0 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
98800 74 65 33 50 61 67 65 72 43 61 63 68 65 53 74 61  te3PagerCacheSta
98810 74 28 70 50 61 67 65 72 2c 20 6f 70 2c 20 72 65  t(pPager, op, re
98820 73 65 74 46 6c 61 67 2c 20 26 6e 52 65 74 29 3b  setFlag, &nRet);
98830 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
98840 20 7d 0a 20 20 20 20 20 20 2a 70 48 69 67 68 77   }.      *pHighw
98850 61 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20  ater = 0;.      
98860 2a 70 43 75 72 72 65 6e 74 20 3d 20 6e 52 65 74  *pCurrent = nRet
98870 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
98880 20 20 20 7d 0a 0a 20 20 20 20 64 65 66 61 75 6c     }..    defaul
98890 74 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  t: {.      rc = 
988a0 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
988b0 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
988c0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
988d0 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
988e0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  rn rc;.}../*****
988f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
98900 20 73 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a   status.c ******
98910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98930 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
98940 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
98950 66 69 6c 65 20 64 61 74 65 2e 63 20 2a 2a 2a 2a  file date.c ****
98960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98980 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
98990 32 30 30 33 20 4f 63 74 6f 62 65 72 20 33 31 0a  2003 October 31.
989a0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
989b0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
989c0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
989d0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
989e0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
989f0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
98a00 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
98a10 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
98a20 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
98a30 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
98a40 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
98a50 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
98a60 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
98a70 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
98a80 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
98a90 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
98aa0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
98ab0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
98ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
98b00 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
98b10 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74  ains the C funct
98b20 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ions that implem
98b30 65 6e 74 20 64 61 74 65 20 61 6e 64 20 74 69 6d  ent date and tim
98b40 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 66  e.** functions f
98b50 6f 72 20 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a  or SQLite.  .**.
98b60 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 6c 79  ** There is only
98b70 20 6f 6e 65 20 65 78 70 6f 72 74 65 64 20 73 79   one exported sy
98b80 6d 62 6f 6c 20 69 6e 20 74 68 69 73 20 66 69 6c  mbol in this fil
98b90 65 20 2d 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  e - the function
98ba0 0a 2a 2a 20 73 71 6c 69 74 65 33 52 65 67 69 73  .** sqlite3Regis
98bb0 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74  terDateTimeFunct
98bc0 69 6f 6e 73 28 29 20 66 6f 75 6e 64 20 61 74 20  ions() found at 
98bd0 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68  the bottom of th
98be0 65 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 6f  e file..** All o
98bf0 74 68 65 72 20 63 6f 64 65 20 68 61 73 20 66 69  ther code has fi
98c00 6c 65 20 73 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a 20  le scope..**.** 
98c10 53 51 4c 69 74 65 20 70 72 6f 63 65 73 73 65 73  SQLite processes
98c20 20 61 6c 6c 20 74 69 6d 65 73 20 61 6e 64 20 64   all times and d
98c30 61 74 65 73 20 61 73 20 4a 75 6c 69 61 6e 20 44  ates as Julian D
98c40 61 79 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 65  ay numbers.  The
98c50 0a 2a 2a 20 64 61 74 65 73 20 61 6e 64 20 74 69  .** dates and ti
98c60 6d 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 61  mes are stored a
98c70 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
98c80 64 61 79 73 20 73 69 6e 63 65 20 6e 6f 6f 6e 0a  days since noon.
98c90 2a 2a 20 69 6e 20 47 72 65 65 6e 77 69 63 68 20  ** in Greenwich 
98ca0 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32 34 2c 20  on November 24, 
98cb0 34 37 31 34 20 42 2e 43 2e 20 61 63 63 6f 72 64  4714 B.C. accord
98cc0 69 6e 67 20 74 6f 20 74 68 65 20 47 72 65 67 6f  ing to the Grego
98cd0 72 69 61 6e 0a 2a 2a 20 63 61 6c 65 6e 64 61 72  rian.** calendar
98ce0 20 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a 2a 20   system. .**.** 
98cf0 31 39 37 30 2d 30 31 2d 30 31 20 30 30 3a 30 30  1970-01-01 00:00
98d00 3a 30 30 20 69 73 20 4a 44 20 32 34 34 30 35 38  :00 is JD 244058
98d10 37 2e 35 0a 2a 2a 20 32 30 30 30 2d 30 31 2d 30  7.5.** 2000-01-0
98d20 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44  1 00:00:00 is JD
98d30 20 32 34 35 31 35 34 34 2e 35 0a 2a 2a 0a 2a 2a   2451544.5.**.**
98d40 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 69   This implementi
98d50 6f 6e 20 72 65 71 75 69 72 65 73 20 79 65 61 72  on requires year
98d60 73 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 65  s to be expresse
98d70 64 20 61 73 20 61 20 34 2d 64 69 67 69 74 20 6e  d as a 4-digit n
98d80 75 6d 62 65 72 0a 2a 2a 20 77 68 69 63 68 20 6d  umber.** which m
98d90 65 61 6e 73 20 74 68 61 74 20 6f 6e 6c 79 20 64  eans that only d
98da0 61 74 65 73 20 62 65 74 77 65 65 6e 20 30 30 30  ates between 000
98db0 30 2d 30 31 2d 30 31 20 61 6e 64 20 39 39 39 39  0-01-01 and 9999
98dc0 2d 31 32 2d 33 31 20 63 61 6e 0a 2a 2a 20 62 65  -12-31 can.** be
98dd0 20 72 65 70 72 65 73 65 6e 74 65 64 2c 20 65 76   represented, ev
98de0 65 6e 20 74 68 6f 75 67 68 20 6a 75 6c 69 61 6e  en though julian
98df0 20 64 61 79 20 6e 75 6d 62 65 72 73 20 61 6c 6c   day numbers all
98e00 6f 77 20 61 20 6d 75 63 68 20 77 69 64 65 72 0a  ow a much wider.
98e10 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74 65  ** range of date
98e20 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 47 72 65  s..**.** The Gre
98e30 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 20  gorian calendar 
98e40 73 79 73 74 65 6d 20 69 73 20 75 73 65 64 20 66  system is used f
98e50 6f 72 20 61 6c 6c 20 64 61 74 65 73 20 61 6e 64  or all dates and
98e60 20 74 69 6d 65 73 2c 0a 2a 2a 20 65 76 65 6e 20   times,.** even 
98e70 74 68 6f 73 65 20 74 68 61 74 20 70 72 65 64 61  those that preda
98e80 74 65 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e  te the Gregorian
98e90 20 63 61 6c 65 6e 64 61 72 2e 20 20 48 69 73 74   calendar.  Hist
98ea0 6f 72 69 61 6e 73 20 75 73 75 61 6c 6c 79 0a 2a  orians usually.*
98eb0 2a 20 75 73 65 20 74 68 65 20 4a 75 6c 69 61 6e  * use the Julian
98ec0 20 63 61 6c 65 6e 64 61 72 20 66 6f 72 20 64 61   calendar for da
98ed0 74 65 73 20 70 72 69 6f 72 20 74 6f 20 31 35 38  tes prior to 158
98ee0 32 2d 31 30 2d 31 35 20 61 6e 64 20 66 6f 72 20  2-10-15 and for 
98ef0 73 6f 6d 65 0a 2a 2a 20 64 61 74 65 73 20 61 66  some.** dates af
98f00 74 65 72 77 61 72 64 73 2c 20 64 65 70 65 6e 64  terwards, depend
98f10 69 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65 2e 20 20  ing on locale.  
98f20 42 65 77 61 72 65 20 6f 66 20 74 68 69 73 20 64  Beware of this d
98f30 69 66 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a  ifference..**.**
98f40 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   The conversion 
98f50 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 65 20 69  algorithms are i
98f60 6d 70 6c 65 6d 65 6e 74 65 64 20 62 61 73 65 64  mplemented based
98f70 20 6f 6e 20 64 65 73 63 72 69 70 74 69 6f 6e 73   on descriptions
98f80 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  .** in the follo
98f90 77 69 6e 67 20 74 65 78 74 3a 0a 2a 2a 0a 2a 2a  wing text:.**.**
98fa0 20 20 20 20 20 20 4a 65 61 6e 20 4d 65 65 75 73        Jean Meeus
98fb0 0a 2a 2a 20 20 20 20 20 20 41 73 74 72 6f 6e 6f  .**      Astrono
98fc0 6d 69 63 61 6c 20 41 6c 67 6f 72 69 74 68 6d 73  mical Algorithms
98fd0 2c 20 32 6e 64 20 45 64 69 74 69 6f 6e 2c 20 31  , 2nd Edition, 1
98fe0 39 39 38 0a 2a 2a 20 20 20 20 20 20 49 53 42 4d  998.**      ISBM
98ff0 20 30 2d 39 34 33 33 39 36 2d 36 31 2d 31 0a 2a   0-943396-61-1.*
99000 2a 20 20 20 20 20 20 57 69 6c 6c 6d 61 6e 6e 2d  *      Willmann-
99010 42 65 6c 6c 2c 20 49 6e 63 0a 2a 2a 20 20 20 20  Bell, Inc.**    
99020 20 20 52 69 63 68 6d 6f 6e 64 2c 20 56 69 72 67    Richmond, Virg
99030 69 6e 69 61 20 28 55 53 41 29 0a 2a 2f 0a 2f 2a  inia (USA).*/./*
99040 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69   #include <stdli
99050 62 2e 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e 63 6c  b.h> */./* #incl
99060 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a  ude <assert.h> *
99070 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 74 69 6d 65  /.#include <time
99080 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .h>..#ifndef SQL
99090 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d  ITE_OMIT_DATETIM
990a0 45 5f 46 55 4e 43 53 0a 0a 0a 2f 2a 0a 2a 2a 20  E_FUNCS.../*.** 
990b0 41 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  A structure for 
990c0 68 6f 6c 64 69 6e 67 20 61 20 73 69 6e 67 6c 65  holding a single
990d0 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 2e 0a   date and time..
990e0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
990f0 74 20 44 61 74 65 54 69 6d 65 20 44 61 74 65 54  t DateTime DateT
99100 69 6d 65 3b 0a 73 74 72 75 63 74 20 44 61 74 65  ime;.struct Date
99110 54 69 6d 65 20 7b 0a 20 20 73 71 6c 69 74 65 33  Time {.  sqlite3
99120 5f 69 6e 74 36 34 20 69 4a 44 3b 20 2f 2a 20 54  _int64 iJD; /* T
99130 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75  he julian day nu
99140 6d 62 65 72 20 74 69 6d 65 73 20 38 36 34 30 30  mber times 86400
99150 30 30 30 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20  000 */.  int Y, 
99160 4d 2c 20 44 3b 20 20 20 20 20 20 20 2f 2a 20 59  M, D;       /* Y
99170 65 61 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e 64 20  ear, month, and 
99180 64 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20  day */.  int h, 
99190 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48  m;          /* H
991a0 6f 75 72 20 61 6e 64 20 6d 69 6e 75 74 65 73 20  our and minutes 
991b0 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b 20 20 20 20  */.  int tz;    
991c0 20 20 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 7a          /* Timez
991d0 6f 6e 65 20 6f 66 66 73 65 74 20 69 6e 20 6d 69  one offset in mi
991e0 6e 75 74 65 73 20 2a 2f 0a 20 20 64 6f 75 62 6c  nutes */.  doubl
991f0 65 20 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  e s;          /*
99200 20 53 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 63 68   Seconds */.  ch
99210 61 72 20 76 61 6c 69 64 59 4d 44 3b 20 20 20 20  ar validYMD;    
99220 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20   /* True (1) if 
99230 59 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69 64 20  Y,M,D are valid 
99240 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48  */.  char validH
99250 4d 53 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20  MS;     /* True 
99260 28 31 29 20 69 66 20 68 2c 6d 2c 73 20 61 72 65  (1) if h,m,s are
99270 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72   valid */.  char
99280 20 76 61 6c 69 64 4a 44 3b 20 20 20 20 20 20 2f   validJD;      /
99290 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 69 4a  * True (1) if iJ
992a0 44 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20  D is valid */.  
992b0 63 68 61 72 20 76 61 6c 69 64 54 5a 3b 20 20 20  char validTZ;   
992c0 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69     /* True (1) i
992d0 66 20 74 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f  f tz is valid */
992e0 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  .};.../*.** Conv
992f0 65 72 74 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f  ert zDate into o
99300 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67  ne or more integ
99310 65 72 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c  ers.  Additional
99320 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f   arguments.** co
99330 6d 65 20 69 6e 20 67 72 6f 75 70 73 20 6f 66 20  me in groups of 
99340 35 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  5 as follows:.**
99350 0a 2a 2a 20 20 20 20 20 20 20 4e 20 20 20 20 20  .**       N     
99360 20 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69    number of digi
99370 74 73 20 69 6e 20 74 68 65 20 69 6e 74 65 67 65  ts in the intege
99380 72 0a 2a 2a 20 20 20 20 20 20 20 6d 69 6e 20 20  r.**       min  
99390 20 20 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77     minimum allow
993a0 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ed value of the 
993b0 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20  integer.**      
993c0 20 6d 61 78 20 20 20 20 20 6d 61 78 69 6d 75 6d   max     maximum
993d0 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f   allowed value o
993e0 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a  f the integer.**
993f0 20 20 20 20 20 20 20 6e 65 78 74 43 20 20 20 66         nextC   f
99400 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 61  irst character a
99410 66 74 65 72 20 74 68 65 20 69 6e 74 65 67 65 72  fter the integer
99420 0a 2a 2a 20 20 20 20 20 20 20 70 56 61 6c 20 20  .**       pVal  
99430 20 20 77 68 65 72 65 20 74 6f 20 77 72 69 74 65    where to write
99440 20 74 68 65 20 69 6e 74 65 67 65 72 73 20 76 61   the integers va
99450 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65  lue..**.** Conve
99460 72 73 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20  rsions continue 
99470 75 6e 74 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e  until one with n
99480 65 78 74 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75  extC==0 is encou
99490 6e 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66  ntered..** The f
994a0 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
994b0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 75  the number of su
994c0 63 63 65 73 73 66 75 6c 20 63 6f 6e 76 65 72 73  ccessful convers
994d0 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ions..*/.static 
994e0 69 6e 74 20 67 65 74 44 69 67 69 74 73 28 63 6f  int getDigits(co
994f0 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c  nst char *zDate,
99500 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74   ...){.  va_list
99510 20 61 70 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a   ap;.  int val;.
99520 20 20 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d    int N;.  int m
99530 69 6e 3b 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20  in;.  int max;. 
99540 20 69 6e 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e   int nextC;.  in
99550 74 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63  t *pVal;.  int c
99560 6e 74 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61  nt = 0;.  va_sta
99570 72 74 28 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20  rt(ap, zDate);. 
99580 20 64 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f   do{.    N = va_
99590 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
995a0 20 20 6d 69 6e 20 3d 20 76 61 5f 61 72 67 28 61    min = va_arg(a
995b0 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78  p, int);.    max
995c0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
995d0 74 29 3b 0a 20 20 20 20 6e 65 78 74 43 20 3d 20  t);.    nextC = 
995e0 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
995f0 0a 20 20 20 20 70 56 61 6c 20 3d 20 76 61 5f 61  .    pVal = va_a
99600 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20  rg(ap, int*);.  
99610 20 20 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77    val = 0;.    w
99620 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20  hile( N-- ){.   
99630 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49     if( !sqlite3I
99640 73 64 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29  sdigit(*zDate) )
99650 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65  {.        goto e
99660 6e 64 5f 67 65 74 44 69 67 69 74 73 3b 0a 20 20  nd_getDigits;.  
99670 20 20 20 20 7d 0a 20 20 20 20 20 20 76 61 6c 20      }.      val 
99680 3d 20 76 61 6c 2a 31 30 20 2b 20 2a 7a 44 61 74  = val*10 + *zDat
99690 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 7a  e - '0';.      z
996a0 44 61 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  Date++;.    }.  
996b0 20 20 69 66 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c    if( val<min ||
996c0 20 76 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e 65 78   val>max || (nex
996d0 74 43 21 3d 30 20 26 26 20 6e 65 78 74 43 21 3d  tC!=0 && nextC!=
996e0 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20  *zDate) ){.     
996f0 20 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69 67   goto end_getDig
99700 69 74 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a  its;.    }.    *
99710 70 56 61 6c 20 3d 20 76 61 6c 3b 0a 20 20 20 20  pVal = val;.    
99720 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 63 6e 74  zDate++;.    cnt
99730 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6e 65  ++;.  }while( ne
99740 78 74 43 20 29 3b 0a 65 6e 64 5f 67 65 74 44 69  xtC );.end_getDi
99750 67 69 74 73 3a 0a 20 20 76 61 5f 65 6e 64 28 61  gits:.  va_end(a
99760 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6e 74  p);.  return cnt
99770 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65  ;.}../*.** Parse
99780 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65 78 74 65   a timezone exte
99790 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20 65 6e 64  nsion on the end
997a0 20 6f 66 20 61 20 64 61 74 65 2d 74 69 6d 65 2e   of a date-time.
997b0 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e 73 69 6f  .** The extensio
997c0 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  n is of the form
997d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 28  :.**.**        (
997e0 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20  +/-)HH:MM.**.** 
997f0 4f 72 20 74 68 65 20 22 7a 75 6c 75 22 20 6e 6f  Or the "zulu" no
99800 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20  tation:.**.**   
99810 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66 20       Z.**.** If 
99820 74 68 65 20 70 61 72 73 65 20 69 73 20 73 75 63  the parse is suc
99830 63 65 73 73 66 75 6c 2c 20 77 72 69 74 65 20 74  cessful, write t
99840 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6e  he number of min
99850 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68 61 6e 67  utes.** of chang
99860 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 72  e in p->tz and r
99870 65 74 75 72 6e 20 30 2e 20 20 49 66 20 61 20 70  eturn 0.  If a p
99880 61 72 73 65 72 20 65 72 72 6f 72 20 6f 63 63 75  arser error occu
99890 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f  rs,.** return no
998a0 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20  n-zero..**.** A 
998b0 6d 69 73 73 69 6e 67 20 73 70 65 63 69 66 69 65  missing specifie
998c0 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65  r is not conside
998d0 72 65 64 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f  red an error..*/
998e0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73  .static int pars
998f0 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 20  eTimezone(const 
99900 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74  char *zDate, Dat
99910 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74  eTime *p){.  int
99920 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 20   sgn = 0;.  int 
99930 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74 20  nHr, nMn;.  int 
99940 63 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69  c;.  while( sqli
99950 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61 74  te3Isspace(*zDat
99960 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d  e) ){ zDate++; }
99970 0a 20 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20 20  .  p->tz = 0;.  
99980 63 20 3d 20 2a 7a 44 61 74 65 3b 0a 20 20 69 66  c = *zDate;.  if
99990 28 20 63 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20  ( c=='-' ){.    
999a0 73 67 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73  sgn = -1;.  }els
999b0 65 20 69 66 28 20 63 3d 3d 27 2b 27 20 29 7b 0a  e if( c=='+' ){.
999c0 20 20 20 20 73 67 6e 20 3d 20 2b 31 3b 0a 20 20      sgn = +1;.  
999d0 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 5a 27  }else if( c=='Z'
999e0 20 7c 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a 20 20   || c=='z' ){.  
999f0 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 67    zDate++;.    g
99a00 6f 74 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a 20  oto zulu_time;. 
99a10 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
99a20 72 6e 20 63 21 3d 30 3b 0a 20 20 7d 0a 20 20 7a  rn c!=0;.  }.  z
99a30 44 61 74 65 2b 2b 3b 0a 20 20 69 66 28 20 67 65  Date++;.  if( ge
99a40 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32  tDigits(zDate, 2
99a50 2c 20 30 2c 20 31 34 2c 20 27 3a 27 2c 20 26 6e  , 0, 14, ':', &n
99a60 48 72 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c  Hr, 2, 0, 59, 0,
99a70 20 26 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20 20 20   &nMn)!=2 ){.   
99a80 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
99a90 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 70   zDate += 5;.  p
99aa0 2d 3e 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e 20  ->tz = sgn*(nMn 
99ab0 2b 20 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c 75 5f  + nHr*60);.zulu_
99ac0 74 69 6d 65 3a 0a 20 20 77 68 69 6c 65 28 20 73  time:.  while( s
99ad0 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a  qlite3Isspace(*z
99ae0 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b  Date) ){ zDate++
99af0 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 44  ; }.  return *zD
99b00 61 74 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ate!=0;.}../*.**
99b10 20 50 61 72 73 65 20 74 69 6d 65 73 20 6f 66 20   Parse times of 
99b20 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d 4d 20 6f  the form HH:MM o
99b30 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72 20 48 48  r HH:MM:SS or HH
99b40 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0a 2a 2a 20  :MM:SS.FFFF..** 
99b50 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61 6e 64 20  The HH, MM, and 
99b60 53 53 20 6d 75 73 74 20 65 61 63 68 20 62 65 20  SS must each be 
99b70 65 78 61 63 74 6c 79 20 32 20 64 69 67 69 74 73  exactly 2 digits
99b80 2e 20 20 54 68 65 0a 2a 2a 20 66 72 61 63 74 69  .  The.** fracti
99b90 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 46 46 46  onal seconds FFF
99ba0 46 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f 72 20  F can be one or 
99bb0 6d 6f 72 65 20 64 69 67 69 74 73 2e 0a 2a 2a 0a  more digits..**.
99bc0 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74  ** Return 1 if t
99bd0 68 65 72 65 20 69 73 20 61 20 70 61 72 73 69 6e  here is a parsin
99be0 67 20 65 72 72 6f 72 20 61 6e 64 20 30 20 6f 6e  g error and 0 on
99bf0 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 61   success..*/.sta
99c00 74 69 63 20 69 6e 74 20 70 61 72 73 65 48 68 4d  tic int parseHhM
99c10 6d 53 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  mSs(const char *
99c20 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20  zDate, DateTime 
99c30 2a 70 29 7b 0a 20 20 69 6e 74 20 68 2c 20 6d 2c  *p){.  int h, m,
99c40 20 73 3b 0a 20 20 64 6f 75 62 6c 65 20 6d 73 20   s;.  double ms 
99c50 3d 20 30 2e 30 3b 0a 20 20 69 66 28 20 67 65 74  = 0.0;.  if( get
99c60 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c  Digits(zDate, 2,
99c70 20 30 2c 20 32 34 2c 20 27 3a 27 2c 20 26 68 2c   0, 24, ':', &h,
99c80 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 6d   2, 0, 59, 0, &m
99c90 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75  )!=2 ){.    retu
99ca0 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74  rn 1;.  }.  zDat
99cb0 65 20 2b 3d 20 35 3b 0a 20 20 69 66 28 20 2a 7a  e += 5;.  if( *z
99cc0 44 61 74 65 3d 3d 27 3a 27 20 29 7b 0a 20 20 20  Date==':' ){.   
99cd0 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 69 66   zDate++;.    if
99ce0 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 74  ( getDigits(zDat
99cf0 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20  e, 2, 0, 59, 0, 
99d00 26 73 29 21 3d 31 20 29 7b 0a 20 20 20 20 20 20  &s)!=1 ){.      
99d10 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
99d20 20 20 20 20 7a 44 61 74 65 20 2b 3d 20 32 3b 0a      zDate += 2;.
99d30 20 20 20 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d      if( *zDate==
99d40 27 2e 27 20 26 26 20 73 71 6c 69 74 65 33 49 73  '.' && sqlite3Is
99d50 64 69 67 69 74 28 7a 44 61 74 65 5b 31 5d 29 20  digit(zDate[1]) 
99d60 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20  ){.      double 
99d70 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20  rScale = 1.0;.  
99d80 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20      zDate++;.   
99d90 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65     while( sqlite
99da0 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 65 29  3Isdigit(*zDate)
99db0 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 73 20 3d   ){.        ms =
99dc0 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 61 74   ms*10.0 + *zDat
99dd0 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20  e - '0';.       
99de0 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 2e 30 3b   rScale *= 10.0;
99df0 0a 20 20 20 20 20 20 20 20 7a 44 61 74 65 2b 2b  .        zDate++
99e00 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
99e10 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b 0a 20 20  ms /= rScale;.  
99e20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
99e30 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 2d   s = 0;.  }.  p-
99e40 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20  >validJD = 0;.  
99e50 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b  p->validHMS = 1;
99e60 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 20 70  .  p->h = h;.  p
99e70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73 20  ->m = m;.  p->s 
99e80 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 66 28 20  = s + ms;.  if( 
99e90 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 7a 44  parseTimezone(zD
99ea0 61 74 65 2c 20 70 29 20 29 20 72 65 74 75 72 6e  ate, p) ) return
99eb0 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54 5a   1;.  p->validTZ
99ec0 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29 3f 31 3a   = (p->tz!=0)?1:
99ed0 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  0;.  return 0;.}
99ee0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
99ef0 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 44 20  from YYYY-MM-DD 
99f00 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c 69  HH:MM:SS to juli
99f10 61 6e 20 64 61 79 2e 20 20 57 65 20 61 6c 77 61  an day.  We alwa
99f20 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 74 68 61  ys assume.** tha
99f30 74 20 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44 44  t the YYYY-MM-DD
99f40 20 69 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f   is according to
99f50 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63   the Gregorian c
99f60 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 52  alendar..**.** R
99f70 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 65 75 73  eference:  Meeus
99f80 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 74 61 74   page 61.*/.stat
99f90 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 4a  ic void computeJ
99fa0 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  D(DateTime *p){.
99fb0 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 41    int Y, M, D, A
99fc0 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a 0a 20 20  , B, X1, X2;..  
99fd0 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a 44 20 29  if( p->validJD )
99fe0 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70   return;.  if( p
99ff0 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b 0a 20 20  ->validYMD ){.  
9a000 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 20 20    Y = p->Y;.    
9a010 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 44 20  M = p->M;.    D 
9a020 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65 7b  = p->D;.  }else{
9a030 0a 20 20 20 20 59 20 3d 20 32 30 30 30 3b 20 20  .    Y = 2000;  
9a040 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 73 70 65  /* If no YMD spe
9a050 63 69 66 69 65 64 2c 20 61 73 73 75 6d 65 20 32  cified, assume 2
9a060 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20 20  000-Jan-01 */.  
9a070 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 44 20 3d    M = 1;.    D =
9a080 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d 3c   1;.  }.  if( M<
9a090 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a 20  =2 ){.    Y--;. 
9a0a0 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d 0a     M += 12;.  }.
9a0b0 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a 20 20 42    A = Y/100;.  B
9a0c0 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 2f 34 29   = 2 - A + (A/4)
9a0d0 3b 0a 20 20 58 31 20 3d 20 33 36 35 32 35 2a 28  ;.  X1 = 36525*(
9a0e0 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a 20 20 58  Y+4716)/100;.  X
9a0f0 32 20 3d 20 33 30 36 30 30 31 2a 28 4d 2b 31 29  2 = 306001*(M+1)
9a100 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e 69 4a 44  /10000;.  p->iJD
9a110 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   = (sqlite3_int6
9a120 34 29 28 28 58 31 20 2b 20 58 32 20 2b 20 44 20  4)((X1 + X2 + D 
9a130 2b 20 42 20 2d 20 31 35 32 34 2e 35 20 29 20 2a  + B - 1524.5 ) *
9a140 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20 70 2d   86400000);.  p-
9a150 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20  >validJD = 1;.  
9a160 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20  if( p->validHMS 
9a170 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d  ){.    p->iJD +=
9a180 20 70 2d 3e 68 2a 33 36 30 30 30 30 30 20 2b 20   p->h*3600000 + 
9a190 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 28 73 71  p->m*60000 + (sq
9a1a0 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 70 2d 3e  lite3_int64)(p->
9a1b0 73 2a 31 30 30 30 29 3b 0a 20 20 20 20 69 66 28  s*1000);.    if(
9a1c0 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20   p->validTZ ){. 
9a1d0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 70       p->iJD -= p
9a1e0 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20 20 20 20  ->tz*60000;.    
9a1f0 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20    p->validYMD = 
9a200 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  0;.      p->vali
9a210 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20 20 20 20  dHMS = 0;.      
9a220 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a  p->validTZ = 0;.
9a230 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
9a240 2a 2a 20 50 61 72 73 65 20 64 61 74 65 73 20 6f  ** Parse dates o
9a250 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a  f the form.**.**
9a260 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20       YYYY-MM-DD 
9a270 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a 20  HH:MM:SS.FFF.** 
9a280 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48      YYYY-MM-DD H
9a290 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 20 59  H:MM:SS.**     Y
9a2a0 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 0a  YYY-MM-DD HH:MM.
9a2b0 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44  **     YYYY-MM-D
9a2c0 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  D.**.** Write th
9a2d0 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 74 68  e result into th
9a2e0 65 20 44 61 74 65 54 69 6d 65 20 73 74 72 75 63  e DateTime struc
9a2f0 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20  ture and return 
9a300 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20  0.** on success 
9a310 61 6e 64 20 31 20 69 66 20 74 68 65 20 69 6e 70  and 1 if the inp
9a320 75 74 20 73 74 72 69 6e 67 20 69 73 20 6e 6f 74  ut string is not
9a330 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a   a well-formed.*
9a340 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69  * date..*/.stati
9a350 63 20 69 6e 74 20 70 61 72 73 65 59 79 79 79 4d  c int parseYyyyM
9a360 6d 44 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  mDd(const char *
9a370 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20  zDate, DateTime 
9a380 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c  *p){.  int Y, M,
9a390 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 28 20   D, neg;..  if( 
9a3a0 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 29 7b  zDate[0]=='-' ){
9a3b0 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20  .    zDate++;.  
9a3c0 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 7d 65 6c    neg = 1;.  }el
9a3d0 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b  se{.    neg = 0;
9a3e0 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 74 44 69  .  }.  if( getDi
9a3f0 67 69 74 73 28 7a 44 61 74 65 2c 34 2c 30 2c 39  gits(zDate,4,0,9
9a400 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c 31  999,'-',&Y,2,1,1
9a410 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31 2c  2,'-',&M,2,1,31,
9a420 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20 20 20 20  0,&D)!=3 ){.    
9a430 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
9a440 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a 20 20 77  zDate += 10;.  w
9a450 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73  hile( sqlite3Iss
9a460 70 61 63 65 28 2a 7a 44 61 74 65 29 20 7c 7c 20  pace(*zDate) || 
9a470 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 74 65  'T'==*(u8*)zDate
9a480 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20   ){ zDate++; }. 
9a490 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73   if( parseHhMmSs
9a4a0 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b  (zDate, p)==0 ){
9a4b0 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f 74 20 74  .    /* We got t
9a4c0 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 7d 65 6c  he time */.  }el
9a4d0 73 65 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 30  se if( *zDate==0
9a4e0 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64   ){.    p->valid
9a4f0 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  HMS = 0;.  }else
9a500 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  {.    return 1;.
9a510 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44    }.  p->validJD
9a520 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64   = 0;.  p->valid
9a530 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d 3e 59 20  YMD = 1;.  p->Y 
9a540 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 59 3b 0a  = neg ? -Y : Y;.
9a550 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70 2d    p->M = M;.  p-
9a560 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 20 70 2d  >D = D;.  if( p-
9a570 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20  >validTZ ){.    
9a580 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
9a590 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
9a5a0 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74  ./*.** Set the t
9a5b0 69 6d 65 20 74 6f 20 74 68 65 20 63 75 72 72 65  ime to the curre
9a5c0 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 74 65 64  nt time reported
9a5d0 20 62 79 20 74 68 65 20 56 46 53 2e 0a 2a 2a 0a   by the VFS..**.
9a5e0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
9a5f0 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a  mber of errors..
9a600 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
9a610 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65  tDateTimeToCurre
9a620 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nt(sqlite3_conte
9a630 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74  xt *context, Dat
9a640 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 73 71 6c  eTime *p){.  sql
9a650 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
9a660 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
9a670 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ndle(context);. 
9a680 20 69 66 28 20 73 71 6c 69 74 65 33 4f 73 43 75   if( sqlite3OsCu
9a690 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 64  rrentTimeInt64(d
9a6a0 62 2d 3e 70 56 66 73 2c 20 26 70 2d 3e 69 4a 44  b->pVfs, &p->iJD
9a6b0 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  )==SQLITE_OK ){.
9a6c0 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d      p->validJD =
9a6d0 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   1;.    return 0
9a6e0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
9a6f0 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 0a  eturn 1;.  }.}..
9a700 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f  /*.** Attempt to
9a710 20 70 61 72 73 65 20 74 68 65 20 67 69 76 65 6e   parse the given
9a720 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 4a   string into a J
9a730 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d 62 65 72  ulian Day Number
9a740 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65  .  Return.** the
9a750 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   number of error
9a760 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  s..**.** The fol
9a770 6c 6f 77 69 6e 67 20 61 72 65 20 61 63 63 65 70  lowing are accep
9a780 74 61 62 6c 65 20 66 6f 72 6d 73 20 66 6f 72 20  table forms for 
9a790 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67  the input string
9a7a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 59 59  :.**.**      YYY
9a7b0 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53  Y-MM-DD HH:MM:SS
9a7c0 2e 46 46 46 20 20 2b 2f 2d 48 48 3a 4d 4d 0a 2a  .FFF  +/-HH:MM.*
9a7d0 2a 20 20 20 20 20 20 44 44 44 44 2e 44 44 20 0a  *      DDDD.DD .
9a7e0 2a 2a 20 20 20 20 20 20 6e 6f 77 0a 2a 2a 0a 2a  **      now.**.*
9a7f0 2a 20 49 6e 20 74 68 65 20 66 69 72 73 74 20 66  * In the first f
9a800 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d 48 48 3a 4d  orm, the +/-HH:M
9a810 4d 20 69 73 20 61 6c 77 61 79 73 20 6f 70 74 69  M is always opti
9a820 6f 6e 61 6c 2e 20 20 54 68 65 20 66 72 61 63 74  onal.  The fract
9a830 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63 6f 6e 64 73  ional.** seconds
9a840 20 65 78 74 65 6e 73 69 6f 6e 20 28 74 68 65 20   extension (the 
9a850 22 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 6f  ".FFF") is optio
9a860 6e 61 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  nal.  The second
9a870 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a 20 28 22 3a  s portion.** (":
9a880 53 53 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69  SS.FFF") is opti
9a890 6f 6e 2e 20 20 54 68 65 20 79 65 61 72 20 61 6e  on.  The year an
9a8a0 64 20 64 61 74 65 20 63 61 6e 20 62 65 20 6f 6d  d date can be om
9a8b0 69 74 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a  itted as long.**
9a8c0 20 61 73 20 74 68 65 72 65 20 69 73 20 61 20 74   as there is a t
9a8d0 69 6d 65 20 73 74 72 69 6e 67 2e 20 20 54 68 65  ime string.  The
9a8e0 20 74 69 6d 65 20 73 74 72 69 6e 67 20 63 61 6e   time string can
9a8f0 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c   be omitted as l
9a900 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20  ong.** as there 
9a910 69 73 20 61 20 79 65 61 72 20 61 6e 64 20 64 61  is a year and da
9a920 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  te..*/.static in
9a930 74 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d  t parseDateOrTim
9a940 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  e(.  sqlite3_con
9a950 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a  text *context, .
9a960 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
9a970 61 74 65 2c 20 0a 20 20 44 61 74 65 54 69 6d 65  ate, .  DateTime
9a980 20 2a 70 0a 29 7b 0a 20 20 64 6f 75 62 6c 65 20   *p.){.  double 
9a990 72 3b 0a 20 20 69 66 28 20 70 61 72 73 65 59 79  r;.  if( parseYy
9a9a0 79 79 4d 6d 44 64 28 7a 44 61 74 65 2c 70 29 3d  yyMmDd(zDate,p)=
9a9b0 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
9a9c0 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
9a9d0 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 61 74  parseHhMmSs(zDat
9a9e0 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20  e, p)==0 ){.    
9a9f0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73  return 0;.  }els
9aa00 65 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72  e if( sqlite3Str
9aa10 49 43 6d 70 28 7a 44 61 74 65 2c 22 6e 6f 77 22  ICmp(zDate,"now"
9aa20 29 3d 3d 30 29 7b 0a 20 20 20 20 72 65 74 75 72  )==0){.    retur
9aa30 6e 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f 43  n setDateTimeToC
9aa40 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c 20  urrent(context, 
9aa50 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  p);.  }else if( 
9aa60 73 71 6c 69 74 65 33 41 74 6f 46 28 7a 44 61 74  sqlite3AtoF(zDat
9aa70 65 2c 20 26 72 2c 20 73 71 6c 69 74 65 33 53 74  e, &r, sqlite3St
9aa80 72 6c 65 6e 33 30 28 7a 44 61 74 65 29 2c 20 53  rlen30(zDate), S
9aa90 51 4c 49 54 45 5f 55 54 46 38 29 20 29 7b 0a 20  QLITE_UTF8) ){. 
9aaa0 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c     p->iJD = (sql
9aab0 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36  ite3_int64)(r*86
9aac0 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b  400000.0 + 0.5);
9aad0 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20  .    p->validJD 
9aae0 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
9aaf0 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
9ab00 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  1;.}../*.** Comp
9ab10 75 74 65 20 74 68 65 20 59 65 61 72 2c 20 4d 6f  ute the Year, Mo
9ab20 6e 74 68 2c 20 61 6e 64 20 44 61 79 20 66 72 6f  nth, and Day fro
9ab30 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79  m the julian day
9ab40 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74   number..*/.stat
9ab50 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 59  ic void computeY
9ab60 4d 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b  MD(DateTime *p){
9ab70 0a 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42 2c 20  .  int Z, A, B, 
9ab80 43 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20 20 69  C, D, E, X1;.  i
9ab90 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29  f( p->validYMD )
9aba0 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21   return;.  if( !
9abb0 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20  p->validJD ){.  
9abc0 20 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b 0a 20    p->Y = 2000;. 
9abd0 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20     p->M = 1;.   
9abe0 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 6c   p->D = 1;.  }el
9abf0 73 65 7b 0a 20 20 20 20 5a 20 3d 20 28 69 6e 74  se{.    Z = (int
9ac00 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30  )((p->iJD + 4320
9ac10 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b  0000)/86400000);
9ac20 0a 20 20 20 20 41 20 3d 20 28 69 6e 74 29 28 28  .    A = (int)((
9ac30 5a 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29 2f  Z - 1867216.25)/
9ac40 33 36 35 32 34 2e 32 35 29 3b 0a 20 20 20 20 41  36524.25);.    A
9ac50 20 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28   = Z + 1 + A - (
9ac60 41 2f 34 29 3b 0a 20 20 20 20 42 20 3d 20 41 20  A/4);.    B = A 
9ac70 2b 20 31 35 32 34 3b 0a 20 20 20 20 43 20 3d 20  + 1524;.    C = 
9ac80 28 69 6e 74 29 28 28 42 20 2d 20 31 32 32 2e 31  (int)((B - 122.1
9ac90 29 2f 33 36 35 2e 32 35 29 3b 0a 20 20 20 20 44  )/365.25);.    D
9aca0 20 3d 20 28 33 36 35 32 35 2a 43 29 2f 31 30 30   = (36525*C)/100
9acb0 3b 0a 20 20 20 20 45 20 3d 20 28 69 6e 74 29 28  ;.    E = (int)(
9acc0 28 42 2d 44 29 2f 33 30 2e 36 30 30 31 29 3b 0a  (B-D)/30.6001);.
9acd0 20 20 20 20 58 31 20 3d 20 28 69 6e 74 29 28 33      X1 = (int)(3
9ace0 30 2e 36 30 30 31 2a 45 29 3b 0a 20 20 20 20 70  0.6001*E);.    p
9acf0 2d 3e 44 20 3d 20 42 20 2d 20 44 20 2d 20 58 31  ->D = B - D - X1
9ad00 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31  ;.    p->M = E<1
9ad10 34 20 3f 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a  4 ? E-1 : E-13;.
9ad20 20 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e      p->Y = p->M>
9ad30 32 20 3f 20 43 20 2d 20 34 37 31 36 20 3a 20 43  2 ? C - 4716 : C
9ad40 20 2d 20 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70   - 4715;.  }.  p
9ad50 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a  ->validYMD = 1;.
9ad60 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  }../*.** Compute
9ad70 20 74 68 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74   the Hour, Minut
9ad80 65 2c 20 61 6e 64 20 53 65 63 6f 6e 64 73 20 66  e, and Seconds f
9ad90 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64  rom the julian d
9ada0 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74  ay number..*/.st
9adb0 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74  atic void comput
9adc0 65 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70  eHMS(DateTime *p
9add0 29 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 66  ){.  int s;.  if
9ade0 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 20  ( p->validHMS ) 
9adf0 72 65 74 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 74  return;.  comput
9ae00 65 4a 44 28 70 29 3b 0a 20 20 73 20 3d 20 28 69  eJD(p);.  s = (i
9ae10 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33  nt)((p->iJD + 43
9ae20 32 30 30 30 30 30 29 20 25 20 38 36 34 30 30 30  200000) % 864000
9ae30 30 30 29 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f  00);.  p->s = s/
9ae40 31 30 30 30 2e 30 3b 0a 20 20 73 20 3d 20 28 69  1000.0;.  s = (i
9ae50 6e 74 29 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20  nt)p->s;.  p->s 
9ae60 2d 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73  -= s;.  p->h = s
9ae70 2f 33 36 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d  /3600;.  s -= p-
9ae80 3e 68 2a 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20  >h*3600;.  p->m 
9ae90 3d 20 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b  = s/60;.  p->s +
9aea0 3d 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20  = s - p->m*60;. 
9aeb0 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31   p->validHMS = 1
9aec0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75  ;.}../*.** Compu
9aed0 74 65 20 62 6f 74 68 20 59 4d 44 20 61 6e 64 20  te both YMD and 
9aee0 48 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  HMS.*/.static vo
9aef0 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d  id computeYMD_HM
9af00 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  S(DateTime *p){.
9af10 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b    computeYMD(p);
9af20 0a 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 70 29  .  computeHMS(p)
9af30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  ;.}../*.** Clear
9af40 20 74 68 65 20 59 4d 44 20 61 6e 64 20 48 4d 53   the YMD and HMS
9af50 20 61 6e 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73   and the TZ.*/.s
9af60 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72  tatic void clear
9af70 59 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54  YMD_HMS_TZ(DateT
9af80 69 6d 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61  ime *p){.  p->va
9af90 6c 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d  lidYMD = 0;.  p-
9afa0 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20  >validHMS = 0;. 
9afb0 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b   p->validTZ = 0;
9afc0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 72 65 63  .}../*.** On rec
9afd0 65 6e 74 20 57 69 6e 64 6f 77 73 20 70 6c 61 74  ent Windows plat
9afe0 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f 63 61 6c  forms, the local
9aff0 74 69 6d 65 5f 73 28 29 20 66 75 6e 63 74 69 6f  time_s() functio
9b000 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a  n is available.*
9b010 2a 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65  * as part of the
9b020 20 22 53 65 63 75 72 65 20 43 52 54 22 2e 20 49   "Secure CRT". I
9b030 74 20 69 73 20 65 73 73 65 6e 74 69 61 6c 6c 79  t is essentially
9b040 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a   equivalent to .
9b050 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 29  ** localtime_r()
9b060 20 61 76 61 69 6c 61 62 6c 65 20 75 6e 64 65 72   available under
9b070 20 6d 6f 73 74 20 50 4f 53 49 58 20 70 6c 61 74   most POSIX plat
9b080 66 6f 72 6d 73 2c 20 65 78 63 65 70 74 20 74 68  forms, except th
9b090 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72 64 65 72  at the .** order
9b0a0 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   of the paramete
9b0b0 72 73 20 69 73 20 72 65 76 65 72 73 65 64 2e 0a  rs is reversed..
9b0c0 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74 70 3a 2f  **.** See http:/
9b0d0 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f 66 74 2e  /msdn.microsoft.
9b0e0 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62 72 61 72  com/en-us/librar
9b0f0 79 2f 61 34 34 32 78 33 79 65 28 56 53 2e 38 30  y/a442x3ye(VS.80
9b100 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a 20 49 66  ).aspx..**.** If
9b110 20 74 68 65 20 75 73 65 72 20 68 61 73 20 6e 6f   the user has no
9b120 74 20 69 6e 64 69 63 61 74 65 64 20 74 6f 20 75  t indicated to u
9b130 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 29  se localtime_r()
9b140 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28   or localtime_s(
9b150 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c 20 63 68  ).** already, ch
9b160 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53 56 43 20  eck for an MSVC 
9b170 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e 6d 65 6e  build environmen
9b180 74 20 74 68 61 74 20 70 72 6f 76 69 64 65 73 20  t that provides 
9b190 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28  .** localtime_s(
9b1a0 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  )..*/.#if !defin
9b1b0 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d  ed(HAVE_LOCALTIM
9b1c0 45 5f 52 29 20 26 26 20 21 64 65 66 69 6e 65 64  E_R) && !defined
9b1d0 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f  (HAVE_LOCALTIME_
9b1e0 53 29 20 26 26 20 5c 0a 20 20 20 20 20 64 65 66  S) && \.     def
9b1f0 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 20 26  ined(_MSC_VER) &
9b200 26 20 64 65 66 69 6e 65 64 28 5f 43 52 54 5f 49  & defined(_CRT_I
9b210 4e 53 45 43 55 52 45 5f 44 45 50 52 45 43 41 54  NSECURE_DEPRECAT
9b220 45 29 0a 23 64 65 66 69 6e 65 20 48 41 56 45 5f  E).#define HAVE_
9b230 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31 0a 23 65  LOCALTIME_S 1.#e
9b240 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
9b250 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54  LITE_OMIT_LOCALT
9b260 49 4d 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  IME./*.** The fo
9b270 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20  llowing routine 
9b280 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 72  implements the r
9b290 6f 75 67 68 20 65 71 75 69 76 61 6c 65 6e 74 20  ough equivalent 
9b2a0 6f 66 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 29  of localtime_r()
9b2b0 0a 2a 2a 20 75 73 69 6e 67 20 77 68 61 74 65 76  .** using whatev
9b2c0 65 72 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73  er operating-sys
9b2d0 74 65 6d 20 73 70 65 63 69 66 69 63 20 6c 6f 63  tem specific loc
9b2e0 61 6c 74 69 6d 65 20 66 61 63 69 6c 69 74 79 20  altime facility 
9b2f0 74 68 61 74 0a 2a 2a 20 69 73 20 61 76 61 69 6c  that.** is avail
9b300 61 62 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74  able.  This rout
9b310 69 6e 65 20 72 65 74 75 72 6e 73 20 30 20 6f 6e  ine returns 0 on
9b320 20 73 75 63 63 65 73 73 20 61 6e 64 0a 2a 2a 20   success and.** 
9b330 6e 6f 6e 2d 7a 65 72 6f 20 6f 6e 20 61 6e 79 20  non-zero on any 
9b340 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e 0a 2a  kind of error..*
9b350 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 71 6c 69  *.** If the sqli
9b360 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9b370 62 4c 6f 63 61 6c 74 69 6d 65 46 61 75 6c 74 20  bLocaltimeFault 
9b380 76 61 72 69 61 62 6c 65 20 69 73 20 74 72 75 65  variable is true
9b390 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f   then this.** ro
9b3a0 75 74 69 6e 65 20 77 69 6c 6c 20 61 6c 77 61 79  utine will alway
9b3b0 73 20 66 61 69 6c 2e 0a 2a 2f 0a 73 74 61 74 69  s fail..*/.stati
9b3c0 63 20 69 6e 74 20 6f 73 4c 6f 63 61 6c 74 69 6d  c int osLocaltim
9b3d0 65 28 74 69 6d 65 5f 74 20 2a 74 2c 20 73 74 72  e(time_t *t, str
9b3e0 75 63 74 20 74 6d 20 2a 70 54 6d 29 7b 0a 20 20  uct tm *pTm){.  
9b3f0 69 6e 74 20 72 63 3b 0a 23 69 66 20 28 21 64 65  int rc;.#if (!de
9b400 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c  fined(HAVE_LOCAL
9b410 54 49 4d 45 5f 52 29 20 7c 7c 20 21 48 41 56 45  TIME_R) || !HAVE
9b420 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 29 20 5c 0a  _LOCALTIME_R) \.
9b430 20 20 20 20 20 20 26 26 20 28 21 64 65 66 69 6e        && (!defin
9b440 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d  ed(HAVE_LOCALTIM
9b450 45 5f 53 29 20 7c 7c 20 21 48 41 56 45 5f 4c 4f  E_S) || !HAVE_LO
9b460 43 41 4c 54 49 4d 45 5f 53 29 0a 20 20 73 74 72  CALTIME_S).  str
9b470 75 63 74 20 74 6d 20 2a 70 58 3b 0a 23 69 66 20  uct tm *pX;.#if 
9b480 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
9b490 45 3e 30 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  E>0.  sqlite3_mu
9b4a0 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c  tex *mutex = sql
9b4b0 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
9b4c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
9b4d0 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64  IC_MASTER);.#end
9b4e0 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  if.  sqlite3_mut
9b4f0 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b  ex_enter(mutex);
9b500 0a 20 20 70 58 20 3d 20 6c 6f 63 61 6c 74 69 6d  .  pX = localtim
9b510 65 28 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  e(t);.#ifndef SQ
9b520 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49  LITE_OMIT_BUILTI
9b530 4e 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c  N_TEST.  if( sql
9b540 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9b550 2e 62 4c 6f 63 61 6c 74 69 6d 65 46 61 75 6c 74  .bLocaltimeFault
9b560 20 29 20 70 58 20 3d 20 30 3b 0a 23 65 6e 64 69   ) pX = 0;.#endi
9b570 66 0a 20 20 69 66 28 20 70 58 20 29 20 2a 70 54  f.  if( pX ) *pT
9b580 6d 20 3d 20 2a 70 58 3b 0a 20 20 73 71 6c 69 74  m = *pX;.  sqlit
9b590 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
9b5a0 75 74 65 78 29 3b 0a 20 20 72 63 20 3d 20 70 58  utex);.  rc = pX
9b5b0 3d 3d 30 3b 0a 23 65 6c 73 65 0a 23 69 66 6e 64  ==0;.#else.#ifnd
9b5c0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
9b5d0 55 49 4c 54 49 4e 5f 54 45 53 54 0a 20 20 69 66  UILTIN_TEST.  if
9b5e0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9b5f0 6f 6e 66 69 67 2e 62 4c 6f 63 61 6c 74 69 6d 65  onfig.bLocaltime
9b600 46 61 75 6c 74 20 29 20 72 65 74 75 72 6e 20 31  Fault ) return 1
9b610 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66  ;.#endif.#if def
9b620 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54  ined(HAVE_LOCALT
9b630 49 4d 45 5f 52 29 20 26 26 20 48 41 56 45 5f 4c  IME_R) && HAVE_L
9b640 4f 43 41 4c 54 49 4d 45 5f 52 0a 20 20 72 63 20  OCALTIME_R.  rc 
9b650 3d 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 74 2c  = localtime_r(t,
9b660 20 70 54 6d 29 3d 3d 30 3b 0a 23 65 6c 73 65 0a   pTm)==0;.#else.
9b670 20 20 72 63 20 3d 20 6c 6f 63 61 6c 74 69 6d 65    rc = localtime
9b680 5f 73 28 70 54 6d 2c 20 74 29 3b 0a 23 65 6e 64  _s(pTm, t);.#end
9b690 69 66 20 2f 2a 20 48 41 56 45 5f 4c 4f 43 41 4c  if /* HAVE_LOCAL
9b6a0 54 49 4d 45 5f 52 20 2a 2f 0a 23 65 6e 64 69 66  TIME_R */.#endif
9b6b0 20 2f 2a 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49   /* HAVE_LOCALTI
9b6c0 4d 45 5f 52 20 7c 7c 20 48 41 56 45 5f 4c 4f 43  ME_R || HAVE_LOC
9b6d0 41 4c 54 49 4d 45 5f 53 20 2a 2f 0a 20 20 72 65  ALTIME_S */.  re
9b6e0 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
9b6f0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
9b700 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a 2f 0a 0a 0a  _LOCALTIME */...
9b710 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
9b720 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 2f 2a  MIT_LOCALTIME./*
9b730 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20  .** Compute the 
9b740 64 69 66 66 65 72 65 6e 63 65 20 28 69 6e 20 6d  difference (in m
9b750 69 6c 6c 69 73 65 63 6f 6e 64 73 29 20 62 65 74  illiseconds) bet
9b760 77 65 65 6e 20 6c 6f 63 61 6c 74 69 6d 65 20 61  ween localtime a
9b770 6e 64 20 55 54 43 0a 2a 2a 20 28 61 2e 6b 2e 61  nd UTC.** (a.k.a
9b780 2e 20 47 4d 54 29 20 66 6f 72 20 74 68 65 20 74  . GMT) for the t
9b790 69 6d 65 20 76 61 6c 75 65 20 70 20 77 68 65 72  ime value p wher
9b7a0 65 20 70 20 69 73 20 69 6e 20 55 54 43 2e 20 49  e p is in UTC. I
9b7b0 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72  f no error occur
9b7c0 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68 69  s,.** return thi
9b7d0 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20  s value and set 
9b7e0 2a 70 52 63 20 74 6f 20 53 51 4c 49 54 45 5f 4f  *pRc to SQLITE_O
9b7f0 4b 2e 20 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 66  K. .**.** Or, if
9b800 20 61 6e 20 65 72 72 6f 72 20 64 6f 65 73 20 6f   an error does o
9b810 63 63 75 72 2c 20 73 65 74 20 2a 70 52 63 20 74  ccur, set *pRc t
9b820 6f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20  o SQLITE_ERROR. 
9b830 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c  The returned val
9b840 75 65 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e  ue.** is undefin
9b850 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ed in this case.
9b860 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74  .*/.static sqlit
9b870 65 33 5f 69 6e 74 36 34 20 6c 6f 63 61 6c 74 69  e3_int64 localti
9b880 6d 65 4f 66 66 73 65 74 28 0a 20 20 44 61 74 65  meOffset(.  Date
9b890 54 69 6d 65 20 2a 70 2c 20 20 20 20 20 20 20 20  Time *p,        
9b8a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
9b8b0 61 74 65 20 61 74 20 77 68 69 63 68 20 74 6f 20  ate at which to 
9b8c0 63 61 6c 63 75 6c 61 74 65 20 6f 66 66 73 65 74  calculate offset
9b8d0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f   */.  sqlite3_co
9b8e0 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20 20 20  ntext *pCtx,    
9b8f0 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65        /* Write e
9b900 72 72 6f 72 20 68 65 72 65 20 69 66 20 6f 6e 65  rror here if one
9b910 20 6f 63 63 75 72 73 20 2a 2f 0a 20 20 69 6e 74   occurs */.  int
9b920 20 2a 70 52 63 20 20 20 20 20 20 20 20 20 20 20   *pRc           
9b930 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
9b940 4f 55 54 3a 20 45 72 72 6f 72 20 63 6f 64 65 2e  OUT: Error code.
9b950 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 45 52   SQLITE_OK or ER
9b960 52 4f 52 20 2a 2f 0a 29 7b 0a 20 20 44 61 74 65  ROR */.){.  Date
9b970 54 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69 6d  Time x, y;.  tim
9b980 65 5f 74 20 74 3b 0a 20 20 73 74 72 75 63 74 20  e_t t;.  struct 
9b990 74 6d 20 73 4c 6f 63 61 6c 3b 0a 0a 20 20 2f 2a  tm sLocal;..  /*
9b9a0 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
9b9b0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 73 4c 6f 63  contents of sLoc
9b9c0 61 6c 20 74 6f 20 61 76 6f 69 64 20 61 20 63 6f  al to avoid a co
9b9d0 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 2e 20  mpiler warning. 
9b9e0 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73 4c 6f  */.  memset(&sLo
9b9f0 63 61 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  cal, 0, sizeof(s
9ba00 4c 6f 63 61 6c 29 29 3b 0a 0a 20 20 78 20 3d 20  Local));..  x = 
9ba10 2a 70 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44  *p;.  computeYMD
9ba20 5f 48 4d 53 28 26 78 29 3b 0a 20 20 69 66 28 20  _HMS(&x);.  if( 
9ba30 78 2e 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59 3e  x.Y<1971 || x.Y>
9ba40 3d 32 30 33 38 20 29 7b 0a 20 20 20 20 78 2e 59  =2038 ){.    x.Y
9ba50 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 78 2e 4d   = 2000;.    x.M
9ba60 20 3d 20 31 3b 0a 20 20 20 20 78 2e 44 20 3d 20   = 1;.    x.D = 
9ba70 31 3b 0a 20 20 20 20 78 2e 68 20 3d 20 30 3b 0a  1;.    x.h = 0;.
9ba80 20 20 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 20      x.m = 0;.   
9ba90 20 78 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 7d 20   x.s = 0.0;.  } 
9baa0 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 73  else {.    int s
9bab0 20 3d 20 28 69 6e 74 29 28 78 2e 73 20 2b 20 30   = (int)(x.s + 0
9bac0 2e 35 29 3b 0a 20 20 20 20 78 2e 73 20 3d 20 73  .5);.    x.s = s
9bad0 3b 0a 20 20 7d 0a 20 20 78 2e 74 7a 20 3d 20 30  ;.  }.  x.tz = 0
9bae0 3b 0a 20 20 78 2e 76 61 6c 69 64 4a 44 20 3d 20  ;.  x.validJD = 
9baf0 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26  0;.  computeJD(&
9bb00 78 29 3b 0a 20 20 74 20 3d 20 28 74 69 6d 65 5f  x);.  t = (time_
9bb10 74 29 28 78 2e 69 4a 44 2f 31 30 30 30 20 2d 20  t)(x.iJD/1000 - 
9bb20 32 31 30 38 36 36 37 36 2a 28 69 36 34 29 31 30  21086676*(i64)10
9bb30 30 30 30 29 3b 0a 20 20 69 66 28 20 6f 73 4c 6f  000);.  if( osLo
9bb40 63 61 6c 74 69 6d 65 28 26 74 2c 20 26 73 4c 6f  caltime(&t, &sLo
9bb50 63 61 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  cal) ){.    sqli
9bb60 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
9bb70 28 70 43 74 78 2c 20 22 6c 6f 63 61 6c 20 74 69  (pCtx, "local ti
9bb80 6d 65 20 75 6e 61 76 61 69 6c 61 62 6c 65 22 2c  me unavailable",
9bb90 20 2d 31 29 3b 0a 20 20 20 20 2a 70 52 63 20 3d   -1);.    *pRc =
9bba0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
9bbb0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
9bbc0 0a 20 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e  .  y.Y = sLocal.
9bbd0 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a  tm_year + 1900;.
9bbe0 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.M = sLocal.t
9bbf0 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 79 2e 44  m_mon + 1;.  y.D
9bc00 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61   = sLocal.tm_mda
9bc10 79 3b 0a 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61  y;.  y.h = sLoca
9bc20 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 79 2e 6d  l.tm_hour;.  y.m
9bc30 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e   = sLocal.tm_min
9bc40 3b 0a 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c  ;.  y.s = sLocal
9bc50 2e 74 6d 5f 73 65 63 3b 0a 20 20 79 2e 76 61 6c  .tm_sec;.  y.val
9bc60 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 79 2e 76  idYMD = 1;.  y.v
9bc70 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 79  alidHMS = 1;.  y
9bc80 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20  .validJD = 0;.  
9bc90 79 2e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20  y.validTZ = 0;. 
9bca0 20 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b 0a   computeJD(&y);.
9bcb0 20 20 2a 70 52 63 20 3d 20 53 51 4c 49 54 45 5f    *pRc = SQLITE_
9bcc0 4f 4b 3b 0a 20 20 72 65 74 75 72 6e 20 79 2e 69  OK;.  return y.i
9bcd0 4a 44 20 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23 65  JD - x.iJD;.}.#e
9bce0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
9bcf0 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a 2f  MIT_LOCALTIME */
9bd00 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20  ../*.** Process 
9bd10 61 20 6d 6f 64 69 66 69 65 72 20 74 6f 20 61 20  a modifier to a 
9bd20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e  date-time stamp.
9bd30 20 20 54 68 65 20 6d 6f 64 69 66 69 65 72 73 20    The modifiers 
9bd40 61 72 65 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77  are.** as follow
9bd50 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e 4e  s:.**.**     NNN
9bd60 20 64 61 79 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e   days.**     NNN
9bd70 20 68 6f 75 72 73 0a 2a 2a 20 20 20 20 20 4e 4e   hours.**     NN
9bd80 4e 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 20 20 20  N minutes.**    
9bd90 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e 64   NNN.NNNN second
9bda0 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f 6e  s.**     NNN mon
9bdb0 74 68 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 79  ths.**     NNN y
9bdc0 65 61 72 73 0a 2a 2a 20 20 20 20 20 73 74 61 72  ears.**     star
9bdd0 74 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20  t of month.**   
9bde0 20 20 73 74 61 72 74 20 6f 66 20 79 65 61 72 0a    start of year.
9bdf0 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20  **     start of 
9be00 77 65 65 6b 0a 2a 2a 20 20 20 20 20 73 74 61 72  week.**     star
9be10 74 20 6f 66 20 64 61 79 0a 2a 2a 20 20 20 20 20  t of day.**     
9be20 77 65 65 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 20  weekday N.**    
9be30 20 75 6e 69 78 65 70 6f 63 68 0a 2a 2a 20 20 20   unixepoch.**   
9be40 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 20    localtime.**  
9be50 20 20 20 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 74     utc.**.** Ret
9be60 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73  urn 0 on success
9be70 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65 20   and 1 if there 
9be80 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65  is any kind of e
9be90 72 72 6f 72 2e 20 49 66 20 74 68 65 20 65 72 72  rror. If the err
9bea0 6f 72 0a 2a 2a 20 69 73 20 69 6e 20 61 20 73 79  or.** is in a sy
9beb0 73 74 65 6d 20 63 61 6c 6c 20 28 69 2e 65 2e 20  stem call (i.e. 
9bec0 6c 6f 63 61 6c 74 69 6d 65 28 29 29 2c 20 74 68  localtime()), th
9bed0 65 6e 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  en an error mess
9bee0 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 0a 2a  age is written.*
9bef0 2a 20 74 6f 20 63 6f 6e 74 65 78 74 20 70 43 74  * to context pCt
9bf00 78 2e 20 49 66 20 74 68 65 20 65 72 72 6f 72 20  x. If the error 
9bf10 69 73 20 61 6e 20 75 6e 72 65 63 6f 67 6e 69 7a  is an unrecogniz
9bf20 65 64 20 6d 6f 64 69 66 69 65 72 2c 20 6e 6f 20  ed modifier, no 
9bf30 65 72 72 6f 72 20 69 73 0a 2a 2a 20 77 72 69 74  error is.** writ
9bf40 74 65 6e 20 74 6f 20 70 43 74 78 2e 0a 2a 2f 0a  ten to pCtx..*/.
9bf50 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65  static int parse
9bf60 4d 6f 64 69 66 69 65 72 28 73 71 6c 69 74 65 33  Modifier(sqlite3
9bf70 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
9bf80 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64  const char *zMod
9bf90 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  , DateTime *p){.
9bfa0 20 20 69 6e 74 20 72 63 20 3d 20 31 3b 0a 20 20    int rc = 1;.  
9bfb0 69 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 6c 65 20  int n;.  double 
9bfc0 72 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 7a 42  r;.  char *z, zB
9bfd0 75 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42  uf[30];.  z = zB
9bfe0 75 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 6e  uf;.  for(n=0; n
9bff0 3c 41 72 72 61 79 53 69 7a 65 28 7a 42 75 66 29  <ArraySize(zBuf)
9c000 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e  -1 && zMod[n]; n
9c010 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d 20 3d 20  ++){.    z[n] = 
9c020 28 63 68 61 72 29 73 71 6c 69 74 65 33 55 70 70  (char)sqlite3Upp
9c030 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29 7a 4d  erToLower[(u8)zM
9c040 6f 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20 20 7a 5b  od[n]];.  }.  z[
9c050 6e 5d 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68  n] = 0;.  switch
9c060 28 20 7a 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65  ( z[0] ){.#ifnde
9c070 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
9c080 43 41 4c 54 49 4d 45 0a 20 20 20 20 63 61 73 65  CALTIME.    case
9c090 20 27 6c 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a   'l': {.      /*
9c0a0 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 20 20      localtime.  
9c0b0 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
9c0c0 41 73 73 75 6d 69 6e 67 20 74 68 65 20 63 75 72  Assuming the cur
9c0d0 72 65 6e 74 20 74 69 6d 65 20 76 61 6c 75 65 20  rent time value 
9c0e0 69 73 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47  is UTC (a.k.a. G
9c0f0 4d 54 29 2c 20 73 68 69 66 74 20 69 74 20 74 6f  MT), shift it to
9c100 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f 77 20 6c  .      ** show l
9c110 6f 63 61 6c 20 74 69 6d 65 2e 0a 20 20 20 20 20  ocal time..     
9c120 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74   */.      if( st
9c130 72 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 6c 74 69  rcmp(z, "localti
9c140 6d 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  me")==0 ){.     
9c150 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b     computeJD(p);
9c160 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
9c170 2b 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73  += localtimeOffs
9c180 65 74 28 70 2c 20 70 43 74 78 2c 20 26 72 63 29  et(p, pCtx, &rc)
9c190 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59  ;.        clearY
9c1a0 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20  MD_HMS_TZ(p);.  
9c1b0 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
9c1c0 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  k;.    }.#endif.
9c1d0 20 20 20 20 63 61 73 65 20 27 75 27 3a 20 7b 0a      case 'u': {.
9c1e0 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a        /*.      *
9c1f0 2a 20 20 20 20 75 6e 69 78 65 70 6f 63 68 0a 20  *    unixepoch. 
9c200 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
9c210 20 54 72 65 61 74 20 74 68 65 20 63 75 72 72 65   Treat the curre
9c220 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69  nt value of p->i
9c230 4a 44 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72  JD as the number
9c240 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 73 65 63   of.      ** sec
9c250 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2e  onds since 1970.
9c260 20 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 20 72    Convert to a r
9c270 65 61 6c 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e  eal julian day n
9c280 75 6d 62 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a  umber..      */.
9c290 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70        if( strcmp
9c2a0 28 7a 2c 20 22 75 6e 69 78 65 70 6f 63 68 22 29  (z, "unixepoch")
9c2b0 3d 3d 30 20 26 26 20 70 2d 3e 76 61 6c 69 64 4a  ==0 && p->validJ
9c2c0 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  D ){.        p->
9c2d0 69 4a 44 20 3d 20 28 70 2d 3e 69 4a 44 20 2b 20  iJD = (p->iJD + 
9c2e0 34 33 32 30 30 29 2f 38 36 34 30 30 20 2b 20 32  43200)/86400 + 2
9c2f0 31 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30  1086676*(i64)100
9c300 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 63  00000;.        c
9c310 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70  learYMD_HMS_TZ(p
9c320 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
9c330 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64  0;.      }.#ifnd
9c340 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
9c350 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 20 20 65  OCALTIME.      e
9c360 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a  lse if( strcmp(z
9c370 2c 20 22 75 74 63 22 29 3d 3d 30 20 29 7b 0a 20  , "utc")==0 ){. 
9c380 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
9c390 6e 74 36 34 20 63 31 3b 0a 20 20 20 20 20 20 20  nt64 c1;.       
9c3a0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
9c3b0 20 20 20 20 20 20 20 63 31 20 3d 20 6c 6f 63 61         c1 = loca
9c3c0 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 2c 20 70  ltimeOffset(p, p
9c3d0 43 74 78 2c 20 26 72 63 29 3b 0a 20 20 20 20 20  Ctx, &rc);.     
9c3e0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
9c3f0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
9c400 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 63 31 3b 0a    p->iJD -= c1;.
9c410 20 20 20 20 20 20 20 20 20 20 63 6c 65 61 72 59            clearY
9c420 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20  MD_HMS_TZ(p);.  
9c430 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b          p->iJD +
9c440 3d 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 6d 65  = c1 - localtime
9c450 4f 66 66 73 65 74 28 70 2c 20 70 43 74 78 2c 20  Offset(p, pCtx, 
9c460 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  &rc);.        }.
9c470 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
9c480 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
9c490 7d 0a 20 20 20 20 63 61 73 65 20 27 77 27 3a 20  }.    case 'w': 
9c4a0 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20  {.      /*.     
9c4b0 20 2a 2a 20 20 20 20 77 65 65 6b 64 61 79 20 4e   **    weekday N
9c4c0 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
9c4d0 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74 65  ** Move the date
9c4e0 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 69 6d   to the same tim
9c4f0 65 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 6f 63  e on the next oc
9c500 63 75 72 72 65 6e 63 65 20 6f 66 0a 20 20 20 20  currence of.    
9c510 20 20 2a 2a 20 77 65 65 6b 64 61 79 20 4e 20 77    ** weekday N w
9c520 68 65 72 65 20 30 3d 3d 53 75 6e 64 61 79 2c 20  here 0==Sunday, 
9c530 31 3d 3d 4d 6f 6e 64 61 79 2c 20 61 6e 64 20 73  1==Monday, and s
9c540 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68 65  o forth.  If the
9c550 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 65 20 69  .      ** date i
9c560 73 20 61 6c 72 65 61 64 79 20 6f 6e 20 74 68 65  s already on the
9c570 20 61 70 70 72 6f 70 72 69 61 74 65 20 77 65 65   appropriate wee
9c580 6b 64 61 79 2c 20 74 68 69 73 20 69 73 20 61 20  kday, this is a 
9c590 6e 6f 2d 6f 70 2e 0a 20 20 20 20 20 20 2a 2f 0a  no-op..      */.
9c5a0 20 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 6d        if( strncm
9c5b0 70 28 7a 2c 20 22 77 65 65 6b 64 61 79 20 22 2c  p(z, "weekday ",
9c5c0 20 38 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20   8)==0.         
9c5d0 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33        && sqlite3
9c5e0 41 74 6f 46 28 26 7a 5b 38 5d 2c 20 26 72 2c 20  AtoF(&z[8], &r, 
9c5f0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
9c600 26 7a 5b 38 5d 29 2c 20 53 51 4c 49 54 45 5f 55  &z[8]), SQLITE_U
9c610 54 46 38 29 0a 20 20 20 20 20 20 20 20 20 20 20  TF8).           
9c620 20 20 20 20 26 26 20 28 6e 3d 28 69 6e 74 29 72      && (n=(int)r
9c630 29 3d 3d 72 20 26 26 20 6e 3e 3d 30 20 26 26 20  )==r && n>=0 && 
9c640 72 3c 37 20 29 7b 0a 20 20 20 20 20 20 20 20 73  r<7 ){.        s
9c650 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 5a 3b 0a  qlite3_int64 Z;.
9c660 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 59          computeY
9c670 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 20  MD_HMS(p);.     
9c680 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20     p->validTZ = 
9c690 30 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61  0;.        p->va
9c6a0 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20  lidJD = 0;.     
9c6b0 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b     computeJD(p);
9c6c0 0a 20 20 20 20 20 20 20 20 5a 20 3d 20 28 28 70  .        Z = ((p
9c6d0 2d 3e 69 4a 44 20 2b 20 31 32 39 36 30 30 30 30  ->iJD + 12960000
9c6e0 30 29 2f 38 36 34 30 30 30 30 30 29 20 25 20 37  0)/86400000) % 7
9c6f0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 5a 3e  ;.        if( Z>
9c700 6e 20 29 20 5a 20 2d 3d 20 37 3b 0a 20 20 20 20  n ) Z -= 7;.    
9c710 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 6e      p->iJD += (n
9c720 20 2d 20 5a 29 2a 38 36 34 30 30 30 30 30 3b 0a   - Z)*86400000;.
9c730 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44          clearYMD
9c740 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20  _HMS_TZ(p);.    
9c750 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20      rc = 0;.    
9c760 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
9c770 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
9c780 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a  's': {.      /*.
9c790 20 20 20 20 20 20 2a 2a 20 20 20 20 73 74 61 72        **    star
9c7a0 74 20 6f 66 20 54 54 54 54 54 0a 20 20 20 20 20  t of TTTTT.     
9c7b0 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d 6f 76   **.      ** Mov
9c7c0 65 20 74 68 65 20 64 61 74 65 20 62 61 63 6b 77  e the date backw
9c7d0 61 72 64 73 20 74 6f 20 74 68 65 20 62 65 67 69  ards to the begi
9c7e0 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 63 75 72  nning of the cur
9c7f0 72 65 6e 74 20 64 61 79 2c 0a 20 20 20 20 20 20  rent day,.      
9c800 2a 2a 20 6f 72 20 6d 6f 6e 74 68 20 6f 72 20 79  ** or month or y
9c810 65 61 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ear..      */.  
9c820 20 20 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28      if( strncmp(
9c830 7a 2c 20 22 73 74 61 72 74 20 6f 66 20 22 2c 20  z, "start of ", 
9c840 39 29 21 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  9)!=0 ) break;. 
9c850 20 20 20 20 20 7a 20 2b 3d 20 39 3b 0a 20 20 20       z += 9;.   
9c860 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29     computeYMD(p)
9c870 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64  ;.      p->valid
9c880 48 4d 53 20 3d 20 31 3b 0a 20 20 20 20 20 20 70  HMS = 1;.      p
9c890 2d 3e 68 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b 0a  ->h = p->m = 0;.
9c8a0 20 20 20 20 20 20 70 2d 3e 73 20 3d 20 30 2e 30        p->s = 0.0
9c8b0 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64  ;.      p->valid
9c8c0 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  TZ = 0;.      p-
9c8d0 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20  >validJD = 0;.  
9c8e0 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a      if( strcmp(z
9c8f0 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29 7b 0a  ,"month")==0 ){.
9c900 20 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 31          p->D = 1
9c910 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30  ;.        rc = 0
9c920 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
9c930 28 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 72  ( strcmp(z,"year
9c940 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
9c950 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a   computeYMD(p);.
9c960 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 3d 20 31          p->M = 1
9c970 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20 3d  ;.        p->D =
9c980 20 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   1;.        rc =
9c990 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20   0;.      }else 
9c9a0 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 64 61  if( strcmp(z,"da
9c9b0 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  y")==0 ){.      
9c9c0 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20    rc = 0;.      
9c9d0 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
9c9e0 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2b     }.    case '+
9c9f0 27 3a 0a 20 20 20 20 63 61 73 65 20 27 2d 27 3a  ':.    case '-':
9ca00 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a 0a 20  .    case '0':. 
9ca10 20 20 20 63 61 73 65 20 27 31 27 3a 0a 20 20 20     case '1':.   
9ca20 20 63 61 73 65 20 27 32 27 3a 0a 20 20 20 20 63   case '2':.    c
9ca30 61 73 65 20 27 33 27 3a 0a 20 20 20 20 63 61 73  ase '3':.    cas
9ca40 65 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65 20  e '4':.    case 
9ca50 27 35 27 3a 0a 20 20 20 20 63 61 73 65 20 27 36  '5':.    case '6
9ca60 27 3a 0a 20 20 20 20 63 61 73 65 20 27 37 27 3a  ':.    case '7':
9ca70 0a 20 20 20 20 63 61 73 65 20 27 38 27 3a 0a 20  .    case '8':. 
9ca80 20 20 20 63 61 73 65 20 27 39 27 3a 20 7b 0a 20     case '9': {. 
9ca90 20 20 20 20 20 64 6f 75 62 6c 65 20 72 52 6f 75       double rRou
9caa0 6e 64 65 72 3b 0a 20 20 20 20 20 20 66 6f 72 28  nder;.      for(
9cab0 6e 3d 31 3b 20 7a 5b 6e 5d 20 26 26 20 7a 5b 6e  n=1; z[n] && z[n
9cac0 5d 21 3d 27 3a 27 20 26 26 20 21 73 71 6c 69 74  ]!=':' && !sqlit
9cad0 65 33 49 73 73 70 61 63 65 28 7a 5b 6e 5d 29 3b  e3Isspace(z[n]);
9cae0 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 69 66   n++){}.      if
9caf0 28 20 21 73 71 6c 69 74 65 33 41 74 6f 46 28 7a  ( !sqlite3AtoF(z
9cb00 2c 20 26 72 2c 20 6e 2c 20 53 51 4c 49 54 45 5f  , &r, n, SQLITE_
9cb10 55 54 46 38 29 20 29 7b 0a 20 20 20 20 20 20 20  UTF8) ){.       
9cb20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20   rc = 1;.       
9cb30 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
9cb40 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 5d 3d 3d        if( z[n]==
9cb50 27 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ':' ){.        /
9cb60 2a 20 41 20 6d 6f 64 69 66 69 65 72 20 6f 66 20  * A modifier of 
9cb70 74 68 65 20 66 6f 72 6d 20 28 2b 7c 2d 29 48 48  the form (+|-)HH
9cb80 3a 4d 4d 3a 53 53 2e 46 46 46 20 61 64 64 73 20  :MM:SS.FFF adds 
9cb90 28 6f 72 20 73 75 62 74 72 61 63 74 73 29 20 74  (or subtracts) t
9cba0 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 70  he.        ** sp
9cbb0 65 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f  ecified number o
9cbc0 66 20 68 6f 75 72 73 2c 20 6d 69 6e 75 74 65 73  f hours, minutes
9cbd0 2c 20 73 65 63 6f 6e 64 73 2c 20 61 6e 64 20 66  , seconds, and f
9cbe0 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64  ractional second
9cbf0 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20  s.        ** to 
9cc00 74 68 65 20 74 69 6d 65 2e 20 20 54 68 65 20 22  the time.  The "
9cc10 2e 46 46 46 22 20 6d 61 79 20 62 65 20 6f 6d 69  .FFF" may be omi
9cc20 74 74 65 64 2e 20 20 54 68 65 20 22 3a 53 53 2e  tted.  The ":SS.
9cc30 46 46 46 22 20 6d 61 79 20 62 65 0a 20 20 20 20  FFF" may be.    
9cc40 20 20 20 20 2a 2a 20 6f 6d 69 74 74 65 64 2e 0a      ** omitted..
9cc50 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
9cc60 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
9cc70 32 20 3d 20 7a 3b 0a 20 20 20 20 20 20 20 20 44  2 = z;.        D
9cc80 61 74 65 54 69 6d 65 20 74 78 3b 0a 20 20 20 20  ateTime tx;.    
9cc90 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
9cca0 34 20 64 61 79 3b 0a 20 20 20 20 20 20 20 20 69  4 day;.        i
9ccb0 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67  f( !sqlite3Isdig
9ccc0 69 74 28 2a 7a 32 29 20 29 20 7a 32 2b 2b 3b 0a  it(*z2) ) z2++;.
9ccd0 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26          memset(&
9cce0 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 74 78  tx, 0, sizeof(tx
9ccf0 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ));.        if( 
9cd00 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 32 2c 20  parseHhMmSs(z2, 
9cd10 26 74 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20  &tx) ) break;.  
9cd20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
9cd30 26 74 78 29 3b 0a 20 20 20 20 20 20 20 20 74 78  &tx);.        tx
9cd40 2e 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 30 30  .iJD -= 43200000
9cd50 3b 0a 20 20 20 20 20 20 20 20 64 61 79 20 3d 20  ;.        day = 
9cd60 74 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 3b  tx.iJD/86400000;
9cd70 0a 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44 20  .        tx.iJD 
9cd80 2d 3d 20 64 61 79 2a 38 36 34 30 30 30 30 30 3b  -= day*86400000;
9cd90 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 30  .        if( z[0
9cda0 5d 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a 44 20  ]=='-' ) tx.iJD 
9cdb0 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20  = -tx.iJD;.     
9cdc0 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b     computeJD(p);
9cdd0 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d  .        clearYM
9cde0 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20  D_HMS_TZ(p);.   
9cdf0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 74       p->iJD += t
9ce00 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20 20 72  x.iJD;.        r
9ce10 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62  c = 0;.        b
9ce20 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
9ce30 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20 20      z += n;.    
9ce40 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
9ce50 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b  Isspace(*z) ) z+
9ce60 2b 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c  +;.      n = sql
9ce70 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b  ite3Strlen30(z);
9ce80 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 31 30 20  .      if( n>10 
9ce90 7c 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 3b 0a  || n<3 ) break;.
9cea0 20 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d        if( z[n-1]
9ceb0 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20  =='s' ){ z[n-1] 
9cec0 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20  = 0; n--; }.    
9ced0 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
9cee0 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20        rc = 0;.  
9cef0 20 20 20 20 72 52 6f 75 6e 64 65 72 20 3d 20 72      rRounder = r
9cf00 3c 30 20 3f 20 2d 30 2e 35 20 3a 20 2b 30 2e 35  <0 ? -0.5 : +0.5
9cf10 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 33  ;.      if( n==3
9cf20 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 64 61   && strcmp(z,"da
9cf30 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  y")==0 ){.      
9cf40 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c    p->iJD += (sql
9cf50 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36  ite3_int64)(r*86
9cf60 34 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e  400000.0 + rRoun
9cf70 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  der);.      }els
9cf80 65 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 73 74  e if( n==4 && st
9cf90 72 63 6d 70 28 7a 2c 22 68 6f 75 72 22 29 3d 3d  rcmp(z,"hour")==
9cfa0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  0 ){.        p->
9cfb0 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f  iJD += (sqlite3_
9cfc0 69 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30  int64)(r*(864000
9cfd0 30 30 2e 30 2f 32 34 2e 30 29 20 2b 20 72 52 6f  00.0/24.0) + rRo
9cfe0 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65  under);.      }e
9cff0 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20  lse if( n==6 && 
9d000 73 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 74 65  strcmp(z,"minute
9d010 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
9d020 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69   p->iJD += (sqli
9d030 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36  te3_int64)(r*(86
9d040 34 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36  400000.0/(24.0*6
9d050 30 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72  0.0)) + rRounder
9d060 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
9d070 66 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d  f( n==6 && strcm
9d080 70 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30  p(z,"second")==0
9d090 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   ){.        p->i
9d0a0 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69  JD += (sqlite3_i
9d0b0 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30  nt64)(r*(8640000
9d0c0 30 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36  0.0/(24.0*60.0*6
9d0d0 30 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72  0.0)) + rRounder
9d0e0 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
9d0f0 66 28 20 6e 3d 3d 35 20 26 26 20 73 74 72 63 6d  f( n==5 && strcm
9d100 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20  p(z,"month")==0 
9d110 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78  ){.        int x
9d120 2c 20 79 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d  , y;.        com
9d130 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a  puteYMD_HMS(p);.
9d140 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2b 3d 20          p->M += 
9d150 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20  (int)r;.        
9d160 78 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d  x = p->M>0 ? (p-
9d170 3e 4d 2d 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d  >M-1)/12 : (p->M
9d180 2d 31 32 29 2f 31 32 3b 0a 20 20 20 20 20 20 20  -12)/12;.       
9d190 20 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 20 20   p->Y += x;.    
9d1a0 20 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32      p->M -= x*12
9d1b0 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c  ;.        p->val
9d1c0 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20  idJD = 0;.      
9d1d0 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
9d1e0 20 20 20 20 20 20 20 20 79 20 3d 20 28 69 6e 74          y = (int
9d1f0 29 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  )r;.        if( 
9d200 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20  y!=r ){.        
9d210 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c    p->iJD += (sql
9d220 69 74 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d  ite3_int64)((r -
9d230 20 79 29 2a 33 30 2e 30 2a 38 36 34 30 30 30 30   y)*30.0*8640000
9d240 30 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b  0.0 + rRounder);
9d250 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
9d260 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20   }else if( n==4 
9d270 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61  && strcmp(z,"yea
9d280 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  r")==0 ){.      
9d290 20 20 69 6e 74 20 79 20 3d 20 28 69 6e 74 29 72    int y = (int)r
9d2a0 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74  ;.        comput
9d2b0 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20  eYMD_HMS(p);.   
9d2c0 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 79 3b 0a       p->Y += y;.
9d2d0 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64          p->valid
9d2e0 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  JD = 0;.        
9d2f0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
9d300 20 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29        if( y!=r )
9d310 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69  {.          p->i
9d320 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69  JD += (sqlite3_i
9d330 6e 74 36 34 29 28 28 72 20 2d 20 79 29 2a 33 36  nt64)((r - y)*36
9d340 35 2e 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b  5.0*86400000.0 +
9d350 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20   rRounder);.    
9d360 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
9d370 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  e{.        rc = 
9d380 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
9d390 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a   clearYMD_HMS_TZ
9d3a0 28 70 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  (p);.      break
9d3b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
9d3c0 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65  ult: {.      bre
9d3d0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
9d3e0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
9d3f0 0a 2a 2a 20 50 72 6f 63 65 73 73 20 74 69 6d 65  .** Process time
9d400 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
9d410 6e 74 73 2e 20 20 61 72 67 76 5b 30 5d 20 69 73  nts.  argv[0] is
9d420 20 61 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61   a date-time sta
9d430 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b 31 5d 20 61  mp..** argv[1] a
9d440 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  nd following are
9d450 20 6d 6f 64 69 66 69 65 72 73 2e 20 20 50 61 72   modifiers.  Par
9d460 73 65 20 74 68 65 6d 20 61 6c 6c 20 61 6e 64 20  se them all and 
9d470 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 72 65 73  write.** the res
9d480 75 6c 74 69 6e 67 20 74 69 6d 65 20 69 6e 74 6f  ulting time into
9d490 20 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74   the DateTime st
9d4a0 72 75 63 74 75 72 65 20 70 2e 20 20 52 65 74 75  ructure p.  Retu
9d4b0 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65  rn 0.** on succe
9d4c0 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72  ss and 1 if ther
9d4d0 65 20 61 72 65 20 61 6e 79 20 65 72 72 6f 72 73  e are any errors
9d4e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ..**.** If there
9d4f0 20 61 72 65 20 7a 65 72 6f 20 70 61 72 61 6d 65   are zero parame
9d500 74 65 72 73 20 28 69 66 20 65 76 65 6e 20 61 72  ters (if even ar
9d510 67 76 5b 30 5d 20 69 73 20 75 6e 64 65 66 69 6e  gv[0] is undefin
9d520 65 64 29 0a 2a 2a 20 74 68 65 6e 20 61 73 73 75  ed).** then assu
9d530 6d 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c  me a default val
9d540 75 65 20 6f 66 20 22 6e 6f 77 22 20 66 6f 72 20  ue of "now" for 
9d550 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 74 61 74  argv[0]..*/.stat
9d560 69 63 20 69 6e 74 20 69 73 44 61 74 65 28 0a 20  ic int isDate(. 
9d570 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
9d580 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e   *context, .  in
9d590 74 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74  t argc, .  sqlit
9d5a0 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 2c  e3_value **argv,
9d5b0 20 0a 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a   .  DateTime *p.
9d5c0 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f  ){.  int i;.  co
9d5d0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
9d5e0 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 65 54 79 70  r *z;.  int eTyp
9d5f0 65 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  e;.  memset(p, 0
9d600 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20  , sizeof(*p));. 
9d610 20 69 66 28 20 61 72 67 63 3d 3d 30 20 29 7b 0a   if( argc==0 ){.
9d620 20 20 20 20 72 65 74 75 72 6e 20 73 65 74 44 61      return setDa
9d630 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 28  teTimeToCurrent(
9d640 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a 20 20 7d  context, p);.  }
9d650 0a 20 20 69 66 28 20 28 65 54 79 70 65 20 3d 20  .  if( (eType = 
9d660 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
9d670 70 65 28 61 72 67 76 5b 30 5d 29 29 3d 3d 53 51  pe(argv[0]))==SQ
9d680 4c 49 54 45 5f 46 4c 4f 41 54 0a 20 20 20 20 20  LITE_FLOAT.     
9d690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
9d6a0 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49   eType==SQLITE_I
9d6b0 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70 2d  NTEGER ){.    p-
9d6c0 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f  >iJD = (sqlite3_
9d6d0 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 76  int64)(sqlite3_v
9d6e0 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76  alue_double(argv
9d6f0 5b 30 5d 29 2a 38 36 34 30 30 30 30 30 2e 30 20  [0])*86400000.0 
9d700 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70 2d 3e 76  + 0.5);.    p->v
9d710 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 7d 65  alidJD = 1;.  }e
9d720 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c  lse{.    z = sql
9d730 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
9d740 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66  argv[0]);.    if
9d750 28 20 21 7a 20 7c 7c 20 70 61 72 73 65 44 61 74  ( !z || parseDat
9d760 65 4f 72 54 69 6d 65 28 63 6f 6e 74 65 78 74 2c  eOrTime(context,
9d770 20 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b   (char*)z, p) ){
9d780 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
9d790 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72  .    }.  }.  for
9d7a0 28 69 3d 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b  (i=1; i<argc; i+
9d7b0 2b 29 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69  +){.    z = sqli
9d7c0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
9d7d0 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[i]);.    if(
9d7e0 20 7a 3d 3d 30 20 7c 7c 20 70 61 72 73 65 4d 6f   z==0 || parseMo
9d7f0 64 69 66 69 65 72 28 63 6f 6e 74 65 78 74 2c 20  difier(context, 
9d800 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 20 72  (char*)z, p) ) r
9d810 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72  eturn 1;.  }.  r
9d820 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn 0;.}.../*.
9d830 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
9d840 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d   routines implem
9d850 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 73 20  ent the various 
9d860 64 61 74 65 20 61 6e 64 20 74 69 6d 65 20 66 75  date and time fu
9d870 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51  nctions.** of SQ
9d880 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Lite..*/../*.** 
9d890 20 20 20 6a 75 6c 69 61 6e 64 61 79 28 20 54 49     julianday( TI
9d8a0 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d  MESTRING, MOD, M
9d8b0 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52  OD, ...).**.** R
9d8c0 65 74 75 72 6e 20 74 68 65 20 6a 75 6c 69 61 6e  eturn the julian
9d8d0 20 64 61 79 20 6e 75 6d 62 65 72 20 6f 66 20 74   day number of t
9d8e0 68 65 20 64 61 74 65 20 73 70 65 63 69 66 69 65  he date specifie
9d8f0 64 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e  d in the argumen
9d900 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ts.*/.static voi
9d910 64 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 28  d juliandayFunc(
9d920 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
9d930 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
9d940 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
9d950 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
9d960 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b  ){.  DateTime x;
9d970 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 6f  .  if( isDate(co
9d980 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67  ntext, argc, arg
9d990 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20  v, &x)==0 ){.   
9d9a0 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a   computeJD(&x);.
9d9b0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
9d9c0 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78  lt_double(contex
9d9d0 74 2c 20 78 2e 69 4a 44 2f 38 36 34 30 30 30 30  t, x.iJD/8640000
9d9e0 30 2e 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  0.0);.  }.}../*.
9d9f0 2a 2a 20 20 20 20 64 61 74 65 74 69 6d 65 28 20  **    datetime( 
9da00 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c  TIMESTRING, MOD,
9da10 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a   MOD, ...).**.**
9da20 20 52 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d   Return YYYY-MM-
9da30 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73  DD HH:MM:SS.*/.s
9da40 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 65 74  tatic void datet
9da50 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  imeFunc(.  sqlit
9da60 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
9da70 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
9da80 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
9da90 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74   **argv.){.  Dat
9daa0 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69  eTime x;.  if( i
9dab0 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61  sDate(context, a
9dac0 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d  rgc, argv, &x)==
9dad0 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42  0 ){.    char zB
9dae0 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d  uf[100];.    com
9daf0 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b  puteYMD_HMS(&x);
9db00 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
9db10 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75  rintf(sizeof(zBu
9db20 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d  f), zBuf, "%04d-
9db30 25 30 32 64 2d 25 30 32 64 20 25 30 32 64 3a 25  %02d-%02d %02d:%
9db40 30 32 64 3a 25 30 32 64 22 2c 0a 20 20 20 20 20  02d:%02d",.     
9db50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9db60 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c 20 78  x.Y, x.M, x.D, x
9db70 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 28 78  .h, x.m, (int)(x
9db80 2e 73 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  .s));.    sqlite
9db90 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
9dba0 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c  ntext, zBuf, -1,
9dbb0 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
9dbc0 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  T);.  }.}../*.**
9dbd0 20 20 20 20 74 69 6d 65 28 20 54 49 4d 45 53 54      time( TIMEST
9dbe0 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20  RING, MOD, MOD, 
9dbf0 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ...).**.** Retur
9dc00 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74  n HH:MM:SS.*/.st
9dc10 61 74 69 63 20 76 6f 69 64 20 74 69 6d 65 46 75  atic void timeFu
9dc20 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
9dc30 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
9dc40 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
9dc50 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
9dc60 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65  gv.){.  DateTime
9dc70 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65   x;.  if( isDate
9dc80 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20  (context, argc, 
9dc90 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a  argv, &x)==0 ){.
9dca0 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30      char zBuf[10
9dcb0 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 48  0];.    computeH
9dcc0 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69  MS(&x);.    sqli
9dcd0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
9dce0 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c  eof(zBuf), zBuf,
9dcf0 20 22 25 30 32 64 3a 25 30 32 64 3a 25 30 32 64   "%02d:%02d:%02d
9dd00 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e  ", x.h, x.m, (in
9dd10 74 29 78 2e 73 29 3b 0a 20 20 20 20 73 71 6c 69  t)x.s);.    sqli
9dd20 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
9dd30 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d  context, zBuf, -
9dd40 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  1, SQLITE_TRANSI
9dd50 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ENT);.  }.}../*.
9dd60 2a 2a 20 20 20 20 64 61 74 65 28 20 54 49 4d 45  **    date( TIME
9dd70 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44  STRING, MOD, MOD
9dd80 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74  , ...).**.** Ret
9dd90 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a  urn YYYY-MM-DD.*
9dda0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61  /.static void da
9ddb0 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  teFunc(.  sqlite
9ddc0 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
9ddd0 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
9dde0 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
9ddf0 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65  **argv.){.  Date
9de00 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73  Time x;.  if( is
9de10 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  Date(context, ar
9de20 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30  gc, argv, &x)==0
9de30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75   ){.    char zBu
9de40 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70  f[100];.    comp
9de50 75 74 65 59 4d 44 28 26 78 29 3b 0a 20 20 20 20  uteYMD(&x);.    
9de60 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
9de70 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a  (sizeof(zBuf), z
9de80 42 75 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d  Buf, "%04d-%02d-
9de90 25 30 32 64 22 2c 20 78 2e 59 2c 20 78 2e 4d 2c  %02d", x.Y, x.M,
9dea0 20 78 2e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74   x.D);.    sqlit
9deb0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
9dec0 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31  ontext, zBuf, -1
9ded0 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
9dee0 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  NT);.  }.}../*.*
9def0 2a 20 20 20 20 73 74 72 66 74 69 6d 65 28 20 46  *    strftime( F
9df00 4f 52 4d 41 54 2c 20 54 49 4d 45 53 54 52 49 4e  ORMAT, TIMESTRIN
9df10 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e  G, MOD, MOD, ...
9df20 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ).**.** Return a
9df30 20 73 74 72 69 6e 67 20 64 65 73 63 72 69 62 65   string describe
9df40 64 20 62 79 20 46 4f 52 4d 41 54 2e 20 20 43 6f  d by FORMAT.  Co
9df50 6e 76 65 72 73 69 6f 6e 73 20 61 73 20 66 6f 6c  nversions as fol
9df60 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 25 64  lows:.**.**   %d
9df70 20 20 64 61 79 20 6f 66 20 6d 6f 6e 74 68 0a 2a    day of month.*
9df80 2a 20 20 20 25 66 20 20 2a 2a 20 66 72 61 63 74  *   %f  ** fract
9df90 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 20 53  ional seconds  S
9dfa0 53 2e 53 53 53 0a 2a 2a 20 20 20 25 48 20 20 68  S.SSS.**   %H  h
9dfb0 6f 75 72 20 30 30 2d 32 34 0a 2a 2a 20 20 20 25  our 00-24.**   %
9dfc0 6a 20 20 64 61 79 20 6f 66 20 79 65 61 72 20 30  j  day of year 0
9dfd0 30 30 2d 33 36 36 0a 2a 2a 20 20 20 25 4a 20 20  00-366.**   %J  
9dfe0 2a 2a 20 4a 75 6c 69 61 6e 20 64 61 79 20 6e 75  ** Julian day nu
9dff0 6d 62 65 72 0a 2a 2a 20 20 20 25 6d 20 20 6d 6f  mber.**   %m  mo
9e000 6e 74 68 20 30 31 2d 31 32 0a 2a 2a 20 20 20 25  nth 01-12.**   %
9e010 4d 20 20 6d 69 6e 75 74 65 20 30 30 2d 35 39 0a  M  minute 00-59.
9e020 2a 2a 20 20 20 25 73 20 20 73 65 63 6f 6e 64 73  **   %s  seconds
9e030 20 73 69 6e 63 65 20 31 39 37 30 2d 30 31 2d 30   since 1970-01-0
9e040 31 0a 2a 2a 20 20 20 25 53 20 20 73 65 63 6f 6e  1.**   %S  secon
9e050 64 73 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 77  ds 00-59.**   %w
9e060 20 20 64 61 79 20 6f 66 20 77 65 65 6b 20 30 2d    day of week 0-
9e070 36 20 20 73 75 6e 64 61 79 3d 3d 30 0a 2a 2a 20  6  sunday==0.** 
9e080 20 20 25 57 20 20 77 65 65 6b 20 6f 66 20 79 65    %W  week of ye
9e090 61 72 20 30 30 2d 35 33 0a 2a 2a 20 20 20 25 59  ar 00-53.**   %Y
9e0a0 20 20 79 65 61 72 20 30 30 30 30 2d 39 39 39 39    year 0000-9999
9e0b0 0a 2a 2a 20 20 20 25 25 20 20 25 0a 2a 2f 0a 73  .**   %%  %.*/.s
9e0c0 74 61 74 69 63 20 76 6f 69 64 20 73 74 72 66 74  tatic void strft
9e0d0 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  imeFunc(.  sqlit
9e0e0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
9e0f0 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
9e100 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
9e110 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74   **argv.){.  Dat
9e120 65 54 69 6d 65 20 78 3b 0a 20 20 75 36 34 20 6e  eTime x;.  u64 n
9e130 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 6a 3b 0a  ;.  size_t i,j;.
9e140 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 71 6c    char *z;.  sql
9e150 69 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73  ite3 *db;.  cons
9e160 74 20 63 68 61 72 20 2a 7a 46 6d 74 20 3d 20 28  t char *zFmt = (
9e170 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69  const char*)sqli
9e180 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
9e190 72 67 76 5b 30 5d 29 3b 0a 20 20 63 68 61 72 20  rgv[0]);.  char 
9e1a0 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 69 66 28  zBuf[100];.  if(
9e1b0 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 69 73 44 61   zFmt==0 || isDa
9e1c0 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63  te(context, argc
9e1d0 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26 78 29 20  -1, argv+1, &x) 
9e1e0 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d  ) return;.  db =
9e1f0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
9e200 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
9e210 78 74 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  xt);.  for(i=0, 
9e220 6e 3d 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b  n=1; zFmt[i]; i+
9e230 2b 2c 20 6e 2b 2b 29 7b 0a 20 20 20 20 69 66 28  +, n++){.    if(
9e240 20 7a 46 6d 74 5b 69 5d 3d 3d 27 25 27 20 29 7b   zFmt[i]=='%' ){
9e250 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20 7a  .      switch( z
9e260 46 6d 74 5b 69 2b 31 5d 20 29 7b 0a 20 20 20 20  Fmt[i+1] ){.    
9e270 20 20 20 20 63 61 73 65 20 27 64 27 3a 0a 20 20      case 'd':.  
9e280 20 20 20 20 20 20 63 61 73 65 20 27 48 27 3a 0a        case 'H':.
9e290 20 20 20 20 20 20 20 20 63 61 73 65 20 27 6d 27          case 'm'
9e2a0 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  :.        case '
9e2b0 4d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65  M':.        case
9e2c0 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20 63 61   'S':.        ca
9e2d0 73 65 20 27 57 27 3a 0a 20 20 20 20 20 20 20 20  se 'W':.        
9e2e0 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    n++;.         
9e2f0 20 2f 2a 20 66 61 6c 6c 20 74 68 72 75 20 2a 2f   /* fall thru */
9e300 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 77  .        case 'w
9e310 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  ':.        case 
9e320 27 25 27 3a 0a 20 20 20 20 20 20 20 20 20 20 62  '%':.          b
9e330 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
9e340 73 65 20 27 66 27 3a 0a 20 20 20 20 20 20 20 20  se 'f':.        
9e350 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20 20    n += 8;.      
9e360 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
9e370 20 20 20 63 61 73 65 20 27 6a 27 3a 0a 20 20 20     case 'j':.   
9e380 20 20 20 20 20 20 20 6e 20 2b 3d 20 33 3b 0a 20         n += 3;. 
9e390 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
9e3a0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 59 27          case 'Y'
9e3b0 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d  :.          n +=
9e3c0 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72   8;.          br
9e3d0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
9e3e0 65 20 27 73 27 3a 0a 20 20 20 20 20 20 20 20 63  e 's':.        c
9e3f0 61 73 65 20 27 4a 27 3a 0a 20 20 20 20 20 20 20  ase 'J':.       
9e400 20 20 20 6e 20 2b 3d 20 35 30 3b 0a 20 20 20 20     n += 50;.    
9e410 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
9e420 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20       default:.  
9e430 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 20          return; 
9e440 20 2f 2a 20 45 52 52 4f 52 2e 20 20 72 65 74 75   /* ERROR.  retu
9e450 72 6e 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20  rn a NULL */.   
9e460 20 20 20 7d 0a 20 20 20 20 20 20 69 2b 2b 3b 0a     }.      i++;.
9e470 20 20 20 20 7d 0a 20 20 7d 0a 20 20 74 65 73 74      }.  }.  test
9e480 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28  case( n==sizeof(
9e490 7a 42 75 66 29 2d 31 20 29 3b 0a 20 20 74 65 73  zBuf)-1 );.  tes
9e4a0 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66  tcase( n==sizeof
9e4b0 28 7a 42 75 66 29 20 29 3b 0a 20 20 74 65 73 74  (zBuf) );.  test
9e4c0 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29 64 62  case( n==(u64)db
9e4d0 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
9e4e0 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20  LIMIT_LENGTH]+1 
9e4f0 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e  );.  testcase( n
9e500 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69  ==(u64)db->aLimi
9e510 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
9e520 45 4e 47 54 48 5d 20 29 3b 0a 20 20 69 66 28 20  ENGTH] );.  if( 
9e530 6e 3c 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 29  n<sizeof(zBuf) )
9e540 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a  {.    z = zBuf;.
9e550 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e 28 75    }else if( n>(u
9e560 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  64)db->aLimit[SQ
9e570 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
9e580 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  H] ){.    sqlite
9e590 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  3_result_error_t
9e5a0 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a  oobig(context);.
9e5b0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65      return;.  }e
9e5c0 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c  lse{.    z = sql
9e5d0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
9e5e0 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 20  db, (int)n);.   
9e5f0 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20   if( z==0 ){.   
9e600 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
9e610 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f  t_error_nomem(co
9e620 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 72 65  ntext);.      re
9e630 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
9e640 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b    computeJD(&x);
9e650 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d  .  computeYMD_HM
9e660 53 28 26 78 29 3b 0a 20 20 66 6f 72 28 69 3d 6a  S(&x);.  for(i=j
9e670 3d 30 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b  =0; zFmt[i]; i++
9e680 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b  ){.    if( zFmt[
9e690 69 5d 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20  i]!='%' ){.     
9e6a0 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74 5b 69   z[j++] = zFmt[i
9e6b0 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  ];.    }else{.  
9e6c0 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 73      i++;.      s
9e6d0 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 5d 20 29  witch( zFmt[i] )
9e6e0 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  {.        case '
9e6f0 64 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  d':  sqlite3_snp
9e700 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22  rintf(3, &z[j],"
9e710 25 30 32 64 22 2c 78 2e 44 29 3b 20 6a 2b 3d 32  %02d",x.D); j+=2
9e720 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
9e730 20 63 61 73 65 20 27 66 27 3a 20 7b 0a 20 20 20   case 'f': {.   
9e740 20 20 20 20 20 20 20 64 6f 75 62 6c 65 20 73 20         double s 
9e750 3d 20 78 2e 73 3b 0a 20 20 20 20 20 20 20 20 20  = x.s;.         
9e760 20 69 66 28 20 73 3e 35 39 2e 39 39 39 20 29 20   if( s>59.999 ) 
9e770 73 20 3d 20 35 39 2e 39 39 39 3b 0a 20 20 20 20  s = 59.999;.    
9e780 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
9e790 70 72 69 6e 74 66 28 37 2c 20 26 7a 5b 6a 5d 2c  printf(7, &z[j],
9e7a0 22 25 30 36 2e 33 66 22 2c 20 73 29 3b 0a 20 20  "%06.3f", s);.  
9e7b0 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c          j += sql
9e7c0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b  ite3Strlen30(&z[
9e7d0 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62  j]);.          b
9e7e0 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
9e7f0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 48 27          case 'H'
9e800 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  :  sqlite3_snpri
9e810 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30  ntf(3, &z[j],"%0
9e820 32 64 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20  2d",x.h); j+=2; 
9e830 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
9e840 61 73 65 20 27 57 27 3a 20 2f 2a 20 46 61 6c 6c  ase 'W': /* Fall
9e850 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 20   thru */.       
9e860 20 63 61 73 65 20 27 6a 27 3a 20 7b 0a 20 20 20   case 'j': {.   
9e870 20 20 20 20 20 20 20 69 6e 74 20 6e 44 61 79 3b         int nDay;
9e880 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
9e890 4e 75 6d 62 65 72 20 6f 66 20 64 61 79 73 20 73  Number of days s
9e8a0 69 6e 63 65 20 31 73 74 20 64 61 79 20 6f 66 20  ince 1st day of 
9e8b0 79 65 61 72 20 2a 2f 0a 20 20 20 20 20 20 20 20  year */.        
9e8c0 20 20 44 61 74 65 54 69 6d 65 20 79 20 3d 20 78    DateTime y = x
9e8d0 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e 76 61  ;.          y.va
9e8e0 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20  lidJD = 0;.     
9e8f0 20 20 20 20 20 79 2e 4d 20 3d 20 31 3b 0a 20 20       y.M = 1;.  
9e900 20 20 20 20 20 20 20 20 79 2e 44 20 3d 20 31 3b          y.D = 1;
9e910 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6d 70 75  .          compu
9e920 74 65 4a 44 28 26 79 29 3b 0a 20 20 20 20 20 20  teJD(&y);.      
9e930 20 20 20 20 6e 44 61 79 20 3d 20 28 69 6e 74 29      nDay = (int)
9e940 28 28 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b 34 33  ((x.iJD-y.iJD+43
9e950 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30  200000)/86400000
9e960 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
9e970 20 7a 46 6d 74 5b 69 5d 3d 3d 27 57 27 20 29 7b   zFmt[i]=='W' ){
9e980 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
9e990 20 77 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f 6e 64   wd;   /* 0=Mond
9e9a0 61 79 2c 20 31 3d 54 75 65 73 64 61 79 2c 20 2e  ay, 1=Tuesday, .
9e9b0 2e 2e 20 36 3d 53 75 6e 64 61 79 20 2a 2f 0a 20  .. 6=Sunday */. 
9e9c0 20 20 20 20 20 20 20 20 20 20 20 77 64 20 3d 20             wd = 
9e9d0 28 69 6e 74 29 28 28 28 78 2e 69 4a 44 2b 34 33  (int)(((x.iJD+43
9e9e0 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30  200000)/86400000
9e9f0 29 25 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20  )%7);.          
9ea00 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
9ea10 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32  tf(3, &z[j],"%02
9ea20 64 22 2c 28 6e 44 61 79 2b 37 2d 77 64 29 2f 37  d",(nDay+7-wd)/7
9ea30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a  );.            j
9ea40 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 20   += 2;.         
9ea50 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
9ea60 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
9ea70 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c 22 25  intf(4, &z[j],"%
9ea80 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b 0a 20 20  03d",nDay+1);.  
9ea90 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 33            j += 3
9eaa0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
9eab0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
9eac0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
9ead0 20 63 61 73 65 20 27 4a 27 3a 20 7b 0a 20 20 20   case 'J': {.   
9eae0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
9eaf0 6e 70 72 69 6e 74 66 28 32 30 2c 20 26 7a 5b 6a  nprintf(20, &z[j
9eb00 5d 2c 22 25 2e 31 36 67 22 2c 78 2e 69 4a 44 2f  ],"%.16g",x.iJD/
9eb10 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20 20 20  86400000.0);.   
9eb20 20 20 20 20 20 20 20 6a 2b 3d 73 71 6c 69 74 65         j+=sqlite
9eb30 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29  3Strlen30(&z[j])
9eb40 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
9eb50 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
9eb60 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a 20 20       case 'm':  
9eb70 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
9eb80 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22  (3, &z[j],"%02d"
9eb90 2c 78 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65  ,x.M); j+=2; bre
9eba0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
9ebb0 20 27 4d 27 3a 20 20 73 71 6c 69 74 65 33 5f 73   'M':  sqlite3_s
9ebc0 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d  nprintf(3, &z[j]
9ebd0 2c 22 25 30 32 64 22 2c 78 2e 6d 29 3b 20 6a 2b  ,"%02d",x.m); j+
9ebe0 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  =2; break;.     
9ebf0 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20     case 's': {. 
9ec00 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
9ec10 5f 73 6e 70 72 69 6e 74 66 28 33 30 2c 26 7a 5b  _snprintf(30,&z[
9ec20 6a 5d 2c 22 25 6c 6c 64 22 2c 0a 20 20 20 20 20  j],"%lld",.     
9ec30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ec40 20 20 20 20 20 20 28 69 36 34 29 28 78 2e 69 4a        (i64)(x.iJ
9ec50 44 2f 31 30 30 30 20 2d 20 32 31 30 38 36 36 37  D/1000 - 2108667
9ec60 36 2a 28 69 36 34 29 31 30 30 30 30 29 29 3b 0a  6*(i64)10000));.
9ec70 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73            j += s
9ec80 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26  qlite3Strlen30(&
9ec90 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20  z[j]);.         
9eca0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
9ecb0 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  }.        case '
9ecc0 53 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  S':  sqlite3_snp
9ecd0 72 69 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25  rintf(3,&z[j],"%
9ece0 30 32 64 22 2c 28 69 6e 74 29 78 2e 73 29 3b 20  02d",(int)x.s); 
9ecf0 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20  j+=2; break;.   
9ed00 20 20 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b       case 'w': {
9ed10 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b  .          z[j++
9ed20 5d 20 3d 20 28 63 68 61 72 29 28 28 28 78 2e 69  ] = (char)(((x.i
9ed30 4a 44 2b 31 32 39 36 30 30 30 30 30 29 2f 38 36  JD+129600000)/86
9ed40 34 30 30 30 30 30 29 20 25 20 37 29 20 2b 20 27  400000) % 7) + '
9ed50 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  0';.          br
9ed60 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
9ed70 20 20 20 20 20 20 20 63 61 73 65 20 27 59 27 3a         case 'Y':
9ed80 20 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c   {.          sql
9ed90 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 2c  ite3_snprintf(5,
9eda0 26 7a 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 2e 59  &z[j],"%04d",x.Y
9edb0 29 3b 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72  ); j+=sqlite3Str
9edc0 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20  len30(&z[j]);.  
9edd0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
9ede0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
9edf0 20 64 65 66 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b   default:   z[j+
9ee00 2b 5d 20 3d 20 27 25 27 3b 20 62 72 65 61 6b 3b  +] = '%'; break;
9ee10 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
9ee20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20   }.  z[j] = 0;. 
9ee30 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
9ee40 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c  text(context, z,
9ee50 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20   -1,.           
9ee60 20 20 20 20 20 20 20 20 20 20 20 7a 3d 3d 7a 42             z==zB
9ee70 75 66 20 3f 20 53 51 4c 49 54 45 5f 54 52 41 4e  uf ? SQLITE_TRAN
9ee80 53 49 45 4e 54 20 3a 20 53 51 4c 49 54 45 5f 44  SIENT : SQLITE_D
9ee90 59 4e 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  YNAMIC);.}../*.*
9eea0 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29  * current_time()
9eeb0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
9eec0 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
9eed0 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74   same value as t
9eee0 69 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73  ime('now')..*/.s
9eef0 74 61 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65  tatic void ctime
9ef00 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
9ef10 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
9ef20 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c  ,.  int NotUsed,
9ef30 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
9ef40 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20   **NotUsed2.){. 
9ef50 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
9ef60 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
9ef70 73 65 64 32 29 3b 0a 20 20 74 69 6d 65 46 75 6e  sed2);.  timeFun
9ef80 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29  c(context, 0, 0)
9ef90 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65  ;.}../*.** curre
9efa0 6e 74 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20  nt_date().**.** 
9efb0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
9efc0 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76  turns the same v
9efd0 61 6c 75 65 20 61 73 20 64 61 74 65 28 27 6e 6f  alue as date('no
9efe0 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  w')..*/.static v
9eff0 6f 69 64 20 63 64 61 74 65 46 75 6e 63 28 0a 20  oid cdateFunc(. 
9f000 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
9f010 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
9f020 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
9f030 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55  te3_value **NotU
9f040 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44  sed2.){.  UNUSED
9f050 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
9f060 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
9f070 20 20 64 61 74 65 46 75 6e 63 28 63 6f 6e 74 65    dateFunc(conte
9f080 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a  xt, 0, 0);.}../*
9f090 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65  .** current_time
9f0a0 73 74 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68  stamp().**.** Th
9f0b0 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
9f0c0 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c  rns the same val
9f0d0 75 65 20 61 73 20 64 61 74 65 74 69 6d 65 28 27  ue as datetime('
9f0e0 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  now')..*/.static
9f0f0 20 76 6f 69 64 20 63 74 69 6d 65 73 74 61 6d 70   void ctimestamp
9f100 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
9f110 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
9f120 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c  ,.  int NotUsed,
9f130 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
9f140 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20   **NotUsed2.){. 
9f150 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
9f160 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
9f170 73 65 64 32 29 3b 0a 20 20 64 61 74 65 74 69 6d  sed2);.  datetim
9f180 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30  eFunc(context, 0
9f190 2c 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  , 0);.}.#endif /
9f1a0 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
9f1b0 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f  E_OMIT_DATETIME_
9f1c0 46 55 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 64 65  FUNCS) */..#ifde
9f1d0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41  f SQLITE_OMIT_DA
9f1e0 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a  TETIME_FUNCS./*.
9f1f0 2a 2a 20 49 66 20 74 68 65 20 6c 69 62 72 61 72  ** If the librar
9f200 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f  y is compiled to
9f210 20 6f 6d 69 74 20 74 68 65 20 66 75 6c 6c 2d 73   omit the full-s
9f220 63 61 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69  cale date and ti
9f230 6d 65 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 20 28  me.** handling (
9f240 74 6f 20 67 65 74 20 61 20 73 6d 61 6c 6c 65 72  to get a smaller
9f250 20 62 69 6e 61 72 79 29 2c 20 74 68 65 20 66 6f   binary), the fo
9f260 6c 6c 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 6c 20  llowing minimal 
9f270 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68  version.** of th
9f280 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 75 72 72  e functions curr
9f290 65 6e 74 5f 74 69 6d 65 28 29 2c 20 63 75 72 72  ent_time(), curr
9f2a0 65 6e 74 5f 64 61 74 65 28 29 20 61 6e 64 20 63  ent_date() and c
9f2b0 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70  urrent_timestamp
9f2c0 28 29 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64  ().** are includ
9f2d0 65 64 20 69 6e 73 74 65 61 64 2e 20 54 68 69 73  ed instead. This
9f2e0 20 69 73 20 74 6f 20 73 75 70 70 6f 72 74 20 63   is to support c
9f2f0 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f  olumn declaratio
9f300 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 63 6c 75  ns that.** inclu
9f310 64 65 20 22 44 45 46 41 55 4c 54 20 43 55 52 52  de "DEFAULT CURR
9f320 45 4e 54 5f 54 49 4d 45 22 20 65 74 63 2e 0a 2a  ENT_TIME" etc..*
9f330 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
9f340 6f 6e 20 75 73 65 73 20 74 68 65 20 43 2d 6c 69  on uses the C-li
9f350 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20  brary functions 
9f360 74 69 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28 29  time(), gmtime()
9f370 0a 2a 2a 20 61 6e 64 20 73 74 72 66 74 69 6d 65  .** and strftime
9f380 28 29 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 73  (). The format s
9f390 74 72 69 6e 67 20 74 6f 20 70 61 73 73 20 74 6f  tring to pass to
9f3a0 20 73 74 72 66 74 69 6d 65 28 29 20 69 73 20 73   strftime() is s
9f3b0 75 70 70 6c 69 65 64 0a 2a 2a 20 61 73 20 74 68  upplied.** as th
9f3c0 65 20 75 73 65 72 2d 64 61 74 61 20 66 6f 72 20  e user-data for 
9f3d0 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  the function..*/
9f3e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 75 72  .static void cur
9f3f0 72 65 6e 74 54 69 6d 65 46 75 6e 63 28 0a 20 20  rentTimeFunc(.  
9f400 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
9f410 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
9f420 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
9f430 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
9f440 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 63 68    time_t t;.  ch
9f450 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 28 63  ar *zFormat = (c
9f460 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 75 73  har *)sqlite3_us
9f470 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
9f480 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
9f490 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
9f4a0 20 69 54 3b 0a 20 20 73 74 72 75 63 74 20 74 6d   iT;.  struct tm
9f4b0 20 2a 70 54 6d 3b 0a 20 20 73 74 72 75 63 74 20   *pTm;.  struct 
9f4c0 74 6d 20 73 4e 6f 77 3b 0a 20 20 63 68 61 72 20  tm sNow;.  char 
9f4d0 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 20 55 4e 55  zBuf[20];..  UNU
9f4e0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72  SED_PARAMETER(ar
9f4f0 67 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  gc);.  UNUSED_PA
9f500 52 41 4d 45 54 45 52 28 61 72 67 76 29 3b 0a 0a  RAMETER(argv);..
9f510 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63    db = sqlite3_c
9f520 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
9f530 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28  (context);.  if(
9f540 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
9f550 74 54 69 6d 65 49 6e 74 36 34 28 64 62 2d 3e 70  tTimeInt64(db->p
9f560 56 66 73 2c 20 26 69 54 29 20 29 20 72 65 74 75  Vfs, &iT) ) retu
9f570 72 6e 3b 0a 20 20 74 20 3d 20 69 54 2f 31 30 30  rn;.  t = iT/100
9f580 30 20 2d 20 31 30 30 30 30 2a 28 73 71 6c 69 74  0 - 10000*(sqlit
9f590 65 33 5f 69 6e 74 36 34 29 32 31 30 38 36 36 37  e3_int64)2108667
9f5a0 36 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f 47  6;.#ifdef HAVE_G
9f5b0 4d 54 49 4d 45 5f 52 0a 20 20 70 54 6d 20 3d 20  MTIME_R.  pTm = 
9f5c0 67 6d 74 69 6d 65 5f 72 28 26 74 2c 20 26 73 4e  gmtime_r(&t, &sN
9f5d0 6f 77 29 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c  ow);.#else.  sql
9f5e0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
9f5f0 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  (sqlite3MutexAll
9f600 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
9f610 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b  STATIC_MASTER));
9f620 0a 20 20 70 54 6d 20 3d 20 67 6d 74 69 6d 65 28  .  pTm = gmtime(
9f630 26 74 29 3b 0a 20 20 69 66 28 20 70 54 6d 20 29  &t);.  if( pTm )
9f640 20 6d 65 6d 63 70 79 28 26 73 4e 6f 77 2c 20 70   memcpy(&sNow, p
9f650 54 6d 2c 20 73 69 7a 65 6f 66 28 73 4e 6f 77 29  Tm, sizeof(sNow)
9f660 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
9f670 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33  ex_leave(sqlite3
9f680 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
9f690 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
9f6a0 41 53 54 45 52 29 29 3b 0a 23 65 6e 64 69 66 0a  ASTER));.#endif.
9f6b0 20 20 69 66 28 20 70 54 6d 20 29 7b 0a 20 20 20    if( pTm ){.   
9f6c0 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c 20   strftime(zBuf, 
9f6d0 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 26 73 4e  20, zFormat, &sN
9f6e0 6f 77 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ow);.    sqlite3
9f6f0 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
9f700 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20  text, zBuf, -1, 
9f710 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
9f720 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a  );.  }.}.#endif.
9f730 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
9f740 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20  tion registered 
9f750 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65  all of the above
9f760 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20   C functions as 
9f770 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73  SQL.** functions
9f780 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62  .  This should b
9f790 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69  e the only routi
9f7a0 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  ne in this file 
9f7b0 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61 6c  with.** external
9f7c0 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c   linkage..*/.SQL
9f7d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
9f7e0 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
9f7f0 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e  DateTimeFunction
9f800 73 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  s(void){.  stati
9f810 63 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e  c SQLITE_WSD Fun
9f820 63 44 65 66 20 61 44 61 74 65 54 69 6d 65 46 75  cDef aDateTimeFu
9f830 6e 63 73 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64 65  ncs[] = {.#ifnde
9f840 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41  f SQLITE_OMIT_DA
9f850 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 20 20 20  TETIME_FUNCS.   
9f860 20 46 55 4e 43 54 49 4f 4e 28 6a 75 6c 69 61 6e   FUNCTION(julian
9f870 64 61 79 2c 20 20 20 20 20 20 20 20 2d 31 2c 20  day,        -1, 
9f880 30 2c 20 30 2c 20 6a 75 6c 69 61 6e 64 61 79 46  0, 0, juliandayF
9f890 75 6e 63 20 29 2c 0a 20 20 20 20 46 55 4e 43 54  unc ),.    FUNCT
9f8a0 49 4f 4e 28 64 61 74 65 2c 20 20 20 20 20 20 20  ION(date,       
9f8b0 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20        -1, 0, 0, 
9f8c0 64 61 74 65 46 75 6e 63 20 20 20 20 20 20 29 2c  dateFunc      ),
9f8d0 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 69  .    FUNCTION(ti
9f8e0 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  me,             
9f8f0 2d 31 2c 20 30 2c 20 30 2c 20 74 69 6d 65 46 75  -1, 0, 0, timeFu
9f900 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46  nc      ),.    F
9f910 55 4e 43 54 49 4f 4e 28 64 61 74 65 74 69 6d 65  UNCTION(datetime
9f920 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c  ,         -1, 0,
9f930 20 30 2c 20 64 61 74 65 74 69 6d 65 46 75 6e 63   0, datetimeFunc
9f940 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
9f950 4e 28 73 74 72 66 74 69 6d 65 2c 20 20 20 20 20  N(strftime,     
9f960 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 73 74      -1, 0, 0, st
9f970 72 66 74 69 6d 65 46 75 6e 63 20 20 29 2c 0a 20  rftimeFunc  ),. 
9f980 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72     FUNCTION(curr
9f990 65 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20 30  ent_time,      0
9f9a0 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 46 75 6e  , 0, 0, ctimeFun
9f9b0 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e  c     ),.    FUN
9f9c0 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69  CTION(current_ti
9f9d0 6d 65 73 74 61 6d 70 2c 20 30 2c 20 30 2c 20 30  mestamp, 0, 0, 0
9f9e0 2c 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 63  , ctimestampFunc
9f9f0 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
9fa00 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 20 20  current_date,   
9fa10 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 64 61 74     0, 0, 0, cdat
9fa20 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 23 65 6c  eFunc     ),.#el
9fa30 73 65 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54  se.    STR_FUNCT
9fa40 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65  ION(current_time
9fa50 2c 20 20 20 20 20 20 30 2c 20 22 25 48 3a 25 4d  ,      0, "%H:%M
9fa60 3a 25 53 22 2c 20 20 20 20 20 20 20 20 20 20 30  :%S",          0
9fa70 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e  , currentTimeFun
9fa80 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e 43  c),.    STR_FUNC
9fa90 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74  TION(current_dat
9faa0 65 2c 20 20 20 20 20 20 30 2c 20 22 25 59 2d 25  e,      0, "%Y-%
9fab0 6d 2d 25 64 22 2c 20 20 20 20 20 20 20 20 20 20  m-%d",          
9fac0 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75  0, currentTimeFu
9fad0 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e  nc),.    STR_FUN
9fae0 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69  CTION(current_ti
9faf0 6d 65 73 74 61 6d 70 2c 20 30 2c 20 22 25 59 2d  mestamp, 0, "%Y-
9fb00 25 6d 2d 25 64 20 25 48 3a 25 4d 3a 25 53 22 2c  %m-%d %H:%M:%S",
9fb10 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46   0, currentTimeF
9fb20 75 6e 63 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d  unc),.#endif.  }
9fb30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75 6e  ;.  int i;.  Fun
9fb40 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 20  cDefHash *pHash 
9fb50 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65  = &GLOBAL(FuncDe
9fb60 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c  fHash, sqlite3Gl
9fb70 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a  obalFunctions);.
9fb80 20 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e 63    FuncDef *aFunc
9fb90 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47 4c   = (FuncDef*)&GL
9fba0 4f 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61 44  OBAL(FuncDef, aD
9fbb0 61 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 0a 0a  ateTimeFuncs);..
9fbc0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72    for(i=0; i<Arr
9fbd0 61 79 53 69 7a 65 28 61 44 61 74 65 54 69 6d 65  aySize(aDateTime
9fbe0 46 75 6e 63 73 29 3b 20 69 2b 2b 29 7b 0a 20 20  Funcs); i++){.  
9fbf0 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66    sqlite3FuncDef
9fc00 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26 61  Insert(pHash, &a
9fc10 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a  Func[i]);.  }.}.
9fc20 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
9fc30 20 45 6e 64 20 6f 66 20 64 61 74 65 2e 63 20 2a   End of date.c *
9fc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
9fc70 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
9fc80 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e 63   Begin file os.c
9fc90 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
9fca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
9fcc0 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e 6f 76 65  ./*.** 2005 Nove
9fcd0 6d 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54 68  mber 29.**.** Th
9fce0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
9fcf0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
9fd00 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
9fd10 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
9fd20 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
9fd30 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
9fd40 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
9fd50 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
9fd60 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
9fd70 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
9fd80 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
9fd90 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
9fda0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
9fdb0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
9fdc0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
9fdd0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
9fde0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
9fdf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fe00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fe10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fe20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fe30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
9fe40 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
9fe50 61 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61 63  ains OS interfac
9fe60 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63  e code that is c
9fe70 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20  ommon to all.** 
9fe80 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a 2a  architectures..*
9fe90 2f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54  /.#define _SQLIT
9fea0 45 5f 4f 53 5f 43 5f 20 31 0a 23 75 6e 64 65 66  E_OS_C_ 1.#undef
9feb0 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 0a 0a   _SQLITE_OS_C_..
9fec0 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c  /*.** The defaul
9fed0 74 20 53 51 4c 69 74 65 20 73 71 6c 69 74 65 33  t SQLite sqlite3
9fee0 5f 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  _vfs implementat
9fef0 69 6f 6e 73 20 64 6f 20 6e 6f 74 20 61 6c 6c 6f  ions do not allo
9ff00 63 61 74 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 28  cate.** memory (
9ff10 61 63 74 75 61 6c 6c 79 2c 20 6f 73 5f 75 6e 69  actually, os_uni
9ff20 78 2e 63 20 61 6c 6c 6f 63 61 74 65 73 20 61 20  x.c allocates a 
9ff30 73 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20  small amount of 
9ff40 6d 65 6d 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20 77  memory.** from w
9ff50 69 74 68 69 6e 20 4f 73 4f 70 65 6e 28 29 29 2c  ithin OsOpen()),
9ff60 20 62 75 74 20 73 6f 6d 65 20 74 68 69 72 64 2d   but some third-
9ff70 70 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61  party implementa
9ff80 74 69 6f 6e 73 20 6d 61 79 2e 0a 2a 2a 20 53 6f  tions may..** So
9ff90 20 77 65 20 74 65 73 74 20 74 68 65 20 65 66 66   we test the eff
9ffa0 65 63 74 73 20 6f 66 20 61 20 6d 61 6c 6c 6f 63  ects of a malloc
9ffb0 28 29 20 66 61 69 6c 69 6e 67 20 61 6e 64 20 74  () failing and t
9ffc0 68 65 20 73 71 6c 69 74 65 33 4f 73 58 58 58 28  he sqlite3OsXXX(
9ffd0 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65  ).** function re
9ffe0 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 49  turning SQLITE_I
9fff0 4f 45 52 52 5f 4e 4f 4d 45 4d 20 75 73 69 6e 67  OERR_NOMEM using
a0000 20 74 68 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f   the DO_OS_MALLO
a0010 43 5f 54 45 53 54 20 6d 61 63 72 6f 2e 0a 2a 2a  C_TEST macro..**
a0020 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
a0030 67 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  g functions are 
a0040 69 6e 73 74 72 75 6d 65 6e 74 65 64 20 66 6f 72  instrumented for
a0050 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
a0060 65 20 0a 2a 2a 20 74 65 73 74 69 6e 67 3a 0a 2a  e .** testing:.*
a0070 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  *.**     sqlite3
a0080 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20 20 20 20  OsRead().**     
a0090 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29  sqlite3OsWrite()
a00a0 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f  .**     sqlite3O
a00b0 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20 20 20 73  sSync().**     s
a00c0 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65  qlite3OsFileSize
a00d0 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
a00e0 33 4f 73 4c 6f 63 6b 28 29 0a 2a 2a 20 20 20 20  3OsLock().**    
a00f0 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52   sqlite3OsCheckR
a0100 65 73 65 72 76 65 64 4c 6f 63 6b 28 29 0a 2a 2a  eservedLock().**
a0110 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69       sqlite3OsFi
a0120 6c 65 43 6f 6e 74 72 6f 6c 28 29 0a 2a 2a 20 20  leControl().**  
a0130 20 20 20 73 71 6c 69 74 65 33 4f 73 53 68 6d 4d     sqlite3OsShmM
a0140 61 70 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  ap().**     sqli
a0150 74 65 33 4f 73 4f 70 65 6e 28 29 0a 2a 2a 20 20  te3OsOpen().**  
a0160 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65     sqlite3OsDele
a0170 74 65 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  te().**     sqli
a0180 74 65 33 4f 73 41 63 63 65 73 73 28 29 0a 2a 2a  te3OsAccess().**
a0190 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 75       sqlite3OsFu
a01a0 6c 6c 50 61 74 68 6e 61 6d 65 28 29 0a 2a 2a 0a  llPathname().**.
a01b0 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
a01c0 51 4c 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49  QLITE_TEST).SQLI
a01d0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
a01e0 65 33 5f 6d 65 6d 64 65 62 75 67 5f 76 66 73 5f  e3_memdebug_vfs_
a01f0 6f 6f 6d 5f 74 65 73 74 20 3d 20 31 3b 0a 20 20  oom_test = 1;.  
a0200 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41  #define DO_OS_MA
a0210 4c 4c 4f 43 5f 54 45 53 54 28 78 29 20 20 20 20  LLOC_TEST(x)    
a0220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0240 20 20 20 5c 0a 20 20 69 66 20 28 73 71 6c 69 74     \.  if (sqlit
a0250 65 33 5f 6d 65 6d 64 65 62 75 67 5f 76 66 73 5f  e3_memdebug_vfs_
a0260 6f 6f 6d 5f 74 65 73 74 20 26 26 20 28 21 78 20  oom_test && (!x 
a0270 7c 7c 20 21 73 71 6c 69 74 65 33 49 73 4d 65 6d  || !sqlite3IsMem
a0280 4a 6f 75 72 6e 61 6c 28 78 29 29 29 20 7b 20 20  Journal(x))) {  
a0290 5c 0a 20 20 20 20 76 6f 69 64 20 2a 70 54 73 74  \.    void *pTst
a02a0 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 4d  Alloc = sqlite3M
a02b0 61 6c 6c 6f 63 28 31 30 29 3b 20 20 20 20 20 20  alloc(10);      
a02c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a02d0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66 20         \.    if 
a02e0 28 21 70 54 73 74 41 6c 6c 6f 63 29 20 72 65 74  (!pTstAlloc) ret
a02f0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
a0300 5f 4e 4f 4d 45 4d 3b 20 20 20 20 20 20 20 20 20  _NOMEM;         
a0310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
a0320 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
a0330 28 70 54 73 74 41 6c 6c 6f 63 29 3b 20 20 20 20  (pTstAlloc);    
a0340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0360 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6c 73 65       \.  }.#else
a0370 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53  .  #define DO_OS
a0380 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 78 29 0a  _MALLOC_TEST(x).
a0390 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
a03a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74  e following rout
a03b0 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e 69  ines are conveni
a03c0 65 6e 63 65 20 77 72 61 70 70 65 72 73 20 61 72  ence wrappers ar
a03d0 6f 75 6e 64 20 6d 65 74 68 6f 64 73 0a 2a 2a 20  ound methods.** 
a03e0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66  of the sqlite3_f
a03f0 69 6c 65 20 6f 62 6a 65 63 74 2e 20 20 54 68 69  ile object.  Thi
a0400 73 20 69 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74  s is mostly just
a0410 20 73 79 6e 74 61 63 74 69 63 20 73 75 67 61 72   syntactic sugar
a0420 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68 69 73  . All.** of this
a0430 20 77 6f 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65   would be comple
a0440 74 65 6c 79 20 61 75 74 6f 6d 61 74 69 63 20 69  tely automatic i
a0450 66 20 53 51 4c 69 74 65 20 77 65 72 65 20 63 6f  f SQLite were co
a0460 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 43 2b 2b  ded using.** C++
a0470 20 69 6e 73 74 65 61 64 20 6f 66 20 70 6c 61 69   instead of plai
a0480 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51 4c 49  n old C..*/.SQLI
a0490 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a04a0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71  qlite3OsClose(sq
a04b0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 29  lite3_file *pId)
a04c0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
a04d0 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 49  ITE_OK;.  if( pI
a04e0 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20  d->pMethods ){. 
a04f0 20 20 20 72 63 20 3d 20 70 49 64 2d 3e 70 4d 65     rc = pId->pMe
a0500 74 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 28 70 49  thods->xClose(pI
a0510 64 29 3b 0a 20 20 20 20 70 49 64 2d 3e 70 4d 65  d);.    pId->pMe
a0520 74 68 6f 64 73 20 3d 20 30 3b 0a 20 20 7d 0a 20  thods = 0;.  }. 
a0530 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51   return rc;.}.SQ
a0540 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
a0550 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73   sqlite3OsRead(s
a0560 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
a0570 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74   void *pBuf, int
a0580 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74   amt, i64 offset
a0590 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f  ){.  DO_OS_MALLO
a05a0 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65  C_TEST(id);.  re
a05b0 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64  turn id->pMethod
a05c0 73 2d 3e 78 52 65 61 64 28 69 64 2c 20 70 42 75  s->xRead(id, pBu
a05d0 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b  f, amt, offset);
a05e0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
a05f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57  E int sqlite3OsW
a0600 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  rite(sqlite3_fil
a0610 65 20 2a 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69  e *id, const voi
a0620 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d 74  d *pBuf, int amt
a0630 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a 20  , i64 offset){. 
a0640 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
a0650 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e  ST(id);.  return
a0660 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78   id->pMethods->x
a0670 57 72 69 74 65 28 69 64 2c 20 70 42 75 66 2c 20  Write(id, pBuf, 
a0680 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a  amt, offset);.}.
a0690 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a06a0 6e 74 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e  nt sqlite3OsTrun
a06b0 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  cate(sqlite3_fil
a06c0 65 20 2a 69 64 2c 20 69 36 34 20 73 69 7a 65 29  e *id, i64 size)
a06d0 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  {.  return id->p
a06e0 4d 65 74 68 6f 64 73 2d 3e 78 54 72 75 6e 63 61  Methods->xTrunca
a06f0 74 65 28 69 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a  te(id, size);.}.
a0700 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a0710 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63  nt sqlite3OsSync
a0720 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
a0730 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  d, int flags){. 
a0740 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
a0750 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e  ST(id);.  return
a0760 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78   id->pMethods->x
a0770 53 79 6e 63 28 69 64 2c 20 66 6c 61 67 73 29 3b  Sync(id, flags);
a0780 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
a0790 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46  E int sqlite3OsF
a07a0 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f  ileSize(sqlite3_
a07b0 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a 70  file *id, i64 *p
a07c0 53 69 7a 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d  Size){.  DO_OS_M
a07d0 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a  ALLOC_TEST(id);.
a07e0 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65    return id->pMe
a07f0 74 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65  thods->xFileSize
a0800 28 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 53  (id, pSize);.}.S
a0810 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
a0820 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  t sqlite3OsLock(
a0830 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
a0840 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b  , int lockType){
a0850 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f  .  DO_OS_MALLOC_
a0860 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75  TEST(id);.  retu
a0870 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
a0880 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54  >xLock(id, lockT
a0890 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  ype);.}.SQLITE_P
a08a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
a08b0 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  e3OsUnlock(sqlit
a08c0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
a08d0 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 72 65   lockType){.  re
a08e0 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64  turn id->pMethod
a08f0 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c  s->xUnlock(id, l
a0900 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49  ockType);.}.SQLI
a0910 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a0920 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73  qlite3OsCheckRes
a0930 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
a0940 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
a0950 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f  *pResOut){.  DO_
a0960 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69  OS_MALLOC_TEST(i
a0970 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  d);.  return id-
a0980 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 68 65 63  >pMethods->xChec
a0990 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 69 64  kReservedLock(id
a09a0 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a 0a 2f  , pResOut);.}../
a09b0 2a 0a 2a 2a 20 55 73 65 20 73 71 6c 69 74 65 33  *.** Use sqlite3
a09c0 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20  OsFileControl() 
a09d0 77 68 65 6e 20 77 65 20 61 72 65 20 64 6f 69 6e  when we are doin
a09e0 67 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74  g something that
a09f0 20 6d 69 67 68 74 20 66 61 69 6c 0a 2a 2a 20 61   might fail.** a
a0a00 6e 64 20 77 65 20 6e 65 65 64 20 74 6f 20 6b 6e  nd we need to kn
a0a10 6f 77 20 61 62 6f 75 74 20 74 68 65 20 66 61 69  ow about the fai
a0a20 6c 75 72 65 73 2e 20 20 55 73 65 20 73 71 6c 69  lures.  Use sqli
a0a30 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c  te3OsFileControl
a0a40 48 69 6e 74 28 29 0a 2a 2a 20 77 68 65 6e 20 73  Hint().** when s
a0a50 69 6d 70 6c 79 20 74 6f 73 73 69 6e 67 20 69 6e  imply tossing in
a0a60 66 6f 72 6d 61 74 69 6f 6e 20 6f 76 65 72 20 74  formation over t
a0a70 68 65 20 77 61 6c 6c 20 74 6f 20 74 68 65 20 56  he wall to the V
a0a80 46 53 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f 74  FS and we do not
a0a90 0a 2a 2a 20 72 65 61 6c 6c 79 20 63 61 72 65 20  .** really care 
a0aa0 69 66 20 74 68 65 20 56 46 53 20 72 65 63 65 69  if the VFS recei
a0ab0 76 65 73 20 61 6e 64 20 75 6e 64 65 72 73 74 61  ves and understa
a0ac0 6e 64 73 20 74 68 65 20 69 6e 66 6f 72 6d 61 74  nds the informat
a0ad0 69 6f 6e 20 73 69 6e 63 65 20 69 74 0a 2a 2a 20  ion since it.** 
a0ae0 69 73 20 6f 6e 6c 79 20 61 20 68 69 6e 74 20 61  is only a hint a
a0af0 6e 64 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79  nd can be safely
a0b00 20 69 67 6e 6f 72 65 64 2e 20 20 54 68 65 20 73   ignored.  The s
a0b10 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74  qlite3OsFileCont
a0b20 72 6f 6c 48 69 6e 74 28 29 0a 2a 2a 20 72 6f 75  rolHint().** rou
a0b30 74 69 6e 65 20 68 61 73 20 6e 6f 20 72 65 74 75  tine has no retu
a0b40 72 6e 20 76 61 6c 75 65 20 73 69 6e 63 65 20 74  rn value since t
a0b50 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
a0b60 77 6f 75 6c 64 20 62 65 20 6d 65 61 6e 69 6e 67  would be meaning
a0b70 6c 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  less..*/.SQLITE_
a0b80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
a0b90 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c  te3OsFileControl
a0ba0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
a0bb0 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20  d, int op, void 
a0bc0 2a 70 41 72 67 29 7b 0a 20 20 44 4f 5f 4f 53 5f  *pArg){.  DO_OS_
a0bd0 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b  MALLOC_TEST(id);
a0be0 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d  .  return id->pM
a0bf0 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 43 6f 6e  ethods->xFileCon
a0c00 74 72 6f 6c 28 69 64 2c 20 6f 70 2c 20 70 41 72  trol(id, op, pAr
a0c10 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  g);.}.SQLITE_PRI
a0c20 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
a0c30 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 48 69  3OsFileControlHi
a0c40 6e 74 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  nt(sqlite3_file 
a0c50 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69  *id, int op, voi
a0c60 64 20 2a 70 41 72 67 29 7b 0a 20 20 28 76 6f 69  d *pArg){.  (voi
a0c70 64 29 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e  d)id->pMethods->
a0c80 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 2c  xFileControl(id,
a0c90 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 0a 53   op, pArg);.}..S
a0ca0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
a0cb0 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f  t sqlite3OsSecto
a0cc0 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  rSize(sqlite3_fi
a0cd0 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28  le *id){.  int (
a0ce0 2a 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71  *xSectorSize)(sq
a0cf0 6c 69 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69  lite3_file*) = i
a0d00 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65  d->pMethods->xSe
a0d10 63 74 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74 75  ctorSize;.  retu
a0d20 72 6e 20 28 78 53 65 63 74 6f 72 53 69 7a 65 20  rn (xSectorSize 
a0d30 3f 20 78 53 65 63 74 6f 72 53 69 7a 65 28 69 64  ? xSectorSize(id
a0d40 29 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ) : SQLITE_DEFAU
a0d50 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b  LT_SECTOR_SIZE);
a0d60 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
a0d70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44  E int sqlite3OsD
a0d80 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
a0d90 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c  tics(sqlite3_fil
a0da0 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e  e *id){.  return
a0db0 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78   id->pMethods->x
a0dc0 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
a0dd0 73 74 69 63 73 28 69 64 29 3b 0a 7d 0a 53 51 4c  stics(id);.}.SQL
a0de0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
a0df0 73 71 6c 69 74 65 33 4f 73 53 68 6d 4c 6f 63 6b  sqlite3OsShmLock
a0e00 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
a0e10 64 2c 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 69  d, int offset, i
a0e20 6e 74 20 6e 2c 20 69 6e 74 20 66 6c 61 67 73 29  nt n, int flags)
a0e30 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  {.  return id->p
a0e40 4d 65 74 68 6f 64 73 2d 3e 78 53 68 6d 4c 6f 63  Methods->xShmLoc
a0e50 6b 28 69 64 2c 20 6f 66 66 73 65 74 2c 20 6e 2c  k(id, offset, n,
a0e60 20 66 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49 54   flags);.}.SQLIT
a0e70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
a0e80 71 6c 69 74 65 33 4f 73 53 68 6d 42 61 72 72 69  qlite3OsShmBarri
a0e90 65 72 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  er(sqlite3_file 
a0ea0 2a 69 64 29 7b 0a 20 20 69 64 2d 3e 70 4d 65 74  *id){.  id->pMet
a0eb0 68 6f 64 73 2d 3e 78 53 68 6d 42 61 72 72 69 65  hods->xShmBarrie
a0ec0 72 28 69 64 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  r(id);.}.SQLITE_
a0ed0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
a0ee0 74 65 33 4f 73 53 68 6d 55 6e 6d 61 70 28 73 71  te3OsShmUnmap(sq
a0ef0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
a0f00 69 6e 74 20 64 65 6c 65 74 65 46 6c 61 67 29 7b  int deleteFlag){
a0f10 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d  .  return id->pM
a0f20 65 74 68 6f 64 73 2d 3e 78 53 68 6d 55 6e 6d 61  ethods->xShmUnma
a0f30 70 28 69 64 2c 20 64 65 6c 65 74 65 46 6c 61 67  p(id, deleteFlag
a0f40 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
a0f50 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
a0f60 73 53 68 6d 4d 61 70 28 0a 20 20 73 71 6c 69 74  sShmMap(.  sqlit
a0f70 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20  e3_file *id,    
a0f80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
a0f90 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 6e 64  tabase file hand
a0fa0 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67  le */.  int iPag
a0fb0 65 2c 0a 20 20 69 6e 74 20 70 67 73 7a 2c 0a 20  e,.  int pgsz,. 
a0fc0 20 69 6e 74 20 62 45 78 74 65 6e 64 2c 20 20 20   int bExtend,   
a0fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0fe0 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 78 74 65   /* True to exte
a0ff0 6e 64 20 66 69 6c 65 20 69 66 20 6e 65 63 65 73  nd file if neces
a1000 73 61 72 79 20 2a 2f 0a 20 20 76 6f 69 64 20 76  sary */.  void v
a1010 6f 6c 61 74 69 6c 65 20 2a 2a 70 70 20 20 20 20  olatile **pp    
a1020 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
a1030 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d 61 70  : Pointer to map
a1040 70 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 44 4f 5f  ping */.){.  DO_
a1050 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69  OS_MALLOC_TEST(i
a1060 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  d);.  return id-
a1070 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 68 6d 4d  >pMethods->xShmM
a1080 61 70 28 69 64 2c 20 69 50 61 67 65 2c 20 70 67  ap(id, iPage, pg
a1090 73 7a 2c 20 62 45 78 74 65 6e 64 2c 20 70 70 29  sz, bExtend, pp)
a10a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e  ;.}../*.** The n
a10b0 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75  ext group of rou
a10c0 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e  tines are conven
a10d0 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 20 61  ience wrappers a
a10e0 72 6f 75 6e 64 20 74 68 65 0a 2a 2a 20 56 46 53  round the.** VFS
a10f0 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c   methods..*/.SQL
a1100 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
a1110 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 0a 20  sqlite3OsOpen(. 
a1120 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
a1130 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  fs, .  const cha
a1140 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 73 71 6c  r *zPath, .  sql
a1150 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65  ite3_file *pFile
a1160 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  , .  int flags, 
a1170 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67 73 4f 75  .  int *pFlagsOu
a1180 74 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  t.){.  int rc;. 
a1190 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
a11a0 53 54 28 30 29 3b 0a 20 20 2f 2a 20 30 78 38 37  ST(0);.  /* 0x87
a11b0 66 37 66 20 69 73 20 61 20 6d 61 73 6b 20 6f 66  f7f is a mask of
a11c0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 6c   SQLITE_OPEN_ fl
a11d0 61 67 73 20 74 68 61 74 20 61 72 65 20 76 61 6c  ags that are val
a11e0 69 64 20 74 6f 20 62 65 20 70 61 73 73 65 64 0a  id to be passed.
a11f0 20 20 2a 2a 20 64 6f 77 6e 20 69 6e 74 6f 20 74    ** down into t
a1200 68 65 20 56 46 53 20 6c 61 79 65 72 2e 20 20 53  he VFS layer.  S
a1210 6f 6d 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ome SQLITE_OPEN_
a1220 20 66 6c 61 67 73 20 28 66 6f 72 20 65 78 61 6d   flags (for exam
a1230 70 6c 65 2c 0a 20 20 2a 2a 20 53 51 4c 49 54 45  ple,.  ** SQLITE
a1240 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20  _OPEN_FULLMUTEX 
a1250 6f 72 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  or SQLITE_OPEN_S
a1260 48 41 52 45 44 43 41 43 48 45 29 20 61 72 65 20  HAREDCACHE) are 
a1270 62 6c 6f 63 6b 65 64 20 62 65 66 6f 72 65 0a 20  blocked before. 
a1280 20 2a 2a 20 72 65 61 63 68 69 6e 67 20 74 68 65   ** reaching the
a1290 20 56 46 53 2e 20 2a 2f 0a 20 20 72 63 20 3d 20   VFS. */.  rc = 
a12a0 70 56 66 73 2d 3e 78 4f 70 65 6e 28 70 56 66 73  pVfs->xOpen(pVfs
a12b0 2c 20 7a 50 61 74 68 2c 20 70 46 69 6c 65 2c 20  , zPath, pFile, 
a12c0 66 6c 61 67 73 20 26 20 30 78 38 37 66 37 66 2c  flags & 0x87f7f,
a12d0 20 70 46 6c 61 67 73 4f 75 74 29 3b 0a 20 20 61   pFlagsOut);.  a
a12e0 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
a12f0 45 5f 4f 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 70  E_OK || pFile->p
a1300 4d 65 74 68 6f 64 73 3d 3d 30 20 29 3b 0a 20 20  Methods==0 );.  
a1310 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
a1320 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
a1330 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
a1340 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
a1350 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  s, const char *z
a1360 50 61 74 68 2c 20 69 6e 74 20 64 69 72 53 79 6e  Path, int dirSyn
a1370 63 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c  c){.  DO_OS_MALL
a1380 4f 43 5f 54 45 53 54 28 30 29 3b 0a 20 20 61 73  OC_TEST(0);.  as
a1390 73 65 72 74 28 20 64 69 72 53 79 6e 63 3d 3d 30  sert( dirSync==0
a13a0 20 7c 7c 20 64 69 72 53 79 6e 63 3d 3d 31 20 29   || dirSync==1 )
a13b0 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d  ;.  return pVfs-
a13c0 3e 78 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a  >xDelete(pVfs, z
a13d0 50 61 74 68 2c 20 64 69 72 53 79 6e 63 29 3b 0a  Path, dirSync);.
a13e0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
a13f0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63   int sqlite3OsAc
a1400 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f  cess(.  sqlite3_
a1410 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f  vfs *pVfs, .  co
a1420 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c  nst char *zPath,
a1430 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a   .  int flags, .
a1440 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 0a 29    int *pResOut.)
a1450 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43  {.  DO_OS_MALLOC
a1460 5f 54 45 53 54 28 30 29 3b 0a 20 20 72 65 74 75  _TEST(0);.  retu
a1470 72 6e 20 70 56 66 73 2d 3e 78 41 63 63 65 73 73  rn pVfs->xAccess
a1480 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 66 6c  (pVfs, zPath, fl
a1490 61 67 73 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d  ags, pResOut);.}
a14a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a14b0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c  int sqlite3OsFul
a14c0 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c  lPathname(.  sql
a14d0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
a14e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
a14f0 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61  Path, .  int nPa
a1500 74 68 4f 75 74 2c 20 0a 20 20 63 68 61 72 20 2a  thOut, .  char *
a1510 7a 50 61 74 68 4f 75 74 0a 29 7b 0a 20 20 44 4f  zPathOut.){.  DO
a1520 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28  _OS_MALLOC_TEST(
a1530 30 29 3b 0a 20 20 7a 50 61 74 68 4f 75 74 5b 30  0);.  zPathOut[0
a1540 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  ] = 0;.  return 
a1550 70 56 66 73 2d 3e 78 46 75 6c 6c 50 61 74 68 6e  pVfs->xFullPathn
a1560 61 6d 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c  ame(pVfs, zPath,
a1570 20 6e 50 61 74 68 4f 75 74 2c 20 7a 50 61 74 68   nPathOut, zPath
a1580 4f 75 74 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  Out);.}.#ifndef 
a1590 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
a15a0 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54  _EXTENSION.SQLIT
a15b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
a15c0 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28  sqlite3OsDlOpen(
a15d0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
a15e0 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  s, const char *z
a15f0 50 61 74 68 29 7b 0a 20 20 72 65 74 75 72 6e 20  Path){.  return 
a1600 70 56 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 70 56  pVfs->xDlOpen(pV
a1610 66 73 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 53 51  fs, zPath);.}.SQ
a1620 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
a1630 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72  d sqlite3OsDlErr
a1640 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  or(sqlite3_vfs *
a1650 70 56 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c  pVfs, int nByte,
a1660 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b   char *zBufOut){
a1670 0a 20 20 70 56 66 73 2d 3e 78 44 6c 45 72 72 6f  .  pVfs->xDlErro
a1680 72 28 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a  r(pVfs, nByte, z
a1690 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54  BufOut);.}.SQLIT
a16a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 28  E_PRIVATE void (
a16b0 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28  *sqlite3OsDlSym(
a16c0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
a16d0 73 2c 20 76 6f 69 64 20 2a 70 48 64 6c 65 2c 20  s, void *pHdle, 
a16e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d  const char *zSym
a16f0 29 29 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75  ))(void){.  retu
a1700 72 6e 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28  rn pVfs->xDlSym(
a1710 70 56 66 73 2c 20 70 48 64 6c 65 2c 20 7a 53 79  pVfs, pHdle, zSy
a1720 6d 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  m);.}.SQLITE_PRI
a1730 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
a1740 33 4f 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74  3OsDlClose(sqlit
a1750 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f  e3_vfs *pVfs, vo
a1760 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20  id *pHandle){.  
a1770 70 56 66 73 2d 3e 78 44 6c 43 6c 6f 73 65 28 70  pVfs->xDlClose(p
a1780 56 66 73 2c 20 70 48 61 6e 64 6c 65 29 3b 0a 7d  Vfs, pHandle);.}
a1790 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
a17a0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
a17b0 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f  NSION */.SQLITE_
a17c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
a17d0 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28  te3OsRandomness(
a17e0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
a17f0 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68  s, int nByte, ch
a1800 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20  ar *zBufOut){.  
a1810 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 52 61  return pVfs->xRa
a1820 6e 64 6f 6d 6e 65 73 73 28 70 56 66 73 2c 20 6e  ndomness(pVfs, n
a1830 42 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a  Byte, zBufOut);.
a1840 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
a1850 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 6c   int sqlite3OsSl
a1860 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20  eep(sqlite3_vfs 
a1870 2a 70 56 66 73 2c 20 69 6e 74 20 6e 4d 69 63 72  *pVfs, int nMicr
a1880 6f 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66  o){.  return pVf
a1890 73 2d 3e 78 53 6c 65 65 70 28 70 56 66 73 2c 20  s->xSleep(pVfs, 
a18a0 6e 4d 69 63 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54  nMicro);.}.SQLIT
a18b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
a18c0 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69  lite3OsCurrentTi
a18d0 6d 65 49 6e 74 36 34 28 73 71 6c 69 74 65 33 5f  meInt64(sqlite3_
a18e0 76 66 73 20 2a 70 56 66 73 2c 20 73 71 6c 69 74  vfs *pVfs, sqlit
a18f0 65 33 5f 69 6e 74 36 34 20 2a 70 54 69 6d 65 4f  e3_int64 *pTimeO
a1900 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ut){.  int rc;. 
a1910 20 2f 2a 20 49 4d 50 4c 45 4d 45 4e 54 41 54 49   /* IMPLEMENTATI
a1920 4f 4e 2d 4f 46 3a 20 52 2d 34 39 30 34 35 2d 34  ON-OF: R-49045-4
a1930 32 34 39 33 20 53 51 4c 69 74 65 20 77 69 6c 6c  2493 SQLite will
a1940 20 75 73 65 20 74 68 65 20 78 43 75 72 72 65 6e   use the xCurren
a1950 74 54 69 6d 65 49 6e 74 36 34 28 29 0a 20 20 2a  tTimeInt64().  *
a1960 2a 20 6d 65 74 68 6f 64 20 74 6f 20 67 65 74 20  * method to get 
a1970 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 65  the current date
a1980 20 61 6e 64 20 74 69 6d 65 20 69 66 20 74 68 61   and time if tha
a1990 74 20 6d 65 74 68 6f 64 20 69 73 20 61 76 61 69  t method is avai
a19a0 6c 61 62 6c 65 0a 20 20 2a 2a 20 28 69 66 20 69  lable.  ** (if i
a19b0 56 65 72 73 69 6f 6e 20 69 73 20 32 20 6f 72 20  Version is 2 or 
a19c0 67 72 65 61 74 65 72 20 61 6e 64 20 74 68 65 20  greater and the 
a19d0 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
a19e0 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 61 6e   is not NULL) an
a19f0 64 0a 20 20 2a 2a 20 77 69 6c 6c 20 66 61 6c 6c  d.  ** will fall
a1a00 20 62 61 63 6b 20 74 6f 20 78 43 75 72 72 65 6e   back to xCurren
a1a10 74 54 69 6d 65 28 29 20 69 66 20 78 43 75 72 72  tTime() if xCurr
a1a20 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 29 20 69  entTimeInt64() i
a1a30 73 0a 20 20 2a 2a 20 75 6e 61 76 61 69 6c 61 62  s.  ** unavailab
a1a40 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  le..  */.  if( p
a1a50 56 66 73 2d 3e 69 56 65 72 73 69 6f 6e 3e 3d 32  Vfs->iVersion>=2
a1a60 20 26 26 20 70 56 66 73 2d 3e 78 43 75 72 72 65   && pVfs->xCurre
a1a70 6e 74 54 69 6d 65 49 6e 74 36 34 20 29 7b 0a 20  ntTimeInt64 ){. 
a1a80 20 20 20 72 63 20 3d 20 70 56 66 73 2d 3e 78 43     rc = pVfs->xC
a1a90 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28  urrentTimeInt64(
a1aa0 70 56 66 73 2c 20 70 54 69 6d 65 4f 75 74 29 3b  pVfs, pTimeOut);
a1ab0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f  .  }else{.    do
a1ac0 75 62 6c 65 20 72 3b 0a 20 20 20 20 72 63 20 3d  uble r;.    rc =
a1ad0 20 70 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54   pVfs->xCurrentT
a1ae0 69 6d 65 28 70 56 66 73 2c 20 26 72 29 3b 0a 20  ime(pVfs, &r);. 
a1af0 20 20 20 2a 70 54 69 6d 65 4f 75 74 20 3d 20 28     *pTimeOut = (
a1b00 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72  sqlite3_int64)(r
a1b10 2a 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20 20  *86400000.0);.  
a1b20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
a1b30 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
a1b40 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70   int sqlite3OsOp
a1b50 65 6e 4d 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69  enMalloc(.  sqli
a1b60 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a  te3_vfs *pVfs, .
a1b70 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
a1b80 69 6c 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f  ile, .  sqlite3_
a1b90 66 69 6c 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a  file **ppFile, .
a1ba0 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69    int flags,.  i
a1bb0 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b  nt *pOutFlags.){
a1bc0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
a1bd0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69  TE_NOMEM;.  sqli
a1be0 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b  te3_file *pFile;
a1bf0 0a 20 20 70 46 69 6c 65 20 3d 20 28 73 71 6c 69  .  pFile = (sqli
a1c00 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74  te3_file *)sqlit
a1c10 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 66  e3MallocZero(pVf
a1c20 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 20  s->szOsFile);.  
a1c30 69 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 20  if( pFile ){.   
a1c40 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f   rc = sqlite3OsO
a1c50 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65 2c  pen(pVfs, zFile,
a1c60 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 70   pFile, flags, p
a1c70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 69  OutFlags);.    i
a1c80 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
a1c90 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
a1ca0 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20  3_free(pFile);. 
a1cb0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a1cc0 2a 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65 3b  *ppFile = pFile;
a1cd0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
a1ce0 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45  urn rc;.}.SQLITE
a1cf0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
a1d00 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28  ite3OsCloseFree(
a1d10 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46  sqlite3_file *pF
a1d20 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ile){.  int rc =
a1d30 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73   SQLITE_OK;.  as
a1d40 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20  sert( pFile );. 
a1d50 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43   rc = sqlite3OsC
a1d60 6c 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20 73  lose(pFile);.  s
a1d70 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c  qlite3_free(pFil
a1d80 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  e);.  return rc;
a1d90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
a1da0 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72 61  unction is a wra
a1db0 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20  pper around the 
a1dc0 4f 53 20 73 70 65 63 69 66 69 63 20 69 6d 70 6c  OS specific impl
a1dd0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a 2a  ementation of.**
a1de0 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
a1df0 28 29 2e 20 54 68 65 20 70 75 72 70 6f 73 65 20  (). The purpose 
a1e00 6f 66 20 74 68 65 20 77 72 61 70 70 65 72 20 69  of the wrapper i
a1e10 73 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65  s to provide the
a1e20 0a 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f 20 73  .** ability to s
a1e30 69 6d 75 6c 61 74 65 20 61 20 6d 61 6c 6c 6f 63  imulate a malloc
a1e40 20 66 61 69 6c 75 72 65 2c 20 73 6f 20 74 68 61   failure, so tha
a1e50 74 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20 6f  t the handling o
a1e60 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 69 6e  f an.** error in
a1e70 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
a1e80 28 29 20 62 79 20 74 68 65 20 75 70 70 65 72 20  () by the upper 
a1e90 6c 61 79 65 72 73 20 63 61 6e 20 62 65 20 74 65  layers can be te
a1ea0 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sted..*/.SQLITE_
a1eb0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
a1ec0 74 65 33 4f 73 49 6e 69 74 28 76 6f 69 64 29 7b  te3OsInit(void){
a1ed0 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c  .  void *p = sql
a1ee0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 31 30 29 3b  ite3_malloc(10);
a1ef0 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
a1f00 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
a1f10 4d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  M;.  sqlite3_fre
a1f20 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  e(p);.  return s
a1f30 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29  qlite3_os_init()
a1f40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6c  ;.}../*.** The l
a1f50 69 73 74 20 6f 66 20 61 6c 6c 20 72 65 67 69 73  ist of all regis
a1f60 74 65 72 65 64 20 56 46 53 20 69 6d 70 6c 65 6d  tered VFS implem
a1f70 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  entations..*/.st
a1f80 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73  atic sqlite3_vfs
a1f90 20 2a 20 53 51 4c 49 54 45 5f 57 53 44 20 76 66   * SQLITE_WSD vf
a1fa0 73 4c 69 73 74 20 3d 20 30 3b 0a 23 64 65 66 69  sList = 0;.#defi
a1fb0 6e 65 20 76 66 73 4c 69 73 74 20 47 4c 4f 42 41  ne vfsList GLOBA
a1fc0 4c 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  L(sqlite3_vfs *,
a1fd0 20 76 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a 2a 2a   vfsList)../*.**
a1fe0 20 4c 6f 63 61 74 65 20 61 20 56 46 53 20 62 79   Locate a VFS by
a1ff0 20 6e 61 6d 65 2e 20 20 49 66 20 6e 6f 20 6e 61   name.  If no na
a2000 6d 65 20 69 73 20 67 69 76 65 6e 2c 20 73 69 6d  me is given, sim
a2010 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 0a 2a  ply return the.*
a2020 2a 20 66 69 72 73 74 20 56 46 53 20 6f 6e 20 74  * first VFS on t
a2030 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49  he list..*/.SQLI
a2040 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76  TE_API sqlite3_v
a2050 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f  fs *sqlite3_vfs_
a2060 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20  find(const char 
a2070 2a 7a 56 66 73 29 7b 0a 20 20 73 71 6c 69 74 65  *zVfs){.  sqlite
a2080 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 30 3b  3_vfs *pVfs = 0;
a2090 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
a20a0 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33  ADSAFE.  sqlite3
a20b0 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 23  _mutex *mutex;.#
a20c0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
a20d0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
a20e0 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71  IT.  int rc = sq
a20f0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
a2100 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  ();.  if( rc ) r
a2110 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a  eturn 0;.#endif.
a2120 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
a2130 44 53 41 46 45 0a 20 20 6d 75 74 65 78 20 3d 20  DSAFE.  mutex = 
a2140 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
a2150 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
a2160 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23  TATIC_MASTER);.#
a2170 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
a2180 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
a2190 78 29 3b 0a 20 20 66 6f 72 28 70 56 66 73 20 3d  x);.  for(pVfs =
a21a0 20 76 66 73 4c 69 73 74 3b 20 70 56 66 73 3b 20   vfsList; pVfs; 
a21b0 70 56 66 73 3d 70 56 66 73 2d 3e 70 4e 65 78 74  pVfs=pVfs->pNext
a21c0 29 7b 0a 20 20 20 20 69 66 28 20 7a 56 66 73 3d  ){.    if( zVfs=
a21d0 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
a21e0 69 66 28 20 73 74 72 63 6d 70 28 7a 56 66 73 2c  if( strcmp(zVfs,
a21f0 20 70 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d 3d 30   pVfs->zName)==0
a2200 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20   ) break;.  }.  
a2210 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
a2220 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65  ave(mutex);.  re
a2230 74 75 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a 2f 2a  turn pVfs;.}../*
a2240 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46 53  .** Unlink a VFS
a2250 20 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65 64   from the linked
a2260 20 6c 69 73 74 0a 2a 2f 0a 73 74 61 74 69 63 20   list.*/.static 
a2270 76 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28 73  void vfsUnlink(s
a2280 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
a2290 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
a22a0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
a22b0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
a22c0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
a22d0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 20 29  TATIC_MASTER)) )
a22e0 3b 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30 20  ;.  if( pVfs==0 
a22f0 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20  ){.    /* No-op 
a2300 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76  */.  }else if( v
a2310 66 73 4c 69 73 74 3d 3d 70 56 66 73 20 29 7b 0a  fsList==pVfs ){.
a2320 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56      vfsList = pV
a2330 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c  fs->pNext;.  }el
a2340 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 20 29  se if( vfsList )
a2350 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66  {.    sqlite3_vf
a2360 73 20 2a 70 20 3d 20 76 66 73 4c 69 73 74 3b 0a  s *p = vfsList;.
a2370 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 4e      while( p->pN
a2380 65 78 74 20 26 26 20 70 2d 3e 70 4e 65 78 74 21  ext && p->pNext!
a2390 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70  =pVfs ){.      p
a23a0 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20   = p->pNext;.   
a23b0 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4e   }.    if( p->pN
a23c0 65 78 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20  ext==pVfs ){.   
a23d0 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 56     p->pNext = pV
a23e0 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  fs->pNext;.    }
a23f0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
a2400 67 69 73 74 65 72 20 61 20 56 46 53 20 77 69 74  gister a VFS wit
a2410 68 20 74 68 65 20 73 79 73 74 65 6d 2e 20 20 49  h the system.  I
a2420 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f  t is harmless to
a2430 20 72 65 67 69 73 74 65 72 20 74 68 65 20 73 61   register the sa
a2440 6d 65 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 69 70  me.** VFS multip
a2450 6c 65 20 74 69 6d 65 73 2e 20 20 54 68 65 20 6e  le times.  The n
a2460 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74  ew VFS becomes t
a2470 68 65 20 64 65 66 61 75 6c 74 20 69 66 20 6d 61  he default if ma
a2480 6b 65 44 66 6c 74 20 69 73 0a 2a 2a 20 74 72 75  keDflt is.** tru
a2490 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
a24a0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73   int sqlite3_vfs
a24b0 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65  _register(sqlite
a24c0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
a24d0 20 6d 61 6b 65 44 66 6c 74 29 7b 0a 20 20 4d 55   makeDflt){.  MU
a24e0 54 45 58 5f 4c 4f 47 49 43 28 73 71 6c 69 74 65  TEX_LOGIC(sqlite
a24f0 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 29  3_mutex *mutex;)
a2500 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
a2510 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
a2520 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33  int rc = sqlite3
a2530 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20  _initialize();. 
a2540 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
a2550 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 4d 55   rc;.#endif.  MU
a2560 54 45 58 5f 4c 4f 47 49 43 28 20 6d 75 74 65 78  TEX_LOGIC( mutex
a2570 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
a2580 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
a2590 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
a25a0 3b 20 29 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ; ).  sqlite3_mu
a25b0 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
a25c0 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56  ;.  vfsUnlink(pV
a25d0 66 73 29 3b 0a 20 20 69 66 28 20 6d 61 6b 65 44  fs);.  if( makeD
a25e0 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 74 3d 3d  flt || vfsList==
a25f0 30 20 29 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70  0 ){.    pVfs->p
a2600 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 3b 0a  Next = vfsList;.
a2610 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56      vfsList = pV
a2620 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  fs;.  }else{.   
a2630 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76   pVfs->pNext = v
a2640 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20  fsList->pNext;. 
a2650 20 20 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78     vfsList->pNex
a2660 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 0a 20 20  t = pVfs;.  }.  
a2670 61 73 73 65 72 74 28 76 66 73 4c 69 73 74 29 3b  assert(vfsList);
a2680 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
a2690 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20  _leave(mutex);. 
a26a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
a26b0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 72 65  K;.}../*.** Unre
a26c0 67 69 73 74 65 72 20 61 20 56 46 53 20 73 6f 20  gister a VFS so 
a26d0 74 68 61 74 20 69 74 20 69 73 20 6e 6f 20 6c 6f  that it is no lo
a26e0 6e 67 65 72 20 61 63 63 65 73 73 69 62 6c 65 2e  nger accessible.
a26f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
a2700 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75  nt sqlite3_vfs_u
a2710 6e 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65  nregister(sqlite
a2720 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 23 69  3_vfs *pVfs){.#i
a2730 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
a2740 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  AFE.  sqlite3_mu
a2750 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c  tex *mutex = sql
a2760 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
a2770 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
a2780 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64  IC_MASTER);.#end
a2790 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  if.  sqlite3_mut
a27a0 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b  ex_enter(mutex);
a27b0 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66  .  vfsUnlink(pVf
a27c0 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  s);.  sqlite3_mu
a27d0 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29  tex_leave(mutex)
a27e0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
a27f0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  E_OK;.}../******
a2800 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
a2810 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  os.c ***********
a2820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2840 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
a2850 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
a2860 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a  ile fault.c ****
a2870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2890 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
a28a0 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a 2a 2a  008 Jan 22.**.**
a28b0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
a28c0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
a28d0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
a28e0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
a28f0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
a2900 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
a2910 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
a2920 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
a2930 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
a2940 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
a2950 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
a2960 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
a2970 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
a2980 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
a2990 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
a29a0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
a29b0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
a29c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a29d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a29e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a29f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a2a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
a2a10 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
a2a20 6e 73 20 63 6f 64 65 20 74 6f 20 73 75 70 70 6f  ns code to suppo
a2a30 72 74 20 74 68 65 20 63 6f 6e 63 65 70 74 20 6f  rt the concept o
a2a40 66 20 22 62 65 6e 69 67 6e 22 20 0a 2a 2a 20 6d  f "benign" .** m
a2a50 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 28  alloc failures (
a2a60 77 68 65 6e 20 74 68 65 20 78 4d 61 6c 6c 6f 63  when the xMalloc
a2a70 28 29 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28 29  () or xRealloc()
a2a80 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a   method of the.*
a2a90 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  * sqlite3_mem_me
a2aa0 74 68 6f 64 73 20 73 74 72 75 63 74 75 72 65 20  thods structure 
a2ab0 66 61 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74  fails to allocat
a2ac0 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d  e a block of mem
a2ad0 6f 72 79 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72  ory.** and retur
a2ae0 6e 73 20 30 29 2e 20 0a 2a 2a 0a 2a 2a 20 4d 6f  ns 0). .**.** Mo
a2af0 73 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  st malloc failur
a2b00 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 67  es are non-benig
a2b10 6e 2e 20 41 66 74 65 72 20 74 68 65 79 20 6f 63  n. After they oc
a2b20 63 75 72 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 61  cur, SQLite.** a
a2b30 62 61 6e 64 6f 6e 73 20 74 68 65 20 63 75 72 72  bandons the curr
a2b40 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e  ent operation an
a2b50 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72  d returns an err
a2b60 6f 72 20 63 6f 64 65 20 28 75 73 75 61 6c 6c 79  or code (usually
a2b70 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  .** SQLITE_NOMEM
a2b80 29 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 48  ) to the user. H
a2b90 6f 77 65 76 65 72 2c 20 73 6f 6d 65 74 69 6d 65  owever, sometime
a2ba0 73 20 61 20 66 61 75 6c 74 20 69 73 20 6e 6f 74  s a fault is not
a2bb0 20 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20   necessarily.** 
a2bc0 66 61 74 61 6c 2e 20 46 6f 72 20 65 78 61 6d 70  fatal. For examp
a2bd0 6c 65 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20  le, if a malloc 
a2be0 66 61 69 6c 73 20 77 68 69 6c 65 20 72 65 73 69  fails while resi
a2bf0 7a 69 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c  zing a hash tabl
a2c00 65 2c 20 74 68 69 73 20 0a 2a 2a 20 69 73 20 63  e, this .** is c
a2c10 6f 6d 70 6c 65 74 65 6c 79 20 72 65 63 6f 76 65  ompletely recove
a2c20 72 61 62 6c 65 20 73 69 6d 70 6c 79 20 62 79 20  rable simply by 
a2c30 6e 6f 74 20 63 61 72 72 79 69 6e 67 20 6f 75 74  not carrying out
a2c40 20 74 68 65 20 72 65 73 69 7a 65 2e 20 54 68 65   the resize. The
a2c50 20 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20   .** hash table 
a2c60 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f  will continue to
a2c70 20 66 75 6e 63 74 69 6f 6e 20 6e 6f 72 6d 61 6c   function normal
a2c80 6c 79 2e 20 20 53 6f 20 61 20 6d 61 6c 6c 6f 63  ly.  So a malloc
a2c90 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 64 75 72   failure .** dur
a2ca0 69 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c 65  ing a hash table
a2cb0 20 72 65 73 69 7a 65 20 69 73 20 61 20 62 65 6e   resize is a ben
a2cc0 69 67 6e 20 66 61 75 6c 74 2e 0a 2a 2f 0a 0a 0a  ign fault..*/...
a2cd0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
a2ce0 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54  MIT_BUILTIN_TEST
a2cf0 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 76  ../*.** Global v
a2d00 61 72 69 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79 70  ariables..*/.typ
a2d10 65 64 65 66 20 73 74 72 75 63 74 20 42 65 6e 69  edef struct Beni
a2d20 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 42 65  gnMallocHooks Be
a2d30 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 3b  nignMallocHooks;
a2d40 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57  .static SQLITE_W
a2d50 53 44 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e  SD struct Benign
a2d60 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 7b 0a 20 20  MallocHooks {.  
a2d70 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 65  void (*xBenignBe
a2d80 67 69 6e 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f  gin)(void);.  vo
a2d90 69 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29  id (*xBenignEnd)
a2da0 28 76 6f 69 64 29 3b 0a 7d 20 73 71 6c 69 74 65  (void);.} sqlite
a2db0 33 48 6f 6f 6b 73 20 3d 20 7b 20 30 2c 20 30 20  3Hooks = { 0, 0 
a2dc0 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 48  };../* The "wsdH
a2dd0 6f 6f 6b 73 22 20 6d 61 63 72 6f 20 77 69 6c 6c  ooks" macro will
a2de0 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20   resolve to the 
a2df0 61 70 70 72 6f 70 72 69 61 74 65 20 42 65 6e 69  appropriate Beni
a2e00 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 0a 2a 2a  gnMallocHooks.**
a2e10 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 66 20   structure.  If 
a2e20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20  writable static 
a2e30 64 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72  data is unsuppor
a2e40 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65  ted on the targe
a2e50 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f  t,.** we have to
a2e60 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74   locate the stat
a2e70 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d  e vector at run-
a2e80 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f  time.  In the mo
a2e90 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73  re common.** cas
a2ea0 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65  e where writable
a2eb0 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20   static data is 
a2ec0 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 48 6f  supported, wsdHo
a2ed0 6f 6b 73 20 63 61 6e 20 72 65 66 65 72 20 64 69  oks can refer di
a2ee0 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65  rectly.** to the
a2ef0 20 22 73 71 6c 69 74 65 33 48 6f 6f 6b 73 22 20   "sqlite3Hooks" 
a2f00 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63  state vector dec
a2f10 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a  lared above..*/.
a2f20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
a2f30 49 54 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20  IT_WSD.# define 
a2f40 77 73 64 48 6f 6f 6b 73 49 6e 69 74 20 5c 0a 20  wsdHooksInit \. 
a2f50 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f   BenignMallocHoo
a2f60 6b 73 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28  ks *x = &GLOBAL(
a2f70 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b  BenignMallocHook
a2f80 73 2c 73 71 6c 69 74 65 33 48 6f 6f 6b 73 29 0a  s,sqlite3Hooks).
a2f90 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b  # define wsdHook
a2fa0 73 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64  s x[0].#else.# d
a2fb0 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e  efine wsdHooksIn
a2fc0 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48  it.# define wsdH
a2fd0 6f 6f 6b 73 20 73 71 6c 69 74 65 33 48 6f 6f 6b  ooks sqlite3Hook
a2fe0 73 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  s.#endif.../*.**
a2ff0 20 52 65 67 69 73 74 65 72 20 68 6f 6f 6b 73 20   Register hooks 
a3000 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20 73 71 6c  to call when sql
a3010 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
a3020 61 6c 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a 20 73  alloc() and.** s
a3030 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
a3040 61 6c 6c 6f 63 28 29 20 61 72 65 20 63 61 6c 6c  alloc() are call
a3050 65 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79  ed, respectively
a3060 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
a3070 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
a3080 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b  BenignMallocHook
a3090 73 28 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e  s(.  void (*xBen
a30a0 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 2c  ignBegin)(void),
a30b0 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67  .  void (*xBenig
a30c0 6e 45 6e 64 29 28 76 6f 69 64 29 0a 29 7b 0a 20  nEnd)(void).){. 
a30d0 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20   wsdHooksInit;. 
a30e0 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67   wsdHooks.xBenig
a30f0 6e 42 65 67 69 6e 20 3d 20 78 42 65 6e 69 67 6e  nBegin = xBenign
a3100 42 65 67 69 6e 3b 0a 20 20 77 73 64 48 6f 6f 6b  Begin;.  wsdHook
a3110 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20 78  s.xBenignEnd = x
a3120 42 65 6e 69 67 6e 45 6e 64 3b 0a 7d 0a 0a 2f 2a  BenignEnd;.}../*
a3130 0a 2a 2a 20 54 68 69 73 20 28 73 71 6c 69 74 65  .** This (sqlite
a3140 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
a3150 28 29 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ()) is called by
a3160 20 53 51 4c 69 74 65 20 63 6f 64 65 20 74 6f 20   SQLite code to 
a3170 69 6e 64 69 63 61 74 65 20 74 68 61 74 0a 2a 2a  indicate that.**
a3180 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c   subsequent mall
a3190 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 20  oc failures are 
a31a0 62 65 6e 69 67 6e 2e 20 41 20 63 61 6c 6c 20 74  benign. A call t
a31b0 6f 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69  o sqlite3EndBeni
a31c0 67 6e 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 6e  gnMalloc().** in
a31d0 64 69 63 61 74 65 73 20 74 68 61 74 20 73 75 62  dicates that sub
a31e0 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66  sequent malloc f
a31f0 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d  ailures are non-
a3200 62 65 6e 69 67 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  benign..*/.SQLIT
a3210 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
a3220 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
a3230 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20  nMalloc(void){. 
a3240 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20   wsdHooksInit;. 
a3250 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42   if( wsdHooks.xB
a3260 65 6e 69 67 6e 42 65 67 69 6e 20 29 7b 0a 20 20  enignBegin ){.  
a3270 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69    wsdHooks.xBeni
a3280 67 6e 42 65 67 69 6e 28 29 3b 0a 20 20 7d 0a 7d  gnBegin();.  }.}
a3290 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a32a0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42  void sqlite3EndB
a32b0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64  enignMalloc(void
a32c0 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69  ){.  wsdHooksIni
a32d0 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b  t;.  if( wsdHook
a32e0 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 29 7b 0a  s.xBenignEnd ){.
a32f0 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65      wsdHooks.xBe
a3300 6e 69 67 6e 45 6e 64 28 29 3b 0a 20 20 7d 0a 7d  nignEnd();.  }.}
a3310 0a 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 23 69  ..#endif   /* #i
a3320 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
a3330 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a  T_BUILTIN_TEST *
a3340 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
a3350 2a 2a 20 45 6e 64 20 6f 66 20 66 61 75 6c 74 2e  ** End of fault.
a3360 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
a3370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3390 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
a33a0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65  ** Begin file me
a33b0 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m0.c ***********
a33c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a33d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a33e0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63  */./*.** 2008 Oc
a33f0 74 6f 62 65 72 20 32 38 0a 2a 2a 0a 2a 2a 20 54  tober 28.**.** T
a3400 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
a3410 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
a3420 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
a3430 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
a3440 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
a3450 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
a3460 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
a3470 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
a3480 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
a3490 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
a34a0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
a34b0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
a34c0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
a34d0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
a34e0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
a34f0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
a3500 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
a3510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3550 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
a3560 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
a3570 20 61 20 6e 6f 2d 6f 70 20 6d 65 6d 6f 72 79 20   a no-op memory 
a3580 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65  allocation drive
a3590 72 73 20 66 6f 72 20 75 73 65 20 77 68 65 6e 0a  rs for use when.
a35a0 2a 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d  ** SQLITE_ZERO_M
a35b0 41 4c 4c 4f 43 20 69 73 20 64 65 66 69 6e 65 64  ALLOC is defined
a35c0 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f  .  The allocatio
a35d0 6e 20 64 72 69 76 65 72 73 20 69 6d 70 6c 65 6d  n drivers implem
a35e0 65 6e 74 65 64 0a 2a 2a 20 68 65 72 65 20 61 6c  ented.** here al
a35f0 77 61 79 73 20 66 61 69 6c 2e 20 20 53 51 4c 69  ways fail.  SQLi
a3600 74 65 20 77 69 6c 6c 20 6e 6f 74 20 6f 70 65 72  te will not oper
a3610 61 74 65 20 77 69 74 68 20 74 68 65 73 65 20 64  ate with these d
a3620 72 69 76 65 72 73 2e 20 20 54 68 65 73 65 0a 2a  rivers.  These.*
a3630 2a 20 61 72 65 20 6d 65 72 65 6c 79 20 70 6c 61  * are merely pla
a3640 63 65 68 6f 6c 64 65 72 73 2e 20 20 52 65 61 6c  ceholders.  Real
a3650 20 64 72 69 76 65 72 73 20 6d 75 73 74 20 62 65   drivers must be
a3660 20 73 75 62 73 74 69 74 75 74 65 64 20 75 73 69   substituted usi
a3670 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  ng.** sqlite3_co
a3680 6e 66 69 67 28 29 20 62 65 66 6f 72 65 20 53 51  nfig() before SQ
a3690 4c 69 74 65 20 77 69 6c 6c 20 6f 70 65 72 61 74  Lite will operat
a36a0 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  e..*/../*.** Thi
a36b0 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  s version of the
a36c0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
a36d0 72 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74  r is the default
a36e0 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64  .  It is.** used
a36f0 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 6d   when no other m
a3700 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
a3710 69 73 20 73 70 65 63 69 66 69 65 64 20 75 73 69  is specified usi
a3720 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a  ng compile-time.
a3730 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69  ** macros..*/.#i
a3740 66 64 65 66 20 53 51 4c 49 54 45 5f 5a 45 52 4f  fdef SQLITE_ZERO
a3750 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4e  _MALLOC../*.** N
a3760 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73 20 6f 66  o-op versions of
a3770 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   all memory allo
a3780 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 0a  cation routines.
a3790 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
a37a0 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63  sqlite3MemMalloc
a37b0 28 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 65 74  (int nByte){ ret
a37c0 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20  urn 0; }.static 
a37d0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46  void sqlite3MemF
a37e0 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ree(void *pPrior
a37f0 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61  ){ return; }.sta
a3800 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  tic void *sqlite
a3810 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64  3MemRealloc(void
a3820 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42   *pPrior, int nB
a3830 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30 3b 20  yte){ return 0; 
a3840 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  }.static int sql
a3850 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64  ite3MemSize(void
a3860 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 75 72   *pPrior){ retur
a3870 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e  n 0; }.static in
a3880 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e  t sqlite3MemRoun
a3890 64 75 70 28 69 6e 74 20 6e 29 7b 20 72 65 74 75  dup(int n){ retu
a38a0 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 69 63 20 69  rn n; }.static i
a38b0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69  nt sqlite3MemIni
a38c0 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
a38d0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  { return SQLITE_
a38e0 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69  OK; }.static voi
a38f0 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74  d sqlite3MemShut
a3900 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73  down(void *NotUs
a3910 65 64 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 0a  ed){ return; }..
a3920 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
a3930 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72  ne is the only r
a3940 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66  outine in this f
a3950 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61  ile with externa
a3960 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a  l linkage..**.**
a3970 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f   Populate the lo
a3980 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
a3990 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69  llocation functi
a39a0 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a  on pointers in.*
a39b0 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  * sqlite3GlobalC
a39c0 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69  onfig.m with poi
a39d0 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75  nters to the rou
a39e0 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
a39f0 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
a3a00 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
a3a10 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28  e3MemSetDefault(
a3a20 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
a3a30 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65  const sqlite3_me
a3a40 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c  m_methods defaul
a3a50 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20  tMethods = {.   
a3a60 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c    sqlite3MemMall
a3a70 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  oc,.     sqlite3
a3a80 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71  MemFree,.     sq
a3a90 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c  lite3MemRealloc,
a3aa0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
a3ab0 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74  Size,.     sqlit
a3ac0 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20  e3MemRoundup,.  
a3ad0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69     sqlite3MemIni
a3ae0 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  t,.     sqlite3M
a3af0 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20  emShutdown,.    
a3b00 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65   0.  };.  sqlite
a3b10 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
a3b20 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26  CONFIG_MALLOC, &
a3b30 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b  defaultMethods);
a3b40 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
a3b50 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43  LITE_ZERO_MALLOC
a3b60 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
a3b70 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 30  **** End of mem0
a3b80 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
a3b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3bb0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
a3bc0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
a3bd0 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem1.c *********
a3be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3c00 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
a3c10 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20  August 14.**.** 
a3c20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
a3c30 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
a3c40 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
a3c50 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
a3c60 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
a3c70 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
a3c80 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
a3c90 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
a3ca0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
a3cb0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
a3cc0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
a3cd0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
a3ce0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
a3cf0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
a3d00 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
a3d10 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
a3d20 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
a3d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a3d70 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
a3d80 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
a3d90 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f  s low-level memo
a3da0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72  ry allocation dr
a3db0 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a 2a  ivers for when.*
a3dc0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73  * SQLite will us
a3dd0 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43  e the standard C
a3de0 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63 2f  -library malloc/
a3df0 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e 74  realloc/free int
a3e00 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62 74  erface.** to obt
a3e10 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 69  ain the memory i
a3e20 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54  t needs..**.** T
a3e30 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
a3e40 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
a3e50 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76  s of the low-lev
a3e60 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  el memory alloca
a3e70 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  tion.** routines
a3e80 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
a3e90 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  e sqlite3_mem_me
a3ea0 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 20 20 54  thods object.  T
a3eb0 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a  he content of.**
a3ec0 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e   this file is on
a3ed0 6c 79 20 75 73 65 64 20 69 66 20 53 51 4c 49 54  ly used if SQLIT
a3ee0 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20  E_SYSTEM_MALLOC 
a3ef0 69 73 20 64 65 66 69 6e 65 64 2e 20 20 54 68 65  is defined.  The
a3f00 0a 2a 2a 20 53 51 4c 49 54 45 5f 53 59 53 54 45  .** SQLITE_SYSTE
a3f10 4d 5f 4d 41 4c 4c 4f 43 20 6d 61 63 72 6f 20 69  M_MALLOC macro i
a3f20 73 20 64 65 66 69 6e 65 64 20 61 75 74 6f 6d 61  s defined automa
a3f30 74 69 63 61 6c 6c 79 20 69 66 20 6e 65 69 74 68  tically if neith
a3f40 65 72 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45  er the.** SQLITE
a3f50 5f 4d 45 4d 44 45 42 55 47 20 6e 6f 72 20 74 68  _MEMDEBUG nor th
a3f60 65 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 4d  e SQLITE_WIN32_M
a3f70 41 4c 4c 4f 43 20 6d 61 63 72 6f 73 20 61 72 65  ALLOC macros are
a3f80 20 64 65 66 69 6e 65 64 2e 20 20 54 68 65 0a 2a   defined.  The.*
a3f90 2a 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67  * default config
a3fa0 75 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 75 73  uration is to us
a3fb0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
a3fc0 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 69 6e 20  ion routines in 
a3fd0 74 68 69 73 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a  this.** file..**
a3fe0 0a 2a 2a 20 43 2d 70 72 65 70 72 6f 63 65 73 73  .** C-preprocess
a3ff0 6f 72 20 6d 61 63 72 6f 20 73 75 6d 6d 61 72 79  or macro summary
a4000 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 48 41 56 45 5f  :.**.**    HAVE_
a4010 4d 41 4c 4c 4f 43 5f 55 53 41 42 4c 45 5f 53 49  MALLOC_USABLE_SI
a4020 5a 45 20 20 20 20 20 54 68 65 20 63 6f 6e 66 69  ZE     The confi
a4030 67 75 72 65 20 73 63 72 69 70 74 20 73 65 74 73  gure script sets
a4040 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 69 66 0a   this symbol if.
a4050 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
a4060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4070 20 20 74 68 65 20 6d 61 6c 6c 6f 63 5f 75 73 61    the malloc_usa
a4080 62 6c 65 5f 73 69 7a 65 28 29 20 69 6e 74 65 72  ble_size() inter
a4090 66 61 63 65 20 65 78 69 73 74 73 0a 2a 2a 20 20  face exists.**  
a40a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a40b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e                on
a40c0 20 74 68 65 20 74 61 72 67 65 74 20 70 6c 61 74   the target plat
a40d0 66 6f 72 6d 2e 20 20 4f 72 2c 20 74 68 69 73 20  form.  Or, this 
a40e0 73 79 6d 62 6f 6c 0a 2a 2a 20 20 20 20 20 20 20  symbol.**       
a40f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4100 20 20 20 20 20 20 20 20 20 63 61 6e 20 62 65 20           can be 
a4110 73 65 74 20 6d 61 6e 75 61 6c 6c 79 2c 20 69 66  set manually, if
a4120 20 64 65 73 69 72 65 64 2e 0a 2a 2a 20 20 20 20   desired..**    
a4130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4140 20 20 20 20 20 20 20 20 20 20 20 20 49 66 20 61              If a
a4150 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 74  n equivalent int
a4160 65 72 66 61 63 65 20 65 78 69 73 74 73 20 62 79  erface exists by
a4170 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
a4180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4190 20 20 20 61 20 64 69 66 66 65 72 65 6e 74 20 6e     a different n
a41a0 61 6d 65 2c 20 75 73 69 6e 67 20 61 20 73 65 70  ame, using a sep
a41b0 61 72 61 74 65 20 2d 44 0a 2a 2a 20 20 20 20 20  arate -D.**     
a41c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a41d0 20 20 20 20 20 20 20 20 20 20 20 6f 70 74 69 6f             optio
a41e0 6e 20 74 6f 20 72 65 6e 61 6d 65 20 69 74 2e 0a  n to rename it..
a41f0 2a 2a 0a 2a 2a 20 20 20 20 53 51 4c 49 54 45 5f  **.**    SQLITE_
a4200 57 49 54 48 4f 55 54 5f 5a 4f 4e 45 4d 41 4c 4c  WITHOUT_ZONEMALL
a4210 4f 43 20 20 20 53 6f 6d 65 20 6f 6c 64 65 72 20  OC   Some older 
a4220 6d 61 63 73 20 6c 61 63 6b 20 73 75 70 70 6f 72  macs lack suppor
a4230 74 20 66 6f 72 20 74 68 65 20 7a 6f 6e 65 0a 2a  t for the zone.*
a4240 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
a4250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4260 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
a4270 72 2e 20 20 53 65 74 20 74 68 69 73 20 73 79 6d  r.  Set this sym
a4280 62 6f 6c 20 74 6f 20 65 6e 61 62 6c 65 0a 2a 2a  bol to enable.**
a4290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a42a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a42b0 62 75 69 6c 64 69 6e 67 20 6f 6e 20 6f 6c 64 65  building on olde
a42c0 72 20 6d 61 63 73 2e 0a 2a 2a 0a 2a 2a 20 20 20  r macs..**.**   
a42d0 20 53 51 4c 49 54 45 5f 57 49 54 48 4f 55 54 5f   SQLITE_WITHOUT_
a42e0 4d 53 49 5a 45 20 20 20 20 20 20 20 20 53 65 74  MSIZE        Set
a42f0 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 74 6f 20   this symbol to 
a4300 64 69 73 61 62 6c 65 20 74 68 65 20 75 73 65 20  disable the use 
a4310 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  of.**           
a4320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4330 20 20 20 20 20 5f 6d 73 69 7a 65 28 29 20 6f 6e       _msize() on
a4340 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73   windows systems
a4350 2e 20 20 54 68 69 73 20 6d 69 67 68 74 0a 2a 2a  .  This might.**
a4360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a4380 62 65 20 6e 65 63 65 73 73 61 72 79 20 77 68 65  be necessary whe
a4390 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20  n compiling for 
a43a0 44 65 6c 70 68 69 2c 0a 2a 2a 20 20 20 20 20 20  Delphi,.**      
a43b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a43c0 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 65 78            for ex
a43d0 61 6d 70 6c 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ample..*/../*.**
a43e0 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   This version of
a43f0 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
a4400 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 66  cator is the def
a4410 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20  ault.  It is.** 
a4420 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68  used when no oth
a4430 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  er memory alloca
a4440 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 64  tor is specified
a4450 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74   using compile-t
a4460 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a  ime.** macros..*
a4470 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
a4480 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a 2f  SYSTEM_MALLOC../
a4490 2a 0a 2a 2a 20 54 68 65 20 4d 53 56 43 52 54 20  *.** The MSVCRT 
a44a0 68 61 73 20 6d 61 6c 6c 6f 63 5f 75 73 61 62 6c  has malloc_usabl
a44b0 65 5f 73 69 7a 65 28 29 20 62 75 74 20 69 74 20  e_size() but it 
a44c0 69 73 20 63 61 6c 6c 65 64 20 5f 6d 73 69 7a 65  is called _msize
a44d0 28 29 2e 0a 2a 2a 20 54 68 65 20 75 73 65 20 6f  ()..** The use o
a44e0 66 20 5f 6d 73 69 7a 65 28 29 20 69 73 20 61 75  f _msize() is au
a44f0 74 6f 6d 61 74 69 63 2c 20 62 75 74 20 63 61 6e  tomatic, but can
a4500 20 62 65 20 64 69 73 61 62 6c 65 64 20 62 79 20   be disabled by 
a4510 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 77 69 74  compiling.** wit
a4520 68 20 2d 44 53 51 4c 49 54 45 5f 57 49 54 48 4f  h -DSQLITE_WITHO
a4530 55 54 5f 4d 53 49 5a 45 0a 2a 2f 0a 23 69 66 20  UT_MSIZE.*/.#if 
a4540 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52  defined(_MSC_VER
a4550 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
a4560 4c 49 54 45 5f 57 49 54 48 4f 55 54 5f 4d 53 49  LITE_WITHOUT_MSI
a4570 5a 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ZE).# define SQL
a4580 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a 45 20 5f  ITE_MALLOCSIZE _
a4590 6d 73 69 7a 65 0a 23 65 6e 64 69 66 0a 0a 23 69  msize.#endif..#i
a45a0 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  f defined(__APPL
a45b0 45 5f 5f 29 20 26 26 20 21 64 65 66 69 6e 65 64  E__) && !defined
a45c0 28 53 51 4c 49 54 45 5f 57 49 54 48 4f 55 54 5f  (SQLITE_WITHOUT_
a45d0 5a 4f 4e 45 4d 41 4c 4c 4f 43 29 0a 0a 2f 2a 0a  ZONEMALLOC)../*.
a45e0 2a 2a 20 55 73 65 20 74 68 65 20 7a 6f 6e 65 20  ** Use the zone 
a45f0 61 6c 6c 6f 63 61 74 6f 72 20 61 76 61 69 6c 61  allocator availa
a4600 62 6c 65 20 6f 6e 20 61 70 70 6c 65 20 70 72 6f  ble on apple pro
a4610 64 75 63 74 73 20 75 6e 6c 65 73 73 20 74 68 65  ducts unless the
a4620 0a 2a 2a 20 53 51 4c 49 54 45 5f 57 49 54 48 4f  .** SQLITE_WITHO
a4630 55 54 5f 5a 4f 4e 45 4d 41 4c 4c 4f 43 20 73 79  UT_ZONEMALLOC sy
a4640 6d 62 6f 6c 20 69 73 20 64 65 66 69 6e 65 64 2e  mbol is defined.
a4650 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79  .*/.#include <sy
a4660 73 2f 73 79 73 63 74 6c 2e 68 3e 0a 23 69 6e 63  s/sysctl.h>.#inc
a4670 6c 75 64 65 20 3c 6d 61 6c 6c 6f 63 2f 6d 61 6c  lude <malloc/mal
a4680 6c 6f 63 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  loc.h>.#include 
a4690 3c 6c 69 62 6b 65 72 6e 2f 4f 53 41 74 6f 6d 69  <libkern/OSAtomi
a46a0 63 2e 68 3e 0a 73 74 61 74 69 63 20 6d 61 6c 6c  c.h>.static mall
a46b0 6f 63 5f 7a 6f 6e 65 5f 74 2a 20 5f 73 71 6c 69  oc_zone_t* _sqli
a46c0 74 65 5a 6f 6e 65 5f 3b 0a 23 64 65 66 69 6e 65  teZone_;.#define
a46d0 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 28 78   SQLITE_MALLOC(x
a46e0 29 20 6d 61 6c 6c 6f 63 5f 7a 6f 6e 65 5f 6d 61  ) malloc_zone_ma
a46f0 6c 6c 6f 63 28 5f 73 71 6c 69 74 65 5a 6f 6e 65  lloc(_sqliteZone
a4700 5f 2c 20 28 78 29 29 0a 23 64 65 66 69 6e 65 20  _, (x)).#define 
a4710 53 51 4c 49 54 45 5f 46 52 45 45 28 78 29 20 6d  SQLITE_FREE(x) m
a4720 61 6c 6c 6f 63 5f 7a 6f 6e 65 5f 66 72 65 65 28  alloc_zone_free(
a4730 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f 2c 20 28 78  _sqliteZone_, (x
a4740 29 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  ));.#define SQLI
a4750 54 45 5f 52 45 41 4c 4c 4f 43 28 78 2c 79 29 20  TE_REALLOC(x,y) 
a4760 6d 61 6c 6c 6f 63 5f 7a 6f 6e 65 5f 72 65 61 6c  malloc_zone_real
a4770 6c 6f 63 28 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f  loc(_sqliteZone_
a4780 2c 20 28 78 29 2c 20 28 79 29 29 0a 23 64 65 66  , (x), (y)).#def
a4790 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f  ine SQLITE_MALLO
a47a0 43 53 49 5a 45 28 78 29 20 5c 0a 20 20 20 20 20  CSIZE(x) \.     
a47b0 20 20 20 28 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f     (_sqliteZone_
a47c0 20 3f 20 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f 2d   ? _sqliteZone_-
a47d0 3e 73 69 7a 65 28 5f 73 71 6c 69 74 65 5a 6f 6e  >size(_sqliteZon
a47e0 65 5f 2c 78 29 20 3a 20 6d 61 6c 6c 6f 63 5f 73  e_,x) : malloc_s
a47f0 69 7a 65 28 78 29 29 0a 0a 23 65 6c 73 65 20 2f  ize(x))..#else /
a4800 2a 20 69 66 20 6e 6f 74 20 5f 5f 41 50 50 4c 45  * if not __APPLE
a4810 5f 5f 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 55 73 65  __ */../*.** Use
a4820 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72   standard C libr
a4830 61 72 79 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 66  ary malloc and f
a4840 72 65 65 20 6f 6e 20 6e 6f 6e 2d 41 70 70 6c 65  ree on non-Apple
a4850 20 73 79 73 74 65 6d 73 2e 20 20 0a 2a 2a 20 41   systems.  .** A
a4860 6c 73 6f 20 75 73 65 64 20 62 79 20 41 70 70 6c  lso used by Appl
a4870 65 20 73 79 73 74 65 6d 73 20 69 66 20 53 51 4c  e systems if SQL
a4880 49 54 45 5f 57 49 54 48 4f 55 54 5f 5a 4f 4e 45  ITE_WITHOUT_ZONE
a4890 4d 41 4c 4c 4f 43 20 69 73 20 64 65 66 69 6e 65  MALLOC is define
a48a0 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  d..*/.#define SQ
a48b0 4c 49 54 45 5f 4d 41 4c 4c 4f 43 28 78 29 20 20  LITE_MALLOC(x)  
a48c0 20 20 6d 61 6c 6c 6f 63 28 78 29 0a 23 64 65 66    malloc(x).#def
a48d0 69 6e 65 20 53 51 4c 49 54 45 5f 46 52 45 45 28  ine SQLITE_FREE(
a48e0 78 29 20 20 20 20 20 20 66 72 65 65 28 78 29 0a  x)      free(x).
a48f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
a4900 45 41 4c 4c 4f 43 28 78 2c 79 29 20 72 65 61 6c  EALLOC(x,y) real
a4910 6c 6f 63 28 28 78 29 2c 28 79 29 29 0a 0a 23 69  loc((x),(y))..#i
a4920 66 20 28 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f  f (defined(_MSC_
a4930 56 45 52 29 20 26 26 20 21 64 65 66 69 6e 65 64  VER) && !defined
a4940 28 53 51 4c 49 54 45 5f 57 49 54 48 4f 55 54 5f  (SQLITE_WITHOUT_
a4950 4d 53 49 5a 45 29 29 20 5c 0a 20 20 20 20 20 20  MSIZE)) \.      
a4960 7c 7c 20 28 64 65 66 69 6e 65 64 28 48 41 56 45  || (defined(HAVE
a4970 5f 4d 41 4c 4c 4f 43 5f 48 29 20 26 26 20 64 65  _MALLOC_H) && de
a4980 66 69 6e 65 64 28 48 41 56 45 5f 4d 41 4c 4c 4f  fined(HAVE_MALLO
a4990 43 5f 55 53 41 42 4c 45 5f 53 49 5a 45 29 29 0a  C_USABLE_SIZE)).
a49a0 23 20 69 6e 63 6c 75 64 65 20 3c 6d 61 6c 6c 6f  # include <mallo
a49b0 63 2e 68 3e 20 20 20 20 2f 2a 20 4e 65 65 64 65  c.h>    /* Neede
a49c0 64 20 66 6f 72 20 6d 61 6c 6c 6f 63 5f 75 73 61  d for malloc_usa
a49d0 62 6c 65 5f 73 69 7a 65 20 6f 6e 20 6c 69 6e 75  ble_size on linu
a49e0 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64  x */.#endif.#ifd
a49f0 65 66 20 48 41 56 45 5f 4d 41 4c 4c 4f 43 5f 55  ef HAVE_MALLOC_U
a4a00 53 41 42 4c 45 5f 53 49 5a 45 0a 23 20 69 66 6e  SABLE_SIZE.# ifn
a4a10 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f  def SQLITE_MALLO
a4a20 43 53 49 5a 45 0a 23 20 20 64 65 66 69 6e 65 20  CSIZE.#  define 
a4a30 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a  SQLITE_MALLOCSIZ
a4a40 45 28 78 29 20 6d 61 6c 6c 6f 63 5f 75 73 61 62  E(x) malloc_usab
a4a50 6c 65 5f 73 69 7a 65 28 78 29 0a 23 20 65 6e 64  le_size(x).# end
a4a60 69 66 0a 23 65 6c 73 65 0a 23 20 75 6e 64 65 66  if.#else.# undef
a4a70 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49   SQLITE_MALLOCSI
a4a80 5a 45 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69  ZE.#endif..#endi
a4a90 66 20 2f 2a 20 5f 5f 41 50 50 4c 45 5f 5f 20 6f  f /* __APPLE__ o
a4aa0 72 20 6e 6f 74 20 5f 5f 41 50 50 4c 45 5f 5f 20  r not __APPLE__ 
a4ab0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 6d  */../*.** Like m
a4ac0 61 6c 6c 6f 63 28 29 2c 20 62 75 74 20 72 65 6d  alloc(), but rem
a4ad0 65 6d 62 65 72 20 74 68 65 20 73 69 7a 65 20 6f  ember the size o
a4ae0 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
a4af0 0a 2a 2a 20 73 6f 20 74 68 61 74 20 77 65 20 63  .** so that we c
a4b00 61 6e 20 66 69 6e 64 20 69 74 20 6c 61 74 65 72  an find it later
a4b10 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4d 65   using sqlite3Me
a4b20 6d 53 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 46  mSize()..**.** F
a4b30 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65  or this low-leve
a4b40 6c 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 72  l routine, we ar
a4b50 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 68 61  e guaranteed tha
a4b60 74 20 6e 42 79 74 65 3e 30 20 62 65 63 61 75 73  t nByte>0 becaus
a4b70 65 0a 2a 2a 20 63 61 73 65 73 20 6f 66 20 6e 42  e.** cases of nB
a4b80 79 74 65 3c 3d 30 20 77 69 6c 6c 20 62 65 20 69  yte<=0 will be i
a4b90 6e 74 65 72 63 65 70 74 65 64 20 61 6e 64 20 64  ntercepted and d
a4ba0 65 61 6c 74 20 77 69 74 68 20 62 79 20 68 69 67  ealt with by hig
a4bb0 68 65 72 20 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75  her level.** rou
a4bc0 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  tines..*/.static
a4bd0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65   void *sqlite3Me
a4be0 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74  mMalloc(int nByt
a4bf0 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  e){.#ifdef SQLIT
a4c00 45 5f 4d 41 4c 4c 4f 43 53 49 5a 45 0a 20 20 76  E_MALLOCSIZE.  v
a4c10 6f 69 64 20 2a 70 20 3d 20 53 51 4c 49 54 45 5f  oid *p = SQLITE_
a4c20 4d 41 4c 4c 4f 43 28 20 6e 42 79 74 65 20 29 3b  MALLOC( nByte );
a4c30 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20  .  if( p==0 ){. 
a4c40 20 20 20 74 65 73 74 63 61 73 65 28 20 73 71 6c     testcase( sql
a4c50 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
a4c60 2e 78 4c 6f 67 21 3d 30 20 29 3b 0a 20 20 20 20  .xLog!=0 );.    
a4c70 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49  sqlite3_log(SQLI
a4c80 54 45 5f 4e 4f 4d 45 4d 2c 20 22 66 61 69 6c 65  TE_NOMEM, "faile
a4c90 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 25 75  d to allocate %u
a4ca0 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
a4cb0 22 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 0a 20  ", nByte);.  }. 
a4cc0 20 72 65 74 75 72 6e 20 70 3b 0a 23 65 6c 73 65   return p;.#else
a4cd0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
a4ce0 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e   *p;.  assert( n
a4cf0 42 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74  Byte>0 );.  nByt
a4d00 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65  e = ROUND8(nByte
a4d10 29 3b 0a 20 20 70 20 3d 20 53 51 4c 49 54 45 5f  );.  p = SQLITE_
a4d20 4d 41 4c 4c 4f 43 28 20 6e 42 79 74 65 2b 38 20  MALLOC( nByte+8 
a4d30 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  );.  if( p ){.  
a4d40 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b 0a    p[0] = nByte;.
a4d50 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 65 6c 73 65      p++;.  }else
a4d60 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  {.    testcase( 
a4d70 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
a4d80 66 69 67 2e 78 4c 6f 67 21 3d 30 20 29 3b 0a 20  fig.xLog!=0 );. 
a4d90 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53     sqlite3_log(S
a4da0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 22 66 61  QLITE_NOMEM, "fa
a4db0 69 6c 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  iled to allocate
a4dc0 20 25 75 20 62 79 74 65 73 20 6f 66 20 6d 65 6d   %u bytes of mem
a4dd0 6f 72 79 22 2c 20 6e 42 79 74 65 29 3b 0a 20 20  ory", nByte);.  
a4de0 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  }.  return (void
a4df0 20 2a 29 70 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a   *)p;.#endif.}..
a4e00 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 66 72 65 65 28  /*.** Like free(
a4e10 29 20 62 75 74 20 77 6f 72 6b 73 20 66 6f 72 20  ) but works for 
a4e20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 62 74 61  allocations obta
a4e30 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
a4e40 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20  3MemMalloc().** 
a4e50 6f 72 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61  or sqlite3MemRea
a4e60 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f  lloc()..**.** Fo
a4e70 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c  r this low-level
a4e80 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 6c 72   routine, we alr
a4e90 65 61 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 70  eady know that p
a4ea0 50 72 69 6f 72 21 3d 30 20 73 69 6e 63 65 0a 2a  Prior!=0 since.*
a4eb0 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 70 50  * cases where pP
a4ec0 72 69 6f 72 3d 3d 30 20 77 69 6c 6c 20 68 61 76  rior==0 will hav
a4ed0 65 20 62 65 65 6e 20 69 6e 74 65 63 65 70 74 65  e been intecepte
a4ee0 64 20 61 6e 64 20 64 65 61 6c 74 20 77 69 74 68  d and dealt with
a4ef0 0a 2a 2a 20 62 79 20 68 69 67 68 65 72 2d 6c 65  .** by higher-le
a4f00 76 65 6c 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f  vel routines..*/
a4f10 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
a4f20 69 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64  ite3MemFree(void
a4f30 20 2a 70 50 72 69 6f 72 29 7b 0a 23 69 66 64 65   *pPrior){.#ifde
a4f40 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53  f SQLITE_MALLOCS
a4f50 49 5a 45 0a 20 20 53 51 4c 49 54 45 5f 46 52 45  IZE.  SQLITE_FRE
a4f60 45 28 70 50 72 69 6f 72 29 3b 0a 23 65 6c 73 65  E(pPrior);.#else
a4f70 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
a4f80 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69   *p = (sqlite3_i
a4f90 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20  nt64*)pPrior;.  
a4fa0 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d  assert( pPrior!=
a4fb0 30 20 29 3b 0a 20 20 70 2d 2d 3b 0a 20 20 53 51  0 );.  p--;.  SQ
a4fc0 4c 49 54 45 5f 46 52 45 45 28 70 29 3b 0a 23 65  LITE_FREE(p);.#e
a4fd0 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ndif.}../*.** Re
a4fe0 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f 63 61 74  port the allocat
a4ff0 65 64 20 73 69 7a 65 20 6f 66 20 61 20 70 72 69  ed size of a pri
a5000 6f 72 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 78  or return from x
a5010 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 78  Malloc().** or x
a5020 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74  Realloc()..*/.st
a5030 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
a5040 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50  MemSize(void *pP
a5050 72 69 6f 72 29 7b 0a 23 69 66 64 65 66 20 53 51  rior){.#ifdef SQ
a5060 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49 5a 45 0a  LITE_MALLOCSIZE.
a5070 20 20 72 65 74 75 72 6e 20 70 50 72 69 6f 72 20    return pPrior 
a5080 3f 20 28 69 6e 74 29 53 51 4c 49 54 45 5f 4d 41  ? (int)SQLITE_MA
a5090 4c 4c 4f 43 53 49 5a 45 28 70 50 72 69 6f 72 29  LLOCSIZE(pPrior)
a50a0 20 3a 20 30 3b 0a 23 65 6c 73 65 0a 20 20 73 71   : 0;.#else.  sq
a50b0 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a  lite3_int64 *p;.
a50c0 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20    if( pPrior==0 
a50d0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20  ) return 0;.  p 
a50e0 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  = (sqlite3_int64
a50f0 2a 29 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b  *)pPrior;.  p--;
a5100 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70  .  return (int)p
a5110 5b 30 5d 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  [0];.#endif.}../
a5120 2a 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c 6f  *.** Like reallo
a5130 63 28 29 2e 20 20 52 65 73 69 7a 65 20 61 6e 20  c().  Resize an 
a5140 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69  allocation previ
a5150 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66  ously obtained f
a5160 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 65  rom.** sqlite3Me
a5170 6d 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a  mMalloc()..**.**
a5180 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65   For this low-le
a5190 76 65 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 77  vel interface, w
a51a0 65 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69  e know that pPri
a51b0 6f 72 21 3d 30 2e 20 20 43 61 73 65 73 20 77 68  or!=0.  Cases wh
a51c0 65 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d 30  ere.** pPrior==0
a51d0 20 77 68 69 6c 65 20 68 61 76 65 20 62 65 65 6e   while have been
a51e0 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 79 20   intercepted by 
a51f0 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75  higher-level rou
a5200 74 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64 69  tine and.** redi
a5210 72 65 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c 6f  rected to xMallo
a5220 63 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 77  c.  Similarly, w
a5230 65 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79 74  e know that nByt
a5240 65 3e 30 20 62 65 63 61 75 73 65 73 0a 2a 2a 20  e>0 becauses.** 
a5250 63 61 73 65 73 20 77 68 65 72 65 20 6e 42 79 74  cases where nByt
a5260 65 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62  e<=0 will have b
a5270 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20  een intercepted 
a5280 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 0a  by higher-level.
a5290 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e 64 20  ** routines and 
a52a0 72 65 64 69 72 65 63 74 65 64 20 74 6f 20 78 46  redirected to xF
a52b0 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ree..*/.static v
a52c0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52  oid *sqlite3MemR
a52d0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72  ealloc(void *pPr
a52e0 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b  ior, int nByte){
a52f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d  .#ifdef SQLITE_M
a5300 41 4c 4c 4f 43 53 49 5a 45 0a 20 20 76 6f 69 64  ALLOCSIZE.  void
a5310 20 2a 70 20 3d 20 53 51 4c 49 54 45 5f 52 45 41   *p = SQLITE_REA
a5320 4c 4c 4f 43 28 70 50 72 69 6f 72 2c 20 6e 42 79  LLOC(pPrior, nBy
a5330 74 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  te);.  if( p==0 
a5340 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  ){.    testcase(
a5350 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
a5360 6e 66 69 67 2e 78 4c 6f 67 21 3d 30 20 29 3b 0a  nfig.xLog!=0 );.
a5370 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28      sqlite3_log(
a5380 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 0a 20 20  SQLITE_NOMEM,.  
a5390 20 20 20 20 22 66 61 69 6c 65 64 20 6d 65 6d 6f      "failed memo
a53a0 72 79 20 72 65 73 69 7a 65 20 25 75 20 74 6f 20  ry resize %u to 
a53b0 25 75 20 62 79 74 65 73 22 2c 0a 20 20 20 20 20  %u bytes",.     
a53c0 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 53 49   SQLITE_MALLOCSI
a53d0 5a 45 28 70 50 72 69 6f 72 29 2c 20 6e 42 79 74  ZE(pPrior), nByt
a53e0 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
a53f0 20 70 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69   p;.#else.  sqli
a5400 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28  te3_int64 *p = (
a5410 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70  sqlite3_int64*)p
a5420 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28  Prior;.  assert(
a5430 20 70 50 72 69 6f 72 21 3d 30 20 26 26 20 6e 42   pPrior!=0 && nB
a5440 79 74 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  yte>0 );.  asser
a5450 74 28 20 6e 42 79 74 65 3d 3d 52 4f 55 4e 44 38  t( nByte==ROUND8
a5460 28 6e 42 79 74 65 29 20 29 3b 20 2f 2a 20 45 56  (nByte) ); /* EV
a5470 3a 20 52 2d 34 36 31 39 39 2d 33 30 32 34 39 20  : R-46199-30249 
a5480 2a 2f 0a 20 20 70 2d 2d 3b 0a 20 20 70 20 3d 20  */.  p--;.  p = 
a5490 53 51 4c 49 54 45 5f 52 45 41 4c 4c 4f 43 28 70  SQLITE_REALLOC(p
a54a0 2c 20 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69  , nByte+8 );.  i
a54b0 66 28 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d  f( p ){.    p[0]
a54c0 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b   = nByte;.    p+
a54d0 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  +;.  }else{.    
a54e0 74 65 73 74 63 61 73 65 28 20 73 71 6c 69 74 65  testcase( sqlite
a54f0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 4c  3GlobalConfig.xL
a5500 6f 67 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c  og!=0 );.    sql
a5510 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54 45 5f  ite3_log(SQLITE_
a5520 4e 4f 4d 45 4d 2c 0a 20 20 20 20 20 20 22 66 61  NOMEM,.      "fa
a5530 69 6c 65 64 20 6d 65 6d 6f 72 79 20 72 65 73 69  iled memory resi
a5540 7a 65 20 25 75 20 74 6f 20 25 75 20 62 79 74 65  ze %u to %u byte
a5550 73 22 2c 0a 20 20 20 20 20 20 73 71 6c 69 74 65  s",.      sqlite
a5560 33 4d 65 6d 53 69 7a 65 28 70 50 72 69 6f 72 29  3MemSize(pPrior)
a5570 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 0a 20 20  , nByte);.  }.  
a5580 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b  return (void*)p;
a5590 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
a55a0 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75   Round up a requ
a55b0 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20  est size to the 
a55c0 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63  next valid alloc
a55d0 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73  ation size..*/.s
a55e0 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
a55f0 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20  3MemRoundup(int 
a5600 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 4f 55  n){.  return ROU
a5610 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ND8(n);.}../*.**
a5620 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73   Initialize this
a5630 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74   module..*/.stat
a5640 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  ic int sqlite3Me
a5650 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55  mInit(void *NotU
a5660 73 65 64 29 7b 0a 23 69 66 20 64 65 66 69 6e 65  sed){.#if define
a5670 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20  d(__APPLE__) && 
a5680 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
a5690 57 49 54 48 4f 55 54 5f 5a 4f 4e 45 4d 41 4c 4c  WITHOUT_ZONEMALL
a56a0 4f 43 29 0a 20 20 69 6e 74 20 63 70 75 43 6f 75  OC).  int cpuCou
a56b0 6e 74 3b 0a 20 20 73 69 7a 65 5f 74 20 6c 65 6e  nt;.  size_t len
a56c0 3b 0a 20 20 69 66 28 20 5f 73 71 6c 69 74 65 5a  ;.  if( _sqliteZ
a56d0 6f 6e 65 5f 20 29 7b 0a 20 20 20 20 72 65 74 75  one_ ){.    retu
a56e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
a56f0 7d 0a 20 20 6c 65 6e 20 3d 20 73 69 7a 65 6f 66  }.  len = sizeof
a5700 28 63 70 75 43 6f 75 6e 74 29 3b 0a 20 20 2f 2a  (cpuCount);.  /*
a5710 20 4f 6e 65 20 75 73 75 61 6c 6c 79 20 77 61 6e   One usually wan
a5720 74 73 20 74 6f 20 75 73 65 20 68 77 2e 61 63 63  ts to use hw.acc
a5730 74 69 76 65 63 70 75 20 66 6f 72 20 4d 54 20 64  tivecpu for MT d
a5740 65 63 69 73 69 6f 6e 73 2c 20 62 75 74 20 6e 6f  ecisions, but no
a5750 74 20 68 65 72 65 20 2a 2f 0a 20 20 73 79 73 63  t here */.  sysc
a5760 74 6c 62 79 6e 61 6d 65 28 22 68 77 2e 6e 63 70  tlbyname("hw.ncp
a5770 75 22 2c 20 26 63 70 75 43 6f 75 6e 74 2c 20 26  u", &cpuCount, &
a5780 6c 65 6e 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20  len, NULL, 0);. 
a5790 20 69 66 28 20 63 70 75 43 6f 75 6e 74 3e 31 20   if( cpuCount>1 
a57a0 29 7b 0a 20 20 20 20 2f 2a 20 64 65 66 65 72 20  ){.    /* defer 
a57b0 4d 54 20 64 65 63 69 73 69 6f 6e 73 20 74 6f 20  MT decisions to 
a57c0 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 20 2a 2f  system malloc */
a57d0 0a 20 20 20 20 5f 73 71 6c 69 74 65 5a 6f 6e 65  .    _sqliteZone
a57e0 5f 20 3d 20 6d 61 6c 6c 6f 63 5f 64 65 66 61 75  _ = malloc_defau
a57f0 6c 74 5f 7a 6f 6e 65 28 29 3b 0a 20 20 7d 65 6c  lt_zone();.  }el
a5800 73 65 7b 0a 20 20 20 20 2f 2a 20 6f 6e 6c 79 20  se{.    /* only 
a5810 31 20 63 6f 72 65 2c 20 75 73 65 20 6f 75 72 20  1 core, use our 
a5820 6f 77 6e 20 7a 6f 6e 65 20 74 6f 20 63 6f 6e 74  own zone to cont
a5830 65 6e 74 69 6f 6e 20 6f 76 65 72 20 67 6c 6f 62  ention over glob
a5840 61 6c 20 6c 6f 63 6b 73 2c 20 0a 20 20 20 20 2a  al locks, .    *
a5850 2a 20 65 2e 67 2e 20 77 65 20 68 61 76 65 20 6f  * e.g. we have o
a5860 75 72 20 6f 77 6e 20 64 65 64 69 63 61 74 65 64  ur own dedicated
a5870 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 62 6f   locks */.    bo
a5880 6f 6c 20 73 75 63 63 65 73 73 3b 0a 20 20 20 20  ol success;.    
a5890 6d 61 6c 6c 6f 63 5f 7a 6f 6e 65 5f 74 2a 20 6e  malloc_zone_t* n
a58a0 65 77 7a 6f 6e 65 20 3d 20 6d 61 6c 6c 6f 63 5f  ewzone = malloc_
a58b0 63 72 65 61 74 65 5f 7a 6f 6e 65 28 34 30 39 36  create_zone(4096
a58c0 2c 20 30 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f 63  , 0);.    malloc
a58d0 5f 73 65 74 5f 7a 6f 6e 65 5f 6e 61 6d 65 28 6e  _set_zone_name(n
a58e0 65 77 7a 6f 6e 65 2c 20 22 53 71 6c 69 74 65 5f  ewzone, "Sqlite_
a58f0 48 65 61 70 22 29 3b 0a 20 20 20 20 64 6f 7b 0a  Heap");.    do{.
a5900 20 20 20 20 20 20 73 75 63 63 65 73 73 20 3d 20        success = 
a5910 4f 53 41 74 6f 6d 69 63 43 6f 6d 70 61 72 65 41  OSAtomicCompareA
a5920 6e 64 53 77 61 70 50 74 72 42 61 72 72 69 65 72  ndSwapPtrBarrier
a5930 28 4e 55 4c 4c 2c 20 6e 65 77 7a 6f 6e 65 2c 20  (NULL, newzone, 
a5940 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
a5950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5960 20 20 28 76 6f 69 64 20 2a 20 76 6f 6c 61 74 69    (void * volati
a5970 6c 65 20 2a 29 26 5f 73 71 6c 69 74 65 5a 6f 6e  le *)&_sqliteZon
a5980 65 5f 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28  e_);.    }while(
a5990 21 5f 73 71 6c 69 74 65 5a 6f 6e 65 5f 29 3b 0a  !_sqliteZone_);.
a59a0 20 20 20 20 69 66 28 20 21 73 75 63 63 65 73 73      if( !success
a59b0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 73 6f 6d   ){.      /* som
a59c0 65 62 6f 64 79 20 72 65 67 69 73 74 65 72 65 64  ebody registered
a59d0 20 61 20 7a 6f 6e 65 20 66 69 72 73 74 20 2a 2f   a zone first */
a59e0 0a 20 20 20 20 20 20 6d 61 6c 6c 6f 63 5f 64 65  .      malloc_de
a59f0 73 74 72 6f 79 5f 7a 6f 6e 65 28 6e 65 77 7a 6f  stroy_zone(newzo
a5a00 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  ne);.    }.  }.#
a5a10 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50  endif.  UNUSED_P
a5a20 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
a5a30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
a5a40 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
a5a50 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69  Deinitialize thi
a5a60 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61  s module..*/.sta
a5a70 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
a5a80 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64  MemShutdown(void
a5a90 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e   *NotUsed){.  UN
a5aa0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
a5ab0 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  otUsed);.  retur
a5ac0 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  n;.}../*.** This
a5ad0 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20   routine is the 
a5ae0 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20  only routine in 
a5af0 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65  this file with e
a5b00 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e  xternal linkage.
a5b10 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20  .**.** Populate 
a5b20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  the low-level me
a5b30 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
a5b40 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
a5b50 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47  s in.** sqlite3G
a5b60 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69  lobalConfig.m wi
a5b70 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74  th pointers to t
a5b80 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74  he routines in t
a5b90 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c  his file..*/.SQL
a5ba0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a5bb0 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65   sqlite3MemSetDe
a5bc0 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73  fault(void){.  s
a5bd0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
a5be0 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20  te3_mem_methods 
a5bf0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d  defaultMethods =
a5c00 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d   {.     sqlite3M
a5c10 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73  emMalloc,.     s
a5c20 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20  qlite3MemFree,. 
a5c30 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65      sqlite3MemRe
a5c40 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69  alloc,.     sqli
a5c50 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20  te3MemSize,.    
a5c60 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64   sqlite3MemRound
a5c70 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  up,.     sqlite3
a5c80 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71  MemInit,.     sq
a5c90 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e  lite3MemShutdown
a5ca0 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20  ,.     0.  };.  
a5cb0 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53  sqlite3_config(S
a5cc0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c  QLITE_CONFIG_MAL
a5cd0 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74  LOC, &defaultMet
a5ce0 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66  hods);.}..#endif
a5cf0 20 2f 2a 20 53 51 4c 49 54 45 5f 53 59 53 54 45   /* SQLITE_SYSTE
a5d00 4d 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a  M_MALLOC */../**
a5d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
a5d20 20 6f 66 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a   of mem1.c *****
a5d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
a5d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
a5d70 69 6e 20 66 69 6c 65 20 6d 65 6d 32 2e 63 20 2a  in file mem2.c *
a5d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
a5db0 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31  ** 2007 August 1
a5dc0 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
a5dd0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
a5de0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
a5df0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
a5e00 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
a5e10 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
a5e20 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
a5e30 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
a5e40 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
a5e50 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
a5e60 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
a5e70 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
a5e80 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
a5e90 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
a5ea0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
a5eb0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
a5ec0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
a5ed0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
a5ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5f20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
a5f30 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 2d 6c 65   contains low-le
a5f40 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  vel memory alloc
a5f50 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66 6f  ation drivers fo
a5f60 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65  r when.** SQLite
a5f70 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20 73 74   will use the st
a5f80 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 61 72 79  andard C-library
a5f90 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f   malloc/realloc/
a5fa0 66 72 65 65 20 69 6e 74 65 72 66 61 63 65 0a 2a  free interface.*
a5fb0 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20  * to obtain the 
a5fc0 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 20  memory it needs 
a5fd0 77 68 69 6c 65 20 61 64 64 69 6e 67 20 6c 6f 74  while adding lot
a5fe0 73 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20  s of additional 
a5ff0 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 69 6e 66  debugging.** inf
a6000 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 65 61 63 68  ormation to each
a6010 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 6f   allocation in o
a6020 72 64 65 72 20 74 6f 20 68 65 6c 70 20 64 65 74  rder to help det
a6030 65 63 74 20 61 6e 64 20 66 69 78 20 6d 65 6d 6f  ect and fix memo
a6040 72 79 0a 2a 2a 20 6c 65 61 6b 73 20 61 6e 64 20  ry.** leaks and 
a6050 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 65 72 72  memory usage err
a6060 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ors..**.** This 
a6070 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d  file contains im
a6080 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
a6090 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d   the low-level m
a60a0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
a60b0 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 73 70 65  .** routines spe
a60c0 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 73 71  cified in the sq
a60d0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
a60e0 73 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 0a 2f 2a  s object..*/../*
a60f0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
a6100 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
a6110 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64  llocator is used
a6120 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20   only if the.** 
a6130 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 20  SQLITE_MEMDEBUG 
a6140 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64  macro is defined
a6150 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
a6160 45 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f 2a 0a 2a  E_MEMDEBUG../*.*
a6170 2a 20 54 68 65 20 62 61 63 6b 74 72 61 63 65 20  * The backtrace 
a6180 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 73  functionality is
a6190 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
a61a0 77 69 74 68 20 47 4c 49 42 43 0a 2a 2f 0a 23 69  with GLIBC.*/.#i
a61b0 66 64 65 66 20 5f 5f 47 4c 49 42 43 5f 5f 0a 20  fdef __GLIBC__. 
a61c0 20 65 78 74 65 72 6e 20 69 6e 74 20 62 61 63 6b   extern int back
a61d0 74 72 61 63 65 28 76 6f 69 64 2a 2a 2c 69 6e 74  trace(void**,int
a61e0 29 3b 0a 20 20 65 78 74 65 72 6e 20 76 6f 69 64  );.  extern void
a61f0 20 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f   backtrace_symbo
a6200 6c 73 5f 66 64 28 76 6f 69 64 2a 63 6f 6e 73 74  ls_fd(void*const
a6210 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23 65 6c 73  *,int,int);.#els
a6220 65 0a 23 20 64 65 66 69 6e 65 20 62 61 63 6b 74  e.# define backt
a6230 72 61 63 65 28 41 2c 42 29 20 31 0a 23 20 64 65  race(A,B) 1.# de
a6240 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 5f 73  fine backtrace_s
a6250 79 6d 62 6f 6c 73 5f 66 64 28 41 2c 42 2c 43 29  ymbols_fd(A,B,C)
a6260 0a 23 65 6e 64 69 66 0a 2f 2a 20 23 69 6e 63 6c  .#endif./* #incl
a6270 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e 20 2a 2f  ude <stdio.h> */
a6280 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d 65 6d  ../*.** Each mem
a6290 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c  ory allocation l
a62a0 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a  ooks like this:.
a62b0 2a 2a 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  **.**  ---------
a62c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a62d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a62e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a62f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
a6300 2a 2a 20 20 7c 20 54 69 74 6c 65 20 7c 20 20 62  **  | Title |  b
a6310 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72  acktrace pointer
a6320 73 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 64 72  s |  MemBlockHdr
a6330 20 7c 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 7c   |  allocation |
a6340 20 20 45 6e 64 47 75 61 72 64 20 7c 0a 2a 2a 20    EndGuard |.** 
a6350 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
a6360 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a6370 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a6380 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a6390 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20  ---------.**.** 
a63a0 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  The application 
a63b0 63 6f 64 65 20 73 65 65 73 20 6f 6e 6c 79 20 61  code sees only a
a63c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
a63d0 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 57 65 20  allocation.  We 
a63e0 68 61 76 65 0a 2a 2a 20 74 6f 20 62 61 63 6b 20  have.** to back 
a63f0 75 70 20 66 72 6f 6d 20 74 68 65 20 61 6c 6c 6f  up from the allo
a6400 63 61 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 74  cation pointer t
a6410 6f 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c  o find the MemBl
a6420 6f 63 6b 48 64 72 2e 20 20 54 68 65 0a 2a 2a 20  ockHdr.  The.** 
a6430 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 74 65 6c 6c  MemBlockHdr tell
a6440 73 20 75 73 20 74 68 65 20 73 69 7a 65 20 6f 66  s us the size of
a6450 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
a6460 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  and the number o
a6470 66 0a 2a 2a 20 62 61 63 6b 74 72 61 63 65 20 70  f.** backtrace p
a6480 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 72 65 20  ointers.  There 
a6490 69 73 20 61 6c 73 6f 20 61 20 67 75 61 72 64 20  is also a guard 
a64a0 77 6f 72 64 20 61 74 20 74 68 65 20 65 6e 64 20  word at the end 
a64b0 6f 66 20 74 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f  of the.** MemBlo
a64c0 63 6b 48 64 72 2e 0a 2a 2f 0a 73 74 72 75 63 74  ckHdr..*/.struct
a64d0 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7b 0a 20   MemBlockHdr {. 
a64e0 20 69 36 34 20 69 53 69 7a 65 3b 20 20 20 20 20   i64 iSize;     
a64f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6500 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
a6510 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  this allocation 
a6520 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42  */.  struct MemB
a6530 6c 6f 63 6b 48 64 72 20 2a 70 4e 65 78 74 2c 20  lockHdr *pNext, 
a6540 2a 70 50 72 65 76 3b 20 20 2f 2a 20 4c 69 6e 6b  *pPrev;  /* Link
a6550 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 75  ed list of all u
a6560 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 2a 2f  nfreed memory */
a6570 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61  .  char nBacktra
a6580 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ce;             
a6590 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
a65a0 20 6f 66 20 62 61 63 6b 74 72 61 63 65 73 20 6f   of backtraces o
a65b0 6e 20 74 68 69 73 20 61 6c 6c 6f 63 20 2a 2f 0a  n this alloc */.
a65c0 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63    char nBacktrac
a65d0 65 53 6c 6f 74 73 3b 20 20 20 20 20 20 20 20 20  eSlots;         
a65e0 20 20 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62        /* Availab
a65f0 6c 65 20 62 61 63 6b 74 72 61 63 65 20 73 6c 6f  le backtrace slo
a6600 74 73 20 2a 2f 0a 20 20 75 38 20 6e 54 69 74 6c  ts */.  u8 nTitl
a6610 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
a6620 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
a6630 79 74 65 73 20 6f 66 20 74 69 74 6c 65 3b 20 69  ytes of title; i
a6640 6e 63 6c 75 64 65 73 20 27 5c 30 27 20 2a 2f 0a  ncludes '\0' */.
a6650 20 20 75 38 20 65 54 79 70 65 3b 20 20 20 20 20    u8 eType;     
a6660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6670 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74        /* Allocat
a6680 69 6f 6e 20 74 79 70 65 20 63 6f 64 65 20 2a 2f  ion type code */
a6690 0a 20 20 69 6e 74 20 69 46 6f 72 65 47 75 61 72  .  int iForeGuar
a66a0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
a66b0 20 20 20 20 20 20 20 2f 2a 20 47 75 61 72 64 20         /* Guard 
a66c0 77 6f 72 64 20 66 6f 72 20 73 61 6e 69 74 79 20  word for sanity 
a66d0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75 61  */.};../*.** Gua
a66e0 72 64 20 77 6f 72 64 73 0a 2a 2f 0a 23 64 65 66  rd words.*/.#def
a66f0 69 6e 65 20 46 4f 52 45 47 55 41 52 44 20 30 78  ine FOREGUARD 0x
a6700 38 30 46 35 45 31 35 33 0a 23 64 65 66 69 6e 65  80F5E153.#define
a6710 20 52 45 41 52 47 55 41 52 44 20 30 78 45 34 36   REARGUARD 0xE46
a6720 37 36 42 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d  76B53../*.** Num
a6730 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 73 69  ber of malloc si
a6740 7a 65 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 6f  ze increments to
a6750 20 74 72 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66 69   track..*/.#defi
a6760 6e 65 20 4e 43 53 49 5a 45 20 20 31 30 30 30 0a  ne NCSIZE  1000.
a6770 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68  ./*.** All of th
a6780 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c  e static variabl
a6790 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20  es used by this 
a67a0 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65  module are colle
a67b0 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73  cted.** into a s
a67c0 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20  ingle structure 
a67d0 6e 61 6d 65 64 20 22 6d 65 6d 22 2e 20 20 54 68  named "mem".  Th
a67e0 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68  is is to keep th
a67f0 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69  e.** static vari
a6800 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20  ables organized 
a6810 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61  and to reduce na
a6820 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f  mespace pollutio
a6830 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d  n.** when this m
a6840 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65  odule is combine
a6850 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20  d with other in 
a6860 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
a6870 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  ..*/.static stru
a6880 63 74 20 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a  ct {.  .  /*.  *
a6890 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72  * Mutex to contr
a68a0 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  ol access to the
a68b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
a68c0 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20  on subsystem..  
a68d0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
a68e0 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a  ex *mutex;..  /*
a68f0 0a 20 20 2a 2a 20 48 65 61 64 20 61 6e 64 20 74  .  ** Head and t
a6900 61 69 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20  ail of a linked 
a6910 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73  list of all outs
a6920 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69  tanding allocati
a6930 6f 6e 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75 63  ons.  */.  struc
a6940 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70  t MemBlockHdr *p
a6950 46 69 72 73 74 3b 0a 20 20 73 74 72 75 63 74 20  First;.  struct 
a6960 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61  MemBlockHdr *pLa
a6970 73 74 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a  st;.  .  /*.  **
a6980 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c   The number of l
a6990 65 76 65 6c 73 20 6f 66 20 62 61 63 6b 74 72 61  evels of backtra
a69a0 63 65 20 74 6f 20 73 61 76 65 20 69 6e 20 6e 65  ce to save in ne
a69b0 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20  w allocations.. 
a69c0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 74   */.  int nBackt
a69d0 72 61 63 65 3b 0a 20 20 76 6f 69 64 20 28 2a 78  race;.  void (*x
a69e0 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20  Backtrace)(int, 
a69f0 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a 0a  int, void **);..
a6a00 20 20 2f 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 20    /*.  ** Title 
a6a10 74 65 78 74 20 74 6f 20 69 6e 73 65 72 74 20 69  text to insert i
a6a20 6e 20 66 72 6f 6e 74 20 6f 66 20 65 61 63 68 20  n front of each 
a6a30 62 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e 74  block.  */.  int
a6a40 20 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20   nTitle;        
a6a50 2f 2a 20 42 79 74 65 73 20 6f 66 20 7a 54 69 74  /* Bytes of zTit
a6a60 6c 65 20 74 6f 20 73 61 76 65 2e 20 20 49 6e 63  le to save.  Inc
a6a70 6c 75 64 65 73 20 27 5c 30 27 20 61 6e 64 20 70  ludes '\0' and p
a6a80 61 64 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72  adding */.  char
a6a90 20 7a 54 69 74 6c 65 5b 31 30 30 5d 3b 20 20 2f   zTitle[100];  /
a6aa0 2a 20 54 68 65 20 74 69 74 6c 65 20 74 65 78 74  * The title text
a6ab0 20 2a 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20   */..  /* .  ** 
a6ac0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73  sqlite3MallocDis
a6ad0 61 6c 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e  allow() incremen
a6ae0 74 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ts the following
a6af0 20 63 6f 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 73   counter..  ** s
a6b00 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f  qlite3MallocAllo
a6b10 77 28 29 20 64 65 63 72 65 6d 65 6e 74 73 20 69  w() decrements i
a6b20 74 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 64 69  t..  */.  int di
a6b30 73 61 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e 6f  sallow; /* Do no
a6b40 74 20 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 61  t allow memory a
a6b50 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20  llocation */..  
a6b60 2f 2a 0a 20 20 2a 2a 20 47 61 74 68 65 72 20 73  /*.  ** Gather s
a6b70 74 61 74 69 73 74 69 63 73 20 6f 6e 20 74 68 65  tatistics on the
a6b80 20 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79   sizes of memory
a6b90 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20   allocations..  
a6ba0 2a 2a 20 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73 20  ** nAlloc[i] is 
a6bb0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c  the number of al
a6bc0 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74  location attempt
a6bd0 73 20 6f 66 20 69 2a 38 0a 20 20 2a 2a 20 62 79  s of i*8.  ** by
a6be0 74 65 73 2e 20 20 69 3d 3d 4e 43 53 49 5a 45 20  tes.  i==NCSIZE 
a6bf0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
a6c00 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65   allocation atte
a6c10 6d 70 74 73 20 66 6f 72 0a 20 20 2a 2a 20 73 69  mpts for.  ** si
a6c20 7a 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 4e 43  zes more than NC
a6c30 53 49 5a 45 2a 38 20 62 79 74 65 73 2e 0a 20 20  SIZE*8 bytes..  
a6c40 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 5b  */.  int nAlloc[
a6c50 4e 43 53 49 5a 45 5d 3b 20 20 20 20 20 20 2f 2a  NCSIZE];      /*
a6c60 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
a6c70 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a   allocations */.
a6c80 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 5b 4e    int nCurrent[N
a6c90 43 53 49 5a 45 5d 3b 20 20 20 20 2f 2a 20 43 75  CSIZE];    /* Cu
a6ca0 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  rrent number of 
a6cb0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20  allocations */. 
a6cc0 20 69 6e 74 20 6d 78 43 75 72 72 65 6e 74 5b 4e   int mxCurrent[N
a6cd0 43 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 48 69 67  CSIZE];   /* Hig
a6ce0 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20  hwater mark for 
a6cf0 6e 43 75 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20 6d  nCurrent */..} m
a6d00 65 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75  em;.../*.** Adju
a6d10 73 74 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20  st memory usage 
a6d20 73 74 61 74 69 73 74 69 63 73 0a 2a 2f 0a 73 74  statistics.*/.st
a6d30 61 74 69 63 20 76 6f 69 64 20 61 64 6a 75 73 74  atic void adjust
a6d40 53 74 61 74 73 28 69 6e 74 20 69 53 69 7a 65 2c  Stats(int iSize,
a6d50 20 69 6e 74 20 69 6e 63 72 65 6d 65 6e 74 29 7b   int increment){
a6d60 0a 20 20 69 6e 74 20 69 20 3d 20 52 4f 55 4e 44  .  int i = ROUND
a6d70 38 28 69 53 69 7a 65 29 2f 38 3b 0a 20 20 69 66  8(iSize)/8;.  if
a6d80 28 20 69 3e 4e 43 53 49 5a 45 2d 31 20 29 7b 0a  ( i>NCSIZE-1 ){.
a6d90 20 20 20 20 69 20 3d 20 4e 43 53 49 5a 45 20 2d      i = NCSIZE -
a6da0 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e   1;.  }.  if( in
a6db0 63 72 65 6d 65 6e 74 3e 30 20 29 7b 0a 20 20 20  crement>0 ){.   
a6dc0 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b   mem.nAlloc[i]++
a6dd0 3b 0a 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65  ;.    mem.nCurre
a6de0 6e 74 5b 69 5d 2b 2b 3b 0a 20 20 20 20 69 66 28  nt[i]++;.    if(
a6df0 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d   mem.nCurrent[i]
a6e00 3e 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69  >mem.mxCurrent[i
a6e10 5d 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 6d  ] ){.      mem.m
a6e20 78 43 75 72 72 65 6e 74 5b 69 5d 20 3d 20 6d 65  xCurrent[i] = me
a6e30 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3b 0a 20  m.nCurrent[i];. 
a6e40 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
a6e50 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69    mem.nCurrent[i
a6e60 5d 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ]--;.    assert(
a6e70 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d   mem.nCurrent[i]
a6e80 3e 3d 30 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  >=0 );.  }.}../*
a6e90 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 61 6c 6c  .** Given an all
a6ea0 6f 63 61 74 69 6f 6e 2c 20 66 69 6e 64 20 74 68  ocation, find th
a6eb0 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 66 6f  e MemBlockHdr fo
a6ec0 72 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 69 6f  r that allocatio
a6ed0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  n..**.** This ro
a6ee0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 65  utine checks the
a6ef0 20 67 75 61 72 64 73 20 61 74 20 65 69 74 68 65   guards at eithe
a6f00 72 20 65 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c  r end of the all
a6f10 6f 63 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 69  ocation and.** i
a6f20 66 20 74 68 65 79 20 61 72 65 20 69 6e 63 6f 72  f they are incor
a6f30 72 65 63 74 20 69 74 20 61 73 73 65 72 74 73 2e  rect it asserts.
a6f40 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63  .*/.static struc
a6f50 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 73  t MemBlockHdr *s
a6f60 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48  qlite3MemsysGetH
a6f70 65 61 64 65 72 28 76 6f 69 64 20 2a 70 41 6c 6c  eader(void *pAll
a6f80 6f 63 61 74 69 6f 6e 29 7b 0a 20 20 73 74 72 75  ocation){.  stru
a6f90 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
a6fa0 70 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a  p;.  int *pInt;.
a6fb0 20 20 75 38 20 2a 70 55 38 3b 0a 20 20 69 6e 74    u8 *pU8;.  int
a6fc0 20 6e 52 65 73 65 72 76 65 3b 0a 0a 20 20 70 20   nReserve;..  p 
a6fd0 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  = (struct MemBlo
a6fe0 63 6b 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74 69  ckHdr*)pAllocati
a6ff0 6f 6e 3b 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 73  on;.  p--;.  ass
a7000 65 72 74 28 20 70 2d 3e 69 46 6f 72 65 47 75 61  ert( p->iForeGua
a7010 72 64 3d 3d 28 69 6e 74 29 46 4f 52 45 47 55 41  rd==(int)FOREGUA
a7020 52 44 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 65  RD );.  nReserve
a7030 20 3d 20 52 4f 55 4e 44 38 28 70 2d 3e 69 53 69   = ROUND8(p->iSi
a7040 7a 65 29 3b 0a 20 20 70 49 6e 74 20 3d 20 28 69  ze);.  pInt = (i
a7050 6e 74 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b  nt*)pAllocation;
a7060 0a 20 20 70 55 38 20 3d 20 28 75 38 2a 29 70 41  .  pU8 = (u8*)pA
a7070 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 73 73  llocation;.  ass
a7080 65 72 74 28 20 70 49 6e 74 5b 6e 52 65 73 65 72  ert( pInt[nReser
a7090 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 3d  ve/sizeof(int)]=
a70a0 3d 28 69 6e 74 29 52 45 41 52 47 55 41 52 44 20  =(int)REARGUARD 
a70b0 29 3b 0a 20 20 2f 2a 20 54 68 69 73 20 63 68 65  );.  /* This che
a70c0 63 6b 73 20 61 6e 79 20 6f 66 20 74 68 65 20 22  cks any of the "
a70d0 65 78 74 72 61 22 20 62 79 74 65 73 20 61 6c 6c  extra" bytes all
a70e0 6f 63 61 74 65 64 20 64 75 65 0a 20 20 2a 2a 20  ocated due.  ** 
a70f0 74 6f 20 72 6f 75 6e 64 69 6e 67 20 75 70 20 74  to rounding up t
a7100 6f 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e  o an 8 byte boun
a7110 64 61 72 79 20 74 6f 20 65 6e 73 75 72 65 20 0a  dary to ensure .
a7120 20 20 2a 2a 20 74 68 65 79 20 68 61 76 65 6e 27    ** they haven'
a7130 74 20 62 65 65 6e 20 6f 76 65 72 77 72 69 74 74  t been overwritt
a7140 65 6e 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65  en..  */.  while
a7150 28 20 6e 52 65 73 65 72 76 65 2d 2d 20 3e 20 70  ( nReserve-- > p
a7160 2d 3e 69 53 69 7a 65 20 29 20 61 73 73 65 72 74  ->iSize ) assert
a7170 28 20 70 55 38 5b 6e 52 65 73 65 72 76 65 5d 3d  ( pU8[nReserve]=
a7180 3d 30 78 36 35 20 29 3b 0a 20 20 72 65 74 75 72  =0x65 );.  retur
a7190 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n p;.}../*.** Re
a71a0 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
a71b0 6f 66 20 62 79 74 65 73 20 63 75 72 72 65 6e 74  of bytes current
a71c0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20  ly allocated at 
a71d0 61 64 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74  address p..*/.st
a71e0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
a71f0 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29  MemSize(void *p)
a7200 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  {.  struct MemBl
a7210 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20  ockHdr *pHdr;.  
a7220 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65  if( !p ){.    re
a7230 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48  turn 0;.  }.  pH
a7240 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73  dr = sqlite3Mems
a7250 79 73 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a  ysGetHeader(p);.
a7260 20 20 72 65 74 75 72 6e 20 70 48 64 72 2d 3e 69    return pHdr->i
a7270 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  Size;.}../*.** I
a7280 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
a7290 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
a72a0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74  subsystem..*/.st
a72b0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
a72c0 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f  MemInit(void *No
a72d0 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44  tUsed){.  UNUSED
a72e0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
a72f0 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ed);.  assert( (
a7300 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 4d 65  sizeof(struct Me
a7310 6d 42 6c 6f 63 6b 48 64 72 29 26 37 29 20 3d 3d  mBlockHdr)&7) ==
a7320 20 30 20 29 3b 0a 20 20 69 66 28 20 21 73 71 6c   0 );.  if( !sql
a7330 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
a7340 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20  .bMemstat ){.   
a7350 20 2f 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73 74   /* If memory st
a7360 61 74 75 73 20 69 73 20 65 6e 61 62 6c 65 64 2c  atus is enabled,
a7370 20 74 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63   then the malloc
a7380 2e 63 20 77 72 61 70 70 65 72 20 77 69 6c 6c 20  .c wrapper will 
a7390 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 68  already.    ** h
a73a0 6f 6c 64 20 74 68 65 20 53 54 41 54 49 43 5f 4d  old the STATIC_M
a73b0 45 4d 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68  EM mutex when th
a73c0 65 20 72 6f 75 74 69 6e 65 73 20 68 65 72 65 20  e routines here 
a73d0 61 72 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a  are invoked. */.
a73e0 20 20 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20      mem.mutex = 
a73f0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
a7400 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
a7410 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a  TATIC_MEM);.  }.
a7420 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
a7430 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69  OK;.}../*.** Dei
a7440 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
a7450 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
a7460 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74  subsystem..*/.st
a7470 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
a7480 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69  3MemShutdown(voi
a7490 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  d *NotUsed){.  U
a74a0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
a74b0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 2e  NotUsed);.  mem.
a74c0 6d 75 74 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  mutex = 0;.}../*
a74d0 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72  .** Round up a r
a74e0 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74  equest size to t
a74f0 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c  he next valid al
a7500 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a  location size..*
a7510 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
a7520 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69  ite3MemRoundup(i
a7530 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt n){.  return 
a7540 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a  ROUND8(n);.}../*
a7550 0a 2a 2a 20 46 69 6c 6c 20 61 20 62 75 66 66 65  .** Fill a buffe
a7560 72 20 77 69 74 68 20 70 73 65 75 64 6f 2d 72 61  r with pseudo-ra
a7570 6e 64 6f 6d 20 62 79 74 65 73 2e 20 20 54 68 69  ndom bytes.  Thi
a7580 73 20 69 73 20 75 73 65 64 20 74 6f 20 70 72 65  s is used to pre
a7590 73 65 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65  set.** the conte
a75a0 6e 74 20 6f 66 20 61 20 6e 65 77 20 6d 65 6d 6f  nt of a new memo
a75b0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f  ry allocation to
a75c0 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 76   unpredictable v
a75d0 61 6c 75 65 73 20 61 6e 64 0a 2a 2a 20 74 6f 20  alues and.** to 
a75e0 63 6c 65 61 72 20 74 68 65 20 63 6f 6e 74 65 6e  clear the conten
a75f0 74 20 6f 66 20 61 20 66 72 65 65 64 20 61 6c 6c  t of a freed all
a7600 6f 63 61 74 69 6f 6e 20 74 6f 20 75 6e 70 72 65  ocation to unpre
a7610 64 69 63 74 61 62 6c 65 20 76 61 6c 75 65 73 2e  dictable values.
a7620 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
a7630 72 61 6e 64 6f 6d 46 69 6c 6c 28 63 68 61 72 20  randomFill(char 
a7640 2a 70 42 75 66 2c 20 69 6e 74 20 6e 42 79 74 65  *pBuf, int nByte
a7650 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  ){.  unsigned in
a7660 74 20 78 2c 20 79 2c 20 72 3b 0a 20 20 78 20 3d  t x, y, r;.  x =
a7670 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49   SQLITE_PTR_TO_I
a7680 4e 54 28 70 42 75 66 29 3b 0a 20 20 79 20 3d 20  NT(pBuf);.  y = 
a7690 6e 42 79 74 65 20 7c 20 31 3b 0a 20 20 77 68 69  nByte | 1;.  whi
a76a0 6c 65 28 20 6e 42 79 74 65 20 3e 3d 20 34 20 29  le( nByte >= 4 )
a76b0 7b 0a 20 20 20 20 78 20 3d 20 28 78 3e 3e 31 29  {.    x = (x>>1)
a76c0 20 5e 20 28 2d 28 78 26 31 29 20 26 20 30 78 64   ^ (-(x&1) & 0xd
a76d0 30 30 30 30 30 30 31 29 3b 0a 20 20 20 20 79 20  0000001);.    y 
a76e0 3d 20 79 2a 31 31 30 33 35 31 35 32 34 35 20 2b  = y*1103515245 +
a76f0 20 31 32 33 34 35 3b 0a 20 20 20 20 72 20 3d 20   12345;.    r = 
a7700 78 20 5e 20 79 3b 0a 20 20 20 20 2a 28 69 6e 74  x ^ y;.    *(int
a7710 2a 29 70 42 75 66 20 3d 20 72 3b 0a 20 20 20 20  *)pBuf = r;.    
a7720 70 42 75 66 20 2b 3d 20 34 3b 0a 20 20 20 20 6e  pBuf += 4;.    n
a7730 42 79 74 65 20 2d 3d 20 34 3b 0a 20 20 7d 0a 20  Byte -= 4;.  }. 
a7740 20 77 68 69 6c 65 28 20 6e 42 79 74 65 2d 2d 20   while( nByte-- 
a7750 3e 20 30 20 29 7b 0a 20 20 20 20 78 20 3d 20 28  > 0 ){.    x = (
a7760 78 3e 3e 31 29 20 5e 20 28 2d 28 78 26 31 29 20  x>>1) ^ (-(x&1) 
a7770 26 20 30 78 64 30 30 30 30 30 30 31 29 3b 0a 20  & 0xd0000001);. 
a7780 20 20 20 79 20 3d 20 79 2a 31 31 30 33 35 31 35     y = y*1103515
a7790 32 34 35 20 2b 20 31 32 33 34 35 3b 0a 20 20 20  245 + 12345;.   
a77a0 20 72 20 3d 20 78 20 5e 20 79 3b 0a 20 20 20 20   r = x ^ y;.    
a77b0 2a 28 70 42 75 66 2b 2b 29 20 3d 20 72 20 26 20  *(pBuf++) = r & 
a77c0 30 78 66 66 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  0xff;.  }.}../*.
a77d0 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74  ** Allocate nByt
a77e0 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  e bytes of memor
a77f0 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  y..*/.static voi
a7800 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c  d *sqlite3MemMal
a7810 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a  loc(int nByte){.
a7820 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63    struct MemBloc
a7830 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f  kHdr *pHdr;.  vo
a7840 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61 72  id **pBt;.  char
a7850 20 2a 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74   *z;.  int *pInt
a7860 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b  ;.  void *p = 0;
a7870 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65  .  int totalSize
a7880 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65  ;.  int nReserve
a7890 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
a78a0 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65  x_enter(mem.mute
a78b0 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65  x);.  assert( me
a78c0 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 3b  m.disallow==0 );
a78d0 0a 20 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f  .  nReserve = RO
a78e0 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 74  UND8(nByte);.  t
a78f0 6f 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73 65  otalSize = nRese
a7900 72 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48  rve + sizeof(*pH
a7910 64 72 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74  dr) + sizeof(int
a7920 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ) +.            
a7930 20 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63     mem.nBacktrac
a7940 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20  e*sizeof(void*) 
a7950 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20  + mem.nTitle;.  
a7960 70 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74 61 6c  p = malloc(total
a7970 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 20 29  Size);.  if( p )
a7980 7b 0a 20 20 20 20 7a 20 3d 20 70 3b 0a 20 20 20  {.    z = p;.   
a7990 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 26   pBt = (void**)&
a79a0 7a 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a 20  z[mem.nTitle];. 
a79b0 20 20 20 70 48 64 72 20 3d 20 28 73 74 72 75 63     pHdr = (struc
a79c0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 26  t MemBlockHdr*)&
a79d0 70 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 72 61  pBt[mem.nBacktra
a79e0 63 65 5d 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70  ce];.    pHdr->p
a79f0 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 48  Next = 0;.    pH
a7a00 64 72 2d 3e 70 50 72 65 76 20 3d 20 6d 65 6d 2e  dr->pPrev = mem.
a7a10 70 4c 61 73 74 3b 0a 20 20 20 20 69 66 28 20 6d  pLast;.    if( m
a7a20 65 6d 2e 70 4c 61 73 74 20 29 7b 0a 20 20 20 20  em.pLast ){.    
a7a30 20 20 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70 4e 65    mem.pLast->pNe
a7a40 78 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d  xt = pHdr;.    }
a7a50 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 2e  else{.      mem.
a7a60 70 46 69 72 73 74 20 3d 20 70 48 64 72 3b 0a 20  pFirst = pHdr;. 
a7a70 20 20 20 7d 0a 20 20 20 20 6d 65 6d 2e 70 4c 61     }.    mem.pLa
a7a80 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 70  st = pHdr;.    p
a7a90 48 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 20  Hdr->iForeGuard 
a7aa0 3d 20 46 4f 52 45 47 55 41 52 44 3b 0a 20 20 20  = FOREGUARD;.   
a7ab0 20 70 48 64 72 2d 3e 65 54 79 70 65 20 3d 20 4d   pHdr->eType = M
a7ac0 45 4d 54 59 50 45 5f 48 45 41 50 3b 0a 20 20 20  EMTYPE_HEAP;.   
a7ad0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
a7ae0 65 53 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e 42 61  eSlots = mem.nBa
a7af0 63 6b 74 72 61 63 65 3b 0a 20 20 20 20 70 48 64  cktrace;.    pHd
a7b00 72 2d 3e 6e 54 69 74 6c 65 20 3d 20 6d 65 6d 2e  r->nTitle = mem.
a7b10 6e 54 69 74 6c 65 3b 0a 20 20 20 20 69 66 28 20  nTitle;.    if( 
a7b20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 29  mem.nBacktrace )
a7b30 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 61 41  {.      void *aA
a7b40 64 64 72 5b 34 30 5d 3b 0a 20 20 20 20 20 20 70  ddr[40];.      p
a7b50 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20  Hdr->nBacktrace 
a7b60 3d 20 62 61 63 6b 74 72 61 63 65 28 61 41 64 64  = backtrace(aAdd
a7b70 72 2c 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63  r, mem.nBacktrac
a7b80 65 2b 31 29 2d 31 3b 0a 20 20 20 20 20 20 6d 65  e+1)-1;.      me
a7b90 6d 63 70 79 28 70 42 74 2c 20 26 61 41 64 64 72  mcpy(pBt, &aAddr
a7ba0 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b  [1], pHdr->nBack
a7bb0 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69  trace*sizeof(voi
a7bc0 64 2a 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65  d*));.      asse
a7bd0 72 74 28 70 42 74 5b 30 5d 29 3b 0a 20 20 20 20  rt(pBt[0]);.    
a7be0 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b 74    if( mem.xBackt
a7bf0 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20  race ){.        
a7c00 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 6e  mem.xBacktrace(n
a7c10 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63  Byte, pHdr->nBac
a7c20 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64 72  ktrace-1, &aAddr
a7c30 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  [1]);.      }.  
a7c40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
a7c50 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20  Hdr->nBacktrace 
a7c60 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 0;.    }.    i
a7c70 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 7b  f( mem.nTitle ){
a7c80 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c  .      memcpy(z,
a7c90 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 6d   mem.zTitle, mem
a7ca0 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d 0a  .nTitle);.    }.
a7cb0 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20      pHdr->iSize 
a7cc0 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 6a  = nByte;.    adj
a7cd0 75 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c 20  ustStats(nByte, 
a7ce0 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d 20  +1);.    pInt = 
a7cf0 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b 0a  (int*)&pHdr[1];.
a7d00 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 76      pInt[nReserv
a7d10 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 3d  e/sizeof(int)] =
a7d20 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20 20   REARGUARD;.    
a7d30 72 61 6e 64 6f 6d 46 69 6c 6c 28 28 63 68 61 72  randomFill((char
a7d40 2a 29 70 49 6e 74 2c 20 6e 42 79 74 65 29 3b 0a  *)pInt, nByte);.
a7d50 20 20 20 20 6d 65 6d 73 65 74 28 28 28 63 68 61      memset(((cha
a7d60 72 2a 29 70 49 6e 74 29 2b 6e 42 79 74 65 2c 20  r*)pInt)+nByte, 
a7d70 30 78 36 35 2c 20 6e 52 65 73 65 72 76 65 2d 6e  0x65, nReserve-n
a7d80 42 79 74 65 29 3b 0a 20 20 20 20 70 20 3d 20 28  Byte);.    p = (
a7d90 76 6f 69 64 2a 29 70 49 6e 74 3b 0a 20 20 7d 0a  void*)pInt;.  }.
a7da0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
a7db0 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29  leave(mem.mutex)
a7dc0 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 0a 7d  ;.  return p; .}
a7dd0 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d  ../*.** Free mem
a7de0 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ory..*/.static v
a7df0 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 72  oid sqlite3MemFr
a7e00 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29  ee(void *pPrior)
a7e10 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  {.  struct MemBl
a7e20 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20  ockHdr *pHdr;.  
a7e30 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68  void **pBt;.  ch
a7e40 61 72 20 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28  ar *z;.  assert(
a7e50 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
a7e60 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 7c 7c  nfig.bMemstat ||
a7e70 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
a7e80 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 3d  nfig.bCoreMutex=
a7e90 3d 30 20 0a 20 20 20 20 20 20 20 7c 7c 20 6d 65  =0 .       || me
a7ea0 6d 2e 6d 75 74 65 78 21 3d 30 20 29 3b 0a 20 20  m.mutex!=0 );.  
a7eb0 70 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65  pHdr = sqlite3Me
a7ec0 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50  msysGetHeader(pP
a7ed0 72 69 6f 72 29 3b 0a 20 20 70 42 74 20 3d 20 28  rior);.  pBt = (
a7ee0 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 70  void**)pHdr;.  p
a7ef0 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63  Bt -= pHdr->nBac
a7f00 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 73  ktraceSlots;.  s
a7f10 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
a7f20 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20  er(mem.mutex);. 
a7f30 20 69 66 28 20 70 48 64 72 2d 3e 70 50 72 65 76   if( pHdr->pPrev
a7f40 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
a7f50 70 48 64 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65  pHdr->pPrev->pNe
a7f60 78 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20  xt==pHdr );.    
a7f70 70 48 64 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65  pHdr->pPrev->pNe
a7f80 78 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 78 74  xt = pHdr->pNext
a7f90 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
a7fa0 73 73 65 72 74 28 20 6d 65 6d 2e 70 46 69 72 73  ssert( mem.pFirs
a7fb0 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 6d  t==pHdr );.    m
a7fc0 65 6d 2e 70 46 69 72 73 74 20 3d 20 70 48 64 72  em.pFirst = pHdr
a7fd0 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69  ->pNext;.  }.  i
a7fe0 66 28 20 70 48 64 72 2d 3e 70 4e 65 78 74 20 29  f( pHdr->pNext )
a7ff0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48  {.    assert( pH
a8000 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76  dr->pNext->pPrev
a8010 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 70 48  ==pHdr );.    pH
a8020 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76  dr->pNext->pPrev
a8030 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a   = pHdr->pPrev;.
a8040 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
a8050 65 72 74 28 20 6d 65 6d 2e 70 4c 61 73 74 3d 3d  ert( mem.pLast==
a8060 70 48 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e  pHdr );.    mem.
a8070 70 4c 61 73 74 20 3d 20 70 48 64 72 2d 3e 70 50  pLast = pHdr->pP
a8080 72 65 76 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28  rev;.  }.  z = (
a8090 63 68 61 72 2a 29 70 42 74 3b 0a 20 20 7a 20 2d  char*)pBt;.  z -
a80a0 3d 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a  = pHdr->nTitle;.
a80b0 20 20 61 64 6a 75 73 74 53 74 61 74 73 28 70 48    adjustStats(pH
a80c0 64 72 2d 3e 69 53 69 7a 65 2c 20 2d 31 29 3b 0a  dr->iSize, -1);.
a80d0 20 20 72 61 6e 64 6f 6d 46 69 6c 6c 28 7a 2c 20    randomFill(z, 
a80e0 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 2a 70 48  sizeof(void*)*pH
a80f0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c  dr->nBacktraceSl
a8100 6f 74 73 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48  ots + sizeof(*pH
a8110 64 72 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20  dr) +.          
a8120 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a        pHdr->iSiz
a8130 65 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20  e + sizeof(int) 
a8140 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b  + pHdr->nTitle);
a8150 0a 20 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71  .  free(z);.  sq
a8160 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
a8170 65 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a  e(mem.mutex);  .
a8180 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
a8190 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65  the size of an e
a81a0 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61  xisting memory a
a81b0 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  llocation..**.**
a81c0 20 46 6f 72 20 74 68 69 73 20 64 65 62 75 67 67   For this debugg
a81d0 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ing implementati
a81e0 6f 6e 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20  on, we *always* 
a81f0 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74  make a copy of t
a8200 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  he.** allocation
a8210 20 69 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63   into a new plac
a8220 65 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e  e in memory.  In
a8230 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68   this way, if th
a8240 65 20 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76  e .** higher lev
a8250 65 6c 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67  el code is using
a8260 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
a8270 6f 6c 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20  old allocation, 
a8280 69 74 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d  it is .** much m
a8290 6f 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72  ore likely to br
a82a0 65 61 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d  eak and we are m
a82b0 75 63 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20  uch more liking 
a82c0 74 6f 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65  to find.** the e
a82d0 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rror..*/.static 
a82e0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d  void *sqlite3Mem
a82f0 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50  Realloc(void *pP
a8300 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29  rior, int nByte)
a8310 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  {.  struct MemBl
a8320 6f 63 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b  ockHdr *pOldHdr;
a8330 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20  .  void *pNew;. 
a8340 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73   assert( mem.dis
a8350 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73  allow==0 );.  as
a8360 73 65 72 74 28 20 28 6e 42 79 74 65 20 26 20 37  sert( (nByte & 7
a8370 29 3d 3d 30 20 29 3b 20 20 20 20 20 2f 2a 20 45  )==0 );     /* E
a8380 56 3a 20 52 2d 34 36 31 39 39 2d 33 30 32 34 39  V: R-46199-30249
a8390 20 2a 2f 0a 20 20 70 4f 6c 64 48 64 72 20 3d 20   */.  pOldHdr = 
a83a0 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74  sqlite3MemsysGet
a83b0 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a  Header(pPrior);.
a83c0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
a83d0 4d 65 6d 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29  MemMalloc(nByte)
a83e0 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a  ;.  if( pNew ){.
a83f0 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c      memcpy(pNew,
a8400 20 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 3c 70   pPrior, nByte<p
a8410 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 3f 20  OldHdr->iSize ? 
a8420 6e 42 79 74 65 20 3a 20 70 4f 6c 64 48 64 72 2d  nByte : pOldHdr-
a8430 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28  >iSize);.    if(
a8440 20 6e 42 79 74 65 3e 70 4f 6c 64 48 64 72 2d 3e   nByte>pOldHdr->
a8450 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72  iSize ){.      r
a8460 61 6e 64 6f 6d 46 69 6c 6c 28 26 28 28 63 68 61  andomFill(&((cha
a8470 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 48 64 72  r*)pNew)[pOldHdr
a8480 2d 3e 69 53 69 7a 65 5d 2c 20 6e 42 79 74 65 20  ->iSize], nByte 
a8490 2d 20 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65  - pOldHdr->iSize
a84a0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
a84b0 69 74 65 33 4d 65 6d 46 72 65 65 28 70 50 72 69  ite3MemFree(pPri
a84c0 6f 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  or);.  }.  retur
a84d0 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pNew;.}../*.**
a84e0 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f   Populate the lo
a84f0 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
a8500 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69  llocation functi
a8510 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a  on pointers in.*
a8520 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  * sqlite3GlobalC
a8530 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69  onfig.m with poi
a8540 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75  nters to the rou
a8550 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
a8560 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
a8570 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
a8580 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28  e3MemSetDefault(
a8590 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
a85a0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65  const sqlite3_me
a85b0 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c  m_methods defaul
a85c0 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20  tMethods = {.   
a85d0 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c    sqlite3MemMall
a85e0 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  oc,.     sqlite3
a85f0 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71  MemFree,.     sq
a8600 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c  lite3MemRealloc,
a8610 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
a8620 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74  Size,.     sqlit
a8630 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20  e3MemRoundup,.  
a8640 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69     sqlite3MemIni
a8650 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  t,.     sqlite3M
a8660 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20  emShutdown,.    
a8670 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65   0.  };.  sqlite
a8680 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
a8690 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26  CONFIG_MALLOC, &
a86a0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b  defaultMethods);
a86b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
a86c0 65 20 22 74 79 70 65 22 20 6f 66 20 61 6e 20 61  e "type" of an a
a86d0 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  llocation..*/.SQ
a86e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
a86f0 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  d sqlite3Memdebu
a8700 67 53 65 74 54 79 70 65 28 76 6f 69 64 20 2a 70  gSetType(void *p
a8710 2c 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20 69  , u8 eType){.  i
a8720 66 28 20 70 20 26 26 20 73 71 6c 69 74 65 33 47  f( p && sqlite3G
a8730 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d  lobalConfig.m.xM
a8740 61 6c 6c 6f 63 3d 3d 73 71 6c 69 74 65 33 4d 65  alloc==sqlite3Me
a8750 6d 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 73  mMalloc ){.    s
a8760 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64  truct MemBlockHd
a8770 72 20 2a 70 48 64 72 3b 0a 20 20 20 20 70 48 64  r *pHdr;.    pHd
a8780 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79  r = sqlite3Memsy
a8790 73 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a 20  sGetHeader(p);. 
a87a0 20 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d     assert( pHdr-
a87b0 3e 69 46 6f 72 65 47 75 61 72 64 3d 3d 46 4f 52  >iForeGuard==FOR
a87c0 45 47 55 41 52 44 20 29 3b 0a 20 20 20 20 70 48  EGUARD );.    pH
a87d0 64 72 2d 3e 65 54 79 70 65 20 3d 20 65 54 79 70  dr->eType = eTyp
a87e0 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  e;.  }.}../*.** 
a87f0 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74  Return TRUE if t
a8800 68 65 20 6d 61 73 6b 20 6f 66 20 74 79 70 65 20  he mask of type 
a8810 69 6e 20 65 54 79 70 65 20 6d 61 74 63 68 65 73  in eType matches
a8820 20 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65   the type of the
a8830 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70  .** allocation p
a8840 2e 20 20 41 6c 73 6f 20 72 65 74 75 72 6e 20 74  .  Also return t
a8850 72 75 65 20 69 66 20 70 3d 3d 4e 55 4c 4c 2e 0a  rue if p==NULL..
a8860 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
a8870 6e 65 20 69 73 20 64 65 73 69 67 6e 65 64 20 66  ne is designed f
a8880 6f 72 20 75 73 65 20 77 69 74 68 69 6e 20 61 6e  or use within an
a8890 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
a88a0 65 6e 74 2c 20 74 6f 0a 2a 2a 20 76 65 72 69 66  ent, to.** verif
a88b0 79 20 74 68 65 20 74 79 70 65 20 6f 66 20 61 6e  y the type of an
a88c0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 46 6f   allocation.  Fo
a88d0 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  r example:.**.**
a88e0 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c       assert( sql
a88f0 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54  ite3MemdebugHasT
a8900 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 44  ype(p, MEMTYPE_D
a8910 42 29 20 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f  B) );.*/.SQLITE_
a8920 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
a8930 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54 79  te3MemdebugHasTy
a8940 70 65 28 76 6f 69 64 20 2a 70 2c 20 75 38 20 65  pe(void *p, u8 e
a8950 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  Type){.  int rc 
a8960 3d 20 31 3b 0a 20 20 69 66 28 20 70 20 26 26 20  = 1;.  if( p && 
a8970 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
a8980 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 73  fig.m.xMalloc==s
a8990 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 20  qlite3MemMalloc 
a89a0 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 4d 65  ){.    struct Me
a89b0 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b  mBlockHdr *pHdr;
a89c0 0a 20 20 20 20 70 48 64 72 20 3d 20 73 71 6c 69  .    pHdr = sqli
a89d0 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64  te3MemsysGetHead
a89e0 65 72 28 70 29 3b 0a 20 20 20 20 61 73 73 65 72  er(p);.    asser
a89f0 74 28 20 70 48 64 72 2d 3e 69 46 6f 72 65 47 75  t( pHdr->iForeGu
a8a00 61 72 64 3d 3d 46 4f 52 45 47 55 41 52 44 20 29  ard==FOREGUARD )
a8a10 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c  ;         /* All
a8a20 6f 63 61 74 69 6f 6e 20 69 73 20 76 61 6c 69 64  ocation is valid
a8a30 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 70 48 64   */.    if( (pHd
a8a40 72 2d 3e 65 54 79 70 65 26 65 54 79 70 65 29 3d  r->eType&eType)=
a8a50 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  =0 ){.      rc =
a8a60 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
a8a70 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
a8a80 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
a8a90 69 66 20 74 68 65 20 6d 61 73 6b 20 6f 66 20 74  if the mask of t
a8aa0 79 70 65 20 69 6e 20 65 54 79 70 65 20 6d 61 74  ype in eType mat
a8ab0 63 68 65 73 20 6e 6f 20 62 69 74 73 20 6f 66 20  ches no bits of 
a8ac0 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 0a  the type of the.
a8ad0 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 2e  ** allocation p.
a8ae0 20 20 41 6c 73 6f 20 72 65 74 75 72 6e 20 74 72    Also return tr
a8af0 75 65 20 69 66 20 70 3d 3d 4e 55 4c 4c 2e 0a 2a  ue if p==NULL..*
a8b00 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
a8b10 65 20 69 73 20 64 65 73 69 67 6e 65 64 20 66 6f  e is designed fo
a8b20 72 20 75 73 65 20 77 69 74 68 69 6e 20 61 6e 20  r use within an 
a8b30 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
a8b40 6e 74 2c 20 74 6f 0a 2a 2a 20 76 65 72 69 66 79  nt, to.** verify
a8b50 20 74 68 65 20 74 79 70 65 20 6f 66 20 61 6e 20   the type of an 
a8b60 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 46 6f 72  allocation.  For
a8b70 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20   example:.**.** 
a8b80 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
a8b90 74 65 33 4d 65 6d 64 65 62 75 67 4e 6f 54 79 70  te3MemdebugNoTyp
a8ba0 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 44 42 29  e(p, MEMTYPE_DB)
a8bb0 20 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52   );.*/.SQLITE_PR
a8bc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
a8bd0 33 4d 65 6d 64 65 62 75 67 4e 6f 54 79 70 65 28  3MemdebugNoType(
a8be0 76 6f 69 64 20 2a 70 2c 20 75 38 20 65 54 79 70  void *p, u8 eTyp
a8bf0 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 31  e){.  int rc = 1
a8c00 3b 0a 20 20 69 66 28 20 70 20 26 26 20 73 71 6c  ;.  if( p && sql
a8c10 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
a8c20 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 73 71 6c 69  .m.xMalloc==sqli
a8c30 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 20 29 7b 0a  te3MemMalloc ){.
a8c40 20 20 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c      struct MemBl
a8c50 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20  ockHdr *pHdr;.  
a8c60 20 20 70 48 64 72 20 3d 20 73 71 6c 69 74 65 33    pHdr = sqlite3
a8c70 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 28  MemsysGetHeader(
a8c80 70 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  p);.    assert( 
a8c90 70 48 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64  pHdr->iForeGuard
a8ca0 3d 3d 46 4f 52 45 47 55 41 52 44 20 29 3b 20 20  ==FOREGUARD );  
a8cb0 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61         /* Alloca
a8cc0 74 69 6f 6e 20 69 73 20 76 61 6c 69 64 20 2a 2f  tion is valid */
a8cd0 0a 20 20 20 20 69 66 28 20 28 70 48 64 72 2d 3e  .    if( (pHdr->
a8ce0 65 54 79 70 65 26 65 54 79 70 65 29 21 3d 30 20  eType&eType)!=0 
a8cf0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 30 3b  ){.      rc = 0;
a8d00 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
a8d10 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
a8d20 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20   Set the number 
a8d30 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c 65 76  of backtrace lev
a8d40 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 61 63  els kept for eac
a8d50 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a  h allocation..**
a8d60 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f   A value of zero
a8d70 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63 6b 74   turns off backt
a8d80 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e 75 6d  racing.  The num
a8d90 62 65 72 20 69 73 20 61 6c 77 61 79 73 20 72 6f  ber is always ro
a8da0 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f 20 61  unded.** up to a
a8db0 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 2e 0a   multiple of 2..
a8dc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
a8dd0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  E void sqlite3Me
a8de0 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 28  mdebugBacktrace(
a8df0 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 69 66  int depth){.  if
a8e00 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64 65 70  ( depth<0 ){ dep
a8e10 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 28 20  th = 0; }.  if( 
a8e20 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65 70 74  depth>20 ){ dept
a8e30 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 70 74  h = 20; }.  dept
a8e40 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26 30 78  h = (depth+1)&0x
a8e50 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 6b 74  fe;.  mem.nBackt
a8e60 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a 7d 0a  race = depth;.}.
a8e70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a8e80 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64  void sqlite3Memd
a8e90 65 62 75 67 42 61 63 6b 74 72 61 63 65 43 61 6c  ebugBacktraceCal
a8ea0 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 42 61  lback(void (*xBa
a8eb0 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 6e  cktrace)(int, in
a8ec0 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a 20 20  t, void **)){.  
a8ed0 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 3d  mem.xBacktrace =
a8ee0 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d 0a 0a   xBacktrace;.}..
a8ef0 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69  /*.** Set the ti
a8f00 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 20 73  tle string for s
a8f10 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61  ubsequent alloca
a8f20 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tions..*/.SQLITE
a8f30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
a8f40 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74  lite3MemdebugSet
a8f50 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 61 72  title(const char
a8f60 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75 6e 73   *zTitle){.  uns
a8f70 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20 73 71  igned int n = sq
a8f80 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54  lite3Strlen30(zT
a8f90 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73 71 6c  itle) + 1;.  sql
a8fa0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
a8fb0 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69  (mem.mutex);.  i
a8fc0 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d 65 6d  f( n>=sizeof(mem
a8fd0 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d 20 73  .zTitle) ) n = s
a8fe0 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65  izeof(mem.zTitle
a8ff0 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28 6d 65  )-1;.  memcpy(me
a9000 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74 6c 65  m.zTitle, zTitle
a9010 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54 69 74  , n);.  mem.zTit
a9020 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d  le[n] = 0;.  mem
a9030 2e 6e 54 69 74 6c 65 20 3d 20 52 4f 55 4e 44 38  .nTitle = ROUND8
a9040 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  (n);.  sqlite3_m
a9050 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d  utex_leave(mem.m
a9060 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45  utex);.}..SQLITE
a9070 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
a9080 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 79 6e  lite3MemdebugSyn
a9090 63 28 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65  c(){.  struct Me
a90a0 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b  mBlockHdr *pHdr;
a90b0 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d 2e  .  for(pHdr=mem.
a90c0 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70 48  pFirst; pHdr; pH
a90d0 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29 7b  dr=pHdr->pNext){
a90e0 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 74 20  .    void **pBt 
a90f0 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a  = (void**)pHdr;.
a9100 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d      pBt -= pHdr-
a9110 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73  >nBacktraceSlots
a9120 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b 74  ;.    mem.xBackt
a9130 72 61 63 65 28 70 48 64 72 2d 3e 69 53 69 7a 65  race(pHdr->iSize
a9140 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61  , pHdr->nBacktra
a9150 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 3b 0a  ce-1, &pBt[1]);.
a9160 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65    }.}../*.** Ope
a9170 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63  n the file indic
a9180 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61  ated and write a
a9190 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72   log of all unfr
a91a0 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61  eed memory .** a
a91b0 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20  llocations into 
a91c0 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c  that log..*/.SQL
a91d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a91e0 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67   sqlite3Memdebug
a91f0 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20  Dump(const char 
a9200 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 46  *zFilename){.  F
a9210 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 72 75  ILE *out;.  stru
a9220 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
a9230 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70  pHdr;.  void **p
a9240 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6f  Bt;.  int i;.  o
a9250 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65  ut = fopen(zFile
a9260 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 69 66  name, "w");.  if
a9270 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ( out==0 ){.    
a9280 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
a9290 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75  "** Unable to ou
a92a0 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75  tput memory debu
a92b0 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73  g output log: %s
a92c0 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   **\n",.        
a92d0 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c              zFil
a92e0 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75  ename);.    retu
a92f0 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 48  rn;.  }.  for(pH
a9300 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20 70  dr=mem.pFirst; p
a9310 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d 3e  Hdr; pHdr=pHdr->
a9320 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 61 72  pNext){.    char
a9330 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 48 64   *z = (char*)pHd
a9340 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 64 72  r;.    z -= pHdr
a9350 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74  ->nBacktraceSlot
a9360 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20  s*sizeof(void*) 
a9370 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a  + pHdr->nTitle;.
a9380 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
a9390 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 74 65   "**** %lld byte
a93a0 73 20 61 74 20 25 70 20 66 72 6f 6d 20 25 73 20  s at %p from %s 
a93b0 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 20 20  ****\n", .      
a93c0 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a        pHdr->iSiz
a93d0 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 48 64  e, &pHdr[1], pHd
a93e0 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 3a 20  r->nTitle ? z : 
a93f0 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 28 20  "???");.    if( 
a9400 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
a9410 20 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 73 68   ){.      fflush
a9420 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 42 74  (out);.      pBt
a9430 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b   = (void**)pHdr;
a9440 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20 70 48  .      pBt -= pH
a9450 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c  dr->nBacktraceSl
a9460 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 63 6b 74  ots;.      backt
a9470 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28  race_symbols_fd(
a9480 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b  pBt, pHdr->nBack
a9490 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 6f 75  trace, fileno(ou
a94a0 74 29 29 3b 0a 20 20 20 20 20 20 66 70 72 69 6e  t));.      fprin
a94b0 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20  tf(out, "\n");. 
a94c0 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 69 6e     }.  }.  fprin
a94d0 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 53 3a  tf(out, "COUNTS:
a94e0 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  \n");.  for(i=0;
a94f0 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 2b 2b   i<NCSIZE-1; i++
a9500 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e  ){.    if( mem.n
a9510 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 20 20  Alloc[i] ){.    
a9520 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
a9530 20 20 20 25 35 64 3a 20 25 31 30 64 20 25 31 30     %5d: %10d %10
a9540 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 20 20  d %10d\n", .    
a9550 20 20 20 20 20 20 20 20 69 2a 38 2c 20 6d 65 6d          i*8, mem
a9560 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 6d 2e  .nAlloc[i], mem.
a9570 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d 65 6d  nCurrent[i], mem
a9580 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 3b 0a  .mxCurrent[i]);.
a9590 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
a95a0 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a  mem.nAlloc[NCSIZ
a95b0 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 72 69  E-1] ){.    fpri
a95c0 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 64  ntf(out, "   %5d
a95d0 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30 64  : %10d %10d %10d
a95e0 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
a95f0 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 6d 65    NCSIZE*8-8, me
a9600 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d  m.nAlloc[NCSIZE-
a9610 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  1],.            
a9620 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 4e 43   mem.nCurrent[NC
a9630 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 43  SIZE-1], mem.mxC
a9640 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d  urrent[NCSIZE-1]
a9650 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 65 28  );.  }.  fclose(
a9660 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  out);.}../*.** R
a9670 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
a9680 20 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 74 65   of times sqlite
a9690 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 61 73  3MemMalloc() has
a96a0 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f   been called..*/
a96b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a96c0 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 64 65  int sqlite3Memde
a96d0 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 28 29  bugMallocCount()
a96e0 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
a96f0 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 66   nTotal = 0;.  f
a9700 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a 45  or(i=0; i<NCSIZE
a9710 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 6f 74  ; i++){.    nTot
a9720 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f 63  al += mem.nAlloc
a9730 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  [i];.  }.  retur
a9740 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 65  n nTotal;.}...#e
a9750 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d  ndif /* SQLITE_M
a9760 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a  EMDEBUG */../***
a9770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
a9780 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a  of mem2.c ******
a9790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a97a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a97b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
a97c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
a97d0 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 2a 2a  n file mem3.c **
a97e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a97f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
a9810 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 31  * 2007 October 1
a9820 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  4.**.** The auth
a9830 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
a9840 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
a9850 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
a9860 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
a9870 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
a9880 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
a9890 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
a98a0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
a98b0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
a98c0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
a98d0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
a98e0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
a98f0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
a9900 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
a9910 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
a9920 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
a9930 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
a9940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9980 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
a9990 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e  ntains the C fun
a99a0 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c  ctions that impl
a99b0 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a  ement a memory.*
a99c0 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  * allocation sub
a99d0 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 62  system for use b
a99e0 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a  y SQLite. .**.**
a99f0 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   This version of
a9a00 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
a9a10 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
a9a20 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73   omits all.** us
a9a30 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54  e of malloc(). T
a9a40 68 65 20 53 51 4c 69 74 65 20 75 73 65 72 20 73  he SQLite user s
a9a50 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b 20  upplies a block 
a9a60 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66  of memory.** bef
a9a70 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69  ore calling sqli
a9a80 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
a9a90 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f   from which allo
a9aa0 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d  cations.** are m
a9ab0 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64  ade and returned
a9ac0 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28   by the xMalloc(
a9ad0 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29  ) and xRealloc()
a9ae0 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74   .** implementat
a9af0 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74  ions. Once sqlit
a9b00 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
a9b10 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c  has been called,
a9b20 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f  .** the amount o
a9b30 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62  f memory availab
a9b40 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20  le to SQLite is 
a9b50 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74  fixed and cannot
a9b60 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a  .** be changed..
a9b70 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  **.** This versi
a9b80 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
a9b90 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
a9ba0 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65  ystem is include
a9bb0 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c  d.** in the buil
a9bc0 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45  d only if SQLITE
a9bd0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20  _ENABLE_MEMSYS3 
a9be0 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 0a  is defined..*/..
a9bf0 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  /*.** This versi
a9c00 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
a9c10 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f 6e   allocator is on
a9c20 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68  ly built into th
a9c30 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 4c  e library.** SQL
a9c40 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
a9c50 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 20 44  S3 is defined. D
a9c60 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73 79 6d  efining this sym
a9c70 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20  bol does not.** 
a9c80 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69  mean that the li
a9c90 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65 20 61  brary will use a
a9ca0 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 20   memory-pool by 
a9cb0 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 68  default, just th
a9cc0 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 69  at.** it is avai
a9cd0 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 6f  lable. The mempo
a9ce0 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ol allocator is 
a9cf0 61 63 74 69 76 61 74 65 64 20 62 79 20 63 61 6c  activated by cal
a9d00 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ling.** sqlite3_
a9d10 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 66  config()..*/.#if
a9d20 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
a9d30 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a  E_MEMSYS3../*.**
a9d40 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 69   Maximum size (i
a9d50 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f 66  n Mem3Blocks) of
a9d60 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e 6b   a "small" chunk
a9d70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f  ..*/.#define MX_
a9d80 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a  SMALL 10.../*.**
a9d90 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c   Number of freel
a9da0 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 0a 2a  ist hash slots.*
a9db0 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 48  /.#define N_HASH
a9dc0 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65    61../*.** A me
a9dd0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
a9de0 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 22  (also called a "
a9df0 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 73  chunk") consists
a9e00 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d   of two or .** m
a9e10 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 65  ore blocks where
a9e20 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 38   each block is 8
a9e30 20 62 79 74 65 73 2e 20 20 54 68 65 20 66 69 72   bytes.  The fir
a9e40 73 74 20 38 20 62 79 74 65 73 20 61 72 65 20 0a  st 8 bytes are .
a9e50 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68 61 74  ** a header that
a9e60 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 64   is not returned
a9e70 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a 2a   to the user..**
a9e80 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 74  .** A chunk is t
a9e90 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 6b  wo or more block
a9ea0 73 20 74 68 61 74 20 69 73 20 65 69 74 68 65 72  s that is either
a9eb0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 0a   checked out or.
a9ec0 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20 66 69  ** free.  The fi
a9ed0 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 6f  rst block has fo
a9ee0 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75 2e 68  rmat u.hdr.  u.h
a9ef0 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34 20 74  dr.size4x is 4 t
a9f00 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65  imes the.** size
a9f10 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   of the allocati
a9f20 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20  on in blocks if 
a9f30 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  the allocation i
a9f40 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 75  s free..** The u
a9f50 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20 62 69  .hdr.size4x&1 bi
a9f60 74 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65  t is true if the
a9f70 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65   chunk is checke
a9f80 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 6c  d out and.** fal
a9f90 73 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20  se if the chunk 
a9fa0 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69  is on the freeli
a9fb0 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 73  st.  The u.hdr.s
a9fc0 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 69  ize4x&2 bit.** i
a9fd0 73 20 74 72 75 65 20 69 66 20 74 68 65 20 70 72  s true if the pr
a9fe0 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20  evious chunk is 
a9ff0 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20  checked out and 
aa000 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a 20  false if the.** 
aa010 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69  previous chunk i
aa020 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 2e 68  s free.  The u.h
aa030 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 65 6c  dr.prevSize fiel
aa040 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66  d is the size of
aa050 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73  .** the previous
aa060 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73   chunk in blocks
aa070 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   if the previous
aa080 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65   chunk is on the
aa090 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 66  .** freelist. If
aa0a0 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68   the previous ch
aa0b0 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f  unk is checked o
aa0c0 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 64  ut, then.** u.hd
aa0d0 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e 20 62  r.prevSize can b
aa0e0 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61  e part of the da
aa0f0 74 61 20 66 6f 72 20 74 68 61 74 20 63 68 75 6e  ta for that chun
aa100 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20  k and should.** 
aa110 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77  not be read or w
aa120 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65  ritten..**.** We
aa130 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66 79 20   often identify 
aa140 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73 20 69  a chunk by its i
aa150 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f  ndex in mem3.aPo
aa160 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 74  ol[].  When.** t
aa170 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 65  his is done, the
aa180 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 66   chunk index ref
aa190 65 72 73 20 74 6f 20 74 68 65 20 73 65 63 6f 6e  ers to the secon
aa1a0 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68  d block of.** th
aa1b0 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 69  e chunk.  In thi
aa1c0 73 20 77 61 79 2c 20 74 68 65 20 66 69 72 73 74  s way, the first
aa1d0 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 6e   chunk has an in
aa1e0 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 63  dex of 1..** A c
aa1f0 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 20  hunk index of 0 
aa200 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 63  means "no such c
aa210 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 65  hunk" and is the
aa220 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f   equivalent.** o
aa230 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  f a NULL pointer
aa240 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ..**.** The seco
aa250 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 65  nd block of free
aa260 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 68   chunks is of th
aa270 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 20  e form u.list.  
aa280 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c 64  The.** two field
aa290 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 2d  s form a double-
aa2a0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 63  linked list of c
aa2b0 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 64  hunks of related
aa2c0 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74   sizes..** Point
aa2d0 65 72 73 20 74 6f 20 74 68 65 20 68 65 61 64 20  ers to the head 
aa2e0 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72 65 20  of the list are 
aa2f0 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e 61  stored in mem3.a
aa300 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72  iSmall[] .** for
aa310 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 20   smaller chunks 
aa320 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b  and mem3.aiHash[
aa330 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63 68 75  ] for larger chu
aa340 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  nks..**.** The s
aa350 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 61  econd block of a
aa360 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 20 64   chunk is user d
aa370 61 74 61 20 69 66 20 74 68 65 20 63 68 75 6e 6b  ata if the chunk
aa380 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a 20   is checked .** 
aa390 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75 6e 6b  out.  If a chunk
aa3a0 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c   is checked out,
aa3b0 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 6d   the user data m
aa3c0 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a 2a  ay extend into.*
aa3d0 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72 65 76  * the u.hdr.prev
aa3e0 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 68  Size value of th
aa3f0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 6e  e following chun
aa400 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  k..*/.typedef st
aa410 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d  ruct Mem3Block M
aa420 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 74  em3Block;.struct
aa430 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 75   Mem3Block {.  u
aa440 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63  nion {.    struc
aa450 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70 72  t {.      u32 pr
aa460 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 7a  evSize;   /* Siz
aa470 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20 63 68  e of previous ch
aa480 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b  unk in Mem3Block
aa490 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20   elements */.   
aa4a0 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b 20 20     u32 size4x;  
aa4b0 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73 69 7a     /* 4x the siz
aa4c0 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 75  e of current chu
aa4d0 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20  nk in Mem3Block 
aa4e0 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  elements */.    
aa4f0 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72 75 63  } hdr;.    struc
aa500 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6e 65  t {.      u32 ne
aa510 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  xt;       /* Ind
aa520 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ex in mem3.aPool
aa530 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20  [] of next free 
aa540 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 75  chunk */.      u
aa550 33 32 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f  32 prev;       /
aa560 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e  * Index in mem3.
aa570 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69  aPool[] of previ
aa580 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a  ous free chunk *
aa590 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 20  /.    } list;.  
aa5a0 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  } u;.};../*.** A
aa5b0 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63  ll of the static
aa5c0 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20   variables used 
aa5d0 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61  by this module a
aa5e0 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20  re collected.** 
aa5f0 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74  into a single st
aa600 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d  ructure named "m
aa610 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73 20 74  em3".  This is t
aa620 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74  o keep the.** st
aa630 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f  atic variables o
aa640 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20  rganized and to 
aa650 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65  reduce namespace
aa660 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68   pollution.** wh
aa670 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69  en this module i
aa680 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20  s combined with 
aa690 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61  other in the ama
aa6a0 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  lgamation..*/.st
aa6b0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
aa6c0 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61  struct Mem3Globa
aa6d0 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65  l {.  /*.  ** Me
aa6e0 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66  mory available f
aa6f0 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e  or allocation. n
aa700 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69 7a 65  Pool is the size
aa710 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a 20 20   of the array.  
aa720 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b  ** (in Mem3Block
aa730 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  s) pointed to by
aa740 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a 20   aPool less 2.. 
aa750 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c 3b   */.  u32 nPool;
aa760 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 50  .  Mem3Block *aP
aa770 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ool;..  /*.  ** 
aa780 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20 65  True if we are e
aa790 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 74  valuating an out
aa7a0 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c 62  -of-memory callb
aa7b0 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  ack..  */.  int 
aa7c0 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20  alarmBusy;.  .  
aa7d0 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f  /*.  ** Mutex to
aa7e0 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20   control access 
aa7f0 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  to the memory al
aa800 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
aa810 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  em..  */.  sqlit
aa820 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b  e3_mutex *mutex;
aa830 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68  .  .  /*.  ** Th
aa840 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74  e minimum amount
aa850 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 20 74   of free space t
aa860 68 61 74 20 77 65 20 68 61 76 65 20 73 65 65 6e  hat we have seen
aa870 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d  ..  */.  u32 mnM
aa880 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  aster;..  /*.  *
aa890 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 68 65  * iMaster is the
aa8a0 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61   index of the ma
aa8b0 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f 73  ster chunk.  Mos
aa8c0 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e  t new allocation
aa8d0 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 66  s.  ** occur off
aa8e0 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 2e 20   of this chunk. 
aa8f0 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65   szMaster is the
aa900 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c   size (in Mem3Bl
aa910 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 68  ocks).  ** of th
aa920 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74 65 72  e current master
aa930 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20 30 20  .  iMaster is 0 
aa940 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20  if there is not 
aa950 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 20  master chunk..  
aa960 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 63 68  ** The master ch
aa970 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 69  unk is not in ei
aa980 74 68 65 72 20 74 68 65 20 61 69 48 61 73 68 5b  ther the aiHash[
aa990 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a  ] or aiSmall[]..
aa9a0 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 74    */.  u32 iMast
aa9b0 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 74  er;.  u32 szMast
aa9c0 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41  er;..  /*.  ** A
aa9d0 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20 6f 66  rray of lists of
aa9e0 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 63   free blocks acc
aa9f0 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 62 6c  ording to the bl
aaa00 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 66  ock size .  ** f
aaa10 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b  or smaller chunk
aaa20 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f 6e 20  s, or a hash on 
aaa30 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 66  the block size f
aaa40 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 63  or larger.  ** c
aaa50 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 33  hunks..  */.  u3
aaa60 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41  2 aiSmall[MX_SMA
aaa70 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 20  LL-1];   /* For 
aaa80 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67 68 20  sizes 2 through 
aaa90 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73  MX_SMALL, inclus
aaaa0 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 48  ive */.  u32 aiH
aaab0 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 20  ash[N_HASH];    
aaac0 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73      /* For sizes
aaad0 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 20   MX_SMALL+1 and 
aaae0 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33  larger */.} mem3
aaaf0 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3b   = { 97535575 };
aab00 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 47  ..#define mem3 G
aab10 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d  LOBAL(struct Mem
aab20 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a  3Global, mem3)..
aab30 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65  /*.** Unlink the
aab40 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61   chunk at mem3.a
aab50 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73  Pool[i] from lis
aab60 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c  t it is currentl
aab70 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74  y.** on.  *pRoot
aab80 20 69 73 20 74 68 65 20 6c 69 73 74 20 74 68 61   is the list tha
aab90 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65 72 20  t i is a member 
aaba0 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  of..*/.static vo
aabb0 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b  id memsys3Unlink
aabc0 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c 20  FromList(u32 i, 
aabd0 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75  u32 *pRoot){.  u
aabe0 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e 61  32 next = mem3.a
aabf0 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e  Pool[i].u.list.n
aac00 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65 76 20  ext;.  u32 prev 
aac10 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e  = mem3.aPool[i].
aac20 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 61  u.list.prev;.  a
aac30 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
aac40 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d  utex_held(mem3.m
aac50 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
aac60 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70  rev==0 ){.    *p
aac70 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d  Root = next;.  }
aac80 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e 61  else{.    mem3.a
aac90 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73  Pool[prev].u.lis
aaca0 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20  t.next = next;. 
aacb0 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20 29 7b   }.  if( next ){
aacc0 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  .    mem3.aPool[
aacd0 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65  next].u.list.pre
aace0 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 20  v = prev;.  }.  
aacf0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e  mem3.aPool[i].u.
aad00 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a 20  list.next = 0;. 
aad10 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75   mem3.aPool[i].u
aad20 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a  .list.prev = 0;.
aad30 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20  }../*.** Unlink 
aad40 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64  the chunk at ind
aad50 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 68  ex i from .** wh
aad60 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 20 63  atever list is c
aad70 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 65  urrently a membe
aad80 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  r of..*/.static 
aad90 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69  void memsys3Unli
aada0 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32  nk(u32 i){.  u32
aadb0 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61   size, hash;.  a
aadc0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
aadd0 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d  utex_held(mem3.m
aade0 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
aadf0 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t( (mem3.aPool[i
aae00 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
aae10 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 73   & 1)==0 );.  as
aae20 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20  sert( i>=1 );.  
aae30 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f  size = mem3.aPoo
aae40 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
aae50 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28  e4x/4;.  assert(
aae60 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f   size==mem3.aPoo
aae70 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64  l[i+size-1].u.hd
aae80 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20  r.prevSize );.  
aae90 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20  assert( size>=2 
aaea0 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d  );.  if( size <=
aaeb0 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20   MX_SMALL ){.   
aaec0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72   memsys3UnlinkFr
aaed0 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e  omList(i, &mem3.
aaee0 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29  aiSmall[size-2])
aaef0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68  ;.  }else{.    h
aaf00 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48  ash = size % N_H
aaf10 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33  ASH;.    memsys3
aaf20 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69  UnlinkFromList(i
aaf30 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68  , &mem3.aiHash[h
aaf40 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ash]);.  }.}../*
aaf50 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75  .** Link the chu
aaf60 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c  nk at mem3.aPool
aaf70 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f  [i] so that is o
aaf80 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 65  n the list roote
aaf90 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e 0a  d.** at *pRoot..
aafa0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
aafb0 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69  emsys3LinkIntoLi
aafc0 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70  st(u32 i, u32 *p
aafd0 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 28  Root){.  assert(
aafe0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
aaff0 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
ab000 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  );.  mem3.aPool[
ab010 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d  i].u.list.next =
ab020 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e   *pRoot;.  mem3.
ab030 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e  aPool[i].u.list.
ab040 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 20  prev = 0;.  if( 
ab050 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d 65  *pRoot ){.    me
ab060 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d  m3.aPool[*pRoot]
ab070 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 69  .u.list.prev = i
ab080 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d  ;.  }.  *pRoot =
ab090 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e   i;.}../*.** Lin
ab0a0 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69  k the chunk at i
ab0b0 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69 74 68  ndex i into eith
ab0c0 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  er the appropria
ab0d0 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 6e  te.** small chun
ab0e0 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f 20  k list, or into 
ab0f0 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20  the large chunk 
ab100 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  hash table..*/.s
ab110 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
ab120 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20  s3Link(u32 i){. 
ab130 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b   u32 size, hash;
ab140 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
ab150 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
ab160 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  m3.mutex) );.  a
ab170 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20  ssert( i>=1 );. 
ab180 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61   assert( (mem3.a
ab190 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
ab1a0 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29  size4x & 1)==0 )
ab1b0 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e  ;.  size = mem3.
ab1c0 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
ab1d0 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73  .size4x/4;.  ass
ab1e0 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e  ert( size==mem3.
ab1f0 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e  aPool[i+size-1].
ab200 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29  u.hdr.prevSize )
ab210 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
ab220 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a  >=2 );.  if( siz
ab230 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b  e <= MX_SMALL ){
ab240 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b  .    memsys3Link
ab250 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d  IntoList(i, &mem
ab260 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32  3.aiSmall[size-2
ab270 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ]);.  }else{.   
ab280 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e   hash = size % N
ab290 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79  _HASH;.    memsy
ab2a0 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69  s3LinkIntoList(i
ab2b0 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68  , &mem3.aiHash[h
ab2c0 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ash]);.  }.}../*
ab2d0 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 49  .** If the STATI
ab2e0 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e  C_MEM mutex is n
ab2f0 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c  ot already held,
ab300 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20   obtain it now. 
ab310 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c  The mutex.** wil
ab320 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c  l already be hel
ab330 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63  d (obtained by c
ab340 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29  ode in malloc.c)
ab350 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c   if.** sqlite3Gl
ab360 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53  obalConfig.bMemS
ab370 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a  tat is true..*/.
ab380 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
ab390 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a  ys3Enter(void){.
ab3a0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
ab3b0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
ab3c0 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d 75  at==0 && mem3.mu
ab3d0 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65  tex==0 ){.    me
ab3e0 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  m3.mutex = sqlit
ab3f0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
ab400 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
ab410 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  _MEM);.  }.  sql
ab420 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
ab430 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a  (mem3.mutex);.}.
ab440 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
ab450 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a  ys3Leave(void){.
ab460 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
ab470 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78  leave(mem3.mutex
ab480 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  );.}../*.** Call
ab490 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 20 75  ed when we are u
ab4a0 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79  nable to satisfy
ab4b0 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f   an allocation o
ab4c0 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  f nBytes..*/.sta
ab4d0 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33  tic void memsys3
ab4e0 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20  OutOfMemory(int 
ab4f0 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20 21 6d  nByte){.  if( !m
ab500 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 7b  em3.alarmBusy ){
ab510 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42  .    mem3.alarmB
ab520 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 73  usy = 1;.    ass
ab530 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
ab540 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
ab550 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ex) );.    sqlit
ab560 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
ab570 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em3.mutex);.    
ab580 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
ab590 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a 20  memory(nByte);. 
ab5a0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
ab5b0 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65  _enter(mem3.mute
ab5c0 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61  x);.    mem3.ala
ab5d0 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d 0a  rmBusy = 0;.  }.
ab5e0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20  }.../*.** Chunk 
ab5f0 69 20 69 73 20 61 20 66 72 65 65 20 63 68 75 6e  i is a free chun
ab600 6b 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20  k that has been 
ab610 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 73  unlinked.  Adjus
ab620 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 70  t its .** size p
ab630 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 63 68  arameters for ch
ab640 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 75  eck-out and retu
ab650 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
ab660 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f 72  the .** user por
ab670 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 6e  tion of the chun
ab680 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  k..*/.static voi
ab690 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f  d *memsys3Checko
ab6a0 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 6e 42  ut(u32 i, u32 nB
ab6b0 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b 0a  lock){.  u32 x;.
ab6c0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
ab6d0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d  3_mutex_held(mem
ab6e0 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  3.mutex) );.  as
ab6f0 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20  sert( i>=1 );.  
ab700 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f  assert( mem3.aPo
ab710 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
ab720 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29  ze4x/4==nBlock )
ab730 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33  ;.  assert( mem3
ab740 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d  .aPool[i+nBlock-
ab750 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
ab760 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 78  e==nBlock );.  x
ab770 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d   = mem3.aPool[i-
ab780 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b  1].u.hdr.size4x;
ab790 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d  .  mem3.aPool[i-
ab7a0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
ab7b0 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c  = nBlock*4 | 1 |
ab7c0 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e 61   (x&2);.  mem3.a
ab7d0 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d  Pool[i+nBlock-1]
ab7e0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
ab7f0 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33  = nBlock;.  mem3
ab800 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d  .aPool[i+nBlock-
ab810 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
ab820 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 26  |= 2;.  return &
ab830 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d  mem3.aPool[i];.}
ab840 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 20  ../*.** Carve a 
ab850 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 65  piece off of the
ab860 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d 33   end of the mem3
ab870 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20 63 68  .iMaster free ch
ab880 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61  unk..** Return a
ab890 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
ab8a0 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  new allocation. 
ab8b0 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 73 74   Or, if the mast
ab8c0 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e  er chunk.** is n
ab8d0 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c  ot large enough,
ab8e0 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74   return 0..*/.st
ab8f0 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79  atic void *memsy
ab900 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75 33 32  s3FromMaster(u32
ab910 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 65   nBlock){.  asse
ab920 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
ab930 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65  x_held(mem3.mute
ab940 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
ab950 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e  mem3.szMaster>=n
ab960 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 6e  Block );.  if( n
ab970 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61  Block>=mem3.szMa
ab980 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f 2a  ster-1 ){.    /*
ab990 20 55 73 65 20 74 68 65 20 65 6e 74 69 72 65 20   Use the entire 
ab9a0 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 6f  master */.    vo
ab9b0 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 43  id *p = memsys3C
ab9c0 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d 61  heckout(mem3.iMa
ab9d0 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73  ster, mem3.szMas
ab9e0 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 69  ter);.    mem3.i
ab9f0 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Master = 0;.    
aba00 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20  mem3.szMaster = 
aba10 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61  0;.    mem3.mnMa
aba20 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 65  ster = 0;.    re
aba30 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 7b  turn p;.  }else{
aba40 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 68  .    /* Split th
aba50 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20  e master block. 
aba60 20 52 65 74 75 72 6e 20 74 68 65 20 74 61 69 6c   Return the tail
aba70 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 77  . */.    u32 new
aba80 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 3d  i, x;.    newi =
aba90 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b 20   mem3.iMaster + 
abaa0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 20  mem3.szMaster - 
abab0 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65  nBlock;.    asse
abac0 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 2e  rt( newi > mem3.
abad0 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 20  iMaster+1 );.   
abae0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
abaf0 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a  .iMaster+mem3.sz
abb00 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
abb10 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63  prevSize = nBloc
abb20 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  k;.    mem3.aPoo
abb30 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d  l[mem3.iMaster+m
abb40 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e  em3.szMaster-1].
abb50 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20  u.hdr.size4x |= 
abb60 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  2;.    mem3.aPoo
abb70 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e  l[newi-1].u.hdr.
abb80 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a  size4x = nBlock*
abb90 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 2e  4 + 1;.    mem3.
abba0 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c 6f  szMaster -= nBlo
abbb0 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f  ck;.    mem3.aPo
abbc0 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72  ol[newi-1].u.hdr
abbd0 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33  .prevSize = mem3
abbe0 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 78  .szMaster;.    x
abbf0 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65   = mem3.aPool[me
abc00 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  m3.iMaster-1].u.
abc10 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a  hdr.size4x & 2;.
abc20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d      mem3.aPool[m
abc30 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
abc40 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65  .hdr.size4x = me
abc50 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20  m3.szMaster*4 | 
abc60 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e  x;.    if( mem3.
abc70 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 2e  szMaster < mem3.
abc80 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20  mnMaster ){.    
abc90 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20    mem3.mnMaster 
abca0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b  = mem3.szMaster;
abcb0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
abcc0 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e 61  n (void*)&mem3.a
abcd0 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a  Pool[newi];.  }.
abce0 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20  }../*.** *pRoot 
abcf0 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20 61  is the head of a
abd00 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 63 68   list of free ch
abd10 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 6d 65  unks of the same
abd20 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d 65   size.** or same
abd30 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49 6e 20   size hash.  In 
abd40 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 52  other words, *pR
abd50 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72 79 20  oot is an entry 
abd60 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 6d  in either.** mem
abd70 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d  3.aiSmall[] or m
abd80 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 0a  em3.aiHash[].  .
abd90 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
abda0 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c 20  ne examines all 
abdb0 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65 20 67  entries on the g
abdc0 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 72  iven list and tr
abdd0 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 73  ies.** to coales
abde0 63 65 20 65 61 63 68 20 65 6e 74 72 69 65 73 20  ce each entries 
abdf0 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20 66 72  with adjacent fr
abe00 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a  ee chunks.  .**.
abe10 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20 61 20  ** If it sees a 
abe20 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20 6c 61  chunk that is la
abe30 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e 69  rger than mem3.i
abe40 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70 6c 61  Master, it repla
abe50 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 72  ces .** the curr
abe60 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ent mem3.iMaster
abe70 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 6c 61   with the new la
abe80 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e 20  rger chunk.  In 
abe90 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 69  order for.** thi
abea0 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 72  s mem3.iMaster r
abeb0 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 6f  eplacement to wo
abec0 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72 20 63  rk, the master c
abed0 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a 20  hunk must be.** 
abee0 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 20  linked into the 
abef0 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20 54 68  hash tables.  Th
abf00 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 6e 6f  at is not the no
abf10 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a 2a  rmal state of.**
abf20 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63 6f 75   affairs, of cou
abf30 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e  rse.  The callin
abf40 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6c  g routine must l
abf50 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72 0a 2a  ink the master.*
abf60 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 69  * chunk before i
abf70 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75  nvoking this rou
abf80 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 20  tine, then must 
abf90 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 73  unlink the (poss
abfa0 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 29  ibly.** changed)
abfb0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6f 6e   master chunk on
abfc0 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ce this routine 
abfd0 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2f  has finished..*/
abfe0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
abff0 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 2a 70  sys3Merge(u32 *p
ac000 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e 65  Root){.  u32 iNe
ac010 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c 20  xt, prev, size, 
ac020 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 28  i, x;..  assert(
ac030 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
ac040 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
ac050 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f 6f  );.  for(i=*pRoo
ac060 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 29  t; i>0; i=iNext)
ac070 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 6d 65  {.    iNext = me
ac080 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69  m3.aPool[i].u.li
ac090 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 7a  st.next;.    siz
ac0a0 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  e = mem3.aPool[i
ac0b0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
ac0c0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 73  ;.    assert( (s
ac0d0 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 20  ize&1)==0 );.   
ac0e0 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d 3d 30   if( (size&2)==0
ac0f0 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73   ){.      memsys
ac100 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28  3UnlinkFromList(
ac110 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20  i, pRoot);.     
ac120 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d 65 6d   assert( i > mem
ac130 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
ac140 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20  dr.prevSize );. 
ac150 20 20 20 20 20 70 72 65 76 20 3d 20 69 20 2d 20       prev = i - 
ac160 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e  mem3.aPool[i-1].
ac170 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a  u.hdr.prevSize;.
ac180 20 20 20 20 20 20 69 66 28 20 70 72 65 76 3d 3d        if( prev==
ac190 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20  iNext ){.       
ac1a0 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50   iNext = mem3.aP
ac1b0 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74  ool[prev].u.list
ac1c0 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  .next;.      }. 
ac1d0 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69       memsys3Unli
ac1e0 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20  nk(prev);.      
ac1f0 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 2f  size = i + size/
ac200 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 20  4 - prev;.      
ac210 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70  x = mem3.aPool[p
ac220 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  rev-1].u.hdr.siz
ac230 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d  e4x & 2;.      m
ac240 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31  em3.aPool[prev-1
ac250 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d  ].u.hdr.size4x =
ac260 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 20   size*4 | x;.   
ac270 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72     mem3.aPool[pr
ac280 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72  ev+size-1].u.hdr
ac290 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65  .prevSize = size
ac2a0 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4c  ;.      memsys3L
ac2b0 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20  ink(prev);.     
ac2c0 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 7d   i = prev;.    }
ac2d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 7a 65  else{.      size
ac2e0 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20   /= 4;.    }.   
ac2f0 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e 73   if( size>mem3.s
ac300 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20  zMaster ){.     
ac310 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20   mem3.iMaster = 
ac320 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a  i;.      mem3.sz
ac330 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a 20  Master = size;. 
ac340 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
ac350 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b  * Return a block
ac360 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74   of memory of at
ac370 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69 6e   least nBytes in
ac380 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e   size..** Return
ac390 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e   NULL if unable.
ac3a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
ac3b0 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61  tion assumes tha
ac3c0 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20  t the necessary 
ac3d0 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c  mutexes, if any,
ac3e0 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20   are.** already 
ac3f0 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c  held by the call
ac400 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66  er. Hence "Unsaf
ac410 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  e"..*/.static vo
ac420 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f  id *memsys3Mallo
ac430 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74  cUnsafe(int nByt
ac440 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 75  e){.  u32 i;.  u
ac450 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33 32  32 nBlock;.  u32
ac460 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 65   toFree;..  asse
ac470 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
ac480 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65  x_held(mem3.mute
ac490 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
ac4a0 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b  sizeof(Mem3Block
ac4b0 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e 42  )==8 );.  if( nB
ac4c0 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 6e  yte<=12 ){.    n
ac4d0 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c  Block = 2;.  }el
ac4e0 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d  se{.    nBlock =
ac4f0 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 3b   (nByte + 11)/8;
ac500 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e  .  }.  assert( n
ac510 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f  Block>=2 );..  /
ac520 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 4c  * STEP 1:.  ** L
ac530 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79  ook for an entry
ac540 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 20   of the correct 
ac550 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72 20 74  size in either t
ac560 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68  he small.  ** ch
ac570 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e 20  unk table or in 
ac580 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20  the large chunk 
ac590 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69  hash table.  Thi
ac5a0 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 73  s is.  ** succes
ac5b0 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 65  sful most of the
ac5c0 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39 20 74   time (about 9 t
ac5d0 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30 29 2e  imes out of 10).
ac5e0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c 6f  .  */.  if( nBlo
ac5f0 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29  ck <= MX_SMALL )
ac600 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e 61  {.    i = mem3.a
ac610 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d  iSmall[nBlock-2]
ac620 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b  ;.    if( i>0 ){
ac630 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e  .      memsys3Un
ac640 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20  linkFromList(i, 
ac650 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42  &mem3.aiSmall[nB
ac660 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 20  lock-2]);.      
ac670 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68  return memsys3Ch
ac680 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b  eckout(i, nBlock
ac690 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
ac6a0 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68 20 3d  {.    int hash =
ac6b0 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 48   nBlock % N_HASH
ac6c0 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d 33  ;.    for(i=mem3
ac6d0 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 69  .aiHash[hash]; i
ac6e0 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c  >0; i=mem3.aPool
ac6f0 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29  [i].u.list.next)
ac700 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33  {.      if( mem3
ac710 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64  .aPool[i-1].u.hd
ac720 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f  r.size4x/4==nBlo
ac730 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  ck ){.        me
ac740 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c  msys3UnlinkFromL
ac750 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48  ist(i, &mem3.aiH
ac760 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 20  ash[hash]);.    
ac770 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79      return memsy
ac780 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42  s3Checkout(i, nB
ac790 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20  lock);.      }. 
ac7a0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53     }.  }..  /* S
ac7b0 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 20  TEP 2:.  ** Try 
ac7c0 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 61  to satisfy the a
ac7d0 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72  llocation by car
ac7e0 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66  ving a piece off
ac7f0 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a   of the end.  **
ac800 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63   of the master c
ac810 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70  hunk.  This step
ac820 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 69   usually works i
ac830 66 20 73 74 65 70 20 31 20 66 61 69 6c 73 2e 0a  f step 1 fails..
ac840 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e    */.  if( mem3.
ac850 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b  szMaster>=nBlock
ac860 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d   ){.    return m
ac870 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72  emsys3FromMaster
ac880 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a  (nBlock);.  }...
ac890 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a 20    /* STEP 3:  . 
ac8a0 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   ** Loop through
ac8b0 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65 6d 6f   the entire memo
ac8c0 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73  ry pool.  Coales
ac8d0 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65  ce adjacent free
ac8e0 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 52  .  ** chunks.  R
ac8f0 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 73  ecompute the mas
ac900 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74 68 65  ter chunk as the
ac910 20 6c 61 72 67 65 73 74 20 66 72 65 65 20 63 68   largest free ch
ac920 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 74  unk..  ** Then t
ac930 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61 74 69  ry again to sati
ac940 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 69  sfy the allocati
ac950 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61 20  on by carving a 
ac960 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 6f  piece off.  ** o
ac970 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  f the end of the
ac980 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20   master chunk.  
ac990 54 68 69 73 20 73 74 65 70 20 68 61 70 70 65 6e  This step happen
ac9a0 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65  s very.  ** rare
ac9b0 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a 20 20  ly (we hope!).  
ac9c0 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 3d  */.  for(toFree=
ac9d0 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 65  nBlock*16; toFre
ac9e0 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36  e<(mem3.nPool*16
ac9f0 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 7b  ); toFree *= 2){
aca00 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 4f  .    memsys3OutO
aca10 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 3b  fMemory(toFree);
aca20 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d  .    if( mem3.iM
aca30 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d  aster ){.      m
aca40 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e  emsys3Link(mem3.
aca50 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  iMaster);.      
aca60 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30  mem3.iMaster = 0
aca70 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d  ;.      mem3.szM
aca80 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 7d  aster = 0;.    }
aca90 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
acaa0 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20  N_HASH; i++){.  
acab0 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65      memsys3Merge
acac0 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d  (&mem3.aiHash[i]
acad0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  );.    }.    for
acae0 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c  (i=0; i<MX_SMALL
acaf0 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  -1; i++){.      
acb00 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65  memsys3Merge(&me
acb10 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a  m3.aiSmall[i]);.
acb20 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65      }.    if( me
acb30 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20  m3.szMaster ){. 
acb40 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69       memsys3Unli
acb50 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29  nk(mem3.iMaster)
acb60 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33  ;.      if( mem3
acb70 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63  .szMaster>=nBloc
acb80 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  k ){.        ret
acb90 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d  urn memsys3FromM
acba0 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20  aster(nBlock);. 
acbb0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
acbc0 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f  ..  /* If none o
acbd0 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f 72 6b  f the above work
acbe0 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61 69 6c  ed, then we fail
acbf0 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b  . */.  return 0;
acc00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  .}../*.** Free a
acc10 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65  n outstanding me
acc20 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  mory allocation.
acc30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
acc40 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61  tion assumes tha
acc50 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20  t the necessary 
acc60 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c  mutexes, if any,
acc70 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20   are.** already 
acc80 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c  held by the call
acc90 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66  er. Hence "Unsaf
acca0 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  e"..*/.static vo
accb0 69 64 20 6d 65 6d 73 79 73 33 46 72 65 65 55 6e  id memsys3FreeUn
accc0 73 61 66 65 28 76 6f 69 64 20 2a 70 4f 6c 64 29  safe(void *pOld)
accd0 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 70  {.  Mem3Block *p
acce0 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 70   = (Mem3Block*)p
accf0 4f 6c 64 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  Old;.  int i;.  
acd00 75 33 32 20 73 69 7a 65 2c 20 78 3b 0a 20 20 61  u32 size, x;.  a
acd10 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
acd20 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d  utex_held(mem3.m
acd30 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
acd40 74 28 20 70 3e 6d 65 6d 33 2e 61 50 6f 6f 6c 20  t( p>mem3.aPool 
acd50 26 26 20 70 3c 26 6d 65 6d 33 2e 61 50 6f 6f 6c  && p<&mem3.aPool
acd60 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 20 29 3b 0a  [mem3.nPool] );.
acd70 20 20 69 20 3d 20 70 20 2d 20 6d 65 6d 33 2e 61    i = p - mem3.a
acd80 50 6f 6f 6c 3b 0a 20 20 61 73 73 65 72 74 28 20  Pool;.  assert( 
acd90 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d  (mem3.aPool[i-1]
acda0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 29  .u.hdr.size4x&1)
acdb0 3d 3d 31 20 29 3b 0a 20 20 73 69 7a 65 20 3d 20  ==1 );.  size = 
acdc0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e  mem3.aPool[i-1].
acdd0 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a  u.hdr.size4x/4;.
acde0 20 20 61 73 73 65 72 74 28 20 69 2b 73 69 7a 65    assert( i+size
acdf0 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 2b 31 20 29  <=mem3.nPool+1 )
ace00 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ;.  mem3.aPool[i
ace10 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
ace20 20 26 3d 20 7e 31 3b 0a 20 20 6d 65 6d 33 2e 61   &= ~1;.  mem3.a
ace30 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75  Pool[i+size-1].u
ace40 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20  .hdr.prevSize = 
ace50 73 69 7a 65 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f  size;.  mem3.aPo
ace60 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68  ol[i+size-1].u.h
ace70 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 32 3b  dr.size4x &= ~2;
ace80 0a 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 69  .  memsys3Link(i
ace90 29 3b 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20  );..  /* Try to 
acea0 65 78 70 61 6e 64 20 74 68 65 20 6d 61 73 74 65  expand the maste
aceb0 72 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 6c  r using the newl
acec0 79 20 66 72 65 65 64 20 63 68 75 6e 6b 20 2a 2f  y freed chunk */
aced0 0a 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d 61 73  .  if( mem3.iMas
acee0 74 65 72 20 29 7b 0a 20 20 20 20 77 68 69 6c 65  ter ){.    while
acef0 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65  ( (mem3.aPool[me
acf00 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  m3.iMaster-1].u.
acf10 68 64 72 2e 73 69 7a 65 34 78 26 32 29 3d 3d 30  hdr.size4x&2)==0
acf20 20 29 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d   ){.      size =
acf30 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
acf40 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  .iMaster-1].u.hd
acf50 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20  r.prevSize;.    
acf60 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2d    mem3.iMaster -
acf70 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65  = size;.      me
acf80 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2b 3d 20 73  m3.szMaster += s
acf90 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79  ize;.      memsy
acfa0 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d  s3Unlink(mem3.iM
acfb0 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 78 20  aster);.      x 
acfc0 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d  = mem3.aPool[mem
acfd0 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68  3.iMaster-1].u.h
acfe0 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20  dr.size4x & 2;. 
acff0 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b       mem3.aPool[
ad000 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e  mem3.iMaster-1].
ad010 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d  u.hdr.size4x = m
ad020 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c  em3.szMaster*4 |
ad030 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61   x;.      mem3.a
ad040 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
ad050 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d  r+mem3.szMaster-
ad060 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
ad070 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  e = mem3.szMaste
ad080 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 78 20 3d  r;.    }.    x =
ad090 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
ad0a0 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  .iMaster-1].u.hd
ad0b0 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 20  r.size4x & 2;.  
ad0c0 20 20 77 68 69 6c 65 28 20 28 6d 65 6d 33 2e 61    while( (mem3.a
ad0d0 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
ad0e0 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d  r+mem3.szMaster-
ad0f0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26  1].u.hdr.size4x&
ad100 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d  1)==0 ){.      m
ad110 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d  emsys3Unlink(mem
ad120 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73  3.iMaster+mem3.s
ad130 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  zMaster);.      
ad140 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2b 3d  mem3.szMaster +=
ad150 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
ad160 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a  .iMaster+mem3.sz
ad170 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
ad180 73 69 7a 65 34 78 2f 34 3b 0a 20 20 20 20 20 20  size4x/4;.      
ad190 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e  mem3.aPool[mem3.
ad1a0 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72  iMaster-1].u.hdr
ad1b0 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73  .size4x = mem3.s
ad1c0 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20  zMaster*4 | x;. 
ad1d0 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b       mem3.aPool[
ad1e0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d  mem3.iMaster+mem
ad1f0 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  3.szMaster-1].u.
ad200 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d  hdr.prevSize = m
ad210 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20  em3.szMaster;.  
ad220 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
ad230 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65   Return the size
ad240 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69   of an outstandi
ad250 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69  ng allocation, i
ad260 6e 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a  n bytes.  The.**
ad270 20 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20 6f   size returned o
ad280 6d 69 74 73 20 74 68 65 20 38 2d 62 79 74 65 20  mits the 8-byte 
ad290 68 65 61 64 65 72 20 6f 76 65 72 68 65 61 64 2e  header overhead.
ad2a0 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77    This only.** w
ad2b0 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 20  orks for chunks 
ad2c0 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74  that are current
ad2d0 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a  ly checked out..
ad2e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65  */.static int me
ad2f0 6d 73 79 73 33 53 69 7a 65 28 76 6f 69 64 20 2a  msys3Size(void *
ad300 70 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20  p){.  Mem3Block 
ad310 2a 70 42 6c 6f 63 6b 3b 0a 20 20 69 66 28 20 70  *pBlock;.  if( p
ad320 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
ad330 20 20 70 42 6c 6f 63 6b 20 3d 20 28 4d 65 6d 33    pBlock = (Mem3
ad340 42 6c 6f 63 6b 2a 29 70 3b 0a 20 20 61 73 73 65  Block*)p;.  asse
ad350 72 74 28 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e  rt( (pBlock[-1].
ad360 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 29 21  u.hdr.size4x&1)!
ad370 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  =0 );.  return (
ad380 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72  pBlock[-1].u.hdr
ad390 2e 73 69 7a 65 34 78 26 7e 33 29 2a 32 20 2d 20  .size4x&~3)*2 - 
ad3a0 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e  4;.}../*.** Roun
ad3b0 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 73  d up a request s
ad3c0 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ize to the next 
ad3d0 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e  valid allocation
ad3e0 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   size..*/.static
ad3f0 20 69 6e 74 20 6d 65 6d 73 79 73 33 52 6f 75 6e   int memsys3Roun
ad400 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 69 66  dup(int n){.  if
ad410 28 20 6e 3c 3d 31 32 20 29 7b 0a 20 20 20 20 72  ( n<=12 ){.    r
ad420 65 74 75 72 6e 20 31 32 3b 0a 20 20 7d 65 6c 73  eturn 12;.  }els
ad430 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 28  e{.    return ((
ad440 6e 2b 31 31 29 26 7e 37 29 20 2d 20 34 3b 0a 20  n+11)&~7) - 4;. 
ad450 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f   }.}../*.** Allo
ad460 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66 20 6d  cate nBytes of m
ad470 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  emory..*/.static
ad480 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61   void *memsys3Ma
ad490 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 29  lloc(int nBytes)
ad4a0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  {.  sqlite3_int6
ad4b0 34 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  4 *p;.  assert( 
ad4c0 6e 42 79 74 65 73 3e 30 20 29 3b 20 20 20 20 20  nBytes>0 );     
ad4d0 20 20 20 20 20 2f 2a 20 6d 61 6c 6c 6f 63 2e 63       /* malloc.c
ad4e0 20 66 69 6c 74 65 72 73 20 6f 75 74 20 30 20 62   filters out 0 b
ad4f0 79 74 65 20 72 65 71 75 65 73 74 73 20 2a 2f 0a  yte requests */.
ad500 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29    memsys3Enter()
ad510 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d  ;.  p = memsys3M
ad520 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74  allocUnsafe(nByt
ad530 65 73 29 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 65  es);.  memsys3Le
ad540 61 76 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20  ave();.  return 
ad550 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a  (void*)p; .}../*
ad560 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 2e  .** Free memory.
ad570 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
ad580 6d 65 6d 73 79 73 33 46 72 65 65 28 76 6f 69 64  memsys3Free(void
ad590 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73   *pPrior){.  ass
ad5a0 65 72 74 28 20 70 50 72 69 6f 72 20 29 3b 0a 20  ert( pPrior );. 
ad5b0 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b   memsys3Enter();
ad5c0 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 65 55 6e  .  memsys3FreeUn
ad5d0 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  safe(pPrior);.  
ad5e0 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a  memsys3Leave();.
ad5f0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
ad600 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65  the size of an e
ad610 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61  xisting memory a
ad620 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61  llocation.*/.sta
ad630 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73  tic void *memsys
ad640 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70  3Realloc(void *p
ad650 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65  Prior, int nByte
ad660 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a  s){.  int nOld;.
ad670 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28    void *p;.  if(
ad680 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20   pPrior==0 ){.  
ad690 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
ad6a0 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b  _malloc(nBytes);
ad6b0 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 65  .  }.  if( nByte
ad6c0 73 3c 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  s<=0 ){.    sqli
ad6d0 74 65 33 5f 66 72 65 65 28 70 50 72 69 6f 72 29  te3_free(pPrior)
ad6e0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
ad6f0 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d    }.  nOld = mem
ad700 73 79 73 33 53 69 7a 65 28 70 50 72 69 6f 72 29  sys3Size(pPrior)
ad710 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d  ;.  if( nBytes<=
ad720 6e 4f 6c 64 20 26 26 20 6e 42 79 74 65 73 3e 3d  nOld && nBytes>=
ad730 6e 4f 6c 64 2d 31 32 38 20 29 7b 0a 20 20 20 20  nOld-128 ){.    
ad740 72 65 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20  return pPrior;. 
ad750 20 7d 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65   }.  memsys3Ente
ad760 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79  r();.  p = memsy
ad770 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e  s3MallocUnsafe(n
ad780 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 70 20  Bytes);.  if( p 
ad790 29 7b 0a 20 20 20 20 69 66 28 20 6e 4f 6c 64 3c  ){.    if( nOld<
ad7a0 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20 20 20  nBytes ){.      
ad7b0 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72  memcpy(p, pPrior
ad7c0 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d 65 6c  , nOld);.    }el
ad7d0 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  se{.      memcpy
ad7e0 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 42 79 74  (p, pPrior, nByt
ad7f0 65 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d  es);.    }.    m
ad800 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66 65  emsys3FreeUnsafe
ad810 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20  (pPrior);.  }.  
ad820 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a  memsys3Leave();.
ad830 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
ad840 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
ad850 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a  this module..*/.
ad860 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79  static int memsy
ad870 73 33 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74  s3Init(void *Not
ad880 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Used){.  UNUSED_
ad890 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
ad8a0 64 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74  d);.  if( !sqlit
ad8b0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
ad8c0 48 65 61 70 20 29 7b 0a 20 20 20 20 72 65 74 75  Heap ){.    retu
ad8d0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
ad8e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 6f 72 65  .  }..  /* Store
ad8f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
ad900 65 20 6d 65 6d 6f 72 79 20 62 6c 6f 63 6b 20 69  e memory block i
ad910 6e 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75  n global structu
ad920 72 65 20 6d 65 6d 33 2e 20 2a 2f 0a 20 20 61 73  re mem3. */.  as
ad930 73 65 72 74 28 20 73 69 7a 65 6f 66 28 4d 65 6d  sert( sizeof(Mem
ad940 33 42 6c 6f 63 6b 29 3d 3d 38 20 29 3b 0a 20 20  3Block)==8 );.  
ad950 6d 65 6d 33 2e 61 50 6f 6f 6c 20 3d 20 28 4d 65  mem3.aPool = (Me
ad960 6d 33 42 6c 6f 63 6b 20 2a 29 73 71 6c 69 74 65  m3Block *)sqlite
ad970 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48  3GlobalConfig.pH
ad980 65 61 70 3b 0a 20 20 6d 65 6d 33 2e 6e 50 6f 6f  eap;.  mem3.nPoo
ad990 6c 20 3d 20 28 73 71 6c 69 74 65 33 47 6c 6f 62  l = (sqlite3Glob
ad9a0 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 20 2f  alConfig.nHeap /
ad9b0 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63   sizeof(Mem3Bloc
ad9c0 6b 29 29 20 2d 20 32 3b 0a 0a 20 20 2f 2a 20 49  k)) - 2;..  /* I
ad9d0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 61  nitialize the ma
ad9e0 73 74 65 72 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20  ster block. */. 
ad9f0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d   mem3.szMaster =
ada00 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d   mem3.nPool;.  m
ada10 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d  em3.mnMaster = m
ada20 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20  em3.szMaster;.  
ada30 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 31  mem3.iMaster = 1
ada40 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 30  ;.  mem3.aPool[0
ada50 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d  ].u.hdr.size4x =
ada60 20 28 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3c   (mem3.szMaster<
ada70 3c 32 29 20 2b 20 32 3b 0a 20 20 6d 65 6d 33 2e  <2) + 2;.  mem3.
ada80 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c  aPool[mem3.nPool
ada90 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65  ].u.hdr.prevSize
adaa0 20 3d 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20   = mem3.nPool;. 
adab0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
adac0 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 73 69  .nPool].u.hdr.si
adad0 7a 65 34 78 20 3d 20 31 3b 0a 0a 20 20 72 65 74  ze4x = 1;..  ret
adae0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
adaf0 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61  ../*.** Deinitia
adb00 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65  lize this module
adb10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
adb20 20 6d 65 6d 73 79 73 33 53 68 75 74 64 6f 77 6e   memsys3Shutdown
adb30 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b  (void *NotUsed){
adb40 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
adb50 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
adb60 6d 65 6d 33 2e 6d 75 74 65 78 20 3d 20 30 3b 0a  mem3.mutex = 0;.
adb70 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 0a 0a 2f    return;.}..../
adb80 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69  *.** Open the fi
adb90 6c 65 20 69 6e 64 69 63 61 74 65 64 20 61 6e 64  le indicated and
adba0 20 77 72 69 74 65 20 61 20 6c 6f 67 20 6f 66 20   write a log of 
adbb0 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f  all unfreed memo
adbc0 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  ry .** allocatio
adbd0 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67  ns into that log
adbe0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
adbf0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
adc00 4d 65 6d 73 79 73 33 44 75 6d 70 28 63 6f 6e 73  Memsys3Dump(cons
adc10 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
adc20 65 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  e){.#ifdef SQLIT
adc30 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 2a  E_DEBUG.  FILE *
adc40 6f 75 74 3b 0a 20 20 75 33 32 20 69 2c 20 6a 3b  out;.  u32 i, j;
adc50 0a 20 20 75 33 32 20 73 69 7a 65 3b 0a 20 20 69  .  u32 size;.  i
adc60 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20  f( zFilename==0 
adc70 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d  || zFilename[0]=
adc80 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74 20 3d 20  =0 ){.    out = 
adc90 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 7b  stdout;.  }else{
adca0 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e  .    out = fopen
adcb0 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29  (zFilename, "w")
adcc0 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 3d 3d 30  ;.    if( out==0
adcd0 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74   ){.      fprint
adce0 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 55 6e  f(stderr, "** Un
adcf0 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 20 6d  able to output m
add00 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 74 70  emory debug outp
add10 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22  ut log: %s **\n"
add20 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
add30 20 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d          zFilenam
add40 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  e);.      return
add50 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65  ;.    }.  }.  me
add60 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20  msys3Enter();.  
add70 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 43 48  fprintf(out, "CH
add80 55 4e 4b 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72  UNKS:\n");.  for
add90 28 69 3d 31 3b 20 69 3c 3d 6d 65 6d 33 2e 6e 50  (i=1; i<=mem3.nP
adda0 6f 6f 6c 3b 20 69 2b 3d 73 69 7a 65 2f 34 29 7b  ool; i+=size/4){
addb0 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33  .    size = mem3
addc0 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64  .aPool[i-1].u.hd
addd0 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20 20 69 66  r.size4x;.    if
adde0 28 20 73 69 7a 65 2f 34 3c 3d 31 20 29 7b 0a 20  ( size/4<=1 ){. 
addf0 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
ade00 2c 20 22 25 70 20 73 69 7a 65 20 65 72 72 6f 72  , "%p size error
ade10 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c  \n", &mem3.aPool
ade20 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65  [i]);.      asse
ade30 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62  rt( 0 );.      b
ade40 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
ade50 69 66 28 20 28 73 69 7a 65 26 31 29 3d 3d 30 20  if( (size&1)==0 
ade60 26 26 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b  && mem3.aPool[i+
ade70 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e  size/4-1].u.hdr.
ade80 70 72 65 76 53 69 7a 65 21 3d 73 69 7a 65 2f 34  prevSize!=size/4
ade90 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74   ){.      fprint
adea0 66 28 6f 75 74 2c 20 22 25 70 20 74 61 69 6c 20  f(out, "%p tail 
adeb0 73 69 7a 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61  size does not ma
adec0 74 63 68 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50  tch\n", &mem3.aP
aded0 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61  ool[i]);.      a
adee0 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20  ssert( 0 );.    
adef0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
adf00 20 20 20 69 66 28 20 28 28 6d 65 6d 33 2e 61 50     if( ((mem3.aP
adf10 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e  ool[i+size/4-1].
adf20 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 29 3e  u.hdr.size4x&2)>
adf30 3e 31 29 21 3d 28 73 69 7a 65 26 31 29 20 29 7b  >1)!=(size&1) ){
adf40 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f  .      fprintf(o
adf50 75 74 2c 20 22 25 70 20 74 61 69 6c 20 63 68 65  ut, "%p tail che
adf60 63 6b 6f 75 74 20 62 69 74 20 69 73 20 69 6e 63  ckout bit is inc
adf70 6f 72 72 65 63 74 5c 6e 22 2c 20 26 6d 65 6d 33  orrect\n", &mem3
adf80 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20  .aPool[i]);.    
adf90 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20    assert( 0 );. 
adfa0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
adfb0 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 26 31  }.    if( size&1
adfc0 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74   ){.      fprint
adfd0 66 28 6f 75 74 2c 20 22 25 70 20 25 36 64 20 62  f(out, "%p %6d b
adfe0 79 74 65 73 20 63 68 65 63 6b 65 64 20 6f 75 74  ytes checked out
adff0 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c  \n", &mem3.aPool
ae000 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a 38 2d  [i], (size/4)*8-
ae010 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  8);.    }else{. 
ae020 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
ae030 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 73 20  , "%p %6d bytes 
ae040 66 72 65 65 25 73 5c 6e 22 2c 20 26 6d 65 6d 33  free%s\n", &mem3
ae050 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65  .aPool[i], (size
ae060 2f 34 29 2a 38 2d 38 2c 0a 20 20 20 20 20 20 20  /4)*8-8,.       
ae070 20 20 20 20 20 20 20 20 20 20 20 69 3d 3d 6d 65             i==me
ae080 6d 33 2e 69 4d 61 73 74 65 72 20 3f 20 22 20 2a  m3.iMaster ? " *
ae090 2a 6d 61 73 74 65 72 2a 2a 22 20 3a 20 22 22 29  *master**" : "")
ae0a0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f  ;.    }.  }.  fo
ae0b0 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c  r(i=0; i<MX_SMAL
ae0c0 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  L-1; i++){.    i
ae0d0 66 28 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b  f( mem3.aiSmall[
ae0e0 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  i]==0 ) continue
ae0f0 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  ;.    fprintf(ou
ae100 74 2c 20 22 73 6d 61 6c 6c 28 25 32 64 29 3a 22  t, "small(%2d):"
ae110 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a 20  , i);.    for(j 
ae120 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69  = mem3.aiSmall[i
ae130 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61  ]; j>0; j=mem3.a
ae140 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e  Pool[j].u.list.n
ae150 65 78 74 29 7b 0a 20 20 20 20 20 20 66 70 72 69  ext){.      fpri
ae160 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28 25 64  ntf(out, " %p(%d
ae170 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  )", &mem3.aPool[
ae180 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  j],.            
ae190 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d    (mem3.aPool[j-
ae1a0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f  1].u.hdr.size4x/
ae1b0 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 0a 20  4)*8-8);.    }. 
ae1c0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
ae1d0 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 20 66 6f  "\n"); .  }.  fo
ae1e0 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 53 48 3b  r(i=0; i<N_HASH;
ae1f0 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d   i++){.    if( m
ae200 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 3d 3d 30  em3.aiHash[i]==0
ae210 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
ae220 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 68   fprintf(out, "h
ae230 61 73 68 28 25 32 64 29 3a 22 2c 20 69 29 3b 0a  ash(%2d):", i);.
ae240 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 6d 33      for(j = mem3
ae250 2e 61 69 48 61 73 68 5b 69 5d 3b 20 6a 3e 30 3b  .aiHash[i]; j>0;
ae260 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d   j=mem3.aPool[j]
ae270 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20  .u.list.next){. 
ae280 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
ae290 2c 20 22 20 25 70 28 25 64 29 22 2c 20 26 6d 65  , " %p(%d)", &me
ae2a0 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20  m3.aPool[j],.   
ae2b0 20 20 20 20 20 20 20 20 20 20 20 28 6d 65 6d 33             (mem3
ae2c0 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64  .aPool[j-1].u.hd
ae2d0 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 2d 38 29  r.size4x/4)*8-8)
ae2e0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 70 72 69  ;.    }.    fpri
ae2f0 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 20  ntf(out, "\n"); 
ae300 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f  .  }.  fprintf(o
ae310 75 74 2c 20 22 6d 61 73 74 65 72 3d 25 64 5c 6e  ut, "master=%d\n
ae320 22 2c 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29  ", mem3.iMaster)
ae330 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  ;.  fprintf(out,
ae340 20 22 6e 6f 77 55 73 65 64 3d 25 64 5c 6e 22 2c   "nowUsed=%d\n",
ae350 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20   mem3.nPool*8 - 
ae360 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 38 29  mem3.szMaster*8)
ae370 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  ;.  fprintf(out,
ae380 20 22 6d 78 55 73 65 64 3d 25 64 5c 6e 22 2c 20   "mxUsed=%d\n", 
ae390 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d  mem3.nPool*8 - m
ae3a0 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 2a 38 29 3b  em3.mnMaster*8);
ae3b0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
ae3c0 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65  _leave(mem3.mute
ae3d0 78 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73  x);.  if( out==s
ae3e0 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c  tdout ){.    ffl
ae3f0 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d  ush(stdout);.  }
ae400 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65  else{.    fclose
ae410 28 6f 75 74 29 3b 0a 20 20 7d 0a 23 65 6c 73 65  (out);.  }.#else
ae420 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
ae430 54 45 52 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  TER(zFilename);.
ae440 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
ae450 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
ae460 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65  the only routine
ae470 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69   in this file wi
ae480 74 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a 20  th external .** 
ae490 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50  linkage..**.** P
ae4a0 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d  opulate the low-
ae4b0 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  level memory all
ae4c0 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  ocation function
ae4d0 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20   pointers in.** 
ae4e0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
ae4f0 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74  fig.m with point
ae500 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74 69  ers to the routi
ae510 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65  nes in this file
ae520 2e 20 54 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e  . The.** argumen
ae530 74 73 20 73 70 65 63 69 66 79 20 74 68 65 20 62  ts specify the b
ae540 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74  lock of memory t
ae550 6f 20 6d 61 6e 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  o manage..**.** 
ae560 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
ae570 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 62 79 20 73  only called by s
ae580 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 2c  qlite3_config(),
ae590 20 61 6e 64 20 74 68 65 72 65 66 6f 72 65 0a 2a   and therefore.*
ae5a0 2a 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65  * is not require
ae5b0 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 61  d to be threadsa
ae5c0 66 65 20 28 69 74 20 69 73 20 6e 6f 74 29 2e 0a  fe (it is not)..
ae5d0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
ae5e0 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  E const sqlite3_
ae5f0 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c  mem_methods *sql
ae600 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73  ite3MemGetMemsys
ae610 33 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  3(void){.  stati
ae620 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
ae630 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 65 6d 70  mem_methods memp
ae640 6f 6f 6c 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20  oolMethods = {. 
ae650 20 20 20 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f      memsys3Mallo
ae660 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 46  c,.     memsys3F
ae670 72 65 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73  ree,.     memsys
ae680 33 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d  3Realloc,.     m
ae690 65 6d 73 79 73 33 53 69 7a 65 2c 0a 20 20 20 20  emsys3Size,.    
ae6a0 20 6d 65 6d 73 79 73 33 52 6f 75 6e 64 75 70 2c   memsys3Roundup,
ae6b0 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 49 6e 69  .     memsys3Ini
ae6c0 74 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 53  t,.     memsys3S
ae6d0 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a  hutdown,.     0.
ae6e0 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 26 6d    };.  return &m
ae6f0 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 3b 0a 7d  empoolMethods;.}
ae700 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
ae710 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
ae720 33 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  3 */../*********
ae730 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d  ***** End of mem
ae740 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  3.c ************
ae750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ae760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ae770 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
ae780 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
ae790 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   mem5.c ********
ae7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ae7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ae7c0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
ae7d0 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a 0a 2a   October 14.**.*
ae7e0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
ae7f0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
ae800 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
ae810 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
ae820 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
ae830 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
ae840 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
ae850 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
ae860 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
ae870 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
ae880 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
ae890 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
ae8a0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
ae8b0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
ae8c0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
ae8d0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
ae8e0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
ae8f0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
ae900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ae910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ae920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ae930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
ae940 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
ae950 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73   the C functions
ae960 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
ae970 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f  a memory.** allo
ae980 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
ae990 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c 69   for use by SQLi
ae9a0 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  te. .**.** This 
ae9b0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d  version of the m
ae9c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
ae9d0 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69 74 73   subsystem omits
ae9e0 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66 20 6d   all.** use of m
ae9f0 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 61 70 70  alloc(). The app
aea00 6c 69 63 61 74 69 6f 6e 20 67 69 76 65 73 20 53  lication gives S
aea10 51 4c 69 74 65 20 61 20 62 6c 6f 63 6b 20 6f 66  QLite a block of
aea20 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72   memory.** befor
aea30 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65  e calling sqlite
aea40 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66  3_initialize() f
aea50 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63 61  rom which alloca
aea60 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64  tions.** are mad
aea70 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 62  e and returned b
aea80 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20  y the xMalloc() 
aea90 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a  and xRealloc() .
aeaa0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
aeab0 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65 33  ns. Once sqlite3
aeac0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 68 61  _initialize() ha
aead0 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a  s been called,.*
aeae0 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  * the amount of 
aeaf0 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65  memory available
aeb00 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20 66 69   to SQLite is fi
aeb10 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a  xed and cannot.*
aeb20 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a  * be changed..**
aeb30 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
aeb40 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
aeb50 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
aeb60 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 0a  tem is included.
aeb70 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64 20  ** in the build 
aeb80 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f 45  only if SQLITE_E
aeb90 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69 73  NABLE_MEMSYS5 is
aeba0 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20   defined..**.** 
aebb0 54 68 69 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  This memory allo
aebc0 63 61 74 6f 72 20 75 73 65 73 20 74 68 65 20 66  cator uses the f
aebd0 6f 6c 6c 6f 77 69 6e 67 20 61 6c 67 6f 72 69 74  ollowing algorit
aebe0 68 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20  hm:.**.**   1.  
aebf0 41 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  All memory alloc
aec00 61 74 69 6f 6e 73 20 73 69 7a 65 73 20 61 72 65  ations sizes are
aec10 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 61   rounded up to a
aec20 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a   power of 2..**.
aec30 2a 2a 20 20 20 32 2e 20 20 49 66 20 74 77 6f 20  **   2.  If two 
aec40 61 64 6a 61 63 65 6e 74 20 66 72 65 65 20 62 6c  adjacent free bl
aec50 6f 63 6b 73 20 61 72 65 20 74 68 65 20 68 61 6c  ocks are the hal
aec60 76 65 73 20 6f 66 20 61 20 6c 61 72 67 65 72 20  ves of a larger 
aec70 62 6c 6f 63 6b 2c 0a 2a 2a 20 20 20 20 20 20 20  block,.**       
aec80 74 68 65 6e 20 74 68 65 20 74 77 6f 20 62 6c 6f  then the two blo
aec90 63 6b 73 20 61 72 65 20 63 6f 61 6c 65 73 65 64  cks are coalesed
aeca0 20 69 6e 74 6f 20 74 68 65 20 73 69 6e 67 6c 65   into the single
aecb0 20 6c 61 72 67 65 72 20 62 6c 6f 63 6b 2e 0a 2a   larger block..*
aecc0 2a 0a 2a 2a 20 20 20 33 2e 20 20 4e 65 77 20 6d  *.**   3.  New m
aecd0 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74  emory is allocat
aece0 65 64 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73  ed from the firs
aecf0 74 20 61 76 61 69 6c 61 62 6c 65 20 66 72 65 65  t available free
aed00 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68   block..**.** Th
aed10 69 73 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20  is algorithm is 
aed20 64 65 73 63 72 69 62 65 64 20 69 6e 3a 20 4a 2e  described in: J.
aed30 20 4d 2e 20 52 6f 62 73 6f 6e 2e 20 22 42 6f 75   M. Robson. "Bou
aed40 6e 64 73 20 66 6f 72 20 53 6f 6d 65 20 46 75 6e  nds for Some Fun
aed50 63 74 69 6f 6e 73 0a 2a 2a 20 43 6f 6e 63 65 72  ctions.** Concer
aed60 6e 69 6e 67 20 44 79 6e 61 6d 69 63 20 53 74 6f  ning Dynamic Sto
aed70 72 61 67 65 20 41 6c 6c 6f 63 61 74 69 6f 6e 22  rage Allocation"
aed80 2e 20 4a 6f 75 72 6e 61 6c 20 6f 66 20 74 68 65  . Journal of the
aed90 20 41 73 73 6f 63 69 61 74 69 6f 6e 20 66 6f 72   Association for
aeda0 0a 2a 2a 20 43 6f 6d 70 75 74 69 6e 67 20 4d 61  .** Computing Ma
aedb0 63 68 69 6e 65 72 79 2c 20 56 6f 6c 75 6d 65 20  chinery, Volume 
aedc0 32 31 2c 20 4e 75 6d 62 65 72 20 38 2c 20 4a 75  21, Number 8, Ju
aedd0 6c 79 20 31 39 37 34 2c 20 70 61 67 65 73 20 34  ly 1974, pages 4
aede0 39 31 2d 34 39 39 2e 0a 2a 2a 20 0a 2a 2a 20 4c  91-499..** .** L
aedf0 65 74 20 6e 20 62 65 20 74 68 65 20 73 69 7a 65  et n be the size
aee00 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20   of the largest 
aee10 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 69 76 69 64  allocation divid
aee20 65 64 20 62 79 20 74 68 65 20 6d 69 6e 69 6d 75  ed by the minimu
aee30 6d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  m.** allocation 
aee40 73 69 7a 65 20 28 61 66 74 65 72 20 72 6f 75 6e  size (after roun
aee50 64 69 6e 67 20 61 6c 6c 20 73 69 7a 65 73 20 75  ding all sizes u
aee60 70 20 74 6f 20 61 20 70 6f 77 65 72 20 6f 66 20  p to a power of 
aee70 32 2e 29 20 20 4c 65 74 20 4d 0a 2a 2a 20 62 65  2.)  Let M.** be
aee80 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f   the maximum amo
aee90 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 65 76  unt of memory ev
aeea0 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61  er outstanding a
aeeb0 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 4c 65 74  t one time.  Let
aeec0 0a 2a 2a 20 4e 20 62 65 20 74 68 65 20 74 6f 74  .** N be the tot
aeed0 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d  al amount of mem
aeee0 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66 6f  ory available fo
aeef0 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 52  r allocation.  R
aef00 6f 62 73 6f 6e 0a 2a 2a 20 70 72 6f 76 65 64 20  obson.** proved 
aef10 74 68 61 74 20 74 68 69 73 20 6d 65 6d 6f 72 79  that this memory
aef20 20 61 6c 6c 6f 63 61 74 6f 72 20 77 69 6c 6c 20   allocator will 
aef30 6e 65 76 65 72 20 62 72 65 61 6b 64 6f 77 6e 20  never breakdown 
aef40 64 75 65 20 74 6f 20 0a 2a 2a 20 66 72 61 67 6d  due to .** fragm
aef50 65 6e 74 61 74 69 6f 6e 20 61 73 20 6c 6f 6e 67  entation as long
aef60 20 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   as the followin
aef70 67 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 6f 6c  g constraint hol
aef80 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 4e  ds:.**.**      N
aef90 20 3e 3d 20 20 4d 2a 28 31 20 2b 20 6c 6f 67 32   >=  M*(1 + log2
aefa0 28 6e 29 2f 32 29 20 2d 20 6e 20 2b 20 31 0a 2a  (n)/2) - n + 1.*
aefb0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
aefc0 5f 73 74 61 74 75 73 28 29 20 6c 6f 67 69 63 20  _status() logic 
aefd0 74 72 61 63 6b 73 20 74 68 65 20 6d 61 78 69 6d  tracks the maxim
aefe0 75 6d 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 61  um values of n a
aeff0 6e 64 20 4d 20 73 6f 0a 2a 2a 20 74 68 61 74 20  nd M so.** that 
af000 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63  an application c
af010 61 6e 2c 20 61 74 20 61 6e 79 20 74 69 6d 65 2c  an, at any time,
af020 20 76 65 72 69 66 79 20 74 68 69 73 20 63 6f 6e   verify this con
af030 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a  straint..*/../*.
af040 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20  ** This version 
af050 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  of the memory al
af060 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64 20  locator is used 
af070 6f 6e 6c 79 20 77 68 65 6e 20 0a 2a 2a 20 53 51  only when .** SQ
af080 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
af090 59 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a  YS5 is defined..
af0a0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
af0b0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a  _ENABLE_MEMSYS5.
af0c0 0a 2f 2a 0a 2a 2a 20 41 20 6d 69 6e 69 6d 75 6d  ./*.** A minimum
af0d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61   allocation is a
af0e0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
af0f0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
af100 63 74 75 72 65 2e 0a 2a 2a 20 4c 61 72 67 65 72  cture..** Larger
af110 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65   allocations are
af120 20 61 6e 20 61 72 72 61 79 20 6f 66 20 74 68 65   an array of the
af130 73 65 20 73 74 72 75 63 74 75 72 65 73 20 77 68  se structures wh
af140 65 72 65 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20  ere the.** size 
af150 6f 66 20 74 68 65 20 61 72 72 61 79 20 69 73 20  of the array is 
af160 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2a  a power of 2..**
af170 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20  .** The size of 
af180 74 68 69 73 20 6f 62 6a 65 63 74 20 6d 75 73 74  this object must
af190 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74   be a power of t
af1a0 77 6f 2e 20 20 54 68 61 74 20 66 61 63 74 20 69  wo.  That fact i
af1b0 73 0a 2a 2a 20 76 65 72 69 66 69 65 64 20 69 6e  s.** verified in
af1c0 20 6d 65 6d 73 79 73 35 49 6e 69 74 28 29 2e 0a   memsys5Init()..
af1d0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
af1e0 74 20 4d 65 6d 35 4c 69 6e 6b 20 4d 65 6d 35 4c  t Mem5Link Mem5L
af1f0 69 6e 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d 35  ink;.struct Mem5
af200 4c 69 6e 6b 20 7b 0a 20 20 69 6e 74 20 6e 65 78  Link {.  int nex
af210 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65  t;       /* Inde
af220 78 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20 63  x of next free c
af230 68 75 6e 6b 20 2a 2f 0a 20 20 69 6e 74 20 70 72  hunk */.  int pr
af240 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  ev;       /* Ind
af250 65 78 20 6f 66 20 70 72 65 76 69 6f 75 73 20 66  ex of previous f
af260 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a  ree chunk */.};.
af270 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73  ./*.** Maximum s
af280 69 7a 65 20 6f 66 20 61 6e 79 20 61 6c 6c 6f 63  ize of any alloc
af290 61 74 69 6f 6e 20 69 73 20 28 28 31 3c 3c 4c 4f  ation is ((1<<LO
af2a0 47 4d 41 58 29 2a 6d 65 6d 35 2e 73 7a 41 74 6f  GMAX)*mem5.szAto
af2b0 6d 29 2e 20 53 69 6e 63 65 0a 2a 2a 20 6d 65 6d  m). Since.** mem
af2c0 35 2e 73 7a 41 74 6f 6d 20 69 73 20 61 6c 77 61  5.szAtom is alwa
af2d0 79 73 20 61 74 20 6c 65 61 73 74 20 38 20 61 6e  ys at least 8 an
af2e0 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  d 32-bit integer
af2f0 73 20 61 72 65 20 75 73 65 64 2c 0a 2a 2a 20 69  s are used,.** i
af300 74 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c  t is not actuall
af310 79 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65  y possible to re
af320 61 63 68 20 74 68 69 73 20 6c 69 6d 69 74 2e 0a  ach this limit..
af330 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 4f 47 4d 41  */.#define LOGMA
af340 58 20 33 30 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b  X 30../*.** Mask
af350 73 20 75 73 65 64 20 66 6f 72 20 6d 65 6d 35 2e  s used for mem5.
af360 61 43 74 72 6c 5b 5d 20 65 6c 65 6d 65 6e 74 73  aCtrl[] elements
af370 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 52  ..*/.#define CTR
af380 4c 5f 4c 4f 47 53 49 5a 45 20 20 30 78 31 66 20  L_LOGSIZE  0x1f 
af390 20 20 20 2f 2a 20 4c 6f 67 32 20 53 69 7a 65 20     /* Log2 Size 
af3a0 6f 66 20 74 68 69 73 20 62 6c 6f 63 6b 20 2a 2f  of this block */
af3b0 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 46 52  .#define CTRL_FR
af3c0 45 45 20 20 20 20 20 30 78 32 30 20 20 20 20 2f  EE     0x20    /
af3d0 2a 20 54 72 75 65 20 69 66 20 6e 6f 74 20 63 68  * True if not ch
af3e0 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 0a 2f 2a  ecked out */../*
af3f0 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 73  .** All of the s
af400 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20  tatic variables 
af410 75 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f 64  used by this mod
af420 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 65  ule are collecte
af430 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67  d.** into a sing
af440 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d  le structure nam
af450 65 64 20 22 6d 65 6d 35 22 2e 20 20 54 68 69 73  ed "mem5".  This
af460 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65 0a   is to keep the.
af470 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 61 62  ** static variab
af480 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e  les organized an
af490 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d 65  d to reduce name
af4a0 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a  space pollution.
af4b0 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f 64  ** when this mod
af4c0 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 20  ule is combined 
af4d0 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74 68  with other in th
af4e0 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a  e amalgamation..
af4f0 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45  */.static SQLITE
af500 5f 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d 35  _WSD struct Mem5
af510 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20  Global {.  /*.  
af520 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61  ** Memory availa
af530 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69  ble for allocati
af540 6f 6e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  on.  */.  int sz
af550 41 74 6f 6d 3b 20 20 20 20 20 20 2f 2a 20 53 6d  Atom;      /* Sm
af560 61 6c 6c 65 73 74 20 70 6f 73 73 69 62 6c 65 20  allest possible 
af570 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 62 79  allocation in by
af580 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 6c  tes */.  int nBl
af590 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ock;      /* Num
af5a0 62 65 72 20 6f 66 20 73 7a 41 74 6f 6d 20 73 69  ber of szAtom si
af5b0 7a 65 64 20 62 6c 6f 63 6b 73 20 69 6e 20 7a 50  zed blocks in zP
af5c0 6f 6f 6c 20 2a 2f 0a 20 20 75 38 20 2a 7a 50 6f  ool */.  u8 *zPo
af5d0 6f 6c 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d  ol;       /* Mem
af5e0 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f  ory available to
af5f0 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f   be allocated */
af600 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75  .  .  /*.  ** Mu
af610 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61  tex to control a
af620 63 63 65 73 73 20 74 6f 20 74 68 65 20 6d 65 6d  ccess to the mem
af630 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
af640 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20  ubsystem..  */. 
af650 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
af660 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  mutex;..  /*.  *
af670 2a 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 73 74  * Performance st
af680 61 74 69 73 74 69 63 73 0a 20 20 2a 2f 0a 20 20  atistics.  */.  
af690 75 36 34 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20  u64 nAlloc;     
af6a0 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
af6b0 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f 20  ber of calls to 
af6c0 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 36 34 20  malloc */.  u64 
af6d0 74 6f 74 61 6c 41 6c 6c 6f 63 3b 20 20 20 20 20  totalAlloc;     
af6e0 2f 2a 20 54 6f 74 61 6c 20 6f 66 20 61 6c 6c 20  /* Total of all 
af6f0 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 2d 20 69  malloc calls - i
af700 6e 63 6c 75 64 65 73 20 69 6e 74 65 72 6e 61 6c  ncludes internal
af710 20 66 72 61 67 20 2a 2f 0a 20 20 75 36 34 20 74   frag */.  u64 t
af720 6f 74 61 6c 45 78 63 65 73 73 3b 20 20 20 20 2f  otalExcess;    /
af730 2a 20 54 6f 74 61 6c 20 69 6e 74 65 72 6e 61 6c  * Total internal
af740 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a   fragmentation *
af750 2f 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74 4f  /.  u32 currentO
af760 75 74 3b 20 20 20 20 20 2f 2a 20 43 75 72 72 65  ut;     /* Curre
af770 6e 74 20 63 68 65 63 6b 6f 75 74 2c 20 69 6e 63  nt checkout, inc
af780 6c 75 64 69 6e 67 20 69 6e 74 65 72 6e 61 6c 20  luding internal 
af790 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f  fragmentation */
af7a0 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74 43 6f  .  u32 currentCo
af7b0 75 6e 74 3b 20 20 20 2f 2a 20 43 75 72 72 65 6e  unt;   /* Curren
af7c0 74 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 73 74  t number of dist
af7d0 69 6e 63 74 20 63 68 65 63 6b 6f 75 74 73 20 2a  inct checkouts *
af7e0 2f 0a 20 20 75 33 32 20 6d 61 78 4f 75 74 3b 20  /.  u32 maxOut; 
af7f0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
af800 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73  um instantaneous
af810 20 63 75 72 72 65 6e 74 4f 75 74 20 2a 2f 0a 20   currentOut */. 
af820 20 75 33 32 20 6d 61 78 43 6f 75 6e 74 3b 20 20   u32 maxCount;  
af830 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
af840 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75  instantaneous cu
af850 72 72 65 6e 74 43 6f 75 6e 74 20 2a 2f 0a 20 20  rrentCount */.  
af860 75 33 32 20 6d 61 78 52 65 71 75 65 73 74 3b 20  u32 maxRequest; 
af870 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 61      /* Largest a
af880 6c 6c 6f 63 61 74 69 6f 6e 20 28 65 78 63 6c 75  llocation (exclu
af890 73 69 76 65 20 6f 66 20 69 6e 74 65 72 6e 61 6c  sive of internal
af8a0 20 66 72 61 67 29 20 2a 2f 0a 20 20 0a 20 20 2f   frag) */.  .  /
af8b0 2a 0a 20 20 2a 2a 20 4c 69 73 74 73 20 6f 66 20  *.  ** Lists of 
af8c0 66 72 65 65 20 62 6c 6f 63 6b 73 2e 20 20 61 69  free blocks.  ai
af8d0 46 72 65 65 6c 69 73 74 5b 30 5d 20 69 73 20 61  Freelist[0] is a
af8e0 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 62 6c   list of free bl
af8f0 6f 63 6b 73 20 6f 66 0a 20 20 2a 2a 20 73 69 7a  ocks of.  ** siz
af900 65 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2e 20 20  e mem5.szAtom.  
af910 61 69 46 72 65 65 6c 69 73 74 5b 31 5d 20 68 6f  aiFreelist[1] ho
af920 6c 64 73 20 62 6c 6f 63 6b 73 20 6f 66 20 73 69  lds blocks of si
af930 7a 65 20 73 7a 41 74 6f 6d 2a 32 2e 0a 20 20 2a  ze szAtom*2..  *
af940 2a 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a  * and so forth..
af950 20 20 2a 2f 0a 20 20 69 6e 74 20 61 69 46 72 65    */.  int aiFre
af960 65 6c 69 73 74 5b 4c 4f 47 4d 41 58 2b 31 5d 3b  elist[LOGMAX+1];
af970 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 70 61 63  ..  /*.  ** Spac
af980 65 20 66 6f 72 20 74 72 61 63 6b 69 6e 67 20 77  e for tracking w
af990 68 69 63 68 20 62 6c 6f 63 6b 73 20 61 72 65 20  hich blocks are 
af9a0 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20  checked out and 
af9b0 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66  the size.  ** of
af9c0 20 65 61 63 68 20 62 6c 6f 63 6b 2e 20 20 4f 6e   each block.  On
af9d0 65 20 62 79 74 65 20 70 65 72 20 62 6c 6f 63 6b  e byte per block
af9e0 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 2a 61 43 74  ..  */.  u8 *aCt
af9f0 72 6c 3b 0a 0a 7d 20 6d 65 6d 35 3b 0a 0a 2f 2a  rl;..} mem5;../*
afa00 0a 2a 2a 20 41 63 63 65 73 73 20 74 68 65 20 73  .** Access the s
afa10 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 20 74  tatic variable t
afa20 68 72 6f 75 67 68 20 61 20 6d 61 63 72 6f 20 66  hrough a macro f
afa30 6f 72 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57  or SQLITE_OMIT_W
afa40 53 44 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 6d 65  SD.*/.#define me
afa50 6d 35 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74  m5 GLOBAL(struct
afa60 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c 20 6d 65 6d   Mem5Global, mem
afa70 35 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69  5)../*.** Assumi
afa80 6e 67 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 69 73  ng mem5.zPool is
afa90 20 64 69 76 69 64 65 64 20 75 70 20 69 6e 74 6f   divided up into
afaa0 20 61 6e 20 61 72 72 61 79 20 6f 66 20 4d 65 6d   an array of Mem
afab0 35 4c 69 6e 6b 0a 2a 2a 20 73 74 72 75 63 74 75  5Link.** structu
afac0 72 65 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f  res, return a po
afad0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 69 64 78  inter to the idx
afae0 2d 74 68 20 73 75 63 68 20 6c 69 6b 2e 0a 2a 2f  -th such lik..*/
afaf0 0a 23 64 65 66 69 6e 65 20 4d 45 4d 35 4c 49 4e  .#define MEM5LIN
afb00 4b 28 69 64 78 29 20 28 28 4d 65 6d 35 4c 69 6e  K(idx) ((Mem5Lin
afb10 6b 20 2a 29 28 26 6d 65 6d 35 2e 7a 50 6f 6f 6c  k *)(&mem5.zPool
afb20 5b 28 69 64 78 29 2a 6d 65 6d 35 2e 73 7a 41 74  [(idx)*mem5.szAt
afb30 6f 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c  om]))../*.** Unl
afb40 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74  ink the chunk at
afb50 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 66   mem5.aPool[i] f
afb60 72 6f 6d 20 6c 69 73 74 20 69 74 20 69 73 20 63  rom list it is c
afb70 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 2e 20  urrently.** on. 
afb80 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20 66 6f   It should be fo
afb90 75 6e 64 20 6f 6e 20 6d 65 6d 35 2e 61 69 46 72  und on mem5.aiFr
afba0 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d  eelist[iLogsize]
afbb0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
afbc0 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69   memsys5Unlink(i
afbd0 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67 73 69  nt i, int iLogsi
afbe0 7a 65 29 7b 0a 20 20 69 6e 74 20 6e 65 78 74 2c  ze){.  int next,
afbf0 20 70 72 65 76 3b 0a 20 20 61 73 73 65 72 74 28   prev;.  assert(
afc00 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e   i>=0 && i<mem5.
afc10 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65  nBlock );.  asse
afc20 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20  rt( iLogsize>=0 
afc30 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47  && iLogsize<=LOG
afc40 4d 41 58 20 29 3b 0a 20 20 61 73 73 65 72 74 28  MAX );.  assert(
afc50 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20   (mem5.aCtrl[i] 
afc60 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d  & CTRL_LOGSIZE)=
afc70 3d 69 4c 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20  =iLogsize );..  
afc80 6e 65 78 74 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28  next = MEM5LINK(
afc90 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 70 72 65 76  i)->next;.  prev
afca0 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e   = MEM5LINK(i)->
afcb0 70 72 65 76 3b 0a 20 20 69 66 28 20 70 72 65 76  prev;.  if( prev
afcc0 3c 30 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61  <0 ){.    mem5.a
afcd0 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69  iFreelist[iLogsi
afce0 7a 65 5d 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 65  ze] = next;.  }e
afcf0 6c 73 65 7b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e  lse{.    MEM5LIN
afd00 4b 28 70 72 65 76 29 2d 3e 6e 65 78 74 20 3d 20  K(prev)->next = 
afd10 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  next;.  }.  if( 
afd20 6e 65 78 74 3e 3d 30 20 29 7b 0a 20 20 20 20 4d  next>=0 ){.    M
afd30 45 4d 35 4c 49 4e 4b 28 6e 65 78 74 29 2d 3e 70  EM5LINK(next)->p
afd40 72 65 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a  rev = prev;.  }.
afd50 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68  }../*.** Link th
afd60 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 35 2e  e chunk at mem5.
afd70 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20 74 68 61 74  aPool[i] so that
afd80 20 69 73 20 6f 6e 20 74 68 65 20 69 4c 6f 67 73   is on the iLogs
afd90 69 7a 65 0a 2a 2a 20 66 72 65 65 20 6c 69 73 74  ize.** free list
afda0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
afdb0 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 6e 74   memsys5Link(int
afdc0 20 69 2c 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65   i, int iLogsize
afdd0 29 7b 0a 20 20 69 6e 74 20 78 3b 0a 20 20 61 73  ){.  int x;.  as
afde0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
afdf0 74 65 78 5f 68 65 6c 64 28 6d 65 6d 35 2e 6d 75  tex_held(mem5.mu
afe00 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
afe10 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65 6d 35  ( i>=0 && i<mem5
afe20 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73  .nBlock );.  ass
afe30 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d 30  ert( iLogsize>=0
afe40 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f   && iLogsize<=LO
afe50 47 4d 41 58 20 29 3b 0a 20 20 61 73 73 65 72 74  GMAX );.  assert
afe60 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d  ( (mem5.aCtrl[i]
afe70 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29   & CTRL_LOGSIZE)
afe80 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b 0a 0a 20  ==iLogsize );.. 
afe90 20 78 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29   x = MEM5LINK(i)
afea0 2d 3e 6e 65 78 74 20 3d 20 6d 65 6d 35 2e 61 69  ->next = mem5.ai
afeb0 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a  Freelist[iLogsiz
afec0 65 5d 3b 0a 20 20 4d 45 4d 35 4c 49 4e 4b 28 69  e];.  MEM5LINK(i
afed0 29 2d 3e 70 72 65 76 20 3d 20 2d 31 3b 0a 20 20  )->prev = -1;.  
afee0 69 66 28 20 78 3e 3d 30 20 29 7b 0a 20 20 20 20  if( x>=0 ){.    
afef0 61 73 73 65 72 74 28 20 78 3c 6d 65 6d 35 2e 6e  assert( x<mem5.n
aff00 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 4d 45 4d  Block );.    MEM
aff10 35 4c 49 4e 4b 28 78 29 2d 3e 70 72 65 76 20 3d  5LINK(x)->prev =
aff20 20 69 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61   i;.  }.  mem5.a
aff30 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69  iFreelist[iLogsi
aff40 7a 65 5d 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a  ze] = i;.}../*.*
aff50 2a 20 49 66 20 74 68 65 20 53 54 41 54 49 43 5f  * If the STATIC_
aff60 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e 6f 74  MEM mutex is not
aff70 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f   already held, o
aff80 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68  btain it now. Th
aff90 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20  e mutex.** will 
affa0 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c 64 20  already be held 
affb0 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63 6f 64  (obtained by cod
affc0 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69  e in malloc.c) i
affd0 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62  f.** sqlite3Glob
affe0 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61  alConfig.bMemSta
afff0 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74  t is true..*/.st
b0000 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
b0010 35 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a 20 20  5Enter(void){.  
b0020 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
b0030 74 65 72 28 6d 65 6d 35 2e 6d 75 74 65 78 29 3b  ter(mem5.mutex);
b0040 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  .}.static void m
b0050 65 6d 73 79 73 35 4c 65 61 76 65 28 76 6f 69 64  emsys5Leave(void
b0060 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
b0070 65 78 5f 6c 65 61 76 65 28 6d 65 6d 35 2e 6d 75  ex_leave(mem5.mu
b0080 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  tex);.}../*.** R
b0090 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f  eturn the size o
b00a0 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67  f an outstanding
b00b0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20   allocation, in 
b00c0 62 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73  bytes.  The.** s
b00d0 69 7a 65 20 72 65 74 75 72 6e 65 64 20 6f 6d 69  ize returned omi
b00e0 74 73 20 74 68 65 20 38 2d 62 79 74 65 20 68 65  ts the 8-byte he
b00f0 61 64 65 72 20 6f 76 65 72 68 65 61 64 2e 20 20  ader overhead.  
b0100 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72  This only.** wor
b0110 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 20 74 68  ks for chunks th
b0120 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79  at are currently
b0130 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f   checked out..*/
b0140 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73  .static int mems
b0150 79 73 35 53 69 7a 65 28 76 6f 69 64 20 2a 70 29  ys5Size(void *p)
b0160 7b 0a 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20  {.  int iSize = 
b0170 30 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  0;.  if( p ){.  
b0180 20 20 69 6e 74 20 69 20 3d 20 28 28 75 38 20 2a    int i = ((u8 *
b0190 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d  )p-mem5.zPool)/m
b01a0 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20 20 20 20  em5.szAtom;.    
b01b0 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
b01c0 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b  i<mem5.nBlock );
b01d0 0a 20 20 20 20 69 53 69 7a 65 20 3d 20 6d 65 6d  .    iSize = mem
b01e0 35 2e 73 7a 41 74 6f 6d 20 2a 20 28 31 20 3c 3c  5.szAtom * (1 <<
b01f0 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 26   (mem5.aCtrl[i]&
b0200 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 29 3b 0a  CTRL_LOGSIZE));.
b0210 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 53 69    }.  return iSi
b0220 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e  ze;.}../*.** Fin
b0230 64 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72  d the first entr
b0240 79 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73  y on the freelis
b0250 74 20 69 4c 6f 67 73 69 7a 65 2e 20 20 55 6e 6c  t iLogsize.  Unl
b0260 69 6e 6b 20 74 68 61 74 0a 2a 2a 20 65 6e 74 72  ink that.** entr
b0270 79 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 73  y and return its
b0280 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73 74 61 74   index. .*/.stat
b0290 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 55 6e  ic int memsys5Un
b02a0 6c 69 6e 6b 46 69 72 73 74 28 69 6e 74 20 69 4c  linkFirst(int iL
b02b0 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 69  ogsize){.  int i
b02c0 3b 0a 20 20 69 6e 74 20 69 46 69 72 73 74 3b 0a  ;.  int iFirst;.
b02d0 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73  .  assert( iLogs
b02e0 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69  ize>=0 && iLogsi
b02f0 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20  ze<=LOGMAX );.  
b0300 69 20 3d 20 69 46 69 72 73 74 20 3d 20 6d 65 6d  i = iFirst = mem
b0310 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f  5.aiFreelist[iLo
b0320 67 73 69 7a 65 5d 3b 0a 20 20 61 73 73 65 72 74  gsize];.  assert
b0330 28 20 69 46 69 72 73 74 3e 3d 30 20 29 3b 0a 20  ( iFirst>=0 );. 
b0340 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20   while( i>0 ){. 
b0350 20 20 20 69 66 28 20 69 3c 69 46 69 72 73 74 20     if( i<iFirst 
b0360 29 20 69 46 69 72 73 74 20 3d 20 69 3b 0a 20 20  ) iFirst = i;.  
b0370 20 20 69 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69    i = MEM5LINK(i
b0380 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 6d  )->next;.  }.  m
b0390 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 46 69  emsys5Unlink(iFi
b03a0 72 73 74 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a  rst, iLogsize);.
b03b0 20 20 72 65 74 75 72 6e 20 69 46 69 72 73 74 3b    return iFirst;
b03c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
b03d0 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f   a block of memo
b03e0 72 79 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e  ry of at least n
b03f0 42 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a  Bytes in size..*
b0400 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66  * Return NULL if
b0410 20 75 6e 61 62 6c 65 2e 20 20 52 65 74 75 72 6e   unable.  Return
b0420 20 4e 55 4c 4c 20 69 66 20 6e 42 79 74 65 73 3d   NULL if nBytes=
b0430 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61  =0..**.** The ca
b0440 6c 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73 20  ller guarantees 
b0450 74 68 61 74 20 6e 42 79 74 65 20 70 6f 73 69 74  that nByte posit
b0460 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ive..**.** The c
b0470 61 6c 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e  aller has obtain
b0480 65 64 20 61 20 6d 75 74 65 78 20 70 72 69 6f 72  ed a mutex prior
b0490 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69   to invoking thi
b04a0 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 73 6f 20  s.** routine so 
b04b0 74 68 65 72 65 20 69 73 20 6e 65 76 65 72 20 61  there is never a
b04c0 6e 79 20 63 68 61 6e 63 65 20 74 68 61 74 20 74  ny chance that t
b04d0 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 74 68  wo or more.** th
b04e0 72 65 61 64 73 20 63 61 6e 20 62 65 20 69 6e 20  reads can be in 
b04f0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74 20  this routine at 
b0500 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a  the same time..*
b0510 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d  /.static void *m
b0520 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61  emsys5MallocUnsa
b0530 66 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  fe(int nByte){. 
b0540 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
b0550 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20    /* Index of a 
b0560 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 5d 20 73 6c 6f  mem5.aPool[] slo
b0570 74 20 2a 2f 0a 20 20 69 6e 74 20 69 42 69 6e 3b  t */.  int iBin;
b0580 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
b0590 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 69 46 72 65   into mem5.aiFre
b05a0 65 6c 69 73 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74  elist[] */.  int
b05b0 20 69 46 75 6c 6c 53 7a 3b 20 20 20 20 20 2f 2a   iFullSz;     /*
b05c0 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74   Size of allocat
b05d0 69 6f 6e 20 72 6f 75 6e 64 65 64 20 75 70 20 74  ion rounded up t
b05e0 6f 20 70 6f 77 65 72 20 6f 66 20 32 20 2a 2f 0a  o power of 2 */.
b05f0 20 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 3b 20    int iLogsize; 
b0600 20 20 20 2f 2a 20 4c 6f 67 32 20 6f 66 20 69 46     /* Log2 of iF
b0610 75 6c 6c 53 7a 2f 50 4f 57 32 5f 4d 49 4e 20 2a  ullSz/POW2_MIN *
b0620 2f 0a 0a 20 20 2f 2a 20 6e 42 79 74 65 20 6d 75  /..  /* nByte mu
b0630 73 74 20 62 65 20 61 20 70 6f 73 69 74 69 76 65  st be a positive
b0640 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e 42   */.  assert( nB
b0650 79 74 65 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 4b  yte>0 );..  /* K
b0660 65 65 70 20 74 72 61 63 6b 20 6f 66 20 74 68 65  eep track of the
b0670 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 63 61 74   maximum allocat
b0680 69 6f 6e 20 72 65 71 75 65 73 74 2e 20 20 45 76  ion request.  Ev
b0690 65 6e 20 75 6e 66 75 6c 66 69 6c 6c 65 64 0a 20  en unfulfilled. 
b06a0 20 2a 2a 20 72 65 71 75 65 73 74 73 20 61 72 65   ** requests are
b06b0 20 63 6f 75 6e 74 65 64 20 2a 2f 0a 20 20 69 66   counted */.  if
b06c0 28 20 28 75 33 32 29 6e 42 79 74 65 3e 6d 65 6d  ( (u32)nByte>mem
b06d0 35 2e 6d 61 78 52 65 71 75 65 73 74 20 29 7b 0a  5.maxRequest ){.
b06e0 20 20 20 20 6d 65 6d 35 2e 6d 61 78 52 65 71 75      mem5.maxRequ
b06f0 65 73 74 20 3d 20 6e 42 79 74 65 3b 0a 20 20 7d  est = nByte;.  }
b0700 0a 0a 20 20 2f 2a 20 41 62 6f 72 74 20 69 66 20  ..  /* Abort if 
b0710 74 68 65 20 72 65 71 75 65 73 74 65 64 20 61 6c  the requested al
b0720 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 69 73  location size is
b0730 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
b0740 20 6c 61 72 67 65 73 74 0a 20 20 2a 2a 20 70 6f   largest.  ** po
b0750 77 65 72 20 6f 66 20 74 77 6f 20 74 68 61 74 20  wer of two that 
b0760 77 65 20 63 61 6e 20 72 65 70 72 65 73 65 6e 74  we can represent
b0770 20 75 73 69 6e 67 20 33 32 2d 62 69 74 20 73 69   using 32-bit si
b0780 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 20  gned integers.. 
b0790 20 2a 2f 0a 20 20 69 66 28 20 6e 42 79 74 65 20   */.  if( nByte 
b07a0 3e 20 30 78 34 30 30 30 30 30 30 30 20 29 7b 0a  > 0x40000000 ){.
b07b0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
b07c0 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e 64 20 6e 42  }..  /* Round nB
b07d0 79 74 65 20 75 70 20 74 6f 20 74 68 65 20 6e 65  yte up to the ne
b07e0 78 74 20 76 61 6c 69 64 20 70 6f 77 65 72 20 6f  xt valid power o
b07f0 66 20 74 77 6f 20 2a 2f 0a 20 20 66 6f 72 28 69  f two */.  for(i
b0800 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74  FullSz=mem5.szAt
b0810 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65 3d 30 3b 20  om, iLogsize=0; 
b0820 69 46 75 6c 6c 53 7a 3c 6e 42 79 74 65 3b 20 69  iFullSz<nByte; i
b0830 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c 20 69 4c 6f  FullSz *= 2, iLo
b0840 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a 20 20 2f 2a  gsize++){}..  /*
b0850 20 4d 61 6b 65 20 73 75 72 65 20 6d 65 6d 35 2e   Make sure mem5.
b0860 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73  aiFreelist[iLogs
b0870 69 7a 65 5d 20 63 6f 6e 74 61 69 6e 73 20 61 74  ize] contains at
b0880 20 6c 65 61 73 74 20 6f 6e 65 20 66 72 65 65 0a   least one free.
b0890 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 20    ** block.  If 
b08a0 6e 6f 74 2c 20 74 68 65 6e 20 73 70 6c 69 74 20  not, then split 
b08b0 61 20 62 6c 6f 63 6b 20 6f 66 20 74 68 65 20 6e  a block of the n
b08c0 65 78 74 20 6c 61 72 67 65 72 20 70 6f 77 65 72  ext larger power
b08d0 20 6f 66 0a 20 20 2a 2a 20 74 77 6f 20 69 6e 20   of.  ** two in 
b08e0 6f 72 64 65 72 20 74 6f 20 63 72 65 61 74 65 20  order to create 
b08f0 61 20 6e 65 77 20 66 72 65 65 20 62 6c 6f 63 6b  a new free block
b0900 20 6f 66 20 73 69 7a 65 20 69 4c 6f 67 73 69 7a   of size iLogsiz
b0910 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 42  e..  */.  for(iB
b0920 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b 20 6d 65 6d  in=iLogsize; mem
b0930 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 42 69  5.aiFreelist[iBi
b0940 6e 5d 3c 30 20 26 26 20 69 42 69 6e 3c 3d 4c 4f  n]<0 && iBin<=LO
b0950 47 4d 41 58 3b 20 69 42 69 6e 2b 2b 29 7b 7d 0a  GMAX; iBin++){}.
b0960 20 20 69 66 28 20 69 42 69 6e 3e 4c 4f 47 4d 41    if( iBin>LOGMA
b0970 58 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73  X ){.    testcas
b0980 65 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  e( sqlite3Global
b0990 43 6f 6e 66 69 67 2e 78 4c 6f 67 21 3d 30 20 29  Config.xLog!=0 )
b09a0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f  ;.    sqlite3_lo
b09b0 67 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20  g(SQLITE_NOMEM, 
b09c0 22 66 61 69 6c 65 64 20 74 6f 20 61 6c 6c 6f 63  "failed to alloc
b09d0 61 74 65 20 25 75 20 62 79 74 65 73 22 2c 20 6e  ate %u bytes", n
b09e0 42 79 74 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Byte);.    retur
b09f0 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 20 3d 20 6d  n 0;.  }.  i = m
b0a00 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 46 69 72 73  emsys5UnlinkFirs
b0a10 74 28 69 42 69 6e 29 3b 0a 20 20 77 68 69 6c 65  t(iBin);.  while
b0a20 28 20 69 42 69 6e 3e 69 4c 6f 67 73 69 7a 65 20  ( iBin>iLogsize 
b0a30 29 7b 0a 20 20 20 20 69 6e 74 20 6e 65 77 53 69  ){.    int newSi
b0a40 7a 65 3b 0a 0a 20 20 20 20 69 42 69 6e 2d 2d 3b  ze;..    iBin--;
b0a50 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 31  .    newSize = 1
b0a60 20 3c 3c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65   << iBin;.    me
b0a70 6d 35 2e 61 43 74 72 6c 5b 69 2b 6e 65 77 53 69  m5.aCtrl[i+newSi
b0a80 7a 65 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20  ze] = CTRL_FREE 
b0a90 7c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 73  | iBin;.    mems
b0aa0 79 73 35 4c 69 6e 6b 28 69 2b 6e 65 77 53 69 7a  ys5Link(i+newSiz
b0ab0 65 2c 20 69 42 69 6e 29 3b 0a 20 20 7d 0a 20 20  e, iBin);.  }.  
b0ac0 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 3d 20  mem5.aCtrl[i] = 
b0ad0 69 4c 6f 67 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20  iLogsize;..  /* 
b0ae0 55 70 64 61 74 65 20 61 6c 6c 6f 63 61 74 6f 72  Update allocator
b0af0 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 73 74 61   performance sta
b0b00 74 69 73 74 69 63 73 2e 20 2a 2f 0a 20 20 6d 65  tistics. */.  me
b0b10 6d 35 2e 6e 41 6c 6c 6f 63 2b 2b 3b 0a 20 20 6d  m5.nAlloc++;.  m
b0b20 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 2b  em5.totalAlloc +
b0b30 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 6d 65 6d  = iFullSz;.  mem
b0b40 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 20 2b 3d  5.totalExcess +=
b0b50 20 69 46 75 6c 6c 53 7a 20 2d 20 6e 42 79 74 65   iFullSz - nByte
b0b60 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  ;.  mem5.current
b0b70 43 6f 75 6e 74 2b 2b 3b 0a 20 20 6d 65 6d 35 2e  Count++;.  mem5.
b0b80 63 75 72 72 65 6e 74 4f 75 74 20 2b 3d 20 69 46  currentOut += iF
b0b90 75 6c 6c 53 7a 3b 0a 20 20 69 66 28 20 6d 65 6d  ullSz;.  if( mem
b0ba0 35 2e 6d 61 78 43 6f 75 6e 74 3c 6d 65 6d 35 2e  5.maxCount<mem5.
b0bb0 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 29 20 6d  currentCount ) m
b0bc0 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 20 3d 20 6d  em5.maxCount = m
b0bd0 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74  em5.currentCount
b0be0 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78  ;.  if( mem5.max
b0bf0 4f 75 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74  Out<mem5.current
b0c00 4f 75 74 20 29 20 6d 65 6d 35 2e 6d 61 78 4f 75  Out ) mem5.maxOu
b0c10 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  t = mem5.current
b0c20 4f 75 74 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72  Out;..  /* Retur
b0c30 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
b0c40 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d  he allocated mem
b0c50 6f 72 79 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e  ory. */.  return
b0c60 20 28 76 6f 69 64 2a 29 26 6d 65 6d 35 2e 7a 50   (void*)&mem5.zP
b0c70 6f 6f 6c 5b 69 2a 6d 65 6d 35 2e 73 7a 41 74 6f  ool[i*mem5.szAto
b0c80 6d 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  m];.}../*.** Fre
b0c90 65 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67  e an outstanding
b0ca0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
b0cb0 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
b0cc0 69 64 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e  id memsys5FreeUn
b0cd0 73 61 66 65 28 76 6f 69 64 20 2a 70 4f 6c 64 29  safe(void *pOld)
b0ce0 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 69 4c  {.  u32 size, iL
b0cf0 6f 67 73 69 7a 65 3b 0a 20 20 69 6e 74 20 69 42  ogsize;.  int iB
b0d00 6c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 53 65 74 20  lock;..  /* Set 
b0d10 69 42 6c 6f 63 6b 20 74 6f 20 74 68 65 20 69 6e  iBlock to the in
b0d20 64 65 78 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b  dex of the block
b0d30 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70   pointed to by p
b0d40 4f 6c 64 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65  Old in .  ** the
b0d50 20 61 72 72 61 79 20 6f 66 20 6d 65 6d 35 2e 73   array of mem5.s
b0d60 7a 41 74 6f 6d 20 62 79 74 65 20 62 6c 6f 63 6b  zAtom byte block
b0d70 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  s pointed to by 
b0d80 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a 20 20 2a 2f  mem5.zPool..  */
b0d90 0a 20 20 69 42 6c 6f 63 6b 20 3d 20 28 28 75 38  .  iBlock = ((u8
b0da0 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f   *)pOld-mem5.zPo
b0db0 6f 6c 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b  ol)/mem5.szAtom;
b0dc0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
b0dd0 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70 4f  t the pointer pO
b0de0 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 76  ld points to a v
b0df0 61 6c 69 64 2c 20 6e 6f 6e 2d 66 72 65 65 20 62  alid, non-free b
b0e00 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 61 73 73 65 72  lock. */.  asser
b0e10 74 28 20 69 42 6c 6f 63 6b 3e 3d 30 20 26 26 20  t( iBlock>=0 && 
b0e20 69 42 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e 42 6c 6f  iBlock<mem5.nBlo
b0e30 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ck );.  assert( 
b0e40 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35  ((u8 *)pOld-mem5
b0e50 2e 7a 50 6f 6f 6c 29 25 6d 65 6d 35 2e 73 7a 41  .zPool)%mem5.szA
b0e60 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  tom==0 );.  asse
b0e70 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b  rt( (mem5.aCtrl[
b0e80 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 46  iBlock] & CTRL_F
b0e90 52 45 45 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 4c  REE)==0 );..  iL
b0ea0 6f 67 73 69 7a 65 20 3d 20 6d 65 6d 35 2e 61 43  ogsize = mem5.aC
b0eb0 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54  trl[iBlock] & CT
b0ec0 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20 73 69  RL_LOGSIZE;.  si
b0ed0 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67 73 69 7a 65  ze = 1<<iLogsize
b0ee0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f  ;.  assert( iBlo
b0ef0 63 6b 2b 73 69 7a 65 2d 31 3c 28 75 33 32 29 6d  ck+size-1<(u32)m
b0f00 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 0a 20  em5.nBlock );.. 
b0f10 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f   mem5.aCtrl[iBlo
b0f20 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45  ck] |= CTRL_FREE
b0f30 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69  ;.  mem5.aCtrl[i
b0f40 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20 7c 3d  Block+size-1] |=
b0f50 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 61 73   CTRL_FREE;.  as
b0f60 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65  sert( mem5.curre
b0f70 6e 74 43 6f 75 6e 74 3e 30 20 29 3b 0a 20 20 61  ntCount>0 );.  a
b0f80 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72  ssert( mem5.curr
b0f90 65 6e 74 4f 75 74 3e 3d 28 73 69 7a 65 2a 6d 65  entOut>=(size*me
b0fa0 6d 35 2e 73 7a 41 74 6f 6d 29 20 29 3b 0a 20 20  m5.szAtom) );.  
b0fb0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e  mem5.currentCoun
b0fc0 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72  t--;.  mem5.curr
b0fd0 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a 65 2a 6d  entOut -= size*m
b0fe0 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20 20 61 73  em5.szAtom;.  as
b0ff0 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65  sert( mem5.curre
b1000 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e  ntOut>0 || mem5.
b1010 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d 30 20  currentCount==0 
b1020 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d  );.  assert( mem
b1030 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30  5.currentCount>0
b1040 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74   || mem5.current
b1050 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d  Out==0 );..  mem
b1060 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
b1070 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c  = CTRL_FREE | iL
b1080 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c 65 28  ogsize;.  while(
b1090 20 41 4c 57 41 59 53 28 69 4c 6f 67 73 69 7a 65   ALWAYS(iLogsize
b10a0 3c 4c 4f 47 4d 41 58 29 20 29 7b 0a 20 20 20 20  <LOGMAX) ){.    
b10b0 69 6e 74 20 69 42 75 64 64 79 3b 0a 20 20 20 20  int iBuddy;.    
b10c0 69 66 28 20 28 69 42 6c 6f 63 6b 3e 3e 69 4c 6f  if( (iBlock>>iLo
b10d0 67 73 69 7a 65 29 20 26 20 31 20 29 7b 0a 20 20  gsize) & 1 ){.  
b10e0 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 42 6c      iBuddy = iBl
b10f0 6f 63 6b 20 2d 20 73 69 7a 65 3b 0a 20 20 20 20  ock - size;.    
b1100 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 42 75  }else{.      iBu
b1110 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2b 20 73  ddy = iBlock + s
b1120 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  ize;.    }.    a
b1130 73 73 65 72 74 28 20 69 42 75 64 64 79 3e 3d 30  ssert( iBuddy>=0
b1140 20 29 3b 0a 20 20 20 20 69 66 28 20 28 69 42 75   );.    if( (iBu
b1150 64 64 79 2b 28 31 3c 3c 69 4c 6f 67 73 69 7a 65  ddy+(1<<iLogsize
b1160 29 29 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29  ))>mem5.nBlock )
b1170 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20   break;.    if( 
b1180 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64  mem5.aCtrl[iBudd
b1190 79 5d 21 3d 28 43 54 52 4c 5f 46 52 45 45 20 7c  y]!=(CTRL_FREE |
b11a0 20 69 4c 6f 67 73 69 7a 65 29 20 29 20 62 72 65   iLogsize) ) bre
b11b0 61 6b 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 55  ak;.    memsys5U
b11c0 6e 6c 69 6e 6b 28 69 42 75 64 64 79 2c 20 69 4c  nlink(iBuddy, iL
b11d0 6f 67 73 69 7a 65 29 3b 0a 20 20 20 20 69 4c 6f  ogsize);.    iLo
b11e0 67 73 69 7a 65 2b 2b 3b 0a 20 20 20 20 69 66 28  gsize++;.    if(
b11f0 20 69 42 75 64 64 79 3c 69 42 6c 6f 63 6b 20 29   iBuddy<iBlock )
b1200 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74  {.      mem5.aCt
b1210 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 43 54 52  rl[iBuddy] = CTR
b1220 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a  L_FREE | iLogsiz
b1230 65 3b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43  e;.      mem5.aC
b1240 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 30 3b  trl[iBlock] = 0;
b1250 0a 20 20 20 20 20 20 69 42 6c 6f 63 6b 20 3d 20  .      iBlock = 
b1260 69 42 75 64 64 79 3b 0a 20 20 20 20 7d 65 6c 73  iBuddy;.    }els
b1270 65 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43  e{.      mem5.aC
b1280 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43 54  trl[iBlock] = CT
b1290 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69  RL_FREE | iLogsi
b12a0 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61  ze;.      mem5.a
b12b0 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 30  Ctrl[iBuddy] = 0
b12c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 69 7a 65  ;.    }.    size
b12d0 20 2a 3d 20 32 3b 0a 20 20 7d 0a 20 20 6d 65 6d   *= 2;.  }.  mem
b12e0 73 79 73 35 4c 69 6e 6b 28 69 42 6c 6f 63 6b 2c  sys5Link(iBlock,
b12f0 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 7d 0a 0a 2f   iLogsize);.}../
b1300 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42  *.** Allocate nB
b1310 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a  ytes of memory.*
b1320 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d  /.static void *m
b1330 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 28 69 6e 74  emsys5Malloc(int
b1340 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c 69   nBytes){.  sqli
b1350 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 30  te3_int64 *p = 0
b1360 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3e 30  ;.  if( nBytes>0
b1370 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 35 45   ){.    memsys5E
b1380 6e 74 65 72 28 29 3b 0a 20 20 20 20 70 20 3d 20  nter();.    p = 
b1390 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73  memsys5MallocUns
b13a0 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 20  afe(nBytes);.   
b13b0 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b   memsys5Leave();
b13c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76  .  }.  return (v
b13d0 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a  oid*)p; .}../*.*
b13e0 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a  * Free memory..*
b13f0 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 65 72 20 6c  *.** The outer l
b1400 61 79 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ayer memory allo
b1410 63 61 74 6f 72 20 70 72 65 76 65 6e 74 73 20 74  cator prevents t
b1420 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d  his routine from
b1430 0a 2a 2a 20 62 65 69 6e 67 20 63 61 6c 6c 65 64  .** being called
b1440 20 77 69 74 68 20 70 50 72 69 6f 72 3d 3d 30 2e   with pPrior==0.
b1450 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
b1460 6d 65 6d 73 79 73 35 46 72 65 65 28 76 6f 69 64  memsys5Free(void
b1470 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73   *pPrior){.  ass
b1480 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29  ert( pPrior!=0 )
b1490 3b 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72  ;.  memsys5Enter
b14a0 28 29 3b 0a 20 20 6d 65 6d 73 79 73 35 46 72 65  ();.  memsys5Fre
b14b0 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b  eUnsafe(pPrior);
b14c0 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28  .  memsys5Leave(
b14d0 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  );  .}../*.** Ch
b14e0 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66  ange the size of
b14f0 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d   an existing mem
b1500 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a  ory allocation..
b1510 2a 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 65 72 20  **.** The outer 
b1520 6c 61 79 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c  layer memory all
b1530 6f 63 61 74 6f 72 20 70 72 65 76 65 6e 74 73 20  ocator prevents 
b1540 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f  this routine fro
b1550 6d 0a 2a 2a 20 62 65 69 6e 67 20 63 61 6c 6c 65  m.** being calle
b1560 64 20 77 69 74 68 20 70 50 72 69 6f 72 3d 3d 30  d with pPrior==0
b1570 2e 20 20 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 65 73  .  .**.** nBytes
b1580 20 69 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c   is always a val
b1590 75 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ue obtained from
b15a0 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f   a prior call to
b15b0 0a 2a 2a 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64  .** memsys5Round
b15c0 28 29 2e 20 20 48 65 6e 63 65 20 6e 42 79 74 65  ().  Hence nByte
b15d0 73 20 69 73 20 61 6c 77 61 79 73 20 61 20 6e 6f  s is always a no
b15e0 6e 2d 6e 65 67 61 74 69 76 65 20 70 6f 77 65 72  n-negative power
b15f0 0a 2a 2a 20 6f 66 20 74 77 6f 2e 20 20 49 66 20  .** of two.  If 
b1600 6e 42 79 74 65 73 3d 3d 30 20 74 68 61 74 20 6d  nBytes==0 that m
b1610 65 61 6e 73 20 74 68 61 74 20 61 6e 20 6f 76 65  eans that an ove
b1620 72 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  rsize allocation
b1630 0a 2a 2a 20 28 61 6e 20 61 6c 6c 6f 63 61 74 69  .** (an allocati
b1640 6f 6e 20 6c 61 72 67 65 72 20 74 68 61 6e 20 30  on larger than 0
b1650 78 34 30 30 30 30 30 30 30 29 20 77 61 73 20 72  x40000000) was r
b1660 65 71 75 65 73 74 65 64 20 61 6e 64 20 74 68 69  equested and thi
b1670 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 73 68 6f  s.** routine sho
b1680 75 6c 64 20 72 65 74 75 72 6e 20 30 20 77 69 74  uld return 0 wit
b1690 68 6f 75 74 20 66 72 65 65 69 6e 67 20 70 50 72  hout freeing pPr
b16a0 69 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ior..*/.static v
b16b0 6f 69 64 20 2a 6d 65 6d 73 79 73 35 52 65 61 6c  oid *memsys5Real
b16c0 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  loc(void *pPrior
b16d0 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20  , int nBytes){. 
b16e0 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69   int nOld;.  voi
b16f0 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  d *p;.  assert( 
b1700 70 50 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 61  pPrior!=0 );.  a
b1710 73 73 65 72 74 28 20 28 6e 42 79 74 65 73 26 28  ssert( (nBytes&(
b1720 6e 42 79 74 65 73 2d 31 29 29 3d 3d 30 20 29 3b  nBytes-1))==0 );
b1730 20 20 2f 2a 20 45 56 3a 20 52 2d 34 36 31 39 39    /* EV: R-46199
b1740 2d 33 30 32 34 39 20 2a 2f 0a 20 20 61 73 73 65  -30249 */.  asse
b1750 72 74 28 20 6e 42 79 74 65 73 3e 3d 30 20 29 3b  rt( nBytes>=0 );
b1760 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3d 3d 30  .  if( nBytes==0
b1770 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
b1780 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d  ;.  }.  nOld = m
b1790 65 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f  emsys5Size(pPrio
b17a0 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73  r);.  if( nBytes
b17b0 3c 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65  <=nOld ){.    re
b17c0 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d  turn pPrior;.  }
b17d0 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28  .  memsys5Enter(
b17e0 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35  );.  p = memsys5
b17f0 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79  MallocUnsafe(nBy
b1800 74 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  tes);.  if( p ){
b1810 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70  .    memcpy(p, p
b1820 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20  Prior, nOld);.  
b1830 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73    memsys5FreeUns
b1840 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d  afe(pPrior);.  }
b1850 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28  .  memsys5Leave(
b1860 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d  );.  return p;.}
b1870 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70  ../*.** Round up
b1880 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20   a request size 
b1890 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69  to the next vali
b18a0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a  d allocation siz
b18b0 65 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 61 6c  e.  If.** the al
b18c0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 74 6f 6f 20  location is too 
b18d0 6c 61 72 67 65 20 74 6f 20 62 65 20 68 61 6e 64  large to be hand
b18e0 6c 65 64 20 62 79 20 74 68 69 73 20 61 6c 6c 6f  led by this allo
b18f0 63 61 74 69 6f 6e 20 73 79 73 74 65 6d 2c 0a 2a  cation system,.*
b1900 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a  * return 0..**.*
b1910 2a 20 41 6c 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e  * All allocation
b1920 73 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65  s must be a powe
b1930 72 20 6f 66 20 74 77 6f 20 61 6e 64 20 6d 75 73  r of two and mus
b1940 74 20 62 65 20 65 78 70 72 65 73 73 65 64 20 62  t be expressed b
b1950 79 20 61 0a 2a 2a 20 33 32 2d 62 69 74 20 73 69  y a.** 32-bit si
b1960 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 48  gned integer.  H
b1970 65 6e 63 65 20 74 68 65 20 6c 61 72 67 65 73 74  ence the largest
b1980 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 30   allocation is 0
b1990 78 34 30 30 30 30 30 30 30 0a 2a 2a 20 6f 72 20  x40000000.** or 
b19a0 31 30 37 33 37 34 31 38 32 34 20 62 79 74 65 73  1073741824 bytes
b19b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
b19c0 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69  memsys5Roundup(i
b19d0 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75  nt n){.  int iFu
b19e0 6c 6c 53 7a 3b 0a 20 20 69 66 28 20 6e 20 3e 20  llSz;.  if( n > 
b19f0 30 78 34 30 30 30 30 30 30 30 20 29 20 72 65 74  0x40000000 ) ret
b1a00 75 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 46 75  urn 0;.  for(iFu
b1a10 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d  llSz=mem5.szAtom
b1a20 3b 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75  ; iFullSz<n; iFu
b1a30 6c 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65  llSz *= 2);.  re
b1a40 74 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a  turn iFullSz;.}.
b1a50 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
b1a60 65 20 63 65 69 6c 69 6e 67 20 6f 66 20 74 68 65  e ceiling of the
b1a70 20 6c 6f 67 61 72 69 74 68 6d 20 62 61 73 65 20   logarithm base 
b1a80 32 20 6f 66 20 69 56 61 6c 75 65 2e 0a 2a 2a 0a  2 of iValue..**.
b1a90 2a 2a 20 45 78 61 6d 70 6c 65 73 3a 20 20 20 6d  ** Examples:   m
b1aa0 65 6d 73 79 73 35 4c 6f 67 28 31 29 20 2d 3e 20  emsys5Log(1) -> 
b1ab0 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  0.**            
b1ac0 20 6d 65 6d 73 79 73 35 4c 6f 67 28 32 29 20 2d   memsys5Log(2) -
b1ad0 3e 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  > 1.**          
b1ae0 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 34 29     memsys5Log(4)
b1af0 20 2d 3e 20 32 0a 2a 2a 20 20 20 20 20 20 20 20   -> 2.**        
b1b00 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28       memsys5Log(
b1b10 35 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 20  5) -> 3.**      
b1b20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f         memsys5Lo
b1b30 67 28 38 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20  g(8) -> 3.**    
b1b40 20 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35           memsys5
b1b50 4c 6f 67 28 39 29 20 2d 3e 20 34 0a 2a 2f 0a 73  Log(9) -> 4.*/.s
b1b60 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73  tatic int memsys
b1b70 35 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29  5Log(int iValue)
b1b80 7b 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20  {.  int iLog;.  
b1b90 66 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 69 4c 6f  for(iLog=0; (iLo
b1ba0 67 3c 28 69 6e 74 29 28 28 73 69 7a 65 6f 66 28  g<(int)((sizeof(
b1bb0 69 6e 74 29 2a 38 29 2d 31 29 29 20 26 26 20 28  int)*8)-1)) && (
b1bc0 31 3c 3c 69 4c 6f 67 29 3c 69 56 61 6c 75 65 3b  1<<iLog)<iValue;
b1bd0 20 69 4c 6f 67 2b 2b 29 3b 0a 20 20 72 65 74 75   iLog++);.  retu
b1be0 72 6e 20 69 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn iLog;.}../*.*
b1bf0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
b1c00 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
b1c10 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  r..**.** This ro
b1c20 75 74 69 6e 65 20 69 73 20 6e 6f 74 20 74 68 72  utine is not thr
b1c30 65 61 64 73 61 66 65 2e 20 20 54 68 65 20 63 61  eadsafe.  The ca
b1c40 6c 6c 65 72 20 6d 75 73 74 20 62 65 20 68 6f 6c  ller must be hol
b1c50 64 69 6e 67 20 61 20 6d 75 74 65 78 0a 2a 2a 20  ding a mutex.** 
b1c60 74 6f 20 70 72 65 76 65 6e 74 20 6d 75 6c 74 69  to prevent multi
b1c70 70 6c 65 20 74 68 72 65 61 64 73 20 66 72 6f 6d  ple threads from
b1c80 20 65 6e 74 65 72 69 6e 67 20 61 74 20 74 68 65   entering at the
b1c90 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73   same time..*/.s
b1ca0 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73  tatic int memsys
b1cb0 35 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55  5Init(void *NotU
b1cc0 73 65 64 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20  sed){.  int ii; 
b1cd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
b1ce0 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
b1cf0 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20  int nByte;      
b1d00 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
b1d10 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  bytes of memory 
b1d20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 69  available to thi
b1d30 73 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20  s allocator */. 
b1d40 20 75 38 20 2a 7a 42 79 74 65 3b 20 20 20 20 20   u8 *zByte;     
b1d50 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 75 73      /* Memory us
b1d60 61 62 6c 65 20 62 79 20 74 68 69 73 20 61 6c 6c  able by this all
b1d70 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  ocator */.  int 
b1d80 6e 4d 69 6e 4c 6f 67 3b 20 20 20 20 20 20 20 2f  nMinLog;       /
b1d90 2a 20 4c 6f 67 20 62 61 73 65 20 32 20 6f 66 20  * Log base 2 of 
b1da0 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69  minimum allocati
b1db0 6f 6e 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73  on size in bytes
b1dc0 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65   */.  int iOffse
b1dd0 74 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f  t;       /* An o
b1de0 66 66 73 65 74 20 69 6e 74 6f 20 6d 65 6d 35 2e  ffset into mem5.
b1df0 61 43 74 72 6c 5b 5d 20 2a 2f 0a 0a 20 20 55 4e  aCtrl[] */..  UN
b1e00 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
b1e10 6f 74 55 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 46  otUsed);..  /* F
b1e20 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20  or the purposes 
b1e30 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c  of this routine,
b1e40 20 64 69 73 61 62 6c 65 20 74 68 65 20 6d 75 74   disable the mut
b1e50 65 78 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6d 75 74  ex */.  mem5.mut
b1e60 65 78 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68  ex = 0;..  /* Th
b1e70 65 20 73 69 7a 65 20 6f 66 20 61 20 4d 65 6d 35  e size of a Mem5
b1e80 4c 69 6e 6b 20 6f 62 6a 65 63 74 20 6d 75 73 74  Link object must
b1e90 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74   be a power of t
b1ea0 77 6f 2e 20 20 56 65 72 69 66 79 20 74 68 61 74  wo.  Verify that
b1eb0 0a 20 20 2a 2a 20 74 68 69 73 20 69 73 20 63 61  .  ** this is ca
b1ec0 73 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  se..  */.  asser
b1ed0 74 28 20 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c  t( (sizeof(Mem5L
b1ee0 69 6e 6b 29 26 28 73 69 7a 65 6f 66 28 4d 65 6d  ink)&(sizeof(Mem
b1ef0 35 4c 69 6e 6b 29 2d 31 29 29 3d 3d 30 20 29 3b  5Link)-1))==0 );
b1f00 0a 0a 20 20 6e 42 79 74 65 20 3d 20 73 71 6c 69  ..  nByte = sqli
b1f10 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
b1f20 6e 48 65 61 70 3b 0a 20 20 7a 42 79 74 65 20 3d  nHeap;.  zByte =
b1f30 20 28 75 38 2a 29 73 71 6c 69 74 65 33 47 6c 6f   (u8*)sqlite3Glo
b1f40 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b  balConfig.pHeap;
b1f50 0a 20 20 61 73 73 65 72 74 28 20 7a 42 79 74 65  .  assert( zByte
b1f60 21 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74  !=0 );  /* sqlit
b1f70 65 33 5f 63 6f 6e 66 69 67 28 29 20 64 6f 65 73  e3_config() does
b1f80 20 6e 6f 74 20 61 6c 6c 6f 77 20 6f 74 68 65 72   not allow other
b1f90 77 69 73 65 20 2a 2f 0a 0a 20 20 2f 2a 20 62 6f  wise */..  /* bo
b1fa0 75 6e 64 61 72 69 65 73 20 6f 6e 20 73 71 6c 69  undaries on sqli
b1fb0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
b1fc0 6d 6e 52 65 71 20 61 72 65 20 65 6e 66 6f 72 63  mnReq are enforc
b1fd0 65 64 20 69 6e 20 73 71 6c 69 74 65 33 5f 63 6f  ed in sqlite3_co
b1fe0 6e 66 69 67 28 29 20 2a 2f 0a 20 20 6e 4d 69 6e  nfig() */.  nMin
b1ff0 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67  Log = memsys5Log
b2000 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
b2010 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a 20 20 6d  nfig.mnReq);.  m
b2020 65 6d 35 2e 73 7a 41 74 6f 6d 20 3d 20 28 31 3c  em5.szAtom = (1<
b2030 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20 77 68 69  <nMinLog);.  whi
b2040 6c 65 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 28  le( (int)sizeof(
b2050 4d 65 6d 35 4c 69 6e 6b 29 3e 6d 65 6d 35 2e 73  Mem5Link)>mem5.s
b2060 7a 41 74 6f 6d 20 29 7b 0a 20 20 20 20 6d 65 6d  zAtom ){.    mem
b2070 35 2e 73 7a 41 74 6f 6d 20 3d 20 6d 65 6d 35 2e  5.szAtom = mem5.
b2080 73 7a 41 74 6f 6d 20 3c 3c 20 31 3b 0a 20 20 7d  szAtom << 1;.  }
b2090 0a 0a 20 20 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20  ..  mem5.nBlock 
b20a0 3d 20 28 6e 42 79 74 65 20 2f 20 28 6d 65 6d 35  = (nByte / (mem5
b20b0 2e 73 7a 41 74 6f 6d 2b 73 69 7a 65 6f 66 28 75  .szAtom+sizeof(u
b20c0 38 29 29 29 3b 0a 20 20 6d 65 6d 35 2e 7a 50 6f  8)));.  mem5.zPo
b20d0 6f 6c 20 3d 20 7a 42 79 74 65 3b 0a 20 20 6d 65  ol = zByte;.  me
b20e0 6d 35 2e 61 43 74 72 6c 20 3d 20 28 75 38 20 2a  m5.aCtrl = (u8 *
b20f0 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d  )&mem5.zPool[mem
b2100 35 2e 6e 42 6c 6f 63 6b 2a 6d 65 6d 35 2e 73 7a  5.nBlock*mem5.sz
b2110 41 74 6f 6d 5d 3b 0a 0a 20 20 66 6f 72 28 69 69  Atom];..  for(ii
b2120 3d 30 3b 20 69 69 3c 3d 4c 4f 47 4d 41 58 3b 20  =0; ii<=LOGMAX; 
b2130 69 69 2b 2b 29 7b 0a 20 20 20 20 6d 65 6d 35 2e  ii++){.    mem5.
b2140 61 69 46 72 65 65 6c 69 73 74 5b 69 69 5d 20 3d  aiFreelist[ii] =
b2150 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 69 4f 66 66   -1;.  }..  iOff
b2160 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  set = 0;.  for(i
b2170 69 3d 4c 4f 47 4d 41 58 3b 20 69 69 3e 3d 30 3b  i=LOGMAX; ii>=0;
b2180 20 69 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20   ii--){.    int 
b2190 6e 41 6c 6c 6f 63 20 3d 20 28 31 3c 3c 69 69 29  nAlloc = (1<<ii)
b21a0 3b 0a 20 20 20 20 69 66 28 20 28 69 4f 66 66 73  ;.    if( (iOffs
b21b0 65 74 2b 6e 41 6c 6c 6f 63 29 3c 3d 6d 65 6d 35  et+nAlloc)<=mem5
b21c0 2e 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20  .nBlock ){.     
b21d0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 4f 66 66   mem5.aCtrl[iOff
b21e0 73 65 74 5d 20 3d 20 69 69 20 7c 20 43 54 52 4c  set] = ii | CTRL
b21f0 5f 46 52 45 45 3b 0a 20 20 20 20 20 20 6d 65 6d  _FREE;.      mem
b2200 73 79 73 35 4c 69 6e 6b 28 69 4f 66 66 73 65 74  sys5Link(iOffset
b2210 2c 20 69 69 29 3b 0a 20 20 20 20 20 20 69 4f 66  , ii);.      iOf
b2220 66 73 65 74 20 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a  fset += nAlloc;.
b2230 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
b2240 28 28 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f 63  ((iOffset+nAlloc
b2250 29 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 29 3b 0a  )>mem5.nBlock);.
b2260 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 6d    }..  /* If a m
b2270 75 74 65 78 20 69 73 20 72 65 71 75 69 72 65 64  utex is required
b2280 20 66 6f 72 20 6e 6f 72 6d 61 6c 20 6f 70 65 72   for normal oper
b2290 61 74 69 6f 6e 2c 20 61 6c 6c 6f 63 61 74 65 20  ation, allocate 
b22a0 6f 6e 65 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  one */.  if( sql
b22b0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
b22c0 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20 29 7b 0a  .bMemstat==0 ){.
b22d0 20 20 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d      mem5.mutex =
b22e0 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
b22f0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
b2300 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d  STATIC_MEM);.  }
b2310 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
b2320 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  E_OK;.}../*.** D
b2330 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73  einitialize this
b2340 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74   module..*/.stat
b2350 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 53  ic void memsys5S
b2360 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f  hutdown(void *No
b2370 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44  tUsed){.  UNUSED
b2380 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
b2390 65 64 29 3b 0a 20 20 6d 65 6d 35 2e 6d 75 74 65  ed);.  mem5.mute
b23a0 78 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 3b  x = 0;.  return;
b23b0 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
b23c0 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4f 70 65  E_TEST./*.** Ope
b23d0 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63  n the file indic
b23e0 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61  ated and write a
b23f0 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72   log of all unfr
b2400 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61  eed memory .** a
b2410 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20  llocations into 
b2420 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c  that log..*/.SQL
b2430 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
b2440 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 35 44   sqlite3Memsys5D
b2450 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ump(const char *
b2460 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 49  zFilename){.  FI
b2470 4c 45 20 2a 6f 75 74 3b 0a 20 20 69 6e 74 20 69  LE *out;.  int i
b2480 2c 20 6a 2c 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d  , j, n;.  int nM
b2490 69 6e 4c 6f 67 3b 0a 0a 20 20 69 66 28 20 7a 46  inLog;..  if( zF
b24a0 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46  ilename==0 || zF
b24b0 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b  ilename[0]==0 ){
b24c0 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75  .    out = stdou
b24d0 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
b24e0 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c  out = fopen(zFil
b24f0 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20  ename, "w");.   
b2500 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20   if( out==0 ){. 
b2510 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64       fprintf(std
b2520 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20  err, "** Unable 
b2530 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79  to output memory
b2540 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f   debug output lo
b2550 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20  g: %s **\n",.   
b2560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b2570 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20     zFilename);. 
b2580 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
b2590 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35   }.  }.  memsys5
b25a0 45 6e 74 65 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c  Enter();.  nMinL
b25b0 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28  og = memsys5Log(
b25c0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 3b 0a 20 20  mem5.szAtom);.  
b25d0 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 4c 4f 47 4d  for(i=0; i<=LOGM
b25e0 41 58 20 26 26 20 69 2b 6e 4d 69 6e 4c 6f 67 3c  AX && i+nMinLog<
b25f0 33 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66 6f  32; i++){.    fo
b2600 72 28 6e 3d 30 2c 20 6a 3d 6d 65 6d 35 2e 61 69  r(n=0, j=mem5.ai
b2610 46 72 65 65 6c 69 73 74 5b 69 5d 3b 20 6a 3e 3d  Freelist[i]; j>=
b2620 30 3b 20 6a 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28  0; j = MEM5LINK(
b2630 6a 29 2d 3e 6e 65 78 74 2c 20 6e 2b 2b 29 7b 7d  j)->next, n++){}
b2640 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74  .    fprintf(out
b2650 2c 20 22 66 72 65 65 6c 69 73 74 20 69 74 65 6d  , "freelist item
b2660 73 20 6f 66 20 73 69 7a 65 20 25 64 3a 20 25 64  s of size %d: %d
b2670 5c 6e 22 2c 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d  \n", mem5.szAtom
b2680 20 3c 3c 20 69 2c 20 6e 29 3b 0a 20 20 7d 0a 20   << i, n);.  }. 
b2690 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d   fprintf(out, "m
b26a0 65 6d 35 2e 6e 41 6c 6c 6f 63 20 20 20 20 20 20  em5.nAlloc      
b26b0 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35   = %llu\n", mem5
b26c0 2e 6e 41 6c 6c 6f 63 29 3b 0a 20 20 66 70 72 69  .nAlloc);.  fpri
b26d0 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 74  ntf(out, "mem5.t
b26e0 6f 74 61 6c 41 6c 6c 6f 63 20 20 20 3d 20 25 6c  otalAlloc   = %l
b26f0 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 74 6f 74 61  lu\n", mem5.tota
b2700 6c 41 6c 6c 6f 63 29 3b 0a 20 20 66 70 72 69 6e  lAlloc);.  fprin
b2710 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 74 6f  tf(out, "mem5.to
b2720 74 61 6c 45 78 63 65 73 73 20 20 3d 20 25 6c 6c  talExcess  = %ll
b2730 75 5c 6e 22 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c  u\n", mem5.total
b2740 45 78 63 65 73 73 29 3b 0a 20 20 66 70 72 69 6e  Excess);.  fprin
b2750 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 63 75  tf(out, "mem5.cu
b2760 72 72 65 6e 74 4f 75 74 20 20 20 3d 20 25 75 5c  rrentOut   = %u\
b2770 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  n", mem5.current
b2780 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  Out);.  fprintf(
b2790 6f 75 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65  out, "mem5.curre
b27a0 6e 74 43 6f 75 6e 74 20 3d 20 25 75 5c 6e 22 2c  ntCount = %u\n",
b27b0 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75   mem5.currentCou
b27c0 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  nt);.  fprintf(o
b27d0 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 4f 75 74  ut, "mem5.maxOut
b27e0 20 20 20 20 20 20 20 3d 20 25 75 5c 6e 22 2c 20         = %u\n", 
b27f0 6d 65 6d 35 2e 6d 61 78 4f 75 74 29 3b 0a 20 20  mem5.maxOut);.  
b2800 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65  fprintf(out, "me
b2810 6d 35 2e 6d 61 78 43 6f 75 6e 74 20 20 20 20 20  m5.maxCount     
b2820 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61  = %u\n", mem5.ma
b2830 78 43 6f 75 6e 74 29 3b 0a 20 20 66 70 72 69 6e  xCount);.  fprin
b2840 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61  tf(out, "mem5.ma
b2850 78 52 65 71 75 65 73 74 20 20 20 3d 20 25 75 5c  xRequest   = %u\
b2860 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 52 65 71 75  n", mem5.maxRequ
b2870 65 73 74 29 3b 0a 20 20 6d 65 6d 73 79 73 35 4c  est);.  memsys5L
b2880 65 61 76 65 28 29 3b 0a 20 20 69 66 28 20 6f 75  eave();.  if( ou
b2890 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a 20 20 20  t==stdout ){.   
b28a0 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b   fflush(stdout);
b28b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 63  .  }else{.    fc
b28c0 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 7d 0a 7d  lose(out);.  }.}
b28d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
b28e0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74  his routine is t
b28f0 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20  he only routine 
b2900 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74  in this file wit
b2910 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c  h external .** l
b2920 69 6e 6b 61 67 65 2e 20 49 74 20 72 65 74 75 72  inkage. It retur
b2930 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
b2940 61 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  a static sqlite3
b2950 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20  _mem_methods.** 
b2960 73 74 72 75 63 74 20 70 6f 70 75 6c 61 74 65 64  struct populated
b2970 20 77 69 74 68 20 74 68 65 20 6d 65 6d 73 79 73   with the memsys
b2980 35 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51  5 methods..*/.SQ
b2990 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
b29a0 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  st sqlite3_mem_m
b29b0 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d  ethods *sqlite3M
b29c0 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f 69  emGetMemsys5(voi
b29d0 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  d){.  static con
b29e0 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  st sqlite3_mem_m
b29f0 65 74 68 6f 64 73 20 6d 65 6d 73 79 73 35 4d 65  ethods memsys5Me
b2a00 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d  thods = {.     m
b2a10 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 2c 0a 20 20  emsys5Malloc,.  
b2a20 20 20 20 6d 65 6d 73 79 73 35 46 72 65 65 2c 0a     memsys5Free,.
b2a30 20 20 20 20 20 6d 65 6d 73 79 73 35 52 65 61 6c       memsys5Real
b2a40 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73  loc,.     memsys
b2a50 35 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73  5Size,.     mems
b2a60 79 73 35 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20  ys5Roundup,.    
b2a70 20 6d 65 6d 73 79 73 35 49 6e 69 74 2c 0a 20 20   memsys5Init,.  
b2a80 20 20 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f     memsys5Shutdo
b2a90 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a  wn,.     0.  };.
b2aa0 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 73 79 73    return &memsys
b2ab0 35 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e  5Methods;.}..#en
b2ac0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e  dif /* SQLITE_EN
b2ad0 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 2a 2f 0a  ABLE_MEMSYS5 */.
b2ae0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
b2af0 20 45 6e 64 20 6f 66 20 6d 65 6d 35 2e 63 20 2a   End of mem5.c *
b2b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
b2b30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
b2b40 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65   Begin file mute
b2b50 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.c ************
b2b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
b2b80 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  ./*.** 2007 Augu
b2b90 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 14.**.** The 
b2ba0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
b2bb0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
b2bc0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
b2bd0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
b2be0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
b2bf0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
b2c00 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
b2c10 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
b2c20 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
b2c30 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
b2c40 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
b2c50 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
b2c60 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
b2c70 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
b2c80 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
b2c90 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
b2ca0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
b2cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2cf0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
b2d00 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43  e contains the C
b2d10 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
b2d20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65  implement mutexe
b2d30 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  s..**.** This fi
b2d40 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
b2d50 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20   that is common 
b2d60 61 63 72 6f 73 73 20 61 6c 6c 20 6d 75 74 65 78  across all mutex
b2d70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
b2d80 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65  ..*/..#if define
b2d90 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20  d(SQLITE_DEBUG) 
b2da0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
b2db0 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 29 0a 2f  TE_MUTEX_OMIT)./
b2dc0 2a 0a 2a 2a 20 46 6f 72 20 64 65 62 75 67 67 69  *.** For debuggi
b2dd0 6e 67 20 70 75 72 70 6f 73 65 73 2c 20 72 65 63  ng purposes, rec
b2de0 6f 72 64 20 77 68 65 6e 20 74 68 65 20 6d 75 74  ord when the mut
b2df0 65 78 20 73 75 62 73 79 73 74 65 6d 20 69 73 20  ex subsystem is 
b2e00 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a 20 61  initialized.** a
b2e10 6e 64 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64  nd uninitialized
b2e20 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20   so that we can 
b2e30 61 73 73 65 72 74 28 29 20 69 66 20 74 68 65 72  assert() if ther
b2e40 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20  e is an attempt 
b2e50 74 6f 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 61  to.** allocate a
b2e60 20 6d 75 74 65 78 20 77 68 69 6c 65 20 74 68 65   mutex while the
b2e70 20 73 79 73 74 65 6d 20 69 73 20 75 6e 69 6e 69   system is unini
b2e80 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 73 74 61  tialized..*/.sta
b2e90 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 69  tic SQLITE_WSD i
b2ea0 6e 74 20 6d 75 74 65 78 49 73 49 6e 69 74 20 3d  nt mutexIsInit =
b2eb0 20 30 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51   0;.#endif /* SQ
b2ec0 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 0a  LITE_DEBUG */...
b2ed0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
b2ee0 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20  UTEX_OMIT./*.** 
b2ef0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  Initialize the m
b2f00 75 74 65 78 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a  utex system..*/.
b2f10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b2f20 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49  nt sqlite3MutexI
b2f30 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 69 6e  nit(void){ .  in
b2f40 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
b2f50 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ;.  if( !sqlite3
b2f60 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
b2f70 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 29  ex.xMutexAlloc )
b2f80 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  {.    /* If the 
b2f90 78 4d 75 74 65 78 41 6c 6c 6f 63 20 6d 65 74 68  xMutexAlloc meth
b2fa0 6f 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  od has not been 
b2fb0 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 75 73  set, then the us
b2fc0 65 72 20 64 69 64 20 6e 6f 74 0a 20 20 20 20 2a  er did not.    *
b2fd0 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6d 75 74 65  * install a mute
b2fe0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
b2ff0 20 76 69 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e   via sqlite3_con
b3000 66 69 67 28 29 20 70 72 69 6f 72 20 74 6f 20 0a  fig() prior to .
b3010 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 69      ** sqlite3_i
b3020 6e 69 74 69 61 6c 69 7a 65 28 29 20 62 65 69 6e  nitialize() bein
b3030 67 20 63 61 6c 6c 65 64 2e 20 54 68 69 73 20 62  g called. This b
b3040 6c 6f 63 6b 20 63 6f 70 69 65 73 20 70 6f 69 6e  lock copies poin
b3050 74 65 72 73 20 74 6f 0a 20 20 20 20 2a 2a 20 74  ters to.    ** t
b3060 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70 6c 65  he default imple
b3070 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74  mentation into t
b3080 68 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  he sqlite3Global
b3090 43 6f 6e 66 69 67 20 73 74 72 75 63 74 75 72 65  Config structure
b30a0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
b30b0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
b30c0 64 73 20 63 6f 6e 73 74 20 2a 70 46 72 6f 6d 3b  ds const *pFrom;
b30d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
b30e0 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70 54 6f 20  ex_methods *pTo 
b30f0 3d 20 26 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  = &sqlite3Global
b3100 43 6f 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 0a 20  Config.mutex;.. 
b3110 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c     if( sqlite3Gl
b3120 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65  obalConfig.bCore
b3130 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 20 20 70  Mutex ){.      p
b3140 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 44 65  From = sqlite3De
b3150 66 61 75 6c 74 4d 75 74 65 78 28 29 3b 0a 20 20  faultMutex();.  
b3160 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
b3170 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 4e 6f  From = sqlite3No
b3180 6f 70 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 7d  opMutex();.    }
b3190 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c  .    memcpy(pTo,
b31a0 20 70 46 72 6f 6d 2c 20 6f 66 66 73 65 74 6f 66   pFrom, offsetof
b31b0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d  (sqlite3_mutex_m
b31c0 65 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 41 6c  ethods, xMutexAl
b31d0 6c 6f 63 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70  loc));.    memcp
b31e0 79 28 26 70 54 6f 2d 3e 78 4d 75 74 65 78 46 72  y(&pTo->xMutexFr
b31f0 65 65 2c 20 26 70 46 72 6f 6d 2d 3e 78 4d 75 74  ee, &pFrom->xMut
b3200 65 78 46 72 65 65 2c 0a 20 20 20 20 20 20 20 20  exFree,.        
b3210 20 20 20 73 69 7a 65 6f 66 28 2a 70 54 6f 29 20     sizeof(*pTo) 
b3220 2d 20 6f 66 66 73 65 74 6f 66 28 73 71 6c 69 74  - offsetof(sqlit
b3230 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
b3240 2c 20 78 4d 75 74 65 78 46 72 65 65 29 29 3b 0a  , xMutexFree));.
b3250 20 20 20 20 70 54 6f 2d 3e 78 4d 75 74 65 78 41      pTo->xMutexA
b3260 6c 6c 6f 63 20 3d 20 70 46 72 6f 6d 2d 3e 78 4d  lloc = pFrom->xM
b3270 75 74 65 78 41 6c 6c 6f 63 3b 0a 20 20 7d 0a 20  utexAlloc;.  }. 
b3280 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f   rc = sqlite3Glo
b3290 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
b32a0 78 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a 0a 23  xMutexInit();..#
b32b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
b32c0 55 47 0a 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c  UG.  GLOBAL(int,
b32d0 20 6d 75 74 65 78 49 73 49 6e 69 74 29 20 3d 20   mutexIsInit) = 
b32e0 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74  1;.#endif..  ret
b32f0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
b3300 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 6d 75   Shutdown the mu
b3310 74 65 78 20 73 79 73 74 65 6d 2e 20 54 68 69 73  tex system. This
b3320 20 63 61 6c 6c 20 66 72 65 65 73 20 72 65 73 6f   call frees reso
b3330 75 72 63 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  urces allocated 
b3340 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74  by.** sqlite3Mut
b3350 65 78 49 6e 69 74 28 29 2e 0a 2a 2f 0a 53 51 4c  exInit()..*/.SQL
b3360 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b3370 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28  sqlite3MutexEnd(
b3380 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 20  void){.  int rc 
b3390 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
b33a0 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
b33b0 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
b33c0 74 65 78 45 6e 64 20 29 7b 0a 20 20 20 20 72 63  texEnd ){.    rc
b33d0 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
b33e0 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
b33f0 74 65 78 45 6e 64 28 29 3b 0a 20 20 7d 0a 0a 23  texEnd();.  }..#
b3400 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
b3410 55 47 0a 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c  UG.  GLOBAL(int,
b3420 20 6d 75 74 65 78 49 73 49 6e 69 74 29 20 3d 20   mutexIsInit) = 
b3430 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74  0;.#endif..  ret
b3440 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
b3450 20 52 65 74 72 69 65 76 65 20 61 20 70 6f 69 6e   Retrieve a poin
b3460 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20  ter to a static 
b3470 6d 75 74 65 78 20 6f 72 20 61 6c 6c 6f 63 61 74  mutex or allocat
b3480 65 20 61 20 6e 65 77 20 64 79 6e 61 6d 69 63 20  e a new dynamic 
b3490 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  one..*/.SQLITE_A
b34a0 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  PI sqlite3_mutex
b34b0 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   *sqlite3_mutex_
b34c0 61 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 23  alloc(int id){.#
b34d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
b34e0 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66  IT_AUTOINIT.  if
b34f0 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  ( sqlite3_initia
b3500 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20  lize() ) return 
b3510 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75  0;.#endif.  retu
b3520 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  rn sqlite3Global
b3530 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
b3540 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a  texAlloc(id);.}.
b3550 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b3560 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73  sqlite3_mutex *s
b3570 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
b3580 28 69 6e 74 20 69 64 29 7b 0a 20 20 69 66 28 20  (int id){.  if( 
b3590 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  !sqlite3GlobalCo
b35a0 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20  nfig.bCoreMutex 
b35b0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
b35c0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 47  .  }.  assert( G
b35d0 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75 74 65 78  LOBAL(int, mutex
b35e0 49 73 49 6e 69 74 29 20 29 3b 0a 20 20 72 65 74  IsInit) );.  ret
b35f0 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  urn sqlite3Globa
b3600 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d  lConfig.mutex.xM
b3610 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d  utexAlloc(id);.}
b3620 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 64  ../*.** Free a d
b3630 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e 0a 2a 2f  ynamic mutex..*/
b3640 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
b3650 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66   sqlite3_mutex_f
b3660 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ree(sqlite3_mute
b3670 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  x *p){.  if( p )
b3680 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  {.    sqlite3Glo
b3690 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
b36a0 78 4d 75 74 65 78 46 72 65 65 28 70 29 3b 0a 20  xMutexFree(p);. 
b36b0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61   }.}../*.** Obta
b36c0 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70 2e 20  in the mutex p. 
b36d0 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68  If some other th
b36e0 72 65 61 64 20 61 6c 72 65 61 64 79 20 68 61 73  read already has
b36f0 20 74 68 65 20 6d 75 74 65 78 2c 20 62 6c 6f 63   the mutex, bloc
b3700 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69 74 20 63 61  k.** until it ca
b3710 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a  n be obtained..*
b3720 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
b3730 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
b3740 65 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75  enter(sqlite3_mu
b3750 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  tex *p){.  if( p
b3760 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47   ){.    sqlite3G
b3770 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
b3780 78 2e 78 4d 75 74 65 78 45 6e 74 65 72 28 70 29  x.xMutexEnter(p)
b3790 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f  ;.  }.}../*.** O
b37a0 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20  btain the mutex 
b37b0 70 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  p. If successful
b37c0 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
b37d0 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  OK. Otherwise, i
b37e0 66 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 74 68 72  f another.** thr
b37f0 65 61 64 20 68 6f 6c 64 73 20 74 68 65 20 6d 75  ead holds the mu
b3800 74 65 78 20 61 6e 64 20 69 74 20 63 61 6e 6e 6f  tex and it canno
b3810 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 72  t be obtained, r
b3820 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
b3830 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  Y..*/.SQLITE_API
b3840 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74   int sqlite3_mut
b3850 65 78 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d  ex_try(sqlite3_m
b3860 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20  utex *p){.  int 
b3870 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
b3880 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 72    if( p ){.    r
b3890 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f  eturn sqlite3Glo
b38a0 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
b38b0 78 4d 75 74 65 78 54 72 79 28 70 29 3b 0a 20 20  xMutexTry(p);.  
b38c0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
b38d0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ../*.** The sqli
b38e0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
b38f0 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20  ) routine exits 
b3900 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73  a mutex that was
b3910 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 65   previously.** e
b3920 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61  ntered by the sa
b3930 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20  me thread.  The 
b3940 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65  behavior is unde
b3950 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74  fined if the mut
b3960 65 78 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75  ex .** is not cu
b3970 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 2e  rrently entered.
b3980 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   If a NULL point
b3990 65 72 20 69 73 20 70 61 73 73 65 64 20 61 73 20  er is passed as 
b39a0 61 6e 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  an argument.** t
b39b0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
b39c0 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49  a no-op..*/.SQLI
b39d0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
b39e0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
b39f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
b3a00 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  ){.  if( p ){.  
b3a10 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
b3a20 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74  onfig.mutex.xMut
b3a30 65 78 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a  exLeave(p);.  }.
b3a40 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  }..#ifndef NDEBU
b3a50 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  G./*.** The sqli
b3a60 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29  te3_mutex_held()
b3a70 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
b3a80 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75  ex_notheld() rou
b3a90 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65  tine are.** inte
b3aa0 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73  nded for use ins
b3ab0 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ide assert() sta
b3ac0 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49  tements..*/.SQLI
b3ad0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
b3ae0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71  e3_mutex_held(sq
b3af0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
b3b00 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c  .  return p==0 |
b3b10 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  | sqlite3GlobalC
b3b20 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74  onfig.mutex.xMut
b3b30 65 78 48 65 6c 64 28 70 29 3b 0a 7d 0a 53 51 4c  exHeld(p);.}.SQL
b3b40 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
b3b50 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
b3b60 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  d(sqlite3_mutex 
b3b70 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d  *p){.  return p=
b3b80 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f  =0 || sqlite3Glo
b3b90 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
b3ba0 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29  xMutexNotheld(p)
b3bb0 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64  ;.}.#endif..#end
b3bc0 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
b3bd0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54  QLITE_MUTEX_OMIT
b3be0 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
b3bf0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74  ***** End of mut
b3c00 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ex.c ***********
b3c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3c30 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
b3c40 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
b3c50 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 20 2a 2a   mutex_noop.c **
b3c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3c80 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
b3c90 20 4f 63 74 6f 62 65 72 20 30 37 0a 2a 2a 0a 2a   October 07.**.*
b3ca0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
b3cb0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
b3cc0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
b3cd0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
b3ce0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
b3cf0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
b3d00 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
b3d10 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
b3d20 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
b3d30 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
b3d40 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
b3d50 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
b3d60 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
b3d70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
b3d80 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
b3d90 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
b3da0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
b3db0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
b3dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
b3e00 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
b3e10 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73   the C functions
b3e20 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
b3e30 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  mutexes..**.** T
b3e40 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
b3e50 6f 6e 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  on in this file 
b3e60 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65  does not provide
b3e70 20 61 6e 79 20 6d 75 74 75 61 6c 0a 2a 2a 20 65   any mutual.** e
b3e80 78 63 6c 75 73 69 6f 6e 20 61 6e 64 20 69 73 20  xclusion and is 
b3e90 74 68 75 73 20 73 75 69 74 61 62 6c 65 20 66 6f  thus suitable fo
b3ea0 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 20 61 70  r use only in ap
b3eb0 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 74 68  plications.** th
b3ec0 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 69 6e  at use SQLite in
b3ed0 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64   a single thread
b3ee0 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20  .  The routines 
b3ef0 64 65 66 69 6e 65 64 0a 2a 2a 20 68 65 72 65 20  defined.** here 
b3f00 61 72 65 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72  are place-holder
b3f10 73 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  s.  Applications
b3f20 20 63 61 6e 20 73 75 62 73 74 69 74 75 74 65 20   can substitute 
b3f30 77 6f 72 6b 69 6e 67 0a 2a 2a 20 6d 75 74 65 78  working.** mutex
b3f40 20 72 6f 75 74 69 6e 65 73 20 61 74 20 73 74 61   routines at sta
b3f50 72 74 2d 74 69 6d 65 20 75 73 69 6e 67 20 74 68  rt-time using th
b3f60 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69  e.**.**     sqli
b3f70 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54  te3_config(SQLIT
b3f80 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 2c 2e  E_CONFIG_MUTEX,.
b3f90 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69 6e 74 65 72 66  ..).**.** interf
b3fa0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 6f  ace..**.** If co
b3fb0 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49  mpiled with SQLI
b3fc0 54 45 5f 44 45 42 55 47 2c 20 74 68 65 6e 20 61  TE_DEBUG, then a
b3fd0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 67 69 63 20  dditional logic 
b3fe0 69 73 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 74  is inserted.** t
b3ff0 68 61 74 20 64 6f 65 73 20 65 72 72 6f 72 20 63  hat does error c
b4000 68 65 63 6b 69 6e 67 20 6f 6e 20 6d 75 74 65 78  hecking on mutex
b4010 65 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  es to make sure 
b4020 74 68 65 79 20 61 72 65 20 62 65 69 6e 67 0a 2a  they are being.*
b4030 2a 20 63 61 6c 6c 65 64 20 63 6f 72 72 65 63 74  * called correct
b4040 6c 79 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ly..*/..#ifndef 
b4050 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49  SQLITE_MUTEX_OMI
b4060 54 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  T..#ifndef SQLIT
b4070 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 53 74  E_DEBUG./*.** St
b4080 75 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20  ub routines for 
b4090 61 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64  all mutex method
b40a0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
b40b0 75 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e  utines provide n
b40c0 6f 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69  o mutual exclusi
b40d0 6f 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63  on or error chec
b40e0 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  king..*/.static 
b40f0 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69  int noopMutexIni
b4100 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  t(void){ return 
b4110 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61  SQLITE_OK; }.sta
b4120 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65  tic int noopMute
b4130 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75  xEnd(void){ retu
b4140 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a  rn SQLITE_OK; }.
b4150 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
b4160 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65 78 41  utex *noopMutexA
b4170 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20 0a 20  lloc(int id){ . 
b4180 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
b4190 52 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  R(id);.  return 
b41a0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
b41b0 38 3b 20 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  8; .}.static voi
b41c0 64 20 6e 6f 6f 70 4d 75 74 65 78 46 72 65 65 28  d noopMutexFree(
b41d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
b41e0 29 7b 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  ){ UNUSED_PARAME
b41f0 54 45 52 28 70 29 3b 20 72 65 74 75 72 6e 3b 20  TER(p); return; 
b4200 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f  }.static void no
b4210 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c  opMutexEnter(sql
b4220 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20  ite3_mutex *p){ 
b4230 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
b4240 28 70 29 3b 20 72 65 74 75 72 6e 3b 20 7d 0a 73  (p); return; }.s
b4250 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75  tatic int noopMu
b4260 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d  texTry(sqlite3_m
b4270 75 74 65 78 20 2a 70 29 7b 0a 20 20 55 4e 55 53  utex *p){.  UNUS
b4280 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29 3b  ED_PARAMETER(p);
b4290 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
b42a0 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  _OK;.}.static vo
b42b0 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76  id noopMutexLeav
b42c0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
b42d0 2a 70 29 7b 20 55 4e 55 53 45 44 5f 50 41 52 41  *p){ UNUSED_PARA
b42e0 4d 45 54 45 52 28 70 29 3b 20 72 65 74 75 72 6e  METER(p); return
b42f0 3b 20 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ; }..SQLITE_PRIV
b4300 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ATE sqlite3_mute
b4310 78 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20  x_methods const 
b4320 2a 73 71 6c 69 74 65 33 4e 6f 6f 70 4d 75 74 65  *sqlite3NoopMute
b4330 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  x(void){.  stati
b4340 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
b4350 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d  mutex_methods sM
b4360 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 6e 6f 6f  utex = {.    noo
b4370 70 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20  pMutexInit,.    
b4380 6e 6f 6f 70 4d 75 74 65 78 45 6e 64 2c 0a 20 20  noopMutexEnd,.  
b4390 20 20 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f 63    noopMutexAlloc
b43a0 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 46  ,.    noopMutexF
b43b0 72 65 65 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74  ree,.    noopMut
b43c0 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 6e 6f 6f  exEnter,.    noo
b43d0 70 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 6e  pMutexTry,.    n
b43e0 6f 6f 70 4d 75 74 65 78 4c 65 61 76 65 2c 0a 0a  oopMutexLeave,..
b43f0 20 20 20 20 30 2c 0a 20 20 20 20 30 2c 0a 20 20      0,.    0,.  
b4400 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d  };..  return &sM
b4410 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  utex;.}.#endif /
b4420 2a 20 21 53 51 4c 49 54 45 5f 44 45 42 55 47 20  * !SQLITE_DEBUG 
b4430 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
b4440 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 49 6e  E_DEBUG./*.** In
b4450 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   this implementa
b4460 74 69 6f 6e 2c 20 65 72 72 6f 72 20 63 68 65 63  tion, error chec
b4470 6b 69 6e 67 20 69 73 20 70 72 6f 76 69 64 65 64  king is provided
b4480 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20   for testing.** 
b4490 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 70 75  and debugging pu
b44a0 72 70 6f 73 65 73 2e 20 20 54 68 65 20 6d 75 74  rposes.  The mut
b44b0 65 78 65 73 20 73 74 69 6c 6c 20 64 6f 20 6e 6f  exes still do no
b44c0 74 20 70 72 6f 76 69 64 65 20 61 6e 79 0a 2a 2a  t provide any.**
b44d0 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f   mutual exclusio
b44e0 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  n..*/../*.** The
b44f0 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2f   mutex object.*/
b4500 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
b4510 73 71 6c 69 74 65 33 5f 64 65 62 75 67 5f 6d 75  sqlite3_debug_mu
b4520 74 65 78 20 7b 0a 20 20 69 6e 74 20 69 64 3b 20  tex {.  int id; 
b4530 20 20 20 20 2f 2a 20 54 68 65 20 6d 75 74 65 78      /* The mutex
b4540 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 63   type */.  int c
b4550 6e 74 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  nt;    /* Number
b4560 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69 74 68   of entries with
b4570 6f 75 74 20 61 20 6d 61 74 63 68 69 6e 67 20 6c  out a matching l
b4580 65 61 76 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65  eave */.} sqlite
b4590 33 5f 64 65 62 75 67 5f 6d 75 74 65 78 3b 0a 0a  3_debug_mutex;..
b45a0 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
b45b0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61  3_mutex_held() a
b45c0 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nd sqlite3_mutex
b45d0 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69  _notheld() routi
b45e0 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64  ne are.** intend
b45f0 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64  ed for use insid
b4600 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  e assert() state
b4610 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ments..*/.static
b4620 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 48   int debugMutexH
b4630 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  eld(sqlite3_mute
b4640 78 20 2a 70 58 29 7b 0a 20 20 73 71 6c 69 74 65  x *pX){.  sqlite
b4650 33 5f 64 65 62 75 67 5f 6d 75 74 65 78 20 2a 70  3_debug_mutex *p
b4660 20 3d 20 28 73 71 6c 69 74 65 33 5f 64 65 62 75   = (sqlite3_debu
b4670 67 5f 6d 75 74 65 78 2a 29 70 58 3b 0a 20 20 72  g_mutex*)pX;.  r
b4680 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d  eturn p==0 || p-
b4690 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74 69 63  >cnt>0;.}.static
b46a0 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 4e   int debugMutexN
b46b0 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  otheld(sqlite3_m
b46c0 75 74 65 78 20 2a 70 58 29 7b 0a 20 20 73 71 6c  utex *pX){.  sql
b46d0 69 74 65 33 5f 64 65 62 75 67 5f 6d 75 74 65 78  ite3_debug_mutex
b46e0 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 64   *p = (sqlite3_d
b46f0 65 62 75 67 5f 6d 75 74 65 78 2a 29 70 58 3b 0a  ebug_mutex*)pX;.
b4700 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c    return p==0 ||
b4710 20 70 2d 3e 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f   p->cnt==0;.}../
b4720 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
b4730 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65  and deinitialize
b4740 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79   the mutex subsy
b4750 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  stem..*/.static 
b4760 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 49 6e  int debugMutexIn
b4770 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  it(void){ return
b4780 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74   SQLITE_OK; }.st
b4790 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75  atic int debugMu
b47a0 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65  texEnd(void){ re
b47b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
b47c0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
b47d0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
b47e0 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63  () routine alloc
b47f0 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75  ates a new.** mu
b4800 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20  tex and returns 
b4810 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e  a pointer to it.
b4820 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20    If it returns 
b4830 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61  NULL.** that mea
b4840 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20  ns that a mutex 
b4850 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c  could not be all
b4860 6f 63 61 74 65 64 2e 20 0a 2a 2f 0a 73 74 61 74  ocated. .*/.stat
b4870 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
b4880 20 2a 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f   *debugMutexAllo
b4890 63 28 69 6e 74 20 69 64 29 7b 0a 20 20 73 74 61  c(int id){.  sta
b48a0 74 69 63 20 73 71 6c 69 74 65 33 5f 64 65 62 75  tic sqlite3_debu
b48b0 67 5f 6d 75 74 65 78 20 61 53 74 61 74 69 63 5b  g_mutex aStatic[
b48c0 36 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 64 65  6];.  sqlite3_de
b48d0 62 75 67 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20  bug_mutex *pNew 
b48e0 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69  = 0;.  switch( i
b48f0 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  d ){.    case SQ
b4900 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a  LITE_MUTEX_FAST:
b4910 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
b4920 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
b4930 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d  : {.      pNew =
b4940 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73   sqlite3Malloc(s
b4950 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20  izeof(*pNew));. 
b4960 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b       if( pNew ){
b4970 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69  .        pNew->i
b4980 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20  d = id;.        
b4990 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20  pNew->cnt = 0;. 
b49a0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
b49b0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
b49c0 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61  fault: {.      a
b49d0 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30  ssert( id-2 >= 0
b49e0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
b49f0 28 20 69 64 2d 32 20 3c 20 28 69 6e 74 29 28 73  ( id-2 < (int)(s
b4a00 69 7a 65 6f 66 28 61 53 74 61 74 69 63 29 2f 73  izeof(aStatic)/s
b4a10 69 7a 65 6f 66 28 61 53 74 61 74 69 63 5b 30 5d  izeof(aStatic[0]
b4a20 29 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77  )) );.      pNew
b4a30 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64 2d 32   = &aStatic[id-2
b4a40 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 69  ];.      pNew->i
b4a50 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 62 72  d = id;.      br
b4a60 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
b4a70 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33   return (sqlite3
b4a80 5f 6d 75 74 65 78 2a 29 70 4e 65 77 3b 0a 7d 0a  _mutex*)pNew;.}.
b4a90 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
b4aa0 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20  ine deallocates 
b4ab0 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c  a previously all
b4ac0 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2f  ocated mutex..*/
b4ad0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62  .static void deb
b4ae0 75 67 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69  ugMutexFree(sqli
b4af0 74 65 33 5f 6d 75 74 65 78 20 2a 70 58 29 7b 0a  te3_mutex *pX){.
b4b00 20 20 73 71 6c 69 74 65 33 5f 64 65 62 75 67 5f    sqlite3_debug_
b4b10 6d 75 74 65 78 20 2a 70 20 3d 20 28 73 71 6c 69  mutex *p = (sqli
b4b20 74 65 33 5f 64 65 62 75 67 5f 6d 75 74 65 78 2a  te3_debug_mutex*
b4b30 29 70 58 3b 0a 20 20 61 73 73 65 72 74 28 20 70  )pX;.  assert( p
b4b40 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73  ->cnt==0 );.  as
b4b50 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
b4b60 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c  ITE_MUTEX_FAST |
b4b70 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  | p->id==SQLITE_
b4b80 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
b4b90 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
b4ba0 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  e(p);.}../*.** T
b4bb0 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
b4bc0 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c  _enter() and sql
b4bd0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
b4be0 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70   routines attemp
b4bf0 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20  t.** to enter a 
b4c00 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68  mutex.  If anoth
b4c10 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72  er thread is alr
b4c20 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20  eady within the 
b4c30 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65  mutex,.** sqlite
b4c40 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20  3_mutex_enter() 
b4c50 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73  will block and s
b4c60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
b4c70 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a  () will return.*
b4c80 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20  * SQLITE_BUSY.  
b4c90 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
b4ca0 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63  x_try() interfac
b4cb0 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  e returns SQLITE
b4cc0 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63  _OK.** upon succ
b4cd0 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d  essful entry.  M
b4ce0 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75  utexes created u
b4cf0 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45  sing SQLITE_MUTE
b4d00 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a  X_RECURSIVE can.
b4d10 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75  ** be entered mu
b4d20 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20  ltiple times by 
b4d30 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e  the same thread.
b4d40 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20    In such cases 
b4d50 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75  the,.** mutex mu
b4d60 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20  st be exited an 
b4d70 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  equal number of 
b4d80 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f  times before ano
b4d90 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63  ther thread.** c
b4da0 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68  an enter.  If th
b4db0 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72  e same thread tr
b4dc0 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79  ies to enter any
b4dd0 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d   other kind of m
b4de0 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61  utex.** more tha
b4df0 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61  n once, the beha
b4e00 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
b4e10 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
b4e20 64 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65  d debugMutexEnte
b4e30 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  r(sqlite3_mutex 
b4e40 2a 70 58 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  *pX){.  sqlite3_
b4e50 64 65 62 75 67 5f 6d 75 74 65 78 20 2a 70 20 3d  debug_mutex *p =
b4e60 20 28 73 71 6c 69 74 65 33 5f 64 65 62 75 67 5f   (sqlite3_debug_
b4e70 6d 75 74 65 78 2a 29 70 58 3b 0a 20 20 61 73 73  mutex*)pX;.  ass
b4e80 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
b4e90 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
b4ea0 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78  VE || debugMutex
b4eb0 4e 6f 74 68 65 6c 64 28 70 58 29 20 29 3b 0a 20  Notheld(pX) );. 
b4ec0 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73 74 61   p->cnt++;.}.sta
b4ed0 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74  tic int debugMut
b4ee0 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75  exTry(sqlite3_mu
b4ef0 74 65 78 20 2a 70 58 29 7b 0a 20 20 73 71 6c 69  tex *pX){.  sqli
b4f00 74 65 33 5f 64 65 62 75 67 5f 6d 75 74 65 78 20  te3_debug_mutex 
b4f10 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 64 65  *p = (sqlite3_de
b4f20 62 75 67 5f 6d 75 74 65 78 2a 29 70 58 3b 0a 20  bug_mutex*)pX;. 
b4f30 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d   assert( p->id==
b4f40 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
b4f50 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d  URSIVE || debugM
b4f60 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 58 29 20  utexNotheld(pX) 
b4f70 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20  );.  p->cnt++;. 
b4f80 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
b4f90 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  K;.}../*.** The 
b4fa0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
b4fb0 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78  ave() routine ex
b4fc0 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74  its a mutex that
b4fd0 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73   was.** previous
b4fe0 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68  ly entered by th
b4ff0 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20  e same thread.  
b5000 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20  The behavior.** 
b5010 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20  is undefined if 
b5020 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74  the mutex is not
b5030 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72   currently enter
b5040 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20  ed or.** is not 
b5050 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61  currently alloca
b5060 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c  ted.  SQLite wil
b5070 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65  l never do eithe
b5080 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
b5090 64 20 64 65 62 75 67 4d 75 74 65 78 4c 65 61 76  d debugMutexLeav
b50a0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
b50b0 2a 70 58 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  *pX){.  sqlite3_
b50c0 64 65 62 75 67 5f 6d 75 74 65 78 20 2a 70 20 3d  debug_mutex *p =
b50d0 20 28 73 71 6c 69 74 65 33 5f 64 65 62 75 67 5f   (sqlite3_debug_
b50e0 6d 75 74 65 78 2a 29 70 58 3b 0a 20 20 61 73 73  mutex*)pX;.  ass
b50f0 65 72 74 28 20 64 65 62 75 67 4d 75 74 65 78 48  ert( debugMutexH
b5100 65 6c 64 28 70 58 29 20 29 3b 0a 20 20 70 2d 3e  eld(pX) );.  p->
b5110 63 6e 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28  cnt--;.  assert(
b5120 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
b5130 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c  UTEX_RECURSIVE |
b5140 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68  | debugMutexNoth
b5150 65 6c 64 28 70 58 29 20 29 3b 0a 7d 0a 0a 53 51  eld(pX) );.}..SQ
b5160 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
b5170 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
b5180 64 73 20 63 6f 6e 73 74 20 2a 73 71 6c 69 74 65  ds const *sqlite
b5190 33 4e 6f 6f 70 4d 75 74 65 78 28 76 6f 69 64 29  3NoopMutex(void)
b51a0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
b51b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
b51c0 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20  ethods sMutex = 
b51d0 7b 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78  {.    debugMutex
b51e0 49 6e 69 74 2c 0a 20 20 20 20 64 65 62 75 67 4d  Init,.    debugM
b51f0 75 74 65 78 45 6e 64 2c 0a 20 20 20 20 64 65 62  utexEnd,.    deb
b5200 75 67 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20  ugMutexAlloc,.  
b5210 20 20 64 65 62 75 67 4d 75 74 65 78 46 72 65 65    debugMutexFree
b5220 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78  ,.    debugMutex
b5230 45 6e 74 65 72 2c 0a 20 20 20 20 64 65 62 75 67  Enter,.    debug
b5240 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 64 65  MutexTry,.    de
b5250 62 75 67 4d 75 74 65 78 4c 65 61 76 65 2c 0a 0a  bugMutexLeave,..
b5260 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 48 65      debugMutexHe
b5270 6c 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74  ld,.    debugMut
b5280 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a  exNotheld.  };..
b5290 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78    return &sMutex
b52a0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
b52b0 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 2f  LITE_DEBUG */../
b52c0 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64  *.** If compiled
b52d0 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4d 55 54   with SQLITE_MUT
b52e0 45 58 5f 4e 4f 4f 50 2c 20 74 68 65 6e 20 74 68  EX_NOOP, then th
b52f0 65 20 6e 6f 2d 6f 70 20 6d 75 74 65 78 20 69 6d  e no-op mutex im
b5300 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20  plementation.** 
b5310 69 73 20 75 73 65 64 20 72 65 67 61 72 64 6c 65  is used regardle
b5320 73 73 20 6f 66 20 74 68 65 20 72 75 6e 2d 74 69  ss of the run-ti
b5330 6d 65 20 74 68 72 65 61 64 73 61 66 65 74 79 20  me threadsafety 
b5340 73 65 74 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64  setting..*/.#ifd
b5350 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ef SQLITE_MUTEX_
b5360 4e 4f 4f 50 0a 53 51 4c 49 54 45 5f 50 52 49 56  NOOP.SQLITE_PRIV
b5370 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ATE sqlite3_mute
b5380 78 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20  x_methods const 
b5390 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d  *sqlite3DefaultM
b53a0 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 72 65  utex(void){.  re
b53b0 74 75 72 6e 20 73 71 6c 69 74 65 33 4e 6f 6f 70  turn sqlite3Noop
b53c0 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 69  Mutex();.}.#endi
b53d0 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c  f /* defined(SQL
b53e0 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20  ITE_MUTEX_NOOP) 
b53f0 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  */.#endif /* !de
b5400 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54  fined(SQLITE_MUT
b5410 45 58 5f 4f 4d 49 54 29 20 2a 2f 0a 0a 2f 2a 2a  EX_OMIT) */../**
b5420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
b5430 20 6f 66 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63   of mutex_noop.c
b5440 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
b5450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
b5470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
b5480 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e  in file mutex_un
b5490 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ix.c ***********
b54a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b54b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
b54c0 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32  ** 2007 August 2
b54d0 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  8.**.** The auth
b54e0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
b54f0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
b5500 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
b5510 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
b5520 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
b5530 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
b5540 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
b5550 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
b5560 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
b5570 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
b5580 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
b5590 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
b55a0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
b55b0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
b55c0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
b55d0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
b55e0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
b55f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5630 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
b5640 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e  ntains the C fun
b5650 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c  ctions that impl
b5660 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f  ement mutexes fo
b5670 72 20 70 74 68 72 65 61 64 73 0a 2a 2f 0a 0a 2f  r pthreads.*/../
b5680 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e  *.** The code in
b5690 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e   this file is on
b56a0 6c 79 20 75 73 65 64 20 69 66 20 77 65 20 61 72  ly used if we ar
b56b0 65 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 72 65  e compiling thre
b56c0 61 64 73 61 66 65 0a 2a 2a 20 75 6e 64 65 72 20  adsafe.** under 
b56d0 75 6e 69 78 20 77 69 74 68 20 70 74 68 72 65 61  unix with pthrea
b56e0 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  ds..**.** Note t
b56f0 68 61 74 20 74 68 69 73 20 69 6d 70 6c 65 6d 65  hat this impleme
b5700 6e 74 61 74 69 6f 6e 20 72 65 71 75 69 72 65 73  ntation requires
b5710 20 61 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 74   a version of pt
b5720 68 72 65 61 64 73 20 74 68 61 74 0a 2a 2a 20 73  hreads that.** s
b5730 75 70 70 6f 72 74 73 20 72 65 63 75 72 73 69 76  upports recursiv
b5740 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 69  e mutexes..*/.#i
b5750 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45  fdef SQLITE_MUTE
b5760 58 5f 50 54 48 52 45 41 44 53 0a 0a 23 69 6e 63  X_PTHREADS..#inc
b5770 6c 75 64 65 20 3c 70 74 68 72 65 61 64 2e 68 3e  lude <pthread.h>
b5780 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ../*.** The sqli
b5790 74 65 33 5f 6d 75 74 65 78 2e 69 64 2c 20 73 71  te3_mutex.id, sq
b57a0 6c 69 74 65 33 5f 6d 75 74 65 78 2e 6e 52 65 66  lite3_mutex.nRef
b57b0 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  , and sqlite3_mu
b57c0 74 65 78 2e 6f 77 6e 65 72 20 66 69 65 6c 64 73  tex.owner fields
b57d0 0a 2a 2a 20 61 72 65 20 6e 65 63 65 73 73 61 72  .** are necessar
b57e0 79 20 75 6e 64 65 72 20 74 77 6f 20 63 6f 6e 64  y under two cond
b57f0 69 64 74 69 6f 6e 73 3a 20 20 28 31 29 20 44 65  idtions:  (1) De
b5800 62 75 67 20 62 75 69 6c 64 73 20 61 6e 64 20 28  bug builds and (
b5810 32 29 20 75 73 69 6e 67 0a 2a 2a 20 68 6f 6d 65  2) using.** home
b5820 2d 67 72 6f 77 6e 20 6d 75 74 65 78 65 73 2e 20  -grown mutexes. 
b5830 20 45 6e 63 61 70 73 75 6c 61 74 65 20 74 68 65   Encapsulate the
b5840 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 6e  se conditions in
b5850 74 6f 20 61 20 73 69 6e 67 6c 65 20 23 64 65 66  to a single #def
b5860 69 6e 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69  ine..*/.#if defi
b5870 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
b5880 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
b5890 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45  ITE_HOMEGROWN_RE
b58a0 43 55 52 53 49 56 45 5f 4d 55 54 45 58 29 0a 23  CURSIVE_MUTEX).#
b58b0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
b58c0 55 54 45 58 5f 4e 52 45 46 20 31 0a 23 65 6c 73  UTEX_NREF 1.#els
b58d0 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  e.# define SQLIT
b58e0 45 5f 4d 55 54 45 58 5f 4e 52 45 46 20 30 0a 23  E_MUTEX_NREF 0.#
b58f0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  endif../*.** Eac
b5900 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  h recursive mute
b5910 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  x is an instance
b5920 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
b5930 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  g structure..*/.
b5940 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d  struct sqlite3_m
b5950 75 74 65 78 20 7b 0a 20 20 70 74 68 72 65 61 64  utex {.  pthread
b5960 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65 78 3b 20  _mutex_t mutex; 
b5970 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e      /* Mutex con
b5980 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63  trolling the loc
b5990 6b 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  k */.#if SQLITE_
b59a0 4d 55 54 45 58 5f 4e 52 45 46 0a 20 20 69 6e 74  MUTEX_NREF.  int
b59b0 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20   id;            
b59c0 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78          /* Mutex
b59d0 20 74 79 70 65 20 2a 2f 0a 20 20 76 6f 6c 61 74   type */.  volat
b59e0 69 6c 65 20 69 6e 74 20 6e 52 65 66 3b 20 20 20  ile int nRef;   
b59f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
b5a00 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f 0a  of entrances */.
b5a10 20 20 76 6f 6c 61 74 69 6c 65 20 70 74 68 72 65    volatile pthre
b5a20 61 64 5f 74 20 6f 77 6e 65 72 3b 20 20 2f 2a 20  ad_t owner;  /* 
b5a30 54 68 72 65 61 64 20 74 68 61 74 20 69 73 20 77  Thread that is w
b5a40 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74 65 78  ithin this mutex
b5a50 20 2a 2f 0a 20 20 69 6e 74 20 74 72 61 63 65 3b   */.  int trace;
b5a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b5a70 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63   /* True to trac
b5a80 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 65 6e  e changes */.#en
b5a90 64 69 66 0a 7d 3b 0a 23 69 66 20 53 51 4c 49 54  dif.};.#if SQLIT
b5aa0 45 5f 4d 55 54 45 58 5f 4e 52 45 46 0a 23 64 65  E_MUTEX_NREF.#de
b5ab0 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54  fine SQLITE3_MUT
b5ac0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b  EX_INITIALIZER {
b5ad0 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49   PTHREAD_MUTEX_I
b5ae0 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30  NITIALIZER, 0, 0
b5af0 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 2c 20  , (pthread_t)0, 
b5b00 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  0 }.#else.#defin
b5b10 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f  e SQLITE3_MUTEX_
b5b20 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 50 54  INITIALIZER { PT
b5b30 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54  HREAD_MUTEX_INIT
b5b40 49 41 4c 49 5a 45 52 20 7d 0a 23 65 6e 64 69 66  IALIZER }.#endif
b5b50 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ../*.** The sqli
b5b60 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29  te3_mutex_held()
b5b70 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
b5b80 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75  ex_notheld() rou
b5b90 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65  tine are.** inte
b5ba0 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 6c  nded for use onl
b5bb0 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28  y inside assert(
b5bc0 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 4f  ) statements.  O
b5bd0 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f 72 6d 73  n some platforms
b5be0 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 69 67 68 74  ,.** there might
b5bf0 20 62 65 20 72 61 63 65 20 63 6f 6e 64 69 74 69   be race conditi
b5c00 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 63 61 75  ons that can cau
b5c10 73 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  se these routine
b5c20 73 20 74 6f 0a 2a 2a 20 64 65 6c 69 76 65 72 20  s to.** deliver 
b5c30 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c 74  incorrect result
b5c40 73 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c 61  s.  In particula
b5c50 72 2c 20 69 66 20 70 74 68 72 65 61 64 5f 65 71  r, if pthread_eq
b5c60 75 61 6c 28 29 20 69 73 0a 2a 2a 20 6e 6f 74 20  ual() is.** not 
b5c70 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74  an atomic operat
b5c80 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 73 65 20  ion, then these 
b5c90 72 6f 75 74 69 6e 65 73 20 6d 69 67 68 74 20 64  routines might d
b5ca0 65 6c 69 76 65 72 79 0a 2a 2a 20 69 6e 63 6f 72  elivery.** incor
b5cb0 72 65 63 74 20 72 65 73 75 6c 74 73 2e 20 20 4f  rect results.  O
b5cc0 6e 20 6d 6f 73 74 20 70 6c 61 74 66 6f 72 6d 73  n most platforms
b5cd0 2c 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  , pthread_equal(
b5ce0 29 20 69 73 20 61 20 0a 2a 2a 20 63 6f 6d 70 61  ) is a .** compa
b5cf0 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20 69 6e 74  rison of two int
b5d00 65 67 65 72 73 20 61 6e 64 20 69 73 20 74 68 65  egers and is the
b5d10 72 65 66 6f 72 65 20 61 74 6f 6d 69 63 2e 20 20  refore atomic.  
b5d20 42 75 74 20 77 65 20 61 72 65 0a 2a 2a 20 74 6f  But we are.** to
b5d30 6c 64 20 74 68 61 74 20 48 50 55 58 20 69 73 20  ld that HPUX is 
b5d40 6e 6f 74 20 73 75 63 68 20 61 20 70 6c 61 74 66  not such a platf
b5d50 6f 72 6d 2e 20 20 49 66 20 73 6f 2c 20 74 68 65  orm.  If so, the
b5d60 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  n these routines
b5d70 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 61 6c 77  .** will not alw
b5d80 61 79 73 20 77 6f 72 6b 20 63 6f 72 72 65 63 74  ays work correct
b5d90 6c 79 20 6f 6e 20 48 50 55 58 2e 0a 2a 2a 0a 2a  ly on HPUX..**.*
b5da0 2a 20 4f 6e 20 74 68 6f 73 65 20 70 6c 61 74 66  * On those platf
b5db0 6f 72 6d 73 20 77 68 65 72 65 20 70 74 68 72 65  orms where pthre
b5dc0 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 6e 6f  ad_equal() is no
b5dd0 74 20 61 74 6f 6d 69 63 2c 20 53 51 4c 69 74 65  t atomic, SQLite
b5de0 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 6f  .** should be co
b5df0 6d 70 69 6c 65 64 20 77 69 74 68 6f 75 74 20 2d  mpiled without -
b5e00 44 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e  DSQLITE_DEBUG an
b5e10 64 20 77 69 74 68 20 2d 44 4e 44 45 42 55 47 20  d with -DNDEBUG 
b5e20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 73 75 72 65 20  to.** make sure 
b5e30 6e 6f 20 61 73 73 65 72 74 28 29 20 73 74 61 74  no assert() stat
b5e40 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75  ements are evalu
b5e50 61 74 65 64 20 61 6e 64 20 68 65 6e 63 65 20 74  ated and hence t
b5e60 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  hese.** routines
b5e70 20 61 72 65 20 6e 65 76 65 72 20 63 61 6c 6c 65   are never calle
b5e80 64 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  d..*/.#if !defin
b5e90 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65  ed(NDEBUG) || de
b5ea0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
b5eb0 55 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 70  UG).static int p
b5ec0 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28  threadMutexHeld(
b5ed0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
b5ee0 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e  ){.  return (p->
b5ef0 6e 52 65 66 21 3d 30 20 26 26 20 70 74 68 72 65  nRef!=0 && pthre
b5f00 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65  ad_equal(p->owne
b5f10 72 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28  r, pthread_self(
b5f20 29 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  )));.}.static in
b5f30 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f  t pthreadMutexNo
b5f40 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75  theld(sqlite3_mu
b5f50 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  tex *p){.  retur
b5f60 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20  n p->nRef==0 || 
b5f70 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d  pthread_equal(p-
b5f80 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64 5f  >owner, pthread_
b5f90 73 65 6c 66 28 29 29 3d 3d 30 3b 0a 7d 0a 23 65  self())==0;.}.#e
b5fa0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  ndif../*.** Init
b5fb0 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69  ialize and deini
b5fc0 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65  tialize the mute
b5fd0 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a  x subsystem..*/.
b5fe0 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65  static int pthre
b5ff0 61 64 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64  adMutexInit(void
b6000 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ){ return SQLITE
b6010 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e  _OK; }.static in
b6020 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e  t pthreadMutexEn
b6030 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  d(void){ return 
b6040 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a  SQLITE_OK; }../*
b6050 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
b6060 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f  mutex_alloc() ro
b6070 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20  utine allocates 
b6080 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61  a new.** mutex a
b6090 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  nd returns a poi
b60a0 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20  nter to it.  If 
b60b0 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a  it returns NULL.
b60c0 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  ** that means th
b60d0 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64  at a mutex could
b60e0 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65   not be allocate
b60f0 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69  d.  SQLite.** wi
b6100 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74  ll unwind its st
b6110 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ack and return a
b6120 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61 72  n error.  The ar
b6130 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c  gument.** to sql
b6140 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
b6150 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65  () is one of the
b6160 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74  se integer const
b6170 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e  ants:.**.** <ul>
b6180 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
b6190 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 3c  _MUTEX_FAST.** <
b61a0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
b61b0 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 3c  X_RECURSIVE.** <
b61c0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
b61d0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 0a  X_STATIC_MASTER.
b61e0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
b61f0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
b6200 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
b6210 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45  _MUTEX_STATIC_ME
b6220 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  M2.** <li>  SQLI
b6230 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
b6240 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  PRNG.** <li>  SQ
b6250 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
b6260 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20 53  C_LRU.** <li>  S
b6270 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
b6280 49 43 5f 50 4d 45 4d 0a 2a 2a 20 3c 2f 75 6c 3e  IC_PMEM.** </ul>
b6290 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
b62a0 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63   two constants c
b62b0 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  ause sqlite3_mut
b62c0 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72  ex_alloc() to cr
b62d0 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75  eate.** a new mu
b62e0 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75  tex.  The new mu
b62f0 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 65  tex is recursive
b6300 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54   when SQLITE_MUT
b6310 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20  EX_RECURSIVE.** 
b6320 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20  is used but not 
b6330 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77  necessarily so w
b6340 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  hen SQLITE_MUTEX
b6350 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a  _FAST is used..*
b6360 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c  * The mutex impl
b6370 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20  ementation does 
b6380 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65  not need to make
b6390 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a   a distinction.*
b63a0 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45  * between SQLITE
b63b0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
b63c0 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45   and SQLITE_MUTE
b63d0 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f 65  X_FAST if it doe
b63e0 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f  s.** not want to
b63f0 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 69  .  But SQLite wi
b6400 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20  ll only request 
b6410 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  a recursive mute
b6420 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68  x in.** cases wh
b6430 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65  ere it really ne
b6440 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66  eds one.  If a f
b6450 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73  aster non-recurs
b6460 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70  ive mutex.** imp
b6470 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61  lementation is a
b6480 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20  vailable on the 
b6490 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74  host platform, t
b64a0 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
b64b0 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75  em.** might retu
b64c0 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 20  rn such a mutex 
b64d0 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53  in response to S
b64e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
b64f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65  ..**.** The othe
b6500 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65  r allowed parame
b6510 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  ters to sqlite3_
b6520 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61  mutex_alloc() ea
b6530 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70  ch return.** a p
b6540 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74  ointer to a stat
b6550 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d  ic preexisting m
b6560 75 74 65 78 2e 20 20 53 69 78 20 73 74 61 74 69  utex.  Six stati
b6570 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a  c mutexes are.**
b6580 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 72   used by the cur
b6590 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20  rent version of 
b65a0 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20  SQLite.  Future 
b65b0 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  versions of SQLi
b65c0 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64  te.** may add ad
b65d0 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20  ditional static 
b65e0 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 63  mutexes.  Static
b65f0 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f 72   mutexes are for
b6600 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65   internal.** use
b6610 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e   by SQLite only.
b6620 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74    Applications t
b6630 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d  hat use SQLite m
b6640 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a  utexes should.**
b6650 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79   use only the dy
b6660 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65  namic mutexes re
b6670 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45  turned by SQLITE
b6680 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a  _MUTEX_FAST or.*
b6690 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  * SQLITE_MUTEX_R
b66a0 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20  ECURSIVE..**.** 
b66b0 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65  Note that if one
b66c0 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20   of the dynamic 
b66d0 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73  mutex parameters
b66e0 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46   (SQLITE_MUTEX_F
b66f0 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45  AST.** or SQLITE
b6700 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
b6710 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73  ) is used then s
b6720 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
b6730 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20  oc().** returns 
b6740 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65  a different mute
b6750 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e  x on every call.
b6760 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 74    But for the st
b6770 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 74  atic .** mutex t
b6780 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d  ypes, the same m
b6790 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 64  utex is returned
b67a0 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74   on every call t
b67b0 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 73  hat has.** the s
b67c0 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e  ame type number.
b67d0 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74  .*/.static sqlit
b67e0 65 33 5f 6d 75 74 65 78 20 2a 70 74 68 72 65 61  e3_mutex *pthrea
b67f0 64 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20  dMutexAlloc(int 
b6800 69 54 79 70 65 29 7b 0a 20 20 73 74 61 74 69 63  iType){.  static
b6810 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 73   sqlite3_mutex s
b6820 74 61 74 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d  taticMutexes[] =
b6830 20 7b 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d   {.    SQLITE3_M
b6840 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
b6850 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55  ,.    SQLITE3_MU
b6860 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
b6870 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54  .    SQLITE3_MUT
b6880 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a  EX_INITIALIZER,.
b6890 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45      SQLITE3_MUTE
b68a0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20  X_INITIALIZER,. 
b68b0 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58     SQLITE3_MUTEX
b68c0 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20  _INITIALIZER,.  
b68d0 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f    SQLITE3_MUTEX_
b68e0 49 4e 49 54 49 41 4c 49 5a 45 52 0a 20 20 7d 3b  INITIALIZER.  };
b68f0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
b6900 20 2a 70 3b 0a 20 20 73 77 69 74 63 68 28 20 69   *p;.  switch( i
b6910 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65  Type ){.    case
b6920 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
b6930 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20  CURSIVE: {.     
b6940 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c   p = sqlite3Mall
b6950 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a  ocZero( sizeof(*
b6960 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  p) );.      if( 
b6970 70 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  p ){.#ifdef SQLI
b6980 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43  TE_HOMEGROWN_REC
b6990 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 20  URSIVE_MUTEX.   
b69a0 20 20 20 20 20 2f 2a 20 49 66 20 72 65 63 75 72       /* If recur
b69b0 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 65  sive mutexes are
b69c0 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20   not available, 
b69d0 77 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 0a  we will have to.
b69e0 20 20 20 20 20 20 20 20 2a 2a 20 62 75 69 6c 64          ** build
b69f0 20 6f 75 72 20 6f 77 6e 2e 20 20 53 65 65 20 62   our own.  See b
b6a00 65 6c 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 20  elow. */.       
b6a10 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69   pthread_mutex_i
b6a20 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 30  nit(&p->mutex, 0
b6a30 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20  );.#else.       
b6a40 20 2f 2a 20 55 73 65 20 61 20 72 65 63 75 72 73   /* Use a recurs
b6a50 69 76 65 20 6d 75 74 65 78 20 69 66 20 69 74 20  ive mutex if it 
b6a60 69 73 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a  is available */.
b6a70 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f          pthread_
b6a80 6d 75 74 65 78 61 74 74 72 5f 74 20 72 65 63 75  mutexattr_t recu
b6a90 72 73 69 76 65 41 74 74 72 3b 0a 20 20 20 20 20  rsiveAttr;.     
b6aa0 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78     pthread_mutex
b6ab0 61 74 74 72 5f 69 6e 69 74 28 26 72 65 63 75 72  attr_init(&recur
b6ac0 73 69 76 65 41 74 74 72 29 3b 0a 20 20 20 20 20  siveAttr);.     
b6ad0 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78     pthread_mutex
b6ae0 61 74 74 72 5f 73 65 74 74 79 70 65 28 26 72 65  attr_settype(&re
b6af0 63 75 72 73 69 76 65 41 74 74 72 2c 20 50 54 48  cursiveAttr, PTH
b6b00 52 45 41 44 5f 4d 55 54 45 58 5f 52 45 43 55 52  READ_MUTEX_RECUR
b6b10 53 49 56 45 29 3b 0a 20 20 20 20 20 20 20 20 70  SIVE);.        p
b6b20 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e 69  thread_mutex_ini
b6b30 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 26 72 65  t(&p->mutex, &re
b6b40 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 20 20  cursiveAttr);.  
b6b50 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75        pthread_mu
b6b60 74 65 78 61 74 74 72 5f 64 65 73 74 72 6f 79 28  texattr_destroy(
b6b70 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 3b  &recursiveAttr);
b6b80 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
b6b90 54 45 5f 4d 55 54 45 58 5f 4e 52 45 46 0a 20 20  TE_MUTEX_NREF.  
b6ba0 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54        p->id = iT
b6bb0 79 70 65 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  ype;.#endif.    
b6bc0 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
b6bd0 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
b6be0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
b6bf0 54 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73  T: {.      p = s
b6c00 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
b6c10 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a  ( sizeof(*p) );.
b6c20 20 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a 23        if( p ){.#
b6c30 69 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  if SQLITE_MUTEX_
b6c40 4e 52 45 46 0a 20 20 20 20 20 20 20 20 70 2d 3e  NREF.        p->
b6c50 69 64 20 3d 20 69 54 79 70 65 3b 0a 23 65 6e 64  id = iType;.#end
b6c60 69 66 0a 20 20 20 20 20 20 20 20 70 74 68 72 65  if.        pthre
b6c70 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70  ad_mutex_init(&p
b6c80 2d 3e 6d 75 74 65 78 2c 20 30 29 3b 0a 20 20 20  ->mutex, 0);.   
b6c90 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
b6ca0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
b6cb0 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ult: {.      ass
b6cc0 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20  ert( iType-2 >= 
b6cd0 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
b6ce0 74 28 20 69 54 79 70 65 2d 32 20 3c 20 41 72 72  t( iType-2 < Arr
b6cf0 61 79 53 69 7a 65 28 73 74 61 74 69 63 4d 75 74  aySize(staticMut
b6d00 65 78 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70  exes) );.      p
b6d10 20 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78 65   = &staticMutexe
b6d20 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 23 69 66 20  s[iType-2];.#if 
b6d30 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 52 45  SQLITE_MUTEX_NRE
b6d40 46 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20  F.      p->id = 
b6d50 69 54 79 70 65 3b 0a 23 65 6e 64 69 66 0a 20 20  iType;.#endif.  
b6d60 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
b6d70 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
b6d80 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .}.../*.** This 
b6d90 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61  routine dealloca
b6da0 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79  tes a previously
b6db0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75  .** allocated mu
b6dc0 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20  tex.  SQLite is 
b6dd0 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c  careful to deall
b6de0 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d  ocate every.** m
b6df0 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c  utex that it all
b6e00 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  ocates..*/.stati
b6e10 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75  c void pthreadMu
b6e20 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f  texFree(sqlite3_
b6e30 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
b6e40 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20  ert( p->nRef==0 
b6e50 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
b6e60 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
b6e70 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d  _FAST || p->id==
b6e80 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
b6e90 55 52 53 49 56 45 20 29 3b 0a 20 20 70 74 68 72  URSIVE );.  pthr
b6ea0 65 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72 6f  ead_mutex_destro
b6eb0 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  y(&p->mutex);.  
b6ec0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
b6ed0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  .}../*.** The sq
b6ee0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
b6ef0 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  r() and sqlite3_
b6f00 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74  mutex_try() rout
b6f10 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20  ines attempt.** 
b6f20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78  to enter a mutex
b6f30 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  .  If another th
b6f40 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20  read is already 
b6f50 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78  within the mutex
b6f60 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74  ,.** sqlite3_mut
b6f70 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20  ex_enter() will 
b6f80 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65  block and sqlite
b6f90 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69  3_mutex_try() wi
b6fa0 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  ll return.** SQL
b6fb0 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73  ITE_BUSY.  The s
b6fc0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
b6fd0 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
b6fe0 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a  urns SQLITE_OK.*
b6ff0 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75  * upon successfu
b7000 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65  l entry.  Mutexe
b7010 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  s created using 
b7020 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
b7030 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65  URSIVE can.** be
b7040 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c   entered multipl
b7050 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73  e times by the s
b7060 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20  ame thread.  In 
b7070 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a  such cases the,.
b7080 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65  ** mutex must be
b7090 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c   exited an equal
b70a0 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
b70b0 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20   before another 
b70c0 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e  thread.** can en
b70d0 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d  ter.  If the sam
b70e0 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74  e thread tries t
b70f0 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65  o enter any othe
b7100 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a  r kind of mutex.
b7110 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ** more than onc
b7120 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  e, the behavior 
b7130 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f  is undefined..*/
b7140 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68  .static void pth
b7150 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 28 73  readMutexEnter(s
b7160 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
b7170 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  {.  assert( p->i
b7180 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
b7190 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68  RECURSIVE || pth
b71a0 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64  readMutexNotheld
b71b0 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53  (p) );..#ifdef S
b71c0 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f  QLITE_HOMEGROWN_
b71d0 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a  RECURSIVE_MUTEX.
b71e0 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76    /* If recursiv
b71f0 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f  e mutexes are no
b7200 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65  t available, the
b7210 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f  n we have to gro
b7220 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20  w.  ** our own. 
b7230 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   This implementa
b7240 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61  tion assumes tha
b7250 74 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  t pthread_equal(
b7260 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63  ).  ** is atomic
b7270 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f   - that it canno
b7280 74 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e  t be deceived in
b7290 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66  to thinking self
b72a0 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e  .  ** and p->own
b72b0 65 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20  er are equal if 
b72c0 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73  p->owner changes
b72d0 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c   between two val
b72e0 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72  ues.  ** that ar
b72f0 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73  e not equal to s
b7300 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f  elf while the co
b7310 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69  mparison is taki
b7320 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54  ng place..  ** T
b7330 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
b7340 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20  on also assumes 
b7350 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65  a coherent cache
b7360 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65   - that .  ** se
b7370 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73  parate processes
b7380 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66   cannot read dif
b7390 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72  ferent values fr
b73a0 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a  om the same.  **
b73b0 20 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20   address at the 
b73c0 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65  same time.  If e
b73d0 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74  ither of these t
b73e0 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20  wo conditions.  
b73f0 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20  ** are not met, 
b7400 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73  then the mutexes
b7410 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70   will fail and p
b7420 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73  roblems will res
b7430 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20  ult..  */.  {.  
b7440 20 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66    pthread_t self
b7450 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28   = pthread_self(
b7460 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52  );.    if( p->nR
b7470 65 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f  ef>0 && pthread_
b7480 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20  equal(p->owner, 
b7490 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70  self) ){.      p
b74a0 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 65  ->nRef++;.    }e
b74b0 6c 73 65 7b 0a 20 20 20 20 20 20 70 74 68 72 65  lse{.      pthre
b74c0 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70  ad_mutex_lock(&p
b74d0 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ->mutex);.      
b74e0 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d  assert( p->nRef=
b74f0 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f  =0 );.      p->o
b7500 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20  wner = self;.   
b7510 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a     p->nRef = 1;.
b7520 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a      }.  }.#else.
b7530 20 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69    /* Use the bui
b7540 6c 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20  lt-in recursive 
b7550 6d 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20  mutexes if they 
b7560 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20  are available.. 
b7570 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 6d 75   */.  pthread_mu
b7580 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74  tex_lock(&p->mut
b7590 65 78 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  ex);.#if SQLITE_
b75a0 4d 55 54 45 58 5f 4e 52 45 46 0a 20 20 61 73 73  MUTEX_NREF.  ass
b75b0 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 7c  ert( p->nRef>0 |
b75c0 7c 20 70 2d 3e 6f 77 6e 65 72 3d 3d 30 20 29 3b  | p->owner==0 );
b75d0 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74  .  p->owner = pt
b75e0 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20  hread_self();.  
b75f0 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 65 6e 64 69  p->nRef++;.#endi
b7600 66 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  f.#endif..#ifdef
b7610 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
b7620 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a  if( p->trace ){.
b7630 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65      printf("ente
b7640 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20  r mutex %p (%d) 
b7650 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c  with nRef=%d\n",
b7660 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d   p, p->trace, p-
b7670 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64  >nRef);.  }.#end
b7680 69 66 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  if.}.static int 
b7690 70 74 68 72 65 61 64 4d 75 74 65 78 54 72 79 28  pthreadMutexTry(
b76a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
b76b0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61  ){.  int rc;.  a
b76c0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
b76d0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
b76e0 53 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d  SIVE || pthreadM
b76f0 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29  utexNotheld(p) )
b7700 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
b7710 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52  _HOMEGROWN_RECUR
b7720 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20  SIVE_MUTEX.  /* 
b7730 49 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  If recursive mut
b7740 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61  exes are not ava
b7750 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20  ilable, then we 
b7760 68 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a  have to grow.  *
b7770 2a 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73  * our own.  This
b7780 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
b7790 61 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 68  assumes that pth
b77a0 72 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a  read_equal().  *
b77b0 2a 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68  * is atomic - th
b77c0 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20  at it cannot be 
b77d0 64 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68  deceived into th
b77e0 69 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a  inking self.  **
b77f0 20 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72   and p->owner ar
b7800 65 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77  e equal if p->ow
b7810 6e 65 72 20 63 68 61 6e 67 65 73 20 62 65 74 77  ner changes betw
b7820 65 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20  een two values. 
b7830 20 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74   ** that are not
b7840 20 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77   equal to self w
b7850 68 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69  hile the compari
b7860 73 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c  son is taking pl
b7870 61 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69  ace..  ** This i
b7880 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c  mplementation al
b7890 73 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68  so assumes a coh
b78a0 65 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68  erent cache - th
b78b0 61 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74  at .  ** separat
b78c0 65 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e  e processes cann
b78d0 6f 74 20 72 65 61 64 20 64 69 66 66 65 72 65 6e  ot read differen
b78e0 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68  t values from th
b78f0 65 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72  e same.  ** addr
b7900 65 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 20  ess at the same 
b7910 74 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72  time.  If either
b7920 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f   of these two co
b7930 6e 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72  nditions.  ** ar
b7940 65 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20  e not met, then 
b7950 74 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c  the mutexes will
b7960 20 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65   fail and proble
b7970 6d 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a  ms will result..
b7980 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68    */.  {.    pth
b7990 72 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74  read_t self = pt
b79a0 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20  hread_self();.  
b79b0 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20    if( p->nRef>0 
b79c0 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c  && pthread_equal
b79d0 28 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29  (p->owner, self)
b79e0 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65   ){.      p->nRe
b79f0 66 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  f++;.      rc = 
b7a00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
b7a10 65 6c 73 65 20 69 66 28 20 70 74 68 72 65 61 64  else if( pthread
b7a20 5f 6d 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26  _mutex_trylock(&
b7a30 70 2d 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a  p->mutex)==0 ){.
b7a40 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
b7a50 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20  >nRef==0 );.    
b7a60 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c    p->owner = sel
b7a70 66 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66  f;.      p->nRef
b7a80 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d   = 1;.      rc =
b7a90 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
b7aa0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
b7ab0 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
b7ac0 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20     }.  }.#else. 
b7ad0 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c   /* Use the buil
b7ae0 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d  t-in recursive m
b7af0 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20 61  utexes if they a
b7b00 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20  re available..  
b7b10 2a 2f 0a 20 20 69 66 28 20 70 74 68 72 65 61 64  */.  if( pthread
b7b20 5f 6d 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26  _mutex_trylock(&
b7b30 70 2d 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a  p->mutex)==0 ){.
b7b40 23 69 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  #if SQLITE_MUTEX
b7b50 5f 4e 52 45 46 0a 20 20 20 20 70 2d 3e 6f 77 6e  _NREF.    p->own
b7b60 65 72 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c  er = pthread_sel
b7b70 66 28 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66  f();.    p->nRef
b7b80 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72  ++;.#endif.    r
b7b90 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
b7ba0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
b7bb0 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
b7bc0 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  }.#endif..#ifdef
b7bd0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
b7be0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
b7bf0 4b 20 26 26 20 70 2d 3e 74 72 61 63 65 20 29 7b  K && p->trace ){
b7c00 0a 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74  .    printf("ent
b7c10 65 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29  er mutex %p (%d)
b7c20 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22   with nRef=%d\n"
b7c30 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70  , p, p->trace, p
b7c40 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e  ->nRef);.  }.#en
b7c50 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  dif.  return rc;
b7c60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  .}../*.** The sq
b7c70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
b7c80 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74  e() routine exit
b7c90 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77  s a mutex that w
b7ca0 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  as.** previously
b7cb0 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20   entered by the 
b7cc0 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68  same thread.  Th
b7cd0 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73  e behavior.** is
b7ce0 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68   undefined if th
b7cf0 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63  e mutex is not c
b7d00 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64  urrently entered
b7d10 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75   or.** is not cu
b7d20 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65  rrently allocate
b7d30 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  d.  SQLite will 
b7d40 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e  never do either.
b7d50 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
b7d60 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76  pthreadMutexLeav
b7d70 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
b7d80 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
b7d90 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28  threadMutexHeld(
b7da0 70 29 20 29 3b 0a 23 69 66 20 53 51 4c 49 54 45  p) );.#if SQLITE
b7db0 5f 4d 55 54 45 58 5f 4e 52 45 46 0a 20 20 70 2d  _MUTEX_NREF.  p-
b7dc0 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70  >nRef--;.  if( p
b7dd0 2d 3e 6e 52 65 66 3d 3d 30 20 29 20 70 2d 3e 6f  ->nRef==0 ) p->o
b7de0 77 6e 65 72 20 3d 20 30 3b 0a 23 65 6e 64 69 66  wner = 0;.#endif
b7df0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52  .  assert( p->nR
b7e00 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d  ef==0 || p->id==
b7e10 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
b7e20 55 52 53 49 56 45 20 29 3b 0a 0a 23 69 66 64 65  URSIVE );..#ifde
b7e30 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f  f SQLITE_HOMEGRO
b7e40 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54  WN_RECURSIVE_MUT
b7e50 45 58 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 66  EX.  if( p->nRef
b7e60 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74 68 72 65  ==0 ){.    pthre
b7e70 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28  ad_mutex_unlock(
b7e80 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  &p->mutex);.  }.
b7e90 23 65 6c 73 65 0a 20 20 70 74 68 72 65 61 64 5f  #else.  pthread_
b7ea0 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d  mutex_unlock(&p-
b7eb0 3e 6d 75 74 65 78 29 3b 0a 23 65 6e 64 69 66 0a  >mutex);.#endif.
b7ec0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
b7ed0 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 72  EBUG.  if( p->tr
b7ee0 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74  ace ){.    print
b7ef0 66 28 22 6c 65 61 76 65 20 6d 75 74 65 78 20 25  f("leave mutex %
b7f00 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66  p (%d) with nRef
b7f10 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72  =%d\n", p, p->tr
b7f20 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20  ace, p->nRef);. 
b7f30 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53 51 4c   }.#endif.}..SQL
b7f40 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
b7f50 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
b7f60 73 20 63 6f 6e 73 74 20 2a 73 71 6c 69 74 65 33  s const *sqlite3
b7f70 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69  DefaultMutex(voi
b7f80 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  d){.  static con
b7f90 73 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  st sqlite3_mutex
b7fa0 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20  _methods sMutex 
b7fb0 3d 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 4d  = {.    pthreadM
b7fc0 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 70 74  utexInit,.    pt
b7fd0 68 72 65 61 64 4d 75 74 65 78 45 6e 64 2c 0a 20  hreadMutexEnd,. 
b7fe0 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 41     pthreadMutexA
b7ff0 6c 6c 6f 63 2c 0a 20 20 20 20 70 74 68 72 65 61  lloc,.    pthrea
b8000 64 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20  dMutexFree,.    
b8010 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65  pthreadMutexEnte
b8020 72 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75  r,.    pthreadMu
b8030 74 65 78 54 72 79 2c 0a 20 20 20 20 70 74 68 72  texTry,.    pthr
b8040 65 61 64 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23  eadMutexLeave,.#
b8050 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
b8060 55 47 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75  UG.    pthreadMu
b8070 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 70 74 68  texHeld,.    pth
b8080 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64  readMutexNotheld
b8090 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a 20 20  .#else.    0,.  
b80a0 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a    0.#endif.  };.
b80b0 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65  .  return &sMute
b80c0 78 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  x;.}..#endif /* 
b80d0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48  SQLITE_MUTEX_PTH
b80e0 52 45 41 44 53 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  READS */../*****
b80f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
b8100 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a 2a   mutex_unix.c **
b8110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8130 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
b8140 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
b8150 66 69 6c 65 20 6d 75 74 65 78 5f 77 33 32 2e 63  file mutex_w32.c
b8160 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
b8170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8180 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
b8190 32 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a  2007 August 14.*
b81a0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
b81b0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
b81c0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
b81d0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
b81e0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
b81f0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
b8200 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
b8210 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
b8220 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
b8230 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
b8240 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
b8250 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
b8260 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
b8270 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
b8280 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
b8290 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
b82a0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
b82b0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
b82c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b82d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b82e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b82f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
b8300 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
b8310 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69  ins the C functi
b8320 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
b8330 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 77  nt mutexes for w
b8340 69 6e 33 32 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  in32.*/../*.** T
b8350 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
b8360 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 65  file is only use
b8370 64 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d 70  d if we are comp
b8380 69 6c 69 6e 67 20 6d 75 6c 74 69 74 68 72 65 61  iling multithrea
b8390 64 65 64 0a 2a 2a 20 6f 6e 20 61 20 77 69 6e 33  ded.** on a win3
b83a0 32 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a 23 69 66  2 system..*/.#if
b83b0 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  def SQLITE_MUTEX
b83c0 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  _W32../*.** Each
b83d0 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
b83e0 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
b83f0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
b8400 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
b8410 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75  truct sqlite3_mu
b8420 74 65 78 20 7b 0a 20 20 43 52 49 54 49 43 41 4c  tex {.  CRITICAL
b8430 5f 53 45 43 54 49 4f 4e 20 6d 75 74 65 78 3b 20  _SECTION mutex; 
b8440 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74     /* Mutex cont
b8450 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b  rolling the lock
b8460 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20 20 20   */.  int id;   
b8470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b8480 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 20 2a   /* Mutex type *
b8490 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
b84a0 44 45 42 55 47 0a 20 20 76 6f 6c 61 74 69 6c 65  DEBUG.  volatile
b84b0 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
b84c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
b84d0 65 6e 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20  enterances */.  
b84e0 76 6f 6c 61 74 69 6c 65 20 44 57 4f 52 44 20 6f  volatile DWORD o
b84f0 77 6e 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 68  wner;      /* Th
b8500 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69  read holding thi
b8510 73 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69 6e 74  s mutex */.  int
b8520 20 74 72 61 63 65 3b 20 20 20 20 20 20 20 20 20   trace;         
b8530 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
b8540 74 6f 20 74 72 61 63 65 20 63 68 61 6e 67 65 73  to trace changes
b8550 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 64   */.#endif.};.#d
b8560 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 33 32  efine SQLITE_W32
b8570 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
b8580 45 52 20 7b 20 30 20 7d 0a 23 69 66 64 65 66 20  ER { 0 }.#ifdef 
b8590 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 64 65  SQLITE_DEBUG.#de
b85a0 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54  fine SQLITE3_MUT
b85b0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b  EX_INITIALIZER {
b85c0 20 53 51 4c 49 54 45 5f 57 33 32 5f 4d 55 54 45   SQLITE_W32_MUTE
b85d0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30  X_INITIALIZER, 0
b85e0 2c 20 30 4c 2c 20 28 44 57 4f 52 44 29 30 2c 20  , 0L, (DWORD)0, 
b85f0 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  0 }.#else.#defin
b8600 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f  e SQLITE3_MUTEX_
b8610 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 53 51  INITIALIZER { SQ
b8620 4c 49 54 45 5f 57 33 32 5f 4d 55 54 45 58 5f 49  LITE_W32_MUTEX_I
b8630 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 20 7d 0a  NITIALIZER, 0 }.
b8640 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65  #endif../*.** Re
b8650 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a  turn true (non-z
b8660 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 20 72  ero) if we are r
b8670 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 69 6e  unning under Win
b8680 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e 58 50  NT, Win2K, WinXP
b8690 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e 20 20  ,.** or WinCE.  
b86a0 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 7a 65  Return false (ze
b86b0 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c 20 57  ro) for Win95, W
b86c0 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a  in98, or WinME..
b86d0 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 61 6e  **.** Here is an
b86e0 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f 62 73   interesting obs
b86f0 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35  ervation:  Win95
b8700 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e  , Win98, and Win
b8710 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 20 4c  ME lack.** the L
b8720 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e  ockFileEx() API.
b8730 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 74 69    But we can sti
b8740 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69  ll statically li
b8750 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 74 0a  nk against that.
b8760 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 20 61  ** API as long a
b8770 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20  s we don't call 
b8780 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67 20 57  it win running W
b8790 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63  in95/98/ME.  A c
b87a0 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72  all to.** this r
b87b0 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74  outine is used t
b87c0 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74  o determine if t
b87d0 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e 39 35  he host is Win95
b87e0 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e  /98/ME or.** Win
b87f0 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 61 74  NT/2K/XP so that
b8800 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68   we will know wh
b8810 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 65 20  ether or not we 
b8820 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c 6c 0a  can safely call.
b8830 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45  ** the LockFileE
b8840 78 28 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 6d  x() API..**.** m
b8850 75 74 65 78 49 73 4e 54 28 29 20 69 73 20 6f 6e  utexIsNT() is on
b8860 6c 79 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  ly used for the 
b8870 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c  TryEnterCritical
b8880 53 65 63 74 69 6f 6e 28 29 20 41 50 49 20 63 61  Section() API ca
b8890 6c 6c 2c 0a 2a 2a 20 77 68 69 63 68 20 69 73 20  ll,.** which is 
b88a0 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69  only available i
b88b0 66 20 79 6f 75 72 20 61 70 70 6c 69 63 61 74 69  f your applicati
b88c0 6f 6e 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20  on was compiled 
b88d0 77 69 74 68 20 0a 2a 2a 20 5f 57 49 4e 33 32 5f  with .** _WIN32_
b88e0 57 49 4e 4e 54 20 64 65 66 69 6e 65 64 20 74 6f  WINNT defined to
b88f0 20 61 20 76 61 6c 75 65 20 3e 3d 20 30 78 30 34   a value >= 0x04
b8900 30 30 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20  00.  Currently, 
b8910 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 63 61 6c 6c  the only.** call
b8920 20 74 6f 20 54 72 79 45 6e 74 65 72 43 72 69 74   to TryEnterCrit
b8930 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 69 73  icalSection() is
b8940 20 23 69 66 64 65 66 27 65 64 20 6f 75 74 2c 20   #ifdef'ed out, 
b8950 73 6f 20 23 69 66 64 65 66 20 0a 2a 2a 20 74 68  so #ifdef .** th
b8960 69 73 20 6f 75 74 20 61 73 20 77 65 6c 6c 2e 0a  is out as well..
b8970 2a 2f 0a 23 69 66 20 30 0a 23 69 66 20 53 51 4c  */.#if 0.#if SQL
b8980 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 7c 7c 20  ITE_OS_WINCE || 
b8990 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a  SQLITE_OS_WINRT.
b89a0 23 20 64 65 66 69 6e 65 20 6d 75 74 65 78 49 73  # define mutexIs
b89b0 4e 54 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a  NT()  (1).#else.
b89c0 20 20 73 74 61 74 69 63 20 69 6e 74 20 6d 75 74    static int mut
b89d0 65 78 49 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20  exIsNT(void){.  
b89e0 20 20 73 74 61 74 69 63 20 69 6e 74 20 6f 73 54    static int osT
b89f0 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  ype = 0;.    if(
b8a00 20 6f 73 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20   osType==0 ){.  
b8a10 20 20 20 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46      OSVERSIONINF
b8a20 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73  O sInfo;.      s
b8a30 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e  Info.dwOSVersion
b8a40 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f  InfoSize = sizeo
b8a50 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  f(sInfo);.      
b8a60 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26 73 49  GetVersionEx(&sI
b8a70 6e 66 6f 29 3b 0a 20 20 20 20 20 20 6f 73 54 79  nfo);.      osTy
b8a80 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61  pe = sInfo.dwPla
b8a90 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41  tformId==VER_PLA
b8aa0 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f  TFORM_WIN32_NT ?
b8ab0 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20   2 : 1;.    }.  
b8ac0 20 20 72 65 74 75 72 6e 20 6f 73 54 79 70 65 3d    return osType=
b8ad0 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f  =2;.  }.#endif /
b8ae0 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  * SQLITE_OS_WINC
b8af0 45 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 23 69 66  E */.#endif..#if
b8b00 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
b8b10 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
b8b20 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20  e3_mutex_held() 
b8b30 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
b8b40 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74  x_notheld() rout
b8b50 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e  ine are.** inten
b8b60 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79  ded for use only
b8b70 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29   inside assert()
b8b80 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a   statements..*/.
b8b90 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75  static int winMu
b8ba0 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f  texHeld(sqlite3_
b8bb0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74  mutex *p){.  ret
b8bc0 75 72 6e 20 70 2d 3e 6e 52 65 66 21 3d 30 20 26  urn p->nRef!=0 &
b8bd0 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43  & p->owner==GetC
b8be0 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29  urrentThreadId()
b8bf0 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  ;.}.static int w
b8c00 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 32 28  inMutexNotheld2(
b8c10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
b8c20 2c 20 44 57 4f 52 44 20 74 69 64 29 7b 0a 20 20  , DWORD tid){.  
b8c30 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d  return p->nRef==
b8c40 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21 3d 74  0 || p->owner!=t
b8c50 69 64 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  id;.}.static int
b8c60 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64   winMutexNotheld
b8c70 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
b8c80 70 29 7b 0a 20 20 44 57 4f 52 44 20 74 69 64 20  p){.  DWORD tid 
b8c90 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 65  = GetCurrentThre
b8ca0 61 64 49 64 28 29 3b 20 0a 20 20 72 65 74 75 72  adId(); .  retur
b8cb0 6e 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c  n winMutexNothel
b8cc0 64 32 28 70 2c 20 74 69 64 29 3b 0a 7d 0a 23 65  d2(p, tid);.}.#e
b8cd0 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  ndif.../*.** Ini
b8ce0 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e  tialize and dein
b8cf0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74  itialize the mut
b8d00 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f  ex subsystem..*/
b8d10 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
b8d20 6d 75 74 65 78 20 77 69 6e 4d 75 74 65 78 5f 73  mutex winMutex_s
b8d30 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d 20  taticMutexes[6] 
b8d40 3d 20 7b 0a 20 20 53 51 4c 49 54 45 33 5f 4d 55  = {.  SQLITE3_MU
b8d50 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
b8d60 0a 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58  .  SQLITE3_MUTEX
b8d70 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20  _INITIALIZER,.  
b8d80 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e  SQLITE3_MUTEX_IN
b8d90 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 53 51 4c  ITIALIZER,.  SQL
b8da0 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49  ITE3_MUTEX_INITI
b8db0 41 4c 49 5a 45 52 2c 0a 20 20 53 51 4c 49 54 45  ALIZER,.  SQLITE
b8dc0 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
b8dd0 5a 45 52 2c 0a 20 20 53 51 4c 49 54 45 33 5f 4d  ZER,.  SQLITE3_M
b8de0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
b8df0 0a 7d 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .};.static int w
b8e00 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d  inMutex_isInit =
b8e10 20 30 3b 0a 2f 2a 20 41 73 20 77 69 6e 4d 75 74   0;./* As winMut
b8e20 65 78 49 6e 69 74 28 29 20 61 6e 64 20 77 69 6e  exInit() and win
b8e30 4d 75 74 65 78 45 6e 64 28 29 20 61 72 65 20 63  MutexEnd() are c
b8e40 61 6c 6c 65 64 20 61 73 20 70 61 72 74 0a 2a 2a  alled as part.**
b8e50 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
b8e60 69 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73  initialize and s
b8e70 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
b8e80 29 0a 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 2c  ).** processing,
b8e90 20 74 68 65 20 22 69 6e 74 65 72 6c 6f 63 6b 65   the "interlocke
b8ea0 64 22 20 6d 61 67 69 63 20 69 73 20 70 72 6f 62  d" magic is prob
b8eb0 61 62 6c 79 20 6e 6f 74 0a 2a 2a 20 73 74 72 69  ably not.** stri
b8ec0 63 74 6c 79 20 6e 65 63 65 73 73 61 72 79 2e 0a  ctly necessary..
b8ed0 2a 2f 0a 73 74 61 74 69 63 20 6c 6f 6e 67 20 77  */.static long w
b8ee0 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 20 3d 20 30  inMutex_lock = 0
b8ef0 3b 0a 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  ;..SQLITE_API vo
b8f00 69 64 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32  id sqlite3_win32
b8f10 5f 73 6c 65 65 70 28 44 57 4f 52 44 20 6d 69 6c  _sleep(DWORD mil
b8f20 6c 69 73 65 63 6f 6e 64 73 29 3b 20 2f 2a 20 6f  liseconds); /* o
b8f30 73 5f 77 69 6e 2e 63 20 2a 2f 0a 0a 73 74 61 74  s_win.c */..stat
b8f40 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 49  ic int winMutexI
b8f50 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a  nit(void){ .  /*
b8f60 20 54 68 65 20 66 69 72 73 74 20 74 6f 20 69 6e   The first to in
b8f70 63 72 65 6d 65 6e 74 20 74 6f 20 31 20 64 6f 65  crement to 1 doe
b8f80 73 20 61 63 74 75 61 6c 20 69 6e 69 74 69 61 6c  s actual initial
b8f90 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28  ization */.  if(
b8fa0 20 49 6e 74 65 72 6c 6f 63 6b 65 64 43 6f 6d 70   InterlockedComp
b8fb0 61 72 65 45 78 63 68 61 6e 67 65 28 26 77 69 6e  areExchange(&win
b8fc0 4d 75 74 65 78 5f 6c 6f 63 6b 2c 20 31 2c 20 30  Mutex_lock, 1, 0
b8fd0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )==0 ){.    int 
b8fe0 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  i;.    for(i=0; 
b8ff0 69 3c 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d  i<ArraySize(winM
b9000 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78  utex_staticMutex
b9010 65 73 29 3b 20 69 2b 2b 29 7b 0a 23 69 66 20 53  es); i++){.#if S
b9020 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20  QLITE_OS_WINRT. 
b9030 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 43       InitializeC
b9040 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 45 78  riticalSectionEx
b9050 28 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69  (&winMutex_stati
b9060 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65  cMutexes[i].mute
b9070 78 2c 20 30 2c 20 30 29 3b 0a 23 65 6c 73 65 0a  x, 0, 0);.#else.
b9080 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65        Initialize
b9090 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28  CriticalSection(
b90a0 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63  &winMutex_static
b90b0 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78  Mutexes[i].mutex
b90c0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a  );.#endif.    }.
b90d0 20 20 20 20 77 69 6e 4d 75 74 65 78 5f 69 73 49      winMutex_isI
b90e0 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  nit = 1;.  }else
b90f0 7b 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 6f 6e 65  {.    /* Someone
b9100 20 65 6c 73 65 20 69 73 20 69 6e 20 74 68 65 20   else is in the 
b9110 70 72 6f 63 65 73 73 20 6f 66 20 69 6e 69 74 69  process of initi
b9120 6e 67 20 74 68 65 20 73 74 61 74 69 63 20 6d 75  ng the static mu
b9130 74 65 78 65 73 20 2a 2f 0a 20 20 20 20 77 68 69  texes */.    whi
b9140 6c 65 28 20 21 77 69 6e 4d 75 74 65 78 5f 69 73  le( !winMutex_is
b9150 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 73 71  Init ){.      sq
b9160 6c 69 74 65 33 5f 77 69 6e 33 32 5f 73 6c 65 65  lite3_win32_slee
b9170 70 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  p(1);.    }.  }.
b9180 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
b9190 4f 4b 3b 20 0a 7d 0a 0a 73 74 61 74 69 63 20 69  OK; .}..static i
b91a0 6e 74 20 77 69 6e 4d 75 74 65 78 45 6e 64 28 76  nt winMutexEnd(v
b91b0 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 54 68 65 20  oid){ .  /* The 
b91c0 66 69 72 73 74 20 74 6f 20 64 65 63 72 65 6d 65  first to decreme
b91d0 6e 74 20 74 6f 20 30 20 64 6f 65 73 20 61 63 74  nt to 0 does act
b91e0 75 61 6c 20 73 68 75 74 64 6f 77 6e 20 0a 20 20  ual shutdown .  
b91f0 2a 2a 20 28 77 68 69 63 68 20 73 68 6f 75 6c 64  ** (which should
b9200 20 62 65 20 74 68 65 20 6c 61 73 74 20 74 6f 20   be the last to 
b9210 73 68 75 74 64 6f 77 6e 2e 29 20 2a 2f 0a 20 20  shutdown.) */.  
b9220 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 43  if( InterlockedC
b9230 6f 6d 70 61 72 65 45 78 63 68 61 6e 67 65 28 26  ompareExchange(&
b9240 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c 20 30  winMutex_lock, 0
b9250 2c 20 31 29 3d 3d 31 20 29 7b 0a 20 20 20 20 69  , 1)==1 ){.    i
b9260 66 28 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e  f( winMutex_isIn
b9270 69 74 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 69  it==1 ){.      i
b9280 6e 74 20 69 3b 0a 20 20 20 20 20 20 66 6f 72 28  nt i;.      for(
b9290 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65  i=0; i<ArraySize
b92a0 28 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63  (winMutex_static
b92b0 4d 75 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a  Mutexes); i++){.
b92c0 20 20 20 20 20 20 20 20 44 65 6c 65 74 65 43 72          DeleteCr
b92d0 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 77  iticalSection(&w
b92e0 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75  inMutex_staticMu
b92f0 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b  texes[i].mutex);
b9300 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 77  .      }.      w
b9310 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d  inMutex_isInit =
b9320 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
b9330 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
b9340 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ; .}../*.** The 
b9350 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
b9360 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c  loc() routine al
b9370 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a  locates a new.**
b9380 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72   mutex and retur
b9390 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
b93a0 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72  it.  If it retur
b93b0 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20  ns NULL.** that 
b93c0 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74  means that a mut
b93d0 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ex could not be 
b93e0 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69  allocated.  SQLi
b93f0 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e  te.** will unwin
b9400 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20  d its stack and 
b9410 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e  return an error.
b9420 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a    The argument.*
b9430 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  * to sqlite3_mut
b9440 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e  ex_alloc() is on
b9450 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67  e of these integ
b9460 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a  er constants:.**
b9470 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
b9480 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46    SQLITE_MUTEX_F
b9490 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  AST.** <li>  SQL
b94a0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
b94b0 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  IVE.** <li>  SQL
b94c0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
b94d0 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20  _MASTER.** <li> 
b94e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
b94f0 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e  ATIC_MEM.** <li>
b9500 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
b9510 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c  TATIC_MEM2.** <l
b9520 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
b9530 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20  _STATIC_PRNG.** 
b9540 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
b9550 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a  EX_STATIC_LRU.**
b9560 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
b9570 54 45 58 5f 53 54 41 54 49 43 5f 50 4d 45 4d 0a  TEX_STATIC_PMEM.
b9580 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54  ** </ul>.**.** T
b9590 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e  he first two con
b95a0 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c  stants cause sql
b95b0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
b95c0 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20  () to create.** 
b95d0 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68  a new mutex.  Th
b95e0 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72  e new mutex is r
b95f0 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51  ecursive when SQ
b9600 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
b9610 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20  SIVE.** is used 
b9620 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72  but not necessar
b9630 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49  ily so when SQLI
b9640 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73  TE_MUTEX_FAST is
b9650 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75   used..** The mu
b9660 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tex implementati
b9670 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  on does not need
b9680 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69   to make a disti
b9690 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65  nction.** betwee
b96a0 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
b96b0 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c  ECURSIVE and SQL
b96c0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69  ITE_MUTEX_FAST i
b96d0 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  f it does.** not
b96e0 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53   want to.  But S
b96f0 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  QLite will only 
b9700 72 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73  request a recurs
b9710 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20  ive mutex in.** 
b9720 63 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72  cases where it r
b9730 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e  eally needs one.
b9740 20 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f    If a faster no
b9750 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  n-recursive mute
b9760 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  x.** implementat
b9770 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65  ion is available
b9780 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61   on the host pla
b9790 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78  tform, the mutex
b97a0 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69   subsystem.** mi
b97b0 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20  ght return such 
b97c0 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f  a mutex in respo
b97d0 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55  nse to SQLITE_MU
b97e0 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20  TEX_FAST..**.** 
b97f0 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65  The other allowe
b9800 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  d parameters to 
b9810 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
b9820 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72  loc() each retur
b9830 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  n.** a pointer t
b9840 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78  o a static preex
b9850 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 53  isting mutex.  S
b9860 69 78 20 73 74 61 74 69 63 20 6d 75 74 65 78 65  ix static mutexe
b9870 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79  s are.** used by
b9880 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72   the current ver
b9890 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20  sion of SQLite. 
b98a0 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73   Future versions
b98b0 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61   of SQLite.** ma
b98c0 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c  y add additional
b98d0 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e   static mutexes.
b98e0 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73    Static mutexes
b98f0 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61   are for interna
b9900 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69  l.** use by SQLi
b9910 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63  te only.  Applic
b9920 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20  ations that use 
b9930 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73  SQLite mutexes s
b9940 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c  hould.** use onl
b9950 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  y the dynamic mu
b9960 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62  texes returned b
b9970 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  y SQLITE_MUTEX_F
b9980 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45  AST or.** SQLITE
b9990 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
b99a0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
b99b0 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20  t if one of the 
b99c0 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61  dynamic mutex pa
b99d0 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45  rameters (SQLITE
b99e0 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f  _MUTEX_FAST.** o
b99f0 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  r SQLITE_MUTEX_R
b9a00 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65  ECURSIVE) is use
b9a10 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d  d then sqlite3_m
b9a20 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20  utex_alloc().** 
b9a30 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72  returns a differ
b9a40 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65  ent mutex on eve
b9a50 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f  ry call.  But fo
b9a60 72 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a  r the static .**
b9a70 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68   mutex types, th
b9a80 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20  e same mutex is 
b9a90 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72  returned on ever
b9aa0 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a  y call that has.
b9ab0 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65  ** the same type
b9ac0 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74   number..*/.stat
b9ad0 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
b9ae0 20 2a 77 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 28   *winMutexAlloc(
b9af0 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71  int iType){.  sq
b9b00 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a  lite3_mutex *p;.
b9b10 0a 20 20 73 77 69 74 63 68 28 20 69 54 79 70 65  .  switch( iType
b9b20 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
b9b30 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a  ITE_MUTEX_FAST:.
b9b40 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
b9b50 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a  MUTEX_RECURSIVE:
b9b60 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c   {.      p = sql
b9b70 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20  ite3MallocZero( 
b9b80 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20  sizeof(*p) );.  
b9b90 20 20 20 20 69 66 28 20 70 20 29 7b 20 20 0a 23      if( p ){  .#
b9ba0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
b9bb0 55 47 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64  UG.        p->id
b9bc0 20 3d 20 69 54 79 70 65 3b 0a 23 65 6e 64 69 66   = iType;.#endif
b9bd0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
b9be0 49 4e 52 54 0a 20 20 20 20 20 20 20 20 49 6e 69  INRT.        Ini
b9bf0 74 69 61 6c 69 7a 65 43 72 69 74 69 63 61 6c 53  tializeCriticalS
b9c00 65 63 74 69 6f 6e 45 78 28 26 70 2d 3e 6d 75 74  ectionEx(&p->mut
b9c10 65 78 2c 20 30 2c 20 30 29 3b 0a 23 65 6c 73 65  ex, 0, 0);.#else
b9c20 0a 20 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c  .        Initial
b9c30 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63 74 69  izeCriticalSecti
b9c40 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 23  on(&p->mutex);.#
b9c50 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20  endif.      }.  
b9c60 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
b9c70 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
b9c80 20 20 20 20 20 20 61 73 73 65 72 74 28 20 77 69        assert( wi
b9c90 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31  nMutex_isInit==1
b9ca0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
b9cb0 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29  ( iType-2 >= 0 )
b9cc0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
b9cd0 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79 53  iType-2 < ArrayS
b9ce0 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73 74 61  ize(winMutex_sta
b9cf0 74 69 63 4d 75 74 65 78 65 73 29 20 29 3b 0a 20  ticMutexes) );. 
b9d00 20 20 20 20 20 70 20 3d 20 26 77 69 6e 4d 75 74       p = &winMut
b9d10 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73  ex_staticMutexes
b9d20 5b 69 54 79 70 65 2d 32 5d 3b 0a 23 69 66 64 65  [iType-2];.#ifde
b9d30 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
b9d40 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79       p->id = iTy
b9d50 70 65 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  pe;.#endif.     
b9d60 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
b9d70 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
b9d80 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
b9d90 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73  tine deallocates
b9da0 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a   a previously.**
b9db0 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78   allocated mutex
b9dc0 2e 20 20 53 51 4c 69 74 65 20 69 73 20 63 61 72  .  SQLite is car
b9dd0 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61  eful to dealloca
b9de0 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65  te every.** mute
b9df0 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61  x that it alloca
b9e00 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tes..*/.static v
b9e10 6f 69 64 20 77 69 6e 4d 75 74 65 78 46 72 65 65  oid winMutexFree
b9e20 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
b9e30 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20  p){.  assert( p 
b9e40 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
b9e50 6e 52 65 66 3d 3d 30 20 26 26 20 70 2d 3e 6f 77  nRef==0 && p->ow
b9e60 6e 65 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  ner==0 );.  asse
b9e70 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54  rt( p->id==SQLIT
b9e80 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20  E_MUTEX_FAST || 
b9e90 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
b9ea0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b  TEX_RECURSIVE );
b9eb0 0a 20 20 44 65 6c 65 74 65 43 72 69 74 69 63 61  .  DeleteCritica
b9ec0 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74  lSection(&p->mut
b9ed0 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ex);.  sqlite3_f
b9ee0 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ree(p);.}../*.**
b9ef0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
b9f00 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73  ex_enter() and s
b9f10 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
b9f20 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65  () routines atte
b9f30 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20  mpt.** to enter 
b9f40 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f  a mutex.  If ano
b9f50 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61  ther thread is a
b9f60 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68  lready within th
b9f70 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69  e mutex,.** sqli
b9f80 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
b9f90 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64  ) will block and
b9fa0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
b9fb0 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e  ry() will return
b9fc0 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e  .** SQLITE_BUSY.
b9fd0 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75    The sqlite3_mu
b9fe0 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66  tex_try() interf
b9ff0 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ace returns SQLI
ba000 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75  TE_OK.** upon su
ba010 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20  ccessful entry. 
ba020 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64   Mutexes created
ba030 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55   using SQLITE_MU
ba040 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61  TEX_RECURSIVE ca
ba050 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20  n.** be entered 
ba060 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62  multiple times b
ba070 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  y the same threa
ba080 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65  d.  In such case
ba090 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20  s the,.** mutex 
ba0a0 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61  must be exited a
ba0b0 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f  n equal number o
ba0c0 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61  f times before a
ba0d0 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a  nother thread.**
ba0e0 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20   can enter.  If 
ba0f0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
ba100 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61  tries to enter a
ba110 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66  ny other kind of
ba120 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74   mutex.** more t
ba130 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65  han once, the be
ba140 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69  havior is undefi
ba150 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ned..*/.static v
ba160 6f 69 64 20 77 69 6e 4d 75 74 65 78 45 6e 74 65  oid winMutexEnte
ba170 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  r(sqlite3_mutex 
ba180 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  *p){.#ifdef SQLI
ba190 54 45 5f 44 45 42 55 47 0a 20 20 44 57 4f 52 44  TE_DEBUG.  DWORD
ba1a0 20 74 69 64 20 3d 20 47 65 74 43 75 72 72 65 6e   tid = GetCurren
ba1b0 74 54 68 72 65 61 64 49 64 28 29 3b 20 0a 20 20  tThreadId(); .  
ba1c0 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53  assert( p->id==S
ba1d0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
ba1e0 52 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65  RSIVE || winMute
ba1f0 78 4e 6f 74 68 65 6c 64 32 28 70 2c 20 74 69 64  xNotheld2(p, tid
ba200 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 45 6e  ) );.#endif.  En
ba210 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69  terCriticalSecti
ba220 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 23  on(&p->mutex);.#
ba230 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
ba240 55 47 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  UG.  assert( p->
ba250 6e 52 65 66 3e 30 20 7c 7c 20 70 2d 3e 6f 77 6e  nRef>0 || p->own
ba260 65 72 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 6f 77  er==0 );.  p->ow
ba270 6e 65 72 20 3d 20 74 69 64 3b 20 0a 20 20 70 2d  ner = tid; .  p-
ba280 3e 6e 52 65 66 2b 2b 3b 0a 20 20 69 66 28 20 70  >nRef++;.  if( p
ba290 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70  ->trace ){.    p
ba2a0 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 74  rintf("enter mut
ba2b0 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20  ex %p (%d) with 
ba2c0 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70  nRef=%d\n", p, p
ba2d0 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66  ->trace, p->nRef
ba2e0 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a  );.  }.#endif.}.
ba2f0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75  static int winMu
ba300 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d  texTry(sqlite3_m
ba310 75 74 65 78 20 2a 70 29 7b 0a 23 69 66 6e 64 65  utex *p){.#ifnde
ba320 66 20 4e 44 45 42 55 47 0a 20 20 44 57 4f 52 44  f NDEBUG.  DWORD
ba330 20 74 69 64 20 3d 20 47 65 74 43 75 72 72 65 6e   tid = GetCurren
ba340 74 54 68 72 65 61 64 49 64 28 29 3b 20 0a 23 65  tThreadId(); .#e
ba350 6e 64 69 66 0a 20 20 69 6e 74 20 72 63 20 3d 20  ndif.  int rc = 
ba360 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61  SQLITE_BUSY;.  a
ba370 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
ba380 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
ba390 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78  SIVE || winMutex
ba3a0 4e 6f 74 68 65 6c 64 32 28 70 2c 20 74 69 64 29  Notheld2(p, tid)
ba3b0 20 29 3b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68   );.  /*.  ** Th
ba3c0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
ba3d0 74 72 79 28 29 20 72 6f 75 74 69 6e 65 20 69 73  try() routine is
ba3e0 20 76 65 72 79 20 72 61 72 65 6c 79 20 75 73 65   very rarely use
ba3f0 64 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 0a 20  d, and when it. 
ba400 20 2a 2a 20 69 73 20 75 73 65 64 20 69 74 20 69   ** is used it i
ba410 73 20 6d 65 72 65 6c 79 20 61 6e 20 6f 70 74 69  s merely an opti
ba420 6d 69 7a 61 74 69 6f 6e 2e 20 20 53 6f 20 69 74  mization.  So it
ba430 20 69 73 20 4f 4b 20 66 6f 72 20 69 74 20 74 6f   is OK for it to
ba440 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 66 61 69   always.  ** fai
ba450 6c 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  l.  .  **.  ** T
ba460 68 65 20 54 72 79 45 6e 74 65 72 43 72 69 74 69  he TryEnterCriti
ba470 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 69 6e 74  calSection() int
ba480 65 72 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 61  erface is only a
ba490 76 61 69 6c 61 62 6c 65 20 6f 6e 20 57 69 6e 4e  vailable on WinN
ba4a0 54 2e 0a 20 20 2a 2a 20 41 6e 64 20 73 6f 6d 65  T..  ** And some
ba4b0 20 77 69 6e 64 6f 77 73 20 63 6f 6d 70 69 6c 65   windows compile
ba4c0 72 73 20 63 6f 6d 70 6c 61 69 6e 20 69 66 20 79  rs complain if y
ba4d0 6f 75 20 74 72 79 20 74 6f 20 75 73 65 20 69 74  ou try to use it
ba4e0 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 66 69   without.  ** fi
ba4f0 72 73 74 20 64 6f 69 6e 67 20 73 6f 6d 65 20 23  rst doing some #
ba500 64 65 66 69 6e 65 73 20 74 68 61 74 20 70 72 65  defines that pre
ba510 76 65 6e 74 20 53 51 4c 69 74 65 20 66 72 6f 6d  vent SQLite from
ba520 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20 57 69 6e   building on Win
ba530 39 38 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 61  98..  ** For tha
ba540 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 77 69 6c  t reason, we wil
ba550 6c 20 6f 6d 69 74 20 74 68 69 73 20 6f 70 74 69  l omit this opti
ba560 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20 6e 6f 77  mization for now
ba570 2e 20 20 53 65 65 0a 20 20 2a 2a 20 74 69 63 6b  .  See.  ** tick
ba580 65 74 20 23 32 36 38 35 2e 0a 20 20 2a 2f 0a 23  et #2685..  */.#
ba590 69 66 20 30 0a 20 20 69 66 28 20 6d 75 74 65 78  if 0.  if( mutex
ba5a0 49 73 4e 54 28 29 20 26 26 20 54 72 79 45 6e 74  IsNT() && TryEnt
ba5b0 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f  erCriticalSectio
ba5c0 6e 28 26 70 2d 3e 6d 75 74 65 78 29 20 29 7b 0a  n(&p->mutex) ){.
ba5d0 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74      p->owner = t
ba5e0 69 64 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b  id;.    p->nRef+
ba5f0 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  +;.    rc = SQLI
ba600 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 23 65 6c 73 65  TE_OK;.  }.#else
ba610 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
ba620 54 45 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 23  TER(p);.#endif.#
ba630 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
ba640 55 47 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  UG.  if( rc==SQL
ba650 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 74 72 61  ITE_OK && p->tra
ba660 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66  ce ){.    printf
ba670 28 22 74 72 79 20 6d 75 74 65 78 20 25 70 20 28  ("try mutex %p (
ba680 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64  %d) with nRef=%d
ba690 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65  \n", p, p->trace
ba6a0 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a  , p->nRef);.  }.
ba6b0 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
ba6c0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
ba6d0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
ba6e0 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65  eave() routine e
ba6f0 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61  xits a mutex tha
ba700 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75  t was.** previou
ba710 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74  sly entered by t
ba720 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20  he same thread. 
ba730 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a   The behavior.**
ba740 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66   is undefined if
ba750 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f   the mutex is no
ba760 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65  t currently ente
ba770 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74  red or.** is not
ba780 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63   currently alloc
ba790 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69  ated.  SQLite wi
ba7a0 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68  ll never do eith
ba7b0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
ba7c0 69 64 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 65  id winMutexLeave
ba7d0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
ba7e0 70 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  p){.#ifndef NDEB
ba7f0 55 47 0a 20 20 44 57 4f 52 44 20 74 69 64 20 3d  UG.  DWORD tid =
ba800 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61   GetCurrentThrea
ba810 64 49 64 28 29 3b 0a 20 20 61 73 73 65 72 74 28  dId();.  assert(
ba820 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20   p->nRef>0 );.  
ba830 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72  assert( p->owner
ba840 3d 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52  ==tid );.  p->nR
ba850 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  ef--;.  if( p->n
ba860 52 65 66 3d 3d 30 20 29 20 70 2d 3e 6f 77 6e 65  Ref==0 ) p->owne
ba870 72 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28  r = 0;.  assert(
ba880 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70   p->nRef==0 || p
ba890 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
ba8a0 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a  EX_RECURSIVE );.
ba8b0 23 65 6e 64 69 66 0a 20 20 4c 65 61 76 65 43 72  #endif.  LeaveCr
ba8c0 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70  iticalSection(&p
ba8d0 2d 3e 6d 75 74 65 78 29 3b 0a 23 69 66 64 65 66  ->mutex);.#ifdef
ba8e0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
ba8f0 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a  if( p->trace ){.
ba900 20 20 20 20 70 72 69 6e 74 66 28 22 6c 65 61 76      printf("leav
ba910 65 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20  e mutex %p (%d) 
ba920 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c  with nRef=%d\n",
ba930 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d   p, p->trace, p-
ba940 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64  >nRef);.  }.#end
ba950 69 66 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  if.}..SQLITE_PRI
ba960 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74  VATE sqlite3_mut
ba970 65 78 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74  ex_methods const
ba980 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74   *sqlite3Default
ba990 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73  Mutex(void){.  s
ba9a0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
ba9b0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
ba9c0 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20  s sMutex = {.   
ba9d0 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 2c 0a 20   winMutexInit,. 
ba9e0 20 20 20 77 69 6e 4d 75 74 65 78 45 6e 64 2c 0a     winMutexEnd,.
ba9f0 20 20 20 20 77 69 6e 4d 75 74 65 78 41 6c 6c 6f      winMutexAllo
baa00 63 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 46  c,.    winMutexF
baa10 72 65 65 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65  ree,.    winMute
baa20 78 45 6e 74 65 72 2c 0a 20 20 20 20 77 69 6e 4d  xEnter,.    winM
baa30 75 74 65 78 54 72 79 2c 0a 20 20 20 20 77 69 6e  utexTry,.    win
baa40 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 64  MutexLeave,.#ifd
baa50 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
baa60 20 20 20 20 77 69 6e 4d 75 74 65 78 48 65 6c 64      winMutexHeld
baa70 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 4e 6f  ,.    winMutexNo
baa80 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20  theld.#else.    
baa90 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69 66 0a  0,.    0.#endif.
baaa0 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26    };..  return &
baab0 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66  sMutex;.}.#endif
baac0 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   /* SQLITE_MUTEX
baad0 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  _W32 */../******
baae0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
baaf0 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a  mutex_w32.c ****
bab00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bab10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bab20 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
bab30 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
bab40 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a  ile malloc.c ***
bab50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bab60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bab70 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
bab80 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
bab90 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
baba0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
babb0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
babc0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
babd0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
babe0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
babf0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
bac00 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
bac10 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
bac20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
bac30 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
bac40 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
bac50 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
bac60 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
bac70 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
bac80 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
bac90 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
baca0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
bacb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bacc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bacd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bace0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
bacf0 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c  **.** Memory all
bad00 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  ocation function
bad10 73 20 75 73 65 64 20 74 68 72 6f 75 67 68 6f 75  s used throughou
bad20 74 20 73 71 6c 69 74 65 2e 0a 2a 2f 0a 2f 2a 20  t sqlite..*/./* 
bad30 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67  #include <stdarg
bad40 2e 68 3e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 74  .h> */../*.** At
bad50 74 65 6d 70 74 20 74 6f 20 72 65 6c 65 61 73 65  tempt to release
bad60 20 75 70 20 74 6f 20 6e 20 62 79 74 65 73 20 6f   up to n bytes o
bad70 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20  f non-essential 
bad80 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c 79  memory currently
bad90 0a 2a 2a 20 68 65 6c 64 20 62 79 20 53 51 4c 69  .** held by SQLi
bada0 74 65 2e 20 41 6e 20 65 78 61 6d 70 6c 65 20 6f  te. An example o
badb0 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20  f non-essential 
badc0 6d 65 6d 6f 72 79 20 69 73 20 6d 65 6d 6f 72 79  memory is memory
badd0 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 61 63 68   used to.** cach
bade0 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 73  e database pages
badf0 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 63 75   that are not cu
bae00 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a  rrently in use..
bae10 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
bae20 74 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73  t sqlite3_releas
bae30 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b  e_memory(int n){
bae40 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
bae50 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e  NABLE_MEMORY_MAN
bae60 41 47 45 4d 45 4e 54 0a 20 20 72 65 74 75 72 6e  AGEMENT.  return
bae70 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
bae80 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 29 3b 0a  leaseMemory(n);.
bae90 23 65 6c 73 65 0a 20 20 2f 2a 20 49 4d 50 4c 45  #else.  /* IMPLE
baea0 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d  MENTATION-OF: R-
baeb0 33 34 33 39 31 2d 32 34 39 32 31 20 54 68 65 20  34391-24921 The 
baec0 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
baed0 6d 65 6d 6f 72 79 28 29 20 72 6f 75 74 69 6e 65  memory() routine
baee0 0a 20 20 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70  .  ** is a no-op
baef0 20 72 65 74 75 72 6e 69 6e 67 20 7a 65 72 6f 20   returning zero 
baf00 69 66 20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74  if SQLite is not
baf10 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 20   compiled with. 
baf20 20 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   ** SQLITE_ENABL
baf30 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d  E_MEMORY_MANAGEM
baf40 45 4e 54 2e 20 2a 2f 0a 20 20 55 4e 55 53 45 44  ENT. */.  UNUSED
baf50 5f 50 41 52 41 4d 45 54 45 52 28 6e 29 3b 0a 20  _PARAMETER(n);. 
baf60 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69   return 0;.#endi
baf70 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  f.}../*.** An in
baf80 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
baf90 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 74 20 72  llowing object r
bafa0 65 63 6f 72 64 73 20 74 68 65 20 6c 6f 63 61 74  ecords the locat
bafb0 69 6f 6e 20 6f 66 0a 2a 2a 20 65 61 63 68 20 75  ion of.** each u
bafc0 6e 75 73 65 64 20 73 63 72 61 74 63 68 20 62 75  nused scratch bu
bafd0 66 66 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ffer..*/.typedef
bafe0 20 73 74 72 75 63 74 20 53 63 72 61 74 63 68 46   struct ScratchF
baff0 72 65 65 73 6c 6f 74 20 7b 0a 20 20 73 74 72 75  reeslot {.  stru
bb000 63 74 20 53 63 72 61 74 63 68 46 72 65 65 73 6c  ct ScratchFreesl
bb010 6f 74 20 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20  ot *pNext;   /* 
bb020 4e 65 78 74 20 75 6e 75 73 65 64 20 73 63 72 61  Next unused scra
bb030 74 63 68 20 62 75 66 66 65 72 20 2a 2f 0a 7d 20  tch buffer */.} 
bb040 53 63 72 61 74 63 68 46 72 65 65 73 6c 6f 74 3b  ScratchFreeslot;
bb050 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 74 65 20 69 6e  ../*.** State in
bb060 66 6f 72 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c 20  formation local 
bb070 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  to the memory al
bb080 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
bb090 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51  em..*/.static SQ
bb0a0 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20  LITE_WSD struct 
bb0b0 4d 65 6d 30 47 6c 6f 62 61 6c 20 7b 0a 20 20 73  Mem0Global {.  s
bb0c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
bb0d0 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  tex;         /* 
bb0e0 4d 75 74 65 78 20 74 6f 20 73 65 72 69 61 6c 69  Mutex to seriali
bb0f0 7a 65 20 61 63 63 65 73 73 20 2a 2f 0a 0a 20 20  ze access */..  
bb100 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 61 6c 61 72  /*.  ** The alar
bb110 6d 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69  m callback and i
bb120 74 73 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54  ts arguments.  T
bb130 68 65 20 6d 65 6d 30 2e 6d 75 74 65 78 20 6c 6f  he mem0.mutex lo
bb140 63 6b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20  ck will.  ** be 
bb150 68 65 6c 64 20 77 68 69 6c 65 20 74 68 65 20 63  held while the c
bb160 61 6c 6c 62 61 63 6b 20 69 73 20 72 75 6e 6e 69  allback is runni
bb170 6e 67 2e 20 20 52 65 63 75 72 73 69 76 65 20 63  ng.  Recursive c
bb180 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74  alls into.  ** t
bb190 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73  he memory subsys
bb1a0 74 65 6d 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c  tem are allowed,
bb1b0 20 62 75 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c   but no new call
bb1c0 62 61 63 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20  backs will be.  
bb1d0 2a 2a 20 69 73 73 75 65 64 2e 0a 20 20 2a 2f 0a  ** issued..  */.
bb1e0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
bb1f0 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b 0a  alarmThreshold;.
bb200 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43 61    void (*alarmCa
bb210 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20 73  llback)(void*, s
bb220 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74  qlite3_int64,int
bb230 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61 72 6d  );.  void *alarm
bb240 41 72 67 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  Arg;..  /*.  ** 
bb250 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20  Pointers to the 
bb260 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 47 6c  end of sqlite3Gl
bb270 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61  obalConfig.pScra
bb280 74 63 68 20 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20  tch memory.  ** 
bb290 28 73 6f 20 74 68 61 74 20 61 20 72 61 6e 67 65  (so that a range
bb2a0 20 74 65 73 74 20 63 61 6e 20 62 65 20 75 73 65   test can be use
bb2b0 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69  d to determine i
bb2c0 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a  f an allocation.
bb2d0 20 20 2a 2a 20 62 65 69 6e 67 20 66 72 65 65 64    ** being freed
bb2e0 20 63 61 6d 65 20 66 72 6f 6d 20 70 53 63 72 61   came from pScra
bb2f0 74 63 68 29 20 61 6e 64 20 61 20 70 6f 69 6e 74  tch) and a point
bb300 65 72 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f  er to the list o
bb310 66 0a 20 20 2a 2a 20 75 6e 75 73 65 64 20 73 63  f.  ** unused sc
bb320 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e  ratch allocation
bb330 73 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  s..  */.  void *
bb340 70 53 63 72 61 74 63 68 45 6e 64 3b 0a 20 20 53  pScratchEnd;.  S
bb350 63 72 61 74 63 68 46 72 65 65 73 6c 6f 74 20 2a  cratchFreeslot *
bb360 70 53 63 72 61 74 63 68 46 72 65 65 3b 0a 20 20  pScratchFree;.  
bb370 75 33 32 20 6e 53 63 72 61 74 63 68 46 72 65 65  u32 nScratchFree
bb380 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 75  ;..  /*.  ** Tru
bb390 65 20 69 66 20 68 65 61 70 20 69 73 20 6e 65 61  e if heap is nea
bb3a0 72 6c 79 20 22 66 75 6c 6c 22 20 77 68 65 72 65  rly "full" where
bb3b0 20 22 66 75 6c 6c 22 20 69 73 20 64 65 66 69 6e   "full" is defin
bb3c0 65 64 20 62 79 20 74 68 65 0a 20 20 2a 2a 20 73  ed by the.  ** s
bb3d0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70  qlite3_soft_heap
bb3e0 5f 6c 69 6d 69 74 28 29 20 73 65 74 74 69 6e 67  _limit() setting
bb3f0 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 61  ..  */.  int nea
bb400 72 6c 79 46 75 6c 6c 3b 0a 7d 20 6d 65 6d 30 20  rlyFull;.} mem0 
bb410 3d 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20  = { 0, 0, 0, 0, 
bb420 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23  0, 0, 0, 0 };..#
bb430 64 65 66 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42  define mem0 GLOB
bb440 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 30 47 6c  AL(struct Mem0Gl
bb450 6f 62 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a  obal, mem0)../*.
bb460 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
bb470 72 75 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 65  runs when the me
bb480 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 73  mory allocator s
bb490 65 65 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ees that the.** 
bb4a0 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  total memory all
bb4b0 6f 63 61 74 69 6f 6e 20 69 73 20 61 62 6f 75 74  ocation is about
bb4c0 20 74 6f 20 65 78 63 65 65 64 20 74 68 65 20 73   to exceed the s
bb4d0 6f 66 74 20 68 65 61 70 0a 2a 2a 20 6c 69 6d 69  oft heap.** limi
bb4e0 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
bb4f0 64 20 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45  d softHeapLimitE
bb500 6e 66 6f 72 63 65 72 28 0a 20 20 76 6f 69 64 20  nforcer(.  void 
bb510 2a 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73 71 6c  *NotUsed, .  sql
bb520 69 74 65 33 5f 69 6e 74 36 34 20 4e 6f 74 55 73  ite3_int64 NotUs
bb530 65 64 32 2c 0a 20 20 69 6e 74 20 61 6c 6c 6f 63  ed2,.  int alloc
bb540 53 69 7a 65 0a 29 7b 0a 20 20 55 4e 55 53 45 44  Size.){.  UNUSED
bb550 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
bb560 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
bb570 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73    sqlite3_releas
bb580 65 5f 6d 65 6d 6f 72 79 28 61 6c 6c 6f 63 53 69  e_memory(allocSi
bb590 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  ze);.}../*.** Ch
bb5a0 61 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63  ange the alarm c
bb5b0 61 6c 6c 62 61 63 6b 0a 2a 2f 0a 73 74 61 74 69  allback.*/.stati
bb5c0 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  c int sqlite3Mem
bb5d0 6f 72 79 41 6c 61 72 6d 28 0a 20 20 76 6f 69 64  oryAlarm(.  void
bb5e0 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69  (*xCallback)(voi
bb5f0 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65 33  d *pArg, sqlite3
bb600 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74 20  _int64 used,int 
bb610 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  N),.  void *pArg
bb620 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ,.  sqlite3_int6
bb630 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a  4 iThreshold.){.
bb640 20 20 69 6e 74 20 6e 55 73 65 64 3b 0a 20 20 73    int nUsed;.  s
bb650 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
bb660 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  er(mem0.mutex);.
bb670 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c    mem0.alarmCall
bb680 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b  back = xCallback
bb690 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72  ;.  mem0.alarmAr
bb6a0 67 20 3d 20 70 41 72 67 3b 0a 20 20 6d 65 6d 30  g = pArg;.  mem0
bb6b0 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20  .alarmThreshold 
bb6c0 3d 20 69 54 68 72 65 73 68 6f 6c 64 3b 0a 20 20  = iThreshold;.  
bb6d0 6e 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33 53  nUsed = sqlite3S
bb6e0 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54  tatusValue(SQLIT
bb6f0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
bb700 55 53 45 44 29 3b 0a 20 20 6d 65 6d 30 2e 6e 65  USED);.  mem0.ne
bb710 61 72 6c 79 46 75 6c 6c 20 3d 20 28 69 54 68 72  arlyFull = (iThr
bb720 65 73 68 6f 6c 64 3e 30 20 26 26 20 69 54 68 72  eshold>0 && iThr
bb730 65 73 68 6f 6c 64 3c 3d 6e 55 73 65 64 29 3b 0a  eshold<=nUsed);.
bb740 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
bb750 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78  leave(mem0.mutex
bb760 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
bb770 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65  TE_OK;.}..#ifnde
bb780 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
bb790 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44  PRECATED./*.** D
bb7a0 65 70 72 65 63 61 74 65 64 20 65 78 74 65 72 6e  eprecated extern
bb7b0 61 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49  al interface.  I
bb7c0 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c  nternal/core SQL
bb7d0 69 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75  ite code.** shou
bb7e0 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d  ld call sqlite3M
bb7f0 65 6d 6f 72 79 41 6c 61 72 6d 2e 0a 2a 2f 0a 53  emoryAlarm..*/.S
bb800 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
bb810 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61  lite3_memory_ala
bb820 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c  rm(.  void(*xCal
bb830 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72  lback)(void *pAr
bb840 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  g, sqlite3_int64
bb850 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20   used,int N),.  
bb860 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71  void *pArg,.  sq
bb870 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72  lite3_int64 iThr
bb880 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 72 65 74 75  eshold.){.  retu
bb890 72 6e 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79  rn sqlite3Memory
bb8a0 41 6c 61 72 6d 28 78 43 61 6c 6c 62 61 63 6b 2c  Alarm(xCallback,
bb8b0 20 70 41 72 67 2c 20 69 54 68 72 65 73 68 6f 6c   pArg, iThreshol
bb8c0 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  d);.}.#endif../*
bb8d0 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66 74  .** Set the soft
bb8e0 20 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69 74   heap-size limit
bb8f0 20 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72 79   for the library
bb900 2e 20 50 61 73 73 69 6e 67 20 61 20 7a 65 72 6f  . Passing a zero
bb910 20 6f 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76 65   or .** negative
bb920 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73   value indicates
bb930 20 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 51   no limit..*/.SQ
bb940 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
bb950 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 73  _int64 sqlite3_s
bb960 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 36 34  oft_heap_limit64
bb970 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e  (sqlite3_int64 n
bb980 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  ){.  sqlite3_int
bb990 36 34 20 70 72 69 6f 72 4c 69 6d 69 74 3b 0a 20  64 priorLimit;. 
bb9a0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 65   sqlite3_int64 e
bb9b0 78 63 65 73 73 3b 0a 23 69 66 6e 64 65 66 20 53  xcess;.#ifndef S
bb9c0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
bb9d0 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  NIT.  int rc = s
bb9e0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
bb9f0 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  e();.  if( rc ) 
bba00 72 65 74 75 72 6e 20 2d 31 3b 0a 23 65 6e 64 69  return -1;.#endi
bba10 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  f.  sqlite3_mute
bba20 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74  x_enter(mem0.mut
bba30 65 78 29 3b 0a 20 20 70 72 69 6f 72 4c 69 6d 69  ex);.  priorLimi
bba40 74 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68  t = mem0.alarmTh
bba50 72 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74  reshold;.  sqlit
bba60 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
bba70 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66  em0.mutex);.  if
bba80 28 20 6e 3c 30 20 29 20 72 65 74 75 72 6e 20 70  ( n<0 ) return p
bba90 72 69 6f 72 4c 69 6d 69 74 3b 0a 20 20 69 66 28  riorLimit;.  if(
bbaa0 20 6e 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69   n>0 ){.    sqli
bbab0 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 73  te3MemoryAlarm(s
bbac0 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f  oftHeapLimitEnfo
bbad0 72 63 65 72 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d  rcer, 0, n);.  }
bbae0 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
bbaf0 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 30 2c 20  3MemoryAlarm(0, 
bbb00 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 65 78 63  0, 0);.  }.  exc
bbb10 65 73 73 20 3d 20 73 71 6c 69 74 65 33 5f 6d 65  ess = sqlite3_me
bbb20 6d 6f 72 79 5f 75 73 65 64 28 29 20 2d 20 6e 3b  mory_used() - n;
bbb30 0a 20 20 69 66 28 20 65 78 63 65 73 73 3e 30 20  .  if( excess>0 
bbb40 29 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73  ) sqlite3_releas
bbb50 65 5f 6d 65 6d 6f 72 79 28 28 69 6e 74 29 28 65  e_memory((int)(e
bbb60 78 63 65 73 73 20 26 20 30 78 37 66 66 66 66 66  xcess & 0x7fffff
bbb70 66 66 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  ff));.  return p
bbb80 72 69 6f 72 4c 69 6d 69 74 3b 0a 7d 0a 53 51 4c  riorLimit;.}.SQL
bbb90 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
bbba0 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c  ite3_soft_heap_l
bbbb0 69 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20 20 69  imit(int n){.  i
bbbc0 66 28 20 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a  f( n<0 ) n = 0;.
bbbd0 20 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68    sqlite3_soft_h
bbbe0 65 61 70 5f 6c 69 6d 69 74 36 34 28 6e 29 3b 0a  eap_limit64(n);.
bbbf0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  }../*.** Initial
bbc00 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  ize the memory a
bbc10 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
bbc20 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  tem..*/.SQLITE_P
bbc30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
bbc40 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69  e3MallocInit(voi
bbc50 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  d){.  if( sqlite
bbc60 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
bbc70 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 7b 0a 20 20  xMalloc==0 ){.  
bbc80 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44    sqlite3MemSetD
bbc90 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d 0a 20 20  efault();.  }.  
bbca0 6d 65 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c  memset(&mem0, 0,
bbcb0 20 73 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a   sizeof(mem0));.
bbcc0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
bbcd0 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d  balConfig.bCoreM
bbce0 75 74 65 78 20 29 7b 0a 20 20 20 20 6d 65 6d 30  utex ){.    mem0
bbcf0 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33  .mutex = sqlite3
bbd00 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
bbd10 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
bbd20 45 4d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  EM);.  }.  if( s
bbd30 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
bbd40 69 67 2e 70 53 63 72 61 74 63 68 20 26 26 20 73  ig.pScratch && s
bbd50 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
bbd60 69 67 2e 73 7a 53 63 72 61 74 63 68 3e 3d 31 30  ig.szScratch>=10
bbd70 30 0a 20 20 20 20 20 20 26 26 20 73 71 6c 69 74  0.      && sqlit
bbd80 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
bbd90 53 63 72 61 74 63 68 3e 30 20 29 7b 0a 20 20 20  Scratch>0 ){.   
bbda0 20 69 6e 74 20 69 2c 20 6e 2c 20 73 7a 3b 0a 20   int i, n, sz;. 
bbdb0 20 20 20 53 63 72 61 74 63 68 46 72 65 65 73 6c     ScratchFreesl
bbdc0 6f 74 20 2a 70 53 6c 6f 74 3b 0a 20 20 20 20 73  ot *pSlot;.    s
bbdd0 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73  z = ROUNDDOWN8(s
bbde0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
bbdf0 69 67 2e 73 7a 53 63 72 61 74 63 68 29 3b 0a 20  ig.szScratch);. 
bbe00 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
bbe10 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68  Config.szScratch
bbe20 20 3d 20 73 7a 3b 0a 20 20 20 20 70 53 6c 6f 74   = sz;.    pSlot
bbe30 20 3d 20 28 53 63 72 61 74 63 68 46 72 65 65 73   = (ScratchFrees
bbe40 6c 6f 74 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62  lot*)sqlite3Glob
bbe50 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
bbe60 68 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74  h;.    n = sqlit
bbe70 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
bbe80 53 63 72 61 74 63 68 3b 0a 20 20 20 20 6d 65 6d  Scratch;.    mem
bbe90 30 2e 70 53 63 72 61 74 63 68 46 72 65 65 20 3d  0.pScratchFree =
bbea0 20 70 53 6c 6f 74 3b 0a 20 20 20 20 6d 65 6d 30   pSlot;.    mem0
bbeb0 2e 6e 53 63 72 61 74 63 68 46 72 65 65 20 3d 20  .nScratchFree = 
bbec0 6e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  n;.    for(i=0; 
bbed0 69 3c 6e 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20  i<n-1; i++){.   
bbee0 20 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 20     pSlot->pNext 
bbef0 3d 20 28 53 63 72 61 74 63 68 46 72 65 65 73 6c  = (ScratchFreesl
bbf00 6f 74 2a 29 28 73 7a 2b 28 63 68 61 72 2a 29 70  ot*)(sz+(char*)p
bbf10 53 6c 6f 74 29 3b 0a 20 20 20 20 20 20 70 53 6c  Slot);.      pSl
bbf20 6f 74 20 3d 20 70 53 6c 6f 74 2d 3e 70 4e 65 78  ot = pSlot->pNex
bbf30 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 6c  t;.    }.    pSl
bbf40 6f 74 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20  ot->pNext = 0;. 
bbf50 20 20 20 6d 65 6d 30 2e 70 53 63 72 61 74 63 68     mem0.pScratch
bbf60 45 6e 64 20 3d 20 28 76 6f 69 64 2a 29 26 70 53  End = (void*)&pS
bbf70 6c 6f 74 5b 31 5d 3b 0a 20 20 7d 65 6c 73 65 7b  lot[1];.  }else{
bbf80 0a 20 20 20 20 6d 65 6d 30 2e 70 53 63 72 61 74  .    mem0.pScrat
bbf90 63 68 45 6e 64 20 3d 20 30 3b 0a 20 20 20 20 73  chEnd = 0;.    s
bbfa0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
bbfb0 69 67 2e 70 53 63 72 61 74 63 68 20 3d 20 30 3b  ig.pScratch = 0;
bbfc0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
bbfd0 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74  alConfig.szScrat
bbfe0 63 68 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  ch = 0;.    sqli
bbff0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
bc000 6e 53 63 72 61 74 63 68 20 3d 20 30 3b 0a 20 20  nScratch = 0;.  
bc010 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  }.  if( sqlite3G
bc020 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67  lobalConfig.pPag
bc030 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47  e==0 || sqlite3G
bc040 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61  lobalConfig.szPa
bc050 67 65 3c 35 31 32 0a 20 20 20 20 20 20 7c 7c 20  ge<512.      || 
bc060 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
bc070 66 69 67 2e 6e 50 61 67 65 3c 31 20 29 7b 0a 20  fig.nPage<1 ){. 
bc080 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
bc090 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 30  Config.pPage = 0
bc0a0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  ;.    sqlite3Glo
bc0b0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65  balConfig.szPage
bc0c0 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 0;.    sqlite
bc0d0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50  3GlobalConfig.nP
bc0e0 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  age = 0;.  }.  r
bc0f0 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f  eturn sqlite3Glo
bc100 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69  balConfig.m.xIni
bc110 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  t(sqlite3GlobalC
bc120 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61  onfig.m.pAppData
bc130 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
bc140 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 68  rn true if the h
bc150 65 61 70 20 69 73 20 63 75 72 72 65 6e 74 6c 79  eap is currently
bc160 20 75 6e 64 65 72 20 6d 65 6d 6f 72 79 20 70 72   under memory pr
bc170 65 73 73 75 72 65 20 2d 20 69 6e 20 6f 74 68 65  essure - in othe
bc180 72 0a 2a 2a 20 77 6f 72 64 73 20 69 66 20 74 68  r.** words if th
bc190 65 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 61 70  e amount of heap
bc1a0 20 75 73 65 64 20 69 73 20 63 6c 6f 73 65 20 74   used is close t
bc1b0 6f 20 74 68 65 20 6c 69 6d 69 74 20 73 65 74 20  o the limit set 
bc1c0 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 6f  by.** sqlite3_so
bc1d0 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 2e  ft_heap_limit().
bc1e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
bc1f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 48 65  TE int sqlite3He
bc200 61 70 4e 65 61 72 6c 79 46 75 6c 6c 28 76 6f 69  apNearlyFull(voi
bc210 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 6d 65 6d  d){.  return mem
bc220 30 2e 6e 65 61 72 6c 79 46 75 6c 6c 3b 0a 7d 0a  0.nearlyFull;.}.
bc230 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c  ./*.** Deinitial
bc240 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  ize the memory a
bc250 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
bc260 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  tem..*/.SQLITE_P
bc270 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
bc280 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f 69  te3MallocEnd(voi
bc290 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  d){.  if( sqlite
bc2a0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
bc2b0 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 20 20  xShutdown ){.   
bc2c0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
bc2d0 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64 6f 77 6e  nfig.m.xShutdown
bc2e0 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
bc2f0 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 29  nfig.m.pAppData)
bc300 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26  ;.  }.  memset(&
bc310 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28  mem0, 0, sizeof(
bc320 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  mem0));.}../*.**
bc330 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75   Return the amou
bc340 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 75 72  nt of memory cur
bc350 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f  rently checked o
bc360 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ut..*/.SQLITE_AP
bc370 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  I sqlite3_int64 
bc380 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75  sqlite3_memory_u
bc390 73 65 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74  sed(void){.  int
bc3a0 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c 69 74 65   n, mx;.  sqlite
bc3b0 33 5f 69 6e 74 36 34 20 72 65 73 3b 0a 20 20 73  3_int64 res;.  s
bc3c0 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 53 51  qlite3_status(SQ
bc3d0 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f  LITE_STATUS_MEMO
bc3e0 52 59 5f 55 53 45 44 2c 20 26 6e 2c 20 26 6d 78  RY_USED, &n, &mx
bc3f0 2c 20 30 29 3b 0a 20 20 72 65 73 20 3d 20 28 73  , 0);.  res = (s
bc400 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 6e 3b 20  qlite3_int64)n; 
bc410 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 6e 64 20   /* Work around 
bc420 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e 64 20 43  bug in Borland C
bc430 2e 20 54 69 63 6b 65 74 20 23 33 32 31 36 20 2a  . Ticket #3216 *
bc440 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a  /.  return res;.
bc450 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
bc460 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75  the maximum amou
bc470 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68 61  nt of memory tha
bc480 74 20 68 61 73 20 65 76 65 72 20 62 65 65 6e 0a  t has ever been.
bc490 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 20 73  ** checked out s
bc4a0 69 6e 63 65 20 65 69 74 68 65 72 20 74 68 65 20  ince either the 
bc4b0 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 69  beginning of thi
bc4c0 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20 6f 72 20  s process.** or 
bc4d0 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 20 72  since the most r
bc4e0 65 63 65 6e 74 20 72 65 73 65 74 2e 0a 2a 2f 0a  ecent reset..*/.
bc4f0 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
bc500 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e3_int64 sqlite3
bc510 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65  _memory_highwate
bc520 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67 29  r(int resetFlag)
bc530 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20  {.  int n, mx;. 
bc540 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 72   sqlite3_int64 r
bc550 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  es;.  sqlite3_st
bc560 61 74 75 73 28 53 51 4c 49 54 45 5f 53 54 41 54  atus(SQLITE_STAT
bc570 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20  US_MEMORY_USED, 
bc580 26 6e 2c 20 26 6d 78 2c 20 72 65 73 65 74 46 6c  &n, &mx, resetFl
bc590 61 67 29 3b 0a 20 20 72 65 73 20 3d 20 28 73 71  ag);.  res = (sq
bc5a0 6c 69 74 65 33 5f 69 6e 74 36 34 29 6d 78 3b 20  lite3_int64)mx; 
bc5b0 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 6e 64 20   /* Work around 
bc5c0 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e 64 20 43  bug in Borland C
bc5d0 2e 20 54 69 63 6b 65 74 20 23 33 32 31 36 20 2a  . Ticket #3216 *
bc5e0 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a  /.  return res;.
bc5f0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67 67 65 72  }../*.** Trigger
bc600 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a 2f 0a 73   the alarm .*/.s
bc610 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
bc620 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 69 6e  e3MallocAlarm(in
bc630 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f 69 64  t nByte){.  void
bc640 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f   (*xCallback)(vo
bc650 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36  id*,sqlite3_int6
bc660 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65  4,int);.  sqlite
bc670 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 65 64 3b  3_int64 nowUsed;
bc680 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20  .  void *pArg;. 
bc690 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43   if( mem0.alarmC
bc6a0 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20 72 65 74  allback==0 ) ret
bc6b0 75 72 6e 3b 0a 20 20 78 43 61 6c 6c 62 61 63 6b  urn;.  xCallback
bc6c0 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c   = mem0.alarmCal
bc6d0 6c 62 61 63 6b 3b 0a 20 20 6e 6f 77 55 73 65 64  lback;.  nowUsed
bc6e0 20 3d 20 73 71 6c 69 74 65 33 53 74 61 74 75 73   = sqlite3Status
bc6f0 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41  Value(SQLITE_STA
bc700 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29  TUS_MEMORY_USED)
bc710 3b 0a 20 20 70 41 72 67 20 3d 20 6d 65 6d 30 2e  ;.  pArg = mem0.
bc720 61 6c 61 72 6d 41 72 67 3b 0a 20 20 6d 65 6d 30  alarmArg;.  mem0
bc730 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d  .alarmCallback =
bc740 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   0;.  sqlite3_mu
bc750 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d  tex_leave(mem0.m
bc760 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c 62 61  utex);.  xCallba
bc770 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73 65 64  ck(pArg, nowUsed
bc780 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 71 6c 69  , nByte);.  sqli
bc790 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
bc7a0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d  mem0.mutex);.  m
bc7b0 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63  em0.alarmCallbac
bc7c0 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20  k = xCallback;. 
bc7d0 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d   mem0.alarmArg =
bc7e0 20 70 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pArg;.}../*.** 
bc7f0 44 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  Do a memory allo
bc800 63 61 74 69 6f 6e 20 77 69 74 68 20 73 74 61 74  cation with stat
bc810 69 73 74 69 63 73 20 61 6e 64 20 61 6c 61 72 6d  istics and alarm
bc820 73 2e 20 20 41 73 73 75 6d 65 20 74 68 65 0a 2a  s.  Assume the.*
bc830 2a 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64  * lock is alread
bc840 79 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69  y held..*/.stati
bc850 63 20 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74 68  c int mallocWith
bc860 41 6c 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f 69  Alarm(int n, voi
bc870 64 20 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20 6e  d **pp){.  int n
bc880 46 75 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70 3b  Full;.  void *p;
bc890 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
bc8a0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
bc8b0 6d 30 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e  m0.mutex) );.  n
bc8c0 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 6c  Full = sqlite3Gl
bc8d0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f  obalConfig.m.xRo
bc8e0 75 6e 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c 69  undup(n);.  sqli
bc8f0 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c  te3StatusSet(SQL
bc900 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f  ITE_STATUS_MALLO
bc910 43 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69 66  C_SIZE, n);.  if
bc920 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c  ( mem0.alarmCall
bc930 62 61 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 69  back!=0 ){.    i
bc940 6e 74 20 6e 55 73 65 64 20 3d 20 73 71 6c 69 74  nt nUsed = sqlit
bc950 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 51  e3StatusValue(SQ
bc960 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f  LITE_STATUS_MEMO
bc970 52 59 5f 55 53 45 44 29 3b 0a 20 20 20 20 69 66  RY_USED);.    if
bc980 28 20 6e 55 73 65 64 20 3e 3d 20 6d 65 6d 30 2e  ( nUsed >= mem0.
bc990 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 2d  alarmThreshold -
bc9a0 20 6e 46 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20   nFull ){.      
bc9b0 6d 65 6d 30 2e 6e 65 61 72 6c 79 46 75 6c 6c 20  mem0.nearlyFull 
bc9c0 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 1;.      sqlit
bc9d0 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46  e3MallocAlarm(nF
bc9e0 75 6c 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ull);.    }else{
bc9f0 0a 20 20 20 20 20 20 6d 65 6d 30 2e 6e 65 61 72  .      mem0.near
bca00 6c 79 46 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20  lyFull = 0;.    
bca10 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69  }.  }.  p = sqli
bca20 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
bca30 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29  m.xMalloc(nFull)
bca40 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
bca50 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
bca60 4e 41 47 45 4d 45 4e 54 0a 20 20 69 66 28 20 70  NAGEMENT.  if( p
bca70 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72  ==0 && mem0.alar
bca80 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20  mCallback ){.   
bca90 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c   sqlite3MallocAl
bcaa0 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20  arm(nFull);.    
bcab0 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  p = sqlite3Globa
bcac0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f  lConfig.m.xMallo
bcad0 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 23 65  c(nFull);.  }.#e
bcae0 6e 64 69 66 0a 20 20 69 66 28 20 70 20 29 7b 0a  ndif.  if( p ){.
bcaf0 20 20 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69      nFull = sqli
bcb00 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29  te3MallocSize(p)
bcb10 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
bcb20 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
bcb30 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44  ATUS_MEMORY_USED
bcb40 2c 20 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 73 71  , nFull);.    sq
bcb50 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
bcb60 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c  QLITE_STATUS_MAL
bcb70 4c 4f 43 5f 43 4f 55 4e 54 2c 20 31 29 3b 0a 20  LOC_COUNT, 1);. 
bcb80 20 7d 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20   }.  *pp = p;.  
bcb90 72 65 74 75 72 6e 20 6e 46 75 6c 6c 3b 0a 7d 0a  return nFull;.}.
bcba0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
bcbb0 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 72 6f  memory.  This ro
bcbc0 75 74 69 6e 65 20 69 73 20 6c 69 6b 65 20 73 71  utine is like sq
bcbd0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 65  lite3_malloc() e
bcbe0 78 63 65 70 74 20 74 68 61 74 20 69 74 0a 2a 2a  xcept that it.**
bcbf0 20 61 73 73 75 6d 65 73 20 74 68 65 20 6d 65 6d   assumes the mem
bcc00 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20 68 61  ory subsystem ha
bcc10 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69  s already been i
bcc20 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53  nitialized..*/.S
bcc30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
bcc40 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  id *sqlite3Mallo
bcc50 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64  c(int n){.  void
bcc60 20 2a 70 3b 0a 20 20 69 66 28 20 6e 3c 3d 30 20   *p;.  if( n<=0 
bcc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
bcc80 20 49 4d 50 3a 20 52 2d 36 35 33 31 32 2d 30 34   IMP: R-65312-04
bcc90 39 31 37 20 2a 2f 20 0a 20 20 20 7c 7c 20 6e 3e  917 */ .   || n>
bcca0 3d 30 78 37 66 66 66 66 66 30 30 0a 20 20 29 7b  =0x7fffff00.  ){
bccb0 0a 20 20 20 20 2f 2a 20 41 20 6d 65 6d 6f 72 79  .    /* A memory
bccc0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61   allocation of a
bccd0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
bcce0 20 77 68 69 63 68 20 69 73 20 6e 65 61 72 20 74   which is near t
bccf0 68 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a  he maximum.    *
bcd00 2a 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  * signed integer
bcd10 20 76 61 6c 75 65 20 6d 69 67 68 74 20 63 61 75   value might cau
bcd20 73 65 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 76  se an integer ov
bcd30 65 72 66 6c 6f 77 20 69 6e 73 69 64 65 20 6f 66  erflow inside of
bcd40 20 74 68 65 0a 20 20 20 20 2a 2a 20 78 4d 61 6c   the.    ** xMal
bcd50 6c 6f 63 28 29 2e 20 20 48 65 6e 63 65 20 77 65  loc().  Hence we
bcd60 20 6c 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d   limit the maxim
bcd70 75 6d 20 73 69 7a 65 20 74 6f 20 30 78 37 66 66  um size to 0x7ff
bcd80 66 66 66 30 30 2c 20 67 69 76 69 6e 67 0a 20 20  fff00, giving.  
bcd90 20 20 2a 2a 20 32 35 35 20 62 79 74 65 73 20 6f    ** 255 bytes o
bcda0 66 20 6f 76 65 72 68 65 61 64 2e 20 20 53 51 4c  f overhead.  SQL
bcdb0 69 74 65 20 69 74 73 65 6c 66 20 77 69 6c 6c 20  ite itself will 
bcdc0 6e 65 76 65 72 20 75 73 65 20 61 6e 79 74 68 69  never use anythi
bcdd0 6e 67 20 6e 65 61 72 0a 20 20 20 20 2a 2a 20 74  ng near.    ** t
bcde0 68 69 73 20 61 6d 6f 75 6e 74 2e 20 20 54 68 65  his amount.  The
bcdf0 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 72 65 61   only way to rea
bce00 63 68 20 74 68 65 20 6c 69 6d 69 74 20 69 73 20  ch the limit is 
bce10 77 69 74 68 20 73 71 6c 69 74 65 33 5f 6d 61 6c  with sqlite3_mal
bce20 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20 70 20 3d  loc() */.    p =
bce30 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
bce40 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
bce50 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a  fig.bMemstat ){.
bce60 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
bce70 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74  x_enter(mem0.mut
bce80 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f 63 57  ex);.    mallocW
bce90 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b  ithAlarm(n, &p);
bcea0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
bceb0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75  ex_leave(mem0.mu
bcec0 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  tex);.  }else{. 
bced0 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c     p = sqlite3Gl
bcee0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61  obalConfig.m.xMa
bcef0 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 20 61  lloc(n);.  }.  a
bcf00 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54  ssert( EIGHT_BYT
bcf10 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 29 20 29  E_ALIGNMENT(p) )
bcf20 3b 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 30 34 36  ;  /* IMP: R-046
bcf30 37 35 2d 34 34 38 35 30 20 2a 2f 0a 20 20 72 65  75-44850 */.  re
bcf40 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
bcf50 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   This version of
bcf60 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
bcf70 63 61 74 69 6f 6e 20 69 73 20 66 6f 72 20 75 73  cation is for us
bcf80 65 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61  e by the applica
bcf90 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 74 20 6d  tion..** First m
bcfa0 61 6b 65 20 73 75 72 65 20 74 68 65 20 6d 65 6d  ake sure the mem
bcfb0 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20 69 73  ory subsystem is
bcfc0 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68   initialized, th
bcfd0 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c  en do the.** all
bcfe0 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  ocation..*/.SQLI
bcff0 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
bd000 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 20  ite3_malloc(int 
bd010 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  n){.#ifndef SQLI
bd020 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
bd030 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69  .  if( sqlite3_i
bd040 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65  nitialize() ) re
bd050 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20  turn 0;.#endif. 
bd060 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4d   return sqlite3M
bd070 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a  alloc(n);.}../*.
bd080 2a 2a 20 45 61 63 68 20 74 68 72 65 61 64 20 6d  ** Each thread m
bd090 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 73  ay only have a s
bd0a0 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e 64 69 6e  ingle outstandin
bd0b0 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f  g allocation fro
bd0c0 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68 4d 61 6c  m.** xScratchMal
bd0d0 6c 6f 63 28 29 2e 20 20 57 65 20 76 65 72 69 66  loc().  We verif
bd0e0 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e  y this constrain
bd0f0 74 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65 2d  t in the single-
bd100 74 68 72 65 61 64 65 64 0a 2a 2a 20 63 61 73 65  threaded.** case
bd110 20 62 79 20 73 65 74 74 69 6e 67 20 73 63 72 61   by setting scra
bd120 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74 6f 20 31  tchAllocOut to 1
bd130 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74   when an allocat
bd140 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 73 74 61  ion.** is outsta
bd150 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e 67 20 69  nding clearing i
bd160 74 20 77 68 65 6e 20 74 68 65 20 61 6c 6c 6f 63  t when the alloc
bd170 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e 0a  ation is freed..
bd180 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
bd190 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21  READSAFE==0 && !
bd1a0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a  defined(NDEBUG).
bd1b0 73 74 61 74 69 63 20 69 6e 74 20 73 63 72 61 74  static int scrat
bd1c0 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a  chAllocOut = 0;.
bd1d0 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41  #endif.../*.** A
bd1e0 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74  llocate memory t
bd1f0 68 61 74 20 69 73 20 74 6f 20 62 65 20 75 73 65  hat is to be use
bd200 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 20 72  d and released r
bd210 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a 20 54 68  ight away..** Th
bd220 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69  is routine is si
bd230 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f 63 61 28  milar to alloca(
bd240 29 20 69 6e 20 74 68 61 74 20 69 74 20 69 73 20  ) in that it is 
bd250 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a 2a 2a 20  not intended.** 
bd260 66 6f 72 20 73 69 74 75 61 74 69 6f 6e 73 20 77  for situations w
bd270 68 65 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  here the memory 
bd280 6d 69 67 68 74 20 62 65 20 68 65 6c 64 20 6c 6f  might be held lo
bd290 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69 73 0a 2a  ng-term.  This.*
bd2a0 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 74  * routine is int
bd2b0 65 6e 64 65 64 20 74 6f 20 67 65 74 20 6d 65 6d  ended to get mem
bd2c0 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61 72 67 65  ory to old large
bd2d0 20 74 72 61 6e 73 69 65 6e 74 20 64 61 74 61 0a   transient data.
bd2e0 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 74 68  ** structures th
bd2f0 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 6e 6f 72  at would not nor
bd300 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20 74 68 65  mally fit on the
bd310 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a 2a 2a 20   stack of an.** 
bd320 65 6d 62 65 64 64 65 64 20 70 72 6f 63 65 73 73  embedded process
bd330 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  or..*/.SQLITE_PR
bd340 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
bd350 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63  te3ScratchMalloc
bd360 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20  (int n){.  void 
bd370 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e  *p;.  assert( n>
bd380 30 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  0 );..  sqlite3_
bd390 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30  mutex_enter(mem0
bd3a0 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6d  .mutex);.  if( m
bd3b0 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65  em0.nScratchFree
bd3c0 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   && sqlite3Globa
bd3d0 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63  lConfig.szScratc
bd3e0 68 3e 3d 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20  h>=n ){.    p = 
bd3f0 6d 65 6d 30 2e 70 53 63 72 61 74 63 68 46 72 65  mem0.pScratchFre
bd400 65 3b 0a 20 20 20 20 6d 65 6d 30 2e 70 53 63 72  e;.    mem0.pScr
bd410 61 74 63 68 46 72 65 65 20 3d 20 6d 65 6d 30 2e  atchFree = mem0.
bd420 70 53 63 72 61 74 63 68 46 72 65 65 2d 3e 70 4e  pScratchFree->pN
bd430 65 78 74 3b 0a 20 20 20 20 6d 65 6d 30 2e 6e 53  ext;.    mem0.nS
bd440 63 72 61 74 63 68 46 72 65 65 2d 2d 3b 0a 20 20  cratchFree--;.  
bd450 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41    sqlite3StatusA
bd460 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dd(SQLITE_STATUS
bd470 5f 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20 31  _SCRATCH_USED, 1
bd480 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  );.    sqlite3St
bd490 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53  atusSet(SQLITE_S
bd4a0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49  TATUS_SCRATCH_SI
bd4b0 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69  ZE, n);.    sqli
bd4c0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
bd4d0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d  mem0.mutex);.  }
bd4e0 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 73 71  else{.    if( sq
bd4f0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
bd500 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20  g.bMemstat ){.  
bd510 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
bd520 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54  sSet(SQLITE_STAT
bd530 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 2c  US_SCRATCH_SIZE,
bd540 20 6e 29 3b 0a 20 20 20 20 20 20 6e 20 3d 20 6d   n);.      n = m
bd550 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 6e  allocWithAlarm(n
bd560 2c 20 26 70 29 3b 0a 20 20 20 20 20 20 69 66 28  , &p);.      if(
bd570 20 70 20 29 20 73 71 6c 69 74 65 33 53 74 61 74   p ) sqlite3Stat
bd580 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
bd590 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52  TUS_SCRATCH_OVER
bd5a0 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 20 20 20  FLOW, n);.      
bd5b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
bd5c0 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ave(mem0.mutex);
bd5d0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
bd5e0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
bd5f0 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78  leave(mem0.mutex
bd600 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c  );.      p = sql
bd610 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
bd620 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20  .m.xMalloc(n);. 
bd630 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
bd640 4d 65 6d 64 65 62 75 67 53 65 74 54 79 70 65 28  MemdebugSetType(
bd650 70 2c 20 4d 45 4d 54 59 50 45 5f 53 43 52 41 54  p, MEMTYPE_SCRAT
bd660 43 48 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  CH);.  }.  asser
bd670 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
bd680 5f 6e 6f 74 68 65 6c 64 28 6d 65 6d 30 2e 6d 75  _notheld(mem0.mu
bd690 74 65 78 29 20 29 3b 0a 0a 0a 23 69 66 20 53 51  tex) );...#if SQ
bd6a0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d  LITE_THREADSAFE=
bd6b0 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e  =0 && !defined(N
bd6c0 44 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72 69  DEBUG).  /* Veri
bd6d0 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20  fy that no more 
bd6e0 74 68 61 6e 20 74 77 6f 20 73 63 72 61 74 63 68  than two scratch
bd6f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 70 65 72   allocations per
bd700 20 74 68 72 65 61 64 0a 20 20 2a 2a 20 61 72 65   thread.  ** are
bd710 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20   outstanding at 
bd720 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73  one time.  (This
bd730 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64   is only checked
bd740 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e   in the.  ** sin
bd750 67 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61 73  gle-threaded cas
bd760 65 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67  e since checking
bd770 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68   in the multi-th
bd780 72 65 61 64 65 64 20 63 61 73 65 0a 20 20 2a 2a  readed case.  **
bd790 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d   would be much m
bd7a0 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e  ore complicated.
bd7b0 29 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73  ) */.  assert( s
bd7c0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3c 3d  cratchAllocOut<=
bd7d0 31 20 29 3b 0a 20 20 69 66 28 20 70 20 29 20 73  1 );.  if( p ) s
bd7e0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 2b 2b  cratchAllocOut++
bd7f0 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75  ;.#endif..  retu
bd800 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  rn p;.}.SQLITE_P
bd810 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
bd820 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28 76  te3ScratchFree(v
bd830 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  oid *p){.  if( p
bd840 20 29 7b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f   ){..#if SQLITE_
bd850 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26  THREADSAFE==0 &&
bd860 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
bd870 29 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20  ).    /* Verify 
bd880 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61  that no more tha
bd890 6e 20 74 77 6f 20 73 63 72 61 74 63 68 20 61 6c  n two scratch al
bd8a0 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72  location per thr
bd8b0 65 61 64 0a 20 20 20 20 2a 2a 20 69 73 20 6f 75  ead.    ** is ou
bd8c0 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65  tstanding at one
bd8d0 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73   time.  (This is
bd8e0 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e   only checked in
bd8f0 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 69 6e 67   the.    ** sing
bd900 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 65  le-threaded case
bd910 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20   since checking 
bd920 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72  in the multi-thr
bd930 65 61 64 65 64 20 63 61 73 65 0a 20 20 20 20 2a  eaded case.    *
bd940 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20  * would be much 
bd950 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64  more complicated
bd960 2e 29 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  .) */.    assert
bd970 28 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75  ( scratchAllocOu
bd980 74 3e 3d 31 20 26 26 20 73 63 72 61 74 63 68 41  t>=1 && scratchA
bd990 6c 6c 6f 63 4f 75 74 3c 3d 32 20 29 3b 0a 20 20  llocOut<=2 );.  
bd9a0 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75    scratchAllocOu
bd9b0 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20  t--;.#endif..   
bd9c0 20 69 66 28 20 70 3e 3d 73 71 6c 69 74 65 33 47   if( p>=sqlite3G
bd9d0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72  lobalConfig.pScr
bd9e0 61 74 63 68 20 26 26 20 70 3c 6d 65 6d 30 2e 70  atch && p<mem0.p
bd9f0 53 63 72 61 74 63 68 45 6e 64 20 29 7b 0a 20 20  ScratchEnd ){.  
bda00 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 6d      /* Release m
bda10 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65 20 53  emory from the S
bda20 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52  QLITE_CONFIG_SCR
bda30 41 54 43 48 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  ATCH allocation 
bda40 2a 2f 0a 20 20 20 20 20 20 53 63 72 61 74 63 68  */.      Scratch
bda50 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74 3b  Freeslot *pSlot;
bda60 0a 20 20 20 20 20 20 70 53 6c 6f 74 20 3d 20 28  .      pSlot = (
bda70 53 63 72 61 74 63 68 46 72 65 65 73 6c 6f 74 2a  ScratchFreeslot*
bda80 29 70 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  )p;.      sqlite
bda90 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
bdaa0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  m0.mutex);.     
bdab0 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 20 3d 20   pSlot->pNext = 
bdac0 6d 65 6d 30 2e 70 53 63 72 61 74 63 68 46 72 65  mem0.pScratchFre
bdad0 65 3b 0a 20 20 20 20 20 20 6d 65 6d 30 2e 70 53  e;.      mem0.pS
bdae0 63 72 61 74 63 68 46 72 65 65 20 3d 20 70 53 6c  cratchFree = pSl
bdaf0 6f 74 3b 0a 20 20 20 20 20 20 6d 65 6d 30 2e 6e  ot;.      mem0.n
bdb00 53 63 72 61 74 63 68 46 72 65 65 2b 2b 3b 0a 20  ScratchFree++;. 
bdb10 20 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d       assert( mem
bdb20 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 20 3c  0.nScratchFree <
bdb30 3d 20 28 75 33 32 29 73 71 6c 69 74 65 33 47 6c  = (u32)sqlite3Gl
bdb40 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61  obalConfig.nScra
bdb50 74 63 68 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  tch );.      sql
bdb60 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51  ite3StatusAdd(SQ
bdb70 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41  LITE_STATUS_SCRA
bdb80 54 43 48 5f 55 53 45 44 2c 20 2d 31 29 3b 0a 20  TCH_USED, -1);. 
bdb90 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
bdba0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75  ex_leave(mem0.mu
bdbb0 74 65 78 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  tex);.    }else{
bdbc0 0a 20 20 20 20 20 20 2f 2a 20 52 65 6c 65 61 73  .      /* Releas
bdbd0 65 20 6d 65 6d 6f 72 79 20 62 61 63 6b 20 74 6f  e memory back to
bdbe0 20 74 68 65 20 68 65 61 70 20 2a 2f 0a 20 20 20   the heap */.   
bdbf0 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
bdc00 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54 79 70  e3MemdebugHasTyp
bdc10 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 53 43 52  e(p, MEMTYPE_SCR
bdc20 41 54 43 48 29 20 29 3b 0a 20 20 20 20 20 20 61  ATCH) );.      a
bdc30 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 4d 65  ssert( sqlite3Me
bdc40 6d 64 65 62 75 67 4e 6f 54 79 70 65 28 70 2c 20  mdebugNoType(p, 
bdc50 7e 4d 45 4d 54 59 50 45 5f 53 43 52 41 54 43 48  ~MEMTYPE_SCRATCH
bdc60 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) );.      sqlit
bdc70 65 33 4d 65 6d 64 65 62 75 67 53 65 74 54 79 70  e3MemdebugSetTyp
bdc80 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 48 45 41  e(p, MEMTYPE_HEA
bdc90 50 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71  P);.      if( sq
bdca0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
bdcb0 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20  g.bMemstat ){.  
bdcc0 20 20 20 20 20 20 69 6e 74 20 69 53 69 7a 65 20        int iSize 
bdcd0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  = sqlite3MallocS
bdce0 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20  ize(p);.        
bdcf0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
bdd00 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ter(mem0.mutex);
bdd10 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
bdd20 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45  StatusAdd(SQLITE
bdd30 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f  _STATUS_SCRATCH_
bdd40 4f 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65  OVERFLOW, -iSize
bdd50 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
bdd60 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49  e3StatusAdd(SQLI
bdd70 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59  TE_STATUS_MEMORY
bdd80 5f 55 53 45 44 2c 20 2d 69 53 69 7a 65 29 3b 0a  _USED, -iSize);.
bdd90 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
bdda0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f  tatusAdd(SQLITE_
bddb0 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 43 4f  STATUS_MALLOC_CO
bddc0 55 4e 54 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  UNT, -1);.      
bddd0 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
bdde0 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29  onfig.m.xFree(p)
bddf0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
bde00 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
bde10 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  m0.mutex);.     
bde20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
bde30 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
bde40 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a  fig.m.xFree(p);.
bde50 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
bde60 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20  }.}../*.** TRUE 
bde70 69 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b 61 73  if p is a lookas
bde80 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ide memory alloc
bde90 61 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f  ation from db.*/
bdea0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
bdeb0 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73  OMIT_LOOKASIDE.s
bdec0 74 61 74 69 63 20 69 6e 74 20 69 73 4c 6f 6f 6b  tatic int isLook
bded0 61 73 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64  aside(sqlite3 *d
bdee0 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72  b, void *p){.  r
bdef0 65 74 75 72 6e 20 70 20 26 26 20 70 3e 3d 64 62  eturn p && p>=db
bdf00 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61  ->lookaside.pSta
bdf10 72 74 20 26 26 20 70 3c 64 62 2d 3e 6c 6f 6f 6b  rt && p<db->look
bdf20 61 73 69 64 65 2e 70 45 6e 64 3b 0a 7d 0a 23 65  aside.pEnd;.}.#e
bdf30 6c 73 65 0a 23 64 65 66 69 6e 65 20 69 73 4c 6f  lse.#define isLo
bdf40 6f 6b 61 73 69 64 65 28 41 2c 42 29 20 30 0a 23  okaside(A,B) 0.#
bdf50 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  endif../*.** Ret
bdf60 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20  urn the size of 
bdf70 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
bdf80 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 6f  ion previously o
bdf90 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
bdfa0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20  sqlite3Malloc() 
bdfb0 6f 72 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  or sqlite3_mallo
bdfc0 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  c()..*/.SQLITE_P
bdfd0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
bdfe0 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f 69  e3MallocSize(voi
bdff0 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  d *p){.  assert(
be000 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67   sqlite3Memdebug
be010 48 61 73 54 79 70 65 28 70 2c 20 4d 45 4d 54 59  HasType(p, MEMTY
be020 50 45 5f 48 45 41 50 29 20 29 3b 0a 20 20 61 73  PE_HEAP) );.  as
be030 73 65 72 74 28 20 73 71 6c 69 74 65 33 4d 65 6d  sert( sqlite3Mem
be040 64 65 62 75 67 4e 6f 54 79 70 65 28 70 2c 20 4d  debugNoType(p, M
be050 45 4d 54 59 50 45 5f 44 42 29 20 29 3b 0a 20 20  EMTYPE_DB) );.  
be060 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c  return sqlite3Gl
be070 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69  obalConfig.m.xSi
be080 7a 65 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  ze(p);.}.SQLITE_
be090 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
be0a0 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
be0b0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69  sqlite3 *db, voi
be0c0 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  d *p){.  assert(
be0d0 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65   db==0 || sqlite
be0e0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
be0f0 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28  >mutex) );.  if(
be100 20 64 62 20 26 26 20 69 73 4c 6f 6f 6b 61 73 69   db && isLookasi
be110 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20  de(db, p) ){.   
be120 20 72 65 74 75 72 6e 20 64 62 2d 3e 6c 6f 6f 6b   return db->look
be130 61 73 69 64 65 2e 73 7a 3b 0a 20 20 7d 65 6c 73  aside.sz;.  }els
be140 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  e{.    assert( s
be150 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61  qlite3MemdebugHa
be160 73 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45  sType(p, MEMTYPE
be170 5f 44 42 29 20 29 3b 0a 20 20 20 20 61 73 73 65  _DB) );.    asse
be180 72 74 28 20 73 71 6c 69 74 65 33 4d 65 6d 64 65  rt( sqlite3Memde
be190 62 75 67 48 61 73 54 79 70 65 28 70 2c 20 4d 45  bugHasType(p, ME
be1a0 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 7c  MTYPE_LOOKASIDE|
be1b0 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 20 29 3b  MEMTYPE_HEAP) );
be1c0 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 21  .    assert( db!
be1d0 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 4d 65 6d  =0 || sqlite3Mem
be1e0 64 65 62 75 67 4e 6f 54 79 70 65 28 70 2c 20 4d  debugNoType(p, M
be1f0 45 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45  EMTYPE_LOOKASIDE
be200 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) );.    return 
be210 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
be220 66 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a  fig.m.xSize(p);.
be230 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65    }.}../*.** Fre
be240 65 20 6d 65 6d 6f 72 79 20 70 72 65 76 69 6f 75  e memory previou
be250 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  sly obtained fro
be260 6d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  m sqlite3Malloc(
be270 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  )..*/.SQLITE_API
be280 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72   void sqlite3_fr
be290 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69  ee(void *p){.  i
be2a0 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
be2b0 3b 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 34 39 30  ;  /* IMP: R-490
be2c0 35 33 2d 35 34 35 35 34 20 2a 2f 0a 20 20 61 73  53-54554 */.  as
be2d0 73 65 72 74 28 20 73 71 6c 69 74 65 33 4d 65 6d  sert( sqlite3Mem
be2e0 64 65 62 75 67 4e 6f 54 79 70 65 28 70 2c 20 4d  debugNoType(p, M
be2f0 45 4d 54 59 50 45 5f 44 42 29 20 29 3b 0a 20 20  EMTYPE_DB) );.  
be300 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 4d  assert( sqlite3M
be310 65 6d 64 65 62 75 67 48 61 73 54 79 70 65 28 70  emdebugHasType(p
be320 2c 20 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 20  , MEMTYPE_HEAP) 
be330 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
be340 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
be350 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c  mstat ){.    sql
be360 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
be370 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
be380 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41    sqlite3StatusA
be390 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dd(SQLITE_STATUS
be3a0 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 73  _MEMORY_USED, -s
be3b0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65  qlite3MallocSize
be3c0 28 70 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  (p));.    sqlite
be3d0 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
be3e0 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f  E_STATUS_MALLOC_
be3f0 43 4f 55 4e 54 2c 20 2d 31 29 3b 0a 20 20 20 20  COUNT, -1);.    
be400 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
be410 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a  fig.m.xFree(p);.
be420 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
be430 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74  x_leave(mem0.mut
be440 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ex);.  }else{.  
be450 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
be460 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29  onfig.m.xFree(p)
be470 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  ;.  }.}../*.** F
be480 72 65 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20  ree memory that 
be490 6d 69 67 68 74 20 62 65 20 61 73 73 6f 63 69 61  might be associa
be4a0 74 65 64 20 77 69 74 68 20 61 20 70 61 72 74 69  ted with a parti
be4b0 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a  cular database.*
be4c0 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  * connection..*/
be4d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
be4e0 76 6f 69 64 20 73 71 6c 69 74 65 33 44 62 46 72  void sqlite3DbFr
be4f0 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ee(sqlite3 *db, 
be500 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65  void *p){.  asse
be510 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  rt( db==0 || sql
be520 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
be530 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
be540 69 66 28 20 64 62 20 29 7b 0a 20 20 20 20 69 66  if( db ){.    if
be550 28 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65  ( db->pnBytesFre
be560 65 64 20 29 7b 0a 20 20 20 20 20 20 2a 64 62 2d  ed ){.      *db-
be570 3e 70 6e 42 79 74 65 73 46 72 65 65 64 20 2b 3d  >pnBytesFreed +=
be580 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
be590 53 69 7a 65 28 64 62 2c 20 70 29 3b 0a 20 20 20  Size(db, p);.   
be5a0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
be5b0 0a 20 20 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61  .    if( isLooka
be5c0 73 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20  side(db, p) ){. 
be5d0 20 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c       LookasideSl
be5e0 6f 74 20 2a 70 42 75 66 20 3d 20 28 4c 6f 6f 6b  ot *pBuf = (Look
be5f0 61 73 69 64 65 53 6c 6f 74 2a 29 70 3b 0a 23 69  asideSlot*)p;.#i
be600 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
be610 20 20 20 20 20 2f 2a 20 54 72 61 73 68 20 61 6c       /* Trash al
be620 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74 68 65  l content in the
be630 20 62 75 66 66 65 72 20 62 65 69 6e 67 20 66 72   buffer being fr
be640 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 6d 65 6d  eed */.      mem
be650 73 65 74 28 70 2c 20 30 78 61 61 2c 20 64 62 2d  set(p, 0xaa, db-
be660 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 29 3b 0a  >lookaside.sz);.
be670 23 65 6e 64 69 66 0a 20 20 20 20 20 20 70 42 75  #endif.      pBu
be680 66 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c  f->pNext = db->l
be690 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 3b 0a  ookaside.pFree;.
be6a0 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73        db->lookas
be6b0 69 64 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66  ide.pFree = pBuf
be6c0 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  ;.      db->look
be6d0 61 73 69 64 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20  aside.nOut--;.  
be6e0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
be6f0 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  }.  }.  assert( 
be700 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 48  sqlite3MemdebugH
be710 61 73 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50  asType(p, MEMTYP
be720 45 5f 44 42 29 20 29 3b 0a 20 20 61 73 73 65 72  E_DB) );.  asser
be730 74 28 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62  t( sqlite3Memdeb
be740 75 67 48 61 73 54 79 70 65 28 70 2c 20 4d 45 4d  ugHasType(p, MEM
be750 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 7c 4d  TYPE_LOOKASIDE|M
be760 45 4d 54 59 50 45 5f 48 45 41 50 29 20 29 3b 0a  EMTYPE_HEAP) );.
be770 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20    assert( db!=0 
be780 7c 7c 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62  || sqlite3Memdeb
be790 75 67 4e 6f 54 79 70 65 28 70 2c 20 4d 45 4d 54  ugNoType(p, MEMT
be7a0 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 29 20 29  YPE_LOOKASIDE) )
be7b0 3b 0a 20 20 73 71 6c 69 74 65 33 4d 65 6d 64 65  ;.  sqlite3Memde
be7c0 62 75 67 53 65 74 54 79 70 65 28 70 2c 20 4d 45  bugSetType(p, ME
be7d0 4d 54 59 50 45 5f 48 45 41 50 29 3b 0a 20 20 73  MTYPE_HEAP);.  s
be7e0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
be7f0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
be800 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65  the size of an e
be810 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61  xisting memory a
be820 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c  llocation.*/.SQL
be830 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
be840 20 2a 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63   *sqlite3Realloc
be850 28 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74  (void *pOld, int
be860 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20   nBytes){.  int 
be870 6e 4f 6c 64 2c 20 6e 4e 65 77 2c 20 6e 44 69 66  nOld, nNew, nDif
be880 66 3b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b  f;.  void *pNew;
be890 0a 20 20 69 66 28 20 70 4f 6c 64 3d 3d 30 20 29  .  if( pOld==0 )
be8a0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
be8b0 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 65  ite3Malloc(nByte
be8c0 73 29 3b 20 2f 2a 20 49 4d 50 3a 20 52 2d 32 38  s); /* IMP: R-28
be8d0 33 35 34 2d 32 35 37 36 39 20 2a 2f 0a 20 20 7d  354-25769 */.  }
be8e0 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30  .  if( nBytes<=0
be8f0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
be900 66 72 65 65 28 70 4f 6c 64 29 3b 20 2f 2a 20 49  free(pOld); /* I
be910 4d 50 3a 20 52 2d 33 31 35 39 33 2d 31 30 35 37  MP: R-31593-1057
be920 34 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  4 */.    return 
be930 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79  0;.  }.  if( nBy
be940 74 65 73 3e 3d 30 78 37 66 66 66 66 66 30 30 20  tes>=0x7fffff00 
be950 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 30 78  ){.    /* The 0x
be960 37 66 66 66 66 30 30 20 6c 69 6d 69 74 20 74 65  7ffff00 limit te
be970 72 6d 20 69 73 20 65 78 70 6c 61 69 6e 65 64 20  rm is explained 
be980 69 6e 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 73  in comments on s
be990 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a  qlite3Malloc() *
be9a0 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  /.    return 0;.
be9b0 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 73 71 6c    }.  nOld = sql
be9c0 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  ite3MallocSize(p
be9d0 4f 6c 64 29 3b 0a 20 20 2f 2a 20 49 4d 50 4c 45  Old);.  /* IMPLE
be9e0 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d  MENTATION-OF: R-
be9f0 34 36 31 39 39 2d 33 30 32 34 39 20 53 51 4c 69  46199-30249 SQLi
bea00 74 65 20 67 75 61 72 61 6e 74 65 65 73 20 74 68  te guarantees th
bea10 61 74 20 74 68 65 20 73 65 63 6f 6e 64 0a 20 20  at the second.  
bea20 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78  ** argument to x
bea30 52 65 61 6c 6c 6f 63 20 69 73 20 61 6c 77 61 79  Realloc is alway
bea40 73 20 61 20 76 61 6c 75 65 20 72 65 74 75 72 6e  s a value return
bea50 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 63 61  ed by a prior ca
bea60 6c 6c 20 74 6f 0a 20 20 2a 2a 20 78 52 6f 75 6e  ll to.  ** xRoun
bea70 64 75 70 2e 20 2a 2f 0a 20 20 6e 4e 65 77 20 3d  dup. */.  nNew =
bea80 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
bea90 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28  nfig.m.xRoundup(
beaa0 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 6e  nBytes);.  if( n
beab0 4f 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20 20  Old==nNew ){.   
beac0 20 70 4e 65 77 20 3d 20 70 4f 6c 64 3b 0a 20 20   pNew = pOld;.  
bead0 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65  }else if( sqlite
beae0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d  3GlobalConfig.bM
beaf0 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71  emstat ){.    sq
beb00 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
beb10 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  r(mem0.mutex);. 
beb20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
beb30 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Set(SQLITE_STATU
beb40 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e  S_MALLOC_SIZE, n
beb50 42 79 74 65 73 29 3b 0a 20 20 20 20 6e 44 69 66  Bytes);.    nDif
beb60 66 20 3d 20 6e 4e 65 77 20 2d 20 6e 4f 6c 64 3b  f = nNew - nOld;
beb70 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
beb80 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49  StatusValue(SQLI
beb90 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59  TE_STATUS_MEMORY
beba0 5f 55 53 45 44 29 20 3e 3d 20 0a 20 20 20 20 20  _USED) >= .     
bebb0 20 20 20 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54       mem0.alarmT
bebc0 68 72 65 73 68 6f 6c 64 2d 6e 44 69 66 66 20 29  hreshold-nDiff )
bebd0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d  {.      sqlite3M
bebe0 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 44 69 66 66  allocAlarm(nDiff
bebf0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  );.    }.    ass
bec00 65 72 74 28 20 73 71 6c 69 74 65 33 4d 65 6d 64  ert( sqlite3Memd
bec10 65 62 75 67 48 61 73 54 79 70 65 28 70 4f 6c 64  ebugHasType(pOld
bec20 2c 20 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 20  , MEMTYPE_HEAP) 
bec30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
bec40 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4e 6f  qlite3MemdebugNo
bec50 54 79 70 65 28 70 4f 6c 64 2c 20 7e 4d 45 4d 54  Type(pOld, ~MEMT
bec60 59 50 45 5f 48 45 41 50 29 20 29 3b 0a 20 20 20  YPE_HEAP) );.   
bec70 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47   pNew = sqlite3G
bec80 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52  lobalConfig.m.xR
bec90 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65  ealloc(pOld, nNe
beca0 77 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77  w);.    if( pNew
becb0 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72  ==0 && mem0.alar
becc0 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20  mCallback ){.   
becd0 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63     sqlite3Malloc
bece0 41 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b 0a 20  Alarm(nBytes);. 
becf0 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69       pNew = sqli
bed00 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
bed10 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c  m.xRealloc(pOld,
bed20 20 6e 4e 65 77 29 3b 0a 20 20 20 20 7d 0a 20 20   nNew);.    }.  
bed30 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20    if( pNew ){.  
bed40 20 20 20 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74      nNew = sqlit
bed50 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4e 65  e3MallocSize(pNe
bed60 77 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  w);.      sqlite
bed70 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
bed80 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
bed90 55 53 45 44 2c 20 6e 4e 65 77 2d 6e 4f 6c 64 29  USED, nNew-nOld)
beda0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
bedb0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
bedc0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d  mem0.mutex);.  }
bedd0 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 20 3d  else{.    pNew =
bede0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
bedf0 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28  nfig.m.xRealloc(
bee00 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 7d  pOld, nNew);.  }
bee10 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54  .  assert( EIGHT
bee20 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28  _BYTE_ALIGNMENT(
bee30 70 4e 65 77 29 20 29 3b 20 2f 2a 20 49 4d 50 3a  pNew) ); /* IMP:
bee40 20 52 2d 30 34 36 37 35 2d 34 34 38 35 30 20 2a   R-04675-44850 *
bee50 2f 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  /.  return pNew;
bee60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 75  .}../*.** The pu
bee70 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74  blic interface t
bee80 6f 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63  o sqlite3Realloc
bee90 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 61  .  Make sure tha
beea0 74 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20  t the memory.** 
beeb0 73 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69  subsystem is ini
beec0 74 69 61 6c 69 7a 65 64 20 70 72 69 6f 72 20 74  tialized prior t
beed0 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74  o invoking sqlit
beee0 65 52 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 51 4c  eRealloc..*/.SQL
beef0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
bef00 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f  lite3_realloc(vo
bef10 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 29  id *pOld, int n)
bef20 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
bef30 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20  _OMIT_AUTOINIT. 
bef40 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69   if( sqlite3_ini
bef50 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75  tialize() ) retu
bef60 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72  rn 0;.#endif.  r
bef70 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65 61  eturn sqlite3Rea
bef80 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b 0a 7d  lloc(pOld, n);.}
bef90 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  .../*.** Allocat
befa0 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72  e and zero memor
befb0 79 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52  y..*/ .SQLITE_PR
befc0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
befd0 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e  te3MallocZero(in
befe0 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20  t n){.  void *p 
beff0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  = sqlite3Malloc(
bf000 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  n);.  if( p ){. 
bf010 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20     memset(p, 0, 
bf020 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  n);.  }.  return
bf030 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c   p;.}../*.** All
bf040 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d  ocate and zero m
bf050 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61  emory.  If the a
bf060 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
bf070 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c   make.** the mal
bf080 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69  locFailed flag i
bf090 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  n the connection
bf0a0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c   pointer..*/.SQL
bf0b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
bf0c0 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f   *sqlite3DbMallo
bf0d0 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 20 2a 64  cZero(sqlite3 *d
bf0e0 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69  b, int n){.  voi
bf0f0 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 44 62  d *p = sqlite3Db
bf100 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29  MallocRaw(db, n)
bf110 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
bf120 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29   memset(p, 0, n)
bf130 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
bf140 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  ;.}../*.** Alloc
bf150 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d  ate and zero mem
bf160 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c 6c  ory.  If the all
bf170 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d  ocation fails, m
bf180 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f  ake.** the mallo
bf190 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20  cFailed flag in 
bf1a0 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  the connection p
bf1b0 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66  ointer..**.** If
bf1c0 20 64 62 21 3d 30 20 61 6e 64 20 64 62 2d 3e 6d   db!=0 and db->m
bf1d0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 74  allocFailed is t
bf1e0 72 75 65 20 28 69 6e 64 69 63 61 74 69 6e 67 20  rue (indicating 
bf1f0 61 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 0a 2a  a prior malloc.*
bf200 2a 20 66 61 69 6c 75 72 65 20 6f 6e 20 74 68 65  * failure on the
bf210 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63   same database c
bf220 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 65 6e 20  onnection) then 
bf230 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 30 2e  always return 0.
bf240 0a 2a 2a 20 48 65 6e 63 65 20 66 6f 72 20 61 20  .** Hence for a 
bf250 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62  particular datab
bf260 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20  ase connection, 
bf270 6f 6e 63 65 20 6d 61 6c 6c 6f 63 20 73 74 61 72  once malloc star
bf280 74 73 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c 20 69  ts.** failing, i
bf290 74 20 66 61 69 6c 73 20 63 6f 6e 73 69 73 74 65  t fails consiste
bf2a0 6e 74 6c 79 20 75 6e 74 69 6c 20 6d 61 6c 6c 6f  ntly until mallo
bf2b0 63 46 61 69 6c 65 64 20 69 73 20 72 65 73 65 74  cFailed is reset
bf2c0 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20  ..** This is an 
bf2d0 69 6d 70 6f 72 74 61 6e 74 20 61 73 73 75 6d 70  important assump
bf2e0 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 61 72 65  tion.  There are
bf2f0 20 6d 61 6e 79 20 70 6c 61 63 65 73 20 69 6e 20   many places in 
bf300 74 68 65 0a 2a 2a 20 63 6f 64 65 20 74 68 61 74  the.** code that
bf310 20 64 6f 20 74 68 69 6e 67 73 20 6c 69 6b 65 20   do things like 
bf320 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
bf330 20 20 20 20 69 6e 74 20 2a 61 20 3d 20 28 69 6e      int *a = (in
bf340 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  t*)sqlite3DbMall
bf350 6f 63 52 61 77 28 64 62 2c 20 31 30 30 29 3b 0a  ocRaw(db, 100);.
bf360 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 20 2a  **         int *
bf370 62 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74 65  b = (int*)sqlite
bf380 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
bf390 20 32 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20   200);.**       
bf3a0 20 20 69 66 28 20 62 20 29 20 61 5b 31 30 5d 20    if( b ) a[10] 
bf3b0 3d 20 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74  = 9;.**.** In ot
bf3c0 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 61 20  her words, if a 
bf3d0 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f  subsequent mallo
bf3e0 63 20 28 65 78 3a 20 22 62 22 29 20 77 6f 72 6b  c (ex: "b") work
bf3f0 65 64 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65  ed, it is assume
bf400 64 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 70 72  d.** that all pr
bf410 69 6f 72 20 6d 61 6c 6c 6f 63 73 20 28 65 78 3a  ior mallocs (ex:
bf420 20 22 61 22 29 20 77 6f 72 6b 65 64 20 74 6f 6f   "a") worked too
bf430 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
bf440 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
bf450 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c  3DbMallocRaw(sql
bf460 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29  ite3 *db, int n)
bf470 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61  {.  void *p;.  a
bf480 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20  ssert( db==0 || 
bf490 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
bf4a0 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
bf4b0 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30  .  assert( db==0
bf4c0 20 7c 7c 20 64 62 2d 3e 70 6e 42 79 74 65 73 46   || db->pnBytesF
bf4d0 72 65 65 64 3d 3d 30 20 29 3b 0a 23 69 66 6e 64  reed==0 );.#ifnd
bf4e0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
bf4f0 4f 4f 4b 41 53 49 44 45 0a 20 20 69 66 28 20 64  OOKASIDE.  if( d
bf500 62 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69  b ){.    Lookasi
bf510 64 65 53 6c 6f 74 20 2a 70 42 75 66 3b 0a 20 20  deSlot *pBuf;.  
bf520 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
bf530 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
bf540 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
bf550 20 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b      if( db->look
bf560 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 29  aside.bEnabled )
bf570 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 64 62  {.      if( n>db
bf580 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 29  ->lookaside.sz )
bf590 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f  {.        db->lo
bf5a0 6f 6b 61 73 69 64 65 2e 61 6e 53 74 61 74 5b 31  okaside.anStat[1
bf5b0 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ]++;.      }else
bf5c0 20 69 66 28 20 28 70 42 75 66 20 3d 20 64 62 2d   if( (pBuf = db-
bf5d0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65  >lookaside.pFree
bf5e0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
bf5f0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 61 6e  db->lookaside.an
bf600 53 74 61 74 5b 32 5d 2b 2b 3b 0a 20 20 20 20 20  Stat[2]++;.     
bf610 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
bf620 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46  db->lookaside.pF
bf630 72 65 65 20 3d 20 70 42 75 66 2d 3e 70 4e 65 78  ree = pBuf->pNex
bf640 74 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c  t;.        db->l
bf650 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2b 2b 3b  ookaside.nOut++;
bf660 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f  .        db->loo
bf670 6b 61 73 69 64 65 2e 61 6e 53 74 61 74 5b 30 5d  kaside.anStat[0]
bf680 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ++;.        if( 
bf690 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f  db->lookaside.nO
bf6a0 75 74 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  ut>db->lookaside
bf6b0 2e 6d 78 4f 75 74 20 29 7b 0a 20 20 20 20 20 20  .mxOut ){.      
bf6c0 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
bf6d0 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f  e.mxOut = db->lo
bf6e0 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20  okaside.nOut;.  
bf6f0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
bf700 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 42  return (void*)pB
bf710 75 66 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  uf;.      }.    
bf720 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66  }.  }.#else.  if
bf730 28 20 64 62 20 26 26 20 64 62 2d 3e 6d 61 6c 6c  ( db && db->mall
bf740 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
bf750 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65  return 0;.  }.#e
bf760 6e 64 69 66 0a 20 20 70 20 3d 20 73 71 6c 69 74  ndif.  p = sqlit
bf770 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69  e3Malloc(n);.  i
bf780 66 28 20 21 70 20 26 26 20 64 62 20 29 7b 0a 20  f( !p && db ){. 
bf790 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
bf7a0 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73  led = 1;.  }.  s
bf7b0 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65  qlite3MemdebugSe
bf7c0 74 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45  tType(p, MEMTYPE
bf7d0 5f 44 42 20 7c 0a 20 20 20 20 20 20 20 20 20 28  _DB |.         (
bf7e0 28 64 62 20 26 26 20 64 62 2d 3e 6c 6f 6f 6b 61  (db && db->looka
bf7f0 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 29 20 3f  side.bEnabled) ?
bf800 20 4d 45 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49   MEMTYPE_LOOKASI
bf810 44 45 20 3a 20 4d 45 4d 54 59 50 45 5f 48 45 41  DE : MEMTYPE_HEA
bf820 50 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b  P));.  return p;
bf830 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65  .}../*.** Resize
bf840 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65   the block of me
bf850 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f 20  mory pointed to 
bf860 62 79 20 70 20 74 6f 20 6e 20 62 79 74 65 73 2e  by p to n bytes.
bf870 20 49 66 20 74 68 65 0a 2a 2a 20 72 65 73 69 7a   If the.** resiz
bf880 65 20 66 61 69 6c 73 2c 20 73 65 74 20 74 68 65  e fails, set the
bf890 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c   mallocFailed fl
bf8a0 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63  ag in the connec
bf8b0 74 69 6f 6e 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  tion object..*/.
bf8c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
bf8d0 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 65  oid *sqlite3DbRe
bf8e0 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20 2a 64  alloc(sqlite3 *d
bf8f0 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20  b, void *p, int 
bf900 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77  n){.  void *pNew
bf910 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
bf920 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  db!=0 );.  asser
bf930 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
bf940 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
bf950 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   );.  if( db->ma
bf960 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 7b  llocFailed==0 ){
bf970 0a 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 7b  .    if( p==0 ){
bf980 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 71  .      return sq
bf990 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
bf9a0 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20  (db, n);.    }. 
bf9b0 20 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69     if( isLookasi
bf9c0 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20  de(db, p) ){.   
bf9d0 20 20 20 69 66 28 20 6e 3c 3d 64 62 2d 3e 6c 6f     if( n<=db->lo
bf9e0 6f 6b 61 73 69 64 65 2e 73 7a 20 29 7b 0a 20 20  okaside.sz ){.  
bf9f0 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a        return p;.
bfa00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e        }.      pN
bfa10 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
bfa20 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a  llocRaw(db, n);.
bfa30 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29        if( pNew )
bfa40 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  {.        memcpy
bfa50 28 70 4e 65 77 2c 20 70 2c 20 64 62 2d 3e 6c 6f  (pNew, p, db->lo
bfa60 6f 6b 61 73 69 64 65 2e 73 7a 29 3b 0a 20 20 20  okaside.sz);.   
bfa70 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
bfa80 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20  ee(db, p);.     
bfa90 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
bfaa0 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
bfab0 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54 79  te3MemdebugHasTy
bfac0 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 44 42  pe(p, MEMTYPE_DB
bfad0 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ) );.      asser
bfae0 74 28 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62  t( sqlite3Memdeb
bfaf0 75 67 48 61 73 54 79 70 65 28 70 2c 20 4d 45 4d  ugHasType(p, MEM
bfb00 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45 7c 4d  TYPE_LOOKASIDE|M
bfb10 45 4d 54 59 50 45 5f 48 45 41 50 29 20 29 3b 0a  EMTYPE_HEAP) );.
bfb20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d        sqlite3Mem
bfb30 64 65 62 75 67 53 65 74 54 79 70 65 28 70 2c 20  debugSetType(p, 
bfb40 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 3b 0a 20  MEMTYPE_HEAP);. 
bfb50 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69       pNew = sqli
bfb60 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 2c 20 6e  te3_realloc(p, n
bfb70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e  );.      if( !pN
bfb80 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ew ){.        sq
bfb90 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74  lite3MemdebugSet
bfba0 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f  Type(p, MEMTYPE_
bfbb0 44 42 7c 4d 45 4d 54 59 50 45 5f 48 45 41 50 29  DB|MEMTYPE_HEAP)
bfbc0 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61  ;.        db->ma
bfbd0 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
bfbe0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
bfbf0 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74  lite3MemdebugSet
bfc00 54 79 70 65 28 70 4e 65 77 2c 20 4d 45 4d 54 59  Type(pNew, MEMTY
bfc10 50 45 5f 44 42 20 7c 20 0a 20 20 20 20 20 20 20  PE_DB | .       
bfc20 20 20 20 20 20 28 64 62 2d 3e 6c 6f 6f 6b 61 73       (db->lookas
bfc30 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3f 20 4d  ide.bEnabled ? M
bfc40 45 4d 54 59 50 45 5f 4c 4f 4f 4b 41 53 49 44 45  EMTYPE_LOOKASIDE
bfc50 20 3a 20 4d 45 4d 54 59 50 45 5f 48 45 41 50 29   : MEMTYPE_HEAP)
bfc60 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
bfc70 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  eturn pNew;.}../
bfc80 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  *.** Attempt to 
bfc90 72 65 61 6c 6c 6f 63 61 74 65 20 70 2e 20 20 49  reallocate p.  I
bfca0 66 20 74 68 65 20 72 65 61 6c 6c 6f 63 61 74 69  f the reallocati
bfcb0 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 66  on fails, then f
bfcc0 72 65 65 20 70 0a 2a 2a 20 61 6e 64 20 73 65 74  ree p.** and set
bfcd0 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65   the mallocFaile
bfce0 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 64 61  d flag in the da
bfcf0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
bfd00 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
bfd10 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
bfd20 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65  e3DbReallocOrFre
bfd30 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76  e(sqlite3 *db, v
bfd40 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a  oid *p, int n){.
bfd50 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20    void *pNew;.  
bfd60 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
bfd70 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 2c 20 6e  Realloc(db, p, n
bfd80 29 3b 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29  );.  if( !pNew )
bfd90 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
bfda0 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a  ree(db, p);.  }.
bfdb0 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
bfdc0 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63  ../*.** Make a c
bfdd0 6f 70 79 20 6f 66 20 61 20 73 74 72 69 6e 67 20  opy of a string 
bfde0 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  in memory obtain
bfdf0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61  ed from sqliteMa
bfe00 6c 6c 6f 63 28 29 2e 20 54 68 65 73 65 20 0a 2a  lloc(). These .*
bfe10 2a 20 66 75 6e 63 74 69 6f 6e 73 20 63 61 6c 6c  * functions call
bfe20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61   sqlite3MallocRa
bfe30 77 28 29 20 64 69 72 65 63 74 6c 79 20 69 6e 73  w() directly ins
bfe40 74 65 61 64 20 6f 66 20 73 71 6c 69 74 65 4d 61  tead of sqliteMa
bfe50 6c 6c 6f 63 28 29 2e 20 54 68 69 73 0a 2a 2a 20  lloc(). This.** 
bfe60 69 73 20 62 65 63 61 75 73 65 20 77 68 65 6e 20  is because when 
bfe70 6d 65 6d 6f 72 79 20 64 65 62 75 67 67 69 6e 67  memory debugging
bfe80 20 69 73 20 74 75 72 6e 65 64 20 6f 6e 2c 20 74   is turned on, t
bfe90 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f  hese two functio
bfea0 6e 73 20 61 72 65 20 0a 2a 2a 20 63 61 6c 6c 65  ns are .** calle
bfeb0 64 20 76 69 61 20 6d 61 63 72 6f 73 20 74 68 61  d via macros tha
bfec0 74 20 72 65 63 6f 72 64 20 74 68 65 20 63 75 72  t record the cur
bfed0 72 65 6e 74 20 66 69 6c 65 20 61 6e 64 20 6c 69  rent file and li
bfee0 6e 65 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65  ne number in the
bfef0 0a 2a 2a 20 54 68 72 65 61 64 44 61 74 61 20 73  .** ThreadData s
bff00 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c  tructure..*/.SQL
bff10 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
bff20 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 44 75   *sqlite3DbStrDu
bff30 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  p(sqlite3 *db, c
bff40 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
bff50 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 73   char *zNew;.  s
bff60 69 7a 65 5f 74 20 6e 3b 0a 20 20 69 66 28 20 7a  ize_t n;.  if( z
bff70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
bff80 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73  n 0;.  }.  n = s
bff90 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
bffa0 29 20 2b 20 31 3b 0a 20 20 61 73 73 65 72 74 28  ) + 1;.  assert(
bffb0 20 28 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d   (n&0x7fffffff)=
bffc0 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73  =n );.  zNew = s
bffd0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
bffe0 77 28 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20  w(db, (int)n);. 
bfff0 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20   if( zNew ){.   
c0000 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c   memcpy(zNew, z,
c0010 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   n);.  }.  retur
c0020 6e 20 7a 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45  n zNew;.}.SQLITE
c0030 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
c0040 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
c0050 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
c0060 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
c0070 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77  n){.  char *zNew
c0080 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a  ;.  if( z==0 ){.
c0090 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
c00a0 7d 0a 20 20 61 73 73 65 72 74 28 20 28 6e 26 30  }.  assert( (n&0
c00b0 78 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b  x7fffffff)==n );
c00c0 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  zNew = sqlite
c00d0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
c00e0 20 6e 2b 31 29 3b 0a 20 20 69 66 28 20 7a 4e 65   n+1);.  if( zNe
c00f0 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  w ){.    memcpy(
c0100 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20  zNew, z, n);.   
c0110 20 7a 4e 65 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20   zNew[n] = 0;.  
c0120 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b  }.  return zNew;
c0130 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
c0140 20 61 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 74   a string from t
c0150 68 65 20 7a 46 72 6f 6d 61 74 20 61 72 67 75 6d  he zFromat argum
c0160 65 6e 74 20 61 6e 64 20 74 68 65 20 76 61 5f 6c  ent and the va_l
c0170 69 73 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  ist that follows
c0180 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 73  ..** Store the s
c0190 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20  tring in memory 
c01a0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
c01b0 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64  liteMalloc() and
c01c0 20 6d 61 6b 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69   make *pz.** poi
c01d0 6e 74 20 74 6f 20 74 68 61 74 20 73 74 72 69 6e  nt to that strin
c01e0 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
c01f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
c0200 33 53 65 74 53 74 72 69 6e 67 28 63 68 61 72 20  3SetString(char 
c0210 2a 2a 70 7a 2c 20 73 71 6c 69 74 65 33 20 2a 64  **pz, sqlite3 *d
c0220 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
c0230 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20  Format, ...){.  
c0240 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68  va_list ap;.  ch
c0250 61 72 20 2a 7a 3b 0a 0a 20 20 76 61 5f 73 74 61  ar *z;..  va_sta
c0260 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b  rt(ap, zFormat);
c0270 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d  .  z = sqlite3VM
c0280 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d  Printf(db, zForm
c0290 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e  at, ap);.  va_en
c02a0 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  d(ap);.  sqlite3
c02b0 44 62 46 72 65 65 28 64 62 2c 20 2a 70 7a 29 3b  DbFree(db, *pz);
c02c0 0a 20 20 2a 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a  .  *pz = z;.}...
c02d0 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
c02e0 69 6f 6e 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  ion must be call
c02f0 65 64 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e  ed before exitin
c0300 67 20 61 6e 79 20 41 50 49 20 66 75 6e 63 74 69  g any API functi
c0310 6f 6e 20 28 69 2e 65 2e 20 0a 2a 2a 20 72 65 74  on (i.e. .** ret
c0320 75 72 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74  urning control t
c0330 6f 20 74 68 65 20 75 73 65 72 29 20 74 68 61 74  o the user) that
c0340 20 68 61 73 20 63 61 6c 6c 65 64 20 73 71 6c 69   has called sqli
c0350 74 65 33 5f 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a  te3_malloc or.**
c0360 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
c0370 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
c0380 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6e 6f  rned value is no
c0390 72 6d 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66  rmally a copy of
c03a0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
c03b0 6d 65 6e 74 20 74 6f 20 74 68 69 73 0a 2a 2a 20  ment to this.** 
c03c0 66 75 6e 63 74 69 6f 6e 2e 20 48 6f 77 65 76 65  function. Howeve
c03d0 72 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29  r, if a malloc()
c03e0 20 66 61 69 6c 75 72 65 20 68 61 73 20 6f 63 63   failure has occ
c03f0 75 72 72 65 64 20 73 69 6e 63 65 20 74 68 65 20  urred since the 
c0400 70 72 65 76 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f  previous.** invo
c0410 63 61 74 69 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f  cation SQLITE_NO
c0420 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20  MEM is returned 
c0430 69 6e 73 74 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20  instead. .**.** 
c0440 49 66 20 74 68 65 20 66 69 72 73 74 20 61 72 67  If the first arg
c0450 75 6d 65 6e 74 2c 20 64 62 2c 20 69 73 20 6e 6f  ument, db, is no
c0460 74 20 4e 55 4c 4c 20 61 6e 64 20 61 20 6d 61 6c  t NULL and a mal
c0470 6c 6f 63 28 29 20 65 72 72 6f 72 20 68 61 73 20  loc() error has 
c0480 6f 63 63 75 72 72 65 64 2c 0a 2a 2a 20 74 68 65  occurred,.** the
c0490 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  n the connection
c04a0 20 65 72 72 6f 72 2d 63 6f 64 65 20 28 74 68 65   error-code (the
c04b0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
c04c0 62 79 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  by sqlite3_errco
c04d0 64 65 28 29 29 0a 2a 2a 20 69 73 20 73 65 74 20  de()).** is set 
c04e0 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e  to SQLITE_NOMEM.
c04f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
c0500 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70  TE int sqlite3Ap
c0510 69 45 78 69 74 28 73 71 6c 69 74 65 33 2a 20 64  iExit(sqlite3* d
c0520 62 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 2f 2a  b, int rc){.  /*
c0530 20 49 66 20 74 68 65 20 64 62 20 68 61 6e 64 6c   If the db handl
c0540 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  e is not NULL, t
c0550 68 65 6e 20 77 65 20 6d 75 73 74 20 68 6f 6c 64  hen we must hold
c0560 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   the connection 
c0570 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 6d 75 74 65  handle.  ** mute
c0580 78 20 68 65 72 65 2e 20 4f 74 68 65 72 77 69 73  x here. Otherwis
c0590 65 20 74 68 65 20 72 65 61 64 20 28 61 6e 64 20  e the read (and 
c05a0 70 6f 73 73 69 62 6c 65 20 77 72 69 74 65 29 20  possible write) 
c05b0 6f 66 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  of db->mallocFai
c05c0 6c 65 64 20 0a 20 20 2a 2a 20 69 73 20 75 6e 73  led .  ** is uns
c05d0 61 66 65 2c 20 61 73 20 69 73 20 74 68 65 20 63  afe, as is the c
c05e0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 72  all to sqlite3Er
c05f0 72 6f 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73  ror()..  */.  as
c0600 73 65 72 74 28 20 21 64 62 20 7c 7c 20 73 71 6c  sert( !db || sql
c0610 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
c0620 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
c0630 69 66 28 20 64 62 20 26 26 20 28 64 62 2d 3e 6d  if( db && (db->m
c0640 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 72  allocFailed || r
c0650 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  c==SQLITE_IOERR_
c0660 4e 4f 4d 45 4d 29 20 29 7b 0a 20 20 20 20 73 71  NOMEM) ){.    sq
c0670 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
c0680 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b  QLITE_NOMEM, 0);
c0690 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  .    db->mallocF
c06a0 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 72  ailed = 0;.    r
c06b0 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
c06c0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
c06d0 63 20 26 20 28 64 62 20 3f 20 64 62 2d 3e 65 72  c & (db ? db->er
c06e0 72 4d 61 73 6b 20 3a 20 30 78 66 66 29 3b 0a 7d  rMask : 0xff);.}
c06f0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
c0700 2a 20 45 6e 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e  * End of malloc.
c0710 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
c0720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0740 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
c0750 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 72 69  * Begin file pri
c0760 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ntf.c **********
c0770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0790 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 70 72 69  /./*.** The "pri
c07a0 6e 74 66 22 20 63 6f 64 65 20 74 68 61 74 20 66  ntf" code that f
c07b0 6f 6c 6c 6f 77 73 20 64 61 74 65 73 20 66 72 6f  ollows dates fro
c07c0 6d 20 74 68 65 20 31 39 38 30 27 73 2e 20 20 49  m the 1980's.  I
c07d0 74 20 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20 70  t is in.** the p
c07e0 75 62 6c 69 63 20 64 6f 6d 61 69 6e 2e 20 20 54  ublic domain.  T
c07f0 68 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6d 6d  he original comm
c0800 65 6e 74 73 20 61 72 65 20 69 6e 63 6c 75 64 65  ents are include
c0810 64 20 68 65 72 65 20 66 6f 72 0a 2a 2a 20 63 6f  d here for.** co
c0820 6d 70 6c 65 74 65 6e 65 73 73 2e 20 20 54 68 65  mpleteness.  The
c0830 79 20 61 72 65 20 76 65 72 79 20 6f 75 74 2d 6f  y are very out-o
c0840 66 2d 64 61 74 65 20 62 75 74 20 6d 69 67 68 74  f-date but might
c0850 20 62 65 20 75 73 65 66 75 6c 20 61 73 0a 2a 2a   be useful as.**
c0860 20 61 6e 20 68 69 73 74 6f 72 69 63 61 6c 20 72   an historical r
c0870 65 66 65 72 65 6e 63 65 2e 20 20 4d 6f 73 74 20  eference.  Most 
c0880 6f 66 20 74 68 65 20 22 65 6e 68 61 6e 63 65 6d  of the "enhancem
c0890 65 6e 74 73 22 20 68 61 76 65 20 62 65 65 6e 20  ents" have been 
c08a0 62 61 63 6b 65 64 0a 2a 2a 20 6f 75 74 20 73 6f  backed.** out so
c08b0 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69   that the functi
c08c0 6f 6e 61 6c 69 74 79 20 69 73 20 6e 6f 77 20 74  onality is now t
c08d0 68 65 20 73 61 6d 65 20 61 73 20 73 74 61 6e 64  he same as stand
c08e0 61 72 64 20 70 72 69 6e 74 66 28 29 2e 0a 2a 2a  ard printf()..**
c08f0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
c0900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
c0940 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
c0950 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 61 20  ains code for a 
c0960 73 65 74 20 6f 66 20 22 70 72 69 6e 74 66 22 2d  set of "printf"-
c0970 6c 69 6b 65 20 72 6f 75 74 69 6e 65 73 2e 20 20  like routines.  
c0980 54 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65  These.** routine
c0990 73 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 73  s format strings
c09a0 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65 20 70   much like the p
c09b0 72 69 6e 74 66 28 29 20 66 72 6f 6d 20 74 68 65  rintf() from the
c09c0 20 73 74 61 6e 64 61 72 64 20 43 0a 2a 2a 20 6c   standard C.** l
c09d0 69 62 72 61 72 79 2c 20 74 68 6f 75 67 68 20 74  ibrary, though t
c09e0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
c09f0 6e 20 68 65 72 65 20 68 61 73 20 65 6e 68 61 6e  n here has enhan
c0a00 63 65 6d 65 6e 74 73 20 74 6f 20 73 75 70 70 6f  cements to suppo
c0a10 72 74 0a 2a 2a 20 53 51 4c 6c 69 74 65 2e 0a 2a  rt.** SQLlite..*
c0a20 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73  /../*.** Convers
c0a30 69 6f 6e 20 74 79 70 65 73 20 66 61 6c 6c 20 69  ion types fall i
c0a40 6e 74 6f 20 76 61 72 69 6f 75 73 20 63 61 74 65  nto various cate
c0a50 67 6f 72 69 65 73 20 61 73 20 64 65 66 69 6e 65  gories as define
c0a60 64 20 62 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c  d by the.** foll
c0a70 6f 77 69 6e 67 20 65 6e 75 6d 65 72 61 74 69 6f  owing enumeratio
c0a80 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  n..*/.#define et
c0a90 52 41 44 49 58 20 20 20 20 20 20 20 31 20 2f 2a  RADIX       1 /*
c0aa0 20 49 6e 74 65 67 65 72 20 74 79 70 65 73 2e 20   Integer types. 
c0ab0 20 25 64 2c 20 25 78 2c 20 25 6f 2c 20 61 6e 64   %d, %x, %o, and
c0ac0 20 73 6f 20 66 6f 72 74 68 20 2a 2f 0a 23 64 65   so forth */.#de
c0ad0 66 69 6e 65 20 65 74 46 4c 4f 41 54 20 20 20 20  fine etFLOAT    
c0ae0 20 20 20 32 20 2f 2a 20 46 6c 6f 61 74 69 6e 67     2 /* Floating
c0af0 20 70 6f 69 6e 74 2e 20 20 25 66 20 2a 2f 0a 23   point.  %f */.#
c0b00 64 65 66 69 6e 65 20 65 74 45 58 50 20 20 20 20  define etEXP    
c0b10 20 20 20 20 20 33 20 2f 2a 20 45 78 70 6f 6e 65       3 /* Expone
c0b20 6e 74 69 6f 6e 61 6c 20 6e 6f 74 61 74 69 6f 6e  ntional notation
c0b30 2e 20 25 65 20 61 6e 64 20 25 45 20 2a 2f 0a 23  . %e and %E */.#
c0b40 64 65 66 69 6e 65 20 65 74 47 45 4e 45 52 49 43  define etGENERIC
c0b50 20 20 20 20 20 34 20 2f 2a 20 46 6c 6f 61 74 69       4 /* Floati
c0b60 6e 67 20 6f 72 20 65 78 70 6f 6e 65 6e 74 69 61  ng or exponentia
c0b70 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  l, depending on 
c0b80 65 78 70 6f 6e 65 6e 74 2e 20 25 67 20 2a 2f 0a  exponent. %g */.
c0b90 23 64 65 66 69 6e 65 20 65 74 53 49 5a 45 20 20  #define etSIZE  
c0ba0 20 20 20 20 20 20 35 20 2f 2a 20 52 65 74 75 72        5 /* Retur
c0bb0 6e 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  n number of char
c0bc0 61 63 74 65 72 73 20 70 72 6f 63 65 73 73 65 64  acters processed
c0bd0 20 73 6f 20 66 61 72 2e 20 25 6e 20 2a 2f 0a 23   so far. %n */.#
c0be0 64 65 66 69 6e 65 20 65 74 53 54 52 49 4e 47 20  define etSTRING 
c0bf0 20 20 20 20 20 36 20 2f 2a 20 53 74 72 69 6e 67       6 /* String
c0c00 73 2e 20 25 73 20 2a 2f 0a 23 64 65 66 69 6e 65  s. %s */.#define
c0c10 20 65 74 44 59 4e 53 54 52 49 4e 47 20 20 20 37   etDYNSTRING   7
c0c20 20 2f 2a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20   /* Dynamically 
c0c30 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69 6e 67  allocated string
c0c40 73 2e 20 25 7a 20 2a 2f 0a 23 64 65 66 69 6e 65  s. %z */.#define
c0c50 20 65 74 50 45 52 43 45 4e 54 20 20 20 20 20 38   etPERCENT     8
c0c60 20 2f 2a 20 50 65 72 63 65 6e 74 20 73 79 6d 62   /* Percent symb
c0c70 6f 6c 2e 20 25 25 20 2a 2f 0a 23 64 65 66 69 6e  ol. %% */.#defin
c0c80 65 20 65 74 43 48 41 52 58 20 20 20 20 20 20 20  e etCHARX       
c0c90 39 20 2f 2a 20 43 68 61 72 61 63 74 65 72 73 2e  9 /* Characters.
c0ca0 20 25 63 20 2a 2f 0a 2f 2a 20 54 68 65 20 72 65   %c */./* The re
c0cb0 73 74 20 61 72 65 20 65 78 74 65 6e 73 69 6f 6e  st are extension
c0cc0 73 2c 20 6e 6f 74 20 6e 6f 72 6d 61 6c 6c 79 20  s, not normally 
c0cd0 66 6f 75 6e 64 20 69 6e 20 70 72 69 6e 74 66 28  found in printf(
c0ce0 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53  ) */.#define etS
c0cf0 51 4c 45 53 43 41 50 45 20 20 31 30 20 2f 2a 20  QLESCAPE  10 /* 
c0d00 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c 27  Strings with '\'
c0d10 27 20 64 6f 75 62 6c 65 64 2e 20 20 25 71 20 2a  ' doubled.  %q *
c0d20 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45  /.#define etSQLE
c0d30 53 43 41 50 45 32 20 31 31 20 2f 2a 20 53 74 72  SCAPE2 11 /* Str
c0d40 69 6e 67 73 20 77 69 74 68 20 27 5c 27 27 20 64  ings with '\'' d
c0d50 6f 75 62 6c 65 64 20 61 6e 64 20 65 6e 63 6c 6f  oubled and enclo
c0d60 73 65 64 20 69 6e 20 27 27 2c 0a 20 20 20 20 20  sed in '',.     
c0d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c0d80 20 20 20 20 20 4e 55 4c 4c 20 70 6f 69 6e 74 65       NULL pointe
c0d90 72 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 53  rs replaced by S
c0da0 51 4c 20 4e 55 4c 4c 2e 20 20 25 51 20 2a 2f 0a  QL NULL.  %Q */.
c0db0 23 64 65 66 69 6e 65 20 65 74 54 4f 4b 45 4e 20  #define etTOKEN 
c0dc0 20 20 20 20 20 31 32 20 2f 2a 20 61 20 70 6f 69       12 /* a poi
c0dd0 6e 74 65 72 20 74 6f 20 61 20 54 6f 6b 65 6e 20  nter to a Token 
c0de0 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65  structure */.#de
c0df0 66 69 6e 65 20 65 74 53 52 43 4c 49 53 54 20 20  fine etSRCLIST  
c0e00 20 20 31 33 20 2f 2a 20 61 20 70 6f 69 6e 74 65    13 /* a pointe
c0e10 72 20 74 6f 20 61 20 53 72 63 4c 69 73 74 20 2a  r to a SrcList *
c0e20 2f 0a 23 64 65 66 69 6e 65 20 65 74 50 4f 49 4e  /.#define etPOIN
c0e30 54 45 52 20 20 20 20 31 34 20 2f 2a 20 54 68 65  TER    14 /* The
c0e40 20 25 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a   %p conversion *
c0e50 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45  /.#define etSQLE
c0e60 53 43 41 50 45 33 20 31 35 20 2f 2a 20 25 77 20  SCAPE3 15 /* %w 
c0e70 2d 3e 20 53 74 72 69 6e 67 73 20 77 69 74 68 20  -> Strings with 
c0e80 27 5c 22 27 20 64 6f 75 62 6c 65 64 20 2a 2f 0a  '\"' doubled */.
c0e90 23 64 65 66 69 6e 65 20 65 74 4f 52 44 49 4e 41  #define etORDINA
c0ea0 4c 20 20 20 20 31 36 20 2f 2a 20 25 72 20 2d 3e  L    16 /* %r ->
c0eb0 20 31 73 74 2c 20 32 6e 64 2c 20 33 72 64 2c 20   1st, 2nd, 3rd, 
c0ec0 34 74 68 2c 20 65 74 63 2e 20 20 45 6e 67 6c 69  4th, etc.  Engli
c0ed0 73 68 20 6f 6e 6c 79 20 2a 2f 0a 0a 23 64 65 66  sh only */..#def
c0ee0 69 6e 65 20 65 74 49 4e 56 41 4c 49 44 20 20 20  ine etINVALID   
c0ef0 20 20 30 20 2f 2a 20 41 6e 79 20 75 6e 72 65 63    0 /* Any unrec
c0f00 6f 67 6e 69 7a 65 64 20 63 6f 6e 76 65 72 73 69  ognized conversi
c0f10 6f 6e 20 74 79 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a  on type */.../*.
c0f20 2a 2a 20 41 6e 20 22 65 74 42 79 74 65 22 20 69  ** An "etByte" i
c0f30 73 20 61 6e 20 38 2d 62 69 74 20 75 6e 73 69 67  s an 8-bit unsig
c0f40 6e 65 64 20 76 61 6c 75 65 2e 0a 2a 2f 0a 74 79  ned value..*/.ty
c0f50 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 63  pedef unsigned c
c0f60 68 61 72 20 65 74 42 79 74 65 3b 0a 0a 2f 2a 0a  har etByte;../*.
c0f70 2a 2a 20 45 61 63 68 20 62 75 69 6c 74 69 6e 20  ** Each builtin 
c0f80 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68 61 72 61  conversion chara
c0f90 63 74 65 72 20 28 65 78 3a 20 74 68 65 20 27 64  cter (ex: the 'd
c0fa0 27 20 69 6e 20 22 25 64 22 29 20 69 73 20 64 65  ' in "%d") is de
c0fb0 73 63 72 69 62 65 64 0a 2a 2a 20 62 79 20 61 6e  scribed.** by an
c0fc0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
c0fd0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
c0fe0 74 75 72 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ture.*/.typedef 
c0ff0 73 74 72 75 63 74 20 65 74 5f 69 6e 66 6f 20 7b  struct et_info {
c1000 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
c1010 6e 20 61 62 6f 75 74 20 65 61 63 68 20 66 6f 72  n about each for
c1020 6d 61 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20 63  mat field */.  c
c1030 68 61 72 20 66 6d 74 74 79 70 65 3b 20 20 20 20  har fmttype;    
c1040 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
c1050 6f 72 6d 61 74 20 66 69 65 6c 64 20 63 6f 64 65  ormat field code
c1060 20 6c 65 74 74 65 72 20 2a 2f 0a 20 20 65 74 42   letter */.  etB
c1070 79 74 65 20 62 61 73 65 3b 20 20 20 20 20 20 20  yte base;       
c1080 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 61 73        /* The bas
c1090 65 20 66 6f 72 20 72 61 64 69 78 20 63 6f 6e 76  e for radix conv
c10a0 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 65 74 42 79  ersion */.  etBy
c10b0 74 65 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20  te flags;       
c10c0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d       /* One or m
c10d0 6f 72 65 20 6f 66 20 46 4c 41 47 5f 20 63 6f 6e  ore of FLAG_ con
c10e0 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 2a 2f 0a  stants below */.
c10f0 20 20 65 74 42 79 74 65 20 74 79 70 65 3b 20 20    etByte type;  
c1100 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
c1110 6e 76 65 72 73 69 6f 6e 20 70 61 72 61 64 69 67  nversion paradig
c1120 6d 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 63 68  m */.  etByte ch
c1130 61 72 73 65 74 3b 20 20 20 20 20 20 20 20 20 20  arset;          
c1140 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61  /* Offset into a
c1150 44 69 67 69 74 73 5b 5d 20 6f 66 20 74 68 65 20  Digits[] of the 
c1160 64 69 67 69 74 73 20 73 74 72 69 6e 67 20 2a 2f  digits string */
c1170 0a 20 20 65 74 42 79 74 65 20 70 72 65 66 69 78  .  etByte prefix
c1180 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  ;           /* O
c1190 66 66 73 65 74 20 69 6e 74 6f 20 61 50 72 65 66  ffset into aPref
c11a0 69 78 5b 5d 20 6f 66 20 74 68 65 20 70 72 65 66  ix[] of the pref
c11b0 69 78 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 65  ix string */.} e
c11c0 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 41  t_info;../*.** A
c11d0 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f  llowed values fo
c11e0 72 20 65 74 5f 69 6e 66 6f 2e 66 6c 61 67 73 0a  r et_info.flags.
c11f0 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f  */.#define FLAG_
c1200 53 49 47 4e 45 44 20 20 31 20 20 20 20 20 2f 2a  SIGNED  1     /*
c1210 20 54 72 75 65 20 69 66 20 74 68 65 20 76 61 6c   True if the val
c1220 75 65 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 73  ue to convert is
c1230 20 73 69 67 6e 65 64 20 2a 2f 0a 23 64 65 66 69   signed */.#defi
c1240 6e 65 20 46 4c 41 47 5f 49 4e 54 45 52 4e 20 20  ne FLAG_INTERN  
c1250 32 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  2     /* True if
c1260 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
c1270 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  e only */.#defin
c1280 65 20 46 4c 41 47 5f 53 54 52 49 4e 47 20 20 34  e FLAG_STRING  4
c1290 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 20 69 6e       /* Allow in
c12a0 66 69 6e 69 74 79 20 70 72 65 63 69 73 69 6f 6e  finity precision
c12b0 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   */.../*.** The 
c12c0 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20  following table 
c12d0 69 73 20 73 65 61 72 63 68 65 64 20 6c 69 6e 65  is searched line
c12e0 61 72 6c 79 2c 20 73 6f 20 69 74 20 69 73 20 67  arly, so it is g
c12f0 6f 6f 64 20 74 6f 20 70 75 74 20 74 68 65 0a 2a  ood to put the.*
c1300 2a 20 6d 6f 73 74 20 66 72 65 71 75 65 6e 74 6c  * most frequentl
c1310 79 20 75 73 65 64 20 63 6f 6e 76 65 72 73 69 6f  y used conversio
c1320 6e 20 74 79 70 65 73 20 66 69 72 73 74 2e 0a 2a  n types first..*
c1330 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
c1340 68 61 72 20 61 44 69 67 69 74 73 5b 5d 20 3d 20  har aDigits[] = 
c1350 22 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45  "0123456789ABCDE
c1360 46 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65  F0123456789abcde
c1370 66 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  f";.static const
c1380 20 63 68 61 72 20 61 50 72 65 66 69 78 5b 5d 20   char aPrefix[] 
c1390 3d 20 22 2d 78 30 5c 30 30 30 58 30 22 3b 0a 73  = "-x0\000X0";.s
c13a0 74 61 74 69 63 20 63 6f 6e 73 74 20 65 74 5f 69  tatic const et_i
c13b0 6e 66 6f 20 66 6d 74 69 6e 66 6f 5b 5d 20 3d 20  nfo fmtinfo[] = 
c13c0 7b 0a 20 20 7b 20 20 27 64 27 2c 20 31 30 2c 20  {.  {  'd', 10, 
c13d0 31 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20  1, etRADIX,     
c13e0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
c13f0 73 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 54 52  s',  0, 4, etSTR
c1400 49 4e 47 2c 20 20 20 20 20 30 2c 20 20 30 20 7d  ING,     0,  0 }
c1410 2c 0a 20 20 7b 20 20 27 67 27 2c 20 20 30 2c 20  ,.  {  'g',  0, 
c1420 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20  1, etGENERIC,   
c1430 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27   30, 0 },.  {  '
c1440 7a 27 2c 20 20 30 2c 20 34 2c 20 65 74 44 59 4e  z',  0, 4, etDYN
c1450 53 54 52 49 4e 47 2c 20 20 30 2c 20 20 30 20 7d  STRING,  0,  0 }
c1460 2c 0a 20 20 7b 20 20 27 71 27 2c 20 20 30 2c 20  ,.  {  'q',  0, 
c1470 34 2c 20 65 74 53 51 4c 45 53 43 41 50 45 2c 20  4, etSQLESCAPE, 
c1480 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
c1490 51 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c  Q',  0, 4, etSQL
c14a0 45 53 43 41 50 45 32 2c 20 30 2c 20 20 30 20 7d  ESCAPE2, 0,  0 }
c14b0 2c 0a 20 20 7b 20 20 27 77 27 2c 20 20 30 2c 20  ,.  {  'w',  0, 
c14c0 34 2c 20 65 74 53 51 4c 45 53 43 41 50 45 33 2c  4, etSQLESCAPE3,
c14d0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
c14e0 63 27 2c 20 20 30 2c 20 30 2c 20 65 74 43 48 41  c',  0, 0, etCHA
c14f0 52 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d  RX,      0,  0 }
c1500 2c 0a 20 20 7b 20 20 27 6f 27 2c 20 20 38 2c 20  ,.  {  'o',  8, 
c1510 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20  0, etRADIX,     
c1520 20 30 2c 20 20 32 20 7d 2c 0a 20 20 7b 20 20 27   0,  2 },.  {  '
c1530 75 27 2c 20 31 30 2c 20 30 2c 20 65 74 52 41 44  u', 10, 0, etRAD
c1540 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d  IX,      0,  0 }
c1550 2c 0a 20 20 7b 20 20 27 78 27 2c 20 31 36 2c 20  ,.  {  'x', 16, 
c1560 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20  0, etRADIX,     
c1570 20 31 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 20 27   16, 1 },.  {  '
c1580 58 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44  X', 16, 0, etRAD
c1590 49 58 2c 20 20 20 20 20 20 30 2c 20 20 34 20 7d  IX,      0,  4 }
c15a0 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ,.#ifndef SQLITE
c15b0 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
c15c0 4f 49 4e 54 0a 20 20 7b 20 20 27 66 27 2c 20 20  OINT.  {  'f',  
c15d0 30 2c 20 31 2c 20 65 74 46 4c 4f 41 54 2c 20 20  0, 1, etFLOAT,  
c15e0 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b      0,  0 },.  {
c15f0 20 20 27 65 27 2c 20 20 30 2c 20 31 2c 20 65 74    'e',  0, 1, et
c1600 45 58 50 2c 20 20 20 20 20 20 20 20 33 30 2c 20  EXP,        30, 
c1610 30 20 7d 2c 0a 20 20 7b 20 20 27 45 27 2c 20 20  0 },.  {  'E',  
c1620 30 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20  0, 1, etEXP,    
c1630 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a 20 20 7b      14, 0 },.  {
c1640 20 20 27 47 27 2c 20 20 30 2c 20 31 2c 20 65 74    'G',  0, 1, et
c1650 47 45 4e 45 52 49 43 2c 20 20 20 20 31 34 2c 20  GENERIC,    14, 
c1660 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 7b 20  0 },.#endif.  { 
c1670 20 27 69 27 2c 20 31 30 2c 20 31 2c 20 65 74 52   'i', 10, 1, etR
c1680 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30  ADIX,      0,  0
c1690 20 7d 2c 0a 20 20 7b 20 20 27 6e 27 2c 20 20 30   },.  {  'n',  0
c16a0 2c 20 30 2c 20 65 74 53 49 5a 45 2c 20 20 20 20  , 0, etSIZE,    
c16b0 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20     0,  0 },.  { 
c16c0 20 27 25 27 2c 20 20 30 2c 20 30 2c 20 65 74 50   '%',  0, 0, etP
c16d0 45 52 43 45 4e 54 2c 20 20 20 20 30 2c 20 20 30  ERCENT,    0,  0
c16e0 20 7d 2c 0a 20 20 7b 20 20 27 70 27 2c 20 31 36   },.  {  'p', 16
c16f0 2c 20 30 2c 20 65 74 50 4f 49 4e 54 45 52 2c 20  , 0, etPOINTER, 
c1700 20 20 20 30 2c 20 20 31 20 7d 2c 0a 0a 2f 2a 20     0,  1 },../* 
c1710 41 6c 6c 20 74 68 65 20 72 65 73 74 20 68 61 76  All the rest hav
c1720 65 20 74 68 65 20 46 4c 41 47 5f 49 4e 54 45 52  e the FLAG_INTER
c1730 4e 20 62 69 74 20 73 65 74 20 61 6e 64 20 61 72  N bit set and ar
c1740 65 20 74 68 75 73 20 66 6f 72 20 69 6e 74 65 72  e thus for inter
c1750 6e 61 6c 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20  nal.** use only 
c1760 2a 2f 0a 20 20 7b 20 20 27 54 27 2c 20 20 30 2c  */.  {  'T',  0,
c1770 20 32 2c 20 65 74 54 4f 4b 45 4e 2c 20 20 20 20   2, etTOKEN,    
c1780 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
c1790 27 53 27 2c 20 20 30 2c 20 32 2c 20 65 74 53 52  'S',  0, 2, etSR
c17a0 43 4c 49 53 54 2c 20 20 20 20 30 2c 20 20 30 20  CLIST,    0,  0 
c17b0 7d 2c 0a 20 20 7b 20 20 27 72 27 2c 20 31 30 2c  },.  {  'r', 10,
c17c0 20 33 2c 20 65 74 4f 52 44 49 4e 41 4c 2c 20 20   3, etORDINAL,  
c17d0 20 20 30 2c 20 20 30 20 7d 2c 0a 7d 3b 0a 0a 2f    0,  0 },.};../
c17e0 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f  *.** If SQLITE_O
c17f0 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
c1800 4e 54 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74  NT is defined, t
c1810 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20  hen none of the 
c1820 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a  floating point.*
c1830 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69  * conversions wi
c1840 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66 6e  ll work..*/.#ifn
c1850 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
c1860 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f  FLOATING_POINT./
c1870 2a 0a 2a 2a 20 22 2a 76 61 6c 22 20 69 73 20 61  *.** "*val" is a
c1880 20 64 6f 75 62 6c 65 20 73 75 63 68 20 74 68 61   double such tha
c1890 74 20 30 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c 20  t 0.1 <= *val < 
c18a0 31 30 2e 30 0a 2a 2a 20 52 65 74 75 72 6e 20 74  10.0.** Return t
c18b0 68 65 20 61 73 63 69 69 20 63 6f 64 65 20 66 6f  he ascii code fo
c18c0 72 20 74 68 65 20 6c 65 61 64 69 6e 67 20 64 69  r the leading di
c18d0 67 69 74 20 6f 66 20 2a 76 61 6c 2c 20 74 68 65  git of *val, the
c18e0 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 79 20 22 2a  n.** multiply "*
c18f0 76 61 6c 22 20 62 79 20 31 30 2e 30 20 74 6f 20  val" by 10.0 to 
c1900 72 65 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a  renormalize..**.
c1910 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 20  ** Example:.**  
c1920 20 20 20 69 6e 70 75 74 3a 20 20 20 20 20 2a 76     input:     *v
c1930 61 6c 20 3d 20 33 2e 31 34 31 35 39 0a 2a 2a 20  al = 3.14159.** 
c1940 20 20 20 20 6f 75 74 70 75 74 3a 20 20 20 20 2a      output:    *
c1950 76 61 6c 20 3d 20 31 2e 34 31 35 39 20 20 20 20  val = 1.4159    
c1960 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 20  function return 
c1970 3d 20 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20  = '3'.**.** The 
c1980 63 6f 75 6e 74 65 72 20 2a 63 6e 74 20 69 73 20  counter *cnt is 
c1990 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68  incremented each
c19a0 20 74 69 6d 65 2e 20 20 41 66 74 65 72 20 63 6f   time.  After co
c19b0 75 6e 74 65 72 20 65 78 63 65 65 64 73 0a 2a 2a  unter exceeds.**
c19c0 20 31 36 20 28 74 68 65 20 6e 75 6d 62 65 72 20   16 (the number 
c19d0 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64  of significant d
c19e0 69 67 69 74 73 20 69 6e 20 61 20 36 34 2d 62 69  igits in a 64-bi
c19f0 74 20 66 6c 6f 61 74 29 20 27 30 27 20 69 73 0a  t float) '0' is.
c1a00 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e  ** always return
c1a10 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68  ed..*/.static ch
c1a20 61 72 20 65 74 5f 67 65 74 64 69 67 69 74 28 4c  ar et_getdigit(L
c1a30 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a  ONGDOUBLE_TYPE *
c1a40 76 61 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a  val, int *cnt){.
c1a50 20 20 69 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c    int digit;.  L
c1a60 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 64  ONGDOUBLE_TYPE d
c1a70 3b 0a 20 20 69 66 28 20 28 2a 63 6e 74 29 3c 3d  ;.  if( (*cnt)<=
c1a80 30 20 29 20 72 65 74 75 72 6e 20 27 30 27 3b 0a  0 ) return '0';.
c1a90 20 20 28 2a 63 6e 74 29 2d 2d 3b 0a 20 20 64 69    (*cnt)--;.  di
c1aa0 67 69 74 20 3d 20 28 69 6e 74 29 2a 76 61 6c 3b  git = (int)*val;
c1ab0 0a 20 20 64 20 3d 20 64 69 67 69 74 3b 0a 20 20  .  d = digit;.  
c1ac0 64 69 67 69 74 20 2b 3d 20 27 30 27 3b 0a 20 20  digit += '0';.  
c1ad0 2a 76 61 6c 20 3d 20 28 2a 76 61 6c 20 2d 20 64  *val = (*val - d
c1ae0 29 2a 31 30 2e 30 3b 0a 20 20 72 65 74 75 72 6e  )*10.0;.  return
c1af0 20 28 63 68 61 72 29 64 69 67 69 74 3b 0a 7d 0a   (char)digit;.}.
c1b00 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
c1b10 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
c1b20 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  OINT */../*.** A
c1b30 70 70 65 6e 64 20 4e 20 73 70 61 63 65 20 63 68  ppend N space ch
c1b40 61 72 61 63 74 65 72 73 20 74 6f 20 74 68 65 20  aracters to the 
c1b50 67 69 76 65 6e 20 73 74 72 69 6e 67 20 62 75 66  given string buf
c1b60 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  fer..*/.SQLITE_P
c1b70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
c1b80 74 65 33 41 70 70 65 6e 64 53 70 61 63 65 28 53  te3AppendSpace(S
c1b90 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c  trAccum *pAccum,
c1ba0 20 69 6e 74 20 4e 29 7b 0a 20 20 73 74 61 74 69   int N){.  stati
c1bb0 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 70  c const char zSp
c1bc0 61 63 65 73 5b 5d 20 3d 20 22 20 20 20 20 20 20  aces[] = "      
c1bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1be0 20 20 20 20 20 20 20 22 3b 0a 20 20 77 68 69 6c         ";.  whil
c1bf0 65 28 20 4e 3e 3d 28 69 6e 74 29 73 69 7a 65 6f  e( N>=(int)sizeo
c1c00 66 28 7a 53 70 61 63 65 73 29 2d 31 20 29 7b 0a  f(zSpaces)-1 ){.
c1c10 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
c1c20 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
c1c30 2c 20 7a 53 70 61 63 65 73 2c 20 73 69 7a 65 6f  , zSpaces, sizeo
c1c40 66 28 7a 53 70 61 63 65 73 29 2d 31 29 3b 0a 20  f(zSpaces)-1);. 
c1c50 20 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 28 7a     N -= sizeof(z
c1c60 53 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d 0a 20  Spaces)-1;.  }. 
c1c70 20 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20   if( N>0 ){.    
c1c80 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
c1c90 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53  ppend(pAccum, zS
c1ca0 70 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a 7d  paces, N);.  }.}
c1cb0 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68 69  ../*.** On machi
c1cc0 6e 65 73 20 77 69 74 68 20 61 20 73 6d 61 6c 6c  nes with a small
c1cd0 20 73 74 61 63 6b 20 73 69 7a 65 2c 20 79 6f 75   stack size, you
c1ce0 20 63 61 6e 20 72 65 64 65 66 69 6e 65 20 74 68   can redefine th
c1cf0 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 49 4e  e.** SQLITE_PRIN
c1d00 54 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20 62 65  T_BUF_SIZE to be
c1d10 20 73 6f 6d 65 74 68 69 6e 67 20 73 6d 61 6c 6c   something small
c1d20 65 72 2c 20 69 66 20 64 65 73 69 72 65 64 2e 0a  er, if desired..
c1d30 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
c1d40 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45  E_PRINT_BUF_SIZE
c1d50 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
c1d60 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20  _PRINT_BUF_SIZE 
c1d70 37 30 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e  70.#endif.#defin
c1d80 65 20 65 74 42 55 46 53 49 5a 45 20 53 51 4c 49  e etBUFSIZE SQLI
c1d90 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a  TE_PRINT_BUF_SIZ
c1da0 45 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68  E  /* Size of th
c1db0 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  e output buffer 
c1dc0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6e 64 65 72  */../*.** Render
c1dd0 20 61 20 73 74 72 69 6e 67 20 67 69 76 65 6e 20   a string given 
c1de0 62 79 20 22 66 6d 74 22 20 69 6e 74 6f 20 74 68  by "fmt" into th
c1df0 65 20 53 74 72 41 63 63 75 6d 20 6f 62 6a 65 63  e StrAccum objec
c1e00 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
c1e10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
c1e20 33 56 58 50 72 69 6e 74 66 28 0a 20 20 53 74 72  3VXPrintf(.  Str
c1e30 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 20  Accum *pAccum,  
c1e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1e50 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 72 65  /* Accumulate re
c1e60 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20  sults here */.  
c1e70 69 6e 74 20 75 73 65 45 78 74 65 6e 64 65 64 2c  int useExtended,
c1e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1e90 20 20 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 65     /* Allow exte
c1ea0 6e 64 65 64 20 25 2d 63 6f 6e 76 65 72 73 69 6f  nded %-conversio
c1eb0 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ns */.  const ch
c1ec0 61 72 20 2a 66 6d 74 2c 20 20 20 20 20 20 20 20  ar *fmt,        
c1ed0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f             /* Fo
c1ee0 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20  rmat string */. 
c1ef0 20 76 61 5f 6c 69 73 74 20 61 70 20 20 20 20 20   va_list ap     
c1f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1f10 20 20 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73      /* arguments
c1f20 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 20   */.){.  int c; 
c1f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1f40 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 61 72      /* Next char
c1f50 61 63 74 65 72 20 69 6e 20 74 68 65 20 66 6f 72  acter in the for
c1f60 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  mat string */.  
c1f70 63 68 61 72 20 2a 62 75 66 70 74 3b 20 20 20 20  char *bufpt;    
c1f80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
c1f90 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e  inter to the con
c1fa0 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a  version buffer *
c1fb0 2f 0a 20 20 69 6e 74 20 70 72 65 63 69 73 69 6f  /.  int precisio
c1fc0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  n;             /
c1fd0 2a 20 50 72 65 63 69 73 69 6f 6e 20 6f 66 20 74  * Precision of t
c1fe0 68 65 20 63 75 72 72 65 6e 74 20 66 69 65 6c 64  he current field
c1ff0 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 74 68   */.  int length
c2000 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
c2010 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68   /* Length of th
c2020 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74  e field */.  int
c2030 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20   idx;           
c2040 20 20 20 20 20 20 20 20 2f 2a 20 41 20 67 65 6e          /* A gen
c2050 65 72 61 6c 20 70 75 72 70 6f 73 65 20 6c 6f 6f  eral purpose loo
c2060 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
c2070 6e 74 20 77 69 64 74 68 3b 20 20 20 20 20 20 20  nt width;       
c2080 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69 64            /* Wid
c2090 74 68 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  th of the curren
c20a0 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20 65 74 42  t field */.  etB
c20b0 79 74 65 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73  yte flag_leftjus
c20c0 74 69 66 79 3b 20 20 20 2f 2a 20 54 72 75 65 20  tify;   /* True 
c20d0 69 66 20 22 2d 22 20 66 6c 61 67 20 69 73 20 70  if "-" flag is p
c20e0 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79  resent */.  etBy
c20f0 74 65 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e  te flag_plussign
c2100 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ;      /* True i
c2110 66 20 22 2b 22 20 66 6c 61 67 20 69 73 20 70 72  f "+" flag is pr
c2120 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74  esent */.  etByt
c2130 65 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e  e flag_blanksign
c2140 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ;     /* True if
c2150 20 22 20 22 20 66 6c 61 67 20 69 73 20 70 72 65   " " flag is pre
c2160 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65  sent */.  etByte
c2170 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66   flag_alternatef
c2180 6f 72 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66 20  orm; /* True if 
c2190 22 23 22 20 66 6c 61 67 20 69 73 20 70 72 65 73  "#" flag is pres
c21a0 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  ent */.  etByte 
c21b0 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20  flag_altform2;  
c21c0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22      /* True if "
c21d0 21 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65  !" flag is prese
c21e0 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  nt */.  etByte f
c21f0 6c 61 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20 20  lag_zeropad;    
c2200 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66 69     /* True if fi
c2210 65 6c 64 20 77 69 64 74 68 20 63 6f 6e 73 74 61  eld width consta
c2220 6e 74 20 73 74 61 72 74 73 20 77 69 74 68 20 7a  nt starts with z
c2230 65 72 6f 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  ero */.  etByte 
c2240 66 6c 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 20  flag_long;      
c2250 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22      /* True if "
c2260 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65  l" flag is prese
c2270 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  nt */.  etByte f
c2280 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20  lag_longlong;   
c2290 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
c22a0 65 20 22 6c 6c 22 20 66 6c 61 67 20 69 73 20 70  e "ll" flag is p
c22b0 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79  resent */.  etBy
c22c0 74 65 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20  te done;        
c22d0 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74         /* Loop t
c22e0 65 72 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 67 20  ermination flag 
c22f0 2a 2f 0a 20 20 65 74 42 79 74 65 20 78 74 79 70  */.  etByte xtyp
c2300 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
c2310 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61  /* Conversion pa
c2320 72 61 64 69 67 6d 20 2a 2f 0a 20 20 63 68 61 72  radigm */.  char
c2330 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20   prefix;        
c2340 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 69 78         /* Prefix
c2350 20 63 68 61 72 61 63 74 65 72 2e 20 20 22 2b 22   character.  "+"
c2360 20 6f 72 20 22 2d 22 20 6f 72 20 22 20 22 20 6f   or "-" or " " o
c2370 72 20 27 5c 30 27 2e 20 2a 2f 0a 20 20 73 71 6c  r '\0'. */.  sql
c2380 69 74 65 5f 75 69 6e 74 36 34 20 6c 6f 6e 67 76  ite_uint64 longv
c2390 61 6c 75 65 3b 20 20 20 2f 2a 20 56 61 6c 75 65  alue;   /* Value
c23a0 20 66 6f 72 20 69 6e 74 65 67 65 72 20 74 79 70   for integer typ
c23b0 65 73 20 2a 2f 0a 20 20 4c 4f 4e 47 44 4f 55 42  es */.  LONGDOUB
c23c0 4c 45 5f 54 59 50 45 20 72 65 61 6c 76 61 6c 75  LE_TYPE realvalu
c23d0 65 3b 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20  e; /* Value for 
c23e0 72 65 61 6c 20 74 79 70 65 73 20 2a 2f 0a 20 20  real types */.  
c23f0 63 6f 6e 73 74 20 65 74 5f 69 6e 66 6f 20 2a 69  const et_info *i
c2400 6e 66 6f 70 3b 20 20 20 20 20 20 2f 2a 20 50 6f  nfop;      /* Po
c2410 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70  inter to the app
c2420 72 6f 70 72 69 61 74 65 20 69 6e 66 6f 20 73 74  ropriate info st
c2430 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 63 68 61  ructure */.  cha
c2440 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20  r *zOut;        
c2450 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6e 64 65          /* Rende
c2460 72 69 6e 67 20 62 75 66 66 65 72 20 2a 2f 0a 20  ring buffer */. 
c2470 20 69 6e 74 20 6e 4f 75 74 3b 20 20 20 20 20 20   int nOut;      
c2480 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
c2490 69 7a 65 20 6f 66 20 74 68 65 20 72 65 6e 64 65  ize of the rende
c24a0 72 69 6e 67 20 62 75 66 66 65 72 20 2a 2f 0a 20  ring buffer */. 
c24b0 20 63 68 61 72 20 2a 7a 45 78 74 72 61 3b 20 20   char *zExtra;  
c24c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
c24d0 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 20 75  alloced memory u
c24e0 73 65 64 20 62 79 20 73 6f 6d 65 20 63 6f 6e 76  sed by some conv
c24f0 65 72 73 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65  ersion */.#ifnde
c2500 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
c2510 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 69  OATING_POINT.  i
c2520 6e 74 20 20 65 78 70 2c 20 65 32 3b 20 20 20 20  nt  exp, e2;    
c2530 20 20 20 20 20 20 20 20 20 20 2f 2a 20 65 78 70            /* exp
c2540 6f 6e 65 6e 74 20 6f 66 20 72 65 61 6c 20 6e 75  onent of real nu
c2550 6d 62 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  mbers */.  int n
c2560 73 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sd;             
c2570 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
c2580 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64  of significant d
c2590 69 67 69 74 73 20 72 65 74 75 72 6e 65 64 20 2a  igits returned *
c25a0 2f 0a 20 20 64 6f 75 62 6c 65 20 72 6f 75 6e 64  /.  double round
c25b0 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  er;            /
c25c0 2a 20 55 73 65 64 20 66 6f 72 20 72 6f 75 6e 64  * Used for round
c25d0 69 6e 67 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  ing floating poi
c25e0 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 65  nt values */.  e
c25f0 74 42 79 74 65 20 66 6c 61 67 5f 64 70 3b 20 20  tByte flag_dp;  
c2600 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
c2610 65 20 69 66 20 64 65 63 69 6d 61 6c 20 70 6f 69  e if decimal poi
c2620 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 73 68 6f  nt should be sho
c2630 77 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  wn */.  etByte f
c2640 6c 61 67 5f 72 74 7a 3b 20 20 20 20 20 20 20 20  lag_rtz;        
c2650 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 72     /* True if tr
c2660 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 73 68 6f  ailing zeros sho
c2670 75 6c 64 20 62 65 20 72 65 6d 6f 76 65 64 20 2a  uld be removed *
c2680 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68 61 72 20  /.#endif.  char 
c2690 62 75 66 5b 65 74 42 55 46 53 49 5a 45 5d 3b 20  buf[etBUFSIZE]; 
c26a0 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73        /* Convers
c26b0 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 0a 20  ion buffer */.. 
c26c0 20 62 75 66 70 74 20 3d 20 30 3b 0a 20 20 66 6f   bufpt = 0;.  fo
c26d0 72 28 3b 20 28 63 3d 28 2a 66 6d 74 29 29 21 3d  r(; (c=(*fmt))!=
c26e0 30 3b 20 2b 2b 66 6d 74 29 7b 0a 20 20 20 20 69  0; ++fmt){.    i
c26f0 66 28 20 63 21 3d 27 25 27 20 29 7b 0a 20 20 20  f( c!='%' ){.   
c2700 20 20 20 69 6e 74 20 61 6d 74 3b 0a 20 20 20 20     int amt;.    
c2710 20 20 62 75 66 70 74 20 3d 20 28 63 68 61 72 20    bufpt = (char 
c2720 2a 29 66 6d 74 3b 0a 20 20 20 20 20 20 61 6d 74  *)fmt;.      amt
c2730 20 3d 20 31 3b 0a 20 20 20 20 20 20 77 68 69 6c   = 1;.      whil
c2740 65 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21  e( (c=(*++fmt))!
c2750 3d 27 25 27 20 26 26 20 63 21 3d 30 20 29 20 61  ='%' && c!=0 ) a
c2760 6d 74 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69  mt++;.      sqli
c2770 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
c2780 64 28 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c  d(pAccum, bufpt,
c2790 20 61 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28   amt);.      if(
c27a0 20 63 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20   c==0 ) break;. 
c27b0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 63 3d     }.    if( (c=
c27c0 28 2a 2b 2b 66 6d 74 29 29 3d 3d 30 20 29 7b 0a  (*++fmt))==0 ){.
c27d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
c27e0 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63  AccumAppend(pAcc
c27f0 75 6d 2c 20 22 25 22 2c 20 31 29 3b 0a 20 20 20  um, "%", 1);.   
c2800 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
c2810 20 20 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20      /* Find out 
c2820 77 68 61 74 20 66 6c 61 67 73 20 61 72 65 20 70  what flags are p
c2830 72 65 73 65 6e 74 20 2a 2f 0a 20 20 20 20 66 6c  resent */.    fl
c2840 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d  ag_leftjustify =
c2850 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d   flag_plussign =
c2860 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20   flag_blanksign 
c2870 3d 20 0a 20 20 20 20 20 66 6c 61 67 5f 61 6c 74  = .     flag_alt
c2880 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 66 6c 61  ernateform = fla
c2890 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 66 6c 61  g_altform2 = fla
c28a0 67 5f 7a 65 72 6f 70 61 64 20 3d 20 30 3b 0a 20  g_zeropad = 0;. 
c28b0 20 20 20 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20     done = 0;.   
c28c0 20 64 6f 7b 0a 20 20 20 20 20 20 73 77 69 74 63   do{.      switc
c28d0 68 28 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20  h( c ){.        
c28e0 63 61 73 65 20 27 2d 27 3a 20 20 20 66 6c 61 67  case '-':   flag
c28f0 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31  _leftjustify = 1
c2900 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ;     break;.   
c2910 20 20 20 20 20 63 61 73 65 20 27 2b 27 3a 20 20       case '+':  
c2920 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d   flag_plussign =
c2930 20 31 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b   1;        break
c2940 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
c2950 20 27 3a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b   ':   flag_blank
c2960 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20  sign = 1;       
c2970 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
c2980 61 73 65 20 27 23 27 3a 20 20 20 66 6c 61 67 5f  ase '#':   flag_
c2990 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20  alternateform = 
c29a0 31 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  1;   break;.    
c29b0 20 20 20 20 63 61 73 65 20 27 21 27 3a 20 20 20      case '!':   
c29c0 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20  flag_altform2 = 
c29d0 31 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  1;        break;
c29e0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 30  .        case '0
c29f0 27 3a 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61  ':   flag_zeropa
c2a00 64 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 62  d = 1;         b
c2a10 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65  reak;.        de
c2a20 66 61 75 6c 74 3a 20 20 20 20 64 6f 6e 65 20 3d  fault:    done =
c2a30 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   1;             
c2a40 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
c2a50 20 7d 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 21   }.    }while( !
c2a60 64 6f 6e 65 20 26 26 20 28 63 3d 28 2a 2b 2b 66  done && (c=(*++f
c2a70 6d 74 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 2f  mt))!=0 );.    /
c2a80 2a 20 47 65 74 20 74 68 65 20 66 69 65 6c 64 20  * Get the field 
c2a90 77 69 64 74 68 20 2a 2f 0a 20 20 20 20 77 69 64  width */.    wid
c2aa0 74 68 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  th = 0;.    if( 
c2ab0 63 3d 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20  c=='*' ){.      
c2ac0 77 69 64 74 68 20 3d 20 76 61 5f 61 72 67 28 61  width = va_arg(a
c2ad0 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 66  p,int);.      if
c2ae0 28 20 77 69 64 74 68 3c 30 20 29 7b 0a 20 20 20  ( width<0 ){.   
c2af0 20 20 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75       flag_leftju
c2b00 73 74 69 66 79 20 3d 20 31 3b 0a 20 20 20 20 20  stify = 1;.     
c2b10 20 20 20 77 69 64 74 68 20 3d 20 2d 77 69 64 74     width = -widt
c2b20 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  h;.      }.     
c2b30 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20   c = *++fmt;.   
c2b40 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 77 68   }else{.      wh
c2b50 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63  ile( c>='0' && c
c2b60 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20  <='9' ){.       
c2b70 20 77 69 64 74 68 20 3d 20 77 69 64 74 68 2a 31   width = width*1
c2b80 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20  0 + c - '0';.   
c2b90 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b       c = *++fmt;
c2ba0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
c2bb0 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 70 72     /* Get the pr
c2bc0 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69  ecision */.    i
c2bd0 66 28 20 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20  f( c=='.' ){.   
c2be0 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 30     precision = 0
c2bf0 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66  ;.      c = *++f
c2c00 6d 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d  mt;.      if( c=
c2c10 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='*' ){.        
c2c20 70 72 65 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61  precision = va_a
c2c30 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20  rg(ap,int);.    
c2c40 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f      if( precisio
c2c50 6e 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20  n<0 ) precision 
c2c60 3d 20 2d 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20  = -precision;.  
c2c70 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74        c = *++fmt
c2c80 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
c2c90 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e         while( c>
c2ca0 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29  ='0' && c<='9' )
c2cb0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 63  {.          prec
c2cc0 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f  ision = precisio
c2cd0 6e 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a  n*10 + c - '0';.
c2ce0 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b            c = *+
c2cf0 2b 66 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  +fmt;.        }.
c2d00 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
c2d10 65 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69  e{.      precisi
c2d20 6f 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  on = -1;.    }. 
c2d30 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f     /* Get the co
c2d40 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f  nversion type mo
c2d50 64 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 66  difier */.    if
c2d60 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20  ( c=='l' ){.    
c2d70 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b    flag_long = 1;
c2d80 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d  .      c = *++fm
c2d90 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d  t;.      if( c==
c2da0 27 6c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 66  'l' ){.        f
c2db0 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31  lag_longlong = 1
c2dc0 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b  ;.        c = *+
c2dd0 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73  +fmt;.      }els
c2de0 65 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f  e{.        flag_
c2df0 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20  longlong = 0;.  
c2e00 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
c2e10 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67  .      flag_long
c2e20 20 3d 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67   = flag_longlong
c2e30 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
c2e40 2f 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 66  /* Fetch the inf
c2e50 6f 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20  o entry for the 
c2e60 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66  field */.    inf
c2e70 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 30 5d  op = &fmtinfo[0]
c2e80 3b 0a 20 20 20 20 78 74 79 70 65 20 3d 20 65 74  ;.    xtype = et
c2e90 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 66 6f 72  INVALID;.    for
c2ea0 28 69 64 78 3d 30 3b 20 69 64 78 3c 41 72 72 61  (idx=0; idx<Arra
c2eb0 79 53 69 7a 65 28 66 6d 74 69 6e 66 6f 29 3b 20  ySize(fmtinfo); 
c2ec0 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  idx++){.      if
c2ed0 28 20 63 3d 3d 66 6d 74 69 6e 66 6f 5b 69 64 78  ( c==fmtinfo[idx
c2ee0 5d 2e 66 6d 74 74 79 70 65 20 29 7b 0a 20 20 20  ].fmttype ){.   
c2ef0 20 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d       infop = &fm
c2f00 74 69 6e 66 6f 5b 69 64 78 5d 3b 0a 20 20 20 20  tinfo[idx];.    
c2f10 20 20 20 20 69 66 28 20 75 73 65 45 78 74 65 6e      if( useExten
c2f20 64 65 64 20 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66  ded || (infop->f
c2f30 6c 61 67 73 20 26 20 46 4c 41 47 5f 49 4e 54 45  lags & FLAG_INTE
c2f40 52 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  RN)==0 ){.      
c2f50 20 20 20 20 78 74 79 70 65 20 3d 20 69 6e 66 6f      xtype = info
c2f60 70 2d 3e 74 79 70 65 3b 0a 20 20 20 20 20 20 20  p->type;.       
c2f70 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
c2f80 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
c2f90 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
c2fa0 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
c2fb0 0a 20 20 20 20 7a 45 78 74 72 61 20 3d 20 30 3b  .    zExtra = 0;
c2fc0 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
c2fd0 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 76  At this point, v
c2fe0 61 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e 69  ariables are ini
c2ff0 74 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c 6c  tialized as foll
c3000 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ows:.    **.    
c3010 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e  **   flag_altern
c3020 61 74 65 66 6f 72 6d 20 20 20 20 20 20 20 20 20  ateform         
c3030 20 54 52 55 45 20 69 66 20 61 20 27 23 27 20 69   TRUE if a '#' i
c3040 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a  s present..    *
c3050 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d  *   flag_altform
c3060 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2               
c3070 54 52 55 45 20 69 66 20 61 20 27 21 27 20 69 73  TRUE if a '!' is
c3080 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a   present..    **
c3090 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e     flag_plussign
c30a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
c30b0 52 55 45 20 69 66 20 61 20 27 2b 27 20 69 73 20  RUE if a '+' is 
c30c0 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20  present..    ** 
c30d0 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69    flag_leftjusti
c30e0 66 79 20 20 20 20 20 20 20 20 20 20 20 20 54 52  fy            TR
c30f0 55 45 20 69 66 20 61 20 27 2d 27 20 69 73 20 70  UE if a '-' is p
c3100 72 65 73 65 6e 74 20 6f 72 20 69 66 20 74 68 65  resent or if the
c3110 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
c3120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3130 20 20 20 20 20 20 66 69 65 6c 64 20 77 69 64 74        field widt
c3140 68 20 77 61 73 20 6e 65 67 61 74 69 76 65 2e 0a  h was negative..
c3150 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a 65      **   flag_ze
c3160 72 6f 70 61 64 20 20 20 20 20 20 20 20 20 20 20  ropad           
c3170 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65       TRUE if the
c3180 20 77 69 64 74 68 20 62 65 67 61 6e 20 77 69 74   width began wit
c3190 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c  h 0..    **   fl
c31a0 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20 20  ag_long         
c31b0 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69            TRUE i
c31c0 66 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c 27  f the letter 'l'
c31d0 20 28 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a   (ell) prefixed.
c31e0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
c31f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3200 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73       the convers
c3210 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20  ion character.. 
c3220 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e     **   flag_lon
c3230 67 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 20  glong           
c3240 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20      TRUE if the 
c3250 6c 65 74 74 65 72 20 27 6c 6c 27 20 28 65 6c 6c  letter 'll' (ell
c3260 20 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20   ell) prefixed. 
c3270 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
c3280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3290 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69      the conversi
c32a0 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20  on character..  
c32b0 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61 6e    **   flag_blan
c32c0 6b 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 20  ksign           
c32d0 20 20 20 54 52 55 45 20 69 66 20 61 20 27 20 27     TRUE if a ' '
c32e0 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20   is present..   
c32f0 20 2a 2a 20 20 20 77 69 64 74 68 20 20 20 20 20   **   width     
c3300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3310 20 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20    The specified 
c3320 66 69 65 6c 64 20 77 69 64 74 68 2e 20 20 54 68  field width.  Th
c3330 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 20 20 20  is is.    **    
c3340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3350 20 20 20 20 20 20 20 20 20 20 20 61 6c 77 61 79             alway
c3360 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20  s non-negative. 
c3370 20 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65 66   Zero is the def
c3380 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20 70  ault..    **   p
c3390 72 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20 20  recision        
c33a0 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 73             The s
c33b0 70 65 63 69 66 69 65 64 20 70 72 65 63 69 73 69  pecified precisi
c33c0 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74  on.  The default
c33d0 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
c33e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c33f0 20 20 20 20 20 20 69 73 20 2d 31 2e 0a 20 20 20        is -1..   
c3400 20 2a 2a 20 20 20 78 74 79 70 65 20 20 20 20 20   **   xtype     
c3410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3420 20 20 54 68 65 20 63 6c 61 73 73 20 6f 66 20 74    The class of t
c3430 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 20  he conversion.. 
c3440 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20 20     **   infop   
c3450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3460 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 74      Pointer to t
c3470 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 69  he appropriate i
c3480 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20 20 20 20  nfo struct..    
c3490 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 28 20 78  */.    switch( x
c34a0 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61  type ){.      ca
c34b0 73 65 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20 20  se etPOINTER:.  
c34c0 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c        flag_longl
c34d0 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61  ong = sizeof(cha
c34e0 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34 29  r*)==sizeof(i64)
c34f0 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c  ;.        flag_l
c3500 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61  ong = sizeof(cha
c3510 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e 67  r*)==sizeof(long
c3520 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 2f   int);.        /
c3530 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69  * Fall through i
c3540 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73  nto the next cas
c3550 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  e */.      case 
c3560 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20 20  etORDINAL:.     
c3570 20 63 61 73 65 20 65 74 52 41 44 49 58 3a 0a 20   case etRADIX:. 
c3580 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 70         if( infop
c3590 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53  ->flags & FLAG_S
c35a0 49 47 4e 45 44 20 29 7b 0a 20 20 20 20 20 20 20  IGNED ){.       
c35b0 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20 20     i64 v;.      
c35c0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e      if( flag_lon
c35d0 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20  glong ){.       
c35e0 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28       v = va_arg(
c35f0 61 70 2c 69 36 34 29 3b 0a 20 20 20 20 20 20 20  ap,i64);.       
c3600 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61     }else if( fla
c3610 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20  g_long ){.      
c3620 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67        v = va_arg
c3630 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20  (ap,long int);. 
c3640 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
c3650 20 20 20 20 20 20 20 20 20 20 20 20 76 20 3d 20              v = 
c3660 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a  va_arg(ap,int);.
c3670 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
c3680 20 20 20 20 20 20 69 66 28 20 76 3c 30 20 29 7b        if( v<0 ){
c3690 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
c36a0 20 76 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54   v==SMALLEST_INT
c36b0 36 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  64 ){.          
c36c0 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20      longvalue = 
c36d0 28 28 75 36 34 29 31 29 3c 3c 36 33 3b 0a 20 20  ((u64)1)<<63;.  
c36e0 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
c36f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c  .              l
c3700 6f 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a 20  ongvalue = -v;. 
c3710 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
c3720 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20           prefix 
c3730 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20  = '-';.         
c3740 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
c3750 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20      longvalue = 
c3760 76 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  v;.            i
c3770 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e  f( flag_plussign
c3780 20 29 20 20 20 20 20 20 20 20 70 72 65 66 69 78   )        prefix
c3790 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20   = '+';.        
c37a0 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61      else if( fla
c37b0 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 70  g_blanksign )  p
c37c0 72 65 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20  refix = ' ';.   
c37d0 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 20           else   
c37e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c37f0 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a      prefix = 0;.
c3800 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
c3810 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
c3820 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f       if( flag_lo
c3830 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20  nglong ){.      
c3840 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20        longvalue 
c3850 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 36 34 29  = va_arg(ap,u64)
c3860 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
c3870 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20  e if( flag_long 
c3880 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c  ){.            l
c3890 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72  ongvalue = va_ar
c38a0 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 6c 6f  g(ap,unsigned lo
c38b0 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20  ng int);.       
c38c0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
c38d0 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20        longvalue 
c38e0 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 69  = va_arg(ap,unsi
c38f0 67 6e 65 64 20 69 6e 74 29 3b 0a 20 20 20 20 20  gned int);.     
c3900 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
c3910 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20   prefix = 0;.   
c3920 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
c3930 66 28 20 6c 6f 6e 67 76 61 6c 75 65 3d 3d 30 20  f( longvalue==0 
c3940 29 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65  ) flag_alternate
c3950 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20  form = 0;.      
c3960 20 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70    if( flag_zerop
c3970 61 64 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c  ad && precision<
c3980 77 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30  width-(prefix!=0
c3990 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
c39a0 72 65 63 69 73 69 6f 6e 20 3d 20 77 69 64 74 68  recision = width
c39b0 2d 28 70 72 65 66 69 78 21 3d 30 29 3b 0a 20 20  -(prefix!=0);.  
c39c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
c39d0 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 65 74  if( precision<et
c39e0 42 55 46 53 49 5a 45 2d 31 30 20 29 7b 0a 20 20  BUFSIZE-10 ){.  
c39f0 20 20 20 20 20 20 20 20 6e 4f 75 74 20 3d 20 65          nOut = e
c3a00 74 42 55 46 53 49 5a 45 3b 0a 20 20 20 20 20 20  tBUFSIZE;.      
c3a10 20 20 20 20 7a 4f 75 74 20 3d 20 62 75 66 3b 0a      zOut = buf;.
c3a20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
c3a30 20 20 20 20 20 20 20 20 20 6e 4f 75 74 20 3d 20           nOut = 
c3a40 70 72 65 63 69 73 69 6f 6e 20 2b 20 31 30 3b 0a  precision + 10;.
c3a50 20 20 20 20 20 20 20 20 20 20 7a 4f 75 74 20 3d            zOut =
c3a60 20 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 74 65   zExtra = sqlite
c3a70 33 4d 61 6c 6c 6f 63 28 20 6e 4f 75 74 20 29 3b  3Malloc( nOut );
c3a80 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a  .          if( z
c3a90 4f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Out==0 ){.      
c3aa0 20 20 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 61        pAccum->ma
c3ab0 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
c3ac0 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
c3ad0 72 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  rn;.          }.
c3ae0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
c3af0 20 20 62 75 66 70 74 20 3d 20 26 7a 4f 75 74 5b    bufpt = &zOut[
c3b00 6e 4f 75 74 2d 31 5d 3b 0a 20 20 20 20 20 20 20  nOut-1];.       
c3b10 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 4f 52   if( xtype==etOR
c3b20 44 49 4e 41 4c 20 29 7b 0a 20 20 20 20 20 20 20  DINAL ){.       
c3b30 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
c3b40 63 68 61 72 20 7a 4f 72 64 5b 5d 20 3d 20 22 74  char zOrd[] = "t
c3b50 68 73 74 6e 64 72 64 22 3b 0a 20 20 20 20 20 20  hstndrd";.      
c3b60 20 20 20 20 69 6e 74 20 78 20 3d 20 28 69 6e 74      int x = (int
c3b70 29 28 6c 6f 6e 67 76 61 6c 75 65 20 25 20 31 30  )(longvalue % 10
c3b80 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
c3b90 20 78 3e 3d 34 20 7c 7c 20 28 6c 6f 6e 67 76 61   x>=4 || (longva
c3ba0 6c 75 65 2f 31 30 29 25 31 30 3d 3d 31 20 29 7b  lue/10)%10==1 ){
c3bb0 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 20 3d  .            x =
c3bc0 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   0;.          }.
c3bd0 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75            *(--bu
c3be0 66 70 74 29 20 3d 20 7a 4f 72 64 5b 78 2a 32 2b  fpt) = zOrd[x*2+
c3bf0 31 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 28  1];.          *(
c3c00 2d 2d 62 75 66 70 74 29 20 3d 20 7a 4f 72 64 5b  --bufpt) = zOrd[
c3c10 78 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  x*2];.        }.
c3c20 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20          {.      
c3c30 20 20 20 20 72 65 67 69 73 74 65 72 20 63 6f 6e      register con
c3c40 73 74 20 63 68 61 72 20 2a 63 73 65 74 3b 20 20  st char *cset;  
c3c50 20 20 20 20 2f 2a 20 55 73 65 20 72 65 67 69 73      /* Use regis
c3c60 74 65 72 73 20 66 6f 72 20 73 70 65 65 64 20 2a  ters for speed *
c3c70 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 69  /.          regi
c3c80 73 74 65 72 20 69 6e 74 20 62 61 73 65 3b 0a 20  ster int base;. 
c3c90 20 20 20 20 20 20 20 20 20 63 73 65 74 20 3d 20           cset = 
c3ca0 26 61 44 69 67 69 74 73 5b 69 6e 66 6f 70 2d 3e  &aDigits[infop->
c3cb0 63 68 61 72 73 65 74 5d 3b 0a 20 20 20 20 20 20  charset];.      
c3cc0 20 20 20 20 62 61 73 65 20 3d 20 69 6e 66 6f 70      base = infop
c3cd0 2d 3e 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20  ->base;.        
c3ce0 20 20 64 6f 7b 20 20 20 20 20 20 20 20 20 20 20    do{           
c3cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3d10 2f 2a 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 73  /* Convert to as
c3d20 63 69 69 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  cii */.         
c3d30 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20     *(--bufpt) = 
c3d40 63 73 65 74 5b 6c 6f 6e 67 76 61 6c 75 65 25 62  cset[longvalue%b
c3d50 61 73 65 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  ase];.          
c3d60 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 6c 6f    longvalue = lo
c3d70 6e 67 76 61 6c 75 65 2f 62 61 73 65 3b 0a 20 20  ngvalue/base;.  
c3d80 20 20 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20          }while( 
c3d90 6c 6f 6e 67 76 61 6c 75 65 3e 30 20 29 3b 0a 20  longvalue>0 );. 
c3da0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
c3db0 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e 74 29 28   length = (int)(
c3dc0 26 7a 4f 75 74 5b 6e 4f 75 74 2d 31 5d 2d 62 75  &zOut[nOut-1]-bu
c3dd0 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 66 6f  fpt);.        fo
c3de0 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2d  r(idx=precision-
c3df0 6c 65 6e 67 74 68 3b 20 69 64 78 3e 30 3b 20 69  length; idx>0; i
c3e00 64 78 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20  dx--){.         
c3e10 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 27 30   *(--bufpt) = '0
c3e20 27 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ';              
c3e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c3e40 2a 20 5a 65 72 6f 20 70 61 64 20 2a 2f 0a 20 20  * Zero pad */.  
c3e50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
c3e60 69 66 28 20 70 72 65 66 69 78 20 29 20 2a 28 2d  if( prefix ) *(-
c3e70 2d 62 75 66 70 74 29 20 3d 20 70 72 65 66 69 78  -bufpt) = prefix
c3e80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
c3e90 2f 2a 20 41 64 64 20 73 69 67 6e 20 2a 2f 0a 20  /* Add sign */. 
c3ea0 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f         if( flag_
c3eb0 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 26 26  alternateform &&
c3ec0 20 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78 20 29   infop->prefix )
c3ed0 7b 20 20 20 20 20 20 2f 2a 20 41 64 64 20 22 30  {      /* Add "0
c3ee0 22 20 6f 72 20 22 30 78 22 20 2a 2f 0a 20 20 20  " or "0x" */.   
c3ef0 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
c3f00 72 20 2a 70 72 65 3b 0a 20 20 20 20 20 20 20 20  r *pre;.        
c3f10 20 20 63 68 61 72 20 78 3b 0a 20 20 20 20 20 20    char x;.      
c3f20 20 20 20 20 70 72 65 20 3d 20 26 61 50 72 65 66      pre = &aPref
c3f30 69 78 5b 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78  ix[infop->prefix
c3f40 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  ];.          for
c3f50 28 3b 20 28 78 3d 28 2a 70 72 65 29 29 21 3d 30  (; (x=(*pre))!=0
c3f60 3b 20 70 72 65 2b 2b 29 20 2a 28 2d 2d 62 75 66  ; pre++) *(--buf
c3f70 70 74 29 20 3d 20 78 3b 0a 20 20 20 20 20 20 20  pt) = x;.       
c3f80 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74   }.        lengt
c3f90 68 20 3d 20 28 69 6e 74 29 28 26 7a 4f 75 74 5b  h = (int)(&zOut[
c3fa0 6e 4f 75 74 2d 31 5d 2d 62 75 66 70 74 29 3b 0a  nOut-1]-bufpt);.
c3fb0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
c3fc0 20 20 20 20 20 63 61 73 65 20 65 74 46 4c 4f 41       case etFLOA
c3fd0 54 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  T:.      case et
c3fe0 45 58 50 3a 0a 20 20 20 20 20 20 63 61 73 65 20  EXP:.      case 
c3ff0 65 74 47 45 4e 45 52 49 43 3a 0a 20 20 20 20 20  etGENERIC:.     
c4000 20 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 76     realvalue = v
c4010 61 5f 61 72 67 28 61 70 2c 64 6f 75 62 6c 65 29  a_arg(ap,double)
c4020 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
c4030 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
c4040 49 4e 54 0a 20 20 20 20 20 20 20 20 6c 65 6e 67  INT.        leng
c4050 74 68 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 20 20  th = 0;.#else.  
c4060 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73        if( precis
c4070 69 6f 6e 3c 30 20 29 20 70 72 65 63 69 73 69 6f  ion<0 ) precisio
c4080 6e 20 3d 20 36 3b 20 20 20 20 20 20 20 20 20 2f  n = 6;         /
c4090 2a 20 53 65 74 20 64 65 66 61 75 6c 74 20 70 72  * Set default pr
c40a0 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  ecision */.     
c40b0 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65     if( realvalue
c40c0 3c 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  <0.0 ){.        
c40d0 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 2d 72    realvalue = -r
c40e0 65 61 6c 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  ealvalue;.      
c40f0 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27      prefix = '-'
c4100 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
c4110 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66  .          if( f
c4120 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 29 20 20  lag_plussign )  
c4130 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d          prefix =
c4140 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20   '+';.          
c4150 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 62 6c  else if( flag_bl
c4160 61 6e 6b 73 69 67 6e 20 29 20 20 20 20 70 72 65  anksign )    pre
c4170 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 20 20  fix = ' ';.     
c4180 20 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20       else       
c4190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c41a0 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20    prefix = 0;.  
c41b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
c41c0 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e  if( xtype==etGEN
c41d0 45 52 49 43 20 26 26 20 70 72 65 63 69 73 69 6f  ERIC && precisio
c41e0 6e 3e 30 20 29 20 70 72 65 63 69 73 69 6f 6e 2d  n>0 ) precision-
c41f0 2d 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 20  -;.#if 0.       
c4200 20 2f 2a 20 52 6f 75 6e 64 69 6e 67 20 77 6f 72   /* Rounding wor
c4210 6b 73 20 6c 69 6b 65 20 42 53 44 20 77 68 65 6e  ks like BSD when
c4220 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 30 2e   the constant 0.
c4230 34 39 39 39 20 69 73 20 75 73 65 64 2e 20 20 57  4999 is used.  W
c4240 69 65 72 64 21 20 2a 2f 0a 20 20 20 20 20 20 20  ierd! */.       
c4250 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69   for(idx=precisi
c4260 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 34 39  on, rounder=0.49
c4270 39 39 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d  99; idx>0; idx--
c4280 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 3b  , rounder*=0.1);
c4290 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 2f  .#else.        /
c42a0 2a 20 49 74 20 6d 61 6b 65 73 20 6d 6f 72 65 20  * It makes more 
c42b0 73 65 6e 73 65 20 74 6f 20 75 73 65 20 30 2e 35  sense to use 0.5
c42c0 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28   */.        for(
c42d0 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72  idx=precision, r
c42e0 6f 75 6e 64 65 72 3d 30 2e 35 3b 20 69 64 78 3e  ounder=0.5; idx>
c42f0 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65  0; idx--, rounde
c4300 72 2a 3d 30 2e 31 29 7b 7d 0a 23 65 6e 64 69 66  r*=0.1){}.#endif
c4310 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79  .        if( xty
c4320 70 65 3d 3d 65 74 46 4c 4f 41 54 20 29 20 72 65  pe==etFLOAT ) re
c4330 61 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64  alvalue += round
c4340 65 72 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e  er;.        /* N
c4350 6f 72 6d 61 6c 69 7a 65 20 72 65 61 6c 76 61 6c  ormalize realval
c4360 75 65 20 74 6f 20 77 69 74 68 69 6e 20 31 30 2e  ue to within 10.
c4370 30 20 3e 20 72 65 61 6c 76 61 6c 75 65 20 3e 3d  0 > realvalue >=
c4380 20 31 2e 30 20 2a 2f 0a 20 20 20 20 20 20 20 20   1.0 */.        
c4390 65 78 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  exp = 0;.       
c43a0 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61   if( sqlite3IsNa
c43b0 4e 28 28 64 6f 75 62 6c 65 29 72 65 61 6c 76 61  N((double)realva
c43c0 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  lue) ){.        
c43d0 20 20 62 75 66 70 74 20 3d 20 22 4e 61 4e 22 3b    bufpt = "NaN";
c43e0 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74  .          lengt
c43f0 68 20 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20  h = 3;.         
c4400 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
c4410 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 65  }.        if( re
c4420 61 6c 76 61 6c 75 65 3e 30 2e 30 20 29 7b 0a 20  alvalue>0.0 ){. 
c4430 20 20 20 20 20 20 20 20 20 4c 4f 4e 47 44 4f 55           LONGDOU
c4440 42 4c 45 5f 54 59 50 45 20 73 63 61 6c 65 20 3d  BLE_TYPE scale =
c4450 20 31 2e 30 3b 0a 20 20 20 20 20 20 20 20 20 20   1.0;.          
c4460 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65  while( realvalue
c4470 3e 3d 31 65 31 30 30 2a 73 63 61 6c 65 20 26 26  >=1e100*scale &&
c4480 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 73 63 61   exp<=350 ){ sca
c4490 6c 65 20 2a 3d 20 31 65 31 30 30 3b 65 78 70 2b  le *= 1e100;exp+
c44a0 3d 31 30 30 3b 7d 0a 20 20 20 20 20 20 20 20 20  =100;}.         
c44b0 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75   while( realvalu
c44c0 65 3e 3d 31 65 36 34 2a 73 63 61 6c 65 20 26 26  e>=1e64*scale &&
c44d0 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 73 63 61   exp<=350 ){ sca
c44e0 6c 65 20 2a 3d 20 31 65 36 34 3b 20 65 78 70 2b  le *= 1e64; exp+
c44f0 3d 36 34 3b 20 7d 0a 20 20 20 20 20 20 20 20 20  =64; }.         
c4500 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75   while( realvalu
c4510 65 3e 3d 31 65 38 2a 73 63 61 6c 65 20 26 26 20  e>=1e8*scale && 
c4520 65 78 70 3c 3d 33 35 30 20 29 7b 20 73 63 61 6c  exp<=350 ){ scal
c4530 65 20 2a 3d 20 31 65 38 3b 20 65 78 70 2b 3d 38  e *= 1e8; exp+=8
c4540 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68  ; }.          wh
c4550 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d  ile( realvalue>=
c4560 31 30 2e 30 2a 73 63 61 6c 65 20 26 26 20 65 78  10.0*scale && ex
c4570 70 3c 3d 33 35 30 20 29 7b 20 73 63 61 6c 65 20  p<=350 ){ scale 
c4580 2a 3d 20 31 30 2e 30 3b 20 65 78 70 2b 2b 3b 20  *= 10.0; exp++; 
c4590 7d 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c  }.          real
c45a0 76 61 6c 75 65 20 2f 3d 20 73 63 61 6c 65 3b 0a  value /= scale;.
c45b0 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28            while(
c45c0 20 72 65 61 6c 76 61 6c 75 65 3c 31 65 2d 38 20   realvalue<1e-8 
c45d0 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20  ){ realvalue *= 
c45e0 31 65 38 3b 20 65 78 70 2d 3d 38 3b 20 7d 0a 20  1e8; exp-=8; }. 
c45f0 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
c4600 72 65 61 6c 76 61 6c 75 65 3c 31 2e 30 20 29 7b  realvalue<1.0 ){
c4610 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 30   realvalue *= 10
c4620 2e 30 3b 20 65 78 70 2d 2d 3b 20 7d 0a 20 20 20  .0; exp--; }.   
c4630 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 33         if( exp>3
c4640 35 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  50 ){.          
c4650 20 20 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2d    if( prefix=='-
c4660 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ' ){.           
c4670 20 20 20 62 75 66 70 74 20 3d 20 22 2d 49 6e 66     bufpt = "-Inf
c4680 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  ";.            }
c4690 65 6c 73 65 20 69 66 28 20 70 72 65 66 69 78 3d  else if( prefix=
c46a0 3d 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='+' ){.        
c46b0 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 2b        bufpt = "+
c46c0 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20  Inf";.          
c46d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
c46e0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22         bufpt = "
c46f0 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20  Inf";.          
c4700 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
c4710 6c 65 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33  length = sqlite3
c4720 53 74 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b  Strlen30(bufpt);
c4730 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
c4740 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
c4750 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
c4760 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20    bufpt = buf;. 
c4770 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20         /*.      
c4780 20 20 2a 2a 20 49 66 20 74 68 65 20 66 69 65 6c    ** If the fiel
c4790 64 20 74 79 70 65 20 69 73 20 65 74 47 45 4e 45  d type is etGENE
c47a0 52 49 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 72  RIC, then conver
c47b0 74 20 74 6f 20 65 69 74 68 65 72 20 65 74 45 58  t to either etEX
c47c0 50 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20  P.        ** or 
c47d0 65 74 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 72  etFLOAT, as appr
c47e0 6f 70 72 69 61 74 65 2e 0a 20 20 20 20 20 20 20  opriate..       
c47f0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
c4800 78 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 20 29  xtype!=etFLOAT )
c4810 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c  {.          real
c4820 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72  value += rounder
c4830 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
c4840 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20  realvalue>=10.0 
c4850 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20  ){ realvalue *= 
c4860 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20  0.1; exp++; }.  
c4870 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
c4880 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e  if( xtype==etGEN
c4890 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20  ERIC ){.        
c48a0 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 66 6c    flag_rtz = !fl
c48b0 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d  ag_alternateform
c48c0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
c48d0 65 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 72  exp<-4 || exp>pr
c48e0 65 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 20 20  ecision ){.     
c48f0 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65         xtype = e
c4900 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 20 20  tEXP;.          
c4910 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
c4920 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70     precision = p
c4930 72 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b 0a  recision - exp;.
c4940 20 20 20 20 20 20 20 20 20 20 20 20 78 74 79 70              xtyp
c4950 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 20 20  e = etFLOAT;.   
c4960 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
c4970 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
c4980 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 66 6c 61    flag_rtz = fla
c4990 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 20 20  g_altform2;.    
c49a0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
c49b0 28 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29  ( xtype==etEXP )
c49c0 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d  {.          e2 =
c49d0 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   0;.        }els
c49e0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20  e{.          e2 
c49f0 3d 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 7d  = exp;.        }
c4a00 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 32 2b  .        if( e2+
c4a10 70 72 65 63 69 73 69 6f 6e 2b 77 69 64 74 68 20  precision+width 
c4a20 3e 20 65 74 42 55 46 53 49 5a 45 20 2d 20 31 35  > etBUFSIZE - 15
c4a30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75   ){.          bu
c4a40 66 70 74 20 3d 20 7a 45 78 74 72 61 20 3d 20 73  fpt = zExtra = s
c4a50 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 65 32  qlite3Malloc( e2
c4a60 2b 70 72 65 63 69 73 69 6f 6e 2b 77 69 64 74 68  +precision+width
c4a70 2b 31 35 20 29 3b 0a 20 20 20 20 20 20 20 20 20  +15 );.         
c4a80 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20 29 7b   if( bufpt==0 ){
c4a90 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 41 63  .            pAc
c4aa0 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  cum->mallocFaile
c4ab0 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  d = 1;.         
c4ac0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
c4ad0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
c4ae0 0a 20 20 20 20 20 20 20 20 7a 4f 75 74 20 3d 20  .        zOut = 
c4af0 62 75 66 70 74 3b 0a 20 20 20 20 20 20 20 20 6e  bufpt;.        n
c4b00 73 64 20 3d 20 31 36 20 2b 20 66 6c 61 67 5f 61  sd = 16 + flag_a
c4b10 6c 74 66 6f 72 6d 32 2a 31 30 3b 0a 20 20 20 20  ltform2*10;.    
c4b20 20 20 20 20 66 6c 61 67 5f 64 70 20 3d 20 28 70      flag_dp = (p
c4b30 72 65 63 69 73 69 6f 6e 3e 30 20 3f 31 3a 30 29  recision>0 ?1:0)
c4b40 20 7c 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74   | flag_alternat
c4b50 65 66 6f 72 6d 20 7c 20 66 6c 61 67 5f 61 6c 74  eform | flag_alt
c4b60 66 6f 72 6d 32 3b 0a 20 20 20 20 20 20 20 20 2f  form2;.        /
c4b70 2a 20 54 68 65 20 73 69 67 6e 20 69 6e 20 66 72  * The sign in fr
c4b80 6f 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ont of the numbe
c4b90 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  r */.        if(
c4ba0 20 70 72 65 66 69 78 20 29 7b 0a 20 20 20 20 20   prefix ){.     
c4bb0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
c4bc0 3d 20 70 72 65 66 69 78 3b 0a 20 20 20 20 20 20  = prefix;.      
c4bd0 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 44    }.        /* D
c4be0 69 67 69 74 73 20 70 72 69 6f 72 20 74 6f 20 74  igits prior to t
c4bf0 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74  he decimal point
c4c00 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
c4c10 65 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e2<0 ){.        
c4c20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27    *(bufpt++) = '
c4c30 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  0';.        }els
c4c40 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  e{.          for
c4c50 28 3b 20 65 32 3e 3d 30 3b 20 65 32 2d 2d 29 7b  (; e2>=0; e2--){
c4c60 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62  .            *(b
c4c70 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74  ufpt++) = et_get
c4c80 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65  digit(&realvalue
c4c90 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20  ,&nsd);.        
c4ca0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
c4cb0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 63        /* The dec
c4cc0 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20  imal point */.  
c4cd0 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 64        if( flag_d
c4ce0 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a  p ){.          *
c4cf0 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2e 27 3b  (bufpt++) = '.';
c4d00 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
c4d10 20 20 20 2f 2a 20 22 30 22 20 64 69 67 69 74 73     /* "0" digits
c4d20 20 61 66 74 65 72 20 74 68 65 20 64 65 63 69 6d   after the decim
c4d30 61 6c 20 70 6f 69 6e 74 20 62 75 74 20 62 65 66  al point but bef
c4d40 6f 72 65 20 74 68 65 20 66 69 72 73 74 0a 20 20  ore the first.  
c4d50 20 20 20 20 20 20 2a 2a 20 73 69 67 6e 69 66 69        ** signifi
c4d60 63 61 6e 74 20 64 69 67 69 74 20 6f 66 20 74 68  cant digit of th
c4d70 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  e number */.    
c4d80 20 20 20 20 66 6f 72 28 65 32 2b 2b 3b 20 65 32      for(e2++; e2
c4d90 3c 30 3b 20 70 72 65 63 69 73 69 6f 6e 2d 2d 2c  <0; precision--,
c4da0 20 65 32 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20   e2++){.        
c4db0 20 20 61 73 73 65 72 74 28 20 70 72 65 63 69 73    assert( precis
c4dc0 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 20  ion>0 );.       
c4dd0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
c4de0 27 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  '0';.        }. 
c4df0 20 20 20 20 20 20 20 2f 2a 20 53 69 67 6e 69 66         /* Signif
c4e00 69 63 61 6e 74 20 64 69 67 69 74 73 20 61 66 74  icant digits aft
c4e10 65 72 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70  er the decimal p
c4e20 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20  oint */.        
c4e30 77 68 69 6c 65 28 20 28 70 72 65 63 69 73 69 6f  while( (precisio
c4e40 6e 2d 2d 29 3e 30 20 29 7b 0a 20 20 20 20 20 20  n--)>0 ){.      
c4e50 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
c4e60 20 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65   et_getdigit(&re
c4e70 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20  alvalue,&nsd);. 
c4e80 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
c4e90 20 2f 2a 20 52 65 6d 6f 76 65 20 74 72 61 69 6c   /* Remove trail
c4ea0 69 6e 67 20 7a 65 72 6f 73 20 61 6e 64 20 74 68  ing zeros and th
c4eb0 65 20 22 2e 22 20 69 66 20 6e 6f 20 64 69 67 69  e "." if no digi
c4ec0 74 73 20 66 6f 6c 6c 6f 77 20 74 68 65 20 22 2e  ts follow the ".
c4ed0 22 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  " */.        if(
c4ee0 20 66 6c 61 67 5f 72 74 7a 20 26 26 20 66 6c 61   flag_rtz && fla
c4ef0 67 5f 64 70 20 29 7b 0a 20 20 20 20 20 20 20 20  g_dp ){.        
c4f00 20 20 77 68 69 6c 65 28 20 62 75 66 70 74 5b 2d    while( bufpt[-
c4f10 31 5d 3d 3d 27 30 27 20 29 20 2a 28 2d 2d 62 75  1]=='0' ) *(--bu
c4f20 66 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20  fpt) = 0;.      
c4f30 20 20 20 20 61 73 73 65 72 74 28 20 62 75 66 70      assert( bufp
c4f40 74 3e 7a 4f 75 74 20 29 3b 0a 20 20 20 20 20 20  t>zOut );.      
c4f50 20 20 20 20 69 66 28 20 62 75 66 70 74 5b 2d 31      if( bufpt[-1
c4f60 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20  ]=='.' ){.      
c4f70 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 61        if( flag_a
c4f80 6c 74 66 6f 72 6d 32 20 29 7b 0a 20 20 20 20 20  ltform2 ){.     
c4f90 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74           *(bufpt
c4fa0 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20  ++) = '0';.     
c4fb0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
c4fc0 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d              *(--
c4fd0 62 75 66 70 74 29 20 3d 20 30 3b 0a 20 20 20 20  bufpt) = 0;.    
c4fe0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
c4ff0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
c5000 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74          /* Add t
c5010 68 65 20 22 65 4e 4e 4e 22 20 73 75 66 66 69 78  he "eNNN" suffix
c5020 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
c5030 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b 0a  xtype==etEXP ){.
c5040 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
c5050 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 5b 69  t++) = aDigits[i
c5060 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a  nfop->charset];.
c5070 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78            if( ex
c5080 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  p<0 ){.         
c5090 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
c50a0 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 70 3b  '-'; exp = -exp;
c50b0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
c50c0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28  {.            *(
c50d0 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 3b 0a  bufpt++) = '+';.
c50e0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
c50f0 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 3d 31        if( exp>=1
c5100 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  00 ){.          
c5110 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28    *(bufpt++) = (
c5120 63 68 61 72 29 28 28 65 78 70 2f 31 30 30 29 2b  char)((exp/100)+
c5130 27 30 27 29 3b 20 20 20 20 20 20 20 20 2f 2a 20  '0');        /* 
c5140 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20  100's digit */. 
c5150 20 20 20 20 20 20 20 20 20 20 20 65 78 70 20 25             exp %
c5160 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 20 20  = 100;.         
c5170 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62   }.          *(b
c5180 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29  ufpt++) = (char)
c5190 28 65 78 70 2f 31 30 2b 27 30 27 29 3b 20 20 20  (exp/10+'0');   
c51a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 27            /* 10'
c51b0 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20  s digit */.     
c51c0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
c51d0 3d 20 28 63 68 61 72 29 28 65 78 70 25 31 30 2b  = (char)(exp%10+
c51e0 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 20 20  '0');           
c51f0 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 20 2a    /* 1's digit *
c5200 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  /.        }.    
c5210 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 3b 0a      *bufpt = 0;.
c5220 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
c5230 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 65 72  converted number
c5240 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 6e 64   is in buf[] and
c5250 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64   zero terminated
c5260 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 20 20  . Output it..   
c5270 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61       ** Note tha
c5280 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20  t the number is 
c5290 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f 72 64  in the usual ord
c52a0 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 65 64  er, not reversed
c52b0 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 20 20   as with.       
c52c0 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f 6e 76   ** integer conv
c52d0 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20  ersions. */.    
c52e0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e      length = (in
c52f0 74 29 28 62 75 66 70 74 2d 7a 4f 75 74 29 3b 0a  t)(bufpt-zOut);.
c5300 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
c5310 7a 4f 75 74 3b 0a 0a 20 20 20 20 20 20 20 20 2f  zOut;..        /
c5320 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20  * Special case: 
c5330 20 41 64 64 20 6c 65 61 64 69 6e 67 20 7a 65 72   Add leading zer
c5340 6f 73 20 69 66 20 74 68 65 20 66 6c 61 67 5f 7a  os if the flag_z
c5350 65 72 6f 70 61 64 20 66 6c 61 67 20 69 73 0a 20  eropad flag is. 
c5360 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 61 6e         ** set an
c5370 64 20 77 65 20 61 72 65 20 6e 6f 74 20 6c 65 66  d we are not lef
c5380 74 20 6a 75 73 74 69 66 69 65 64 20 2a 2f 0a 20  t justified */. 
c5390 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f         if( flag_
c53a0 7a 65 72 6f 70 61 64 20 26 26 20 21 66 6c 61 67  zeropad && !flag
c53b0 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 26 26 20  _leftjustify && 
c53c0 6c 65 6e 67 74 68 20 3c 20 77 69 64 74 68 29 7b  length < width){
c53d0 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
c53e0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  ;.          int 
c53f0 6e 50 61 64 20 3d 20 77 69 64 74 68 20 2d 20 6c  nPad = width - l
c5400 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 20 20 20  ength;.         
c5410 20 66 6f 72 28 69 3d 77 69 64 74 68 3b 20 69 3e   for(i=width; i>
c5420 3d 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 20 20  =nPad; i--){.   
c5430 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 69           bufpt[i
c5440 5d 20 3d 20 62 75 66 70 74 5b 69 2d 6e 50 61 64  ] = bufpt[i-nPad
c5450 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  ];.          }. 
c5460 20 20 20 20 20 20 20 20 20 69 20 3d 20 70 72 65           i = pre
c5470 66 69 78 21 3d 30 3b 0a 20 20 20 20 20 20 20 20  fix!=0;.        
c5480 20 20 77 68 69 6c 65 28 20 6e 50 61 64 2d 2d 20    while( nPad-- 
c5490 29 20 62 75 66 70 74 5b 69 2b 2b 5d 20 3d 20 27  ) bufpt[i++] = '
c54a0 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65  0';.          le
c54b0 6e 67 74 68 20 3d 20 77 69 64 74 68 3b 0a 20 20  ngth = width;.  
c54c0 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f        }.#endif /
c54d0 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
c54e0 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
c54f0 50 4f 49 4e 54 29 20 2a 2f 0a 20 20 20 20 20 20  POINT) */.      
c5500 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
c5510 61 73 65 20 65 74 53 49 5a 45 3a 0a 20 20 20 20  ase etSIZE:.    
c5520 20 20 20 20 2a 28 76 61 5f 61 72 67 28 61 70 2c      *(va_arg(ap,
c5530 69 6e 74 2a 29 29 20 3d 20 70 41 63 63 75 6d 2d  int*)) = pAccum-
c5540 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20 20 20 20  >nChar;.        
c5550 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d  length = width =
c5560 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   0;.        brea
c5570 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  k;.      case et
c5580 50 45 52 43 45 4e 54 3a 0a 20 20 20 20 20 20 20  PERCENT:.       
c5590 20 62 75 66 5b 30 5d 20 3d 20 27 25 27 3b 0a 20   buf[0] = '%';. 
c55a0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62         bufpt = b
c55b0 75 66 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67  uf;.        leng
c55c0 74 68 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  th = 1;.        
c55d0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
c55e0 65 20 65 74 43 48 41 52 58 3a 0a 20 20 20 20 20  e etCHARX:.     
c55f0 20 20 20 63 20 3d 20 76 61 5f 61 72 67 28 61 70     c = va_arg(ap
c5600 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 62  ,int);.        b
c5610 75 66 5b 30 5d 20 3d 20 28 63 68 61 72 29 63 3b  uf[0] = (char)c;
c5620 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65  .        if( pre
c5630 63 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20  cision>=0 ){.   
c5640 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 31         for(idx=1
c5650 3b 20 69 64 78 3c 70 72 65 63 69 73 69 6f 6e 3b  ; idx<precision;
c5660 20 69 64 78 2b 2b 29 20 62 75 66 5b 69 64 78 5d   idx++) buf[idx]
c5670 20 3d 20 28 63 68 61 72 29 63 3b 0a 20 20 20 20   = (char)c;.    
c5680 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 70        length = p
c5690 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20  recision;.      
c56a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
c56b0 20 20 20 6c 65 6e 67 74 68 20 3d 31 3b 0a 20 20     length =1;.  
c56c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
c56d0 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20  bufpt = buf;.   
c56e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
c56f0 20 20 63 61 73 65 20 65 74 53 54 52 49 4e 47 3a    case etSTRING:
c5700 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 44 59  .      case etDY
c5710 4e 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 20  NSTRING:.       
c5720 20 62 75 66 70 74 20 3d 20 76 61 5f 61 72 67 28   bufpt = va_arg(
c5730 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20 20 20 20  ap,char*);.     
c5740 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20     if( bufpt==0 
c5750 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66  ){.          buf
c5760 70 74 20 3d 20 22 22 3b 0a 20 20 20 20 20 20 20  pt = "";.       
c5770 20 7d 65 6c 73 65 20 69 66 28 20 78 74 79 70 65   }else if( xtype
c5780 3d 3d 65 74 44 59 4e 53 54 52 49 4e 47 20 29 7b  ==etDYNSTRING ){
c5790 0a 20 20 20 20 20 20 20 20 20 20 7a 45 78 74 72  .          zExtr
c57a0 61 20 3d 20 62 75 66 70 74 3b 0a 20 20 20 20 20  a = bufpt;.     
c57b0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
c57c0 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b   precision>=0 ){
c57d0 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6c  .          for(l
c57e0 65 6e 67 74 68 3d 30 3b 20 6c 65 6e 67 74 68 3c  ength=0; length<
c57f0 70 72 65 63 69 73 69 6f 6e 20 26 26 20 62 75 66  precision && buf
c5800 70 74 5b 6c 65 6e 67 74 68 5d 3b 20 6c 65 6e 67  pt[length]; leng
c5810 74 68 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20  th++){}.        
c5820 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
c5830 20 6c 65 6e 67 74 68 20 3d 20 73 71 6c 69 74 65   length = sqlite
c5840 33 53 74 72 6c 65 6e 33 30 28 62 75 66 70 74 29  3Strlen30(bufpt)
c5850 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
c5860 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
c5870 20 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50   case etSQLESCAP
c5880 45 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  E:.      case et
c5890 53 51 4c 45 53 43 41 50 45 32 3a 0a 20 20 20 20  SQLESCAPE2:.    
c58a0 20 20 63 61 73 65 20 65 74 53 51 4c 45 53 43 41    case etSQLESCA
c58b0 50 45 33 3a 20 7b 0a 20 20 20 20 20 20 20 20 69  PE3: {.        i
c58c0 6e 74 20 69 2c 20 6a 2c 20 6b 2c 20 6e 2c 20 69  nt i, j, k, n, i
c58d0 73 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69  snull;.        i
c58e0 6e 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20  nt needQuote;.  
c58f0 20 20 20 20 20 20 63 68 61 72 20 63 68 3b 0a 20        char ch;. 
c5900 20 20 20 20 20 20 20 63 68 61 72 20 71 20 3d 20         char q = 
c5910 28 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53  ((xtype==etSQLES
c5920 43 41 50 45 33 29 3f 27 22 27 3a 27 5c 27 27 29  CAPE3)?'"':'\'')
c5930 3b 20 20 20 2f 2a 20 51 75 6f 74 65 20 63 68 61  ;   /* Quote cha
c5940 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20  racter */.      
c5950 20 20 63 68 61 72 20 2a 65 73 63 61 72 67 20 3d    char *escarg =
c5960 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a   va_arg(ap,char*
c5970 29 3b 0a 20 20 20 20 20 20 20 20 69 73 6e 75 6c  );.        isnul
c5980 6c 20 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20  l = escarg==0;. 
c5990 20 20 20 20 20 20 20 69 66 28 20 69 73 6e 75 6c         if( isnul
c59a0 6c 20 29 20 65 73 63 61 72 67 20 3d 20 28 78 74  l ) escarg = (xt
c59b0 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45  ype==etSQLESCAPE
c59c0 32 20 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e  2 ? "NULL" : "(N
c59d0 55 4c 4c 29 22 29 3b 0a 20 20 20 20 20 20 20 20  ULL)");.        
c59e0 6b 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20  k = precision;. 
c59f0 20 20 20 20 20 20 20 66 6f 72 28 69 3d 6e 3d 30         for(i=n=0
c5a00 3b 20 6b 21 3d 30 20 26 26 20 28 63 68 3d 65 73  ; k!=0 && (ch=es
c5a10 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b  carg[i])!=0; i++
c5a20 2c 20 6b 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20  , k--){.        
c5a30 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 20 6e    if( ch==q )  n
c5a40 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ++;.        }.  
c5a50 20 20 20 20 20 20 6e 65 65 64 51 75 6f 74 65 20        needQuote 
c5a60 3d 20 21 69 73 6e 75 6c 6c 20 26 26 20 78 74 79  = !isnull && xty
c5a70 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 32  pe==etSQLESCAPE2
c5a80 3b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 69  ;.        n += i
c5a90 20 2b 20 31 20 2b 20 6e 65 65 64 51 75 6f 74 65   + 1 + needQuote
c5aa0 2a 32 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  *2;.        if( 
c5ab0 6e 3e 65 74 42 55 46 53 49 5a 45 20 29 7b 0a 20  n>etBUFSIZE ){. 
c5ac0 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d           bufpt =
c5ad0 20 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 74 65   zExtra = sqlite
c5ae0 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20  3Malloc( n );.  
c5af0 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70          if( bufp
c5b00 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  t==0 ){.        
c5b10 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c      pAccum->mall
c5b20 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
c5b30 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
c5b40 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
c5b50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
c5b60 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62         bufpt = b
c5b70 75 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  uf;.        }.  
c5b80 20 20 20 20 20 20 6a 20 3d 20 30 3b 0a 20 20 20        j = 0;.   
c5b90 20 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f       if( needQuo
c5ba0 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20  te ) bufpt[j++] 
c5bb0 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 6b 20 3d  = q;.        k =
c5bc0 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   i;.        for(
c5bd0 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a  i=0; i<k; i++){.
c5be0 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b            bufpt[
c5bf0 6a 2b 2b 5d 20 3d 20 63 68 20 3d 20 65 73 63 61  j++] = ch = esca
c5c00 72 67 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20  rg[i];.         
c5c10 20 69 66 28 20 63 68 3d 3d 71 20 29 20 62 75 66   if( ch==q ) buf
c5c20 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20  pt[j++] = ch;.  
c5c30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
c5c40 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20  if( needQuote ) 
c5c50 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a  bufpt[j++] = q;.
c5c60 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a 5d          bufpt[j]
c5c70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 6c 65   = 0;.        le
c5c80 6e 67 74 68 20 3d 20 6a 3b 0a 20 20 20 20 20 20  ngth = j;.      
c5c90 20 20 2f 2a 20 54 68 65 20 70 72 65 63 69 73 69    /* The precisi
c5ca0 6f 6e 20 69 6e 20 25 71 20 61 6e 64 20 25 51 20  on in %q and %Q 
c5cb0 6d 65 61 6e 73 20 68 6f 77 20 6d 61 6e 79 20 69  means how many i
c5cc0 6e 70 75 74 20 63 68 61 72 61 63 74 65 72 73 20  nput characters 
c5cd0 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  to.        ** co
c5ce0 6e 73 75 6d 65 2c 20 6e 6f 74 20 74 68 65 20 6c  nsume, not the l
c5cf0 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6f 75 74  ength of the out
c5d00 70 75 74 2e 2e 2e 0a 20 20 20 20 20 20 20 20 2a  put....        *
c5d10 2a 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e  * if( precision>
c5d20 3d 30 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c  =0 && precision<
c5d30 6c 65 6e 67 74 68 20 29 20 6c 65 6e 67 74 68 20  length ) length 
c5d40 3d 20 70 72 65 63 69 73 69 6f 6e 3b 20 2a 2f 0a  = precision; */.
c5d50 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
c5d60 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
c5d70 65 20 65 74 54 4f 4b 45 4e 3a 20 7b 0a 20 20 20  e etTOKEN: {.   
c5d80 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b       Token *pTok
c5d90 65 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  en = va_arg(ap, 
c5da0 54 6f 6b 65 6e 2a 29 3b 0a 20 20 20 20 20 20 20  Token*);.       
c5db0 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20   if( pToken ){. 
c5dc0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
c5dd0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70  StrAccumAppend(p
c5de0 41 63 63 75 6d 2c 20 28 63 6f 6e 73 74 20 63 68  Accum, (const ch
c5df0 61 72 2a 29 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70  ar*)pToken->z, p
c5e00 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20  Token->n);.     
c5e10 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e     }.        len
c5e20 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b  gth = width = 0;
c5e30 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
c5e40 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
c5e50 73 65 20 65 74 53 52 43 4c 49 53 54 3a 20 7b 0a  se etSRCLIST: {.
c5e60 20 20 20 20 20 20 20 20 53 72 63 4c 69 73 74 20          SrcList 
c5e70 2a 70 53 72 63 20 3d 20 76 61 5f 61 72 67 28 61  *pSrc = va_arg(a
c5e80 70 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 20 20  p, SrcList*);.  
c5e90 20 20 20 20 20 20 69 6e 74 20 6b 20 3d 20 76 61        int k = va
c5ea0 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
c5eb0 20 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72         struct Sr
c5ec0 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
c5ed0 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 6b 5d 3b  m = &pSrc->a[k];
c5ee0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
c5ef0 20 6b 3e 3d 30 20 26 26 20 6b 3c 70 53 72 63 2d   k>=0 && k<pSrc-
c5f00 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20 20 20 20  >nSrc );.       
c5f10 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 61 74   if( pItem->zDat
c5f20 61 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20  abase ){.       
c5f30 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
c5f40 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c  umAppend(pAccum,
c5f50 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73   pItem->zDatabas
c5f60 65 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  e, -1);.        
c5f70 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
c5f80 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20  mAppend(pAccum, 
c5f90 22 2e 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  ".", 1);.       
c5fa0 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
c5fb0 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
c5fc0 28 70 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e  (pAccum, pItem->
c5fd0 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 20 20  zName, -1);.    
c5fe0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64      length = wid
c5ff0 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  th = 0;.        
c6000 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
c6010 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a       default: {.
c6020 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
c6030 78 74 79 70 65 3d 3d 65 74 49 4e 56 41 4c 49 44  xtype==etINVALID
c6040 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75   );.        retu
c6050 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
c6060 7d 2f 2a 20 45 6e 64 20 73 77 69 74 63 68 20 6f  }/* End switch o
c6070 76 65 72 20 74 68 65 20 66 6f 72 6d 61 74 20 74  ver the format t
c6080 79 70 65 20 2a 2f 0a 20 20 20 20 2f 2a 0a 20 20  ype */.    /*.  
c6090 20 20 2a 2a 20 54 68 65 20 74 65 78 74 20 6f 66    ** The text of
c60a0 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   the conversion 
c60b0 69 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  is pointed to by
c60c0 20 22 62 75 66 70 74 22 20 61 6e 64 20 69 73 0a   "bufpt" and is.
c60d0 20 20 20 20 2a 2a 20 22 6c 65 6e 67 74 68 22 20      ** "length" 
c60e0 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e  characters long.
c60f0 20 20 54 68 65 20 66 69 65 6c 64 20 77 69 64 74    The field widt
c6100 68 20 69 73 20 22 77 69 64 74 68 22 2e 20 20 44  h is "width".  D
c6110 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 75 74  o.    ** the out
c6120 70 75 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  put..    */.    
c6130 69 66 28 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75  if( !flag_leftju
c6140 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20 72  stify ){.      r
c6150 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70 61  egister int nspa
c6160 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63 65  ce;.      nspace
c6170 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68 3b   = width-length;
c6180 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61 63  .      if( nspac
c6190 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  e>0 ){.        s
c61a0 71 6c 69 74 65 33 41 70 70 65 6e 64 53 70 61 63  qlite3AppendSpac
c61b0 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65  e(pAccum, nspace
c61c0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
c61d0 0a 20 20 20 20 69 66 28 20 6c 65 6e 67 74 68 3e  .    if( length>
c61e0 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
c61f0 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
c6200 28 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20  (pAccum, bufpt, 
c6210 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d 0a 20  length);.    }. 
c6220 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 65 66 74     if( flag_left
c6230 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20  justify ){.     
c6240 20 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73   register int ns
c6250 70 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61  pace;.      nspa
c6260 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74  ce = width-lengt
c6270 68 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70  h;.      if( nsp
c6280 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  ace>0 ){.       
c6290 20 73 71 6c 69 74 65 33 41 70 70 65 6e 64 53 70   sqlite3AppendSp
c62a0 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 61  ace(pAccum, nspa
c62b0 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ce);.      }.   
c62c0 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
c62d0 72 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 7d  ree(zExtra);.  }
c62e0 2f 2a 20 45 6e 64 20 66 6f 72 20 6c 6f 6f 70 20  /* End for loop 
c62f0 6f 76 65 72 20 74 68 65 20 66 6f 72 6d 61 74 20  over the format 
c6300 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 2f 2a 20 45  string */.} /* E
c6310 6e 64 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 2a  nd of function *
c6320 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20  /../*.** Append 
c6330 4e 20 62 79 74 65 73 20 6f 66 20 74 65 78 74 20  N bytes of text 
c6340 66 72 6f 6d 20 7a 20 74 6f 20 74 68 65 20 53 74  from z to the St
c6350 72 41 63 63 75 6d 20 6f 62 6a 65 63 74 2e 0a 2a  rAccum object..*
c6360 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
c6370 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72   void sqlite3Str
c6380 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74 72 41  AccumAppend(StrA
c6390 63 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 74 20 63  ccum *p, const c
c63a0 68 61 72 20 2a 7a 2c 20 69 6e 74 20 4e 29 7b 0a  har *z, int N){.
c63b0 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20 7c    assert( z!=0 |
c63c0 7c 20 4e 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20  | N==0 );.  if( 
c63d0 70 2d 3e 74 6f 6f 42 69 67 20 7c 20 70 2d 3e 6d  p->tooBig | p->m
c63e0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
c63f0 20 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e 74     testcase(p->t
c6400 6f 6f 42 69 67 29 3b 0a 20 20 20 20 74 65 73 74  ooBig);.    test
c6410 63 61 73 65 28 70 2d 3e 6d 61 6c 6c 6f 63 46 61  case(p->mallocFa
c6420 69 6c 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72  iled);.    retur
c6430 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  n;.  }.  assert(
c6440 20 70 2d 3e 7a 54 65 78 74 21 3d 30 20 7c 7c 20   p->zText!=0 || 
c6450 70 2d 3e 6e 43 68 61 72 3d 3d 30 20 29 3b 0a 20  p->nChar==0 );. 
c6460 20 69 66 28 20 4e 3c 30 20 29 7b 0a 20 20 20 20   if( N<0 ){.    
c6470 4e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  N = sqlite3Strle
c6480 6e 33 30 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66  n30(z);.  }.  if
c6490 28 20 4e 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28  ( N==0 || NEVER(
c64a0 7a 3d 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74  z==0) ){.    ret
c64b0 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  urn;.  }.  if( p
c64c0 2d 3e 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e  ->nChar+N >= p->
c64d0 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68  nAlloc ){.    ch
c64e0 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66  ar *zNew;.    if
c64f0 28 20 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20  ( !p->useMalloc 
c6500 29 7b 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42  ){.      p->tooB
c6510 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20  ig = 1;.      N 
c6520 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d  = p->nAlloc - p-
c6530 3e 6e 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20  >nChar - 1;.    
c6540 20 20 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20    if( N<=0 ){.  
c6550 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
c6560 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
c6570 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4f 6c  .      char *zOl
c6580 64 20 3d 20 28 70 2d 3e 7a 54 65 78 74 3d 3d 70  d = (p->zText==p
c6590 2d 3e 7a 42 61 73 65 20 3f 20 30 20 3a 20 70 2d  ->zBase ? 0 : p-
c65a0 3e 7a 54 65 78 74 29 3b 0a 20 20 20 20 20 20 69  >zText);.      i
c65b0 36 34 20 73 7a 4e 65 77 20 3d 20 70 2d 3e 6e 43  64 szNew = p->nC
c65c0 68 61 72 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77  har;.      szNew
c65d0 20 2b 3d 20 4e 20 2b 20 31 3b 0a 20 20 20 20 20   += N + 1;.     
c65e0 20 69 66 28 20 73 7a 4e 65 77 20 3e 20 70 2d 3e   if( szNew > p->
c65f0 6d 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20  mxAlloc ){.     
c6600 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
c6610 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20  umReset(p);.    
c6620 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20      p->tooBig = 
c6630 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  1;.        retur
c6640 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  n;.      }else{.
c6650 20 20 20 20 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f          p->nAllo
c6660 63 20 3d 20 28 69 6e 74 29 73 7a 4e 65 77 3b 0a  c = (int)szNew;.
c6670 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
c6680 28 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 3d 3d  ( p->useMalloc==
c6690 31 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4e 65  1 ){.        zNe
c66a0 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  w = sqlite3DbRea
c66b0 6c 6c 6f 63 28 70 2d 3e 64 62 2c 20 7a 4f 6c 64  lloc(p->db, zOld
c66c0 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 29 3b 0a 20 20  , p->nAlloc);.  
c66d0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
c66e0 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65     zNew = sqlite
c66f0 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f 6c 64 2c 20  3_realloc(zOld, 
c6700 70 2d 3e 6e 41 6c 6c 6f 63 29 3b 0a 20 20 20 20  p->nAlloc);.    
c6710 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 7a 4e    }.      if( zN
c6720 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ew ){.        if
c6730 28 20 7a 4f 6c 64 3d 3d 30 20 26 26 20 70 2d 3e  ( zOld==0 && p->
c6740 6e 43 68 61 72 3e 30 20 29 20 6d 65 6d 63 70 79  nChar>0 ) memcpy
c6750 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 78 74 2c  (zNew, p->zText,
c6760 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 20 20 20   p->nChar);.    
c6770 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 7a      p->zText = z
c6780 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  New;.      }else
c6790 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c  {.        p->mal
c67a0 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
c67b0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
c67c0 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a  rAccumReset(p);.
c67d0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
c67e0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
c67f0 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 7a  }.  assert( p->z
c6800 54 65 78 74 20 29 3b 0a 20 20 6d 65 6d 63 70 79  Text );.  memcpy
c6810 28 26 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43  (&p->zText[p->nC
c6820 68 61 72 5d 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70  har], z, N);.  p
c6830 2d 3e 6e 43 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a  ->nChar += N;.}.
c6840 0a 2f 2a 0a 2a 2a 20 46 69 6e 69 73 68 20 6f 66  ./*.** Finish of
c6850 66 20 61 20 73 74 72 69 6e 67 20 62 79 20 6d 61  f a string by ma
c6860 6b 69 6e 67 20 73 75 72 65 20 69 74 20 69 73 20  king sure it is 
c6870 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e  zero-terminated.
c6880 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
c6890 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65 73 75  nter to the resu
c68a0 6c 74 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 52  lting string.  R
c68b0 65 74 75 72 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20  eturn a NULL.** 
c68c0 70 6f 69 6e 74 65 72 20 69 66 20 61 6e 79 20 6b  pointer if any k
c68d0 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 77 61 73  ind of error was
c68e0 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f   encountered..*/
c68f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
c6900 63 68 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72  char *sqlite3Str
c6910 41 63 63 75 6d 46 69 6e 69 73 68 28 53 74 72 41  AccumFinish(StrA
c6920 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20  ccum *p){.  if( 
c6930 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20 20 20 20  p->zText ){.    
c6940 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61  p->zText[p->nCha
c6950 72 5d 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  r] = 0;.    if( 
c6960 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20  p->useMalloc && 
c6970 70 2d 3e 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61  p->zText==p->zBa
c6980 73 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  se ){.      if( 
c6990 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 3d 3d 31 20  p->useMalloc==1 
c69a0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 54  ){.        p->zT
c69b0 65 78 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  ext = sqlite3DbM
c69c0 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20  allocRaw(p->db, 
c69d0 70 2d 3e 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20  p->nChar+1 );.  
c69e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
c69f0 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 73 71     p->zText = sq
c6a00 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 70 2d 3e  lite3_malloc(p->
c6a10 6e 43 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20  nChar+1);.      
c6a20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 7a  }.      if( p->z
c6a30 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Text ){.        
c6a40 6d 65 6d 63 70 79 28 70 2d 3e 7a 54 65 78 74 2c  memcpy(p->zText,
c6a50 20 70 2d 3e 7a 42 61 73 65 2c 20 70 2d 3e 6e 43   p->zBase, p->nC
c6a60 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20 7d 65  har+1);.      }e
c6a70 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  lse{.        p->
c6a80 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
c6a90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
c6aa0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e    }.  return p->
c6ab0 7a 54 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  zText;.}../*.** 
c6ac0 52 65 73 65 74 20 61 6e 20 53 74 72 41 63 63 75  Reset an StrAccu
c6ad0 6d 20 73 74 72 69 6e 67 2e 20 20 52 65 63 6c 61  m string.  Recla
c6ae0 69 6d 20 61 6c 6c 20 6d 61 6c 6c 6f 63 65 64 20  im all malloced 
c6af0 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54  memory..*/.SQLIT
c6b00 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
c6b10 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65  qlite3StrAccumRe
c6b20 73 65 74 28 53 74 72 41 63 63 75 6d 20 2a 70 29  set(StrAccum *p)
c6b30 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74  {.  if( p->zText
c6b40 21 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20 20  !=p->zBase ){.  
c6b50 20 20 69 66 28 20 70 2d 3e 75 73 65 4d 61 6c 6c    if( p->useMall
c6b60 6f 63 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 73  oc==1 ){.      s
c6b70 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e  qlite3DbFree(p->
c6b80 64 62 2c 20 70 2d 3e 7a 54 65 78 74 29 3b 0a 20  db, p->zText);. 
c6b90 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
c6ba0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
c6bb0 7a 54 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20  zText);.    }.  
c6bc0 7d 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30  }.  p->zText = 0
c6bd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  ;.}../*.** Initi
c6be0 61 6c 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61  alize a string a
c6bf0 63 63 75 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51  ccumulator.*/.SQ
c6c00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
c6c10 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75  d sqlite3StrAccu
c6c20 6d 49 6e 69 74 28 53 74 72 41 63 63 75 6d 20 2a  mInit(StrAccum *
c6c30 70 2c 20 63 68 61 72 20 2a 7a 42 61 73 65 2c 20  p, char *zBase, 
c6c40 69 6e 74 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a  int n, int mx){.
c6c50 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e    p->zText = p->
c6c60 7a 42 61 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20  zBase = zBase;. 
c6c70 20 70 2d 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d   p->db = 0;.  p-
c6c80 3e 6e 43 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d  >nChar = 0;.  p-
c6c90 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70  >nAlloc = n;.  p
c6ca0 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a  ->mxAlloc = mx;.
c6cb0 20 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d    p->useMalloc =
c6cc0 20 31 3b 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20   1;.  p->tooBig 
c6cd0 3d 20 30 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63  = 0;.  p->malloc
c6ce0 46 61 69 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f  Failed = 0;.}../
c6cf0 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20  *.** Print into 
c6d00 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
c6d10 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  from sqliteMallo
c6d20 63 28 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e  c().  Use the in
c6d30 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76  ternal.** %-conv
c6d40 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e  ersion extension
c6d50 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
c6d60 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
c6d70 65 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74  e3VMPrintf(sqlit
c6d80 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
c6d90 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f  ar *zFormat, va_
c6da0 6c 69 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72  list ap){.  char
c6db0 20 2a 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73   *z;.  char zBas
c6dc0 65 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42  e[SQLITE_PRINT_B
c6dd0 55 46 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41  UF_SIZE];.  StrA
c6de0 63 63 75 6d 20 61 63 63 3b 0a 20 20 61 73 73 65  ccum acc;.  asse
c6df0 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 73  rt( db!=0 );.  s
c6e00 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e  qlite3StrAccumIn
c6e10 69 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20  it(&acc, zBase, 
c6e20 73 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a 20  sizeof(zBase),. 
c6e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6e40 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b       db->aLimit[
c6e50 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
c6e60 47 54 48 5d 29 3b 0a 20 20 61 63 63 2e 64 62 20  GTH]);.  acc.db 
c6e70 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56  = db;.  sqlite3V
c6e80 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 31 2c  XPrintf(&acc, 1,
c6e90 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20   zFormat, ap);. 
c6ea0 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41   z = sqlite3StrA
c6eb0 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29  ccumFinish(&acc)
c6ec0 3b 0a 20 20 69 66 28 20 61 63 63 2e 6d 61 6c 6c  ;.  if( acc.mall
c6ed0 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
c6ee0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
c6ef0 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 1;.  }.  retu
c6f00 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  rn z;.}../*.** P
c6f10 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  rint into memory
c6f20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
c6f30 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20  qliteMalloc().  
c6f40 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  Use the internal
c6f50 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e  .** %-conversion
c6f60 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a   extensions..*/.
c6f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
c6f80 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69  har *sqlite3MPri
c6f90 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ntf(sqlite3 *db,
c6fa0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
c6fb0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
c6fc0 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
c6fd0 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28   *z;.  va_start(
c6fe0 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20  ap, zFormat);.  
c6ff0 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69  z = sqlite3VMPri
c7000 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c  ntf(db, zFormat,
c7010 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61   ap);.  va_end(a
c7020 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a  p);.  return z;.
c7030 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71  }../*.** Like sq
c7040 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20  lite3MPrintf(), 
c7050 62 75 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  but call sqlite3
c7060 44 62 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72  DbFree() on zStr
c7070 20 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e   after formattin
c7080 67 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20  g.** the string 
c7090 61 6e 64 20 62 65 66 6f 72 65 20 72 65 74 75 72  and before retur
c70a0 6e 6e 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75  nning.  This rou
c70b0 74 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64  tine is intended
c70c0 20 74 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74   to be used.** t
c70d0 6f 20 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73  o modify an exis
c70e0 74 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f  ting string.  Fo
c70f0 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  r example:.**.**
c7100 20 20 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74         x = sqlit
c7110 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c  e3MPrintf(db, x,
c7120 20 22 70 72 65 66 69 78 20 25 73 20 73 75 66 66   "prefix %s suff
c7130 69 78 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53  ix", x);.**.*/.S
c7140 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
c7150 61 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65  ar *sqlite3MAppe
c7160 6e 64 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ndf(sqlite3 *db,
c7170 20 63 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e   char *zStr, con
c7180 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
c7190 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73  , ...){.  va_lis
c71a0 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b  t ap;.  char *z;
c71b0 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20  .  va_start(ap, 
c71c0 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20  zFormat);.  z = 
c71d0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
c71e0 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29  db, zFormat, ap)
c71f0 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
c7200 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
c7210 64 62 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74  db, zStr);.  ret
c7220 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn z;.}../*.** 
c7230 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72  Print into memor
c7240 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
c7250 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
c7260 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65  .  Omit the inte
c7270 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72  rnal.** %-conver
c7280 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e  sion extensions.
c7290 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
c72a0 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70  har *sqlite3_vmp
c72b0 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72  rintf(const char
c72c0 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69   *zFormat, va_li
c72d0 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a  st ap){.  char *
c72e0 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b  z;.  char zBase[
c72f0 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46  SQLITE_PRINT_BUF
c7300 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63  _SIZE];.  StrAcc
c7310 75 6d 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20  um acc;.#ifndef 
c7320 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
c7330 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74  INIT.  if( sqlit
c7340 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
c7350 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64  ) return 0;.#end
c7360 69 66 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41  if.  sqlite3StrA
c7370 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a  ccumInit(&acc, z
c7380 42 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61  Base, sizeof(zBa
c7390 73 65 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f  se), SQLITE_MAX_
c73a0 4c 45 4e 47 54 48 29 3b 0a 20 20 61 63 63 2e 75  LENGTH);.  acc.u
c73b0 73 65 4d 61 6c 6c 6f 63 20 3d 20 32 3b 0a 20 20  seMalloc = 2;.  
c73c0 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28  sqlite3VXPrintf(
c73d0 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74  &acc, 0, zFormat
c73e0 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71 6c  , ap);.  z = sql
c73f0 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69  ite3StrAccumFini
c7400 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65 74 75  sh(&acc);.  retu
c7410 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  rn z;.}../*.** P
c7420 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  rint into memory
c7430 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
c7440 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 28  qlite3_malloc()(
c7450 29 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74  ).  Omit the int
c7460 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65  ernal.** %-conve
c7470 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73  rsion extensions
c7480 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
c7490 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70  char *sqlite3_mp
c74a0 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72  rintf(const char
c74b0 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
c74c0 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
c74d0 20 63 68 61 72 20 2a 7a 3b 0a 23 69 66 6e 64 65   char *z;.#ifnde
c74e0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
c74f0 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c  TOINIT.  if( sql
c7500 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
c7510 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65  ) ) return 0;.#e
c7520 6e 64 69 66 0a 20 20 76 61 5f 73 74 61 72 74 28  ndif.  va_start(
c7530 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20  ap, zFormat);.  
c7540 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 6d 70 72  z = sqlite3_vmpr
c7550 69 6e 74 66 28 7a 46 6f 72 6d 61 74 2c 20 61 70  intf(zFormat, ap
c7560 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b  );.  va_end(ap);
c7570 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a  .  return z;.}..
c7580 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 6e  /*.** sqlite3_sn
c7590 70 72 69 6e 74 66 28 29 20 77 6f 72 6b 73 20 6c  printf() works l
c75a0 69 6b 65 20 73 6e 70 72 69 6e 74 66 28 29 20 65  ike snprintf() e
c75b0 78 63 65 70 74 20 74 68 61 74 20 69 74 20 69 67  xcept that it ig
c75c0 6e 6f 72 65 73 20 74 68 65 0a 2a 2a 20 63 75 72  nores the.** cur
c75d0 72 65 6e 74 20 6c 6f 63 61 6c 65 20 73 65 74 74  rent locale sett
c75e0 69 6e 67 73 2e 20 20 54 68 69 73 20 69 73 20 69  ings.  This is i
c75f0 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20 53 51 4c  mportant for SQL
c7600 69 74 65 20 62 65 63 61 75 73 65 20 77 65 0a 2a  ite because we.*
c7610 2a 20 61 72 65 20 6e 6f 74 20 61 62 6c 65 20 74  * are not able t
c7620 6f 20 75 73 65 20 61 20 22 2c 22 20 61 73 20 74  o use a "," as t
c7630 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74  he decimal point
c7640 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 22 2e 22   in place of "."
c7650 20 61 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64   as.** specified
c7660 20 62 79 20 73 6f 6d 65 20 6c 6f 63 61 6c 65 73   by some locales
c7670 2e 0a 2a 2a 0a 2a 2a 20 4f 6f 70 73 3a 20 20 54  ..**.** Oops:  T
c7680 68 65 20 66 69 72 73 74 20 74 77 6f 20 61 72 67  he first two arg
c7690 75 6d 65 6e 74 73 20 6f 66 20 73 71 6c 69 74 65  uments of sqlite
c76a0 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 61 72 65  3_snprintf() are
c76b0 20 62 61 63 6b 77 61 72 64 73 0a 2a 2a 20 66 72   backwards.** fr
c76c0 6f 6d 20 74 68 65 20 73 6e 70 72 69 6e 74 66 28  om the snprintf(
c76d0 29 20 73 74 61 6e 64 61 72 64 2e 20 20 55 6e 66  ) standard.  Unf
c76e0 6f 72 74 75 6e 61 74 65 6c 79 2c 20 69 74 20 69  ortunately, it i
c76f0 73 20 74 6f 6f 20 6c 61 74 65 20 74 6f 20 63 68  s too late to ch
c7700 61 6e 67 65 0a 2a 2a 20 74 68 69 73 20 77 69 74  ange.** this wit
c7710 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 20 63 6f  hout breaking co
c7720 6d 70 61 74 69 62 69 6c 69 74 79 2c 20 73 6f 20  mpatibility, so 
c7730 77 65 20 6a 75 73 74 20 68 61 76 65 20 74 6f 20  we just have to 
c7740 6c 69 76 65 20 77 69 74 68 20 74 68 65 0a 2a 2a  live with the.**
c7750 20 6d 69 73 74 61 6b 65 2e 0a 2a 2a 0a 2a 2a 20   mistake..**.** 
c7760 73 71 6c 69 74 65 33 5f 76 73 6e 70 72 69 6e 74  sqlite3_vsnprint
c7770 66 28 29 20 69 73 20 74 68 65 20 76 61 72 61 72  f() is the varar
c7780 67 73 20 76 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 53  gs version..*/.S
c7790 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
c77a0 73 71 6c 69 74 65 33 5f 76 73 6e 70 72 69 6e 74  sqlite3_vsnprint
c77b0 66 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a  f(int n, char *z
c77c0 42 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Buf, const char 
c77d0 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73  *zFormat, va_lis
c77e0 74 20 61 70 29 7b 0a 20 20 53 74 72 41 63 63 75  t ap){.  StrAccu
c77f0 6d 20 61 63 63 3b 0a 20 20 69 66 28 20 6e 3c 3d  m acc;.  if( n<=
c7800 30 20 29 20 72 65 74 75 72 6e 20 7a 42 75 66 3b  0 ) return zBuf;
c7810 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  .  sqlite3StrAcc
c7820 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75  umInit(&acc, zBu
c7830 66 2c 20 6e 2c 20 30 29 3b 0a 20 20 61 63 63 2e  f, n, 0);.  acc.
c7840 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20  useMalloc = 0;. 
c7850 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66   sqlite3VXPrintf
c7860 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61  (&acc, 0, zForma
c7870 74 2c 20 61 70 29 3b 0a 20 20 72 65 74 75 72 6e  t, ap);.  return
c7880 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
c7890 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 7d 0a  Finish(&acc);.}.
c78a0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
c78b0 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74  *sqlite3_snprint
c78c0 66 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a  f(int n, char *z
c78d0 42 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Buf, const char 
c78e0 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
c78f0 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f    char *z;.  va_
c7900 6c 69 73 74 20 61 70 3b 0a 20 20 76 61 5f 73 74  list ap;.  va_st
c7910 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b  art(ap,zFormat);
c7920 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76  .  z = sqlite3_v
c7930 73 6e 70 72 69 6e 74 66 28 6e 2c 20 7a 42 75 66  snprintf(n, zBuf
c7940 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
c7950 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
c7960 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a  return z;.}../*.
c7970 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 72  ** This is the r
c7980 6f 75 74 69 6e 65 20 74 68 61 74 20 61 63 74 75  outine that actu
c7990 61 6c 6c 79 20 66 6f 72 6d 61 74 73 20 74 68 65  ally formats the
c79a0 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 20 6d   sqlite3_log() m
c79b0 65 73 73 61 67 65 2e 0a 2a 2a 20 57 65 20 68 6f  essage..** We ho
c79c0 75 73 65 20 69 74 20 69 6e 20 61 20 73 65 70 61  use it in a sepa
c79d0 72 61 74 65 20 72 6f 75 74 69 6e 65 20 66 72 6f  rate routine fro
c79e0 6d 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 20  m sqlite3_log() 
c79f0 74 6f 20 61 76 6f 69 64 20 75 73 69 6e 67 0a 2a  to avoid using.*
c7a00 2a 20 73 74 61 63 6b 20 73 70 61 63 65 20 6f 6e  * stack space on
c7a10 20 73 6d 61 6c 6c 2d 73 74 61 63 6b 20 73 79 73   small-stack sys
c7a20 74 65 6d 73 20 77 68 65 6e 20 6c 6f 67 67 69 6e  tems when loggin
c7a30 67 20 69 73 20 64 69 73 61 62 6c 65 64 2e 0a 2a  g is disabled..*
c7a40 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 6f 67  *.** sqlite3_log
c7a50 28 29 20 6d 75 73 74 20 72 65 6e 64 65 72 20 69  () must render i
c7a60 6e 74 6f 20 61 20 73 74 61 74 69 63 20 62 75 66  nto a static buf
c7a70 66 65 72 2e 20 20 49 74 20 63 61 6e 6e 6f 74 20  fer.  It cannot 
c7a80 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61  dynamically.** a
c7a90 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 62  llocate memory b
c7aa0 65 63 61 75 73 65 20 69 74 20 6d 69 67 68 74 20  ecause it might 
c7ab0 62 65 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 20  be called while 
c7ac0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
c7ad0 61 74 6f 72 0a 2a 2a 20 6d 75 74 65 78 20 69 73  ator.** mutex is
c7ae0 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63   held..*/.static
c7af0 20 76 6f 69 64 20 72 65 6e 64 65 72 4c 6f 67 4d   void renderLogM
c7b00 73 67 28 69 6e 74 20 69 45 72 72 43 6f 64 65 2c  sg(int iErrCode,
c7b10 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
c7b20 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 70  rmat, va_list ap
c7b30 29 7b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63  ){.  StrAccum ac
c7b40 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
c7b50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
c7b60 74 72 69 6e 67 20 61 63 63 75 6d 75 6c 61 74 6f  tring accumulato
c7b70 72 20 2a 2f 0a 20 20 63 68 61 72 20 7a 4d 73 67  r */.  char zMsg
c7b80 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55  [SQLITE_PRINT_BU
c7b90 46 5f 53 49 5a 45 2a 33 5d 3b 20 20 20 20 2f 2a  F_SIZE*3];    /*
c7ba0 20 43 6f 6d 70 6c 65 74 65 20 6c 6f 67 20 6d 65   Complete log me
c7bb0 73 73 61 67 65 20 2a 2f 0a 0a 20 20 73 71 6c 69  ssage */..  sqli
c7bc0 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28  te3StrAccumInit(
c7bd0 26 61 63 63 2c 20 7a 4d 73 67 2c 20 73 69 7a 65  &acc, zMsg, size
c7be0 6f 66 28 7a 4d 73 67 29 2c 20 30 29 3b 0a 20 20  of(zMsg), 0);.  
c7bf0 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20  acc.useMalloc = 
c7c00 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72  0;.  sqlite3VXPr
c7c10 69 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46  intf(&acc, 0, zF
c7c20 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 73 71  ormat, ap);.  sq
c7c30 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
c7c40 67 2e 78 4c 6f 67 28 73 71 6c 69 74 65 33 47 6c  g.xLog(sqlite3Gl
c7c50 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 4c 6f 67 41  obalConfig.pLogA
c7c60 72 67 2c 20 69 45 72 72 43 6f 64 65 2c 0a 20 20  rg, iErrCode,.  
c7c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7c80 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
c7c90 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26  StrAccumFinish(&
c7ca0 61 63 63 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  acc));.}../*.** 
c7cb0 46 6f 72 6d 61 74 20 61 6e 64 20 77 72 69 74 65  Format and write
c7cc0 20 61 20 6d 65 73 73 61 67 65 20 74 6f 20 74 68   a message to th
c7cd0 65 20 6c 6f 67 20 69 66 20 6c 6f 67 67 69 6e 67  e log if logging
c7ce0 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a 2a 2f 0a   is enabled..*/.
c7cf0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
c7d00 73 71 6c 69 74 65 33 5f 6c 6f 67 28 69 6e 74 20  sqlite3_log(int 
c7d10 69 45 72 72 43 6f 64 65 2c 20 63 6f 6e 73 74 20  iErrCode, const 
c7d20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
c7d30 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
c7d40 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
c7d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c7d60 2a 20 56 61 72 61 72 67 20 6c 69 73 74 20 2a 2f  * Vararg list */
c7d70 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
c7d80 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f 67 20  obalConfig.xLog 
c7d90 29 7b 0a 20 20 20 20 76 61 5f 73 74 61 72 74 28  ){.    va_start(
c7da0 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20  ap, zFormat);.  
c7db0 20 20 72 65 6e 64 65 72 4c 6f 67 4d 73 67 28 69    renderLogMsg(i
c7dc0 45 72 72 43 6f 64 65 2c 20 7a 46 6f 72 6d 61 74  ErrCode, zFormat
c7dd0 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e  , ap);.    va_en
c7de0 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69  d(ap);.  }.}..#i
c7df0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
c7e00 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20  _DEBUG)./*.** A 
c7e10 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74  version of print
c7e20 66 28 29 20 74 68 61 74 20 75 6e 64 65 72 73 74  f() that underst
c7e30 61 6e 64 73 20 25 6c 6c 64 2e 20 20 55 73 65 64  ands %lld.  Used
c7e40 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0a   for debugging..
c7e50 2a 2a 20 54 68 65 20 70 72 69 6e 74 66 28 29 20  ** The printf() 
c7e60 62 75 69 6c 74 20 69 6e 74 6f 20 73 6f 6d 65 20  built into some 
c7e70 76 65 72 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64  versions of wind
c7e80 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 75 6e 64  ows does not und
c7e90 65 72 73 74 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20  erstand %lld.** 
c7ea0 61 6e 64 20 73 65 67 66 61 75 6c 74 73 20 69 66  and segfaults if
c7eb0 20 79 6f 75 20 67 69 76 65 20 69 74 20 61 20 6c   you give it a l
c7ec0 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f  ong long int..*/
c7ed0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
c7ee0 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 62 75  void sqlite3Debu
c7ef0 67 50 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68  gPrintf(const ch
c7f00 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
c7f10 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
c7f20 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b  .  StrAccum acc;
c7f30 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 35 30 30  .  char zBuf[500
c7f40 5d 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41  ];.  sqlite3StrA
c7f50 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a  ccumInit(&acc, z
c7f60 42 75 66 2c 20 73 69 7a 65 6f 66 28 7a 42 75 66  Buf, sizeof(zBuf
c7f70 29 2c 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65  ), 0);.  acc.use
c7f80 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61  Malloc = 0;.  va
c7f90 5f 73 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61  _start(ap,zForma
c7fa0 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50  t);.  sqlite3VXP
c7fb0 72 69 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a  rintf(&acc, 0, z
c7fc0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76  Format, ap);.  v
c7fd0 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c  a_end(ap);.  sql
c7fe0 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69  ite3StrAccumFini
c7ff0 73 68 28 26 61 63 63 29 3b 0a 20 20 66 70 72 69  sh(&acc);.  fpri
c8000 6e 74 66 28 73 74 64 6f 75 74 2c 22 25 73 22 2c  ntf(stdout,"%s",
c8010 20 7a 42 75 66 29 3b 0a 20 20 66 66 6c 75 73 68   zBuf);.  fflush
c8020 28 73 74 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64  (stdout);.}.#end
c8030 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
c8040 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a  TE_OMIT_TRACE./*
c8050 0a 2a 2a 20 76 61 72 69 61 62 6c 65 2d 61 72 67  .** variable-arg
c8060 75 6d 65 6e 74 20 77 72 61 70 70 65 72 20 61 72  ument wrapper ar
c8070 6f 75 6e 64 20 73 71 6c 69 74 65 33 56 58 50 72  ound sqlite3VXPr
c8080 69 6e 74 66 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  intf()..*/.SQLIT
c8090 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
c80a0 71 6c 69 74 65 33 58 50 72 69 6e 74 66 28 53 74  qlite3XPrintf(St
c80b0 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 74  rAccum *p, const
c80c0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
c80d0 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
c80e0 61 70 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61  ap;.  va_start(a
c80f0 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71  p,zFormat);.  sq
c8100 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 70 2c  lite3VXPrintf(p,
c8110 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29   1, zFormat, ap)
c8120 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
c8130 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  }.#endif../*****
c8140 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
c8150 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a   printf.c ******
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 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
c8190 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
c81a0 66 69 6c 65 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a  file random.c **
c81b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c81c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c81d0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
c81e0 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
c81f0 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
c8200 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
c8210 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
c8220 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
c8230 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
c8240 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
c8250 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
c8260 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
c8270 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
c8280 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
c8290 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
c82a0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
c82b0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
c82c0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
c82d0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
c82e0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
c82f0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
c8300 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
c8310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8350 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
c8360 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 69  ntains code to i
c8370 6d 70 6c 65 6d 65 6e 74 20 61 20 70 73 65 75 64  mplement a pseud
c8380 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 0a  o-random number.
c8390 2a 2a 20 67 65 6e 65 72 61 74 6f 72 20 28 50 52  ** generator (PR
c83a0 4e 47 29 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a  NG) for SQLite..
c83b0 2a 2a 0a 2a 2a 20 52 61 6e 64 6f 6d 20 6e 75 6d  **.** Random num
c83c0 62 65 72 73 20 61 72 65 20 75 73 65 64 20 62 79  bers are used by
c83d0 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 64 61 74   some of the dat
c83e0 61 62 61 73 65 20 62 61 63 6b 65 6e 64 73 20 69  abase backends i
c83f0 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 67 65  n order.** to ge
c8400 6e 65 72 61 74 65 20 72 61 6e 64 6f 6d 20 69 6e  nerate random in
c8410 74 65 67 65 72 20 6b 65 79 73 20 66 6f 72 20 74  teger keys for t
c8420 61 62 6c 65 73 20 6f 72 20 72 61 6e 64 6f 6d 20  ables or random 
c8430 66 69 6c 65 6e 61 6d 65 73 2e 0a 2a 2f 0a 0a 0a  filenames..*/...
c8440 2f 2a 20 41 6c 6c 20 74 68 72 65 61 64 73 20 73  /* All threads s
c8450 68 61 72 65 20 61 20 73 69 6e 67 6c 65 20 72 61  hare a single ra
c8460 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65  ndom number gene
c8470 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20 73  rator..** This s
c8480 74 72 75 63 74 75 72 65 20 69 73 20 74 68 65 20  tructure is the 
c8490 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66  current state of
c84a0 20 74 68 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a   the generator..
c84b0 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45  */.static SQLITE
c84c0 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69  _WSD struct sqli
c84d0 74 65 33 50 72 6e 67 54 79 70 65 20 7b 0a 20 20  te3PrngType {.  
c84e0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 73  unsigned char is
c84f0 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f  Init;          /
c8500 2a 20 54 72 75 65 20 69 66 20 69 6e 69 74 69 61  * True if initia
c8510 6c 69 7a 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67  lized */.  unsig
c8520 6e 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20 20  ned char i, j;  
c8530 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
c8540 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a  te variables */.
c8550 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
c8560 73 5b 32 35 36 5d 3b 20 20 20 20 20 20 20 20 20  s[256];         
c8570 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62   /* State variab
c8580 6c 65 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33  les */.} sqlite3
c8590 50 72 6e 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  Prng;../*.** Get
c85a0 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74 20   a single 8-bit 
c85b0 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72 6f  random value fro
c85c0 6d 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e 20  m the RC4 PRNG. 
c85d0 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d 75   The Mutex.** mu
c85e0 73 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65  st be held while
c85f0 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20   executing this 
c8600 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57  routine..**.** W
c8610 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65 20  hy not just use 
c8620 61 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f 6d  a library random
c8630 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65 20   generator like 
c8640 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74 68  lrand48() for th
c8650 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20 74  is?.** Because t
c8660 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 6f  he OP_NewRowid o
c8670 70 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44 42  pcode in the VDB
c8680 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61 76  E depends on hav
c8690 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67 6f  ing a very.** go
c86a0 6f 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61 6e  od source of ran
c86b0 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54 68  dom numbers.  Th
c86c0 65 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62 72  e lrand48() libr
c86d0 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  ary function may
c86e0 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f 64  .** well be good
c86f0 20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d 61   enough.  But ma
c8700 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 79  ybe not.  Or may
c8710 62 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61 73  be lrand48() has
c8720 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 20   some.** subtle 
c8730 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65  problems on some
c8740 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 63 6f   systems that co
c8750 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c 65  uld cause proble
c8760 6d 73 2e 20 20 49 74 20 69 73 20 68 61 72 64 0a  ms.  It is hard.
c8770 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20  ** to know.  To 
c8780 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69 73  minimize the ris
c8790 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64 75  k of problems du
c87a0 65 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34 38  e to bad lrand48
c87b0 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  ().** implementa
c87c0 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75 73  tions, SQLite us
c87d0 65 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20 6e  es this random n
c87e0 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20  umber generator 
c87f0 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c  based.** on RC4,
c8800 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 77   which we know w
c8810 6f 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e 0a  orks very well..
c8820 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20 20  **.** (Later):  
c8830 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77  Actually, OP_New
c8840 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20 64  Rowid does not d
c8850 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 20  epend on a good 
c8860 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61 6e  source of.** ran
c8870 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72 65  domness any more
c8880 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 6c  .  But we will l
c8890 65 61 76 65 20 74 68 69 73 20 63 6f 64 65 20 69  eave this code i
c88a0 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e 0a  n all the same..
c88b0 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 72 61 6e  */.static u8 ran
c88c0 64 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b 0a 20  domByte(void){. 
c88d0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74   unsigned char t
c88e0 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22 77 73  ;...  /* The "ws
c88f0 64 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 69 6c  dPrng" macro wil
c8900 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65  l resolve to the
c8910 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e   pseudo-random n
c8920 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 0a  umber generator.
c8930 20 20 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f    ** state vecto
c8940 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20  r.  If writable 
c8950 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75  static data is u
c8960 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68  nsupported on th
c8970 65 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a 20 77  e target,.  ** w
c8980 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65  e have to locate
c8990 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 6f   the state vecto
c89a0 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20  r at run-time.  
c89b0 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d  In the more comm
c89c0 6f 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77 68 65  on.  ** case whe
c89d0 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74  re writable stat
c89e0 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f  ic data is suppo
c89f0 72 74 65 64 2c 20 77 73 64 50 72 6e 67 20 63 61  rted, wsdPrng ca
c8a00 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79  n refer directly
c8a10 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 73 71  .  ** to the "sq
c8a20 6c 69 74 65 33 50 72 6e 67 22 20 73 74 61 74 65  lite3Prng" state
c8a30 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64   vector declared
c8a40 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69 66   above..  */.#if
c8a50 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
c8a60 57 53 44 0a 20 20 73 74 72 75 63 74 20 73 71 6c  WSD.  struct sql
c8a70 69 74 65 33 50 72 6e 67 54 79 70 65 20 2a 70 20  ite3PrngType *p 
c8a80 3d 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74  = &GLOBAL(struct
c8a90 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65   sqlite3PrngType
c8aa0 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 3b 0a  , sqlite3Prng);.
c8ab0 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e 67  # define wsdPrng
c8ac0 20 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65   p[0].#else.# de
c8ad0 66 69 6e 65 20 77 73 64 50 72 6e 67 20 73 71 6c  fine wsdPrng sql
c8ae0 69 74 65 33 50 72 6e 67 0a 23 65 6e 64 69 66 0a  ite3Prng.#endif.
c8af0 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
c8b00 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  e the state of t
c8b10 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  he random number
c8b20 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65 2c   generator once,
c8b30 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20  .  ** the first 
c8b40 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e  time this routin
c8b50 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68  e is called.  Th
c8b60 65 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f 65  e seed value doe
c8b70 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64 20  s.  ** not need 
c8b80 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f 74  to contain a lot
c8b90 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 73   of randomness s
c8ba0 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74 0a  ince we are not.
c8bb0 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 64    ** trying to d
c8bc0 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79 70 74  o secure encrypt
c8bd0 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e 67 20  ion or anything 
c8be0 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 20 2a  like that....  *
c8bf0 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 20 69  *.  ** Nothing i
c8c00 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 72 20 61  n this file or a
c8c10 6e 79 77 68 65 72 65 20 65 6c 73 65 20 69 6e 20  nywhere else in 
c8c20 53 51 4c 69 74 65 20 64 6f 65 73 20 61 6e 79 20  SQLite does any 
c8c30 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 6e 63  kind of.  ** enc
c8c40 72 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 52 43  ryption.  The RC
c8c50 34 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 62  4 algorithm is b
c8c60 65 69 6e 67 20 75 73 65 64 20 61 73 20 61 20 50  eing used as a P
c8c70 52 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 6e 64  RNG (pseudo-rand
c8c80 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67  om.  ** number g
c8c90 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 61 73  enerator) not as
c8ca0 20 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e 20 64   an encryption d
c8cb0 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  evice..  */.  if
c8cc0 28 20 21 77 73 64 50 72 6e 67 2e 69 73 49 6e 69  ( !wsdPrng.isIni
c8cd0 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  t ){.    int i;.
c8ce0 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d 3b      char k[256];
c8cf0 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 3d  .    wsdPrng.j =
c8d00 20 30 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e   0;.    wsdPrng.
c8d10 69 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  i = 0;.    sqlit
c8d20 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73  e3OsRandomness(s
c8d30 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28  qlite3_vfs_find(
c8d40 30 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20 20 20  0), 256, k);.   
c8d50 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b   for(i=0; i<256;
c8d60 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73 64   i++){.      wsd
c8d70 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 28 75 38 29  Prng.s[i] = (u8)
c8d80 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  i;.    }.    for
c8d90 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b  (i=0; i<256; i++
c8da0 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67  ){.      wsdPrng
c8db0 2e 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b  .j += wsdPrng.s[
c8dc0 69 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20  i] + k[i];.     
c8dd0 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77   t = wsdPrng.s[w
c8de0 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20  sdPrng.j];.     
c8df0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
c8e00 6e 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e  ng.j] = wsdPrng.
c8e10 73 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 50  s[i];.      wsdP
c8e20 72 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20  rng.s[i] = t;.  
c8e30 20 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 2e    }.    wsdPrng.
c8e40 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a  isInit = 1;.  }.
c8e50 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  .  /* Generate a
c8e60 6e 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c 65  nd return single
c8e70 20 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a   random byte.  *
c8e80 2f 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b  /.  wsdPrng.i++;
c8e90 0a 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73  .  t = wsdPrng.s
c8ea0 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77  [wsdPrng.i];.  w
c8eb0 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20  sdPrng.j += t;. 
c8ec0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
c8ed0 6e 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e  ng.i] = wsdPrng.
c8ee0 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20  s[wsdPrng.j];.  
c8ef0 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e  wsdPrng.s[wsdPrn
c8f00 67 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d  g.j] = t;.  t +=
c8f10 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
c8f20 6e 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20  ng.i];.  return 
c8f30 77 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a  wsdPrng.s[t];.}.
c8f40 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20  ./*.** Return N 
c8f50 72 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f  random bytes..*/
c8f60 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
c8f70 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
c8f80 65 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20  ess(int N, void 
c8f90 2a 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e  *pBuf){.  unsign
c8fa0 65 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20  ed char *zBuf = 
c8fb0 70 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 45  pBuf;.#if SQLITE
c8fc0 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71  _THREADSAFE.  sq
c8fd0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
c8fe0 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
c8ff0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
c9000 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 29  TEX_STATIC_PRNG)
c9010 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  ;.#endif.  sqlit
c9020 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
c9030 75 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 20  utex);.  while( 
c9040 4e 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75  N-- ){.    *(zBu
c9050 66 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74  f++) = randomByt
c9060 65 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  e();.  }.  sqlit
c9070 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
c9080 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  utex);.}..#ifnde
c9090 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55  f SQLITE_OMIT_BU
c90a0 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  ILTIN_TEST./*.**
c90b0 20 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72   For testing pur
c90c0 70 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 69  poses, we someti
c90d0 6d 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65 73  mes want to pres
c90e0 65 72 76 65 20 74 68 65 20 73 74 61 74 65 20 6f  erve the state o
c90f0 66 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 65  f.** PRNG and re
c9100 73 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20 74  store the PRNG t
c9110 6f 20 69 74 73 20 73 61 76 65 64 20 73 74 61 74  o its saved stat
c9120 65 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d  e at a later tim
c9130 65 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65  e, or.** to rese
c9140 74 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74  t the PRNG to it
c9150 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e  s initial state.
c9160 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73    These routines
c9170 20 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74   accomplish.** t
c9180 68 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a  hose tasks..**.*
c9190 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65  * The sqlite3_te
c91a0 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74  st_control() int
c91b0 65 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68 65  erface calls the
c91c0 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a  se routines to.*
c91d0 2a 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52  * control the PR
c91e0 4e 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51  NG..*/.static SQ
c91f0 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20  LITE_WSD struct 
c9200 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20  sqlite3PrngType 
c9210 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67  sqlite3SavedPrng
c9220 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
c9230 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e   void sqlite3Prn
c9240 67 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 29  gSaveState(void)
c9250 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 20  {.  memcpy(.    
c9260 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73  &GLOBAL(struct s
c9270 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20  qlite3PrngType, 
c9280 73 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67  sqlite3SavedPrng
c9290 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73  ),.    &GLOBAL(s
c92a0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e  truct sqlite3Prn
c92b0 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72  gType, sqlite3Pr
c92c0 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28  ng),.    sizeof(
c92d0 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 29  sqlite3Prng).  )
c92e0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
c92f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
c9300 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 28  rngRestoreState(
c9310 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 28  void){.  memcpy(
c9320 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72  .    &GLOBAL(str
c9330 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54  uct sqlite3PrngT
c9340 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67  ype, sqlite3Prng
c9350 29 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73  ),.    &GLOBAL(s
c9360 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e  truct sqlite3Prn
c9370 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61  gType, sqlite3Sa
c9380 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 73 69  vedPrng),.    si
c9390 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e 67  zeof(sqlite3Prng
c93a0 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  ).  );.}.SQLITE_
c93b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
c93c0 69 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 61  ite3PrngResetSta
c93d0 74 65 28 76 6f 69 64 29 7b 0a 20 20 47 4c 4f 42  te(void){.  GLOB
c93e0 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65  AL(struct sqlite
c93f0 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74  3PrngType, sqlit
c9400 65 33 50 72 6e 67 29 2e 69 73 49 6e 69 74 20 3d  e3Prng).isInit =
c9410 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   0;.}.#endif /* 
c9420 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
c9430 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a  TIN_TEST */../**
c9440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
c9450 20 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a   of random.c ***
c9460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
c9490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
c94a0 69 6e 20 66 69 6c 65 20 75 74 66 2e 63 20 2a 2a  in file utf.c **
c94b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c94c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c94d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
c94e0 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 31 33  ** 2004 April 13
c94f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
c9500 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
c9510 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
c9520 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
c9530 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
c9540 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
c9550 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
c9560 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
c9570 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
c9580 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
c9590 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
c95a0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
c95b0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
c95c0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
c95d0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
c95e0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
c95f0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
c9600 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
c9610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
c9650 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
c9660 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75  tains routines u
c9670 73 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65  sed to translate
c9680 20 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c 20   between UTF-8, 
c9690 0a 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46 2d  .** UTF-16, UTF-
c96a0 31 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31 36  16BE, and UTF-16
c96b0 4c 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20  LE..**.** Notes 
c96c0 6f 6e 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20  on UTF-8:.**.** 
c96d0 20 20 42 79 74 65 2d 30 20 20 20 20 42 79 74 65    Byte-0    Byte
c96e0 2d 31 20 20 20 20 42 79 74 65 2d 32 20 20 20 20  -1    Byte-2    
c96f0 42 79 74 65 2d 33 20 20 20 20 56 61 6c 75 65 0a  Byte-3    Value.
c9700 2a 2a 20 20 30 78 78 78 78 78 78 78 20 20 20 20  **  0xxxxxxx    
c9710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9720 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30               000
c9730 30 30 30 30 30 20 30 30 30 30 30 30 30 30 20 30  00000 00000000 0
c9740 78 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 30 79  xxxxxxx.**  110y
c9750 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20  yyyy  10xxxxxx  
c9760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9770 20 20 20 20 20 30 30 30 30 30 30 30 30 20 30 30       00000000 00
c9780 30 30 30 79 79 79 20 79 79 78 78 78 78 78 78 0a  000yyy yyxxxxxx.
c9790 2a 2a 20 20 31 31 31 30 7a 7a 7a 7a 20 20 31 30  **  1110zzzz  10
c97a0 79 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78  yyyyyy  10xxxxxx
c97b0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30               000
c97c0 30 30 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79  00000 zzzzyyyy y
c97d0 79 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 31  yxxxxxx.**  1111
c97e0 30 75 75 75 20 20 31 30 75 75 7a 7a 7a 7a 20 20  0uuu  10uuzzzz  
c97f0 31 30 79 79 79 79 79 79 20 20 31 30 78 78 78 78  10yyyyyy  10xxxx
c9800 78 78 20 20 20 30 30 30 75 75 75 75 75 20 7a 7a  xx   000uuuuu zz
c9810 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a  zzyyyy yyxxxxxx.
c9820 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f  **.**.** Notes o
c9830 6e 20 55 54 46 2d 31 36 3a 20 20 28 77 69 74 68  n UTF-16:  (with
c9840 20 77 77 77 77 2b 31 3d 3d 75 75 75 75 75 29 0a   wwww+1==uuuuu).
c9850 2a 2a 0a 2a 2a 20 20 20 20 20 20 57 6f 72 64 2d  **.**      Word-
c9860 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
c9870 57 6f 72 64 2d 31 20 20 20 20 20 20 20 20 20 20  Word-1          
c9880 56 61 6c 75 65 0a 2a 2a 20 20 31 31 30 31 31 30  Value.**  110110
c9890 77 77 20 77 77 7a 7a 7a 7a 79 79 20 20 20 31 31  ww wwzzzzyy   11
c98a0 30 31 31 31 79 79 20 79 79 78 78 78 78 78 78 20  0111yy yyxxxxxx 
c98b0 20 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a     000uuuuu zzzz
c98c0 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a  yyyy yyxxxxxx.**
c98d0 20 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78    zzzzyyyy yyxxx
c98e0 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20  xxx             
c98f0 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30             00000
c9900 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78  000 zzzzyyyy yyx
c9910 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42  xxxxx.**.**.** B
c9920 4f 4d 20 6f 72 20 42 79 74 65 20 4f 72 64 65 72  OM or Byte Order
c9930 20 4d 61 72 6b 3a 0a 2a 2a 20 20 20 20 20 30 78   Mark:.**     0x
c9940 66 66 20 30 78 66 65 20 20 20 6c 69 74 74 6c 65  ff 0xfe   little
c9950 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31 36 20 66  -endian utf-16 f
c9960 6f 6c 6c 6f 77 73 0a 2a 2a 20 20 20 20 20 30 78  ollows.**     0x
c9970 66 65 20 30 78 66 66 20 20 20 62 69 67 2d 65 6e  fe 0xff   big-en
c9980 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c  dian utf-16 foll
c9990 6f 77 73 0a 2a 2a 0a 2a 2f 0a 2f 2a 20 23 69 6e  ows.**.*/./* #in
c99a0 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e  clude <assert.h>
c99b0 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
c99c0 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
c99d0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
c99e0 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 76 61  wing constant va
c99f0 6c 75 65 20 69 73 20 75 73 65 64 20 62 79 20 74  lue is used by t
c9a00 68 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44  he SQLITE_BIGEND
c9a10 49 41 4e 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54  IAN and.** SQLIT
c9a20 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 6d  E_LITTLEENDIAN m
c9a30 61 63 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  acros..*/.SQLITE
c9a40 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69  _PRIVATE const i
c9a50 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20  nt sqlite3one = 
c9a60 31 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  1;.#endif /* SQL
c9a70 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
c9a80 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20   */../*.** This 
c9a90 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20  lookup table is 
c9aa0 75 73 65 64 20 74 6f 20 68 65 6c 70 20 64 65 63  used to help dec
c9ab0 6f 64 65 20 74 68 65 20 66 69 72 73 74 20 62 79  ode the first by
c9ac0 74 65 20 6f 66 0a 2a 2a 20 61 20 6d 75 6c 74 69  te of.** a multi
c9ad0 2d 62 79 74 65 20 55 54 46 38 20 63 68 61 72 61  -byte UTF8 chara
c9ae0 63 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  cter..*/.static 
c9af0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
c9b00 68 61 72 20 73 71 6c 69 74 65 33 55 74 66 38 54  har sqlite3Utf8T
c9b10 72 61 6e 73 31 5b 5d 20 3d 20 7b 0a 20 20 30 78  rans1[] = {.  0x
c9b20 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20  00, 0x01, 0x02, 
c9b30 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35  0x03, 0x04, 0x05
c9b40 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20  , 0x06, 0x07,.  
c9b50 30 78 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61  0x08, 0x09, 0x0a
c9b60 2c 20 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78  , 0x0b, 0x0c, 0x
c9b70 30 64 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a  0d, 0x0e, 0x0f,.
c9b80 20 20 30 78 31 30 2c 20 30 78 31 31 2c 20 30 78    0x10, 0x11, 0x
c9b90 31 32 2c 20 30 78 31 33 2c 20 30 78 31 34 2c 20  12, 0x13, 0x14, 
c9ba0 30 78 31 35 2c 20 30 78 31 36 2c 20 30 78 31 37  0x15, 0x16, 0x17
c9bb0 2c 0a 20 20 30 78 31 38 2c 20 30 78 31 39 2c 20  ,.  0x18, 0x19, 
c9bc0 30 78 31 61 2c 20 30 78 31 62 2c 20 30 78 31 63  0x1a, 0x1b, 0x1c
c9bd0 2c 20 30 78 31 64 2c 20 30 78 31 65 2c 20 30 78  , 0x1d, 0x1e, 0x
c9be0 31 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31  1f,.  0x00, 0x01
c9bf0 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78  , 0x02, 0x03, 0x
c9c00 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20  04, 0x05, 0x06, 
c9c10 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78  0x07,.  0x08, 0x
c9c20 30 39 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20  09, 0x0a, 0x0b, 
c9c30 30 78 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65  0x0c, 0x0d, 0x0e
c9c40 2c 20 30 78 30 66 2c 0a 20 20 30 78 30 30 2c 20  , 0x0f,.  0x00, 
c9c50 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33  0x01, 0x02, 0x03
c9c60 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78  , 0x04, 0x05, 0x
c9c70 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 30  06, 0x07,.  0x00
c9c80 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78  , 0x01, 0x02, 0x
c9c90 30 33 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20  03, 0x00, 0x01, 
c9ca0 30 78 30 30 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a  0x00, 0x00,.};..
c9cb0 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55  .#define WRITE_U
c9cc0 54 46 38 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20  TF8(zOut, c) {  
c9cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9ce0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20          \.  if( 
c9cf0 63 3c 30 78 30 30 30 38 30 20 29 7b 20 20 20 20  c<0x00080 ){    
c9d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9d20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
c9d30 20 28 75 38 29 28 63 26 30 78 46 46 29 3b 20 20   (u8)(c&0xFF);  
c9d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9d50 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20            \.  } 
c9d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9d90 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20     \.  else if( 
c9da0 63 3c 30 78 30 30 38 30 30 20 29 7b 20 20 20 20  c<0x00800 ){    
c9db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9dc0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
c9dd0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 43 30    *zOut++ = 0xC0
c9de0 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 26 30   + (u8)((c>>6)&0
c9df0 78 31 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x1F);           
c9e00 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
c9e10 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29  ++ = 0x80 + (u8)
c9e20 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20 20  (c & 0x3F);     
c9e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
c9e40 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20    }             
c9e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9e70 20 20 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20         \.  else 
c9e80 69 66 28 20 63 3c 30 78 31 30 30 30 30 20 29 7b  if( c<0x10000 ){
c9e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9eb0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
c9ec0 30 78 45 30 20 2b 20 28 75 38 29 28 28 63 3e 3e  0xE0 + (u8)((c>>
c9ed0 31 32 29 26 30 78 30 46 29 3b 20 20 20 20 20 20  12)&0x0F);      
c9ee0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
c9ef0 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20  zOut++ = 0x80 + 
c9f00 28 75 38 29 28 28 63 3e 3e 36 29 20 26 20 30 78  (u8)((c>>6) & 0x
c9f10 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3F);            
c9f20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
c9f30 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 20  = 0x80 + (u8)(c 
c9f40 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20  & 0x3F);        
c9f50 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
c9f60 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20  else{           
c9f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9f90 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
c9fa0 2b 20 3d 20 30 78 46 30 20 2b 20 28 75 38 29 28  + = 0xF0 + (u8)(
c9fb0 28 63 3e 3e 31 38 29 20 26 20 30 78 30 37 29 3b  (c>>18) & 0x07);
c9fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
c9fd0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38     *zOut++ = 0x8
c9fe0 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29  0 + (u8)((c>>12)
c9ff0 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20   & 0x3F);       
ca000 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
ca010 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38  t++ = 0x80 + (u8
ca020 29 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29  )((c>>6) & 0x3F)
ca030 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c  ;              \
ca040 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30  .    *zOut++ = 0
ca050 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30  x80 + (u8)(c & 0
ca060 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x3F);           
ca070 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20          \.  }   
ca080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca0b0 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52   \.}..#define WR
ca0c0 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 4f 75 74  ITE_UTF16LE(zOut
ca0d0 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20  , c) {          
ca0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca0f0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66            \.  if
ca100 28 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20  ( c<=0xFFFF ){  
ca110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca140 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
ca150 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b 20  (u8)(c&0x00FF); 
ca160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca180 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
ca190 74 2b 2b 20 3d 20 28 75 38 29 28 28 63 3e 3e 38  t++ = (u8)((c>>8
ca1a0 29 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20  )&0x00FF);      
ca1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca1c0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ca1d0 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20  }else{          
ca1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca210 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
ca220 3d 20 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26  = (u8)(((c>>10)&
ca230 30 78 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30  0x003F) + (((c-0
ca240 78 31 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30  x10000)>>10)&0x0
ca250 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a  0C0));  \.    *z
ca260 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30  Out++ = (u8)(0x0
ca270 30 44 38 20 2b 20 28 28 28 63 2d 30 78 31 30 30  0D8 + (((c-0x100
ca280 30 30 29 3e 3e 31 38 29 26 30 78 30 33 29 29 3b  00)>>18)&0x03));
ca290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ca2a0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
ca2b0 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20  8)(c&0x00FF);   
ca2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca2e0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
ca2f0 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 43 20  + = (u8)(0x00DC 
ca300 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29  + ((c>>8)&0x03))
ca310 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ca320 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20            \.  } 
ca330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca370 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49  \.}..#define WRI
ca380 54 45 5f 55 54 46 31 36 42 45 28 7a 4f 75 74 2c  TE_UTF16BE(zOut,
ca390 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 20   c) {           
ca3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca3b0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28           \.  if(
ca3c0 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20   c<=0xFFFF ){   
ca3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ca400 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
ca410 75 38 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46  u8)((c>>8)&0x00F
ca420 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
ca430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca440 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
ca450 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 30 30  ++ = (u8)(c&0x00
ca460 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  FF);            
ca470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca480 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
ca490 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20  else{           
ca4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca4d0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
ca4e0 20 28 75 38 29 28 30 78 30 30 44 38 20 2b 20 28   (u8)(0x00D8 + (
ca4f0 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38  ((c-0x10000)>>18
ca500 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20  )&0x03));       
ca510 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
ca520 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 28 63 3e  ut++ = (u8)(((c>
ca530 3e 31 30 29 26 30 78 30 30 33 46 29 20 2b 20 28  >10)&0x003F) + (
ca540 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30  ((c-0x10000)>>10
ca550 29 26 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20  )&0x00C0));  \. 
ca560 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38     *zOut++ = (u8
ca570 29 28 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e  )(0x00DC + ((c>>
ca580 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20  8)&0x03));      
ca590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca5a0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
ca5b0 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46   = (u8)(c&0x00FF
ca5c0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ca5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca5e0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20           \.  }  
ca5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ca630 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44  .}..#define READ
ca640 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20 54 45  _UTF16LE(zIn, TE
ca650 52 4d 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20  RM, c){         
ca660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca670 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20            \.  c 
ca680 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20  = (*zIn++);     
ca690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca6c0 20 20 5c 0a 20 20 63 20 2b 3d 20 28 28 2a 7a 49    \.  c += ((*zI
ca6d0 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20  n++)<<8);       
ca6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca700 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66            \.  if
ca710 28 20 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63  ( c>=0xD800 && c
ca720 3c 30 78 45 30 30 30 20 26 26 20 54 45 52 4d 20  <0xE000 && TERM 
ca730 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ){              
ca740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca750 20 20 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 3d    \.    int c2 =
ca760 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20   (*zIn++);      
ca770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca790 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ca7a0 63 32 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c  c2 += ((*zIn++)<
ca7b0 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  <8);            
ca7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca7e0 20 20 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 26    \.    c = (c2&
ca7f0 30 78 30 33 46 46 29 20 2b 20 28 28 63 26 30 78  0x03FF) + ((c&0x
ca800 30 30 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 28  003F)<<10) + (((
ca810 63 26 30 78 30 33 43 30 29 2b 30 78 30 30 34 30  c&0x03C0)+0x0040
ca820 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20  )<<10);   \.  } 
ca830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca870 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52    \.}..#define R
ca880 45 41 44 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c  EAD_UTF16BE(zIn,
ca890 20 54 45 52 4d 2c 20 63 29 7b 20 20 20 20 20 20   TERM, c){      
ca8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ca8c0 20 63 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c   c = ((*zIn++)<<
ca8d0 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  8);             
ca8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca900 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28 2a       \.  c += (*
ca910 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20  zIn++);         
ca920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca940 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ca950 20 69 66 28 20 63 3e 3d 30 78 44 38 30 30 20 26   if( c>=0xD800 &
ca960 26 20 63 3c 30 78 45 30 30 30 20 26 26 20 54 45  & c<0xE000 && TE
ca970 52 4d 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  RM ){           
ca980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca990 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 63       \.    int c
ca9a0 32 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38  2 = ((*zIn++)<<8
ca9b0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ca9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ca9e0 20 20 20 63 32 20 2b 3d 20 28 2a 7a 49 6e 2b 2b     c2 += (*zIn++
ca9f0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
caa00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
caa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
caa20 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 28       \.    c = (
caa30 63 32 26 30 78 30 33 46 46 29 20 2b 20 28 28 63  c2&0x03FF) + ((c
caa40 26 30 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20  &0x003F)<<10) + 
caa50 28 28 28 63 26 30 78 30 33 43 30 29 2b 30 78 30  (((c&0x03C0)+0x0
caa60 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20  040)<<10);   \. 
caa70 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20   }              
caa80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
caa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
caaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
caab0 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20       \.}../*.** 
caac0 54 72 61 6e 73 6c 61 74 65 20 61 20 73 69 6e 67  Translate a sing
caad0 6c 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  le UTF-8 charact
caae0 65 72 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  er.  Return the 
caaf0 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 2e 0a 2a  unicode value..*
cab00 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 74 72 61 6e  *.** During tran
cab10 73 6c 61 74 69 6f 6e 2c 20 61 73 73 75 6d 65 20  slation, assume 
cab20 74 68 61 74 20 74 68 65 20 62 79 74 65 20 74 68  that the byte th
cab30 61 74 20 7a 54 65 72 6d 20 70 6f 69 6e 74 73 0a  at zTerm points.
cab40 2a 2a 20 69 73 20 61 20 30 78 30 30 2e 0a 2a 2a  ** is a 0x00..**
cab50 0a 2a 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e  .** Write a poin
cab60 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ter to the next 
cab70 75 6e 72 65 61 64 20 62 79 74 65 20 62 61 63 6b  unread byte back
cab80 20 69 6e 74 6f 20 2a 70 7a 4e 65 78 74 2e 0a 2a   into *pzNext..*
cab90 2a 0a 2a 2a 20 4e 6f 74 65 73 20 4f 6e 20 49 6e  *.** Notes On In
caba0 76 61 6c 69 64 20 55 54 46 2d 38 3a 0a 2a 2a 0a  valid UTF-8:.**.
cabb0 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74  **  *  This rout
cabc0 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73  ine never allows
cabd0 20 61 20 37 2d 62 69 74 20 63 68 61 72 61 63 74   a 7-bit charact
cabe0 65 72 20 28 30 78 30 30 20 74 68 72 6f 75 67 68  er (0x00 through
cabf0 20 30 78 37 66 29 20 74 6f 0a 2a 2a 20 20 20 20   0x7f) to.**    
cac00 20 62 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61   be encoded as a
cac10 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72   multi-byte char
cac20 61 63 74 65 72 2e 20 20 41 6e 79 20 6d 75 6c 74  acter.  Any mult
cac30 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72  i-byte character
cac40 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 61 74 74   that.**     att
cac50 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20  empts to encode 
cac60 61 20 76 61 6c 75 65 20 62 65 74 77 65 65 6e 20  a value between 
cac70 30 78 30 30 20 61 6e 64 20 30 78 37 66 20 69 73  0x00 and 0x7f is
cac80 20 72 65 6e 64 65 72 65 64 20 61 73 20 30 78 66   rendered as 0xf
cac90 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54  ffd..**.**  *  T
caca0 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65  his routine neve
cacb0 72 20 61 6c 6c 6f 77 73 20 61 20 55 54 46 31 36  r allows a UTF16
cacc0 20 73 75 72 72 6f 67 61 74 65 20 76 61 6c 75 65   surrogate value
cacd0 20 74 6f 20 62 65 20 65 6e 63 6f 64 65 64 2e 0a   to be encoded..
cace0 2a 2a 20 20 20 20 20 49 66 20 61 20 6d 75 6c 74  **     If a mult
cacf0 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72  i-byte character
cad00 20 61 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63   attempts to enc
cad10 6f 64 65 20 61 20 76 61 6c 75 65 20 62 65 74 77  ode a value betw
cad20 65 65 6e 0a 2a 2a 20 20 20 20 20 30 78 64 38 30  een.**     0xd80
cad30 30 20 61 6e 64 20 30 78 65 30 30 30 20 74 68 65  0 and 0xe000 the
cad40 6e 20 69 74 20 69 73 20 72 65 6e 64 65 72 65 64  n it is rendered
cad50 20 61 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a   as 0xfffd..**.*
cad60 2a 20 20 2a 20 20 42 79 74 65 73 20 69 6e 20 74  *  *  Bytes in t
cad70 68 65 20 72 61 6e 67 65 20 6f 66 20 30 78 38 30  he range of 0x80
cad80 20 74 68 72 6f 75 67 68 20 30 78 62 66 20 77 68   through 0xbf wh
cad90 69 63 68 20 6f 63 63 75 72 20 61 73 20 74 68 65  ich occur as the
cada0 20 66 69 72 73 74 0a 2a 2a 20 20 20 20 20 62 79   first.**     by
cadb0 74 65 20 6f 66 20 61 20 63 68 61 72 61 63 74 65  te of a characte
cadc0 72 20 61 72 65 20 69 6e 74 65 72 70 72 65 74 65  r are interprete
cadd0 64 20 61 73 20 73 69 6e 67 6c 65 2d 62 79 74 65  d as single-byte
cade0 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 20   characters.**  
cadf0 20 20 20 61 6e 64 20 72 65 6e 64 65 72 65 64 20     and rendered 
cae00 61 73 20 74 68 65 6d 73 65 6c 76 65 73 20 65 76  as themselves ev
cae10 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79 20 61  en though they a
cae20 72 65 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a  re technically.*
cae30 2a 20 20 20 20 20 69 6e 76 61 6c 69 64 20 63 68  *     invalid ch
cae40 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  aracters..**.** 
cae50 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65   *  This routine
cae60 20 61 63 63 65 70 74 73 20 61 6e 20 69 6e 66 69   accepts an infi
cae70 6e 69 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 64  nite number of d
cae80 69 66 66 65 72 65 6e 74 20 55 54 46 38 20 65 6e  ifferent UTF8 en
cae90 63 6f 64 69 6e 67 73 0a 2a 2a 20 20 20 20 20 66  codings.**     f
caea0 6f 72 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65  or unicode value
caeb0 73 20 30 78 38 30 20 61 6e 64 20 67 72 65 61 74  s 0x80 and great
caec0 65 72 2e 20 20 49 74 20 64 6f 20 6e 6f 74 20 63  er.  It do not c
caed0 68 61 6e 67 65 20 6f 76 65 72 2d 6c 65 6e 67 74  hange over-lengt
caee0 68 0a 2a 2a 20 20 20 20 20 65 6e 63 6f 64 69 6e  h.**     encodin
caef0 67 73 20 74 6f 20 30 78 66 66 66 64 20 61 73 20  gs to 0xfffd as 
caf00 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 72 65 63  some systems rec
caf10 6f 6d 6d 65 6e 64 2e 0a 2a 2f 0a 23 64 65 66 69  ommend..*/.#defi
caf20 6e 65 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e  ne READ_UTF8(zIn
caf30 2c 20 7a 54 65 72 6d 2c 20 63 29 20 20 20 20 20  , zTerm, c)     
caf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
caf50 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 2a 28        \.  c = *(
caf60 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20  zIn++);         
caf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
caf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
caf90 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78     \.  if( c>=0x
cafa0 63 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  c0 ){           
cafb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cafc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cafd0 5c 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65  \.    c = sqlite
cafe0 33 55 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78  3Utf8Trans1[c-0x
caff0 63 30 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  c0];            
cb000 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
cb010 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a     while( zIn!=z
cb020 54 65 72 6d 20 26 26 20 28 2a 7a 49 6e 20 26 20  Term && (*zIn & 
cb030 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 20  0xc0)==0x80 ){  
cb040 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
cb050 20 20 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28    c = (c<<6) + (
cb060 30 78 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29  0x3f & *(zIn++))
cb070 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
cb080 20 20 20 20 20 20 20 5c 0a 20 20 20 20 7d 20 20         \.    }  
cb090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb0c0 20 20 20 20 5c 0a 20 20 20 20 69 66 28 20 63 3c      \.    if( c<
cb0d0 30 78 38 30 20 20 20 20 20 20 20 20 20 20 20 20  0x80            
cb0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb100 20 5c 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63   \.        || (c
cb110 26 30 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78  &0xFFFFF800)==0x
cb120 44 38 30 30 20 20 20 20 20 20 20 20 20 20 20 20  D800            
cb130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
cb140 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78          || (c&0x
cb150 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46  FFFFFFFE)==0xFFF
cb160 45 20 29 7b 20 20 63 20 3d 20 30 78 46 46 46 44  E ){  c = 0xFFFD
cb170 3b 20 7d 20 20 20 20 20 20 20 20 5c 0a 20 20 7d  ; }        \.  }
cb180 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
cb190 75 33 32 20 73 71 6c 69 74 65 33 55 74 66 38 52  u32 sqlite3Utf8R
cb1a0 65 61 64 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73  ead(.  const uns
cb1b0 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 70 7a 20  igned char **pz 
cb1c0 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
cb1d0 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 77 68 69   string from whi
cb1e0 63 68 20 74 6f 20 72 65 61 64 20 63 68 61 72 20  ch to read char 
cb1f0 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  */.){.  unsigned
cb200 20 69 6e 74 20 63 3b 0a 0a 20 20 2f 2a 20 53 61   int c;..  /* Sa
cb210 6d 65 20 61 73 20 52 45 41 44 5f 55 54 46 38 28  me as READ_UTF8(
cb220 29 20 61 62 6f 76 65 20 62 75 74 20 77 69 74 68  ) above but with
cb230 6f 75 74 20 74 68 65 20 7a 54 65 72 6d 20 70 61  out the zTerm pa
cb240 72 61 6d 65 74 65 72 2e 0a 20 20 2a 2a 20 46 6f  rameter..  ** Fo
cb250 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20  r this routine, 
cb260 77 65 20 61 73 73 75 6d 65 20 74 68 65 20 55 54  we assume the UT
cb270 46 38 20 73 74 72 69 6e 67 20 69 73 20 61 6c 77  F8 string is alw
cb280 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  ays zero-termina
cb290 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 63 20 3d 20  ted..  */.  c = 
cb2a0 2a 28 28 2a 70 7a 29 2b 2b 29 3b 0a 20 20 69 66  *((*pz)++);.  if
cb2b0 28 20 63 3e 3d 30 78 63 30 20 29 7b 0a 20 20 20  ( c>=0xc0 ){.   
cb2c0 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38   c = sqlite3Utf8
cb2d0 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 0a  Trans1[c-0xc0];.
cb2e0 20 20 20 20 77 68 69 6c 65 28 20 28 2a 28 2a 70      while( (*(*p
cb2f0 7a 29 20 26 20 30 78 63 30 29 3d 3d 30 78 38 30  z) & 0xc0)==0x80
cb300 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 28 63   ){.      c = (c
cb310 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26 20 2a  <<6) + (0x3f & *
cb320 28 28 2a 70 7a 29 2b 2b 29 29 3b 0a 20 20 20 20  ((*pz)++));.    
cb330 7d 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38 30  }.    if( c<0x80
cb340 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30  .        || (c&0
cb350 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38  xFFFFF800)==0xD8
cb360 30 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63  00.        || (c
cb370 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78  &0xFFFFFFFE)==0x
cb380 46 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78 46  FFFE ){  c = 0xF
cb390 46 46 44 3b 20 7d 0a 20 20 7d 0a 20 20 72 65 74  FFD; }.  }.  ret
cb3a0 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a 0a  urn c;.}...../*.
cb3b0 2a 2a 20 49 66 20 74 68 65 20 54 52 41 4e 53 4c  ** If the TRANSL
cb3c0 41 54 45 5f 54 52 41 43 45 20 6d 61 63 72 6f 20  ATE_TRACE macro 
cb3d0 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 20  is defined, the 
cb3e0 76 61 6c 75 65 20 6f 66 20 65 61 63 68 20 4d 65  value of each Me
cb3f0 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 64 20  m is.** printed 
cb400 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 68 65  on stderr on the
cb410 20 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 6f 75   way into and ou
cb420 74 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65  t of sqlite3Vdbe
cb430 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 2e 0a  MemTranslate()..
cb440 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 20 54  */ ./* #define T
cb450 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 31  RANSLATE_TRACE 1
cb460 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
cb470 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f  ITE_OMIT_UTF16./
cb480 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
cb490 65 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 68 65  e transforms the
cb4a0 20 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 20 65   internal text e
cb4b0 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20  ncoding used by 
cb4c0 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 69 72  pMem to.** desir
cb4d0 65 64 45 6e 63 2e 20 49 74 20 69 73 20 61 6e 20  edEnc. It is an 
cb4e0 65 72 72 6f 72 20 69 66 20 74 68 65 20 73 74 72  error if the str
cb4f0 69 6e 67 20 69 73 20 61 6c 72 65 61 64 79 20 6f  ing is already o
cb500 66 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a  f the desired.**
cb510 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 69 66   encoding, or if
cb520 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f 74 20   *pMem does not 
cb530 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e 67  contain a string
cb540 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54   value..*/.SQLIT
cb550 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
cb560 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e  lite3VdbeMemTran
cb570 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c  slate(Mem *pMem,
cb580 20 75 38 20 64 65 73 69 72 65 64 45 6e 63 29 7b   u8 desiredEnc){
cb590 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20  .  int len;     
cb5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
cb5b0 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  * Maximum length
cb5c0 20 6f 66 20 6f 75 74 70 75 74 20 73 74 72 69 6e   of output strin
cb5d0 67 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  g in bytes */.  
cb5e0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
cb5f0 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  Out;            
cb600 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20        /* Output 
cb610 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e 73 69  buffer */.  unsi
cb620 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20  gned char *zIn; 
cb630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb640 20 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 72 61    /* Input itera
cb650 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  tor */.  unsigne
cb660 64 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20  d char *zTerm;  
cb670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
cb680 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75 74 20 2a  * End of input *
cb690 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
cb6a0 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20  r *z;           
cb6b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74            /* Out
cb6c0 70 75 74 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a  put iterator */.
cb6d0 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63    unsigned int c
cb6e0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  ;..  assert( pMe
cb6f0 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
cb700 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
cb710 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
cb720 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  );.  assert( pMe
cb730 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72  m->flags&MEM_Str
cb740 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d   );.  assert( pM
cb750 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 64  em->enc!=desired
cb760 45 6e 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Enc );.  assert(
cb770 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20 29 3b   pMem->enc!=0 );
cb780 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d  .  assert( pMem-
cb790 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20 64 65  >n>=0 );..#if de
cb7a0 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45 5f  fined(TRANSLATE_
cb7b0 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e 65  TRACE) && define
cb7c0 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
cb7d0 20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75    {.    char zBu
cb7e0 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69  f[100];.    sqli
cb7f0 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79  te3VdbeMemPretty
cb800 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75 66  Print(pMem, zBuf
cb810 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73  );.    fprintf(s
cb820 74 64 65 72 72 2c 20 22 49 4e 50 55 54 3a 20 20  tderr, "INPUT:  
cb830 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 20  %s\n", zBuf);.  
cb840 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
cb850 66 20 74 68 65 20 74 72 61 6e 73 6c 61 74 69 6f  f the translatio
cb860 6e 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46  n is between UTF
cb870 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e 64 20 62  -16 little and b
cb880 69 67 20 65 6e 64 69 61 6e 2c 20 74 68 65 6e 20  ig endian, then 
cb890 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69  .  ** all that i
cb8a0 73 20 72 65 71 75 69 72 65 64 20 69 73 20 74 6f  s required is to
cb8b0 20 73 77 61 70 20 74 68 65 20 62 79 74 65 20 6f   swap the byte o
cb8c0 72 64 65 72 2e 20 54 68 69 73 20 63 61 73 65 20  rder. This case 
cb8d0 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a 2a 20  is handled.  ** 
cb8e0 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f 6d  differently from
cb8f0 20 74 68 65 20 6f 74 68 65 72 73 2e 0a 20 20 2a   the others..  *
cb900 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e  /.  if( pMem->en
cb910 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26  c!=SQLITE_UTF8 &
cb920 26 20 64 65 73 69 72 65 64 45 6e 63 21 3d 53 51  & desiredEnc!=SQ
cb930 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20  LITE_UTF8 ){.   
cb940 20 75 38 20 74 65 6d 70 3b 0a 20 20 20 20 69 6e   u8 temp;.    in
cb950 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73  t rc;.    rc = s
cb960 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b  qlite3VdbeMemMak
cb970 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29  eWriteable(pMem)
cb980 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
cb990 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
cb9a0 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
cb9b0 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20  ITE_NOMEM );.   
cb9c0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
cb9d0 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
cb9e0 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65    zIn = (u8*)pMe
cb9f0 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72 6d 20  m->z;.    zTerm 
cba00 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 26 7e  = &zIn[pMem->n&~
cba10 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a  1];.    while( z
cba20 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20  In<zTerm ){.    
cba30 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b 0a 20    temp = *zIn;. 
cba40 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 7a 49       *zIn = *(zI
cba50 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 6e 2b  n+1);.      zIn+
cba60 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b 2b 20  +;.      *zIn++ 
cba70 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20  = temp;.    }.  
cba80 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65    pMem->enc = de
cba90 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 67 6f  siredEnc;.    go
cbaa0 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74  to translate_out
cbab0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20  ;.  }..  /* Set 
cbac0 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 69 6d  len to the maxim
cbad0 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  um number of byt
cbae0 65 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 74  es required in t
cbaf0 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72  he output buffer
cbb00 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 69 72  . */.  if( desir
cbb10 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  edEnc==SQLITE_UT
cbb20 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65  F8 ){.    /* Whe
cbb30 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f  n converting fro
cbb40 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 20 6d 61  m UTF-16, the ma
cbb50 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 65 73  ximum growth res
cbb60 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a  ults from.    **
cbb70 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20 32   translating a 2
cbb80 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20  -byte character 
cbb90 74 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46 2d  to a 4-byte UTF-
cbba0 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20  8 character..   
cbbb0 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79 74   ** A single byt
cbbc0 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f  e is required fo
cbbd0 72 20 74 68 65 20 6f 75 74 70 75 74 20 73 74 72  r the output str
cbbe0 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74  ing.    ** nul-t
cbbf0 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a  erminator..    *
cbc00 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 26 3d  /.    pMem->n &=
cbc10 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70   ~1;.    len = p
cbc20 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b 0a  Mem->n * 2 + 1;.
cbc30 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
cbc40 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20  When converting 
cbc50 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54  from UTF-8 to UT
cbc60 46 2d 31 36 20 74 68 65 20 6d 61 78 69 6d 75 6d  F-16 the maximum
cbc70 20 67 72 6f 77 74 68 20 69 73 20 63 61 75 73 65   growth is cause
cbc80 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 61 20  d.    ** when a 
cbc90 31 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 61  1-byte UTF-8 cha
cbca0 72 61 63 74 65 72 20 69 73 20 74 72 61 6e 73 6c  racter is transl
cbcb0 61 74 65 64 20 69 6e 74 6f 20 61 20 32 2d 62 79  ated into a 2-by
cbcc0 74 65 20 55 54 46 2d 31 36 0a 20 20 20 20 2a 2a  te UTF-16.    **
cbcd0 20 63 68 61 72 61 63 74 65 72 2e 20 54 77 6f 20   character. Two 
cbce0 62 79 74 65 73 20 61 72 65 20 72 65 71 75 69 72  bytes are requir
cbcf0 65 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74  ed in the output
cbd00 20 62 75 66 66 65 72 20 66 6f 72 20 74 68 65 0a   buffer for the.
cbd10 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69      ** nul-termi
cbd20 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  nator..    */.  
cbd30 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20    len = pMem->n 
cbd40 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20 20  * 2 + 2;.  }..  
cbd50 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 70 6f  /* Set zIn to po
cbd60 69 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 74  int at the start
cbd70 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62 75   of the input bu
cbd80 66 66 65 72 20 61 6e 64 20 7a 54 65 72 6d 20 74  ffer and zTerm t
cbd90 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a 20 62  o point 1.  ** b
cbda0 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64  yte past the end
cbdb0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 69  ..  **.  ** Vari
cbdc0 61 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65 74  able zOut is set
cbdd0 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
cbde0 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c 20   output buffer, 
cbdf0 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0a 20  space obtained. 
cbe00 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33   ** from sqlite3
cbe10 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a 2f 0a  _malloc()..  */.
cbe20 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65    zIn = (u8*)pMe
cbe30 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d 20  m->z;.  zTerm = 
cbe40 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20  &zIn[pMem->n];. 
cbe50 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 44   zOut = sqlite3D
cbe60 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d  bMallocRaw(pMem-
cbe70 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 66 28  >db, len);.  if(
cbe80 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 72 65   !zOut ){.    re
cbe90 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
cbea0 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a 4f 75  M;.  }.  z = zOu
cbeb0 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e  t;..  if( pMem->
cbec0 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc==SQLITE_UTF8
cbed0 20 29 7b 0a 20 20 20 20 69 66 28 20 64 65 73 69   ){.    if( desi
cbee0 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  redEnc==SQLITE_U
cbef0 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20  TF16LE ){.      
cbf00 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d  /* UTF-8 -> UTF-
cbf10 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e  16 Little-endian
cbf20 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28   */.      while(
cbf30 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20   zIn<zTerm ){.  
cbf40 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 28        READ_UTF8(
cbf50 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a  zIn, zTerm, c);.
cbf60 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54          WRITE_UT
cbf70 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20  F16LE(z, c);.   
cbf80 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
cbf90 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 65        assert( de
cbfa0 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45  siredEnc==SQLITE
cbfb0 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 20 20  _UTF16BE );.    
cbfc0 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54    /* UTF-8 -> UT
cbfd0 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 6e 20  F-16 Big-endian 
cbfe0 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  */.      while( 
cbff0 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  zIn<zTerm ){.   
cc000 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 28 7a       READ_UTF8(z
cc010 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20  In, zTerm, c);. 
cc020 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 46         WRITE_UTF
cc030 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20  16BE(z, c);.    
cc040 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d    }.    }.    pM
cc050 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20  em->n = (int)(z 
cc060 2d 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2a 7a 2b  - zOut);.    *z+
cc070 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  + = 0;.  }else{.
cc080 20 20 20 20 61 73 73 65 72 74 28 20 64 65 73 69      assert( desi
cc090 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  redEnc==SQLITE_U
cc0a0 54 46 38 20 29 3b 0a 20 20 20 20 69 66 28 20 70  TF8 );.    if( p
cc0b0 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45  Mem->enc==SQLITE
cc0c0 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20  _UTF16LE ){.    
cc0d0 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c 69 74 74    /* UTF-16 Litt
cc0e0 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46  le-endian -> UTF
cc0f0 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c  -8 */.      whil
cc100 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a  e( zIn<zTerm ){.
cc110 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46          READ_UTF
cc120 31 36 4c 45 28 7a 49 6e 2c 20 7a 49 6e 3c 7a 54  16LE(zIn, zIn<zT
cc130 65 72 6d 2c 20 63 29 3b 20 0a 20 20 20 20 20 20  erm, c); .      
cc140 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20    WRITE_UTF8(z, 
cc150 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c);.      }.    
cc160 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
cc170 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61  UTF-16 Big-endia
cc180 6e 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20  n -> UTF-8 */.  
cc190 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a      while( zIn<z
cc1a0 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  Term ){.        
cc1b0 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 49 6e  READ_UTF16BE(zIn
cc1c0 2c 20 7a 49 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b  , zIn<zTerm, c);
cc1d0 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f   .        WRITE_
cc1e0 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20  UTF8(z, c);.    
cc1f0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d    }.    }.    pM
cc200 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20  em->n = (int)(z 
cc210 2d 20 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a  - zOut);.  }.  *
cc220 7a 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28  z = 0;.  assert(
cc230 20 28 70 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72   (pMem->n+(desir
cc240 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  edEnc==SQLITE_UT
cc250 46 38 3f 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b  F8?1:2))<=len );
cc260 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ..  sqlite3VdbeM
cc270 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b  emRelease(pMem);
cc280 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  .  pMem->flags &
cc290 3d 20 7e 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d  = ~(MEM_Static|M
cc2a0 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d  EM_Dyn|MEM_Ephem
cc2b0 29 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d  );.  pMem->enc =
cc2c0 20 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 70   desiredEnc;.  p
cc2d0 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d  Mem->flags |= (M
cc2e0 45 4d 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29  EM_Term|MEM_Dyn)
cc2f0 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63  ;.  pMem->z = (c
cc300 68 61 72 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65  har*)zOut;.  pMe
cc310 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65  m->zMalloc = pMe
cc320 6d 2d 3e 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65  m->z;..translate
cc330 5f 6f 75 74 3a 0a 23 69 66 20 64 65 66 69 6e 65  _out:.#if define
cc340 64 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43  d(TRANSLATE_TRAC
cc350 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  E) && defined(SQ
cc360 4c 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a  LITE_DEBUG).  {.
cc370 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30      char zBuf[10
cc380 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0];.    sqlite3V
cc390 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e  dbeMemPrettyPrin
cc3a0 74 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20  t(pMem, zBuf);. 
cc3b0 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72     fprintf(stder
cc3c0 72 2c 20 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e  r, "OUTPUT: %s\n
cc3d0 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65  ", zBuf);.  }.#e
cc3e0 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51  ndif.  return SQ
cc3f0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
cc400 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
cc410 68 65 63 6b 73 20 66 6f 72 20 61 20 62 79 74 65  hecks for a byte
cc420 2d 6f 72 64 65 72 20 6d 61 72 6b 20 61 74 20 74  -order mark at t
cc430 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
cc440 74 68 65 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73  the .** UTF-16 s
cc450 74 72 69 6e 67 20 73 74 6f 72 65 64 20 69 6e 20  tring stored in 
cc460 2a 70 4d 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73  *pMem. If one is
cc470 20 70 72 65 73 65 6e 74 2c 20 69 74 20 69 73 20   present, it is 
cc480 72 65 6d 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74  removed and.** t
cc490 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74  he encoding of t
cc4a0 68 65 20 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e  he Mem adjusted.
cc4b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f   This routine do
cc4c0 65 73 20 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a  es not do any.**
cc4d0 20 62 79 74 65 2d 73 77 61 70 70 69 6e 67 2c 20   byte-swapping, 
cc4e0 69 74 20 6a 75 73 74 20 73 65 74 73 20 4d 65 6d  it just sets Mem
cc4f0 2e 65 6e 63 20 61 70 70 72 6f 70 72 69 61 74 65  .enc appropriate
cc500 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c  ly..**.** The al
cc510 6c 6f 63 61 74 69 6f 6e 20 28 73 74 61 74 69 63  location (static
cc520 2c 20 64 79 6e 61 6d 69 63 20 65 74 63 2e 29 20  , dynamic etc.) 
cc530 61 6e 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  and encoding of 
cc540 74 68 65 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a  the Mem may be.*
cc550 2a 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69  * changed by thi
cc560 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  s function..*/.S
cc570 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
cc580 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
cc590 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70  HandleBom(Mem *p
cc5a0 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  Mem){.  int rc =
cc5b0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38   SQLITE_OK;.  u8
cc5c0 20 62 6f 6d 20 3d 20 30 3b 0a 0a 20 20 61 73 73   bom = 0;..  ass
cc5d0 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20  ert( pMem->n>=0 
cc5e0 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e  );.  if( pMem->n
cc5f0 3e 31 20 29 7b 0a 20 20 20 20 75 38 20 62 31 20  >1 ){.    u8 b1 
cc600 3d 20 2a 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a  = *(u8 *)pMem->z
cc610 3b 0a 20 20 20 20 75 38 20 62 32 20 3d 20 2a 28  ;.    u8 b2 = *(
cc620 28 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20  ((u8 *)pMem->z) 
cc630 2b 20 31 29 3b 0a 20 20 20 20 69 66 28 20 62 31  + 1);.    if( b1
cc640 3d 3d 30 78 46 45 20 26 26 20 62 32 3d 3d 30 78  ==0xFE && b2==0x
cc650 46 46 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20  FF ){.      bom 
cc660 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  = SQLITE_UTF16BE
cc670 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
cc680 62 31 3d 3d 30 78 46 46 20 26 26 20 62 32 3d 3d  b1==0xFF && b2==
cc690 30 78 46 45 20 29 7b 0a 20 20 20 20 20 20 62 6f  0xFE ){.      bo
cc6a0 6d 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36  m = SQLITE_UTF16
cc6b0 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  LE;.    }.  }.  
cc6c0 0a 20 20 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20  .  if( bom ){.  
cc6d0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
cc6e0 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
cc6f0 6c 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66  le(pMem);.    if
cc700 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
cc710 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e  ){.      pMem->n
cc720 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d   -= 2;.      mem
cc730 6d 6f 76 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70  move(pMem->z, &p
cc740 4d 65 6d 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d  Mem->z[2], pMem-
cc750 3e 6e 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  >n);.      pMem-
cc760 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c  >z[pMem->n] = '\
cc770 30 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  0';.      pMem->
cc780 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27  z[pMem->n+1] = '
cc790 5c 30 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  \0';.      pMem-
cc7a0 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65  >flags |= MEM_Te
cc7b0 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  rm;.      pMem->
cc7c0 65 6e 63 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d  enc = bom;.    }
cc7d0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
cc7e0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
cc7f0 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
cc800 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20  */../*.** pZ is 
cc810 61 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  a UTF-8 encoded 
cc820 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20  unicode string. 
cc830 49 66 20 6e 42 79 74 65 20 69 73 20 6c 65 73 73  If nByte is less
cc840 20 74 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72   than zero,.** r
cc850 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
cc860 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72   of unicode char
cc870 61 63 74 65 72 73 20 69 6e 20 70 5a 20 75 70 20  acters in pZ up 
cc880 74 6f 20 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c  to (but not incl
cc890 75 64 69 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69  uding).** the fi
cc8a0 72 73 74 20 30 78 30 30 20 62 79 74 65 2e 20 49  rst 0x00 byte. I
cc8b0 66 20 6e 42 79 74 65 20 69 73 20 6e 6f 74 20 6c  f nByte is not l
cc8c0 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72  ess than zero, r
cc8d0 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d  eturn the.** num
cc8e0 62 65 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 63  ber of unicode c
cc8f0 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65  haracters in the
cc900 20 66 69 72 73 74 20 6e 42 79 74 65 20 6f 66 20   first nByte of 
cc910 70 5a 20 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a  pZ (or up to .**
cc920 20 74 68 65 20 66 69 72 73 74 20 30 78 30 30 2c   the first 0x00,
cc930 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73   whichever comes
cc940 20 66 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49   first)..*/.SQLI
cc950 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
cc960 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65  qlite3Utf8CharLe
cc970 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49  n(const char *zI
cc980 6e 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  n, int nByte){. 
cc990 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f   int r = 0;.  co
cc9a0 6e 73 74 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e  nst u8 *z = (con
cc9b0 73 74 20 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f  st u8*)zIn;.  co
cc9c0 6e 73 74 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20  nst u8 *zTerm;. 
cc9d0 20 69 66 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b   if( nByte>=0 ){
cc9e0 0a 20 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b  .    zTerm = &z[
cc9f0 6e 42 79 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b  nByte];.  }else{
cca00 0a 20 20 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f  .    zTerm = (co
cca10 6e 73 74 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20  nst u8*)(-1);.  
cca20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a  }.  assert( z<=z
cca30 54 65 72 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28  Term );.  while(
cca40 20 2a 7a 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72   *z!=0 && z<zTer
cca50 6d 20 29 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f  m ){.    SQLITE_
cca60 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20  SKIP_UTF8(z);.  
cca70 20 20 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74    r++;.  }.  ret
cca80 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69  urn r;.}../* Thi
cca90 73 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20  s test function 
ccaa0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
ccab0 20 75 73 65 64 20 62 79 20 74 68 65 20 61 75 74   used by the aut
ccac0 6f 6d 61 74 65 64 20 74 65 73 74 2d 73 75 69 74  omated test-suit
ccad0 65 2e 20 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20  e. .** Hence it 
ccae0 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  is only availabl
ccaf0 65 20 69 6e 20 64 65 62 75 67 20 62 75 69 6c 64  e in debug build
ccb00 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  s..*/.#if define
ccb10 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26  d(SQLITE_TEST) &
ccb20 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
ccb30 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72  _DEBUG)./*.** Tr
ccb40 61 6e 73 6c 61 74 65 20 55 54 46 2d 38 20 74 6f  anslate UTF-8 to
ccb50 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68   UTF-8..**.** Th
ccb60 69 73 20 68 61 73 20 74 68 65 20 65 66 66 65 63  is has the effec
ccb70 74 20 6f 66 20 6d 61 6b 69 6e 67 20 73 75 72 65  t of making sure
ccb80 20 74 68 61 74 20 74 68 65 20 73 74 72 69 6e 67   that the string
ccb90 20 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a   is well-formed.
ccba0 2a 2a 20 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f  ** UTF-8.  Misco
ccbb0 64 65 64 20 63 68 61 72 61 63 74 65 72 73 20 61  ded characters a
ccbc0 72 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a  re removed..**.*
ccbd0 2a 20 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f  * The translatio
ccbe0 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61  n is done in-pla
ccbf0 63 65 20 61 6e 64 20 61 62 6f 72 74 65 64 20 69  ce and aborted i
ccc00 66 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20  f the output.** 
ccc10 6f 76 65 72 72 75 6e 73 20 74 68 65 20 69 6e 70  overruns the inp
ccc20 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ut..*/.SQLITE_PR
ccc30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
ccc40 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65  3Utf8To8(unsigne
ccc50 64 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20  d char *zIn){.  
ccc60 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
ccc70 4f 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73  Out = zIn;.  uns
ccc80 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 61  igned char *zSta
ccc90 72 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20  rt = zIn;.  u32 
ccca0 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e  c;..  while( zIn
cccb0 5b 30 5d 20 26 26 20 7a 4f 75 74 3c 3d 7a 49 6e  [0] && zOut<=zIn
cccc0 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c 69   ){.    c = sqli
cccd0 74 65 33 55 74 66 38 52 65 61 64 28 28 63 6f 6e  te3Utf8Read((con
ccce0 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 0a 20  st u8**)&zIn);. 
cccf0 20 20 20 69 66 28 20 63 21 3d 30 78 66 66 66 64     if( c!=0xfffd
ccd00 20 29 7b 0a 20 20 20 20 20 20 57 52 49 54 45 5f   ){.      WRITE_
ccd10 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 3b 0a 20  UTF8(zOut, c);. 
ccd20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f 75 74     }.  }.  *zOut
ccd30 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28   = 0;.  return (
ccd40 69 6e 74 29 28 7a 4f 75 74 20 2d 20 7a 53 74 61  int)(zOut - zSta
ccd50 72 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  rt);.}.#endif..#
ccd60 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
ccd70 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 43  IT_UTF16./*.** C
ccd80 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 31 36 20  onvert a UTF-16 
ccd90 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 6e 61  string in the na
ccda0 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 69 6e  tive encoding in
ccdb0 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e  to a UTF-8 strin
ccdc0 67 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f 20  g..** Memory to 
ccdd0 68 6f 6c 64 20 74 68 65 20 55 54 46 2d 38 20 73  hold the UTF-8 s
ccde0 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65  tring is obtaine
ccdf0 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
cce00 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 0a 2a  alloc and must.*
cce10 2a 20 62 65 20 66 72 65 65 64 20 62 79 20 74 68  * be freed by th
cce20 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
cce30 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 55 4c 4c 20 69  on..**.** NULL i
cce40 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68  s returned if th
cce50 65 72 65 20 69 73 20 61 6e 20 61 6c 6c 6f 63 61  ere is an alloca
cce60 74 69 6f 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53  tion error..*/.S
cce70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
cce80 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 31 36  ar *sqlite3Utf16
cce90 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  to8(sqlite3 *db,
ccea0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20   const void *z, 
cceb0 69 6e 74 20 6e 42 79 74 65 2c 20 75 38 20 65 6e  int nByte, u8 en
ccec0 63 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d  c){.  Mem m;.  m
cced0 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a  emset(&m, 0, siz
ccee0 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20  eof(m));.  m.db 
ccef0 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56  = db;.  sqlite3V
ccf00 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c  dbeMemSetStr(&m,
ccf10 20 7a 2c 20 6e 42 79 74 65 2c 20 65 6e 63 2c 20   z, nByte, enc, 
ccf20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
ccf30 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
ccf40 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20  ngeEncoding(&m, 
ccf50 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20  SQLITE_UTF8);.  
ccf60 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
ccf70 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  iled ){.    sqli
ccf80 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
ccf90 65 28 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d  e(&m);.    m.z =
ccfa0 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
ccfb0 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d  ( (m.flags & MEM
ccfc0 5f 54 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d  _Term)!=0 || db-
ccfd0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
ccfe0 0a 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c  .  assert( (m.fl
ccff0 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d  ags & MEM_Str)!=
cd000 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  0 || db->mallocF
cd010 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65 72  ailed );.  asser
cd020 74 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45  t( (m.flags & ME
cd030 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 64 62 2d  M_Dyn)!=0 || db-
cd040 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
cd050 0a 20 20 61 73 73 65 72 74 28 20 6d 2e 7a 20 7c  .  assert( m.z |
cd060 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
cd070 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d  ed );.  return m
cd080 2e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  .z;.}../*.** Con
cd090 76 65 72 74 20 61 20 55 54 46 2d 38 20 73 74 72  vert a UTF-8 str
cd0a0 69 6e 67 20 74 6f 20 74 68 65 20 55 54 46 2d 31  ing to the UTF-1
cd0b0 36 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 63 69  6 encoding speci
cd0c0 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65  fied by paramete
cd0d0 72 0a 2a 2a 20 65 6e 63 2e 20 41 20 70 6f 69 6e  r.** enc. A poin
cd0e0 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73  ter to the new s
cd0f0 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e 65  tring is returne
cd100 64 2c 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65  d, and the value
cd110 20 6f 66 20 2a 70 6e 4f 75 74 0a 2a 2a 20 69 73   of *pnOut.** is
cd120 20 73 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67   set to the leng
cd130 74 68 20 6f 66 20 74 68 65 20 72 65 74 75 72 6e  th of the return
cd140 65 64 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74  ed string in byt
cd150 65 73 2e 20 54 68 65 20 63 61 6c 6c 20 73 68 6f  es. The call sho
cd160 75 6c 64 0a 2a 2a 20 61 72 72 61 6e 67 65 20 74  uld.** arrange t
cd170 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62  o call sqlite3Db
cd180 46 72 65 65 28 29 20 6f 6e 20 74 68 65 20 72 65  Free() on the re
cd190 74 75 72 6e 65 64 20 70 6f 69 6e 74 65 72 20 77  turned pointer w
cd1a0 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 20  hen it is.** no 
cd1b0 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e  longer required.
cd1c0 0a 2a 2a 20 0a 2a 2a 20 49 66 20 61 20 6d 61 6c  .** .** If a mal
cd1d0 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75  loc failure occu
cd1e0 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  rs, NULL is retu
cd1f0 72 6e 65 64 20 61 6e 64 20 74 68 65 20 64 62 2e  rned and the db.
cd200 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 0a 2a 2a 20  mallocFailed.** 
cd210 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 69 66  flag set..*/.#if
cd220 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
cd230 45 5f 53 54 41 54 33 0a 53 51 4c 49 54 45 5f 50  E_STAT3.SQLITE_P
cd240 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
cd250 69 74 65 33 55 74 66 38 74 6f 31 36 28 73 71 6c  ite3Utf8to16(sql
cd260 69 74 65 33 20 2a 64 62 2c 20 75 38 20 65 6e 63  ite3 *db, u8 enc
cd270 2c 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e  , char *z, int n
cd280 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20  , int *pnOut){. 
cd290 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74   Mem m;.  memset
cd2a0 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d  (&m, 0, sizeof(m
cd2b0 29 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b  ));.  m.db = db;
cd2c0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
cd2d0 6d 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e  mSetStr(&m, z, n
cd2e0 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
cd2f0 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
cd300 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
cd310 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 26 6d 2c  MemTranslate(&m,
cd320 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 61 73 73   enc) ){.    ass
cd330 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ert( db->mallocF
cd340 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74  ailed );.    ret
cd350 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 0;.  }.  ass
cd360 65 72 74 28 20 6d 2e 7a 3d 3d 6d 2e 7a 4d 61 6c  ert( m.z==m.zMal
cd370 6c 6f 63 20 29 3b 0a 20 20 2a 70 6e 4f 75 74 20  loc );.  *pnOut 
cd380 3d 20 6d 2e 6e 3b 0a 20 20 72 65 74 75 72 6e 20  = m.n;.  return 
cd390 6d 2e 7a 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  m.z;.}.#endif../
cd3a0 2a 0a 2a 2a 20 7a 49 6e 20 69 73 20 61 20 55 54  *.** zIn is a UT
cd3b0 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 75 6e 69  F-16 encoded uni
cd3c0 63 6f 64 65 20 73 74 72 69 6e 67 20 61 74 20 6c  code string at l
cd3d0 65 61 73 74 20 6e 43 68 61 72 20 63 68 61 72 61  east nChar chara
cd3e0 63 74 65 72 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52  cters long..** R
cd3f0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
cd400 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65   of bytes in the
cd410 20 66 69 72 73 74 20 6e 43 68 61 72 20 75 6e 69   first nChar uni
cd420 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 0a  code characters.
cd430 2a 2a 20 69 6e 20 70 5a 2e 20 20 6e 43 68 61 72  ** in pZ.  nChar
cd440 20 6d 75 73 74 20 62 65 20 6e 6f 6e 2d 6e 65 67   must be non-neg
cd450 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ative..*/.SQLITE
cd460 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
cd470 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e  ite3Utf16ByteLen
cd480 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e  (const void *zIn
cd490 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20  , int nChar){.  
cd4a0 69 6e 74 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65  int c;.  unsigne
cd4b0 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20  d char const *z 
cd4c0 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74 20 6e 20 3d  = zIn;.  int n =
cd4d0 20 30 3b 0a 20 20 0a 20 20 69 66 28 20 53 51 4c   0;.  .  if( SQL
cd4e0 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d  ITE_UTF16NATIVE=
cd4f0 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
cd500 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 3c  ){.    while( n<
cd510 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 52  nChar ){.      R
cd520 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 31  EAD_UTF16BE(z, 1
cd530 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b  , c);.      n++;
cd540 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
cd550 20 20 20 20 77 68 69 6c 65 28 20 6e 3c 6e 43 68      while( n<nCh
cd560 61 72 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44  ar ){.      READ
cd570 5f 55 54 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63  _UTF16LE(z, 1, c
cd580 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20  );.      n++;.  
cd590 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
cd5a0 20 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 67 6e   (int)(z-(unsign
cd5b0 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 29  ed char const *)
cd5c0 7a 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64 65 66  zIn);.}..#if def
cd5d0 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
cd5e0 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  )./*.** This rou
cd5f0 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66  tine is called f
cd600 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73 74  rom the TCL test
cd610 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e 73   function "trans
cd620 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e 0a  late_selftest"..
cd630 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68 61  ** It checks tha
cd640 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 73  t the primitives
cd650 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67   for serializing
cd660 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a 69   and deserializi
cd670 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73  ng.** characters
cd680 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69 6e   in each encodin
cd690 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 20 6f  g are inverses o
cd6a0 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f  f each other..*/
cd6b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
cd6c0 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66 53  void sqlite3UtfS
cd6d0 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a 20  elfTest(void){. 
cd6e0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c   unsigned int i,
cd6f0 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   t;.  unsigned c
cd700 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20 20  har zBuf[20];.  
cd710 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
cd720 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73  ;.  int n;.  uns
cd730 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20  igned int c;..  
cd740 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31  for(i=0; i<0x001
cd750 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  10000; i++){.   
cd760 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57   z = zBuf;.    W
cd770 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 3b  RITE_UTF8(z, i);
cd780 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a  .    n = (int)(z
cd790 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65  -zBuf);.    asse
cd7a0 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20  rt( n>0 && n<=4 
cd7b0 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b  );.    z[0] = 0;
cd7c0 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20  .    z = zBuf;. 
cd7d0 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74     c = sqlite3Ut
cd7e0 66 38 52 65 61 64 28 28 63 6f 6e 73 74 20 75 38  f8Read((const u8
cd7f0 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74 20 3d 20  **)&z);.    t = 
cd800 69 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78  i;.    if( i>=0x
cd810 44 38 30 30 20 26 26 20 69 3c 3d 30 78 44 46 46  D800 && i<=0xDFF
cd820 46 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a  F ) t = 0xFFFD;.
cd830 20 20 20 20 69 66 28 20 28 69 26 30 78 46 46 46      if( (i&0xFFF
cd840 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29  FFFFE)==0xFFFE )
cd850 20 74 20 3d 20 30 78 46 46 46 44 3b 0a 20 20 20   t = 0xFFFD;.   
cd860 20 61 73 73 65 72 74 28 20 63 3d 3d 74 20 29 3b   assert( c==t );
cd870 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d  .    assert( (z-
cd880 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a  zBuf)==n );.  }.
cd890 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30    for(i=0; i<0x0
cd8a0 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20  0110000; i++){. 
cd8b0 20 20 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30     if( i>=0xD800
cd8c0 20 26 26 20 69 3c 30 78 45 30 30 30 20 29 20 63   && i<0xE000 ) c
cd8d0 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d  ontinue;.    z =
cd8e0 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45   zBuf;.    WRITE
cd8f0 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69 29 3b 0a  _UTF16LE(z, i);.
cd900 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d      n = (int)(z-
cd910 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72  zBuf);.    asser
cd920 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29  t( n>0 && n<=4 )
cd930 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a  ;.    z[0] = 0;.
cd940 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20      z = zBuf;.  
cd950 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a    READ_UTF16LE(z
cd960 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20 61 73 73  , 1, c);.    ass
cd970 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20  ert( c==i );.   
cd980 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66   assert( (z-zBuf
cd990 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 6f  )==n );.  }.  fo
cd9a0 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 31 30  r(i=0; i<0x00110
cd9b0 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  000; i++){.    i
cd9c0 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20  f( i>=0xD800 && 
cd9d0 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e 74 69  i<0xE000 ) conti
cd9e0 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75  nue;.    z = zBu
cd9f0 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46  f;.    WRITE_UTF
cda00 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20 20 20  16BE(z, i);.    
cda10 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66  n = (int)(z-zBuf
cda20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  );.    assert( n
cda30 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20  >0 && n<=4 );.  
cda40 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20    z[0] = 0;.    
cda50 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52 45  z = zBuf;.    RE
cda60 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 31 2c  AD_UTF16BE(z, 1,
cda70 20 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   c);.    assert(
cda80 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73 73   c==i );.    ass
cda90 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e  ert( (z-zBuf)==n
cdaa0 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66   );.  }.}.#endif
cdab0 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20   /* SQLITE_TEST 
cdac0 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  */.#endif /* SQL
cdad0 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
cdae0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
cdaf0 2a 2a 20 45 6e 64 20 6f 66 20 75 74 66 2e 63 20  ** End of utf.c 
cdb00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdb30 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
cdb40 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74  ** Begin file ut
cdb50 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  il.c ***********
cdb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdb80 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
cdb90 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
cdba0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
cdbb0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
cdbc0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
cdbd0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
cdbe0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
cdbf0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
cdc00 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
cdc10 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
cdc20 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
cdc30 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
cdc40 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
cdc50 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
cdc60 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
cdc70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
cdc80 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
cdc90 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
cdca0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
cdcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cdcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 74 69  *********.** Uti
cdd00 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 75  lity functions u
cdd10 73 65 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73  sed throughout s
cdd20 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  qlite..**.** Thi
cdd30 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
cdd40 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c  functions for al
cdd50 6c 6f 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c  locating memory,
cdd60 20 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 73 74   comparing.** st
cdd70 72 69 6e 67 73 2c 20 61 6e 64 20 73 74 75 66 66  rings, and stuff
cdd80 20 6c 69 6b 65 20 74 68 61 74 2e 0a 2a 2a 0a 2a   like that..**.*
cdd90 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73  /./* #include <s
cdda0 74 64 61 72 67 2e 68 3e 20 2a 2f 0a 23 69 66 64  tdarg.h> */.#ifd
cddb0 65 66 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49  ef SQLITE_HAVE_I
cddc0 53 4e 41 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c  SNAN.# include <
cddd0 6d 61 74 68 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a  math.h>.#endif..
cdde0 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 6e 65  /*.** Routine ne
cddf0 65 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20  eded to support 
cde00 74 68 65 20 74 65 73 74 63 61 73 65 28 29 20 6d  the testcase() m
cde10 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  acro..*/.#ifdef 
cde20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f  SQLITE_COVERAGE_
cde30 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  TEST.SQLITE_PRIV
cde40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
cde50 43 6f 76 65 72 61 67 65 28 69 6e 74 20 78 29 7b  Coverage(int x){
cde60 0a 20 20 73 74 61 74 69 63 20 75 6e 73 69 67 6e  .  static unsign
cde70 65 64 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 20  ed dummy = 0;.  
cde80 64 75 6d 6d 79 20 2b 3d 20 28 75 6e 73 69 67 6e  dummy += (unsign
cde90 65 64 29 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ed)x;.}.#endif..
cdea0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
cdeb0 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
cdec0 4e 54 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  NT./*.** Return 
cded0 74 72 75 65 20 69 66 20 74 68 65 20 66 6c 6f 61  true if the floa
cdee0 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
cdef0 20 69 73 20 4e 6f 74 20 61 20 4e 75 6d 62 65 72   is Not a Number
cdf00 20 28 4e 61 4e 29 2e 0a 2a 2a 0a 2a 2a 20 55 73   (NaN)..**.** Us
cdf10 65 20 74 68 65 20 6d 61 74 68 20 6c 69 62 72 61  e the math libra
cdf20 72 79 20 69 73 6e 61 6e 28 29 20 66 75 6e 63 74  ry isnan() funct
cdf30 69 6f 6e 20 69 66 20 63 6f 6d 70 69 6c 65 64 20  ion if compiled 
cdf40 77 69 74 68 20 53 51 4c 49 54 45 5f 48 41 56 45  with SQLITE_HAVE
cdf50 5f 49 53 4e 41 4e 2e 0a 2a 2a 20 4f 74 68 65 72  _ISNAN..** Other
cdf60 77 69 73 65 2c 20 77 65 20 68 61 76 65 20 6f 75  wise, we have ou
cdf70 72 20 6f 77 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  r own implementa
cdf80 74 69 6f 6e 20 74 68 61 74 20 77 6f 72 6b 73 20  tion that works 
cdf90 6f 6e 20 6d 6f 73 74 20 73 79 73 74 65 6d 73 2e  on most systems.
cdfa0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
cdfb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73  TE int sqlite3Is
cdfc0 4e 61 4e 28 64 6f 75 62 6c 65 20 78 29 7b 0a 20  NaN(double x){. 
cdfd0 20 69 6e 74 20 72 63 3b 20 20 20 2f 2a 20 54 68   int rc;   /* Th
cdfe0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 20 2a  e value return *
cdff0 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
ce000 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e  QLITE_HAVE_ISNAN
ce010 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 79 73 74  ).  /*.  ** Syst
ce020 65 6d 73 20 74 68 61 74 20 73 75 70 70 6f 72 74  ems that support
ce030 20 74 68 65 20 69 73 6e 61 6e 28 29 20 6c 69 62   the isnan() lib
ce040 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 73 68  rary function sh
ce050 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 0a 20 20  ould probably.  
ce060 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 69  ** make use of i
ce070 74 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77  t by compiling w
ce080 69 74 68 20 2d 44 53 51 4c 49 54 45 5f 48 41 56  ith -DSQLITE_HAV
ce090 45 5f 49 53 4e 41 4e 2e 20 20 42 75 74 20 77 65  E_ISNAN.  But we
ce0a0 20 68 61 76 65 0a 20 20 2a 2a 20 66 6f 75 6e 64   have.  ** found
ce0b0 20 74 68 61 74 20 6d 61 6e 79 20 73 79 73 74 65   that many syste
ce0c0 6d 73 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 61  ms do not have a
ce0d0 20 77 6f 72 6b 69 6e 67 20 69 73 6e 61 6e 28 29   working isnan()
ce0e0 20 66 75 6e 63 74 69 6f 6e 20 73 6f 0a 20 20 2a   function so.  *
ce0f0 2a 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74  * this implement
ce100 61 74 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65  ation is provide
ce110 64 20 61 73 20 61 6e 20 61 6c 74 65 72 6e 61 74  d as an alternat
ce120 69 76 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ive..  **.  ** T
ce130 68 69 73 20 4e 61 4e 20 74 65 73 74 20 73 6f 6d  his NaN test som
ce140 65 74 69 6d 65 73 20 66 61 69 6c 73 20 69 66 20  etimes fails if 
ce150 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 47 43 43 20  compiled on GCC 
ce160 77 69 74 68 20 2d 66 66 61 73 74 2d 6d 61 74 68  with -ffast-math
ce170 2e 0a 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74  ..  ** On the ot
ce180 68 65 72 20 68 61 6e 64 2c 20 74 68 65 20 75 73  her hand, the us
ce190 65 20 6f 66 20 2d 66 66 61 73 74 2d 6d 61 74 68  e of -ffast-math
ce1a0 20 63 6f 6d 65 73 20 77 69 74 68 20 74 68 65 20   comes with the 
ce1b0 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 77  following.  ** w
ce1c0 61 72 6e 69 6e 67 3a 0a 20 20 2a 2a 0a 20 20 2a  arning:.  **.  *
ce1d0 2a 20 20 20 20 20 20 54 68 69 73 20 6f 70 74 69  *      This opti
ce1e0 6f 6e 20 5b 2d 66 66 61 73 74 2d 6d 61 74 68 5d  on [-ffast-math]
ce1f0 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65   should never be
ce200 20 74 75 72 6e 65 64 20 6f 6e 20 62 79 20 61 6e   turned on by an
ce210 79 0a 20 20 2a 2a 20 20 20 20 20 20 2d 4f 20 6f  y.  **      -O o
ce220 70 74 69 6f 6e 20 73 69 6e 63 65 20 69 74 20 63  ption since it c
ce230 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 69 6e 63  an result in inc
ce240 6f 72 72 65 63 74 20 6f 75 74 70 75 74 20 66 6f  orrect output fo
ce250 72 20 70 72 6f 67 72 61 6d 73 0a 20 20 2a 2a 20  r programs.  ** 
ce260 20 20 20 20 20 77 68 69 63 68 20 64 65 70 65 6e       which depen
ce270 64 20 6f 6e 20 61 6e 20 65 78 61 63 74 20 69 6d  d on an exact im
ce280 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
ce290 49 45 45 45 20 6f 72 20 49 53 4f 20 0a 20 20 2a  IEEE or ISO .  *
ce2a0 2a 20 20 20 20 20 20 72 75 6c 65 73 2f 73 70 65  *      rules/spe
ce2b0 63 69 66 69 63 61 74 69 6f 6e 73 20 66 6f 72 20  cifications for 
ce2c0 6d 61 74 68 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  math functions..
ce2d0 20 20 2a 2a 0a 20 20 2a 2a 20 55 6e 64 65 72 20    **.  ** Under 
ce2e0 4d 53 56 43 2c 20 74 68 69 73 20 4e 61 4e 20 74  MSVC, this NaN t
ce2f0 65 73 74 20 6d 61 79 20 66 61 69 6c 20 69 66 20  est may fail if 
ce300 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 61 20  compiled with a 
ce310 66 6c 6f 61 74 69 6e 67 2d 0a 20 20 2a 2a 20 70  floating-.  ** p
ce320 6f 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 20 6d  oint precision m
ce330 6f 64 65 20 6f 74 68 65 72 20 74 68 61 6e 20 2f  ode other than /
ce340 66 70 3a 70 72 65 63 69 73 65 2e 20 20 46 72 6f  fp:precise.  Fro
ce350 6d 20 74 68 65 20 4d 53 44 4e 20 0a 20 20 2a 2a  m the MSDN .  **
ce360 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 3a 0a   documentation:.
ce370 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54    **.  **      T
ce380 68 65 20 63 6f 6d 70 69 6c 65 72 20 5b 77 69 74  he compiler [wit
ce390 68 20 2f 66 70 3a 70 72 65 63 69 73 65 5d 20 77  h /fp:precise] w
ce3a0 69 6c 6c 20 70 72 6f 70 65 72 6c 79 20 68 61 6e  ill properly han
ce3b0 64 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20  dle comparisons 
ce3c0 0a 20 20 2a 2a 20 20 20 20 20 20 69 6e 76 6f 6c  .  **      invol
ce3d0 76 69 6e 67 20 4e 61 4e 2e 20 46 6f 72 20 65 78  ving NaN. For ex
ce3e0 61 6d 70 6c 65 2c 20 78 20 21 3d 20 78 20 65 76  ample, x != x ev
ce3f0 61 6c 75 61 74 65 73 20 74 6f 20 74 72 75 65 20  aluates to true 
ce400 69 66 20 78 20 69 73 20 4e 61 4e 20 0a 20 20 2a  if x is NaN .  *
ce410 2a 20 20 20 20 20 20 2e 2e 2e 0a 20 20 2a 2f 0a  *      ....  */.
ce420 23 69 66 64 65 66 20 5f 5f 46 41 53 54 5f 4d 41  #ifdef __FAST_MA
ce430 54 48 5f 5f 0a 23 20 65 72 72 6f 72 20 53 51 4c  TH__.# error SQL
ce440 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72  ite will not wor
ce450 6b 20 63 6f 72 72 65 63 74 6c 79 20 77 69 74 68  k correctly with
ce460 20 74 68 65 20 2d 66 66 61 73 74 2d 6d 61 74 68   the -ffast-math
ce470 20 6f 70 74 69 6f 6e 20 6f 66 20 47 43 43 2e 0a   option of GCC..
ce480 23 65 6e 64 69 66 0a 20 20 76 6f 6c 61 74 69 6c  #endif.  volatil
ce490 65 20 64 6f 75 62 6c 65 20 79 20 3d 20 78 3b 0a  e double y = x;.
ce4a0 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c    volatile doubl
ce4b0 65 20 7a 20 3d 20 79 3b 0a 20 20 72 63 20 3d 20  e z = y;.  rc = 
ce4c0 28 79 21 3d 7a 29 3b 0a 23 65 6c 73 65 20 20 2f  (y!=z);.#else  /
ce4d0 2a 20 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  * if defined(SQL
ce4e0 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 29 20  ITE_HAVE_ISNAN) 
ce4f0 2a 2f 0a 20 20 72 63 20 3d 20 69 73 6e 61 6e 28  */.  rc = isnan(
ce500 78 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  x);.#endif /* SQ
ce510 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 20  LITE_HAVE_ISNAN 
ce520 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28 20 72  */.  testcase( r
ce530 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  c );.  return rc
ce540 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
ce550 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
ce560 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a  NG_POINT */../*.
ce570 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72  ** Compute a str
ce580 69 6e 67 20 6c 65 6e 67 74 68 20 74 68 61 74 20  ing length that 
ce590 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 77 68  is limited to wh
ce5a0 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64  at can be stored
ce5b0 20 69 6e 0a 2a 2a 20 6c 6f 77 65 72 20 33 30 20   in.** lower 30 
ce5c0 62 69 74 73 20 6f 66 20 61 20 33 32 2d 62 69 74  bits of a 32-bit
ce5d0 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e   signed integer.
ce5e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  .**.** The value
ce5f0 20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e   returned will n
ce600 65 76 65 72 20 62 65 20 6e 65 67 61 74 69 76 65  ever be negative
ce610 2e 20 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 65  .  Nor will it e
ce620 76 65 72 20 62 65 20 67 72 65 61 74 65 72 0a 2a  ver be greater.*
ce630 2a 20 74 68 61 6e 20 74 68 65 20 61 63 74 75 61  * than the actua
ce640 6c 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  l length of the 
ce650 73 74 72 69 6e 67 2e 20 20 46 6f 72 20 76 65 72  string.  For ver
ce660 79 20 6c 6f 6e 67 20 73 74 72 69 6e 67 73 20 28  y long strings (
ce670 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20  greater.** than 
ce680 31 47 69 42 29 20 74 68 65 20 76 61 6c 75 65 20  1GiB) the value 
ce690 72 65 74 75 72 6e 65 64 20 6d 69 67 68 74 20 62  returned might b
ce6a0 65 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20  e less than the 
ce6b0 74 72 75 65 20 73 74 72 69 6e 67 20 6c 65 6e 67  true string leng
ce6c0 74 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  th..*/.SQLITE_PR
ce6d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
ce6e0 33 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74 20  3Strlen30(const 
ce6f0 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 6f 6e 73  char *z){.  cons
ce700 74 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a  t char *z2 = z;.
ce710 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74    if( z==0 ) ret
ce720 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  urn 0;.  while( 
ce730 2a 7a 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20  *z2 ){ z2++; }. 
ce740 20 72 65 74 75 72 6e 20 30 78 33 66 66 66 66 66   return 0x3fffff
ce750 66 66 20 26 20 28 69 6e 74 29 28 7a 32 20 2d 20  ff & (int)(z2 - 
ce760 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  z);.}../*.** Set
ce770 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
ce780 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20   error code and 
ce790 65 72 72 6f 72 20 73 74 72 69 6e 67 20 66 6f 72  error string for
ce7a0 20 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68   the sqlite.** h
ce7b0 61 6e 64 6c 65 20 22 64 62 22 2e 20 54 68 65 20  andle "db". The 
ce7c0 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 65  error code is se
ce7d0 74 20 74 6f 20 22 65 72 72 5f 63 6f 64 65 22 2e  t to "err_code".
ce7e0 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20  .**.** If it is 
ce7f0 6e 6f 74 20 4e 55 4c 4c 2c 20 73 74 72 69 6e 67  not NULL, string
ce800 20 7a 46 6f 72 6d 61 74 20 73 70 65 63 69 66 69   zFormat specifi
ce810 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66  es the format of
ce820 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74   the.** error st
ce830 72 69 6e 67 20 69 6e 20 74 68 65 20 73 74 79 6c  ring in the styl
ce840 65 20 6f 66 20 74 68 65 20 70 72 69 6e 74 66 20  e of the printf 
ce850 66 75 6e 63 74 69 6f 6e 73 3a 20 54 68 65 20 66  functions: The f
ce860 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d  ollowing.** form
ce870 61 74 20 63 68 61 72 61 63 74 65 72 73 20 61 72  at characters ar
ce880 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a  e allowed:.**.**
ce890 20 20 20 20 20 20 25 73 20 20 20 20 20 20 49 6e        %s      In
ce8a0 73 65 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a  sert a string.**
ce8b0 20 20 20 20 20 20 25 7a 20 20 20 20 20 20 41 20        %z      A 
ce8c0 73 74 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75  string that shou
ce8d0 6c 64 20 62 65 20 66 72 65 65 64 20 61 66 74 65  ld be freed afte
ce8e0 72 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64  r use.**      %d
ce8f0 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20        Insert an 
ce900 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20  integer.**      
ce910 25 54 20 20 20 20 20 20 49 6e 73 65 72 74 20 61  %T      Insert a
ce920 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25   token.**      %
ce930 53 20 20 20 20 20 20 49 6e 73 65 72 74 20 74 68  S      Insert th
ce940 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20  e first element 
ce950 6f 66 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a  of a SrcList.**.
ce960 2a 2a 20 7a 46 6f 72 6d 61 74 20 61 6e 64 20 61  ** zFormat and a
ce970 6e 79 20 73 74 72 69 6e 67 20 74 6f 6b 65 6e 73  ny string tokens
ce980 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 69 74 20   that follow it 
ce990 61 72 65 20 61 73 73 75 6d 65 64 20 74 6f 20 62  are assumed to b
ce9a0 65 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20  e.** encoded in 
ce9b0 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20  UTF-8..**.** To 
ce9c0 63 6c 65 61 72 20 74 68 65 20 6d 6f 73 74 20 72  clear the most r
ce9d0 65 63 65 6e 74 20 65 72 72 6f 72 20 66 6f 72 20  ecent error for 
ce9e0 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 20 22 64  sqlite handle "d
ce9f0 62 22 2c 20 73 71 6c 69 74 65 33 45 72 72 6f 72  b", sqlite3Error
cea00 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 61  .** should be ca
cea10 6c 6c 65 64 20 77 69 74 68 20 65 72 72 5f 63 6f  lled with err_co
cea20 64 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  de set to SQLITE
cea30 5f 4f 4b 20 61 6e 64 20 7a 46 6f 72 6d 61 74 20  _OK and zFormat 
cea40 73 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a  set.** to NULL..
cea50 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
cea60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72  E void sqlite3Er
cea70 72 6f 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ror(sqlite3 *db,
cea80 20 69 6e 74 20 65 72 72 5f 63 6f 64 65 2c 20 63   int err_code, c
cea90 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
ceaa0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 69 66 28 20  at, ...){.  if( 
ceab0 64 62 20 26 26 20 28 64 62 2d 3e 70 45 72 72 20  db && (db->pErr 
ceac0 7c 7c 20 28 64 62 2d 3e 70 45 72 72 20 3d 20 73  || (db->pErr = s
cead0 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64  qlite3ValueNew(d
ceae0 62 29 29 21 3d 30 29 20 29 7b 0a 20 20 20 20 64  b))!=0) ){.    d
ceaf0 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 65 72 72  b->errCode = err
ceb00 5f 63 6f 64 65 3b 0a 20 20 20 20 69 66 28 20 7a  _code;.    if( z
ceb10 46 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 20 20  Format ){.      
ceb20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 76  char *z;.      v
ceb30 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 20 20 20  a_list ap;.     
ceb40 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
ceb50 6f 72 6d 61 74 29 3b 0a 20 20 20 20 20 20 7a 20  ormat);.      z 
ceb60 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74  = sqlite3VMPrint
ceb70 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  f(db, zFormat, a
ceb80 70 29 3b 0a 20 20 20 20 20 20 76 61 5f 65 6e 64  p);.      va_end
ceb90 28 61 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  (ap);.      sqli
ceba0 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64  te3ValueSetStr(d
cebb0 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 7a 2c 20  b->pErr, -1, z, 
cebc0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
cebd0 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20  ITE_DYNAMIC);.  
cebe0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
cebf0 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74  qlite3ValueSetSt
cec00 72 28 64 62 2d 3e 70 45 72 72 2c 20 30 2c 20 30  r(db->pErr, 0, 0
cec10 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
cec20 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
cec30 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
cec40 2a 20 41 64 64 20 61 6e 20 65 72 72 6f 72 20 6d  * Add an error m
cec50 65 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65  essage to pParse
cec60 2d 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 69 6e  ->zErrMsg and in
cec70 63 72 65 6d 65 6e 74 20 70 50 61 72 73 65 2d 3e  crement pParse->
cec80 6e 45 72 72 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c  nErr..** The fol
cec90 6c 6f 77 69 6e 67 20 66 6f 72 6d 61 74 74 69 6e  lowing formattin
ceca0 67 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65  g characters are
cecb0 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20   allowed:.**.** 
cecc0 20 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73       %s      Ins
cecd0 65 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20  ert a string.** 
cece0 20 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73       %z      A s
cecf0 74 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c  tring that shoul
ced00 64 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72  d be freed after
ced10 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20   use.**      %d 
ced20 20 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69       Insert an i
ced30 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25  nteger.**      %
ced40 54 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20  T      Insert a 
ced50 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53  token.**      %S
ced60 20 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65        Insert the
ced70 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f   first element o
ced80 66 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a  f a SrcList.**.*
ced90 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
ceda0 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 74  should be used t
cedb0 6f 20 72 65 70 6f 72 74 20 61 6e 79 20 65 72 72  o report any err
cedc0 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 77  or that occurs w
cedd0 68 69 6c 73 74 0a 2a 2a 20 63 6f 6d 70 69 6c 69  hilst.** compili
cede0 6e 67 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  ng an SQL statem
cedf0 65 6e 74 20 28 69 2e 65 2e 20 77 69 74 68 69 6e  ent (i.e. within
cee00 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
cee10 28 29 29 2e 20 54 68 65 0a 2a 2a 20 6c 61 73 74  ()). The.** last
cee20 20 74 68 69 6e 67 20 74 68 65 20 73 71 6c 69 74   thing the sqlit
cee30 65 33 5f 70 72 65 70 61 72 65 28 29 20 66 75 6e  e3_prepare() fun
cee40 63 74 69 6f 6e 20 64 6f 65 73 20 69 73 20 63 6f  ction does is co
cee50 70 79 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20  py the error.** 
cee60 73 74 6f 72 65 64 20 62 79 20 74 68 69 73 20 66  stored by this f
cee70 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65  unction into the
cee80 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
cee90 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 45 72   using sqlite3Er
ceea0 72 6f 72 28 29 2e 0a 2a 2a 20 46 75 6e 63 74 69  ror()..** Functi
ceeb0 6f 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  on sqlite3Error(
ceec0 29 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64  ) should be used
ceed0 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e   during statemen
ceee0 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 28  t execution.** (
ceef0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 65  sqlite3_step() e
cef00 74 63 2e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tc.)..*/.SQLITE_
cef10 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
cef20 69 74 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72  ite3ErrorMsg(Par
cef30 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73  se *pParse, cons
cef40 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
cef50 20 2e 2e 2e 29 7b 0a 20 20 63 68 61 72 20 2a 7a   ...){.  char *z
cef60 4d 73 67 3b 0a 20 20 76 61 5f 6c 69 73 74 20 61  Msg;.  va_list a
cef70 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  p;.  sqlite3 *db
cef80 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
cef90 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
cefa0 6f 72 6d 61 74 29 3b 0a 20 20 7a 4d 73 67 20 3d  ormat);.  zMsg =
cefb0 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66   sqlite3VMPrintf
cefc0 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  (db, zFormat, ap
cefd0 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b  );.  va_end(ap);
cefe0 0a 20 20 69 66 28 20 64 62 2d 3e 73 75 70 70 72  .  if( db->suppr
ceff0 65 73 73 45 72 72 20 29 7b 0a 20 20 20 20 73 71  essErr ){.    sq
cf000 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
cf010 7a 4d 73 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  zMsg);.  }else{.
cf020 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72      pParse->nErr
cf030 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  ++;.    sqlite3D
cf040 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65  bFree(db, pParse
cf050 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
cf060 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20  pParse->zErrMsg 
cf070 3d 20 7a 4d 73 67 3b 0a 20 20 20 20 70 50 61 72  = zMsg;.    pPar
cf080 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
cf090 45 52 52 4f 52 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ERROR;.  }.}../*
cf0a0 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 53  .** Convert an S
cf0b0 51 4c 2d 73 74 79 6c 65 20 71 75 6f 74 65 64 20  QL-style quoted 
cf0c0 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 6e 6f  string into a no
cf0d0 72 6d 61 6c 20 73 74 72 69 6e 67 20 62 79 20 72  rmal string by r
cf0e0 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74 68 65 20 71  emoving.** the q
cf0f0 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 73 2e  uote characters.
cf100 20 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e    The conversion
cf110 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63   is done in-plac
cf120 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 69 6e  e.  If the.** in
cf130 70 75 74 20 64 6f 65 73 20 6e 6f 74 20 62 65 67  put does not beg
cf140 69 6e 20 77 69 74 68 20 61 20 71 75 6f 74 65 20  in with a quote 
cf150 63 68 61 72 61 63 74 65 72 2c 20 74 68 65 6e 20  character, then 
cf160 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
cf170 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
cf180 2a 20 54 68 65 20 69 6e 70 75 74 20 73 74 72 69  * The input stri
cf190 6e 67 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d  ng must be zero-
cf1a0 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 41 20 6e  terminated.  A n
cf1b0 65 77 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  ew zero-terminat
cf1c0 6f 72 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74  or.** is added t
cf1d0 6f 20 74 68 65 20 64 65 71 75 6f 74 65 64 20 73  o the dequoted s
cf1e0 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tring..**.** The
cf1f0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
cf200 20 2d 31 20 69 66 20 6e 6f 20 64 65 71 75 6f 74   -1 if no dequot
cf210 69 6e 67 20 6f 63 63 75 72 73 20 6f 72 20 74 68  ing occurs or th
cf220 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 0a  e length of the.
cf230 2a 2a 20 64 65 71 75 6f 74 65 64 20 73 74 72 69  ** dequoted stri
cf240 6e 67 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66  ng, exclusive of
cf250 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e   the zero termin
cf260 61 74 6f 72 2c 20 69 66 20 64 65 71 75 6f 74 69  ator, if dequoti
cf270 6e 67 20 64 6f 65 73 0a 2a 2a 20 6f 63 63 75 72  ng does.** occur
cf280 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 32 2d 46 65 62  ..**.** 2002-Feb
cf290 2d 31 34 3a 20 54 68 69 73 20 72 6f 75 74 69 6e  -14: This routin
cf2a0 65 20 69 73 20 65 78 74 65 6e 64 65 64 20 74 6f  e is extended to
cf2b0 20 72 65 6d 6f 76 65 20 4d 53 2d 41 63 63 65 73   remove MS-Acces
cf2c0 73 20 73 74 79 6c 65 0a 2a 2a 20 62 72 61 63 6b  s style.** brack
cf2d0 65 74 73 20 66 72 6f 6d 20 61 72 6f 75 6e 64 20  ets from around 
cf2e0 69 64 65 6e 74 69 66 65 72 73 2e 20 20 46 6f 72  identifers.  For
cf2f0 20 65 78 61 6d 70 6c 65 3a 20 20 22 5b 61 2d 62   example:  "[a-b
cf300 2d 63 5d 22 20 62 65 63 6f 6d 65 73 0a 2a 2a 20  -c]" becomes.** 
cf310 22 61 2d 62 2d 63 22 2e 0a 2a 2f 0a 53 51 4c 49  "a-b-c"..*/.SQLI
cf320 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
cf330 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 68  qlite3Dequote(ch
cf340 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20 71  ar *z){.  char q
cf350 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69 2c 20 6a  uote;.  int i, j
cf360 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72  ;.  if( z==0 ) r
cf370 65 74 75 72 6e 20 2d 31 3b 0a 20 20 71 75 6f 74  eturn -1;.  quot
cf380 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69 74  e = z[0];.  swit
cf390 63 68 28 20 71 75 6f 74 65 20 29 7b 0a 20 20 20  ch( quote ){.   
cf3a0 20 63 61 73 65 20 27 5c 27 27 3a 20 20 62 72 65   case '\'':  bre
cf3b0 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 22 27  ak;.    case '"'
cf3c0 3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63  :   break;.    c
cf3d0 61 73 65 20 27 60 27 3a 20 20 20 62 72 65 61 6b  ase '`':   break
cf3e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
cf3f0 20 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63 6f   /* For MySQL co
cf400 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20  mpatibility */. 
cf410 20 20 20 63 61 73 65 20 27 5b 27 3a 20 20 20 71     case '[':   q
cf420 75 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62 72 65  uote = ']';  bre
cf430 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20 53  ak;  /* For MS S
cf440 71 6c 53 65 72 76 65 72 20 63 6f 6d 70 61 74 69  qlServer compati
cf450 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64 65  bility */.    de
cf460 66 61 75 6c 74 3a 20 20 20 20 72 65 74 75 72 6e  fault:    return
cf470 20 2d 31 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69   -1;.  }.  for(i
cf480 3d 31 2c 20 6a 3d 30 3b 20 41 4c 57 41 59 53 28  =1, j=0; ALWAYS(
cf490 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  z[i]); i++){.   
cf4a0 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65   if( z[i]==quote
cf4b0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b   ){.      if( z[
cf4c0 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20  i+1]==quote ){. 
cf4d0 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20         z[j++] = 
cf4e0 71 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 20 69  quote;.        i
cf4f0 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ++;.      }else{
cf500 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
cf510 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
cf520 65 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20  e{.      z[j++] 
cf530 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20  = z[i];.    }.  
cf540 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20  }.  z[j] = 0;.  
cf550 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 20  return j;.}../* 
cf560 43 6f 6e 76 65 6e 69 65 6e 74 20 73 68 6f 72 74  Convenient short
cf570 2d 68 61 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65  -hand */.#define
cf580 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 20 73 71   UpperToLower sq
cf590 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
cf5a0 72 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79  r../*.** Some sy
cf5b0 73 74 65 6d 73 20 68 61 76 65 20 73 74 72 69 63  stems have stric
cf5c0 6d 70 28 29 2e 20 20 4f 74 68 65 72 73 20 68 61  mp().  Others ha
cf5d0 76 65 20 73 74 72 63 61 73 65 63 6d 70 28 29 2e  ve strcasecmp().
cf5e0 20 20 42 65 63 61 75 73 65 0a 2a 2a 20 74 68 65    Because.** the
cf5f0 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 69 73 74  re is no consist
cf600 65 6e 63 79 2c 20 77 65 20 77 69 6c 6c 20 64 65  ency, we will de
cf610 66 69 6e 65 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2a  fine our own..**
cf620 0a 2a 2a 20 49 4d 50 4c 45 4d 45 4e 54 41 54 49  .** IMPLEMENTATI
cf630 4f 4e 2d 4f 46 3a 20 52 2d 33 30 32 34 33 2d 30  ON-OF: R-30243-0
cf640 32 34 39 34 20 54 68 65 20 73 71 6c 69 74 65 33  2494 The sqlite3
cf650 5f 73 74 72 69 63 6d 70 28 29 20 61 6e 64 0a 2a  _stricmp() and.*
cf660 2a 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63  * sqlite3_strnic
cf670 6d 70 28 29 20 41 50 49 73 20 61 6c 6c 6f 77 20  mp() APIs allow 
cf680 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64  applications and
cf690 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 63   extensions to c
cf6a0 6f 6d 70 61 72 65 0a 2a 2a 20 74 68 65 20 63 6f  ompare.** the co
cf6b0 6e 74 65 6e 74 73 20 6f 66 20 74 77 6f 20 62 75  ntents of two bu
cf6c0 66 66 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67  ffers containing
cf6d0 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 69   UTF-8 strings i
cf6e0 6e 20 61 0a 2a 2a 20 63 61 73 65 2d 69 6e 64 65  n a.** case-inde
cf6f0 70 65 6e 64 65 6e 74 20 66 61 73 68 69 6f 6e 2c  pendent fashion,
cf700 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20   using the same 
cf710 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 22 63  definition of "c
cf720 61 73 65 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65  ase.** independe
cf730 6e 63 65 22 20 74 68 61 74 20 53 51 4c 69 74 65  nce" that SQLite
cf740 20 75 73 65 73 20 69 6e 74 65 72 6e 61 6c 6c 79   uses internally
cf750 20 77 68 65 6e 20 63 6f 6d 70 61 72 69 6e 67 20   when comparing 
cf760 69 64 65 6e 74 69 66 69 65 72 73 2e 0a 2a 2f 0a  identifiers..*/.
cf770 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
cf780 71 6c 69 74 65 33 5f 73 74 72 69 63 6d 70 28 63  qlite3_stricmp(c
cf790 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74  onst char *zLeft
cf7a0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52  , const char *zR
cf7b0 69 67 68 74 29 7b 0a 20 20 72 65 67 69 73 74 65  ight){.  registe
cf7c0 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  r unsigned char 
cf7d0 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75  *a, *b;.  a = (u
cf7e0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a  nsigned char *)z
cf7f0 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73  Left;.  b = (uns
cf800 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69  igned char *)zRi
cf810 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 2a 61  ght;.  while( *a
cf820 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f  !=0 && UpperToLo
cf830 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f  wer[*a]==UpperTo
cf840 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b  Lower[*b]){ a++;
cf850 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e   b++; }.  return
cf860 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61   UpperToLower[*a
cf870 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72  ] - UpperToLower
cf880 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  [*b];.}.SQLITE_A
cf890 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
cf8a0 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 68  trnicmp(const ch
cf8b0 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74  ar *zLeft, const
cf8c0 20 63 68 61 72 20 2a 7a 52 69 67 68 74 2c 20 69   char *zRight, i
cf8d0 6e 74 20 4e 29 7b 0a 20 20 72 65 67 69 73 74 65  nt N){.  registe
cf8e0 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  r unsigned char 
cf8f0 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75  *a, *b;.  a = (u
cf900 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a  nsigned char *)z
cf910 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73  Left;.  b = (uns
cf920 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69  igned char *)zRi
cf930 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d  ght;.  while( N-
cf940 2d 20 3e 20 30 20 26 26 20 2a 61 21 3d 30 20 26  - > 0 && *a!=0 &
cf950 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  & UpperToLower[*
cf960 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72  a]==UpperToLower
cf970 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b  [*b]){ a++; b++;
cf980 20 7d 0a 20 20 72 65 74 75 72 6e 20 4e 3c 30 20   }.  return N<0 
cf990 3f 20 30 20 3a 20 55 70 70 65 72 54 6f 4c 6f 77  ? 0 : UpperToLow
cf9a0 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f  er[*a] - UpperTo
cf9b0 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a  Lower[*b];.}../*
cf9c0 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a  .** The string z
cf9d0 5b 5d 20 69 73 20 61 6e 20 74 65 78 74 20 72 65  [] is an text re
cf9e0 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
cf9f0 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 2e 0a 2a  a real number..*
cfa00 2a 20 43 6f 6e 76 65 72 74 20 74 68 69 73 20 73  * Convert this s
cfa10 74 72 69 6e 67 20 74 6f 20 61 20 64 6f 75 62 6c  tring to a doubl
cfa20 65 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 69  e and write it i
cfa30 6e 74 6f 20 2a 70 52 65 73 75 6c 74 2e 0a 2a 2a  nto *pResult..**
cfa40 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a  .** The string z
cfa50 5b 5d 20 69 73 20 6c 65 6e 67 74 68 20 62 79 74  [] is length byt
cfa60 65 73 20 69 6e 20 6c 65 6e 67 74 68 20 28 62 79  es in length (by
cfa70 74 65 73 2c 20 6e 6f 74 20 63 68 61 72 61 63 74  tes, not charact
cfa80 65 72 73 29 20 61 6e 64 0a 2a 2a 20 75 73 65 73  ers) and.** uses
cfa90 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 65 6e   the encoding en
cfaa0 63 2e 20 20 54 68 65 20 73 74 72 69 6e 67 20 69  c.  The string i
cfab0 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  s not necessaril
cfac0 79 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  y zero-terminate
cfad0 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  d..**.** Return 
cfae0 54 52 55 45 20 69 66 20 74 68 65 20 72 65 73 75  TRUE if the resu
cfaf0 6c 74 20 69 73 20 61 20 76 61 6c 69 64 20 72 65  lt is a valid re
cfb00 61 6c 20 6e 75 6d 62 65 72 20 28 6f 72 20 69 6e  al number (or in
cfb10 74 65 67 65 72 29 20 61 6e 64 20 46 41 4c 53 45  teger) and FALSE
cfb20 0a 2a 2a 20 69 66 20 74 68 65 20 73 74 72 69 6e  .** if the strin
cfb30 67 20 69 73 20 65 6d 70 74 79 20 6f 72 20 63 6f  g is empty or co
cfb40 6e 74 61 69 6e 73 20 65 78 74 72 61 6e 65 6f 75  ntains extraneou
cfb50 73 20 74 65 78 74 2e 20 20 56 61 6c 69 64 20 6e  s text.  Valid n
cfb60 75 6d 62 65 72 73 0a 2a 2a 20 61 72 65 20 69 6e  umbers.** are in
cfb70 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 66 6f   one of these fo
cfb80 72 6d 61 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  rmats:.**.**    
cfb90 5b 2b 2d 5d 64 69 67 69 74 73 5b 45 5b 2b 2d 5d  [+-]digits[E[+-]
cfba0 64 69 67 69 74 73 5d 0a 2a 2a 20 20 20 20 5b 2b  digits].**    [+
cfbb0 2d 5d 64 69 67 69 74 73 2e 5b 64 69 67 69 74 73  -]digits.[digits
cfbc0 5d 5b 45 5b 2b 2d 5d 64 69 67 69 74 73 5d 0a 2a  ][E[+-]digits].*
cfbd0 2a 20 20 20 20 5b 2b 2d 5d 2e 64 69 67 69 74 73  *    [+-].digits
cfbe0 5b 45 5b 2b 2d 5d 64 69 67 69 74 73 5d 0a 2a 2a  [E[+-]digits].**
cfbf0 0a 2a 2a 20 4c 65 61 64 69 6e 67 20 61 6e 64 20  .** Leading and 
cfc00 74 72 61 69 6c 69 6e 67 20 77 68 69 74 65 73 70  trailing whitesp
cfc10 61 63 65 20 69 73 20 69 67 6e 6f 72 65 64 20 66  ace is ignored f
cfc20 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 20 6f  or the purpose o
cfc30 66 20 64 65 74 65 72 6d 69 6e 69 6e 67 0a 2a 2a  f determining.**
cfc40 20 76 61 6c 69 64 69 74 79 2e 0a 2a 2a 0a 2a 2a   validity..**.**
cfc50 20 49 66 20 73 6f 6d 65 20 70 72 65 66 69 78 20   If some prefix 
cfc60 6f 66 20 74 68 65 20 69 6e 70 75 74 20 73 74 72  of the input str
cfc70 69 6e 67 20 69 73 20 61 20 76 61 6c 69 64 20 6e  ing is a valid n
cfc80 75 6d 62 65 72 2c 20 74 68 69 73 20 72 6f 75 74  umber, this rout
cfc90 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 46  ine.** returns F
cfca0 41 4c 53 45 20 62 75 74 20 69 74 20 73 74 69 6c  ALSE but it stil
cfcb0 6c 20 63 6f 6e 76 65 72 74 73 20 74 68 65 20 70  l converts the p
cfcc0 72 65 66 69 78 20 61 6e 64 20 77 72 69 74 65 73  refix and writes
cfcd0 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69   the result.** i
cfce0 6e 74 6f 20 2a 70 52 65 73 75 6c 74 2e 0a 2a 2f  nto *pResult..*/
cfcf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
cfd00 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28  int sqlite3AtoF(
cfd10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64  const char *z, d
cfd20 6f 75 62 6c 65 20 2a 70 52 65 73 75 6c 74 2c 20  ouble *pResult, 
cfd30 69 6e 74 20 6c 65 6e 67 74 68 2c 20 75 38 20 65  int length, u8 e
cfd40 6e 63 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  nc){.#ifndef SQL
cfd50 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
cfd60 47 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20 69 6e  G_POINT.  int in
cfd70 63 72 20 3d 20 28 65 6e 63 3d 3d 53 51 4c 49 54  cr = (enc==SQLIT
cfd80 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a 20 20 63  E_UTF8?1:2);.  c
cfd90 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 20  onst char *zEnd 
cfda0 3d 20 7a 20 2b 20 6c 65 6e 67 74 68 3b 0a 20 20  = z + length;.  
cfdb0 2f 2a 20 73 69 67 6e 20 2a 20 73 69 67 6e 69 66  /* sign * signif
cfdc0 69 63 61 6e 64 20 2a 20 28 31 30 20 5e 20 28 65  icand * (10 ^ (e
cfdd0 73 69 67 6e 20 2a 20 65 78 70 6f 6e 65 6e 74 29  sign * exponent)
cfde0 29 20 2a 2f 0a 20 20 69 6e 74 20 73 69 67 6e 20  ) */.  int sign 
cfdf0 3d 20 31 3b 20 20 20 20 2f 2a 20 73 69 67 6e 20  = 1;    /* sign 
cfe00 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a  of significand *
cfe10 2f 0a 20 20 69 36 34 20 73 20 3d 20 30 3b 20 20  /.  i64 s = 0;  
cfe20 20 20 20 20 20 2f 2a 20 73 69 67 6e 69 66 69 63       /* signific
cfe30 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 64 20 3d  and */.  int d =
cfe40 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 61 64 6a   0;       /* adj
cfe50 75 73 74 20 65 78 70 6f 6e 65 6e 74 20 66 6f 72  ust exponent for
cfe60 20 73 68 69 66 74 69 6e 67 20 64 65 63 69 6d 61   shifting decima
cfe70 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 69 6e 74  l point */.  int
cfe80 20 65 73 69 67 6e 20 3d 20 31 3b 20 20 20 2f 2a   esign = 1;   /*
cfe90 20 73 69 67 6e 20 6f 66 20 65 78 70 6f 6e 65 6e   sign of exponen
cfea0 74 20 2a 2f 0a 20 20 69 6e 74 20 65 20 3d 20 30  t */.  int e = 0
cfeb0 3b 20 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e  ;       /* expon
cfec0 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 65 56 61  ent */.  int eVa
cfed0 6c 69 64 20 3d 20 31 3b 20 20 2f 2a 20 54 72 75  lid = 1;  /* Tru
cfee0 65 20 65 78 70 6f 6e 65 6e 74 20 69 73 20 65 69  e exponent is ei
cfef0 74 68 65 72 20 6e 6f 74 20 75 73 65 64 20 6f 72  ther not used or
cff00 20 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20   is well-formed 
cff10 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 65 73 75  */.  double resu
cff20 6c 74 3b 0a 20 20 69 6e 74 20 6e 44 69 67 69 74  lt;.  int nDigit
cff30 73 20 3d 20 30 3b 0a 0a 20 20 2a 70 52 65 73 75  s = 0;..  *pResu
cff40 6c 74 20 3d 20 30 2e 30 3b 20 20 20 2f 2a 20 44  lt = 0.0;   /* D
cff50 65 66 61 75 6c 74 20 72 65 74 75 72 6e 20 76 61  efault return va
cff60 6c 75 65 2c 20 69 6e 20 63 61 73 65 20 6f 66 20  lue, in case of 
cff70 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 69  an error */..  i
cff80 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  f( enc==SQLITE_U
cff90 54 46 31 36 42 45 20 29 20 7a 2b 2b 3b 0a 0a 20  TF16BE ) z++;.. 
cffa0 20 2f 2a 20 73 6b 69 70 20 6c 65 61 64 69 6e 67   /* skip leading
cffb0 20 73 70 61 63 65 73 20 2a 2f 0a 20 20 77 68 69   spaces */.  whi
cffc0 6c 65 28 20 7a 3c 7a 45 6e 64 20 26 26 20 73 71  le( z<zEnd && sq
cffd0 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29  lite3Isspace(*z)
cffe0 20 29 20 7a 2b 3d 69 6e 63 72 3b 0a 20 20 69 66   ) z+=incr;.  if
cfff0 28 20 7a 3e 3d 7a 45 6e 64 20 29 20 72 65 74 75  ( z>=zEnd ) retu
d0000 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 67 65 74 20  rn 0;..  /* get 
d0010 73 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63  sign of signific
d0020 61 6e 64 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d  and */.  if( *z=
d0030 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 69 67 6e  ='-' ){.    sign
d0040 20 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b 3d 69 6e   = -1;.    z+=in
d0050 63 72 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  cr;.  }else if( 
d0060 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a  *z=='+' ){.    z
d0070 2b 3d 69 6e 63 72 3b 0a 20 20 7d 0a 0a 20 20 2f  +=incr;.  }..  /
d0080 2a 20 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a  * skip leading z
d0090 65 72 6f 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65  eroes */.  while
d00a0 28 20 7a 3c 7a 45 6e 64 20 26 26 20 7a 5b 30 5d  ( z<zEnd && z[0]
d00b0 3d 3d 27 30 27 20 29 20 7a 2b 3d 69 6e 63 72 2c  =='0' ) z+=incr,
d00c0 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 0a 20 20 2f   nDigits++;..  /
d00d0 2a 20 63 6f 70 79 20 6d 61 78 20 73 69 67 6e 69  * copy max signi
d00e0 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 74 6f  ficant digits to
d00f0 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a   significand */.
d0100 20 20 77 68 69 6c 65 28 20 7a 3c 7a 45 6e 64 20    while( z<zEnd 
d0110 26 26 20 73 71 6c 69 74 65 33 49 73 64 69 67 69  && sqlite3Isdigi
d0120 74 28 2a 7a 29 20 26 26 20 73 3c 28 28 4c 41 52  t(*z) && s<((LAR
d0130 47 45 53 54 5f 49 4e 54 36 34 2d 39 29 2f 31 30  GEST_INT64-9)/10
d0140 29 20 29 7b 0a 20 20 20 20 73 20 3d 20 73 2a 31  ) ){.    s = s*1
d0150 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a  0 + (*z - '0');.
d0160 20 20 20 20 7a 2b 3d 69 6e 63 72 2c 20 6e 44 69      z+=incr, nDi
d0170 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f  gits++;.  }..  /
d0180 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e 69  * skip non-signi
d0190 66 69 63 61 6e 74 20 73 69 67 6e 69 66 69 63 61  ficant significa
d01a0 6e 64 20 64 69 67 69 74 73 0a 20 20 2a 2a 20 28  nd digits.  ** (
d01b0 69 6e 63 72 65 61 73 65 20 65 78 70 6f 6e 65 6e  increase exponen
d01c0 74 20 62 79 20 64 20 74 6f 20 73 68 69 66 74 20  t by d to shift 
d01d0 64 65 63 69 6d 61 6c 20 6c 65 66 74 29 20 2a 2f  decimal left) */
d01e0 0a 20 20 77 68 69 6c 65 28 20 7a 3c 7a 45 6e 64  .  while( z<zEnd
d01f0 20 26 26 20 73 71 6c 69 74 65 33 49 73 64 69 67   && sqlite3Isdig
d0200 69 74 28 2a 7a 29 20 29 20 7a 2b 3d 69 6e 63 72  it(*z) ) z+=incr
d0210 2c 20 6e 44 69 67 69 74 73 2b 2b 2c 20 64 2b 2b  , nDigits++, d++
d0220 3b 0a 20 20 69 66 28 20 7a 3e 3d 7a 45 6e 64 20  ;.  if( z>=zEnd 
d0230 29 20 67 6f 74 6f 20 64 6f 5f 61 74 6f 66 5f 63  ) goto do_atof_c
d0240 61 6c 63 3b 0a 0a 20 20 2f 2a 20 69 66 20 64 65  alc;..  /* if de
d0250 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 73 20 70  cimal point is p
d0260 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28 20  resent */.  if( 
d0270 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a  *z=='.' ){.    z
d0280 2b 3d 69 6e 63 72 3b 0a 20 20 20 20 2f 2a 20 63  +=incr;.    /* c
d0290 6f 70 79 20 64 69 67 69 74 73 20 66 72 6f 6d 20  opy digits from 
d02a0 61 66 74 65 72 20 64 65 63 69 6d 61 6c 20 74 6f  after decimal to
d02b0 20 73 69 67 6e 69 66 69 63 61 6e 64 0a 20 20 20   significand.   
d02c0 20 2a 2a 20 28 64 65 63 72 65 61 73 65 20 65 78   ** (decrease ex
d02d0 70 6f 6e 65 6e 74 20 62 79 20 64 20 74 6f 20 73  ponent by d to s
d02e0 68 69 66 74 20 64 65 63 69 6d 61 6c 20 72 69 67  hift decimal rig
d02f0 68 74 29 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65  ht) */.    while
d0300 28 20 7a 3c 7a 45 6e 64 20 26 26 20 73 71 6c 69  ( z<zEnd && sqli
d0310 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26  te3Isdigit(*z) &
d0320 26 20 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e  & s<((LARGEST_IN
d0330 54 36 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20  T64-9)/10) ){.  
d0340 20 20 20 20 73 20 3d 20 73 2a 31 30 20 2b 20 28      s = s*10 + (
d0350 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20  *z - '0');.     
d0360 20 7a 2b 3d 69 6e 63 72 2c 20 6e 44 69 67 69 74   z+=incr, nDigit
d0370 73 2b 2b 2c 20 64 2d 2d 3b 0a 20 20 20 20 7d 0a  s++, d--;.    }.
d0380 20 20 20 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d      /* skip non-
d0390 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69  significant digi
d03a0 74 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28  ts */.    while(
d03b0 20 7a 3c 7a 45 6e 64 20 26 26 20 73 71 6c 69 74   z<zEnd && sqlit
d03c0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20  e3Isdigit(*z) ) 
d03d0 7a 2b 3d 69 6e 63 72 2c 20 6e 44 69 67 69 74 73  z+=incr, nDigits
d03e0 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 3e  ++;.  }.  if( z>
d03f0 3d 7a 45 6e 64 20 29 20 67 6f 74 6f 20 64 6f 5f  =zEnd ) goto do_
d0400 61 74 6f 66 5f 63 61 6c 63 3b 0a 0a 20 20 2f 2a  atof_calc;..  /*
d0410 20 69 66 20 65 78 70 6f 6e 65 6e 74 20 69 73 20   if exponent is 
d0420 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28  present */.  if(
d0430 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d   *z=='e' || *z==
d0440 27 45 27 20 29 7b 0a 20 20 20 20 7a 2b 3d 69 6e  'E' ){.    z+=in
d0450 63 72 3b 0a 20 20 20 20 65 56 61 6c 69 64 20 3d  cr;.    eValid =
d0460 20 30 3b 0a 20 20 20 20 69 66 28 20 7a 3e 3d 7a   0;.    if( z>=z
d0470 45 6e 64 20 29 20 67 6f 74 6f 20 64 6f 5f 61 74  End ) goto do_at
d0480 6f 66 5f 63 61 6c 63 3b 0a 20 20 20 20 2f 2a 20  of_calc;.    /* 
d0490 67 65 74 20 73 69 67 6e 20 6f 66 20 65 78 70 6f  get sign of expo
d04a0 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 20  nent */.    if( 
d04b0 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20  *z=='-' ){.     
d04c0 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20   esign = -1;.   
d04d0 20 20 20 7a 2b 3d 69 6e 63 72 3b 0a 20 20 20 20     z+=incr;.    
d04e0 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b  }else if( *z=='+
d04f0 27 20 29 7b 0a 20 20 20 20 20 20 7a 2b 3d 69 6e  ' ){.      z+=in
d0500 63 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a  cr;.    }.    /*
d0510 20 63 6f 70 79 20 64 69 67 69 74 73 20 74 6f 20   copy digits to 
d0520 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20  exponent */.    
d0530 77 68 69 6c 65 28 20 7a 3c 7a 45 6e 64 20 26 26  while( z<zEnd &&
d0540 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
d0550 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 65 20 3d  *z) ){.      e =
d0560 20 65 3c 31 30 30 30 30 20 3f 20 28 65 2a 31 30   e<10000 ? (e*10
d0570 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 29 20 3a   + (*z - '0')) :
d0580 20 31 30 30 30 30 3b 0a 20 20 20 20 20 20 7a 2b   10000;.      z+
d0590 3d 69 6e 63 72 3b 0a 20 20 20 20 20 20 65 56 61  =incr;.      eVa
d05a0 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  lid = 1;.    }. 
d05b0 20 7d 0a 0a 20 20 2f 2a 20 73 6b 69 70 20 74 72   }..  /* skip tr
d05c0 61 69 6c 69 6e 67 20 73 70 61 63 65 73 20 2a 2f  ailing spaces */
d05d0 0a 20 20 69 66 28 20 6e 44 69 67 69 74 73 20 26  .  if( nDigits &
d05e0 26 20 65 56 61 6c 69 64 20 29 7b 0a 20 20 20 20  & eValid ){.    
d05f0 77 68 69 6c 65 28 20 7a 3c 7a 45 6e 64 20 26 26  while( z<zEnd &&
d0600 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28   sqlite3Isspace(
d0610 2a 7a 29 20 29 20 7a 2b 3d 69 6e 63 72 3b 0a 20  *z) ) z+=incr;. 
d0620 20 7d 0a 0a 64 6f 5f 61 74 6f 66 5f 63 61 6c 63   }..do_atof_calc
d0630 3a 0a 20 20 2f 2a 20 61 64 6a 75 73 74 20 65 78  :.  /* adjust ex
d0640 70 6f 6e 65 6e 74 20 62 79 20 64 2c 20 61 6e 64  ponent by d, and
d0650 20 75 70 64 61 74 65 20 73 69 67 6e 20 2a 2f 0a   update sign */.
d0660 20 20 65 20 3d 20 28 65 2a 65 73 69 67 6e 29 20    e = (e*esign) 
d0670 2b 20 64 3b 0a 20 20 69 66 28 20 65 3c 30 20 29  + d;.  if( e<0 )
d0680 20 7b 0a 20 20 20 20 65 73 69 67 6e 20 3d 20 2d   {.    esign = -
d0690 31 3b 0a 20 20 20 20 65 20 2a 3d 20 2d 31 3b 0a  1;.    e *= -1;.
d06a0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 65    } else {.    e
d06b0 73 69 67 6e 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20  sign = 1;.  }.. 
d06c0 20 2f 2a 20 69 66 20 30 20 73 69 67 6e 69 66 69   /* if 0 signifi
d06d0 63 61 6e 64 20 2a 2f 0a 20 20 69 66 28 20 21 73  cand */.  if( !s
d06e0 20 29 20 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74   ) {.    /* In t
d06f0 68 65 20 49 45 45 45 20 37 35 34 20 73 74 61 6e  he IEEE 754 stan
d0700 64 61 72 64 2c 20 7a 65 72 6f 20 69 73 20 73 69  dard, zero is si
d0710 67 6e 65 64 2e 0a 20 20 20 20 2a 2a 20 41 64 64  gned..    ** Add
d0720 20 74 68 65 20 73 69 67 6e 20 69 66 20 77 65 27   the sign if we'
d0730 76 65 20 73 65 65 6e 20 61 74 20 6c 65 61 73 74  ve seen at least
d0740 20 6f 6e 65 20 64 69 67 69 74 20 2a 2f 0a 20 20   one digit */.  
d0750 20 20 72 65 73 75 6c 74 20 3d 20 28 73 69 67 6e    result = (sign
d0760 3c 30 20 26 26 20 6e 44 69 67 69 74 73 29 20 3f  <0 && nDigits) ?
d0770 20 2d 28 64 6f 75 62 6c 65 29 30 20 3a 20 28 64   -(double)0 : (d
d0780 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 20 65 6c 73  ouble)0;.  } els
d0790 65 20 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65 6d  e {.    /* attem
d07a0 70 74 20 74 6f 20 72 65 64 75 63 65 20 65 78 70  pt to reduce exp
d07b0 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28  onent */.    if(
d07c0 20 65 73 69 67 6e 3e 30 20 29 7b 0a 20 20 20 20   esign>0 ){.    
d07d0 20 20 77 68 69 6c 65 28 20 73 3c 28 4c 41 52 47    while( s<(LARG
d07e0 45 53 54 5f 49 4e 54 36 34 2f 31 30 29 20 26 26  EST_INT64/10) &&
d07f0 20 65 3e 30 20 29 20 65 2d 2d 2c 73 2a 3d 31 30   e>0 ) e--,s*=10
d0800 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
d0810 20 20 20 77 68 69 6c 65 28 20 21 28 73 25 31 30     while( !(s%10
d0820 29 20 26 26 20 65 3e 30 20 29 20 65 2d 2d 2c 73  ) && e>0 ) e--,s
d0830 2f 3d 31 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  /=10;.    }..   
d0840 20 2f 2a 20 61 64 6a 75 73 74 20 74 68 65 20 73   /* adjust the s
d0850 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 61  ign of significa
d0860 6e 64 20 2a 2f 0a 20 20 20 20 73 20 3d 20 73 69  nd */.    s = si
d0870 67 6e 3c 30 20 3f 20 2d 73 20 3a 20 73 3b 0a 0a  gn<0 ? -s : s;..
d0880 20 20 20 20 2f 2a 20 69 66 20 65 78 70 6f 6e 65      /* if expone
d0890 6e 74 2c 20 73 63 61 6c 65 20 73 69 67 6e 69 66  nt, scale signif
d08a0 69 63 61 6e 64 20 61 73 20 61 70 70 72 6f 70 72  icand as appropr
d08b0 69 61 74 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20  iate.    ** and 
d08c0 73 74 6f 72 65 20 69 6e 20 72 65 73 75 6c 74 2e  store in result.
d08d0 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 20 29 7b   */.    if( e ){
d08e0 0a 20 20 20 20 20 20 4c 4f 4e 47 44 4f 55 42 4c  .      LONGDOUBL
d08f0 45 5f 54 59 50 45 20 73 63 61 6c 65 20 3d 20 31  E_TYPE scale = 1
d0900 2e 30 3b 0a 20 20 20 20 20 20 2f 2a 20 61 74 74  .0;.      /* att
d0910 65 6d 70 74 20 74 6f 20 68 61 6e 64 6c 65 20 65  empt to handle e
d0920 78 74 72 65 6d 65 6c 79 20 73 6d 61 6c 6c 2f 6c  xtremely small/l
d0930 61 72 67 65 20 6e 75 6d 62 65 72 73 20 62 65 74  arge numbers bet
d0940 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ter */.      if(
d0950 20 65 3e 33 30 37 20 26 26 20 65 3c 33 34 32 20   e>307 && e<342 
d0960 29 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  ){.        while
d0970 28 20 65 25 33 30 38 20 29 20 7b 20 73 63 61 6c  ( e%308 ) { scal
d0980 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 20 2d  e *= 1.0e+1; e -
d0990 3d 20 31 3b 20 7d 0a 20 20 20 20 20 20 20 20 69  = 1; }.        i
d09a0 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20  f( esign<0 ){.  
d09b0 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d          result =
d09c0 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20   s / scale;.    
d09d0 20 20 20 20 20 20 72 65 73 75 6c 74 20 2f 3d 20        result /= 
d09e0 31 2e 30 65 2b 33 30 38 3b 0a 20 20 20 20 20 20  1.0e+308;.      
d09f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
d0a00 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20 2a 20     result = s * 
d0a10 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 20  scale;.         
d0a20 20 72 65 73 75 6c 74 20 2a 3d 20 31 2e 30 65 2b   result *= 1.0e+
d0a30 33 30 38 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  308;.        }. 
d0a40 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65       }else if( e
d0a50 3e 3d 33 34 32 20 29 7b 0a 20 20 20 20 20 20 20  >=342 ){.       
d0a60 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a   if( esign<0 ){.
d0a70 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74            result
d0a80 20 3d 20 30 2e 30 2a 73 3b 0a 20 20 20 20 20 20   = 0.0*s;.      
d0a90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
d0aa0 20 20 20 72 65 73 75 6c 74 20 3d 20 31 65 33 30     result = 1e30
d0ab0 38 2a 31 65 33 30 38 2a 73 3b 20 20 2f 2a 20 49  8*1e308*s;  /* I
d0ac0 6e 66 69 6e 69 74 79 20 2a 2f 0a 20 20 20 20 20  nfinity */.     
d0ad0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
d0ae0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 31 2e 30  {.        /* 1.0
d0af0 65 2b 32 32 20 69 73 20 74 68 65 20 6c 61 72 67  e+22 is the larg
d0b00 65 73 74 20 70 6f 77 65 72 20 6f 66 20 31 30 20  est power of 10 
d0b10 74 68 61 6e 20 63 61 6e 20 62 65 20 0a 20 20 20  than can be .   
d0b20 20 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65 6e       ** represen
d0b30 74 65 64 20 65 78 61 63 74 6c 79 2e 20 2a 2f 0a  ted exactly. */.
d0b40 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 65          while( e
d0b50 25 32 32 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d  %22 ) { scale *=
d0b60 20 31 2e 30 65 2b 31 3b 20 65 20 2d 3d 20 31 3b   1.0e+1; e -= 1;
d0b70 20 7d 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65   }.        while
d0b80 28 20 65 3e 30 20 29 20 7b 20 73 63 61 6c 65 20  ( e>0 ) { scale 
d0b90 2a 3d 20 31 2e 30 65 2b 32 32 3b 20 65 20 2d 3d  *= 1.0e+22; e -=
d0ba0 20 32 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 69   22; }.        i
d0bb0 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20  f( esign<0 ){.  
d0bc0 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d          result =
d0bd0 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20   s / scale;.    
d0be0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d0bf0 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20       result = s 
d0c00 2a 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20  * scale;.       
d0c10 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
d0c20 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 72 65   else {.      re
d0c30 73 75 6c 74 20 3d 20 28 64 6f 75 62 6c 65 29 73  sult = (double)s
d0c40 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
d0c50 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75  * store the resu
d0c60 6c 74 20 2a 2f 0a 20 20 2a 70 52 65 73 75 6c 74  lt */.  *pResult
d0c70 20 3d 20 72 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a   = result;..  /*
d0c80 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   return true if 
d0c90 6e 75 6d 62 65 72 20 61 6e 64 20 6e 6f 20 65 78  number and no ex
d0ca0 74 72 61 20 6e 6f 6e 2d 77 68 69 74 65 73 70 61  tra non-whitespa
d0cb0 63 65 20 63 68 72 61 63 74 65 72 73 20 61 66 74  ce chracters aft
d0cc0 65 72 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 7a  er */.  return z
d0cd0 3e 3d 7a 45 6e 64 20 26 26 20 6e 44 69 67 69 74  >=zEnd && nDigit
d0ce0 73 3e 30 20 26 26 20 65 56 61 6c 69 64 3b 0a 23  s>0 && eValid;.#
d0cf0 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 21 73  else.  return !s
d0d00 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 2c 20  qlite3Atoi64(z, 
d0d10 70 52 65 73 75 6c 74 2c 20 6c 65 6e 67 74 68 2c  pResult, length,
d0d20 20 65 6e 63 29 3b 0a 23 65 6e 64 69 66 20 2f 2a   enc);.#endif /*
d0d30 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
d0d40 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 7d  ATING_POINT */.}
d0d50 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20  ../*.** Compare 
d0d60 74 68 65 20 31 39 2d 63 68 61 72 61 63 74 65 72  the 19-character
d0d70 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 61 67 61   string zNum aga
d0d80 69 6e 73 74 20 74 68 65 20 74 65 78 74 20 72 65  inst the text re
d0d90 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20  presentation.** 
d0da0 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 39 32 32  value 2^63:  922
d0db0 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 38  3372036854775808
d0dc0 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 61 74 69  .  Return negati
d0dd0 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73  ve, zero, or pos
d0de0 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a 4e 75 6d  itive.** if zNum
d0df0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65   is less than, e
d0e00 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61  qual to, or grea
d0e10 74 65 72 20 74 68 61 6e 20 74 68 65 20 73 74 72  ter than the str
d0e20 69 6e 67 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ing..** Note tha
d0e30 74 20 7a 4e 75 6d 20 6d 75 73 74 20 63 6f 6e 74  t zNum must cont
d0e40 61 69 6e 20 65 78 61 63 74 6c 79 20 31 39 20 63  ain exactly 19 c
d0e50 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a  haracters..**.**
d0e60 20 55 6e 6c 69 6b 65 20 6d 65 6d 63 6d 70 28 29   Unlike memcmp()
d0e70 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
d0e80 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 72   guaranteed to r
d0e90 65 74 75 72 6e 20 74 68 65 20 64 69 66 66 65 72  eturn the differ
d0ea0 65 6e 63 65 0a 2a 2a 20 69 6e 20 74 68 65 20 76  ence.** in the v
d0eb0 61 6c 75 65 73 20 6f 66 20 74 68 65 20 6c 61 73  alues of the las
d0ec0 74 20 64 69 67 69 74 20 69 66 20 74 68 65 20 6f  t digit if the o
d0ed0 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 69  nly difference i
d0ee0 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6c 61 73 74  s in the.** last
d0ef0 20 64 69 67 69 74 2e 20 20 53 6f 2c 20 66 6f 72   digit.  So, for
d0f00 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20   example,.**.** 
d0f10 20 20 20 20 20 63 6f 6d 70 61 72 65 32 70 6f 77       compare2pow
d0f20 36 33 28 22 39 32 32 33 33 37 32 30 33 36 38 35  63("922337203685
d0f30 34 37 37 35 38 30 30 22 2c 20 31 29 0a 2a 2a 0a  4775800", 1).**.
d0f40 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 2d  ** will return -
d0f50 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  8..*/.static int
d0f60 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 63   compare2pow63(c
d0f70 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c  onst char *zNum,
d0f80 20 69 6e 74 20 69 6e 63 72 29 7b 0a 20 20 69 6e   int incr){.  in
d0f90 74 20 63 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  t c = 0;.  int i
d0fa0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
d0fb0 20 20 20 20 20 20 2f 2a 20 30 31 32 33 34 35 36        /* 0123456
d0fc0 37 38 39 30 31 32 33 34 35 36 37 20 2a 2f 0a 20  78901234567 */. 
d0fd0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 6f 77   const char *pow
d0fe0 36 33 20 3d 20 22 39 32 32 33 33 37 32 30 33 36  63 = "9223372036
d0ff0 38 35 34 37 37 35 38 30 22 3b 0a 20 20 66 6f 72  85477580";.  for
d1000 28 69 3d 30 3b 20 63 3d 3d 30 20 26 26 20 69 3c  (i=0; c==0 && i<
d1010 31 38 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 20  18; i++){.    c 
d1020 3d 20 28 7a 4e 75 6d 5b 69 2a 69 6e 63 72 5d 2d  = (zNum[i*incr]-
d1030 70 6f 77 36 33 5b 69 5d 29 2a 31 30 3b 0a 20 20  pow63[i])*10;.  
d1040 7d 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a  }.  if( c==0 ){.
d1050 20 20 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 2a      c = zNum[18*
d1060 69 6e 63 72 5d 20 2d 20 27 38 27 3b 0a 20 20 20  incr] - '8';.   
d1070 20 74 65 73 74 63 61 73 65 28 20 63 3d 3d 28 2d   testcase( c==(-
d1080 31 29 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  1) );.    testca
d1090 73 65 28 20 63 3d 3d 30 20 29 3b 0a 20 20 20 20  se( c==0 );.    
d10a0 74 65 73 74 63 61 73 65 28 20 63 3d 3d 28 2b 31  testcase( c==(+1
d10b0 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ) );.  }.  retur
d10c0 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43  n c;.}.../*.** C
d10d0 6f 6e 76 65 72 74 20 7a 4e 75 6d 20 74 6f 20 61  onvert zNum to a
d10e0 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69   64-bit signed i
d10f0 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66  nteger..**.** If
d1100 20 74 68 65 20 7a 4e 75 6d 20 76 61 6c 75 65 20   the zNum value 
d1110 69 73 20 72 65 70 72 65 73 65 6e 74 61 62 6c 65  is representable
d1120 20 61 73 20 61 20 36 34 2d 62 69 74 20 74 77 6f   as a 64-bit two
d1130 73 2d 63 6f 6d 70 6c 65 6d 65 6e 74 20 0a 2a 2a  s-complement .**
d1140 20 69 6e 74 65 67 65 72 2c 20 74 68 65 6e 20 77   integer, then w
d1150 72 69 74 65 20 74 68 61 74 20 76 61 6c 75 65 20  rite that value 
d1160 69 6e 74 6f 20 2a 70 4e 75 6d 20 61 6e 64 20 72  into *pNum and r
d1170 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49  eturn 0..**.** I
d1180 66 20 7a 4e 75 6d 20 69 73 20 65 78 61 63 74 6c  f zNum is exactl
d1190 79 20 39 32 32 33 33 37 32 30 33 36 38 35 34 36  y 92233720368546
d11a0 36 35 38 30 38 2c 20 72 65 74 75 72 6e 20 32 2e  65808, return 2.
d11b0 20 20 54 68 69 73 20 73 70 65 63 69 61 6c 0a 2a    This special.*
d11c0 2a 20 63 61 73 65 20 69 73 20 62 72 6f 6b 65 6e  * case is broken
d11d0 20 6f 75 74 20 62 65 63 61 75 73 65 20 77 68 69   out because whi
d11e0 6c 65 20 39 32 32 33 33 37 32 30 33 36 38 35 34  le 9223372036854
d11f0 36 36 35 38 30 38 20 63 61 6e 6e 6f 74 20 62 65  665808 cannot be
d1200 20 61 20 0a 2a 2a 20 73 69 67 6e 65 64 20 36 34   a .** signed 64
d1210 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 69 74  -bit integer, it
d1220 73 20 6e 65 67 61 74 69 76 65 20 2d 39 32 32 33  s negative -9223
d1230 33 37 32 30 33 36 38 35 34 36 36 35 38 30 38 20  372036854665808 
d1240 63 61 6e 20 62 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  can be..**.** If
d1250 20 7a 4e 75 6d 20 69 73 20 74 6f 6f 20 62 69 67   zNum is too big
d1260 20 66 6f 72 20 61 20 36 34 2d 62 69 74 20 69 6e   for a 64-bit in
d1270 74 65 67 65 72 20 61 6e 64 20 69 73 20 6e 6f 74  teger and is not
d1280 0a 2a 2a 20 39 32 32 33 33 37 32 30 33 36 38 35  .** 922337203685
d1290 34 36 36 35 38 30 38 20 74 68 65 6e 20 72 65 74  4665808 then ret
d12a0 75 72 6e 20 31 2e 0a 2a 2a 0a 2a 2a 20 6c 65 6e  urn 1..**.** len
d12b0 67 74 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65  gth is the numbe
d12c0 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
d12d0 65 20 73 74 72 69 6e 67 20 28 62 79 74 65 73 2c  e string (bytes,
d12e0 20 6e 6f 74 20 63 68 61 72 61 63 74 65 72 73 29   not characters)
d12f0 2e 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20  ..** The string 
d1300 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  is not necessari
d1310 6c 79 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  ly zero-terminat
d1320 65 64 2e 20 20 54 68 65 20 65 6e 63 6f 64 69 6e  ed.  The encodin
d1330 67 20 69 73 0a 2a 2a 20 67 69 76 65 6e 20 62 79  g is.** given by
d1340 20 65 6e 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   enc..*/.SQLITE_
d1350 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
d1360 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74 20  te3Atoi64(const 
d1370 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 36 34 20  char *zNum, i64 
d1380 2a 70 4e 75 6d 2c 20 69 6e 74 20 6c 65 6e 67 74  *pNum, int lengt
d1390 68 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69 6e  h, u8 enc){.  in
d13a0 74 20 69 6e 63 72 20 3d 20 28 65 6e 63 3d 3d 53  t incr = (enc==S
d13b0 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b  QLITE_UTF8?1:2);
d13c0 0a 20 20 75 36 34 20 75 20 3d 20 30 3b 0a 20 20  .  u64 u = 0;.  
d13d0 69 6e 74 20 6e 65 67 20 3d 20 30 3b 20 2f 2a 20  int neg = 0; /* 
d13e0 61 73 73 75 6d 65 20 70 6f 73 69 74 69 76 65 20  assume positive 
d13f0 2a 2f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  */.  int i;.  in
d1400 74 20 63 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74  t c = 0;.  const
d1410 20 63 68 61 72 20 2a 7a 53 74 61 72 74 3b 0a 20   char *zStart;. 
d1420 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e   const char *zEn
d1430 64 20 3d 20 7a 4e 75 6d 20 2b 20 6c 65 6e 67 74  d = zNum + lengt
d1440 68 3b 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53 51  h;.  if( enc==SQ
d1450 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 20 7a  LITE_UTF16BE ) z
d1460 4e 75 6d 2b 2b 3b 0a 20 20 77 68 69 6c 65 28 20  Num++;.  while( 
d1470 7a 4e 75 6d 3c 7a 45 6e 64 20 26 26 20 73 71 6c  zNum<zEnd && sql
d1480 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 4e 75  ite3Isspace(*zNu
d1490 6d 29 20 29 20 7a 4e 75 6d 2b 3d 69 6e 63 72 3b  m) ) zNum+=incr;
d14a0 0a 20 20 69 66 28 20 7a 4e 75 6d 3c 7a 45 6e 64  .  if( zNum<zEnd
d14b0 20 29 7b 0a 20 20 20 20 69 66 28 20 2a 7a 4e 75   ){.    if( *zNu
d14c0 6d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20  m=='-' ){.      
d14d0 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a  neg = 1;.      z
d14e0 4e 75 6d 2b 3d 69 6e 63 72 3b 0a 20 20 20 20 7d  Num+=incr;.    }
d14f0 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d  else if( *zNum==
d1500 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 7a 4e 75  '+' ){.      zNu
d1510 6d 2b 3d 69 6e 63 72 3b 0a 20 20 20 20 7d 0a 20  m+=incr;.    }. 
d1520 20 7d 0a 20 20 7a 53 74 61 72 74 20 3d 20 7a 4e   }.  zStart = zN
d1530 75 6d 3b 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75  um;.  while( zNu
d1540 6d 3c 7a 45 6e 64 20 26 26 20 7a 4e 75 6d 5b 30  m<zEnd && zNum[0
d1550 5d 3d 3d 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 3d  ]=='0' ){ zNum+=
d1560 69 6e 63 72 3b 20 7d 20 2f 2a 20 53 6b 69 70 20  incr; } /* Skip 
d1570 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 2a  leading zeros. *
d1580 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 26 7a 4e  /.  for(i=0; &zN
d1590 75 6d 5b 69 5d 3c 7a 45 6e 64 20 26 26 20 28 63  um[i]<zEnd && (c
d15a0 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26  =zNum[i])>='0' &
d15b0 26 20 63 3c 3d 27 39 27 3b 20 69 2b 3d 69 6e 63  & c<='9'; i+=inc
d15c0 72 29 7b 0a 20 20 20 20 75 20 3d 20 75 2a 31 30  r){.    u = u*10
d15d0 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 7d 0a   + c - '0';.  }.
d15e0 20 20 69 66 28 20 75 3e 4c 41 52 47 45 53 54 5f    if( u>LARGEST_
d15f0 49 4e 54 36 34 20 29 7b 0a 20 20 20 20 2a 70 4e  INT64 ){.    *pN
d1600 75 6d 20 3d 20 53 4d 41 4c 4c 45 53 54 5f 49 4e  um = SMALLEST_IN
d1610 54 36 34 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  T64;.  }else if(
d1620 20 6e 65 67 20 29 7b 0a 20 20 20 20 2a 70 4e 75   neg ){.    *pNu
d1630 6d 20 3d 20 2d 28 69 36 34 29 75 3b 0a 20 20 7d  m = -(i64)u;.  }
d1640 65 6c 73 65 7b 0a 20 20 20 20 2a 70 4e 75 6d 20  else{.    *pNum 
d1650 3d 20 28 69 36 34 29 75 3b 0a 20 20 7d 0a 20 20  = (i64)u;.  }.  
d1660 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 38 20  testcase( i==18 
d1670 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 69  );.  testcase( i
d1680 3d 3d 31 39 20 29 3b 0a 20 20 74 65 73 74 63 61  ==19 );.  testca
d1690 73 65 28 20 69 3d 3d 32 30 20 29 3b 0a 20 20 69  se( i==20 );.  i
d16a0 66 28 20 28 63 21 3d 30 20 26 26 20 26 7a 4e 75  f( (c!=0 && &zNu
d16b0 6d 5b 69 5d 3c 7a 45 6e 64 29 20 7c 7c 20 28 69  m[i]<zEnd) || (i
d16c0 3d 3d 30 20 26 26 20 7a 53 74 61 72 74 3d 3d 7a  ==0 && zStart==z
d16d0 4e 75 6d 29 20 7c 7c 20 69 3e 31 39 2a 69 6e 63  Num) || i>19*inc
d16e0 72 20 29 7b 0a 20 20 20 20 2f 2a 20 7a 4e 75 6d  r ){.    /* zNum
d16f0 20 69 73 20 65 6d 70 74 79 20 6f 72 20 63 6f 6e   is empty or con
d1700 74 61 69 6e 73 20 6e 6f 6e 2d 6e 75 6d 65 72 69  tains non-numeri
d1710 63 20 74 65 78 74 20 6f 72 20 69 73 20 6c 6f 6e  c text or is lon
d1720 67 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20  ger.    ** than 
d1730 31 39 20 64 69 67 69 74 73 20 28 74 68 75 73 20  19 digits (thus 
d1740 67 75 61 72 61 6e 74 65 65 69 6e 67 20 74 68 61  guaranteeing tha
d1750 74 20 69 74 20 69 73 20 74 6f 6f 20 6c 61 72 67  t it is too larg
d1760 65 29 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  e) */.    return
d1770 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   1;.  }else if( 
d1780 69 3c 31 39 2a 69 6e 63 72 20 29 7b 0a 20 20 20  i<19*incr ){.   
d1790 20 2f 2a 20 4c 65 73 73 20 74 68 61 6e 20 31 39   /* Less than 19
d17a0 20 64 69 67 69 74 73 2c 20 73 6f 20 77 65 20 6b   digits, so we k
d17b0 6e 6f 77 20 74 68 61 74 20 69 74 20 66 69 74 73  now that it fits
d17c0 20 69 6e 20 36 34 20 62 69 74 73 20 2a 2f 0a 20   in 64 bits */. 
d17d0 20 20 20 61 73 73 65 72 74 28 20 75 3c 3d 4c 41     assert( u<=LA
d17e0 52 47 45 53 54 5f 49 4e 54 36 34 20 29 3b 0a 20  RGEST_INT64 );. 
d17f0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
d1800 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 7a 4e 75  else{.    /* zNu
d1810 6d 20 69 73 20 61 20 31 39 2d 64 69 67 69 74 20  m is a 19-digit 
d1820 6e 75 6d 62 65 72 73 2e 20 20 43 6f 6d 70 61 72  numbers.  Compar
d1830 65 20 69 74 20 61 67 61 69 6e 73 74 20 39 32 32  e it against 922
d1840 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 38  3372036854775808
d1850 2e 20 2a 2f 0a 20 20 20 20 63 20 3d 20 63 6f 6d  . */.    c = com
d1860 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d 2c  pare2pow63(zNum,
d1870 20 69 6e 63 72 29 3b 0a 20 20 20 20 69 66 28 20   incr);.    if( 
d1880 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  c<0 ){.      /* 
d1890 7a 4e 75 6d 20 69 73 20 6c 65 73 73 20 74 68 61  zNum is less tha
d18a0 6e 20 39 32 32 33 33 37 32 30 33 36 38 35 34 37  n 92233720368547
d18b0 37 35 38 30 38 20 73 6f 20 69 74 20 66 69 74 73  75808 so it fits
d18c0 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
d18d0 28 20 75 3c 3d 4c 41 52 47 45 53 54 5f 49 4e 54  ( u<=LARGEST_INT
d18e0 36 34 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75  64 );.      retu
d18f0 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20  rn 0;.    }else 
d1900 69 66 28 20 63 3e 30 20 29 7b 0a 20 20 20 20 20  if( c>0 ){.     
d1910 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 67 72 65 61   /* zNum is grea
d1920 74 65 72 20 74 68 61 6e 20 39 32 32 33 33 37 32  ter than 9223372
d1930 30 33 36 38 35 34 37 37 35 38 30 38 20 73 6f 20  036854775808 so 
d1940 69 74 20 6f 76 65 72 66 6c 6f 77 73 20 2a 2f 0a  it overflows */.
d1950 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
d1960 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d1970 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65 78 61 63   /* zNum is exac
d1980 74 6c 79 20 39 32 32 33 33 37 32 30 33 36 38 35  tly 922337203685
d1990 34 37 37 35 38 30 38 2e 20 20 46 69 74 73 20 69  4775808.  Fits i
d19a0 66 20 6e 65 67 61 74 69 76 65 2e 20 20 54 68 65  f negative.  The
d19b0 0a 20 20 20 20 20 20 2a 2a 20 73 70 65 63 69 61  .      ** specia
d19c0 6c 20 63 61 73 65 20 32 20 6f 76 65 72 66 6c 6f  l case 2 overflo
d19d0 77 20 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f  w if positive */
d19e0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75  .      assert( u
d19f0 2d 31 3d 3d 4c 41 52 47 45 53 54 5f 49 4e 54 36  -1==LARGEST_INT6
d1a00 34 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  4 );.      asser
d1a10 74 28 20 28 2a 70 4e 75 6d 29 3d 3d 53 4d 41 4c  t( (*pNum)==SMAL
d1a20 4c 45 53 54 5f 49 4e 54 36 34 20 29 3b 0a 20 20  LEST_INT64 );.  
d1a30 20 20 20 20 72 65 74 75 72 6e 20 6e 65 67 20 3f      return neg ?
d1a40 20 30 20 3a 20 32 3b 0a 20 20 20 20 7d 0a 20 20   0 : 2;.    }.  
d1a50 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e  }.}../*.** If zN
d1a60 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e  um represents an
d1a70 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 77 69   integer that wi
d1a80 6c 6c 20 66 69 74 20 69 6e 20 33 32 2d 62 69 74  ll fit in 32-bit
d1a90 73 2c 20 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a  s, then set.** *
d1aa0 70 56 61 6c 75 65 20 74 6f 20 74 68 61 74 20 69  pValue to that i
d1ab0 6e 74 65 67 65 72 20 61 6e 64 20 72 65 74 75 72  nteger and retur
d1ac0 6e 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69  n true.  Otherwi
d1ad0 73 65 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e  se return false.
d1ae0 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e  .**.** Any non-n
d1af0 75 6d 65 72 69 63 20 63 68 61 72 61 63 74 65 72  umeric character
d1b00 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67  s that following
d1b10 20 7a 4e 75 6d 20 61 72 65 20 69 67 6e 6f 72 65   zNum are ignore
d1b20 64 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 69  d..** This is di
d1b30 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c  fferent from sql
d1b40 69 74 65 33 41 74 6f 69 36 34 28 29 20 77 68 69  ite3Atoi64() whi
d1b50 63 68 20 72 65 71 75 69 72 65 73 20 74 68 65 0a  ch requires the.
d1b60 2a 2a 20 69 6e 70 75 74 20 6e 75 6d 62 65 72 20  ** input number 
d1b70 74 6f 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69  to be zero-termi
d1b80 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nated..*/.SQLITE
d1b90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
d1ba0 69 74 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e  ite3GetInt32(con
d1bb0 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69  st char *zNum, i
d1bc0 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73  nt *pValue){.  s
d1bd0 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 20 3d 20  qlite_int64 v = 
d1be0 30 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20  0;.  int i, c;. 
d1bf0 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20   int neg = 0;.  
d1c00 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27  if( zNum[0]=='-'
d1c10 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b   ){.    neg = 1;
d1c20 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d  .    zNum++;.  }
d1c30 65 6c 73 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d  else if( zNum[0]
d1c40 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75  =='+' ){.    zNu
d1c50 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65  m++;.  }.  while
d1c60 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29  ( zNum[0]=='0' )
d1c70 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69   zNum++;.  for(i
d1c80 3d 30 3b 20 69 3c 31 31 20 26 26 20 28 63 20 3d  =0; i<11 && (c =
d1c90 20 7a 4e 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e   zNum[i] - '0')>
d1ca0 3d 30 20 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29  =0 && c<=9; i++)
d1cb0 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b  {.    v = v*10 +
d1cc0 20 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68   c;.  }..  /* Th
d1cd0 65 20 6c 6f 6e 67 65 73 74 20 64 65 63 69 6d 61  e longest decima
d1ce0 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  l representation
d1cf0 20 6f 66 20 61 20 33 32 20 62 69 74 20 69 6e 74   of a 32 bit int
d1d00 65 67 65 72 20 69 73 20 31 30 20 64 69 67 69 74  eger is 10 digit
d1d10 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  s:.  **.  **    
d1d20 20 20 20 20 20 20 20 20 20 31 32 33 34 35 36 37           1234567
d1d30 38 39 30 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33  890.  **     2^3
d1d40 31 20 2d 3e 20 32 31 34 37 34 38 33 36 34 38 0a  1 -> 2147483648.
d1d50 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28    */.  testcase(
d1d60 20 69 3d 3d 31 30 20 29 3b 0a 20 20 69 66 28 20   i==10 );.  if( 
d1d70 69 3e 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75  i>10 ){.    retu
d1d80 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 74 65 73 74  rn 0;.  }.  test
d1d90 63 61 73 65 28 20 76 2d 6e 65 67 3d 3d 32 31 34  case( v-neg==214
d1da0 37 34 38 33 36 34 37 20 29 3b 0a 20 20 69 66 28  7483647 );.  if(
d1db0 20 76 2d 6e 65 67 3e 32 31 34 37 34 38 33 36 34   v-neg>214748364
d1dc0 37 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  7 ){.    return 
d1dd0 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67  0;.  }.  if( neg
d1de0 20 29 7b 0a 20 20 20 20 76 20 3d 20 2d 76 3b 0a   ){.    v = -v;.
d1df0 20 20 7d 0a 20 20 2a 70 56 61 6c 75 65 20 3d 20    }.  *pValue = 
d1e00 28 69 6e 74 29 76 3b 0a 20 20 72 65 74 75 72 6e  (int)v;.  return
d1e10 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74   1;.}../*.** Ret
d1e20 75 72 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74  urn a 32-bit int
d1e30 65 67 65 72 20 76 61 6c 75 65 20 65 78 74 72 61  eger value extra
d1e40 63 74 65 64 20 66 72 6f 6d 20 61 20 73 74 72 69  cted from a stri
d1e50 6e 67 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 73  ng.  If the.** s
d1e60 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 61 6e 20  tring is not an 
d1e70 69 6e 74 65 67 65 72 2c 20 6a 75 73 74 20 72 65  integer, just re
d1e80 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54  turn 0..*/.SQLIT
d1e90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
d1ea0 6c 69 74 65 33 41 74 6f 69 28 63 6f 6e 73 74 20  lite3Atoi(const 
d1eb0 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20  char *z){.  int 
d1ec0 78 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a 20 29  x = 0;.  if( z )
d1ed0 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32   sqlite3GetInt32
d1ee0 28 7a 2c 20 26 78 29 3b 0a 20 20 72 65 74 75 72  (z, &x);.  retur
d1ef0 6e 20 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  n x;.}../*.** Th
d1f00 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  e variable-lengt
d1f10 68 20 69 6e 74 65 67 65 72 20 65 6e 63 6f 64 69  h integer encodi
d1f20 6e 67 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  ng is as follows
d1f30 3a 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20  :.**.** KEY:.** 
d1f40 20 20 20 20 20 20 20 20 41 20 3d 20 30 78 78 78          A = 0xxx
d1f50 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20 6f  xxxx    7 bits o
d1f60 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 66  f data and one f
d1f70 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 20  lag bit.**      
d1f80 20 20 20 42 20 3d 20 31 78 78 78 78 78 78 78 20     B = 1xxxxxxx 
d1f90 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 61 74     7 bits of dat
d1fa0 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62  a and one flag b
d1fb0 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 43 20  it.**         C 
d1fc0 3d 20 78 78 78 78 78 78 78 78 20 20 20 20 38 20  = xxxxxxxx    8 
d1fd0 62 69 74 73 20 6f 66 20 64 61 74 61 0a 2a 2a 0a  bits of data.**.
d1fe0 2a 2a 20 20 37 20 62 69 74 73 20 2d 20 41 0a 2a  **  7 bits - A.*
d1ff0 2a 20 31 34 20 62 69 74 73 20 2d 20 42 41 0a 2a  * 14 bits - BA.*
d2000 2a 20 32 31 20 62 69 74 73 20 2d 20 42 42 41 0a  * 21 bits - BBA.
d2010 2a 2a 20 32 38 20 62 69 74 73 20 2d 20 42 42 42  ** 28 bits - BBB
d2020 41 0a 2a 2a 20 33 35 20 62 69 74 73 20 2d 20 42  A.** 35 bits - B
d2030 42 42 42 41 0a 2a 2a 20 34 32 20 62 69 74 73 20  BBBA.** 42 bits 
d2040 2d 20 42 42 42 42 42 41 0a 2a 2a 20 34 39 20 62  - BBBBBA.** 49 b
d2050 69 74 73 20 2d 20 42 42 42 42 42 42 41 0a 2a 2a  its - BBBBBBA.**
d2060 20 35 36 20 62 69 74 73 20 2d 20 42 42 42 42 42   56 bits - BBBBB
d2070 42 42 41 0a 2a 2a 20 36 34 20 62 69 74 73 20 2d  BBA.** 64 bits -
d2080 20 42 42 42 42 42 42 42 42 43 0a 2a 2f 0a 0a 2f   BBBBBBBBC.*/../
d2090 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 36 34 2d  *.** Write a 64-
d20a0 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  bit variable-len
d20b0 67 74 68 20 69 6e 74 65 67 65 72 20 74 6f 20 6d  gth integer to m
d20c0 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61  emory starting a
d20d0 74 20 70 5b 30 5d 2e 0a 2a 2a 20 54 68 65 20 6c  t p[0]..** The l
d20e0 65 6e 67 74 68 20 6f 66 20 64 61 74 61 20 77 72  ength of data wr
d20f0 69 74 65 20 77 69 6c 6c 20 62 65 20 62 65 74 77  ite will be betw
d2100 65 65 6e 20 31 20 61 6e 64 20 39 20 62 79 74 65  een 1 and 9 byte
d2110 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a  s.  The number.*
d2120 2a 20 6f 66 20 62 79 74 65 73 20 77 72 69 74 74  * of bytes writt
d2130 65 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  en is returned..
d2140 2a 2a 0a 2a 2a 20 41 20 76 61 72 69 61 62 6c 65  **.** A variable
d2150 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20  -length integer 
d2160 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20  consists of the 
d2170 6c 6f 77 65 72 20 37 20 62 69 74 73 20 6f 66 20  lower 7 bits of 
d2180 65 61 63 68 20 62 79 74 65 0a 2a 2a 20 66 6f 72  each byte.** for
d2190 20 61 6c 6c 20 62 79 74 65 73 20 74 68 61 74 20   all bytes that 
d21a0 68 61 76 65 20 74 68 65 20 38 74 68 20 62 69 74  have the 8th bit
d21b0 20 73 65 74 20 61 6e 64 20 6f 6e 65 20 62 79 74   set and one byt
d21c0 65 20 77 69 74 68 20 74 68 65 20 38 74 68 0a 2a  e with the 8th.*
d21d0 2a 20 62 69 74 20 63 6c 65 61 72 2e 20 20 45 78  * bit clear.  Ex
d21e0 63 65 70 74 2c 20 69 66 20 77 65 20 67 65 74 20  cept, if we get 
d21f0 74 6f 20 74 68 65 20 39 74 68 20 62 79 74 65 2c  to the 9th byte,
d2200 20 69 74 20 73 74 6f 72 65 73 20 74 68 65 20 66   it stores the f
d2210 75 6c 6c 0a 2a 2a 20 38 20 62 69 74 73 20 61 6e  ull.** 8 bits an
d2220 64 20 69 73 20 74 68 65 20 6c 61 73 74 20 62 79  d is the last by
d2230 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  te..*/.SQLITE_PR
d2240 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
d2250 33 50 75 74 56 61 72 69 6e 74 28 75 6e 73 69 67  3PutVarint(unsig
d2260 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 36 34  ned char *p, u64
d2270 20 76 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c   v){.  int i, j,
d2280 20 6e 3b 0a 20 20 75 38 20 62 75 66 5b 31 30 5d   n;.  u8 buf[10]
d2290 3b 0a 20 20 69 66 28 20 76 20 26 20 28 28 28 75  ;.  if( v & (((u
d22a0 36 34 29 30 78 66 66 30 30 30 30 30 30 29 3c 3c  64)0xff000000)<<
d22b0 33 32 29 20 29 7b 0a 20 20 20 20 70 5b 38 5d 20  32) ){.    p[8] 
d22c0 3d 20 28 75 38 29 76 3b 0a 20 20 20 20 76 20 3e  = (u8)v;.    v >
d22d0 3e 3d 20 38 3b 0a 20 20 20 20 66 6f 72 28 69 3d  >= 8;.    for(i=
d22e0 37 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20  7; i>=0; i--){. 
d22f0 20 20 20 20 20 70 5b 69 5d 20 3d 20 28 75 38 29       p[i] = (u8)
d2300 28 28 76 20 26 20 30 78 37 66 29 20 7c 20 30 78  ((v & 0x7f) | 0x
d2310 38 30 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d  80);.      v >>=
d2320 20 37 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65   7;.    }.    re
d2330 74 75 72 6e 20 39 3b 0a 20 20 7d 20 20 20 20 0a  turn 9;.  }    .
d2340 20 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20    n = 0;.  do{. 
d2350 20 20 20 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28 75     buf[n++] = (u
d2360 38 29 28 28 76 20 26 20 30 78 37 66 29 20 7c 20  8)((v & 0x7f) | 
d2370 30 78 38 30 29 3b 0a 20 20 20 20 76 20 3e 3e 3d  0x80);.    v >>=
d2380 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21   7;.  }while( v!
d2390 3d 30 20 29 3b 0a 20 20 62 75 66 5b 30 5d 20 26  =0 );.  buf[0] &
d23a0 3d 20 30 78 37 66 3b 0a 20 20 61 73 73 65 72 74  = 0x7f;.  assert
d23b0 28 20 6e 3c 3d 39 20 29 3b 0a 20 20 66 6f 72 28  ( n<=9 );.  for(
d23c0 69 3d 30 2c 20 6a 3d 6e 2d 31 3b 20 6a 3e 3d 30  i=0, j=n-1; j>=0
d23d0 3b 20 6a 2d 2d 2c 20 69 2b 2b 29 7b 0a 20 20 20  ; j--, i++){.   
d23e0 20 70 5b 69 5d 20 3d 20 62 75 66 5b 6a 5d 3b 0a   p[i] = buf[j];.
d23f0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a    }.  return n;.
d2400 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
d2410 75 74 69 6e 65 20 69 73 20 61 20 66 61 73 74 65  utine is a faste
d2420 72 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c  r version of sql
d2430 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 29 20  ite3PutVarint() 
d2440 74 68 61 74 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72  that only.** wor
d2450 6b 73 20 66 6f 72 20 33 32 2d 62 69 74 20 70 6f  ks for 32-bit po
d2460 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 73 20  sitive integers 
d2470 61 6e 64 20 77 68 69 63 68 20 69 73 20 6f 70 74  and which is opt
d2480 69 6d 69 7a 65 64 20 66 6f 72 0a 2a 2a 20 74 68  imized for.** th
d2490 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 6f 66  e common case of
d24a0 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e   small integers.
d24b0 20 20 41 20 4d 41 43 52 4f 20 76 65 72 73 69 6f    A MACRO versio
d24c0 6e 2c 20 70 75 74 56 61 72 69 6e 74 33 32 2c 0a  n, putVarint32,.
d24d0 2a 2a 20 69 73 20 70 72 6f 76 69 64 65 64 20 77  ** is provided w
d24e0 68 69 63 68 20 69 6e 6c 69 6e 65 73 20 74 68 65  hich inlines the
d24f0 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73   single-byte cas
d2500 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f  e.  All code sho
d2510 75 6c 64 20 75 73 65 0a 2a 2a 20 74 68 65 20 4d  uld use.** the M
d2520 41 43 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20  ACRO version as 
d2530 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73  this function as
d2540 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67 6c 65  sumes the single
d2550 2d 62 79 74 65 20 63 61 73 65 20 68 61 73 0a 2a  -byte case has.*
d2560 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 68  * already been h
d2570 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  andled..*/.SQLIT
d2580 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
d2590 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32  lite3PutVarint32
d25a0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
d25b0 70 2c 20 75 33 32 20 76 29 7b 0a 23 69 66 6e 64  p, u32 v){.#ifnd
d25c0 65 66 20 70 75 74 56 61 72 69 6e 74 33 32 0a 20  ef putVarint32. 
d25d0 20 69 66 28 20 28 76 20 26 20 7e 30 78 37 66 29   if( (v & ~0x7f)
d25e0 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20  ==0 ){.    p[0] 
d25f0 3d 20 76 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = v;.    return 
d2600 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  1;.  }.#endif.  
d2610 69 66 28 20 28 76 20 26 20 7e 30 78 33 66 66 66  if( (v & ~0x3fff
d2620 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d  )==0 ){.    p[0]
d2630 20 3d 20 28 75 38 29 28 28 76 3e 3e 37 29 20 7c   = (u8)((v>>7) |
d2640 20 30 78 38 30 29 3b 0a 20 20 20 20 70 5b 31 5d   0x80);.    p[1]
d2650 20 3d 20 28 75 38 29 28 76 20 26 20 30 78 37 66   = (u8)(v & 0x7f
d2660 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b  );.    return 2;
d2670 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71  .  }.  return sq
d2680 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 70  lite3PutVarint(p
d2690 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42  , v);.}../*.** B
d26a0 69 74 6d 61 73 6b 73 20 75 73 65 64 20 62 79 20  itmasks used by 
d26b0 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
d26c0 28 29 2e 20 20 54 68 65 73 65 20 70 72 65 63 6f  ().  These preco
d26d0 6d 70 75 74 65 64 20 63 6f 6e 73 74 61 6e 74 73  mputed constants
d26e0 0a 2a 2a 20 61 72 65 20 64 65 66 69 6e 65 64 20  .** are defined 
d26f0 68 65 72 65 20 72 61 74 68 65 72 20 74 68 61 6e  here rather than
d2700 20 73 69 6d 70 6c 79 20 70 75 74 74 69 6e 67 20   simply putting 
d2710 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 65 78 70  the constant exp
d2720 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 69 6e 6c 69  ressions.** inli
d2730 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 77  ne in order to w
d2740 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20  ork around bugs 
d2750 69 6e 20 74 68 65 20 52 56 54 20 63 6f 6d 70 69  in the RVT compi
d2760 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 53 4c 4f 54 5f  ler..**.** SLOT_
d2770 32 5f 30 20 20 20 20 20 41 20 6d 61 73 6b 20 66  2_0     A mask f
d2780 6f 72 20 20 28 30 78 37 66 3c 3c 31 34 29 20 7c  or  (0x7f<<14) |
d2790 20 30 78 37 66 0a 2a 2a 0a 2a 2a 20 53 4c 4f 54   0x7f.**.** SLOT
d27a0 5f 34 5f 32 5f 30 20 20 20 41 20 6d 61 73 6b 20  _4_2_0   A mask 
d27b0 66 6f 72 20 20 28 30 78 37 66 3c 3c 32 38 29 20  for  (0x7f<<28) 
d27c0 7c 20 53 4c 4f 54 5f 32 5f 30 0a 2a 2f 0a 23 64  | SLOT_2_0.*/.#d
d27d0 65 66 69 6e 65 20 53 4c 4f 54 5f 32 5f 30 20 20  efine SLOT_2_0  
d27e0 20 20 20 30 78 30 30 31 66 63 30 37 66 0a 23 64     0x001fc07f.#d
d27f0 65 66 69 6e 65 20 53 4c 4f 54 5f 34 5f 32 5f 30  efine SLOT_4_2_0
d2800 20 20 20 30 78 66 30 31 66 63 30 37 66 0a 0a 0a     0xf01fc07f...
d2810 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36 34 2d  /*.** Read a 64-
d2820 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  bit variable-len
d2830 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d  gth integer from
d2840 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67   memory starting
d2850 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74   at p[0]..** Ret
d2860 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
d2870 66 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54  f bytes read.  T
d2880 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72  he value is stor
d2890 65 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c  ed in *v..*/.SQL
d28a0 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73  ITE_PRIVATE u8 s
d28b0 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28  qlite3GetVarint(
d28c0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
d28d0 68 61 72 20 2a 70 2c 20 75 36 34 20 2a 76 29 7b  har *p, u64 *v){
d28e0 0a 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a 0a 20  .  u32 a,b,s;.. 
d28f0 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a   a = *p;.  /* a:
d2900 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a   p0 (unmasked) *
d2910 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30  /.  if (!(a&0x80
d2920 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 3d 20  )).  {.    *v = 
d2930 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  a;.    return 1;
d2940 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62  .  }..  p++;.  b
d2950 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70   = *p;.  /* b: p
d2960 31 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  1 (unmasked) */.
d2970 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29    if (!(b&0x80))
d2980 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 30 78  .  {.    a &= 0x
d2990 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37  7f;.    a = a<<7
d29a0 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20  ;.    a |= b;.  
d29b0 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65    *v = a;.    re
d29c0 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 2;.  }..  /
d29d0 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 63 6f  * Verify that co
d29e0 6e 73 74 61 6e 74 73 20 61 72 65 20 70 72 65 63  nstants are prec
d29f0 6f 6d 70 75 74 65 64 20 63 6f 72 72 65 63 74 6c  omputed correctl
d2a00 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 53  y */.  assert( S
d2a10 4c 4f 54 5f 32 5f 30 20 3d 3d 20 28 28 30 78 37  LOT_2_0 == ((0x7
d2a20 66 3c 3c 31 34 29 20 7c 20 28 30 78 37 66 29 29  f<<14) | (0x7f))
d2a30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 4c   );.  assert( SL
d2a40 4f 54 5f 34 5f 32 5f 30 20 3d 3d 20 28 28 30 78  OT_4_2_0 == ((0x
d2a50 66 55 3c 3c 32 38 29 20 7c 20 28 30 78 37 66 3c  fU<<28) | (0x7f<
d2a60 3c 31 34 29 20 7c 20 28 30 78 37 66 29 29 20 29  <14) | (0x7f)) )
d2a70 3b 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20  ;..  p++;.  a = 
d2a80 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70  a<<14;.  a |= *p
d2a90 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34  ;.  /* a: p0<<14
d2aa0 20 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29   | p2 (unmasked)
d2ab0 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78   */.  if (!(a&0x
d2ac0 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26  80)).  {.    a &
d2ad0 3d 20 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 20 20  = SLOT_2_0;.    
d2ae0 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62  b &= 0x7f;.    b
d2af0 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c   = b<<7;.    a |
d2b00 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b  = b;.    *v = a;
d2b10 0a 20 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20  .    return 3;. 
d2b20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 31 20 66 72   }..  /* CSE1 fr
d2b30 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20  om below */.  a 
d2b40 26 3d 20 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 70  &= SLOT_2_0;.  p
d2b50 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b  ++;.  b = b<<14;
d2b60 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a  .  b |= *p;.  /*
d2b70 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20   b: p1<<14 | p3 
d2b80 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  (unmasked) */.  
d2b90 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20  if (!(b&0x80)). 
d2ba0 20 7b 0a 20 20 20 20 62 20 26 3d 20 53 4c 4f 54   {.    b &= SLOT
d2bb0 5f 32 5f 30 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76  _2_0;.    /* mov
d2bc0 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20 20  ed CSE1 up */.  
d2bd0 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c    /* a &= (0x7f<
d2be0 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a  <14)|(0x7f); */.
d2bf0 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20      a = a<<7;.  
d2c00 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76    a |= b;.    *v
d2c10 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e   = a;.    return
d2c20 20 34 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 3a   4;.  }..  /* a:
d2c30 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61   p0<<14 | p2 (ma
d2c40 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 62 3a  sked) */.  /* b:
d2c50 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e   p1<<14 | p3 (un
d2c60 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20  masked) */.  /* 
d2c70 31 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32  1:save off p0<<2
d2c80 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c  1 | p1<<14 | p2<
d2c90 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29  <7 | p3 (masked)
d2ca0 20 2a 2f 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 43   */.  /* moved C
d2cb0 53 45 31 20 75 70 20 2a 2f 0a 20 20 2f 2a 20 61  SE1 up */.  /* a
d2cc0 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
d2cd0 30 78 37 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d  0x7f); */.  b &=
d2ce0 20 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 73 20 3d   SLOT_2_0;.  s =
d2cf0 20 61 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c   a;.  /* s: p0<<
d2d00 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29  14 | p2 (masked)
d2d10 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20   */..  p++;.  a 
d2d20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20  = a<<14;.  a |= 
d2d30 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c  *p;.  /* a: p0<<
d2d40 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34  28 | p2<<14 | p4
d2d50 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
d2d60 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a   if (!(a&0x80)).
d2d70 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61    {.    /* we ca
d2d80 6e 20 73 6b 69 70 20 74 68 65 73 65 20 63 61 75  n skip these cau
d2d90 73 65 20 74 68 65 79 20 77 65 72 65 20 28 65 66  se they were (ef
d2da0 66 65 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20  fectively) done 
d2db0 61 62 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e  above in calc'in
d2dc0 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20  g s */.    /* a 
d2dd0 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30  &= (0x7f<<28)|(0
d2de0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
d2df0 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20   */.    /* b &= 
d2e00 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
d2e10 29 3b 20 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c  ); */.    b = b<
d2e20 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a  <7;.    a |= b;.
d2e30 20 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20      s = s>>18;. 
d2e40 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29     *v = ((u64)s)
d2e50 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65  <<32 | a;.    re
d2e60 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 5;.  }..  /
d2e70 2a 20 32 3a 73 61 76 65 20 6f 66 66 20 70 30 3c  * 2:save off p0<
d2e80 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70  <21 | p1<<14 | p
d2e90 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65  2<<7 | p3 (maske
d2ea0 64 29 20 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37  d) */.  s = s<<7
d2eb0 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a  ;.  s |= b;.  /*
d2ec0 20 73 3a 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c   s: p0<<21 | p1<
d2ed0 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33  <14 | p2<<7 | p3
d2ee0 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20   (masked) */..  
d2ef0 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34  p++;.  b = b<<14
d2f00 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f  ;.  b |= *p;.  /
d2f10 2a 20 62 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33  * b: p1<<28 | p3
d2f20 3c 3c 31 34 20 7c 20 70 35 20 28 75 6e 6d 61 73  <<14 | p5 (unmas
d2f30 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28  ked) */.  if (!(
d2f40 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  b&0x80)).  {.   
d2f50 20 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20   /* we can skip 
d2f60 74 68 69 73 20 63 61 75 73 65 20 69 74 20 77 61  this cause it wa
d2f70 73 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20  s (effectively) 
d2f80 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61  done above in ca
d2f90 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20  lc'ing s */.    
d2fa0 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 32  /* b &= (0x7f<<2
d2fb0 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  8)|(0x7f<<14)|(0
d2fc0 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26  x7f); */.    a &
d2fd0 3d 20 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 20 20  = SLOT_2_0;.    
d2fe0 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20  a = a<<7;.    a 
d2ff0 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e  |= b;.    s = s>
d3000 3e 31 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28  >18;.    *v = ((
d3010 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a  u64)s)<<32 | a;.
d3020 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 20      return 6;.  
d3030 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20  }..  p++;.  a = 
d3040 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70  a<<14;.  a |= *p
d3050 3b 0a 20 20 2f 2a 20 61 3a 20 70 32 3c 3c 32 38  ;.  /* a: p2<<28
d3060 20 7c 20 70 34 3c 3c 31 34 20 7c 20 70 36 20 28   | p4<<14 | p6 (
d3070 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69  unmasked) */.  i
d3080 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20  f (!(a&0x80)).  
d3090 7b 0a 20 20 20 20 61 20 26 3d 20 53 4c 4f 54 5f  {.    a &= SLOT_
d30a0 34 5f 32 5f 30 3b 0a 20 20 20 20 62 20 26 3d 20  4_2_0;.    b &= 
d30b0 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 20 20 62 20  SLOT_2_0;.    b 
d30c0 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d  = b<<7;.    a |=
d30d0 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31   b;.    s = s>>1
d30e0 31 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36  1;.    *v = ((u6
d30f0 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20  4)s)<<32 | a;.  
d3100 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a    return 7;.  }.
d3110 0a 20 20 2f 2a 20 43 53 45 32 20 66 72 6f 6d 20  .  /* CSE2 from 
d3120 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 3d 20  below */.  a &= 
d3130 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 70 2b 2b 3b  SLOT_2_0;.  p++;
d3140 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20  .  b = b<<14;.  
d3150 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a  b |= *p;.  /* b:
d3160 20 70 33 3c 3c 32 38 20 7c 20 70 35 3c 3c 31 34   p3<<28 | p5<<14
d3170 20 7c 20 70 37 20 28 75 6e 6d 61 73 6b 65 64 29   | p7 (unmasked)
d3180 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78   */.  if (!(b&0x
d3190 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 20 26  80)).  {.    b &
d31a0 3d 20 53 4c 4f 54 5f 34 5f 32 5f 30 3b 0a 20 20  = SLOT_4_2_0;.  
d31b0 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 32 20    /* moved CSE2 
d31c0 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26  up */.    /* a &
d31d0 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
d31e0 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20  7f); */.    a = 
d31f0 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62  a<<7;.    a |= b
d3200 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 34 3b 0a  ;.    s = s>>4;.
d3210 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73      *v = ((u64)s
d3220 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72  )<<32 | a;.    r
d3230 65 74 75 72 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20  eturn 8;.  }..  
d3240 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 35  p++;.  a = a<<15
d3250 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f  ;.  a |= *p;.  /
d3260 2a 20 61 3a 20 70 34 3c 3c 32 39 20 7c 20 70 36  * a: p4<<29 | p6
d3270 3c 3c 31 35 20 7c 20 70 38 20 28 75 6e 6d 61 73  <<15 | p8 (unmas
d3280 6b 65 64 29 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f  ked) */..  /* mo
d3290 76 65 64 20 43 53 45 32 20 75 70 20 2a 2f 0a 20  ved CSE2 up */. 
d32a0 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c   /* a &= (0x7f<<
d32b0 32 39 29 7c 28 30 78 37 66 3c 3c 31 35 29 7c 28  29)|(0x7f<<15)|(
d32c0 30 78 66 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d  0xff); */.  b &=
d32d0 20 53 4c 4f 54 5f 32 5f 30 3b 0a 20 20 62 20 3d   SLOT_2_0;.  b =
d32e0 20 62 3c 3c 38 3b 0a 20 20 61 20 7c 3d 20 62 3b   b<<8;.  a |= b;
d32f0 0a 0a 20 20 73 20 3d 20 73 3c 3c 34 3b 0a 20 20  ..  s = s<<4;.  
d3300 62 20 3d 20 70 5b 2d 34 5d 3b 0a 20 20 62 20 26  b = p[-4];.  b &
d3310 3d 20 30 78 37 66 3b 0a 20 20 62 20 3d 20 62 3e  = 0x7f;.  b = b>
d3320 3e 33 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 0a 20  >3;.  s |= b;.. 
d3330 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c   *v = ((u64)s)<<
d3340 33 32 20 7c 20 61 3b 0a 0a 20 20 72 65 74 75 72  32 | a;..  retur
d3350 6e 20 39 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n 9;.}../*.** Re
d3360 61 64 20 61 20 33 32 2d 62 69 74 20 76 61 72 69  ad a 32-bit vari
d3370 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65  able-length inte
d3380 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20  ger from memory 
d3390 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d  starting at p[0]
d33a0 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
d33b0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
d33c0 72 65 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65  read.  The value
d33d0 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76   is stored in *v
d33e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76  ..**.** If the v
d33f0 61 72 69 6e 74 20 73 74 6f 72 65 64 20 69 6e 20  arint stored in 
d3400 70 5b 30 5d 20 69 73 20 6c 61 72 67 65 72 20 74  p[0] is larger t
d3410 68 61 6e 20 63 61 6e 20 66 69 74 20 69 6e 20 61  han can fit in a
d3420 20 33 32 2d 62 69 74 20 75 6e 73 69 67 6e 65 64   32-bit unsigned
d3430 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 74 68 65  .** integer, the
d3440 6e 20 73 65 74 20 2a 76 20 74 6f 20 30 78 66 66  n set *v to 0xff
d3450 66 66 66 66 66 66 2e 0a 2a 2a 0a 2a 2a 20 41 20  ffffff..**.** A 
d3460 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 67  MACRO version, g
d3470 65 74 56 61 72 69 6e 74 33 32 2c 20 69 73 20 70  etVarint32, is p
d3480 72 6f 76 69 64 65 64 20 77 68 69 63 68 20 69 6e  rovided which in
d3490 6c 69 6e 65 73 20 74 68 65 20 0a 2a 2a 20 73 69  lines the .** si
d34a0 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20  ngle-byte case. 
d34b0 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64   All code should
d34c0 20 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20 76   use the MACRO v
d34d0 65 72 73 69 6f 6e 20 61 73 20 0a 2a 2a 20 74 68  ersion as .** th
d34e0 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75  is function assu
d34f0 6d 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62  mes the single-b
d3500 79 74 65 20 63 61 73 65 20 68 61 73 20 61 6c 72  yte case has alr
d3510 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64 6c 65  eady been handle
d3520 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
d3530 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 47  VATE u8 sqlite3G
d3540 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74  etVarint32(const
d3550 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
d3560 70 2c 20 75 33 32 20 2a 76 29 7b 0a 20 20 75 33  p, u32 *v){.  u3
d3570 32 20 61 2c 62 3b 0a 0a 20 20 2f 2a 20 54 68 65  2 a,b;..  /* The
d3580 20 31 2d 62 79 74 65 20 63 61 73 65 2e 20 20 4f   1-byte case.  O
d3590 76 65 72 77 68 65 6c 6d 69 6e 67 6c 79 20 74 68  verwhelmingly th
d35a0 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 2e 20 20  e most common.  
d35b0 48 61 6e 64 6c 65 64 20 69 6e 6c 69 6e 65 0a 20  Handled inline. 
d35c0 20 2a 2a 20 62 79 20 74 68 65 20 67 65 74 56 61   ** by the getVa
d35d0 72 69 6e 33 32 28 29 20 6d 61 63 72 6f 20 2a 2f  rin32() macro */
d35e0 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20  .  a = *p;.  /* 
d35f0 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29  a: p0 (unmasked)
d3600 20 2a 2f 0a 23 69 66 6e 64 65 66 20 67 65 74 56   */.#ifndef getV
d3610 61 72 69 6e 74 33 32 0a 20 20 69 66 20 28 21 28  arint32.  if (!(
d3620 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  a&0x80)).  {.   
d3630 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65   /* Values betwe
d3640 65 6e 20 30 20 61 6e 64 20 31 32 37 20 2a 2f 0a  en 0 and 127 */.
d3650 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20      *v = a;.    
d3660 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65  return 1;.  }.#e
d3670 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65 20 32  ndif..  /* The 2
d3680 2d 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20  -byte case */.  
d3690 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20  p++;.  b = *p;. 
d36a0 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73   /* b: p1 (unmas
d36b0 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28  ked) */.  if (!(
d36c0 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  b&0x80)).  {.   
d36d0 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65   /* Values betwe
d36e0 65 6e 20 31 32 38 20 61 6e 64 20 31 36 33 38 33  en 128 and 16383
d36f0 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 30 78 37   */.    a &= 0x7
d3700 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b  f;.    a = a<<7;
d3710 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b  .    *v = a | b;
d3720 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20  .    return 2;. 
d3730 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 33 2d 62   }..  /* The 3-b
d3740 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20 70 2b  yte case */.  p+
d3750 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a  +;.  a = a<<14;.
d3760 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20    a |= *p;.  /* 
d3770 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28  a: p0<<14 | p2 (
d3780 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69  unmasked) */.  i
d3790 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20  f (!(a&0x80)).  
d37a0 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20  {.    /* Values 
d37b0 62 65 74 77 65 65 6e 20 31 36 33 38 34 20 61 6e  between 16384 an
d37c0 64 20 32 30 39 37 31 35 31 20 2a 2f 0a 20 20 20  d 2097151 */.   
d37d0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   a &= (0x7f<<14)
d37e0 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26  |(0x7f);.    b &
d37f0 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d 20  = 0x7f;.    b = 
d3800 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61  b<<7;.    *v = a
d3810 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e   | b;.    return
d3820 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 20   3;.  }..  /* A 
d3830 33 32 2d 62 69 74 20 76 61 72 69 6e 74 20 69 73  32-bit varint is
d3840 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 73   used to store s
d3850 69 7a 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ize information 
d3860 69 6e 20 62 74 72 65 65 73 2e 0a 20 20 2a 2a 20  in btrees..  ** 
d3870 4f 62 6a 65 63 74 73 20 61 72 65 20 72 61 72 65  Objects are rare
d3880 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 32  ly larger than 2
d3890 4d 69 42 20 6c 69 6d 69 74 20 6f 66 20 61 20 33  MiB limit of a 3
d38a0 2d 62 79 74 65 20 76 61 72 69 6e 74 2e 0a 20 20  -byte varint..  
d38b0 2a 2a 20 41 20 33 2d 62 79 74 65 20 76 61 72 69  ** A 3-byte vari
d38c0 6e 74 20 69 73 20 73 75 66 66 69 63 69 65 6e 74  nt is sufficient
d38d0 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74  , for example, t
d38e0 6f 20 72 65 63 6f 72 64 20 74 68 65 20 73 69 7a  o record the siz
d38f0 65 0a 20 20 2a 2a 20 6f 66 20 61 20 31 30 34 38  e.  ** of a 1048
d3900 35 36 39 2d 62 79 74 65 20 42 4c 4f 42 20 6f 72  569-byte BLOB or
d3910 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2a 0a 20 20   string..  **.  
d3920 2a 2a 20 57 65 20 6f 6e 6c 79 20 75 6e 72 6f 6c  ** We only unrol
d3930 6c 20 74 68 65 20 66 69 72 73 74 20 31 2d 2c 20  l the first 1-, 
d3940 32 2d 2c 20 61 6e 64 20 33 2d 20 62 79 74 65 20  2-, and 3- byte 
d3950 63 61 73 65 73 2e 20 20 54 68 65 20 76 65 72 79  cases.  The very
d3960 0a 20 20 2a 2a 20 72 61 72 65 20 6c 61 72 67 65  .  ** rare large
d3970 72 20 63 61 73 65 73 20 63 61 6e 20 62 65 20 68  r cases can be h
d3980 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 73 6c  andled by the sl
d3990 6f 77 65 72 20 36 34 2d 62 69 74 20 76 61 72 69  ower 64-bit vari
d39a0 6e 74 0a 20 20 2a 2a 20 72 6f 75 74 69 6e 65 2e  nt.  ** routine.
d39b0 0a 20 20 2a 2f 0a 23 69 66 20 31 0a 20 20 7b 0a  .  */.#if 1.  {.
d39c0 20 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20      u64 v64;.   
d39d0 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d   u8 n;..    p -=
d39e0 20 32 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69   2;.    n = sqli
d39f0 74 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20  te3GetVarint(p, 
d3a00 26 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72  &v64);.    asser
d3a10 74 28 20 6e 3e 33 20 26 26 20 6e 3c 3d 39 20 29  t( n>3 && n<=9 )
d3a20 3b 0a 20 20 20 20 69 66 28 20 28 76 36 34 20 26  ;.    if( (v64 &
d3a30 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29   SQLITE_MAX_U32)
d3a40 21 3d 76 36 34 20 29 7b 0a 20 20 20 20 20 20 2a  !=v64 ){.      *
d3a50 76 20 3d 20 30 78 66 66 66 66 66 66 66 66 3b 0a  v = 0xffffffff;.
d3a60 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d3a70 20 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b 0a   *v = (u32)v64;.
d3a80 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
d3a90 20 6e 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 20   n;.  }..#else. 
d3aa0 20 2f 2a 20 46 6f 72 20 66 6f 6c 6c 6f 77 69 6e   /* For followin
d3ab0 67 20 63 6f 64 65 20 28 6b 65 70 74 20 66 6f 72  g code (kept for
d3ac0 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 63 6f   historical reco
d3ad0 72 64 20 6f 6e 6c 79 29 20 73 68 6f 77 73 20 61  rd only) shows a
d3ae0 6e 0a 20 20 2a 2a 20 75 6e 72 6f 6c 6c 69 6e 67  n.  ** unrolling
d3af0 20 66 6f 72 20 74 68 65 20 33 2d 20 61 6e 64 20   for the 3- and 
d3b00 34 2d 62 79 74 65 20 76 61 72 69 6e 74 20 63 61  4-byte varint ca
d3b10 73 65 73 2e 20 20 54 68 69 73 20 63 6f 64 65 20  ses.  This code 
d3b20 69 73 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79  is.  ** slightly
d3b30 20 66 61 73 74 65 72 2c 20 62 75 74 20 69 74 20   faster, but it 
d3b40 69 73 20 61 6c 73 6f 20 6c 61 72 67 65 72 20 61  is also larger a
d3b50 6e 64 20 6d 75 63 68 20 68 61 72 64 65 72 20 74  nd much harder t
d3b60 6f 20 74 65 73 74 2e 0a 20 20 2a 2f 0a 20 20 70  o test..  */.  p
d3b70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b  ++;.  b = b<<14;
d3b80 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a  .  b |= *p;.  /*
d3b90 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20   b: p1<<14 | p3 
d3ba0 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  (unmasked) */.  
d3bb0 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20  if (!(b&0x80)). 
d3bc0 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73   {.    /* Values
d3bd0 20 62 65 74 77 65 65 6e 20 32 30 39 37 31 35 32   between 2097152
d3be0 20 61 6e 64 20 32 36 38 34 33 35 34 35 35 20 2a   and 268435455 *
d3bf0 2f 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66  /.    b &= (0x7f
d3c00 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20  <<14)|(0x7f);.  
d3c10 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34    a &= (0x7f<<14
d3c20 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20  )|(0x7f);.    a 
d3c30 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d  = a<<7;.    *v =
d3c40 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75   a | b;.    retu
d3c50 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b  rn 4;.  }..  p++
d3c60 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20  ;.  a = a<<14;. 
d3c70 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61   a |= *p;.  /* a
d3c80 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31  : p0<<28 | p2<<1
d3c90 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64  4 | p4 (unmasked
d3ca0 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30  ) */.  if (!(a&0
d3cb0 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a  x80)).  {.    /*
d3cc0 20 56 61 6c 75 65 73 20 20 62 65 74 77 65 65 6e   Values  between
d3cd0 20 32 36 38 34 33 35 34 35 36 20 61 6e 64 20 33   268435456 and 3
d3ce0 34 33 35 39 37 33 38 33 36 37 20 2a 2f 0a 20 20  4359738367 */.  
d3cf0 20 20 61 20 26 3d 20 53 4c 4f 54 5f 34 5f 32 5f    a &= SLOT_4_2_
d3d00 30 3b 0a 20 20 20 20 62 20 26 3d 20 53 4c 4f 54  0;.    b &= SLOT
d3d10 5f 34 5f 32 5f 30 3b 0a 20 20 20 20 62 20 3d 20  _4_2_0;.    b = 
d3d20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61  b<<7;.    *v = a
d3d30 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e   | b;.    return
d3d40 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65   5;.  }..  /* We
d3d50 20 63 61 6e 20 6f 6e 6c 79 20 72 65 61 63 68 20   can only reach 
d3d60 74 68 69 73 20 70 6f 69 6e 74 20 77 68 65 6e 20  this point when 
d3d70 72 65 61 64 69 6e 67 20 61 20 63 6f 72 72 75 70  reading a corrup
d3d80 74 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  t database.  ** 
d3d90 66 69 6c 65 2e 20 20 49 6e 20 74 68 61 74 20 63  file.  In that c
d3da0 61 73 65 20 77 65 20 61 72 65 20 6e 6f 74 20 69  ase we are not i
d3db0 6e 20 61 6e 79 20 68 75 72 72 79 2e 20 20 55 73  n any hurry.  Us
d3dc0 65 20 74 68 65 20 28 72 65 6c 61 74 69 76 65 6c  e the (relativel
d3dd0 79 0a 20 20 2a 2a 20 73 6c 6f 77 29 20 67 65 6e  y.  ** slow) gen
d3de0 65 72 61 6c 2d 70 75 72 70 6f 73 65 20 73 71 6c  eral-purpose sql
d3df0 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 29 20  ite3GetVarint() 
d3e00 72 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 72 61  routine to extra
d3e10 63 74 20 74 68 65 0a 20 20 2a 2a 20 76 61 6c 75  ct the.  ** valu
d3e20 65 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 36  e. */.  {.    u6
d3e30 34 20 76 36 34 3b 0a 20 20 20 20 75 38 20 6e 3b  4 v64;.    u8 n;
d3e40 0a 0a 20 20 20 20 70 20 2d 3d 20 34 3b 0a 20 20  ..    p -= 4;.  
d3e50 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74    n = sqlite3Get
d3e60 56 61 72 69 6e 74 28 70 2c 20 26 76 36 34 29 3b  Varint(p, &v64);
d3e70 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 35  .    assert( n>5
d3e80 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20 20 20   && n<=9 );.    
d3e90 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b 0a 20  *v = (u32)v64;. 
d3ea0 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a 20 20 7d     return n;.  }
d3eb0 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
d3ec0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
d3ed0 65 72 20 6f 66 20 62 79 74 65 73 20 74 68 61 74  er of bytes that
d3ee0 20 77 69 6c 6c 20 62 65 20 6e 65 65 64 65 64 20   will be needed 
d3ef0 74 6f 20 73 74 6f 72 65 20 74 68 65 20 67 69 76  to store the giv
d3f00 65 6e 0a 2a 2a 20 36 34 2d 62 69 74 20 69 6e 74  en.** 64-bit int
d3f10 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eger..*/.SQLITE_
d3f20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
d3f30 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34  te3VarintLen(u64
d3f40 20 76 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 30   v){.  int i = 0
d3f50 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 69 2b 2b 3b  ;.  do{.    i++;
d3f60 0a 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20  .    v >>= 7;.  
d3f70 7d 77 68 69 6c 65 28 20 76 21 3d 30 20 26 26 20  }while( v!=0 && 
d3f80 41 4c 57 41 59 53 28 69 3c 39 29 20 29 3b 0a 20  ALWAYS(i<9) );. 
d3f90 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 0a 2f   return i;.}.../
d3fa0 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72 69  *.** Read or wri
d3fb0 74 65 20 61 20 66 6f 75 72 2d 62 79 74 65 20 62  te a four-byte b
d3fc0 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
d3fd0 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49  r value..*/.SQLI
d3fe0 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73  TE_PRIVATE u32 s
d3ff0 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 63  qlite3Get4byte(c
d4000 6f 6e 73 74 20 75 38 20 2a 70 29 7b 0a 20 20 72  onst u8 *p){.  r
d4010 65 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 32 34 29  eturn (p[0]<<24)
d4020 20 7c 20 28 70 5b 31 5d 3c 3c 31 36 29 20 7c 20   | (p[1]<<16) | 
d4030 28 70 5b 32 5d 3c 3c 38 29 20 7c 20 70 5b 33 5d  (p[2]<<8) | p[3]
d4040 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
d4050 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
d4060 75 74 34 62 79 74 65 28 75 6e 73 69 67 6e 65 64  ut4byte(unsigned
d4070 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29   char *p, u32 v)
d4080 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28  {.  p[0] = (u8)(
d4090 76 3e 3e 32 34 29 3b 0a 20 20 70 5b 31 5d 20 3d  v>>24);.  p[1] =
d40a0 20 28 75 38 29 28 76 3e 3e 31 36 29 3b 0a 20 20   (u8)(v>>16);.  
d40b0 70 5b 32 5d 20 3d 20 28 75 38 29 28 76 3e 3e 38  p[2] = (u8)(v>>8
d40c0 29 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 75 38 29  );.  p[3] = (u8)
d40d0 76 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72  v;.}..../*.** Tr
d40e0 61 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65  anslate a single
d40f0 20 62 79 74 65 20 6f 66 20 48 65 78 20 69 6e 74   byte of Hex int
d4100 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a  o an integer..**
d4110 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e   This routine on
d4120 6c 79 20 77 6f 72 6b 73 20 69 66 20 68 20 72 65  ly works if h re
d4130 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20  ally is a valid 
d4140 68 65 78 61 64 65 63 69 6d 61 6c 0a 2a 2a 20 63  hexadecimal.** c
d4150 68 61 72 61 63 74 65 72 3a 20 20 30 2e 2e 39 61  haracter:  0..9a
d4160 2e 2e 66 41 2e 2e 46 0a 2a 2f 0a 53 51 4c 49 54  ..fA..F.*/.SQLIT
d4170 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c  E_PRIVATE u8 sql
d4180 69 74 65 33 48 65 78 54 6f 49 6e 74 28 69 6e 74  ite3HexToInt(int
d4190 20 68 29 7b 0a 20 20 61 73 73 65 72 74 28 20 28   h){.  assert( (
d41a0 68 3e 3d 27 30 27 20 26 26 20 68 3c 3d 27 39 27  h>='0' && h<='9'
d41b0 29 20 7c 7c 20 20 28 68 3e 3d 27 61 27 20 26 26  ) ||  (h>='a' &&
d41c0 20 68 3c 3d 27 66 27 29 20 7c 7c 20 20 28 68 3e   h<='f') ||  (h>
d41d0 3d 27 41 27 20 26 26 20 68 3c 3d 27 46 27 29 20  ='A' && h<='F') 
d41e0 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  );.#ifdef SQLITE
d41f0 5f 41 53 43 49 49 0a 20 20 68 20 2b 3d 20 39 2a  _ASCII.  h += 9*
d4200 28 31 26 28 68 3e 3e 36 29 29 3b 0a 23 65 6e 64  (1&(h>>6));.#end
d4210 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
d4220 5f 45 42 43 44 49 43 0a 20 20 68 20 2b 3d 20 39  _EBCDIC.  h += 9
d4230 2a 28 31 26 7e 28 68 3e 3e 34 29 29 3b 0a 23 65  *(1&~(h>>4));.#e
d4240 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 28 75  ndif.  return (u
d4250 38 29 28 68 20 26 20 30 78 66 29 3b 0a 7d 0a 0a  8)(h & 0xf);.}..
d4260 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
d4270 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49  ITE_OMIT_BLOB_LI
d4280 54 45 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65  TERAL) || define
d4290 64 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  d(SQLITE_HAS_COD
d42a0 45 43 29 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  EC)./*.** Conver
d42b0 74 20 61 20 42 4c 4f 42 20 6c 69 74 65 72 61 6c  t a BLOB literal
d42c0 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78 27   of the form "x'
d42d0 68 68 68 68 68 68 27 22 20 69 6e 74 6f 20 69 74  hhhhhh'" into it
d42e0 73 20 62 69 6e 61 72 79 0a 2a 2a 20 76 61 6c 75  s binary.** valu
d42f0 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  e.  Return a poi
d4300 6e 74 65 72 20 74 6f 20 69 74 73 20 62 69 6e 61  nter to its bina
d4310 72 79 20 76 61 6c 75 65 2e 20 20 53 70 61 63 65  ry value.  Space
d4320 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20   to hold the.** 
d4330 62 69 6e 61 72 79 20 76 61 6c 75 65 20 68 61 73  binary value has
d4340 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66   been obtained f
d4350 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d  rom malloc and m
d4360 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 0a  ust be freed by.
d4370 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72  ** the calling r
d4380 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54  outine..*/.SQLIT
d4390 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
d43a0 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62  sqlite3HexToBlob
d43b0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
d43c0 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
d43d0 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 42 6c   n){.  char *zBl
d43e0 6f 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20  ob;.  int i;..  
d43f0 7a 42 6c 6f 62 20 3d 20 28 63 68 61 72 20 2a 29  zBlob = (char *)
d4400 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
d4410 61 77 28 64 62 2c 20 6e 2f 32 20 2b 20 31 29 3b  aw(db, n/2 + 1);
d4420 0a 20 20 6e 2d 2d 3b 0a 20 20 69 66 28 20 7a 42  .  n--;.  if( zB
d4430 6c 6f 62 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  lob ){.    for(i
d4440 3d 30 3b 20 69 3c 6e 3b 20 69 2b 3d 32 29 7b 0a  =0; i<n; i+=2){.
d4450 20 20 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d        zBlob[i/2]
d4460 20 3d 20 28 73 71 6c 69 74 65 33 48 65 78 54 6f   = (sqlite3HexTo
d4470 49 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20  Int(z[i])<<4) | 
d4480 73 71 6c 69 74 65 33 48 65 78 54 6f 49 6e 74 28  sqlite3HexToInt(
d4490 7a 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20  z[i+1]);.    }. 
d44a0 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20     zBlob[i/2] = 
d44b0 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
d44c0 7a 42 6c 6f 62 3b 0a 7d 0a 23 65 6e 64 69 66 20  zBlob;.}.#endif 
d44d0 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  /* !SQLITE_OMIT_
d44e0 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20  BLOB_LITERAL || 
d44f0 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
d4500 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 67 20 61   */../*.** Log a
d4510 6e 20 65 72 72 6f 72 20 74 68 61 74 20 69 73 20  n error that is 
d4520 61 6e 20 41 50 49 20 63 61 6c 6c 20 6f 6e 20 61  an API call on a
d4530 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e   connection poin
d4540 74 65 72 20 74 68 61 74 20 73 68 6f 75 6c 64 0a  ter that should.
d4550 2a 2a 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e  ** not have been
d4560 20 75 73 65 64 2e 20 20 54 68 65 20 22 74 79 70   used.  The "typ
d4570 65 22 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e  e" of connection
d4580 20 70 6f 69 6e 74 65 72 20 69 73 20 67 69 76 65   pointer is give
d4590 6e 20 61 73 20 74 68 65 0a 2a 2a 20 61 72 67 75  n as the.** argu
d45a0 6d 65 6e 74 2e 20 20 54 68 65 20 7a 54 79 70 65  ment.  The zType
d45b0 20 69 73 20 61 20 77 6f 72 64 20 6c 69 6b 65 20   is a word like 
d45c0 22 4e 55 4c 4c 22 20 6f 72 20 22 63 6c 6f 73 65  "NULL" or "close
d45d0 64 22 20 6f 72 20 22 69 6e 76 61 6c 69 64 22 2e  d" or "invalid".
d45e0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
d45f0 6c 6f 67 42 61 64 43 6f 6e 6e 65 63 74 69 6f 6e  logBadConnection
d4600 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79  (const char *zTy
d4610 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6c  pe){.  sqlite3_l
d4620 6f 67 28 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  og(SQLITE_MISUSE
d4630 2c 20 0a 20 20 20 20 20 22 41 50 49 20 63 61 6c  , .     "API cal
d4640 6c 20 77 69 74 68 20 25 73 20 64 61 74 61 62 61  l with %s databa
d4650 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f  se connection po
d4660 69 6e 74 65 72 22 2c 0a 20 20 20 20 20 7a 54 79  inter",.     zTy
d4670 70 65 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pe.  );.}../*.**
d4680 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73   Check to make s
d4690 75 72 65 20 77 65 20 68 61 76 65 20 61 20 76 61  ure we have a va
d46a0 6c 69 64 20 64 62 20 70 6f 69 6e 74 65 72 2e 20  lid db pointer. 
d46b0 20 54 68 69 73 20 74 65 73 74 20 69 73 20 6e 6f   This test is no
d46c0 74 0a 2a 2a 20 66 6f 6f 6c 70 72 6f 6f 66 20 62  t.** foolproof b
d46d0 75 74 20 69 74 20 64 6f 65 73 20 70 72 6f 76 69  ut it does provi
d46e0 64 65 20 73 6f 6d 65 20 6d 65 61 73 75 72 65 20  de some measure 
d46f0 6f 66 20 70 72 6f 74 65 63 74 69 6f 6e 20 61 67  of protection ag
d4700 61 69 6e 73 74 0a 2a 2a 20 6d 69 73 75 73 65 20  ainst.** misuse 
d4710 6f 66 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  of the interface
d4720 20 73 75 63 68 20 61 73 20 70 61 73 73 69 6e 67   such as passing
d4730 20 69 6e 20 64 62 20 70 6f 69 6e 74 65 72 73 20   in db pointers 
d4740 74 68 61 74 20 61 72 65 0a 2a 2a 20 4e 55 4c 4c  that are.** NULL
d4750 20 6f 72 20 77 68 69 63 68 20 68 61 76 65 20 62   or which have b
d4760 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 63  een previously c
d4770 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69 73 20  losed.  If this 
d4780 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 0a  routine returns.
d4790 2a 2a 20 31 20 69 74 20 6d 65 61 6e 73 20 74 68  ** 1 it means th
d47a0 61 74 20 74 68 65 20 64 62 20 70 6f 69 6e 74 65  at the db pointe
d47b0 72 20 69 73 20 76 61 6c 69 64 20 61 6e 64 20 30  r is valid and 0
d47c0 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6e 6f   if it should no
d47d0 74 20 62 65 0a 2a 2a 20 64 65 72 65 66 65 72 65  t be.** derefere
d47e0 6e 63 65 64 20 66 6f 72 20 61 6e 79 20 72 65 61  nced for any rea
d47f0 73 6f 6e 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e  son.  The callin
d4800 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c  g function shoul
d4810 64 20 69 6e 76 6f 6b 65 0a 2a 2a 20 53 51 4c 49  d invoke.** SQLI
d4820 54 45 5f 4d 49 53 55 53 45 20 69 6d 6d 65 64 69  TE_MISUSE immedi
d4830 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c  ately..**.** sql
d4840 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f  ite3SafetyCheckO
d4850 6b 28 29 20 72 65 71 75 69 72 65 73 20 74 68 61  k() requires tha
d4860 74 20 74 68 65 20 64 62 20 70 6f 69 6e 74 65 72  t the db pointer
d4870 20 62 65 20 76 61 6c 69 64 20 66 6f 72 0a 2a 2a   be valid for.**
d4880 20 75 73 65 2e 20 20 73 71 6c 69 74 65 33 53 61   use.  sqlite3Sa
d4890 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f  fetyCheckSickOrO
d48a0 6b 28 29 20 61 6c 6c 6f 77 73 20 61 20 64 62 20  k() allows a db 
d48b0 70 6f 69 6e 74 65 72 20 74 68 61 74 20 66 61 69  pointer that fai
d48c0 6c 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 6e 20 70  led to.** open p
d48d0 72 6f 70 65 72 6c 79 20 61 6e 64 20 69 73 20 6e  roperly and is n
d48e0 6f 74 20 66 69 74 20 66 6f 72 20 67 65 6e 65 72  ot fit for gener
d48f0 61 6c 20 75 73 65 20 62 75 74 20 77 68 69 63 68  al use but which
d4900 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64 20   can be.** used 
d4910 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74  as an argument t
d4920 6f 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  o sqlite3_errmsg
d4930 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6c  () or sqlite3_cl
d4940 6f 73 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ose()..*/.SQLITE
d4950 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
d4960 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f  ite3SafetyCheckO
d4970 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  k(sqlite3 *db){.
d4980 20 20 75 33 32 20 6d 61 67 69 63 3b 0a 20 20 69    u32 magic;.  i
d4990 66 28 20 64 62 3d 3d 30 20 29 7b 0a 20 20 20 20  f( db==0 ){.    
d49a0 6c 6f 67 42 61 64 43 6f 6e 6e 65 63 74 69 6f 6e  logBadConnection
d49b0 28 22 4e 55 4c 4c 22 29 3b 0a 20 20 20 20 72 65  ("NULL");.    re
d49c0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6d 61  turn 0;.  }.  ma
d49d0 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b  gic = db->magic;
d49e0 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51  .  if( magic!=SQ
d49f0 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20  LITE_MAGIC_OPEN 
d4a00 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
d4a10 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63  e3SafetyCheckSic
d4a20 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20  kOrOk(db) ){.   
d4a30 20 20 20 74 65 73 74 63 61 73 65 28 20 73 71 6c     testcase( sql
d4a40 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
d4a50 2e 78 4c 6f 67 21 3d 30 20 29 3b 0a 20 20 20 20  .xLog!=0 );.    
d4a60 20 20 6c 6f 67 42 61 64 43 6f 6e 6e 65 63 74 69    logBadConnecti
d4a70 6f 6e 28 22 75 6e 6f 70 65 6e 65 64 22 29 3b 0a  on("unopened");.
d4a80 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
d4a90 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
d4aa0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d   return 1;.  }.}
d4ab0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
d4ac0 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74  int sqlite3Safet
d4ad0 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73  yCheckSickOrOk(s
d4ae0 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 75  qlite3 *db){.  u
d4af0 33 32 20 6d 61 67 69 63 3b 0a 20 20 6d 61 67 69  32 magic;.  magi
d4b00 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20  c = db->magic;. 
d4b10 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 4c 49   if( magic!=SQLI
d4b20 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 26 26  TE_MAGIC_SICK &&
d4b30 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51  .      magic!=SQ
d4b40 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20  LITE_MAGIC_OPEN 
d4b50 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d  &&.      magic!=
d4b60 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53  SQLITE_MAGIC_BUS
d4b70 59 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73  Y ){.    testcas
d4b80 65 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  e( sqlite3Global
d4b90 43 6f 6e 66 69 67 2e 78 4c 6f 67 21 3d 30 20 29  Config.xLog!=0 )
d4ba0 3b 0a 20 20 20 20 6c 6f 67 42 61 64 43 6f 6e 6e  ;.    logBadConn
d4bb0 65 63 74 69 6f 6e 28 22 69 6e 76 61 6c 69 64 22  ection("invalid"
d4bc0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
d4bd0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
d4be0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  turn 1;.  }.}../
d4bf0 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  *.** Attempt to 
d4c00 61 64 64 2c 20 73 75 62 73 74 72 61 63 74 2c 20  add, substract, 
d4c10 6f 72 20 6d 75 6c 74 69 70 6c 79 20 74 68 65 20  or multiply the 
d4c20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 76 61  64-bit signed va
d4c30 6c 75 65 20 69 42 20 61 67 61 69 6e 73 74 0a 2a  lue iB against.*
d4c40 2a 20 74 68 65 20 6f 74 68 65 72 20 36 34 2d 62  * the other 64-b
d4c50 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  it signed intege
d4c60 72 20 61 74 20 2a 70 41 20 61 6e 64 20 73 74 6f  r at *pA and sto
d4c70 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  re the result in
d4c80 20 2a 70 41 2e 0a 2a 2a 20 52 65 74 75 72 6e 20   *pA..** Return 
d4c90 30 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 4f  0 on success.  O
d4ca0 72 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 69  r if the operati
d4cb0 6f 6e 20 77 6f 75 6c 64 20 68 61 76 65 20 72 65  on would have re
d4cc0 73 75 6c 74 65 64 20 69 6e 20 61 6e 0a 2a 2a 20  sulted in an.** 
d4cd0 6f 76 65 72 66 6c 6f 77 2c 20 6c 65 61 76 65 20  overflow, leave 
d4ce0 2a 70 41 20 75 6e 63 68 61 6e 67 65 64 20 61 6e  *pA unchanged an
d4cf0 64 20 72 65 74 75 72 6e 20 31 2e 0a 2a 2f 0a 53  d return 1..*/.S
d4d00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
d4d10 74 20 73 71 6c 69 74 65 33 41 64 64 49 6e 74 36  t sqlite3AddInt6
d4d20 34 28 69 36 34 20 2a 70 41 2c 20 69 36 34 20 69  4(i64 *pA, i64 i
d4d30 42 29 7b 0a 20 20 69 36 34 20 69 41 20 3d 20 2a  B){.  i64 iA = *
d4d40 70 41 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20  pA;.  testcase( 
d4d50 69 41 3d 3d 30 20 29 3b 20 74 65 73 74 63 61 73  iA==0 ); testcas
d4d60 65 28 20 69 41 3d 3d 31 20 29 3b 0a 20 20 74 65  e( iA==1 );.  te
d4d70 73 74 63 61 73 65 28 20 69 42 3d 3d 2d 31 20 29  stcase( iB==-1 )
d4d80 3b 20 74 65 73 74 63 61 73 65 28 20 69 42 3d 3d  ; testcase( iB==
d4d90 30 20 29 3b 0a 20 20 69 66 28 20 69 42 3e 3d 30  0 );.  if( iB>=0
d4da0 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65   ){.    testcase
d4db0 28 20 69 41 3e 30 20 26 26 20 4c 41 52 47 45 53  ( iA>0 && LARGES
d4dc0 54 5f 49 4e 54 36 34 20 2d 20 69 41 20 3d 3d 20  T_INT64 - iA == 
d4dd0 69 42 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  iB );.    testca
d4de0 73 65 28 20 69 41 3e 30 20 26 26 20 4c 41 52 47  se( iA>0 && LARG
d4df0 45 53 54 5f 49 4e 54 36 34 20 2d 20 69 41 20 3d  EST_INT64 - iA =
d4e00 3d 20 69 42 20 2d 20 31 20 29 3b 0a 20 20 20 20  = iB - 1 );.    
d4e10 69 66 28 20 69 41 3e 30 20 26 26 20 4c 41 52 47  if( iA>0 && LARG
d4e20 45 53 54 5f 49 4e 54 36 34 20 2d 20 69 41 20 3c  EST_INT64 - iA <
d4e30 20 69 42 20 29 20 72 65 74 75 72 6e 20 31 3b 0a   iB ) return 1;.
d4e40 20 20 20 20 2a 70 41 20 2b 3d 20 69 42 3b 0a 20      *pA += iB;. 
d4e50 20 7d 65 6c 73 65 7b 0a 20 20 20 20 74 65 73 74   }else{.    test
d4e60 63 61 73 65 28 20 69 41 3c 30 20 26 26 20 2d 28  case( iA<0 && -(
d4e70 69 41 20 2b 20 4c 41 52 47 45 53 54 5f 49 4e 54  iA + LARGEST_INT
d4e80 36 34 29 20 3d 3d 20 69 42 20 2b 20 31 20 29 3b  64) == iB + 1 );
d4e90 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69  .    testcase( i
d4ea0 41 3c 30 20 26 26 20 2d 28 69 41 20 2b 20 4c 41  A<0 && -(iA + LA
d4eb0 52 47 45 53 54 5f 49 4e 54 36 34 29 20 3d 3d 20  RGEST_INT64) == 
d4ec0 69 42 20 2b 20 32 20 29 3b 0a 20 20 20 20 69 66  iB + 2 );.    if
d4ed0 28 20 69 41 3c 30 20 26 26 20 2d 28 69 41 20 2b  ( iA<0 && -(iA +
d4ee0 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 20   LARGEST_INT64) 
d4ef0 3e 20 69 42 20 2b 20 31 20 29 20 72 65 74 75 72  > iB + 1 ) retur
d4f00 6e 20 31 3b 0a 20 20 20 20 2a 70 41 20 2b 3d 20  n 1;.    *pA += 
d4f10 69 42 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  iB;.  }.  return
d4f20 20 30 3b 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52   0; .}.SQLITE_PR
d4f30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
d4f40 33 53 75 62 49 6e 74 36 34 28 69 36 34 20 2a 70  3SubInt64(i64 *p
d4f50 41 2c 20 69 36 34 20 69 42 29 7b 0a 20 20 74 65  A, i64 iB){.  te
d4f60 73 74 63 61 73 65 28 20 69 42 3d 3d 53 4d 41 4c  stcase( iB==SMAL
d4f70 4c 45 53 54 5f 49 4e 54 36 34 2b 31 20 29 3b 0a  LEST_INT64+1 );.
d4f80 20 20 69 66 28 20 69 42 3d 3d 53 4d 41 4c 4c 45    if( iB==SMALLE
d4f90 53 54 5f 49 4e 54 36 34 20 29 7b 0a 20 20 20 20  ST_INT64 ){.    
d4fa0 74 65 73 74 63 61 73 65 28 20 28 2a 70 41 29 3d  testcase( (*pA)=
d4fb0 3d 28 2d 31 29 20 29 3b 20 74 65 73 74 63 61 73  =(-1) ); testcas
d4fc0 65 28 20 28 2a 70 41 29 3d 3d 30 20 29 3b 0a 20  e( (*pA)==0 );. 
d4fd0 20 20 20 69 66 28 20 28 2a 70 41 29 3e 3d 30 20     if( (*pA)>=0 
d4fe0 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
d4ff0 2a 70 41 20 2d 3d 20 69 42 3b 0a 20 20 20 20 72  *pA -= iB;.    r
d5000 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65  eturn 0;.  }else
d5010 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
d5020 69 74 65 33 41 64 64 49 6e 74 36 34 28 70 41 2c  ite3AddInt64(pA,
d5030 20 2d 69 42 29 3b 0a 20 20 7d 0a 7d 0a 23 64 65   -iB);.  }.}.#de
d5040 66 69 6e 65 20 54 57 4f 50 4f 57 45 52 33 32 20  fine TWOPOWER32 
d5050 28 28 28 69 36 34 29 31 29 3c 3c 33 32 29 0a 23  (((i64)1)<<32).#
d5060 64 65 66 69 6e 65 20 54 57 4f 50 4f 57 45 52 33  define TWOPOWER3
d5070 31 20 28 28 28 69 36 34 29 31 29 3c 3c 33 31 29  1 (((i64)1)<<31)
d5080 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
d5090 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 6c 49 6e  int sqlite3MulIn
d50a0 74 36 34 28 69 36 34 20 2a 70 41 2c 20 69 36 34  t64(i64 *pA, i64
d50b0 20 69 42 29 7b 0a 20 20 69 36 34 20 69 41 20 3d   iB){.  i64 iA =
d50c0 20 2a 70 41 3b 0a 20 20 69 36 34 20 69 41 31 2c   *pA;.  i64 iA1,
d50d0 20 69 41 30 2c 20 69 42 31 2c 20 69 42 30 2c 20   iA0, iB1, iB0, 
d50e0 72 3b 0a 0a 20 20 69 41 31 20 3d 20 69 41 2f 54  r;..  iA1 = iA/T
d50f0 57 4f 50 4f 57 45 52 33 32 3b 0a 20 20 69 41 30  WOPOWER32;.  iA0
d5100 20 3d 20 69 41 20 25 20 54 57 4f 50 4f 57 45 52   = iA % TWOPOWER
d5110 33 32 3b 0a 20 20 69 42 31 20 3d 20 69 42 2f 54  32;.  iB1 = iB/T
d5120 57 4f 50 4f 57 45 52 33 32 3b 0a 20 20 69 42 30  WOPOWER32;.  iB0
d5130 20 3d 20 69 42 20 25 20 54 57 4f 50 4f 57 45 52   = iB % TWOPOWER
d5140 33 32 3b 0a 20 20 69 66 28 20 69 41 31 2a 69 42  32;.  if( iA1*iB
d5150 31 20 21 3d 20 30 20 29 20 72 65 74 75 72 6e 20  1 != 0 ) return 
d5160 31 3b 0a 20 20 61 73 73 65 72 74 28 20 69 41 31  1;.  assert( iA1
d5170 2a 69 42 30 3d 3d 30 20 7c 7c 20 69 41 30 2a 69  *iB0==0 || iA0*i
d5180 42 31 3d 3d 30 20 29 3b 0a 20 20 72 20 3d 20 69  B1==0 );.  r = i
d5190 41 31 2a 69 42 30 20 2b 20 69 41 30 2a 69 42 31  A1*iB0 + iA0*iB1
d51a0 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 72 3d  ;.  testcase( r=
d51b0 3d 28 2d 54 57 4f 50 4f 57 45 52 33 31 29 2d 31  =(-TWOPOWER31)-1
d51c0 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
d51d0 72 3d 3d 28 2d 54 57 4f 50 4f 57 45 52 33 31 29  r==(-TWOPOWER31)
d51e0 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
d51f0 72 3d 3d 54 57 4f 50 4f 57 45 52 33 31 20 29 3b  r==TWOPOWER31 );
d5200 0a 20 20 74 65 73 74 63 61 73 65 28 20 72 3d 3d  .  testcase( r==
d5210 54 57 4f 50 4f 57 45 52 33 31 2d 31 20 29 3b 0a  TWOPOWER31-1 );.
d5220 20 20 69 66 28 20 72 3c 28 2d 54 57 4f 50 4f 57    if( r<(-TWOPOW
d5230 45 52 33 31 29 20 7c 7c 20 72 3e 3d 54 57 4f 50  ER31) || r>=TWOP
d5240 4f 57 45 52 33 31 20 29 20 72 65 74 75 72 6e 20  OWER31 ) return 
d5250 31 3b 0a 20 20 72 20 2a 3d 20 54 57 4f 50 4f 57  1;.  r *= TWOPOW
d5260 45 52 33 32 3b 0a 20 20 69 66 28 20 73 71 6c 69  ER32;.  if( sqli
d5270 74 65 33 41 64 64 49 6e 74 36 34 28 26 72 2c 20  te3AddInt64(&r, 
d5280 69 41 30 2a 69 42 30 29 20 29 20 72 65 74 75 72  iA0*iB0) ) retur
d5290 6e 20 31 3b 0a 20 20 2a 70 41 20 3d 20 72 3b 0a  n 1;.  *pA = r;.
d52a0 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
d52b0 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65  *.** Compute the
d52c0 20 61 62 73 6f 6c 75 74 65 20 76 61 6c 75 65 20   absolute value 
d52d0 6f 66 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e  of a 32-bit sign
d52e0 65 64 20 69 6e 74 65 67 65 72 2c 20 6f 66 20 70  ed integer, of p
d52f0 6f 73 73 69 62 6c 65 2e 20 20 4f 72 20 0a 2a 2a  ossible.  Or .**
d5300 20 69 66 20 74 68 65 20 69 6e 74 65 67 65 72 20   if the integer 
d5310 68 61 73 20 61 20 76 61 6c 75 65 20 6f 66 20 2d  has a value of -
d5320 32 31 34 37 34 38 33 36 34 38 2c 20 72 65 74 75  2147483648, retu
d5330 72 6e 20 2b 32 31 34 37 34 38 33 36 34 37 0a 2a  rn +2147483647.*
d5340 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
d5350 20 69 6e 74 20 73 71 6c 69 74 65 33 41 62 73 49   int sqlite3AbsI
d5360 6e 74 33 32 28 69 6e 74 20 78 29 7b 0a 20 20 69  nt32(int x){.  i
d5370 66 28 20 78 3e 3d 30 20 29 20 72 65 74 75 72 6e  f( x>=0 ) return
d5380 20 78 3b 0a 20 20 69 66 28 20 78 3d 3d 28 69 6e   x;.  if( x==(in
d5390 74 29 30 78 38 30 30 30 30 30 30 30 20 29 20 72  t)0x80000000 ) r
d53a0 65 74 75 72 6e 20 30 78 37 66 66 66 66 66 66 66  eturn 0x7fffffff
d53b0 3b 0a 20 20 72 65 74 75 72 6e 20 2d 78 3b 0a 7d  ;.  return -x;.}
d53c0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
d53d0 45 4e 41 42 4c 45 5f 38 5f 33 5f 4e 41 4d 45 53  ENABLE_8_3_NAMES
d53e0 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45  ./*.** If SQLITE
d53f0 5f 45 4e 41 42 4c 45 5f 38 5f 33 5f 4e 41 4d 45  _ENABLE_8_3_NAME
d5400 53 20 69 73 20 73 65 74 20 61 74 20 63 6f 6d 70  S is set at comp
d5410 69 6c 65 2d 74 69 6d 65 20 61 6e 64 20 69 66 20  ile-time and if 
d5420 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
d5430 66 69 6c 65 6e 61 6d 65 20 69 6e 20 7a 42 61 73  filename in zBas
d5440 65 46 69 6c 65 6e 61 6d 65 20 69 73 20 61 20 55  eFilename is a U
d5450 52 49 20 77 69 74 68 20 74 68 65 20 22 38 5f 33  RI with the "8_3
d5460 5f 6e 61 6d 65 73 3d 31 22 20 70 61 72 61 6d 65  _names=1" parame
d5470 74 65 72 20 61 6e 64 0a 2a 2a 20 69 66 20 66 69  ter and.** if fi
d5480 6c 65 6e 61 6d 65 20 69 6e 20 7a 5b 5d 20 68 61  lename in z[] ha
d5490 73 20 61 20 73 75 66 66 69 78 20 28 61 2e 6b 2e  s a suffix (a.k.
d54a0 61 2e 20 22 65 78 74 65 6e 73 69 6f 6e 22 29 20  a. "extension") 
d54b0 74 68 61 74 20 69 73 20 6c 6f 6e 67 65 72 20 74  that is longer t
d54c0 68 61 6e 0a 2a 2a 20 74 68 72 65 65 20 63 68 61  han.** three cha
d54d0 72 61 63 74 65 72 73 2c 20 74 68 65 6e 20 73 68  racters, then sh
d54e0 6f 72 74 65 6e 20 74 68 65 20 73 75 66 66 69 78  orten the suffix
d54f0 20 6f 6e 20 7a 5b 5d 20 74 6f 20 62 65 20 74 68   on z[] to be th
d5500 65 20 6c 61 73 74 20 74 68 72 65 65 0a 2a 2a 20  e last three.** 
d5510 63 68 61 72 61 63 74 65 72 73 20 6f 66 20 74 68  characters of th
d5520 65 20 6f 72 69 67 69 6e 61 6c 20 73 75 66 66 69  e original suffi
d5530 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49  x..**.** If SQLI
d5540 54 45 5f 45 4e 41 42 4c 45 5f 38 5f 33 5f 4e 41  TE_ENABLE_8_3_NA
d5550 4d 45 53 20 69 73 20 73 65 74 20 74 6f 20 32 20  MES is set to 2 
d5560 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2c  at compile-time,
d5570 20 74 68 65 6e 20 61 6c 77 61 79 73 0a 2a 2a 20   then always.** 
d5580 64 6f 20 74 68 65 20 73 75 66 66 69 78 20 73 68  do the suffix sh
d5590 6f 72 74 65 6e 69 6e 67 20 72 65 67 61 72 64 6c  ortening regardl
d55a0 65 73 73 20 6f 66 20 55 52 49 20 70 61 72 61 6d  ess of URI param
d55b0 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d  eter..**.** Exam
d55c0 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ples:.**.**     
d55d0 74 65 73 74 2e 64 62 2d 6a 6f 75 72 6e 61 6c 20  test.db-journal 
d55e0 20 20 20 3d 3e 20 20 20 74 65 73 74 2e 6e 61 6c     =>   test.nal
d55f0 0a 2a 2a 20 20 20 20 20 74 65 73 74 2e 64 62 2d  .**     test.db-
d5600 77 61 6c 20 20 20 20 20 20 20 20 3d 3e 20 20 20  wal        =>   
d5610 74 65 73 74 2e 77 61 6c 0a 2a 2a 20 20 20 20 20  test.wal.**     
d5620 74 65 73 74 2e 64 62 2d 73 68 6d 20 20 20 20 20  test.db-shm     
d5630 20 20 20 3d 3e 20 20 20 74 65 73 74 2e 73 68 6d     =>   test.shm
d5640 0a 2a 2a 20 20 20 20 20 74 65 73 74 2e 64 62 2d  .**     test.db-
d5650 6d 6a 37 66 33 33 31 39 66 61 20 3d 3e 20 20 20  mj7f3319fa =>   
d5660 74 65 73 74 2e 39 66 61 0a 2a 2f 0a 53 51 4c 49  test.9fa.*/.SQLI
d5670 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
d5680 73 71 6c 69 74 65 33 46 69 6c 65 53 75 66 66 69  sqlite3FileSuffi
d5690 78 33 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  x3(const char *z
d56a0 42 61 73 65 46 69 6c 65 6e 61 6d 65 2c 20 63 68  BaseFilename, ch
d56b0 61 72 20 2a 7a 29 7b 0a 23 69 66 20 53 51 4c 49  ar *z){.#if SQLI
d56c0 54 45 5f 45 4e 41 42 4c 45 5f 38 5f 33 5f 4e 41  TE_ENABLE_8_3_NA
d56d0 4d 45 53 3c 32 0a 20 20 69 66 28 20 73 71 6c 69  MES<2.  if( sqli
d56e0 74 65 33 5f 75 72 69 5f 62 6f 6f 6c 65 61 6e 28  te3_uri_boolean(
d56f0 7a 42 61 73 65 46 69 6c 65 6e 61 6d 65 2c 20 22  zBaseFilename, "
d5700 38 5f 33 5f 6e 61 6d 65 73 22 2c 20 30 29 20 29  8_3_names", 0) )
d5710 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20  .#endif.  {.    
d5720 69 6e 74 20 69 2c 20 73 7a 3b 0a 20 20 20 20 73  int i, sz;.    s
d5730 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  z = sqlite3Strle
d5740 6e 33 30 28 7a 29 3b 0a 20 20 20 20 66 6f 72 28  n30(z);.    for(
d5750 69 3d 73 7a 2d 31 3b 20 69 3e 30 20 26 26 20 7a  i=sz-1; i>0 && z
d5760 5b 69 5d 21 3d 27 2f 27 20 26 26 20 7a 5b 69 5d  [i]!='/' && z[i]
d5770 21 3d 27 2e 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20  !='.'; i--){}.  
d5780 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2e 27 20    if( z[i]=='.' 
d5790 26 26 20 41 4c 57 41 59 53 28 73 7a 3e 69 2b 34  && ALWAYS(sz>i+4
d57a0 29 20 29 20 6d 65 6d 6d 6f 76 65 28 26 7a 5b 69  ) ) memmove(&z[i
d57b0 2b 31 5d 2c 20 26 7a 5b 73 7a 2d 33 5d 2c 20 34  +1], &z[sz-3], 4
d57c0 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a  );.  }.}.#endif.
d57d0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
d57e0 20 45 6e 64 20 6f 66 20 75 74 69 6c 2e 63 20 2a   End of util.c *
d57f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
d5820 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
d5830 20 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 68   Begin file hash
d5840 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
d5850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
d5870 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
d5880 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54  ember 22.**.** T
d5890 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
d58a0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
d58b0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
d58c0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
d58d0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
d58e0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
d58f0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
d5900 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
d5910 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
d5920 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
d5930 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
d5940 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
d5950 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
d5960 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
d5970 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
d5980 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
d5990 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
d59a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d59b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d59c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d59d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d59e0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
d59f0 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  is the implement
d5a00 61 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 69 63  ation of generic
d5a10 20 68 61 73 68 2d 74 61 62 6c 65 73 0a 2a 2a 20   hash-tables.** 
d5a20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a  used in SQLite..
d5a30 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c  */./* #include <
d5a40 61 73 73 65 72 74 2e 68 3e 20 2a 2f 0a 0a 2f 2a  assert.h> */../*
d5a50 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72   Turn bulk memor
d5a60 79 20 69 6e 74 6f 20 61 20 68 61 73 68 20 74 61  y into a hash ta
d5a70 62 6c 65 20 6f 62 6a 65 63 74 20 62 79 20 69 6e  ble object by in
d5a80 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a 2a  itializing the.*
d5a90 2a 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  * fields of the 
d5aa0 48 61 73 68 20 73 74 72 75 63 74 75 72 65 2e 0a  Hash structure..
d5ab0 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 20 69 73 20  **.** "pNew" is 
d5ac0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
d5ad0 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74   hash table that
d5ae0 20 69 73 20 74 6f 20 62 65 20 69 6e 69 74 69 61   is to be initia
d5af0 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  lized..*/.SQLITE
d5b00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
d5b10 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 48 61  lite3HashInit(Ha
d5b20 73 68 20 2a 70 4e 65 77 29 7b 0a 20 20 61 73 73  sh *pNew){.  ass
d5b30 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a  ert( pNew!=0 );.
d5b40 20 20 70 4e 65 77 2d 3e 66 69 72 73 74 20 3d 20    pNew->first = 
d5b50 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 75 6e 74  0;.  pNew->count
d5b60 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74   = 0;.  pNew->ht
d5b70 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70 4e 65 77  size = 0;.  pNew
d5b80 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20  ->ht = 0;.}../* 
d5b90 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74 72 69  Remove all entri
d5ba0 65 73 20 66 72 6f 6d 20 61 20 68 61 73 68 20 74  es from a hash t
d5bb0 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d 20 61  able.  Reclaim a
d5bc0 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 43 61  ll memory..** Ca
d5bd0 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ll this routine 
d5be0 74 6f 20 64 65 6c 65 74 65 20 61 20 68 61 73 68  to delete a hash
d5bf0 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 72 65 73   table or to res
d5c00 65 74 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a  et a hash table.
d5c10 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70 74 79 20  ** to the empty 
d5c20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  state..*/.SQLITE
d5c30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
d5c40 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 48  lite3HashClear(H
d5c50 61 73 68 20 2a 70 48 29 7b 0a 20 20 48 61 73 68  ash *pH){.  Hash
d5c60 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20  Elem *elem;     
d5c70 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
d5c80 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d  ng over all elem
d5c90 65 6e 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ents of the tabl
d5ca0 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
d5cb0 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20  pH!=0 );.  elem 
d5cc0 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 70  = pH->first;.  p
d5cd0 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20  H->first = 0;.  
d5ce0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d  sqlite3_free(pH-
d5cf0 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d  >ht);.  pH->ht =
d5d00 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65   0;.  pH->htsize
d5d10 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 65   = 0;.  while( e
d5d20 6c 65 6d 20 29 7b 0a 20 20 20 20 48 61 73 68 45  lem ){.    HashE
d5d30 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d 20 3d  lem *next_elem =
d5d40 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20   elem->next;.   
d5d50 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 65 6c   sqlite3_free(el
d5d60 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20  em);.    elem = 
d5d70 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20  next_elem;.  }. 
d5d80 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a   pH->count = 0;.
d5d90 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 61 73  }../*.** The has
d5da0 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  hing function..*
d5db0 2f 0a 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65  /.static unsigne
d5dc0 64 20 69 6e 74 20 73 74 72 48 61 73 68 28 63 6f  d int strHash(co
d5dd0 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
d5de0 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 20   nKey){.  int h 
d5df0 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  = 0;.  assert( n
d5e00 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 77 68 69 6c  Key>=0 );.  whil
d5e10 65 28 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0a  e( nKey > 0  ){.
d5e20 20 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e      h = (h<<3) ^
d5e30 20 68 20 5e 20 73 71 6c 69 74 65 33 55 70 70 65   h ^ sqlite3Uppe
d5e40 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e  rToLower[(unsign
d5e50 65 64 20 63 68 61 72 29 2a 7a 2b 2b 5d 3b 0a 20  ed char)*z++];. 
d5e60 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 7d 0a 20     nKey--;.  }. 
d5e70 20 72 65 74 75 72 6e 20 68 3b 0a 7d 0a 0a 0a 2f   return h;.}.../
d5e80 2a 20 4c 69 6e 6b 20 70 4e 65 77 20 65 6c 65 6d  * Link pNew elem
d5e90 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73  ent into the has
d5ea0 68 20 74 61 62 6c 65 20 70 48 2e 20 20 49 66 20  h table pH.  If 
d5eb0 70 45 6e 74 72 79 21 3d 30 20 74 68 65 6e 20 61  pEntry!=0 then a
d5ec0 6c 73 6f 0a 2a 2a 20 69 6e 73 65 72 74 20 70 4e  lso.** insert pN
d5ed0 65 77 20 69 6e 74 6f 20 74 68 65 20 70 45 6e 74  ew into the pEnt
d5ee0 72 79 20 68 61 73 68 20 62 75 63 6b 65 74 2e 0a  ry hash bucket..
d5ef0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
d5f00 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 0a 20 20  nsertElement(.  
d5f10 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20  Hash *pH,       
d5f20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f         /* The co
d5f30 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c  mplete hash tabl
d5f40 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68  e */.  struct _h
d5f50 74 20 2a 70 45 6e 74 72 79 2c 20 20 20 20 2f 2a  t *pEntry,    /*
d5f60 20 54 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20   The entry into 
d5f70 77 68 69 63 68 20 70 4e 65 77 20 69 73 20 69 6e  which pNew is in
d5f80 73 65 72 74 65 64 20 2a 2f 0a 20 20 48 61 73 68  serted */.  Hash
d5f90 45 6c 65 6d 20 2a 70 4e 65 77 20 20 20 20 20 20  Elem *pNew      
d5fa0 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e     /* The elemen
d5fb0 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  t to be inserted
d5fc0 20 2a 2f 0a 29 7b 0a 20 20 48 61 73 68 45 6c 65   */.){.  HashEle
d5fd0 6d 20 2a 70 48 65 61 64 3b 20 20 20 20 20 20 20  m *pHead;       
d5fe0 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74  /* First element
d5ff0 20 61 6c 72 65 61 64 79 20 69 6e 20 70 45 6e 74   already in pEnt
d6000 72 79 20 2a 2f 0a 20 20 69 66 28 20 70 45 6e 74  ry */.  if( pEnt
d6010 72 79 20 29 7b 0a 20 20 20 20 70 48 65 61 64 20  ry ){.    pHead 
d6020 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 20  = pEntry->count 
d6030 3f 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20  ? pEntry->chain 
d6040 3a 20 30 3b 0a 20 20 20 20 70 45 6e 74 72 79 2d  : 0;.    pEntry-
d6050 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 20 20 70 45  >count++;.    pE
d6060 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 70 4e  ntry->chain = pN
d6070 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ew;.  }else{.   
d6080 20 70 48 65 61 64 20 3d 20 30 3b 0a 20 20 7d 0a   pHead = 0;.  }.
d6090 20 20 69 66 28 20 70 48 65 61 64 20 29 7b 0a 20    if( pHead ){. 
d60a0 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20     pNew->next = 
d60b0 70 48 65 61 64 3b 0a 20 20 20 20 70 4e 65 77 2d  pHead;.    pNew-
d60c0 3e 70 72 65 76 20 3d 20 70 48 65 61 64 2d 3e 70  >prev = pHead->p
d60d0 72 65 76 3b 0a 20 20 20 20 69 66 28 20 70 48 65  rev;.    if( pHe
d60e0 61 64 2d 3e 70 72 65 76 20 29 7b 20 70 48 65 61  ad->prev ){ pHea
d60f0 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20  d->prev->next = 
d6100 70 4e 65 77 3b 20 7d 0a 20 20 20 20 65 6c 73 65  pNew; }.    else
d6110 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 70               { p
d6120 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b  H->first = pNew;
d6130 20 7d 0a 20 20 20 20 70 48 65 61 64 2d 3e 70 72   }.    pHead->pr
d6140 65 76 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c  ev = pNew;.  }el
d6150 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65  se{.    pNew->ne
d6160 78 74 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a  xt = pH->first;.
d6170 20 20 20 20 69 66 28 20 70 48 2d 3e 66 69 72 73      if( pH->firs
d6180 74 20 29 7b 20 70 48 2d 3e 66 69 72 73 74 2d 3e  t ){ pH->first->
d6190 70 72 65 76 20 3d 20 70 4e 65 77 3b 20 7d 0a 20  prev = pNew; }. 
d61a0 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20     pNew->prev = 
d61b0 30 3b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74  0;.    pH->first
d61c0 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a 0a   = pNew;.  }.}..
d61d0 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68 65 20 68  ./* Resize the h
d61e0 61 73 68 20 74 61 62 6c 65 20 73 6f 20 74 68 61  ash table so tha
d61f0 74 20 69 74 20 63 61 6e 74 61 69 6e 73 20 22 6e  t it cantains "n
d6200 65 77 5f 73 69 7a 65 22 20 62 75 63 6b 65 74 73  ew_size" buckets
d6210 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 73 68  ..**.** The hash
d6220 20 74 61 62 6c 65 20 6d 69 67 68 74 20 66 61 69   table might fai
d6230 6c 20 74 6f 20 72 65 73 69 7a 65 20 69 66 20 73  l to resize if s
d6240 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
d6250 66 61 69 6c 73 20 6f 72 0a 2a 2a 20 69 66 20 74  fails or.** if t
d6260 68 65 20 6e 65 77 20 73 69 7a 65 20 69 73 20 74  he new size is t
d6270 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 70  he same as the p
d6280 72 69 6f 72 20 73 69 7a 65 2e 0a 2a 2a 20 52 65  rior size..** Re
d6290 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
d62a0 20 72 65 73 69 7a 65 20 6f 63 63 75 72 73 20 61   resize occurs a
d62b0 6e 64 20 66 61 6c 73 65 20 69 66 20 6e 6f 74 2e  nd false if not.
d62c0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
d62d0 65 68 61 73 68 28 48 61 73 68 20 2a 70 48 2c 20  ehash(Hash *pH, 
d62e0 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 65 77  unsigned int new
d62f0 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72 75 63 74  _size){.  struct
d6300 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b 20 20 20   _ht *new_ht;   
d6310 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
d6320 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65 20 2a  new hash table *
d6330 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c  /.  HashElem *el
d6340 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b 20  em, *next_elem; 
d6350 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
d6360 67 20 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20  g over existing 
d6370 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a 23 69 66  elements */..#if
d6380 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53   SQLITE_MALLOC_S
d6390 4f 46 54 5f 4c 49 4d 49 54 3e 30 0a 20 20 69 66  OFT_LIMIT>0.  if
d63a0 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f  ( new_size*sizeo
d63b0 66 28 73 74 72 75 63 74 20 5f 68 74 29 3e 53 51  f(struct _ht)>SQ
d63c0 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54  LITE_MALLOC_SOFT
d63d0 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 6e 65  _LIMIT ){.    ne
d63e0 77 5f 73 69 7a 65 20 3d 20 53 51 4c 49 54 45 5f  w_size = SQLITE_
d63f0 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49  MALLOC_SOFT_LIMI
d6400 54 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20  T/sizeof(struct 
d6410 5f 68 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  _ht);.  }.  if( 
d6420 6e 65 77 5f 73 69 7a 65 3d 3d 70 48 2d 3e 68 74  new_size==pH->ht
d6430 73 69 7a 65 20 29 20 72 65 74 75 72 6e 20 30 3b  size ) return 0;
d6440 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68  .#endif..  /* Th
d6450 65 20 69 6e 61 62 69 6c 69 74 79 20 74 6f 20 61  e inability to a
d6460 6c 6c 6f 63 61 74 65 73 20 73 70 61 63 65 20 66  llocates space f
d6470 6f 72 20 61 20 6c 61 72 67 65 72 20 68 61 73 68  or a larger hash
d6480 20 74 61 62 6c 65 20 69 73 0a 20 20 2a 2a 20 61   table is.  ** a
d6490 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 69 74   performance hit
d64a0 20 62 75 74 20 69 74 20 69 73 20 6e 6f 74 20 61   but it is not a
d64b0 20 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 53   fatal error.  S
d64c0 6f 20 6d 61 72 6b 20 74 68 65 0a 20 20 2a 2a 20  o mark the.  ** 
d64d0 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 73 20 61 20  allocation as a 
d64e0 62 65 6e 69 67 6e 2e 20 55 73 65 20 73 71 6c 69  benign. Use sqli
d64f0 74 65 33 4d 61 6c 6c 6f 63 28 29 2f 6d 65 6d 73  te3Malloc()/mems
d6500 65 74 28 30 29 20 69 6e 73 74 65 61 64 20 6f 66  et(0) instead of
d6510 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61   .  ** sqlite3Ma
d6520 6c 6c 6f 63 5a 65 72 6f 28 29 20 74 6f 20 6d 61  llocZero() to ma
d6530 6b 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  ke the allocatio
d6540 6e 2c 20 61 73 20 73 71 6c 69 74 65 33 4d 61 6c  n, as sqlite3Mal
d6550 6c 6f 63 5a 65 72 6f 28 29 0a 20 20 2a 2a 20 6f  locZero().  ** o
d6560 6e 6c 79 20 7a 65 72 6f 65 73 20 74 68 65 20 72  nly zeroes the r
d6570 65 71 75 65 73 74 65 64 20 6e 75 6d 62 65 72 20  equested number 
d6580 6f 66 20 62 79 74 65 73 20 77 68 65 72 65 61 73  of bytes whereas
d6590 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 77 69 6c   this module wil
d65a0 6c 0a 20 20 2a 2a 20 75 73 65 20 74 68 65 20 61  l.  ** use the a
d65b0 63 74 75 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20  ctual amount of 
d65c0 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20  space allocated 
d65d0 66 6f 72 20 74 68 65 20 68 61 73 68 20 74 61 62  for the hash tab
d65e0 6c 65 20 28 77 68 69 63 68 0a 20 20 2a 2a 20 6d  le (which.  ** m
d65f0 61 79 20 62 65 20 6c 61 72 67 65 72 20 74 68 61  ay be larger tha
d6600 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  n the requested 
d6610 61 6d 6f 75 6e 74 29 2e 0a 20 20 2a 2f 0a 20 20  amount)..  */.  
d6620 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
d6630 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 6e 65  gnMalloc();.  ne
d6640 77 5f 68 74 20 3d 20 28 73 74 72 75 63 74 20 5f  w_ht = (struct _
d6650 68 74 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  ht *)sqlite3Mall
d6660 6f 63 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a  oc( new_size*siz
d6670 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 20  eof(struct _ht) 
d6680 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 42  );.  sqlite3EndB
d6690 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a  enignMalloc();..
d66a0 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20    if( new_ht==0 
d66b0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 73 71  ) return 0;.  sq
d66c0 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d 3e 68  lite3_free(pH->h
d66d0 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e  t);.  pH->ht = n
d66e0 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73  ew_ht;.  pH->hts
d66f0 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65 20 3d  ize = new_size =
d6700 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69   sqlite3MallocSi
d6710 7a 65 28 6e 65 77 5f 68 74 29 2f 73 69 7a 65 6f  ze(new_ht)/sizeo
d6720 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b 0a 20  f(struct _ht);. 
d6730 20 6d 65 6d 73 65 74 28 6e 65 77 5f 68 74 2c 20   memset(new_ht, 
d6740 30 2c 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65  0, new_size*size
d6750 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 29 3b  of(struct _ht));
d6760 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48 2d 3e  .  for(elem=pH->
d6770 66 69 72 73 74 2c 20 70 48 2d 3e 66 69 72 73 74  first, pH->first
d6780 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20 3d  =0; elem; elem =
d6790 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 20 20   next_elem){.   
d67a0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20   unsigned int h 
d67b0 3d 20 73 74 72 48 61 73 68 28 65 6c 65 6d 2d 3e  = strHash(elem->
d67c0 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 79  pKey, elem->nKey
d67d0 29 20 25 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 20  ) % new_size;.  
d67e0 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c    next_elem = el
d67f0 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69 6e  em->next;.    in
d6800 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20  sertElement(pH, 
d6810 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65 6d  &new_ht[h], elem
d6820 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
d6830 31 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75  1;.}../* This fu
d6840 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74 65  nction (for inte
d6850 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20 6c  rnal use only) l
d6860 6f 63 61 74 65 73 20 61 6e 20 65 6c 65 6d 65 6e  ocates an elemen
d6870 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 20  t in an.** hash 
d6880 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68  table that match
d6890 65 73 20 74 68 65 20 67 69 76 65 6e 20 6b 65 79  es the given key
d68a0 2e 20 20 54 68 65 20 68 61 73 68 20 66 6f 72 20  .  The hash for 
d68b0 74 68 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a 20  this key has.** 
d68c0 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d  already been com
d68d0 70 75 74 65 64 20 61 6e 64 20 69 73 20 70 61 73  puted and is pas
d68e0 73 65 64 20 61 73 20 74 68 65 20 34 74 68 20 70  sed as the 4th p
d68f0 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61  arameter..*/.sta
d6900 74 69 63 20 48 61 73 68 45 6c 65 6d 20 2a 66 69  tic HashElem *fi
d6910 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61  ndElementGivenHa
d6920 73 68 28 0a 20 20 63 6f 6e 73 74 20 48 61 73 68  sh(.  const Hash
d6930 20 2a 70 48 2c 20 20 20 20 20 2f 2a 20 54 68 65   *pH,     /* The
d6940 20 70 48 20 74 6f 20 62 65 20 73 65 61 72 63 68   pH to be search
d6950 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ed */.  const ch
d6960 61 72 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 54  ar *pKey,   /* T
d6970 68 65 20 6b 65 79 20 77 65 20 61 72 65 20 73 65  he key we are se
d6980 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20  arching for */. 
d6990 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20 20 20   int nKey,      
d69a0 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e       /* Bytes in
d69b0 20 6b 65 79 20 28 6e 6f 74 20 63 6f 75 6e 74 69   key (not counti
d69c0 6e 67 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74  ng zero terminat
d69d0 6f 72 29 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  or) */.  unsigne
d69e0 64 20 69 6e 74 20 68 20 20 20 20 20 20 2f 2a 20  d int h      /* 
d69f0 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68 69  The hash for thi
d6a00 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 20 48  s key. */.){.  H
d6a10 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20  ashElem *elem;  
d6a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d6a30 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68   Used to loop th
d6a40 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c  ru the element l
d6a50 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75  ist */.  int cou
d6a60 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
d6a70 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
d6a80 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 6c 65  r of elements le
d6a90 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a 0a 20  ft to test */.. 
d6aa0 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20   if( pH->ht ){. 
d6ab0 20 20 20 73 74 72 75 63 74 20 5f 68 74 20 2a 70     struct _ht *p
d6ac0 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b  Entry = &pH->ht[
d6ad0 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70  h];.    elem = p
d6ae0 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20  Entry->chain;.  
d6af0 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72 79    count = pEntry
d6b00 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d 65 6c 73 65  ->count;.  }else
d6b10 7b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 48 2d  {.    elem = pH-
d6b20 3e 66 69 72 73 74 3b 0a 20 20 20 20 63 6f 75 6e  >first;.    coun
d6b30 74 20 3d 20 70 48 2d 3e 63 6f 75 6e 74 3b 0a 20  t = pH->count;. 
d6b40 20 7d 0a 20 20 77 68 69 6c 65 28 20 63 6f 75 6e   }.  while( coun
d6b50 74 2d 2d 20 26 26 20 41 4c 57 41 59 53 28 65 6c  t-- && ALWAYS(el
d6b60 65 6d 29 20 29 7b 0a 20 20 20 20 69 66 28 20 65  em) ){.    if( e
d6b70 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d 6e 4b 65 79 20  lem->nKey==nKey 
d6b80 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
d6b90 6d 70 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 70 4b  mp(elem->pKey,pK
d6ba0 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20 0a  ey,nKey)==0 ){ .
d6bb0 20 20 20 20 20 20 72 65 74 75 72 6e 20 65 6c 65        return ele
d6bc0 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c 65  m;.    }.    ele
d6bd0 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  m = elem->next;.
d6be0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
d6bf0 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 20 73  }../* Remove a s
d6c00 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d  ingle entry from
d6c10 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
d6c20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 20  given a pointer 
d6c30 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65 6d 65  to that.** eleme
d6c40 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20 6f 6e  nt and a hash on
d6c50 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73 20 6b   the element's k
d6c60 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ey..*/.static vo
d6c70 69 64 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74  id removeElement
d6c80 47 69 76 65 6e 48 61 73 68 28 0a 20 20 48 61 73  GivenHash(.  Has
d6c90 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20 2f  h *pH,         /
d6ca0 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61 69 6e  * The pH contain
d6cb0 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20 20  ing "elem" */.  
d6cc0 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20  HashElem* elem, 
d6cd0 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74    /* The element
d6ce0 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64 20 66   to be removed f
d6cf0 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a 20 20  rom the pH */.  
d6d00 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 20  unsigned int h  
d6d10 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20    /* Hash value 
d6d20 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e 74 20  for the element 
d6d30 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 5f  */.){.  struct _
d6d40 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66  ht *pEntry;.  if
d6d50 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a  ( elem->prev ){.
d6d60 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e      elem->prev->
d6d70 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78  next = elem->nex
d6d80 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t; .  }else{.   
d6d90 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65   pH->first = ele
d6da0 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69  m->next;.  }.  i
d6db0 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b  f( elem->next ){
d6dc0 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d  .    elem->next-
d6dd0 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72  >prev = elem->pr
d6de0 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48  ev;.  }.  if( pH
d6df0 2d 3e 68 74 20 29 7b 0a 20 20 20 20 70 45 6e 74  ->ht ){.    pEnt
d6e00 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b  ry = &pH->ht[h];
d6e10 0a 20 20 20 20 69 66 28 20 70 45 6e 74 72 79 2d  .    if( pEntry-
d6e20 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 7b 0a  >chain==elem ){.
d6e30 20 20 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68        pEntry->ch
d6e40 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74  ain = elem->next
d6e50 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 6e 74  ;.    }.    pEnt
d6e60 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 20  ry->count--;.   
d6e70 20 61 73 73 65 72 74 28 20 70 45 6e 74 72 79 2d   assert( pEntry-
d6e80 3e 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 20 7d  >count>=0 );.  }
d6e90 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
d6ea0 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e 63   elem );.  pH->c
d6eb0 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 48  ount--;.  if( pH
d6ec0 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 7b 0a 20 20  ->count==0 ){.  
d6ed0 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 66 69    assert( pH->fi
d6ee0 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73  rst==0 );.    as
d6ef0 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74 3d  sert( pH->count=
d6f00 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  =0 );.    sqlite
d6f10 33 48 61 73 68 43 6c 65 61 72 28 70 48 29 3b 0a  3HashClear(pH);.
d6f20 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70    }.}../* Attemp
d6f30 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20 65  t to locate an e
d6f40 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 68 61  lement of the ha
d6f50 73 68 20 74 61 62 6c 65 20 70 48 20 77 69 74 68  sh table pH with
d6f60 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20 6d   a key.** that m
d6f70 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65 79  atches pKey,nKey
d6f80 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 64 61  .  Return the da
d6f90 74 61 20 66 6f 72 20 74 68 69 73 20 65 6c 65 6d  ta for this elem
d6fa0 65 6e 74 20 69 66 20 69 74 20 69 73 0a 2a 2a 20  ent if it is.** 
d6fb0 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20 69  found, or NULL i
d6fc0 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  f there is no ma
d6fd0 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  tch..*/.SQLITE_P
d6fe0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
d6ff0 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f 6e  ite3HashFind(con
d7000 73 74 20 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e  st Hash *pH, con
d7010 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69  st char *pKey, i
d7020 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 48 61 73 68  nt nKey){.  Hash
d7030 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f  Elem *elem;    /
d7040 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68  * The element th
d7050 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a  at matches key *
d7060 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
d7070 20 68 3b 20 20 20 20 2f 2a 20 41 20 68 61 73 68   h;    /* A hash
d7080 20 6f 6e 20 6b 65 79 20 2a 2f 0a 0a 20 20 61 73   on key */..  as
d7090 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20  sert( pH!=0 );. 
d70a0 20 61 73 73 65 72 74 28 20 70 4b 65 79 21 3d 30   assert( pKey!=0
d70b0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4b   );.  assert( nK
d70c0 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  ey>=0 );.  if( p
d70d0 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 68 20 3d  H->ht ){.    h =
d70e0 20 73 74 72 48 61 73 68 28 70 4b 65 79 2c 20 6e   strHash(pKey, n
d70f0 4b 65 79 29 20 25 20 70 48 2d 3e 68 74 73 69 7a  Key) % pH->htsiz
d7100 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  e;.  }else{.    
d7110 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 6c 65  h = 0;.  }.  ele
d7120 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74 47  m = findElementG
d7130 69 76 65 6e 48 61 73 68 28 70 48 2c 20 70 4b 65  ivenHash(pH, pKe
d7140 79 2c 20 6e 4b 65 79 2c 20 68 29 3b 0a 20 20 72  y, nKey, h);.  r
d7150 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c 65  eturn elem ? ele
d7160 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a  m->data : 0;.}..
d7170 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 65 6c 65  /* Insert an ele
d7180 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61  ment into the ha
d7190 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20 54 68  sh table pH.  Th
d71a0 65 20 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b  e key is pKey,nK
d71b0 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61  ey.** and the da
d71c0 74 61 20 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a  ta is "data"..**
d71d0 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e  .** If no elemen
d71e0 74 20 65 78 69 73 74 73 20 77 69 74 68 20 61 20  t exists with a 
d71f0 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20 74 68  matching key, th
d7200 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d  en a new.** elem
d7210 65 6e 74 20 69 73 20 63 72 65 61 74 65 64 20 61  ent is created a
d7220 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  nd NULL is retur
d7230 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ned..**.** If an
d7240 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74 20 61 6c  other element al
d7250 72 65 61 64 79 20 65 78 69 73 74 73 20 77 69 74  ready exists wit
d7260 68 20 74 68 65 20 73 61 6d 65 20 6b 65 79 2c 20  h the same key, 
d7270 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e 65 77 20  then the.** new 
d7280 64 61 74 61 20 72 65 70 6c 61 63 65 73 20 74 68  data replaces th
d7290 65 20 6f 6c 64 20 64 61 74 61 20 61 6e 64 20 74  e old data and t
d72a0 68 65 20 6f 6c 64 20 64 61 74 61 20 69 73 20 72  he old data is r
d72b0 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 65 20  eturned..** The 
d72c0 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f 70 69 65  key is not copie
d72d0 64 20 69 6e 20 74 68 69 73 20 69 6e 73 74 61 6e  d in this instan
d72e0 63 65 2e 20 20 49 66 20 61 20 6d 61 6c 6c 6f 63  ce.  If a malloc
d72f0 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a 2a 2a 20   fails, then.** 
d7300 74 68 65 20 6e 65 77 20 64 61 74 61 20 69 73 20  the new data is 
d7310 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
d7320 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 75   hash table is u
d7330 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20  nchanged..**.** 
d7340 49 66 20 74 68 65 20 22 64 61 74 61 22 20 70 61  If the "data" pa
d7350 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73 20  rameter to this 
d7360 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c  function is NULL
d7370 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 6c  , then the.** el
d7380 65 6d 65 6e 74 20 63 6f 72 72 65 73 70 6f 6e 64  ement correspond
d7390 69 6e 67 20 74 6f 20 22 6b 65 79 22 20 69 73 20  ing to "key" is 
d73a0 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65  removed from the
d73b0 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a   hash table..*/.
d73c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
d73d0 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68  oid *sqlite3Hash
d73e0 49 6e 73 65 72 74 28 48 61 73 68 20 2a 70 48 2c  Insert(Hash *pH,
d73f0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65   const char *pKe
d7400 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69  y, int nKey, voi
d7410 64 20 2a 64 61 74 61 29 7b 0a 20 20 75 6e 73 69  d *data){.  unsi
d7420 67 6e 65 64 20 69 6e 74 20 68 3b 20 20 20 20 20  gned int h;     
d7430 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20 6f 66    /* the hash of
d7440 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f 20   the key modulo 
d7450 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a 65 20  hash table size 
d7460 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65  */.  HashElem *e
d7470 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73  lem;       /* Us
d7480 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20  ed to loop thru 
d7490 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74  the element list
d74a0 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a   */.  HashElem *
d74b0 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e  new_elem;   /* N
d74c0 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 65 64  ew element added
d74d0 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a 0a 20   to the pH */.. 
d74e0 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29   assert( pH!=0 )
d74f0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79  ;.  assert( pKey
d7500 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
d7510 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66   nKey>=0 );.  if
d7520 28 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a  ( pH->htsize ){.
d7530 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 28      h = strHash(
d7540 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48  pKey, nKey) % pH
d7550 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65 6c 73  ->htsize;.  }els
d7560 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a 20 20  e{.    h = 0;.  
d7570 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45  }.  elem = findE
d7580 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28  lementGivenHash(
d7590 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 29 3b  pH,pKey,nKey,h);
d75a0 0a 20 20 69 66 28 20 65 6c 65 6d 20 29 7b 0a 20  .  if( elem ){. 
d75b0 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 74     void *old_dat
d75c0 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a  a = elem->data;.
d75d0 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20      if( data==0 
d75e0 29 7b 0a 20 20 20 20 20 20 72 65 6d 6f 76 65 45  ){.      removeE
d75f0 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28  lementGivenHash(
d7600 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20 20 20  pH,elem,h);.    
d7610 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 6c 65  }else{.      ele
d7620 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a  m->data = data;.
d7630 20 20 20 20 20 20 65 6c 65 6d 2d 3e 70 4b 65 79        elem->pKey
d7640 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 61   = pKey;.      a
d7650 73 73 65 72 74 28 6e 4b 65 79 3d 3d 65 6c 65 6d  ssert(nKey==elem
d7660 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 7d 0a 20  ->nKey);.    }. 
d7670 20 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61     return old_da
d7680 74 61 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 61  ta;.  }.  if( da
d7690 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ta==0 ) return 0
d76a0 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 20 3d 20 28  ;.  new_elem = (
d76b0 48 61 73 68 45 6c 65 6d 2a 29 73 71 6c 69 74 65  HashElem*)sqlite
d76c0 33 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28  3Malloc( sizeof(
d76d0 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a 20 20 69  HashElem) );.  i
d76e0 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 20 29  f( new_elem==0 )
d76f0 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20   return data;.  
d7700 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d  new_elem->pKey =
d7710 20 70 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 6c 65   pKey;.  new_ele
d7720 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 3b 0a  m->nKey = nKey;.
d7730 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74 61    new_elem->data
d7740 20 3d 20 64 61 74 61 3b 0a 20 20 70 48 2d 3e 63   = data;.  pH->c
d7750 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70 48  ount++;.  if( pH
d7760 2d 3e 63 6f 75 6e 74 3e 3d 31 30 20 26 26 20 70  ->count>=10 && p
d7770 48 2d 3e 63 6f 75 6e 74 20 3e 20 32 2a 70 48 2d  H->count > 2*pH-
d7780 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 69  >htsize ){.    i
d7790 66 28 20 72 65 68 61 73 68 28 70 48 2c 20 70 48  f( rehash(pH, pH
d77a0 2d 3e 63 6f 75 6e 74 2a 32 29 20 29 7b 0a 20 20  ->count*2) ){.  
d77b0 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e      assert( pH->
d77c0 68 74 73 69 7a 65 3e 30 20 29 3b 0a 20 20 20 20  htsize>0 );.    
d77d0 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b    h = strHash(pK
d77e0 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e  ey, nKey) % pH->
d77f0 68 74 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 20  htsize;.    }.  
d7800 7d 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29  }.  if( pH->ht )
d7810 7b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d  {.    insertElem
d7820 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68 74 5b  ent(pH, &pH->ht[
d7830 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20  h], new_elem);. 
d7840 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 73 65   }else{.    inse
d7850 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 30 2c  rtElement(pH, 0,
d7860 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 7d 0a   new_elem);.  }.
d7870 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
d7880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
d7890 6e 64 20 6f 66 20 68 61 73 68 2e 63 20 2a 2a 2a  nd of hash.c ***
d78a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d78b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d78c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
d78d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
d78e0 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65  egin file opcode
d78f0 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.c ************
d7900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
d7920 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * Automatically 
d7930 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e  generated.  Do n
d7940 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65  ot edit */./* Se
d7950 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 63 2e  e the mkopcodec.
d7960 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64  awk script for d
d7970 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69 66 20 21  etails. */.#if !
d7980 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
d7990 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c 20  MIT_EXPLAIN) || 
d79a0 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
d79b0 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45   || defined(VDBE
d79c0 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66  _PROFILE) || def
d79d0 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
d79e0 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  G).SQLITE_PRIVAT
d79f0 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  E const char *sq
d7a00 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28  lite3OpcodeName(
d7a10 69 6e 74 20 69 29 7b 0a 20 73 74 61 74 69 63 20  int i){. static 
d7a20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
d7a30 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 20 22  t azName[] = { "
d7a40 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 31 20  ?",.     /*   1 
d7a50 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20 20  */ "Goto",.     
d7a60 2f 2a 20 20 20 32 20 2a 2f 20 22 47 6f 73 75 62  /*   2 */ "Gosub
d7a70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 33 20 2a  ",.     /*   3 *
d7a80 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 20 20 20  / "Return",.    
d7a90 20 2f 2a 20 20 20 34 20 2a 2f 20 22 59 69 65 6c   /*   4 */ "Yiel
d7aa0 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 35 20  d",.     /*   5 
d7ab0 2a 2f 20 22 48 61 6c 74 49 66 4e 75 6c 6c 22 2c  */ "HaltIfNull",
d7ac0 0a 20 20 20 20 20 2f 2a 20 20 20 36 20 2a 2f 20  .     /*   6 */ 
d7ad0 22 48 61 6c 74 22 2c 0a 20 20 20 20 20 2f 2a 20  "Halt",.     /* 
d7ae0 20 20 37 20 2a 2f 20 22 49 6e 74 65 67 65 72 22    7 */ "Integer"
d7af0 2c 0a 20 20 20 20 20 2f 2a 20 20 20 38 20 2a 2f  ,.     /*   8 */
d7b00 20 22 49 6e 74 36 34 22 2c 0a 20 20 20 20 20 2f   "Int64",.     /
d7b10 2a 20 20 20 39 20 2a 2f 20 22 53 74 72 69 6e 67  *   9 */ "String
d7b20 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 30 20 2a  ",.     /*  10 *
d7b30 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f  / "Null",.     /
d7b40 2a 20 20 31 31 20 2a 2f 20 22 42 6c 6f 62 22 2c  *  11 */ "Blob",
d7b50 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20  .     /*  12 */ 
d7b60 22 56 61 72 69 61 62 6c 65 22 2c 0a 20 20 20 20  "Variable",.    
d7b70 20 2f 2a 20 20 31 33 20 2a 2f 20 22 4d 6f 76 65   /*  13 */ "Move
d7b80 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34 20 2a  ",.     /*  14 *
d7b90 2f 20 22 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f  / "Copy",.     /
d7ba0 2a 20 20 31 35 20 2a 2f 20 22 53 43 6f 70 79 22  *  15 */ "SCopy"
d7bb0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 36 20 2a 2f  ,.     /*  16 */
d7bc0 20 22 52 65 73 75 6c 74 52 6f 77 22 2c 0a 20 20   "ResultRow",.  
d7bd0 20 20 20 2f 2a 20 20 31 37 20 2a 2f 20 22 43 6f     /*  17 */ "Co
d7be0 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a 20  llSeq",.     /* 
d7bf0 20 31 38 20 2a 2f 20 22 46 75 6e 63 74 69 6f 6e   18 */ "Function
d7c00 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 39 20 2a  ",.     /*  19 *
d7c10 2f 20 22 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a  / "Not",.     /*
d7c20 20 20 32 30 20 2a 2f 20 22 41 64 64 49 6d 6d 22    20 */ "AddImm"
d7c30 2c 0a 20 20 20 20 20 2f 2a 20 20 32 31 20 2a 2f  ,.     /*  21 */
d7c40 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a 20 20   "MustBeInt",.  
d7c50 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22 52 65     /*  22 */ "Re
d7c60 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20  alAffinity",.   
d7c70 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 50 65 72    /*  23 */ "Per
d7c80 6d 75 74 61 74 69 6f 6e 22 2c 0a 20 20 20 20 20  mutation",.     
d7c90 2f 2a 20 20 32 34 20 2a 2f 20 22 43 6f 6d 70 61  /*  24 */ "Compa
d7ca0 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 35  re",.     /*  25
d7cb0 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a 20 20 20 20   */ "Jump",.    
d7cc0 20 2f 2a 20 20 32 36 20 2a 2f 20 22 4f 6e 63 65   /*  26 */ "Once
d7cd0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 37 20 2a  ",.     /*  27 *
d7ce0 2f 20 22 49 66 22 2c 0a 20 20 20 20 20 2f 2a 20  / "If",.     /* 
d7cf0 20 32 38 20 2a 2f 20 22 49 66 4e 6f 74 22 2c 0a   28 */ "IfNot",.
d7d00 20 20 20 20 20 2f 2a 20 20 32 39 20 2a 2f 20 22       /*  29 */ "
d7d10 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a  Column",.     /*
d7d20 20 20 33 30 20 2a 2f 20 22 41 66 66 69 6e 69 74    30 */ "Affinit
d7d30 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 31 20  y",.     /*  31 
d7d40 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 64 22 2c  */ "MakeRecord",
d7d50 0a 20 20 20 20 20 2f 2a 20 20 33 32 20 2a 2f 20  .     /*  32 */ 
d7d60 22 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a  "Count",.     /*
d7d70 20 20 33 33 20 2a 2f 20 22 53 61 76 65 70 6f 69    33 */ "Savepoi
d7d80 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 34  nt",.     /*  34
d7d90 20 2a 2f 20 22 41 75 74 6f 43 6f 6d 6d 69 74 22   */ "AutoCommit"
d7da0 2c 0a 20 20 20 20 20 2f 2a 20 20 33 35 20 2a 2f  ,.     /*  35 */
d7db0 20 22 54 72 61 6e 73 61 63 74 69 6f 6e 22 2c 0a   "Transaction",.
d7dc0 20 20 20 20 20 2f 2a 20 20 33 36 20 2a 2f 20 22       /*  36 */ "
d7dd0 52 65 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 20  ReadCookie",.   
d7de0 20 20 2f 2a 20 20 33 37 20 2a 2f 20 22 53 65 74    /*  37 */ "Set
d7df0 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a  Cookie",.     /*
d7e00 20 20 33 38 20 2a 2f 20 22 56 65 72 69 66 79 43    38 */ "VerifyC
d7e10 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20  ookie",.     /* 
d7e20 20 33 39 20 2a 2f 20 22 4f 70 65 6e 52 65 61 64   39 */ "OpenRead
d7e30 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 30 20 2a  ",.     /*  40 *
d7e40 2f 20 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a 20  / "OpenWrite",. 
d7e50 20 20 20 20 2f 2a 20 20 34 31 20 2a 2f 20 22 4f      /*  41 */ "O
d7e60 70 65 6e 41 75 74 6f 69 6e 64 65 78 22 2c 0a 20  penAutoindex",. 
d7e70 20 20 20 20 2f 2a 20 20 34 32 20 2a 2f 20 22 4f      /*  42 */ "O
d7e80 70 65 6e 45 70 68 65 6d 65 72 61 6c 22 2c 0a 20  penEphemeral",. 
d7e90 20 20 20 20 2f 2a 20 20 34 33 20 2a 2f 20 22 53      /*  43 */ "S
d7ea0 6f 72 74 65 72 4f 70 65 6e 22 2c 0a 20 20 20 20  orterOpen",.    
d7eb0 20 2f 2a 20 20 34 34 20 2a 2f 20 22 4f 70 65 6e   /*  44 */ "Open
d7ec0 50 73 65 75 64 6f 22 2c 0a 20 20 20 20 20 2f 2a  Pseudo",.     /*
d7ed0 20 20 34 35 20 2a 2f 20 22 43 6c 6f 73 65 22 2c    45 */ "Close",
d7ee0 0a 20 20 20 20 20 2f 2a 20 20 34 36 20 2a 2f 20  .     /*  46 */ 
d7ef0 22 53 65 65 6b 4c 74 22 2c 0a 20 20 20 20 20 2f  "SeekLt",.     /
d7f00 2a 20 20 34 37 20 2a 2f 20 22 53 65 65 6b 4c 65  *  47 */ "SeekLe
d7f10 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 38 20 2a  ",.     /*  48 *
d7f20 2f 20 22 53 65 65 6b 47 65 22 2c 0a 20 20 20 20  / "SeekGe",.    
d7f30 20 2f 2a 20 20 34 39 20 2a 2f 20 22 53 65 65 6b   /*  49 */ "Seek
d7f40 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30  Gt",.     /*  50
d7f50 20 2a 2f 20 22 53 65 65 6b 22 2c 0a 20 20 20 20   */ "Seek",.    
d7f60 20 2f 2a 20 20 35 31 20 2a 2f 20 22 4e 6f 74 46   /*  51 */ "NotF
d7f70 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ound",.     /*  
d7f80 35 32 20 2a 2f 20 22 46 6f 75 6e 64 22 2c 0a 20  52 */ "Found",. 
d7f90 20 20 20 20 2f 2a 20 20 35 33 20 2a 2f 20 22 49      /*  53 */ "I
d7fa0 73 55 6e 69 71 75 65 22 2c 0a 20 20 20 20 20 2f  sUnique",.     /
d7fb0 2a 20 20 35 34 20 2a 2f 20 22 4e 6f 74 45 78 69  *  54 */ "NotExi
d7fc0 73 74 73 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35  sts",.     /*  5
d7fd0 35 20 2a 2f 20 22 53 65 71 75 65 6e 63 65 22 2c  5 */ "Sequence",
d7fe0 0a 20 20 20 20 20 2f 2a 20 20 35 36 20 2a 2f 20  .     /*  56 */ 
d7ff0 22 4e 65 77 52 6f 77 69 64 22 2c 0a 20 20 20 20  "NewRowid",.    
d8000 20 2f 2a 20 20 35 37 20 2a 2f 20 22 49 6e 73 65   /*  57 */ "Inse
d8010 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 38  rt",.     /*  58
d8020 20 2a 2f 20 22 49 6e 73 65 72 74 49 6e 74 22 2c   */ "InsertInt",
d8030 0a 20 20 20 20 20 2f 2a 20 20 35 39 20 2a 2f 20  .     /*  59 */ 
d8040 22 44 65 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f  "Delete",.     /
d8050 2a 20 20 36 30 20 2a 2f 20 22 52 65 73 65 74 43  *  60 */ "ResetC
d8060 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ount",.     /*  
d8070 36 31 20 2a 2f 20 22 53 6f 72 74 65 72 43 6f 6d  61 */ "SorterCom
d8080 70 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  pare",.     /*  
d8090 36 32 20 2a 2f 20 22 53 6f 72 74 65 72 44 61 74  62 */ "SorterDat
d80a0 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 33 20  a",.     /*  63 
d80b0 2a 2f 20 22 52 6f 77 4b 65 79 22 2c 0a 20 20 20  */ "RowKey",.   
d80c0 20 20 2f 2a 20 20 36 34 20 2a 2f 20 22 52 6f 77    /*  64 */ "Row
d80d0 44 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Data",.     /*  
d80e0 36 35 20 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 20  65 */ "Rowid",. 
d80f0 20 20 20 20 2f 2a 20 20 36 36 20 2a 2f 20 22 4e      /*  66 */ "N
d8100 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a  ullRow",.     /*
d8110 20 20 36 37 20 2a 2f 20 22 4c 61 73 74 22 2c 0a    67 */ "Last",.
d8120 20 20 20 20 20 2f 2a 20 20 36 38 20 2a 2f 20 22       /*  68 */ "
d8130 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 39  Or",.     /*  69
d8140 20 2a 2f 20 22 41 6e 64 22 2c 0a 20 20 20 20 20   */ "And",.     
d8150 2f 2a 20 20 37 30 20 2a 2f 20 22 53 6f 72 74 65  /*  70 */ "Sorte
d8160 72 53 6f 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20  rSort",.     /* 
d8170 20 37 31 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20   71 */ "Sort",. 
d8180 20 20 20 20 2f 2a 20 20 37 32 20 2a 2f 20 22 52      /*  72 */ "R
d8190 65 77 69 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20  ewind",.     /* 
d81a0 20 37 33 20 2a 2f 20 22 49 73 4e 75 6c 6c 22 2c   73 */ "IsNull",
d81b0 0a 20 20 20 20 20 2f 2a 20 20 37 34 20 2a 2f 20  .     /*  74 */ 
d81c0 22 4e 6f 74 4e 75 6c 6c 22 2c 0a 20 20 20 20 20  "NotNull",.     
d81d0 2f 2a 20 20 37 35 20 2a 2f 20 22 4e 65 22 2c 0a  /*  75 */ "Ne",.
d81e0 20 20 20 20 20 2f 2a 20 20 37 36 20 2a 2f 20 22       /*  76 */ "
d81f0 45 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 37  Eq",.     /*  77
d8200 20 2a 2f 20 22 47 74 22 2c 0a 20 20 20 20 20 2f   */ "Gt",.     /
d8210 2a 20 20 37 38 20 2a 2f 20 22 4c 65 22 2c 0a 20  *  78 */ "Le",. 
d8220 20 20 20 20 2f 2a 20 20 37 39 20 2a 2f 20 22 4c      /*  79 */ "L
d8230 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 30 20  t",.     /*  80 
d8240 2a 2f 20 22 47 65 22 2c 0a 20 20 20 20 20 2f 2a  */ "Ge",.     /*
d8250 20 20 38 31 20 2a 2f 20 22 53 6f 72 74 65 72 4e    81 */ "SorterN
d8260 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38  ext",.     /*  8
d8270 32 20 2a 2f 20 22 42 69 74 41 6e 64 22 2c 0a 20  2 */ "BitAnd",. 
d8280 20 20 20 20 2f 2a 20 20 38 33 20 2a 2f 20 22 42      /*  83 */ "B
d8290 69 74 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20  itOr",.     /*  
d82a0 38 34 20 2a 2f 20 22 53 68 69 66 74 4c 65 66 74  84 */ "ShiftLeft
d82b0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 35 20 2a  ",.     /*  85 *
d82c0 2f 20 22 53 68 69 66 74 52 69 67 68 74 22 2c 0a  / "ShiftRight",.
d82d0 20 20 20 20 20 2f 2a 20 20 38 36 20 2a 2f 20 22       /*  86 */ "
d82e0 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38  Add",.     /*  8
d82f0 37 20 2a 2f 20 22 53 75 62 74 72 61 63 74 22 2c  7 */ "Subtract",
d8300 0a 20 20 20 20 20 2f 2a 20 20 38 38 20 2a 2f 20  .     /*  88 */ 
d8310 22 4d 75 6c 74 69 70 6c 79 22 2c 0a 20 20 20 20  "Multiply",.    
d8320 20 2f 2a 20 20 38 39 20 2a 2f 20 22 44 69 76 69   /*  89 */ "Divi
d8330 64 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 30  de",.     /*  90
d8340 20 2a 2f 20 22 52 65 6d 61 69 6e 64 65 72 22 2c   */ "Remainder",
d8350 0a 20 20 20 20 20 2f 2a 20 20 39 31 20 2a 2f 20  .     /*  91 */ 
d8360 22 43 6f 6e 63 61 74 22 2c 0a 20 20 20 20 20 2f  "Concat",.     /
d8370 2a 20 20 39 32 20 2a 2f 20 22 50 72 65 76 22 2c  *  92 */ "Prev",
d8380 0a 20 20 20 20 20 2f 2a 20 20 39 33 20 2a 2f 20  .     /*  93 */ 
d8390 22 42 69 74 4e 6f 74 22 2c 0a 20 20 20 20 20 2f  "BitNot",.     /
d83a0 2a 20 20 39 34 20 2a 2f 20 22 53 74 72 69 6e 67  *  94 */ "String
d83b0 38 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 35 20  8",.     /*  95 
d83c0 2a 2f 20 22 4e 65 78 74 22 2c 0a 20 20 20 20 20  */ "Next",.     
d83d0 2f 2a 20 20 39 36 20 2a 2f 20 22 53 6f 72 74 65  /*  96 */ "Sorte
d83e0 72 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 2f  rInsert",.     /
d83f0 2a 20 20 39 37 20 2a 2f 20 22 49 64 78 49 6e 73  *  97 */ "IdxIns
d8400 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39  ert",.     /*  9
d8410 38 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65 22  8 */ "IdxDelete"
d8420 2c 0a 20 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f  ,.     /*  99 */
d8430 20 22 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20   "IdxRowid",.   
d8440 20 20 2f 2a 20 31 30 30 20 2a 2f 20 22 49 64 78    /* 100 */ "Idx
d8450 4c 54 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 31  LT",.     /* 101
d8460 20 2a 2f 20 22 49 64 78 47 45 22 2c 0a 20 20 20   */ "IdxGE",.   
d8470 20 20 2f 2a 20 31 30 32 20 2a 2f 20 22 44 65 73    /* 102 */ "Des
d8480 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 31  troy",.     /* 1
d8490 30 33 20 2a 2f 20 22 43 6c 65 61 72 22 2c 0a 20  03 */ "Clear",. 
d84a0 20 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20 22 43      /* 104 */ "C
d84b0 72 65 61 74 65 49 6e 64 65 78 22 2c 0a 20 20 20  reateIndex",.   
d84c0 20 20 2f 2a 20 31 30 35 20 2a 2f 20 22 43 72 65    /* 105 */ "Cre
d84d0 61 74 65 54 61 62 6c 65 22 2c 0a 20 20 20 20 20  ateTable",.     
d84e0 2f 2a 20 31 30 36 20 2a 2f 20 22 50 61 72 73 65  /* 106 */ "Parse
d84f0 53 63 68 65 6d 61 22 2c 0a 20 20 20 20 20 2f 2a  Schema",.     /*
d8500 20 31 30 37 20 2a 2f 20 22 4c 6f 61 64 41 6e 61   107 */ "LoadAna
d8510 6c 79 73 69 73 22 2c 0a 20 20 20 20 20 2f 2a 20  lysis",.     /* 
d8520 31 30 38 20 2a 2f 20 22 44 72 6f 70 54 61 62 6c  108 */ "DropTabl
d8530 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 39 20  e",.     /* 109 
d8540 2a 2f 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0a  */ "DropIndex",.
d8550 20 20 20 20 20 2f 2a 20 31 31 30 20 2a 2f 20 22       /* 110 */ "
d8560 44 72 6f 70 54 72 69 67 67 65 72 22 2c 0a 20 20  DropTrigger",.  
d8570 20 20 20 2f 2a 20 31 31 31 20 2a 2f 20 22 49 6e     /* 111 */ "In
d8580 74 65 67 72 69 74 79 43 6b 22 2c 0a 20 20 20 20  tegrityCk",.    
d8590 20 2f 2a 20 31 31 32 20 2a 2f 20 22 52 6f 77 53   /* 112 */ "RowS
d85a0 65 74 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20  etAdd",.     /* 
d85b0 31 31 33 20 2a 2f 20 22 52 6f 77 53 65 74 52 65  113 */ "RowSetRe
d85c0 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 34  ad",.     /* 114
d85d0 20 2a 2f 20 22 52 6f 77 53 65 74 54 65 73 74 22   */ "RowSetTest"
d85e0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35 20 2a 2f  ,.     /* 115 */
d85f0 20 22 50 72 6f 67 72 61 6d 22 2c 0a 20 20 20 20   "Program",.    
d8600 20 2f 2a 20 31 31 36 20 2a 2f 20 22 50 61 72 61   /* 116 */ "Para
d8610 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 37 20  m",.     /* 117 
d8620 2a 2f 20 22 46 6b 43 6f 75 6e 74 65 72 22 2c 0a  */ "FkCounter",.
d8630 20 20 20 20 20 2f 2a 20 31 31 38 20 2a 2f 20 22       /* 118 */ "
d8640 46 6b 49 66 5a 65 72 6f 22 2c 0a 20 20 20 20 20  FkIfZero",.     
d8650 2f 2a 20 31 31 39 20 2a 2f 20 22 4d 65 6d 4d 61  /* 119 */ "MemMa
d8660 78 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 30 20  x",.     /* 120 
d8670 2a 2f 20 22 49 66 50 6f 73 22 2c 0a 20 20 20 20  */ "IfPos",.    
d8680 20 2f 2a 20 31 32 31 20 2a 2f 20 22 49 66 4e 65   /* 121 */ "IfNe
d8690 67 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 32 20  g",.     /* 122 
d86a0 2a 2f 20 22 49 66 5a 65 72 6f 22 2c 0a 20 20 20  */ "IfZero",.   
d86b0 20 20 2f 2a 20 31 32 33 20 2a 2f 20 22 41 67 67    /* 123 */ "Agg
d86c0 53 74 65 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Step",.     /* 1
d86d0 32 34 20 2a 2f 20 22 41 67 67 46 69 6e 61 6c 22  24 */ "AggFinal"
d86e0 2c 0a 20 20 20 20 20 2f 2a 20 31 32 35 20 2a 2f  ,.     /* 125 */
d86f0 20 22 43 68 65 63 6b 70 6f 69 6e 74 22 2c 0a 20   "Checkpoint",. 
d8700 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22 4a      /* 126 */ "J
d8710 6f 75 72 6e 61 6c 4d 6f 64 65 22 2c 0a 20 20 20  ournalMode",.   
d8720 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22 56 61 63    /* 127 */ "Vac
d8730 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32  uum",.     /* 12
d8740 38 20 2a 2f 20 22 49 6e 63 72 56 61 63 75 75 6d  8 */ "IncrVacuum
d8750 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 39 20 2a  ",.     /* 129 *
d8760 2f 20 22 45 78 70 69 72 65 22 2c 0a 20 20 20 20  / "Expire",.    
d8770 20 2f 2a 20 31 33 30 20 2a 2f 20 22 52 65 61 6c   /* 130 */ "Real
d8780 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 31 20 2a  ",.     /* 131 *
d8790 2f 20 22 54 61 62 6c 65 4c 6f 63 6b 22 2c 0a 20  / "TableLock",. 
d87a0 20 20 20 20 2f 2a 20 31 33 32 20 2a 2f 20 22 56      /* 132 */ "V
d87b0 42 65 67 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20  Begin",.     /* 
d87c0 31 33 33 20 2a 2f 20 22 56 43 72 65 61 74 65 22  133 */ "VCreate"
d87d0 2c 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f  ,.     /* 134 */
d87e0 20 22 56 44 65 73 74 72 6f 79 22 2c 0a 20 20 20   "VDestroy",.   
d87f0 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 56 4f 70    /* 135 */ "VOp
d8800 65 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 36  en",.     /* 136
d8810 20 2a 2f 20 22 56 46 69 6c 74 65 72 22 2c 0a 20   */ "VFilter",. 
d8820 20 20 20 20 2f 2a 20 31 33 37 20 2a 2f 20 22 56      /* 137 */ "V
d8830 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a  Column",.     /*
d8840 20 31 33 38 20 2a 2f 20 22 56 4e 65 78 74 22 2c   138 */ "VNext",
d8850 0a 20 20 20 20 20 2f 2a 20 31 33 39 20 2a 2f 20  .     /* 139 */ 
d8860 22 56 52 65 6e 61 6d 65 22 2c 0a 20 20 20 20 20  "VRename",.     
d8870 2f 2a 20 31 34 30 20 2a 2f 20 22 56 55 70 64 61  /* 140 */ "VUpda
d8880 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31  te",.     /* 141
d8890 20 2a 2f 20 22 54 6f 54 65 78 74 22 2c 0a 20 20   */ "ToText",.  
d88a0 20 20 20 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f     /* 142 */ "To
d88b0 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Blob",.     /* 1
d88c0 34 33 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63  43 */ "ToNumeric
d88d0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 34 20 2a  ",.     /* 144 *
d88e0 2f 20 22 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20  / "ToInt",.     
d88f0 2f 2a 20 31 34 35 20 2a 2f 20 22 54 6f 52 65 61  /* 145 */ "ToRea
d8900 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 36 20  l",.     /* 146 
d8910 2a 2f 20 22 50 61 67 65 63 6f 75 6e 74 22 2c 0a  */ "Pagecount",.
d8920 20 20 20 20 20 2f 2a 20 31 34 37 20 2a 2f 20 22       /* 147 */ "
d8930 4d 61 78 50 67 63 6e 74 22 2c 0a 20 20 20 20 20  MaxPgcnt",.     
d8940 2f 2a 20 31 34 38 20 2a 2f 20 22 54 72 61 63 65  /* 148 */ "Trace
d8950 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 39 20 2a  ",.     /* 149 *
d8960 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 20 2f  / "Noop",.     /
d8970 2a 20 31 35 30 20 2a 2f 20 22 45 78 70 6c 61 69  * 150 */ "Explai
d8980 6e 22 2c 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72  n",.  };.  retur
d8990 6e 20 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23  n azName[i];.}.#
d89a0 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
d89b0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70  ****** End of op
d89c0 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  codes.c ********
d89d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d89e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d89f0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
d8a00 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
d8a10 65 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a  e os_unix.c ****
d8a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8a40 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
d8a50 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54  4 May 22.**.** T
d8a60 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
d8a70 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
d8a80 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
d8a90 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
d8aa0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
d8ab0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
d8ac0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
d8ad0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
d8ae0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
d8af0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
d8b00 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
d8b10 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
d8b20 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
d8b30 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
d8b40 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
d8b50 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
d8b60 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
d8b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
d8bc0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
d8bd0 74 61 69 6e 73 20 74 68 65 20 56 46 53 20 69 6d  tains the VFS im
d8be0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  plementation for
d8bf0 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61   unix-like opera
d8c00 74 69 6e 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20  ting systems.** 
d8c10 69 6e 63 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d  include Linux, M
d8c20 61 63 4f 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58  acOSX, *BSD, QNX
d8c30 2c 20 56 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20  , VxWorks, AIX, 
d8c40 48 50 55 58 2c 20 61 6e 64 20 6f 74 68 65 72 73  HPUX, and others
d8c50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72  ..**.** There ar
d8c60 65 20 61 63 74 75 61 6c 6c 79 20 73 65 76 65 72  e actually sever
d8c70 61 6c 20 64 69 66 66 65 72 65 6e 74 20 56 46 53  al different VFS
d8c80 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
d8c90 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a   in this file..*
d8ca0 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65  * The difference
d8cb0 73 20 61 72 65 20 69 6e 20 74 68 65 20 77 61 79  s are in the way
d8cc0 20 74 68 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69   that file locki
d8cd0 6e 67 20 69 73 20 64 6f 6e 65 2e 20 20 54 68 65  ng is done.  The
d8ce0 20 64 65 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c   default.** impl
d8cf0 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20  ementation uses 
d8d00 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c  Posix Advisory L
d8d10 6f 63 6b 73 2e 20 20 41 6c 74 65 72 6e 61 74 69  ocks.  Alternati
d8d20 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ve implementatio
d8d30 6e 73 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28  ns.** use flock(
d8d40 29 2c 20 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61  ), dot-files, va
d8d50 72 69 6f 75 73 20 70 72 6f 70 72 69 65 74 61 72  rious proprietar
d8d60 79 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61  y locking schema
d8d70 73 2c 20 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20  s, or simply.** 
d8d80 73 6b 69 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c  skip locking all
d8d90 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a   together..**.**
d8da0 20 54 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c   This source fil
d8db0 65 20 69 73 20 6f 72 67 61 6e 69 7a 65 64 20 69  e is organized i
d8dc0 6e 74 6f 20 64 69 76 69 73 69 6f 6e 73 20 77 68  nto divisions wh
d8dd0 65 72 65 20 74 68 65 20 6c 6f 67 69 63 20 66 6f  ere the logic fo
d8de0 72 20 76 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62  r various.** sub
d8df0 66 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e  functions is con
d8e00 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68  tained within th
d8e10 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 64 69  e appropriate di
d8e20 76 69 73 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a  vision.  PLEASE.
d8e30 2a 2a 20 4b 45 45 50 20 54 48 45 20 53 54 52 55  ** KEEP THE STRU
d8e40 43 54 55 52 45 20 4f 46 20 54 48 49 53 20 46 49  CTURE OF THIS FI
d8e50 4c 45 20 49 4e 54 41 43 54 2e 20 20 4e 65 77 20  LE INTACT.  New 
d8e60 63 6f 64 65 20 73 68 6f 75 6c 64 20 62 65 20 70  code should be p
d8e70 6c 61 63 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20  laced.** in the 
d8e80 63 6f 72 72 65 63 74 20 64 69 76 69 73 69 6f 6e  correct division
d8e90 20 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 63   and should be c
d8ea0 6c 65 61 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a  learly labeled..
d8eb0 2a 2a 0a 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74  **.** The layout
d8ec0 20 6f 66 20 64 69 76 69 73 69 6f 6e 73 20 69 73   of divisions is
d8ed0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
d8ee0 2a 2a 20 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d  **   *  General-
d8ef0 70 75 72 70 6f 73 65 20 64 65 63 6c 61 72 61 74  purpose declarat
d8f00 69 6f 6e 73 20 61 6e 64 20 75 74 69 6c 69 74 79  ions and utility
d8f10 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20   functions..**  
d8f20 20 2a 20 20 55 6e 69 71 75 65 20 66 69 6c 65 20   *  Unique file 
d8f30 49 44 20 6c 6f 67 69 63 20 75 73 65 64 20 62 79  ID logic used by
d8f40 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a   VxWorks..**   *
d8f50 20 20 56 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e    Various lockin
d8f60 67 20 70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c  g primitive impl
d8f70 65 6d 65 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c  ementations (all
d8f80 20 65 78 63 65 70 74 20 70 72 6f 78 79 20 6c 6f   except proxy lo
d8f90 63 6b 69 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20  cking):.**      
d8fa0 2b 20 66 6f 72 20 50 6f 73 69 78 20 41 64 76 69  + for Posix Advi
d8fb0 73 6f 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20  sory Locks.**   
d8fc0 20 20 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c     + for no-op l
d8fd0 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66  ocks.**      + f
d8fe0 6f 72 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b  or dot-file lock
d8ff0 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20  s.**      + for 
d9000 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a  flock() locking.
d9010 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61  **      + for na
d9020 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f  med semaphore lo
d9030 63 6b 73 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c  cks (VxWorks onl
d9040 79 29 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72  y).**      + for
d9050 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 20   AFP filesystem 
d9060 6c 6f 63 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e  locks (MacOSX on
d9070 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69  ly).**   *  sqli
d9080 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73  te3_file methods
d9090 20 6e 6f 74 20 61 73 73 6f 63 69 61 74 65 64 20   not associated 
d90a0 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a  with locking..**
d90b0 20 20 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e     *  Definition
d90c0 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f  s of sqlite3_io_
d90d0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20  methods objects 
d90e0 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a  for all locking.
d90f0 2a 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20  **      methods 
d9100 70 6c 75 73 20 22 66 69 6e 64 65 72 22 20 66 75  plus "finder" fu
d9110 6e 63 74 69 6f 6e 73 20 66 6f 72 20 65 61 63 68  nctions for each
d9120 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e   locking method.
d9130 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33  .**   *  sqlite3
d9140 5f 76 66 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c  _vfs method impl
d9150 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20  ementations..** 
d9160 20 20 2a 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69    *  Locking pri
d9170 6d 69 74 69 76 65 73 20 66 6f 72 20 74 68 65 20  mitives for the 
d9180 70 72 6f 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69  proxy uber-locki
d9190 6e 67 2d 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f  ng-method. (MacO
d91a0 53 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20  SX only).**   * 
d91b0 20 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20   Definitions of 
d91c0 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65  sqlite3_vfs obje
d91d0 63 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b  cts for all lock
d91e0 69 6e 67 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20  ing methods.**  
d91f0 20 20 20 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65      plus impleme
d9200 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69  ntations of sqli
d9210 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e  te3_os_init() an
d9220 64 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64  d sqlite3_os_end
d9230 28 29 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ()..*/.#if SQLIT
d9240 45 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 20  E_OS_UNIX       
d9250 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66         /* This f
d9260 69 6c 65 20 69 73 20 75 73 65 64 20 6f 6e 20 75  ile is used on u
d9270 6e 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 20  nix only */../* 
d9280 55 73 65 20 70 6f 73 69 78 5f 66 61 6c 6c 6f 63  Use posix_falloc
d9290 61 74 65 28 29 20 69 66 20 69 74 20 69 73 20 61  ate() if it is a
d92a0 76 61 69 6c 61 62 6c 65 0a 2a 2f 0a 23 69 66 20  vailable.*/.#if 
d92b0 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 50 4f  !defined(HAVE_PO
d92c0 53 49 58 5f 46 41 4c 4c 4f 43 41 54 45 29 20 5c  SIX_FALLOCATE) \
d92d0 0a 20 20 20 20 20 20 26 26 20 28 5f 58 4f 50 45  .      && (_XOPE
d92e0 4e 5f 53 4f 55 52 43 45 20 3e 3d 20 36 30 30 20  N_SOURCE >= 600 
d92f0 7c 7c 20 5f 50 4f 53 49 58 5f 43 5f 53 4f 55 52  || _POSIX_C_SOUR
d9300 43 45 20 3e 3d 20 32 30 30 31 31 32 4c 29 0a 23  CE >= 200112L).#
d9310 20 64 65 66 69 6e 65 20 48 41 56 45 5f 50 4f 53   define HAVE_POS
d9320 49 58 5f 46 41 4c 4c 4f 43 41 54 45 20 31 0a 23  IX_FALLOCATE 1.#
d9330 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
d9340 72 65 20 61 72 65 20 76 61 72 69 6f 75 73 20 6d  re are various m
d9350 65 74 68 6f 64 73 20 66 6f 72 20 66 69 6c 65 20  ethods for file 
d9360 6c 6f 63 6b 69 6e 67 20 75 73 65 64 20 66 6f 72  locking used for
d9370 20 63 6f 6e 63 75 72 72 65 6e 63 79 0a 2a 2a 20   concurrency.** 
d9380 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a 0a 2a 2a 20 20  control:.**.**  
d9390 20 31 2e 20 50 4f 53 49 58 20 6c 6f 63 6b 69 6e   1. POSIX lockin
d93a0 67 20 28 74 68 65 20 64 65 66 61 75 6c 74 29 2c  g (the default),
d93b0 0a 2a 2a 20 20 20 32 2e 20 4e 6f 20 6c 6f 63 6b  .**   2. No lock
d93c0 69 6e 67 2c 0a 2a 2a 20 20 20 33 2e 20 44 6f 74  ing,.**   3. Dot
d93d0 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 0a 2a  -file locking,.*
d93e0 2a 20 20 20 34 2e 20 66 6c 6f 63 6b 28 29 20 6c  *   4. flock() l
d93f0 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 35 2e 20  ocking,.**   5. 
d9400 41 46 50 20 6c 6f 63 6b 69 6e 67 20 28 4f 53 58  AFP locking (OSX
d9410 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20 36 2e 20   only),.**   6. 
d9420 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 65 6d 61  Named POSIX sema
d9430 70 68 6f 72 65 73 20 28 56 58 57 6f 72 6b 73 20  phores (VXWorks 
d9440 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20 37 2e 20 70  only),.**   7. p
d9450 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 20 28 4f  roxy locking. (O
d9460 53 58 20 6f 6e 6c 79 29 0a 2a 2a 0a 2a 2a 20 53  SX only).**.** S
d9470 74 79 6c 65 73 20 34 2c 20 35 2c 20 61 6e 64 20  tyles 4, 5, and 
d9480 37 20 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c  7 are only avail
d9490 61 62 6c 65 20 6f 66 20 53 51 4c 49 54 45 5f 45  able of SQLITE_E
d94a0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
d94b0 59 4c 45 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65  YLE.** is define
d94c0 64 20 74 6f 20 31 2e 20 20 54 68 65 20 53 51 4c  d to 1.  The SQL
d94d0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
d94e0 4e 47 5f 53 54 59 4c 45 20 61 6c 73 6f 20 65 6e  NG_STYLE also en
d94f0 61 62 6c 65 73 20 61 75 74 6f 6d 61 74 69 63 0a  ables automatic.
d9500 2a 2a 20 73 65 6c 65 63 74 69 6f 6e 20 6f 66 20  ** selection of 
d9510 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
d9520 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 62 61  locking style ba
d9530 73 65 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 73  sed on the files
d9540 79 73 74 65 6d 0a 2a 2a 20 77 68 65 72 65 20 74  ystem.** where t
d9550 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6c  he database is l
d9560 6f 63 61 74 65 64 2e 20 20 0a 2a 2f 0a 23 69 66  ocated.  .*/.#if
d9570 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
d9580 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
d9590 53 54 59 4c 45 29 0a 23 20 20 69 66 20 64 65 66  STYLE).#  if def
d95a0 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a  ined(__APPLE__).
d95b0 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49  #    define SQLI
d95c0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
d95d0 47 5f 53 54 59 4c 45 20 31 0a 23 20 20 65 6c 73  G_STYLE 1.#  els
d95e0 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51  e.#    define SQ
d95f0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
d9600 49 4e 47 5f 53 54 59 4c 45 20 30 0a 23 20 20 65  ING_STYLE 0.#  e
d9610 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  ndif.#endif../*.
d9620 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20 4f 53  ** Define the OS
d9630 5f 56 58 57 4f 52 4b 53 20 70 72 65 2d 70 72 6f  _VXWORKS pre-pro
d9640 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 74 6f 20  cessor macro to 
d9650 31 20 69 66 20 62 75 69 6c 64 69 6e 67 20 6f 6e  1 if building on
d9660 20 0a 2a 2a 20 76 78 77 6f 72 6b 73 2c 20 6f 72   .** vxworks, or
d9670 20 30 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f   0 otherwise..*/
d9680 0a 23 69 66 6e 64 65 66 20 4f 53 5f 56 58 57 4f  .#ifndef OS_VXWO
d9690 52 4b 53 0a 23 20 20 69 66 20 64 65 66 69 6e 65  RKS.#  if define
d96a0 64 28 5f 5f 52 54 50 5f 5f 29 20 7c 7c 20 64 65  d(__RTP__) || de
d96b0 66 69 6e 65 64 28 5f 57 52 53 5f 4b 45 52 4e 45  fined(_WRS_KERNE
d96c0 4c 29 0a 23 20 20 20 20 64 65 66 69 6e 65 20 4f  L).#    define O
d96d0 53 5f 56 58 57 4f 52 4b 53 20 31 0a 23 20 20 65  S_VXWORKS 1.#  e
d96e0 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20  lse.#    define 
d96f0 4f 53 5f 56 58 57 4f 52 4b 53 20 30 0a 23 20 20  OS_VXWORKS 0.#  
d9700 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a  endif.#endif../*
d9710 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 66 69 6e  .** These #defin
d9720 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 62 6c 65  es should enable
d9730 20 3e 32 47 42 20 66 69 6c 65 20 73 75 70 70 6f   >2GB file suppo
d9740 72 74 20 6f 6e 20 50 6f 73 69 78 20 69 66 20 74  rt on Posix if t
d9750 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67  he.** underlying
d9760 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
d9770 6d 20 73 75 70 70 6f 72 74 73 20 69 74 2e 20 20  m supports it.  
d9780 49 66 20 74 68 65 20 4f 53 20 6c 61 63 6b 73 0a  If the OS lacks.
d9790 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65 20 73 75  ** large file su
d97a0 70 70 6f 72 74 2c 20 74 68 65 73 65 20 73 68 6f  pport, these sho
d97b0 75 6c 64 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a  uld be no-ops..*
d97c0 2a 0a 2a 2a 20 4c 61 72 67 65 20 66 69 6c 65 20  *.** Large file 
d97d0 73 75 70 70 6f 72 74 20 63 61 6e 20 62 65 20 64  support can be d
d97e0 69 73 61 62 6c 65 64 20 75 73 69 6e 67 20 74 68  isabled using th
d97f0 65 20 2d 44 53 51 4c 49 54 45 5f 44 49 53 41 42  e -DSQLITE_DISAB
d9800 4c 45 5f 4c 46 53 20 73 77 69 74 63 68 0a 2a 2a  LE_LFS switch.**
d9810 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72   on the compiler
d9820 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 20 20   command line.  
d9830 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72  This is necessar
d9840 79 20 69 66 20 79 6f 75 20 61 72 65 20 63 6f 6d  y if you are com
d9850 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e 20 61 20 72  piling.** on a r
d9860 65 63 65 6e 74 20 6d 61 63 68 69 6e 65 20 28 65  ecent machine (e
d9870 78 3a 20 52 65 64 48 61 74 20 37 2e 32 29 20 62  x: RedHat 7.2) b
d9880 75 74 20 79 6f 75 20 77 61 6e 74 20 79 6f 75 72  ut you want your
d9890 20 63 6f 64 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a   code to work.**
d98a0 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 20 6d 61 63   on an older mac
d98b0 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61 74  hine (ex: RedHat
d98c0 20 36 2e 30 29 2e 20 20 49 66 20 79 6f 75 20 63   6.0).  If you c
d98d0 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 64 48 61 74  ompile on RedHat
d98e0 20 37 2e 32 0a 2a 2a 20 77 69 74 68 6f 75 74 20   7.2.** without 
d98f0 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 4c 46 53  this option, LFS
d9900 20 69 73 20 65 6e 61 62 6c 65 2e 20 20 42 75 74   is enable.  But
d9910 20 4c 46 53 20 64 6f 65 73 20 6e 6f 74 20 65 78   LFS does not ex
d9920 69 73 74 20 69 6e 20 74 68 65 20 6b 65 72 6e 65  ist in the kerne
d9930 6c 0a 2a 2a 20 69 6e 20 52 65 64 48 61 74 20 36  l.** in RedHat 6
d9940 2e 30 2c 20 73 6f 20 74 68 65 20 63 6f 64 65 20  .0, so the code 
d9950 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e  won't work.  Hen
d9960 63 65 2c 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20  ce, for maximum 
d9970 62 69 6e 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62  binary.** portab
d9980 69 6c 69 74 79 20 79 6f 75 20 73 68 6f 75 6c 64  ility you should
d9990 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a   omit LFS..**.**
d99a0 20 54 68 65 20 70 72 65 76 69 6f 75 73 20 70 61   The previous pa
d99b0 72 61 67 72 61 70 68 20 77 61 73 20 77 72 69 74  ragraph was writ
d99c0 74 65 6e 20 69 6e 20 32 30 30 35 2e 20 20 28 54  ten in 2005.  (T
d99d0 68 69 73 20 70 61 72 61 67 72 61 70 68 20 69 73  his paragraph is
d99e0 20 77 72 69 74 74 65 6e 0a 2a 2a 20 6f 6e 20 32   written.** on 2
d99f0 30 30 38 2d 31 31 2d 32 38 2e 29 20 54 68 65 73  008-11-28.) Thes
d9a00 65 20 64 61 79 73 2c 20 61 6c 6c 20 4c 69 6e 75  e days, all Linu
d9a10 78 20 6b 65 72 6e 65 6c 73 20 73 75 70 70 6f 72  x kernels suppor
d9a20 74 20 6c 61 72 67 65 20 66 69 6c 65 73 2c 20 73  t large files, s
d9a30 6f 0a 2a 2a 20 79 6f 75 20 73 68 6f 75 6c 64 20  o.** you should 
d9a40 70 72 6f 62 61 62 6c 79 20 6c 65 61 76 65 20 4c  probably leave L
d9a50 46 53 20 65 6e 61 62 6c 65 64 2e 20 20 42 75 74  FS enabled.  But
d9a60 20 73 6f 6d 65 20 65 6d 62 65 64 64 65 64 20 70   some embedded p
d9a70 6c 61 74 66 6f 72 6d 73 20 6d 69 67 68 74 0a 2a  latforms might.*
d9a80 2a 20 6c 61 63 6b 20 4c 46 53 20 69 6e 20 77 68  * lack LFS in wh
d9a90 69 63 68 20 63 61 73 65 20 74 68 65 20 53 51 4c  ich case the SQL
d9aa0 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20  ITE_DISABLE_LFS 
d9ab0 6d 61 63 72 6f 20 6d 69 67 68 74 20 73 74 69 6c  macro might stil
d9ac0 6c 20 62 65 20 75 73 65 66 75 6c 2e 0a 2a 2f 0a  l be useful..*/.
d9ad0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44  #ifndef SQLITE_D
d9ae0 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66  ISABLE_LFS.# def
d9af0 69 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20  ine _LARGE_FILE 
d9b00 20 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66        1.# ifndef
d9b10 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49   _FILE_OFFSET_BI
d9b20 54 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46  TS.#   define _F
d9b30 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20  ILE_OFFSET_BITS 
d9b40 36 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66  64.# endif.# def
d9b50 69 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53  ine _LARGEFILE_S
d9b60 4f 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a  OURCE 1.#endif..
d9b70 2f 2a 0a 2a 2a 20 73 74 61 6e 64 61 72 64 20 69  /*.** standard i
d9b80 6e 63 6c 75 64 65 20 66 69 6c 65 73 2e 0a 2a 2f  nclude files..*/
d9b90 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74  .#include <sys/t
d9ba0 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  ypes.h>.#include
d9bb0 20 3c 73 79 73 2f 73 74 61 74 2e 68 3e 0a 23 69   <sys/stat.h>.#i
d9bc0 6e 63 6c 75 64 65 20 3c 66 63 6e 74 6c 2e 68 3e  nclude <fcntl.h>
d9bd0 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 73 74  .#include <unist
d9be0 64 2e 68 3e 0a 2f 2a 20 23 69 6e 63 6c 75 64 65  d.h>./* #include
d9bf0 20 3c 74 69 6d 65 2e 68 3e 20 2a 2f 0a 23 69 6e   <time.h> */.#in
d9c00 63 6c 75 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e  clude <sys/time.
d9c10 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72  h>.#include <err
d9c20 6e 6f 2e 68 3e 0a 23 69 66 6e 64 65 66 20 53 51  no.h>.#ifndef SQ
d9c30 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 23 69  LITE_OMIT_WAL.#i
d9c40 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d 6d 61 6e  nclude <sys/mman
d9c50 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66  .h>.#endif...#if
d9c60 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
d9c70 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 23 20 69  OCKING_STYLE.# i
d9c80 6e 63 6c 75 64 65 20 3c 73 79 73 2f 69 6f 63 74  nclude <sys/ioct
d9c90 6c 2e 68 3e 0a 23 20 69 66 20 4f 53 5f 56 58 57  l.h>.# if OS_VXW
d9ca0 4f 52 4b 53 0a 23 20 20 69 6e 63 6c 75 64 65 20  ORKS.#  include 
d9cb0 3c 73 65 6d 61 70 68 6f 72 65 2e 68 3e 0a 23 20  <semaphore.h>.# 
d9cc0 20 69 6e 63 6c 75 64 65 20 3c 6c 69 6d 69 74 73   include <limits
d9cd0 2e 68 3e 0a 23 20 65 6c 73 65 0a 23 20 20 69 6e  .h>.# else.#  in
d9ce0 63 6c 75 64 65 20 3c 73 79 73 2f 66 69 6c 65 2e  clude <sys/file.
d9cf0 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73  h>.#  include <s
d9d00 79 73 2f 70 61 72 61 6d 2e 68 3e 0a 23 20 65 6e  ys/param.h>.# en
d9d10 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  dif.#endif /* SQ
d9d20 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
d9d30 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 23 69  ING_STYLE */..#i
d9d40 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  f defined(__APPL
d9d50 45 5f 5f 29 20 7c 7c 20 28 53 51 4c 49 54 45 5f  E__) || (SQLITE_
d9d60 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
d9d70 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f  TYLE && !OS_VXWO
d9d80 52 4b 53 29 0a 23 20 69 6e 63 6c 75 64 65 20 3c  RKS).# include <
d9d90 73 79 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 65 6e  sys/mount.h>.#en
d9da0 64 69 66 0a 0a 23 69 66 64 65 66 20 48 41 56 45  dif..#ifdef HAVE
d9db0 5f 55 54 49 4d 45 0a 23 20 69 6e 63 6c 75 64 65  _UTIME.# include
d9dc0 20 3c 75 74 69 6d 65 2e 68 3e 0a 23 65 6e 64 69   <utime.h>.#endi
d9dd0 66 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  f../*.** Allowed
d9de0 20 76 61 6c 75 65 73 20 6f 66 20 75 6e 69 78 46   values of unixF
d9df0 69 6c 65 2e 66 73 46 6c 61 67 73 0a 2a 2f 0a 23  ile.fsFlags.*/.#
d9e00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 53  define SQLITE_FS
d9e10 46 4c 41 47 53 5f 49 53 5f 4d 53 44 4f 53 20 20  FLAGS_IS_MSDOS  
d9e20 20 20 20 30 78 31 0a 0a 2f 2a 0a 2a 2a 20 49 66     0x1../*.** If
d9e30 20 77 65 20 61 72 65 20 74 6f 20 62 65 20 74 68   we are to be th
d9e40 72 65 61 64 2d 73 61 66 65 2c 20 69 6e 63 6c 75  read-safe, inclu
d9e50 64 65 20 74 68 65 20 70 74 68 72 65 61 64 73 20  de the pthreads 
d9e60 68 65 61 64 65 72 20 61 6e 64 20 64 65 66 69 6e  header and defin
d9e70 65 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f  e.** the SQLITE_
d9e80 55 4e 49 58 5f 54 48 52 45 41 44 53 20 6d 61 63  UNIX_THREADS mac
d9e90 72 6f 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ro..*/.#if SQLIT
d9ea0 45 5f 54 48 52 45 41 44 53 41 46 45 0a 2f 2a 20  E_THREADSAFE./* 
d9eb0 23 20 69 6e 63 6c 75 64 65 20 3c 70 74 68 72 65  # include <pthre
d9ec0 61 64 2e 68 3e 20 2a 2f 0a 23 20 64 65 66 69 6e  ad.h> */.# defin
d9ed0 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48  e SQLITE_UNIX_TH
d9ee0 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a  READS 1.#endif..
d9ef0 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 70 65  /*.** Default pe
d9f00 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63  rmissions when c
d9f10 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 66 69  reating a new fi
d9f20 6c 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  le.*/.#ifndef SQ
d9f30 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c  LITE_DEFAULT_FIL
d9f40 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20  E_PERMISSIONS.# 
d9f50 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
d9f60 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49  FAULT_FILE_PERMI
d9f70 53 53 49 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64  SSIONS 0644.#end
d9f80 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c  if../*.** Defaul
d9f90 74 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 77 68  t permissions wh
d9fa0 65 6e 20 63 72 65 61 74 69 6e 67 20 61 75 74 6f  en creating auto
d9fb0 20 70 72 6f 78 79 20 64 69 72 0a 2a 2f 0a 23 69   proxy dir.*/.#i
d9fc0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46  fndef SQLITE_DEF
d9fd0 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45  AULT_PROXYDIR_PE
d9fe0 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69  RMISSIONS.# defi
d9ff0 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ne SQLITE_DEFAUL
da000 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49  T_PROXYDIR_PERMI
da010 53 53 49 4f 4e 53 20 30 37 35 35 0a 23 65 6e 64  SSIONS 0755.#end
da020 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75  if../*.** Maximu
da030 6d 20 73 75 70 70 6f 72 74 65 64 20 70 61 74 68  m supported path
da040 2d 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 65 66  -length..*/.#def
da050 69 6e 65 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45  ine MAX_PATHNAME
da060 20 35 31 32 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79   512../*.** Only
da070 20 73 65 74 20 74 68 65 20 6c 61 73 74 45 72 72   set the lastErr
da080 6e 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20  no if the error 
da090 63 6f 64 65 20 69 73 20 61 20 72 65 61 6c 20 65  code is a real e
da0a0 72 72 6f 72 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a  rror and not .**
da0b0 20 61 20 6e 6f 72 6d 61 6c 20 65 78 70 65 63 74   a normal expect
da0c0 65 64 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f  ed return code o
da0d0 66 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 72  f SQLITE_BUSY or
da0e0 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64   SQLITE_OK.*/.#d
da0f0 65 66 69 6e 65 20 49 53 5f 4c 4f 43 4b 5f 45 52  efine IS_LOCK_ER
da100 52 4f 52 28 78 29 20 20 28 28 78 20 21 3d 20 53  ROR(x)  ((x != S
da110 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28 78 20  QLITE_OK) && (x 
da120 21 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 29 29  != SQLITE_BUSY))
da130 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66  ../* Forward ref
da140 65 72 65 6e 63 65 73 20 2a 2f 0a 74 79 70 65 64  erences */.typed
da150 65 66 20 73 74 72 75 63 74 20 75 6e 69 78 53 68  ef struct unixSh
da160 6d 20 75 6e 69 78 53 68 6d 3b 20 20 20 20 20 20  m unixShm;      
da170 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e           /* Conn
da180 65 63 74 69 6f 6e 20 73 68 61 72 65 64 20 6d 65  ection shared me
da190 6d 6f 72 79 20 2a 2f 0a 74 79 70 65 64 65 66 20  mory */.typedef 
da1a0 73 74 72 75 63 74 20 75 6e 69 78 53 68 6d 4e 6f  struct unixShmNo
da1b0 64 65 20 75 6e 69 78 53 68 6d 4e 6f 64 65 3b 20  de unixShmNode; 
da1c0 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20        /* Shared 
da1d0 6d 65 6d 6f 72 79 20 69 6e 73 74 61 6e 63 65 20  memory instance 
da1e0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
da1f0 74 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20  t unixInodeInfo 
da200 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 3b 20 20  unixInodeInfo;  
da210 20 2f 2a 20 41 6e 20 69 2d 6e 6f 64 65 20 2a 2f   /* An i-node */
da220 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
da230 55 6e 69 78 55 6e 75 73 65 64 46 64 20 55 6e 69  UnixUnusedFd Uni
da240 78 55 6e 75 73 65 64 46 64 3b 20 20 20 20 20 2f  xUnusedFd;     /
da250 2a 20 41 6e 20 75 6e 75 73 65 64 20 66 69 6c 65  * An unused file
da260 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 0a   descriptor */..
da270 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 2c  /*.** Sometimes,
da280 20 61 66 74 65 72 20 61 20 66 69 6c 65 20 68 61   after a file ha
da290 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 62  ndle is closed b
da2a0 79 20 53 51 4c 69 74 65 2c 20 74 68 65 20 66 69  y SQLite, the fi
da2b0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a  le descriptor.**
da2c0 20 63 61 6e 6e 6f 74 20 62 65 20 63 6c 6f 73 65   cannot be close
da2d0 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49  d immediately. I
da2e0 6e 20 74 68 65 73 65 20 63 61 73 65 73 2c 20 69  n these cases, i
da2f0 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20  nstances of the 
da300 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72  following.** str
da310 75 63 74 75 72 65 20 61 72 65 20 75 73 65 64 20  ucture are used 
da320 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c  to store the fil
da330 65 20 64 65 73 63 72 69 70 74 6f 72 20 77 68 69  e descriptor whi
da340 6c 65 20 77 61 69 74 69 6e 67 20 66 6f 72 20 61  le waiting for a
da350 6e 0a 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79  n.** opportunity
da360 20 74 6f 20 65 69 74 68 65 72 20 63 6c 6f 73 65   to either close
da370 20 6f 72 20 72 65 75 73 65 20 69 74 2e 0a 2a 2f   or reuse it..*/
da380 0a 73 74 72 75 63 74 20 55 6e 69 78 55 6e 75 73  .struct UnixUnus
da390 65 64 46 64 20 7b 0a 20 20 69 6e 74 20 66 64 3b  edFd {.  int fd;
da3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
da3b0 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72     /* File descr
da3c0 69 70 74 6f 72 20 74 6f 20 63 6c 6f 73 65 20 2a  iptor to close *
da3d0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20  /.  int flags;  
da3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
da3f0 20 46 6c 61 67 73 20 74 68 69 73 20 66 69 6c 65   Flags this file
da400 20 64 65 73 63 72 69 70 74 6f 72 20 77 61 73 20   descriptor was 
da410 6f 70 65 6e 65 64 20 77 69 74 68 20 2a 2f 0a 20  opened with */. 
da420 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70   UnixUnusedFd *p
da430 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65  Next;      /* Ne
da440 78 74 20 75 6e 75 73 65 64 20 66 69 6c 65 20 64  xt unused file d
da450 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 73 61 6d  escriptor on sam
da460 65 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  e file */.};../*
da470 0a 2a 2a 20 54 68 65 20 75 6e 69 78 46 69 6c 65  .** The unixFile
da480 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73 75   structure is su
da490 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65  bclass of sqlite
da4a0 33 5f 66 69 6c 65 20 73 70 65 63 69 66 69 63 20  3_file specific 
da4b0 74 6f 20 74 68 65 20 75 6e 69 78 0a 2a 2a 20 56  to the unix.** V
da4c0 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  FS implementatio
da4d0 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ns..*/.typedef s
da4e0 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20 75  truct unixFile u
da4f0 6e 69 78 46 69 6c 65 3b 0a 73 74 72 75 63 74 20  nixFile;.struct 
da500 75 6e 69 78 46 69 6c 65 20 7b 0a 20 20 73 71 6c  unixFile {.  sql
da510 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
da520 63 6f 6e 73 74 20 2a 70 4d 65 74 68 6f 64 3b 20  const *pMethod; 
da530 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66   /* Always the f
da540 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20  irst entry */.  
da550 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
da560 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
da570 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74      /* The VFS t
da580 68 61 74 20 63 72 65 61 74 65 64 20 74 68 69 73  hat created this
da590 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 20 20 75   unixFile */.  u
da5a0 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49  nixInodeInfo *pI
da5b0 6e 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  node;           
da5c0 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74     /* Info about
da5d0 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 69 73 20 69   locks on this i
da5e0 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 3b  node */.  int h;
da5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
da600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
da610 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   The file descri
da620 70 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  ptor */.  unsign
da630 65 64 20 63 68 61 72 20 65 46 69 6c 65 4c 6f 63  ed char eFileLoc
da640 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  k;            /*
da650 20 54 68 65 20 74 79 70 65 20 6f 66 20 6c 6f 63   The type of loc
da660 6b 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66  k held on this f
da670 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  d */.  unsigned 
da680 73 68 6f 72 74 20 69 6e 74 20 63 74 72 6c 46 6c  short int ctrlFl
da690 61 67 73 3b 20 20 20 20 20 20 20 2f 2a 20 42 65  ags;       /* Be
da6a0 68 61 76 69 6f 72 61 6c 20 62 69 74 73 2e 20 20  havioral bits.  
da6b0 55 4e 49 58 46 49 4c 45 5f 2a 20 66 6c 61 67 73  UNIXFILE_* flags
da6c0 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 73 74 45 72   */.  int lastEr
da6d0 72 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  rno;            
da6e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
da6f0 20 75 6e 69 78 20 65 72 72 6e 6f 20 66 72 6f 6d   unix errno from
da700 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 20   last I/O error 
da710 2a 2f 0a 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69  */.  void *locki
da720 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 20  ngContext;      
da730 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b           /* Lock
da740 69 6e 67 20 73 74 79 6c 65 20 73 70 65 63 69 66  ing style specif
da750 69 63 20 73 74 61 74 65 20 2a 2f 0a 20 20 55 6e  ic state */.  Un
da760 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75  ixUnusedFd *pUnu
da770 73 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  sed;            
da780 20 20 2f 2a 20 50 72 65 2d 61 6c 6c 6f 63 61 74    /* Pre-allocat
da790 65 64 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20  ed UnixUnusedFd 
da7a0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
da7b0 2a 7a 50 61 74 68 3b 20 20 20 20 20 20 20 20 20  *zPath;         
da7c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
da7d0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a   of the file */.
da7e0 20 20 75 6e 69 78 53 68 6d 20 2a 70 53 68 6d 3b    unixShm *pShm;
da7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
da800 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20        /* Shared 
da810 6d 65 6d 6f 72 79 20 73 65 67 6d 65 6e 74 20 69  memory segment i
da820 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20  nformation */.  
da830 69 6e 74 20 73 7a 43 68 75 6e 6b 3b 20 20 20 20  int szChunk;    
da840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
da850 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65      /* Configure
da860 64 20 62 79 20 46 43 4e 54 4c 5f 43 48 55 4e 4b  d by FCNTL_CHUNK
da870 5f 53 49 5a 45 20 2a 2f 0a 23 69 66 64 65 66 20  _SIZE */.#ifdef 
da880 5f 5f 51 4e 58 4e 54 4f 5f 5f 0a 20 20 69 6e 74  __QNXNTO__.  int
da890 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 20   sectorSize;    
da8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
da8b0 20 2f 2a 20 44 65 76 69 63 65 20 73 65 63 74 6f   /* Device secto
da8c0 72 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20  r size */.  int 
da8d0 64 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  deviceCharacteri
da8e0 73 74 69 63 73 3b 20 20 20 20 20 20 20 20 20 20  stics;          
da8f0 2f 2a 20 50 72 65 63 6f 6d 70 75 74 65 64 20 64  /* Precomputed d
da900 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69  evice characteri
da910 73 74 69 63 73 20 2a 2f 0a 23 65 6e 64 69 66 0a  stics */.#endif.
da920 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  #if SQLITE_ENABL
da930 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
da940 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73 3b    int openFlags;
da950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
da960 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6c 61        /* The fla
da970 67 73 20 73 70 65 63 69 66 69 65 64 20 61 74 20  gs specified at 
da980 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 6e 64 69 66  open() */.#endif
da990 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  .#if SQLITE_ENAB
da9a0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
da9b0 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 41 50   || defined(__AP
da9c0 50 4c 45 5f 5f 29 0a 20 20 75 6e 73 69 67 6e 65  PLE__).  unsigne
da9d0 64 20 66 73 46 6c 61 67 73 3b 20 20 20 20 20 20  d fsFlags;      
da9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
da9f0 63 61 63 68 65 64 20 64 65 74 61 69 6c 73 20 66  cached details f
daa00 72 6f 6d 20 73 74 61 74 66 73 28 29 20 2a 2f 0a  rom statfs() */.
daa10 23 65 6e 64 69 66 0a 23 69 66 20 4f 53 5f 56 58  #endif.#if OS_VX
daa20 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 76  WORKS.  struct v
daa30 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49  xworksFileId *pI
daa40 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55  d;          /* U
daa50 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 2a 2f  nique file ID */
daa60 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
daa70 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a  QLITE_DEBUG.  /*
daa80 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20   The next group 
daa90 6f 66 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  of variables are
daaa0 20 75 73 65 64 20 74 6f 20 74 72 61 63 6b 20 77   used to track w
daab0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
daac0 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69  e.  ** transacti
daad0 6f 6e 20 63 6f 75 6e 74 65 72 20 69 6e 20 62 79  on counter in by
daae0 74 65 73 20 32 34 2d 32 37 20 6f 66 20 64 61 74  tes 24-27 of dat
daaf0 61 62 61 73 65 20 66 69 6c 65 73 20 61 72 65 20  abase files are 
dab00 75 70 64 61 74 65 64 0a 20 20 2a 2a 20 77 68 65  updated.  ** whe
dab10 6e 65 76 65 72 20 61 6e 79 20 70 61 72 74 20 6f  never any part o
dab20 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  f the database c
dab30 68 61 6e 67 65 73 2e 20 20 41 6e 20 61 73 73 65  hanges.  An asse
dab40 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c  rtion fault will
dab50 0a 20 20 2a 2a 20 6f 63 63 75 72 20 69 66 20 61  .  ** occur if a
dab60 20 66 69 6c 65 20 69 73 20 75 70 64 61 74 65 64   file is updated
dab70 20 77 69 74 68 6f 75 74 20 61 6c 73 6f 20 75 70   without also up
dab80 64 61 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73  dating the trans
dab90 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 6f 75 6e  action.  ** coun
daba0 74 65 72 2e 20 20 54 68 69 73 20 74 65 73 74 20  ter.  This test 
dabb0 69 73 20 6d 61 64 65 20 74 6f 20 61 76 6f 69 64  is made to avoid
dabc0 20 6e 65 77 20 70 72 6f 62 6c 65 6d 73 20 73 69   new problems si
dabd0 6d 69 6c 61 72 20 74 6f 20 74 68 65 0a 20 20 2a  milar to the.  *
dabe0 2a 20 6f 6e 65 20 64 65 73 63 72 69 62 65 64 20  * one described 
dabf0 62 79 20 74 69 63 6b 65 74 20 23 33 35 38 34 2e  by ticket #3584.
dac00 20 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65   .  */.  unsigne
dac10 64 20 63 68 61 72 20 74 72 61 6e 73 43 6e 74 72  d char transCntr
dac20 43 68 6e 67 3b 20 20 20 2f 2a 20 54 72 75 65 20  Chng;   /* True 
dac30 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  if the transacti
dac40 6f 6e 20 63 6f 75 6e 74 65 72 20 63 68 61 6e 67  on counter chang
dac50 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  ed */.  unsigned
dac60 20 63 68 61 72 20 64 62 55 70 64 61 74 65 3b 20   char dbUpdate; 
dac70 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
dac80 66 20 61 6e 79 20 70 61 72 74 20 6f 66 20 64 61  f any part of da
dac90 74 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e  tabase file chan
daca0 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ged */.  unsigne
dacb0 64 20 63 68 61 72 20 69 6e 4e 6f 72 6d 61 6c 57  d char inNormalW
dacc0 72 69 74 65 3b 20 20 20 2f 2a 20 54 72 75 65 20  rite;   /* True 
dacd0 69 66 20 69 6e 20 61 20 6e 6f 72 6d 61 6c 20 77  if in a normal w
dace0 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 2a  rite operation *
dacf0 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  /.#endif.#ifdef 
dad00 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a  SQLITE_TEST.  /*
dad10 20 49 6e 20 74 65 73 74 20 6d 6f 64 65 2c 20 69   In test mode, i
dad20 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65  ncrease the size
dad30 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
dad40 72 65 20 61 20 62 69 74 20 73 6f 20 74 68 61 74  re a bit so that
dad50 20 0a 20 20 2a 2a 20 69 74 20 69 73 20 6c 61 72   .  ** it is lar
dad60 67 65 72 20 74 68 61 6e 20 74 68 65 20 73 74 72  ger than the str
dad70 75 63 74 20 43 72 61 73 68 46 69 6c 65 20 64 65  uct CrashFile de
dad80 66 69 6e 65 64 20 69 6e 20 74 65 73 74 36 2e 63  fined in test6.c
dad90 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 61 50  ..  */.  char aP
dada0 61 64 64 69 6e 67 5b 33 32 5d 3b 0a 23 65 6e 64  adding[32];.#end
dadb0 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  if.};../*.** All
dadc0 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20  owed values for 
dadd0 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 63 74 72  the unixFile.ctr
dade0 6c 46 6c 61 67 73 20 62 69 74 6d 61 73 6b 3a 0a  lFlags bitmask:.
dadf0 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 49 58 46  */.#define UNIXF
dae00 49 4c 45 5f 45 58 43 4c 20 20 20 20 20 20 20 20  ILE_EXCL        
dae10 30 78 30 31 20 20 20 20 20 2f 2a 20 43 6f 6e 6e  0x01     /* Conn
dae20 65 63 74 69 6f 6e 73 20 66 72 6f 6d 20 6f 6e 65  ections from one
dae30 20 70 72 6f 63 65 73 73 20 6f 6e 6c 79 20 2a 2f   process only */
dae40 0a 23 64 65 66 69 6e 65 20 55 4e 49 58 46 49 4c  .#define UNIXFIL
dae50 45 5f 52 44 4f 4e 4c 59 20 20 20 20 20 20 30 78  E_RDONLY      0x
dae60 30 32 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63  02     /* Connec
dae70 74 69 6f 6e 20 69 73 20 72 65 61 64 20 6f 6e 6c  tion is read onl
dae80 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 49  y */.#define UNI
dae90 58 46 49 4c 45 5f 50 45 52 53 49 53 54 5f 57 41  XFILE_PERSIST_WA
daea0 4c 20 30 78 30 34 20 20 20 20 20 2f 2a 20 50 65  L 0x04     /* Pe
daeb0 72 73 69 73 74 65 6e 74 20 57 41 4c 20 6d 6f 64  rsistent WAL mod
daec0 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  e */.#ifndef SQL
daed0 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 53  ITE_DISABLE_DIRS
daee0 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 55 4e 49  YNC.# define UNI
daef0 58 46 49 4c 45 5f 44 49 52 53 59 4e 43 20 20 20  XFILE_DIRSYNC   
daf00 20 30 78 30 38 20 20 20 20 20 2f 2a 20 44 69 72   0x08     /* Dir
daf10 65 63 74 6f 72 79 20 73 79 6e 63 20 6e 65 65 64  ectory sync need
daf20 65 64 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 64 65  ed */.#else.# de
daf30 66 69 6e 65 20 55 4e 49 58 46 49 4c 45 5f 44 49  fine UNIXFILE_DI
daf40 52 53 59 4e 43 20 20 20 20 30 78 30 30 0a 23 65  RSYNC    0x00.#e
daf50 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 55 4e 49  ndif.#define UNI
daf60 58 46 49 4c 45 5f 50 53 4f 57 20 20 20 20 20 20  XFILE_PSOW      
daf70 20 20 30 78 31 30 20 20 20 20 20 2f 2a 20 53 51    0x10     /* SQ
daf80 4c 49 54 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52  LITE_IOCAP_POWER
daf90 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 20 2a  SAFE_OVERWRITE *
dafa0 2f 0a 23 64 65 66 69 6e 65 20 55 4e 49 58 46 49  /.#define UNIXFI
dafb0 4c 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 30  LE_DELETE      0
dafc0 78 32 30 20 20 20 20 20 2f 2a 20 44 65 6c 65 74  x20     /* Delet
dafd0 65 20 6f 6e 20 63 6c 6f 73 65 20 2a 2f 0a 23 64  e on close */.#d
dafe0 65 66 69 6e 65 20 55 4e 49 58 46 49 4c 45 5f 55  efine UNIXFILE_U
daff0 52 49 20 20 20 20 20 20 20 20 20 30 78 34 30 20  RI         0x40 
db000 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20      /* Filename 
db010 6d 69 67 68 74 20 68 61 76 65 20 71 75 65 72 79  might have query
db020 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a 23   parameters */.#
db030 64 65 66 69 6e 65 20 55 4e 49 58 46 49 4c 45 5f  define UNIXFILE_
db040 4e 4f 4c 4f 43 4b 20 20 20 20 20 20 30 78 38 30  NOLOCK      0x80
db050 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 20 66 69       /* Do no fi
db060 6c 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 2f  le locking */../
db070 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64  *.** Include cod
db080 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e  e that is common
db090 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66   to all os_*.c f
db0a0 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  iles.*/./*******
db0b0 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
db0c0 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74  os_common.h in t
db0d0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f  he middle of os_
db0e0 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  unix.c *********
db0f0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
db100 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
db110 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a  le os_common.h *
db120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db140 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
db150 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20  04 May 22.**.** 
db160 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
db170 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
db180 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
db190 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
db1a0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
db1b0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
db1c0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
db1d0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
db1e0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
db1f0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
db200 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
db210 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
db220 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
db230 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
db240 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
db250 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
db260 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
db270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
db2c0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
db2d0 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e  ntains macros an
db2e0 64 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f  d a little bit o
db2f0 66 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63  f code that is c
db300 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20  ommon to.** all 
db310 6f 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d  of the platform-
db320 73 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28  specific files (
db330 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23  os_*.c) and is #
db340 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68  included into th
db350 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a  ose.** files..**
db360 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68  .** This file sh
db370 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65  ould be #include
db380 64 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20  d by the os_*.c 
db390 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20  files only.  It 
db3a0 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65  is not a.** gene
db3b0 72 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64  ral purpose head
db3c0 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e  er file..*/.#ifn
db3d0 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48  def _OS_COMMON_H
db3e0 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f  _.#define _OS_CO
db3f0 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41  MMON_H_../*.** A
db400 74 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73  t least two bugs
db410 20 68 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e   have slipped in
db420 20 62 65 63 61 75 73 65 20 77 65 20 63 68 61 6e   because we chan
db430 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44  ged the MEMORY_D
db440 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f  EBUG.** macro to
db450 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e   SQLITE_DEBUG an
db460 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b  d some older mak
db470 65 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20  efiles have not 
db480 79 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20  yet made the.** 
db490 73 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c  switch.  The fol
db4a0 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75  lowing code shou
db4b0 6c 64 20 63 61 74 63 68 20 74 68 69 73 20 70 72  ld catch this pr
db4c0 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65  oblem at compile
db4d0 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  -time..*/.#ifdef
db4e0 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20   MEMORY_DEBUG.# 
db4f0 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52  error "The MEMOR
db500 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73  Y_DEBUG macro is
db510 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20   obsolete.  Use 
db520 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73  SQLITE_DEBUG ins
db530 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23  tead.".#endif..#
db540 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
db550 45 5f 54 45 53 54 29 20 26 26 20 64 65 66 69 6e  E_TEST) && defin
db560 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
db570 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  .# ifndef SQLITE
db580 5f 44 45 42 55 47 5f 4f 53 5f 54 52 41 43 45 0a  _DEBUG_OS_TRACE.
db590 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  #   define SQLIT
db5a0 45 5f 44 45 42 55 47 5f 4f 53 5f 54 52 41 43 45  E_DEBUG_OS_TRACE
db5b0 20 30 0a 23 20 65 6e 64 69 66 0a 20 20 69 6e 74   0.# endif.  int
db5c0 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20   sqlite3OSTrace 
db5d0 3d 20 53 51 4c 49 54 45 5f 44 45 42 55 47 5f 4f  = SQLITE_DEBUG_O
db5e0 53 5f 54 52 41 43 45 3b 0a 23 20 64 65 66 69 6e  S_TRACE;.# defin
db5f0 65 20 4f 53 54 52 41 43 45 28 58 29 20 20 20 20  e OSTRACE(X)    
db600 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
db610 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74  3OSTrace ) sqlit
db620 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 58 0a  e3DebugPrintf X.
db630 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 4f  #else.# define O
db640 53 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66  STRACE(X).#endif
db650 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66  ../*.** Macros f
db660 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74  or performance t
db670 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c  racing.  Normall
db680 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f  y turned off.  O
db690 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20  nly works.** on 
db6a0 69 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a  i486 hardware..*
db6b0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
db6c0 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43  PERFORMANCE_TRAC
db6d0 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65  E../* .** hwtime
db6e0 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69  .h contains inli
db6f0 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64  ne assembler cod
db700 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69  e for implementi
db710 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66  ng .** high-perf
db720 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72  ormance timing r
db730 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a  outines..*/./***
db740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
db750 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20  ude hwtime.h in 
db760 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73  the middle of os
db770 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a  _common.h ******
db780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
db790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
db7a0 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20  n file hwtime.h 
db7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
db7e0 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a  * 2008 May 27.**
db7f0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
db800 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
db810 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
db820 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
db830 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
db840 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
db850 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
db860 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
db870 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
db880 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
db890 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
db8a0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
db8b0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
db8c0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
db8d0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
db8e0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
db8f0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
db900 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
db910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db950 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
db960 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e  e contains inlin
db970 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72  e asm code for r
db980 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d  etrieving "high-
db990 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20  performance".** 
db9a0 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36  counters for x86
db9b0 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2f 0a   class CPUs..*/.
db9c0 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f  #ifndef _HWTIME_
db9d0 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49  H_.#define _HWTI
db9e0 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ME_H_../*.** The
db9f0 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
dba00 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e  ne only works on
dba10 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28   pentium-class (
dba20 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73  or newer) proces
dba30 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73  sors..** It uses
dba40 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64   the RDTSC opcod
dba50 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79  e to read the cy
dba60 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20  cle count value 
dba70 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72  out of the.** pr
dba80 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75  ocessor and retu
dba90 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20  rns that value. 
dbaa0 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65   This can be use
dbab0 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a  d for high-res.*
dbac0 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a  * profiling..*/.
dbad0 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47  #if (defined(__G
dbae0 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  NUC__) || define
dbaf0 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20  d(_MSC_VER)) && 
dbb00 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64  \.      (defined
dbb10 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65  (i386) || define
dbb20 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64  d(__i386__) || d
dbb30 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29  efined(_M_IX86))
dbb40 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28  ..  #if defined(
dbb50 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69  __GNUC__)..  __i
dbb60 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75  nline__ sqlite_u
dbb70 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
dbb80 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
dbb90 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c  unsigned int lo,
dbba0 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f   hi;.     __asm_
dbbb0 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
dbbc0 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28  "rdtsc" : "=a" (
dbbd0 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b  lo), "=d" (hi));
dbbe0 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71  .     return (sq
dbbf0 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c  lite_uint64)hi <
dbc00 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a  < 32 | lo;.  }..
dbc10 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28    #elif defined(
dbc20 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64  _MSC_VER)..  __d
dbc30 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f  eclspec(naked) _
dbc40 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75  _inline sqlite_u
dbc50 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71  int64 __cdecl sq
dbc60 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
dbc70 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a  ){.     __asm {.
dbc80 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20          rdtsc.  
dbc90 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20        ret       
dbca0 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61  ; return value a
dbcb0 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d  t EDX:EAX.     }
dbcc0 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a  .  }..  #endif..
dbcd0 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f  #elif (defined(_
dbce0 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69  _GNUC__) && defi
dbcf0 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29  ned(__x86_64__))
dbd00 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
dbd10 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
dbd20 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
dbd30 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
dbd40 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20   long val;.     
dbd50 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74   __asm__ __volat
dbd60 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a  ile__ ("rdtsc" :
dbd70 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20   "=A" (val));.  
dbd80 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a      return val;.
dbd90 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66    }. .#elif (def
dbda0 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26  ined(__GNUC__) &
dbdb0 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f  & defined(__ppc_
dbdc0 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f  _))..  __inline_
dbdd0 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  _ sqlite_uint64 
dbde0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
dbdf0 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67  id){.      unsig
dbe00 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65  ned long long re
dbe10 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69  tval;.      unsi
dbe20 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a  gned long junk;.
dbe30 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f        __asm__ __
dbe40 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c  volatile__ ("\n\
dbe50 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20  .          1:   
dbe60 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c     mftbu   %1\n\
dbe70 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
dbe80 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e     mftb    %L0\n
dbe90 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
dbea0 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e      mftbu   %0\n
dbeb0 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
dbec0 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25      cmpw    %0,%
dbed0 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  1\n\.           
dbee0 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31         bne     1
dbef0 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b".             
dbf00 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74       : "=r" (ret
dbf10 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b  val), "=r" (junk
dbf20 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ));.      return
dbf30 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65   retval;.  }..#e
dbf40 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65  lse..  #error Ne
dbf50 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ed implementatio
dbf60 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69  n of sqlite3Hwti
dbf70 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c  me() for your pl
dbf80 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20  atform...  /*.  
dbf90 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69  ** To compile wi
dbfa0 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69  thout implementi
dbfb0 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  ng sqlite3Hwtime
dbfc0 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74  () for your plat
dbfd0 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63  form,.  ** you c
dbfe0 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62  an remove the ab
dbff0 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75  ove #error and u
dc000 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  se the following
dc010 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74  .  ** stub funct
dc020 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c  ion.  You will l
dc030 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f  ose timing suppo
dc040 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a  rt for many.  **
dc050 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e   of the debuggin
dc060 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74  g and testing ut
dc070 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20  ilities, but it 
dc080 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c  should at.  ** l
dc090 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64  east compile and
dc0a0 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54   run..  */.SQLIT
dc0b0 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69  E_PRIVATE   sqli
dc0c0 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
dc0d0 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72  3Hwtime(void){ r
dc0e0 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75  eturn ((sqlite_u
dc0f0 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e  int64)0); }..#en
dc100 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  dif..#endif /* !
dc110 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f  defined(_HWTIME_
dc120 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  H_) */../*******
dc130 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68  ******* End of h
dc140 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
dc150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dc170 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
dc180 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
dc190 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
dc1a0 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f   off in os_commo
dc1b0 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.h ************
dc1c0 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20  ******/..static 
dc1d0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f  sqlite_uint64 g_
dc1e0 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71  start;.static sq
dc1f0 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c  lite_uint64 g_el
dc200 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54  apsed;.#define T
dc210 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20  IMER_START      
dc220 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33   g_start=sqlite3
dc230 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65  Hwtime().#define
dc240 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20   TIMER_END      
dc250 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c     g_elapsed=sql
dc260 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73  ite3Hwtime()-g_s
dc270 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d  tart.#define TIM
dc280 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67  ER_ELAPSED     g
dc290 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23  _elapsed.#else.#
dc2a0 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41  define TIMER_STA
dc2b0 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  RT.#define TIMER
dc2c0 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d  _END.#define TIM
dc2d0 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28  ER_ELAPSED     (
dc2e0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30  (sqlite_uint64)0
dc2f0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
dc300 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69  If we compile wi
dc310 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45  th the SQLITE_TE
dc320 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68  ST macro set, th
dc330 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  en the following
dc340 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64   block.** of cod
dc350 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74  e will give us t
dc360 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69  he ability to si
dc370 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f  mulate a disk I/
dc380 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a  O error.  This.*
dc390 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  * is used for te
dc3a0 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65  sting the I/O re
dc3b0 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f  covery logic..*/
dc3c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
dc3d0 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
dc3e0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
dc3f0 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20  ror_hit = 0;    
dc400 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
dc410 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45   number of I/O E
dc420 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f  rrors */.SQLITE_
dc430 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
dc440 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74  io_error_hardhit
dc450 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
dc460 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65  Number of non-be
dc470 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53  nign errors */.S
dc480 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
dc490 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
dc4a0 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20  ending = 0;     
dc4b0 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e     /* Count down
dc4c0 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72   to first I/O er
dc4d0 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ror */.SQLITE_AP
dc4e0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
dc4f0 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d  _error_persist =
dc500 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72   0;        /* Tr
dc510 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73  ue if I/O errors
dc520 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49   persist */.SQLI
dc530 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
dc540 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69  e3_io_error_beni
dc550 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  gn = 0;         
dc560 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72  /* True if error
dc570 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a  s are benign */.
dc580 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
dc590 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
dc5a0 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c  pending = 0;.SQL
dc5b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
dc5c0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30  te3_diskfull = 0
dc5d0 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ;.#define Simula
dc5e0 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
dc5f0 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  X) sqlite3_io_er
dc600 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23  ror_benign=(X).#
dc610 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49  define SimulateI
dc620 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a  OError(CODE)  \.
dc630 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69    if( (sqlite3_i
dc640 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20  o_error_persist 
dc650 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  && sqlite3_io_er
dc660 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20  ror_hit) \.     
dc670 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f    || sqlite3_io_
dc680 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20  error_pending-- 
dc690 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20  == 1 )  \.      
dc6a0 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f          { local_
dc6b0 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d  ioerr(); CODE; }
dc6c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63  .static void loc
dc6d0 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f  al_ioerr(){.  IO
dc6e0 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22  TRACE(("IOERR\n"
dc6f0 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f  ));.  sqlite3_io
dc700 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20  _error_hit++;.  
dc710 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f  if( !sqlite3_io_
dc720 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73  error_benign ) s
dc730 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
dc740 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65  hardhit++;.}.#de
dc750 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73  fine SimulateDis
dc760 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29  kfullError(CODE)
dc770 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65   \.   if( sqlite
dc780 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
dc790 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28  ng ){ \.     if(
dc7a0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
dc7b0 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29  l_pending == 1 )
dc7c0 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c  { \.       local
dc7d0 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20  _ioerr(); \.    
dc7e0 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66     sqlite3_diskf
dc7f0 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20  ull = 1; \.     
dc800 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72    sqlite3_io_err
dc810 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20  or_hit = 1; \.  
dc820 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20       CODE; \.   
dc830 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20    }else{ \.     
dc840 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75    sqlite3_diskfu
dc850 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a  ll_pending--; \.
dc860 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65       } \.   }.#e
dc870 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  lse.#define Simu
dc880 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
dc890 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d  n(X).#define Sim
dc8a0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a  ulateIOError(A).
dc8b0 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
dc8c0 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29  DiskfullError(A)
dc8d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
dc8e0 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65  hen testing, kee
dc8f0 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65  p a count of the
dc900 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20   number of open 
dc910 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66  files..*/.#ifdef
dc920 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
dc930 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
dc940 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f  te3_open_file_co
dc950 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65  unt = 0;.#define
dc960 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20   OpenCounter(X) 
dc970 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69   sqlite3_open_fi
dc980 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65  le_count+=(X).#e
dc990 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e  lse.#define Open
dc9a0 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69  Counter(X).#endi
dc9b0 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  f..#endif /* !de
dc9c0 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e  fined(_OS_COMMON
dc9d0 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  _H_) */../******
dc9e0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
dc9f0 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
dca00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dca10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dca20 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
dca30 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
dca40 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
dca50 74 20 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78  t off in os_unix
dca60 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
dca70 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
dca80 44 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 6d  Define various m
dca90 61 63 72 6f 73 20 74 68 61 74 20 61 72 65 20 6d  acros that are m
dcaa0 69 73 73 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65  issing from some
dcab0 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66   systems..*/.#if
dcac0 6e 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45  ndef O_LARGEFILE
dcad0 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47  .# define O_LARG
dcae0 45 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23  EFILE 0.#endif.#
dcaf0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53  ifdef SQLITE_DIS
dcb00 41 42 4c 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66  ABLE_LFS.# undef
dcb10 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64   O_LARGEFILE.# d
dcb20 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c  efine O_LARGEFIL
dcb30 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  E 0.#endif.#ifnd
dcb40 65 66 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20  ef O_NOFOLLOW.# 
dcb50 64 65 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f  define O_NOFOLLO
dcb60 57 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  W 0.#endif.#ifnd
dcb70 65 66 20 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65  ef O_BINARY.# de
dcb80 66 69 6e 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a  fine O_BINARY 0.
dcb90 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
dcba0 65 20 74 68 72 65 61 64 69 64 20 6d 61 63 72 6f  e threadid macro
dcbb0 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 74 68 65   resolves to the
dcbc0 20 74 68 72 65 61 64 2d 69 64 20 6f 72 20 74 6f   thread-id or to
dcbd0 20 30 2e 20 20 55 73 65 64 20 66 6f 72 0a 2a 2a   0.  Used for.**
dcbe0 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62   testing and deb
dcbf0 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ugging only..*/.
dcc00 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
dcc10 44 53 41 46 45 0a 23 64 65 66 69 6e 65 20 74 68  DSAFE.#define th
dcc20 72 65 61 64 69 64 20 70 74 68 72 65 61 64 5f 73  readid pthread_s
dcc30 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64 65 66  elf().#else.#def
dcc40 69 6e 65 20 74 68 72 65 61 64 69 64 20 30 0a 23  ine threadid 0.#
dcc50 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 69 66  endif../*.** Dif
dcc60 66 65 72 65 6e 74 20 55 6e 69 78 20 73 79 73 74  ferent Unix syst
dcc70 65 6d 73 20 64 65 63 6c 61 72 65 20 6f 70 65 6e  ems declare open
dcc80 28 29 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20  () in different 
dcc90 77 61 79 73 2e 20 20 53 61 6d 65 20 75 73 65 0a  ways.  Same use.
dcca0 2a 2a 20 6f 70 65 6e 28 63 6f 6e 73 74 20 63 68  ** open(const ch
dccb0 61 72 2a 2c 69 6e 74 2c 6d 6f 64 65 5f 74 29 2e  ar*,int,mode_t).
dccc0 20 20 4f 74 68 65 72 73 20 75 73 65 20 6f 70 65    Others use ope
dccd0 6e 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  n(const char*,in
dcce0 74 2c 2e 2e 2e 29 2e 0a 2a 2a 20 54 68 65 20 64  t,...)..** The d
dccf0 69 66 66 65 72 65 6e 63 65 20 69 73 20 69 6d 70  ifference is imp
dcd00 6f 72 74 61 6e 74 20 77 68 65 6e 20 75 73 69 6e  ortant when usin
dcd10 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  g a pointer to t
dcd20 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  he function..**.
dcd30 2a 2a 20 54 68 65 20 73 61 66 65 73 74 20 77 61  ** The safest wa
dcd40 79 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74  y to deal with t
dcd50 68 65 20 70 72 6f 62 6c 65 6d 20 69 73 20 74 6f  he problem is to
dcd60 20 61 6c 77 61 79 73 20 75 73 65 20 74 68 69 73   always use this
dcd70 20 77 72 61 70 70 65 72 0a 2a 2a 20 77 68 69 63   wrapper.** whic
dcd80 68 20 61 6c 77 61 79 73 20 68 61 73 20 74 68 65  h always has the
dcd90 20 73 61 6d 65 20 77 65 6c 6c 2d 64 65 66 69 6e   same well-defin
dcda0 65 64 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  ed interface..*/
dcdb0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 73 69  .static int posi
dcdc0 78 4f 70 65 6e 28 63 6f 6e 73 74 20 63 68 61 72  xOpen(const char
dcdd0 20 2a 7a 46 69 6c 65 2c 20 69 6e 74 20 66 6c 61   *zFile, int fla
dcde0 67 73 2c 20 69 6e 74 20 6d 6f 64 65 29 7b 0a 20  gs, int mode){. 
dcdf0 20 72 65 74 75 72 6e 20 6f 70 65 6e 28 7a 46 69   return open(zFi
dce00 6c 65 2c 20 66 6c 61 67 73 2c 20 6d 6f 64 65 29  le, flags, mode)
dce10 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 73 6f  ;.}../*.** On so
dce20 6d 65 20 73 79 73 74 65 6d 73 2c 20 63 61 6c 6c  me systems, call
dce30 73 20 74 6f 20 66 63 68 6f 77 6e 28 29 20 77 69  s to fchown() wi
dce40 6c 6c 20 74 72 69 67 67 65 72 20 61 20 6d 65 73  ll trigger a mes
dce50 73 61 67 65 20 69 6e 20 61 20 73 65 63 75 72 69  sage in a securi
dce60 74 79 0a 2a 2a 20 6c 6f 67 20 69 66 20 74 68 65  ty.** log if the
dce70 79 20 63 6f 6d 65 20 66 72 6f 6d 20 6e 6f 6e 2d  y come from non-
dce80 72 6f 6f 74 20 70 72 6f 63 65 73 73 65 73 2e 20  root processes. 
dce90 20 53 6f 20 61 76 6f 69 64 20 63 61 6c 6c 69 6e   So avoid callin
dcea0 67 20 66 63 68 6f 77 6e 28 29 20 69 66 0a 2a 2a  g fchown() if.**
dceb0 20 77 65 20 61 72 65 20 6e 6f 74 20 72 75 6e 6e   we are not runn
dcec0 69 6e 67 20 61 73 20 72 6f 6f 74 2e 0a 2a 2f 0a  ing as root..*/.
dced0 73 74 61 74 69 63 20 69 6e 74 20 70 6f 73 69 78  static int posix
dcee0 46 63 68 6f 77 6e 28 69 6e 74 20 66 64 2c 20 75  Fchown(int fd, u
dcef0 69 64 5f 74 20 75 69 64 2c 20 67 69 64 5f 74 20  id_t uid, gid_t 
dcf00 67 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 67  gid){.  return g
dcf10 65 74 65 75 69 64 28 29 20 3f 20 30 20 3a 20 66  eteuid() ? 0 : f
dcf20 63 68 6f 77 6e 28 66 64 2c 75 69 64 2c 67 69 64  chown(fd,uid,gid
dcf30 29 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  );.}../* Forward
dcf40 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74   reference */.st
dcf50 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 69 72  atic int openDir
dcf60 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 68 61  ectory(const cha
dcf70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 2f 2a 0a 2a  r*, int*);../*.*
dcf80 2a 20 4d 61 6e 79 20 73 79 73 74 65 6d 20 63 61  * Many system ca
dcf90 6c 6c 73 20 61 72 65 20 61 63 63 65 73 73 65 64  lls are accessed
dcfa0 20 74 68 72 6f 75 67 68 20 70 6f 69 6e 74 65 72   through pointer
dcfb0 2d 74 6f 2d 66 75 6e 63 74 69 6f 6e 73 20 73 6f  -to-functions so
dcfc0 20 74 68 61 74 0a 2a 2a 20 74 68 65 79 20 6d 61   that.** they ma
dcfd0 79 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20  y be overridden 
dcfe0 61 74 20 72 75 6e 74 69 6d 65 20 74 6f 20 66 61  at runtime to fa
dcff0 63 69 6c 69 74 61 74 65 20 66 61 75 6c 74 20 69  cilitate fault i
dd000 6e 6a 65 63 74 69 6f 6e 20 64 75 72 69 6e 67 0a  njection during.
dd010 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 73  ** testing and s
dd020 61 6e 64 62 6f 78 69 6e 67 2e 20 20 54 68 65 20  andboxing.  The 
dd030 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 20  following array 
dd040 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 73 20  holds the names 
dd050 61 6e 64 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20  and pointers.** 
dd060 74 6f 20 61 6c 6c 20 6f 76 65 72 72 69 64 65 61  to all overridea
dd070 62 6c 65 20 73 79 73 74 65 6d 20 63 61 6c 6c 73  ble system calls
dd080 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  ..*/.static stru
dd090 63 74 20 75 6e 69 78 5f 73 79 73 63 61 6c 6c 20  ct unix_syscall 
dd0a0 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
dd0b0 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  zName;          
dd0c0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
dd0d0 20 73 79 74 65 6d 20 63 61 6c 6c 20 2a 2f 0a 20   sytem call */. 
dd0e0 20 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c   sqlite3_syscall
dd0f0 5f 70 74 72 20 70 43 75 72 72 65 6e 74 3b 20 2f  _ptr pCurrent; /
dd100 2a 20 43 75 72 72 65 6e 74 20 76 61 6c 75 65 20  * Current value 
dd110 6f 66 20 74 68 65 20 73 79 73 74 65 6d 20 63 61  of the system ca
dd120 6c 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ll */.  sqlite3_
dd130 73 79 73 63 61 6c 6c 5f 70 74 72 20 70 44 65 66  syscall_ptr pDef
dd140 61 75 6c 74 3b 20 2f 2a 20 44 65 66 61 75 6c 74  ault; /* Default
dd150 20 76 61 6c 75 65 20 2a 2f 0a 7d 20 61 53 79 73   value */.} aSys
dd160 63 61 6c 6c 5b 5d 20 3d 20 7b 0a 20 20 7b 20 22  call[] = {.  { "
dd170 6f 70 65 6e 22 2c 20 20 20 20 20 20 20 20 20 28  open",         (
dd180 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f  sqlite3_syscall_
dd190 70 74 72 29 70 6f 73 69 78 4f 70 65 6e 2c 20 20  ptr)posixOpen,  
dd1a0 30 20 20 7d 2c 0a 23 64 65 66 69 6e 65 20 6f 73  0  },.#define os
dd1b0 4f 70 65 6e 20 20 20 20 20 20 28 28 69 6e 74 28  Open      ((int(
dd1c0 2a 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  *)(const char*,i
dd1d0 6e 74 2c 69 6e 74 29 29 61 53 79 73 63 61 6c 6c  nt,int))aSyscall
dd1e0 5b 30 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20  [0].pCurrent).. 
dd1f0 20 7b 20 22 63 6c 6f 73 65 22 2c 20 20 20 20 20   { "close",     
dd200 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63     (sqlite3_sysc
dd210 61 6c 6c 5f 70 74 72 29 63 6c 6f 73 65 2c 20 20  all_ptr)close,  
dd220 20 20 20 20 30 20 20 7d 2c 0a 23 64 65 66 69 6e      0  },.#defin
dd230 65 20 6f 73 43 6c 6f 73 65 20 20 20 20 20 28 28  e osClose     ((
dd240 69 6e 74 28 2a 29 28 69 6e 74 29 29 61 53 79 73  int(*)(int))aSys
dd250 63 61 6c 6c 5b 31 5d 2e 70 43 75 72 72 65 6e 74  call[1].pCurrent
dd260 29 0a 0a 20 20 7b 20 22 61 63 63 65 73 73 22 2c  )..  { "access",
dd270 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f         (sqlite3_
dd280 73 79 73 63 61 6c 6c 5f 70 74 72 29 61 63 63 65  syscall_ptr)acce
dd290 73 73 2c 20 20 20 20 20 30 20 20 7d 2c 0a 23 64  ss,     0  },.#d
dd2a0 65 66 69 6e 65 20 6f 73 41 63 63 65 73 73 20 20  efine osAccess  
dd2b0 20 20 28 28 69 6e 74 28 2a 29 28 63 6f 6e 73 74    ((int(*)(const
dd2c0 20 63 68 61 72 2a 2c 69 6e 74 29 29 61 53 79 73   char*,int))aSys
dd2d0 63 61 6c 6c 5b 32 5d 2e 70 43 75 72 72 65 6e 74  call[2].pCurrent
dd2e0 29 0a 0a 20 20 7b 20 22 67 65 74 63 77 64 22 2c  )..  { "getcwd",
dd2f0 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f         (sqlite3_
dd300 73 79 73 63 61 6c 6c 5f 70 74 72 29 67 65 74 63  syscall_ptr)getc
dd310 77 64 2c 20 20 20 20 20 30 20 20 7d 2c 0a 23 64  wd,     0  },.#d
dd320 65 66 69 6e 65 20 6f 73 47 65 74 63 77 64 20 20  efine osGetcwd  
dd330 20 20 28 28 63 68 61 72 2a 28 2a 29 28 63 68 61    ((char*(*)(cha
dd340 72 2a 2c 73 69 7a 65 5f 74 29 29 61 53 79 73 63  r*,size_t))aSysc
dd350 61 6c 6c 5b 33 5d 2e 70 43 75 72 72 65 6e 74 29  all[3].pCurrent)
dd360 0a 0a 20 20 7b 20 22 73 74 61 74 22 2c 20 20 20  ..  { "stat",   
dd370 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73        (sqlite3_s
dd380 79 73 63 61 6c 6c 5f 70 74 72 29 73 74 61 74 2c  yscall_ptr)stat,
dd390 20 20 20 20 20 20 20 30 20 20 7d 2c 0a 23 64 65         0  },.#de
dd3a0 66 69 6e 65 20 6f 73 53 74 61 74 20 20 20 20 20  fine osStat     
dd3b0 20 28 28 69 6e 74 28 2a 29 28 63 6f 6e 73 74 20   ((int(*)(const 
dd3c0 63 68 61 72 2a 2c 73 74 72 75 63 74 20 73 74 61  char*,struct sta
dd3d0 74 2a 29 29 61 53 79 73 63 61 6c 6c 5b 34 5d 2e  t*))aSyscall[4].
dd3e0 70 43 75 72 72 65 6e 74 29 0a 0a 2f 2a 0a 2a 2a  pCurrent)../*.**
dd3f0 20 54 68 65 20 44 4a 47 50 50 20 63 6f 6d 70 69   The DJGPP compi
dd400 6c 65 72 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20  ler environment 
dd410 6c 6f 6f 6b 73 20 6d 6f 73 74 6c 79 20 6c 69 6b  looks mostly lik
dd420 65 20 55 6e 69 78 2c 20 62 75 74 20 69 74 0a 2a  e Unix, but it.*
dd430 2a 20 6c 61 63 6b 73 20 74 68 65 20 66 63 6e 74  * lacks the fcnt
dd440 6c 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 2e  l() system call.
dd450 20 20 53 6f 20 72 65 64 65 66 69 6e 65 20 66 63    So redefine fc
dd460 6e 74 6c 28 29 20 74 6f 20 62 65 20 73 6f 6d 65  ntl() to be some
dd470 74 68 69 6e 67 0a 2a 2a 20 74 68 61 74 20 61 6c  thing.** that al
dd480 77 61 79 73 20 73 75 63 63 65 65 64 73 2e 20 20  ways succeeds.  
dd490 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20  This means that 
dd4a0 6c 6f 63 6b 69 6e 67 20 64 6f 65 73 20 6e 6f 74  locking does not
dd4b0 20 6f 63 63 75 72 20 75 6e 64 65 72 0a 2a 2a 20   occur under.** 
dd4c0 44 4a 47 50 50 2e 20 20 42 75 74 20 69 74 20 69  DJGPP.  But it i
dd4d0 73 20 44 4f 53 20 2d 20 77 68 61 74 20 64 69 64  s DOS - what did
dd4e0 20 79 6f 75 20 65 78 70 65 63 74 3f 0a 2a 2f 0a   you expect?.*/.
dd4f0 23 69 66 64 65 66 20 5f 5f 44 4a 47 50 50 5f 5f  #ifdef __DJGPP__
dd500 0a 20 20 7b 20 22 66 73 74 61 74 22 2c 20 20 20  .  { "fstat",   
dd510 20 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20       0,         
dd520 20 20 20 20 20 20 20 20 30 20 20 7d 2c 0a 23 64          0  },.#d
dd530 65 66 69 6e 65 20 6f 73 46 73 74 61 74 28 61 2c  efine osFstat(a,
dd540 62 2c 63 29 20 20 20 20 30 0a 23 65 6c 73 65 20  b,c)    0.#else 
dd550 20 20 20 20 0a 20 20 7b 20 22 66 73 74 61 74 22      .  { "fstat"
dd560 2c 20 20 20 20 20 20 20 20 28 73 71 6c 69 74 65  ,        (sqlite
dd570 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 66 73  3_syscall_ptr)fs
dd580 74 61 74 2c 20 20 20 20 20 20 30 20 20 7d 2c 0a  tat,      0  },.
dd590 23 64 65 66 69 6e 65 20 6f 73 46 73 74 61 74 20  #define osFstat 
dd5a0 20 20 20 20 28 28 69 6e 74 28 2a 29 28 69 6e 74      ((int(*)(int
dd5b0 2c 73 74 72 75 63 74 20 73 74 61 74 2a 29 29 61  ,struct stat*))a
dd5c0 53 79 73 63 61 6c 6c 5b 35 5d 2e 70 43 75 72 72  Syscall[5].pCurr
dd5d0 65 6e 74 29 0a 23 65 6e 64 69 66 0a 0a 20 20 7b  ent).#endif..  {
dd5e0 20 22 66 74 72 75 6e 63 61 74 65 22 2c 20 20 20   "ftruncate",   
dd5f0 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c   (sqlite3_syscal
dd600 6c 5f 70 74 72 29 66 74 72 75 6e 63 61 74 65 2c  l_ptr)ftruncate,
dd610 20 20 30 20 20 7d 2c 0a 23 64 65 66 69 6e 65 20    0  },.#define 
dd620 6f 73 46 74 72 75 6e 63 61 74 65 20 28 28 69 6e  osFtruncate ((in
dd630 74 28 2a 29 28 69 6e 74 2c 6f 66 66 5f 74 29 29  t(*)(int,off_t))
dd640 61 53 79 73 63 61 6c 6c 5b 36 5d 2e 70 43 75 72  aSyscall[6].pCur
dd650 72 65 6e 74 29 0a 0a 20 20 7b 20 22 66 63 6e 74  rent)..  { "fcnt
dd660 6c 22 2c 20 20 20 20 20 20 20 20 28 73 71 6c 69  l",        (sqli
dd670 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29  te3_syscall_ptr)
dd680 66 63 6e 74 6c 2c 20 20 20 20 20 20 30 20 20 7d  fcntl,      0  }
dd690 2c 0a 23 64 65 66 69 6e 65 20 6f 73 46 63 6e 74  ,.#define osFcnt
dd6a0 6c 20 20 20 20 20 28 28 69 6e 74 28 2a 29 28 69  l     ((int(*)(i
dd6b0 6e 74 2c 69 6e 74 2c 2e 2e 2e 29 29 61 53 79 73  nt,int,...))aSys
dd6c0 63 61 6c 6c 5b 37 5d 2e 70 43 75 72 72 65 6e 74  call[7].pCurrent
dd6d0 29 0a 0a 20 20 7b 20 22 72 65 61 64 22 2c 20 20  )..  { "read",  
dd6e0 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f         (sqlite3_
dd6f0 73 79 73 63 61 6c 6c 5f 70 74 72 29 72 65 61 64  syscall_ptr)read
dd700 2c 20 20 20 20 20 20 20 30 20 20 7d 2c 0a 23 64  ,       0  },.#d
dd710 65 66 69 6e 65 20 6f 73 52 65 61 64 20 20 20 20  efine osRead    
dd720 20 20 28 28 73 73 69 7a 65 5f 74 28 2a 29 28 69    ((ssize_t(*)(i
dd730 6e 74 2c 76 6f 69 64 2a 2c 73 69 7a 65 5f 74 29  nt,void*,size_t)
dd740 29 61 53 79 73 63 61 6c 6c 5b 38 5d 2e 70 43 75  )aSyscall[8].pCu
dd750 72 72 65 6e 74 29 0a 0a 23 69 66 20 64 65 66 69  rrent)..#if defi
dd760 6e 65 64 28 55 53 45 5f 50 52 45 41 44 29 20 7c  ned(USE_PREAD) |
dd770 7c 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  | SQLITE_ENABLE_
dd780 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20  LOCKING_STYLE.  
dd790 7b 20 22 70 72 65 61 64 22 2c 20 20 20 20 20 20  { "pread",      
dd7a0 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61    (sqlite3_sysca
dd7b0 6c 6c 5f 70 74 72 29 70 72 65 61 64 2c 20 20 20  ll_ptr)pread,   
dd7c0 20 20 20 30 20 20 7d 2c 0a 23 65 6c 73 65 0a 20     0  },.#else. 
dd7d0 20 7b 20 22 70 72 65 61 64 22 2c 20 20 20 20 20   { "pread",     
dd7e0 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63     (sqlite3_sysc
dd7f0 61 6c 6c 5f 70 74 72 29 30 2c 20 20 20 20 20 20  all_ptr)0,      
dd800 20 20 20 20 30 20 20 7d 2c 0a 23 65 6e 64 69 66      0  },.#endif
dd810 0a 23 64 65 66 69 6e 65 20 6f 73 50 72 65 61 64  .#define osPread
dd820 20 20 20 20 20 28 28 73 73 69 7a 65 5f 74 28 2a       ((ssize_t(*
dd830 29 28 69 6e 74 2c 76 6f 69 64 2a 2c 73 69 7a 65  )(int,void*,size
dd840 5f 74 2c 6f 66 66 5f 74 29 29 61 53 79 73 63 61  _t,off_t))aSysca
dd850 6c 6c 5b 39 5d 2e 70 43 75 72 72 65 6e 74 29 0a  ll[9].pCurrent).
dd860 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55 53 45  .#if defined(USE
dd870 5f 50 52 45 41 44 36 34 29 0a 20 20 7b 20 22 70  _PREAD64).  { "p
dd880 72 65 61 64 36 34 22 2c 20 20 20 20 20 20 28 73  read64",      (s
dd890 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70  qlite3_syscall_p
dd8a0 74 72 29 70 72 65 61 64 36 34 2c 20 20 20 20 30  tr)pread64,    0
dd8b0 20 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22    },.#else.  { "
dd8c0 70 72 65 61 64 36 34 22 2c 20 20 20 20 20 20 28  pread64",      (
dd8d0 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f  sqlite3_syscall_
dd8e0 70 74 72 29 30 2c 20 20 20 20 20 20 20 20 20 20  ptr)0,          
dd8f0 30 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 64 65  0  },.#endif.#de
dd900 66 69 6e 65 20 6f 73 50 72 65 61 64 36 34 20 20  fine osPread64  
dd910 20 28 28 73 73 69 7a 65 5f 74 28 2a 29 28 69 6e   ((ssize_t(*)(in
dd920 74 2c 76 6f 69 64 2a 2c 73 69 7a 65 5f 74 2c 6f  t,void*,size_t,o
dd930 66 66 5f 74 29 29 61 53 79 73 63 61 6c 6c 5b 31  ff_t))aSyscall[1
dd940 30 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20  0].pCurrent)..  
dd950 7b 20 22 77 72 69 74 65 22 2c 20 20 20 20 20 20  { "write",      
dd960 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61    (sqlite3_sysca
dd970 6c 6c 5f 70 74 72 29 77 72 69 74 65 2c 20 20 20  ll_ptr)write,   
dd980 20 20 20 30 20 20 7d 2c 0a 23 64 65 66 69 6e 65     0  },.#define
dd990 20 6f 73 57 72 69 74 65 20 20 20 20 20 28 28 73   osWrite     ((s
dd9a0 73 69 7a 65 5f 74 28 2a 29 28 69 6e 74 2c 63 6f  size_t(*)(int,co
dd9b0 6e 73 74 20 76 6f 69 64 2a 2c 73 69 7a 65 5f 74  nst void*,size_t
dd9c0 29 29 61 53 79 73 63 61 6c 6c 5b 31 31 5d 2e 70  ))aSyscall[11].p
dd9d0 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 64 65  Current)..#if de
dd9e0 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 29  fined(USE_PREAD)
dd9f0 20 7c 7c 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   || SQLITE_ENABL
dda00 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
dda10 20 20 7b 20 22 70 77 72 69 74 65 22 2c 20 20 20    { "pwrite",   
dda20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73      (sqlite3_sys
dda30 63 61 6c 6c 5f 70 74 72 29 70 77 72 69 74 65 2c  call_ptr)pwrite,
dda40 20 20 20 20 20 30 20 20 7d 2c 0a 23 65 6c 73 65       0  },.#else
dda50 0a 20 20 7b 20 22 70 77 72 69 74 65 22 2c 20 20  .  { "pwrite",  
dda60 20 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79       (sqlite3_sy
dda70 73 63 61 6c 6c 5f 70 74 72 29 30 2c 20 20 20 20  scall_ptr)0,    
dda80 20 20 20 20 20 20 30 20 20 7d 2c 0a 23 65 6e 64        0  },.#end
dda90 69 66 0a 23 64 65 66 69 6e 65 20 6f 73 50 77 72  if.#define osPwr
ddaa0 69 74 65 20 20 20 20 28 28 73 73 69 7a 65 5f 74  ite    ((ssize_t
ddab0 28 2a 29 28 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  (*)(int,const vo
ddac0 69 64 2a 2c 73 69 7a 65 5f 74 2c 6f 66 66 5f 74  id*,size_t,off_t
ddad0 29 29 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ))\.            
ddae0 20 20 20 20 20 20 20 20 61 53 79 73 63 61 6c 6c          aSyscall
ddaf0 5b 31 32 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a  [12].pCurrent)..
ddb00 23 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f  #if defined(USE_
ddb10 50 52 45 41 44 36 34 29 0a 20 20 7b 20 22 70 77  PREAD64).  { "pw
ddb20 72 69 74 65 36 34 22 2c 20 20 20 20 20 28 73 71  rite64",     (sq
ddb30 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74  lite3_syscall_pt
ddb40 72 29 70 77 72 69 74 65 36 34 2c 20 20 20 30 20  r)pwrite64,   0 
ddb50 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 70   },.#else.  { "p
ddb60 77 72 69 74 65 36 34 22 2c 20 20 20 20 20 28 73  write64",     (s
ddb70 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70  qlite3_syscall_p
ddb80 74 72 29 30 2c 20 20 20 20 20 20 20 20 20 20 30  tr)0,          0
ddb90 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 64 65 66    },.#endif.#def
ddba0 69 6e 65 20 6f 73 50 77 72 69 74 65 36 34 20 20  ine osPwrite64  
ddbb0 28 28 73 73 69 7a 65 5f 74 28 2a 29 28 69 6e 74  ((ssize_t(*)(int
ddbc0 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 73 69 7a  ,const void*,siz
ddbd0 65 5f 74 2c 6f 66 66 5f 74 29 29 5c 0a 20 20 20  e_t,off_t))\.   
ddbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ddbf0 20 61 53 79 73 63 61 6c 6c 5b 31 33 5d 2e 70 43   aSyscall[13].pC
ddc00 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 53 51 4c  urrent)..#if SQL
ddc10 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
ddc20 4e 47 5f 53 54 59 4c 45 0a 20 20 7b 20 22 66 63  NG_STYLE.  { "fc
ddc30 68 6d 6f 64 22 2c 20 20 20 20 20 20 20 28 73 71  hmod",       (sq
ddc40 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74  lite3_syscall_pt
ddc50 72 29 66 63 68 6d 6f 64 2c 20 20 20 20 20 30 20  r)fchmod,     0 
ddc60 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 66   },.#else.  { "f
ddc70 63 68 6d 6f 64 22 2c 20 20 20 20 20 20 20 28 73  chmod",       (s
ddc80 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70  qlite3_syscall_p
ddc90 74 72 29 30 2c 20 20 20 20 20 20 20 20 20 20 30  tr)0,          0
ddca0 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 64 65 66    },.#endif.#def
ddcb0 69 6e 65 20 6f 73 46 63 68 6d 6f 64 20 20 20 20  ine osFchmod    
ddcc0 28 28 69 6e 74 28 2a 29 28 69 6e 74 2c 6d 6f 64  ((int(*)(int,mod
ddcd0 65 5f 74 29 29 61 53 79 73 63 61 6c 6c 5b 31 34  e_t))aSyscall[14
ddce0 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66  ].pCurrent)..#if
ddcf0 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f 50 4f   defined(HAVE_PO
ddd00 53 49 58 5f 46 41 4c 4c 4f 43 41 54 45 29 20 26  SIX_FALLOCATE) &
ddd10 26 20 48 41 56 45 5f 50 4f 53 49 58 5f 46 41 4c  & HAVE_POSIX_FAL
ddd20 4c 4f 43 41 54 45 0a 20 20 7b 20 22 66 61 6c 6c  LOCATE.  { "fall
ddd30 6f 63 61 74 65 22 2c 20 20 20 20 28 73 71 6c 69  ocate",    (sqli
ddd40 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29  te3_syscall_ptr)
ddd50 70 6f 73 69 78 5f 66 61 6c 6c 6f 63 61 74 65 2c  posix_fallocate,
ddd60 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b    0 },.#else.  {
ddd70 20 22 66 61 6c 6c 6f 63 61 74 65 22 2c 20 20 20   "fallocate",   
ddd80 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c   (sqlite3_syscal
ddd90 6c 5f 70 74 72 29 30 2c 20 20 20 20 20 20 20 20  l_ptr)0,        
ddda0 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e          0 },.#en
dddb0 64 69 66 0a 23 64 65 66 69 6e 65 20 6f 73 46 61  dif.#define osFa
dddc0 6c 6c 6f 63 61 74 65 20 28 28 69 6e 74 28 2a 29  llocate ((int(*)
dddd0 28 69 6e 74 2c 6f 66 66 5f 74 2c 6f 66 66 5f 74  (int,off_t,off_t
ddde0 29 29 61 53 79 73 63 61 6c 6c 5b 31 35 5d 2e 70  ))aSyscall[15].p
dddf0 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 75  Current)..  { "u
dde00 6e 6c 69 6e 6b 22 2c 20 20 20 20 20 20 20 28 73  nlink",       (s
dde10 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70  qlite3_syscall_p
dde20 74 72 29 75 6e 6c 69 6e 6b 2c 20 20 20 20 20 20  tr)unlink,      
dde30 20 20 20 20 20 30 20 7d 2c 0a 23 64 65 66 69 6e       0 },.#defin
dde40 65 20 6f 73 55 6e 6c 69 6e 6b 20 20 20 20 28 28  e osUnlink    ((
dde50 69 6e 74 28 2a 29 28 63 6f 6e 73 74 20 63 68 61  int(*)(const cha
dde60 72 2a 29 29 61 53 79 73 63 61 6c 6c 5b 31 36 5d  r*))aSyscall[16]
dde70 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20  .pCurrent)..  { 
dde80 22 6f 70 65 6e 44 69 72 65 63 74 6f 72 79 22 2c  "openDirectory",
dde90 20 20 20 20 28 73 71 6c 69 74 65 33 5f 73 79 73      (sqlite3_sys
ddea0 63 61 6c 6c 5f 70 74 72 29 6f 70 65 6e 44 69 72  call_ptr)openDir
ddeb0 65 63 74 6f 72 79 2c 20 20 20 20 20 20 30 20 7d  ectory,      0 }
ddec0 2c 0a 23 64 65 66 69 6e 65 20 6f 73 4f 70 65 6e  ,.#define osOpen
dded0 44 69 72 65 63 74 6f 72 79 20 28 28 69 6e 74 28  Directory ((int(
ddee0 2a 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  *)(const char*,i
ddef0 6e 74 2a 29 29 61 53 79 73 63 61 6c 6c 5b 31 37  nt*))aSyscall[17
ddf00 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b  ].pCurrent)..  {
ddf10 20 22 6d 6b 64 69 72 22 2c 20 20 20 20 20 20 20   "mkdir",       
ddf20 20 28 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c   (sqlite3_syscal
ddf30 6c 5f 70 74 72 29 6d 6b 64 69 72 2c 20 20 20 20  l_ptr)mkdir,    
ddf40 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 64 65 66         0 },.#def
ddf50 69 6e 65 20 6f 73 4d 6b 64 69 72 20 20 20 20 20  ine osMkdir     
ddf60 28 28 69 6e 74 28 2a 29 28 63 6f 6e 73 74 20 63  ((int(*)(const c
ddf70 68 61 72 2a 2c 6d 6f 64 65 5f 74 29 29 61 53 79  har*,mode_t))aSy
ddf80 73 63 61 6c 6c 5b 31 38 5d 2e 70 43 75 72 72 65  scall[18].pCurre
ddf90 6e 74 29 0a 0a 20 20 7b 20 22 72 6d 64 69 72 22  nt)..  { "rmdir"
ddfa0 2c 20 20 20 20 20 20 20 20 28 73 71 6c 69 74 65  ,        (sqlite
ddfb0 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 72 6d  3_syscall_ptr)rm
ddfc0 64 69 72 2c 20 20 20 20 20 20 20 20 20 20 20 30  dir,           0
ddfd0 20 7d 2c 0a 23 64 65 66 69 6e 65 20 6f 73 52 6d   },.#define osRm
ddfe0 64 69 72 20 20 20 20 20 28 28 69 6e 74 28 2a 29  dir     ((int(*)
ddff0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 61 53  (const char*))aS
de000 79 73 63 61 6c 6c 5b 31 39 5d 2e 70 43 75 72 72  yscall[19].pCurr
de010 65 6e 74 29 0a 0a 20 20 7b 20 22 66 63 68 6f 77  ent)..  { "fchow
de020 6e 22 2c 20 20 20 20 20 20 20 28 73 71 6c 69 74  n",       (sqlit
de030 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 29 70  e3_syscall_ptr)p
de040 6f 73 69 78 46 63 68 6f 77 6e 2c 20 20 20 20 20  osixFchown,     
de050 30 20 7d 2c 0a 23 64 65 66 69 6e 65 20 6f 73 46  0 },.#define osF
de060 63 68 6f 77 6e 20 20 20 20 28 28 69 6e 74 28 2a  chown    ((int(*
de070 29 28 69 6e 74 2c 75 69 64 5f 74 2c 67 69 64 5f  )(int,uid_t,gid_
de080 74 29 29 61 53 79 73 63 61 6c 6c 5b 32 30 5d 2e  t))aSyscall[20].
de090 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22  pCurrent)..  { "
de0a0 75 6d 61 73 6b 22 2c 20 20 20 20 20 20 20 20 28  umask",        (
de0b0 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f  sqlite3_syscall_
de0c0 70 74 72 29 75 6d 61 73 6b 2c 20 20 20 20 20 20  ptr)umask,      
de0d0 20 20 20 20 20 30 20 7d 2c 0a 23 64 65 66 69 6e       0 },.#defin
de0e0 65 20 6f 73 55 6d 61 73 6b 20 20 20 20 20 28 28  e osUmask     ((
de0f0 6d 6f 64 65 5f 74 28 2a 29 28 6d 6f 64 65 5f 74  mode_t(*)(mode_t
de100 29 29 61 53 79 73 63 61 6c 6c 5b 32 31 5d 2e 70  ))aSyscall[21].p
de110 43 75 72 72 65 6e 74 29 0a 0a 7d 3b 20 2f 2a 20  Current)..}; /* 
de120 45 6e 64 20 6f 66 20 74 68 65 20 6f 76 65 72 72  End of the overr
de130 69 64 65 61 62 6c 65 20 73 79 73 74 65 6d 20 63  ideable system c
de140 61 6c 6c 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  alls */../*.** T
de150 68 69 73 20 69 73 20 74 68 65 20 78 53 65 74 53  his is the xSetS
de160 79 73 74 65 6d 43 61 6c 6c 28 29 20 6d 65 74 68  ystemCall() meth
de170 6f 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66  od of sqlite3_vf
de180 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65  s for all of the
de190 0a 2a 2a 20 22 75 6e 69 78 22 20 56 46 53 65 73  .** "unix" VFSes
de1a0 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
de1b0 5f 4f 4b 20 6f 70 6f 6e 20 73 75 63 63 65 73 73  _OK opon success
de1c0 66 75 6c 6c 79 20 75 70 64 61 74 69 6e 67 20 74  fully updating t
de1d0 68 65 0a 2a 2a 20 73 79 73 74 65 6d 20 63 61 6c  he.** system cal
de1e0 6c 20 70 6f 69 6e 74 65 72 2c 20 6f 72 20 53 51  l pointer, or SQ
de1f0 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 69 66  LITE_NOTFOUND if
de200 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e   there is no con
de210 66 69 67 75 72 61 62 6c 65 0a 2a 2a 20 73 79 73  figurable.** sys
de220 74 65 6d 20 63 61 6c 6c 20 6e 61 6d 65 64 20 7a  tem call named z
de230 4e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Name..*/.static 
de240 69 6e 74 20 75 6e 69 78 53 65 74 53 79 73 74 65  int unixSetSyste
de250 6d 43 61 6c 6c 28 0a 20 20 73 71 6c 69 74 65 33  mCall(.  sqlite3
de260 5f 76 66 73 20 2a 70 4e 6f 74 55 73 65 64 2c 20  _vfs *pNotUsed, 
de270 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46         /* The VF
de280 53 20 70 6f 69 6e 74 65 72 2e 20 20 4e 6f 74 20  S pointer.  Not 
de290 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  used */.  const 
de2a0 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20  char *zName,    
de2b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
de2c0 6f 66 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 74  of system call t
de2d0 6f 20 6f 76 65 72 72 69 64 65 20 2a 2f 0a 20 20  o override */.  
de2e0 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f  sqlite3_syscall_
de2f0 70 74 72 20 70 4e 65 77 46 75 6e 63 20 20 2f 2a  ptr pNewFunc  /*
de300 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20   Pointer to new 
de310 73 79 73 74 65 6d 20 63 61 6c 6c 20 76 61 6c 75  system call valu
de320 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e  e */.){.  unsign
de330 65 64 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  ed int i;.  int 
de340 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 46  rc = SQLITE_NOTF
de350 4f 55 4e 44 3b 0a 0a 20 20 55 4e 55 53 45 44 5f  OUND;..  UNUSED_
de360 50 41 52 41 4d 45 54 45 52 28 70 4e 6f 74 55 73  PARAMETER(pNotUs
de370 65 64 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65  ed);.  if( zName
de380 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  ==0 ){.    /* If
de390 20 6e 6f 20 7a 4e 61 6d 65 20 69 73 20 67 69 76   no zName is giv
de3a0 65 6e 2c 20 72 65 73 74 6f 72 65 20 61 6c 6c 20  en, restore all 
de3b0 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 74 6f 20  system calls to 
de3c0 74 68 65 69 72 20 64 65 66 61 75 6c 74 0a 20 20  their default.  
de3d0 20 20 2a 2a 20 73 65 74 74 69 6e 67 73 20 61 6e    ** settings an
de3e0 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 0a 20 20  d return NULL.  
de3f0 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51    */.    rc = SQ
de400 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 66 6f 72  LITE_OK;.    for
de410 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61  (i=0; i<sizeof(a
de420 53 79 73 63 61 6c 6c 29 2f 73 69 7a 65 6f 66 28  Syscall)/sizeof(
de430 61 53 79 73 63 61 6c 6c 5b 30 5d 29 3b 20 69 2b  aSyscall[0]); i+
de440 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 53  +){.      if( aS
de450 79 73 63 61 6c 6c 5b 69 5d 2e 70 44 65 66 61 75  yscall[i].pDefau
de460 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 53  lt ){.        aS
de470 79 73 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65  yscall[i].pCurre
de480 6e 74 20 3d 20 61 53 79 73 63 61 6c 6c 5b 69 5d  nt = aSyscall[i]
de490 2e 70 44 65 66 61 75 6c 74 3b 0a 20 20 20 20 20  .pDefault;.     
de4a0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
de4b0 7b 0a 20 20 20 20 2f 2a 20 49 66 20 7a 4e 61 6d  {.    /* If zNam
de4c0 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20  e is specified, 
de4d0 6f 70 65 72 61 74 65 20 6f 6e 20 6f 6e 6c 79 20  operate on only 
de4e0 74 68 65 20 6f 6e 65 20 73 79 73 74 65 6d 20 63  the one system c
de4f0 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 70 65 63 69  all.    ** speci
de500 66 69 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  fied..    */.   
de510 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65   for(i=0; i<size
de520 6f 66 28 61 53 79 73 63 61 6c 6c 29 2f 73 69 7a  of(aSyscall)/siz
de530 65 6f 66 28 61 53 79 73 63 61 6c 6c 5b 30 5d 29  eof(aSyscall[0])
de540 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
de550 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20  ( strcmp(zName, 
de560 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d  aSyscall[i].zNam
de570 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  e)==0 ){.       
de580 20 69 66 28 20 61 53 79 73 63 61 6c 6c 5b 69 5d   if( aSyscall[i]
de590 2e 70 44 65 66 61 75 6c 74 3d 3d 30 20 29 7b 0a  .pDefault==0 ){.
de5a0 20 20 20 20 20 20 20 20 20 20 61 53 79 73 63 61            aSysca
de5b0 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 6c 74 20 3d  ll[i].pDefault =
de5c0 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 43 75   aSyscall[i].pCu
de5d0 72 72 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 7d  rrent;.        }
de5e0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
de5f0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  LITE_OK;.       
de600 20 69 66 28 20 70 4e 65 77 46 75 6e 63 3d 3d 30   if( pNewFunc==0
de610 20 29 20 70 4e 65 77 46 75 6e 63 20 3d 20 61 53   ) pNewFunc = aS
de620 79 73 63 61 6c 6c 5b 69 5d 2e 70 44 65 66 61 75  yscall[i].pDefau
de630 6c 74 3b 0a 20 20 20 20 20 20 20 20 61 53 79 73  lt;.        aSys
de640 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 6e 74  call[i].pCurrent
de650 20 3d 20 70 4e 65 77 46 75 6e 63 3b 0a 20 20 20   = pNewFunc;.   
de660 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
de670 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
de680 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
de690 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76  .** Return the v
de6a0 61 6c 75 65 20 6f 66 20 61 20 73 79 73 74 65 6d  alue of a system
de6b0 20 63 61 6c 6c 2e 20 20 52 65 74 75 72 6e 20 4e   call.  Return N
de6c0 55 4c 4c 20 69 66 20 7a 4e 61 6d 65 20 69 73 20  ULL if zName is 
de6d0 6e 6f 74 20 61 0a 2a 2a 20 72 65 63 6f 67 6e 69  not a.** recogni
de6e0 7a 65 64 20 73 79 73 74 65 6d 20 63 61 6c 6c 20  zed system call 
de6f0 6e 61 6d 65 2e 20 20 4e 55 4c 4c 20 69 73 20 61  name.  NULL is a
de700 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 20  lso returned if 
de710 74 68 65 20 73 79 73 74 65 6d 20 63 61 6c 6c 0a  the system call.
de720 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  ** is currently 
de730 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74  undefined..*/.st
de740 61 74 69 63 20 73 71 6c 69 74 65 33 5f 73 79 73  atic sqlite3_sys
de750 63 61 6c 6c 5f 70 74 72 20 75 6e 69 78 47 65 74  call_ptr unixGet
de760 53 79 73 74 65 6d 43 61 6c 6c 28 0a 20 20 73 71  SystemCall(.  sq
de770 6c 69 74 65 33 5f 76 66 73 20 2a 70 4e 6f 74 55  lite3_vfs *pNotU
de780 73 65 64 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  sed,.  const cha
de790 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20 75 6e  r *zName.){.  un
de7a0 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 0a 20  signed int i;.. 
de7b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
de7c0 52 28 70 4e 6f 74 55 73 65 64 29 3b 0a 20 20 66  R(pNotUsed);.  f
de7d0 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66  or(i=0; i<sizeof
de7e0 28 61 53 79 73 63 61 6c 6c 29 2f 73 69 7a 65 6f  (aSyscall)/sizeo
de7f0 66 28 61 53 79 73 63 61 6c 6c 5b 30 5d 29 3b 20  f(aSyscall[0]); 
de800 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 74  i++){.    if( st
de810 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 61 53 79 73  rcmp(zName, aSys
de820 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d  call[i].zName)==
de830 30 20 29 20 72 65 74 75 72 6e 20 61 53 79 73 63  0 ) return aSysc
de840 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 6e 74 3b  all[i].pCurrent;
de850 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
de860 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
de870 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
de880 20 66 69 72 73 74 20 73 79 73 74 65 6d 20 63 61   first system ca
de890 6c 6c 20 61 66 74 65 72 20 7a 4e 61 6d 65 2e 20  ll after zName. 
de8a0 20 49 66 20 7a 4e 61 6d 65 3d 3d 4e 55 4c 4c 0a   If zName==NULL.
de8b0 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74  ** then return t
de8c0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66  he name of the f
de8d0 69 72 73 74 20 73 79 73 74 65 6d 20 63 61 6c 6c  irst system call
de8e0 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69  .  Return NULL i
de8f0 66 20 7a 4e 61 6d 65 0a 2a 2a 20 69 73 20 74 68  f zName.** is th
de900 65 20 6c 61 73 74 20 73 79 73 74 65 6d 20 63 61  e last system ca
de910 6c 6c 20 6f 72 20 69 66 20 7a 4e 61 6d 65 20 69  ll or if zName i
de920 73 20 6e 6f 74 20 74 68 65 20 6e 61 6d 65 20 6f  s not the name o
de930 66 20 61 20 76 61 6c 69 64 0a 2a 2a 20 73 79 73  f a valid.** sys
de940 74 65 6d 20 63 61 6c 6c 2e 0a 2a 2f 0a 73 74 61  tem call..*/.sta
de950 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
de960 75 6e 69 78 4e 65 78 74 53 79 73 74 65 6d 43 61  unixNextSystemCa
de970 6c 6c 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ll(sqlite3_vfs *
de980 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  p, const char *z
de990 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 20 3d  Name){.  int i =
de9a0 20 2d 31 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50   -1;..  UNUSED_P
de9b0 41 52 41 4d 45 54 45 52 28 70 29 3b 0a 20 20 69  ARAMETER(p);.  i
de9c0 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20  f( zName ){.    
de9d0 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79  for(i=0; i<Array
de9e0 53 69 7a 65 28 61 53 79 73 63 61 6c 6c 29 2d 31  Size(aSyscall)-1
de9f0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
dea00 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 20  ( strcmp(zName, 
dea10 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d  aSyscall[i].zNam
dea20 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  e)==0 ) break;. 
dea30 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69     }.  }.  for(i
dea40 2b 2b 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  ++; i<ArraySize(
dea50 61 53 79 73 63 61 6c 6c 29 3b 20 69 2b 2b 29 7b  aSyscall); i++){
dea60 0a 20 20 20 20 69 66 28 20 61 53 79 73 63 61 6c  .    if( aSyscal
dea70 6c 5b 69 5d 2e 70 43 75 72 72 65 6e 74 21 3d 30  l[i].pCurrent!=0
dea80 20 29 20 72 65 74 75 72 6e 20 61 53 79 73 63 61   ) return aSysca
dea90 6c 6c 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d  ll[i].zName;.  }
deaa0 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
deab0 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 6f 70 65  /*.** Invoke ope
deac0 6e 28 29 2e 20 20 44 6f 20 73 6f 20 6d 75 6c 74  n().  Do so mult
dead0 69 70 6c 65 20 74 69 6d 65 73 2c 20 75 6e 74 69  iple times, unti
deae0 6c 20 69 74 20 65 69 74 68 65 72 20 73 75 63 63  l it either succ
deaf0 65 65 64 73 20 6f 72 0a 2a 2a 20 66 61 69 6c 73  eeds or.** fails
deb00 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e   for some reason
deb10 20 6f 74 68 65 72 20 74 68 61 6e 20 45 49 4e 54   other than EINT
deb20 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  R..**.** If the 
deb30 66 69 6c 65 20 63 72 65 61 74 69 6f 6e 20 6d 6f  file creation mo
deb40 64 65 20 22 6d 22 20 69 73 20 30 20 74 68 65 6e  de "m" is 0 then
deb50 20 73 65 74 20 69 74 20 74 6f 20 74 68 65 20 64   set it to the d
deb60 65 66 61 75 6c 74 20 66 6f 72 0a 2a 2a 20 53 51  efault for.** SQ
deb70 4c 69 74 65 2e 20 20 54 68 65 20 64 65 66 61 75  Lite.  The defau
deb80 6c 74 20 69 73 20 53 51 4c 49 54 45 5f 44 45 46  lt is SQLITE_DEF
deb90 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53  AULT_FILE_PERMIS
deba0 53 49 4f 4e 53 20 28 6e 6f 72 6d 61 6c 6c 79 0a  SIONS (normally.
debb0 2a 2a 20 30 36 34 34 29 20 61 73 20 6d 6f 64 69  ** 0644) as modi
debc0 66 69 65 64 20 62 79 20 74 68 65 20 73 79 73 74  fied by the syst
debd0 65 6d 20 75 6d 61 73 6b 2e 20 20 49 66 20 6d 20  em umask.  If m 
debe0 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 0a 2a  is not 0, then.*
debf0 2a 20 6d 61 6b 65 20 74 68 65 20 66 69 6c 65 20  * make the file 
dec00 63 72 65 61 74 69 6f 6e 20 6d 6f 64 65 20 62 65  creation mode be
dec10 20 65 78 61 63 74 6c 79 20 6d 20 69 67 6e 6f 72   exactly m ignor
dec20 69 6e 67 20 74 68 65 20 75 6d 61 73 6b 2e 0a 2a  ing the umask..*
dec30 2a 0a 2a 2a 20 54 68 65 20 6d 20 70 61 72 61 6d  *.** The m param
dec40 65 74 65 72 20 77 69 6c 6c 20 62 65 20 6e 6f 6e  eter will be non
dec50 2d 7a 65 72 6f 20 6f 6e 6c 79 20 77 68 65 6e 20  -zero only when 
dec60 63 72 65 61 74 69 6e 67 20 2d 77 61 6c 2c 20 2d  creating -wal, -
dec70 6a 6f 75 72 6e 61 6c 2c 0a 2a 2a 20 61 6e 64 20  journal,.** and 
dec80 2d 73 68 6d 20 66 69 6c 65 73 2e 20 20 57 65 20  -shm files.  We 
dec90 77 61 6e 74 20 74 68 6f 73 65 20 66 69 6c 65 73  want those files
deca0 20 74 6f 20 68 61 76 65 20 2a 65 78 61 63 74 6c   to have *exactl
decb0 79 2a 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 70  y* the same.** p
decc0 65 72 6d 69 73 73 69 6f 6e 73 20 61 73 20 74 68  ermissions as th
decd0 65 69 72 20 6f 72 69 67 69 6e 61 6c 20 64 61 74  eir original dat
dece0 61 62 61 73 65 2c 20 75 6e 61 64 75 6c 74 65 72  abase, unadulter
decf0 61 74 65 64 20 62 79 20 74 68 65 20 75 6d 61 73  ated by the umas
ded00 6b 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 20 77 61  k..** In that wa
ded10 79 2c 20 69 66 20 61 20 64 61 74 61 62 61 73 65  y, if a database
ded20 20 66 69 6c 65 20 69 73 20 2d 72 77 2d 72 77 2d   file is -rw-rw-
ded30 72 77 20 6f 72 20 2d 72 77 2d 72 77 2d 72 2d 2c  rw or -rw-rw-r-,
ded40 20 61 6e 64 20 61 0a 2a 2a 20 74 72 61 6e 73 61   and a.** transa
ded50 63 74 69 6f 6e 20 63 72 61 73 68 65 73 20 61 6e  ction crashes an
ded60 64 20 6c 65 61 76 65 73 20 62 65 68 69 6e 64 20  d leaves behind 
ded70 68 6f 74 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68  hot journals, th
ded80 65 6e 20 61 6e 79 0a 2a 2a 20 70 72 6f 63 65 73  en any.** proces
ded90 73 20 74 68 61 74 20 69 73 20 61 62 6c 65 20 74  s that is able t
deda0 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 64  o write to the d
dedb0 61 74 61 62 61 73 65 20 77 69 6c 6c 20 61 6c 73  atabase will als
dedc0 6f 20 62 65 20 61 62 6c 65 20 74 6f 0a 2a 2a 20  o be able to.** 
dedd0 72 65 63 6f 76 65 72 20 74 68 65 20 68 6f 74 20  recover the hot 
dede0 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2f 0a 73 74 61  journals..*/.sta
dedf0 74 69 63 20 69 6e 74 20 72 6f 62 75 73 74 5f 6f  tic int robust_o
dee00 70 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  pen(const char *
dee10 7a 2c 20 69 6e 74 20 66 2c 20 6d 6f 64 65 5f 74  z, int f, mode_t
dee20 20 6d 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20   m){.  int fd;. 
dee30 20 6d 6f 64 65 5f 74 20 6d 32 3b 0a 20 20 6d 6f   mode_t m2;.  mo
dee40 64 65 5f 74 20 6f 72 69 67 4d 20 3d 20 30 3b 0a  de_t origM = 0;.
dee50 20 20 69 66 28 20 6d 3d 3d 30 20 29 7b 0a 20 20    if( m==0 ){.  
dee60 20 20 6d 32 20 3d 20 53 51 4c 49 54 45 5f 44 45    m2 = SQLITE_DE
dee70 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49  FAULT_FILE_PERMI
dee80 53 53 49 4f 4e 53 3b 0a 20 20 7d 65 6c 73 65 7b  SSIONS;.  }else{
dee90 0a 20 20 20 20 6d 32 20 3d 20 6d 3b 0a 20 20 20  .    m2 = m;.   
deea0 20 6f 72 69 67 4d 20 3d 20 6f 73 55 6d 61 73 6b   origM = osUmask
deeb0 28 30 29 3b 0a 20 20 7d 0a 20 20 64 6f 7b 0a 23  (0);.  }.  do{.#
deec0 69 66 20 64 65 66 69 6e 65 64 28 4f 5f 43 4c 4f  if defined(O_CLO
deed0 45 58 45 43 29 0a 20 20 20 20 66 64 20 3d 20 6f  EXEC).    fd = o
deee0 73 4f 70 65 6e 28 7a 2c 66 7c 4f 5f 43 4c 4f 45  sOpen(z,f|O_CLOE
deef0 58 45 43 2c 6d 32 29 3b 0a 23 65 6c 73 65 0a 20  XEC,m2);.#else. 
def00 20 20 20 66 64 20 3d 20 6f 73 4f 70 65 6e 28 7a     fd = osOpen(z
def10 2c 66 2c 6d 32 29 3b 0a 23 65 6e 64 69 66 0a 20  ,f,m2);.#endif. 
def20 20 7d 77 68 69 6c 65 28 20 66 64 3c 30 20 26 26   }while( fd<0 &&
def30 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20 29 3b   errno==EINTR );
def40 0a 20 20 69 66 28 20 6d 20 29 7b 0a 20 20 20 20  .  if( m ){.    
def50 6f 73 55 6d 61 73 6b 28 6f 72 69 67 4d 29 3b 0a  osUmask(origM);.
def60 20 20 7d 0a 23 69 66 20 64 65 66 69 6e 65 64 28    }.#if defined(
def70 46 44 5f 43 4c 4f 45 58 45 43 29 20 26 26 20 28  FD_CLOEXEC) && (
def80 21 64 65 66 69 6e 65 64 28 4f 5f 43 4c 4f 45 58  !defined(O_CLOEX
def90 45 43 29 20 7c 7c 20 4f 5f 43 4c 4f 45 58 45 43  EC) || O_CLOEXEC
defa0 3d 3d 30 29 0a 20 20 69 66 28 20 66 64 3e 3d 30  ==0).  if( fd>=0
defb0 20 29 20 6f 73 46 63 6e 74 6c 28 66 64 2c 20 46   ) osFcntl(fd, F
defc0 5f 53 45 54 46 44 2c 20 6f 73 46 63 6e 74 6c 28  _SETFD, osFcntl(
defd0 66 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20  fd, F_GETFD, 0) 
defe0 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23  | FD_CLOEXEC);.#
deff0 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 66  endif.  return f
df000 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70  d;.}../*.** Help
df010 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20  er functions to 
df020 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c 69 6e  obtain and relin
df030 71 75 69 73 68 20 74 68 65 20 67 6c 6f 62 61 6c  quish the global
df040 20 6d 75 74 65 78 2e 20 54 68 65 0a 2a 2a 20 67   mutex. The.** g
df050 6c 6f 62 61 6c 20 6d 75 74 65 78 20 69 73 20 75  lobal mutex is u
df060 73 65 64 20 74 6f 20 70 72 6f 74 65 63 74 20 74  sed to protect t
df070 68 65 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f  he unixInodeInfo
df080 20 61 6e 64 0a 2a 2a 20 76 78 77 6f 72 6b 73 46   and.** vxworksF
df090 69 6c 65 49 64 20 6f 62 6a 65 63 74 73 20 75 73  ileId objects us
df0a0 65 64 20 62 79 20 74 68 69 73 20 66 69 6c 65 2c  ed by this file,
df0b0 20 61 6c 6c 20 6f 66 20 77 68 69 63 68 20 6d 61   all of which ma
df0c0 79 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64 20  y be .** shared 
df0d0 62 79 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65  by multiple thre
df0e0 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74  ads..**.** Funct
df0f0 69 6f 6e 20 75 6e 69 78 4d 75 74 65 78 48 65 6c  ion unixMutexHel
df100 64 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61  d() is used to a
df110 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65  ssert() that the
df120 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 0a 2a   global mutex .*
df130 2a 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20 72  * is held when r
df140 65 71 75 69 72 65 64 2e 20 54 68 69 73 20 66 75  equired. This fu
df150 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75  nction is only u
df160 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61  sed as part of a
df170 73 73 65 72 74 28 29 20 0a 2a 2a 20 73 74 61 74  ssert() .** stat
df180 65 6d 65 6e 74 73 2e 20 65 2e 67 2e 0a 2a 2a 0a  ements. e.g..**.
df190 2a 2a 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  **   unixEnterMu
df1a0 74 65 78 28 29 0a 2a 2a 20 20 20 20 20 61 73 73  tex().**     ass
df1b0 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65  ert( unixMutexHe
df1c0 6c 64 28 29 20 29 3b 0a 2a 2a 20 20 20 75 6e 69  ld() );.**   uni
df1d0 78 45 6e 74 65 72 4c 65 61 76 65 28 29 0a 2a 2f  xEnterLeave().*/
df1e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69  .static void uni
df1f0 78 45 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 64  xEnterMutex(void
df200 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
df210 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
df220 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
df230 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
df240 41 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69  ASTER));.}.stati
df250 63 20 76 6f 69 64 20 75 6e 69 78 4c 65 61 76 65  c void unixLeave
df260 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73  Mutex(void){.  s
df270 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
df280 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41  ve(sqlite3MutexA
df290 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
df2a0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
df2b0 29 3b 0a 7d 0a 23 69 66 64 65 66 20 53 51 4c 49  );.}.#ifdef SQLI
df2c0 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20  TE_DEBUG.static 
df2d0 69 6e 74 20 75 6e 69 78 4d 75 74 65 78 48 65 6c  int unixMutexHel
df2e0 64 28 76 6f 69 64 29 20 7b 0a 20 20 72 65 74 75  d(void) {.  retu
df2f0 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  rn sqlite3_mutex
df300 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 74  _held(sqlite3Mut
df310 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
df320 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
df330 45 52 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ER));.}.#endif..
df340 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
df350 49 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 66  ITE_TEST) && def
df360 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
df370 47 29 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20  G)./*.** Helper 
df380 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 72 69  function for pri
df390 6e 74 69 6e 67 20 6f 75 74 20 74 72 61 63 65 20  nting out trace 
df3a0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
df3b0 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 62 69   debugging.** bi
df3c0 6e 61 72 69 65 73 2e 20 54 68 69 73 20 72 65 74  naries. This ret
df3d0 75 72 6e 73 20 74 68 65 20 73 74 72 69 6e 67 20  urns the string 
df3e0 72 65 70 72 65 73 65 74 61 74 69 6f 6e 20 6f 66  represetation of
df3f0 20 74 68 65 20 73 75 70 70 6c 69 65 64 0a 2a 2a   the supplied.**
df400 20 69 6e 74 65 67 65 72 20 6c 6f 63 6b 2d 74 79   integer lock-ty
df410 70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  pe..*/.static co
df420 6e 73 74 20 63 68 61 72 20 2a 61 7a 46 69 6c 65  nst char *azFile
df430 4c 6f 63 6b 28 69 6e 74 20 65 46 69 6c 65 4c 6f  Lock(int eFileLo
df440 63 6b 29 7b 0a 20 20 73 77 69 74 63 68 28 20 65  ck){.  switch( e
df450 46 69 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20  FileLock ){.    
df460 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b 3a 20 72 65  case NO_LOCK: re
df470 74 75 72 6e 20 22 4e 4f 4e 45 22 3b 0a 20 20 20  turn "NONE";.   
df480 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c 4f 43   case SHARED_LOC
df490 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41 52 45  K: return "SHARE
df4a0 44 22 3b 0a 20 20 20 20 63 61 73 65 20 52 45 53  D";.    case RES
df4b0 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75  ERVED_LOCK: retu
df4c0 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a 20  rn "RESERVED";. 
df4d0 20 20 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f     case PENDING_
df4e0 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45  LOCK: return "PE
df4f0 4e 44 49 4e 47 22 3b 0a 20 20 20 20 63 61 73 65  NDING";.    case
df500 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a   EXCLUSIVE_LOCK:
df510 20 72 65 74 75 72 6e 20 22 45 58 43 4c 55 53 49   return "EXCLUSI
df520 56 45 22 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  VE";.  }.  retur
df530 6e 20 22 45 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e  n "ERROR";.}.#en
df540 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
df550 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2f 2a  TE_LOCK_TRACE./*
df560 0a 2a 2a 20 50 72 69 6e 74 20 6f 75 74 20 69 6e  .** Print out in
df570 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
df580 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72  all locking oper
df590 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ations..**.** Th
df5a0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
df5b0 65 64 20 66 6f 72 20 74 72 6f 75 62 6c 65 73 68  ed for troublesh
df5c0 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20  ooting locks on 
df5d0 6d 75 6c 74 69 74 68 72 65 61 64 65 64 0a 2a 2a  multithreaded.**
df5e0 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 45 6e 61   platforms.  Ena
df5f0 62 6c 65 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67  ble by compiling
df600 20 77 69 74 68 20 74 68 65 20 2d 44 53 51 4c 49   with the -DSQLI
df610 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2a 2a  TE_LOCK_TRACE.**
df620 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70   command-line op
df630 74 69 6f 6e 20 6f 6e 20 74 68 65 20 63 6f 6d 70  tion on the comp
df640 69 6c 65 72 2e 20 20 54 68 69 73 20 63 6f 64 65  iler.  This code
df650 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20   is normally.** 
df660 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2f 0a 73  turned off..*/.s
df670 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54 72  tatic int lockTr
df680 61 63 65 28 69 6e 74 20 66 64 2c 20 69 6e 74 20  ace(int fd, int 
df690 6f 70 2c 20 73 74 72 75 63 74 20 66 6c 6f 63 6b  op, struct flock
df6a0 20 2a 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4f   *p){.  char *zO
df6b0 70 4e 61 6d 65 2c 20 2a 7a 54 79 70 65 3b 0a 20  pName, *zType;. 
df6c0 20 69 6e 74 20 73 3b 0a 20 20 69 6e 74 20 73 61   int s;.  int sa
df6d0 76 65 64 45 72 72 6e 6f 3b 0a 20 20 69 66 28 20  vedErrno;.  if( 
df6e0 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20 29 7b 0a 20  op==F_GETLK ){. 
df6f0 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 47 45     zOpName = "GE
df700 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66  TLK";.  }else if
df710 28 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 29 7b  ( op==F_SETLK ){
df720 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22  .    zOpName = "
df730 53 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b  SETLK";.  }else{
df740 0a 20 20 20 20 73 20 3d 20 6f 73 46 63 6e 74 6c  .    s = osFcntl
df750 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 20  (fd, op, p);.   
df760 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
df770 6e 74 66 28 22 66 63 6e 74 6c 20 75 6e 6b 6e 6f  ntf("fcntl unkno
df780 77 6e 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20  wn %d %d %d\n", 
df790 66 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 20 20 20  fd, op, s);.    
df7a0 72 65 74 75 72 6e 20 73 3b 0a 20 20 7d 0a 20 20  return s;.  }.  
df7b0 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46  if( p->l_type==F
df7c0 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54  _RDLCK ){.    zT
df7d0 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20  ype = "RDLCK";. 
df7e0 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f   }else if( p->l_
df7f0 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b  type==F_WRLCK ){
df800 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52  .    zType = "WR
df810 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66  LCK";.  }else if
df820 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 55  ( p->l_type==F_U
df830 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70  NLCK ){.    zTyp
df840 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 7d  e = "UNLCK";.  }
df850 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
df860 28 20 30 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73  ( 0 );.  }.  ass
df870 65 72 74 28 20 70 2d 3e 6c 5f 77 68 65 6e 63 65  ert( p->l_whence
df880 3d 3d 53 45 45 4b 5f 53 45 54 20 29 3b 0a 20 20  ==SEEK_SET );.  
df890 73 20 3d 20 6f 73 46 63 6e 74 6c 28 66 64 2c 20  s = osFcntl(fd, 
df8a0 6f 70 2c 20 70 29 3b 0a 20 20 73 61 76 65 64 45  op, p);.  savedE
df8b0 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
df8c0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
df8d0 74 66 28 22 66 63 6e 74 6c 20 25 64 20 25 64 20  tf("fcntl %d %d 
df8e0 25 73 20 25 73 20 25 64 20 25 64 20 25 64 20 25  %s %s %d %d %d %
df8f0 64 5c 6e 22 2c 0a 20 20 20 20 20 74 68 72 65 61  d\n",.     threa
df900 64 69 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65  did, fd, zOpName
df910 2c 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 70 2d  , zType, (int)p-
df920 3e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 70  >l_start, (int)p
df930 2d 3e 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69  ->l_len,.     (i
df940 6e 74 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b  nt)p->l_pid, s);
df950 0a 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 26  .  if( s==(-1) &
df960 26 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26  & op==F_SETLK &&
df970 20 28 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52   (p->l_type==F_R
df980 44 4c 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70  DLCK || p->l_typ
df990 65 3d 3d 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20  e==F_WRLCK) ){. 
df9a0 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20     struct flock 
df9b0 6c 32 3b 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b  l2;.    l2 = *p;
df9c0 0a 20 20 20 20 6f 73 46 63 6e 74 6c 28 66 64 2c  .    osFcntl(fd,
df9d0 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a   F_GETLK, &l2);.
df9e0 20 20 20 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70      if( l2.l_typ
df9f0 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20  e==F_RDLCK ){.  
dfa00 20 20 20 20 7a 54 79 70 65 20 3d 20 22 52 44 4c      zType = "RDL
dfa10 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  CK";.    }else i
dfa20 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f  f( l2.l_type==F_
dfa30 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a  WRLCK ){.      z
dfa40 54 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a  Type = "WRLCK";.
dfa50 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32      }else if( l2
dfa60 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b  .l_type==F_UNLCK
dfa70 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20   ){.      zType 
dfa80 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d  = "UNLCK";.    }
dfa90 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
dfaa0 72 74 28 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20  rt( 0 );.    }. 
dfab0 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
dfac0 72 69 6e 74 66 28 22 66 63 6e 74 6c 2d 66 61 69  rintf("fcntl-fai
dfad0 6c 75 72 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20  lure-reason: %s 
dfae0 25 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20  %d %d %d\n",.   
dfaf0 20 20 20 20 7a 54 79 70 65 2c 20 28 69 6e 74 29      zType, (int)
dfb00 6c 32 2e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74  l2.l_start, (int
dfb10 29 6c 32 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29  )l2.l_len, (int)
dfb20 6c 32 2e 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20  l2.l_pid);.  }. 
dfb30 20 65 72 72 6e 6f 20 3d 20 73 61 76 65 64 45 72   errno = savedEr
dfb40 72 6e 6f 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b  rno;.  return s;
dfb50 0a 7d 0a 23 75 6e 64 65 66 20 6f 73 46 63 6e 74  .}.#undef osFcnt
dfb60 6c 0a 23 64 65 66 69 6e 65 20 6f 73 46 63 6e 74  l.#define osFcnt
dfb70 6c 20 6c 6f 63 6b 54 72 61 63 65 0a 23 65 6e 64  l lockTrace.#end
dfb80 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43  if /* SQLITE_LOC
dfb90 4b 5f 54 52 41 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a  K_TRACE */../*.*
dfba0 2a 20 52 65 74 72 79 20 66 74 72 75 6e 63 61 74  * Retry ftruncat
dfbb0 65 28 29 20 63 61 6c 6c 73 20 74 68 61 74 20 66  e() calls that f
dfbc0 61 69 6c 20 64 75 65 20 74 6f 20 45 49 4e 54 52  ail due to EINTR
dfbd0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
dfbe0 6f 62 75 73 74 5f 66 74 72 75 6e 63 61 74 65 28  obust_ftruncate(
dfbf0 69 6e 74 20 68 2c 20 73 71 6c 69 74 65 33 5f 69  int h, sqlite3_i
dfc00 6e 74 36 34 20 73 7a 29 7b 0a 20 20 69 6e 74 20  nt64 sz){.  int 
dfc10 72 63 3b 0a 20 20 64 6f 7b 20 72 63 20 3d 20 6f  rc;.  do{ rc = o
dfc20 73 46 74 72 75 6e 63 61 74 65 28 68 2c 73 7a 29  sFtruncate(h,sz)
dfc30 3b 20 7d 77 68 69 6c 65 28 20 72 63 3c 30 20 26  ; }while( rc<0 &
dfc40 26 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20 29  & errno==EINTR )
dfc50 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
dfc60 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
dfc70 74 69 6e 65 20 74 72 61 6e 73 6c 61 74 65 73 20  tine translates 
dfc80 61 20 73 74 61 6e 64 61 72 64 20 50 4f 53 49 58  a standard POSIX
dfc90 20 65 72 72 6e 6f 20 63 6f 64 65 20 69 6e 74 6f   errno code into
dfca0 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 75 73   something.** us
dfcb0 65 66 75 6c 20 74 6f 20 74 68 65 20 63 6c 69 65  eful to the clie
dfcc0 6e 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74  nts of the sqlit
dfcd0 65 33 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 53  e3 functions.  S
dfce0 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 69 74 20  pecifically, it 
dfcf0 69 73 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 74  is.** intended t
dfd00 6f 20 74 72 61 6e 73 6c 61 74 65 20 61 20 76 61  o translate a va
dfd10 72 69 65 74 79 20 6f 66 20 22 74 72 79 20 61 67  riety of "try ag
dfd20 61 69 6e 22 20 65 72 72 6f 72 73 20 69 6e 74 6f  ain" errors into
dfd30 20 53 51 4c 49 54 45 5f 42 55 53 59 0a 2a 2a 20   SQLITE_BUSY.** 
dfd40 61 6e 64 20 61 20 76 61 72 69 65 74 79 20 6f 66  and a variety of
dfd50 20 22 70 6c 65 61 73 65 20 63 6c 6f 73 65 20 74   "please close t
dfd60 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
dfd70 6f 72 20 4e 4f 57 22 20 65 72 72 6f 72 73 20 69  or NOW" errors i
dfd80 6e 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 49  nto .** SQLITE_I
dfd90 4f 45 52 52 0a 2a 2a 20 0a 2a 2a 20 45 72 72 6f  OERR.** .** Erro
dfda0 72 73 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61  rs during initia
dfdb0 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 6c 6f 63 6b  lization of lock
dfdc0 73 2c 20 6f 72 20 66 69 6c 65 20 73 79 73 74 65  s, or file syste
dfdd0 6d 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6c 6f  m support for lo
dfde0 63 6b 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20 68  cks,.** should h
dfdf0 61 6e 64 6c 65 20 45 4e 4f 4c 43 4b 2c 20 45 4e  andle ENOLCK, EN
dfe00 4f 54 53 55 50 2c 20 45 4f 50 4e 4f 54 53 55 50  OTSUP, EOPNOTSUP
dfe10 50 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 2a 2f  P separately..*/
dfe20 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
dfe30 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78  teErrorFromPosix
dfe40 45 72 72 6f 72 28 69 6e 74 20 70 6f 73 69 78 45  Error(int posixE
dfe50 72 72 6f 72 2c 20 69 6e 74 20 73 71 6c 69 74 65  rror, int sqlite
dfe60 49 4f 45 72 72 29 20 7b 0a 20 20 73 77 69 74 63  IOErr) {.  switc
dfe70 68 20 28 70 6f 73 69 78 45 72 72 6f 72 29 20 7b  h (posixError) {
dfe80 0a 23 69 66 20 30 0a 20 20 2f 2a 20 41 74 20 6f  .#if 0.  /* At o
dfe90 6e 65 20 70 6f 69 6e 74 20 74 68 69 73 20 63 6f  ne point this co
dfea0 64 65 20 77 61 73 20 6e 6f 74 20 63 6f 6d 6d 65  de was not comme
dfeb0 6e 74 65 64 20 6f 75 74 2e 20 49 6e 20 74 68 65  nted out. In the
dfec0 6f 72 79 2c 20 74 68 69 73 20 62 72 61 6e 63 68  ory, this branch
dfed0 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 6e 65 76  .  ** should nev
dfee0 65 72 20 62 65 20 68 69 74 2c 20 61 73 20 74 68  er be hit, as th
dfef0 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  is function shou
dff00 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65  ld only be calle
dff10 64 20 61 66 74 65 72 0a 20 20 2a 2a 20 61 20 6c  d after.  ** a l
dff20 6f 63 6b 69 6e 67 2d 72 65 6c 61 74 65 64 20 66  ocking-related f
dff30 75 6e 63 74 69 6f 6e 20 28 69 2e 65 2e 20 66 63  unction (i.e. fc
dff40 6e 74 6c 28 29 29 20 68 61 73 20 72 65 74 75 72  ntl()) has retur
dff50 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 20 77 69 74  ned non-zero wit
dff60 68 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65  h.  ** the value
dff70 20 6f 66 20 65 72 72 6e 6f 20 61 73 20 74 68 65   of errno as the
dff80 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
dff90 20 53 69 6e 63 65 20 61 20 73 79 73 74 65 6d 20   Since a system 
dffa0 63 61 6c 6c 20 68 61 73 20 66 61 69 6c 65 64 2c  call has failed,
dffb0 0a 20 20 2a 2a 20 65 72 72 6e 6f 20 73 68 6f 75  .  ** errno shou
dffc0 6c 64 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a  ld be non-zero..
dffd0 20 20 2a 2a 0a 20 20 2a 2a 20 44 65 73 70 69 74    **.  ** Despit
dffe0 65 20 74 68 69 73 2c 20 69 66 20 65 72 72 6e 6f  e this, if errno
dfff0 20 72 65 61 6c 6c 79 20 69 73 20 7a 65 72 6f 2c   really is zero,
e0000 20 77 65 20 73 74 69 6c 6c 20 64 6f 6e 27 74 20   we still don't 
e0010 77 61 6e 74 20 74 6f 20 72 65 74 75 72 6e 0a 20  want to return. 
e0020 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54   ** SQLITE_OK. T
e0030 68 65 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 66  he system call f
e0040 61 69 6c 65 64 2c 20 61 6e 64 20 2a 73 6f 6d 65  ailed, and *some
e0050 2a 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 73  * SQLite error s
e0060 68 6f 75 6c 64 20 62 65 0a 20 20 2a 2a 20 70 72  hould be.  ** pr
e0070 6f 70 61 67 61 74 65 64 20 62 61 63 6b 20 74 6f  opagated back to
e0080 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 43 6f 6d   the caller. Com
e0090 6d 65 6e 74 69 6e 67 20 74 68 69 73 20 62 72 61  menting this bra
e00a0 6e 63 68 20 6f 75 74 20 6d 65 61 6e 73 20 65 72  nch out means er
e00b0 72 6e 6f 3d 3d 30 0a 20 20 2a 2a 20 77 69 6c 6c  rno==0.  ** will
e00c0 20 62 65 20 68 61 6e 64 6c 65 64 20 62 79 20 74   be handled by t
e00d0 68 65 20 22 64 65 66 61 75 6c 74 3a 22 20 63 61  he "default:" ca
e00e0 73 65 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20  se below..  */. 
e00f0 20 63 61 73 65 20 30 3a 20 0a 20 20 20 20 72 65   case 0: .    re
e0100 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
e0110 23 65 6e 64 69 66 0a 0a 20 20 63 61 73 65 20 45  #endif..  case E
e0120 41 47 41 49 4e 3a 0a 20 20 63 61 73 65 20 45 54  AGAIN:.  case ET
e0130 49 4d 45 44 4f 55 54 3a 0a 20 20 63 61 73 65 20  IMEDOUT:.  case 
e0140 45 42 55 53 59 3a 0a 20 20 63 61 73 65 20 45 49  EBUSY:.  case EI
e0150 4e 54 52 3a 0a 20 20 63 61 73 65 20 45 4e 4f 4c  NTR:.  case ENOL
e0160 43 4b 3a 20 20 0a 20 20 20 20 2f 2a 20 72 61 6e  CK:  .    /* ran
e0170 64 6f 6d 20 4e 46 53 20 72 65 74 72 79 20 65 72  dom NFS retry er
e0180 72 6f 72 2c 20 75 6e 6c 65 73 73 20 64 75 72 69  ror, unless duri
e0190 6e 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73  ng file system s
e01a0 75 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69  upport .     * i
e01b0 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e  ntrospection, in
e01c0 20 77 68 69 63 68 20 69 74 20 61 63 74 75 61 6c   which it actual
e01d0 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74  ly means what it
e01e0 20 73 61 79 73 20 2a 2f 0a 20 20 20 20 72 65 74   says */.    ret
e01f0 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  urn SQLITE_BUSY;
e0200 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 41 43  .    .  case EAC
e0210 43 45 53 3a 20 0a 20 20 20 20 2f 2a 20 45 41 43  CES: .    /* EAC
e0220 43 45 53 20 69 73 20 6c 69 6b 65 20 45 41 47 41  CES is like EAGA
e0230 49 4e 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e  IN during lockin
e0240 67 20 6f 70 65 72 61 74 69 6f 6e 73 2c 20 62 75  g operations, bu
e0250 74 20 6e 6f 74 20 61 6e 79 20 6f 74 68 65 72 20  t not any other 
e0260 74 69 6d 65 2a 2f 0a 20 20 20 20 69 66 28 20 28  time*/.    if( (
e0270 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53  sqliteIOErr == S
e0280 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b  QLITE_IOERR_LOCK
e0290 29 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 28 73  ) || .        (s
e02a0 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51  qliteIOErr == SQ
e02b0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
e02c0 4b 29 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 28  K) || .        (
e02d0 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53  sqliteIOErr == S
e02e0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f  QLITE_IOERR_RDLO
e02f0 43 4b 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 28  CK) ||.        (
e0300 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53  sqliteIOErr == S
e0310 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43  QLITE_IOERR_CHEC
e0320 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20 29  KRESERVEDLOCK) )
e0330 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
e0340 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
e0350 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20 66 61  }.    /* else fa
e0360 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20  ll through */.  
e0370 63 61 73 65 20 45 50 45 52 4d 3a 20 0a 20 20 20  case EPERM: .   
e0380 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50   return SQLITE_P
e0390 45 52 4d 3b 0a 20 20 20 20 0a 20 20 2f 2a 20 45  ERM;.    .  /* E
e03a0 44 45 41 44 4c 4b 20 69 73 20 6f 6e 6c 79 20 70  DEADLK is only p
e03b0 6f 73 73 69 62 6c 65 20 69 66 20 61 20 63 61 6c  ossible if a cal
e03c0 6c 20 74 6f 20 66 63 6e 74 6c 28 46 5f 53 45 54  l to fcntl(F_SET
e03d0 4c 4b 57 29 20 69 73 20 6d 61 64 65 2e 20 41 6e  LKW) is made. An
e03e0 64 0a 20 20 2a 2a 20 74 68 69 73 20 6d 6f 64 75  d.  ** this modu
e03f0 6c 65 20 6e 65 76 65 72 20 6d 61 6b 65 73 20 73  le never makes s
e0400 75 63 68 20 61 20 63 61 6c 6c 2e 20 41 6e 64 20  uch a call. And 
e0410 74 68 65 20 63 6f 64 65 20 69 6e 20 53 51 4c 69  the code in SQLi
e0420 74 65 20 69 74 73 65 6c 66 20 0a 20 20 2a 2a 20  te itself .  ** 
e0430 61 73 73 65 72 74 73 20 74 68 61 74 20 53 51 4c  asserts that SQL
e0440 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
e0450 44 20 69 73 20 6e 65 76 65 72 20 72 65 74 75 72  D is never retur
e0460 6e 65 64 2e 20 46 6f 72 20 74 68 65 73 65 20 72  ned. For these r
e0470 65 61 73 6f 6e 73 0a 20 20 2a 2a 20 74 68 69 73  easons.  ** this
e0480 20 63 61 73 65 20 69 73 20 61 6c 73 6f 20 63 6f   case is also co
e0490 6d 6d 65 6e 74 65 64 20 6f 75 74 2e 20 49 66 20  mmented out. If 
e04a0 74 68 65 20 73 79 73 74 65 6d 20 64 6f 65 73 20  the system does 
e04b0 73 65 74 20 65 72 72 6e 6f 20 74 6f 20 45 44 45  set errno to EDE
e04c0 41 44 4c 4b 2c 0a 20 20 2a 2a 20 74 68 65 20 64  ADLK,.  ** the d
e04d0 65 66 61 75 6c 74 20 53 51 4c 49 54 45 5f 49 4f  efault SQLITE_IO
e04e0 45 52 52 5f 58 58 58 20 63 6f 64 65 20 77 69 6c  ERR_XXX code wil
e04f0 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 2a  l be returned. *
e0500 2f 0a 23 69 66 20 30 0a 20 20 63 61 73 65 20 45  /.#if 0.  case E
e0510 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72 65 74 75  DEADLK:.    retu
e0520 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
e0530 42 4c 4f 43 4b 45 44 3b 0a 23 65 6e 64 69 66 0a  BLOCKED;.#endif.
e0540 20 20 20 20 0a 23 69 66 20 45 4f 50 4e 4f 54 53      .#if EOPNOTS
e0550 55 50 50 21 3d 45 4e 4f 54 53 55 50 0a 20 20 63  UPP!=ENOTSUP.  c
e0560 61 73 65 20 45 4f 50 4e 4f 54 53 55 50 50 3a 20  ase EOPNOTSUPP: 
e0570 0a 20 20 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e  .    /* somethin
e0580 67 20 77 65 6e 74 20 74 65 72 72 69 62 6c 79 20  g went terribly 
e0590 61 77 72 79 2c 20 75 6e 6c 65 73 73 20 64 75 72  awry, unless dur
e05a0 69 6e 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20  ing file system 
e05b0 73 75 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20  support .     * 
e05c0 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69  introspection, i
e05d0 6e 20 77 68 69 63 68 20 69 74 20 61 63 74 75 61  n which it actua
e05e0 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69  lly means what i
e05f0 74 20 73 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66  t says */.#endif
e0600 0a 23 69 66 64 65 66 20 45 4e 4f 54 53 55 50 0a  .#ifdef ENOTSUP.
e0610 20 20 63 61 73 65 20 45 4e 4f 54 53 55 50 3a 20    case ENOTSUP: 
e0620 0a 20 20 20 20 2f 2a 20 69 6e 76 61 6c 69 64 20  .    /* invalid 
e0630 66 64 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e  fd, unless durin
e0640 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75  g file system su
e0650 70 70 6f 72 74 20 69 6e 74 72 6f 73 70 65 63 74  pport introspect
e0660 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 0a 20  ion, in which . 
e0670 20 20 20 20 2a 20 69 74 20 61 63 74 75 61 6c 6c      * it actuall
e0680 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20  y means what it 
e0690 73 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  says */.#endif. 
e06a0 20 63 61 73 65 20 45 49 4f 3a 0a 20 20 63 61 73   case EIO:.  cas
e06b0 65 20 45 42 41 44 46 3a 0a 20 20 63 61 73 65 20  e EBADF:.  case 
e06c0 45 49 4e 56 41 4c 3a 0a 20 20 63 61 73 65 20 45  EINVAL:.  case E
e06d0 4e 4f 54 43 4f 4e 4e 3a 0a 20 20 63 61 73 65 20  NOTCONN:.  case 
e06e0 45 4e 4f 44 45 56 3a 0a 20 20 63 61 73 65 20 45  ENODEV:.  case E
e06f0 4e 58 49 4f 3a 0a 20 20 63 61 73 65 20 45 4e 4f  NXIO:.  case ENO
e0700 45 4e 54 3a 0a 23 69 66 64 65 66 20 45 53 54 41  ENT:.#ifdef ESTA
e0710 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LE              
e0720 20 20 20 20 20 20 20 2f 2a 20 45 53 54 41 4c 45         /* ESTALE
e0730 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 20   is not defined 
e0740 6f 6e 20 49 6e 74 65 72 69 78 20 73 79 73 74 65  on Interix syste
e0750 6d 73 20 2a 2f 0a 20 20 63 61 73 65 20 45 53 54  ms */.  case EST
e0760 41 4c 45 3a 0a 23 65 6e 64 69 66 0a 20 20 63 61  ALE:.#endif.  ca
e0770 73 65 20 45 4e 4f 53 59 53 3a 0a 20 20 20 20 2f  se ENOSYS:.    /
e0780 2a 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 66  * these should f
e0790 6f 72 63 65 20 74 68 65 20 63 6c 69 65 6e 74 20  orce the client 
e07a0 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c  to close the fil
e07b0 65 20 61 6e 64 20 72 65 63 6f 6e 6e 65 63 74 20  e and reconnect 
e07c0 2a 2f 0a 20 20 20 20 0a 20 20 64 65 66 61 75 6c  */.    .  defaul
e07d0 74 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20 73  t: .    return s
e07e0 71 6c 69 74 65 49 4f 45 72 72 3b 0a 20 20 7d 0a  qliteIOErr;.  }.
e07f0 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }..../**********
e0800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0840 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
e0850 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 55 6e  ******* Begin Un
e0860 69 71 75 65 20 46 69 6c 65 20 49 44 20 55 74 69  ique File ID Uti
e0870 6c 69 74 79 20 55 73 65 64 20 42 79 20 56 78 57  lity Used By VxW
e0880 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  orks ***********
e0890 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 6e 20 6d 6f  ****.**.** On mo
e08a0 73 74 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 75  st versions of u
e08b0 6e 69 78 2c 20 77 65 20 63 61 6e 20 67 65 74 20  nix, we can get 
e08c0 61 20 75 6e 69 71 75 65 20 49 44 20 66 6f 72 20  a unique ID for 
e08d0 61 20 66 69 6c 65 20 62 79 20 63 6f 6e 63 61 74  a file by concat
e08e0 65 6e 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20 64  enating.** the d
e08f0 65 76 69 63 65 20 6e 75 6d 62 65 72 20 61 6e 64  evice number and
e0900 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65   the inode numbe
e0910 72 2e 20 20 42 75 74 20 74 68 69 73 20 64 6f 65  r.  But this doe
e0920 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 78  s not work on Vx
e0930 57 6f 72 6b 73 2e 0a 2a 2a 20 4f 6e 20 56 78 57  Works..** On VxW
e0940 6f 72 6b 73 2c 20 61 20 75 6e 69 71 75 65 20 66  orks, a unique f
e0950 69 6c 65 20 69 64 20 6d 75 73 74 20 62 65 20 62  ile id must be b
e0960 61 73 65 64 20 6f 6e 20 74 68 65 20 63 61 6e 6f  ased on the cano
e0970 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a  nical filename..
e0980 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  **.** A pointer 
e0990 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  to an instance o
e09a0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
e09b0 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65  structure can be
e09c0 20 75 73 65 64 20 61 73 20 61 0a 2a 2a 20 75 6e   used as a.** un
e09d0 69 71 75 65 20 66 69 6c 65 20 49 44 20 69 6e 20  ique file ID in 
e09e0 56 78 57 6f 72 6b 73 2e 20 20 45 61 63 68 20 69  VxWorks.  Each i
e09f0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
e0a00 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
e0a10 6e 73 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20  ns.** a copy of 
e0a20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69  the canonical fi
e0a30 6c 65 6e 61 6d 65 2e 20 20 54 68 65 72 65 20 69  lename.  There i
e0a40 73 20 61 6c 73 6f 20 61 20 72 65 66 65 72 65 6e  s also a referen
e0a50 63 65 20 63 6f 75 6e 74 2e 20 20 0a 2a 2a 20 54  ce count.  .** T
e0a60 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  he structure is 
e0a70 72 65 63 6c 61 69 6d 65 64 20 77 68 65 6e 20 74  reclaimed when t
e0a80 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f 69  he number of poi
e0a90 6e 74 65 72 73 20 74 6f 20 69 74 20 64 72 6f 70  nters to it drop
e0aa0 73 20 74 6f 0a 2a 2a 20 7a 65 72 6f 2e 0a 2a 2a  s to.** zero..**
e0ab0 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 6e 65  .** There are ne
e0ac0 76 65 72 20 76 65 72 79 20 6d 61 6e 79 20 66 69  ver very many fi
e0ad0 6c 65 73 20 6f 70 65 6e 20 61 74 20 6f 6e 65 20  les open at one 
e0ae0 74 69 6d 65 20 61 6e 64 20 6c 6f 6f 6b 75 70 73  time and lookups
e0af0 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 61 20 70 65   are not.** a pe
e0b00 72 66 6f 72 6d 61 6e 63 65 2d 63 72 69 74 69 63  rformance-critic
e0b10 61 6c 20 70 61 74 68 2c 20 73 6f 20 69 74 20 69  al path, so it i
e0b20 73 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20  s sufficient to 
e0b30 70 75 74 20 74 68 65 73 65 0a 2a 2a 20 73 74 72  put these.** str
e0b40 75 63 74 75 72 65 73 20 6f 6e 20 61 20 6c 69 6e  uctures on a lin
e0b50 6b 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72  ked list..*/.str
e0b60 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  uct vxworksFileI
e0b70 64 20 7b 0a 20 20 73 74 72 75 63 74 20 76 78 77  d {.  struct vxw
e0b80 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 78  orksFileId *pNex
e0b90 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 61  t;  /* Next in a
e0ba0 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c   list of them al
e0bb0 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b  l */.  int nRef;
e0bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0bd0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
e0be0 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  f references to 
e0bf0 74 68 69 73 20 6f 6e 65 20 2a 2f 0a 20 20 69 6e  this one */.  in
e0c00 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20  t nName;        
e0c10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
e0c20 65 6e 67 74 68 20 6f 66 20 74 68 65 20 7a 43 61  ength of the zCa
e0c30 6e 6f 6e 69 63 61 6c 4e 61 6d 65 5b 5d 20 73 74  nonicalName[] st
e0c40 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ring */.  char *
e0c50 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 3b 20  zCanonicalName; 
e0c60 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e 6f 6e          /* Canon
e0c70 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 2a 2f  ical filename */
e0c80 0a 7d 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f  .};..#if OS_VXWO
e0c90 52 4b 53 0a 2f 2a 20 0a 2a 2a 20 41 6c 6c 20 75  RKS./* .** All u
e0ca0 6e 69 71 75 65 20 66 69 6c 65 6e 61 6d 65 73 20  nique filenames 
e0cb0 61 72 65 20 68 65 6c 64 20 6f 6e 20 61 20 6c 69  are held on a li
e0cc0 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64  nked list headed
e0cd0 20 62 79 20 74 68 69 73 0a 2a 2a 20 76 61 72 69   by this.** vari
e0ce0 61 62 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69 63 20  able:.*/.static 
e0cf0 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69  struct vxworksFi
e0d00 6c 65 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 6c  leId *vxworksFil
e0d10 65 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a  eList = 0;../*.*
e0d20 2a 20 53 69 6d 70 6c 69 66 79 20 61 20 66 69 6c  * Simplify a fil
e0d30 65 6e 61 6d 65 20 69 6e 74 6f 20 69 74 73 20 63  ename into its c
e0d40 61 6e 6f 6e 69 63 61 6c 20 66 6f 72 6d 0a 2a 2a  anonical form.**
e0d50 20 62 79 20 6d 61 6b 69 6e 67 20 74 68 65 20 66   by making the f
e0d60 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 73  ollowing changes
e0d70 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 72 65 6d 6f 76  :.**.**  * remov
e0d80 69 6e 67 20 61 6e 79 20 74 72 61 69 6c 69 6e 67  ing any trailing
e0d90 20 61 6e 64 20 64 75 70 6c 69 63 61 74 65 20 2f   and duplicate /
e0da0 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f  .**  * convert /
e0db0 2e 2f 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a  ./ into just /.*
e0dc0 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f 41 2f  *  * convert /A/
e0dd0 2e 2e 2f 20 77 68 65 72 65 20 41 20 69 73 20 61  ../ where A is a
e0de0 6e 79 20 73 69 6d 70 6c 65 20 6e 61 6d 65 20 69  ny simple name i
e0df0 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 0a 2a 2a  nto just /.**.**
e0e00 20 43 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64   Changes are mad
e0e10 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 52 65 74  e in-place.  Ret
e0e20 75 72 6e 20 74 68 65 20 6e 65 77 20 6e 61 6d 65  urn the new name
e0e30 20 6c 65 6e 67 74 68 2e 0a 2a 2a 0a 2a 2a 20 54   length..**.** T
e0e40 68 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65  he original file
e0e50 6e 61 6d 65 20 69 73 20 69 6e 20 7a 5b 30 2e 2e  name is in z[0..
e0e60 6e 2d 31 5d 2e 20 20 52 65 74 75 72 6e 20 74 68  n-1].  Return th
e0e70 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63  e number of.** c
e0e80 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65  haracters in the
e0e90 20 73 69 6d 70 6c 69 66 69 65 64 20 6e 61 6d 65   simplified name
e0ea0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e0eb0 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66 79 4e  vxworksSimplifyN
e0ec0 61 6d 65 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74  ame(char *z, int
e0ed0 20 6e 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b   n){.  int i, j;
e0ee0 0a 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 26 26  .  while( n>1 &&
e0ef0 20 7a 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 29 7b 20   z[n-1]=='/' ){ 
e0f00 6e 2d 2d 3b 20 7d 0a 20 20 66 6f 72 28 69 3d 6a  n--; }.  for(i=j
e0f10 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20  =0; i<n; i++){. 
e0f20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2f 27     if( z[i]=='/'
e0f30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b   ){.      if( z[
e0f40 69 2b 31 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e 74  i+1]=='/' ) cont
e0f50 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20  inue;.      if( 
e0f60 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69  z[i+1]=='.' && i
e0f70 2b 32 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d  +2<n && z[i+2]==
e0f80 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69  '/' ){.        i
e0f90 20 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63   += 1;.        c
e0fa0 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d  ontinue;.      }
e0fb0 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31  .      if( z[i+1
e0fc0 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 33 3c 6e 20  ]=='.' && i+3<n 
e0fd0 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2e 27 20 26  && z[i+2]=='.' &
e0fe0 26 20 7a 5b 69 2b 33 5d 3d 3d 27 2f 27 20 29 7b  & z[i+3]=='/' ){
e0ff0 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
e1000 6a 3e 30 20 26 26 20 7a 5b 6a 2d 31 5d 21 3d 27  j>0 && z[j-1]!='
e1010 2f 27 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20  /' ){ j--; }.   
e1020 20 20 20 20 20 69 66 28 20 6a 3e 30 20 29 7b 20       if( j>0 ){ 
e1030 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 69  j--; }.        i
e1040 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 63   += 2;.        c
e1050 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d  ontinue;.      }
e1060 0a 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 2b 2b  .    }.    z[j++
e1070 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 7d 0a 20 20  ] = z[i];.  }.  
e1080 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75  z[j] = 0;.  retu
e1090 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  rn j;.}../*.** F
e10a0 69 6e 64 20 61 20 75 6e 69 71 75 65 20 66 69 6c  ind a unique fil
e10b0 65 20 49 44 20 66 6f 72 20 74 68 65 20 67 69 76  e ID for the giv
e10c0 65 6e 20 61 62 73 6f 6c 75 74 65 20 70 61 74 68  en absolute path
e10d0 6e 61 6d 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a  name.  Return.**
e10e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
e10f0 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20  e vxworksFileId 
e1100 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 70 6f  object.  This po
e1110 69 6e 74 65 72 20 69 73 20 74 68 65 20 75 6e 69  inter is the uni
e1120 71 75 65 0a 2a 2a 20 66 69 6c 65 20 49 44 2e 0a  que.** file ID..
e1130 2a 2a 0a 2a 2a 20 54 68 65 20 6e 52 65 66 20 66  **.** The nRef f
e1140 69 65 6c 64 20 6f 66 20 74 68 65 20 76 78 77 6f  ield of the vxwo
e1150 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74  rksFileId object
e1160 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
e1170 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6f 62  before.** the ob
e1180 6a 65 63 74 20 69 73 20 72 65 74 75 72 6e 65 64  ject is returned
e1190 2e 20 20 41 20 6e 65 77 20 76 78 77 6f 72 6b 73  .  A new vxworks
e11a0 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 69 73  FileId object is
e11b0 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20   created.** and 
e11c0 61 64 64 65 64 20 74 6f 20 74 68 65 20 67 6c 6f  added to the glo
e11d0 62 61 6c 20 6c 69 73 74 20 69 66 20 6e 65 63 65  bal list if nece
e11e0 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ssary..**.** If 
e11f0 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
e1200 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ion error occurs
e1210 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a  , return NULL..*
e1220 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20  /.static struct 
e1230 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76  vxworksFileId *v
e1240 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65 49 64  xworksFindFileId
e1250 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 62  (const char *zAb
e1260 73 6f 6c 75 74 65 4e 61 6d 65 29 7b 0a 20 20 73  soluteName){.  s
e1270 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c  truct vxworksFil
e1280 65 49 64 20 2a 70 4e 65 77 3b 20 20 20 20 20 20  eId *pNew;      
e1290 20 20 20 2f 2a 20 73 65 61 72 63 68 20 6b 65 79     /* search key
e12a0 20 61 6e 64 20 6e 65 77 20 66 69 6c 65 20 49 44   and new file ID
e12b0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78 77   */.  struct vxw
e12c0 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 43 61 6e  orksFileId *pCan
e12d0 64 69 64 61 74 65 3b 20 20 20 2f 2a 20 46 6f 72  didate;   /* For
e12e0 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78   looping over ex
e12f0 69 73 74 69 6e 67 20 66 69 6c 65 20 49 44 73 20  isting file IDs 
e1300 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20  */.  int n;     
e1310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1320 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
e1330 74 68 20 6f 66 20 7a 41 62 73 6f 6c 75 74 65 4e  th of zAbsoluteN
e1340 61 6d 65 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20  ame string */.. 
e1350 20 61 73 73 65 72 74 28 20 7a 41 62 73 6f 6c 75   assert( zAbsolu
e1360 74 65 4e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 29  teName[0]=='/' )
e1370 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 29 73 74 72  ;.  n = (int)str
e1380 6c 65 6e 28 7a 41 62 73 6f 6c 75 74 65 4e 61 6d  len(zAbsoluteNam
e1390 65 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c  e);.  pNew = sql
e13a0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a  ite3_malloc( siz
e13b0 65 6f 66 28 2a 70 4e 65 77 29 20 2b 20 28 6e 2b  eof(*pNew) + (n+
e13c0 31 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  1) );.  if( pNew
e13d0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
e13e0 20 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63    pNew->zCanonic
e13f0 61 6c 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29  alName = (char*)
e1400 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63  &pNew[1];.  memc
e1410 70 79 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69  py(pNew->zCanoni
e1420 63 61 6c 4e 61 6d 65 2c 20 7a 41 62 73 6f 6c 75  calName, zAbsolu
e1430 74 65 4e 61 6d 65 2c 20 6e 2b 31 29 3b 0a 20 20  teName, n+1);.  
e1440 6e 20 3d 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c  n = vxworksSimpl
e1450 69 66 79 4e 61 6d 65 28 70 4e 65 77 2d 3e 7a 43  ifyName(pNew->zC
e1460 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29  anonicalName, n)
e1470 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 66  ;..  /* Search f
e1480 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20 65  or an existing e
e1490 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 69  ntry that matchi
e14a0 6e 67 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c  ng the canonical
e14b0 20 6e 61 6d 65 2e 0a 20 20 2a 2a 20 49 66 20 66   name..  ** If f
e14c0 6f 75 6e 64 2c 20 69 6e 63 72 65 6d 65 6e 74 20  ound, increment 
e14d0 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
e14e0 75 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61  unt and return a
e14f0 20 70 6f 69 6e 74 65 72 20 74 6f 0a 20 20 2a 2a   pointer to.  **
e1500 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 66 69   the existing fi
e1510 6c 65 20 49 44 2e 0a 20 20 2a 2f 0a 20 20 75 6e  le ID..  */.  un
e1520 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  ixEnterMutex();.
e1530 20 20 66 6f 72 28 70 43 61 6e 64 69 64 61 74 65    for(pCandidate
e1540 3d 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74  =vxworksFileList
e1550 3b 20 70 43 61 6e 64 69 64 61 74 65 3b 20 70 43  ; pCandidate; pC
e1560 61 6e 64 69 64 61 74 65 3d 70 43 61 6e 64 69 64  andidate=pCandid
e1570 61 74 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ate->pNext){.   
e1580 20 69 66 28 20 70 43 61 6e 64 69 64 61 74 65 2d   if( pCandidate-
e1590 3e 6e 4e 61 6d 65 3d 3d 6e 20 0a 20 20 20 20 20  >nName==n .     
e15a0 26 26 20 6d 65 6d 63 6d 70 28 70 43 61 6e 64 69  && memcmp(pCandi
e15b0 64 61 74 65 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c  date->zCanonical
e15c0 4e 61 6d 65 2c 20 70 4e 65 77 2d 3e 7a 43 61 6e  Name, pNew->zCan
e15d0 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3d 3d  onicalName, n)==
e15e0 30 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20  0.    ){.       
e15f0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65  sqlite3_free(pNe
e1600 77 29 3b 0a 20 20 20 20 20 20 20 70 43 61 6e 64  w);.       pCand
e1610 69 64 61 74 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20  idate->nRef++;. 
e1620 20 20 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d        unixLeaveM
e1630 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 20 72  utex();.       r
e1640 65 74 75 72 6e 20 70 43 61 6e 64 69 64 61 74 65  eturn pCandidate
e1650 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
e1660 2a 20 4e 6f 20 6d 61 74 63 68 20 77 61 73 20 66  * No match was f
e1670 6f 75 6e 64 2e 20 20 57 65 20 77 69 6c 6c 20 6d  ound.  We will m
e1680 61 6b 65 20 61 20 6e 65 77 20 66 69 6c 65 20 49  ake a new file I
e1690 44 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e 6e 52 65  D */.  pNew->nRe
e16a0 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 6e  f = 1;.  pNew->n
e16b0 4e 61 6d 65 20 3d 20 6e 3b 0a 20 20 70 4e 65 77  Name = n;.  pNew
e16c0 2d 3e 70 4e 65 78 74 20 3d 20 76 78 77 6f 72 6b  ->pNext = vxwork
e16d0 73 46 69 6c 65 4c 69 73 74 3b 0a 20 20 76 78 77  sFileList;.  vxw
e16e0 6f 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 70  orksFileList = p
e16f0 4e 65 77 3b 0a 20 20 75 6e 69 78 4c 65 61 76 65  New;.  unixLeave
e1700 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72  Mutex();.  retur
e1710 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pNew;.}../*.**
e1720 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72   Decrement the r
e1730 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f  eference count o
e1740 6e 20 61 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  n a vxworksFileI
e1750 64 20 6f 62 6a 65 63 74 2e 20 20 46 72 65 65 0a  d object.  Free.
e1760 2a 2a 20 74 68 65 20 6f 62 6a 65 63 74 20 77 68  ** the object wh
e1770 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  en the reference
e1780 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a   count reaches z
e1790 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ero..*/.static v
e17a0 6f 69 64 20 76 78 77 6f 72 6b 73 52 65 6c 65 61  oid vxworksRelea
e17b0 73 65 46 69 6c 65 49 64 28 73 74 72 75 63 74 20  seFileId(struct 
e17c0 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70  vxworksFileId *p
e17d0 49 64 29 7b 0a 20 20 75 6e 69 78 45 6e 74 65 72  Id){.  unixEnter
e17e0 4d 75 74 65 78 28 29 3b 0a 20 20 61 73 73 65 72  Mutex();.  asser
e17f0 74 28 20 70 49 64 2d 3e 6e 52 65 66 3e 30 20 29  t( pId->nRef>0 )
e1800 3b 0a 20 20 70 49 64 2d 3e 6e 52 65 66 2d 2d 3b  ;.  pId->nRef--;
e1810 0a 20 20 69 66 28 20 70 49 64 2d 3e 6e 52 65 66  .  if( pId->nRef
e1820 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63  ==0 ){.    struc
e1830 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20  t vxworksFileId 
e1840 2a 2a 70 70 3b 0a 20 20 20 20 66 6f 72 28 70 70  **pp;.    for(pp
e1850 3d 26 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73  =&vxworksFileLis
e1860 74 3b 20 2a 70 70 20 26 26 20 2a 70 70 21 3d 70  t; *pp && *pp!=p
e1870 49 64 3b 20 70 70 20 3d 20 26 28 28 2a 70 70 29  Id; pp = &((*pp)
e1880 2d 3e 70 4e 65 78 74 29 29 7b 7d 0a 20 20 20 20  ->pNext)){}.    
e1890 61 73 73 65 72 74 28 20 2a 70 70 3d 3d 70 49 64  assert( *pp==pId
e18a0 20 29 3b 0a 20 20 20 20 2a 70 70 20 3d 20 70 49   );.    *pp = pI
e18b0 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71  d->pNext;.    sq
e18c0 6c 69 74 65 33 5f 66 72 65 65 28 70 49 64 29 3b  lite3_free(pId);
e18d0 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65  .  }.  unixLeave
e18e0 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 69  Mutex();.}.#endi
e18f0 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20  f /* OS_VXWORKS 
e1900 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
e1910 2a 2a 2a 20 45 6e 64 20 6f 66 20 55 6e 69 71 75  *** End of Uniqu
e1920 65 20 46 69 6c 65 20 49 44 20 55 74 69 6c 69 74  e File ID Utilit
e1930 79 20 55 73 65 64 20 42 79 20 56 78 57 6f 72 6b  y Used By VxWork
e1940 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s **************
e1950 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
e1960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e19a0 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */.../**********
e19b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e19c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e19d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e19e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e19f0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
e1a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1a10 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20   Posix Advisory 
e1a20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a  Locking ********
e1a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1a40 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 58  ****.**.** POSIX
e1a50 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20   advisory locks 
e1a60 61 72 65 20 62 72 6f 6b 65 6e 20 62 79 20 64 65  are broken by de
e1a70 73 69 67 6e 2e 20 20 41 4e 53 49 20 53 54 44 20  sign.  ANSI STD 
e1a80 31 30 30 33 2e 31 20 28 31 39 39 36 29 0a 2a 2a  1003.1 (1996).**
e1a90 20 73 65 63 74 69 6f 6e 20 36 2e 35 2e 32 2e 32   section 6.5.2.2
e1aa0 20 6c 69 6e 65 73 20 34 38 33 20 74 68 72 6f 75   lines 483 throu
e1ab0 67 68 20 34 39 30 20 73 70 65 63 69 66 79 20 74  gh 490 specify t
e1ac0 68 61 74 20 77 68 65 6e 20 61 20 70 72 6f 63 65  hat when a proce
e1ad0 73 73 0a 2a 2a 20 73 65 74 73 20 6f 72 20 63 6c  ss.** sets or cl
e1ae0 65 61 72 73 20 61 20 6c 6f 63 6b 2c 20 74 68 61  ears a lock, tha
e1af0 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 76 65 72  t operation over
e1b00 72 69 64 65 73 20 61 6e 79 20 70 72 69 6f 72 20  rides any prior 
e1b10 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a 20 62 79 20  locks set.** by 
e1b20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73  the same process
e1b30 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 65  .  It does not e
e1b40 78 70 6c 69 63 69 74 6c 79 20 73 61 79 20 73 6f  xplicitly say so
e1b50 2c 20 62 75 74 20 74 68 69 73 20 69 6d 70 6c 69  , but this impli
e1b60 65 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 6f 76  es.** that it ov
e1b70 65 72 72 69 64 65 73 20 6c 6f 63 6b 73 20 73 65  errides locks se
e1b80 74 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 72  t by the same pr
e1b90 6f 63 65 73 73 20 75 73 69 6e 67 20 61 20 64 69  ocess using a di
e1ba0 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20  fferent.** file 
e1bb0 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43 6f 6e  descriptor.  Con
e1bc0 73 69 64 65 72 20 74 68 69 73 20 74 65 73 74 20  sider this test 
e1bd0 63 61 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  case:.**.**     
e1be0 20 20 69 6e 74 20 66 64 31 20 3d 20 6f 70 65 6e    int fd1 = open
e1bf0 28 22 2e 2f 66 69 6c 65 31 22 2c 20 4f 5f 52 44  ("./file1", O_RD
e1c00 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34  WR|O_CREAT, 0644
e1c10 29 3b 0a 2a 2a 20 20 20 20 20 20 20 69 6e 74 20  );.**       int 
e1c20 66 64 32 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69  fd2 = open("./fi
e1c30 6c 65 32 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43  le2", O_RDWR|O_C
e1c40 52 45 41 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 0a  REAT, 0644);.**.
e1c50 2a 2a 20 53 75 70 70 6f 73 65 20 2e 2f 66 69 6c  ** Suppose ./fil
e1c60 65 31 20 61 6e 64 20 2e 2f 66 69 6c 65 32 20 61  e1 and ./file2 a
e1c70 72 65 20 72 65 61 6c 6c 79 20 74 68 65 20 73 61  re really the sa
e1c80 6d 65 20 66 69 6c 65 20 28 62 65 63 61 75 73 65  me file (because
e1c90 0a 2a 2a 20 6f 6e 65 20 69 73 20 61 20 68 61 72  .** one is a har
e1ca0 64 20 6f 72 20 73 79 6d 62 6f 6c 69 63 20 6c 69  d or symbolic li
e1cb0 6e 6b 20 74 6f 20 74 68 65 20 6f 74 68 65 72 29  nk to the other)
e1cc0 20 74 68 65 6e 20 69 66 20 79 6f 75 20 73 65 74   then if you set
e1cd0 0a 2a 2a 20 61 6e 20 65 78 63 6c 75 73 69 76 65  .** an exclusive
e1ce0 20 6c 6f 63 6b 20 6f 6e 20 66 64 31 2c 20 74 68   lock on fd1, th
e1cf0 65 6e 20 74 72 79 20 74 6f 20 67 65 74 20 61 6e  en try to get an
e1d00 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a   exclusive lock.
e1d10 2a 2a 20 6f 6e 20 66 64 32 2c 20 69 74 20 77 6f  ** on fd2, it wo
e1d20 72 6b 73 2e 20 20 49 20 77 6f 75 6c 64 20 68 61  rks.  I would ha
e1d30 76 65 20 65 78 70 65 63 74 65 64 20 74 68 65 20  ve expected the 
e1d40 73 65 63 6f 6e 64 20 6c 6f 63 6b 20 74 6f 0a 2a  second lock to.*
e1d50 2a 20 66 61 69 6c 20 73 69 6e 63 65 20 74 68 65  * fail since the
e1d60 72 65 20 77 61 73 20 61 6c 72 65 61 64 79 20 61  re was already a
e1d70 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c   lock on the fil
e1d80 65 20 64 75 65 20 74 6f 20 66 64 31 2e 0a 2a 2a  e due to fd1..**
e1d90 20 42 75 74 20 6e 6f 74 20 73 6f 2e 20 20 53 69   But not so.  Si
e1da0 6e 63 65 20 62 6f 74 68 20 6c 6f 63 6b 73 20 63  nce both locks c
e1db0 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 73 61 6d  ame from the sam
e1dc0 65 20 70 72 6f 63 65 73 73 2c 20 74 68 65 0a 2a  e process, the.*
e1dd0 2a 20 73 65 63 6f 6e 64 20 6f 76 65 72 72 69 64  * second overrid
e1de0 65 73 20 74 68 65 20 66 69 72 73 74 2c 20 65 76  es the first, ev
e1df0 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79 20 77  en though they w
e1e00 65 72 65 20 6f 6e 20 64 69 66 66 65 72 65 6e 74  ere on different
e1e10 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70  .** file descrip
e1e20 74 6f 72 73 20 6f 70 65 6e 65 64 20 6f 6e 20 64  tors opened on d
e1e30 69 66 66 65 72 65 6e 74 20 66 69 6c 65 20 6e 61  ifferent file na
e1e40 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  mes..**.** This 
e1e50 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 63 61  means that we ca
e1e60 6e 6e 6f 74 20 75 73 65 20 50 4f 53 49 58 20 6c  nnot use POSIX l
e1e70 6f 63 6b 73 20 74 6f 20 73 79 6e 63 68 72 6f 6e  ocks to synchron
e1e80 69 7a 65 20 66 69 6c 65 20 61 63 63 65 73 73 0a  ize file access.
e1e90 2a 2a 20 61 6d 6f 6e 67 20 63 6f 6d 70 65 74 69  ** among competi
e1ea0 6e 67 20 74 68 72 65 61 64 73 20 6f 66 20 74 68  ng threads of th
e1eb0 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20  e same process. 
e1ec0 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 77 69 6c   POSIX locks wil
e1ed0 6c 20 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20 74  l work fine.** t
e1ee0 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20 61 63  o synchronize ac
e1ef0 63 65 73 73 20 66 6f 72 20 74 68 72 65 61 64 73  cess for threads
e1f00 20 69 6e 20 73 65 70 61 72 61 74 65 20 70 72 6f   in separate pro
e1f10 63 65 73 73 65 73 2c 20 62 75 74 20 6e 6f 74 0a  cesses, but not.
e1f20 2a 2a 20 74 68 72 65 61 64 73 20 77 69 74 68 69  ** threads withi
e1f30 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65  n the same proce
e1f40 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72  ss..**.** To wor
e1f50 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70 72 6f  k around the pro
e1f60 62 6c 65 6d 2c 20 53 51 4c 69 74 65 20 68 61 73  blem, SQLite has
e1f70 20 74 6f 20 6d 61 6e 61 67 65 20 66 69 6c 65 20   to manage file 
e1f80 6c 6f 63 6b 73 20 69 6e 74 65 72 6e 61 6c 6c 79  locks internally
e1f90 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 20  .** on its own. 
e1fa0 20 57 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20   Whenever a new 
e1fb0 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e  database is open
e1fc0 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66  ed, we have to f
e1fd0 69 6e 64 20 74 68 65 0a 2a 2a 20 73 70 65 63 69  ind the.** speci
e1fe0 66 69 63 20 69 6e 6f 64 65 20 6f 66 20 74 68 65  fic inode of the
e1ff0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28   database file (
e2000 74 68 65 20 69 6e 6f 64 65 20 69 73 20 64 65 74  the inode is det
e2010 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 0a 2a  ermined by the.*
e2020 2a 20 73 74 5f 64 65 76 20 61 6e 64 20 73 74 5f  * st_dev and st_
e2030 69 6e 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68  ino fields of th
e2040 65 20 73 74 61 74 20 73 74 72 75 63 74 75 72 65  e stat structure
e2050 20 74 68 61 74 20 66 73 74 61 74 28 29 20 66 69   that fstat() fi
e2060 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61 6e 64 20 63  lls in).** and c
e2070 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 73 20 61  heck for locks a
e2080 6c 72 65 61 64 79 20 65 78 69 73 74 69 6e 67 20  lready existing 
e2090 6f 6e 20 74 68 61 74 20 69 6e 6f 64 65 2e 20 20  on that inode.  
e20a0 57 68 65 6e 20 6c 6f 63 6b 73 20 61 72 65 0a 2a  When locks are.*
e20b0 2a 20 63 72 65 61 74 65 64 20 6f 72 20 72 65 6d  * created or rem
e20c0 6f 76 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f  oved, we have to
e20d0 20 6c 6f 6f 6b 20 61 74 20 6f 75 72 20 6f 77 6e   look at our own
e20e0 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 64   internal record
e20f0 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 73   of the.** locks
e2100 20 74 6f 20 73 65 65 20 69 66 20 61 6e 6f 74 68   to see if anoth
e2110 65 72 20 74 68 72 65 61 64 20 68 61 73 20 70 72  er thread has pr
e2120 65 76 69 6f 75 73 6c 79 20 73 65 74 20 61 20 6c  eviously set a l
e2130 6f 63 6b 20 6f 6e 20 74 68 61 74 20 73 61 6d 65  ock on that same
e2140 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a  .** inode..**.**
e2150 20 28 41 73 69 64 65 3a 20 54 68 65 20 75 73 65   (Aside: The use
e2160 20 6f 66 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72   of inode number
e2170 73 20 61 73 20 75 6e 69 71 75 65 20 49 44 73 20  s as unique IDs 
e2180 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e  does not work on
e2190 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 46 6f 72   VxWorks..** For
e21a0 20 56 78 57 6f 72 6b 73 2c 20 77 65 20 68 61 76   VxWorks, we hav
e21b0 65 20 74 6f 20 75 73 65 20 74 68 65 20 61 6c 74  e to use the alt
e21c0 65 72 6e 61 74 69 76 65 20 75 6e 69 71 75 65 20  ernative unique 
e21d0 49 44 20 73 79 73 74 65 6d 20 62 61 73 65 64 20  ID system based 
e21e0 6f 6e 0a 2a 2a 20 63 61 6e 6f 6e 69 63 61 6c 20  on.** canonical 
e21f0 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6d 70  filename and imp
e2200 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 68 65 20  lemented in the 
e2210 70 72 65 76 69 6f 75 73 20 64 69 76 69 73 69 6f  previous divisio
e2220 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  n.).**.** The sq
e2230 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63  lite3_file struc
e2240 74 75 72 65 20 66 6f 72 20 50 4f 53 49 58 20 69  ture for POSIX i
e2250 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6a 75 73 74  s no longer just
e2260 20 61 6e 20 69 6e 74 65 67 65 72 20 66 69 6c 65   an integer file
e2270 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 2e 20  .** descriptor. 
e2280 20 49 74 20 69 73 20 6e 6f 77 20 61 20 73 74 72   It is now a str
e2290 75 63 74 75 72 65 20 74 68 61 74 20 68 6f 6c 64  ucture that hold
e22a0 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 66 69  s the integer fi
e22b0 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72  le.** descriptor
e22c0 20 61 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 74   and a pointer t
e22d0 6f 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68  o a structure th
e22e0 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65  at describes the
e22f0 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 6c 6f 63   internal.** loc
e2300 6b 73 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 73  ks on the corres
e2310 70 6f 6e 64 69 6e 67 20 69 6e 6f 64 65 2e 20 20  ponding inode.  
e2320 54 68 65 72 65 20 69 73 20 6f 6e 65 20 6c 6f 63  There is one loc
e2330 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a  king structure.*
e2340 2a 20 70 65 72 20 69 6e 6f 64 65 2c 20 73 6f 20  * per inode, so 
e2350 69 66 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64  if the same inod
e2360 65 20 69 73 20 6f 70 65 6e 65 64 20 74 77 69 63  e is opened twic
e2370 65 2c 20 62 6f 74 68 20 75 6e 69 78 46 69 6c 65  e, both unixFile
e2380 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 70   structures.** p
e2390 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 61 6d 65  oint to the same
e23a0 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75   locking structu
e23b0 72 65 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67  re.  The locking
e23c0 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73   structure keeps
e23d0 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 65 20  .** a reference 
e23e0 63 6f 75 6e 74 20 28 73 6f 20 77 65 20 77 69 6c  count (so we wil
e23f0 6c 20 6b 6e 6f 77 20 77 68 65 6e 20 74 6f 20 64  l know when to d
e2400 65 6c 65 74 65 20 69 74 29 20 61 6e 64 20 61 20  elete it) and a 
e2410 22 63 6e 74 22 0a 2a 2a 20 66 69 65 6c 64 20 74  "cnt".** field t
e2420 68 61 74 20 74 65 6c 6c 73 20 75 73 20 69 74 73  hat tells us its
e2430 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73   internal lock s
e2440 74 61 74 75 73 2e 20 20 63 6e 74 3d 3d 30 20 6d  tatus.  cnt==0 m
e2450 65 61 6e 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65  eans the.** file
e2460 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e 20 20 63   is unlocked.  c
e2470 6e 74 3d 3d 2d 31 20 6d 65 61 6e 73 20 74 68 65  nt==-1 means the
e2480 20 66 69 6c 65 20 68 61 73 20 61 6e 20 65 78 63   file has an exc
e2490 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 0a 2a 2a 20  lusive lock..** 
e24a0 63 6e 74 3e 30 20 6d 65 61 6e 73 20 74 68 65 72  cnt>0 means ther
e24b0 65 20 61 72 65 20 63 6e 74 20 73 68 61 72 65 64  e are cnt shared
e24c0 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 66 69   locks on the fi
e24d0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 61 74  le..**.** Any at
e24e0 74 65 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 6f 72  tempt to lock or
e24f0 20 75 6e 6c 6f 63 6b 20 61 20 66 69 6c 65 20 66   unlock a file f
e2500 69 72 73 74 20 63 68 65 63 6b 73 20 74 68 65 20  irst checks the 
e2510 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 75 63  locking.** struc
e2520 74 75 72 65 2e 20 20 54 68 65 20 66 63 6e 74 6c  ture.  The fcntl
e2530 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 69  () system call i
e2540 73 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 64 20 74  s only invoked t
e2550 6f 20 73 65 74 20 61 20 0a 2a 2a 20 50 4f 53 49  o set a .** POSI
e2560 58 20 6c 6f 63 6b 20 69 66 20 74 68 65 20 69 6e  X lock if the in
e2570 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 72 75  ternal lock stru
e2580 63 74 75 72 65 20 74 72 61 6e 73 69 74 69 6f 6e  cture transition
e2590 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 20 6c  s between.** a l
e25a0 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20 75 6e 6c  ocked and an unl
e25b0 6f 63 6b 65 64 20 73 74 61 74 65 2e 0a 2a 2a 0a  ocked state..**.
e25c0 2a 2a 20 42 75 74 20 77 61 69 74 3a 20 20 74 68  ** But wait:  th
e25d0 65 72 65 20 61 72 65 20 79 65 74 20 6d 6f 72 65  ere are yet more
e25e0 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 50   problems with P
e25f0 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f  OSIX advisory lo
e2600 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 79 6f  cks..**.** If yo
e2610 75 20 63 6c 6f 73 65 20 61 20 66 69 6c 65 20 64  u close a file d
e2620 65 73 63 72 69 70 74 6f 72 20 74 68 61 74 20 70  escriptor that p
e2630 6f 69 6e 74 73 20 74 6f 20 61 20 66 69 6c 65 20  oints to a file 
e2640 74 68 61 74 20 68 61 73 20 6c 6f 63 6b 73 2c 0a  that has locks,.
e2650 2a 2a 20 61 6c 6c 20 6c 6f 63 6b 73 20 6f 6e 20  ** all locks on 
e2660 74 68 61 74 20 66 69 6c 65 20 74 68 61 74 20 61  that file that a
e2670 72 65 20 6f 77 6e 65 64 20 62 79 20 74 68 65 20  re owned by the 
e2680 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20  current process 
e2690 61 72 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 2e  are.** released.
e26a0 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64    To work around
e26b0 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 2c 20 65   this problem, e
e26c0 61 63 68 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66  ach unixInodeInf
e26d0 6f 20 6f 62 6a 65 63 74 0a 2a 2a 20 6d 61 69 6e  o object.** main
e26e0 74 61 69 6e 73 20 61 20 63 6f 75 6e 74 20 6f 66  tains a count of
e26f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
e2700 65 6e 64 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20  ending locks on 
e2710 74 68 61 20 69 6e 6f 64 65 2e 0a 2a 2a 20 57 68  tha inode..** Wh
e2720 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73  en an attempt is
e2730 20 6d 61 64 65 20 74 6f 20 63 6c 6f 73 65 20 61   made to close a
e2740 6e 20 75 6e 69 78 46 69 6c 65 2c 20 69 66 20 74  n unixFile, if t
e2750 68 65 72 65 20 61 72 65 0a 2a 2a 20 6f 74 68 65  here are.** othe
e2760 72 20 75 6e 69 78 46 69 6c 65 20 6f 70 65 6e 20  r unixFile open 
e2770 6f 6e 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64  on the same inod
e2780 65 20 74 68 61 74 20 61 72 65 20 68 6f 6c 64 69  e that are holdi
e2790 6e 67 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 61  ng locks, the ca
e27a0 6c 6c 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 28 29  ll.** to close()
e27b0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
e27c0 70 74 6f 72 20 69 73 20 64 65 66 65 72 72 65 64  ptor is deferred
e27d0 20 75 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 74 68   until all of th
e27e0 65 20 6c 6f 63 6b 73 20 63 6c 65 61 72 2e 0a 2a  e locks clear..*
e27f0 2a 20 54 68 65 20 75 6e 69 78 49 6e 6f 64 65 49  * The unixInodeI
e2800 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 6b 65  nfo structure ke
e2810 65 70 73 20 61 20 6c 69 73 74 20 6f 66 20 66 69  eps a list of fi
e2820 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 74  le descriptors t
e2830 68 61 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 62  hat need to.** b
e2840 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 61  e closed and tha
e2850 74 20 6c 69 73 74 20 69 73 20 77 61 6c 6b 65 64  t list is walked
e2860 20 28 61 6e 64 20 63 6c 65 61 72 65 64 29 20 77   (and cleared) w
e2870 68 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63  hen the last loc
e2880 6b 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a  k.** clears..**.
e2890 2a 2a 20 59 65 74 20 61 6e 6f 74 68 65 72 20 70  ** Yet another p
e28a0 72 6f 62 6c 65 6d 3a 20 20 4c 69 6e 75 78 54 68  roblem:  LinuxTh
e28b0 72 65 61 64 73 20 64 6f 20 6e 6f 74 20 70 6c 61  reads do not pla
e28c0 79 20 77 65 6c 6c 20 77 69 74 68 20 70 6f 73 69  y well with posi
e28d0 78 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4d  x locks..**.** M
e28e0 61 6e 79 20 6f 6c 64 65 72 20 76 65 72 73 69 6f  any older versio
e28f0 6e 73 20 6f 66 20 6c 69 6e 75 78 20 75 73 65 20  ns of linux use 
e2900 74 68 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73  the LinuxThreads
e2910 20 6c 69 62 72 61 72 79 20 77 68 69 63 68 20 69   library which i
e2920 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 69 78 20 63  s.** not posix c
e2930 6f 6d 70 6c 69 61 6e 74 2e 20 20 55 6e 64 65 72  ompliant.  Under
e2940 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2c 20 61   LinuxThreads, a
e2950 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20 62 79   lock created by
e2960 20 74 68 72 65 61 64 0a 2a 2a 20 41 20 63 61 6e   thread.** A can
e2970 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20  not be modified 
e2980 6f 72 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79  or overridden by
e2990 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 72   a different thr
e29a0 65 61 64 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 74  ead B..** Only t
e29b0 68 72 65 61 64 20 41 20 63 61 6e 20 6d 6f 64 69  hread A can modi
e29c0 66 79 20 74 68 65 20 6c 6f 63 6b 2e 20 20 4c 6f  fy the lock.  Lo
e29d0 63 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 20 69  cking behavior i
e29e0 73 20 63 6f 72 72 65 63 74 0a 2a 2a 20 69 66 20  s correct.** if 
e29f0 74 68 65 20 61 70 70 6c 69 61 74 69 6f 6e 20 75  the appliation u
e2a00 73 65 73 20 74 68 65 20 6e 65 77 65 72 20 4e 61  ses the newer Na
e2a10 74 69 76 65 20 50 6f 73 69 78 20 54 68 72 65 61  tive Posix Threa
e2a20 64 20 4c 69 62 72 61 72 79 20 28 4e 50 54 4c 29  d Library (NPTL)
e2a30 0a 2a 2a 20 6f 6e 20 6c 69 6e 75 78 20 2d 20 77  .** on linux - w
e2a40 69 74 68 20 4e 50 54 4c 20 61 20 6c 6f 63 6b 20  ith NPTL a lock 
e2a50 63 72 65 61 74 65 64 20 62 79 20 74 68 72 65 61  created by threa
e2a60 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65  d A can override
e2a70 20 6c 6f 63 6b 73 0a 2a 2a 20 69 6e 20 74 68 72   locks.** in thr
e2a80 65 61 64 20 42 2e 20 20 42 75 74 20 74 68 65 72  ead B.  But ther
e2a90 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b  e is no way to k
e2aa0 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  now at compile-t
e2ab0 69 6d 65 20 77 68 69 63 68 0a 2a 2a 20 74 68 72  ime which.** thr
e2ac0 65 61 64 69 6e 67 20 6c 69 62 72 61 72 79 20 69  eading library i
e2ad0 73 20 62 65 69 6e 67 20 75 73 65 64 2e 20 20 53  s being used.  S
e2ae0 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61  o there is no wa
e2af0 79 20 74 6f 20 6b 6e 6f 77 20 61 74 0a 2a 2a 20  y to know at.** 
e2b00 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 65  compile-time whe
e2b10 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65  ther or not thre
e2b20 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64  ad A can overrid
e2b30 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 72 65 61  e locks on threa
e2b40 64 20 42 2e 0a 2a 2a 20 4f 6e 65 20 68 61 73 20  d B..** One has 
e2b50 74 6f 20 64 6f 20 61 20 72 75 6e 2d 74 69 6d 65  to do a run-time
e2b60 20 63 68 65 63 6b 20 74 6f 20 64 69 73 63 6f 76   check to discov
e2b70 65 72 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  er the behavior 
e2b80 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  of the.** curren
e2b90 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a  t process..**.**
e2ba0 20 53 51 4c 69 74 65 20 75 73 65 64 20 74 6f 20   SQLite used to 
e2bb0 73 75 70 70 6f 72 74 20 4c 69 6e 75 78 54 68 72  support LinuxThr
e2bc0 65 61 64 73 2e 20 20 42 75 74 20 73 75 70 70 6f  eads.  But suppo
e2bd0 72 74 20 66 6f 72 20 4c 69 6e 75 78 54 68 72 65  rt for LinuxThre
e2be0 61 64 73 0a 2a 2a 20 77 61 73 20 64 72 6f 70 70  ads.** was dropp
e2bf0 65 64 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  ed beginning wit
e2c00 68 20 76 65 72 73 69 6f 6e 20 33 2e 37 2e 30 2e  h version 3.7.0.
e2c10 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 73 74    SQLite will st
e2c20 69 6c 6c 20 77 6f 72 6b 20 77 69 74 68 0a 2a 2a  ill work with.**
e2c30 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 70 72   LinuxThreads pr
e2c40 6f 76 69 64 65 64 20 74 68 61 74 20 28 31 29 20  ovided that (1) 
e2c50 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 6f 72 65  there is no more
e2c60 20 74 68 61 6e 20 6f 6e 65 20 63 6f 6e 6e 65 63   than one connec
e2c70 74 69 6f 6e 20 0a 2a 2a 20 70 65 72 20 64 61 74  tion .** per dat
e2c80 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 74 68  abase file in th
e2c90 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 61  e same process a
e2ca0 6e 64 20 28 32 29 20 64 61 74 61 62 61 73 65 20  nd (2) database 
e2cb0 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a 2a 20 64  connections.** d
e2cc0 6f 20 6e 6f 74 20 6d 6f 76 65 20 61 63 72 6f 73  o not move acros
e2cd0 73 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 0a 2f  s threads..*/../
e2ce0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
e2cf0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
e2d00 67 20 73 74 72 75 63 74 75 72 65 20 73 65 72 76  g structure serv
e2d10 65 73 20 61 73 20 74 68 65 20 6b 65 79 20 75 73  es as the key us
e2d20 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20  ed.** to locate 
e2d30 61 20 70 61 72 74 69 63 75 6c 61 72 20 75 6e 69  a particular uni
e2d40 78 49 6e 6f 64 65 49 6e 66 6f 20 6f 62 6a 65 63  xInodeInfo objec
e2d50 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69  t..*/.struct uni
e2d60 78 46 69 6c 65 49 64 20 7b 0a 20 20 64 65 76 5f  xFileId {.  dev_
e2d70 74 20 64 65 76 3b 20 20 20 20 20 20 20 20 20 20  t dev;          
e2d80 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63          /* Devic
e2d90 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 69 66 20  e number */.#if 
e2da0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72  OS_VXWORKS.  str
e2db0 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  uct vxworksFileI
e2dc0 64 20 2a 70 49 64 3b 20 20 2f 2a 20 55 6e 69 71  d *pId;  /* Uniq
e2dd0 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72 20 76  ue file ID for v
e2de0 78 77 6f 72 6b 73 2e 20 2a 2f 0a 23 65 6c 73 65  xworks. */.#else
e2df0 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20  .  ino_t ino;   
e2e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e2e10 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a  * Inode number *
e2e20 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a  /.#endif.};../*.
e2e30 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
e2e40 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
e2e50 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c  structure is all
e2e60 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20  ocated for each 
e2e70 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20  open.** inode.  
e2e80 4f 72 2c 20 6f 6e 20 4c 69 6e 75 78 54 68 72 65  Or, on LinuxThre
e2e90 61 64 73 2c 20 74 68 65 72 65 20 69 73 20 6f 6e  ads, there is on
e2ea0 65 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63  e of these struc
e2eb0 74 75 72 65 73 20 66 6f 72 0a 2a 2a 20 65 61 63  tures for.** eac
e2ec0 68 20 69 6e 6f 64 65 20 6f 70 65 6e 65 64 20 62  h inode opened b
e2ed0 79 20 65 61 63 68 20 74 68 72 65 61 64 2e 0a 2a  y each thread..*
e2ee0 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e  *.** A single in
e2ef0 6f 64 65 20 63 61 6e 20 68 61 76 65 20 6d 75 6c  ode can have mul
e2f00 74 69 70 6c 65 20 66 69 6c 65 20 64 65 73 63 72  tiple file descr
e2f10 69 70 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 20  iptors, so each 
e2f20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75  unixFile.** stru
e2f30 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61  cture contains a
e2f40 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69   pointer to an i
e2f50 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
e2f60 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 69 73 0a  object and this.
e2f70 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 20  ** object keeps 
e2f80 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e  a count of the n
e2f90 75 6d 62 65 72 20 6f 66 20 75 6e 69 78 46 69 6c  umber of unixFil
e2fa0 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74  e pointing to it
e2fb0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78  ..*/.struct unix
e2fc0 49 6e 6f 64 65 49 6e 66 6f 20 7b 0a 20 20 73 74  InodeInfo {.  st
e2fd0 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20  ruct unixFileId 
e2fe0 66 69 6c 65 49 64 3b 20 20 20 20 20 20 20 2f 2a  fileId;       /*
e2ff0 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20   The lookup key 
e3000 2a 2f 0a 20 20 69 6e 74 20 6e 53 68 61 72 65 64  */.  int nShared
e3010 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
e3020 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
e3030 66 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 68  f SHARED locks h
e3040 65 6c 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  eld */.  unsigne
e3050 64 20 63 68 61 72 20 65 46 69 6c 65 4c 6f 63 6b  d char eFileLock
e3060 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20  ;        /* One 
e3070 6f 66 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20  of SHARED_LOCK, 
e3080 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 65 74  RESERVED_LOCK et
e3090 63 2e 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  c. */.  unsigned
e30a0 20 63 68 61 72 20 62 50 72 6f 63 65 73 73 4c 6f   char bProcessLo
e30b0 63 6b 3b 20 20 20 20 20 2f 2a 20 41 6e 20 65 78  ck;     /* An ex
e30c0 63 6c 75 73 69 76 65 20 70 72 6f 63 65 73 73 20  clusive process 
e30d0 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 2a 2f 0a  lock is held */.
e30e0 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20    int nRef;     
e30f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3100 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
e3110 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20  ointers to this 
e3120 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 75  structure */.  u
e3130 6e 69 78 53 68 6d 4e 6f 64 65 20 2a 70 53 68 6d  nixShmNode *pShm
e3140 4e 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f  Node;          /
e3150 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  * Shared memory 
e3160 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
e3170 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20  this inode */.  
e3180 69 6e 74 20 6e 4c 6f 63 6b 3b 20 20 20 20 20 20  int nLock;      
e3190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e31a0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75 74  /* Number of out
e31b0 73 74 61 6e 64 69 6e 67 20 66 69 6c 65 20 6c 6f  standing file lo
e31c0 63 6b 73 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75  cks */.  UnixUnu
e31d0 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20  sedFd *pUnused; 
e31e0 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 75 73           /* Unus
e31f0 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ed file descript
e3200 6f 72 73 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a  ors to close */.
e3210 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20    unixInodeInfo 
e3220 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
e3230 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c    /* List of all
e3240 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 6f   unixInodeInfo o
e3250 62 6a 65 63 74 73 20 2a 2f 0a 20 20 75 6e 69 78  bjects */.  unix
e3260 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 50 72 65 76  InodeInfo *pPrev
e3270 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20  ;           /*  
e3280 20 20 2e 2e 2e 2e 20 64 6f 75 62 6c 79 20 6c 69    .... doubly li
e3290 6e 6b 65 64 20 2a 2f 0a 23 69 66 20 53 51 4c 49  nked */.#if SQLI
e32a0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
e32b0 47 5f 53 54 59 4c 45 0a 20 20 75 6e 73 69 67 6e  G_STYLE.  unsign
e32c0 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61  ed long long sha
e32d0 72 65 64 42 79 74 65 3b 20 20 2f 2a 20 66 6f 72  redByte;  /* for
e32e0 20 41 46 50 20 73 69 6d 75 6c 61 74 65 64 20 73   AFP simulated s
e32f0 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 23 65  hared lock */.#e
e3300 6e 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57 4f  ndif.#if OS_VXWO
e3310 52 4b 53 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65  RKS.  sem_t *pSe
e3320 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
e3330 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 64 20 50        /* Named P
e3340 4f 53 49 58 20 73 65 6d 61 70 68 6f 72 65 20 2a  OSIX semaphore *
e3350 2f 0a 20 20 63 68 61 72 20 61 53 65 6d 4e 61 6d  /.  char aSemNam
e3360 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 32  e[MAX_PATHNAME+2
e3370 5d 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  ];  /* Name of t
e3380 68 61 74 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f  hat semaphore */
e3390 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a  .#endif.};../*.*
e33a0 2a 20 41 20 6c 69 73 74 73 20 6f 66 20 61 6c 6c  * A lists of all
e33b0 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 6f   unixInodeInfo o
e33c0 62 6a 65 63 74 73 2e 0a 2a 2f 0a 73 74 61 74 69  bjects..*/.stati
e33d0 63 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20  c unixInodeInfo 
e33e0 2a 69 6e 6f 64 65 4c 69 73 74 20 3d 20 30 3b 0a  *inodeList = 0;.
e33f0 0a 2f 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ./*.**.** This f
e3400 75 6e 63 74 69 6f 6e 20 2d 20 75 6e 69 78 4c 6f  unction - unixLo
e3410 67 45 72 72 6f 72 5f 78 28 29 2c 20 69 73 20 6f  gError_x(), is o
e3420 6e 6c 79 20 65 76 65 72 20 63 61 6c 6c 65 64 20  nly ever called 
e3430 76 69 61 20 74 68 65 20 6d 61 63 72 6f 0a 2a 2a  via the macro.**
e3440 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 29 2e   unixLogError().
e3450 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6e 76  .**.** It is inv
e3460 6f 6b 65 64 20 61 66 74 65 72 20 61 6e 20 65 72  oked after an er
e3470 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 61 6e  ror occurs in an
e3480 20 4f 53 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64   OS function and
e3490 20 65 72 72 6e 6f 20 68 61 73 20 62 65 65 6e 0a   errno has been.
e34a0 2a 2a 20 73 65 74 2e 20 49 74 20 6c 6f 67 73 20  ** set. It logs 
e34b0 61 20 6d 65 73 73 61 67 65 20 75 73 69 6e 67 20  a message using 
e34c0 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 20 63 6f  sqlite3_log() co
e34d0 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 75 72  ntaining the cur
e34e0 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 0a 2a 2a  rent value of.**
e34f0 20 65 72 72 6e 6f 20 61 6e 64 2c 20 69 66 20 70   errno and, if p
e3500 6f 73 73 69 62 6c 65 2c 20 74 68 65 20 68 75 6d  ossible, the hum
e3510 61 6e 2d 72 65 61 64 61 62 6c 65 20 65 71 75 69  an-readable equi
e3520 76 61 6c 65 6e 74 20 66 72 6f 6d 20 73 74 72 65  valent from stre
e3530 72 72 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 74 72  rror() or.** str
e3540 65 72 72 6f 72 5f 72 28 29 2e 0a 2a 2a 0a 2a 2a  error_r()..**.**
e3550 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
e3560 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 74 68  ent passed to th
e3570 65 20 6d 61 63 72 6f 20 73 68 6f 75 6c 64 20 62  e macro should b
e3580 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  e the error code
e3590 20 74 68 61 74 0a 2a 2a 20 77 69 6c 6c 20 62 65   that.** will be
e35a0 20 72 65 74 75 72 6e 65 64 20 74 6f 20 53 51 4c   returned to SQL
e35b0 69 74 65 20 28 65 2e 67 2e 20 53 51 4c 49 54 45  ite (e.g. SQLITE
e35c0 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 2c 20 53  _IOERR_DELETE, S
e35d0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 29 2e  QLITE_CANTOPEN).
e35e0 20 0a 2a 2a 20 54 68 65 20 74 77 6f 20 73 75 62   .** The two sub
e35f0 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74  sequent argument
e3600 73 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20  s should be the 
e3610 6e 61 6d 65 20 6f 66 20 74 68 65 20 4f 53 20 66  name of the OS f
e3620 75 6e 63 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20  unction that.** 
e3630 66 61 69 6c 65 64 20 28 65 2e 67 2e 20 22 75 6e  failed (e.g. "un
e3640 6c 69 6e 6b 22 2c 20 22 6f 70 65 6e 22 29 20 61  link", "open") a
e3650 6e 64 20 74 68 65 20 61 73 73 6f 63 69 61 74 65  nd the associate
e3660 64 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 70 61  d file-system pa
e3670 74 68 2c 0a 2a 2a 20 69 66 20 61 6e 79 2e 0a 2a  th,.** if any..*
e3680 2f 0a 23 64 65 66 69 6e 65 20 75 6e 69 78 4c 6f  /.#define unixLo
e3690 67 45 72 72 6f 72 28 61 2c 62 2c 63 29 20 20 20  gError(a,b,c)   
e36a0 20 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 41 74    unixLogErrorAt
e36b0 4c 69 6e 65 28 61 2c 62 2c 63 2c 5f 5f 4c 49 4e  Line(a,b,c,__LIN
e36c0 45 5f 5f 29 0a 73 74 61 74 69 63 20 69 6e 74 20  E__).static int 
e36d0 75 6e 69 78 4c 6f 67 45 72 72 6f 72 41 74 4c 69  unixLogErrorAtLi
e36e0 6e 65 28 0a 20 20 69 6e 74 20 65 72 72 63 6f 64  ne(.  int errcod
e36f0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
e3700 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20        /* SQLite 
e3710 65 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a 20 20  error code */.  
e3720 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e  const char *zFun
e3730 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
e3740 2f 2a 20 4e 61 6d 65 20 6f 66 20 4f 53 20 66 75  /* Name of OS fu
e3750 6e 63 74 69 6f 6e 20 74 68 61 74 20 66 61 69 6c  nction that fail
e3760 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ed */.  const ch
e3770 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20  ar *zPath,      
e3780 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20          /* File 
e3790 70 61 74 68 20 61 73 73 6f 63 69 61 74 65 64 20  path associated 
e37a0 77 69 74 68 20 65 72 72 6f 72 20 2a 2f 0a 20 20  with error */.  
e37b0 69 6e 74 20 69 4c 69 6e 65 20 20 20 20 20 20 20  int iLine       
e37c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e37d0 2f 2a 20 53 6f 75 72 63 65 20 6c 69 6e 65 20 6e  /* Source line n
e37e0 75 6d 62 65 72 20 77 68 65 72 65 20 65 72 72 6f  umber where erro
e37f0 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 29 7b  r occurred */.){
e3800 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 20 20  .  char *zErr;  
e3810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3820 20 20 20 2f 2a 20 4d 65 73 73 61 67 65 20 66 72     /* Message fr
e3830 6f 6d 20 73 74 72 65 72 72 6f 72 28 29 20 6f 72  om strerror() or
e3840 20 65 71 75 69 76 61 6c 65 6e 74 20 2a 2f 0a 20   equivalent */. 
e3850 20 69 6e 74 20 69 45 72 72 6e 6f 20 3d 20 65 72   int iErrno = er
e3860 72 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  rno;            
e3870 20 2f 2a 20 53 61 76 65 64 20 73 79 73 63 61 6c   /* Saved syscal
e3880 6c 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 20 2a  l error number *
e3890 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  /..  /* If this 
e38a0 69 73 20 6e 6f 74 20 61 20 74 68 72 65 61 64 73  is not a threads
e38b0 61 66 65 20 62 75 69 6c 64 20 28 53 51 4c 49 54  afe build (SQLIT
e38c0 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 29  E_THREADSAFE==0)
e38d0 2c 20 74 68 65 6e 20 75 73 65 0a 20 20 2a 2a 20  , then use.  ** 
e38e0 74 68 65 20 73 74 72 65 72 72 6f 72 28 29 20 66  the strerror() f
e38f0 75 6e 63 74 69 6f 6e 20 74 6f 20 6f 62 74 61 69  unction to obtai
e3900 6e 20 74 68 65 20 68 75 6d 61 6e 2d 72 65 61 64  n the human-read
e3910 61 62 6c 65 20 65 72 72 6f 72 20 6d 65 73 73 61  able error messa
e3920 67 65 0a 20 20 2a 2a 20 65 71 75 69 76 61 6c 65  ge.  ** equivale
e3930 6e 74 20 74 6f 20 65 72 72 6e 6f 2e 20 4f 74 68  nt to errno. Oth
e3940 65 72 77 69 73 65 2c 20 75 73 65 20 73 74 72 65  erwise, use stre
e3950 72 72 6f 72 5f 72 28 29 2e 0a 20 20 2a 2f 20 0a  rror_r()..  */ .
e3960 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
e3970 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64  DSAFE && defined
e3980 28 48 41 56 45 5f 53 54 52 45 52 52 4f 52 5f 52  (HAVE_STRERROR_R
e3990 29 0a 20 20 63 68 61 72 20 61 45 72 72 5b 38 30  ).  char aErr[80
e39a0 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 45 72 72  ];.  memset(aErr
e39b0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 45 72 72  , 0, sizeof(aErr
e39c0 29 29 3b 0a 20 20 7a 45 72 72 20 3d 20 61 45 72  ));.  zErr = aEr
e39d0 72 3b 0a 0a 20 20 2f 2a 20 49 66 20 53 54 52 45  r;..  /* If STRE
e39e0 52 52 4f 52 5f 52 5f 43 48 41 52 5f 50 20 28 73  RROR_R_CHAR_P (s
e39f0 65 74 20 62 79 20 61 75 74 6f 63 6f 6e 66 20 73  et by autoconf s
e3a00 63 72 69 70 74 73 29 20 6f 72 20 5f 5f 55 53 45  cripts) or __USE
e3a10 5f 47 4e 55 20 69 73 20 64 65 66 69 6e 65 64 2c  _GNU is defined,
e3a20 0a 20 20 2a 2a 20 61 73 73 75 6d 65 20 74 68 61  .  ** assume tha
e3a30 74 20 74 68 65 20 73 79 73 74 65 6d 20 70 72 6f  t the system pro
e3a40 76 69 64 65 73 20 74 68 65 20 47 4e 55 20 76 65  vides the GNU ve
e3a50 72 73 69 6f 6e 20 6f 66 20 73 74 72 65 72 72 6f  rsion of strerro
e3a60 72 5f 72 28 29 20 74 68 61 74 0a 20 20 2a 2a 20  r_r() that.  ** 
e3a70 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
e3a80 72 20 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f  r to a buffer co
e3a90 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65 72 72  ntaining the err
e3aa0 6f 72 20 6d 65 73 73 61 67 65 2e 20 54 68 61 74  or message. That
e3ab0 20 70 6f 69 6e 74 65 72 20 0a 20 20 2a 2a 20 6d   pointer .  ** m
e3ac0 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 45 72 72  ay point to aErr
e3ad0 5b 5d 2c 20 6f 72 20 69 74 20 6d 61 79 20 70 6f  [], or it may po
e3ae0 69 6e 74 20 74 6f 20 73 6f 6d 65 20 73 74 61 74  int to some stat
e3af0 69 63 20 73 74 6f 72 61 67 65 20 73 6f 6d 65 77  ic storage somew
e3b00 68 65 72 65 2e 20 0a 20 20 2a 2a 20 4f 74 68 65  here. .  ** Othe
e3b10 72 77 69 73 65 2c 20 61 73 73 75 6d 65 20 74 68  rwise, assume th
e3b20 61 74 20 74 68 65 20 73 79 73 74 65 6d 20 70 72  at the system pr
e3b30 6f 76 69 64 65 73 20 74 68 65 20 50 4f 53 49 58  ovides the POSIX
e3b40 20 76 65 72 73 69 6f 6e 20 6f 66 20 0a 20 20 2a   version of .  *
e3b50 2a 20 73 74 72 65 72 72 6f 72 5f 72 28 29 2c 20  * strerror_r(), 
e3b60 77 68 69 63 68 20 61 6c 77 61 79 73 20 77 72 69  which always wri
e3b70 74 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  tes an error mes
e3b80 73 61 67 65 20 69 6e 74 6f 20 61 45 72 72 5b 5d  sage into aErr[]
e3b90 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74  ..  **.  ** If t
e3ba0 68 65 20 63 6f 64 65 20 69 6e 63 6f 72 72 65 63  he code incorrec
e3bb0 74 6c 79 20 61 73 73 75 6d 65 73 20 74 68 61 74  tly assumes that
e3bc0 20 69 74 20 69 73 20 74 68 65 20 50 4f 53 49 58   it is the POSIX
e3bd0 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20 69 73   version that is
e3be0 0a 20 20 2a 2a 20 61 76 61 69 6c 61 62 6c 65 2c  .  ** available,
e3bf0 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
e3c00 67 65 20 77 69 6c 6c 20 6f 66 74 65 6e 20 62 65  ge will often be
e3c10 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67   an empty string
e3c20 2e 20 4e 6f 74 20 61 0a 20 20 2a 2a 20 68 75 67  . Not a.  ** hug
e3c30 65 20 70 72 6f 62 6c 65 6d 2e 20 49 6e 63 6f 72  e problem. Incor
e3c40 72 65 63 74 6c 79 20 63 6f 6e 63 6c 75 64 69 6e  rectly concludin
e3c50 67 20 74 68 61 74 20 74 68 65 20 47 4e 55 20 76  g that the GNU v
e3c60 65 72 73 69 6f 6e 20 69 73 20 61 76 61 69 6c 61  ersion is availa
e3c70 62 6c 65 20 0a 20 20 2a 2a 20 63 6f 75 6c 64 20  ble .  ** could 
e3c80 6c 65 61 64 20 74 6f 20 61 20 73 65 67 66 61 75  lead to a segfau
e3c90 6c 74 20 74 68 6f 75 67 68 2e 0a 20 20 2a 2f 0a  lt though..  */.
e3ca0 23 69 66 20 64 65 66 69 6e 65 64 28 53 54 52 45  #if defined(STRE
e3cb0 52 52 4f 52 5f 52 5f 43 48 41 52 5f 50 29 20 7c  RROR_R_CHAR_P) |
e3cc0 7c 20 64 65 66 69 6e 65 64 28 5f 5f 55 53 45 5f  | defined(__USE_
e3cd0 47 4e 55 29 0a 20 20 7a 45 72 72 20 3d 20 0a 23  GNU).  zErr = .#
e3ce0 20 65 6e 64 69 66 0a 20 20 73 74 72 65 72 72 6f   endif.  strerro
e3cf0 72 5f 72 28 69 45 72 72 6e 6f 2c 20 61 45 72 72  r_r(iErrno, aErr
e3d00 2c 20 73 69 7a 65 6f 66 28 61 45 72 72 29 2d 31  , sizeof(aErr)-1
e3d10 29 3b 0a 0a 23 65 6c 69 66 20 53 51 4c 49 54 45  );..#elif SQLITE
e3d20 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 2f 2a  _THREADSAFE.  /*
e3d30 20 54 68 69 73 20 69 73 20 61 20 74 68 72 65 61   This is a threa
e3d40 64 73 61 66 65 20 62 75 69 6c 64 2c 20 62 75 74  dsafe build, but
e3d50 20 73 74 72 65 72 72 6f 72 5f 72 28 29 20 69 73   strerror_r() is
e3d60 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20   not available. 
e3d70 2a 2f 0a 20 20 7a 45 72 72 20 3d 20 22 22 3b 0a  */.  zErr = "";.
e3d80 23 65 6c 73 65 0a 20 20 2f 2a 20 4e 6f 6e 2d 74  #else.  /* Non-t
e3d90 68 72 65 61 64 73 61 66 65 20 62 75 69 6c 64 2c  hreadsafe build,
e3da0 20 75 73 65 20 73 74 72 65 72 72 6f 72 28 29 2e   use strerror().
e3db0 20 2a 2f 0a 20 20 7a 45 72 72 20 3d 20 73 74 72   */.  zErr = str
e3dc0 65 72 72 6f 72 28 69 45 72 72 6e 6f 29 3b 0a 23  error(iErrno);.#
e3dd0 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28  endif..  assert(
e3de0 20 65 72 72 63 6f 64 65 21 3d 53 51 4c 49 54 45   errcode!=SQLITE
e3df0 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 7a 50 61  _OK );.  if( zPa
e3e00 74 68 3d 3d 30 20 29 20 7a 50 61 74 68 20 3d 20  th==0 ) zPath = 
e3e10 22 22 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6c 6f  "";.  sqlite3_lo
e3e20 67 28 65 72 72 63 6f 64 65 2c 0a 20 20 20 20 20  g(errcode,.     
e3e30 20 22 6f 73 5f 75 6e 69 78 2e 63 3a 25 64 3a 20   "os_unix.c:%d: 
e3e40 28 25 64 29 20 25 73 28 25 73 29 20 2d 20 25 73  (%d) %s(%s) - %s
e3e50 22 2c 0a 20 20 20 20 20 20 69 4c 69 6e 65 2c 20  ",.      iLine, 
e3e60 69 45 72 72 6e 6f 2c 20 7a 46 75 6e 63 2c 20 7a  iErrno, zFunc, z
e3e70 50 61 74 68 2c 20 7a 45 72 72 0a 20 20 29 3b 0a  Path, zErr.  );.
e3e80 0a 20 20 72 65 74 75 72 6e 20 65 72 72 63 6f 64  .  return errcod
e3e90 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  e;.}../*.** Clos
e3ea0 65 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70  e a file descrip
e3eb0 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 73  tor..**.** We as
e3ec0 73 75 6d 65 20 74 68 61 74 20 63 6c 6f 73 65 28  sume that close(
e3ed0 29 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20  ) almost always 
e3ee0 77 6f 72 6b 73 2c 20 73 69 6e 63 65 20 69 74 20  works, since it 
e3ef0 69 73 20 6f 6e 6c 79 20 69 6e 20 61 0a 2a 2a 20  is only in a.** 
e3f00 76 65 72 79 20 73 69 63 6b 20 61 70 70 6c 69 63  very sick applic
e3f10 61 74 69 6f 6e 20 6f 72 20 6f 6e 20 61 20 76 65  ation or on a ve
e3f20 72 79 20 73 69 63 6b 20 70 6c 61 74 66 6f 72 6d  ry sick platform
e3f30 20 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 66   that it might f
e3f40 61 69 6c 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f  ail..** If it do
e3f50 65 73 20 66 61 69 6c 2c 20 73 69 6d 70 6c 79 20  es fail, simply 
e3f60 6c 65 61 6b 20 74 68 65 20 66 69 6c 65 20 64 65  leak the file de
e3f70 73 63 72 69 70 74 6f 72 2c 20 62 75 74 20 64 6f  scriptor, but do
e3f80 20 6c 6f 67 20 74 68 65 0a 2a 2a 20 65 72 72 6f   log the.** erro
e3f90 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  r..**.** Note th
e3fa0 61 74 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66  at it is not saf
e3fb0 65 20 74 6f 20 72 65 74 72 79 20 63 6c 6f 73 65  e to retry close
e3fc0 28 29 20 61 66 74 65 72 20 45 49 4e 54 52 20 73  () after EINTR s
e3fd0 69 6e 63 65 20 74 68 65 0a 2a 2a 20 66 69 6c 65  ince the.** file
e3fe0 20 64 65 73 63 72 69 70 74 6f 72 20 6d 69 67 68   descriptor migh
e3ff0 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  t have already b
e4000 65 65 6e 20 72 65 75 73 65 64 20 62 79 20 61 6e  een reused by an
e4010 6f 74 68 65 72 20 74 68 72 65 61 64 2e 0a 2a 2a  other thread..**
e4020 20 53 6f 20 77 65 20 64 6f 6e 27 74 20 65 76 65   So we don't eve
e4030 6e 20 74 72 79 20 74 6f 20 72 65 63 6f 76 65 72  n try to recover
e4040 20 66 72 6f 6d 20 61 6e 20 45 49 4e 54 52 2e 20   from an EINTR. 
e4050 20 4a 75 73 74 20 6c 6f 67 20 74 68 65 20 65 72   Just log the er
e4060 72 6f 72 0a 2a 2a 20 61 6e 64 20 6d 6f 76 65 20  ror.** and move 
e4070 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
e4080 69 64 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28  id robust_close(
e4090 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c  unixFile *pFile,
e40a0 20 69 6e 74 20 68 2c 20 69 6e 74 20 6c 69 6e 65   int h, int line
e40b0 6e 6f 29 7b 0a 20 20 69 66 28 20 6f 73 43 6c 6f  no){.  if( osClo
e40c0 73 65 28 68 29 20 29 7b 0a 20 20 20 20 75 6e 69  se(h) ){.    uni
e40d0 78 4c 6f 67 45 72 72 6f 72 41 74 4c 69 6e 65 28  xLogErrorAtLine(
e40e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f  SQLITE_IOERR_CLO
e40f0 53 45 2c 20 22 63 6c 6f 73 65 22 2c 0a 20 20 20  SE, "close",.   
e4100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4110 20 20 20 20 70 46 69 6c 65 20 3f 20 70 46 69 6c      pFile ? pFil
e4120 65 2d 3e 7a 50 61 74 68 20 3a 20 30 2c 20 6c 69  e->zPath : 0, li
e4130 6e 65 6e 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  neno);.  }.}../*
e4140 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 66 69  .** Close all fi
e4150 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 61  le descriptors a
e4160 63 63 75 6d 75 61 74 65 64 20 69 6e 20 74 68 65  ccumuated in the
e4170 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 2d 3e   unixInodeInfo->
e4180 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2f  pUnused list..*/
e4190 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c   .static void cl
e41a0 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 75 6e  osePendingFds(un
e41b0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a  ixFile *pFile){.
e41c0 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20    unixInodeInfo 
e41d0 2a 70 49 6e 6f 64 65 20 3d 20 70 46 69 6c 65 2d  *pInode = pFile-
e41e0 3e 70 49 6e 6f 64 65 3b 0a 20 20 55 6e 69 78 55  >pInode;.  UnixU
e41f0 6e 75 73 65 64 46 64 20 2a 70 3b 0a 20 20 55 6e  nusedFd *p;.  Un
e4200 69 78 55 6e 75 73 65 64 46 64 20 2a 70 4e 65 78  ixUnusedFd *pNex
e4210 74 3b 0a 20 20 66 6f 72 28 70 3d 70 49 6e 6f 64  t;.  for(p=pInod
e4220 65 2d 3e 70 55 6e 75 73 65 64 3b 20 70 3b 20 70  e->pUnused; p; p
e4230 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65  =pNext){.    pNe
e4240 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20  xt = p->pNext;. 
e4250 20 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28     robust_close(
e4260 70 46 69 6c 65 2c 20 70 2d 3e 66 64 2c 20 5f 5f  pFile, p->fd, __
e4270 4c 49 4e 45 5f 5f 29 3b 0a 20 20 20 20 73 71 6c  LINE__);.    sql
e4280 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
e4290 7d 0a 20 20 70 49 6e 6f 64 65 2d 3e 70 55 6e 75  }.  pInode->pUnu
e42a0 73 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  sed = 0;.}../*.*
e42b0 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78  * Release a unix
e42c0 49 6e 6f 64 65 49 6e 66 6f 20 73 74 72 75 63 74  InodeInfo struct
e42d0 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61  ure previously a
e42e0 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64  llocated by find
e42f0 49 6e 6f 64 65 49 6e 66 6f 28 29 2e 0a 2a 2a 0a  InodeInfo()..**.
e4300 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74  ** The mutex ent
e4310 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 75  ered using the u
e4320 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20  nixEnterMutex() 
e4330 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65  function must be
e4340 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68   held.** when th
e4350 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
e4360 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  alled..*/.static
e4370 20 76 6f 69 64 20 72 65 6c 65 61 73 65 49 6e 6f   void releaseIno
e4380 64 65 49 6e 66 6f 28 75 6e 69 78 46 69 6c 65 20  deInfo(unixFile 
e4390 2a 70 46 69 6c 65 29 7b 0a 20 20 75 6e 69 78 49  *pFile){.  unixI
e43a0 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 6e 6f 64 65  nodeInfo *pInode
e43b0 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65   = pFile->pInode
e43c0 3b 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69 78  ;.  assert( unix
e43d0 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20  MutexHeld() );. 
e43e0 20 69 66 28 20 41 4c 57 41 59 53 28 70 49 6e 6f   if( ALWAYS(pIno
e43f0 64 65 29 20 29 7b 0a 20 20 20 20 70 49 6e 6f 64  de) ){.    pInod
e4400 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69  e->nRef--;.    i
e4410 66 28 20 70 49 6e 6f 64 65 2d 3e 6e 52 65 66 3d  f( pInode->nRef=
e4420 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
e4430 72 74 28 20 70 49 6e 6f 64 65 2d 3e 70 53 68 6d  rt( pInode->pShm
e4440 4e 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Node==0 );.     
e4450 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 73   closePendingFds
e4460 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69  (pFile);.      i
e4470 66 28 20 70 49 6e 6f 64 65 2d 3e 70 50 72 65 76  f( pInode->pPrev
e4480 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
e4490 72 74 28 20 70 49 6e 6f 64 65 2d 3e 70 50 72 65  rt( pInode->pPre
e44a0 76 2d 3e 70 4e 65 78 74 3d 3d 70 49 6e 6f 64 65  v->pNext==pInode
e44b0 20 29 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 6f   );.        pIno
e44c0 64 65 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74  de->pPrev->pNext
e44d0 20 3d 20 70 49 6e 6f 64 65 2d 3e 70 4e 65 78 74   = pInode->pNext
e44e0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
e44f0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69         assert( i
e4500 6e 6f 64 65 4c 69 73 74 3d 3d 70 49 6e 6f 64 65  nodeList==pInode
e4510 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 6f 64   );.        inod
e4520 65 4c 69 73 74 20 3d 20 70 49 6e 6f 64 65 2d 3e  eList = pInode->
e4530 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  pNext;.      }. 
e4540 20 20 20 20 20 69 66 28 20 70 49 6e 6f 64 65 2d       if( pInode-
e4550 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20  >pNext ){.      
e4560 20 20 61 73 73 65 72 74 28 20 70 49 6e 6f 64 65    assert( pInode
e4570 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d  ->pNext->pPrev==
e4580 70 49 6e 6f 64 65 20 29 3b 0a 20 20 20 20 20 20  pInode );.      
e4590 20 20 70 49 6e 6f 64 65 2d 3e 70 4e 65 78 74 2d    pInode->pNext-
e45a0 3e 70 50 72 65 76 20 3d 20 70 49 6e 6f 64 65 2d  >pPrev = pInode-
e45b0 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a  >pPrev;.      }.
e45c0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
e45d0 65 65 28 70 49 6e 6f 64 65 29 3b 0a 20 20 20 20  ee(pInode);.    
e45e0 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  }.  }.}../*.** G
e45f0 69 76 65 6e 20 61 20 66 69 6c 65 20 64 65 73 63  iven a file desc
e4600 72 69 70 74 6f 72 2c 20 6c 6f 63 61 74 65 20 74  riptor, locate t
e4610 68 65 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f  he unixInodeInfo
e4620 20 6f 62 6a 65 63 74 20 74 68 61 74 0a 2a 2a 20   object that.** 
e4630 64 65 73 63 72 69 62 65 73 20 74 68 61 74 20 66  describes that f
e4640 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20  ile descriptor. 
e4650 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 6f 6e   Create a new on
e4660 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 20  e if necessary. 
e4670 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76   The.** return v
e4680 61 6c 75 65 20 6d 69 67 68 74 20 62 65 20 75 6e  alue might be un
e4690 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 66 20 61  initialized if a
e46a0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a  n error occurs..
e46b0 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20  **.** The mutex 
e46c0 65 6e 74 65 72 65 64 20 75 73 69 6e 67 20 74 68  entered using th
e46d0 65 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78  e unixEnterMutex
e46e0 28 29 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74  () function must
e46f0 20 62 65 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e   be held.** when
e4700 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
e4710 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20  s called..**.** 
e4720 52 65 74 75 72 6e 20 61 6e 20 61 70 70 72 6f 70  Return an approp
e4730 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65  riate error code
e4740 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e4750 66 69 6e 64 49 6e 6f 64 65 49 6e 66 6f 28 0a 20  findInodeInfo(. 
e4760 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
e4770 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e4780 2f 2a 20 55 6e 69 78 20 66 69 6c 65 20 77 69 74  /* Unix file wit
e4790 68 20 66 69 6c 65 20 64 65 73 63 20 75 73 65 64  h file desc used
e47a0 20 69 6e 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20   in the key */. 
e47b0 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a   unixInodeInfo *
e47c0 2a 70 70 49 6e 6f 64 65 20 20 20 20 20 20 20 20  *ppInode        
e47d0 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e  /* Return the un
e47e0 69 78 49 6e 6f 64 65 49 6e 66 6f 20 6f 62 6a 65  ixInodeInfo obje
e47f0 63 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  ct here */.){.  
e4800 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
e4810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e4820 2a 20 53 79 73 74 65 6d 20 63 61 6c 6c 20 72 65  * System call re
e4830 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69  turn code */.  i
e4840 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20  nt fd;          
e4850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e4860 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   The file descri
e4870 70 74 6f 72 20 66 6f 72 20 70 46 69 6c 65 20 2a  ptor for pFile *
e4880 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 46  /.  struct unixF
e4890 69 6c 65 49 64 20 66 69 6c 65 49 64 3b 20 20 20  ileId fileId;   
e48a0 20 20 20 2f 2a 20 4c 6f 6f 6b 75 70 20 6b 65 79     /* Lookup key
e48b0 20 66 6f 72 20 74 68 65 20 75 6e 69 78 49 6e 6f   for the unixIno
e48c0 64 65 49 6e 66 6f 20 2a 2f 0a 20 20 73 74 72 75  deInfo */.  stru
e48d0 63 74 20 73 74 61 74 20 73 74 61 74 62 75 66 3b  ct stat statbuf;
e48e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
e48f0 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 69 6e 66  w-level file inf
e4900 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 6e  ormation */.  un
e4910 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 6e  ixInodeInfo *pIn
e4920 6f 64 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ode = 0;     /* 
e4930 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 49 6e  Candidate unixIn
e4940 6f 64 65 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a  odeInfo object *
e4950 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69  /..  assert( uni
e4960 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a  xMutexHeld() );.
e4970 0a 20 20 2f 2a 20 47 65 74 20 6c 6f 77 2d 6c 65  .  /* Get low-le
e4980 76 65 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  vel information 
e4990 61 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 74  about the file t
e49a0 68 61 74 20 77 65 20 63 61 6e 20 75 73 65 64 20  hat we can used 
e49b0 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 20 61  to.  ** create a
e49c0 20 75 6e 69 71 75 65 20 6e 61 6d 65 20 66 6f 72   unique name for
e49d0 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a   the file..  */.
e49e0 20 20 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68 3b    fd = pFile->h;
e49f0 0a 20 20 72 63 20 3d 20 6f 73 46 73 74 61 74 28  .  rc = osFstat(
e4a00 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20  fd, &statbuf);. 
e4a10 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20   if( rc!=0 ){.  
e4a20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
e4a30 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 64  no = errno;.#ifd
e4a40 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20  ef EOVERFLOW.   
e4a50 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 73 74   if( pFile->last
e4a60 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f 57  Errno==EOVERFLOW
e4a70 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
e4a80 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 0a 20  _NOLFS;.#endif. 
e4a90 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
e4aa0 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23 69 66  _IOERR;.  }..#if
e4ab0 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20 20  def __APPLE__.  
e4ac0 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 61 6e  /* On OS X on an
e4ad0 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74 65   msdos filesyste
e4ae0 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d  m, the inode num
e4af0 62 65 72 20 69 73 20 72 65 70 6f 72 74 65 64 0a  ber is reported.
e4b00 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 6c 79    ** incorrectly
e4b10 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 20 66   for zero-size f
e4b20 69 6c 65 73 2e 20 20 53 65 65 20 74 69 63 6b 65  iles.  See ticke
e4b30 74 20 23 33 32 36 30 2e 20 20 54 6f 20 77 6f 72  t #3260.  To wor
e4b40 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 74 68  k.  ** around th
e4b50 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 20 63  is problem (we c
e4b60 6f 6e 73 69 64 65 72 20 69 74 20 61 20 62 75 67  onsider it a bug
e4b70 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 53 51   in OS X, not SQ
e4b80 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 61 6c  Lite).  ** we al
e4b90 77 61 79 73 20 69 6e 63 72 65 61 73 65 20 74 68  ways increase th
e4ba0 65 20 66 69 6c 65 20 73 69 7a 65 20 74 6f 20 31  e file size to 1
e4bb0 20 62 79 20 77 72 69 74 69 6e 67 20 61 20 73 69   by writing a si
e4bc0 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a 20 70  ngle byte.  ** p
e4bd0 72 69 6f 72 20 74 6f 20 61 63 63 65 73 73 69 6e  rior to accessin
e4be0 67 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62  g the inode numb
e4bf0 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 79 74  er.  The one byt
e4c00 65 20 77 72 69 74 74 65 6e 20 69 73 0a 20 20 2a  e written is.  *
e4c10 2a 20 61 6e 20 41 53 43 49 49 20 27 53 27 20 63  * an ASCII 'S' c
e4c20 68 61 72 61 63 74 65 72 20 77 68 69 63 68 20 61  haracter which a
e4c30 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 6f 20 62  lso happens to b
e4c40 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  e the first byte
e4c50 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 65 61  .  ** in the hea
e4c60 64 65 72 20 6f 66 20 65 76 65 72 79 20 53 51 4c  der of every SQL
e4c70 69 74 65 20 64 61 74 61 62 61 73 65 2e 20 20 49  ite database.  I
e4c80 6e 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 74  n this way, if t
e4c90 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20 72  here.  ** is a r
e4ca0 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73 75  ace condition su
e4cb0 63 68 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20  ch that another 
e4cc0 74 68 72 65 61 64 20 68 61 73 20 61 6c 72 65 61  thread has alrea
e4cd0 64 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 20 2a  dy populated.  *
e4ce0 2a 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65  * the first page
e4cf0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
e4d00 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 20 64  , no damage is d
e4d10 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  one..  */.  if( 
e4d20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65 3d  statbuf.st_size=
e4d30 3d 30 20 26 26 20 28 70 46 69 6c 65 2d 3e 66 73  =0 && (pFile->fs
e4d40 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46  Flags & SQLITE_F
e4d50 53 46 4c 41 47 53 5f 49 53 5f 4d 53 44 4f 53 29  SFLAGS_IS_MSDOS)
e4d60 21 3d 30 20 29 7b 0a 20 20 20 20 64 6f 7b 20 72  !=0 ){.    do{ r
e4d70 63 20 3d 20 6f 73 57 72 69 74 65 28 66 64 2c 20  c = osWrite(fd, 
e4d80 22 53 22 2c 20 31 29 3b 20 7d 77 68 69 6c 65 28  "S", 1); }while(
e4d90 20 72 63 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d   rc<0 && errno==
e4da0 45 49 4e 54 52 20 29 3b 0a 20 20 20 20 69 66 28  EINTR );.    if(
e4db0 20 72 63 21 3d 31 20 29 7b 0a 20 20 20 20 20 20   rc!=1 ){.      
e4dc0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
e4dd0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
e4de0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
e4df0 45 52 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  ERR;.    }.    r
e4e00 63 20 3d 20 6f 73 46 73 74 61 74 28 66 64 2c 20  c = osFstat(fd, 
e4e10 26 73 74 61 74 62 75 66 29 3b 0a 20 20 20 20 69  &statbuf);.    i
e4e20 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20  f( rc!=0 ){.    
e4e30 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
e4e40 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
e4e50 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e4e60 49 4f 45 52 52 3b 0a 20 20 20 20 7d 0a 20 20 7d  IOERR;.    }.  }
e4e70 0a 23 65 6e 64 69 66 0a 0a 20 20 6d 65 6d 73 65  .#endif..  memse
e4e80 74 28 26 66 69 6c 65 49 64 2c 20 30 2c 20 73 69  t(&fileId, 0, si
e4e90 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 3b 0a 20  zeof(fileId));. 
e4ea0 20 66 69 6c 65 49 64 2e 64 65 76 20 3d 20 73 74   fileId.dev = st
e4eb0 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69  atbuf.st_dev;.#i
e4ec0 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 66  f OS_VXWORKS.  f
e4ed0 69 6c 65 49 64 2e 70 49 64 20 3d 20 70 46 69 6c  ileId.pId = pFil
e4ee0 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 65 0a 20 20  e->pId;.#else.  
e4ef0 66 69 6c 65 49 64 2e 69 6e 6f 20 3d 20 73 74 61  fileId.ino = sta
e4f00 74 62 75 66 2e 73 74 5f 69 6e 6f 3b 0a 23 65 6e  tbuf.st_ino;.#en
e4f10 64 69 66 0a 20 20 70 49 6e 6f 64 65 20 3d 20 69  dif.  pInode = i
e4f20 6e 6f 64 65 4c 69 73 74 3b 0a 20 20 77 68 69 6c  nodeList;.  whil
e4f30 65 28 20 70 49 6e 6f 64 65 20 26 26 20 6d 65 6d  e( pInode && mem
e4f40 63 6d 70 28 26 66 69 6c 65 49 64 2c 20 26 70 49  cmp(&fileId, &pI
e4f50 6e 6f 64 65 2d 3e 66 69 6c 65 49 64 2c 20 73 69  node->fileId, si
e4f60 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 20 29 7b  zeof(fileId)) ){
e4f70 0a 20 20 20 20 70 49 6e 6f 64 65 20 3d 20 70 49  .    pInode = pI
e4f80 6e 6f 64 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  node->pNext;.  }
e4f90 0a 20 20 69 66 28 20 70 49 6e 6f 64 65 3d 3d 30  .  if( pInode==0
e4fa0 20 29 7b 0a 20 20 20 20 70 49 6e 6f 64 65 20 3d   ){.    pInode =
e4fb0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
e4fc0 20 73 69 7a 65 6f 66 28 2a 70 49 6e 6f 64 65 29   sizeof(*pInode)
e4fd0 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 6f   );.    if( pIno
e4fe0 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  de==0 ){.      r
e4ff0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
e5000 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65  EM;.    }.    me
e5010 6d 73 65 74 28 70 49 6e 6f 64 65 2c 20 30 2c 20  mset(pInode, 0, 
e5020 73 69 7a 65 6f 66 28 2a 70 49 6e 6f 64 65 29 29  sizeof(*pInode))
e5030 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 49  ;.    memcpy(&pI
e5040 6e 6f 64 65 2d 3e 66 69 6c 65 49 64 2c 20 26 66  node->fileId, &f
e5050 69 6c 65 49 64 2c 20 73 69 7a 65 6f 66 28 66 69  ileId, sizeof(fi
e5060 6c 65 49 64 29 29 3b 0a 20 20 20 20 70 49 6e 6f  leId));.    pIno
e5070 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20  de->nRef = 1;.  
e5080 20 20 70 49 6e 6f 64 65 2d 3e 70 4e 65 78 74 20    pInode->pNext 
e5090 3d 20 69 6e 6f 64 65 4c 69 73 74 3b 0a 20 20 20  = inodeList;.   
e50a0 20 70 49 6e 6f 64 65 2d 3e 70 50 72 65 76 20 3d   pInode->pPrev =
e50b0 20 30 3b 0a 20 20 20 20 69 66 28 20 69 6e 6f 64   0;.    if( inod
e50c0 65 4c 69 73 74 20 29 20 69 6e 6f 64 65 4c 69 73  eList ) inodeLis
e50d0 74 2d 3e 70 50 72 65 76 20 3d 20 70 49 6e 6f 64  t->pPrev = pInod
e50e0 65 3b 0a 20 20 20 20 69 6e 6f 64 65 4c 69 73 74  e;.    inodeList
e50f0 20 3d 20 70 49 6e 6f 64 65 3b 0a 20 20 7d 65 6c   = pInode;.  }el
e5100 73 65 7b 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e  se{.    pInode->
e5110 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70  nRef++;.  }.  *p
e5120 70 49 6e 6f 64 65 20 3d 20 70 49 6e 6f 64 65 3b  pInode = pInode;
e5130 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
e5140 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  _OK;.}.../*.** T
e5150 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
e5160 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ks if there is a
e5170 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68   RESERVED lock h
e5180 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69  eld on the speci
e5190 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20  fied.** file by 
e51a0 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65  this or any othe
e51b0 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75  r process. If su
e51c0 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c  ch a lock is hel
e51d0 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a  d, set *pResOut.
e51e0 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  ** to a non-zero
e51f0 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65   value otherwise
e5200 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74   *pResOut is set
e5210 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72   to zero.  The r
e5220 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69  eturn value.** i
e5230 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f  s set to SQLITE_
e5240 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f  OK unless an I/O
e5250 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75   error occurs du
e5260 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69  ring lock checki
e5270 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
e5280 74 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72  t unixCheckReser
e5290 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
e52a0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70  file *id, int *p
e52b0 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72  ResOut){.  int r
e52c0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
e52d0 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20   int reserved = 
e52e0 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  0;.  unixFile *p
e52f0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
e5300 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74  *)id;..  Simulat
e5310 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e  eIOError( return
e5320 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48   SQLITE_IOERR_CH
e5330 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b  ECKRESERVEDLOCK;
e5340 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   );..  assert( p
e5350 46 69 6c 65 20 29 3b 0a 20 20 75 6e 69 78 45 6e  File );.  unixEn
e5360 74 65 72 4d 75 74 65 78 28 29 3b 20 2f 2a 20 42  terMutex(); /* B
e5370 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 49  ecause pFile->pI
e5380 6e 6f 64 65 20 69 73 20 73 68 61 72 65 64 20 61  node is shared a
e5390 63 72 6f 73 73 20 74 68 72 65 61 64 73 20 2a 2f  cross threads */
e53a0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20  ..  /* Check if 
e53b0 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73  a thread in this
e53c0 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73   process holds s
e53d0 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20  uch a lock */.  
e53e0 69 66 28 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64  if( pFile->pInod
e53f0 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3e 53 48 41  e->eFileLock>SHA
e5400 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
e5410 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20  reserved = 1;.  
e5420 7d 0a 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73  }..  /* Otherwis
e5430 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74  e see if some ot
e5440 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64  her process hold
e5450 73 20 69 74 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64  s it..  */.#ifnd
e5460 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 20 20 69  ef __DJGPP__.  i
e5470 66 28 20 21 72 65 73 65 72 76 65 64 20 26 26 20  f( !reserved && 
e5480 21 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e  !pFile->pInode->
e5490 62 50 72 6f 63 65 73 73 4c 6f 63 6b 20 29 7b 0a  bProcessLock ){.
e54a0 20 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b      struct flock
e54b0 20 6c 6f 63 6b 3b 0a 20 20 20 20 6c 6f 63 6b 2e   lock;.    lock.
e54c0 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
e54d0 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f  SET;.    lock.l_
e54e0 73 74 61 72 74 20 3d 20 52 45 53 45 52 56 45 44  start = RESERVED
e54f0 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e  _BYTE;.    lock.
e5500 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 20 20 6c  l_len = 1;.    l
e5510 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57  ock.l_type = F_W
e5520 52 4c 43 4b 3b 0a 20 20 20 20 69 66 28 20 6f 73  RLCK;.    if( os
e5530 46 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20  Fcntl(pFile->h, 
e5540 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 20  F_GETLK, &lock) 
e5550 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
e5560 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b  LITE_IOERR_CHECK
e5570 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 0a 20 20  RESERVEDLOCK;.  
e5580 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
e5590 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
e55a0 20 20 7d 20 65 6c 73 65 20 69 66 28 20 6c 6f 63    } else if( loc
e55b0 6b 2e 6c 5f 74 79 70 65 21 3d 46 5f 55 4e 4c 43  k.l_type!=F_UNLC
e55c0 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 73 65 72  K ){.      reser
e55d0 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ved = 1;.    }. 
e55e0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 75   }.#endif.  .  u
e55f0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
e5600 0a 20 20 4f 53 54 52 41 43 45 28 28 22 54 45 53  .  OSTRACE(("TES
e5610 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20  T WR-LOCK %d %d 
e5620 25 64 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46  %d (unix)\n", pF
e5630 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65  ile->h, rc, rese
e5640 72 76 65 64 29 29 3b 0a 0a 20 20 2a 70 52 65 73  rved));..  *pRes
e5650 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a  Out = reserved;.
e5660 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
e5670 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f  /*.** Attempt to
e5680 20 73 65 74 20 61 20 73 79 73 74 65 6d 2d 6c 6f   set a system-lo
e5690 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 70  ck on the file p
e56a0 46 69 6c 65 2e 20 20 54 68 65 20 6c 6f 63 6b 20  File.  The lock 
e56b0 69 73 20 0a 2a 2a 20 64 65 73 63 72 69 62 65 64  is .** described
e56c0 20 62 79 20 70 4c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a   by pLock..**.**
e56d0 20 49 66 20 74 68 65 20 70 46 69 6c 65 20 77 61   If the pFile wa
e56e0 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2f 77 72  s opened read/wr
e56f0 69 74 65 20 66 72 6f 6d 20 75 6e 69 78 2d 65 78  ite from unix-ex
e5700 63 6c 2c 20 74 68 65 6e 20 74 68 65 20 6f 6e 6c  cl, then the onl
e5710 79 20 6c 6f 63 6b 0a 2a 2a 20 65 76 65 72 20 6f  y lock.** ever o
e5720 62 74 61 69 6e 65 64 20 69 73 20 61 6e 20 65 78  btained is an ex
e5730 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 20 61 6e  clusive lock, an
e5740 64 20 69 74 20 69 73 20 6f 62 74 61 69 6e 65 64  d it is obtained
e5750 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 0a 2a 2a   exactly once.**
e5760 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   the first time 
e5770 61 6e 79 20 6c 6f 63 6b 20 69 73 20 61 74 74 65  any lock is atte
e5780 6d 70 74 65 64 2e 20 20 41 6c 6c 20 73 75 62 73  mpted.  All subs
e5790 65 71 75 65 6e 74 20 73 79 73 74 65 6d 20 6c 6f  equent system lo
e57a0 63 6b 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69  cking.** operati
e57b0 6f 6e 73 20 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70  ons become no-op
e57c0 73 2e 20 20 4c 6f 63 6b 69 6e 67 20 6f 70 65 72  s.  Locking oper
e57d0 61 74 69 6f 6e 73 20 73 74 69 6c 6c 20 68 61 70  ations still hap
e57e0 70 65 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 0a  pen internally,.
e57f0 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63  ** in order to c
e5800 6f 6f 72 64 69 6e 61 74 65 20 61 63 63 65 73 73  oordinate access
e5810 20 62 65 74 77 65 65 6e 20 73 65 70 61 72 61 74   between separat
e5820 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
e5830 63 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 69 6e  ctions.** within
e5840 20 74 68 69 73 20 70 72 6f 63 65 73 73 2c 20 62   this process, b
e5850 75 74 20 61 6c 6c 20 6f 66 20 74 68 61 74 20 69  ut all of that i
e5860 73 20 68 61 6e 64 6c 65 64 20 69 6e 20 6d 65 6d  s handled in mem
e5870 6f 72 79 20 61 6e 64 20 74 68 65 0a 2a 2a 20 6f  ory and the.** o
e5880 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
e5890 64 6f 65 73 20 6e 6f 74 20 70 61 72 74 69 63 69  does not partici
e58a0 70 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  pate..**.** This
e58b0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 70   function is a p
e58c0 61 73 73 2d 74 68 72 6f 75 67 68 20 74 6f 20 66  ass-through to f
e58d0 63 6e 74 6c 28 46 5f 53 45 54 4c 4b 29 20 69 66  cntl(F_SETLK) if
e58e0 20 70 46 69 6c 65 20 69 73 20 75 73 69 6e 67 0a   pFile is using.
e58f0 2a 2a 20 61 6e 79 20 56 46 53 20 6f 74 68 65 72  ** any VFS other
e5900 20 74 68 61 6e 20 22 75 6e 69 78 2d 65 78 63 6c   than "unix-excl
e5910 22 20 6f 72 20 69 66 20 70 46 69 6c 65 20 69 73  " or if pFile is
e5920 20 6f 70 65 6e 65 64 20 6f 6e 20 22 75 6e 69 78   opened on "unix
e5930 2d 65 78 63 6c 22 0a 2a 2a 20 61 6e 64 20 69 73  -excl".** and is
e5940 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2a 0a 2a   read-only..**.*
e5950 2a 20 5a 65 72 6f 20 69 73 20 72 65 74 75 72 6e  * Zero is return
e5960 65 64 20 69 66 20 74 68 65 20 63 61 6c 6c 20 63  ed if the call c
e5970 6f 6d 70 6c 65 74 65 73 20 73 75 63 63 65 73 73  ompletes success
e5980 66 75 6c 6c 79 2c 20 6f 72 20 2d 31 20 69 66 20  fully, or -1 if 
e5990 61 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 66 63 6e  a call.** to fcn
e59a0 74 6c 28 29 20 66 61 69 6c 73 2e 20 49 6e 20 74  tl() fails. In t
e59b0 68 69 73 20 63 61 73 65 2c 20 65 72 72 6e 6f 20  his case, errno 
e59c0 69 73 20 73 65 74 20 61 70 70 72 6f 70 72 69 61  is set appropria
e59d0 74 65 6c 79 20 28 62 79 20 66 63 6e 74 6c 28 29  tely (by fcntl()
e59e0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
e59f0 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 75 6e   unixFileLock(un
e5a00 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 73  ixFile *pFile, s
e5a10 74 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 4c 6f  truct flock *pLo
e5a20 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ck){.  int rc;. 
e5a30 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a   unixInodeInfo *
e5a40 70 49 6e 6f 64 65 20 3d 20 70 46 69 6c 65 2d 3e  pInode = pFile->
e5a50 70 49 6e 6f 64 65 3b 0a 20 20 61 73 73 65 72 74  pInode;.  assert
e5a60 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28  ( unixMutexHeld(
e5a70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
e5a80 49 6e 6f 64 65 21 3d 30 20 29 3b 0a 20 20 69 66  Inode!=0 );.  if
e5a90 28 20 28 28 70 46 69 6c 65 2d 3e 63 74 72 6c 46  ( ((pFile->ctrlF
e5aa0 6c 61 67 73 20 26 20 55 4e 49 58 46 49 4c 45 5f  lags & UNIXFILE_
e5ab0 45 58 43 4c 29 21 3d 30 20 7c 7c 20 70 49 6e 6f  EXCL)!=0 || pIno
e5ac0 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c 6f 63 6b  de->bProcessLock
e5ad0 29 0a 20 20 20 26 26 20 28 28 70 46 69 6c 65 2d  ).   && ((pFile-
e5ae0 3e 63 74 72 6c 46 6c 61 67 73 20 26 20 55 4e 49  >ctrlFlags & UNI
e5af0 58 46 49 4c 45 5f 52 44 4f 4e 4c 59 29 3d 3d 30  XFILE_RDONLY)==0
e5b00 29 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ).  ){.    if( p
e5b10 49 6e 6f 64 65 2d 3e 62 50 72 6f 63 65 73 73 4c  Inode->bProcessL
e5b20 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ock==0 ){.      
e5b30 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63  struct flock loc
e5b40 6b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  k;.      assert(
e5b50 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 3d 3d   pInode->nLock==
e5b60 30 20 29 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e  0 );.      lock.
e5b70 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
e5b80 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e  SET;.      lock.
e5b90 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44  l_start = SHARED
e5ba0 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 6c 6f  _FIRST;.      lo
e5bb0 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45  ck.l_len = SHARE
e5bc0 44 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 6c 6f  D_SIZE;.      lo
e5bd0 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52  ck.l_type = F_WR
e5be0 4c 43 4b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  LCK;.      rc = 
e5bf0 6f 73 46 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68  osFcntl(pFile->h
e5c00 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_SETLK, &lock
e5c10 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3c  );.      if( rc<
e5c20 30 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  0 ) return rc;. 
e5c30 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e 62 50 72       pInode->bPr
e5c40 6f 63 65 73 73 4c 6f 63 6b 20 3d 20 31 3b 0a 20  ocessLock = 1;. 
e5c50 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f       pInode->nLo
e5c60 63 6b 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ck++;.    }else{
e5c70 0a 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20  .      rc = 0;. 
e5c80 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
e5c90 20 20 72 63 20 3d 20 6f 73 46 63 6e 74 6c 28 70    rc = osFcntl(p
e5ca0 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b  File->h, F_SETLK
e5cb0 2c 20 70 4c 6f 63 6b 29 3b 0a 20 20 7d 0a 20 20  , pLock);.  }.  
e5cc0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
e5cd0 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c  .** Lock the fil
e5ce0 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20  e with the lock 
e5cf0 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72  specified by par
e5d00 61 6d 65 74 65 72 20 65 46 69 6c 65 4c 6f 63 6b  ameter eFileLock
e5d10 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65   - one.** of the
e5d20 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
e5d30 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44  *     (1) SHARED
e5d40 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29  _LOCK.**     (2)
e5d50 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a   RESERVED_LOCK.*
e5d60 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e  *     (3) PENDIN
e5d70 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34  G_LOCK.**     (4
e5d80 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ) EXCLUSIVE_LOCK
e5d90 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73  .**.** Sometimes
e5da0 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67   when requesting
e5db0 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c   one lock state,
e5dc0 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b   additional lock
e5dd0 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69   states.** are i
e5de0 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65  nserted in betwe
e5df0 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67  en.  The locking
e5e00 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f   might fail on o
e5e10 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a  ne of the later.
e5e20 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c  ** transitions l
e5e30 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  eaving the lock 
e5e40 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20  state different 
e5e50 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61  from what it sta
e5e60 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c  rted but.** stil
e5e70 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67  l short of its g
e5e80 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  oal.  The follow
e5e90 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20  ing chart shows 
e5ea0 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74  the allowed.** t
e5eb0 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74  ransitions and t
e5ec0 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65  he inserted inte
e5ed0 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a  rmediate states:
e5ee0 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b  .**.**    UNLOCK
e5ef0 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20  ED -> SHARED.** 
e5f00 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53     SHARED -> RES
e5f10 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  ERVED.**    SHAR
e5f20 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
e5f30 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
e5f40 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28     RESERVED -> (
e5f50 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
e5f60 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44  USIVE.**    PEND
e5f70 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  ING -> EXCLUSIVE
e5f80 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
e5f90 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e  ine will only in
e5fa0 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20  crease a lock.  
e5fb0 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f  Use the sqlite3O
e5fc0 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75  sUnlock().** rou
e5fd0 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20  tine to lower a 
e5fe0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a  locking level..*
e5ff0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
e6000 78 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  xLock(sqlite3_fi
e6010 6c 65 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c  le *id, int eFil
e6020 65 4c 6f 63 6b 29 7b 0a 20 20 2f 2a 20 54 68 65  eLock){.  /* The
e6030 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 65 73 63 72   following descr
e6040 69 62 65 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  ibes the impleme
e6050 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76  ntation of the v
e6060 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 61 6e 64  arious locks and
e6070 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74 72 61 6e 73  .  ** lock trans
e6080 69 74 69 6f 6e 73 20 69 6e 20 74 65 72 6d 73 20  itions in terms 
e6090 6f 66 20 74 68 65 20 50 4f 53 49 58 20 61 64 76  of the POSIX adv
e60a0 69 73 6f 72 79 20 73 68 61 72 65 64 20 61 6e 64  isory shared and
e60b0 20 65 78 63 6c 75 73 69 76 65 0a 20 20 2a 2a 20   exclusive.  ** 
e60c0 6c 6f 63 6b 20 70 72 69 6d 69 74 69 76 65 73 20  lock primitives 
e60d0 28 63 61 6c 6c 65 64 20 72 65 61 64 2d 6c 6f 63  (called read-loc
e60e0 6b 73 20 61 6e 64 20 77 72 69 74 65 2d 6c 6f 63  ks and write-loc
e60f0 6b 73 20 62 65 6c 6f 77 2c 20 74 6f 20 61 76 6f  ks below, to avo
e6100 69 64 0a 20 20 2a 2a 20 63 6f 6e 66 75 73 69 6f  id.  ** confusio
e6110 6e 20 77 69 74 68 20 53 51 4c 69 74 65 20 6c 6f  n with SQLite lo
e6120 63 6b 20 6e 61 6d 65 73 29 2e 20 54 68 65 20 61  ck names). The a
e6130 6c 67 6f 72 69 74 68 6d 73 20 61 72 65 20 63 6f  lgorithms are co
e6140 6d 70 6c 69 63 61 74 65 64 0a 20 20 2a 2a 20 73  mplicated.  ** s
e6150 6c 69 67 68 74 6c 79 20 69 6e 20 6f 72 64 65 72  lightly in order
e6160 20 74 6f 20 62 65 20 63 6f 6d 70 61 74 69 62 6c   to be compatibl
e6170 65 20 77 69 74 68 20 77 69 6e 64 6f 77 73 20 73  e with windows s
e6180 79 73 74 65 6d 73 20 73 69 6d 75 6c 74 61 6e 65  ystems simultane
e6190 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 63 63 65 73  ously.  ** acces
e61a0 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61  sing the same da
e61b0 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 6e 20  tabase file, in 
e61c0 63 61 73 65 20 74 68 61 74 20 69 73 20 65 76 65  case that is eve
e61d0 72 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a  r required..  **
e61e0 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c 73 20 64 65  .  ** Symbols de
e61f0 66 69 6e 65 64 20 69 6e 20 6f 73 2e 68 20 69 6e  fined in os.h in
e6200 64 65 6e 74 69 66 79 20 74 68 65 20 27 70 65 6e  dentify the 'pen
e6210 64 69 6e 67 20 62 79 74 65 27 20 61 6e 64 20 74  ding byte' and t
e6220 68 65 20 27 72 65 73 65 72 76 65 64 0a 20 20 2a  he 'reserved.  *
e6230 2a 20 62 79 74 65 27 2c 20 65 61 63 68 20 73 69  * byte', each si
e6240 6e 67 6c 65 20 62 79 74 65 73 20 61 74 20 77 65  ngle bytes at we
e6250 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 73  ll known offsets
e6260 2c 20 61 6e 64 20 74 68 65 20 27 73 68 61 72 65  , and the 'share
e6270 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67  d byte.  ** rang
e6280 65 27 2c 20 61 20 72 61 6e 67 65 20 6f 66 20 35  e', a range of 5
e6290 31 30 20 62 79 74 65 73 20 61 74 20 61 20 77 65  10 bytes at a we
e62a0 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 2e  ll known offset.
e62b0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 6f 62  .  **.  ** To ob
e62c0 74 61 69 6e 20 61 20 53 48 41 52 45 44 20 6c 6f  tain a SHARED lo
e62d0 63 6b 2c 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20  ck, a read-lock 
e62e0 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74  is obtained on t
e62f0 68 65 20 27 70 65 6e 64 69 6e 67 0a 20 20 2a 2a  he 'pending.  **
e6300 20 62 79 74 65 27 2e 20 20 49 66 20 74 68 69 73   byte'.  If this
e6310 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20   is successful, 
e6320 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 66 72  a random byte fr
e6330 6f 6d 20 74 68 65 20 27 73 68 61 72 65 64 20 62  om the 'shared b
e6340 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20  yte.  ** range' 
e6350 69 73 20 72 65 61 64 2d 6c 6f 63 6b 65 64 20 61  is read-locked a
e6360 6e 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74  nd the lock on t
e6370 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65  he 'pending byte
e6380 27 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2a  ' released..  **
e6390 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20  .  ** A process 
e63a0 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20  may only obtain 
e63b0 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
e63c0 61 66 74 65 72 20 69 74 20 68 61 73 20 61 20 53  after it has a S
e63d0 48 41 52 45 44 20 6c 6f 63 6b 2e 0a 20 20 2a 2a  HARED lock..  **
e63e0 20 41 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   A RESERVED lock
e63f0 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
e6400 62 79 20 67 72 61 62 62 69 6e 67 20 61 20 77 72  by grabbing a wr
e6410 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a  ite-lock on the.
e6420 20 20 2a 2a 20 27 72 65 73 65 72 76 65 64 20 62    ** 'reserved b
e6430 79 74 65 27 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a  yte'. .  **.  **
e6440 20 41 20 70 72 6f 63 65 73 73 20 6d 61 79 20 6f   A process may o
e6450 6e 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 45 4e  nly obtain a PEN
e6460 44 49 4e 47 20 6c 6f 63 6b 20 61 66 74 65 72 20  DING lock after 
e6470 69 74 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20  it has obtained 
e6480 61 0a 20 20 2a 2a 20 53 48 41 52 45 44 20 6c 6f  a.  ** SHARED lo
e6490 63 6b 2e 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f  ck. A PENDING lo
e64a0 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65  ck is implemente
e64b0 64 20 62 79 20 6f 62 74 61 69 6e 69 6e 67 20 61  d by obtaining a
e64c0 20 77 72 69 74 65 2d 6c 6f 63 6b 0a 20 20 2a 2a   write-lock.  **
e64d0 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67   on the 'pending
e64e0 20 62 79 74 65 27 2e 20 54 68 69 73 20 65 6e 73   byte'. This ens
e64f0 75 72 65 73 20 74 68 61 74 20 6e 6f 20 6e 65 77  ures that no new
e6500 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 63 61   SHARED locks ca
e6510 6e 20 62 65 0a 20 20 2a 2a 20 6f 62 74 61 69 6e  n be.  ** obtain
e6520 65 64 2c 20 62 75 74 20 65 78 69 73 74 69 6e 67  ed, but existing
e6530 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 61 72   SHARED locks ar
e6540 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 70 65 72  e allowed to per
e6550 73 69 73 74 2e 20 41 20 70 72 6f 63 65 73 73 0a  sist. A process.
e6560 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61    ** does not ha
e6570 76 65 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 52  ve to obtain a R
e6580 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20  ESERVED lock on 
e6590 74 68 65 20 77 61 79 20 74 6f 20 61 20 50 45 4e  the way to a PEN
e65a0 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20  DING lock..  ** 
e65b0 54 68 69 73 20 70 72 6f 70 65 72 74 79 20 69 73  This property is
e65c0 20 75 73 65 64 20 62 79 20 74 68 65 20 61 6c 67   used by the alg
e65d0 6f 72 69 74 68 6d 20 66 6f 72 20 72 6f 6c 6c 69  orithm for rolli
e65e0 6e 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61  ng back a journa
e65f0 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 61 66 74 65  l file.  ** afte
e6600 72 20 61 20 63 72 61 73 68 2e 0a 20 20 2a 2a 0a  r a crash..  **.
e6610 20 20 2a 2a 20 41 6e 20 45 58 43 4c 55 53 49 56    ** An EXCLUSIV
e6620 45 20 6c 6f 63 6b 2c 20 6f 62 74 61 69 6e 65 64  E lock, obtained
e6630 20 61 66 74 65 72 20 61 20 50 45 4e 44 49 4e 47   after a PENDING
e6640 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 69   lock is held, i
e6650 73 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  s.  ** implement
e6660 65 64 20 62 79 20 6f 62 74 61 69 6e 69 6e 67 20  ed by obtaining 
e6670 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
e6680 74 68 65 20 65 6e 74 69 72 65 20 27 73 68 61 72  the entire 'shar
e6690 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e  ed byte.  ** ran
e66a0 67 65 27 2e 20 53 69 6e 63 65 20 61 6c 6c 20 6f  ge'. Since all o
e66b0 74 68 65 72 20 6c 6f 63 6b 73 20 72 65 71 75 69  ther locks requi
e66c0 72 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f  re a read-lock o
e66d0 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 62 79 74  n one of the byt
e66e0 65 73 0a 20 20 2a 2a 20 77 69 74 68 69 6e 20 74  es.  ** within t
e66f0 68 69 73 20 72 61 6e 67 65 2c 20 74 68 69 73 20  his range, this 
e6700 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f 20  ensures that no 
e6710 6f 74 68 65 72 20 6c 6f 63 6b 73 20 61 72 65 20  other locks are 
e6720 68 65 6c 64 20 6f 6e 20 74 68 65 0a 20 20 2a 2a  held on the.  **
e6730 20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 2a 2a   database. .  **
e6740 0a 20 20 2a 2a 20 54 68 65 20 72 65 61 73 6f 6e  .  ** The reason
e6750 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 63   a single byte c
e6760 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 69 6e  annot be used in
e6770 73 74 65 61 64 20 6f 66 20 74 68 65 20 27 73 68  stead of the 'sh
e6780 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72  ared byte.  ** r
e6790 61 6e 67 65 27 20 69 73 20 74 68 61 74 20 73 6f  ange' is that so
e67a0 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 77  me versions of w
e67b0 69 6e 64 6f 77 73 20 64 6f 20 6e 6f 74 20 73 75  indows do not su
e67c0 70 70 6f 72 74 20 72 65 61 64 2d 6c 6f 63 6b 73  pport read-locks
e67d0 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e  . By.  ** lockin
e67e0 67 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20  g a random byte 
e67f0 66 72 6f 6d 20 61 20 72 61 6e 67 65 2c 20 63 6f  from a range, co
e6800 6e 63 75 72 72 65 6e 74 20 53 48 41 52 45 44 20  ncurrent SHARED 
e6810 6c 6f 63 6b 73 20 6d 61 79 20 65 78 69 73 74 0a  locks may exist.
e6820 20 20 2a 2a 20 65 76 65 6e 20 69 66 20 74 68 65    ** even if the
e6830 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69   locking primiti
e6840 76 65 20 75 73 65 64 20 69 73 20 61 6c 77 61 79  ve used is alway
e6850 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2e 0a  s a write-lock..
e6860 20 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20    */.  int rc = 
e6870 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69  SQLITE_OK;.  uni
e6880 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
e6890 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
e68a0 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70  unixInodeInfo *p
e68b0 49 6e 6f 64 65 3b 0a 20 20 73 74 72 75 63 74 20  Inode;.  struct 
e68c0 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e  flock lock;.  in
e68d0 74 20 74 45 72 72 6e 6f 20 3d 20 30 3b 0a 0a 20  t tErrno = 0;.. 
e68e0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
e68f0 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 4c 4f  ;.  OSTRACE(("LO
e6900 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 20  CK    %d %s was 
e6910 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d 25 64  %s(%s,%d) pid=%d
e6920 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69 6c   (unix)\n", pFil
e6930 65 2d 3e 68 2c 0a 20 20 20 20 20 20 61 7a 46 69  e->h,.      azFi
e6940 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c 6f 63 6b  leLock(eFileLock
e6950 29 2c 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 70 46  ), azFileLock(pF
e6960 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 29 2c  ile->eFileLock),
e6970 0a 20 20 20 20 20 20 61 7a 46 69 6c 65 4c 6f 63  .      azFileLoc
e6980 6b 28 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d  k(pFile->pInode-
e6990 3e 65 46 69 6c 65 4c 6f 63 6b 29 2c 20 70 46 69  >eFileLock), pFi
e69a0 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 6e 53 68 61  le->pInode->nSha
e69b0 72 65 64 20 2c 20 67 65 74 70 69 64 28 29 29 29  red , getpid()))
e69c0 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ;..  /* If there
e69d0 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f   is already a lo
e69e0 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20  ck of this type 
e69f0 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74  or more restrict
e6a00 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  ive on the.  ** 
e6a10 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74  unixFile, do not
e6a20 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20  hing. Don't use 
e6a30 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78  the end_lock: ex
e6a40 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a  it path, as.  **
e6a50 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
e6a60 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61  ) hasn't been ca
e6a70 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20  lled yet..  */. 
e6a80 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c   if( pFile->eFil
e6a90 65 4c 6f 63 6b 3e 3d 65 46 69 6c 65 4c 6f 63 6b  eLock>=eFileLock
e6aa0 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 28   ){.    OSTRACE(
e6ab0 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20  ("LOCK    %d %s 
e6ac0 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64  ok (already held
e6ad0 29 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69  ) (unix)\n", pFi
e6ae0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
e6af0 20 20 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 65 46     azFileLock(eF
e6b00 69 6c 65 4c 6f 63 6b 29 29 29 3b 0a 20 20 20 20  ileLock)));.    
e6b10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e6b20 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ;.  }..  /* Make
e6b30 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e   sure the lockin
e6b40 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f  g sequence is co
e6b50 72 72 65 63 74 2e 0a 20 20 2a 2a 20 20 28 31 29  rrect..  **  (1)
e6b60 20 57 65 20 6e 65 76 65 72 20 6d 6f 76 65 20 66   We never move f
e6b70 72 6f 6d 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20  rom unlocked to 
e6b80 61 6e 79 74 68 69 6e 67 20 68 69 67 68 65 72 20  anything higher 
e6b90 74 68 61 6e 20 73 68 61 72 65 64 20 6c 6f 63 6b  than shared lock
e6ba0 2e 0a 20 20 2a 2a 20 20 28 32 29 20 53 51 4c 69  ..  **  (2) SQLi
e6bb0 74 65 20 6e 65 76 65 72 20 65 78 70 6c 69 63 69  te never explici
e6bc0 74 6c 79 20 72 65 71 75 65 73 74 73 20 61 20 70  tly requests a p
e6bd0 65 6e 64 69 67 20 6c 6f 63 6b 2e 0a 20 20 2a 2a  endig lock..  **
e6be0 20 20 28 33 29 20 41 20 73 68 61 72 65 64 20 6c    (3) A shared l
e6bf0 6f 63 6b 20 69 73 20 61 6c 77 61 79 73 20 68 65  ock is always he
e6c00 6c 64 20 77 68 65 6e 20 61 20 72 65 73 65 72 76  ld when a reserv
e6c10 65 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73  e lock is reques
e6c20 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ted..  */.  asse
e6c30 72 74 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65  rt( pFile->eFile
e6c40 4c 6f 63 6b 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c  Lock!=NO_LOCK ||
e6c50 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52   eFileLock==SHAR
e6c60 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  ED_LOCK );.  ass
e6c70 65 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b 21 3d  ert( eFileLock!=
e6c80 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a  PENDING_LOCK );.
e6c90 20 20 61 73 73 65 72 74 28 20 65 46 69 6c 65 4c    assert( eFileL
e6ca0 6f 63 6b 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ock!=RESERVED_LO
e6cb0 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 65 46 69  CK || pFile->eFi
e6cc0 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c  leLock==SHARED_L
e6cd0 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69  OCK );..  /* Thi
e6ce0 73 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65  s mutex is neede
e6cf0 64 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d  d because pFile-
e6d00 3e 70 49 6e 6f 64 65 20 69 73 20 73 68 61 72 65  >pInode is share
e6d10 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73  d across threads
e6d20 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65  .  */.  unixEnte
e6d30 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 49 6e 6f  rMutex();.  pIno
e6d40 64 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f  de = pFile->pIno
e6d50 64 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d  de;..  /* If som
e6d60 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74  e thread using t
e6d70 68 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f  his PID has a lo
e6d80 63 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65  ck via a differe
e6d90 6e 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a  nt unixFile*.  *
e6da0 2a 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72  * handle that pr
e6db0 65 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75  ecludes the requ
e6dc0 65 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75  ested lock, retu
e6dd0 72 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20  rn BUSY..  */.  
e6de0 69 66 28 20 28 70 46 69 6c 65 2d 3e 65 46 69 6c  if( (pFile->eFil
e6df0 65 4c 6f 63 6b 21 3d 70 49 6e 6f 64 65 2d 3e 65  eLock!=pInode->e
e6e00 46 69 6c 65 4c 6f 63 6b 20 26 26 20 0a 20 20 20  FileLock && .   
e6e10 20 20 20 20 20 20 20 28 70 49 6e 6f 64 65 2d 3e         (pInode->
e6e20 65 46 69 6c 65 4c 6f 63 6b 3e 3d 50 45 4e 44 49  eFileLock>=PENDI
e6e30 4e 47 5f 4c 4f 43 4b 20 7c 7c 20 65 46 69 6c 65  NG_LOCK || eFile
e6e40 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  Lock>SHARED_LOCK
e6e50 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d  )).  ){.    rc =
e6e60 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
e6e70 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b    goto end_lock;
e6e80 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20  .  }..  /* If a 
e6e90 53 48 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 72  SHARED lock is r
e6ea0 65 71 75 65 73 74 65 64 2c 20 61 6e 64 20 73 6f  equested, and so
e6eb0 6d 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20  me thread using 
e6ec0 74 68 69 73 20 50 49 44 20 61 6c 72 65 61 64 79  this PID already
e6ed0 0a 20 20 2a 2a 20 68 61 73 20 61 20 53 48 41 52  .  ** has a SHAR
e6ee0 45 44 20 6f 72 20 52 45 53 45 52 56 45 44 20 6c  ED or RESERVED l
e6ef0 6f 63 6b 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d  ock, then increm
e6f00 65 6e 74 20 72 65 66 65 72 65 6e 63 65 20 63 6f  ent reference co
e6f10 75 6e 74 73 20 61 6e 64 0a 20 20 2a 2a 20 72 65  unts and.  ** re
e6f20 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a  turn SQLITE_OK..
e6f30 20 20 2a 2f 0a 20 20 69 66 28 20 65 46 69 6c 65    */.  if( eFile
e6f40 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  Lock==SHARED_LOC
e6f50 4b 20 26 26 20 0a 20 20 20 20 20 20 28 70 49 6e  K && .      (pIn
e6f60 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d  ode->eFileLock==
e6f70 53 48 41 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70  SHARED_LOCK || p
e6f80 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b  Inode->eFileLock
e6f90 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29  ==RESERVED_LOCK)
e6fa0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
e6fb0 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45  eFileLock==SHARE
e6fc0 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73  D_LOCK );.    as
e6fd0 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 65 46 69  sert( pFile->eFi
e6fe0 6c 65 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 20  leLock==0 );.   
e6ff0 20 61 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d   assert( pInode-
e7000 3e 6e 53 68 61 72 65 64 3e 30 20 29 3b 0a 20 20  >nShared>0 );.  
e7010 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f    pFile->eFileLo
e7020 63 6b 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b  ck = SHARED_LOCK
e7030 3b 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 53  ;.    pInode->nS
e7040 68 61 72 65 64 2b 2b 3b 0a 20 20 20 20 70 49 6e  hared++;.    pIn
e7050 6f 64 65 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20  ode->nLock++;.  
e7060 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b    goto end_lock;
e7070 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 20 50 45  .  }...  /* A PE
e7080 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65  NDING lock is ne
e7090 65 64 65 64 20 62 65 66 6f 72 65 20 61 63 71 75  eded before acqu
e70a0 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c  iring a SHARED l
e70b0 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20  ock and before. 
e70c0 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e   ** acquiring an
e70d0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e   EXCLUSIVE lock.
e70e0 20 20 46 6f 72 20 74 68 65 20 53 48 41 52 45 44    For the SHARED
e70f0 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49   lock, the PENDI
e7100 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20  NG will.  ** be 
e7110 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20  released..  */. 
e7120 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c   lock.l_len = 1L
e7130 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63  ;.  lock.l_whenc
e7140 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20  e = SEEK_SET;.  
e7150 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53  if( eFileLock==S
e7160 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20  HARED_LOCK .    
e7170 20 20 7c 7c 20 28 65 46 69 6c 65 4c 6f 63 6b 3d    || (eFileLock=
e7180 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
e7190 26 26 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c  && pFile->eFileL
e71a0 6f 63 6b 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  ock<PENDING_LOCK
e71b0 29 0a 20 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 2e  ).  ){.    lock.
e71c0 6c 5f 74 79 70 65 20 3d 20 28 65 46 69 6c 65 4c  l_type = (eFileL
e71d0 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ock==SHARED_LOCK
e71e0 3f 46 5f 52 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b  ?F_RDLCK:F_WRLCK
e71f0 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74  );.    lock.l_st
e7200 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59  art = PENDING_BY
e7210 54 45 3b 0a 20 20 20 20 69 66 28 20 75 6e 69 78  TE;.    if( unix
e7220 46 69 6c 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20  FileLock(pFile, 
e7230 26 6c 6f 63 6b 29 20 29 7b 0a 20 20 20 20 20 20  &lock) ){.      
e7240 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
e7250 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
e7260 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
e7270 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
e7280 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b  ITE_IOERR_LOCK);
e7290 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
e72a0 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20  QLITE_BUSY ){.  
e72b0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
e72c0 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
e72d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67  .      }.      g
e72e0 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20  oto end_lock;.  
e72f0 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 49    }.  }...  /* I
e7300 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73 20 74  f control gets t
e7310 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68  o this point, th
e7320 65 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f 20 61  en actually go a
e7330 68 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a 20 20  head and make.  
e7340 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  ** operating sys
e7350 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20 74 68  tem calls for th
e7360 65 20 73 70 65 63 69 66 69 65 64 20 6c 6f 63 6b  e specified lock
e7370 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 46 69  ..  */.  if( eFi
e7380 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c  leLock==SHARED_L
e7390 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  OCK ){.    asser
e73a0 74 28 20 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72  t( pInode->nShar
e73b0 65 64 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  ed==0 );.    ass
e73c0 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 65 46 69  ert( pInode->eFi
e73d0 6c 65 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 20  leLock==0 );.   
e73e0 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
e73f0 49 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20 2f  ITE_OK );..    /
e7400 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 20 72 65  * Now get the re
e7410 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c  ad-lock */.    l
e7420 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48  ock.l_start = SH
e7430 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20  ARED_FIRST;.    
e7440 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41  lock.l_len = SHA
e7450 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 69 66  RED_SIZE;.    if
e7460 28 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 70  ( unixFileLock(p
e7470 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 20 29 7b 0a  File, &lock) ){.
e7480 20 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65        tErrno = e
e7490 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d  rrno;.      rc =
e74a0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
e74b0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
e74c0 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o, SQLITE_IOERR_
e74d0 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  LOCK);.    }..  
e74e0 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 65    /* Drop the te
e74f0 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e 47 20  mporary PENDING 
e7500 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b  lock */.    lock
e7510 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49  .l_start = PENDI
e7520 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63  NG_BYTE;.    loc
e7530 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20  k.l_len = 1L;.  
e7540 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20    lock.l_type = 
e7550 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 69 66 28  F_UNLCK;.    if(
e7560 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 70 46   unixFileLock(pF
e7570 69 6c 65 2c 20 26 6c 6f 63 6b 29 20 26 26 20 72  ile, &lock) && r
e7580 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
e7590 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 6f        /* This co
e75a0 75 6c 64 20 68 61 70 70 65 6e 20 77 69 74 68 20  uld happen with 
e75b0 61 20 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e 74 20  a network mount 
e75c0 2a 2f 0a 20 20 20 20 20 20 74 45 72 72 6e 6f 20  */.      tErrno 
e75d0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72  = errno;.      r
e75e0 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
e75f0 5f 55 4e 4c 4f 43 4b 3b 20 0a 20 20 20 20 7d 0a  _UNLOCK; .    }.
e7600 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20  .    if( rc ){. 
e7610 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
e7620 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20  ITE_BUSY ){.    
e7630 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
e7640 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
e7650 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74       }.      got
e7660 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20  o end_lock;.    
e7670 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69  }else{.      pFi
e7680 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20  le->eFileLock = 
e7690 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20  SHARED_LOCK;.   
e76a0 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b     pInode->nLock
e76b0 2b 2b 3b 0a 20 20 20 20 20 20 70 49 6e 6f 64 65  ++;.      pInode
e76c0 2d 3e 6e 53 68 61 72 65 64 20 3d 20 31 3b 0a 20  ->nShared = 1;. 
e76d0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
e76e0 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 4c   eFileLock==EXCL
e76f0 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 49  USIVE_LOCK && pI
e7700 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 3e 31 20  node->nShared>1 
e7710 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65  ){.    /* We are
e7720 20 74 72 79 69 6e 67 20 66 6f 72 20 61 6e 20 65   trying for an e
e7730 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 62 75  xclusive lock bu
e7740 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  t another thread
e7750 20 69 6e 20 74 68 69 73 0a 20 20 20 20 2a 2a 20   in this.    ** 
e7760 73 61 6d 65 20 70 72 6f 63 65 73 73 20 69 73 20  same process is 
e7770 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 20  still holding a 
e7780 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a  shared lock. */.
e7790 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
e77a0 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  BUSY;.  }else{. 
e77b0 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73     /* The reques
e77c0 74 20 77 61 73 20 66 6f 72 20 61 20 52 45 53 45  t was for a RESE
e77d0 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56  RVED or EXCLUSIV
e77e0 45 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20  E lock.  It is. 
e77f0 20 20 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68     ** assumed th
e7800 61 74 20 74 68 65 72 65 20 69 73 20 61 20 53 48  at there is a SH
e7810 41 52 45 44 20 6f 72 20 67 72 65 61 74 65 72 20  ARED or greater 
e7820 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65  lock on the file
e7830 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e  .    ** already.
e7840 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
e7850 72 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 65 46  rt( 0!=pFile->eF
e7860 69 6c 65 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 6c  ileLock );.    l
e7870 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57  ock.l_type = F_W
e7880 52 4c 43 4b 3b 0a 0a 20 20 20 20 61 73 73 65 72  RLCK;..    asser
e7890 74 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 52 45  t( eFileLock==RE
e78a0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 65  SERVED_LOCK || e
e78b0 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53  FileLock==EXCLUS
e78c0 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  IVE_LOCK );.    
e78d0 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 52  if( eFileLock==R
e78e0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a  ESERVED_LOCK ){.
e78f0 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61        lock.l_sta
e7900 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59  rt = RESERVED_BY
e7910 54 45 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c  TE;.      lock.l
e7920 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 20 20 7d  _len = 1L;.    }
e7930 65 6c 73 65 7b 0a 20 20 20 20 20 20 6c 6f 63 6b  else{.      lock
e7940 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45  .l_start = SHARE
e7950 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 6c  D_FIRST;.      l
e7960 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52  ock.l_len = SHAR
e7970 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 0a  ED_SIZE;.    }..
e7980 20 20 20 20 69 66 28 20 75 6e 69 78 46 69 6c 65      if( unixFile
e7990 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 26 6c 6f 63  Lock(pFile, &loc
e79a0 6b 29 20 29 7b 0a 20 20 20 20 20 20 74 45 72 72  k) ){.      tErr
e79b0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
e79c0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
e79d0 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
e79e0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
e79f0 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20  IOERR_LOCK);.   
e7a00 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
e7a10 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20  E_BUSY ){.      
e7a20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
e7a30 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
e7a40 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
e7a50 20 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   ..#ifdef SQLITE
e7a60 5f 44 45 42 55 47 0a 20 20 2f 2a 20 53 65 74 20  _DEBUG.  /* Set 
e7a70 75 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  up the transacti
e7a80 6f 6e 2d 63 6f 75 6e 74 65 72 20 63 68 61 6e 67  on-counter chang
e7a90 65 20 63 68 65 63 6b 69 6e 67 20 66 6c 61 67 73  e checking flags
e7aa0 20 77 68 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73   when.  ** trans
e7ab0 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 61 20  itioning from a 
e7ac0 53 48 41 52 45 44 20 74 6f 20 61 20 52 45 53 45  SHARED to a RESE
e7ad0 52 56 45 44 20 6c 6f 63 6b 2e 20 20 54 68 65 20  RVED lock.  The 
e7ae0 63 68 61 6e 67 65 0a 20 20 2a 2a 20 66 72 6f 6d  change.  ** from
e7af0 20 53 48 41 52 45 44 20 74 6f 20 52 45 53 45 52   SHARED to RESER
e7b00 56 45 44 20 6d 61 72 6b 73 20 74 68 65 20 62 65  VED marks the be
e7b10 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 6e 6f 72  ginning of a nor
e7b20 6d 61 6c 0a 20 20 2a 2a 20 77 72 69 74 65 20 6f  mal.  ** write o
e7b30 70 65 72 61 74 69 6f 6e 20 28 6e 6f 74 20 61 20  peration (not a 
e7b40 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c  hot journal roll
e7b50 62 61 63 6b 29 2e 0a 20 20 2a 2f 0a 20 20 69 66  back)..  */.  if
e7b60 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a  ( rc==SQLITE_OK.
e7b70 20 20 20 26 26 20 70 46 69 6c 65 2d 3e 65 46 69     && pFile->eFi
e7b80 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45 44 5f 4c  leLock<=SHARED_L
e7b90 4f 43 4b 0a 20 20 20 26 26 20 65 46 69 6c 65 4c  OCK.   && eFileL
e7ba0 6f 63 6b 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ock==RESERVED_LO
e7bb0 43 4b 0a 20 20 29 7b 0a 20 20 20 20 70 46 69 6c  CK.  ){.    pFil
e7bc0 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67  e->transCntrChng
e7bd0 20 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d   = 0;.    pFile-
e7be0 3e 64 62 55 70 64 61 74 65 20 3d 20 30 3b 0a 20  >dbUpdate = 0;. 
e7bf0 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d     pFile->inNorm
e7c00 61 6c 57 72 69 74 65 20 3d 20 31 3b 0a 20 20 7d  alWrite = 1;.  }
e7c10 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 69 66 28 20  .#endif...  if( 
e7c20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
e7c30 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c  .    pFile->eFil
e7c40 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65 4c 6f 63  eLock = eFileLoc
e7c50 6b 3b 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e 65  k;.    pInode->e
e7c60 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65  FileLock = eFile
e7c70 4c 6f 63 6b 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Lock;.  }else if
e7c80 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43  ( eFileLock==EXC
e7c90 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20  LUSIVE_LOCK ){. 
e7ca0 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c     pFile->eFileL
e7cb0 6f 63 6b 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f  ock = PENDING_LO
e7cc0 43 4b 3b 0a 20 20 20 20 70 49 6e 6f 64 65 2d 3e  CK;.    pInode->
e7cd0 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 50 45 4e 44  eFileLock = PEND
e7ce0 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 0a 65  ING_LOCK;.  }..e
e7cf0 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c  nd_lock:.  unixL
e7d00 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f  eaveMutex();.  O
e7d10 53 54 52 41 43 45 28 28 22 4c 4f 43 4b 20 20 20  STRACE(("LOCK   
e7d20 20 25 64 20 25 73 20 25 73 20 28 75 6e 69 78 29   %d %s %s (unix)
e7d30 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 61  \n", pFile->h, a
e7d40 7a 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c  zFileLock(eFileL
e7d50 6f 63 6b 29 2c 20 0a 20 20 20 20 20 20 72 63 3d  ock), .      rc=
e7d60 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b  =SQLITE_OK ? "ok
e7d70 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b 0a  " : "failed"));.
e7d80 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
e7d90 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 66 69  /*.** Add the fi
e7da0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 75 73  le descriptor us
e7db0 65 64 20 62 79 20 66 69 6c 65 20 68 61 6e 64 6c  ed by file handl
e7dc0 65 20 70 46 69 6c 65 20 74 6f 20 74 68 65 20 63  e pFile to the c
e7dd0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20  orresponding.** 
e7de0 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2f  pUnused list..*/
e7df0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74  .static void set
e7e00 50 65 6e 64 69 6e 67 46 64 28 75 6e 69 78 46 69  PendingFd(unixFi
e7e10 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 75 6e  le *pFile){.  un
e7e20 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49 6e  ixInodeInfo *pIn
e7e30 6f 64 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e  ode = pFile->pIn
e7e40 6f 64 65 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65  ode;.  UnixUnuse
e7e50 64 46 64 20 2a 70 20 3d 20 70 46 69 6c 65 2d 3e  dFd *p = pFile->
e7e60 70 55 6e 75 73 65 64 3b 0a 20 20 70 2d 3e 70 4e  pUnused;.  p->pN
e7e70 65 78 74 20 3d 20 70 49 6e 6f 64 65 2d 3e 70 55  ext = pInode->pU
e7e80 6e 75 73 65 64 3b 0a 20 20 70 49 6e 6f 64 65 2d  nused;.  pInode-
e7e90 3e 70 55 6e 75 73 65 64 20 3d 20 70 3b 0a 20 20  >pUnused = p;.  
e7ea0 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20  pFile->h = -1;. 
e7eb0 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 20   pFile->pUnused 
e7ec0 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  = 0;.}../*.** Lo
e7ed0 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  wer the locking 
e7ee0 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65  level on file de
e7ef0 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74  scriptor pFile t
e7f00 6f 20 65 46 69 6c 65 4c 6f 63 6b 2e 20 20 65 46  o eFileLock.  eF
e7f10 69 6c 65 4c 6f 63 6b 0a 2a 2a 20 6d 75 73 74 20  ileLock.** must 
e7f20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43  be either NO_LOC
e7f30 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b  K or SHARED_LOCK
e7f40 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
e7f50 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20  ocking level of 
e7f60 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
e7f70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61  tor is already a
e7f80 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68  t or below.** th
e7f90 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  e requested lock
e7fa0 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20  ing level, this 
e7fb0 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
e7fc0 6f 70 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 68 61  op..** .** If ha
e7fd0 6e 64 6c 65 4e 46 53 55 6e 6c 6f 63 6b 20 69 73  ndleNFSUnlock is
e7fe0 20 74 72 75 65 2c 20 74 68 65 6e 20 6f 6e 20 64   true, then on d
e7ff0 6f 77 6e 67 72 61 64 69 6e 67 20 61 6e 20 45 58  owngrading an EX
e8000 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 74 6f 20  CLUSIVE_LOCK to 
e8010 53 48 41 52 45 44 0a 2a 2a 20 74 68 65 20 62 79  SHARED.** the by
e8020 74 65 20 72 61 6e 67 65 20 69 73 20 64 69 76 69  te range is divi
e8030 64 65 64 20 69 6e 74 6f 20 32 20 70 61 72 74 73  ded into 2 parts
e8040 20 61 6e 64 20 74 68 65 20 66 69 72 73 74 20 70   and the first p
e8050 61 72 74 20 69 73 20 75 6e 6c 6f 63 6b 65 64 20  art is unlocked 
e8060 74 68 65 6e 0a 2a 2a 20 73 65 74 20 74 6f 20 61  then.** set to a
e8070 20 72 65 61 64 20 6c 6f 63 6b 2c 20 74 68 65 6e   read lock, then
e8080 20 74 68 65 20 6f 74 68 65 72 20 70 61 72 74 20   the other part 
e8090 69 73 20 73 69 6d 70 6c 79 20 75 6e 6c 6f 63 6b  is simply unlock
e80a0 65 64 2e 20 20 54 68 69 73 20 77 6f 72 6b 73 20  ed.  This works 
e80b0 0a 2a 2a 20 61 72 6f 75 6e 64 20 61 20 62 75 67  .** around a bug
e80c0 20 69 6e 20 42 53 44 20 4e 46 53 20 6c 6f 63 6b   in BSD NFS lock
e80d0 64 20 28 61 6c 73 6f 20 73 65 65 6e 20 6f 6e 20  d (also seen on 
e80e0 4d 61 63 4f 53 58 20 31 30 2e 33 2b 29 20 74 68  MacOSX 10.3+) th
e80f0 61 74 20 66 61 69 6c 73 20 74 6f 20 0a 2a 2a 20  at fails to .** 
e8100 72 65 6d 6f 76 65 20 74 68 65 20 77 72 69 74 65  remove the write
e8110 20 6c 6f 63 6b 20 6f 6e 20 61 20 72 65 67 69 6f   lock on a regio
e8120 6e 20 77 68 65 6e 20 61 20 72 65 61 64 20 6c 6f  n when a read lo
e8130 63 6b 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74  ck is set..*/.st
e8140 61 74 69 63 20 69 6e 74 20 70 6f 73 69 78 55 6e  atic int posixUn
e8150 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
e8160 65 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65  e *id, int eFile
e8170 4c 6f 63 6b 2c 20 69 6e 74 20 68 61 6e 64 6c 65  Lock, int handle
e8180 4e 46 53 55 6e 6c 6f 63 6b 29 7b 0a 20 20 75 6e  NFSUnlock){.  un
e8190 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
e81a0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
e81b0 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a   unixInodeInfo *
e81c0 70 49 6e 6f 64 65 3b 0a 20 20 73 74 72 75 63 74  pInode;.  struct
e81d0 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69   flock lock;.  i
e81e0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
e81f0 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46  K;..  assert( pF
e8200 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  ile );.  OSTRACE
e8210 28 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64  (("UNLOCK  %d %d
e8220 20 77 61 73 20 25 64 28 25 64 2c 25 64 29 20 70   was %d(%d,%d) p
e8230 69 64 3d 25 64 20 28 75 6e 69 78 29 5c 6e 22 2c  id=%d (unix)\n",
e8240 20 70 46 69 6c 65 2d 3e 68 2c 20 65 46 69 6c 65   pFile->h, eFile
e8250 4c 6f 63 6b 2c 0a 20 20 20 20 20 20 70 46 69 6c  Lock,.      pFil
e8260 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 2c 20 70 46  e->eFileLock, pF
e8270 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 65 46 69  ile->pInode->eFi
e8280 6c 65 4c 6f 63 6b 2c 20 70 46 69 6c 65 2d 3e 70  leLock, pFile->p
e8290 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 2c 0a  Inode->nShared,.
e82a0 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29 29        getpid()))
e82b0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 65 46 69  ;..  assert( eFi
e82c0 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45 44 5f 4c  leLock<=SHARED_L
e82d0 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 46 69  OCK );.  if( pFi
e82e0 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3c 3d 65  le->eFileLock<=e
e82f0 46 69 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20  FileLock ){.    
e8300 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e8310 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65  ;.  }.  unixEnte
e8320 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 49 6e 6f  rMutex();.  pIno
e8330 64 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f  de = pFile->pIno
e8340 64 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49  de;.  assert( pI
e8350 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 21 3d 30  node->nShared!=0
e8360 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   );.  if( pFile-
e8370 3e 65 46 69 6c 65 4c 6f 63 6b 3e 53 48 41 52 45  >eFileLock>SHARE
e8380 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73  D_LOCK ){.    as
e8390 73 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 65 46  sert( pInode->eF
e83a0 69 6c 65 4c 6f 63 6b 3d 3d 70 46 69 6c 65 2d 3e  ileLock==pFile->
e83b0 65 46 69 6c 65 4c 6f 63 6b 20 29 3b 0a 0a 23 69  eFileLock );..#i
e83c0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
e83d0 47 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 72 65  G.    /* When re
e83e0 64 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20 73 75  ducing a lock su
e83f0 63 68 20 74 68 61 74 20 6f 74 68 65 72 20 70 72  ch that other pr
e8400 6f 63 65 73 73 65 73 20 63 61 6e 20 73 74 61 72  ocesses can star
e8410 74 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67  t.    ** reading
e8420 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
e8430 6c 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65 20 73  le again, make s
e8440 75 72 65 20 74 68 61 74 20 74 68 65 0a 20 20 20  ure that the.   
e8450 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
e8460 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 61  counter was upda
e8470 74 65 64 20 69 66 20 61 6e 79 20 70 61 72 74 20  ted if any part 
e8480 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  of the database.
e8490 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 68 61 6e      ** file chan
e84a0 67 65 64 2e 20 20 49 66 20 74 68 65 20 74 72 61  ged.  If the tra
e84b0 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72  nsaction counter
e84c0 20 69 73 20 6e 6f 74 20 75 70 64 61 74 65 64 2c   is not updated,
e84d0 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f  .    ** other co
e84e0 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 65  nnections to the
e84f0 20 73 61 6d 65 20 66 69 6c 65 20 6d 69 67 68 74   same file might
e8500 20 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74 68 61   not realize tha
e8510 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6c  t.    ** the fil
e8520 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 61 6e  e has changed an
e8530 64 20 68 65 6e 63 65 20 6d 69 67 68 74 20 6e 6f  d hence might no
e8540 74 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73 68 20  t know to flush 
e8550 74 68 65 69 72 0a 20 20 20 20 2a 2a 20 63 61 63  their.    ** cac
e8560 68 65 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20  he.  The use of 
e8570 61 20 73 74 61 6c 65 20 63 61 63 68 65 20 63 61  a stale cache ca
e8580 6e 20 6c 65 61 64 20 74 6f 20 64 61 74 61 62 61  n lead to databa
e8590 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20  se corruption.. 
e85a0 20 20 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d     */.    pFile-
e85b0 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d  >inNormalWrite =
e85c0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20   0;.#endif..    
e85d0 2f 2a 20 64 6f 77 6e 67 72 61 64 69 6e 67 20 74  /* downgrading t
e85e0 6f 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  o a shared lock 
e85f0 6f 6e 20 4e 46 53 20 69 6e 76 6f 6c 76 65 73 20  on NFS involves 
e8600 63 6c 65 61 72 69 6e 67 20 74 68 65 20 77 72 69  clearing the wri
e8610 74 65 20 6c 6f 63 6b 0a 20 20 20 20 2a 2a 20 62  te lock.    ** b
e8620 65 66 6f 72 65 20 65 73 74 61 62 6c 69 73 68 69  efore establishi
e8630 6e 67 20 74 68 65 20 72 65 61 64 6c 6f 63 6b 20  ng the readlock 
e8640 2d 20 74 6f 20 61 76 6f 69 64 20 61 20 72 61 63  - to avoid a rac
e8650 65 20 63 6f 6e 64 69 74 69 6f 6e 20 77 65 20 64  e condition we d
e8660 6f 77 6e 67 72 61 64 65 0a 20 20 20 20 2a 2a 20  owngrade.    ** 
e8670 74 68 65 20 6c 6f 63 6b 20 69 6e 20 32 20 62 6c  the lock in 2 bl
e8680 6f 63 6b 73 2c 20 73 6f 20 74 68 61 74 20 70 61  ocks, so that pa
e8690 72 74 20 6f 66 20 74 68 65 20 72 61 6e 67 65 20  rt of the range 
e86a0 77 69 6c 6c 20 62 65 20 63 6f 76 65 72 65 64 20  will be covered 
e86b0 62 79 20 61 20 0a 20 20 20 20 2a 2a 20 77 72 69  by a .    ** wri
e86c0 74 65 20 6c 6f 63 6b 20 75 6e 74 69 6c 20 74 68  te lock until th
e86d0 65 20 72 65 73 74 20 69 73 20 63 6f 76 65 72 65  e rest is covere
e86e0 64 20 62 79 20 61 20 72 65 61 64 20 6c 6f 63 6b  d by a read lock
e86f0 3a 0a 20 20 20 20 2a 2a 20 20 31 3a 20 20 20 5b  :.    **  1:   [
e8700 57 57 57 57 57 5d 0a 20 20 20 20 2a 2a 20 20 32  WWWWW].    **  2
e8710 3a 20 20 20 5b 2e 2e 2e 2e 57 5d 0a 20 20 20 20  :   [....W].    
e8720 2a 2a 20 20 33 3a 20 20 20 5b 52 52 52 52 57 5d  **  3:   [RRRRW]
e8730 0a 20 20 20 20 2a 2a 20 20 34 3a 20 20 20 5b 52  .    **  4:   [R
e8740 52 52 52 2e 5d 0a 20 20 20 20 2a 2f 0a 20 20 20  RRR.].    */.   
e8750 20 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d   if( eFileLock==
e8760 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 0a  SHARED_LOCK ){..
e8770 23 69 66 20 21 64 65 66 69 6e 65 64 28 5f 5f 41  #if !defined(__A
e8780 50 50 4c 45 5f 5f 29 20 7c 7c 20 21 53 51 4c 49  PPLE__) || !SQLI
e8790 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
e87a0 47 5f 53 54 59 4c 45 0a 20 20 20 20 20 20 28 76  G_STYLE.      (v
e87b0 6f 69 64 29 68 61 6e 64 6c 65 4e 46 53 55 6e 6c  oid)handleNFSUnl
e87c0 6f 63 6b 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ock;.      asser
e87d0 74 28 20 68 61 6e 64 6c 65 4e 46 53 55 6e 6c 6f  t( handleNFSUnlo
e87e0 63 6b 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a  ck==0 );.#endif.
e87f0 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50  #if defined(__AP
e8800 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45  PLE__) && SQLITE
e8810 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
e8820 53 54 59 4c 45 0a 20 20 20 20 20 20 69 66 28 20  STYLE.      if( 
e8830 68 61 6e 64 6c 65 4e 46 53 55 6e 6c 6f 63 6b 20  handleNFSUnlock 
e8840 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 74  ){.        int t
e8850 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 20 20  Errno;          
e8860 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f       /* Error co
e8870 64 65 20 66 72 6f 6d 20 73 79 73 74 65 6d 20 63  de from system c
e8880 61 6c 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  all errors */.  
e8890 20 20 20 20 20 20 6f 66 66 5f 74 20 64 69 76 53        off_t divS
e88a0 69 7a 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a  ize = SHARED_SIZ
e88b0 45 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 0a  E - 1;.        .
e88c0 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74          lock.l_t
e88d0 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20  ype = F_UNLCK;. 
e88e0 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68         lock.l_wh
e88f0 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b  ence = SEEK_SET;
e8900 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  .        lock.l_
e8910 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46  start = SHARED_F
e8920 49 52 53 54 3b 0a 20 20 20 20 20 20 20 20 6c 6f  IRST;.        lo
e8930 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 64 69 76 53 69  ck.l_len = divSi
e8940 7a 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ze;.        if( 
e8950 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 70 46 69  unixFileLock(pFi
e8960 6c 65 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31 29  le, &lock)==(-1)
e8970 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74 45   ){.          tE
e8980 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
e8990 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
e89a0 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
e89b0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
e89c0 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63  IS_LOCK_ERROR(rc
e89d0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
e89e0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
e89f0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
e8a00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
e8a10 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63    goto end_unloc
e8a20 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
e8a30 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65       lock.l_type
e8a40 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 20 20   = F_RDLCK;.    
e8a50 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63      lock.l_whenc
e8a60 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20  e = SEEK_SET;.  
e8a70 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61        lock.l_sta
e8a80 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53  rt = SHARED_FIRS
e8a90 54 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e  T;.        lock.
e8aa0 6c 5f 6c 65 6e 20 3d 20 64 69 76 53 69 7a 65 3b  l_len = divSize;
e8ab0 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 6e 69  .        if( uni
e8ac0 78 46 69 6c 65 4c 6f 63 6b 28 70 46 69 6c 65 2c  xFileLock(pFile,
e8ad0 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31 29 20 29 7b   &lock)==(-1) ){
e8ae0 0a 20 20 20 20 20 20 20 20 20 20 74 45 72 72 6e  .          tErrn
e8af0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  o = errno;.     
e8b00 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
e8b10 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
e8b20 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
e8b30 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29  TE_IOERR_RDLOCK)
e8b40 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
e8b50 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63  IS_LOCK_ERROR(rc
e8b60 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
e8b70 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
e8b80 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
e8b90 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
e8ba0 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63    goto end_unloc
e8bb0 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
e8bc0 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65       lock.l_type
e8bd0 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20   = F_UNLCK;.    
e8be0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63      lock.l_whenc
e8bf0 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20  e = SEEK_SET;.  
e8c00 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61        lock.l_sta
e8c10 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53  rt = SHARED_FIRS
e8c20 54 2b 64 69 76 53 69 7a 65 3b 0a 20 20 20 20 20  T+divSize;.     
e8c30 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20     lock.l_len = 
e8c40 53 48 41 52 45 44 5f 53 49 5a 45 2d 64 69 76 53  SHARED_SIZE-divS
e8c50 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ize;.        if(
e8c60 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28 70 46   unixFileLock(pF
e8c70 69 6c 65 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31  ile, &lock)==(-1
e8c80 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74  ) ){.          t
e8c90 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
e8ca0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
e8cb0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
e8cc0 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  K;.          if(
e8cd0 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72   IS_LOCK_ERROR(r
e8ce0 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  c) ){.          
e8cf0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
e8d00 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
e8d10 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
e8d20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f     goto end_unlo
e8d30 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ck;.        }.  
e8d40 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
e8d50 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50   /* defined(__AP
e8d60 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45  PLE__) && SQLITE
e8d70 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
e8d80 53 54 59 4c 45 20 2a 2f 0a 20 20 20 20 20 20 7b  STYLE */.      {
e8d90 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  .        lock.l_
e8da0 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a  type = F_RDLCK;.
e8db0 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77          lock.l_w
e8dc0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54  hence = SEEK_SET
e8dd0 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c  ;.        lock.l
e8de0 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f  _start = SHARED_
e8df0 46 49 52 53 54 3b 0a 20 20 20 20 20 20 20 20 6c  FIRST;.        l
e8e00 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52  ock.l_len = SHAR
e8e10 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 20  ED_SIZE;.       
e8e20 20 69 66 28 20 75 6e 69 78 46 69 6c 65 4c 6f 63   if( unixFileLoc
e8e30 6b 28 70 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 20  k(pFile, &lock) 
e8e40 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
e8e50 49 6e 20 74 68 65 6f 72 79 2c 20 74 68 65 20 63  In theory, the c
e8e60 61 6c 6c 20 74 6f 20 75 6e 69 78 46 69 6c 65 4c  all to unixFileL
e8e70 6f 63 6b 28 29 20 63 61 6e 6e 6f 74 20 66 61 69  ock() cannot fai
e8e80 6c 20 62 65 63 61 75 73 65 20 61 6e 6f 74 68 65  l because anothe
e8e90 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70  r.          ** p
e8ea0 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e  rocess is holdin
e8eb0 67 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c  g an incompatibl
e8ec0 65 20 6c 6f 63 6b 2e 20 49 66 20 69 74 20 64 6f  e lock. If it do
e8ed0 65 73 2c 20 74 68 69 73 20 0a 20 20 20 20 20 20  es, this .      
e8ee0 20 20 20 20 2a 2a 20 69 6e 64 69 63 61 74 65 73      ** indicates
e8ef0 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 72 20   that the other 
e8f00 70 72 6f 63 65 73 73 20 69 73 20 6e 6f 74 20 66  process is not f
e8f10 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 6f 63  ollowing the loc
e8f20 6b 69 6e 67 0a 20 20 20 20 20 20 20 20 20 20 2a  king.          *
e8f30 2a 20 70 72 6f 74 6f 63 6f 6c 2e 20 49 66 20 74  * protocol. If t
e8f40 68 69 73 20 68 61 70 70 65 6e 73 2c 20 72 65 74  his happens, ret
e8f50 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
e8f60 5f 52 44 4c 4f 43 4b 2e 20 52 65 74 75 72 6e 69  _RDLOCK. Returni
e8f70 6e 67 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  ng.          ** 
e8f80 53 51 4c 49 54 45 5f 42 55 53 59 20 77 6f 75 6c  SQLITE_BUSY woul
e8f90 64 20 63 6f 6e 66 75 73 65 20 74 68 65 20 75 70  d confuse the up
e8fa0 70 65 72 20 6c 61 79 65 72 20 28 69 6e 20 70 72  per layer (in pr
e8fb0 61 63 74 69 63 65 20 69 74 20 63 61 75 73 65 73  actice it causes
e8fc0 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61   .          ** a
e8fd0 6e 20 61 73 73 65 72 74 20 74 6f 20 66 61 69 6c  n assert to fail
e8fe0 29 2e 20 2a 2f 20 0a 20 20 20 20 20 20 20 20 20  ). */ .         
e8ff0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45   rc = SQLITE_IOE
e9000 52 52 5f 52 44 4c 4f 43 4b 3b 0a 20 20 20 20 20  RR_RDLOCK;.     
e9010 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
e9020 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
e9030 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e           goto en
e9040 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20  d_unlock;.      
e9050 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
e9060 7d 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70  }.    lock.l_typ
e9070 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20  e = F_UNLCK;.   
e9080 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d   lock.l_whence =
e9090 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c   SEEK_SET;.    l
e90a0 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45  ock.l_start = PE
e90b0 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20  NDING_BYTE;.    
e90c0 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b  lock.l_len = 2L;
e90d0 20 20 61 73 73 65 72 74 28 20 50 45 4e 44 49 4e    assert( PENDIN
e90e0 47 5f 42 59 54 45 2b 31 3d 3d 52 45 53 45 52 56  G_BYTE+1==RESERV
e90f0 45 44 5f 42 59 54 45 20 29 3b 0a 20 20 20 20 69  ED_BYTE );.    i
e9100 66 28 20 75 6e 69 78 46 69 6c 65 4c 6f 63 6b 28  f( unixFileLock(
e9110 70 46 69 6c 65 2c 20 26 6c 6f 63 6b 29 3d 3d 30  pFile, &lock)==0
e9120 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 6f 64 65   ){.      pInode
e9130 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 53 48  ->eFileLock = SH
e9140 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d  ARED_LOCK;.    }
e9150 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
e9160 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e   SQLITE_IOERR_UN
e9170 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69 6c  LOCK;.      pFil
e9180 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  e->lastErrno = e
e9190 72 72 6e 6f 3b 0a 20 20 20 20 20 20 67 6f 74 6f  rrno;.      goto
e91a0 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20   end_unlock;.   
e91b0 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 65 46 69   }.  }.  if( eFi
e91c0 6c 65 4c 6f 63 6b 3d 3d 4e 4f 5f 4c 4f 43 4b 20  leLock==NO_LOCK 
e91d0 29 7b 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d  ){.    /* Decrem
e91e0 65 6e 74 20 74 68 65 20 73 68 61 72 65 64 20 6c  ent the shared l
e91f0 6f 63 6b 20 63 6f 75 6e 74 65 72 2e 20 20 52 65  ock counter.  Re
e9200 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b 20 75  lease the lock u
e9210 73 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 4f  sing an.    ** O
e9220 53 20 63 61 6c 6c 20 6f 6e 6c 79 20 77 68 65 6e  S call only when
e9230 20 61 6c 6c 20 74 68 72 65 61 64 73 20 69 6e 20   all threads in 
e9240 74 68 69 73 20 73 61 6d 65 20 70 72 6f 63 65 73  this same proces
e9250 73 20 68 61 76 65 20 72 65 6c 65 61 73 65 64 0a  s have released.
e9260 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e      ** the lock.
e9270 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 6e 6f  .    */.    pIno
e9280 64 65 2d 3e 6e 53 68 61 72 65 64 2d 2d 3b 0a 20  de->nShared--;. 
e9290 20 20 20 69 66 28 20 70 49 6e 6f 64 65 2d 3e 6e     if( pInode->n
e92a0 53 68 61 72 65 64 3d 3d 30 20 29 7b 0a 20 20 20  Shared==0 ){.   
e92b0 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d     lock.l_type =
e92c0 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20   F_UNLCK;.      
e92d0 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20  lock.l_whence = 
e92e0 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20  SEEK_SET;.      
e92f0 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 6c  lock.l_start = l
e9300 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c 3b 0a  ock.l_len = 0L;.
e9310 20 20 20 20 20 20 69 66 28 20 75 6e 69 78 46 69        if( unixFi
e9320 6c 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 26 6c  leLock(pFile, &l
e9330 6f 63 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ock)==0 ){.     
e9340 20 20 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65     pInode->eFile
e9350 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a  Lock = NO_LOCK;.
e9360 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
e9370 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
e9380 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20  _IOERR_UNLOCK;. 
e9390 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
e93a0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
e93b0 0a 20 20 20 20 20 20 20 20 70 49 6e 6f 64 65 2d  .        pInode-
e93c0 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 4e 4f 5f  >eFileLock = NO_
e93d0 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 70 46  LOCK;.        pF
e93e0 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d  ile->eFileLock =
e93f0 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20   NO_LOCK;.      
e9400 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
e9410 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f  Decrement the co
e9420 75 6e 74 20 6f 66 20 6c 6f 63 6b 73 20 61 67 61  unt of locks aga
e9430 69 6e 73 74 20 74 68 69 73 20 73 61 6d 65 20 66  inst this same f
e9440 69 6c 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 20  ile.  When the. 
e9450 20 20 20 2a 2a 20 63 6f 75 6e 74 20 72 65 61 63     ** count reac
e9460 68 65 73 20 7a 65 72 6f 2c 20 63 6c 6f 73 65 20  hes zero, close 
e9470 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65 20 64  any other file d
e9480 65 73 63 72 69 70 74 6f 72 73 20 77 68 6f 73 65  escriptors whose
e9490 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 77 61   close.    ** wa
e94a0 73 20 64 65 66 65 72 72 65 64 20 62 65 63 61 75  s deferred becau
e94b0 73 65 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e  se of outstandin
e94c0 67 20 6c 6f 63 6b 73 2e 0a 20 20 20 20 2a 2f 0a  g locks..    */.
e94d0 20 20 20 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63      pInode->nLoc
e94e0 6b 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  k--;.    assert(
e94f0 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 3e 3d   pInode->nLock>=
e9500 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49 6e  0 );.    if( pIn
e9510 6f 64 65 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b  ode->nLock==0 ){
e9520 0a 20 20 20 20 20 20 63 6c 6f 73 65 50 65 6e 64  .      closePend
e9530 69 6e 67 46 64 73 28 70 46 69 6c 65 29 3b 0a 20  ingFds(pFile);. 
e9540 20 20 20 7d 0a 20 20 7d 0a 0a 65 6e 64 5f 75 6e     }.  }..end_un
e9550 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76  lock:.  unixLeav
e9560 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20  eMutex();.  if( 
e9570 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc==SQLITE_OK ) 
e9580 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b  pFile->eFileLock
e9590 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20   = eFileLock;.  
e95a0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
e95b0 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f  .** Lower the lo
e95c0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66  cking level on f
e95d0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70  ile descriptor p
e95e0 46 69 6c 65 20 74 6f 20 65 46 69 6c 65 4c 6f 63  File to eFileLoc
e95f0 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 6b 0a 2a 2a  k.  eFileLock.**
e9600 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
e9610 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45  NO_LOCK or SHARE
e9620 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  D_LOCK..**.** If
e9630 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
e9640 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64  el of the file d
e9650 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72  escriptor is alr
e9660 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77  eady at or below
e9670 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65  .** the requeste
e9680 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c  d locking level,
e9690 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
e96a0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61   a no-op..*/.sta
e96b0 74 69 63 20 69 6e 74 20 75 6e 69 78 55 6e 6c 6f  tic int unixUnlo
e96c0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
e96d0 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f  *id, int eFileLo
e96e0 63 6b 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 6f  ck){.  return po
e96f0 73 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 65 46  sixUnlock(id, eF
e9700 69 6c 65 4c 6f 63 6b 2c 20 30 29 3b 0a 7d 0a 0a  ileLock, 0);.}..
e9710 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
e9720 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20 74 68 65  ion performs the
e9730 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 22 63   parts of the "c
e9740 6c 6f 73 65 20 66 69 6c 65 22 20 6f 70 65 72 61  lose file" opera
e9750 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20  tion .** common 
e9760 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 73  to all locking s
e9770 63 68 65 6d 65 73 2e 20 49 74 20 63 6c 6f 73 65  chemes. It close
e9780 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  s the directory 
e9790 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 68 61 6e 64  and file.** hand
e97a0 6c 65 73 2c 20 69 66 20 74 68 65 79 20 61 72 65  les, if they are
e97b0 20 76 61 6c 69 64 2c 20 61 6e 64 20 73 65 74 73   valid, and sets
e97c0 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66 20 74   all fields of t
e97d0 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73  he unixFile.** s
e97e0 74 72 75 63 74 75 72 65 20 74 6f 20 30 2e 0a 2a  tructure to 0..*
e97f0 2a 0a 2a 2a 20 49 74 20 69 73 20 2a 6e 6f 74 2a  *.** It is *not*
e9800 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 68 6f   necessary to ho
e9810 6c 64 20 74 68 65 20 6d 75 74 65 78 20 77 68 65  ld the mutex whe
e9820 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
e9830 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 65 76 65  s called,.** eve
e9840 6e 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 20 20 41  n on VxWorks.  A
e9850 20 6d 75 74 65 78 20 77 69 6c 6c 20 62 65 20 61   mutex will be a
e9860 63 71 75 69 72 65 64 20 6f 6e 20 56 78 57 6f 72  cquired on VxWor
e9870 6b 73 20 62 79 20 74 68 65 0a 2a 2a 20 76 78 77  ks by the.** vxw
e9880 6f 72 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49  orksReleaseFileI
e9890 64 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a  d() routine..*/.
e98a0 73 74 61 74 69 63 20 69 6e 74 20 63 6c 6f 73 65  static int close
e98b0 55 6e 69 78 46 69 6c 65 28 73 71 6c 69 74 65 33  UnixFile(sqlite3
e98c0 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 75 6e  _file *id){.  un
e98d0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
e98e0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
e98f0 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30   if( pFile->h>=0
e9900 20 29 7b 0a 20 20 20 20 72 6f 62 75 73 74 5f 63   ){.    robust_c
e9910 6c 6f 73 65 28 70 46 69 6c 65 2c 20 70 46 69 6c  lose(pFile, pFil
e9920 65 2d 3e 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b  e->h, __LINE__);
e9930 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20  .    pFile->h = 
e9940 2d 31 3b 0a 20 20 7d 0a 23 69 66 20 4f 53 5f 56  -1;.  }.#if OS_V
e9950 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 70 46 69  XWORKS.  if( pFi
e9960 6c 65 2d 3e 70 49 64 20 29 7b 0a 20 20 20 20 69  le->pId ){.    i
e9970 66 28 20 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c  f( pFile->ctrlFl
e9980 61 67 73 20 26 20 55 4e 49 58 46 49 4c 45 5f 44  ags & UNIXFILE_D
e9990 45 4c 45 54 45 20 29 7b 0a 20 20 20 20 20 20 6f  ELETE ){.      o
e99a0 73 55 6e 6c 69 6e 6b 28 70 46 69 6c 65 2d 3e 70  sUnlink(pFile->p
e99b0 49 64 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61  Id->zCanonicalNa
e99c0 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 76  me);.    }.    v
e99d0 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 69 6c  xworksReleaseFil
e99e0 65 49 64 28 70 46 69 6c 65 2d 3e 70 49 64 29 3b  eId(pFile->pId);
e99f0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 49 64 20  .    pFile->pId 
e9a00 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  = 0;.  }.#endif.
e9a10 20 20 4f 53 54 52 41 43 45 28 28 22 43 4c 4f 53    OSTRACE(("CLOS
e9a20 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69  E   %-3d\n", pFi
e9a30 6c 65 2d 3e 68 29 29 3b 0a 20 20 4f 70 65 6e 43  le->h));.  OpenC
e9a40 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 73 71  ounter(-1);.  sq
e9a50 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65  lite3_free(pFile
e9a60 2d 3e 70 55 6e 75 73 65 64 29 3b 0a 20 20 6d 65  ->pUnused);.  me
e9a70 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73  mset(pFile, 0, s
e9a80 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29  izeof(unixFile))
e9a90 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
e9aa0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
e9ab0 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a  lose a file..*/.
e9ac0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43  static int unixC
e9ad0 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  lose(sqlite3_fil
e9ae0 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 63  e *id){.  int rc
e9af0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
e9b00 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
e9b10 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 69 64  = (unixFile *)id
e9b20 3b 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 69  ;.  unixUnlock(i
e9b30 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 75  d, NO_LOCK);.  u
e9b40 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
e9b50 0a 0a 20 20 2f 2a 20 75 6e 69 78 46 69 6c 65 2e  ..  /* unixFile.
e9b60 70 49 6e 6f 64 65 20 69 73 20 61 6c 77 61 79 73  pInode is always
e9b70 20 76 61 6c 69 64 20 68 65 72 65 2e 20 4f 74 68   valid here. Oth
e9b80 65 72 77 69 73 65 2c 20 61 20 64 69 66 66 65 72  erwise, a differ
e9b90 65 6e 74 20 63 6c 6f 73 65 0a 20 20 2a 2a 20 72  ent close.  ** r
e9ba0 6f 75 74 69 6e 65 20 28 65 2e 67 2e 20 6e 6f 6c  outine (e.g. nol
e9bb0 6f 63 6b 43 6c 6f 73 65 28 29 29 20 77 6f 75 6c  ockClose()) woul
e9bc0 64 20 62 65 20 63 61 6c 6c 65 64 20 69 6e 73 74  d be called inst
e9bd0 65 61 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ead..  */.  asse
e9be0 72 74 28 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64  rt( pFile->pInod
e9bf0 65 2d 3e 6e 4c 6f 63 6b 3e 30 20 7c 7c 20 70 46  e->nLock>0 || pF
e9c00 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 62 50 72  ile->pInode->bPr
e9c10 6f 63 65 73 73 4c 6f 63 6b 3d 3d 30 20 29 3b 0a  ocessLock==0 );.
e9c20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 46 69    if( ALWAYS(pFi
e9c30 6c 65 2d 3e 70 49 6e 6f 64 65 29 20 26 26 20 70  le->pInode) && p
e9c40 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 6e 4c  File->pInode->nL
e9c50 6f 63 6b 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  ock ){.    /* If
e9c60 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74   there are outst
e9c70 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f  anding locks, do
e9c80 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c   not actually cl
e9c90 6f 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73  ose the file jus
e9ca0 74 0a 20 20 20 20 2a 2a 20 79 65 74 20 62 65 63  t.    ** yet bec
e9cb0 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20  ause that would 
e9cc0 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 6b  clear those lock
e9cd0 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64 64  s.  Instead, add
e9ce0 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a   the file.    **
e9cf0 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 70   descriptor to p
e9d00 49 6e 6f 64 65 2d 3e 70 55 6e 75 73 65 64 20 6c  Inode->pUnused l
e9d10 69 73 74 2e 20 20 49 74 20 77 69 6c 6c 20 62 65  ist.  It will be
e9d20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63   automatically c
e9d30 6c 6f 73 65 64 20 0a 20 20 20 20 2a 2a 20 77 68  losed .    ** wh
e9d40 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b  en the last lock
e9d50 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20   is cleared..   
e9d60 20 2a 2f 0a 20 20 20 20 73 65 74 50 65 6e 64 69   */.    setPendi
e9d70 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a 20 20 7d  ngFd(pFile);.  }
e9d80 0a 20 20 72 65 6c 65 61 73 65 49 6e 6f 64 65 49  .  releaseInodeI
e9d90 6e 66 6f 28 70 46 69 6c 65 29 3b 0a 20 20 72 63  nfo(pFile);.  rc
e9da0 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65   = closeUnixFile
e9db0 28 69 64 29 3b 0a 20 20 75 6e 69 78 4c 65 61 76  (id);.  unixLeav
e9dc0 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75  eMutex();.  retu
e9dd0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  rn rc;.}../*****
e9de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
e9df0 20 74 68 65 20 70 6f 73 69 78 20 61 64 76 69 73   the posix advis
e9e00 6f 72 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65  ory lock impleme
e9e10 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a  ntation ********
e9e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
e9e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9e70 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a  ********/../****
e9e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
e9ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 2d 6f 70 20  ********* No-op 
e9ef0 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a  Locking ********
e9f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
e9f20 20 4f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20   Of the various 
e9f30 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e  locking implemen
e9f40 74 61 74 69 6f 6e 73 20 61 76 61 69 6c 61 62 6c  tations availabl
e9f50 65 2c 20 74 68 69 73 20 69 73 20 62 79 20 66 61  e, this is by fa
e9f60 72 20 74 68 65 0a 2a 2a 20 73 69 6d 70 6c 65 73  r the.** simples
e9f70 74 3a 20 20 6c 6f 63 6b 69 6e 67 20 69 73 20 69  t:  locking is i
e9f80 67 6e 6f 72 65 64 2e 20 20 4e 6f 20 61 74 74 65  gnored.  No atte
e9f90 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6c  mpt is made to l
e9fa0 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65  ock the database
e9fb0 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 72 65 61  .** file for rea
e9fc0 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 2e  ding or writing.
e9fd0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 63 6b  .**.** This lock
e9fe0 69 6e 67 20 6d 6f 64 65 20 69 73 20 61 70 70 72  ing mode is appr
e9ff0 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73 65 20  opriate for use 
ea000 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74  on read-only dat
ea010 61 62 61 73 65 73 0a 2a 2a 20 28 65 78 3a 20 64  abases.** (ex: d
ea020 61 74 61 62 61 73 65 73 20 74 68 61 74 20 61 72  atabases that ar
ea030 65 20 62 75 72 6e 65 64 20 69 6e 74 6f 20 43 44  e burned into CD
ea040 2d 52 4f 4d 2c 20 66 6f 72 20 65 78 61 6d 70 6c  -ROM, for exampl
ea050 65 2e 29 20 20 49 74 20 63 61 6e 0a 2a 2a 20 61  e.)  It can.** a
ea060 6c 73 6f 20 62 65 20 75 73 65 64 20 69 66 20 74  lso be used if t
ea070 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 65  he application e
ea080 6d 70 6c 6f 79 73 20 73 6f 6d 65 20 65 78 74 65  mploys some exte
ea090 72 6e 61 6c 20 6d 65 63 68 61 6e 69 73 6d 20 74  rnal mechanism t
ea0a0 6f 0a 2a 2a 20 70 72 65 76 65 6e 74 20 73 69 6d  o.** prevent sim
ea0b0 75 6c 74 61 6e 65 6f 75 73 20 61 63 63 65 73 73  ultaneous access
ea0c0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 64 61 74   of the same dat
ea0d0 61 62 61 73 65 20 62 79 20 74 77 6f 20 6f 72 20  abase by two or 
ea0e0 6d 6f 72 65 0a 2a 2a 20 64 61 74 61 62 61 73 65  more.** database
ea0f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 42   connections.  B
ea100 75 74 20 74 68 65 72 65 20 69 73 20 61 20 73 65  ut there is a se
ea110 72 69 6f 75 73 20 72 69 73 6b 20 6f 66 20 64 61  rious risk of da
ea120 74 61 62 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70  tabase.** corrup
ea130 74 69 6f 6e 20 69 66 20 74 68 69 73 20 6c 6f 63  tion if this loc
ea140 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 75 73 65  king mode is use
ea150 64 20 69 6e 20 73 69 74 75 61 74 69 6f 6e 73 20  d in situations 
ea160 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65 0a 2a  where multiple.*
ea170 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
ea180 63 74 69 6f 6e 73 20 61 72 65 20 61 63 63 65 73  ctions are acces
ea190 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61  sing the same da
ea1a0 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 74  tabase file at t
ea1b0 68 65 20 73 61 6d 65 0a 2a 2a 20 74 69 6d 65 20  he same.** time 
ea1c0 61 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  and one or more 
ea1d0 6f 66 20 74 68 6f 73 65 20 63 6f 6e 6e 65 63 74  of those connect
ea1e0 69 6f 6e 73 20 61 72 65 20 77 72 69 74 69 6e 67  ions are writing
ea1f0 2e 0a 2a 2f 0a 0a 73 74 61 74 69 63 20 69 6e 74  ..*/..static int
ea200 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65   nolockCheckRese
ea210 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33  rvedLock(sqlite3
ea220 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20  _file *NotUsed, 
ea230 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20  int *pResOut){. 
ea240 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
ea250 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 2a 70  R(NotUsed);.  *p
ea260 52 65 73 4f 75 74 20 3d 20 30 3b 0a 20 20 72 65  ResOut = 0;.  re
ea270 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
ea280 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c  }.static int nol
ea290 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  ockLock(sqlite3_
ea2a0 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69  file *NotUsed, i
ea2b0 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20  nt NotUsed2){.  
ea2c0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
ea2d0 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73  2(NotUsed, NotUs
ea2e0 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ed2);.  return S
ea2f0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74  QLITE_OK;.}.stat
ea300 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55 6e 6c  ic int nolockUnl
ea310 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
ea320 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e   *NotUsed, int N
ea330 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53  otUsed2){.  UNUS
ea340 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
ea350 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
ea360 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
ea370 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
ea380 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a  lose the file..*
ea390 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c  /.static int nol
ea3a0 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  ockClose(sqlite3
ea3b0 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 72  _file *id) {.  r
ea3c0 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46  eturn closeUnixF
ea3d0 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 2a 2a  ile(id);.}../***
ea3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea3f0 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 6f 2d 6f   End of the no-o
ea400 70 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74  p lock implement
ea410 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ation **********
ea420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
ea430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a  **********/../**
ea480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
ea4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea4e0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 64 6f 74  ****** Begin dot
ea4f0 2d 66 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a  -file Locking **
ea500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
ea520 2a 2a 20 54 68 65 20 64 6f 74 66 69 6c 65 20 6c  ** The dotfile l
ea530 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74  ocking implement
ea540 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 65  ation uses the e
ea550 78 69 73 74 61 6e 63 65 20 6f 66 20 73 65 70 61  xistance of sepa
ea560 72 61 74 65 20 6c 6f 63 6b 0a 2a 2a 20 66 69 6c  rate lock.** fil
ea570 65 73 20 28 72 65 61 6c 6c 79 20 61 20 64 69 72  es (really a dir
ea580 65 63 74 6f 72 79 29 20 74 6f 20 63 6f 6e 74 72  ectory) to contr
ea590 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  ol access to the
ea5a0 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73   database.  This
ea5b0 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 6a 75 73   works.** on jus
ea5c0 74 20 61 62 6f 75 74 20 65 76 65 72 79 20 66 69  t about every fi
ea5d0 6c 65 73 79 73 74 65 6d 20 69 6d 61 67 69 6e 61  lesystem imagina
ea5e0 62 6c 65 2e 20 20 42 75 74 20 74 68 65 72 65 20  ble.  But there 
ea5f0 61 72 65 20 73 65 72 69 6f 75 73 20 64 6f 77 6e  are serious down
ea600 73 69 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  sides:.**.**    
ea610 28 31 29 20 20 54 68 65 72 65 20 69 73 20 7a 65  (1)  There is ze
ea620 72 6f 20 63 6f 6e 63 75 72 72 65 6e 63 79 2e 20  ro concurrency. 
ea630 20 41 20 73 69 6e 67 6c 65 20 72 65 61 64 65 72   A single reader
ea640 20 62 6c 6f 63 6b 73 20 61 6c 6c 20 6f 74 68 65   blocks all othe
ea650 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e  r.**         con
ea660 6e 65 63 74 69 6f 6e 73 20 66 72 6f 6d 20 72 65  nections from re
ea670 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67  ading or writing
ea680 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a   the database..*
ea690 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 41 6e 20  *.**    (2)  An 
ea6a0 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 72 61 73  application cras
ea6b0 68 20 6f 72 20 70 6f 77 65 72 20 6c 6f 73 73 20  h or power loss 
ea6c0 63 61 6e 20 6c 65 61 76 65 20 73 74 61 6c 65 20  can leave stale 
ea6d0 6c 6f 63 6b 20 66 69 6c 65 73 0a 2a 2a 20 20 20  lock files.**   
ea6e0 20 20 20 20 20 20 73 69 74 74 69 6e 67 20 61 72        sitting ar
ea6f0 6f 75 6e 64 20 74 68 61 74 20 6e 65 65 64 20 74  ound that need t
ea700 6f 20 62 65 20 63 6c 65 61 72 65 64 20 6d 61 6e  o be cleared man
ea710 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 65 76  ually..**.** Nev
ea720 65 72 74 68 65 6c 65 73 73 2c 20 61 20 64 6f 74  ertheless, a dot
ea730 6c 6f 63 6b 20 69 73 20 61 6e 20 61 70 70 72 6f  lock is an appro
ea740 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 6d  priate locking m
ea750 6f 64 65 20 66 6f 72 20 75 73 65 20 69 66 20 6e  ode for use if n
ea760 6f 0a 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69  o.** other locki
ea770 6e 67 20 73 74 72 61 74 65 67 79 20 69 73 20 61  ng strategy is a
ea780 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  vailable..**.** 
ea790 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  Dotfile locking 
ea7a0 77 6f 72 6b 73 20 62 79 20 63 72 65 61 74 69 6e  works by creatin
ea7b0 67 20 61 20 73 75 62 64 69 72 65 63 74 6f 72 79  g a subdirectory
ea7c0 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72   in the same dir
ea7d0 65 63 74 6f 72 79 20 61 73 0a 2a 2a 20 74 68 65  ectory as.** the
ea7e0 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 69   database and wi
ea7f0 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  th the same name
ea800 20 62 75 74 20 77 69 74 68 20 61 20 22 2e 6c 6f   but with a ".lo
ea810 63 6b 22 20 65 78 74 65 6e 73 69 6f 6e 20 61 64  ck" extension ad
ea820 64 65 64 2e 0a 2a 2a 20 54 68 65 20 65 78 69 73  ded..** The exis
ea830 74 61 6e 63 65 20 6f 66 20 61 20 6c 6f 63 6b 20  tance of a lock 
ea840 64 69 72 65 63 74 6f 72 79 20 69 6d 70 6c 69 65  directory implie
ea850 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  s an EXCLUSIVE l
ea860 6f 63 6b 2e 20 20 41 6c 6c 20 6f 74 68 65 72 0a  ock.  All other.
ea870 2a 2a 20 6c 6f 63 6b 20 74 79 70 65 73 20 28 53  ** lock types (S
ea880 48 41 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c  HARED, RESERVED,
ea890 20 50 45 4e 44 49 4e 47 29 20 61 72 65 20 6d 61   PENDING) are ma
ea8a0 70 70 65 64 20 69 6e 74 6f 20 45 58 43 4c 55 53  pped into EXCLUS
ea8b0 49 56 45 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  IVE..*/../*.** T
ea8c0 68 65 20 66 69 6c 65 20 73 75 66 66 69 78 20 61  he file suffix a
ea8d0 64 64 65 64 20 74 6f 20 74 68 65 20 64 61 74 61  dded to the data
ea8e0 20 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 69   base filename i
ea8f0 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61 74  n order to creat
ea900 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 64 69  e the.** lock di
ea910 72 65 63 74 6f 72 79 2e 0a 2a 2f 0a 23 64 65 66  rectory..*/.#def
ea920 69 6e 65 20 44 4f 54 4c 4f 43 4b 5f 53 55 46 46  ine DOTLOCK_SUFF
ea930 49 58 20 22 2e 6c 6f 63 6b 22 0a 0a 2f 2a 0a 2a  IX ".lock"../*.*
ea940 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
ea950 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69  hecks if there i
ea960 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  s a RESERVED loc
ea970 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70  k held on the sp
ea980 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20  ecified.** file 
ea990 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f  by this or any o
ea9a0 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66  ther process. If
ea9b0 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20   such a lock is 
ea9c0 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f  held, set *pResO
ea9d0 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a  ut.** to a non-z
ea9e0 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77  ero value otherw
ea9f0 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20  ise *pResOut is 
eaa00 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68  set to zero.  Th
eaa10 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a  e return value.*
eaa20 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49  * is set to SQLI
eaa30 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20  TE_OK unless an 
eaa40 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73  I/O error occurs
eaa50 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65   during lock che
eaa60 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  cking..**.** In 
eaa70 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c  dotfile locking,
eaa80 20 65 69 74 68 65 72 20 61 20 6c 6f 63 6b 20 65   either a lock e
eaa90 78 69 73 74 73 20 6f 72 20 69 74 20 64 6f 65 73  xists or it does
eaaa0 20 6e 6f 74 2e 20 20 53 6f 20 69 6e 20 74 68 69   not.  So in thi
eaab0 73 0a 2a 2a 20 76 61 72 69 61 74 69 6f 6e 20 6f  s.** variation o
eaac0 66 20 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  f CheckReservedL
eaad0 6f 63 6b 28 29 2c 20 2a 70 52 65 73 4f 75 74 20  ock(), *pResOut 
eaae0 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69  is set to true i
eaaf0 66 20 61 6e 79 20 6c 6f 63 6b 0a 2a 2a 20 69 73  f any lock.** is
eab00 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 66 69 6c   held on the fil
eab10 65 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 74  e and false if t
eab20 68 65 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63  he file is unloc
eab30 6b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ked..*/.static i
eab40 6e 74 20 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52  nt dotlockCheckR
eab50 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69  eservedLock(sqli
eab60 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
eab70 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 20  t *pResOut) {.  
eab80 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
eab90 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76  OK;.  int reserv
eaba0 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69  ed = 0;.  unixFi
eabb0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
eabc0 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69  xFile*)id;..  Si
eabd0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
eabe0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
eabf0 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44  RR_CHECKRESERVED
eac00 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73  LOCK; );.  .  as
eac10 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a  sert( pFile );..
eac20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20    /* Check if a 
eac30 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70  thread in this p
eac40 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63  rocess holds suc
eac50 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66  h a lock */.  if
eac60 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f  ( pFile->eFileLo
eac70 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  ck>SHARED_LOCK )
eac80 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20  {.    /* Either 
eac90 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  this connection 
eaca0 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f  or some other co
eacb0 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 74 68 65 20  nnection in the 
eacc0 73 61 6d 65 20 70 72 6f 63 65 73 73 0a 20 20 20  same process.   
eacd0 20 2a 2a 20 68 6f 6c 64 73 20 61 20 6c 6f 63 6b   ** holds a lock
eace0 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 4e   on the file.  N
eacf0 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20  o need to check 
ead00 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20 20 20  further. */.    
ead10 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20  reserved = 1;.  
ead20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
ead30 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 69  e lock is held i
ead40 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68  f and only if th
ead50 65 20 6c 6f 63 6b 66 69 6c 65 20 65 78 69 73 74  e lockfile exist
ead60 73 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63  s */.    const c
ead70 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d  har *zLockFile =
ead80 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 46   (const char*)pF
ead90 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
eada0 65 78 74 3b 0a 20 20 20 20 72 65 73 65 72 76 65  ext;.    reserve
eadb0 64 20 3d 20 6f 73 41 63 63 65 73 73 28 7a 4c 6f  d = osAccess(zLo
eadc0 63 6b 46 69 6c 65 2c 20 30 29 3d 3d 30 3b 0a 20  ckFile, 0)==0;. 
eadd0 20 7d 0a 20 20 4f 53 54 52 41 43 45 28 28 22 54   }.  OSTRACE(("T
eade0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25  EST WR-LOCK %d %
eadf0 64 20 25 64 20 28 64 6f 74 6c 6f 63 6b 29 5c 6e  d %d (dotlock)\n
eae00 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c  ", pFile->h, rc,
eae10 20 72 65 73 65 72 76 65 64 29 29 3b 0a 20 20 2a   reserved));.  *
eae20 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76  pResOut = reserv
eae30 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ed;.  return rc;
eae40 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
eae50 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65  he file with the
eae60 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20   lock specified 
eae70 62 79 20 70 61 72 61 6d 65 74 65 72 20 65 46 69  by parameter eFi
eae80 6c 65 4c 6f 63 6b 20 2d 20 6f 6e 65 0a 2a 2a 20  leLock - one.** 
eae90 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
eaea0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20  :.**.**     (1) 
eaeb0 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20  SHARED_LOCK.**  
eaec0 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f     (2) RESERVED_
eaed0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20  LOCK.**     (3) 
eaee0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20  PENDING_LOCK.** 
eaef0 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56      (4) EXCLUSIV
eaf00 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d  E_LOCK.**.** Som
eaf10 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75  etimes when requ
eaf20 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20  esting one lock 
eaf30 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61  state, additiona
eaf40 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a  l lock states.**
eaf50 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e   are inserted in
eaf60 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c   between.  The l
eaf70 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69  ocking might fai
eaf80 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20  l on one of the 
eaf90 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74  later.** transit
eafa0 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65  ions leaving the
eafb0 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66   lock state diff
eafc0 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20  erent from what 
eafd0 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a  it started but.*
eafe0 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66  * still short of
eaff0 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20   its goal.  The 
eb000 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20  following chart 
eb010 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65  shows the allowe
eb020 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73  d.** transitions
eb030 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65   and the inserte
eb040 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73  d intermediate s
eb050 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  tates:.**.**    
eb060 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52  UNLOCKED -> SHAR
eb070 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20  ED.**    SHARED 
eb080 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20  -> RESERVED.**  
eb090 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e    SHARED -> (PEN
eb0a0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49  DING) -> EXCLUSI
eb0b0 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45  VE.**    RESERVE
eb0c0 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d  D -> (PENDING) -
eb0d0 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20  > EXCLUSIVE.**  
eb0e0 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43    PENDING -> EXC
eb0f0 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69  LUSIVE.**.** Thi
eb100 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f  s routine will o
eb110 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c  nly increase a l
eb120 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71  ock.  Use the sq
eb130 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a  lite3OsUnlock().
eb140 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f  ** routine to lo
eb150 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65  wer a locking le
eb160 76 65 6c 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20  vel..**.** With 
eb170 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c  dotfile locking,
eb180 20 77 65 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 20   we really only 
eb190 73 75 70 70 6f 72 74 20 73 74 61 74 65 20 28 34  support state (4
eb1a0 29 3a 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2a  ): EXCLUSIVE..**
eb1b0 20 42 75 74 20 77 65 20 74 72 61 63 6b 20 74 68   But we track th
eb1c0 65 20 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67 20  e other locking 
eb1d0 6c 65 76 65 6c 73 20 69 6e 74 65 72 6e 61 6c 6c  levels internall
eb1e0 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  y..*/.static int
eb1f0 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c   dotlockLock(sql
eb200 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
eb210 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 20 7b 0a  nt eFileLock) {.
eb220 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
eb230 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
eb240 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b  d;.  char *zLock
eb250 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 70  File = (char *)p
eb260 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
eb270 74 65 78 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d  text;.  int rc =
eb280 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20 20   SQLITE_OK;...  
eb290 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 61 6e  /* If we have an
eb2a0 79 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65  y lock, then the
eb2b0 20 6c 6f 63 6b 20 66 69 6c 65 20 61 6c 72 65 61   lock file alrea
eb2c0 64 79 20 65 78 69 73 74 73 2e 20 20 41 6c 6c 20  dy exists.  All 
eb2d0 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 74 6f 20  we have.  ** to 
eb2e0 64 6f 20 69 73 20 61 64 6a 75 73 74 20 6f 75 72  do is adjust our
eb2f0 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 64   internal record
eb300 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 6c 65 76   of the lock lev
eb310 65 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  el..  */.  if( p
eb320 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20  File->eFileLock 
eb330 3e 20 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  > NO_LOCK ){.   
eb340 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63   pFile->eFileLoc
eb350 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20  k = eFileLock;. 
eb360 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 75 70 64     /* Always upd
eb370 61 74 65 20 74 68 65 20 74 69 6d 65 73 74 61 6d  ate the timestam
eb380 70 20 6f 6e 20 74 68 65 20 6f 6c 64 20 66 69 6c  p on the old fil
eb390 65 20 2a 2f 0a 23 69 66 64 65 66 20 48 41 56 45  e */.#ifdef HAVE
eb3a0 5f 55 54 49 4d 45 0a 20 20 20 20 75 74 69 6d 65  _UTIME.    utime
eb3b0 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e 55 4c 4c  (zLockFile, NULL
eb3c0 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 75 74 69  );.#else.    uti
eb3d0 6d 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e  mes(zLockFile, N
eb3e0 55 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  ULL);.#endif.   
eb3f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
eb400 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67  K;.  }.  .  /* g
eb410 72 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76 65  rab an exclusive
eb420 20 6c 6f 63 6b 20 2a 2f 0a 20 20 72 63 20 3d 20   lock */.  rc = 
eb430 6f 73 4d 6b 64 69 72 28 7a 4c 6f 63 6b 46 69 6c  osMkdir(zLockFil
eb440 65 2c 20 30 37 37 37 29 3b 0a 20 20 69 66 28 20  e, 0777);.  if( 
eb450 72 63 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 66  rc<0 ){.    /* f
eb460 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 2f 63 72  ailed to open/cr
eb470 65 61 74 65 20 74 68 65 20 6c 6f 63 6b 20 64 69  eate the lock di
eb480 72 65 63 74 6f 72 79 20 2a 2f 0a 20 20 20 20 69  rectory */.    i
eb490 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
eb4a0 6f 3b 0a 20 20 20 20 69 66 28 20 45 45 58 49 53  o;.    if( EEXIS
eb4b0 54 20 3d 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20  T == tErrno ){. 
eb4c0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
eb4d0 5f 42 55 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73  _BUSY;.    } els
eb4e0 65 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  e {.      rc = s
eb4f0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
eb500 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
eb510 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
eb520 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49  CK);.      if( I
eb530 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
eb540 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
eb550 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
eb560 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20  Errno;.      }. 
eb570 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
eb580 72 63 3b 0a 20 20 7d 20 0a 20 20 0a 20 20 2f 2a  rc;.  } .  .  /*
eb590 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65   got it, set the
eb5a0 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e   type and return
eb5b0 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e   ok */.  pFile->
eb5c0 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c  eFileLock = eFil
eb5d0 65 4c 6f 63 6b 3b 0a 20 20 72 65 74 75 72 6e 20  eLock;.  return 
eb5e0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77  rc;.}../*.** Low
eb5f0 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  er the locking l
eb600 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73  evel on file des
eb610 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f  criptor pFile to
eb620 20 65 46 69 6c 65 4c 6f 63 6b 2e 20 20 65 46 69   eFileLock.  eFi
eb630 6c 65 4c 6f 63 6b 0a 2a 2a 20 6d 75 73 74 20 62  leLock.** must b
eb640 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
eb650 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
eb660 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
eb670 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
eb680 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
eb690 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
eb6a0 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
eb6b0 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
eb6c0 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
eb6d0 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
eb6e0 70 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  p..**.** When th
eb6f0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
eb700 72 65 61 63 68 65 73 20 4e 4f 5f 4c 4f 43 4b 2c  reaches NO_LOCK,
eb710 20 64 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b   delete the lock
eb720 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
eb730 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f   int dotlockUnlo
eb740 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
eb750 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f  *id, int eFileLo
eb760 63 6b 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65  ck) {.  unixFile
eb770 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
eb780 69 6c 65 2a 29 69 64 3b 0a 20 20 63 68 61 72 20  ile*)id;.  char 
eb790 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68  *zLockFile = (ch
eb7a0 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ar *)pFile->lock
eb7b0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 69 6e  ingContext;.  in
eb7c0 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
eb7d0 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52   pFile );.  OSTR
eb7e0 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20 20 25 64  ACE(("UNLOCK  %d
eb7f0 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25   %d was %d pid=%
eb800 64 20 28 64 6f 74 6c 6f 63 6b 29 5c 6e 22 2c 20  d (dotlock)\n", 
eb810 70 46 69 6c 65 2d 3e 68 2c 20 65 46 69 6c 65 4c  pFile->h, eFileL
eb820 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ock,.           
eb830 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b  pFile->eFileLock
eb840 2c 20 67 65 74 70 69 64 28 29 29 29 3b 0a 20 20  , getpid()));.  
eb850 61 73 73 65 72 74 28 20 65 46 69 6c 65 4c 6f 63  assert( eFileLoc
eb860 6b 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  k<=SHARED_LOCK )
eb870 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20  ;.  .  /* no-op 
eb880 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20  if possible */. 
eb890 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c   if( pFile->eFil
eb8a0 65 4c 6f 63 6b 3d 3d 65 46 69 6c 65 4c 6f 63 6b  eLock==eFileLock
eb8b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
eb8c0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
eb8d0 20 2f 2a 20 54 6f 20 64 6f 77 6e 67 72 61 64 65   /* To downgrade
eb8e0 20 74 6f 20 73 68 61 72 65 64 2c 20 73 69 6d 70   to shared, simp
eb8f0 6c 79 20 75 70 64 61 74 65 20 6f 75 72 20 69 6e  ly update our in
eb900 74 65 72 6e 61 6c 20 6e 6f 74 69 6f 6e 20 6f 66  ternal notion of
eb910 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20 73   the.  ** lock s
eb920 74 61 74 65 2e 20 20 4e 6f 20 6e 65 65 64 20 74  tate.  No need t
eb930 6f 20 6d 65 73 73 20 77 69 74 68 20 74 68 65 20  o mess with the 
eb940 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 0a 20 20  file on disk..  
eb950 2a 2f 0a 20 20 69 66 28 20 65 46 69 6c 65 4c 6f  */.  if( eFileLo
eb960 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ck==SHARED_LOCK 
eb970 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 46  ){.    pFile->eF
eb980 69 6c 65 4c 6f 63 6b 20 3d 20 53 48 41 52 45 44  ileLock = SHARED
eb990 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75 72  _LOCK;.    retur
eb9a0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
eb9b0 0a 20 20 0a 20 20 2f 2a 20 54 6f 20 66 75 6c 6c  .  .  /* To full
eb9c0 79 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74  y unlock the dat
eb9d0 61 62 61 73 65 2c 20 64 65 6c 65 74 65 20 74 68  abase, delete th
eb9e0 65 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20  e lock file */. 
eb9f0 20 61 73 73 65 72 74 28 20 65 46 69 6c 65 4c 6f   assert( eFileLo
eba00 63 6b 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20  ck==NO_LOCK );. 
eba10 20 72 63 20 3d 20 6f 73 52 6d 64 69 72 28 7a 4c   rc = osRmdir(zL
eba20 6f 63 6b 46 69 6c 65 29 3b 0a 20 20 69 66 28 20  ockFile);.  if( 
eba30 72 63 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d 45  rc<0 && errno==E
eba40 4e 4f 54 44 49 52 20 29 20 72 63 20 3d 20 6f 73  NOTDIR ) rc = os
eba50 55 6e 6c 69 6e 6b 28 7a 4c 6f 63 6b 46 69 6c 65  Unlink(zLockFile
eba60 29 3b 0a 20 20 69 66 28 20 72 63 3c 30 20 29 7b  );.  if( rc<0 ){
eba70 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20  .    int tErrno 
eba80 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20  = errno;.    rc 
eba90 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 45 4e 4f  = 0;.    if( ENO
ebaa0 45 4e 54 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b  ENT != tErrno ){
ebab0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
ebac0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b  TE_IOERR_UNLOCK;
ebad0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 49  .    }.    if( I
ebae0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
ebaf0 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d   ){.      pFile-
ebb00 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
ebb10 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  rno;.    }.    r
ebb20 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20  eturn rc; .  }. 
ebb30 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63   pFile->eFileLoc
ebb40 6b 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72  k = NO_LOCK;.  r
ebb50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
ebb60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
ebb70 61 20 66 69 6c 65 2e 20 20 4d 61 6b 65 20 73 75  a file.  Make su
ebb80 72 65 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20  re the lock has 
ebb90 62 65 65 6e 20 72 65 6c 65 61 73 65 64 20 62 65  been released be
ebba0 66 6f 72 65 20 63 6c 6f 73 69 6e 67 2e 0a 2a 2f  fore closing..*/
ebbb0 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c  .static int dotl
ebbc0 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  ockClose(sqlite3
ebbd0 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69  _file *id) {.  i
ebbe0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
ebbf0 4b 3b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20  K;.  if( id ){. 
ebc00 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69     unixFile *pFi
ebc10 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
ebc20 69 64 3b 0a 20 20 20 20 64 6f 74 6c 6f 63 6b 55  id;.    dotlockU
ebc30 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43  nlock(id, NO_LOC
ebc40 4b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  K);.    sqlite3_
ebc50 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b  free(pFile->lock
ebc60 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20  ingContext);.   
ebc70 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46   rc = closeUnixF
ebc80 69 6c 65 28 69 64 29 3b 0a 20 20 7d 0a 20 20 72  ile(id);.  }.  r
ebc90 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 2f 2a 2a 2a  eturn rc;.}./***
ebca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
ebcb0 45 6e 64 20 6f 66 20 74 68 65 20 64 6f 74 2d 66  End of the dot-f
ebcc0 69 6c 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65  ile lock impleme
ebcd0 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a  ntation ********
ebce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
ebcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebd10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a  **********/../**
ebd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
ebd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebda0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 6c  ******* Begin fl
ebdb0 6f 63 6b 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a  ock Locking ****
ebdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
ebde0 2a 2a 20 55 73 65 20 74 68 65 20 66 6c 6f 63 6b  ** Use the flock
ebdf0 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 74  () system call t
ebe00 6f 20 64 6f 20 66 69 6c 65 20 6c 6f 63 6b 69 6e  o do file lockin
ebe10 67 2e 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29  g..**.** flock()
ebe20 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b 65   locking is like
ebe30 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e   dot-file lockin
ebe40 67 20 69 6e 20 74 68 61 74 20 74 68 65 20 76 61  g in that the va
ebe50 72 69 6f 75 73 0a 2a 2a 20 66 69 6e 65 2d 67 72  rious.** fine-gr
ebe60 61 69 6e 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  ain locking leve
ebe70 6c 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 20  ls supported by 
ebe80 53 51 4c 69 74 65 20 61 72 65 20 63 6f 6c 6c 61  SQLite are colla
ebe90 70 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 20 73  psed into.** a s
ebea0 69 6e 67 6c 65 20 65 78 63 6c 75 73 69 76 65 20  ingle exclusive 
ebeb0 6c 6f 63 6b 2e 20 20 49 6e 20 6f 74 68 65 72 20  lock.  In other 
ebec0 77 6f 72 64 73 2c 20 53 48 41 52 45 44 2c 20 52  words, SHARED, R
ebed0 45 53 45 52 56 45 44 2c 20 61 6e 64 0a 2a 2a 20  ESERVED, and.** 
ebee0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 73 20 61 72  PENDING locks ar
ebef0 65 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67  e the same thing
ebf00 20 61 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45   as an EXCLUSIVE
ebf10 20 6c 6f 63 6b 2e 20 20 53 51 4c 69 74 65 0a 2a   lock.  SQLite.*
ebf20 2a 20 73 74 69 6c 6c 20 77 6f 72 6b 73 20 77 68  * still works wh
ebf30 65 6e 20 79 6f 75 20 64 6f 20 74 68 69 73 2c 20  en you do this, 
ebf40 62 75 74 20 63 6f 6e 63 75 72 72 65 6e 63 79 20  but concurrency 
ebf50 69 73 20 72 65 64 75 63 65 64 20 73 69 6e 63 65  is reduced since
ebf60 0a 2a 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c  .** only a singl
ebf70 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20 62 65  e process can be
ebf80 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74   reading the dat
ebf90 61 62 61 73 65 20 61 74 20 61 20 74 69 6d 65 2e  abase at a time.
ebfa0 0a 2a 2a 0a 2a 2a 20 4f 6d 69 74 20 74 68 69 73  .**.** Omit this
ebfb0 20 73 65 63 74 69 6f 6e 20 69 66 20 53 51 4c 49   section if SQLI
ebfc0 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
ebfd0 47 5f 53 54 59 4c 45 20 69 73 20 74 75 72 6e 65  G_STYLE is turne
ebfe0 64 20 6f 66 66 20 6f 72 20 69 66 0a 2a 2a 20 63  d off or if.** c
ebff0 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 56 58 57  ompiling for VXW
ec000 4f 52 4b 53 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  ORKS..*/.#if SQL
ec010 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
ec020 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 5f  NG_STYLE && !OS_
ec030 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20 52  VXWORKS../*.** R
ec040 65 74 72 79 20 66 6c 6f 63 6b 28 29 20 63 61 6c  etry flock() cal
ec050 6c 73 20 74 68 61 74 20 66 61 69 6c 20 77 69 74  ls that fail wit
ec060 68 20 45 49 4e 54 52 0a 2a 2f 0a 23 69 66 64 65  h EINTR.*/.#ifde
ec070 66 20 45 49 4e 54 52 0a 73 74 61 74 69 63 20 69  f EINTR.static i
ec080 6e 74 20 72 6f 62 75 73 74 5f 66 6c 6f 63 6b 28  nt robust_flock(
ec090 69 6e 74 20 66 64 2c 20 69 6e 74 20 6f 70 29 7b  int fd, int op){
ec0a0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 64 6f 7b  .  int rc;.  do{
ec0b0 20 72 63 20 3d 20 66 6c 6f 63 6b 28 66 64 2c 6f   rc = flock(fd,o
ec0c0 70 29 3b 20 7d 77 68 69 6c 65 28 20 72 63 3c 30  p); }while( rc<0
ec0d0 20 26 26 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52   && errno==EINTR
ec0e0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   );.  return rc;
ec0f0 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  .}.#else.# defin
ec100 65 20 72 6f 62 75 73 74 5f 66 6c 6f 63 6b 28 61  e robust_flock(a
ec110 2c 62 29 20 66 6c 6f 63 6b 28 61 2c 62 29 0a 23  ,b) flock(a,b).#
ec120 65 6e 64 69 66 0a 20 20 20 20 20 0a 0a 2f 2a 0a  endif.     ../*.
ec130 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
ec140 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20  checks if there 
ec150 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  is a RESERVED lo
ec160 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73  ck held on the s
ec170 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65  pecified.** file
ec180 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20   by this or any 
ec190 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49  other process. I
ec1a0 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73  f such a lock is
ec1b0 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73   held, set *pRes
ec1c0 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d  Out.** to a non-
ec1d0 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72  zero value other
ec1e0 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73  wise *pResOut is
ec1f0 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54   set to zero.  T
ec200 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
ec210 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c  ** is set to SQL
ec220 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e  ITE_OK unless an
ec230 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72   I/O error occur
ec240 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68  s during lock ch
ec250 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  ecking..*/.stati
ec260 63 20 69 6e 74 20 66 6c 6f 63 6b 43 68 65 63 6b  c int flockCheck
ec270 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c  ReservedLock(sql
ec280 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
ec290 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20  nt *pResOut){.  
ec2a0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
ec2b0 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76  OK;.  int reserv
ec2c0 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69  ed = 0;.  unixFi
ec2d0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
ec2e0 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20  xFile*)id;.  .  
ec2f0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
ec300 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
ec310 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56  OERR_CHECKRESERV
ec320 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20  EDLOCK; );.  .  
ec330 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
ec340 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69  .  .  /* Check i
ec350 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68  f a thread in th
ec360 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73  is process holds
ec370 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a   such a lock */.
ec380 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69    if( pFile->eFi
ec390 6c 65 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f  leLock>SHARED_LO
ec3a0 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76  CK ){.    reserv
ec3b0 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20  ed = 1;.  }.  . 
ec3c0 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65   /* Otherwise se
ec3d0 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  e if some other 
ec3e0 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74  process holds it
ec3f0 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65  . */.  if( !rese
ec400 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 61  rved ){.    /* a
ec410 74 74 65 6d 70 74 20 74 6f 20 67 65 74 20 74 68  ttempt to get th
ec420 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e  e lock */.    in
ec430 74 20 6c 72 63 20 3d 20 72 6f 62 75 73 74 5f 66  t lrc = robust_f
ec440 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c  lock(pFile->h, L
ec450 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42  OCK_EX | LOCK_NB
ec460 29 3b 0a 20 20 20 20 69 66 28 20 21 6c 72 63 20  );.    if( !lrc 
ec470 29 7b 0a 20 20 20 20 20 20 2f 2a 20 67 6f 74 20  ){.      /* got 
ec480 74 68 65 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b  the lock, unlock
ec490 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63   it */.      lrc
ec4a0 20 3d 20 72 6f 62 75 73 74 5f 66 6c 6f 63 6b 28   = robust_flock(
ec4b0 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55  pFile->h, LOCK_U
ec4c0 4e 29 3b 0a 20 20 20 20 20 20 69 66 20 28 20 6c  N);.      if ( l
ec4d0 72 63 20 29 20 7b 0a 20 20 20 20 20 20 20 20 69  rc ) {.        i
ec4e0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
ec4f0 6f 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 6e  o;.        /* un
ec500 6c 6f 63 6b 20 66 61 69 6c 65 64 20 77 69 74 68  lock failed with
ec510 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20   an error */.   
ec520 20 20 20 20 20 6c 72 63 20 3d 20 53 51 4c 49 54       lrc = SQLIT
ec530 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 20  E_IOERR_UNLOCK; 
ec540 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53 5f  .        if( IS_
ec550 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20  LOCK_ERROR(lrc) 
ec560 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69  ){.          pFi
ec570 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
ec580 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20  tErrno;.        
ec590 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20    rc = lrc;.    
ec5a0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
ec5b0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
ec5c0 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72   int tErrno = er
ec5d0 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 73 65 72  rno;.      reser
ec5e0 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f  ved = 1;.      /
ec5f0 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d  * someone else m
ec600 69 67 68 74 20 68 61 76 65 20 69 74 20 72 65 73  ight have it res
ec610 65 72 76 65 64 20 2a 2f 0a 20 20 20 20 20 20 6c  erved */.      l
ec620 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72  rc = sqliteError
ec630 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74  FromPosixError(t
ec640 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f  Errno, SQLITE_IO
ec650 45 52 52 5f 4c 4f 43 4b 29 3b 20 0a 20 20 20 20  ERR_LOCK); .    
ec660 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
ec670 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20  ROR(lrc) ){.    
ec680 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
ec690 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
ec6a0 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 3b         rc = lrc;
ec6b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
ec6c0 20 7d 0a 20 20 4f 53 54 52 41 43 45 28 28 22 54   }.  OSTRACE(("T
ec6d0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25  EST WR-LOCK %d %
ec6e0 64 20 25 64 20 28 66 6c 6f 63 6b 29 5c 6e 22 2c  d %d (flock)\n",
ec6f0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72   pFile->h, rc, r
ec700 65 73 65 72 76 65 64 29 29 3b 0a 0a 23 69 66 64  eserved));..#ifd
ec710 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  ef SQLITE_IGNORE
ec720 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f  _FLOCK_LOCK_ERRO
ec730 52 53 0a 20 20 69 66 28 20 28 72 63 20 26 20 53  RS.  if( (rc & S
ec740 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20  QLITE_IOERR) == 
ec750 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a  SQLITE_IOERR ){.
ec760 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
ec770 4f 4b 3b 0a 20 20 20 20 72 65 73 65 72 76 65 64  OK;.    reserved
ec780 3d 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f  =1;.  }.#endif /
ec790 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f  * SQLITE_IGNORE_
ec7a0 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52  FLOCK_LOCK_ERROR
ec7b0 53 20 2a 2f 0a 20 20 2a 70 52 65 73 4f 75 74 20  S */.  *pResOut 
ec7c0 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65  = reserved;.  re
ec7d0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
ec7e0 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20  * Lock the file 
ec7f0 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70  with the lock sp
ec800 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d  ecified by param
ec810 65 74 65 72 20 65 46 69 6c 65 4c 6f 63 6b 20 2d  eter eFileLock -
ec820 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66   one.** of the f
ec830 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
ec840 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c      (1) SHARED_L
ec850 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52  OCK.**     (2) R
ec860 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20  ESERVED_LOCK.** 
ec870 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f      (3) PENDING_
ec880 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20  LOCK.**     (4) 
ec890 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a  EXCLUSIVE_LOCK.*
ec8a0 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77  *.** Sometimes w
ec8b0 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f  hen requesting o
ec8c0 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61  ne lock state, a
ec8d0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73  dditional lock s
ec8e0 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73  tates.** are ins
ec8f0 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e  erted in between
ec900 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d  .  The locking m
ec910 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65  ight fail on one
ec920 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a   of the later.**
ec930 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61   transitions lea
ec940 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74  ving the lock st
ec950 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72  ate different fr
ec960 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74  om what it start
ec970 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20  ed but.** still 
ec980 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61  short of its goa
ec990 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  l.  The followin
ec9a0 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68  g chart shows th
ec9b0 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61  e allowed.** tra
ec9c0 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65  nsitions and the
ec9d0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d   inserted interm
ec9e0 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a  ediate states:.*
ec9f0 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44  *.**    UNLOCKED
eca00 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20   -> SHARED.**   
eca10 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52   SHARED -> RESER
eca20 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  VED.**    SHARED
eca30 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
eca40 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
eca50 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45   RESERVED -> (PE
eca60 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
eca70 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e  IVE.**    PENDIN
eca80 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  G -> EXCLUSIVE.*
eca90 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6f 6e 6c  *.** flock() onl
ecaa0 79 20 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74  y really support
ecab0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73   EXCLUSIVE locks
ecac0 2e 20 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65  .  We track inte
ecad0 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b  rmediate.** lock
ecae0 20 73 74 61 74 65 73 20 69 6e 20 74 68 65 20 73   states in the s
ecaf0 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75  qlite3_file stru
ecb00 63 74 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c  cture, but all l
ecb10 6f 63 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a  ocks SHARED or.*
ecb20 2a 20 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c  * above are real
ecb30 6c 79 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  ly EXCLUSIVE loc
ecb40 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61  ks and exclude a
ecb50 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ll other process
ecb60 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73  es from.** acces
ecb70 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  s the file..**.*
ecb80 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
ecb90 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73  ill only increas
ecba0 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74  e a lock.  Use t
ecbb0 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  he sqlite3OsUnlo
ecbc0 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  ck().** routine 
ecbd0 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69  to lower a locki
ecbe0 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61  ng level..*/.sta
ecbf0 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63  tic int flockLoc
ecc00 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
ecc10 69 64 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f 63  id, int eFileLoc
ecc20 6b 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  k) {.  int rc = 
ecc30 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69  SQLITE_OK;.  uni
ecc40 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
ecc50 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20  unixFile*)id;.. 
ecc60 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
ecc70 3b 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c  ;..  /* if we al
ecc80 72 65 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63  ready have a loc
ecc90 6b 2c 20 69 74 20 69 73 20 65 78 63 6c 75 73 69  k, it is exclusi
ecca0 76 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20  ve.  .  ** Just 
eccb0 61 64 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64  adjust level and
eccc0 20 70 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68   punt on outta h
eccd0 65 72 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46  ere. */.  if (pF
ecce0 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3e  ile->eFileLock >
eccf0 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20   NO_LOCK) {.    
ecd00 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b  pFile->eFileLock
ecd10 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20   = eFileLock;.  
ecd20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
ecd30 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  OK;.  }.  .  /* 
ecd40 67 72 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76  grab an exclusiv
ecd50 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 0a 20 20 69  e lock */.  .  i
ecd60 66 20 28 72 6f 62 75 73 74 5f 66 6c 6f 63 6b 28  f (robust_flock(
ecd70 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45  pFile->h, LOCK_E
ecd80 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 29 20 7b 0a  X | LOCK_NB)) {.
ecd90 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d      int tErrno =
ecda0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 2f 2a 20 64   errno;.    /* d
ecdb0 69 64 6e 27 74 20 67 65 74 2c 20 6d 75 73 74 20  idn't get, must 
ecdc0 62 65 20 62 75 73 79 20 2a 2f 0a 20 20 20 20 72  be busy */.    r
ecdd0 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46  c = sqliteErrorF
ecde0 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45  romPosixError(tE
ecdf0 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45  rrno, SQLITE_IOE
ece00 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66  RR_LOCK);.    if
ece10 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
ece20 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69  rc) ){.      pFi
ece30 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
ece40 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20  tErrno;.    }.  
ece50 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20  } else {.    /* 
ece60 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 20  got it, set the 
ece70 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20  type and return 
ece80 6f 6b 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d  ok */.    pFile-
ece90 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69  >eFileLock = eFi
ecea0 6c 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 4f 53  leLock;.  }.  OS
eceb0 54 52 41 43 45 28 28 22 4c 4f 43 4b 20 20 20 20  TRACE(("LOCK    
ecec0 25 64 20 25 73 20 25 73 20 28 66 6c 6f 63 6b 29  %d %s %s (flock)
eced0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 61  \n", pFile->h, a
ecee0 7a 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c  zFileLock(eFileL
ecef0 6f 63 6b 29 2c 20 0a 20 20 20 20 20 20 20 20 20  ock), .         
ecf00 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20    rc==SQLITE_OK 
ecf10 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64  ? "ok" : "failed
ecf20 22 29 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  "));.#ifdef SQLI
ecf30 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f  TE_IGNORE_FLOCK_
ecf40 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66  LOCK_ERRORS.  if
ecf50 28 20 28 72 63 20 26 20 53 51 4c 49 54 45 5f 49  ( (rc & SQLITE_I
ecf60 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f  OERR) == SQLITE_
ecf70 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 72 63 20  IOERR ){.    rc 
ecf80 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
ecf90 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
ecfa0 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b  ITE_IGNORE_FLOCK
ecfb0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a  _LOCK_ERRORS */.
ecfc0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
ecfd0 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65  ./*.** Lower the
ecfe0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
ecff0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
ed000 72 20 70 46 69 6c 65 20 74 6f 20 65 46 69 6c 65  r pFile to eFile
ed010 4c 6f 63 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 6b  Lock.  eFileLock
ed020 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68  .** must be eith
ed030 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48  er NO_LOCK or SH
ed040 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a  ARED_LOCK..**.**
ed050 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   If the locking 
ed060 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c  level of the fil
ed070 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20  e descriptor is 
ed080 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65  already at or be
ed090 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65  low.** the reque
ed0a0 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  sted locking lev
ed0b0 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  el, this routine
ed0c0 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
ed0d0 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b  static int flock
ed0e0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
ed0f0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 65 46 69  ile *id, int eFi
ed100 6c 65 4c 6f 63 6b 29 20 7b 0a 20 20 75 6e 69 78  leLock) {.  unix
ed110 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
ed120 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a  nixFile*)id;.  .
ed130 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
ed140 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 55  );.  OSTRACE(("U
ed150 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73  NLOCK  %d %d was
ed160 20 25 64 20 70 69 64 3d 25 64 20 28 66 6c 6f 63   %d pid=%d (floc
ed170 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  k)\n", pFile->h,
ed180 20 65 46 69 6c 65 4c 6f 63 6b 2c 0a 20 20 20 20   eFileLock,.    
ed190 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 65 46         pFile->eF
ed1a0 69 6c 65 4c 6f 63 6b 2c 20 67 65 74 70 69 64 28  ileLock, getpid(
ed1b0 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65  )));.  assert( e
ed1c0 46 69 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45 44  FileLock<=SHARED
ed1d0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a  _LOCK );.  .  /*
ed1e0 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62   no-op if possib
ed1f0 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  le */.  if( pFil
ed200 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 65 46  e->eFileLock==eF
ed210 69 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 72  ileLock ){.    r
ed220 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
ed230 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61  .  }.  .  /* sha
ed240 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65 20  red can just be 
ed250 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20 61  set because we a
ed260 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65 78  lways have an ex
ed270 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 20  clusive */.  if 
ed280 28 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52  (eFileLock==SHAR
ed290 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70  ED_LOCK) {.    p
ed2a0 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20  File->eFileLock 
ed2b0 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 20  = eFileLock;.   
ed2c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
ed2d0 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e  K;.  }.  .  /* n
ed2e0 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63  o, really, unloc
ed2f0 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 72 6f 62 75  k. */.  if( robu
ed300 73 74 5f 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e  st_flock(pFile->
ed310 68 2c 20 4c 4f 43 4b 5f 55 4e 29 20 29 7b 0a 23  h, LOCK_UN) ){.#
ed320 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e  ifdef SQLITE_IGN
ed330 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45  ORE_FLOCK_LOCK_E
ed340 52 52 4f 52 53 0a 20 20 20 20 72 65 74 75 72 6e  RRORS.    return
ed350 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64   SQLITE_OK;.#end
ed360 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e  if /* SQLITE_IGN
ed370 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45  ORE_FLOCK_LOCK_E
ed380 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 72 65 74  RRORS */.    ret
ed390 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
ed3a0 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 7d 65 6c 73 65  _UNLOCK;.  }else
ed3b0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69  {.    pFile->eFi
ed3c0 6c 65 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c 4f 43 4b  leLock = NO_LOCK
ed3d0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
ed3e0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ITE_OK;.  }.}../
ed3f0 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c  *.** Close a fil
ed400 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
ed410 20 66 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69   flockClose(sqli
ed420 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a  te3_file *id) {.
ed430 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
ed440 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64 20 29  E_OK;.  if( id )
ed450 7b 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63  {.    flockUnloc
ed460 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  k(id, NO_LOCK);.
ed470 20 20 20 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e      rc = closeUn
ed480 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 7d 0a  ixFile(id);.  }.
ed490 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
ed4a0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
ed4b0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
ed4c0 53 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57  STYLE && !OS_VXW
ed4d0 4f 52 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ORK */../*******
ed4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
ed4f0 20 6f 66 20 74 68 65 20 66 6c 6f 63 6b 20 6c 6f   of the flock lo
ed500 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ck implementatio
ed510 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n **************
ed520 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
ed530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed570 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
ed580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed5c0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ********.*******
ed5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed5e0 2a 20 42 65 67 69 6e 20 4e 61 6d 65 64 20 53 65  * Begin Named Se
ed5f0 6d 61 70 68 6f 72 65 20 4c 6f 63 6b 69 6e 67 20  maphore Locking 
ed600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed610 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e  ********.**.** N
ed620 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c  amed semaphore l
ed630 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 73  ocking is only s
ed640 75 70 70 6f 72 74 65 64 20 6f 6e 20 56 78 57 6f  upported on VxWo
ed650 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70  rks..**.** Semap
ed660 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20  hore locking is 
ed670 6c 69 6b 65 20 64 6f 74 2d 6c 6f 63 6b 20 61 6e  like dot-lock an
ed680 64 20 66 6c 6f 63 6b 20 69 6e 20 74 68 61 74 20  d flock in that 
ed690 69 74 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 0a 2a  it really only.*
ed6a0 2a 20 73 75 70 70 6f 72 74 73 20 45 58 43 4c 55  * supports EXCLU
ed6b0 53 49 56 45 20 6c 6f 63 6b 69 6e 67 2e 20 20 4f  SIVE locking.  O
ed6c0 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f  nly a single pro
ed6d0 63 65 73 73 20 63 61 6e 20 72 65 61 64 20 6f 72  cess can read or
ed6e0 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 64 61   write.** the da
ed6f0 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 61  tabase file at a
ed700 20 74 69 6d 65 2e 20 20 54 68 69 73 20 72 65 64   time.  This red
ed710 75 63 65 73 20 70 6f 74 65 6e 74 69 61 6c 20 63  uces potential c
ed720 6f 6e 63 75 72 72 65 6e 63 79 2c 20 62 75 74 0a  oncurrency, but.
ed730 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20 6c 6f 63  ** makes the loc
ed740 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  k implementation
ed750 20 6d 75 63 68 20 65 61 73 69 65 72 2e 0a 2a 2f   much easier..*/
ed760 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  .#if OS_VXWORKS.
ed770 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
ed780 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68  ine checks if th
ed790 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45  ere is a RESERVE
ed7a0 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74  D lock held on t
ed7b0 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  he specified.** 
ed7c0 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20  file by this or 
ed7d0 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73  any other proces
ed7e0 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63  s. If such a loc
ed7f0 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a  k is held, set *
ed800 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20  pResOut.** to a 
ed810 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f  non-zero value o
ed820 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75  therwise *pResOu
ed830 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f  t is set to zero
ed840 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
ed850 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  lue.** is set to
ed860 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73   SQLITE_OK unles
ed870 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f  s an I/O error o
ed880 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63  ccurs during loc
ed890 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73  k checking..*/.s
ed8a0 74 61 74 69 63 20 69 6e 74 20 73 65 6d 43 68 65  tatic int semChe
ed8b0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
ed8c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
ed8d0 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b   int *pResOut) {
ed8e0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
ed8f0 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73  TE_OK;.  int res
ed900 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69  erved = 0;.  uni
ed910 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
ed920 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20  unixFile*)id;.. 
ed930 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
ed940 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
ed950 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52  IOERR_CHECKRESER
ed960 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20  VEDLOCK; );.  . 
ed970 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
ed980 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  ;..  /* Check if
ed990 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69   a thread in thi
ed9a0 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  s process holds 
ed9b0 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20  such a lock */. 
ed9c0 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c   if( pFile->eFil
ed9d0 65 4c 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43  eLock>SHARED_LOC
ed9e0 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65  K ){.    reserve
ed9f0 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20  d = 1;.  }.  .  
eda00 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 65  /* Otherwise see
eda10 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70   if some other p
eda20 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e  rocess holds it.
eda30 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72   */.  if( !reser
eda40 76 65 64 20 29 7b 0a 20 20 20 20 73 65 6d 5f 74  ved ){.    sem_t
eda50 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e   *pSem = pFile->
eda60 70 49 6e 6f 64 65 2d 3e 70 53 65 6d 3b 0a 20 20  pInode->pSem;.  
eda70 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73 74    struct stat st
eda80 61 74 42 75 66 3b 0a 0a 20 20 20 20 69 66 28 20  atBuf;..    if( 
eda90 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53 65 6d  sem_trywait(pSem
edaa0 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 69  )==-1 ){.      i
edab0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
edac0 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 45 41 47  o;.      if( EAG
edad0 41 49 4e 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b  AIN != tErrno ){
edae0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
edaf0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
edb00 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
edb10 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
edb20 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 3b  CKRESERVEDLOCK);
edb30 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
edb40 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
edb50 6e 6f 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65  no;.      } else
edb60 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 73 6f   {.        /* so
edb70 6d 65 6f 6e 65 20 65 6c 73 65 20 68 61 73 20 74  meone else has t
edb80 68 65 20 6c 6f 63 6b 20 77 68 65 6e 20 77 65 20  he lock when we 
edb90 61 72 65 20 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a  are in NO_LOCK *
edba0 2f 0a 20 20 20 20 20 20 20 20 72 65 73 65 72 76  /.        reserv
edbb0 65 64 20 3d 20 28 70 46 69 6c 65 2d 3e 65 46 69  ed = (pFile->eFi
edbc0 6c 65 4c 6f 63 6b 20 3c 20 53 48 41 52 45 44 5f  leLock < SHARED_
edbd0 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20  LOCK);.      }. 
edbe0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
edbf0 2f 2a 20 77 65 20 63 6f 75 6c 64 20 68 61 76 65  /* we could have
edc00 20 69 74 20 69 66 20 77 65 20 77 61 6e 74 20 69   it if we want i
edc10 74 20 2a 2f 0a 20 20 20 20 20 20 73 65 6d 5f 70  t */.      sem_p
edc20 6f 73 74 28 70 53 65 6d 29 3b 0a 20 20 20 20 7d  ost(pSem);.    }
edc30 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 28 28  .  }.  OSTRACE((
edc40 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
edc50 20 25 64 20 25 64 20 28 73 65 6d 29 5c 6e 22 2c   %d %d (sem)\n",
edc60 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72   pFile->h, rc, r
edc70 65 73 65 72 76 65 64 29 29 3b 0a 0a 20 20 2a 70  eserved));..  *p
edc80 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65  ResOut = reserve
edc90 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  d;.  return rc;.
edca0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
edcb0 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
edcc0 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
edcd0 79 20 70 61 72 61 6d 65 74 65 72 20 65 46 69 6c  y parameter eFil
edce0 65 4c 6f 63 6b 20 2d 20 6f 6e 65 0a 2a 2a 20 6f  eLock - one.** o
edcf0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  f the following:
edd00 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53  .**.**     (1) S
edd10 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  HARED_LOCK.**   
edd20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
edd30 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50  OCK.**     (3) P
edd40 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20  ENDING_LOCK.**  
edd50 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
edd60 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  _LOCK.**.** Some
edd70 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65  times when reque
edd80 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73  sting one lock s
edd90 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c  tate, additional
edda0 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20   lock states.** 
eddb0 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
eddc0 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
eddd0 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
edde0 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
eddf0 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69  ater.** transiti
ede00 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20  ons leaving the 
ede10 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65  lock state diffe
ede20 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69  rent from what i
ede30 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a  t started but.**
ede40 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20   still short of 
ede50 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66  its goal.  The f
ede60 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73  ollowing chart s
ede70 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64  hows the allowed
ede80 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
ede90 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64  and the inserted
edea0 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74   intermediate st
edeb0 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55  ates:.**.**    U
edec0 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
eded0 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
edee0 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20  > RESERVED.**   
edef0 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44   SHARED -> (PEND
edf00 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
edf10 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44  E.**    RESERVED
edf20 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
edf30 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
edf40 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
edf50 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 53 65 6d 61  USIVE.**.** Sema
edf60 70 68 6f 72 65 20 6c 6f 63 6b 73 20 6f 6e 6c 79  phore locks only
edf70 20 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 20   really support 
edf80 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e  EXCLUSIVE locks.
edf90 20 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65 72    We track inter
edfa0 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20  mediate.** lock 
edfb0 73 74 61 74 65 73 20 69 6e 20 74 68 65 20 73 71  states in the sq
edfc0 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63  lite3_file struc
edfd0 74 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f  ture, but all lo
edfe0 63 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a  cks SHARED or.**
edff0 20 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c 6c   above are reall
ee000 79 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  y EXCLUSIVE lock
ee010 73 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61 6c  s and exclude al
ee020 6c 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65  l other processe
ee030 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73  s from.** access
ee040 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a   the file..**.**
ee050 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   This routine wi
ee060 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65  ll only increase
ee070 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68   a lock.  Use th
ee080 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63  e sqlite3OsUnloc
ee090 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74  k().** routine t
ee0a0 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e  o lower a lockin
ee0b0 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74  g level..*/.stat
ee0c0 69 63 20 69 6e 74 20 73 65 6d 4c 6f 63 6b 28 73  ic int semLock(s
ee0d0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
ee0e0 20 69 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 20   int eFileLock) 
ee0f0 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  {.  unixFile *pF
ee100 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
ee110 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a 20  )id;.  int fd;. 
ee120 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70   sem_t *pSem = p
ee130 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53  File->pInode->pS
ee140 65 6d 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  em;.  int rc = S
ee150 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20  QLITE_OK;..  /* 
ee160 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 68 61  if we already ha
ee170 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73  ve a lock, it is
ee180 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a 20 20   exclusive.  .  
ee190 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 20 6c  ** Just adjust l
ee1a0 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e  evel and punt on
ee1b0 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a   outta here. */.
ee1c0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 65 46 69    if (pFile->eFi
ee1d0 6c 65 4c 6f 63 6b 20 3e 20 4e 4f 5f 4c 4f 43 4b  leLock > NO_LOCK
ee1e0 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65  ) {.    pFile->e
ee1f0 46 69 6c 65 4c 6f 63 6b 20 3d 20 65 46 69 6c 65  FileLock = eFile
ee200 4c 6f 63 6b 3b 0a 20 20 20 20 72 63 20 3d 20 53  Lock;.    rc = S
ee210 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f  QLITE_OK;.    go
ee220 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b  to sem_end_lock;
ee230 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c 6f 63  .  }.  .  /* loc
ee240 6b 20 73 65 6d 61 70 68 6f 72 65 20 6e 6f 77 20  k semaphore now 
ee250 62 75 74 20 62 61 69 6c 20 6f 75 74 20 77 68 65  but bail out whe
ee260 6e 20 61 6c 72 65 61 64 79 20 6c 6f 63 6b 65 64  n already locked
ee270 2e 20 2a 2f 0a 20 20 69 66 28 20 73 65 6d 5f 74  . */.  if( sem_t
ee280 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31  rywait(pSem)==-1
ee290 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
ee2a0 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f  ITE_BUSY;.    go
ee2b0 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b  to sem_end_lock;
ee2c0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69  .  }..  /* got i
ee2d0 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20  t, set the type 
ee2e0 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f  and return ok */
ee2f0 0a 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c  .  pFile->eFileL
ee300 6f 63 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b  ock = eFileLock;
ee310 0a 0a 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a  .. sem_end_lock:
ee320 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
ee330 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65  ./*.** Lower the
ee340 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
ee350 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
ee360 72 20 70 46 69 6c 65 20 74 6f 20 65 46 69 6c 65  r pFile to eFile
ee370 4c 6f 63 6b 2e 20 20 65 46 69 6c 65 4c 6f 63 6b  Lock.  eFileLock
ee380 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68  .** must be eith
ee390 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48  er NO_LOCK or SH
ee3a0 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a  ARED_LOCK..**.**
ee3b0 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   If the locking 
ee3c0 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c  level of the fil
ee3d0 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20  e descriptor is 
ee3e0 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65  already at or be
ee3f0 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65  low.** the reque
ee400 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  sted locking lev
ee410 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  el, this routine
ee420 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
ee430 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 55 6e  static int semUn
ee440 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
ee450 65 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c 65  e *id, int eFile
ee460 4c 6f 63 6b 29 20 7b 0a 20 20 75 6e 69 78 46 69  Lock) {.  unixFi
ee470 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
ee480 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 73 65 6d  xFile*)id;.  sem
ee490 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65  _t *pSem = pFile
ee4a0 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53 65 6d 3b 0a  ->pInode->pSem;.
ee4b0 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
ee4c0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53   );.  assert( pS
ee4d0 65 6d 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 28  em );.  OSTRACE(
ee4e0 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20  ("UNLOCK  %d %d 
ee4f0 77 61 73 20 25 64 20 70 69 64 3d 25 64 20 28 73  was %d pid=%d (s
ee500 65 6d 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  em)\n", pFile->h
ee510 2c 20 65 46 69 6c 65 4c 6f 63 6b 2c 0a 20 20 20  , eFileLock,.   
ee520 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 65          pFile->e
ee530 46 69 6c 65 4c 6f 63 6b 2c 20 67 65 74 70 69 64  FileLock, getpid
ee540 28 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ()));.  assert( 
ee550 65 46 69 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45  eFileLock<=SHARE
ee560 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f  D_LOCK );.  .  /
ee570 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69  * no-op if possi
ee580 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69  ble */.  if( pFi
ee590 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 65  le->eFileLock==e
ee5a0 46 69 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20  FileLock ){.    
ee5b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
ee5c0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68  ;.  }.  .  /* sh
ee5d0 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65  ared can just be
ee5e0 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20   set because we 
ee5f0 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65  always have an e
ee600 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66  xclusive */.  if
ee610 20 28 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41   (eFileLock==SHA
ee620 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20  RED_LOCK) {.    
ee630 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b  pFile->eFileLock
ee640 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20   = eFileLock;.  
ee650 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
ee660 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  OK;.  }.  .  /* 
ee670 6e 6f 2c 20 72 65 61 6c 6c 79 20 75 6e 6c 6f 63  no, really unloc
ee680 6b 2e 20 2a 2f 0a 20 20 69 66 20 28 20 73 65 6d  k. */.  if ( sem
ee690 5f 70 6f 73 74 28 70 53 65 6d 29 3d 3d 2d 31 20  _post(pSem)==-1 
ee6a0 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 63 2c 20  ) {.    int rc, 
ee6b0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
ee6c0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
ee6d0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
ee6e0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
ee6f0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b  E_IOERR_UNLOCK);
ee700 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b  .    if( IS_LOCK
ee710 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20  _ERROR(rc) ){.  
ee720 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
ee730 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
ee740 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
ee750 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65  rc; .  }.  pFile
ee760 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 4e 4f  ->eFileLock = NO
ee770 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20  _LOCK;.  return 
ee780 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
ee790 0a 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c  . ** Close a fil
ee7a0 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  e.. */.static in
ee7b0 74 20 73 65 6d 43 6c 6f 73 65 28 73 71 6c 69 74  t semClose(sqlit
ee7c0 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20  e3_file *id) {. 
ee7d0 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75   if( id ){.    u
ee7e0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
ee7f0 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
ee800 20 20 20 20 73 65 6d 55 6e 6c 6f 63 6b 28 69 64      semUnlock(id
ee810 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  , NO_LOCK);.    
ee820 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
ee830 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  .    unixEnterMu
ee840 74 65 78 28 29 3b 0a 20 20 20 20 72 65 6c 65 61  tex();.    relea
ee850 73 65 49 6e 6f 64 65 49 6e 66 6f 28 70 46 69 6c  seInodeInfo(pFil
ee860 65 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76  e);.    unixLeav
ee870 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 63 6c  eMutex();.    cl
ee880 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b  oseUnixFile(id);
ee890 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
ee8a0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64  LITE_OK;.}..#end
ee8b0 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53  if /* OS_VXWORKS
ee8c0 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 64 20   */./*.** Named 
ee8d0 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e  semaphore lockin
ee8e0 67 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  g is only availa
ee8f0 62 6c 65 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a  ble on VxWorks..
ee900 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
ee910 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 61  ** End of the na
ee920 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f  med semaphore lo
ee930 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ck implementatio
ee940 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n **************
ee950 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
ee960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee9a0 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */.../**********
ee9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ee9f0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
eea00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eea10 20 42 65 67 69 6e 20 41 46 50 20 4c 6f 63 6b 69   Begin AFP Locki
eea20 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ng *************
eea30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eea40 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 46 50 20 69  ****.**.** AFP i
eea50 73 20 74 68 65 20 41 70 70 6c 65 20 46 69 6c 69  s the Apple Fili
eea60 6e 67 20 50 72 6f 74 6f 63 6f 6c 2e 20 20 41 46  ng Protocol.  AF
eea70 50 20 69 73 20 61 20 6e 65 74 77 6f 72 6b 20 66  P is a network f
eea80 69 6c 65 73 79 73 74 65 6d 20 66 6f 75 6e 64 0a  ilesystem found.
eea90 2a 2a 20 6f 6e 20 41 70 70 6c 65 20 4d 61 63 69  ** on Apple Maci
eeaa0 6e 74 6f 73 68 20 63 6f 6d 70 75 74 65 72 73 20  ntosh computers 
eeab0 2d 20 62 6f 74 68 20 4f 53 39 20 61 6e 64 20 4f  - both OS9 and O
eeac0 53 58 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 72 64 2d  SX..**.** Third-
eead0 70 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61  party implementa
eeae0 74 69 6f 6e 73 20 6f 66 20 41 46 50 20 61 72 65  tions of AFP are
eeaf0 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 42 75 74   available.  But
eeb00 20 74 68 69 73 20 63 6f 64 65 20 68 65 72 65 0a   this code here.
eeb10 2a 2a 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e  ** only works on
eeb20 20 4f 53 58 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65   OSX..*/..#if de
eeb30 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
eeb40 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
eeb50 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
eeb60 2f 2a 0a 2a 2a 20 54 68 65 20 61 66 70 4c 6f 63  /*.** The afpLoc
eeb70 6b 69 6e 67 43 6f 6e 74 65 78 74 20 73 74 72 75  kingContext stru
eeb80 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61  cture contains a
eeb90 6c 6c 20 61 66 70 20 6c 6f 63 6b 20 73 70 65 63  ll afp lock spec
eeba0 69 66 69 63 20 73 74 61 74 65 0a 2a 2f 0a 74 79  ific state.*/.ty
eebb0 70 65 64 65 66 20 73 74 72 75 63 74 20 61 66 70  pedef struct afp
eebc0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61  LockingContext a
eebd0 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
eebe0 3b 0a 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b  ;.struct afpLock
eebf0 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20 69  ingContext {.  i
eec00 6e 74 20 72 65 73 65 72 76 65 64 3b 0a 20 20 63  nt reserved;.  c
eec10 6f 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61 74  onst char *dbPat
eec20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  h;             /
eec30 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70  * Name of the op
eec40 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73  en file */.};..s
eec50 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c  truct ByteRangeL
eec60 6f 63 6b 50 42 32 0a 7b 0a 20 20 75 6e 73 69 67  ockPB2.{.  unsig
eec70 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66  ned long long of
eec80 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  fset;        /* 
eec90 6f 66 66 73 65 74 20 74 6f 20 66 69 72 73 74 20  offset to first 
eeca0 62 79 74 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a  byte to lock */.
eecb0 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20    unsigned long 
eecc0 6c 6f 6e 67 20 6c 65 6e 67 74 68 3b 20 20 20 20  long length;    
eecd0 20 20 20 20 2f 2a 20 6e 62 72 20 6f 66 20 62 79      /* nbr of by
eece0 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20  tes to lock */. 
eecf0 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c   unsigned long l
eed00 6f 6e 67 20 72 65 74 52 61 6e 67 65 53 74 61 72  ong retRangeStar
eed10 74 3b 20 2f 2a 20 6e 62 72 20 6f 66 20 31 73 74  t; /* nbr of 1st
eed20 20 62 79 74 65 20 6c 6f 63 6b 65 64 20 69 66 20   byte locked if 
eed30 73 75 63 63 65 73 73 66 75 6c 20 2a 2f 0a 20 20  successful */.  
eed40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75 6e  unsigned char un
eed50 4c 6f 63 6b 46 6c 61 67 3b 20 20 20 20 20 20 20  LockFlag;       
eed60 20 20 2f 2a 20 31 20 3d 20 75 6e 6c 6f 63 6b 2c    /* 1 = unlock,
eed70 20 30 20 3d 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75   0 = lock */.  u
eed80 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 74 61  nsigned char sta
eed90 72 74 45 6e 64 46 6c 61 67 3b 20 20 20 20 20 20  rtEndFlag;      
eeda0 20 2f 2a 20 31 3d 72 65 6c 20 74 6f 20 65 6e 64   /* 1=rel to end
eedb0 20 6f 66 20 66 6f 72 6b 2c 20 30 3d 72 65 6c 20   of fork, 0=rel 
eedc0 74 6f 20 73 74 61 72 74 20 2a 2f 0a 20 20 69 6e  to start */.  in
eedd0 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 20  t fd;           
eede0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eedf0 2f 2a 20 66 69 6c 65 20 64 65 73 63 20 74 6f 20  /* file desc to 
eee00 61 73 73 6f 63 20 74 68 69 73 20 6c 6f 63 6b 20  assoc this lock 
eee10 77 69 74 68 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66  with */.};..#def
eee20 69 6e 65 20 61 66 70 66 73 42 79 74 65 52 61 6e  ine afpfsByteRan
eee30 67 65 4c 6f 63 6b 32 46 53 43 54 4c 20 20 20 20  geLock2FSCTL    
eee40 20 20 20 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32      _IOWR('z', 2
eee50 33 2c 20 73 74 72 75 63 74 20 42 79 74 65 52 61  3, struct ByteRa
eee60 6e 67 65 4c 6f 63 6b 50 42 32 29 0a 0a 2f 2a 0a  ngeLockPB2)../*.
eee70 2a 2a 20 54 68 69 73 20 69 73 20 61 20 75 74 69  ** This is a uti
eee80 6c 69 74 79 20 66 6f 72 20 73 65 74 74 69 6e 67  lity for setting
eee90 20 6f 72 20 63 6c 65 61 72 69 6e 67 20 61 20 62   or clearing a b
eeea0 69 74 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 6f 6e  it-range lock on
eeeb0 20 61 6e 0a 2a 2a 20 41 46 50 20 66 69 6c 65 73   an.** AFP files
eeec0 79 73 74 65 6d 2e 0a 2a 2a 20 0a 2a 2a 20 52 65  ystem..** .** Re
eeed0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
eeee0 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54  n success, SQLIT
eeef0 45 5f 42 55 53 59 20 6f 6e 20 66 61 69 6c 75 72  E_BUSY on failur
eef00 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
eef10 20 61 66 70 53 65 74 4c 6f 63 6b 28 0a 20 20 63   afpSetLock(.  c
eef20 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c  onst char *path,
eef30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
eef40 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c   Name of the fil
eef50 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 6f  e to be locked o
eef60 72 20 75 6e 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20  r unlocked */.  
eef70 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c  unixFile *pFile,
eef80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
eef90 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63  * Open file desc
eefa0 72 69 70 74 6f 72 20 6f 6e 20 70 61 74 68 20 2a  riptor on path *
eefb0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  /.  unsigned lon
eefc0 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 2c 20 20  g long offset,  
eefd0 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65     /* First byte
eefe0 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f   to be locked */
eeff0 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  .  unsigned long
ef000 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 2c 20 20 20   long length,   
ef010 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
ef020 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a  ytes to lock */.
ef030 20 20 69 6e 74 20 73 65 74 4c 6f 63 6b 46 6c 61    int setLockFla
ef040 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g               
ef050 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 65 74 20   /* True to set 
ef060 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20 74 6f 20  lock.  False to 
ef070 63 6c 65 61 72 20 6c 6f 63 6b 20 2a 2f 0a 29 7b  clear lock */.){
ef080 0a 20 20 73 74 72 75 63 74 20 42 79 74 65 52 61  .  struct ByteRa
ef090 6e 67 65 4c 6f 63 6b 50 42 32 20 70 62 3b 0a 20  ngeLockPB2 pb;. 
ef0a0 20 69 6e 74 20 65 72 72 3b 0a 20 20 0a 20 20 70   int err;.  .  p
ef0b0 62 2e 75 6e 4c 6f 63 6b 46 6c 61 67 20 3d 20 73  b.unLockFlag = s
ef0c0 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 30 20 3a  etLockFlag ? 0 :
ef0d0 20 31 3b 0a 20 20 70 62 2e 73 74 61 72 74 45 6e   1;.  pb.startEn
ef0e0 64 46 6c 61 67 20 3d 20 30 3b 0a 20 20 70 62 2e  dFlag = 0;.  pb.
ef0f0 6f 66 66 73 65 74 20 3d 20 6f 66 66 73 65 74 3b  offset = offset;
ef100 0a 20 20 70 62 2e 6c 65 6e 67 74 68 20 3d 20 6c  .  pb.length = l
ef110 65 6e 67 74 68 3b 20 0a 20 20 70 62 2e 66 64 20  ength; .  pb.fd 
ef120 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 0a 20  = pFile->h;.  . 
ef130 20 4f 53 54 52 41 43 45 28 28 22 41 46 50 53 45   OSTRACE(("AFPSE
ef140 54 4c 4f 43 4b 20 5b 25 73 5d 20 66 6f 72 20 25  TLOCK [%s] for %
ef150 64 25 73 20 69 6e 20 72 61 6e 67 65 20 25 6c 6c  d%s in range %ll
ef160 78 3a 25 6c 6c 78 5c 6e 22 2c 20 0a 20 20 20 20  x:%llx\n", .    
ef170 28 73 65 74 4c 6f 63 6b 46 6c 61 67 3f 22 4f 4e  (setLockFlag?"ON
ef180 22 3a 22 4f 46 46 22 29 2c 20 70 46 69 6c 65 2d  ":"OFF"), pFile-
ef190 3e 68 2c 20 28 70 62 2e 66 64 3d 3d 2d 31 3f 22  >h, (pb.fd==-1?"
ef1a0 5b 74 65 73 74 76 61 6c 2d 31 5d 22 3a 22 22 29  [testval-1]":"")
ef1b0 2c 0a 20 20 20 20 6f 66 66 73 65 74 2c 20 6c 65  ,.    offset, le
ef1c0 6e 67 74 68 29 29 3b 0a 20 20 65 72 72 20 3d 20  ngth));.  err = 
ef1d0 66 73 63 74 6c 28 70 61 74 68 2c 20 61 66 70 66  fsctl(path, afpf
ef1e0 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46  sByteRangeLock2F
ef1f0 53 43 54 4c 2c 20 26 70 62 2c 20 30 29 3b 0a 20  SCTL, &pb, 0);. 
ef200 20 69 66 20 28 20 65 72 72 3d 3d 2d 31 20 29 20   if ( err==-1 ) 
ef210 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  {.    int rc;.  
ef220 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65    int tErrno = e
ef230 72 72 6e 6f 3b 0a 20 20 20 20 4f 53 54 52 41 43  rrno;.    OSTRAC
ef240 45 28 28 22 41 46 50 53 45 54 4c 4f 43 4b 20 66  E(("AFPSETLOCK f
ef250 61 69 6c 65 64 20 74 6f 20 66 73 63 74 6c 28 29  ailed to fsctl()
ef260 20 27 25 73 27 20 25 64 20 25 73 5c 6e 22 2c 0a   '%s' %d %s\n",.
ef270 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74               pat
ef280 68 2c 20 74 45 72 72 6e 6f 2c 20 73 74 72 65 72  h, tErrno, strer
ef290 72 6f 72 28 74 45 72 72 6e 6f 29 29 29 3b 0a 23  ror(tErrno)));.#
ef2a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e  ifdef SQLITE_IGN
ef2b0 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52  ORE_AFP_LOCK_ERR
ef2c0 4f 52 53 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ORS.    rc = SQL
ef2d0 49 54 45 5f 42 55 53 59 3b 0a 23 65 6c 73 65 0a  ITE_BUSY;.#else.
ef2e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
ef2f0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
ef300 6f 72 28 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20  or(tErrno,.     
ef310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
ef320 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 53 51 4c  etLockFlag ? SQL
ef330 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 3a  ITE_IOERR_LOCK :
ef340 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e   SQLITE_IOERR_UN
ef350 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69 66 20 2f 2a  LOCK);.#endif /*
ef360 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41   SQLITE_IGNORE_A
ef370 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a  FP_LOCK_ERRORS *
ef380 2f 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43  /.    if( IS_LOC
ef390 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20  K_ERROR(rc) ){. 
ef3a0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
ef3b0 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a  Errno = tErrno;.
ef3c0 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
ef3d0 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a   rc;.  } else {.
ef3e0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ef3f0 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  E_OK;.  }.}../*.
ef400 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
ef410 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20  checks if there 
ef420 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  is a RESERVED lo
ef430 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73  ck held on the s
ef440 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65  pecified.** file
ef450 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20   by this or any 
ef460 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49  other process. I
ef470 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73  f such a lock is
ef480 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73   held, set *pRes
ef490 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d  Out.** to a non-
ef4a0 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72  zero value other
ef4b0 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73  wise *pResOut is
ef4c0 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54   set to zero.  T
ef4d0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
ef4e0 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c  ** is set to SQL
ef4f0 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e  ITE_OK unless an
ef500 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72   I/O error occur
ef510 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68  s during lock ch
ef520 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  ecking..*/.stati
ef530 63 20 69 6e 74 20 61 66 70 43 68 65 63 6b 52 65  c int afpCheckRe
ef540 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74  servedLock(sqlit
ef550 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
ef560 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e   *pResOut){.  in
ef570 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
ef580 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64  ;.  int reserved
ef590 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65   = 0;.  unixFile
ef5a0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
ef5b0 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f  ile*)id;.  afpLo
ef5c0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f  ckingContext *co
ef5d0 6e 74 65 78 74 3b 0a 20 20 0a 20 20 53 69 6d 75  ntext;.  .  Simu
ef5e0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
ef5f0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
ef600 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f  _CHECKRESERVEDLO
ef610 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65  CK; );.  .  asse
ef620 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 63  rt( pFile );.  c
ef630 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63  ontext = (afpLoc
ef640 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70  kingContext *) p
ef650 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
ef660 74 65 78 74 3b 0a 20 20 69 66 28 20 63 6f 6e 74  text;.  if( cont
ef670 65 78 74 2d 3e 72 65 73 65 72 76 65 64 20 29 7b  ext->reserved ){
ef680 0a 20 20 20 20 2a 70 52 65 73 4f 75 74 20 3d 20  .    *pResOut = 
ef690 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  1;.    return SQ
ef6a0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 75  LITE_OK;.  }.  u
ef6b0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
ef6c0 20 2f 2a 20 42 65 63 61 75 73 65 20 70 46 69 6c   /* Because pFil
ef6d0 65 2d 3e 70 49 6e 6f 64 65 20 69 73 20 73 68 61  e->pInode is sha
ef6e0 72 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 61  red across threa
ef6f0 64 73 20 2a 2f 0a 20 20 0a 20 20 2f 2a 20 43 68  ds */.  .  /* Ch
ef700 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20  eck if a thread 
ef710 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  in this process 
ef720 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63  holds such a loc
ef730 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65  k */.  if( pFile
ef740 2d 3e 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c  ->pInode->eFileL
ef750 6f 63 6b 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ock>SHARED_LOCK 
ef760 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20  ){.    reserved 
ef770 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a  = 1;.  }.  .  /*
ef780 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69   Otherwise see i
ef790 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f  f some other pro
ef7a0 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20  cess holds it.. 
ef7b0 20 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65    */.  if( !rese
ef7c0 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 6c  rved ){.    /* l
ef7d0 6f 63 6b 20 74 68 65 20 52 45 53 45 52 56 45 44  ock the RESERVED
ef7e0 20 62 79 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74   byte */.    int
ef7f0 20 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63   lrc = afpSetLoc
ef800 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
ef810 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56  h, pFile, RESERV
ef820 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 20 20  ED_BYTE, 1,1);  
ef830 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
ef840 4f 4b 3d 3d 6c 72 63 20 29 7b 0a 20 20 20 20 20  OK==lrc ){.     
ef850 20 2f 2a 20 69 66 20 77 65 20 73 75 63 63 65 65   /* if we succee
ef860 64 65 64 20 69 6e 20 74 61 6b 69 6e 67 20 74 68  ded in taking th
ef870 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2c  e reserved lock,
ef880 20 75 6e 6c 6f 63 6b 20 69 74 20 74 6f 20 72 65   unlock it to re
ef890 73 74 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 74  store.      ** t
ef8a0 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74  he original stat
ef8b0 65 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d  e */.      lrc =
ef8c0 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74   afpSetLock(cont
ef8d0 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  ext->dbPath, pFi
ef8e0 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  le, RESERVED_BYT
ef8f0 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20  E, 1, 0);.    } 
ef900 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 2f 2a 20  else {.      /* 
ef910 69 66 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20  if we failed to 
ef920 67 65 74 20 74 68 65 20 6c 6f 63 6b 20 74 68 65  get the lock the
ef930 6e 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d  n someone else m
ef940 75 73 74 20 68 61 76 65 20 69 74 20 2a 2f 0a 20  ust have it */. 
ef950 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20       reserved = 
ef960 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
ef970 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c   IS_LOCK_ERROR(l
ef980 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 63 3d  rc) ){.      rc=
ef990 6c 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  lrc;.    }.  }. 
ef9a0 20 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74   .  unixLeaveMut
ef9b0 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 28  ex();.  OSTRACE(
ef9c0 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25  ("TEST WR-LOCK %
ef9d0 64 20 25 64 20 25 64 20 28 61 66 70 29 5c 6e 22  d %d %d (afp)\n"
ef9e0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20  , pFile->h, rc, 
ef9f0 72 65 73 65 72 76 65 64 29 29 3b 0a 20 20 0a 20  reserved));.  . 
efa00 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65   *pResOut = rese
efa10 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72  rved;.  return r
efa20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b  c;.}../*.** Lock
efa30 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74   the file with t
efa40 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65  he lock specifie
efa50 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 65  d by parameter e
efa60 46 69 6c 65 4c 6f 63 6b 20 2d 20 6f 6e 65 0a 2a  FileLock - one.*
efa70 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
efa80 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31  ng:.**.**     (1
efa90 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a  ) SHARED_LOCK.**
efaa0 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45       (2) RESERVE
efab0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33  D_LOCK.**     (3
efac0 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a  ) PENDING_LOCK.*
efad0 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53  *     (4) EXCLUS
efae0 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53  IVE_LOCK.**.** S
efaf0 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65  ometimes when re
efb00 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63  questing one loc
efb10 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f  k state, additio
efb20 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a  nal lock states.
efb30 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20  ** are inserted 
efb40 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65  in between.  The
efb50 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66   locking might f
efb60 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68  ail on one of th
efb70 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73  e later.** trans
efb80 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74  itions leaving t
efb90 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69  he lock state di
efba0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61  fferent from wha
efbb0 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74  t it started but
efbc0 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20  .** still short 
efbd0 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68  of its goal.  Th
efbe0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72  e following char
efbf0 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f  t shows the allo
efc00 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  wed.** transitio
efc10 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72  ns and the inser
efc20 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ted intermediate
efc30 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
efc40 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48    UNLOCKED -> SH
efc50 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45  ARED.**    SHARE
efc60 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a  D -> RESERVED.**
efc70 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50      SHARED -> (P
efc80 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
efc90 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52  SIVE.**    RESER
efca0 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  VED -> (PENDING)
efcb0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
efcc0 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45      PENDING -> E
efcd0 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54  XCLUSIVE.**.** T
efce0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
efcf0 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61   only increase a
efd00 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20   lock.  Use the 
efd10 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28  sqlite3OsUnlock(
efd20 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20  ).** routine to 
efd30 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20  lower a locking 
efd40 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  level..*/.static
efd50 20 69 6e 74 20 61 66 70 4c 6f 63 6b 28 73 71 6c   int afpLock(sql
efd60 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
efd70 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 7b 0a 20  nt eFileLock){. 
efd80 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
efd90 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  _OK;.  unixFile 
efda0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
efdb0 6c 65 2a 29 69 64 3b 0a 20 20 75 6e 69 78 49 6e  le*)id;.  unixIn
efdc0 6f 64 65 49 6e 66 6f 20 2a 70 49 6e 6f 64 65 20  odeInfo *pInode 
efdd0 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 3b  = pFile->pInode;
efde0 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  .  afpLockingCon
efdf0 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20  text *context = 
efe00 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  (afpLockingConte
efe10 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63  xt *) pFile->loc
efe20 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a  kingContext;.  .
efe30 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
efe40 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 4c  );.  OSTRACE(("L
efe50 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73  OCK    %d %s was
efe60 20 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d 25   %s(%s,%d) pid=%
efe70 64 20 28 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c  d (afp)\n", pFil
efe80 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20  e->h,.          
efe90 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c   azFileLock(eFil
efea0 65 4c 6f 63 6b 29 2c 20 61 7a 46 69 6c 65 4c 6f  eLock), azFileLo
efeb0 63 6b 28 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c  ck(pFile->eFileL
efec0 6f 63 6b 29 2c 0a 20 20 20 20 20 20 20 20 20 20  ock),.          
efed0 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 70 49 6e 6f   azFileLock(pIno
efee0 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 29 2c 20  de->eFileLock), 
efef0 70 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 20  pInode->nShared 
eff00 2c 20 67 65 74 70 69 64 28 29 29 29 3b 0a 0a 20  , getpid()));.. 
eff10 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
eff20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f  already a lock o
eff30 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d  f this type or m
eff40 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20  ore restrictive 
eff50 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78  on the.  ** unix
eff60 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67  File, do nothing
eff70 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20  . Don't use the 
eff80 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78  afp_end_lock: ex
eff90 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a  it path, as.  **
effa0 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
effb0 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61  ) hasn't been ca
effc0 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20  lled yet..  */. 
effd0 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c   if( pFile->eFil
effe0 65 4c 6f 63 6b 3e 3d 65 46 69 6c 65 4c 6f 63 6b  eLock>=eFileLock
efff0 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 28   ){.    OSTRACE(
f0000 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20  ("LOCK    %d %s 
f0010 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64  ok (already held
f0020 29 20 28 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c  ) (afp)\n", pFil
f0030 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20  e->h,.          
f0040 20 61 7a 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c   azFileLock(eFil
f0050 65 4c 6f 63 6b 29 29 29 3b 0a 20 20 20 20 72 65  eLock)));.    re
f0060 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
f0070 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
f0080 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  ure the locking 
f0090 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72  sequence is corr
f00a0 65 63 74 0a 20 20 2a 2a 20 20 28 31 29 20 57 65  ect.  **  (1) We
f00b0 20 6e 65 76 65 72 20 6d 6f 76 65 20 66 72 6f 6d   never move from
f00c0 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20 61 6e 79   unlocked to any
f00d0 74 68 69 6e 67 20 68 69 67 68 65 72 20 74 68 61  thing higher tha
f00e0 6e 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 0a 20  n shared lock.. 
f00f0 20 2a 2a 20 20 28 32 29 20 53 51 4c 69 74 65 20   **  (2) SQLite 
f0100 6e 65 76 65 72 20 65 78 70 6c 69 63 69 74 6c 79  never explicitly
f0110 20 72 65 71 75 65 73 74 73 20 61 20 70 65 6e 64   requests a pend
f0120 69 67 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 20 28  ig lock..  **  (
f0130 33 29 20 41 20 73 68 61 72 65 64 20 6c 6f 63 6b  3) A shared lock
f0140 20 69 73 20 61 6c 77 61 79 73 20 68 65 6c 64 20   is always held 
f0150 77 68 65 6e 20 61 20 72 65 73 65 72 76 65 20 6c  when a reserve l
f0160 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64  ock is requested
f0170 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
f0180 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63   pFile->eFileLoc
f0190 6b 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 65 46  k!=NO_LOCK || eF
f01a0 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f  ileLock==SHARED_
f01b0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
f01c0 28 20 65 46 69 6c 65 4c 6f 63 6b 21 3d 50 45 4e  ( eFileLock!=PEN
f01d0 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  DING_LOCK );.  a
f01e0 73 73 65 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b  ssert( eFileLock
f01f0 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  !=RESERVED_LOCK 
f0200 7c 7c 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c  || pFile->eFileL
f0210 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ock==SHARED_LOCK
f0220 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73   );.  .  /* This
f0230 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64   mutex is needed
f0240 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e   because pFile->
f0250 70 49 6e 6f 64 65 20 69 73 20 73 68 61 72 65 64  pInode is shared
f0260 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a   across threads.
f0270 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72    */.  unixEnter
f0280 4d 75 74 65 78 28 29 3b 0a 20 20 70 49 6e 6f 64  Mutex();.  pInod
f0290 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64  e = pFile->pInod
f02a0 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65  e;..  /* If some
f02b0 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68   thread using th
f02c0 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63  is PID has a loc
f02d0 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65 6e  k via a differen
f02e0 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a 2a  t unixFile*.  **
f02f0 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 65   handle that pre
f0300 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 65  cludes the reque
f0310 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 72  sted lock, retur
f0320 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69  n BUSY..  */.  i
f0330 66 28 20 28 70 46 69 6c 65 2d 3e 65 46 69 6c 65  f( (pFile->eFile
f0340 4c 6f 63 6b 21 3d 70 49 6e 6f 64 65 2d 3e 65 46  Lock!=pInode->eF
f0350 69 6c 65 4c 6f 63 6b 20 26 26 20 0a 20 20 20 20  ileLock && .    
f0360 20 20 20 28 70 49 6e 6f 64 65 2d 3e 65 46 69 6c     (pInode->eFil
f0370 65 4c 6f 63 6b 3e 3d 50 45 4e 44 49 4e 47 5f 4c  eLock>=PENDING_L
f0380 4f 43 4b 20 7c 7c 20 65 46 69 6c 65 4c 6f 63 6b  OCK || eFileLock
f0390 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20  >SHARED_LOCK)). 
f03a0 20 20 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20      ){.    rc = 
f03b0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
f03c0 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f   goto afp_end_lo
f03d0 63 6b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  ck;.  }.  .  /* 
f03e0 49 66 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  If a SHARED lock
f03f0 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 61   is requested, a
f0400 6e 64 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75  nd some thread u
f0410 73 69 6e 67 20 74 68 69 73 20 50 49 44 20 61 6c  sing this PID al
f0420 72 65 61 64 79 0a 20 20 2a 2a 20 68 61 73 20 61  ready.  ** has a
f0430 20 53 48 41 52 45 44 20 6f 72 20 52 45 53 45 52   SHARED or RESER
f0440 56 45 44 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 69  VED lock, then i
f0450 6e 63 72 65 6d 65 6e 74 20 72 65 66 65 72 65 6e  ncrement referen
f0460 63 65 20 63 6f 75 6e 74 73 20 61 6e 64 0a 20 20  ce counts and.  
f0470 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ** return SQLITE
f0480 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  _OK..  */.  if( 
f0490 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45  eFileLock==SHARE
f04a0 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 20 20 20 20  D_LOCK && .     
f04b0 28 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f  (pInode->eFileLo
f04c0 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ck==SHARED_LOCK 
f04d0 7c 7c 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65  || pInode->eFile
f04e0 4c 6f 63 6b 3d 3d 52 45 53 45 52 56 45 44 5f 4c  Lock==RESERVED_L
f04f0 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 65  OCK) ){.    asse
f0500 72 74 28 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53  rt( eFileLock==S
f0510 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
f0520 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
f0530 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 30 20 29 3b  >eFileLock==0 );
f0540 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e  .    assert( pIn
f0550 6f 64 65 2d 3e 6e 53 68 61 72 65 64 3e 30 20 29  ode->nShared>0 )
f0560 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69  ;.    pFile->eFi
f0570 6c 65 4c 6f 63 6b 20 3d 20 53 48 41 52 45 44 5f  leLock = SHARED_
f0580 4c 4f 43 4b 3b 0a 20 20 20 20 70 49 6e 6f 64 65  LOCK;.    pInode
f0590 2d 3e 6e 53 68 61 72 65 64 2b 2b 3b 0a 20 20 20  ->nShared++;.   
f05a0 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 2b 2b   pInode->nLock++
f05b0 3b 0a 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65  ;.    goto afp_e
f05c0 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 20  nd_lock;.  }.   
f05d0 20 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47   .  /* A PENDING
f05e0 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 20   lock is needed 
f05f0 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 6e 67  before acquiring
f0600 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 61   a SHARED lock a
f0610 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61  nd before.  ** a
f0620 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 43 4c  cquiring an EXCL
f0630 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72  USIVE lock.  For
f0640 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b   the SHARED lock
f0650 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 77 69  , the PENDING wi
f0660 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61  ll.  ** be relea
f0670 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  sed..  */.  if( 
f0680 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45  eFileLock==SHARE
f0690 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c  D_LOCK .      ||
f06a0 20 28 65 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43   (eFileLock==EXC
f06b0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70  LUSIVE_LOCK && p
f06c0 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3c  File->eFileLock<
f06d0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20  PENDING_LOCK).  
f06e0 29 7b 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65  ){.    int faile
f06f0 64 3b 0a 20 20 20 20 66 61 69 6c 65 64 20 3d 20  d;.    failed = 
f0700 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65  afpSetLock(conte
f0710 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c  xt->dbPath, pFil
f0720 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c  e, PENDING_BYTE,
f0730 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 20 28   1, 1);.    if (
f0740 66 61 69 6c 65 64 29 20 7b 0a 20 20 20 20 20 20  failed) {.      
f0750 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20  rc = failed;.   
f0760 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f     goto afp_end_
f0770 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  lock;.    }.  }.
f0780 20 20 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72    .  /* If contr
f0790 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 69 73 20  ol gets to this 
f07a0 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75  point, then actu
f07b0 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e  ally go ahead an
f07c0 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72  d make.  ** oper
f07d0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c  ating system cal
f07e0 6c 73 20 66 6f 72 20 74 68 65 20 73 70 65 63 69  ls for the speci
f07f0 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a  fied lock..  */.
f0800 20 20 69 66 28 20 65 46 69 6c 65 4c 6f 63 6b 3d    if( eFileLock=
f0810 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a  =SHARED_LOCK ){.
f0820 20 20 20 20 69 6e 74 20 6c 72 63 31 2c 20 6c 72      int lrc1, lr
f0830 63 32 2c 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20  c2, lrc1Errno = 
f0840 30 3b 0a 20 20 20 20 6c 6f 6e 67 20 6c 6b 2c 20  0;.    long lk, 
f0850 6d 61 73 6b 3b 0a 20 20 20 20 0a 20 20 20 20 61  mask;.    .    a
f0860 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 6e  ssert( pInode->n
f0870 53 68 61 72 65 64 3d 3d 30 20 29 3b 0a 20 20 20  Shared==0 );.   
f0880 20 61 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d   assert( pInode-
f0890 3e 65 46 69 6c 65 4c 6f 63 6b 3d 3d 30 20 29 3b  >eFileLock==0 );
f08a0 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 6d 61  .        .    ma
f08b0 73 6b 20 3d 20 28 73 69 7a 65 6f 66 28 6c 6f 6e  sk = (sizeof(lon
f08c0 67 29 3d 3d 38 29 20 3f 20 4c 41 52 47 45 53 54  g)==8) ? LARGEST
f08d0 5f 49 4e 54 36 34 20 3a 20 30 78 37 66 66 66 66  _INT64 : 0x7ffff
f08e0 66 66 66 3b 0a 20 20 20 20 2f 2a 20 4e 6f 77 20  fff;.    /* Now 
f08f0 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63  get the read-loc
f0900 6b 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f  k SHARED_LOCK */
f0910 0a 20 20 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61  .    /* note tha
f0920 74 20 74 68 65 20 71 75 61 6c 69 74 79 20 6f 66  t the quality of
f0930 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20   the randomness 
f0940 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 74  doesn't matter t
f0950 68 61 74 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20  hat much */.    
f0960 6c 6b 20 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a  lk = random(); .
f0970 20 20 20 20 70 49 6e 6f 64 65 2d 3e 73 68 61 72      pInode->shar
f0980 65 64 42 79 74 65 20 3d 20 28 6c 6b 20 26 20 6d  edByte = (lk & m
f0990 61 73 6b 29 25 28 53 48 41 52 45 44 5f 53 49 5a  ask)%(SHARED_SIZ
f09a0 45 20 2d 20 31 29 3b 0a 20 20 20 20 6c 72 63 31  E - 1);.    lrc1
f09b0 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f   = afpSetLock(co
f09c0 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70  ntext->dbPath, p
f09d0 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 20  File, .         
f09e0 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 70 49   SHARED_FIRST+pI
f09f0 6e 6f 64 65 2d 3e 73 68 61 72 65 64 42 79 74 65  node->sharedByte
f0a00 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 28  , 1, 1);.    if(
f0a10 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c   IS_LOCK_ERROR(l
f0a20 72 63 31 29 20 29 7b 0a 20 20 20 20 20 20 6c 72  rc1) ){.      lr
f0a30 63 31 45 72 72 6e 6f 20 3d 20 70 46 69 6c 65 2d  c1Errno = pFile-
f0a40 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20  >lastErrno;.    
f0a50 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68  }.    /* Drop th
f0a60 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44  e temporary PEND
f0a70 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  ING lock */.    
f0a80 6c 72 63 32 20 3d 20 61 66 70 53 65 74 4c 6f 63  lrc2 = afpSetLoc
f0a90 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
f0aa0 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e  h, pFile, PENDIN
f0ab0 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20  G_BYTE, 1, 0);. 
f0ac0 20 20 20 0a 20 20 20 20 69 66 28 20 49 53 5f 4c     .    if( IS_L
f0ad0 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29 20  OCK_ERROR(lrc1) 
f0ae0 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  ) {.      pFile-
f0af0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 72 63  >lastErrno = lrc
f0b00 31 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63  1Errno;.      rc
f0b10 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 20 20 67   = lrc1;.      g
f0b20 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b  oto afp_end_lock
f0b30 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28  ;.    } else if(
f0b40 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c   IS_LOCK_ERROR(l
f0b50 72 63 32 29 20 29 7b 0a 20 20 20 20 20 20 72 63  rc2) ){.      rc
f0b60 20 3d 20 6c 72 63 32 3b 0a 20 20 20 20 20 20 67   = lrc2;.      g
f0b70 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b  oto afp_end_lock
f0b80 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28  ;.    } else if(
f0b90 20 6c 72 63 31 20 21 3d 20 53 51 4c 49 54 45 5f   lrc1 != SQLITE_
f0ba0 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 72 63 20  OK ) {.      rc 
f0bb0 3d 20 6c 72 63 31 3b 0a 20 20 20 20 7d 20 65 6c  = lrc1;.    } el
f0bc0 73 65 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  se {.      pFile
f0bd0 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20 53 48  ->eFileLock = SH
f0be0 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20  ARED_LOCK;.     
f0bf0 20 70 49 6e 6f 64 65 2d 3e 6e 4c 6f 63 6b 2b 2b   pInode->nLock++
f0c00 3b 0a 20 20 20 20 20 20 70 49 6e 6f 64 65 2d 3e  ;.      pInode->
f0c10 6e 53 68 61 72 65 64 20 3d 20 31 3b 0a 20 20 20  nShared = 1;.   
f0c20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 65   }.  }else if( e
f0c30 46 69 6c 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53  FileLock==EXCLUS
f0c40 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 49 6e 6f  IVE_LOCK && pIno
f0c50 64 65 2d 3e 6e 53 68 61 72 65 64 3e 31 20 29 7b  de->nShared>1 ){
f0c60 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 74  .    /* We are t
f0c70 72 79 69 6e 67 20 66 6f 72 20 61 6e 20 65 78 63  rying for an exc
f0c80 6c 75 73 69 76 65 20 6c 6f 63 6b 20 62 75 74 20  lusive lock but 
f0c90 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69  another thread i
f0ca0 6e 20 74 68 69 73 0a 20 20 20 20 20 2a 2a 20 73  n this.     ** s
f0cb0 61 6d 65 20 70 72 6f 63 65 73 73 20 69 73 20 73  ame process is s
f0cc0 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 20 73  till holding a s
f0cd0 68 61 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20  hared lock. */. 
f0ce0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
f0cf0 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  USY;.  }else{.  
f0d00 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74    /* The request
f0d10 20 77 61 73 20 66 6f 72 20 61 20 52 45 53 45 52   was for a RESER
f0d20 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45  VED or EXCLUSIVE
f0d30 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 20   lock.  It is.  
f0d40 20 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61    ** assumed tha
f0d50 74 20 74 68 65 72 65 20 69 73 20 61 20 53 48 41  t there is a SHA
f0d60 52 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c  RED or greater l
f0d70 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a  ock on the file.
f0d80 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a      ** already..
f0d90 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 66      */.    int f
f0da0 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 61  ailed = 0;.    a
f0db0 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d  ssert( 0!=pFile-
f0dc0 3e 65 46 69 6c 65 4c 6f 63 6b 20 29 3b 0a 20 20  >eFileLock );.  
f0dd0 20 20 69 66 20 28 65 46 69 6c 65 4c 6f 63 6b 20    if (eFileLock 
f0de0 3e 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  >= RESERVED_LOCK
f0df0 20 26 26 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65   && pFile->eFile
f0e00 4c 6f 63 6b 20 3c 20 52 45 53 45 52 56 45 44 5f  Lock < RESERVED_
f0e10 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 20 20  LOCK) {.        
f0e20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 53  /* Acquire a RES
f0e30 45 52 56 45 44 20 6c 6f 63 6b 20 2a 2f 0a 20 20  ERVED lock */.  
f0e40 20 20 20 20 20 20 66 61 69 6c 65 64 20 3d 20 61        failed = a
f0e50 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78  fpSetLock(contex
f0e60 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  t->dbPath, pFile
f0e70 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c  , RESERVED_BYTE,
f0e80 20 31 2c 31 29 3b 0a 20 20 20 20 20 20 69 66 28   1,1);.      if(
f0e90 20 21 66 61 69 6c 65 64 20 29 7b 0a 20 20 20 20   !failed ){.    
f0ea0 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 72 65 73      context->res
f0eb0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  erved = 1;.     
f0ec0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20   }.    }.    if 
f0ed0 28 21 66 61 69 6c 65 64 20 26 26 20 65 46 69 6c  (!failed && eFil
f0ee0 65 4c 6f 63 6b 20 3d 3d 20 45 58 43 4c 55 53 49  eLock == EXCLUSI
f0ef0 56 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20  VE_LOCK) {.     
f0f00 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45   /* Acquire an E
f0f10 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 2a 2f  XCLUSIVE lock */
f0f20 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20  .        .      
f0f30 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73 68  /* Remove the sh
f0f40 61 72 65 64 20 6c 6f 63 6b 20 62 65 66 6f 72 65  ared lock before
f0f50 20 74 72 79 69 6e 67 20 74 68 65 20 72 61 6e 67   trying the rang
f0f60 65 2e 20 20 77 65 27 6c 6c 20 6e 65 65 64 20 74  e.  we'll need t
f0f70 6f 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 65 73  o .      ** rees
f0f80 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 72  tablish the shar
f0f90 65 64 20 6c 6f 63 6b 20 69 66 20 77 65 20 63 61  ed lock if we ca
f0fa0 6e 27 74 20 67 65 74 20 74 68 65 20 20 61 66 70  n't get the  afp
f0fb0 55 6e 6c 6f 63 6b 0a 20 20 20 20 20 20 2a 2f 0a  Unlock.      */.
f0fc0 20 20 20 20 20 20 69 66 28 20 21 28 66 61 69 6c        if( !(fail
f0fd0 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28  ed = afpSetLock(
f0fe0 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c  context->dbPath,
f0ff0 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46   pFile, SHARED_F
f1000 49 52 53 54 20 2b 0a 20 20 20 20 20 20 20 20 20  IRST +.         
f1010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f1020 70 49 6e 6f 64 65 2d 3e 73 68 61 72 65 64 42 79  pInode->sharedBy
f1030 74 65 2c 20 31 2c 20 30 29 29 20 29 7b 0a 20 20  te, 1, 0)) ){.  
f1040 20 20 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64        int failed
f1050 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  2 = SQLITE_OK;. 
f1060 20 20 20 20 20 20 20 2f 2a 20 6e 6f 77 20 61 74         /* now at
f1070 74 65 6d 6d 70 74 20 74 6f 20 67 65 74 20 74 68  temmpt to get th
f1080 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  e exclusive lock
f1090 20 72 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20   range */.      
f10a0 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65    failed = afpSe
f10b0 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64  tLock(context->d
f10c0 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48  bPath, pFile, SH
f10d0 41 52 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20  ARED_FIRST, .   
f10e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f10f0 20 20 20 20 20 20 20 20 20 20 20 20 53 48 41 52              SHAR
f1100 45 44 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20  ED_SIZE, 1);.   
f1110 20 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 20       if( failed 
f1120 26 26 20 28 66 61 69 6c 65 64 32 20 3d 20 61 66  && (failed2 = af
f1130 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74  pSetLock(context
f1140 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c  ->dbPath, pFile,
f1150 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
f1160 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f           SHARED_
f1170 46 49 52 53 54 20 2b 20 70 49 6e 6f 64 65 2d 3e  FIRST + pInode->
f1180 73 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31  sharedByte, 1, 1
f1190 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  )) ){.          
f11a0 2f 2a 20 43 61 6e 27 74 20 72 65 65 73 74 61 62  /* Can't reestab
f11b0 6c 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20  lish the shared 
f11c0 6c 6f 63 6b 2e 20 20 53 71 6c 69 74 65 20 63 61  lock.  Sqlite ca
f11d0 6e 27 74 20 64 65 61 6c 2c 20 74 68 69 73 20 69  n't deal, this i
f11e0 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61  s.          ** a
f11f0 20 63 72 69 74 69 63 61 6c 20 49 2f 4f 20 65 72   critical I/O er
f1200 72 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  ror.          */
f1210 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
f1220 28 28 66 61 69 6c 65 64 20 26 20 53 51 4c 49 54  ((failed & SQLIT
f1230 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49  E_IOERR) == SQLI
f1240 54 45 5f 49 4f 45 52 52 29 20 3f 20 66 61 69 6c  TE_IOERR) ? fail
f1250 65 64 32 20 3a 20 0a 20 20 20 20 20 20 20 20 20  ed2 : .         
f1260 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45        SQLITE_IOE
f1270 52 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20  RR_LOCK;.       
f1280 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f     goto afp_end_
f1290 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 20  lock;.        } 
f12a0 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
f12b0 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65        rc = faile
f12c0 64 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  d; .      }.    
f12d0 7d 0a 20 20 20 20 69 66 28 20 66 61 69 6c 65 64  }.    if( failed
f12e0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66   ){.      rc = f
f12f0 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d  ailed;.    }.  }
f1300 0a 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  .  .  if( rc==SQ
f1310 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
f1320 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20  File->eFileLock 
f1330 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 20  = eFileLock;.   
f1340 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f   pInode->eFileLo
f1350 63 6b 20 3d 20 65 46 69 6c 65 4c 6f 63 6b 3b 0a  ck = eFileLock;.
f1360 20 20 7d 65 6c 73 65 20 69 66 28 20 65 46 69 6c    }else if( eFil
f1370 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45  eLock==EXCLUSIVE
f1380 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69  _LOCK ){.    pFi
f1390 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20  le->eFileLock = 
f13a0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20  PENDING_LOCK;.  
f13b0 20 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c    pInode->eFileL
f13c0 6f 63 6b 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f  ock = PENDING_LO
f13d0 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70 5f 65  CK;.  }.  .afp_e
f13e0 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c  nd_lock:.  unixL
f13f0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f  eaveMutex();.  O
f1400 53 54 52 41 43 45 28 28 22 4c 4f 43 4b 20 20 20  STRACE(("LOCK   
f1410 20 25 64 20 25 73 20 25 73 20 28 61 66 70 29 5c   %d %s %s (afp)\
f1420 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 61 7a  n", pFile->h, az
f1430 46 69 6c 65 4c 6f 63 6b 28 65 46 69 6c 65 4c 6f  FileLock(eFileLo
f1440 63 6b 29 2c 20 0a 20 20 20 20 20 20 20 20 20 72  ck), .         r
f1450 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22  c==SQLITE_OK ? "
f1460 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29  ok" : "failed"))
f1470 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
f1480 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68  ../*.** Lower th
f1490 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
f14a0 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  on file descript
f14b0 6f 72 20 70 46 69 6c 65 20 74 6f 20 65 46 69 6c  or pFile to eFil
f14c0 65 4c 6f 63 6b 2e 20 20 65 46 69 6c 65 4c 6f 63  eLock.  eFileLoc
f14d0 6b 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74  k.** must be eit
f14e0 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53  her NO_LOCK or S
f14f0 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  HARED_LOCK..**.*
f1500 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  * If the locking
f1510 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69   level of the fi
f1520 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
f1530 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62   already at or b
f1540 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75  elow.** the requ
f1550 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65  ested locking le
f1560 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  vel, this routin
f1570 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  e is a no-op..*/
f1580 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 55  .static int afpU
f1590 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  nlock(sqlite3_fi
f15a0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 65 46 69 6c  le *id, int eFil
f15b0 65 4c 6f 63 6b 29 20 7b 0a 20 20 69 6e 74 20 72  eLock) {.  int r
f15c0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
f15d0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
f15e0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
f15f0 3b 0a 20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66  ;.  unixInodeInf
f1600 6f 20 2a 70 49 6e 6f 64 65 3b 0a 20 20 61 66 70  o *pInode;.  afp
f1610 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
f1620 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f  context = (afpLo
f1630 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20  ckingContext *) 
f1640 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
f1650 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20 73 6b 69  ntext;.  int ski
f1660 70 53 68 61 72 65 64 20 3d 20 30 3b 0a 23 69 66  pShared = 0;.#if
f1670 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
f1680 20 20 69 6e 74 20 68 20 3d 20 70 46 69 6c 65 2d    int h = pFile-
f1690 3e 68 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73  >h;.#endif..  as
f16a0 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20  sert( pFile );. 
f16b0 20 4f 53 54 52 41 43 45 28 28 22 55 4e 4c 4f 43   OSTRACE(("UNLOC
f16c0 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 28  K  %d %d was %d(
f16d0 25 64 2c 25 64 29 20 70 69 64 3d 25 64 20 28 61  %d,%d) pid=%d (a
f16e0 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  fp)\n", pFile->h
f16f0 2c 20 65 46 69 6c 65 4c 6f 63 6b 2c 0a 20 20 20  , eFileLock,.   
f1700 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 65          pFile->e
f1710 46 69 6c 65 4c 6f 63 6b 2c 20 70 46 69 6c 65 2d  FileLock, pFile-
f1720 3e 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f  >pInode->eFileLo
f1730 63 6b 2c 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64  ck, pFile->pInod
f1740 65 2d 3e 6e 53 68 61 72 65 64 2c 0a 20 20 20 20  e->nShared,.    
f1750 20 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29         getpid())
f1760 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 65 46  );..  assert( eF
f1770 69 6c 65 4c 6f 63 6b 3c 3d 53 48 41 52 45 44 5f  ileLock<=SHARED_
f1780 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 46  LOCK );.  if( pF
f1790 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3c 3d  ile->eFileLock<=
f17a0 65 46 69 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20  eFileLock ){.   
f17b0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f17c0 4b 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e 74  K;.  }.  unixEnt
f17d0 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 49 6e  erMutex();.  pIn
f17e0 6f 64 65 20 3d 20 70 46 69 6c 65 2d 3e 70 49 6e  ode = pFile->pIn
f17f0 6f 64 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ode;.  assert( p
f1800 49 6e 6f 64 65 2d 3e 6e 53 68 61 72 65 64 21 3d  Inode->nShared!=
f1810 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65  0 );.  if( pFile
f1820 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3e 53 48 41 52  ->eFileLock>SHAR
f1830 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61  ED_LOCK ){.    a
f1840 73 73 65 72 74 28 20 70 49 6e 6f 64 65 2d 3e 65  ssert( pInode->e
f1850 46 69 6c 65 4c 6f 63 6b 3d 3d 70 46 69 6c 65 2d  FileLock==pFile-
f1860 3e 65 46 69 6c 65 4c 6f 63 6b 20 29 3b 0a 20 20  >eFileLock );.  
f1870 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
f1880 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20  rBenign(1);.    
f1890 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
f18a0 20 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 53 69   h=(-1) ).    Si
f18b0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
f18c0 69 67 6e 28 30 29 3b 0a 20 20 20 20 0a 23 69 66  ign(0);.    .#if
f18d0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
f18e0 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 72 65 64  .    /* When red
f18f0 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20 73 75 63  ucing a lock suc
f1900 68 20 74 68 61 74 20 6f 74 68 65 72 20 70 72 6f  h that other pro
f1910 63 65 73 73 65 73 20 63 61 6e 20 73 74 61 72 74  cesses can start
f1920 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20  .    ** reading 
f1930 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
f1940 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65 20 73 75  e again, make su
f1950 72 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20  re that the.    
f1960 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63  ** transaction c
f1970 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 61 74  ounter was updat
f1980 65 64 20 69 66 20 61 6e 79 20 70 61 72 74 20 6f  ed if any part o
f1990 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20  f the database. 
f19a0 20 20 20 2a 2a 20 66 69 6c 65 20 63 68 61 6e 67     ** file chang
f19b0 65 64 2e 20 20 49 66 20 74 68 65 20 74 72 61 6e  ed.  If the tran
f19c0 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20  saction counter 
f19d0 69 73 20 6e 6f 74 20 75 70 64 61 74 65 64 2c 0a  is not updated,.
f19e0 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f 6e      ** other con
f19f0 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 65 20  nections to the 
f1a00 73 61 6d 65 20 66 69 6c 65 20 6d 69 67 68 74 20  same file might 
f1a10 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74 68 61 74  not realize that
f1a20 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6c 65  .    ** the file
f1a30 20 68 61 73 20 63 68 61 6e 67 65 64 20 61 6e 64   has changed and
f1a40 20 68 65 6e 63 65 20 6d 69 67 68 74 20 6e 6f 74   hence might not
f1a50 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73 68 20 74   know to flush t
f1a60 68 65 69 72 0a 20 20 20 20 2a 2a 20 63 61 63 68  heir.    ** cach
f1a70 65 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 61  e.  The use of a
f1a80 20 73 74 61 6c 65 20 63 61 63 68 65 20 63 61 6e   stale cache can
f1a90 20 6c 65 61 64 20 74 6f 20 64 61 74 61 62 61 73   lead to databas
f1aa0 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20  e corruption..  
f1ab0 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
f1ac0 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c   pFile->inNormal
f1ad0 57 72 69 74 65 3d 3d 30 0a 20 20 20 20 20 20 20  Write==0.       
f1ae0 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 64 62      || pFile->db
f1af0 55 70 64 61 74 65 3d 3d 30 0a 20 20 20 20 20 20  Update==0.      
f1b00 20 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 74       || pFile->t
f1b10 72 61 6e 73 43 6e 74 72 43 68 6e 67 3d 3d 31 20  ransCntrChng==1 
f1b20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e  );.    pFile->in
f1b30 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 30 3b  NormalWrite = 0;
f1b40 0a 23 65 6e 64 69 66 0a 20 20 20 20 0a 20 20 20  .#endif.    .   
f1b50 20 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c   if( pFile->eFil
f1b60 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45  eLock==EXCLUSIVE
f1b70 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72  _LOCK ){.      r
f1b80 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63  c = afpSetLock(c
f1b90 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20  ontext->dbPath, 
f1ba0 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49  pFile, SHARED_FI
f1bb0 52 53 54 2c 20 53 48 41 52 45 44 5f 53 49 5a 45  RST, SHARED_SIZE
f1bc0 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
f1bd0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
f1be0 20 28 65 46 69 6c 65 4c 6f 63 6b 3d 3d 53 48 41   (eFileLock==SHA
f1bf0 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 6e 6f  RED_LOCK || pIno
f1c00 64 65 2d 3e 6e 53 68 61 72 65 64 3e 31 29 20 29  de->nShared>1) )
f1c10 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c  {.        /* onl
f1c20 79 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74  y re-establish t
f1c30 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69  he shared lock i
f1c40 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20  f necessary */. 
f1c50 20 20 20 20 20 20 20 69 6e 74 20 73 68 61 72 65         int share
f1c60 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52  dLockByte = SHAR
f1c70 45 44 5f 46 49 52 53 54 2b 70 49 6e 6f 64 65 2d  ED_FIRST+pInode-
f1c80 3e 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20  >sharedByte;.   
f1c90 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74       rc = afpSet
f1ca0 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62  Lock(context->db
f1cb0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 73 68 61  Path, pFile, sha
f1cc0 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20  redLockByte, 1, 
f1cd0 31 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65  1);.      } else
f1ce0 20 7b 0a 20 20 20 20 20 20 20 20 73 6b 69 70 53   {.        skipS
f1cf0 68 61 72 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  hared = 1;.     
f1d00 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
f1d10 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
f1d20 26 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65 4c 6f  & pFile->eFileLo
f1d30 63 6b 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  ck>=PENDING_LOCK
f1d40 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61   ){.      rc = a
f1d50 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78  fpSetLock(contex
f1d60 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  t->dbPath, pFile
f1d70 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20  , PENDING_BYTE, 
f1d80 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20  1, 0);.    } .  
f1d90 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
f1da0 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 65 46  _OK && pFile->eF
f1db0 69 6c 65 4c 6f 63 6b 3e 3d 52 45 53 45 52 56 45  ileLock>=RESERVE
f1dc0 44 5f 4c 4f 43 4b 20 26 26 20 63 6f 6e 74 65 78  D_LOCK && contex
f1dd0 74 2d 3e 72 65 73 65 72 76 65 64 20 29 7b 0a 20  t->reserved ){. 
f1de0 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74       rc = afpSet
f1df0 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62  Lock(context->db
f1e00 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53  Path, pFile, RES
f1e10 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 20 30  ERVED_BYTE, 1, 0
f1e20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 72 63  );.      if( !rc
f1e30 20 29 7b 20 0a 20 20 20 20 20 20 20 20 63 6f 6e   ){ .        con
f1e40 74 65 78 74 2d 3e 72 65 73 65 72 76 65 64 20 3d  text->reserved =
f1e50 20 30 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20   0; .      }.   
f1e60 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
f1e70 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 65 46 69  QLITE_OK && (eFi
f1e80 6c 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44 5f 4c  leLock==SHARED_L
f1e90 4f 43 4b 20 7c 7c 20 70 49 6e 6f 64 65 2d 3e 6e  OCK || pInode->n
f1ea0 53 68 61 72 65 64 3e 31 29 29 7b 0a 20 20 20 20  Shared>1)){.    
f1eb0 20 20 70 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c    pInode->eFileL
f1ec0 6f 63 6b 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43  ock = SHARED_LOC
f1ed0 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  K;.    }.  }.  i
f1ee0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
f1ef0 20 26 26 20 65 46 69 6c 65 4c 6f 63 6b 3d 3d 4e   && eFileLock==N
f1f00 4f 5f 4c 4f 43 4b 20 29 7b 0a 0a 20 20 20 20 2f  O_LOCK ){..    /
f1f10 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20  * Decrement the 
f1f20 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f 75 6e  shared lock coun
f1f30 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20 74 68  ter.  Release th
f1f40 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61 6e 0a  e lock using an.
f1f50 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c 6c 20 6f      ** OS call o
f1f60 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20 74 68 72  nly when all thr
f1f70 65 61 64 73 20 69 6e 20 74 68 69 73 20 73 61 6d  eads in this sam
f1f80 65 20 70 72 6f 63 65 73 73 20 68 61 76 65 20 72  e process have r
f1f90 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a 20 74  eleased.    ** t
f1fa0 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a  he lock..    */.
f1fb0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e      unsigned lon
f1fc0 67 20 6c 6f 6e 67 20 73 68 61 72 65 64 4c 6f 63  g long sharedLoc
f1fd0 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f 46  kByte = SHARED_F
f1fe0 49 52 53 54 2b 70 49 6e 6f 64 65 2d 3e 73 68 61  IRST+pInode->sha
f1ff0 72 65 64 42 79 74 65 3b 0a 20 20 20 20 70 49 6e  redByte;.    pIn
f2000 6f 64 65 2d 3e 6e 53 68 61 72 65 64 2d 2d 3b 0a  ode->nShared--;.
f2010 20 20 20 20 69 66 28 20 70 49 6e 6f 64 65 2d 3e      if( pInode->
f2020 6e 53 68 61 72 65 64 3d 3d 30 20 29 7b 0a 20 20  nShared==0 ){.  
f2030 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72      SimulateIOEr
f2040 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20  rorBenign(1);.  
f2050 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72      SimulateIOEr
f2060 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a 20 20  ror( h=(-1) ).  
f2070 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72      SimulateIOEr
f2080 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20  rorBenign(0);.  
f2090 20 20 20 20 69 66 28 20 21 73 6b 69 70 53 68 61      if( !skipSha
f20a0 72 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72  red ){.        r
f20b0 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63  c = afpSetLock(c
f20c0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20  ontext->dbPath, 
f20d0 70 46 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63  pFile, sharedLoc
f20e0 6b 42 79 74 65 2c 20 31 2c 20 30 29 3b 0a 20 20  kByte, 1, 0);.  
f20f0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
f2100 21 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 70  !rc ){.        p
f2110 49 6e 6f 64 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b  Inode->eFileLock
f2120 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20   = NO_LOCK;.    
f2130 20 20 20 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65      pFile->eFile
f2140 4c 6f 63 6b 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a  Lock = NO_LOCK;.
f2150 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
f2160 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
f2170 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 49 6e  _OK ){.      pIn
f2180 6f 64 65 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20  ode->nLock--;.  
f2190 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 6f      assert( pIno
f21a0 64 65 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b 0a  de->nLock>=0 );.
f21b0 20 20 20 20 20 20 69 66 28 20 70 49 6e 6f 64 65        if( pInode
f21c0 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20  ->nLock==0 ){.  
f21d0 20 20 20 20 20 20 63 6c 6f 73 65 50 65 6e 64 69        closePendi
f21e0 6e 67 46 64 73 28 70 46 69 6c 65 29 3b 0a 20 20  ngFds(pFile);.  
f21f0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
f2200 20 20 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75    .  unixLeaveMu
f2210 74 65 78 28 29 3b 0a 20 20 69 66 28 20 72 63 3d  tex();.  if( rc=
f2220 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 70 46 69  =SQLITE_OK ) pFi
f2230 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20  le->eFileLock = 
f2240 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 72 65 74  eFileLock;.  ret
f2250 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
f2260 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 26 20   Close a file & 
f2270 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70 65 63  cleanup AFP spec
f2280 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e  ific locking con
f2290 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69 63 20  text .*/.static 
f22a0 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73 71 6c  int afpClose(sql
f22b0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b  ite3_file *id) {
f22c0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
f22d0 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64 20  TE_OK;.  if( id 
f22e0 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20  ){.    unixFile 
f22f0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
f2300 6c 65 2a 29 69 64 3b 0a 20 20 20 20 61 66 70 55  le*)id;.    afpU
f2310 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43  nlock(id, NO_LOC
f2320 4b 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65  K);.    unixEnte
f2330 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66  rMutex();.    if
f2340 28 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 20  ( pFile->pInode 
f2350 26 26 20 70 46 69 6c 65 2d 3e 70 49 6e 6f 64 65  && pFile->pInode
f2360 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20  ->nLock ){.     
f2370 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
f2380 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63   outstanding loc
f2390 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75 61  ks, do not actua
f23a0 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65 20 66 69  lly close the fi
f23b0 6c 65 20 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a  le just.      **
f23c0 20 79 65 74 20 62 65 63 61 75 73 65 20 74 68 61   yet because tha
f23d0 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 20 74 68  t would clear th
f23e0 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74  ose locks.  Inst
f23f0 65 61 64 2c 20 61 64 64 20 74 68 65 20 66 69 6c  ead, add the fil
f2400 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72  e.      ** descr
f2410 69 70 74 6f 72 20 74 6f 20 70 49 6e 6f 64 65 2d  iptor to pInode-
f2420 3e 61 50 65 6e 64 69 6e 67 2e 20 20 49 74 20 77  >aPending.  It w
f2430 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63  ill be automatic
f2440 61 6c 6c 79 20 63 6c 6f 73 65 64 20 77 68 65 6e  ally closed when
f2450 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 61  .      ** the la
f2460 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72  st lock is clear
f2470 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ed..      */.   
f2480 20 20 20 73 65 74 50 65 6e 64 69 6e 67 46 64 28     setPendingFd(
f2490 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20  pFile);.    }.  
f24a0 20 20 72 65 6c 65 61 73 65 49 6e 6f 64 65 49 6e    releaseInodeIn
f24b0 66 6f 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 73  fo(pFile);.    s
f24c0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c  qlite3_free(pFil
f24d0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f24e0 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c 6f  t);.    rc = clo
f24f0 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a  seUnixFile(id);.
f2500 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74      unixLeaveMut
f2510 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ex();.  }.  retu
f2520 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66  rn rc;.}..#endif
f2530 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50   /* defined(__AP
f2540 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45  PLE__) && SQLITE
f2550 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
f2560 53 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54  STYLE */./*.** T
f2570 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20 69 73  he code above is
f2580 20 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d   the AFP lock im
f2590 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54  plementation.  T
f25a0 68 65 20 63 6f 64 65 20 69 73 20 73 70 65 63 69  he code is speci
f25b0 66 69 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 58  fic.** to MacOSX
f25c0 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 6f   and does not wo
f25d0 72 6b 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 78  rk on other unix
f25e0 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f 20   platforms.  No 
f25f0 61 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 69  alternative.** i
f2600 73 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66  s available.  If
f2610 20 79 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70 69   you don't compi
f2620 6c 65 20 66 6f 72 20 61 20 6d 61 63 2c 20 74 68  le for a mac, th
f2630 65 6e 20 74 68 65 20 22 75 6e 69 78 2d 61 66 70  en the "unix-afp
f2640 22 0a 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74 20  ".** VFS is not 
f2650 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  available..**.**
f2660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2670 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 41  *** End of the A
f2680 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e  FP lock implemen
f2690 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tation *********
f26a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
f26b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f26c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f26d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f26e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f26f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
f2700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
f2750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
f2770 20 4e 46 53 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a   NFS Locking ***
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 2f 0a 0a  *************/..
f27a0 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50  #if defined(__AP
f27b0 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45  PLE__) && SQLITE
f27c0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
f27d0 53 54 59 4c 45 0a 2f 2a 0a 20 2a 2a 20 4c 6f 77  STYLE./*. ** Low
f27e0 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  er the locking l
f27f0 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73  evel on file des
f2800 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f  criptor pFile to
f2810 20 65 46 69 6c 65 4c 6f 63 6b 2e 20 20 65 46 69   eFileLock.  eFi
f2820 6c 65 4c 6f 63 6b 0a 20 2a 2a 20 6d 75 73 74 20  leLock. ** must 
f2830 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43  be either NO_LOC
f2840 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b  K or SHARED_LOCK
f2850 2e 0a 20 2a 2a 0a 20 2a 2a 20 49 66 20 74 68 65  .. **. ** If the
f2860 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
f2870 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  f the file descr
f2880 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79  iptor is already
f2890 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 20 2a 2a   at or below. **
f28a0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
f28b0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68  ocking level, th
f28c0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
f28d0 6e 6f 2d 6f 70 2e 0a 20 2a 2f 0a 73 74 61 74 69  no-op.. */.stati
f28e0 63 20 69 6e 74 20 6e 66 73 55 6e 6c 6f 63 6b 28  c int nfsUnlock(
f28f0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
f2900 2c 20 69 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29  , int eFileLock)
f2910 7b 0a 20 20 72 65 74 75 72 6e 20 70 6f 73 69 78  {.  return posix
f2920 55 6e 6c 6f 63 6b 28 69 64 2c 20 65 46 69 6c 65  Unlock(id, eFile
f2930 4c 6f 63 6b 2c 20 31 29 3b 0a 7d 0a 0a 23 65 6e  Lock, 1);.}..#en
f2940 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f  dif /* defined(_
f2950 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c  _APPLE__) && SQL
f2960 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
f2970 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a  NG_STYLE */./*.*
f2980 2a 20 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65  * The code above
f2990 20 69 73 20 74 68 65 20 4e 46 53 20 6c 6f 63 6b   is the NFS lock
f29a0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
f29b0 20 20 54 68 65 20 63 6f 64 65 20 69 73 20 73 70    The code is sp
f29c0 65 63 69 66 69 63 0a 2a 2a 20 74 6f 20 4d 61 63  ecific.** to Mac
f29d0 4f 53 58 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74  OSX and does not
f29e0 20 77 6f 72 6b 20 6f 6e 20 6f 74 68 65 72 20 75   work on other u
f29f0 6e 69 78 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20  nix platforms.  
f2a00 4e 6f 20 61 6c 74 65 72 6e 61 74 69 76 65 0a 2a  No alternative.*
f2a10 2a 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 20  * is available. 
f2a20 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   .**.***********
f2a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
f2a40 66 20 74 68 65 20 4e 46 53 20 6c 6f 63 6b 20 69  f the NFS lock i
f2a50 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a  mplementation **
f2a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2a70 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
f2a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2ac0 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
f2ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2b10 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
f2b20 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c 6f 63 6b 69  ****** Non-locki
f2b30 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ng sqlite3_file 
f2b40 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a  methods ********
f2b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2b60 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  *****.**.** The 
f2b70 6e 65 78 74 20 64 69 76 69 73 69 6f 6e 20 63 6f  next division co
f2b80 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74  ntains implement
f2b90 61 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 20 6d  ations for all m
f2ba0 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 0a 2a  ethods of the .*
f2bb0 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f  * sqlite3_file o
f2bc0 62 6a 65 63 74 20 6f 74 68 65 72 20 74 68 61 6e  bject other than
f2bd0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74   the locking met
f2be0 68 6f 64 73 2e 20 20 54 68 65 20 6c 6f 63 6b 69  hods.  The locki
f2bf0 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 77 65  ng.** methods we
f2c00 72 65 20 64 65 66 69 6e 65 64 20 69 6e 20 64 69  re defined in di
f2c10 76 69 73 69 6f 6e 73 20 61 62 6f 76 65 20 28 6f  visions above (o
f2c20 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f  ne locking metho
f2c30 64 20 70 65 72 0a 2a 2a 20 64 69 76 69 73 69 6f  d per.** divisio
f2c40 6e 29 2e 20 20 54 68 6f 73 65 20 6d 65 74 68 6f  n).  Those metho
f2c50 64 73 20 74 68 61 74 20 61 72 65 20 63 6f 6d 6d  ds that are comm
f2c60 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e  on to all lockin
f2c70 67 20 6d 6f 64 65 73 0a 2a 2a 20 61 72 65 20 67  g modes.** are g
f2c80 61 74 68 65 72 20 74 6f 67 65 74 68 65 72 20 69  ather together i
f2c90 6e 74 6f 20 74 68 69 73 20 64 69 76 69 73 69 6f  nto this divisio
f2ca0 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 65  n..*/../*.** See
f2cb0 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20  k to the offset 
f2cc0 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
f2cd0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 74  cond argument, t
f2ce0 68 65 6e 20 72 65 61 64 20 63 6e 74 20 0a 2a 2a  hen read cnt .**
f2cf0 20 62 79 74 65 73 20 69 6e 74 6f 20 70 42 75 66   bytes into pBuf
f2d00 2e 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  . Return the num
f2d10 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74  ber of bytes act
f2d20 75 61 6c 6c 79 20 72 65 61 64 2e 0a 2a 2a 0a 2a  ually read..**.*
f2d30 2a 20 4e 42 3a 20 20 49 66 20 79 6f 75 20 64 65  * NB:  If you de
f2d40 66 69 6e 65 20 55 53 45 5f 50 52 45 41 44 20 6f  fine USE_PREAD o
f2d50 72 20 55 53 45 5f 50 52 45 41 44 36 34 2c 20 74  r USE_PREAD64, t
f2d60 68 65 6e 20 69 74 20 6d 69 67 68 74 20 61 6c 73  hen it might als
f2d70 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 73 73 61 72  o.** be necessar
f2d80 79 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50  y to define _XOP
f2d90 45 4e 5f 53 4f 55 52 43 45 20 74 6f 20 62 65 20  EN_SOURCE to be 
f2da0 35 30 30 2e 20 20 54 68 69 73 20 76 61 72 69 65  500.  This varie
f2db0 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 73 79  s from.** one sy
f2dc0 73 74 65 6d 20 74 6f 20 61 6e 6f 74 68 65 72 2e  stem to another.
f2dd0 20 20 53 69 6e 63 65 20 53 51 4c 69 74 65 20 64    Since SQLite d
f2de0 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20 55  oes not define U
f2df0 53 45 5f 50 52 45 41 44 0a 2a 2a 20 61 6e 79 20  SE_PREAD.** any 
f2e00 61 6e 79 20 66 6f 72 6d 20 62 79 20 64 65 66 61  any form by defa
f2e10 75 6c 74 2c 20 77 65 20 77 69 6c 6c 20 6e 6f 74  ult, we will not
f2e20 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 66 69   attempt to defi
f2e30 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45  ne _XOPEN_SOURCE
f2e40 2e 0a 2a 2a 20 53 65 65 20 74 69 63 6b 65 74 73  ..** See tickets
f2e50 20 23 32 37 34 31 20 61 6e 64 20 23 32 36 38 31   #2741 and #2681
f2e60 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64  ..**.** To avoid
f2e70 20 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 65 72   stomping the er
f2e80 72 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20 66  rno value on a f
f2e90 61 69 6c 65 64 20 72 65 61 64 20 74 68 65 20 6c  ailed read the l
f2ea0 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a  astErrno value.*
f2eb0 2a 20 69 73 20 73 65 74 20 62 65 66 6f 72 65 20  * is set before 
f2ec0 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74  returning..*/.st
f2ed0 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64  atic int seekAnd
f2ee0 52 65 61 64 28 75 6e 69 78 46 69 6c 65 20 2a 69  Read(unixFile *i
f2ef0 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  d, sqlite3_int64
f2f00 20 6f 66 66 73 65 74 2c 20 76 6f 69 64 20 2a 70   offset, void *p
f2f10 42 75 66 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20  Buf, int cnt){. 
f2f20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69 6e 74 20   int got;.  int 
f2f30 70 72 69 6f 72 20 3d 20 30 3b 0a 23 69 66 20 28  prior = 0;.#if (
f2f40 21 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45  !defined(USE_PRE
f2f50 41 44 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  AD) && !defined(
f2f60 55 53 45 5f 50 52 45 41 44 36 34 29 29 0a 20 20  USE_PREAD64)).  
f2f70 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a 23  i64 newOffset;.#
f2f80 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 53 54  endif.  TIMER_ST
f2f90 41 52 54 3b 0a 20 20 61 73 73 65 72 74 28 20 63  ART;.  assert( c
f2fa0 6e 74 3d 3d 28 63 6e 74 26 30 78 31 66 66 66 66  nt==(cnt&0x1ffff
f2fb0 29 20 29 3b 0a 20 20 63 6e 74 20 26 3d 20 30 78  ) );.  cnt &= 0x
f2fc0 31 66 66 66 66 3b 0a 20 20 64 6f 7b 0a 23 69 66  1ffff;.  do{.#if
f2fd0 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45   defined(USE_PRE
f2fe0 41 44 29 0a 20 20 20 20 67 6f 74 20 3d 20 6f 73  AD).    got = os
f2ff0 50 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 75  Pread(id->h, pBu
f3000 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b  f, cnt, offset);
f3010 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  .    SimulateIOE
f3020 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29  rror( got = -1 )
f3030 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28  ;.#elif defined(
f3040 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 20  USE_PREAD64).   
f3050 20 67 6f 74 20 3d 20 6f 73 50 72 65 61 64 36 34   got = osPread64
f3060 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e  (id->h, pBuf, cn
f3070 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20  t, offset);.    
f3080 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
f3090 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c   got = -1 );.#el
f30a0 73 65 0a 20 20 20 20 6e 65 77 4f 66 66 73 65 74  se.    newOffset
f30b0 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20   = lseek(id->h, 
f30c0 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54  offset, SEEK_SET
f30d0 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49  );.    SimulateI
f30e0 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73 65  OError( newOffse
f30f0 74 2d 2d 20 29 3b 0a 20 20 20 20 69 66 28 20 6e  t-- );.    if( n
f3100 65 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74  ewOffset!=offset
f3110 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65   ){.      if( ne
f3120 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b  wOffset == -1 ){
f3130 0a 20 20 20 20 20 20 20 20 28 28 75 6e 69 78 46  .        ((unixF
f3140 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72  ile*)id)->lastEr
f3150 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
f3160 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
f3170 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64    ((unixFile*)id
f3180 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30  )->lastErrno = 0
f3190 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
f31a0 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d  return -1;.    }
f31b0 0a 20 20 20 20 67 6f 74 20 3d 20 6f 73 52 65 61  .    got = osRea
f31c0 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63  d(id->h, pBuf, c
f31d0 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  nt);.#endif.    
f31e0 69 66 28 20 67 6f 74 3d 3d 63 6e 74 20 29 20 62  if( got==cnt ) b
f31f0 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 67 6f  reak;.    if( go
f3200 74 3c 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  t<0 ){.      if(
f3210 20 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20 29 7b   errno==EINTR ){
f3220 20 67 6f 74 20 3d 20 31 3b 20 63 6f 6e 74 69 6e   got = 1; contin
f3230 75 65 3b 20 7d 0a 20 20 20 20 20 20 70 72 69 6f  ue; }.      prio
f3240 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 28 28 75  r = 0;.      ((u
f3250 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61  nixFile*)id)->la
f3260 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
f3270 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
f3280 20 20 7d 65 6c 73 65 20 69 66 28 20 67 6f 74 3e    }else if( got>
f3290 30 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 20 2d  0 ){.      cnt -
f32a0 3d 20 67 6f 74 3b 0a 20 20 20 20 20 20 6f 66 66  = got;.      off
f32b0 73 65 74 20 2b 3d 20 67 6f 74 3b 0a 20 20 20 20  set += got;.    
f32c0 20 20 70 72 69 6f 72 20 2b 3d 20 67 6f 74 3b 0a    prior += got;.
f32d0 20 20 20 20 20 20 70 42 75 66 20 3d 20 28 76 6f        pBuf = (vo
f32e0 69 64 2a 29 28 67 6f 74 20 2b 20 28 63 68 61 72  id*)(got + (char
f32f0 2a 29 70 42 75 66 29 3b 0a 20 20 20 20 7d 0a 20  *)pBuf);.    }. 
f3300 20 7d 77 68 69 6c 65 28 20 67 6f 74 3e 30 20 29   }while( got>0 )
f3310 3b 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20  ;.  TIMER_END;. 
f3320 20 4f 53 54 52 41 43 45 28 28 22 52 45 41 44 20   OSTRACE(("READ 
f3330 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c     %-3d %5d %7ll
f3340 64 20 25 6c 6c 75 5c 6e 22 2c 0a 20 20 20 20 20  d %llu\n",.     
f3350 20 20 20 20 20 20 20 69 64 2d 3e 68 2c 20 67 6f         id->h, go
f3360 74 2b 70 72 69 6f 72 2c 20 6f 66 66 73 65 74 2d  t+prior, offset-
f3370 70 72 69 6f 72 2c 20 54 49 4d 45 52 5f 45 4c 41  prior, TIMER_ELA
f3380 50 53 45 44 29 29 3b 0a 20 20 72 65 74 75 72 6e  PSED));.  return
f3390 20 67 6f 74 2b 70 72 69 6f 72 3b 0a 7d 0a 0a 2f   got+prior;.}../
f33a0 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66  *.** Read data f
f33b0 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 6f 20  rom a file into 
f33c0 61 20 62 75 66 66 65 72 2e 20 20 52 65 74 75 72  a buffer.  Retur
f33d0 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61  n SQLITE_OK if a
f33e0 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 72 65  ll.** bytes were
f33f0 20 72 65 61 64 20 73 75 63 63 65 73 73 66 75 6c   read successful
f3400 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f  ly and SQLITE_IO
f3410 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e 67 20  ERR if anything 
f3420 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a  goes.** wrong..*
f3430 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
f3440 78 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33  xRead(.  sqlite3
f3450 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 76 6f  _file *id, .  vo
f3460 69 64 20 2a 70 42 75 66 2c 20 0a 20 20 69 6e 74  id *pBuf, .  int
f3470 20 61 6d 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f   amt,.  sqlite3_
f3480 69 6e 74 36 34 20 6f 66 66 73 65 74 0a 29 7b 0a  int64 offset.){.
f3490 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
f34a0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29  e = (unixFile *)
f34b0 69 64 3b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20  id;.  int got;. 
f34c0 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 0a   assert( id );..
f34d0 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
f34e0 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  a database file 
f34f0 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20  (not a journal, 
f3500 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f  master-journal o
f3510 72 20 74 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65  r temp.  ** file
f3520 29 2c 20 74 68 65 20 62 79 74 65 73 20 69 6e 20  ), the bytes in 
f3530 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67  the locking rang
f3540 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  e should never b
f3550 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65  e read or writte
f3560 6e 2e 20 2a 2f 0a 23 69 66 20 30 0a 20 20 61 73  n. */.#if 0.  as
f3570 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e  sert( pFile->pUn
f3580 75 73 65 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c  used==0.       |
f3590 7c 20 6f 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e  | offset>=PENDIN
f35a0 47 5f 42 59 54 45 2b 35 31 32 0a 20 20 20 20 20  G_BYTE+512.     
f35b0 20 20 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c    || offset+amt<
f35c0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20  =PENDING_BYTE . 
f35d0 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 67 6f   );.#endif..  go
f35e0 74 20 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28  t = seekAndRead(
f35f0 70 46 69 6c 65 2c 20 6f 66 66 73 65 74 2c 20 70  pFile, offset, p
f3600 42 75 66 2c 20 61 6d 74 29 3b 0a 20 20 69 66 28  Buf, amt);.  if(
f3610 20 67 6f 74 3d 3d 61 6d 74 20 29 7b 0a 20 20 20   got==amt ){.   
f3620 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f3630 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67  K;.  }else if( g
f3640 6f 74 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 6c  ot<0 ){.    /* l
f3650 61 73 74 45 72 72 6e 6f 20 73 65 74 20 62 79 20  astErrno set by 
f3660 73 65 65 6b 41 6e 64 52 65 61 64 20 2a 2f 0a 20  seekAndRead */. 
f3670 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
f3680 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d  _IOERR_READ;.  }
f3690 65 6c 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d  else{.    pFile-
f36a0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20  >lastErrno = 0; 
f36b0 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20  /* not a system 
f36c0 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20  error */.    /* 
f36d0 55 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20  Unread parts of 
f36e0 74 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20  the buffer must 
f36f0 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a  be zero-filled *
f3700 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28  /.    memset(&((
f3710 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d  char*)pBuf)[got]
f3720 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20  , 0, amt-got);. 
f3730 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
f3740 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
f3750 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  D;.  }.}../*.** 
f3760 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73  Seek to the offs
f3770 65 74 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74  et in id->offset
f3780 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62   then read cnt b
f3790 79 74 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a  ytes into pBuf..
f37a0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
f37b0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63  mber of bytes ac
f37c0 74 75 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70  tually read.  Up
f37d0 64 61 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e  date the offset.
f37e0 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20  .**.** To avoid 
f37f0 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 72  stomping the err
f3800 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 61  no value on a fa
f3810 69 6c 65 64 20 77 72 69 74 65 20 74 68 65 20 6c  iled write the l
f3820 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a  astErrno value.*
f3830 2a 20 69 73 20 73 65 74 20 62 65 66 6f 72 65 20  * is set before 
f3840 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74  returning..*/.st
f3850 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64  atic int seekAnd
f3860 57 72 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a  Write(unixFile *
f3870 69 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20  id, i64 offset, 
f3880 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66  const void *pBuf
f3890 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e  , int cnt){.  in
f38a0 74 20 67 6f 74 3b 0a 23 69 66 20 28 21 64 65 66  t got;.#if (!def
f38b0 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 29 20  ined(USE_PREAD) 
f38c0 26 26 20 21 64 65 66 69 6e 65 64 28 55 53 45 5f  && !defined(USE_
f38d0 50 52 45 41 44 36 34 29 29 0a 20 20 69 36 34 20  PREAD64)).  i64 
f38e0 6e 65 77 4f 66 66 73 65 74 3b 0a 23 65 6e 64 69  newOffset;.#endi
f38f0 66 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 3d  f.  assert( cnt=
f3900 3d 28 63 6e 74 26 30 78 31 66 66 66 66 29 20 29  =(cnt&0x1ffff) )
f3910 3b 0a 20 20 63 6e 74 20 26 3d 20 30 78 31 66 66  ;.  cnt &= 0x1ff
f3920 66 66 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52  ff;.  TIMER_STAR
f3930 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55  T;.#if defined(U
f3940 53 45 5f 50 52 45 41 44 29 0a 20 20 64 6f 7b 20  SE_PREAD).  do{ 
f3950 67 6f 74 20 3d 20 6f 73 50 77 72 69 74 65 28 69  got = osPwrite(i
f3960 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c  d->h, pBuf, cnt,
f3970 20 6f 66 66 73 65 74 29 3b 20 7d 77 68 69 6c 65   offset); }while
f3980 28 20 67 6f 74 3c 30 20 26 26 20 65 72 72 6e 6f  ( got<0 && errno
f3990 3d 3d 45 49 4e 54 52 20 29 3b 0a 23 65 6c 69 66  ==EINTR );.#elif
f39a0 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45   defined(USE_PRE
f39b0 41 44 36 34 29 0a 20 20 64 6f 7b 20 67 6f 74 20  AD64).  do{ got 
f39c0 3d 20 6f 73 50 77 72 69 74 65 36 34 28 69 64 2d  = osPwrite64(id-
f39d0 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f  >h, pBuf, cnt, o
f39e0 66 66 73 65 74 29 3b 7d 77 68 69 6c 65 28 20 67  ffset);}while( g
f39f0 6f 74 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d 45  ot<0 && errno==E
f3a00 49 4e 54 52 29 3b 0a 23 65 6c 73 65 0a 20 20 64  INTR);.#else.  d
f3a10 6f 7b 0a 20 20 20 20 6e 65 77 4f 66 66 73 65 74  o{.    newOffset
f3a20 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20   = lseek(id->h, 
f3a30 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54  offset, SEEK_SET
f3a40 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49  );.    SimulateI
f3a50 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73 65  OError( newOffse
f3a60 74 2d 2d 20 29 3b 0a 20 20 20 20 69 66 28 20 6e  t-- );.    if( n
f3a70 65 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74  ewOffset!=offset
f3a80 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65   ){.      if( ne
f3a90 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b  wOffset == -1 ){
f3aa0 0a 20 20 20 20 20 20 20 20 28 28 75 6e 69 78 46  .        ((unixF
f3ab0 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72  ile*)id)->lastEr
f3ac0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
f3ad0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
f3ae0 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64    ((unixFile*)id
f3af0 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30  )->lastErrno = 0
f3b00 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
f3b10 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d  return -1;.    }
f3b20 0a 20 20 20 20 67 6f 74 20 3d 20 6f 73 57 72 69  .    got = osWri
f3b30 74 65 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20  te(id->h, pBuf, 
f3b40 63 6e 74 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20  cnt);.  }while( 
f3b50 67 6f 74 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d  got<0 && errno==
f3b60 45 49 4e 54 52 20 29 3b 0a 23 65 6e 64 69 66 0a  EINTR );.#endif.
f3b70 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 69    TIMER_END;.  i
f3b80 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 20  f( got<0 ){.    
f3b90 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
f3ba0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
f3bb0 6e 6f 3b 0a 20 20 7d 0a 0a 20 20 4f 53 54 52 41  no;.  }..  OSTRA
f3bc0 43 45 28 28 22 57 52 49 54 45 20 20 20 25 2d 33  CE(("WRITE   %-3
f3bd0 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75  d %5d %7lld %llu
f3be0 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c  \n", id->h, got,
f3bf0 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f 45   offset, TIMER_E
f3c00 4c 41 50 53 45 44 29 29 3b 0a 20 20 72 65 74 75  LAPSED));.  retu
f3c10 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  rn got;.}.../*.*
f3c20 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f  * Write data fro
f3c30 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20  m a buffer into 
f3c40 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20  a file.  Return 
f3c50 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
f3c60 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20  cess.** or some 
f3c70 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65  other error code
f3c80 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a   on failure..*/.
f3c90 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 57  static int unixW
f3ca0 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  rite(.  sqlite3_
f3cb0 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 6f 6e  file *id, .  con
f3cc0 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a  st void *pBuf, .
f3cd0 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c    int amt,.  sql
f3ce0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65  ite3_int64 offse
f3cf0 74 20 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65  t .){.  unixFile
f3d00 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
f3d10 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 77  ile*)id;.  int w
f3d20 72 6f 74 65 20 3d 20 30 3b 0a 20 20 61 73 73 65  rote = 0;.  asse
f3d30 72 74 28 20 69 64 20 29 3b 0a 20 20 61 73 73 65  rt( id );.  asse
f3d40 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a 20 20  rt( amt>0 );..  
f3d50 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  /* If this is a 
f3d60 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e  database file (n
f3d70 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61  ot a journal, ma
f3d80 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20  ster-journal or 
f3d90 74 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c  temp.  ** file),
f3da0 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68   the bytes in th
f3db0 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20  e locking range 
f3dc0 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20  should never be 
f3dd0 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e  read or written.
f3de0 20 2a 2f 0a 23 69 66 20 30 0a 20 20 61 73 73 65   */.#if 0.  asse
f3df0 72 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73  rt( pFile->pUnus
f3e00 65 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20  ed==0.       || 
f3e10 6f 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e 47 5f  offset>=PENDING_
f3e20 42 59 54 45 2b 35 31 32 0a 20 20 20 20 20 20 20  BYTE+512.       
f3e30 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50  || offset+amt<=P
f3e40 45 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 20 29  ENDING_BYTE .  )
f3e50 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ;.#endif..#ifdef
f3e60 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
f3e70 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69  /* If we are doi
f3e80 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74  ng a normal writ
f3e90 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20  e to a database 
f3ea0 66 69 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64  file (as opposed
f3eb0 20 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61   to.  ** doing a
f3ec0 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c   hot-journal rol
f3ed0 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65  lback or a write
f3ee0 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74   to some file ot
f3ef0 68 65 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20  her than a.  ** 
f3f00 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20  normal database 
f3f10 66 69 6c 65 29 20 74 68 65 6e 20 72 65 63 6f 72  file) then recor
f3f20 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20  d the fact that 
f3f30 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a  the database.  *
f3f40 2a 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20  * has changed.  
f3f50 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  If the transacti
f3f60 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f  on counter is mo
f3f70 64 69 66 69 65 64 2c 20 72 65 63 6f 72 64 20 74  dified, record t
f3f80 68 61 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f  hat.  ** fact to
f3f90 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46  o..  */.  if( pF
f3fa0 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69  ile->inNormalWri
f3fb0 74 65 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  te ){.    pFile-
f3fc0 3e 64 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20  >dbUpdate = 1;  
f3fd0 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
f3fe0 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  has been modifie
f3ff0 64 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 66 66  d */.    if( off
f4000 73 65 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65  set<=24 && offse
f4010 74 2b 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20  t+amt>=27 ){.   
f4020 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20     int rc;.     
f4030 20 63 68 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d   char oldCntr[4]
f4040 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65  ;.      Simulate
f4050 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29  IOErrorBenign(1)
f4060 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65 65  ;.      rc = see
f4070 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20  kAndRead(pFile, 
f4080 32 34 2c 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b  24, oldCntr, 4);
f4090 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49  .      SimulateI
f40a0 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b  OErrorBenign(0);
f40b0 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 34  .      if( rc!=4
f40c0 20 7c 7c 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e   || memcmp(oldCn
f40d0 74 72 2c 20 26 28 28 63 68 61 72 2a 29 70 42 75  tr, &((char*)pBu
f40e0 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34  f)[24-offset], 4
f40f0 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
f4100 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72  pFile->transCntr
f4110 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68  Chng = 1;  /* Th
f4120 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  e transaction co
f4130 75 6e 74 65 72 20 68 61 73 20 63 68 61 6e 67 65  unter has change
f4140 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  d */.      }.   
f4150 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
f4160 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 20 26 26   while( amt>0 &&
f4170 20 28 77 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e   (wrote = seekAn
f4180 64 57 72 69 74 65 28 70 46 69 6c 65 2c 20 6f 66  dWrite(pFile, of
f4190 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 29  fset, pBuf, amt)
f41a0 29 3e 30 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d  )>0 ){.    amt -
f41b0 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 6f 66 66  = wrote;.    off
f41c0 73 65 74 20 2b 3d 20 77 72 6f 74 65 3b 0a 20 20  set += wrote;.  
f41d0 20 20 70 42 75 66 20 3d 20 26 28 28 63 68 61 72    pBuf = &((char
f41e0 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a  *)pBuf)[wrote];.
f41f0 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f    }.  SimulateIO
f4200 45 72 72 6f 72 28 28 20 77 72 6f 74 65 3d 28 2d  Error(( wrote=(-
f4210 31 29 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20  1), amt=1 ));.  
f4220 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c  SimulateDiskfull
f4230 45 72 72 6f 72 28 28 20 77 72 6f 74 65 3d 30 2c  Error(( wrote=0,
f4240 20 61 6d 74 3d 31 20 29 29 3b 0a 0a 20 20 69 66   amt=1 ));..  if
f4250 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 69  ( amt>0 ){.    i
f4260 66 28 20 77 72 6f 74 65 3c 30 20 26 26 20 70 46  f( wrote<0 && pF
f4270 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 21 3d  ile->lastErrno!=
f4280 45 4e 4f 53 50 43 20 29 7b 0a 20 20 20 20 20 20  ENOSPC ){.      
f4290 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20 73 65 74  /* lastErrno set
f42a0 20 62 79 20 73 65 65 6b 41 6e 64 57 72 69 74 65   by seekAndWrite
f42b0 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
f42c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52   SQLITE_IOERR_WR
f42d0 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ITE;.    }else{.
f42e0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
f42f0 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e  tErrno = 0; /* n
f4300 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72 6f  ot a system erro
f4310 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72  r */.      retur
f4320 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20  n SQLITE_FULL;. 
f4330 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
f4340 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
f4350 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
f4360 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20  EST./*.** Count 
f4370 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75  the number of fu
f4380 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d  llsyncs and norm
f4390 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20  al syncs.  This 
f43a0 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a  is used to test.
f43b0 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e  ** that syncs an
f43c0 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20  d fullsyncs are 
f43d0 6f 63 63 75 72 72 69 6e 67 20 61 74 20 74 68 65  occurring at the
f43e0 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f   right times..*/
f43f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
f4400 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75  sqlite3_sync_cou
f4410 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41  nt = 0;.SQLITE_A
f4420 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
f4430 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20  ullsync_count = 
f4440 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
f4450 20 57 65 20 64 6f 20 6e 6f 74 20 74 72 75 73 74   We do not trust
f4460 20 73 79 73 74 65 6d 73 20 74 6f 20 70 72 6f 76   systems to prov
f4470 69 64 65 20 61 20 77 6f 72 6b 69 6e 67 20 66 64  ide a working fd
f4480 61 74 61 73 79 6e 63 28 29 2e 20 20 53 6f 6d 65  atasync().  Some
f4490 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73 20 64   do..** Others d
f44a0 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73 61 66  o no.  To be saf
f44b0 65 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 63 6b  e, we will stick
f44c0 20 77 69 74 68 20 74 68 65 20 28 73 6c 69 67 68   with the (sligh
f44d0 74 6c 79 20 73 6c 6f 77 65 72 29 0a 2a 2a 20 66  tly slower).** f
f44e0 73 79 6e 63 28 29 2e 20 49 66 20 79 6f 75 20 6b  sync(). If you k
f44f0 6e 6f 77 20 74 68 61 74 20 79 6f 75 72 20 73 79  now that your sy
f4500 73 74 65 6d 20 64 6f 65 73 20 73 75 70 70 6f 72  stem does suppor
f4510 74 20 66 64 61 74 61 73 79 6e 63 28 29 20 63 6f  t fdatasync() co
f4520 72 72 65 63 74 6c 79 2c 0a 2a 2a 20 74 68 65 6e  rrectly,.** then
f4530 20 73 69 6d 70 6c 79 20 63 6f 6d 70 69 6c 65 20   simply compile 
f4540 77 69 74 68 20 2d 44 66 64 61 74 61 73 79 6e 63  with -Dfdatasync
f4550 3d 66 64 61 74 61 73 79 6e 63 0a 2a 2f 0a 23 69  =fdatasync.*/.#i
f4560 66 20 21 64 65 66 69 6e 65 64 28 66 64 61 74 61  f !defined(fdata
f4570 73 79 6e 63 29 0a 23 20 64 65 66 69 6e 65 20 66  sync).# define f
f4580 64 61 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23  datasync fsync.#
f4590 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66  endif../*.** Def
f45a0 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59  ine HAVE_FULLFSY
f45b0 4e 43 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70  NC to 0 or 1 dep
f45c0 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65  ending on whethe
f45d0 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20  r or not.** the 
f45e0 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72  F_FULLFSYNC macr
f45f0 6f 20 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46  o is defined.  F
f4600 5f 46 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75  _FULLFSYNC is cu
f4610 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20  rrently.** only 
f4620 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63  available on Mac
f4630 20 4f 53 20 58 2e 20 20 42 75 74 20 74 68 61 74   OS X.  But that
f4640 20 63 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a   could change..*
f4650 2f 0a 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46  /.#ifdef F_FULLF
f4660 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41  SYNC.# define HA
f4670 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23  VE_FULLFSYNC 1.#
f4680 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41  else.# define HA
f4690 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23  VE_FULLFSYNC 0.#
f46a0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  endif.../*.** Th
f46b0 65 20 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d  e fsync() system
f46c0 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77   call does not w
f46d0 6f 72 6b 20 61 73 20 61 64 76 65 72 74 69 73 65  ork as advertise
f46e0 64 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69  d on many.** uni
f46f0 78 20 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20  x systems.  The 
f4700 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64  following proced
f4710 75 72 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70  ure is an attemp
f4720 74 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20  t to make.** it 
f4730 77 6f 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a  work better..**.
f4740 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f  ** The SQLITE_NO
f4750 5f 53 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61  _SYNC macro disa
f4760 62 6c 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29  bles all fsync()
f4770 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 66  s.  This is usef
f4780 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e  ul.** for testin
f4790 67 20 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74  g when we want t
f47a0 6f 20 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68  o run through th
f47b0 65 20 74 65 73 74 20 73 75 69 74 65 20 71 75 69  e test suite qui
f47c0 63 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65  ckly..** You are
f47d0 20 73 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65   strongly advise
f47e0 64 20 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f  d *not* to deplo
f47f0 79 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f  y with SQLITE_NO
f4800 5f 53 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64  _SYNC.** enabled
f4810 2c 20 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65  , however, since
f4820 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f   with SQLITE_NO_
f4830 53 59 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e  SYNC enabled, an
f4840 20 4f 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20   OS crash.** or 
f4850 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 69  power failure wi
f4860 6c 6c 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70  ll likely corrup
f4870 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  t the database f
f4880 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  ile..**.** SQLit
f4890 65 20 73 65 74 73 20 74 68 65 20 64 61 74 61 4f  e sets the dataO
f48a0 6e 6c 79 20 66 6c 61 67 20 69 66 20 74 68 65 20  nly flag if the 
f48b0 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
f48c0 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a   is unchanged..*
f48d0 2a 20 54 68 65 20 69 64 65 61 20 62 65 68 69 6e  * The idea behin
f48e0 64 20 64 61 74 61 4f 6e 6c 79 20 69 73 20 74 68  d dataOnly is th
f48f0 61 74 20 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c  at it should onl
f4900 79 20 77 72 69 74 65 20 74 68 65 20 66 69 6c 65  y write the file
f4910 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64   content.** to d
f4920 69 73 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f  isk, not the ino
f4930 64 65 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74  de.  We only set
f4940 20 64 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65   dataOnly if the
f4950 20 66 69 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a   file size is .*
f4960 2a 20 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63  * unchanged sinc
f4970 65 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20  e the file size 
f4980 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 69  is part of the i
f4990 6e 6f 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20  node.  However, 
f49a0 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c  .** Ted Ts'o tel
f49b0 6c 73 20 75 73 20 74 68 61 74 20 66 64 61 74 61  ls us that fdata
f49c0 73 79 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f  sync() will also
f49d0 20 77 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65   write the inode
f49e0 20 69 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20   if the.** file 
f49f0 73 69 7a 65 20 68 61 73 20 63 68 61 6e 67 65 64  size has changed
f4a00 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c  .  The only real
f4a10 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77   difference betw
f4a20 65 65 6e 20 66 64 61 74 61 73 79 6e 63 28 29 0a  een fdatasync().
f4a30 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28 29 2c 20  ** and fsync(), 
f4a40 54 65 64 20 74 65 6c 6c 73 20 75 73 2c 20 69 73  Ted tells us, is
f4a50 20 74 68 61 74 20 66 64 61 74 61 73 79 6e 63 28   that fdatasync(
f4a60 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68  ) will not flush
f4a70 20 74 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66   the.** inode if
f4a80 20 74 68 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77   the mtime or ow
f4a90 6e 65 72 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f  ner or other ino
f4aa0 64 65 20 61 74 74 72 69 62 75 74 65 73 20 68 61  de attributes ha
f4ab0 76 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57  ve changed..** W
f4ac0 65 20 6f 6e 6c 79 20 63 61 72 65 20 61 62 6f 75  e only care abou
f4ad0 74 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2c  t the file size,
f4ae0 20 6e 6f 74 20 74 68 65 20 6f 74 68 65 72 20 66   not the other f
f4af0 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 2c 20  ile attributes, 
f4b00 73 6f 0a 2a 2a 20 61 73 20 66 61 72 20 61 73 20  so.** as far as 
f4b10 53 51 4c 69 74 65 20 69 73 20 63 6f 6e 63 65 72  SQLite is concer
f4b20 6e 65 64 2c 20 61 6e 20 66 64 61 74 61 73 79 6e  ned, an fdatasyn
f4b30 63 28 29 20 69 73 20 61 6c 77 61 79 73 20 61 64  c() is always ad
f4b40 65 71 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77  equate..** So, w
f4b50 65 20 61 6c 77 61 79 73 20 75 73 65 20 66 64 61  e always use fda
f4b60 74 61 73 79 6e 63 28 29 20 69 66 20 69 74 20 69  tasync() if it i
f4b70 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 67  s available, reg
f4b80 61 72 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68  ardless of.** th
f4b90 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64  e value of the d
f4ba0 61 74 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f  ataOnly flag..*/
f4bb0 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c  .static int full
f4bc0 5f 66 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69  _fsync(int fd, i
f4bd0 6e 74 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74  nt fullSync, int
f4be0 20 64 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e   dataOnly){.  in
f4bf0 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  t rc;..  /* The 
f4c00 66 6f 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66  following "ifdef
f4c10 2f 65 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f  /elif/else/" blo
f4c20 63 6b 20 68 61 73 20 74 68 65 20 73 61 6d 65 20  ck has the same 
f4c30 73 74 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a  structure as.  *
f4c40 2a 20 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e  * the one below.
f4c50 20 49 74 20 69 73 20 72 65 70 6c 69 63 61 74 65   It is replicate
f4c60 64 20 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f  d here solely to
f4c70 20 61 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e   avoid clutterin
f4c80 67 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72  g .  ** up the r
f4c90 65 61 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68  eal code with th
f4ca0 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54  e UNUSED_PARAMET
f4cb0 45 52 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a  ER() macros..  *
f4cc0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
f4cd0 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44  NO_SYNC.  UNUSED
f4ce0 5f 50 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a  _PARAMETER(fd);.
f4cf0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
f4d00 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20  ER(fullSync);.  
f4d10 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
f4d20 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69  (dataOnly);.#eli
f4d30 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43  f HAVE_FULLFSYNC
f4d40 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
f4d50 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23  TER(dataOnly);.#
f4d60 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41  else.  UNUSED_PA
f4d70 52 41 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63  RAMETER(fullSync
f4d80 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
f4d90 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b  METER(dataOnly);
f4da0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65  .#endif..  /* Re
f4db0 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20  cord the number 
f4dc0 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 77 65  of times that we
f4dd0 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79   do a normal fsy
f4de0 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46  nc() and .  ** F
f4df0 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69  ULLSYNC.  This i
f4e00 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65  s used during te
f4e10 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20  sting to verify 
f4e20 74 68 61 74 20 74 68 69 73 20 70 72 6f 63 65 64  that this proced
f4e30 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61  ure.  ** gets ca
f4e40 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 6f  lled with the co
f4e50 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e  rrect arguments.
f4e60 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  .  */.#ifdef SQL
f4e70 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66  ITE_TEST.  if( f
f4e80 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65  ullSync ) sqlite
f4e90 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74  3_fullsync_count
f4ea0 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79  ++;.  sqlite3_sy
f4eb0 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64  nc_count++;.#end
f4ec0 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63  if..  /* If we c
f4ed0 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
f4ee0 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20   SQLITE_NO_SYNC 
f4ef0 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69  flag, then synci
f4f00 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d  ng is a.  ** no-
f4f10 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  op.  */.#ifdef S
f4f20 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20  QLITE_NO_SYNC.  
f4f30 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
f4f40 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46  #elif HAVE_FULLF
f4f50 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53  SYNC.  if( fullS
f4f60 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ync ){.    rc = 
f4f70 6f 73 46 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55  osFcntl(fd, F_FU
f4f80 4c 4c 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d  LLFSYNC, 0);.  }
f4f90 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31  else{.    rc = 1
f4fa0 3b 0a 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68  ;.  }.  /* If th
f4fb0 65 20 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c  e FULLFSYNC fail
f4fc0 65 64 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f  ed, fall back to
f4fd0 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66   attempting an f
f4fe0 73 79 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74 20  sync()..  ** It 
f4ff0 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73  shouldn't be pos
f5000 73 69 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73  sible for fullfs
f5010 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74  ync to fail on t
f5020 68 65 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 66  he local .  ** f
f5030 69 6c 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f  ile system (on O
f5040 53 58 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20  SX), so failure 
f5050 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 46  indicates that F
f5060 55 4c 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69 73  ULLFSYNC.  ** is
f5070 6e 27 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f  n't supported fo
f5080 72 20 74 68 69 73 20 66 69 6c 65 20 73 79 73 74  r this file syst
f5090 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20  em. So, attempt 
f50a0 61 6e 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20 61  an fsync .  ** a
f50b0 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e  nd (for now) ign
f50c0 6f 72 65 20 74 68 65 20 6f 76 65 72 68 65 61 64  ore the overhead
f50d0 20 6f 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75   of a superfluou
f50e0 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a  s fcntl call.  .
f50f0 20 20 2a 2a 20 49 74 27 64 20 62 65 20 62 65 74    ** It'd be bet
f5100 74 65 72 20 74 6f 20 64 65 74 65 63 74 20 66 75  ter to detect fu
f5110 6c 6c 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20  llfsync support 
f5120 6f 6e 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a  once and avoid .
f5130 20 20 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20 63    ** the fcntl c
f5140 61 6c 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73  all every time s
f5150 79 6e 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20  ync is called.. 
f5160 20 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72   */.  if( rc ) r
f5170 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a  c = fsync(fd);..
f5180 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 5f  #elif defined(__
f5190 41 50 50 4c 45 5f 5f 29 0a 20 20 2f 2a 20 66 64  APPLE__).  /* fd
f51a0 61 74 61 73 79 6e 63 28 29 20 6f 6e 20 48 46 53  atasync() on HFS
f51b0 2b 20 64 6f 65 73 6e 27 74 20 79 65 74 20 66 6c  + doesn't yet fl
f51c0 75 73 68 20 74 68 65 20 66 69 6c 65 20 73 69 7a  ush the file siz
f51d0 65 20 69 66 20 69 74 20 63 68 61 6e 67 65 64 20  e if it changed 
f51e0 63 6f 72 72 65 63 74 6c 79 0a 20 20 2a 2a 20 73  correctly.  ** s
f51f0 6f 20 63 75 72 72 65 6e 74 6c 79 20 77 65 20 64  o currently we d
f5200 65 66 61 75 6c 74 20 74 6f 20 74 68 65 20 6d 61  efault to the ma
f5210 63 72 6f 20 74 68 61 74 20 72 65 64 65 66 69 6e  cro that redefin
f5220 65 73 20 66 64 61 74 61 73 79 6e 63 20 74 6f 20  es fdatasync to 
f5230 66 73 79 6e 63 0a 20 20 2a 2f 0a 20 20 72 63 20  fsync.  */.  rc 
f5240 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 23 65 6c  = fsync(fd);.#el
f5250 73 65 20 0a 20 20 72 63 20 3d 20 66 64 61 74 61  se .  rc = fdata
f5260 73 79 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f 53  sync(fd);.#if OS
f5270 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72  _VXWORKS.  if( r
f5280 63 3d 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d 3d  c==-1 && errno==
f5290 45 4e 4f 54 53 55 50 20 29 7b 0a 20 20 20 20 72  ENOTSUP ){.    r
f52a0 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20  c = fsync(fd);. 
f52b0 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f   }.#endif /* OS_
f52c0 56 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64 69  VXWORKS */.#endi
f52d0 66 20 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 54  f /* ifdef SQLIT
f52e0 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20 48  E_NO_SYNC elif H
f52f0 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a 2f  AVE_FULLFSYNC */
f5300 0a 0a 20 20 69 66 28 20 4f 53 5f 56 58 57 4f 52  ..  if( OS_VXWOR
f5310 4b 53 20 26 26 20 72 63 21 3d 20 2d 31 20 29 7b  KS && rc!= -1 ){
f5320 0a 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 7d  .    rc = 0;.  }
f5330 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
f5340 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69  ./*.** Open a fi
f5350 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f  le descriptor to
f5360 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 63   the directory c
f5370 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c 65 20 7a  ontaining file z
f5380 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 49 66 20  Filename..** If 
f5390 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 46 64  successful, *pFd
f53a0 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6f   is set to the o
f53b0 70 65 6e 65 64 20 66 69 6c 65 20 64 65 73 63 72  pened file descr
f53c0 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 53 51 4c  iptor and.** SQL
f53d0 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
f53e0 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  ed. If an error 
f53f0 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 20 53  occurs, either S
f5400 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 6f  QLITE_NOMEM.** o
f5410 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  r SQLITE_CANTOPE
f5420 4e 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  N is returned an
f5430 64 20 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f  d *pFd is set to
f5440 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a   an undefined.**
f5450 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   value..**.** Th
f5460 65 20 64 69 72 65 63 74 6f 72 79 20 66 69 6c 65  e directory file
f5470 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 75   descriptor is u
f5480 73 65 64 20 66 6f 72 20 6f 6e 6c 79 20 6f 6e 65  sed for only one
f5490 20 74 68 69 6e 67 20 2d 20 74 6f 0a 2a 2a 20 66   thing - to.** f
f54a0 73 79 6e 63 28 29 20 61 20 64 69 72 65 63 74 6f  sync() a directo
f54b0 72 79 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ry to make sure 
f54c0 66 69 6c 65 20 63 72 65 61 74 69 6f 6e 20 61 6e  file creation an
f54d0 64 20 64 65 6c 65 74 69 6f 6e 20 65 76 65 6e 74  d deletion event
f54e0 73 0a 2a 2a 20 61 72 65 20 66 6c 75 73 68 65 64  s.** are flushed
f54f0 20 74 6f 20 64 69 73 6b 2e 20 20 53 75 63 68 20   to disk.  Such 
f5500 66 73 79 6e 63 73 20 61 72 65 20 6e 6f 74 20 6e  fsyncs are not n
f5510 65 65 64 65 64 20 6f 6e 20 6e 65 77 65 72 0a 2a  eeded on newer.*
f5520 2a 20 6a 6f 75 72 6e 61 6c 69 6e 67 20 66 69 6c  * journaling fil
f5530 65 73 79 73 74 65 6d 73 2c 20 62 75 74 20 61 72  esystems, but ar
f5540 65 20 72 65 71 75 69 72 65 64 20 6f 6e 20 6f 6c  e required on ol
f5550 64 65 72 20 66 69 6c 65 73 79 73 74 65 6d 73 2e  der filesystems.
f5560 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
f5570 69 6e 65 20 63 61 6e 20 62 65 20 6f 76 65 72 72  ine can be overr
f5580 69 64 64 65 6e 20 75 73 69 6e 67 20 74 68 65 20  idden using the 
f5590 78 53 65 74 53 79 73 43 61 6c 6c 20 69 6e 74 65  xSetSysCall inte
f55a0 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 61 62  rface..** The ab
f55b0 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 72 69 64  ility to overrid
f55c0 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  e this routine w
f55d0 61 73 20 61 64 64 65 64 20 69 6e 20 73 75 70 70  as added in supp
f55e0 6f 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 63 68  ort of the.** ch
f55f0 72 6f 6d 69 75 6d 20 73 61 6e 64 62 6f 78 2e 20  romium sandbox. 
f5600 20 4f 70 65 6e 69 6e 67 20 61 20 64 69 72 65 63   Opening a direc
f5610 74 6f 72 79 20 69 73 20 61 20 73 65 63 75 72 69  tory is a securi
f5620 74 79 20 72 69 73 6b 20 28 77 65 20 61 72 65 0a  ty risk (we are.
f5630 2a 2a 20 74 6f 6c 64 29 20 73 6f 20 6d 61 6b 69  ** told) so maki
f5640 6e 67 20 69 74 20 6f 76 65 72 72 69 64 65 61 62  ng it overrideab
f5650 6c 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 63 68  le allows the ch
f5660 72 6f 6d 69 75 6d 20 73 61 6e 64 62 6f 78 20 74  romium sandbox t
f5670 6f 0a 2a 2a 20 72 65 70 6c 61 63 65 20 74 68 69  o.** replace thi
f5680 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 61  s routine with a
f5690 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e   harmless no-op.
f56a0 20 20 54 6f 20 6d 61 6b 65 20 74 68 69 73 20 72    To make this r
f56b0 6f 75 74 69 6e 65 0a 2a 2a 20 61 20 6e 6f 2d 6f  outine.** a no-o
f56c0 70 2c 20 72 65 70 6c 61 63 65 20 69 74 20 77 69  p, replace it wi
f56d0 74 68 20 61 20 73 74 75 62 20 74 68 61 74 20 72  th a stub that r
f56e0 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
f56f0 20 62 75 74 20 6c 65 61 76 65 73 0a 2a 2a 20 2a   but leaves.** *
f5700 70 46 64 20 73 65 74 20 74 6f 20 61 20 6e 65 67  pFd set to a neg
f5710 61 74 69 76 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a  ative number..**
f5720 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b  .** If SQLITE_OK
f5730 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68   is returned, th
f5740 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70  e caller is resp
f5750 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73  onsible for clos
f5760 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20  ing.** the file 
f5770 64 65 73 63 72 69 70 74 6f 72 20 2a 70 46 64 20  descriptor *pFd 
f5780 75 73 69 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a  using close()..*
f5790 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65  /.static int ope
f57a0 6e 44 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74  nDirectory(const
f57b0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
f57c0 2c 20 69 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69  , int *pFd){.  i
f57d0 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 66 64 20  nt ii;.  int fd 
f57e0 3d 20 2d 31 3b 0a 20 20 63 68 61 72 20 7a 44 69  = -1;.  char zDi
f57f0 72 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41  rname[MAX_PATHNA
f5800 4d 45 2b 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65  ME+1];..  sqlite
f5810 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50  3_snprintf(MAX_P
f5820 41 54 48 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d  ATHNAME, zDirnam
f5830 65 2c 20 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61  e, "%s", zFilena
f5840 6d 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d 28 69  me);.  for(ii=(i
f5850 6e 74 29 73 74 72 6c 65 6e 28 7a 44 69 72 6e 61  nt)strlen(zDirna
f5860 6d 65 29 3b 20 69 69 3e 31 20 26 26 20 7a 44 69  me); ii>1 && zDi
f5870 72 6e 61 6d 65 5b 69 69 5d 21 3d 27 2f 27 3b 20  rname[ii]!='/'; 
f5880 69 69 2d 2d 29 3b 0a 20 20 69 66 28 20 69 69 3e  ii--);.  if( ii>
f5890 30 20 29 7b 0a 20 20 20 20 7a 44 69 72 6e 61 6d  0 ){.    zDirnam
f58a0 65 5b 69 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  e[ii] = '\0';.  
f58b0 20 20 66 64 20 3d 20 72 6f 62 75 73 74 5f 6f 70    fd = robust_op
f58c0 65 6e 28 7a 44 69 72 6e 61 6d 65 2c 20 4f 5f 52  en(zDirname, O_R
f58d0 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41 52 59 2c 20  DONLY|O_BINARY, 
f58e0 30 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3e 3d  0);.    if( fd>=
f58f0 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 54 52 41  0 ){.      OSTRA
f5900 43 45 28 28 22 4f 50 45 4e 44 49 52 20 25 2d 33  CE(("OPENDIR %-3
f5910 64 20 25 73 5c 6e 22 2c 20 66 64 2c 20 7a 44 69  d %s\n", fd, zDi
f5920 72 6e 61 6d 65 29 29 3b 0a 20 20 20 20 7d 0a 20  rname));.    }. 
f5930 20 7d 0a 20 20 2a 70 46 64 20 3d 20 66 64 3b 0a   }.  *pFd = fd;.
f5940 20 20 72 65 74 75 72 6e 20 28 66 64 3e 3d 30 3f    return (fd>=0?
f5950 53 51 4c 49 54 45 5f 4f 4b 3a 75 6e 69 78 4c 6f  SQLITE_OK:unixLo
f5960 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 43 41  gError(SQLITE_CA
f5970 4e 54 4f 50 45 4e 5f 42 4b 50 54 2c 20 22 6f 70  NTOPEN_BKPT, "op
f5980 65 6e 22 2c 20 7a 44 69 72 6e 61 6d 65 29 29 3b  en", zDirname));
f5990 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  .}../*.** Make s
f59a0 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74  ure all writes t
f59b0 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66  o a particular f
f59c0 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65  ile are committe
f59d0 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a  d to disk..**.**
f59e0 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20   If dataOnly==0 
f59f0 74 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66 69  then both the fi
f5a00 6c 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69 74  le itself and it
f5a10 73 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c 65  s metadata (file
f5a20 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 73  .** size, access
f5a30 20 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65 20   time, etc) are 
f5a40 73 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74 61  synced.  If data
f5a50 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c  Only!=0 then onl
f5a60 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61  y the.** file da
f5a70 74 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a  ta is synced..**
f5a80 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c 20  .** Under Unix, 
f5a90 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  also make sure t
f5aa0 68 61 74 20 74 68 65 20 64 69 72 65 63 74 6f 72  hat the director
f5ab0 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20  y entry for the 
f5ac0 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e  file.** has been
f5ad0 20 63 72 65 61 74 65 64 20 62 79 20 66 73 79 6e   created by fsyn
f5ae0 63 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63 74  c-ing the direct
f5af0 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ory that contain
f5b00 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49  s the file..** I
f5b10 66 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74  f we do not do t
f5b20 68 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f 75  his and we encou
f5b30 6e 74 65 72 20 61 20 70 6f 77 65 72 20 66 61 69  nter a power fai
f5b40 6c 75 72 65 2c 20 74 68 65 20 64 69 72 65 63 74  lure, the direct
f5b50 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72  ory.** entry for
f5b60 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67   the journal mig
f5b70 68 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66 74  ht not exist aft
f5b80 65 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 54  er we reboot.  T
f5b90 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74  he next.** SQLit
f5ba0 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20  e to access the 
f5bb0 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e  file will not kn
f5bc0 6f 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72  ow that the jour
f5bd0 6e 61 6c 20 65 78 69 73 74 73 20 28 62 65 63 61  nal exists (beca
f5be0 75 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 63  use.** the direc
f5bf0 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74  tory entry for t
f5c00 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e  he journal was n
f5c10 65 76 65 72 20 63 72 65 61 74 65 64 29 20 61 6e  ever created) an
f5c20 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  d the transactio
f5c30 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f  n.** will not ro
f5c40 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 62  ll back - possib
f5c50 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 61  ly leading to da
f5c60 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f  tabase corruptio
f5c70 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
f5c80 20 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 65   unixSync(sqlite
f5c90 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
f5ca0 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63  flags){.  int rc
f5cb0 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  ;.  unixFile *pF
f5cc0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
f5cd0 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 61  )id;..  int isDa
f5ce0 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 26  taOnly = (flags&
f5cf0 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41  SQLITE_SYNC_DATA
f5d00 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 46  ONLY);.  int isF
f5d10 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 73  ullsync = (flags
f5d20 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53  &0x0F)==SQLITE_S
f5d30 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20  YNC_FULL;..  /* 
f5d40 43 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 6f  Check that one o
f5d50 66 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f  f SQLITE_SYNC_NO
f5d60 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 73  RMAL or FULL was
f5d70 20 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 73   passed */.  ass
f5d80 65 72 74 28 28 66 6c 61 67 73 26 30 78 30 46 29  ert((flags&0x0F)
f5d90 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f  ==SQLITE_SYNC_NO
f5da0 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 66  RMAL.      || (f
f5db0 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49  lags&0x0F)==SQLI
f5dc0 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 29  TE_SYNC_FULL.  )
f5dd0 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e  ;..  /* Unix can
f5de0 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 79  not, but some sy
f5df0 73 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72 6e  stems may return
f5e00 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f   SQLITE_FULL fro
f5e10 6d 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20 2a  m here. This.  *
f5e20 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 73  * line is to tes
f5e30 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f 20  t that doing so 
f5e40 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20 61  does not cause a
f5e50 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a  ny problems..  *
f5e60 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b  /.  SimulateDisk
f5e70 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72  fullError( retur
f5e80 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b  n SQLITE_FULL );
f5e90 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ..  assert( pFil
f5ea0 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 28 28  e );.  OSTRACE((
f5eb0 22 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22  "SYNC    %-3d\n"
f5ec0 2c 20 70 46 69 6c 65 2d 3e 68 29 29 3b 0a 20 20  , pFile->h));.  
f5ed0 72 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28  rc = full_fsync(
f5ee0 70 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c  pFile->h, isFull
f5ef0 73 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79  sync, isDataOnly
f5f00 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
f5f10 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20  rror( rc=1 );.  
f5f20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 46  if( rc ){.    pF
f5f30 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
f5f40 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75   errno;.    retu
f5f50 72 6e 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28  rn unixLogError(
f5f60 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59  SQLITE_IOERR_FSY
f5f70 4e 43 2c 20 22 66 75 6c 6c 5f 66 73 79 6e 63 22  NC, "full_fsync"
f5f80 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74 68 29 3b  , pFile->zPath);
f5f90 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 73 6f 20  .  }..  /* Also 
f5fa0 66 73 79 6e 63 20 74 68 65 20 64 69 72 65 63 74  fsync the direct
f5fb0 6f 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ory containing t
f5fc0 68 65 20 66 69 6c 65 20 69 66 20 74 68 65 20 44  he file if the D
f5fd0 49 52 53 59 4e 43 20 66 6c 61 67 0a 20 20 2a 2a  IRSYNC flag.  **
f5fe0 20 69 73 20 73 65 74 2e 20 20 54 68 69 73 20 69   is set.  This i
f5ff0 73 20 61 20 6f 6e 65 2d 74 69 6d 65 20 6f 63 63  s a one-time occ
f6000 75 72 72 61 6e 63 65 2e 20 20 4d 61 6e 79 20 73  urrance.  Many s
f6010 79 73 74 65 6d 73 20 28 65 78 61 6d 70 6c 65 73  ystems (examples
f6020 3a 20 41 49 58 29 0a 20 20 2a 2a 20 61 72 65 20  : AIX).  ** are 
f6030 75 6e 61 62 6c 65 20 74 6f 20 66 73 79 6e 63 20  unable to fsync 
f6040 61 20 64 69 72 65 63 74 6f 72 79 2c 20 73 6f 20  a directory, so 
f6050 69 67 6e 6f 72 65 20 65 72 72 6f 72 73 20 6f 6e  ignore errors on
f6060 20 74 68 65 20 66 73 79 6e 63 2e 0a 20 20 2a 2f   the fsync..  */
f6070 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 63 74  .  if( pFile->ct
f6080 72 6c 46 6c 61 67 73 20 26 20 55 4e 49 58 46 49  rlFlags & UNIXFI
f6090 4c 45 5f 44 49 52 53 59 4e 43 20 29 7b 0a 20 20  LE_DIRSYNC ){.  
f60a0 20 20 69 6e 74 20 64 69 72 66 64 3b 0a 20 20 20    int dirfd;.   
f60b0 20 4f 53 54 52 41 43 45 28 28 22 44 49 52 53 59   OSTRACE(("DIRSY
f60c0 4e 43 20 25 73 20 28 68 61 76 65 5f 66 75 6c 6c  NC %s (have_full
f60d0 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73 79 6e  fsync=%d fullsyn
f60e0 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  c=%d)\n", pFile-
f60f0 3e 7a 50 61 74 68 2c 0a 20 20 20 20 20 20 20 20  >zPath,.        
f6100 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46 53 59      HAVE_FULLFSY
f6110 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63 29 29  NC, isFullsync))
f6120 3b 0a 20 20 20 20 72 63 20 3d 20 6f 73 4f 70 65  ;.    rc = osOpe
f6130 6e 44 69 72 65 63 74 6f 72 79 28 70 46 69 6c 65  nDirectory(pFile
f6140 2d 3e 7a 50 61 74 68 2c 20 26 64 69 72 66 64 29  ->zPath, &dirfd)
f6150 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
f6160 4c 49 54 45 5f 4f 4b 20 26 26 20 64 69 72 66 64  LITE_OK && dirfd
f6170 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 75 6c  >=0 ){.      ful
f6180 6c 5f 66 73 79 6e 63 28 64 69 72 66 64 2c 20 30  l_fsync(dirfd, 0
f6190 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 6f 62 75  , 0);.      robu
f61a0 73 74 5f 63 6c 6f 73 65 28 70 46 69 6c 65 2c 20  st_close(pFile, 
f61b0 64 69 72 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29  dirfd, __LINE__)
f61c0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
f61d0 72 63 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f  rc==SQLITE_CANTO
f61e0 50 45 4e 20 29 7b 0a 20 20 20 20 20 20 72 63 20  PEN ){.      rc 
f61f0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
f6200 20 7d 0a 20 20 20 20 70 46 69 6c 65 2d 3e 63 74   }.    pFile->ct
f6210 72 6c 46 6c 61 67 73 20 26 3d 20 7e 55 4e 49 58  rlFlags &= ~UNIX
f6220 46 49 4c 45 5f 44 49 52 53 59 4e 43 3b 0a 20 20  FILE_DIRSYNC;.  
f6230 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
f6240 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65  ../*.** Truncate
f6250 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f   an open file to
f6260 20 61 20 73 70 65 63 69 66 69 65 64 20 73 69 7a   a specified siz
f6270 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  e.*/.static int 
f6280 75 6e 69 78 54 72 75 6e 63 61 74 65 28 73 71 6c  unixTruncate(sql
f6290 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
f62a0 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 75 6e 69  64 nByte){.  uni
f62b0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
f62c0 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20  unixFile *)id;. 
f62d0 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72   int rc;.  asser
f62e0 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 53 69  t( pFile );.  Si
f62f0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
f6300 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
f6310 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 0a  RR_TRUNCATE );..
f6320 20 20 2f 2a 20 49 66 20 74 68 65 20 75 73 65 72    /* If the user
f6330 20 68 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20   has configured 
f6340 61 20 63 68 75 6e 6b 2d 73 69 7a 65 20 66 6f 72  a chunk-size for
f6350 20 74 68 69 73 20 66 69 6c 65 2c 20 74 72 75 6e   this file, trun
f6360 63 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 66 69  cate the.  ** fi
f6370 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 6f  le so that it co
f6380 6e 73 69 73 74 73 20 6f 66 20 61 6e 20 69 6e 74  nsists of an int
f6390 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 63  eger number of c
f63a0 68 75 6e 6b 73 20 28 69 2e 65 2e 20 74 68 65 0a  hunks (i.e. the.
f63b0 20 20 2a 2a 20 61 63 74 75 61 6c 20 66 69 6c 65    ** actual file
f63c0 20 73 69 7a 65 20 61 66 74 65 72 20 74 68 65 20   size after the 
f63d0 6f 70 65 72 61 74 69 6f 6e 20 6d 61 79 20 62 65  operation may be
f63e0 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
f63f0 20 72 65 71 75 65 73 74 65 64 0a 20 20 2a 2a 20   requested.  ** 
f6400 73 69 7a 65 29 2e 0a 20 20 2a 2f 0a 20 20 69 66  size)..  */.  if
f6410 28 20 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b  ( pFile->szChunk
f6420 3e 30 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20  >0 ){.    nByte 
f6430 3d 20 28 28 6e 42 79 74 65 20 2b 20 70 46 69 6c  = ((nByte + pFil
f6440 65 2d 3e 73 7a 43 68 75 6e 6b 20 2d 20 31 29 2f  e->szChunk - 1)/
f6450 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 29 20  pFile->szChunk) 
f6460 2a 20 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b  * pFile->szChunk
f6470 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 72 6f  ;.  }..  rc = ro
f6480 62 75 73 74 5f 66 74 72 75 6e 63 61 74 65 28 70  bust_ftruncate(p
f6490 46 69 6c 65 2d 3e 68 2c 20 28 6f 66 66 5f 74 29  File->h, (off_t)
f64a0 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 72 63  nByte);.  if( rc
f64b0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
f64c0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
f64d0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 75 6e 69  ;.    return uni
f64e0 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45  xLogError(SQLITE
f64f0 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 2c  _IOERR_TRUNCATE,
f6500 20 22 66 74 72 75 6e 63 61 74 65 22 2c 20 70 46   "ftruncate", pF
f6510 69 6c 65 2d 3e 7a 50 61 74 68 29 3b 0a 20 20 7d  ile->zPath);.  }
f6520 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 53 51 4c  else{.#ifdef SQL
f6530 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 2f 2a  ITE_DEBUG.    /*
f6540 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67   If we are doing
f6550 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20   a normal write 
f6560 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69  to a database fi
f6570 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74  le (as opposed t
f6580 6f 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 61  o.    ** doing a
f6590 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c   hot-journal rol
f65a0 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65  lback or a write
f65b0 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74   to some file ot
f65c0 68 65 72 20 74 68 61 6e 20 61 0a 20 20 20 20 2a  her than a.    *
f65d0 2a 20 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61 73  * normal databas
f65e0 65 20 66 69 6c 65 29 20 61 6e 64 20 77 65 20 74  e file) and we t
f65f0 72 75 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65  runcate the file
f6600 20 74 6f 20 7a 65 72 6f 20 6c 65 6e 67 74 68 2c   to zero length,
f6610 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 65 66 66  .    ** that eff
f6620 65 63 74 69 76 65 6c 79 20 75 70 64 61 74 65 73  ectively updates
f6630 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e   the change coun
f6640 74 65 72 2e 20 20 54 68 69 73 20 6d 69 67 68 74  ter.  This might
f6650 20 68 61 70 70 65 6e 0a 20 20 20 20 2a 2a 20 77   happen.    ** w
f6660 68 65 6e 20 72 65 73 74 6f 72 69 6e 67 20 61 20  hen restoring a 
f6670 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 74  database using t
f6680 68 65 20 62 61 63 6b 75 70 20 41 50 49 20 66 72  he backup API fr
f6690 6f 6d 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68  om a zero-length
f66a0 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63 65 2e 0a  .    ** source..
f66b0 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
f66c0 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72  File->inNormalWr
f66d0 69 74 65 20 26 26 20 6e 42 79 74 65 3d 3d 30 20  ite && nByte==0 
f66e0 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
f66f0 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 20 3d 20  transCntrChng = 
f6700 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  1;.    }.#endif.
f6710 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
f6720 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  TE_OK;.  }.}../*
f6730 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68  .** Determine th
f6740 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f  e current size o
f6750 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65  f a file in byte
f6760 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  s.*/.static int 
f6770 75 6e 69 78 46 69 6c 65 53 69 7a 65 28 73 71 6c  unixFileSize(sql
f6780 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
f6790 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e  64 *pSize){.  in
f67a0 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20 73  t rc;.  struct s
f67b0 74 61 74 20 62 75 66 3b 0a 20 20 61 73 73 65 72  tat buf;.  asser
f67c0 74 28 20 69 64 20 29 3b 0a 20 20 72 63 20 3d 20  t( id );.  rc = 
f67d0 6f 73 46 73 74 61 74 28 28 28 75 6e 69 78 46 69  osFstat(((unixFi
f67e0 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 26 62 75 66  le*)id)->h, &buf
f67f0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
f6800 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20  rror( rc=1 );.  
f6810 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20  if( rc!=0 ){.   
f6820 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
f6830 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
f6840 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  rno;.    return 
f6850 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54  SQLITE_IOERR_FST
f6860 41 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65  AT;.  }.  *pSize
f6870 20 3d 20 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a   = buf.st_size;.
f6880 0a 20 20 2f 2a 20 57 68 65 6e 20 6f 70 65 6e 69  .  /* When openi
f6890 6e 67 20 61 20 7a 65 72 6f 2d 73 69 7a 65 20 64  ng a zero-size d
f68a0 61 74 61 62 61 73 65 2c 20 74 68 65 20 66 69 6e  atabase, the fin
f68b0 64 49 6e 6f 64 65 49 6e 66 6f 28 29 20 70 72 6f  dInodeInfo() pro
f68c0 63 65 64 75 72 65 0a 20 20 2a 2a 20 77 72 69 74  cedure.  ** writ
f68d0 65 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65  es a single byte
f68e0 20 69 6e 74 6f 20 74 68 61 74 20 66 69 6c 65 20   into that file 
f68f0 69 6e 20 6f 72 64 65 72 20 74 6f 20 77 6f 72 6b  in order to work
f6900 20 61 72 6f 75 6e 64 20 61 20 62 75 67 0a 20 20   around a bug.  
f6910 2a 2a 20 69 6e 20 74 68 65 20 4f 53 2d 58 20 6d  ** in the OS-X m
f6920 73 64 6f 73 20 66 69 6c 65 73 79 73 74 65 6d 2e  sdos filesystem.
f6930 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 61 76    In order to av
f6940 6f 69 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 74  oid problems wit
f6950 68 20 75 70 70 65 72 0a 20 20 2a 2a 20 6c 61 79  h upper.  ** lay
f6960 65 72 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20  ers, we need to 
f6970 72 65 70 6f 72 74 20 74 68 69 73 20 66 69 6c 65  report this file
f6980 20 73 69 7a 65 20 61 73 20 7a 65 72 6f 20 65 76   size as zero ev
f6990 65 6e 20 74 68 6f 75 67 68 20 69 74 20 69 73 0a  en though it is.
f69a0 20 20 2a 2a 20 72 65 61 6c 6c 79 20 31 2e 20 20    ** really 1.  
f69b0 20 54 69 63 6b 65 74 20 23 33 32 36 30 2e 0a 20   Ticket #3260.. 
f69c0 20 2a 2f 0a 20 20 69 66 28 20 2a 70 53 69 7a 65   */.  if( *pSize
f69d0 3d 3d 31 20 29 20 2a 70 53 69 7a 65 20 3d 20 30  ==1 ) *pSize = 0
f69e0 3b 0a 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ;...  return SQL
f69f0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53  ITE_OK;.}..#if S
f6a00 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
f6a10 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65  KING_STYLE && de
f6a20 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
f6a30 0a 2f 2a 0a 2a 2a 20 48 61 6e 64 6c 65 72 20 66  ./*.** Handler f
f6a40 6f 72 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67  or proxy-locking
f6a50 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 76 65   file-control ve
f6a60 72 62 73 2e 20 20 44 65 66 69 6e 65 64 20 62 65  rbs.  Defined be
f6a70 6c 6f 77 20 69 6e 20 74 68 65 0a 2a 2a 20 70 72  low in the.** pr
f6a80 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 64  oxying locking d
f6a90 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  ivision..*/.stat
f6aa0 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69 6c 65  ic int proxyFile
f6ab0 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f  Control(sqlite3_
f6ac0 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29  file*,int,void*)
f6ad0 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a  ;.#endif../* .**
f6ae0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
f6af0 73 20 63 61 6c 6c 65 64 20 74 6f 20 68 61 6e 64  s called to hand
f6b00 6c 65 20 74 68 65 20 53 51 4c 49 54 45 5f 46 43  le the SQLITE_FC
f6b10 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e 54 20 0a 2a  NTL_SIZE_HINT .*
f6b20 2a 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 6f  * file-control o
f6b30 70 65 72 61 74 69 6f 6e 2e 20 20 45 6e 6c 61 72  peration.  Enlar
f6b40 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ge the database 
f6b50 74 6f 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a  to nBytes in siz
f6b60 65 0a 2a 2a 20 28 72 6f 75 6e 64 65 64 20 75 70  e.** (rounded up
f6b70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 63 68 75   to the next chu
f6b80 6e 6b 2d 73 69 7a 65 29 2e 20 20 49 66 20 74 68  nk-size).  If th
f6b90 65 20 64 61 74 61 62 61 73 65 20 69 73 20 61 6c  e database is al
f6ba0 72 65 61 64 79 0a 2a 2a 20 6e 42 79 74 65 73 20  ready.** nBytes 
f6bb0 6f 72 20 6c 61 72 67 65 72 2c 20 74 68 69 73 20  or larger, this 
f6bc0 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
f6bd0 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  op..*/.static in
f6be0 74 20 66 63 6e 74 6c 53 69 7a 65 48 69 6e 74 28  t fcntlSizeHint(
f6bf0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c  unixFile *pFile,
f6c00 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 69   i64 nByte){.  i
f6c10 66 28 20 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e  f( pFile->szChun
f6c20 6b 3e 30 20 29 7b 0a 20 20 20 20 69 36 34 20 6e  k>0 ){.    i64 n
f6c30 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
f6c40 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 71 75           /* Requ
f6c50 69 72 65 64 20 66 69 6c 65 20 73 69 7a 65 20 2a  ired file size *
f6c60 2f 0a 20 20 20 20 73 74 72 75 63 74 20 73 74 61  /.    struct sta
f6c70 74 20 62 75 66 3b 20 20 20 20 20 20 20 20 20 20  t buf;          
f6c80 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 68      /* Used to h
f6c90 6f 6c 64 20 72 65 74 75 72 6e 20 76 61 6c 75 65  old return value
f6ca0 73 20 6f 66 20 66 73 74 61 74 28 29 20 2a 2f 0a  s of fstat() */.
f6cb0 20 20 20 0a 20 20 20 20 69 66 28 20 6f 73 46 73     .    if( osFs
f6cc0 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26 62  tat(pFile->h, &b
f6cd0 75 66 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c  uf) ) return SQL
f6ce0 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b  ITE_IOERR_FSTAT;
f6cf0 0a 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 28 28  ..    nSize = ((
f6d00 6e 42 79 74 65 2b 70 46 69 6c 65 2d 3e 73 7a 43  nByte+pFile->szC
f6d10 68 75 6e 6b 2d 31 29 20 2f 20 70 46 69 6c 65 2d  hunk-1) / pFile-
f6d20 3e 73 7a 43 68 75 6e 6b 29 20 2a 20 70 46 69 6c  >szChunk) * pFil
f6d30 65 2d 3e 73 7a 43 68 75 6e 6b 3b 0a 20 20 20 20  e->szChunk;.    
f6d40 69 66 28 20 6e 53 69 7a 65 3e 28 69 36 34 29 62  if( nSize>(i64)b
f6d50 75 66 2e 73 74 5f 73 69 7a 65 20 29 7b 0a 0a 23  uf.st_size ){..#
f6d60 69 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f  if defined(HAVE_
f6d70 50 4f 53 49 58 5f 46 41 4c 4c 4f 43 41 54 45 29  POSIX_FALLOCATE)
f6d80 20 26 26 20 48 41 56 45 5f 50 4f 53 49 58 5f 46   && HAVE_POSIX_F
f6d90 41 4c 4c 4f 43 41 54 45 0a 20 20 20 20 20 20 2f  ALLOCATE.      /
f6da0 2a 20 54 68 65 20 63 6f 64 65 20 62 65 6c 6f 77  * The code below
f6db0 20 69 73 20 68 61 6e 64 6c 69 6e 67 20 74 68 65   is handling the
f6dc0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
f6dd0 20 6f 73 46 61 6c 6c 6f 63 61 74 65 28 29 20 0a   osFallocate() .
f6de0 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 65 63 74        ** correct
f6df0 6c 79 2e 20 70 6f 73 69 78 5f 66 61 6c 6c 6f 63  ly. posix_falloc
f6e00 61 74 65 28 29 20 69 73 20 64 65 66 69 6e 65 64  ate() is defined
f6e10 20 74 6f 20 22 72 65 74 75 72 6e 73 20 7a 65 72   to "returns zer
f6e20 6f 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 0a 20  o on success, . 
f6e30 20 20 20 20 20 2a 2a 20 6f 72 20 61 6e 20 65 72       ** or an er
f6e40 72 6f 72 20 6e 75 6d 62 65 72 20 6f 6e 20 20 66  ror number on  f
f6e50 61 69 6c 75 72 65 22 2e 20 53 65 65 20 74 68 65  ailure". See the
f6e60 20 6d 61 6e 70 61 67 65 20 66 6f 72 20 64 65 74   manpage for det
f6e70 61 69 6c 73 2e 20 2a 2f 0a 20 20 20 20 20 20 69  ails. */.      i
f6e80 6e 74 20 65 72 72 3b 0a 20 20 20 20 20 20 64 6f  nt err;.      do
f6e90 7b 0a 20 20 20 20 20 20 20 20 65 72 72 20 3d 20  {.        err = 
f6ea0 6f 73 46 61 6c 6c 6f 63 61 74 65 28 70 46 69 6c  osFallocate(pFil
f6eb0 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 73 69 7a  e->h, buf.st_siz
f6ec0 65 2c 20 6e 53 69 7a 65 2d 62 75 66 2e 73 74 5f  e, nSize-buf.st_
f6ed0 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 77 68  size);.      }wh
f6ee0 69 6c 65 28 20 65 72 72 3d 3d 45 49 4e 54 52 20  ile( err==EINTR 
f6ef0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72 72  );.      if( err
f6f00 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
f6f10 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 23 65  _IOERR_WRITE;.#e
f6f20 6c 73 65 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  lse.      /* If 
f6f30 74 68 65 20 4f 53 20 64 6f 65 73 20 6e 6f 74 20  the OS does not 
f6f40 68 61 76 65 20 70 6f 73 69 78 5f 66 61 6c 6c 6f  have posix_fallo
f6f50 63 61 74 65 28 29 2c 20 66 61 6b 65 20 69 74 2e  cate(), fake it.
f6f60 20 46 69 72 73 74 20 75 73 65 0a 20 20 20 20 20   First use.     
f6f70 20 2a 2a 20 66 74 72 75 6e 63 61 74 65 28 29 20   ** ftruncate() 
f6f80 74 6f 20 73 65 74 20 74 68 65 20 66 69 6c 65 20  to set the file 
f6f90 73 69 7a 65 2c 20 74 68 65 6e 20 77 72 69 74 65  size, then write
f6fa0 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 74   a single byte t
f6fb0 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c  o.      ** the l
f6fc0 61 73 74 20 62 79 74 65 20 69 6e 20 65 61 63 68  ast byte in each
f6fd0 20 62 6c 6f 63 6b 20 77 69 74 68 69 6e 20 74 68   block within th
f6fe0 65 20 65 78 74 65 6e 64 65 64 20 72 65 67 69 6f  e extended regio
f6ff0 6e 2e 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a  n. This.      **
f7000 20 69 73 20 74 68 65 20 73 61 6d 65 20 74 65 63   is the same tec
f7010 68 6e 69 71 75 65 20 75 73 65 64 20 62 79 20 67  hnique used by g
f7020 6c 69 62 63 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  libc to implemen
f7030 74 20 70 6f 73 69 78 5f 66 61 6c 6c 6f 63 61 74  t posix_fallocat
f7040 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20  e().      ** on 
f7050 73 79 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20  systems that do 
f7060 6e 6f 74 20 68 61 76 65 20 61 20 72 65 61 6c 20  not have a real 
f7070 66 61 6c 6c 6f 63 61 74 65 28 29 20 73 79 73 74  fallocate() syst
f7080 65 6d 20 63 61 6c 6c 2e 0a 20 20 20 20 20 20 2a  em call..      *
f7090 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 6c 6b  /.      int nBlk
f70a0 20 3d 20 62 75 66 2e 73 74 5f 62 6c 6b 73 69 7a   = buf.st_blksiz
f70b0 65 3b 20 20 2f 2a 20 46 69 6c 65 2d 73 79 73 74  e;  /* File-syst
f70c0 65 6d 20 62 6c 6f 63 6b 20 73 69 7a 65 20 2a 2f  em block size */
f70d0 0a 20 20 20 20 20 20 69 36 34 20 69 57 72 69 74  .      i64 iWrit
f70e0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
f70f0 20 20 20 2f 2a 20 4e 65 78 74 20 6f 66 66 73 65     /* Next offse
f7100 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 2a 2f  t to write to */
f7110 0a 0a 20 20 20 20 20 20 69 66 28 20 72 6f 62 75  ..      if( robu
f7120 73 74 5f 66 74 72 75 6e 63 61 74 65 28 70 46 69  st_ftruncate(pFi
f7130 6c 65 2d 3e 68 2c 20 6e 53 69 7a 65 29 20 29 7b  le->h, nSize) ){
f7140 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
f7150 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
f7160 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  o;.        retur
f7170 6e 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 53  n unixLogError(S
f7180 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e  QLITE_IOERR_TRUN
f7190 43 41 54 45 2c 20 22 66 74 72 75 6e 63 61 74 65  CATE, "ftruncate
f71a0 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74 68 29  ", pFile->zPath)
f71b0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
f71c0 69 57 72 69 74 65 20 3d 20 28 28 62 75 66 2e 73  iWrite = ((buf.s
f71d0 74 5f 73 69 7a 65 20 2b 20 32 2a 6e 42 6c 6b 20  t_size + 2*nBlk 
f71e0 2d 20 31 29 2f 6e 42 6c 6b 29 2a 6e 42 6c 6b 2d  - 1)/nBlk)*nBlk-
f71f0 31 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  1;.      while( 
f7200 69 57 72 69 74 65 3c 6e 53 69 7a 65 20 29 7b 0a  iWrite<nSize ){.
f7210 20 20 20 20 20 20 20 20 69 6e 74 20 6e 57 72 69          int nWri
f7220 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 72 69 74  te = seekAndWrit
f7230 65 28 70 46 69 6c 65 2c 20 69 57 72 69 74 65 2c  e(pFile, iWrite,
f7240 20 22 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 20   "", 1);.       
f7250 20 69 66 28 20 6e 57 72 69 74 65 21 3d 31 20 29   if( nWrite!=1 )
f7260 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
f7270 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20  OERR_WRITE;.    
f7280 20 20 20 20 69 57 72 69 74 65 20 2b 3d 20 6e 42      iWrite += nB
f7290 6c 6b 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  lk;.      }.#end
f72a0 69 66 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  if.    }.  }..  
f72b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f72c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 2a 70  ;.}../*.** If *p
f72d0 41 72 67 20 69 73 20 69 6e 69 74 69 74 69 61 6c  Arg is inititial
f72e0 6c 79 20 6e 65 67 61 74 69 76 65 20 74 68 65 6e  ly negative then
f72f0 20 74 68 69 73 20 69 73 20 61 20 71 75 65 72 79   this is a query
f7300 2e 20 20 53 65 74 20 2a 70 41 72 67 20 74 6f 0a  .  Set *pArg to.
f7310 2a 2a 20 31 20 6f 72 20 30 20 64 65 70 65 6e 64  ** 1 or 0 depend
f7320 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 6f  ing on whether o
f7330 72 20 6e 6f 74 20 62 69 74 20 6d 61 73 6b 20 6f  r not bit mask o
f7340 66 20 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c 61  f pFile->ctrlFla
f7350 67 73 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a  gs is set..**.**
f7360 20 49 66 20 2a 70 41 72 67 20 69 73 20 30 20 6f   If *pArg is 0 o
f7370 72 20 31 2c 20 74 68 65 6e 20 63 6c 65 61 72 20  r 1, then clear 
f7380 6f 72 20 73 65 74 20 74 68 65 20 6d 61 73 6b 20  or set the mask 
f7390 62 69 74 20 6f 66 20 70 46 69 6c 65 2d 3e 63 74  bit of pFile->ct
f73a0 72 6c 46 6c 61 67 73 2e 0a 2a 2f 0a 73 74 61 74  rlFlags..*/.stat
f73b0 69 63 20 76 6f 69 64 20 75 6e 69 78 4d 6f 64 65  ic void unixMode
f73c0 42 69 74 28 75 6e 69 78 46 69 6c 65 20 2a 70 46  Bit(unixFile *pF
f73d0 69 6c 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68  ile, unsigned ch
f73e0 61 72 20 6d 61 73 6b 2c 20 69 6e 74 20 2a 70 41  ar mask, int *pA
f73f0 72 67 29 7b 0a 20 20 69 66 28 20 2a 70 41 72 67  rg){.  if( *pArg
f7400 3c 30 20 29 7b 0a 20 20 20 20 2a 70 41 72 67 20  <0 ){.    *pArg 
f7410 3d 20 28 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c  = (pFile->ctrlFl
f7420 61 67 73 20 26 20 6d 61 73 6b 29 21 3d 30 3b 0a  ags & mask)!=0;.
f7430 20 20 7d 65 6c 73 65 20 69 66 28 20 28 2a 70 41    }else if( (*pA
f7440 72 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 46  rg)==0 ){.    pF
f7450 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 20 26  ile->ctrlFlags &
f7460 3d 20 7e 6d 61 73 6b 3b 0a 20 20 7d 65 6c 73 65  = ~mask;.  }else
f7470 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 63 74 72  {.    pFile->ctr
f7480 6c 46 6c 61 67 73 20 7c 3d 20 6d 61 73 6b 3b 0a  lFlags |= mask;.
f7490 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66    }.}../*.** Inf
f74a0 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e  ormation and con
f74b0 74 72 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20  trol of an open 
f74c0 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a  file handle..*/.
f74d0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46  static int unixF
f74e0 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74  ileControl(sqlit
f74f0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
f7500 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29   op, void *pArg)
f7510 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  {.  unixFile *pF
f7520 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
f7530 29 69 64 3b 0a 20 20 73 77 69 74 63 68 28 20 6f  )id;.  switch( o
f7540 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  p ){.    case SQ
f7550 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53  LITE_FCNTL_LOCKS
f7560 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28  TATE: {.      *(
f7570 69 6e 74 2a 29 70 41 72 67 20 3d 20 70 46 69 6c  int*)pArg = pFil
f7580 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20  e->eFileLock;.  
f7590 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
f75a0 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  E_OK;.    }.    
f75b0 63 61 73 65 20 53 51 4c 49 54 45 5f 4c 41 53 54  case SQLITE_LAST
f75c0 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 20 20 20  _ERRNO: {.      
f75d0 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 70 46  *(int*)pArg = pF
f75e0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a  ile->lastErrno;.
f75f0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
f7600 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
f7610 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43    case SQLITE_FC
f7620 4e 54 4c 5f 43 48 55 4e 4b 5f 53 49 5a 45 3a 20  NTL_CHUNK_SIZE: 
f7630 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73  {.      pFile->s
f7640 7a 43 68 75 6e 6b 20 3d 20 2a 28 69 6e 74 20 2a  zChunk = *(int *
f7650 29 70 41 72 67 3b 0a 20 20 20 20 20 20 72 65 74  )pArg;.      ret
f7660 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
f7670 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
f7680 4c 49 54 45 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f  LITE_FCNTL_SIZE_
f7690 48 49 4e 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e  HINT: {.      in
f76a0 74 20 72 63 3b 0a 20 20 20 20 20 20 53 69 6d 75  t rc;.      Simu
f76b0 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
f76c0 6e 28 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  n(1);.      rc =
f76d0 20 66 63 6e 74 6c 53 69 7a 65 48 69 6e 74 28 70   fcntlSizeHint(p
f76e0 46 69 6c 65 2c 20 2a 28 69 36 34 20 2a 29 70 41  File, *(i64 *)pA
f76f0 72 67 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c  rg);.      Simul
f7700 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
f7710 28 30 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  (0);.      retur
f7720 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
f7730 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54  case SQLITE_FCNT
f7740 4c 5f 50 45 52 53 49 53 54 5f 57 41 4c 3a 20 7b  L_PERSIST_WAL: {
f7750 0a 20 20 20 20 20 20 75 6e 69 78 4d 6f 64 65 42  .      unixModeB
f7760 69 74 28 70 46 69 6c 65 2c 20 55 4e 49 58 46 49  it(pFile, UNIXFI
f7770 4c 45 5f 50 45 52 53 49 53 54 5f 57 41 4c 2c 20  LE_PERSIST_WAL, 
f7780 28 69 6e 74 2a 29 70 41 72 67 29 3b 0a 20 20 20  (int*)pArg);.   
f7790 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
f77a0 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  _OK;.    }.    c
f77b0 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c  ase SQLITE_FCNTL
f77c0 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57  _POWERSAFE_OVERW
f77d0 52 49 54 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e  RITE: {.      un
f77e0 69 78 4d 6f 64 65 42 69 74 28 70 46 69 6c 65 2c  ixModeBit(pFile,
f77f0 20 55 4e 49 58 46 49 4c 45 5f 50 53 4f 57 2c 20   UNIXFILE_PSOW, 
f7800 28 69 6e 74 2a 29 70 41 72 67 29 3b 0a 20 20 20  (int*)pArg);.   
f7810 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
f7820 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  _OK;.    }.    c
f7830 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c  ase SQLITE_FCNTL
f7840 5f 56 46 53 4e 41 4d 45 3a 20 7b 0a 20 20 20 20  _VFSNAME: {.    
f7850 20 20 2a 28 63 68 61 72 2a 2a 29 70 41 72 67 20    *(char**)pArg 
f7860 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
f7870 66 28 22 25 73 22 2c 20 70 46 69 6c 65 2d 3e 70  f("%s", pFile->p
f7880 56 66 73 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Vfs->zName);.   
f7890 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
f78a0 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65  _OK;.    }.#ifde
f78b0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
f78c0 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20     /* The pager 
f78d0 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f  calls this metho
f78e0 64 20 74 6f 20 73 69 67 6e 61 6c 20 74 68 61 74  d to signal that
f78f0 20 69 74 20 68 61 73 20 64 6f 6e 65 0a 20 20 20   it has done.   
f7900 20 2a 2a 20 61 20 72 6f 6c 6c 62 61 63 6b 20 61   ** a rollback a
f7910 6e 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61  nd that the data
f7920 62 61 73 65 20 69 73 20 74 68 65 72 65 66 6f 72  base is therefor
f7930 65 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a  e unchanged and.
f7940 20 20 20 20 2a 2a 20 69 74 20 68 65 6e 63 65 20      ** it hence 
f7950 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 74 68 65  it is OK for the
f7960 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 68 61   transaction cha
f7970 6e 67 65 20 63 6f 75 6e 74 65 72 20 74 6f 20 62  nge counter to b
f7980 65 0a 20 20 20 20 2a 2a 20 75 6e 63 68 61 6e 67  e.    ** unchang
f7990 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ed..    */.    c
f79a0 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c  ase SQLITE_FCNTL
f79b0 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 3a 20 7b  _DB_UNCHANGED: {
f79c0 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c  .      ((unixFil
f79d0 65 2a 29 69 64 29 2d 3e 64 62 55 70 64 61 74 65  e*)id)->dbUpdate
f79e0 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75   = 0;.      retu
f79f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
f7a00 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20 53    }.#endif.#if S
f7a10 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
f7a20 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65  KING_STYLE && de
f7a30 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
f7a40 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
f7a50 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _SET_LOCKPROXYFI
f7a60 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c  LE:.    case SQL
f7a70 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58  ITE_GET_LOCKPROX
f7a80 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 72  YFILE: {.      r
f7a90 65 74 75 72 6e 20 70 72 6f 78 79 46 69 6c 65 43  eturn proxyFileC
f7aa0 6f 6e 74 72 6f 6c 28 69 64 2c 6f 70 2c 70 41 72  ontrol(id,op,pAr
f7ab0 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  g);.    }.#endif
f7ac0 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
f7ad0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
f7ae0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  && defined(__APP
f7af0 4c 45 5f 5f 29 20 2a 2f 0a 20 20 7d 0a 20 20 72  LE__) */.  }.  r
f7b00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 54  eturn SQLITE_NOT
f7b10 46 4f 55 4e 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  FOUND;.}../*.** 
f7b20 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f  Return the secto
f7b30 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  r size in bytes 
f7b40 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  of the underlyin
f7b50 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66  g block device f
f7b60 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66  or.** the specif
f7b70 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69  ied file. This i
f7b80 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20  s almost always 
f7b90 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d  512 bytes, but m
f7ba0 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20  ay be.** larger 
f7bb0 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73  for some devices
f7bc0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63  ..**.** SQLite c
f7bd0 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73  ode assumes this
f7be0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74   function cannot
f7bf0 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61   fail. It also a
f7c00 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69  ssumes that.** i
f7c10 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20  f two files are 
f7c20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73  created in the s
f7c30 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  ame file-system 
f7c40 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a  directory (i.e..
f7c50 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e  ** a database an
f7c60 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69  d its journal fi
f7c70 6c 65 29 20 74 68 61 74 20 74 68 65 20 73 65 63  le) that the sec
f7c80 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65  tor size will be
f7c90 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72   the.** same for
f7ca0 20 62 6f 74 68 2e 0a 2a 2f 0a 23 69 66 6e 64 65   both..*/.#ifnde
f7cb0 66 20 5f 5f 51 4e 58 4e 54 4f 5f 5f 20 0a 73 74  f __QNXNTO__ .st
f7cc0 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 65 63  atic int unixSec
f7cd0 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f  torSize(sqlite3_
f7ce0 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 29 7b 0a  file *NotUsed){.
f7cf0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
f7d00 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72  ER(NotUsed);.  r
f7d10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 45 46  eturn SQLITE_DEF
f7d20 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45  AULT_SECTOR_SIZE
f7d30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
f7d40 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
f7d50 76 65 72 73 69 6f 6e 20 6f 66 20 75 6e 69 78 53  version of unixS
f7d60 65 63 74 6f 72 53 69 7a 65 28 29 20 69 73 20 6f  ectorSize() is o
f7d70 70 74 69 6d 69 7a 65 64 20 66 6f 72 20 51 4e 58  ptimized for QNX
f7d80 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 5f 51 4e  ..*/.#ifdef __QN
f7d90 58 4e 54 4f 5f 5f 0a 23 69 6e 63 6c 75 64 65 20  XNTO__.#include 
f7da0 3c 73 79 73 2f 64 63 6d 64 5f 62 6c 6b 2e 68 3e  <sys/dcmd_blk.h>
f7db0 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73  .#include <sys/s
f7dc0 74 61 74 76 66 73 2e 68 3e 0a 73 74 61 74 69 63  tatvfs.h>.static
f7dd0 20 69 6e 74 20 75 6e 69 78 53 65 63 74 6f 72 53   int unixSectorS
f7de0 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ize(sqlite3_file
f7df0 20 2a 69 64 29 7b 0a 20 20 75 6e 69 78 46 69 6c   *id){.  unixFil
f7e00 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
f7e10 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 66 28 20  File*)id;.  if( 
f7e20 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a  pFile->sectorSiz
f7e30 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 73 74  e == 0 ){.    st
f7e40 72 75 63 74 20 73 74 61 74 76 66 73 20 66 73 49  ruct statvfs fsI
f7e50 6e 66 6f 3b 0a 20 20 20 20 20 20 20 0a 20 20 20  nfo;.       .   
f7e60 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c 74 73   /* Set defaults
f7e70 20 66 6f 72 20 6e 6f 6e 2d 73 75 70 70 6f 72 74   for non-support
f7e80 65 64 20 66 69 6c 65 73 79 73 74 65 6d 73 20 2a  ed filesystems *
f7e90 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 65 63  /.    pFile->sec
f7ea0 74 6f 72 53 69 7a 65 20 3d 20 53 51 4c 49 54 45  torSize = SQLITE
f7eb0 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
f7ec0 53 49 5a 45 3b 0a 20 20 20 20 70 46 69 6c 65 2d  SIZE;.    pFile-
f7ed0 3e 64 65 76 69 63 65 43 68 61 72 61 63 74 65 72  >deviceCharacter
f7ee0 69 73 74 69 63 73 20 3d 20 30 3b 0a 20 20 20 20  istics = 0;.    
f7ef0 69 66 28 20 66 73 74 61 74 76 66 73 28 70 46 69  if( fstatvfs(pFi
f7f00 6c 65 2d 3e 68 2c 20 26 66 73 49 6e 66 6f 29 20  le->h, &fsInfo) 
f7f10 3d 3d 20 2d 31 20 29 20 7b 0a 20 20 20 20 20 20  == -1 ) {.      
f7f20 72 65 74 75 72 6e 20 70 46 69 6c 65 2d 3e 73 65  return pFile->se
f7f30 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20 7d 0a  ctorSize;.    }.
f7f40 0a 20 20 20 20 69 66 28 20 21 73 74 72 63 6d 70  .    if( !strcmp
f7f50 28 66 73 49 6e 66 6f 2e 66 5f 62 61 73 65 74 79  (fsInfo.f_basety
f7f60 70 65 2c 20 22 74 6d 70 22 29 20 29 20 7b 0a 20  pe, "tmp") ) {. 
f7f70 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 65 63 74       pFile->sect
f7f80 6f 72 53 69 7a 65 20 3d 20 66 73 49 6e 66 6f 2e  orSize = fsInfo.
f7f90 66 5f 62 73 69 7a 65 3b 0a 20 20 20 20 20 20 70  f_bsize;.      p
f7fa0 46 69 6c 65 2d 3e 64 65 76 69 63 65 43 68 61 72  File->deviceChar
f7fb0 61 63 74 65 72 69 73 74 69 63 73 20 3d 0a 20 20  acteristics =.  
f7fc0 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 43        SQLITE_IOC
f7fd0 41 50 5f 41 54 4f 4d 49 43 34 4b 20 7c 20 20 20  AP_ATOMIC4K |   
f7fe0 20 20 20 20 2f 2a 20 41 6c 6c 20 72 61 6d 20 66      /* All ram f
f7ff0 69 6c 65 73 79 73 74 65 6d 20 77 72 69 74 65 73  ilesystem writes
f8000 20 61 72 65 20 61 74 6f 6d 69 63 20 2a 2f 0a 20   are atomic */. 
f8010 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f         SQLITE_IO
f8020 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20  CAP_SAFE_APPEND 
f8030 7c 20 20 20 20 2f 2a 20 67 72 6f 77 69 6e 67 20  |    /* growing 
f8040 74 68 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f  the file does no
f8050 74 20 6f 63 63 75 72 20 75 6e 74 69 6c 0a 20 20  t occur until.  
f8060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8080 20 20 20 20 2a 2a 20 74 68 65 20 77 72 69 74 65      ** the write
f8090 20 73 75 63 63 65 65 64 73 20 2a 2f 0a 20 20 20   succeeds */.   
f80a0 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 43 41       SQLITE_IOCA
f80b0 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 7c 20 20  P_SEQUENTIAL |  
f80c0 20 20 20 2f 2a 20 54 68 65 20 72 61 6d 20 66 69     /* The ram fi
f80d0 6c 65 73 79 73 74 65 6d 20 68 61 73 20 6e 6f 20  lesystem has no 
f80e0 77 72 69 74 65 20 62 65 68 69 6e 64 0a 20 20 20  write behind.   
f80f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8110 20 20 20 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f     ** so it is o
f8120 72 64 65 72 65 64 20 2a 2f 0a 20 20 20 20 20 20  rdered */.      
f8130 20 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69    0;.    }else i
f8140 66 28 20 73 74 72 73 74 72 28 66 73 49 6e 66 6f  f( strstr(fsInfo
f8150 2e 66 5f 62 61 73 65 74 79 70 65 2c 20 22 65 74  .f_basetype, "et
f8160 66 73 22 29 20 29 7b 0a 20 20 20 20 20 20 70 46  fs") ){.      pF
f8170 69 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20  ile->sectorSize 
f8180 3d 20 66 73 49 6e 66 6f 2e 66 5f 62 73 69 7a 65  = fsInfo.f_bsize
f8190 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 64  ;.      pFile->d
f81a0 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
f81b0 74 69 63 73 20 3d 0a 20 20 20 20 20 20 20 20 2f  tics =.        /
f81c0 2a 20 65 74 66 73 20 63 6c 75 73 74 65 72 20 73  * etfs cluster s
f81d0 69 7a 65 20 77 72 69 74 65 73 20 61 72 65 20 61  ize writes are a
f81e0 74 6f 6d 69 63 20 2a 2f 0a 20 20 20 20 20 20 20  tomic */.       
f81f0 20 28 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 53   (pFile->sectorS
f8200 69 7a 65 20 2f 20 35 31 32 20 2a 20 53 51 4c 49  ize / 512 * SQLI
f8210 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35  TE_IOCAP_ATOMIC5
f8220 31 32 29 20 7c 0a 20 20 20 20 20 20 20 20 53 51  12) |.        SQ
f8230 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f  LITE_IOCAP_SAFE_
f8240 41 50 50 45 4e 44 20 7c 20 20 20 20 2f 2a 20 67  APPEND |    /* g
f8250 72 6f 77 69 6e 67 20 74 68 65 20 66 69 6c 65 20  rowing the file 
f8260 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20 75  does not occur u
f8270 6e 74 69 6c 0a 20 20 20 20 20 20 20 20 20 20 20  ntil.           
f8280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8290 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68             ** th
f82a0 65 20 77 72 69 74 65 20 73 75 63 63 65 65 64 73  e write succeeds
f82b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 4c 49   */.        SQLI
f82c0 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54  TE_IOCAP_SEQUENT
f82d0 49 41 4c 20 7c 20 20 20 20 20 2f 2a 20 54 68 65  IAL |     /* The
f82e0 20 72 61 6d 20 66 69 6c 65 73 79 73 74 65 6d 20   ram filesystem 
f82f0 68 61 73 20 6e 6f 20 77 72 69 74 65 20 62 65 68  has no write beh
f8300 69 6e 64 0a 20 20 20 20 20 20 20 20 20 20 20 20  ind.            
f8310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8320 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20            ** so 
f8330 69 74 20 69 73 20 6f 72 64 65 72 65 64 20 2a 2f  it is ordered */
f8340 0a 20 20 20 20 20 20 20 20 30 3b 0a 20 20 20 20  .        0;.    
f8350 7d 65 6c 73 65 20 69 66 28 20 21 73 74 72 63 6d  }else if( !strcm
f8360 70 28 66 73 49 6e 66 6f 2e 66 5f 62 61 73 65 74  p(fsInfo.f_baset
f8370 79 70 65 2c 20 22 71 6e 78 36 22 29 20 29 7b 0a  ype, "qnx6") ){.
f8380 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 65 63        pFile->sec
f8390 74 6f 72 53 69 7a 65 20 3d 20 66 73 49 6e 66 6f  torSize = fsInfo
f83a0 2e 66 5f 62 73 69 7a 65 3b 0a 20 20 20 20 20 20  .f_bsize;.      
f83b0 70 46 69 6c 65 2d 3e 64 65 76 69 63 65 43 68 61  pFile->deviceCha
f83c0 72 61 63 74 65 72 69 73 74 69 63 73 20 3d 0a 20  racteristics =. 
f83d0 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f         SQLITE_IO
f83e0 43 41 50 5f 41 54 4f 4d 49 43 20 7c 20 20 20 20  CAP_ATOMIC |    
f83f0 20 20 20 20 20 2f 2a 20 41 6c 6c 20 66 69 6c 65       /* All file
f8400 73 79 73 74 65 6d 20 77 72 69 74 65 73 20 61 72  system writes ar
f8410 65 20 61 74 6f 6d 69 63 20 2a 2f 0a 20 20 20 20  e atomic */.    
f8420 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 43 41 50      SQLITE_IOCAP
f8430 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 7c 20 20  _SAFE_APPEND |  
f8440 20 20 2f 2a 20 67 72 6f 77 69 6e 67 20 74 68 65    /* growing the
f8450 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 6f   file does not o
f8460 63 63 75 72 20 75 6e 74 69 6c 0a 20 20 20 20 20  ccur until.     
f8470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8490 20 2a 2a 20 74 68 65 20 77 72 69 74 65 20 73 75   ** the write su
f84a0 63 63 65 65 64 73 20 2a 2f 0a 20 20 20 20 20 20  cceeds */.      
f84b0 20 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53    SQLITE_IOCAP_S
f84c0 45 51 55 45 4e 54 49 41 4c 20 7c 20 20 20 20 20  EQUENTIAL |     
f84d0 2f 2a 20 54 68 65 20 72 61 6d 20 66 69 6c 65 73  /* The ram files
f84e0 79 73 74 65 6d 20 68 61 73 20 6e 6f 20 77 72 69  ystem has no wri
f84f0 74 65 20 62 65 68 69 6e 64 0a 20 20 20 20 20 20  te behind.      
f8500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8520 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 72 64 65  ** so it is orde
f8530 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 30  red */.        0
f8540 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
f8550 21 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66  !strcmp(fsInfo.f
f8560 5f 62 61 73 65 74 79 70 65 2c 20 22 71 6e 78 34  _basetype, "qnx4
f8570 22 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  ") ){.      pFil
f8580 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20  e->sectorSize = 
f8590 66 73 49 6e 66 6f 2e 66 5f 62 73 69 7a 65 3b 0a  fsInfo.f_bsize;.
f85a0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 64 65 76        pFile->dev
f85b0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
f85c0 63 73 20 3d 0a 20 20 20 20 20 20 20 20 2f 2a 20  cs =.        /* 
f85d0 66 75 6c 6c 20 62 69 74 73 65 74 20 6f 66 20 61  full bitset of a
f85e0 74 6f 6d 69 63 73 20 66 72 6f 6d 20 6d 61 78 20  tomics from max 
f85f0 73 65 63 74 6f 72 20 73 69 7a 65 20 61 6e 64 20  sector size and 
f8600 73 6d 61 6c 6c 65 72 20 2a 2f 0a 20 20 20 20 20  smaller */.     
f8610 20 20 20 28 28 70 46 69 6c 65 2d 3e 73 65 63 74     ((pFile->sect
f8620 6f 72 53 69 7a 65 20 2f 20 35 31 32 20 2a 20 53  orSize / 512 * S
f8630 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
f8640 49 43 35 31 32 29 20 3c 3c 20 31 29 20 2d 20 32  IC512) << 1) - 2
f8650 20 7c 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54   |.        SQLIT
f8660 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49  E_IOCAP_SEQUENTI
f8670 41 4c 20 7c 20 20 20 20 20 2f 2a 20 54 68 65 20  AL |     /* The 
f8680 72 61 6d 20 66 69 6c 65 73 79 73 74 65 6d 20 68  ram filesystem h
f8690 61 73 20 6e 6f 20 77 72 69 74 65 20 62 65 68 69  as no write behi
f86a0 6e 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  nd.             
f86b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f86c0 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20 69           ** so i
f86d0 74 20 69 73 20 6f 72 64 65 72 65 64 20 2a 2f 0a  t is ordered */.
f86e0 20 20 20 20 20 20 20 20 30 3b 0a 20 20 20 20 7d          0;.    }
f86f0 65 6c 73 65 20 69 66 28 20 73 74 72 73 74 72 28  else if( strstr(
f8700 66 73 49 6e 66 6f 2e 66 5f 62 61 73 65 74 79 70  fsInfo.f_basetyp
f8710 65 2c 20 22 64 6f 73 22 29 20 29 7b 0a 20 20 20  e, "dos") ){.   
f8720 20 20 20 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72     pFile->sector
f8730 53 69 7a 65 20 3d 20 66 73 49 6e 66 6f 2e 66 5f  Size = fsInfo.f_
f8740 62 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 46 69  bsize;.      pFi
f8750 6c 65 2d 3e 64 65 76 69 63 65 43 68 61 72 61 63  le->deviceCharac
f8760 74 65 72 69 73 74 69 63 73 20 3d 0a 20 20 20 20  teristics =.    
f8770 20 20 20 20 2f 2a 20 66 75 6c 6c 20 62 69 74 73      /* full bits
f8780 65 74 20 6f 66 20 61 74 6f 6d 69 63 73 20 66 72  et of atomics fr
f8790 6f 6d 20 6d 61 78 20 73 65 63 74 6f 72 20 73 69  om max sector si
f87a0 7a 65 20 61 6e 64 20 73 6d 61 6c 6c 65 72 20 2a  ze and smaller *
f87b0 2f 0a 20 20 20 20 20 20 20 20 28 28 70 46 69 6c  /.        ((pFil
f87c0 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 2f 20  e->sectorSize / 
f87d0 35 31 32 20 2a 20 53 51 4c 49 54 45 5f 49 4f 43  512 * SQLITE_IOC
f87e0 41 50 5f 41 54 4f 4d 49 43 35 31 32 29 20 3c 3c  AP_ATOMIC512) <<
f87f0 20 31 29 20 2d 20 32 20 7c 0a 20 20 20 20 20 20   1) - 2 |.      
f8800 20 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53    SQLITE_IOCAP_S
f8810 45 51 55 45 4e 54 49 41 4c 20 7c 20 20 20 20 20  EQUENTIAL |     
f8820 2f 2a 20 54 68 65 20 72 61 6d 20 66 69 6c 65 73  /* The ram files
f8830 79 73 74 65 6d 20 68 61 73 20 6e 6f 20 77 72 69  ystem has no wri
f8840 74 65 20 62 65 68 69 6e 64 0a 20 20 20 20 20 20  te behind.      
f8850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8870 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 72 64 65  ** so it is orde
f8880 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 30  red */.        0
f8890 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
f88a0 20 20 20 70 46 69 6c 65 2d 3e 64 65 76 69 63 65     pFile->device
f88b0 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20  Characteristics 
f88c0 3d 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45  =.        SQLITE
f88d0 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32  _IOCAP_ATOMIC512
f88e0 20 7c 20 20 20 20 20 20 2f 2a 20 62 6c 6f 63 6b   |      /* block
f88f0 73 20 61 72 65 20 61 74 6f 6d 69 63 20 2a 2f 0a  s are atomic */.
f8900 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49          SQLITE_I
f8910 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44  OCAP_SAFE_APPEND
f8920 20 7c 20 20 20 20 2f 2a 20 67 72 6f 77 69 6e 67   |    /* growing
f8930 20 74 68 65 20 66 69 6c 65 20 64 6f 65 73 20 6e   the file does n
f8940 6f 74 20 6f 63 63 75 72 20 75 6e 74 69 6c 0a 20  ot occur until. 
f8950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8970 20 20 20 20 20 2a 2a 20 74 68 65 20 77 72 69 74       ** the writ
f8980 65 20 73 75 63 63 65 65 64 73 20 2a 2f 0a 20 20  e succeeds */.  
f8990 20 20 20 20 20 20 30 3b 0a 20 20 20 20 7d 0a 20        0;.    }. 
f89a0 20 7d 0a 20 20 2f 2a 20 4c 61 73 74 20 63 68 61   }.  /* Last cha
f89b0 6e 63 65 20 76 65 72 69 66 69 63 61 74 69 6f 6e  nce verification
f89c0 2e 20 20 49 66 20 74 68 65 20 73 65 63 74 6f 72  .  If the sector
f89d0 20 73 69 7a 65 20 69 73 6e 27 74 20 61 20 6d 75   size isn't a mu
f89e0 6c 74 69 70 6c 65 20 6f 66 20 35 31 32 0a 20 20  ltiple of 512.  
f89f0 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 6e 27 74  ** then it isn't
f8a00 20 76 61 6c 69 64 2e 2a 2f 0a 20 20 69 66 28 20   valid.*/.  if( 
f8a10 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a  pFile->sectorSiz
f8a20 65 20 25 20 35 31 32 20 21 3d 20 30 20 29 7b 0a  e % 512 != 0 ){.
f8a30 20 20 20 20 70 46 69 6c 65 2d 3e 64 65 76 69 63      pFile->devic
f8a40 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
f8a50 20 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d   = 0;.    pFile-
f8a60 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 53 51  >sectorSize = SQ
f8a70 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43  LITE_DEFAULT_SEC
f8a80 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a 20 20  TOR_SIZE;.  }.  
f8a90 72 65 74 75 72 6e 20 70 46 69 6c 65 2d 3e 73 65  return pFile->se
f8aa0 63 74 6f 72 53 69 7a 65 3b 0a 7d 0a 23 65 6e 64  ctorSize;.}.#end
f8ab0 69 66 20 2f 2a 20 5f 5f 51 4e 58 4e 54 4f 5f 5f  if /* __QNXNTO__
f8ac0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72   */../*.** Retur
f8ad0 6e 20 74 68 65 20 64 65 76 69 63 65 20 63 68 61  n the device cha
f8ae0 72 61 63 74 65 72 69 73 74 69 63 73 20 66 6f 72  racteristics for
f8af0 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a   the file..**.**
f8b00 20 54 68 69 73 20 56 46 53 20 69 73 20 73 65 74   This VFS is set
f8b10 20 75 70 20 74 6f 20 72 65 74 75 72 6e 20 53 51   up to return SQ
f8b20 4c 49 54 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52  LITE_IOCAP_POWER
f8b30 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 20 62  SAFE_OVERWRITE b
f8b40 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 20 48 6f  y default..** Ho
f8b50 77 65 76 65 72 2c 20 74 68 61 74 20 63 68 6f 69  wever, that choi
f8b60 63 65 20 69 73 20 63 6f 6e 74 72 61 76 65 72 73  ce is contravers
f8b70 69 61 6c 20 73 69 6e 63 65 20 74 65 63 68 6e 69  ial since techni
f8b80 63 61 6c 6c 79 20 74 68 65 20 75 6e 64 65 72 6c  cally the underl
f8b90 79 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 73 79 73  ying.** file sys
f8ba0 74 65 6d 20 64 6f 65 73 20 6e 6f 74 20 61 6c 77  tem does not alw
f8bb0 61 79 73 20 70 72 6f 76 69 64 65 20 70 6f 77 65  ays provide powe
f8bc0 72 73 61 66 65 20 6f 76 65 72 77 72 69 74 65 73  rsafe overwrites
f8bd0 2e 20 20 28 49 6e 20 6f 74 68 65 72 0a 2a 2a 20  .  (In other.** 
f8be0 77 6f 72 64 73 2c 20 61 66 74 65 72 20 61 20 70  words, after a p
f8bf0 6f 77 65 72 2d 6c 6f 73 73 20 65 76 65 6e 74 2c  ower-loss event,
f8c00 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 66 69   parts of the fi
f8c10 6c 65 20 74 68 61 74 20 77 65 72 65 20 6e 65 76  le that were nev
f8c20 65 72 0a 2a 2a 20 77 72 69 74 74 65 6e 20 6d 69  er.** written mi
f8c30 67 68 74 20 65 6e 64 20 75 70 20 62 65 69 6e 67  ght end up being
f8c40 20 61 6c 74 65 72 65 64 2e 29 20 20 48 6f 77 65   altered.)  Howe
f8c50 76 65 72 2c 20 6e 6f 6e 2d 50 53 4f 57 20 62 65  ver, non-PSOW be
f8c60 68 61 76 69 6f 72 20 69 73 20 76 65 72 79 2c 0a  havior is very,.
f8c70 2a 2a 20 76 65 72 79 20 72 61 72 65 2e 20 20 41  ** very rare.  A
f8c80 6e 64 20 61 73 73 65 72 74 69 6e 67 20 50 53 4f  nd asserting PSO
f8c90 57 20 6d 61 6b 65 73 20 61 20 6c 61 72 67 65 20  W makes a large 
f8ca0 72 65 64 75 63 74 69 6f 6e 20 69 6e 20 74 68 65  reduction in the
f8cb0 20 61 6d 6f 75 6e 74 0a 2a 2a 20 6f 66 20 72 65   amount.** of re
f8cc0 71 75 69 72 65 64 20 49 2f 4f 20 66 6f 72 20 6a  quired I/O for j
f8cd0 6f 75 72 6e 61 6c 69 6e 67 2c 20 73 69 6e 63 65  ournaling, since
f8ce0 20 61 20 6c 6f 74 20 6f 66 20 70 61 64 64 69 6e   a lot of paddin
f8cf0 67 20 69 73 20 65 6c 69 6d 69 6e 61 74 65 64 2e  g is eliminated.
f8d00 0a 2a 2a 20 20 48 65 6e 63 65 2c 20 77 68 69 6c  .**  Hence, whil
f8d10 65 20 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52  e POWERSAFE_OVER
f8d20 57 52 49 54 45 20 69 73 20 6f 6e 20 62 79 20 64  WRITE is on by d
f8d30 65 66 61 75 6c 74 2c 20 74 68 65 72 65 20 69 73  efault, there is
f8d40 20 61 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 0a   a file-control.
f8d50 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20  ** available to 
f8d60 74 75 72 6e 20 69 74 20 6f 66 66 20 61 6e 64 20  turn it off and 
f8d70 55 52 49 20 71 75 65 72 79 20 70 61 72 61 6d 65  URI query parame
f8d80 74 65 72 20 61 76 61 69 6c 61 62 6c 65 20 74 6f  ter available to
f8d90 20 74 75 72 6e 20 69 74 20 6f 66 66 2e 0a 2a 2f   turn it off..*/
f8da0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
f8db0 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
f8dc0 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69  stics(sqlite3_fi
f8dd0 6c 65 20 2a 69 64 29 7b 0a 20 20 75 6e 69 78 46  le *id){.  unixF
f8de0 69 6c 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 69  ile *p = (unixFi
f8df0 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63  le*)id;.  int rc
f8e00 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 5f 5f 51   = 0;.#ifdef __Q
f8e10 4e 58 4e 54 4f 5f 5f 0a 20 20 69 66 28 20 70 2d  NXNTO__.  if( p-
f8e20 3e 73 65 63 74 6f 72 53 69 7a 65 3d 3d 30 20 29  >sectorSize==0 )
f8e30 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 28   unixSectorSize(
f8e40 69 64 29 3b 0a 20 20 72 63 20 3d 20 70 2d 3e 64  id);.  rc = p->d
f8e50 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
f8e60 74 69 63 73 3b 0a 23 65 6e 64 69 66 0a 20 20 69  tics;.#endif.  i
f8e70 66 28 20 70 2d 3e 63 74 72 6c 46 6c 61 67 73 20  f( p->ctrlFlags 
f8e80 26 20 55 4e 49 58 46 49 4c 45 5f 50 53 4f 57 20  & UNIXFILE_PSOW 
f8e90 29 7b 0a 20 20 20 20 72 63 20 7c 3d 20 53 51 4c  ){.    rc |= SQL
f8ea0 49 54 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52 53  ITE_IOCAP_POWERS
f8eb0 41 46 45 5f 4f 56 45 52 57 52 49 54 45 3b 0a 20  AFE_OVERWRITE;. 
f8ec0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
f8ed0 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
f8ee0 45 5f 4f 4d 49 54 5f 57 41 4c 0a 0a 0a 2f 2a 0a  E_OMIT_WAL.../*.
f8ef0 2a 2a 20 4f 62 6a 65 63 74 20 75 73 65 64 20 74  ** Object used t
f8f00 6f 20 72 65 70 72 65 73 65 6e 74 20 61 6e 20 73  o represent an s
f8f10 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 62 75 66  hared memory buf
f8f20 66 65 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 68 65  fer.  .**.** Whe
f8f30 6e 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 61  n multiple threa
f8f40 64 73 20 61 6c 6c 20 72 65 66 65 72 65 6e 63 65  ds all reference
f8f50 20 74 68 65 20 73 61 6d 65 20 77 61 6c 2d 69 6e   the same wal-in
f8f60 64 65 78 2c 20 65 61 63 68 20 74 68 72 65 61 64  dex, each thread
f8f70 0a 2a 2a 20 68 61 73 20 69 74 73 20 6f 77 6e 20  .** has its own 
f8f80 75 6e 69 78 53 68 6d 20 6f 62 6a 65 63 74 2c 20  unixShm object, 
f8f90 62 75 74 20 74 68 65 79 20 61 6c 6c 20 70 6f 69  but they all poi
f8fa0 6e 74 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 69  nt to a single i
f8fb0 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68  nstance.** of th
f8fc0 69 73 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 6f  is unixShmNode o
f8fd0 62 6a 65 63 74 2e 20 20 49 6e 20 6f 74 68 65 72  bject.  In other
f8fe0 20 77 6f 72 64 73 2c 20 65 61 63 68 20 77 61 6c   words, each wal
f8ff0 2d 69 6e 64 65 78 20 69 73 20 6f 70 65 6e 65 64  -index is opened
f9000 0a 2a 2a 20 6f 6e 6c 79 20 6f 6e 63 65 20 70 65  .** only once pe
f9010 72 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a  r process..**.**
f9020 20 45 61 63 68 20 75 6e 69 78 53 68 6d 4e 6f 64   Each unixShmNod
f9030 65 20 6f 62 6a 65 63 74 20 69 73 20 63 6f 6e 6e  e object is conn
f9040 65 63 74 65 64 20 74 6f 20 61 20 73 69 6e 67 6c  ected to a singl
f9050 65 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20  e unixInodeInfo 
f9060 6f 62 6a 65 63 74 2e 0a 2a 2a 20 57 65 20 63 6f  object..** We co
f9070 75 6c 64 20 63 6f 61 6c 65 73 63 65 20 74 68 69  uld coalesce thi
f9080 73 20 6f 62 6a 65 63 74 20 69 6e 74 6f 20 75 6e  s object into un
f9090 69 78 49 6e 6f 64 65 49 6e 66 6f 2c 20 62 75 74  ixInodeInfo, but
f90a0 20 74 68 61 74 20 77 6f 75 6c 64 20 6d 65 61 6e   that would mean
f90b0 0a 2a 2a 20 65 76 65 72 79 20 6f 70 65 6e 20 66  .** every open f
f90c0 69 6c 65 20 74 68 61 74 20 64 6f 65 73 20 6e 6f  ile that does no
f90d0 74 20 75 73 65 20 73 68 61 72 65 64 20 6d 65 6d  t use shared mem
f90e0 6f 72 79 20 28 69 6e 20 6f 74 68 65 72 20 77 6f  ory (in other wo
f90f0 72 64 73 2c 20 6d 6f 73 74 0a 2a 2a 20 6f 70 65  rds, most.** ope
f9100 6e 20 66 69 6c 65 73 29 20 77 6f 75 6c 64 20 68  n files) would h
f9110 61 76 65 20 74 6f 20 63 61 72 72 79 20 61 72 6f  ave to carry aro
f9120 75 6e 64 20 74 68 69 73 20 65 78 74 72 61 20 69  und this extra i
f9130 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 53 6f 0a  nformation.  So.
f9140 2a 2a 20 74 68 65 20 75 6e 69 78 49 6e 6f 64 65  ** the unixInode
f9150 49 6e 66 6f 20 6f 62 6a 65 63 74 20 63 6f 6e 74  Info object cont
f9160 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ains a pointer t
f9170 6f 20 74 68 69 73 20 75 6e 69 78 53 68 6d 4e 6f  o this unixShmNo
f9180 64 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64  de object.** and
f9190 20 74 68 65 20 75 6e 69 78 53 68 6d 4e 6f 64 65   the unixShmNode
f91a0 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74   object is creat
f91b0 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 6e 65 65  ed only when nee
f91c0 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 75 6e 69 78 4d  ded..**.** unixM
f91d0 75 74 65 78 48 65 6c 64 28 29 20 6d 75 73 74 20  utexHeld() must 
f91e0 62 65 20 74 72 75 65 20 77 68 65 6e 20 63 72 65  be true when cre
f91f0 61 74 69 6e 67 20 6f 72 20 64 65 73 74 72 6f 79  ating or destroy
f9200 69 6e 67 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65  ing.** this obje
f9210 63 74 20 6f 72 20 77 68 69 6c 65 20 72 65 61 64  ct or while read
f9220 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 74  ing or writing t
f9230 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65  he following fie
f9240 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  lds:.**.**      
f9250 6e 52 65 66 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  nRef.**.** The f
f9260 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 20  ollowing fields 
f9270 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20 61 66  are read-only af
f9280 74 65 72 20 74 68 65 20 6f 62 6a 65 63 74 20 69  ter the object i
f9290 73 20 63 72 65 61 74 65 64 3a 0a 2a 2a 20 0a 2a  s created:.** .*
f92a0 2a 20 20 20 20 20 20 66 69 64 0a 2a 2a 20 20 20  *      fid.**   
f92b0 20 20 20 7a 46 69 6c 65 6e 61 6d 65 0a 2a 2a 0a     zFilename.**.
f92c0 2a 2a 20 45 69 74 68 65 72 20 75 6e 69 78 53 68  ** Either unixSh
f92d0 6d 4e 6f 64 65 2e 6d 75 74 65 78 20 6d 75 73 74  mNode.mutex must
f92e0 20 62 65 20 68 65 6c 64 20 6f 72 20 75 6e 69 78   be held or unix
f92f0 53 68 6d 4e 6f 64 65 2e 6e 52 65 66 3d 3d 30 20  ShmNode.nRef==0 
f9300 61 6e 64 0a 2a 2a 20 75 6e 69 78 4d 75 74 65 78  and.** unixMutex
f9310 48 65 6c 64 28 29 20 69 73 20 74 72 75 65 20 77  Held() is true w
f9320 68 65 6e 20 72 65 61 64 69 6e 67 20 6f 72 20 77  hen reading or w
f9330 72 69 74 69 6e 67 20 61 6e 79 20 6f 74 68 65 72  riting any other
f9340 20 66 69 65 6c 64 0a 2a 2a 20 69 6e 20 74 68 69   field.** in thi
f9350 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  s structure..*/.
f9360 73 74 72 75 63 74 20 75 6e 69 78 53 68 6d 4e 6f  struct unixShmNo
f9370 64 65 20 7b 0a 20 20 75 6e 69 78 49 6e 6f 64 65  de {.  unixInode
f9380 49 6e 66 6f 20 2a 70 49 6e 6f 64 65 3b 20 20 20  Info *pInode;   
f9390 20 20 2f 2a 20 75 6e 69 78 49 6e 6f 64 65 49 6e    /* unixInodeIn
f93a0 66 6f 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69  fo that owns thi
f93b0 73 20 53 48 4d 20 6e 6f 64 65 20 2a 2f 0a 20 20  s SHM node */.  
f93c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
f93d0 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 4d 75  utex;      /* Mu
f93e0 74 65 78 20 74 6f 20 61 63 63 65 73 73 20 74 68  tex to access th
f93f0 69 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63  is object */.  c
f9400 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 20  har *zFilename; 
f9410 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
f9420 65 20 6f 66 20 74 68 65 20 6d 6d 61 70 70 65 64  e of the mmapped
f9430 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 68   file */.  int h
f9440 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
f9450 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69        /* Open fi
f9460 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f  le descriptor */
f9470 0a 20 20 69 6e 74 20 73 7a 52 65 67 69 6f 6e 3b  .  int szRegion;
f9480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f9490 20 53 69 7a 65 20 6f 66 20 73 68 61 72 65 64 2d   Size of shared-
f94a0 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 73 20 2a  memory regions *
f94b0 2f 0a 20 20 75 31 36 20 6e 52 65 67 69 6f 6e 3b  /.  u16 nRegion;
f94c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f94d0 2a 20 53 69 7a 65 20 6f 66 20 61 72 72 61 79 20  * Size of array 
f94e0 61 70 52 65 67 69 6f 6e 20 2a 2f 0a 20 20 75 38  apRegion */.  u8
f94f0 20 69 73 52 65 61 64 6f 6e 6c 79 3b 20 20 20 20   isReadonly;    
f9500 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
f9510 20 69 66 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f   if read-only */
f9520 0a 20 20 63 68 61 72 20 2a 2a 61 70 52 65 67 69  .  char **apRegi
f9530 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  on;           /*
f9540 20 41 72 72 61 79 20 6f 66 20 6d 61 70 70 65 64   Array of mapped
f9550 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 72   shared-memory r
f9560 65 67 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  egions */.  int 
f9570 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
f9580 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
f9590 20 6f 66 20 75 6e 69 78 53 68 6d 20 6f 62 6a 65   of unixShm obje
f95a0 63 74 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  cts pointing to 
f95b0 74 68 69 73 20 2a 2f 0a 20 20 75 6e 69 78 53 68  this */.  unixSh
f95c0 6d 20 2a 70 46 69 72 73 74 3b 20 20 20 20 20 20  m *pFirst;      
f95d0 20 20 20 20 20 2f 2a 20 41 6c 6c 20 75 6e 69 78       /* All unix
f95e0 53 68 6d 20 6f 62 6a 65 63 74 73 20 70 6f 69 6e  Shm objects poin
f95f0 74 69 6e 67 20 74 6f 20 74 68 69 73 20 2a 2f 0a  ting to this */.
f9600 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
f9610 42 55 47 0a 20 20 75 38 20 65 78 63 6c 4d 61 73  BUG.  u8 exclMas
f9620 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
f9630 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 65 78 63 6c   /* Mask of excl
f9640 75 73 69 76 65 20 6c 6f 63 6b 73 20 68 65 6c 64  usive locks held
f9650 20 2a 2f 0a 20 20 75 38 20 73 68 61 72 65 64 4d   */.  u8 sharedM
f9660 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ask;            
f9670 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 73 68 61 72   /* Mask of shar
f9680 65 64 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f  ed locks held */
f9690 0a 20 20 75 38 20 6e 65 78 74 53 68 6d 49 64 3b  .  u8 nextShmId;
f96a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f96b0 20 4e 65 78 74 20 61 76 61 69 6c 61 62 6c 65 20   Next available 
f96c0 75 6e 69 78 53 68 6d 2e 69 64 20 76 61 6c 75 65  unixShm.id value
f96d0 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f   */.#endif.};../
f96e0 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 75  *.** Structure u
f96f0 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62  sed internally b
f9700 79 20 74 68 69 73 20 56 46 53 20 74 6f 20 72 65  y this VFS to re
f9710 63 6f 72 64 20 74 68 65 20 73 74 61 74 65 20 6f  cord the state o
f9720 66 20 61 6e 0a 2a 2a 20 6f 70 65 6e 20 73 68 61  f an.** open sha
f9730 72 65 64 20 6d 65 6d 6f 72 79 20 63 6f 6e 6e 65  red memory conne
f9740 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ction..**.** The
f9750 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64   following field
f9760 73 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65  s are initialize
f9770 64 20 77 68 65 6e 20 74 68 69 73 20 6f 62 6a 65  d when this obje
f9780 63 74 20 69 73 20 63 72 65 61 74 65 64 20 61 6e  ct is created an
f9790 64 0a 2a 2a 20 61 72 65 20 72 65 61 64 2d 6f 6e  d.** are read-on
f97a0 6c 79 20 74 68 65 72 65 61 66 74 65 72 3a 0a 2a  ly thereafter:.*
f97b0 2a 0a 2a 2a 20 20 20 20 75 6e 69 78 53 68 6d 2e  *.**    unixShm.
f97c0 70 46 69 6c 65 0a 2a 2a 20 20 20 20 75 6e 69 78  pFile.**    unix
f97d0 53 68 6d 2e 69 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c  Shm.id.**.** All
f97e0 20 6f 74 68 65 72 20 66 69 65 6c 64 73 20 61 72   other fields ar
f97f0 65 20 72 65 61 64 2f 77 72 69 74 65 2e 20 20 54  e read/write.  T
f9800 68 65 20 75 6e 69 78 53 68 6d 2e 70 46 69 6c 65  he unixShm.pFile
f9810 2d 3e 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  ->mutex must be 
f9820 68 65 6c 64 0a 2a 2a 20 77 68 69 6c 65 20 61 63  held.** while ac
f9830 63 65 73 73 69 6e 67 20 61 6e 79 20 72 65 61 64  cessing any read
f9840 2f 77 72 69 74 65 20 66 69 65 6c 64 73 2e 0a 2a  /write fields..*
f9850 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 53 68 6d  /.struct unixShm
f9860 20 7b 0a 20 20 75 6e 69 78 53 68 6d 4e 6f 64 65   {.  unixShmNode
f9870 20 2a 70 53 68 6d 4e 6f 64 65 3b 20 20 20 20 20   *pShmNode;     
f9880 2f 2a 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e  /* The underlyin
f9890 67 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 6f 62  g unixShmNode ob
f98a0 6a 65 63 74 20 2a 2f 0a 20 20 75 6e 69 78 53 68  ject */.  unixSh
f98b0 6d 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20  m *pNext;       
f98c0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 75 6e 69       /* Next uni
f98d0 78 53 68 6d 20 77 69 74 68 20 74 68 65 20 73 61  xShm with the sa
f98e0 6d 65 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 2a  me unixShmNode *
f98f0 2f 0a 20 20 75 38 20 68 61 73 4d 75 74 65 78 3b  /.  u8 hasMutex;
f9900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f9910 2a 20 54 72 75 65 20 69 66 20 68 6f 6c 64 69 6e  * True if holdin
f9920 67 20 74 68 65 20 75 6e 69 78 53 68 6d 4e 6f 64  g the unixShmNod
f9930 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 75 38 20  e mutex */.  u8 
f9940 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  id;             
f9950 20 20 20 20 20 20 20 20 2f 2a 20 49 64 20 6f 66          /* Id of
f9960 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e   this connection
f9970 20 77 69 74 68 69 6e 20 69 74 73 20 75 6e 69 78   within its unix
f9980 53 68 6d 4e 6f 64 65 20 2a 2f 0a 20 20 75 31 36  ShmNode */.  u16
f9990 20 73 68 61 72 65 64 4d 61 73 6b 3b 20 20 20 20   sharedMask;    
f99a0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
f99b0 6f 66 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20  of shared locks 
f99c0 68 65 6c 64 20 2a 2f 0a 20 20 75 31 36 20 65 78  held */.  u16 ex
f99d0 63 6c 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20  clMask;         
f99e0 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
f99f0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 73 20  exclusive locks 
f9a00 68 65 6c 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  held */.};../*.*
f9a10 2a 20 43 6f 6e 73 74 61 6e 74 73 20 75 73 65 64  * Constants used
f9a20 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 0a 2a 2f 0a   for locking.*/.
f9a30 23 64 65 66 69 6e 65 20 55 4e 49 58 5f 53 48 4d  #define UNIX_SHM
f9a40 5f 42 41 53 45 20 20 20 28 28 32 32 2b 53 51 4c  _BASE   ((22+SQL
f9a50 49 54 45 5f 53 48 4d 5f 4e 4c 4f 43 4b 29 2a 34  ITE_SHM_NLOCK)*4
f9a60 29 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 72  )         /* fir
f9a70 73 74 20 6c 6f 63 6b 20 62 79 74 65 20 2a 2f 0a  st lock byte */.
f9a80 23 64 65 66 69 6e 65 20 55 4e 49 58 5f 53 48 4d  #define UNIX_SHM
f9a90 5f 44 4d 53 20 20 20 20 28 55 4e 49 58 5f 53 48  _DMS    (UNIX_SH
f9aa0 4d 5f 42 41 53 45 2b 53 51 4c 49 54 45 5f 53 48  M_BASE+SQLITE_SH
f9ab0 4d 5f 4e 4c 4f 43 4b 29 20 20 2f 2a 20 64 65 61  M_NLOCK)  /* dea
f9ac0 64 6d 61 6e 20 73 77 69 74 63 68 20 2a 2f 0a 0a  dman switch */..
f9ad0 2f 2a 0a 2a 2a 20 41 70 70 6c 79 20 70 6f 73 69  /*.** Apply posi
f9ae0 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73  x advisory locks
f9af0 20 66 6f 72 20 61 6c 6c 20 62 79 74 65 73 20 66   for all bytes f
f9b00 72 6f 6d 20 6f 66 73 74 20 74 68 72 6f 75 67 68  rom ofst through
f9b10 20 6f 66 73 74 2b 6e 2d 31 2e 0a 2a 2a 0a 2a 2a   ofst+n-1..**.**
f9b20 20 4c 6f 63 6b 73 20 62 6c 6f 63 6b 20 69 66 20   Locks block if 
f9b30 74 68 65 20 6d 61 73 6b 20 69 73 20 65 78 61 63  the mask is exac
f9b40 74 6c 79 20 55 4e 49 58 5f 53 48 4d 5f 43 20 61  tly UNIX_SHM_C a
f9b50 6e 64 20 61 72 65 20 6e 6f 6e 2d 62 6c 6f 63 6b  nd are non-block
f9b60 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65  ing.** otherwise
f9b70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
f9b80 75 6e 69 78 53 68 6d 53 79 73 74 65 6d 4c 6f 63  unixShmSystemLoc
f9b90 6b 28 0a 20 20 75 6e 69 78 53 68 6d 4e 6f 64 65  k(.  unixShmNode
f9ba0 20 2a 70 53 68 6d 4e 6f 64 65 2c 20 2f 2a 20 41   *pShmNode, /* A
f9bb0 70 70 6c 79 20 6c 6f 63 6b 73 20 74 6f 20 74 68  pply locks to th
f9bc0 69 73 20 6f 70 65 6e 20 73 68 61 72 65 64 2d 6d  is open shared-m
f9bd0 65 6d 6f 72 79 20 73 65 67 6d 65 6e 74 20 2a 2f  emory segment */
f9be0 0a 20 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 2c  .  int lockType,
f9bf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 5f 55            /* F_U
f9c00 4e 4c 43 4b 2c 20 46 5f 52 44 4c 43 4b 2c 20 6f  NLCK, F_RDLCK, o
f9c10 72 20 46 5f 57 52 4c 43 4b 20 2a 2f 0a 20 20 69  r F_WRLCK */.  i
f9c20 6e 74 20 6f 66 73 74 2c 20 20 20 20 20 20 20 20  nt ofst,        
f9c30 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62        /* First b
f9c40 79 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69  yte of the locki
f9c50 6e 67 20 72 61 6e 67 65 20 2a 2f 0a 20 20 69 6e  ng range */.  in
f9c60 74 20 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  t n             
f9c70 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
f9c80 66 20 62 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20  f bytes to lock 
f9c90 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 66  */.){.  struct f
f9ca0 6c 6f 63 6b 20 66 3b 20 20 20 20 20 20 20 2f 2a  lock f;       /*
f9cb0 20 54 68 65 20 70 6f 73 69 78 20 61 64 76 69 73   The posix advis
f9cc0 6f 72 79 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75  ory locking stru
f9cd0 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72  cture */.  int r
f9ce0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
f9cf0 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20   /* Result code 
f9d00 66 6f 72 6d 20 66 63 6e 74 6c 28 29 20 2a 2f 0a  form fcntl() */.
f9d10 0a 20 20 2f 2a 20 41 63 63 65 73 73 20 74 6f 20  .  /* Access to 
f9d20 74 68 65 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20  the unixShmNode 
f9d30 6f 62 6a 65 63 74 20 69 73 20 73 65 72 69 61 6c  object is serial
f9d40 69 7a 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c  ized by the call
f9d50 65 72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  er */.  assert( 
f9d60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
f9d70 6c 64 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74  ld(pShmNode->mut
f9d80 65 78 29 20 7c 7c 20 70 53 68 6d 4e 6f 64 65 2d  ex) || pShmNode-
f9d90 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 0a 20 20 2f  >nRef==0 );..  /
f9da0 2a 20 53 68 61 72 65 64 20 6c 6f 63 6b 73 20 6e  * Shared locks n
f9db0 65 76 65 72 20 73 70 61 6e 20 6d 6f 72 65 20 74  ever span more t
f9dc0 68 61 6e 20 6f 6e 65 20 62 79 74 65 20 2a 2f 0a  han one byte */.
f9dd0 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 31 20 7c    assert( n==1 |
f9de0 7c 20 6c 6f 63 6b 54 79 70 65 21 3d 46 5f 52 44  | lockType!=F_RD
f9df0 4c 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63  LCK );..  /* Loc
f9e00 6b 73 20 61 72 65 20 77 69 74 68 69 6e 20 72 61  ks are within ra
f9e10 6e 67 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  nge */.  assert(
f9e20 20 6e 3e 3d 31 20 26 26 20 6e 3c 53 51 4c 49 54   n>=1 && n<SQLIT
f9e30 45 5f 53 48 4d 5f 4e 4c 4f 43 4b 20 29 3b 0a 0a  E_SHM_NLOCK );..
f9e40 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e    if( pShmNode->
f9e50 68 3e 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49  h>=0 ){.    /* I
f9e60 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6c 6f  nitialize the lo
f9e70 63 6b 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73  cking parameters
f9e80 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26   */.    memset(&
f9e90 66 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 29 29  f, 0, sizeof(f))
f9ea0 3b 0a 20 20 20 20 66 2e 6c 5f 74 79 70 65 20 3d  ;.    f.l_type =
f9eb0 20 6c 6f 63 6b 54 79 70 65 3b 0a 20 20 20 20 66   lockType;.    f
f9ec0 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b  .l_whence = SEEK
f9ed0 5f 53 45 54 3b 0a 20 20 20 20 66 2e 6c 5f 73 74  _SET;.    f.l_st
f9ee0 61 72 74 20 3d 20 6f 66 73 74 3b 0a 20 20 20 20  art = ofst;.    
f9ef0 66 2e 6c 5f 6c 65 6e 20 3d 20 6e 3b 0a 0a 20 20  f.l_len = n;..  
f9f00 20 20 72 63 20 3d 20 6f 73 46 63 6e 74 6c 28 70    rc = osFcntl(p
f9f10 53 68 6d 4e 6f 64 65 2d 3e 68 2c 20 46 5f 53 45  ShmNode->h, F_SE
f9f20 54 4c 4b 2c 20 26 66 29 3b 0a 20 20 20 20 72 63  TLK, &f);.    rc
f9f30 20 3d 20 28 72 63 21 3d 28 2d 31 29 29 20 3f 20   = (rc!=(-1)) ? 
f9f40 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49  SQLITE_OK : SQLI
f9f50 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 20  TE_BUSY;.  }..  
f9f60 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 67 6c  /* Update the gl
f9f70 6f 62 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 20  obal lock state 
f9f80 61 6e 64 20 64 6f 20 64 65 62 75 67 20 74 72 61  and do debug tra
f9f90 63 69 6e 67 20 2a 2f 0a 23 69 66 64 65 66 20 53  cing */.#ifdef S
f9fa0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 7b 20  QLITE_DEBUG.  { 
f9fb0 75 31 36 20 6d 61 73 6b 3b 0a 20 20 4f 53 54 52  u16 mask;.  OSTR
f9fc0 41 43 45 28 28 22 53 48 4d 2d 4c 4f 43 4b 20 22  ACE(("SHM-LOCK "
f9fd0 29 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 28 31 3c  ));.  mask = (1<
f9fe0 3c 28 6f 66 73 74 2b 6e 29 29 20 2d 20 28 31 3c  <(ofst+n)) - (1<
f9ff0 3c 6f 66 73 74 29 3b 0a 20 20 69 66 28 20 72 63  <ofst);.  if( rc
fa000 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
fa010 20 20 20 69 66 28 20 6c 6f 63 6b 54 79 70 65 3d     if( lockType=
fa020 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20  =F_UNLCK ){.    
fa030 20 20 4f 53 54 52 41 43 45 28 28 22 75 6e 6c 6f    OSTRACE(("unlo
fa040 63 6b 20 25 64 20 6f 6b 22 2c 20 6f 66 73 74 29  ck %d ok", ofst)
fa050 29 3b 0a 20 20 20 20 20 20 70 53 68 6d 4e 6f 64  );.      pShmNod
fa060 65 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 3d 20 7e  e->exclMask &= ~
fa070 6d 61 73 6b 3b 0a 20 20 20 20 20 20 70 53 68 6d  mask;.      pShm
fa080 4e 6f 64 65 2d 3e 73 68 61 72 65 64 4d 61 73 6b  Node->sharedMask
fa090 20 26 3d 20 7e 6d 61 73 6b 3b 0a 20 20 20 20 7d   &= ~mask;.    }
fa0a0 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 54 79 70  else if( lockTyp
fa0b0 65 3d 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20  e==F_RDLCK ){.  
fa0c0 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 72 65      OSTRACE(("re
fa0d0 61 64 2d 6c 6f 63 6b 20 25 64 20 6f 6b 22 2c 20  ad-lock %d ok", 
fa0e0 6f 66 73 74 29 29 3b 0a 20 20 20 20 20 20 70 53  ofst));.      pS
fa0f0 68 6d 4e 6f 64 65 2d 3e 65 78 63 6c 4d 61 73 6b  hmNode->exclMask
fa100 20 26 3d 20 7e 6d 61 73 6b 3b 0a 20 20 20 20 20   &= ~mask;.     
fa110 20 70 53 68 6d 4e 6f 64 65 2d 3e 73 68 61 72 65   pShmNode->share
fa120 64 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20  dMask |= mask;. 
fa130 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
fa140 61 73 73 65 72 74 28 20 6c 6f 63 6b 54 79 70 65  assert( lockType
fa150 3d 3d 46 5f 57 52 4c 43 4b 20 29 3b 0a 20 20 20  ==F_WRLCK );.   
fa160 20 20 20 4f 53 54 52 41 43 45 28 28 22 77 72 69     OSTRACE(("wri
fa170 74 65 2d 6c 6f 63 6b 20 25 64 20 6f 6b 22 2c 20  te-lock %d ok", 
fa180 6f 66 73 74 29 29 3b 0a 20 20 20 20 20 20 70 53  ofst));.      pS
fa190 68 6d 4e 6f 64 65 2d 3e 65 78 63 6c 4d 61 73 6b  hmNode->exclMask
fa1a0 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20   |= mask;.      
fa1b0 70 53 68 6d 4e 6f 64 65 2d 3e 73 68 61 72 65 64  pShmNode->shared
fa1c0 4d 61 73 6b 20 26 3d 20 7e 6d 61 73 6b 3b 0a 20  Mask &= ~mask;. 
fa1d0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
fa1e0 20 20 69 66 28 20 6c 6f 63 6b 54 79 70 65 3d 3d    if( lockType==
fa1f0 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20  F_UNLCK ){.     
fa200 20 4f 53 54 52 41 43 45 28 28 22 75 6e 6c 6f 63   OSTRACE(("unloc
fa210 6b 20 25 64 20 66 61 69 6c 65 64 22 2c 20 6f 66  k %d failed", of
fa220 73 74 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  st));.    }else 
fa230 69 66 28 20 6c 6f 63 6b 54 79 70 65 3d 3d 46 5f  if( lockType==F_
fa240 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 4f  RDLCK ){.      O
fa250 53 54 52 41 43 45 28 28 22 72 65 61 64 2d 6c 6f  STRACE(("read-lo
fa260 63 6b 20 66 61 69 6c 65 64 22 29 29 3b 0a 20 20  ck failed"));.  
fa270 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
fa280 73 73 65 72 74 28 20 6c 6f 63 6b 54 79 70 65 3d  ssert( lockType=
fa290 3d 46 5f 57 52 4c 43 4b 20 29 3b 0a 20 20 20 20  =F_WRLCK );.    
fa2a0 20 20 4f 53 54 52 41 43 45 28 28 22 77 72 69 74    OSTRACE(("writ
fa2b0 65 2d 6c 6f 63 6b 20 25 64 20 66 61 69 6c 65 64  e-lock %d failed
fa2c0 22 2c 20 6f 66 73 74 29 29 3b 0a 20 20 20 20 7d  ", ofst));.    }
fa2d0 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 28 28  .  }.  OSTRACE((
fa2e0 22 20 2d 20 61 66 74 65 72 77 61 72 64 73 20 25  " - afterwards %
fa2f0 30 33 78 2c 25 30 33 78 5c 6e 22 2c 0a 20 20 20  03x,%03x\n",.   
fa300 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 65          pShmNode
fa310 2d 3e 73 68 61 72 65 64 4d 61 73 6b 2c 20 70 53  ->sharedMask, pS
fa320 68 6d 4e 6f 64 65 2d 3e 65 78 63 6c 4d 61 73 6b  hmNode->exclMask
fa330 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ));.  }.#endif..
fa340 20 20 72 65 74 75 72 6e 20 72 63 3b 20 20 20 20    return rc;    
fa350 20 20 20 20 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 50      .}.../*.** P
fa360 75 72 67 65 20 74 68 65 20 75 6e 69 78 53 68 6d  urge the unixShm
fa370 4e 6f 64 65 4c 69 73 74 20 6c 69 73 74 20 6f 66  NodeList list of
fa380 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 77 69 74   all entries wit
fa390 68 20 75 6e 69 78 53 68 6d 4e 6f 64 65 2e 6e 52  h unixShmNode.nR
fa3a0 65 66 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ef==0..**.** Thi
fa3b0 73 20 69 73 20 6e 6f 74 20 61 20 56 46 53 20 73  s is not a VFS s
fa3c0 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 6d 65 74  hared-memory met
fa3d0 68 6f 64 3b 20 69 74 20 69 73 20 61 20 75 74 69  hod; it is a uti
fa3e0 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 20 63 61  lity function ca
fa3f0 6c 6c 65 64 0a 2a 2a 20 62 79 20 56 46 53 20 73  lled.** by VFS s
fa400 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 6d 65 74  hared-memory met
fa410 68 6f 64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  hods..*/.static 
fa420 76 6f 69 64 20 75 6e 69 78 53 68 6d 50 75 72 67  void unixShmPurg
fa430 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 64 29  e(unixFile *pFd)
fa440 7b 0a 20 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20  {.  unixShmNode 
fa450 2a 70 20 3d 20 70 46 64 2d 3e 70 49 6e 6f 64 65  *p = pFd->pInode
fa460 2d 3e 70 53 68 6d 4e 6f 64 65 3b 0a 20 20 61 73  ->pShmNode;.  as
fa470 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48  sert( unixMutexH
fa480 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 28 20 70  eld() );.  if( p
fa490 20 26 26 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29   && p->nRef==0 )
fa4a0 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
fa4b0 20 61 73 73 65 72 74 28 20 70 2d 3e 70 49 6e 6f   assert( p->pIno
fa4c0 64 65 3d 3d 70 46 64 2d 3e 70 49 6e 6f 64 65 20  de==pFd->pInode 
fa4d0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
fa4e0 75 74 65 78 5f 66 72 65 65 28 70 2d 3e 6d 75 74  utex_free(p->mut
fa4f0 65 78 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  ex);.    for(i=0
fa500 3b 20 69 3c 70 2d 3e 6e 52 65 67 69 6f 6e 3b 20  ; i<p->nRegion; 
fa510 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
fa520 70 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20  p->h>=0 ){.     
fa530 20 20 20 6d 75 6e 6d 61 70 28 70 2d 3e 61 70 52     munmap(p->apR
fa540 65 67 69 6f 6e 5b 69 5d 2c 20 70 2d 3e 73 7a 52  egion[i], p->szR
fa550 65 67 69 6f 6e 29 3b 0a 20 20 20 20 20 20 7d 65  egion);.      }e
fa560 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
fa570 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 70 52  ite3_free(p->apR
fa580 65 67 69 6f 6e 5b 69 5d 29 3b 0a 20 20 20 20 20  egion[i]);.     
fa590 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
fa5a0 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 70 52  ite3_free(p->apR
fa5b0 65 67 69 6f 6e 29 3b 0a 20 20 20 20 69 66 28 20  egion);.    if( 
fa5c0 70 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20  p->h>=0 ){.     
fa5d0 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 46   robust_close(pF
fa5e0 64 2c 20 70 2d 3e 68 2c 20 5f 5f 4c 49 4e 45 5f  d, p->h, __LINE_
fa5f0 5f 29 3b 0a 20 20 20 20 20 20 70 2d 3e 68 20 3d  _);.      p->h =
fa600 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70   -1;.    }.    p
fa610 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53 68 6d 4e 6f  ->pInode->pShmNo
fa620 64 65 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  de = 0;.    sqli
fa630 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d  te3_free(p);.  }
fa640 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61  .}../*.** Open a
fa650 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 61   shared-memory a
fa660 72 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  rea associated w
fa670 69 74 68 20 6f 70 65 6e 20 64 61 74 61 62 61 73  ith open databas
fa680 65 20 66 69 6c 65 20 70 44 62 46 64 2e 20 20 0a  e file pDbFd.  .
fa690 2a 2a 20 54 68 69 73 20 70 61 72 74 69 63 75 6c  ** This particul
fa6a0 61 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ar implementatio
fa6b0 6e 20 75 73 65 73 20 6d 6d 61 70 70 65 64 20 66  n uses mmapped f
fa6c0 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  iles..**.** The 
fa6d0 66 69 6c 65 20 75 73 65 64 20 74 6f 20 69 6d 70  file used to imp
fa6e0 6c 65 6d 65 6e 74 20 73 68 61 72 65 64 2d 6d 65  lement shared-me
fa6f0 6d 6f 72 79 20 69 73 20 69 6e 20 74 68 65 20 73  mory is in the s
fa700 61 6d 65 20 64 69 72 65 63 74 6f 72 79 0a 2a 2a  ame directory.**
fa710 20 61 73 20 74 68 65 20 6f 70 65 6e 20 64 61 74   as the open dat
fa720 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 68  abase file and h
fa730 61 73 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  as the same name
fa740 20 61 73 20 74 68 65 20 6f 70 65 6e 20 64 61 74   as the open dat
fa750 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 77 69  abase.** file wi
fa760 74 68 20 74 68 65 20 22 2d 73 68 6d 22 20 73 75  th the "-shm" su
fa770 66 66 69 78 20 61 64 64 65 64 2e 20 20 46 6f 72  ffix added.  For
fa780 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65   example, if the
fa790 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a   database file.*
fa7a0 2a 20 69 73 20 22 2f 68 6f 6d 65 2f 75 73 65 72  * is "/home/user
fa7b0 31 2f 63 6f 6e 66 69 67 2e 64 62 22 20 74 68 65  1/config.db" the
fa7c0 6e 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 20  n the file that 
fa7d0 69 73 20 63 72 65 61 74 65 64 20 61 6e 64 20 6d  is created and m
fa7e0 6d 61 70 70 65 64 0a 2a 2a 20 66 6f 72 20 73 68  mapped.** for sh
fa7f0 61 72 65 64 20 6d 65 6d 6f 72 79 20 77 69 6c 6c  ared memory will
fa800 20 62 65 20 63 61 6c 6c 65 64 20 22 2f 68 6f 6d   be called "/hom
fa810 65 2f 75 73 65 72 31 2f 63 6f 6e 66 69 67 2e 64  e/user1/config.d
fa820 62 2d 73 68 6d 22 2e 20 20 0a 2a 2a 0a 2a 2a 20  b-shm".  .**.** 
fa830 41 6e 6f 74 68 65 72 20 61 70 70 72 6f 61 63 68  Another approach
fa840 20 74 6f 20 69 73 20 74 6f 20 75 73 65 20 66 69   to is to use fi
fa850 6c 65 73 20 69 6e 20 2f 64 65 76 2f 73 68 6d 20  les in /dev/shm 
fa860 6f 72 20 2f 64 65 76 2f 74 6d 70 20 6f 72 20 61  or /dev/tmp or a
fa870 6e 0a 2a 2a 20 73 6f 6d 65 20 6f 74 68 65 72 20  n.** some other 
fa880 74 6d 70 66 73 20 6d 6f 75 6e 74 2e 20 42 75 74  tmpfs mount. But
fa890 20 69 66 20 61 20 66 69 6c 65 20 69 6e 20 61 20   if a file in a 
fa8a0 64 69 66 66 65 72 65 6e 74 20 64 69 72 65 63 74  different direct
fa8b0 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ory.** from the 
fa8c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
fa8d0 20 75 73 65 64 2c 20 74 68 65 6e 20 64 69 66 66   used, then diff
fa8e0 65 72 69 6e 67 20 61 63 63 65 73 73 20 70 65 72  ering access per
fa8f0 6d 69 73 73 69 6f 6e 73 0a 2a 2a 20 6f 72 20 61  missions.** or a
fa900 20 63 68 72 6f 6f 74 28 29 20 6d 69 67 68 74 20   chroot() might 
fa910 63 61 75 73 65 20 74 77 6f 20 64 69 66 66 65 72  cause two differ
fa920 65 6e 74 20 70 72 6f 63 65 73 73 65 73 20 6f 6e  ent processes on
fa930 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 64 61 74   the same.** dat
fa940 61 62 61 73 65 20 74 6f 20 65 6e 64 20 75 70 20  abase to end up 
fa950 75 73 69 6e 67 20 64 69 66 66 65 72 65 6e 74 20  using different 
fa960 66 69 6c 65 73 20 66 6f 72 20 73 68 61 72 65 64  files for shared
fa970 20 6d 65 6d 6f 72 79 20 2d 20 0a 2a 2a 20 6d 65   memory - .** me
fa980 61 6e 69 6e 67 20 74 68 61 74 20 74 68 65 69 72  aning that their
fa990 20 6d 65 6d 6f 72 79 20 77 6f 75 6c 64 20 6e 6f   memory would no
fa9a0 74 20 72 65 61 6c 6c 79 20 62 65 20 73 68 61 72  t really be shar
fa9b0 65 64 20 2d 20 72 65 73 75 6c 74 69 6e 67 0a 2a  ed - resulting.*
fa9c0 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f  * in database co
fa9d0 72 72 75 70 74 69 6f 6e 2e 20 20 4e 65 76 65 72  rruption.  Never
fa9e0 74 68 65 6c 65 73 73 2c 20 74 68 69 73 20 74 6d  theless, this tm
fa9f0 70 66 73 20 66 69 6c 65 20 75 73 61 67 65 0a 2a  pfs file usage.*
faa00 2a 20 63 61 6e 20 62 65 20 65 6e 61 62 6c 65 64  * can be enabled
faa10 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65   at compile-time
faa20 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f   using -DSQLITE_
faa30 53 48 4d 5f 44 49 52 45 43 54 4f 52 59 3d 22 2f  SHM_DIRECTORY="/
faa40 64 65 76 2f 73 68 6d 22 0a 2a 2a 20 6f 72 20 74  dev/shm".** or t
faa50 68 65 20 65 71 75 69 76 61 6c 65 6e 74 2e 20 20  he equivalent.  
faa60 54 68 65 20 75 73 65 20 6f 66 20 74 68 65 20 53  The use of the S
faa70 51 4c 49 54 45 5f 53 48 4d 5f 44 49 52 45 43 54  QLITE_SHM_DIRECT
faa80 4f 52 59 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  ORY compile-time
faa90 0a 2a 2a 20 6f 70 74 69 6f 6e 20 72 65 73 75 6c  .** option resul
faaa0 74 73 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61  ts in an incompa
faab0 74 69 62 6c 65 20 62 75 69 6c 64 20 6f 66 20 53  tible build of S
faac0 51 4c 69 74 65 3b 20 20 62 75 69 6c 64 73 20 6f  QLite;  builds o
faad0 66 20 53 51 4c 69 74 65 0a 2a 2a 20 74 68 61 74  f SQLite.** that
faae0 20 77 69 74 68 20 64 69 66 66 65 72 69 6e 67 20   with differing 
faaf0 53 51 4c 49 54 45 5f 53 48 4d 5f 44 49 52 45 43  SQLITE_SHM_DIREC
fab00 54 4f 52 59 20 73 65 74 74 69 6e 67 73 20 61 74  TORY settings at
fab10 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65  tempt to use the
fab20 0a 2a 2a 20 73 61 6d 65 20 64 61 74 61 62 61 73  .** same databas
fab30 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 73 61  e file at the sa
fab40 6d 65 20 74 69 6d 65 2c 20 64 61 74 61 62 61 73  me time, databas
fab50 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 69 6c  e corruption wil
fab60 6c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75  l likely.** resu
fab70 6c 74 2e 20 54 68 65 20 53 51 4c 49 54 45 5f 53  lt. The SQLITE_S
fab80 48 4d 5f 44 49 52 45 43 54 4f 52 59 20 63 6f 6d  HM_DIRECTORY com
fab90 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e  pile-time option
faba0 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a   is considered.*
fabb0 2a 20 22 75 6e 73 75 70 70 6f 72 74 65 64 22 20  * "unsupported" 
fabc0 61 6e 64 20 6d 61 79 20 67 6f 20 61 77 61 79 20  and may go away 
fabd0 69 6e 20 61 20 66 75 74 75 72 65 20 53 51 4c 69  in a future SQLi
fabe0 74 65 20 72 65 6c 65 61 73 65 2e 0a 2a 2a 0a 2a  te release..**.*
fabf0 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61  * When opening a
fac00 20 6e 65 77 20 73 68 61 72 65 64 2d 6d 65 6d 6f   new shared-memo
fac10 72 79 20 66 69 6c 65 2c 20 69 66 20 6e 6f 20 6f  ry file, if no o
fac20 74 68 65 72 20 69 6e 73 74 61 6e 63 65 73 20 6f  ther instances o
fac30 66 20 74 68 61 74 0a 2a 2a 20 66 69 6c 65 20 61  f that.** file a
fac40 72 65 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65  re currently ope
fac50 6e 2c 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65  n, in this proce
fac60 73 73 20 6f 72 20 69 6e 20 6f 74 68 65 72 20 70  ss or in other p
fac70 72 6f 63 65 73 73 65 73 2c 20 74 68 65 6e 0a 2a  rocesses, then.*
fac80 2a 20 74 68 65 20 66 69 6c 65 20 6d 75 73 74 20  * the file must 
fac90 62 65 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20  be truncated to 
faca0 7a 65 72 6f 20 6c 65 6e 67 74 68 20 6f 72 20 68  zero length or h
facb0 61 76 65 20 69 74 73 20 68 65 61 64 65 72 20 63  ave its header c
facc0 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  leared..**.** If
facd0 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61   the original da
face0 74 61 62 61 73 65 20 66 69 6c 65 20 28 70 44 62  tabase file (pDb
facf0 46 64 29 20 69 73 20 75 73 69 6e 67 20 74 68 65  Fd) is using the
fad00 20 22 75 6e 69 78 2d 65 78 63 6c 22 20 56 46 53   "unix-excl" VFS
fad10 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74  .** that means t
fad20 68 61 74 20 61 6e 20 65 78 63 6c 75 73 69 76 65  hat an exclusive
fad30 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e   lock is held on
fad40 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
fad50 6c 65 20 61 6e 64 0a 2a 2a 20 74 68 61 74 20 6e  le and.** that n
fad60 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65  o other processe
fad70 73 20 61 72 65 20 61 62 6c 65 20 74 6f 20 72 65  s are able to re
fad80 61 64 20 6f 72 20 77 72 69 74 65 20 74 68 65 20  ad or write the 
fad90 64 61 74 61 62 61 73 65 2e 20 20 49 6e 0a 2a 2a  database.  In.**
fada0 20 74 68 61 74 20 63 61 73 65 2c 20 77 65 20 64   that case, we d
fadb0 6f 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6e 65 65  o not really nee
fadc0 64 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 2e  d shared memory.
fadd0 20 20 4e 6f 20 73 68 61 72 65 64 20 6d 65 6d 6f    No shared memo
fade0 72 79 0a 2a 2a 20 66 69 6c 65 20 69 73 20 63 72  ry.** file is cr
fadf0 65 61 74 65 64 2e 20 20 54 68 65 20 73 68 61 72  eated.  The shar
fae00 65 64 20 6d 65 6d 6f 72 79 20 77 69 6c 6c 20 62  ed memory will b
fae10 65 20 73 69 6d 75 6c 61 74 65 64 20 77 69 74 68  e simulated with
fae20 20 68 65 61 70 20 6d 65 6d 6f 72 79 2e 0a 2a 2f   heap memory..*/
fae30 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
fae40 4f 70 65 6e 53 68 61 72 65 64 4d 65 6d 6f 72 79  OpenSharedMemory
fae50 28 75 6e 69 78 46 69 6c 65 20 2a 70 44 62 46 64  (unixFile *pDbFd
fae60 29 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78  ){.  struct unix
fae70 53 68 6d 20 2a 70 20 3d 20 30 3b 20 20 20 20 20  Shm *p = 0;     
fae80 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 6e       /* The conn
fae90 65 63 74 69 6f 6e 20 74 6f 20 62 65 20 6f 70 65  ection to be ope
faea0 6e 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ned */.  struct 
faeb0 75 6e 69 78 53 68 6d 4e 6f 64 65 20 2a 70 53 68  unixShmNode *pSh
faec0 6d 4e 6f 64 65 3b 20 20 20 2f 2a 20 54 68 65 20  mNode;   /* The 
faed0 75 6e 64 65 72 6c 79 69 6e 67 20 6d 6d 61 70 70  underlying mmapp
faee0 65 64 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74  ed file */.  int
faef0 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
faf00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
faf10 52 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 20  Result code */. 
faf20 20 75 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a   unixInodeInfo *
faf30 70 49 6e 6f 64 65 3b 20 20 20 20 20 20 20 20 20  pInode;         
faf40 20 2f 2a 20 54 68 65 20 69 6e 6f 64 65 20 6f 66   /* The inode of
faf50 20 66 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a   fd */.  char *z
faf60 53 68 6d 46 69 6c 65 6e 61 6d 65 3b 20 20 20 20  ShmFilename;    
faf70 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
faf80 20 6f 66 20 74 68 65 20 66 69 6c 65 20 75 73 65   of the file use
faf90 64 20 66 6f 72 20 53 48 4d 20 2a 2f 0a 20 20 69  d for SHM */.  i
fafa0 6e 74 20 6e 53 68 6d 46 69 6c 65 6e 61 6d 65 3b  nt nShmFilename;
fafb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
fafc0 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 53 48  * Size of the SH
fafd0 4d 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 62 79  M filename in by
fafe0 74 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c  tes */..  /* All
faff0 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20  ocate space for 
fb000 74 68 65 20 6e 65 77 20 75 6e 69 78 53 68 6d 20  the new unixShm 
fb010 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 70 20 3d  object. */.  p =
fb020 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
fb030 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20   sizeof(*p) );. 
fb040 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
fb050 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
fb060 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20  .  memset(p, 0, 
fb070 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 61  sizeof(*p));.  a
fb080 73 73 65 72 74 28 20 70 44 62 46 64 2d 3e 70 53  ssert( pDbFd->pS
fb090 68 6d 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 43  hm==0 );..  /* C
fb0a0 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 61  heck to see if a
fb0b0 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 6f 62 6a   unixShmNode obj
fb0c0 65 63 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  ect already exis
fb0d0 74 73 2e 20 52 65 75 73 65 20 61 6e 20 65 78 69  ts. Reuse an exi
fb0e0 73 74 69 6e 67 0a 20 20 2a 2a 20 6f 6e 65 20 69  sting.  ** one i
fb0f0 66 20 70 72 65 73 65 6e 74 2e 20 43 72 65 61 74  f present. Creat
fb100 65 20 61 20 6e 65 77 20 6f 6e 65 20 69 66 20 6e  e a new one if n
fb110 65 63 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20  ecessary..  */. 
fb120 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
fb130 29 3b 0a 20 20 70 49 6e 6f 64 65 20 3d 20 70 44  );.  pInode = pD
fb140 62 46 64 2d 3e 70 49 6e 6f 64 65 3b 0a 20 20 70  bFd->pInode;.  p
fb150 53 68 6d 4e 6f 64 65 20 3d 20 70 49 6e 6f 64 65  ShmNode = pInode
fb160 2d 3e 70 53 68 6d 4e 6f 64 65 3b 0a 20 20 69 66  ->pShmNode;.  if
fb170 28 20 70 53 68 6d 4e 6f 64 65 3d 3d 30 20 29 7b  ( pShmNode==0 ){
fb180 0a 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74  .    struct stat
fb190 20 73 53 74 61 74 3b 20 20 20 20 20 20 20 20 20   sStat;         
fb1a0 20 20 20 20 20 20 20 20 2f 2a 20 66 73 74 61 74          /* fstat
fb1b0 28 29 20 69 6e 66 6f 20 66 6f 72 20 64 61 74 61  () info for data
fb1c0 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20  base file */..  
fb1d0 20 20 2f 2a 20 43 61 6c 6c 20 66 73 74 61 74 28    /* Call fstat(
fb1e0 29 20 74 6f 20 66 69 67 75 72 65 20 6f 75 74 20  ) to figure out 
fb1f0 74 68 65 20 70 65 72 6d 69 73 73 69 6f 6e 73 20  the permissions 
fb200 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
fb210 66 69 6c 65 2e 20 49 66 0a 20 20 20 20 2a 2a 20  file. If.    ** 
fb220 61 20 6e 65 77 20 2a 2d 73 68 6d 20 66 69 6c 65  a new *-shm file
fb230 20 69 73 20 63 72 65 61 74 65 64 2c 20 61 6e 20   is created, an 
fb240 61 74 74 65 6d 70 74 20 77 69 6c 6c 20 62 65 20  attempt will be 
fb250 6d 61 64 65 20 74 6f 20 63 72 65 61 74 65 20 69  made to create i
fb260 74 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74 68  t.    ** with th
fb270 65 20 73 61 6d 65 20 70 65 72 6d 69 73 73 69 6f  e same permissio
fb280 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ns..    */.    i
fb290 66 28 20 6f 73 46 73 74 61 74 28 70 44 62 46 64  f( osFstat(pDbFd
fb2a0 2d 3e 68 2c 20 26 73 53 74 61 74 29 20 26 26 20  ->h, &sStat) && 
fb2b0 70 49 6e 6f 64 65 2d 3e 62 50 72 6f 63 65 73 73  pInode->bProcess
fb2c0 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Lock==0 ){.     
fb2d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45   rc = SQLITE_IOE
fb2e0 52 52 5f 46 53 54 41 54 3b 0a 20 20 20 20 20 20  RR_FSTAT;.      
fb2f0 67 6f 74 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65 72  goto shm_open_er
fb300 72 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 64 65 66  r;.    }..#ifdef
fb310 20 53 51 4c 49 54 45 5f 53 48 4d 5f 44 49 52 45   SQLITE_SHM_DIRE
fb320 43 54 4f 52 59 0a 20 20 20 20 6e 53 68 6d 46 69  CTORY.    nShmFi
fb330 6c 65 6e 61 6d 65 20 3d 20 73 69 7a 65 6f 66 28  lename = sizeof(
fb340 53 51 4c 49 54 45 5f 53 48 4d 5f 44 49 52 45 43  SQLITE_SHM_DIREC
fb350 54 4f 52 59 29 20 2b 20 33 31 3b 0a 23 65 6c 73  TORY) + 31;.#els
fb360 65 0a 20 20 20 20 6e 53 68 6d 46 69 6c 65 6e 61  e.    nShmFilena
fb370 6d 65 20 3d 20 36 20 2b 20 28 69 6e 74 29 73 74  me = 6 + (int)st
fb380 72 6c 65 6e 28 70 44 62 46 64 2d 3e 7a 50 61 74  rlen(pDbFd->zPat
fb390 68 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70  h);.#endif.    p
fb3a0 53 68 6d 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65  ShmNode = sqlite
fb3b0 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66  3_malloc( sizeof
fb3c0 28 2a 70 53 68 6d 4e 6f 64 65 29 20 2b 20 6e 53  (*pShmNode) + nS
fb3d0 68 6d 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20  hmFilename );.  
fb3e0 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 3d 3d    if( pShmNode==
fb3f0 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
fb400 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
fb410 20 20 20 20 67 6f 74 6f 20 73 68 6d 5f 6f 70 65      goto shm_ope
fb420 6e 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 20 20 20  n_err;.    }.   
fb430 20 6d 65 6d 73 65 74 28 70 53 68 6d 4e 6f 64 65   memset(pShmNode
fb440 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 53 68  , 0, sizeof(*pSh
fb450 6d 4e 6f 64 65 29 2b 6e 53 68 6d 46 69 6c 65 6e  mNode)+nShmFilen
fb460 61 6d 65 29 3b 0a 20 20 20 20 7a 53 68 6d 46 69  ame);.    zShmFi
fb470 6c 65 6e 61 6d 65 20 3d 20 70 53 68 6d 4e 6f 64  lename = pShmNod
fb480 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 28  e->zFilename = (
fb490 63 68 61 72 2a 29 26 70 53 68 6d 4e 6f 64 65 5b  char*)&pShmNode[
fb4a0 31 5d 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  1];.#ifdef SQLIT
fb4b0 45 5f 53 48 4d 5f 44 49 52 45 43 54 4f 52 59 0a  E_SHM_DIRECTORY.
fb4c0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
fb4d0 69 6e 74 66 28 6e 53 68 6d 46 69 6c 65 6e 61 6d  intf(nShmFilenam
fb4e0 65 2c 20 7a 53 68 6d 46 69 6c 65 6e 61 6d 65 2c  e, zShmFilename,
fb4f0 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
fb500 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 48         SQLITE_SH
fb510 4d 5f 44 49 52 45 43 54 4f 52 59 20 22 2f 73 71  M_DIRECTORY "/sq
fb520 6c 69 74 65 2d 73 68 6d 2d 25 78 2d 25 78 22 2c  lite-shm-%x-%x",
fb530 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
fb540 20 20 20 20 20 20 28 75 33 32 29 73 53 74 61 74        (u32)sStat
fb550 2e 73 74 5f 69 6e 6f 2c 20 28 75 33 32 29 73 53  .st_ino, (u32)sS
fb560 74 61 74 2e 73 74 5f 64 65 76 29 3b 0a 23 65 6c  tat.st_dev);.#el
fb570 73 65 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  se.    sqlite3_s
fb580 6e 70 72 69 6e 74 66 28 6e 53 68 6d 46 69 6c 65  nprintf(nShmFile
fb590 6e 61 6d 65 2c 20 7a 53 68 6d 46 69 6c 65 6e 61  name, zShmFilena
fb5a0 6d 65 2c 20 22 25 73 2d 73 68 6d 22 2c 20 70 44  me, "%s-shm", pD
fb5b0 62 46 64 2d 3e 7a 50 61 74 68 29 3b 0a 20 20 20  bFd->zPath);.   
fb5c0 20 73 71 6c 69 74 65 33 46 69 6c 65 53 75 66 66   sqlite3FileSuff
fb5d0 69 78 33 28 70 44 62 46 64 2d 3e 7a 50 61 74 68  ix3(pDbFd->zPath
fb5e0 2c 20 7a 53 68 6d 46 69 6c 65 6e 61 6d 65 29 3b  , zShmFilename);
fb5f0 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 53 68 6d  .#endif.    pShm
fb600 4e 6f 64 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20  Node->h = -1;.  
fb610 20 20 70 44 62 46 64 2d 3e 70 49 6e 6f 64 65 2d    pDbFd->pInode-
fb620 3e 70 53 68 6d 4e 6f 64 65 20 3d 20 70 53 68 6d  >pShmNode = pShm
fb630 4e 6f 64 65 3b 0a 20 20 20 20 70 53 68 6d 4e 6f  Node;.    pShmNo
fb640 64 65 2d 3e 70 49 6e 6f 64 65 20 3d 20 70 44 62  de->pInode = pDb
fb650 46 64 2d 3e 70 49 6e 6f 64 65 3b 0a 20 20 20 20  Fd->pInode;.    
fb660 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 20  pShmNode->mutex 
fb670 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  = sqlite3_mutex_
fb680 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  alloc(SQLITE_MUT
fb690 45 58 5f 46 41 53 54 29 3b 0a 20 20 20 20 69 66  EX_FAST);.    if
fb6a0 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65  ( pShmNode->mute
fb6b0 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63  x==0 ){.      rc
fb6c0 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
fb6d0 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 68 6d 5f  .      goto shm_
fb6e0 6f 70 65 6e 5f 65 72 72 3b 0a 20 20 20 20 7d 0a  open_err;.    }.
fb6f0 0a 20 20 20 20 69 66 28 20 70 49 6e 6f 64 65 2d  .    if( pInode-
fb700 3e 62 50 72 6f 63 65 73 73 4c 6f 63 6b 3d 3d 30  >bProcessLock==0
fb710 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6f 70   ){.      int op
fb720 65 6e 46 6c 61 67 73 20 3d 20 4f 5f 52 44 57 52  enFlags = O_RDWR
fb730 20 7c 20 4f 5f 43 52 45 41 54 3b 0a 20 20 20 20   | O_CREAT;.    
fb740 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 75 72    if( sqlite3_ur
fb750 69 5f 62 6f 6f 6c 65 61 6e 28 70 44 62 46 64 2d  i_boolean(pDbFd-
fb760 3e 7a 50 61 74 68 2c 20 22 72 65 61 64 6f 6e 6c  >zPath, "readonl
fb770 79 5f 73 68 6d 22 2c 20 30 29 20 29 7b 0a 20 20  y_shm", 0) ){.  
fb780 20 20 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 20        openFlags 
fb790 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 20 20  = O_RDONLY;.    
fb7a0 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 69 73      pShmNode->is
fb7b0 52 65 61 64 6f 6e 6c 79 20 3d 20 31 3b 0a 20 20  Readonly = 1;.  
fb7c0 20 20 20 20 7d 0a 20 20 20 20 20 20 70 53 68 6d      }.      pShm
fb7d0 4e 6f 64 65 2d 3e 68 20 3d 20 72 6f 62 75 73 74  Node->h = robust
fb7e0 5f 6f 70 65 6e 28 7a 53 68 6d 46 69 6c 65 6e 61  _open(zShmFilena
fb7f0 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 28  me, openFlags, (
fb800 73 53 74 61 74 2e 73 74 5f 6d 6f 64 65 26 30 37  sStat.st_mode&07
fb810 37 37 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20  77));.      if( 
fb820 70 53 68 6d 4e 6f 64 65 2d 3e 68 3c 30 20 29 7b  pShmNode->h<0 ){
fb830 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e  .        rc = un
fb840 69 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54  ixLogError(SQLIT
fb850 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 2c  E_CANTOPEN_BKPT,
fb860 20 22 6f 70 65 6e 22 2c 20 7a 53 68 6d 46 69 6c   "open", zShmFil
fb870 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  ename);.        
fb880 67 6f 74 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65 72  goto shm_open_er
fb890 72 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  r;.      }..    
fb8a0 20 20 2f 2a 20 49 66 20 74 68 69 73 20 70 72 6f    /* If this pro
fb8b0 63 65 73 73 20 69 73 20 72 75 6e 6e 69 6e 67 20  cess is running 
fb8c0 61 73 20 72 6f 6f 74 2c 20 6d 61 6b 65 20 73 75  as root, make su
fb8d0 72 65 20 74 68 61 74 20 74 68 65 20 53 48 4d 20  re that the SHM 
fb8e0 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 69 73  file.      ** is
fb8f0 20 6f 77 6e 65 64 20 62 79 20 74 68 65 20 73 61   owned by the sa
fb900 6d 65 20 75 73 65 72 20 74 68 61 74 20 6f 77 6e  me user that own
fb910 73 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64  s the original d
fb920 61 74 61 62 61 73 65 2e 20 20 4f 74 68 65 72 77  atabase.  Otherw
fb930 69 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68  ise,.      ** th
fb940 65 20 6f 72 69 67 69 6e 61 6c 20 6f 77 6e 65 72  e original owner
fb950 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c   will not be abl
fb960 65 20 74 6f 20 63 6f 6e 6e 65 63 74 2e 0a 20 20  e to connect..  
fb970 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6f 73 46      */.      osF
fb980 63 68 6f 77 6e 28 70 53 68 6d 4e 6f 64 65 2d 3e  chown(pShmNode->
fb990 68 2c 20 73 53 74 61 74 2e 73 74 5f 75 69 64 2c  h, sStat.st_uid,
fb9a0 20 73 53 74 61 74 2e 73 74 5f 67 69 64 29 3b 0a   sStat.st_gid);.
fb9b0 20 20 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63    .      /* Chec
fb9c0 6b 20 74 6f 20 73 65 65 20 69 66 20 61 6e 6f 74  k to see if anot
fb9d0 68 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 68  her process is h
fb9e0 6f 6c 64 69 6e 67 20 74 68 65 20 64 65 61 64 2d  olding the dead-
fb9f0 6d 61 6e 20 73 77 69 74 63 68 2e 0a 20 20 20 20  man switch..    
fba00 20 20 2a 2a 20 49 66 20 6e 6f 74 2c 20 74 72 75    ** If not, tru
fba10 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 20 74  ncate the file t
fba20 6f 20 7a 65 72 6f 20 6c 65 6e 67 74 68 2e 20 0a  o zero length. .
fba30 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72        */.      r
fba40 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
fba50 20 20 20 20 20 69 66 28 20 75 6e 69 78 53 68 6d       if( unixShm
fba60 53 79 73 74 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e  SystemLock(pShmN
fba70 6f 64 65 2c 20 46 5f 57 52 4c 43 4b 2c 20 55 4e  ode, F_WRLCK, UN
fba80 49 58 5f 53 48 4d 5f 44 4d 53 2c 20 31 29 3d 3d  IX_SHM_DMS, 1)==
fba90 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
fbaa0 20 20 20 20 20 69 66 28 20 72 6f 62 75 73 74 5f       if( robust_
fbab0 66 74 72 75 6e 63 61 74 65 28 70 53 68 6d 4e 6f  ftruncate(pShmNo
fbac0 64 65 2d 3e 68 2c 20 30 29 20 29 7b 0a 20 20 20  de->h, 0) ){.   
fbad0 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69 78         rc = unix
fbae0 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f  LogError(SQLITE_
fbaf0 49 4f 45 52 52 5f 53 48 4d 4f 50 45 4e 2c 20 22  IOERR_SHMOPEN, "
fbb00 66 74 72 75 6e 63 61 74 65 22 2c 20 7a 53 68 6d  ftruncate", zShm
fbb10 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20  Filename);.     
fbb20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
fbb30 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
fbb40 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
fbb50 72 63 20 3d 20 75 6e 69 78 53 68 6d 53 79 73 74  rc = unixShmSyst
fbb60 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 2c  emLock(pShmNode,
fbb70 20 46 5f 52 44 4c 43 4b 2c 20 55 4e 49 58 5f 53   F_RDLCK, UNIX_S
fbb80 48 4d 5f 44 4d 53 2c 20 31 29 3b 0a 20 20 20 20  HM_DMS, 1);.    
fbb90 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63    }.      if( rc
fbba0 20 29 20 67 6f 74 6f 20 73 68 6d 5f 6f 70 65 6e   ) goto shm_open
fbbb0 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  _err;.    }.  }.
fbbc0 0a 20 20 2f 2a 20 4d 61 6b 65 20 74 68 65 20 6e  .  /* Make the n
fbbd0 65 77 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 20  ew connection a 
fbbe0 63 68 69 6c 64 20 6f 66 20 74 68 65 20 75 6e 69  child of the uni
fbbf0 78 53 68 6d 4e 6f 64 65 20 2a 2f 0a 20 20 70 2d  xShmNode */.  p-
fbc00 3e 70 53 68 6d 4e 6f 64 65 20 3d 20 70 53 68 6d  >pShmNode = pShm
fbc10 4e 6f 64 65 3b 0a 23 69 66 64 65 66 20 53 51 4c  Node;.#ifdef SQL
fbc20 49 54 45 5f 44 45 42 55 47 0a 20 20 70 2d 3e 69  ITE_DEBUG.  p->i
fbc30 64 20 3d 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 65  d = pShmNode->ne
fbc40 78 74 53 68 6d 49 64 2b 2b 3b 0a 23 65 6e 64 69  xtShmId++;.#endi
fbc50 66 0a 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52  f.  pShmNode->nR
fbc60 65 66 2b 2b 3b 0a 20 20 70 44 62 46 64 2d 3e 70  ef++;.  pDbFd->p
fbc70 53 68 6d 20 3d 20 70 3b 0a 20 20 75 6e 69 78 4c  Shm = p;.  unixL
fbc80 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 0a 20 20  eaveMutex();..  
fbc90 2f 2a 20 54 68 65 20 72 65 66 65 72 65 6e 63 65  /* The reference
fbca0 20 63 6f 75 6e 74 20 6f 6e 20 70 53 68 6d 4e 6f   count on pShmNo
fbcb0 64 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  de has already b
fbcc0 65 65 6e 20 69 6e 63 72 65 6d 65 6e 74 65 64 20  een incremented 
fbcd0 75 6e 64 65 72 0a 20 20 2a 2a 20 74 68 65 20 63  under.  ** the c
fbce0 6f 76 65 72 20 6f 66 20 74 68 65 20 75 6e 69 78  over of the unix
fbcf0 45 6e 74 65 72 4d 75 74 65 78 28 29 20 6d 75 74  EnterMutex() mut
fbd00 65 78 20 61 6e 64 20 74 68 65 20 70 6f 69 6e 74  ex and the point
fbd10 65 72 20 66 72 6f 6d 20 74 68 65 0a 20 20 2a 2a  er from the.  **
fbd20 20 6e 65 77 20 28 73 74 72 75 63 74 20 75 6e 69   new (struct uni
fbd30 78 53 68 6d 29 20 6f 62 6a 65 63 74 20 74 6f 20  xShm) object to 
fbd40 74 68 65 20 70 53 68 6d 4e 6f 64 65 20 68 61 73  the pShmNode has
fbd50 20 62 65 65 6e 20 73 65 74 2e 20 41 6c 6c 20 74   been set. All t
fbd60 68 61 74 20 69 73 0a 20 20 2a 2a 20 6c 65 66 74  hat is.  ** left
fbd70 20 74 6f 20 64 6f 20 69 73 20 74 6f 20 6c 69 6e   to do is to lin
fbd80 6b 20 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 74  k the new object
fbd90 20 69 6e 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64   into the linked
fbda0 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 0a 20   list starting. 
fbdb0 20 2a 2a 20 61 74 20 70 53 68 6d 4e 6f 64 65 2d   ** at pShmNode-
fbdc0 3e 70 46 69 72 73 74 2e 20 54 68 69 73 20 6d 75  >pFirst. This mu
fbdd0 73 74 20 62 65 20 64 6f 6e 65 20 77 68 69 6c 65  st be done while
fbde0 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 70 53 68   holding the pSh
fbdf0 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 20 0a 20 20  mNode->mutex .  
fbe00 2a 2a 20 6d 75 74 65 78 2e 0a 20 20 2a 2f 0a 20  ** mutex..  */. 
fbe10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
fbe20 6e 74 65 72 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d  nter(pShmNode->m
fbe30 75 74 65 78 29 3b 0a 20 20 70 2d 3e 70 4e 65 78  utex);.  p->pNex
fbe40 74 20 3d 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 46  t = pShmNode->pF
fbe50 69 72 73 74 3b 0a 20 20 70 53 68 6d 4e 6f 64 65  irst;.  pShmNode
fbe60 2d 3e 70 46 69 72 73 74 20 3d 20 70 3b 0a 20 20  ->pFirst = p;.  
fbe70 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
fbe80 61 76 65 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75  ave(pShmNode->mu
fbe90 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  tex);.  return S
fbea0 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20  QLITE_OK;..  /* 
fbeb0 4a 75 6d 70 20 68 65 72 65 20 6f 6e 20 61 6e 79  Jump here on any
fbec0 20 65 72 72 6f 72 20 2a 2f 0a 73 68 6d 5f 6f 70   error */.shm_op
fbed0 65 6e 5f 65 72 72 3a 0a 20 20 75 6e 69 78 53 68  en_err:.  unixSh
fbee0 6d 50 75 72 67 65 28 70 44 62 46 64 29 3b 20 20  mPurge(pDbFd);  
fbef0 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c       /* This cal
fbf00 6c 20 66 72 65 65 73 20 70 53 68 6d 4e 6f 64 65  l frees pShmNode
fbf10 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a   if required */.
fbf20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
fbf30 29 3b 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75  );.  unixLeaveMu
fbf40 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20  tex();.  return 
fbf50 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
fbf60 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
fbf70 6c 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 61  lled to obtain a
fbf80 20 70 6f 69 6e 74 65 72 20 74 6f 20 72 65 67 69   pointer to regi
fbf90 6f 6e 20 69 52 65 67 69 6f 6e 20 6f 66 20 74 68  on iRegion of th
fbfa0 65 20 0a 2a 2a 20 73 68 61 72 65 64 2d 6d 65 6d  e .** shared-mem
fbfb0 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  ory associated w
fbfc0 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65  ith the database
fbfd0 20 66 69 6c 65 20 66 64 2e 20 53 68 61 72 65 64   file fd. Shared
fbfe0 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 73 20  -memory regions 
fbff0 0a 2a 2a 20 61 72 65 20 6e 75 6d 62 65 72 65 64  .** are numbered
fc000 20 73 74 61 72 74 69 6e 67 20 66 72 6f 6d 20 7a   starting from z
fc010 65 72 6f 2e 20 45 61 63 68 20 73 68 61 72 65 64  ero. Each shared
fc020 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 20 69  -memory region i
fc030 73 20 73 7a 52 65 67 69 6f 6e 20 0a 2a 2a 20 62  s szRegion .** b
fc040 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a  ytes in size..**
fc050 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
fc060 6f 63 63 75 72 73 2c 20 61 6e 20 65 72 72 6f 72  occurs, an error
fc070 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
fc080 64 20 61 6e 64 20 2a 70 70 20 69 73 20 73 65 74  d and *pp is set
fc090 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   to NULL..**.** 
fc0a0 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68  Otherwise, if th
fc0b0 65 20 62 45 78 74 65 6e 64 20 70 61 72 61 6d 65  e bExtend parame
fc0c0 74 65 72 20 69 73 20 30 20 61 6e 64 20 74 68 65  ter is 0 and the
fc0d0 20 72 65 71 75 65 73 74 65 64 20 73 68 61 72 65   requested share
fc0e0 64 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 72 65 67 69  d-memory.** regi
fc0f0 6f 6e 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  on has not been 
fc100 61 6c 6c 6f 63 61 74 65 64 20 28 62 79 20 61 6e  allocated (by an
fc110 79 20 63 6c 69 65 6e 74 2c 20 69 6e 63 6c 75 64  y client, includ
fc120 69 6e 67 20 6f 6e 65 20 72 75 6e 6e 69 6e 67 20  ing one running 
fc130 69 6e 20 61 0a 2a 2a 20 73 65 70 61 72 61 74 65  in a.** separate
fc140 20 70 72 6f 63 65 73 73 29 2c 20 74 68 65 6e 20   process), then 
fc150 2a 70 70 20 69 73 20 73 65 74 20 74 6f 20 4e 55  *pp is set to NU
fc160 4c 4c 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b  LL and SQLITE_OK
fc170 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 0a 2a   returned. If .*
fc180 2a 20 62 45 78 74 65 6e 64 20 69 73 20 6e 6f 6e  * bExtend is non
fc190 2d 7a 65 72 6f 20 61 6e 64 20 74 68 65 20 72 65  -zero and the re
fc1a0 71 75 65 73 74 65 64 20 73 68 61 72 65 64 2d 6d  quested shared-m
fc1b0 65 6d 6f 72 79 20 72 65 67 69 6f 6e 20 68 61 73  emory region has
fc1c0 20 6e 6f 74 20 79 65 74 20 0a 2a 2a 20 62 65 65   not yet .** bee
fc1d0 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20  n allocated, it 
fc1e0 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  is allocated by 
fc1f0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  this function..*
fc200 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 68 61 72  *.** If the shar
fc210 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e  ed-memory region
fc220 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
fc230 6e 20 61 6c 6c 6f 63 61 74 65 64 20 6f 72 20 69  n allocated or i
fc240 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a  s allocated by.*
fc250 2a 20 74 68 69 73 20 63 61 6c 6c 20 61 73 20 64  * this call as d
fc260 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2c 20  escribed above, 
fc270 74 68 65 6e 20 69 74 20 69 73 20 6d 61 70 70 65  then it is mappe
fc280 64 20 69 6e 74 6f 20 74 68 69 73 20 70 72 6f 63  d into this proc
fc290 65 73 73 65 73 20 0a 2a 2a 20 61 64 64 72 65 73  esses .** addres
fc2a0 73 20 73 70 61 63 65 20 28 69 66 20 69 74 20 69  s space (if it i
fc2b0 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 29 2c 20  s not already), 
fc2c0 2a 70 70 20 69 73 20 73 65 74 20 74 6f 20 70 6f  *pp is set to po
fc2d0 69 6e 74 20 74 6f 20 74 68 65 20 6d 61 70 70 65  int to the mappe
fc2e0 64 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e 64  d .** memory and
fc2f0 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72   SQLITE_OK retur
fc300 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
fc310 6e 74 20 75 6e 69 78 53 68 6d 4d 61 70 28 0a 20  nt unixShmMap(. 
fc320 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66   sqlite3_file *f
fc330 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
fc340 20 2f 2a 20 48 61 6e 64 6c 65 20 6f 70 65 6e 20   /* Handle open 
fc350 6f 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  on database file
fc360 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 69 6f   */.  int iRegio
fc370 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
fc380 20 20 20 20 20 20 2f 2a 20 52 65 67 69 6f 6e 20        /* Region 
fc390 74 6f 20 72 65 74 72 69 65 76 65 20 2a 2f 0a 20  to retrieve */. 
fc3a0 20 69 6e 74 20 73 7a 52 65 67 69 6f 6e 2c 20 20   int szRegion,  
fc3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fc3c0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 72 65 67 69   /* Size of regi
fc3d0 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 62 45 78  ons */.  int bEx
fc3e0 74 65 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20  tend,           
fc3f0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
fc400 20 74 6f 20 65 78 74 65 6e 64 20 66 69 6c 65 20   to extend file 
fc410 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a  if necessary */.
fc420 20 20 76 6f 69 64 20 76 6f 6c 61 74 69 6c 65 20    void volatile 
fc430 2a 2a 70 70 20 20 20 20 20 20 20 20 20 20 20 20  **pp            
fc440 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 70 70 65 64    /* OUT: Mapped
fc450 20 6d 65 6d 6f 72 79 20 2a 2f 0a 29 7b 0a 20 20   memory */.){.  
fc460 75 6e 69 78 46 69 6c 65 20 2a 70 44 62 46 64 20  unixFile *pDbFd 
fc470 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 66 64 3b  = (unixFile*)fd;
fc480 0a 20 20 75 6e 69 78 53 68 6d 20 2a 70 3b 0a 20  .  unixShm *p;. 
fc490 20 75 6e 69 78 53 68 6d 4e 6f 64 65 20 2a 70 53   unixShmNode *pS
fc4a0 68 6d 4e 6f 64 65 3b 0a 20 20 69 6e 74 20 72 63  hmNode;.  int rc
fc4b0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
fc4c0 20 2f 2a 20 49 66 20 74 68 65 20 73 68 61 72 65   /* If the share
fc4d0 64 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 68 61  d-memory file ha
fc4e0 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f  s not yet been o
fc4f0 70 65 6e 65 64 2c 20 6f 70 65 6e 20 69 74 20 6e  pened, open it n
fc500 6f 77 2e 20 2a 2f 0a 20 20 69 66 28 20 70 44 62  ow. */.  if( pDb
fc510 46 64 2d 3e 70 53 68 6d 3d 3d 30 20 29 7b 0a 20  Fd->pShm==0 ){. 
fc520 20 20 20 72 63 20 3d 20 75 6e 69 78 4f 70 65 6e     rc = unixOpen
fc530 53 68 61 72 65 64 4d 65 6d 6f 72 79 28 70 44 62  SharedMemory(pDb
fc540 46 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  Fd);.    if( rc!
fc550 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
fc560 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 70  urn rc;.  }..  p
fc570 20 3d 20 70 44 62 46 64 2d 3e 70 53 68 6d 3b 0a   = pDbFd->pShm;.
fc580 20 20 70 53 68 6d 4e 6f 64 65 20 3d 20 70 2d 3e    pShmNode = p->
fc590 70 53 68 6d 4e 6f 64 65 3b 0a 20 20 73 71 6c 69  pShmNode;.  sqli
fc5a0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
fc5b0 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29  pShmNode->mutex)
fc5c0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 52 65  ;.  assert( szRe
fc5d0 67 69 6f 6e 3d 3d 70 53 68 6d 4e 6f 64 65 2d 3e  gion==pShmNode->
fc5e0 73 7a 52 65 67 69 6f 6e 20 7c 7c 20 70 53 68 6d  szRegion || pShm
fc5f0 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f 6e 3d 3d 30  Node->nRegion==0
fc600 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53   );.  assert( pS
fc610 68 6d 4e 6f 64 65 2d 3e 70 49 6e 6f 64 65 3d 3d  hmNode->pInode==
fc620 70 44 62 46 64 2d 3e 70 49 6e 6f 64 65 20 29 3b  pDbFd->pInode );
fc630 0a 20 20 61 73 73 65 72 74 28 20 70 53 68 6d 4e  .  assert( pShmN
fc640 6f 64 65 2d 3e 68 3e 3d 30 20 7c 7c 20 70 44 62  ode->h>=0 || pDb
fc650 46 64 2d 3e 70 49 6e 6f 64 65 2d 3e 62 50 72 6f  Fd->pInode->bPro
fc660 63 65 73 73 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20  cessLock==1 );. 
fc670 20 61 73 73 65 72 74 28 20 70 53 68 6d 4e 6f 64   assert( pShmNod
fc680 65 2d 3e 68 3c 30 20 7c 7c 20 70 44 62 46 64 2d  e->h<0 || pDbFd-
fc690 3e 70 49 6e 6f 64 65 2d 3e 62 50 72 6f 63 65 73  >pInode->bProces
fc6a0 73 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 69  sLock==0 );..  i
fc6b0 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65  f( pShmNode->nRe
fc6c0 67 69 6f 6e 3c 3d 69 52 65 67 69 6f 6e 20 29 7b  gion<=iRegion ){
fc6d0 0a 20 20 20 20 63 68 61 72 20 2a 2a 61 70 4e 65  .    char **apNe
fc6e0 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
fc6f0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 61          /* New a
fc700 70 52 65 67 69 6f 6e 5b 5d 20 61 72 72 61 79 20  pRegion[] array 
fc710 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65  */.    int nByte
fc720 20 3d 20 28 69 52 65 67 69 6f 6e 2b 31 29 2a 73   = (iRegion+1)*s
fc730 7a 52 65 67 69 6f 6e 3b 20 20 2f 2a 20 4d 69 6e  zRegion;  /* Min
fc740 69 6d 75 6d 20 72 65 71 75 69 72 65 64 20 66 69  imum required fi
fc750 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 73  le size */.    s
fc760 74 72 75 63 74 20 73 74 61 74 20 73 53 74 61 74  truct stat sStat
fc770 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
fc780 20 20 2f 2a 20 55 73 65 64 20 62 79 20 66 73 74    /* Used by fst
fc790 61 74 28 29 20 2a 2f 0a 0a 20 20 20 20 70 53 68  at() */..    pSh
fc7a0 6d 4e 6f 64 65 2d 3e 73 7a 52 65 67 69 6f 6e 20  mNode->szRegion 
fc7b0 3d 20 73 7a 52 65 67 69 6f 6e 3b 0a 0a 20 20 20  = szRegion;..   
fc7c0 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 68   if( pShmNode->h
fc7d0 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  >=0 ){.      /* 
fc7e0 54 68 65 20 72 65 71 75 65 73 74 65 64 20 72 65  The requested re
fc7f0 67 69 6f 6e 20 69 73 20 6e 6f 74 20 6d 61 70 70  gion is not mapp
fc800 65 64 20 69 6e 74 6f 20 74 68 69 73 20 70 72 6f  ed into this pro
fc810 63 65 73 73 65 73 20 61 64 64 72 65 73 73 20 73  cesses address s
fc820 70 61 63 65 2e 0a 20 20 20 20 20 20 2a 2a 20 43  pace..      ** C
fc830 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 69  heck to see if i
fc840 74 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63  t has been alloc
fc850 61 74 65 64 20 28 69 2e 65 2e 20 69 66 20 74 68  ated (i.e. if th
fc860 65 20 77 61 6c 2d 69 6e 64 65 78 20 66 69 6c 65  e wal-index file
fc870 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 6c 61 72   is.      ** lar
fc880 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 63 6f 6e  ge enough to con
fc890 74 61 69 6e 20 74 68 65 20 72 65 71 75 65 73 74  tain the request
fc8a0 65 64 20 72 65 67 69 6f 6e 29 2e 0a 20 20 20 20  ed region)..    
fc8b0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6f    */.      if( o
fc8c0 73 46 73 74 61 74 28 70 53 68 6d 4e 6f 64 65 2d  sFstat(pShmNode-
fc8d0 3e 68 2c 20 26 73 53 74 61 74 29 20 29 7b 0a 20  >h, &sStat) ){. 
fc8e0 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
fc8f0 54 45 5f 49 4f 45 52 52 5f 53 48 4d 53 49 5a 45  TE_IOERR_SHMSIZE
fc900 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73  ;.        goto s
fc910 68 6d 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20  hmpage_out;.    
fc920 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 69 66 28    }.  .      if(
fc930 20 73 53 74 61 74 2e 73 74 5f 73 69 7a 65 3c 6e   sStat.st_size<n
fc940 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Byte ){.        
fc950 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 65 64  /* The requested
fc960 20 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 20 64   memory region d
fc970 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 49  oes not exist. I
fc980 66 20 62 45 78 74 65 6e 64 20 69 73 20 73 65 74  f bExtend is set
fc990 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 66   to.        ** f
fc9a0 61 6c 73 65 2c 20 65 78 69 74 20 65 61 72 6c 79  alse, exit early
fc9b0 2e 20 2a 70 70 20 77 69 6c 6c 20 62 65 20 73 65  . *pp will be se
fc9c0 74 20 74 6f 20 4e 55 4c 4c 20 61 6e 64 20 53 51  t to NULL and SQ
fc9d0 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64  LITE_OK returned
fc9e0 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ..        **.   
fc9f0 20 20 20 20 20 2a 2a 20 41 6c 74 65 72 6e 61 74       ** Alternat
fca00 69 76 65 6c 79 2c 20 69 66 20 62 45 78 74 65 6e  ively, if bExten
fca10 64 20 69 73 20 74 72 75 65 2c 20 75 73 65 20 66  d is true, use f
fca20 74 72 75 6e 63 61 74 65 28 29 20 74 6f 20 61 6c  truncate() to al
fca30 6c 6f 63 61 74 65 0a 20 20 20 20 20 20 20 20 2a  locate.        *
fca40 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
fca50 6d 65 6d 6f 72 79 20 72 65 67 69 6f 6e 2e 0a 20  memory region.. 
fca60 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
fca70 20 20 69 66 28 20 21 62 45 78 74 65 6e 64 20 29    if( !bExtend )
fca80 20 67 6f 74 6f 20 73 68 6d 70 61 67 65 5f 6f 75   goto shmpage_ou
fca90 74 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 48  t;.#if defined(H
fcaa0 41 56 45 5f 50 4f 53 49 58 5f 46 41 4c 4c 4f 43  AVE_POSIX_FALLOC
fcab0 41 54 45 29 20 26 26 20 48 41 56 45 5f 50 4f 53  ATE) && HAVE_POS
fcac0 49 58 5f 46 41 4c 4c 4f 43 41 54 45 0a 20 20 20  IX_FALLOCATE.   
fcad0 20 20 20 20 20 69 66 28 20 6f 73 46 61 6c 6c 6f       if( osFallo
fcae0 63 61 74 65 28 70 53 68 6d 4e 6f 64 65 2d 3e 68  cate(pShmNode->h
fcaf0 2c 20 73 53 74 61 74 2e 73 74 5f 73 69 7a 65 2c  , sStat.st_size,
fcb00 20 6e 42 79 74 65 29 21 3d 30 20 29 7b 0a 20 20   nByte)!=0 ){.  
fcb10 20 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69          rc = uni
fcb20 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45  xLogError(SQLITE
fcb30 5f 49 4f 45 52 52 5f 53 48 4d 53 49 5a 45 2c 20  _IOERR_SHMSIZE, 
fcb40 22 66 61 6c 6c 6f 63 61 74 65 22 2c 0a 20 20 20  "fallocate",.   
fcb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fcb60 20 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f 64           pShmNod
fcb70 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  e->zFilename);. 
fcb80 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73 68           goto sh
fcb90 6d 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 20  mpage_out;.     
fcba0 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 20     }.#else.     
fcbb0 20 20 20 69 66 28 20 72 6f 62 75 73 74 5f 66 74     if( robust_ft
fcbc0 72 75 6e 63 61 74 65 28 70 53 68 6d 4e 6f 64 65  runcate(pShmNode
fcbd0 2d 3e 68 2c 20 6e 42 79 74 65 29 20 29 7b 0a 20  ->h, nByte) ){. 
fcbe0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e           rc = un
fcbf0 69 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54  ixLogError(SQLIT
fcc00 45 5f 49 4f 45 52 52 5f 53 48 4d 53 49 5a 45 2c  E_IOERR_SHMSIZE,
fcc10 20 22 66 74 72 75 6e 63 61 74 65 22 2c 0a 20 20   "ftruncate",.  
fcc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fcc30 20 20 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f            pShmNo
fcc40 64 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  de->zFilename);.
fcc50 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73            goto s
fcc60 68 6d 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20  hmpage_out;.    
fcc70 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
fcc80 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
fcc90 2f 2a 20 4d 61 70 20 74 68 65 20 72 65 71 75 65  /* Map the reque
fcca0 73 74 65 64 20 6d 65 6d 6f 72 79 20 72 65 67 69  sted memory regi
fccb0 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 70 72 6f  on into this pro
fccc0 63 65 73 73 65 73 20 61 64 64 72 65 73 73 20 73  cesses address s
fccd0 70 61 63 65 2e 20 2a 2f 0a 20 20 20 20 61 70 4e  pace. */.    apN
fcce0 65 77 20 3d 20 28 63 68 61 72 20 2a 2a 29 73 71  ew = (char **)sq
fccf0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 0a 20  lite3_realloc(. 
fcd00 20 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d         pShmNode-
fcd10 3e 61 70 52 65 67 69 6f 6e 2c 20 28 69 52 65 67  >apRegion, (iReg
fcd20 69 6f 6e 2b 31 29 2a 73 69 7a 65 6f 66 28 63 68  ion+1)*sizeof(ch
fcd30 61 72 20 2a 29 0a 20 20 20 20 29 3b 0a 20 20 20  ar *).    );.   
fcd40 20 69 66 28 20 21 61 70 4e 65 77 20 29 7b 0a 20   if( !apNew ){. 
fcd50 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
fcd60 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20  _IOERR_NOMEM;.  
fcd70 20 20 20 20 67 6f 74 6f 20 73 68 6d 70 61 67 65      goto shmpage
fcd80 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
fcd90 70 53 68 6d 4e 6f 64 65 2d 3e 61 70 52 65 67 69  pShmNode->apRegi
fcda0 6f 6e 20 3d 20 61 70 4e 65 77 3b 0a 20 20 20 20  on = apNew;.    
fcdb0 77 68 69 6c 65 28 70 53 68 6d 4e 6f 64 65 2d 3e  while(pShmNode->
fcdc0 6e 52 65 67 69 6f 6e 3c 3d 69 52 65 67 69 6f 6e  nRegion<=iRegion
fcdd0 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 70  ){.      void *p
fcde0 4d 65 6d 3b 0a 20 20 20 20 20 20 69 66 28 20 70  Mem;.      if( p
fcdf0 53 68 6d 4e 6f 64 65 2d 3e 68 3e 3d 30 20 29 7b  ShmNode->h>=0 ){
fce00 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 20 3d 20  .        pMem = 
fce10 6d 6d 61 70 28 30 2c 20 73 7a 52 65 67 69 6f 6e  mmap(0, szRegion
fce20 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 53  ,.            pS
fce30 68 6d 4e 6f 64 65 2d 3e 69 73 52 65 61 64 6f 6e  hmNode->isReadon
fce40 6c 79 20 3f 20 50 52 4f 54 5f 52 45 41 44 20 3a  ly ? PROT_READ :
fce50 20 50 52 4f 54 5f 52 45 41 44 7c 50 52 4f 54 5f   PROT_READ|PROT_
fce60 57 52 49 54 45 2c 20 0a 20 20 20 20 20 20 20 20  WRITE, .        
fce70 20 20 20 20 4d 41 50 5f 53 48 41 52 45 44 2c 20      MAP_SHARED, 
fce80 70 53 68 6d 4e 6f 64 65 2d 3e 68 2c 20 73 7a 52  pShmNode->h, szR
fce90 65 67 69 6f 6e 2a 28 69 36 34 29 70 53 68 6d 4e  egion*(i64)pShmN
fcea0 6f 64 65 2d 3e 6e 52 65 67 69 6f 6e 0a 20 20 20  ode->nRegion.   
fceb0 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20       );.        
fcec0 69 66 28 20 70 4d 65 6d 3d 3d 4d 41 50 5f 46 41  if( pMem==MAP_FA
fced0 49 4c 45 44 20 29 7b 0a 20 20 20 20 20 20 20 20  ILED ){.        
fcee0 20 20 72 63 20 3d 20 75 6e 69 78 4c 6f 67 45 72    rc = unixLogEr
fcef0 72 6f 72 28 53 51 4c 49 54 45 5f 49 4f 45 52 52  ror(SQLITE_IOERR
fcf00 5f 53 48 4d 4d 41 50 2c 20 22 6d 6d 61 70 22 2c  _SHMMAP, "mmap",
fcf10 20 70 53 68 6d 4e 6f 64 65 2d 3e 7a 46 69 6c 65   pShmNode->zFile
fcf20 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20  name);.         
fcf30 20 67 6f 74 6f 20 73 68 6d 70 61 67 65 5f 6f 75   goto shmpage_ou
fcf40 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
fcf50 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
fcf60 20 20 70 4d 65 6d 20 3d 20 73 71 6c 69 74 65 33    pMem = sqlite3
fcf70 5f 6d 61 6c 6c 6f 63 28 73 7a 52 65 67 69 6f 6e  _malloc(szRegion
fcf80 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
fcf90 4d 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Mem==0 ){.      
fcfa0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
fcfb0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20  NOMEM;.         
fcfc0 20 67 6f 74 6f 20 73 68 6d 70 61 67 65 5f 6f 75   goto shmpage_ou
fcfd0 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
fcfe0 20 20 20 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d       memset(pMem
fcff0 2c 20 30 2c 20 73 7a 52 65 67 69 6f 6e 29 3b 0a  , 0, szRegion);.
fd000 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 53        }.      pS
fd010 68 6d 4e 6f 64 65 2d 3e 61 70 52 65 67 69 6f 6e  hmNode->apRegion
fd020 5b 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67 69  [pShmNode->nRegi
fd030 6f 6e 5d 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20  on] = pMem;.    
fd040 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67    pShmNode->nReg
fd050 69 6f 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  ion++;.    }.  }
fd060 0a 0a 73 68 6d 70 61 67 65 5f 6f 75 74 3a 0a 20  ..shmpage_out:. 
fd070 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e   if( pShmNode->n
fd080 52 65 67 69 6f 6e 3e 69 52 65 67 69 6f 6e 20 29  Region>iRegion )
fd090 7b 0a 20 20 20 20 2a 70 70 20 3d 20 70 53 68 6d  {.    *pp = pShm
fd0a0 4e 6f 64 65 2d 3e 61 70 52 65 67 69 6f 6e 5b 69  Node->apRegion[i
fd0b0 52 65 67 69 6f 6e 5d 3b 0a 20 20 7d 65 6c 73 65  Region];.  }else
fd0c0 7b 0a 20 20 20 20 2a 70 70 20 3d 20 30 3b 0a 20  {.    *pp = 0;. 
fd0d0 20 7d 0a 20 20 69 66 28 20 70 53 68 6d 4e 6f 64   }.  if( pShmNod
fd0e0 65 2d 3e 69 73 52 65 61 64 6f 6e 6c 79 20 26 26  e->isReadonly &&
fd0f0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
fd100 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41   rc = SQLITE_REA
fd110 44 4f 4e 4c 59 3b 0a 20 20 73 71 6c 69 74 65 33  DONLY;.  sqlite3
fd120 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 53 68  _mutex_leave(pSh
fd130 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0a 20  mNode->mutex);. 
fd140 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
fd150 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
fd160 6c 6f 63 6b 20 73 74 61 74 65 20 66 6f 72 20 61  lock state for a
fd170 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 73   shared-memory s
fd180 65 67 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  egment..**.** No
fd190 74 65 20 74 68 61 74 20 74 68 65 20 72 65 6c 61  te that the rela
fd1a0 74 69 6f 6e 73 68 69 70 20 62 65 74 77 65 65 6e  tionship between
fd1b0 20 53 48 41 52 45 64 20 61 6e 64 20 45 58 43 4c   SHAREd and EXCL
fd1c0 55 53 49 56 45 20 6c 6f 63 6b 73 20 69 73 20 61  USIVE locks is a
fd1d0 20 6c 69 74 74 6c 65 0a 2a 2a 20 64 69 66 66 65   little.** diffe
fd1e0 72 65 6e 74 20 68 65 72 65 20 74 68 61 6e 20 69  rent here than i
fd1f0 6e 20 70 6f 73 69 78 2e 20 20 49 6e 20 78 53 68  n posix.  In xSh
fd200 6d 4c 6f 63 6b 28 29 2c 20 6f 6e 65 20 63 61 6e  mLock(), one can
fd210 20 67 6f 20 66 72 6f 6d 20 75 6e 6c 6f 63 6b 65   go from unlocke
fd220 64 0a 2a 2a 20 74 6f 20 73 68 61 72 65 64 20 61  d.** to shared a
fd230 6e 64 20 62 61 63 6b 20 6f 72 20 66 72 6f 6d 20  nd back or from 
fd240 75 6e 6c 6f 63 6b 65 64 20 74 6f 20 65 78 63 6c  unlocked to excl
fd250 75 73 69 76 65 20 61 6e 64 20 62 61 63 6b 2e 20  usive and back. 
fd260 20 42 75 74 20 6f 6e 65 20 6d 61 79 0a 2a 2a 20   But one may.** 
fd270 6e 6f 74 20 67 6f 20 66 72 6f 6d 20 73 68 61 72  not go from shar
fd280 65 64 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20  ed to exclusive 
fd290 6f 72 20 66 72 6f 6d 20 65 78 63 6c 75 73 69 76  or from exclusiv
fd2a0 65 20 74 6f 20 73 68 61 72 65 64 2e 0a 2a 2f 0a  e to shared..*/.
fd2b0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53  static int unixS
fd2c0 68 6d 4c 6f 63 6b 28 0a 20 20 73 71 6c 69 74 65  hmLock(.  sqlite
fd2d0 33 5f 66 69 6c 65 20 2a 66 64 2c 20 20 20 20 20  3_file *fd,     
fd2e0 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
fd2f0 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68   file holding th
fd300 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  e shared memory 
fd310 2a 2f 0a 20 20 69 6e 74 20 6f 66 73 74 2c 20 20  */.  int ofst,  
fd320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd330 2f 2a 20 46 69 72 73 74 20 6c 6f 63 6b 20 74 6f  /* First lock to
fd340 20 61 63 71 75 69 72 65 20 6f 72 20 72 65 6c 65   acquire or rele
fd350 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20  ase */.  int n, 
fd360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd370 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
fd380 20 6c 6f 63 6b 73 20 74 6f 20 61 63 71 75 69 72   locks to acquir
fd390 65 20 6f 72 20 72 65 6c 65 61 73 65 20 2a 2f 0a  e or release */.
fd3a0 20 20 69 6e 74 20 66 6c 61 67 73 20 20 20 20 20    int flags     
fd3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
fd3c0 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20  What to do with 
fd3d0 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20  the lock */.){. 
fd3e0 20 75 6e 69 78 46 69 6c 65 20 2a 70 44 62 46 64   unixFile *pDbFd
fd3f0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 66 64   = (unixFile*)fd
fd400 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63  ;      /* Connec
fd410 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 73 68 61  tion holding sha
fd420 72 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20  red memory */.  
fd430 75 6e 69 78 53 68 6d 20 2a 70 20 3d 20 70 44 62  unixShm *p = pDb
fd440 46 64 2d 3e 70 53 68 6d 3b 20 20 20 20 20 20 20  Fd->pShm;       
fd450 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68 61        /* The sha
fd460 72 65 64 20 6d 65 6d 6f 72 79 20 62 65 69 6e 67  red memory being
fd470 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 69   locked */.  uni
fd480 78 53 68 6d 20 2a 70 58 3b 20 20 20 20 20 20 20  xShm *pX;       
fd490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd4a0 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
fd4b0 67 20 6f 76 65 72 20 61 6c 6c 20 73 69 62 6c 69  g over all sibli
fd4c0 6e 67 73 20 2a 2f 0a 20 20 75 6e 69 78 53 68 6d  ngs */.  unixShm
fd4d0 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f 64 65 20 3d  Node *pShmNode =
fd4e0 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 3b 20 20 2f   p->pShmNode;  /
fd4f0 2a 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  * The underlying
fd500 20 66 69 6c 65 20 69 4e 6f 64 65 20 2a 2f 0a 20   file iNode */. 
fd510 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
fd520 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
fd530 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
fd540 20 63 6f 64 65 20 2a 2f 0a 20 20 75 31 36 20 6d   code */.  u16 m
fd550 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ask;            
fd560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd570 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6c 6f 63 6b   /* Mask of lock
fd580 73 20 74 6f 20 74 61 6b 65 20 6f 72 20 72 65 6c  s to take or rel
fd590 65 61 73 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72  ease */..  asser
fd5a0 74 28 20 70 53 68 6d 4e 6f 64 65 3d 3d 70 44 62  t( pShmNode==pDb
fd5b0 46 64 2d 3e 70 49 6e 6f 64 65 2d 3e 70 53 68 6d  Fd->pInode->pShm
fd5c0 4e 6f 64 65 20 29 3b 0a 20 20 61 73 73 65 72 74  Node );.  assert
fd5d0 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 49 6e 6f  ( pShmNode->pIno
fd5e0 64 65 3d 3d 70 44 62 46 64 2d 3e 70 49 6e 6f 64  de==pDbFd->pInod
fd5f0 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  e );.  assert( o
fd600 66 73 74 3e 3d 30 20 26 26 20 6f 66 73 74 2b 6e  fst>=0 && ofst+n
fd610 3c 3d 53 51 4c 49 54 45 5f 53 48 4d 5f 4e 4c 4f  <=SQLITE_SHM_NLO
fd620 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
fd630 6e 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74  n>=1 );.  assert
fd640 28 20 66 6c 61 67 73 3d 3d 28 53 51 4c 49 54 45  ( flags==(SQLITE
fd650 5f 53 48 4d 5f 4c 4f 43 4b 20 7c 20 53 51 4c 49  _SHM_LOCK | SQLI
fd660 54 45 5f 53 48 4d 5f 53 48 41 52 45 44 29 0a 20  TE_SHM_SHARED). 
fd670 20 20 20 20 20 20 7c 7c 20 66 6c 61 67 73 3d 3d        || flags==
fd680 28 53 51 4c 49 54 45 5f 53 48 4d 5f 4c 4f 43 4b  (SQLITE_SHM_LOCK
fd690 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f 45 58   | SQLITE_SHM_EX
fd6a0 43 4c 55 53 49 56 45 29 0a 20 20 20 20 20 20 20  CLUSIVE).       
fd6b0 7c 7c 20 66 6c 61 67 73 3d 3d 28 53 51 4c 49 54  || flags==(SQLIT
fd6c0 45 5f 53 48 4d 5f 55 4e 4c 4f 43 4b 20 7c 20 53  E_SHM_UNLOCK | S
fd6d0 51 4c 49 54 45 5f 53 48 4d 5f 53 48 41 52 45 44  QLITE_SHM_SHARED
fd6e0 29 0a 20 20 20 20 20 20 20 7c 7c 20 66 6c 61 67  ).       || flag
fd6f0 73 3d 3d 28 53 51 4c 49 54 45 5f 53 48 4d 5f 55  s==(SQLITE_SHM_U
fd700 4e 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53  NLOCK | SQLITE_S
fd710 48 4d 5f 45 58 43 4c 55 53 49 56 45 29 20 29 3b  HM_EXCLUSIVE) );
fd720 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 31 20  .  assert( n==1 
fd730 7c 7c 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49  || (flags & SQLI
fd740 54 45 5f 53 48 4d 5f 45 58 43 4c 55 53 49 56 45  TE_SHM_EXCLUSIVE
fd750 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  )!=0 );.  assert
fd760 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 68 3e 3d 30  ( pShmNode->h>=0
fd770 20 7c 7c 20 70 44 62 46 64 2d 3e 70 49 6e 6f 64   || pDbFd->pInod
fd780 65 2d 3e 62 50 72 6f 63 65 73 73 4c 6f 63 6b 3d  e->bProcessLock=
fd790 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =1 );.  assert( 
fd7a0 70 53 68 6d 4e 6f 64 65 2d 3e 68 3c 30 20 7c 7c  pShmNode->h<0 ||
fd7b0 20 70 44 62 46 64 2d 3e 70 49 6e 6f 64 65 2d 3e   pDbFd->pInode->
fd7c0 62 50 72 6f 63 65 73 73 4c 6f 63 6b 3d 3d 30 20  bProcessLock==0 
fd7d0 29 3b 0a 0a 20 20 6d 61 73 6b 20 3d 20 28 31 3c  );..  mask = (1<
fd7e0 3c 28 6f 66 73 74 2b 6e 29 29 20 2d 20 28 31 3c  <(ofst+n)) - (1<
fd7f0 3c 6f 66 73 74 29 3b 0a 20 20 61 73 73 65 72 74  <ofst);.  assert
fd800 28 20 6e 3e 31 20 7c 7c 20 6d 61 73 6b 3d 3d 28  ( n>1 || mask==(
fd810 31 3c 3c 6f 66 73 74 29 20 29 3b 0a 20 20 73 71  1<<ofst) );.  sq
fd820 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
fd830 72 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65  r(pShmNode->mute
fd840 78 29 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20  x);.  if( flags 
fd850 26 20 53 51 4c 49 54 45 5f 53 48 4d 5f 55 4e 4c  & SQLITE_SHM_UNL
fd860 4f 43 4b 20 29 7b 0a 20 20 20 20 75 31 36 20 61  OCK ){.    u16 a
fd870 6c 6c 4d 61 73 6b 20 3d 20 30 3b 20 2f 2a 20 4d  llMask = 0; /* M
fd880 61 73 6b 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c  ask of locks hel
fd890 64 20 62 79 20 73 69 62 6c 69 6e 67 73 20 2a 2f  d by siblings */
fd8a0 0a 0a 20 20 20 20 2f 2a 20 53 65 65 20 69 66 20  ..    /* See if 
fd8b0 61 6e 79 20 73 69 62 6c 69 6e 67 73 20 68 6f 6c  any siblings hol
fd8c0 64 20 74 68 69 73 20 73 61 6d 65 20 6c 6f 63 6b  d this same lock
fd8d0 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 58 3d 70   */.    for(pX=p
fd8e0 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 74 3b  ShmNode->pFirst;
fd8f0 20 70 58 3b 20 70 58 3d 70 58 2d 3e 70 4e 65 78   pX; pX=pX->pNex
fd900 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 58  t){.      if( pX
fd910 3d 3d 70 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==p ) continue;.
fd920 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
fd930 58 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 20 28 70  X->exclMask & (p
fd940 2d 3e 65 78 63 6c 4d 61 73 6b 7c 70 2d 3e 73 68  ->exclMask|p->sh
fd950 61 72 65 64 4d 61 73 6b 29 29 3d 3d 30 20 29 3b  aredMask))==0 );
fd960 0a 20 20 20 20 20 20 61 6c 6c 4d 61 73 6b 20 7c  .      allMask |
fd970 3d 20 70 58 2d 3e 73 68 61 72 65 64 4d 61 73 6b  = pX->sharedMask
fd980 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
fd990 55 6e 6c 6f 63 6b 20 74 68 65 20 73 79 73 74 65  Unlock the syste
fd9a0 6d 2d 6c 65 76 65 6c 20 6c 6f 63 6b 73 20 2a 2f  m-level locks */
fd9b0 0a 20 20 20 20 69 66 28 20 28 6d 61 73 6b 20 26  .    if( (mask &
fd9c0 20 61 6c 6c 4d 61 73 6b 29 3d 3d 30 20 29 7b 0a   allMask)==0 ){.
fd9d0 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69 78 53        rc = unixS
fd9e0 68 6d 53 79 73 74 65 6d 4c 6f 63 6b 28 70 53 68  hmSystemLock(pSh
fd9f0 6d 4e 6f 64 65 2c 20 46 5f 55 4e 4c 43 4b 2c 20  mNode, F_UNLCK, 
fda00 6f 66 73 74 2b 55 4e 49 58 5f 53 48 4d 5f 42 41  ofst+UNIX_SHM_BA
fda10 53 45 2c 20 6e 29 3b 0a 20 20 20 20 7d 65 6c 73  SE, n);.    }els
fda20 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  e{.      rc = SQ
fda30 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a  LITE_OK;.    }..
fda40 20 20 20 20 2f 2a 20 55 6e 64 6f 20 74 68 65 20      /* Undo the 
fda50 6c 6f 63 61 6c 20 6c 6f 63 6b 73 20 2a 2f 0a 20  local locks */. 
fda60 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
fda70 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d  E_OK ){.      p-
fda80 3e 65 78 63 6c 4d 61 73 6b 20 26 3d 20 7e 6d 61  >exclMask &= ~ma
fda90 73 6b 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68 61  sk;.      p->sha
fdaa0 72 65 64 4d 61 73 6b 20 26 3d 20 7e 6d 61 73 6b  redMask &= ~mask
fdab0 3b 0a 20 20 20 20 7d 20 0a 20 20 7d 65 6c 73 65  ;.    } .  }else
fdac0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
fdad0 49 54 45 5f 53 48 4d 5f 53 48 41 52 45 44 20 29  ITE_SHM_SHARED )
fdae0 7b 0a 20 20 20 20 75 31 36 20 61 6c 6c 53 68 61  {.    u16 allSha
fdaf0 72 65 64 20 3d 20 30 3b 20 20 2f 2a 20 55 6e 69  red = 0;  /* Uni
fdb00 6f 6e 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64  on of locks held
fdb10 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20   by connections 
fdb20 6f 74 68 65 72 20 74 68 61 6e 20 22 70 22 20 2a  other than "p" *
fdb30 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 6f  /..    /* Find o
fdb40 75 74 20 77 68 69 63 68 20 73 68 61 72 65 64 20  ut which shared 
fdb50 6c 6f 63 6b 73 20 61 72 65 20 61 6c 72 65 61 64  locks are alread
fdb60 79 20 68 65 6c 64 20 62 79 20 73 69 62 6c 69 6e  y held by siblin
fdb70 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 20  g connections.. 
fdb80 20 20 20 2a 2a 20 49 66 20 61 6e 79 20 73 69 62     ** If any sib
fdb90 6c 69 6e 67 20 61 6c 72 65 61 64 79 20 68 6f 6c  ling already hol
fdba0 64 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  ds an exclusive 
fdbb0 6c 6f 63 6b 2c 20 67 6f 20 61 68 65 61 64 20 61  lock, go ahead a
fdbc0 6e 64 20 72 65 74 75 72 6e 0a 20 20 20 20 2a 2a  nd return.    **
fdbd0 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 20 20   SQLITE_BUSY..  
fdbe0 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 58 3d    */.    for(pX=
fdbf0 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 74  pShmNode->pFirst
fdc00 3b 20 70 58 3b 20 70 58 3d 70 58 2d 3e 70 4e 65  ; pX; pX=pX->pNe
fdc10 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28  xt){.      if( (
fdc20 70 58 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 20 6d  pX->exclMask & m
fdc30 61 73 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ask)!=0 ){.     
fdc40 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
fdc50 55 53 59 3b 0a 20 20 20 20 20 20 20 20 62 72 65  USY;.        bre
fdc60 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
fdc70 20 20 61 6c 6c 53 68 61 72 65 64 20 7c 3d 20 70    allShared |= p
fdc80 58 2d 3e 73 68 61 72 65 64 4d 61 73 6b 3b 0a 20  X->sharedMask;. 
fdc90 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 74     }..    /* Get
fdca0 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 61 74   shared locks at
fdcb0 20 74 68 65 20 73 79 73 74 65 6d 20 6c 65 76 65   the system leve
fdcc0 6c 2c 20 69 66 20 6e 65 63 65 73 73 61 72 79 20  l, if necessary 
fdcd0 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  */.    if( rc==S
fdce0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
fdcf0 20 20 69 66 28 20 28 61 6c 6c 53 68 61 72 65 64    if( (allShared
fdd00 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a 20   & mask)==0 ){. 
fdd10 20 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69 78         rc = unix
fdd20 53 68 6d 53 79 73 74 65 6d 4c 6f 63 6b 28 70 53  ShmSystemLock(pS
fdd30 68 6d 4e 6f 64 65 2c 20 46 5f 52 44 4c 43 4b 2c  hmNode, F_RDLCK,
fdd40 20 6f 66 73 74 2b 55 4e 49 58 5f 53 48 4d 5f 42   ofst+UNIX_SHM_B
fdd50 41 53 45 2c 20 6e 29 3b 0a 20 20 20 20 20 20 7d  ASE, n);.      }
fdd60 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63  else{.        rc
fdd70 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
fdd80 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
fdd90 20 2f 2a 20 47 65 74 20 74 68 65 20 6c 6f 63 61   /* Get the loca
fdda0 6c 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 2a  l shared locks *
fddb0 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  /.    if( rc==SQ
fddc0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
fddd0 20 70 2d 3e 73 68 61 72 65 64 4d 61 73 6b 20 7c   p->sharedMask |
fdde0 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 7d 0a 20 20  = mask;.    }.  
fddf0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4d 61  }else{.    /* Ma
fde00 6b 65 20 73 75 72 65 20 6e 6f 20 73 69 62 6c 69  ke sure no sibli
fde10 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 68  ng connections h
fde20 6f 6c 64 20 6c 6f 63 6b 73 20 74 68 61 74 20 77  old locks that w
fde30 69 6c 6c 20 62 6c 6f 63 6b 20 74 68 69 73 0a 20  ill block this. 
fde40 20 20 20 2a 2a 20 6c 6f 63 6b 2e 20 20 49 66 20     ** lock.  If 
fde50 61 6e 79 20 64 6f 2c 20 72 65 74 75 72 6e 20 53  any do, return S
fde60 51 4c 49 54 45 5f 42 55 53 59 20 72 69 67 68 74  QLITE_BUSY right
fde70 20 61 77 61 79 2e 0a 20 20 20 20 2a 2f 0a 20 20   away..    */.  
fde80 20 20 66 6f 72 28 70 58 3d 70 53 68 6d 4e 6f 64    for(pX=pShmNod
fde90 65 2d 3e 70 46 69 72 73 74 3b 20 70 58 3b 20 70  e->pFirst; pX; p
fdea0 58 3d 70 58 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  X=pX->pNext){.  
fdeb0 20 20 20 20 69 66 28 20 28 70 58 2d 3e 65 78 63      if( (pX->exc
fdec0 6c 4d 61 73 6b 20 26 20 6d 61 73 6b 29 21 3d 30  lMask & mask)!=0
fded0 20 7c 7c 20 28 70 58 2d 3e 73 68 61 72 65 64 4d   || (pX->sharedM
fdee0 61 73 6b 20 26 20 6d 61 73 6b 29 21 3d 30 20 29  ask & mask)!=0 )
fdef0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
fdf00 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
fdf10 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
fdf20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20   }.    }.  .    
fdf30 2f 2a 20 47 65 74 20 74 68 65 20 65 78 63 6c 75  /* Get the exclu
fdf40 73 69 76 65 20 6c 6f 63 6b 73 20 61 74 20 74 68  sive locks at th
fdf50 65 20 73 79 73 74 65 6d 20 6c 65 76 65 6c 2e 20  e system level. 
fdf60 20 54 68 65 6e 20 69 66 20 73 75 63 63 65 73 73   Then if success
fdf70 66 75 6c 0a 20 20 20 20 2a 2a 20 61 6c 73 6f 20  ful.    ** also 
fdf80 6d 61 72 6b 20 74 68 65 20 6c 6f 63 61 6c 20 63  mark the local c
fdf90 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 62 65 69  onnection as bei
fdfa0 6e 67 20 6c 6f 63 6b 65 64 2e 0a 20 20 20 20 2a  ng locked..    *
fdfb0 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  /.    if( rc==SQ
fdfc0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
fdfd0 20 72 63 20 3d 20 75 6e 69 78 53 68 6d 53 79 73   rc = unixShmSys
fdfe0 74 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65  temLock(pShmNode
fdff0 2c 20 46 5f 57 52 4c 43 4b 2c 20 6f 66 73 74 2b  , F_WRLCK, ofst+
fe000 55 4e 49 58 5f 53 48 4d 5f 42 41 53 45 2c 20 6e  UNIX_SHM_BASE, n
fe010 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
fe020 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
fe030 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
fe040 2d 3e 73 68 61 72 65 64 4d 61 73 6b 20 26 20 6d  ->sharedMask & m
fe050 61 73 6b 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ask)==0 );.     
fe060 20 20 20 70 2d 3e 65 78 63 6c 4d 61 73 6b 20 7c     p->exclMask |
fe070 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 7d 0a  = mask;.      }.
fe080 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
fe090 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
fe0a0 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29  pShmNode->mutex)
fe0b0 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 53 48  ;.  OSTRACE(("SH
fe0c0 4d 2d 4c 4f 43 4b 20 73 68 6d 69 64 2d 25 64 2c  M-LOCK shmid-%d,
fe0d0 20 70 69 64 2d 25 64 20 67 6f 74 20 25 30 33 78   pid-%d got %03x
fe0e0 2c 25 30 33 78 5c 6e 22 2c 0a 20 20 20 20 20 20  ,%03x\n",.      
fe0f0 20 20 20 20 20 70 2d 3e 69 64 2c 20 67 65 74 70       p->id, getp
fe100 69 64 28 29 2c 20 70 2d 3e 73 68 61 72 65 64 4d  id(), p->sharedM
fe110 61 73 6b 2c 20 70 2d 3e 65 78 63 6c 4d 61 73 6b  ask, p->exclMask
fe120 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ));.  return rc;
fe130 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
fe140 65 6e 74 20 61 20 6d 65 6d 6f 72 79 20 62 61 72  ent a memory bar
fe150 72 69 65 72 20 6f 72 20 6d 65 6d 6f 72 79 20 66  rier or memory f
fe160 65 6e 63 65 20 6f 6e 20 73 68 61 72 65 64 20 6d  ence on shared m
fe170 65 6d 6f 72 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 41  emory.  .**.** A
fe180 6c 6c 20 6c 6f 61 64 73 20 61 6e 64 20 73 74 6f  ll loads and sto
fe190 72 65 73 20 62 65 67 75 6e 20 62 65 66 6f 72 65  res begun before
fe1a0 20 74 68 65 20 62 61 72 72 69 65 72 20 6d 75 73   the barrier mus
fe1b0 74 20 63 6f 6d 70 6c 65 74 65 20 62 65 66 6f 72  t complete befor
fe1c0 65 0a 2a 2a 20 61 6e 79 20 6c 6f 61 64 20 6f 72  e.** any load or
fe1d0 20 73 74 6f 72 65 20 62 65 67 75 6e 20 61 66 74   store begun aft
fe1e0 65 72 20 74 68 65 20 62 61 72 72 69 65 72 2e 0a  er the barrier..
fe1f0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  */.static void u
fe200 6e 69 78 53 68 6d 42 61 72 72 69 65 72 28 0a 20  nixShmBarrier(. 
fe210 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66   sqlite3_file *f
fe220 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
fe230 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c   /* Database fil
fe240 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 73 68  e holding the sh
fe250 61 72 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 29  ared memory */.)
fe260 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
fe270 45 54 45 52 28 66 64 29 3b 0a 20 20 75 6e 69 78  ETER(fd);.  unix
fe280 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
fe290 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
fe2a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
fe2b0 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f   a connection to
fe2c0 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 2e 20   shared-memory. 
fe2d0 20 44 65 6c 65 74 65 20 74 68 65 20 75 6e 64 65   Delete the unde
fe2e0 72 6c 79 69 6e 67 20 0a 2a 2a 20 73 74 6f 72 61  rlying .** stora
fe2f0 67 65 20 69 66 20 64 65 6c 65 74 65 46 6c 61 67  ge if deleteFlag
fe300 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20   is true..**.** 
fe310 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73  If there is no s
fe320 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 61 73 73  hared memory ass
fe330 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
fe340 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 65 6e   connection then
fe350 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
fe360 20 69 73 20 61 20 68 61 72 6d 6c 65 73 73 20 6e   is a harmless n
fe370 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
fe380 69 6e 74 20 75 6e 69 78 53 68 6d 55 6e 6d 61 70  int unixShmUnmap
fe390 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
fe3a0 20 2a 66 64 2c 20 20 20 20 20 20 20 20 20 20 20   *fd,           
fe3b0 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 64 65 72      /* The under
fe3c0 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 66  lying database f
fe3d0 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 64 65 6c  ile */.  int del
fe3e0 65 74 65 46 6c 61 67 20 20 20 20 20 20 20 20 20  eteFlag         
fe3f0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65           /* Dele
fe400 74 65 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79  te shared-memory
fe410 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20   if true */.){. 
fe420 20 75 6e 69 78 53 68 6d 20 2a 70 3b 20 20 20 20   unixShm *p;    
fe430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe440 20 2f 2a 20 54 68 65 20 63 6f 6e 6e 65 63 74 69   /* The connecti
fe450 6f 6e 20 74 6f 20 62 65 20 63 6c 6f 73 65 64 20  on to be closed 
fe460 2a 2f 0a 20 20 75 6e 69 78 53 68 6d 4e 6f 64 65  */.  unixShmNode
fe470 20 2a 70 53 68 6d 4e 6f 64 65 3b 20 20 20 20 20   *pShmNode;     
fe480 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 64 65       /* The unde
fe490 72 6c 79 69 6e 67 20 73 68 61 72 65 64 2d 6d 65  rlying shared-me
fe4a0 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 75  mory file */.  u
fe4b0 6e 69 78 53 68 6d 20 2a 2a 70 70 3b 20 20 20 20  nixShm **pp;    
fe4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
fe4d0 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  * For looping ov
fe4e0 65 72 20 73 69 62 6c 69 6e 67 20 63 6f 6e 6e 65  er sibling conne
fe4f0 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 6e 69 78  ctions */.  unix
fe500 46 69 6c 65 20 2a 70 44 62 46 64 3b 20 20 20 20  File *pDbFd;    
fe510 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
fe520 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61  he underlying da
fe530 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a  tabase file */..
fe540 20 20 70 44 62 46 64 20 3d 20 28 75 6e 69 78 46    pDbFd = (unixF
fe550 69 6c 65 2a 29 66 64 3b 0a 20 20 70 20 3d 20 70  ile*)fd;.  p = p
fe560 44 62 46 64 2d 3e 70 53 68 6d 3b 0a 20 20 69 66  DbFd->pShm;.  if
fe570 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
fe580 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 53 68  SQLITE_OK;.  pSh
fe590 6d 4e 6f 64 65 20 3d 20 70 2d 3e 70 53 68 6d 4e  mNode = p->pShmN
fe5a0 6f 64 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ode;..  assert( 
fe5b0 70 53 68 6d 4e 6f 64 65 3d 3d 70 44 62 46 64 2d  pShmNode==pDbFd-
fe5c0 3e 70 49 6e 6f 64 65 2d 3e 70 53 68 6d 4e 6f 64  >pInode->pShmNod
fe5d0 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e );.  assert( p
fe5e0 53 68 6d 4e 6f 64 65 2d 3e 70 49 6e 6f 64 65 3d  ShmNode->pInode=
fe5f0 3d 70 44 62 46 64 2d 3e 70 49 6e 6f 64 65 20 29  =pDbFd->pInode )
fe600 3b 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 63  ;..  /* Remove c
fe610 6f 6e 6e 65 63 74 69 6f 6e 20 70 20 66 72 6f 6d  onnection p from
fe620 20 74 68 65 20 73 65 74 20 6f 66 20 63 6f 6e 6e   the set of conn
fe630 65 63 74 69 6f 6e 73 20 61 73 73 6f 63 69 61 74  ections associat
fe640 65 64 0a 20 20 2a 2a 20 77 69 74 68 20 70 53 68  ed.  ** with pSh
fe650 6d 4e 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74  mNode */.  sqlit
fe660 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
fe670 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b  ShmNode->mutex);
fe680 0a 20 20 66 6f 72 28 70 70 3d 26 70 53 68 6d 4e  .  for(pp=&pShmN
fe690 6f 64 65 2d 3e 70 46 69 72 73 74 3b 20 28 2a 70  ode->pFirst; (*p
fe6a0 70 29 21 3d 70 3b 20 70 70 20 3d 20 26 28 2a 70  p)!=p; pp = &(*p
fe6b0 70 29 2d 3e 70 4e 65 78 74 29 7b 7d 0a 20 20 2a  p)->pNext){}.  *
fe6c0 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 0a  pp = p->pNext;..
fe6d0 20 20 2f 2a 20 46 72 65 65 20 74 68 65 20 63 6f    /* Free the co
fe6e0 6e 6e 65 63 74 69 6f 6e 20 70 20 2a 2f 0a 20 20  nnection p */.  
fe6f0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
fe700 0a 20 20 70 44 62 46 64 2d 3e 70 53 68 6d 20 3d  .  pDbFd->pShm =
fe710 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   0;.  sqlite3_mu
fe720 74 65 78 5f 6c 65 61 76 65 28 70 53 68 6d 4e 6f  tex_leave(pShmNo
fe730 64 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 2f  de->mutex);..  /
fe740 2a 20 49 66 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e  * If pShmNode->n
fe750 52 65 66 20 68 61 73 20 72 65 61 63 68 65 64 20  Ref has reached 
fe760 30 2c 20 74 68 65 6e 20 63 6c 6f 73 65 20 74 68  0, then close th
fe770 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 20 20 2a  e underlying.  *
fe780 2a 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20  * shared-memory 
fe790 66 69 6c 65 2c 20 74 6f 6f 20 2a 2f 0a 20 20 75  file, too */.  u
fe7a0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
fe7b0 0a 20 20 61 73 73 65 72 74 28 20 70 53 68 6d 4e  .  assert( pShmN
fe7c0 6f 64 65 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ode->nRef>0 );. 
fe7d0 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 66 2d   pShmNode->nRef-
fe7e0 2d 3b 0a 20 20 69 66 28 20 70 53 68 6d 4e 6f 64  -;.  if( pShmNod
fe7f0 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20  e->nRef==0 ){.  
fe800 20 20 69 66 28 20 64 65 6c 65 74 65 46 6c 61 67    if( deleteFlag
fe810 20 26 26 20 70 53 68 6d 4e 6f 64 65 2d 3e 68 3e   && pShmNode->h>
fe820 3d 30 20 29 20 6f 73 55 6e 6c 69 6e 6b 28 70 53  =0 ) osUnlink(pS
fe830 68 6d 4e 6f 64 65 2d 3e 7a 46 69 6c 65 6e 61 6d  hmNode->zFilenam
fe840 65 29 3b 0a 20 20 20 20 75 6e 69 78 53 68 6d 50  e);.    unixShmP
fe850 75 72 67 65 28 70 44 62 46 64 29 3b 0a 20 20 7d  urge(pDbFd);.  }
fe860 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65  .  unixLeaveMute
fe870 78 28 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53  x();..  return S
fe880 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 65  QLITE_OK;.}...#e
fe890 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 75 6e 69  lse.# define uni
fe8a0 78 53 68 6d 4d 61 70 20 20 20 20 20 30 0a 23 20  xShmMap     0.# 
fe8b0 64 65 66 69 6e 65 20 75 6e 69 78 53 68 6d 4c 6f  define unixShmLo
fe8c0 63 6b 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65  ck    0.# define
fe8d0 20 75 6e 69 78 53 68 6d 42 61 72 72 69 65 72 20   unixShmBarrier 
fe8e0 30 0a 23 20 64 65 66 69 6e 65 20 75 6e 69 78 53  0.# define unixS
fe8f0 68 6d 55 6e 6d 61 70 20 20 20 30 0a 23 65 6e 64  hmUnmap   0.#end
fe900 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51  if /* #ifndef SQ
fe910 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 20 2a 2f  LITE_OMIT_WAL */
fe920 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64  ../*.** Here end
fe930 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
fe940 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 73 71 6c 69  tion of all sqli
fe950 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73  te3_file methods
fe960 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
fe970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
fe980 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 4d 65 74  sqlite3_file Met
fe990 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  hods ***********
fe9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fe9b0 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
fe9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fe9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fe9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fe9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fea00 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ***/../*.** This
fea10 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69   division contai
fea20 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 6f  ns definitions o
fea30 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  f sqlite3_io_met
fea40 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 74 68 61  hods objects tha
fea50 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 76  t.** implement v
fea60 61 72 69 6f 75 73 20 66 69 6c 65 20 6c 6f 63 6b  arious file lock
fea70 69 6e 67 20 73 74 72 61 74 65 67 69 65 73 2e 20  ing strategies. 
fea80 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e   It also contain
fea90 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 0a 2a 2a  s definitions.**
feaa0 20 6f 66 20 22 66 69 6e 64 65 72 22 20 66 75 6e   of "finder" fun
feab0 63 74 69 6f 6e 73 2e 20 20 41 20 66 69 6e 64 65  ctions.  A finde
feac0 72 2d 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  r-function is us
fead0 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65  ed to locate the
feae0 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20   appropriate.** 
feaf0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
feb00 64 73 20 6f 62 6a 65 63 74 20 66 6f 72 20 61 20  ds object for a 
feb10 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62  particular datab
feb20 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70  ase file.  The p
feb30 41 70 70 44 61 74 61 0a 2a 2a 20 66 69 65 6c 64  AppData.** field
feb40 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
feb50 76 66 73 20 56 46 53 20 6f 62 6a 65 63 74 73 20  vfs VFS objects 
feb60 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  are initialized 
feb70 74 6f 20 62 65 20 70 6f 69 6e 74 65 72 73 20 74  to be pointers t
feb80 6f 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 74  o.** the correct
feb90 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e   finder-function
feba0 20 66 6f 72 20 74 68 61 74 20 56 46 53 2e 0a 2a   for that VFS..*
febb0 2a 0a 2a 2a 20 4d 6f 73 74 20 66 69 6e 64 65 72  *.** Most finder
febc0 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72   functions retur
febd0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  n a pointer to a
febe0 20 66 69 78 65 64 20 73 71 6c 69 74 65 33 5f 69   fixed sqlite3_i
febf0 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 6a  o_methods.** obj
fec00 65 63 74 2e 20 20 54 68 65 20 6f 6e 6c 79 20 69  ect.  The only i
fec10 6e 74 65 72 65 73 74 69 6e 67 20 66 69 6e 64 65  nteresting finde
fec20 72 2d 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 75  r-function is au
fec30 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20  tolockIoFinder, 
fec40 77 68 69 63 68 0a 2a 2a 20 6c 6f 6f 6b 73 20 61  which.** looks a
fec50 74 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d  t the filesystem
fec60 20 74 79 70 65 20 61 6e 64 20 74 72 69 65 73 20   type and tries 
fec70 74 6f 20 67 75 65 73 73 20 74 68 65 20 62 65 73  to guess the bes
fec80 74 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72  t locking.** str
fec90 61 74 65 67 79 20 66 72 6f 6d 20 74 68 61 74 2e  ategy from that.
feca0 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 69 6e 64 65  .**.** For finde
fecb0 72 2d 66 75 6e 74 69 6f 6e 20 46 2c 20 74 77 6f  r-funtion F, two
fecc0 20 6f 62 6a 65 63 74 73 20 61 72 65 20 63 72 65   objects are cre
fecd0 61 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28  ated:.**.**    (
fece0 31 29 20 54 68 65 20 72 65 61 6c 20 66 69 6e 64  1) The real find
fecf0 65 72 2d 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  er-function name
fed00 64 20 22 46 49 6d 70 74 28 29 22 2e 0a 2a 2a 0a  d "FImpt()"..**.
fed10 2a 2a 20 20 20 20 28 32 29 20 41 20 63 6f 6e 73  **    (2) A cons
fed20 74 61 6e 74 20 70 6f 69 6e 74 65 72 20 74 6f 20  tant pointer to 
fed30 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6e 61  this function na
fed40 6d 65 64 20 6a 75 73 74 20 22 46 22 2e 0a 2a 2a  med just "F"..**
fed50 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72  .**.** A pointer
fed60 20 74 6f 20 74 68 65 20 46 20 70 6f 69 6e 74 65   to the F pointe
fed70 72 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65  r is used as the
fed80 20 70 41 70 70 44 61 74 61 20 76 61 6c 75 65 20   pAppData value 
fed90 66 6f 72 20 56 46 53 0a 2a 2a 20 6f 62 6a 65 63  for VFS.** objec
feda0 74 73 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20  ts.  We have to 
fedb0 64 6f 20 74 68 69 73 20 69 6e 73 74 65 61 64 20  do this instead 
fedc0 6f 66 20 6c 65 74 74 69 6e 67 20 70 41 70 70 44  of letting pAppD
fedd0 61 74 61 20 70 6f 69 6e 74 0a 2a 2a 20 64 69 72  ata point.** dir
fede0 65 63 74 6c 79 20 61 74 20 74 68 65 20 66 69 6e  ectly at the fin
fedf0 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 73 69 6e  der-function sin
fee00 63 65 20 43 39 30 20 72 75 6c 65 73 20 70 72 65  ce C90 rules pre
fee10 76 65 6e 74 20 61 20 76 6f 69 64 2a 0a 2a 2a 20  vent a void*.** 
fee20 66 72 6f 6d 20 62 65 20 63 61 73 74 20 69 6e 74  from be cast int
fee30 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  o a function poi
fee40 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 45  nter..**.**.** E
fee50 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  ach instance of 
fee60 74 68 69 73 20 6d 61 63 72 6f 20 67 65 6e 65 72  this macro gener
fee70 61 74 65 73 20 74 77 6f 20 6f 62 6a 65 63 74 73  ates two objects
fee80 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 20 63  :.**.**   *  A c
fee90 6f 6e 73 74 61 6e 74 20 73 71 6c 69 74 65 33 5f  onstant sqlite3_
feea0 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63  io_methods objec
feeb0 74 20 63 61 6c 6c 20 4d 45 54 48 4f 44 20 74 68  t call METHOD th
feec0 61 74 20 68 61 73 20 6c 6f 63 6b 69 6e 67 0a 2a  at has locking.*
feed0 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 43  *      methods C
feee0 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f  LOSE, LOCK, UNLO
feef0 43 4b 2c 20 43 4b 52 45 53 4c 4f 43 4b 2e 0a 2a  CK, CKRESLOCK..*
fef00 2a 0a 2a 2a 20 20 20 2a 20 20 41 6e 20 49 2f 4f  *.**   *  An I/O
fef10 20 6d 65 74 68 6f 64 20 66 69 6e 64 65 72 20 66   method finder f
fef20 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65 64 20 46  unction called F
fef30 49 4e 44 45 52 20 74 68 61 74 20 72 65 74 75 72  INDER that retur
fef40 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  ns a pointer.** 
fef50 20 20 20 20 20 74 6f 20 74 68 65 20 4d 45 54 48       to the METH
fef60 4f 44 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65  OD object in the
fef70 20 70 72 65 76 69 6f 75 73 20 62 75 6c 6c 65 74   previous bullet
fef80 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 49 4f 4d  ..*/.#define IOM
fef90 45 54 48 4f 44 53 28 46 49 4e 44 45 52 2c 20 4d  ETHODS(FINDER, M
fefa0 45 54 48 4f 44 2c 20 56 45 52 53 49 4f 4e 2c 20  ETHOD, VERSION, 
fefb0 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c  CLOSE, LOCK, UNL
fefc0 4f 43 4b 2c 20 43 4b 4c 4f 43 4b 29 20 20 20 20  OCK, CKLOCK)    
fefd0 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73 74    \.static const
fefe0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
feff0 6f 64 73 20 4d 45 54 48 4f 44 20 3d 20 7b 20 20  ods METHOD = {  
ff000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff020 20 5c 0a 20 20 20 56 45 52 53 49 4f 4e 2c 20 20   \.   VERSION,  
ff030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff040 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f    /* iVersion */
ff050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff070 5c 0a 20 20 20 43 4c 4f 53 45 2c 20 20 20 20 20  \.   CLOSE,     
ff080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff090 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 20 20 20   /* xClose */   
ff0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ff0c0 0a 20 20 20 75 6e 69 78 52 65 61 64 2c 20 20 20  .   unixRead,   
ff0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff0e0 2f 2a 20 78 52 65 61 64 20 2a 2f 20 20 20 20 20  /* xRead */     
ff0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ff110 20 20 20 75 6e 69 78 57 72 69 74 65 2c 20 20 20     unixWrite,   
ff120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ff130 2a 20 78 57 72 69 74 65 20 2a 2f 20 20 20 20 20  * xWrite */     
ff140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff150 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ff160 20 20 75 6e 69 78 54 72 75 6e 63 61 74 65 2c 20    unixTruncate, 
ff170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
ff180 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 20 20 20   xTruncate */   
ff190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff1a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ff1b0 20 75 6e 69 78 53 79 6e 63 2c 20 20 20 20 20 20   unixSync,      
ff1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ff1d0 78 53 79 6e 63 20 2a 2f 20 20 20 20 20 20 20 20  xSync */        
ff1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff1f0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ff200 75 6e 69 78 46 69 6c 65 53 69 7a 65 2c 20 20 20  unixFileSize,   
ff210 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
ff220 46 69 6c 65 53 69 7a 65 20 2a 2f 20 20 20 20 20  FileSize */     
ff230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff240 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 4c            \.   L
ff250 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20  OCK,            
ff260 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c             /* xL
ff270 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ock */          
ff280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff290 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 55 4e           \.   UN
ff2a0 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20  LOCK,           
ff2b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e            /* xUn
ff2c0 6c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20  lock */         
ff2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff2e0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 43 4b 4c          \.   CKL
ff2f0 4f 43 4b 2c 20 20 20 20 20 20 20 20 20 20 20 20  OCK,            
ff300 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 68 65           /* xChe
ff310 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 2a  ckReservedLock *
ff320 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
ff330 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78         \.   unix
ff340 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 20 20 20 20  FileControl,    
ff350 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65          /* xFile
ff360 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20 20 20 20 20  Control */      
ff370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff380 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 53        \.   unixS
ff390 65 63 74 6f 72 53 69 7a 65 2c 20 20 20 20 20 20  ectorSize,      
ff3a0 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 74 6f         /* xSecto
ff3b0 72 53 69 7a 65 20 2a 2f 20 20 20 20 20 20 20 20  rSize */        
ff3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff3d0 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 44 65       \.   unixDe
ff3e0 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
ff3f0 69 63 73 2c 20 20 2f 2a 20 78 44 65 76 69 63 65  ics,  /* xDevice
ff400 43 61 70 61 62 69 6c 69 74 69 65 73 20 2a 2f 20  Capabilities */ 
ff410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff420 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 68 6d      \.   unixShm
ff430 4d 61 70 2c 20 20 20 20 20 20 20 20 20 20 20 20  Map,            
ff440 20 20 20 20 20 2f 2a 20 78 53 68 6d 4d 61 70 20       /* xShmMap 
ff450 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
ff460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff470 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 68 6d 4c     \.   unixShmL
ff480 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20  ock,            
ff490 20 20 20 20 2f 2a 20 78 53 68 6d 4c 6f 63 6b 20      /* xShmLock 
ff4a0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
ff4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff4c0 20 20 5c 0a 20 20 20 75 6e 69 78 53 68 6d 42 61    \.   unixShmBa
ff4d0 72 72 69 65 72 2c 20 20 20 20 20 20 20 20 20 20  rrier,          
ff4e0 20 20 20 2f 2a 20 78 53 68 6d 42 61 72 72 69 65     /* xShmBarrie
ff4f0 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  r */            
ff500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff510 20 5c 0a 20 20 20 75 6e 69 78 53 68 6d 55 6e 6d   \.   unixShmUnm
ff520 61 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ap              
ff530 20 20 2f 2a 20 78 53 68 6d 55 6e 6d 61 70 20 2a    /* xShmUnmap *
ff540 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
ff550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff560 5c 0a 7d 3b 20 20 20 20 20 20 20 20 20 20 20 20  \.};            
ff570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ff5b0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
ff5c0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
ff5d0 20 2a 46 49 4e 44 45 52 23 23 49 6d 70 6c 28 63   *FINDER##Impl(c
ff5e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 75 6e  onst char *z, un
ff5f0 69 78 46 69 6c 65 20 2a 70 29 7b 20 20 20 5c 0a  ixFile *p){   \.
ff600 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
ff610 45 52 28 7a 29 3b 20 55 4e 55 53 45 44 5f 50 41  ER(z); UNUSED_PA
ff620 52 41 4d 45 54 45 52 28 70 29 3b 20 20 20 20 20  RAMETER(p);     
ff630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff640 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ff650 20 72 65 74 75 72 6e 20 26 4d 45 54 48 4f 44 3b   return &METHOD;
ff660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff690 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 20              \.} 
ff6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff6e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61             \.sta
ff6f0 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
ff700 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a  3_io_methods *(*
ff710 63 6f 6e 73 74 20 46 49 4e 44 45 52 29 28 63 6f  const FINDER)(co
ff720 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 78 46 69  nst char*,unixFi
ff730 6c 65 20 2a 70 29 20 20 20 20 5c 0a 20 20 20 20  le *p)    \.    
ff740 3d 20 46 49 4e 44 45 52 23 23 49 6d 70 6c 3b 0a  = FINDER##Impl;.
ff750 0a 2f 2a 0a 2a 2a 20 48 65 72 65 20 61 72 65 20  ./*.** Here are 
ff760 61 6c 6c 20 6f 66 20 74 68 65 20 73 71 6c 69 74  all of the sqlit
ff770 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
ff780 6a 65 63 74 73 20 66 6f 72 20 65 61 63 68 20 6f  jects for each o
ff790 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67  f the.** locking
ff7a0 20 73 74 72 61 74 65 67 69 65 73 2e 20 20 46 75   strategies.  Fu
ff7b0 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 72 65 74  nctions that ret
ff7c0 75 72 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  urn pointers to 
ff7d0 74 68 65 73 65 20 6d 65 74 68 6f 64 73 0a 2a 2a  these methods.**
ff7e0 20 61 72 65 20 61 6c 73 6f 20 63 72 65 61 74 65   are also create
ff7f0 64 2e 0a 2a 2f 0a 49 4f 4d 45 54 48 4f 44 53 28  d..*/.IOMETHODS(
ff800 0a 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72  .  posixIoFinder
ff810 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
ff820 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20  Finder function 
ff830 6e 61 6d 65 20 2a 2f 0a 20 20 70 6f 73 69 78 49  name */.  posixI
ff840 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20  oMethods,       
ff850 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69      /* sqlite3_i
ff860 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
ff870 20 6e 61 6d 65 20 2a 2f 0a 20 20 32 2c 20 20 20   name */.  2,   
ff880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff890 20 20 20 20 20 2f 2a 20 73 68 61 72 65 64 20 6d       /* shared m
ff8a0 65 6d 6f 72 79 20 69 73 20 65 6e 61 62 6c 65 64  emory is enabled
ff8b0 20 2a 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c   */.  unixClose,
ff8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff8d0 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  /* xClose method
ff8e0 20 2a 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20   */.  unixLock, 
ff8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff900 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  /* xLock method 
ff910 2a 2f 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c  */.  unixUnlock,
ff920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ff930 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64  * xUnlock method
ff940 20 2a 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b 52   */.  unixCheckR
ff950 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20  eservedLock     
ff960 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
ff970 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  dLock method */.
ff980 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e  ).IOMETHODS(.  n
ff990 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20  olockIoFinder,  
ff9a0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64           /* Find
ff9b0 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  er function name
ff9c0 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65   */.  nolockIoMe
ff9d0 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20  thods,          
ff9e0 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  /* sqlite3_io_me
ff9f0 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d  thods object nam
ffa00 65 20 2a 2f 0a 20 20 31 2c 20 20 20 20 20 20 20  e */.  1,       
ffa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ffa20 20 2f 2a 20 73 68 61 72 65 64 20 6d 65 6d 6f 72   /* shared memor
ffa30 79 20 69 73 20 64 69 73 61 62 6c 65 64 20 2a 2f  y is disabled */
ffa40 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 2c 20  .  nolockClose, 
ffa50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ffa60 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f  xClose method */
ffa70 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c 20 20  .  nolockLock,  
ffa80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ffa90 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  xLock method */.
ffaa0 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20    nolockUnlock, 
ffab0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
ffac0 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  Unlock method */
ffad0 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65  .  nolockCheckRe
ffae0 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f 2a 20  servedLock   /* 
ffaf0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
ffb00 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 49  ck method */.).I
ffb10 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f 74 6c  OMETHODS(.  dotl
ffb20 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20  ockIoFinder,    
ffb30 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20        /* Finder 
ffb40 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  function name */
ffb50 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68  .  dotlockIoMeth
ffb60 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ods,         /* 
ffb70 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
ffb80 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ds object name *
ffb90 2f 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20  /.  1,          
ffba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
ffbb0 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 69   shared memory i
ffbc0 73 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20 20  s disabled */.  
ffbd0 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 2c 20 20 20  dotlockClose,   
ffbe0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c            /* xCl
ffbf0 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ose method */.  
ffc00 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 2c 20 20 20 20  dotlockLock,    
ffc10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f            /* xLo
ffc20 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 64  ck method */.  d
ffc30 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 20 20  otlockUnlock,   
ffc40 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c           /* xUnl
ffc50 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ock method */.  
ffc60 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65  dotlockCheckRese
ffc70 72 76 65 64 4c 6f 63 6b 20 20 2f 2a 20 78 43 68  rvedLock  /* xCh
ffc80 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20  eckReservedLock 
ffc90 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 0a 23 69 66  method */.)..#if
ffca0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
ffcb0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20  OCKING_STYLE && 
ffcc0 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45  !OS_VXWORKS.IOME
ffcd0 54 48 4f 44 53 28 0a 20 20 66 6c 6f 63 6b 49 6f  THODS(.  flockIo
ffce0 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20  Finder,         
ffcf0 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e     /* Finder fun
ffd00 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  ction name */.  
ffd10 66 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 2c 20  flockIoMethods, 
ffd20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c            /* sql
ffd30 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
ffd40 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20  object name */. 
ffd50 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   1,             
ffd60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68             /* sh
ffd70 61 72 65 64 20 6d 65 6d 6f 72 79 20 69 73 20 64  ared memory is d
ffd80 69 73 61 62 6c 65 64 20 2a 2f 0a 20 20 66 6c 6f  isabled */.  flo
ffd90 63 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  ckClose,        
ffda0 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
ffdb0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f   method */.  flo
ffdc0 63 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  ckLock,         
ffdd0 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20         /* xLock 
ffde0 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63  method */.  floc
ffdf0 6b 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20  kUnlock,        
ffe00 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b        /* xUnlock
ffe10 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f   method */.  flo
ffe20 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  ckCheckReservedL
ffe30 6f 63 6b 20 20 20 20 2f 2a 20 78 43 68 65 63 6b  ock    /* xCheck
ffe40 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74  ReservedLock met
ffe50 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a  hod */.).#endif.
ffe60 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  .#if OS_VXWORKS.
ffe70 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 73 65 6d  IOMETHODS(.  sem
ffe80 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20  IoFinder,       
ffe90 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72         /* Finder
ffea0 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a   function name *
ffeb0 2f 0a 20 20 73 65 6d 49 6f 4d 65 74 68 6f 64 73  /.  semIoMethods
ffec0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
ffed0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
ffee0 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20  ods object name 
ffef0 2a 2f 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20  */.  1,         
fff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
fff10 2a 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  * shared memory 
fff20 69 73 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20  is disabled */. 
fff30 20 73 65 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20   semClose,      
fff40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
fff50 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  lose method */. 
fff60 20 73 65 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20   semLock,       
fff70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c             /* xL
fff80 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ock method */.  
fff90 73 65 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20  semUnlock,      
fffa0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e            /* xUn
fffb0 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  lock method */. 
fffc0 20 73 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65   semCheckReserve
fffd0 64 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43  dLock      /* xC
fffe0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
ffff0 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e   method */.).#en
10000 64 69 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  dif..#if defined
10001 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53  (__APPLE__) && S
10002 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
10003 4b 49 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54  KING_STYLE.IOMET
10004 48 4f 44 53 28 0a 20 20 61 66 70 49 6f 46 69 6e  HODS(.  afpIoFin
10005 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  der,            
10006 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63    /* Finder func
10007 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61  tion name */.  a
10008 66 70 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20  fpIoMethods,    
10009 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69           /* sqli
1000a 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f  te3_io_methods o
1000b 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20  bject name */.  
1000c 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
1000d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61            /* sha
1000e 72 65 64 20 6d 65 6d 6f 72 79 20 69 73 20 64 69  red memory is di
1000f 73 61 62 6c 65 64 20 2a 2f 0a 20 20 61 66 70 43  sabled */.  afpC
10010 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  lose,           
10011 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20        /* xClose 
10012 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 4c  method */.  afpL
10013 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20  ock,            
10014 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d        /* xLock m
10015 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 55 6e  ethod */.  afpUn
10016 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  lock,           
10017 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20       /* xUnlock 
10018 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 43  method */.  afpC
10019 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
1001a 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52        /* xCheckR
1001b 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68  eservedLock meth
1001c 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a  od */.).#endif..
1001d 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20  /*.** The proxy 
1001e 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 69  locking method i
1001f 73 20 61 20 22 73 75 70 65 72 2d 6d 65 74 68 6f  s a "super-metho
10020 64 22 20 69 6e 20 74 68 65 20 73 65 6e 73 65 20  d" in the sense 
10021 74 68 61 74 20 69 74 0a 2a 2a 20 6f 70 65 6e 73  that it.** opens
10022 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 20   secondary file 
10023 64 65 73 63 72 69 70 74 6f 72 73 20 66 6f 72 20  descriptors for 
10024 74 68 65 20 63 6f 6e 63 68 20 61 6e 64 20 6c 6f  the conch and lo
10025 63 6b 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20  ck files and.** 
10026 69 74 20 75 73 65 73 20 70 72 6f 78 79 2c 20 64  it uses proxy, d
10027 6f 74 2d 66 69 6c 65 2c 20 41 46 50 2c 20 61 6e  ot-file, AFP, an
10028 64 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e  d flock() lockin
10029 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68 6f  g methods on tho
1002a 73 65 0a 2a 2a 20 73 65 63 6f 6e 64 61 72 79 20  se.** secondary 
1002b 66 69 6c 65 73 2e 20 20 46 6f 72 20 74 68 69 73  files.  For this
1002c 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76   reason, the div
1002d 69 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65  ision that imple
1002e 6d 65 6e 74 73 0a 2a 2a 20 70 72 6f 78 79 20 6c  ments.** proxy l
1002f 6f 63 6b 69 6e 67 20 69 73 20 6c 6f 63 61 74 65  ocking is locate
10030 64 20 6d 75 63 68 20 66 75 72 74 68 65 72 20 64  d much further d
10031 6f 77 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 2e  own in the file.
10032 20 20 42 75 74 20 77 65 20 6e 65 65 64 0a 2a 2a    But we need.**
10033 20 74 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64   to go ahead and
10034 20 64 65 66 69 6e 65 20 74 68 65 20 73 71 6c 69   define the sqli
10035 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 61  te3_io_methods a
10036 6e 64 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69  nd finder functi
10037 6f 6e 0a 2a 2a 20 66 6f 72 20 70 72 6f 78 79 20  on.** for proxy 
10038 6c 6f 63 6b 69 6e 67 20 68 65 72 65 2e 20 20 53  locking here.  S
10039 6f 20 77 65 20 66 6f 72 77 61 72 64 20 64 65 63  o we forward dec
1003a 6c 61 72 65 20 74 68 65 20 49 2f 4f 20 6d 65 74  lare the I/O met
1003b 68 6f 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  hods..*/.#if def
1003c 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20  ined(__APPLE__) 
1003d 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
1003e 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 73  _LOCKING_STYLE.s
1003f 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43  tatic int proxyC
10040 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  lose(sqlite3_fil
10041 65 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20  e*);.static int 
10042 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65  proxyLock(sqlite
10043 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73  3_file*, int);.s
10044 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 55  tatic int proxyU
10045 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  nlock(sqlite3_fi
10046 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69  le*, int);.stati
10047 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b  c int proxyCheck
10048 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c  ReservedLock(sql
10049 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2a  ite3_file*, int*
1004a 29 3b 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20  );.IOMETHODS(.  
1004b 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 2c 20 20  proxyIoFinder,  
1004c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e            /* Fin
1004d 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  der function nam
1004e 65 20 2a 2f 0a 20 20 70 72 6f 78 79 49 6f 4d 65  e */.  proxyIoMe
1004f 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20  thods,          
10050 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d   /* sqlite3_io_m
10051 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61  ethods object na
10052 6d 65 20 2a 2f 0a 20 20 31 2c 20 20 20 20 20 20  me */.  1,      
10053 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10054 20 20 2f 2a 20 73 68 61 72 65 64 20 6d 65 6d 6f    /* shared memo
10055 72 79 20 69 73 20 64 69 73 61 62 6c 65 64 20 2a  ry is disabled *
10056 2f 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65 2c 20  /.  proxyClose, 
10057 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10058 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a   xClose method *
10059 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c 20 20  /.  proxyLock,  
1005a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1005b 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f   xLock method */
1005c 0a 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 2c 20  .  proxyUnlock, 
1005d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1005e 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  xUnlock method *
1005f 2f 0a 20 20 70 72 6f 78 79 43 68 65 63 6b 52 65  /.  proxyCheckRe
10060 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 2f 2a  servedLock    /*
10061 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
10062 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a  ock method */.).
10063 23 65 6e 64 69 66 0a 0a 2f 2a 20 6e 66 73 20 6c  #endif../* nfs l
10064 6f 63 6b 64 20 6f 6e 20 4f 53 58 20 31 30 2e 33  ockd on OSX 10.3
10065 2b 20 64 6f 65 73 6e 27 74 20 63 6c 65 61 72 20  + doesn't clear 
10066 77 72 69 74 65 20 6c 6f 63 6b 73 20 77 68 65 6e  write locks when
10067 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20   a read lock is 
10068 73 65 74 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e  set */.#if defin
10069 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26  ed(__APPLE__) &&
1006a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
1006b 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d  OCKING_STYLE.IOM
1006c 45 54 48 4f 44 53 28 0a 20 20 6e 66 73 49 6f 46  ETHODS(.  nfsIoF
1006d 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20  inder,          
1006e 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66       /* Finder f
1006f 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a  unction name */.
10070 20 20 6e 66 73 49 6f 4d 65 74 68 6f 64 73 2c 20    nfsIoMethods, 
10071 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10072 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
10073 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ds object name *
10074 2f 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20  /.  1,          
10075 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10076 2a 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  * shared memory 
10077 69 73 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20  is disabled */. 
10078 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20 20 20 20   unixClose,     
10079 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1007a 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a  Close method */.
1007b 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20 20 20    unixLock,     
1007c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1007d 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  xLock method */.
1007e 20 20 6e 66 73 55 6e 6c 6f 63 6b 2c 20 20 20 20    nfsUnlock,    
1007f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10080 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  xUnlock method *
10081 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73  /.  unixCheckRes
10082 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 20 2f  ervedLock      /
10083 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  * xCheckReserved
10084 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29  Lock method */.)
10085 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 64 65 66  .#endif..#if def
10086 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20  ined(__APPLE__) 
10087 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
10088 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f  _LOCKING_STYLE./
10089 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 6e 64  * .** This "find
1008a 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 74 74  er" function att
1008b 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d 69  empts to determi
1008c 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b  ne the best lock
1008d 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a 2a 2a  ing strategy .**
1008e 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73   for the databas
1008f 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 74 68  e file "filePath
10090 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 74 75  ".  It then retu
10091 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f  rns the sqlite3_
10092 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62  io_methods.** ob
10093 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d  ject that implem
10094 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 74 65  ents that strate
10095 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  gy..**.** This i
10096 73 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f 6e 6c  s for MacOSX onl
10097 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  y..*/.static con
10098 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
10099 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 6b 49  thods *autolockI
1009a 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 20 63  oFinderImpl(.  c
1009b 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 50  onst char *fileP
1009c 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d 65 20  ath,    /* name 
1009d 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1009e 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69  file */.  unixFi
1009f 6c 65 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20  le *pNew        
100a0 20 20 20 2f 2a 20 6f 70 65 6e 20 66 69 6c 65 20     /* open file 
100a1 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 64  object for the d
100a2 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
100a3 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
100a4 74 20 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67  t struct Mapping
100a5 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61   {.    const cha
100a6 72 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20  r *zFilesystem; 
100a7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
100a8 46 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20  Filesystem type 
100a9 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 63 6f 6e 73  name */.    cons
100aa 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
100ab 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20  hods *pMethods; 
100ac 20 20 2f 2a 20 41 70 70 72 6f 70 72 69 61 74 65    /* Appropriate
100ad 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20   locking method 
100ae 2a 2f 0a 20 20 7d 20 61 4d 61 70 5b 5d 20 3d 20  */.  } aMap[] = 
100af 7b 0a 20 20 20 20 7b 20 22 68 66 73 22 2c 20 20  {.    { "hfs",  
100b0 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64    &posixIoMethod
100b1 73 20 7d 2c 0a 20 20 20 20 7b 20 22 75 66 73 22  s },.    { "ufs"
100b2 2c 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74  ,    &posixIoMet
100b3 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 61  hods },.    { "a
100b4 66 70 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65  fpfs",  &afpIoMe
100b5 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22  thods },.    { "
100b6 73 6d 62 66 73 22 2c 20 20 26 61 66 70 49 6f 4d  smbfs",  &afpIoM
100b7 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20  ethods },.    { 
100b8 22 77 65 62 64 61 76 22 2c 20 26 6e 6f 6c 6f 63  "webdav", &noloc
100b9 6b 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20  kIoMethods },.  
100ba 20 20 7b 20 30 2c 20 30 20 7d 0a 20 20 7d 3b 0a    { 0, 0 }.  };.
100bb 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63    int i;.  struc
100bc 74 20 73 74 61 74 66 73 20 66 73 49 6e 66 6f 3b  t statfs fsInfo;
100bd 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20  .  struct flock 
100be 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66 28  lockInfo;..  if(
100bf 20 21 66 69 6c 65 50 61 74 68 20 29 7b 0a 20 20   !filePath ){.  
100c0 20 20 2f 2a 20 49 66 20 66 69 6c 65 50 61 74 68    /* If filePath
100c1 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e  ==NULL that mean
100c2 73 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67  s we are dealing
100c3 20 77 69 74 68 20 61 20 74 72 61 6e 73 69 65 6e   with a transien
100c4 74 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68  t file.    ** th
100c5 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  at does not need
100c6 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 2e 20 2a   to be locked. *
100c7 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 26 6e 6f  /.    return &no
100c8 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20  lockIoMethods;. 
100c9 20 7d 0a 20 20 69 66 28 20 73 74 61 74 66 73 28   }.  if( statfs(
100ca 66 69 6c 65 50 61 74 68 2c 20 26 66 73 49 6e 66  filePath, &fsInf
100cb 6f 29 20 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20  o) != -1 ){.    
100cc 69 66 28 20 66 73 49 6e 66 6f 2e 66 5f 66 6c 61  if( fsInfo.f_fla
100cd 67 73 20 26 20 4d 4e 54 5f 52 44 4f 4e 4c 59 20  gs & MNT_RDONLY 
100ce 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
100cf 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73  &nolockIoMethods
100d0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
100d1 69 3d 30 3b 20 61 4d 61 70 5b 69 5d 2e 7a 46 69  i=0; aMap[i].zFi
100d2 6c 65 73 79 73 74 65 6d 3b 20 69 2b 2b 29 7b 0a  lesystem; i++){.
100d3 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70        if( strcmp
100d4 28 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70 65  (fsInfo.f_fstype
100d5 6e 61 6d 65 2c 20 61 4d 61 70 5b 69 5d 2e 7a 46  name, aMap[i].zF
100d6 69 6c 65 73 79 73 74 65 6d 29 3d 3d 30 20 29 7b  ilesystem)==0 ){
100d7 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
100d8 61 4d 61 70 5b 69 5d 2e 70 4d 65 74 68 6f 64 73  aMap[i].pMethods
100d9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
100da 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 66 61 75 6c    }..  /* Defaul
100db 74 20 63 61 73 65 2e 20 48 61 6e 64 6c 65 73 2c  t case. Handles,
100dc 20 61 6d 6f 6e 67 73 74 20 6f 74 68 65 72 73 2c   amongst others,
100dd 20 22 6e 66 73 22 2e 0a 20 20 2a 2a 20 54 65 73   "nfs"..  ** Tes
100de 74 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63  t byte-range loc
100df 6b 20 75 73 69 6e 67 20 66 63 6e 74 6c 28 29 2e  k using fcntl().
100e0 20 49 66 20 74 68 65 20 63 61 6c 6c 20 73 75 63   If the call suc
100e1 63 65 65 64 73 2c 20 0a 20 20 2a 2a 20 61 73 73  ceeds, .  ** ass
100e2 75 6d 65 20 74 68 61 74 20 74 68 65 20 66 69 6c  ume that the fil
100e3 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74  e-system support
100e4 73 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f  s POSIX style lo
100e5 63 6b 73 2e 20 0a 20 20 2a 2f 0a 20 20 6c 6f 63  cks. .  */.  loc
100e6 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b  kInfo.l_len = 1;
100e7 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74  .  lockInfo.l_st
100e8 61 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49  art = 0;.  lockI
100e9 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53  nfo.l_whence = S
100ea 45 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49  EEK_SET;.  lockI
100eb 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52  nfo.l_type = F_R
100ec 44 4c 43 4b 3b 0a 20 20 69 66 28 20 6f 73 46 63  DLCK;.  if( osFc
100ed 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c 20 46 5f 47  ntl(pNew->h, F_G
100ee 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e 66 6f 29  ETLK, &lockInfo)
100ef 21 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69 66 28  !=-1 ) {.    if(
100f0 20 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66   strcmp(fsInfo.f
100f1 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 22 6e 66  _fstypename, "nf
100f2 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  s")==0 ){.      
100f3 72 65 74 75 72 6e 20 26 6e 66 73 49 6f 4d 65 74  return &nfsIoMet
100f4 68 6f 64 73 3b 0a 20 20 20 20 7d 20 65 6c 73 65  hods;.    } else
100f5 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   {.      return 
100f6 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b  &posixIoMethods;
100f7 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
100f8 20 20 20 20 72 65 74 75 72 6e 20 26 64 6f 74 6c      return &dotl
100f9 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20  ockIoMethods;.  
100fa 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  }.}.static const
100fb 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
100fc 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20  ods .  *(*const 
100fd 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72  autolockIoFinder
100fe 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e  )(const char*,un
100ff 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f 6c  ixFile*) = autol
10100 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b  ockIoFinderImpl;
10101 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69  ..#endif /* defi
10102 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26  ned(__APPLE__) &
10103 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  & SQLITE_ENABLE_
10104 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f  LOCKING_STYLE */
10105 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53  ..#if OS_VXWORKS
10106 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
10107 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
10108 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 6e  /* .** This "fin
10109 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 74  der" function at
1010a 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d  tempts to determ
1010b 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f 63  ine the best loc
1010c 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a 2a  king strategy .*
1010d 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61  * for the databa
1010e 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 74  se file "filePat
1010f 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 74  h".  It then ret
10110 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33  urns the sqlite3
10111 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f  _io_methods.** o
10112 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65  bject that imple
10113 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 74  ments that strat
10114 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  egy..**.** This 
10115 69 73 20 66 6f 72 20 56 58 57 6f 72 6b 73 20 6f  is for VXWorks o
10116 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  nly..*/.static c
10117 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
10118 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63  methods *autoloc
10119 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20  kIoFinderImpl(. 
1011a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c   const char *fil
1011b 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d  ePath,    /* nam
1011c 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
1011d 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78  e file */.  unix
1011e 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20 20 20  File *pNew      
1011f 20 20 20 20 20 2f 2a 20 74 68 65 20 6f 70 65 6e       /* the open
10120 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 2a 2f 0a   file object */.
10121 29 7b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63  ){.  struct floc
10122 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20 69  k lockInfo;..  i
10123 66 28 20 21 66 69 6c 65 50 61 74 68 20 29 7b 0a  f( !filePath ){.
10124 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65 50 61      /* If filePa
10125 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d 65  th==NULL that me
10126 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 6c 69  ans we are deali
10127 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e 73 69  ng with a transi
10128 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20  ent file.    ** 
10129 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65  that does not ne
1012a 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 2e  ed to be locked.
1012b 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 26   */.    return &
1012c 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b  nolockIoMethods;
1012d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20  .  }..  /* Test 
1012e 69 66 20 66 63 6e 74 6c 28 29 20 69 73 20 73 75  if fcntl() is su
1012f 70 70 6f 72 74 65 64 20 61 6e 64 20 75 73 65 20  pported and use 
10130 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63 6b  POSIX style lock
10131 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73  s..  ** Otherwis
10132 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 74  e fall back to t
10133 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70 68 6f  he named semapho
10134 72 65 20 6d 65 74 68 6f 64 2e 0a 20 20 2a 2f 0a  re method..  */.
10135 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e    lockInfo.l_len
10136 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f   = 1;.  lockInfo
10137 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20  .l_start = 0;.  
10138 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63  lockInfo.l_whenc
10139 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20  e = SEEK_SET;.  
1013a 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 20  lockInfo.l_type 
1013b 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 28  = F_RDLCK;.  if(
1013c 20 6f 73 46 63 6e 74 6c 28 70 4e 65 77 2d 3e 68   osFcntl(pNew->h
1013d 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_GETLK, &lock
1013e 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20  Info)!=-1 ) {.  
1013f 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49    return &posixI
10140 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73  oMethods;.  }els
10141 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 73  e{.    return &s
10142 65 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d  emIoMethods;.  }
10143 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .}.static const 
10144 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
10145 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 61  ds .  *(*const a
10146 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29  utolockIoFinder)
10147 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69  (const char*,uni
10148 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f 6c 6f  xFile*) = autolo
10149 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a  ckIoFinderImpl;.
1014a 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58  .#endif /* OS_VX
1014b 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f  WORKS && SQLITE_
1014c 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
1014d 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  TYLE */../*.** A
1014e 6e 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20  n abstract type 
1014f 66 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  for a pointer to
10150 20 61 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e   a IO method fin
10151 64 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f  der function:.*/
10152 0a 74 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73  .typedef const s
10153 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
10154 73 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65  s *(*finder_type
10155 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e  )(const char*,un
10156 69 78 46 69 6c 65 2a 29 3b 0a 0a 0a 2f 2a 2a 2a  ixFile*);.../***
10157 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10158 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10159 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1015a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1015b 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
1015c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1015d 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76  ****** sqlite3_v
1015e 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a  fs methods *****
1015f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10160 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
10161 69 73 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74  is division cont
10162 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  ains the impleme
10163 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f  ntation of metho
10164 64 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c  ds on the.** sql
10165 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e  ite3_vfs object.
10166 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  .*/../*.** Initi
10167 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74 65 6e  alize the conten
10168 74 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69  ts of the unixFi
10169 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69  le structure poi
1016a 6e 74 65 64 20 74 6f 20 62 79 20 70 49 64 2e 0a  nted to by pId..
1016b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69  */.static int fi
1016c 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20  llInUnixFile(.  
1016d 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
1016e 73 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  s,      /* Point
1016f 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74  er to vfs object
10170 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20   */.  int h,    
10171 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10172 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72   Open file descr
10173 69 70 74 6f 72 20 6f 66 20 66 69 6c 65 20 62 65  iptor of file be
10174 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20  ing opened */.  
10175 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49  sqlite3_file *pI
10176 64 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65  d,      /* Write
10177 20 74 6f 20 74 68 65 20 75 6e 69 78 46 69 6c 65   to the unixFile
10178 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20   structure here 
10179 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1017a 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20  *zFilename,  /* 
1017b 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65  Name of the file
1017c 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f   being opened */
1017d 0a 20 20 69 6e 74 20 63 74 72 6c 46 6c 61 67 73  .  int ctrlFlags
1017e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 5a 65             /* Ze
1017f 72 6f 20 6f 72 20 6d 6f 72 65 20 55 4e 49 58 46  ro or more UNIXF
10180 49 4c 45 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a  ILE_* values */.
10181 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  ){.  const sqlit
10182 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70  e3_io_methods *p
10183 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20  LockingStyle;.  
10184 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 3d  unixFile *pNew =
10185 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 70 49 64   (unixFile *)pId
10186 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
10187 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72  ITE_OK;..  asser
10188 74 28 20 70 4e 65 77 2d 3e 70 49 6e 6f 64 65 3d  t( pNew->pInode=
10189 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 20 2f 2a 20 55  =NULL );..  /* U
1018a 73 75 61 6c 6c 79 20 74 68 65 20 70 61 74 68 20  sually the path 
1018b 7a 46 69 6c 65 6e 61 6d 65 20 73 68 6f 75 6c 64  zFilename should
1018c 20 6e 6f 74 20 62 65 20 61 20 72 65 6c 61 74 69   not be a relati
1018d 76 65 20 70 61 74 68 6e 61 6d 65 2e 20 54 68 65  ve pathname. The
1018e 0a 20 20 2a 2a 20 65 78 63 65 70 74 69 6f 6e 20  .  ** exception 
1018f 69 73 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20  is when opening 
10190 74 68 65 20 70 72 6f 78 79 20 22 63 6f 6e 63 68  the proxy "conch
10191 22 20 66 69 6c 65 20 69 6e 20 62 75 69 6c 64 73  " file in builds
10192 20 74 68 61 74 0a 20 20 2a 2a 20 69 6e 63 6c 75   that.  ** inclu
10193 64 65 20 74 68 65 20 73 70 65 63 69 61 6c 20 41  de the special A
10194 70 70 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79  pple locking sty
10195 6c 65 73 2e 0a 20 20 2a 2f 0a 23 69 66 20 64 65  les..  */.#if de
10196 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
10197 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
10198 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
10199 20 20 61 73 73 65 72 74 28 20 7a 46 69 6c 65 6e    assert( zFilen
1019a 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e  ame==0 || zFilen
1019b 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 0a 20 20 20  ame[0]=='/' .   
1019c 20 7c 7c 20 70 56 66 73 2d 3e 70 41 70 70 44 61   || pVfs->pAppDa
1019d 74 61 3d 3d 28 76 6f 69 64 2a 29 26 61 75 74 6f  ta==(void*)&auto
1019e 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 3b 0a  lockIoFinder );.
1019f 23 65 6c 73 65 0a 20 20 61 73 73 65 72 74 28 20  #else.  assert( 
101a0 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20  zFilename==0 || 
101a1 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 27 2f  zFilename[0]=='/
101a2 27 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  ' );.#endif..  /
101a3 2a 20 4e 6f 20 6c 6f 63 6b 69 6e 67 20 6f 63 63  * No locking occ
101a4 75 72 73 20 69 6e 20 74 65 6d 70 6f 72 61 72 79  urs in temporary
101a5 20 66 69 6c 65 73 20 2a 2f 0a 20 20 61 73 73 65   files */.  asse
101a6 72 74 28 20 7a 46 69 6c 65 6e 61 6d 65 21 3d 30  rt( zFilename!=0
101a7 20 7c 7c 20 28 63 74 72 6c 46 6c 61 67 73 20 26   || (ctrlFlags &
101a8 20 55 4e 49 58 46 49 4c 45 5f 4e 4f 4c 4f 43 4b   UNIXFILE_NOLOCK
101a9 29 21 3d 30 20 29 3b 0a 0a 20 20 4f 53 54 52 41  )!=0 );..  OSTRA
101aa 43 45 28 28 22 4f 50 45 4e 20 20 20 20 25 2d 33  CE(("OPEN    %-3
101ab 64 20 25 73 5c 6e 22 2c 20 68 2c 20 7a 46 69 6c  d %s\n", h, zFil
101ac 65 6e 61 6d 65 29 29 3b 0a 20 20 70 4e 65 77 2d  ename));.  pNew-
101ad 3e 68 20 3d 20 68 3b 0a 20 20 70 4e 65 77 2d 3e  >h = h;.  pNew->
101ae 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 70  pVfs = pVfs;.  p
101af 4e 65 77 2d 3e 7a 50 61 74 68 20 3d 20 7a 46 69  New->zPath = zFi
101b0 6c 65 6e 61 6d 65 3b 0a 20 20 70 4e 65 77 2d 3e  lename;.  pNew->
101b1 63 74 72 6c 46 6c 61 67 73 20 3d 20 28 75 38 29  ctrlFlags = (u8)
101b2 63 74 72 6c 46 6c 61 67 73 3b 0a 20 20 69 66 28  ctrlFlags;.  if(
101b3 20 73 71 6c 69 74 65 33 5f 75 72 69 5f 62 6f 6f   sqlite3_uri_boo
101b4 6c 65 61 6e 28 28 28 63 74 72 6c 46 6c 61 67 73  lean(((ctrlFlags
101b5 20 26 20 55 4e 49 58 46 49 4c 45 5f 55 52 49 29   & UNIXFILE_URI)
101b6 20 3f 20 7a 46 69 6c 65 6e 61 6d 65 20 3a 20 30   ? zFilename : 0
101b7 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
101b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 70                "p
101b9 73 6f 77 22 2c 20 53 51 4c 49 54 45 5f 50 4f 57  sow", SQLITE_POW
101ba 45 52 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45  ERSAFE_OVERWRITE
101bb 29 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 63  ) ){.    pNew->c
101bc 74 72 6c 46 6c 61 67 73 20 7c 3d 20 55 4e 49 58  trlFlags |= UNIX
101bd 46 49 4c 45 5f 50 53 4f 57 3b 0a 20 20 7d 0a 20  FILE_PSOW;.  }. 
101be 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 56 66 73   if( memcmp(pVfs
101bf 2d 3e 7a 4e 61 6d 65 2c 22 75 6e 69 78 2d 65 78  ->zName,"unix-ex
101c0 63 6c 22 2c 31 30 29 3d 3d 30 20 29 7b 0a 20 20  cl",10)==0 ){.  
101c1 20 20 70 4e 65 77 2d 3e 63 74 72 6c 46 6c 61 67    pNew->ctrlFlag
101c2 73 20 7c 3d 20 55 4e 49 58 46 49 4c 45 5f 45 58  s |= UNIXFILE_EX
101c3 43 4c 3b 0a 20 20 7d 0a 0a 23 69 66 20 4f 53 5f  CL;.  }..#if OS_
101c4 56 58 57 4f 52 4b 53 0a 20 20 70 4e 65 77 2d 3e  VXWORKS.  pNew->
101c5 70 49 64 20 3d 20 76 78 77 6f 72 6b 73 46 69 6e  pId = vxworksFin
101c6 64 46 69 6c 65 49 64 28 7a 46 69 6c 65 6e 61 6d  dFileId(zFilenam
101c7 65 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e  e);.  if( pNew->
101c8 70 49 64 3d 3d 30 20 29 7b 0a 20 20 20 20 63 74  pId==0 ){.    ct
101c9 72 6c 46 6c 61 67 73 20 7c 3d 20 55 4e 49 58 46  rlFlags |= UNIXF
101ca 49 4c 45 5f 4e 4f 4c 4f 43 4b 3b 0a 20 20 20 20  ILE_NOLOCK;.    
101cb 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
101cc 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  M;.  }.#endif.. 
101cd 20 69 66 28 20 63 74 72 6c 46 6c 61 67 73 20 26   if( ctrlFlags &
101ce 20 55 4e 49 58 46 49 4c 45 5f 4e 4f 4c 4f 43 4b   UNIXFILE_NOLOCK
101cf 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 69 6e 67   ){.    pLocking
101d0 53 74 79 6c 65 20 3d 20 26 6e 6f 6c 6f 63 6b 49  Style = &nolockI
101d1 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73  oMethods;.  }els
101d2 65 7b 0a 20 20 20 20 70 4c 6f 63 6b 69 6e 67 53  e{.    pLockingS
101d3 74 79 6c 65 20 3d 20 28 2a 2a 28 66 69 6e 64 65  tyle = (**(finde
101d4 72 5f 74 79 70 65 2a 29 70 56 66 73 2d 3e 70 41  r_type*)pVfs->pA
101d5 70 70 44 61 74 61 29 28 7a 46 69 6c 65 6e 61 6d  ppData)(zFilenam
101d6 65 2c 20 70 4e 65 77 29 3b 0a 23 69 66 20 53 51  e, pNew);.#if SQ
101d7 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
101d8 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 2f 2a  ING_STYLE.    /*
101d9 20 43 61 63 68 65 20 7a 46 69 6c 65 6e 61 6d 65   Cache zFilename
101da 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   in the locking 
101db 63 6f 6e 74 65 78 74 20 28 41 46 50 20 61 6e 64  context (AFP and
101dc 20 64 6f 74 6c 6f 63 6b 20 6f 76 65 72 72 69 64   dotlock overrid
101dd 65 29 20 66 6f 72 0a 20 20 20 20 2a 2a 20 70 72  e) for.    ** pr
101de 6f 78 79 4c 6f 63 6b 20 61 63 74 69 76 61 74 69  oxyLock activati
101df 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 20 28  on is possible (
101e0 72 65 6d 6f 74 65 20 70 72 6f 78 79 20 69 73 20  remote proxy is 
101e1 62 61 73 65 64 20 6f 6e 20 64 62 20 6e 61 6d 65  based on db name
101e2 29 0a 20 20 20 20 2a 2a 20 7a 46 69 6c 65 6e 61  ).    ** zFilena
101e3 6d 65 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64  me remains valid
101e4 20 75 6e 74 69 6c 20 66 69 6c 65 20 69 73 20 63   until file is c
101e5 6c 6f 73 65 64 2c 20 74 6f 20 73 75 70 70 6f 72  losed, to suppor
101e6 74 20 2a 2f 0a 20 20 20 20 70 4e 65 77 2d 3e 6c  t */.    pNew->l
101e7 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20  ockingContext = 
101e8 28 76 6f 69 64 2a 29 7a 46 69 6c 65 6e 61 6d 65  (void*)zFilename
101e9 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20  ;.#endif.  }..  
101ea 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c  if( pLockingStyl
101eb 65 20 3d 3d 20 26 70 6f 73 69 78 49 6f 4d 65 74  e == &posixIoMet
101ec 68 6f 64 73 0a 23 69 66 20 64 65 66 69 6e 65 64  hods.#if defined
101ed 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53  (__APPLE__) && S
101ee 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
101ef 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 7c  KING_STYLE.    |
101f0 7c 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20  | pLockingStyle 
101f1 3d 3d 20 26 6e 66 73 49 6f 4d 65 74 68 6f 64 73  == &nfsIoMethods
101f2 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20  .#endif.  ){.   
101f3 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
101f4 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64  );.    rc = find
101f5 49 6e 6f 64 65 49 6e 66 6f 28 70 4e 65 77 2c 20  InodeInfo(pNew, 
101f6 26 70 4e 65 77 2d 3e 70 49 6e 6f 64 65 29 3b 0a  &pNew->pInode);.
101f7 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
101f8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f  TE_OK ){.      /
101f9 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
101fa 63 75 72 65 64 20 69 6e 20 66 69 6e 64 49 6e 6f  cured in findIno
101fb 64 65 49 6e 66 6f 28 29 2c 20 63 6c 6f 73 65 20  deInfo(), close 
101fc 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
101fd 74 6f 72 0a 20 20 20 20 20 20 2a 2a 20 69 6d 6d  tor.      ** imm
101fe 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f 72 65  ediately, before
101ff 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65 20 6d   releasing the m
10200 75 74 65 78 2e 20 66 69 6e 64 49 6e 6f 64 65 49  utex. findInodeI
10201 6e 66 6f 28 29 20 6d 61 79 20 66 61 69 6c 0a 20  nfo() may fail. 
10202 20 20 20 20 20 2a 2a 20 69 6e 20 74 77 6f 20 73       ** in two s
10203 63 65 6e 61 72 69 6f 73 3a 0a 20 20 20 20 20 20  cenarios:.      
10204 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 28 61  **.      **   (a
10205 29 20 41 20 63 61 6c 6c 20 74 6f 20 66 73 74 61  ) A call to fsta
10206 74 28 29 20 66 61 69 6c 65 64 2e 0a 20 20 20 20  t() failed..    
10207 20 20 2a 2a 20 20 20 28 62 29 20 41 20 6d 61 6c    **   (b) A mal
10208 6c 6f 63 20 66 61 69 6c 65 64 2e 0a 20 20 20 20  loc failed..    
10209 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 63    **.      ** Sc
1020a 65 6e 61 72 69 6f 20 28 62 29 20 6d 61 79 20 6f  enario (b) may o
1020b 6e 6c 79 20 6f 63 63 75 72 20 69 66 20 74 68 65  nly occur if the
1020c 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64   process is hold
1020d 69 6e 67 20 6e 6f 20 6f 74 68 65 72 0a 20 20 20  ing no other.   
1020e 20 20 20 2a 2a 20 66 69 6c 65 20 64 65 73 63 72     ** file descr
1020f 69 70 74 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74  iptors open on t
10210 68 65 20 73 61 6d 65 20 66 69 6c 65 2e 20 49 66  he same file. If
10211 20 74 68 65 72 65 20 77 65 72 65 20 6f 74 68 65   there were othe
10212 72 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20  r file.      ** 
10213 64 65 73 63 72 69 70 74 6f 72 73 20 6f 6e 20 74  descriptors on t
10214 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20 6e  his file, then n
10215 6f 20 6d 61 6c 6c 6f 63 20 77 6f 75 6c 64 20 62  o malloc would b
10216 65 20 72 65 71 75 69 72 65 64 20 62 79 0a 20 20  e required by.  
10217 20 20 20 20 2a 2a 20 66 69 6e 64 49 6e 6f 64 65      ** findInode
10218 49 6e 66 6f 28 29 2e 20 49 66 20 74 68 69 73 20  Info(). If this 
10219 69 73 20 74 68 65 20 63 61 73 65 2c 20 69 74 20  is the case, it 
1021a 69 73 20 71 75 69 74 65 20 73 61 66 65 20 74 6f  is quite safe to
1021b 20 63 6c 6f 73 65 0a 20 20 20 20 20 20 2a 2a 20   close.      ** 
1021c 68 61 6e 64 6c 65 20 68 20 2d 20 61 73 20 69 74  handle h - as it
1021d 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
1021e 68 61 74 20 6e 6f 20 70 6f 73 69 78 20 6c 6f 63  hat no posix loc
1021f 6b 73 20 77 69 6c 6c 20 62 65 20 72 65 6c 65 61  ks will be relea
10220 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20  sed.      ** by 
10221 64 6f 69 6e 67 20 73 6f 2e 0a 20 20 20 20 20 20  doing so..      
10222 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 73  **.      ** If s
10223 63 65 6e 61 72 69 6f 20 28 61 29 20 63 61 75 73  cenario (a) caus
10224 65 64 20 74 68 65 20 65 72 72 6f 72 20 74 68 65  ed the error the
10225 6e 20 74 68 69 6e 67 73 20 61 72 65 20 6e 6f 74  n things are not
10226 20 73 6f 20 73 61 66 65 2e 20 54 68 65 0a 20 20   so safe. The.  
10227 20 20 20 20 2a 2a 20 69 6d 70 6c 69 63 69 74 20      ** implicit 
10228 61 73 73 75 6d 70 74 69 6f 6e 20 68 65 72 65 20  assumption here 
10229 69 73 20 74 68 61 74 20 69 66 20 66 73 74 61 74  is that if fstat
1022a 28 29 20 66 61 69 6c 73 2c 20 74 68 69 6e 67 73  () fails, things
1022b 20 61 72 65 20 69 6e 0a 20 20 20 20 20 20 2a 2a   are in.      **
1022c 20 73 75 63 68 20 62 61 64 20 73 68 61 70 65 20   such bad shape 
1022d 74 68 61 74 20 64 72 6f 70 70 69 6e 67 20 61 20  that dropping a 
1022e 6c 6f 63 6b 20 6f 72 20 74 77 6f 20 64 6f 65 73  lock or two does
1022f 6e 27 74 20 6d 61 74 74 65 72 20 6d 75 63 68 2e  n't matter much.
10230 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
10231 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 4e 65  robust_close(pNe
10232 77 2c 20 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b  w, h, __LINE__);
10233 0a 20 20 20 20 20 20 68 20 3d 20 2d 31 3b 0a 20  .      h = -1;. 
10234 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65 61     }.    unixLea
10235 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 0a  veMutex();.  }..
10236 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  #if SQLITE_ENABL
10237 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
10238 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  && defined(__APP
10239 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69 66 28  LE__).  else if(
1023a 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d   pLockingStyle =
1023b 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20  = &afpIoMethods 
1023c 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20 6c 6f  ){.    /* AFP lo
1023d 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 66  cking uses the f
1023e 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e  ile path so it n
1023f 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 6c 75  eeds to be inclu
10240 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68  ded in.    ** th
10241 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74  e afpLockingCont
10242 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ext..    */.    
10243 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
10244 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70 4e 65  t *pCtx;.    pNe
10245 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  w->lockingContex
10246 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c 69 74  t = pCtx = sqlit
10247 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f  e3_malloc( sizeo
10248 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 20 20  f(*pCtx) );.    
10249 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b 0a 20  if( pCtx==0 ){. 
1024a 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1024b 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73  _NOMEM;.    }els
1024c 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42 3a 20  e{.      /* NB: 
1024d 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73 74 73  zFilename exists
1024e 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76 61 6c   and remains val
1024f 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66 69 6c  id until the fil
10250 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20 20 20  e is closed.    
10251 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 74    ** according t
10252 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 46 31  o requirement F1
10253 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 6f 20  1141.  So we do 
10254 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65  not need to make
10255 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 79   a.      ** copy
10256 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65   of the filename
10257 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74 78 2d  . */.      pCtx-
10258 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c 65 6e  >dbPath = zFilen
10259 61 6d 65 3b 0a 20 20 20 20 20 20 70 43 74 78 2d  ame;.      pCtx-
1025a 3e 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20  >reserved = 0;. 
1025b 20 20 20 20 20 73 72 61 6e 64 6f 6d 64 65 76 28       srandomdev(
1025c 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 45 6e 74  );.      unixEnt
1025d 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20  erMutex();.     
1025e 20 72 63 20 3d 20 66 69 6e 64 49 6e 6f 64 65 49   rc = findInodeI
1025f 6e 66 6f 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d  nfo(pNew, &pNew-
10260 3e 70 49 6e 6f 64 65 29 3b 0a 20 20 20 20 20 20  >pInode);.      
10261 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
10262 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  K ){.        sql
10263 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 2d 3e  ite3_free(pNew->
10264 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b  lockingContext);
10265 0a 20 20 20 20 20 20 20 20 72 6f 62 75 73 74 5f  .        robust_
10266 63 6c 6f 73 65 28 70 4e 65 77 2c 20 68 2c 20 5f  close(pNew, h, _
10267 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 20 20 20 20 20  _LINE__);.      
10268 20 20 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20    h = -1;.      
10269 7d 0a 20 20 20 20 20 20 75 6e 69 78 4c 65 61 76  }.      unixLeav
1026a 65 4d 75 74 65 78 28 29 3b 20 20 20 20 20 20 20  eMutex();       
1026b 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64   .    }.  }.#end
1026c 69 66 0a 0a 20 20 65 6c 73 65 20 69 66 28 20 70  if..  else if( p
1026d 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20  LockingStyle == 
1026e 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64  &dotlockIoMethod
1026f 73 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 74 66  s ){.    /* Dotf
10270 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73  ile locking uses
10271 20 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 73   the file path s
10272 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65  o it needs to be
10273 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 20 20 20   included in.   
10274 20 2a 2a 20 74 68 65 20 64 6f 74 6c 6f 63 6b 4c   ** the dotlockL
10275 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 0a 20  ockingContext . 
10276 20 20 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a     */.    char *
10277 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 20 20 69  zLockFile;.    i
10278 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 3b 0a 20 20  nt nFilename;.  
10279 20 20 61 73 73 65 72 74 28 20 7a 46 69 6c 65 6e    assert( zFilen
1027a 61 6d 65 21 3d 30 20 29 3b 0a 20 20 20 20 6e 46  ame!=0 );.    nF
1027b 69 6c 65 6e 61 6d 65 20 3d 20 28 69 6e 74 29 73  ilename = (int)s
1027c 74 72 6c 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 29  trlen(zFilename)
1027d 20 2b 20 36 3b 0a 20 20 20 20 7a 4c 6f 63 6b 46   + 6;.    zLockF
1027e 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 73 71  ile = (char *)sq
1027f 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 46 69  lite3_malloc(nFi
10280 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28  lename);.    if(
10281 20 7a 4c 6f 63 6b 46 69 6c 65 3d 3d 30 20 29 7b   zLockFile==0 ){
10282 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
10283 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65  TE_NOMEM;.    }e
10284 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
10285 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 46 69 6c  e3_snprintf(nFil
10286 65 6e 61 6d 65 2c 20 7a 4c 6f 63 6b 46 69 6c 65  ename, zLockFile
10287 2c 20 22 25 73 22 20 44 4f 54 4c 4f 43 4b 5f 53  , "%s" DOTLOCK_S
10288 55 46 46 49 58 2c 20 7a 46 69 6c 65 6e 61 6d 65  UFFIX, zFilename
10289 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65  );.    }.    pNe
1028a 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  w->lockingContex
1028b 74 20 3d 20 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20  t = zLockFile;. 
1028c 20 7d 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52   }..#if OS_VXWOR
1028d 4b 53 0a 20 20 65 6c 73 65 20 69 66 28 20 70 4c  KS.  else if( pL
1028e 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26  ockingStyle == &
1028f 73 65 6d 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a  semIoMethods ){.
10290 20 20 20 20 2f 2a 20 4e 61 6d 65 64 20 73 65 6d      /* Named sem
10291 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 75  aphore locking u
10292 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 74  ses the file pat
10293 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f  h so it needs to
10294 20 62 65 0a 20 20 20 20 2a 2a 20 69 6e 63 6c 75   be.    ** inclu
10295 64 65 64 20 69 6e 20 74 68 65 20 73 65 6d 4c 6f  ded in the semLo
10296 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 20 20 20  ckingContext.   
10297 20 2a 2f 0a 20 20 20 20 75 6e 69 78 45 6e 74 65   */.    unixEnte
10298 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 63  rMutex();.    rc
10299 20 3d 20 66 69 6e 64 49 6e 6f 64 65 49 6e 66 6f   = findInodeInfo
1029a 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d 3e 70 49  (pNew, &pNew->pI
1029b 6e 6f 64 65 29 3b 0a 20 20 20 20 69 66 28 20 28  node);.    if( (
1029c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 26  rc==SQLITE_OK) &
1029d 26 20 28 70 4e 65 77 2d 3e 70 49 6e 6f 64 65 2d  & (pNew->pInode-
1029e 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20 29 7b 0a  >pSem==NULL) ){.
1029f 20 20 20 20 20 20 63 68 61 72 20 2a 7a 53 65 6d        char *zSem
102a0 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e 70 49 6e  Name = pNew->pIn
102a1 6f 64 65 2d 3e 61 53 65 6d 4e 61 6d 65 3b 0a 20  ode->aSemName;. 
102a2 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20       int n;.    
102a3 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
102a4 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c  tf(MAX_PATHNAME,
102a5 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 2f 25 73 2e   zSemName, "/%s.
102a6 73 65 6d 22 2c 0a 20 20 20 20 20 20 20 20 20 20  sem",.          
102a7 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 65               pNe
102a8 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 63  w->pId->zCanonic
102a9 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 66  alName);.      f
102aa 6f 72 28 20 6e 3d 31 3b 20 7a 53 65 6d 4e 61 6d  or( n=1; zSemNam
102ab 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20 20 20  e[n]; n++ ).    
102ac 20 20 20 20 69 66 28 20 7a 53 65 6d 4e 61 6d 65      if( zSemName
102ad 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 6d 4e  [n]=='/' ) zSemN
102ae 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a 20 20  ame[n] = '_';.  
102af 20 20 20 20 70 4e 65 77 2d 3e 70 49 6e 6f 64 65      pNew->pInode
102b0 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 65  ->pSem = sem_ope
102b1 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 52  n(zSemName, O_CR
102b2 45 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a 20  EAT, 0666, 1);. 
102b3 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e 70       if( pNew->p
102b4 49 6e 6f 64 65 2d 3e 70 53 65 6d 20 3d 3d 20 53  Inode->pSem == S
102b5 45 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20  EM_FAILED ){.   
102b6 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
102b7 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
102b8 70 4e 65 77 2d 3e 70 49 6e 6f 64 65 2d 3e 61 53  pNew->pInode->aS
102b9 65 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27  emName[0] = '\0'
102ba 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
102bb 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74      unixLeaveMut
102bc 65 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  ex();.  }.#endif
102bd 0a 20 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74  .  .  pNew->last
102be 45 72 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20 4f  Errno = 0;.#if O
102bf 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20  S_VXWORKS.  if( 
102c0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
102c1 0a 20 20 20 20 69 66 28 20 68 3e 3d 30 20 29 20  .    if( h>=0 ) 
102c2 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28 70 4e 65  robust_close(pNe
102c3 77 2c 20 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b  w, h, __LINE__);
102c4 0a 20 20 20 20 68 20 3d 20 2d 31 3b 0a 20 20 20  .    h = -1;.   
102c5 20 6f 73 55 6e 6c 69 6e 6b 28 7a 46 69 6c 65 6e   osUnlink(zFilen
102c6 61 6d 65 29 3b 0a 20 20 20 20 69 73 44 65 6c 65  ame);.    isDele
102c7 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  te = 0;.  }.  if
102c8 28 20 69 73 44 65 6c 65 74 65 20 29 20 70 4e 65  ( isDelete ) pNe
102c9 77 2d 3e 63 74 72 6c 46 6c 61 67 73 20 7c 3d 20  w->ctrlFlags |= 
102ca 55 4e 49 58 46 49 4c 45 5f 44 45 4c 45 54 45 3b  UNIXFILE_DELETE;
102cb 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 72 63  .#endif.  if( rc
102cc 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
102cd 20 20 20 69 66 28 20 68 3e 3d 30 20 29 20 72 6f     if( h>=0 ) ro
102ce 62 75 73 74 5f 63 6c 6f 73 65 28 70 4e 65 77 2c  bust_close(pNew,
102cf 20 68 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a 20   h, __LINE__);. 
102d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77   }else{.    pNew
102d1 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f 63  ->pMethod = pLoc
102d2 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20 4f  kingStyle;.    O
102d3 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a  penCounter(+1);.
102d4 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
102d5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
102d6 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64   the name of a d
102d7 69 72 65 63 74 6f 72 79 20 69 6e 20 77 68 69 63  irectory in whic
102d8 68 20 74 6f 20 70 75 74 20 74 65 6d 70 6f 72 61  h to put tempora
102d9 72 79 20 66 69 6c 65 73 2e 0a 2a 2a 20 49 66 20  ry files..** If 
102da 6e 6f 20 73 75 69 74 61 62 6c 65 20 74 65 6d 70  no suitable temp
102db 6f 72 61 72 79 20 66 69 6c 65 20 64 69 72 65 63  orary file direc
102dc 74 6f 72 79 20 63 61 6e 20 62 65 20 66 6f 75 6e  tory can be foun
102dd 64 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a  d, return NULL..
102de 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
102df 63 68 61 72 20 2a 75 6e 69 78 54 65 6d 70 46 69  char *unixTempFi
102e0 6c 65 44 69 72 28 76 6f 69 64 29 7b 0a 20 20 73  leDir(void){.  s
102e1 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
102e2 20 2a 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a 20   *azDirs[] = {. 
102e3 20 20 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a 20      0,.     0,. 
102e4 20 20 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c 0a      "/var/tmp",.
102e5 20 20 20 20 20 22 2f 75 73 72 2f 74 6d 70 22 2c       "/usr/tmp",
102e6 0a 20 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20 20  .     "/tmp",.  
102e7 20 20 20 30 20 20 20 20 20 20 20 20 2f 2a 20 4c     0        /* L
102e8 69 73 74 20 74 65 72 6d 69 6e 61 74 6f 72 20 2a  ist terminator *
102e9 2f 0a 20 20 7d 3b 0a 20 20 75 6e 73 69 67 6e 65  /.  };.  unsigne
102ea 64 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63  d int i;.  struc
102eb 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 63 6f  t stat buf;.  co
102ec 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 72 20 3d  nst char *zDir =
102ed 20 30 3b 0a 0a 20 20 61 7a 44 69 72 73 5b 30 5d   0;..  azDirs[0]
102ee 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f   = sqlite3_temp_
102ef 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 69 66 28  directory;.  if(
102f0 20 21 61 7a 44 69 72 73 5b 31 5d 20 29 20 61 7a   !azDirs[1] ) az
102f1 44 69 72 73 5b 31 5d 20 3d 20 67 65 74 65 6e 76  Dirs[1] = getenv
102f2 28 22 54 4d 50 44 49 52 22 29 3b 0a 20 20 66 6f  ("TMPDIR");.  fo
102f3 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28  r(i=0; i<sizeof(
102f4 61 7a 44 69 72 73 29 2f 73 69 7a 65 6f 66 28 61  azDirs)/sizeof(a
102f5 7a 44 69 72 73 5b 30 5d 29 3b 20 7a 44 69 72 3d  zDirs[0]); zDir=
102f6 61 7a 44 69 72 73 5b 69 2b 2b 5d 29 7b 0a 20 20  azDirs[i++]){.  
102f7 20 20 69 66 28 20 7a 44 69 72 3d 3d 30 20 29 20    if( zDir==0 ) 
102f8 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66  continue;.    if
102f9 28 20 6f 73 53 74 61 74 28 7a 44 69 72 2c 20 26  ( osStat(zDir, &
102fa 62 75 66 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  buf) ) continue;
102fb 0a 20 20 20 20 69 66 28 20 21 53 5f 49 53 44 49  .    if( !S_ISDI
102fc 52 28 62 75 66 2e 73 74 5f 6d 6f 64 65 29 20 29  R(buf.st_mode) )
102fd 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69   continue;.    i
102fe 66 28 20 6f 73 41 63 63 65 73 73 28 7a 44 69 72  f( osAccess(zDir
102ff 2c 20 30 37 29 20 29 20 63 6f 6e 74 69 6e 75 65  , 07) ) continue
10300 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  ;.    break;.  }
10301 0a 20 20 72 65 74 75 72 6e 20 7a 44 69 72 3b 0a  .  return zDir;.
10302 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
10303 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
10304 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20   name in zBuf.  
10305 7a 42 75 66 20 6d 75 73 74 20 62 65 20 61 6c 6c  zBuf must be all
10306 6f 63 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65  ocated.** by the
10307 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 73 73   calling process
10308 20 61 6e 64 20 6d 75 73 74 20 62 65 20 62 69 67   and must be big
10309 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20   enough to hold 
1030a 61 74 20 6c 65 61 73 74 0a 2a 2a 20 70 56 66 73  at least.** pVfs
1030b 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 62 79 74  ->mxPathname byt
1030c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
1030d 74 20 75 6e 69 78 47 65 74 54 65 6d 70 6e 61 6d  t unixGetTempnam
1030e 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  e(int nBuf, char
1030f 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69   *zBuf){.  stati
10310 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
10311 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d   char zChars[] =
10312 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 69 6a  .    "abcdefghij
10313 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a  klmnopqrstuvwxyz
10314 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 48 49  ".    "ABCDEFGHI
10315 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59  JKLMNOPQRSTUVWXY
10316 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 36 37  Z".    "01234567
10317 38 39 22 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  89";.  unsigned 
10318 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 6f 6e 73  int i, j;.  cons
10319 74 20 63 68 61 72 20 2a 7a 44 69 72 3b 0a 0a 20  t char *zDir;.. 
1031a 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20   /* It's odd to 
1031b 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65  simulate an io-e
1031c 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72  rror here, but r
1031d 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75  eally this is ju
1031e 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68  st.  ** using th
1031f 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61  e io-error infra
10320 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 65 73  structure to tes
10321 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 68 61  t that SQLite ha
10322 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20  ndles this.  ** 
10323 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67  function failing
10324 2e 20 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61  . .  */.  Simula
10325 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
10326 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29  n SQLITE_IOERR )
10327 3b 0a 0a 20 20 7a 44 69 72 20 3d 20 75 6e 69 78  ;..  zDir = unix
10328 54 65 6d 70 46 69 6c 65 44 69 72 28 29 3b 0a 20  TempFileDir();. 
10329 20 69 66 28 20 7a 44 69 72 3d 3d 30 20 29 20 7a   if( zDir==0 ) z
1032a 44 69 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a  Dir = ".";..  /*
1032b 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20   Check that the 
1032c 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 73  output buffer is
1032d 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 66 6f   large enough fo
1032e 72 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  r the temporary 
1032f 66 69 6c 65 20 0a 20 20 2a 2a 20 6e 61 6d 65 2e  file .  ** name.
10330 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 72   If it is not, r
10331 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
10332 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  OR..  */.  if( (
10333 73 74 72 6c 65 6e 28 7a 44 69 72 29 20 2b 20 73  strlen(zDir) + s
10334 74 72 6c 65 6e 28 53 51 4c 49 54 45 5f 54 45 4d  trlen(SQLITE_TEM
10335 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 29 20 2b  P_FILE_PREFIX) +
10336 20 31 38 29 20 3e 3d 20 28 73 69 7a 65 5f 74 29   18) >= (size_t)
10337 6e 42 75 66 20 29 7b 0a 20 20 20 20 72 65 74 75  nBuf ){.    retu
10338 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
10339 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20  .  }..  do{.    
1033a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1033b 28 6e 42 75 66 2d 31 38 2c 20 7a 42 75 66 2c 20  (nBuf-18, zBuf, 
1033c 22 25 73 2f 22 53 51 4c 49 54 45 5f 54 45 4d 50  "%s/"SQLITE_TEMP
1033d 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 44  _FILE_PREFIX, zD
1033e 69 72 29 3b 0a 20 20 20 20 6a 20 3d 20 28 69 6e  ir);.    j = (in
1033f 74 29 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a  t)strlen(zBuf);.
10340 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64      sqlite3_rand
10341 6f 6d 6e 65 73 73 28 31 35 2c 20 26 7a 42 75 66  omness(15, &zBuf
10342 5b 6a 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  [j]);.    for(i=
10343 30 3b 20 69 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b  0; i<15; i++, j+
10344 2b 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 6a  +){.      zBuf[j
10345 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73  ] = (char)zChars
10346 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  [ ((unsigned cha
10347 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65  r)zBuf[j])%(size
10348 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b  of(zChars)-1) ];
10349 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b  .    }.    zBuf[
1034a 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 42 75 66  j] = 0;.    zBuf
1034b 5b 6a 2b 31 5d 20 3d 20 30 3b 0a 20 20 7d 77 68  [j+1] = 0;.  }wh
1034c 69 6c 65 28 20 6f 73 41 63 63 65 73 73 28 7a 42  ile( osAccess(zB
1034d 75 66 2c 30 29 3d 3d 30 20 29 3b 0a 20 20 72 65  uf,0)==0 );.  re
1034e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1034f 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  }..#if SQLITE_EN
10350 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
10351 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  LE && defined(__
10352 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20 52  APPLE__)./*.** R
10353 6f 75 74 69 6e 65 20 74 6f 20 74 72 61 6e 73 66  outine to transf
10354 6f 72 6d 20 61 20 75 6e 69 78 46 69 6c 65 20 69  orm a unixFile i
10355 6e 74 6f 20 61 20 70 72 6f 78 79 2d 6c 6f 63 6b  nto a proxy-lock
10356 69 6e 67 20 75 6e 69 78 46 69 6c 65 2e 0a 2a 2a  ing unixFile..**
10357 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
10358 69 6e 20 74 68 65 20 70 72 6f 78 79 2d 6c 6f 63  in the proxy-loc
10359 6b 20 64 69 76 69 73 69 6f 6e 2c 20 62 75 74 20  k division, but 
1035a 75 73 65 64 20 62 79 20 75 6e 69 78 4f 70 65 6e  used by unixOpen
1035b 28 29 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f  ().** if SQLITE_
1035c 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43  PREFER_PROXY_LOC
1035d 4b 49 4e 47 20 69 73 20 64 65 66 69 6e 65 64 2e  KING is defined.
1035e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
1035f 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69  roxyTransformUni
10360 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 2a 2c  xFile(unixFile*,
10361 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23   const char*);.#
10362 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 61  endif../*.** Sea
10363 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73 65  rch for an unuse
10364 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  d file descripto
10365 72 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e 65  r that was opene
10366 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  d on the databas
10367 65 20 0a 2a 2a 20 66 69 6c 65 20 28 6e 6f 74 20  e .** file (not 
10368 61 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 6d 61 73  a journal or mas
10369 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter-journal file
1036a 29 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  ) identified by 
1036b 70 61 74 68 6e 61 6d 65 0a 2a 2a 20 7a 50 61 74  pathname.** zPat
1036c 68 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4f 50  h with SQLITE_OP
1036d 45 4e 5f 58 58 58 20 66 6c 61 67 73 20 6d 61 74  EN_XXX flags mat
1036e 63 68 69 6e 67 20 74 68 6f 73 65 20 70 61 73 73  ching those pass
1036f 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  ed as the second
10370 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  .** argument to 
10371 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  this function..*
10372 2a 0a 2a 2a 20 53 75 63 68 20 61 20 66 69 6c 65  *.** Such a file
10373 20 64 65 73 63 72 69 70 74 6f 72 20 6d 61 79 20   descriptor may 
10374 65 78 69 73 74 20 69 66 20 61 20 64 61 74 61 62  exist if a datab
10375 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77  ase connection w
10376 61 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 62 75 74  as closed.** but
10377 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20   the associated 
10378 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
10379 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6c 6f  could not be clo
1037a 73 65 64 20 62 65 63 61 75 73 65 20 73 6f 6d 65  sed because some
1037b 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 20 64  .** other file d
1037c 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e 20 6f  escriptor open o
1037d 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20  n the same file 
1037e 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 66 69 6c  is holding a fil
1037f 65 2d 6c 6f 63 6b 2e 0a 2a 2a 20 52 65 66 65 72  e-lock..** Refer
10380 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20   to comments in 
10381 74 68 65 20 75 6e 69 78 43 6c 6f 73 65 28 29 20  the unixClose() 
10382 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 74 68 65  function and the
10383 20 6c 65 6e 67 74 68 79 20 63 6f 6d 6d 65 6e 74   lengthy comment
10384 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 20 22  .** describing "
10385 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c  Posix Advisory L
10386 6f 63 6b 69 6e 67 22 20 61 74 20 74 68 65 20 73  ocking" at the s
10387 74 61 72 74 20 6f 66 20 74 68 69 73 20 66 69 6c  tart of this fil
10388 65 20 66 6f 72 20 0a 2a 2a 20 66 75 72 74 68 65  e for .** furthe
10389 72 20 64 65 74 61 69 6c 73 2e 20 41 6c 73 6f 2c  r details. Also,
1038a 20 74 69 63 6b 65 74 20 23 34 30 31 38 2e 0a 2a   ticket #4018..*
1038b 2a 0a 2a 2a 20 49 66 20 61 20 73 75 69 74 61 62  *.** If a suitab
1038c 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  le file descript
1038d 6f 72 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65  or is found, the
1038e 6e 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64  n it is returned
1038f 2e 20 49 66 20 6e 6f 0a 2a 2a 20 73 75 63 68 20  . If no.** such 
10390 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
10391 69 73 20 6c 6f 63 61 74 65 64 2c 20 2d 31 20 69  is located, -1 i
10392 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73  s returned..*/.s
10393 74 61 74 69 63 20 55 6e 69 78 55 6e 75 73 65 64  tatic UnixUnused
10394 46 64 20 2a 66 69 6e 64 52 65 75 73 61 62 6c 65  Fd *findReusable
10395 46 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  Fd(const char *z
10396 50 61 74 68 2c 20 69 6e 74 20 66 6c 61 67 73 29  Path, int flags)
10397 7b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64  {.  UnixUnusedFd
10398 20 2a 70 55 6e 75 73 65 64 20 3d 20 30 3b 0a 0a   *pUnused = 0;..
10399 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 65 61 72    /* Do not sear
1039a 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73 65 64  ch for an unused
1039b 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
1039c 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 4e 6f 74   on vxworks. Not
1039d 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 76 78   because.  ** vx
1039e 77 6f 72 6b 73 20 77 6f 75 6c 64 20 6e 6f 74 20  works would not 
1039f 62 65 6e 65 66 69 74 20 66 72 6f 6d 20 74 68 65  benefit from the
103a0 20 63 68 61 6e 67 65 20 28 69 74 20 6d 69 67 68   change (it migh
103a1 74 2c 20 77 65 27 72 65 20 6e 6f 74 20 73 75 72  t, we're not sur
103a2 65 29 2c 0a 20 20 2a 2a 20 62 75 74 20 62 65 63  e),.  ** but bec
103a3 61 75 73 65 20 6e 6f 20 77 61 79 20 74 6f 20 74  ause no way to t
103a4 65 73 74 20 69 74 20 69 73 20 63 75 72 72 65 6e  est it is curren
103a5 74 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e 20 49  tly available. I
103a6 74 20 69 73 20 62 65 74 74 65 72 20 0a 20 20 2a  t is better .  *
103a7 2a 20 6e 6f 74 20 74 6f 20 72 69 73 6b 20 62 72  * not to risk br
103a8 65 61 6b 69 6e 67 20 76 78 77 6f 72 6b 73 20 73  eaking vxworks s
103a9 75 70 70 6f 72 74 20 66 6f 72 20 74 68 65 20 73  upport for the s
103aa 61 6b 65 20 6f 66 20 73 75 63 68 20 61 6e 20 6f  ake of such an o
103ab 62 73 63 75 72 65 20 0a 20 20 2a 2a 20 66 65 61  bscure .  ** fea
103ac 74 75 72 65 2e 20 20 2a 2f 0a 23 69 66 20 21 4f  ture.  */.#if !O
103ad 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75  S_VXWORKS.  stru
103ae 63 74 20 73 74 61 74 20 73 53 74 61 74 3b 20 20  ct stat sStat;  
103af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
103b0 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20 73   /* Results of s
103b1 74 61 74 28 29 20 63 61 6c 6c 20 2a 2f 0a 0a 20  tat() call */.. 
103b2 20 2f 2a 20 41 20 73 74 61 74 28 29 20 63 61 6c   /* A stat() cal
103b3 6c 20 6d 61 79 20 66 61 69 6c 20 66 6f 72 20 76  l may fail for v
103b4 61 72 69 6f 75 73 20 72 65 61 73 6f 6e 73 2e 20  arious reasons. 
103b5 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c  If this happens,
103b6 20 69 74 20 69 73 0a 20 20 2a 2a 20 61 6c 6d 6f   it is.  ** almo
103b7 73 74 20 63 65 72 74 61 69 6e 20 74 68 61 74 20  st certain that 
103b8 61 6e 20 6f 70 65 6e 28 29 20 63 61 6c 6c 20 6f  an open() call o
103b9 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74 68 20  n the same path 
103ba 77 69 6c 6c 20 61 6c 73 6f 20 66 61 69 6c 2e 0a  will also fail..
103bb 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 65    ** For this re
103bc 61 73 6f 6e 2c 20 69 66 20 61 6e 20 65 72 72 6f  ason, if an erro
103bd 72 20 6f 63 63 75 72 73 20 69 6e 20 74 68 65 20  r occurs in the 
103be 73 74 61 74 28 29 20 63 61 6c 6c 20 68 65 72 65  stat() call here
103bf 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 69 67 6e  , it is.  ** ign
103c0 6f 72 65 64 20 61 6e 64 20 2d 31 20 69 73 20 72  ored and -1 is r
103c1 65 74 75 72 6e 65 64 2e 20 54 68 65 20 63 61 6c  eturned. The cal
103c2 6c 65 72 20 77 69 6c 6c 20 74 72 79 20 74 6f 20  ler will try to 
103c3 6f 70 65 6e 20 61 20 6e 65 77 20 66 69 6c 65 0a  open a new file.
103c4 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20    ** descriptor 
103c5 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74 68  on the same path
103c6 2c 20 66 61 69 6c 2c 20 61 6e 64 20 72 65 74 75  , fail, and retu
103c7 72 6e 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 53  rn an error to S
103c8 51 4c 69 74 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  QLite..  **.  **
103c9 20 45 76 65 6e 20 69 66 20 61 20 73 75 62 73 65   Even if a subse
103ca 71 75 65 6e 74 20 6f 70 65 6e 28 29 20 63 61 6c  quent open() cal
103cb 6c 20 64 6f 65 73 20 73 75 63 63 65 65 64 2c 20  l does succeed, 
103cc 74 68 65 20 63 6f 6e 73 65 71 75 65 6e 63 65 73  the consequences
103cd 20 6f 66 0a 20 20 2a 2a 20 6e 6f 74 20 73 65 61   of.  ** not sea
103ce 72 63 68 69 6e 67 20 66 6f 72 20 61 20 72 65 73  rching for a res
103cf 75 73 61 62 6c 65 20 66 69 6c 65 20 64 65 73 63  usable file desc
103d0 72 69 70 74 6f 72 20 61 72 65 20 6e 6f 74 20 64  riptor are not d
103d1 69 72 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 30  ire.  */.  if( 0
103d2 3d 3d 6f 73 53 74 61 74 28 7a 50 61 74 68 2c 20  ==osStat(zPath, 
103d3 26 73 53 74 61 74 29 20 29 7b 0a 20 20 20 20 75  &sStat) ){.    u
103d4 6e 69 78 49 6e 6f 64 65 49 6e 66 6f 20 2a 70 49  nixInodeInfo *pI
103d5 6e 6f 64 65 3b 0a 0a 20 20 20 20 75 6e 69 78 45  node;..    unixE
103d6 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
103d7 20 70 49 6e 6f 64 65 20 3d 20 69 6e 6f 64 65 4c   pInode = inodeL
103d8 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  ist;.    while( 
103d9 70 49 6e 6f 64 65 20 26 26 20 28 70 49 6e 6f 64  pInode && (pInod
103da 65 2d 3e 66 69 6c 65 49 64 2e 64 65 76 21 3d 73  e->fileId.dev!=s
103db 53 74 61 74 2e 73 74 5f 64 65 76 0a 20 20 20 20  Stat.st_dev.    
103dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
103dd 20 7c 7c 20 70 49 6e 6f 64 65 2d 3e 66 69 6c 65   || pInode->file
103de 49 64 2e 69 6e 6f 21 3d 73 53 74 61 74 2e 73 74  Id.ino!=sStat.st
103df 5f 69 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 20  _ino) ){.       
103e0 70 49 6e 6f 64 65 20 3d 20 70 49 6e 6f 64 65 2d  pInode = pInode-
103e1 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  >pNext;.    }.  
103e2 20 20 69 66 28 20 70 49 6e 6f 64 65 20 29 7b 0a    if( pInode ){.
103e3 20 20 20 20 20 20 55 6e 69 78 55 6e 75 73 65 64        UnixUnused
103e4 46 64 20 2a 2a 70 70 3b 0a 20 20 20 20 20 20 66  Fd **pp;.      f
103e5 6f 72 28 70 70 3d 26 70 49 6e 6f 64 65 2d 3e 70  or(pp=&pInode->p
103e6 55 6e 75 73 65 64 3b 20 2a 70 70 20 26 26 20 28  Unused; *pp && (
103e7 2a 70 70 29 2d 3e 66 6c 61 67 73 21 3d 66 6c 61  *pp)->flags!=fla
103e8 67 73 3b 20 70 70 3d 26 28 28 2a 70 70 29 2d 3e  gs; pp=&((*pp)->
103e9 70 4e 65 78 74 29 29 3b 0a 20 20 20 20 20 20 70  pNext));.      p
103ea 55 6e 75 73 65 64 20 3d 20 2a 70 70 3b 0a 20 20  Unused = *pp;.  
103eb 20 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 20      if( pUnused 
103ec 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 70 20 3d  ){.        *pp =
103ed 20 70 55 6e 75 73 65 64 2d 3e 70 4e 65 78 74 3b   pUnused->pNext;
103ee 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
103ef 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65     unixLeaveMute
103f0 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  x();.  }.#endif 
103f1 20 20 20 2f 2a 20 69 66 20 21 4f 53 5f 56 58 57     /* if !OS_VXW
103f2 4f 52 4b 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e  ORKS */.  return
103f3 20 70 55 6e 75 73 65 64 3b 0a 7d 0a 0a 2f 2a 0a   pUnused;.}../*.
103f4 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
103f5 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 75 6e   is called by un
103f6 69 78 4f 70 65 6e 28 29 20 74 6f 20 64 65 74 65  ixOpen() to dete
103f7 72 6d 69 6e 65 20 74 68 65 20 75 6e 69 78 20 70  rmine the unix p
103f8 65 72 6d 69 73 73 69 6f 6e 73 0a 2a 2a 20 74 6f  ermissions.** to
103f9 20 63 72 65 61 74 65 20 6e 65 77 20 66 69 6c 65   create new file
103fa 73 20 77 69 74 68 2e 20 49 66 20 6e 6f 20 65 72  s with. If no er
103fb 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e  ror occurs, then
103fc 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
103fd 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 61 20  turned.** and a 
103fe 76 61 6c 75 65 20 73 75 69 74 61 62 6c 65 20 66  value suitable f
103ff 6f 72 20 70 61 73 73 69 6e 67 20 61 73 20 74 68  or passing as th
10400 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
10401 20 74 6f 20 6f 70 65 6e 28 32 29 20 69 73 0a 2a   to open(2) is.*
10402 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 4d  * written to *pM
10403 6f 64 65 2e 20 49 66 20 61 6e 20 49 4f 20 65 72  ode. If an IO er
10404 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 53  ror occurs, an S
10405 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
10406 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64   is .** returned
10407 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f   and the value o
10408 66 20 2a 70 4d 6f 64 65 20 69 73 20 6e 6f 74 20  f *pMode is not 
10409 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20  modified..**.** 
1040a 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 20 63 61  In most cases ca
1040b 73 65 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ses, this routin
1040c 65 20 73 65 74 73 20 2a 70 4d 6f 64 65 20 74 6f  e sets *pMode to
1040d 20 30 2c 20 77 68 69 63 68 20 77 69 6c 6c 20 62   0, which will b
1040e 65 63 6f 6d 65 0a 2a 2a 20 61 6e 20 69 6e 64 69  ecome.** an indi
1040f 63 61 74 69 6f 6e 20 74 6f 20 72 6f 62 75 73 74  cation to robust
10410 5f 6f 70 65 6e 28 29 20 74 6f 20 63 72 65 61 74  _open() to creat
10411 65 20 74 68 65 20 66 69 6c 65 20 75 73 69 6e 67  e the file using
10412 0a 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 41 55  .** SQLITE_DEFAU
10413 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49  LT_FILE_PERMISSI
10414 4f 4e 53 20 61 64 6a 75 73 74 65 64 20 62 79 20  ONS adjusted by 
10415 74 68 65 20 75 6d 61 73 6b 2e 0a 2a 2a 20 42 75  the umask..** Bu
10416 74 20 69 66 20 74 68 65 20 66 69 6c 65 20 62 65  t if the file be
10417 69 6e 67 20 6f 70 65 6e 65 64 20 69 73 20 61 20  ing opened is a 
10418 57 41 4c 20 6f 72 20 72 65 67 75 6c 61 72 20 6a  WAL or regular j
10419 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65  ournal file, the
1041a 6e 20 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74  n .** this funct
1041b 69 6f 6e 20 71 75 65 72 69 65 73 20 74 68 65 20  ion queries the 
1041c 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 6f 72 20  file-system for 
1041d 74 68 65 20 70 65 72 6d 69 73 73 69 6f 6e 73 20  the permissions 
1041e 6f 6e 20 74 68 65 20 0a 2a 2a 20 63 6f 72 72 65  on the .** corre
1041f 73 70 6f 6e 64 69 6e 67 20 64 61 74 61 62 61 73  sponding databas
10420 65 20 66 69 6c 65 20 61 6e 64 20 73 65 74 73 20  e file and sets 
10421 2a 70 4d 6f 64 65 20 74 6f 20 74 68 69 73 20 76  *pMode to this v
10422 61 6c 75 65 2e 20 57 68 65 6e 65 76 65 72 20 0a  alue. Whenever .
10423 2a 2a 20 70 6f 73 73 69 62 6c 65 2c 20 57 41 4c  ** possible, WAL
10424 20 61 6e 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   and journal fil
10425 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20 75  es are created u
10426 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 70 65  sing the same pe
10427 72 6d 69 73 73 69 6f 6e 73 20 0a 2a 2a 20 61 73  rmissions .** as
10428 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20   the associated 
10429 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
1042a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49  *.** If the SQLI
1042b 54 45 5f 45 4e 41 42 4c 45 5f 38 5f 33 5f 4e 41  TE_ENABLE_8_3_NA
1042c 4d 45 53 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e  MES option is en
1042d 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 0a  abled, then the.
1042e 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65  ** original file
1042f 6e 61 6d 65 20 69 73 20 75 6e 61 76 61 69 6c 61  name is unavaila
10430 62 6c 65 2e 20 20 42 75 74 20 38 5f 33 5f 4e 41  ble.  But 8_3_NA
10431 4d 45 53 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  MES is only used
10432 20 66 6f 72 0a 2a 2a 20 46 41 54 20 66 69 6c 65   for.** FAT file
10433 73 79 73 74 65 6d 73 20 61 6e 64 20 70 65 72 6d  systems and perm
10434 69 73 73 69 6f 6e 73 20 64 6f 20 6e 6f 74 20 6d  issions do not m
10435 61 74 74 65 72 20 74 68 65 72 65 2c 20 73 6f 20  atter there, so 
10436 6a 75 73 74 20 75 73 65 0a 2a 2a 20 74 68 65 20  just use.** the 
10437 64 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69  default permissi
10438 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ons..*/.static i
10439 6e 74 20 66 69 6e 64 43 72 65 61 74 65 46 69 6c  nt findCreateFil
1043a 65 4d 6f 64 65 28 0a 20 20 63 6f 6e 73 74 20 63  eMode(.  const c
1043b 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20  har *zPath,     
1043c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 74 68           /* Path
1043d 20 6f 66 20 66 69 6c 65 20 28 70 6f 73 73 69 62   of file (possib
1043e 6c 79 29 20 62 65 69 6e 67 20 63 72 65 61 74 65  ly) being create
1043f 64 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  d */.  int flags
10440 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
10441 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
10442 70 61 73 73 65 64 20 61 73 20 34 74 68 20 61 72  passed as 4th ar
10443 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e 28  gument to xOpen(
10444 29 20 2a 2f 0a 20 20 6d 6f 64 65 5f 74 20 2a 70  ) */.  mode_t *p
10445 4d 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20  Mode,           
10446 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50         /* OUT: P
10447 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 6f 70  ermissions to op
10448 65 6e 20 66 69 6c 65 20 77 69 74 68 20 2a 2f 0a  en file with */.
10449 20 20 75 69 64 5f 74 20 2a 70 55 69 64 2c 20 20    uid_t *pUid,  
1044a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1044b 20 20 2f 2a 20 4f 55 54 3a 20 75 69 64 20 74 6f    /* OUT: uid to
1044c 20 73 65 74 20 6f 6e 20 74 68 65 20 66 69 6c 65   set on the file
1044d 20 2a 2f 0a 20 20 67 69 64 5f 74 20 2a 70 47 69   */.  gid_t *pGi
1044e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
1044f 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 67 69        /* OUT: gi
10450 64 20 74 6f 20 73 65 74 20 6f 6e 20 74 68 65 20  d to set on the 
10451 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  file */.){.  int
10452 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
10453 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10454 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20  Return Code */. 
10455 20 2a 70 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 2a   *pMode = 0;.  *
10456 70 55 69 64 20 3d 20 30 3b 0a 20 20 2a 70 47 69  pUid = 0;.  *pGi
10457 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 66 6c 61  d = 0;.  if( fla
10458 67 73 20 26 20 28 53 51 4c 49 54 45 5f 4f 50 45  gs & (SQLITE_OPE
10459 4e 5f 57 41 4c 7c 53 51 4c 49 54 45 5f 4f 50 45  N_WAL|SQLITE_OPE
1045a 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 20  N_MAIN_JOURNAL) 
1045b 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 44 62 5b  ){.    char zDb[
1045c 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b  MAX_PATHNAME+1];
1045d 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
1045e 20 66 69 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20   file path */.  
1045f 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20 20 20    int nDb;      
10460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10461 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 61 6c  /* Number of val
10462 69 64 20 62 79 74 65 73 20 69 6e 20 7a 44 62 20  id bytes in zDb 
10463 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 73 74  */.    struct st
10464 61 74 20 73 53 74 61 74 3b 20 20 20 20 20 20 20  at sStat;       
10465 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 6f       /* Output o
10466 66 20 73 74 61 74 28 29 20 6f 6e 20 64 61 74 61  f stat() on data
10467 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20  base file */..  
10468 20 20 2f 2a 20 7a 50 61 74 68 20 69 73 20 61 20    /* zPath is a 
10469 70 61 74 68 20 74 6f 20 61 20 57 41 4c 20 6f 72  path to a WAL or
1046a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54   journal file. T
1046b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
1046c 63 6b 20 64 65 72 69 76 65 73 0a 20 20 20 20 2a  ck derives.    *
1046d 2a 20 74 68 65 20 70 61 74 68 20 74 6f 20 74 68  * the path to th
1046e 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74  e associated dat
1046f 61 62 61 73 65 20 66 69 6c 65 20 66 72 6f 6d 20  abase file from 
10470 7a 50 61 74 68 2e 20 54 68 69 73 20 62 6c 6f 63  zPath. This bloc
10471 6b 20 68 61 6e 64 6c 65 73 0a 20 20 20 20 2a 2a  k handles.    **
10472 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e   the following n
10473 61 6d 69 6e 67 20 63 6f 6e 76 65 6e 74 69 6f 6e  aming convention
10474 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  s:.    **.    **
10475 20 20 20 22 3c 70 61 74 68 20 74 6f 20 64 62 3e     "<path to db>
10476 2d 6a 6f 75 72 6e 61 6c 22 0a 20 20 20 20 2a 2a  -journal".    **
10477 20 20 20 22 3c 70 61 74 68 20 74 6f 20 64 62 3e     "<path to db>
10478 2d 77 61 6c 22 0a 20 20 20 20 2a 2a 20 20 20 22  -wal".    **   "
10479 3c 70 61 74 68 20 74 6f 20 64 62 3e 2d 6a 6f 75  <path to db>-jou
1047a 72 6e 61 6c 4e 4e 22 0a 20 20 20 20 2a 2a 20 20  rnalNN".    **  
1047b 20 22 3c 70 61 74 68 20 74 6f 20 64 62 3e 2d 77   "<path to db>-w
1047c 61 6c 4e 4e 22 0a 20 20 20 20 2a 2a 0a 20 20 20  alNN".    **.   
1047d 20 2a 2a 20 77 68 65 72 65 20 4e 4e 20 69 73 20   ** where NN is 
1047e 61 20 64 65 63 69 6d 61 6c 20 6e 75 6d 62 65 72  a decimal number
1047f 2e 20 54 68 65 20 4e 4e 20 6e 61 6d 69 6e 67 20  . The NN naming 
10480 73 63 68 65 6d 65 73 20 61 72 65 20 0a 20 20 20  schemes are .   
10481 20 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20   ** used by the 
10482 74 65 73 74 5f 6d 75 6c 74 69 70 6c 65 78 2e 63  test_multiplex.c
10483 20 6d 6f 64 75 6c 65 2e 0a 20 20 20 20 2a 2f 0a   module..    */.
10484 20 20 20 20 6e 44 62 20 3d 20 73 71 6c 69 74 65      nDb = sqlite
10485 33 53 74 72 6c 65 6e 33 30 28 7a 50 61 74 68 29  3Strlen30(zPath)
10486 20 2d 20 31 3b 20 0a 23 69 66 64 65 66 20 53 51   - 1; .#ifdef SQ
10487 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 38 5f 33 5f  LITE_ENABLE_8_3_
10488 4e 41 4d 45 53 0a 20 20 20 20 77 68 69 6c 65 28  NAMES.    while(
10489 20 6e 44 62 3e 30 20 26 26 20 73 71 6c 69 74 65   nDb>0 && sqlite
1048a 33 49 73 61 6c 6e 75 6d 28 7a 50 61 74 68 5b 6e  3Isalnum(zPath[n
1048b 44 62 5d 29 20 29 20 6e 44 62 2d 2d 3b 0a 20 20  Db]) ) nDb--;.  
1048c 20 20 69 66 28 20 6e 44 62 3d 3d 30 20 7c 7c 20    if( nDb==0 || 
1048d 7a 50 61 74 68 5b 6e 44 62 5d 21 3d 27 2d 27 20  zPath[nDb]!='-' 
1048e 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
1048f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 20 20 77 68  OK;.#else.    wh
10490 69 6c 65 28 20 7a 50 61 74 68 5b 6e 44 62 5d 21  ile( zPath[nDb]!
10491 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 61 73  ='-' ){.      as
10492 73 65 72 74 28 20 6e 44 62 3e 30 20 29 3b 0a 20  sert( nDb>0 );. 
10493 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 50 61       assert( zPa
10494 74 68 5b 6e 44 62 5d 21 3d 27 5c 6e 27 20 29 3b  th[nDb]!='\n' );
10495 0a 20 20 20 20 20 20 6e 44 62 2d 2d 3b 0a 20 20  .      nDb--;.  
10496 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 6d    }.#endif.    m
10497 65 6d 63 70 79 28 7a 44 62 2c 20 7a 50 61 74 68  emcpy(zDb, zPath
10498 2c 20 6e 44 62 29 3b 0a 20 20 20 20 7a 44 62 5b  , nDb);.    zDb[
10499 6e 44 62 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20  nDb] = '\0';..  
1049a 20 20 69 66 28 20 30 3d 3d 6f 73 53 74 61 74 28    if( 0==osStat(
1049b 7a 44 62 2c 20 26 73 53 74 61 74 29 20 29 7b 0a  zDb, &sStat) ){.
1049c 20 20 20 20 20 20 2a 70 4d 6f 64 65 20 3d 20 73        *pMode = s
1049d 53 74 61 74 2e 73 74 5f 6d 6f 64 65 20 26 20 30  Stat.st_mode & 0
1049e 37 37 37 3b 0a 20 20 20 20 20 20 2a 70 55 69 64  777;.      *pUid
1049f 20 3d 20 73 53 74 61 74 2e 73 74 5f 75 69 64 3b   = sStat.st_uid;
104a0 0a 20 20 20 20 20 20 2a 70 47 69 64 20 3d 20 73  .      *pGid = s
104a1 53 74 61 74 2e 73 74 5f 67 69 64 3b 0a 20 20 20  Stat.st_gid;.   
104a2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
104a3 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
104a4 46 53 54 41 54 3b 0a 20 20 20 20 7d 0a 20 20 7d  FSTAT;.    }.  }
104a5 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26  else if( flags &
104a6 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c   SQLITE_OPEN_DEL
104a7 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 20 20  ETEONCLOSE ){.  
104a8 20 20 2a 70 4d 6f 64 65 20 3d 20 30 36 30 30 3b    *pMode = 0600;
104a9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
104aa 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
104ab 74 68 65 20 66 69 6c 65 20 7a 50 61 74 68 2e 0a  the file zPath..
104ac 2a 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75 73 6c  ** .** Previousl
104ad 79 2c 20 74 68 65 20 53 51 4c 69 74 65 20 4f 53  y, the SQLite OS
104ae 20 6c 61 79 65 72 20 75 73 65 64 20 74 68 72 65   layer used thre
104af 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 70  e functions in p
104b0 6c 61 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20  lace of this.** 
104b1 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73  one:.**.**     s
104b2 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64  qlite3OsOpenRead
104b3 57 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20 20 20  Write();.**     
104b4 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61  sqlite3OsOpenRea
104b5 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20 20 20  dOnly();.**     
104b6 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 63  sqlite3OsOpenExc
104b7 6c 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20  lusive();.**.** 
104b8 54 68 65 73 65 20 63 61 6c 6c 73 20 63 6f 72 72  These calls corr
104b9 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f  espond to the fo
104ba 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74  llowing combinat
104bb 69 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a 0a 2a  ions of flags:.*
104bc 2a 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 72 69  *.**     ReadWri
104bd 74 65 28 29 20 2d 3e 20 20 20 20 20 28 52 45 41  te() ->     (REA
104be 44 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 29  DWRITE | CREATE)
104bf 0a 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e 6c 79  .**     ReadOnly
104c0 28 29 20 20 2d 3e 20 20 20 20 20 28 52 45 41 44  ()  ->     (READ
104c1 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20 4f 70  ONLY) .**     Op
104c2 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 2d 3e  enExclusive() ->
104c3 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52   (READWRITE | CR
104c4 45 41 54 45 20 7c 20 45 58 43 4c 55 53 49 56 45  EATE | EXCLUSIVE
104c5 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20  ).**.** The old 
104c6 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20  OpenExclusive() 
104c7 61 63 63 65 70 74 65 64 20 61 20 62 6f 6f 6c 65  accepted a boole
104c8 61 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20 22 64  an argument - "d
104c9 65 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a 20 74  elFlag". If.** t
104ca 72 75 65 2c 20 74 68 65 20 66 69 6c 65 20 77 61  rue, the file wa
104cb 73 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20  s configured to 
104cc 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  be automatically
104cd 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68   deleted when th
104ce 65 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64 6c 65  e.** file handle
104cf 20 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63 68 69   closed. To achi
104d0 65 76 65 20 74 68 65 20 73 61 6d 65 20 65 66 66  eve the same eff
104d1 65 63 74 20 75 73 69 6e 67 20 74 68 69 73 20 6e  ect using this n
104d2 65 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  ew .** interface
104d3 2c 20 61 64 64 20 74 68 65 20 44 45 4c 45 54 45  , add the DELETE
104d4 4f 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74 6f 20  ONCLOSE flag to 
104d5 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 20  those specified 
104d6 61 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20 4f 70  above for .** Op
104d7 65 6e 45 78 63 6c 75 73 69 76 65 28 29 2e 0a 2a  enExclusive()..*
104d8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
104d9 78 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  xOpen(.  sqlite3
104da 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
104db 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53        /* The VFS
104dc 20 66 6f 72 20 77 68 69 63 68 20 74 68 69 73 20   for which this 
104dd 69 73 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74  is the xOpen met
104de 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  hod */.  const c
104df 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20  har *zPath,     
104e0 20 20 20 20 20 20 2f 2a 20 50 61 74 68 6e 61 6d        /* Pathnam
104e1 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20  e of file to be 
104e2 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69  opened */.  sqli
104e3 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c  te3_file *pFile,
104e4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
104e5 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
104e6 74 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 6e 20  to be filled in 
104e7 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  */.  int flags, 
104e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
104e9 20 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61 67 73    /* Input flags
104ea 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20   to control the 
104eb 6f 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  opening */.  int
104ec 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20   *pOutFlags     
104ed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74            /* Out
104ee 70 75 74 20 66 6c 61 67 73 20 72 65 74 75 72 6e  put flags return
104ef 65 64 20 74 6f 20 53 51 4c 69 74 65 20 63 6f 72  ed to SQLite cor
104f0 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78 46 69  e */.){.  unixFi
104f1 6c 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 69 6c  le *p = (unixFil
104f2 65 20 2a 29 70 46 69 6c 65 3b 0a 20 20 69 6e 74  e *)pFile;.  int
104f3 20 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20   fd = -1;       
104f4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
104f5 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 72  ile descriptor r
104f6 65 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e 28  eturned by open(
104f7 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46  ) */.  int openF
104f8 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20  lags = 0;       
104f9 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74        /* Flags t
104fa 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e 28 29  o pass to open()
104fb 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65 20   */.  int eType 
104fc 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46 46  = flags&0xFFFFFF
104fd 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66 20  00;  /* Type of 
104fe 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a  file to open */.
104ff 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20 20    int noLock;   
10500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10501 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74   /* True to omit
10502 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69   locking primiti
10503 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  ves */.  int rc 
10504 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
10505 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74          /* Funct
10506 69 6f 6e 20 52 65 74 75 72 6e 20 43 6f 64 65 20  ion Return Code 
10507 2a 2f 0a 20 20 69 6e 74 20 63 74 72 6c 46 6c 61  */.  int ctrlFla
10508 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  gs = 0;         
10509 20 20 20 20 2f 2a 20 55 4e 49 58 46 49 4c 45 5f      /* UNIXFILE_
1050a 2a 20 66 6c 61 67 73 20 2a 2f 0a 0a 20 20 69 6e  * flags */..  in
1050b 74 20 69 73 45 78 63 6c 75 73 69 76 65 20 20 3d  t isExclusive  =
1050c 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45   (flags & SQLITE
1050d 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 29  _OPEN_EXCLUSIVE)
1050e 3b 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65  ;.  int isDelete
1050f 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20       = (flags & 
10510 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
10511 54 45 4f 4e 43 4c 4f 53 45 29 3b 0a 20 20 69 6e  TEONCLOSE);.  in
10512 74 20 69 73 43 72 65 61 74 65 20 20 20 20 20 3d  t isCreate     =
10513 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45   (flags & SQLITE
10514 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20  _OPEN_CREATE);. 
10515 20 69 6e 74 20 69 73 52 65 61 64 6f 6e 6c 79 20   int isReadonly 
10516 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c    = (flags & SQL
10517 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
10518 59 29 3b 0a 20 20 69 6e 74 20 69 73 52 65 61 64  Y);.  int isRead
10519 57 72 69 74 65 20 20 3d 20 28 66 6c 61 67 73 20  Write  = (flags 
1051a 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  & SQLITE_OPEN_RE
1051b 41 44 57 52 49 54 45 29 3b 0a 23 69 66 20 53 51  ADWRITE);.#if SQ
1051c 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
1051d 49 4e 47 5f 53 54 59 4c 45 0a 20 20 69 6e 74 20  ING_STYLE.  int 
1051e 69 73 41 75 74 6f 50 72 6f 78 79 20 20 3d 20 28  isAutoProxy  = (
1051f 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
10520 50 45 4e 5f 41 55 54 4f 50 52 4f 58 59 29 3b 0a  PEN_AUTOPROXY);.
10521 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e  #endif.#if defin
10522 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 7c 7c  ed(__APPLE__) ||
10523 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
10524 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 73  OCKING_STYLE.  s
10525 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73 49  truct statfs fsI
10526 6e 66 6f 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  nfo;.#endif..  /
10527 2a 20 49 66 20 63 72 65 61 74 69 6e 67 20 61 20  * If creating a 
10528 6d 61 73 74 65 72 20 6f 72 20 6d 61 69 6e 2d 66  master or main-f
10529 69 6c 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 69  ile journal, thi
1052a 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20  s function will 
1052b 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 66 69 6c 65  open.  ** a file
1052c 2d 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 74  -descriptor on t
1052d 68 65 20 64 69 72 65 63 74 6f 72 79 20 74 6f 6f  he directory too
1052e 2e 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65  . The first time
1052f 20 75 6e 69 78 53 79 6e 63 28 29 0a 20 20 2a 2a   unixSync().  **
10530 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 64   is called the d
10531 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65  irectory file de
10532 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65  scriptor will be
10533 20 66 73 79 6e 63 28 29 65 64 20 61 6e 64 20 63   fsync()ed and c
10534 6c 6f 73 65 28 29 64 2e 0a 20 20 2a 2f 0a 20 20  lose()d..  */.  
10535 69 6e 74 20 73 79 6e 63 44 69 72 20 3d 20 28 69  int syncDir = (i
10536 73 43 72 65 61 74 65 20 26 26 20 28 0a 20 20 20  sCreate && (.   
10537 20 20 20 20 20 65 54 79 70 65 3d 3d 53 51 4c 49       eType==SQLI
10538 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a  TE_OPEN_MASTER_J
10539 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 7c 7c 20  OURNAL .     || 
1053a 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50  eType==SQLITE_OP
1053b 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20  EN_MAIN_JOURNAL 
1053c 0a 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d  .     || eType==
1053d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 57 41 4c 0a  SQLITE_OPEN_WAL.
1053e 20 20 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61    ));..  /* If a
1053f 72 67 75 6d 65 6e 74 20 7a 50 61 74 68 20 69 73  rgument zPath is
10540 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
10541 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
10542 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 6f 70  s required to op
10543 65 6e 0a 20 20 2a 2a 20 61 20 74 65 6d 70 6f 72  en.  ** a tempor
10544 61 72 79 20 66 69 6c 65 2e 20 55 73 65 20 74 68  ary file. Use th
10545 69 73 20 62 75 66 66 65 72 20 74 6f 20 73 74 6f  is buffer to sto
10546 72 65 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65  re the file name
10547 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72   in..  */.  char
10548 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41   zTmpname[MAX_PA
10549 54 48 4e 41 4d 45 2b 32 5d 3b 0a 20 20 63 6f 6e  THNAME+2];.  con
1054a 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d  st char *zName =
1054b 20 7a 50 61 74 68 3b 0a 0a 20 20 2f 2a 20 43 68   zPath;..  /* Ch
1054c 65 63 6b 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  eck the followin
1054d 67 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65  g statements are
1054e 20 74 72 75 65 3a 20 0a 20 20 2a 2a 0a 20 20 2a   true: .  **.  *
1054f 2a 20 20 20 28 61 29 20 45 78 61 63 74 6c 79 20  *   (a) Exactly 
10550 6f 6e 65 20 6f 66 20 74 68 65 20 52 45 41 44 57  one of the READW
10551 52 49 54 45 20 61 6e 64 20 52 45 41 44 4f 4e 4c  RITE and READONL
10552 59 20 66 6c 61 67 73 20 6d 75 73 74 20 62 65 20  Y flags must be 
10553 73 65 74 2c 20 61 6e 64 20 0a 20 20 2a 2a 20 20  set, and .  **  
10554 20 28 62 29 20 69 66 20 43 52 45 41 54 45 20 69   (b) if CREATE i
10555 73 20 73 65 74 2c 20 74 68 65 6e 20 52 45 41 44  s set, then READ
10556 57 52 49 54 45 20 6d 75 73 74 20 61 6c 73 6f 20  WRITE must also 
10557 62 65 20 73 65 74 2c 20 61 6e 64 0a 20 20 2a 2a  be set, and.  **
10558 20 20 20 28 63 29 20 69 66 20 45 58 43 4c 55 53     (c) if EXCLUS
10559 49 56 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e  IVE is set, then
1055a 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73   CREATE must als
1055b 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2a 20 20  o be set..  **  
1055c 20 28 64 29 20 69 66 20 44 45 4c 45 54 45 4f 4e   (d) if DELETEON
1055d 43 4c 4f 53 45 20 69 73 20 73 65 74 2c 20 74 68  CLOSE is set, th
1055e 65 6e 20 43 52 45 41 54 45 20 6d 75 73 74 20 61  en CREATE must a
1055f 6c 73 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2f  lso be set..  */
10560 0a 20 20 61 73 73 65 72 74 28 28 69 73 52 65 61  .  assert((isRea
10561 64 6f 6e 6c 79 3d 3d 30 20 7c 7c 20 69 73 52 65  donly==0 || isRe
10562 61 64 57 72 69 74 65 3d 3d 30 29 20 26 26 20 28  adWrite==0) && (
10563 69 73 52 65 61 64 57 72 69 74 65 20 7c 7c 20 69  isReadWrite || i
10564 73 52 65 61 64 6f 6e 6c 79 29 29 3b 0a 20 20 61  sReadonly));.  a
10565 73 73 65 72 74 28 69 73 43 72 65 61 74 65 3d 3d  ssert(isCreate==
10566 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65  0 || isReadWrite
10567 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 45 78  );.  assert(isEx
10568 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20 69 73  clusive==0 || is
10569 43 72 65 61 74 65 29 3b 0a 20 20 61 73 73 65 72  Create);.  asser
1056a 74 28 69 73 44 65 6c 65 74 65 3d 3d 30 20 7c 7c  t(isDelete==0 ||
1056b 20 69 73 43 72 65 61 74 65 29 3b 0a 0a 20 20 2f   isCreate);..  /
1056c 2a 20 54 68 65 20 6d 61 69 6e 20 44 42 2c 20 6d  * The main DB, m
1056d 61 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 57 41 4c  ain journal, WAL
1056e 20 66 69 6c 65 20 61 6e 64 20 6d 61 73 74 65 72   file and master
1056f 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76   journal are nev
10570 65 72 20 0a 20 20 2a 2a 20 61 75 74 6f 6d 61 74  er .  ** automat
10571 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 2e 20  ically deleted. 
10572 4e 6f 72 20 61 72 65 20 74 68 65 79 20 65 76 65  Nor are they eve
10573 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  r temporary file
10574 73 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  s.  */.  assert(
10575 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a   (!isDelete && z
10576 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d  Name) || eType!=
10577 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
10578 5f 44 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _DB );.  assert(
10579 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a   (!isDelete && z
1057a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d  Name) || eType!=
1057b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
1057c 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a 20 20 61 73  _JOURNAL );.  as
1057d 73 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 65  sert( (!isDelete
1057e 20 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54   && zName) || eT
1057f 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype!=SQLITE_OPEN
10580 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20  _MASTER_JOURNAL 
10581 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 21 69  );.  assert( (!i
10582 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65  sDelete && zName
10583 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49  ) || eType!=SQLI
10584 54 45 5f 4f 50 45 4e 5f 57 41 4c 20 29 3b 0a 0a  TE_OPEN_WAL );..
10585 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74    /* Assert that
10586 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72   the upper layer
10587 20 68 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 20   has set one of 
10588 74 68 65 20 22 66 69 6c 65 2d 74 79 70 65 22 20  the "file-type" 
10589 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73 65  flags. */.  asse
1058a 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54  rt( eType==SQLIT
1058b 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20  E_OPEN_MAIN_DB  
1058c 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51      || eType==SQ
1058d 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44  LITE_OPEN_TEMP_D
1058e 42 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79  B .       || eTy
1058f 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe==SQLITE_OPEN_
10590 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20  MAIN_JOURNAL || 
10591 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50  eType==SQLITE_OP
10592 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20  EN_TEMP_JOURNAL 
10593 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65  .       || eType
10594 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55  ==SQLITE_OPEN_SU
10595 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65 54  BJOURNAL   || eT
10596 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
10597 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20  _MASTER_JOURNAL 
10598 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65  .       || eType
10599 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52  ==SQLITE_OPEN_TR
1059a 41 4e 53 49 45 4e 54 5f 44 42 20 7c 7c 20 65 54  ANSIENT_DB || eT
1059b 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
1059c 5f 57 41 4c 0a 20 20 29 3b 0a 0a 20 20 6d 65 6d  _WAL.  );..  mem
1059d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66  set(p, 0, sizeof
1059e 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20  (unixFile));..  
1059f 69 66 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54  if( eType==SQLIT
105a0 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29  E_OPEN_MAIN_DB )
105a1 7b 0a 20 20 20 20 55 6e 69 78 55 6e 75 73 65 64  {.    UnixUnused
105a2 46 64 20 2a 70 55 6e 75 73 65 64 3b 0a 20 20 20  Fd *pUnused;.   
105a3 20 70 55 6e 75 73 65 64 20 3d 20 66 69 6e 64 52   pUnused = findR
105a4 65 75 73 61 62 6c 65 46 64 28 7a 4e 61 6d 65 2c  eusableFd(zName,
105a5 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28   flags);.    if(
105a6 20 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20   pUnused ){.    
105a7 20 20 66 64 20 3d 20 70 55 6e 75 73 65 64 2d 3e    fd = pUnused->
105a8 66 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  fd;.    }else{. 
105a9 20 20 20 20 20 70 55 6e 75 73 65 64 20 3d 20 73       pUnused = s
105aa 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
105ab 7a 65 6f 66 28 2a 70 55 6e 75 73 65 64 29 29 3b  zeof(*pUnused));
105ac 0a 20 20 20 20 20 20 69 66 28 20 21 70 55 6e 75  .      if( !pUnu
105ad 73 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72  sed ){.        r
105ae 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
105af 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  EM;.      }.    
105b0 7d 0a 20 20 20 20 70 2d 3e 70 55 6e 75 73 65 64  }.    p->pUnused
105b1 20 3d 20 70 55 6e 75 73 65 64 3b 0a 0a 20 20 20   = pUnused;..   
105b2 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c   /* Database fil
105b3 65 6e 61 6d 65 73 20 61 72 65 20 64 6f 75 62 6c  enames are doubl
105b4 65 2d 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65  e-zero terminate
105b5 64 20 69 66 20 74 68 65 79 20 61 72 65 20 6e 6f  d if they are no
105b6 74 0a 20 20 20 20 2a 2a 20 55 52 49 73 20 77 69  t.    ** URIs wi
105b7 74 68 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20  th parameters.  
105b8 48 65 6e 63 65 2c 20 74 68 65 79 20 63 61 6e 20  Hence, they can 
105b9 61 6c 77 61 79 73 20 62 65 20 70 61 73 73 65 64  always be passed
105ba 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c   into.    ** sql
105bb 69 74 65 33 5f 75 72 69 5f 70 61 72 61 6d 65 74  ite3_uri_paramet
105bc 65 72 28 29 2e 20 2a 2f 0a 20 20 20 20 61 73 73  er(). */.    ass
105bd 65 72 74 28 20 28 66 6c 61 67 73 20 26 20 53 51  ert( (flags & SQ
105be 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49 29 20 7c  LITE_OPEN_URI) |
105bf 7c 20 7a 4e 61 6d 65 5b 73 74 72 6c 65 6e 28 7a  | zName[strlen(z
105c0 4e 61 6d 65 29 2b 31 5d 3d 3d 30 20 29 3b 0a 0a  Name)+1]==0 );..
105c1 20 20 7d 65 6c 73 65 20 69 66 28 20 21 7a 4e 61    }else if( !zNa
105c2 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  me ){.    /* If 
105c3 7a 4e 61 6d 65 20 69 73 20 4e 55 4c 4c 2c 20 74  zName is NULL, t
105c4 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 20 69  he upper layer i
105c5 73 20 72 65 71 75 65 73 74 69 6e 67 20 61 20 74  s requesting a t
105c6 65 6d 70 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20  emp file. */.   
105c7 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74 65   assert(isDelete
105c8 20 26 26 20 21 73 79 6e 63 44 69 72 29 3b 0a 20   && !syncDir);. 
105c9 20 20 20 72 63 20 3d 20 75 6e 69 78 47 65 74 54     rc = unixGetT
105ca 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48  empname(MAX_PATH
105cb 4e 41 4d 45 2b 32 2c 20 7a 54 6d 70 6e 61 6d 65  NAME+2, zTmpname
105cc 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
105cd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
105ce 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
105cf 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a   }.    zName = z
105d0 54 6d 70 6e 61 6d 65 3b 0a 0a 20 20 20 20 2f 2a  Tmpname;..    /*
105d1 20 47 65 6e 65 72 61 74 65 64 20 74 65 6d 70 6f   Generated tempo
105d2 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 73 20 61  rary filenames a
105d3 72 65 20 61 6c 77 61 79 73 20 64 6f 75 62 6c 65  re always double
105d4 2d 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64  -zero terminated
105d5 0a 20 20 20 20 2a 2a 20 66 6f 72 20 75 73 65 20  .    ** for use 
105d6 62 79 20 73 71 6c 69 74 65 33 5f 75 72 69 5f 70  by sqlite3_uri_p
105d7 61 72 61 6d 65 74 65 72 28 29 2e 20 2a 2f 0a 20  arameter(). */. 
105d8 20 20 20 61 73 73 65 72 74 28 20 7a 4e 61 6d 65     assert( zName
105d9 5b 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 2b 31  [strlen(zName)+1
105da 5d 3d 3d 30 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f  ]==0 );.  }..  /
105db 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20  * Determine the 
105dc 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6c 61  value of the fla
105dd 67 73 20 70 61 72 61 6d 65 74 65 72 20 70 61 73  gs parameter pas
105de 73 65 64 20 74 6f 20 50 4f 53 49 58 20 66 75 6e  sed to POSIX fun
105df 63 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 28  ction.  ** open(
105e0 29 2e 20 54 68 65 73 65 20 6d 75 73 74 20 62 65  ). These must be
105e1 20 63 61 6c 63 75 6c 61 74 65 64 20 65 76 65 6e   calculated even
105e2 20 69 66 20 6f 70 65 6e 28 29 20 69 73 20 6e 6f   if open() is no
105e3 74 20 63 61 6c 6c 65 64 2c 20 61 73 0a 20 20 2a  t called, as.  *
105e4 2a 20 74 68 65 79 20 6d 61 79 20 62 65 20 73 74  * they may be st
105e5 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  ored as part of 
105e6 74 68 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20  the file handle 
105e7 61 6e 64 20 75 73 65 64 20 62 79 20 74 68 65 20  and used by the 
105e8 0a 20 20 2a 2a 20 27 63 6f 6e 63 68 20 66 69 6c  .  ** 'conch fil
105e9 65 27 20 6c 6f 63 6b 69 6e 67 20 66 75 6e 63 74  e' locking funct
105ea 69 6f 6e 73 20 6c 61 74 65 72 20 6f 6e 2e 20 20  ions later on.  
105eb 2a 2f 0a 20 20 69 66 28 20 69 73 52 65 61 64 6f  */.  if( isReado
105ec 6e 6c 79 20 29 20 20 6f 70 65 6e 46 6c 61 67 73  nly )  openFlags
105ed 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20   |= O_RDONLY;.  
105ee 69 66 28 20 69 73 52 65 61 64 57 72 69 74 65 20  if( isReadWrite 
105ef 29 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f  ) openFlags |= O
105f0 5f 52 44 57 52 3b 0a 20 20 69 66 28 20 69 73 43  _RDWR;.  if( isC
105f1 72 65 61 74 65 20 29 20 20 20 20 6f 70 65 6e 46  reate )    openF
105f2 6c 61 67 73 20 7c 3d 20 4f 5f 43 52 45 41 54 3b  lags |= O_CREAT;
105f3 0a 20 20 69 66 28 20 69 73 45 78 63 6c 75 73 69  .  if( isExclusi
105f4 76 65 20 29 20 6f 70 65 6e 46 6c 61 67 73 20 7c  ve ) openFlags |
105f5 3d 20 28 4f 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 4f  = (O_EXCL|O_NOFO
105f6 4c 4c 4f 57 29 3b 0a 20 20 6f 70 65 6e 46 6c 61  LLOW);.  openFla
105f7 67 73 20 7c 3d 20 28 4f 5f 4c 41 52 47 45 46 49  gs |= (O_LARGEFI
105f8 4c 45 7c 4f 5f 42 49 4e 41 52 59 29 3b 0a 0a 20  LE|O_BINARY);.. 
105f9 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20   if( fd<0 ){.   
105fa 20 6d 6f 64 65 5f 74 20 6f 70 65 6e 4d 6f 64 65   mode_t openMode
105fb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
105fc 2a 20 50 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f  * Permissions to
105fd 20 63 72 65 61 74 65 20 66 69 6c 65 20 77 69 74   create file wit
105fe 68 20 2a 2f 0a 20 20 20 20 75 69 64 5f 74 20 75  h */.    uid_t u
105ff 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  id;             
10600 20 20 20 20 20 20 20 2f 2a 20 55 73 65 72 69 64         /* Userid
10601 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 2a 2f   for the file */
10602 0a 20 20 20 20 67 69 64 5f 74 20 67 69 64 3b 20  .    gid_t gid; 
10603 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10604 20 20 20 2f 2a 20 47 72 6f 75 70 69 64 20 66 6f     /* Groupid fo
10605 72 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20  r the file */.  
10606 20 20 72 63 20 3d 20 66 69 6e 64 43 72 65 61 74    rc = findCreat
10607 65 46 69 6c 65 4d 6f 64 65 28 7a 4e 61 6d 65 2c  eFileMode(zName,
10608 20 66 6c 61 67 73 2c 20 26 6f 70 65 6e 4d 6f 64   flags, &openMod
10609 65 2c 20 26 75 69 64 2c 20 26 67 69 64 29 3b 0a  e, &uid, &gid);.
1060a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1060b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61  TE_OK ){.      a
1060c 73 73 65 72 74 28 20 21 70 2d 3e 70 55 6e 75 73  ssert( !p->pUnus
1060d 65 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ed );.      asse
1060e 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54  rt( eType==SQLIT
1060f 45 5f 4f 50 45 4e 5f 57 41 4c 20 7c 7c 20 65 54  E_OPEN_WAL || eT
10610 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
10611 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 29 3b  _MAIN_JOURNAL );
10612 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
10613 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 64 20 3d  ;.    }.    fd =
10614 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 7a 4e 61   robust_open(zNa
10615 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 6f  me, openFlags, o
10616 70 65 6e 4d 6f 64 65 29 3b 0a 20 20 20 20 4f 53  penMode);.    OS
10617 54 52 41 43 45 28 28 22 4f 50 45 4e 58 20 20 20  TRACE(("OPENX   
10618 25 2d 33 64 20 25 73 20 30 25 6f 5c 6e 22 2c 20  %-3d %s 0%o\n", 
10619 66 64 2c 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46  fd, zName, openF
1061a 6c 61 67 73 29 29 3b 0a 20 20 20 20 69 66 28 20  lags));.    if( 
1061b 66 64 3c 30 20 26 26 20 65 72 72 6e 6f 21 3d 45  fd<0 && errno!=E
1061c 49 53 44 49 52 20 26 26 20 69 73 52 65 61 64 57  ISDIR && isReadW
1061d 72 69 74 65 20 26 26 20 21 69 73 45 78 63 6c 75  rite && !isExclu
1061e 73 69 76 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a  sive ){.      /*
1061f 20 46 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 20   Failed to open 
10620 74 68 65 20 66 69 6c 65 20 66 6f 72 20 72 65 61  the file for rea
10621 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e 20  d/write access. 
10622 54 72 79 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a  Try read-only. *
10623 2f 0a 20 20 20 20 20 20 66 6c 61 67 73 20 26 3d  /.      flags &=
10624 20 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   ~(SQLITE_OPEN_R
10625 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f  EADWRITE|SQLITE_
10626 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20  OPEN_CREATE);.  
10627 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 26 3d      openFlags &=
10628 20 7e 28 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41   ~(O_RDWR|O_CREA
10629 54 29 3b 0a 20 20 20 20 20 20 66 6c 61 67 73 20  T);.      flags 
1062a 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  |= SQLITE_OPEN_R
1062b 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 6f  EADONLY;.      o
1062c 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44  penFlags |= O_RD
1062d 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 69 73 52 65  ONLY;.      isRe
1062e 61 64 6f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 20  adonly = 1;.    
1062f 20 20 66 64 20 3d 20 72 6f 62 75 73 74 5f 6f 70    fd = robust_op
10630 65 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c  en(zName, openFl
10631 61 67 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a  ags, openMode);.
10632 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66 64      }.    if( fd
10633 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  <0 ){.      rc =
10634 20 75 6e 69 78 4c 6f 67 45 72 72 6f 72 28 53 51   unixLogError(SQ
10635 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b  LITE_CANTOPEN_BK
10636 50 54 2c 20 22 6f 70 65 6e 22 2c 20 7a 4e 61 6d  PT, "open", zNam
10637 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f  e);.      goto o
10638 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20  pen_finished;.  
10639 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
1063a 68 69 73 20 70 72 6f 63 65 73 73 20 69 73 20 72  his process is r
1063b 75 6e 6e 69 6e 67 20 61 73 20 72 6f 6f 74 20 61  unning as root a
1063c 6e 64 20 69 66 20 63 72 65 61 74 69 6e 67 20 61  nd if creating a
1063d 20 6e 65 77 20 72 6f 6c 6c 62 61 63 6b 0a 20 20   new rollback.  
1063e 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6f 72 20    ** journal or 
1063f 57 41 4c 20 66 69 6c 65 2c 20 73 65 74 20 74 68  WAL file, set th
10640 65 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 74  e ownership of t
10641 68 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 57 41  he journal or WA
10642 4c 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a 20 74  L to be.    ** t
10643 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 6f  he same as the o
10644 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65  riginal database
10645 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
10646 20 66 6c 61 67 73 20 26 20 28 53 51 4c 49 54 45   flags & (SQLITE
10647 5f 4f 50 45 4e 5f 57 41 4c 7c 53 51 4c 49 54 45  _OPEN_WAL|SQLITE
10648 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e  _OPEN_MAIN_JOURN
10649 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 6f 73 46  AL) ){.      osF
1064a 63 68 6f 77 6e 28 66 64 2c 20 75 69 64 2c 20 67  chown(fd, uid, g
1064b 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  id);.    }.  }. 
1064c 20 61 73 73 65 72 74 28 20 66 64 3e 3d 30 20 29   assert( fd>=0 )
1064d 3b 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67  ;.  if( pOutFlag
1064e 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c  s ){.    *pOutFl
1064f 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 7d  ags = flags;.  }
10650 0a 0a 20 20 69 66 28 20 70 2d 3e 70 55 6e 75 73  ..  if( p->pUnus
10651 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 70 55 6e  ed ){.    p->pUn
10652 75 73 65 64 2d 3e 66 64 20 3d 20 66 64 3b 0a 20  used->fd = fd;. 
10653 20 20 20 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66     p->pUnused->f
10654 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20  lags = flags;.  
10655 7d 0a 0a 20 20 69 66 28 20 69 73 44 65 6c 65 74  }..  if( isDelet
10656 65 20 29 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f  e ){.#if OS_VXWO
10657 52 4b 53 0a 20 20 20 20 7a 50 61 74 68 20 3d 20  RKS.    zPath = 
10658 7a 4e 61 6d 65 3b 0a 23 65 6c 73 65 0a 20 20 20  zName;.#else.   
10659 20 6f 73 55 6e 6c 69 6e 6b 28 7a 4e 61 6d 65 29   osUnlink(zName)
1065a 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 69 66  ;.#endif.  }.#if
1065b 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
1065c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 65  OCKING_STYLE.  e
1065d 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 6f 70 65 6e  lse{.    p->open
1065e 46 6c 61 67 73 20 3d 20 6f 70 65 6e 46 6c 61 67  Flags = openFlag
1065f 73 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  s;.  }.#endif.. 
10660 20 6e 6f 4c 6f 63 6b 20 3d 20 65 54 79 70 65 21   noLock = eType!
10661 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  =SQLITE_OPEN_MAI
10662 4e 5f 44 42 3b 0a 0a 20 20 0a 23 69 66 20 64 65  N_DB;..  .#if de
10663 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
10664 20 7c 7c 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   || SQLITE_ENABL
10665 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
10666 20 20 69 66 28 20 66 73 74 61 74 66 73 28 66 64    if( fstatfs(fd
10667 2c 20 26 66 73 49 6e 66 6f 29 20 3d 3d 20 2d 31  , &fsInfo) == -1
10668 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69   ){.    ((unixFi
10669 6c 65 2a 29 70 46 69 6c 65 29 2d 3e 6c 61 73 74  le*)pFile)->last
1066a 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
1066b 20 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28     robust_close(
1066c 70 2c 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29  p, fd, __LINE__)
1066d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
1066e 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53  ITE_IOERR_ACCESS
1066f 3b 0a 20 20 7d 0a 20 20 69 66 20 28 30 20 3d 3d  ;.  }.  if (0 ==
10670 20 73 74 72 6e 63 6d 70 28 22 6d 73 64 6f 73 22   strncmp("msdos"
10671 2c 20 66 73 49 6e 66 6f 2e 66 5f 66 73 74 79 70  , fsInfo.f_fstyp
10672 65 6e 61 6d 65 2c 20 35 29 29 20 7b 0a 20 20 20  ename, 5)) {.   
10673 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69   ((unixFile*)pFi
10674 6c 65 29 2d 3e 66 73 46 6c 61 67 73 20 7c 3d 20  le)->fsFlags |= 
10675 53 51 4c 49 54 45 5f 46 53 46 4c 41 47 53 5f 49  SQLITE_FSFLAGS_I
10676 53 5f 4d 53 44 4f 53 3b 0a 20 20 7d 0a 23 65 6e  S_MSDOS;.  }.#en
10677 64 69 66 0a 0a 20 20 2f 2a 20 53 65 74 20 75 70  dif..  /* Set up
10678 20 61 70 70 72 6f 70 72 69 61 74 65 20 63 74 72   appropriate ctr
10679 6c 46 6c 61 67 73 20 2a 2f 0a 20 20 69 66 28 20  lFlags */.  if( 
1067a 69 73 44 65 6c 65 74 65 20 29 20 20 20 20 20 20  isDelete )      
1067b 20 20 20 20 20 20 20 20 20 20 63 74 72 6c 46 6c            ctrlFl
1067c 61 67 73 20 7c 3d 20 55 4e 49 58 46 49 4c 45 5f  ags |= UNIXFILE_
1067d 44 45 4c 45 54 45 3b 0a 20 20 69 66 28 20 69 73  DELETE;.  if( is
1067e 52 65 61 64 6f 6e 6c 79 20 29 20 20 20 20 20 20  Readonly )      
1067f 20 20 20 20 20 20 20 20 63 74 72 6c 46 6c 61 67          ctrlFlag
10680 73 20 7c 3d 20 55 4e 49 58 46 49 4c 45 5f 52 44  s |= UNIXFILE_RD
10681 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 6e 6f 4c 6f  ONLY;.  if( noLo
10682 63 6b 20 29 20 20 20 20 20 20 20 20 20 20 20 20  ck )            
10683 20 20 20 20 20 20 63 74 72 6c 46 6c 61 67 73 20        ctrlFlags 
10684 7c 3d 20 55 4e 49 58 46 49 4c 45 5f 4e 4f 4c 4f  |= UNIXFILE_NOLO
10685 43 4b 3b 0a 20 20 69 66 28 20 73 79 6e 63 44 69  CK;.  if( syncDi
10686 72 20 29 20 20 20 20 20 20 20 20 20 20 20 20 20  r )             
10687 20 20 20 20 63 74 72 6c 46 6c 61 67 73 20 7c 3d      ctrlFlags |=
10688 20 55 4e 49 58 46 49 4c 45 5f 44 49 52 53 59 4e   UNIXFILE_DIRSYN
10689 43 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26  C;.  if( flags &
1068a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49   SQLITE_OPEN_URI
1068b 20 29 20 63 74 72 6c 46 6c 61 67 73 20 7c 3d 20   ) ctrlFlags |= 
1068c 55 4e 49 58 46 49 4c 45 5f 55 52 49 3b 0a 0a 23  UNIXFILE_URI;..#
1068d 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  if SQLITE_ENABLE
1068e 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 23  _LOCKING_STYLE.#
1068f 69 66 20 53 51 4c 49 54 45 5f 50 52 45 46 45 52  if SQLITE_PREFER
10690 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 0a 20  _PROXY_LOCKING. 
10691 20 69 73 41 75 74 6f 50 72 6f 78 79 20 3d 20 31   isAutoProxy = 1
10692 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 69  ;.#endif.  if( i
10693 73 41 75 74 6f 50 72 6f 78 79 20 26 26 20 28 7a  sAutoProxy && (z
10694 50 61 74 68 21 3d 4e 55 4c 4c 29 20 26 26 20 28  Path!=NULL) && (
10695 21 6e 6f 4c 6f 63 6b 29 20 26 26 20 70 56 66 73  !noLock) && pVfs
10696 2d 3e 78 4f 70 65 6e 20 29 7b 0a 20 20 20 20 63  ->xOpen ){.    c
10697 68 61 72 20 2a 65 6e 76 66 6f 72 63 65 20 3d 20  har *envforce = 
10698 67 65 74 65 6e 76 28 22 53 51 4c 49 54 45 5f 46  getenv("SQLITE_F
10699 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49  ORCE_PROXY_LOCKI
1069a 4e 47 22 29 3b 0a 20 20 20 20 69 6e 74 20 75 73  NG");.    int us
1069b 65 50 72 6f 78 79 20 3d 20 30 3b 0a 0a 20 20 20  eProxy = 0;..   
1069c 20 2f 2a 20 53 51 4c 49 54 45 5f 46 4f 52 43 45   /* SQLITE_FORCE
1069d 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 3d 3d  _PROXY_LOCKING==
1069e 31 20 6d 65 61 6e 73 20 66 6f 72 63 65 20 61 6c  1 means force al
1069f 77 61 79 73 20 75 73 65 20 70 72 6f 78 79 2c 20  ways use proxy, 
106a0 30 20 6d 65 61 6e 73 20 0a 20 20 20 20 2a 2a 20  0 means .    ** 
106a1 6e 65 76 65 72 20 75 73 65 20 70 72 6f 78 79 2c  never use proxy,
106a2 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 75 73 65 20   NULL means use 
106a3 70 72 6f 78 79 20 66 6f 72 20 6e 6f 6e 2d 6c 6f  proxy for non-lo
106a4 63 61 6c 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20  cal files only. 
106a5 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 6e 76 66   */.    if( envf
106a6 6f 72 63 65 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20  orce!=NULL ){.  
106a7 20 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 61      useProxy = a
106a8 74 6f 69 28 65 6e 76 66 6f 72 63 65 29 3e 30 3b  toi(envforce)>0;
106a9 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
106aa 20 20 69 66 28 20 73 74 61 74 66 73 28 7a 50 61    if( statfs(zPa
106ab 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 3d 3d 20  th, &fsInfo) == 
106ac 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  -1 ){.        /*
106ad 20 49 6e 20 74 68 65 6f 72 79 2c 20 74 68 65 20   In theory, the 
106ae 63 6c 6f 73 65 28 66 64 29 20 63 61 6c 6c 20 69  close(fd) call i
106af 73 20 73 75 62 2d 6f 70 74 69 6d 61 6c 2e 20 49  s sub-optimal. I
106b0 66 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e 65  f the file opene
106b1 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 69 74  d.        ** wit
106b2 68 20 66 64 20 69 73 20 61 20 64 61 74 61 62 61  h fd is a databa
106b3 73 65 20 66 69 6c 65 2c 20 61 6e 64 20 74 68 65  se file, and the
106b4 72 65 20 61 72 65 20 6f 74 68 65 72 20 63 6f 6e  re are other con
106b5 6e 65 63 74 69 6f 6e 73 20 6f 70 65 6e 0a 20 20  nections open.  
106b6 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 61 74        ** on that
106b7 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 20 63   file that are c
106b8 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67  urrently holding
106b9 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20   advisory locks 
106ba 6f 6e 20 69 74 2c 0a 20 20 20 20 20 20 20 20 2a  on it,.        *
106bb 2a 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 20  * then the call 
106bc 74 6f 20 63 6c 6f 73 65 28 29 20 77 69 6c 6c 20  to close() will 
106bd 63 61 6e 63 65 6c 20 74 68 6f 73 65 20 6c 6f 63  cancel those loc
106be 6b 73 2e 20 49 6e 20 70 72 61 63 74 69 63 65 2c  ks. In practice,
106bf 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65 27 72  .        ** we'r
106c0 65 20 61 73 73 75 6d 69 6e 67 20 74 68 61 74 20  e assuming that 
106c1 73 74 61 74 66 73 28 29 20 64 6f 65 73 6e 27 74  statfs() doesn't
106c2 20 66 61 69 6c 20 76 65 72 79 20 6f 66 74 65 6e   fail very often
106c3 2e 20 41 74 20 6c 65 61 73 74 0a 20 20 20 20 20  . At least.     
106c4 20 20 20 2a 2a 20 6e 6f 74 20 77 68 69 6c 65 20     ** not while 
106c5 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 63 72  other file descr
106c6 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 62 79  iptors opened by
106c7 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73   the same proces
106c8 73 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  s on.        ** 
106c9 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 61 72  the same file ar
106ca 65 20 77 6f 72 6b 69 6e 67 2e 20 20 2a 2f 0a 20  e working.  */. 
106cb 20 20 20 20 20 20 20 70 2d 3e 6c 61 73 74 45 72         p->lastEr
106cc 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
106cd 20 20 20 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73       robust_clos
106ce 65 28 70 2c 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f  e(p, fd, __LINE_
106cf 5f 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  _);.        rc =
106d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43   SQLITE_IOERR_AC
106d1 43 45 53 53 3b 0a 20 20 20 20 20 20 20 20 67 6f  CESS;.        go
106d2 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64  to open_finished
106d3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
106d4 75 73 65 50 72 6f 78 79 20 3d 20 21 28 66 73 49  useProxy = !(fsI
106d5 6e 66 6f 2e 66 5f 66 6c 61 67 73 26 4d 4e 54 5f  nfo.f_flags&MNT_
106d6 4c 4f 43 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20  LOCAL);.    }.  
106d7 20 20 69 66 28 20 75 73 65 50 72 6f 78 79 20 29    if( useProxy )
106d8 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 6c  {.      rc = fil
106d9 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 73  lInUnixFile(pVfs
106da 2c 20 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61  , fd, pFile, zPa
106db 74 68 2c 20 63 74 72 6c 46 6c 61 67 73 29 3b 0a  th, ctrlFlags);.
106dc 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
106dd 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
106de 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54 72 61     rc = proxyTra
106df 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 28  nsformUnixFile((
106e0 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 6c 65 2c  unixFile*)pFile,
106e1 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 20 20 20 20   ":auto:");.    
106e2 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
106e3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
106e4 20 20 20 2f 2a 20 55 73 65 20 75 6e 69 78 43 6c     /* Use unixCl
106e5 6f 73 65 20 74 6f 20 63 6c 65 61 6e 20 75 70 20  ose to clean up 
106e6 74 68 65 20 72 65 73 6f 75 72 63 65 73 20 61 64  the resources ad
106e7 64 65 64 20 69 6e 20 66 69 6c 6c 49 6e 55 6e 69  ded in fillInUni
106e8 78 46 69 6c 65 20 0a 20 20 20 20 20 20 20 20 20  xFile .         
106e9 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 20 61 6c   ** and clear al
106ea 6c 20 74 68 65 20 73 74 72 75 63 74 75 72 65 27  l the structure'
106eb 73 20 72 65 66 65 72 65 6e 63 65 73 2e 20 20 53  s references.  S
106ec 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 0a 20 20  pecifically, .  
106ed 20 20 20 20 20 20 20 20 2a 2a 20 70 46 69 6c 65          ** pFile
106ee 2d 3e 70 4d 65 74 68 6f 64 73 20 77 69 6c 6c 20  ->pMethods will 
106ef 62 65 20 4e 55 4c 4c 20 73 6f 20 73 71 6c 69 74  be NULL so sqlit
106f0 65 33 4f 73 43 6c 6f 73 65 20 77 69 6c 6c 20 62  e3OsClose will b
106f1 65 20 61 20 6e 6f 2d 6f 70 20 0a 20 20 20 20 20  e a no-op .     
106f2 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
106f3 20 20 75 6e 69 78 43 6c 6f 73 65 28 70 46 69 6c    unixClose(pFil
106f4 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  e);.          re
106f5 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
106f6 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
106f7 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73   goto open_finis
106f8 68 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  hed;.    }.  }.#
106f9 65 6e 64 69 66 0a 20 20 0a 20 20 72 63 20 3d 20  endif.  .  rc = 
106fa 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70  fillInUnixFile(p
106fb 56 66 73 2c 20 66 64 2c 20 70 46 69 6c 65 2c 20  Vfs, fd, pFile, 
106fc 7a 50 61 74 68 2c 20 63 74 72 6c 46 6c 61 67 73  zPath, ctrlFlags
106fd 29 3b 0a 0a 6f 70 65 6e 5f 66 69 6e 69 73 68 65  );..open_finishe
106fe 64 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  d:.  if( rc!=SQL
106ff 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
10700 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 70 55  lite3_free(p->pU
10701 6e 75 73 65 64 29 3b 0a 20 20 7d 0a 20 20 72 65  nused);.  }.  re
10702 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
10703 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 66 69  ** Delete the fi
10704 6c 65 20 61 74 20 7a 50 61 74 68 2e 20 49 66 20  le at zPath. If 
10705 74 68 65 20 64 69 72 53 79 6e 63 20 61 72 67 75  the dirSync argu
10706 6d 65 6e 74 20 69 73 20 74 72 75 65 2c 20 66 73  ment is true, fs
10707 79 6e 63 28 29 0a 2a 2a 20 74 68 65 20 64 69 72  ync().** the dir
10708 65 63 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c  ectory after del
10709 65 74 69 6e 67 20 74 68 65 20 66 69 6c 65 2e 0a  eting the file..
1070a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
1070b 69 78 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69  ixDelete(.  sqli
1070c 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64  te3_vfs *NotUsed
1070d 2c 20 20 20 20 20 2f 2a 20 56 46 53 20 63 6f 6e  ,     /* VFS con
1070e 74 61 69 6e 69 6e 67 20 74 68 69 73 20 61 73 20  taining this as 
1070f 74 68 65 20 78 44 65 6c 65 74 65 20 6d 65 74 68  the xDelete meth
10710 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  od */.  const ch
10711 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20  ar *zPath,      
10712 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c    /* Name of fil
10713 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20  e to be deleted 
10714 2a 2f 0a 20 20 69 6e 74 20 64 69 72 53 79 6e 63  */.  int dirSync
10715 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10716 2a 20 49 66 20 74 72 75 65 2c 20 66 73 79 6e 63  * If true, fsync
10717 28 29 20 64 69 72 65 63 74 6f 72 79 20 61 66 74  () directory aft
10718 65 72 20 64 65 6c 65 74 69 6e 67 20 66 69 6c 65  er deleting file
10719 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20   */.){.  int rc 
1071a 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 55  = SQLITE_OK;.  U
1071b 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
1071c 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 6d 75  NotUsed);.  Simu
1071d 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75  lateIOError(retu
1071e 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
1071f 44 45 4c 45 54 45 29 3b 0a 20 20 69 66 28 20 6f  DELETE);.  if( o
10720 73 55 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 3d 3d  sUnlink(zPath)==
10721 28 2d 31 29 20 29 7b 0a 20 20 20 20 69 66 28 20  (-1) ){.    if( 
10722 65 72 72 6e 6f 3d 3d 45 4e 4f 45 4e 54 20 29 7b  errno==ENOENT ){
10723 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
10724 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 5f  TE_IOERR_DELETE_
10725 4e 4f 45 4e 54 3b 0a 20 20 20 20 7d 65 6c 73 65  NOENT;.    }else
10726 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69  {.      rc = uni
10727 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45  xLogError(SQLITE
10728 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 2c 20 22  _IOERR_DELETE, "
10729 75 6e 6c 69 6e 6b 22 2c 20 7a 50 61 74 68 29 3b  unlink", zPath);
1072a 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
1072b 6e 20 72 63 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  n rc;.  }.#ifnde
1072c 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45  f SQLITE_DISABLE
1072d 5f 44 49 52 53 59 4e 43 0a 20 20 69 66 28 20 28  _DIRSYNC.  if( (
1072e 64 69 72 53 79 6e 63 20 26 20 31 29 21 3d 30 20  dirSync & 1)!=0 
1072f 29 7b 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20  ){.    int fd;. 
10730 20 20 20 72 63 20 3d 20 6f 73 4f 70 65 6e 44 69     rc = osOpenDi
10731 72 65 63 74 6f 72 79 28 7a 50 61 74 68 2c 20 26  rectory(zPath, &
10732 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  fd);.    if( rc=
10733 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69  =SQLITE_OK ){.#i
10734 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20  f OS_VXWORKS.   
10735 20 20 20 69 66 28 20 66 73 79 6e 63 28 66 64 29     if( fsync(fd)
10736 3d 3d 2d 31 20 29 0a 23 65 6c 73 65 0a 20 20 20  ==-1 ).#else.   
10737 20 20 20 69 66 28 20 66 73 79 6e 63 28 66 64 29     if( fsync(fd)
10738 20 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   ).#endif.      
10739 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 75  {.        rc = u
1073a 6e 69 78 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49  nixLogError(SQLI
1073b 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59  TE_IOERR_DIR_FSY
1073c 4e 43 2c 20 22 66 73 79 6e 63 22 2c 20 7a 50 61  NC, "fsync", zPa
1073d 74 68 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  th);.      }.   
1073e 20 20 20 72 6f 62 75 73 74 5f 63 6c 6f 73 65 28     robust_close(
1073f 30 2c 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29  0, fd, __LINE__)
10740 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
10741 72 63 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f  rc==SQLITE_CANTO
10742 50 45 4e 20 29 7b 0a 20 20 20 20 20 20 72 63 20  PEN ){.      rc 
10743 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
10744 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20   }.  }.#endif.  
10745 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
10746 0a 2a 2a 20 54 65 73 74 20 74 68 65 20 65 78 69  .** Test the exi
10747 73 74 61 6e 63 65 20 6f 66 20 6f 72 20 61 63 63  stance of or acc
10748 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 73 20  ess permissions 
10749 6f 66 20 66 69 6c 65 20 7a 50 61 74 68 2e 20 54  of file zPath. T
1074a 68 65 0a 2a 2a 20 74 65 73 74 20 70 65 72 66 6f  he.** test perfo
1074b 72 6d 65 64 20 64 65 70 65 6e 64 73 20 6f 6e 20  rmed depends on 
1074c 74 68 65 20 76 61 6c 75 65 20 6f 66 20 66 6c 61  the value of fla
1074d 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51  gs:.**.**     SQ
1074e 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53  LITE_ACCESS_EXIS
1074f 54 53 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20  TS: Return 1 if 
10750 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 0a  the file exists.
10751 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43  **     SQLITE_AC
10752 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 20  CESS_READWRITE: 
10753 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
10754 66 69 6c 65 20 69 73 20 72 65 61 64 20 61 6e 64  file is read and
10755 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 20 20   writable..**   
10756 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f    SQLITE_ACCESS_
10757 52 45 41 44 4f 4e 4c 59 3a 20 52 65 74 75 72 6e  READONLY: Return
10758 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 69   1 if the file i
10759 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a  s readable..**.*
1075a 2a 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75  * Otherwise retu
1075b 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rn 0..*/.static 
1075c 69 6e 74 20 75 6e 69 78 41 63 63 65 73 73 28 0a  int unixAccess(.
1075d 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e    sqlite3_vfs *N
1075e 6f 74 55 73 65 64 2c 20 20 20 2f 2a 20 54 68 65  otUsed,   /* The
1075f 20 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20   VFS containing 
10760 74 68 69 73 20 78 41 63 63 65 73 73 20 6d 65 74  this xAccess met
10761 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  hod */.  const c
10762 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20  har *zPath,     
10763 20 2f 2a 20 50 61 74 68 20 6f 66 20 74 68 65 20   /* Path of the 
10764 66 69 6c 65 20 74 6f 20 65 78 61 6d 69 6e 65 20  file to examine 
10765 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  */.  int flags, 
10766 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10767 57 68 61 74 20 64 6f 20 77 65 20 77 61 6e 74 20  What do we want 
10768 74 6f 20 6c 65 61 72 6e 20 61 62 6f 75 74 20 74  to learn about t
10769 68 65 20 7a 50 61 74 68 20 66 69 6c 65 3f 20 2a  he zPath file? *
1076a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  /.  int *pResOut
1076b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
1076c 72 69 74 65 20 72 65 73 75 6c 74 20 62 6f 6f 6c  rite result bool
1076d 65 61 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ean here */.){. 
1076e 20 69 6e 74 20 61 6d 6f 64 65 20 3d 20 30 3b 0a   int amode = 0;.
1076f 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
10770 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53  ER(NotUsed);.  S
10771 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
10772 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
10773 45 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0a 20  ERR_ACCESS; );. 
10774 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29   switch( flags )
10775 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
10776 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a  E_ACCESS_EXISTS:
10777 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 46  .      amode = F
10778 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  _OK;.      break
10779 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
1077a 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49  E_ACCESS_READWRI
1077b 54 45 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20  TE:.      amode 
1077c 3d 20 57 5f 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20 20  = W_OK|R_OK;.   
1077d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
1077e 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  se SQLITE_ACCESS
1077f 5f 52 45 41 44 3a 0a 20 20 20 20 20 20 61 6d 6f  _READ:.      amo
10780 64 65 20 3d 20 52 5f 4f 4b 3b 0a 20 20 20 20 20  de = R_OK;.     
10781 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65 66   break;..    def
10782 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65  ault:.      asse
10783 72 74 28 21 22 49 6e 76 61 6c 69 64 20 66 6c 61  rt(!"Invalid fla
10784 67 73 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20  gs argument");. 
10785 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20   }.  *pResOut = 
10786 28 6f 73 41 63 63 65 73 73 28 7a 50 61 74 68 2c  (osAccess(zPath,
10787 20 61 6d 6f 64 65 29 3d 3d 30 29 3b 0a 20 20 69   amode)==0);.  i
10788 66 28 20 66 6c 61 67 73 3d 3d 53 51 4c 49 54 45  f( flags==SQLITE
10789 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 20 26  _ACCESS_EXISTS &
1078a 26 20 2a 70 52 65 73 4f 75 74 20 29 7b 0a 20 20  & *pResOut ){.  
1078b 20 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75    struct stat bu
1078c 66 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 6f 73  f;.    if( 0==os
1078d 53 74 61 74 28 7a 50 61 74 68 2c 20 26 62 75 66  Stat(zPath, &buf
1078e 29 20 26 26 20 62 75 66 2e 73 74 5f 73 69 7a 65  ) && buf.st_size
1078f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 52  ==0 ){.      *pR
10790 65 73 4f 75 74 20 3d 20 30 3b 0a 20 20 20 20 7d  esOut = 0;.    }
10791 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
10792 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
10793 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69  ** Turn a relati
10794 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f  ve pathname into
10795 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65   a full pathname
10796 2e 20 54 68 65 20 72 65 6c 61 74 69 76 65 20 70  . The relative p
10797 61 74 68 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64  ath.** is stored
10798 20 61 73 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e   as a nul-termin
10799 61 74 65 64 20 73 74 72 69 6e 67 20 69 6e 20 74  ated string in t
1079a 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65  he buffer pointe
1079b 64 20 74 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68  d to by.** zPath
1079c 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f  . .**.** zOut po
1079d 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 72  ints to a buffer
1079e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 71 6c   of at least sql
1079f 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e  ite3_vfs.mxPathn
107a0 61 6d 65 20 62 79 74 65 73 20 0a 2a 2a 20 28 69  ame bytes .** (i
107a1 6e 20 74 68 69 73 20 63 61 73 65 2c 20 4d 41 58  n this case, MAX
107a2 5f 50 41 54 48 4e 41 4d 45 20 62 79 74 65 73 29  _PATHNAME bytes)
107a3 2e 20 54 68 65 20 66 75 6c 6c 2d 70 61 74 68 20  . The full-path 
107a4 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a  is written to.**
107a5 20 74 68 69 73 20 62 75 66 66 65 72 20 62 65 66   this buffer bef
107a6 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
107a7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
107a8 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20  xFullPathname(. 
107a9 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
107aa 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  fs,            /
107ab 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73  * Pointer to vfs
107ac 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e   object */.  con
107ad 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20  st char *zPath, 
107ae 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
107af 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20  ssibly relative 
107b0 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20  input path */.  
107b1 69 6e 74 20 6e 4f 75 74 2c 20 20 20 20 20 20 20  int nOut,       
107b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
107b3 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20   Size of output 
107b4 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20  buffer in bytes 
107b5 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 20  */.  char *zOut 
107b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
107b7 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66     /* Output buf
107b8 66 65 72 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20  fer */.){..  /* 
107b9 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75  It's odd to simu
107ba 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72  late an io-error
107bb 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c   here, but reall
107bc 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20  y this is just. 
107bd 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f   ** using the io
107be 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75  -error infrastru
107bf 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 74 68  cture to test th
107c0 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65  at SQLite handle
107c1 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63  s this.  ** func
107c2 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68  tion failing. Th
107c3 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c  is function coul
107c4 64 20 66 61 69 6c 20 69 66 2c 20 66 6f 72 20 65  d fail if, for e
107c5 78 61 6d 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a  xample, the.  **
107c6 20 63 75 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67   current working
107c7 20 64 69 72 65 63 74 6f 72 79 20 68 61 73 20 62   directory has b
107c8 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20  een unlinked..  
107c9 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  */.  SimulateIOE
107ca 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
107cb 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20  ITE_ERROR );..  
107cc 61 73 73 65 72 74 28 20 70 56 66 73 2d 3e 6d 78  assert( pVfs->mx
107cd 50 61 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41  Pathname==MAX_PA
107ce 54 48 4e 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53  THNAME );.  UNUS
107cf 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66  ED_PARAMETER(pVf
107d0 73 29 3b 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74  s);..  zOut[nOut
107d1 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66  -1] = '\0';.  if
107d2 28 20 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20  ( zPath[0]=='/' 
107d3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ){.    sqlite3_s
107d4 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f  nprintf(nOut, zO
107d5 75 74 2c 20 22 25 73 22 2c 20 7a 50 61 74 68 29  ut, "%s", zPath)
107d6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
107d7 6e 74 20 6e 43 77 64 3b 0a 20 20 20 20 69 66 28  nt nCwd;.    if(
107d8 20 6f 73 47 65 74 63 77 64 28 7a 4f 75 74 2c 20   osGetcwd(zOut, 
107d9 6e 4f 75 74 2d 31 29 3d 3d 30 20 29 7b 0a 20 20  nOut-1)==0 ){.  
107da 20 20 20 20 72 65 74 75 72 6e 20 75 6e 69 78 4c      return unixL
107db 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 43  ogError(SQLITE_C
107dc 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 2c 20 22 67  ANTOPEN_BKPT, "g
107dd 65 74 63 77 64 22 2c 20 7a 50 61 74 68 29 3b 0a  etcwd", zPath);.
107de 20 20 20 20 7d 0a 20 20 20 20 6e 43 77 64 20 3d      }.    nCwd =
107df 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 4f 75   (int)strlen(zOu
107e0 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  t);.    sqlite3_
107e1 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2d 6e 43  snprintf(nOut-nC
107e2 77 64 2c 20 26 7a 4f 75 74 5b 6e 43 77 64 5d 2c  wd, &zOut[nCwd],
107e3 20 22 2f 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a   "/%s", zPath);.
107e4 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
107e5 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e  ITE_OK;.}...#ifn
107e6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
107e7 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f  LOAD_EXTENSION./
107e8 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20  *.** Interfaces 
107e9 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68  for opening a sh
107ea 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69  ared library, fi
107eb 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e  nding entry poin
107ec 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65  ts.** within the
107ed 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c   shared library,
107ee 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65   and closing the
107ef 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e   shared library.
107f0 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 64 6c  .*/.#include <dl
107f1 66 63 6e 2e 68 3e 0a 73 74 61 74 69 63 20 76 6f  fcn.h>.static vo
107f2 69 64 20 2a 75 6e 69 78 44 6c 4f 70 65 6e 28 73  id *unixDlOpen(s
107f3 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55  qlite3_vfs *NotU
107f4 73 65 64 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  sed, const char 
107f5 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 55  *zFilename){.  U
107f6 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
107f7 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75  NotUsed);.  retu
107f8 72 6e 20 64 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e  rn dlopen(zFilen
107f9 61 6d 65 2c 20 52 54 4c 44 5f 4e 4f 57 20 7c 20  ame, RTLD_NOW | 
107fa 52 54 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a  RTLD_GLOBAL);.}.
107fb 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61  ./*.** SQLite ca
107fc 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f  lls this functio
107fd 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66  n immediately af
107fe 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 75 6e  ter a call to un
107ff 69 78 44 6c 53 79 6d 28 29 20 6f 72 0a 2a 2a 20  ixDlSym() or.** 
10800 75 6e 69 78 44 6c 4f 70 65 6e 28 29 20 66 61 69  unixDlOpen() fai
10801 6c 73 20 28 72 65 74 75 72 6e 73 20 61 20 6e 75  ls (returns a nu
10802 6c 6c 20 70 6f 69 6e 74 65 72 29 2e 20 49 66 20  ll pointer). If 
10803 61 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20  a more detailed 
10804 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65  error.** message
10805 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 69   is available, i
10806 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  t is written to 
10807 7a 42 75 66 4f 75 74 2e 20 49 66 20 6e 6f 20 65  zBufOut. If no e
10808 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20  rror message.** 
10809 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 7a 42  is available, zB
1080a 75 66 4f 75 74 20 69 73 20 6c 65 66 74 20 75 6e  ufOut is left un
1080b 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 53 51 4c  modified and SQL
1080c 69 74 65 20 75 73 65 73 20 61 20 64 65 66 61 75  ite uses a defau
1080d 6c 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73  lt.** error mess
1080e 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  age..*/.static v
1080f 6f 69 64 20 75 6e 69 78 44 6c 45 72 72 6f 72 28  oid unixDlError(
10810 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74  sqlite3_vfs *Not
10811 55 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 2c 20  Used, int nBuf, 
10812 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a  char *zBufOut){.
10813 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45    const char *zE
10814 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  rr;.  UNUSED_PAR
10815 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
10816 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65  .  unixEnterMute
10817 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 64 6c  x();.  zErr = dl
10818 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20 7a  error();.  if( z
10819 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Err ){.    sqlit
1081a 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66  e3_snprintf(nBuf
1081b 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 22 2c  , zBufOut, "%s",
1081c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 75 6e   zErr);.  }.  un
1081d 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  ixLeaveMutex();.
1081e 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 28 2a  }.static void (*
1081f 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 74 65  unixDlSym(sqlite
10820 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20  3_vfs *NotUsed, 
10821 76 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74 20 63  void *p, const c
10822 68 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29  har*zSym))(void)
10823 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47 43 43  {.  /* .  ** GCC
10824 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d   with -pedantic-
10825 65 72 72 6f 72 73 20 73 61 79 73 20 74 68 61 74  errors says that
10826 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 61 6c   C90 does not al
10827 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f 20 62  low a void* to b
10828 65 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e 74 6f  e.  ** cast into
10829 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
1082a 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 20 79  function.  And y
1082b 65 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 64  et the library d
1082c 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 0a 20  lsym() routine. 
1082d 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76 6f   ** returns a vo
1082e 69 64 2a 20 77 68 69 63 68 20 69 73 20 72 65 61  id* which is rea
1082f 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  lly a pointer to
10830 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 6f   a function.  So
10831 20 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a 2a 20   how do we.  ** 
10832 75 73 65 20 64 6c 73 79 6d 28 29 20 77 69 74 68  use dlsym() with
10833 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f 72   -pedantic-error
10834 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72  s?.  **.  ** Var
10835 69 61 62 6c 65 20 78 20 62 65 6c 6f 77 20 69 73  iable x below is
10836 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 61   defined to be a
10837 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75   pointer to a fu
10838 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a 20 20  nction taking.  
10839 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 76 6f  ** parameters vo
1083a 69 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20 63 68  id* and const ch
1083b 61 72 2a 20 61 6e 64 20 72 65 74 75 72 6e 69 6e  ar* and returnin
1083c 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  g a pointer to a
1083d 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20   function..  ** 
1083e 57 65 20 69 6e 69 74 69 61 6c 69 7a 65 20 78 20  We initialize x 
1083f 62 79 20 61 73 73 69 67 6e 69 6e 67 20 69 74 20  by assigning it 
10840 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
10841 20 64 6c 73 79 6d 28 29 20 66 75 6e 63 74 69 6f   dlsym() functio
10842 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20 61 73  n..  ** (That as
10843 73 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65  signment require
10844 73 20 61 20 63 61 73 74 2e 29 20 20 54 68 65 6e  s a cast.)  Then
10845 20 77 65 20 63 61 6c 6c 20 74 68 65 20 66 75 6e   we call the fun
10846 63 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a 2a 20  ction that.  ** 
10847 78 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 0a 20  x points to.  . 
10848 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 77 6f   **.  ** This wo
10849 72 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75 6e 6c  rk-around is unl
1084a 69 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20 63 6f  ikely to work co
1084b 72 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79 20 73  rrectly on any s
1084c 79 73 74 65 6d 20 77 68 65 72 65 0a 20 20 2a 2a  ystem where.  **
1084d 20 79 6f 75 20 72 65 61 6c 6c 79 20 63 61 6e 6e   you really cann
1084e 6f 74 20 63 61 73 74 20 61 20 66 75 6e 63 74 69  ot cast a functi
1084f 6f 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20  on pointer into 
10850 76 6f 69 64 2a 2e 20 20 42 75 74 20 74 68 65 6e  void*.  But then
10851 2c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f 74  , on the.  ** ot
10852 68 65 72 20 68 61 6e 64 2c 20 64 6c 73 79 6d 28  her hand, dlsym(
10853 29 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20  ) will not work 
10854 6f 6e 20 73 75 63 68 20 61 20 73 79 73 74 65 6d  on such a system
10855 20 65 69 74 68 65 72 2c 20 73 6f 20 77 65 20 68   either, so we h
10856 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72 65 61  ave.  ** not rea
10857 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68 69 6e  lly lost anythin
10858 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 20 28  g..  */.  void (
10859 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f 6e 73  *(*x)(void*,cons
1085a 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29 3b  t char*))(void);
1085b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
1085c 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
1085d 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29 28 76  x = (void(*(*)(v
1085e 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  oid*,const char*
1085f 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d 3b 0a  ))(void))dlsym;.
10860 20 20 72 65 74 75 72 6e 20 28 2a 78 29 28 70 2c    return (*x)(p,
10861 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74 69 63   zSym);.}.static
10862 20 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c 6f 73   void unixDlClos
10863 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e  e(sqlite3_vfs *N
10864 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 48  otUsed, void *pH
10865 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 44  andle){.  UNUSED
10866 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
10867 65 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 28 70  ed);.  dlclose(p
10868 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65  Handle);.}.#else
10869 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d   /* if SQLITE_OM
1086a 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
1086b 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f  N is defined: */
1086c 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44  .  #define unixD
1086d 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66 69  lOpen  0.  #defi
1086e 6e 65 20 75 6e 69 78 44 6c 45 72 72 6f 72 20 30  ne unixDlError 0
1086f 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44  .  #define unixD
10870 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66 69  lSym   0.  #defi
10871 6e 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65 20 30  ne unixDlClose 0
10872 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
10873 72 69 74 65 20 6e 42 75 66 20 62 79 74 65 73 20  rite nBuf bytes 
10874 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 20 74  of random data t
10875 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 62  o the supplied b
10876 75 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a 73  uffer zBuf..*/.s
10877 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 52 61  tatic int unixRa
10878 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33  ndomness(sqlite3
10879 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69  _vfs *NotUsed, i
1087a 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
1087b 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  Buf){.  UNUSED_P
1087c 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
1087d 29 3b 0a 20 20 61 73 73 65 72 74 28 28 73 69 7a  );.  assert((siz
1087e 65 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a 65 6f  e_t)nBuf>=(sizeo
1087f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f 66  f(time_t)+sizeof
10880 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20 57  (int)));..  /* W
10881 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74 69 61  e have to initia
10882 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 72 65  lize zBuf to pre
10883 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 66 72  vent valgrind fr
10884 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 20 2a  om reporting.  *
10885 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20 72  * errors.  The r
10886 65 70 6f 72 74 73 20 69 73 73 75 65 64 20 62 79  eports issued by
10887 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20 69 6e   valgrind are in
10888 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77 6f 75  correct - we wou
10889 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 20 74  ld.  ** prefer t
1088a 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65  hat the randomne
1088b 73 73 20 62 65 20 69 6e 63 72 65 61 73 65 64 20  ss be increased 
1088c 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f 66  by making use of
1088d 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69 74   the.  ** uninit
1088e 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20 69 6e  ialized space in
1088f 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61 6c 67   zBuf - but valg
10890 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65 6e 64  rind errors tend
10891 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20 73   to worry.  ** s
10892 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61 74 68  ome users.  Rath
10893 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c 20 69  er than argue, i
10894 74 20 73 65 65 6d 73 20 65 61 73 69 65 72 20 6a  t seems easier j
10895 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a  ust to initializ
10896 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c 65  e.  ** the whole
10897 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c 65 6e   array and silen
10898 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 76 65  ce valgrind, eve
10899 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20  n if that means 
1089a 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73 0a  less randomness.
1089b 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e 64    ** in the rand
1089c 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20 20  om seed..  **.  
1089d 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c  ** When testing,
1089e 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a 42   initializing zB
1089f 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73 20  uf[] to zero is 
108a0 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61 74  all we do.  That
108a1 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61 74   means.  ** that
108a2 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 74   we always use t
108a3 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20 6e  he same random n
108a4 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e 20  umber sequence. 
108a5 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 0a   This makes the.
108a6 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65 61    ** tests repea
108a7 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65  table..  */.  me
108a8 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42  mset(zBuf, 0, nB
108a9 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69 6e 65  uf);.#if !define
108aa 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20  d(SQLITE_TEST). 
108ab 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 2c 20   {.    int pid, 
108ac 66 64 2c 20 67 6f 74 3b 0a 20 20 20 20 66 64 20  fd, got;.    fd 
108ad 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 22 2f  = robust_open("/
108ae 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f  dev/urandom", O_
108af 52 44 4f 4e 4c 59 2c 20 30 29 3b 0a 20 20 20 20  RDONLY, 0);.    
108b0 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20  if( fd<0 ){.    
108b1 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20 20    time_t t;.    
108b2 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20    time(&t);.    
108b3 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20 26    memcpy(zBuf, &
108b4 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20  t, sizeof(t));. 
108b5 20 20 20 20 20 70 69 64 20 3d 20 67 65 74 70 69       pid = getpi
108b6 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70  d();.      memcp
108b7 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66 28 74  y(&zBuf[sizeof(t
108b8 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66  )], &pid, sizeof
108b9 28 70 69 64 29 29 3b 0a 20 20 20 20 20 20 61 73  (pid));.      as
108ba 73 65 72 74 28 20 73 69 7a 65 6f 66 28 74 29 2b  sert( sizeof(t)+
108bb 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d 28 73 69  sizeof(pid)<=(si
108bc 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a 20 20 20  ze_t)nBuf );.   
108bd 20 20 20 6e 42 75 66 20 3d 20 73 69 7a 65 6f 66     nBuf = sizeof
108be 28 74 29 20 2b 20 73 69 7a 65 6f 66 28 70 69 64  (t) + sizeof(pid
108bf 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
108c0 20 20 20 20 64 6f 7b 20 67 6f 74 20 3d 20 6f 73      do{ got = os
108c1 52 65 61 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e  Read(fd, zBuf, n
108c2 42 75 66 29 3b 20 7d 77 68 69 6c 65 28 20 67 6f  Buf); }while( go
108c3 74 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d 45 49  t<0 && errno==EI
108c4 4e 54 52 20 29 3b 0a 20 20 20 20 20 20 72 6f 62  NTR );.      rob
108c5 75 73 74 5f 63 6c 6f 73 65 28 30 2c 20 66 64 2c  ust_close(0, fd,
108c6 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a 20 20 20 20   __LINE__);.    
108c7 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  }.  }.#endif.  r
108c8 65 74 75 72 6e 20 6e 42 75 66 3b 0a 7d 0a 0a 0a  eturn nBuf;.}...
108c9 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20  /*.** Sleep for 
108ca 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20  a little while. 
108cb 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75   Return the amou
108cc 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74  nt of time slept
108cd 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e  ..** The argumen
108ce 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  t is the number 
108cf 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20  of microseconds 
108d0 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70  we want to sleep
108d1 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  ..** The return 
108d2 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d  value is the num
108d3 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f  ber of microseco
108d4 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74  nds of sleep act
108d5 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74  ually.** request
108d6 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65  ed from the unde
108d7 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67  rlying operating
108d8 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65   system, a numbe
108d9 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74  r which.** might
108da 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e   be greater than
108db 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
108dc 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e   argument, but n
108dd 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  ot less.** than 
108de 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  the argument..*/
108df 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
108e0 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66  Sleep(sqlite3_vf
108e1 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20  s *NotUsed, int 
108e2 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 7b 0a 23  microseconds){.#
108e3 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if OS_VXWORKS.  
108e4 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63 20  struct timespec 
108e5 73 70 3b 0a 0a 20 20 73 70 2e 74 76 5f 73 65 63  sp;..  sp.tv_sec
108e6 20 3d 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20   = microseconds 
108e7 2f 20 31 30 30 30 30 30 30 3b 0a 20 20 73 70 2e  / 1000000;.  sp.
108e8 74 76 5f 6e 73 65 63 20 3d 20 28 6d 69 63 72 6f  tv_nsec = (micro
108e9 73 65 63 6f 6e 64 73 20 25 20 31 30 30 30 30 30  seconds % 100000
108ea 30 29 20 2a 20 31 30 30 30 3b 0a 20 20 6e 61 6e  0) * 1000;.  nan
108eb 6f 73 6c 65 65 70 28 26 73 70 2c 20 4e 55 4c 4c  osleep(&sp, NULL
108ec 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
108ed 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
108ee 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65    return microse
108ef 63 6f 6e 64 73 3b 0a 23 65 6c 69 66 20 64 65 66  conds;.#elif def
108f0 69 6e 65 64 28 48 41 56 45 5f 55 53 4c 45 45 50  ined(HAVE_USLEEP
108f1 29 20 26 26 20 48 41 56 45 5f 55 53 4c 45 45 50  ) && HAVE_USLEEP
108f2 0a 20 20 75 73 6c 65 65 70 28 6d 69 63 72 6f 73  .  usleep(micros
108f3 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e 55 53 45  econds);.  UNUSE
108f4 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
108f5 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d  sed);.  return m
108f6 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23 65 6c  icroseconds;.#el
108f7 73 65 0a 20 20 69 6e 74 20 73 65 63 6f 6e 64 73  se.  int seconds
108f8 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73   = (microseconds
108f9 2b 39 39 39 39 39 39 29 2f 31 30 30 30 30 30 30  +999999)/1000000
108fa 3b 0a 20 20 73 6c 65 65 70 28 73 65 63 6f 6e 64  ;.  sleep(second
108fb 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  s);.  UNUSED_PAR
108fc 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
108fd 0a 20 20 72 65 74 75 72 6e 20 73 65 63 6f 6e 64  .  return second
108fe 73 2a 31 30 30 30 30 30 30 3b 0a 23 65 6e 64 69  s*1000000;.#endi
108ff 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  f.}../*.** The f
10900 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c  ollowing variabl
10901 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e  e, if set to a n
10902 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 69  on-zero value, i
10903 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73  s interpreted as
10904 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
10905 66 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20  f seconds since 
10906 31 39 37 30 20 61 6e 64 20 69 73 20 75 73 65 64  1970 and is used
10907 20 74 6f 20 73 65 74 20 74 68 65 20 72 65 73 75   to set the resu
10908 6c 74 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33  lt of.** sqlite3
10909 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29 20  OsCurrentTime() 
1090a 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2e 0a  during testing..
1090b 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
1090c 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
1090d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72   int sqlite3_cur
1090e 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 20 20  rent_time = 0;  
1090f 2f 2a 20 46 61 6b 65 20 73 79 73 74 65 6d 20 74  /* Fake system t
10910 69 6d 65 20 69 6e 20 73 65 63 6f 6e 64 73 20 73  ime in seconds s
10911 69 6e 63 65 20 31 39 37 30 2e 20 2a 2f 0a 23 65  ince 1970. */.#e
10912 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  ndif../*.** Find
10913 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d   the current tim
10914 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20  e (in Universal 
10915 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65  Coordinated Time
10916 29 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a  ).  Write into *
10917 70 69 4e 6f 77 0a 2a 2a 20 74 68 65 20 63 75 72  piNow.** the cur
10918 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61  rent time and da
10919 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44  te as a Julian D
1091a 61 79 20 6e 75 6d 62 65 72 20 74 69 6d 65 73 20  ay number times 
1091b 38 36 5f 34 30 30 5f 30 30 30 2e 20 20 49 6e 0a  86_400_000.  In.
1091c 2a 2a 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  ** other words, 
1091d 77 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 4e 6f  write into *piNo
1091e 77 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  w the number of 
1091f 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 73 69 6e  milliseconds sin
10920 63 65 20 74 68 65 20 4a 75 6c 69 61 6e 0a 2a 2a  ce the Julian.**
10921 20 65 70 6f 63 68 20 6f 66 20 6e 6f 6f 6e 20 69   epoch of noon i
10922 6e 20 47 72 65 65 6e 77 69 63 68 20 6f 6e 20 4e  n Greenwich on N
10923 6f 76 65 6d 62 65 72 20 32 34 2c 20 34 37 31 34  ovember 24, 4714
10924 20 42 2e 43 20 61 63 63 6f 72 64 69 6e 67 20 74   B.C according t
10925 6f 20 74 68 65 0a 2a 2a 20 70 72 6f 6c 65 70 74  o the.** prolept
10926 69 63 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c  ic Gregorian cal
10927 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20  endar..**.** On 
10928 73 75 63 63 65 73 73 2c 20 72 65 74 75 72 6e 20  success, return 
10929 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 52 65 74 75  SQLITE_OK.  Retu
1092a 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  rn SQLITE_ERROR 
1092b 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20  if the time and 
1092c 64 61 74 65 20 0a 2a 2a 20 63 61 6e 6e 6f 74 20  date .** cannot 
1092d 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61  be found..*/.sta
1092e 74 69 63 20 69 6e 74 20 75 6e 69 78 43 75 72 72  tic int unixCurr
1092f 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 73 71 6c  entTimeInt64(sql
10930 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65  ite3_vfs *NotUse
10931 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  d, sqlite3_int64
10932 20 2a 70 69 4e 6f 77 29 7b 0a 20 20 73 74 61 74   *piNow){.  stat
10933 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
10934 5f 69 6e 74 36 34 20 75 6e 69 78 45 70 6f 63 68  _int64 unixEpoch
10935 20 3d 20 32 34 34 30 35 38 37 35 2a 28 73 71 6c   = 24405875*(sql
10936 69 74 65 33 5f 69 6e 74 36 34 29 38 36 34 30 30  ite3_int64)86400
10937 30 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  00;.  int rc = S
10938 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 20 64 65  QLITE_OK;.#if de
10939 66 69 6e 65 64 28 4e 4f 5f 47 45 54 54 4f 44 29  fined(NO_GETTOD)
1093a 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74  .  time_t t;.  t
1093b 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70 69 4e 6f  ime(&t);.  *piNo
1093c 77 20 3d 20 28 28 73 71 6c 69 74 65 33 5f 69 6e  w = ((sqlite3_in
1093d 74 36 34 29 74 29 2a 31 30 30 30 20 2b 20 75 6e  t64)t)*1000 + un
1093e 69 78 45 70 6f 63 68 3b 0a 23 65 6c 69 66 20 4f  ixEpoch;.#elif O
1093f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75  S_VXWORKS.  stru
10940 63 74 20 74 69 6d 65 73 70 65 63 20 73 4e 6f 77  ct timespec sNow
10941 3b 0a 20 20 63 6c 6f 63 6b 5f 67 65 74 74 69 6d  ;.  clock_gettim
10942 65 28 43 4c 4f 43 4b 5f 52 45 41 4c 54 49 4d 45  e(CLOCK_REALTIME
10943 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 2a 70 69 4e  , &sNow);.  *piN
10944 6f 77 20 3d 20 75 6e 69 78 45 70 6f 63 68 20 2b  ow = unixEpoch +
10945 20 31 30 30 30 2a 28 73 71 6c 69 74 65 33 5f 69   1000*(sqlite3_i
10946 6e 74 36 34 29 73 4e 6f 77 2e 74 76 5f 73 65 63  nt64)sNow.tv_sec
10947 20 2b 20 73 4e 6f 77 2e 74 76 5f 6e 73 65 63 2f   + sNow.tv_nsec/
10948 31 30 30 30 30 30 30 3b 0a 23 65 6c 73 65 0a 20  1000000;.#else. 
10949 20 73 74 72 75 63 74 20 74 69 6d 65 76 61 6c 20   struct timeval 
1094a 73 4e 6f 77 3b 0a 20 20 69 66 28 20 67 65 74 74  sNow;.  if( gett
1094b 69 6d 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c 20  imeofday(&sNow, 
1094c 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 69  0)==0 ){.    *pi
1094d 4e 6f 77 20 3d 20 75 6e 69 78 45 70 6f 63 68 20  Now = unixEpoch 
1094e 2b 20 31 30 30 30 2a 28 73 71 6c 69 74 65 33 5f  + 1000*(sqlite3_
1094f 69 6e 74 36 34 29 73 4e 6f 77 2e 74 76 5f 73 65  int64)sNow.tv_se
10950 63 20 2b 20 73 4e 6f 77 2e 74 76 5f 75 73 65 63  c + sNow.tv_usec
10951 2f 31 30 30 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  /1000;.  }else{.
10952 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
10953 45 52 52 4f 52 3b 0a 20 20 7d 0a 23 65 6e 64 69  ERROR;.  }.#endi
10954 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
10955 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69  _TEST.  if( sqli
10956 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65  te3_current_time
10957 20 29 7b 0a 20 20 20 20 2a 70 69 4e 6f 77 20 3d   ){.    *piNow =
10958 20 31 30 30 30 2a 28 73 71 6c 69 74 65 33 5f 69   1000*(sqlite3_i
10959 6e 74 36 34 29 73 71 6c 69 74 65 33 5f 63 75 72  nt64)sqlite3_cur
1095a 72 65 6e 74 5f 74 69 6d 65 20 2b 20 75 6e 69 78  rent_time + unix
1095b 45 70 6f 63 68 3b 0a 20 20 7d 0a 23 65 6e 64 69  Epoch;.  }.#endi
1095c 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  f.  UNUSED_PARAM
1095d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
1095e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1095f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75  *.** Find the cu
10960 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55  rrent time (in U
10961 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e  niversal Coordin
10962 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69  ated Time).  Wri
10963 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  te the.** curren
10964 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20  t time and date 
10965 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20  as a Julian Day 
10966 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e  number into *prN
10967 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e  ow and.** return
10968 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66   0.  Return 1 if
10969 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61   the time and da
1096a 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75  te cannot be fou
1096b 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nd..*/.static in
1096c 74 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d  t unixCurrentTim
1096d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e  e(sqlite3_vfs *N
1096e 6f 74 55 73 65 64 2c 20 64 6f 75 62 6c 65 20 2a  otUsed, double *
1096f 70 72 4e 6f 77 29 7b 0a 20 20 73 71 6c 69 74 65  prNow){.  sqlite
10970 33 5f 69 6e 74 36 34 20 69 20 3d 20 30 3b 0a 20  3_int64 i = 0;. 
10971 20 69 6e 74 20 72 63 3b 0a 20 20 55 4e 55 53 45   int rc;.  UNUSE
10972 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
10973 73 65 64 29 3b 0a 20 20 72 63 20 3d 20 75 6e 69  sed);.  rc = uni
10974 78 43 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36  xCurrentTimeInt6
10975 34 28 30 2c 20 26 69 29 3b 0a 20 20 2a 70 72 4e  4(0, &i);.  *prN
10976 6f 77 20 3d 20 69 2f 38 36 34 30 30 30 30 30 2e  ow = i/86400000.
10977 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  0;.  return rc;.
10978 7d 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65  }../*.** We adde
10979 64 20 74 68 65 20 78 47 65 74 4c 61 73 74 45 72  d the xGetLastEr
1097a 72 6f 72 28 29 20 6d 65 74 68 6f 64 20 77 69 74  ror() method wit
1097b 68 20 74 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20  h the intention 
1097c 6f 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20  of providing.** 
1097d 62 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c  better low-level
1097e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
1097f 77 68 65 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73  when operating-s
10980 79 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63  ystem problems c
10981 6f 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67  ome up.** during
10982 20 53 51 4c 69 74 65 20 6f 70 65 72 61 74 69 6f   SQLite operatio
10983 6e 2e 20 20 42 75 74 20 73 6f 20 66 61 72 2c 20  n.  But so far, 
10984 6e 6f 6e 65 20 6f 66 20 74 68 61 74 20 68 61 73  none of that has
10985 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65   been implemente
10986 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 65  d.** in the core
10987 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69  .  So this routi
10988 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c  ne is never call
10989 65 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74  ed.  For now, it
1098a 20 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20   is merely.** a 
1098b 70 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f  place-holder..*/
1098c 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
1098d 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c  GetLastError(sql
1098e 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65  ite3_vfs *NotUse
1098f 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c  d, int NotUsed2,
10990 20 63 68 61 72 20 2a 4e 6f 74 55 73 65 64 33 29   char *NotUsed3)
10991 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
10992 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
10993 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
10994 52 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 55  R(NotUsed2);.  U
10995 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
10996 4e 6f 74 55 73 65 64 33 29 3b 0a 20 20 72 65 74  NotUsed3);.  ret
10997 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn 0;.}.../*.**
10998 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10999 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71  ****** End of sq
1099a 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64  lite3_vfs method
1099b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s **************
1099c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
1099d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1099e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1099f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
109a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
109a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
109a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
109a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
109a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
109a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
109a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
109a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
109a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
109a9 50 72 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a  Proxy Locking **
109aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
109ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
109ac 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69  *.** Proxy locki
109ad 6e 67 20 69 73 20 61 20 22 75 62 65 72 2d 6c 6f  ng is a "uber-lo
109ae 63 6b 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69 6e  cking-method" in
109af 20 74 68 69 73 20 73 65 6e 73 65 3a 20 20 49 74   this sense:  It
109b0 20 75 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74 68   uses the.** oth
109b1 65 72 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f  er locking metho
109b2 64 73 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79 20  ds on secondary 
109b3 6c 6f 63 6b 20 66 69 6c 65 73 2e 20 20 50 72 6f  lock files.  Pro
109b4 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 0a  xy locking is a.
109b5 2a 2a 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f 76  ** meta-layer ov
109b6 65 72 20 74 6f 70 20 6f 66 20 74 68 65 20 70 72  er top of the pr
109b7 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20  imitive locking 
109b8 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f 76  implemented abov
109b9 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73 20  e.  For.** this 
109ba 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69  reason, the divi
109bb 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d  sion that implem
109bc 65 6e 74 73 20 6f 66 20 70 72 6f 78 79 20 6c 6f  ents of proxy lo
109bd 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65  cking is deferre
109be 64 0a 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65 20  d.** until late 
109bf 69 6e 20 74 68 65 20 66 69 6c 65 20 28 68 65 72  in the file (her
109c0 65 29 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20  e) after all of 
109c1 74 68 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d 65  the other I/O me
109c2 74 68 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62 65  thods have.** be
109c3 65 6e 20 64 65 66 69 6e 65 64 20 2d 20 73 6f 20  en defined - so 
109c4 74 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 69  that the primiti
109c5 76 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f  ve locking metho
109c6 64 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  ds are available
109c7 0a 2a 2a 20 61 73 20 73 65 72 76 69 63 65 73 20  .** as services 
109c8 74 6f 20 68 65 6c 70 20 77 69 74 68 20 74 68 65  to help with the
109c9 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
109ca 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  of proxy locking
109cb 2e 0a 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  ..**.****.**.** 
109cc 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b  The default lock
109cd 69 6e 67 20 73 63 68 65 6d 65 73 20 69 6e 20 53  ing schemes in S
109ce 51 4c 69 74 65 20 75 73 65 20 62 79 74 65 2d 72  QLite use byte-r
109cf 61 6e 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68  ange locks on th
109d0 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69  e.** database fi
109d1 6c 65 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65  le to coordinate
109d2 20 73 61 66 65 2c 20 63 6f 6e 63 75 72 72 65 6e   safe, concurren
109d3 74 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c 74  t access by mult
109d4 69 70 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a 20  iple readers.** 
109d5 61 6e 64 20 77 72 69 74 65 72 73 20 5b 68 74 74  and writers [htt
109d6 70 3a 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f 6c  p://sqlite.org/l
109d7 6f 63 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20  ockingv3.html]. 
109d8 20 54 68 65 20 66 69 76 65 20 66 69 6c 65 20 6c   The five file l
109d9 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73  ocking.** states
109da 20 28 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e 44   (UNLOCKED, PEND
109db 49 4e 47 2c 20 53 48 41 52 45 44 2c 20 52 45 53  ING, SHARED, RES
109dc 45 52 56 45 44 2c 20 45 58 43 4c 55 53 49 56 45  ERVED, EXCLUSIVE
109dd 29 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65  ) are implemente
109de 64 0a 2a 2a 20 61 73 20 50 4f 53 49 58 20 72 65  d.** as POSIX re
109df 61 64 20 26 20 77 72 69 74 65 20 6c 6f 63 6b 73  ad & write locks
109e0 20 6f 76 65 72 20 66 69 78 65 64 20 73 65 74 20   over fixed set 
109e1 6f 66 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76 69  of locations (vi
109e2 61 20 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20  a fsctl),.** on 
109e3 41 46 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c 79  AFP and SMB only
109e4 20 65 78 63 6c 75 73 69 76 65 20 62 79 74 65 2d   exclusive byte-
109e5 72 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20  range locks are 
109e6 61 76 61 69 6c 61 62 6c 65 20 76 69 61 20 66 73  available via fs
109e7 63 74 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f 57  ctl.** with _IOW
109e8 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63  R('z', 23, struc
109e9 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50  t ByteRangeLockP
109ea 42 32 29 20 74 6f 20 74 72 61 63 6b 20 74 68 65  B2) to track the
109eb 20 73 61 6d 65 20 35 20 73 74 61 74 65 73 2e 0a   same 5 states..
109ec 2a 2a 20 54 6f 20 73 69 6d 75 6c 61 74 65 20 61  ** To simulate a
109ed 20 46 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65 20   F_RDLCK on the 
109ee 73 68 61 72 65 64 20 72 61 6e 67 65 2c 20 6f 6e  shared range, on
109ef 20 41 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79 20   AFP a randomly 
109f0 73 65 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64 72  selected.** addr
109f1 65 73 73 20 69 6e 20 74 68 65 20 73 68 61 72 65  ess in the share
109f2 64 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e  d range is taken
109f3 20 66 6f 72 20 61 20 53 48 41 52 45 44 20 6c 6f   for a SHARED lo
109f4 63 6b 2c 20 74 68 65 20 65 6e 74 69 72 65 0a 2a  ck, the entire.*
109f5 2a 20 73 68 61 72 65 64 20 72 61 6e 67 65 20 69  * shared range i
109f6 73 20 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20 45  s taken for an E
109f7 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a 0a  XCLUSIVE lock):.
109f8 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44 49  **.**      PENDI
109f9 4e 47 5f 42 59 54 45 20 20 20 20 20 20 20 20 30  NG_BYTE        0
109fa 78 34 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20  x40000000.**    
109fb 20 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 20    RESERVED_BYTE 
109fc 20 20 20 20 20 20 30 78 34 30 30 30 30 30 30 31        0x40000001
109fd 0a 2a 2a 20 20 20 20 20 20 53 48 41 52 45 44 5f  .**      SHARED_
109fe 52 41 4e 47 45 20 20 20 20 20 20 20 20 30 78 34  RANGE        0x4
109ff 30 30 30 30 30 30 32 20 2d 3e 20 30 78 34 30 30  0000002 -> 0x400
10a00 30 30 32 30 30 0a 2a 2a 0a 2a 2a 20 54 68 69 73  00200.**.** This
10a01 20 77 6f 72 6b 73 20 77 65 6c 6c 20 6f 6e 20 74   works well on t
10a02 68 65 20 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79  he local file sy
10a03 73 74 65 6d 2c 20 62 75 74 20 73 68 6f 77 73 20  stem, but shows 
10a04 61 20 6e 65 61 72 6c 79 20 31 30 30 78 0a 2a 2a  a nearly 100x.**
10a05 20 73 6c 6f 77 64 6f 77 6e 20 69 6e 20 72 65 61   slowdown in rea
10a06 64 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f 6e  d performance on
10a07 20 41 46 50 20 62 65 63 61 75 73 65 20 74 68 65   AFP because the
10a08 20 41 46 50 20 63 6c 69 65 6e 74 20 64 69 73 61   AFP client disa
10a09 62 6c 65 73 0a 2a 2a 20 74 68 65 20 72 65 61 64  bles.** the read
10a0a 20 63 61 63 68 65 20 77 68 65 6e 20 62 79 74 65   cache when byte
10a0b 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65  -range locks are
10a0c 20 70 72 65 73 65 6e 74 2e 20 20 45 6e 61 62 6c   present.  Enabl
10a0d 69 6e 67 20 74 68 65 20 72 65 61 64 0a 2a 2a 20  ing the read.** 
10a0e 63 61 63 68 65 20 65 78 70 6f 73 65 73 20 61 20  cache exposes a 
10a0f 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 20  cache coherency 
10a10 70 72 6f 62 6c 65 6d 20 74 68 61 74 20 69 73 20  problem that is 
10a11 70 72 65 73 65 6e 74 20 6f 6e 20 61 6c 6c 20 4f  present on all O
10a12 53 20 58 0a 2a 2a 20 73 75 70 70 6f 72 74 65 64  S X.** supported
10a13 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 20 73 79   network file sy
10a14 73 74 65 6d 73 2e 20 20 4e 46 53 20 61 6e 64 20  stems.  NFS and 
10a15 41 46 50 20 62 6f 74 68 20 6f 62 73 65 72 76 65  AFP both observe
10a16 20 74 68 65 0a 2a 2a 20 63 6c 6f 73 65 2d 74 6f   the.** close-to
10a17 2d 6f 70 65 6e 20 73 65 6d 61 6e 74 69 63 73 20  -open semantics 
10a18 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 63 61 63  for ensuring cac
10a19 68 65 20 63 6f 68 65 72 65 6e 63 79 0a 2a 2a 20  he coherency.** 
10a1a 5b 68 74 74 70 3a 2f 2f 6e 66 73 2e 73 6f 75 72  [http://nfs.sour
10a1b 63 65 66 6f 72 67 65 2e 6e 65 74 2f 23 66 61 71  ceforge.net/#faq
10a1c 5f 61 38 5d 2c 20 77 68 69 63 68 20 64 6f 65 73  _a8], which does
10a1d 20 6e 6f 74 20 65 66 66 65 63 74 69 76 65 6c 79   not effectively
10a1e 0a 2a 2a 20 61 64 64 72 65 73 73 20 74 68 65 20  .** address the 
10a1f 72 65 71 75 69 72 65 6d 65 6e 74 73 20 66 6f 72  requirements for
10a20 20 63 6f 6e 63 75 72 72 65 6e 74 20 64 61 74 61   concurrent data
10a21 62 61 73 65 20 61 63 63 65 73 73 20 62 79 20 6d  base access by m
10a22 75 6c 74 69 70 6c 65 0a 2a 2a 20 72 65 61 64 65  ultiple.** reade
10a23 72 73 20 61 6e 64 20 77 72 69 74 65 72 73 0a 2a  rs and writers.*
10a24 2a 20 5b 68 74 74 70 3a 2f 2f 77 77 77 2e 6e 61  * [http://www.na
10a25 62 62 6c 65 2e 63 6f 6d 2f 53 51 4c 69 74 65 2d  bble.com/SQLite-
10a26 6f 6e 2d 4e 46 53 2d 63 61 63 68 65 2d 63 6f 68  on-NFS-cache-coh
10a27 65 72 65 6e 63 79 2d 74 64 31 35 36 35 35 37 30  erency-td1565570
10a28 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a 0a 2a 2a 20 54  1.html]..**.** T
10a29 6f 20 61 64 64 72 65 73 73 20 74 68 65 20 70 65  o address the pe
10a2a 72 66 6f 72 6d 61 6e 63 65 20 61 6e 64 20 63 61  rformance and ca
10a2b 63 68 65 20 63 6f 68 65 72 65 6e 63 79 20 69 73  che coherency is
10a2c 73 75 65 73 2c 20 70 72 6f 78 79 20 66 69 6c 65  sues, proxy file
10a2d 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 63 68 61 6e   locking.** chan
10a2e 67 65 73 20 74 68 65 20 77 61 79 20 64 61 74 61  ges the way data
10a2f 62 61 73 65 20 61 63 63 65 73 73 20 69 73 20 63  base access is c
10a30 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 6c 69 6d  ontrolled by lim
10a31 69 74 69 6e 67 20 61 63 63 65 73 73 20 74 6f 20  iting access to 
10a32 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 68 6f 73 74  a.** single host
10a33 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64 20 6d   at a time and m
10a34 6f 76 69 6e 67 20 66 69 6c 65 20 6c 6f 63 6b 73  oving file locks
10a35 20 6f 66 66 20 6f 66 20 74 68 65 20 64 61 74 61   off of the data
10a36 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64  base file.** and
10a37 20 6f 6e 74 6f 20 61 20 70 72 6f 78 79 20 66 69   onto a proxy fi
10a38 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  le on the local 
10a39 66 69 6c 65 20 73 79 73 74 65 6d 2e 20 20 0a 2a  file system.  .*
10a3a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69 6e 67 20 70 72  *.**.** Using pr
10a3b 6f 78 79 20 6c 6f 63 6b 73 0a 2a 2a 20 2d 2d 2d  oxy locks.** ---
10a3c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
10a3d 2a 0a 2a 2a 20 43 20 41 50 49 73 0a 2a 2a 0a 2a  *.** C APIs.**.*
10a3e 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  *  sqlite3_file_
10a3f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 62 6e 61  control(db, dbna
10a40 6d 65 2c 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c  me, SQLITE_SET_L
10a41 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 0a 2a 2a  OCKPROXYFILE,.**
10a42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10a43 20 20 20 20 20 20 20 3c 70 72 6f 78 79 5f 70 61         <proxy_pa
10a44 74 68 3e 20 7c 20 22 3a 61 75 74 6f 3a 22 29 3b  th> | ":auto:");
10a45 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  .**  sqlite3_fil
10a46 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64 62  e_control(db, db
10a47 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 47 45 54  name, SQLITE_GET
10a48 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20  _LOCKPROXYFILE, 
10a49 26 3c 70 72 6f 78 79 5f 70 61 74 68 3e 29 3b 0a  &<proxy_path>);.
10a4a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51 4c 20 70 72 61  **.**.** SQL pra
10a4b 67 6d 61 73 0a 2a 2a 0a 2a 2a 20 20 50 52 41 47  gmas.**.**  PRAG
10a4c 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f  MA [database.]lo
10a4d 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 3d 3c 70  ck_proxy_file=<p
10a4e 72 6f 78 79 5f 70 61 74 68 3e 20 7c 20 3a 61 75  roxy_path> | :au
10a4f 74 6f 3a 0a 2a 2a 20 20 50 52 41 47 4d 41 20 5b  to:.**  PRAGMA [
10a50 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70  database.]lock_p
10a51 72 6f 78 79 5f 66 69 6c 65 0a 2a 2a 0a 2a 2a 20  roxy_file.**.** 
10a52 53 70 65 63 69 66 79 69 6e 67 20 22 3a 61 75 74  Specifying ":aut
10a53 6f 3a 22 20 6d 65 61 6e 73 20 74 68 61 74 20 69  o:" means that i
10a54 66 20 74 68 65 72 65 20 69 73 20 61 20 63 6f 6e  f there is a con
10a55 63 68 20 66 69 6c 65 20 77 69 74 68 20 61 20 6d  ch file with a m
10a56 61 74 63 68 69 6e 67 0a 2a 2a 20 68 6f 73 74 20  atching.** host 
10a57 49 44 20 69 6e 20 69 74 2c 20 74 68 65 20 70 72  ID in it, the pr
10a58 6f 78 79 20 70 61 74 68 20 69 6e 20 74 68 65 20  oxy path in the 
10a59 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 6c 6c 20  conch file will 
10a5a 62 65 20 75 73 65 64 2c 20 6f 74 68 65 72 77 69  be used, otherwi
10a5b 73 65 0a 2a 2a 20 61 20 70 72 6f 78 79 20 70 61  se.** a proxy pa
10a5c 74 68 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20  th based on the 
10a5d 75 73 65 72 27 73 20 74 65 6d 70 20 64 69 72 0a  user's temp dir.
10a5e 2a 2a 20 28 76 69 61 20 63 6f 6e 66 73 74 72 28  ** (via confstr(
10a5f 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f  _CS_DARWIN_USER_
10a60 54 45 4d 50 5f 44 49 52 2c 2e 2e 2e 29 29 20 77  TEMP_DIR,...)) w
10a61 69 6c 6c 20 62 65 20 75 73 65 64 20 61 6e 64 20  ill be used and 
10a62 74 68 65 0a 2a 2a 20 61 63 74 75 61 6c 20 70 72  the.** actual pr
10a63 6f 78 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 73  oxy file name is
10a64 20 67 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20   generated from 
10a65 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 70 61 74  the name and pat
10a66 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61  h of the.** data
10a67 62 61 73 65 20 66 69 6c 65 2e 20 20 46 6f 72 20  base file.  For 
10a68 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20  example:.**.**  
10a69 20 20 20 20 20 46 6f 72 20 64 61 74 61 62 61 73       For databas
10a6a 65 20 70 61 74 68 20 22 2f 55 73 65 72 73 2f 6d  e path "/Users/m
10a6b 65 2f 66 6f 6f 2e 64 62 22 20 0a 2a 2a 20 20 20  e/foo.db" .**   
10a6c 20 20 20 20 54 68 65 20 6c 6f 63 6b 20 70 61 74      The lock pat
10a6d 68 20 77 69 6c 6c 20 62 65 20 22 3c 74 6d 70 64  h will be "<tmpd
10a6e 69 72 3e 2f 73 71 6c 69 74 65 70 6c 6f 63 6b 73  ir>/sqliteplocks
10a6f 2f 5f 55 73 65 72 73 5f 6d 65 5f 66 6f 6f 2e 64  /_Users_me_foo.d
10a70 62 3a 61 75 74 6f 3a 22 29 0a 2a 2a 0a 2a 2a 20  b:auto:").**.** 
10a71 4f 6e 63 65 20 61 20 6c 6f 63 6b 20 70 72 6f 78  Once a lock prox
10a72 79 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 20  y is configured 
10a73 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 63  for a database c
10a74 6f 6e 6e 65 63 74 69 6f 6e 2c 20 69 74 20 63 61  onnection, it ca
10a75 6e 20 6e 6f 74 0a 2a 2a 20 62 65 20 72 65 6d 6f  n not.** be remo
10a76 76 65 64 2c 20 68 6f 77 65 76 65 72 20 69 74 20  ved, however it 
10a77 6d 61 79 20 62 65 20 73 77 69 74 63 68 65 64 20  may be switched 
10a78 74 6f 20 61 20 64 69 66 66 65 72 65 6e 74 20 70  to a different p
10a79 72 6f 78 79 20 70 61 74 68 20 76 69 61 0a 2a 2a  roxy path via.**
10a7a 20 74 68 65 20 61 62 6f 76 65 20 41 50 49 73 20   the above APIs 
10a7b 28 61 73 73 75 6d 69 6e 67 20 74 68 65 20 63 6f  (assuming the co
10a7c 6e 63 68 20 66 69 6c 65 20 69 73 20 6e 6f 74 20  nch file is not 
10a7d 62 65 69 6e 67 20 68 65 6c 64 20 62 79 20 61 6e  being held by an
10a7e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e 6e 65 63 74  other.** connect
10a7f 69 6f 6e 20 6f 72 20 70 72 6f 63 65 73 73 29 2e  ion or process).
10a80 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 48 6f 77 20 70   .**.**.** How p
10a81 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 77 6f 72  roxy locking wor
10a82 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ks.** ----------
10a83 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
10a84 0a 2a 2a 20 50 72 6f 78 79 20 66 69 6c 65 20 6c  .** Proxy file l
10a85 6f 63 6b 69 6e 67 20 72 65 6c 69 65 73 20 70 72  ocking relies pr
10a86 69 6d 61 72 69 6c 79 20 6f 6e 20 74 77 6f 20 6e  imarily on two n
10a87 65 77 20 73 75 70 70 6f 72 74 69 6e 67 20 66 69  ew supporting fi
10a88 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  les: .**.**   * 
10a89 20 63 6f 6e 63 68 20 66 69 6c 65 20 74 6f 20 6c   conch file to l
10a8a 69 6d 69 74 20 61 63 63 65 73 73 20 74 6f 20 74  imit access to t
10a8b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
10a8c 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 68 6f 73   to a single hos
10a8d 74 0a 2a 2a 20 20 20 20 20 20 61 74 20 61 20 74  t.**      at a t
10a8e 69 6d 65 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 70  ime.**.**   *  p
10a8f 72 6f 78 79 20 66 69 6c 65 20 74 6f 20 61 63 74  roxy file to act
10a90 20 61 73 20 61 20 70 72 6f 78 79 20 66 6f 72 20   as a proxy for 
10a91 74 68 65 20 61 64 76 69 73 6f 72 79 20 6c 6f 63  the advisory loc
10a92 6b 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 20  ks normally.**  
10a93 20 20 20 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65      taken on the
10a94 20 64 61 74 61 62 61 73 65 0a 2a 2a 0a 2a 2a 20   database.**.** 
10a95 54 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2d  The conch file -
10a96 20 74 6f 20 75 73 65 20 61 20 70 72 6f 78 79 20   to use a proxy 
10a97 66 69 6c 65 2c 20 73 71 6c 69 74 65 20 6d 75 73  file, sqlite mus
10a98 74 20 66 69 72 73 74 20 22 68 6f 6c 64 20 74 68  t first "hold th
10a99 65 20 63 6f 6e 63 68 22 0a 2a 2a 20 62 79 20 74  e conch".** by t
10a9a 61 6b 69 6e 67 20 61 6e 20 73 71 6c 69 74 65 2d  aking an sqlite-
10a9b 73 74 79 6c 65 20 73 68 61 72 65 64 20 6c 6f 63  style shared loc
10a9c 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 68 20 66  k on the conch f
10a9d 69 6c 65 2c 20 72 65 61 64 69 6e 67 20 74 68 65  ile, reading the
10a9e 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 61 6e 64  .** contents and
10a9f 20 63 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 68   comparing the h
10aa0 6f 73 74 27 73 20 75 6e 69 71 75 65 20 68 6f 73  ost's unique hos
10aa1 74 20 49 44 20 28 73 65 65 20 62 65 6c 6f 77 29  t ID (see below)
10aa2 20 61 6e 64 20 6c 6f 63 6b 0a 2a 2a 20 70 72 6f   and lock.** pro
10aa3 78 79 20 70 61 74 68 20 61 67 61 69 6e 73 74 20  xy path against 
10aa4 74 68 65 20 76 61 6c 75 65 73 20 73 74 6f 72 65  the values store
10aa5 64 20 69 6e 20 74 68 65 20 63 6f 6e 63 68 2e 20  d in the conch. 
10aa6 20 54 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20   The conch file 
10aa7 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20  is.** stored in 
10aa8 74 68 65 20 73 61 6d 65 20 64 69 72 65 63 74 6f  the same directo
10aa9 72 79 20 61 73 20 74 68 65 20 64 61 74 61 62 61  ry as the databa
10aaa 73 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20  se file and the 
10aab 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 20  file name.** is 
10aac 70 61 74 74 65 72 6e 65 64 20 61 66 74 65 72 20  patterned after 
10aad 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
10aae 65 20 6e 61 6d 65 20 61 73 20 22 2e 3c 64 61 74  e name as ".<dat
10aaf 61 62 61 73 65 6e 61 6d 65 3e 2d 63 6f 6e 63 68  abasename>-conch
10ab0 22 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6e  "..** If the con
10ab1 63 68 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74  ch file does not
10ab2 20 65 78 69 73 74 2c 20 6f 72 20 69 74 27 73 20   exist, or it's 
10ab3 63 6f 6e 74 65 6e 74 73 20 64 6f 20 6e 6f 74 20  contents do not 
10ab4 6d 61 74 63 68 20 74 68 65 0a 2a 2a 20 68 6f 73  match the.** hos
10ab5 74 20 49 44 20 61 6e 64 2f 6f 72 20 70 72 6f 78  t ID and/or prox
10ab6 79 20 70 61 74 68 2c 20 74 68 65 6e 20 74 68 65  y path, then the
10ab7 20 6c 6f 63 6b 20 69 73 20 65 73 63 61 6c 61 74   lock is escalat
10ab8 65 64 20 74 6f 20 61 6e 20 65 78 63 6c 75 73 69  ed to an exclusi
10ab9 76 65 0a 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 74  ve.** lock and t
10aba 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 63 6f  he conch file co
10abb 6e 74 65 6e 74 73 20 69 73 20 75 70 64 61 74 65  ntents is update
10abc 64 20 77 69 74 68 20 74 68 65 20 68 6f 73 74 20  d with the host 
10abd 49 44 20 61 6e 64 20 70 72 6f 78 79 0a 2a 2a 20  ID and proxy.** 
10abe 70 61 74 68 20 61 6e 64 20 74 68 65 20 6c 6f 63  path and the loc
10abf 6b 20 69 73 20 64 6f 77 6e 67 72 61 64 65 64 20  k is downgraded 
10ac0 74 6f 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  to a shared lock
10ac1 20 61 67 61 69 6e 2e 20 20 49 66 20 74 68 65 20   again.  If the 
10ac2 63 6f 6e 63 68 0a 2a 2a 20 69 73 20 68 65 6c 64  conch.** is held
10ac3 20 62 79 20 61 6e 6f 74 68 65 72 20 70 72 6f 63   by another proc
10ac4 65 73 73 20 28 77 69 74 68 20 61 20 73 68 61 72  ess (with a shar
10ac5 65 64 20 6c 6f 63 6b 29 2c 20 74 68 65 20 65 78  ed lock), the ex
10ac6 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20  clusive lock.** 
10ac7 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 53 51  will fail and SQ
10ac8 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74  LITE_BUSY is ret
10ac9 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  urned..**.** The
10aca 20 70 72 6f 78 79 20 66 69 6c 65 20 2d 20 61 20   proxy file - a 
10acb 73 69 6e 67 6c 65 2d 62 79 74 65 20 66 69 6c 65  single-byte file
10acc 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 61 64   used for all ad
10acd 76 69 73 6f 72 79 20 66 69 6c 65 20 6c 6f 63 6b  visory file lock
10ace 73 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 74 61  s.** normally ta
10acf 6b 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ken on the datab
10ad0 61 73 65 20 66 69 6c 65 2e 20 20 20 54 68 69 73  ase file.   This
10ad1 20 61 6c 6c 6f 77 73 20 66 6f 72 20 73 61 66 65   allows for safe
10ad2 20 73 68 61 72 69 6e 67 0a 2a 2a 20 6f 66 20 74   sharing.** of t
10ad3 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
10ad4 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 20 72 65   for multiple re
10ad5 61 64 65 72 73 20 61 6e 64 20 77 72 69 74 65 72  aders and writer
10ad6 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a  s on the same.**
10ad7 20 68 6f 73 74 20 28 74 68 65 20 63 6f 6e 63 68   host (the conch
10ad8 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 74 68   ensures that th
10ad9 65 79 20 61 6c 6c 20 75 73 65 20 74 68 65 20 73  ey all use the s
10ada 61 6d 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66  ame local lock f
10adb 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  ile)..**.** Requ
10adc 65 73 74 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  esting the lock 
10add 70 72 6f 78 79 20 64 6f 65 73 20 6e 6f 74 20 69  proxy does not i
10ade 6d 6d 65 64 69 61 74 65 6c 79 20 74 61 6b 65 20  mmediately take 
10adf 74 68 65 20 63 6f 6e 63 68 2c 20 69 74 20 69 73  the conch, it is
10ae0 0a 2a 2a 20 6f 6e 6c 79 20 74 61 6b 65 6e 20 77  .** only taken w
10ae1 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 72 65  hen the first re
10ae2 71 75 65 73 74 20 74 6f 20 6c 6f 63 6b 20 64 61  quest to lock da
10ae3 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d  tabase file is m
10ae4 61 64 65 2e 20 20 0a 2a 2a 20 54 68 69 73 20 6d  ade.  .** This m
10ae5 61 74 63 68 65 73 20 74 68 65 20 73 65 6d 61 6e  atches the seman
10ae6 74 69 63 73 20 6f 66 20 74 68 65 20 74 72 61 64  tics of the trad
10ae7 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20  itional locking 
10ae8 62 65 68 61 76 69 6f 72 2c 20 77 68 65 72 65 0a  behavior, where.
10ae9 2a 2a 20 6f 70 65 6e 69 6e 67 20 61 20 63 6f 6e  ** opening a con
10aea 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 64 61 74  nection to a dat
10aeb 61 62 61 73 65 20 66 69 6c 65 20 64 6f 65 73 20  abase file does 
10aec 6e 6f 74 20 74 61 6b 65 20 61 20 6c 6f 63 6b 20  not take a lock 
10aed 6f 6e 20 69 74 2e 0a 2a 2a 20 54 68 65 20 73 68  on it..** The sh
10aee 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6e  ared lock and an
10aef 20 6f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72   open file descr
10af0 69 70 74 6f 72 20 61 72 65 20 6d 61 69 6e 74 61  iptor are mainta
10af1 69 6e 65 64 20 75 6e 74 69 6c 20 0a 2a 2a 20 74  ined until .** t
10af2 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f  he connection to
10af3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
10af4 20 63 6c 6f 73 65 64 2e 20 0a 2a 2a 0a 2a 2a 20   closed. .**.** 
10af5 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65 20 61  The proxy file a
10af6 6e 64 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65  nd the lock file
10af7 20 61 72 65 20 6e 65 76 65 72 20 64 65 6c 65 74   are never delet
10af8 65 64 20 73 6f 20 74 68 65 79 20 6f 6e 6c 79 20  ed so they only 
10af9 6e 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 63 72  need.** to be cr
10afa 65 61 74 65 64 20 74 68 65 20 66 69 72 73 74 20  eated the first 
10afb 74 69 6d 65 20 74 68 65 79 20 61 72 65 20 75 73  time they are us
10afc 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67  ed..**.** Config
10afd 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 0a  uration options.
10afe 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ** -------------
10aff 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20  --------.**.**  
10b00 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52  SQLITE_PREFER_PR
10b01 4f 58 59 5f 4c 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a  OXY_LOCKING.**.*
10b02 2a 20 20 20 20 20 20 20 44 61 74 61 62 61 73 65  *       Database
10b03 20 66 69 6c 65 73 20 61 63 63 65 73 73 65 64 20   files accessed 
10b04 6f 6e 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c  on non-local fil
10b05 65 20 73 79 73 74 65 6d 73 20 61 72 65 0a 2a 2a  e systems are.**
10b06 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63         automatic
10b07 61 6c 6c 79 20 63 6f 6e 66 69 67 75 72 65 64 20  ally configured 
10b08 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e  for proxy lockin
10b09 67 2c 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 72  g, lock files ar
10b0a 65 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 64  e.**       named
10b0b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 75   automatically u
10b0c 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f  sing the same lo
10b0d 67 69 63 20 61 73 0a 2a 2a 20 20 20 20 20 20 20  gic as.**       
10b0e 50 52 41 47 4d 41 20 6c 6f 63 6b 5f 70 72 6f 78  PRAGMA lock_prox
10b0f 79 5f 66 69 6c 65 3d 22 3a 61 75 74 6f 3a 22 0a  y_file=":auto:".
10b10 2a 2a 20 20 20 20 0a 2a 2a 20 20 53 51 4c 49 54  **    .**  SQLIT
10b11 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 0a 2a 2a  E_PROXY_DEBUG.**
10b12 0a 2a 2a 20 20 20 20 20 20 20 45 6e 61 62 6c 65  .**       Enable
10b13 73 20 74 68 65 20 6c 6f 67 67 69 6e 67 20 6f 66  s the logging of
10b14 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
10b15 64 75 72 69 6e 67 20 68 6f 73 74 20 69 64 20 66  during host id f
10b16 69 6c 65 0a 2a 2a 20 20 20 20 20 20 20 72 65 74  ile.**       ret
10b17 72 69 65 76 61 6c 20 61 6e 64 20 63 72 65 61 74  rieval and creat
10b18 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 4c 4f 43 4b 50  ion.**.**  LOCKP
10b19 52 4f 58 59 44 49 52 0a 2a 2a 0a 2a 2a 20 20 20  ROXYDIR.**.**   
10b1a 20 20 20 20 4f 76 65 72 72 69 64 65 73 20 74 68      Overrides th
10b1b 65 20 64 65 66 61 75 6c 74 20 64 69 72 65 63 74  e default direct
10b1c 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f 63  ory used for loc
10b1d 6b 20 70 72 6f 78 79 20 66 69 6c 65 73 20 74 68  k proxy files th
10b1e 61 74 0a 2a 2a 20 20 20 20 20 20 20 61 72 65 20  at.**       are 
10b1f 6e 61 6d 65 64 20 61 75 74 6f 6d 61 74 69 63 61  named automatica
10b20 6c 6c 79 20 76 69 61 20 74 68 65 20 22 3a 61 75  lly via the ":au
10b21 74 6f 3a 22 20 73 65 74 74 69 6e 67 0a 2a 2a 0a  to:" setting.**.
10b22 2a 2a 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55  **  SQLITE_DEFAU
10b23 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d  LT_PROXYDIR_PERM
10b24 49 53 53 49 4f 4e 53 0a 2a 2a 0a 2a 2a 20 20 20  ISSIONS.**.**   
10b25 20 20 20 20 50 65 72 6d 69 73 73 69 6f 6e 73 20      Permissions 
10b26 74 6f 20 75 73 65 20 77 68 65 6e 20 63 72 65 61  to use when crea
10b27 74 69 6e 67 20 61 20 64 69 72 65 63 74 6f 72 79  ting a directory
10b28 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65   for storing the
10b29 0a 2a 2a 20 20 20 20 20 20 20 6c 6f 63 6b 20 70  .**       lock p
10b2a 72 6f 78 79 20 66 69 6c 65 73 2c 20 6f 6e 6c 79  roxy files, only
10b2b 20 75 73 65 64 20 77 68 65 6e 20 4c 4f 43 4b 50   used when LOCKP
10b2c 52 4f 58 59 44 49 52 20 69 73 20 6e 6f 74 20 73  ROXYDIR is not s
10b2d 65 74 2e 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20 20  et..**    .**   
10b2e 20 0a 2a 2a 20 41 73 20 6d 65 6e 74 69 6f 6e 65   .** As mentione
10b2f 64 20 61 62 6f 76 65 2c 20 77 68 65 6e 20 63 6f  d above, when co
10b30 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49  mpiled with SQLI
10b31 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f  TE_PREFER_PROXY_
10b32 4c 4f 43 4b 49 4e 47 2c 0a 2a 2a 20 73 65 74 74  LOCKING,.** sett
10b33 69 6e 67 20 74 68 65 20 65 6e 76 69 72 6f 6e 6d  ing the environm
10b34 65 6e 74 20 76 61 72 69 61 62 6c 65 20 53 51 4c  ent variable SQL
10b35 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f  ITE_FORCE_PROXY_
10b36 4c 4f 43 4b 49 4e 47 20 74 6f 20 31 20 77 69 6c  LOCKING to 1 wil
10b37 6c 0a 2a 2a 20 66 6f 72 63 65 20 70 72 6f 78 79  l.** force proxy
10b38 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 20 75   locking to be u
10b39 73 65 64 20 66 6f 72 20 65 76 65 72 79 20 64 61  sed for every da
10b3a 74 61 62 61 73 65 20 66 69 6c 65 20 6f 70 65 6e  tabase file open
10b3b 65 64 2c 20 61 6e 64 20 30 0a 2a 2a 20 77 69 6c  ed, and 0.** wil
10b3c 6c 20 66 6f 72 63 65 20 61 75 74 6f 6d 61 74 69  l force automati
10b3d 63 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20  c proxy locking 
10b3e 74 6f 20 62 65 20 64 69 73 61 62 6c 65 64 20 66  to be disabled f
10b3f 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 0a  or all database.
10b40 2a 2a 20 66 69 6c 65 73 20 28 65 78 70 6c 69 63  ** files (explic
10b41 69 74 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20  ity calling the 
10b42 53 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50  SQLITE_SET_LOCKP
10b43 52 4f 58 59 46 49 4c 45 20 70 72 61 67 6d 61 20  ROXYFILE pragma 
10b44 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 5f 66 69 6c  or.** sqlite_fil
10b45 65 5f 63 6f 6e 74 72 6f 6c 20 41 50 49 20 69 73  e_control API is
10b46 20 6e 6f 74 20 61 66 66 65 63 74 65 64 20 62 79   not affected by
10b47 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52   SQLITE_FORCE_PR
10b48 4f 58 59 5f 4c 4f 43 4b 49 4e 47 29 2e 0a 2a 2f  OXY_LOCKING)..*/
10b49 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f  ../*.** Proxy lo
10b4a 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61 76  cking is only av
10b4b 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53  ailable on MacOS
10b4c 58 20 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  X .*/.#if define
10b4d 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20  d(__APPLE__) && 
10b4e 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
10b4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 0a 2f 2a 0a  CKING_STYLE../*.
10b50 2a 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b  ** The proxyLock
10b51 69 6e 67 43 6f 6e 74 65 78 74 20 68 61 73 20 74  ingContext has t
10b52 68 65 20 70 61 74 68 20 61 6e 64 20 66 69 6c 65  he path and file
10b53 20 73 74 72 75 63 74 75 72 65 73 20 66 6f 72 20   structures for 
10b54 74 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61  the remote .** a
10b55 6e 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66  nd local proxy f
10b56 69 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79  iles in it.*/.ty
10b57 70 65 64 65 66 20 73 74 72 75 63 74 20 70 72 6f  pedef struct pro
10b58 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
10b59 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
10b5a 74 65 78 74 3b 0a 73 74 72 75 63 74 20 70 72 6f  text;.struct pro
10b5b 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
10b5c 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63   {.  unixFile *c
10b5d 6f 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20  onchFile;       
10b5e 20 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20    /* Open conch 
10b5f 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  file */.  char *
10b60 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b 20 20  conchFilePath;  
10b61 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
10b62 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  f the conch file
10b63 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a   */.  unixFile *
10b64 6c 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20 20 20  lockProxy;      
10b65 20 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79     /* Open proxy
10b66 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20   lock file */.  
10b67 63 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50  char *lockProxyP
10b68 61 74 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ath;         /* 
10b69 4e 61 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 78  Name of the prox
10b6a 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20  y lock file */. 
10b6b 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20   char *dbPath;  
10b6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10b6d 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65   Name of the ope
10b6e 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  n file */.  int 
10b6f 63 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20 20 20  conchHeld;      
10b70 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 69 66           /* 1 if
10b71 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20 68 65   the conch is he
10b72 6c 64 2c 20 2d 31 20 69 66 20 6c 6f 63 6b 6c 65  ld, -1 if lockle
10b73 73 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c  ss */.  void *ol
10b74 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  dLockingContext;
10b75 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c       /* Original
10b76 20 6c 6f 63 6b 69 6e 67 63 6f 6e 74 65 78 74 20   lockingcontext 
10b77 74 6f 20 72 65 73 74 6f 72 65 20 6f 6e 20 63 6c  to restore on cl
10b78 6f 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ose */.  sqlite3
10b79 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73  _io_methods cons
10b7a 74 20 2a 70 4f 6c 64 4d 65 74 68 6f 64 3b 20 20  t *pOldMethod;  
10b7b 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 49     /* Original I
10b7c 2f 4f 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 63  /O methods for c
10b7d 6c 6f 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a  lose */.};../* .
10b7e 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63  ** The proxy loc
10b7f 6b 20 66 69 6c 65 20 70 61 74 68 20 66 6f 72 20  k file path for 
10b80 74 68 65 20 64 61 74 61 62 61 73 65 20 61 74 20  the database at 
10b81 64 62 50 61 74 68 20 69 73 20 77 72 69 74 74 65  dbPath is writte
10b82 6e 20 69 6e 74 6f 20 6c 50 61 74 68 2c 20 0a 2a  n into lPath, .*
10b83 2a 20 77 68 69 63 68 20 6d 75 73 74 20 70 6f 69  * which must poi
10b84 6e 74 20 74 6f 20 76 61 6c 69 64 2c 20 77 72 69  nt to valid, wri
10b85 74 61 62 6c 65 20 6d 65 6d 6f 72 79 20 6c 61 72  table memory lar
10b86 67 65 20 65 6e 6f 75 67 68 20 66 6f 72 20 61 20  ge enough for a 
10b87 6d 61 78 4c 65 6e 20 6c 65 6e 67 74 68 0a 2a 2a  maxLen length.**
10b88 20 66 69 6c 65 20 70 61 74 68 2e 20 0a 2a 2f 0a   file path. .*/.
10b89 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
10b8a 47 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73  GetLockPath(cons
10b8b 74 20 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20  t char *dbPath, 
10b8c 63 68 61 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a  char *lPath, siz
10b8d 65 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69  e_t maxLen){.  i
10b8e 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62  nt len;.  int db
10b8f 4c 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23  Len;.  int i;..#
10b90 69 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44  ifdef LOCKPROXYD
10b91 49 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63  IR.  len = strlc
10b92 70 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52  py(lPath, LOCKPR
10b93 4f 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b  OXYDIR, maxLen);
10b94 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 5f  .#else.# ifdef _
10b95 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54  CS_DARWIN_USER_T
10b96 45 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20  EMP_DIR.  {.    
10b97 69 66 28 20 21 63 6f 6e 66 73 74 72 28 5f 43 53  if( !confstr(_CS
10b98 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d  _DARWIN_USER_TEM
10b99 50 5f 44 49 52 2c 20 6c 50 61 74 68 2c 20 6d 61  P_DIR, lPath, ma
10b9a 78 4c 65 6e 29 20 29 7b 0a 20 20 20 20 20 20 4f  xLen) ){.      O
10b9b 53 54 52 41 43 45 28 28 22 47 45 54 4c 4f 43 4b  STRACE(("GETLOCK
10b9c 50 41 54 48 20 20 66 61 69 6c 65 64 20 25 73 20  PATH  failed %s 
10b9d 65 72 72 6e 6f 3d 25 64 20 70 69 64 3d 25 64 5c  errno=%d pid=%d\
10b9e 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
10b9f 20 20 20 6c 50 61 74 68 2c 20 65 72 72 6e 6f 2c     lPath, errno,
10ba0 20 67 65 74 70 69 64 28 29 29 29 3b 0a 20 20 20   getpid()));.   
10ba1 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
10ba2 5f 49 4f 45 52 52 5f 4c 4f 43 4b 3b 0a 20 20 20  _IOERR_LOCK;.   
10ba3 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 72   }.    len = str
10ba4 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 73 71 6c  lcat(lPath, "sql
10ba5 69 74 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c  iteplocks", maxL
10ba6 65 6e 29 3b 20 20 20 20 0a 20 20 7d 0a 23 20 65  en);    .  }.# e
10ba7 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c  lse.  len = strl
10ba8 63 70 79 28 6c 50 61 74 68 2c 20 22 2f 74 6d 70  cpy(lPath, "/tmp
10ba9 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23 20 65  /", maxLen);.# e
10baa 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20 69  ndif.#endif..  i
10bab 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d 31 5d 21  f( lPath[len-1]!
10bac 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c 65 6e 20  ='/' ){.    len 
10bad 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c  = strlcat(lPath,
10bae 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 20   "/", maxLen);. 
10baf 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72 61 6e 73   }.  .  /* trans
10bb0 66 6f 72 6d 20 74 68 65 20 64 62 20 70 61 74 68  form the db path
10bb1 20 74 6f 20 61 20 75 6e 69 71 75 65 20 63 61 63   to a unique cac
10bb2 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 62 4c  he name */.  dbL
10bb3 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e  en = (int)strlen
10bb4 28 64 62 50 61 74 68 29 3b 0a 20 20 66 6f 72 28  (dbPath);.  for(
10bb5 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e 20 26 26   i=0; i<dbLen &&
10bb6 20 28 69 2b 6c 65 6e 2b 37 29 3c 28 69 6e 74 29   (i+len+7)<(int)
10bb7 6d 61 78 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20  maxLen; i++){.  
10bb8 20 20 63 68 61 72 20 63 20 3d 20 64 62 50 61 74    char c = dbPat
10bb9 68 5b 69 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b  h[i];.    lPath[
10bba 69 2b 6c 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27  i+len] = (c=='/'
10bbb 29 3f 27 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c  )?'_':c;.  }.  l
10bbc 50 61 74 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27  Path[i+len]='\0'
10bbd 3b 0a 20 20 73 74 72 6c 63 61 74 28 6c 50 61 74  ;.  strlcat(lPat
10bbe 68 2c 20 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78  h, ":auto:", max
10bbf 4c 65 6e 29 3b 0a 20 20 4f 53 54 52 41 43 45 28  Len);.  OSTRACE(
10bc0 28 22 47 45 54 4c 4f 43 4b 50 41 54 48 20 20 70  ("GETLOCKPATH  p
10bc1 72 6f 78 79 20 6c 6f 63 6b 20 70 61 74 68 3d 25  roxy lock path=%
10bc2 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c 50 61  s pid=%d\n", lPa
10bc3 74 68 2c 20 67 65 74 70 69 64 28 29 29 29 3b 0a  th, getpid()));.
10bc4 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
10bc5 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 20 2a 2a 20 43  OK;.}../* . ** C
10bc6 72 65 61 74 65 73 20 74 68 65 20 6c 6f 63 6b 20  reates the lock 
10bc7 66 69 6c 65 20 61 6e 64 20 61 6e 79 20 6d 69 73  file and any mis
10bc8 73 69 6e 67 20 64 69 72 65 63 74 6f 72 69 65 73  sing directories
10bc9 20 69 6e 20 6c 6f 63 6b 50 61 74 68 0a 20 2a 2f   in lockPath. */
10bca 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78  .static int prox
10bcb 79 43 72 65 61 74 65 4c 6f 63 6b 50 61 74 68 28  yCreateLockPath(
10bcc 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f 63 6b  const char *lock
10bcd 50 61 74 68 29 7b 0a 20 20 69 6e 74 20 69 2c 20  Path){.  int i, 
10bce 6c 65 6e 3b 0a 20 20 63 68 61 72 20 62 75 66 5b  len;.  char buf[
10bcf 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20 20 69  MAXPATHLEN];.  i
10bd0 6e 74 20 73 74 61 72 74 20 3d 20 30 3b 0a 20 20  nt start = 0;.  
10bd1 0a 20 20 61 73 73 65 72 74 28 6c 6f 63 6b 50 61  .  assert(lockPa
10bd2 74 68 21 3d 4e 55 4c 4c 29 3b 0a 20 20 2f 2a 20  th!=NULL);.  /* 
10bd3 74 72 79 20 74 6f 20 63 72 65 61 74 65 20 61 6c  try to create al
10bd4 6c 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61  l the intermedia
10bd5 74 65 20 64 69 72 65 63 74 6f 72 69 65 73 20 2a  te directories *
10bd6 2f 0a 20 20 6c 65 6e 20 3d 20 28 69 6e 74 29 73  /.  len = (int)s
10bd7 74 72 6c 65 6e 28 6c 6f 63 6b 50 61 74 68 29 3b  trlen(lockPath);
10bd8 0a 20 20 62 75 66 5b 30 5d 20 3d 20 6c 6f 63 6b  .  buf[0] = lock
10bd9 50 61 74 68 5b 30 5d 3b 0a 20 20 66 6f 72 28 20  Path[0];.  for( 
10bda 69 3d 31 3b 20 69 3c 6c 65 6e 3b 20 69 2b 2b 20  i=1; i<len; i++ 
10bdb 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50  ){.    if( lockP
10bdc 61 74 68 5b 69 5d 20 3d 3d 20 27 2f 27 20 26 26  ath[i] == '/' &&
10bdd 20 28 69 20 2d 20 73 74 61 72 74 20 3e 20 30 29   (i - start > 0)
10bde 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 6f 6e 6c   ){.      /* onl
10bdf 79 20 6d 6b 64 69 72 20 69 66 20 6c 65 61 66 20  y mkdir if leaf 
10be0 64 69 72 20 21 3d 20 22 2e 22 20 6f 72 20 22 2f  dir != "." or "/
10be1 22 20 6f 72 20 22 2e 2e 22 20 2a 2f 0a 20 20 20  " or ".." */.   
10be2 20 20 20 69 66 28 20 69 2d 73 74 61 72 74 3e 32     if( i-start>2
10be3 20 7c 7c 20 28 69 2d 73 74 61 72 74 3d 3d 31 20   || (i-start==1 
10be4 26 26 20 62 75 66 5b 73 74 61 72 74 5d 20 21 3d  && buf[start] !=
10be5 20 27 2e 27 20 26 26 20 62 75 66 5b 73 74 61 72   '.' && buf[star
10be6 74 5d 20 21 3d 20 27 2f 27 29 20 0a 20 20 20 20  t] != '/') .    
10be7 20 20 20 20 20 7c 7c 20 28 69 2d 73 74 61 72 74       || (i-start
10be8 3d 3d 32 20 26 26 20 62 75 66 5b 73 74 61 72 74  ==2 && buf[start
10be9 5d 20 21 3d 20 27 2e 27 20 26 26 20 62 75 66 5b  ] != '.' && buf[
10bea 73 74 61 72 74 2b 31 5d 20 21 3d 20 27 2e 27 29  start+1] != '.')
10beb 20 29 7b 0a 20 20 20 20 20 20 20 20 62 75 66 5b   ){.        buf[
10bec 69 5d 3d 27 5c 30 27 3b 0a 20 20 20 20 20 20 20  i]='\0';.       
10bed 20 69 66 28 20 6f 73 4d 6b 64 69 72 28 62 75 66   if( osMkdir(buf
10bee 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  , SQLITE_DEFAULT
10bef 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53  _PROXYDIR_PERMIS
10bf0 53 49 4f 4e 53 29 20 29 7b 0a 20 20 20 20 20 20  SIONS) ){.      
10bf1 20 20 20 20 69 6e 74 20 65 72 72 3d 65 72 72 6e      int err=errn
10bf2 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  o;.          if(
10bf3 20 65 72 72 21 3d 45 45 58 49 53 54 20 29 20 7b   err!=EEXIST ) {
10bf4 0a 20 20 20 20 20 20 20 20 20 20 20 20 4f 53 54  .            OST
10bf5 52 41 43 45 28 28 22 43 52 45 41 54 45 4c 4f 43  RACE(("CREATELOC
10bf6 4b 50 41 54 48 20 20 46 41 49 4c 45 44 20 63 72  KPATH  FAILED cr
10bf7 65 61 74 69 6e 67 20 25 73 2c 20 22 0a 20 20 20  eating %s, ".   
10bf8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10bf9 20 20 22 27 25 73 27 20 70 72 6f 78 79 20 6c 6f    "'%s' proxy lo
10bfa 63 6b 20 70 61 74 68 3d 25 73 20 70 69 64 3d 25  ck path=%s pid=%
10bfb 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  d\n",.          
10bfc 20 20 20 20 20 20 20 20 20 20 20 62 75 66 2c 20             buf, 
10bfd 73 74 72 65 72 72 6f 72 28 65 72 72 29 2c 20 6c  strerror(err), l
10bfe 6f 63 6b 50 61 74 68 2c 20 67 65 74 70 69 64 28  ockPath, getpid(
10bff 29 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  )));.           
10c00 20 72 65 74 75 72 6e 20 65 72 72 3b 0a 20 20 20   return err;.   
10c01 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
10c02 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
10c03 20 73 74 61 72 74 3d 69 2b 31 3b 0a 20 20 20 20   start=i+1;.    
10c04 7d 0a 20 20 20 20 62 75 66 5b 69 5d 20 3d 20 6c  }.    buf[i] = l
10c05 6f 63 6b 50 61 74 68 5b 69 5d 3b 0a 20 20 7d 0a  ockPath[i];.  }.
10c06 20 20 4f 53 54 52 41 43 45 28 28 22 43 52 45 41    OSTRACE(("CREA
10c07 54 45 4c 4f 43 4b 50 41 54 48 20 20 70 72 6f 78  TELOCKPATH  prox
10c08 79 20 6c 6f 63 6b 20 70 61 74 68 3d 25 73 20 70  y lock path=%s p
10c09 69 64 3d 25 64 5c 6e 22 2c 20 6c 6f 63 6b 50 61  id=%d\n", lockPa
10c0a 74 68 2c 20 67 65 74 70 69 64 28 29 29 29 3b 0a  th, getpid()));.
10c0b 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
10c0c 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
10c0d 77 20 56 46 53 20 66 69 6c 65 20 64 65 73 63 72  w VFS file descr
10c0e 69 70 74 6f 72 20 28 73 74 6f 72 65 64 20 69 6e  iptor (stored in
10c0f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
10c10 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33   from.** sqlite3
10c11 5f 6d 61 6c 6c 6f 63 29 20 61 6e 64 20 6f 70 65  _malloc) and ope
10c12 6e 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 64  n the file named
10c13 20 22 70 61 74 68 22 20 69 6e 20 74 68 65 20 66   "path" in the f
10c14 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 0a  ile descriptor..
10c15 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72  **.** The caller
10c16 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
10c17 6e 6f 74 20 6f 6e 6c 79 20 66 6f 72 20 63 6c 6f  not only for clo
10c18 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 64 65  sing the file de
10c19 73 63 72 69 70 74 6f 72 0a 2a 2a 20 62 75 74 20  scriptor.** but 
10c1a 61 6c 73 6f 20 66 6f 72 20 66 72 65 65 69 6e 67  also for freeing
10c1b 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 73 73 6f   the memory asso
10c1c 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
10c1d 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e  file descriptor.
10c1e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
10c1f 72 6f 78 79 43 72 65 61 74 65 55 6e 69 78 46 69  roxyCreateUnixFi
10c20 6c 65 28 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  le(.    const ch
10c21 61 72 20 2a 70 61 74 68 2c 20 20 20 20 20 20 20  ar *path,       
10c22 20 2f 2a 20 70 61 74 68 20 66 6f 72 20 74 68 65   /* path for the
10c23 20 6e 65 77 20 75 6e 69 78 46 69 6c 65 20 2a 2f   new unixFile */
10c24 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 2a  .    unixFile **
10c25 70 70 46 69 6c 65 2c 20 20 20 20 20 20 20 2f 2a  ppFile,       /*
10c26 20 75 6e 69 78 46 69 6c 65 20 63 72 65 61 74 65   unixFile create
10c27 64 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 62  d and returned b
10c28 79 20 72 65 66 20 2a 2f 0a 20 20 20 20 69 6e 74  y ref */.    int
10c29 20 69 73 6c 6f 63 6b 66 69 6c 65 20 20 20 20 20   islockfile     
10c2a 20 20 20 20 20 20 2f 2a 20 69 66 20 6e 6f 6e 20        /* if non 
10c2b 7a 65 72 6f 20 6d 69 73 73 69 6e 67 20 64 69 72  zero missing dir
10c2c 73 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65  s will be create
10c2d 64 20 2a 2f 0a 29 20 7b 0a 20 20 69 6e 74 20 66  d */.) {.  int f
10c2e 64 20 3d 20 2d 31 3b 0a 20 20 75 6e 69 78 46 69  d = -1;.  unixFi
10c2f 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20  le *pNew;.  int 
10c30 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
10c31 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73 20    int openFlags 
10c32 3d 20 4f 5f 52 44 57 52 20 7c 20 4f 5f 43 52 45  = O_RDWR | O_CRE
10c33 41 54 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  AT;.  sqlite3_vf
10c34 73 20 64 75 6d 6d 79 56 66 73 3b 0a 20 20 69 6e  s dummyVfs;.  in
10c35 74 20 74 65 72 72 6e 6f 20 3d 20 30 3b 0a 20 20  t terrno = 0;.  
10c36 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55  UnixUnusedFd *pU
10c37 6e 75 73 65 64 20 3d 20 4e 55 4c 4c 3b 0a 0a 20  nused = NULL;.. 
10c38 20 2f 2a 20 31 2e 20 66 69 72 73 74 20 74 72 79   /* 1. first try
10c39 20 74 6f 20 6f 70 65 6e 2f 63 72 65 61 74 65 20   to open/create 
10c3a 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 32 2e  the file.  ** 2.
10c3b 20 69 66 20 74 68 61 74 20 66 61 69 6c 73 2c 20   if that fails, 
10c3c 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 6c 6f  and this is a lo
10c3d 63 6b 20 66 69 6c 65 20 28 6e 6f 74 2d 63 6f 6e  ck file (not-con
10c3e 63 68 29 2c 20 74 72 79 20 63 72 65 61 74 69 6e  ch), try creatin
10c3f 67 0a 20 20 2a 2a 20 74 68 65 20 70 61 72 65 6e  g.  ** the paren
10c40 74 20 64 69 72 65 63 74 6f 72 69 65 73 20 61 6e  t directories an
10c41 64 20 74 68 65 6e 20 74 72 79 20 61 67 61 69 6e  d then try again
10c42 2e 0a 20 20 2a 2a 20 33 2e 20 69 66 20 74 68 61  ..  ** 3. if tha
10c43 74 20 66 61 69 6c 73 2c 20 74 72 79 20 74 6f 20  t fails, try to 
10c44 6f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 72 65  open the file re
10c45 61 64 2d 6f 6e 6c 79 0a 20 20 2a 2a 20 6f 74 68  ad-only.  ** oth
10c46 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 42 55  erwise return BU
10c47 53 59 20 28 69 66 20 6c 6f 63 6b 20 66 69 6c 65  SY (if lock file
10c48 29 20 6f 72 20 43 41 4e 54 4f 50 45 4e 20 66 6f  ) or CANTOPEN fo
10c49 72 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  r the conch file
10c4a 0a 20 20 2a 2f 0a 20 20 70 55 6e 75 73 65 64 20  .  */.  pUnused 
10c4b 3d 20 66 69 6e 64 52 65 75 73 61 62 6c 65 46 64  = findReusableFd
10c4c 28 70 61 74 68 2c 20 6f 70 65 6e 46 6c 61 67 73  (path, openFlags
10c4d 29 3b 0a 20 20 69 66 28 20 70 55 6e 75 73 65 64  );.  if( pUnused
10c4e 20 29 7b 0a 20 20 20 20 66 64 20 3d 20 70 55 6e   ){.    fd = pUn
10c4f 75 73 65 64 2d 3e 66 64 3b 0a 20 20 7d 65 6c 73  used->fd;.  }els
10c50 65 7b 0a 20 20 20 20 70 55 6e 75 73 65 64 20 3d  e{.    pUnused =
10c51 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
10c52 73 69 7a 65 6f 66 28 2a 70 55 6e 75 73 65 64 29  sizeof(*pUnused)
10c53 29 3b 0a 20 20 20 20 69 66 28 20 21 70 55 6e 75  );.    if( !pUnu
10c54 73 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74  sed ){.      ret
10c55 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
10c56 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
10c57 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 66 64  ( fd<0 ){.    fd
10c58 20 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 70   = robust_open(p
10c59 61 74 68 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20  ath, openFlags, 
10c5a 30 29 3b 0a 20 20 20 20 74 65 72 72 6e 6f 20 3d  0);.    terrno =
10c5b 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20   errno;.    if( 
10c5c 66 64 3c 30 20 26 26 20 65 72 72 6e 6f 3d 3d 45  fd<0 && errno==E
10c5d 4e 4f 45 4e 54 20 26 26 20 69 73 6c 6f 63 6b 66  NOENT && islockf
10c5e 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ile ){.      if(
10c5f 20 70 72 6f 78 79 43 72 65 61 74 65 4c 6f 63 6b   proxyCreateLock
10c60 50 61 74 68 28 70 61 74 68 29 20 3d 3d 20 53 51  Path(path) == SQ
10c61 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
10c62 20 20 20 66 64 20 3d 20 72 6f 62 75 73 74 5f 6f     fd = robust_o
10c63 70 65 6e 28 70 61 74 68 2c 20 6f 70 65 6e 46 6c  pen(path, openFl
10c64 61 67 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  ags, 0);.      }
10c65 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
10c66 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 6f 70 65   fd<0 ){.    ope
10c67 6e 46 6c 61 67 73 20 3d 20 4f 5f 52 44 4f 4e 4c  nFlags = O_RDONL
10c68 59 3b 0a 20 20 20 20 66 64 20 3d 20 72 6f 62 75  Y;.    fd = robu
10c69 73 74 5f 6f 70 65 6e 28 70 61 74 68 2c 20 6f 70  st_open(path, op
10c6a 65 6e 46 6c 61 67 73 2c 20 30 29 3b 0a 20 20 20  enFlags, 0);.   
10c6b 20 74 65 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   terrno = errno;
10c6c 0a 20 20 7d 0a 20 20 69 66 28 20 66 64 3c 30 20  .  }.  if( fd<0 
10c6d 29 7b 0a 20 20 20 20 69 66 28 20 69 73 6c 6f 63  ){.    if( isloc
10c6e 6b 66 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 72  kfile ){.      r
10c6f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
10c70 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 77 69  Y;.    }.    swi
10c71 74 63 68 20 28 74 65 72 72 6e 6f 29 20 7b 0a 20  tch (terrno) {. 
10c72 20 20 20 20 20 63 61 73 65 20 45 41 43 43 45 53       case EACCES
10c73 3a 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  :.        return
10c74 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20   SQLITE_PERM;.  
10c75 20 20 20 20 63 61 73 65 20 45 49 4f 3a 20 0a 20      case EIO: . 
10c76 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
10c77 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 3b  LITE_IOERR_LOCK;
10c78 20 2f 2a 20 65 76 65 6e 20 74 68 6f 75 67 68 20   /* even though 
10c79 69 74 20 69 73 20 74 68 65 20 63 6f 6e 63 68 20  it is the conch 
10c7a 2a 2f 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74  */.      default
10c7b 3a 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  :.        return
10c7c 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
10c7d 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 7d  _BKPT;.    }.  }
10c7e 0a 20 20 0a 20 20 70 4e 65 77 20 3d 20 28 75 6e  .  .  pNew = (un
10c7f 69 78 46 69 6c 65 20 2a 29 73 71 6c 69 74 65 33  ixFile *)sqlite3
10c80 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a  _malloc(sizeof(*
10c81 70 4e 65 77 29 29 3b 0a 20 20 69 66 28 20 70 4e  pNew));.  if( pN
10c82 65 77 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ew==NULL ){.    
10c83 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
10c84 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  M;.    goto end_
10c85 63 72 65 61 74 65 5f 70 72 6f 78 79 3b 0a 20 20  create_proxy;.  
10c86 7d 0a 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c  }.  memset(pNew,
10c87 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46   0, sizeof(unixF
10c88 69 6c 65 29 29 3b 0a 20 20 70 4e 65 77 2d 3e 6f  ile));.  pNew->o
10c89 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 65 6e 46  penFlags = openF
10c8a 6c 61 67 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26  lags;.  memset(&
10c8b 64 75 6d 6d 79 56 66 73 2c 20 30 2c 20 73 69 7a  dummyVfs, 0, siz
10c8c 65 6f 66 28 64 75 6d 6d 79 56 66 73 29 29 3b 0a  eof(dummyVfs));.
10c8d 20 20 64 75 6d 6d 79 56 66 73 2e 70 41 70 70 44    dummyVfs.pAppD
10c8e 61 74 61 20 3d 20 28 76 6f 69 64 2a 29 26 61 75  ata = (void*)&au
10c8f 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 3b 0a  tolockIoFinder;.
10c90 20 20 64 75 6d 6d 79 56 66 73 2e 7a 4e 61 6d 65    dummyVfs.zName
10c91 20 3d 20 22 64 75 6d 6d 79 22 3b 0a 20 20 70 55   = "dummy";.  pU
10c92 6e 75 73 65 64 2d 3e 66 64 20 3d 20 66 64 3b 0a  nused->fd = fd;.
10c93 20 20 70 55 6e 75 73 65 64 2d 3e 66 6c 61 67 73    pUnused->flags
10c94 20 3d 20 6f 70 65 6e 46 6c 61 67 73 3b 0a 20 20   = openFlags;.  
10c95 70 4e 65 77 2d 3e 70 55 6e 75 73 65 64 20 3d 20  pNew->pUnused = 
10c96 70 55 6e 75 73 65 64 3b 0a 20 20 0a 20 20 72 63  pUnused;.  .  rc
10c97 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c   = fillInUnixFil
10c98 65 28 26 64 75 6d 6d 79 56 66 73 2c 20 66 64 2c  e(&dummyVfs, fd,
10c99 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29   (sqlite3_file*)
10c9a 70 4e 65 77 2c 20 70 61 74 68 2c 20 30 29 3b 0a  pNew, path, 0);.
10c9b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
10c9c 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 70 46 69  _OK ){.    *ppFi
10c9d 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 72  le = pNew;.    r
10c9e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
10c9f 0a 20 20 7d 0a 65 6e 64 5f 63 72 65 61 74 65 5f  .  }.end_create_
10ca0 70 72 6f 78 79 3a 20 20 20 20 0a 20 20 72 6f 62  proxy:    .  rob
10ca1 75 73 74 5f 63 6c 6f 73 65 28 70 4e 65 77 2c 20  ust_close(pNew, 
10ca2 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b 0a 20  fd, __LINE__);. 
10ca3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e   sqlite3_free(pN
10ca4 65 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ew);.  sqlite3_f
10ca5 72 65 65 28 70 55 6e 75 73 65 64 29 3b 0a 20 20  ree(pUnused);.  
10ca6 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69  return rc;.}..#i
10ca7 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
10ca8 0a 2f 2a 20 73 69 6d 75 6c 61 74 65 20 6d 75 6c  ./* simulate mul
10ca9 74 69 70 6c 65 20 68 6f 73 74 73 20 62 79 20 63  tiple hosts by c
10caa 72 65 61 74 69 6e 67 20 75 6e 69 71 75 65 20 68  reating unique h
10cab 6f 73 74 69 64 20 66 69 6c 65 20 70 61 74 68 73  ostid file paths
10cac 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
10cad 6e 74 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69  nt sqlite3_hosti
10cae 64 5f 6e 75 6d 20 3d 20 30 3b 0a 23 65 6e 64 69  d_num = 0;.#endi
10caf 66 0a 0a 23 64 65 66 69 6e 65 20 50 52 4f 58 59  f..#define PROXY
10cb0 5f 48 4f 53 54 49 44 4c 45 4e 20 20 20 20 31 36  _HOSTIDLEN    16
10cb1 20 20 2f 2a 20 63 6f 6e 63 68 20 66 69 6c 65 20    /* conch file 
10cb2 68 6f 73 74 20 69 64 20 6c 65 6e 67 74 68 20 2a  host id length *
10cb3 2f 0a 0a 2f 2a 20 4e 6f 74 20 61 6c 77 61 79 73  /../* Not always
10cb4 20 64 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20   defined in the 
10cb5 68 65 61 64 65 72 73 20 61 73 20 69 74 20 6f 75  headers as it ou
10cb6 67 68 74 20 74 6f 20 62 65 20 2a 2f 0a 65 78 74  ght to be */.ext
10cb7 65 72 6e 20 69 6e 74 20 67 65 74 68 6f 73 74 75  ern int gethostu
10cb8 75 69 64 28 75 75 69 64 5f 74 20 69 64 2c 20 63  uid(uuid_t id, c
10cb9 6f 6e 73 74 20 73 74 72 75 63 74 20 74 69 6d 65  onst struct time
10cba 73 70 65 63 20 2a 77 61 69 74 29 3b 0a 0a 2f 2a  spec *wait);../*
10cbb 20 67 65 74 20 74 68 65 20 68 6f 73 74 20 49 44   get the host ID
10cbc 20 76 69 61 20 67 65 74 68 6f 73 74 75 75 69 64   via gethostuuid
10cbd 28 29 2c 20 70 48 6f 73 74 49 44 20 6d 75 73 74  (), pHostID must
10cbe 20 70 6f 69 6e 74 20 74 6f 20 50 52 4f 58 59 5f   point to PROXY_
10cbf 48 4f 53 54 49 44 4c 45 4e 20 0a 2a 2a 20 62 79  HOSTIDLEN .** by
10cc0 74 65 73 20 6f 66 20 77 72 69 74 61 62 6c 65 20  tes of writable 
10cc1 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  memory..*/.stati
10cc2 63 20 69 6e 74 20 70 72 6f 78 79 47 65 74 48 6f  c int proxyGetHo
10cc3 73 74 49 44 28 75 6e 73 69 67 6e 65 64 20 63 68  stID(unsigned ch
10cc4 61 72 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e 74  ar *pHostID, int
10cc5 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20 61 73 73   *pError){.  ass
10cc6 65 72 74 28 50 52 4f 58 59 5f 48 4f 53 54 49 44  ert(PROXY_HOSTID
10cc7 4c 45 4e 20 3d 3d 20 73 69 7a 65 6f 66 28 75 75  LEN == sizeof(uu
10cc8 69 64 5f 74 29 29 3b 0a 20 20 6d 65 6d 73 65 74  id_t));.  memset
10cc9 28 70 48 6f 73 74 49 44 2c 20 30 2c 20 50 52 4f  (pHostID, 0, PRO
10cca 58 59 5f 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 23  XY_HOSTIDLEN);.#
10ccb 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 4d 41 58  if defined(__MAX
10ccc 5f 4f 53 5f 58 5f 56 45 52 53 49 4f 4e 5f 4d 49  _OS_X_VERSION_MI
10ccd 4e 5f 52 45 51 55 49 52 45 44 29 5c 0a 20 20 20  N_REQUIRED)\.   
10cce 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 5f              && _
10ccf 5f 4d 41 43 5f 4f 53 5f 58 5f 56 45 52 53 49 4f  _MAC_OS_X_VERSIO
10cd0 4e 5f 4d 49 4e 5f 52 45 51 55 49 52 45 44 3c 31  N_MIN_REQUIRED<1
10cd1 30 35 30 0a 20 20 7b 0a 20 20 20 20 73 74 61 74  050.  {.    stat
10cd2 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  ic const struct 
10cd3 74 69 6d 65 73 70 65 63 20 74 69 6d 65 6f 75 74  timespec timeout
10cd4 20 3d 20 7b 31 2c 20 30 7d 3b 20 2f 2a 20 31 20   = {1, 0}; /* 1 
10cd5 73 65 63 20 74 69 6d 65 6f 75 74 20 2a 2f 0a 20  sec timeout */. 
10cd6 20 20 20 69 66 28 20 67 65 74 68 6f 73 74 75 75     if( gethostuu
10cd7 69 64 28 70 48 6f 73 74 49 44 2c 20 26 74 69 6d  id(pHostID, &tim
10cd8 65 6f 75 74 29 20 29 7b 0a 20 20 20 20 20 20 69  eout) ){.      i
10cd9 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a  nt err = errno;.
10cda 20 20 20 20 20 20 69 66 28 20 70 45 72 72 6f 72        if( pError
10cdb 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 45 72   ){.        *pEr
10cdc 72 6f 72 20 3d 20 65 72 72 3b 0a 20 20 20 20 20  ror = err;.     
10cdd 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   }.      return 
10cde 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20  SQLITE_IOERR;.  
10cdf 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20    }.  }.#else.  
10ce0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
10ce1 28 70 45 72 72 6f 72 29 3b 0a 23 65 6e 64 69 66  (pError);.#endif
10ce2 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
10ce3 45 53 54 0a 20 20 2f 2a 20 73 69 6d 75 6c 61 74  EST.  /* simulat
10ce4 65 20 6d 75 6c 74 69 70 6c 65 20 68 6f 73 74 73  e multiple hosts
10ce5 20 62 79 20 63 72 65 61 74 69 6e 67 20 75 6e 69   by creating uni
10ce6 71 75 65 20 68 6f 73 74 69 64 20 66 69 6c 65 20  que hostid file 
10ce7 70 61 74 68 73 20 2a 2f 0a 20 20 69 66 28 20 73  paths */.  if( s
10ce8 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75  qlite3_hostid_nu
10ce9 6d 20 21 3d 20 30 29 7b 0a 20 20 20 20 70 48 6f  m != 0){.    pHo
10cea 73 74 49 44 5b 30 5d 20 3d 20 28 63 68 61 72 29  stID[0] = (char)
10ceb 28 70 48 6f 73 74 49 44 5b 30 5d 20 2b 20 28 63  (pHostID[0] + (c
10cec 68 61 72 29 28 73 71 6c 69 74 65 33 5f 68 6f 73  har)(sqlite3_hos
10ced 74 69 64 5f 6e 75 6d 20 26 20 30 78 46 46 29 29  tid_num & 0xFF))
10cee 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a  ;.  }.#endif.  .
10cef 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
10cf0 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 63 6f  OK;.}../* The co
10cf1 6e 63 68 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  nch file contain
10cf2 73 20 74 68 65 20 68 65 61 64 65 72 2c 20 68 6f  s the header, ho
10cf3 73 74 20 69 64 20 61 6e 64 20 6c 6f 63 6b 20 66  st id and lock f
10cf4 69 6c 65 20 70 61 74 68 0a 20 2a 2f 0a 23 64 65  ile path. */.#de
10cf5 66 69 6e 65 20 50 52 4f 58 59 5f 43 4f 4e 43 48  fine PROXY_CONCH
10cf6 56 45 52 53 49 4f 4e 20 32 20 20 20 2f 2a 20 31  VERSION 2   /* 1
10cf7 2d 62 79 74 65 20 68 65 61 64 65 72 2c 20 31 36  -byte header, 16
10cf8 2d 62 79 74 65 20 68 6f 73 74 20 69 64 2c 20 70  -byte host id, p
10cf9 61 74 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ath */.#define P
10cfa 52 4f 58 59 5f 48 45 41 44 45 52 4c 45 4e 20 20  ROXY_HEADERLEN  
10cfb 20 20 31 20 20 20 2f 2a 20 63 6f 6e 63 68 20 66    1   /* conch f
10cfc 69 6c 65 20 68 65 61 64 65 72 20 6c 65 6e 67 74  ile header lengt
10cfd 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 52 4f  h */.#define PRO
10cfe 58 59 5f 50 41 54 48 49 4e 44 45 58 20 20 20 20  XY_PATHINDEX    
10cff 28 50 52 4f 58 59 5f 48 45 41 44 45 52 4c 45 4e  (PROXY_HEADERLEN
10d00 2b 50 52 4f 58 59 5f 48 4f 53 54 49 44 4c 45 4e  +PROXY_HOSTIDLEN
10d01 29 0a 23 64 65 66 69 6e 65 20 50 52 4f 58 59 5f  ).#define PROXY_
10d02 4d 41 58 43 4f 4e 43 48 4c 45 4e 20 20 28 50 52  MAXCONCHLEN  (PR
10d03 4f 58 59 5f 48 45 41 44 45 52 4c 45 4e 2b 50 52  OXY_HEADERLEN+PR
10d04 4f 58 59 5f 48 4f 53 54 49 44 4c 45 4e 2b 4d 41  OXY_HOSTIDLEN+MA
10d05 58 50 41 54 48 4c 45 4e 29 0a 0a 2f 2a 20 0a 2a  XPATHLEN)../* .*
10d06 2a 20 54 61 6b 65 73 20 61 6e 20 6f 70 65 6e 20  * Takes an open 
10d07 63 6f 6e 63 68 20 66 69 6c 65 2c 20 63 6f 70 69  conch file, copi
10d08 65 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  es the contents 
10d09 74 6f 20 61 20 6e 65 77 20 70 61 74 68 20 61 6e  to a new path an
10d0a 64 20 74 68 65 6e 20 6d 6f 76 65 73 20 0a 2a 2a  d then moves .**
10d0b 20 69 74 20 62 61 63 6b 2e 20 20 54 68 65 20 6e   it back.  The n
10d0c 65 77 6c 79 20 63 72 65 61 74 65 64 20 66 69 6c  ewly created fil
10d0d 65 27 73 20 66 69 6c 65 20 64 65 73 63 72 69 70  e's file descrip
10d0e 74 6f 72 20 69 73 20 61 73 73 69 67 6e 65 64 20  tor is assigned 
10d0f 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e 63 68 20  to the.** conch 
10d10 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 61  file structure a
10d11 6e 64 20 66 69 6e 61 6c 6c 79 20 74 68 65 20 6f  nd finally the o
10d12 72 69 67 69 6e 61 6c 20 63 6f 6e 63 68 20 66 69  riginal conch fi
10d13 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
10d14 20 0a 2a 2a 20 63 6c 6f 73 65 64 2e 20 20 52 65   .** closed.  Re
10d15 74 75 72 6e 73 20 7a 65 72 6f 20 69 66 20 73 75  turns zero if su
10d16 63 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a 73 74 61  ccessful..*/.sta
10d17 74 69 63 20 69 6e 74 20 70 72 6f 78 79 42 72 65  tic int proxyBre
10d18 61 6b 43 6f 6e 63 68 4c 6f 63 6b 28 75 6e 69 78  akConchLock(unix
10d19 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 75 75 69  File *pFile, uui
10d1a 64 5f 74 20 6d 79 48 6f 73 74 49 44 29 7b 0a 20  d_t myHostID){. 
10d1b 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
10d1c 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72  text *pCtx = (pr
10d1d 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
10d1e 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  t *)pFile->locki
10d1f 6e 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 75 6e  ngContext; .  un
10d20 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c  ixFile *conchFil
10d21 65 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46  e = pCtx->conchF
10d22 69 6c 65 3b 0a 20 20 63 68 61 72 20 74 50 61 74  ile;.  char tPat
10d23 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20  h[MAXPATHLEN];. 
10d24 20 63 68 61 72 20 62 75 66 5b 50 52 4f 58 59 5f   char buf[PROXY_
10d25 4d 41 58 43 4f 4e 43 48 4c 45 4e 5d 3b 0a 20 20  MAXCONCHLEN];.  
10d26 63 68 61 72 20 2a 63 50 61 74 68 20 3d 20 70 43  char *cPath = pC
10d27 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74  tx->conchFilePat
10d28 68 3b 0a 20 20 73 69 7a 65 5f 74 20 72 65 61 64  h;.  size_t read
10d29 4c 65 6e 20 3d 20 30 3b 0a 20 20 73 69 7a 65 5f  Len = 0;.  size_
10d2a 74 20 70 61 74 68 4c 65 6e 20 3d 20 30 3b 0a 20  t pathLen = 0;. 
10d2b 20 63 68 61 72 20 65 72 72 6d 73 67 5b 36 34 5d   char errmsg[64]
10d2c 20 3d 20 22 22 3b 0a 20 20 69 6e 74 20 66 64 20   = "";.  int fd 
10d2d 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 72 63 20 3d  = -1;.  int rc =
10d2e 20 2d 31 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41   -1;.  UNUSED_PA
10d2f 52 41 4d 45 54 45 52 28 6d 79 48 6f 73 74 49 44  RAMETER(myHostID
10d30 29 3b 0a 0a 20 20 2f 2a 20 63 72 65 61 74 65 20  );..  /* create 
10d31 61 20 6e 65 77 20 70 61 74 68 20 62 79 20 72 65  a new path by re
10d32 70 6c 61 63 65 20 74 68 65 20 74 72 61 69 6c 69  place the traili
10d33 6e 67 20 27 2d 63 6f 6e 63 68 27 20 77 69 74 68  ng '-conch' with
10d34 20 27 2d 62 72 65 61 6b 27 20 2a 2f 0a 20 20 70   '-break' */.  p
10d35 61 74 68 4c 65 6e 20 3d 20 73 74 72 6c 63 70 79  athLen = strlcpy
10d36 28 74 50 61 74 68 2c 20 63 50 61 74 68 2c 20 4d  (tPath, cPath, M
10d37 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 69 66  AXPATHLEN);.  if
10d38 28 20 70 61 74 68 4c 65 6e 3e 4d 41 58 50 41 54  ( pathLen>MAXPAT
10d39 48 4c 45 4e 20 7c 7c 20 70 61 74 68 4c 65 6e 3c  HLEN || pathLen<
10d3a 36 20 7c 7c 20 0a 20 20 20 20 20 28 73 74 72 6c  6 || .     (strl
10d3b 63 70 79 28 26 74 50 61 74 68 5b 70 61 74 68 4c  cpy(&tPath[pathL
10d3c 65 6e 2d 35 5d 2c 20 22 62 72 65 61 6b 22 2c 20  en-5], "break", 
10d3d 36 29 20 21 3d 20 35 29 20 29 7b 0a 20 20 20 20  6) != 5) ){.    
10d3e 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
10d3f 28 73 69 7a 65 6f 66 28 65 72 72 6d 73 67 29 2c  (sizeof(errmsg),
10d40 65 72 72 6d 73 67 2c 22 70 61 74 68 20 65 72 72  errmsg,"path err
10d41 6f 72 20 28 6c 65 6e 20 25 64 29 22 2c 28 69 6e  or (len %d)",(in
10d42 74 29 70 61 74 68 4c 65 6e 29 3b 0a 20 20 20 20  t)pathLen);.    
10d43 67 6f 74 6f 20 65 6e 64 5f 62 72 65 61 6b 6c 6f  goto end_breaklo
10d44 63 6b 3b 0a 20 20 7d 0a 20 20 2f 2a 20 72 65 61  ck;.  }.  /* rea
10d45 64 20 74 68 65 20 63 6f 6e 63 68 20 63 6f 6e 74  d the conch cont
10d46 65 6e 74 20 2a 2f 0a 20 20 72 65 61 64 4c 65 6e  ent */.  readLen
10d47 20 3d 20 6f 73 50 72 65 61 64 28 63 6f 6e 63 68   = osPread(conch
10d48 46 69 6c 65 2d 3e 68 2c 20 62 75 66 2c 20 50 52  File->h, buf, PR
10d49 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45 4e 2c  OXY_MAXCONCHLEN,
10d4a 20 30 29 3b 0a 20 20 69 66 28 20 72 65 61 64 4c   0);.  if( readL
10d4b 65 6e 3c 50 52 4f 58 59 5f 50 41 54 48 49 4e 44  en<PROXY_PATHIND
10d4c 45 58 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  EX ){.    sqlite
10d4d 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
10d4e 66 28 65 72 72 6d 73 67 29 2c 65 72 72 6d 73 67  f(errmsg),errmsg
10d4f 2c 22 72 65 61 64 20 65 72 72 6f 72 20 28 6c 65  ,"read error (le
10d50 6e 20 25 64 29 22 2c 28 69 6e 74 29 72 65 61 64  n %d)",(int)read
10d51 4c 65 6e 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  Len);.    goto e
10d52 6e 64 5f 62 72 65 61 6b 6c 6f 63 6b 3b 0a 20 20  nd_breaklock;.  
10d53 7d 0a 20 20 2f 2a 20 77 72 69 74 65 20 69 74 20  }.  /* write it 
10d54 6f 75 74 20 74 6f 20 74 68 65 20 74 65 6d 70 6f  out to the tempo
10d55 72 61 72 79 20 62 72 65 61 6b 20 66 69 6c 65 20  rary break file 
10d56 2a 2f 0a 20 20 66 64 20 3d 20 72 6f 62 75 73 74  */.  fd = robust
10d57 5f 6f 70 65 6e 28 74 50 61 74 68 2c 20 28 4f 5f  _open(tPath, (O_
10d58 52 44 57 52 7c 4f 5f 43 52 45 41 54 7c 4f 5f 45  RDWR|O_CREAT|O_E
10d59 58 43 4c 29 2c 20 30 29 3b 0a 20 20 69 66 28 20  XCL), 0);.  if( 
10d5a 66 64 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  fd<0 ){.    sqli
10d5b 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
10d5c 65 6f 66 28 65 72 72 6d 73 67 29 2c 20 65 72 72  eof(errmsg), err
10d5d 6d 73 67 2c 20 22 63 72 65 61 74 65 20 66 61 69  msg, "create fai
10d5e 6c 65 64 20 28 25 64 29 22 2c 20 65 72 72 6e 6f  led (%d)", errno
10d5f 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  );.    goto end_
10d60 62 72 65 61 6b 6c 6f 63 6b 3b 0a 20 20 7d 0a 20  breaklock;.  }. 
10d61 20 69 66 28 20 6f 73 50 77 72 69 74 65 28 66 64   if( osPwrite(fd
10d62 2c 20 62 75 66 2c 20 72 65 61 64 4c 65 6e 2c 20  , buf, readLen, 
10d63 30 29 20 21 3d 20 28 73 73 69 7a 65 5f 74 29 72  0) != (ssize_t)r
10d64 65 61 64 4c 65 6e 20 29 7b 0a 20 20 20 20 73 71  eadLen ){.    sq
10d65 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
10d66 69 7a 65 6f 66 28 65 72 72 6d 73 67 29 2c 20 65  izeof(errmsg), e
10d67 72 72 6d 73 67 2c 20 22 77 72 69 74 65 20 66 61  rrmsg, "write fa
10d68 69 6c 65 64 20 28 25 64 29 22 2c 20 65 72 72 6e  iled (%d)", errn
10d69 6f 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64  o);.    goto end
10d6a 5f 62 72 65 61 6b 6c 6f 63 6b 3b 0a 20 20 7d 0a  _breaklock;.  }.
10d6b 20 20 69 66 28 20 72 65 6e 61 6d 65 28 74 50 61    if( rename(tPa
10d6c 74 68 2c 20 63 50 61 74 68 29 20 29 7b 0a 20 20  th, cPath) ){.  
10d6d 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
10d6e 74 66 28 73 69 7a 65 6f 66 28 65 72 72 6d 73 67  tf(sizeof(errmsg
10d6f 29 2c 20 65 72 72 6d 73 67 2c 20 22 72 65 6e 61  ), errmsg, "rena
10d70 6d 65 20 66 61 69 6c 65 64 20 28 25 64 29 22 2c  me failed (%d)",
10d71 20 65 72 72 6e 6f 29 3b 0a 20 20 20 20 67 6f 74   errno);.    got
10d72 6f 20 65 6e 64 5f 62 72 65 61 6b 6c 6f 63 6b 3b  o end_breaklock;
10d73 0a 20 20 7d 0a 20 20 72 63 20 3d 20 30 3b 0a 20  .  }.  rc = 0;. 
10d74 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
10d75 20 22 62 72 6f 6b 65 20 73 74 61 6c 65 20 6c 6f   "broke stale lo
10d76 63 6b 20 6f 6e 20 25 73 5c 6e 22 2c 20 63 50 61  ck on %s\n", cPa
10d77 74 68 29 3b 0a 20 20 72 6f 62 75 73 74 5f 63 6c  th);.  robust_cl
10d78 6f 73 65 28 70 46 69 6c 65 2c 20 63 6f 6e 63 68  ose(pFile, conch
10d79 46 69 6c 65 2d 3e 68 2c 20 5f 5f 4c 49 4e 45 5f  File->h, __LINE_
10d7a 5f 29 3b 0a 20 20 63 6f 6e 63 68 46 69 6c 65 2d  _);.  conchFile-
10d7b 3e 68 20 3d 20 66 64 3b 0a 20 20 63 6f 6e 63 68  >h = fd;.  conch
10d7c 46 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 20  File->openFlags 
10d7d 3d 20 4f 5f 52 44 57 52 20 7c 20 4f 5f 43 52 45  = O_RDWR | O_CRE
10d7e 41 54 3b 0a 0a 65 6e 64 5f 62 72 65 61 6b 6c 6f  AT;..end_breaklo
10d7f 63 6b 3a 0a 20 20 69 66 28 20 72 63 20 29 7b 0a  ck:.  if( rc ){.
10d80 20 20 20 20 69 66 28 20 66 64 3e 3d 30 20 29 7b      if( fd>=0 ){
10d81 0a 20 20 20 20 20 20 6f 73 55 6e 6c 69 6e 6b 28  .      osUnlink(
10d82 74 50 61 74 68 29 3b 0a 20 20 20 20 20 20 72 6f  tPath);.      ro
10d83 62 75 73 74 5f 63 6c 6f 73 65 28 70 46 69 6c 65  bust_close(pFile
10d84 2c 20 66 64 2c 20 5f 5f 4c 49 4e 45 5f 5f 29 3b  , fd, __LINE__);
10d85 0a 20 20 20 20 7d 0a 20 20 20 20 66 70 72 69 6e  .    }.    fprin
10d86 74 66 28 73 74 64 65 72 72 2c 20 22 66 61 69 6c  tf(stderr, "fail
10d87 65 64 20 74 6f 20 62 72 65 61 6b 20 73 74 61 6c  ed to break stal
10d88 65 20 6c 6f 63 6b 20 6f 6e 20 25 73 2c 20 25 73  e lock on %s, %s
10d89 5c 6e 22 2c 20 63 50 61 74 68 2c 20 65 72 72 6d  \n", cPath, errm
10d8a 73 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  sg);.  }.  retur
10d8b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 61 6b 65  n rc;.}../* Take
10d8c 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
10d8d 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 68  ock on the conch
10d8e 20 66 69 6c 65 20 61 6e 64 20 62 72 65 61 6b 20   file and break 
10d8f 61 20 73 74 61 6c 65 20 6c 6f 63 6b 20 69 66 20  a stale lock if 
10d90 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 69 64 20  the .** host id 
10d91 6d 61 74 63 68 65 73 2e 0a 2a 2f 0a 73 74 61 74  matches..*/.stat
10d92 69 63 20 69 6e 74 20 70 72 6f 78 79 43 6f 6e 63  ic int proxyConc
10d93 68 4c 6f 63 6b 28 75 6e 69 78 46 69 6c 65 20 2a  hLock(unixFile *
10d94 70 46 69 6c 65 2c 20 75 75 69 64 5f 74 20 6d 79  pFile, uuid_t my
10d95 48 6f 73 74 49 44 2c 20 69 6e 74 20 6c 6f 63 6b  HostID, int lock
10d96 54 79 70 65 29 7b 0a 20 20 70 72 6f 78 79 4c 6f  Type){.  proxyLo
10d97 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
10d98 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69  tx = (proxyLocki
10d99 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c  ngContext *)pFil
10d9a 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
10d9b 74 3b 20 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  t; .  unixFile *
10d9c 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78  conchFile = pCtx
10d9d 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 69  ->conchFile;.  i
10d9e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
10d9f 4b 3b 0a 20 20 69 6e 74 20 6e 54 72 69 65 73 20  K;.  int nTries 
10da0 3d 20 30 3b 0a 20 20 73 74 72 75 63 74 20 74 69  = 0;.  struct ti
10da1 6d 65 73 70 65 63 20 63 6f 6e 63 68 4d 6f 64 54  mespec conchModT
10da2 69 6d 65 3b 0a 20 20 0a 20 20 6d 65 6d 73 65 74  ime;.  .  memset
10da3 28 26 63 6f 6e 63 68 4d 6f 64 54 69 6d 65 2c 20  (&conchModTime, 
10da4 30 2c 20 73 69 7a 65 6f 66 28 63 6f 6e 63 68 4d  0, sizeof(conchM
10da5 6f 64 54 69 6d 65 29 29 3b 0a 20 20 64 6f 20 7b  odTime));.  do {
10da6 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46  .    rc = conchF
10da7 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c  ile->pMethod->xL
10da8 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock((sqlite3_fil
10da9 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 6c 6f  e*)conchFile, lo
10daa 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 6e 54 72  ckType);.    nTr
10dab 69 65 73 20 2b 2b 3b 0a 20 20 20 20 69 66 28 20  ies ++;.    if( 
10dac 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
10dad 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  ){.      /* If t
10dae 68 65 20 6c 6f 63 6b 20 66 61 69 6c 65 64 20 28  he lock failed (
10daf 62 75 73 79 29 3a 0a 20 20 20 20 20 20 20 2a 20  busy):.       * 
10db0 31 73 74 20 74 72 79 3a 20 67 65 74 20 74 68 65  1st try: get the
10db1 20 6d 6f 64 20 74 69 6d 65 20 6f 66 20 74 68 65   mod time of the
10db2 20 63 6f 6e 63 68 2c 20 77 61 69 74 20 30 2e 35   conch, wait 0.5
10db3 73 20 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e  s and try again.
10db4 20 0a 20 20 20 20 20 20 20 2a 20 32 6e 64 20 74   .       * 2nd t
10db5 72 79 3a 20 66 61 69 6c 20 69 66 20 74 68 65 20  ry: fail if the 
10db6 6d 6f 64 20 74 69 6d 65 20 63 68 61 6e 67 65 64  mod time changed
10db7 20 6f 72 20 68 6f 73 74 20 69 64 20 69 73 20 64   or host id is d
10db8 69 66 66 65 72 65 6e 74 2c 20 77 61 69 74 20 0a  ifferent, wait .
10db9 20 20 20 20 20 20 20 2a 20 20 20 20 20 20 20 20         *        
10dba 20 20 20 31 30 20 73 65 63 20 61 6e 64 20 74 72     10 sec and tr
10dbb 79 20 61 67 61 69 6e 0a 20 20 20 20 20 20 20 2a  y again.       *
10dbc 20 33 72 64 20 74 72 79 3a 20 62 72 65 61 6b 20   3rd try: break 
10dbd 74 68 65 20 6c 6f 63 6b 20 75 6e 6c 65 73 73 20  the lock unless 
10dbe 74 68 65 20 6d 6f 64 20 74 69 6d 65 20 68 61 73  the mod time has
10dbf 20 63 68 61 6e 67 65 64 2e 0a 20 20 20 20 20 20   changed..      
10dc0 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74   */.      struct
10dc1 20 73 74 61 74 20 62 75 66 3b 0a 20 20 20 20 20   stat buf;.     
10dc2 20 69 66 28 20 6f 73 46 73 74 61 74 28 63 6f 6e   if( osFstat(con
10dc3 63 68 46 69 6c 65 2d 3e 68 2c 20 26 62 75 66 29  chFile->h, &buf)
10dc4 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
10dc5 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  e->lastErrno = e
10dc6 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65  rrno;.        re
10dc7 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
10dc8 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d 0a  R_LOCK;.      }.
10dc9 20 20 20 20 20 20 0a 20 20 20 20 20 20 69 66 28        .      if(
10dca 20 6e 54 72 69 65 73 3d 3d 31 20 29 7b 0a 20 20   nTries==1 ){.  
10dcb 20 20 20 20 20 20 63 6f 6e 63 68 4d 6f 64 54 69        conchModTi
10dcc 6d 65 20 3d 20 62 75 66 2e 73 74 5f 6d 74 69 6d  me = buf.st_mtim
10dcd 65 73 70 65 63 3b 0a 20 20 20 20 20 20 20 20 75  espec;.        u
10dce 73 6c 65 65 70 28 35 30 30 30 30 30 29 3b 20 2f  sleep(500000); /
10dcf 2a 20 77 61 69 74 20 30 2e 35 20 73 65 63 20 61  * wait 0.5 sec a
10dd0 6e 64 20 74 72 79 20 74 68 65 20 6c 6f 63 6b 20  nd try the lock 
10dd1 61 67 61 69 6e 2a 2f 0a 20 20 20 20 20 20 20 20  again*/.        
10dd2 63 6f 6e 74 69 6e 75 65 3b 20 20 0a 20 20 20 20  continue;  .    
10dd3 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72    }..      asser
10dd4 74 28 20 6e 54 72 69 65 73 3e 31 20 29 3b 0a 20  t( nTries>1 );. 
10dd5 20 20 20 20 20 69 66 28 20 63 6f 6e 63 68 4d 6f       if( conchMo
10dd6 64 54 69 6d 65 2e 74 76 5f 73 65 63 20 21 3d 20  dTime.tv_sec != 
10dd7 62 75 66 2e 73 74 5f 6d 74 69 6d 65 73 70 65 63  buf.st_mtimespec
10dd8 2e 74 76 5f 73 65 63 20 7c 7c 20 0a 20 20 20 20  .tv_sec || .    
10dd9 20 20 20 20 20 63 6f 6e 63 68 4d 6f 64 54 69 6d       conchModTim
10dda 65 2e 74 76 5f 6e 73 65 63 20 21 3d 20 62 75 66  e.tv_nsec != buf
10ddb 2e 73 74 5f 6d 74 69 6d 65 73 70 65 63 2e 74 76  .st_mtimespec.tv
10ddc 5f 6e 73 65 63 20 29 7b 0a 20 20 20 20 20 20 20  _nsec ){.       
10ddd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
10dde 55 53 59 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  USY;.      }.   
10ddf 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 6e 54     .      if( nT
10de0 72 69 65 73 3d 3d 32 20 29 7b 20 20 0a 20 20 20  ries==2 ){  .   
10de1 20 20 20 20 20 63 68 61 72 20 74 42 75 66 5b 50       char tBuf[P
10de2 52 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45 4e  ROXY_MAXCONCHLEN
10de3 5d 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c  ];.        int l
10de4 65 6e 20 3d 20 6f 73 50 72 65 61 64 28 63 6f 6e  en = osPread(con
10de5 63 68 46 69 6c 65 2d 3e 68 2c 20 74 42 75 66 2c  chFile->h, tBuf,
10de6 20 50 52 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c   PROXY_MAXCONCHL
10de7 45 4e 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  EN, 0);.        
10de8 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20  if( len<0 ){.   
10de9 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
10dea 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
10deb 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
10dec 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c  n SQLITE_IOERR_L
10ded 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  OCK;.        }. 
10dee 20 20 20 20 20 20 20 69 66 28 20 6c 65 6e 3e 50         if( len>P
10def 52 4f 58 59 5f 50 41 54 48 49 4e 44 45 58 20 26  ROXY_PATHINDEX &
10df0 26 20 74 42 75 66 5b 30 5d 3d 3d 28 63 68 61 72  & tBuf[0]==(char
10df1 29 50 52 4f 58 59 5f 43 4f 4e 43 48 56 45 52 53  )PROXY_CONCHVERS
10df2 49 4f 4e 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ION){.          
10df3 2f 2a 20 64 6f 6e 27 74 20 62 72 65 61 6b 20 74  /* don't break t
10df4 68 65 20 6c 6f 63 6b 20 69 66 20 74 68 65 20 68  he lock if the h
10df5 6f 73 74 20 69 64 20 64 6f 65 73 6e 27 74 20 6d  ost id doesn't m
10df6 61 74 63 68 20 2a 2f 0a 20 20 20 20 20 20 20 20  atch */.        
10df7 20 20 69 66 28 20 30 21 3d 6d 65 6d 63 6d 70 28    if( 0!=memcmp(
10df8 26 74 42 75 66 5b 50 52 4f 58 59 5f 48 45 41 44  &tBuf[PROXY_HEAD
10df9 45 52 4c 45 4e 5d 2c 20 6d 79 48 6f 73 74 49 44  ERLEN], myHostID
10dfa 2c 20 50 52 4f 58 59 5f 48 4f 53 54 49 44 4c 45  , PROXY_HOSTIDLE
10dfb 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  N) ){.          
10dfc 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
10dfd 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 20 20  BUSY;.          
10dfe 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  }.        }else{
10dff 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 6f  .          /* do
10e00 6e 27 74 20 62 72 65 61 6b 20 74 68 65 20 6c 6f  n't break the lo
10e01 63 6b 20 6f 6e 20 73 68 6f 72 74 20 72 65 61 64  ck on short read
10e02 20 6f 72 20 61 20 76 65 72 73 69 6f 6e 20 6d 69   or a version mi
10e03 73 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 20 20  smatch */.      
10e04 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
10e05 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20  E_BUSY;.        
10e06 7d 0a 20 20 20 20 20 20 20 20 75 73 6c 65 65 70  }.        usleep
10e07 28 31 30 30 30 30 30 30 30 29 3b 20 2f 2a 20 77  (10000000); /* w
10e08 61 69 74 20 31 30 20 73 65 63 20 61 6e 64 20 74  ait 10 sec and t
10e09 72 79 20 74 68 65 20 6c 6f 63 6b 20 61 67 61 69  ry the lock agai
10e0a 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e  n */.        con
10e0b 74 69 6e 75 65 3b 20 0a 20 20 20 20 20 20 7d 0a  tinue; .      }.
10e0c 20 20 20 20 20 20 0a 20 20 20 20 20 20 61 73 73        .      ass
10e0d 65 72 74 28 20 6e 54 72 69 65 73 3d 3d 33 20 29  ert( nTries==3 )
10e0e 3b 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 70  ;.      if( 0==p
10e0f 72 6f 78 79 42 72 65 61 6b 43 6f 6e 63 68 4c 6f  roxyBreakConchLo
10e10 63 6b 28 70 46 69 6c 65 2c 20 6d 79 48 6f 73 74  ck(pFile, myHost
10e11 49 44 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ID) ){.        r
10e12 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
10e13 20 20 20 20 20 20 20 69 66 28 20 6c 6f 63 6b 54         if( lockT
10e14 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
10e15 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  OCK ){.         
10e16 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d   rc = conchFile-
10e17 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28  >pMethod->xLock(
10e18 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63  (sqlite3_file*)c
10e19 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52 45 44  onchFile, SHARED
10e1a 5f 4c 4f 43 4b 29 3b 20 20 20 20 20 20 20 20 20  _LOCK);         
10e1b 20 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20   .        }.    
10e1c 20 20 20 20 69 66 28 20 21 72 63 20 29 7b 0a 20      if( !rc ){. 
10e1d 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6f           rc = co
10e1e 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64  nchFile->pMethod
10e1f 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33  ->xLock((sqlite3
10e20 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65  _file*)conchFile
10e21 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20  , lockType);.   
10e22 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
10e23 20 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65 28 20     }.  } while( 
10e24 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
10e25 26 26 20 6e 54 72 69 65 73 3c 33 20 29 3b 0a 20  && nTries<3 );. 
10e26 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d   .  return rc;.}
10e27 0a 0a 2f 2a 20 54 61 6b 65 73 20 74 68 65 20 63  ../* Takes the c
10e28 6f 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20 61  onch by taking a
10e29 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64   shared lock and
10e2a 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e   read the conten
10e2b 74 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a  ts conch, if .**
10e2c 20 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f 6e   lockPath is non
10e2d 2d 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74 20  -NULL, the host 
10e2e 49 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65  ID and lock file
10e2f 20 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63 68   path must match
10e30 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f  .  A NULL .** lo
10e31 63 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68 61  ckPath means tha
10e32 74 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20 69  t the lockPath i
10e33 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  n the conch file
10e34 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 66   will be used if
10e35 20 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49 44   the .** host ID
10e36 73 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e 65  s match, or a ne
10e37 77 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c  w lock path will
10e38 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 61 75   be generated au
10e39 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20  tomatically .** 
10e3a 61 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74  and written to t
10e3b 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a  he conch file..*
10e3c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
10e3d 78 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69 78  xyTakeConch(unix
10e3e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20  File *pFile){.  
10e3f 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
10e40 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f  ext *pCtx = (pro
10e41 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
10e42 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
10e43 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20 20  gContext; .  .  
10e44 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48  if( pCtx->conchH
10e45 65 6c 64 21 3d 30 20 29 7b 0a 20 20 20 20 72 65  eld!=0 ){.    re
10e46 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
10e47 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69    }else{.    uni
10e48 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65  xFile *conchFile
10e49 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69   = pCtx->conchFi
10e4a 6c 65 3b 0a 20 20 20 20 75 75 69 64 5f 74 20 6d  le;.    uuid_t m
10e4b 79 48 6f 73 74 49 44 3b 0a 20 20 20 20 69 6e 74  yHostID;.    int
10e4c 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20   pError = 0;.   
10e4d 20 63 68 61 72 20 72 65 61 64 42 75 66 5b 50 52   char readBuf[PR
10e4e 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45 4e 5d  OXY_MAXCONCHLEN]
10e4f 3b 0a 20 20 20 20 63 68 61 72 20 6c 6f 63 6b 50  ;.    char lockP
10e50 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b  ath[MAXPATHLEN];
10e51 0a 20 20 20 20 63 68 61 72 20 2a 74 65 6d 70 4c  .    char *tempL
10e52 6f 63 6b 50 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a  ockPath = NULL;.
10e53 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c      int rc = SQL
10e54 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 6e 74 20  ITE_OK;.    int 
10e55 63 72 65 61 74 65 43 6f 6e 63 68 20 3d 20 30 3b  createConch = 0;
10e56 0a 20 20 20 20 69 6e 74 20 68 6f 73 74 49 64 4d  .    int hostIdM
10e57 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 69 6e  atch = 0;.    in
10e58 74 20 72 65 61 64 4c 65 6e 20 3d 20 30 3b 0a 20  t readLen = 0;. 
10e59 20 20 20 69 6e 74 20 74 72 79 4f 6c 64 4c 6f 63     int tryOldLoc
10e5a 6b 50 61 74 68 20 3d 20 30 3b 0a 20 20 20 20 69  kPath = 0;.    i
10e5b 6e 74 20 66 6f 72 63 65 4e 65 77 4c 6f 63 6b 50  nt forceNewLockP
10e5c 61 74 68 20 3d 20 30 3b 0a 20 20 20 20 0a 20 20  ath = 0;.    .  
10e5d 20 20 4f 53 54 52 41 43 45 28 28 22 54 41 4b 45    OSTRACE(("TAKE
10e5e 43 4f 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73  CONCH  %d for %s
10e5f 20 70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63   pid=%d\n", conc
10e60 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20  hFile->h,.      
10e61 20 20 20 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f         (pCtx->lo
10e62 63 6b 50 72 6f 78 79 50 61 74 68 20 3f 20 70 43  ckProxyPath ? pC
10e63 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
10e64 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67  h : ":auto:"), g
10e65 65 74 70 69 64 28 29 29 29 3b 0a 0a 20 20 20 20  etpid()));..    
10e66 72 63 20 3d 20 70 72 6f 78 79 47 65 74 48 6f 73  rc = proxyGetHos
10e67 74 49 44 28 6d 79 48 6f 73 74 49 44 2c 20 26 70  tID(myHostID, &p
10e68 45 72 72 6f 72 29 3b 0a 20 20 20 20 69 66 28 20  Error);.    if( 
10e69 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54  (rc&0xff)==SQLIT
10e6a 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 20  E_IOERR ){.     
10e6b 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
10e6c 6f 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20 20 20  o = pError;.    
10e6d 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63    goto end_takec
10e6e 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20  onch;.    }.    
10e6f 72 63 20 3d 20 70 72 6f 78 79 43 6f 6e 63 68 4c  rc = proxyConchL
10e70 6f 63 6b 28 70 46 69 6c 65 2c 20 6d 79 48 6f 73  ock(pFile, myHos
10e71 74 49 44 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b  tID, SHARED_LOCK
10e72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
10e73 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
10e74 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63    goto end_takec
10e75 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20  onch;.    }.    
10e76 2f 2a 20 72 65 61 64 20 74 68 65 20 65 78 69 73  /* read the exis
10e77 74 69 6e 67 20 63 6f 6e 63 68 20 66 69 6c 65 20  ting conch file 
10e78 2a 2f 0a 20 20 20 20 72 65 61 64 4c 65 6e 20 3d  */.    readLen =
10e79 20 73 65 65 6b 41 6e 64 52 65 61 64 28 28 75 6e   seekAndRead((un
10e7a 69 78 46 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c  ixFile*)conchFil
10e7b 65 2c 20 30 2c 20 72 65 61 64 42 75 66 2c 20 50  e, 0, readBuf, P
10e7c 52 4f 58 59 5f 4d 41 58 43 4f 4e 43 48 4c 45 4e  ROXY_MAXCONCHLEN
10e7d 29 3b 0a 20 20 20 20 69 66 28 20 72 65 61 64 4c  );.    if( readL
10e7e 65 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  en<0 ){.      /*
10e7f 20 49 2f 4f 20 65 72 72 6f 72 3a 20 6c 61 73 74   I/O error: last
10e80 45 72 72 6e 6f 20 73 65 74 20 62 79 20 73 65 65  Errno set by see
10e81 6b 41 6e 64 52 65 61 64 20 2a 2f 0a 20 20 20 20  kAndRead */.    
10e82 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
10e83 6e 6f 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  no = conchFile->
10e84 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20  lastErrno;.     
10e85 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45   rc = SQLITE_IOE
10e86 52 52 5f 52 45 41 44 3b 0a 20 20 20 20 20 20 67  RR_READ;.      g
10e87 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63  oto end_takeconc
10e88 68 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  h;.    }else if(
10e89 20 72 65 61 64 4c 65 6e 3c 3d 28 50 52 4f 58 59   readLen<=(PROXY
10e8a 5f 48 45 41 44 45 52 4c 45 4e 2b 50 52 4f 58 59  _HEADERLEN+PROXY
10e8b 5f 48 4f 53 54 49 44 4c 45 4e 29 20 7c 7c 20 0a  _HOSTIDLEN) || .
10e8c 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 61               rea
10e8d 64 42 75 66 5b 30 5d 21 3d 28 63 68 61 72 29 50  dBuf[0]!=(char)P
10e8e 52 4f 58 59 5f 43 4f 4e 43 48 56 45 52 53 49 4f  ROXY_CONCHVERSIO
10e8f 4e 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 61 20  N ){.      /* a 
10e90 73 68 6f 72 74 20 72 65 61 64 20 6f 72 20 76 65  short read or ve
10e91 72 73 69 6f 6e 20 66 6f 72 6d 61 74 20 6d 69 73  rsion format mis
10e92 6d 61 74 63 68 20 6d 65 61 6e 73 20 77 65 20 6e  match means we n
10e93 65 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  eed to create a 
10e94 6e 65 77 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f  new .      ** co
10e95 6e 63 68 20 66 69 6c 65 2e 20 0a 20 20 20 20 20  nch file. .     
10e96 20 2a 2f 0a 20 20 20 20 20 20 63 72 65 61 74 65   */.      create
10e97 43 6f 6e 63 68 20 3d 20 31 3b 0a 20 20 20 20 7d  Conch = 1;.    }
10e98 0a 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20 68  .    /* if the h
10e99 6f 73 74 20 69 64 20 6d 61 74 63 68 65 73 20 61  ost id matches a
10e9a 6e 64 20 74 68 65 20 6c 6f 63 6b 20 70 61 74 68  nd the lock path
10e9b 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
10e9c 69 6e 20 74 68 65 20 63 6f 6e 63 68 0a 20 20 20  in the conch.   
10e9d 20 2a 2a 20 77 65 27 6c 6c 20 74 72 79 20 74 6f   ** we'll try to
10e9e 20 75 73 65 20 74 68 65 20 70 61 74 68 20 74 68   use the path th
10e9f 65 72 65 2c 20 69 66 20 77 65 20 63 61 6e 27 74  ere, if we can't
10ea0 20 6f 70 65 6e 20 74 68 61 74 20 70 61 74 68 2c   open that path,
10ea1 20 77 65 27 6c 6c 20 0a 20 20 20 20 2a 2a 20 72   we'll .    ** r
10ea2 65 74 72 79 20 77 69 74 68 20 61 20 6e 65 77 20  etry with a new 
10ea3 61 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 70  auto-generated p
10ea4 61 74 68 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ath .    */.    
10ea5 64 6f 20 7b 20 2f 2a 20 69 6e 20 63 61 73 65 20  do { /* in case 
10ea6 77 65 20 6e 65 65 64 20 74 6f 20 74 72 79 20 61  we need to try a
10ea7 67 61 69 6e 20 66 6f 72 20 61 6e 20 3a 61 75 74  gain for an :aut
10ea8 6f 3a 20 6e 61 6d 65 64 20 6c 6f 63 6b 20 66 69  o: named lock fi
10ea9 6c 65 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28  le */..      if(
10eaa 20 21 63 72 65 61 74 65 43 6f 6e 63 68 20 26 26   !createConch &&
10eab 20 21 66 6f 72 63 65 4e 65 77 4c 6f 63 6b 50 61   !forceNewLockPa
10eac 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 68 6f  th ){.        ho
10ead 73 74 49 64 4d 61 74 63 68 20 3d 20 21 6d 65 6d  stIdMatch = !mem
10eae 63 6d 70 28 26 72 65 61 64 42 75 66 5b 50 52 4f  cmp(&readBuf[PRO
10eaf 58 59 5f 48 45 41 44 45 52 4c 45 4e 5d 2c 20 6d  XY_HEADERLEN], m
10eb0 79 48 6f 73 74 49 44 2c 20 0a 20 20 20 20 20 20  yHostID, .      
10eb1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10eb2 20 20 20 20 20 20 20 20 20 20 20 20 50 52 4f 58              PROX
10eb3 59 5f 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20  Y_HOSTIDLEN);.  
10eb4 20 20 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20        /* if the 
10eb5 63 6f 6e 63 68 20 68 61 73 20 64 61 74 61 20 63  conch has data c
10eb6 6f 6d 70 61 72 65 20 74 68 65 20 63 6f 6e 74 65  ompare the conte
10eb7 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  nts */.        i
10eb8 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  f( !pCtx->lockPr
10eb9 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20 20 20  oxyPath ){.     
10eba 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 74 6f       /* for auto
10ebb 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f 63  -named local loc
10ebc 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 68 65  k file, just che
10ebd 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 20 61  ck the host ID a
10ebe 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20 20 20  nd we'll.       
10ebf 20 20 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c      ** use the l
10ec0 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70  ocal lock file p
10ec1 61 74 68 20 74 68 61 74 27 73 20 61 6c 72 65 61  ath that's alrea
10ec2 64 79 20 69 6e 20 74 68 65 72 65 0a 20 20 20 20  dy in there.    
10ec3 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
10ec4 20 20 20 20 69 66 28 20 68 6f 73 74 49 64 4d 61      if( hostIdMa
10ec5 74 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20 20  tch ){.         
10ec6 20 20 20 73 69 7a 65 5f 74 20 70 61 74 68 4c 65     size_t pathLe
10ec7 6e 20 3d 20 28 72 65 61 64 4c 65 6e 20 2d 20 50  n = (readLen - P
10ec8 52 4f 58 59 5f 50 41 54 48 49 4e 44 45 58 29 3b  ROXY_PATHINDEX);
10ec9 0a 20 20 20 20 20 20 20 20 20 20 20 20 0a 20 20  .            .  
10eca 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 61            if( pa
10ecb 74 68 4c 65 6e 3e 3d 4d 41 58 50 41 54 48 4c 45  thLen>=MAXPATHLE
10ecc 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  N ){.           
10ecd 20 20 20 70 61 74 68 4c 65 6e 3d 4d 41 58 50 41     pathLen=MAXPA
10ece 54 48 4c 45 4e 2d 31 3b 0a 20 20 20 20 20 20 20  THLEN-1;.       
10ecf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
10ed0 20 20 20 6d 65 6d 63 70 79 28 6c 6f 63 6b 50 61     memcpy(lockPa
10ed1 74 68 2c 20 26 72 65 61 64 42 75 66 5b 50 52 4f  th, &readBuf[PRO
10ed2 58 59 5f 50 41 54 48 49 4e 44 45 58 5d 2c 20 70  XY_PATHINDEX], p
10ed3 61 74 68 4c 65 6e 29 3b 0a 20 20 20 20 20 20 20  athLen);.       
10ed4 20 20 20 20 20 6c 6f 63 6b 50 61 74 68 5b 70 61       lockPath[pa
10ed5 74 68 4c 65 6e 5d 20 3d 20 30 3b 0a 20 20 20 20  thLen] = 0;.    
10ed6 20 20 20 20 20 20 20 20 74 65 6d 70 4c 6f 63 6b          tempLock
10ed7 50 61 74 68 20 3d 20 6c 6f 63 6b 50 61 74 68 3b  Path = lockPath;
10ed8 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 72 79  .            try
10ed9 4f 6c 64 4c 6f 63 6b 50 61 74 68 20 3d 20 31 3b  OldLockPath = 1;
10eda 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
10edb 63 72 65 61 74 65 20 61 20 63 6f 70 79 20 6f 66  create a copy of
10edc 20 74 68 65 20 6c 6f 63 6b 20 70 61 74 68 20 69   the lock path i
10edd 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20 74  f the conch is t
10ede 61 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  aken */.        
10edf 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b      goto end_tak
10ee0 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20 20 20  econch;.        
10ee1 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73    }.        }els
10ee2 65 20 69 66 28 20 68 6f 73 74 49 64 4d 61 74 63  e if( hostIdMatc
10ee3 68 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h.              
10ee4 20 26 26 20 21 73 74 72 6e 63 6d 70 28 70 43 74   && !strncmp(pCt
10ee5 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
10ee6 2c 20 26 72 65 61 64 42 75 66 5b 50 52 4f 58 59  , &readBuf[PROXY
10ee7 5f 50 41 54 48 49 4e 44 45 58 5d 2c 0a 20 20 20  _PATHINDEX],.   
10ee8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ee9 20 20 20 20 20 20 20 20 72 65 61 64 4c 65 6e 2d          readLen-
10eea 50 52 4f 58 59 5f 50 41 54 48 49 4e 44 45 58 29  PROXY_PATHINDEX)
10eeb 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20  .        ){.    
10eec 20 20 20 20 20 20 2f 2a 20 63 6f 6e 63 68 20 68        /* conch h
10eed 6f 73 74 20 61 6e 64 20 6c 6f 63 6b 20 70 61 74  ost and lock pat
10eee 68 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 20  h match */.     
10eef 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61       goto end_ta
10ef0 6b 65 63 6f 6e 63 68 3b 20 0a 20 20 20 20 20 20  keconch; .      
10ef1 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
10ef2 20 20 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 74    .      /* if t
10ef3 68 65 20 63 6f 6e 63 68 20 69 73 6e 27 74 20 77  he conch isn't w
10ef4 72 69 74 61 62 6c 65 20 61 6e 64 20 64 6f 65 73  ritable and does
10ef5 6e 27 74 20 6d 61 74 63 68 2c 20 77 65 20 63 61  n't match, we ca
10ef6 6e 27 74 20 74 61 6b 65 20 69 74 20 2a 2f 0a 20  n't take it */. 
10ef7 20 20 20 20 20 69 66 28 20 28 63 6f 6e 63 68 46       if( (conchF
10ef8 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 26 4f  ile->openFlags&O
10ef9 5f 52 44 57 52 29 20 3d 3d 20 30 20 29 7b 0a 20  _RDWR) == 0 ){. 
10efa 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
10efb 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20  TE_BUSY;.       
10efc 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f   goto end_takeco
10efd 6e 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nch;.      }.   
10efe 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 65 69 74     .      /* eit
10eff 68 65 72 20 74 68 65 20 63 6f 6e 63 68 20 64 69  her the conch di
10f00 64 6e 27 74 20 6d 61 74 63 68 20 6f 72 20 77 65  dn't match or we
10f01 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74 65 20   need to create 
10f02 61 20 6e 65 77 20 6f 6e 65 20 2a 2f 0a 20 20 20  a new one */.   
10f03 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f     if( !pCtx->lo
10f04 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20  ckProxyPath ){. 
10f05 20 20 20 20 20 20 20 70 72 6f 78 79 47 65 74 4c         proxyGetL
10f06 6f 63 6b 50 61 74 68 28 70 43 74 78 2d 3e 64 62  ockPath(pCtx->db
10f07 50 61 74 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20  Path, lockPath, 
10f08 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20  MAXPATHLEN);.   
10f09 20 20 20 20 20 74 65 6d 70 4c 6f 63 6b 50 61 74       tempLockPat
10f0a 68 20 3d 20 6c 6f 63 6b 50 61 74 68 3b 0a 20 20  h = lockPath;.  
10f0b 20 20 20 20 20 20 2f 2a 20 63 72 65 61 74 65 20        /* create 
10f0c 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6c 6f  a copy of the lo
10f0d 63 6b 20 70 61 74 68 20 5f 6f 6e 6c 79 5f 20 69  ck path _only_ i
10f0e 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20 74  f the conch is t
10f0f 61 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 7d 0a  aken */.      }.
10f10 20 20 20 20 20 20 0a 20 20 20 20 20 20 2f 2a 20        .      /* 
10f11 75 70 64 61 74 65 20 63 6f 6e 63 68 20 77 69 74  update conch wit
10f12 68 20 68 6f 73 74 20 61 6e 64 20 70 61 74 68 20  h host and path 
10f13 28 74 68 69 73 20 77 69 6c 6c 20 66 61 69 6c 20  (this will fail 
10f14 69 66 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  if other process
10f15 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20 61 20  .      ** has a 
10f16 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6c 72 65  shared lock alre
10f17 61 64 79 29 2c 20 69 66 20 74 68 65 20 68 6f 73  ady), if the hos
10f18 74 20 69 64 20 6d 61 74 63 68 65 73 2c 20 75 73  t id matches, us
10f19 65 20 74 68 65 20 62 69 67 0a 20 20 20 20 20 20  e the big.      
10f1a 2a 2a 20 73 74 69 63 6b 2e 0a 20 20 20 20 20 20  ** stick..      
10f1b 2a 2f 0a 20 20 20 20 20 20 66 75 74 69 6d 65 73  */.      futimes
10f1c 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 4e  (conchFile->h, N
10f1d 55 4c 4c 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ULL);.      if( 
10f1e 68 6f 73 74 49 64 4d 61 74 63 68 20 26 26 20 21  hostIdMatch && !
10f1f 63 72 65 61 74 65 43 6f 6e 63 68 20 29 7b 0a 20  createConch ){. 
10f20 20 20 20 20 20 20 20 69 66 28 20 63 6f 6e 63 68         if( conch
10f21 46 69 6c 65 2d 3e 70 49 6e 6f 64 65 20 26 26 20  File->pInode && 
10f22 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 49 6e 6f 64  conchFile->pInod
10f23 65 2d 3e 6e 53 68 61 72 65 64 3e 31 20 29 7b 0a  e->nShared>1 ){.
10f24 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20            /* We 
10f25 61 72 65 20 74 72 79 69 6e 67 20 66 6f 72 20 61  are trying for a
10f26 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
10f27 20 62 75 74 20 61 6e 6f 74 68 65 72 20 74 68 72   but another thr
10f28 65 61 64 20 69 6e 20 74 68 69 73 0a 20 20 20 20  ead in this.    
10f29 20 20 20 20 20 20 20 2a 2a 20 73 61 6d 65 20 70         ** same p
10f2a 72 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20  rocess is still 
10f2b 68 6f 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64  holding a shared
10f2c 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 20 20   lock. */.      
10f2d 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
10f2e 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 20  BUSY;.        } 
10f2f 65 6c 73 65 20 7b 20 20 20 20 20 20 20 20 20 20  else {          
10f30 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
10f31 70 72 6f 78 79 43 6f 6e 63 68 4c 6f 63 6b 28 70  proxyConchLock(p
10f32 46 69 6c 65 2c 20 6d 79 48 6f 73 74 49 44 2c 20  File, myHostID, 
10f33 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b  EXCLUSIVE_LOCK);
10f34 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
10f35 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
10f36 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  rc = conchFile->
10f37 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28  pMethod->xLock((
10f38 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f  sqlite3_file*)co
10f39 6e 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53 49  nchFile, EXCLUSI
10f3a 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20  VE_LOCK);.      
10f3b 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  }.      if( rc==
10f3c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
10f3d 20 20 20 20 20 63 68 61 72 20 77 72 69 74 65 42       char writeB
10f3e 75 66 66 65 72 5b 50 52 4f 58 59 5f 4d 41 58 43  uffer[PROXY_MAXC
10f3f 4f 4e 43 48 4c 45 4e 5d 3b 0a 20 20 20 20 20 20  ONCHLEN];.      
10f40 20 20 69 6e 74 20 77 72 69 74 65 53 69 7a 65 20    int writeSize 
10f41 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 0a 20 20  = 0;.        .  
10f42 20 20 20 20 20 20 77 72 69 74 65 42 75 66 66 65        writeBuffe
10f43 72 5b 30 5d 20 3d 20 28 63 68 61 72 29 50 52 4f  r[0] = (char)PRO
10f44 58 59 5f 43 4f 4e 43 48 56 45 52 53 49 4f 4e 3b  XY_CONCHVERSION;
10f45 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
10f46 26 77 72 69 74 65 42 75 66 66 65 72 5b 50 52 4f  &writeBuffer[PRO
10f47 58 59 5f 48 45 41 44 45 52 4c 45 4e 5d 2c 20 6d  XY_HEADERLEN], m
10f48 79 48 6f 73 74 49 44 2c 20 50 52 4f 58 59 5f 48  yHostID, PROXY_H
10f49 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20 20 20  OSTIDLEN);.     
10f4a 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63     if( pCtx->loc
10f4b 6b 50 72 6f 78 79 50 61 74 68 21 3d 4e 55 4c 4c  kProxyPath!=NULL
10f4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74   ){.          st
10f4d 72 6c 63 70 79 28 26 77 72 69 74 65 42 75 66 66  rlcpy(&writeBuff
10f4e 65 72 5b 50 52 4f 58 59 5f 50 41 54 48 49 4e 44  er[PROXY_PATHIND
10f4f 45 58 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50  EX], pCtx->lockP
10f50 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54  roxyPath, MAXPAT
10f51 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 20 20 7d  HLEN);.        }
10f52 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
10f53 73 74 72 6c 63 70 79 28 26 77 72 69 74 65 42 75  strlcpy(&writeBu
10f54 66 66 65 72 5b 50 52 4f 58 59 5f 50 41 54 48 49  ffer[PROXY_PATHI
10f55 4e 44 45 58 5d 2c 20 74 65 6d 70 4c 6f 63 6b 50  NDEX], tempLockP
10f56 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29  ath, MAXPATHLEN)
10f57 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
10f58 20 20 20 20 77 72 69 74 65 53 69 7a 65 20 3d 20      writeSize = 
10f59 50 52 4f 58 59 5f 50 41 54 48 49 4e 44 45 58 20  PROXY_PATHINDEX 
10f5a 2b 20 73 74 72 6c 65 6e 28 26 77 72 69 74 65 42  + strlen(&writeB
10f5b 75 66 66 65 72 5b 50 52 4f 58 59 5f 50 41 54 48  uffer[PROXY_PATH
10f5c 49 4e 44 45 58 5d 29 3b 0a 20 20 20 20 20 20 20  INDEX]);.       
10f5d 20 72 6f 62 75 73 74 5f 66 74 72 75 6e 63 61 74   robust_ftruncat
10f5e 65 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20  e(conchFile->h, 
10f5f 77 72 69 74 65 53 69 7a 65 29 3b 0a 20 20 20 20  writeSize);.    
10f60 20 20 20 20 72 63 20 3d 20 75 6e 69 78 57 72 69      rc = unixWri
10f61 74 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  te((sqlite3_file
10f62 20 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 77 72   *)conchFile, wr
10f63 69 74 65 42 75 66 66 65 72 2c 20 77 72 69 74 65  iteBuffer, write
10f64 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Size, 0);.      
10f65 20 20 66 73 79 6e 63 28 63 6f 6e 63 68 46 69 6c    fsync(conchFil
10f66 65 2d 3e 68 29 3b 0a 20 20 20 20 20 20 20 20 2f  e->h);.        /
10f67 2a 20 49 66 20 77 65 20 63 72 65 61 74 65 64 20  * If we created 
10f68 61 20 6e 65 77 20 63 6f 6e 63 68 20 66 69 6c 65  a new conch file
10f69 20 28 6e 6f 74 20 6a 75 73 74 20 75 70 64 61 74   (not just updat
10f6a 65 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ed the contents 
10f6b 6f 66 20 61 20 0a 20 20 20 20 20 20 20 20 20 2a  of a .         *
10f6c 2a 20 76 61 6c 69 64 20 63 6f 6e 63 68 20 66 69  * valid conch fi
10f6d 6c 65 29 2c 20 74 72 79 20 74 6f 20 6d 61 74 63  le), try to matc
10f6e 68 20 74 68 65 20 70 65 72 6d 69 73 73 69 6f 6e  h the permission
10f6f 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  s of the databas
10f70 65 20 0a 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  e .         */. 
10f71 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
10f72 51 4c 49 54 45 5f 4f 4b 20 26 26 20 63 72 65 61  QLITE_OK && crea
10f73 74 65 43 6f 6e 63 68 20 29 7b 0a 20 20 20 20 20  teConch ){.     
10f74 20 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74       struct stat
10f75 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 20 20   buf;.          
10f76 69 6e 74 20 65 72 72 20 3d 20 6f 73 46 73 74 61  int err = osFsta
10f77 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26 62 75 66  t(pFile->h, &buf
10f78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
10f79 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20   err==0 ){.     
10f7a 20 20 20 20 20 20 20 6d 6f 64 65 5f 74 20 63 6d         mode_t cm
10f7b 6f 64 65 20 3d 20 62 75 66 2e 73 74 5f 6d 6f 64  ode = buf.st_mod
10f7c 65 26 28 53 5f 49 52 55 53 52 7c 53 5f 49 57 55  e&(S_IRUSR|S_IWU
10f7d 53 52 20 7c 20 53 5f 49 52 47 52 50 7c 53 5f 49  SR | S_IRGRP|S_I
10f7e 57 47 52 50 20 7c 0a 20 20 20 20 20 20 20 20 20  WGRP |.         
10f7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
10f81 5f 49 52 4f 54 48 7c 53 5f 49 57 4f 54 48 29 3b  _IROTH|S_IWOTH);
10f82 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
10f83 74 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65  try to match the
10f84 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 52   database file R
10f85 2f 57 20 70 65 72 6d 69 73 73 69 6f 6e 73 2c 20  /W permissions, 
10f86 69 67 6e 6f 72 65 20 66 61 69 6c 75 72 65 20 2a  ignore failure *
10f87 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
10f88 5f 50 52 4f 58 59 5f 44 45 42 55 47 0a 20 20 20  _PROXY_DEBUG.   
10f89 20 20 20 20 20 20 20 20 20 6f 73 46 63 68 6d 6f           osFchmo
10f8a 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20  d(conchFile->h, 
10f8b 63 6d 6f 64 65 29 3b 0a 23 65 6c 73 65 0a 20 20  cmode);.#else.  
10f8c 20 20 20 20 20 20 20 20 20 20 64 6f 7b 0a 20 20            do{.  
10f8d 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
10f8e 20 6f 73 46 63 68 6d 6f 64 28 63 6f 6e 63 68 46   osFchmod(conchF
10f8f 69 6c 65 2d 3e 68 2c 20 63 6d 6f 64 65 29 3b 0a  ile->h, cmode);.
10f90 20 20 20 20 20 20 20 20 20 20 20 20 7d 77 68 69              }whi
10f91 6c 65 28 20 72 63 3d 3d 28 2d 31 29 20 26 26 20  le( rc==(-1) && 
10f92 65 72 72 6e 6f 3d 3d 45 49 4e 54 52 20 29 3b 0a  errno==EINTR );.
10f93 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
10f94 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  rc!=0 ){.       
10f95 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20         int code 
10f96 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20  = errno;.       
10f97 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73         fprintf(s
10f98 74 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25  tderr, "fchmod %
10f99 6f 20 46 41 49 4c 45 44 20 77 69 74 68 20 25 64  o FAILED with %d
10f9a 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   %s\n",.        
10f9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d                cm
10f9c 6f 64 65 2c 20 63 6f 64 65 2c 20 73 74 72 65 72  ode, code, strer
10f9d 72 6f 72 28 63 6f 64 65 29 29 3b 0a 20 20 20 20  ror(code));.    
10f9e 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b          } else {
10f9f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66  .              f
10fa0 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22  printf(stderr, "
10fa1 66 63 68 6d 6f 64 20 25 6f 20 53 55 43 43 45 44  fchmod %o SUCCED
10fa2 45 44 5c 6e 22 2c 63 6d 6f 64 65 29 3b 0a 20 20  ED\n",cmode);.  
10fa3 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
10fa4 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
10fa5 20 20 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64           int cod
10fa6 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  e = errno;.     
10fa7 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73         fprintf(s
10fa8 74 64 65 72 72 2c 20 22 53 54 41 54 20 46 41 49  tderr, "STAT FAI
10fa9 4c 45 44 5b 25 64 5d 20 77 69 74 68 20 25 64 20  LED[%d] with %d 
10faa 25 73 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20  %s\n", .        
10fab 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72 2c              err,
10fac 20 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28   code, strerror(
10fad 63 6f 64 65 29 29 3b 0a 23 65 6e 64 69 66 0a 20  code));.#endif. 
10fae 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
10faf 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
10fb0 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d     conchFile->pM
10fb1 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28  ethod->xUnlock((
10fb2 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f  sqlite3_file*)co
10fb3 6e 63 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f  nchFile, SHARED_
10fb4 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 0a 20 20  LOCK);.      .  
10fb5 20 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3a    end_takeconch:
10fb6 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 28 28  .      OSTRACE((
10fb7 22 54 52 41 4e 53 50 52 4f 58 59 3a 20 43 4c 4f  "TRANSPROXY: CLO
10fb8 53 45 20 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  SE  %d\n", pFile
10fb9 2d 3e 68 29 29 3b 0a 20 20 20 20 20 20 69 66 28  ->h));.      if(
10fba 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
10fbb 26 20 70 46 69 6c 65 2d 3e 6f 70 65 6e 46 6c 61  & pFile->openFla
10fbc 67 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  gs ){.        in
10fbd 74 20 66 64 3b 0a 20 20 20 20 20 20 20 20 69 66  t fd;.        if
10fbe 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 20 29 7b  ( pFile->h>=0 ){
10fbf 0a 20 20 20 20 20 20 20 20 20 20 72 6f 62 75 73  .          robus
10fc0 74 5f 63 6c 6f 73 65 28 70 46 69 6c 65 2c 20 70  t_close(pFile, p
10fc1 46 69 6c 65 2d 3e 68 2c 20 5f 5f 4c 49 4e 45 5f  File->h, __LINE_
10fc2 5f 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  _);.        }.  
10fc3 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d        pFile->h =
10fc4 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 66 64 20   -1;.        fd 
10fc5 3d 20 72 6f 62 75 73 74 5f 6f 70 65 6e 28 70 43  = robust_open(pC
10fc6 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c  tx->dbPath, pFil
10fc7 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 2c 20 30 29  e->openFlags, 0)
10fc8 3b 0a 20 20 20 20 20 20 20 20 4f 53 54 52 41 43  ;.        OSTRAC
10fc9 45 28 28 22 54 52 41 4e 53 50 52 4f 58 59 3a 20  E(("TRANSPROXY: 
10fca 4f 50 45 4e 20 20 25 64 5c 6e 22 2c 20 66 64 29  OPEN  %d\n", fd)
10fcb 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66  );.        if( f
10fcc 64 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  d>=0 ){.        
10fcd 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 66 64 3b    pFile->h = fd;
10fce 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
10fcf 20 20 20 20 20 20 20 20 20 20 72 63 3d 53 51 4c            rc=SQL
10fd0 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50  ITE_CANTOPEN_BKP
10fd1 54 3b 20 2f 2a 20 53 51 4c 49 54 45 5f 42 55 53  T; /* SQLITE_BUS
10fd2 59 3f 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63  Y? proxyTakeConc
10fd3 68 20 63 61 6c 6c 65 64 0a 20 20 20 20 20 20 20  h called.       
10fd4 20 20 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69      during locki
10fd5 6e 67 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a  ng */.        }.
10fd6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
10fd7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
10fd8 26 26 20 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  && !pCtx->lockPr
10fd9 6f 78 79 20 29 7b 0a 20 20 20 20 20 20 20 20 63  oxy ){.        c
10fda 68 61 72 20 2a 70 61 74 68 20 3d 20 74 65 6d 70  har *path = temp
10fdb 4c 6f 63 6b 50 61 74 68 20 3f 20 74 65 6d 70 4c  LockPath ? tempL
10fdc 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78 2d 3e  ockPath : pCtx->
10fdd 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20  lockProxyPath;. 
10fde 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78         rc = prox
10fdf 79 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28  yCreateUnixFile(
10fe0 70 61 74 68 2c 20 26 70 43 74 78 2d 3e 6c 6f 63  path, &pCtx->loc
10fe1 6b 50 72 6f 78 79 2c 20 31 29 3b 0a 20 20 20 20  kProxy, 1);.    
10fe2 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
10fe3 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c  TE_OK && rc!=SQL
10fe4 49 54 45 5f 4e 4f 4d 45 4d 20 26 26 20 74 72 79  ITE_NOMEM && try
10fe5 4f 6c 64 4c 6f 63 6b 50 61 74 68 20 29 7b 0a 20  OldLockPath ){. 
10fe6 20 20 20 20 20 20 20 20 20 2f 2a 20 77 65 20 63           /* we c
10fe7 6f 75 6c 64 6e 27 74 20 63 72 65 61 74 65 20 74  ouldn't create t
10fe8 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69  he proxy lock fi
10fe9 6c 65 20 77 69 74 68 20 74 68 65 20 6f 6c 64 20  le with the old 
10fea 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 68 0a 20  lock file path. 
10feb 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20            ** so 
10fec 74 72 79 20 61 67 61 69 6e 20 76 69 61 20 61 75  try again via au
10fed 74 6f 2d 6e 61 6d 69 6e 67 20 0a 20 20 20 20 20  to-naming .     
10fee 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
10fef 20 20 20 66 6f 72 63 65 4e 65 77 4c 6f 63 6b 50     forceNewLockP
10ff0 61 74 68 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ath = 1;.       
10ff1 20 20 20 74 72 79 4f 6c 64 4c 6f 63 6b 50 61 74     tryOldLockPat
10ff2 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  h = 0;.         
10ff3 20 63 6f 6e 74 69 6e 75 65 3b 20 2f 2a 20 67 6f   continue; /* go
10ff4 20 62 61 63 6b 20 74 6f 20 74 68 65 20 64 6f 20   back to the do 
10ff5 7b 7d 20 77 68 69 6c 65 20 73 74 61 72 74 20 70  {} while start p
10ff6 6f 69 6e 74 2c 20 74 72 79 20 61 67 61 69 6e 20  oint, try again 
10ff7 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  */.        }.   
10ff8 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72     }.      if( r
10ff9 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
10ffa 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 65 64 20          /* Need 
10ffb 74 6f 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f  to make a copy o
10ffc 66 20 70 61 74 68 20 69 66 20 77 65 20 65 78 74  f path if we ext
10ffd 72 61 63 74 65 64 20 74 68 65 20 76 61 6c 75 65  racted the value
10ffe 0a 20 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f  .         ** fro
10fff 6d 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  m the conch file
11000 20 6f 72 20 74 68 65 20 70 61 74 68 20 77 61 73   or the path was
11001 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68   allocated on th
11002 65 20 73 74 61 63 6b 0a 20 20 20 20 20 20 20 20  e stack.        
11003 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
11004 74 65 6d 70 4c 6f 63 6b 50 61 74 68 20 29 7b 0a  tempLockPath ){.
11005 20 20 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e            pCtx->
11006 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20  lockProxyPath = 
11007 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
11008 30 2c 20 74 65 6d 70 4c 6f 63 6b 50 61 74 68 29  0, tempLockPath)
11009 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
1100a 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79  !pCtx->lockProxy
1100b 50 61 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20  Path ){.        
1100c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1100d 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20  NOMEM;.         
1100e 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
1100f 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72     }.      if( r
11010 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
11011 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 63 6f          pCtx->co
11012 6e 63 68 48 65 6c 64 20 3d 20 31 3b 0a 20 20 20  nchHeld = 1;.   
11013 20 20 20 20 20 0a 20 20 20 20 20 20 20 20 69 66       .        if
11014 28 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78  ( pCtx->lockProx
11015 79 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61  y->pMethod == &a
11016 66 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20  fpIoMethods ){. 
11017 20 20 20 20 20 20 20 20 20 61 66 70 4c 6f 63 6b           afpLock
11018 69 6e 67 43 6f 6e 74 65 78 74 20 2a 61 66 70 43  ingContext *afpC
11019 74 78 3b 0a 20 20 20 20 20 20 20 20 20 20 61 66  tx;.          af
1101a 70 43 74 78 20 3d 20 28 61 66 70 4c 6f 63 6b 69  pCtx = (afpLocki
1101b 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 43 74 78  ngContext *)pCtx
1101c 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 6c 6f 63  ->lockProxy->loc
1101d 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20  kingContext;.   
1101e 20 20 20 20 20 20 20 61 66 70 43 74 78 2d 3e 64         afpCtx->d
1101f 62 50 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f  bPath = pCtx->lo
11020 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20  ckProxyPath;.   
11021 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 65       }.      } e
11022 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 63 6f  lse {.        co
11023 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64  nchFile->pMethod
11024 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74  ->xUnlock((sqlit
11025 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69  e3_file*)conchFi
11026 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20  le, NO_LOCK);.  
11027 20 20 20 20 7d 0a 20 20 20 20 20 20 4f 53 54 52      }.      OSTR
11028 41 43 45 28 28 22 54 41 4b 45 43 4f 4e 43 48 20  ACE(("TAKECONCH 
11029 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68   %d %s\n", conch
1102a 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
1102b 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49          rc==SQLI
1102c 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c  TE_OK?"ok":"fail
1102d 65 64 22 29 29 3b 0a 20 20 20 20 20 20 72 65 74  ed"));.      ret
1102e 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 20 77 68  urn rc;.    } wh
1102f 69 6c 65 20 28 31 29 3b 20 2f 2a 20 69 6e 20 63  ile (1); /* in c
11030 61 73 65 20 77 65 20 6e 65 65 64 20 74 6f 20 72  ase we need to r
11031 65 74 72 79 20 74 68 65 20 3a 61 75 74 6f 3a 20  etry the :auto: 
11032 6c 6f 63 6b 20 66 69 6c 65 20 2d 20 0a 20 20 20  lock file - .   
11033 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
11034 20 77 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72   we should never
11035 20 67 65 74 20 68 65 72 65 20 65 78 63 65 70 74   get here except
11036 20 76 69 61 20 74 68 65 20 27 63 6f 6e 74 69 6e   via the 'contin
11037 75 65 27 20 63 61 6c 6c 2e 20 2a 2f 0a 20 20 7d  ue' call. */.  }
11038 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69  .}../*.** If pFi
11039 6c 65 20 68 6f 6c 64 73 20 61 20 6c 6f 63 6b 20  le holds a lock 
1103a 6f 6e 20 61 20 63 6f 6e 63 68 20 66 69 6c 65 2c  on a conch file,
1103b 20 74 68 65 6e 20 72 65 6c 65 61 73 65 20 74 68   then release th
1103c 61 74 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74  at lock..*/.stat
1103d 69 63 20 69 6e 74 20 70 72 6f 78 79 52 65 6c 65  ic int proxyRele
1103e 61 73 65 43 6f 6e 63 68 28 75 6e 69 78 46 69 6c  aseConch(unixFil
1103f 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74  e *pFile){.  int
11040 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
11041 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 72           /* Subr
11042 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 20 63 6f  outine return co
11043 64 65 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f 63  de */.  proxyLoc
11044 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74  kingContext *pCt
11045 78 3b 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69  x;  /* The locki
11046 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 74  ng context for t
11047 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 2a 2f  he proxy lock */
11048 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e  .  unixFile *con
11049 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20 2f  chFile;        /
1104a 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f  * Name of the co
1104b 6e 63 68 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 70  nch file */..  p
1104c 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b  Ctx = (proxyLock
1104d 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69  ingContext *)pFi
1104e 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
1104f 78 74 3b 0a 20 20 63 6f 6e 63 68 46 69 6c 65 20  xt;.  conchFile 
11050 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c  = pCtx->conchFil
11051 65 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 52  e;.  OSTRACE(("R
11052 45 4c 45 41 53 45 43 4f 4e 43 48 20 20 25 64 20  ELEASECONCH  %d 
11053 66 6f 72 20 25 73 20 70 69 64 3d 25 64 5c 6e 22  for %s pid=%d\n"
11054 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a  , conchFile->h,.
11055 20 20 20 20 20 20 20 20 20 20 20 28 70 43 74 78             (pCtx
11056 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20  ->lockProxyPath 
11057 3f 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78  ? pCtx->lockProx
11058 79 50 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22  yPath : ":auto:"
11059 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 67  ), .           g
1105a 65 74 70 69 64 28 29 29 29 3b 0a 20 20 69 66 28  etpid()));.  if(
1105b 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64   pCtx->conchHeld
1105c 3e 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63  >0 ){.    rc = c
1105d 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f  onchFile->pMetho
1105e 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69  d->xUnlock((sqli
1105f 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46  te3_file*)conchF
11060 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20  ile, NO_LOCK);. 
11061 20 7d 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68   }.  pCtx->conch
11062 48 65 6c 64 20 3d 20 30 3b 0a 20 20 4f 53 54 52  Held = 0;.  OSTR
11063 41 43 45 28 28 22 52 45 4c 45 41 53 45 43 4f 4e  ACE(("RELEASECON
11064 43 48 20 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f  CH  %d %s\n", co
11065 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20  nchFile->h,.    
11066 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49         (rc==SQLI
11067 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22  TE_OK ? "ok" : "
11068 66 61 69 6c 65 64 22 29 29 29 3b 0a 20 20 72 65  failed")));.  re
11069 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1106a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 61 6d 65  * Given the name
1106b 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66   of a database f
1106c 69 6c 65 2c 20 63 6f 6d 70 75 74 65 20 74 68 65  ile, compute the
1106d 20 6e 61 6d 65 20 6f 66 20 69 74 73 20 63 6f 6e   name of its con
1106e 63 68 20 66 69 6c 65 2e 0a 2a 2a 20 53 74 6f 72  ch file..** Stor
1106f 65 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  e the conch file
11070 6e 61 6d 65 20 69 6e 20 6d 65 6d 6f 72 79 20 6f  name in memory o
11071 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
11072 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a  ite3_malloc()..*
11073 2a 20 4d 61 6b 65 20 2a 70 43 6f 6e 63 68 50 61  * Make *pConchPa
11074 74 68 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  th point to the 
11075 6e 65 77 20 6e 61 6d 65 2e 20 20 52 65 74 75 72  new name.  Retur
11076 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
11077 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 53 51 4c  uccess.** or SQL
11078 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 75 6e 61  ITE_NOMEM if una
11079 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e 20 6d 65  ble to obtain me
1107a 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  mory..**.** The 
1107b 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e  caller is respon
1107c 73 69 62 6c 65 20 66 6f 72 20 65 6e 73 75 72 69  sible for ensuri
1107d 6e 67 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f  ng that the allo
1107e 63 61 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20  cated memory.** 
1107f 73 70 61 63 65 20 69 73 20 65 76 65 6e 74 75 61  space is eventua
11080 6c 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a  lly freed..**.**
11081 20 2a 70 43 6f 6e 63 68 50 61 74 68 20 69 73 20   *pConchPath is 
11082 73 65 74 20 74 6f 20 4e 55 4c 4c 20 69 66 20 61  set to NULL if a
11083 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
11084 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e  on error occurs.
11085 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
11086 72 6f 78 79 43 72 65 61 74 65 43 6f 6e 63 68 50  roxyCreateConchP
11087 61 74 68 6e 61 6d 65 28 63 68 61 72 20 2a 64 62  athname(char *db
11088 50 61 74 68 2c 20 63 68 61 72 20 2a 2a 70 43 6f  Path, char **pCo
11089 6e 63 68 50 61 74 68 29 7b 0a 20 20 69 6e 74 20  nchPath){.  int 
1108a 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
1108b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
1108c 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
1108d 6e 74 20 6c 65 6e 20 3d 20 28 69 6e 74 29 73 74  nt len = (int)st
1108e 72 6c 65 6e 28 64 62 50 61 74 68 29 3b 20 2f 2a  rlen(dbPath); /*
1108f 20 4c 65 6e 67 74 68 20 6f 66 20 64 61 74 61 62   Length of datab
11090 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 2d 20 64  ase filename - d
11091 62 50 61 74 68 20 2a 2f 0a 20 20 63 68 61 72 20  bPath */.  char 
11092 2a 63 6f 6e 63 68 50 61 74 68 3b 20 20 20 20 20  *conchPath;     
11093 20 20 20 20 20 20 20 20 20 2f 2a 20 62 75 66 66           /* buff
11094 65 72 20 69 6e 20 77 68 69 63 68 20 74 6f 20 63  er in which to c
11095 6f 6e 73 74 72 75 63 74 20 63 6f 6e 63 68 20 6e  onstruct conch n
11096 61 6d 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c  ame */..  /* All
11097 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20  ocate space for 
11098 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 6e 61  the conch filena
11099 6d 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  me and initializ
1109a 65 20 74 68 65 20 6e 61 6d 65 20 74 6f 0a 20 20  e the name to.  
1109b 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  ** the name of t
1109c 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61  he original data
1109d 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 20 20 0a  base file. */  .
1109e 20 20 2a 70 43 6f 6e 63 68 50 61 74 68 20 3d 20    *pConchPath = 
1109f 63 6f 6e 63 68 50 61 74 68 20 3d 20 28 63 68 61  conchPath = (cha
110a0 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  r *)sqlite3_mall
110a1 6f 63 28 6c 65 6e 20 2b 20 38 29 3b 0a 20 20 69  oc(len + 8);.  i
110a2 66 28 20 63 6f 6e 63 68 50 61 74 68 3d 3d 30 20  f( conchPath==0 
110a3 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
110a4 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
110a5 20 20 6d 65 6d 63 70 79 28 63 6f 6e 63 68 50 61    memcpy(conchPa
110a6 74 68 2c 20 64 62 50 61 74 68 2c 20 6c 65 6e 2b  th, dbPath, len+
110a7 31 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 77 20  1);.  .  /* now 
110a8 69 6e 73 65 72 74 20 61 20 22 2e 22 20 62 65 66  insert a "." bef
110a9 6f 72 65 20 74 68 65 20 6c 61 73 74 20 2f 20 63  ore the last / c
110aa 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 66 6f  haracter */.  fo
110ab 72 28 20 69 3d 28 6c 65 6e 2d 31 29 3b 20 69 3e  r( i=(len-1); i>
110ac 3d 30 3b 20 69 2d 2d 20 29 7b 0a 20 20 20 20 69  =0; i-- ){.    i
110ad 66 28 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d  f( conchPath[i]=
110ae 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 69 2b  ='/' ){.      i+
110af 2b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  +;.      break;.
110b0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6e 63      }.  }.  conc
110b1 68 50 61 74 68 5b 69 5d 3d 27 2e 27 3b 0a 20 20  hPath[i]='.';.  
110b2 77 68 69 6c 65 20 28 20 69 3c 6c 65 6e 20 29 7b  while ( i<len ){
110b3 0a 20 20 20 20 63 6f 6e 63 68 50 61 74 68 5b 69  .    conchPath[i
110b4 2b 31 5d 3d 64 62 50 61 74 68 5b 69 5d 3b 0a 20  +1]=dbPath[i];. 
110b5 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f     i++;.  }..  /
110b6 2a 20 61 70 70 65 6e 64 20 74 68 65 20 22 2d 63  * append the "-c
110b7 6f 6e 63 68 22 20 73 75 66 66 69 78 20 74 6f 20  onch" suffix to 
110b8 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 6d 65  the file */.  me
110b9 6d 63 70 79 28 26 63 6f 6e 63 68 50 61 74 68 5b  mcpy(&conchPath[
110ba 69 2b 31 5d 2c 20 22 2d 63 6f 6e 63 68 22 2c 20  i+1], "-conch", 
110bb 37 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 69  7);.  assert( (i
110bc 6e 74 29 73 74 72 6c 65 6e 28 63 6f 6e 63 68 50  nt)strlen(conchP
110bd 61 74 68 29 20 3d 3d 20 6c 65 6e 2b 37 20 29 3b  ath) == len+7 );
110be 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
110bf 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 20 54 61 6b  E_OK;.}.../* Tak
110c0 65 73 20 61 20 66 75 6c 6c 79 20 63 6f 6e 66 69  es a fully confi
110c1 67 75 72 65 64 20 70 72 6f 78 79 20 6c 6f 63 6b  gured proxy lock
110c2 69 6e 67 2d 73 74 79 6c 65 20 75 6e 69 78 20 66  ing-style unix f
110c3 69 6c 65 20 61 6e 64 20 73 77 69 74 63 68 65 73  ile and switches
110c4 0a 2a 2a 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f  .** the local lo
110c5 63 6b 20 66 69 6c 65 20 70 61 74 68 20 0a 2a 2f  ck file path .*/
110c6 0a 73 74 61 74 69 63 20 69 6e 74 20 73 77 69 74  .static int swit
110c7 63 68 4c 6f 63 6b 50 72 6f 78 79 50 61 74 68 28  chLockProxyPath(
110c8 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c  unixFile *pFile,
110c9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74   const char *pat
110ca 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 6b  h) {.  proxyLock
110cb 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78  ingContext *pCtx
110cc 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67   = (proxyLocking
110cd 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e  Context*)pFile->
110ce 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  lockingContext;.
110cf 20 20 63 68 61 72 20 2a 6f 6c 64 50 61 74 68 20    char *oldPath 
110d0 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78  = pCtx->lockProx
110d1 79 50 61 74 68 3b 0a 20 20 69 6e 74 20 72 63 20  yPath;.  int rc 
110d2 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
110d3 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65  if( pFile->eFile
110d4 4c 6f 63 6b 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b  Lock!=NO_LOCK ){
110d5 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
110d6 54 45 5f 42 55 53 59 3b 0a 20 20 7d 20 20 0a 0a  TE_BUSY;.  }  ..
110d7 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20    /* nothing to 
110d8 64 6f 20 69 66 20 74 68 65 20 70 61 74 68 20 69  do if the path i
110d9 73 20 4e 55 4c 4c 2c 20 3a 61 75 74 6f 3a 20 6f  s NULL, :auto: o
110da 72 20 6d 61 74 63 68 65 73 20 74 68 65 20 65 78  r matches the ex
110db 69 73 74 69 6e 67 20 70 61 74 68 20 2a 2f 0a 20  isting path */. 
110dc 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20 70 61   if( !path || pa
110dd 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c 20 21  th[0]=='\0' || !
110de 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22 3a 61  strcmp(path, ":a
110df 75 74 6f 3a 22 29 20 7c 7c 0a 20 20 20 20 28 6f  uto:") ||.    (o
110e0 6c 64 50 61 74 68 20 26 26 20 21 73 74 72 6e 63  ldPath && !strnc
110e1 6d 70 28 6f 6c 64 50 61 74 68 2c 20 70 61 74 68  mp(oldPath, path
110e2 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 29 20 29  , MAXPATHLEN)) )
110e3 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
110e4 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b  ITE_OK;.  }else{
110e5 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c  .    unixFile *l
110e6 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 74 78 2d  ockProxy = pCtx-
110e7 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 20  >lockProxy;.    
110e8 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3d  pCtx->lockProxy=
110e9 4e 55 4c 4c 3b 0a 20 20 20 20 70 43 74 78 2d 3e  NULL;.    pCtx->
110ea 63 6f 6e 63 68 48 65 6c 64 20 3d 20 30 3b 0a 20  conchHeld = 0;. 
110eb 20 20 20 69 66 28 20 6c 6f 63 6b 50 72 6f 78 79     if( lockProxy
110ec 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  !=NULL ){.      
110ed 72 63 3d 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d  rc=lockProxy->pM
110ee 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73  ethod->xClose((s
110ef 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 6c 6f  qlite3_file *)lo
110f0 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 20  ckProxy);.      
110f1 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
110f2 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  rc;.      sqlite
110f3 33 5f 66 72 65 65 28 6c 6f 63 6b 50 72 6f 78 79  3_free(lockProxy
110f4 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
110f5 69 74 65 33 5f 66 72 65 65 28 6f 6c 64 50 61 74  ite3_free(oldPat
110f6 68 29 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f  h);.    pCtx->lo
110f7 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 71  ckProxyPath = sq
110f8 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c  lite3DbStrDup(0,
110f9 20 70 61 74 68 29 3b 0a 20 20 7d 0a 20 20 0a 20   path);.  }.  . 
110fa 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
110fb 2a 0a 2a 2a 20 70 46 69 6c 65 20 69 73 20 61 20  *.** pFile is a 
110fc 66 69 6c 65 20 74 68 61 74 20 68 61 73 20 62 65  file that has be
110fd 65 6e 20 6f 70 65 6e 65 64 20 62 79 20 61 20 70  en opened by a p
110fe 72 69 6f 72 20 78 4f 70 65 6e 20 63 61 6c 6c 2e  rior xOpen call.
110ff 20 20 64 62 50 61 74 68 0a 2a 2a 20 69 73 20 61    dbPath.** is a
11100 20 73 74 72 69 6e 67 20 62 75 66 66 65 72 20 61   string buffer a
11101 74 20 6c 65 61 73 74 20 4d 41 58 50 41 54 48 4c  t least MAXPATHL
11102 45 4e 2b 31 20 63 68 61 72 61 63 74 65 72 73 20  EN+1 characters 
11103 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54  in size..**.** T
11104 68 69 73 20 72 6f 75 74 69 6e 65 20 66 69 6e 64  his routine find
11105 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 73   the filename as
11106 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 46  sociated with pF
11107 69 6c 65 20 61 6e 64 20 77 72 69 74 65 73 20 69  ile and writes i
11108 74 0a 2a 2a 20 69 6e 74 20 64 62 50 61 74 68 2e  t.** int dbPath.
11109 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
1110a 72 6f 78 79 47 65 74 44 62 50 61 74 68 46 6f 72  roxyGetDbPathFor
1110b 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c  UnixFile(unixFil
1110c 65 20 2a 70 46 69 6c 65 2c 20 63 68 61 72 20 2a  e *pFile, char *
1110d 64 62 50 61 74 68 29 7b 0a 23 69 66 20 64 65 66  dbPath){.#if def
1110e 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a  ined(__APPLE__).
1110f 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65    if( pFile->pMe
11110 74 68 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65  thod == &afpIoMe
11111 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20  thods ){.    /* 
11112 61 66 70 20 73 74 79 6c 65 20 6b 65 65 70 73 20  afp style keeps 
11113 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  a reference to t
11114 68 65 20 64 62 20 70 61 74 68 20 69 6e 20 74 68  he db path in th
11115 65 20 66 69 6c 65 50 61 74 68 20 66 69 65 6c 64  e filePath field
11116 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20   .    ** of the 
11117 73 74 72 75 63 74 20 2a 2f 0a 20 20 20 20 61 73  struct */.    as
11118 73 65 72 74 28 20 28 69 6e 74 29 73 74 72 6c 65  sert( (int)strle
11119 6e 28 28 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e  n((char*)pFile->
1111a 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3c  lockingContext)<
1111b 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29 3b 0a 20  =MAXPATHLEN );. 
1111c 20 20 20 73 74 72 6c 63 70 79 28 64 62 50 61 74     strlcpy(dbPat
1111d 68 2c 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43  h, ((afpLockingC
1111e 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e  ontext *)pFile->
1111f 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 2d  lockingContext)-
11120 3e 64 62 50 61 74 68 2c 20 4d 41 58 50 41 54 48  >dbPath, MAXPATH
11121 4c 45 4e 29 3b 0a 20 20 7d 20 65 6c 73 65 0a 23  LEN);.  } else.#
11122 65 6e 64 69 66 0a 20 20 69 66 28 20 70 46 69 6c  endif.  if( pFil
11123 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 64  e->pMethod == &d
11124 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20  otlockIoMethods 
11125 29 7b 0a 20 20 20 20 2f 2a 20 64 6f 74 20 6c 6f  ){.    /* dot lo
11126 63 6b 20 73 74 79 6c 65 20 75 73 65 73 20 74 68  ck style uses th
11127 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78  e locking contex
11128 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 64  t to store the d
11129 6f 74 20 6c 6f 63 6b 0a 20 20 20 20 2a 2a 20 66  ot lock.    ** f
1112a 69 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20 20 20  ile path */.    
1112b 69 6e 74 20 6c 65 6e 20 3d 20 73 74 72 6c 65 6e  int len = strlen
1112c 28 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e  ((char *)pFile->
1112d 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 20  lockingContext) 
1112e 2d 20 73 74 72 6c 65 6e 28 44 4f 54 4c 4f 43 4b  - strlen(DOTLOCK
1112f 5f 53 55 46 46 49 58 29 3b 0a 20 20 20 20 6d 65  _SUFFIX);.    me
11130 6d 63 70 79 28 64 62 50 61 74 68 2c 20 28 63 68  mcpy(dbPath, (ch
11131 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ar *)pFile->lock
11132 69 6e 67 43 6f 6e 74 65 78 74 2c 20 6c 65 6e 20  ingContext, len 
11133 2b 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  + 1);.  }else{. 
11134 20 20 20 2f 2a 20 61 6c 6c 20 6f 74 68 65 72 20     /* all other 
11135 73 74 79 6c 65 73 20 75 73 65 20 74 68 65 20 6c  styles use the l
11136 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 74  ocking context t
11137 6f 20 73 74 6f 72 65 20 74 68 65 20 64 62 20 66  o store the db f
11138 69 6c 65 20 70 61 74 68 20 2a 2f 0a 20 20 20 20  ile path */.    
11139 61 73 73 65 72 74 28 20 73 74 72 6c 65 6e 28 28  assert( strlen((
1113a 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63  char*)pFile->loc
1113b 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3c 3d 4d 41  kingContext)<=MA
1113c 58 50 41 54 48 4c 45 4e 20 29 3b 0a 20 20 20 20  XPATHLEN );.    
1113d 73 74 72 6c 63 70 79 28 64 62 50 61 74 68 2c 20  strlcpy(dbPath, 
1113e 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c  (char *)pFile->l
1113f 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2c 20 4d  ockingContext, M
11140 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 7d 0a  AXPATHLEN);.  }.
11141 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
11142 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 6b  OK;.}../*.** Tak
11143 65 73 20 61 6e 20 61 6c 72 65 61 64 79 20 66 69  es an already fi
11144 6c 6c 65 64 20 69 6e 20 75 6e 69 78 20 66 69 6c  lled in unix fil
11145 65 20 61 6e 64 20 61 6c 74 65 72 73 20 69 74 20  e and alters it 
11146 73 6f 20 61 6c 6c 20 66 69 6c 65 20 6c 6f 63 6b  so all file lock
11147 69 6e 67 20 0a 2a 2a 20 77 69 6c 6c 20 62 65 20  ing .** will be 
11148 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65  performed on the
11149 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 6c 6f 63   local proxy loc
1114a 6b 20 66 69 6c 65 2e 20 20 54 68 65 20 66 6f 6c  k file.  The fol
1114b 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 0a 2a 2a  lowing fields.**
1114c 20 61 72 65 20 70 72 65 73 65 72 76 65 64 20 69   are preserved i
1114d 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f  n the locking co
1114e 6e 74 65 78 74 20 73 6f 20 74 68 61 74 20 74 68  ntext so that th
1114f 65 79 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72  ey can be restor
11150 65 64 20 61 6e 64 20 0a 2a 2a 20 74 68 65 20 75  ed and .** the u
11151 6e 69 78 20 73 74 72 75 63 74 75 72 65 20 70 72  nix structure pr
11152 6f 70 65 72 6c 79 20 63 6c 65 61 6e 65 64 20 75  operly cleaned u
11153 70 20 61 74 20 63 6c 6f 73 65 20 74 69 6d 65 3a  p at close time:
11154 0a 2a 2a 20 20 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  .**  ->lockingCo
11155 6e 74 65 78 74 0a 2a 2a 20 20 2d 3e 70 4d 65 74  ntext.**  ->pMet
11156 68 6f 64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  hod.*/.static in
11157 74 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d  t proxyTransform
11158 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c  UnixFile(unixFil
11159 65 20 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20  e *pFile, const 
1115a 63 68 61 72 20 2a 70 61 74 68 29 20 7b 0a 20 20  char *path) {.  
1115b 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
1115c 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 63 68 61  ext *pCtx;.  cha
1115d 72 20 64 62 50 61 74 68 5b 4d 41 58 50 41 54 48  r dbPath[MAXPATH
1115e 4c 45 4e 2b 31 5d 3b 20 20 20 20 20 20 20 2f 2a  LEN+1];       /*
1115f 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
11160 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
11161 63 68 61 72 20 2a 6c 6f 63 6b 50 61 74 68 3d 4e  char *lockPath=N
11162 55 4c 4c 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ULL;.  int rc = 
11163 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 0a 20 20  SQLITE_OK;.  .  
11164 69 66 28 20 70 46 69 6c 65 2d 3e 65 46 69 6c 65  if( pFile->eFile
11165 4c 6f 63 6b 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b  Lock!=NO_LOCK ){
11166 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
11167 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70  TE_BUSY;.  }.  p
11168 72 6f 78 79 47 65 74 44 62 50 61 74 68 46 6f 72  roxyGetDbPathFor
11169 55 6e 69 78 46 69 6c 65 28 70 46 69 6c 65 2c 20  UnixFile(pFile, 
1116a 64 62 50 61 74 68 29 3b 0a 20 20 69 66 28 20 21  dbPath);.  if( !
1116b 70 61 74 68 20 7c 7c 20 70 61 74 68 5b 30 5d 3d  path || path[0]=
1116c 3d 27 5c 30 27 20 7c 7c 20 21 73 74 72 63 6d 70  ='\0' || !strcmp
1116d 28 70 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22 29  (path, ":auto:")
1116e 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 50 61 74 68   ){.    lockPath
1116f 3d 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a  =NULL;.  }else{.
11170 20 20 20 20 6c 6f 63 6b 50 61 74 68 3d 28 63 68      lockPath=(ch
11171 61 72 20 2a 29 70 61 74 68 3b 0a 20 20 7d 0a 20  ar *)path;.  }. 
11172 20 0a 20 20 4f 53 54 52 41 43 45 28 28 22 54 52   .  OSTRACE(("TR
11173 41 4e 53 50 52 4f 58 59 20 20 25 64 20 66 6f 72  ANSPROXY  %d for
11174 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70   %s pid=%d\n", p
11175 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
11176 20 20 20 20 28 6c 6f 63 6b 50 61 74 68 20 3f 20      (lockPath ? 
11177 6c 6f 63 6b 50 61 74 68 20 3a 20 22 3a 61 75 74  lockPath : ":aut
11178 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29 29 29  o:"), getpid()))
11179 3b 0a 0a 20 20 70 43 74 78 20 3d 20 73 71 6c 69  ;..  pCtx = sqli
1117a 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65  te3_malloc( size
1117b 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 69  of(*pCtx) );.  i
1117c 66 28 20 70 43 74 78 3d 3d 30 20 29 7b 0a 20 20  f( pCtx==0 ){.  
1117d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1117e 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d  NOMEM;.  }.  mem
1117f 73 65 74 28 70 43 74 78 2c 20 30 2c 20 73 69 7a  set(pCtx, 0, siz
11180 65 6f 66 28 2a 70 43 74 78 29 29 3b 0a 0a 20 20  eof(*pCtx));..  
11181 72 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65  rc = proxyCreate
11182 43 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 64 62  ConchPathname(db
11183 50 61 74 68 2c 20 26 70 43 74 78 2d 3e 63 6f 6e  Path, &pCtx->con
11184 63 68 46 69 6c 65 50 61 74 68 29 3b 0a 20 20 69  chFilePath);.  i
11185 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
11186 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f   ){.    rc = pro
11187 78 79 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65  xyCreateUnixFile
11188 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65  (pCtx->conchFile
11189 50 61 74 68 2c 20 26 70 43 74 78 2d 3e 63 6f 6e  Path, &pCtx->con
1118a 63 68 46 69 6c 65 2c 20 30 29 3b 0a 20 20 20 20  chFile, 0);.    
1118b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43  if( rc==SQLITE_C
1118c 41 4e 54 4f 50 45 4e 20 26 26 20 28 28 70 46 69  ANTOPEN && ((pFi
1118d 6c 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 26 4f 5f  le->openFlags&O_
1118e 52 44 57 52 29 20 3d 3d 20 30 29 20 29 7b 0a 20  RDWR) == 0) ){. 
1118f 20 20 20 20 20 2f 2a 20 69 66 20 28 61 29 20 74       /* if (a) t
11190 68 65 20 6f 70 65 6e 20 66 6c 61 67 73 20 61 72  he open flags ar
11191 65 20 6e 6f 74 20 4f 5f 52 44 57 52 2c 20 28 62  e not O_RDWR, (b
11192 29 20 74 68 65 20 63 6f 6e 63 68 20 69 73 6e 27  ) the conch isn'
11193 74 20 74 68 65 72 65 2c 20 61 6e 64 0a 20 20 20  t there, and.   
11194 20 20 20 2a 2a 20 28 63 29 20 74 68 65 20 66 69     ** (c) the fi
11195 6c 65 20 73 79 73 74 65 6d 20 69 73 20 72 65 61  le system is rea
11196 64 2d 6f 6e 6c 79 2c 20 74 68 65 6e 20 65 6e 61  d-only, then ena
11197 62 6c 65 20 6e 6f 2d 6c 6f 63 6b 69 6e 67 20 61  ble no-locking a
11198 63 63 65 73 73 2e 0a 20 20 20 20 20 20 2a 2a 20  ccess..      ** 
11199 55 67 68 2c 20 73 69 6e 63 65 20 4f 5f 52 44 4f  Ugh, since O_RDO
1119a 4e 4c 59 3d 3d 30 78 30 30 30 30 20 77 65 20 74  NLY==0x0000 we t
1119b 65 73 74 20 66 6f 72 20 21 4f 5f 52 44 57 52 20  est for !O_RDWR 
1119c 73 69 6e 63 65 20 75 6e 69 78 4f 70 65 6e 20 61  since unixOpen a
1119d 73 73 65 72 74 73 0a 20 20 20 20 20 20 2a 2a 20  sserts.      ** 
1119e 74 68 61 74 20 6f 70 65 6e 46 6c 61 67 73 20 77  that openFlags w
1119f 69 6c 6c 20 68 61 76 65 20 6f 6e 6c 79 20 6f 6e  ill have only on
111a0 65 20 6f 66 20 4f 5f 52 44 4f 4e 4c 59 20 6f 72  e of O_RDONLY or
111a1 20 4f 5f 52 44 57 52 2e 0a 20 20 20 20 20 20 2a   O_RDWR..      *
111a2 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73  /.      struct s
111a3 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20  tatfs fsInfo;.  
111a4 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74 20      struct stat 
111a5 63 6f 6e 63 68 49 6e 66 6f 3b 0a 20 20 20 20 20  conchInfo;.     
111a6 20 69 6e 74 20 67 6f 4c 6f 63 6b 6c 65 73 73 20   int goLockless 
111a7 3d 20 30 3b 0a 0a 20 20 20 20 20 20 69 66 28 20  = 0;..      if( 
111a8 6f 73 53 74 61 74 28 70 43 74 78 2d 3e 63 6f 6e  osStat(pCtx->con
111a9 63 68 46 69 6c 65 50 61 74 68 2c 20 26 63 6f 6e  chFilePath, &con
111aa 63 68 49 6e 66 6f 29 20 3d 3d 20 2d 31 20 29 20  chInfo) == -1 ) 
111ab 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 65 72  {.        int er
111ac 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  r = errno;.     
111ad 20 20 20 69 66 28 20 28 65 72 72 3d 3d 45 4e 4f     if( (err==ENO
111ae 45 4e 54 29 20 26 26 20 28 73 74 61 74 66 73 28  ENT) && (statfs(
111af 64 62 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29  dbPath, &fsInfo)
111b0 20 21 3d 20 2d 31 29 20 29 7b 0a 20 20 20 20 20   != -1) ){.     
111b1 20 20 20 20 20 67 6f 4c 6f 63 6b 6c 65 73 73 20       goLockless 
111b2 3d 20 28 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67  = (fsInfo.f_flag
111b3 73 26 4d 4e 54 5f 52 44 4f 4e 4c 59 29 20 3d 3d  s&MNT_RDONLY) ==
111b4 20 4d 4e 54 5f 52 44 4f 4e 4c 59 3b 0a 20 20 20   MNT_RDONLY;.   
111b5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
111b6 20 20 20 20 20 69 66 28 20 67 6f 4c 6f 63 6b 6c       if( goLockl
111b7 65 73 73 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ess ){.        p
111b8 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d  Ctx->conchHeld =
111b9 20 2d 31 3b 20 2f 2a 20 72 65 61 64 20 6f 6e 6c   -1; /* read onl
111ba 79 20 46 53 2f 20 6c 6f 63 6b 6c 65 73 73 20 2a  y FS/ lockless *
111bb 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  /.        rc = S
111bc 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
111bd 7d 0a 20 20 20 20 7d 0a 20 20 7d 20 20 0a 20 20  }.    }.  }  .  
111be 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
111bf 4b 20 26 26 20 6c 6f 63 6b 50 61 74 68 20 29 7b  K && lockPath ){
111c0 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50  .    pCtx->lockP
111c1 72 6f 78 79 50 61 74 68 20 3d 20 73 71 6c 69 74  roxyPath = sqlit
111c2 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 6c 6f  e3DbStrDup(0, lo
111c3 63 6b 50 61 74 68 29 3b 0a 20 20 7d 0a 0a 20 20  ckPath);.  }..  
111c4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
111c5 4b 20 29 7b 0a 20 20 20 20 70 43 74 78 2d 3e 64  K ){.    pCtx->d
111c6 62 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44  bPath = sqlite3D
111c7 62 53 74 72 44 75 70 28 30 2c 20 64 62 50 61 74  bStrDup(0, dbPat
111c8 68 29 3b 0a 20 20 20 20 69 66 28 20 70 43 74 78  h);.    if( pCtx
111c9 2d 3e 64 62 50 61 74 68 3d 3d 4e 55 4c 4c 20 29  ->dbPath==NULL )
111ca 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
111cb 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
111cc 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
111cd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
111ce 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 69 73  /* all memory is
111cf 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72 6f 78   allocated, prox
111d0 79 73 20 61 72 65 20 63 72 65 61 74 65 64 20 61  ys are created a
111d1 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a 20 20  nd assigned, .  
111d2 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68 65 20    ** switch the 
111d3 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20  locking context 
111d4 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65 6e  and pMethod then
111d5 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a 2f 0a   return..    */.
111d6 20 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63      pCtx->oldLoc
111d7 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 46  kingContext = pF
111d8 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
111d9 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  ext;.    pFile->
111da 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d  lockingContext =
111db 20 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78 2d   pCtx;.    pCtx-
111dc 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70 46  >pOldMethod = pF
111dd 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20 20  ile->pMethod;.  
111de 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64    pFile->pMethod
111df 20 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f   = &proxyIoMetho
111e0 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ds;.  }else{.   
111e1 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68   if( pCtx->conch
111e2 46 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20 70  File ){ .      p
111e3 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 2d 3e  Ctx->conchFile->
111e4 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28  pMethod->xClose(
111e5 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
111e6 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29  pCtx->conchFile)
111e7 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
111e8 66 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68  free(pCtx->conch
111e9 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  File);.    }.   
111ea 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 30   sqlite3DbFree(0
111eb 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78  , pCtx->lockProx
111ec 79 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69  yPath);.    sqli
111ed 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 63  te3_free(pCtx->c
111ee 6f 6e 63 68 46 69 6c 65 50 61 74 68 29 3b 20 0a  onchFilePath); .
111ef 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
111f0 28 70 43 74 78 29 3b 0a 20 20 7d 0a 20 20 4f 53  (pCtx);.  }.  OS
111f1 54 52 41 43 45 28 28 22 54 52 41 4e 53 50 52 4f  TRACE(("TRANSPRO
111f2 58 59 20 20 25 64 20 25 73 5c 6e 22 2c 20 70 46  XY  %d %s\n", pF
111f3 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20  ile->h,.        
111f4 20 20 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f     (rc==SQLITE_O
111f5 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c  K ? "ok" : "fail
111f6 65 64 22 29 29 29 3b 0a 20 20 72 65 74 75 72 6e  ed")));.  return
111f7 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54   rc;.}.../*.** T
111f8 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 6e 64  his routine hand
111f9 6c 65 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  les sqlite3_file
111fa 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 61 6c 6c 73  _control() calls
111fb 20 74 68 61 74 20 61 72 65 20 73 70 65 63 69 66   that are specif
111fc 69 63 0a 2a 2a 20 74 6f 20 70 72 6f 78 79 20 6c  ic.** to proxy l
111fd 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  ocking..*/.stati
111fe 63 20 69 6e 74 20 70 72 6f 78 79 46 69 6c 65 43  c int proxyFileC
111ff 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66  ontrol(sqlite3_f
11200 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c  ile *id, int op,
11201 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20   void *pArg){.  
11202 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20  switch( op ){.  
11203 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 47 45    case SQLITE_GE
11204 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a  T_LOCKPROXYFILE:
11205 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c   {.      unixFil
11206 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
11207 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20  File*)id;.      
11208 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68  if( pFile->pMeth
11209 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49 6f 4d 65  od == &proxyIoMe
1120a 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20  thods ){.       
1120b 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
1120c 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72  text *pCtx = (pr
1120d 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
1120e 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e  t*)pFile->lockin
1120f 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20  gContext;.      
11210 20 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68    proxyTakeConch
11211 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 20  (pFile);.       
11212 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b 50   if( pCtx->lockP
11213 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20 20  roxyPath ){.    
11214 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 20 63 68        *(const ch
11215 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 70 43 74  ar **)pArg = pCt
11216 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
11217 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
11218 0a 20 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e  .          *(con
11219 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20  st char **)pArg 
1121a 3d 20 22 3a 61 75 74 6f 3a 20 28 6e 6f 74 20 68  = ":auto: (not h
1121b 65 6c 64 29 22 3b 0a 20 20 20 20 20 20 20 20 7d  eld)";.        }
1121c 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a  .      } else {.
1121d 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 20          *(const 
1121e 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 4e  char **)pArg = N
1121f 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ULL;.      }.   
11220 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
11221 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  _OK;.    }.    c
11222 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54 5f 4c  ase SQLITE_SET_L
11223 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20 7b 0a  OCKPROXYFILE: {.
11224 20 20 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a        unixFile *
11225 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
11226 65 2a 29 69 64 3b 0a 20 20 20 20 20 20 69 6e 74  e*)id;.      int
11227 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
11228 0a 20 20 20 20 20 20 69 6e 74 20 69 73 50 72 6f  .      int isPro
11229 78 79 53 74 79 6c 65 20 3d 20 28 70 46 69 6c 65  xyStyle = (pFile
1122a 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 70 72  ->pMethod == &pr
1122b 6f 78 79 49 6f 4d 65 74 68 6f 64 73 29 3b 0a 20  oxyIoMethods);. 
1122c 20 20 20 20 20 69 66 28 20 70 41 72 67 3d 3d 4e       if( pArg==N
1122d 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74 20 63 68  ULL || (const ch
1122e 61 72 20 2a 29 70 41 72 67 3d 3d 30 20 29 7b 0a  ar *)pArg==0 ){.
1122f 20 20 20 20 20 20 20 20 69 66 28 20 69 73 50 72          if( isPr
11230 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20 20 20 20  oxyStyle ){.    
11231 20 20 20 20 20 20 2f 2a 20 74 75 72 6e 20 6f 66        /* turn of
11232 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20  f proxy locking 
11233 2d 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 64 20  - not supported 
11234 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  */.          rc 
11235 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 2f  = SQLITE_ERROR /
11236 2a 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c  *SQLITE_PROTOCOL
11237 3f 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3f  ? SQLITE_MISUSE?
11238 2a 2f 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  */;.        }els
11239 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e{.          /* 
1123a 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 79 20 6c  turn off proxy l
1123b 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 65 61 64 79  ocking - already
1123c 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a 2f 0a 20   off - NOOP */. 
1123d 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
1123e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  LITE_OK;.       
1123f 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
11240 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
11241 61 72 20 2a 70 72 6f 78 79 50 61 74 68 20 3d 20  ar *proxyPath = 
11242 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 41  (const char *)pA
11243 72 67 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  rg;.        if( 
11244 69 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a  isProxyStyle ){.
11245 20 20 20 20 20 20 20 20 20 20 70 72 6f 78 79 4c            proxyL
11246 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70  ockingContext *p
11247 43 74 78 20 3d 20 0a 20 20 20 20 20 20 20 20 20  Ctx = .         
11248 20 20 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67     (proxyLocking
11249 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e  Context*)pFile->
1124a 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  lockingContext;.
1124b 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 73            if( !s
1124c 74 72 63 6d 70 28 70 41 72 67 2c 20 22 3a 61 75  trcmp(pArg, ":au
1124d 74 6f 3a 22 29 20 0a 20 20 20 20 20 20 20 20 20  to:") .         
1124e 20 20 7c 7c 20 28 70 43 74 78 2d 3e 6c 6f 63 6b    || (pCtx->lock
1124f 50 72 6f 78 79 50 61 74 68 20 26 26 0a 20 20 20  ProxyPath &&.   
11250 20 20 20 20 20 20 20 20 20 20 20 20 21 73 74 72              !str
11251 6e 63 6d 70 28 70 43 74 78 2d 3e 6c 6f 63 6b 50  ncmp(pCtx->lockP
11252 72 6f 78 79 50 61 74 68 2c 20 70 72 6f 78 79 50  roxyPath, proxyP
11253 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29  ath, MAXPATHLEN)
11254 29 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20  ).          ){. 
11255 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
11256 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
11257 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
11258 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 77 69          rc = swi
11259 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50 61 74 68  tchLockProxyPath
1125a 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 61 74  (pFile, proxyPat
1125b 68 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  h);.          }.
1125c 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1125d 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e           /* turn
1125e 20 6f 6e 20 70 72 6f 78 79 20 66 69 6c 65 20 6c   on proxy file l
1125f 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20  ocking */.      
11260 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54 72      rc = proxyTr
11261 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28  ansformUnixFile(
11262 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 61 74 68  pFile, proxyPath
11263 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
11264 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
11265 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
11266 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
11267 20 61 73 73 65 72 74 28 20 30 20 29 3b 20 20 2f   assert( 0 );  /
11268 2a 20 54 68 65 20 63 61 6c 6c 20 61 73 73 75 72  * The call assur
11269 65 73 20 74 68 61 74 20 6f 6e 6c 79 20 76 61 6c  es that only val
1126a 69 64 20 6f 70 63 6f 64 65 73 20 61 72 65 20 73  id opcodes are s
1126b 65 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d  ent */.    }.  }
1126c 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44 2a  .  /*NOTREACHED*
1126d 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  /.  return SQLIT
1126e 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a  E_ERROR;.}../*.*
1126f 2a 20 57 69 74 68 69 6e 20 74 68 69 73 20 64 69  * Within this di
11270 76 69 73 69 6f 6e 20 28 74 68 65 20 70 72 6f 78  vision (the prox
11271 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 69 6d 70  ying locking imp
11272 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 20 74 68 65  lementation) the
11273 20 70 72 6f 63 65 64 75 72 65 73 0a 2a 2a 20 61   procedures.** a
11274 62 6f 76 65 20 74 68 69 73 20 70 6f 69 6e 74 20  bove this point 
11275 61 72 65 20 61 6c 6c 20 75 74 69 6c 69 74 69 65  are all utilitie
11276 73 2e 20 20 54 68 65 20 6c 6f 63 6b 2d 72 65 6c  s.  The lock-rel
11277 61 74 65 64 20 6d 65 74 68 6f 64 73 20 6f 66 20  ated methods of 
11278 74 68 65 0a 2a 2a 20 70 72 6f 78 79 2d 6c 6f 63  the.** proxy-loc
11279 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6f 5f  king sqlite3_io_
1127a 6d 65 74 68 6f 64 20 6f 62 6a 65 63 74 20 66 6f  method object fo
1127b 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  llow..*/.../*.**
1127c 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
1127d 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73  ecks if there is
1127e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
1127f 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65   held on the spe
11280 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62  cified.** file b
11281 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74  y this or any ot
11282 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20  her process. If 
11283 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68  such a lock is h
11284 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75  eld, set *pResOu
11285 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65  t.** to a non-ze
11286 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69  ro value otherwi
11287 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73  se *pResOut is s
11288 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65  et to zero.  The
11289 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
1128a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54   is set to SQLIT
1128b 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49  E_OK unless an I
1128c 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  /O error occurs 
1128d 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63  during lock chec
1128e 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  king..*/.static 
1128f 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b 52 65  int proxyCheckRe
11290 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74  servedLock(sqlit
11291 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
11292 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 20 75   *pResOut) {.  u
11293 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
11294 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
11295 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79    int rc = proxy
11296 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29  TakeConch(pFile)
11297 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
11298 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f  TE_OK ){.    pro
11299 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
1129a 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c   *pCtx = (proxyL
1129b 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29  ockingContext *)
1129c 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
1129d 6e 74 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70  ntext;.    if( p
1129e 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 3e 30  Ctx->conchHeld>0
1129f 20 29 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69   ){.      unixFi
112a0 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74 78  le *proxy = pCtx
112a1 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20  ->lockProxy;.   
112a2 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78 79 2d     return proxy-
112a3 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68 65 63 6b  >pMethod->xCheck
112a4 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 28 73 71  ReservedLock((sq
112a5 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78  lite3_file*)prox
112a6 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a 20 20 20  y, pResOut);.   
112a7 20 7d 65 6c 73 65 7b 20 2f 2a 20 63 6f 6e 63 68   }else{ /* conch
112a8 48 65 6c 64 20 3c 20 30 20 69 73 20 6c 6f 63 6b  Held < 0 is lock
112a9 6c 65 73 73 20 2a 2f 0a 20 20 20 20 20 20 70 52  less */.      pR
112aa 65 73 4f 75 74 3d 30 3b 0a 20 20 20 20 7d 0a 20  esOut=0;.    }. 
112ab 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
112ac 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
112ad 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
112ae 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
112af 79 20 70 61 72 61 6d 65 74 65 72 20 65 46 69 6c  y parameter eFil
112b0 65 4c 6f 63 6b 20 2d 20 6f 6e 65 0a 2a 2a 20 6f  eLock - one.** o
112b1 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  f the following:
112b2 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53  .**.**     (1) S
112b3 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  HARED_LOCK.**   
112b4 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
112b5 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50  OCK.**     (3) P
112b6 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20  ENDING_LOCK.**  
112b7 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
112b8 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  _LOCK.**.** Some
112b9 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65  times when reque
112ba 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73  sting one lock s
112bb 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c  tate, additional
112bc 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20   lock states.** 
112bd 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
112be 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
112bf 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
112c0 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
112c1 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69  ater.** transiti
112c2 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20  ons leaving the 
112c3 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65  lock state diffe
112c4 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69  rent from what i
112c5 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a  t started but.**
112c6 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20   still short of 
112c7 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66  its goal.  The f
112c8 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73  ollowing chart s
112c9 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64  hows the allowed
112ca 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
112cb 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64  and the inserted
112cc 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74   intermediate st
112cd 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55  ates:.**.**    U
112ce 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
112cf 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
112d0 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20  > RESERVED.**   
112d1 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44   SHARED -> (PEND
112d2 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
112d3 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44  E.**    RESERVED
112d4 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
112d5 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
112d6 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
112d7 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73  USIVE.**.** This
112d8 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e   routine will on
112d9 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f  ly increase a lo
112da 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c  ck.  Use the sql
112db 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a  ite3OsUnlock().*
112dc 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77  * routine to low
112dd 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  er a locking lev
112de 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  el..*/.static in
112df 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69  t proxyLock(sqli
112e0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
112e1 74 20 65 46 69 6c 65 4c 6f 63 6b 29 20 7b 0a 20  t eFileLock) {. 
112e2 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
112e3 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
112e4 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f  ;.  int rc = pro
112e5 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c  xyTakeConch(pFil
112e6 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  e);.  if( rc==SQ
112e7 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
112e8 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
112e9 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78  xt *pCtx = (prox
112ea 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
112eb 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
112ec 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 69 66 28  Context;.    if(
112ed 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64   pCtx->conchHeld
112ee 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 6e 69 78  >0 ){.      unix
112ef 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43  File *proxy = pC
112f0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20  tx->lockProxy;. 
112f1 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d       rc = proxy-
112f2 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28  >pMethod->xLock(
112f3 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70  (sqlite3_file*)p
112f4 72 6f 78 79 2c 20 65 46 69 6c 65 4c 6f 63 6b 29  roxy, eFileLock)
112f5 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 65  ;.      pFile->e
112f6 46 69 6c 65 4c 6f 63 6b 20 3d 20 70 72 6f 78 79  FileLock = proxy
112f7 2d 3e 65 46 69 6c 65 4c 6f 63 6b 3b 0a 20 20 20  ->eFileLock;.   
112f8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
112f9 20 63 6f 6e 63 68 48 65 6c 64 20 3c 20 30 20 69   conchHeld < 0 i
112fa 73 20 6c 6f 63 6b 6c 65 73 73 20 2a 2f 0a 20 20  s lockless */.  
112fb 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
112fc 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c   rc;.}.../*.** L
112fd 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ower the locking
112fe 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64   level on file d
112ff 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20  escriptor pFile 
11300 74 6f 20 65 46 69 6c 65 4c 6f 63 6b 2e 20 20 65  to eFileLock.  e
11301 46 69 6c 65 4c 6f 63 6b 0a 2a 2a 20 6d 75 73 74  FileLock.** must
11302 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f   be either NO_LO
11303 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43  CK or SHARED_LOC
11304 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  K..**.** If the 
11305 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66  locking level of
11306 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
11307 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20  ptor is already 
11308 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74  at or below.** t
11309 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
1130a 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73  king level, this
1130b 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
1130c 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  -op..*/.static i
1130d 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73  nt proxyUnlock(s
1130e 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
1130f 20 69 6e 74 20 65 46 69 6c 65 4c 6f 63 6b 29 20   int eFileLock) 
11310 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  {.  unixFile *pF
11311 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
11312 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  )id;.  int rc = 
11313 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70  proxyTakeConch(p
11314 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d  File);.  if( rc=
11315 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
11316 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f    proxyLockingCo
11317 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70  ntext *pCtx = (p
11318 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
11319 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  xt *)pFile->lock
1131a 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  ingContext;.    
1131b 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48  if( pCtx->conchH
1131c 65 6c 64 3e 30 20 29 7b 0a 20 20 20 20 20 20 75  eld>0 ){.      u
1131d 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d  nixFile *proxy =
1131e 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
1131f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f  ;.      rc = pro
11320 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e  xy->pMethod->xUn
11321 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69  lock((sqlite3_fi
11322 6c 65 2a 29 70 72 6f 78 79 2c 20 65 46 69 6c 65  le*)proxy, eFile
11323 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 70 46 69  Lock);.      pFi
11324 6c 65 2d 3e 65 46 69 6c 65 4c 6f 63 6b 20 3d 20  le->eFileLock = 
11325 70 72 6f 78 79 2d 3e 65 46 69 6c 65 4c 6f 63 6b  proxy->eFileLock
11326 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
11327 20 20 20 2f 2a 20 63 6f 6e 63 68 48 65 6c 64 20     /* conchHeld 
11328 3c 20 30 20 69 73 20 6c 6f 63 6b 6c 65 73 73 20  < 0 is lockless 
11329 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  */.    }.  }.  r
1132a 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1132b 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20  ** Close a file 
1132c 74 68 61 74 20 75 73 65 73 20 70 72 6f 78 79 20  that uses proxy 
1132d 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  locks..*/.static
1132e 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 28   int proxyClose(
1132f 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
11330 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a  ) {.  if( id ){.
11331 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46      unixFile *pF
11332 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
11333 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79 4c 6f  )id;.    proxyLo
11334 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
11335 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69  tx = (proxyLocki
11336 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c  ngContext *)pFil
11337 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
11338 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20  t;.    unixFile 
11339 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 74  *lockProxy = pCt
1133a 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20  x->lockProxy;.  
1133b 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63    unixFile *conc
1133c 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f  hFile = pCtx->co
1133d 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74  nchFile;.    int
1133e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1133f 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 6c 6f  .    .    if( lo
11340 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 20  ckProxy ){.     
11341 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 2d   rc = lockProxy-
11342 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63  >pMethod->xUnloc
11343 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  k((sqlite3_file*
11344 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f 4c  )lockProxy, NO_L
11345 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20  OCK);.      if( 
11346 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
11347 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50        rc = lockP
11348 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  roxy->pMethod->x
11349 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66  Close((sqlite3_f
1134a 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b  ile*)lockProxy);
1134b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20  .      if( rc ) 
1134c 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
1134d 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f   sqlite3_free(lo
1134e 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 20  ckProxy);.      
1134f 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20  pCtx->lockProxy 
11350 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 0;.    }.    i
11351 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29 7b 0a  f( conchFile ){.
11352 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e        if( pCtx->
11353 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20 20 20  conchHeld ){.   
11354 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 52       rc = proxyR
11355 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 69 6c  eleaseConch(pFil
11356 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  e);.        if( 
11357 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
11358 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
11359 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d   = conchFile->pM
1135a 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73  ethod->xClose((s
1135b 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e  qlite3_file*)con
1135c 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69  chFile);.      i
1135d 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1135e 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  c;.      sqlite3
1135f 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c 65 29  _free(conchFile)
11360 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
11361 74 65 33 44 62 46 72 65 65 28 30 2c 20 70 43 74  te3DbFree(0, pCt
11362 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
11363 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
11364 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46  ree(pCtx->conchF
11365 69 6c 65 50 61 74 68 29 3b 0a 20 20 20 20 73 71  ilePath);.    sq
11366 6c 69 74 65 33 44 62 46 72 65 65 28 30 2c 20 70  lite3DbFree(0, p
11367 43 74 78 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20  Ctx->dbPath);.  
11368 20 20 2f 2a 20 72 65 73 74 6f 72 65 20 74 68 65    /* restore the
11369 20 6f 72 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 6e   original lockin
1136a 67 20 63 6f 6e 74 65 78 74 20 61 6e 64 20 70 4d  g context and pM
1136b 65 74 68 6f 64 20 74 68 65 6e 20 63 6c 6f 73 65  ethod then close
1136c 20 69 74 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65   it */.    pFile
1136d 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
1136e 20 3d 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b   = pCtx->oldLock
1136f 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  ingContext;.    
11370 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d  pFile->pMethod =
11371 20 70 43 74 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f   pCtx->pOldMetho
11372 64 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  d;.    sqlite3_f
11373 72 65 65 28 70 43 74 78 29 3b 0a 20 20 20 20 72  ree(pCtx);.    r
11374 65 74 75 72 6e 20 70 46 69 6c 65 2d 3e 70 4d 65  eturn pFile->pMe
11375 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 69 64 29  thod->xClose(id)
11376 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
11377 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 23  QLITE_OK;.}....#
11378 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64  endif /* defined
11379 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53  (__APPLE__) && S
1137a 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
1137b 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 2f 2a  KING_STYLE */./*
1137c 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f  .** The proxy lo
1137d 63 6b 69 6e 67 20 73 74 79 6c 65 20 69 73 20 69  cking style is i
1137e 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
1137f 77 69 74 68 20 41 46 50 20 66 69 6c 65 73 79 73  with AFP filesys
11380 74 65 6d 73 2e 0a 2a 2a 20 41 6e 64 20 73 69 6e  tems..** And sin
11381 63 65 20 41 46 50 20 69 73 20 6f 6e 6c 79 20 73  ce AFP is only s
11382 75 70 70 6f 72 74 65 64 20 6f 6e 20 4d 61 63 4f  upported on MacO
11383 53 58 2c 20 74 68 65 20 70 72 6f 78 79 20 6c 6f  SX, the proxy lo
11384 63 6b 69 6e 67 20 69 73 20 61 6c 73 6f 0a 2a 2a  cking is also.**
11385 20 72 65 73 74 72 69 63 74 65 64 20 74 6f 20 4d   restricted to M
11386 61 63 4f 53 58 2e 0a 2a 2a 20 0a 2a 2a 0a 2a 2a  acOSX..** .**.**
11387 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11388 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 70 72 6f  * End of the pro
11389 78 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e  xy lock implemen
1138a 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tation *********
1138b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
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 2a 2a  ****************
1138e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1138f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
11391 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
11392 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
11393 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a  stem interface..
11394 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
11395 6e 65 20 72 65 67 69 73 74 65 72 73 20 61 6c 6c  ne registers all
11396 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74   VFS implementat
11397 69 6f 6e 73 20 66 6f 72 20 75 6e 69 78 2d 6c 69  ions for unix-li
11398 6b 65 20 6f 70 65 72 61 74 69 6e 67 0a 2a 2a 20  ke operating.** 
11399 73 79 73 74 65 6d 73 2e 20 20 54 68 69 73 20 72  systems.  This r
1139a 6f 75 74 69 6e 65 2c 20 61 6e 64 20 74 68 65 20  outine, and the 
1139b 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29  sqlite3_os_end()
1139c 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 66 6f   routine that fo
1139d 6c 6c 6f 77 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64  llows,.** should
1139e 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75   be the only rou
1139f 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
113a0 6c 65 20 74 68 61 74 20 61 72 65 20 76 69 73 69  le that are visi
113a1 62 6c 65 20 66 72 6f 6d 20 6f 74 68 65 72 0a 2a  ble from other.*
113a2 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  * files..**.** T
113a3 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
113a4 61 6c 6c 65 64 20 6f 6e 63 65 20 64 75 72 69 6e  alled once durin
113a5 67 20 53 51 4c 69 74 65 20 69 6e 69 74 69 61 6c  g SQLite initial
113a6 69 7a 61 74 69 6f 6e 20 61 6e 64 20 62 79 20 61  ization and by a
113a7 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 68 72 65 61  .** single threa
113a8 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61  d.  The memory a
113a9 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 6d 75  llocation and mu
113aa 74 65 78 20 73 75 62 73 79 73 74 65 6d 73 20 68  tex subsystems h
113ab 61 76 65 20 6e 6f 74 0a 2a 2a 20 6e 65 63 65 73  ave not.** neces
113ac 73 61 72 69 6c 79 20 62 65 65 6e 20 69 6e 69 74  sarily been init
113ad 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 74 68 69  ialized when thi
113ae 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
113af 6c 65 64 2c 20 61 6e 64 20 73 6f 20 74 68 65 79  led, and so they
113b0 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  .** should not b
113b1 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  e used..*/.SQLIT
113b2 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
113b3 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b  3_os_init(void){
113b4 20 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 54 68 65   .  /* .  ** The
113b5 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
113b6 20 64 65 66 69 6e 65 73 20 61 6e 20 69 6e 69 74   defines an init
113b7 69 61 6c 69 7a 65 72 20 66 6f 72 20 61 6e 20 73  ializer for an s
113b8 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63  qlite3_vfs objec
113b9 74 2e 0a 20 20 2a 2a 20 54 68 65 20 6e 61 6d 65  t..  ** The name
113ba 20 6f 66 20 74 68 65 20 56 46 53 20 69 73 20 4e   of the VFS is N
113bb 41 4d 45 2e 20 20 54 68 65 20 70 41 70 70 44 61  AME.  The pAppDa
113bc 74 61 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ta is a pointer 
113bd 74 6f 20 61 20 70 6f 69 6e 74 65 72 0a 20 20 2a  to a pointer.  *
113be 2a 20 74 6f 20 74 68 65 20 22 66 69 6e 64 65 72  * to the "finder
113bf 22 20 66 75 6e 63 74 69 6f 6e 2e 20 20 28 70 41  " function.  (pA
113c0 70 70 44 61 74 61 20 69 73 20 61 20 70 6f 69 6e  ppData is a poin
113c1 74 65 72 20 74 6f 20 61 20 70 6f 69 6e 74 65 72  ter to a pointer
113c2 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 73 69   because.  ** si
113c3 6c 6c 79 20 43 39 30 20 72 75 6c 65 73 20 70 72  lly C90 rules pr
113c4 6f 68 69 62 69 74 20 61 20 76 6f 69 64 2a 20 66  ohibit a void* f
113c5 72 6f 6d 20 62 65 69 6e 67 20 63 61 73 74 20 74  rom being cast t
113c6 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  o a function poi
113c7 6e 74 65 72 0a 20 20 2a 2a 20 61 6e 64 20 73 6f  nter.  ** and so
113c8 20 77 65 20 68 61 76 65 20 74 6f 20 67 6f 20 74   we have to go t
113c9 68 72 6f 75 67 68 20 74 68 65 20 69 6e 74 65 72  hrough the inter
113ca 6d 65 64 69 61 74 65 20 70 6f 69 6e 74 65 72 20  mediate pointer 
113cb 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d  to avoid problem
113cc 73 0a 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70  s.  ** when comp
113cd 69 6c 69 6e 67 20 77 69 74 68 20 2d 70 65 64 61  iling with -peda
113ce 6e 74 69 63 2d 65 72 72 6f 72 73 20 6f 6e 20 47  ntic-errors on G
113cf 43 43 2e 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  CC.).  **.  ** T
113d0 68 65 20 46 49 4e 44 45 52 20 70 61 72 61 6d 65  he FINDER parame
113d1 74 65 72 20 74 6f 20 74 68 69 73 20 6d 61 63 72  ter to this macr
113d2 6f 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  o is the name of
113d3 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20   the pointer to 
113d4 74 68 65 0a 20 20 2a 2a 20 66 69 6e 64 65 72 2d  the.  ** finder-
113d5 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 66  function.  The f
113d6 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72  inder-function r
113d7 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
113d8 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c   to the.  ** sql
113d9 69 74 65 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f  ite_io_methods o
113da 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65  bject that imple
113db 6d 65 6e 74 73 20 74 68 65 20 64 65 73 69 72 65  ments the desire
113dc 64 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 62  d locking.  ** b
113dd 65 68 61 76 69 6f 72 73 2e 20 20 53 65 65 20 74  ehaviors.  See t
113de 68 65 20 64 69 76 69 73 69 6f 6e 20 61 62 6f 76  he division abov
113df 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
113e0 74 68 65 20 49 4f 4d 45 54 48 4f 44 53 0a 20 20  the IOMETHODS.  
113e1 2a 2a 20 6d 61 63 72 6f 20 66 6f 72 20 61 64 64  ** macro for add
113e2 69 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f  ition informatio
113e3 6e 20 6f 6e 20 66 69 6e 64 65 72 2d 66 75 6e 63  n on finder-func
113e4 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
113e5 20 4d 6f 73 74 20 66 69 6e 64 65 72 73 20 73 69   Most finders si
113e6 6d 70 6c 79 20 72 65 74 75 72 6e 20 61 20 70 6f  mply return a po
113e7 69 6e 74 65 72 20 74 6f 20 61 20 66 69 78 65 64  inter to a fixed
113e8 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
113e9 6f 64 73 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 2e  ods.  ** object.
113ea 20 20 42 75 74 20 74 68 65 20 22 61 75 74 6f 6c    But the "autol
113eb 6f 63 6b 49 6f 46 69 6e 64 65 72 22 20 61 76 61  ockIoFinder" ava
113ec 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58  ilable on MacOSX
113ed 20 64 6f 65 73 20 61 20 6c 69 74 74 6c 65 0a 20   does a little. 
113ee 20 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 74 68   ** more than th
113ef 61 74 3b 20 69 74 20 6c 6f 6f 6b 73 20 61 74 20  at; it looks at 
113f0 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 74  the filesystem t
113f1 79 70 65 20 74 68 61 74 20 68 6f 73 74 73 20 74  ype that hosts t
113f2 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  he .  ** databas
113f3 65 20 66 69 6c 65 20 61 6e 64 20 74 72 69 65 73  e file and tries
113f4 20 74 6f 20 63 68 6f 6f 73 65 20 61 6e 20 6c 6f   to choose an lo
113f5 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 61 70 70  cking method app
113f6 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 20 20 2a  ropriate for.  *
113f7 2a 20 74 68 61 74 20 66 69 6c 65 73 79 73 74 65  * that filesyste
113f8 6d 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 23  m time..  */.  #
113f9 64 65 66 69 6e 65 20 55 4e 49 58 56 46 53 28 56  define UNIXVFS(V
113fa 46 53 4e 41 4d 45 2c 20 46 49 4e 44 45 52 29 20  FSNAME, FINDER) 
113fb 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
113fc 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 33           \.    3
113fd 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
113fe 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
113ff 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
11400 20 20 20 20 20 20 20 5c 0a 20 20 20 20 73 69 7a         \.    siz
11401 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 2c 20 20  eof(unixFile),  
11402 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a     /* szOsFile *
11403 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
11404 20 20 20 20 20 5c 0a 20 20 20 20 4d 41 58 5f 50       \.    MAX_P
11405 41 54 48 4e 41 4d 45 2c 20 20 20 20 20 20 20 20  ATHNAME,        
11406 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a   /* mxPathname *
11407 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
11408 20 20 20 5c 0a 20 20 20 20 30 2c 20 20 20 20 20     \.    0,     
11409 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1140a 2a 20 70 4e 65 78 74 20 2a 2f 20 20 20 20 20 20  * pNext */      
1140b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1140c 20 5c 0a 20 20 20 20 56 46 53 4e 41 4d 45 2c 20   \.    VFSNAME, 
1140d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1140e 7a 4e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20  zName */        
1140f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
11410 0a 20 20 20 20 28 76 6f 69 64 2a 29 26 46 49 4e  .    (void*)&FIN
11411 44 45 52 2c 20 20 20 20 20 20 20 2f 2a 20 70 41  DER,       /* pA
11412 70 70 44 61 74 61 20 2a 2f 20 20 20 20 20 20 20  ppData */       
11413 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
11414 20 20 20 75 6e 69 78 4f 70 65 6e 2c 20 20 20 20     unixOpen,    
11415 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65           /* xOpe
11416 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  n */            
11417 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
11418 20 75 6e 69 78 44 65 6c 65 74 65 2c 20 20 20 20   unixDelete,    
11419 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74         /* xDelet
1141a 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  e */            
1141b 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75           \.    u
1141c 6e 69 78 41 63 63 65 73 73 2c 20 20 20 20 20 20  nixAccess,      
1141d 20 20 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20       /* xAccess 
1141e 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
1141f 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69         \.    uni
11420 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20  xFullPathname,  
11421 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e     /* xFullPathn
11422 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ame */          
11423 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44       \.    unixD
11424 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20  lOpen,          
11425 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 20 20   /* xDlOpen */  
11426 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11427 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 45     \.    unixDlE
11428 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20 2f  rror,          /
11429 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 20 20 20  * xDlError */   
1142a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1142b 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 53 79 6d   \.    unixDlSym
1142c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
1142d 78 44 6c 53 79 6d 20 2a 2f 20 20 20 20 20 20 20  xDlSym */       
1142e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
1142f 0a 20 20 20 20 75 6e 69 78 44 6c 43 6c 6f 73 65  .    unixDlClose
11430 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44  ,          /* xD
11431 6c 43 6c 6f 73 65 20 2a 2f 20 20 20 20 20 20 20  lClose */       
11432 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
11433 20 20 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73     unixRandomnes
11434 73 2c 20 20 20 20 20 20 20 2f 2a 20 78 52 61 6e  s,       /* xRan
11435 64 6f 6d 6e 65 73 73 20 2a 2f 20 20 20 20 20 20  domness */      
11436 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
11437 20 75 6e 69 78 53 6c 65 65 70 2c 20 20 20 20 20   unixSleep,     
11438 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70         /* xSleep
11439 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
1143a 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75           \.    u
1143b 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65 2c 20  nixCurrentTime, 
1143c 20 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74       /* xCurrent
1143d 54 69 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20  Time */         
1143e 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69         \.    uni
1143f 78 47 65 74 4c 61 73 74 45 72 72 6f 72 2c 20 20  xGetLastError,  
11440 20 20 20 2f 2a 20 78 47 65 74 4c 61 73 74 45 72     /* xGetLastEr
11441 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ror */          
11442 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 43       \.    unixC
11443 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 2c  urrentTimeInt64,
11444 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65   /* xCurrentTime
11445 49 6e 74 36 34 20 2a 2f 20 20 20 20 20 20 20 20  Int64 */        
11446 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 65 74     \.    unixSet
11447 53 79 73 74 65 6d 43 61 6c 6c 2c 20 20 20 20 2f  SystemCall,    /
11448 2a 20 78 53 65 74 53 79 73 74 65 6d 43 61 6c 6c  * xSetSystemCall
11449 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
1144a 20 5c 0a 20 20 20 20 75 6e 69 78 47 65 74 53 79   \.    unixGetSy
1144b 73 74 65 6d 43 61 6c 6c 2c 20 20 20 20 2f 2a 20  stemCall,    /* 
1144c 78 47 65 74 53 79 73 74 65 6d 43 61 6c 6c 20 2a  xGetSystemCall *
1144d 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c  /              \
1144e 0a 20 20 20 20 75 6e 69 78 4e 65 78 74 53 79 73  .    unixNextSys
1144f 74 65 6d 43 61 6c 6c 2c 20 20 20 2f 2a 20 78 4e  temCall,   /* xN
11450 65 78 74 53 79 73 74 65 6d 43 61 6c 6c 20 2a 2f  extSystemCall */
11451 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
11452 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c   }..  /*.  ** Al
11453 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65 73 20  l default VFSes 
11454 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63 6f 6e  for unix are con
11455 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 6f  tained in the fo
11456 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e 0a 20  llowing array.. 
11457 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68   **.  ** Note th
11458 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  at the sqlite3_v
11459 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64 20 6f  fs.pNext field o
1145a 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63 74  f the VFS object
1145b 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20 20 2a   is modified.  *
1145c 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20  * by the SQLite 
1145d 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20 56 46  core when the VF
1145e 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e  S is registered.
1145f 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69    So the followi
11460 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20 63 61  ng.  ** array ca
11461 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e 0a 20  nnot be const.. 
11462 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73 71 6c   */.  static sql
11463 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b 5d 20  ite3_vfs aVfs[] 
11464 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 45  = {.#if SQLITE_E
11465 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
11466 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 57 4f 52  YLE && (OS_VXWOR
11467 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  KS || defined(__
11468 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20 20 55 4e  APPLE__)).    UN
11469 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20 20  IXVFS("unix",   
1146a 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63 6b 49         autolockI
1146b 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c 73 65  oFinder ),.#else
1146c 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e  .    UNIXVFS("un
1146d 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 70 6f  ix",          po
1146e 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23  sixIoFinder ),.#
1146f 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58 56 46  endif.    UNIXVF
11470 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20 20  S("unix-none",  
11471 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65     nolockIoFinde
11472 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53  r ),.    UNIXVFS
11473 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22 2c  ("unix-dotfile",
11474 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65    dotlockIoFinde
11475 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53  r ),.    UNIXVFS
11476 28 22 75 6e 69 78 2d 65 78 63 6c 22 2c 20 20 20  ("unix-excl",   
11477 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 20    posixIoFinder 
11478 29 2c 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b  ),.#if OS_VXWORK
11479 53 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75  S.    UNIXVFS("u
1147a 6e 69 78 2d 6e 61 6d 65 64 73 65 6d 22 2c 20 73  nix-namedsem", s
1147b 65 6d 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65  emIoFinder ),.#e
1147c 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
1147d 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
1147e 54 59 4c 45 0a 20 20 20 20 55 4e 49 58 56 46 53  TYLE.    UNIXVFS
1147f 28 22 75 6e 69 78 2d 70 6f 73 69 78 22 2c 20 20  ("unix-posix",  
11480 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 20    posixIoFinder 
11481 29 2c 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f 52  ),.#if !OS_VXWOR
11482 4b 53 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22  KS.    UNIXVFS("
11483 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c 20 20 20 20  unix-flock",    
11484 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c  flockIoFinder ),
11485 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23  .#endif.#endif.#
11486 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  if SQLITE_ENABLE
11487 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26  _LOCKING_STYLE &
11488 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  & defined(__APPL
11489 45 5f 5f 29 0a 20 20 20 20 55 4e 49 58 56 46 53  E__).    UNIXVFS
1148a 28 22 75 6e 69 78 2d 61 66 70 22 2c 20 20 20 20  ("unix-afp",    
1148b 20 20 61 66 70 49 6f 46 69 6e 64 65 72 20 29 2c    afpIoFinder ),
1148c 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e  .    UNIXVFS("un
1148d 69 78 2d 6e 66 73 22 2c 20 20 20 20 20 20 6e 66  ix-nfs",      nf
1148e 73 49 6f 46 69 6e 64 65 72 20 29 2c 0a 20 20 20  sIoFinder ),.   
1148f 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70   UNIXVFS("unix-p
11490 72 6f 78 79 22 2c 20 20 20 20 70 72 6f 78 79 49  roxy",    proxyI
11491 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69  oFinder ),.#endi
11492 66 0a 20 20 7d 3b 0a 20 20 75 6e 73 69 67 6e 65  f.  };.  unsigne
11493 64 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20  d int i;        
11494 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
11495 72 20 2a 2f 0a 0a 20 20 2f 2a 20 44 6f 75 62 6c  r */..  /* Doubl
11496 65 2d 63 68 65 63 6b 20 74 68 61 74 20 74 68 65  e-check that the
11497 20 61 53 79 73 63 61 6c 6c 5b 5d 20 61 72 72 61   aSyscall[] arra
11498 79 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 73 74  y has been const
11499 72 75 63 74 65 64 0a 20 20 2a 2a 20 63 6f 72 72  ructed.  ** corr
1149a 65 63 74 6c 79 2e 20 20 53 65 65 20 74 69 63 6b  ectly.  See tick
1149b 65 74 20 5b 62 62 33 61 38 36 65 38 39 30 63 38  et [bb3a86e890c8
1149c 65 39 36 61 62 5d 20 2a 2f 0a 20 20 61 73 73 65  e96ab] */.  asse
1149d 72 74 28 20 41 72 72 61 79 53 69 7a 65 28 61 53  rt( ArraySize(aS
1149e 79 73 63 61 6c 6c 29 3d 3d 32 32 20 29 3b 0a 0a  yscall)==22 );..
1149f 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c    /* Register al
114a0 6c 20 56 46 53 65 73 20 64 65 66 69 6e 65 64 20  l VFSes defined 
114a1 69 6e 20 74 68 65 20 61 56 66 73 5b 5d 20 61 72  in the aVfs[] ar
114a2 72 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ray */.  for(i=0
114a3 3b 20 69 3c 28 73 69 7a 65 6f 66 28 61 56 66 73  ; i<(sizeof(aVfs
114a4 29 2f 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33  )/sizeof(sqlite3
114a5 5f 76 66 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20  _vfs)); i++){.  
114a6 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65    sqlite3_vfs_re
114a7 67 69 73 74 65 72 28 26 61 56 66 73 5b 69 5d 2c  gister(&aVfs[i],
114a8 20 69 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65   i==0);.  }.  re
114a9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
114aa 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f  .}../*.** Shutdo
114ab 77 6e 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67  wn the operating
114ac 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63   system interfac
114ad 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70  e..**.** Some op
114ae 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20  erating systems 
114af 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20 64 6f  might need to do
114b0 20 73 6f 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e   some cleanup in
114b1 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a   this routine,.*
114b2 2a 20 74 6f 20 72 65 6c 65 61 73 65 20 64 79 6e  * to release dyn
114b3 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
114b4 65 64 20 6f 62 6a 65 63 74 73 2e 20 20 42 75 74  ed objects.  But
114b5 20 6e 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a   not on unix..**
114b6 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
114b7 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69   a no-op for uni
114b8 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  x..*/.SQLITE_API
114b9 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f   int sqlite3_os_
114ba 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65  end(void){ .  re
114bb 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
114bc 0a 7d 0a 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .}. .#endif /* S
114bd 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f  QLITE_OS_UNIX */
114be 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
114bf 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78  * End of os_unix
114c0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
114c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114c3 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
114c4 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f  * Begin file os_
114c5 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  win.c **********
114c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114c8 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
114c9 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
114ca 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
114cb 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
114cc 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
114cd 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
114ce 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
114cf 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
114d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
114d1 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
114d2 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
114d3 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
114d4 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
114d5 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
114d6 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
114d7 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
114d8 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
114d9 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
114da 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
114db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114df 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
114e0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
114e1 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 70   code that is sp
114e2 65 63 69 66 69 63 20 74 6f 20 57 69 6e 64 6f 77  ecific to Window
114e3 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  s..*/.#if SQLITE
114e4 5f 4f 53 5f 57 49 4e 20 20 20 20 20 20 20 20 20  _OS_WIN         
114e5 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69        /* This fi
114e6 6c 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 57  le is used for W
114e7 69 6e 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a  indows only */..
114e8 23 69 66 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f  #ifdef __CYGWIN_
114e9 5f 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73  _.# include <sys
114ea 2f 63 79 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69  /cygwin.h>.#endi
114eb 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65  f../*.** Include
114ec 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f   code that is co
114ed 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a  mmon to all os_*
114ee 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a  .c files.*/./***
114ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
114f0 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20  ude os_common.h 
114f1 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
114f2 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a   os_win.c ******
114f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
114f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
114f5 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e  n file os_common
114f6 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
114f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
114f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
114f9 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a  * 2004 May 22.**
114fa 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
114fb 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
114fc 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
114fd 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
114fe 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
114ff 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
11500 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
11501 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
11502 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
11503 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
11504 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
11505 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
11506 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
11507 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
11508 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
11509 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1150a 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1150b 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1150c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1150d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1150e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1150f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11510 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
11511 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f  e contains macro
11512 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62  s and a little b
11513 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20  it of code that 
11514 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20  is common to.** 
11515 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66  all of the platf
11516 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c  orm-specific fil
11517 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20  es (os_*.c) and 
11518 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74  is #included int
11519 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73  o those.** files
1151a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ..**.** This fil
1151b 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63  e should be #inc
1151c 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f  luded by the os_
1151d 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20  *.c files only. 
1151e 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20   It is not a.** 
1151f 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20  general purpose 
11520 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a  header file..*/.
11521 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d  #ifndef _OS_COMM
11522 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f  ON_H_.#define _O
11523 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a  S_COMMON_H_../*.
11524 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20  ** At least two 
11525 62 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65  bugs have slippe
11526 64 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20  d in because we 
11527 63 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f  changed the MEMO
11528 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72  RY_DEBUG.** macr
11529 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55  o to SQLITE_DEBU
1152a 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72  G and some older
1152b 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20   makefiles have 
1152c 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65  not yet made the
1152d 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65  .** switch.  The
1152e 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20   following code 
1152f 73 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69  should catch thi
11530 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d  s problem at com
11531 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69  pile-time..*/.#i
11532 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55  fdef MEMORY_DEBU
11533 47 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d  G.# error "The M
11534 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72  EMORY_DEBUG macr
11535 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20  o is obsolete.  
11536 55 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47  Use SQLITE_DEBUG
11537 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69   instead.".#endi
11538 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  f..#if defined(S
11539 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 64  QLITE_TEST) && d
1153a 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
1153b 42 55 47 29 0a 23 20 69 66 6e 64 65 66 20 53 51  BUG).# ifndef SQ
1153c 4c 49 54 45 5f 44 45 42 55 47 5f 4f 53 5f 54 52  LITE_DEBUG_OS_TR
1153d 41 43 45 0a 23 20 20 20 64 65 66 69 6e 65 20 53  ACE.#   define S
1153e 51 4c 49 54 45 5f 44 45 42 55 47 5f 4f 53 5f 54  QLITE_DEBUG_OS_T
1153f 52 41 43 45 20 30 0a 23 20 65 6e 64 69 66 0a 20  RACE 0.# endif. 
11540 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72   int sqlite3OSTr
11541 61 63 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 42  ace = SQLITE_DEB
11542 55 47 5f 4f 53 5f 54 52 41 43 45 3b 0a 23 20 64  UG_OS_TRACE;.# d
11543 65 66 69 6e 65 20 4f 53 54 52 41 43 45 28 58 29  efine OSTRACE(X)
11544 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71            if( sq
11545 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73  lite3OSTrace ) s
11546 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
11547 66 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  f X.#else.# defi
11548 6e 65 20 4f 53 54 52 41 43 45 28 58 29 0a 23 65  ne OSTRACE(X).#e
11549 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72  ndif../*.** Macr
1154a 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e  os for performan
1154b 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72  ce tracing.  Nor
1154c 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66  mally turned off
1154d 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a  .  Only works.**
1154e 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72   on i486 hardwar
1154f 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
11550 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f  ITE_PERFORMANCE_
11551 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77  TRACE../* .** hw
11552 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20  time.h contains 
11553 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72  inline assembler
11554 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d   code for implem
11555 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d  enting .** high-
11556 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69  performance timi
11557 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a  ng routines..*/.
11558 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
11559 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68  Include hwtime.h
1155a 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
1155b 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  f os_common.h **
1155c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1155d 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1155e 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d  Begin file hwtim
1155f 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
11560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11561 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
11562 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32  /*.** 2008 May 2
11563 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
11564 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
11565 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
11566 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
11567 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
11568 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
11569 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
1156a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
1156b 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
1156c 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
1156d 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
1156e 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1156f 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
11570 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
11571 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
11572 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
11573 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
11574 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
11575 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11576 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11577 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11578 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11579 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
1157a 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69   file contains i
1157b 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66  nline asm code f
1157c 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68  or retrieving "h
1157d 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22  igh-performance"
1157e 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72  .** counters for
1157f 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e   x86 class CPUs.
11580 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54  .*/.#ifndef _HWT
11581 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  IME_H_.#define _
11582 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a  HWTIME_H_../*.**
11583 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
11584 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b  outine only work
11585 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61  s on pentium-cla
11586 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72  ss (or newer) pr
11587 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20  ocessors..** It 
11588 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f  uses the RDTSC o
11589 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68  pcode to read th
1158a 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61  e cycle count va
1158b 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a  lue out of the.*
1158c 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20  * processor and 
1158d 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c  returns that val
1158e 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65  ue.  This can be
1158f 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72   used for high-r
11590 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e  es.** profiling.
11591 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64  .*/.#if (defined
11592 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65  (__GNUC__) || de
11593 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29  fined(_MSC_VER))
11594 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66   && \.      (def
11595 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65  ined(i386) || de
11596 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20  fined(__i386__) 
11597 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58  || defined(_M_IX
11598 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69  86))..  #if defi
11599 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20  ned(__GNUC__).. 
1159a 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69   __inline__ sqli
1159b 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
1159c 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
1159d 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
1159e 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f   lo, hi;.     __
1159f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
115a0 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d  __ ("rdtsc" : "=
115a1 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68  a" (lo), "=d" (h
115a2 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e  i));.     return
115a3 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29   (sqlite_uint64)
115a4 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20  hi << 32 | lo;. 
115a5 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69   }..  #elif defi
115a6 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20  ned(_MSC_VER).. 
115a7 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65   __declspec(nake
115a8 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69  d) __inline sqli
115a9 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63  te_uint64 __cdec
115aa 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  l sqlite3Hwtime(
115ab 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73  void){.     __as
115ac 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73  m {.        rdts
115ad 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20  c.        ret   
115ae 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c      ; return val
115af 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20  ue at EDX:EAX.  
115b0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64     }.  }..  #end
115b1 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e  if..#elif (defin
115b2 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20  ed(__GNUC__) && 
115b3 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34  defined(__x86_64
115b4 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65  __))..  __inline
115b5 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  __ sqlite_uint64
115b6 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
115b7 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  oid){.      unsi
115b8 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20  gned long val;. 
115b9 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76       __asm__ __v
115ba 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73  olatile__ ("rdts
115bb 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29  c" : "=A" (val))
115bc 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76  ;.      return v
115bd 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20  al;.  }. .#elif 
115be 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f  (defined(__GNUC_
115bf 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  _) && defined(__
115c0 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c  ppc__))..  __inl
115c1 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
115c2 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
115c3 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75  e(void){.      u
115c4 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
115c5 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20  g retval;.      
115c6 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75  unsigned long ju
115c7 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f  nk;.      __asm_
115c8 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
115c9 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31  "\n\.          1
115ca 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25  :      mftbu   %
115cb 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  1\n\.           
115cc 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25         mftb    %
115cd 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  L0\n\.          
115ce 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20          mftbu   
115cf 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  %0\n\.          
115d0 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20          cmpw    
115d1 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20  %0,%1\n\.       
115d2 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20             bne  
115d3 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20     1b".         
115d4 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20           : "=r" 
115d5 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28  (retval), "=r" (
115d6 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65  junk));.      re
115d7 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d  turn retval;.  }
115d8 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f  ..#else..  #erro
115d9 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74  r Need implement
115da 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  ation of sqlite3
115db 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75  Hwtime() for you
115dc 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f  r platform...  /
115dd 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c  *.  ** To compil
115de 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d  e without implem
115df 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77  enting sqlite3Hw
115e0 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
115e1 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79  platform,.  ** y
115e2 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68  ou can remove th
115e3 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61  e above #error a
115e4 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f  nd use the follo
115e5 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66  wing.  ** stub f
115e6 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69  unction.  You wi
115e7 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73  ll lose timing s
115e8 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a  upport for many.
115e9 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75    ** of the debu
115ea 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e  gging and testin
115eb 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74  g utilities, but
115ec 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20   it should at.  
115ed 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65  ** least compile
115ee 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53   and run..  */.S
115ef 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
115f0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
115f1 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
115f2 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69  ){ return ((sqli
115f3 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a  te_uint64)0); }.
115f4 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20  .#endif..#endif 
115f5 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54  /* !defined(_HWT
115f6 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a  IME_H_) */../***
115f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
115f8 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a  of hwtime.h ****
115f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
115fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
115fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
115fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
115fd 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
115fe 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63  left off in os_c
115ff 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
11600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61  **********/..sta
11601 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  tic sqlite_uint6
11602 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69  4 g_start;.stati
11603 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  c sqlite_uint64 
11604 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69  g_elapsed;.#defi
11605 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20  ne TIMER_START  
11606 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c       g_start=sql
11607 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65  ite3Hwtime().#de
11608 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20  fine TIMER_END  
11609 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64         g_elapsed
1160a 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29  =sqlite3Hwtime()
1160b 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65  -g_start.#define
1160c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20   TIMER_ELAPSED  
1160d 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c     g_elapsed.#el
1160e 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  se.#define TIMER
1160f 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54  _START.#define T
11610 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65  IMER_END.#define
11611 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20   TIMER_ELAPSED  
11612 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74     ((sqlite_uint
11613 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  64)0).#endif../*
11614 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c  .** If we compil
11615 65 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54  e with the SQLIT
11616 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74  E_TEST macro set
11617 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f  , then the follo
11618 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66  wing block.** of
11619 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20   code will give 
1161a 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 74  us the ability t
1161b 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73  o simulate a dis
1161c 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68  k I/O error.  Th
1161d 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f  is.** is used fo
1161e 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f  r testing the I/
1161f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63  O recovery logic
11620 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
11621 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
11622 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
11623 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b  o_error_hit = 0;
11624 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
11625 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49  otal number of I
11626 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c  /O Errors */.SQL
11627 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
11628 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72  te3_io_error_har
11629 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20  dhit = 0;       
1162a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f   /* Number of no
1162b 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20  n-benign errors 
1162c 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1162d 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
1162e 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20  or_pending = 0; 
1162f 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20         /* Count 
11630 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f  down to first I/
11631 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54  O error */.SQLIT
11632 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
11633 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69  3_io_error_persi
11634 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  st = 0;        /
11635 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72  * True if I/O er
11636 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a  rors persist */.
11637 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
11638 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
11639 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20  benign = 0;     
1163a 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65      /* True if e
1163b 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e  rrors are benign
1163c 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
1163d 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  nt sqlite3_diskf
1163e 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b  ull_pending = 0;
1163f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
11640 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
11641 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69   = 0;.#define Si
11642 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
11643 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69  ign(X) sqlite3_i
11644 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28  o_error_benign=(
11645 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  X).#define Simul
11646 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29  ateIOError(CODE)
11647 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74    \.  if( (sqlit
11648 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73  e3_io_error_pers
11649 69 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69  ist && sqlite3_i
1164a 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20  o_error_hit) \. 
1164b 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
1164c 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e  _io_error_pendin
1164d 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20  g-- == 1 )  \.  
1164e 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f              { lo
1164f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44  cal_ioerr(); COD
11650 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  E; }.static void
11651 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a   local_ioerr(){.
11652 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52    IOTRACE(("IOER
11653 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65  R\n"));.  sqlite
11654 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b  3_io_error_hit++
11655 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ;.  if( !sqlite3
11656 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e  _io_error_benign
11657 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72   ) sqlite3_io_er
11658 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d  ror_hardhit++;.}
11659 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
1165a 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43  eDiskfullError(C
1165b 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71  ODE) \.   if( sq
1165c 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70  lite3_diskfull_p
1165d 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20  ending ){ \.    
1165e 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73   if( sqlite3_dis
1165f 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d  kfull_pending ==
11660 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c   1 ){ \.       l
11661 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a  ocal_ioerr(); \.
11662 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64         sqlite3_d
11663 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20  iskfull = 1; \. 
11664 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f        sqlite3_io
11665 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20  _error_hit = 1; 
11666 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c  \.       CODE; \
11667 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20  .     }else{ \. 
11668 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69        sqlite3_di
11669 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d  skfull_pending--
1166a 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20  ; \.     } \.   
1166b 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  }.#else.#define 
1166c 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42  SimulateIOErrorB
1166d 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65  enign(X).#define
1166e 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
1166f 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  (A).#define Simu
11670 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f  lateDiskfullErro
11671 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  r(A).#endif../*.
11672 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c  ** When testing,
11673 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66   keep a count of
11674 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f   the number of o
11675 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69  pen files..*/.#i
11676 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
11677 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
11678 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c  sqlite3_open_fil
11679 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65  e_count = 0;.#de
1167a 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72  fine OpenCounter
1167b 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65  (X)  sqlite3_ope
1167c 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58  n_file_count+=(X
1167d 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  ).#else.#define 
1167e 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23  OpenCounter(X).#
1167f 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a  endif..#endif /*
11680 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f   !defined(_OS_CO
11681 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a  MMON_H_) */../**
11682 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
11683 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20   of os_common.h 
11684 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11685 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11686 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
11687 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
11688 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
11689 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f   left off in os_
1168a 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  win.c **********
1168b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
1168c 0a 2a 2a 20 43 6f 6d 70 69 6c 69 6e 67 20 61 6e  .** Compiling an
1168d 64 20 75 73 69 6e 67 20 57 41 4c 20 6d 6f 64 65  d using WAL mode
1168e 20 72 65 71 75 69 72 65 73 20 73 65 76 65 72 61   requires severa
1168f 6c 20 41 50 49 73 20 74 68 61 74 20 61 72 65 20  l APIs that are 
11690 6f 6e 6c 79 0a 2a 2a 20 61 76 61 69 6c 61 62 6c  only.** availabl
11691 65 20 69 6e 20 57 69 6e 64 6f 77 73 20 70 6c 61  e in Windows pla
11692 74 66 6f 72 6d 73 20 62 61 73 65 64 20 6f 6e 20  tforms based on 
11693 74 68 65 20 4e 54 20 6b 65 72 6e 65 6c 2e 0a 2a  the NT kernel..*
11694 2f 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53  /.#if !SQLITE_OS
11695 5f 57 49 4e 4e 54 20 26 26 20 21 64 65 66 69 6e  _WINNT && !defin
11696 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57  ed(SQLITE_OMIT_W
11697 41 4c 29 0a 23 20 65 72 72 6f 72 20 22 57 41 4c  AL).# error "WAL
11698 20 6d 6f 64 65 20 72 65 71 75 69 72 65 73 20 73   mode requires s
11699 75 70 70 6f 72 74 20 66 72 6f 6d 20 74 68 65 20  upport from the 
1169a 57 69 6e 64 6f 77 73 20 4e 54 20 6b 65 72 6e 65  Windows NT kerne
1169b 6c 2c 20 63 6f 6d 70 69 6c 65 5c 0a 20 77 69 74  l, compile\. wit
1169c 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41  h SQLITE_OMIT_WA
1169d 4c 2e 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  L.".#endif../*.*
1169e 2a 20 41 72 65 20 6d 6f 73 74 20 6f 66 20 74 68  * Are most of th
1169f 65 20 57 69 6e 33 32 20 41 4e 53 49 20 41 50 49  e Win32 ANSI API
116a0 73 20 61 76 61 69 6c 61 62 6c 65 20 28 69 2e 65  s available (i.e
116a1 2e 20 77 69 74 68 20 63 65 72 74 61 69 6e 20 65  . with certain e
116a2 78 63 65 70 74 69 6f 6e 73 0a 2a 2a 20 62 61 73  xceptions.** bas
116a3 65 64 20 6f 6e 20 74 68 65 20 73 75 62 2d 70 6c  ed on the sub-pl
116a4 61 74 66 6f 72 6d 29 3f 0a 2a 2f 0a 23 69 66 20  atform)?.*/.#if 
116a5 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45  !SQLITE_OS_WINCE
116a6 20 26 26 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57   && !SQLITE_OS_W
116a7 49 4e 52 54 0a 23 20 20 64 65 66 69 6e 65 20 53  INRT.#  define S
116a8 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f  QLITE_WIN32_HAS_
116a9 41 4e 53 49 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  ANSI.#endif../*.
116aa 2a 2a 20 41 72 65 20 6d 6f 73 74 20 6f 66 20 74  ** Are most of t
116ab 68 65 20 57 69 6e 33 32 20 55 6e 69 63 6f 64 65  he Win32 Unicode
116ac 20 41 50 49 73 20 61 76 61 69 6c 61 62 6c 65 20   APIs available 
116ad 28 69 2e 65 2e 20 77 69 74 68 20 63 65 72 74 61  (i.e. with certa
116ae 69 6e 20 65 78 63 65 70 74 69 6f 6e 73 0a 2a 2a  in exceptions.**
116af 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 73 75   based on the su
116b0 62 2d 70 6c 61 74 66 6f 72 6d 29 3f 0a 2a 2f 0a  b-platform)?.*/.
116b1 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
116b2 4e 43 45 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 53  NCE || SQLITE_OS
116b3 5f 57 49 4e 4e 54 20 7c 7c 20 53 51 4c 49 54 45  _WINNT || SQLITE
116b4 5f 4f 53 5f 57 49 4e 52 54 0a 23 20 20 64 65 66  _OS_WINRT.#  def
116b5 69 6e 65 20 53 51 4c 49 54 45 5f 57 49 4e 33 32  ine SQLITE_WIN32
116b6 5f 48 41 53 5f 57 49 44 45 0a 23 65 6e 64 69 66  _HAS_WIDE.#endif
116b7 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 77 65 20 6e 65  ../*.** Do we ne
116b8 65 64 20 74 6f 20 6d 61 6e 75 61 6c 6c 79 20 64  ed to manually d
116b9 65 66 69 6e 65 20 74 68 65 20 57 69 6e 33 32 20  efine the Win32 
116ba 66 69 6c 65 20 6d 61 70 70 69 6e 67 20 41 50 49  file mapping API
116bb 73 20 66 6f 72 20 75 73 65 20 77 69 74 68 20 57  s for use with W
116bc 41 4c 0a 2a 2a 20 6d 6f 64 65 20 28 65 2e 67 2e  AL.** mode (e.g.
116bd 20 74 68 65 73 65 20 41 50 49 73 20 61 72 65 20   these APIs are 
116be 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65  available in the
116bf 20 57 69 6e 64 6f 77 73 20 43 45 20 53 44 4b 3b   Windows CE SDK;
116c0 20 68 6f 77 65 76 65 72 2c 20 74 68 65 79 0a 2a   however, they.*
116c1 2a 20 61 72 65 20 6e 6f 74 20 70 72 65 73 65 6e  * are not presen
116c2 74 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20  t in the header 
116c3 66 69 6c 65 29 3f 0a 2a 2f 0a 23 69 66 20 53 51  file)?.*/.#if SQ
116c4 4c 49 54 45 5f 57 49 4e 33 32 5f 46 49 4c 45 4d  LITE_WIN32_FILEM
116c5 41 50 50 49 4e 47 5f 41 50 49 20 26 26 20 21 64  APPING_API && !d
116c6 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
116c7 49 54 5f 57 41 4c 29 0a 2f 2a 0a 2a 2a 20 54 77  IT_WAL)./*.** Tw
116c8 6f 20 6f 66 20 74 68 65 20 66 69 6c 65 20 6d 61  o of the file ma
116c9 70 70 69 6e 67 20 41 50 49 73 20 61 72 65 20 64  pping APIs are d
116ca 69 66 66 65 72 65 6e 74 20 75 6e 64 65 72 20 57  ifferent under W
116cb 69 6e 52 54 2e 20 20 46 69 67 75 72 65 20 6f 75  inRT.  Figure ou
116cc 74 20 77 68 69 63 68 0a 2a 2a 20 73 65 74 20 77  t which.** set w
116cd 65 20 6e 65 65 64 2e 0a 2a 2f 0a 23 69 66 20 53  e need..*/.#if S
116ce 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 57  QLITE_OS_WINRT.W
116cf 49 4e 42 41 53 45 41 50 49 20 48 41 4e 44 4c 45  INBASEAPI HANDLE
116d0 20 57 49 4e 41 50 49 20 43 72 65 61 74 65 46 69   WINAPI CreateFi
116d1 6c 65 4d 61 70 70 69 6e 67 46 72 6f 6d 41 70 70  leMappingFromApp
116d2 28 48 41 4e 44 4c 45 2c 20 5c 0a 20 20 20 20 20  (HANDLE, \.     
116d3 20 20 20 4c 50 53 45 43 55 52 49 54 59 5f 41 54     LPSECURITY_AT
116d4 54 52 49 42 55 54 45 53 2c 20 55 4c 4f 4e 47 2c  TRIBUTES, ULONG,
116d5 20 55 4c 4f 4e 47 36 34 2c 20 4c 50 43 57 53 54   ULONG64, LPCWST
116d6 52 29 3b 0a 0a 57 49 4e 42 41 53 45 41 50 49 20  R);..WINBASEAPI 
116d7 4c 50 56 4f 49 44 20 57 49 4e 41 50 49 20 4d 61  LPVOID WINAPI Ma
116d8 70 56 69 65 77 4f 66 46 69 6c 65 46 72 6f 6d 41  pViewOfFileFromA
116d9 70 70 28 48 41 4e 44 4c 45 2c 20 55 4c 4f 4e 47  pp(HANDLE, ULONG
116da 2c 20 55 4c 4f 4e 47 36 34 2c 20 53 49 5a 45 5f  , ULONG64, SIZE_
116db 54 29 3b 0a 23 65 6c 73 65 0a 23 69 66 20 64 65  T);.#else.#if de
116dc 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e  fined(SQLITE_WIN
116dd 33 32 5f 48 41 53 5f 41 4e 53 49 29 0a 57 49 4e  32_HAS_ANSI).WIN
116de 42 41 53 45 41 50 49 20 48 41 4e 44 4c 45 20 57  BASEAPI HANDLE W
116df 49 4e 41 50 49 20 43 72 65 61 74 65 46 69 6c 65  INAPI CreateFile
116e0 4d 61 70 70 69 6e 67 41 28 48 41 4e 44 4c 45 2c  MappingA(HANDLE,
116e1 20 4c 50 53 45 43 55 52 49 54 59 5f 41 54 54 52   LPSECURITY_ATTR
116e2 49 42 55 54 45 53 2c 20 5c 0a 20 20 20 20 20 20  IBUTES, \.      
116e3 20 20 44 57 4f 52 44 2c 20 44 57 4f 52 44 2c 20    DWORD, DWORD, 
116e4 44 57 4f 52 44 2c 20 4c 50 43 53 54 52 29 3b 0a  DWORD, LPCSTR);.
116e5 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65  #endif /* define
116e6 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48  d(SQLITE_WIN32_H
116e7 41 53 5f 41 4e 53 49 29 20 2a 2f 0a 0a 23 69 66  AS_ANSI) */..#if
116e8 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
116e9 57 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a  WIN32_HAS_WIDE).
116ea 57 49 4e 42 41 53 45 41 50 49 20 48 41 4e 44 4c  WINBASEAPI HANDL
116eb 45 20 57 49 4e 41 50 49 20 43 72 65 61 74 65 46  E WINAPI CreateF
116ec 69 6c 65 4d 61 70 70 69 6e 67 57 28 48 41 4e 44  ileMappingW(HAND
116ed 4c 45 2c 20 4c 50 53 45 43 55 52 49 54 59 5f 41  LE, LPSECURITY_A
116ee 54 54 52 49 42 55 54 45 53 2c 20 5c 0a 20 20 20  TTRIBUTES, \.   
116ef 20 20 20 20 20 44 57 4f 52 44 2c 20 44 57 4f 52       DWORD, DWOR
116f0 44 2c 20 44 57 4f 52 44 2c 20 4c 50 43 57 53 54  D, DWORD, LPCWST
116f1 52 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65  R);.#endif /* de
116f2 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e  fined(SQLITE_WIN
116f3 33 32 5f 48 41 53 5f 57 49 44 45 29 20 2a 2f 0a  32_HAS_WIDE) */.
116f4 0a 57 49 4e 42 41 53 45 41 50 49 20 4c 50 56 4f  .WINBASEAPI LPVO
116f5 49 44 20 57 49 4e 41 50 49 20 4d 61 70 56 69 65  ID WINAPI MapVie
116f6 77 4f 66 46 69 6c 65 28 48 41 4e 44 4c 45 2c 20  wOfFile(HANDLE, 
116f7 44 57 4f 52 44 2c 20 44 57 4f 52 44 2c 20 44 57  DWORD, DWORD, DW
116f8 4f 52 44 2c 20 53 49 5a 45 5f 54 29 3b 0a 23 65  ORD, SIZE_T);.#e
116f9 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
116fa 53 5f 57 49 4e 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a  S_WINRT */../*.*
116fb 2a 20 54 68 69 73 20 66 69 6c 65 20 6d 61 70 70  * This file mapp
116fc 69 6e 67 20 41 50 49 20 69 73 20 63 6f 6d 6d 6f  ing API is commo
116fd 6e 20 74 6f 20 62 6f 74 68 20 57 69 6e 33 32 20  n to both Win32 
116fe 61 6e 64 20 57 69 6e 52 54 2e 0a 2a 2f 0a 57 49  and WinRT..*/.WI
116ff 4e 42 41 53 45 41 50 49 20 42 4f 4f 4c 20 57 49  NBASEAPI BOOL WI
11700 4e 41 50 49 20 55 6e 6d 61 70 56 69 65 77 4f 66  NAPI UnmapViewOf
11701 46 69 6c 65 28 4c 50 43 56 4f 49 44 29 3b 0a 23  File(LPCVOID);.#
11702 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
11703 57 49 4e 33 32 5f 46 49 4c 45 4d 41 50 50 49 4e  WIN32_FILEMAPPIN
11704 47 5f 41 50 49 20 26 26 20 21 64 65 66 69 6e 65  G_API && !define
11705 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41  d(SQLITE_OMIT_WA
11706 4c 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  L) */../*.** Mac
11707 72 6f 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d  ro to find the m
11708 69 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75  inimum of two nu
11709 6d 65 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f  meric values..*/
1170a 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64  .#ifndef MIN.# d
1170b 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28  efine MIN(x,y) (
1170c 28 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 29  (x)<(y)?(x):(y))
1170d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53  .#endif../*.** S
1170e 6f 6d 65 20 4d 69 63 72 6f 73 6f 66 74 20 63 6f  ome Microsoft co
1170f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69  mpilers lack thi
11710 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f  s definition..*/
11711 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44  .#ifndef INVALID
11712 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53  _FILE_ATTRIBUTES
11713 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c 49  .# define INVALI
11714 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45  D_FILE_ATTRIBUTE
11715 53 20 28 28 44 57 4f 52 44 29 2d 31 29 20 0a 23  S ((DWORD)-1) .#
11716 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 46  endif..#ifndef F
11717 49 4c 45 5f 46 4c 41 47 5f 4d 41 53 4b 0a 23 20  ILE_FLAG_MASK.# 
11718 64 65 66 69 6e 65 20 46 49 4c 45 5f 46 4c 41 47  define FILE_FLAG
11719 5f 4d 41 53 4b 20 20 20 20 20 20 20 20 20 20 28  _MASK          (
1171a 30 78 46 46 33 43 30 30 30 30 29 0a 23 65 6e 64  0xFF3C0000).#end
1171b 69 66 0a 0a 23 69 66 6e 64 65 66 20 46 49 4c 45  if..#ifndef FILE
1171c 5f 41 54 54 52 49 42 55 54 45 5f 4d 41 53 4b 0a  _ATTRIBUTE_MASK.
1171d 23 20 64 65 66 69 6e 65 20 46 49 4c 45 5f 41 54  # define FILE_AT
1171e 54 52 49 42 55 54 45 5f 4d 41 53 4b 20 20 20 20  TRIBUTE_MASK    
1171f 20 28 30 78 30 30 30 33 46 46 46 37 29 0a 23 65   (0x0003FFF7).#e
11720 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
11721 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 2f 2a  LITE_OMIT_WAL./*
11722 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e   Forward referen
11723 63 65 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73  ces */.typedef s
11724 74 72 75 63 74 20 77 69 6e 53 68 6d 20 77 69 6e  truct winShm win
11725 53 68 6d 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Shm;           /
11726 2a 20 41 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  * A connection t
11727 6f 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20  o shared-memory 
11728 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
11729 74 20 77 69 6e 53 68 6d 4e 6f 64 65 20 77 69 6e  t winShmNode win
1172a 53 68 6d 4e 6f 64 65 3b 20 20 20 2f 2a 20 41 20  ShmNode;   /* A 
1172b 72 65 67 69 6f 6e 20 6f 66 20 73 68 61 72 65 64  region of shared
1172c 2d 6d 65 6d 6f 72 79 20 2a 2f 0a 23 65 6e 64 69  -memory */.#endi
1172d 66 0a 0a 2f 2a 0a 2a 2a 20 57 69 6e 43 45 20 6c  f../*.** WinCE l
1172e 61 63 6b 73 20 6e 61 74 69 76 65 20 73 75 70 70  acks native supp
1172f 6f 72 74 20 66 6f 72 20 66 69 6c 65 20 6c 6f 63  ort for file loc
11730 6b 69 6e 67 20 73 6f 20 77 65 20 68 61 76 65 20  king so we have 
11731 74 6f 20 66 61 6b 65 20 69 74 0a 2a 2a 20 77 69  to fake it.** wi
11732 74 68 20 73 6f 6d 65 20 63 6f 64 65 20 6f 66 20  th some code of 
11733 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 20  our own..*/.#if 
11734 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a  SQLITE_OS_WINCE.
11735 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 77  typedef struct w
11736 69 6e 63 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e 74  inceLock {.  int
11737 20 6e 52 65 61 64 65 72 73 3b 20 20 20 20 20 20   nReaders;      
11738 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
11739 61 64 65 72 20 6c 6f 63 6b 73 20 6f 62 74 61 69  ader locks obtai
1173a 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 50  ned */.  BOOL bP
1173b 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 2f 2a 20  ending;      /* 
1173c 49 6e 64 69 63 61 74 65 73 20 61 20 70 65 6e 64  Indicates a pend
1173d 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65 65  ing lock has bee
1173e 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20  n obtained */.  
1173f 42 4f 4f 4c 20 62 52 65 73 65 72 76 65 64 3b 20  BOOL bReserved; 
11740 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73      /* Indicates
11741 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b   a reserved lock
11742 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e   has been obtain
11743 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 45 78  ed */.  BOOL bEx
11744 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a 20 49  clusive;    /* I
11745 6e 64 69 63 61 74 65 73 20 61 6e 20 65 78 63 6c  ndicates an excl
11746 75 73 69 76 65 20 6c 6f 63 6b 20 68 61 73 20 62  usive lock has b
11747 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a  een obtained */.
11748 7d 20 77 69 6e 63 65 4c 6f 63 6b 3b 0a 23 65 6e  } winceLock;.#en
11749 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 77  dif../*.** The w
1174a 69 6e 46 69 6c 65 20 73 74 72 75 63 74 75 72 65  inFile structure
1174b 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f   is a subclass o
1174c 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 20  f sqlite3_file* 
1174d 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 20  specific to the 
1174e 77 69 6e 33 32 0a 2a 2a 20 70 6f 72 74 61 62 69  win32.** portabi
1174f 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74  lity layer..*/.t
11750 79 70 65 64 65 66 20 73 74 72 75 63 74 20 77 69  ypedef struct wi
11751 6e 46 69 6c 65 20 77 69 6e 46 69 6c 65 3b 0a 73  nFile winFile;.s
11752 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20 7b 0a  truct winFile {.
11753 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
11754 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74  io_methods *pMet
11755 68 6f 64 3b 20 2f 2a 2a 2a 20 4d 75 73 74 20 62  hod; /*** Must b
11756 65 20 66 69 72 73 74 20 2a 2a 2a 2f 0a 20 20 73  e first ***/.  s
11757 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
11758 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46  ;      /* The VF
11759 53 20 75 73 65 64 20 74 6f 20 6f 70 65 6e 20 74  S used to open t
1175a 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 48 41  his file */.  HA
1175b 4e 44 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20  NDLE h;         
1175c 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20        /* Handle 
1175d 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68  for accessing th
1175e 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6c  e file */.  u8 l
1175f 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 20  ocktype;        
11760 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c      /* Type of l
11761 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 68 65  ock currently he
11762 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20  ld on this file 
11763 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68 61 72 65  */.  short share
11764 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20 2f 2a 20  dLockByte;   /* 
11765 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20  Randomly chosen 
11766 62 79 74 65 20 75 73 65 64 20 61 73 20 61 20 73  byte used as a s
11767 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20  hared lock */.  
11768 75 38 20 63 74 72 6c 46 6c 61 67 73 3b 20 20 20  u8 ctrlFlags;   
11769 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73          /* Flags
1176a 2e 20 20 53 65 65 20 57 49 4e 46 49 4c 45 5f 2a  .  See WINFILE_*
1176b 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 44 57 4f 52   below */.  DWOR
1176c 44 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20 20  D lastErrno;    
1176d 20 20 20 20 2f 2a 20 54 68 65 20 57 69 6e 64 6f      /* The Windo
1176e 77 73 20 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68  ws errno from th
1176f 65 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f 72  e last I/O error
11770 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
11771 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 20 20 77 69  TE_OMIT_WAL.  wi
11772 6e 53 68 6d 20 2a 70 53 68 6d 3b 20 20 20 20 20  nShm *pShm;     
11773 20 20 20 20 20 20 2f 2a 20 49 6e 73 74 61 6e 63        /* Instanc
11774 65 20 6f 66 20 73 68 61 72 65 64 20 6d 65 6d 6f  e of shared memo
11775 72 79 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20  ry on this file 
11776 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 6f 6e 73  */.#endif.  cons
11777 74 20 63 68 61 72 20 2a 7a 50 61 74 68 3b 20 20  t char *zPath;  
11778 20 20 20 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68      /* Full path
11779 6e 61 6d 65 20 6f 66 20 74 68 69 73 20 66 69 6c  name of this fil
1177a 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 43 68 75  e */.  int szChu
1177b 6e 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  nk;            /
1177c 2a 20 43 68 75 6e 6b 20 73 69 7a 65 20 63 6f 6e  * Chunk size con
1177d 66 69 67 75 72 65 64 20 62 79 20 46 43 4e 54 4c  figured by FCNTL
1177e 5f 43 48 55 4e 4b 5f 53 49 5a 45 20 2a 2f 0a 23  _CHUNK_SIZE */.#
1177f 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
11780 43 45 0a 20 20 4c 50 57 53 54 52 20 7a 44 65 6c  CE.  LPWSTR zDel
11781 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20  eteOnClose;  /* 
11782 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Name of file to 
11783 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f 73  delete when clos
11784 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20  ing */.  HANDLE 
11785 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  hMutex;         
11786 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 74   /* Mutex used t
11787 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73  o control access
11788 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 20   to shared lock 
11789 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68 53  */  .  HANDLE hS
1178a 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20 2f  hared;         /
1178b 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  * Shared memory 
1178c 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f 72  segment used for
1178d 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77 69   locking */.  wi
1178e 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20  nceLock local;  
1178f 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f        /* Locks o
11790 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20  btained by this 
11791 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e 46  instance of winF
11792 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f  ile */.  winceLo
11793 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20 20  ck *shared;     
11794 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72 65   /* Global share
11795 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66 6f  d lock memory fo
11796 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a 23  r the file  */.#
11797 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
11798 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66  Allowed values f
11799 6f 72 20 77 69 6e 46 69 6c 65 2e 63 74 72 6c 46  or winFile.ctrlF
1179a 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  lags.*/.#define 
1179b 57 49 4e 46 49 4c 45 5f 50 45 52 53 49 53 54 5f  WINFILE_PERSIST_
1179c 57 41 4c 20 20 20 20 20 30 78 30 34 20 20 20 2f  WAL     0x04   /
1179d 2a 20 50 65 72 73 69 73 74 65 6e 74 20 57 41 4c  * Persistent WAL
1179e 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65   mode */.#define
1179f 20 57 49 4e 46 49 4c 45 5f 50 53 4f 57 20 20 20   WINFILE_PSOW   
117a0 20 20 20 20 20 20 20 20 20 30 78 31 30 20 20 20           0x10   
117a1 2f 2a 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  /* SQLITE_IOCAP_
117a2 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 52  POWERSAFE_OVERWR
117a3 49 54 45 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 54 68  ITE */../*. * Th
117a4 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75  e size of the bu
117a5 66 66 65 72 20 75 73 65 64 20 62 79 20 73 71 6c  ffer used by sql
117a6 69 74 65 33 5f 77 69 6e 33 32 5f 77 72 69 74 65  ite3_win32_write
117a7 5f 64 65 62 75 67 28 29 2e 0a 20 2a 2f 0a 23 69  _debug().. */.#i
117a8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 57 49 4e  fndef SQLITE_WIN
117a9 33 32 5f 44 42 47 5f 42 55 46 5f 53 49 5a 45 0a  32_DBG_BUF_SIZE.
117aa 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  #  define SQLITE
117ab 5f 57 49 4e 33 32 5f 44 42 47 5f 42 55 46 5f 53  _WIN32_DBG_BUF_S
117ac 49 5a 45 20 20 20 28 28 69 6e 74 29 28 34 30 39  IZE   ((int)(409
117ad 36 2d 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 29  6-sizeof(DWORD))
117ae 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20  ).#endif../*. * 
117af 54 68 65 20 76 61 6c 75 65 20 75 73 65 64 20 77  The value used w
117b0 69 74 68 20 73 71 6c 69 74 65 33 5f 77 69 6e 33  ith sqlite3_win3
117b1 32 5f 73 65 74 5f 64 69 72 65 63 74 6f 72 79 28  2_set_directory(
117b2 29 20 74 6f 20 73 70 65 63 69 66 79 20 74 68 61  ) to specify tha
117b3 74 0a 20 2a 20 74 68 65 20 64 61 74 61 20 64 69  t. * the data di
117b4 72 65 63 74 6f 72 79 20 73 68 6f 75 6c 64 20 62  rectory should b
117b5 65 20 63 68 61 6e 67 65 64 2e 0a 20 2a 2f 0a 23  e changed.. */.#
117b6 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 57 49  ifndef SQLITE_WI
117b7 4e 33 32 5f 44 41 54 41 5f 44 49 52 45 43 54 4f  N32_DATA_DIRECTO
117b8 52 59 5f 54 59 50 45 0a 23 20 20 64 65 66 69 6e  RY_TYPE.#  defin
117b9 65 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 44  e SQLITE_WIN32_D
117ba 41 54 41 5f 44 49 52 45 43 54 4f 52 59 5f 54 59  ATA_DIRECTORY_TY
117bb 50 45 20 28 31 29 0a 23 65 6e 64 69 66 0a 0a 2f  PE (1).#endif../
117bc 2a 0a 20 2a 20 54 68 65 20 76 61 6c 75 65 20 75  *. * The value u
117bd 73 65 64 20 77 69 74 68 20 73 71 6c 69 74 65 33  sed with sqlite3
117be 5f 77 69 6e 33 32 5f 73 65 74 5f 64 69 72 65 63  _win32_set_direc
117bf 74 6f 72 79 28 29 20 74 6f 20 73 70 65 63 69 66  tory() to specif
117c0 79 20 74 68 61 74 0a 20 2a 20 74 68 65 20 74 65  y that. * the te
117c1 6d 70 6f 72 61 72 79 20 64 69 72 65 63 74 6f 72  mporary director
117c2 79 20 73 68 6f 75 6c 64 20 62 65 20 63 68 61 6e  y should be chan
117c3 67 65 64 2e 0a 20 2a 2f 0a 23 69 66 6e 64 65 66  ged.. */.#ifndef
117c4 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 54 45   SQLITE_WIN32_TE
117c5 4d 50 5f 44 49 52 45 43 54 4f 52 59 5f 54 59 50  MP_DIRECTORY_TYP
117c6 45 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49  E.#  define SQLI
117c7 54 45 5f 57 49 4e 33 32 5f 54 45 4d 50 5f 44 49  TE_WIN32_TEMP_DI
117c8 52 45 43 54 4f 52 59 5f 54 59 50 45 20 28 32 29  RECTORY_TYPE (2)
117c9 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 49  .#endif../*. * I
117ca 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  f compiled with 
117cb 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 4d 41 4c  SQLITE_WIN32_MAL
117cc 4c 4f 43 20 6f 6e 20 57 69 6e 64 6f 77 73 2c 20  LOC on Windows, 
117cd 77 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 0a  we will use the.
117ce 20 2a 20 76 61 72 69 6f 75 73 20 57 69 6e 33 32   * various Win32
117cf 20 41 50 49 20 68 65 61 70 20 66 75 6e 63 74 69   API heap functi
117d0 6f 6e 73 20 69 6e 73 74 65 61 64 20 6f 66 20 6f  ons instead of o
117d1 75 72 20 6f 77 6e 2e 0a 20 2a 2f 0a 23 69 66 64  ur own.. */.#ifd
117d2 65 66 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f  ef SQLITE_WIN32_
117d3 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 20 2a 20 49 66  MALLOC../*. * If
117d4 20 74 68 69 73 20 69 73 20 6e 6f 6e 2d 7a 65 72   this is non-zer
117d5 6f 2c 20 61 6e 20 69 73 6f 6c 61 74 65 64 20 68  o, an isolated h
117d6 65 61 70 20 77 69 6c 6c 20 62 65 20 63 72 65 61  eap will be crea
117d7 74 65 64 20 62 79 20 74 68 65 20 6e 61 74 69 76  ted by the nativ
117d8 65 20 57 69 6e 33 32 0a 20 2a 20 61 6c 6c 6f 63  e Win32. * alloc
117d9 61 74 6f 72 20 73 75 62 73 79 73 74 65 6d 3b 20  ator subsystem; 
117da 6f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 64  otherwise, the d
117db 65 66 61 75 6c 74 20 70 72 6f 63 65 73 73 20 68  efault process h
117dc 65 61 70 20 77 69 6c 6c 20 62 65 20 75 73 65 64  eap will be used
117dd 2e 20 20 54 68 69 73 0a 20 2a 20 73 65 74 74 69  .  This. * setti
117de 6e 67 20 68 61 73 20 6e 6f 20 65 66 66 65 63 74  ng has no effect
117df 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20   when compiling 
117e0 66 6f 72 20 57 69 6e 52 54 2e 20 20 42 79 20 64  for WinRT.  By d
117e1 65 66 61 75 6c 74 2c 20 74 68 69 73 20 69 73 20  efault, this is 
117e2 65 6e 61 62 6c 65 64 0a 20 2a 20 61 6e 64 20 61  enabled. * and a
117e3 6e 20 69 73 6f 6c 61 74 65 64 20 68 65 61 70 20  n isolated heap 
117e4 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 20  will be created 
117e5 74 6f 20 73 74 6f 72 65 20 61 6c 6c 20 61 6c 6c  to store all all
117e6 6f 63 61 74 65 64 20 64 61 74 61 2e 0a 20 2a 0a  ocated data.. *.
117e7 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
117e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
117e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
117ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
117eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
117ec 20 2a 20 57 41 52 4e 49 4e 47 3a 20 49 74 20 69   * WARNING: It i
117ed 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e  s important to n
117ee 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20 74 68  ote that when th
117ef 69 73 20 73 65 74 74 69 6e 67 20 69 73 20 6e 6f  is setting is no
117f0 6e 2d 7a 65 72 6f 20 61 6e 64 20 74 68 65 0a 20  n-zero and the. 
117f1 2a 20 20 20 20 20 20 20 20 20 20 77 69 6e 4d 65  *          winMe
117f2 6d 53 68 75 74 64 6f 77 6e 20 66 75 6e 63 74 69  mShutdown functi
117f3 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 28 65 2e  on is called (e.
117f4 67 2e 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  g. by the sqlite
117f5 33 5f 73 68 75 74 64 6f 77 6e 0a 20 2a 20 20 20  3_shutdown. *   
117f6 20 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 29         function)
117f7 2c 20 61 6c 6c 20 64 61 74 61 20 74 68 61 74 20  , all data that 
117f8 77 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 75 73  was allocated us
117f9 69 6e 67 20 74 68 65 20 69 73 6f 6c 61 74 65 64  ing the isolated
117fa 20 68 65 61 70 20 77 69 6c 6c 0a 20 2a 20 20 20   heap will. *   
117fb 20 20 20 20 20 20 20 62 65 20 66 72 65 65 64 20         be freed 
117fc 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 6e 64 20  immediately and 
117fd 61 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 61  any attempt to a
117fe 63 63 65 73 73 20 61 6e 79 20 6f 66 20 74 68 61  ccess any of tha
117ff 74 20 66 72 65 65 64 0a 20 2a 20 20 20 20 20 20  t freed. *      
11800 20 20 20 20 64 61 74 61 20 77 69 6c 6c 20 61 6c      data will al
11801 6d 6f 73 74 20 63 65 72 74 61 69 6e 6c 79 20 72  most certainly r
11802 65 73 75 6c 74 20 69 6e 20 61 6e 20 69 6d 6d 65  esult in an imme
11803 64 69 61 74 65 20 61 63 63 65 73 73 20 76 69 6f  diate access vio
11804 6c 61 74 69 6f 6e 2e 0a 20 2a 2a 2a 2a 2a 2a 2a  lation.. *******
11805 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11806 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11807 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11808 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11809 2a 2a 2a 2a 2a 2a 2a 0a 20 2a 2f 0a 23 69 66 6e  *******. */.#ifn
1180a 64 65 66 20 53 51 4c 49 54 45 5f 57 49 4e 33 32  def SQLITE_WIN32
1180b 5f 48 45 41 50 5f 43 52 45 41 54 45 0a 23 20 20  _HEAP_CREATE.#  
1180c 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 49  define SQLITE_WI
1180d 4e 33 32 5f 48 45 41 50 5f 43 52 45 41 54 45 20  N32_HEAP_CREATE 
1180e 20 20 20 28 54 52 55 45 29 0a 23 65 6e 64 69 66     (TRUE).#endif
1180f 0a 0a 2f 2a 0a 20 2a 20 54 68 65 20 69 6e 69 74  ../*. * The init
11810 69 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20  ial size of the 
11811 57 69 6e 33 32 2d 73 70 65 63 69 66 69 63 20 68  Win32-specific h
11812 65 61 70 2e 20 20 54 68 69 73 20 76 61 6c 75 65  eap.  This value
11813 20 6d 61 79 20 62 65 20 7a 65 72 6f 2e 0a 20 2a   may be zero.. *
11814 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
11815 5f 57 49 4e 33 32 5f 48 45 41 50 5f 49 4e 49 54  _WIN32_HEAP_INIT
11816 5f 53 49 5a 45 0a 23 20 20 64 65 66 69 6e 65 20  _SIZE.#  define 
11817 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41  SQLITE_WIN32_HEA
11818 50 5f 49 4e 49 54 5f 53 49 5a 45 20 28 28 53 51  P_INIT_SIZE ((SQ
11819 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  LITE_DEFAULT_CAC
1181a 48 45 5f 53 49 5a 45 29 20 2a 20 5c 0a 20 20 20  HE_SIZE) * \.   
1181b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1181c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1181d 20 20 20 20 28 53 51 4c 49 54 45 5f 44 45 46 41      (SQLITE_DEFA
1181e 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 29 20 2b  ULT_PAGE_SIZE) +
1181f 20 34 31 39 34 33 30 34 29 0a 23 65 6e 64 69 66   4194304).#endif
11820 0a 0a 2f 2a 0a 20 2a 20 54 68 65 20 6d 61 78 69  ../*. * The maxi
11821 6d 75 6d 20 73 69 7a 65 20 6f 66 20 74 68 65 20  mum size of the 
11822 57 69 6e 33 32 2d 73 70 65 63 69 66 69 63 20 68  Win32-specific h
11823 65 61 70 2e 20 20 54 68 69 73 20 76 61 6c 75 65  eap.  This value
11824 20 6d 61 79 20 62 65 20 7a 65 72 6f 2e 0a 20 2a   may be zero.. *
11825 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
11826 5f 57 49 4e 33 32 5f 48 45 41 50 5f 4d 41 58 5f  _WIN32_HEAP_MAX_
11827 53 49 5a 45 0a 23 20 20 64 65 66 69 6e 65 20 53  SIZE.#  define S
11828 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41 50  QLITE_WIN32_HEAP
11829 5f 4d 41 58 5f 53 49 5a 45 20 20 28 30 29 0a 23  _MAX_SIZE  (0).#
1182a 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 54 68 65  endif../*. * The
1182b 20 65 78 74 72 61 20 66 6c 61 67 73 20 74 6f 20   extra flags to 
1182c 75 73 65 20 69 6e 20 63 61 6c 6c 73 20 74 6f 20  use in calls to 
1182d 74 68 65 20 57 69 6e 33 32 20 68 65 61 70 20 41  the Win32 heap A
1182e 50 49 73 2e 20 20 54 68 69 73 20 76 61 6c 75 65  PIs.  This value
1182f 20 6d 61 79 20 62 65 0a 20 2a 20 7a 65 72 6f 20   may be. * zero 
11830 66 6f 72 20 74 68 65 20 64 65 66 61 75 6c 74 20  for the default 
11831 62 65 68 61 76 69 6f 72 2e 0a 20 2a 2f 0a 23 69  behavior.. */.#i
11832 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 57 49 4e  fndef SQLITE_WIN
11833 33 32 5f 48 45 41 50 5f 46 4c 41 47 53 0a 23 20  32_HEAP_FLAGS.# 
11834 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57   define SQLITE_W
11835 49 4e 33 32 5f 48 45 41 50 5f 46 4c 41 47 53 20  IN32_HEAP_FLAGS 
11836 20 20 20 20 28 30 29 0a 23 65 6e 64 69 66 0a 0a      (0).#endif..
11837 2f 2a 0a 2a 2a 20 54 68 65 20 77 69 6e 4d 65 6d  /*.** The winMem
11838 44 61 74 61 20 73 74 72 75 63 74 75 72 65 20 73  Data structure s
11839 74 6f 72 65 73 20 69 6e 66 6f 72 6d 61 74 69 6f  tores informatio
1183a 6e 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68  n required by th
1183b 65 20 57 69 6e 33 32 2d 73 70 65 63 69 66 69 63  e Win32-specific
1183c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  .** sqlite3_mem_
1183d 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65 6d 65 6e  methods implemen
1183e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64  tation..*/.typed
1183f 65 66 20 73 74 72 75 63 74 20 77 69 6e 4d 65 6d  ef struct winMem
11840 44 61 74 61 20 77 69 6e 4d 65 6d 44 61 74 61 3b  Data winMemData;
11841 0a 73 74 72 75 63 74 20 77 69 6e 4d 65 6d 44 61  .struct winMemDa
11842 74 61 20 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45  ta {.#ifndef NDE
11843 42 55 47 0a 20 20 75 33 32 20 6d 61 67 69 63 3b  BUG.  u32 magic;
11844 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d      /* Magic num
11845 62 65 72 20 74 6f 20 64 65 74 65 63 74 20 73 74  ber to detect st
11846 72 75 63 74 75 72 65 20 63 6f 72 72 75 70 74 69  ructure corrupti
11847 6f 6e 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  on. */.#endif.  
11848 48 41 4e 44 4c 45 20 68 48 65 61 70 3b 20 2f 2a  HANDLE hHeap; /*
11849 20 54 68 65 20 68 61 6e 64 6c 65 20 74 6f 20 6f   The handle to o
1184a 75 72 20 68 65 61 70 2e 20 2a 2f 0a 20 20 42 4f  ur heap. */.  BO
1184b 4f 4c 20 62 4f 77 6e 65 64 3b 20 20 2f 2a 20 44  OL bOwned;  /* D
1184c 6f 20 77 65 20 6f 77 6e 20 74 68 65 20 68 65 61  o we own the hea
1184d 70 20 28 69 2e 65 2e 20 64 65 73 74 72 6f 79 20  p (i.e. destroy 
1184e 69 74 20 6f 6e 20 73 68 75 74 64 6f 77 6e 29 3f  it on shutdown)?
1184f 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 6e 64 65 66 20   */.};..#ifndef 
11850 4e 44 45 42 55 47 0a 23 64 65 66 69 6e 65 20 57  NDEBUG.#define W
11851 49 4e 4d 45 4d 5f 4d 41 47 49 43 20 20 20 20 20  INMEM_MAGIC     
11852 30 78 34 32 62 32 38 33 30 62 0a 23 65 6e 64 69  0x42b2830b.#endi
11853 66 0a 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  f..static struct
11854 20 77 69 6e 4d 65 6d 44 61 74 61 20 77 69 6e 5f   winMemData win_
11855 6d 65 6d 5f 64 61 74 61 20 3d 20 7b 0a 23 69 66  mem_data = {.#if
11856 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 57 49  ndef NDEBUG.  WI
11857 4e 4d 45 4d 5f 4d 41 47 49 43 2c 0a 23 65 6e 64  NMEM_MAGIC,.#end
11858 69 66 0a 20 20 4e 55 4c 4c 2c 20 46 41 4c 53 45  if.  NULL, FALSE
11859 0a 7d 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  .};..#ifndef NDE
1185a 42 55 47 0a 23 64 65 66 69 6e 65 20 77 69 6e 4d  BUG.#define winM
1185b 65 6d 41 73 73 65 72 74 4d 61 67 69 63 28 29 20  emAssertMagic() 
1185c 61 73 73 65 72 74 28 20 77 69 6e 5f 6d 65 6d 5f  assert( win_mem_
1185d 64 61 74 61 2e 6d 61 67 69 63 3d 3d 57 49 4e 4d  data.magic==WINM
1185e 45 4d 5f 4d 41 47 49 43 20 29 0a 23 65 6c 73 65  EM_MAGIC ).#else
1185f 0a 23 64 65 66 69 6e 65 20 77 69 6e 4d 65 6d 41  .#define winMemA
11860 73 73 65 72 74 4d 61 67 69 63 28 29 0a 23 65 6e  ssertMagic().#en
11861 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 77 69 6e  dif..#define win
11862 4d 65 6d 47 65 74 48 65 61 70 28 29 20 77 69 6e  MemGetHeap() win
11863 5f 6d 65 6d 5f 64 61 74 61 2e 68 48 65 61 70 0a  _mem_data.hHeap.
11864 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 77 69  .static void *wi
11865 6e 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e  nMemMalloc(int n
11866 42 79 74 65 73 29 3b 0a 73 74 61 74 69 63 20 76  Bytes);.static v
11867 6f 69 64 20 77 69 6e 4d 65 6d 46 72 65 65 28 76  oid winMemFree(v
11868 6f 69 64 20 2a 70 50 72 69 6f 72 29 3b 0a 73 74  oid *pPrior);.st
11869 61 74 69 63 20 76 6f 69 64 20 2a 77 69 6e 4d 65  atic void *winMe
1186a 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70  mRealloc(void *p
1186b 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65  Prior, int nByte
1186c 73 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 77  s);.static int w
1186d 69 6e 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a  inMemSize(void *
1186e 70 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 77  p);.static int w
1186f 69 6e 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74  inMemRoundup(int
11870 20 6e 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20   n);.static int 
11871 77 69 6e 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20  winMemInit(void 
11872 2a 70 41 70 70 44 61 74 61 29 3b 0a 73 74 61 74  *pAppData);.stat
11873 69 63 20 76 6f 69 64 20 77 69 6e 4d 65 6d 53 68  ic void winMemSh
11874 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 70 41 70  utdown(void *pAp
11875 70 44 61 74 61 29 3b 0a 0a 53 51 4c 49 54 45 5f  pData);..SQLITE_
11876 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71  PRIVATE const sq
11877 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
11878 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74  s *sqlite3MemGet
11879 57 69 6e 33 32 28 76 6f 69 64 29 3b 0a 23 65 6e  Win32(void);.#en
1187a 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 57 49  dif /* SQLITE_WI
1187b 4e 33 32 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f  N32_MALLOC */../
1187c 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
1187d 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73 20 28  ng variable is (
1187e 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 6f 6e  normally) set on
1187f 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63 68 61  ce and never cha
11880 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 66 74  nges.** thereaft
11881 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64 73 20  er.  It records 
11882 77 68 65 74 68 65 72 20 74 68 65 20 6f 70 65 72  whether the oper
11883 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20  ating system is 
11884 57 69 6e 39 78 0a 2a 2a 20 6f 72 20 57 69 6e 4e  Win9x.** or WinN
11885 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f 70  T..**.** 0:   Op
11886 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 75  erating system u
11887 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20 20  nknown..** 1:   
11888 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  Operating system
11889 20 69 73 20 57 69 6e 39 78 2e 0a 2a 2a 20 32 3a   is Win9x..** 2:
1188a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73     Operating sys
1188b 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a 2a 2a  tem is WinNT..**
1188c 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f 20  .** In order to 
1188d 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 74 69  facilitate testi
1188e 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73 79  ng on a WinNT sy
1188f 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20 66  stem, the test f
11890 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 6d 61  ixture.** can ma
11891 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69 73 20  nually set this 
11892 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 65 6d  value to 1 to em
11893 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65 68 61  ulate Win98 beha
11894 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  vior..*/.#ifdef 
11895 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
11896 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
11897 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 3b 0a  e3_os_type = 0;.
11898 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e 74  #else.static int
11899 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65   sqlite3_os_type
1189a 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69   = 0;.#endif..#i
1189b 66 6e 64 65 66 20 53 59 53 43 41 4c 4c 0a 23 20  fndef SYSCALL.# 
1189c 20 64 65 66 69 6e 65 20 53 59 53 43 41 4c 4c 20   define SYSCALL 
1189d 73 71 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f  sqlite3_syscall_
1189e 70 74 72 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ptr.#endif../*.*
1189f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
118a0 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  is not available
118a1 20 6f 6e 20 57 69 6e 64 6f 77 73 20 43 45 20 6f   on Windows CE o
118a2 72 20 57 69 6e 52 54 2e 0a 20 2a 2f 0a 0a 23 69  r WinRT.. */..#i
118a3 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
118a4 45 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 53 5f 57  E || SQLITE_OS_W
118a5 49 4e 52 54 0a 23 20 20 64 65 66 69 6e 65 20 6f  INRT.#  define o
118a6 73 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49  sAreFileApisANSI
118a7 28 29 20 20 20 20 20 20 20 31 0a 23 65 6e 64 69  ()       1.#endi
118a8 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 73 79  f../*.** Many sy
118a9 73 74 65 6d 20 63 61 6c 6c 73 20 61 72 65 20 61  stem calls are a
118aa 63 63 65 73 73 65 64 20 74 68 72 6f 75 67 68 20  ccessed through 
118ab 70 6f 69 6e 74 65 72 2d 74 6f 2d 66 75 6e 63 74  pointer-to-funct
118ac 69 6f 6e 73 20 73 6f 20 74 68 61 74 0a 2a 2a 20  ions so that.** 
118ad 74 68 65 79 20 6d 61 79 20 62 65 20 6f 76 65 72  they may be over
118ae 72 69 64 64 65 6e 20 61 74 20 72 75 6e 74 69 6d  ridden at runtim
118af 65 20 74 6f 20 66 61 63 69 6c 69 74 61 74 65 20  e to facilitate 
118b0 66 61 75 6c 74 20 69 6e 6a 65 63 74 69 6f 6e 20  fault injection 
118b1 64 75 72 69 6e 67 0a 2a 2a 20 74 65 73 74 69 6e  during.** testin
118b2 67 20 61 6e 64 20 73 61 6e 64 62 6f 78 69 6e 67  g and sandboxing
118b3 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
118b4 20 61 72 72 61 79 20 68 6f 6c 64 73 20 74 68 65   array holds the
118b5 20 6e 61 6d 65 73 20 61 6e 64 20 70 6f 69 6e 74   names and point
118b6 65 72 73 0a 2a 2a 20 74 6f 20 61 6c 6c 20 6f 76  ers.** to all ov
118b7 65 72 72 69 64 65 61 62 6c 65 20 73 79 73 74 65  errideable syste
118b8 6d 20 63 61 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74  m calls..*/.stat
118b9 69 63 20 73 74 72 75 63 74 20 77 69 6e 5f 73 79  ic struct win_sy
118ba 73 63 61 6c 6c 20 7b 0a 20 20 63 6f 6e 73 74 20  scall {.  const 
118bb 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
118bc 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
118bd 6f 66 20 74 68 65 20 73 79 74 65 6d 20 63 61 6c  of the sytem cal
118be 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  l */.  sqlite3_s
118bf 79 73 63 61 6c 6c 5f 70 74 72 20 70 43 75 72 72  yscall_ptr pCurr
118c0 65 6e 74 3b 20 2f 2a 20 43 75 72 72 65 6e 74 20  ent; /* Current 
118c1 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73 79 73  value of the sys
118c2 74 65 6d 20 63 61 6c 6c 20 2a 2f 0a 20 20 73 71  tem call */.  sq
118c3 6c 69 74 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74  lite3_syscall_pt
118c4 72 20 70 44 65 66 61 75 6c 74 3b 20 2f 2a 20 44  r pDefault; /* D
118c5 65 66 61 75 6c 74 20 76 61 6c 75 65 20 2a 2f 0a  efault value */.
118c6 7d 20 61 53 79 73 63 61 6c 6c 5b 5d 20 3d 20 7b  } aSyscall[] = {
118c7 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f  .#if !SQLITE_OS_
118c8 57 49 4e 43 45 20 26 26 20 21 53 51 4c 49 54 45  WINCE && !SQLITE
118c9 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22 41  _OS_WINRT.  { "A
118ca 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 22 2c  reFileApisANSI",
118cb 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c           (SYSCAL
118cc 4c 29 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53  L)AreFileApisANS
118cd 49 2c 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a  I,         0 },.
118ce 23 65 6c 73 65 0a 20 20 7b 20 22 41 72 65 46 69  #else.  { "AreFi
118cf 6c 65 41 70 69 73 41 4e 53 49 22 2c 20 20 20 20  leApisANSI",    
118d0 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c       (SYSCALL)0,
118d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118d2 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64         0 },.#end
118d3 69 66 0a 0a 23 69 66 6e 64 65 66 20 6f 73 41 72  if..#ifndef osAr
118d4 65 46 69 6c 65 41 70 69 73 41 4e 53 49 0a 23 64  eFileApisANSI.#d
118d5 65 66 69 6e 65 20 6f 73 41 72 65 46 69 6c 65 41  efine osAreFileA
118d6 70 69 73 41 4e 53 49 20 28 28 42 4f 4f 4c 28 57  pisANSI ((BOOL(W
118d7 49 4e 41 50 49 2a 29 28 56 4f 49 44 29 29 61 53  INAPI*)(VOID))aS
118d8 79 73 63 61 6c 6c 5b 30 5d 2e 70 43 75 72 72 65  yscall[0].pCurre
118d9 6e 74 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20  nt).#endif..#if 
118da 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20  SQLITE_OS_WINCE 
118db 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
118dc 45 5f 57 49 4e 33 32 5f 48 41 53 5f 57 49 44 45  E_WIN32_HAS_WIDE
118dd 29 0a 20 20 7b 20 22 43 68 61 72 4c 6f 77 65 72  ).  { "CharLower
118de 57 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  W",             
118df 20 28 53 59 53 43 41 4c 4c 29 43 68 61 72 4c 6f   (SYSCALL)CharLo
118e0 77 65 72 57 2c 20 20 20 20 20 20 20 20 20 20 20  werW,           
118e1 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20     0 },.#else.  
118e2 7b 20 22 43 68 61 72 4c 6f 77 65 72 57 22 2c 20  { "CharLowerW", 
118e3 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59               (SY
118e4 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20  SCALL)0,        
118e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
118e6 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66   },.#endif..#def
118e7 69 6e 65 20 6f 73 43 68 61 72 4c 6f 77 65 72 57  ine osCharLowerW
118e8 20 28 28 4c 50 57 53 54 52 28 57 49 4e 41 50 49   ((LPWSTR(WINAPI
118e9 2a 29 28 4c 50 57 53 54 52 29 29 61 53 79 73 63  *)(LPWSTR))aSysc
118ea 61 6c 6c 5b 31 5d 2e 70 43 75 72 72 65 6e 74 29  all[1].pCurrent)
118eb 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ..#if SQLITE_OS_
118ec 57 49 4e 43 45 20 26 26 20 64 65 66 69 6e 65 64  WINCE && defined
118ed 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41  (SQLITE_WIN32_HA
118ee 53 5f 57 49 44 45 29 0a 20 20 7b 20 22 43 68 61  S_WIDE).  { "Cha
118ef 72 55 70 70 65 72 57 22 2c 20 20 20 20 20 20 20  rUpperW",       
118f0 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
118f1 43 68 61 72 55 70 70 65 72 57 2c 20 20 20 20 20  CharUpperW,     
118f2 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65           0 },.#e
118f3 6c 73 65 0a 20 20 7b 20 22 43 68 61 72 55 70 70  lse.  { "CharUpp
118f4 65 72 57 22 2c 20 20 20 20 20 20 20 20 20 20 20  erW",           
118f5 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20     (SYSCALL)0,  
118f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118f7 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66       0 },.#endif
118f8 0a 0a 23 64 65 66 69 6e 65 20 6f 73 43 68 61 72  ..#define osChar
118f9 55 70 70 65 72 57 20 28 28 4c 50 57 53 54 52 28  UpperW ((LPWSTR(
118fa 57 49 4e 41 50 49 2a 29 28 4c 50 57 53 54 52 29  WINAPI*)(LPWSTR)
118fb 29 61 53 79 73 63 61 6c 6c 5b 32 5d 2e 70 43 75  )aSyscall[2].pCu
118fc 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 43 6c 6f  rrent)..  { "Clo
118fd 73 65 48 61 6e 64 6c 65 22 2c 20 20 20 20 20 20  seHandle",      
118fe 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
118ff 43 6c 6f 73 65 48 61 6e 64 6c 65 2c 20 20 20 20  CloseHandle,    
11900 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 0a 23           0 },..#
11901 64 65 66 69 6e 65 20 6f 73 43 6c 6f 73 65 48 61  define osCloseHa
11902 6e 64 6c 65 20 28 28 42 4f 4f 4c 28 57 49 4e 41  ndle ((BOOL(WINA
11903 50 49 2a 29 28 48 41 4e 44 4c 45 29 29 61 53 79  PI*)(HANDLE))aSy
11904 73 63 61 6c 6c 5b 33 5d 2e 70 43 75 72 72 65 6e  scall[3].pCurren
11905 74 29 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  t)..#if defined(
11906 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41 53  SQLITE_WIN32_HAS
11907 5f 41 4e 53 49 29 0a 20 20 7b 20 22 43 72 65 61  _ANSI).  { "Crea
11908 74 65 46 69 6c 65 41 22 2c 20 20 20 20 20 20 20  teFileA",       
11909 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 43        (SYSCALL)C
1190a 72 65 61 74 65 46 69 6c 65 41 2c 20 20 20 20 20  reateFileA,     
1190b 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6c          0 },.#el
1190c 73 65 0a 20 20 7b 20 22 43 72 65 61 74 65 46 69  se.  { "CreateFi
1190d 6c 65 41 22 2c 20 20 20 20 20 20 20 20 20 20 20  leA",           
1190e 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20    (SYSCALL)0,   
1190f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11910 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a      0 },.#endif.
11911 0a 23 64 65 66 69 6e 65 20 6f 73 43 72 65 61 74  .#define osCreat
11912 65 46 69 6c 65 41 20 28 28 48 41 4e 44 4c 45 28  eFileA ((HANDLE(
11913 57 49 4e 41 50 49 2a 29 28 4c 50 43 53 54 52 2c  WINAPI*)(LPCSTR,
11914 44 57 4f 52 44 2c 44 57 4f 52 44 2c 20 5c 0a 20  DWORD,DWORD, \. 
11915 20 20 20 20 20 20 20 4c 50 53 45 43 55 52 49 54         LPSECURIT
11916 59 5f 41 54 54 52 49 42 55 54 45 53 2c 44 57 4f  Y_ATTRIBUTES,DWO
11917 52 44 2c 44 57 4f 52 44 2c 48 41 4e 44 4c 45 29  RD,DWORD,HANDLE)
11918 29 61 53 79 73 63 61 6c 6c 5b 34 5d 2e 70 43 75  )aSyscall[4].pCu
11919 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c  rrent)..#if !SQL
1191a 49 54 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26 20  ITE_OS_WINRT && 
1191b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57  defined(SQLITE_W
1191c 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a 20  IN32_HAS_WIDE). 
1191d 20 7b 20 22 43 72 65 61 74 65 46 69 6c 65 57 22   { "CreateFileW"
1191e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53  ,             (S
1191f 59 53 43 41 4c 4c 29 43 72 65 61 74 65 46 69 6c  YSCALL)CreateFil
11920 65 57 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  eW,             
11921 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22  0 },.#else.  { "
11922 43 72 65 61 74 65 46 69 6c 65 57 22 2c 20 20 20  CreateFileW",   
11923 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41            (SYSCA
11924 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20  LL)0,           
11925 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c              0 },
11926 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65  .#endif..#define
11927 20 6f 73 43 72 65 61 74 65 46 69 6c 65 57 20 28   osCreateFileW (
11928 28 48 41 4e 44 4c 45 28 57 49 4e 41 50 49 2a 29  (HANDLE(WINAPI*)
11929 28 4c 50 43 57 53 54 52 2c 44 57 4f 52 44 2c 44  (LPCWSTR,DWORD,D
1192a 57 4f 52 44 2c 20 5c 0a 20 20 20 20 20 20 20 20  WORD, \.        
1192b 4c 50 53 45 43 55 52 49 54 59 5f 41 54 54 52 49  LPSECURITY_ATTRI
1192c 42 55 54 45 53 2c 44 57 4f 52 44 2c 44 57 4f 52  BUTES,DWORD,DWOR
1192d 44 2c 48 41 4e 44 4c 45 29 29 61 53 79 73 63 61  D,HANDLE))aSysca
1192e 6c 6c 5b 35 5d 2e 70 43 75 72 72 65 6e 74 29 0a  ll[5].pCurrent).
1192f 0a 23 69 66 20 28 21 53 51 4c 49 54 45 5f 4f 53  .#if (!SQLITE_OS
11930 5f 57 49 4e 52 54 20 26 26 20 64 65 66 69 6e 65  _WINRT && define
11931 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48  d(SQLITE_WIN32_H
11932 41 53 5f 41 4e 53 49 29 20 26 26 20 5c 0a 20 20  AS_ANSI) && \.  
11933 20 20 20 20 20 20 21 64 65 66 69 6e 65 64 28 53        !defined(S
11934 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 29 29  QLITE_OMIT_WAL))
11935 0a 20 20 7b 20 22 43 72 65 61 74 65 46 69 6c 65  .  { "CreateFile
11936 4d 61 70 70 69 6e 67 41 22 2c 20 20 20 20 20 20  MappingA",      
11937 28 53 59 53 43 41 4c 4c 29 43 72 65 61 74 65 46  (SYSCALL)CreateF
11938 69 6c 65 4d 61 70 70 69 6e 67 41 2c 20 20 20 20  ileMappingA,    
11939 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b    0 },.#else.  {
1193a 20 22 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70   "CreateFileMapp
1193b 69 6e 67 41 22 2c 20 20 20 20 20 20 28 53 59 53  ingA",      (SYS
1193c 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20  CALL)0,         
1193d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
1193e 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69  },.#endif..#defi
1193f 6e 65 20 6f 73 43 72 65 61 74 65 46 69 6c 65 4d  ne osCreateFileM
11940 61 70 70 69 6e 67 41 20 28 28 48 41 4e 44 4c 45  appingA ((HANDLE
11941 28 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45  (WINAPI*)(HANDLE
11942 2c 4c 50 53 45 43 55 52 49 54 59 5f 41 54 54 52  ,LPSECURITY_ATTR
11943 49 42 55 54 45 53 2c 20 5c 0a 20 20 20 20 20 20  IBUTES, \.      
11944 20 20 44 57 4f 52 44 2c 44 57 4f 52 44 2c 44 57    DWORD,DWORD,DW
11945 4f 52 44 2c 4c 50 43 53 54 52 29 29 61 53 79 73  ORD,LPCSTR))aSys
11946 63 61 6c 6c 5b 36 5d 2e 70 43 75 72 72 65 6e 74  call[6].pCurrent
11947 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  )..#if SQLITE_OS
11948 5f 57 49 4e 43 45 20 7c 7c 20 28 21 53 51 4c 49  _WINCE || (!SQLI
11949 54 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26 20 64  TE_OS_WINRT && d
1194a 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49  efined(SQLITE_WI
1194b 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 20 26 26  N32_HAS_WIDE) &&
1194c 20 5c 0a 20 20 20 20 20 20 20 20 21 64 65 66 69   \.        !defi
1194d 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
1194e 57 41 4c 29 29 0a 20 20 7b 20 22 43 72 65 61 74  WAL)).  { "Creat
1194f 65 46 69 6c 65 4d 61 70 70 69 6e 67 57 22 2c 20  eFileMappingW", 
11950 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 43 72       (SYSCALL)Cr
11951 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 57  eateFileMappingW
11952 2c 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73  ,      0 },.#els
11953 65 0a 20 20 7b 20 22 43 72 65 61 74 65 46 69 6c  e.  { "CreateFil
11954 65 4d 61 70 70 69 6e 67 57 22 2c 20 20 20 20 20  eMappingW",     
11955 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20   (SYSCALL)0,    
11956 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11957 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a     0 },.#endif..
11958 23 64 65 66 69 6e 65 20 6f 73 43 72 65 61 74 65  #define osCreate
11959 46 69 6c 65 4d 61 70 70 69 6e 67 57 20 28 28 48  FileMappingW ((H
1195a 41 4e 44 4c 45 28 57 49 4e 41 50 49 2a 29 28 48  ANDLE(WINAPI*)(H
1195b 41 4e 44 4c 45 2c 4c 50 53 45 43 55 52 49 54 59  ANDLE,LPSECURITY
1195c 5f 41 54 54 52 49 42 55 54 45 53 2c 20 5c 0a 20  _ATTRIBUTES, \. 
1195d 20 20 20 20 20 20 20 44 57 4f 52 44 2c 44 57 4f         DWORD,DWO
1195e 52 44 2c 44 57 4f 52 44 2c 4c 50 43 57 53 54 52  RD,DWORD,LPCWSTR
1195f 29 29 61 53 79 73 63 61 6c 6c 5b 37 5d 2e 70 43  ))aSyscall[7].pC
11960 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51  urrent)..#if !SQ
11961 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26  LITE_OS_WINRT &&
11962 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
11963 57 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a  WIN32_HAS_WIDE).
11964 20 20 7b 20 22 43 72 65 61 74 65 4d 75 74 65 78    { "CreateMutex
11965 57 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 28  W",            (
11966 53 59 53 43 41 4c 4c 29 43 72 65 61 74 65 4d 75  SYSCALL)CreateMu
11967 74 65 78 57 2c 20 20 20 20 20 20 20 20 20 20 20  texW,           
11968 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20   0 },.#else.  { 
11969 22 43 72 65 61 74 65 4d 75 74 65 78 57 22 2c 20  "CreateMutexW", 
1196a 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43             (SYSC
1196b 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20  ALL)0,          
1196c 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d               0 }
1196d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e  ,.#endif..#defin
1196e 65 20 6f 73 43 72 65 61 74 65 4d 75 74 65 78 57  e osCreateMutexW
1196f 20 28 28 48 41 4e 44 4c 45 28 57 49 4e 41 50 49   ((HANDLE(WINAPI
11970 2a 29 28 4c 50 53 45 43 55 52 49 54 59 5f 41 54  *)(LPSECURITY_AT
11971 54 52 49 42 55 54 45 53 2c 42 4f 4f 4c 2c 20 5c  TRIBUTES,BOOL, \
11972 0a 20 20 20 20 20 20 20 20 4c 50 43 57 53 54 52  .        LPCWSTR
11973 29 29 61 53 79 73 63 61 6c 6c 5b 38 5d 2e 70 43  ))aSyscall[8].pC
11974 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 64 65 66  urrent)..#if def
11975 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33  ined(SQLITE_WIN3
11976 32 5f 48 41 53 5f 41 4e 53 49 29 0a 20 20 7b 20  2_HAS_ANSI).  { 
11977 22 44 65 6c 65 74 65 46 69 6c 65 41 22 2c 20 20  "DeleteFileA",  
11978 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43             (SYSC
11979 41 4c 4c 29 44 65 6c 65 74 65 46 69 6c 65 41 2c  ALL)DeleteFileA,
1197a 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d               0 }
1197b 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 44 65 6c  ,.#else.  { "Del
1197c 65 74 65 46 69 6c 65 41 22 2c 20 20 20 20 20 20  eteFileA",      
1197d 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
1197e 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
1197f 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65           0 },.#e
11980 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73  ndif..#define os
11981 44 65 6c 65 74 65 46 69 6c 65 41 20 28 28 42 4f  DeleteFileA ((BO
11982 4f 4c 28 57 49 4e 41 50 49 2a 29 28 4c 50 43 53  OL(WINAPI*)(LPCS
11983 54 52 29 29 61 53 79 73 63 61 6c 6c 5b 39 5d 2e  TR))aSyscall[9].
11984 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 64  pCurrent)..#if d
11985 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49  efined(SQLITE_WI
11986 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a 20 20  N32_HAS_WIDE).  
11987 7b 20 22 44 65 6c 65 74 65 46 69 6c 65 57 22 2c  { "DeleteFileW",
11988 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59               (SY
11989 53 43 41 4c 4c 29 44 65 6c 65 74 65 46 69 6c 65  SCALL)DeleteFile
1198a 57 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 30  W,             0
1198b 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 44   },.#else.  { "D
1198c 65 6c 65 74 65 46 69 6c 65 57 22 2c 20 20 20 20  eleteFileW",    
1198d 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c           (SYSCAL
1198e 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  L)0,            
1198f 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a             0 },.
11990 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20  #endif..#define 
11991 6f 73 44 65 6c 65 74 65 46 69 6c 65 57 20 28 28  osDeleteFileW ((
11992 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 4c 50  BOOL(WINAPI*)(LP
11993 43 57 53 54 52 29 29 61 53 79 73 63 61 6c 6c 5b  CWSTR))aSyscall[
11994 31 30 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23  10].pCurrent)..#
11995 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
11996 43 45 0a 20 20 7b 20 22 46 69 6c 65 54 69 6d 65  CE.  { "FileTime
11997 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65 22  ToLocalFileTime"
11998 2c 20 28 53 59 53 43 41 4c 4c 29 46 69 6c 65 54  , (SYSCALL)FileT
11999 69 6d 65 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69  imeToLocalFileTi
1199a 6d 65 2c 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20  me, 0 },.#else. 
1199b 20 7b 20 22 46 69 6c 65 54 69 6d 65 54 6f 4c 6f   { "FileTimeToLo
1199c 63 61 6c 46 69 6c 65 54 69 6d 65 22 2c 20 28 53  calFileTime", (S
1199d 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20  YSCALL)0,       
1199e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1199f 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65  0 },.#endif..#de
119a0 66 69 6e 65 20 6f 73 46 69 6c 65 54 69 6d 65 54  fine osFileTimeT
119a1 6f 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65 20 28  oLocalFileTime (
119a2 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 43  (BOOL(WINAPI*)(C
119a3 4f 4e 53 54 20 46 49 4c 45 54 49 4d 45 2a 2c 20  ONST FILETIME*, 
119a4 5c 0a 20 20 20 20 20 20 20 20 4c 50 46 49 4c 45  \.        LPFILE
119a5 54 49 4d 45 29 29 61 53 79 73 63 61 6c 6c 5b 31  TIME))aSyscall[1
119a6 31 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69  1].pCurrent)..#i
119a7 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
119a8 45 0a 20 20 7b 20 22 46 69 6c 65 54 69 6d 65 54  E.  { "FileTimeT
119a9 6f 53 79 73 74 65 6d 54 69 6d 65 22 2c 20 20 20  oSystemTime",   
119aa 20 28 53 59 53 43 41 4c 4c 29 46 69 6c 65 54 69   (SYSCALL)FileTi
119ab 6d 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 2c 20  meToSystemTime, 
119ac 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20     0 },.#else.  
119ad 7b 20 22 46 69 6c 65 54 69 6d 65 54 6f 53 79 73  { "FileTimeToSys
119ae 74 65 6d 54 69 6d 65 22 2c 20 20 20 20 28 53 59  temTime",    (SY
119af 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20  SCALL)0,        
119b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
119b1 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66   },.#endif..#def
119b2 69 6e 65 20 6f 73 46 69 6c 65 54 69 6d 65 54 6f  ine osFileTimeTo
119b3 53 79 73 74 65 6d 54 69 6d 65 20 28 28 42 4f 4f  SystemTime ((BOO
119b4 4c 28 57 49 4e 41 50 49 2a 29 28 43 4f 4e 53 54  L(WINAPI*)(CONST
119b5 20 46 49 4c 45 54 49 4d 45 2a 2c 20 5c 0a 20 20   FILETIME*, \.  
119b6 20 20 20 20 20 20 4c 50 53 59 53 54 45 4d 54 49        LPSYSTEMTI
119b7 4d 45 29 29 61 53 79 73 63 61 6c 6c 5b 31 32 5d  ME))aSyscall[12]
119b8 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20  .pCurrent)..  { 
119b9 22 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72  "FlushFileBuffer
119ba 73 22 2c 20 20 20 20 20 20 20 20 28 53 59 53 43  s",        (SYSC
119bb 41 4c 4c 29 46 6c 75 73 68 46 69 6c 65 42 75 66  ALL)FlushFileBuf
119bc 66 65 72 73 2c 20 20 20 20 20 20 20 20 30 20 7d  fers,        0 }
119bd 2c 0a 0a 23 64 65 66 69 6e 65 20 6f 73 46 6c 75  ,..#define osFlu
119be 73 68 46 69 6c 65 42 75 66 66 65 72 73 20 28 28  shFileBuffers ((
119bf 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41  BOOL(WINAPI*)(HA
119c0 4e 44 4c 45 29 29 61 53 79 73 63 61 6c 6c 5b 31  NDLE))aSyscall[1
119c1 33 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69  3].pCurrent)..#i
119c2 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
119c3 5f 57 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 29  _WIN32_HAS_ANSI)
119c4 0a 20 20 7b 20 22 46 6f 72 6d 61 74 4d 65 73 73  .  { "FormatMess
119c5 61 67 65 41 22 2c 20 20 20 20 20 20 20 20 20 20  ageA",          
119c6 28 53 59 53 43 41 4c 4c 29 46 6f 72 6d 61 74 4d  (SYSCALL)FormatM
119c7 65 73 73 61 67 65 41 2c 20 20 20 20 20 20 20 20  essageA,        
119c8 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b    0 },.#else.  {
119c9 20 22 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 41   "FormatMessageA
119ca 22 2c 20 20 20 20 20 20 20 20 20 20 28 53 59 53  ",          (SYS
119cb 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20  CALL)0,         
119cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
119cd 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69  },.#endif..#defi
119ce 6e 65 20 6f 73 46 6f 72 6d 61 74 4d 65 73 73 61  ne osFormatMessa
119cf 67 65 41 20 28 28 44 57 4f 52 44 28 57 49 4e 41  geA ((DWORD(WINA
119d0 50 49 2a 29 28 44 57 4f 52 44 2c 4c 50 43 56 4f  PI*)(DWORD,LPCVO
119d1 49 44 2c 44 57 4f 52 44 2c 44 57 4f 52 44 2c 4c  ID,DWORD,DWORD,L
119d2 50 53 54 52 2c 20 5c 0a 20 20 20 20 20 20 20 20  PSTR, \.        
119d3 44 57 4f 52 44 2c 76 61 5f 6c 69 73 74 2a 29 29  DWORD,va_list*))
119d4 61 53 79 73 63 61 6c 6c 5b 31 34 5d 2e 70 43 75  aSyscall[14].pCu
119d5 72 72 65 6e 74 29 0a 0a 23 69 66 20 64 65 66 69  rrent)..#if defi
119d6 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32  ned(SQLITE_WIN32
119d7 5f 48 41 53 5f 57 49 44 45 29 0a 20 20 7b 20 22  _HAS_WIDE).  { "
119d8 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 57 22 2c  FormatMessageW",
119d9 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41            (SYSCA
119da 4c 4c 29 46 6f 72 6d 61 74 4d 65 73 73 61 67 65  LL)FormatMessage
119db 57 2c 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c  W,          0 },
119dc 0a 23 65 6c 73 65 0a 20 20 7b 20 22 46 6f 72 6d  .#else.  { "Form
119dd 61 74 4d 65 73 73 61 67 65 57 22 2c 20 20 20 20  atMessageW",    
119de 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30        (SYSCALL)0
119df 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
119e0 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e          0 },.#en
119e1 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 46  dif..#define osF
119e2 6f 72 6d 61 74 4d 65 73 73 61 67 65 57 20 28 28  ormatMessageW ((
119e3 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 28 44  DWORD(WINAPI*)(D
119e4 57 4f 52 44 2c 4c 50 43 56 4f 49 44 2c 44 57 4f  WORD,LPCVOID,DWO
119e5 52 44 2c 44 57 4f 52 44 2c 4c 50 57 53 54 52 2c  RD,DWORD,LPWSTR,
119e6 20 5c 0a 20 20 20 20 20 20 20 20 44 57 4f 52 44   \.        DWORD
119e7 2c 76 61 5f 6c 69 73 74 2a 29 29 61 53 79 73 63  ,va_list*))aSysc
119e8 61 6c 6c 5b 31 35 5d 2e 70 43 75 72 72 65 6e 74  all[15].pCurrent
119e9 29 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  )..#if !defined(
119ea 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
119eb 5f 45 58 54 45 4e 53 49 4f 4e 29 0a 20 20 7b 20  _EXTENSION).  { 
119ec 22 46 72 65 65 4c 69 62 72 61 72 79 22 2c 20 20  "FreeLibrary",  
119ed 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43             (SYSC
119ee 41 4c 4c 29 46 72 65 65 4c 69 62 72 61 72 79 2c  ALL)FreeLibrary,
119ef 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d               0 }
119f0 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 46 72 65  ,.#else.  { "Fre
119f1 65 4c 69 62 72 61 72 79 22 2c 20 20 20 20 20 20  eLibrary",      
119f2 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
119f3 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
119f4 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65           0 },.#e
119f5 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73  ndif..#define os
119f6 46 72 65 65 4c 69 62 72 61 72 79 20 28 28 42 4f  FreeLibrary ((BO
119f7 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 4d 4f 44  OL(WINAPI*)(HMOD
119f8 55 4c 45 29 29 61 53 79 73 63 61 6c 6c 5b 31 36  ULE))aSyscall[16
119f9 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b  ].pCurrent)..  {
119fa 20 22 47 65 74 43 75 72 72 65 6e 74 50 72 6f 63   "GetCurrentProc
119fb 65 73 73 49 64 22 2c 20 20 20 20 20 28 53 59 53  essId",     (SYS
119fc 43 41 4c 4c 29 47 65 74 43 75 72 72 65 6e 74 50  CALL)GetCurrentP
119fd 72 6f 63 65 73 73 49 64 2c 20 20 20 20 20 30 20  rocessId,     0 
119fe 7d 2c 0a 0a 23 64 65 66 69 6e 65 20 6f 73 47 65  },..#define osGe
119ff 74 43 75 72 72 65 6e 74 50 72 6f 63 65 73 73 49  tCurrentProcessI
11a00 64 20 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49  d ((DWORD(WINAPI
11a01 2a 29 28 56 4f 49 44 29 29 61 53 79 73 63 61 6c  *)(VOID))aSyscal
11a02 6c 5b 31 37 5d 2e 70 43 75 72 72 65 6e 74 29 0a  l[17].pCurrent).
11a03 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f  .#if !SQLITE_OS_
11a04 57 49 4e 43 45 20 26 26 20 64 65 66 69 6e 65 64  WINCE && defined
11a05 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41  (SQLITE_WIN32_HA
11a06 53 5f 41 4e 53 49 29 0a 20 20 7b 20 22 47 65 74  S_ANSI).  { "Get
11a07 44 69 73 6b 46 72 65 65 53 70 61 63 65 41 22 2c  DiskFreeSpaceA",
11a08 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
11a09 47 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 65  GetDiskFreeSpace
11a0a 41 2c 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65  A,       0 },.#e
11a0b 6c 73 65 0a 20 20 7b 20 22 47 65 74 44 69 73 6b  lse.  { "GetDisk
11a0c 46 72 65 65 53 70 61 63 65 41 22 2c 20 20 20 20  FreeSpaceA",    
11a0d 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20     (SYSCALL)0,  
11a0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a0f 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66       0 },.#endif
11a10 0a 0a 23 64 65 66 69 6e 65 20 6f 73 47 65 74 44  ..#define osGetD
11a11 69 73 6b 46 72 65 65 53 70 61 63 65 41 20 28 28  iskFreeSpaceA ((
11a12 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 4c 50  BOOL(WINAPI*)(LP
11a13 43 53 54 52 2c 4c 50 44 57 4f 52 44 2c 4c 50 44  CSTR,LPDWORD,LPD
11a14 57 4f 52 44 2c 4c 50 44 57 4f 52 44 2c 20 5c 0a  WORD,LPDWORD, \.
11a15 20 20 20 20 20 20 20 20 4c 50 44 57 4f 52 44 29          LPDWORD)
11a16 29 61 53 79 73 63 61 6c 6c 5b 31 38 5d 2e 70 43  )aSyscall[18].pC
11a17 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51  urrent)..#if !SQ
11a18 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26  LITE_OS_WINCE &&
11a19 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52   !SQLITE_OS_WINR
11a1a 54 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  T && defined(SQL
11a1b 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 57 49  ITE_WIN32_HAS_WI
11a1c 44 45 29 0a 20 20 7b 20 22 47 65 74 44 69 73 6b  DE).  { "GetDisk
11a1d 46 72 65 65 53 70 61 63 65 57 22 2c 20 20 20 20  FreeSpaceW",    
11a1e 20 20 20 28 53 59 53 43 41 4c 4c 29 47 65 74 44     (SYSCALL)GetD
11a1f 69 73 6b 46 72 65 65 53 70 61 63 65 57 2c 20 20  iskFreeSpaceW,  
11a20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a       0 },.#else.
11a21 20 20 7b 20 22 47 65 74 44 69 73 6b 46 72 65 65    { "GetDiskFree
11a22 53 70 61 63 65 57 22 2c 20 20 20 20 20 20 20 28  SpaceW",       (
11a23 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20  SYSCALL)0,      
11a24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a25 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64   0 },.#endif..#d
11a26 65 66 69 6e 65 20 6f 73 47 65 74 44 69 73 6b 46  efine osGetDiskF
11a27 72 65 65 53 70 61 63 65 57 20 28 28 42 4f 4f 4c  reeSpaceW ((BOOL
11a28 28 57 49 4e 41 50 49 2a 29 28 4c 50 43 57 53 54  (WINAPI*)(LPCWST
11a29 52 2c 4c 50 44 57 4f 52 44 2c 4c 50 44 57 4f 52  R,LPDWORD,LPDWOR
11a2a 44 2c 4c 50 44 57 4f 52 44 2c 20 5c 0a 20 20 20  D,LPDWORD, \.   
11a2b 20 20 20 20 20 4c 50 44 57 4f 52 44 29 29 61 53       LPDWORD))aS
11a2c 79 73 63 61 6c 6c 5b 31 39 5d 2e 70 43 75 72 72  yscall[19].pCurr
11a2d 65 6e 74 29 0a 0a 23 69 66 20 64 65 66 69 6e 65  ent)..#if define
11a2e 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48  d(SQLITE_WIN32_H
11a2f 41 53 5f 41 4e 53 49 29 0a 20 20 7b 20 22 47 65  AS_ANSI).  { "Ge
11a30 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 41  tFileAttributesA
11a31 22 2c 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c  ",      (SYSCALL
11a32 29 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74  )GetFileAttribut
11a33 65 73 41 2c 20 20 20 20 20 20 30 20 7d 2c 0a 23  esA,      0 },.#
11a34 65 6c 73 65 0a 20 20 7b 20 22 47 65 74 46 69 6c  else.  { "GetFil
11a35 65 41 74 74 72 69 62 75 74 65 73 41 22 2c 20 20  eAttributesA",  
11a36 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20      (SYSCALL)0, 
11a37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a38 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69        0 },.#endi
11a39 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 47 65 74  f..#define osGet
11a3a 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 41 20  FileAttributesA 
11a3b 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29  ((DWORD(WINAPI*)
11a3c 28 4c 50 43 53 54 52 29 29 61 53 79 73 63 61 6c  (LPCSTR))aSyscal
11a3d 6c 5b 32 30 5d 2e 70 43 75 72 72 65 6e 74 29 0a  l[20].pCurrent).
11a3e 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f  .#if !SQLITE_OS_
11a3f 57 49 4e 52 54 20 26 26 20 64 65 66 69 6e 65 64  WINRT && defined
11a40 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41  (SQLITE_WIN32_HA
11a41 53 5f 57 49 44 45 29 0a 20 20 7b 20 22 47 65 74  S_WIDE).  { "Get
11a42 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 22  FileAttributesW"
11a43 2c 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29  ,      (SYSCALL)
11a44 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65  GetFileAttribute
11a45 73 57 2c 20 20 20 20 20 20 30 20 7d 2c 0a 23 65  sW,      0 },.#e
11a46 6c 73 65 0a 20 20 7b 20 22 47 65 74 46 69 6c 65  lse.  { "GetFile
11a47 41 74 74 72 69 62 75 74 65 73 57 22 2c 20 20 20  AttributesW",   
11a48 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20     (SYSCALL)0,  
11a49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a4a 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66       0 },.#endif
11a4b 0a 0a 23 64 65 66 69 6e 65 20 6f 73 47 65 74 46  ..#define osGetF
11a4c 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 20 28  ileAttributesW (
11a4d 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 28  (DWORD(WINAPI*)(
11a4e 4c 50 43 57 53 54 52 29 29 61 53 79 73 63 61 6c  LPCWSTR))aSyscal
11a4f 6c 5b 32 31 5d 2e 70 43 75 72 72 65 6e 74 29 0a  l[21].pCurrent).
11a50 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
11a51 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 57 49  ITE_WIN32_HAS_WI
11a52 44 45 29 0a 20 20 7b 20 22 47 65 74 46 69 6c 65  DE).  { "GetFile
11a53 41 74 74 72 69 62 75 74 65 73 45 78 57 22 2c 20  AttributesExW", 
11a54 20 20 20 28 53 59 53 43 41 4c 4c 29 47 65 74 46     (SYSCALL)GetF
11a55 69 6c 65 41 74 74 72 69 62 75 74 65 73 45 78 57  ileAttributesExW
11a56 2c 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a  ,    0 },.#else.
11a57 20 20 7b 20 22 47 65 74 46 69 6c 65 41 74 74 72    { "GetFileAttr
11a58 69 62 75 74 65 73 45 78 57 22 2c 20 20 20 20 28  ibutesExW",    (
11a59 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20  SYSCALL)0,      
11a5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a5b 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64   0 },.#endif..#d
11a5c 65 66 69 6e 65 20 6f 73 47 65 74 46 69 6c 65 41  efine osGetFileA
11a5d 74 74 72 69 62 75 74 65 73 45 78 57 20 28 28 42  ttributesExW ((B
11a5e 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 4c 50 43  OOL(WINAPI*)(LPC
11a5f 57 53 54 52 2c 47 45 54 5f 46 49 4c 45 45 58 5f  WSTR,GET_FILEEX_
11a60 49 4e 46 4f 5f 4c 45 56 45 4c 53 2c 20 5c 0a 20  INFO_LEVELS, \. 
11a61 20 20 20 20 20 20 20 4c 50 56 4f 49 44 29 29 61         LPVOID))a
11a62 53 79 73 63 61 6c 6c 5b 32 32 5d 2e 70 43 75 72  Syscall[22].pCur
11a63 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c 49  rent)..#if !SQLI
11a64 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 7b 20  TE_OS_WINRT.  { 
11a65 22 47 65 74 46 69 6c 65 53 69 7a 65 22 2c 20 20  "GetFileSize",  
11a66 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43             (SYSC
11a67 41 4c 4c 29 47 65 74 46 69 6c 65 53 69 7a 65 2c  ALL)GetFileSize,
11a68 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d               0 }
11a69 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 47 65 74  ,.#else.  { "Get
11a6a 46 69 6c 65 53 69 7a 65 22 2c 20 20 20 20 20 20  FileSize",      
11a6b 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
11a6c 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
11a6d 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65           0 },.#e
11a6e 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73  ndif..#define os
11a6f 47 65 74 46 69 6c 65 53 69 7a 65 20 28 28 44 57  GetFileSize ((DW
11a70 4f 52 44 28 57 49 4e 41 50 49 2a 29 28 48 41 4e  ORD(WINAPI*)(HAN
11a71 44 4c 45 2c 4c 50 44 57 4f 52 44 29 29 61 53 79  DLE,LPDWORD))aSy
11a72 73 63 61 6c 6c 5b 32 33 5d 2e 70 43 75 72 72 65  scall[23].pCurre
11a73 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c 49 54 45  nt)..#if !SQLITE
11a74 5f 4f 53 5f 57 49 4e 43 45 20 26 26 20 64 65 66  _OS_WINCE && def
11a75 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33  ined(SQLITE_WIN3
11a76 32 5f 48 41 53 5f 41 4e 53 49 29 0a 20 20 7b 20  2_HAS_ANSI).  { 
11a77 22 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65  "GetFullPathName
11a78 41 22 2c 20 20 20 20 20 20 20 20 28 53 59 53 43  A",        (SYSC
11a79 41 4c 4c 29 47 65 74 46 75 6c 6c 50 61 74 68 4e  ALL)GetFullPathN
11a7a 61 6d 65 41 2c 20 20 20 20 20 20 20 20 30 20 7d  ameA,        0 }
11a7b 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 47 65 74  ,.#else.  { "Get
11a7c 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 22 2c 20  FullPathNameA", 
11a7d 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
11a7e 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
11a7f 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65           0 },.#e
11a80 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73  ndif..#define os
11a81 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41  GetFullPathNameA
11a82 20 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a   ((DWORD(WINAPI*
11a83 29 28 4c 50 43 53 54 52 2c 44 57 4f 52 44 2c 4c  )(LPCSTR,DWORD,L
11a84 50 53 54 52 2c 20 5c 0a 20 20 20 20 20 20 20 20  PSTR, \.        
11a85 4c 50 53 54 52 2a 29 29 61 53 79 73 63 61 6c 6c  LPSTR*))aSyscall
11a86 5b 32 34 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a  [24].pCurrent)..
11a87 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57  #if !SQLITE_OS_W
11a88 49 4e 43 45 20 26 26 20 21 53 51 4c 49 54 45 5f  INCE && !SQLITE_
11a89 4f 53 5f 57 49 4e 52 54 20 26 26 20 64 65 66 69  OS_WINRT && defi
11a8a 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32  ned(SQLITE_WIN32
11a8b 5f 48 41 53 5f 57 49 44 45 29 0a 20 20 7b 20 22  _HAS_WIDE).  { "
11a8c 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57  GetFullPathNameW
11a8d 22 2c 20 20 20 20 20 20 20 20 28 53 59 53 43 41  ",        (SYSCA
11a8e 4c 4c 29 47 65 74 46 75 6c 6c 50 61 74 68 4e 61  LL)GetFullPathNa
11a8f 6d 65 57 2c 20 20 20 20 20 20 20 20 30 20 7d 2c  meW,        0 },
11a90 0a 23 65 6c 73 65 0a 20 20 7b 20 22 47 65 74 46  .#else.  { "GetF
11a91 75 6c 6c 50 61 74 68 4e 61 6d 65 57 22 2c 20 20  ullPathNameW",  
11a92 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30        (SYSCALL)0
11a93 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
11a94 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e          0 },.#en
11a95 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 47  dif..#define osG
11a96 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 20  etFullPathNameW 
11a97 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29  ((DWORD(WINAPI*)
11a98 28 4c 50 43 57 53 54 52 2c 44 57 4f 52 44 2c 4c  (LPCWSTR,DWORD,L
11a99 50 57 53 54 52 2c 20 5c 0a 20 20 20 20 20 20 20  PWSTR, \.       
11a9a 20 4c 50 57 53 54 52 2a 29 29 61 53 79 73 63 61   LPWSTR*))aSysca
11a9b 6c 6c 5b 32 35 5d 2e 70 43 75 72 72 65 6e 74 29  ll[25].pCurrent)
11a9c 0a 0a 20 20 7b 20 22 47 65 74 4c 61 73 74 45 72  ..  { "GetLastEr
11a9d 72 6f 72 22 2c 20 20 20 20 20 20 20 20 20 20 20  ror",           
11a9e 20 28 53 59 53 43 41 4c 4c 29 47 65 74 4c 61 73   (SYSCALL)GetLas
11a9f 74 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20  tError,         
11aa0 20 20 20 30 20 7d 2c 0a 0a 23 64 65 66 69 6e 65     0 },..#define
11aa1 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 20   osGetLastError 
11aa2 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29  ((DWORD(WINAPI*)
11aa3 28 56 4f 49 44 29 29 61 53 79 73 63 61 6c 6c 5b  (VOID))aSyscall[
11aa4 32 36 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23  26].pCurrent)..#
11aa5 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
11aa6 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
11aa7 45 4e 53 49 4f 4e 29 0a 23 69 66 20 53 51 4c 49  ENSION).#if SQLI
11aa8 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 2f 2a  TE_OS_WINCE.  /*
11aa9 20 54 68 65 20 47 65 74 50 72 6f 63 41 64 64 72   The GetProcAddr
11aaa 65 73 73 41 28 29 20 72 6f 75 74 69 6e 65 20 69  essA() routine i
11aab 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
11aac 20 6f 6e 20 57 69 6e 64 6f 77 73 20 43 45 2e 20   on Windows CE. 
11aad 2a 2f 0a 20 20 7b 20 22 47 65 74 50 72 6f 63 41  */.  { "GetProcA
11aae 64 64 72 65 73 73 41 22 2c 20 20 20 20 20 20 20  ddressA",       
11aaf 20 20 28 53 59 53 43 41 4c 4c 29 47 65 74 50 72    (SYSCALL)GetPr
11ab0 6f 63 41 64 64 72 65 73 73 41 2c 20 20 20 20 20  ocAddressA,     
11ab1 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20      0 },.#else. 
11ab2 20 2f 2a 20 41 6c 6c 20 6f 74 68 65 72 20 57 69   /* All other Wi
11ab3 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 20  ndows platforms 
11ab4 65 78 70 65 63 74 20 47 65 74 50 72 6f 63 41 64  expect GetProcAd
11ab5 64 72 65 73 73 28 29 20 74 6f 20 74 61 6b 65 0a  dress() to take.
11ab6 20 20 2a 2a 20 61 6e 20 41 4e 53 49 20 73 74 72    ** an ANSI str
11ab7 69 6e 67 20 72 65 67 61 72 64 6c 65 73 73 20 6f  ing regardless o
11ab8 66 20 74 68 65 20 5f 55 4e 49 43 4f 44 45 20 73  f the _UNICODE s
11ab9 65 74 74 69 6e 67 20 2a 2f 0a 20 20 7b 20 22 47  etting */.  { "G
11aba 65 74 50 72 6f 63 41 64 64 72 65 73 73 41 22 2c  etProcAddressA",
11abb 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c           (SYSCAL
11abc 4c 29 47 65 74 50 72 6f 63 41 64 64 72 65 73 73  L)GetProcAddress
11abd 2c 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a  ,          0 },.
11abe 23 65 6e 64 69 66 0a 23 65 6c 73 65 0a 20 20 7b  #endif.#else.  {
11abf 20 22 47 65 74 50 72 6f 63 41 64 64 72 65 73 73   "GetProcAddress
11ac0 41 22 2c 20 20 20 20 20 20 20 20 20 28 53 59 53  A",         (SYS
11ac1 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20  CALL)0,         
11ac2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
11ac3 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69  },.#endif..#defi
11ac4 6e 65 20 6f 73 47 65 74 50 72 6f 63 41 64 64 72  ne osGetProcAddr
11ac5 65 73 73 41 20 28 28 46 41 52 50 52 4f 43 28 57  essA ((FARPROC(W
11ac6 49 4e 41 50 49 2a 29 28 48 4d 4f 44 55 4c 45 2c  INAPI*)(HMODULE,
11ac7 20 5c 0a 20 20 20 20 20 20 20 20 4c 50 43 53 54   \.        LPCST
11ac8 52 29 29 61 53 79 73 63 61 6c 6c 5b 32 37 5d 2e  R))aSyscall[27].
11ac9 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21  pCurrent)..#if !
11aca 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a  SQLITE_OS_WINRT.
11acb 20 20 7b 20 22 47 65 74 53 79 73 74 65 6d 49 6e    { "GetSystemIn
11acc 66 6f 22 2c 20 20 20 20 20 20 20 20 20 20 20 28  fo",           (
11acd 53 59 53 43 41 4c 4c 29 47 65 74 53 79 73 74 65  SYSCALL)GetSyste
11ace 6d 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20  mInfo,          
11acf 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20   0 },.#else.  { 
11ad0 22 47 65 74 53 79 73 74 65 6d 49 6e 66 6f 22 2c  "GetSystemInfo",
11ad1 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43             (SYSC
11ad2 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20  ALL)0,          
11ad3 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d               0 }
11ad4 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e  ,.#endif..#defin
11ad5 65 20 6f 73 47 65 74 53 79 73 74 65 6d 49 6e 66  e osGetSystemInf
11ad6 6f 20 28 28 56 4f 49 44 28 57 49 4e 41 50 49 2a  o ((VOID(WINAPI*
11ad7 29 28 4c 50 53 59 53 54 45 4d 5f 49 4e 46 4f 29  )(LPSYSTEM_INFO)
11ad8 29 61 53 79 73 63 61 6c 6c 5b 32 38 5d 2e 70 43  )aSyscall[28].pC
11ad9 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 47 65  urrent)..  { "Ge
11ada 74 53 79 73 74 65 6d 54 69 6d 65 22 2c 20 20 20  tSystemTime",   
11adb 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c          (SYSCALL
11adc 29 47 65 74 53 79 73 74 65 6d 54 69 6d 65 2c 20  )GetSystemTime, 
11add 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 0a            0 },..
11ade 23 64 65 66 69 6e 65 20 6f 73 47 65 74 53 79 73  #define osGetSys
11adf 74 65 6d 54 69 6d 65 20 28 28 56 4f 49 44 28 57  temTime ((VOID(W
11ae0 49 4e 41 50 49 2a 29 28 4c 50 53 59 53 54 45 4d  INAPI*)(LPSYSTEM
11ae1 54 49 4d 45 29 29 61 53 79 73 63 61 6c 6c 5b 32  TIME))aSyscall[2
11ae2 39 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69  9].pCurrent)..#i
11ae3 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  f !SQLITE_OS_WIN
11ae4 43 45 0a 20 20 7b 20 22 47 65 74 53 79 73 74 65  CE.  { "GetSyste
11ae5 6d 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 22  mTimeAsFileTime"
11ae6 2c 20 28 53 59 53 43 41 4c 4c 29 47 65 74 53 79  , (SYSCALL)GetSy
11ae7 73 74 65 6d 54 69 6d 65 41 73 46 69 6c 65 54 69  stemTimeAsFileTi
11ae8 6d 65 2c 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20  me, 0 },.#else. 
11ae9 20 7b 20 22 47 65 74 53 79 73 74 65 6d 54 69 6d   { "GetSystemTim
11aea 65 41 73 46 69 6c 65 54 69 6d 65 22 2c 20 28 53  eAsFileTime", (S
11aeb 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20  YSCALL)0,       
11aec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11aed 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65  0 },.#endif..#de
11aee 66 69 6e 65 20 6f 73 47 65 74 53 79 73 74 65 6d  fine osGetSystem
11aef 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 20 28  TimeAsFileTime (
11af0 28 56 4f 49 44 28 57 49 4e 41 50 49 2a 29 28 20  (VOID(WINAPI*)( 
11af1 5c 0a 20 20 20 20 20 20 20 20 4c 50 46 49 4c 45  \.        LPFILE
11af2 54 49 4d 45 29 29 61 53 79 73 63 61 6c 6c 5b 33  TIME))aSyscall[3
11af3 30 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69  0].pCurrent)..#i
11af4 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
11af5 5f 57 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 29  _WIN32_HAS_ANSI)
11af6 0a 20 20 7b 20 22 47 65 74 54 65 6d 70 50 61 74  .  { "GetTempPat
11af7 68 41 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  hA",            
11af8 28 53 59 53 43 41 4c 4c 29 47 65 74 54 65 6d 70  (SYSCALL)GetTemp
11af9 50 61 74 68 41 2c 20 20 20 20 20 20 20 20 20 20  PathA,          
11afa 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b    0 },.#else.  {
11afb 20 22 47 65 74 54 65 6d 70 50 61 74 68 41 22 2c   "GetTempPathA",
11afc 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53              (SYS
11afd 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20  CALL)0,         
11afe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
11aff 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69  },.#endif..#defi
11b00 6e 65 20 6f 73 47 65 74 54 65 6d 70 50 61 74 68  ne osGetTempPath
11b01 41 20 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49  A ((DWORD(WINAPI
11b02 2a 29 28 44 57 4f 52 44 2c 4c 50 53 54 52 29 29  *)(DWORD,LPSTR))
11b03 61 53 79 73 63 61 6c 6c 5b 33 31 5d 2e 70 43 75  aSyscall[31].pCu
11b04 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c  rrent)..#if !SQL
11b05 49 54 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26 20  ITE_OS_WINRT && 
11b06 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57  defined(SQLITE_W
11b07 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a 20  IN32_HAS_WIDE). 
11b08 20 7b 20 22 47 65 74 54 65 6d 70 50 61 74 68 57   { "GetTempPathW
11b09 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 28 53  ",            (S
11b0a 59 53 43 41 4c 4c 29 47 65 74 54 65 6d 70 50 61  YSCALL)GetTempPa
11b0b 74 68 57 2c 20 20 20 20 20 20 20 20 20 20 20 20  thW,            
11b0c 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22  0 },.#else.  { "
11b0d 47 65 74 54 65 6d 70 50 61 74 68 57 22 2c 20 20  GetTempPathW",  
11b0e 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41            (SYSCA
11b0f 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20  LL)0,           
11b10 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c              0 },
11b11 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65  .#endif..#define
11b12 20 6f 73 47 65 74 54 65 6d 70 50 61 74 68 57 20   osGetTempPathW 
11b13 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29  ((DWORD(WINAPI*)
11b14 28 44 57 4f 52 44 2c 4c 50 57 53 54 52 29 29 61  (DWORD,LPWSTR))a
11b15 53 79 73 63 61 6c 6c 5b 33 32 5d 2e 70 43 75 72  Syscall[32].pCur
11b16 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c 49  rent)..#if !SQLI
11b17 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 7b 20  TE_OS_WINRT.  { 
11b18 22 47 65 74 54 69 63 6b 43 6f 75 6e 74 22 2c 20  "GetTickCount", 
11b19 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43             (SYSC
11b1a 41 4c 4c 29 47 65 74 54 69 63 6b 43 6f 75 6e 74  ALL)GetTickCount
11b1b 2c 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d  ,            0 }
11b1c 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 47 65 74  ,.#else.  { "Get
11b1d 54 69 63 6b 43 6f 75 6e 74 22 2c 20 20 20 20 20  TickCount",     
11b1e 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
11b1f 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
11b20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65           0 },.#e
11b21 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73  ndif..#define os
11b22 47 65 74 54 69 63 6b 43 6f 75 6e 74 20 28 28 44  GetTickCount ((D
11b23 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 28 56 4f  WORD(WINAPI*)(VO
11b24 49 44 29 29 61 53 79 73 63 61 6c 6c 5b 33 33 5d  ID))aSyscall[33]
11b25 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20  .pCurrent)..#if 
11b26 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57  defined(SQLITE_W
11b27 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 29 0a 20  IN32_HAS_ANSI). 
11b28 20 7b 20 22 47 65 74 56 65 72 73 69 6f 6e 45 78   { "GetVersionEx
11b29 41 22 2c 20 20 20 20 20 20 20 20 20 20 20 28 53  A",           (S
11b2a 59 53 43 41 4c 4c 29 47 65 74 56 65 72 73 69 6f  YSCALL)GetVersio
11b2b 6e 45 78 41 2c 20 20 20 20 20 20 20 20 20 20 20  nExA,           
11b2c 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22  0 },.#else.  { "
11b2d 47 65 74 56 65 72 73 69 6f 6e 45 78 41 22 2c 20  GetVersionExA", 
11b2e 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41            (SYSCA
11b2f 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20  LL)0,           
11b30 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c              0 },
11b31 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65  .#endif..#define
11b32 20 6f 73 47 65 74 56 65 72 73 69 6f 6e 45 78 41   osGetVersionExA
11b33 20 28 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29   ((BOOL(WINAPI*)
11b34 28 20 5c 0a 20 20 20 20 20 20 20 20 4c 50 4f 53  ( \.        LPOS
11b35 56 45 52 53 49 4f 4e 49 4e 46 4f 41 29 29 61 53  VERSIONINFOA))aS
11b36 79 73 63 61 6c 6c 5b 33 34 5d 2e 70 43 75 72 72  yscall[34].pCurr
11b37 65 6e 74 29 0a 0a 20 20 7b 20 22 48 65 61 70 41  ent)..  { "HeapA
11b38 6c 6c 6f 63 22 2c 20 20 20 20 20 20 20 20 20 20  lloc",          
11b39 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 48 65       (SYSCALL)He
11b3a 61 70 41 6c 6c 6f 63 2c 20 20 20 20 20 20 20 20  apAlloc,        
11b3b 20 20 20 20 20 20 20 30 20 7d 2c 0a 0a 23 64 65         0 },..#de
11b3c 66 69 6e 65 20 6f 73 48 65 61 70 41 6c 6c 6f 63  fine osHeapAlloc
11b3d 20 28 28 4c 50 56 4f 49 44 28 57 49 4e 41 50 49   ((LPVOID(WINAPI
11b3e 2a 29 28 48 41 4e 44 4c 45 2c 44 57 4f 52 44 2c  *)(HANDLE,DWORD,
11b3f 20 5c 0a 20 20 20 20 20 20 20 20 53 49 5a 45 5f   \.        SIZE_
11b40 54 29 29 61 53 79 73 63 61 6c 6c 5b 33 35 5d 2e  T))aSyscall[35].
11b41 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21  pCurrent)..#if !
11b42 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a  SQLITE_OS_WINRT.
11b43 20 20 7b 20 22 48 65 61 70 43 72 65 61 74 65 22    { "HeapCreate"
11b44 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28  ,              (
11b45 53 59 53 43 41 4c 4c 29 48 65 61 70 43 72 65 61  SYSCALL)HeapCrea
11b46 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
11b47 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20   0 },.#else.  { 
11b48 22 48 65 61 70 43 72 65 61 74 65 22 2c 20 20 20  "HeapCreate",   
11b49 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43             (SYSC
11b4a 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20  ALL)0,          
11b4b 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d               0 }
11b4c 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e  ,.#endif..#defin
11b4d 65 20 6f 73 48 65 61 70 43 72 65 61 74 65 20 28  e osHeapCreate (
11b4e 28 48 41 4e 44 4c 45 28 57 49 4e 41 50 49 2a 29  (HANDLE(WINAPI*)
11b4f 28 44 57 4f 52 44 2c 53 49 5a 45 5f 54 2c 20 5c  (DWORD,SIZE_T, \
11b50 0a 20 20 20 20 20 20 20 20 53 49 5a 45 5f 54 29  .        SIZE_T)
11b51 29 61 53 79 73 63 61 6c 6c 5b 33 36 5d 2e 70 43  )aSyscall[36].pC
11b52 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51  urrent)..#if !SQ
11b53 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20  LITE_OS_WINRT.  
11b54 7b 20 22 48 65 61 70 44 65 73 74 72 6f 79 22 2c  { "HeapDestroy",
11b55 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59               (SY
11b56 53 43 41 4c 4c 29 48 65 61 70 44 65 73 74 72 6f  SCALL)HeapDestro
11b57 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 30  y,             0
11b58 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 48   },.#else.  { "H
11b59 65 61 70 44 65 73 74 72 6f 79 22 2c 20 20 20 20  eapDestroy",    
11b5a 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c           (SYSCAL
11b5b 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  L)0,            
11b5c 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a             0 },.
11b5d 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20  #endif..#define 
11b5e 6f 73 48 65 61 70 44 65 73 74 72 6f 79 20 28 28  osHeapDestroy ((
11b5f 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41  BOOL(WINAPI*)(HA
11b60 4e 44 4c 45 29 29 61 53 79 73 63 61 6c 6c 5b 33  NDLE))aSyscall[3
11b61 37 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20  7].pCurrent)..  
11b62 7b 20 22 48 65 61 70 46 72 65 65 22 2c 20 20 20  { "HeapFree",   
11b63 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59               (SY
11b64 53 43 41 4c 4c 29 48 65 61 70 46 72 65 65 2c 20  SCALL)HeapFree, 
11b65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
11b66 20 7d 2c 0a 0a 23 64 65 66 69 6e 65 20 6f 73 48   },..#define osH
11b67 65 61 70 46 72 65 65 20 28 28 42 4f 4f 4c 28 57  eapFree ((BOOL(W
11b68 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45 2c 44  INAPI*)(HANDLE,D
11b69 57 4f 52 44 2c 4c 50 56 4f 49 44 29 29 61 53 79  WORD,LPVOID))aSy
11b6a 73 63 61 6c 6c 5b 33 38 5d 2e 70 43 75 72 72 65  scall[38].pCurre
11b6b 6e 74 29 0a 0a 20 20 7b 20 22 48 65 61 70 52 65  nt)..  { "HeapRe
11b6c 41 6c 6c 6f 63 22 2c 20 20 20 20 20 20 20 20 20  Alloc",         
11b6d 20 20 20 20 28 53 59 53 43 41 4c 4c 29 48 65 61      (SYSCALL)Hea
11b6e 70 52 65 41 6c 6c 6f 63 2c 20 20 20 20 20 20 20  pReAlloc,       
11b6f 20 20 20 20 20 20 30 20 7d 2c 0a 0a 23 64 65 66        0 },..#def
11b70 69 6e 65 20 6f 73 48 65 61 70 52 65 41 6c 6c 6f  ine osHeapReAllo
11b71 63 20 28 28 4c 50 56 4f 49 44 28 57 49 4e 41 50  c ((LPVOID(WINAP
11b72 49 2a 29 28 48 41 4e 44 4c 45 2c 44 57 4f 52 44  I*)(HANDLE,DWORD
11b73 2c 4c 50 56 4f 49 44 2c 20 5c 0a 20 20 20 20 20  ,LPVOID, \.     
11b74 20 20 20 53 49 5a 45 5f 54 29 29 61 53 79 73 63     SIZE_T))aSysc
11b75 61 6c 6c 5b 33 39 5d 2e 70 43 75 72 72 65 6e 74  all[39].pCurrent
11b76 29 0a 0a 20 20 7b 20 22 48 65 61 70 53 69 7a 65  )..  { "HeapSize
11b77 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
11b78 20 20 28 53 59 53 43 41 4c 4c 29 48 65 61 70 53    (SYSCALL)HeapS
11b79 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ize,            
11b7a 20 20 20 20 30 20 7d 2c 0a 0a 23 64 65 66 69 6e      0 },..#defin
11b7b 65 20 6f 73 48 65 61 70 53 69 7a 65 20 28 28 53  e osHeapSize ((S
11b7c 49 5a 45 5f 54 28 57 49 4e 41 50 49 2a 29 28 48  IZE_T(WINAPI*)(H
11b7d 41 4e 44 4c 45 2c 44 57 4f 52 44 2c 20 5c 0a 20  ANDLE,DWORD, \. 
11b7e 20 20 20 20 20 20 20 4c 50 43 56 4f 49 44 29 29         LPCVOID))
11b7f 61 53 79 73 63 61 6c 6c 5b 34 30 5d 2e 70 43 75  aSyscall[40].pCu
11b80 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c  rrent)..#if !SQL
11b81 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 7b  ITE_OS_WINRT.  {
11b82 20 22 48 65 61 70 56 61 6c 69 64 61 74 65 22 2c   "HeapValidate",
11b83 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53              (SYS
11b84 43 41 4c 4c 29 48 65 61 70 56 61 6c 69 64 61 74  CALL)HeapValidat
11b85 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 30 20  e,            0 
11b86 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 48 65  },.#else.  { "He
11b87 61 70 56 61 6c 69 64 61 74 65 22 2c 20 20 20 20  apValidate",    
11b88 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c          (SYSCALL
11b89 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  )0,             
11b8a 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23            0 },.#
11b8b 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f  endif..#define o
11b8c 73 48 65 61 70 56 61 6c 69 64 61 74 65 20 28 28  sHeapValidate ((
11b8d 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41  BOOL(WINAPI*)(HA
11b8e 4e 44 4c 45 2c 44 57 4f 52 44 2c 20 5c 0a 20 20  NDLE,DWORD, \.  
11b8f 20 20 20 20 20 20 4c 50 43 56 4f 49 44 29 29 61        LPCVOID))a
11b90 53 79 73 63 61 6c 6c 5b 34 31 5d 2e 70 43 75 72  Syscall[41].pCur
11b91 72 65 6e 74 29 0a 0a 23 69 66 20 64 65 66 69 6e  rent)..#if defin
11b92 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f  ed(SQLITE_WIN32_
11b93 48 41 53 5f 41 4e 53 49 29 20 26 26 20 21 64 65  HAS_ANSI) && !de
11b94 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
11b95 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
11b96 29 0a 20 20 7b 20 22 4c 6f 61 64 4c 69 62 72 61  ).  { "LoadLibra
11b97 72 79 41 22 2c 20 20 20 20 20 20 20 20 20 20 20  ryA",           
11b98 20 28 53 59 53 43 41 4c 4c 29 4c 6f 61 64 4c 69   (SYSCALL)LoadLi
11b99 62 72 61 72 79 41 2c 20 20 20 20 20 20 20 20 20  braryA,         
11b9a 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20     0 },.#else.  
11b9b 7b 20 22 4c 6f 61 64 4c 69 62 72 61 72 79 41 22  { "LoadLibraryA"
11b9c 2c 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59  ,            (SY
11b9d 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20  SCALL)0,        
11b9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
11b9f 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66   },.#endif..#def
11ba0 69 6e 65 20 6f 73 4c 6f 61 64 4c 69 62 72 61 72  ine osLoadLibrar
11ba1 79 41 20 28 28 48 4d 4f 44 55 4c 45 28 57 49 4e  yA ((HMODULE(WIN
11ba2 41 50 49 2a 29 28 4c 50 43 53 54 52 29 29 61 53  API*)(LPCSTR))aS
11ba3 79 73 63 61 6c 6c 5b 34 32 5d 2e 70 43 75 72 72  yscall[42].pCurr
11ba4 65 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c 49 54  ent)..#if !SQLIT
11ba5 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26 20 64 65  E_OS_WINRT && de
11ba6 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e  fined(SQLITE_WIN
11ba7 33 32 5f 48 41 53 5f 57 49 44 45 29 20 26 26 20  32_HAS_WIDE) && 
11ba8 5c 0a 20 20 20 20 20 20 20 20 21 64 65 66 69 6e  \.        !defin
11ba9 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ed(SQLITE_OMIT_L
11baa 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 29 0a 20  OAD_EXTENSION). 
11bab 20 7b 20 22 4c 6f 61 64 4c 69 62 72 61 72 79 57   { "LoadLibraryW
11bac 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 28 53  ",            (S
11bad 59 53 43 41 4c 4c 29 4c 6f 61 64 4c 69 62 72 61  YSCALL)LoadLibra
11bae 72 79 57 2c 20 20 20 20 20 20 20 20 20 20 20 20  ryW,            
11baf 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22  0 },.#else.  { "
11bb0 4c 6f 61 64 4c 69 62 72 61 72 79 57 22 2c 20 20  LoadLibraryW",  
11bb1 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41            (SYSCA
11bb2 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20  LL)0,           
11bb3 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c              0 },
11bb4 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65  .#endif..#define
11bb5 20 6f 73 4c 6f 61 64 4c 69 62 72 61 72 79 57 20   osLoadLibraryW 
11bb6 28 28 48 4d 4f 44 55 4c 45 28 57 49 4e 41 50 49  ((HMODULE(WINAPI
11bb7 2a 29 28 4c 50 43 57 53 54 52 29 29 61 53 79 73  *)(LPCWSTR))aSys
11bb8 63 61 6c 6c 5b 34 33 5d 2e 70 43 75 72 72 65 6e  call[43].pCurren
11bb9 74 29 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 5f  t)..#if !SQLITE_
11bba 4f 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22 4c 6f  OS_WINRT.  { "Lo
11bbb 63 61 6c 46 72 65 65 22 2c 20 20 20 20 20 20 20  calFree",       
11bbc 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c          (SYSCALL
11bbd 29 4c 6f 63 61 6c 46 72 65 65 2c 20 20 20 20 20  )LocalFree,     
11bbe 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23            0 },.#
11bbf 65 6c 73 65 0a 20 20 7b 20 22 4c 6f 63 61 6c 46  else.  { "LocalF
11bc0 72 65 65 22 2c 20 20 20 20 20 20 20 20 20 20 20  ree",           
11bc1 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20      (SYSCALL)0, 
11bc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11bc3 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69        0 },.#endi
11bc4 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 4c 6f 63  f..#define osLoc
11bc5 61 6c 46 72 65 65 20 28 28 48 4c 4f 43 41 4c 28  alFree ((HLOCAL(
11bc6 57 49 4e 41 50 49 2a 29 28 48 4c 4f 43 41 4c 29  WINAPI*)(HLOCAL)
11bc7 29 61 53 79 73 63 61 6c 6c 5b 34 34 5d 2e 70 43  )aSyscall[44].pC
11bc8 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53 51  urrent)..#if !SQ
11bc9 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26  LITE_OS_WINCE &&
11bca 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52   !SQLITE_OS_WINR
11bcb 54 0a 20 20 7b 20 22 4c 6f 63 6b 46 69 6c 65 22  T.  { "LockFile"
11bcc 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
11bcd 20 28 53 59 53 43 41 4c 4c 29 4c 6f 63 6b 46 69   (SYSCALL)LockFi
11bce 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  le,             
11bcf 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20     0 },.#else.  
11bd0 7b 20 22 4c 6f 63 6b 46 69 6c 65 22 2c 20 20 20  { "LockFile",   
11bd1 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59               (SY
11bd2 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20  SCALL)0,        
11bd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
11bd4 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e   },.#endif..#ifn
11bd5 64 65 66 20 6f 73 4c 6f 63 6b 46 69 6c 65 0a 23  def osLockFile.#
11bd6 64 65 66 69 6e 65 20 6f 73 4c 6f 63 6b 46 69 6c  define osLockFil
11bd7 65 20 28 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a  e ((BOOL(WINAPI*
11bd8 29 28 48 41 4e 44 4c 45 2c 44 57 4f 52 44 2c 44  )(HANDLE,DWORD,D
11bd9 57 4f 52 44 2c 44 57 4f 52 44 2c 20 5c 0a 20 20  WORD,DWORD, \.  
11bda 20 20 20 20 20 20 44 57 4f 52 44 29 29 61 53 79        DWORD))aSy
11bdb 73 63 61 6c 6c 5b 34 35 5d 2e 70 43 75 72 72 65  scall[45].pCurre
11bdc 6e 74 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20  nt).#endif..#if 
11bdd 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45  !SQLITE_OS_WINCE
11bde 0a 20 20 7b 20 22 4c 6f 63 6b 46 69 6c 65 45 78  .  { "LockFileEx
11bdf 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
11be0 28 53 59 53 43 41 4c 4c 29 4c 6f 63 6b 46 69 6c  (SYSCALL)LockFil
11be1 65 45 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  eEx,            
11be2 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b    0 },.#else.  {
11be3 20 22 4c 6f 63 6b 46 69 6c 65 45 78 22 2c 20 20   "LockFileEx",  
11be4 20 20 20 20 20 20 20 20 20 20 20 20 28 53 59 53              (SYS
11be5 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20  CALL)0,         
11be6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
11be7 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  },.#endif..#ifnd
11be8 65 66 20 6f 73 4c 6f 63 6b 46 69 6c 65 45 78 0a  ef osLockFileEx.
11be9 23 64 65 66 69 6e 65 20 6f 73 4c 6f 63 6b 46 69  #define osLockFi
11bea 6c 65 45 78 20 28 28 42 4f 4f 4c 28 57 49 4e 41  leEx ((BOOL(WINA
11beb 50 49 2a 29 28 48 41 4e 44 4c 45 2c 44 57 4f 52  PI*)(HANDLE,DWOR
11bec 44 2c 44 57 4f 52 44 2c 44 57 4f 52 44 2c 44 57  D,DWORD,DWORD,DW
11bed 4f 52 44 2c 20 5c 0a 20 20 20 20 20 20 20 20 4c  ORD, \.        L
11bee 50 4f 56 45 52 4c 41 50 50 45 44 29 29 61 53 79  POVERLAPPED))aSy
11bef 73 63 61 6c 6c 5b 34 36 5d 2e 70 43 75 72 72 65  scall[46].pCurre
11bf0 6e 74 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20  nt).#endif..#if 
11bf1 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20  SQLITE_OS_WINCE 
11bf2 7c 7c 20 28 21 53 51 4c 49 54 45 5f 4f 53 5f 57  || (!SQLITE_OS_W
11bf3 49 4e 52 54 20 26 26 20 21 64 65 66 69 6e 65 64  INRT && !defined
11bf4 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c  (SQLITE_OMIT_WAL
11bf5 29 29 0a 20 20 7b 20 22 4d 61 70 56 69 65 77 4f  )).  { "MapViewO
11bf6 66 46 69 6c 65 22 2c 20 20 20 20 20 20 20 20 20  fFile",         
11bf7 20 20 28 53 59 53 43 41 4c 4c 29 4d 61 70 56 69    (SYSCALL)MapVi
11bf8 65 77 4f 66 46 69 6c 65 2c 20 20 20 20 20 20 20  ewOfFile,       
11bf9 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20      0 },.#else. 
11bfa 20 7b 20 22 4d 61 70 56 69 65 77 4f 66 46 69 6c   { "MapViewOfFil
11bfb 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 28 53  e",           (S
11bfc 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20  YSCALL)0,       
11bfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11bfe 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65  0 },.#endif..#de
11bff 66 69 6e 65 20 6f 73 4d 61 70 56 69 65 77 4f 66  fine osMapViewOf
11c00 46 69 6c 65 20 28 28 4c 50 56 4f 49 44 28 57 49  File ((LPVOID(WI
11c01 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45 2c 44 57  NAPI*)(HANDLE,DW
11c02 4f 52 44 2c 44 57 4f 52 44 2c 44 57 4f 52 44 2c  ORD,DWORD,DWORD,
11c03 20 5c 0a 20 20 20 20 20 20 20 20 53 49 5a 45 5f   \.        SIZE_
11c04 54 29 29 61 53 79 73 63 61 6c 6c 5b 34 37 5d 2e  T))aSyscall[47].
11c05 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22  pCurrent)..  { "
11c06 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43  MultiByteToWideC
11c07 68 61 72 22 2c 20 20 20 20 20 28 53 59 53 43 41  har",     (SYSCA
11c08 4c 4c 29 4d 75 6c 74 69 42 79 74 65 54 6f 57 69  LL)MultiByteToWi
11c09 64 65 43 68 61 72 2c 20 20 20 20 20 30 20 7d 2c  deChar,     0 },
11c0a 0a 0a 23 64 65 66 69 6e 65 20 6f 73 4d 75 6c 74  ..#define osMult
11c0b 69 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 20  iByteToWideChar 
11c0c 28 28 69 6e 74 28 57 49 4e 41 50 49 2a 29 28 55  ((int(WINAPI*)(U
11c0d 49 4e 54 2c 44 57 4f 52 44 2c 4c 50 43 53 54 52  INT,DWORD,LPCSTR
11c0e 2c 69 6e 74 2c 4c 50 57 53 54 52 2c 20 5c 0a 20  ,int,LPWSTR, \. 
11c0f 20 20 20 20 20 20 20 69 6e 74 29 29 61 53 79 73         int))aSys
11c10 63 61 6c 6c 5b 34 38 5d 2e 70 43 75 72 72 65 6e  call[48].pCurren
11c11 74 29 0a 0a 20 20 7b 20 22 51 75 65 72 79 50 65  t)..  { "QueryPe
11c12 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72  rformanceCounter
11c13 22 2c 20 28 53 59 53 43 41 4c 4c 29 51 75 65 72  ", (SYSCALL)Quer
11c14 79 50 65 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e  yPerformanceCoun
11c15 74 65 72 2c 20 30 20 7d 2c 0a 0a 23 64 65 66 69  ter, 0 },..#defi
11c16 6e 65 20 6f 73 51 75 65 72 79 50 65 72 66 6f 72  ne osQueryPerfor
11c17 6d 61 6e 63 65 43 6f 75 6e 74 65 72 20 28 28 42  manceCounter ((B
11c18 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 20 5c 0a  OOL(WINAPI*)( \.
11c19 20 20 20 20 20 20 20 20 4c 41 52 47 45 5f 49 4e          LARGE_IN
11c1a 54 45 47 45 52 2a 29 29 61 53 79 73 63 61 6c 6c  TEGER*))aSyscall
11c1b 5b 34 39 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a  [49].pCurrent)..
11c1c 20 20 7b 20 22 52 65 61 64 46 69 6c 65 22 2c 20    { "ReadFile", 
11c1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
11c1e 53 59 53 43 41 4c 4c 29 52 65 61 64 46 69 6c 65  SYSCALL)ReadFile
11c1f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
11c20 20 30 20 7d 2c 0a 0a 23 64 65 66 69 6e 65 20 6f   0 },..#define o
11c21 73 52 65 61 64 46 69 6c 65 20 28 28 42 4f 4f 4c  sReadFile ((BOOL
11c22 28 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45  (WINAPI*)(HANDLE
11c23 2c 4c 50 56 4f 49 44 2c 44 57 4f 52 44 2c 4c 50  ,LPVOID,DWORD,LP
11c24 44 57 4f 52 44 2c 20 5c 0a 20 20 20 20 20 20 20  DWORD, \.       
11c25 20 4c 50 4f 56 45 52 4c 41 50 50 45 44 29 29 61   LPOVERLAPPED))a
11c26 53 79 73 63 61 6c 6c 5b 35 30 5d 2e 70 43 75 72  Syscall[50].pCur
11c27 72 65 6e 74 29 0a 0a 20 20 7b 20 22 53 65 74 45  rent)..  { "SetE
11c28 6e 64 4f 66 46 69 6c 65 22 2c 20 20 20 20 20 20  ndOfFile",      
11c29 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 53        (SYSCALL)S
11c2a 65 74 45 6e 64 4f 66 46 69 6c 65 2c 20 20 20 20  etEndOfFile,    
11c2b 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 0a 23 64          0 },..#d
11c2c 65 66 69 6e 65 20 6f 73 53 65 74 45 6e 64 4f 66  efine osSetEndOf
11c2d 46 69 6c 65 20 28 28 42 4f 4f 4c 28 57 49 4e 41  File ((BOOL(WINA
11c2e 50 49 2a 29 28 48 41 4e 44 4c 45 29 29 61 53 79  PI*)(HANDLE))aSy
11c2f 73 63 61 6c 6c 5b 35 31 5d 2e 70 43 75 72 72 65  scall[51].pCurre
11c30 6e 74 29 0a 0a 23 69 66 20 21 53 51 4c 49 54 45  nt)..#if !SQLITE
11c31 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22 53  _OS_WINRT.  { "S
11c32 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 22 2c 20  etFilePointer", 
11c33 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c           (SYSCAL
11c34 4c 29 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72  L)SetFilePointer
11c35 2c 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a  ,          0 },.
11c36 23 65 6c 73 65 0a 20 20 7b 20 22 53 65 74 46 69  #else.  { "SetFi
11c37 6c 65 50 6f 69 6e 74 65 72 22 2c 20 20 20 20 20  lePointer",     
11c38 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c       (SYSCALL)0,
11c39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11c3a 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64         0 },.#end
11c3b 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 53 65  if..#define osSe
11c3c 74 46 69 6c 65 50 6f 69 6e 74 65 72 20 28 28 44  tFilePointer ((D
11c3d 57 4f 52 44 28 57 49 4e 41 50 49 2a 29 28 48 41  WORD(WINAPI*)(HA
11c3e 4e 44 4c 45 2c 4c 4f 4e 47 2c 50 4c 4f 4e 47 2c  NDLE,LONG,PLONG,
11c3f 20 5c 0a 20 20 20 20 20 20 20 20 44 57 4f 52 44   \.        DWORD
11c40 29 29 61 53 79 73 63 61 6c 6c 5b 35 32 5d 2e 70  ))aSyscall[52].p
11c41 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 21 53  Current)..#if !S
11c42 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20  QLITE_OS_WINRT. 
11c43 20 7b 20 22 53 6c 65 65 70 22 2c 20 20 20 20 20   { "Sleep",     
11c44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53                (S
11c45 59 53 43 41 4c 4c 29 53 6c 65 65 70 2c 20 20 20  YSCALL)Sleep,   
11c46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11c47 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22  0 },.#else.  { "
11c48 53 6c 65 65 70 22 2c 20 20 20 20 20 20 20 20 20  Sleep",         
11c49 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41            (SYSCA
11c4a 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20  LL)0,           
11c4b 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c              0 },
11c4c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65  .#endif..#define
11c4d 20 6f 73 53 6c 65 65 70 20 28 28 56 4f 49 44 28   osSleep ((VOID(
11c4e 57 49 4e 41 50 49 2a 29 28 44 57 4f 52 44 29 29  WINAPI*)(DWORD))
11c4f 61 53 79 73 63 61 6c 6c 5b 35 33 5d 2e 70 43 75  aSyscall[53].pCu
11c50 72 72 65 6e 74 29 0a 0a 20 20 7b 20 22 53 79 73  rrent)..  { "Sys
11c51 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d  temTimeToFileTim
11c52 65 22 2c 20 20 20 20 28 53 59 53 43 41 4c 4c 29  e",    (SYSCALL)
11c53 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65  SystemTimeToFile
11c54 54 69 6d 65 2c 20 20 20 20 30 20 7d 2c 0a 0a 23  Time,    0 },..#
11c55 64 65 66 69 6e 65 20 6f 73 53 79 73 74 65 6d 54  define osSystemT
11c56 69 6d 65 54 6f 46 69 6c 65 54 69 6d 65 20 28 28  imeToFileTime ((
11c57 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 43 4f  BOOL(WINAPI*)(CO
11c58 4e 53 54 20 53 59 53 54 45 4d 54 49 4d 45 2a 2c  NST SYSTEMTIME*,
11c59 20 5c 0a 20 20 20 20 20 20 20 20 4c 50 46 49 4c   \.        LPFIL
11c5a 45 54 49 4d 45 29 29 61 53 79 73 63 61 6c 6c 5b  ETIME))aSyscall[
11c5b 35 34 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23  54].pCurrent)..#
11c5c 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49  if !SQLITE_OS_WI
11c5d 4e 43 45 20 26 26 20 21 53 51 4c 49 54 45 5f 4f  NCE && !SQLITE_O
11c5e 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22 55 6e 6c  S_WINRT.  { "Unl
11c5f 6f 63 6b 46 69 6c 65 22 2c 20 20 20 20 20 20 20  ockFile",       
11c60 20 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29         (SYSCALL)
11c61 55 6e 6c 6f 63 6b 46 69 6c 65 2c 20 20 20 20 20  UnlockFile,     
11c62 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65           0 },.#e
11c63 6c 73 65 0a 20 20 7b 20 22 55 6e 6c 6f 63 6b 46  lse.  { "UnlockF
11c64 69 6c 65 22 2c 20 20 20 20 20 20 20 20 20 20 20  ile",           
11c65 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20     (SYSCALL)0,  
11c66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11c67 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66       0 },.#endif
11c68 0a 0a 23 69 66 6e 64 65 66 20 6f 73 55 6e 6c 6f  ..#ifndef osUnlo
11c69 63 6b 46 69 6c 65 0a 23 64 65 66 69 6e 65 20 6f  ckFile.#define o
11c6a 73 55 6e 6c 6f 63 6b 46 69 6c 65 20 28 28 42 4f  sUnlockFile ((BO
11c6b 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41 4e 44  OL(WINAPI*)(HAND
11c6c 4c 45 2c 44 57 4f 52 44 2c 44 57 4f 52 44 2c 44  LE,DWORD,DWORD,D
11c6d 57 4f 52 44 2c 20 5c 0a 20 20 20 20 20 20 20 20  WORD, \.        
11c6e 44 57 4f 52 44 29 29 61 53 79 73 63 61 6c 6c 5b  DWORD))aSyscall[
11c6f 35 35 5d 2e 70 43 75 72 72 65 6e 74 29 0a 23 65  55].pCurrent).#e
11c70 6e 64 69 66 0a 0a 23 69 66 20 21 53 51 4c 49 54  ndif..#if !SQLIT
11c71 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 7b 20 22  E_OS_WINCE.  { "
11c72 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 22 2c 20 20  UnlockFileEx",  
11c73 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41            (SYSCA
11c74 4c 4c 29 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 2c  LL)UnlockFileEx,
11c75 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c              0 },
11c76 0a 23 65 6c 73 65 0a 20 20 7b 20 22 55 6e 6c 6f  .#else.  { "Unlo
11c77 63 6b 46 69 6c 65 45 78 22 2c 20 20 20 20 20 20  ckFileEx",      
11c78 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30        (SYSCALL)0
11c79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
11c7a 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e          0 },.#en
11c7b 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 55  dif..#define osU
11c7c 6e 6c 6f 63 6b 46 69 6c 65 45 78 20 28 28 42 4f  nlockFileEx ((BO
11c7d 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41 4e 44  OL(WINAPI*)(HAND
11c7e 4c 45 2c 44 57 4f 52 44 2c 44 57 4f 52 44 2c 44  LE,DWORD,DWORD,D
11c7f 57 4f 52 44 2c 20 5c 0a 20 20 20 20 20 20 20 20  WORD, \.        
11c80 4c 50 4f 56 45 52 4c 41 50 50 45 44 29 29 61 53  LPOVERLAPPED))aS
11c81 79 73 63 61 6c 6c 5b 35 36 5d 2e 70 43 75 72 72  yscall[56].pCurr
11c82 65 6e 74 29 0a 0a 23 69 66 20 53 51 4c 49 54 45  ent)..#if SQLITE
11c83 5f 4f 53 5f 57 49 4e 43 45 20 7c 7c 20 21 64 65  _OS_WINCE || !de
11c84 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
11c85 54 5f 57 41 4c 29 0a 20 20 7b 20 22 55 6e 6d 61  T_WAL).  { "Unma
11c86 70 56 69 65 77 4f 66 46 69 6c 65 22 2c 20 20 20  pViewOfFile",   
11c87 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 55        (SYSCALL)U
11c88 6e 6d 61 70 56 69 65 77 4f 66 46 69 6c 65 2c 20  nmapViewOfFile, 
11c89 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6c          0 },.#el
11c8a 73 65 0a 20 20 7b 20 22 55 6e 6d 61 70 56 69 65  se.  { "UnmapVie
11c8b 77 4f 66 46 69 6c 65 22 2c 20 20 20 20 20 20 20  wOfFile",       
11c8c 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20    (SYSCALL)0,   
11c8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11c8e 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a      0 },.#endif.
11c8f 0a 23 64 65 66 69 6e 65 20 6f 73 55 6e 6d 61 70  .#define osUnmap
11c90 56 69 65 77 4f 66 46 69 6c 65 20 28 28 42 4f 4f  ViewOfFile ((BOO
11c91 4c 28 57 49 4e 41 50 49 2a 29 28 4c 50 43 56 4f  L(WINAPI*)(LPCVO
11c92 49 44 29 29 61 53 79 73 63 61 6c 6c 5b 35 37 5d  ID))aSyscall[57]
11c93 2e 70 43 75 72 72 65 6e 74 29 0a 0a 20 20 7b 20  .pCurrent)..  { 
11c94 22 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69  "WideCharToMulti
11c95 42 79 74 65 22 2c 20 20 20 20 20 28 53 59 53 43  Byte",     (SYSC
11c96 41 4c 4c 29 57 69 64 65 43 68 61 72 54 6f 4d 75  ALL)WideCharToMu
11c97 6c 74 69 42 79 74 65 2c 20 20 20 20 20 30 20 7d  ltiByte,     0 }
11c98 2c 0a 0a 23 64 65 66 69 6e 65 20 6f 73 57 69 64  ,..#define osWid
11c99 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65  eCharToMultiByte
11c9a 20 28 28 69 6e 74 28 57 49 4e 41 50 49 2a 29 28   ((int(WINAPI*)(
11c9b 55 49 4e 54 2c 44 57 4f 52 44 2c 4c 50 43 57 53  UINT,DWORD,LPCWS
11c9c 54 52 2c 69 6e 74 2c 4c 50 53 54 52 2c 69 6e 74  TR,int,LPSTR,int
11c9d 2c 20 5c 0a 20 20 20 20 20 20 20 20 4c 50 43 53  , \.        LPCS
11c9e 54 52 2c 4c 50 42 4f 4f 4c 29 29 61 53 79 73 63  TR,LPBOOL))aSysc
11c9f 61 6c 6c 5b 35 38 5d 2e 70 43 75 72 72 65 6e 74  all[58].pCurrent
11ca0 29 0a 0a 20 20 7b 20 22 57 72 69 74 65 46 69 6c  )..  { "WriteFil
11ca1 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  e",             
11ca2 20 20 28 53 59 53 43 41 4c 4c 29 57 72 69 74 65    (SYSCALL)Write
11ca3 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20  File,           
11ca4 20 20 20 20 30 20 7d 2c 0a 0a 23 64 65 66 69 6e      0 },..#defin
11ca5 65 20 6f 73 57 72 69 74 65 46 69 6c 65 20 28 28  e osWriteFile ((
11ca6 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41  BOOL(WINAPI*)(HA
11ca7 4e 44 4c 45 2c 4c 50 43 56 4f 49 44 2c 44 57 4f  NDLE,LPCVOID,DWO
11ca8 52 44 2c 4c 50 44 57 4f 52 44 2c 20 5c 0a 20 20  RD,LPDWORD, \.  
11ca9 20 20 20 20 20 20 4c 50 4f 56 45 52 4c 41 50 50        LPOVERLAPP
11caa 45 44 29 29 61 53 79 73 63 61 6c 6c 5b 35 39 5d  ED))aSyscall[59]
11cab 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20  .pCurrent)..#if 
11cac 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a  SQLITE_OS_WINRT.
11cad 20 20 7b 20 22 43 72 65 61 74 65 45 76 65 6e 74    { "CreateEvent
11cae 45 78 57 22 2c 20 20 20 20 20 20 20 20 20 20 28  ExW",          (
11caf 53 59 53 43 41 4c 4c 29 43 72 65 61 74 65 45 76  SYSCALL)CreateEv
11cb0 65 6e 74 45 78 57 2c 20 20 20 20 20 20 20 20 20  entExW,         
11cb1 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20   0 },.#else.  { 
11cb2 22 43 72 65 61 74 65 45 76 65 6e 74 45 78 57 22  "CreateEventExW"
11cb3 2c 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43  ,          (SYSC
11cb4 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20  ALL)0,          
11cb5 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d               0 }
11cb6 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e  ,.#endif..#defin
11cb7 65 20 6f 73 43 72 65 61 74 65 45 76 65 6e 74 45  e osCreateEventE
11cb8 78 57 20 28 28 48 41 4e 44 4c 45 28 57 49 4e 41  xW ((HANDLE(WINA
11cb9 50 49 2a 29 28 4c 50 53 45 43 55 52 49 54 59 5f  PI*)(LPSECURITY_
11cba 41 54 54 52 49 42 55 54 45 53 2c 4c 50 43 57 53  ATTRIBUTES,LPCWS
11cbb 54 52 2c 20 5c 0a 20 20 20 20 20 20 20 20 44 57  TR, \.        DW
11cbc 4f 52 44 2c 44 57 4f 52 44 29 29 61 53 79 73 63  ORD,DWORD))aSysc
11cbd 61 6c 6c 5b 36 30 5d 2e 70 43 75 72 72 65 6e 74  all[60].pCurrent
11cbe 29 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f  )..#if !SQLITE_O
11cbf 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22 57 61 69  S_WINRT.  { "Wai
11cc0 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74  tForSingleObject
11cc1 22 2c 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29  ",     (SYSCALL)
11cc2 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a  WaitForSingleObj
11cc3 65 63 74 2c 20 20 20 20 20 30 20 7d 2c 0a 23 65  ect,     0 },.#e
11cc4 6c 73 65 0a 20 20 7b 20 22 57 61 69 74 46 6f 72  lse.  { "WaitFor
11cc5 53 69 6e 67 6c 65 4f 62 6a 65 63 74 22 2c 20 20  SingleObject",  
11cc6 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20     (SYSCALL)0,  
11cc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11cc8 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66       0 },.#endif
11cc9 0a 0a 23 64 65 66 69 6e 65 20 6f 73 57 61 69 74  ..#define osWait
11cca 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74 20  ForSingleObject 
11ccb 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29  ((DWORD(WINAPI*)
11ccc 28 48 41 4e 44 4c 45 2c 20 5c 0a 20 20 20 20 20  (HANDLE, \.     
11ccd 20 20 20 44 57 4f 52 44 29 29 61 53 79 73 63 61     DWORD))aSysca
11cce 6c 6c 5b 36 31 5d 2e 70 43 75 72 72 65 6e 74 29  ll[61].pCurrent)
11ccf 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ..#if SQLITE_OS_
11cd0 57 49 4e 52 54 0a 20 20 7b 20 22 57 61 69 74 46  WINRT.  { "WaitF
11cd1 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74 45 78  orSingleObjectEx
11cd2 22 2c 20 20 20 28 53 59 53 43 41 4c 4c 29 57 61  ",   (SYSCALL)Wa
11cd3 69 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63  itForSingleObjec
11cd4 74 45 78 2c 20 20 20 30 20 7d 2c 0a 23 65 6c 73  tEx,   0 },.#els
11cd5 65 0a 20 20 7b 20 22 57 61 69 74 46 6f 72 53 69  e.  { "WaitForSi
11cd6 6e 67 6c 65 4f 62 6a 65 63 74 45 78 22 2c 20 20  ngleObjectEx",  
11cd7 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20   (SYSCALL)0,    
11cd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11cd9 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a     0 },.#endif..
11cda 23 64 65 66 69 6e 65 20 6f 73 57 61 69 74 46 6f  #define osWaitFo
11cdb 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74 45 78 20  rSingleObjectEx 
11cdc 28 28 44 57 4f 52 44 28 57 49 4e 41 50 49 2a 29  ((DWORD(WINAPI*)
11cdd 28 48 41 4e 44 4c 45 2c 44 57 4f 52 44 2c 20 5c  (HANDLE,DWORD, \
11cde 0a 20 20 20 20 20 20 20 20 42 4f 4f 4c 29 29 61  .        BOOL))a
11cdf 53 79 73 63 61 6c 6c 5b 36 32 5d 2e 70 43 75 72  Syscall[62].pCur
11ce0 72 65 6e 74 29 0a 0a 23 69 66 20 53 51 4c 49 54  rent)..#if SQLIT
11ce1 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22  E_OS_WINRT.  { "
11ce2 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 45 78  SetFilePointerEx
11ce3 22 2c 20 20 20 20 20 20 20 20 28 53 59 53 43 41  ",        (SYSCA
11ce4 4c 4c 29 53 65 74 46 69 6c 65 50 6f 69 6e 74 65  LL)SetFilePointe
11ce5 72 45 78 2c 20 20 20 20 20 20 20 20 30 20 7d 2c  rEx,        0 },
11ce6 0a 23 65 6c 73 65 0a 20 20 7b 20 22 53 65 74 46  .#else.  { "SetF
11ce7 69 6c 65 50 6f 69 6e 74 65 72 45 78 22 2c 20 20  ilePointerEx",  
11ce8 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 30        (SYSCALL)0
11ce9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
11cea 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e          0 },.#en
11ceb 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 53  dif..#define osS
11cec 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 45 78 20  etFilePointerEx 
11ced 28 28 42 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28  ((BOOL(WINAPI*)(
11cee 48 41 4e 44 4c 45 2c 4c 41 52 47 45 5f 49 4e 54  HANDLE,LARGE_INT
11cef 45 47 45 52 2c 20 5c 0a 20 20 20 20 20 20 20 20  EGER, \.        
11cf0 50 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 2c 44  PLARGE_INTEGER,D
11cf1 57 4f 52 44 29 29 61 53 79 73 63 61 6c 6c 5b 36  WORD))aSyscall[6
11cf2 33 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69  3].pCurrent)..#i
11cf3 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52  f SQLITE_OS_WINR
11cf4 54 0a 20 20 7b 20 22 47 65 74 46 69 6c 65 49 6e  T.  { "GetFileIn
11cf5 66 6f 72 6d 61 74 69 6f 6e 42 79 48 61 6e 64 6c  formationByHandl
11cf6 65 45 78 22 2c 20 28 53 59 53 43 41 4c 4c 29 47  eEx", (SYSCALL)G
11cf7 65 74 46 69 6c 65 49 6e 66 6f 72 6d 61 74 69 6f  etFileInformatio
11cf8 6e 42 79 48 61 6e 64 6c 65 45 78 2c 20 30 20 7d  nByHandleEx, 0 }
11cf9 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 47 65 74  ,.#else.  { "Get
11cfa 46 69 6c 65 49 6e 66 6f 72 6d 61 74 69 6f 6e 42  FileInformationB
11cfb 79 48 61 6e 64 6c 65 45 78 22 2c 20 28 53 59 53  yHandleEx", (SYS
11cfc 43 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20  CALL)0,         
11cfd 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65           0 },.#e
11cfe 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73  ndif..#define os
11cff 47 65 74 46 69 6c 65 49 6e 66 6f 72 6d 61 74 69  GetFileInformati
11d00 6f 6e 42 79 48 61 6e 64 6c 65 45 78 20 28 28 42  onByHandleEx ((B
11d01 4f 4f 4c 28 57 49 4e 41 50 49 2a 29 28 48 41 4e  OOL(WINAPI*)(HAN
11d02 44 4c 45 2c 20 5c 0a 20 20 20 20 20 20 20 20 46  DLE, \.        F
11d03 49 4c 45 5f 49 4e 46 4f 5f 42 59 5f 48 41 4e 44  ILE_INFO_BY_HAND
11d04 4c 45 5f 43 4c 41 53 53 2c 4c 50 56 4f 49 44 2c  LE_CLASS,LPVOID,
11d05 44 57 4f 52 44 29 29 61 53 79 73 63 61 6c 6c 5b  DWORD))aSyscall[
11d06 36 34 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23  64].pCurrent)..#
11d07 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
11d08 52 54 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  RT && !defined(S
11d09 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 29 0a  QLITE_OMIT_WAL).
11d0a 20 20 7b 20 22 4d 61 70 56 69 65 77 4f 66 46 69    { "MapViewOfFi
11d0b 6c 65 46 72 6f 6d 41 70 70 22 2c 20 20 20 20 28  leFromApp",    (
11d0c 53 59 53 43 41 4c 4c 29 4d 61 70 56 69 65 77 4f  SYSCALL)MapViewO
11d0d 66 46 69 6c 65 46 72 6f 6d 41 70 70 2c 20 20 20  fFileFromApp,   
11d0e 20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20   0 },.#else.  { 
11d0f 22 4d 61 70 56 69 65 77 4f 66 46 69 6c 65 46 72  "MapViewOfFileFr
11d10 6f 6d 41 70 70 22 2c 20 20 20 20 28 53 59 53 43  omApp",    (SYSC
11d11 41 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20  ALL)0,          
11d12 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d               0 }
11d13 2c 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e  ,.#endif..#defin
11d14 65 20 6f 73 4d 61 70 56 69 65 77 4f 66 46 69 6c  e osMapViewOfFil
11d15 65 46 72 6f 6d 41 70 70 20 28 28 4c 50 56 4f 49  eFromApp ((LPVOI
11d16 44 28 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c  D(WINAPI*)(HANDL
11d17 45 2c 55 4c 4f 4e 47 2c 55 4c 4f 4e 47 36 34 2c  E,ULONG,ULONG64,
11d18 20 5c 0a 20 20 20 20 20 20 20 20 53 49 5a 45 5f   \.        SIZE_
11d19 54 29 29 61 53 79 73 63 61 6c 6c 5b 36 35 5d 2e  T))aSyscall[65].
11d1a 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 53  pCurrent)..#if S
11d1b 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20  QLITE_OS_WINRT. 
11d1c 20 7b 20 22 43 72 65 61 74 65 46 69 6c 65 32 22   { "CreateFile2"
11d1d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53  ,             (S
11d1e 59 53 43 41 4c 4c 29 43 72 65 61 74 65 46 69 6c  YSCALL)CreateFil
11d1f 65 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  e2,             
11d20 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22  0 },.#else.  { "
11d21 43 72 65 61 74 65 46 69 6c 65 32 22 2c 20 20 20  CreateFile2",   
11d22 20 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41            (SYSCA
11d23 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20  LL)0,           
11d24 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c              0 },
11d25 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65  .#endif..#define
11d26 20 6f 73 43 72 65 61 74 65 46 69 6c 65 32 20 28   osCreateFile2 (
11d27 28 48 41 4e 44 4c 45 28 57 49 4e 41 50 49 2a 29  (HANDLE(WINAPI*)
11d28 28 4c 50 43 57 53 54 52 2c 44 57 4f 52 44 2c 44  (LPCWSTR,DWORD,D
11d29 57 4f 52 44 2c 44 57 4f 52 44 2c 20 5c 0a 20 20  WORD,DWORD, \.  
11d2a 20 20 20 20 20 20 4c 50 43 52 45 41 54 45 46 49        LPCREATEFI
11d2b 4c 45 32 5f 45 58 54 45 4e 44 45 44 5f 50 41 52  LE2_EXTENDED_PAR
11d2c 41 4d 45 54 45 52 53 29 29 61 53 79 73 63 61 6c  AMETERS))aSyscal
11d2d 6c 5b 36 36 5d 2e 70 43 75 72 72 65 6e 74 29 0a  l[66].pCurrent).
11d2e 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
11d2f 49 4e 52 54 20 26 26 20 21 64 65 66 69 6e 65 64  INRT && !defined
11d30 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41  (SQLITE_OMIT_LOA
11d31 44 5f 45 58 54 45 4e 53 49 4f 4e 29 0a 20 20 7b  D_EXTENSION).  {
11d32 20 22 4c 6f 61 64 50 61 63 6b 61 67 65 64 4c 69   "LoadPackagedLi
11d33 62 72 61 72 79 22 2c 20 20 20 20 20 28 53 59 53  brary",     (SYS
11d34 43 41 4c 4c 29 4c 6f 61 64 50 61 63 6b 61 67 65  CALL)LoadPackage
11d35 64 4c 69 62 72 61 72 79 2c 20 20 20 20 20 30 20  dLibrary,     0 
11d36 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 4c 6f  },.#else.  { "Lo
11d37 61 64 50 61 63 6b 61 67 65 64 4c 69 62 72 61 72  adPackagedLibrar
11d38 79 22 2c 20 20 20 20 20 28 53 59 53 43 41 4c 4c  y",     (SYSCALL
11d39 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  )0,             
11d3a 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 23            0 },.#
11d3b 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f  endif..#define o
11d3c 73 4c 6f 61 64 50 61 63 6b 61 67 65 64 4c 69 62  sLoadPackagedLib
11d3d 72 61 72 79 20 28 28 48 4d 4f 44 55 4c 45 28 57  rary ((HMODULE(W
11d3e 49 4e 41 50 49 2a 29 28 4c 50 43 57 53 54 52 2c  INAPI*)(LPCWSTR,
11d3f 20 5c 0a 20 20 20 20 20 20 20 20 44 57 4f 52 44   \.        DWORD
11d40 29 29 61 53 79 73 63 61 6c 6c 5b 36 37 5d 2e 70  ))aSyscall[67].p
11d41 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20 53 51  Current)..#if SQ
11d42 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20  LITE_OS_WINRT.  
11d43 7b 20 22 47 65 74 54 69 63 6b 43 6f 75 6e 74 36  { "GetTickCount6
11d44 34 22 2c 20 20 20 20 20 20 20 20 20 20 28 53 59  4",          (SY
11d45 53 43 41 4c 4c 29 47 65 74 54 69 63 6b 43 6f 75  SCALL)GetTickCou
11d46 6e 74 36 34 2c 20 20 20 20 20 20 20 20 20 20 30  nt64,          0
11d47 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22 47   },.#else.  { "G
11d48 65 74 54 69 63 6b 43 6f 75 6e 74 36 34 22 2c 20  etTickCount64", 
11d49 20 20 20 20 20 20 20 20 20 28 53 59 53 43 41 4c           (SYSCAL
11d4a 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  L)0,            
11d4b 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c 0a             0 },.
11d4c 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20  #endif..#define 
11d4d 6f 73 47 65 74 54 69 63 6b 43 6f 75 6e 74 36 34  osGetTickCount64
11d4e 20 28 28 55 4c 4f 4e 47 4c 4f 4e 47 28 57 49 4e   ((ULONGLONG(WIN
11d4f 41 50 49 2a 29 28 56 4f 49 44 29 29 61 53 79 73  API*)(VOID))aSys
11d50 63 61 6c 6c 5b 36 38 5d 2e 70 43 75 72 72 65 6e  call[68].pCurren
11d51 74 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  t)..#if SQLITE_O
11d52 53 5f 57 49 4e 52 54 0a 20 20 7b 20 22 47 65 74  S_WINRT.  { "Get
11d53 4e 61 74 69 76 65 53 79 73 74 65 6d 49 6e 66 6f  NativeSystemInfo
11d54 22 2c 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29  ",     (SYSCALL)
11d55 47 65 74 4e 61 74 69 76 65 53 79 73 74 65 6d 49  GetNativeSystemI
11d56 6e 66 6f 2c 20 20 20 20 20 30 20 7d 2c 0a 23 65  nfo,     0 },.#e
11d57 6c 73 65 0a 20 20 7b 20 22 47 65 74 4e 61 74 69  lse.  { "GetNati
11d58 76 65 53 79 73 74 65 6d 49 6e 66 6f 22 2c 20 20  veSystemInfo",  
11d59 20 20 20 28 53 59 53 43 41 4c 4c 29 30 2c 20 20     (SYSCALL)0,  
11d5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d5b 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66       0 },.#endif
11d5c 0a 0a 23 64 65 66 69 6e 65 20 6f 73 47 65 74 4e  ..#define osGetN
11d5d 61 74 69 76 65 53 79 73 74 65 6d 49 6e 66 6f 20  ativeSystemInfo 
11d5e 28 28 56 4f 49 44 28 57 49 4e 41 50 49 2a 29 28  ((VOID(WINAPI*)(
11d5f 20 5c 0a 20 20 20 20 20 20 20 20 4c 50 53 59 53   \.        LPSYS
11d60 54 45 4d 5f 49 4e 46 4f 29 29 61 53 79 73 63 61  TEM_INFO))aSysca
11d61 6c 6c 5b 36 39 5d 2e 70 43 75 72 72 65 6e 74 29  ll[69].pCurrent)
11d62 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
11d63 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 41  LITE_WIN32_HAS_A
11d64 4e 53 49 29 0a 20 20 7b 20 22 4f 75 74 70 75 74  NSI).  { "Output
11d65 44 65 62 75 67 53 74 72 69 6e 67 41 22 2c 20 20  DebugStringA",  
11d66 20 20 20 20 28 53 59 53 43 41 4c 4c 29 4f 75 74      (SYSCALL)Out
11d67 70 75 74 44 65 62 75 67 53 74 72 69 6e 67 41 2c  putDebugStringA,
11d68 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6c 73 65        0 },.#else
11d69 0a 20 20 7b 20 22 4f 75 74 70 75 74 44 65 62 75  .  { "OutputDebu
11d6a 67 53 74 72 69 6e 67 41 22 2c 20 20 20 20 20 20  gStringA",      
11d6b 28 53 59 53 43 41 4c 4c 29 30 2c 20 20 20 20 20  (SYSCALL)0,     
11d6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d6d 20 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 0a 23    0 },.#endif..#
11d6e 64 65 66 69 6e 65 20 6f 73 4f 75 74 70 75 74 44  define osOutputD
11d6f 65 62 75 67 53 74 72 69 6e 67 41 20 28 28 56 4f  ebugStringA ((VO
11d70 49 44 28 57 49 4e 41 50 49 2a 29 28 4c 50 43 53  ID(WINAPI*)(LPCS
11d71 54 52 29 29 61 53 79 73 63 61 6c 6c 5b 37 30 5d  TR))aSyscall[70]
11d72 2e 70 43 75 72 72 65 6e 74 29 0a 0a 23 69 66 20  .pCurrent)..#if 
11d73 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57  defined(SQLITE_W
11d74 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a 20  IN32_HAS_WIDE). 
11d75 20 7b 20 22 4f 75 74 70 75 74 44 65 62 75 67 53   { "OutputDebugS
11d76 74 72 69 6e 67 57 22 2c 20 20 20 20 20 20 28 53  tringW",      (S
11d77 59 53 43 41 4c 4c 29 4f 75 74 70 75 74 44 65 62  YSCALL)OutputDeb
11d78 75 67 53 74 72 69 6e 67 57 2c 20 20 20 20 20 20  ugStringW,      
11d79 30 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 7b 20 22  0 },.#else.  { "
11d7a 4f 75 74 70 75 74 44 65 62 75 67 53 74 72 69 6e  OutputDebugStrin
11d7b 67 57 22 2c 20 20 20 20 20 20 28 53 59 53 43 41  gW",      (SYSCA
11d7c 4c 4c 29 30 2c 20 20 20 20 20 20 20 20 20 20 20  LL)0,           
11d7d 20 20 20 20 20 20 20 20 20 20 20 20 30 20 7d 2c              0 },
11d7e 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65  .#endif..#define
11d7f 20 6f 73 4f 75 74 70 75 74 44 65 62 75 67 53 74   osOutputDebugSt
11d80 72 69 6e 67 57 20 28 28 56 4f 49 44 28 57 49 4e  ringW ((VOID(WIN
11d81 41 50 49 2a 29 28 4c 50 43 57 53 54 52 29 29 61  API*)(LPCWSTR))a
11d82 53 79 73 63 61 6c 6c 5b 37 31 5d 2e 70 43 75 72  Syscall[71].pCur
11d83 72 65 6e 74 29 0a 0a 20 20 7b 20 22 47 65 74 50  rent)..  { "GetP
11d84 72 6f 63 65 73 73 48 65 61 70 22 2c 20 20 20 20  rocessHeap",    
11d85 20 20 20 20 20 20 28 53 59 53 43 41 4c 4c 29 47        (SYSCALL)G
11d86 65 74 50 72 6f 63 65 73 73 48 65 61 70 2c 20 20  etProcessHeap,  
11d87 20 20 20 20 20 20 20 20 30 20 7d 2c 0a 0a 23 64          0 },..#d
11d88 65 66 69 6e 65 20 6f 73 47 65 74 50 72 6f 63 65  efine osGetProce
11d89 73 73 48 65 61 70 20 28 28 48 41 4e 44 4c 45 28  ssHeap ((HANDLE(
11d8a 57 49 4e 41 50 49 2a 29 28 56 4f 49 44 29 29 61  WINAPI*)(VOID))a
11d8b 53 79 73 63 61 6c 6c 5b 37 32 5d 2e 70 43 75 72  Syscall[72].pCur
11d8c 72 65 6e 74 29 0a 0a 23 69 66 20 53 51 4c 49 54  rent)..#if SQLIT
11d8d 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26 20 21 64  E_OS_WINRT && !d
11d8e 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
11d8f 49 54 5f 57 41 4c 29 0a 20 20 7b 20 22 43 72 65  IT_WAL).  { "Cre
11d90 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 46 72  ateFileMappingFr
11d91 6f 6d 41 70 70 22 2c 20 28 53 59 53 43 41 4c 4c  omApp", (SYSCALL
11d92 29 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69  )CreateFileMappi
11d93 6e 67 46 72 6f 6d 41 70 70 2c 20 30 20 7d 2c 0a  ngFromApp, 0 },.
11d94 23 65 6c 73 65 0a 20 20 7b 20 22 43 72 65 61 74  #else.  { "Creat
11d95 65 46 69 6c 65 4d 61 70 70 69 6e 67 46 72 6f 6d  eFileMappingFrom
11d96 41 70 70 22 2c 20 28 53 59 53 43 41 4c 4c 29 30  App", (SYSCALL)0
11d97 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
11d98 20 20 20 20 20 20 20 30 20 7d 2c 0a 23 65 6e 64         0 },.#end
11d99 69 66 0a 0a 23 64 65 66 69 6e 65 20 6f 73 43 72  if..#define osCr
11d9a 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 46  eateFileMappingF
11d9b 72 6f 6d 41 70 70 20 28 28 48 41 4e 44 4c 45 28  romApp ((HANDLE(
11d9c 57 49 4e 41 50 49 2a 29 28 48 41 4e 44 4c 45 2c  WINAPI*)(HANDLE,
11d9d 20 5c 0a 20 20 20 20 20 20 20 20 4c 50 53 45 43   \.        LPSEC
11d9e 55 52 49 54 59 5f 41 54 54 52 49 42 55 54 45 53  URITY_ATTRIBUTES
11d9f 2c 55 4c 4f 4e 47 2c 55 4c 4f 4e 47 36 34 2c 4c  ,ULONG,ULONG64,L
11da0 50 43 57 53 54 52 29 29 61 53 79 73 63 61 6c 6c  PCWSTR))aSyscall
11da1 5b 37 33 5d 2e 70 43 75 72 72 65 6e 74 29 0a 0a  [73].pCurrent)..
11da2 7d 3b 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65  }; /* End of the
11da3 20 6f 76 65 72 72 69 64 65 61 62 6c 65 20 73 79   overrideable sy
11da4 73 74 65 6d 20 63 61 6c 6c 73 20 2a 2f 0a 0a 2f  stem calls */../
11da5 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
11da6 20 78 53 65 74 53 79 73 74 65 6d 43 61 6c 6c 28   xSetSystemCall(
11da7 29 20 6d 65 74 68 6f 64 20 6f 66 20 73 71 6c 69  ) method of sqli
11da8 74 65 33 5f 76 66 73 20 66 6f 72 20 61 6c 6c 20  te3_vfs for all 
11da9 6f 66 20 74 68 65 0a 2a 2a 20 22 77 69 6e 33 32  of the.** "win32
11daa 22 20 56 46 53 65 73 2e 20 20 52 65 74 75 72 6e  " VFSes.  Return
11dab 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 70 6f 6e 20   SQLITE_OK opon 
11dac 73 75 63 63 65 73 73 66 75 6c 6c 79 20 75 70 64  successfully upd
11dad 61 74 69 6e 67 20 74 68 65 0a 2a 2a 20 73 79 73  ating the.** sys
11dae 74 65 6d 20 63 61 6c 6c 20 70 6f 69 6e 74 65 72  tem call pointer
11daf 2c 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 54 46  , or SQLITE_NOTF
11db0 4f 55 4e 44 20 69 66 20 74 68 65 72 65 20 69 73  OUND if there is
11db1 20 6e 6f 20 63 6f 6e 66 69 67 75 72 61 62 6c 65   no configurable
11db2 0a 2a 2a 20 73 79 73 74 65 6d 20 63 61 6c 6c 20  .** system call 
11db3 6e 61 6d 65 64 20 7a 4e 61 6d 65 2e 0a 2a 2f 0a  named zName..*/.
11db4 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 65  static int winSe
11db5 74 53 79 73 74 65 6d 43 61 6c 6c 28 0a 20 20 73  tSystemCall(.  s
11db6 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 4e 6f 74  qlite3_vfs *pNot
11db7 55 73 65 64 2c 20 20 20 20 20 20 20 20 2f 2a 20  Used,        /* 
11db8 54 68 65 20 56 46 53 20 70 6f 69 6e 74 65 72 2e  The VFS pointer.
11db9 20 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20    Not used */.  
11dba 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
11dbb 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
11dbc 20 4e 61 6d 65 20 6f 66 20 73 79 73 74 65 6d 20   Name of system 
11dbd 63 61 6c 6c 20 74 6f 20 6f 76 65 72 72 69 64 65  call to override
11dbe 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 79   */.  sqlite3_sy
11dbf 73 63 61 6c 6c 5f 70 74 72 20 70 4e 65 77 46 75  scall_ptr pNewFu
11dc0 6e 63 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74  nc  /* Pointer t
11dc1 6f 20 6e 65 77 20 73 79 73 74 65 6d 20 63 61 6c  o new system cal
11dc2 6c 20 76 61 6c 75 65 20 2a 2f 0a 29 7b 0a 20 20  l value */.){.  
11dc3 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a  unsigned int i;.
11dc4 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
11dc5 45 5f 4e 4f 54 46 4f 55 4e 44 3b 0a 0a 20 20 55  E_NOTFOUND;..  U
11dc6 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
11dc7 70 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28  pNotUsed);.  if(
11dc8 20 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20   zName==0 ){.   
11dc9 20 2f 2a 20 49 66 20 6e 6f 20 7a 4e 61 6d 65 20   /* If no zName 
11dca 69 73 20 67 69 76 65 6e 2c 20 72 65 73 74 6f 72  is given, restor
11dcb 65 20 61 6c 6c 20 73 79 73 74 65 6d 20 63 61 6c  e all system cal
11dcc 6c 73 20 74 6f 20 74 68 65 69 72 20 64 65 66 61  ls to their defa
11dcd 75 6c 74 0a 20 20 20 20 2a 2a 20 73 65 74 74 69  ult.    ** setti
11dce 6e 67 73 20 61 6e 64 20 72 65 74 75 72 6e 20 4e  ngs and return N
11dcf 55 4c 4c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  ULL.    */.    r
11dd0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
11dd1 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 69     for(i=0; i<si
11dd2 7a 65 6f 66 28 61 53 79 73 63 61 6c 6c 29 2f 73  zeof(aSyscall)/s
11dd3 69 7a 65 6f 66 28 61 53 79 73 63 61 6c 6c 5b 30  izeof(aSyscall[0
11dd4 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ]); i++){.      
11dd5 69 66 28 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e  if( aSyscall[i].
11dd6 70 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20  pDefault ){.    
11dd7 20 20 20 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e      aSyscall[i].
11dd8 70 43 75 72 72 65 6e 74 20 3d 20 61 53 79 73 63  pCurrent = aSysc
11dd9 61 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 6c 74 3b  all[i].pDefault;
11dda 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
11ddb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49   }else{.    /* I
11ddc 66 20 7a 4e 61 6d 65 20 69 73 20 73 70 65 63 69  f zName is speci
11ddd 66 69 65 64 2c 20 6f 70 65 72 61 74 65 20 6f 6e  fied, operate on
11dde 20 6f 6e 6c 79 20 74 68 65 20 6f 6e 65 20 73 79   only the one sy
11ddf 73 74 65 6d 20 63 61 6c 6c 0a 20 20 20 20 2a 2a  stem call.    **
11de0 20 73 70 65 63 69 66 69 65 64 2e 0a 20 20 20 20   specified..    
11de1 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  */.    for(i=0; 
11de2 69 3c 73 69 7a 65 6f 66 28 61 53 79 73 63 61 6c  i<sizeof(aSyscal
11de3 6c 29 2f 73 69 7a 65 6f 66 28 61 53 79 73 63 61  l)/sizeof(aSysca
11de4 6c 6c 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20  ll[0]); i++){.  
11de5 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a      if( strcmp(z
11de6 4e 61 6d 65 2c 20 61 53 79 73 63 61 6c 6c 5b 69  Name, aSyscall[i
11de7 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20  ].zName)==0 ){. 
11de8 20 20 20 20 20 20 20 69 66 28 20 61 53 79 73 63         if( aSysc
11de9 61 6c 6c 5b 69 5d 2e 70 44 65 66 61 75 6c 74 3d  all[i].pDefault=
11dea 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
11deb 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 44 65 66  aSyscall[i].pDef
11dec 61 75 6c 74 20 3d 20 61 53 79 73 63 61 6c 6c 5b  ault = aSyscall[
11ded 69 5d 2e 70 43 75 72 72 65 6e 74 3b 0a 20 20 20  i].pCurrent;.   
11dee 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72       }.        r
11def 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
11df0 20 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 46         if( pNewF
11df1 75 6e 63 3d 3d 30 20 29 20 70 4e 65 77 46 75 6e  unc==0 ) pNewFun
11df2 63 20 3d 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e  c = aSyscall[i].
11df3 70 44 65 66 61 75 6c 74 3b 0a 20 20 20 20 20 20  pDefault;.      
11df4 20 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 43    aSyscall[i].pC
11df5 75 72 72 65 6e 74 20 3d 20 70 4e 65 77 46 75 6e  urrent = pNewFun
11df6 63 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  c;.        break
11df7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
11df8 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
11df9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
11dfa 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20   the value of a 
11dfb 73 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 52 65  system call.  Re
11dfc 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 7a 4e 61  turn NULL if zNa
11dfd 6d 65 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 72  me is not a.** r
11dfe 65 63 6f 67 6e 69 7a 65 64 20 73 79 73 74 65 6d  ecognized system
11dff 20 63 61 6c 6c 20 6e 61 6d 65 2e 20 20 4e 55 4c   call name.  NUL
11e00 4c 20 69 73 20 61 6c 73 6f 20 72 65 74 75 72 6e  L is also return
11e01 65 64 20 69 66 20 74 68 65 20 73 79 73 74 65 6d  ed if the system
11e02 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 63 75 72 72   call.** is curr
11e03 65 6e 74 6c 79 20 75 6e 64 65 66 69 6e 65 64 2e  ently undefined.
11e04 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74  .*/.static sqlit
11e05 65 33 5f 73 79 73 63 61 6c 6c 5f 70 74 72 20 77  e3_syscall_ptr w
11e06 69 6e 47 65 74 53 79 73 74 65 6d 43 61 6c 6c 28  inGetSystemCall(
11e07 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
11e08 70 4e 6f 74 55 73 65 64 2c 0a 20 20 63 6f 6e 73  pNotUsed,.  cons
11e09 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 0a 29 7b  t char *zName.){
11e0a 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
11e0b 69 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  i;..  UNUSED_PAR
11e0c 41 4d 45 54 45 52 28 70 4e 6f 74 55 73 65 64 29  AMETER(pNotUsed)
11e0d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73  ;.  for(i=0; i<s
11e0e 69 7a 65 6f 66 28 61 53 79 73 63 61 6c 6c 29 2f  izeof(aSyscall)/
11e0f 73 69 7a 65 6f 66 28 61 53 79 73 63 61 6c 6c 5b  sizeof(aSyscall[
11e10 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  0]); i++){.    i
11e11 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c  f( strcmp(zName,
11e12 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e 61   aSyscall[i].zNa
11e13 6d 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  me)==0 ) return 
11e14 61 53 79 73 63 61 6c 6c 5b 69 5d 2e 70 43 75 72  aSyscall[i].pCur
11e15 72 65 6e 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rent;.  }.  retu
11e16 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn 0;.}../*.** R
11e17 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f  eturn the name o
11e18 66 20 74 68 65 20 66 69 72 73 74 20 73 79 73 74  f the first syst
11e19 65 6d 20 63 61 6c 6c 20 61 66 74 65 72 20 7a 4e  em call after zN
11e1a 61 6d 65 2e 20 20 49 66 20 7a 4e 61 6d 65 3d 3d  ame.  If zName==
11e1b 4e 55 4c 4c 0a 2a 2a 20 74 68 65 6e 20 72 65 74  NULL.** then ret
11e1c 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  urn the name of 
11e1d 74 68 65 20 66 69 72 73 74 20 73 79 73 74 65 6d  the first system
11e1e 20 63 61 6c 6c 2e 20 20 52 65 74 75 72 6e 20 4e   call.  Return N
11e1f 55 4c 4c 20 69 66 20 7a 4e 61 6d 65 0a 2a 2a 20  ULL if zName.** 
11e20 69 73 20 74 68 65 20 6c 61 73 74 20 73 79 73 74  is the last syst
11e21 65 6d 20 63 61 6c 6c 20 6f 72 20 69 66 20 7a 4e  em call or if zN
11e22 61 6d 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6e  ame is not the n
11e23 61 6d 65 20 6f 66 20 61 20 76 61 6c 69 64 0a 2a  ame of a valid.*
11e24 2a 20 73 79 73 74 65 6d 20 63 61 6c 6c 2e 0a 2a  * system call..*
11e25 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
11e26 68 61 72 20 2a 77 69 6e 4e 65 78 74 53 79 73 74  har *winNextSyst
11e27 65 6d 43 61 6c 6c 28 73 71 6c 69 74 65 33 5f 76  emCall(sqlite3_v
11e28 66 73 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61  fs *p, const cha
11e29 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e 74  r *zName){.  int
11e2a 20 69 20 3d 20 2d 31 3b 0a 0a 20 20 55 4e 55 53   i = -1;..  UNUS
11e2b 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29 3b  ED_PARAMETER(p);
11e2c 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a  .  if( zName ){.
11e2d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41      for(i=0; i<A
11e2e 72 72 61 79 53 69 7a 65 28 61 53 79 73 63 61 6c  rraySize(aSyscal
11e2f 6c 29 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  l)-1; i++){.    
11e30 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 4e 61    if( strcmp(zNa
11e31 6d 65 2c 20 61 53 79 73 63 61 6c 6c 5b 69 5d 2e  me, aSyscall[i].
11e32 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61  zName)==0 ) brea
11e33 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66  k;.    }.  }.  f
11e34 6f 72 28 69 2b 2b 3b 20 69 3c 41 72 72 61 79 53  or(i++; i<ArrayS
11e35 69 7a 65 28 61 53 79 73 63 61 6c 6c 29 3b 20 69  ize(aSyscall); i
11e36 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 53 79  ++){.    if( aSy
11e37 73 63 61 6c 6c 5b 69 5d 2e 70 43 75 72 72 65 6e  scall[i].pCurren
11e38 74 21 3d 30 20 29 20 72 65 74 75 72 6e 20 61 53  t!=0 ) return aS
11e39 79 73 63 61 6c 6c 5b 69 5d 2e 7a 4e 61 6d 65 3b  yscall[i].zName;
11e3a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
11e3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
11e3c 75 6e 63 74 69 6f 6e 20 6f 75 74 70 75 74 73 20  unction outputs 
11e3d 74 68 65 20 73 70 65 63 69 66 69 65 64 20 28 41  the specified (A
11e3e 4e 53 49 29 20 73 74 72 69 6e 67 20 74 6f 20 74  NSI) string to t
11e3f 68 65 20 57 69 6e 33 32 20 64 65 62 75 67 67 65  he Win32 debugge
11e40 72 0a 2a 2a 20 28 69 66 20 61 76 61 69 6c 61 62  r.** (if availab
11e41 6c 65 29 2e 0a 2a 2f 0a 0a 53 51 4c 49 54 45 5f  le)..*/..SQLITE_
11e42 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
11e43 5f 77 69 6e 33 32 5f 77 72 69 74 65 5f 64 65 62  _win32_write_deb
11e44 75 67 28 63 68 61 72 20 2a 7a 42 75 66 2c 20 69  ug(char *zBuf, i
11e45 6e 74 20 6e 42 75 66 29 7b 0a 20 20 63 68 61 72  nt nBuf){.  char
11e46 20 7a 44 62 67 42 75 66 5b 53 51 4c 49 54 45 5f   zDbgBuf[SQLITE_
11e47 57 49 4e 33 32 5f 44 42 47 5f 42 55 46 5f 53 49  WIN32_DBG_BUF_SI
11e48 5a 45 5d 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 20  ZE];.  int nMin 
11e49 3d 20 4d 49 4e 28 6e 42 75 66 2c 20 28 53 51 4c  = MIN(nBuf, (SQL
11e4a 49 54 45 5f 57 49 4e 33 32 5f 44 42 47 5f 42 55  ITE_WIN32_DBG_BU
11e4b 46 5f 53 49 5a 45 20 2d 20 31 29 29 3b 20 2f 2a  F_SIZE - 1)); /*
11e4c 20 6d 61 79 20 62 65 20 6e 65 67 61 74 69 76 65   may be negative
11e4d 2e 20 2a 2f 0a 20 20 69 66 28 20 6e 4d 69 6e 3c  . */.  if( nMin<
11e4e 2d 31 20 29 20 6e 4d 69 6e 20 3d 20 2d 31 3b 20  -1 ) nMin = -1; 
11e4f 2f 2a 20 61 6c 6c 20 6e 65 67 61 74 69 76 65 20  /* all negative 
11e50 76 61 6c 75 65 73 20 62 65 63 6f 6d 65 20 2d 31  values become -1
11e51 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e  . */.  assert( n
11e52 4d 69 6e 3d 3d 2d 31 20 7c 7c 20 6e 4d 69 6e 3d  Min==-1 || nMin=
11e53 3d 30 20 7c 7c 20 6e 4d 69 6e 3c 53 51 4c 49 54  =0 || nMin<SQLIT
11e54 45 5f 57 49 4e 33 32 5f 44 42 47 5f 42 55 46 5f  E_WIN32_DBG_BUF_
11e55 53 49 5a 45 20 29 3b 0a 23 69 66 20 64 65 66 69  SIZE );.#if defi
11e56 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32  ned(SQLITE_WIN32
11e57 5f 48 41 53 5f 41 4e 53 49 29 0a 20 20 69 66 28  _HAS_ANSI).  if(
11e58 20 6e 4d 69 6e 3e 30 20 29 7b 0a 20 20 20 20 6d   nMin>0 ){.    m
11e59 65 6d 73 65 74 28 7a 44 62 67 42 75 66 2c 20 30  emset(zDbgBuf, 0
11e5a 2c 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 44  , SQLITE_WIN32_D
11e5b 42 47 5f 42 55 46 5f 53 49 5a 45 29 3b 0a 20 20  BG_BUF_SIZE);.  
11e5c 20 20 6d 65 6d 63 70 79 28 7a 44 62 67 42 75 66    memcpy(zDbgBuf
11e5d 2c 20 7a 42 75 66 2c 20 6e 4d 69 6e 29 3b 0a 20  , zBuf, nMin);. 
11e5e 20 20 20 6f 73 4f 75 74 70 75 74 44 65 62 75 67     osOutputDebug
11e5f 53 74 72 69 6e 67 41 28 7a 44 62 67 42 75 66 29  StringA(zDbgBuf)
11e60 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f  ;.  }else{.    o
11e61 73 4f 75 74 70 75 74 44 65 62 75 67 53 74 72 69  sOutputDebugStri
11e62 6e 67 41 28 7a 42 75 66 29 3b 0a 20 20 7d 0a 23  ngA(zBuf);.  }.#
11e63 65 6c 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  elif defined(SQL
11e64 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 57 49  ITE_WIN32_HAS_WI
11e65 44 45 29 0a 20 20 6d 65 6d 73 65 74 28 7a 44 62  DE).  memset(zDb
11e66 67 42 75 66 2c 20 30 2c 20 53 51 4c 49 54 45 5f  gBuf, 0, SQLITE_
11e67 57 49 4e 33 32 5f 44 42 47 5f 42 55 46 5f 53 49  WIN32_DBG_BUF_SI
11e68 5a 45 29 3b 0a 20 20 69 66 20 28 20 6f 73 4d 75  ZE);.  if ( osMu
11e69 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 61  ltiByteToWideCha
11e6a 72 28 0a 20 20 20 20 20 20 20 20 20 20 6f 73 41  r(.          osA
11e6b 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29  reFileApisANSI()
11e6c 20 3f 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f   ? CP_ACP : CP_O
11e6d 45 4d 43 50 2c 20 30 2c 20 7a 42 75 66 2c 0a 20  EMCP, 0, zBuf,. 
11e6e 20 20 20 20 20 20 20 20 20 6e 4d 69 6e 2c 20 28           nMin, (
11e6f 4c 50 57 53 54 52 29 7a 44 62 67 42 75 66 2c 20  LPWSTR)zDbgBuf, 
11e70 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 44 42 47  SQLITE_WIN32_DBG
11e71 5f 42 55 46 5f 53 49 5a 45 2f 73 69 7a 65 6f 66  _BUF_SIZE/sizeof
11e72 28 57 43 48 41 52 29 29 3c 3d 30 20 29 7b 0a 20  (WCHAR))<=0 ){. 
11e73 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
11e74 20 6f 73 4f 75 74 70 75 74 44 65 62 75 67 53 74   osOutputDebugSt
11e75 72 69 6e 67 57 28 28 4c 50 43 57 53 54 52 29 7a  ringW((LPCWSTR)z
11e76 44 62 67 42 75 66 29 3b 0a 23 65 6c 73 65 0a 20  DbgBuf);.#else. 
11e77 20 69 66 28 20 6e 4d 69 6e 3e 30 20 29 7b 0a 20   if( nMin>0 ){. 
11e78 20 20 20 6d 65 6d 73 65 74 28 7a 44 62 67 42 75     memset(zDbgBu
11e79 66 2c 20 30 2c 20 53 51 4c 49 54 45 5f 57 49 4e  f, 0, SQLITE_WIN
11e7a 33 32 5f 44 42 47 5f 42 55 46 5f 53 49 5a 45 29  32_DBG_BUF_SIZE)
11e7b 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 44 62  ;.    memcpy(zDb
11e7c 67 42 75 66 2c 20 7a 42 75 66 2c 20 6e 4d 69 6e  gBuf, zBuf, nMin
11e7d 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73  );.    fprintf(s
11e7e 74 64 65 72 72 2c 20 22 25 73 22 2c 20 7a 44 62  tderr, "%s", zDb
11e7f 67 42 75 66 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  gBuf);.  }else{.
11e80 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65      fprintf(stde
11e81 72 72 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b  rr, "%s", zBuf);
11e82 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
11e83 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
11e84 6e 67 20 72 6f 75 74 69 6e 65 20 73 75 73 70 65  ng routine suspe
11e85 6e 64 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  nds the current 
11e86 74 68 72 65 61 64 20 66 6f 72 20 61 74 20 6c 65  thread for at le
11e87 61 73 74 20 6d 73 0a 2a 2a 20 6d 69 6c 6c 69 73  ast ms.** millis
11e88 65 63 6f 6e 64 73 2e 20 20 54 68 69 73 20 69 73  econds.  This is
11e89 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74   equivalent to t
11e8a 68 65 20 57 69 6e 33 32 20 53 6c 65 65 70 28 29  he Win32 Sleep()
11e8b 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23   interface..*/.#
11e8c 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
11e8d 52 54 0a 73 74 61 74 69 63 20 48 41 4e 44 4c 45  RT.static HANDLE
11e8e 20 73 6c 65 65 70 4f 62 6a 20 3d 20 4e 55 4c 4c   sleepObj = NULL
11e8f 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45  ;.#endif..SQLITE
11e90 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
11e91 33 5f 77 69 6e 33 32 5f 73 6c 65 65 70 28 44 57  3_win32_sleep(DW
11e92 4f 52 44 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73  ORD milliseconds
11e93 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  ){.#if SQLITE_OS
11e94 5f 57 49 4e 52 54 0a 20 20 69 66 20 28 20 73 6c  _WINRT.  if ( sl
11e95 65 65 70 4f 62 6a 3d 3d 4e 55 4c 4c 20 29 7b 0a  eepObj==NULL ){.
11e96 20 20 20 20 73 6c 65 65 70 4f 62 6a 20 3d 20 6f      sleepObj = o
11e97 73 43 72 65 61 74 65 45 76 65 6e 74 45 78 57 28  sCreateEventExW(
11e98 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 43 52 45 41  NULL, NULL, CREA
11e99 54 45 5f 45 56 45 4e 54 5f 4d 41 4e 55 41 4c 5f  TE_EVENT_MANUAL_
11e9a 52 45 53 45 54 2c 0a 20 20 20 20 20 20 20 20 20  RESET,.         
11e9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11e9c 20 20 20 20 20 20 20 53 59 4e 43 48 52 4f 4e 49         SYNCHRONI
11e9d 5a 45 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  ZE);.  }.  asser
11e9e 74 28 20 73 6c 65 65 70 4f 62 6a 21 3d 4e 55 4c  t( sleepObj!=NUL
11e9f 4c 20 29 3b 0a 20 20 6f 73 57 61 69 74 46 6f 72  L );.  osWaitFor
11ea0 53 69 6e 67 6c 65 4f 62 6a 65 63 74 45 78 28 73  SingleObjectEx(s
11ea1 6c 65 65 70 4f 62 6a 2c 20 6d 69 6c 6c 69 73 65  leepObj, millise
11ea2 63 6f 6e 64 73 2c 20 46 41 4c 53 45 29 3b 0a 23  conds, FALSE);.#
11ea3 65 6c 73 65 0a 20 20 6f 73 53 6c 65 65 70 28 6d  else.  osSleep(m
11ea4 69 6c 6c 69 73 65 63 6f 6e 64 73 29 3b 0a 23 65  illiseconds);.#e
11ea5 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ndif.}../*.** Re
11ea6 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a  turn true (non-z
11ea7 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 20 72  ero) if we are r
11ea8 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 69 6e  unning under Win
11ea9 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e 58 50  NT, Win2K, WinXP
11eaa 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e 20 20  ,.** or WinCE.  
11eab 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 7a 65  Return false (ze
11eac 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c 20 57  ro) for Win95, W
11ead 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a  in98, or WinME..
11eae 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 61 6e  **.** Here is an
11eaf 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f 62 73   interesting obs
11eb0 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35  ervation:  Win95
11eb1 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e  , Win98, and Win
11eb2 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 20 4c  ME lack.** the L
11eb3 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e  ockFileEx() API.
11eb4 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 74 69    But we can sti
11eb5 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69  ll statically li
11eb6 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 74 0a  nk against that.
11eb7 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 20 61  ** API as long a
11eb8 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20  s we don't call 
11eb9 69 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20  it when running 
11eba 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 20  Win95/98/ME.  A 
11ebb 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 20  call to.** this 
11ebc 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
11ebd 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20  to determine if 
11ebe 74 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e 39  the host is Win9
11ebf 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69  5/98/ME or.** Wi
11ec0 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 61  nNT/2K/XP so tha
11ec1 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77  t we will know w
11ec2 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 65  hether or not we
11ec3 20 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c 6c   can safely call
11ec4 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65  .** the LockFile
11ec5 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 69 66  Ex() API..*/.#if
11ec6 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
11ec7 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49   || SQLITE_OS_WI
11ec8 4e 52 54 0a 23 20 64 65 66 69 6e 65 20 69 73 4e  NRT.# define isN
11ec9 54 28 29 20 20 28 31 29 0a 23 65 6c 69 66 20 21  T()  (1).#elif !
11eca 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 57  defined(SQLITE_W
11ecb 49 4e 33 32 5f 48 41 53 5f 57 49 44 45 29 0a 23  IN32_HAS_WIDE).#
11ecc 20 64 65 66 69 6e 65 20 69 73 4e 54 28 29 20 20   define isNT()  
11ecd 28 30 29 0a 23 65 6c 73 65 0a 20 20 73 74 61 74  (0).#else.  stat
11ece 69 63 20 69 6e 74 20 69 73 4e 54 28 76 6f 69 64  ic int isNT(void
11ecf 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
11ed0 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 30 20 29 7b  e3_os_type==0 ){
11ed1 0a 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f 4e  .      OSVERSION
11ed2 49 4e 46 4f 41 20 73 49 6e 66 6f 3b 0a 20 20 20  INFOA sInfo;.   
11ed3 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72     sInfo.dwOSVer
11ed4 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73  sionInfoSize = s
11ed5 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20  izeof(sInfo);.  
11ed6 20 20 20 20 6f 73 47 65 74 56 65 72 73 69 6f 6e      osGetVersion
11ed7 45 78 41 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20  ExA(&sInfo);.   
11ed8 20 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79     sqlite3_os_ty
11ed9 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61  pe = sInfo.dwPla
11eda 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41  tformId==VER_PLA
11edb 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f  TFORM_WIN32_NT ?
11edc 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20   2 : 1;.    }.  
11edd 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
11ede 5f 6f 73 5f 74 79 70 65 3d 3d 32 3b 0a 20 20 7d  _os_type==2;.  }
11edf 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
11ee0 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 4d 41 4c  SQLITE_WIN32_MAL
11ee1 4c 4f 43 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  LOC./*.** Alloca
11ee2 74 65 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d  te nBytes of mem
11ee3 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ory..*/.static v
11ee4 6f 69 64 20 2a 77 69 6e 4d 65 6d 4d 61 6c 6c 6f  oid *winMemMallo
11ee5 63 28 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20  c(int nBytes){. 
11ee6 20 48 41 4e 44 4c 45 20 68 48 65 61 70 3b 0a 20   HANDLE hHeap;. 
11ee7 20 76 6f 69 64 20 2a 70 3b 0a 0a 20 20 77 69 6e   void *p;..  win
11ee8 4d 65 6d 41 73 73 65 72 74 4d 61 67 69 63 28 29  MemAssertMagic()
11ee9 3b 0a 20 20 68 48 65 61 70 20 3d 20 77 69 6e 4d  ;.  hHeap = winM
11eea 65 6d 47 65 74 48 65 61 70 28 29 3b 0a 20 20 61  emGetHeap();.  a
11eeb 73 73 65 72 74 28 20 68 48 65 61 70 21 3d 30 20  ssert( hHeap!=0 
11eec 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 48 65  );.  assert( hHe
11eed 61 70 21 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44  ap!=INVALID_HAND
11eee 4c 45 5f 56 41 4c 55 45 20 29 3b 0a 23 69 66 20  LE_VALUE );.#if 
11eef 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54  !SQLITE_OS_WINRT
11ef0 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49   && defined(SQLI
11ef1 54 45 5f 57 49 4e 33 32 5f 4d 41 4c 4c 4f 43 5f  TE_WIN32_MALLOC_
11ef2 56 41 4c 49 44 41 54 45 29 0a 20 20 61 73 73 65  VALIDATE).  asse
11ef3 72 74 20 28 20 6f 73 48 65 61 70 56 61 6c 69 64  rt ( osHeapValid
11ef4 61 74 65 28 68 48 65 61 70 2c 20 53 51 4c 49 54  ate(hHeap, SQLIT
11ef5 45 5f 57 49 4e 33 32 5f 48 45 41 50 5f 46 4c 41  E_WIN32_HEAP_FLA
11ef6 47 53 2c 20 4e 55 4c 4c 29 20 29 3b 0a 23 65 6e  GS, NULL) );.#en
11ef7 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 6e 42  dif.  assert( nB
11ef8 79 74 65 73 3e 3d 30 20 29 3b 0a 20 20 70 20 3d  ytes>=0 );.  p =
11ef9 20 6f 73 48 65 61 70 41 6c 6c 6f 63 28 68 48 65   osHeapAlloc(hHe
11efa 61 70 2c 20 53 51 4c 49 54 45 5f 57 49 4e 33 32  ap, SQLITE_WIN32
11efb 5f 48 45 41 50 5f 46 4c 41 47 53 2c 20 28 53 49  _HEAP_FLAGS, (SI
11efc 5a 45 5f 54 29 6e 42 79 74 65 73 29 3b 0a 20 20  ZE_T)nBytes);.  
11efd 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 73 71  if( !p ){.    sq
11efe 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54 45  lite3_log(SQLITE
11eff 5f 4e 4f 4d 45 4d 2c 20 22 66 61 69 6c 65 64 20  _NOMEM, "failed 
11f00 74 6f 20 48 65 61 70 41 6c 6c 6f 63 20 25 75 20  to HeapAlloc %u 
11f01 62 79 74 65 73 20 28 25 64 29 2c 20 68 65 61 70  bytes (%d), heap
11f02 3d 25 70 22 2c 0a 20 20 20 20 20 20 20 20 20 20  =%p",.          
11f03 20 20 20 20 20 20 6e 42 79 74 65 73 2c 20 6f 73        nBytes, os
11f04 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 2c 20  GetLastError(), 
11f05 28 76 6f 69 64 2a 29 68 48 65 61 70 29 3b 0a 20  (void*)hHeap);. 
11f06 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
11f07 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d  ../*.** Free mem
11f08 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ory..*/.static v
11f09 6f 69 64 20 77 69 6e 4d 65 6d 46 72 65 65 28 76  oid winMemFree(v
11f0a 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20  oid *pPrior){.  
11f0b 48 41 4e 44 4c 45 20 68 48 65 61 70 3b 0a 0a 20  HANDLE hHeap;.. 
11f0c 20 77 69 6e 4d 65 6d 41 73 73 65 72 74 4d 61 67   winMemAssertMag
11f0d 69 63 28 29 3b 0a 20 20 68 48 65 61 70 20 3d 20  ic();.  hHeap = 
11f0e 77 69 6e 4d 65 6d 47 65 74 48 65 61 70 28 29 3b  winMemGetHeap();
11f0f 0a 20 20 61 73 73 65 72 74 28 20 68 48 65 61 70  .  assert( hHeap
11f10 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
11f11 20 68 48 65 61 70 21 3d 49 4e 56 41 4c 49 44 5f   hHeap!=INVALID_
11f12 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29 3b 0a  HANDLE_VALUE );.
11f13 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57  #if !SQLITE_OS_W
11f14 49 4e 52 54 20 26 26 20 64 65 66 69 6e 65 64 28  INRT && defined(
11f15 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 4d 41 4c  SQLITE_WIN32_MAL
11f16 4c 4f 43 5f 56 41 4c 49 44 41 54 45 29 0a 20 20  LOC_VALIDATE).  
11f17 61 73 73 65 72 74 20 28 20 6f 73 48 65 61 70 56  assert ( osHeapV
11f18 61 6c 69 64 61 74 65 28 68 48 65 61 70 2c 20 53  alidate(hHeap, S
11f19 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41 50  QLITE_WIN32_HEAP
11f1a 5f 46 4c 41 47 53 2c 20 70 50 72 69 6f 72 29 20  _FLAGS, pPrior) 
11f1b 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20  );.#endif.  if( 
11f1c 21 70 50 72 69 6f 72 20 29 20 72 65 74 75 72 6e  !pPrior ) return
11f1d 3b 20 2f 2a 20 50 61 73 73 69 6e 67 20 4e 55 4c  ; /* Passing NUL
11f1e 4c 20 74 6f 20 48 65 61 70 46 72 65 65 20 69 73  L to HeapFree is
11f1f 20 75 6e 64 65 66 69 6e 65 64 2e 20 2a 2f 0a 20   undefined. */. 
11f20 20 69 66 28 20 21 6f 73 48 65 61 70 46 72 65 65   if( !osHeapFree
11f21 28 68 48 65 61 70 2c 20 53 51 4c 49 54 45 5f 57  (hHeap, SQLITE_W
11f22 49 4e 33 32 5f 48 45 41 50 5f 46 4c 41 47 53 2c  IN32_HEAP_FLAGS,
11f23 20 70 50 72 69 6f 72 29 20 29 7b 0a 20 20 20 20   pPrior) ){.    
11f24 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49  sqlite3_log(SQLI
11f25 54 45 5f 4e 4f 4d 45 4d 2c 20 22 66 61 69 6c 65  TE_NOMEM, "faile
11f26 64 20 74 6f 20 48 65 61 70 46 72 65 65 20 62 6c  d to HeapFree bl
11f27 6f 63 6b 20 25 70 20 28 25 64 29 2c 20 68 65 61  ock %p (%d), hea
11f28 70 3d 25 70 22 2c 0a 20 20 20 20 20 20 20 20 20  p=%p",.         
11f29 20 20 20 20 20 20 20 70 50 72 69 6f 72 2c 20 6f         pPrior, o
11f2a 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 2c  sGetLastError(),
11f2b 20 28 76 6f 69 64 2a 29 68 48 65 61 70 29 3b 0a   (void*)hHeap);.
11f2c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61    }.}../*.** Cha
11f2d 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20  nge the size of 
11f2e 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f  an existing memo
11f2f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f  ry allocation.*/
11f30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 77 69  .static void *wi
11f31 6e 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64  nMemRealloc(void
11f32 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42   *pPrior, int nB
11f33 79 74 65 73 29 7b 0a 20 20 48 41 4e 44 4c 45 20  ytes){.  HANDLE 
11f34 68 48 65 61 70 3b 0a 20 20 76 6f 69 64 20 2a 70  hHeap;.  void *p
11f35 3b 0a 0a 20 20 77 69 6e 4d 65 6d 41 73 73 65 72  ;..  winMemAsser
11f36 74 4d 61 67 69 63 28 29 3b 0a 20 20 68 48 65 61  tMagic();.  hHea
11f37 70 20 3d 20 77 69 6e 4d 65 6d 47 65 74 48 65 61  p = winMemGetHea
11f38 70 28 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68  p();.  assert( h
11f39 48 65 61 70 21 3d 30 20 29 3b 0a 20 20 61 73 73  Heap!=0 );.  ass
11f3a 65 72 74 28 20 68 48 65 61 70 21 3d 49 4e 56 41  ert( hHeap!=INVA
11f3b 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45  LID_HANDLE_VALUE
11f3c 20 29 3b 0a 23 69 66 20 21 53 51 4c 49 54 45 5f   );.#if !SQLITE_
11f3d 4f 53 5f 57 49 4e 52 54 20 26 26 20 64 65 66 69  OS_WINRT && defi
11f3e 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32  ned(SQLITE_WIN32
11f3f 5f 4d 41 4c 4c 4f 43 5f 56 41 4c 49 44 41 54 45  _MALLOC_VALIDATE
11f40 29 0a 20 20 61 73 73 65 72 74 20 28 20 6f 73 48  ).  assert ( osH
11f41 65 61 70 56 61 6c 69 64 61 74 65 28 68 48 65 61  eapValidate(hHea
11f42 70 2c 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f  p, SQLITE_WIN32_
11f43 48 45 41 50 5f 46 4c 41 47 53 2c 20 70 50 72 69  HEAP_FLAGS, pPri
11f44 6f 72 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20  or) );.#endif.  
11f45 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e 3d  assert( nBytes>=
11f46 30 20 29 3b 0a 20 20 69 66 28 20 21 70 50 72 69  0 );.  if( !pPri
11f47 6f 72 20 29 7b 0a 20 20 20 20 70 20 3d 20 6f 73  or ){.    p = os
11f48 48 65 61 70 41 6c 6c 6f 63 28 68 48 65 61 70 2c  HeapAlloc(hHeap,
11f49 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45   SQLITE_WIN32_HE
11f4a 41 50 5f 46 4c 41 47 53 2c 20 28 53 49 5a 45 5f  AP_FLAGS, (SIZE_
11f4b 54 29 6e 42 79 74 65 73 29 3b 0a 20 20 7d 65 6c  T)nBytes);.  }el
11f4c 73 65 7b 0a 20 20 20 20 70 20 3d 20 6f 73 48 65  se{.    p = osHe
11f4d 61 70 52 65 41 6c 6c 6f 63 28 68 48 65 61 70 2c  apReAlloc(hHeap,
11f4e 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45   SQLITE_WIN32_HE
11f4f 41 50 5f 46 4c 41 47 53 2c 20 70 50 72 69 6f 72  AP_FLAGS, pPrior
11f50 2c 20 28 53 49 5a 45 5f 54 29 6e 42 79 74 65 73  , (SIZE_T)nBytes
11f51 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 20  );.  }.  if( !p 
11f52 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c  ){.    sqlite3_l
11f53 6f 67 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c  og(SQLITE_NOMEM,
11f54 20 22 66 61 69 6c 65 64 20 74 6f 20 25 73 20 25   "failed to %s %
11f55 75 20 62 79 74 65 73 20 28 25 64 29 2c 20 68 65  u bytes (%d), he
11f56 61 70 3d 25 70 22 2c 0a 20 20 20 20 20 20 20 20  ap=%p",.        
11f57 20 20 20 20 20 20 20 20 70 50 72 69 6f 72 20 3f          pPrior ?
11f58 20 22 48 65 61 70 52 65 41 6c 6c 6f 63 22 20 3a   "HeapReAlloc" :
11f59 20 22 48 65 61 70 41 6c 6c 6f 63 22 2c 20 6e 42   "HeapAlloc", nB
11f5a 79 74 65 73 2c 20 6f 73 47 65 74 4c 61 73 74 45  ytes, osGetLastE
11f5b 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20 20  rror(),.        
11f5c 20 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29 68          (void*)h
11f5d 48 65 61 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Heap);.  }.  ret
11f5e 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
11f5f 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
11f60 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e  of an outstandin
11f61 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e  g allocation, in
11f62 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69   bytes..*/.stati
11f63 63 20 69 6e 74 20 77 69 6e 4d 65 6d 53 69 7a 65  c int winMemSize
11f64 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 48 41 4e  (void *p){.  HAN
11f65 44 4c 45 20 68 48 65 61 70 3b 0a 20 20 53 49 5a  DLE hHeap;.  SIZ
11f66 45 5f 54 20 6e 3b 0a 0a 20 20 77 69 6e 4d 65 6d  E_T n;..  winMem
11f67 41 73 73 65 72 74 4d 61 67 69 63 28 29 3b 0a 20  AssertMagic();. 
11f68 20 68 48 65 61 70 20 3d 20 77 69 6e 4d 65 6d 47   hHeap = winMemG
11f69 65 74 48 65 61 70 28 29 3b 0a 20 20 61 73 73 65  etHeap();.  asse
11f6a 72 74 28 20 68 48 65 61 70 21 3d 30 20 29 3b 0a  rt( hHeap!=0 );.
11f6b 20 20 61 73 73 65 72 74 28 20 68 48 65 61 70 21    assert( hHeap!
11f6c 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f  =INVALID_HANDLE_
11f6d 56 41 4c 55 45 20 29 3b 0a 23 69 66 20 21 53 51  VALUE );.#if !SQ
11f6e 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 20 26 26  LITE_OS_WINRT &&
11f6f 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
11f70 57 49 4e 33 32 5f 4d 41 4c 4c 4f 43 5f 56 41 4c  WIN32_MALLOC_VAL
11f71 49 44 41 54 45 29 0a 20 20 61 73 73 65 72 74 20  IDATE).  assert 
11f72 28 20 6f 73 48 65 61 70 56 61 6c 69 64 61 74 65  ( osHeapValidate
11f73 28 68 48 65 61 70 2c 20 53 51 4c 49 54 45 5f 57  (hHeap, SQLITE_W
11f74 49 4e 33 32 5f 48 45 41 50 5f 46 4c 41 47 53 2c  IN32_HEAP_FLAGS,
11f75 20 4e 55 4c 4c 29 20 29 3b 0a 23 65 6e 64 69 66   NULL) );.#endif
11f76 0a 20 20 69 66 28 20 21 70 20 29 20 72 65 74 75  .  if( !p ) retu
11f77 72 6e 20 30 3b 0a 20 20 6e 20 3d 20 6f 73 48 65  rn 0;.  n = osHe
11f78 61 70 53 69 7a 65 28 68 48 65 61 70 2c 20 53 51  apSize(hHeap, SQ
11f79 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41 50 5f  LITE_WIN32_HEAP_
11f7a 46 4c 41 47 53 2c 20 70 29 3b 0a 20 20 69 66 28  FLAGS, p);.  if(
11f7b 20 6e 3d 3d 28 53 49 5a 45 5f 54 29 2d 31 20 29   n==(SIZE_T)-1 )
11f7c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f  {.    sqlite3_lo
11f7d 67 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20  g(SQLITE_NOMEM, 
11f7e 22 66 61 69 6c 65 64 20 74 6f 20 48 65 61 70 53  "failed to HeapS
11f7f 69 7a 65 20 62 6c 6f 63 6b 20 25 70 20 28 25 64  ize block %p (%d
11f80 29 2c 20 68 65 61 70 3d 25 70 22 2c 0a 20 20 20  ), heap=%p",.   
11f81 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2c 20               p, 
11f82 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  osGetLastError()
11f83 2c 20 28 76 6f 69 64 2a 29 68 48 65 61 70 29 3b  , (void*)hHeap);
11f84 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
11f85 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74   }.  return (int
11f86 29 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75  )n;.}../*.** Rou
11f87 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20  nd up a request 
11f88 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74  size to the next
11f89 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f   valid allocatio
11f8a 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69  n size..*/.stati
11f8b 63 20 69 6e 74 20 77 69 6e 4d 65 6d 52 6f 75 6e  c int winMemRoun
11f8c 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65  dup(int n){.  re
11f8d 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn n;.}../*.**
11f8e 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73   Initialize this
11f8f 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74   module..*/.stat
11f90 69 63 20 69 6e 74 20 77 69 6e 4d 65 6d 49 6e 69  ic int winMemIni
11f91 74 28 76 6f 69 64 20 2a 70 41 70 70 44 61 74 61  t(void *pAppData
11f92 29 7b 0a 20 20 77 69 6e 4d 65 6d 44 61 74 61 20  ){.  winMemData 
11f93 2a 70 57 69 6e 4d 65 6d 44 61 74 61 20 3d 20 28  *pWinMemData = (
11f94 77 69 6e 4d 65 6d 44 61 74 61 20 2a 29 70 41 70  winMemData *)pAp
11f95 70 44 61 74 61 3b 0a 0a 20 20 69 66 28 20 21 70  pData;..  if( !p
11f96 57 69 6e 4d 65 6d 44 61 74 61 20 29 20 72 65 74  WinMemData ) ret
11f97 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
11f98 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 69 6e  ;.  assert( pWin
11f99 4d 65 6d 44 61 74 61 2d 3e 6d 61 67 69 63 3d 3d  MemData->magic==
11f9a 57 49 4e 4d 45 4d 5f 4d 41 47 49 43 20 29 3b 0a  WINMEM_MAGIC );.
11f9b 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f  .#if !SQLITE_OS_
11f9c 57 49 4e 52 54 20 26 26 20 53 51 4c 49 54 45 5f  WINRT && SQLITE_
11f9d 57 49 4e 33 32 5f 48 45 41 50 5f 43 52 45 41 54  WIN32_HEAP_CREAT
11f9e 45 0a 20 20 69 66 28 20 21 70 57 69 6e 4d 65 6d  E.  if( !pWinMem
11f9f 44 61 74 61 2d 3e 68 48 65 61 70 20 29 7b 0a 20  Data->hHeap ){. 
11fa0 20 20 20 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e     pWinMemData->
11fa1 68 48 65 61 70 20 3d 20 6f 73 48 65 61 70 43 72  hHeap = osHeapCr
11fa2 65 61 74 65 28 53 51 4c 49 54 45 5f 57 49 4e 33  eate(SQLITE_WIN3
11fa3 32 5f 48 45 41 50 5f 46 4c 41 47 53 2c 0a 20 20  2_HEAP_FLAGS,.  
11fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11fa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11fa6 20 20 20 20 53 51 4c 49 54 45 5f 57 49 4e 33 32      SQLITE_WIN32
11fa7 5f 48 45 41 50 5f 49 4e 49 54 5f 53 49 5a 45 2c  _HEAP_INIT_SIZE,
11fa8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
11fa9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11faa 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 57 49         SQLITE_WI
11fab 4e 33 32 5f 48 45 41 50 5f 4d 41 58 5f 53 49 5a  N32_HEAP_MAX_SIZ
11fac 45 29 3b 0a 20 20 20 20 69 66 28 20 21 70 57 69  E);.    if( !pWi
11fad 6e 4d 65 6d 44 61 74 61 2d 3e 68 48 65 61 70 20  nMemData->hHeap 
11fae 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
11faf 5f 6c 6f 67 28 53 51 4c 49 54 45 5f 4e 4f 4d 45  _log(SQLITE_NOME
11fb0 4d 2c 0a 20 20 20 20 20 20 20 20 20 20 22 66 61  M,.          "fa
11fb1 69 6c 65 64 20 74 6f 20 48 65 61 70 43 72 65 61  iled to HeapCrea
11fb2 74 65 20 28 25 64 29 2c 20 66 6c 61 67 73 3d 25  te (%d), flags=%
11fb3 75 2c 20 69 6e 69 74 53 69 7a 65 3d 25 75 2c 20  u, initSize=%u, 
11fb4 6d 61 78 53 69 7a 65 3d 25 75 22 2c 0a 20 20 20  maxSize=%u",.   
11fb5 20 20 20 20 20 20 20 6f 73 47 65 74 4c 61 73 74         osGetLast
11fb6 45 72 72 6f 72 28 29 2c 20 53 51 4c 49 54 45 5f  Error(), SQLITE_
11fb7 57 49 4e 33 32 5f 48 45 41 50 5f 46 4c 41 47 53  WIN32_HEAP_FLAGS
11fb8 2c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49  ,.          SQLI
11fb9 54 45 5f 57 49 4e 33 32 5f 48 45 41 50 5f 49 4e  TE_WIN32_HEAP_IN
11fba 49 54 5f 53 49 5a 45 2c 20 53 51 4c 49 54 45 5f  IT_SIZE, SQLITE_
11fbb 57 49 4e 33 32 5f 48 45 41 50 5f 4d 41 58 5f 53  WIN32_HEAP_MAX_S
11fbc 49 5a 45 29 3b 0a 20 20 20 20 20 20 72 65 74 75  IZE);.      retu
11fbd 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
11fbe 0a 20 20 20 20 7d 0a 20 20 20 20 70 57 69 6e 4d  .    }.    pWinM
11fbf 65 6d 44 61 74 61 2d 3e 62 4f 77 6e 65 64 20 3d  emData->bOwned =
11fc0 20 54 52 55 45 3b 0a 20 20 20 20 61 73 73 65 72   TRUE;.    asser
11fc1 74 28 20 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e  t( pWinMemData->
11fc2 62 4f 77 6e 65 64 20 29 3b 0a 20 20 7d 0a 23 65  bOwned );.  }.#e
11fc3 6c 73 65 0a 20 20 70 57 69 6e 4d 65 6d 44 61 74  lse.  pWinMemDat
11fc4 61 2d 3e 68 48 65 61 70 20 3d 20 6f 73 47 65 74  a->hHeap = osGet
11fc5 50 72 6f 63 65 73 73 48 65 61 70 28 29 3b 0a 20  ProcessHeap();. 
11fc6 20 69 66 28 20 21 70 57 69 6e 4d 65 6d 44 61 74   if( !pWinMemDat
11fc7 61 2d 3e 68 48 65 61 70 20 29 7b 0a 20 20 20 20  a->hHeap ){.    
11fc8 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49  sqlite3_log(SQLI
11fc9 54 45 5f 4e 4f 4d 45 4d 2c 0a 20 20 20 20 20 20  TE_NOMEM,.      
11fca 20 20 22 66 61 69 6c 65 64 20 74 6f 20 47 65 74    "failed to Get
11fcb 50 72 6f 63 65 73 73 48 65 61 70 20 28 25 64 29  ProcessHeap (%d)
11fcc 22 2c 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f  ", osGetLastErro
11fcd 72 28 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  r());.    return
11fce 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
11fcf 20 7d 0a 20 20 70 57 69 6e 4d 65 6d 44 61 74 61   }.  pWinMemData
11fd0 2d 3e 62 4f 77 6e 65 64 20 3d 20 46 41 4c 53 45  ->bOwned = FALSE
11fd1 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 57 69  ;.  assert( !pWi
11fd2 6e 4d 65 6d 44 61 74 61 2d 3e 62 4f 77 6e 65 64  nMemData->bOwned
11fd3 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73   );.#endif.  ass
11fd4 65 72 74 28 20 70 57 69 6e 4d 65 6d 44 61 74 61  ert( pWinMemData
11fd5 2d 3e 68 48 65 61 70 21 3d 30 20 29 3b 0a 20 20  ->hHeap!=0 );.  
11fd6 61 73 73 65 72 74 28 20 70 57 69 6e 4d 65 6d 44  assert( pWinMemD
11fd7 61 74 61 2d 3e 68 48 65 61 70 21 3d 49 4e 56 41  ata->hHeap!=INVA
11fd8 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45  LID_HANDLE_VALUE
11fd9 20 29 3b 0a 23 69 66 20 21 53 51 4c 49 54 45 5f   );.#if !SQLITE_
11fda 4f 53 5f 57 49 4e 52 54 20 26 26 20 64 65 66 69  OS_WINRT && defi
11fdb 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32  ned(SQLITE_WIN32
11fdc 5f 4d 41 4c 4c 4f 43 5f 56 41 4c 49 44 41 54 45  _MALLOC_VALIDATE
11fdd 29 0a 20 20 61 73 73 65 72 74 28 20 6f 73 48 65  ).  assert( osHe
11fde 61 70 56 61 6c 69 64 61 74 65 28 70 57 69 6e 4d  apValidate(pWinM
11fdf 65 6d 44 61 74 61 2d 3e 68 48 65 61 70 2c 20 53  emData->hHeap, S
11fe0 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45 41 50  QLITE_WIN32_HEAP
11fe1 5f 46 4c 41 47 53 2c 20 4e 55 4c 4c 29 20 29 3b  _FLAGS, NULL) );
11fe2 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
11fe3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
11fe4 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a  *.** Deinitializ
11fe5 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a  e this module..*
11fe6 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69  /.static void wi
11fe7 6e 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69  nMemShutdown(voi
11fe8 64 20 2a 70 41 70 70 44 61 74 61 29 7b 0a 20 20  d *pAppData){.  
11fe9 77 69 6e 4d 65 6d 44 61 74 61 20 2a 70 57 69 6e  winMemData *pWin
11fea 4d 65 6d 44 61 74 61 20 3d 20 28 77 69 6e 4d 65  MemData = (winMe
11feb 6d 44 61 74 61 20 2a 29 70 41 70 70 44 61 74 61  mData *)pAppData
11fec 3b 0a 0a 20 20 69 66 28 20 21 70 57 69 6e 4d 65  ;..  if( !pWinMe
11fed 6d 44 61 74 61 20 29 20 72 65 74 75 72 6e 3b 0a  mData ) return;.
11fee 20 20 69 66 28 20 70 57 69 6e 4d 65 6d 44 61 74    if( pWinMemDat
11fef 61 2d 3e 68 48 65 61 70 20 29 7b 0a 20 20 20 20  a->hHeap ){.    
11ff0 61 73 73 65 72 74 28 20 70 57 69 6e 4d 65 6d 44  assert( pWinMemD
11ff1 61 74 61 2d 3e 68 48 65 61 70 21 3d 49 4e 56 41  ata->hHeap!=INVA
11ff2 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45  LID_HANDLE_VALUE
11ff3 20 29 3b 0a 23 69 66 20 21 53 51 4c 49 54 45 5f   );.#if !SQLITE_
11ff4 4f 53 5f 57 49 4e 52 54 20 26 26 20 64 65 66 69  OS_WINRT && defi
11ff5 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32  ned(SQLITE_WIN32
11ff6 5f 4d 41 4c 4c 4f 43 5f 56 41 4c 49 44 41 54 45  _MALLOC_VALIDATE
11ff7 29 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 73  ).    assert( os
11ff8 48 65 61 70 56 61 6c 69 64 61 74 65 28 70 57 69  HeapValidate(pWi
11ff9 6e 4d 65 6d 44 61 74 61 2d 3e 68 48 65 61 70 2c  nMemData->hHeap,
11ffa 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 45   SQLITE_WIN32_HE
11ffb 41 50 5f 46 4c 41 47 53 2c 20 4e 55 4c 4c 29 20  AP_FLAGS, NULL) 
11ffc 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66  );.#endif.    if
11ffd 28 20 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e 62  ( pWinMemData->b
11ffe 4f 77 6e 65 64 20 29 7b 0a 20 20 20 20 20 20 69  Owned ){.      i
11fff 66 28 20 21 6f 73 48 65 61 70 44 65 73 74 72 6f  f( !osHeapDestro
12000 79 28 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e 68  y(pWinMemData->h
12001 48 65 61 70 29 20 29 7b 0a 20 20 20 20 20 20 20  Heap) ){.       
12002 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c   sqlite3_log(SQL
12003 49 54 45 5f 4e 4f 4d 45 4d 2c 20 22 66 61 69 6c  ITE_NOMEM, "fail
12004 65 64 20 74 6f 20 48 65 61 70 44 65 73 74 72 6f  ed to HeapDestro
12005 79 20 28 25 64 29 2c 20 68 65 61 70 3d 25 70 22  y (%d), heap=%p"
12006 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
12007 20 20 20 20 20 20 6f 73 47 65 74 4c 61 73 74 45        osGetLastE
12008 72 72 6f 72 28 29 2c 20 28 76 6f 69 64 2a 29 70  rror(), (void*)p
12009 57 69 6e 4d 65 6d 44 61 74 61 2d 3e 68 48 65 61  WinMemData->hHea
1200a 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  p);.      }.    
1200b 20 20 70 57 69 6e 4d 65 6d 44 61 74 61 2d 3e 62    pWinMemData->b
1200c 4f 77 6e 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20  Owned = FALSE;. 
1200d 20 20 20 7d 0a 20 20 20 20 70 57 69 6e 4d 65 6d     }.    pWinMem
1200e 44 61 74 61 2d 3e 68 48 65 61 70 20 3d 20 4e 55  Data->hHeap = NU
1200f 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  LL;.  }.}../*.**
12010 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f   Populate the lo
12011 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
12012 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69  llocation functi
12013 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a  on pointers in.*
12014 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  * sqlite3GlobalC
12015 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69  onfig.m with poi
12016 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75  nters to the rou
12017 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
12018 6c 65 2e 20 54 68 65 0a 2a 2a 20 61 72 67 75 6d  le. The.** argum
12019 65 6e 74 73 20 73 70 65 63 69 66 79 20 74 68 65  ents specify the
1201a 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79   block of memory
1201b 20 74 6f 20 6d 61 6e 61 67 65 2e 0a 2a 2a 0a 2a   to manage..**.*
1201c 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1201d 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 62 79  s only called by
1201e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
1201f 29 2c 20 61 6e 64 20 74 68 65 72 65 66 6f 72 65  ), and therefore
12020 0a 2a 2a 20 69 73 20 6e 6f 74 20 72 65 71 75 69  .** is not requi
12021 72 65 64 20 74 6f 20 62 65 20 74 68 72 65 61 64  red to be thread
12022 73 61 66 65 20 28 69 74 20 69 73 20 6e 6f 74 29  safe (it is not)
12023 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
12024 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ATE const sqlite
12025 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73  3_mem_methods *s
12026 71 6c 69 74 65 33 4d 65 6d 47 65 74 57 69 6e 33  qlite3MemGetWin3
12027 32 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  2(void){.  stati
12028 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
12029 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 77 69 6e 4d  mem_methods winM
1202a 65 6d 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20  emMethods = {.  
1202b 20 20 77 69 6e 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a    winMemMalloc,.
1202c 20 20 20 20 77 69 6e 4d 65 6d 46 72 65 65 2c 0a      winMemFree,.
1202d 20 20 20 20 77 69 6e 4d 65 6d 52 65 61 6c 6c 6f      winMemReallo
1202e 63 2c 0a 20 20 20 20 77 69 6e 4d 65 6d 53 69 7a  c,.    winMemSiz
1202f 65 2c 0a 20 20 20 20 77 69 6e 4d 65 6d 52 6f 75  e,.    winMemRou
12030 6e 64 75 70 2c 0a 20 20 20 20 77 69 6e 4d 65 6d  ndup,.    winMem
12031 49 6e 69 74 2c 0a 20 20 20 20 77 69 6e 4d 65 6d  Init,.    winMem
12032 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 26 77  Shutdown,.    &w
12033 69 6e 5f 6d 65 6d 5f 64 61 74 61 0a 20 20 7d 3b  in_mem_data.  };
12034 0a 20 20 72 65 74 75 72 6e 20 26 77 69 6e 4d 65  .  return &winMe
12035 6d 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 53 51 4c  mMethods;.}..SQL
12036 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
12037 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65   sqlite3MemSetDe
12038 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73  fault(void){.  s
12039 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51  qlite3_config(SQ
1203a 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c  LITE_CONFIG_MALL
1203b 4f 43 2c 20 73 71 6c 69 74 65 33 4d 65 6d 47 65  OC, sqlite3MemGe
1203c 74 57 69 6e 33 32 28 29 29 3b 0a 7d 0a 23 65 6e  tWin32());.}.#en
1203d 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 57 49  dif /* SQLITE_WI
1203e 4e 33 32 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f  N32_MALLOC */../
1203f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55  *.** Convert a U
12040 54 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 4d  TF-8 string to M
12041 69 63 72 6f 73 6f 66 74 20 55 6e 69 63 6f 64 65  icrosoft Unicode
12042 20 28 55 54 46 2d 31 36 3f 29 2e 20 0a 2a 2a 0a   (UTF-16?). .**.
12043 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  ** Space to hold
12044 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   the returned st
12045 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64  ring is obtained
12046 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f   from malloc..*/
12047 0a 73 74 61 74 69 63 20 4c 50 57 53 54 52 20 75  .static LPWSTR u
12048 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e  tf8ToUnicode(con
12049 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
1204a 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 43 68 61 72  me){.  int nChar
1204b 3b 0a 20 20 4c 50 57 53 54 52 20 7a 57 69 64 65  ;.  LPWSTR zWide
1204c 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 43 68  Filename;..  nCh
1204d 61 72 20 3d 20 6f 73 4d 75 6c 74 69 42 79 74 65  ar = osMultiByte
1204e 54 6f 57 69 64 65 43 68 61 72 28 43 50 5f 55 54  ToWideChar(CP_UT
1204f 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65  F8, 0, zFilename
12050 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a  , -1, NULL, 0);.
12051 20 20 69 66 28 20 6e 43 68 61 72 3d 3d 30 20 29    if( nChar==0 )
12052 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
12053 20 20 7d 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e    }.  zWideFilen
12054 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  ame = sqlite3Mal
12055 6c 6f 63 5a 65 72 6f 28 20 6e 43 68 61 72 2a 73  locZero( nChar*s
12056 69 7a 65 6f 66 28 7a 57 69 64 65 46 69 6c 65 6e  izeof(zWideFilen
12057 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28  ame[0]) );.  if(
12058 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3d 3d   zWideFilename==
12059 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1205a 30 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d  0;.  }.  nChar =
1205b 20 6f 73 4d 75 6c 74 69 42 79 74 65 54 6f 57 69   osMultiByteToWi
1205c 64 65 43 68 61 72 28 43 50 5f 55 54 46 38 2c 20  deChar(CP_UTF8, 
1205d 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31  0, zFilename, -1
1205e 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c  , zWideFilename,
1205f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
12060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12061 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 28 20 6e   nChar);.  if( n
12062 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 73  Char==0 ){.    s
12063 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 57 69 64  qlite3_free(zWid
12064 65 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20  eFilename);.    
12065 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d 20  zWideFilename = 
12066 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
12067 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 7d  zWideFilename;.}
12068 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
12069 4d 69 63 72 6f 73 6f 66 74 20 55 6e 69 63 6f 64  Microsoft Unicod
1206a 65 20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61  e to UTF-8.  Spa
1206b 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
1206c 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69  eturned string i
1206d 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72  s.** obtained fr
1206e 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
1206f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  c()..*/.static c
12070 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f 55 74  har *unicodeToUt
12071 66 38 28 4c 50 43 57 53 54 52 20 7a 57 69 64 65  f8(LPCWSTR zWide
12072 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74  Filename){.  int
12073 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 72 20 2a   nByte;.  char *
12074 7a 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 42  zFilename;..  nB
12075 79 74 65 20 3d 20 6f 73 57 69 64 65 43 68 61 72  yte = osWideChar
12076 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f 55  ToMultiByte(CP_U
12077 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c  TF8, 0, zWideFil
12078 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c  ename, -1, 0, 0,
12079 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42   0, 0);.  if( nB
1207a 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20  yte == 0 ){.    
1207b 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
1207c 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 73 71 6c 69  zFilename = sqli
1207d 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 6e  te3MallocZero( n
1207e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46  Byte );.  if( zF
1207f 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  ilename==0 ){.  
12080 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
12081 20 20 6e 42 79 74 65 20 3d 20 6f 73 57 69 64 65    nByte = osWide
12082 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28  CharToMultiByte(
12083 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 64  CP_UTF8, 0, zWid
12084 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a  eFilename, -1, z
12085 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c  Filename, nByte,
12086 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
12087 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12088 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42   0, 0);.  if( nB
12089 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20  yte == 0 ){.    
1208a 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 69  sqlite3_free(zFi
1208b 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 46 69  lename);.    zFi
1208c 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a  lename = 0;.  }.
1208d 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61    return zFilena
1208e 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  me;.}../*.** Con
1208f 76 65 72 74 20 61 6e 20 41 4e 53 49 20 73 74 72  vert an ANSI str
12090 69 6e 67 20 74 6f 20 4d 69 63 72 6f 73 6f 66 74  ing to Microsoft
12091 20 55 6e 69 63 6f 64 65 2c 20 62 61 73 65 64 20   Unicode, based 
12092 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  on the.** curren
12093 74 20 63 6f 64 65 70 61 67 65 20 73 65 74 74 69  t codepage setti
12094 6e 67 73 20 66 6f 72 20 66 69 6c 65 20 61 70 69  ngs for file api
12095 73 2e 0a 2a 2a 20 0a 2a 2a 20 53 70 61 63 65 20  s..** .** Space 
12096 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75  to hold the retu
12097 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f  rned string is o
12098 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20  btained.** from 
12099 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e 0a  sqlite3_malloc..
1209a 2a 2f 0a 73 74 61 74 69 63 20 4c 50 57 53 54 52  */.static LPWSTR
1209b 20 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 63   mbcsToUnicode(c
1209c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
1209d 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79  name){.  int nBy
1209e 74 65 3b 0a 20 20 4c 50 57 53 54 52 20 7a 4d 62  te;.  LPWSTR zMb
1209f 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e  csFilename;.  in
120a0 74 20 63 6f 64 65 70 61 67 65 20 3d 20 6f 73 41  t codepage = osA
120a1 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29  reFileApisANSI()
120a2 20 3f 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f   ? CP_ACP : CP_O
120a3 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d  EMCP;..  nByte =
120a4 20 6f 73 4d 75 6c 74 69 42 79 74 65 54 6f 57 69   osMultiByteToWi
120a5 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c  deChar(codepage,
120a6 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d   0, zFilename, -
120a7 31 2c 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20  1, NULL,.       
120a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
120a9 20 20 20 20 20 20 20 20 20 30 29 2a 73 69 7a 65           0)*size
120aa 6f 66 28 57 43 48 41 52 29 3b 0a 20 20 69 66 28  of(WCHAR);.  if(
120ab 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20   nByte==0 ){.   
120ac 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
120ad 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d   zMbcsFilename =
120ae 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65   sqlite3MallocZe
120af 72 6f 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66  ro( nByte*sizeof
120b0 28 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 5b 30  (zMbcsFilename[0
120b1 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 4d 62 63  ]) );.  if( zMbc
120b2 73 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a  sFilename==0 ){.
120b3 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
120b4 7d 0a 20 20 6e 42 79 74 65 20 3d 20 6f 73 4d 75  }.  nByte = osMu
120b5 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 61  ltiByteToWideCha
120b6 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a  r(codepage, 0, z
120b7 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 4d  Filename, -1, zM
120b8 62 63 73 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20  bcsFilename,.   
120b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
120ba 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 42 79               nBy
120bb 74 65 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65  te);.  if( nByte
120bc 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
120bd 65 33 5f 66 72 65 65 28 7a 4d 62 63 73 46 69 6c  e3_free(zMbcsFil
120be 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d 62 63  ename);.    zMbc
120bf 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20  sFilename = 0;. 
120c0 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d 62 63   }.  return zMbc
120c1 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a  sFilename;.}../*
120c2 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 4d 69 63 72  .** Convert Micr
120c3 6f 73 6f 66 74 20 55 6e 69 63 6f 64 65 20 74 6f  osoft Unicode to
120c4 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72   multi-byte char
120c5 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62 61  acter string, ba
120c6 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75 73  sed on the.** us
120c7 65 72 27 73 20 41 4e 53 49 20 63 6f 64 65 70 61  er's ANSI codepa
120c8 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20  ge..**.** Space 
120c9 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75  to hold the retu
120ca 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f  rned string is o
120cb 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
120cc 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
120cd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
120ce 20 2a 75 6e 69 63 6f 64 65 54 6f 4d 62 63 73 28   *unicodeToMbcs(
120cf 4c 50 43 57 53 54 52 20 7a 57 69 64 65 46 69 6c  LPCWSTR zWideFil
120d0 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42  ename){.  int nB
120d1 79 74 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69  yte;.  char *zFi
120d2 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f  lename;.  int co
120d3 64 65 70 61 67 65 20 3d 20 6f 73 41 72 65 46 69  depage = osAreFi
120d4 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f 20 43  leApisANSI() ? C
120d5 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50  P_ACP : CP_OEMCP
120d6 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 6f 73 57  ;..  nByte = osW
120d7 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79  ideCharToMultiBy
120d8 74 65 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20  te(codepage, 0, 
120d9 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d  zWideFilename, -
120da 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  1, 0, 0, 0, 0);.
120db 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d 20 30    if( nByte == 0
120dc 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
120dd 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d  ;.  }.  zFilenam
120de 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  e = sqlite3Mallo
120df 63 5a 65 72 6f 28 20 6e 42 79 74 65 20 29 3b 0a  cZero( nByte );.
120e0 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d    if( zFilename=
120e1 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
120e2 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20   0;.  }.  nByte 
120e3 3d 20 6f 73 57 69 64 65 43 68 61 72 54 6f 4d 75  = osWideCharToMu
120e4 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 67 65  ltiByte(codepage
120e5 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61  , 0, zWideFilena
120e6 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e 61 6d  me, -1, zFilenam
120e7 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
120e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
120e9 20 20 20 6e 42 79 74 65 2c 20 30 2c 20 30 29 3b     nByte, 0, 0);
120ea 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d 20  .  if( nByte == 
120eb 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
120ec 5f 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29  _free(zFilename)
120ed 3b 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20  ;.    zFilename 
120ee 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
120ef 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a  n zFilename;.}..
120f0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75  /*.** Convert mu
120f1 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74 65  ltibyte characte
120f2 72 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d  r string to UTF-
120f3 38 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  8.  Space to hol
120f4 64 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65  d the.** returne
120f5 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61  d string is obta
120f6 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
120f7 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53  3_malloc()..*/.S
120f8 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
120f9 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62  sqlite3_win32_mb
120fa 63 73 5f 74 6f 5f 75 74 66 38 28 63 6f 6e 73 74  cs_to_utf8(const
120fb 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
120fc 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  ){.  char *zFile
120fd 6e 61 6d 65 55 74 66 38 3b 0a 20 20 4c 50 57 53  nameUtf8;.  LPWS
120fe 54 52 20 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20  TR zTmpWide;..  
120ff 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 73 54  zTmpWide = mbcsT
12100 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61  oUnicode(zFilena
12101 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57  me);.  if( zTmpW
12102 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ide==0 ){.    re
12103 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46  turn 0;.  }.  zF
12104 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75 6e  ilenameUtf8 = un
12105 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d 70  icodeToUtf8(zTmp
12106 57 69 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Wide);.  sqlite3
12107 5f 66 72 65 65 28 7a 54 6d 70 57 69 64 65 29 3b  _free(zTmpWide);
12108 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e  .  return zFilen
12109 61 6d 65 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a  ameUtf8;.}../*.*
1210a 2a 20 43 6f 6e 76 65 72 74 20 55 54 46 2d 38 20  * Convert UTF-8 
1210b 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61  to multibyte cha
1210c 72 61 63 74 65 72 20 73 74 72 69 6e 67 2e 20 20  racter string.  
1210d 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
1210e 65 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73  e .** returned s
1210f 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65  tring is obtaine
12110 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
12111 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49  alloc()..*/.SQLI
12112 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c  TE_API char *sql
12113 69 74 65 33 5f 77 69 6e 33 32 5f 75 74 66 38 5f  ite3_win32_utf8_
12114 74 6f 5f 6d 62 63 73 28 63 6f 6e 73 74 20 63 68  to_mbcs(const ch
12115 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a  ar *zFilename){.
12116 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d    char *zFilenam
12117 65 4d 62 63 73 3b 0a 20 20 4c 50 57 53 54 52 20  eMbcs;.  LPWSTR 
12118 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d  zTmpWide;..  zTm
12119 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55 6e  pWide = utf8ToUn
1211a 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29  icode(zFilename)
1211b 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64 65  ;.  if( zTmpWide
1211c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
1211d 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65  n 0;.  }.  zFile
1211e 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63 6f  nameMbcs = unico
1211f 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69 64  deToMbcs(zTmpWid
12120 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  e);.  sqlite3_fr
12121 65 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20  ee(zTmpWide);.  
12122 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65  return zFilename
12123 4d 62 63 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  Mbcs;.}../*.** T
12124 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74  his function set
12125 73 20 74 68 65 20 64 61 74 61 20 64 69 72 65 63  s the data direc
12126 74 6f 72 79 20 6f 72 20 74 68 65 20 74 65 6d 70  tory or the temp
12127 6f 72 61 72 79 20 64 69 72 65 63 74 6f 72 79 20  orary directory 
12128 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20  based on.** the 
12129 70 72 6f 76 69 64 65 64 20 61 72 67 75 6d 65 6e  provided argumen
1212a 74 73 2e 20 20 54 68 65 20 74 79 70 65 20 61 72  ts.  The type ar
1212b 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 31  gument must be 1
1212c 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 65 74   in order to set
1212d 20 74 68 65 0a 2a 2a 20 64 61 74 61 20 64 69 72   the.** data dir
1212e 65 63 74 6f 72 79 20 6f 72 20 32 20 69 6e 20 6f  ectory or 2 in o
1212f 72 64 65 72 20 74 6f 20 73 65 74 20 74 68 65 20  rder to set the 
12130 74 65 6d 70 6f 72 61 72 79 20 64 69 72 65 63 74  temporary direct
12131 6f 72 79 2e 20 20 54 68 65 20 7a 56 61 6c 75 65  ory.  The zValue
12132 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20  .** argument is 
12133 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
12134 64 69 72 65 63 74 6f 72 79 20 74 6f 20 75 73 65  directory to use
12135 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
12136 6c 75 65 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 53  lue will be.** S
12137 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63  QLITE_OK if succ
12138 65 73 73 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54  essful..*/.SQLIT
12139 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1213a 33 5f 77 69 6e 33 32 5f 73 65 74 5f 64 69 72 65  3_win32_set_dire
1213b 63 74 6f 72 79 28 44 57 4f 52 44 20 74 79 70 65  ctory(DWORD type
1213c 2c 20 4c 50 43 57 53 54 52 20 7a 56 61 6c 75 65  , LPCWSTR zValue
1213d 29 7b 0a 20 20 63 68 61 72 20 2a 2a 70 70 44 69  ){.  char **ppDi
1213e 72 65 63 74 6f 72 79 20 3d 20 30 3b 0a 23 69 66  rectory = 0;.#if
1213f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
12140 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e 74 20  _AUTOINIT.  int 
12141 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69  rc = sqlite3_ini
12142 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28  tialize();.  if(
12143 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
12144 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 74 79  .#endif.  if( ty
12145 70 65 3d 3d 53 51 4c 49 54 45 5f 57 49 4e 33 32  pe==SQLITE_WIN32
12146 5f 44 41 54 41 5f 44 49 52 45 43 54 4f 52 59 5f  _DATA_DIRECTORY_
12147 54 59 50 45 20 29 7b 0a 20 20 20 20 70 70 44 69  TYPE ){.    ppDi
12148 72 65 63 74 6f 72 79 20 3d 20 26 73 71 6c 69 74  rectory = &sqlit
12149 65 33 5f 64 61 74 61 5f 64 69 72 65 63 74 6f 72  e3_data_director
1214a 79 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 74  y;.  }else if( t
1214b 79 70 65 3d 3d 53 51 4c 49 54 45 5f 57 49 4e 33  ype==SQLITE_WIN3
1214c 32 5f 54 45 4d 50 5f 44 49 52 45 43 54 4f 52 59  2_TEMP_DIRECTORY
1214d 5f 54 59 50 45 20 29 7b 0a 20 20 20 20 70 70 44  _TYPE ){.    ppD
1214e 69 72 65 63 74 6f 72 79 20 3d 20 26 73 71 6c 69  irectory = &sqli
1214f 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
12150 72 79 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  ry;.  }.  assert
12151 28 20 21 70 70 44 69 72 65 63 74 6f 72 79 20 7c  ( !ppDirectory |
12152 7c 20 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 57  | type==SQLITE_W
12153 49 4e 33 32 5f 44 41 54 41 5f 44 49 52 45 43 54  IN32_DATA_DIRECT
12154 4f 52 59 5f 54 59 50 45 0a 20 20 20 20 20 20 20  ORY_TYPE.       
12155 20 20 20 7c 7c 20 74 79 70 65 3d 3d 53 51 4c 49     || type==SQLI
12156 54 45 5f 57 49 4e 33 32 5f 54 45 4d 50 5f 44 49  TE_WIN32_TEMP_DI
12157 52 45 43 54 4f 52 59 5f 54 59 50 45 0a 20 20 29  RECTORY_TYPE.  )
12158 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 70 44  ;.  assert( !ppD
12159 69 72 65 63 74 6f 72 79 20 7c 7c 20 73 71 6c 69  irectory || sqli
1215a 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54 79  te3MemdebugHasTy
1215b 70 65 28 2a 70 70 44 69 72 65 63 74 6f 72 79 2c  pe(*ppDirectory,
1215c 20 4d 45 4d 54 59 50 45 5f 48 45 41 50 29 20 29   MEMTYPE_HEAP) )
1215d 3b 0a 20 20 69 66 28 20 70 70 44 69 72 65 63 74  ;.  if( ppDirect
1215e 6f 72 79 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ory ){.    char 
1215f 2a 7a 56 61 6c 75 65 55 74 66 38 20 3d 20 30 3b  *zValueUtf8 = 0;
12160 0a 20 20 20 20 69 66 28 20 7a 56 61 6c 75 65 20  .    if( zValue 
12161 26 26 20 7a 56 61 6c 75 65 5b 30 5d 20 29 7b 0a  && zValue[0] ){.
12162 20 20 20 20 20 20 7a 56 61 6c 75 65 55 74 66 38        zValueUtf8
12163 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38   = unicodeToUtf8
12164 28 7a 56 61 6c 75 65 29 3b 0a 20 20 20 20 20 20  (zValue);.      
12165 69 66 20 28 20 7a 56 61 6c 75 65 55 74 66 38 3d  if ( zValueUtf8=
12166 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  =0 ){.        re
12167 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
12168 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  M;.      }.    }
12169 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
1216a 65 28 2a 70 70 44 69 72 65 63 74 6f 72 79 29 3b  e(*ppDirectory);
1216b 0a 20 20 20 20 2a 70 70 44 69 72 65 63 74 6f 72  .    *ppDirector
1216c 79 20 3d 20 7a 56 61 6c 75 65 55 74 66 38 3b 0a  y = zValueUtf8;.
1216d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1216e 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  E_OK;.  }.  retu
1216f 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
12170 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65  .}../*.** The re
12171 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 67 65  turn value of ge
12172 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 0a 2a 2a  tLastErrorMsg.**
12173 20 69 73 20 7a 65 72 6f 20 69 66 20 74 68 65 20   is zero if the 
12174 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 69  error message fi
12175 74 73 20 69 6e 20 74 68 65 20 62 75 66 66 65 72  ts in the buffer
12176 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a  , or non-zero.**
12177 20 6f 74 68 65 72 77 69 73 65 20 28 69 66 20 74   otherwise (if t
12178 68 65 20 6d 65 73 73 61 67 65 20 77 61 73 20 74  he message was t
12179 72 75 6e 63 61 74 65 64 29 2e 0a 2a 2f 0a 73 74  runcated)..*/.st
1217a 61 74 69 63 20 69 6e 74 20 67 65 74 4c 61 73 74  atic int getLast
1217b 45 72 72 6f 72 4d 73 67 28 44 57 4f 52 44 20 6c  ErrorMsg(DWORD l
1217c 61 73 74 45 72 72 6e 6f 2c 20 69 6e 74 20 6e 42  astErrno, int nB
1217d 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b  uf, char *zBuf){
1217e 0a 20 20 2f 2a 20 46 6f 72 6d 61 74 4d 65 73 73  .  /* FormatMess
1217f 61 67 65 20 72 65 74 75 72 6e 73 20 30 20 6f 6e  age returns 0 on
12180 20 66 61 69 6c 75 72 65 2e 20 20 4f 74 68 65 72   failure.  Other
12181 77 69 73 65 20 69 74 0a 20 20 2a 2a 20 72 65 74  wise it.  ** ret
12182 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
12183 6f 66 20 54 43 48 41 52 73 20 77 72 69 74 74 65  of TCHARs writte
12184 6e 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 0a  n to the output.
12185 20 20 2a 2a 20 62 75 66 66 65 72 2c 20 65 78 63    ** buffer, exc
12186 6c 75 64 69 6e 67 20 74 68 65 20 74 65 72 6d 69  luding the termi
12187 6e 61 74 69 6e 67 20 6e 75 6c 6c 20 63 68 61 72  nating null char
12188 2e 0a 20 20 2a 2f 0a 20 20 44 57 4f 52 44 20 64  ..  */.  DWORD d
12189 77 4c 65 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72  wLen = 0;.  char
1218a 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 0a 20 20 69   *zOut = 0;..  i
1218b 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 23 69 66  f( isNT() ){.#if
1218c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54   SQLITE_OS_WINRT
1218d 0a 20 20 20 20 57 43 48 41 52 20 7a 54 65 6d 70  .    WCHAR zTemp
1218e 57 69 64 65 5b 4d 41 58 5f 50 41 54 48 2b 31 5d  Wide[MAX_PATH+1]
1218f 3b 20 2f 2a 20 4e 4f 54 45 3a 20 53 6f 6d 65 77  ; /* NOTE: Somew
12190 68 61 74 20 61 72 62 69 74 72 61 72 79 2e 20 2a  hat arbitrary. *
12191 2f 0a 20 20 20 20 64 77 4c 65 6e 20 3d 20 6f 73  /.    dwLen = os
12192 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 57 28 46  FormatMessageW(F
12193 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 52  ORMAT_MESSAGE_FR
12194 4f 4d 5f 53 59 53 54 45 4d 20 7c 0a 20 20 20 20  OM_SYSTEM |.    
12195 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12196 20 20 20 20 20 20 20 20 20 46 4f 52 4d 41 54 5f           FORMAT_
12197 4d 45 53 53 41 47 45 5f 49 47 4e 4f 52 45 5f 49  MESSAGE_IGNORE_I
12198 4e 53 45 52 54 53 2c 0a 20 20 20 20 20 20 20 20  NSERTS,.        
12199 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1219a 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20       NULL,.     
1219b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1219c 20 20 20 20 20 20 20 20 6c 61 73 74 45 72 72 6e          lastErrn
1219d 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  o,.             
1219e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1219f 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
121a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121a1 7a 54 65 6d 70 57 69 64 65 2c 0a 20 20 20 20 20  zTempWide,.     
121a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121a3 20 20 20 20 20 20 20 20 4d 41 58 5f 50 41 54 48          MAX_PATH
121a4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
121a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
121a6 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 4c 50 57  );.#else.    LPW
121a7 53 54 52 20 7a 54 65 6d 70 57 69 64 65 20 3d 20  STR zTempWide = 
121a8 4e 55 4c 4c 3b 0a 20 20 20 20 64 77 4c 65 6e 20  NULL;.    dwLen 
121a9 3d 20 6f 73 46 6f 72 6d 61 74 4d 65 73 73 61 67  = osFormatMessag
121aa 65 57 28 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47  eW(FORMAT_MESSAG
121ab 45 5f 41 4c 4c 4f 43 41 54 45 5f 42 55 46 46 45  E_ALLOCATE_BUFFE
121ac 52 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20  R |.            
121ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121ae 20 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f   FORMAT_MESSAGE_
121af 46 52 4f 4d 5f 53 59 53 54 45 4d 20 7c 0a 20 20  FROM_SYSTEM |.  
121b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121b1 20 20 20 20 20 20 20 20 20 20 20 46 4f 52 4d 41             FORMA
121b2 54 5f 4d 45 53 53 41 47 45 5f 49 47 4e 4f 52 45  T_MESSAGE_IGNORE
121b3 5f 49 4e 53 45 52 54 53 2c 0a 20 20 20 20 20 20  _INSERTS,.      
121b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121b5 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20         NULL,.   
121b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121b7 20 20 20 20 20 20 20 20 20 20 6c 61 73 74 45 72            lastEr
121b8 72 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rno,.           
121b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121ba 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20    0,.           
121bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121bc 20 20 28 4c 50 57 53 54 52 29 20 26 7a 54 65 6d    (LPWSTR) &zTem
121bd 70 57 69 64 65 2c 0a 20 20 20 20 20 20 20 20 20  pWide,.         
121be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121bf 20 20 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20      0,.         
121c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121c1 20 20 20 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20      0);.#endif. 
121c2 20 20 20 69 66 28 20 64 77 4c 65 6e 20 3e 20 30     if( dwLen > 0
121c3 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 61 6c 6c   ){.      /* all
121c4 6f 63 61 74 65 20 61 20 62 75 66 66 65 72 20 61  ocate a buffer a
121c5 6e 64 20 63 6f 6e 76 65 72 74 20 74 6f 20 55 54  nd convert to UT
121c6 46 38 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  F8 */.      sqli
121c7 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
121c8 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 7a 4f  lloc();.      zO
121c9 75 74 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74  ut = unicodeToUt
121ca 66 38 28 7a 54 65 6d 70 57 69 64 65 29 3b 0a 20  f8(zTempWide);. 
121cb 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42       sqlite3EndB
121cc 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 23  enignMalloc();.#
121cd 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49  if !SQLITE_OS_WI
121ce 4e 52 54 0a 20 20 20 20 20 20 2f 2a 20 66 72 65  NRT.      /* fre
121cf 65 20 74 68 65 20 73 79 73 74 65 6d 20 62 75 66  e the system buf
121d0 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  fer allocated by
121d1 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 20 2a   FormatMessage *
121d2 2f 0a 20 20 20 20 20 20 6f 73 4c 6f 63 61 6c 46  /.      osLocalF
121d3 72 65 65 28 7a 54 65 6d 70 57 69 64 65 29 3b 0a  ree(zTempWide);.
121d4 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d  #endif.    }.  }
121d5 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 57  .#ifdef SQLITE_W
121d6 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20 20  IN32_HAS_ANSI.  
121d7 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a  else{.    char *
121d8 7a 54 65 6d 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20  zTemp = NULL;.  
121d9 20 20 64 77 4c 65 6e 20 3d 20 6f 73 46 6f 72 6d    dwLen = osForm
121da 61 74 4d 65 73 73 61 67 65 41 28 46 4f 52 4d 41  atMessageA(FORMA
121db 54 5f 4d 45 53 53 41 47 45 5f 41 4c 4c 4f 43 41  T_MESSAGE_ALLOCA
121dc 54 45 5f 42 55 46 46 45 52 20 7c 0a 20 20 20 20  TE_BUFFER |.    
121dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121de 20 20 20 20 20 20 20 20 20 46 4f 52 4d 41 54 5f           FORMAT_
121df 4d 45 53 53 41 47 45 5f 46 52 4f 4d 5f 53 59 53  MESSAGE_FROM_SYS
121e0 54 45 4d 20 7c 0a 20 20 20 20 20 20 20 20 20 20  TEM |.          
121e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121e2 20 20 20 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47     FORMAT_MESSAG
121e3 45 5f 49 47 4e 4f 52 45 5f 49 4e 53 45 52 54 53  E_IGNORE_INSERTS
121e4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
121e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e                 N
121e6 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ULL,.           
121e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121e8 20 20 6c 61 73 74 45 72 72 6e 6f 2c 0a 20 20 20    lastErrno,.   
121e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121ea 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20            0,.   
121eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121ec 20 20 20 20 20 20 20 20 20 20 28 4c 50 53 54 52            (LPSTR
121ed 29 20 26 7a 54 65 6d 70 2c 0a 20 20 20 20 20 20  ) &zTemp,.      
121ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121ef 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20         0,.      
121f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121f1 20 20 20 20 20 20 20 30 29 3b 0a 20 20 20 20 69         0);.    i
121f2 66 28 20 64 77 4c 65 6e 20 3e 20 30 20 29 7b 0a  f( dwLen > 0 ){.
121f3 20 20 20 20 20 20 2f 2a 20 61 6c 6c 6f 63 61 74        /* allocat
121f4 65 20 61 20 62 75 66 66 65 72 20 61 6e 64 20 63  e a buffer and c
121f5 6f 6e 76 65 72 74 20 74 6f 20 55 54 46 38 20 2a  onvert to UTF8 *
121f6 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  /.      sqlite3B
121f7 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
121f8 28 29 3b 0a 20 20 20 20 20 20 7a 4f 75 74 20 3d  ();.      zOut =
121f9 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d   sqlite3_win32_m
121fa 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 54 65 6d  bcs_to_utf8(zTem
121fb 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  p);.      sqlite
121fc 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
121fd 28 29 3b 0a 20 20 20 20 20 20 2f 2a 20 66 72 65  ();.      /* fre
121fe 65 20 74 68 65 20 73 79 73 74 65 6d 20 62 75 66  e the system buf
121ff 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  fer allocated by
12200 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 20 2a   FormatMessage *
12201 2f 0a 20 20 20 20 20 20 6f 73 4c 6f 63 61 6c 46  /.      osLocalF
12202 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20  ree(zTemp);.    
12203 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69  }.  }.#endif.  i
12204 66 28 20 30 20 3d 3d 20 64 77 4c 65 6e 20 29 7b  f( 0 == dwLen ){
12205 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
12206 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66  rintf(nBuf, zBuf
12207 2c 20 22 4f 73 45 72 72 6f 72 20 30 78 25 78 20  , "OsError 0x%x 
12208 28 25 75 29 22 2c 20 6c 61 73 74 45 72 72 6e 6f  (%u)", lastErrno
12209 2c 20 6c 61 73 74 45 72 72 6e 6f 29 3b 0a 20 20  , lastErrno);.  
1220a 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 63 6f  }else{.    /* co
1220b 70 79 20 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20  py a maximum of 
1220c 6e 42 75 66 20 63 68 61 72 73 20 74 6f 20 6f 75  nBuf chars to ou
1220d 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20  tput buffer */. 
1220e 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1220f 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20  ntf(nBuf, zBuf, 
12210 22 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20  "%s", zOut);.   
12211 20 2f 2a 20 66 72 65 65 20 74 68 65 20 55 54 46   /* free the UTF
12212 38 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20  8 buffer */.    
12213 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4f 75  sqlite3_free(zOu
12214 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  t);.  }.  return
12215 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 0a 2a 2a 20   0;.}../*.**.** 
12216 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2d 20  This function - 
12217 77 69 6e 4c 6f 67 45 72 72 6f 72 41 74 4c 69 6e  winLogErrorAtLin
12218 65 28 29 20 2d 20 69 73 20 6f 6e 6c 79 20 65 76  e() - is only ev
12219 65 72 20 63 61 6c 6c 65 64 20 76 69 61 20 74 68  er called via th
1221a 65 20 6d 61 63 72 6f 0a 2a 2a 20 77 69 6e 4c 6f  e macro.** winLo
1221b 67 45 72 72 6f 72 28 29 2e 0a 2a 2a 0a 2a 2a 20  gError()..**.** 
1221c 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
1221d 69 6e 76 6f 6b 65 64 20 61 66 74 65 72 20 61 6e  invoked after an
1221e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 69 6e   error occurs in
1221f 20 61 6e 20 4f 53 20 66 75 6e 63 74 69 6f 6e 2e   an OS function.
12220 0a 2a 2a 20 49 74 20 6c 6f 67 73 20 61 20 6d 65  .** It logs a me
12221 73 73 61 67 65 20 75 73 69 6e 67 20 73 71 6c 69  ssage using sqli
12222 74 65 33 5f 6c 6f 67 28 29 20 63 6f 6e 74 61 69  te3_log() contai
12223 6e 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74  ning the current
12224 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 65 72 72   value of.** err
12225 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 69 66 20  or code and, if 
12226 70 6f 73 73 69 62 6c 65 2c 20 74 68 65 20 68 75  possible, the hu
12227 6d 61 6e 2d 72 65 61 64 61 62 6c 65 20 65 71 75  man-readable equ
12228 69 76 61 6c 65 6e 74 20 66 72 6f 6d 20 0a 2a 2a  ivalent from .**
12229 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 2e 0a   FormatMessage..
1222a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
1222b 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20  argument passed 
1222c 74 6f 20 74 68 65 20 6d 61 63 72 6f 20 73 68 6f  to the macro sho
1222d 75 6c 64 20 62 65 20 74 68 65 20 65 72 72 6f 72  uld be the error
1222e 20 63 6f 64 65 20 74 68 61 74 0a 2a 2a 20 77 69   code that.** wi
1222f 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 74  ll be returned t
12230 6f 20 53 51 4c 69 74 65 20 28 65 2e 67 2e 20 53  o SQLite (e.g. S
12231 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45  QLITE_IOERR_DELE
12232 54 45 2c 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  TE, SQLITE_CANTO
12233 50 45 4e 29 2e 20 0a 2a 2a 20 54 68 65 20 74 77  PEN). .** The tw
12234 6f 20 73 75 62 73 65 71 75 65 6e 74 20 61 72 67  o subsequent arg
12235 75 6d 65 6e 74 73 20 73 68 6f 75 6c 64 20 62 65  uments should be
12236 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
12237 20 4f 53 20 66 75 6e 63 74 69 6f 6e 20 74 68 61   OS function tha
12238 74 0a 2a 2a 20 66 61 69 6c 65 64 20 61 6e 64 20  t.** failed and 
12239 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 66  the associated f
1223a 69 6c 65 2d 73 79 73 74 65 6d 20 70 61 74 68 2c  ile-system path,
1223b 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a 23 64 65 66   if any..*/.#def
1223c 69 6e 65 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28  ine winLogError(
1223d 61 2c 62 2c 63 2c 64 29 20 20 20 77 69 6e 4c 6f  a,b,c,d)   winLo
1223e 67 45 72 72 6f 72 41 74 4c 69 6e 65 28 61 2c 62  gErrorAtLine(a,b
1223f 2c 63 2c 64 2c 5f 5f 4c 49 4e 45 5f 5f 29 0a 73  ,c,d,__LINE__).s
12240 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4c 6f 67  tatic int winLog
12241 45 72 72 6f 72 41 74 4c 69 6e 65 28 0a 20 20 69  ErrorAtLine(.  i
12242 6e 74 20 65 72 72 63 6f 64 65 2c 20 20 20 20 20  nt errcode,     
12243 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
12244 2a 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  * SQLite error c
12245 6f 64 65 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6c  ode */.  DWORD l
12246 61 73 74 45 72 72 6e 6f 2c 20 20 20 20 20 20 20  astErrno,       
12247 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69 6e 33           /* Win3
12248 32 20 6c 61 73 74 20 65 72 72 6f 72 20 2a 2f 0a  2 last error */.
12249 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
1224a 75 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  unc,            
1224b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 4f 53 20    /* Name of OS 
1224c 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 66 61  function that fa
1224d 69 6c 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  iled */.  const 
1224e 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20  char *zPath,    
1224f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c            /* Fil
12250 65 20 70 61 74 68 20 61 73 73 6f 63 69 61 74 65  e path associate
12251 64 20 77 69 74 68 20 65 72 72 6f 72 20 2a 2f 0a  d with error */.
12252 20 20 69 6e 74 20 69 4c 69 6e 65 20 20 20 20 20    int iLine     
12253 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12254 20 20 2f 2a 20 53 6f 75 72 63 65 20 6c 69 6e 65    /* Source line
12255 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 65 72   number where er
12256 72 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a  ror occurred */.
12257 29 7b 0a 20 20 63 68 61 72 20 7a 4d 73 67 5b 35  ){.  char zMsg[5
12258 30 30 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  00];            
12259 20 20 20 20 20 2f 2a 20 48 75 6d 61 6e 20 72 65       /* Human re
1225a 61 64 61 62 6c 65 20 65 72 72 6f 72 20 74 65 78  adable error tex
1225b 74 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  t */.  int i;   
1225c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1225d 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
1225e 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 7a 4d 73  ounter */..  zMs
1225f 67 5b 30 5d 20 3d 20 30 3b 0a 20 20 67 65 74 4c  g[0] = 0;.  getL
12260 61 73 74 45 72 72 6f 72 4d 73 67 28 6c 61 73 74  astErrorMsg(last
12261 45 72 72 6e 6f 2c 20 73 69 7a 65 6f 66 28 7a 4d  Errno, sizeof(zM
12262 73 67 29 2c 20 7a 4d 73 67 29 3b 0a 20 20 61 73  sg), zMsg);.  as
12263 73 65 72 74 28 20 65 72 72 63 6f 64 65 21 3d 53  sert( errcode!=S
12264 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66  QLITE_OK );.  if
12265 28 20 7a 50 61 74 68 3d 3d 30 20 29 20 7a 50 61  ( zPath==0 ) zPa
12266 74 68 20 3d 20 22 22 3b 0a 20 20 66 6f 72 28 69  th = "";.  for(i
12267 3d 30 3b 20 7a 4d 73 67 5b 69 5d 20 26 26 20 7a  =0; zMsg[i] && z
12268 4d 73 67 5b 69 5d 21 3d 27 5c 72 27 20 26 26 20  Msg[i]!='\r' && 
12269 7a 4d 73 67 5b 69 5d 21 3d 27 5c 6e 27 3b 20 69  zMsg[i]!='\n'; i
1226a 2b 2b 29 7b 7d 0a 20 20 7a 4d 73 67 5b 69 5d 20  ++){}.  zMsg[i] 
1226b 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6c  = 0;.  sqlite3_l
1226c 6f 67 28 65 72 72 63 6f 64 65 2c 0a 20 20 20 20  og(errcode,.    
1226d 20 20 22 6f 73 5f 77 69 6e 2e 63 3a 25 64 3a 20    "os_win.c:%d: 
1226e 28 25 64 29 20 25 73 28 25 73 29 20 2d 20 25 73  (%d) %s(%s) - %s
1226f 22 2c 0a 20 20 20 20 20 20 69 4c 69 6e 65 2c 20  ",.      iLine, 
12270 6c 61 73 74 45 72 72 6e 6f 2c 20 7a 46 75 6e 63  lastErrno, zFunc
12271 2c 20 7a 50 61 74 68 2c 20 7a 4d 73 67 0a 20 20  , zPath, zMsg.  
12272 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 65 72 72  );..  return err
12273 63 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  code;.}../*.** T
12274 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d  he number of tim
12275 65 73 20 74 68 61 74 20 61 20 52 65 61 64 46 69  es that a ReadFi
12276 6c 65 28 29 2c 20 57 72 69 74 65 46 69 6c 65 28  le(), WriteFile(
12277 29 2c 20 61 6e 64 20 44 65 6c 65 74 65 46 69 6c  ), and DeleteFil
12278 65 28 29 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72  e().** will be r
12279 65 74 72 69 65 64 20 66 6f 6c 6c 6f 77 69 6e 67  etried following
1227a 20 61 20 6c 6f 63 6b 69 6e 67 20 65 72 72 6f 72   a locking error
1227b 20 2d 20 70 72 6f 62 61 62 6c 79 20 63 61 75 73   - probably caus
1227c 65 64 20 62 79 20 0a 2a 2a 20 61 6e 74 69 76 69  ed by .** antivi
1227d 72 75 73 20 73 6f 66 74 77 61 72 65 2e 20 20 41  rus software.  A
1227e 6c 73 6f 20 74 68 65 20 69 6e 69 74 69 61 6c 20  lso the initial 
1227f 64 65 6c 61 79 20 62 65 66 6f 72 65 20 74 68 65  delay before the
12280 20 66 69 72 73 74 20 72 65 74 72 79 2e 0a 2a 2a   first retry..**
12281 20 54 68 65 20 64 65 6c 61 79 20 69 6e 63 72 65   The delay incre
12282 61 73 65 73 20 6c 69 6e 65 61 72 6c 79 20 77 69  ases linearly wi
12283 74 68 20 65 61 63 68 20 72 65 74 72 79 2e 0a 2a  th each retry..*
12284 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
12285 5f 57 49 4e 33 32 5f 49 4f 45 52 52 5f 52 45 54  _WIN32_IOERR_RET
12286 52 59 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  RY.# define SQLI
12287 54 45 5f 57 49 4e 33 32 5f 49 4f 45 52 52 5f 52  TE_WIN32_IOERR_R
12288 45 54 52 59 20 31 30 0a 23 65 6e 64 69 66 0a 23  ETRY 10.#endif.#
12289 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 57 49  ifndef SQLITE_WI
1228a 4e 33 32 5f 49 4f 45 52 52 5f 52 45 54 52 59 5f  N32_IOERR_RETRY_
1228b 44 45 4c 41 59 0a 23 20 64 65 66 69 6e 65 20 53  DELAY.# define S
1228c 51 4c 49 54 45 5f 57 49 4e 33 32 5f 49 4f 45 52  QLITE_WIN32_IOER
1228d 52 5f 52 45 54 52 59 5f 44 45 4c 41 59 20 32 35  R_RETRY_DELAY 25
1228e 0a 23 65 6e 64 69 66 0a 73 74 61 74 69 63 20 69  .#endif.static i
1228f 6e 74 20 77 69 6e 33 32 49 6f 65 72 72 52 65 74  nt win32IoerrRet
12290 72 79 20 3d 20 53 51 4c 49 54 45 5f 57 49 4e 33  ry = SQLITE_WIN3
12291 32 5f 49 4f 45 52 52 5f 52 45 54 52 59 3b 0a 73  2_IOERR_RETRY;.s
12292 74 61 74 69 63 20 69 6e 74 20 77 69 6e 33 32 49  tatic int win32I
12293 6f 65 72 72 52 65 74 72 79 44 65 6c 61 79 20 3d  oerrRetryDelay =
12294 20 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 49 4f   SQLITE_WIN32_IO
12295 45 52 52 5f 52 45 54 52 59 5f 44 45 4c 41 59 3b  ERR_RETRY_DELAY;
12296 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 61 20 52 65 61  ../*.** If a Rea
12297 64 46 69 6c 65 28 29 20 6f 72 20 57 72 69 74 65  dFile() or Write
12298 46 69 6c 65 28 29 20 65 72 72 6f 72 20 6f 63 63  File() error occ
12299 75 72 73 2c 20 69 6e 76 6f 6b 65 20 74 68 69 73  urs, invoke this
1229a 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 74 6f 20 73   routine.** to s
1229b 65 65 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20  ee if it should 
1229c 62 65 20 72 65 74 72 69 65 64 2e 20 20 52 65 74  be retried.  Ret
1229d 75 72 6e 20 54 52 55 45 20 74 6f 20 72 65 74 72  urn TRUE to retr
1229e 79 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45  y.  Return FALSE
1229f 0a 2a 2a 20 74 6f 20 67 69 76 65 20 75 70 20 77  .** to give up w
122a0 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f  ith an error..*/
122a1 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 74 72  .static int retr
122a2 79 49 6f 65 72 72 28 69 6e 74 20 2a 70 6e 52 65  yIoerr(int *pnRe
122a3 74 72 79 2c 20 44 57 4f 52 44 20 2a 70 45 72 72  try, DWORD *pErr
122a4 6f 72 29 7b 0a 20 20 44 57 4f 52 44 20 65 20 3d  or){.  DWORD e =
122a5 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28   osGetLastError(
122a6 29 3b 0a 20 20 69 66 28 20 2a 70 6e 52 65 74 72  );.  if( *pnRetr
122a7 79 3e 3d 77 69 6e 33 32 49 6f 65 72 72 52 65 74  y>=win32IoerrRet
122a8 72 79 20 29 7b 0a 20 20 20 20 69 66 28 20 70 45  ry ){.    if( pE
122a9 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 2a 70  rror ){.      *p
122aa 45 72 72 6f 72 20 3d 20 65 3b 0a 20 20 20 20 7d  Error = e;.    }
122ab 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
122ac 20 7d 0a 20 20 69 66 28 20 65 3d 3d 45 52 52 4f   }.  if( e==ERRO
122ad 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 45 44 20  R_ACCESS_DENIED 
122ae 7c 7c 0a 20 20 20 20 20 20 65 3d 3d 45 52 52 4f  ||.      e==ERRO
122af 52 5f 4c 4f 43 4b 5f 56 49 4f 4c 41 54 49 4f 4e  R_LOCK_VIOLATION
122b0 20 7c 7c 0a 20 20 20 20 20 20 65 3d 3d 45 52 52   ||.      e==ERR
122b1 4f 52 5f 53 48 41 52 49 4e 47 5f 56 49 4f 4c 41  OR_SHARING_VIOLA
122b2 54 49 4f 4e 20 29 7b 0a 20 20 20 20 73 71 6c 69  TION ){.    sqli
122b3 74 65 33 5f 77 69 6e 33 32 5f 73 6c 65 65 70 28  te3_win32_sleep(
122b4 77 69 6e 33 32 49 6f 65 72 72 52 65 74 72 79 44  win32IoerrRetryD
122b5 65 6c 61 79 2a 28 31 2b 2a 70 6e 52 65 74 72 79  elay*(1+*pnRetry
122b6 29 29 3b 0a 20 20 20 20 2b 2b 2a 70 6e 52 65 74  ));.    ++*pnRet
122b7 72 79 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  ry;.    return 1
122b8 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 72 72  ;.  }.  if( pErr
122b9 6f 72 20 29 7b 0a 20 20 20 20 2a 70 45 72 72 6f  or ){.    *pErro
122ba 72 20 3d 20 65 3b 0a 20 20 7d 0a 20 20 72 65 74  r = e;.  }.  ret
122bb 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
122bc 4c 6f 67 20 61 20 49 2f 4f 20 65 72 72 6f 72 20  Log a I/O error 
122bd 72 65 74 72 79 20 65 70 69 73 6f 64 65 2e 0a 2a  retry episode..*
122be 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f  /.static void lo
122bf 67 49 6f 65 72 72 28 69 6e 74 20 6e 52 65 74 72  gIoerr(int nRetr
122c0 79 29 7b 0a 20 20 69 66 28 20 6e 52 65 74 72 79  y){.  if( nRetry
122c1 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
122c2 6c 6f 67 28 53 51 4c 49 54 45 5f 49 4f 45 52 52  log(SQLITE_IOERR
122c3 2c 20 0a 20 20 20 20 20 20 22 64 65 6c 61 79 65  , .      "delaye
122c4 64 20 25 64 6d 73 20 66 6f 72 20 6c 6f 63 6b 2f  d %dms for lock/
122c5 73 68 61 72 69 6e 67 20 63 6f 6e 66 6c 69 63 74  sharing conflict
122c6 22 2c 0a 20 20 20 20 20 20 77 69 6e 33 32 49 6f  ",.      win32Io
122c7 65 72 72 52 65 74 72 79 44 65 6c 61 79 2a 6e 52  errRetryDelay*nR
122c8 65 74 72 79 2a 28 6e 52 65 74 72 79 2b 31 29 2f  etry*(nRetry+1)/
122c9 32 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 7d 0a 0a  2.    );.  }.}..
122ca 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
122cb 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  NCE./***********
122cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
122cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
122ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
122cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
122d0 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e 20 63  * This section c
122d1 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 6f 72  ontains code for
122d2 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a   WinCE only..*/.
122d3 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 20 43 45  /*.** Windows CE
122d4 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61   does not have a
122d5 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 75 6e   localtime() fun
122d6 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61 74  ction.  So creat
122d7 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 75 74  e a.** substitut
122d8 65 2e 0a 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64  e..*/./* #includ
122d9 65 20 3c 74 69 6d 65 2e 68 3e 20 2a 2f 0a 73 74  e <time.h> */.st
122da 72 75 63 74 20 74 6d 20 2a 5f 5f 63 64 65 63 6c  ruct tm *__cdecl
122db 20 6c 6f 63 61 6c 74 69 6d 65 28 63 6f 6e 73 74   localtime(const
122dc 20 74 69 6d 65 5f 74 20 2a 74 29 0a 7b 0a 20 20   time_t *t).{.  
122dd 73 74 61 74 69 63 20 73 74 72 75 63 74 20 74 6d  static struct tm
122de 20 79 3b 0a 20 20 46 49 4c 45 54 49 4d 45 20 75   y;.  FILETIME u
122df 54 6d 2c 20 6c 54 6d 3b 0a 20 20 53 59 53 54 45  Tm, lTm;.  SYSTE
122e0 4d 54 49 4d 45 20 70 54 6d 3b 0a 20 20 73 71 6c  MTIME pTm;.  sql
122e1 69 74 65 33 5f 69 6e 74 36 34 20 74 36 34 3b 0a  ite3_int64 t64;.
122e2 20 20 74 36 34 20 3d 20 2a 74 3b 0a 20 20 74 36    t64 = *t;.  t6
122e3 34 20 3d 20 28 74 36 34 20 2b 20 31 31 36 34 34  4 = (t64 + 11644
122e4 34 37 33 36 30 30 29 2a 31 30 30 30 30 30 30 30  473600)*10000000
122e5 3b 0a 20 20 75 54 6d 2e 64 77 4c 6f 77 44 61 74  ;.  uTm.dwLowDat
122e6 65 54 69 6d 65 20 3d 20 28 44 57 4f 52 44 29 28  eTime = (DWORD)(
122e7 74 36 34 20 26 20 30 78 46 46 46 46 46 46 46 46  t64 & 0xFFFFFFFF
122e8 29 3b 0a 20 20 75 54 6d 2e 64 77 48 69 67 68 44  );.  uTm.dwHighD
122e9 61 74 65 54 69 6d 65 3d 20 28 44 57 4f 52 44 29  ateTime= (DWORD)
122ea 28 74 36 34 20 3e 3e 20 33 32 29 3b 0a 20 20 6f  (t64 >> 32);.  o
122eb 73 46 69 6c 65 54 69 6d 65 54 6f 4c 6f 63 61 6c  sFileTimeToLocal
122ec 46 69 6c 65 54 69 6d 65 28 26 75 54 6d 2c 26 6c  FileTime(&uTm,&l
122ed 54 6d 29 3b 0a 20 20 6f 73 46 69 6c 65 54 69 6d  Tm);.  osFileTim
122ee 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 28 26 6c  eToSystemTime(&l
122ef 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e 74 6d  Tm,&pTm);.  y.tm
122f0 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 59 65 61  _year = pTm.wYea
122f1 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e 74 6d  r - 1900;.  y.tm
122f2 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74  _mon = pTm.wMont
122f3 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 64  h - 1;.  y.tm_wd
122f4 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57  ay = pTm.wDayOfW
122f5 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 79  eek;.  y.tm_mday
122f6 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20 20 79   = pTm.wDay;.  y
122f7 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77  .tm_hour = pTm.w
122f8 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 6e  Hour;.  y.tm_min
122f9 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0a   = pTm.wMinute;.
122fa 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 54 6d    y.tm_sec = pTm
122fb 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65 74 75  .wSecond;.  retu
122fc 72 6e 20 26 79 3b 0a 7d 0a 0a 23 64 65 66 69 6e  rn &y;.}..#defin
122fd 65 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46  e HANDLE_TO_WINF
122fe 49 4c 45 28 61 29 20 28 77 69 6e 46 69 6c 65 2a  ILE(a) (winFile*
122ff 29 26 28 28 63 68 61 72 2a 29 61 29 5b 2d 28 69  )&((char*)a)[-(i
12300 6e 74 29 6f 66 66 73 65 74 6f 66 28 77 69 6e 46  nt)offsetof(winF
12301 69 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a 20 41  ile,h)]../*.** A
12302 63 71 75 69 72 65 20 61 20 6c 6f 63 6b 20 6f 6e  cquire a lock on
12303 20 74 68 65 20 68 61 6e 64 6c 65 20 68 0a 2a 2f   the handle h.*/
12304 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e  .static void win
12305 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 48  ceMutexAcquire(H
12306 41 4e 44 4c 45 20 68 29 7b 0a 20 20 20 44 57 4f  ANDLE h){.   DWO
12307 52 44 20 64 77 45 72 72 3b 0a 20 20 20 64 6f 20  RD dwErr;.   do 
12308 7b 0a 20 20 20 20 20 64 77 45 72 72 20 3d 20 6f  {.     dwErr = o
12309 73 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 4f 62  sWaitForSingleOb
1230a 6a 65 63 74 28 68 2c 20 49 4e 46 49 4e 49 54 45  ject(h, INFINITE
1230b 29 3b 0a 20 20 20 7d 20 77 68 69 6c 65 20 28 64  );.   } while (d
1230c 77 45 72 72 20 21 3d 20 57 41 49 54 5f 4f 42 4a  wErr != WAIT_OBJ
1230d 45 43 54 5f 30 20 26 26 20 64 77 45 72 72 20 21  ECT_0 && dwErr !
1230e 3d 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e 45 44  = WAIT_ABANDONED
1230f 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  );.}./*.** Relea
12310 73 65 20 61 20 6c 6f 63 6b 20 61 63 71 75 69 72  se a lock acquir
12311 65 64 20 62 79 20 77 69 6e 63 65 4d 75 74 65 78  ed by winceMutex
12312 41 63 71 75 69 72 65 28 29 0a 2a 2f 0a 23 64 65  Acquire().*/.#de
12313 66 69 6e 65 20 77 69 6e 63 65 4d 75 74 65 78 52  fine winceMutexR
12314 65 6c 65 61 73 65 28 68 29 20 52 65 6c 65 61 73  elease(h) Releas
12315 65 4d 75 74 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a  eMutex(h)../*.**
12316 20 43 72 65 61 74 65 20 74 68 65 20 6d 75 74 65   Create the mute
12317 78 20 61 6e 64 20 73 68 61 72 65 64 20 6d 65 6d  x and shared mem
12318 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f 63  ory used for loc
12319 6b 69 6e 67 20 69 6e 20 74 68 65 20 66 69 6c 65  king in the file
1231a 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 70  .** descriptor p
1231b 46 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42  File.*/.static B
1231c 4f 4f 4c 20 77 69 6e 63 65 43 72 65 61 74 65 4c  OOL winceCreateL
1231d 6f 63 6b 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ock(const char *
1231e 7a 46 69 6c 65 6e 61 6d 65 2c 20 77 69 6e 46 69  zFilename, winFi
1231f 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 4c 50  le *pFile){.  LP
12320 57 53 54 52 20 7a 54 6f 6b 3b 0a 20 20 4c 50 57  WSTR zTok;.  LPW
12321 53 54 52 20 7a 4e 61 6d 65 3b 0a 20 20 42 4f 4f  STR zName;.  BOO
12322 4c 20 62 49 6e 69 74 20 3d 20 54 52 55 45 3b 0a  L bInit = TRUE;.
12323 0a 20 20 7a 4e 61 6d 65 20 3d 20 75 74 66 38 54  .  zName = utf8T
12324 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61  oUnicode(zFilena
12325 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65  me);.  if( zName
12326 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 6f 75  ==0 ){.    /* ou
12327 74 20 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20  t of memory */. 
12328 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b     return FALSE;
12329 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69  .  }..  /* Initi
1232a 61 6c 69 7a 65 20 74 68 65 20 6c 6f 63 61 6c 20  alize the local 
1232b 6c 6f 63 6b 64 61 74 61 20 2a 2f 0a 20 20 6d 65  lockdata */.  me
1232c 6d 73 65 74 28 26 70 46 69 6c 65 2d 3e 6c 6f 63  mset(&pFile->loc
1232d 61 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 46  al, 0, sizeof(pF
1232e 69 6c 65 2d 3e 6c 6f 63 61 6c 29 29 3b 0a 0a 20  ile->local));.. 
1232f 20 2f 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20   /* Replace the 
12330 62 61 63 6b 73 6c 61 73 68 65 73 20 66 72 6f 6d  backslashes from
12331 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 6e   the filename an
12332 64 20 6c 6f 77 65 72 63 61 73 65 20 69 74 0a 20  d lowercase it. 
12333 20 2a 2a 20 74 6f 20 64 65 72 69 76 65 20 61 20   ** to derive a 
12334 6d 75 74 65 78 20 6e 61 6d 65 2e 20 2a 2f 0a 20  mutex name. */. 
12335 20 7a 54 6f 6b 20 3d 20 6f 73 43 68 61 72 4c 6f   zTok = osCharLo
12336 77 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 66  werW(zName);.  f
12337 6f 72 20 28 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b 2b  or (;*zTok;zTok+
12338 2b 29 7b 0a 20 20 20 20 69 66 20 28 2a 7a 54 6f  +){.    if (*zTo
12339 6b 20 3d 3d 20 27 5c 5c 27 29 20 2a 7a 54 6f 6b  k == '\\') *zTok
1233a 20 3d 20 27 5f 27 3b 0a 20 20 7d 0a 0a 20 20 2f   = '_';.  }..  /
1233b 2a 20 43 72 65 61 74 65 2f 6f 70 65 6e 20 74 68  * Create/open th
1233c 65 20 6e 61 6d 65 64 20 6d 75 74 65 78 20 2a 2f  e named mutex */
1233d 0a 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78  .  pFile->hMutex
1233e 20 3d 20 6f 73 43 72 65 61 74 65 4d 75 74 65 78   = osCreateMutex
1233f 57 28 4e 55 4c 4c 2c 20 46 41 4c 53 45 2c 20 7a  W(NULL, FALSE, z
12340 4e 61 6d 65 29 3b 0a 20 20 69 66 20 28 21 70 46  Name);.  if (!pF
12341 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20  ile->hMutex){.  
12342 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
12343 6e 6f 20 3d 20 6f 73 47 65 74 4c 61 73 74 45 72  no = osGetLastEr
12344 72 6f 72 28 29 3b 0a 20 20 20 20 77 69 6e 4c 6f  ror();.    winLo
12345 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 45 52  gError(SQLITE_ER
12346 52 4f 52 2c 20 70 46 69 6c 65 2d 3e 6c 61 73 74  ROR, pFile->last
12347 45 72 72 6e 6f 2c 20 22 77 69 6e 63 65 43 72 65  Errno, "winceCre
12348 61 74 65 4c 6f 63 6b 31 22 2c 20 7a 46 69 6c 65  ateLock1", zFile
12349 6e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  name);.    sqlit
1234a 65 33 5f 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a  e3_free(zName);.
1234b 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45      return FALSE
1234c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75  ;.  }..  /* Acqu
1234d 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 62 65  ire the mutex be
1234e 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 20  fore continuing 
1234f 2a 2f 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41  */.  winceMutexA
12350 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d  cquire(pFile->hM
12351 75 74 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20 53  utex);.  .  /* S
12352 69 6e 63 65 20 74 68 65 20 6e 61 6d 65 73 20 6f  ince the names o
12353 66 20 6e 61 6d 65 64 20 6d 75 74 65 78 65 73 2c  f named mutexes,
12354 20 73 65 6d 61 70 68 6f 72 65 73 2c 20 66 69 6c   semaphores, fil
12355 65 20 6d 61 70 70 69 6e 67 73 20 65 74 63 20 61  e mappings etc a
12356 72 65 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73 65  re .  ** case-se
12357 6e 73 69 74 69 76 65 2c 20 74 61 6b 65 20 61 64  nsitive, take ad
12358 76 61 6e 74 61 67 65 20 6f 66 20 74 68 61 74 20  vantage of that 
12359 62 79 20 75 70 70 65 72 63 61 73 69 6e 67 20 74  by uppercasing t
1235a 68 65 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20 20  he mutex name.  
1235b 2a 2a 20 61 6e 64 20 75 73 69 6e 67 20 74 68 61  ** and using tha
1235c 74 20 61 73 20 74 68 65 20 73 68 61 72 65 64 20  t as the shared 
1235d 66 69 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d 65  filemapping name
1235e 2e 0a 20 20 2a 2f 0a 20 20 6f 73 43 68 61 72 55  ..  */.  osCharU
1235f 70 70 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20  pperW(zName);.  
12360 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d  pFile->hShared =
12361 20 6f 73 43 72 65 61 74 65 46 69 6c 65 4d 61 70   osCreateFileMap
12362 70 69 6e 67 57 28 49 4e 56 41 4c 49 44 5f 48 41  pingW(INVALID_HA
12363 4e 44 4c 45 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c  NDLE_VALUE, NULL
12364 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
12365 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12366 20 20 20 20 20 20 20 20 20 20 50 41 47 45 5f 52            PAGE_R
12367 45 41 44 57 52 49 54 45 2c 20 30 2c 20 73 69 7a  EADWRITE, 0, siz
12368 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a  eof(winceLock),.
12369 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1236a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1236b 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 29 3b 20          zName); 
1236c 20 0a 0a 20 20 2f 2a 20 53 65 74 20 61 20 66 6c   ..  /* Set a fl
1236d 61 67 20 74 68 61 74 20 69 6e 64 69 63 61 74 65  ag that indicate
1236e 73 20 77 65 27 72 65 20 74 68 65 20 66 69 72 73  s we're the firs
1236f 74 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  t to create the 
12370 6d 65 6d 6f 72 79 20 73 6f 20 69 74 20 0a 20 20  memory so it .  
12371 2a 2a 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d  ** must be zero-
12372 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20  initialized */. 
12373 20 69 66 20 28 6f 73 47 65 74 4c 61 73 74 45 72   if (osGetLastEr
12374 72 6f 72 28 29 20 3d 3d 20 45 52 52 4f 52 5f 41  ror() == ERROR_A
12375 4c 52 45 41 44 59 5f 45 58 49 53 54 53 29 7b 0a  LREADY_EXISTS){.
12376 20 20 20 20 62 49 6e 69 74 20 3d 20 46 41 4c 53      bInit = FALS
12377 45 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  E;.  }..  sqlite
12378 33 5f 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 0a  3_free(zName);..
12379 20 20 2f 2a 20 49 66 20 77 65 20 73 75 63 63 65    /* If we succe
1237a 65 64 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20 74  eded in making t
1237b 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  he shared memory
1237c 20 68 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74 2e   handle, map it.
1237d 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d   */.  if (pFile-
1237e 3e 68 53 68 61 72 65 64 29 7b 0a 20 20 20 20 70  >hShared){.    p
1237f 46 69 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20 28  File->shared = (
12380 77 69 6e 63 65 4c 6f 63 6b 2a 29 6f 73 4d 61 70  winceLock*)osMap
12381 56 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65  ViewOfFile(pFile
12382 2d 3e 68 53 68 61 72 65 64 2c 20 0a 20 20 20 20  ->hShared, .    
12383 20 20 20 20 20 20 20 20 20 46 49 4c 45 5f 4d 41           FILE_MA
12384 50 5f 52 45 41 44 7c 46 49 4c 45 5f 4d 41 50 5f  P_READ|FILE_MAP_
12385 57 52 49 54 45 2c 20 30 2c 20 30 2c 20 73 69 7a  WRITE, 0, 0, siz
12386 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b  eof(winceLock));
12387 0a 20 20 20 20 2f 2a 20 49 66 20 6d 61 70 70 69  .    /* If mappi
12388 6e 67 20 66 61 69 6c 65 64 2c 20 63 6c 6f 73 65  ng failed, close
12389 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f   the shared memo
1238a 72 79 20 68 61 6e 64 6c 65 20 61 6e 64 20 65 72  ry handle and er
1238b 61 73 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66  ase it */.    if
1238c 20 28 21 70 46 69 6c 65 2d 3e 73 68 61 72 65 64   (!pFile->shared
1238d 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
1238e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73 47 65  lastErrno = osGe
1238f 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20  tLastError();.  
12390 20 20 20 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28      winLogError(
12391 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 70 46  SQLITE_ERROR, pF
12392 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c 0a  ile->lastErrno,.
12393 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
12394 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b 32  winceCreateLock2
12395 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  ", zFilename);. 
12396 20 20 20 20 20 6f 73 43 6c 6f 73 65 48 61 6e 64       osCloseHand
12397 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65  le(pFile->hShare
12398 64 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  d);.      pFile-
12399 3e 68 53 68 61 72 65 64 20 3d 20 4e 55 4c 4c 3b  >hShared = NULL;
1239a 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1239b 20 49 66 20 73 68 61 72 65 64 20 6d 65 6d 6f 72   If shared memor
1239c 79 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63  y could not be c
1239d 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63 6c 6f  reated, then clo
1239e 73 65 20 74 68 65 20 6d 75 74 65 78 20 61 6e 64  se the mutex and
1239f 20 66 61 69 6c 20 2a 2f 0a 20 20 69 66 20 28 70   fail */.  if (p
123a0 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 3d  File->hShared ==
123a1 20 4e 55 4c 4c 29 7b 0a 20 20 20 20 77 69 6e 63   NULL){.    winc
123a2 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46  eMutexRelease(pF
123a3 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20  ile->hMutex);.  
123a4 20 20 6f 73 43 6c 6f 73 65 48 61 6e 64 6c 65 28    osCloseHandle(
123a5 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a  pFile->hMutex);.
123a6 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65      pFile->hMute
123a7 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65  x = NULL;.    re
123a8 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a  turn FALSE;.  }.
123a9 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69    .  /* Initiali
123aa 7a 65 20 74 68 65 20 73 68 61 72 65 64 20 6d 65  ze the shared me
123ab 6d 6f 72 79 20 69 66 20 77 65 27 72 65 20 73 75  mory if we're su
123ac 70 70 6f 73 65 64 20 74 6f 20 2a 2f 0a 20 20 69  pposed to */.  i
123ad 66 20 28 62 49 6e 69 74 29 20 7b 0a 20 20 20 20  f (bInit) {.    
123ae 6d 65 6d 73 65 74 28 70 46 69 6c 65 2d 3e 73 68  memset(pFile->sh
123af 61 72 65 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28  ared, 0, sizeof(
123b0 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 7d  winceLock));.  }
123b1 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65  ..  winceMutexRe
123b2 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75  lease(pFile->hMu
123b3 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 54  tex);.  return T
123b4 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  RUE;.}../*.** De
123b5 73 74 72 6f 79 20 74 68 65 20 70 61 72 74 20 6f  stroy the part o
123b6 66 20 77 69 6e 46 69 6c 65 20 74 68 61 74 20 64  f winFile that d
123b7 65 61 6c 73 20 77 69 74 68 20 77 69 6e 63 65 20  eals with wince 
123b8 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 61 74 69 63 20  locks.*/.static 
123b9 76 6f 69 64 20 77 69 6e 63 65 44 65 73 74 72 6f  void winceDestro
123ba 79 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70  yLock(winFile *p
123bb 46 69 6c 65 29 7b 0a 20 20 69 66 20 28 70 46 69  File){.  if (pFi
123bc 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20  le->hMutex){.   
123bd 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68 65 20   /* Acquire the 
123be 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e  mutex */.    win
123bf 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70  ceMutexAcquire(p
123c0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a  File->hMutex);..
123c1 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f      /* The follo
123c2 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73 68 6f 75  wing blocks shou
123c3 6c 64 20 70 72 6f 62 61 62 6c 79 20 61 73 73 65  ld probably asse
123c4 72 74 20 69 6e 20 64 65 62 75 67 20 6d 6f 64 65  rt in debug mode
123c5 2c 20 62 75 74 20 74 68 65 79 0a 20 20 20 20 20  , but they.     
123c6 20 20 61 72 65 20 74 6f 20 63 6c 65 61 6e 75 70    are to cleanup
123c7 20 69 6e 20 63 61 73 65 20 61 6e 79 20 6c 6f 63   in case any loc
123c8 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f 70 65 6e  ks remained open
123c9 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c   */.    if (pFil
123ca 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72  e->local.nReader
123cb 73 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  s){.      pFile-
123cc 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72  >shared->nReader
123cd 73 20 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  s --;.    }.    
123ce 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  if (pFile->local
123cf 2e 62 52 65 73 65 72 76 65 64 29 7b 0a 20 20 20  .bReserved){.   
123d0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
123d1 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41  ->bReserved = FA
123d2 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  LSE;.    }.    i
123d3 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
123d4 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20  bPending){.     
123d5 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
123d6 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45  bPending = FALSE
123d7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28  ;.    }.    if (
123d8 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78  pFile->local.bEx
123d9 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 20 20  clusive){.      
123da 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
123db 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53  Exclusive = FALS
123dc 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  E;.    }..    /*
123dd 20 44 65 2d 72 65 66 65 72 65 6e 63 65 20 61 6e   De-reference an
123de 64 20 63 6c 6f 73 65 20 6f 75 72 20 63 6f 70 79  d close our copy
123df 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6d   of the shared m
123e0 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 2a 2f 0a  emory handle */.
123e1 20 20 20 20 6f 73 55 6e 6d 61 70 56 69 65 77 4f      osUnmapViewO
123e2 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68 61  fFile(pFile->sha
123e3 72 65 64 29 3b 0a 20 20 20 20 6f 73 43 6c 6f 73  red);.    osClos
123e4 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68  eHandle(pFile->h
123e5 53 68 61 72 65 64 29 3b 0a 0a 20 20 20 20 2f 2a  Shared);..    /*
123e6 20 44 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6d   Done with the m
123e7 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63  utex */.    winc
123e8 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46  eMutexRelease(pF
123e9 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 20 20 20  ile->hMutex);   
123ea 20 0a 20 20 20 20 6f 73 43 6c 6f 73 65 48 61 6e   .    osCloseHan
123eb 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65  dle(pFile->hMute
123ec 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68  x);.    pFile->h
123ed 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20  Mutex = NULL;.  
123ee 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69  }.}../* .** An i
123ef 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
123f0 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 28 29 20   the LockFile() 
123f1 41 50 49 20 6f 66 20 57 69 6e 64 6f 77 73 20 66  API of Windows f
123f2 6f 72 20 43 45 0a 2a 2f 0a 73 74 61 74 69 63 20  or CE.*/.static 
123f3 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69  BOOL winceLockFi
123f4 6c 65 28 0a 20 20 4c 50 48 41 4e 44 4c 45 20 70  le(.  LPHANDLE p
123f5 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64  hFile,.  DWORD d
123f6 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a  wFileOffsetLow,.
123f7 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66    DWORD dwFileOf
123f8 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52  fsetHigh,.  DWOR
123f9 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  D nNumberOfBytes
123fa 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f  ToLockLow,.  DWO
123fb 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  RD nNumberOfByte
123fc 73 54 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20  sToLockHigh.){. 
123fd 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
123fe 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46  = HANDLE_TO_WINF
123ff 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42  ILE(phFile);.  B
12400 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41  OOL bReturn = FA
12401 4c 53 45 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50  LSE;..  UNUSED_P
12402 41 52 41 4d 45 54 45 52 28 64 77 46 69 6c 65 4f  ARAMETER(dwFileO
12403 66 66 73 65 74 48 69 67 68 29 3b 0a 20 20 55 4e  ffsetHigh);.  UN
12404 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e  USED_PARAMETER(n
12405 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
12406 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 69 66 20  ockHigh);..  if 
12407 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29  (!pFile->hMutex)
12408 20 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 20   return TRUE;.  
12409 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72  winceMutexAcquir
1240a 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29  e(pFile->hMutex)
1240b 3b 0a 0a 20 20 2f 2a 20 57 61 6e 74 69 6e 67 20  ;..  /* Wanting 
1240c 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
1240d 6b 3f 20 2a 2f 0a 20 20 69 66 20 28 64 77 46 69  k? */.  if (dwFi
1240e 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28  leOffsetLow == (
1240f 44 57 4f 52 44 29 53 48 41 52 45 44 5f 46 49 52  DWORD)SHARED_FIR
12410 53 54 0a 20 20 20 20 20 20 20 26 26 20 6e 4e 75  ST.       && nNu
12411 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63  mberOfBytesToLoc
12412 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53  kLow == (DWORD)S
12413 48 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20  HARED_SIZE){.   
12414 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72   if (pFile->shar
12415 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20  ed->nReaders == 
12416 30 20 26 26 20 70 46 69 6c 65 2d 3e 73 68 61 72  0 && pFile->shar
12417 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d  ed->bExclusive =
12418 3d 20 30 29 7b 0a 20 20 20 20 20 20 20 70 46 69  = 0){.       pFi
12419 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63  le->shared->bExc
1241a 6c 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20  lusive = TRUE;. 
1241b 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
1241c 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 3d 20  al.bExclusive = 
1241d 54 52 55 45 3b 0a 20 20 20 20 20 20 20 62 52 65  TRUE;.       bRe
1241e 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20  turn = TRUE;.   
1241f 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e   }.  }..  /* Wan
12420 74 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f  t a read-only lo
12421 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66  ck? */.  else if
12422 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f   (dwFileOffsetLo
12423 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52  w == (DWORD)SHAR
12424 45 44 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20  ED_FIRST &&.    
12425 20 20 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66         nNumberOf
12426 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d  BytesToLockLow =
12427 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46  = 1){.    if (pF
12428 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78  ile->shared->bEx
12429 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b 0a 20  clusive == 0){. 
1242a 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
1242b 6c 2e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20  l.nReaders ++;. 
1242c 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e       if (pFile->
1242d 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d  local.nReaders =
1242e 3d 20 31 29 7b 0a 20 20 20 20 20 20 20 20 70 46  = 1){.        pF
1242f 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65  ile->shared->nRe
12430 61 64 65 72 73 20 2b 2b 3b 0a 20 20 20 20 20 20  aders ++;.      
12431 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20  }.      bReturn 
12432 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20  = TRUE;.    }.  
12433 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 70  }..  /* Want a p
12434 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f 20 2a 2f 0a  ending lock? */.
12435 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c    else if (dwFil
12436 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44  eOffsetLow == (D
12437 57 4f 52 44 29 50 45 4e 44 49 4e 47 5f 42 59 54  WORD)PENDING_BYT
12438 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79  E && nNumberOfBy
12439 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20  tesToLockLow == 
1243a 31 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f  1){.    /* If no
1243b 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61   pending lock ha
1243c 73 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 2c  s been acquired,
1243d 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 69 74   then acquire it
1243e 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c   */.    if (pFil
1243f 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64  e->shared->bPend
12440 69 6e 67 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20  ing == 0) {.    
12441 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
12442 3e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45  >bPending = TRUE
12443 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ;.      pFile->l
12444 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20  ocal.bPending = 
12445 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74  TRUE;.      bRet
12446 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
12447 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74  }.  }..  /* Want
12448 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b   a reserved lock
12449 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28  ? */.  else if (
1244a 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
1244b 3d 3d 20 28 44 57 4f 52 44 29 52 45 53 45 52 56  == (DWORD)RESERV
1244c 45 44 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62  ED_BYTE && nNumb
1244d 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c  erOfBytesToLockL
1244e 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66  ow == 1){.    if
1244f 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d   (pFile->shared-
12450 3e 62 52 65 73 65 72 76 65 64 20 3d 3d 20 30 29  >bReserved == 0)
12451 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e   {.      pFile->
12452 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65  shared->bReserve
12453 64 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20  d = TRUE;.      
12454 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65  pFile->local.bRe
12455 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20  served = TRUE;. 
12456 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54       bReturn = T
12457 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  RUE;.    }.  }..
12458 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65    winceMutexRele
12459 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65  ase(pFile->hMute
1245a 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 52 65  x);.  return bRe
1245b 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  turn;.}../*.** A
1245c 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
1245d 20 6f 66 20 74 68 65 20 55 6e 6c 6f 63 6b 46 69   of the UnlockFi
1245e 6c 65 20 41 50 49 20 6f 66 20 57 69 6e 64 6f 77  le API of Window
1245f 73 20 66 6f 72 20 43 45 0a 2a 2f 0a 73 74 61 74  s for CE.*/.stat
12460 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 55 6e 6c  ic BOOL winceUnl
12461 6f 63 6b 46 69 6c 65 28 0a 20 20 4c 50 48 41 4e  ockFile(.  LPHAN
12462 44 4c 45 20 70 68 46 69 6c 65 2c 0a 20 20 44 57  DLE phFile,.  DW
12463 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74  ORD dwFileOffset
12464 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 64 77 46  Low,.  DWORD dwF
12465 69 6c 65 4f 66 66 73 65 74 48 69 67 68 2c 0a 20  ileOffsetHigh,. 
12466 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66   DWORD nNumberOf
12467 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77  BytesToUnlockLow
12468 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65  ,.  DWORD nNumbe
12469 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b  rOfBytesToUnlock
1246a 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69 6c  High.){.  winFil
1246b 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44 4c  e *pFile = HANDL
1246c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68 46  E_TO_WINFILE(phF
1246d 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52 65  ile);.  BOOL bRe
1246e 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a 20  turn = FALSE;.. 
1246f 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
12470 52 28 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69  R(dwFileOffsetHi
12471 67 68 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  gh);.  UNUSED_PA
12472 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f  RAMETER(nNumberO
12473 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 48 69  fBytesToUnlockHi
12474 67 68 29 3b 0a 0a 20 20 69 66 20 28 21 70 46 69  gh);..  if (!pFi
12475 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75  le->hMutex) retu
12476 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65  rn TRUE;.  wince
12477 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69  MutexAcquire(pFi
12478 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20  le->hMutex);..  
12479 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72  /* Releasing a r
1247a 65 61 64 65 72 20 6c 6f 63 6b 20 6f 72 20 61 6e  eader lock or an
1247b 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
1247c 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f  */.  if (dwFileO
1247d 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f  ffsetLow == (DWO
1247e 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 29  RD)SHARED_FIRST)
1247f 7b 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65 20  {.    /* Did we 
12480 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76  have an exclusiv
12481 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 69  e lock? */.    i
12482 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
12483 62 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20  bExclusive){.   
12484 20 20 20 61 73 73 65 72 74 28 6e 4e 75 6d 62 65     assert(nNumbe
12485 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b  rOfBytesToUnlock
12486 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48  Low == (DWORD)SH
12487 41 52 45 44 5f 53 49 5a 45 29 3b 0a 20 20 20 20  ARED_SIZE);.    
12488 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62    pFile->local.b
12489 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53  Exclusive = FALS
1248a 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  E;.      pFile->
1248b 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69  shared->bExclusi
1248c 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20  ve = FALSE;.    
1248d 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45    bReturn = TRUE
1248e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1248f 44 69 64 20 77 65 20 6a 75 73 74 20 68 61 76 65  Did we just have
12490 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 3f 20   a reader lock? 
12491 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28  */.    else if (
12492 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65  pFile->local.nRe
12493 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20 61 73  aders){.      as
12494 73 65 72 74 28 6e 4e 75 6d 62 65 72 4f 66 42 79  sert(nNumberOfBy
12495 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d  tesToUnlockLow =
12496 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f  = (DWORD)SHARED_
12497 53 49 5a 45 20 7c 7c 20 6e 4e 75 6d 62 65 72 4f  SIZE || nNumberO
12498 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f  fBytesToUnlockLo
12499 77 20 3d 3d 20 31 29 3b 0a 20 20 20 20 20 20 70  w == 1);.      p
1249a 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61  File->local.nRea
1249b 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 69  ders --;.      i
1249c 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
1249d 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 29 0a 20  nReaders == 0). 
1249e 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70       {.        p
1249f 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52  File->shared->nR
124a0 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20  eaders --;.     
124a1 20 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e   }.      bReturn
124a2 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
124a3 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69   }..  /* Releasi
124a4 6e 67 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63  ng a pending loc
124a5 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28  k */.  else if (
124a6 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
124a7 3d 3d 20 28 44 57 4f 52 44 29 50 45 4e 44 49 4e  == (DWORD)PENDIN
124a8 47 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65  G_BYTE && nNumbe
124a9 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b  rOfBytesToUnlock
124aa 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69  Low == 1){.    i
124ab 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
124ac 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20  bPending){.     
124ad 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50   pFile->local.bP
124ae 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a  ending = FALSE;.
124af 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
124b0 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20  red->bPending = 
124b1 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65  FALSE;.      bRe
124b2 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20  turn = TRUE;.   
124b3 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 6c 65   }.  }.  /* Rele
124b4 61 73 69 6e 67 20 61 20 72 65 73 65 72 76 65 64  asing a reserved
124b5 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20   lock */.  else 
124b6 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74  if (dwFileOffset
124b7 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 52 45  Low == (DWORD)RE
124b8 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e  SERVED_BYTE && n
124b9 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55  NumberOfBytesToU
124ba 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a  nlockLow == 1){.
124bb 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c      if (pFile->l
124bc 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29 20  ocal.bReserved) 
124bd 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
124be 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d  ocal.bReserved =
124bf 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46   FALSE;.      pF
124c0 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65  ile->shared->bRe
124c1 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a  served = FALSE;.
124c2 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
124c3 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TRUE;.    }.  }.
124c4 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c  .  winceMutexRel
124c5 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74  ease(pFile->hMut
124c6 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 52  ex);.  return bR
124c7 65 74 75 72 6e 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45  eturn;.}./*.** E
124c8 6e 64 20 6f 66 20 74 68 65 20 73 70 65 63 69 61  nd of the specia
124c9 6c 20 63 6f 64 65 20 66 6f 72 20 77 69 6e 63 65  l code for wince
124ca 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
124cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
124cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
124cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
124ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
124cf 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
124d0 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a  _OS_WINCE */../*
124d1 0a 2a 2a 20 4c 6f 63 6b 20 61 20 66 69 6c 65 20  .** Lock a file 
124d2 72 65 67 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  region..*/.stati
124d3 63 20 42 4f 4f 4c 20 77 69 6e 4c 6f 63 6b 46 69  c BOOL winLockFi
124d4 6c 65 28 0a 20 20 4c 50 48 41 4e 44 4c 45 20 70  le(.  LPHANDLE p
124d5 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 66  hFile,.  DWORD f
124d6 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 20 6f 66  lags,.  DWORD of
124d7 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f 52 44  fsetLow,.  DWORD
124d8 20 6f 66 66 73 65 74 48 69 67 68 2c 0a 20 20 44   offsetHigh,.  D
124d9 57 4f 52 44 20 6e 75 6d 42 79 74 65 73 4c 6f 77  WORD numBytesLow
124da 2c 0a 20 20 44 57 4f 52 44 20 6e 75 6d 42 79 74  ,.  DWORD numByt
124db 65 73 48 69 67 68 0a 29 7b 0a 23 69 66 20 53 51  esHigh.){.#if SQ
124dc 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20  LITE_OS_WINCE.  
124dd 2f 2a 0a 20 20 2a 2a 20 4e 4f 54 45 3a 20 57 69  /*.  ** NOTE: Wi
124de 6e 64 6f 77 73 20 43 45 20 69 73 20 68 61 6e 64  ndows CE is hand
124df 6c 65 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20  led differently 
124e0 68 65 72 65 20 64 75 65 20 69 74 73 20 6c 61 63  here due its lac
124e1 6b 20 6f 66 20 74 68 65 20 57 69 6e 33 32 0a 20  k of the Win32. 
124e2 20 2a 2a 20 20 20 20 20 20 20 41 50 49 20 4c 6f   **       API Lo
124e3 63 6b 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72  ckFile..  */.  r
124e4 65 74 75 72 6e 20 77 69 6e 63 65 4c 6f 63 6b 46  eturn winceLockF
124e5 69 6c 65 28 70 68 46 69 6c 65 2c 20 6f 66 66 73  ile(phFile, offs
124e6 65 74 4c 6f 77 2c 20 6f 66 66 73 65 74 48 69 67  etLow, offsetHig
124e7 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  h,.             
124e8 20 20 20 20 20 20 20 20 20 20 6e 75 6d 42 79 74            numByt
124e9 65 73 4c 6f 77 2c 20 6e 75 6d 42 79 74 65 73 48  esLow, numBytesH
124ea 69 67 68 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66  igh);.#else.  if
124eb 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
124ec 4f 56 45 52 4c 41 50 50 45 44 20 6f 76 6c 70 3b  OVERLAPPED ovlp;
124ed 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 6f 76 6c  .    memset(&ovl
124ee 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4f 56 45  p, 0, sizeof(OVE
124ef 52 4c 41 50 50 45 44 29 29 3b 0a 20 20 20 20 6f  RLAPPED));.    o
124f0 76 6c 70 2e 4f 66 66 73 65 74 20 3d 20 6f 66 66  vlp.Offset = off
124f1 73 65 74 4c 6f 77 3b 0a 20 20 20 20 6f 76 6c 70  setLow;.    ovlp
124f2 2e 4f 66 66 73 65 74 48 69 67 68 20 3d 20 6f 66  .OffsetHigh = of
124f3 66 73 65 74 48 69 67 68 3b 0a 20 20 20 20 72 65  fsetHigh;.    re
124f4 74 75 72 6e 20 6f 73 4c 6f 63 6b 46 69 6c 65 45  turn osLockFileE
124f5 78 28 2a 70 68 46 69 6c 65 2c 20 66 6c 61 67 73  x(*phFile, flags
124f6 2c 20 30 2c 20 6e 75 6d 42 79 74 65 73 4c 6f 77  , 0, numBytesLow
124f7 2c 20 6e 75 6d 42 79 74 65 73 48 69 67 68 2c 20  , numBytesHigh, 
124f8 26 6f 76 6c 70 29 3b 0a 20 20 7d 65 6c 73 65 7b  &ovlp);.  }else{
124f9 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 4c 6f  .    return osLo
124fa 63 6b 46 69 6c 65 28 2a 70 68 46 69 6c 65 2c 20  ckFile(*phFile, 
124fb 6f 66 66 73 65 74 4c 6f 77 2c 20 6f 66 66 73 65  offsetLow, offse
124fc 74 48 69 67 68 2c 20 6e 75 6d 42 79 74 65 73 4c  tHigh, numBytesL
124fd 6f 77 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ow,.            
124fe 20 20 20 20 20 20 20 20 20 20 6e 75 6d 42 79 74            numByt
124ff 65 73 48 69 67 68 29 3b 0a 20 20 7d 0a 23 65 6e  esHigh);.  }.#en
12500 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c  dif.}../*.** Unl
12501 6f 63 6b 20 61 20 66 69 6c 65 20 72 65 67 69 6f  ock a file regio
12502 6e 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 42 4f  n.. */.static BO
12503 4f 4c 20 77 69 6e 55 6e 6c 6f 63 6b 46 69 6c 65  OL winUnlockFile
12504 28 0a 20 20 4c 50 48 41 4e 44 4c 45 20 70 68 46  (.  LPHANDLE phF
12505 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 6f 66 66  ile,.  DWORD off
12506 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20  setLow,.  DWORD 
12507 6f 66 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57  offsetHigh,.  DW
12508 4f 52 44 20 6e 75 6d 42 79 74 65 73 4c 6f 77 2c  ORD numBytesLow,
12509 0a 20 20 44 57 4f 52 44 20 6e 75 6d 42 79 74 65  .  DWORD numByte
1250a 73 48 69 67 68 0a 29 7b 0a 23 69 66 20 53 51 4c  sHigh.){.#if SQL
1250b 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 2f  ITE_OS_WINCE.  /
1250c 2a 0a 20 20 2a 2a 20 4e 4f 54 45 3a 20 57 69 6e  *.  ** NOTE: Win
1250d 64 6f 77 73 20 43 45 20 69 73 20 68 61 6e 64 6c  dows CE is handl
1250e 65 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20 68  ed differently h
1250f 65 72 65 20 64 75 65 20 69 74 73 20 6c 61 63 6b  ere due its lack
12510 20 6f 66 20 74 68 65 20 57 69 6e 33 32 0a 20 20   of the Win32.  
12511 2a 2a 20 20 20 20 20 20 20 41 50 49 20 55 6e 6c  **       API Unl
12512 6f 63 6b 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20  ockFile..  */.  
12513 72 65 74 75 72 6e 20 77 69 6e 63 65 55 6e 6c 6f  return winceUnlo
12514 63 6b 46 69 6c 65 28 70 68 46 69 6c 65 2c 20 6f  ckFile(phFile, o
12515 66 66 73 65 74 4c 6f 77 2c 20 6f 66 66 73 65 74  ffsetLow, offset
12516 48 69 67 68 2c 0a 20 20 20 20 20 20 20 20 20 20  High,.          
12517 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e                 n
12518 75 6d 42 79 74 65 73 4c 6f 77 2c 20 6e 75 6d 42  umBytesLow, numB
12519 79 74 65 73 48 69 67 68 29 3b 0a 23 65 6c 73 65  ytesHigh);.#else
1251a 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b  .  if( isNT() ){
1251b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50 45 44 20  .    OVERLAPPED 
1251c 6f 76 6c 70 3b 0a 20 20 20 20 6d 65 6d 73 65 74  ovlp;.    memset
1251d 28 26 6f 76 6c 70 2c 20 30 2c 20 73 69 7a 65 6f  (&ovlp, 0, sizeo
1251e 66 28 4f 56 45 52 4c 41 50 50 45 44 29 29 3b 0a  f(OVERLAPPED));.
1251f 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 20      ovlp.Offset 
12520 3d 20 6f 66 66 73 65 74 4c 6f 77 3b 0a 20 20 20  = offsetLow;.   
12521 20 6f 76 6c 70 2e 4f 66 66 73 65 74 48 69 67 68   ovlp.OffsetHigh
12522 20 3d 20 6f 66 66 73 65 74 48 69 67 68 3b 0a 20   = offsetHigh;. 
12523 20 20 20 72 65 74 75 72 6e 20 6f 73 55 6e 6c 6f     return osUnlo
12524 63 6b 46 69 6c 65 45 78 28 2a 70 68 46 69 6c 65  ckFileEx(*phFile
12525 2c 20 30 2c 20 6e 75 6d 42 79 74 65 73 4c 6f 77  , 0, numBytesLow
12526 2c 20 6e 75 6d 42 79 74 65 73 48 69 67 68 2c 20  , numBytesHigh, 
12527 26 6f 76 6c 70 29 3b 0a 20 20 7d 65 6c 73 65 7b  &ovlp);.  }else{
12528 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 55 6e  .    return osUn
12529 6c 6f 63 6b 46 69 6c 65 28 2a 70 68 46 69 6c 65  lockFile(*phFile
1252a 2c 20 6f 66 66 73 65 74 4c 6f 77 2c 20 6f 66 66  , offsetLow, off
1252b 73 65 74 48 69 67 68 2c 20 6e 75 6d 42 79 74 65  setHigh, numByte
1252c 73 4c 6f 77 2c 0a 20 20 20 20 20 20 20 20 20 20  sLow,.          
1252d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 75                nu
1252e 6d 42 79 74 65 73 48 69 67 68 29 3b 0a 20 20 7d  mBytesHigh);.  }
1252f 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 2a 2a 2a  .#endif.}../****
12530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12531 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12532 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12533 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12534 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65  *********.** The
12535 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72   next group of r
12536 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e  outines implemen
12537 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64  t the I/O method
12538 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62  s specified.** b
12539 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f  y the sqlite3_io
1253a 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e  _methods object.
1253b 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1253c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1253d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1253e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1253f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
12540 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 4d 69 63  ../*.** Some Mic
12541 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73  rosoft compilers
12542 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66 69 6e   lack this defin
12543 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ition..*/.#ifnde
12544 66 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49  f INVALID_SET_FI
12545 4c 45 5f 50 4f 49 4e 54 45 52 0a 23 20 64 65 66  LE_POINTER.# def
12546 69 6e 65 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f  ine INVALID_SET_
12547 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 28 28 44  FILE_POINTER ((D
12548 57 4f 52 44 29 2d 31 29 0a 23 65 6e 64 69 66 0a  WORD)-1).#endif.
12549 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20  ./*.** Move the 
1254a 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e  current position
1254b 20 6f 66 20 74 68 65 20 66 69 6c 65 20 68 61 6e   of the file han
1254c 64 6c 65 20 70 61 73 73 65 64 20 61 73 20 74 68  dle passed as th
1254d 65 20 66 69 72 73 74 20 0a 2a 2a 20 61 72 67 75  e first .** argu
1254e 6d 65 6e 74 20 74 6f 20 6f 66 66 73 65 74 20 69  ment to offset i
1254f 4f 66 66 73 65 74 20 77 69 74 68 69 6e 20 74 68  Offset within th
12550 65 20 66 69 6c 65 2e 20 49 66 20 73 75 63 63 65  e file. If succe
12551 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 30 2e  ssful, return 0.
12552 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20   .** Otherwise, 
12553 73 65 74 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45  set pFile->lastE
12554 72 72 6e 6f 20 61 6e 64 20 72 65 74 75 72 6e 20  rrno and return 
12555 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61  non-zero..*/.sta
12556 74 69 63 20 69 6e 74 20 73 65 65 6b 57 69 6e 46  tic int seekWinF
12557 69 6c 65 28 77 69 6e 46 69 6c 65 20 2a 70 46 69  ile(winFile *pFi
12558 6c 65 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  le, sqlite3_int6
12559 34 20 69 4f 66 66 73 65 74 29 7b 0a 23 69 66 20  4 iOffset){.#if 
1255a 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54  !SQLITE_OS_WINRT
1255b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74  .  LONG upperBit
1255c 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
1255d 20 20 20 2f 2a 20 4d 6f 73 74 20 73 69 67 2e 20     /* Most sig. 
1255e 33 32 20 62 69 74 73 20 6f 66 20 6e 65 77 20 6f  32 bits of new o
1255f 66 66 73 65 74 20 2a 2f 0a 20 20 4c 4f 4e 47 20  ffset */.  LONG 
12560 6c 6f 77 65 72 42 69 74 73 3b 20 20 20 20 20 20  lowerBits;      
12561 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
12562 61 73 74 20 73 69 67 2e 20 33 32 20 62 69 74 73  ast sig. 32 bits
12563 20 6f 66 20 6e 65 77 20 6f 66 66 73 65 74 20 2a   of new offset *
12564 2f 0a 20 20 44 57 4f 52 44 20 64 77 52 65 74 3b  /.  DWORD dwRet;
12565 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12566 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74      /* Value ret
12567 75 72 6e 65 64 20 62 79 20 53 65 74 46 69 6c 65  urned by SetFile
12568 50 6f 69 6e 74 65 72 28 29 20 2a 2f 0a 20 20 44  Pointer() */.  D
12569 57 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 20  WORD lastErrno; 
1256a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1256b 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64  * Value returned
1256c 20 62 79 20 47 65 74 4c 61 73 74 45 72 72 6f 72   by GetLastError
1256d 28 29 20 2a 2f 0a 0a 20 20 75 70 70 65 72 42 69  () */..  upperBi
1256e 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 69 4f 66  ts = (LONG)((iOf
1256f 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66  fset>>32) & 0x7f
12570 66 66 66 66 66 66 29 3b 0a 20 20 6c 6f 77 65 72  ffffff);.  lower
12571 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 69 4f  Bits = (LONG)(iO
12572 66 66 73 65 74 20 26 20 30 78 66 66 66 66 66 66  ffset & 0xffffff
12573 66 66 29 3b 0a 0a 20 20 2f 2a 20 41 50 49 20 6f  ff);..  /* API o
12574 64 64 69 74 79 3a 20 49 66 20 73 75 63 63 65 73  ddity: If succes
12575 73 66 75 6c 2c 20 53 65 74 46 69 6c 65 50 6f 69  sful, SetFilePoi
12576 6e 74 65 72 28 29 20 72 65 74 75 72 6e 73 20 61  nter() returns a
12577 20 64 77 6f 72 64 20 0a 20 20 2a 2a 20 63 6f 6e   dword .  ** con
12578 74 61 69 6e 69 6e 67 20 74 68 65 20 6c 6f 77 65  taining the lowe
12579 72 20 33 32 2d 62 69 74 73 20 6f 66 20 74 68 65  r 32-bits of the
1257a 20 6e 65 77 20 66 69 6c 65 2d 6f 66 66 73 65 74   new file-offset
1257b 2e 20 4f 72 2c 20 69 66 20 69 74 20 66 61 69 6c  . Or, if it fail
1257c 73 2c 0a 20 20 2a 2a 20 69 74 20 72 65 74 75 72  s,.  ** it retur
1257d 6e 73 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46  ns INVALID_SET_F
1257e 49 4c 45 5f 50 4f 49 4e 54 45 52 2e 20 48 6f 77  ILE_POINTER. How
1257f 65 76 65 72 20 61 63 63 6f 72 64 69 6e 67 20 74  ever according t
12580 6f 20 4d 53 44 4e 2c 20 0a 20 20 2a 2a 20 49 4e  o MSDN, .  ** IN
12581 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50  VALID_SET_FILE_P
12582 4f 49 4e 54 45 52 20 6d 61 79 20 61 6c 73 6f 20  OINTER may also 
12583 62 65 20 61 20 76 61 6c 69 64 20 6e 65 77 20 6f  be a valid new o
12584 66 66 73 65 74 2e 20 53 6f 20 74 6f 20 64 65 74  ffset. So to det
12585 65 72 6d 69 6e 65 20 0a 20 20 2a 2a 20 77 68 65  ermine .  ** whe
12586 74 68 65 72 20 61 6e 20 65 72 72 6f 72 20 68 61  ther an error ha
12587 73 20 61 63 74 75 61 6c 6c 79 20 6f 63 63 75 72  s actually occur
12588 65 64 2c 20 69 74 20 69 73 20 61 6c 73 6f 20 6e  ed, it is also n
12589 65 63 65 73 73 61 72 79 20 74 6f 20 63 61 6c 6c  ecessary to call
1258a 20 0a 20 20 2a 2a 20 47 65 74 4c 61 73 74 45 72   .  ** GetLastEr
1258b 72 6f 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 64 77  ror()..  */.  dw
1258c 52 65 74 20 3d 20 6f 73 53 65 74 46 69 6c 65 50  Ret = osSetFileP
1258d 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c  ointer(pFile->h,
1258e 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70   lowerBits, &upp
1258f 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47  erBits, FILE_BEG
12590 49 4e 29 3b 0a 0a 20 20 69 66 28 20 28 64 77 52  IN);..  if( (dwR
12591 65 74 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f  et==INVALID_SET_
12592 46 49 4c 45 5f 50 4f 49 4e 54 45 52 0a 20 20 20  FILE_POINTER.   
12593 20 20 20 26 26 20 28 28 6c 61 73 74 45 72 72 6e     && ((lastErrn
12594 6f 20 3d 20 6f 73 47 65 74 4c 61 73 74 45 72 72  o = osGetLastErr
12595 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 29  or())!=NO_ERROR)
12596 29 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  ) ){.    pFile->
12597 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 61 73 74  lastErrno = last
12598 45 72 72 6e 6f 3b 0a 20 20 20 20 77 69 6e 4c 6f  Errno;.    winLo
12599 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 49 4f  gError(SQLITE_IO
1259a 45 52 52 5f 53 45 45 4b 2c 20 70 46 69 6c 65 2d  ERR_SEEK, pFile-
1259b 3e 6c 61 73 74 45 72 72 6e 6f 2c 0a 20 20 20 20  >lastErrno,.    
1259c 20 20 20 20 20 20 20 20 20 22 73 65 65 6b 57 69           "seekWi
1259d 6e 46 69 6c 65 22 2c 20 70 46 69 6c 65 2d 3e 7a  nFile", pFile->z
1259e 50 61 74 68 29 3b 0a 20 20 20 20 72 65 74 75 72  Path);.    retur
1259f 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  n 1;.  }..  retu
125a0 72 6e 20 30 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a  rn 0;.#else.  /*
125a1 0a 20 20 2a 2a 20 53 61 6d 65 20 61 73 20 61 62  .  ** Same as ab
125a2 6f 76 65 2c 20 65 78 63 65 70 74 20 74 68 61 74  ove, except that
125a3 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   this implementa
125a4 74 69 6f 6e 20 77 6f 72 6b 73 20 66 6f 72 20 57  tion works for W
125a5 69 6e 52 54 2e 0a 20 20 2a 2f 0a 0a 20 20 4c 41  inRT..  */..  LA
125a6 52 47 45 5f 49 4e 54 45 47 45 52 20 78 3b 20 20  RGE_INTEGER x;  
125a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
125a8 20 54 68 65 20 6e 65 77 20 6f 66 66 73 65 74 20   The new offset 
125a9 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 74 3b 20  */.  BOOL bRet; 
125aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
125ab 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65       /* Value re
125ac 74 75 72 6e 65 64 20 62 79 20 53 65 74 46 69 6c  turned by SetFil
125ad 65 50 6f 69 6e 74 65 72 45 78 28 29 20 2a 2f 0a  ePointerEx() */.
125ae 0a 20 20 78 2e 51 75 61 64 50 61 72 74 20 3d 20  .  x.QuadPart = 
125af 69 4f 66 66 73 65 74 3b 0a 20 20 62 52 65 74 20  iOffset;.  bRet 
125b0 3d 20 6f 73 53 65 74 46 69 6c 65 50 6f 69 6e 74  = osSetFilePoint
125b1 65 72 45 78 28 70 46 69 6c 65 2d 3e 68 2c 20 78  erEx(pFile->h, x
125b2 2c 20 30 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29  , 0, FILE_BEGIN)
125b3 3b 0a 0a 20 20 69 66 28 21 62 52 65 74 29 7b 0a  ;..  if(!bRet){.
125b4 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
125b5 72 72 6e 6f 20 3d 20 6f 73 47 65 74 4c 61 73 74  rrno = osGetLast
125b6 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 77 69 6e  Error();.    win
125b7 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f  LogError(SQLITE_
125b8 49 4f 45 52 52 5f 53 45 45 4b 2c 20 70 46 69 6c  IOERR_SEEK, pFil
125b9 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c 0a 20 20  e->lastErrno,.  
125ba 20 20 20 20 20 20 20 20 20 20 20 22 73 65 65 6b             "seek
125bb 57 69 6e 46 69 6c 65 22 2c 20 70 46 69 6c 65 2d  WinFile", pFile-
125bc 3e 7a 50 61 74 68 29 3b 0a 20 20 20 20 72 65 74  >zPath);.    ret
125bd 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 65  urn 1;.  }..  re
125be 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 7d  turn 0;.#endif.}
125bf 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  ../*.** Close a 
125c0 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69  file..**.** It i
125c1 73 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 20  s reported that 
125c2 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6c  an attempt to cl
125c3 6f 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 67  ose a handle mig
125c4 68 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20  ht sometimes.** 
125c5 66 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20 61  fail.  This is a
125c6 20 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 62   very unreasonab
125c7 6c 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20 57  le result, but W
125c8 69 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69  indows is notori
125c9 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67  ous.** for being
125ca 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f   unreasonable so
125cb 20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20   I do not doubt 
125cc 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 68 61  that it might ha
125cd 70 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65  ppen.  If.** the
125ce 20 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 65   close fails, we
125cf 20 70 61 75 73 65 20 66 6f 72 20 31 30 30 20 6d   pause for 100 m
125d0 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20  illiseconds and 
125d1 74 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a  try again.  As.*
125d2 2a 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f  * many as MX_CLO
125d3 53 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65 6d  SE_ATTEMPT attem
125d4 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 65  pts to close the
125d5 20 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 65   handle are made
125d6 20 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e   before.** givin
125d7 67 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69  g up and returni
125d8 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a  ng an error..*/.
125d9 23 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45  #define MX_CLOSE
125da 5f 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74 69  _ATTEMPT 3.stati
125db 63 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73  c int winClose(s
125dc 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
125dd 7b 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 20  {.  int rc, cnt 
125de 3d 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a  = 0;.  winFile *
125df 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
125e0 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28  *)id;..  assert(
125e1 20 69 64 21 3d 30 20 29 3b 0a 23 69 66 6e 64 65   id!=0 );.#ifnde
125e2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41  f SQLITE_OMIT_WA
125e3 4c 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  L.  assert( pFil
125e4 65 2d 3e 70 53 68 6d 3d 3d 30 20 29 3b 0a 23 65  e->pShm==0 );.#e
125e5 6e 64 69 66 0a 20 20 4f 53 54 52 41 43 45 28 28  ndif.  OSTRACE((
125e6 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46  "CLOSE %d\n", pF
125e7 69 6c 65 2d 3e 68 29 29 3b 0a 20 20 64 6f 7b 0a  ile->h));.  do{.
125e8 20 20 20 20 72 63 20 3d 20 6f 73 43 6c 6f 73 65      rc = osClose
125e9 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 29  Handle(pFile->h)
125ea 3b 0a 20 20 20 20 2f 2a 20 53 69 6d 75 6c 61 74  ;.    /* Simulat
125eb 65 49 4f 45 72 72 6f 72 28 20 72 63 3d 30 3b 20  eIOError( rc=0; 
125ec 63 6e 74 3d 4d 58 5f 43 4c 4f 53 45 5f 41 54 54  cnt=MX_CLOSE_ATT
125ed 45 4d 50 54 3b 20 29 3b 20 2a 2f 0a 20 20 7d 77  EMPT; ); */.  }w
125ee 68 69 6c 65 28 20 72 63 3d 3d 30 20 26 26 20 2b  hile( rc==0 && +
125ef 2b 63 6e 74 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f  +cnt < MX_CLOSE_
125f0 41 54 54 45 4d 50 54 20 26 26 20 28 73 71 6c 69  ATTEMPT && (sqli
125f1 74 65 33 5f 77 69 6e 33 32 5f 73 6c 65 65 70 28  te3_win32_sleep(
125f2 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 69 66 20  100), 1) );.#if 
125f3 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a  SQLITE_OS_WINCE.
125f4 23 64 65 66 69 6e 65 20 57 49 4e 43 45 5f 44 45  #define WINCE_DE
125f5 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20  LETION_ATTEMPTS 
125f6 33 0a 20 20 77 69 6e 63 65 44 65 73 74 72 6f 79  3.  winceDestroy
125f7 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 69  Lock(pFile);.  i
125f8 66 28 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74  f( pFile->zDelet
125f9 65 4f 6e 43 6c 6f 73 65 20 29 7b 0a 20 20 20 20  eOnClose ){.    
125fa 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20  int cnt = 0;.   
125fb 20 77 68 69 6c 65 28 0a 20 20 20 20 20 20 20 20   while(.        
125fc 20 20 20 6f 73 44 65 6c 65 74 65 46 69 6c 65 57     osDeleteFileW
125fd 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f  (pFile->zDeleteO
125fe 6e 43 6c 6f 73 65 29 3d 3d 30 0a 20 20 20 20 20  nClose)==0.     
125ff 20 20 20 26 26 20 6f 73 47 65 74 46 69 6c 65 41     && osGetFileA
12600 74 74 72 69 62 75 74 65 73 57 28 70 46 69 6c 65  ttributesW(pFile
12601 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65  ->zDeleteOnClose
12602 29 21 3d 30 78 66 66 66 66 66 66 66 66 20 0a 20  )!=0xffffffff . 
12603 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20         && cnt++ 
12604 3c 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e  < WINCE_DELETION
12605 5f 41 54 54 45 4d 50 54 53 0a 20 20 20 20 29 7b  _ATTEMPTS.    ){
12606 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .       sqlite3_
12607 77 69 6e 33 32 5f 73 6c 65 65 70 28 31 30 30 29  win32_sleep(100)
12608 3b 20 20 2f 2a 20 57 61 69 74 20 61 20 6c 69 74  ;  /* Wait a lit
12609 74 6c 65 20 62 65 66 6f 72 65 20 74 72 79 69 6e  tle before tryin
1260a 67 20 61 67 61 69 6e 20 2a 2f 0a 20 20 20 20 7d  g again */.    }
1260b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
1260c 65 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65  e(pFile->zDelete
1260d 4f 6e 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23 65  OnClose);.  }.#e
1260e 6e 64 69 66 0a 20 20 4f 53 54 52 41 43 45 28 28  ndif.  OSTRACE((
1260f 22 43 4c 4f 53 45 20 25 64 20 25 73 5c 6e 22 2c  "CLOSE %d %s\n",
12610 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 3f 20   pFile->h, rc ? 
12611 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29  "ok" : "failed")
12612 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20  );.  if( rc ){. 
12613 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 4e 55     pFile->h = NU
12614 4c 4c 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e 43 6f  LL;.  }.  OpenCo
12615 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 72 65 74  unter(-1);.  ret
12616 75 72 6e 20 72 63 20 3f 20 53 51 4c 49 54 45 5f  urn rc ? SQLITE_
12617 4f 4b 0a 20 20 20 20 20 20 20 20 20 20 20 20 3a  OK.            :
12618 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c   winLogError(SQL
12619 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 2c  ITE_IOERR_CLOSE,
1261a 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28   osGetLastError(
1261b 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
1261c 20 20 20 20 20 20 20 20 20 20 20 20 20 22 77 69               "wi
1261d 6e 43 6c 6f 73 65 22 2c 20 70 46 69 6c 65 2d 3e  nClose", pFile->
1261e 7a 50 61 74 68 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  zPath);.}../*.**
1261f 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20   Read data from 
12620 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75  a file into a bu
12621 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51  ffer.  Return SQ
12622 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a  LITE_OK if all.*
12623 2a 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61  * bytes were rea
12624 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61  d successfully a
12625 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  nd SQLITE_IOERR 
12626 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73  if anything goes
12627 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74  .** wrong..*/.st
12628 61 74 69 63 20 69 6e 74 20 77 69 6e 52 65 61 64  atic int winRead
12629 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
1262a 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 2f   *id,          /
1262b 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66  * File to read f
1262c 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  rom */.  void *p
1262d 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  Buf,            
1262e 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 6f 6e      /* Write con
1262f 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 73 20 62  tent into this b
12630 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61  uffer */.  int a
12631 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mt,             
12632 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
12633 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64  of bytes to read
12634 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
12635 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 20  t64 offset      
12636 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e   /* Begin readin
12637 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74  g at this offset
12638 20 2a 2f 0a 29 7b 0a 23 69 66 20 21 53 51 4c 49   */.){.#if !SQLI
12639 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 4f 56  TE_OS_WINCE.  OV
1263a 45 52 4c 41 50 50 45 44 20 6f 76 65 72 6c 61 70  ERLAPPED overlap
1263b 70 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ped;          /*
1263c 20 54 68 65 20 6f 66 66 73 65 74 20 66 6f 72 20   The offset for 
1263d 52 65 61 64 46 69 6c 65 2e 20 2a 2f 0a 23 65 6e  ReadFile. */.#en
1263e 64 69 66 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  dif.  winFile *p
1263f 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
12640 29 69 64 3b 20 20 2f 2a 20 66 69 6c 65 20 68 61  )id;  /* file ha
12641 6e 64 6c 65 20 2a 2f 0a 20 20 44 57 4f 52 44 20  ndle */.  DWORD 
12642 6e 52 65 61 64 3b 20 20 20 20 20 20 20 20 20 20  nRead;          
12643 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
12644 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74  ber of bytes act
12645 75 61 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20  ually read from 
12646 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  file */.  int nR
12647 65 74 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20  etry = 0;       
12648 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
12649 62 65 72 20 6f 66 20 72 65 74 72 79 73 20 2a 2f  ber of retrys */
1264a 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d  ..  assert( id!=
1264b 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  0 );.  SimulateI
1264c 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51  OError(return SQ
1264d 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 29  LITE_IOERR_READ)
1264e 3b 0a 20 20 4f 53 54 52 41 43 45 28 28 22 52 45  ;.  OSTRACE(("RE
1264f 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22  AD %d lock=%d\n"
12650 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c  , pFile->h, pFil
12651 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 0a  e->locktype));..
12652 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
12653 4e 43 45 0a 20 20 69 66 28 20 73 65 65 6b 57 69  NCE.  if( seekWi
12654 6e 46 69 6c 65 28 70 46 69 6c 65 2c 20 6f 66 66  nFile(pFile, off
12655 73 65 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75  set) ){.    retu
12656 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a  rn SQLITE_FULL;.
12657 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 21 6f 73    }.  while( !os
12658 52 65 61 64 46 69 6c 65 28 70 46 69 6c 65 2d 3e  ReadFile(pFile->
12659 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 6e  h, pBuf, amt, &n
1265a 52 65 61 64 2c 20 30 29 20 29 7b 0a 23 65 6c 73  Read, 0) ){.#els
1265b 65 0a 20 20 6d 65 6d 73 65 74 28 26 6f 76 65 72  e.  memset(&over
1265c 6c 61 70 70 65 64 2c 20 30 2c 20 73 69 7a 65 6f  lapped, 0, sizeo
1265d 66 28 4f 56 45 52 4c 41 50 50 45 44 29 29 3b 0a  f(OVERLAPPED));.
1265e 20 20 6f 76 65 72 6c 61 70 70 65 64 2e 4f 66 66    overlapped.Off
1265f 73 65 74 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66  set = (LONG)(off
12660 73 65 74 20 26 20 30 78 66 66 66 66 66 66 66 66  set & 0xffffffff
12661 29 3b 0a 20 20 6f 76 65 72 6c 61 70 70 65 64 2e  );.  overlapped.
12662 4f 66 66 73 65 74 48 69 67 68 20 3d 20 28 4c 4f  OffsetHigh = (LO
12663 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e 33 32 29  NG)((offset>>32)
12664 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a   & 0x7fffffff);.
12665 20 20 77 68 69 6c 65 28 20 21 6f 73 52 65 61 64    while( !osRead
12666 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70  File(pFile->h, p
12667 42 75 66 2c 20 61 6d 74 2c 20 26 6e 52 65 61 64  Buf, amt, &nRead
12668 2c 20 26 6f 76 65 72 6c 61 70 70 65 64 29 20 26  , &overlapped) &
12669 26 0a 20 20 20 20 20 20 20 20 20 6f 73 47 65 74  &.         osGet
1266a 4c 61 73 74 45 72 72 6f 72 28 29 21 3d 45 52 52  LastError()!=ERR
1266b 4f 52 5f 48 41 4e 44 4c 45 5f 45 4f 46 20 29 7b  OR_HANDLE_EOF ){
1266c 0a 23 65 6e 64 69 66 0a 20 20 20 20 44 57 4f 52  .#endif.    DWOR
1266d 44 20 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20  D lastErrno;.   
1266e 20 69 66 28 20 72 65 74 72 79 49 6f 65 72 72 28   if( retryIoerr(
1266f 26 6e 52 65 74 72 79 2c 20 26 6c 61 73 74 45 72  &nRetry, &lastEr
12670 72 6e 6f 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  rno) ) continue;
12671 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  .    pFile->last
12672 45 72 72 6e 6f 20 3d 20 6c 61 73 74 45 72 72 6e  Errno = lastErrn
12673 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 77 69  o;.    return wi
12674 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45  nLogError(SQLITE
12675 5f 49 4f 45 52 52 5f 52 45 41 44 2c 20 70 46 69  _IOERR_READ, pFi
12676 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c 0a 20  le->lastErrno,. 
12677 20 20 20 20 20 20 20 20 20 20 20 20 22 77 69 6e              "win
12678 52 65 61 64 22 2c 20 70 46 69 6c 65 2d 3e 7a 50  Read", pFile->zP
12679 61 74 68 29 3b 0a 20 20 7d 0a 20 20 6c 6f 67 49  ath);.  }.  logI
1267a 6f 65 72 72 28 6e 52 65 74 72 79 29 3b 0a 20 20  oerr(nRetry);.  
1267b 69 66 28 20 6e 52 65 61 64 3c 28 44 57 4f 52 44  if( nRead<(DWORD
1267c 29 61 6d 74 20 29 7b 0a 20 20 20 20 2f 2a 20 55  )amt ){.    /* U
1267d 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74  nread parts of t
1267e 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62  he buffer must b
1267f 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f  e zero-filled */
12680 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63  .    memset(&((c
12681 68 61 72 2a 29 70 42 75 66 29 5b 6e 52 65 61 64  har*)pBuf)[nRead
12682 5d 2c 20 30 2c 20 61 6d 74 2d 6e 52 65 61 64 29  ], 0, amt-nRead)
12683 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
12684 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f  ITE_IOERR_SHORT_
12685 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  READ;.  }..  ret
12686 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
12687 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61  ../*.** Write da
12688 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72  ta from a buffer
12689 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52   into a file.  R
1268a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
1268b 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72  on success.** or
1268c 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f   some other erro
1268d 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72  r code on failur
1268e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1268f 20 77 69 6e 57 72 69 74 65 28 0a 20 20 73 71 6c   winWrite(.  sql
12690 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20  ite3_file *id,  
12691 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12692 46 69 6c 65 20 74 6f 20 77 72 69 74 65 20 69 6e  File to write in
12693 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  to */.  const vo
12694 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20  id *pBuf,       
12695 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62          /* The b
12696 79 74 65 73 20 74 6f 20 62 65 20 77 72 69 74 74  ytes to be writt
12697 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c  en */.  int amt,
12698 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12699 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1269a 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72  r of bytes to wr
1269b 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ite */.  sqlite3
1269c 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20  _int64 offset   
1269d 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
1269e 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65  et into the file
1269f 20 74 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e   to begin writin
126a0 67 20 61 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  g at */.){.  int
126a1 20 72 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20   rc = 0;        
126a2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
126a3 54 72 75 65 20 69 66 20 65 72 72 6f 72 20 68 61  True if error ha
126a4 73 20 6f 63 63 75 72 65 64 2c 20 65 6c 73 65 20  s occured, else 
126a5 66 61 6c 73 65 20 2a 2f 0a 20 20 77 69 6e 46 69  false */.  winFi
126a6 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
126a7 46 69 6c 65 2a 29 69 64 3b 20 20 2f 2a 20 46 69  File*)id;  /* Fi
126a8 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69  le handle */.  i
126a9 6e 74 20 6e 52 65 74 72 79 20 3d 20 30 3b 20 20  nt nRetry = 0;  
126aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
126ab 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 74 72  * Number of retr
126ac 69 65 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ies */..  assert
126ad 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 61 73 73  ( amt>0 );.  ass
126ae 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
126af 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
126b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
126b1 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20 53 69  ERR_WRITE);.  Si
126b2 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
126b3 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
126b4 45 5f 46 55 4c 4c 29 3b 0a 0a 20 20 4f 53 54 52  E_FULL);..  OSTR
126b5 41 43 45 28 28 22 57 52 49 54 45 20 25 64 20 6c  ACE(("WRITE %d l
126b6 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  ock=%d\n", pFile
126b7 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ->h, pFile->lock
126b8 74 79 70 65 29 29 3b 0a 0a 23 69 66 20 53 51 4c  type));..#if SQL
126b9 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 72  ITE_OS_WINCE.  r
126ba 63 20 3d 20 73 65 65 6b 57 69 6e 46 69 6c 65 28  c = seekWinFile(
126bb 70 46 69 6c 65 2c 20 6f 66 66 73 65 74 29 3b 0a  pFile, offset);.
126bc 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 23    if( rc==0 ){.#
126bd 65 6c 73 65 0a 20 20 7b 0a 23 65 6e 64 69 66 0a  else.  {.#endif.
126be 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57  #if !SQLITE_OS_W
126bf 49 4e 43 45 0a 20 20 20 20 4f 56 45 52 4c 41 50  INCE.    OVERLAP
126c0 50 45 44 20 6f 76 65 72 6c 61 70 70 65 64 3b 20  PED overlapped; 
126c1 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 66         /* The of
126c2 66 73 65 74 20 66 6f 72 20 57 72 69 74 65 46 69  fset for WriteFi
126c3 6c 65 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  le. */.#endif.  
126c4 20 20 75 38 20 2a 61 52 65 6d 20 3d 20 28 75 38    u8 *aRem = (u8
126c5 20 2a 29 70 42 75 66 3b 20 20 20 20 20 20 20 20   *)pBuf;        
126c6 2f 2a 20 44 61 74 61 20 79 65 74 20 74 6f 20 62  /* Data yet to b
126c7 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 20  e written */.   
126c8 20 69 6e 74 20 6e 52 65 6d 20 3d 20 61 6d 74 3b   int nRem = amt;
126c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
126ca 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
126cb 73 20 79 65 74 20 74 6f 20 62 65 20 77 72 69 74  s yet to be writ
126cc 74 65 6e 20 2a 2f 0a 20 20 20 20 44 57 4f 52 44  ten */.    DWORD
126cd 20 6e 57 72 69 74 65 3b 20 20 20 20 20 20 20 20   nWrite;        
126ce 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
126cf 73 20 77 72 69 74 74 65 6e 20 62 79 20 65 61 63  s written by eac
126d0 68 20 57 72 69 74 65 46 69 6c 65 28 29 20 63 61  h WriteFile() ca
126d1 6c 6c 20 2a 2f 0a 20 20 20 20 44 57 4f 52 44 20  ll */.    DWORD 
126d2 6c 61 73 74 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45  lastErrno = NO_E
126d3 52 52 4f 52 3b 20 20 20 2f 2a 20 56 61 6c 75 65  RROR;   /* Value
126d4 20 72 65 74 75 72 6e 65 64 20 62 79 20 47 65 74   returned by Get
126d5 4c 61 73 74 45 72 72 6f 72 28 29 20 2a 2f 0a 0a  LastError() */..
126d6 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57  #if !SQLITE_OS_W
126d7 49 4e 43 45 0a 20 20 20 20 6d 65 6d 73 65 74 28  INCE.    memset(
126d8 26 6f 76 65 72 6c 61 70 70 65 64 2c 20 30 2c 20  &overlapped, 0, 
126d9 73 69 7a 65 6f 66 28 4f 56 45 52 4c 41 50 50 45  sizeof(OVERLAPPE
126da 44 29 29 3b 0a 20 20 20 20 6f 76 65 72 6c 61 70  D));.    overlap
126db 70 65 64 2e 4f 66 66 73 65 74 20 3d 20 28 4c 4f  ped.Offset = (LO
126dc 4e 47 29 28 6f 66 66 73 65 74 20 26 20 30 78 66  NG)(offset & 0xf
126dd 66 66 66 66 66 66 66 29 3b 0a 20 20 20 20 6f 76  fffffff);.    ov
126de 65 72 6c 61 70 70 65 64 2e 4f 66 66 73 65 74 48  erlapped.OffsetH
126df 69 67 68 20 3d 20 28 4c 4f 4e 47 29 28 28 6f 66  igh = (LONG)((of
126e0 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66  fset>>32) & 0x7f
126e1 66 66 66 66 66 66 29 3b 0a 23 65 6e 64 69 66 0a  ffffff);.#endif.
126e2 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 52 65 6d  .    while( nRem
126e3 3e 30 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45  >0 ){.#if SQLITE
126e4 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 20 20  _OS_WINCE.      
126e5 69 66 28 20 21 6f 73 57 72 69 74 65 46 69 6c 65  if( !osWriteFile
126e6 28 70 46 69 6c 65 2d 3e 68 2c 20 61 52 65 6d 2c  (pFile->h, aRem,
126e7 20 6e 52 65 6d 2c 20 26 6e 57 72 69 74 65 2c 20   nRem, &nWrite, 
126e8 30 29 20 29 7b 0a 23 65 6c 73 65 0a 20 20 20 20  0) ){.#else.    
126e9 20 20 69 66 28 20 21 6f 73 57 72 69 74 65 46 69    if( !osWriteFi
126ea 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 61 52 65  le(pFile->h, aRe
126eb 6d 2c 20 6e 52 65 6d 2c 20 26 6e 57 72 69 74 65  m, nRem, &nWrite
126ec 2c 20 26 6f 76 65 72 6c 61 70 70 65 64 29 20 29  , &overlapped) )
126ed 7b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  {.#endif.       
126ee 20 69 66 28 20 72 65 74 72 79 49 6f 65 72 72 28   if( retryIoerr(
126ef 26 6e 52 65 74 72 79 2c 20 26 6c 61 73 74 45 72  &nRetry, &lastEr
126f0 72 6e 6f 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  rno) ) continue;
126f1 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
126f2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
126f3 73 65 72 74 28 20 6e 57 72 69 74 65 3d 3d 30 20  sert( nWrite==0 
126f4 7c 7c 20 6e 57 72 69 74 65 3c 3d 28 44 57 4f 52  || nWrite<=(DWOR
126f5 44 29 6e 52 65 6d 20 29 3b 0a 20 20 20 20 20 20  D)nRem );.      
126f6 69 66 28 20 6e 57 72 69 74 65 3d 3d 30 20 7c 7c  if( nWrite==0 ||
126f7 20 6e 57 72 69 74 65 3e 28 44 57 4f 52 44 29 6e   nWrite>(DWORD)n
126f8 52 65 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 6c  Rem ){.        l
126f9 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73 47 65 74  astErrno = osGet
126fa 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20  LastError();.   
126fb 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
126fc 20 20 7d 0a 23 69 66 20 21 53 51 4c 49 54 45 5f    }.#if !SQLITE_
126fd 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 20 20 6f  OS_WINCE.      o
126fe 66 66 73 65 74 20 2b 3d 20 6e 57 72 69 74 65 3b  ffset += nWrite;
126ff 0a 20 20 20 20 20 20 6f 76 65 72 6c 61 70 70 65  .      overlappe
12700 64 2e 4f 66 66 73 65 74 20 3d 20 28 4c 4f 4e 47  d.Offset = (LONG
12701 29 28 6f 66 66 73 65 74 20 26 20 30 78 66 66 66  )(offset & 0xfff
12702 66 66 66 66 66 29 3b 0a 20 20 20 20 20 20 6f 76  fffff);.      ov
12703 65 72 6c 61 70 70 65 64 2e 4f 66 66 73 65 74 48  erlapped.OffsetH
12704 69 67 68 20 3d 20 28 4c 4f 4e 47 29 28 28 6f 66  igh = (LONG)((of
12705 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66  fset>>32) & 0x7f
12706 66 66 66 66 66 66 29 3b 0a 23 65 6e 64 69 66 0a  ffffff);.#endif.
12707 20 20 20 20 20 20 61 52 65 6d 20 2b 3d 20 6e 57        aRem += nW
12708 72 69 74 65 3b 0a 20 20 20 20 20 20 6e 52 65 6d  rite;.      nRem
12709 20 2d 3d 20 6e 57 72 69 74 65 3b 0a 20 20 20 20   -= nWrite;.    
1270a 7d 0a 20 20 20 20 69 66 28 20 6e 52 65 6d 3e 30  }.    if( nRem>0
1270b 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d   ){.      pFile-
1270c 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 61 73  >lastErrno = las
1270d 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63  tErrno;.      rc
1270e 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 1;.    }.  }.
1270f 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
12710 20 69 66 28 20 20 20 28 20 70 46 69 6c 65 2d 3e   if(   ( pFile->
12711 6c 61 73 74 45 72 72 6e 6f 3d 3d 45 52 52 4f 52  lastErrno==ERROR
12712 5f 48 41 4e 44 4c 45 5f 44 49 53 4b 5f 46 55 4c  _HANDLE_DISK_FUL
12713 4c 20 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 20  L ).       || ( 
12714 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
12715 3d 3d 45 52 52 4f 52 5f 44 49 53 4b 5f 46 55 4c  ==ERROR_DISK_FUL
12716 4c 20 29 29 7b 0a 20 20 20 20 20 20 72 65 74 75  L )){.      retu
12717 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a  rn SQLITE_FULL;.
12718 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
12719 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c   winLogError(SQL
1271a 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 2c  ITE_IOERR_WRITE,
1271b 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
1271c 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  o,.             
1271d 22 77 69 6e 57 72 69 74 65 22 2c 20 70 46 69 6c  "winWrite", pFil
1271e 65 2d 3e 7a 50 61 74 68 29 3b 0a 20 20 7d 65 6c  e->zPath);.  }el
1271f 73 65 7b 0a 20 20 20 20 6c 6f 67 49 6f 65 72 72  se{.    logIoerr
12720 28 6e 52 65 74 72 79 29 3b 0a 20 20 7d 0a 20 20  (nRetry);.  }.  
12721 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
12722 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63  ;.}../*.** Trunc
12723 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65  ate an open file
12724 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20   to a specified 
12725 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69  size.*/.static i
12726 6e 74 20 77 69 6e 54 72 75 6e 63 61 74 65 28 73  nt winTruncate(s
12727 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
12728 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e   sqlite3_int64 n
12729 42 79 74 65 29 7b 0a 20 20 77 69 6e 46 69 6c 65  Byte){.  winFile
1272a 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
1272b 6c 65 2a 29 69 64 3b 20 20 2f 2a 20 46 69 6c 65  le*)id;  /* File
1272c 20 68 61 6e 64 6c 65 20 6f 62 6a 65 63 74 20 2a   handle object *
1272d 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  /.  int rc = SQL
1272e 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20  ITE_OK;         
1272f 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
12730 64 65 20 66 6f 72 20 74 68 69 73 20 66 75 6e 63  de for this func
12731 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72  tion */..  asser
12732 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 4f  t( pFile );..  O
12733 53 54 52 41 43 45 28 28 22 54 52 55 4e 43 41 54  STRACE(("TRUNCAT
12734 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46  E %d %lld\n", pF
12735 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 29 3b  ile->h, nByte));
12736 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
12737 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45  or(return SQLITE
12738 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 29  _IOERR_TRUNCATE)
12739 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 75  ;..  /* If the u
1273a 73 65 72 20 68 61 73 20 63 6f 6e 66 69 67 75 72  ser has configur
1273b 65 64 20 61 20 63 68 75 6e 6b 2d 73 69 7a 65 20  ed a chunk-size 
1273c 66 6f 72 20 74 68 69 73 20 66 69 6c 65 2c 20 74  for this file, t
1273d 72 75 6e 63 61 74 65 20 74 68 65 0a 20 20 2a 2a  runcate the.  **
1273e 20 66 69 6c 65 20 73 6f 20 74 68 61 74 20 69 74   file so that it
1273f 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 6e 20   consists of an 
12740 69 6e 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f  integer number o
12741 66 20 63 68 75 6e 6b 73 20 28 69 2e 65 2e 20 74  f chunks (i.e. t
12742 68 65 0a 20 20 2a 2a 20 61 63 74 75 61 6c 20 66  he.  ** actual f
12743 69 6c 65 20 73 69 7a 65 20 61 66 74 65 72 20 74  ile size after t
12744 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 6d 61 79  he operation may
12745 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20   be larger than 
12746 74 68 65 20 72 65 71 75 65 73 74 65 64 0a 20 20  the requested.  
12747 2a 2a 20 73 69 7a 65 29 2e 0a 20 20 2a 2f 0a 20  ** size)..  */. 
12748 20 69 66 28 20 70 46 69 6c 65 2d 3e 73 7a 43 68   if( pFile->szCh
12749 75 6e 6b 3e 30 20 29 7b 0a 20 20 20 20 6e 42 79  unk>0 ){.    nBy
1274a 74 65 20 3d 20 28 28 6e 42 79 74 65 20 2b 20 70  te = ((nByte + p
1274b 46 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 20 2d 20  File->szChunk - 
1274c 31 29 2f 70 46 69 6c 65 2d 3e 73 7a 43 68 75 6e  1)/pFile->szChun
1274d 6b 29 20 2a 20 70 46 69 6c 65 2d 3e 73 7a 43 68  k) * pFile->szCh
1274e 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  unk;.  }..  /* S
1274f 65 74 45 6e 64 4f 66 46 69 6c 65 28 29 20 72 65  etEndOfFile() re
12750 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 77  turns non-zero w
12751 68 65 6e 20 73 75 63 63 65 73 73 66 75 6c 2c 20  hen successful, 
12752 6f 72 20 7a 65 72 6f 20 77 68 65 6e 20 69 74 20  or zero when it 
12753 66 61 69 6c 73 2e 20 2a 2f 0a 20 20 69 66 28 20  fails. */.  if( 
12754 73 65 65 6b 57 69 6e 46 69 6c 65 28 70 46 69 6c  seekWinFile(pFil
12755 65 2c 20 6e 42 79 74 65 29 20 29 7b 0a 20 20 20  e, nByte) ){.   
12756 20 72 63 20 3d 20 77 69 6e 4c 6f 67 45 72 72 6f   rc = winLogErro
12757 72 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54  r(SQLITE_IOERR_T
12758 52 55 4e 43 41 54 45 2c 20 70 46 69 6c 65 2d 3e  RUNCATE, pFile->
12759 6c 61 73 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20  lastErrno,.     
1275a 20 20 20 20 20 20 20 20 22 77 69 6e 54 72 75 6e          "winTrun
1275b 63 61 74 65 31 22 2c 20 70 46 69 6c 65 2d 3e 7a  cate1", pFile->z
1275c 50 61 74 68 29 3b 0a 20 20 7d 65 6c 73 65 20 69  Path);.  }else i
1275d 66 28 20 30 3d 3d 6f 73 53 65 74 45 6e 64 4f 66  f( 0==osSetEndOf
1275e 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 29 20 29  File(pFile->h) )
1275f 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
12760 74 45 72 72 6e 6f 20 3d 20 6f 73 47 65 74 4c 61  tErrno = osGetLa
12761 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72  stError();.    r
12762 63 20 3d 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28  c = winLogError(
12763 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55  SQLITE_IOERR_TRU
12764 4e 43 41 54 45 2c 20 70 46 69 6c 65 2d 3e 6c 61  NCATE, pFile->la
12765 73 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20 20  stErrno,.       
12766 20 20 20 20 20 20 22 77 69 6e 54 72 75 6e 63 61        "winTrunca
12767 74 65 32 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 61  te2", pFile->zPa
12768 74 68 29 3b 0a 20 20 7d 0a 0a 20 20 4f 53 54 52  th);.  }..  OSTR
12769 41 43 45 28 28 22 54 52 55 4e 43 41 54 45 20 25  ACE(("TRUNCATE %
1276a 64 20 25 6c 6c 64 20 25 73 5c 6e 22 2c 20 70 46  d %lld %s\n", pF
1276b 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 2c 20 72  ile->h, nByte, r
1276c 63 20 3f 20 22 66 61 69 6c 65 64 22 20 3a 20 22  c ? "failed" : "
1276d 6f 6b 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20  ok"));.  return 
1276e 72 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  rc;.}..#ifdef SQ
1276f 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20  LITE_TEST./*.** 
12770 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72  Count the number
12771 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e   of fullsyncs an
12772 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20  d normal syncs. 
12773 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f   This is used to
12774 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79   test.** that sy
12775 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63  ncs and fullsync
12776 73 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61  s are occuring a
12777 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65  t the right time
12778 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
12779 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e   int sqlite3_syn
1277a 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c  c_count = 0;.SQL
1277b 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1277c 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75  te3_fullsync_cou
1277d 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
1277e 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
1277f 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20  all writes to a 
12780 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20  particular file 
12781 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f  are committed to
12782 20 64 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63   disk..*/.static
12783 20 69 6e 74 20 77 69 6e 53 79 6e 63 28 73 71 6c   int winSync(sql
12784 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
12785 6e 74 20 66 6c 61 67 73 29 7b 0a 23 69 66 6e 64  nt flags){.#ifnd
12786 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e  ef SQLITE_NO_SYN
12787 43 0a 20 20 2f 2a 0a 20 20 2a 2a 20 55 73 65 64  C.  /*.  ** Used
12788 20 6f 6e 6c 79 20 77 68 65 6e 20 53 51 4c 49 54   only when SQLIT
12789 45 5f 4e 4f 5f 53 59 4e 43 20 69 73 20 6e 6f 74  E_NO_SYNC is not
1278a 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 2a 2f 0a   defined..   */.
1278b 20 20 42 4f 4f 4c 20 72 63 3b 0a 23 65 6e 64 69    BOOL rc;.#endi
1278c 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e  f.#if !defined(N
1278d 44 45 42 55 47 29 20 7c 7c 20 21 64 65 66 69 6e  DEBUG) || !defin
1278e 65 64 28 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e  ed(SQLITE_NO_SYN
1278f 43 29 20 7c 7c 20 5c 0a 20 20 20 20 28 64 65 66  C) || \.    (def
12790 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
12791 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  ) && defined(SQL
12792 49 54 45 5f 44 45 42 55 47 29 29 0a 20 20 2f 2a  ITE_DEBUG)).  /*
12793 0a 20 20 2a 2a 20 55 73 65 64 20 77 68 65 6e 20  .  ** Used when 
12794 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 69  SQLITE_NO_SYNC i
12795 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 6e  s not defined an
12796 64 20 62 79 20 74 68 65 20 61 73 73 65 72 74 28  d by the assert(
12797 29 20 61 6e 64 2f 6f 72 0a 20 20 2a 2a 20 4f 53  ) and/or.  ** OS
12798 54 52 41 43 45 28 29 20 6d 61 63 72 6f 73 2e 0a  TRACE() macros..
12799 20 20 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20     */.  winFile 
1279a 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c  *pFile = (winFil
1279b 65 2a 29 69 64 3b 0a 23 65 6c 73 65 0a 20 20 55  e*)id;.#else.  U
1279c 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
1279d 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 61  id);.#endif..  a
1279e 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
1279f 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20    /* Check that 
127a0 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 53 59  one of SQLITE_SY
127a1 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 46 55 4c  NC_NORMAL or FUL
127a2 4c 20 77 61 73 20 70 61 73 73 65 64 20 2a 2f 0a  L was passed */.
127a3 20 20 61 73 73 65 72 74 28 28 66 6c 61 67 73 26    assert((flags&
127a4 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59  0x0F)==SQLITE_SY
127a5 4e 43 5f 4e 4f 52 4d 41 4c 0a 20 20 20 20 20 20  NC_NORMAL.      
127a6 7c 7c 20 28 66 6c 61 67 73 26 30 78 30 46 29 3d  || (flags&0x0F)=
127a7 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c  =SQLITE_SYNC_FUL
127a8 4c 0a 20 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43  L.  );..  OSTRAC
127a9 45 28 28 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b  E(("SYNC %d lock
127aa 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
127ab 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
127ac 65 29 29 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20  e));..  /* Unix 
127ad 63 61 6e 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65  cannot, but some
127ae 20 73 79 73 74 65 6d 73 20 6d 61 79 20 72 65 74   systems may ret
127af 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20  urn SQLITE_FULL 
127b0 66 72 6f 6d 20 68 65 72 65 2e 20 54 68 69 73 0a  from here. This.
127b1 20 20 2a 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20    ** line is to 
127b2 74 65 73 74 20 74 68 61 74 20 64 6f 69 6e 67 20  test that doing 
127b3 73 6f 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73  so does not caus
127b4 65 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a  e any problems..
127b5 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44    */.  SimulateD
127b6 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65  iskfullError( re
127b7 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  turn SQLITE_FULL
127b8 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   );..#ifndef SQL
127b9 49 54 45 5f 54 45 53 54 0a 20 20 55 4e 55 53 45  ITE_TEST.  UNUSE
127ba 44 5f 50 41 52 41 4d 45 54 45 52 28 66 6c 61 67  D_PARAMETER(flag
127bb 73 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20  s);.#else.  if( 
127bc 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51  (flags&0x0F)==SQ
127bd 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20 29  LITE_SYNC_FULL )
127be 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 75  {.    sqlite3_fu
127bf 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a  llsync_count++;.
127c0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73 79    }.  sqlite3_sy
127c1 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64  nc_count++;.#end
127c2 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63  if..  /* If we c
127c3 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
127c4 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20   SQLITE_NO_SYNC 
127c5 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69  flag, then synci
127c6 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d  ng is a.  ** no-
127c7 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  op.  */.#ifdef S
127c8 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20  QLITE_NO_SYNC.  
127c9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
127ca 3b 0a 23 65 6c 73 65 0a 20 20 72 63 20 3d 20 6f  ;.#else.  rc = o
127cb 73 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72  sFlushFileBuffer
127cc 73 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 53  s(pFile->h);.  S
127cd 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
127ce 72 63 3d 46 41 4c 53 45 20 29 3b 0a 20 20 69 66  rc=FALSE );.  if
127cf 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75  ( rc ){.    retu
127d0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
127d1 7d 65 6c 73 65 7b 0a 20 20 20 20 70 46 69 6c 65  }else{.    pFile
127d2 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73  ->lastErrno = os
127d3 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a  GetLastError();.
127d4 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e 4c 6f      return winLo
127d5 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 49 4f  gError(SQLITE_IO
127d6 45 52 52 5f 46 53 59 4e 43 2c 20 70 46 69 6c 65  ERR_FSYNC, pFile
127d7 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c 0a 20 20 20  ->lastErrno,.   
127d8 20 20 20 20 20 20 20 20 20 20 22 77 69 6e 53 79            "winSy
127d9 6e 63 22 2c 20 70 46 69 6c 65 2d 3e 7a 50 61 74  nc", pFile->zPat
127da 68 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  h);.  }.#endif.}
127db 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e  ../*.** Determin
127dc 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69  e the current si
127dd 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20  ze of a file in 
127de 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20  bytes.*/.static 
127df 69 6e 74 20 77 69 6e 46 69 6c 65 53 69 7a 65 28  int winFileSize(
127e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
127e1 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
127e2 2a 70 53 69 7a 65 29 7b 0a 20 20 77 69 6e 46 69  *pSize){.  winFi
127e3 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
127e4 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20  File*)id;.  int 
127e5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
127e6 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30  .  assert( id!=0
127e7 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f   );.  SimulateIO
127e8 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c  Error(return SQL
127e9 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 29  ITE_IOERR_FSTAT)
127ea 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ;.#if SQLITE_OS_
127eb 57 49 4e 52 54 0a 20 20 7b 0a 20 20 20 20 46 49  WINRT.  {.    FI
127ec 4c 45 5f 53 54 41 4e 44 41 52 44 5f 49 4e 46 4f  LE_STANDARD_INFO
127ed 20 69 6e 66 6f 3b 0a 20 20 20 20 69 66 28 20 6f   info;.    if( o
127ee 73 47 65 74 46 69 6c 65 49 6e 66 6f 72 6d 61 74  sGetFileInformat
127ef 69 6f 6e 42 79 48 61 6e 64 6c 65 45 78 28 70 46  ionByHandleEx(pF
127f0 69 6c 65 2d 3e 68 2c 20 46 69 6c 65 53 74 61 6e  ile->h, FileStan
127f1 64 61 72 64 49 6e 66 6f 2c 0a 20 20 20 20 20 20  dardInfo,.      
127f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
127f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
127f4 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69 6e 66  info, sizeof(inf
127f5 6f 29 29 20 29 7b 0a 20 20 20 20 20 20 2a 70 53  o)) ){.      *pS
127f6 69 7a 65 20 3d 20 69 6e 66 6f 2e 45 6e 64 4f 66  ize = info.EndOf
127f7 46 69 6c 65 2e 51 75 61 64 50 61 72 74 3b 0a 20  File.QuadPart;. 
127f8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
127f9 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
127fa 20 3d 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f   = osGetLastErro
127fb 72 28 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  r();.      rc = 
127fc 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49  winLogError(SQLI
127fd 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 2c 20  TE_IOERR_FSTAT, 
127fe 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
127ff 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
12800 20 20 20 20 20 20 20 20 20 22 77 69 6e 46 69 6c           "winFil
12801 65 53 69 7a 65 22 2c 20 70 46 69 6c 65 2d 3e 7a  eSize", pFile->z
12802 50 61 74 68 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Path);.    }.  }
12803 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 44  .#else.  {.    D
12804 57 4f 52 44 20 75 70 70 65 72 42 69 74 73 3b 0a  WORD upperBits;.
12805 20 20 20 20 44 57 4f 52 44 20 6c 6f 77 65 72 42      DWORD lowerB
12806 69 74 73 3b 0a 20 20 20 20 44 57 4f 52 44 20 6c  its;.    DWORD l
12807 61 73 74 45 72 72 6e 6f 3b 0a 0a 20 20 20 20 6c  astErrno;..    l
12808 6f 77 65 72 42 69 74 73 20 3d 20 6f 73 47 65 74  owerBits = osGet
12809 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2d 3e  FileSize(pFile->
1280a 68 2c 20 26 75 70 70 65 72 42 69 74 73 29 3b 0a  h, &upperBits);.
1280b 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 28 28 28      *pSize = (((
1280c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 75 70  sqlite3_int64)up
1280d 70 65 72 42 69 74 73 29 3c 3c 33 32 29 20 2b 20  perBits)<<32) + 
1280e 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 20 20 69  lowerBits;.    i
1280f 66 28 20 20 20 28 6c 6f 77 65 72 42 69 74 73 20  f(   (lowerBits 
12810 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f  == INVALID_FILE_
12811 53 49 5a 45 29 0a 20 20 20 20 20 20 20 26 26 20  SIZE).       && 
12812 28 28 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73  ((lastErrno = os
12813 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 21  GetLastError())!
12814 3d 4e 4f 5f 45 52 52 4f 52 29 20 29 7b 0a 20 20  =NO_ERROR) ){.  
12815 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
12816 72 72 6e 6f 20 3d 20 6c 61 73 74 45 72 72 6e 6f  rrno = lastErrno
12817 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 69 6e  ;.      rc = win
12818 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f  LogError(SQLITE_
12819 49 4f 45 52 52 5f 46 53 54 41 54 2c 20 70 46 69  IOERR_FSTAT, pFi
1281a 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c 0a 20  le->lastErrno,. 
1281b 20 20 20 20 20 20 20 20 20 20 20 20 22 77 69 6e              "win
1281c 46 69 6c 65 53 69 7a 65 22 2c 20 70 46 69 6c 65  FileSize", pFile
1281d 2d 3e 7a 50 61 74 68 29 3b 0a 20 20 20 20 7d 0a  ->zPath);.    }.
1281e 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74    }.#endif.  ret
1281f 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
12820 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49   LOCKFILE_FAIL_I
12821 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20 75 6e  MMEDIATELY is un
12822 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d 65 20  defined on some 
12823 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e  Windows systems.
12824 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f 43 4b  .*/.#ifndef LOCK
12825 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49  FILE_FAIL_IMMEDI
12826 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65 20 4c  ATELY.# define L
12827 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d  OCKFILE_FAIL_IMM
12828 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e 64 69  EDIATELY 1.#endi
12829 66 0a 0a 23 69 66 6e 64 65 66 20 4c 4f 43 4b 46  f..#ifndef LOCKF
1282a 49 4c 45 5f 45 58 43 4c 55 53 49 56 45 5f 4c 4f  ILE_EXCLUSIVE_LO
1282b 43 4b 0a 23 20 64 65 66 69 6e 65 20 4c 4f 43 4b  CK.# define LOCK
1282c 46 49 4c 45 5f 45 58 43 4c 55 53 49 56 45 5f 4c  FILE_EXCLUSIVE_L
1282d 4f 43 4b 20 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a  OCK 2.#endif../*
1282e 0a 2a 2a 20 48 69 73 74 6f 72 69 63 61 6c 6c 79  .** Historically
1282f 2c 20 53 51 4c 69 74 65 20 68 61 73 20 75 73 65  , SQLite has use
12830 64 20 62 6f 74 68 20 74 68 65 20 4c 6f 63 6b 46  d both the LockF
12831 69 6c 65 20 61 6e 64 20 4c 6f 63 6b 46 69 6c 65  ile and LockFile
12832 45 78 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a  Ex functions..**
12833 20 57 68 65 6e 20 74 68 65 20 4c 6f 63 6b 46 69   When the LockFi
12834 6c 65 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20  le function was 
12835 75 73 65 64 2c 20 69 74 20 77 61 73 20 61 6c 77  used, it was alw
12836 61 79 73 20 65 78 70 65 63 74 65 64 20 74 6f 20  ays expected to 
12837 66 61 69 6c 0a 2a 2a 20 69 6d 6d 65 64 69 61 74  fail.** immediat
12838 65 6c 79 20 69 66 20 74 68 65 20 6c 6f 63 6b 20  ely if the lock 
12839 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 6f 62 74  could not be obt
1283a 61 69 6e 65 64 2e 20 20 41 6c 73 6f 2c 20 69 74  ained.  Also, it
1283b 20 61 6c 77 61 79 73 20 65 78 70 65 63 74 65 64   always expected
1283c 20 74 6f 0a 2a 2a 20 6f 62 74 61 69 6e 20 61 6e   to.** obtain an
1283d 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e   exclusive lock.
1283e 20 20 54 68 65 73 65 20 66 6c 61 67 73 20 61 72    These flags ar
1283f 65 20 75 73 65 64 20 77 69 74 68 20 74 68 65 20  e used with the 
12840 4c 6f 63 6b 46 69 6c 65 45 78 20 66 75 6e 63 74  LockFileEx funct
12841 69 6f 6e 0a 2a 2a 20 61 6e 64 20 72 65 66 6c 65  ion.** and refle
12842 63 74 20 74 68 6f 73 65 20 65 78 70 65 63 74 61  ct those expecta
12843 74 69 6f 6e 73 3b 20 74 68 65 72 65 66 6f 72 65  tions; therefore
12844 2c 20 74 68 65 79 20 73 68 6f 75 6c 64 20 6e 6f  , they should no
12845 74 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f  t be changed..*/
12846 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
12847 4c 4f 43 4b 46 49 4c 45 5f 46 4c 41 47 53 0a 23  LOCKFILE_FLAGS.#
12848 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c   define SQLITE_L
12849 4f 43 4b 46 49 4c 45 5f 46 4c 41 47 53 20 20 20  OCKFILE_FLAGS   
1284a 28 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49  (LOCKFILE_FAIL_I
1284b 4d 4d 45 44 49 41 54 45 4c 59 20 7c 20 5c 0a 20  MMEDIATELY | \. 
1284c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1284d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1284e 20 4c 4f 43 4b 46 49 4c 45 5f 45 58 43 4c 55 53   LOCKFILE_EXCLUS
1284f 49 56 45 5f 4c 4f 43 4b 29 0a 23 65 6e 64 69 66  IVE_LOCK).#endif
12850 0a 0a 2f 2a 0a 2a 2a 20 43 75 72 72 65 6e 74 6c  ../*.** Currentl
12851 79 2c 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20  y, SQLite never 
12852 63 61 6c 6c 73 20 74 68 65 20 4c 6f 63 6b 46 69  calls the LockFi
12853 6c 65 45 78 20 66 75 6e 63 74 69 6f 6e 20 77 69  leEx function wi
12854 74 68 6f 75 74 20 77 61 6e 74 69 6e 67 20 74 68  thout wanting th
12855 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 66 61 69  e.** call to fai
12856 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 69 66  l immediately if
12857 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74   the lock cannot
12858 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f   be obtained..*/
12859 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1285a 4c 4f 43 4b 46 49 4c 45 45 58 5f 46 4c 41 47 53  LOCKFILEEX_FLAGS
1285b 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
1285c 5f 4c 4f 43 4b 46 49 4c 45 45 58 5f 46 4c 41 47  _LOCKFILEEX_FLAG
1285d 53 20 28 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c  S (LOCKFILE_FAIL
1285e 5f 49 4d 4d 45 44 49 41 54 45 4c 59 29 0a 23 65  _IMMEDIATELY).#e
1285f 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75  ndif../*.** Acqu
12860 69 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63  ire a reader loc
12861 6b 2e 0a 2a 2a 20 44 69 66 66 65 72 65 6e 74 20  k..** Different 
12862 41 50 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65  API routines are
12863 20 63 61 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e   called dependin
12864 67 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20  g on whether or 
12865 6e 6f 74 20 74 68 69 73 0a 2a 2a 20 69 73 20 57  not this.** is W
12866 69 6e 39 78 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a  in9x or WinNT..*
12867 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
12868 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65  ReadLock(winFile
12869 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20   *pFile){.  int 
1286a 72 65 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28  res;.  if( isNT(
1286b 29 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  ) ){.#if SQLITE_
1286c 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 2f 2a 0a  OS_WINCE.    /*.
1286d 20 20 20 20 2a 2a 20 4e 4f 54 45 3a 20 57 69 6e      ** NOTE: Win
1286e 64 6f 77 73 20 43 45 20 69 73 20 68 61 6e 64 6c  dows CE is handl
1286f 65 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20 68  ed differently h
12870 65 72 65 20 64 75 65 20 69 74 73 20 6c 61 63 6b  ere due its lack
12871 20 6f 66 20 74 68 65 20 57 69 6e 33 32 0a 20 20   of the Win32.  
12872 20 20 2a 2a 20 20 20 20 20 20 20 41 50 49 20 4c    **       API L
12873 6f 63 6b 46 69 6c 65 45 78 2e 0a 20 20 20 20 2a  ockFileEx..    *
12874 2f 0a 20 20 20 20 72 65 73 20 3d 20 77 69 6e 63  /.    res = winc
12875 65 4c 6f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65  eLockFile(&pFile
12876 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53  ->h, SHARED_FIRS
12877 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6c  T, 0, 1, 0);.#el
12878 73 65 0a 20 20 20 20 72 65 73 20 3d 20 77 69 6e  se.    res = win
12879 4c 6f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65 2d  LockFile(&pFile-
1287a 3e 68 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 46  >h, SQLITE_LOCKF
1287b 49 4c 45 45 58 5f 46 4c 41 47 53 2c 20 53 48 41  ILEEX_FLAGS, SHA
1287c 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 0a 20 20  RED_FIRST, 0,.  
1287d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1287e 20 20 20 20 53 48 41 52 45 44 5f 53 49 5a 45 2c      SHARED_SIZE,
1287f 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a   0);.#endif.  }.
12880 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 57 49  #ifdef SQLITE_WI
12881 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20 20 65  N32_HAS_ANSI.  e
12882 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 3b  lse{.    int lk;
12883 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e  .    sqlite3_ran
12884 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 6c  domness(sizeof(l
12885 6b 29 2c 20 26 6c 6b 29 3b 0a 20 20 20 20 70 46  k), &lk);.    pF
12886 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42  ile->sharedLockB
12887 79 74 65 20 3d 20 28 73 68 6f 72 74 29 28 28 6c  yte = (short)((l
12888 6b 20 26 20 30 78 37 66 66 66 66 66 66 66 29 25  k & 0x7fffffff)%
12889 28 53 48 41 52 45 44 5f 53 49 5a 45 20 2d 20 31  (SHARED_SIZE - 1
1288a 29 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 77 69  ));.    res = wi
1288b 6e 4c 6f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65  nLockFile(&pFile
1288c 2d 3e 68 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  ->h, SQLITE_LOCK
1288d 46 49 4c 45 5f 46 4c 41 47 53 2c 0a 20 20 20 20  FILE_FLAGS,.    
1288e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1288f 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 70    SHARED_FIRST+p
12890 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b  File->sharedLock
12891 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b 0a  Byte, 0, 1, 0);.
12892 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28    }.#endif.  if(
12893 20 72 65 73 20 3d 3d 20 30 20 29 7b 0a 20 20 20   res == 0 ){.   
12894 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
12895 6f 20 3d 20 6f 73 47 65 74 4c 61 73 74 45 72 72  o = osGetLastErr
12896 6f 72 28 29 3b 0a 20 20 20 20 2f 2a 20 4e 6f 20  or();.    /* No 
12897 6e 65 65 64 20 74 6f 20 6c 6f 67 20 61 20 66 61  need to log a fa
12898 69 6c 75 72 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f  ilure to lock */
12899 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65  .  }.  return re
1289a 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f  s;.}../*.** Undo
1289b 20 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73   a readlock.*/.s
1289c 74 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b  tatic int unlock
1289d 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65  ReadLock(winFile
1289e 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20   *pFile){.  int 
1289f 72 65 73 3b 0a 20 20 44 57 4f 52 44 20 6c 61 73  res;.  DWORD las
128a0 74 45 72 72 6e 6f 3b 0a 20 20 69 66 28 20 69 73  tErrno;.  if( is
128a1 4e 54 28 29 20 29 7b 0a 20 20 20 20 72 65 73 20  NT() ){.    res 
128a2 3d 20 77 69 6e 55 6e 6c 6f 63 6b 46 69 6c 65 28  = winUnlockFile(
128a3 26 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45  &pFile->h, SHARE
128a4 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52  D_FIRST, 0, SHAR
128a5 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 7d  ED_SIZE, 0);.  }
128a6 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 57  .#ifdef SQLITE_W
128a7 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20 20  IN32_HAS_ANSI.  
128a8 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20 3d 20  else{.    res = 
128a9 77 69 6e 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 70  winUnlockFile(&p
128aa 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f  File->h, SHARED_
128ab 46 49 52 53 54 2b 70 46 69 6c 65 2d 3e 73 68 61  FIRST+pFile->sha
128ac 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20  redLockByte, 0, 
128ad 31 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  1, 0);.  }.#endi
128ae 66 0a 20 20 69 66 28 20 72 65 73 3d 3d 30 20 26  f.  if( res==0 &
128af 26 20 28 28 6c 61 73 74 45 72 72 6e 6f 20 3d 20  & ((lastErrno = 
128b0 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  osGetLastError()
128b1 29 21 3d 45 52 52 4f 52 5f 4e 4f 54 5f 4c 4f 43  )!=ERROR_NOT_LOC
128b2 4b 45 44 29 20 29 7b 0a 20 20 20 20 70 46 69 6c  KED) ){.    pFil
128b3 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c  e->lastErrno = l
128b4 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 77 69  astErrno;.    wi
128b5 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45  nLogError(SQLITE
128b6 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 2c 20 70  _IOERR_UNLOCK, p
128b7 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 2c  File->lastErrno,
128b8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 22 75  .             "u
128b9 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 22 2c 20  nlockReadLock", 
128ba 70 46 69 6c 65 2d 3e 7a 50 61 74 68 29 3b 0a 20  pFile->zPath);. 
128bb 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b   }.  return res;
128bc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
128bd 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65  he file with the
128be 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20   lock specified 
128bf 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63  by parameter loc
128c0 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f  ktype - one.** o
128c1 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  f the following:
128c2 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53  .**.**     (1) S
128c3 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  HARED_LOCK.**   
128c4 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
128c5 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50  OCK.**     (3) P
128c6 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20  ENDING_LOCK.**  
128c7 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
128c8 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  _LOCK.**.** Some
128c9 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65  times when reque
128ca 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73  sting one lock s
128cb 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c  tate, additional
128cc 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20   lock states.** 
128cd 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
128ce 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
128cf 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
128d0 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
128d1 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69  ater.** transiti
128d2 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20  ons leaving the 
128d3 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65  lock state diffe
128d4 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69  rent from what i
128d5 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a  t started but.**
128d6 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20   still short of 
128d7 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66  its goal.  The f
128d8 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73  ollowing chart s
128d9 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64  hows the allowed
128da 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
128db 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64  and the inserted
128dc 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74   intermediate st
128dd 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55  ates:.**.**    U
128de 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
128df 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
128e0 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20  > RESERVED.**   
128e1 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44   SHARED -> (PEND
128e2 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
128e3 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44  E.**    RESERVED
128e4 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
128e5 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
128e6 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
128e7 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73  USIVE.**.** This
128e8 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e   routine will on
128e9 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f  ly increase a lo
128ea 63 6b 2e 20 20 54 68 65 20 77 69 6e 55 6e 6c 6f  ck.  The winUnlo
128eb 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  ck() routine.** 
128ec 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73  erases all locks
128ed 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74   at once and ret
128ee 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74  urns us immediat
128ef 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c  ely to locking l
128f0 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73  evel 0..** It is
128f1 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
128f2 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69   lower the locki
128f3 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65  ng level one ste
128f4 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f  p at a time.  Yo
128f5 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72  u.** must go str
128f6 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67  aight to locking
128f7 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61   level 0..*/.sta
128f8 74 69 63 20 69 6e 74 20 77 69 6e 4c 6f 63 6b 28  tic int winLock(
128f9 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
128fa 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b  , int locktype){
128fb 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
128fc 54 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65 74  TE_OK;    /* Ret
128fd 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75  urn code from su
128fe 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 69  broutines */.  i
128ff 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20  nt res = 1;     
12900 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
12901 6f 66 20 61 20 57 69 6e 64 6f 77 73 20 6c 6f 63  of a Windows loc
12902 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20  k call */.  int 
12903 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20  newLocktype;    
12904 20 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d     /* Set pFile-
12905 3e 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69  >locktype to thi
12906 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65  s value before e
12907 78 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  xiting */.  int 
12908 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d  gotPendingLock =
12909 20 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65   0;/* True if we
1290a 20 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44   acquired a PEND
1290b 49 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69  ING lock this ti
1290c 6d 65 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20  me */.  winFile 
1290d 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c  *pFile = (winFil
1290e 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 6c  e*)id;.  DWORD l
1290f 61 73 74 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45 52  astErrno = NO_ER
12910 52 4f 52 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ROR;..  assert( 
12911 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41  id!=0 );.  OSTRA
12912 43 45 28 28 22 4c 4f 43 4b 20 25 64 20 25 64 20  CE(("LOCK %d %d 
12913 77 61 73 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20  was %d(%d)\n",. 
12914 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d            pFile-
12915 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46  >h, locktype, pF
12916 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70  ile->locktype, p
12917 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b  File->sharedLock
12918 42 79 74 65 29 29 3b 0a 0a 20 20 2f 2a 20 49 66  Byte));..  /* If
12919 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64   there is alread
1291a 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73  y a lock of this
1291b 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65   type or more re
1291c 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65  strictive on the
1291d 0a 20 20 2a 2a 20 4f 73 46 69 6c 65 2c 20 64 6f  .  ** OsFile, do
1291e 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20   nothing. Don't 
1291f 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b  use the end_lock
12920 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a  : exit path, as.
12921 20 20 2a 2a 20 73 71 6c 69 74 65 33 4f 73 45 6e    ** sqlite3OsEn
12922 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27  terMutex() hasn'
12923 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65  t been called ye
12924 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46  t..  */.  if( pF
12925 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c  ile->locktype>=l
12926 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72  ocktype ){.    r
12927 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
12928 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  .  }..  /* Make 
12929 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67  sure the locking
1292a 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72   sequence is cor
1292b 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65  rect.  */.  asse
1292c 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  rt( pFile->lockt
1292d 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20  ype!=NO_LOCK || 
1292e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
1292f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72  _LOCK );.  asser
12930 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e  t( locktype!=PEN
12931 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  DING_LOCK );.  a
12932 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21  ssert( locktype!
12933 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c  =RESERVED_LOCK |
12934 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  | pFile->locktyp
12935 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
12936 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65  ;..  /* Lock the
12937 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79   PENDING_LOCK by
12938 74 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f  te if we need to
12939 20 61 63 71 75 69 72 65 20 61 20 50 45 4e 44 49   acquire a PENDI
1293a 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20  NG lock or.  ** 
1293b 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 20  a SHARED lock.  
1293c 49 66 20 77 65 20 61 72 65 20 61 63 71 75 69 72  If we are acquir
1293d 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63  ing a SHARED loc
1293e 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69 74 69  k, the acquisiti
1293f 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 50  on of.  ** the P
12940 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65  ENDING_LOCK byte
12941 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a 20   is temporary.. 
12942 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70   */.  newLocktyp
12943 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  e = pFile->lockt
12944 79 70 65 3b 0a 20 20 69 66 28 20 20 20 28 70 46  ype;.  if(   (pF
12945 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e  ile->locktype==N
12946 4f 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 7c 7c 20  O_LOCK).     || 
12947 28 20 20 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45  (   (locktype==E
12948 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 0a 20  XCLUSIVE_LOCK). 
12949 20 20 20 20 20 20 20 20 26 26 20 28 70 46 69 6c          && (pFil
1294a 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53  e->locktype==RES
1294b 45 52 56 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29  ERVED_LOCK)).  )
1294c 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20  {.    int cnt = 
1294d 33 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 6e  3;.    while( cn
1294e 74 2d 2d 3e 30 20 26 26 20 28 72 65 73 20 3d 20  t-->0 && (res = 
1294f 77 69 6e 4c 6f 63 6b 46 69 6c 65 28 26 70 46 69  winLockFile(&pFi
12950 6c 65 2d 3e 68 2c 20 53 51 4c 49 54 45 5f 4c 4f  le->h, SQLITE_LO
12951 43 4b 46 49 4c 45 5f 46 4c 41 47 53 2c 0a 20 20  CKFILE_FLAGS,.  
12952 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12953 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12954 20 20 20 20 20 20 20 50 45 4e 44 49 4e 47 5f 42         PENDING_B
12955 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29 3d 3d  YTE, 0, 1, 0))==
12956 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 72  0 ){.      /* Tr
12957 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67 65 74  y 3 times to get
12958 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63   the pending loc
12959 6b 2e 20 20 54 68 69 73 20 69 73 20 6e 65 65 64  k.  This is need
1295a 65 64 20 74 6f 20 77 6f 72 6b 0a 20 20 20 20 20  ed to work.     
1295b 20 2a 2a 20 61 72 6f 75 6e 64 20 70 72 6f 62 6c   ** around probl
1295c 65 6d 73 20 63 61 75 73 65 64 20 62 79 20 69 6e  ems caused by in
1295d 64 65 78 69 6e 67 20 61 6e 64 2f 6f 72 20 61 6e  dexing and/or an
1295e 74 69 2d 76 69 72 75 73 20 73 6f 66 74 77 61 72  ti-virus softwar
1295f 65 20 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 57 69  e on.      ** Wi
12960 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 20  ndows systems.. 
12961 20 20 20 20 20 2a 2a 20 49 66 20 79 6f 75 20 61       ** If you a
12962 72 65 20 75 73 69 6e 67 20 74 68 69 73 20 63 6f  re using this co
12963 64 65 20 61 73 20 61 20 6d 6f 64 65 6c 20 66 6f  de as a model fo
12964 72 20 61 6c 74 65 72 6e 61 74 69 76 65 20 56 46  r alternative VF
12965 53 65 73 2c 20 64 6f 20 6e 6f 74 0a 20 20 20 20  Ses, do not.    
12966 20 20 2a 2a 20 63 6f 70 79 20 74 68 69 73 20 72    ** copy this r
12967 65 74 72 79 20 6c 6f 67 69 63 2e 20 20 49 74 20  etry logic.  It 
12968 69 73 20 61 20 68 61 63 6b 20 69 6e 74 65 6e 64  is a hack intend
12969 65 64 20 66 6f 72 20 57 69 6e 64 6f 77 73 20 6f  ed for Windows o
1296a 6e 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  nly..      */.  
1296b 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 63 6f      OSTRACE(("co
1296c 75 6c 64 20 6e 6f 74 20 67 65 74 20 61 20 50 45  uld not get a PE
1296d 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 63 6e 74 3d  NDING lock. cnt=
1296e 25 64 5c 6e 22 2c 20 63 6e 74 29 29 3b 0a 20 20  %d\n", cnt));.  
1296f 20 20 20 20 69 66 28 20 63 6e 74 20 29 20 73 71      if( cnt ) sq
12970 6c 69 74 65 33 5f 77 69 6e 33 32 5f 73 6c 65 65  lite3_win32_slee
12971 70 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  p(1);.    }.    
12972 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d  gotPendingLock =
12973 20 72 65 73 3b 0a 20 20 20 20 69 66 28 20 21 72   res;.    if( !r
12974 65 73 20 29 7b 0a 20 20 20 20 20 20 6c 61 73 74  es ){.      last
12975 45 72 72 6e 6f 20 3d 20 6f 73 47 65 74 4c 61 73  Errno = osGetLas
12976 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 7d 0a  tError();.    }.
12977 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72    }..  /* Acquir
12978 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a  e a shared lock.
12979 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
1297a 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
1297b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 61   && res ){.    a
1297c 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f  ssert( pFile->lo
1297d 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20  cktype==NO_LOCK 
1297e 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 67 65 74  );.    res = get
1297f 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b  ReadLock(pFile);
12980 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a  .    if( res ){.
12981 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70        newLocktyp
12982 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b  e = SHARED_LOCK;
12983 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
12984 20 20 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f 73    lastErrno = os
12985 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a  GetLastError();.
12986 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
12987 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56  Acquire a RESERV
12988 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69  ED lock.  */.  i
12989 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53  f( locktype==RES
1298a 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65  ERVED_LOCK && re
1298b 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  s ){.    assert(
1298c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
1298d 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  ==SHARED_LOCK );
1298e 0a 20 20 20 20 72 65 73 20 3d 20 77 69 6e 4c 6f  .    res = winLo
1298f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65 2d 3e 68  ckFile(&pFile->h
12990 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 46 49 4c  , SQLITE_LOCKFIL
12991 45 5f 46 4c 41 47 53 2c 20 52 45 53 45 52 56 45  E_FLAGS, RESERVE
12992 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29  D_BYTE, 0, 1, 0)
12993 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b  ;.    if( res ){
12994 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79  .      newLockty
12995 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f  pe = RESERVED_LO
12996 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  CK;.    }else{. 
12997 20 20 20 20 20 6c 61 73 74 45 72 72 6e 6f 20 3d       lastErrno =
12998 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28   osGetLastError(
12999 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
1299a 2f 2a 20 41 63 71 75 69 72 65 20 61 20 50 45 4e  /* Acquire a PEN
1299b 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20  DING lock.  */. 
1299c 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45   if( locktype==E
1299d 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26  XCLUSIVE_LOCK &&
1299e 20 72 65 73 20 29 7b 0a 20 20 20 20 6e 65 77 4c   res ){.    newL
1299f 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e  ocktype = PENDIN
129a0 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50  G_LOCK;.    gotP
129a1 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a  endingLock = 0;.
129a2 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72    }..  /* Acquir
129a3 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  e an EXCLUSIVE l
129a4 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c  ock.  */.  if( l
129a5 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
129a6 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29  VE_LOCK && res )
129a7 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  {.    assert( pF
129a8 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53  ile->locktype>=S
129a9 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
129aa 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65    res = unlockRe
129ab 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20  adLock(pFile);. 
129ac 20 20 20 4f 53 54 52 41 43 45 28 28 22 75 6e 72     OSTRACE(("unr
129ad 65 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c  eadlock = %d\n",
129ae 20 72 65 73 29 29 3b 0a 20 20 20 20 72 65 73 20   res));.    res 
129af 3d 20 77 69 6e 4c 6f 63 6b 46 69 6c 65 28 26 70  = winLockFile(&p
129b0 46 69 6c 65 2d 3e 68 2c 20 53 51 4c 49 54 45 5f  File->h, SQLITE_
129b1 4c 4f 43 4b 46 49 4c 45 5f 46 4c 41 47 53 2c 20  LOCKFILE_FLAGS, 
129b2 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c  SHARED_FIRST, 0,
129b3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
129b4 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 53 49         SHARED_SI
129b5 5a 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ZE, 0);.    if( 
129b6 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77  res ){.      new
129b7 4c 6f 63 6b 74 79 70 65 20 3d 20 45 58 43 4c 55  Locktype = EXCLU
129b8 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d  SIVE_LOCK;.    }
129b9 65 6c 73 65 7b 0a 20 20 20 20 20 20 6c 61 73 74  else{.      last
129ba 45 72 72 6e 6f 20 3d 20 6f 73 47 65 74 4c 61 73  Errno = osGetLas
129bb 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20  tError();.      
129bc 4f 53 54 52 41 43 45 28 28 22 65 72 72 6f 72 2d  OSTRACE(("error-
129bd 63 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20 6c 61  code = %d\n", la
129be 73 74 45 72 72 6e 6f 29 29 3b 0a 20 20 20 20 20  stErrno));.     
129bf 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69   getReadLock(pFi
129c0 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  le);.    }.  }..
129c1 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68    /* If we are h
129c2 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47  olding a PENDING
129c3 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74   lock that ought
129c4 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c   to be released,
129c5 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61   then.  ** relea
129c6 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a  se it now..  */.
129c7 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67    if( gotPending
129c8 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65  Lock && locktype
129c9 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  ==SHARED_LOCK ){
129ca 0a 20 20 20 20 77 69 6e 55 6e 6c 6f 63 6b 46 69  .    winUnlockFi
129cb 6c 65 28 26 70 46 69 6c 65 2d 3e 68 2c 20 50 45  le(&pFile->h, PE
129cc 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31  NDING_BYTE, 0, 1
129cd 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  , 0);.  }..  /* 
129ce 55 70 64 61 74 65 20 74 68 65 20 73 74 61 74 65  Update the state
129cf 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 73   of the lock has
129d0 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66 69 6c   held in the fil
129d1 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 65  e descriptor the
129d2 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68  n.  ** return th
129d3 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 65  e appropriate re
129d4 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a  sult code..  */.
129d5 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
129d6 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
129d7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53  .  }else{.    OS
129d8 54 52 41 43 45 28 28 22 4c 4f 43 4b 20 46 41 49  TRACE(("LOCK FAI
129d9 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20 66 6f  LED %d trying fo
129da 72 20 25 64 20 62 75 74 20 67 6f 74 20 25 64 5c  r %d but got %d\
129db 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20  n", pFile->h,.  
129dc 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70           locktyp
129dd 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65 29 29  e, newLocktype))
129de 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  ;.    pFile->las
129df 74 45 72 72 6e 6f 20 3d 20 6c 61 73 74 45 72 72  tErrno = lastErr
129e0 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  no;.    rc = SQL
129e1 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20  ITE_BUSY;.  }.  
129e2 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
129e3 3d 20 28 75 38 29 6e 65 77 4c 6f 63 6b 74 79 70  = (u8)newLocktyp
129e4 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  e;.  return rc;.
129e5 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
129e6 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20  utine checks if 
129e7 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52  there is a RESER
129e8 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e  VED lock held on
129e9 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
129ea 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f  * file by this o
129eb 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  r any other proc
129ec 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c  ess. If such a l
129ed 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74  ock is held, ret
129ee 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c  urn.** non-zero,
129ef 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e   otherwise zero.
129f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
129f1 69 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  inCheckReservedL
129f2 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
129f3 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f   *id, int *pResO
129f4 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ut){.  int rc;. 
129f5 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
129f6 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
129f7 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
129f8 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
129f9 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53  E_IOERR_CHECKRES
129fa 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 0a 20  ERVEDLOCK; );.. 
129fb 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
129fc 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  ;.  if( pFile->l
129fd 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45  ocktype>=RESERVE
129fe 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 63  D_LOCK ){.    rc
129ff 20 3d 20 31 3b 0a 20 20 20 20 4f 53 54 52 41 43   = 1;.    OSTRAC
12a00 45 28 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  E(("TEST WR-LOCK
12a01 20 25 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e   %d %d (local)\n
12a02 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29  ", pFile->h, rc)
12a03 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
12a04 72 63 20 3d 20 77 69 6e 4c 6f 63 6b 46 69 6c 65  rc = winLockFile
12a05 28 26 70 46 69 6c 65 2d 3e 68 2c 20 53 51 4c 49  (&pFile->h, SQLI
12a06 54 45 5f 4c 4f 43 4b 46 49 4c 45 5f 46 4c 41 47  TE_LOCKFILE_FLAG
12a07 53 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  S, RESERVED_BYTE
12a08 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  , 0, 1, 0);.    
12a09 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
12a0a 77 69 6e 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 70  winUnlockFile(&p
12a0b 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45  File->h, RESERVE
12a0c 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29  D_BYTE, 0, 1, 0)
12a0d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
12a0e 20 21 72 63 3b 0a 20 20 20 20 4f 53 54 52 41 43   !rc;.    OSTRAC
12a0f 45 28 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  E(("TEST WR-LOCK
12a10 20 25 64 20 25 64 20 28 72 65 6d 6f 74 65 29 5c   %d %d (remote)\
12a11 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63  n", pFile->h, rc
12a12 29 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f  ));.  }.  *pResO
12a13 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 72  ut = rc;.  retur
12a14 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
12a15 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20  /*.** Lower the 
12a16 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e  locking level on
12a17 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
12a18 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e   id to locktype.
12a19 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75    locktype.** mu
12a1a 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f  st be either NO_
12a1b 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c  LOCK or SHARED_L
12a1c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  OCK..**.** If th
12a1d 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
12a1e 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  of the file desc
12a1f 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64  riptor is alread
12a20 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a  y at or below.**
12a21 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
12a22 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68  ocking level, th
12a23 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
12a24 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  no-op..**.** It 
12a25 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
12a26 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  for this routine
12a27 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65 20   to fail if the 
12a28 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a  second argument.
12a29 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20  ** is NO_LOCK.  
12a2a 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  If the second ar
12a2b 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45 44  gument is SHARED
12a2c 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 20  _LOCK then this 
12a2d 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 74  routine.** might
12a2e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
12a2f 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 20  OERR;.*/.static 
12a30 69 6e 74 20 77 69 6e 55 6e 6c 6f 63 6b 28 73 71  int winUnlock(sq
12a31 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
12a32 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20  int locktype){. 
12a33 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 77 69 6e   int type;.  win
12a34 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77  File *pFile = (w
12a35 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e  inFile*)id;.  in
12a36 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
12a37 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ;.  assert( pFil
12a38 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  e!=0 );.  assert
12a39 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52  ( locktype<=SHAR
12a3a 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 54  ED_LOCK );.  OST
12a3b 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20 25 64  RACE(("UNLOCK %d
12a3c 20 74 6f 20 25 64 20 77 61 73 20 25 64 28 25 64   to %d was %d(%d
12a3d 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  )\n", pFile->h, 
12a3e 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20  locktype,.      
12a3f 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
12a40 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72  ype, pFile->shar
12a41 65 64 4c 6f 63 6b 42 79 74 65 29 29 3b 0a 20 20  edLockByte));.  
12a42 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f  type = pFile->lo
12a43 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 79  cktype;.  if( ty
12a44 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe>=EXCLUSIVE_LO
12a45 43 4b 20 29 7b 0a 20 20 20 20 77 69 6e 55 6e 6c  CK ){.    winUnl
12a46 6f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65 2d 3e  ockFile(&pFile->
12a47 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c  h, SHARED_FIRST,
12a48 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c   0, SHARED_SIZE,
12a49 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63   0);.    if( loc
12a4a 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
12a4b 43 4b 20 26 26 20 21 67 65 74 52 65 61 64 4c 6f  CK && !getReadLo
12a4c 63 6b 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20  ck(pFile) ){.   
12a4d 20 20 20 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c     /* This shoul
12a4e 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e 2e 20  d never happen. 
12a4f 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79   We should alway
12a50 73 20 62 65 20 61 62 6c 65 20 74 6f 0a 20 20 20  s be able to.   
12a51 20 20 20 2a 2a 20 72 65 61 63 71 75 69 72 65 20     ** reacquire 
12a52 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f  the read lock */
12a53 0a 20 20 20 20 20 20 72 63 20 3d 20 77 69 6e 4c  .      rc = winL
12a54 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 49  ogError(SQLITE_I
12a55 4f 45 52 52 5f 55 4e 4c 4f 43 4b 2c 20 6f 73 47  OERR_UNLOCK, osG
12a56 65 74 4c 61 73 74 45 72 72 6f 72 28 29 2c 0a 20  etLastError(),. 
12a57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 77                "w
12a58 69 6e 55 6e 6c 6f 63 6b 22 2c 20 70 46 69 6c 65  inUnlock", pFile
12a59 2d 3e 7a 50 61 74 68 29 3b 0a 20 20 20 20 7d 0a  ->zPath);.    }.
12a5a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d    }.  if( type>=
12a5b 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b  RESERVED_LOCK ){
12a5c 0a 20 20 20 20 77 69 6e 55 6e 6c 6f 63 6b 46 69  .    winUnlockFi
12a5d 6c 65 28 26 70 46 69 6c 65 2d 3e 68 2c 20 52 45  le(&pFile->h, RE
12a5e 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20  SERVED_BYTE, 0, 
12a5f 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  1, 0);.  }.  if(
12a60 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f   locktype==NO_LO
12a61 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52  CK && type>=SHAR
12a62 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 75  ED_LOCK ){.    u
12a63 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46  nlockReadLock(pF
12a64 69 6c 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ile);.  }.  if( 
12a65 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f  type>=PENDING_LO
12a66 43 4b 20 29 7b 0a 20 20 20 20 77 69 6e 55 6e 6c  CK ){.    winUnl
12a67 6f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65 2d 3e  ockFile(&pFile->
12a68 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c  h, PENDING_BYTE,
12a69 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20   0, 1, 0);.  }. 
12a6a 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
12a6b 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 65 3b   = (u8)locktype;
12a6c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
12a6d 0a 2f 2a 0a 2a 2a 20 49 66 20 2a 70 41 72 67 20  ./*.** If *pArg 
12a6e 69 73 20 69 6e 69 74 69 74 69 61 6c 6c 79 20 6e  is inititially n
12a6f 65 67 61 74 69 76 65 20 74 68 65 6e 20 74 68 69  egative then thi
12a70 73 20 69 73 20 61 20 71 75 65 72 79 2e 20 20 53  s is a query.  S
12a71 65 74 20 2a 70 41 72 67 20 74 6f 0a 2a 2a 20 31  et *pArg to.** 1
12a72 20 6f 72 20 30 20 64 65 70 65 6e 64 69 6e 67 20   or 0 depending 
12a73 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  on whether or no
12a74 74 20 62 69 74 20 6d 61 73 6b 20 6f 66 20 70 46  t bit mask of pF
12a75 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 20 69  ile->ctrlFlags i
12a76 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  s set..**.** If 
12a77 2a 70 41 72 67 20 69 73 20 30 20 6f 72 20 31 2c  *pArg is 0 or 1,
12a78 20 74 68 65 6e 20 63 6c 65 61 72 20 6f 72 20 73   then clear or s
12a79 65 74 20 74 68 65 20 6d 61 73 6b 20 62 69 74 20  et the mask bit 
12a7a 6f 66 20 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c  of pFile->ctrlFl
12a7b 61 67 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ags..*/.static v
12a7c 6f 69 64 20 77 69 6e 4d 6f 64 65 42 69 74 28 77  oid winModeBit(w
12a7d 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 75  inFile *pFile, u
12a7e 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6d 61 73  nsigned char mas
12a7f 6b 2c 20 69 6e 74 20 2a 70 41 72 67 29 7b 0a 20  k, int *pArg){. 
12a80 20 69 66 28 20 2a 70 41 72 67 3c 30 20 29 7b 0a   if( *pArg<0 ){.
12a81 20 20 20 20 2a 70 41 72 67 20 3d 20 28 70 46 69      *pArg = (pFi
12a82 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 20 26 20  le->ctrlFlags & 
12a83 6d 61 73 6b 29 21 3d 30 3b 0a 20 20 7d 65 6c 73  mask)!=0;.  }els
12a84 65 20 69 66 28 20 28 2a 70 41 72 67 29 3d 3d 30  e if( (*pArg)==0
12a85 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 63   ){.    pFile->c
12a86 74 72 6c 46 6c 61 67 73 20 26 3d 20 7e 6d 61 73  trlFlags &= ~mas
12a87 6b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  k;.  }else{.    
12a88 70 46 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73  pFile->ctrlFlags
12a89 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 7d 0a 7d 0a   |= mask;.  }.}.
12a8a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20 61  ./*.** Control a
12a8b 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68 65 20  nd query of the 
12a8c 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65  open file handle
12a8d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
12a8e 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73  winFileControl(s
12a8f 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
12a90 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70   int op, void *p
12a91 41 72 67 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20  Arg){.  winFile 
12a92 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c  *pFile = (winFil
12a93 65 2a 29 69 64 3b 0a 20 20 73 77 69 74 63 68 28  e*)id;.  switch(
12a94 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20   op ){.    case 
12a95 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43  SQLITE_FCNTL_LOC
12a96 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20  KSTATE: {.      
12a97 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 70 46  *(int*)pArg = pF
12a98 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20  ile->locktype;. 
12a99 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
12a9a 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20  TE_OK;.    }.   
12a9b 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4c 41 53   case SQLITE_LAS
12a9c 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 20 20  T_ERRNO: {.     
12a9d 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28   *(int*)pArg = (
12a9e 69 6e 74 29 70 46 69 6c 65 2d 3e 6c 61 73 74 45  int)pFile->lastE
12a9f 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75  rrno;.      retu
12aa0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
12aa1 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
12aa2 49 54 45 5f 46 43 4e 54 4c 5f 43 48 55 4e 4b 5f  ITE_FCNTL_CHUNK_
12aa3 53 49 5a 45 3a 20 7b 0a 20 20 20 20 20 20 70 46  SIZE: {.      pF
12aa4 69 6c 65 2d 3e 73 7a 43 68 75 6e 6b 20 3d 20 2a  ile->szChunk = *
12aa5 28 69 6e 74 20 2a 29 70 41 72 67 3b 0a 20 20 20  (int *)pArg;.   
12aa6 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
12aa7 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  _OK;.    }.    c
12aa8 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c  ase SQLITE_FCNTL
12aa9 5f 53 49 5a 45 5f 48 49 4e 54 3a 20 7b 0a 20 20  _SIZE_HINT: {.  
12aaa 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 73      if( pFile->s
12aab 7a 43 68 75 6e 6b 3e 30 20 29 7b 0a 20 20 20 20  zChunk>0 ){.    
12aac 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
12aad 34 20 6f 6c 64 53 7a 3b 0a 20 20 20 20 20 20 20  4 oldSz;.       
12aae 20 69 6e 74 20 72 63 20 3d 20 77 69 6e 46 69 6c   int rc = winFil
12aaf 65 53 69 7a 65 28 69 64 2c 20 26 6f 6c 64 53 7a  eSize(id, &oldSz
12ab0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
12ab1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
12ab2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
12ab3 33 5f 69 6e 74 36 34 20 6e 65 77 53 7a 20 3d 20  3_int64 newSz = 
12ab4 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a  *(sqlite3_int64*
12ab5 29 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20 20  )pArg;.         
12ab6 20 69 66 28 20 6e 65 77 53 7a 3e 6f 6c 64 53 7a   if( newSz>oldSz
12ab7 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
12ab8 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42  SimulateIOErrorB
12ab9 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 20 20  enign(1);.      
12aba 20 20 20 20 20 20 72 63 20 3d 20 77 69 6e 54 72        rc = winTr
12abb 75 6e 63 61 74 65 28 69 64 2c 20 6e 65 77 53 7a  uncate(id, newSz
12abc 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 53  );.            S
12abd 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65  imulateIOErrorBe
12abe 6e 69 67 6e 28 30 29 3b 0a 20 20 20 20 20 20 20  nign(0);.       
12abf 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
12ac0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
12ac1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
12ac2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
12ac3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
12ac4 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 45   SQLITE_FCNTL_PE
12ac5 52 53 49 53 54 5f 57 41 4c 3a 20 7b 0a 20 20 20  RSIST_WAL: {.   
12ac6 20 20 20 77 69 6e 4d 6f 64 65 42 69 74 28 70 46     winModeBit(pF
12ac7 69 6c 65 2c 20 57 49 4e 46 49 4c 45 5f 50 45 52  ile, WINFILE_PER
12ac8 53 49 53 54 5f 57 41 4c 2c 20 28 69 6e 74 2a 29  SIST_WAL, (int*)
12ac9 70 41 72 67 29 3b 0a 20 20 20 20 20 20 72 65 74  pArg);.      ret
12aca 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
12acb 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
12acc 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 4f 57 45 52  LITE_FCNTL_POWER
12acd 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 3a 20  SAFE_OVERWRITE: 
12ace 7b 0a 20 20 20 20 20 20 77 69 6e 4d 6f 64 65 42  {.      winModeB
12acf 69 74 28 70 46 69 6c 65 2c 20 57 49 4e 46 49 4c  it(pFile, WINFIL
12ad0 45 5f 50 53 4f 57 2c 20 28 69 6e 74 2a 29 70 41  E_PSOW, (int*)pA
12ad1 72 67 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  rg);.      retur
12ad2 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
12ad3 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
12ad4 54 45 5f 46 43 4e 54 4c 5f 56 46 53 4e 41 4d 45  TE_FCNTL_VFSNAME
12ad5 3a 20 7b 0a 20 20 20 20 20 20 2a 28 63 68 61 72  : {.      *(char
12ad6 2a 2a 29 70 41 72 67 20 3d 20 73 71 6c 69 74 65  **)pArg = sqlite
12ad7 33 5f 6d 70 72 69 6e 74 66 28 22 77 69 6e 33 32  3_mprintf("win32
12ad8 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ");.      return
12ad9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
12ada 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
12adb 45 5f 46 43 4e 54 4c 5f 57 49 4e 33 32 5f 41 56  E_FCNTL_WIN32_AV
12adc 5f 52 45 54 52 59 3a 20 7b 0a 20 20 20 20 20 20  _RETRY: {.      
12add 69 6e 74 20 2a 61 20 3d 20 28 69 6e 74 2a 29 70  int *a = (int*)p
12ade 41 72 67 3b 0a 20 20 20 20 20 20 69 66 28 20 61  Arg;.      if( a
12adf 5b 30 5d 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  [0]>0 ){.       
12ae0 20 77 69 6e 33 32 49 6f 65 72 72 52 65 74 72 79   win32IoerrRetry
12ae1 20 3d 20 61 5b 30 5d 3b 0a 20 20 20 20 20 20 7d   = a[0];.      }
12ae2 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 5b  else{.        a[
12ae3 30 5d 20 3d 20 77 69 6e 33 32 49 6f 65 72 72 52  0] = win32IoerrR
12ae4 65 74 72 79 3b 0a 20 20 20 20 20 20 7d 0a 20 20  etry;.      }.  
12ae5 20 20 20 20 69 66 28 20 61 5b 31 5d 3e 30 20 29      if( a[1]>0 )
12ae6 7b 0a 20 20 20 20 20 20 20 20 77 69 6e 33 32 49  {.        win32I
12ae7 6f 65 72 72 52 65 74 72 79 44 65 6c 61 79 20 3d  oerrRetryDelay =
12ae8 20 61 5b 31 5d 3b 0a 20 20 20 20 20 20 7d 65 6c   a[1];.      }el
12ae9 73 65 7b 0a 20 20 20 20 20 20 20 20 61 5b 31 5d  se{.        a[1]
12aea 20 3d 20 77 69 6e 33 32 49 6f 65 72 72 52 65 74   = win32IoerrRet
12aeb 72 79 44 65 6c 61 79 3b 0a 20 20 20 20 20 20 7d  ryDelay;.      }
12aec 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
12aed 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
12aee 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
12aef 54 45 5f 4e 4f 54 46 4f 55 4e 44 3b 0a 7d 0a 0a  TE_NOTFOUND;.}..
12af0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
12af1 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 6e 20   sector size in 
12af2 62 79 74 65 73 20 6f 66 20 74 68 65 20 75 6e 64  bytes of the und
12af3 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65  erlying block de
12af4 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20  vice for.** the 
12af5 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 2e 20  specified file. 
12af6 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 20 61  This is almost a
12af7 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 73 2c  lways 512 bytes,
12af8 20 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c   but may be.** l
12af9 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 20 64  arger for some d
12afa 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51  evices..**.** SQ
12afb 4c 69 74 65 20 63 6f 64 65 20 61 73 73 75 6d 65  Lite code assume
12afc 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  s this function 
12afd 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20  cannot fail. It 
12afe 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 74 68 61  also assumes tha
12aff 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c 65  t.** if two file
12b00 73 20 61 72 65 20 63 72 65 61 74 65 64 20 69 6e  s are created in
12b01 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2d 73   the same file-s
12b02 79 73 74 65 6d 20 64 69 72 65 63 74 6f 72 79 20  ystem directory 
12b03 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62  (i.e..** a datab
12b04 61 73 65 20 61 6e 64 20 69 74 73 20 6a 6f 75 72  ase and its jour
12b05 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 74 20 74  nal file) that t
12b06 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77  he sector size w
12b07 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20 73 61  ill be the.** sa
12b08 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a  me for both..*/.
12b09 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 65  static int winSe
12b0a 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33  ctorSize(sqlite3
12b0b 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 28 76  _file *id){.  (v
12b0c 6f 69 64 29 69 64 3b 0a 20 20 72 65 74 75 72 6e  oid)id;.  return
12b0d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
12b0e 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a 0a  SECTOR_SIZE;.}..
12b0f 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 76  /*.** Return a v
12b10 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63 65 20  ector of device 
12b11 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 2e  characteristics.
12b12 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
12b13 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74 65  inDeviceCharacte
12b14 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f  ristics(sqlite3_
12b15 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 77 69 6e  file *id){.  win
12b16 46 69 6c 65 20 2a 70 20 3d 20 28 77 69 6e 46 69  File *p = (winFi
12b17 6c 65 2a 29 69 64 3b 0a 20 20 72 65 74 75 72 6e  le*)id;.  return
12b18 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 55 4e   SQLITE_IOCAP_UN
12b19 44 45 4c 45 54 41 42 4c 45 5f 57 48 45 4e 5f 4f  DELETABLE_WHEN_O
12b1a 50 45 4e 20 7c 0a 20 20 20 20 20 20 20 20 20 28  PEN |.         (
12b1b 28 70 2d 3e 63 74 72 6c 46 6c 61 67 73 20 26 20  (p->ctrlFlags & 
12b1c 57 49 4e 46 49 4c 45 5f 50 53 4f 57 29 3f 53 51  WINFILE_PSOW)?SQ
12b1d 4c 49 54 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52  LITE_IOCAP_POWER
12b1e 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 3a 30  SAFE_OVERWRITE:0
12b1f 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  );.}..#ifndef SQ
12b20 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 0a 2f  LITE_OMIT_WAL../
12b21 2a 20 0a 2a 2a 20 57 69 6e 64 6f 77 73 20 77 69  * .** Windows wi
12b22 6c 6c 20 6f 6e 6c 79 20 6c 65 74 20 79 6f 75 20  ll only let you 
12b23 63 72 65 61 74 65 20 66 69 6c 65 20 76 69 65 77  create file view
12b24 20 6d 61 70 70 69 6e 67 73 0a 2a 2a 20 6f 6e 20   mappings.** on 
12b25 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20  allocation size 
12b26 67 72 61 6e 75 6c 61 72 69 74 79 20 62 6f 75 6e  granularity boun
12b27 64 61 72 69 65 73 2e 0a 2a 2a 20 44 75 72 69 6e  daries..** Durin
12b28 67 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  g sqlite3_os_ini
12b29 74 28 29 20 77 65 20 64 6f 20 61 20 47 65 74 53  t() we do a GetS
12b2a 79 73 74 65 6d 49 6e 66 6f 28 29 0a 2a 2a 20 74  ystemInfo().** t
12b2b 6f 20 67 65 74 20 74 68 65 20 67 72 61 6e 75 6c  o get the granul
12b2c 61 72 69 74 79 20 73 69 7a 65 2e 0a 2a 2f 0a 53  arity size..*/.S
12b2d 59 53 54 45 4d 5f 49 4e 46 4f 20 77 69 6e 53 79  YSTEM_INFO winSy
12b2e 73 49 6e 66 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 48 65  sInfo;../*.** He
12b2f 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74  lper functions t
12b30 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c  o obtain and rel
12b31 69 6e 71 75 69 73 68 20 74 68 65 20 67 6c 6f 62  inquish the glob
12b32 61 6c 20 6d 75 74 65 78 2e 20 54 68 65 0a 2a 2a  al mutex. The.**
12b33 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 69 73   global mutex is
12b34 20 75 73 65 64 20 74 6f 20 70 72 6f 74 65 63 74   used to protect
12b35 20 74 68 65 20 77 69 6e 4c 6f 63 6b 49 6e 66 6f   the winLockInfo
12b36 20 6f 62 6a 65 63 74 73 20 75 73 65 64 20 62 79   objects used by
12b37 20 0a 2a 2a 20 74 68 69 73 20 66 69 6c 65 2c 20   .** this file, 
12b38 61 6c 6c 20 6f 66 20 77 68 69 63 68 20 6d 61 79  all of which may
12b39 20 62 65 20 73 68 61 72 65 64 20 62 79 20 6d 75   be shared by mu
12b3a 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73 2e 0a  ltiple threads..
12b3b 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 77  **.** Function w
12b3c 69 6e 53 68 6d 4d 75 74 65 78 48 65 6c 64 28 29  inShmMutexHeld()
12b3d 20 69 73 20 75 73 65 64 20 74 6f 20 61 73 73 65   is used to asse
12b3e 72 74 28 29 20 74 68 61 74 20 74 68 65 20 67 6c  rt() that the gl
12b3f 6f 62 61 6c 20 6d 75 74 65 78 20 0a 2a 2a 20 69  obal mutex .** i
12b40 73 20 68 65 6c 64 20 77 68 65 6e 20 72 65 71 75  s held when requ
12b41 69 72 65 64 2e 20 54 68 69 73 20 66 75 6e 63 74  ired. This funct
12b42 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  ion is only used
12b43 20 61 73 20 70 61 72 74 20 6f 66 20 61 73 73 65   as part of asse
12b44 72 74 28 29 20 0a 2a 2a 20 73 74 61 74 65 6d 65  rt() .** stateme
12b45 6e 74 73 2e 20 65 2e 67 2e 0a 2a 2a 0a 2a 2a 20  nts. e.g..**.** 
12b46 20 20 77 69 6e 53 68 6d 45 6e 74 65 72 4d 75 74    winShmEnterMut
12b47 65 78 28 29 0a 2a 2a 20 20 20 20 20 61 73 73 65  ex().**     asse
12b48 72 74 28 20 77 69 6e 53 68 6d 4d 75 74 65 78 48  rt( winShmMutexH
12b49 65 6c 64 28 29 20 29 3b 0a 2a 2a 20 20 20 77 69  eld() );.**   wi
12b4a 6e 53 68 6d 4c 65 61 76 65 4d 75 74 65 78 28 29  nShmLeaveMutex()
12b4b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
12b4c 77 69 6e 53 68 6d 45 6e 74 65 72 4d 75 74 65 78  winShmEnterMutex
12b4d 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  (void){.  sqlite
12b4e 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71  3_mutex_enter(sq
12b4f 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
12b50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
12b51 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a  TIC_MASTER));.}.
12b52 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 53  static void winS
12b53 68 6d 4c 65 61 76 65 4d 75 74 65 78 28 76 6f 69  hmLeaveMutex(voi
12b54 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  d){.  sqlite3_mu
12b55 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
12b56 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
12b57 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
12b58 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 23 69 66 64  MASTER));.}.#ifd
12b59 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
12b5a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 68  static int winSh
12b5b 6d 4d 75 74 65 78 48 65 6c 64 28 76 6f 69 64 29  mMutexHeld(void)
12b5c 20 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69   {.  return sqli
12b5d 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73  te3_mutex_held(s
12b5e 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
12b5f 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
12b60 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d  ATIC_MASTER));.}
12b61 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f  .#endif../*.** O
12b62 62 6a 65 63 74 20 75 73 65 64 20 74 6f 20 72 65  bject used to re
12b63 70 72 65 73 65 6e 74 20 61 20 73 69 6e 67 6c 65  present a single
12b64 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 61 6e 64   file opened and
12b65 20 6d 6d 61 70 70 65 64 20 74 6f 20 70 72 6f 76   mmapped to prov
12b66 69 64 65 0a 2a 2a 20 73 68 61 72 65 64 20 6d 65  ide.** shared me
12b67 6d 6f 72 79 2e 20 20 57 68 65 6e 20 6d 75 6c 74  mory.  When mult
12b68 69 70 6c 65 20 74 68 72 65 61 64 73 20 61 6c 6c  iple threads all
12b69 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 73   reference the s
12b6a 61 6d 65 0a 2a 2a 20 6c 6f 67 2d 73 75 6d 6d 61  ame.** log-summa
12b6b 72 79 2c 20 65 61 63 68 20 74 68 72 65 61 64 20  ry, each thread 
12b6c 68 61 73 20 69 74 73 20 6f 77 6e 20 77 69 6e 46  has its own winF
12b6d 69 6c 65 20 6f 62 6a 65 63 74 2c 20 62 75 74 20  ile object, but 
12b6e 74 68 65 79 20 61 6c 6c 0a 2a 2a 20 70 6f 69 6e  they all.** poin
12b6f 74 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 69 6e  t to a single in
12b70 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
12b71 62 6a 65 63 74 2e 20 20 49 6e 20 6f 74 68 65 72  bject.  In other
12b72 20 77 6f 72 64 73 2c 20 65 61 63 68 0a 2a 2a 20   words, each.** 
12b73 6c 6f 67 2d 73 75 6d 6d 61 72 79 20 69 73 20 6f  log-summary is o
12b74 70 65 6e 65 64 20 6f 6e 6c 79 20 6f 6e 63 65 20  pened only once 
12b75 70 65 72 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a  per process..**.
12b76 2a 2a 20 77 69 6e 53 68 6d 4d 75 74 65 78 48 65  ** winShmMutexHe
12b77 6c 64 28 29 20 6d 75 73 74 20 62 65 20 74 72 75  ld() must be tru
12b78 65 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20  e when creating 
12b79 6f 72 20 64 65 73 74 72 6f 79 69 6e 67 0a 2a 2a  or destroying.**
12b7a 20 74 68 69 73 20 6f 62 6a 65 63 74 20 6f 72 20   this object or 
12b7b 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 6f 72  while reading or
12b7c 20 77 72 69 74 69 6e 67 20 74 68 65 20 66 6f 6c   writing the fol
12b7d 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 3a 0a 2a  lowing fields:.*
12b7e 2a 0a 2a 2a 20 20 20 20 20 20 6e 52 65 66 0a 2a  *.**      nRef.*
12b7f 2a 20 20 20 20 20 20 70 4e 65 78 74 20 0a 2a 2a  *      pNext .**
12b80 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
12b81 67 20 66 69 65 6c 64 73 20 61 72 65 20 72 65 61  g fields are rea
12b82 64 2d 6f 6e 6c 79 20 61 66 74 65 72 20 74 68 65  d-only after the
12b83 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74   object is creat
12b84 65 64 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 20  ed:.** .**      
12b85 66 69 64 0a 2a 2a 20 20 20 20 20 20 7a 46 69 6c  fid.**      zFil
12b86 65 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20 45 69 74 68  ename.**.** Eith
12b87 65 72 20 77 69 6e 53 68 6d 4e 6f 64 65 2e 6d 75  er winShmNode.mu
12b88 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64  tex must be held
12b89 20 6f 72 20 77 69 6e 53 68 6d 4e 6f 64 65 2e 6e   or winShmNode.n
12b8a 52 65 66 3d 3d 30 20 61 6e 64 0a 2a 2a 20 77 69  Ref==0 and.** wi
12b8b 6e 53 68 6d 4d 75 74 65 78 48 65 6c 64 28 29 20  nShmMutexHeld() 
12b8c 69 73 20 74 72 75 65 20 77 68 65 6e 20 72 65 61  is true when rea
12b8d 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20  ding or writing 
12b8e 61 6e 79 20 6f 74 68 65 72 20 66 69 65 6c 64 0a  any other field.
12b8f 2a 2a 20 69 6e 20 74 68 69 73 20 73 74 72 75 63  ** in this struc
12b90 74 75 72 65 2e 0a 2a 2a 0a 2a 2f 0a 73 74 72 75  ture..**.*/.stru
12b91 63 74 20 77 69 6e 53 68 6d 4e 6f 64 65 20 7b 0a  ct winShmNode {.
12b92 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
12b93 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20  *mutex;      /* 
12b94 4d 75 74 65 78 20 74 6f 20 61 63 63 65 73 73 20  Mutex to access 
12b95 74 68 69 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  this object */. 
12b96 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
12b97 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
12b98 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ame of the file 
12b99 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20 68 46 69  */.  winFile hFi
12b9a 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  le;             
12b9b 2f 2a 20 46 69 6c 65 20 68 61 6e 64 6c 65 20 66  /* File handle f
12b9c 72 6f 6d 20 77 69 6e 4f 70 65 6e 20 2a 2f 0a 0a  rom winOpen */..
12b9d 20 20 69 6e 74 20 73 7a 52 65 67 69 6f 6e 3b 20    int szRegion; 
12b9e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12b9f 53 69 7a 65 20 6f 66 20 73 68 61 72 65 64 2d 6d  Size of shared-m
12ba0 65 6d 6f 72 79 20 72 65 67 69 6f 6e 73 20 2a 2f  emory regions */
12ba1 0a 20 20 69 6e 74 20 6e 52 65 67 69 6f 6e 3b 20  .  int nRegion; 
12ba2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12ba3 20 53 69 7a 65 20 6f 66 20 61 72 72 61 79 20 61   Size of array a
12ba4 70 52 65 67 69 6f 6e 20 2a 2f 0a 20 20 73 74 72  pRegion */.  str
12ba5 75 63 74 20 53 68 6d 52 65 67 69 6f 6e 20 7b 0a  uct ShmRegion {.
12ba6 20 20 20 20 48 41 4e 44 4c 45 20 68 4d 61 70 3b      HANDLE hMap;
12ba7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12ba8 46 69 6c 65 20 68 61 6e 64 6c 65 20 66 72 6f 6d  File handle from
12ba9 20 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69   CreateFileMappi
12baa 6e 67 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a  ng */.    void *
12bab 70 4d 61 70 3b 0a 20 20 7d 20 2a 61 52 65 67 69  pMap;.  } *aRegi
12bac 6f 6e 3b 0a 20 20 44 57 4f 52 44 20 6c 61 73 74  on;.  DWORD last
12bad 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 20 20  Errno;          
12bae 20 2f 2a 20 54 68 65 20 57 69 6e 64 6f 77 73 20   /* The Windows 
12baf 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c  errno from the l
12bb0 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f  ast I/O error */
12bb1 0a 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20  ..  int nRef;   
12bb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
12bb3 2a 20 4e 75 6d 62 65 72 20 6f 66 20 77 69 6e 53  * Number of winS
12bb4 68 6d 20 6f 62 6a 65 63 74 73 20 70 6f 69 6e 74  hm objects point
12bb5 69 6e 67 20 74 6f 20 74 68 69 73 20 2a 2f 0a 20  ing to this */. 
12bb6 20 77 69 6e 53 68 6d 20 2a 70 46 69 72 73 74 3b   winShm *pFirst;
12bb7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
12bb8 6c 6c 20 77 69 6e 53 68 6d 20 6f 62 6a 65 63 74  ll winShm object
12bb9 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  s pointing to th
12bba 69 73 20 2a 2f 0a 20 20 77 69 6e 53 68 6d 4e 6f  is */.  winShmNo
12bbb 64 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  de *pNext;      
12bbc 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69     /* Next in li
12bbd 73 74 20 6f 66 20 61 6c 6c 20 77 69 6e 53 68 6d  st of all winShm
12bbe 4e 6f 64 65 20 6f 62 6a 65 63 74 73 20 2a 2f 0a  Node objects */.
12bbf 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
12bc0 42 55 47 0a 20 20 75 38 20 6e 65 78 74 53 68 6d  BUG.  u8 nextShm
12bc1 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Id;             
12bc2 20 2f 2a 20 4e 65 78 74 20 61 76 61 69 6c 61 62   /* Next availab
12bc3 6c 65 20 77 69 6e 53 68 6d 2e 69 64 20 76 61 6c  le winShm.id val
12bc4 75 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a  ue */.#endif.};.
12bc5 0a 2f 2a 0a 2a 2a 20 41 20 67 6c 6f 62 61 6c 20  ./*.** A global 
12bc6 61 72 72 61 79 20 6f 66 20 61 6c 6c 20 77 69 6e  array of all win
12bc7 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74 73 2e  ShmNode objects.
12bc8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 69 6e 53 68  .**.** The winSh
12bc9 6d 4d 75 74 65 78 48 65 6c 64 28 29 20 6d 75 73  mMutexHeld() mus
12bca 74 20 62 65 20 74 72 75 65 20 77 68 69 6c 65 20  t be true while 
12bcb 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69  reading or writi
12bcc 6e 67 20 74 68 69 73 20 6c 69 73 74 2e 0a 2a 2f  ng this list..*/
12bcd 0a 73 74 61 74 69 63 20 77 69 6e 53 68 6d 4e 6f  .static winShmNo
12bce 64 65 20 2a 77 69 6e 53 68 6d 4e 6f 64 65 4c 69  de *winShmNodeLi
12bcf 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 53  st = 0;../*.** S
12bd0 74 72 75 63 74 75 72 65 20 75 73 65 64 20 69 6e  tructure used in
12bd1 74 65 72 6e 61 6c 6c 79 20 62 79 20 74 68 69 73  ternally by this
12bd2 20 56 46 53 20 74 6f 20 72 65 63 6f 72 64 20 74   VFS to record t
12bd3 68 65 20 73 74 61 74 65 20 6f 66 20 61 6e 0a 2a  he state of an.*
12bd4 2a 20 6f 70 65 6e 20 73 68 61 72 65 64 20 6d 65  * open shared me
12bd5 6d 6f 72 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  mory connection.
12bd6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  .**.** The follo
12bd7 77 69 6e 67 20 66 69 65 6c 64 73 20 61 72 65 20  wing fields are 
12bd8 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65 6e  initialized when
12bd9 20 74 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20   this object is 
12bda 63 72 65 61 74 65 64 20 61 6e 64 0a 2a 2a 20 61  created and.** a
12bdb 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20 74 68 65  re read-only the
12bdc 72 65 61 66 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 20  reafter:.**.**  
12bdd 20 20 77 69 6e 53 68 6d 2e 70 53 68 6d 4e 6f 64    winShm.pShmNod
12bde 65 0a 2a 2a 20 20 20 20 77 69 6e 53 68 6d 2e 69  e.**    winShm.i
12bdf 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65  d.**.** All othe
12be0 72 20 66 69 65 6c 64 73 20 61 72 65 20 72 65 61  r fields are rea
12be1 64 2f 77 72 69 74 65 2e 20 20 54 68 65 20 77 69  d/write.  The wi
12be2 6e 53 68 6d 2e 70 53 68 6d 4e 6f 64 65 2d 3e 6d  nShm.pShmNode->m
12be3 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c  utex must be hel
12be4 64 0a 2a 2a 20 77 68 69 6c 65 20 61 63 63 65 73  d.** while acces
12be5 73 69 6e 67 20 61 6e 79 20 72 65 61 64 2f 77 72  sing any read/wr
12be6 69 74 65 20 66 69 65 6c 64 73 2e 0a 2a 2f 0a 73  ite fields..*/.s
12be7 74 72 75 63 74 20 77 69 6e 53 68 6d 20 7b 0a 20  truct winShm {. 
12be8 20 77 69 6e 53 68 6d 4e 6f 64 65 20 2a 70 53 68   winShmNode *pSh
12be9 6d 4e 6f 64 65 3b 20 20 20 20 20 20 2f 2a 20 54  mNode;      /* T
12bea 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 77 69  he underlying wi
12beb 6e 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63 74 20  nShmNode object 
12bec 2a 2f 0a 20 20 77 69 6e 53 68 6d 20 2a 70 4e 65  */.  winShm *pNe
12bed 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  xt;             
12bee 2f 2a 20 4e 65 78 74 20 77 69 6e 53 68 6d 20 77  /* Next winShm w
12bef 69 74 68 20 74 68 65 20 73 61 6d 65 20 77 69 6e  ith the same win
12bf0 53 68 6d 4e 6f 64 65 20 2a 2f 0a 20 20 75 38 20  ShmNode */.  u8 
12bf1 68 61 73 4d 75 74 65 78 3b 20 20 20 20 20 20 20  hasMutex;       
12bf2 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
12bf3 69 66 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 77  if holding the w
12bf4 69 6e 53 68 6d 4e 6f 64 65 20 6d 75 74 65 78 20  inShmNode mutex 
12bf5 2a 2f 0a 20 20 75 31 36 20 73 68 61 72 65 64 4d  */.  u16 sharedM
12bf6 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ask;            
12bf7 2f 2a 20 4d 61 73 6b 20 6f 66 20 73 68 61 72 65  /* Mask of share
12bf8 64 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a  d locks held */.
12bf9 20 20 75 31 36 20 65 78 63 6c 4d 61 73 6b 3b 20    u16 exclMask; 
12bfa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12bfb 4d 61 73 6b 20 6f 66 20 65 78 63 6c 75 73 69 76  Mask of exclusiv
12bfc 65 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a  e locks held */.
12bfd 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
12bfe 42 55 47 0a 20 20 75 38 20 69 64 3b 20 20 20 20  BUG.  u8 id;    
12bff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12c00 20 2f 2a 20 49 64 20 6f 66 20 74 68 69 73 20 63   /* Id of this c
12c01 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 69  onnection with i
12c02 74 73 20 77 69 6e 53 68 6d 4e 6f 64 65 20 2a 2f  ts winShmNode */
12c03 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a  .#endif.};../*.*
12c04 2a 20 43 6f 6e 73 74 61 6e 74 73 20 75 73 65 64  * Constants used
12c05 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 0a 2a 2f 0a   for locking.*/.
12c06 23 64 65 66 69 6e 65 20 57 49 4e 5f 53 48 4d 5f  #define WIN_SHM_
12c07 42 41 53 45 20 20 20 28 28 32 32 2b 53 51 4c 49  BASE   ((22+SQLI
12c08 54 45 5f 53 48 4d 5f 4e 4c 4f 43 4b 29 2a 34 29  TE_SHM_NLOCK)*4)
12c09 20 20 20 20 20 20 20 20 2f 2a 20 66 69 72 73 74          /* first
12c0a 20 6c 6f 63 6b 20 62 79 74 65 20 2a 2f 0a 23 64   lock byte */.#d
12c0b 65 66 69 6e 65 20 57 49 4e 5f 53 48 4d 5f 44 4d  efine WIN_SHM_DM
12c0c 53 20 20 20 20 28 57 49 4e 5f 53 48 4d 5f 42 41  S    (WIN_SHM_BA
12c0d 53 45 2b 53 51 4c 49 54 45 5f 53 48 4d 5f 4e 4c  SE+SQLITE_SHM_NL
12c0e 4f 43 4b 29 20 20 2f 2a 20 64 65 61 64 6d 61 6e  OCK)  /* deadman
12c0f 20 73 77 69 74 63 68 20 2a 2f 0a 0a 2f 2a 0a 2a   switch */../*.*
12c10 2a 20 41 70 70 6c 79 20 61 64 76 69 73 6f 72 79  * Apply advisory
12c11 20 6c 6f 63 6b 73 20 66 6f 72 20 61 6c 6c 20 6e   locks for all n
12c12 20 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67   bytes beginning
12c13 20 61 74 20 6f 66 73 74 2e 0a 2a 2f 0a 23 64 65   at ofst..*/.#de
12c14 66 69 6e 65 20 5f 53 48 4d 5f 55 4e 4c 43 4b 20  fine _SHM_UNLCK 
12c15 20 31 0a 23 64 65 66 69 6e 65 20 5f 53 48 4d 5f   1.#define _SHM_
12c16 52 44 4c 43 4b 20 20 32 0a 23 64 65 66 69 6e 65  RDLCK  2.#define
12c17 20 5f 53 48 4d 5f 57 52 4c 43 4b 20 20 33 0a 73   _SHM_WRLCK  3.s
12c18 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 68 6d  tatic int winShm
12c19 53 79 73 74 65 6d 4c 6f 63 6b 28 0a 20 20 77 69  SystemLock(.  wi
12c1a 6e 53 68 6d 4e 6f 64 65 20 2a 70 46 69 6c 65 2c  nShmNode *pFile,
12c1b 20 20 20 20 2f 2a 20 41 70 70 6c 79 20 6c 6f 63      /* Apply loc
12c1c 6b 73 20 74 6f 20 74 68 69 73 20 6f 70 65 6e 20  ks to this open 
12c1d 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 73 65  shared-memory se
12c1e 67 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6c  gment */.  int l
12c1f 6f 63 6b 54 79 70 65 2c 20 20 20 20 20 20 20 20  ockType,        
12c20 20 2f 2a 20 5f 53 48 4d 5f 55 4e 4c 43 4b 2c 20   /* _SHM_UNLCK, 
12c21 5f 53 48 4d 5f 52 44 4c 43 4b 2c 20 6f 72 20 5f  _SHM_RDLCK, or _
12c22 53 48 4d 5f 57 52 4c 43 4b 20 2a 2f 0a 20 20 69  SHM_WRLCK */.  i
12c23 6e 74 20 6f 66 73 74 2c 20 20 20 20 20 20 20 20  nt ofst,        
12c24 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74       /* Offset t
12c25 6f 20 66 69 72 73 74 20 62 79 74 65 20 74 6f 20  o first byte to 
12c26 62 65 20 6c 6f 63 6b 65 64 2f 75 6e 6c 6f 63 6b  be locked/unlock
12c27 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  ed */.  int nByt
12c28 65 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e             /*
12c29 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
12c2a 20 74 6f 20 6c 6f 63 6b 20 6f 72 20 75 6e 6c 6f   to lock or unlo
12c2b 63 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  ck */.){.  int r
12c2c 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  c = 0;          
12c2d 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20   /* Result code 
12c2e 66 6f 72 6d 20 4c 6f 63 6b 2f 55 6e 6c 6f 63 6b  form Lock/Unlock
12c2f 46 69 6c 65 45 78 28 29 20 2a 2f 0a 0a 20 20 2f  FileEx() */..  /
12c30 2a 20 41 63 63 65 73 73 20 74 6f 20 74 68 65 20  * Access to the 
12c31 77 69 6e 53 68 6d 4e 6f 64 65 20 6f 62 6a 65 63  winShmNode objec
12c32 74 20 69 73 20 73 65 72 69 61 6c 69 7a 65 64 20  t is serialized 
12c33 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 2a 2f  by the caller */
12c34 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
12c35 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 46  e3_mutex_held(pF
12c36 69 6c 65 2d 3e 6d 75 74 65 78 29 20 7c 7c 20 70  ile->mutex) || p
12c37 46 69 6c 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b  File->nRef==0 );
12c38 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 65 2f 41  ..  /* Release/A
12c39 63 71 75 69 72 65 20 74 68 65 20 73 79 73 74 65  cquire the syste
12c3a 6d 2d 6c 65 76 65 6c 20 6c 6f 63 6b 20 2a 2f 0a  m-level lock */.
12c3b 20 20 69 66 28 20 6c 6f 63 6b 54 79 70 65 3d 3d    if( lockType==
12c3c 5f 53 48 4d 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20  _SHM_UNLCK ){.  
12c3d 20 20 72 63 20 3d 20 77 69 6e 55 6e 6c 6f 63 6b    rc = winUnlock
12c3e 46 69 6c 65 28 26 70 46 69 6c 65 2d 3e 68 46 69  File(&pFile->hFi
12c3f 6c 65 2e 68 2c 20 6f 66 73 74 2c 20 30 2c 20 6e  le.h, ofst, 0, n
12c40 42 79 74 65 2c 20 30 29 3b 0a 20 20 7d 65 6c 73  Byte, 0);.  }els
12c41 65 7b 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61  e{.    /* Initia
12c42 6c 69 7a 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67  lize the locking
12c43 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f 0a 20   parameters */. 
12c44 20 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73     DWORD dwFlags
12c45 20 3d 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c   = LOCKFILE_FAIL
12c46 5f 49 4d 4d 45 44 49 41 54 45 4c 59 3b 0a 20 20  _IMMEDIATELY;.  
12c47 20 20 69 66 28 20 6c 6f 63 6b 54 79 70 65 20 3d    if( lockType =
12c48 3d 20 5f 53 48 4d 5f 57 52 4c 43 4b 20 29 20 64  = _SHM_WRLCK ) d
12c49 77 46 6c 61 67 73 20 7c 3d 20 4c 4f 43 4b 46 49  wFlags |= LOCKFI
12c4a 4c 45 5f 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  LE_EXCLUSIVE_LOC
12c4b 4b 3b 0a 20 20 20 20 72 63 20 3d 20 77 69 6e 4c  K;.    rc = winL
12c4c 6f 63 6b 46 69 6c 65 28 26 70 46 69 6c 65 2d 3e  ockFile(&pFile->
12c4d 68 46 69 6c 65 2e 68 2c 20 64 77 46 6c 61 67 73  hFile.h, dwFlags
12c4e 2c 20 6f 66 73 74 2c 20 30 2c 20 6e 42 79 74 65  , ofst, 0, nByte
12c4f 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 69  , 0);.  }.  .  i
12c50 66 28 20 72 63 21 3d 20 30 20 29 7b 0a 20 20 20  f( rc!= 0 ){.   
12c51 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
12c52 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 46  .  }else{.    pF
12c53 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
12c54 20 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72    osGetLastError
12c55 28 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ();.    rc = SQL
12c56 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a 20  ITE_BUSY;.  }.. 
12c57 20 4f 53 54 52 41 43 45 28 28 22 53 48 4d 2d 4c   OSTRACE(("SHM-L
12c58 4f 43 4b 20 25 64 20 25 73 20 25 73 20 30 78 25  OCK %d %s %s 0x%
12c59 30 38 6c 78 5c 6e 22 2c 20 0a 20 20 20 20 20 20  08lx\n", .      
12c5a 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 46 69 6c       pFile->hFil
12c5b 65 2e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20  e.h,.           
12c5c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20  rc==SQLITE_OK ? 
12c5d 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 2c  "ok" : "failed",
12c5e 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b  .           lock
12c5f 54 79 70 65 3d 3d 5f 53 48 4d 5f 55 4e 4c 43 4b  Type==_SHM_UNLCK
12c60 20 3f 20 22 55 6e 6c 6f 63 6b 46 69 6c 65 45 78   ? "UnlockFileEx
12c61 22 20 3a 20 22 4c 6f 63 6b 46 69 6c 65 45 78 22  " : "LockFileEx"
12c62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 46 69  ,.           pFi
12c63 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 29 29 3b  le->lastErrno));
12c64 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
12c65 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66  ../* Forward ref
12c66 65 72 65 6e 63 65 73 20 74 6f 20 56 46 53 20 6d  erences to VFS m
12c67 65 74 68 6f 64 73 20 2a 2f 0a 73 74 61 74 69 63  ethods */.static
12c68 20 69 6e 74 20 77 69 6e 4f 70 65 6e 28 73 71 6c   int winOpen(sql
12c69 69 74 65 33 5f 76 66 73 2a 2c 63 6f 6e 73 74 20  ite3_vfs*,const 
12c6a 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 66 69  char*,sqlite3_fi
12c6b 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2a 29 3b 0a 73  le*,int,int*);.s
12c6c 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 6c  tatic int winDel
12c6d 65 74 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ete(sqlite3_vfs 
12c6e 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  *,const char*,in
12c6f 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 75 72 67 65  t);../*.** Purge
12c70 20 74 68 65 20 77 69 6e 53 68 6d 4e 6f 64 65 4c   the winShmNodeL
12c71 69 73 74 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  ist list of all 
12c72 65 6e 74 72 69 65 73 20 77 69 74 68 20 77 69 6e  entries with win
12c73 53 68 6d 4e 6f 64 65 2e 6e 52 65 66 3d 3d 30 2e  ShmNode.nRef==0.
12c74 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e  .**.** This is n
12c75 6f 74 20 61 20 56 46 53 20 73 68 61 72 65 64 2d  ot a VFS shared-
12c76 6d 65 6d 6f 72 79 20 6d 65 74 68 6f 64 3b 20 69  memory method; i
12c77 74 20 69 73 20 61 20 75 74 69 6c 69 74 79 20 66  t is a utility f
12c78 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65 64 0a 2a  unction called.*
12c79 2a 20 62 79 20 56 46 53 20 73 68 61 72 65 64 2d  * by VFS shared-
12c7a 6d 65 6d 6f 72 79 20 6d 65 74 68 6f 64 73 2e 0a  memory methods..
12c7b 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
12c7c 69 6e 53 68 6d 50 75 72 67 65 28 73 71 6c 69 74  inShmPurge(sqlit
12c7d 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
12c7e 74 20 64 65 6c 65 74 65 46 6c 61 67 29 7b 0a 20  t deleteFlag){. 
12c7f 20 77 69 6e 53 68 6d 4e 6f 64 65 20 2a 2a 70 70   winShmNode **pp
12c80 3b 0a 20 20 77 69 6e 53 68 6d 4e 6f 64 65 20 2a  ;.  winShmNode *
12c81 70 3b 0a 20 20 42 4f 4f 4c 20 62 52 63 3b 0a 20  p;.  BOOL bRc;. 
12c82 20 61 73 73 65 72 74 28 20 77 69 6e 53 68 6d 4d   assert( winShmM
12c83 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 20  utexHeld() );.  
12c84 70 70 20 3d 20 26 77 69 6e 53 68 6d 4e 6f 64 65  pp = &winShmNode
12c85 4c 69 73 74 3b 0a 20 20 77 68 69 6c 65 28 20 28  List;.  while( (
12c86 70 20 3d 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20  p = *pp)!=0 ){. 
12c87 20 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d     if( p->nRef==
12c88 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  0 ){.      int i
12c89 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6d  ;.      if( p->m
12c8a 75 74 65 78 20 29 20 73 71 6c 69 74 65 33 5f 6d  utex ) sqlite3_m
12c8b 75 74 65 78 5f 66 72 65 65 28 70 2d 3e 6d 75 74  utex_free(p->mut
12c8c 65 78 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  ex);.      for(i
12c8d 3d 30 3b 20 69 3c 70 2d 3e 6e 52 65 67 69 6f 6e  =0; i<p->nRegion
12c8e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
12c8f 62 52 63 20 3d 20 6f 73 55 6e 6d 61 70 56 69 65  bRc = osUnmapVie
12c90 77 4f 66 46 69 6c 65 28 70 2d 3e 61 52 65 67 69  wOfFile(p->aRegi
12c91 6f 6e 5b 69 5d 2e 70 4d 61 70 29 3b 0a 20 20 20  on[i].pMap);.   
12c92 20 20 20 20 20 4f 53 54 52 41 43 45 28 28 22 53       OSTRACE(("S
12c93 48 4d 2d 50 55 52 47 45 20 70 69 64 2d 25 64 20  HM-PURGE pid-%d 
12c94 75 6e 6d 61 70 20 72 65 67 69 6f 6e 3d 25 64 20  unmap region=%d 
12c95 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  %s\n",.         
12c96 20 20 20 20 20 20 20 20 28 69 6e 74 29 6f 73 47          (int)osG
12c97 65 74 43 75 72 72 65 6e 74 50 72 6f 63 65 73 73  etCurrentProcess
12c98 49 64 28 29 2c 20 69 2c 0a 20 20 20 20 20 20 20  Id(), i,.       
12c99 20 20 20 20 20 20 20 20 20 20 62 52 63 20 3f 20            bRc ? 
12c9a 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29  "ok" : "failed")
12c9b 29 3b 0a 20 20 20 20 20 20 20 20 62 52 63 20 3d  );.        bRc =
12c9c 20 6f 73 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70   osCloseHandle(p
12c9d 2d 3e 61 52 65 67 69 6f 6e 5b 69 5d 2e 68 4d 61  ->aRegion[i].hMa
12c9e 70 29 3b 0a 20 20 20 20 20 20 20 20 4f 53 54 52  p);.        OSTR
12c9f 41 43 45 28 28 22 53 48 4d 2d 50 55 52 47 45 20  ACE(("SHM-PURGE 
12ca0 70 69 64 2d 25 64 20 63 6c 6f 73 65 20 72 65 67  pid-%d close reg
12ca1 69 6f 6e 3d 25 64 20 25 73 5c 6e 22 2c 0a 20 20  ion=%d %s\n",.  
12ca2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
12ca3 69 6e 74 29 6f 73 47 65 74 43 75 72 72 65 6e 74  int)osGetCurrent
12ca4 50 72 6f 63 65 73 73 49 64 28 29 2c 20 69 2c 0a  ProcessId(), i,.
12ca5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12ca6 20 62 52 63 20 3f 20 22 6f 6b 22 20 3a 20 22 66   bRc ? "ok" : "f
12ca7 61 69 6c 65 64 22 29 29 3b 0a 20 20 20 20 20 20  ailed"));.      
12ca8 7d 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 68  }.      if( p->h
12ca9 46 69 6c 65 2e 68 20 21 3d 20 49 4e 56 41 4c 49  File.h != INVALI
12caa 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 29  D_HANDLE_VALUE )
12cab 7b 0a 20 20 20 20 20 20 20 20 53 69 6d 75 6c 61  {.        Simula
12cac 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
12cad 31 29 3b 0a 20 20 20 20 20 20 20 20 77 69 6e 43  1);.        winC
12cae 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69  lose((sqlite3_fi
12caf 6c 65 20 2a 29 26 70 2d 3e 68 46 69 6c 65 29 3b  le *)&p->hFile);
12cb0 0a 20 20 20 20 20 20 20 20 53 69 6d 75 6c 61 74  .        Simulat
12cb1 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30  eIOErrorBenign(0
12cb2 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
12cb3 20 69 66 28 20 64 65 6c 65 74 65 46 6c 61 67 20   if( deleteFlag 
12cb4 29 7b 0a 20 20 20 20 20 20 20 20 53 69 6d 75 6c  ){.        Simul
12cb5 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
12cb6 28 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  (1);.        sql
12cb7 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
12cb8 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20  alloc();.       
12cb9 20 77 69 6e 44 65 6c 65 74 65 28 70 56 66 73 2c   winDelete(pVfs,
12cba 20 70 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 30   p->zFilename, 0
12cbb 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
12cbc 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
12cbd 63 28 29 3b 0a 20 20 20 20 20 20 20 20 53 69 6d  c();.        Sim
12cbe 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69  ulateIOErrorBeni
12cbf 67 6e 28 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  gn(0);.      }. 
12cc0 20 20 20 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e       *pp = p->pN
12cc1 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ext;.      sqlit
12cc2 65 33 5f 66 72 65 65 28 70 2d 3e 61 52 65 67 69  e3_free(p->aRegi
12cc3 6f 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  on);.      sqlit
12cc4 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20  e3_free(p);.    
12cc5 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 70 20  }else{.      pp 
12cc6 3d 20 26 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  = &p->pNext;.   
12cc7 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
12cc8 4f 70 65 6e 20 74 68 65 20 73 68 61 72 65 64 2d  Open the shared-
12cc9 6d 65 6d 6f 72 79 20 61 72 65 61 20 61 73 73 6f  memory area asso
12cca 63 69 61 74 65 64 20 77 69 74 68 20 64 61 74 61  ciated with data
12ccb 62 61 73 65 20 66 69 6c 65 20 70 44 62 46 64 2e  base file pDbFd.
12ccc 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 6f 70 65 6e  .**.** When open
12ccd 69 6e 67 20 61 20 6e 65 77 20 73 68 61 72 65 64  ing a new shared
12cce 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2c 20 69 66  -memory file, if
12ccf 20 6e 6f 20 6f 74 68 65 72 20 69 6e 73 74 61 6e   no other instan
12cd0 63 65 73 20 6f 66 20 74 68 61 74 0a 2a 2a 20 66  ces of that.** f
12cd1 69 6c 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c  ile are currentl
12cd2 79 20 6f 70 65 6e 2c 20 69 6e 20 74 68 69 73 20  y open, in this 
12cd3 70 72 6f 63 65 73 73 20 6f 72 20 69 6e 20 6f 74  process or in ot
12cd4 68 65 72 20 70 72 6f 63 65 73 73 65 73 2c 20 74  her processes, t
12cd5 68 65 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20  hen.** the file 
12cd6 6d 75 73 74 20 62 65 20 74 72 75 6e 63 61 74 65  must be truncate
12cd7 64 20 74 6f 20 7a 65 72 6f 20 6c 65 6e 67 74 68  d to zero length
12cd8 20 6f 72 20 68 61 76 65 20 69 74 73 20 68 65 61   or have its hea
12cd9 64 65 72 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a  der cleared..*/.
12cda 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4f 70  static int winOp
12cdb 65 6e 53 68 61 72 65 64 4d 65 6d 6f 72 79 28 77  enSharedMemory(w
12cdc 69 6e 46 69 6c 65 20 2a 70 44 62 46 64 29 7b 0a  inFile *pDbFd){.
12cdd 20 20 73 74 72 75 63 74 20 77 69 6e 53 68 6d 20    struct winShm 
12cde 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *p;             
12cdf 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 6e       /* The conn
12ce0 65 63 74 69 6f 6e 20 74 6f 20 62 65 20 6f 70 65  ection to be ope
12ce1 6e 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ned */.  struct 
12ce2 77 69 6e 53 68 6d 4e 6f 64 65 20 2a 70 53 68 6d  winShmNode *pShm
12ce3 4e 6f 64 65 20 3d 20 30 3b 20 20 20 2f 2a 20 54  Node = 0;   /* T
12ce4 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 6d  he underlying mm
12ce5 61 70 70 65 64 20 66 69 6c 65 20 2a 2f 0a 20 20  apped file */.  
12ce6 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
12ce7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12ce8 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64     /* Result cod
12ce9 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 77 69  e */.  struct wi
12cea 6e 53 68 6d 4e 6f 64 65 20 2a 70 4e 65 77 3b 20  nShmNode *pNew; 
12ceb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77            /* New
12cec 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 77 69 6e  ly allocated win
12ced 53 68 6d 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74  ShmNode */.  int
12cee 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20   nName;         
12cef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12cf0 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 4e 61 6d 65  /* Size of zName
12cf1 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 0a 20 20   in bytes */..  
12cf2 61 73 73 65 72 74 28 20 70 44 62 46 64 2d 3e 70  assert( pDbFd->p
12cf3 53 68 6d 3d 3d 30 20 29 3b 20 20 20 20 2f 2a 20  Shm==0 );    /* 
12cf4 4e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 6f  Not previously o
12cf5 70 65 6e 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 41  pened */..  /* A
12cf6 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f  llocate space fo
12cf7 72 20 74 68 65 20 6e 65 77 20 73 71 6c 69 74 65  r the new sqlite
12cf8 33 5f 73 68 6d 20 6f 62 6a 65 63 74 2e 20 20 41  3_shm object.  A
12cf9 6c 73 6f 20 73 70 65 63 75 6c 61 74 69 76 65 6c  lso speculativel
12cfa 79 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20  y.  ** allocate 
12cfb 73 70 61 63 65 20 66 6f 72 20 61 20 6e 65 77 20  space for a new 
12cfc 77 69 6e 53 68 6d 4e 6f 64 65 20 61 6e 64 20 66  winShmNode and f
12cfd 69 6c 65 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20  ilename..  */.  
12cfe 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  p = sqlite3Mallo
12cff 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70  cZero( sizeof(*p
12d00 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  ) );.  if( p==0 
12d01 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
12d02 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 6e  IOERR_NOMEM;.  n
12d03 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
12d04 72 6c 65 6e 33 30 28 70 44 62 46 64 2d 3e 7a 50  rlen30(pDbFd->zP
12d05 61 74 68 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73  ath);.  pNew = s
12d06 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
12d07 28 20 73 69 7a 65 6f 66 28 2a 70 53 68 6d 4e 6f  ( sizeof(*pShmNo
12d08 64 65 29 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 37  de) + nName + 17
12d09 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d   );.  if( pNew==
12d0a 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
12d0b 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 72 65  _free(p);.    re
12d0c 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
12d0d 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70  R_NOMEM;.  }.  p
12d0e 4e 65 77 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20 3d  New->zFilename =
12d0f 20 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d   (char*)&pNew[1]
12d10 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  ;.  sqlite3_snpr
12d11 69 6e 74 66 28 6e 4e 61 6d 65 2b 31 35 2c 20 70  intf(nName+15, p
12d12 4e 65 77 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20  New->zFilename, 
12d13 22 25 73 2d 73 68 6d 22 2c 20 70 44 62 46 64 2d  "%s-shm", pDbFd-
12d14 3e 7a 50 61 74 68 29 3b 0a 20 20 73 71 6c 69 74  >zPath);.  sqlit
12d15 65 33 46 69 6c 65 53 75 66 66 69 78 33 28 70 44  e3FileSuffix3(pD
12d16 62 46 64 2d 3e 7a 50 61 74 68 2c 20 70 4e 65 77  bFd->zPath, pNew
12d17 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 0a 0a  ->zFilename); ..
12d18 20 20 2f 2a 20 4c 6f 6f 6b 20 74 6f 20 73 65 65    /* Look to see
12d19 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   if there is an 
12d1a 65 78 69 73 74 69 6e 67 20 77 69 6e 53 68 6d 4e  existing winShmN
12d1b 6f 64 65 20 74 68 61 74 20 63 61 6e 20 62 65 20  ode that can be 
12d1c 75 73 65 64 2e 0a 20 20 2a 2a 20 49 66 20 6e 6f  used..  ** If no
12d1d 20 6d 61 74 63 68 69 6e 67 20 77 69 6e 53 68 6d   matching winShm
12d1e 4e 6f 64 65 20 63 75 72 72 65 6e 74 6c 79 20 65  Node currently e
12d1f 78 69 73 74 73 2c 20 63 72 65 61 74 65 20 61 20  xists, create a 
12d20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20  new one..  */.  
12d21 77 69 6e 53 68 6d 45 6e 74 65 72 4d 75 74 65 78  winShmEnterMutex
12d22 28 29 3b 0a 20 20 66 6f 72 28 70 53 68 6d 4e 6f  ();.  for(pShmNo
12d23 64 65 20 3d 20 77 69 6e 53 68 6d 4e 6f 64 65 4c  de = winShmNodeL
12d24 69 73 74 3b 20 70 53 68 6d 4e 6f 64 65 3b 20 70  ist; pShmNode; p
12d25 53 68 6d 4e 6f 64 65 3d 70 53 68 6d 4e 6f 64 65  ShmNode=pShmNode
12d26 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 2f 2a  ->pNext){.    /*
12d27 20 54 42 44 20 6e 65 65 64 20 74 6f 20 63 6f 6d   TBD need to com
12d28 65 20 75 70 20 77 69 74 68 20 62 65 74 74 65 72  e up with better
12d29 20 6d 61 74 63 68 20 68 65 72 65 2e 20 20 50 65   match here.  Pe
12d2a 72 68 61 70 73 0a 20 20 20 20 2a 2a 20 75 73 65  rhaps.    ** use
12d2b 20 46 49 4c 45 5f 49 44 5f 42 4f 54 48 5f 44 49   FILE_ID_BOTH_DI
12d2c 52 5f 49 4e 46 4f 20 53 74 72 75 63 74 75 72 65  R_INFO Structure
12d2d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
12d2e 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
12d2f 70 53 68 6d 4e 6f 64 65 2d 3e 7a 46 69 6c 65 6e  pShmNode->zFilen
12d30 61 6d 65 2c 20 70 4e 65 77 2d 3e 7a 46 69 6c 65  ame, pNew->zFile
12d31 6e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b  name)==0 ) break
12d32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 68 6d  ;.  }.  if( pShm
12d33 4e 6f 64 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  Node ){.    sqli
12d34 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29 3b 0a  te3_free(pNew);.
12d35 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 53 68    }else{.    pSh
12d36 6d 4e 6f 64 65 20 3d 20 70 4e 65 77 3b 0a 20 20  mNode = pNew;.  
12d37 20 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20    pNew = 0;.    
12d38 28 28 77 69 6e 46 69 6c 65 2a 29 28 26 70 53 68  ((winFile*)(&pSh
12d39 6d 4e 6f 64 65 2d 3e 68 46 69 6c 65 29 29 2d 3e  mNode->hFile))->
12d3a 68 20 3d 20 49 4e 56 41 4c 49 44 5f 48 41 4e 44  h = INVALID_HAND
12d3b 4c 45 5f 56 41 4c 55 45 3b 0a 20 20 20 20 70 53  LE_VALUE;.    pS
12d3c 68 6d 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20  hmNode->pNext = 
12d3d 77 69 6e 53 68 6d 4e 6f 64 65 4c 69 73 74 3b 0a  winShmNodeList;.
12d3e 20 20 20 20 77 69 6e 53 68 6d 4e 6f 64 65 4c 69      winShmNodeLi
12d3f 73 74 20 3d 20 70 53 68 6d 4e 6f 64 65 3b 0a 0a  st = pShmNode;..
12d40 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 6d 75      pShmNode->mu
12d41 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75  tex = sqlite3_mu
12d42 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45  tex_alloc(SQLITE
12d43 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20  _MUTEX_FAST);.  
12d44 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e    if( pShmNode->
12d45 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20  mutex==0 ){.    
12d46 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
12d47 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ERR_NOMEM;.     
12d48 20 67 6f 74 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65   goto shm_open_e
12d49 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72  rr;.    }..    r
12d4a 63 20 3d 20 77 69 6e 4f 70 65 6e 28 70 44 62 46  c = winOpen(pDbF
12d4b 64 2d 3e 70 56 66 73 2c 0a 20 20 20 20 20 20 20  d->pVfs,.       
12d4c 20 20 20 20 20 20 20 20 20 20 70 53 68 6d 4e 6f            pShmNo
12d4d 64 65 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 20  de->zFilename,  
12d4e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
12d4f 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 28  me of the file (
12d50 55 54 46 2d 38 29 20 2a 2f 0a 20 20 20 20 20 20  UTF-8) */.      
12d51 20 20 20 20 20 20 20 20 20 20 20 28 73 71 6c 69             (sqli
12d52 74 65 33 5f 66 69 6c 65 2a 29 26 70 53 68 6d 4e  te3_file*)&pShmN
12d53 6f 64 65 2d 3e 68 46 69 6c 65 2c 20 20 2f 2a 20  ode->hFile,  /* 
12d54 46 69 6c 65 20 68 61 6e 64 6c 65 20 68 65 72 65  File handle here
12d55 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
12d56 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
12d57 5f 57 41 4c 20 7c 20 53 51 4c 49 54 45 5f 4f 50  _WAL | SQLITE_OP
12d58 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53  EN_READWRITE | S
12d59 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
12d5a 45 2c 20 2f 2a 20 4d 6f 64 65 20 66 6c 61 67 73  E, /* Mode flags
12d5b 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
12d5c 20 20 20 20 20 30 29 3b 0a 20 20 20 20 69 66 28       0);.    if(
12d5d 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29   SQLITE_OK!=rc )
12d5e 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 68 6d  {.      goto shm
12d5f 5f 6f 70 65 6e 5f 65 72 72 3b 0a 20 20 20 20 7d  _open_err;.    }
12d60 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74  ..    /* Check t
12d61 6f 20 73 65 65 20 69 66 20 61 6e 6f 74 68 65 72  o see if another
12d62 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64   process is hold
12d63 69 6e 67 20 74 68 65 20 64 65 61 64 2d 6d 61 6e  ing the dead-man
12d64 20 73 77 69 74 63 68 2e 0a 20 20 20 20 2a 2a 20   switch..    ** 
12d65 49 66 20 6e 6f 74 2c 20 74 72 75 6e 63 61 74 65  If not, truncate
12d66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 7a 65 72   the file to zer
12d67 6f 20 6c 65 6e 67 74 68 2e 20 0a 20 20 20 20 2a  o length. .    *
12d68 2f 0a 20 20 20 20 69 66 28 20 77 69 6e 53 68 6d  /.    if( winShm
12d69 53 79 73 74 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e  SystemLock(pShmN
12d6a 6f 64 65 2c 20 5f 53 48 4d 5f 57 52 4c 43 4b 2c  ode, _SHM_WRLCK,
12d6b 20 57 49 4e 5f 53 48 4d 5f 44 4d 53 2c 20 31 29   WIN_SHM_DMS, 1)
12d6c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
12d6d 20 20 20 20 20 72 63 20 3d 20 77 69 6e 54 72 75       rc = winTru
12d6e 6e 63 61 74 65 28 28 73 71 6c 69 74 65 33 5f 66  ncate((sqlite3_f
12d6f 69 6c 65 20 2a 29 26 70 53 68 6d 4e 6f 64 65 2d  ile *)&pShmNode-
12d70 3e 68 46 69 6c 65 2c 20 30 29 3b 0a 20 20 20 20  >hFile, 0);.    
12d71 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12d72 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
12d73 63 20 3d 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28  c = winLogError(
12d74 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d  SQLITE_IOERR_SHM
12d75 4f 50 45 4e 2c 20 6f 73 47 65 74 4c 61 73 74 45  OPEN, osGetLastE
12d76 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20 20  rror(),.        
12d77 20 20 20 20 20 20 20 20 20 22 77 69 6e 4f 70 65           "winOpe
12d78 6e 53 68 6d 22 2c 20 70 44 62 46 64 2d 3e 7a 50  nShm", pDbFd->zP
12d79 61 74 68 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ath);.      }.  
12d7a 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
12d7b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
12d7c 20 20 20 77 69 6e 53 68 6d 53 79 73 74 65 6d 4c     winShmSystemL
12d7d 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 2c 20 5f 53  ock(pShmNode, _S
12d7e 48 4d 5f 55 4e 4c 43 4b 2c 20 57 49 4e 5f 53 48  HM_UNLCK, WIN_SH
12d7f 4d 5f 44 4d 53 2c 20 31 29 3b 0a 20 20 20 20 20  M_DMS, 1);.     
12d80 20 72 63 20 3d 20 77 69 6e 53 68 6d 53 79 73 74   rc = winShmSyst
12d81 65 6d 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 2c  emLock(pShmNode,
12d82 20 5f 53 48 4d 5f 52 44 4c 43 4b 2c 20 57 49 4e   _SHM_RDLCK, WIN
12d83 5f 53 48 4d 5f 44 4d 53 2c 20 31 29 3b 0a 20 20  _SHM_DMS, 1);.  
12d84 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29    }.    if( rc )
12d85 20 67 6f 74 6f 20 73 68 6d 5f 6f 70 65 6e 5f 65   goto shm_open_e
12d86 72 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61  rr;.  }..  /* Ma
12d87 6b 65 20 74 68 65 20 6e 65 77 20 63 6f 6e 6e 65  ke the new conne
12d88 63 74 69 6f 6e 20 61 20 63 68 69 6c 64 20 6f 66  ction a child of
12d89 20 74 68 65 20 77 69 6e 53 68 6d 4e 6f 64 65 20   the winShmNode 
12d8a 2a 2f 0a 20 20 70 2d 3e 70 53 68 6d 4e 6f 64 65  */.  p->pShmNode
12d8b 20 3d 20 70 53 68 6d 4e 6f 64 65 3b 0a 23 69 66   = pShmNode;.#if
12d8c 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
12d8d 0a 20 20 70 2d 3e 69 64 20 3d 20 70 53 68 6d 4e  .  p->id = pShmN
12d8e 6f 64 65 2d 3e 6e 65 78 74 53 68 6d 49 64 2b 2b  ode->nextShmId++
12d8f 3b 0a 23 65 6e 64 69 66 0a 20 20 70 53 68 6d 4e  ;.#endif.  pShmN
12d90 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 70  ode->nRef++;.  p
12d91 44 62 46 64 2d 3e 70 53 68 6d 20 3d 20 70 3b 0a  DbFd->pShm = p;.
12d92 20 20 77 69 6e 53 68 6d 4c 65 61 76 65 4d 75 74    winShmLeaveMut
12d93 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  ex();..  /* The 
12d94 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20  reference count 
12d95 6f 6e 20 70 53 68 6d 4e 6f 64 65 20 68 61 73 20  on pShmNode has 
12d96 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e 63  already been inc
12d97 72 65 6d 65 6e 74 65 64 20 75 6e 64 65 72 0a 20  remented under. 
12d98 20 2a 2a 20 74 68 65 20 63 6f 76 65 72 20 6f 66   ** the cover of
12d99 20 74 68 65 20 77 69 6e 53 68 6d 45 6e 74 65 72   the winShmEnter
12d9a 4d 75 74 65 78 28 29 20 6d 75 74 65 78 20 61 6e  Mutex() mutex an
12d9b 64 20 74 68 65 20 70 6f 69 6e 74 65 72 20 66 72  d the pointer fr
12d9c 6f 6d 20 74 68 65 0a 20 20 2a 2a 20 6e 65 77 20  om the.  ** new 
12d9d 28 73 74 72 75 63 74 20 77 69 6e 53 68 6d 29 20  (struct winShm) 
12d9e 6f 62 6a 65 63 74 20 74 6f 20 74 68 65 20 70 53  object to the pS
12d9f 68 6d 4e 6f 64 65 20 68 61 73 20 62 65 65 6e 20  hmNode has been 
12da0 73 65 74 2e 20 41 6c 6c 20 74 68 61 74 20 69 73  set. All that is
12da1 0a 20 20 2a 2a 20 6c 65 66 74 20 74 6f 20 64 6f  .  ** left to do
12da2 20 69 73 20 74 6f 20 6c 69 6e 6b 20 74 68 65 20   is to link the 
12da3 6e 65 77 20 6f 62 6a 65 63 74 20 69 6e 74 6f 20  new object into 
12da4 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  the linked list 
12da5 73 74 61 72 74 69 6e 67 0a 20 20 2a 2a 20 61 74  starting.  ** at
12da6 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73   pShmNode->pFirs
12da7 74 2e 20 54 68 69 73 20 6d 75 73 74 20 62 65 20  t. This must be 
12da8 64 6f 6e 65 20 77 68 69 6c 65 20 68 6f 6c 64 69  done while holdi
12da9 6e 67 20 74 68 65 20 70 53 68 6d 4e 6f 64 65 2d  ng the pShmNode-
12daa 3e 6d 75 74 65 78 20 0a 20 20 2a 2a 20 6d 75 74  >mutex .  ** mut
12dab 65 78 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ex..  */.  sqlit
12dac 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
12dad 53 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b  ShmNode->mutex);
12dae 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53  .  p->pNext = pS
12daf 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 74 3b 0a  hmNode->pFirst;.
12db0 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72    pShmNode->pFir
12db1 73 74 20 3d 20 70 3b 0a 20 20 73 71 6c 69 74 65  st = p;.  sqlite
12db2 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 53  3_mutex_leave(pS
12db3 68 6d 4e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0a  hmNode->mutex);.
12db4 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
12db5 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68  OK;..  /* Jump h
12db6 65 72 65 20 6f 6e 20 61 6e 79 20 65 72 72 6f 72  ere on any error
12db7 20 2a 2f 0a 73 68 6d 5f 6f 70 65 6e 5f 65 72 72   */.shm_open_err
12db8 3a 0a 20 20 77 69 6e 53 68 6d 53 79 73 74 65 6d  :.  winShmSystem
12db9 4c 6f 63 6b 28 70 53 68 6d 4e 6f 64 65 2c 20 5f  Lock(pShmNode, _
12dba 53 48 4d 5f 55 4e 4c 43 4b 2c 20 57 49 4e 5f 53  SHM_UNLCK, WIN_S
12dbb 48 4d 5f 44 4d 53 2c 20 31 29 3b 0a 20 20 77 69  HM_DMS, 1);.  wi
12dbc 6e 53 68 6d 50 75 72 67 65 28 70 44 62 46 64 2d  nShmPurge(pDbFd-
12dbd 3e 70 56 66 73 2c 20 30 29 3b 20 20 20 20 20 20  >pVfs, 0);      
12dbe 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 66 72 65  /* This call fre
12dbf 65 73 20 70 53 68 6d 4e 6f 64 65 20 69 66 20 72  es pShmNode if r
12dc0 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 73 71 6c  equired */.  sql
12dc1 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
12dc2 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65  sqlite3_free(pNe
12dc3 77 29 3b 0a 20 20 77 69 6e 53 68 6d 4c 65 61 76  w);.  winShmLeav
12dc4 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75  eMutex();.  retu
12dc5 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
12dc6 43 6c 6f 73 65 20 61 20 63 6f 6e 6e 65 63 74 69  Close a connecti
12dc7 6f 6e 20 74 6f 20 73 68 61 72 65 64 2d 6d 65 6d  on to shared-mem
12dc8 6f 72 79 2e 20 20 44 65 6c 65 74 65 20 74 68 65  ory.  Delete the
12dc9 20 75 6e 64 65 72 6c 79 69 6e 67 20 0a 2a 2a 20   underlying .** 
12dca 73 74 6f 72 61 67 65 20 69 66 20 64 65 6c 65 74  storage if delet
12dcb 65 46 6c 61 67 20 69 73 20 74 72 75 65 2e 0a 2a  eFlag is true..*
12dcc 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
12dcd 53 68 6d 55 6e 6d 61 70 28 0a 20 20 73 71 6c 69  ShmUnmap(.  sqli
12dce 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 20 20  te3_file *fd,   
12dcf 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
12dd0 73 65 20 68 6f 6c 64 69 6e 67 20 73 68 61 72 65  se holding share
12dd1 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e  d memory */.  in
12dd2 74 20 64 65 6c 65 74 65 46 6c 61 67 20 20 20 20  t deleteFlag    
12dd3 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65           /* Dele
12dd4 74 65 20 61 66 74 65 72 20 63 6c 6f 73 69 6e 67  te after closing
12dd5 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20   if true */.){. 
12dd6 20 77 69 6e 46 69 6c 65 20 2a 70 44 62 46 64 3b   winFile *pDbFd;
12dd7 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
12dd8 73 65 20 68 6f 6c 64 69 6e 67 20 73 68 61 72 65  se holding share
12dd9 64 2d 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 77 69  d-memory */.  wi
12dda 6e 53 68 6d 20 2a 70 3b 20 20 20 20 20 20 20 20  nShm *p;        
12ddb 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 6e 65      /* The conne
12ddc 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 6c 6f 73  ction to be clos
12ddd 65 64 20 2a 2f 0a 20 20 77 69 6e 53 68 6d 4e 6f  ed */.  winShmNo
12dde 64 65 20 2a 70 53 68 6d 4e 6f 64 65 3b 20 2f 2a  de *pShmNode; /*
12ddf 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   The underlying 
12de0 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 66 69  shared-memory fi
12de1 6c 65 20 2a 2f 0a 20 20 77 69 6e 53 68 6d 20 2a  le */.  winShm *
12de2 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  *pp;          /*
12de3 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
12de4 72 20 73 69 62 6c 69 6e 67 20 63 6f 6e 6e 65 63  r sibling connec
12de5 74 69 6f 6e 73 20 2a 2f 0a 0a 20 20 70 44 62 46  tions */..  pDbF
12de6 64 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 66 64  d = (winFile*)fd
12de7 3b 0a 20 20 70 20 3d 20 70 44 62 46 64 2d 3e 70  ;.  p = pDbFd->p
12de8 53 68 6d 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  Shm;.  if( p==0 
12de9 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
12dea 4f 4b 3b 0a 20 20 70 53 68 6d 4e 6f 64 65 20 3d  OK;.  pShmNode =
12deb 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 3b 0a 0a 20   p->pShmNode;.. 
12dec 20 2f 2a 20 52 65 6d 6f 76 65 20 63 6f 6e 6e 65   /* Remove conne
12ded 63 74 69 6f 6e 20 70 20 66 72 6f 6d 20 74 68 65  ction p from the
12dee 20 73 65 74 20 6f 66 20 63 6f 6e 6e 65 63 74 69   set of connecti
12def 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64 0a 20  ons associated. 
12df0 20 2a 2a 20 77 69 74 68 20 70 53 68 6d 4e 6f 64   ** with pShmNod
12df1 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d  e */.  sqlite3_m
12df2 75 74 65 78 5f 65 6e 74 65 72 28 70 53 68 6d 4e  utex_enter(pShmN
12df3 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 66  ode->mutex);.  f
12df4 6f 72 28 70 70 3d 26 70 53 68 6d 4e 6f 64 65 2d  or(pp=&pShmNode-
12df5 3e 70 46 69 72 73 74 3b 20 28 2a 70 70 29 21 3d  >pFirst; (*pp)!=
12df6 70 3b 20 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e  p; pp = &(*pp)->
12df7 70 4e 65 78 74 29 7b 7d 0a 20 20 2a 70 70 20 3d  pNext){}.  *pp =
12df8 20 70 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 2f 2a   p->pNext;..  /*
12df9 20 46 72 65 65 20 74 68 65 20 63 6f 6e 6e 65 63   Free the connec
12dfa 74 69 6f 6e 20 70 20 2a 2f 0a 20 20 73 71 6c 69  tion p */.  sqli
12dfb 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 70  te3_free(p);.  p
12dfc 44 62 46 64 2d 3e 70 53 68 6d 20 3d 20 30 3b 0a  DbFd->pShm = 0;.
12dfd 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
12dfe 6c 65 61 76 65 28 70 53 68 6d 4e 6f 64 65 2d 3e  leave(pShmNode->
12dff 6d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 49 66  mutex);..  /* If
12e00 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 66 20   pShmNode->nRef 
12e01 68 61 73 20 72 65 61 63 68 65 64 20 30 2c 20 74  has reached 0, t
12e02 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 75 6e  hen close the un
12e03 64 65 72 6c 79 69 6e 67 0a 20 20 2a 2a 20 73 68  derlying.  ** sh
12e04 61 72 65 64 2d 6d 65 6d 6f 72 79 20 66 69 6c 65  ared-memory file
12e05 2c 20 74 6f 6f 20 2a 2f 0a 20 20 77 69 6e 53 68  , too */.  winSh
12e06 6d 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  mEnterMutex();. 
12e07 20 61 73 73 65 72 74 28 20 70 53 68 6d 4e 6f 64   assert( pShmNod
12e08 65 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 70  e->nRef>0 );.  p
12e09 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 66 2d 2d 3b  ShmNode->nRef--;
12e0a 0a 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d  .  if( pShmNode-
12e0b 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
12e0c 77 69 6e 53 68 6d 50 75 72 67 65 28 70 44 62 46  winShmPurge(pDbF
12e0d 64 2d 3e 70 56 66 73 2c 20 64 65 6c 65 74 65 46  d->pVfs, deleteF
12e0e 6c 61 67 29 3b 0a 20 20 7d 0a 20 20 77 69 6e 53  lag);.  }.  winS
12e0f 68 6d 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  hmLeaveMutex();.
12e10 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
12e11 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  _OK;.}../*.** Ch
12e12 61 6e 67 65 20 74 68 65 20 6c 6f 63 6b 20 73 74  ange the lock st
12e13 61 74 65 20 66 6f 72 20 61 20 73 68 61 72 65 64  ate for a shared
12e14 2d 6d 65 6d 6f 72 79 20 73 65 67 6d 65 6e 74 2e  -memory segment.
12e15 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
12e16 69 6e 53 68 6d 4c 6f 63 6b 28 0a 20 20 73 71 6c  inShmLock(.  sql
12e17 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 20  ite3_file *fd,  
12e18 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
12e19 61 73 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67  ase file holding
12e1a 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f   the shared memo
12e1b 72 79 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 73 74  ry */.  int ofst
12e1c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
12e1d 20 20 20 2f 2a 20 46 69 72 73 74 20 6c 6f 63 6b     /* First lock
12e1e 20 74 6f 20 61 63 71 75 69 72 65 20 6f 72 20 72   to acquire or r
12e1f 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20  elease */.  int 
12e20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
12e21 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
12e22 20 6f 66 20 6c 6f 63 6b 73 20 74 6f 20 61 63 71   of locks to acq
12e23 75 69 72 65 20 6f 72 20 72 65 6c 65 61 73 65 20  uire or release 
12e24 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 20  */.  int flags  
12e25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12e26 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69  /* What to do wi
12e27 74 68 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 29  th the lock */.)
12e28 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 44 62  {.  winFile *pDb
12e29 46 64 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 66  Fd = (winFile*)f
12e2a 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  d;        /* Con
12e2b 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20  nection holding 
12e2c 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 2a 2f  shared memory */
12e2d 0a 20 20 77 69 6e 53 68 6d 20 2a 70 20 3d 20 70  .  winShm *p = p
12e2e 44 62 46 64 2d 3e 70 53 68 6d 3b 20 20 20 20 20  DbFd->pShm;     
12e2f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
12e30 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 62 65  shared memory be
12e31 69 6e 67 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20  ing locked */.  
12e32 77 69 6e 53 68 6d 20 2a 70 58 3b 20 20 20 20 20  winShm *pX;     
12e33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12e34 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f        /* For loo
12e35 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 73 69  ping over all si
12e36 62 6c 69 6e 67 73 20 2a 2f 0a 20 20 77 69 6e 53  blings */.  winS
12e37 68 6d 4e 6f 64 65 20 2a 70 53 68 6d 4e 6f 64 65  hmNode *pShmNode
12e38 20 3d 20 70 2d 3e 70 53 68 6d 4e 6f 64 65 3b 0a   = p->pShmNode;.
12e39 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
12e3a 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20  E_OK;           
12e3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
12e3c 74 20 63 6f 64 65 20 2a 2f 0a 20 20 75 31 36 20  t code */.  u16 
12e3d 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20  mask;           
12e3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12e3f 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6c 6f 63    /* Mask of loc
12e40 6b 73 20 74 6f 20 74 61 6b 65 20 6f 72 20 72 65  ks to take or re
12e41 6c 65 61 73 65 20 2a 2f 0a 0a 20 20 61 73 73 65  lease */..  asse
12e42 72 74 28 20 6f 66 73 74 3e 3d 30 20 26 26 20 6f  rt( ofst>=0 && o
12e43 66 73 74 2b 6e 3c 3d 53 51 4c 49 54 45 5f 53 48  fst+n<=SQLITE_SH
12e44 4d 5f 4e 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  M_NLOCK );.  ass
12e45 65 72 74 28 20 6e 3e 3d 31 20 29 3b 0a 20 20 61  ert( n>=1 );.  a
12e46 73 73 65 72 74 28 20 66 6c 61 67 73 3d 3d 28 53  ssert( flags==(S
12e47 51 4c 49 54 45 5f 53 48 4d 5f 4c 4f 43 4b 20 7c  QLITE_SHM_LOCK |
12e48 20 53 51 4c 49 54 45 5f 53 48 4d 5f 53 48 41 52   SQLITE_SHM_SHAR
12e49 45 44 29 0a 20 20 20 20 20 20 20 7c 7c 20 66 6c  ED).       || fl
12e4a 61 67 73 3d 3d 28 53 51 4c 49 54 45 5f 53 48 4d  ags==(SQLITE_SHM
12e4b 5f 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53  _LOCK | SQLITE_S
12e4c 48 4d 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 20  HM_EXCLUSIVE).  
12e4d 20 20 20 20 20 7c 7c 20 66 6c 61 67 73 3d 3d 28       || flags==(
12e4e 53 51 4c 49 54 45 5f 53 48 4d 5f 55 4e 4c 4f 43  SQLITE_SHM_UNLOC
12e4f 4b 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f 53  K | SQLITE_SHM_S
12e50 48 41 52 45 44 29 0a 20 20 20 20 20 20 20 7c 7c  HARED).       ||
12e51 20 66 6c 61 67 73 3d 3d 28 53 51 4c 49 54 45 5f   flags==(SQLITE_
12e52 53 48 4d 5f 55 4e 4c 4f 43 4b 20 7c 20 53 51 4c  SHM_UNLOCK | SQL
12e53 49 54 45 5f 53 48 4d 5f 45 58 43 4c 55 53 49 56  ITE_SHM_EXCLUSIV
12e54 45 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  E) );.  assert( 
12e55 6e 3d 3d 31 20 7c 7c 20 28 66 6c 61 67 73 20 26  n==1 || (flags &
12e56 20 53 51 4c 49 54 45 5f 53 48 4d 5f 45 58 43 4c   SQLITE_SHM_EXCL
12e57 55 53 49 56 45 29 21 3d 30 20 29 3b 0a 0a 20 20  USIVE)!=0 );..  
12e58 6d 61 73 6b 20 3d 20 28 75 31 36 29 28 28 31 55  mask = (u16)((1U
12e59 3c 3c 28 6f 66 73 74 2b 6e 29 29 20 2d 20 28 31  <<(ofst+n)) - (1
12e5a 55 3c 3c 6f 66 73 74 29 29 3b 0a 20 20 61 73 73  U<<ofst));.  ass
12e5b 65 72 74 28 20 6e 3e 31 20 7c 7c 20 6d 61 73 6b  ert( n>1 || mask
12e5c 3d 3d 28 31 3c 3c 6f 66 73 74 29 20 29 3b 0a 20  ==(1<<ofst) );. 
12e5d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
12e5e 6e 74 65 72 28 70 53 68 6d 4e 6f 64 65 2d 3e 6d  nter(pShmNode->m
12e5f 75 74 65 78 29 3b 0a 20 20 69 66 28 20 66 6c 61  utex);.  if( fla
12e60 67 73 20 26 20 53 51 4c 49 54 45 5f 53 48 4d 5f  gs & SQLITE_SHM_
12e61 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 75 31  UNLOCK ){.    u1
12e62 36 20 61 6c 6c 4d 61 73 6b 20 3d 20 30 3b 20 2f  6 allMask = 0; /
12e63 2a 20 4d 61 73 6b 20 6f 66 20 6c 6f 63 6b 73 20  * Mask of locks 
12e64 68 65 6c 64 20 62 79 20 73 69 62 6c 69 6e 67 73  held by siblings
12e65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 65 20   */..    /* See 
12e66 69 66 20 61 6e 79 20 73 69 62 6c 69 6e 67 73 20  if any siblings 
12e67 68 6f 6c 64 20 74 68 69 73 20 73 61 6d 65 20 6c  hold this same l
12e68 6f 63 6b 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70  ock */.    for(p
12e69 58 3d 70 53 68 6d 4e 6f 64 65 2d 3e 70 46 69 72  X=pShmNode->pFir
12e6a 73 74 3b 20 70 58 3b 20 70 58 3d 70 58 2d 3e 70  st; pX; pX=pX->p
12e6b 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28  Next){.      if(
12e6c 20 70 58 3d 3d 70 20 29 20 63 6f 6e 74 69 6e 75   pX==p ) continu
12e6d 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e;.      assert(
12e6e 20 28 70 58 2d 3e 65 78 63 6c 4d 61 73 6b 20 26   (pX->exclMask &
12e6f 20 28 70 2d 3e 65 78 63 6c 4d 61 73 6b 7c 70 2d   (p->exclMask|p-
12e70 3e 73 68 61 72 65 64 4d 61 73 6b 29 29 3d 3d 30  >sharedMask))==0
12e71 20 29 3b 0a 20 20 20 20 20 20 61 6c 6c 4d 61 73   );.      allMas
12e72 6b 20 7c 3d 20 70 58 2d 3e 73 68 61 72 65 64 4d  k |= pX->sharedM
12e73 61 73 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ask;.    }..    
12e74 2f 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20 73 79  /* Unlock the sy
12e75 73 74 65 6d 2d 6c 65 76 65 6c 20 6c 6f 63 6b 73  stem-level locks
12e76 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 6d 61 73   */.    if( (mas
12e77 6b 20 26 20 61 6c 6c 4d 61 73 6b 29 3d 3d 30 20  k & allMask)==0 
12e78 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 69  ){.      rc = wi
12e79 6e 53 68 6d 53 79 73 74 65 6d 4c 6f 63 6b 28 70  nShmSystemLock(p
12e7a 53 68 6d 4e 6f 64 65 2c 20 5f 53 48 4d 5f 55 4e  ShmNode, _SHM_UN
12e7b 4c 43 4b 2c 20 6f 66 73 74 2b 57 49 4e 5f 53 48  LCK, ofst+WIN_SH
12e7c 4d 5f 42 41 53 45 2c 20 6e 29 3b 0a 20 20 20 20  M_BASE, n);.    
12e7d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
12e7e 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
12e7f 20 7d 0a 0a 20 20 20 20 2f 2a 20 55 6e 64 6f 20   }..    /* Undo 
12e80 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 73 20  the local locks 
12e81 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  */.    if( rc==S
12e82 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
12e83 20 20 70 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 3d    p->exclMask &=
12e84 20 7e 6d 61 73 6b 3b 0a 20 20 20 20 20 20 70 2d   ~mask;.      p-
12e85 3e 73 68 61 72 65 64 4d 61 73 6b 20 26 3d 20 7e  >sharedMask &= ~
12e86 6d 61 73 6b 3b 0a 20 20 20 20 7d 20 0a 20 20 7d  mask;.    } .  }
12e87 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26  else if( flags &
12e88 20 53 51 4c 49 54 45 5f 53 48 4d 5f 53 48 41 52   SQLITE_SHM_SHAR
12e89 45 44 20 29 7b 0a 20 20 20 20 75 31 36 20 61 6c  ED ){.    u16 al
12e8a 6c 53 68 61 72 65 64 20 3d 20 30 3b 20 20 2f 2a  lShared = 0;  /*
12e8b 20 55 6e 69 6f 6e 20 6f 66 20 6c 6f 63 6b 73 20   Union of locks 
12e8c 68 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69  held by connecti
12e8d 6f 6e 73 20 6f 74 68 65 72 20 74 68 61 6e 20 22  ons other than "
12e8e 70 22 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69  p" */..    /* Fi
12e8f 6e 64 20 6f 75 74 20 77 68 69 63 68 20 73 68 61  nd out which sha
12e90 72 65 64 20 6c 6f 63 6b 73 20 61 72 65 20 61 6c  red locks are al
12e91 72 65 61 64 79 20 68 65 6c 64 20 62 79 20 73 69  ready held by si
12e92 62 6c 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e  bling connection
12e93 73 2e 0a 20 20 20 20 2a 2a 20 49 66 20 61 6e 79  s..    ** If any
12e94 20 73 69 62 6c 69 6e 67 20 61 6c 72 65 61 64 79   sibling already
12e95 20 68 6f 6c 64 73 20 61 6e 20 65 78 63 6c 75 73   holds an exclus
12e96 69 76 65 20 6c 6f 63 6b 2c 20 67 6f 20 61 68 65  ive lock, go ahe
12e97 61 64 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20  ad and return.  
12e98 20 20 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59    ** SQLITE_BUSY
12e99 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
12e9a 28 70 58 3d 70 53 68 6d 4e 6f 64 65 2d 3e 70 46  (pX=pShmNode->pF
12e9b 69 72 73 74 3b 20 70 58 3b 20 70 58 3d 70 58 2d  irst; pX; pX=pX-
12e9c 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69  >pNext){.      i
12e9d 66 28 20 28 70 58 2d 3e 65 78 63 6c 4d 61 73 6b  f( (pX->exclMask
12e9e 20 26 20 6d 61 73 6b 29 21 3d 30 20 29 7b 0a 20   & mask)!=0 ){. 
12e9f 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
12ea0 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20  TE_BUSY;.       
12ea1 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
12ea2 20 20 20 20 20 20 61 6c 6c 53 68 61 72 65 64 20        allShared 
12ea3 7c 3d 20 70 58 2d 3e 73 68 61 72 65 64 4d 61 73  |= pX->sharedMas
12ea4 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
12ea5 20 47 65 74 20 73 68 61 72 65 64 20 6c 6f 63 6b   Get shared lock
12ea6 73 20 61 74 20 74 68 65 20 73 79 73 74 65 6d 20  s at the system 
12ea7 6c 65 76 65 6c 2c 20 69 66 20 6e 65 63 65 73 73  level, if necess
12ea8 61 72 79 20 2a 2f 0a 20 20 20 20 69 66 28 20 72  ary */.    if( r
12ea9 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
12eaa 20 20 20 20 20 20 69 66 28 20 28 61 6c 6c 53 68        if( (allSh
12eab 61 72 65 64 20 26 20 6d 61 73 6b 29 3d 3d 30 20  ared & mask)==0 
12eac 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
12ead 77 69 6e 53 68 6d 53 79 73 74 65 6d 4c 6f 63 6b  winShmSystemLock
12eae 28 70 53 68 6d 4e 6f 64 65 2c 20 5f 53 48 4d 5f  (pShmNode, _SHM_
12eaf 52 44 4c 43 4b 2c 20 6f 66 73 74 2b 57 49 4e 5f  RDLCK, ofst+WIN_
12eb0 53 48 4d 5f 42 41 53 45 2c 20 6e 29 3b 0a 20 20  SHM_BASE, n);.  
12eb1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
12eb2 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
12eb3 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  K;.      }.    }
12eb4 0a 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65  ..    /* Get the
12eb5 20 6c 6f 63 61 6c 20 73 68 61 72 65 64 20 6c 6f   local shared lo
12eb6 63 6b 73 20 2a 2f 0a 20 20 20 20 69 66 28 20 72  cks */.    if( r
12eb7 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
12eb8 20 20 20 20 20 20 70 2d 3e 73 68 61 72 65 64 4d        p->sharedM
12eb9 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20  ask |= mask;.   
12eba 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
12ebb 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20  /* Make sure no 
12ebc 73 69 62 6c 69 6e 67 20 63 6f 6e 6e 65 63 74 69  sibling connecti
12ebd 6f 6e 73 20 68 6f 6c 64 20 6c 6f 63 6b 73 20 74  ons hold locks t
12ebe 68 61 74 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 74  hat will block t
12ebf 68 69 73 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 2e  his.    ** lock.
12ec0 20 20 49 66 20 61 6e 79 20 64 6f 2c 20 72 65 74    If any do, ret
12ec1 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20  urn SQLITE_BUSY 
12ec2 72 69 67 68 74 20 61 77 61 79 2e 0a 20 20 20 20  right away..    
12ec3 2a 2f 0a 20 20 20 20 66 6f 72 28 70 58 3d 70 53  */.    for(pX=pS
12ec4 68 6d 4e 6f 64 65 2d 3e 70 46 69 72 73 74 3b 20  hmNode->pFirst; 
12ec5 70 58 3b 20 70 58 3d 70 58 2d 3e 70 4e 65 78 74  pX; pX=pX->pNext
12ec6 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 58  ){.      if( (pX
12ec7 2d 3e 65 78 63 6c 4d 61 73 6b 20 26 20 6d 61 73  ->exclMask & mas
12ec8 6b 29 21 3d 30 20 7c 7c 20 28 70 58 2d 3e 73 68  k)!=0 || (pX->sh
12ec9 61 72 65 64 4d 61 73 6b 20 26 20 6d 61 73 6b 29  aredMask & mask)
12eca 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  !=0 ){.        r
12ecb 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
12ecc 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
12ecd 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
12ece 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20  .    /* Get the 
12ecf 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 73 20  exclusive locks 
12ed0 61 74 20 74 68 65 20 73 79 73 74 65 6d 20 6c 65  at the system le
12ed1 76 65 6c 2e 20 20 54 68 65 6e 20 69 66 20 73 75  vel.  Then if su
12ed2 63 63 65 73 73 66 75 6c 0a 20 20 20 20 2a 2a 20  ccessful.    ** 
12ed3 61 6c 73 6f 20 6d 61 72 6b 20 74 68 65 20 6c 6f  also mark the lo
12ed4 63 61 6c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61  cal connection a
12ed5 73 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 0a  s being locked..
12ed6 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72      */.    if( r
12ed7 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
12ed8 20 20 20 20 20 20 72 63 20 3d 20 77 69 6e 53 68        rc = winSh
12ed9 6d 53 79 73 74 65 6d 4c 6f 63 6b 28 70 53 68 6d  mSystemLock(pShm
12eda 4e 6f 64 65 2c 20 5f 53 48 4d 5f 57 52 4c 43 4b  Node, _SHM_WRLCK
12edb 2c 20 6f 66 73 74 2b 57 49 4e 5f 53 48 4d 5f 42  , ofst+WIN_SHM_B
12edc 41 53 45 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69  ASE, n);.      i
12edd 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
12ede 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
12edf 72 74 28 20 28 70 2d 3e 73 68 61 72 65 64 4d 61  rt( (p->sharedMa
12ee0 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 3b  sk & mask)==0 );
12ee1 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 78 63 6c  .        p->excl
12ee2 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20  Mask |= mask;.  
12ee3 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
12ee4 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
12ee5 6c 65 61 76 65 28 70 53 68 6d 4e 6f 64 65 2d 3e  leave(pShmNode->
12ee6 6d 75 74 65 78 29 3b 0a 20 20 4f 53 54 52 41 43  mutex);.  OSTRAC
12ee7 45 28 28 22 53 48 4d 2d 4c 4f 43 4b 20 73 68 6d  E(("SHM-LOCK shm
12ee8 69 64 2d 25 64 2c 20 70 69 64 2d 25 64 20 67 6f  id-%d, pid-%d go
12ee9 74 20 25 30 33 78 2c 25 30 33 78 20 25 73 5c 6e  t %03x,%03x %s\n
12eea 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d  ",.           p-
12eeb 3e 69 64 2c 20 28 69 6e 74 29 6f 73 47 65 74 43  >id, (int)osGetC
12eec 75 72 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28  urrentProcessId(
12eed 29 2c 20 70 2d 3e 73 68 61 72 65 64 4d 61 73 6b  ), p->sharedMask
12eee 2c 20 70 2d 3e 65 78 63 6c 4d 61 73 6b 2c 0a 20  , p->exclMask,. 
12eef 20 20 20 20 20 20 20 20 20 20 72 63 20 3f 20 22            rc ? "
12ef0 66 61 69 6c 65 64 22 20 3a 20 22 6f 6b 22 29 29  failed" : "ok"))
12ef1 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
12ef2 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
12ef3 74 20 61 20 6d 65 6d 6f 72 79 20 62 61 72 72 69  t a memory barri
12ef4 65 72 20 6f 72 20 6d 65 6d 6f 72 79 20 66 65 6e  er or memory fen
12ef5 63 65 20 6f 6e 20 73 68 61 72 65 64 20 6d 65 6d  ce on shared mem
12ef6 6f 72 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 6c 6c  ory.  .**.** All
12ef7 20 6c 6f 61 64 73 20 61 6e 64 20 73 74 6f 72 65   loads and store
12ef8 73 20 62 65 67 75 6e 20 62 65 66 6f 72 65 20 74  s begun before t
12ef9 68 65 20 62 61 72 72 69 65 72 20 6d 75 73 74 20  he barrier must 
12efa 63 6f 6d 70 6c 65 74 65 20 62 65 66 6f 72 65 0a  complete before.
12efb 2a 2a 20 61 6e 79 20 6c 6f 61 64 20 6f 72 20 73  ** any load or s
12efc 74 6f 72 65 20 62 65 67 75 6e 20 61 66 74 65 72  tore begun after
12efd 20 74 68 65 20 62 61 72 72 69 65 72 2e 0a 2a 2f   the barrier..*/
12efe 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e  .static void win
12eff 53 68 6d 42 61 72 72 69 65 72 28 0a 20 20 73 71  ShmBarrier(.  sq
12f00 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 20 20  lite3_file *fd  
12f01 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
12f02 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  ase holding the 
12f03 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 2a 2f  shared memory */
12f04 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  .){.  UNUSED_PAR
12f05 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20 2f 2a  AMETER(fd);.  /*
12f06 20 4d 65 6d 6f 72 79 42 61 72 72 69 65 72 28 29   MemoryBarrier()
12f07 3b 20 2f 2f 20 64 6f 65 73 20 6e 6f 74 20 77 6f  ; // does not wo
12f08 72 6b 20 2d 2d 20 64 6f 20 6e 6f 74 20 6b 6e 6f  rk -- do not kno
12f09 77 20 77 68 79 20 6e 6f 74 20 2a 2f 0a 20 20 77  w why not */.  w
12f0a 69 6e 53 68 6d 45 6e 74 65 72 4d 75 74 65 78 28  inShmEnterMutex(
12f0b 29 3b 0a 20 20 77 69 6e 53 68 6d 4c 65 61 76 65  );.  winShmLeave
12f0c 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Mutex();.}../*.*
12f0d 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
12f0e 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 6f 62 74  is called to obt
12f0f 61 69 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ain a pointer to
12f10 20 72 65 67 69 6f 6e 20 69 52 65 67 69 6f 6e 20   region iRegion 
12f11 6f 66 20 74 68 65 20 0a 2a 2a 20 73 68 61 72 65  of the .** share
12f12 64 2d 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61  d-memory associa
12f13 74 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74  ted with the dat
12f14 61 62 61 73 65 20 66 69 6c 65 20 66 64 2e 20 53  abase file fd. S
12f15 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67  hared-memory reg
12f16 69 6f 6e 73 20 0a 2a 2a 20 61 72 65 20 6e 75 6d  ions .** are num
12f17 62 65 72 65 64 20 73 74 61 72 74 69 6e 67 20 66  bered starting f
12f18 72 6f 6d 20 7a 65 72 6f 2e 20 45 61 63 68 20 73  rom zero. Each s
12f19 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67  hared-memory reg
12f1a 69 6f 6e 20 69 73 20 73 7a 52 65 67 69 6f 6e 20  ion is szRegion 
12f1b 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a  .** bytes in siz
12f1c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65  e..**.** If an e
12f1d 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20  rror occurs, an 
12f1e 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
12f1f 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 20 69  turned and *pp i
12f20 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a  s set to NULL..*
12f21 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  *.** Otherwise, 
12f22 69 66 20 74 68 65 20 69 73 57 72 69 74 65 20 70  if the isWrite p
12f23 61 72 61 6d 65 74 65 72 20 69 73 20 30 20 61 6e  arameter is 0 an
12f24 64 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  d the requested 
12f25 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 0a 2a 2a  shared-memory.**
12f26 20 72 65 67 69 6f 6e 20 68 61 73 20 6e 6f 74 20   region has not 
12f27 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 28  been allocated (
12f28 62 79 20 61 6e 79 20 63 6c 69 65 6e 74 2c 20 69  by any client, i
12f29 6e 63 6c 75 64 69 6e 67 20 6f 6e 65 20 72 75 6e  ncluding one run
12f2a 6e 69 6e 67 20 69 6e 20 61 0a 2a 2a 20 73 65 70  ning in a.** sep
12f2b 61 72 61 74 65 20 70 72 6f 63 65 73 73 29 2c 20  arate process), 
12f2c 74 68 65 6e 20 2a 70 70 20 69 73 20 73 65 74 20  then *pp is set 
12f2d 74 6f 20 4e 55 4c 4c 20 61 6e 64 20 53 51 4c 49  to NULL and SQLI
12f2e 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20  TE_OK returned. 
12f2f 49 66 20 0a 2a 2a 20 69 73 57 72 69 74 65 20 69  If .** isWrite i
12f30 73 20 6e 6f 6e 2d 7a 65 72 6f 20 61 6e 64 20 74  s non-zero and t
12f31 68 65 20 72 65 71 75 65 73 74 65 64 20 73 68 61  he requested sha
12f32 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67 69 6f  red-memory regio
12f33 6e 20 68 61 73 20 6e 6f 74 20 79 65 74 20 0a 2a  n has not yet .*
12f34 2a 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  * been allocated
12f35 2c 20 69 74 20 69 73 20 61 6c 6c 6f 63 61 74 65  , it is allocate
12f36 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
12f37 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  on..**.** If the
12f38 20 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 72   shared-memory r
12f39 65 67 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 64  egion has alread
12f3a 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  y been allocated
12f3b 20 6f 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64   or is allocated
12f3c 20 62 79 0a 2a 2a 20 74 68 69 73 20 63 61 6c 6c   by.** this call
12f3d 20 61 73 20 64 65 73 63 72 69 62 65 64 20 61 62   as described ab
12f3e 6f 76 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20  ove, then it is 
12f3f 6d 61 70 70 65 64 20 69 6e 74 6f 20 74 68 69 73  mapped into this
12f40 20 70 72 6f 63 65 73 73 65 73 20 0a 2a 2a 20 61   processes .** a
12f41 64 64 72 65 73 73 20 73 70 61 63 65 20 28 69 66  ddress space (if
12f42 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61   it is not alrea
12f43 64 79 29 2c 20 2a 70 70 20 69 73 20 73 65 74 20  dy), *pp is set 
12f44 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
12f45 6d 61 70 70 65 64 20 0a 2a 2a 20 6d 65 6d 6f 72  mapped .** memor
12f46 79 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  y and SQLITE_OK 
12f47 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61  returned..*/.sta
12f48 74 69 63 20 69 6e 74 20 77 69 6e 53 68 6d 4d 61  tic int winShmMa
12f49 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  p(.  sqlite3_fil
12f4a 65 20 2a 66 64 2c 20 20 20 20 20 20 20 20 20 20  e *fd,          
12f4b 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 6f       /* Handle o
12f4c 70 65 6e 20 6f 6e 20 64 61 74 61 62 61 73 65 20  pen on database 
12f4d 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52  file */.  int iR
12f4e 65 67 69 6f 6e 2c 20 20 20 20 20 20 20 20 20 20  egion,          
12f4f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
12f50 69 6f 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20  ion to retrieve 
12f51 2a 2f 0a 20 20 69 6e 74 20 73 7a 52 65 67 69 6f  */.  int szRegio
12f52 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
12f53 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
12f54 72 65 67 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  regions */.  int
12f55 20 69 73 57 72 69 74 65 2c 20 20 20 20 20 20 20   isWrite,       
12f56 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12f57 54 72 75 65 20 74 6f 20 65 78 74 65 6e 64 20 66  True to extend f
12f58 69 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72 79  ile if necessary
12f59 20 2a 2f 0a 20 20 76 6f 69 64 20 76 6f 6c 61 74   */.  void volat
12f5a 69 6c 65 20 2a 2a 70 70 20 20 20 20 20 20 20 20  ile **pp        
12f5b 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61        /* OUT: Ma
12f5c 70 70 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 29  pped memory */.)
12f5d 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 44 62  {.  winFile *pDb
12f5e 46 64 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 66  Fd = (winFile*)f
12f5f 64 3b 0a 20 20 77 69 6e 53 68 6d 20 2a 70 20 3d  d;.  winShm *p =
12f60 20 70 44 62 46 64 2d 3e 70 53 68 6d 3b 0a 20 20   pDbFd->pShm;.  
12f61 77 69 6e 53 68 6d 4e 6f 64 65 20 2a 70 53 68 6d  winShmNode *pShm
12f62 4e 6f 64 65 3b 0a 20 20 69 6e 74 20 72 63 20 3d  Node;.  int rc =
12f63 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69   SQLITE_OK;..  i
12f64 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 63 20  f( !p ){.    rc 
12f65 3d 20 77 69 6e 4f 70 65 6e 53 68 61 72 65 64 4d  = winOpenSharedM
12f66 65 6d 6f 72 79 28 70 44 62 46 64 29 3b 0a 20 20  emory(pDbFd);.  
12f67 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12f68 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
12f69 0a 20 20 20 20 70 20 3d 20 70 44 62 46 64 2d 3e  .    p = pDbFd->
12f6a 70 53 68 6d 3b 0a 20 20 7d 0a 20 20 70 53 68 6d  pShm;.  }.  pShm
12f6b 4e 6f 64 65 20 3d 20 70 2d 3e 70 53 68 6d 4e 6f  Node = p->pShmNo
12f6c 64 65 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d  de;..  sqlite3_m
12f6d 75 74 65 78 5f 65 6e 74 65 72 28 70 53 68 6d 4e  utex_enter(pShmN
12f6e 6f 64 65 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61  ode->mutex);.  a
12f6f 73 73 65 72 74 28 20 73 7a 52 65 67 69 6f 6e 3d  ssert( szRegion=
12f70 3d 70 53 68 6d 4e 6f 64 65 2d 3e 73 7a 52 65 67  =pShmNode->szReg
12f71 69 6f 6e 20 7c 7c 20 70 53 68 6d 4e 6f 64 65 2d  ion || pShmNode-
12f72 3e 6e 52 65 67 69 6f 6e 3d 3d 30 20 29 3b 0a 0a  >nRegion==0 );..
12f73 20 20 69 66 28 20 70 53 68 6d 4e 6f 64 65 2d 3e    if( pShmNode->
12f74 6e 52 65 67 69 6f 6e 3c 3d 69 52 65 67 69 6f 6e  nRegion<=iRegion
12f75 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53   ){.    struct S
12f76 68 6d 52 65 67 69 6f 6e 20 2a 61 70 4e 65 77 3b  hmRegion *apNew;
12f77 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
12f78 77 20 61 52 65 67 69 6f 6e 5b 5d 20 61 72 72 61  w aRegion[] arra
12f79 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 42 79  y */.    int nBy
12f7a 74 65 20 3d 20 28 69 52 65 67 69 6f 6e 2b 31 29  te = (iRegion+1)
12f7b 2a 73 7a 52 65 67 69 6f 6e 3b 20 20 2f 2a 20 4d  *szRegion;  /* M
12f7c 69 6e 69 6d 75 6d 20 72 65 71 75 69 72 65 64 20  inimum required 
12f7d 66 69 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20  file size */.   
12f7e 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
12f7f 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  z;              
12f80 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73      /* Current s
12f81 69 7a 65 20 6f 66 20 77 61 6c 2d 69 6e 64 65 78  ize of wal-index
12f82 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 20 20 70 53   file */..    pS
12f83 68 6d 4e 6f 64 65 2d 3e 73 7a 52 65 67 69 6f 6e  hmNode->szRegion
12f84 20 3d 20 73 7a 52 65 67 69 6f 6e 3b 0a 0a 20 20   = szRegion;..  
12f85 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74    /* The request
12f86 65 64 20 72 65 67 69 6f 6e 20 69 73 20 6e 6f 74  ed region is not
12f87 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 74 68 69   mapped into thi
12f88 73 20 70 72 6f 63 65 73 73 65 73 20 61 64 64 72  s processes addr
12f89 65 73 73 20 73 70 61 63 65 2e 0a 20 20 20 20 2a  ess space..    *
12f8a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
12f8b 66 20 69 74 20 68 61 73 20 62 65 65 6e 20 61 6c  f it has been al
12f8c 6c 6f 63 61 74 65 64 20 28 69 2e 65 2e 20 69 66  located (i.e. if
12f8d 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 66   the wal-index f
12f8e 69 6c 65 20 69 73 0a 20 20 20 20 2a 2a 20 6c 61  ile is.    ** la
12f8f 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 63 6f  rge enough to co
12f90 6e 74 61 69 6e 20 74 68 65 20 72 65 71 75 65 73  ntain the reques
12f91 74 65 64 20 72 65 67 69 6f 6e 29 2e 0a 20 20 20  ted region)..   
12f92 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 77 69 6e   */.    rc = win
12f93 46 69 6c 65 53 69 7a 65 28 28 73 71 6c 69 74 65  FileSize((sqlite
12f94 33 5f 66 69 6c 65 20 2a 29 26 70 53 68 6d 4e 6f  3_file *)&pShmNo
12f95 64 65 2d 3e 68 46 69 6c 65 2c 20 26 73 7a 29 3b  de->hFile, &sz);
12f96 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
12f97 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
12f98 72 63 20 3d 20 77 69 6e 4c 6f 67 45 72 72 6f 72  rc = winLogError
12f99 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48  (SQLITE_IOERR_SH
12f9a 4d 53 49 5a 45 2c 20 6f 73 47 65 74 4c 61 73 74  MSIZE, osGetLast
12f9b 45 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20  Error(),.       
12f9c 20 20 20 20 20 20 20 20 22 77 69 6e 53 68 6d 4d          "winShmM
12f9d 61 70 31 22 2c 20 70 44 62 46 64 2d 3e 7a 50 61  ap1", pDbFd->zPa
12f9e 74 68 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  th);.      goto 
12f9f 73 68 6d 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20  shmpage_out;.   
12fa0 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 7a 3c 6e   }..    if( sz<n
12fa1 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a  Byte ){.      /*
12fa2 20 54 68 65 20 72 65 71 75 65 73 74 65 64 20 6d   The requested m
12fa3 65 6d 6f 72 79 20 72 65 67 69 6f 6e 20 64 6f 65  emory region doe
12fa4 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 49 66 20  s not exist. If 
12fa5 69 73 57 72 69 74 65 20 69 73 20 73 65 74 20 74  isWrite is set t
12fa6 6f 0a 20 20 20 20 20 20 2a 2a 20 7a 65 72 6f 2c  o.      ** zero,
12fa7 20 65 78 69 74 20 65 61 72 6c 79 2e 20 2a 70 70   exit early. *pp
12fa8 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20   will be set to 
12fa9 4e 55 4c 4c 20 61 6e 64 20 53 51 4c 49 54 45 5f  NULL and SQLITE_
12faa 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 20 20 20  OK returned..   
12fab 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41     **.      ** A
12fac 6c 74 65 72 6e 61 74 69 76 65 6c 79 2c 20 69 66  lternatively, if
12fad 20 69 73 57 72 69 74 65 20 69 73 20 6e 6f 6e 2d   isWrite is non-
12fae 7a 65 72 6f 2c 20 75 73 65 20 66 74 72 75 6e 63  zero, use ftrunc
12faf 61 74 65 28 29 20 74 6f 20 61 6c 6c 6f 63 61 74  ate() to allocat
12fb0 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72  e.      ** the r
12fb1 65 71 75 65 73 74 65 64 20 6d 65 6d 6f 72 79 20  equested memory 
12fb2 72 65 67 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f  region..      */
12fb3 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 57 72  .      if( !isWr
12fb4 69 74 65 20 29 20 67 6f 74 6f 20 73 68 6d 70 61  ite ) goto shmpa
12fb5 67 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 72 63  ge_out;.      rc
12fb6 20 3d 20 77 69 6e 54 72 75 6e 63 61 74 65 28 28   = winTruncate((
12fb7 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26  sqlite3_file *)&
12fb8 70 53 68 6d 4e 6f 64 65 2d 3e 68 46 69 6c 65 2c  pShmNode->hFile,
12fb9 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 69   nByte);.      i
12fba 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
12fbb 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
12fbc 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c   winLogError(SQL
12fbd 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d 53 49 5a  ITE_IOERR_SHMSIZ
12fbe 45 2c 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f  E, osGetLastErro
12fbf 72 28 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  r(),.           
12fc0 20 20 20 20 20 20 22 77 69 6e 53 68 6d 4d 61 70        "winShmMap
12fc1 32 22 2c 20 70 44 62 46 64 2d 3e 7a 50 61 74 68  2", pDbFd->zPath
12fc2 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  );.        goto 
12fc3 73 68 6d 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20  shmpage_out;.   
12fc4 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
12fc5 2f 2a 20 4d 61 70 20 74 68 65 20 72 65 71 75 65  /* Map the reque
12fc6 73 74 65 64 20 6d 65 6d 6f 72 79 20 72 65 67 69  sted memory regi
12fc7 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 70 72 6f  on into this pro
12fc8 63 65 73 73 65 73 20 61 64 64 72 65 73 73 20 73  cesses address s
12fc9 70 61 63 65 2e 20 2a 2f 0a 20 20 20 20 61 70 4e  pace. */.    apN
12fca 65 77 20 3d 20 28 73 74 72 75 63 74 20 53 68 6d  ew = (struct Shm
12fcb 52 65 67 69 6f 6e 20 2a 29 73 71 6c 69 74 65 33  Region *)sqlite3
12fcc 5f 72 65 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20  _realloc(.      
12fcd 20 20 70 53 68 6d 4e 6f 64 65 2d 3e 61 52 65 67    pShmNode->aReg
12fce 69 6f 6e 2c 20 28 69 52 65 67 69 6f 6e 2b 31 29  ion, (iRegion+1)
12fcf 2a 73 69 7a 65 6f 66 28 61 70 4e 65 77 5b 30 5d  *sizeof(apNew[0]
12fd0 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28  ).    );.    if(
12fd1 20 21 61 70 4e 65 77 20 29 7b 0a 20 20 20 20 20   !apNew ){.     
12fd2 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45   rc = SQLITE_IOE
12fd3 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  RR_NOMEM;.      
12fd4 67 6f 74 6f 20 73 68 6d 70 61 67 65 5f 6f 75 74  goto shmpage_out
12fd5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 68 6d  ;.    }.    pShm
12fd6 4e 6f 64 65 2d 3e 61 52 65 67 69 6f 6e 20 3d 20  Node->aRegion = 
12fd7 61 70 4e 65 77 3b 0a 0a 20 20 20 20 77 68 69 6c  apNew;..    whil
12fd8 65 28 20 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65  e( pShmNode->nRe
12fd9 67 69 6f 6e 3c 3d 69 52 65 67 69 6f 6e 20 29 7b  gion<=iRegion ){
12fda 0a 20 20 20 20 20 20 48 41 4e 44 4c 45 20 68 4d  .      HANDLE hM
12fdb 61 70 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20  ap = NULL;      
12fdc 20 20 20 2f 2a 20 66 69 6c 65 2d 6d 61 70 70 69     /* file-mappi
12fdd 6e 67 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 20  ng handle */.   
12fde 20 20 20 76 6f 69 64 20 2a 70 4d 61 70 20 3d 20     void *pMap = 
12fdf 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0;             /
12fe0 2a 20 4d 61 70 70 65 64 20 6d 65 6d 6f 72 79 20  * Mapped memory 
12fe1 72 65 67 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 0a  region */.     .
12fe2 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
12fe3 4e 52 54 0a 20 20 20 20 20 20 68 4d 61 70 20 3d  NRT.      hMap =
12fe4 20 6f 73 43 72 65 61 74 65 46 69 6c 65 4d 61 70   osCreateFileMap
12fe5 70 69 6e 67 46 72 6f 6d 41 70 70 28 70 53 68 6d  pingFromApp(pShm
12fe6 4e 6f 64 65 2d 3e 68 46 69 6c 65 2e 68 2c 0a 20  Node->hFile.h,. 
12fe7 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 20 50           NULL, P
12fe8 41 47 45 5f 52 45 41 44 57 52 49 54 45 2c 20 6e  AGE_READWRITE, n
12fe9 42 79 74 65 2c 20 4e 55 4c 4c 0a 20 20 20 20 20  Byte, NULL.     
12fea 20 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65   );.#elif define
12feb 64 28 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48  d(SQLITE_WIN32_H
12fec 41 53 5f 57 49 44 45 29 0a 20 20 20 20 20 20 68  AS_WIDE).      h
12fed 4d 61 70 20 3d 20 6f 73 43 72 65 61 74 65 46 69  Map = osCreateFi
12fee 6c 65 4d 61 70 70 69 6e 67 57 28 70 53 68 6d 4e  leMappingW(pShmN
12fef 6f 64 65 2d 3e 68 46 69 6c 65 2e 68 2c 20 0a 20  ode->hFile.h, . 
12ff0 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 20 50           NULL, P
12ff1 41 47 45 5f 52 45 41 44 57 52 49 54 45 2c 20 30  AGE_READWRITE, 0
12ff2 2c 20 6e 42 79 74 65 2c 20 4e 55 4c 4c 0a 20 20  , nByte, NULL.  
12ff3 20 20 20 20 29 3b 0a 23 65 6c 69 66 20 64 65 66      );.#elif def
12ff4 69 6e 65 64 28 53 51 4c 49 54 45 5f 57 49 4e 33  ined(SQLITE_WIN3
12ff5 32 5f 48 41 53 5f 41 4e 53 49 29 0a 20 20 20 20  2_HAS_ANSI).    
12ff6 20 20 68 4d 61 70 20 3d 20 6f 73 43 72 65 61 74    hMap = osCreat
12ff7 65 46 69 6c 65 4d 61 70 70 69 6e 67 41 28 70 53  eFileMappingA(pS
12ff8 68 6d 4e 6f 64 65 2d 3e 68 46 69 6c 65 2e 68 2c  hmNode->hFile.h,
12ff9 20 0a 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c   .          NULL
12ffa 2c 20 50 41 47 45 5f 52 45 41 44 57 52 49 54 45  , PAGE_READWRITE
12ffb 2c 20 30 2c 20 6e 42 79 74 65 2c 20 4e 55 4c 4c  , 0, nByte, NULL
12ffc 0a 20 20 20 20 20 20 29 3b 0a 23 65 6e 64 69 66  .      );.#endif
12ffd 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 28 28  .      OSTRACE((
12ffe 22 53 48 4d 2d 4d 41 50 20 70 69 64 2d 25 64 20  "SHM-MAP pid-%d 
12fff 63 72 65 61 74 65 20 72 65 67 69 6f 6e 3d 25 64  create region=%d
13000 20 6e 62 79 74 65 3d 25 64 20 25 73 5c 6e 22 2c   nbyte=%d %s\n",
13001 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
13002 28 69 6e 74 29 6f 73 47 65 74 43 75 72 72 65 6e  (int)osGetCurren
13003 74 50 72 6f 63 65 73 73 49 64 28 29 2c 20 70 53  tProcessId(), pS
13004 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f 6e 2c  hmNode->nRegion,
13005 20 6e 42 79 74 65 2c 0a 20 20 20 20 20 20 20 20   nByte,.        
13006 20 20 20 20 20 20 20 68 4d 61 70 20 3f 20 22 6f         hMap ? "o
13007 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b  k" : "failed"));
13008 0a 20 20 20 20 20 20 69 66 28 20 68 4d 61 70 20  .      if( hMap 
13009 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
1300a 4f 66 66 73 65 74 20 3d 20 70 53 68 6d 4e 6f 64  Offset = pShmNod
1300b 65 2d 3e 6e 52 65 67 69 6f 6e 2a 73 7a 52 65 67  e->nRegion*szReg
1300c 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  ion;.        int
1300d 20 69 4f 66 66 73 65 74 53 68 69 66 74 20 3d 20   iOffsetShift = 
1300e 69 4f 66 66 73 65 74 20 25 20 77 69 6e 53 79 73  iOffset % winSys
1300f 49 6e 66 6f 2e 64 77 41 6c 6c 6f 63 61 74 69 6f  Info.dwAllocatio
13010 6e 47 72 61 6e 75 6c 61 72 69 74 79 3b 0a 23 69  nGranularity;.#i
13011 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52  f SQLITE_OS_WINR
13012 54 0a 20 20 20 20 20 20 20 20 70 4d 61 70 20 3d  T.        pMap =
13013 20 6f 73 4d 61 70 56 69 65 77 4f 66 46 69 6c 65   osMapViewOfFile
13014 46 72 6f 6d 41 70 70 28 68 4d 61 70 2c 20 46 49  FromApp(hMap, FI
13015 4c 45 5f 4d 41 50 5f 57 52 49 54 45 20 7c 20 46  LE_MAP_WRITE | F
13016 49 4c 45 5f 4d 41 50 5f 52 45 41 44 2c 0a 20 20  ILE_MAP_READ,.  
13017 20 20 20 20 20 20 20 20 20 20 69 4f 66 66 73 65            iOffse
13018 74 20 2d 20 69 4f 66 66 73 65 74 53 68 69 66 74  t - iOffsetShift
13019 2c 20 73 7a 52 65 67 69 6f 6e 20 2b 20 69 4f 66  , szRegion + iOf
1301a 66 73 65 74 53 68 69 66 74 0a 20 20 20 20 20 20  fsetShift.      
1301b 20 20 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20    );.#else.     
1301c 20 20 20 70 4d 61 70 20 3d 20 6f 73 4d 61 70 56     pMap = osMapV
1301d 69 65 77 4f 66 46 69 6c 65 28 68 4d 61 70 2c 20  iewOfFile(hMap, 
1301e 46 49 4c 45 5f 4d 41 50 5f 57 52 49 54 45 20 7c  FILE_MAP_WRITE |
1301f 20 46 49 4c 45 5f 4d 41 50 5f 52 45 41 44 2c 0a   FILE_MAP_READ,.
13020 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 69              0, i
13021 4f 66 66 73 65 74 20 2d 20 69 4f 66 66 73 65 74  Offset - iOffset
13022 53 68 69 66 74 2c 20 73 7a 52 65 67 69 6f 6e 20  Shift, szRegion 
13023 2b 20 69 4f 66 66 73 65 74 53 68 69 66 74 0a 20  + iOffsetShift. 
13024 20 20 20 20 20 20 20 29 3b 0a 23 65 6e 64 69 66         );.#endif
13025 0a 20 20 20 20 20 20 20 20 4f 53 54 52 41 43 45  .        OSTRACE
13026 28 28 22 53 48 4d 2d 4d 41 50 20 70 69 64 2d 25  (("SHM-MAP pid-%
13027 64 20 6d 61 70 20 72 65 67 69 6f 6e 3d 25 64 20  d map region=%d 
13028 6f 66 66 73 65 74 3d 25 64 20 73 69 7a 65 3d 25  offset=%d size=%
13029 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20  d %s\n",.       
1302a 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29 6f            (int)o
1302b 73 47 65 74 43 75 72 72 65 6e 74 50 72 6f 63 65  sGetCurrentProce
1302c 73 73 49 64 28 29 2c 20 70 53 68 6d 4e 6f 64 65  ssId(), pShmNode
1302d 2d 3e 6e 52 65 67 69 6f 6e 2c 20 69 4f 66 66 73  ->nRegion, iOffs
1302e 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  et,.            
1302f 20 20 20 20 20 73 7a 52 65 67 69 6f 6e 2c 20 70       szRegion, p
13030 4d 61 70 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61  Map ? "ok" : "fa
13031 69 6c 65 64 22 29 29 3b 0a 20 20 20 20 20 20 7d  iled"));.      }
13032 0a 20 20 20 20 20 20 69 66 28 20 21 70 4d 61 70  .      if( !pMap
13033 20 29 7b 0a 20 20 20 20 20 20 20 20 70 53 68 6d   ){.        pShm
13034 4e 6f 64 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  Node->lastErrno 
13035 3d 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72  = osGetLastError
13036 28 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ();.        rc =
13037 20 77 69 6e 4c 6f 67 45 72 72 6f 72 28 53 51 4c   winLogError(SQL
13038 49 54 45 5f 49 4f 45 52 52 5f 53 48 4d 4d 41 50  ITE_IOERR_SHMMAP
13039 2c 20 70 53 68 6d 4e 6f 64 65 2d 3e 6c 61 73 74  , pShmNode->last
1303a 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20  Errno,.         
1303b 20 20 20 20 20 20 20 20 22 77 69 6e 53 68 6d 4d          "winShmM
1303c 61 70 33 22 2c 20 70 44 62 46 64 2d 3e 7a 50 61  ap3", pDbFd->zPa
1303d 74 68 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  th);.        if(
1303e 20 68 4d 61 70 20 29 20 6f 73 43 6c 6f 73 65 48   hMap ) osCloseH
1303f 61 6e 64 6c 65 28 68 4d 61 70 29 3b 0a 20 20 20  andle(hMap);.   
13040 20 20 20 20 20 67 6f 74 6f 20 73 68 6d 70 61 67       goto shmpag
13041 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 0a  e_out;.      }..
13042 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e        pShmNode->
13043 61 52 65 67 69 6f 6e 5b 70 53 68 6d 4e 6f 64 65  aRegion[pShmNode
13044 2d 3e 6e 52 65 67 69 6f 6e 5d 2e 70 4d 61 70 20  ->nRegion].pMap 
13045 3d 20 70 4d 61 70 3b 0a 20 20 20 20 20 20 70 53  = pMap;.      pS
13046 68 6d 4e 6f 64 65 2d 3e 61 52 65 67 69 6f 6e 5b  hmNode->aRegion[
13047 70 53 68 6d 4e 6f 64 65 2d 3e 6e 52 65 67 69 6f  pShmNode->nRegio
13048 6e 5d 2e 68 4d 61 70 20 3d 20 68 4d 61 70 3b 0a  n].hMap = hMap;.
13049 20 20 20 20 20 20 70 53 68 6d 4e 6f 64 65 2d 3e        pShmNode->
1304a 6e 52 65 67 69 6f 6e 2b 2b 3b 0a 20 20 20 20 7d  nRegion++;.    }
1304b 0a 20 20 7d 0a 0a 73 68 6d 70 61 67 65 5f 6f 75  .  }..shmpage_ou
1304c 74 3a 0a 20 20 69 66 28 20 70 53 68 6d 4e 6f 64  t:.  if( pShmNod
1304d 65 2d 3e 6e 52 65 67 69 6f 6e 3e 69 52 65 67 69  e->nRegion>iRegi
1304e 6f 6e 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 4f  on ){.    int iO
1304f 66 66 73 65 74 20 3d 20 69 52 65 67 69 6f 6e 2a  ffset = iRegion*
13050 73 7a 52 65 67 69 6f 6e 3b 0a 20 20 20 20 69 6e  szRegion;.    in
13051 74 20 69 4f 66 66 73 65 74 53 68 69 66 74 20 3d  t iOffsetShift =
13052 20 69 4f 66 66 73 65 74 20 25 20 77 69 6e 53 79   iOffset % winSy
13053 73 49 6e 66 6f 2e 64 77 41 6c 6c 6f 63 61 74 69  sInfo.dwAllocati
13054 6f 6e 47 72 61 6e 75 6c 61 72 69 74 79 3b 0a 20  onGranularity;. 
13055 20 20 20 63 68 61 72 20 2a 70 20 3d 20 28 63 68     char *p = (ch
13056 61 72 20 2a 29 70 53 68 6d 4e 6f 64 65 2d 3e 61  ar *)pShmNode->a
13057 52 65 67 69 6f 6e 5b 69 52 65 67 69 6f 6e 5d 2e  Region[iRegion].
13058 70 4d 61 70 3b 0a 20 20 20 20 2a 70 70 20 3d 20  pMap;.    *pp = 
13059 28 76 6f 69 64 20 2a 29 26 70 5b 69 4f 66 66 73  (void *)&p[iOffs
1305a 65 74 53 68 69 66 74 5d 3b 0a 20 20 7d 65 6c 73  etShift];.  }els
1305b 65 7b 0a 20 20 20 20 2a 70 70 20 3d 20 30 3b 0a  e{.    *pp = 0;.
1305c 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
1305d 74 65 78 5f 6c 65 61 76 65 28 70 53 68 6d 4e 6f  tex_leave(pShmNo
1305e 64 65 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  de->mutex);.  re
1305f 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6c 73  turn rc;.}..#els
13060 65 0a 23 20 64 65 66 69 6e 65 20 77 69 6e 53 68  e.# define winSh
13061 6d 4d 61 70 20 20 20 20 20 30 0a 23 20 64 65 66  mMap     0.# def
13062 69 6e 65 20 77 69 6e 53 68 6d 4c 6f 63 6b 20 20  ine winShmLock  
13063 20 20 30 0a 23 20 64 65 66 69 6e 65 20 77 69 6e    0.# define win
13064 53 68 6d 42 61 72 72 69 65 72 20 30 0a 23 20 64  ShmBarrier 0.# d
13065 65 66 69 6e 65 20 77 69 6e 53 68 6d 55 6e 6d 61  efine winShmUnma
13066 70 20 20 20 30 0a 23 65 6e 64 69 66 20 2f 2a 20  p   0.#endif /* 
13067 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
13068 4d 49 54 5f 57 41 4c 20 2a 2f 0a 0a 2f 2a 0a 2a  MIT_WAL */../*.*
13069 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20  * Here ends the 
1306a 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
1306b 66 20 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 69  f all sqlite3_fi
1306c 6c 65 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a  le methods..**.*
1306d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1306e 2a 2a 2a 2a 2a 20 45 6e 64 20 73 71 6c 69 74 65  ***** End sqlite
1306f 33 5f 66 69 6c 65 20 4d 65 74 68 6f 64 73 20 2a  3_file Methods *
13070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13071 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
13072 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13073 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13074 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13075 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13076 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
13077 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 6f  /*.** This vecto
13078 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 68  r defines all th
13079 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 63  e methods that c
1307a 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e  an operate on an
1307b 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  .** sqlite3_file
1307c 20 66 6f 72 20 77 69 6e 33 32 2e 0a 2a 2f 0a 73   for win32..*/.s
1307d 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
1307e 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 77  te3_io_methods w
1307f 69 6e 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20  inIoMethod = {. 
13080 20 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   2,             
13081 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13082 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a   /* iVersion */.
13083 20 20 77 69 6e 43 6c 6f 73 65 2c 20 20 20 20 20    winClose,     
13084 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13085 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20    /* xClose */. 
13086 20 77 69 6e 52 65 61 64 2c 20 20 20 20 20 20 20   winRead,       
13087 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13088 20 2f 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 77   /* xRead */.  w
13089 69 6e 57 72 69 74 65 2c 20 20 20 20 20 20 20 20  inWrite,        
1308a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1308b 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 20 77 69  * xWrite */.  wi
1308c 6e 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20  nTruncate,      
1308d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1308e 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a 20 20   xTruncate */.  
1308f 77 69 6e 53 79 6e 63 2c 20 20 20 20 20 20 20 20  winSync,        
13090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13091 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20 77 69  /* xSync */.  wi
13092 6e 46 69 6c 65 53 69 7a 65 2c 20 20 20 20 20 20  nFileSize,      
13093 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13094 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20   xFileSize */.  
13095 77 69 6e 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20  winLock,        
13096 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13097 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 77 69  /* xLock */.  wi
13098 6e 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20  nUnlock,        
13099 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1309a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 77 69   xUnlock */.  wi
1309b 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  nCheckReservedLo
1309c 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ck,           /*
1309d 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
1309e 6f 63 6b 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65  ock */.  winFile
1309f 43 6f 6e 74 72 6f 6c 2c 20 20 20 20 20 20 20 20  Control,        
130a0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c           /* xFil
130a1 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 77 69  eControl */.  wi
130a2 6e 53 65 63 74 6f 72 53 69 7a 65 2c 20 20 20 20  nSectorSize,    
130a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
130a4 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 0a   xSectorSize */.
130a5 20 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 61    winDeviceChara
130a6 63 74 65 72 69 73 74 69 63 73 2c 20 20 20 20 20  cteristics,     
130a7 20 20 2f 2a 20 78 44 65 76 69 63 65 43 68 61 72    /* xDeviceChar
130a8 61 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 20  acteristics */. 
130a9 20 77 69 6e 53 68 6d 4d 61 70 2c 20 20 20 20 20   winShmMap,     
130aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
130ab 20 2f 2a 20 78 53 68 6d 4d 61 70 20 2a 2f 0a 20   /* xShmMap */. 
130ac 20 77 69 6e 53 68 6d 4c 6f 63 6b 2c 20 20 20 20   winShmLock,    
130ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
130ae 20 2f 2a 20 78 53 68 6d 4c 6f 63 6b 20 2a 2f 0a   /* xShmLock */.
130af 20 20 77 69 6e 53 68 6d 42 61 72 72 69 65 72 2c    winShmBarrier,
130b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
130b1 20 20 2f 2a 20 78 53 68 6d 42 61 72 72 69 65 72    /* xShmBarrier
130b2 20 2a 2f 0a 20 20 77 69 6e 53 68 6d 55 6e 6d 61   */.  winShmUnma
130b3 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p               
130b4 20 20 20 20 20 20 2f 2a 20 78 53 68 6d 55 6e 6d        /* xShmUnm
130b5 61 70 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a  ap */.};../*****
130b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
130b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
130b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
130b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
130ba 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
130bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
130bc 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66 73  **** sqlite3_vfs
130bd 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a   methods *******
130be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
130bf 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
130c0 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69   division contai
130c1 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ns the implement
130c2 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f 64 73  ation of methods
130c3 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74   on the.** sqlit
130c4 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a 2a  e3_vfs object..*
130c5 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  /../*.** Convert
130c6 20 61 20 55 54 46 2d 38 20 66 69 6c 65 6e 61 6d   a UTF-8 filenam
130c7 65 20 69 6e 74 6f 20 77 68 61 74 65 76 65 72 20  e into whatever 
130c8 66 6f 72 6d 20 74 68 65 20 75 6e 64 65 72 6c 79  form the underly
130c9 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67  ing.** operating
130ca 20 73 79 73 74 65 6d 20 77 61 6e 74 73 20 66 69   system wants fi
130cb 6c 65 6e 61 6d 65 73 20 69 6e 2e 20 20 53 70 61  lenames in.  Spa
130cc 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
130cd 65 73 75 6c 74 0a 2a 2a 20 69 73 20 6f 62 74 61  esult.** is obta
130ce 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  ined from malloc
130cf 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65   and must be fre
130d0 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ed by the callin
130d1 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  g.** function..*
130d2 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 63  /.static void *c
130d3 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61  onvertUtf8Filena
130d4 6d 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  me(const char *z
130d5 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 76 6f 69  Filename){.  voi
130d6 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20  d *zConverted = 
130d7 30 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20  0;.  if( isNT() 
130d8 29 7b 0a 20 20 20 20 7a 43 6f 6e 76 65 72 74 65  ){.    zConverte
130d9 64 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64  d = utf8ToUnicod
130da 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  e(zFilename);.  
130db 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  }.#ifdef SQLITE_
130dc 57 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20  WIN32_HAS_ANSI. 
130dd 20 65 6c 73 65 7b 0a 20 20 20 20 7a 43 6f 6e 76   else{.    zConv
130de 65 72 74 65 64 20 3d 20 73 71 6c 69 74 65 33 5f  erted = sqlite3_
130df 77 69 6e 33 32 5f 75 74 66 38 5f 74 6f 5f 6d 62  win32_utf8_to_mb
130e0 63 73 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  cs(zFilename);. 
130e1 20 7d 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 63   }.#endif.  /* c
130e2 61 6c 6c 65 72 20 77 69 6c 6c 20 68 61 6e 64 6c  aller will handl
130e3 65 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20  e out of memory 
130e4 2a 2f 0a 20 20 72 65 74 75 72 6e 20 7a 43 6f 6e  */.  return zCon
130e5 76 65 72 74 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  verted;.}../*.**
130e6 20 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72   Create a tempor
130e7 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e  ary file name in
130e8 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73   zBuf.  zBuf mus
130e9 74 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20  t be big enough 
130ea 74 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 70 56  to.** hold at pV
130eb 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 63  fs->mxPathname c
130ec 68 61 72 61 63 74 65 72 73 2e 0a 2a 2f 0a 73 74  haracters..*/.st
130ed 61 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70  atic int getTemp
130ee 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63  name(int nBuf, c
130ef 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74  har *zBuf){.  st
130f0 61 74 69 63 20 63 68 61 72 20 7a 43 68 61 72 73  atic char zChars
130f1 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66  [] =.    "abcdef
130f2 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76  ghijklmnopqrstuv
130f3 77 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45  wxyz".    "ABCDE
130f4 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55  FGHIJKLMNOPQRSTU
130f5 56 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33  VWXYZ".    "0123
130f6 34 35 36 37 38 39 22 3b 0a 20 20 73 69 7a 65 5f  456789";.  size_
130f7 74 20 69 2c 20 6a 3b 0a 20 20 69 6e 74 20 6e 54  t i, j;.  int nT
130f8 65 6d 70 50 61 74 68 3b 0a 20 20 63 68 61 72 20  empPath;.  char 
130f9 7a 54 65 6d 70 50 61 74 68 5b 4d 41 58 5f 50 41  zTempPath[MAX_PA
130fa 54 48 2b 32 5d 3b 0a 0a 20 20 2f 2a 20 49 74 27  TH+2];..  /* It'
130fb 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74  s odd to simulat
130fc 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65  e an io-error he
130fd 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74  re, but really t
130fe 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a  his is just.  **
130ff 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72   using the io-er
13100 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75  ror infrastructu
13101 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20  re to test that 
13102 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74  SQLite handles t
13103 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f  his.  ** functio
13104 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20 20 2a 2f  n failing. .  */
13105 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
13106 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
13107 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20 6d 65  E_IOERR );..  me
13108 6d 73 65 74 28 7a 54 65 6d 70 50 61 74 68 2c 20  mset(zTempPath, 
13109 30 2c 20 4d 41 58 5f 50 41 54 48 2b 32 29 3b 0a  0, MAX_PATH+2);.
1310a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74  .  if( sqlite3_t
1310b 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b  emp_directory ){
1310c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
1310d 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33  rintf(MAX_PATH-3
1310e 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25  0, zTempPath, "%
1310f 73 22 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d 70  s", sqlite3_temp
13110 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 7d  _directory);.  }
13111 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f  .#if !SQLITE_OS_
13112 57 49 4e 52 54 0a 20 20 65 6c 73 65 20 69 66 28  WINRT.  else if(
13113 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 63   isNT() ){.    c
13114 68 61 72 20 2a 7a 4d 75 6c 74 69 3b 0a 20 20 20  har *zMulti;.   
13115 20 57 43 48 41 52 20 7a 57 69 64 65 50 61 74 68   WCHAR zWidePath
13116 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20  [MAX_PATH];.    
13117 6f 73 47 65 74 54 65 6d 70 50 61 74 68 57 28 4d  osGetTempPathW(M
13118 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57 69 64  AX_PATH-30, zWid
13119 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d 75 6c  ePath);.    zMul
1311a 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74  ti = unicodeToUt
1311b 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b 0a 20  f8(zWidePath);. 
1311c 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20 29 7b     if( zMulti ){
1311d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
1311e 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48  nprintf(MAX_PATH
1311f 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20  -30, zTempPath, 
13120 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b 0a 20  "%s", zMulti);. 
13121 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
13122 65 28 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 20 7d  e(zMulti);.    }
13123 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75  else{.      retu
13124 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
13125 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d  NOMEM;.    }.  }
13126 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 57  .#ifdef SQLITE_W
13127 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20 20  IN32_HAS_ANSI.  
13128 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a  else{.    char *
13129 7a 55 74 66 38 3b 0a 20 20 20 20 63 68 61 72 20  zUtf8;.    char 
1312a 7a 4d 62 63 73 50 61 74 68 5b 4d 41 58 5f 50 41  zMbcsPath[MAX_PA
1312b 54 48 5d 3b 0a 20 20 20 20 6f 73 47 65 74 54 65  TH];.    osGetTe
1312c 6d 70 50 61 74 68 41 28 4d 41 58 5f 50 41 54 48  mpPathA(MAX_PATH
1312d 2d 33 30 2c 20 7a 4d 62 63 73 50 61 74 68 29 3b  -30, zMbcsPath);
1312e 0a 20 20 20 20 7a 55 74 66 38 20 3d 20 73 71 6c  .    zUtf8 = sql
1312f 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63 73 5f  ite3_win32_mbcs_
13130 74 6f 5f 75 74 66 38 28 7a 4d 62 63 73 50 61 74  to_utf8(zMbcsPat
13131 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 55 74 66  h);.    if( zUtf
13132 38 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  8 ){.      sqlit
13133 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f  e3_snprintf(MAX_
13134 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61  PATH-30, zTempPa
13135 74 68 2c 20 22 25 73 22 2c 20 7a 55 74 66 38 29  th, "%s", zUtf8)
13136 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
13137 66 72 65 65 28 7a 55 74 66 38 29 3b 0a 20 20 20  free(zUtf8);.   
13138 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65   }else{.      re
13139 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
1313a 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  R_NOMEM;.    }. 
1313b 20 7d 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66   }.#endif.#endif
1313c 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
1313d 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  t the output buf
1313e 66 65 72 20 69 73 20 6c 61 72 67 65 20 65 6e 6f  fer is large eno
1313f 75 67 68 20 66 6f 72 20 74 68 65 20 74 65 6d 70  ugh for the temp
13140 6f 72 61 72 79 20 66 69 6c 65 20 0a 20 20 2a 2a  orary file .  **
13141 20 6e 61 6d 65 2e 20 49 66 20 69 74 20 69 73 20   name. If it is 
13142 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  not, return SQLI
13143 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a 2f 0a 20  TE_ERROR..  */. 
13144 20 6e 54 65 6d 70 50 61 74 68 20 3d 20 73 71 6c   nTempPath = sql
13145 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65  ite3Strlen30(zTe
13146 6d 70 50 61 74 68 29 3b 0a 0a 20 20 69 66 28 20  mpPath);..  if( 
13147 28 6e 54 65 6d 70 50 61 74 68 20 2b 20 73 71 6c  (nTempPath + sql
13148 69 74 65 33 53 74 72 6c 65 6e 33 30 28 53 51 4c  ite3Strlen30(SQL
13149 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52  ITE_TEMP_FILE_PR
1314a 45 46 49 58 29 20 2b 20 31 38 29 20 3e 3d 20 6e  EFIX) + 18) >= n
1314b 42 75 66 20 29 7b 0a 20 20 20 20 72 65 74 75 72  Buf ){.    retur
1314c 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
1314d 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 6e 54 65    }..  for(i=nTe
1314e 6d 70 50 61 74 68 3b 20 69 3e 30 20 26 26 20 7a  mpPath; i>0 && z
1314f 54 65 6d 70 50 61 74 68 5b 69 2d 31 5d 3d 3d 27  TempPath[i-1]=='
13150 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20 7a 54  \\'; i--){}.  zT
13151 65 6d 70 50 61 74 68 5b 69 5d 20 3d 20 30 3b 0a  empPath[i] = 0;.
13152 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
13153 6e 74 66 28 6e 42 75 66 2d 31 38 2c 20 7a 42 75  ntf(nBuf-18, zBu
13154 66 2c 20 28 6e 54 65 6d 70 50 61 74 68 20 3e 20  f, (nTempPath > 
13155 30 29 20 3f 0a 20 20 20 20 20 20 20 20 20 20 20  0) ?.           
13156 20 20 20 20 20 20 20 20 20 20 20 20 22 25 73 5c              "%s\
13157 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49  \"SQLITE_TEMP_FI
13158 4c 45 5f 50 52 45 46 49 58 20 3a 20 53 51 4c 49  LE_PREFIX : SQLI
13159 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45  TE_TEMP_FILE_PRE
1315a 46 49 58 2c 0a 20 20 20 20 20 20 20 20 20 20 20  FIX,.           
1315b 20 20 20 20 20 20 20 20 7a 54 65 6d 70 50 61 74          zTempPat
1315c 68 29 3b 0a 20 20 6a 20 3d 20 73 71 6c 69 74 65  h);.  j = sqlite
1315d 33 53 74 72 6c 65 6e 33 30 28 7a 42 75 66 29 3b  3Strlen30(zBuf);
1315e 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f  .  sqlite3_rando
1315f 6d 6e 65 73 73 28 31 35 2c 20 26 7a 42 75 66 5b  mness(15, &zBuf[
13160 6a 5d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  j]);.  for(i=0; 
13161 69 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b  i<15; i++, j++){
13162 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28  .    zBuf[j] = (
13163 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75  char)zChars[ ((u
13164 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75  nsigned char)zBu
13165 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43  f[j])%(sizeof(zC
13166 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a  hars)-1) ];.  }.
13167 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20    zBuf[j] = 0;. 
13168 20 7a 42 75 66 5b 6a 2b 31 5d 20 3d 20 30 3b 0a   zBuf[j+1] = 0;.
13169 0a 20 20 4f 53 54 52 41 43 45 28 28 22 54 45 4d  .  OSTRACE(("TEM
1316a 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e  P FILENAME: %s\n
1316b 22 2c 20 7a 42 75 66 29 29 3b 0a 20 20 72 65 74  ", zBuf));.  ret
1316c 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a  urn SQLITE_OK; .
1316d 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1316e 54 52 55 45 20 69 66 20 74 68 65 20 6e 61 6d 65  TRUE if the name
1316f 64 20 66 69 6c 65 20 69 73 20 72 65 61 6c 6c 79  d file is really
13170 20 61 20 64 69 72 65 63 74 6f 72 79 2e 20 20 52   a directory.  R
13171 65 74 75 72 6e 20 66 61 6c 73 65 20 69 66 0a 2a  eturn false if.*
13172 2a 20 69 74 20 69 73 20 73 6f 6d 65 74 68 69 6e  * it is somethin
13173 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20 64  g other than a d
13174 69 72 65 63 74 6f 72 79 2c 20 6f 72 20 69 66 20  irectory, or if 
13175 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b 69 6e  there is any kin
13176 64 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61  d of memory.** a
13177 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 75 72  llocation failur
13178 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
13179 20 77 69 6e 49 73 44 69 72 28 63 6f 6e 73 74 20   winIsDir(const 
1317a 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64  void *zConverted
1317b 29 7b 0a 20 20 44 57 4f 52 44 20 61 74 74 72 3b  ){.  DWORD attr;
1317c 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20  .  int rc = 0;. 
1317d 20 44 57 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f   DWORD lastErrno
1317e 3b 0a 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20  ;..  if( isNT() 
1317f 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d  ){.    int cnt =
13180 20 30 3b 0a 20 20 20 20 57 49 4e 33 32 5f 46 49   0;.    WIN32_FI
13181 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 44 41 54  LE_ATTRIBUTE_DAT
13182 41 20 73 41 74 74 72 44 61 74 61 3b 0a 20 20 20  A sAttrData;.   
13183 20 6d 65 6d 73 65 74 28 26 73 41 74 74 72 44 61   memset(&sAttrDa
13184 74 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 41  ta, 0, sizeof(sA
13185 74 74 72 44 61 74 61 29 29 3b 0a 20 20 20 20 77  ttrData));.    w
13186 68 69 6c 65 28 20 21 28 72 63 20 3d 20 6f 73 47  hile( !(rc = osG
13187 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73  etFileAttributes
13188 45 78 57 28 28 4c 50 43 57 53 54 52 29 7a 43 6f  ExW((LPCWSTR)zCo
13189 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20  nverted,.       
1318a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1318b 20 20 20 20 20 20 47 65 74 46 69 6c 65 45 78 49        GetFileExI
1318c 6e 66 6f 53 74 61 6e 64 61 72 64 2c 0a 20 20 20  nfoStandard,.   
1318d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1318e 20 20 20 20 20 20 20 20 20 20 26 73 41 74 74 72            &sAttr
1318f 44 61 74 61 29 29 20 26 26 20 72 65 74 72 79 49  Data)) && retryI
13190 6f 65 72 72 28 26 63 6e 74 2c 20 26 6c 61 73 74  oerr(&cnt, &last
13191 45 72 72 6e 6f 29 20 29 7b 7d 0a 20 20 20 20 69  Errno) ){}.    i
13192 66 28 20 21 72 63 20 29 7b 0a 20 20 20 20 20 20  f( !rc ){.      
13193 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 49 6e 76  return 0; /* Inv
13194 61 6c 69 64 20 6e 61 6d 65 3f 20 2a 2f 0a 20 20  alid name? */.  
13195 20 20 7d 0a 20 20 20 20 61 74 74 72 20 3d 20 73    }.    attr = s
13196 41 74 74 72 44 61 74 61 2e 64 77 46 69 6c 65 41  AttrData.dwFileA
13197 74 74 72 69 62 75 74 65 73 3b 0a 23 69 66 20 53  ttributes;.#if S
13198 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
13199 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  0.  }else{.    a
1319a 74 74 72 20 3d 20 6f 73 47 65 74 46 69 6c 65 41  ttr = osGetFileA
1319b 74 74 72 69 62 75 74 65 73 41 28 28 63 68 61 72  ttributesA((char
1319c 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23  *)zConverted);.#
1319d 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75  endif.  }.  retu
1319e 72 6e 20 28 61 74 74 72 21 3d 49 4e 56 41 4c 49  rn (attr!=INVALI
1319f 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45  D_FILE_ATTRIBUTE
131a0 53 29 20 26 26 20 28 61 74 74 72 26 46 49 4c 45  S) && (attr&FILE
131a1 5f 41 54 54 52 49 42 55 54 45 5f 44 49 52 45 43  _ATTRIBUTE_DIREC
131a2 54 4f 52 59 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TORY);.}../*.** 
131a3 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a  Open a file..*/.
131a4 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4f 70  static int winOp
131a5 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  en(.  sqlite3_vf
131a6 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
131a7 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20  /* Not used */. 
131a8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
131a9 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  me,        /* Na
131aa 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 28  me of the file (
131ab 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69  UTF-8) */.  sqli
131ac 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20  te3_file *id,   
131ad 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
131ae 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68  he SQLite file h
131af 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20  andle here */.  
131b0 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20  int flags,      
131b1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
131b2 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a  n mode flags */.
131b3 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73    int *pOutFlags
131b4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
131b5 74 61 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61  tatus return fla
131b6 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 41 4e 44 4c  gs */.){.  HANDL
131b7 45 20 68 3b 0a 20 20 44 57 4f 52 44 20 6c 61 73  E h;.  DWORD las
131b8 74 45 72 72 6e 6f 3b 0a 20 20 44 57 4f 52 44 20  tErrno;.  DWORD 
131b9 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 3b  dwDesiredAccess;
131ba 0a 20 20 44 57 4f 52 44 20 64 77 53 68 61 72 65  .  DWORD dwShare
131bb 4d 6f 64 65 3b 0a 20 20 44 57 4f 52 44 20 64 77  Mode;.  DWORD dw
131bc 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74  CreationDisposit
131bd 69 6f 6e 3b 0a 20 20 44 57 4f 52 44 20 64 77 46  ion;.  DWORD dwF
131be 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65  lagsAndAttribute
131bf 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54  s = 0;.#if SQLIT
131c0 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74  E_OS_WINCE.  int
131c1 20 69 73 54 65 6d 70 20 3d 20 30 3b 0a 23 65 6e   isTemp = 0;.#en
131c2 64 69 66 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  dif.  winFile *p
131c3 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
131c4 29 69 64 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f  )id;.  void *zCo
131c5 6e 76 65 72 74 65 64 3b 20 20 20 20 20 20 20 20  nverted;        
131c6 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d        /* Filenam
131c7 65 20 69 6e 20 4f 53 20 65 6e 63 6f 64 69 6e 67  e in OS encoding
131c8 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
131c9 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 4e   *zUtf8Name = zN
131ca 61 6d 65 3b 20 2f 2a 20 46 69 6c 65 6e 61 6d 65  ame; /* Filename
131cb 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69   in UTF-8 encodi
131cc 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 20  ng */.  int cnt 
131cd 3d 20 30 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 72  = 0;..  /* If ar
131ce 67 75 6d 65 6e 74 20 7a 50 61 74 68 20 69 73 20  gument zPath is 
131cf 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  a NULL pointer, 
131d0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
131d1 20 72 65 71 75 69 72 65 64 20 74 6f 20 6f 70 65   required to ope
131d2 6e 0a 20 20 2a 2a 20 61 20 74 65 6d 70 6f 72 61  n.  ** a tempora
131d3 72 79 20 66 69 6c 65 2e 20 55 73 65 20 74 68 69  ry file. Use thi
131d4 73 20 62 75 66 66 65 72 20 74 6f 20 73 74 6f 72  s buffer to stor
131d5 65 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 20  e the file name 
131d6 69 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20  in..  */.  char 
131d7 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54  zTmpname[MAX_PAT
131d8 48 2b 32 5d 3b 20 20 20 20 20 2f 2a 20 42 75 66  H+2];     /* Buf
131d9 66 65 72 20 75 73 65 64 20 74 6f 20 63 72 65 61  fer used to crea
131da 74 65 20 74 65 6d 70 20 66 69 6c 65 6e 61 6d 65  te temp filename
131db 20 2a 2f 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20   */..  int rc = 
131dc 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
131dd 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f        /* Functio
131de 6e 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f  n Return Code */
131df 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  .#if !defined(ND
131e0 45 42 55 47 29 20 7c 7c 20 53 51 4c 49 54 45 5f  EBUG) || SQLITE_
131e1 4f 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74 20 65  OS_WINCE.  int e
131e2 54 79 70 65 20 3d 20 66 6c 61 67 73 26 30 78 46  Type = flags&0xF
131e3 46 46 46 46 46 30 30 3b 20 20 2f 2a 20 54 79 70  FFFFF00;  /* Typ
131e4 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 6f 70 65  e of file to ope
131e5 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 69  n */.#endif..  i
131e6 6e 74 20 69 73 45 78 63 6c 75 73 69 76 65 20 20  nt isExclusive  
131e7 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  = (flags & SQLIT
131e8 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
131e9 29 3b 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74  );.  int isDelet
131ea 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 26  e     = (flags &
131eb 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c   SQLITE_OPEN_DEL
131ec 45 54 45 4f 4e 43 4c 4f 53 45 29 3b 0a 20 20 69  ETEONCLOSE);.  i
131ed 6e 74 20 69 73 43 72 65 61 74 65 20 20 20 20 20  nt isCreate     
131ee 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  = (flags & SQLIT
131ef 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a  E_OPEN_CREATE);.
131f0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
131f1 20 69 6e 74 20 69 73 52 65 61 64 6f 6e 6c 79 20   int isReadonly 
131f2 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c    = (flags & SQL
131f3 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
131f4 59 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74  Y);.#endif.  int
131f5 20 69 73 52 65 61 64 57 72 69 74 65 20 20 3d 20   isReadWrite  = 
131f6 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  (flags & SQLITE_
131f7 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 3b  OPEN_READWRITE);
131f8 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
131f9 0a 20 20 69 6e 74 20 69 73 4f 70 65 6e 4a 6f 75  .  int isOpenJou
131fa 72 6e 61 6c 20 3d 20 28 69 73 43 72 65 61 74 65  rnal = (isCreate
131fb 20 26 26 20 28 0a 20 20 20 20 20 20 20 20 65 54   && (.        eT
131fc 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
131fd 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20  _MASTER_JOURNAL 
131fe 0a 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d  .     || eType==
131ff 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
13200 5f 4a 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 7c  _JOURNAL .     |
13201 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  | eType==SQLITE_
13202 4f 50 45 4e 5f 57 41 4c 0a 20 20 29 29 3b 0a 23  OPEN_WAL.  ));.#
13203 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 43 68 65 63  endif..  /* Chec
13204 6b 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  k the following 
13205 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 74  statements are t
13206 72 75 65 3a 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  rue: .  **.  ** 
13207 20 20 28 61 29 20 45 78 61 63 74 6c 79 20 6f 6e    (a) Exactly on
13208 65 20 6f 66 20 74 68 65 20 52 45 41 44 57 52 49  e of the READWRI
13209 54 45 20 61 6e 64 20 52 45 41 44 4f 4e 4c 59 20  TE and READONLY 
1320a 66 6c 61 67 73 20 6d 75 73 74 20 62 65 20 73 65  flags must be se
1320b 74 2c 20 61 6e 64 20 0a 20 20 2a 2a 20 20 20 28  t, and .  **   (
1320c 62 29 20 69 66 20 43 52 45 41 54 45 20 69 73 20  b) if CREATE is 
1320d 73 65 74 2c 20 74 68 65 6e 20 52 45 41 44 57 52  set, then READWR
1320e 49 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65  ITE must also be
1320f 20 73 65 74 2c 20 61 6e 64 0a 20 20 2a 2a 20 20   set, and.  **  
13210 20 28 63 29 20 69 66 20 45 58 43 4c 55 53 49 56   (c) if EXCLUSIV
13211 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 43  E is set, then C
13212 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73 6f 20  REATE must also 
13213 62 65 20 73 65 74 2e 0a 20 20 2a 2a 20 20 20 28  be set..  **   (
13214 64 29 20 69 66 20 44 45 4c 45 54 45 4f 4e 43 4c  d) if DELETEONCL
13215 4f 53 45 20 69 73 20 73 65 74 2c 20 74 68 65 6e  OSE is set, then
13216 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 6c 73   CREATE must als
13217 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2f 0a 20  o be set..  */. 
13218 20 61 73 73 65 72 74 28 28 69 73 52 65 61 64 6f   assert((isReado
13219 6e 6c 79 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64  nly==0 || isRead
1321a 57 72 69 74 65 3d 3d 30 29 20 26 26 20 28 69 73  Write==0) && (is
1321b 52 65 61 64 57 72 69 74 65 20 7c 7c 20 69 73 52  ReadWrite || isR
1321c 65 61 64 6f 6e 6c 79 29 29 3b 0a 20 20 61 73 73  eadonly));.  ass
1321d 65 72 74 28 69 73 43 72 65 61 74 65 3d 3d 30 20  ert(isCreate==0 
1321e 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 29 3b  || isReadWrite);
1321f 0a 20 20 61 73 73 65 72 74 28 69 73 45 78 63 6c  .  assert(isExcl
13220 75 73 69 76 65 3d 3d 30 20 7c 7c 20 69 73 43 72  usive==0 || isCr
13221 65 61 74 65 29 3b 0a 20 20 61 73 73 65 72 74 28  eate);.  assert(
13222 69 73 44 65 6c 65 74 65 3d 3d 30 20 7c 7c 20 69  isDelete==0 || i
13223 73 43 72 65 61 74 65 29 3b 0a 0a 20 20 2f 2a 20  sCreate);..  /* 
13224 54 68 65 20 6d 61 69 6e 20 44 42 2c 20 6d 61 69  The main DB, mai
13225 6e 20 6a 6f 75 72 6e 61 6c 2c 20 57 41 4c 20 66  n journal, WAL f
13226 69 6c 65 20 61 6e 64 20 6d 61 73 74 65 72 20 6a  ile and master j
13227 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72  ournal are never
13228 20 0a 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63   .  ** automatic
13229 61 6c 6c 79 20 64 65 6c 65 74 65 64 2e 20 4e 6f  ally deleted. No
1322a 72 20 61 72 65 20 74 68 65 79 20 65 76 65 72 20  r are they ever 
1322b 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e  temporary files.
1322c 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28    */.  assert( (
1322d 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61  !isDelete && zNa
1322e 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51  me) || eType!=SQ
1322f 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44  LITE_OPEN_MAIN_D
13230 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  B );.  assert( (
13231 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61  !isDelete && zNa
13232 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51  me) || eType!=SQ
13233 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a  LITE_OPEN_MAIN_J
13234 4f 55 52 4e 41 4c 20 29 3b 0a 20 20 61 73 73 65  OURNAL );.  asse
13235 72 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 26  rt( (!isDelete &
13236 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70  & zName) || eTyp
13237 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e!=SQLITE_OPEN_M
13238 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 29 3b  ASTER_JOURNAL );
13239 0a 20 20 61 73 73 65 72 74 28 20 28 21 69 73 44  .  assert( (!isD
1323a 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 29 20  elete && zName) 
1323b 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45  || eType!=SQLITE
1323c 5f 4f 50 45 4e 5f 57 41 4c 20 29 3b 0a 0a 20 20  _OPEN_WAL );..  
1323d 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74  /* Assert that t
1323e 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 20 68  he upper layer h
1323f 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 20 74 68  as set one of th
13240 65 20 22 66 69 6c 65 2d 74 79 70 65 22 20 66 6c  e "file-type" fl
13241 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ags. */.  assert
13242 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  ( eType==SQLITE_
13243 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20  OPEN_MAIN_DB    
13244 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49    || eType==SQLI
13245 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20  TE_OPEN_TEMP_DB 
13246 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65  .       || eType
13247 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  ==SQLITE_OPEN_MA
13248 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54  IN_JOURNAL || eT
13249 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
1324a 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 0a 20  _TEMP_JOURNAL . 
1324b 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d        || eType==
1324c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a  SQLITE_OPEN_SUBJ
1324d 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65 54 79 70  OURNAL   || eTyp
1324e 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e==SQLITE_OPEN_M
1324f 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 0a 20  ASTER_JOURNAL . 
13250 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d        || eType==
13251 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e  SQLITE_OPEN_TRAN
13252 53 49 45 4e 54 5f 44 42 20 7c 7c 20 65 54 79 70  SIENT_DB || eTyp
13253 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 57  e==SQLITE_OPEN_W
13254 41 4c 0a 20 20 29 3b 0a 0a 20 20 61 73 73 65 72  AL.  );..  asser
13255 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 55 4e  t( id!=0 );.  UN
13256 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
13257 56 66 73 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54  Vfs);..#if SQLIT
13258 45 5f 4f 53 5f 57 49 4e 52 54 0a 20 20 69 66 28  E_OS_WINRT.  if(
13259 20 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64   !sqlite3_temp_d
1325a 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20  irectory ){.    
1325b 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49  sqlite3_log(SQLI
1325c 54 45 5f 45 52 52 4f 52 2c 0a 20 20 20 20 20 20  TE_ERROR,.      
1325d 20 20 22 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f    "sqlite3_temp_
1325e 64 69 72 65 63 74 6f 72 79 20 76 61 72 69 61 62  directory variab
1325f 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74  le should be set
13260 20 66 6f 72 20 57 69 6e 52 54 22 29 3b 0a 20 20   for WinRT");.  
13261 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 70 46 69 6c  }.#endif..  pFil
13262 65 2d 3e 68 20 3d 20 49 4e 56 41 4c 49 44 5f 48  e->h = INVALID_H
13263 41 4e 44 4c 45 5f 56 41 4c 55 45 3b 0a 0a 20 20  ANDLE_VALUE;..  
13264 2f 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64  /* If the second
13265 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69   argument to thi
13266 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55  s function is NU
13267 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20 61 20 0a  LL, generate a .
13268 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66    ** temporary f
13269 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75 73 65 20  ile name to use 
1326a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 7a 55 74  .  */.  if( !zUt
1326b 66 38 4e 61 6d 65 20 29 7b 0a 20 20 20 20 61 73  f8Name ){.    as
1326c 73 65 72 74 28 69 73 44 65 6c 65 74 65 20 26 26  sert(isDelete &&
1326d 20 21 69 73 4f 70 65 6e 4a 6f 75 72 6e 61 6c 29   !isOpenJournal)
1326e 3b 0a 20 20 20 20 72 63 20 3d 20 67 65 74 54 65  ;.    rc = getTe
1326f 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 2b  mpname(MAX_PATH+
13270 32 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20  2, zTmpname);.  
13271 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13272 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
13273 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
13274 20 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 54    zUtf8Name = zT
13275 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f  mpname;.  }..  /
13276 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e  * Database filen
13277 61 6d 65 73 20 61 72 65 20 64 6f 75 62 6c 65 2d  ames are double-
13278 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 20  zero terminated 
13279 69 66 20 74 68 65 79 20 61 72 65 20 6e 6f 74 0a  if they are not.
1327a 20 20 2a 2a 20 55 52 49 73 20 77 69 74 68 20 70    ** URIs with p
1327b 61 72 61 6d 65 74 65 72 73 2e 20 20 48 65 6e 63  arameters.  Henc
1327c 65 2c 20 74 68 65 79 20 63 61 6e 20 61 6c 77 61  e, they can alwa
1327d 79 73 20 62 65 20 70 61 73 73 65 64 20 69 6e 74  ys be passed int
1327e 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 75  o.  ** sqlite3_u
1327f 72 69 5f 70 61 72 61 6d 65 74 65 72 28 29 2e 0a  ri_parameter()..
13280 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28    */.  assert( (
13281 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50  eType!=SQLITE_OP
13282 45 4e 5f 4d 41 49 4e 5f 44 42 29 20 7c 7c 20 28  EN_MAIN_DB) || (
13283 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
13284 50 45 4e 5f 55 52 49 29 20 7c 7c 0a 20 20 20 20  PEN_URI) ||.    
13285 20 20 20 20 7a 55 74 66 38 4e 61 6d 65 5b 73 74      zUtf8Name[st
13286 72 6c 65 6e 28 7a 55 74 66 38 4e 61 6d 65 29 2b  rlen(zUtf8Name)+
13287 31 5d 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 43  1]==0 );..  /* C
13288 6f 6e 76 65 72 74 20 74 68 65 20 66 69 6c 65 6e  onvert the filen
13289 61 6d 65 20 74 6f 20 74 68 65 20 73 79 73 74 65  ame to the syste
1328a 6d 20 65 6e 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20  m encoding. */. 
1328b 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f   zConverted = co
1328c 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d  nvertUtf8Filenam
1328d 65 28 7a 55 74 66 38 4e 61 6d 65 29 3b 0a 20 20  e(zUtf8Name);.  
1328e 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d  if( zConverted==
1328f 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
13290 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
13291 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 77  EM;.  }..  if( w
13292 69 6e 49 73 44 69 72 28 7a 43 6f 6e 76 65 72 74  inIsDir(zConvert
13293 65 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ed) ){.    sqlit
13294 65 33 5f 66 72 65 65 28 7a 43 6f 6e 76 65 72 74  e3_free(zConvert
13295 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ed);.    return 
13296 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5f  SQLITE_CANTOPEN_
13297 49 53 44 49 52 3b 0a 20 20 7d 0a 0a 20 20 69 66  ISDIR;.  }..  if
13298 28 20 69 73 52 65 61 64 57 72 69 74 65 20 29 7b  ( isReadWrite ){
13299 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63  .    dwDesiredAc
1329a 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52  cess = GENERIC_R
1329b 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 52  EAD | GENERIC_WR
1329c 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ITE;.  }else{.  
1329d 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73    dwDesiredAcces
1329e 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44  s = GENERIC_READ
1329f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 51 4c 49  ;.  }..  /* SQLI
132a0 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
132a1 45 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b  E is used to mak
132a2 65 20 73 75 72 65 20 74 68 61 74 20 61 20 6e 65  e sure that a ne
132a3 77 20 66 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20  w file is .  ** 
132a4 63 72 65 61 74 65 64 2e 20 53 51 4c 69 74 65 20  created. SQLite 
132a5 64 6f 65 73 6e 27 74 20 75 73 65 20 69 74 20 74  doesn't use it t
132a6 6f 20 69 6e 64 69 63 61 74 65 20 22 65 78 63 6c  o indicate "excl
132a7 75 73 69 76 65 20 61 63 63 65 73 73 22 20 0a 20  usive access" . 
132a8 20 2a 2a 20 61 73 20 69 74 20 69 73 20 75 73 75   ** as it is usu
132a9 61 6c 6c 79 20 75 6e 64 65 72 73 74 6f 6f 64 2e  ally understood.
132aa 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 45 78  .  */.  if( isEx
132ab 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 20 2f  clusive ){.    /
132ac 2a 20 43 72 65 61 74 65 73 20 61 20 6e 65 77 20  * Creates a new 
132ad 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74  file, only if it
132ae 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64   does not alread
132af 79 20 65 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20  y exist. */.    
132b0 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 65  /* If the file e
132b1 78 69 73 74 73 2c 20 69 74 20 66 61 69 6c 73 2e  xists, it fails.
132b2 20 2a 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69   */.    dwCreati
132b3 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20  onDisposition = 
132b4 43 52 45 41 54 45 5f 4e 45 57 3b 0a 20 20 7d 65  CREATE_NEW;.  }e
132b5 6c 73 65 20 69 66 28 20 69 73 43 72 65 61 74 65  lse if( isCreate
132b6 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20   ){.    /* Open 
132b7 65 78 69 73 74 69 6e 67 20 66 69 6c 65 2c 20 6f  existing file, o
132b8 72 20 63 72 65 61 74 65 20 69 66 20 69 74 20 64  r create if it d
132b9 6f 65 73 6e 27 74 20 65 78 69 73 74 20 2a 2f 0a  oesn't exist */.
132ba 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69      dwCreationDi
132bb 73 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e  sposition = OPEN
132bc 5f 41 4c 57 41 59 53 3b 0a 20 20 7d 65 6c 73 65  _ALWAYS;.  }else
132bd 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 73 20 61  {.    /* Opens a
132be 20 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69   file, only if i
132bf 74 20 65 78 69 73 74 73 2e 20 2a 2f 0a 20 20 20  t exists. */.   
132c0 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f   dwCreationDispo
132c1 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58  sition = OPEN_EX
132c2 49 53 54 49 4e 47 3b 0a 20 20 7d 0a 0a 20 20 64  ISTING;.  }..  d
132c3 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 46 49 4c  wShareMode = FIL
132c4 45 5f 53 48 41 52 45 5f 52 45 41 44 20 7c 20 46  E_SHARE_READ | F
132c5 49 4c 45 5f 53 48 41 52 45 5f 57 52 49 54 45 3b  ILE_SHARE_WRITE;
132c6 0a 0a 20 20 69 66 28 20 69 73 44 65 6c 65 74 65  ..  if( isDelete
132c7 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f   ){.#if SQLITE_O
132c8 53 5f 57 49 4e 43 45 0a 20 20 20 20 64 77 46 6c  S_WINCE.    dwFl
132c9 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73  agsAndAttributes
132ca 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54   = FILE_ATTRIBUT
132cb 45 5f 48 49 44 44 45 4e 3b 0a 20 20 20 20 69 73  E_HIDDEN;.    is
132cc 54 65 6d 70 20 3d 20 31 3b 0a 23 65 6c 73 65 0a  Temp = 1;.#else.
132cd 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74      dwFlagsAndAt
132ce 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f  tributes = FILE_
132cf 41 54 54 52 49 42 55 54 45 5f 54 45 4d 50 4f 52  ATTRIBUTE_TEMPOR
132d0 41 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20  ARY.            
132d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132d2 20 20 20 7c 20 46 49 4c 45 5f 41 54 54 52 49 42     | FILE_ATTRIB
132d3 55 54 45 5f 48 49 44 44 45 4e 0a 20 20 20 20 20  UTE_HIDDEN.     
132d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132d5 20 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c 45            | FILE
132d6 5f 46 4c 41 47 5f 44 45 4c 45 54 45 5f 4f 4e 5f  _FLAG_DELETE_ON_
132d7 43 4c 4f 53 45 3b 0a 23 65 6e 64 69 66 0a 20 20  CLOSE;.#endif.  
132d8 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 46 6c 61  }else{.    dwFla
132d9 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20  gsAndAttributes 
132da 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45  = FILE_ATTRIBUTE
132db 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 2f  _NORMAL;.  }.  /
132dc 2a 20 52 65 70 6f 72 74 73 20 66 72 6f 6d 20 74  * Reports from t
132dd 68 65 20 69 6e 74 65 72 6e 65 74 20 61 72 65 20  he internet are 
132de 74 68 61 74 20 70 65 72 66 6f 72 6d 61 6e 63 65  that performance
132df 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20   is always.  ** 
132e0 62 65 74 74 65 72 20 69 66 20 46 49 4c 45 5f 46  better if FILE_F
132e1 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53  LAG_RANDOM_ACCES
132e2 53 20 69 73 20 75 73 65 64 2e 20 20 54 69 63 6b  S is used.  Tick
132e3 65 74 20 23 32 36 39 39 2e 20 2a 2f 0a 23 69 66  et #2699. */.#if
132e4 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
132e5 0a 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74  .  dwFlagsAndAtt
132e6 72 69 62 75 74 65 73 20 7c 3d 20 46 49 4c 45 5f  ributes |= FILE_
132e7 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45  FLAG_RANDOM_ACCE
132e8 53 53 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66  SS;.#endif..  if
132e9 28 20 69 73 4e 54 28 29 20 29 7b 0a 23 69 66 20  ( isNT() ){.#if 
132ea 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 52 54 0a  SQLITE_OS_WINRT.
132eb 20 20 20 20 43 52 45 41 54 45 46 49 4c 45 32 5f      CREATEFILE2_
132ec 45 58 54 45 4e 44 45 44 5f 50 41 52 41 4d 45 54  EXTENDED_PARAMET
132ed 45 52 53 20 65 78 74 65 6e 64 65 64 50 61 72 61  ERS extendedPara
132ee 6d 65 74 65 72 73 3b 0a 20 20 20 20 65 78 74 65  meters;.    exte
132ef 6e 64 65 64 50 61 72 61 6d 65 74 65 72 73 2e 64  ndedParameters.d
132f0 77 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 43  wSize = sizeof(C
132f1 52 45 41 54 45 46 49 4c 45 32 5f 45 58 54 45 4e  REATEFILE2_EXTEN
132f2 44 45 44 5f 50 41 52 41 4d 45 54 45 52 53 29 3b  DED_PARAMETERS);
132f3 0a 20 20 20 20 65 78 74 65 6e 64 65 64 50 61 72  .    extendedPar
132f4 61 6d 65 74 65 72 73 2e 64 77 46 69 6c 65 41 74  ameters.dwFileAt
132f5 74 72 69 62 75 74 65 73 20 3d 0a 20 20 20 20 20  tributes =.     
132f6 20 20 20 20 20 20 20 64 77 46 6c 61 67 73 41 6e         dwFlagsAn
132f7 64 41 74 74 72 69 62 75 74 65 73 20 26 20 46 49  dAttributes & FI
132f8 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 4d 41 53  LE_ATTRIBUTE_MAS
132f9 4b 3b 0a 20 20 20 20 65 78 74 65 6e 64 65 64 50  K;.    extendedP
132fa 61 72 61 6d 65 74 65 72 73 2e 64 77 46 69 6c 65  arameters.dwFile
132fb 46 6c 61 67 73 20 3d 20 64 77 46 6c 61 67 73 41  Flags = dwFlagsA
132fc 6e 64 41 74 74 72 69 62 75 74 65 73 20 26 20 46  ndAttributes & F
132fd 49 4c 45 5f 46 4c 41 47 5f 4d 41 53 4b 3b 0a 20  ILE_FLAG_MASK;. 
132fe 20 20 20 65 78 74 65 6e 64 65 64 50 61 72 61 6d     extendedParam
132ff 65 74 65 72 73 2e 64 77 53 65 63 75 72 69 74 79  eters.dwSecurity
13300 51 6f 73 46 6c 61 67 73 20 3d 20 53 45 43 55 52  QosFlags = SECUR
13301 49 54 59 5f 41 4e 4f 4e 59 4d 4f 55 53 3b 0a 20  ITY_ANONYMOUS;. 
13302 20 20 20 65 78 74 65 6e 64 65 64 50 61 72 61 6d     extendedParam
13303 65 74 65 72 73 2e 6c 70 53 65 63 75 72 69 74 79  eters.lpSecurity
13304 41 74 74 72 69 62 75 74 65 73 20 3d 20 4e 55 4c  Attributes = NUL
13305 4c 3b 0a 20 20 20 20 65 78 74 65 6e 64 65 64 50  L;.    extendedP
13306 61 72 61 6d 65 74 65 72 73 2e 68 54 65 6d 70 6c  arameters.hTempl
13307 61 74 65 46 69 6c 65 20 3d 20 4e 55 4c 4c 3b 0a  ateFile = NULL;.
13308 20 20 20 20 77 68 69 6c 65 28 20 28 68 20 3d 20      while( (h = 
13309 6f 73 43 72 65 61 74 65 46 69 6c 65 32 28 28 4c  osCreateFile2((L
1330a 50 43 57 53 54 52 29 7a 43 6f 6e 76 65 72 74 65  PCWSTR)zConverte
1330b 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
1330c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1330d 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73   dwDesiredAccess
1330e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1330f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13310 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20  dwShareMode,.   
13311 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13312 20 20 20 20 20 20 20 20 20 20 20 64 77 43 72 65             dwCre
13313 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e  ationDisposition
13314 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
13315 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13316 26 65 78 74 65 6e 64 65 64 50 61 72 61 6d 65 74  &extendedParamet
13317 65 72 73 29 29 3d 3d 49 4e 56 41 4c 49 44 5f 48  ers))==INVALID_H
13318 41 4e 44 4c 45 5f 56 41 4c 55 45 20 26 26 0a 20  ANDLE_VALUE &&. 
13319 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1331a 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74               ret
1331b 72 79 49 6f 65 72 72 28 26 63 6e 74 2c 20 26 6c  ryIoerr(&cnt, &l
1331c 61 73 74 45 72 72 6e 6f 29 20 29 7b 0a 20 20 20  astErrno) ){.   
1331d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1331e 6f 6f 70 20 2a 2f 0a 20 20 20 20 7d 0a 23 65 6c  oop */.    }.#el
1331f 73 65 0a 20 20 20 20 77 68 69 6c 65 28 20 28 68  se.    while( (h
13320 20 3d 20 6f 73 43 72 65 61 74 65 46 69 6c 65 57   = osCreateFileW
13321 28 28 4c 50 43 57 53 54 52 29 7a 43 6f 6e 76 65  ((LPCWSTR)zConve
13322 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20 20 20  rted,.          
13323 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13324 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63      dwDesiredAcc
13325 65 73 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ess,.           
13326 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13327 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 2c 20     dwShareMode, 
13328 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20  NULL,.          
13329 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1332a 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69      dwCreationDi
1332b 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20  sposition,.     
1332c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1332d 20 20 20 20 20 20 20 20 20 64 77 46 6c 61 67 73           dwFlags
1332e 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a 20  AndAttributes,. 
1332f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13330 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c               NUL
13331 4c 29 29 3d 3d 49 4e 56 41 4c 49 44 5f 48 41 4e  L))==INVALID_HAN
13332 44 4c 45 5f 56 41 4c 55 45 20 26 26 0a 20 20 20  DLE_VALUE &&.   
13333 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13334 20 20 20 20 20 20 20 20 20 20 20 72 65 74 72 79             retry
13335 49 6f 65 72 72 28 26 63 6e 74 2c 20 26 6c 61 73  Ioerr(&cnt, &las
13336 74 45 72 72 6e 6f 29 20 29 7b 0a 20 20 20 20 20  tErrno) ){.     
13337 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 6f            /* Noo
13338 70 20 2a 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69  p */.    }.#endi
13339 66 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c  f.  }.#ifdef SQL
1333a 49 54 45 5f 57 49 4e 33 32 5f 48 41 53 5f 41 4e  ITE_WIN32_HAS_AN
1333b 53 49 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 77  SI.  else{.    w
1333c 68 69 6c 65 28 20 28 68 20 3d 20 6f 73 43 72 65  hile( (h = osCre
1333d 61 74 65 46 69 6c 65 41 28 28 4c 50 43 53 54 52  ateFileA((LPCSTR
1333e 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20  )zConverted,.   
1333f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13340 20 20 20 20 20 20 20 20 20 20 20 64 77 44 65 73             dwDes
13341 69 72 65 64 41 63 63 65 73 73 2c 0a 20 20 20 20  iredAccess,.    
13342 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13343 20 20 20 20 20 20 20 20 20 20 64 77 53 68 61 72            dwShar
13344 65 4d 6f 64 65 2c 20 4e 55 4c 4c 2c 0a 20 20 20  eMode, NULL,.   
13345 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13346 20 20 20 20 20 20 20 20 20 20 20 64 77 43 72 65             dwCre
13347 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e  ationDisposition
13348 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
13349 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1334a 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62  dwFlagsAndAttrib
1334b 75 74 65 73 2c 0a 20 20 20 20 20 20 20 20 20 20  utes,.          
1334c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1334d 20 20 20 20 4e 55 4c 4c 29 29 3d 3d 49 4e 56 41      NULL))==INVA
1334e 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45  LID_HANDLE_VALUE
1334f 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20   &&.            
13350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13351 20 20 72 65 74 72 79 49 6f 65 72 72 28 26 63 6e    retryIoerr(&cn
13352 74 2c 20 26 6c 61 73 74 45 72 72 6e 6f 29 20 29  t, &lastErrno) )
13353 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
13354 20 2f 2a 20 4e 6f 6f 70 20 2a 2f 0a 20 20 20 20   /* Noop */.    
13355 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 6c  }.  }.#endif.  l
13356 6f 67 49 6f 65 72 72 28 63 6e 74 29 3b 0a 0a 20  ogIoerr(cnt);.. 
13357 20 4f 53 54 52 41 43 45 28 28 22 4f 50 45 4e 20   OSTRACE(("OPEN 
13358 25 64 20 25 73 20 30 78 25 6c 78 20 25 73 5c 6e  %d %s 0x%lx %s\n
13359 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 68  ", .           h
1335a 2c 20 7a 4e 61 6d 65 2c 20 64 77 44 65 73 69 72  , zName, dwDesir
1335b 65 64 41 63 63 65 73 73 2c 20 0a 20 20 20 20 20  edAccess, .     
1335c 20 20 20 20 20 20 68 3d 3d 49 4e 56 41 4c 49 44        h==INVALID
1335d 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 20 3f 20  _HANDLE_VALUE ? 
1335e 22 66 61 69 6c 65 64 22 20 3a 20 22 6f 6b 22 29  "failed" : "ok")
1335f 29 3b 0a 0a 20 20 69 66 28 20 68 3d 3d 49 4e 56  );..  if( h==INV
13360 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55  ALID_HANDLE_VALU
13361 45 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  E ){.    pFile->
13362 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 61 73 74  lastErrno = last
13363 45 72 72 6e 6f 3b 0a 20 20 20 20 77 69 6e 4c 6f  Errno;.    winLo
13364 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 43 41  gError(SQLITE_CA
13365 4e 54 4f 50 45 4e 2c 20 70 46 69 6c 65 2d 3e 6c  NTOPEN, pFile->l
13366 61 73 74 45 72 72 6e 6f 2c 20 22 77 69 6e 4f 70  astErrno, "winOp
13367 65 6e 22 2c 20 7a 55 74 66 38 4e 61 6d 65 29 3b  en", zUtf8Name);
13368 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
13369 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
1336a 20 20 20 69 66 28 20 69 73 52 65 61 64 57 72 69     if( isReadWri
1336b 74 65 20 26 26 20 21 69 73 45 78 63 6c 75 73 69  te && !isExclusi
1336c 76 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ve ){.      retu
1336d 72 6e 20 77 69 6e 4f 70 65 6e 28 70 56 66 73 2c  rn winOpen(pVfs,
1336e 20 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a 20 20 20   zName, id, .   
1336f 20 20 20 20 20 20 20 20 20 20 28 28 66 6c 61 67            ((flag
13370 73 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  s|SQLITE_OPEN_RE
13371 41 44 4f 4e 4c 59 29 26 7e 28 53 51 4c 49 54 45  ADONLY)&~(SQLITE
13372 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c  _OPEN_CREATE|SQL
13373 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
13374 54 45 29 29 2c 20 70 4f 75 74 46 6c 61 67 73 29  TE)), pOutFlags)
13375 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
13376 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
13377 5f 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 3b 0a  _CANTOPEN_BKPT;.
13378 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
13379 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 20   pOutFlags ){.  
1337a 20 20 69 66 28 20 69 73 52 65 61 64 57 72 69 74    if( isReadWrit
1337b 65 20 29 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74  e ){.      *pOut
1337c 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f  Flags = SQLITE_O
1337d 50 45 4e 5f 52 45 41 44 57 52 49 54 45 3b 0a 20  PEN_READWRITE;. 
1337e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1337f 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 4c  *pOutFlags = SQL
13380 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
13381 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  Y;.    }.  }..  
13382 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c  memset(pFile, 0,
13383 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29 29   sizeof(*pFile))
13384 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68  ;.  pFile->pMeth
13385 6f 64 20 3d 20 26 77 69 6e 49 6f 4d 65 74 68 6f  od = &winIoMetho
13386 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20  d;.  pFile->h = 
13387 68 3b 0a 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  h;.  pFile->last
13388 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45 52 52 4f 52  Errno = NO_ERROR
13389 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 56 66 73 20  ;.  pFile->pVfs 
1338a 3d 20 70 56 66 73 3b 0a 23 69 66 6e 64 65 66 20  = pVfs;.#ifndef 
1338b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a  SQLITE_OMIT_WAL.
1338c 20 20 70 46 69 6c 65 2d 3e 70 53 68 6d 20 3d 20    pFile->pShm = 
1338d 30 3b 0a 23 65 6e 64 69 66 0a 20 20 70 46 69 6c  0;.#endif.  pFil
1338e 65 2d 3e 7a 50 61 74 68 20 3d 20 7a 4e 61 6d 65  e->zPath = zName
1338f 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ;.  if( sqlite3_
13390 75 72 69 5f 62 6f 6f 6c 65 61 6e 28 7a 4e 61 6d  uri_boolean(zNam
13391 65 2c 20 22 70 73 6f 77 22 2c 20 53 51 4c 49 54  e, "psow", SQLIT
13392 45 5f 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52  E_POWERSAFE_OVER
13393 57 52 49 54 45 29 20 29 7b 0a 20 20 20 20 70 46  WRITE) ){.    pF
13394 69 6c 65 2d 3e 63 74 72 6c 46 6c 61 67 73 20 7c  ile->ctrlFlags |
13395 3d 20 57 49 4e 46 49 4c 45 5f 50 53 4f 57 3b 0a  = WINFILE_PSOW;.
13396 20 20 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f    }..#if SQLITE_
13397 4f 53 5f 57 49 4e 43 45 0a 20 20 69 66 28 20 69  OS_WINCE.  if( i
13398 73 52 65 61 64 57 72 69 74 65 20 26 26 20 65 54  sReadWrite && eT
13399 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
1339a 5f 4d 41 49 4e 5f 44 42 0a 20 20 20 20 20 20 20  _MAIN_DB.       
1339b 26 26 20 21 77 69 6e 63 65 43 72 65 61 74 65 4c  && !winceCreateL
1339c 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70 46 69 6c 65  ock(zName, pFile
1339d 29 0a 20 20 29 7b 0a 20 20 20 20 6f 73 43 6c 6f  ).  ){.    osClo
1339e 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a 20 20 20  seHandle(h);.   
1339f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43   sqlite3_free(zC
133a0 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 72  onverted);.    r
133a1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e  eturn SQLITE_CAN
133a2 54 4f 50 45 4e 5f 42 4b 50 54 3b 0a 20 20 7d 0a  TOPEN_BKPT;.  }.
133a3 20 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b 0a    if( isTemp ){.
133a4 20 20 20 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65      pFile->zDele
133a5 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a 43 6f 6e  teOnClose = zCon
133a6 76 65 72 74 65 64 3b 0a 20 20 7d 65 6c 73 65 0a  verted;.  }else.
133a7 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 73  #endif.  {.    s
133a8 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6e  qlite3_free(zCon
133a9 76 65 72 74 65 64 29 3b 0a 20 20 7d 0a 0a 20 20  verted);.  }..  
133aa 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b  OpenCounter(+1);
133ab 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
133ac 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68  ./*.** Delete th
133ad 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a  e named file..**
133ae 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 57 69  .** Note that Wi
133af 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61  ndows does not a
133b0 6c 6c 6f 77 20 61 20 66 69 6c 65 20 74 6f 20 62  llow a file to b
133b1 65 20 64 65 6c 65 74 65 64 20 69 66 20 73 6f 6d  e deleted if som
133b2 65 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65  e other.** proce
133b3 73 73 20 68 61 73 20 69 74 20 6f 70 65 6e 2e 20  ss has it open. 
133b4 20 53 6f 6d 65 74 69 6d 65 73 20 61 20 76 69 72   Sometimes a vir
133b5 75 73 20 73 63 61 6e 6e 65 72 20 6f 72 20 69 6e  us scanner or in
133b6 64 65 78 69 6e 67 20 70 72 6f 67 72 61 6d 0a 2a  dexing program.*
133b7 2a 20 77 69 6c 6c 20 6f 70 65 6e 20 61 20 6a 6f  * will open a jo
133b8 75 72 6e 61 6c 20 66 69 6c 65 20 73 68 6f 72 74  urnal file short
133b9 6c 79 20 61 66 74 65 72 20 69 74 20 69 73 20 63  ly after it is c
133ba 72 65 61 74 65 64 20 69 6e 20 6f 72 64 65 72 20  reated in order 
133bb 74 6f 20 64 6f 0a 2a 2a 20 77 68 61 74 65 76 65  to do.** whateve
133bc 72 20 69 74 20 64 6f 65 73 2e 20 20 57 68 69 6c  r it does.  Whil
133bd 65 20 74 68 69 73 20 6f 74 68 65 72 20 70 72 6f  e this other pro
133be 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20  cess is holding 
133bf 74 68 65 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 6e  the.** file open
133c0 2c 20 77 65 20 77 69 6c 6c 20 62 65 20 75 6e 61  , we will be una
133c1 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74  ble to delete it
133c2 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e  .  To work aroun
133c3 64 20 74 68 69 73 0a 2a 2a 20 70 72 6f 62 6c 65  d this.** proble
133c4 6d 2c 20 77 65 20 64 65 6c 61 79 20 31 30 30 20  m, we delay 100 
133c5 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64  milliseconds and
133c6 20 74 72 79 20 74 6f 20 64 65 6c 65 74 65 20 61   try to delete a
133c7 67 61 69 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f 20  gain.  Up.** to 
133c8 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45  MX_DELETION_ATTE
133c9 4d 50 54 73 20 64 65 6c 65 74 69 6f 6e 20 61 74  MPTs deletion at
133ca 74 65 6d 70 74 73 20 61 72 65 20 72 75 6e 20 62  tempts are run b
133cb 65 66 6f 72 65 20 67 69 76 69 6e 67 0a 2a 2a 20  efore giving.** 
133cc 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67  up and returning
133cd 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74   an error..*/.st
133ce 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 6c 65  atic int winDele
133cf 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  te(.  sqlite3_vf
133d0 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
133d1 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e    /* Not used on
133d2 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73   win32 */.  cons
133d3 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
133d4 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  e,      /* Name 
133d5 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74  of file to delet
133d6 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44  e */.  int syncD
133d7 69 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ir              
133d8 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f     /* Not used o
133d9 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a 20 20  n win32 */.){.  
133da 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69  int cnt = 0;.  i
133db 6e 74 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 61  nt rc;.  DWORD a
133dc 74 74 72 3b 0a 20 20 44 57 4f 52 44 20 6c 61 73  ttr;.  DWORD las
133dd 74 45 72 72 6e 6f 3b 0a 20 20 76 6f 69 64 20 2a  tErrno;.  void *
133de 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 55 4e  zConverted;.  UN
133df 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
133e0 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  Vfs);.  UNUSED_P
133e1 41 52 41 4d 45 54 45 52 28 73 79 6e 63 44 69 72  ARAMETER(syncDir
133e2 29 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  );..  SimulateIO
133e3 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c  Error(return SQL
133e4 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45  ITE_IOERR_DELETE
133e5 29 3b 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20  );.  zConverted 
133e6 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c  = convertUtf8Fil
133e7 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29  ename(zFilename)
133e8 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74  ;.  if( zConvert
133e9 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ed==0 ){.    ret
133ea 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
133eb 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66  _NOMEM;.  }.  if
133ec 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
133ed 64 6f 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  do {.#if SQLITE_
133ee 4f 53 5f 57 49 4e 52 54 0a 20 20 20 20 20 20 57  OS_WINRT.      W
133ef 49 4e 33 32 5f 46 49 4c 45 5f 41 54 54 52 49 42  IN32_FILE_ATTRIB
133f0 55 54 45 5f 44 41 54 41 20 73 41 74 74 72 44 61  UTE_DATA sAttrDa
133f1 74 61 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74  ta;.      memset
133f2 28 26 73 41 74 74 72 44 61 74 61 2c 20 30 2c 20  (&sAttrData, 0, 
133f3 73 69 7a 65 6f 66 28 73 41 74 74 72 44 61 74 61  sizeof(sAttrData
133f4 29 29 3b 0a 20 20 20 20 20 20 69 66 20 28 20 6f  ));.      if ( o
133f5 73 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74  sGetFileAttribut
133f6 65 73 45 78 57 28 7a 43 6f 6e 76 65 72 74 65 64  esExW(zConverted
133f7 2c 20 47 65 74 46 69 6c 65 45 78 49 6e 66 6f 53  , GetFileExInfoS
133f8 74 61 6e 64 61 72 64 2c 0a 20 20 20 20 20 20 20  tandard,.       
133f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
133fa 20 20 20 20 20 20 20 20 20 20 20 26 73 41 74 74             &sAtt
133fb 72 44 61 74 61 29 20 29 7b 0a 20 20 20 20 20 20  rData) ){.      
133fc 20 20 61 74 74 72 20 3d 20 73 41 74 74 72 44 61    attr = sAttrDa
133fd 74 61 2e 64 77 46 69 6c 65 41 74 74 72 69 62 75  ta.dwFileAttribu
133fe 74 65 73 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  tes;.      }else
133ff 7b 0a 20 20 20 20 20 20 20 20 6c 61 73 74 45 72  {.        lastEr
13400 72 6e 6f 20 3d 20 6f 73 47 65 74 4c 61 73 74 45  rno = osGetLastE
13401 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20 20 20  rror();.        
13402 69 66 28 20 6c 61 73 74 45 72 72 6e 6f 3d 3d 45  if( lastErrno==E
13403 52 52 4f 52 5f 46 49 4c 45 5f 4e 4f 54 5f 46 4f  RROR_FILE_NOT_FO
13404 55 4e 44 20 7c 7c 20 6c 61 73 74 45 72 72 6e 6f  UND || lastErrno
13405 3d 3d 45 52 52 4f 52 5f 50 41 54 48 5f 4e 4f 54  ==ERROR_PATH_NOT
13406 5f 46 4f 55 4e 44 20 29 7b 0a 20 20 20 20 20 20  _FOUND ){.      
13407 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
13408 49 4f 45 52 52 5f 44 45 4c 45 54 45 5f 4e 4f 45  IOERR_DELETE_NOE
13409 4e 54 3b 20 2f 2a 20 41 6c 72 65 61 64 79 20 67  NT; /* Already g
1340a 6f 6e 65 3f 20 2a 2f 0a 20 20 20 20 20 20 20 20  one? */.        
1340b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
1340c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
1340d 4f 52 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  OR;.        }.  
1340e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1340f 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 20     }.#else.     
13410 20 61 74 74 72 20 3d 20 6f 73 47 65 74 46 69 6c   attr = osGetFil
13411 65 41 74 74 72 69 62 75 74 65 73 57 28 7a 43 6f  eAttributesW(zCo
13412 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64 69 66  nverted);.#endif
13413 0a 20 20 20 20 20 20 69 66 20 28 20 61 74 74 72  .      if ( attr
13414 3d 3d 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41  ==INVALID_FILE_A
13415 54 54 52 49 42 55 54 45 53 20 29 7b 0a 20 20 20  TTRIBUTES ){.   
13416 20 20 20 20 20 6c 61 73 74 45 72 72 6e 6f 20 3d       lastErrno =
13417 20 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28   osGetLastError(
13418 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6c  );.        if( l
13419 61 73 74 45 72 72 6e 6f 3d 3d 45 52 52 4f 52 5f  astErrno==ERROR_
1341a 46 49 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44 20 7c  FILE_NOT_FOUND |
1341b 7c 20 6c 61 73 74 45 72 72 6e 6f 3d 3d 45 52 52  | lastErrno==ERR
1341c 4f 52 5f 50 41 54 48 5f 4e 4f 54 5f 46 4f 55 4e  OR_PATH_NOT_FOUN
1341d 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  D ){.          r
1341e 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
1341f 5f 44 45 4c 45 54 45 5f 4e 4f 45 4e 54 3b 20 2f  _DELETE_NOENT; /
13420 2a 20 41 6c 72 65 61 64 79 20 67 6f 6e 65 3f 20  * Already gone? 
13421 2a 2f 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  */.        }else
13422 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
13423 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
13424 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
13425 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
13426 20 20 20 20 20 20 69 66 20 28 20 61 74 74 72 26        if ( attr&
13427 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 44  FILE_ATTRIBUTE_D
13428 49 52 45 43 54 4f 52 59 20 29 7b 0a 20 20 20 20  IRECTORY ){.    
13429 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1342a 45 52 52 4f 52 3b 20 2f 2a 20 46 69 6c 65 73 20  ERROR; /* Files 
1342b 6f 6e 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20  only. */.       
1342c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
1342d 20 20 20 20 20 20 69 66 20 28 20 6f 73 44 65 6c        if ( osDel
1342e 65 74 65 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72  eteFileW(zConver
1342f 74 65 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ted) ){.        
13430 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
13431 2f 2a 20 44 65 6c 65 74 65 64 20 4f 4b 2e 20 2a  /* Deleted OK. *
13432 2f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  /.        break;
13433 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
13434 66 20 28 20 21 72 65 74 72 79 49 6f 65 72 72 28  f ( !retryIoerr(
13435 26 63 6e 74 2c 20 26 6c 61 73 74 45 72 72 6e 6f  &cnt, &lastErrno
13436 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  ) ){.        rc 
13437 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20  = SQLITE_ERROR; 
13438 2f 2a 20 4e 6f 20 6d 6f 72 65 20 72 65 74 72 69  /* No more retri
13439 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 62  es. */.        b
1343a 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1343b 20 20 7d 20 77 68 69 6c 65 28 31 29 3b 0a 20 20    } while(1);.  
1343c 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  }.#ifdef SQLITE_
1343d 57 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20  WIN32_HAS_ANSI. 
1343e 20 65 6c 73 65 7b 0a 20 20 20 20 64 6f 20 7b 0a   else{.    do {.
1343f 20 20 20 20 20 20 61 74 74 72 20 3d 20 6f 73 47        attr = osG
13440 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73  etFileAttributes
13441 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  A(zConverted);. 
13442 20 20 20 20 20 69 66 20 28 20 61 74 74 72 3d 3d       if ( attr==
13443 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54  INVALID_FILE_ATT
13444 52 49 42 55 54 45 53 20 29 7b 0a 20 20 20 20 20  RIBUTES ){.     
13445 20 20 20 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6f     lastErrno = o
13446 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b  sGetLastError();
13447 0a 20 20 20 20 20 20 20 20 69 66 28 20 6c 61 73  .        if( las
13448 74 45 72 72 6e 6f 3d 3d 45 52 52 4f 52 5f 46 49  tErrno==ERROR_FI
13449 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44 20 7c 7c 20  LE_NOT_FOUND || 
1344a 6c 61 73 74 45 72 72 6e 6f 3d 3d 45 52 52 4f 52  lastErrno==ERROR
1344b 5f 50 41 54 48 5f 4e 4f 54 5f 46 4f 55 4e 44 20  _PATH_NOT_FOUND 
1344c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
1344d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44  = SQLITE_IOERR_D
1344e 45 4c 45 54 45 5f 4e 4f 45 4e 54 3b 20 2f 2a 20  ELETE_NOENT; /* 
1344f 41 6c 72 65 61 64 79 20 67 6f 6e 65 3f 20 2a 2f  Already gone? */
13450 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
13451 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
13452 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
13453 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62       }.        b
13454 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
13455 20 20 20 20 69 66 20 28 20 61 74 74 72 26 46 49      if ( attr&FI
13456 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 44 49 52  LE_ATTRIBUTE_DIR
13457 45 43 54 4f 52 59 20 29 7b 0a 20 20 20 20 20 20  ECTORY ){.      
13458 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
13459 52 4f 52 3b 20 2f 2a 20 46 69 6c 65 73 20 6f 6e  ROR; /* Files on
1345a 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 62  ly. */.        b
1345b 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1345c 20 20 20 20 69 66 20 28 20 6f 73 44 65 6c 65 74      if ( osDelet
1345d 65 46 69 6c 65 41 28 7a 43 6f 6e 76 65 72 74 65  eFileA(zConverte
1345e 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  d) ){.        rc
1345f 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 2f 2a   = SQLITE_OK; /*
13460 20 44 65 6c 65 74 65 64 20 4f 4b 2e 20 2a 2f 0a   Deleted OK. */.
13461 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
13462 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 20       }.      if 
13463 28 20 21 72 65 74 72 79 49 6f 65 72 72 28 26 63  ( !retryIoerr(&c
13464 6e 74 2c 20 26 6c 61 73 74 45 72 72 6e 6f 29 20  nt, &lastErrno) 
13465 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
13466 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20 2f 2a  SQLITE_ERROR; /*
13467 20 4e 6f 20 6d 6f 72 65 20 72 65 74 72 69 65 73   No more retries
13468 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 65  . */.        bre
13469 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1346a 7d 20 77 68 69 6c 65 28 31 29 3b 0a 20 20 7d 0a  } while(1);.  }.
1346b 23 65 6e 64 69 66 0a 20 20 69 66 28 20 72 63 20  #endif.  if( rc 
1346c 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f  && rc!=SQLITE_IO
1346d 45 52 52 5f 44 45 4c 45 54 45 5f 4e 4f 45 4e 54  ERR_DELETE_NOENT
1346e 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 77 69 6e   ){.    rc = win
1346f 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f  LogError(SQLITE_
13470 49 4f 45 52 52 5f 44 45 4c 45 54 45 2c 20 6c 61  IOERR_DELETE, la
13471 73 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20 20  stErrno,.       
13472 20 20 20 20 20 20 22 77 69 6e 44 65 6c 65 74 65        "winDelete
13473 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  ", zFilename);. 
13474 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 67 49   }else{.    logI
13475 6f 65 72 72 28 63 6e 74 29 3b 0a 20 20 7d 0a 20  oerr(cnt);.  }. 
13476 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43   sqlite3_free(zC
13477 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 4f 53 54  onverted);.  OST
13478 52 41 43 45 28 28 22 44 45 4c 45 54 45 20 5c 22  RACE(("DELETE \"
13479 25 73 5c 22 20 25 73 5c 6e 22 2c 20 7a 46 69 6c  %s\" %s\n", zFil
1347a 65 6e 61 6d 65 2c 20 28 72 63 20 3f 20 22 66 61  ename, (rc ? "fa
1347b 69 6c 65 64 22 20 3a 20 22 6f 6b 22 20 29 29 29  iled" : "ok" )))
1347c 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1347d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68  ../*.** Check th
1347e 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20  e existance and 
1347f 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65  status of a file
13480 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
13481 77 69 6e 41 63 63 65 73 73 28 0a 20 20 73 71 6c  winAccess(.  sql
13482 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
13483 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75          /* Not u
13484 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a  sed on win32 */.
13485 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
13486 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20  ilename,     /* 
13487 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Name of file to 
13488 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66  check */.  int f
13489 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
1348a 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66        /* Type of
1348b 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e   test to make on
1348c 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20   this file */.  
1348d 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20  int *pResOut    
1348e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
1348f 54 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a  T: Result */.){.
13490 20 20 44 57 4f 52 44 20 61 74 74 72 3b 0a 20 20    DWORD attr;.  
13491 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 44 57  int rc = 0;.  DW
13492 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 0a 20  ORD lastErrno;. 
13493 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65   void *zConverte
13494 64 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  d;.  UNUSED_PARA
13495 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 0a 20 20  METER(pVfs);..  
13496 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
13497 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
13498 4f 45 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0a  OERR_ACCESS; );.
13499 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63    zConverted = c
1349a 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61  onvertUtf8Filena
1349b 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  me(zFilename);. 
1349c 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d   if( zConverted=
1349d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
1349e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f   SQLITE_IOERR_NO
1349f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  MEM;.  }.  if( i
134a0 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 69 6e 74  sNT() ){.    int
134a1 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 57 49   cnt = 0;.    WI
134a2 4e 33 32 5f 46 49 4c 45 5f 41 54 54 52 49 42 55  N32_FILE_ATTRIBU
134a3 54 45 5f 44 41 54 41 20 73 41 74 74 72 44 61 74  TE_DATA sAttrDat
134a4 61 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73  a;.    memset(&s
134a5 41 74 74 72 44 61 74 61 2c 20 30 2c 20 73 69 7a  AttrData, 0, siz
134a6 65 6f 66 28 73 41 74 74 72 44 61 74 61 29 29 3b  eof(sAttrData));
134a7 0a 20 20 20 20 77 68 69 6c 65 28 20 21 28 72 63  .    while( !(rc
134a8 20 3d 20 6f 73 47 65 74 46 69 6c 65 41 74 74 72   = osGetFileAttr
134a9 69 62 75 74 65 73 45 78 57 28 28 4c 50 43 57 53  ibutesExW((LPCWS
134aa 54 52 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20  TR)zConverted,. 
134ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
134ac 20 20 20 20 20 20 20 20 20 20 20 20 47 65 74 46              GetF
134ad 69 6c 65 45 78 49 6e 66 6f 53 74 61 6e 64 61 72  ileExInfoStandar
134ae 64 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  d, .            
134af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
134b0 20 26 73 41 74 74 72 44 61 74 61 29 29 20 26 26   &sAttrData)) &&
134b1 20 72 65 74 72 79 49 6f 65 72 72 28 26 63 6e 74   retryIoerr(&cnt
134b2 2c 20 26 6c 61 73 74 45 72 72 6e 6f 29 20 29 7b  , &lastErrno) ){
134b3 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a  }.    if( rc ){.
134b4 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 6e 20        /* For an 
134b5 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
134b6 49 53 54 53 20 71 75 65 72 79 2c 20 74 72 65 61  ISTS query, trea
134b7 74 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20  t a zero-length 
134b8 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 73  file.      ** as
134b9 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20   if it does not 
134ba 65 78 69 73 74 2e 0a 20 20 20 20 20 20 2a 2f 0a  exist..      */.
134bb 20 20 20 20 20 20 69 66 28 20 20 20 20 66 6c 61        if(    fla
134bc 67 73 3d 3d 53 51 4c 49 54 45 5f 41 43 43 45 53  gs==SQLITE_ACCES
134bd 53 5f 45 58 49 53 54 53 0a 20 20 20 20 20 20 20  S_EXISTS.       
134be 20 20 20 26 26 20 73 41 74 74 72 44 61 74 61 2e     && sAttrData.
134bf 6e 46 69 6c 65 53 69 7a 65 48 69 67 68 3d 3d 30  nFileSizeHigh==0
134c0 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 73   .          && s
134c1 41 74 74 72 44 61 74 61 2e 6e 46 69 6c 65 53 69  AttrData.nFileSi
134c2 7a 65 4c 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20  zeLow==0 ){.    
134c3 20 20 20 20 61 74 74 72 20 3d 20 49 4e 56 41 4c      attr = INVAL
134c4 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54  ID_FILE_ATTRIBUT
134c5 45 53 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ES;.      }else{
134c6 0a 20 20 20 20 20 20 20 20 61 74 74 72 20 3d 20  .        attr = 
134c7 73 41 74 74 72 44 61 74 61 2e 64 77 46 69 6c 65  sAttrData.dwFile
134c8 41 74 74 72 69 62 75 74 65 73 3b 0a 20 20 20 20  Attributes;.    
134c9 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
134ca 20 20 20 20 20 6c 6f 67 49 6f 65 72 72 28 63 6e       logIoerr(cn
134cb 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 61  t);.      if( la
134cc 73 74 45 72 72 6e 6f 21 3d 45 52 52 4f 52 5f 46  stErrno!=ERROR_F
134cd 49 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44 20 26 26  ILE_NOT_FOUND &&
134ce 20 6c 61 73 74 45 72 72 6e 6f 21 3d 45 52 52 4f   lastErrno!=ERRO
134cf 52 5f 50 41 54 48 5f 4e 4f 54 5f 46 4f 55 4e 44  R_PATH_NOT_FOUND
134d0 20 29 7b 0a 20 20 20 20 20 20 20 20 77 69 6e 4c   ){.        winL
134d1 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 49  ogError(SQLITE_I
134d2 4f 45 52 52 5f 41 43 43 45 53 53 2c 20 6c 61 73  OERR_ACCESS, las
134d3 74 45 72 72 6e 6f 2c 20 22 77 69 6e 41 63 63 65  tErrno, "winAcce
134d4 73 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b  ss", zFilename);
134d5 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
134d6 5f 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64  _free(zConverted
134d7 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
134d8 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41  n SQLITE_IOERR_A
134d9 43 43 45 53 53 3b 0a 20 20 20 20 20 20 7d 65 6c  CCESS;.      }el
134da 73 65 7b 0a 20 20 20 20 20 20 20 20 61 74 74 72  se{.        attr
134db 20 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f   = INVALID_FILE_
134dc 41 54 54 52 49 42 55 54 45 53 3b 0a 20 20 20 20  ATTRIBUTES;.    
134dd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69    }.    }.  }.#i
134de 66 64 65 66 20 53 51 4c 49 54 45 5f 57 49 4e 33  fdef SQLITE_WIN3
134df 32 5f 48 41 53 5f 41 4e 53 49 0a 20 20 65 6c 73  2_HAS_ANSI.  els
134e0 65 7b 0a 20 20 20 20 61 74 74 72 20 3d 20 6f 73  e{.    attr = os
134e1 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65  GetFileAttribute
134e2 73 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65  sA((char*)zConve
134e3 72 74 65 64 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  rted);.  }.#endi
134e4 66 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  f.  sqlite3_free
134e5 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20  (zConverted);.  
134e6 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 7b  switch( flags ){
134e7 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
134e8 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20  _ACCESS_READ:.  
134e9 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43    case SQLITE_AC
134ea 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20  CESS_EXISTS:.   
134eb 20 20 20 72 63 20 3d 20 61 74 74 72 21 3d 49 4e     rc = attr!=IN
134ec 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49  VALID_FILE_ATTRI
134ed 42 55 54 45 53 3b 0a 20 20 20 20 20 20 62 72 65  BUTES;.      bre
134ee 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ak;.    case SQL
134ef 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57  ITE_ACCESS_READW
134f0 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d  RITE:.      rc =
134f1 20 61 74 74 72 21 3d 49 4e 56 41 4c 49 44 5f 46   attr!=INVALID_F
134f2 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 20 26  ILE_ATTRIBUTES &
134f3 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28  &.             (
134f4 61 74 74 72 20 26 20 46 49 4c 45 5f 41 54 54 52  attr & FILE_ATTR
134f5 49 42 55 54 45 5f 52 45 41 44 4f 4e 4c 59 29 3d  IBUTE_READONLY)=
134f6 3d 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  =0;.      break;
134f7 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20  .    default:.  
134f8 20 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76      assert(!"Inv
134f9 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d  alid flags argum
134fa 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52  ent");.  }.  *pR
134fb 65 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65  esOut = rc;.  re
134fc 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
134fd 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  }.../*.** Return
134fe 73 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68  s non-zero if th
134ff 65 20 73 70 65 63 69 66 69 65 64 20 70 61 74 68  e specified path
13500 20 6e 61 6d 65 20 73 68 6f 75 6c 64 20 62 65 20   name should be 
13501 75 73 65 64 20 76 65 72 62 61 74 69 6d 2e 20 20  used verbatim.  
13502 49 66 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 69  If.** non-zero i
13503 73 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20  s returned from 
13504 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74  this function, t
13505 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
13506 69 6f 6e 20 6d 75 73 74 20 73 69 6d 70 6c 79 0a  ion must simply.
13507 2a 2a 20 75 73 65 20 74 68 65 20 70 72 6f 76 69  ** use the provi
13508 64 65 64 20 70 61 74 68 20 6e 61 6d 65 20 76 65  ded path name ve
13509 72 62 61 74 69 6d 20 2d 4f 52 2d 20 72 65 73 6f  rbatim -OR- reso
1350a 6c 76 65 20 69 74 20 69 6e 74 6f 20 61 20 66 75  lve it into a fu
1350b 6c 6c 20 70 61 74 68 20 6e 61 6d 65 0a 2a 2a 20  ll path name.** 
1350c 75 73 69 6e 67 20 74 68 65 20 47 65 74 46 75 6c  using the GetFul
1350d 6c 50 61 74 68 4e 61 6d 65 20 57 69 6e 33 32 20  lPathName Win32 
1350e 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 28 69 66  API function (if
1350f 20 61 76 61 69 6c 61 62 6c 65 29 2e 0a 2a 2f 0a   available)..*/.
13510 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 49  static BOOL winI
13511 73 56 65 72 62 61 74 69 6d 50 61 74 68 6e 61 6d  sVerbatimPathnam
13512 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e(.  const char 
13513 2a 7a 50 61 74 68 6e 61 6d 65 0a 29 7b 0a 20 20  *zPathname.){.  
13514 2f 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 70  /*.  ** If the p
13515 61 74 68 20 6e 61 6d 65 20 73 74 61 72 74 73 20  ath name starts 
13516 77 69 74 68 20 61 20 66 6f 72 77 61 72 64 20 73  with a forward s
13517 6c 61 73 68 20 6f 72 20 61 20 62 61 63 6b 73 6c  lash or a backsl
13518 61 73 68 2c 20 69 74 20 69 73 20 65 69 74 68 65  ash, it is eithe
13519 72 0a 20 20 2a 2a 20 61 20 6c 65 67 61 6c 20 55  r.  ** a legal U
1351a 4e 43 20 6e 61 6d 65 2c 20 61 20 76 6f 6c 75 6d  NC name, a volum
1351b 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 2c  e relative path,
1351c 20 6f 72 20 61 6e 20 61 62 73 6f 6c 75 74 65 20   or an absolute 
1351d 70 61 74 68 20 6e 61 6d 65 20 69 6e 20 74 68 65  path name in the
1351e 0a 20 20 2a 2a 20 22 55 6e 69 78 22 20 66 6f 72  .  ** "Unix" for
1351f 6d 61 74 20 6f 6e 20 57 69 6e 64 6f 77 73 2e 20  mat on Windows. 
13520 20 54 68 65 72 65 20 69 73 20 6e 6f 20 65 61 73   There is no eas
13521 79 20 77 61 79 20 74 6f 20 64 69 66 66 65 72 65  y way to differe
13522 6e 74 69 61 74 65 20 62 65 74 77 65 65 6e 0a 20  ntiate between. 
13523 20 2a 2a 20 74 68 65 20 66 69 6e 61 6c 20 74 77   ** the final tw
13524 6f 20 63 61 73 65 73 3b 20 74 68 65 72 65 66 6f  o cases; therefo
13525 72 65 2c 20 77 65 20 72 65 74 75 72 6e 20 74 68  re, we return th
13526 65 20 73 61 66 65 72 20 72 65 74 75 72 6e 20 76  e safer return v
13527 61 6c 75 65 20 6f 66 20 54 52 55 45 0a 20 20 2a  alue of TRUE.  *
13528 2a 20 73 6f 20 74 68 61 74 20 63 61 6c 6c 65 72  * so that caller
13529 73 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69  s of this functi
1352a 6f 6e 20 77 69 6c 6c 20 73 69 6d 70 6c 79 20 75  on will simply u
1352b 73 65 20 69 74 20 76 65 72 62 61 74 69 6d 2e 0a  se it verbatim..
1352c 20 20 2a 2f 0a 20 20 69 66 20 28 20 7a 50 61 74    */.  if ( zPat
1352d 68 6e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 7c 7c  hname[0]=='/' ||
1352e 20 7a 50 61 74 68 6e 61 6d 65 5b 30 5d 3d 3d 27   zPathname[0]=='
1352f 5c 5c 27 20 29 7b 0a 20 20 20 20 72 65 74 75 72  \\' ){.    retur
13530 6e 20 54 52 55 45 3b 0a 20 20 7d 0a 0a 20 20 2f  n TRUE;.  }..  /
13531 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 70 61  *.  ** If the pa
13532 74 68 20 6e 61 6d 65 20 73 74 61 72 74 73 20 77  th name starts w
13533 69 74 68 20 61 20 6c 65 74 74 65 72 20 61 6e 64  ith a letter and
13534 20 61 20 63 6f 6c 6f 6e 20 69 74 20 69 73 20 65   a colon it is e
13535 69 74 68 65 72 20 61 20 76 6f 6c 75 6d 65 0a 20  ither a volume. 
13536 20 2a 2a 20 72 65 6c 61 74 69 76 65 20 70 61 74   ** relative pat
13537 68 20 6f 72 20 61 6e 20 61 62 73 6f 6c 75 74 65  h or an absolute
13538 20 70 61 74 68 2e 20 20 43 61 6c 6c 65 72 73 20   path.  Callers 
13539 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  of this function
1353a 20 6d 75 73 74 20 6e 6f 74 0a 20 20 2a 2a 20 61   must not.  ** a
1353b 74 74 65 6d 70 74 20 74 6f 20 74 72 65 61 74 20  ttempt to treat 
1353c 69 74 20 61 73 20 61 20 72 65 6c 61 74 69 76 65  it as a relative
1353d 20 70 61 74 68 20 6e 61 6d 65 20 28 69 2e 65 2e   path name (i.e.
1353e 20 74 68 65 79 20 73 68 6f 75 6c 64 20 73 69 6d   they should sim
1353f 70 6c 79 20 75 73 65 0a 20 20 2a 2a 20 69 74 20  ply use.  ** it 
13540 76 65 72 62 61 74 69 6d 29 2e 0a 20 20 2a 2f 0a  verbatim)..  */.
13541 20 20 69 66 20 28 20 73 71 6c 69 74 65 33 49 73    if ( sqlite3Is
13542 61 6c 70 68 61 28 7a 50 61 74 68 6e 61 6d 65 5b  alpha(zPathname[
13543 30 5d 29 20 26 26 20 7a 50 61 74 68 6e 61 6d 65  0]) && zPathname
13544 5b 31 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20  [1]==':' ){.    
13545 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 7d  return TRUE;.  }
13546 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49 66 20 77  ..  /*.  ** If w
13547 65 20 67 65 74 20 74 6f 20 74 68 69 73 20 70 6f  e get to this po
13548 69 6e 74 2c 20 74 68 65 20 70 61 74 68 20 6e 61  int, the path na
13549 6d 65 20 73 68 6f 75 6c 64 20 61 6c 6d 6f 73 74  me should almost
1354a 20 63 65 72 74 61 69 6e 6c 79 20 62 65 20 61 20   certainly be a 
1354b 70 75 72 65 6c 79 0a 20 20 2a 2a 20 72 65 6c 61  purely.  ** rela
1354c 74 69 76 65 20 6f 6e 65 20 28 69 2e 65 2e 20 6e  tive one (i.e. n
1354d 6f 74 20 61 20 55 4e 43 20 6e 61 6d 65 2c 20 6e  ot a UNC name, n
1354e 6f 74 20 61 62 73 6f 6c 75 74 65 2c 20 61 6e 64  ot absolute, and
1354f 20 6e 6f 74 20 76 6f 6c 75 6d 65 20 72 65 6c 61   not volume rela
13550 74 69 76 65 29 2e 0a 20 20 2a 2f 0a 20 20 72 65  tive)..  */.  re
13551 74 75 72 6e 20 46 41 4c 53 45 3b 0a 7d 0a 0a 2f  turn FALSE;.}../
13552 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61  *.** Turn a rela
13553 74 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e  tive pathname in
13554 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61  to a full pathna
13555 6d 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 66  me.  Write the f
13556 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20  ull.** pathname 
13557 69 6e 74 6f 20 7a 4f 75 74 5b 5d 2e 20 20 7a 4f  into zOut[].  zO
13558 75 74 5b 5d 20 77 69 6c 6c 20 62 65 20 61 74 20  ut[] will be at 
13559 6c 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 50 61  least pVfs->mxPa
1355a 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 73 20  thname.** bytes 
1355b 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74  in size..*/.stat
1355c 69 63 20 69 6e 74 20 77 69 6e 46 75 6c 6c 50 61  ic int winFullPa
1355d 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65  thname(.  sqlite
1355e 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20  3_vfs *pVfs,    
1355f 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
13560 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74  er to vfs object
13561 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
13562 20 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20   *zRelative,    
13563 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20      /* Possibly 
13564 72 65 6c 61 74 69 76 65 20 69 6e 70 75 74 20 70  relative input p
13565 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75  ath */.  int nFu
13566 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ll,             
13567 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
13568 66 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  f output buffer 
13569 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68  in bytes */.  ch
1356a 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20  ar *zFull       
1356b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1356c 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a  utput buffer */.
1356d 29 7b 0a 20 20 0a 23 69 66 20 64 65 66 69 6e 65  ){.  .#if define
1356e 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20  d(__CYGWIN__).  
1356f 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
13570 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
13571 52 52 4f 52 20 29 3b 0a 20 20 55 4e 55 53 45 44  RROR );.  UNUSED
13572 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c  _PARAMETER(nFull
13573 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 66  );.  assert( pVf
13574 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 3e 3d 4d  s->mxPathname>=M
13575 41 58 5f 50 41 54 48 20 29 3b 0a 20 20 61 73 73  AX_PATH );.  ass
13576 65 72 74 28 20 6e 46 75 6c 6c 3e 3d 70 56 66 73  ert( nFull>=pVfs
13577 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 29 3b 0a  ->mxPathname );.
13578 20 20 69 66 20 28 20 73 71 6c 69 74 65 33 5f 64    if ( sqlite3_d
13579 61 74 61 5f 64 69 72 65 63 74 6f 72 79 20 26 26  ata_directory &&
1357a 20 21 77 69 6e 49 73 56 65 72 62 61 74 69 6d 50   !winIsVerbatimP
1357b 61 74 68 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76  athname(zRelativ
1357c 65 29 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20  e) ){.    /*.   
1357d 20 2a 2a 20 4e 4f 54 45 3a 20 57 65 20 61 72 65   ** NOTE: We are
1357e 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20   dealing with a 
1357f 72 65 6c 61 74 69 76 65 20 70 61 74 68 20 6e 61  relative path na
13580 6d 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 0a  me and the data.
13581 20 20 20 20 2a 2a 20 20 20 20 20 20 20 64 69 72      **       dir
13582 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e 20  ectory has been 
13583 73 65 74 2e 20 20 54 68 65 72 65 66 6f 72 65 2c  set.  Therefore,
13584 20 75 73 65 20 69 74 20 61 73 20 74 68 65 20 62   use it as the b
13585 61 73 69 73 0a 20 20 20 20 2a 2a 20 20 20 20 20  asis.    **     
13586 20 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67    for converting
13587 20 74 68 65 20 72 65 6c 61 74 69 76 65 20 70 61   the relative pa
13588 74 68 20 6e 61 6d 65 20 74 6f 20 61 6e 20 61 62  th name to an ab
13589 73 6f 6c 75 74 65 0a 20 20 20 20 2a 2a 20 20 20  solute.    **   
1358a 20 20 20 20 6f 6e 65 20 62 79 20 70 72 65 70 65      one by prepe
1358b 6e 64 69 6e 67 20 74 68 65 20 64 61 74 61 20 64  nding the data d
1358c 69 72 65 63 74 6f 72 79 20 61 6e 64 20 61 20 73  irectory and a s
1358d 6c 61 73 68 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  lash..    */.   
1358e 20 63 68 61 72 20 7a 4f 75 74 5b 4d 41 58 5f 50   char zOut[MAX_P
1358f 41 54 48 2b 31 5d 3b 0a 20 20 20 20 6d 65 6d 73  ATH+1];.    mems
13590 65 74 28 7a 4f 75 74 2c 20 30 2c 20 4d 41 58 5f  et(zOut, 0, MAX_
13591 50 41 54 48 2b 31 29 3b 0a 20 20 20 20 63 79 67  PATH+1);.    cyg
13592 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 77 69 6e 33  win_conv_to_win3
13593 32 5f 70 61 74 68 28 7a 52 65 6c 61 74 69 76 65  2_path(zRelative
13594 2c 20 7a 4f 75 74 29 3b 20 2f 2a 20 50 4f 53 49  , zOut); /* POSI
13595 58 20 74 6f 20 57 69 6e 33 32 20 2a 2f 0a 20 20  X to Win32 */.  
13596 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
13597 74 66 28 4d 49 4e 28 6e 46 75 6c 6c 2c 20 70 56  tf(MIN(nFull, pV
13598 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 29 2c  fs->mxPathname),
13599 20 7a 46 75 6c 6c 2c 20 22 25 73 5c 5c 25 73 22   zFull, "%s\\%s"
1359a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1359b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64         sqlite3_d
1359c 61 74 61 5f 64 69 72 65 63 74 6f 72 79 2c 20 7a  ata_directory, z
1359d 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Out);.  }else{. 
1359e 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 4e 4f 54     /*.    ** NOT
1359f 45 3a 20 54 68 65 20 43 79 67 77 69 6e 20 64 6f  E: The Cygwin do
135a0 63 73 20 73 74 61 74 65 20 74 68 61 74 20 74 68  cs state that th
135a1 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  e maximum length
135a2 20 6e 65 65 64 65 64 0a 20 20 20 20 2a 2a 20 20   needed.    **  
135a3 20 20 20 20 20 66 6f 72 20 74 68 65 20 62 75 66       for the buf
135a4 66 65 72 20 70 61 73 73 65 64 20 74 6f 20 63 79  fer passed to cy
135a5 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75 6c  gwin_conv_to_ful
135a6 6c 5f 77 69 6e 33 32 5f 70 61 74 68 0a 20 20 20  l_win32_path.   
135a7 20 2a 2a 20 20 20 20 20 20 20 69 73 20 4d 41 58   **       is MAX
135a8 5f 50 41 54 48 2e 0a 20 20 20 20 2a 2f 0a 20 20  _PATH..    */.  
135a9 20 20 63 79 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f    cygwin_conv_to
135aa 5f 66 75 6c 6c 5f 77 69 6e 33 32 5f 70 61 74 68  _full_win32_path
135ab 28 7a 52 65 6c 61 74 69 76 65 2c 20 7a 46 75 6c  (zRelative, zFul
135ac 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  l);.  }.  return
135ad 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64   SQLITE_OK;.#end
135ae 69 66 0a 0a 23 69 66 20 28 53 51 4c 49 54 45 5f  if..#if (SQLITE_
135af 4f 53 5f 57 49 4e 43 45 20 7c 7c 20 53 51 4c 49  OS_WINCE || SQLI
135b0 54 45 5f 4f 53 5f 57 49 4e 52 54 29 20 26 26 20  TE_OS_WINRT) && 
135b1 21 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49  !defined(__CYGWI
135b2 4e 5f 5f 29 0a 20 20 53 69 6d 75 6c 61 74 65 49  N__).  SimulateI
135b3 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
135b4 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 20  QLITE_ERROR );. 
135b5 20 2f 2a 20 57 69 6e 43 45 20 68 61 73 20 6e 6f   /* WinCE has no
135b6 20 63 6f 6e 63 65 70 74 20 6f 66 20 61 20 72 65   concept of a re
135b7 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 2c  lative pathname,
135b8 20 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f 6c 64   or so I am told
135b9 2e 20 2a 2f 0a 20 20 2f 2a 20 57 69 6e 52 54 20  . */.  /* WinRT 
135ba 68 61 73 20 6e 6f 20 77 61 79 20 74 6f 20 63 6f  has no way to co
135bb 6e 76 65 72 74 20 61 20 72 65 6c 61 74 69 76 65  nvert a relative
135bc 20 70 61 74 68 20 74 6f 20 61 6e 20 61 62 73 6f   path to an abso
135bd 6c 75 74 65 20 6f 6e 65 2e 20 2a 2f 0a 20 20 69  lute one. */.  i
135be 66 20 28 20 73 71 6c 69 74 65 33 5f 64 61 74 61  f ( sqlite3_data
135bf 5f 64 69 72 65 63 74 6f 72 79 20 26 26 20 21 77  _directory && !w
135c0 69 6e 49 73 56 65 72 62 61 74 69 6d 50 61 74 68  inIsVerbatimPath
135c1 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76 65 29 20  name(zRelative) 
135c2 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  ){.    /*.    **
135c3 20 4e 4f 54 45 3a 20 57 65 20 61 72 65 20 64 65   NOTE: We are de
135c4 61 6c 69 6e 67 20 77 69 74 68 20 61 20 72 65 6c  aling with a rel
135c5 61 74 69 76 65 20 70 61 74 68 20 6e 61 6d 65 20  ative path name 
135c6 61 6e 64 20 74 68 65 20 64 61 74 61 0a 20 20 20  and the data.   
135c7 20 2a 2a 20 20 20 20 20 20 20 64 69 72 65 63 74   **       direct
135c8 6f 72 79 20 68 61 73 20 62 65 65 6e 20 73 65 74  ory has been set
135c9 2e 20 20 54 68 65 72 65 66 6f 72 65 2c 20 75 73  .  Therefore, us
135ca 65 20 69 74 20 61 73 20 74 68 65 20 62 61 73 69  e it as the basi
135cb 73 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 66  s.    **       f
135cc 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 74 68  or converting th
135cd 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 20  e relative path 
135ce 6e 61 6d 65 20 74 6f 20 61 6e 20 61 62 73 6f 6c  name to an absol
135cf 75 74 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ute.    **      
135d0 20 6f 6e 65 20 62 79 20 70 72 65 70 65 6e 64 69   one by prependi
135d1 6e 67 20 74 68 65 20 64 61 74 61 20 64 69 72 65  ng the data dire
135d2 63 74 6f 72 79 20 61 6e 64 20 61 20 62 61 63 6b  ctory and a back
135d3 73 6c 61 73 68 2e 0a 20 20 20 20 2a 2f 0a 20 20  slash..    */.  
135d4 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
135d5 74 66 28 4d 49 4e 28 6e 46 75 6c 6c 2c 20 70 56  tf(MIN(nFull, pV
135d6 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 29 2c  fs->mxPathname),
135d7 20 7a 46 75 6c 6c 2c 20 22 25 73 5c 5c 25 73 22   zFull, "%s\\%s"
135d8 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
135d9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64         sqlite3_d
135da 61 74 61 5f 64 69 72 65 63 74 6f 72 79 2c 20 7a  ata_directory, z
135db 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 7d 65 6c  Relative);.  }el
135dc 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
135dd 73 6e 70 72 69 6e 74 66 28 4d 49 4e 28 6e 46 75  snprintf(MIN(nFu
135de 6c 6c 2c 20 70 56 66 73 2d 3e 6d 78 50 61 74 68  ll, pVfs->mxPath
135df 6e 61 6d 65 29 2c 20 7a 46 75 6c 6c 2c 20 22 25  name), zFull, "%
135e0 73 22 2c 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a  s", zRelative);.
135e1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
135e2 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a  ITE_OK;.#endif..
135e3 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57  #if !SQLITE_OS_W
135e4 49 4e 43 45 20 26 26 20 21 53 51 4c 49 54 45 5f  INCE && !SQLITE_
135e5 4f 53 5f 57 49 4e 52 54 20 26 26 20 21 64 65 66  OS_WINRT && !def
135e6 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29  ined(__CYGWIN__)
135e7 0a 20 20 44 57 4f 52 44 20 6e 42 79 74 65 3b 0a  .  DWORD nByte;.
135e8 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74    void *zConvert
135e9 65 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74  ed;.  char *zOut
135ea 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  ;..  /* If this 
135eb 70 61 74 68 20 6e 61 6d 65 20 62 65 67 69 6e 73  path name begins
135ec 20 77 69 74 68 20 22 2f 58 3a 22 2c 20 77 68 65   with "/X:", whe
135ed 72 65 20 22 58 22 20 69 73 20 61 6e 79 20 61 6c  re "X" is any al
135ee 70 68 61 62 65 74 69 63 0a 20 20 2a 2a 20 63 68  phabetic.  ** ch
135ef 61 72 61 63 74 65 72 2c 20 64 69 73 63 61 72 64  aracter, discard
135f0 20 74 68 65 20 69 6e 69 74 69 61 6c 20 22 2f 22   the initial "/"
135f1 20 66 72 6f 6d 20 74 68 65 20 70 61 74 68 6e 61   from the pathna
135f2 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a  me..  */.  if( z
135f3 52 65 6c 61 74 69 76 65 5b 30 5d 3d 3d 27 2f 27  Relative[0]=='/'
135f4 20 26 26 20 73 71 6c 69 74 65 33 49 73 61 6c 70   && sqlite3Isalp
135f5 68 61 28 7a 52 65 6c 61 74 69 76 65 5b 31 5d 29  ha(zRelative[1])
135f6 20 26 26 20 7a 52 65 6c 61 74 69 76 65 5b 32 5d   && zRelative[2]
135f7 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 7a 52 65  ==':' ){.    zRe
135f8 6c 61 74 69 76 65 2b 2b 3b 0a 20 20 7d 0a 0a 20  lative++;.  }.. 
135f9 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20   /* It's odd to 
135fa 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65  simulate an io-e
135fb 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72  rror here, but r
135fc 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75  eally this is ju
135fd 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68  st.  ** using th
135fe 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61  e io-error infra
135ff 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 65 73  structure to tes
13600 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 68 61  t that SQLite ha
13601 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20  ndles this.  ** 
13602 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67  function failing
13603 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
13604 63 6f 75 6c 64 20 66 61 69 6c 20 69 66 2c 20 66  could fail if, f
13605 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 0a  or example, the.
13606 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 77 6f 72    ** current wor
13607 6b 69 6e 67 20 64 69 72 65 63 74 6f 72 79 20 68  king directory h
13608 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b 65 64  as been unlinked
13609 2e 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74  ..  */.  Simulat
1360a 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e  eIOError( return
1360b 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b   SQLITE_ERROR );
1360c 0a 20 20 69 66 20 28 20 73 71 6c 69 74 65 33 5f  .  if ( sqlite3_
1360d 64 61 74 61 5f 64 69 72 65 63 74 6f 72 79 20 26  data_directory &
1360e 26 20 21 77 69 6e 49 73 56 65 72 62 61 74 69 6d  & !winIsVerbatim
1360f 50 61 74 68 6e 61 6d 65 28 7a 52 65 6c 61 74 69  Pathname(zRelati
13610 76 65 29 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20  ve) ){.    /*.  
13611 20 20 2a 2a 20 4e 4f 54 45 3a 20 57 65 20 61 72    ** NOTE: We ar
13612 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61  e dealing with a
13613 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 20 6e   relative path n
13614 61 6d 65 20 61 6e 64 20 74 68 65 20 64 61 74 61  ame and the data
13615 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 64 69  .    **       di
13616 72 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e  rectory has been
13617 20 73 65 74 2e 20 20 54 68 65 72 65 66 6f 72 65   set.  Therefore
13618 2c 20 75 73 65 20 69 74 20 61 73 20 74 68 65 20  , use it as the 
13619 62 61 73 69 73 0a 20 20 20 20 2a 2a 20 20 20 20  basis.    **    
1361a 20 20 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e     for convertin
1361b 67 20 74 68 65 20 72 65 6c 61 74 69 76 65 20 70  g the relative p
1361c 61 74 68 20 6e 61 6d 65 20 74 6f 20 61 6e 20 61  ath name to an a
1361d 62 73 6f 6c 75 74 65 0a 20 20 20 20 2a 2a 20 20  bsolute.    **  
1361e 20 20 20 20 20 6f 6e 65 20 62 79 20 70 72 65 70       one by prep
1361f 65 6e 64 69 6e 67 20 74 68 65 20 64 61 74 61 20  ending the data 
13620 64 69 72 65 63 74 6f 72 79 20 61 6e 64 20 61 20  directory and a 
13621 62 61 63 6b 73 6c 61 73 68 2e 0a 20 20 20 20 2a  backslash..    *
13622 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  /.    sqlite3_sn
13623 70 72 69 6e 74 66 28 4d 49 4e 28 6e 46 75 6c 6c  printf(MIN(nFull
13624 2c 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  , pVfs->mxPathna
13625 6d 65 29 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 5c  me), zFull, "%s\
13626 5c 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20  \%s",.          
13627 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
13628 65 33 5f 64 61 74 61 5f 64 69 72 65 63 74 6f 72  e3_data_director
13629 79 2c 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20  y, zRelative);. 
1362a 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1362b 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 7a 43 6f 6e 76  _OK;.  }.  zConv
1362c 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55  erted = convertU
1362d 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 52 65 6c  tf8Filename(zRel
1362e 61 74 69 76 65 29 3b 0a 20 20 69 66 28 20 7a 43  ative);.  if( zC
1362f 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20  onverted==0 ){. 
13630 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
13631 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20  _IOERR_NOMEM;.  
13632 7d 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29  }.  if( isNT() )
13633 7b 0a 20 20 20 20 4c 50 57 53 54 52 20 7a 54 65  {.    LPWSTR zTe
13634 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20  mp;.    nByte = 
13635 6f 73 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d  osGetFullPathNam
13636 65 57 28 28 4c 50 43 57 53 54 52 29 7a 43 6f 6e  eW((LPCWSTR)zCon
13637 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 29  verted, 0, 0, 0)
13638 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d  ;.    if( nByte=
13639 3d 30 20 29 7b 0a 20 20 20 20 20 20 77 69 6e 4c  =0 ){.      winL
1363a 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 45  ogError(SQLITE_E
1363b 52 52 4f 52 2c 20 6f 73 47 65 74 4c 61 73 74 45  RROR, osGetLastE
1363c 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20 20  rror(),.        
1363d 20 20 20 20 20 20 20 20 20 20 22 47 65 74 46 75            "GetFu
1363e 6c 6c 50 61 74 68 4e 61 6d 65 57 31 22 2c 20 7a  llPathNameW1", z
1363f 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
13640 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
13641 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
13642 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
13643 43 41 4e 54 4f 50 45 4e 5f 46 55 4c 4c 50 41 54  CANTOPEN_FULLPAT
13644 48 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 42 79  H;.    }.    nBy
13645 74 65 20 2b 3d 20 33 3b 0a 20 20 20 20 7a 54 65  te += 3;.    zTe
13646 6d 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  mp = sqlite3Mall
13647 6f 63 5a 65 72 6f 28 20 6e 42 79 74 65 2a 73 69  ocZero( nByte*si
13648 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29  zeof(zTemp[0]) )
13649 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d  ;.    if( zTemp=
1364a 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
1364b 74 65 33 5f 66 72 65 65 28 7a 43 6f 6e 76 65 72  te3_free(zConver
1364c 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ted);.      retu
1364d 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
1364e 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20  NOMEM;.    }.   
1364f 20 6e 42 79 74 65 20 3d 20 6f 73 47 65 74 46 75   nByte = osGetFu
13650 6c 6c 50 61 74 68 4e 61 6d 65 57 28 28 4c 50 43  llPathNameW((LPC
13651 57 53 54 52 29 7a 43 6f 6e 76 65 72 74 65 64 2c  WSTR)zConverted,
13652 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30   nByte, zTemp, 0
13653 29 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65  );.    if( nByte
13654 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 77 69 6e  ==0 ){.      win
13655 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f  LogError(SQLITE_
13656 45 52 52 4f 52 2c 20 6f 73 47 65 74 4c 61 73 74  ERROR, osGetLast
13657 45 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20  Error(),.       
13658 20 20 20 20 20 20 20 20 20 20 20 22 47 65 74 46             "GetF
13659 75 6c 6c 50 61 74 68 4e 61 6d 65 57 32 22 2c 20  ullPathNameW2", 
1365a 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20  zConverted);.   
1365b 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1365c 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20  zConverted);.   
1365d 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1365e 7a 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 72 65  zTemp);.      re
1365f 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54  turn SQLITE_CANT
13660 4f 50 45 4e 5f 46 55 4c 4c 50 41 54 48 3b 0a 20  OPEN_FULLPATH;. 
13661 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
13662 5f 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64  _free(zConverted
13663 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75 6e  );.    zOut = un
13664 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 6d  icodeToUtf8(zTem
13665 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  p);.    sqlite3_
13666 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20 7d  free(zTemp);.  }
13667 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 57  .#ifdef SQLITE_W
13668 49 4e 33 32 5f 48 41 53 5f 41 4e 53 49 0a 20 20  IN32_HAS_ANSI.  
13669 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a  else{.    char *
1366a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65  zTemp;.    nByte
1366b 20 3d 20 6f 73 47 65 74 46 75 6c 6c 50 61 74 68   = osGetFullPath
1366c 4e 61 6d 65 41 28 28 63 68 61 72 2a 29 7a 43 6f  NameA((char*)zCo
1366d 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30  nverted, 0, 0, 0
1366e 29 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65  );.    if( nByte
1366f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 77 69 6e  ==0 ){.      win
13670 4c 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f  LogError(SQLITE_
13671 45 52 52 4f 52 2c 20 6f 73 47 65 74 4c 61 73 74  ERROR, osGetLast
13672 45 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20  Error(),.       
13673 20 20 20 20 20 20 20 20 20 20 20 22 47 65 74 46             "GetF
13674 75 6c 6c 50 61 74 68 4e 61 6d 65 41 31 22 2c 20  ullPathNameA1", 
13675 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20  zConverted);.   
13676 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
13677 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20  zConverted);.   
13678 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
13679 5f 43 41 4e 54 4f 50 45 4e 5f 46 55 4c 4c 50 41  _CANTOPEN_FULLPA
1367a 54 48 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 42  TH;.    }.    nB
1367b 79 74 65 20 2b 3d 20 33 3b 0a 20 20 20 20 7a 54  yte += 3;.    zT
1367c 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  emp = sqlite3Mal
1367d 6c 6f 63 5a 65 72 6f 28 20 6e 42 79 74 65 2a 73  locZero( nByte*s
1367e 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20  izeof(zTemp[0]) 
1367f 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70  );.    if( zTemp
13680 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
13681 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6e 76 65  ite3_free(zConve
13682 72 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74  rted);.      ret
13683 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
13684 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
13685 20 20 6e 42 79 74 65 20 3d 20 6f 73 47 65 74 46    nByte = osGetF
13686 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68  ullPathNameA((ch
13687 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20  ar*)zConverted, 
13688 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29  nByte, zTemp, 0)
13689 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d  ;.    if( nByte=
1368a 3d 30 20 29 7b 0a 20 20 20 20 20 20 77 69 6e 4c  =0 ){.      winL
1368b 6f 67 45 72 72 6f 72 28 53 51 4c 49 54 45 5f 45  ogError(SQLITE_E
1368c 52 52 4f 52 2c 20 6f 73 47 65 74 4c 61 73 74 45  RROR, osGetLastE
1368d 72 72 6f 72 28 29 2c 0a 20 20 20 20 20 20 20 20  rror(),.        
1368e 20 20 20 20 20 20 20 20 20 20 22 47 65 74 46 75            "GetFu
1368f 6c 6c 50 61 74 68 4e 61 6d 65 41 32 22 2c 20 7a  llPathNameA2", z
13690 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
13691 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
13692 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
13693 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
13694 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 72 65 74  Temp);.      ret
13695 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  urn SQLITE_CANTO
13696 50 45 4e 5f 46 55 4c 4c 50 41 54 48 3b 0a 20 20  PEN_FULLPATH;.  
13697 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
13698 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29  free(zConverted)
13699 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c  ;.    zOut = sql
1369a 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63 73 5f  ite3_win32_mbcs_
1369b 74 6f 5f 75 74 66 38 28 7a 54 65 6d 70 29 3b 0a  to_utf8(zTemp);.
1369c 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1369d 28 7a 54 65 6d 70 29 3b 0a 20 20 7d 0a 23 65 6e  (zTemp);.  }.#en
1369e 64 69 66 0a 20 20 69 66 28 20 7a 4f 75 74 20 29  dif.  if( zOut )
1369f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
136a0 70 72 69 6e 74 66 28 4d 49 4e 28 6e 46 75 6c 6c  printf(MIN(nFull
136a1 2c 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  , pVfs->mxPathna
136a2 6d 65 29 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22  me), zFull, "%s"
136a3 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 73 71 6c  , zOut);.    sql
136a4 69 74 65 33 5f 66 72 65 65 28 7a 4f 75 74 29 3b  ite3_free(zOut);
136a5 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
136a6 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  TE_OK;.  }else{.
136a7 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
136a8 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20  E_IOERR_NOMEM;. 
136a9 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 23 69 66   }.#endif.}..#if
136aa 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
136ab 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a  _LOAD_EXTENSION.
136ac 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73  /*.** Interfaces
136ad 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73   for opening a s
136ae 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66  hared library, f
136af 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69  inding entry poi
136b0 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68  nts.** within th
136b1 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  e shared library
136b2 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68  , and closing th
136b3 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  e shared library
136b4 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72  ..*/./*.** Inter
136b5 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e  faces for openin
136b6 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61  g a shared libra
136b7 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72  ry, finding entr
136b8 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68  y points.** with
136b9 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  in the shared li
136ba 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69  brary, and closi
136bb 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  ng the shared li
136bc 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  brary..*/.static
136bd 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f 70 65 6e   void *winDlOpen
136be 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
136bf 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  fs, const char *
136c0 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 48 41  zFilename){.  HA
136c1 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69 64 20 2a  NDLE h;.  void *
136c2 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e  zConverted = con
136c3 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65  vertUtf8Filename
136c4 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55  (zFilename);.  U
136c5 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
136c6 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f  pVfs);.  if( zCo
136c7 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20  nverted==0 ){.  
136c8 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
136c9 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a    if( isNT() ){.
136ca 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
136cb 4e 52 54 0a 20 20 20 20 68 20 3d 20 6f 73 4c 6f  NRT.    h = osLo
136cc 61 64 50 61 63 6b 61 67 65 64 4c 69 62 72 61 72  adPackagedLibrar
136cd 79 28 28 4c 50 43 57 53 54 52 29 7a 43 6f 6e 76  y((LPCWSTR)zConv
136ce 65 72 74 65 64 2c 20 30 29 3b 0a 23 65 6c 73 65  erted, 0);.#else
136cf 0a 20 20 20 20 68 20 3d 20 6f 73 4c 6f 61 64 4c  .    h = osLoadL
136d0 69 62 72 61 72 79 57 28 28 4c 50 43 57 53 54 52  ibraryW((LPCWSTR
136d1 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65  )zConverted);.#e
136d2 6e 64 69 66 0a 20 20 7d 0a 23 69 66 64 65 66 20  ndif.  }.#ifdef 
136d3 53 51 4c 49 54 45 5f 57 49 4e 33 32 5f 48 41 53  SQLITE_WIN32_HAS
136d4 5f 41 4e 53 49 0a 20 20 65 6c 73 65 7b 0a 20 20  _ANSI.  else{.  
136d5 20 20 68 20 3d 20 6f 73 4c 6f 61 64 4c 69 62 72    h = osLoadLibr
136d6 61 72 79 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e  aryA((char*)zCon
136d7 76 65 72 74 65 64 29 3b 0a 20 20 7d 0a 23 65 6e  verted);.  }.#en
136d8 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  dif.  sqlite3_fr
136d9 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
136da 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
136db 68 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  h;.}.static void
136dc 20 77 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c 69   winDlError(sqli
136dd 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
136de 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
136df 42 75 66 4f 75 74 29 7b 0a 20 20 55 4e 55 53 45  BufOut){.  UNUSE
136e0 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73  D_PARAMETER(pVfs
136e1 29 3b 0a 20 20 67 65 74 4c 61 73 74 45 72 72 6f  );.  getLastErro
136e2 72 4d 73 67 28 6f 73 47 65 74 4c 61 73 74 45 72  rMsg(osGetLastEr
136e3 72 6f 72 28 29 2c 20 6e 42 75 66 2c 20 7a 42 75  ror(), nBuf, zBu
136e4 66 4f 75 74 29 3b 0a 7d 0a 73 74 61 74 69 63 20  fOut);.}.static 
136e5 76 6f 69 64 20 28 2a 77 69 6e 44 6c 53 79 6d 28  void (*winDlSym(
136e6 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
136e7 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65  s, void *pHandle
136e8 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  , const char *zS
136e9 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 7b 0a 20  ymbol))(void){. 
136ea 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
136eb 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 72  R(pVfs);.  retur
136ec 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 29  n (void(*)(void)
136ed 29 6f 73 47 65 74 50 72 6f 63 41 64 64 72 65 73  )osGetProcAddres
136ee 73 41 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64  sA((HANDLE)pHand
136ef 6c 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 7d 0a  le, zSymbol);.}.
136f0 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 44  static void winD
136f1 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76  lClose(sqlite3_v
136f2 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a  fs *pVfs, void *
136f3 70 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53  pHandle){.  UNUS
136f4 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66  ED_PARAMETER(pVf
136f5 73 29 3b 0a 20 20 6f 73 46 72 65 65 4c 69 62 72  s);.  osFreeLibr
136f6 61 72 79 28 28 48 41 4e 44 4c 45 29 70 48 61 6e  ary((HANDLE)pHan
136f7 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a  dle);.}.#else /*
136f8 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   if SQLITE_OMIT_
136f9 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69  LOAD_EXTENSION i
136fa 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20  s defined: */.  
136fb 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 4f 70 65  #define winDlOpe
136fc 6e 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77  n  0.  #define w
136fd 69 6e 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 64  inDlError 0.  #d
136fe 65 66 69 6e 65 20 77 69 6e 44 6c 53 79 6d 20 20  efine winDlSym  
136ff 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e   0.  #define win
13700 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66  DlClose 0.#endif
13701 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75  .../*.** Write u
13702 70 20 74 6f 20 6e 42 75 66 20 62 79 74 65 73 20  p to nBuf bytes 
13703 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e  of randomness in
13704 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74  to zBuf..*/.stat
13705 69 63 20 69 6e 74 20 77 69 6e 52 61 6e 64 6f 6d  ic int winRandom
13706 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73  ness(sqlite3_vfs
13707 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66   *pVfs, int nBuf
13708 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20  , char *zBuf){. 
13709 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 55 4e   int n = 0;.  UN
1370a 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
1370b 56 66 73 29 3b 0a 23 69 66 20 64 65 66 69 6e 65  Vfs);.#if define
1370c 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20  d(SQLITE_TEST). 
1370d 20 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d   n = nBuf;.  mem
1370e 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75  set(zBuf, 0, nBu
1370f 66 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20  f);.#else.  if( 
13710 73 69 7a 65 6f 66 28 53 59 53 54 45 4d 54 49 4d  sizeof(SYSTEMTIM
13711 45 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20  E)<=nBuf-n ){.  
13712 20 20 53 59 53 54 45 4d 54 49 4d 45 20 78 3b 0a    SYSTEMTIME x;.
13713 20 20 20 20 6f 73 47 65 74 53 79 73 74 65 6d 54      osGetSystemT
13714 69 6d 65 28 26 78 29 3b 0a 20 20 20 20 6d 65 6d  ime(&x);.    mem
13715 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 78  cpy(&zBuf[n], &x
13716 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20  , sizeof(x));.  
13717 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 78 29    n += sizeof(x)
13718 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65  ;.  }.  if( size
13719 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d  of(DWORD)<=nBuf-
1371a 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 70  n ){.    DWORD p
1371b 69 64 20 3d 20 6f 73 47 65 74 43 75 72 72 65 6e  id = osGetCurren
1371c 74 50 72 6f 63 65 73 73 49 64 28 29 3b 0a 20 20  tProcessId();.  
1371d 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
1371e 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66 28  ], &pid, sizeof(
1371f 70 69 64 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  pid));.    n += 
13720 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a 20 20 7d  sizeof(pid);.  }
13721 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
13722 49 4e 52 54 0a 20 20 69 66 28 20 73 69 7a 65 6f  INRT.  if( sizeo
13723 66 28 55 4c 4f 4e 47 4c 4f 4e 47 29 3c 3d 6e 42  f(ULONGLONG)<=nB
13724 75 66 2d 6e 20 29 7b 0a 20 20 20 20 55 4c 4f 4e  uf-n ){.    ULON
13725 47 4c 4f 4e 47 20 63 6e 74 20 3d 20 6f 73 47 65  GLONG cnt = osGe
13726 74 54 69 63 6b 43 6f 75 6e 74 36 34 28 29 3b 0a  tTickCount64();.
13727 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66      memcpy(&zBuf
13728 5b 6e 5d 2c 20 26 63 6e 74 2c 20 73 69 7a 65 6f  [n], &cnt, sizeo
13729 66 28 63 6e 74 29 29 3b 0a 20 20 20 20 6e 20 2b  f(cnt));.    n +
1372a 3d 20 73 69 7a 65 6f 66 28 63 6e 74 29 3b 0a 20  = sizeof(cnt);. 
1372b 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 73   }.#else.  if( s
1372c 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42  izeof(DWORD)<=nB
1372d 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52  uf-n ){.    DWOR
1372e 44 20 63 6e 74 20 3d 20 6f 73 47 65 74 54 69 63  D cnt = osGetTic
1372f 6b 43 6f 75 6e 74 28 29 3b 0a 20 20 20 20 6d 65  kCount();.    me
13730 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
13731 63 6e 74 2c 20 73 69 7a 65 6f 66 28 63 6e 74 29  cnt, sizeof(cnt)
13732 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  );.    n += size
13733 6f 66 28 63 6e 74 29 3b 0a 20 20 7d 0a 23 65 6e  of(cnt);.  }.#en
13734 64 69 66 0a 20 20 69 66 28 20 73 69 7a 65 6f 66  dif.  if( sizeof
13735 28 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 29 3c  (LARGE_INTEGER)<
13736 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 4c  =nBuf-n ){.    L
13737 41 52 47 45 5f 49 4e 54 45 47 45 52 20 69 3b 0a  ARGE_INTEGER i;.
13738 20 20 20 20 6f 73 51 75 65 72 79 50 65 72 66 6f      osQueryPerfo
13739 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 28 26 69  rmanceCounter(&i
1373a 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  );.    memcpy(&z
1373b 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73 69 7a 65  Buf[n], &i, size
1373c 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e 20 2b 3d  of(i));.    n +=
1373d 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20 20 7d 0a   sizeof(i);.  }.
1373e 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
1373f 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65  n;.}.../*.** Sle
13740 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20  ep for a little 
13741 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74  while.  Return t
13742 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d  he amount of tim
13743 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 73 74 61 74  e slept..*/.stat
13744 69 63 20 69 6e 74 20 77 69 6e 53 6c 65 65 70 28  ic int winSleep(
13745 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
13746 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 29  s, int microsec)
13747 7b 0a 20 20 73 71 6c 69 74 65 33 5f 77 69 6e 33  {.  sqlite3_win3
13748 32 5f 73 6c 65 65 70 28 28 6d 69 63 72 6f 73 65  2_sleep((microse
13749 63 2b 39 39 39 29 2f 31 30 30 30 29 3b 0a 20 20  c+999)/1000);.  
1374a 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
1374b 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 72 6e  (pVfs);.  return
1374c 20 28 28 6d 69 63 72 6f 73 65 63 2b 39 39 39 29   ((microsec+999)
1374d 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a 7d 0a 0a  /1000)*1000;.}..
1374e 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
1374f 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66  ing variable, if
13750 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65   set to a non-ze
13751 72 6f 20 76 61 6c 75 65 2c 20 69 73 20 69 6e 74  ro value, is int
13752 65 72 70 72 65 74 65 64 20 61 73 0a 2a 2a 20 74  erpreted as.** t
13753 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 63  he number of sec
13754 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 20  onds since 1970 
13755 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 73  and is used to s
13756 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  et the result of
13757 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 43 75 72  .** sqlite3OsCur
13758 72 65 6e 74 54 69 6d 65 28 29 20 64 75 72 69 6e  rentTime() durin
13759 67 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69  g testing..*/.#i
1375a 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
1375b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1375c 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f  sqlite3_current_
1375d 74 69 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61  time = 0;  /* Fa
1375e 6b 65 20 73 79 73 74 65 6d 20 74 69 6d 65 20 69  ke system time i
1375f 6e 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20  n seconds since 
13760 31 39 37 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a  1970. */.#endif.
13761 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20  ./*.** Find the 
13762 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e  current time (in
13763 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64   Universal Coord
13764 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57  inated Time).  W
13765 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 4e 6f 77  rite into *piNow
13766 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20  .** the current 
13767 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73  time and date as
13768 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75   a Julian Day nu
13769 6d 62 65 72 20 74 69 6d 65 73 20 38 36 5f 34 30  mber times 86_40
1376a 30 5f 30 30 30 2e 20 20 49 6e 0a 2a 2a 20 6f 74  0_000.  In.** ot
1376b 68 65 72 20 77 6f 72 64 73 2c 20 77 72 69 74 65  her words, write
1376c 20 69 6e 74 6f 20 2a 70 69 4e 6f 77 20 74 68 65   into *piNow the
1376d 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69   number of milli
1376e 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 74 68  seconds since th
1376f 65 20 4a 75 6c 69 61 6e 0a 2a 2a 20 65 70 6f 63  e Julian.** epoc
13770 68 20 6f 66 20 6e 6f 6f 6e 20 69 6e 20 47 72 65  h of noon in Gre
13771 65 6e 77 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62  enwich on Novemb
13772 65 72 20 32 34 2c 20 34 37 31 34 20 42 2e 43 20  er 24, 4714 B.C 
13773 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
13774 0a 2a 2a 20 70 72 6f 6c 65 70 74 69 63 20 47 72  .** proleptic Gr
13775 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72  egorian calendar
13776 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65  ..**.** On succe
13777 73 73 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ss, return SQLIT
13778 45 5f 4f 4b 2e 20 20 52 65 74 75 72 6e 20 53 51  E_OK.  Return SQ
13779 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68  LITE_ERROR if th
1377a 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20  e time and date 
1377b 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f  .** cannot be fo
1377c 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  und..*/.static i
1377d 6e 74 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d  nt winCurrentTim
1377e 65 49 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76  eInt64(sqlite3_v
1377f 66 73 20 2a 70 56 66 73 2c 20 73 71 6c 69 74 65  fs *pVfs, sqlite
13780 33 5f 69 6e 74 36 34 20 2a 70 69 4e 6f 77 29 7b  3_int64 *piNow){
13781 0a 20 20 2f 2a 20 46 49 4c 45 54 49 4d 45 20 73  .  /* FILETIME s
13782 74 72 75 63 74 75 72 65 20 69 73 20 61 20 36 34  tructure is a 64
13783 2d 62 69 74 20 76 61 6c 75 65 20 72 65 70 72 65  -bit value repre
13784 73 65 6e 74 69 6e 67 20 74 68 65 20 6e 75 6d 62  senting the numb
13785 65 72 20 6f 66 20 0a 20 20 20 20 20 31 30 30 2d  er of .     100-
13786 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 65 72  nanosecond inter
13787 76 61 6c 73 20 73 69 6e 63 65 20 4a 61 6e 75 61  vals since Janua
13788 72 79 20 31 2c 20 31 36 30 31 20 28 3d 20 4a 44  ry 1, 1601 (= JD
13789 20 32 33 30 35 38 31 33 2e 35 29 2e 20 0a 20 20   2305813.5). .  
1378a 2a 2f 0a 20 20 46 49 4c 45 54 49 4d 45 20 66 74  */.  FILETIME ft
1378b 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
1378c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 77   sqlite3_int64 w
1378d 69 6e 46 69 6c 65 74 69 6d 65 45 70 6f 63 68 20  inFiletimeEpoch 
1378e 3d 20 32 33 30 35 38 31 33 35 2a 28 73 71 6c 69  = 23058135*(sqli
1378f 74 65 33 5f 69 6e 74 36 34 29 38 36 34 30 30 30  te3_int64)864000
13790 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  0;.#ifdef SQLITE
13791 5f 54 45 53 54 0a 20 20 73 74 61 74 69 63 20 63  _TEST.  static c
13792 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74  onst sqlite3_int
13793 36 34 20 75 6e 69 78 45 70 6f 63 68 20 3d 20 32  64 unixEpoch = 2
13794 34 34 30 35 38 37 35 2a 28 73 71 6c 69 74 65 33  4405875*(sqlite3
13795 5f 69 6e 74 36 34 29 38 36 34 30 30 30 30 3b 0a  _int64)8640000;.
13796 23 65 6e 64 69 66 0a 20 20 2f 2a 20 32 5e 33 32  #endif.  /* 2^32
13797 20 2d 20 74 6f 20 61 76 6f 69 64 20 75 73 65 20   - to avoid use 
13798 6f 66 20 4c 4c 20 61 6e 64 20 77 61 72 6e 69 6e  of LL and warnin
13799 67 73 20 69 6e 20 67 63 63 20 2a 2f 0a 20 20 73  gs in gcc */.  s
1379a 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
1379b 74 65 33 5f 69 6e 74 36 34 20 6d 61 78 33 32 42  te3_int64 max32B
1379c 69 74 56 61 6c 75 65 20 3d 20 0a 20 20 20 20 20  itValue = .     
1379d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
1379e 32 30 30 30 30 30 30 30 30 30 20 2b 20 28 73 71  2000000000 + (sq
1379f 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 30 30 30  lite3_int64)2000
137a0 30 30 30 30 30 30 20 2b 20 28 73 71 6c 69 74 65  000000 + (sqlite
137a1 33 5f 69 6e 74 36 34 29 32 39 34 39 36 37 32 39  3_int64)29496729
137a2 36 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  6;..#if SQLITE_O
137a3 53 5f 57 49 4e 43 45 0a 20 20 53 59 53 54 45 4d  S_WINCE.  SYSTEM
137a4 54 49 4d 45 20 74 69 6d 65 3b 0a 20 20 6f 73 47  TIME time;.  osG
137a5 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 74 69  etSystemTime(&ti
137a6 6d 65 29 3b 0a 20 20 2f 2a 20 69 66 20 53 79 73  me);.  /* if Sys
137a7 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d  temTimeToFileTim
137a8 65 28 29 20 66 61 69 6c 73 2c 20 69 74 20 72 65  e() fails, it re
137a9 74 75 72 6e 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20  turns zero. */. 
137aa 20 69 66 20 28 21 6f 73 53 79 73 74 65 6d 54 69   if (!osSystemTi
137ab 6d 65 54 6f 46 69 6c 65 54 69 6d 65 28 26 74 69  meToFileTime(&ti
137ac 6d 65 2c 26 66 74 29 29 7b 0a 20 20 20 20 72 65  me,&ft)){.    re
137ad 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
137ae 52 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 6f  R;.  }.#else.  o
137af 73 47 65 74 53 79 73 74 65 6d 54 69 6d 65 41 73  sGetSystemTimeAs
137b0 46 69 6c 65 54 69 6d 65 28 20 26 66 74 20 29 3b  FileTime( &ft );
137b1 0a 23 65 6e 64 69 66 0a 0a 20 20 2a 70 69 4e 6f  .#endif..  *piNo
137b2 77 20 3d 20 77 69 6e 46 69 6c 65 74 69 6d 65 45  w = winFiletimeE
137b3 70 6f 63 68 20 2b 0a 20 20 20 20 20 20 20 20 20  poch +.         
137b4 20 20 20 28 28 28 28 73 71 6c 69 74 65 33 5f 69     ((((sqlite3_i
137b5 6e 74 36 34 29 66 74 2e 64 77 48 69 67 68 44 61  nt64)ft.dwHighDa
137b6 74 65 54 69 6d 65 29 2a 6d 61 78 33 32 42 69 74  teTime)*max32Bit
137b7 56 61 6c 75 65 29 20 2b 20 0a 20 20 20 20 20 20  Value) + .      
137b8 20 20 20 20 20 20 20 20 20 28 73 71 6c 69 74 65           (sqlite
137b9 33 5f 69 6e 74 36 34 29 66 74 2e 64 77 4c 6f 77  3_int64)ft.dwLow
137ba 44 61 74 65 54 69 6d 65 29 2f 28 73 71 6c 69 74  DateTime)/(sqlit
137bb 65 33 5f 69 6e 74 36 34 29 31 30 30 30 30 3b 0a  e3_int64)10000;.
137bc 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
137bd 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65  EST.  if( sqlite
137be 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29  3_current_time )
137bf 7b 0a 20 20 20 20 2a 70 69 4e 6f 77 20 3d 20 31  {.    *piNow = 1
137c0 30 30 30 2a 28 73 71 6c 69 74 65 33 5f 69 6e 74  000*(sqlite3_int
137c1 36 34 29 73 71 6c 69 74 65 33 5f 63 75 72 72 65  64)sqlite3_curre
137c2 6e 74 5f 74 69 6d 65 20 2b 20 75 6e 69 78 45 70  nt_time + unixEp
137c3 6f 63 68 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  och;.  }.#endif.
137c4 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
137c5 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75  ER(pVfs);.  retu
137c6 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
137c7 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20  ./*.** Find the 
137c8 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e  current time (in
137c9 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64   Universal Coord
137ca 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57  inated Time).  W
137cb 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72  rite the.** curr
137cc 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74  ent time and dat
137cd 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61  e as a Julian Da
137ce 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70  y number into *p
137cf 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75  rNow and.** retu
137d0 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20  rn 0.  Return 1 
137d1 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20  if the time and 
137d2 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66  date cannot be f
137d3 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ound..*/.static 
137d4 69 6e 74 20 77 69 6e 43 75 72 72 65 6e 74 54 69  int winCurrentTi
137d5 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  me(sqlite3_vfs *
137d6 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 72  pVfs, double *pr
137d7 4e 6f 77 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Now){.  int rc;.
137d8 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
137d9 69 3b 0a 20 20 72 63 20 3d 20 77 69 6e 43 75 72  i;.  rc = winCur
137da 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 28 70 56  rentTimeInt64(pV
137db 66 73 2c 20 26 69 29 3b 0a 20 20 69 66 28 20 21  fs, &i);.  if( !
137dc 72 63 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77  rc ){.    *prNow
137dd 20 3d 20 69 2f 38 36 34 30 30 30 30 30 2e 30 3b   = i/86400000.0;
137de 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
137df 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69  ;.}../*.** The i
137e0 64 65 61 20 69 73 20 74 68 61 74 20 74 68 69 73  dea is that this
137e1 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20   function works 
137e2 6c 69 6b 65 20 61 20 63 6f 6d 62 69 6e 61 74 69  like a combinati
137e3 6f 6e 20 6f 66 0a 2a 2a 20 47 65 74 4c 61 73 74  on of.** GetLast
137e4 45 72 72 6f 72 28 29 20 61 6e 64 20 46 6f 72 6d  Error() and Form
137e5 61 74 4d 65 73 73 61 67 65 28 29 20 6f 6e 20 57  atMessage() on W
137e6 69 6e 64 6f 77 73 20 28 6f 72 20 65 72 72 6e 6f  indows (or errno
137e7 20 61 6e 64 0a 2a 2a 20 73 74 72 65 72 72 6f 72   and.** strerror
137e8 5f 72 28 29 20 6f 6e 20 55 6e 69 78 29 2e 20 41  _r() on Unix). A
137e9 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 69 73  fter an error is
137ea 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 6e 20   returned by an 
137eb 4f 53 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c 20  OS.** function, 
137ec 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69  SQLite calls thi
137ed 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  s function with 
137ee 7a 42 75 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f  zBuf pointing to
137ef 0a 2a 2a 20 61 20 62 75 66 66 65 72 20 6f 66 20  .** a buffer of 
137f0 6e 42 75 66 20 62 79 74 65 73 2e 20 54 68 65 20  nBuf bytes. The 
137f1 4f 53 20 6c 61 79 65 72 20 73 68 6f 75 6c 64 20  OS layer should 
137f2 70 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a 2a 20  populate the.** 
137f3 62 75 66 66 65 72 20 77 69 74 68 20 61 20 6e 75  buffer with a nu
137f4 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46  l-terminated UTF
137f5 2d 38 20 65 6e 63 6f 64 65 64 20 65 72 72 6f 72  -8 encoded error
137f6 20 6d 65 73 73 61 67 65 0a 2a 2a 20 64 65 73 63   message.** desc
137f7 72 69 62 69 6e 67 20 74 68 65 20 6c 61 73 74 20  ribing the last 
137f8 49 4f 20 65 72 72 6f 72 20 74 6f 20 68 61 76 65  IO error to have
137f9 20 6f 63 63 75 72 72 65 64 20 77 69 74 68 69 6e   occurred within
137fa 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20   the calling.** 
137fb 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  thread..**.** If
137fc 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
137fd 67 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20  ge is too large 
137fe 66 6f 72 20 74 68 65 20 73 75 70 70 6c 69 65 64  for the supplied
137ff 20 62 75 66 66 65 72 2c 0a 2a 2a 20 69 74 20 73   buffer,.** it s
13800 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61 74  hould be truncat
13801 65 64 2e 20 54 68 65 20 72 65 74 75 72 6e 20 76  ed. The return v
13802 61 6c 75 65 20 6f 66 20 78 47 65 74 4c 61 73 74  alue of xGetLast
13803 45 72 72 6f 72 0a 2a 2a 20 69 73 20 7a 65 72 6f  Error.** is zero
13804 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65   if the error me
13805 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74 68  ssage fits in th
13806 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e  e buffer, or non
13807 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69  -zero.** otherwi
13808 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73 61  se (if the messa
13809 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65 64  ge was truncated
1380a 29 2e 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 69  ). If non-zero i
1380b 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a 20 74  s returned,.** t
1380c 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20 6e 65  hen it is not ne
1380d 63 65 73 73 61 72 79 20 74 6f 20 69 6e 63 6c 75  cessary to inclu
1380e 64 65 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d 69  de the nul-termi
1380f 6e 61 74 6f 72 20 63 68 61 72 61 63 74 65 72 0a  nator character.
13810 2a 2a 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74  ** in the output
13811 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e   buffer..**.** N
13812 6f 74 20 73 75 70 70 6c 79 69 6e 67 20 61 6e 20  ot supplying an 
13813 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 69  error message wi
13814 6c 6c 20 68 61 76 65 20 6e 6f 20 61 64 76 65 72  ll have no adver
13815 73 65 20 65 66 66 65 63 74 0a 2a 2a 20 6f 6e 20  se effect.** on 
13816 53 51 4c 69 74 65 2e 20 49 74 20 69 73 20 66 69  SQLite. It is fi
13817 6e 65 20 74 6f 20 68 61 76 65 20 61 6e 20 69 6d  ne to have an im
13818 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61  plementation tha
13819 74 20 6e 65 76 65 72 0a 2a 2a 20 72 65 74 75 72  t never.** retur
1381a 6e 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ns an error mess
1381b 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 6e 74  age:.**.**   int
1381c 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73   xGetLastError(s
1381d 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
1381e 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  , int nBuf, char
1381f 20 2a 7a 42 75 66 29 7b 0a 2a 2a 20 20 20 20 20   *zBuf){.**     
13820 61 73 73 65 72 74 28 7a 42 75 66 5b 30 5d 3d 3d  assert(zBuf[0]==
13821 27 5c 30 27 29 3b 0a 2a 2a 20 20 20 20 20 72 65  '\0');.**     re
13822 74 75 72 6e 20 30 3b 0a 2a 2a 20 20 20 7d 0a 2a  turn 0;.**   }.*
13823 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 20 69 66 20  *.** However if 
13824 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
13825 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20 69 74   is supplied, it
13826 20 77 69 6c 6c 20 62 65 20 69 6e 63 6f 72 70 6f   will be incorpo
13827 72 61 74 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69  rated.** by sqli
13828 74 65 20 69 6e 74 6f 20 74 68 65 20 65 72 72 6f  te into the erro
13829 72 20 6d 65 73 73 61 67 65 20 61 76 61 69 6c 61  r message availa
1382a 62 6c 65 20 74 6f 20 74 68 65 20 75 73 65 72 20  ble to the user 
1382b 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  using.** sqlite3
1382c 5f 65 72 72 6d 73 67 28 29 2c 20 70 6f 73 73 69  _errmsg(), possi
1382d 62 6c 79 20 6d 61 6b 69 6e 67 20 49 4f 20 65 72  bly making IO er
1382e 72 6f 72 73 20 65 61 73 69 65 72 20 74 6f 20 64  rors easier to d
1382f 65 62 75 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ebug..*/.static 
13830 69 6e 74 20 77 69 6e 47 65 74 4c 61 73 74 45 72  int winGetLastEr
13831 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ror(sqlite3_vfs 
13832 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c  *pVfs, int nBuf,
13833 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20   char *zBuf){.  
13834 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
13835 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 72 6e  (pVfs);.  return
13836 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67   getLastErrorMsg
13837 28 6f 73 47 65 74 4c 61 73 74 45 72 72 6f 72 28  (osGetLastError(
13838 29 2c 20 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a  ), nBuf, zBuf);.
13839 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  }../*.** Initial
1383a 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61  ize and deinitia
1383b 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69  lize the operati
1383c 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66  ng system interf
1383d 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ace..*/.SQLITE_A
1383e 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
1383f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20  s_init(void){.  
13840 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76  static sqlite3_v
13841 66 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20  fs winVfs = {.  
13842 20 20 33 2c 20 20 20 20 20 20 20 20 20 20 20 20    3,            
13843 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69         /* iVersi
13844 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66  on */.    sizeof
13845 28 77 69 6e 46 69 6c 65 29 2c 20 20 20 20 20 2f  (winFile),     /
13846 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20  * szOsFile */.  
13847 20 20 4d 41 58 5f 50 41 54 48 2c 20 20 20 20 20    MAX_PATH,     
13848 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68         /* mxPath
13849 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20  name */.    0,  
1384a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1384b 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20   /* pNext */.   
1384c 20 22 77 69 6e 33 32 22 2c 20 20 20 20 20 20 20   "win32",       
1384d 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a        /* zName *
1384e 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
1384f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41             /* pA
13850 70 70 44 61 74 61 20 2a 2f 0a 20 20 20 20 77 69  ppData */.    wi
13851 6e 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20  nOpen,          
13852 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0a 20     /* xOpen */. 
13853 20 20 20 77 69 6e 44 65 6c 65 74 65 2c 20 20 20     winDelete,   
13854 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c 65          /* xDele
13855 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41 63 63  te */.    winAcc
13856 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ess,           /
13857 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20  * xAccess */.   
13858 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65   winFullPathname
13859 2c 20 20 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61  ,     /* xFullPa
1385a 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69  thname */.    wi
1385b 6e 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20  nDlOpen,        
1385c 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f     /* xDlOpen */
1385d 0a 20 20 20 20 77 69 6e 44 6c 45 72 72 6f 72 2c  .    winDlError,
1385e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c            /* xDl
1385f 45 72 72 6f 72 20 2a 2f 0a 20 20 20 20 77 69 6e  Error */.    win
13860 44 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20  DlSym,          
13861 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a 20    /* xDlSym */. 
13862 20 20 20 77 69 6e 44 6c 43 6c 6f 73 65 2c 20 20     winDlClose,  
13863 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c          /* xDlCl
13864 6f 73 65 20 2a 2f 0a 20 20 20 20 77 69 6e 52 61  ose */.    winRa
13865 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 20 20  ndomness,       
13866 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a  /* xRandomness *
13867 2f 0a 20 20 20 20 77 69 6e 53 6c 65 65 70 2c 20  /.    winSleep, 
13868 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
13869 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69 6e 43  leep */.    winC
1386a 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 20  urrentTime,     
1386b 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65   /* xCurrentTime
1386c 20 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61   */.    winGetLa
1386d 73 74 45 72 72 6f 72 2c 20 20 20 20 20 2f 2a 20  stError,     /* 
1386e 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f  xGetLastError */
1386f 0a 20 20 20 20 77 69 6e 43 75 72 72 65 6e 74 54  .    winCurrentT
13870 69 6d 65 49 6e 74 36 34 2c 20 2f 2a 20 78 43 75  imeInt64, /* xCu
13871 72 72 65 6e 74 54 69 6d 65 49 6e 74 36 34 20 2a  rrentTimeInt64 *
13872 2f 0a 20 20 20 20 77 69 6e 53 65 74 53 79 73 74  /.    winSetSyst
13873 65 6d 43 61 6c 6c 2c 20 20 20 20 2f 2a 20 78 53  emCall,    /* xS
13874 65 74 53 79 73 74 65 6d 43 61 6c 6c 20 2a 2f 0a  etSystemCall */.
13875 20 20 20 20 77 69 6e 47 65 74 53 79 73 74 65 6d      winGetSystem
13876 43 61 6c 6c 2c 20 20 20 20 2f 2a 20 78 47 65 74  Call,    /* xGet
13877 53 79 73 74 65 6d 43 61 6c 6c 20 2a 2f 0a 20 20  SystemCall */.  
13878 20 20 77 69 6e 4e 65 78 74 53 79 73 74 65 6d 43    winNextSystemC
13879 61 6c 6c 2c 20 20 20 2f 2a 20 78 4e 65 78 74 53  all,   /* xNextS
1387a 79 73 74 65 6d 43 61 6c 6c 20 2a 2f 0a 20 20 7d  ystemCall */.  }
1387b 3b 0a 0a 20 20 2f 2a 20 44 6f 75 62 6c 65 2d 63  ;..  /* Double-c
1387c 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 61 53  heck that the aS
1387d 79 73 63 61 6c 6c 5b 5d 20 61 72 72 61 79 20 68  yscall[] array h
1387e 61 73 20 62 65 65 6e 20 63 6f 6e 73 74 72 75 63  as been construc
1387f 74 65 64 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74  ted.  ** correct
13880 6c 79 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20  ly.  See ticket 
13881 5b 62 62 33 61 38 36 65 38 39 30 63 38 65 39 36  [bb3a86e890c8e96
13882 61 62 5d 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ab] */.  assert(
13883 20 41 72 72 61 79 53 69 7a 65 28 61 53 79 73 63   ArraySize(aSysc
13884 61 6c 6c 29 3d 3d 37 34 20 29 3b 0a 0a 23 69 66  all)==74 );..#if
13885 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
13886 5f 57 41 4c 0a 20 20 2f 2a 20 67 65 74 20 6d 65  _WAL.  /* get me
13887 6d 6f 72 79 20 6d 61 70 20 61 6c 6c 6f 63 61 74  mory map allocat
13888 69 6f 6e 20 67 72 61 6e 75 6c 61 72 69 74 79 20  ion granularity 
13889 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 77 69 6e  */.  memset(&win
1388a 53 79 73 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65  SysInfo, 0, size
1388b 6f 66 28 53 59 53 54 45 4d 5f 49 4e 46 4f 29 29  of(SYSTEM_INFO))
1388c 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ;.#if SQLITE_OS_
1388d 57 49 4e 52 54 0a 20 20 6f 73 47 65 74 4e 61 74  WINRT.  osGetNat
1388e 69 76 65 53 79 73 74 65 6d 49 6e 66 6f 28 26 77  iveSystemInfo(&w
1388f 69 6e 53 79 73 49 6e 66 6f 29 3b 0a 23 65 6c 73  inSysInfo);.#els
13890 65 0a 20 20 6f 73 47 65 74 53 79 73 74 65 6d 49  e.  osGetSystemI
13891 6e 66 6f 28 26 77 69 6e 53 79 73 49 6e 66 6f 29  nfo(&winSysInfo)
13892 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72  ;.#endif.  asser
13893 74 28 77 69 6e 53 79 73 49 6e 66 6f 2e 64 77 41  t(winSysInfo.dwA
13894 6c 6c 6f 63 61 74 69 6f 6e 47 72 61 6e 75 6c 61  llocationGranula
13895 72 69 74 79 20 3e 20 30 29 3b 0a 23 65 6e 64 69  rity > 0);.#endi
13896 66 0a 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  f..  sqlite3_vfs
13897 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e 56 66  _register(&winVf
13898 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72 6e 20  s, 1);.  return 
13899 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 53  SQLITE_OK; .}..S
1389a 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1389b 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69  lite3_os_end(voi
1389c 64 29 7b 20 0a 23 69 66 20 53 51 4c 49 54 45 5f  d){ .#if SQLITE_
1389d 4f 53 5f 57 49 4e 52 54 0a 20 20 69 66 28 20 73  OS_WINRT.  if( s
1389e 6c 65 65 70 4f 62 6a 21 3d 4e 55 4c 4c 20 29 7b  leepObj!=NULL ){
1389f 0a 20 20 20 20 6f 73 43 6c 6f 73 65 48 61 6e 64  .    osCloseHand
138a0 6c 65 28 73 6c 65 65 70 4f 62 6a 29 3b 0a 20 20  le(sleepObj);.  
138a1 20 20 73 6c 65 65 70 4f 62 6a 20 3d 20 4e 55 4c    sleepObj = NUL
138a2 4c 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  L;.  }.#endif.  
138a3 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
138a4 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
138a5 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 2a 2f 0a  QLITE_OS_WIN */.
138a6 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
138a7 20 45 6e 64 20 6f 66 20 6f 73 5f 77 69 6e 2e 63   End of os_win.c
138a8 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
138a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
138aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
138ab 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
138ac 20 42 65 67 69 6e 20 66 69 6c 65 20 62 69 74 76   Begin file bitv
138ad 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ec.c ***********
138ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
138af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
138b0 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 46 65 62 72  ./*.** 2008 Febr
138b1 75 61 72 79 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68  uary 16.**.** Th
138b2 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
138b3 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
138b4 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
138b5 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
138b6 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
138b7 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
138b8 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
138b9 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
138ba 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
138bb 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
138bc 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
138bd 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
138be 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
138bf 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
138c0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
138c1 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
138c2 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
138c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
138c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
138c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
138c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
138c7 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
138c8 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ile implements a
138c9 6e 20 6f 62 6a 65 63 74 20 74 68 61 74 20 72 65  n object that re
138ca 70 72 65 73 65 6e 74 73 20 61 20 66 69 78 65 64  presents a fixed
138cb 2d 6c 65 6e 67 74 68 0a 2a 2a 20 62 69 74 6d 61  -length.** bitma
138cc 70 2e 20 20 42 69 74 73 20 61 72 65 20 6e 75 6d  p.  Bits are num
138cd 62 65 72 65 64 20 73 74 61 72 74 69 6e 67 20 77  bered starting w
138ce 69 74 68 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 20 62  ith 1..**.** A b
138cf 69 74 6d 61 70 20 69 73 20 75 73 65 64 20 74 6f  itmap is used to
138d0 20 72 65 63 6f 72 64 20 77 68 69 63 68 20 70 61   record which pa
138d1 67 65 73 20 6f 66 20 61 20 64 61 74 61 62 61 73  ges of a databas
138d2 65 20 66 69 6c 65 20 68 61 76 65 20 62 65 65 6e  e file have been
138d3 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 64  .** journalled d
138d4 75 72 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74  uring a transact
138d5 69 6f 6e 2c 20 6f 72 20 77 68 69 63 68 20 70 61  ion, or which pa
138d6 67 65 73 20 68 61 76 65 20 74 68 65 20 22 64 6f  ges have the "do
138d7 6e 74 2d 77 72 69 74 65 22 0a 2a 2a 20 70 72 6f  nt-write".** pro
138d8 70 65 72 74 79 2e 20 20 55 73 75 61 6c 6c 79 20  perty.  Usually 
138d9 6f 6e 6c 79 20 61 20 66 65 77 20 70 61 67 65 73  only a few pages
138da 20 61 72 65 20 6d 65 65 74 20 65 69 74 68 65 72   are meet either
138db 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 20 53   condition..** S
138dc 6f 20 74 68 65 20 62 69 74 6d 61 70 20 69 73 20  o the bitmap is 
138dd 75 73 75 61 6c 6c 79 20 73 70 61 72 73 65 20 61  usually sparse a
138de 6e 64 20 68 61 73 20 6c 6f 77 20 63 61 72 64 69  nd has low cardi
138df 6e 61 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20 73  nality..** But s
138e0 6f 6d 65 74 69 6d 65 73 20 28 66 6f 72 20 65 78  ometimes (for ex
138e1 61 6d 70 6c 65 20 77 68 65 6e 20 64 75 72 69 6e  ample when durin
138e2 67 20 61 20 44 52 4f 50 20 6f 66 20 61 20 6c 61  g a DROP of a la
138e3 72 67 65 20 74 61 62 6c 65 29 20 6d 6f 73 74 0a  rge table) most.
138e4 2a 2a 20 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65  ** or all of the
138e5 20 70 61 67 65 73 20 69 6e 20 61 20 64 61 74 61   pages in a data
138e6 62 61 73 65 20 63 61 6e 20 67 65 74 20 6a 6f 75  base can get jou
138e7 72 6e 61 6c 6c 65 64 2e 20 20 49 6e 20 74 68 6f  rnalled.  In tho
138e8 73 65 20 63 61 73 65 73 2c 20 0a 2a 2a 20 74 68  se cases, .** th
138e9 65 20 62 69 74 6d 61 70 20 62 65 63 6f 6d 65 73  e bitmap becomes
138ea 20 64 65 6e 73 65 20 77 69 74 68 20 68 69 67 68   dense with high
138eb 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 20 20 54   cardinality.  T
138ec 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 6e 65 65  he algorithm nee
138ed 64 73 20 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65  ds .** to handle
138ee 20 62 6f 74 68 20 63 61 73 65 73 20 77 65 6c 6c   both cases well
138ef 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65  ..**.** The size
138f0 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70 20 69   of the bitmap i
138f1 73 20 66 69 78 65 64 20 77 68 65 6e 20 74 68 65  s fixed when the
138f2 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61 74   object is creat
138f3 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 62 69  ed..**.** All bi
138f4 74 73 20 61 72 65 20 63 6c 65 61 72 20 77 68 65  ts are clear whe
138f5 6e 20 74 68 65 20 62 69 74 6d 61 70 20 69 73 20  n the bitmap is 
138f6 63 72 65 61 74 65 64 2e 20 20 49 6e 64 69 76 69  created.  Indivi
138f7 64 75 61 6c 20 62 69 74 73 0a 2a 2a 20 6d 61 79  dual bits.** may
138f8 20 62 65 20 73 65 74 20 6f 72 20 63 6c 65 61 72   be set or clear
138f9 65 64 20 6f 6e 65 20 61 74 20 61 20 74 69 6d 65  ed one at a time
138fa 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 74 20 6f 70 65  ..**.** Test ope
138fb 72 61 74 69 6f 6e 73 20 61 72 65 20 61 62 6f 75  rations are abou
138fc 74 20 31 30 30 20 74 69 6d 65 73 20 6d 6f 72 65  t 100 times more
138fd 20 63 6f 6d 6d 6f 6e 20 74 68 61 74 20 73 65 74   common that set
138fe 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 20   operations..** 
138ff 43 6c 65 61 72 20 6f 70 65 72 61 74 69 6f 6e 73  Clear operations
13900 20 61 72 65 20 65 78 63 65 65 64 69 6e 67 6c 79   are exceedingly
13901 20 72 61 72 65 2e 20 20 54 68 65 72 65 20 61 72   rare.  There ar
13902 65 20 75 73 75 61 6c 6c 79 20 62 65 74 77 65 65  e usually betwee
13903 6e 0a 2a 2a 20 35 20 61 6e 64 20 35 30 30 20 73  n.** 5 and 500 s
13904 65 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 70 65  et operations pe
13905 72 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2c  r Bitvec object,
13906 20 74 68 6f 75 67 68 20 74 68 65 20 6e 75 6d 62   though the numb
13907 65 72 20 6f 66 20 73 65 74 73 20 63 61 6e 0a 2a  er of sets can.*
13908 2a 20 73 6f 6d 65 74 69 6d 65 73 20 67 72 6f 77  * sometimes grow
13909 20 69 6e 74 6f 20 74 65 6e 73 20 6f 66 20 74 68   into tens of th
1390a 6f 75 73 61 6e 64 73 20 6f 72 20 6c 61 72 67 65  ousands or large
1390b 72 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20  r.  The size of 
1390c 74 68 65 0a 2a 2a 20 42 69 74 76 65 63 20 6f 62  the.** Bitvec ob
1390d 6a 65 63 74 20 69 73 20 74 68 65 20 6e 75 6d 62  ject is the numb
1390e 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
1390f 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
13910 20 61 74 20 74 68 65 0a 2a 2a 20 73 74 61 72 74   at the.** start
13911 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   of a transactio
13912 6e 2c 20 61 6e 64 20 69 73 20 74 68 75 73 20 75  n, and is thus u
13913 73 75 61 6c 6c 79 20 6c 65 73 73 20 74 68 61 6e  sually less than
13914 20 61 20 66 65 77 20 74 68 6f 75 73 61 6e 64 2c   a few thousand,
13915 0a 2a 2a 20 62 75 74 20 63 61 6e 20 62 65 20 61  .** but can be a
13916 73 20 6c 61 72 67 65 20 61 73 20 32 20 62 69 6c  s large as 2 bil
13917 6c 69 6f 6e 20 66 6f 72 20 61 20 72 65 61 6c 6c  lion for a reall
13918 79 20 62 69 67 20 64 61 74 61 62 61 73 65 2e 0a  y big database..
13919 2a 2f 0a 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 74  */../* Size of t
1391a 68 65 20 42 69 74 76 65 63 20 73 74 72 75 63 74  he Bitvec struct
1391b 75 72 65 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  ure in bytes. */
1391c 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f  .#define BITVEC_
1391d 53 5a 20 20 20 20 20 20 20 20 35 31 32 0a 0a 2f  SZ        512../
1391e 2a 20 52 6f 75 6e 64 20 74 68 65 20 75 6e 69 6f  * Round the unio
1391f 6e 20 73 69 7a 65 20 64 6f 77 6e 20 74 6f 20 74  n size down to t
13920 68 65 20 6e 65 61 72 65 73 74 20 70 6f 69 6e 74  he nearest point
13921 65 72 20 62 6f 75 6e 64 61 72 79 2c 20 73 69 6e  er boundary, sin
13922 63 65 20 74 68 61 74 27 73 20 68 6f 77 20 0a 2a  ce that's how .*
13923 2a 20 69 74 20 77 69 6c 6c 20 62 65 20 61 6c 69  * it will be ali
13924 67 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20  gned within the 
13925 42 69 74 76 65 63 20 73 74 72 75 63 74 2e 20 2a  Bitvec struct. *
13926 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43  /.#define BITVEC
13927 5f 55 53 49 5a 45 20 20 20 20 20 28 28 28 42 49  _USIZE     (((BI
13928 54 56 45 43 5f 53 5a 2d 28 33 2a 73 69 7a 65 6f  TVEC_SZ-(3*sizeo
13929 66 28 75 33 32 29 29 29 2f 73 69 7a 65 6f 66 28  f(u32)))/sizeof(
1392a 42 69 74 76 65 63 2a 29 29 2a 73 69 7a 65 6f 66  Bitvec*))*sizeof
1392b 28 42 69 74 76 65 63 2a 29 29 0a 0a 2f 2a 20 54  (Bitvec*))../* T
1392c 79 70 65 20 6f 66 20 74 68 65 20 61 72 72 61 79  ype of the array
1392d 20 22 65 6c 65 6d 65 6e 74 22 20 66 6f 72 20 74   "element" for t
1392e 68 65 20 62 69 74 6d 61 70 20 72 65 70 72 65 73  he bitmap repres
1392f 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a 2a 20 53 68  entation. .** Sh
13930 6f 75 6c 64 20 62 65 20 61 20 70 6f 77 65 72 20  ould be a power 
13931 6f 66 20 32 2c 20 61 6e 64 20 69 64 65 61 6c 6c  of 2, and ideall
13932 79 2c 20 65 76 65 6e 6c 79 20 64 69 76 69 64 65  y, evenly divide
13933 20 69 6e 74 6f 20 42 49 54 56 45 43 5f 55 53 49   into BITVEC_USI
13934 5a 45 2e 20 0a 2a 2a 20 53 65 74 74 69 6e 67 20  ZE. .** Setting 
13935 74 68 69 73 20 74 6f 20 74 68 65 20 22 6e 61 74  this to the "nat
13936 75 72 61 6c 20 77 6f 72 64 22 20 73 69 7a 65 20  ural word" size 
13937 6f 66 20 79 6f 75 72 20 43 50 55 20 6d 61 79 20  of your CPU may 
13938 69 6d 70 72 6f 76 65 0a 2a 2a 20 70 65 72 66 6f  improve.** perfo
13939 72 6d 61 6e 63 65 2e 20 2a 2f 0a 23 64 65 66 69  rmance. */.#defi
1393a 6e 65 20 42 49 54 56 45 43 5f 54 45 4c 45 4d 20  ne BITVEC_TELEM 
1393b 20 20 20 20 75 38 0a 2f 2a 20 53 69 7a 65 2c 20      u8./* Size, 
1393c 69 6e 20 62 69 74 73 2c 20 6f 66 20 74 68 65 20  in bits, of the 
1393d 62 69 74 6d 61 70 20 65 6c 65 6d 65 6e 74 2e 20  bitmap element. 
1393e 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45  */.#define BITVE
1393f 43 5f 53 5a 45 4c 45 4d 20 20 20 20 38 0a 2f 2a  C_SZELEM    8./*
13940 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
13941 6e 74 73 20 69 6e 20 61 20 62 69 74 6d 61 70 20  nts in a bitmap 
13942 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69 6e  array. */.#defin
13943 65 20 42 49 54 56 45 43 5f 4e 45 4c 45 4d 20 20  e BITVEC_NELEM  
13944 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45     (BITVEC_USIZE
13945 2f 73 69 7a 65 6f 66 28 42 49 54 56 45 43 5f 54  /sizeof(BITVEC_T
13946 45 4c 45 4d 29 29 0a 2f 2a 20 4e 75 6d 62 65 72  ELEM))./* Number
13947 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 20   of bits in the 
13948 62 69 74 6d 61 70 20 61 72 72 61 79 2e 20 2a 2f  bitmap array. */
13949 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f  .#define BITVEC_
1394a 4e 42 49 54 20 20 20 20 20 20 28 42 49 54 56 45  NBIT      (BITVE
1394b 43 5f 4e 45 4c 45 4d 2a 42 49 54 56 45 43 5f 53  C_NELEM*BITVEC_S
1394c 5a 45 4c 45 4d 29 0a 0a 2f 2a 20 4e 75 6d 62 65  ZELEM)../* Numbe
1394d 72 20 6f 66 20 75 33 32 20 76 61 6c 75 65 73 20  r of u32 values 
1394e 69 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 20 2a  in hash table. *
1394f 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43  /.#define BITVEC
13950 5f 4e 49 4e 54 20 20 20 20 20 20 28 42 49 54 56  _NINT      (BITV
13951 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28  EC_USIZE/sizeof(
13952 75 33 32 29 29 0a 2f 2a 20 4d 61 78 69 6d 75 6d  u32))./* Maximum
13953 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   number of entri
13954 65 73 20 69 6e 20 68 61 73 68 20 74 61 62 6c 65  es in hash table
13955 20 62 65 66 6f 72 65 20 0a 2a 2a 20 73 75 62 2d   before .** sub-
13956 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72 65 2d  dividing and re-
13957 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 23 64 65 66  hashing. */.#def
13958 69 6e 65 20 42 49 54 56 45 43 5f 4d 58 48 41 53  ine BITVEC_MXHAS
13959 48 20 20 20 20 28 42 49 54 56 45 43 5f 4e 49 4e  H    (BITVEC_NIN
1395a 54 2f 32 29 0a 2f 2a 20 48 61 73 68 69 6e 67 20  T/2)./* Hashing 
1395b 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 65  function for the
1395c 20 61 48 61 73 68 20 72 65 70 72 65 73 65 6e 74   aHash represent
1395d 61 74 69 6f 6e 2e 0a 2a 2a 20 45 6d 70 69 72 69  ation..** Empiri
1395e 63 61 6c 20 74 65 73 74 69 6e 67 20 73 68 6f 77  cal testing show
1395f 65 64 20 74 68 61 74 20 74 68 65 20 2a 33 37 20  ed that the *37 
13960 6d 75 6c 74 69 70 6c 69 65 72 20 0a 2a 2a 20 28  multiplier .** (
13961 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 72 69  an arbitrary pri
13962 6d 65 29 69 6e 20 74 68 65 20 68 61 73 68 20 66  me)in the hash f
13963 75 6e 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 64  unction provided
13964 20 0a 2a 2a 20 6e 6f 20 66 65 77 65 72 20 63 6f   .** no fewer co
13965 6c 6c 69 73 69 6f 6e 73 20 74 68 61 6e 20 74 68  llisions than th
13966 65 20 6e 6f 2d 6f 70 20 2a 31 2e 20 2a 2f 0a 23  e no-op *1. */.#
13967 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 48 41  define BITVEC_HA
13968 53 48 28 58 29 20 20 20 28 28 28 58 29 2a 31 29  SH(X)   (((X)*1)
13969 25 42 49 54 56 45 43 5f 4e 49 4e 54 29 0a 0a 23  %BITVEC_NINT)..#
1396a 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 50  define BITVEC_NP
1396b 54 52 20 20 20 20 20 20 28 42 49 54 56 45 43 5f  TR      (BITVEC_
1396c 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 69 74  USIZE/sizeof(Bit
1396d 76 65 63 20 2a 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20  vec *)).../*.** 
1396e 41 20 62 69 74 6d 61 70 20 69 73 20 61 6e 20 69  A bitmap is an i
1396f 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
13970 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
13971 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 62  re..**.** This b
13972 69 74 6d 61 70 20 72 65 63 6f 72 64 73 20 74 68  itmap records th
13973 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20 7a  e existance of z
13974 65 72 6f 20 6f 72 20 6d 6f 72 65 20 62 69 74 73  ero or more bits
13975 0a 2a 2a 20 77 69 74 68 20 76 61 6c 75 65 73 20  .** with values 
13976 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69 53  between 1 and iS
13977 69 7a 65 2c 20 69 6e 63 6c 75 73 69 76 65 2e 0a  ize, inclusive..
13978 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  **.** There are 
13979 74 68 72 65 65 20 70 6f 73 73 69 62 6c 65 20 72  three possible r
1397a 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 6f  epresentations o
1397b 66 20 74 68 65 20 62 69 74 6d 61 70 2e 0a 2a 2a  f the bitmap..**
1397c 20 49 66 20 69 53 69 7a 65 3c 3d 42 49 54 56 45   If iSize<=BITVE
1397d 43 5f 4e 42 49 54 2c 20 74 68 65 6e 20 42 69 74  C_NBIT, then Bit
1397e 76 65 63 2e 75 2e 61 42 69 74 6d 61 70 5b 5d 20  vec.u.aBitmap[] 
1397f 69 73 20 61 20 73 74 72 61 69 67 68 74 0a 2a 2a  is a straight.**
13980 20 62 69 74 6d 61 70 2e 20 20 54 68 65 20 6c 65   bitmap.  The le
13981 61 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20  ast significant 
13982 62 69 74 20 69 73 20 62 69 74 20 31 2e 0a 2a 2a  bit is bit 1..**
13983 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3e 42 49 54  .** If iSize>BIT
13984 56 45 43 5f 4e 42 49 54 20 61 6e 64 20 69 44 69  VEC_NBIT and iDi
13985 76 69 73 6f 72 3d 3d 30 20 74 68 65 6e 20 42 69  visor==0 then Bi
13986 74 76 65 63 2e 75 2e 61 48 61 73 68 5b 5d 20 69  tvec.u.aHash[] i
13987 73 0a 2a 2a 20 61 20 68 61 73 68 20 74 61 62 6c  s.** a hash tabl
13988 65 20 74 68 61 74 20 77 69 6c 6c 20 68 6f 6c 64  e that will hold
13989 20 75 70 20 74 6f 20 42 49 54 56 45 43 5f 4d 58   up to BITVEC_MX
1398a 48 41 53 48 20 64 69 73 74 69 6e 63 74 20 76 61  HASH distinct va
1398b 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65  lues..**.** Othe
1398c 72 77 69 73 65 2c 20 74 68 65 20 76 61 6c 75 65  rwise, the value
1398d 20 69 20 69 73 20 72 65 64 69 72 65 63 74 65 64   i is redirected
1398e 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20 42 49 54   into one of BIT
1398f 56 45 43 5f 4e 50 54 52 0a 2a 2a 20 73 75 62 2d  VEC_NPTR.** sub-
13990 62 69 74 6d 61 70 73 20 70 6f 69 6e 74 65 64 20  bitmaps pointed 
13991 74 6f 20 62 79 20 42 69 74 76 65 63 2e 75 2e 61  to by Bitvec.u.a
13992 70 53 75 62 5b 5d 2e 20 20 45 61 63 68 20 73 75  pSub[].  Each su
13993 62 62 69 74 6d 61 70 0a 2a 2a 20 68 61 6e 64 6c  bbitmap.** handl
13994 65 73 20 75 70 20 74 6f 20 69 44 69 76 69 73 6f  es up to iDiviso
13995 72 20 73 65 70 61 72 61 74 65 20 76 61 6c 75 65  r separate value
13996 73 20 6f 66 20 69 2e 20 20 61 70 53 75 62 5b 30  s of i.  apSub[0
13997 5d 20 68 6f 6c 64 73 0a 2a 2a 20 76 61 6c 75 65  ] holds.** value
13998 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20  s between 1 and 
13999 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53 75 62  iDivisor.  apSub
1399a 5b 31 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 73  [1] holds values
1399b 20 62 65 74 77 65 65 6e 0a 2a 2a 20 69 44 69 76   between.** iDiv
1399c 69 73 6f 72 2b 31 20 61 6e 64 20 32 2a 69 44 69  isor+1 and 2*iDi
1399d 76 69 73 6f 72 2e 20 20 61 70 53 75 62 5b 4e 5d  visor.  apSub[N]
1399e 20 68 6f 6c 64 73 20 76 61 6c 75 65 73 20 62 65   holds values be
1399f 74 77 65 65 6e 0a 2a 2a 20 4e 2a 69 44 69 76 69  tween.** N*iDivi
139a0 73 6f 72 2b 31 20 61 6e 64 20 28 4e 2b 31 29 2a  sor+1 and (N+1)*
139a1 69 44 69 76 69 73 6f 72 2e 20 20 45 61 63 68 20  iDivisor.  Each 
139a2 73 75 62 62 69 74 6d 61 70 20 69 73 20 6e 6f 72  subbitmap is nor
139a3 6d 61 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 68 6f  malized.** to ho
139a4 6c 64 20 64 65 61 6c 20 77 69 74 68 20 76 61 6c  ld deal with val
139a5 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e  ues between 1 an
139a6 64 20 69 44 69 76 69 73 6f 72 2e 0a 2a 2f 0a 73  d iDivisor..*/.s
139a7 74 72 75 63 74 20 42 69 74 76 65 63 20 7b 0a 20  truct Bitvec {. 
139a8 20 75 33 32 20 69 53 69 7a 65 3b 20 20 20 20 20   u32 iSize;     
139a9 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 62 69 74 20   /* Maximum bit 
139aa 69 6e 64 65 78 2e 20 20 4d 61 78 20 69 53 69 7a  index.  Max iSiz
139ab 65 20 69 73 20 34 2c 32 39 34 2c 39 36 37 2c 32  e is 4,294,967,2
139ac 39 36 2e 20 2a 2f 0a 20 20 75 33 32 20 6e 53 65  96. */.  u32 nSe
139ad 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  t;       /* Numb
139ae 65 72 20 6f 66 20 62 69 74 73 20 74 68 61 74 20  er of bits that 
139af 61 72 65 20 73 65 74 20 2d 20 6f 6e 6c 79 20 76  are set - only v
139b0 61 6c 69 64 20 66 6f 72 20 61 48 61 73 68 0a 20  alid for aHash. 
139b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139b2 20 2a 2a 20 65 6c 65 6d 65 6e 74 2e 20 20 4d 61   ** element.  Ma
139b3 78 20 69 73 20 42 49 54 56 45 43 5f 4e 49 4e 54  x is BITVEC_NINT
139b4 2e 20 20 46 6f 72 20 42 49 54 56 45 43 5f 53 5a  .  For BITVEC_SZ
139b5 20 6f 66 20 35 31 32 2c 0a 20 20 20 20 20 20 20   of 512,.       
139b6 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68             ** th
139b7 69 73 20 77 6f 75 6c 64 20 62 65 20 31 32 35 2e  is would be 125.
139b8 20 2a 2f 0a 20 20 75 33 32 20 69 44 69 76 69 73   */.  u32 iDivis
139b9 6f 72 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  or;   /* Number 
139ba 6f 66 20 62 69 74 73 20 68 61 6e 64 6c 65 64 20  of bits handled 
139bb 62 79 20 65 61 63 68 20 61 70 53 75 62 5b 5d 20  by each apSub[] 
139bc 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20 20 20  entry. */.      
139bd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
139be 68 6f 75 6c 64 20 3e 3d 30 20 66 6f 72 20 61 70  hould >=0 for ap
139bf 53 75 62 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a  Sub element. */.
139c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139c1 20 20 2f 2a 20 4d 61 78 20 69 44 69 76 69 73 6f    /* Max iDiviso
139c2 72 20 69 73 20 6d 61 78 28 75 33 32 29 20 2f 20  r is max(u32) / 
139c3 42 49 54 56 45 43 5f 4e 50 54 52 20 2b 20 31 2e  BITVEC_NPTR + 1.
139c4 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20    */.           
139c5 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 20         /* For a 
139c6 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 31 32  BITVEC_SZ of 512
139c7 2c 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20  , this would be 
139c8 33 34 2c 33 35 39 2c 37 33 39 2e 20 2a 2f 0a 20  34,359,739. */. 
139c9 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 42 49 54   union {.    BIT
139ca 56 45 43 5f 54 45 4c 45 4d 20 61 42 69 74 6d 61  VEC_TELEM aBitma
139cb 70 5b 42 49 54 56 45 43 5f 4e 45 4c 45 4d 5d 3b  p[BITVEC_NELEM];
139cc 20 20 20 20 2f 2a 20 42 69 74 6d 61 70 20 72 65      /* Bitmap re
139cd 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a  presentation */.
139ce 20 20 20 20 75 33 32 20 61 48 61 73 68 5b 42 49      u32 aHash[BI
139cf 54 56 45 43 5f 4e 49 4e 54 5d 3b 20 20 20 20 20  TVEC_NINT];     
139d0 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 72   /* Hash table r
139d1 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f  epresentation */
139d2 0a 20 20 20 20 42 69 74 76 65 63 20 2a 61 70 53  .    Bitvec *apS
139d3 75 62 5b 42 49 54 56 45 43 5f 4e 50 54 52 5d 3b  ub[BITVEC_NPTR];
139d4 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 20 72    /* Recursive r
139d5 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f  epresentation */
139d6 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a  .  } u;.};../*.*
139d7 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 62  * Create a new b
139d8 69 74 6d 61 70 20 6f 62 6a 65 63 74 20 61 62 6c  itmap object abl
139d9 65 20 74 6f 20 68 61 6e 64 6c 65 20 62 69 74 73  e to handle bits
139da 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 69   between 0 and i
139db 53 69 7a 65 2c 0a 2a 2a 20 69 6e 63 6c 75 73 69  Size,.** inclusi
139dc 76 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f  ve.  Return a po
139dd 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77  inter to the new
139de 20 6f 62 6a 65 63 74 2e 20 20 52 65 74 75 72 6e   object.  Return
139df 20 4e 55 4c 4c 20 69 66 20 0a 2a 2a 20 6d 61 6c   NULL if .** mal
139e0 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51  loc fails..*/.SQ
139e1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 69 74  LITE_PRIVATE Bit
139e2 76 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76  vec *sqlite3Bitv
139e3 65 63 43 72 65 61 74 65 28 75 33 32 20 69 53 69  ecCreate(u32 iSi
139e4 7a 65 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70  ze){.  Bitvec *p
139e5 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
139e6 6f 66 28 2a 70 29 3d 3d 42 49 54 56 45 43 5f 53  of(*p)==BITVEC_S
139e7 5a 20 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74  Z );.  p = sqlit
139e8 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69  e3MallocZero( si
139e9 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 66  zeof(*p) );.  if
139ea 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 69 53  ( p ){.    p->iS
139eb 69 7a 65 20 3d 20 69 53 69 7a 65 3b 0a 20 20 7d  ize = iSize;.  }
139ec 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
139ed 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73  /*.** Check to s
139ee 65 65 20 69 66 20 74 68 65 20 69 2d 74 68 20 62  ee if the i-th b
139ef 69 74 20 69 73 20 73 65 74 2e 20 20 52 65 74 75  it is set.  Retu
139f0 72 6e 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65  rn true or false
139f1 2e 0a 2a 2a 20 49 66 20 70 20 69 73 20 4e 55 4c  ..** If p is NUL
139f2 4c 20 28 69 66 20 74 68 65 20 62 69 74 6d 61 70  L (if the bitmap
139f3 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 63 72   has not been cr
139f4 65 61 74 65 64 29 20 6f 72 20 69 66 0a 2a 2a 20  eated) or if.** 
139f5 69 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  i is out of rang
139f6 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 66  e, then return f
139f7 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  alse..*/.SQLITE_
139f8 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
139f9 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42 69  te3BitvecTest(Bi
139fa 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b  tvec *p, u32 i){
139fb 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
139fc 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 69 3e  turn 0;.  if( i>
139fd 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 69 3d 3d 30  p->iSize || i==0
139fe 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
139ff 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e  --;.  while( p->
13a00 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20  iDivisor ){.    
13a01 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69  u32 bin = i/p->i
13a02 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d  Divisor;.    i =
13a03 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a   i%p->iDivisor;.
13a04 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53      p = p->u.apS
13a05 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 20  ub[bin];.    if 
13a06 28 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65 74  (!p) {.      ret
13a07 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 0;.    }.  }
13a08 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c  .  if( p->iSize<
13a09 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a  =BITVEC_NBIT ){.
13a0a 20 20 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 75      return (p->u
13a0b 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45  .aBitmap[i/BITVE
13a0c 43 5f 53 5a 45 4c 45 4d 5d 20 26 20 28 31 3c 3c  C_SZELEM] & (1<<
13a0d 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45  (i&(BITVEC_SZELE
13a0e 4d 2d 31 29 29 29 29 21 3d 30 3b 0a 20 20 7d 20  M-1))))!=0;.  } 
13a0f 65 6c 73 65 7b 0a 20 20 20 20 75 33 32 20 68 20  else{.    u32 h 
13a10 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 69 2b  = BITVEC_HASH(i+
13a11 2b 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70  +);.    while( p
13a12 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a  ->u.aHash[h] ){.
13a13 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61        if( p->u.a
13a14 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65 74  Hash[h]==i ) ret
13a15 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 68 20 3d  urn 1;.      h =
13a16 20 28 68 2b 31 29 20 25 20 42 49 54 56 45 43 5f   (h+1) % BITVEC_
13a17 4e 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20  NINT;.    }.    
13a18 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a  return 0;.  }.}.
13a19 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 69  ./*.** Set the i
13a1a 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75 72 6e  -th bit.  Return
13a1b 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e   0 on success an
13a1c 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  d an error code 
13a1d 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 67  if.** anything g
13a1e 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a  oes wrong..**.**
13a1f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69   This routine mi
13a20 67 68 74 20 63 61 75 73 65 20 73 75 62 2d 62 69  ght cause sub-bi
13a21 74 6d 61 70 73 20 74 6f 20 62 65 20 61 6c 6c 6f  tmaps to be allo
13a22 63 61 74 65 64 2e 20 20 46 61 69 6c 69 6e 67 0a  cated.  Failing.
13a23 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20 6d 65  ** to get the me
13a24 6d 6f 72 79 20 6e 65 65 64 65 64 20 74 6f 20 68  mory needed to h
13a25 6f 6c 64 20 74 68 65 20 73 75 62 2d 62 69 74 6d  old the sub-bitm
13a26 61 70 20 69 73 20 74 68 65 20 6f 6e 6c 79 0a 2a  ap is the only.*
13a27 2a 20 74 68 61 74 20 63 61 6e 20 67 6f 20 77 72  * that can go wr
13a28 6f 6e 67 20 77 69 74 68 20 61 6e 20 69 6e 73 65  ong with an inse
13a29 72 74 2c 20 61 73 73 75 6d 69 6e 67 20 70 20 61  rt, assuming p a
13a2a 6e 64 20 69 20 61 72 65 20 76 61 6c 69 64 2e 0a  nd i are valid..
13a2b 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e  **.** The callin
13a2c 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20  g function must 
13a2d 65 6e 73 75 72 65 20 74 68 61 74 20 70 20 69 73  ensure that p is
13a2e 20 61 20 76 61 6c 69 64 20 42 69 74 76 65 63 20   a valid Bitvec 
13a2f 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 74 68  object.** and th
13a30 61 74 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72  at the value for
13a31 20 22 69 22 20 69 73 20 77 69 74 68 69 6e 20 72   "i" is within r
13a32 61 6e 67 65 20 6f 66 20 74 68 65 20 42 69 74 76  ange of the Bitv
13a33 65 63 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f 74  ec object..** Ot
13a34 68 65 72 77 69 73 65 20 74 68 65 20 62 65 68 61  herwise the beha
13a35 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
13a36 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
13a37 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
13a38 42 69 74 76 65 63 53 65 74 28 42 69 74 76 65 63  BitvecSet(Bitvec
13a39 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 75   *p, u32 i){.  u
13a3a 33 32 20 68 3b 0a 20 20 69 66 28 20 70 3d 3d 30  32 h;.  if( p==0
13a3b 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
13a3c 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 69  _OK;.  assert( i
13a3d 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
13a3e 69 3c 3d 70 2d 3e 69 53 69 7a 65 20 29 3b 0a 20  i<=p->iSize );. 
13a3f 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 28 70   i--;.  while((p
13a40 2d 3e 69 53 69 7a 65 20 3e 20 42 49 54 56 45 43  ->iSize > BITVEC
13a41 5f 4e 42 49 54 29 20 26 26 20 70 2d 3e 69 44 69  _NBIT) && p->iDi
13a42 76 69 73 6f 72 29 20 7b 0a 20 20 20 20 75 33 32  visor) {.    u32
13a43 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76   bin = i/p->iDiv
13a44 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 25  isor;.    i = i%
13a45 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20  p->iDivisor;.   
13a46 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62 5b   if( p->u.apSub[
13a47 62 69 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  bin]==0 ){.     
13a48 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d   p->u.apSub[bin]
13a49 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63   = sqlite3Bitvec
13a4a 43 72 65 61 74 65 28 20 70 2d 3e 69 44 69 76 69  Create( p->iDivi
13a4b 73 6f 72 20 29 3b 0a 20 20 20 20 20 20 69 66 28  sor );.      if(
13a4c 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d   p->u.apSub[bin]
13a4d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==0 ) return SQL
13a4e 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
13a4f 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70  .    p = p->u.ap
13a50 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 7d 0a 20 20  Sub[bin];.  }.  
13a51 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49  if( p->iSize<=BI
13a52 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20  TVEC_NBIT ){.   
13a53 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f   p->u.aBitmap[i/
13a54 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 7c  BITVEC_SZELEM] |
13a55 3d 20 31 20 3c 3c 20 28 69 26 28 42 49 54 56 45  = 1 << (i&(BITVE
13a56 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 3b 0a 20 20  C_SZELEM-1));.  
13a57 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
13a58 4f 4b 3b 0a 20 20 7d 0a 20 20 68 20 3d 20 42 49  OK;.  }.  h = BI
13a59 54 56 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b 0a  TVEC_HASH(i++);.
13a5a 20 20 2f 2a 20 69 66 20 74 68 65 72 65 20 77 61    /* if there wa
13a5b 73 6e 27 74 20 61 20 68 61 73 68 20 63 6f 6c 6c  sn't a hash coll
13a5c 69 73 69 6f 6e 2c 20 61 6e 64 20 74 68 69 73 20  ision, and this 
13a5d 64 6f 65 73 6e 27 74 20 2a 2f 0a 20 20 2f 2a 20  doesn't */.  /* 
13a5e 63 6f 6d 70 6c 65 74 65 6c 79 20 66 69 6c 6c 20  completely fill 
13a5f 74 68 65 20 68 61 73 68 2c 20 74 68 65 6e 20 6a  the hash, then j
13a60 75 73 74 20 61 64 64 20 69 74 20 77 69 74 68 6f  ust add it witho
13a61 75 74 20 2a 2f 0a 20 20 2f 2a 20 77 6f 72 72 69  ut */.  /* worri
13a62 6e 67 20 61 62 6f 75 74 20 73 75 62 2d 64 69 76  ng about sub-div
13a63 69 64 69 6e 67 20 61 6e 64 20 72 65 2d 68 61 73  iding and re-has
13a64 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 20 21  hing. */.  if( !
13a65 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b  p->u.aHash[h] ){
13a66 0a 20 20 20 20 69 66 20 28 70 2d 3e 6e 53 65 74  .    if (p->nSet
13a67 3c 28 42 49 54 56 45 43 5f 4e 49 4e 54 2d 31 29  <(BITVEC_NINT-1)
13a68 29 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  ) {.      goto b
13a69 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3b 0a 20  itvec_set_end;. 
13a6a 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20     } else {.    
13a6b 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 73 65    goto bitvec_se
13a6c 74 5f 72 65 68 61 73 68 3b 0a 20 20 20 20 7d 0a  t_rehash;.    }.
13a6d 20 20 7d 0a 20 20 2f 2a 20 74 68 65 72 65 20 77    }.  /* there w
13a6e 61 73 20 61 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20  as a collision, 
13a6f 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  check to see if 
13a70 69 74 27 73 20 61 6c 72 65 61 64 79 20 2a 2f 0a  it's already */.
13a71 20 20 2f 2a 20 69 6e 20 68 61 73 68 2c 20 69 66    /* in hash, if
13a72 20 6e 6f 74 2c 20 74 72 79 20 74 6f 20 66 69 6e   not, try to fin
13a73 64 20 61 20 73 70 6f 74 20 66 6f 72 20 69 74 20  d a spot for it 
13a74 2a 2f 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 66  */.  do {.    if
13a75 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 3d  ( p->u.aHash[h]=
13a76 3d 69 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =i ) return SQLI
13a77 54 45 5f 4f 4b 3b 0a 20 20 20 20 68 2b 2b 3b 0a  TE_OK;.    h++;.
13a78 20 20 20 20 69 66 28 20 68 3e 3d 42 49 54 56 45      if( h>=BITVE
13a79 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b 0a  C_NINT ) h = 0;.
13a7a 20 20 7d 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e    } while( p->u.
13a7b 61 48 61 73 68 5b 68 5d 20 29 3b 0a 20 20 2f 2a  aHash[h] );.  /*
13a7c 20 77 65 20 64 69 64 6e 27 74 20 66 69 6e 64 20   we didn't find 
13a7d 69 74 20 69 6e 20 74 68 65 20 68 61 73 68 2e 20  it in the hash. 
13a7e 20 68 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65   h points to the
13a7f 20 66 69 72 73 74 20 2a 2f 0a 20 20 2f 2a 20 61   first */.  /* a
13a80 76 61 69 6c 61 62 6c 65 20 66 72 65 65 20 73 70  vailable free sp
13a81 6f 74 2e 20 63 68 65 63 6b 20 74 6f 20 73 65 65  ot. check to see
13a82 20 69 66 20 74 68 69 73 20 69 73 20 67 6f 69 6e   if this is goin
13a83 67 20 74 6f 20 2a 2f 0a 20 20 2f 2a 20 6d 61 6b  g to */.  /* mak
13a84 65 20 6f 75 72 20 68 61 73 68 20 74 6f 6f 20 22  e our hash too "
13a85 66 75 6c 6c 22 2e 20 20 2a 2f 0a 62 69 74 76 65  full".  */.bitve
13a86 63 5f 73 65 74 5f 72 65 68 61 73 68 3a 0a 20 20  c_set_rehash:.  
13a87 69 66 28 20 70 2d 3e 6e 53 65 74 3e 3d 42 49 54  if( p->nSet>=BIT
13a88 56 45 43 5f 4d 58 48 41 53 48 20 29 7b 0a 20 20  VEC_MXHASH ){.  
13a89 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6a    unsigned int j
13a8a 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  ;.    int rc;.  
13a8b 20 20 75 33 32 20 2a 61 69 56 61 6c 75 65 73 20    u32 *aiValues 
13a8c 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c  = sqlite3StackAl
13a8d 6c 6f 63 52 61 77 28 30 2c 20 73 69 7a 65 6f 66  locRaw(0, sizeof
13a8e 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20  (p->u.aHash));. 
13a8f 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 3d     if( aiValues=
13a90 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
13a91 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
13a92 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
13a93 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c 75 65    memcpy(aiValue
13a94 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20 73  s, p->u.aHash, s
13a95 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68  izeof(p->u.aHash
13a96 29 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74  ));.      memset
13a97 28 70 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c 20  (p->u.apSub, 0, 
13a98 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 70 53 75  sizeof(p->u.apSu
13a99 62 29 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 44  b));.      p->iD
13a9a 69 76 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53 69  ivisor = (p->iSi
13a9b 7a 65 20 2b 20 42 49 54 56 45 43 5f 4e 50 54 52  ze + BITVEC_NPTR
13a9c 20 2d 20 31 29 2f 42 49 54 56 45 43 5f 4e 50 54   - 1)/BITVEC_NPT
13a9d 52 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  R;.      rc = sq
13a9e 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70  lite3BitvecSet(p
13a9f 2c 20 69 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  , i);.      for(
13aa0 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49  j=0; j<BITVEC_NI
13aa1 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  NT; j++){.      
13aa2 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 5b 6a    if( aiValues[j
13aa3 5d 20 29 20 72 63 20 7c 3d 20 73 71 6c 69 74 65  ] ) rc |= sqlite
13aa4 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 61 69  3BitvecSet(p, ai
13aa5 56 61 6c 75 65 73 5b 6a 5d 29 3b 0a 20 20 20 20  Values[j]);.    
13aa6 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
13aa7 33 53 74 61 63 6b 46 72 65 65 28 30 2c 20 61 69  3StackFree(0, ai
13aa8 56 61 6c 75 65 73 29 3b 0a 20 20 20 20 20 20 72  Values);.      r
13aa9 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
13aaa 20 20 7d 0a 62 69 74 76 65 63 5f 73 65 74 5f 65    }.bitvec_set_e
13aab 6e 64 3a 0a 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b  nd:.  p->nSet++;
13aac 0a 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d  .  p->u.aHash[h]
13aad 20 3d 20 69 3b 0a 20 20 72 65 74 75 72 6e 20 53   = i;.  return S
13aae 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
13aaf 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 69 2d 74  ** Clear the i-t
13ab0 68 20 62 69 74 2e 0a 2a 2a 0a 2a 2a 20 70 42 75  h bit..**.** pBu
13ab1 66 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e  f must be a poin
13ab2 74 65 72 20 74 6f 20 61 74 20 6c 65 61 73 74 20  ter to at least 
13ab3 42 49 54 56 45 43 5f 53 5a 20 62 79 74 65 73 20  BITVEC_SZ bytes 
13ab4 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 73 74 6f  of temporary sto
13ab5 72 61 67 65 0a 2a 2a 20 74 68 61 74 20 42 69 74  rage.** that Bit
13ab6 76 65 63 43 6c 65 61 72 20 63 61 6e 20 75 73 65  vecClear can use
13ab7 20 74 6f 20 72 65 62 75 69 6c 74 20 69 74 73 20   to rebuilt its 
13ab8 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53  hash table..*/.S
13ab9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
13aba 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  id sqlite3Bitvec
13abb 43 6c 65 61 72 28 42 69 74 76 65 63 20 2a 70 2c  Clear(Bitvec *p,
13abc 20 75 33 32 20 69 2c 20 76 6f 69 64 20 2a 70 42   u32 i, void *pB
13abd 75 66 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20  uf){.  if( p==0 
13abe 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
13abf 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 69 2d 2d  rt( i>0 );.  i--
13ac0 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 69 44  ;.  while( p->iD
13ac1 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 33  ivisor ){.    u3
13ac2 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69  2 bin = i/p->iDi
13ac3 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69  visor;.    i = i
13ac4 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20  %p->iDivisor;.  
13ac5 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62    p = p->u.apSub
13ac6 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28 21  [bin];.    if (!
13ac7 70 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72  p) {.      retur
13ac8 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  n;.    }.  }.  i
13ac9 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54  f( p->iSize<=BIT
13aca 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20  VEC_NBIT ){.    
13acb 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42  p->u.aBitmap[i/B
13acc 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 3d  ITVEC_SZELEM] &=
13acd 20 7e 28 31 20 3c 3c 20 28 69 26 28 42 49 54 56   ~(1 << (i&(BITV
13ace 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 3b 0a  EC_SZELEM-1)));.
13acf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 73    }else{.    uns
13ad0 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20  igned int j;.   
13ad1 20 75 33 32 20 2a 61 69 56 61 6c 75 65 73 20 3d   u32 *aiValues =
13ad2 20 70 42 75 66 3b 0a 20 20 20 20 6d 65 6d 63 70   pBuf;.    memcp
13ad3 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e 75  y(aiValues, p->u
13ad4 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 70  .aHash, sizeof(p
13ad5 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20  ->u.aHash));.   
13ad6 20 6d 65 6d 73 65 74 28 70 2d 3e 75 2e 61 48 61   memset(p->u.aHa
13ad7 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d  sh, 0, sizeof(p-
13ad8 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 20  >u.aHash));.    
13ad9 70 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20 20  p->nSet = 0;.   
13ada 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 54 56   for(j=0; j<BITV
13adb 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a 20  EC_NINT; j++){. 
13adc 20 20 20 20 20 69 66 28 20 61 69 56 61 6c 75 65       if( aiValue
13add 73 5b 6a 5d 20 26 26 20 61 69 56 61 6c 75 65 73  s[j] && aiValues
13ade 5b 6a 5d 21 3d 28 69 2b 31 29 20 29 7b 0a 20 20  [j]!=(i+1) ){.  
13adf 20 20 20 20 20 20 75 33 32 20 68 20 3d 20 42 49        u32 h = BI
13ae0 54 56 45 43 5f 48 41 53 48 28 61 69 56 61 6c 75  TVEC_HASH(aiValu
13ae1 65 73 5b 6a 5d 2d 31 29 3b 0a 20 20 20 20 20 20  es[j]-1);.      
13ae2 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 20    p->nSet++;.   
13ae3 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 75       while( p->u
13ae4 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20  .aHash[h] ){.   
13ae5 20 20 20 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20         h++;.    
13ae6 20 20 20 20 20 20 69 66 28 20 68 3e 3d 42 49 54        if( h>=BIT
13ae7 56 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30  VEC_NINT ) h = 0
13ae8 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
13ae9 20 20 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68      p->u.aHash[h
13aea 5d 20 3d 20 61 69 56 61 6c 75 65 73 5b 6a 5d 3b  ] = aiValues[j];
13aeb 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
13aec 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74   }.}../*.** Dest
13aed 72 6f 79 20 61 20 62 69 74 6d 61 70 20 6f 62 6a  roy a bitmap obj
13aee 65 63 74 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c  ect.  Reclaim al
13aef 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 2e 0a 2a  l memory used..*
13af0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13af1 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74   void sqlite3Bit
13af2 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76 65  vecDestroy(Bitve
13af3 63 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d  c *p){.  if( p==
13af4 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
13af5 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b  ( p->iDivisor ){
13af6 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  .    unsigned in
13af7 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
13af8 3b 20 69 3c 42 49 54 56 45 43 5f 4e 50 54 52 3b  ; i<BITVEC_NPTR;
13af9 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c   i++){.      sql
13afa 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f  ite3BitvecDestro
13afb 79 28 70 2d 3e 75 2e 61 70 53 75 62 5b 69 5d 29  y(p->u.apSub[i])
13afc 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
13afd 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d  lite3_free(p);.}
13afe 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
13aff 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
13b00 69 53 69 7a 65 20 70 61 72 61 6d 65 74 65 72 20  iSize parameter 
13b01 73 70 65 63 69 66 69 65 64 20 77 68 65 6e 20 42  specified when B
13b02 69 74 76 65 63 20 2a 70 0a 2a 2a 20 77 61 73 20  itvec *p.** was 
13b03 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  created..*/.SQLI
13b04 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73  TE_PRIVATE u32 s
13b05 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a 65  qlite3BitvecSize
13b06 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20 20 72  (Bitvec *p){.  r
13b07 65 74 75 72 6e 20 70 2d 3e 69 53 69 7a 65 3b 0a  eturn p->iSize;.
13b08 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
13b09 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54  E_OMIT_BUILTIN_T
13b0a 45 53 54 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 56 5b  EST./*.** Let V[
13b0b 5d 20 62 65 20 61 6e 20 61 72 72 61 79 20 6f 66  ] be an array of
13b0c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63   unsigned charac
13b0d 74 65 72 73 20 73 75 66 66 69 63 69 65 6e 74 20  ters sufficient 
13b0e 74 6f 20 68 6f 6c 64 0a 2a 2a 20 75 70 20 74 6f  to hold.** up to
13b0f 20 4e 20 62 69 74 73 2e 20 20 4c 65 74 20 49 20   N bits.  Let I 
13b10 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65  be an integer be
13b11 74 77 65 65 6e 20 30 20 61 6e 64 20 4e 2e 20 20  tween 0 and N.  
13b12 30 3c 3d 49 3c 4e 2e 0a 2a 2a 20 54 68 65 6e 20  0<=I<N..** Then 
13b13 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  the following ma
13b14 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64  cros can be used
13b15 20 74 6f 20 73 65 74 2c 20 63 6c 65 61 72 2c 20   to set, clear, 
13b16 6f 72 20 74 65 73 74 0a 2a 2a 20 69 6e 64 69 76  or test.** indiv
13b17 69 64 75 61 6c 20 62 69 74 73 20 77 69 74 68 69  idual bits withi
13b18 6e 20 56 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  n V..*/.#define 
13b19 53 45 54 42 49 54 28 56 2c 49 29 20 20 20 20 20  SETBIT(V,I)     
13b1a 20 56 5b 49 3e 3e 33 5d 20 7c 3d 20 28 31 3c 3c   V[I>>3] |= (1<<
13b1b 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20 43  (I&7)).#define C
13b1c 4c 45 41 52 42 49 54 28 56 2c 49 29 20 20 20 20  LEARBIT(V,I)    
13b1d 56 5b 49 3e 3e 33 5d 20 26 3d 20 7e 28 31 3c 3c  V[I>>3] &= ~(1<<
13b1e 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20 54  (I&7)).#define T
13b1f 45 53 54 42 49 54 28 56 2c 49 29 20 20 20 20 20  ESTBIT(V,I)     
13b20 28 56 5b 49 3e 3e 33 5d 26 28 31 3c 3c 28 49 26  (V[I>>3]&(1<<(I&
13b21 37 29 29 29 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20 54  7)))!=0../*.** T
13b22 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73  his routine runs
13b23 20 61 6e 20 65 78 74 65 6e 73 69 76 65 20 74 65   an extensive te
13b24 73 74 20 6f 66 20 74 68 65 20 42 69 74 76 65 63  st of the Bitvec
13b25 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   code..**.** The
13b26 20 69 6e 70 75 74 20 69 73 20 61 6e 20 61 72 72   input is an arr
13b27 61 79 20 6f 66 20 69 6e 74 65 67 65 72 73 20 74  ay of integers t
13b28 68 61 74 20 61 63 74 73 20 61 73 20 61 20 70 72  hat acts as a pr
13b29 6f 67 72 61 6d 0a 2a 2a 20 74 6f 20 74 65 73 74  ogram.** to test
13b2a 20 74 68 65 20 42 69 74 76 65 63 2e 20 20 54 68   the Bitvec.  Th
13b2b 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20 6f  e integers are o
13b2c 70 63 6f 64 65 73 20 66 6f 6c 6c 6f 77 65 64 0a  pcodes followed.
13b2d 2a 2a 20 62 79 20 30 2c 20 31 2c 20 6f 72 20 33  ** by 0, 1, or 3
13b2e 20 6f 70 65 72 61 6e 64 73 2c 20 64 65 70 65 6e   operands, depen
13b2f 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 63 6f  ding on the opco
13b30 64 65 2e 20 20 41 6e 6f 74 68 65 72 0a 2a 2a 20  de.  Another.** 
13b31 6f 70 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 69  opcode follows i
13b32 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72  mmediately after
13b33 20 74 68 65 20 6c 61 73 74 20 6f 70 65 72 61 6e   the last operan
13b34 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61  d..**.** There a
13b35 72 65 20 36 20 6f 70 63 6f 64 65 73 20 6e 75 6d  re 6 opcodes num
13b36 62 65 72 65 64 20 66 72 6f 6d 20 30 20 74 68 72  bered from 0 thr
13b37 6f 75 67 68 20 35 2e 20 20 30 20 69 73 20 74 68  ough 5.  0 is th
13b38 65 0a 2a 2a 20 22 68 61 6c 74 22 20 6f 70 63 6f  e.** "halt" opco
13b39 64 65 20 61 6e 64 20 63 61 75 73 65 73 20 74 68  de and causes th
13b3a 65 20 74 65 73 74 20 74 6f 20 65 6e 64 2e 0a 2a  e test to end..*
13b3b 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20 20  *.**    0       
13b3c 20 20 20 48 61 6c 74 20 61 6e 64 20 72 65 74 75     Halt and retu
13b3d 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
13b3e 20 65 72 72 6f 72 73 0a 2a 2a 20 20 20 20 31 20   errors.**    1 
13b3f 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e 20 62  N S X    Set N b
13b40 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  its beginning wi
13b41 74 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d 65  th S and increme
13b42 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20  nting by X.**   
13b43 20 32 20 4e 20 53 20 58 20 20 20 20 43 6c 65 61   2 N S X    Clea
13b44 72 20 4e 20 62 69 74 73 20 62 65 67 69 6e 6e 69  r N bits beginni
13b45 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20 69 6e  ng with S and in
13b46 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 58 0a  crementing by X.
13b47 2a 2a 20 20 20 20 33 20 4e 20 20 20 20 20 20 20  **    3 N       
13b48 20 53 65 74 20 4e 20 72 61 6e 64 6f 6d 6c 79 20   Set N randomly 
13b49 63 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20  chosen bits.**  
13b4a 20 20 34 20 4e 20 20 20 20 20 20 20 20 43 6c 65    4 N        Cle
13b4b 61 72 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68  ar N randomly ch
13b4c 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20  osen bits.**    
13b4d 35 20 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e  5 N S X    Set N
13b4e 20 62 69 74 73 20 66 72 6f 6d 20 53 20 69 6e 63   bits from S inc
13b4f 72 65 6d 65 6e 74 20 58 20 69 6e 20 61 72 72 61  rement X in arra
13b50 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 69 6e 20 62  y only, not in b
13b51 69 74 76 65 63 0a 2a 2a 0a 2a 2a 20 54 68 65 20  itvec.**.** The 
13b52 6f 70 63 6f 64 65 73 20 31 20 74 68 72 6f 75 67  opcodes 1 throug
13b53 68 20 34 20 70 65 72 66 6f 72 6d 20 73 65 74 20  h 4 perform set 
13b54 61 6e 64 20 63 6c 65 61 72 20 6f 70 65 72 61 74  and clear operat
13b55 69 6f 6e 73 20 61 72 65 20 70 65 72 66 6f 72 6d  ions are perform
13b56 65 64 0a 2a 2a 20 6f 6e 20 62 6f 74 68 20 61 20  ed.** on both a 
13b57 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 61 6e  Bitvec object an
13b58 64 20 6f 6e 20 61 20 6c 69 6e 65 61 72 20 61 72  d on a linear ar
13b59 72 61 79 20 6f 66 20 62 69 74 73 20 6f 62 74 61  ray of bits obta
13b5a 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  ined from malloc
13b5b 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 77 6f  ..** Opcode 5 wo
13b5c 72 6b 73 20 6f 6e 20 74 68 65 20 6c 69 6e 65 61  rks on the linea
13b5d 72 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e 6f  r array only, no
13b5e 74 20 6f 6e 20 74 68 65 20 42 69 74 76 65 63 2e  t on the Bitvec.
13b5f 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 69 73 20  .** Opcode 5 is 
13b60 75 73 65 64 20 74 6f 20 64 65 6c 69 62 65 72 61  used to delibera
13b61 74 65 6c 79 20 69 6e 64 75 63 65 20 61 20 66 61  tely induce a fa
13b62 75 6c 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a  ult in order to.
13b63 2a 2a 20 63 6f 6e 66 69 72 6d 20 74 68 61 74 20  ** confirm that 
13b64 65 72 72 6f 72 20 64 65 74 65 63 74 69 6f 6e 20  error detection 
13b65 77 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 20  works..**.** At 
13b66 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f  the conclusion o
13b67 66 20 74 68 65 20 74 65 73 74 20 74 68 65 20 6c  f the test the l
13b68 69 6e 65 61 72 20 61 72 72 61 79 20 69 73 20 63  inear array is c
13b69 6f 6d 70 61 72 65 64 0a 2a 2a 20 61 67 61 69 6e  ompared.** again
13b6a 73 74 20 74 68 65 20 42 69 74 76 65 63 20 6f 62  st the Bitvec ob
13b6b 6a 65 63 74 2e 20 20 49 66 20 74 68 65 72 65 20  ject.  If there 
13b6c 61 72 65 20 61 6e 79 20 64 69 66 66 65 72 65 6e  are any differen
13b6d 63 65 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72  ces,.** an error
13b6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49   is returned.  I
13b6f 66 20 74 68 65 79 20 61 72 65 20 74 68 65 20 73  f they are the s
13b70 61 6d 65 2c 20 7a 65 72 6f 20 69 73 20 72 65 74  ame, zero is ret
13b71 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
13b72 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
13b73 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ion error occurs
13b74 2c 20 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a  , return -1..*/.
13b75 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
13b76 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  nt sqlite3Bitvec
13b77 42 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 20  BuiltinTest(int 
13b78 73 7a 2c 20 69 6e 74 20 2a 61 4f 70 29 7b 0a 20  sz, int *aOp){. 
13b79 20 42 69 74 76 65 63 20 2a 70 42 69 74 76 65 63   Bitvec *pBitvec
13b7a 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64   = 0;.  unsigned
13b7b 20 63 68 61 72 20 2a 70 56 20 3d 20 30 3b 0a 20   char *pV = 0;. 
13b7c 20 69 6e 74 20 72 63 20 3d 20 2d 31 3b 0a 20 20   int rc = -1;.  
13b7d 69 6e 74 20 69 2c 20 6e 78 2c 20 70 63 2c 20 6f  int i, nx, pc, o
13b7e 70 3b 0a 20 20 76 6f 69 64 20 2a 70 54 6d 70 53  p;.  void *pTmpS
13b7f 70 61 63 65 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  pace;..  /* Allo
13b80 63 61 74 65 20 74 68 65 20 42 69 74 76 65 63 20  cate the Bitvec 
13b81 74 6f 20 62 65 20 74 65 73 74 65 64 20 61 6e 64  to be tested and
13b82 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20   a linear array 
13b83 6f 66 0a 20 20 2a 2a 20 62 69 74 73 20 74 6f 20  of.  ** bits to 
13b84 61 63 74 20 61 73 20 74 68 65 20 72 65 66 65 72  act as the refer
13b85 65 6e 63 65 20 2a 2f 0a 20 20 70 42 69 74 76 65  ence */.  pBitve
13b86 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  c = sqlite3Bitve
13b87 63 43 72 65 61 74 65 28 20 73 7a 20 29 3b 0a 20  cCreate( sz );. 
13b88 20 70 56 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c   pV = sqlite3Mal
13b89 6c 6f 63 5a 65 72 6f 28 20 28 73 7a 2b 37 29 2f  locZero( (sz+7)/
13b8a 38 20 2b 20 31 20 29 3b 0a 20 20 70 54 6d 70 53  8 + 1 );.  pTmpS
13b8b 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d  pace = sqlite3_m
13b8c 61 6c 6c 6f 63 28 42 49 54 56 45 43 5f 53 5a 29  alloc(BITVEC_SZ)
13b8d 3b 0a 20 20 69 66 28 20 70 42 69 74 76 65 63 3d  ;.  if( pBitvec=
13b8e 3d 30 20 7c 7c 20 70 56 3d 3d 30 20 7c 7c 20 70  =0 || pV==0 || p
13b8f 54 6d 70 53 70 61 63 65 3d 3d 30 20 20 29 20 67  TmpSpace==0  ) g
13b90 6f 74 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a  oto bitvec_end;.
13b91 0a 20 20 2f 2a 20 4e 55 4c 4c 20 70 42 69 74 76  .  /* NULL pBitv
13b92 65 63 20 74 65 73 74 73 20 2a 2f 0a 20 20 73 71  ec tests */.  sq
13b93 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 30  lite3BitvecSet(0
13b94 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  , 1);.  sqlite3B
13b95 69 74 76 65 63 43 6c 65 61 72 28 30 2c 20 31 2c  itvecClear(0, 1,
13b96 20 70 54 6d 70 53 70 61 63 65 29 3b 0a 0a 20 20   pTmpSpace);..  
13b97 2f 2a 20 52 75 6e 20 74 68 65 20 70 72 6f 67 72  /* Run the progr
13b98 61 6d 20 2a 2f 0a 20 20 70 63 20 3d 20 30 3b 0a  am */.  pc = 0;.
13b99 20 20 77 68 69 6c 65 28 20 28 6f 70 20 3d 20 61    while( (op = a
13b9a 4f 70 5b 70 63 5d 29 21 3d 30 20 29 7b 0a 20 20  Op[pc])!=0 ){.  
13b9b 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
13b9c 20 20 20 20 20 20 63 61 73 65 20 31 3a 0a 20 20        case 1:.  
13b9d 20 20 20 20 63 61 73 65 20 32 3a 0a 20 20 20 20      case 2:.    
13b9e 20 20 63 61 73 65 20 35 3a 20 7b 0a 20 20 20 20    case 5: {.    
13b9f 20 20 20 20 6e 78 20 3d 20 34 3b 0a 20 20 20 20      nx = 4;.    
13ba0 20 20 20 20 69 20 3d 20 61 4f 70 5b 70 63 2b 32      i = aOp[pc+2
13ba1 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 61  ] - 1;.        a
13ba2 4f 70 5b 70 63 2b 32 5d 20 2b 3d 20 61 4f 70 5b  Op[pc+2] += aOp[
13ba3 70 63 2b 33 5d 3b 0a 20 20 20 20 20 20 20 20 62  pc+3];.        b
13ba4 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
13ba5 20 20 20 20 63 61 73 65 20 33 3a 0a 20 20 20 20      case 3:.    
13ba6 20 20 63 61 73 65 20 34 3a 20 0a 20 20 20 20 20    case 4: .     
13ba7 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
13ba8 20 20 20 20 6e 78 20 3d 20 32 3b 0a 20 20 20 20      nx = 2;.    
13ba9 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64      sqlite3_rand
13baa 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 69 29  omness(sizeof(i)
13bab 2c 20 26 69 29 3b 0a 20 20 20 20 20 20 20 20 62  , &i);.        b
13bac 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
13bad 20 20 7d 0a 20 20 20 20 69 66 28 20 28 2d 2d 61    }.    if( (--a
13bae 4f 70 5b 70 63 2b 31 5d 29 20 3e 20 30 20 29 20  Op[pc+1]) > 0 ) 
13baf 6e 78 20 3d 20 30 3b 0a 20 20 20 20 70 63 20 2b  nx = 0;.    pc +
13bb0 3d 20 6e 78 3b 0a 20 20 20 20 69 20 3d 20 28 69  = nx;.    i = (i
13bb1 20 26 20 30 78 37 66 66 66 66 66 66 66 29 25 73   & 0x7fffffff)%s
13bb2 7a 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 20 26  z;.    if( (op &
13bb3 20 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20   1)!=0 ){.      
13bb4 53 45 54 42 49 54 28 70 56 2c 20 28 69 2b 31 29  SETBIT(pV, (i+1)
13bb5 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 21  );.      if( op!
13bb6 3d 35 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  =5 ){.        if
13bb7 28 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53  ( sqlite3BitvecS
13bb8 65 74 28 70 42 69 74 76 65 63 2c 20 69 2b 31 29  et(pBitvec, i+1)
13bb9 20 29 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 65   ) goto bitvec_e
13bba 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  nd;.      }.    
13bbb 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 43 4c 45  }else{.      CLE
13bbc 41 52 42 49 54 28 70 56 2c 20 28 69 2b 31 29 29  ARBIT(pV, (i+1))
13bbd 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ;.      sqlite3B
13bbe 69 74 76 65 63 43 6c 65 61 72 28 70 42 69 74 76  itvecClear(pBitv
13bbf 65 63 2c 20 69 2b 31 2c 20 70 54 6d 70 53 70 61  ec, i+1, pTmpSpa
13bc0 63 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ce);.    }.  }..
13bc1 20 20 2f 2a 20 54 65 73 74 20 74 6f 20 6d 61 6b    /* Test to mak
13bc2 65 20 73 75 72 65 20 74 68 65 20 6c 69 6e 65 61  e sure the linea
13bc3 72 20 61 72 72 61 79 20 65 78 61 63 74 6c 79 20  r array exactly 
13bc4 6d 61 74 63 68 65 73 20 74 68 65 0a 20 20 2a 2a  matches the.  **
13bc5 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20   Bitvec object. 
13bc6 20 53 74 61 72 74 20 77 69 74 68 20 74 68 65 20   Start with the 
13bc7 61 73 73 75 6d 70 74 69 6f 6e 20 74 68 61 74 20  assumption that 
13bc8 74 68 65 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 74  they do.  ** mat
13bc9 63 68 20 28 72 63 3d 3d 30 29 2e 20 20 43 68 61  ch (rc==0).  Cha
13bca 6e 67 65 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a 65  nge rc to non-ze
13bcb 72 6f 20 69 66 20 61 20 64 69 73 63 72 65 70 61  ro if a discrepa
13bcc 6e 63 79 0a 20 20 2a 2a 20 69 73 20 66 6f 75 6e  ncy.  ** is foun
13bcd 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73  d..  */.  rc = s
13bce 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
13bcf 28 30 2c 30 29 20 2b 20 73 71 6c 69 74 65 33 42  (0,0) + sqlite3B
13bd0 69 74 76 65 63 54 65 73 74 28 70 42 69 74 76 65  itvecTest(pBitve
13bd1 63 2c 20 73 7a 2b 31 29 0a 20 20 20 20 20 20 20  c, sz+1).       
13bd2 20 20 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76     + sqlite3Bitv
13bd3 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 20  ecTest(pBitvec, 
13bd4 30 29 0a 20 20 20 20 20 20 20 20 20 20 2b 20 28  0).          + (
13bd5 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a  sqlite3BitvecSiz
13bd6 65 28 70 42 69 74 76 65 63 29 20 2d 20 73 7a 29  e(pBitvec) - sz)
13bd7 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d  ;.  for(i=1; i<=
13bd8 73 7a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  sz; i++){.    if
13bd9 28 20 20 28 54 45 53 54 42 49 54 28 70 56 2c 69  (  (TESTBIT(pV,i
13bda 29 29 21 3d 73 71 6c 69 74 65 33 42 69 74 76 65  ))!=sqlite3Bitve
13bdb 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 69 29  cTest(pBitvec,i)
13bdc 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69   ){.      rc = i
13bdd 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
13bde 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46     }.  }..  /* F
13bdf 72 65 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 74  ree allocated st
13be0 72 75 63 74 75 72 65 20 2a 2f 0a 62 69 74 76 65  ructure */.bitve
13be1 63 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33  c_end:.  sqlite3
13be2 5f 66 72 65 65 28 70 54 6d 70 53 70 61 63 65 29  _free(pTmpSpace)
13be3 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
13be4 28 70 56 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  (pV);.  sqlite3B
13be5 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 42 69  itvecDestroy(pBi
13be6 74 76 65 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  tvec);.  return 
13be7 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  rc;.}.#endif /* 
13be8 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
13be9 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a  TIN_TEST */../**
13bea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
13beb 20 6f 66 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a   of bitvec.c ***
13bec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13bed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13bee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
13bef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
13bf0 69 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e 63  in file pcache.c
13bf1 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
13bf2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13bf3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
13bf4 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30  ** 2008 August 0
13bf5 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
13bf6 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
13bf7 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
13bf8 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
13bf9 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
13bfa 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
13bfb 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
13bfc 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
13bfd 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
13bfe 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
13bff 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
13c00 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
13c01 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
13c02 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
13c03 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
13c04 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
13c05 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
13c06 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
13c07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13c08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13c09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13c0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13c0b 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d  .** This file im
13c0c 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 70 61  plements that pa
13c0d 67 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 0a 2f 2a  ge cache..*/../*
13c0e 0a 2a 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 70  .** A complete p
13c0f 61 67 65 20 63 61 63 68 65 20 69 73 20 61 6e 20  age cache is an 
13c10 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
13c11 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
13c12 74 72 75 63 74 20 50 43 61 63 68 65 20 7b 0a 20  truct PCache {. 
13c13 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 2c 20   PgHdr *pDirty, 
13c14 2a 70 44 69 72 74 79 54 61 69 6c 3b 20 20 20 20  *pDirtyTail;    
13c15 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
13c16 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 4c  dirty pages in L
13c17 52 55 20 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67  RU order */.  Pg
13c18 48 64 72 20 2a 70 53 79 6e 63 65 64 3b 20 20 20  Hdr *pSynced;   
13c19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13c1a 20 20 2f 2a 20 4c 61 73 74 20 73 79 6e 63 65 64    /* Last synced
13c1b 20 70 61 67 65 20 69 6e 20 64 69 72 74 79 20 70   page in dirty p
13c1c 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  age list */.  in
13c1d 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  t nRef;         
13c1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13c1f 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
13c20 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 20  eferenced pages 
13c21 2a 2f 0a 20 20 69 6e 74 20 73 7a 43 61 63 68 65  */.  int szCache
13c22 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
13c23 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66           /* Conf
13c24 69 67 75 72 65 64 20 63 61 63 68 65 20 73 69 7a  igured cache siz
13c25 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67  e */.  int szPag
13c26 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
13c27 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
13c28 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65  ze of every page
13c29 20 69 6e 20 74 68 69 73 20 63 61 63 68 65 20 2a   in this cache *
13c2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61 3b  /.  int szExtra;
13c2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13c2c 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
13c2d 6f 66 20 65 78 74 72 61 20 73 70 61 63 65 20 66  of extra space f
13c2e 6f 72 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  or each page */.
13c2f 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65    int bPurgeable
13c30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
13c31 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
13c32 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61   pages are on ba
13c33 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20  cking store */. 
13c34 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28   int (*xStress)(
13c35 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 3b 20 20  void*,PgHdr*);  
13c36 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 6f 20       /* Call to 
13c37 74 72 79 20 6d 61 6b 65 20 61 20 70 61 67 65 20  try make a page 
13c38 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  clean */.  void 
13c39 2a 70 53 74 72 65 73 73 3b 20 20 20 20 20 20 20  *pStress;       
13c3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
13c3b 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53  * Argument to xS
13c3c 74 72 65 73 73 20 2a 2f 0a 20 20 73 71 6c 69 74  tress */.  sqlit
13c3d 65 33 5f 70 63 61 63 68 65 20 2a 70 43 61 63 68  e3_pcache *pCach
13c3e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
13c3f 2a 20 50 6c 75 67 67 61 62 6c 65 20 63 61 63 68  * Pluggable cach
13c40 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 50 67  e module */.  Pg
13c41 48 64 72 20 2a 70 50 61 67 65 31 3b 20 20 20 20  Hdr *pPage1;    
13c42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13c43 20 20 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74    /* Reference t
13c44 6f 20 70 61 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a  o page 1 */.};..
13c45 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68  /*.** Some of th
13c46 65 20 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f  e assert() macro
13c47 73 20 69 6e 20 74 68 69 73 20 63 6f 64 65 20 61  s in this code a
13c48 72 65 20 74 6f 6f 20 65 78 70 65 6e 73 69 76 65  re too expensive
13c49 20 74 6f 20 72 75 6e 0a 2a 2a 20 65 76 65 6e 20   to run.** even 
13c4a 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 64 65  during normal de
13c4b 62 75 67 67 69 6e 67 2e 20 20 55 73 65 20 74 68  bugging.  Use th
13c4c 65 6d 20 6f 6e 6c 79 20 72 61 72 65 6c 79 20 6f  em only rarely o
13c4d 6e 20 6c 6f 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a  n long-running.*
13c4e 2a 20 74 65 73 74 73 2e 20 20 45 6e 61 62 6c 65  * tests.  Enable
13c4f 20 74 68 65 20 65 78 70 65 6e 73 69 76 65 20 61   the expensive a
13c50 73 73 65 72 74 73 20 75 73 69 6e 67 20 74 68 65  sserts using the
13c51 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f 45 4e 41  .** -DSQLITE_ENA
13c52 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53  BLE_EXPENSIVE_AS
13c53 53 45 52 54 3d 31 20 63 6f 6d 70 69 6c 65 2d 74  SERT=1 compile-t
13c54 69 6d 65 20 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23  ime option..*/.#
13c55 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
13c56 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53  BLE_EXPENSIVE_AS
13c57 53 45 52 54 0a 23 20 64 65 66 69 6e 65 20 65 78  SERT.# define ex
13c58 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58  pensive_assert(X
13c59 29 20 20 61 73 73 65 72 74 28 58 29 0a 23 65 6c  )  assert(X).#el
13c5a 73 65 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65  se.# define expe
13c5b 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58 29 0a  nsive_assert(X).
13c5c 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
13c5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13c5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b  *********** Link
13c5f 65 64 20 4c 69 73 74 20 4d 61 6e 61 67 65 6d 65  ed List Manageme
13c60 6e 74 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt *************
13c61 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64  *******/..#if !d
13c62 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26  efined(NDEBUG) &
13c63 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
13c64 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56  _ENABLE_EXPENSIV
13c65 45 5f 41 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20  E_ASSERT)./*.** 
13c66 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70  Check that the p
13c67 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 76  Cache->pSynced v
13c68 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 63  ariable is set c
13c69 6f 72 72 65 63 74 6c 79 2e 20 49 66 20 69 74 0a  orrectly. If it.
13c6a 2a 2a 20 69 73 20 6e 6f 74 2c 20 65 69 74 68 65  ** is not, eithe
13c6b 72 20 66 61 69 6c 20 61 6e 20 61 73 73 65 72 74  r fail an assert
13c6c 20 6f 72 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e   or return zero.
13c6d 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75   Otherwise, retu
13c6e 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20  rn.** non-zero. 
13c6f 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65  This is only use
13c70 64 20 69 6e 20 64 65 62 75 67 67 69 6e 67 20 62  d in debugging b
13c71 75 69 6c 64 73 2c 20 61 73 20 66 6f 6c 6c 6f 77  uilds, as follow
13c72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 65 78 70 65 6e  s:.**.**   expen
13c73 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 63 61  sive_assert( pca
13c74 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70  cheCheckSynced(p
13c75 43 61 63 68 65 29 20 29 3b 0a 2a 2f 0a 73 74 61  Cache) );.*/.sta
13c76 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 43 68  tic int pcacheCh
13c77 65 63 6b 53 79 6e 63 65 64 28 50 43 61 63 68 65  eckSynced(PCache
13c78 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48   *pCache){.  PgH
13c79 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70  dr *p;.  for(p=p
13c7a 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69  Cache->pDirtyTai
13c7b 6c 3b 20 70 21 3d 70 43 61 63 68 65 2d 3e 70 53  l; p!=pCache->pS
13c7c 79 6e 63 65 64 3b 20 70 3d 70 2d 3e 70 44 69 72  ynced; p=p->pDir
13c7d 74 79 50 72 65 76 29 7b 0a 20 20 20 20 61 73 73  tyPrev){.    ass
13c7e 65 72 74 28 20 70 2d 3e 6e 52 65 66 20 7c 7c 20  ert( p->nRef || 
13c7f 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  (p->flags&PGHDR_
13c80 4e 45 45 44 5f 53 59 4e 43 29 20 29 3b 0a 20 20  NEED_SYNC) );.  
13c81 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 3d 3d 30  }.  return (p==0
13c82 20 7c 7c 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28   || p->nRef || (
13c83 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  p->flags&PGHDR_N
13c84 45 45 44 5f 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d  EED_SYNC)==0);.}
13c85 0a 23 65 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42  .#endif /* !NDEB
13c86 55 47 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41  UG && SQLITE_ENA
13c87 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53  BLE_EXPENSIVE_AS
13c88 53 45 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52  SERT */../*.** R
13c89 65 6d 6f 76 65 20 70 61 67 65 20 70 50 61 67 65  emove page pPage
13c8a 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f   from the list o
13c8b 66 20 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a  f dirty pages..*
13c8c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
13c8d 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69  acheRemoveFromDi
13c8e 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70  rtyList(PgHdr *p
13c8f 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20  Page){.  PCache 
13c90 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63  *p = pPage->pCac
13c91 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  he;..  assert( p
13c92 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74  Page->pDirtyNext
13c93 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44   || pPage==p->pD
13c94 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 61 73  irtyTail );.  as
13c95 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69  sert( pPage->pDi
13c96 72 74 79 50 72 65 76 20 7c 7c 20 70 50 61 67 65  rtyPrev || pPage
13c97 3d 3d 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 0a  ==p->pDirty );..
13c98 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
13c99 50 43 61 63 68 65 31 2e 70 53 79 6e 63 65 64 20  PCache1.pSynced 
13c9a 76 61 72 69 61 62 6c 65 20 69 66 20 6e 65 63 65  variable if nece
13c9b 73 73 61 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20  ssary. */.  if( 
13c9c 70 2d 3e 70 53 79 6e 63 65 64 3d 3d 70 50 61 67  p->pSynced==pPag
13c9d 65 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a  e ){.    PgHdr *
13c9e 70 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 2d  pSynced = pPage-
13c9f 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20 20  >pDirtyPrev;.   
13ca0 20 77 68 69 6c 65 28 20 70 53 79 6e 63 65 64 20   while( pSynced 
13ca1 26 26 20 28 70 53 79 6e 63 65 64 2d 3e 66 6c 61  && (pSynced->fla
13ca2 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
13ca3 4e 43 29 20 29 7b 0a 20 20 20 20 20 20 70 53 79  NC) ){.      pSy
13ca4 6e 63 65 64 20 3d 20 70 53 79 6e 63 65 64 2d 3e  nced = pSynced->
13ca5 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20  pDirtyPrev;.    
13ca6 7d 0a 20 20 20 20 70 2d 3e 70 53 79 6e 63 65 64  }.    p->pSynced
13ca7 20 3d 20 70 53 79 6e 63 65 64 3b 0a 20 20 7d 0a   = pSynced;.  }.
13ca8 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44  .  if( pPage->pD
13ca9 69 72 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20  irtyNext ){.    
13caa 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78  pPage->pDirtyNex
13cab 74 2d 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20  t->pDirtyPrev = 
13cac 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65  pPage->pDirtyPre
13cad 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  v;.  }else{.    
13cae 61 73 73 65 72 74 28 20 70 50 61 67 65 3d 3d 70  assert( pPage==p
13caf 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 29 3b 0a  ->pDirtyTail );.
13cb0 20 20 20 20 70 2d 3e 70 44 69 72 74 79 54 61 69      p->pDirtyTai
13cb1 6c 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74  l = pPage->pDirt
13cb2 79 50 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28  yPrev;.  }.  if(
13cb3 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72   pPage->pDirtyPr
13cb4 65 76 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d  ev ){.    pPage-
13cb5 3e 70 44 69 72 74 79 50 72 65 76 2d 3e 70 44 69  >pDirtyPrev->pDi
13cb6 72 74 79 4e 65 78 74 20 3d 20 70 50 61 67 65 2d  rtyNext = pPage-
13cb7 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d  >pDirtyNext;.  }
13cb8 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
13cb9 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72  ( pPage==p->pDir
13cba 74 79 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69  ty );.    p->pDi
13cbb 72 74 79 20 3d 20 70 50 61 67 65 2d 3e 70 44 69  rty = pPage->pDi
13cbc 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70  rtyNext;.  }.  p
13cbd 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74  Page->pDirtyNext
13cbe 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 70   = 0;.  pPage->p
13cbf 44 69 72 74 79 50 72 65 76 20 3d 20 30 3b 0a 0a  DirtyPrev = 0;..
13cc0 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65    expensive_asse
13cc1 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53  rt( pcacheCheckS
13cc2 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f  ynced(p) );.}../
13cc3 2a 0a 2a 2a 20 41 64 64 20 70 61 67 65 20 70 50  *.** Add page pP
13cc4 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20  age to the head 
13cc5 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c 69 73  of the dirty lis
13cc6 74 20 28 50 43 61 63 68 65 31 2e 70 44 69 72 74  t (PCache1.pDirt
13cc7 79 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 70  y is set to.** p
13cc8 50 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  Page)..*/.static
13cc9 20 76 6f 69 64 20 70 63 61 63 68 65 41 64 64 54   void pcacheAddT
13cca 6f 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72  oDirtyList(PgHdr
13ccb 20 2a 70 50 61 67 65 29 7b 0a 20 20 50 43 61 63   *pPage){.  PCac
13ccc 68 65 20 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70  he *p = pPage->p
13ccd 43 61 63 68 65 3b 0a 0a 20 20 61 73 73 65 72 74  Cache;..  assert
13cce 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e  ( pPage->pDirtyN
13ccf 65 78 74 3d 3d 30 20 26 26 20 70 50 61 67 65 2d  ext==0 && pPage-
13cd0 3e 70 44 69 72 74 79 50 72 65 76 3d 3d 30 20 26  >pDirtyPrev==0 &
13cd1 26 20 70 2d 3e 70 44 69 72 74 79 21 3d 70 50 61  & p->pDirty!=pPa
13cd2 67 65 20 29 3b 0a 0a 20 20 70 50 61 67 65 2d 3e  ge );..  pPage->
13cd3 70 44 69 72 74 79 4e 65 78 74 20 3d 20 70 2d 3e  pDirtyNext = p->
13cd4 70 44 69 72 74 79 3b 0a 20 20 69 66 28 20 70 50  pDirty;.  if( pP
13cd5 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20  age->pDirtyNext 
13cd6 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
13cd7 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74  Page->pDirtyNext
13cd8 2d 3e 70 44 69 72 74 79 50 72 65 76 3d 3d 30 20  ->pDirtyPrev==0 
13cd9 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44  );.    pPage->pD
13cda 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79  irtyNext->pDirty
13cdb 50 72 65 76 20 3d 20 70 50 61 67 65 3b 0a 20 20  Prev = pPage;.  
13cdc 7d 0a 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20  }.  p->pDirty = 
13cdd 70 50 61 67 65 3b 0a 20 20 69 66 28 20 21 70 2d  pPage;.  if( !p-
13cde 3e 70 44 69 72 74 79 54 61 69 6c 20 29 7b 0a 20  >pDirtyTail ){. 
13cdf 20 20 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c     p->pDirtyTail
13ce0 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20   = pPage;.  }.  
13ce1 69 66 28 20 21 70 2d 3e 70 53 79 6e 63 65 64 20  if( !p->pSynced 
13ce2 26 26 20 30 3d 3d 28 70 50 61 67 65 2d 3e 66 6c  && 0==(pPage->fl
13ce3 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
13ce4 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70  YNC) ){.    p->p
13ce5 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 3b 0a  Synced = pPage;.
13ce6 20 20 7d 0a 20 20 65 78 70 65 6e 73 69 76 65 5f    }.  expensive_
13ce7 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43 68  assert( pcacheCh
13ce8 65 63 6b 53 79 6e 63 65 64 28 70 29 20 29 3b 0a  eckSynced(p) );.
13ce9 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72  }../*.** Wrapper
13cea 20 61 72 6f 75 6e 64 20 74 68 65 20 70 6c 75 67   around the plug
13ceb 67 61 62 6c 65 20 63 61 63 68 65 73 20 78 55 6e  gable caches xUn
13cec 70 69 6e 20 6d 65 74 68 6f 64 2e 20 49 66 20 74  pin method. If t
13ced 68 65 20 63 61 63 68 65 20 69 73 0a 2a 2a 20 62  he cache is.** b
13cee 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20 61 6e  eing used for an
13cef 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
13cf0 61 73 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69  ase, this functi
13cf1 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  on is a no-op..*
13cf2 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
13cf3 61 63 68 65 55 6e 70 69 6e 28 50 67 48 64 72 20  acheUnpin(PgHdr 
13cf4 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70  *p){.  PCache *p
13cf5 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68  Cache = p->pCach
13cf6 65 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d  e;.  if( pCache-
13cf7 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20  >bPurgeable ){. 
13cf8 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d     if( p->pgno==
13cf9 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68  1 ){.      pCach
13cfa 65 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20  e->pPage1 = 0;. 
13cfb 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
13cfc 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
13cfd 63 68 65 32 2e 78 55 6e 70 69 6e 28 70 43 61 63  che2.xUnpin(pCac
13cfe 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2d 3e 70  he->pCache, p->p
13cff 50 61 67 65 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a  Page, 0);.  }.}.
13d00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
13d01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13d02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13d03 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6e  ***** General In
13d04 74 65 72 66 61 63 65 73 20 2a 2a 2a 2a 2a 2a 0a  terfaces ******.
13d05 2a 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  **.** Initialize
13d06 20 61 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68   and shutdown th
13d07 65 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62  e page cache sub
13d08 73 79 73 74 65 6d 2e 20 4e 65 69 74 68 65 72 20  system. Neither 
13d09 6f 66 20 74 68 65 73 65 20 0a 2a 2a 20 66 75 6e  of these .** fun
13d0a 63 74 69 6f 6e 73 20 61 72 65 20 74 68 72 65 61  ctions are threa
13d0b 64 73 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  dsafe..*/.SQLITE
13d0c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
13d0d 69 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61  ite3PcacheInitia
13d0e 6c 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 69 66  lize(void){.  if
13d0f 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
13d10 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78 49  onfig.pcache2.xI
13d11 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  nit==0 ){.    /*
13d12 20 49 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d   IMPLEMENTATION-
13d13 4f 46 3a 20 52 2d 32 36 38 30 31 2d 36 34 31 33  OF: R-26801-6413
13d14 37 20 49 66 20 74 68 65 20 78 49 6e 69 74 28 29  7 If the xInit()
13d15 20 6d 65 74 68 6f 64 20 69 73 20 4e 55 4c 4c 2c   method is NULL,
13d16 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a   then the.    **
13d17 20 62 75 69 6c 74 2d 69 6e 20 64 65 66 61 75 6c   built-in defaul
13d18 74 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20  t page cache is 
13d19 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20  used instead of 
13d1a 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
13d1b 64 65 66 69 6e 65 64 0a 20 20 20 20 2a 2a 20 70  defined.    ** p
13d1c 61 67 65 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20  age cache. */.  
13d1d 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53    sqlite3PCacheS
13d1e 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d  etDefault();.  }
13d1f 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
13d20 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
13d21 61 63 68 65 32 2e 78 49 6e 69 74 28 73 71 6c 69  ache2.xInit(sqli
13d22 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
13d23 70 63 61 63 68 65 32 2e 70 41 72 67 29 3b 0a 7d  pcache2.pArg);.}
13d24 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13d25 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
13d26 68 65 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29  heShutdown(void)
13d27 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  {.  if( sqlite3G
13d28 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
13d29 68 65 32 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b  he2.xShutdown ){
13d2a 0a 20 20 20 20 2f 2a 20 49 4d 50 4c 45 4d 45 4e  .    /* IMPLEMEN
13d2b 54 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d 32 36 30  TATION-OF: R-260
13d2c 30 30 2d 35 36 35 38 39 20 54 68 65 20 78 53 68  00-56589 The xSh
13d2d 75 74 64 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20  utdown() method 
13d2e 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a  may be NULL. */.
13d2f 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
13d30 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e  lConfig.pcache2.
13d31 78 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65  xShutdown(sqlite
13d32 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
13d33 61 63 68 65 32 2e 70 41 72 67 29 3b 0a 20 20 7d  ache2.pArg);.  }
13d34 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
13d35 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74   the size in byt
13d36 65 73 20 6f 66 20 61 20 50 43 61 63 68 65 20 6f  es of a PCache o
13d37 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  bject..*/.SQLITE
13d38 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
13d39 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 76  ite3PcacheSize(v
13d3a 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 73 69 7a  oid){ return siz
13d3b 65 6f 66 28 50 43 61 63 68 65 29 3b 20 7d 0a 0a  eof(PCache); }..
13d3c 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
13d3d 65 77 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74  ew PCache object
13d3e 2e 20 53 74 6f 72 61 67 65 20 73 70 61 63 65 20  . Storage space 
13d3f 74 6f 20 68 6f 6c 64 20 74 68 65 20 6f 62 6a 65  to hold the obje
13d40 63 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64  ct.** has alread
13d41 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  y been allocated
13d42 20 61 6e 64 20 69 73 20 70 61 73 73 65 64 20 69   and is passed i
13d43 6e 20 61 73 20 74 68 65 20 70 20 70 6f 69 6e 74  n as the p point
13d44 65 72 2e 20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  er. .** The call
13d45 65 72 20 64 69 73 63 6f 76 65 72 73 20 68 6f 77  er discovers how
13d46 20 6d 75 63 68 20 73 70 61 63 65 20 6e 65 65 64   much space need
13d47 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65  s to be allocate
13d48 64 20 62 79 20 0a 2a 2a 20 63 61 6c 6c 69 6e 67  d by .** calling
13d49 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 69   sqlite3PcacheSi
13d4a 7a 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ze()..*/.SQLITE_
13d4b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
13d4c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 0a  ite3PcacheOpen(.
13d4d 20 20 69 6e 74 20 73 7a 50 61 67 65 2c 20 20 20    int szPage,   
13d4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
13d4f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79 20  * Size of every 
13d50 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  page */.  int sz
13d51 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 20  Extra,          
13d52 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20         /* Extra 
13d53 73 70 61 63 65 20 61 73 73 6f 63 69 61 74 65 64  space associated
13d54 20 77 69 74 68 20 65 61 63 68 20 70 61 67 65 20   with each page 
13d55 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65 61  */.  int bPurgea
13d56 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ble,            
13d57 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67    /* True if pag
13d58 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e  es are on backin
13d59 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74  g store */.  int
13d5a 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64   (*xStress)(void
13d5b 2a 2c 50 67 48 64 72 2a 29 2c 2f 2a 20 43 61 6c  *,PgHdr*),/* Cal
13d5c 6c 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65  l to try to make
13d5d 20 70 61 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a   pages clean */.
13d5e 20 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 2c    void *pStress,
13d5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
13d60 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53  * Argument to xS
13d61 74 72 65 73 73 20 2a 2f 0a 20 20 50 43 61 63 68  tress */.  PCach
13d62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 20  e *p            
13d63 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c          /* Preal
13d64 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f  located space fo
13d65 72 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a  r the PCache */.
13d66 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  ){.  memset(p, 0
13d67 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 29  , sizeof(PCache)
13d68 29 3b 0a 20 20 70 2d 3e 73 7a 50 61 67 65 20 3d  );.  p->szPage =
13d69 20 73 7a 50 61 67 65 3b 0a 20 20 70 2d 3e 73 7a   szPage;.  p->sz
13d6a 45 78 74 72 61 20 3d 20 73 7a 45 78 74 72 61 3b  Extra = szExtra;
13d6b 0a 20 20 70 2d 3e 62 50 75 72 67 65 61 62 6c 65  .  p->bPurgeable
13d6c 20 3d 20 62 50 75 72 67 65 61 62 6c 65 3b 0a 20   = bPurgeable;. 
13d6d 20 70 2d 3e 78 53 74 72 65 73 73 20 3d 20 78 53   p->xStress = xS
13d6e 74 72 65 73 73 3b 0a 20 20 70 2d 3e 70 53 74 72  tress;.  p->pStr
13d6f 65 73 73 20 3d 20 70 53 74 72 65 73 73 3b 0a 20  ess = pStress;. 
13d70 20 70 2d 3e 73 7a 43 61 63 68 65 20 3d 20 31 30   p->szCache = 10
13d71 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  0;.}../*.** Chan
13d72 67 65 20 74 68 65 20 70 61 67 65 20 73 69 7a 65  ge the page size
13d73 20 66 6f 72 20 50 43 61 63 68 65 20 6f 62 6a 65   for PCache obje
13d74 63 74 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  ct. The caller m
13d75 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20  ust ensure that 
13d76 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20  there.** are no 
13d77 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  outstanding page
13d78 20 72 65 66 65 72 65 6e 63 65 73 20 77 68 65 6e   references when
13d79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
13d7a 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c  s called..*/.SQL
13d7b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
13d7c 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65   sqlite3PcacheSe
13d7d 74 50 61 67 65 53 69 7a 65 28 50 43 61 63 68 65  tPageSize(PCache
13d7e 20 2a 70 43 61 63 68 65 2c 20 69 6e 74 20 73 7a   *pCache, int sz
13d7f 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28  Page){.  assert(
13d80 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 3d 3d 30   pCache->nRef==0
13d81 20 26 26 20 70 43 61 63 68 65 2d 3e 70 44 69 72   && pCache->pDir
13d82 74 79 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  ty==0 );.  if( p
13d83 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b  Cache->pCache ){
13d84 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
13d85 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32  alConfig.pcache2
13d86 2e 78 44 65 73 74 72 6f 79 28 70 43 61 63 68 65  .xDestroy(pCache
13d87 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 20 20 70  ->pCache);.    p
13d88 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 3d 20  Cache->pCache = 
13d89 30 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70  0;.    pCache->p
13d8a 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 20  Page1 = 0;.  }. 
13d8b 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20   pCache->szPage 
13d8c 3d 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a  = szPage;.}../*.
13d8d 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6e  ** Compute the n
13d8e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f  umber of pages o
13d8f 66 20 63 61 63 68 65 20 72 65 71 75 65 73 74 65  f cache requeste
13d90 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
13d91 20 6e 75 6d 62 65 72 4f 66 43 61 63 68 65 50 61   numberOfCachePa
13d92 67 65 73 28 50 43 61 63 68 65 20 2a 70 29 7b 0a  ges(PCache *p){.
13d93 20 20 69 66 28 20 70 2d 3e 73 7a 43 61 63 68 65    if( p->szCache
13d94 3e 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  >=0 ){.    retur
13d95 6e 20 70 2d 3e 73 7a 43 61 63 68 65 3b 0a 20 20  n p->szCache;.  
13d96 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
13d97 6e 20 28 69 6e 74 29 28 28 2d 31 30 32 34 2a 28  n (int)((-1024*(
13d98 69 36 34 29 70 2d 3e 73 7a 43 61 63 68 65 29 2f  i64)p->szCache)/
13d99 28 70 2d 3e 73 7a 50 61 67 65 2b 70 2d 3e 73 7a  (p->szPage+p->sz
13d9a 45 78 74 72 61 29 29 3b 0a 20 20 7d 0a 7d 0a 0a  Extra));.  }.}..
13d9b 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74  /*.** Try to obt
13d9c 61 69 6e 20 61 20 70 61 67 65 20 66 72 6f 6d 20  ain a page from 
13d9d 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51  the cache..*/.SQ
13d9e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
13d9f 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65   sqlite3PcacheFe
13da0 74 63 68 28 0a 20 20 50 43 61 63 68 65 20 2a 70  tch(.  PCache *p
13da1 43 61 63 68 65 2c 20 20 20 20 20 20 20 2f 2a 20  Cache,       /* 
13da2 4f 62 74 61 69 6e 20 74 68 65 20 70 61 67 65 20  Obtain the page 
13da3 66 72 6f 6d 20 74 68 69 73 20 63 61 63 68 65 20  from this cache 
13da4 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  */.  Pgno pgno, 
13da5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
13da6 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6f 62 74  ge number to obt
13da7 61 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65  ain */.  int cre
13da8 61 74 65 46 6c 61 67 2c 20 20 20 20 20 20 20 2f  ateFlag,       /
13da9 2a 20 49 66 20 74 72 75 65 2c 20 63 72 65 61 74  * If true, creat
13daa 65 20 70 61 67 65 20 69 66 20 69 74 20 64 6f 65  e page if it doe
13dab 73 20 6e 6f 74 20 65 78 69 73 74 20 61 6c 72 65  s not exist alre
13dac 61 64 79 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a  ady */.  PgHdr *
13dad 2a 70 70 50 61 67 65 20 20 20 20 20 20 20 20 2f  *ppPage        /
13dae 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 67 65  * Write the page
13daf 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 73 71   here */.){.  sq
13db0 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67  lite3_pcache_pag
13db1 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20  e *pPage = 0;.  
13db2 50 67 48 64 72 20 2a 70 50 67 48 64 72 20 3d 20  PgHdr *pPgHdr = 
13db3 30 3b 0a 20 20 69 6e 74 20 65 43 72 65 61 74 65  0;.  int eCreate
13db4 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 61  ;..  assert( pCa
13db5 63 68 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  che!=0 );.  asse
13db6 72 74 28 20 63 72 65 61 74 65 46 6c 61 67 3d 3d  rt( createFlag==
13db7 31 20 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 3d  1 || createFlag=
13db8 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
13db9 70 67 6e 6f 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20  pgno>0 );..  /* 
13dba 49 66 20 74 68 65 20 70 6c 75 67 67 61 62 6c 65  If the pluggable
13dbb 20 63 61 63 68 65 20 28 73 71 6c 69 74 65 33 5f   cache (sqlite3_
13dbc 70 63 61 63 68 65 2a 29 20 68 61 73 20 6e 6f 74  pcache*) has not
13dbd 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c   been allocated,
13dbe 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 69  .  ** allocate i
13dbf 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66  t now..  */.  if
13dc0 28 20 21 70 43 61 63 68 65 2d 3e 70 43 61 63 68  ( !pCache->pCach
13dc1 65 20 26 26 20 63 72 65 61 74 65 46 6c 61 67 20  e && createFlag 
13dc2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 70  ){.    sqlite3_p
13dc3 63 61 63 68 65 20 2a 70 3b 0a 20 20 20 20 70 20  cache *p;.    p 
13dc4 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
13dc5 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78 43  onfig.pcache2.xC
13dc6 72 65 61 74 65 28 0a 20 20 20 20 20 20 20 20 70  reate(.        p
13dc7 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 2c 20 70  Cache->szPage, p
13dc8 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 2b  Cache->szExtra +
13dc9 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29 2c 20   sizeof(PgHdr), 
13dca 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
13dcb 6c 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66  le.    );.    if
13dcc 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20 72 65  ( !p ){.      re
13dcd 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
13dce 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  M;.    }.    sql
13dcf 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
13dd0 2e 70 63 61 63 68 65 32 2e 78 43 61 63 68 65 73  .pcache2.xCaches
13dd1 69 7a 65 28 70 2c 20 6e 75 6d 62 65 72 4f 66 43  ize(p, numberOfC
13dd2 61 63 68 65 50 61 67 65 73 28 70 43 61 63 68 65  achePages(pCache
13dd3 29 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e  ));.    pCache->
13dd4 70 43 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a  pCache = p;.  }.
13dd5 0a 20 20 65 43 72 65 61 74 65 20 3d 20 63 72 65  .  eCreate = cre
13dd6 61 74 65 46 6c 61 67 20 2a 20 28 31 20 2b 20 28  ateFlag * (1 + (
13dd7 21 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61  !pCache->bPurgea
13dd8 62 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e  ble || !pCache->
13dd9 70 44 69 72 74 79 29 29 3b 0a 20 20 69 66 28 20  pDirty));.  if( 
13dda 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29  pCache->pCache )
13ddb 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 71  {.    pPage = sq
13ddc 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
13ddd 67 2e 70 63 61 63 68 65 32 2e 78 46 65 74 63 68  g.pcache2.xFetch
13dde 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c  (pCache->pCache,
13ddf 20 70 67 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b   pgno, eCreate);
13de0 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61  .  }..  if( !pPa
13de1 67 65 20 26 26 20 65 43 72 65 61 74 65 3d 3d 31  ge && eCreate==1
13de2 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70   ){.    PgHdr *p
13de3 50 67 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64  Pg;..    /* Find
13de4 20 61 20 64 69 72 74 79 20 70 61 67 65 20 74 6f   a dirty page to
13de5 20 77 72 69 74 65 2d 6f 75 74 20 61 6e 64 20 72   write-out and r
13de6 65 63 79 63 6c 65 2e 20 46 69 72 73 74 20 74 72  ecycle. First tr
13de7 79 20 74 6f 20 66 69 6e 64 20 61 20 0a 20 20 20  y to find a .   
13de8 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f   ** page that do
13de9 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61  es not require a
13dea 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f   journal-sync (o
13deb 6e 65 20 77 69 74 68 20 50 47 48 44 52 5f 4e 45  ne with PGHDR_NE
13dec 45 44 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63  ED_SYNC.    ** c
13ded 6c 65 61 72 65 64 29 2c 20 62 75 74 20 69 66 20  leared), but if 
13dee 74 68 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  that is not poss
13def 69 62 6c 65 20 73 65 74 74 6c 65 20 66 6f 72 20  ible settle for 
13df0 61 6e 79 20 6f 74 68 65 72 20 0a 20 20 20 20 2a  any other .    *
13df1 2a 20 75 6e 72 65 66 65 72 65 6e 63 65 64 20 64  * unreferenced d
13df2 69 72 74 79 20 70 61 67 65 2e 0a 20 20 20 20 2a  irty page..    *
13df3 2f 0a 20 20 20 20 65 78 70 65 6e 73 69 76 65 5f  /.    expensive_
13df4 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43 68  assert( pcacheCh
13df5 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68 65  eckSynced(pCache
13df6 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 70 50 67  ) );.    for(pPg
13df7 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64  =pCache->pSynced
13df8 3b 20 0a 20 20 20 20 20 20 20 20 70 50 67 20 26  ; .        pPg &
13df9 26 20 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20  & (pPg->nRef || 
13dfa 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44  (pPg->flags&PGHD
13dfb 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a  R_NEED_SYNC)); .
13dfc 20 20 20 20 20 20 20 20 70 50 67 3d 70 50 67 2d          pPg=pPg-
13dfd 3e 70 44 69 72 74 79 50 72 65 76 0a 20 20 20 20  >pDirtyPrev.    
13dfe 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70  );.    pCache->p
13dff 53 79 6e 63 65 64 20 3d 20 70 50 67 3b 0a 20 20  Synced = pPg;.  
13e00 20 20 69 66 28 20 21 70 50 67 20 29 7b 0a 20 20    if( !pPg ){.  
13e01 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43 61 63      for(pPg=pCac
13e02 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20  he->pDirtyTail; 
13e03 70 50 67 20 26 26 20 70 50 67 2d 3e 6e 52 65 66  pPg && pPg->nRef
13e04 3b 20 70 50 67 3d 70 50 67 2d 3e 70 44 69 72 74  ; pPg=pPg->pDirt
13e05 79 50 72 65 76 29 3b 0a 20 20 20 20 7d 0a 20 20  yPrev);.    }.  
13e06 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20    if( pPg ){.   
13e07 20 20 20 69 6e 74 20 72 63 3b 0a 23 69 66 64 65     int rc;.#ifde
13e08 66 20 53 51 4c 49 54 45 5f 4c 4f 47 5f 43 41 43  f SQLITE_LOG_CAC
13e09 48 45 5f 53 50 49 4c 4c 0a 20 20 20 20 20 20 73  HE_SPILL.      s
13e0a 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54  qlite3_log(SQLIT
13e0b 45 5f 46 55 4c 4c 2c 20 0a 20 20 20 20 20 20 20  E_FULL, .       
13e0c 20 20 20 20 20 20 20 20 20 20 20 22 73 70 69 6c             "spil
13e0d 6c 20 70 61 67 65 20 25 64 20 6d 61 6b 69 6e 67  l page %d making
13e0e 20 72 6f 6f 6d 20 66 6f 72 20 25 64 20 2d 20 63   room for %d - c
13e0f 61 63 68 65 20 75 73 65 64 3a 20 25 64 2f 25 64  ache used: %d/%d
13e10 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
13e11 20 20 20 20 20 70 50 67 2d 3e 70 67 6e 6f 2c 20       pPg->pgno, 
13e12 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20  pgno,.          
13e13 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47          sqlite3G
13e14 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
13e15 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 28 70 43  he.xPagecount(pC
13e16 61 63 68 65 2d 3e 70 43 61 63 68 65 29 2c 0a 20  ache->pCache),. 
13e17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13e18 20 6e 75 6d 62 65 72 4f 66 43 61 63 68 65 50 61   numberOfCachePa
13e19 67 65 73 28 70 43 61 63 68 65 29 29 3b 0a 23 65  ges(pCache));.#e
13e1a 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d 20  ndif.      rc = 
13e1b 70 43 61 63 68 65 2d 3e 78 53 74 72 65 73 73 28  pCache->xStress(
13e1c 70 43 61 63 68 65 2d 3e 70 53 74 72 65 73 73 2c  pCache->pStress,
13e1d 20 70 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28   pPg);.      if(
13e1e 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc!=SQLITE_OK &
13e1f 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53  & rc!=SQLITE_BUS
13e20 59 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  Y ){.        ret
13e21 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
13e22 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65      }..    pPage
13e23 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
13e24 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78  Config.pcache2.x
13e25 46 65 74 63 68 28 70 43 61 63 68 65 2d 3e 70 43  Fetch(pCache->pC
13e26 61 63 68 65 2c 20 70 67 6e 6f 2c 20 32 29 3b 0a  ache, pgno, 2);.
13e27 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65    }..  if( pPage
13e28 20 29 7b 0a 20 20 20 20 70 50 67 48 64 72 20 3d   ){.    pPgHdr =
13e29 20 28 50 67 48 64 72 20 2a 29 70 50 61 67 65 2d   (PgHdr *)pPage-
13e2a 3e 70 45 78 74 72 61 3b 0a 0a 20 20 20 20 69 66  >pExtra;..    if
13e2b 28 20 21 70 50 67 48 64 72 2d 3e 70 50 61 67 65  ( !pPgHdr->pPage
13e2c 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74   ){.      memset
13e2d 28 70 50 67 48 64 72 2c 20 30 2c 20 73 69 7a 65  (pPgHdr, 0, size
13e2e 6f 66 28 50 67 48 64 72 29 29 3b 0a 20 20 20 20  of(PgHdr));.    
13e2f 20 20 70 50 67 48 64 72 2d 3e 70 50 61 67 65 20    pPgHdr->pPage 
13e30 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 70  = pPage;.      p
13e31 50 67 48 64 72 2d 3e 70 44 61 74 61 20 3d 20 70  PgHdr->pData = p
13e32 50 61 67 65 2d 3e 70 42 75 66 3b 0a 20 20 20 20  Page->pBuf;.    
13e33 20 20 70 50 67 48 64 72 2d 3e 70 45 78 74 72 61    pPgHdr->pExtra
13e34 20 3d 20 28 76 6f 69 64 20 2a 29 26 70 50 67 48   = (void *)&pPgH
13e35 64 72 5b 31 5d 3b 0a 20 20 20 20 20 20 6d 65 6d  dr[1];.      mem
13e36 73 65 74 28 70 50 67 48 64 72 2d 3e 70 45 78 74  set(pPgHdr->pExt
13e37 72 61 2c 20 30 2c 20 70 43 61 63 68 65 2d 3e 73  ra, 0, pCache->s
13e38 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 20 20 70  zExtra);.      p
13e39 50 67 48 64 72 2d 3e 70 43 61 63 68 65 20 3d 20  PgHdr->pCache = 
13e3a 70 43 61 63 68 65 3b 0a 20 20 20 20 20 20 70 50  pCache;.      pP
13e3b 67 48 64 72 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e  gHdr->pgno = pgn
13e3c 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  o;.    }.    ass
13e3d 65 72 74 28 20 70 50 67 48 64 72 2d 3e 70 43 61  ert( pPgHdr->pCa
13e3e 63 68 65 3d 3d 70 43 61 63 68 65 20 29 3b 0a 20  che==pCache );. 
13e3f 20 20 20 61 73 73 65 72 74 28 20 70 50 67 48 64     assert( pPgHd
13e40 72 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 20 29 3b  r->pgno==pgno );
13e41 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67  .    assert( pPg
13e42 48 64 72 2d 3e 70 44 61 74 61 3d 3d 70 50 61 67  Hdr->pData==pPag
13e43 65 2d 3e 70 42 75 66 20 29 3b 0a 20 20 20 20 61  e->pBuf );.    a
13e44 73 73 65 72 74 28 20 70 50 67 48 64 72 2d 3e 70  ssert( pPgHdr->p
13e45 45 78 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26  Extra==(void *)&
13e46 70 50 67 48 64 72 5b 31 5d 20 29 3b 0a 0a 20 20  pPgHdr[1] );..  
13e47 20 20 69 66 28 20 30 3d 3d 70 50 67 48 64 72 2d    if( 0==pPgHdr-
13e48 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20 70  >nRef ){.      p
13e49 43 61 63 68 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20  Cache->nRef++;. 
13e4a 20 20 20 7d 0a 20 20 20 20 70 50 67 48 64 72 2d     }.    pPgHdr-
13e4b 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 69 66 28  >nRef++;.    if(
13e4c 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20   pgno==1 ){.    
13e4d 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31    pCache->pPage1
13e4e 20 3d 20 70 50 67 48 64 72 3b 0a 20 20 20 20 7d   = pPgHdr;.    }
13e4f 0a 20 20 7d 0a 20 20 2a 70 70 50 61 67 65 20 3d  .  }.  *ppPage =
13e50 20 70 50 67 48 64 72 3b 0a 20 20 72 65 74 75 72   pPgHdr;.  retur
13e51 6e 20 28 70 50 67 48 64 72 3d 3d 30 20 26 26 20  n (pPgHdr==0 && 
13e52 65 43 72 65 61 74 65 29 20 3f 20 53 51 4c 49 54  eCreate) ? SQLIT
13e53 45 5f 4e 4f 4d 45 4d 20 3a 20 53 51 4c 49 54 45  E_NOMEM : SQLITE
13e54 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
13e55 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65  crement the refe
13e56 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e 20 61  rence count on a
13e57 20 70 61 67 65 2e 20 49 66 20 74 68 65 20 70 61   page. If the pa
13e58 67 65 20 69 73 20 63 6c 65 61 6e 20 61 6e 64 20  ge is clean and 
13e59 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65  the.** reference
13e5a 20 63 6f 75 6e 74 20 64 72 6f 70 73 20 74 6f 20   count drops to 
13e5b 30 2c 20 74 68 65 6e 20 69 74 20 69 73 20 6d 61  0, then it is ma
13e5c 64 65 20 65 6c 69 62 6c 65 20 66 6f 72 20 72 65  de elible for re
13e5d 63 79 63 6c 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49  cycling..*/.SQLI
13e5e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
13e5f 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c  sqlite3PcacheRel
13e60 65 61 73 65 28 50 67 48 64 72 20 2a 70 29 7b 0a  ease(PgHdr *p){.
13e61 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
13e62 66 3e 30 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66  f>0 );.  p->nRef
13e63 2d 2d 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65  --;.  if( p->nRe
13e64 66 3d 3d 30 20 29 7b 0a 20 20 20 20 50 43 61 63  f==0 ){.    PCac
13e65 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70 2d 3e  he *pCache = p->
13e66 70 43 61 63 68 65 3b 0a 20 20 20 20 70 43 61 63  pCache;.    pCac
13e67 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20  he->nRef--;.    
13e68 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47  if( (p->flags&PG
13e69 48 44 52 5f 44 49 52 54 59 29 3d 3d 30 20 29 7b  HDR_DIRTY)==0 ){
13e6a 0a 20 20 20 20 20 20 70 63 61 63 68 65 55 6e 70  .      pcacheUnp
13e6b 69 6e 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65  in(p);.    }else
13e6c 7b 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20  {.      /* Move 
13e6d 74 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20  the page to the 
13e6e 68 65 61 64 20 6f 66 20 74 68 65 20 64 69 72 74  head of the dirt
13e6f 79 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20  y list. */.     
13e70 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f   pcacheRemoveFro
13e71 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20  mDirtyList(p);. 
13e72 20 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f       pcacheAddTo
13e73 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20  DirtyList(p);.  
13e74 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
13e75 20 49 6e 63 72 65 61 73 65 20 74 68 65 20 72 65   Increase the re
13e76 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66  ference count of
13e77 20 61 20 73 75 70 70 6c 69 65 64 20 70 61 67 65   a supplied page
13e78 20 62 79 20 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45   by 1..*/.SQLITE
13e79 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
13e7a 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 28 50  lite3PcacheRef(P
13e7b 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65  gHdr *p){.  asse
13e7c 72 74 28 70 2d 3e 6e 52 65 66 3e 30 29 3b 0a 20  rt(p->nRef>0);. 
13e7d 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f   p->nRef++;.}../
13e7e 2a 0a 2a 2a 20 44 72 6f 70 20 61 20 70 61 67 65  *.** Drop a page
13e7f 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 2e   from the cache.
13e80 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 65   There must be e
13e81 78 61 63 74 6c 79 20 6f 6e 65 20 72 65 66 65 72  xactly one refer
13e82 65 6e 63 65 20 74 6f 20 74 68 65 0a 2a 2a 20 70  ence to the.** p
13e83 61 67 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69  age. This functi
13e84 6f 6e 20 64 65 6c 65 74 65 73 20 74 68 61 74 20  on deletes that 
13e85 72 65 66 65 72 65 6e 63 65 2c 20 73 6f 20 61 66  reference, so af
13e86 74 65 72 20 69 74 20 72 65 74 75 72 6e 73 20 74  ter it returns t
13e87 68 65 0a 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74  he.** page point
13e88 65 64 20 74 6f 20 62 79 20 70 20 69 73 20 69 6e  ed to by p is in
13e89 76 61 6c 69 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  valid..*/.SQLITE
13e8a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
13e8b 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f 70 28  lite3PcacheDrop(
13e8c 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 43 61  PgHdr *p){.  PCa
13e8d 63 68 65 20 2a 70 43 61 63 68 65 3b 0a 20 20 61  che *pCache;.  a
13e8e 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d  ssert( p->nRef==
13e8f 31 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c  1 );.  if( p->fl
13e90 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20  ags&PGHDR_DIRTY 
13e91 29 7b 0a 20 20 20 20 70 63 61 63 68 65 52 65 6d  ){.    pcacheRem
13e92 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74  oveFromDirtyList
13e93 28 70 29 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68  (p);.  }.  pCach
13e94 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20  e = p->pCache;. 
13e95 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2d 2d 3b   pCache->nRef--;
13e96 0a 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d  .  if( p->pgno==
13e97 31 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d  1 ){.    pCache-
13e98 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 7d  >pPage1 = 0;.  }
13e99 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  .  sqlite3Global
13e9a 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78  Config.pcache2.x
13e9b 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e 70 43  Unpin(pCache->pC
13e9c 61 63 68 65 2c 20 70 2d 3e 70 50 61 67 65 2c 20  ache, p->pPage, 
13e9d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  1);.}../*.** Mak
13e9e 65 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20  e sure the page 
13e9f 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72  is marked as dir
13ea0 74 79 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20  ty. If it isn't 
13ea1 64 69 72 74 79 20 61 6c 72 65 61 64 79 2c 0a 2a  dirty already,.*
13ea2 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f  * make it so..*/
13ea3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13ea4 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
13ea5 68 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64  heMakeDirty(PgHd
13ea6 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61 67  r *p){.  p->flag
13ea7 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e 54  s &= ~PGHDR_DONT
13ea8 5f 57 52 49 54 45 3b 0a 20 20 61 73 73 65 72 74  _WRITE;.  assert
13ea9 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ( p->nRef>0 );. 
13eaa 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61 67   if( 0==(p->flag
13eab 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 29  s & PGHDR_DIRTY)
13eac 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73   ){.    p->flags
13ead 20 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b   |= PGHDR_DIRTY;
13eae 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f  .    pcacheAddTo
13eaf 44 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20  DirtyList( p);. 
13eb0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65   }.}../*.** Make
13eb1 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69   sure the page i
13eb2 73 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61  s marked as clea
13eb3 6e 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 63  n. If it isn't c
13eb4 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a  lean already,.**
13eb5 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a   make it so..*/.
13eb6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
13eb7 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
13eb8 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72  eMakeClean(PgHdr
13eb9 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e   *p){.  if( (p->
13eba 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49  flags & PGHDR_DI
13ebb 52 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63  RTY) ){.    pcac
13ebc 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74  heRemoveFromDirt
13ebd 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d  yList(p);.    p-
13ebe 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44  >flags &= ~(PGHD
13ebf 52 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45  R_DIRTY|PGHDR_NE
13ec0 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66  ED_SYNC);.    if
13ec1 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a  ( p->nRef==0 ){.
13ec2 20 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 69        pcacheUnpi
13ec3 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n(p);.    }.  }.
13ec4 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76  }../*.** Make ev
13ec5 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20  ery page in the 
13ec6 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a  cache clean..*/.
13ec7 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
13ec8 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
13ec9 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65  eCleanAll(PCache
13eca 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48   *pCache){.  PgH
13ecb 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20  dr *p;.  while( 
13ecc 28 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69  (p = pCache->pDi
13ecd 72 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73  rty)!=0 ){.    s
13ece 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
13ecf 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a  Clean(p);.  }.}.
13ed0 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
13ed1 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
13ed2 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64   flag from all d
13ed3 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53  irty pages..*/.S
13ed4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
13ed5 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
13ed6 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50  ClearSyncFlags(P
13ed7 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a  Cache *pCache){.
13ed8 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f    PgHdr *p;.  fo
13ed9 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72  r(p=pCache->pDir
13eda 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72  ty; p; p=p->pDir
13edb 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e  tyNext){.    p->
13edc 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f  flags &= ~PGHDR_
13edd 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20  NEED_SYNC;.  }. 
13ede 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64   pCache->pSynced
13edf 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74   = pCache->pDirt
13ee0 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  yTail;.}../*.** 
13ee1 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20  Change the page 
13ee2 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70  number of page p
13ee3 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f   to newPgno. .*/
13ee4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13ee5 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
13ee6 68 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c  heMove(PgHdr *p,
13ee7 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a   Pgno newPgno){.
13ee8 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65    PCache *pCache
13ee9 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20   = p->pCache;.  
13eea 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e  assert( p->nRef>
13eeb 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
13eec 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71  ewPgno>0 );.  sq
13eed 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
13eee 67 2e 70 63 61 63 68 65 32 2e 78 52 65 6b 65 79  g.pcache2.xRekey
13eef 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c  (pCache->pCache,
13ef0 20 70 2d 3e 70 50 61 67 65 2c 20 70 2d 3e 70 67   p->pPage, p->pg
13ef1 6e 6f 2c 6e 65 77 50 67 6e 6f 29 3b 0a 20 20 70  no,newPgno);.  p
13ef2 2d 3e 70 67 6e 6f 20 3d 20 6e 65 77 50 67 6e 6f  ->pgno = newPgno
13ef3 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67  ;.  if( (p->flag
13ef4 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 26  s&PGHDR_DIRTY) &
13ef5 26 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44  & (p->flags&PGHD
13ef6 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a  R_NEED_SYNC) ){.
13ef7 20 20 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65      pcacheRemove
13ef8 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29  FromDirtyList(p)
13ef9 3b 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 54  ;.    pcacheAddT
13efa 6f 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20  oDirtyList(p);. 
13efb 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70   }.}../*.** Drop
13efc 20 65 76 65 72 79 20 63 61 63 68 65 20 65 6e 74   every cache ent
13efd 72 79 20 77 68 6f 73 65 20 70 61 67 65 20 6e 75  ry whose page nu
13efe 6d 62 65 72 20 69 73 20 67 72 65 61 74 65 72 20  mber is greater 
13eff 74 68 61 6e 20 22 70 67 6e 6f 22 2e 20 54 68 65  than "pgno". The
13f00 0a 2a 2a 20 63 61 6c 6c 65 72 20 6d 75 73 74 20  .** caller must 
13f01 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 72  ensure that ther
13f02 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e  e are no outstan
13f03 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20  ding references 
13f04 74 6f 20 61 6e 79 20 70 61 67 65 73 0a 2a 2a 20  to any pages.** 
13f05 6f 74 68 65 72 20 74 68 61 6e 20 70 61 67 65 20  other than page 
13f06 31 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75  1 with a page nu
13f07 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 68 61  mber greater tha
13f08 6e 20 70 67 6e 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66  n pgno..**.** If
13f09 20 74 68 65 72 65 20 69 73 20 61 20 72 65 66 65   there is a refe
13f0a 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20  rence to page 1 
13f0b 61 6e 64 20 74 68 65 20 70 67 6e 6f 20 70 61 72  and the pgno par
13f0c 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 74 6f  ameter passed to
13f0d 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f   this.** functio
13f0e 6e 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 65  n is 0, then the
13f0f 20 64 61 74 61 20 61 72 65 61 20 61 73 73 6f 63   data area assoc
13f10 69 61 74 65 64 20 77 69 74 68 20 70 61 67 65 20  iated with page 
13f11 31 20 69 73 20 7a 65 72 6f 65 64 2c 20 62 75 74  1 is zeroed, but
13f12 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6f 62 6a  .** the page obj
13f13 65 63 74 20 69 73 20 6e 6f 74 20 64 72 6f 70 70  ect is not dropp
13f14 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
13f15 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
13f16 65 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65  e3PcacheTruncate
13f17 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c  (PCache *pCache,
13f18 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69   Pgno pgno){.  i
13f19 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68  f( pCache->pCach
13f1a 65 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a  e ){.    PgHdr *
13f1b 70 3b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 4e  p;.    PgHdr *pN
13f1c 65 78 74 3b 0a 20 20 20 20 66 6f 72 28 70 3d 70  ext;.    for(p=p
13f1d 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 70  Cache->pDirty; p
13f1e 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20  ; p=pNext){.    
13f1f 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 44 69    pNext = p->pDi
13f20 72 74 79 4e 65 78 74 3b 0a 20 20 20 20 20 20 2f  rtyNext;.      /
13f21 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e  * This routine n
13f22 65 76 65 72 20 67 65 74 73 20 63 61 6c 6c 20 77  ever gets call w
13f23 69 74 68 20 61 20 70 6f 73 69 74 69 76 65 20 70  ith a positive p
13f24 67 6e 6f 20 65 78 63 65 70 74 20 72 69 67 68 74  gno except right
13f25 0a 20 20 20 20 20 20 2a 2a 20 61 66 74 65 72 20  .      ** after 
13f26 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65  sqlite3PcacheCle
13f27 61 6e 41 6c 6c 28 29 2e 20 20 53 6f 20 69 66 20  anAll().  So if 
13f28 74 68 65 72 65 20 61 72 65 20 64 69 72 74 79 20  there are dirty 
13f29 70 61 67 65 73 2c 0a 20 20 20 20 20 20 2a 2a 20  pages,.      ** 
13f2a 69 74 20 6d 75 73 74 20 62 65 20 74 68 61 74 20  it must be that 
13f2b 70 67 6e 6f 3d 3d 30 2e 0a 20 20 20 20 20 20 2a  pgno==0..      *
13f2c 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
13f2d 70 2d 3e 70 67 6e 6f 3e 30 20 29 3b 0a 20 20 20  p->pgno>0 );.   
13f2e 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d     if( ALWAYS(p-
13f2f 3e 70 67 6e 6f 3e 70 67 6e 6f 29 20 29 7b 0a 20  >pgno>pgno) ){. 
13f30 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
13f31 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49  ->flags&PGHDR_DI
13f32 52 54 59 20 29 3b 0a 20 20 20 20 20 20 20 20 73  RTY );.        s
13f33 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
13f34 43 6c 65 61 6e 28 70 29 3b 0a 20 20 20 20 20 20  Clean(p);.      
13f35 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
13f36 70 67 6e 6f 3d 3d 30 20 26 26 20 70 43 61 63 68  pgno==0 && pCach
13f37 65 2d 3e 70 50 61 67 65 31 20 29 7b 0a 20 20 20  e->pPage1 ){.   
13f38 20 20 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65     memset(pCache
13f39 2d 3e 70 50 61 67 65 31 2d 3e 70 44 61 74 61 2c  ->pPage1->pData,
13f3a 20 30 2c 20 70 43 61 63 68 65 2d 3e 73 7a 50 61   0, pCache->szPa
13f3b 67 65 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20  ge);.      pgno 
13f3c 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  = 1;.    }.    s
13f3d 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
13f3e 69 67 2e 70 63 61 63 68 65 32 2e 78 54 72 75 6e  ig.pcache2.xTrun
13f3f 63 61 74 65 28 70 43 61 63 68 65 2d 3e 70 43 61  cate(pCache->pCa
13f40 63 68 65 2c 20 70 67 6e 6f 2b 31 29 3b 0a 20 20  che, pgno+1);.  
13f41 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  }.}../*.** Close
13f42 20 61 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c   a cache..*/.SQL
13f43 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
13f44 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c   sqlite3PcacheCl
13f45 6f 73 65 28 50 43 61 63 68 65 20 2a 70 43 61 63  ose(PCache *pCac
13f46 68 65 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68  he){.  if( pCach
13f47 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20  e->pCache ){.   
13f48 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
13f49 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78 44 65  nfig.pcache2.xDe
13f4a 73 74 72 6f 79 28 70 43 61 63 68 65 2d 3e 70 43  stroy(pCache->pC
13f4b 61 63 68 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ache);.  }.}../*
13f4c 20 0a 2a 2a 20 44 69 73 63 61 72 64 20 74 68 65   .** Discard the
13f4d 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
13f4e 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54   cache..*/.SQLIT
13f4f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
13f50 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61  qlite3PcacheClea
13f51 72 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  r(PCache *pCache
13f52 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63  ){.  sqlite3Pcac
13f53 68 65 54 72 75 6e 63 61 74 65 28 70 43 61 63 68  heTruncate(pCach
13f54 65 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  e, 0);.}../*.** 
13f55 4d 65 72 67 65 20 74 77 6f 20 6c 69 73 74 73 20  Merge two lists 
13f56 6f 66 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74  of pages connect
13f57 65 64 20 62 79 20 70 44 69 72 74 79 20 61 6e 64  ed by pDirty and
13f58 20 69 6e 20 70 67 6e 6f 20 6f 72 64 65 72 2e 0a   in pgno order..
13f59 2a 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 20 66  ** Do not both f
13f5a 69 78 69 6e 67 20 74 68 65 20 70 44 69 72 74 79  ixing the pDirty
13f5b 50 72 65 76 20 70 6f 69 6e 74 65 72 73 2e 0a 2a  Prev pointers..*
13f5c 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a  /.static PgHdr *
13f5d 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79  pcacheMergeDirty
13f5e 4c 69 73 74 28 50 67 48 64 72 20 2a 70 41 2c 20  List(PgHdr *pA, 
13f5f 50 67 48 64 72 20 2a 70 42 29 7b 0a 20 20 50 67  PgHdr *pB){.  Pg
13f60 48 64 72 20 72 65 73 75 6c 74 2c 20 2a 70 54 61  Hdr result, *pTa
13f61 69 6c 3b 0a 20 20 70 54 61 69 6c 20 3d 20 26 72  il;.  pTail = &r
13f62 65 73 75 6c 74 3b 0a 20 20 77 68 69 6c 65 28 20  esult;.  while( 
13f63 70 41 20 26 26 20 70 42 20 29 7b 0a 20 20 20 20  pA && pB ){.    
13f64 69 66 28 20 70 41 2d 3e 70 67 6e 6f 3c 70 42 2d  if( pA->pgno<pB-
13f65 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 70  >pgno ){.      p
13f66 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70  Tail->pDirty = p
13f67 41 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d  A;.      pTail =
13f68 20 70 41 3b 0a 20 20 20 20 20 20 70 41 20 3d 20   pA;.      pA = 
13f69 70 41 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 20  pA->pDirty;.    
13f6a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 61  }else{.      pTa
13f6b 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b  il->pDirty = pB;
13f6c 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70  .      pTail = p
13f6d 42 3b 0a 20 20 20 20 20 20 70 42 20 3d 20 70 42  B;.      pB = pB
13f6e 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 0a  ->pDirty;.    }.
13f6f 20 20 7d 0a 20 20 69 66 28 20 70 41 20 29 7b 0a    }.  if( pA ){.
13f70 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74      pTail->pDirt
13f71 79 20 3d 20 70 41 3b 0a 20 20 7d 65 6c 73 65 20  y = pA;.  }else 
13f72 69 66 28 20 70 42 20 29 7b 0a 20 20 20 20 70 54  if( pB ){.    pT
13f73 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 42  ail->pDirty = pB
13f74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
13f75 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 30  Tail->pDirty = 0
13f76 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
13f77 65 73 75 6c 74 2e 70 44 69 72 74 79 3b 0a 7d 0a  esult.pDirty;.}.
13f78 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 20 74 68 65 20  ./*.** Sort the 
13f79 6c 69 73 74 20 6f 66 20 70 61 67 65 73 20 69 6e  list of pages in
13f7a 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   accending order
13f7b 20 62 79 20 70 67 6e 6f 2e 20 20 50 61 67 65 73   by pgno.  Pages
13f7c 20 61 72 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65   are.** connecte
13f7d 64 20 62 79 20 70 44 69 72 74 79 20 70 6f 69 6e  d by pDirty poin
13f7e 74 65 72 73 2e 20 20 54 68 65 20 70 44 69 72 74  ters.  The pDirt
13f7f 79 50 72 65 76 20 70 6f 69 6e 74 65 72 73 20 61  yPrev pointers a
13f80 72 65 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 20  re.** corrupted 
13f81 62 79 20 74 68 69 73 20 73 6f 72 74 2e 0a 2a 2a  by this sort..**
13f82 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 72 65 20  .** Since there 
13f83 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74  cannot be more t
13f84 68 61 6e 20 32 5e 33 31 20 64 69 73 74 69 6e 63  han 2^31 distinc
13f85 74 20 70 61 67 65 73 20 69 6e 20 61 20 64 61 74  t pages in a dat
13f86 61 62 61 73 65 2c 0a 2a 2a 20 74 68 65 72 65 20  abase,.** there 
13f87 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74  cannot be more t
13f88 68 61 6e 20 33 31 20 62 75 63 6b 65 74 73 20 72  han 31 buckets r
13f89 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 6d  equired by the m
13f8a 65 72 67 65 20 73 6f 72 74 65 72 2e 0a 2a 2a 20  erge sorter..** 
13f8b 4f 6e 65 20 65 78 74 72 61 20 62 75 63 6b 65 74  One extra bucket
13f8c 20 69 73 20 61 64 64 65 64 20 74 6f 20 63 61 74   is added to cat
13f8d 63 68 20 6f 76 65 72 66 6c 6f 77 20 69 6e 20 63  ch overflow in c
13f8e 61 73 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a  ase something.**
13f8f 20 65 76 65 72 20 63 68 61 6e 67 65 73 20 74 6f   ever changes to
13f90 20 6d 61 6b 65 20 74 68 65 20 70 72 65 76 69 6f   make the previo
13f91 75 73 20 73 65 6e 74 65 6e 63 65 20 69 6e 63 6f  us sentence inco
13f92 72 72 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e  rrect..*/.#defin
13f93 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 20  e N_SORT_BUCKET 
13f94 20 33 32 0a 73 74 61 74 69 63 20 50 67 48 64 72   32.static PgHdr
13f95 20 2a 70 63 61 63 68 65 53 6f 72 74 44 69 72 74   *pcacheSortDirt
13f96 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 49 6e  yList(PgHdr *pIn
13f97 29 7b 0a 20 20 50 67 48 64 72 20 2a 61 5b 4e 5f  ){.  PgHdr *a[N_
13f98 53 4f 52 54 5f 42 55 43 4b 45 54 5d 2c 20 2a 70  SORT_BUCKET], *p
13f99 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6d 65 6d  ;.  int i;.  mem
13f9a 73 65 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f 66  set(a, 0, sizeof
13f9b 28 61 29 29 3b 0a 20 20 77 68 69 6c 65 28 20 70  (a));.  while( p
13f9c 49 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20 70 49  In ){.    p = pI
13f9d 6e 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 2d 3e  n;.    pIn = p->
13f9e 70 44 69 72 74 79 3b 0a 20 20 20 20 70 2d 3e 70  pDirty;.    p->p
13f9f 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 66  Dirty = 0;.    f
13fa0 6f 72 28 69 3d 30 3b 20 41 4c 57 41 59 53 28 69  or(i=0; ALWAYS(i
13fa1 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 2d 31  <N_SORT_BUCKET-1
13fa2 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  ); i++){.      i
13fa3 66 28 20 61 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20  f( a[i]==0 ){.  
13fa4 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 70 3b 0a        a[i] = p;.
13fa5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
13fa6 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
13fa7 20 20 20 20 70 20 3d 20 70 63 61 63 68 65 4d 65      p = pcacheMe
13fa8 72 67 65 44 69 72 74 79 4c 69 73 74 28 61 5b 69  rgeDirtyList(a[i
13fa9 5d 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 61  ], p);.        a
13faa 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  [i] = 0;.      }
13fab 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 4e  .    }.    if( N
13fac 45 56 45 52 28 69 3d 3d 4e 5f 53 4f 52 54 5f 42  EVER(i==N_SORT_B
13fad 55 43 4b 45 54 2d 31 29 20 29 7b 0a 20 20 20 20  UCKET-1) ){.    
13fae 20 20 2f 2a 20 54 6f 20 67 65 74 20 68 65 72 65    /* To get here
13faf 2c 20 74 68 65 72 65 20 6e 65 65 64 20 74 6f 20  , there need to 
13fb0 62 65 20 32 5e 28 4e 5f 53 4f 52 54 5f 42 55 43  be 2^(N_SORT_BUC
13fb1 4b 45 54 29 20 65 6c 65 6d 65 6e 74 73 20 69 6e  KET) elements in
13fb2 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 69 6e  .      ** the in
13fb3 70 75 74 20 6c 69 73 74 2e 20 20 42 75 74 20 74  put list.  But t
13fb4 68 61 74 20 69 73 20 69 6d 70 6f 73 73 69 62 6c  hat is impossibl
13fb5 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
13fb6 20 20 61 5b 69 5d 20 3d 20 70 63 61 63 68 65 4d    a[i] = pcacheM
13fb7 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 61 5b  ergeDirtyList(a[
13fb8 69 5d 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20  i], p);.    }.  
13fb9 7d 0a 20 20 70 20 3d 20 61 5b 30 5d 3b 0a 20 20  }.  p = a[0];.  
13fba 66 6f 72 28 69 3d 31 3b 20 69 3c 4e 5f 53 4f 52  for(i=1; i<N_SOR
13fbb 54 5f 42 55 43 4b 45 54 3b 20 69 2b 2b 29 7b 0a  T_BUCKET; i++){.
13fbc 20 20 20 20 70 20 3d 20 70 63 61 63 68 65 4d 65      p = pcacheMe
13fbd 72 67 65 44 69 72 74 79 4c 69 73 74 28 70 2c 20  rgeDirtyList(p, 
13fbe 61 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 74  a[i]);.  }.  ret
13fbf 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
13fc0 52 65 74 75 72 6e 20 61 20 6c 69 73 74 20 6f 66  Return a list of
13fc1 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73   all dirty pages
13fc2 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 73   in the cache, s
13fc3 6f 72 74 65 64 20 62 79 20 70 61 67 65 20 6e 75  orted by page nu
13fc4 6d 62 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  mber..*/.SQLITE_
13fc5 50 52 49 56 41 54 45 20 50 67 48 64 72 20 2a 73  PRIVATE PgHdr *s
13fc6 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 74  qlite3PcacheDirt
13fc7 79 4c 69 73 74 28 50 43 61 63 68 65 20 2a 70 43  yList(PCache *pC
13fc8 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 20 2a  ache){.  PgHdr *
13fc9 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68  p;.  for(p=pCach
13fca 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d  e->pDirty; p; p=
13fcb 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 29 7b 0a  p->pDirtyNext){.
13fcc 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20      p->pDirty = 
13fcd 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20  p->pDirtyNext;. 
13fce 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 63 61 63   }.  return pcac
13fcf 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 28  heSortDirtyList(
13fd0 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 29 3b  pCache->pDirty);
13fd1 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72  .}../* .** Retur
13fd2 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  n the total numb
13fd3 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 64  er of referenced
13fd4 20 70 61 67 65 73 20 68 65 6c 64 20 62 79 20 74   pages held by t
13fd5 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c  he cache..*/.SQL
13fd6 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
13fd7 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66  sqlite3PcacheRef
13fd8 43 6f 75 6e 74 28 50 43 61 63 68 65 20 2a 70 43  Count(PCache *pC
13fd9 61 63 68 65 29 7b 0a 20 20 72 65 74 75 72 6e 20  ache){.  return 
13fda 70 43 61 63 68 65 2d 3e 6e 52 65 66 3b 0a 7d 0a  pCache->nRef;.}.
13fdb 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
13fdc 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65  e number of refe
13fdd 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61  rences to the pa
13fde 67 65 20 73 75 70 70 6c 69 65 64 20 61 73 20 61  ge supplied as a
13fdf 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53  n argument..*/.S
13fe0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
13fe1 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50  t sqlite3PcacheP
13fe2 61 67 65 52 65 66 63 6f 75 6e 74 28 50 67 48 64  ageRefcount(PgHd
13fe3 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  r *p){.  return 
13fe4 70 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 20 0a  p->nRef;.}../* .
13fe5 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f  ** Return the to
13fe6 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  tal number of pa
13fe7 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65  ges in the cache
13fe8 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
13fe9 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
13fea 63 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28 50  cachePagecount(P
13feb 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a  Cache *pCache){.
13fec 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 30 3b    int nPage = 0;
13fed 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70  .  if( pCache->p
13fee 43 61 63 68 65 20 29 7b 0a 20 20 20 20 6e 50 61  Cache ){.    nPa
13fef 67 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  ge = sqlite3Glob
13ff0 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 32  alConfig.pcache2
13ff1 2e 78 50 61 67 65 63 6f 75 6e 74 28 70 43 61 63  .xPagecount(pCac
13ff2 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 7d  he->pCache);.  }
13ff3 0a 20 20 72 65 74 75 72 6e 20 6e 50 61 67 65 3b  .  return nPage;
13ff4 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
13ff5 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 47 65 74  E_TEST./*.** Get
13ff6 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63   the suggested c
13ff7 61 63 68 65 2d 73 69 7a 65 20 76 61 6c 75 65 2e  ache-size value.
13ff8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
13ff9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
13ffa 61 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65  acheGetCachesize
13ffb 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29  (PCache *pCache)
13ffc 7b 0a 20 20 72 65 74 75 72 6e 20 6e 75 6d 62 65  {.  return numbe
13ffd 72 4f 66 43 61 63 68 65 50 61 67 65 73 28 70 43  rOfCachePages(pC
13ffe 61 63 68 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  ache);.}.#endif.
13fff 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73  ./*.** Set the s
14000 75 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73  uggested cache-s
14001 69 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51  ize value..*/.SQ
14002 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
14003 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53  d sqlite3PcacheS
14004 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63  etCachesize(PCac
14005 68 65 20 2a 70 43 61 63 68 65 2c 20 69 6e 74 20  he *pCache, int 
14006 6d 78 50 61 67 65 29 7b 0a 20 20 70 43 61 63 68  mxPage){.  pCach
14007 65 2d 3e 73 7a 43 61 63 68 65 20 3d 20 6d 78 50  e->szCache = mxP
14008 61 67 65 3b 0a 20 20 69 66 28 20 70 43 61 63 68  age;.  if( pCach
14009 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20  e->pCache ){.   
1400a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
1400b 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78 43 61  nfig.pcache2.xCa
1400c 63 68 65 73 69 7a 65 28 70 43 61 63 68 65 2d 3e  chesize(pCache->
1400d 70 43 61 63 68 65 2c 0a 20 20 20 20 20 20 20 20  pCache,.        
1400e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1400f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14010 20 20 20 6e 75 6d 62 65 72 4f 66 43 61 63 68 65     numberOfCache
14011 50 61 67 65 73 28 70 43 61 63 68 65 29 29 3b 0a  Pages(pCache));.
14012 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65    }.}../*.** Fre
14013 65 20 75 70 20 61 73 20 6d 75 63 68 20 6d 65 6d  e up as much mem
14014 6f 72 79 20 61 73 20 70 6f 73 73 69 62 6c 65 20  ory as possible 
14015 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20 63 61  from the page ca
14016 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  che..*/.SQLITE_P
14017 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
14018 74 65 33 50 63 61 63 68 65 53 68 72 69 6e 6b 28  te3PcacheShrink(
14019 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
1401a 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70  .  if( pCache->p
1401b 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c  Cache ){.    sql
1401c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
1401d 2e 70 63 61 63 68 65 32 2e 78 53 68 72 69 6e 6b  .pcache2.xShrink
1401e 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29  (pCache->pCache)
1401f 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 64 65 66  ;.  }.}..#if def
14020 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 48 45 43  ined(SQLITE_CHEC
14021 4b 5f 50 41 47 45 53 29 20 7c 7c 20 64 65 66 69  K_PAGES) || defi
14022 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
14023 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 61 6c 6c 20  )./*.** For all 
14024 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72 72  dirty pages curr
14025 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 63 61 63  ently in the cac
14026 68 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 73  he, invoke the s
14027 70 65 63 69 66 69 65 64 0a 2a 2a 20 63 61 6c 6c  pecified.** call
14028 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 6f 6e  back. This is on
14029 6c 79 20 75 73 65 64 20 69 66 20 74 68 65 20 53  ly used if the S
1402a 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
1402b 53 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 64 65  S macro is.** de
1402c 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fined..*/.SQLITE
1402d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1402e 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 61  lite3PcacheItera
1402f 74 65 44 69 72 74 79 28 50 43 61 63 68 65 20 2a  teDirty(PCache *
14030 70 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a 78  pCache, void (*x
14031 49 74 65 72 29 28 50 67 48 64 72 20 2a 29 29 7b  Iter)(PgHdr *)){
14032 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79  .  PgHdr *pDirty
14033 3b 0a 20 20 66 6f 72 28 70 44 69 72 74 79 3d 70  ;.  for(pDirty=p
14034 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 70  Cache->pDirty; p
14035 44 69 72 74 79 3b 20 70 44 69 72 74 79 3d 70 44  Dirty; pDirty=pD
14036 69 72 74 79 2d 3e 70 44 69 72 74 79 4e 65 78 74  irty->pDirtyNext
14037 29 7b 0a 20 20 20 20 78 49 74 65 72 28 70 44 69  ){.    xIter(pDi
14038 72 74 79 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  rty);.  }.}.#end
14039 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
1403a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 68  *** End of pcach
1403b 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
1403c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1403d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1403e 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1403f 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70  *** Begin file p
14040 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a  cache1.c *******
14041 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14042 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14043 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4e  **/./*.** 2008 N
14044 6f 76 65 6d 62 65 72 20 30 35 0a 2a 2a 0a 2a 2a  ovember 05.**.**
14045 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
14046 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
14047 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
14048 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
14049 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1404a 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1404b 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1404c 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1404d 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1404e 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1404f 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
14050 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
14051 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
14052 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
14053 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
14054 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
14055 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
14056 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14057 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14058 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14059 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1405a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
1405b 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d  This file implem
1405c 65 6e 74 73 20 74 68 65 20 64 65 66 61 75 6c 74  ents the default
1405d 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c   page cache impl
1405e 65 6d 65 6e 74 61 74 69 6f 6e 20 28 74 68 65 0a  ementation (the.
1405f 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  ** sqlite3_pcach
14060 65 20 69 6e 74 65 72 66 61 63 65 29 2e 20 49 74  e interface). It
14061 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 70   also contains p
14062 61 72 74 20 6f 66 20 74 68 65 20 69 6d 70 6c 65  art of the imple
14063 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20  mentation.** of 
14064 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  the SQLITE_CONFI
14065 47 5f 50 41 47 45 43 41 43 48 45 20 61 6e 64 20  G_PAGECACHE and 
14066 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
14067 6d 65 6d 6f 72 79 28 29 20 66 65 61 74 75 72 65  memory() feature
14068 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 66  s..** If the def
14069 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 20  ault page cache 
1406a 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
1406b 73 20 6f 76 65 72 72 69 64 65 6e 2c 20 74 68 65  s overriden, the
1406c 6e 20 6e 65 69 74 68 65 72 20 6f 66 0a 2a 2a 20  n neither of.** 
1406d 74 68 65 73 65 20 74 77 6f 20 66 65 61 74 75 72  these two featur
1406e 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  es are available
1406f 2e 0a 2a 2f 0a 0a 0a 74 79 70 65 64 65 66 20 73  ..*/...typedef s
14070 74 72 75 63 74 20 50 43 61 63 68 65 31 20 50 43  truct PCache1 PC
14071 61 63 68 65 31 3b 0a 74 79 70 65 64 65 66 20 73  ache1;.typedef s
14072 74 72 75 63 74 20 50 67 48 64 72 31 20 50 67 48  truct PgHdr1 PgH
14073 64 72 31 3b 0a 74 79 70 65 64 65 66 20 73 74 72  dr1;.typedef str
14074 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 20 50  uct PgFreeslot P
14075 67 46 72 65 65 73 6c 6f 74 3b 0a 74 79 70 65 64  gFreeslot;.typed
14076 65 66 20 73 74 72 75 63 74 20 50 47 72 6f 75 70  ef struct PGroup
14077 20 50 47 72 6f 75 70 3b 0a 0a 2f 2a 20 45 61 63   PGroup;../* Eac
14078 68 20 70 61 67 65 20 63 61 63 68 65 20 28 6f 72  h page cache (or
14079 20 50 43 61 63 68 65 29 20 62 65 6c 6f 6e 67 73   PCache) belongs
1407a 20 74 6f 20 61 20 50 47 72 6f 75 70 2e 20 20 41   to a PGroup.  A
1407b 20 50 47 72 6f 75 70 20 69 73 20 61 20 73 65 74   PGroup is a set
1407c 20 0a 2a 2a 20 6f 66 20 6f 6e 65 20 6f 72 20 6d   .** of one or m
1407d 6f 72 65 20 50 43 61 63 68 65 73 20 74 68 61 74  ore PCaches that
1407e 20 61 72 65 20 61 62 6c 65 20 74 6f 20 72 65 63   are able to rec
1407f 79 63 6c 65 20 65 61 63 68 20 6f 74 68 65 72 73  ycle each others
14080 20 75 6e 70 69 6e 6e 65 64 0a 2a 2a 20 70 61 67   unpinned.** pag
14081 65 73 20 77 68 65 6e 20 74 68 65 79 20 61 72 65  es when they are
14082 20 75 6e 64 65 72 20 6d 65 6d 6f 72 79 20 70 72   under memory pr
14083 65 73 73 75 72 65 2e 20 20 41 20 50 47 72 6f 75  essure.  A PGrou
14084 70 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  p is an instance
14085 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f   of.** the follo
14086 77 69 6e 67 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a  wing object..**.
14087 2a 2a 20 54 68 69 73 20 70 61 67 65 20 63 61 63  ** This page cac
14088 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
14089 6e 20 77 6f 72 6b 73 20 69 6e 20 6f 6e 65 20 6f  n works in one o
1408a 66 20 74 77 6f 20 6d 6f 64 65 73 3a 0a 2a 2a 0a  f two modes:.**.
1408b 2a 2a 20 20 20 28 31 29 20 20 45 76 65 72 79 20  **   (1)  Every 
1408c 50 43 61 63 68 65 20 69 73 20 74 68 65 20 73 6f  PCache is the so
1408d 6c 65 20 6d 65 6d 62 65 72 20 6f 66 20 69 74 73  le member of its
1408e 20 6f 77 6e 20 50 47 72 6f 75 70 2e 20 20 54 68   own PGroup.  Th
1408f 65 72 65 20 69 73 0a 2a 2a 20 20 20 20 20 20 20  ere is.**       
14090 20 6f 6e 65 20 50 47 72 6f 75 70 20 70 65 72 20   one PGroup per 
14091 50 43 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  PCache..**.**   
14092 28 32 29 20 20 54 68 65 72 65 20 69 73 20 61 20  (2)  There is a 
14093 73 69 6e 67 6c 65 20 67 6c 6f 62 61 6c 20 50 47  single global PG
14094 72 6f 75 70 20 74 68 61 74 20 61 6c 6c 20 50 43  roup that all PC
14095 61 63 68 65 73 20 61 72 65 20 61 20 6d 65 6d 62  aches are a memb
14096 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 6f 66 2e  er.**        of.
14097 0a 2a 2a 0a 2a 2a 20 4d 6f 64 65 20 31 20 75 73  .**.** Mode 1 us
14098 65 73 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 28  es more memory (
14099 73 69 6e 63 65 20 50 43 61 63 68 65 20 69 6e 73  since PCache ins
1409a 74 61 6e 63 65 73 20 61 72 65 20 6e 6f 74 20 61  tances are not a
1409b 62 6c 65 20 74 6f 20 72 6f 62 0a 2a 2a 20 75 6e  ble to rob.** un
1409c 75 73 65 64 20 70 61 67 65 73 20 66 72 6f 6d 20  used pages from 
1409d 6f 74 68 65 72 20 50 43 61 63 68 65 73 29 20 62  other PCaches) b
1409e 75 74 20 69 74 20 61 6c 73 6f 20 6f 70 65 72 61  ut it also opera
1409f 74 65 73 20 77 69 74 68 6f 75 74 20 61 20 6d 75  tes without a mu
140a0 74 65 78 2c 0a 2a 2a 20 61 6e 64 20 69 73 20 74  tex,.** and is t
140a1 68 65 72 65 66 6f 72 65 20 6f 66 74 65 6e 20 66  herefore often f
140a2 61 73 74 65 72 2e 20 20 4d 6f 64 65 20 32 20 72  aster.  Mode 2 r
140a3 65 71 75 69 72 65 73 20 61 20 6d 75 74 65 78 20  equires a mutex 
140a4 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 0a 2a  in order to be.*
140a5 2a 20 74 68 72 65 61 64 73 61 66 65 2c 20 62 75  * threadsafe, bu
140a6 74 20 72 65 63 79 63 6c 65 73 20 70 61 67 65 73  t recycles pages
140a7 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 6c   more efficientl
140a8 79 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 64  y..**.** For mod
140a9 65 20 28 31 29 2c 20 50 47 72 6f 75 70 2e 6d 75  e (1), PGroup.mu
140aa 74 65 78 20 69 73 20 4e 55 4c 4c 2e 20 20 46 6f  tex is NULL.  Fo
140ab 72 20 6d 6f 64 65 20 28 32 29 20 74 68 65 72 65  r mode (2) there
140ac 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c   is only a singl
140ad 65 0a 2a 2a 20 50 47 72 6f 75 70 20 77 68 69 63  e.** PGroup whic
140ae 68 20 69 73 20 74 68 65 20 70 63 61 63 68 65 31  h is the pcache1
140af 2e 67 72 70 20 67 6c 6f 62 61 6c 20 76 61 72 69  .grp global vari
140b0 61 62 6c 65 20 61 6e 64 20 69 74 73 20 6d 75 74  able and its mut
140b1 65 78 20 69 73 0a 2a 2a 20 53 51 4c 49 54 45 5f  ex is.** SQLITE_
140b2 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55  MUTEX_STATIC_LRU
140b3 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 47 72 6f  ..*/.struct PGro
140b4 75 70 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  up {.  sqlite3_m
140b5 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20  utex *mutex;    
140b6 20 20 20 20 20 20 2f 2a 20 4d 55 54 45 58 5f 53        /* MUTEX_S
140b7 54 41 54 49 43 5f 4c 52 55 20 6f 72 20 4e 55 4c  TATIC_LRU or NUL
140b8 4c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  L */.  unsigned 
140b9 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20  int nMaxPage;   
140ba 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20        /* Sum of 
140bb 6e 4d 61 78 20 66 6f 72 20 70 75 72 67 65 61 62  nMax for purgeab
140bc 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 75  le caches */.  u
140bd 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d 69 6e  nsigned int nMin
140be 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 2f 2a  Page;         /*
140bf 20 53 75 6d 20 6f 66 20 6e 4d 69 6e 20 66 6f 72   Sum of nMin for
140c0 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65   purgeable cache
140c1 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  s */.  unsigned 
140c2 69 6e 74 20 6d 78 50 69 6e 6e 65 64 3b 20 20 20  int mxPinned;   
140c3 20 20 20 20 20 20 2f 2a 20 6e 4d 61 78 70 61 67        /* nMaxpag
140c4 65 20 2b 20 31 30 20 2d 20 6e 4d 69 6e 50 61 67  e + 10 - nMinPag
140c5 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
140c6 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61 67 65  int nCurrentPage
140c7 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
140c8 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70 61 67  of purgeable pag
140c9 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  es allocated */.
140ca 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65    PgHdr1 *pLruHe
140cb 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20  ad, *pLruTail;  
140cc 20 2f 2a 20 4c 52 55 20 6c 69 73 74 20 6f 66 20   /* LRU list of 
140cd 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a  unpinned pages *
140ce 2f 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 70 61  /.};../* Each pa
140cf 67 65 20 63 61 63 68 65 20 69 73 20 61 6e 20 69  ge cache is an i
140d0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
140d1 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 74 2e  ollowing object.
140d2 20 20 45 76 65 72 79 0a 2a 2a 20 6f 70 65 6e 20    Every.** open 
140d3 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 69  database file (i
140d4 6e 63 6c 75 64 69 6e 67 20 65 61 63 68 20 69 6e  ncluding each in
140d5 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
140d6 20 61 6e 64 20 65 61 63 68 0a 2a 2a 20 74 65 6d   and each.** tem
140d7 70 6f 72 61 72 79 20 6f 72 20 74 72 61 6e 73 69  porary or transi
140d8 65 6e 74 20 64 61 74 61 62 61 73 65 29 20 68 61  ent database) ha
140d9 73 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20  s a single page 
140da 63 61 63 68 65 20 77 68 69 63 68 0a 2a 2a 20 69  cache which.** i
140db 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
140dc 20 74 68 69 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a   this object..**
140dd 0a 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20  .** Pointers to 
140de 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 74 68  structures of th
140df 69 73 20 74 79 70 65 20 61 72 65 20 63 61 73 74  is type are cast
140e0 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 61 73   and returned as
140e1 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73 71 6c 69   .** opaque sqli
140e2 74 65 33 5f 70 63 61 63 68 65 2a 20 68 61 6e 64  te3_pcache* hand
140e3 6c 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50  les..*/.struct P
140e4 43 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20 43 61  Cache1 {.  /* Ca
140e5 63 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  che configuratio
140e6 6e 20 70 61 72 61 6d 65 74 65 72 73 2e 20 50 61  n parameters. Pa
140e7 67 65 20 73 69 7a 65 20 28 73 7a 50 61 67 65 29  ge size (szPage)
140e8 20 61 6e 64 20 74 68 65 20 70 75 72 67 65 61 62   and the purgeab
140e9 6c 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28 62 50  le.  ** flag (bP
140ea 75 72 67 65 61 62 6c 65 29 20 61 72 65 20 73 65  urgeable) are se
140eb 74 20 77 68 65 6e 20 74 68 65 20 63 61 63 68 65  t when the cache
140ec 20 69 73 20 63 72 65 61 74 65 64 2e 20 6e 4d 61   is created. nMa
140ed 78 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a 20 6d  x may be .  ** m
140ee 6f 64 69 66 69 65 64 20 61 74 20 61 6e 79 20 74  odified at any t
140ef 69 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  ime by a call to
140f0 20 74 68 65 20 70 63 61 63 68 65 31 43 61 63 68   the pcache1Cach
140f1 65 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 2e 0a  esize() method..
140f2 20 20 2a 2a 20 54 68 65 20 50 47 72 6f 75 70 20    ** The PGroup 
140f3 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65  mutex must be he
140f4 6c 64 20 77 68 65 6e 20 61 63 63 65 73 73 69 6e  ld when accessin
140f5 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20 50  g nMax..  */.  P
140f6 47 72 6f 75 70 20 2a 70 47 72 6f 75 70 3b 20 20  Group *pGroup;  
140f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140f8 20 20 20 2f 2a 20 50 47 72 6f 75 70 20 74 68 69     /* PGroup thi
140f9 73 20 63 61 63 68 65 20 62 65 6c 6f 6e 67 73 20  s cache belongs 
140fa 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61  to */.  int szPa
140fb 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
140fc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
140fd 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64  ize of allocated
140fe 20 70 61 67 65 73 20 69 6e 20 62 79 74 65 73 20   pages in bytes 
140ff 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61  */.  int szExtra
14100 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
14101 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
14102 20 6f 66 20 65 78 74 72 61 20 73 70 61 63 65 20   of extra space 
14103 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e  in bytes */.  in
14104 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20  t bPurgeable;   
14105 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14106 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 63    /* True if cac
14107 68 65 20 69 73 20 70 75 72 67 65 61 62 6c 65 20  he is purgeable 
14108 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  */.  unsigned in
14109 74 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20 20  t nMin;         
1410a 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69           /* Mini
1410b 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  mum number of pa
1410c 67 65 73 20 72 65 73 65 72 76 65 64 20 2a 2f 0a  ges reserved */.
1410d 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e    unsigned int n
1410e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Max;            
1410f 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75        /* Configu
14110 72 65 64 20 22 63 61 63 68 65 5f 73 69 7a 65 22  red "cache_size"
14111 20 76 61 6c 75 65 20 2a 2f 0a 20 20 75 6e 73 69   value */.  unsi
14112 67 6e 65 64 20 69 6e 74 20 6e 39 30 70 63 74 3b  gned int n90pct;
14113 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14114 2f 2a 20 6e 4d 61 78 2a 39 2f 31 30 20 2a 2f 0a  /* nMax*9/10 */.
14115 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69    unsigned int i
14116 4d 61 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20  MaxKey;         
14117 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74        /* Largest
14118 20 6b 65 79 20 73 65 65 6e 20 73 69 6e 63 65 20   key seen since 
14119 78 54 72 75 6e 63 61 74 65 28 29 20 2a 2f 0a 0a  xTruncate() */..
1411a 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20    /* Hash table 
1411b 6f 66 20 61 6c 6c 20 70 61 67 65 73 2e 20 54 68  of all pages. Th
1411c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69  e following vari
1411d 61 62 6c 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62  ables may only b
1411e 65 20 61 63 63 65 73 73 65 64 0a 20 20 2a 2a 20  e accessed.  ** 
1411f 77 68 65 6e 20 74 68 65 20 61 63 63 65 73 73 6f  when the accesso
14120 72 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65  r is holding the
14121 20 50 47 72 6f 75 70 20 6d 75 74 65 78 2e 0a 20   PGroup mutex.. 
14122 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69   */.  unsigned i
14123 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 20  nt nRecyclable; 
14124 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
14125 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
14126 74 68 65 20 4c 52 55 20 6c 69 73 74 20 2a 2f 0a  the LRU list */.
14127 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e    unsigned int n
14128 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
14129 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
1412a 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
1412b 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20 20 75 6e  n apHash */.  un
1412c 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 61 73 68  signed int nHash
1412d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1412e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
1412f 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 68 5b 5d  lots in apHash[]
14130 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 2a 61   */.  PgHdr1 **a
14131 70 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20  pHash;          
14132 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
14133 68 20 74 61 62 6c 65 20 66 6f 72 20 66 61 73 74  h table for fast
14134 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 79 20 2a   lookup by key *
14135 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  /.};../*.** Each
14136 20 63 61 63 68 65 20 65 6e 74 72 79 20 69 73 20   cache entry is 
14137 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61  represented by a
14138 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
14139 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20  e following .** 
1413a 73 74 72 75 63 74 75 72 65 2e 20 55 6e 6c 65 73  structure. Unles
1413b 73 20 53 51 4c 49 54 45 5f 50 43 41 43 48 45 5f  s SQLITE_PCACHE_
1413c 53 45 50 41 52 41 54 45 5f 48 45 41 44 45 52 20  SEPARATE_HEADER 
1413d 69 73 20 64 65 66 69 6e 65 64 2c 20 61 20 62 75  is defined, a bu
1413e 66 66 65 72 20 6f 66 0a 2a 2a 20 50 67 48 64 72  ffer of.** PgHdr
1413f 31 2e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65  1.pCache->szPage
14140 20 62 79 74 65 73 20 69 73 20 61 6c 6c 6f 63 61   bytes is alloca
14141 74 65 64 20 64 69 72 65 63 74 6c 79 20 62 65 66  ted directly bef
14142 6f 72 65 20 74 68 69 73 20 73 74 72 75 63 74 75  ore this structu
14143 72 65 20 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79  re .** in memory
14144 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 64  ..*/.struct PgHd
14145 72 31 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 70  r1 {.  sqlite3_p
14146 63 61 63 68 65 5f 70 61 67 65 20 70 61 67 65 3b  cache_page page;
14147 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
14148 69 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  iKey;           
14149 20 20 2f 2a 20 4b 65 79 20 76 61 6c 75 65 20 28    /* Key value (
1414a 70 61 67 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a  page number) */.
1414b 20 20 50 67 48 64 72 31 20 2a 70 4e 65 78 74 3b    PgHdr1 *pNext;
1414c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1414d 20 2f 2a 20 4e 65 78 74 20 69 6e 20 68 61 73 68   /* Next in hash
1414e 20 74 61 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a   table chain */.
1414f 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68    PCache1 *pCach
14150 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
14151 20 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 63   /* Cache that c
14152 75 72 72 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68  urrently owns th
14153 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 48  is page */.  PgH
14154 64 72 31 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20  dr1 *pLruNext;  
14155 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
14156 65 78 74 20 69 6e 20 4c 52 55 20 6c 69 73 74 20  ext in LRU list 
14157 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65  of unpinned page
14158 73 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 70  s */.  PgHdr1 *p
14159 4c 72 75 50 72 65 76 3b 20 20 20 20 20 20 20 20  LruPrev;        
1415a 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75        /* Previou
1415b 73 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66  s in LRU list of
1415c 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20   unpinned pages 
1415d 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  */.};../*.** Fre
1415e 65 20 73 6c 6f 74 73 20 69 6e 20 74 68 65 20 61  e slots in the a
1415f 6c 6c 6f 63 61 74 6f 72 20 75 73 65 64 20 74 6f  llocator used to
14160 20 64 69 76 69 64 65 20 75 70 20 74 68 65 20 62   divide up the b
14161 75 66 66 65 72 20 70 72 6f 76 69 64 65 64 20 75  uffer provided u
14162 73 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49  sing.** the SQLI
14163 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41  TE_CONFIG_PAGECA
14164 43 48 45 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a  CHE mechanism..*
14165 2f 0a 73 74 72 75 63 74 20 50 67 46 72 65 65 73  /.struct PgFrees
14166 6c 6f 74 20 7b 0a 20 20 50 67 46 72 65 65 73 6c  lot {.  PgFreesl
14167 6f 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e  ot *pNext;  /* N
14168 65 78 74 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f  ext free slot */
14169 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61  .};../*.** Globa
1416a 6c 20 64 61 74 61 20 75 73 65 64 20 62 79 20 74  l data used by t
1416b 68 69 73 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74  his cache..*/.st
1416c 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
1416d 73 74 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f  struct PCacheGlo
1416e 62 61 6c 20 7b 0a 20 20 50 47 72 6f 75 70 20 67  bal {.  PGroup g
1416f 72 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rp;             
14170 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 67 6c         /* The gl
14171 6f 62 61 6c 20 50 47 72 6f 75 70 20 66 6f 72 20  obal PGroup for 
14172 6d 6f 64 65 20 28 32 29 20 2a 2f 0a 0a 20 20 2f  mode (2) */..  /
14173 2a 20 56 61 72 69 61 62 6c 65 73 20 72 65 6c 61  * Variables rela
14174 74 65 64 20 74 6f 20 53 51 4c 49 54 45 5f 43 4f  ted to SQLITE_CO
14175 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 73  NFIG_PAGECACHE s
14176 65 74 74 69 6e 67 73 2e 20 20 54 68 65 0a 20 20  ettings.  The.  
14177 2a 2a 20 73 7a 53 6c 6f 74 2c 20 6e 53 6c 6f 74  ** szSlot, nSlot
14178 2c 20 70 53 74 61 72 74 2c 20 70 45 6e 64 2c 20  , pStart, pEnd, 
14179 6e 52 65 73 65 72 76 65 2c 20 61 6e 64 20 69 73  nReserve, and is
1417a 49 6e 69 74 20 76 61 6c 75 65 73 20 61 72 65 20  Init values are 
1417b 61 6c 6c 0a 20 20 2a 2a 20 66 69 78 65 64 20 61  all.  ** fixed a
1417c 74 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  t sqlite3_initia
1417d 6c 69 7a 65 28 29 20 74 69 6d 65 20 61 6e 64 20  lize() time and 
1417e 64 6f 20 6e 6f 74 20 72 65 71 75 69 72 65 20 6d  do not require m
1417f 75 74 65 78 20 70 72 6f 74 65 63 74 69 6f 6e 2e  utex protection.
14180 0a 20 20 2a 2a 20 54 68 65 20 6e 46 72 65 65 53  .  ** The nFreeS
14181 6c 6f 74 20 61 6e 64 20 70 46 72 65 65 20 76 61  lot and pFree va
14182 6c 75 65 73 20 64 6f 20 72 65 71 75 69 72 65 20  lues do require 
14183 6d 75 74 65 78 20 70 72 6f 74 65 63 74 69 6f 6e  mutex protection
14184 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49  ..  */.  int isI
14185 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  nit;            
14186 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
14187 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a  if initialized *
14188 2f 0a 20 20 69 6e 74 20 73 7a 53 6c 6f 74 3b 20  /.  int szSlot; 
14189 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1418a 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61     /* Size of ea
1418b 63 68 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a  ch free slot */.
1418c 20 20 69 6e 74 20 6e 53 6c 6f 74 3b 20 20 20 20    int nSlot;    
1418d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1418e 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f   /* The number o
1418f 66 20 70 63 61 63 68 65 20 73 6c 6f 74 73 20 2a  f pcache slots *
14190 2f 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65  /.  int nReserve
14191 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
14192 20 20 20 2f 2a 20 54 72 79 20 74 6f 20 6b 65 65     /* Try to kee
14193 70 20 6e 46 72 65 65 53 6c 6f 74 20 61 62 6f 76  p nFreeSlot abov
14194 65 20 74 68 69 73 20 2a 2f 0a 20 20 76 6f 69 64  e this */.  void
14195 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b   *pStart, *pEnd;
14196 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f             /* Bo
14197 75 6e 64 73 20 6f 66 20 70 61 67 65 63 61 63 68  unds of pagecach
14198 65 20 6d 61 6c 6c 6f 63 20 72 61 6e 67 65 20 2a  e malloc range *
14199 2f 0a 20 20 2f 2a 20 41 62 6f 76 65 20 72 65 71  /.  /* Above req
1419a 75 69 72 65 73 20 6e 6f 20 6d 75 74 65 78 2e 20  uires no mutex. 
1419b 20 55 73 65 20 6d 75 74 65 78 20 62 65 6c 6f 77   Use mutex below
1419c 20 66 6f 72 20 76 61 72 69 61 62 6c 65 20 74 68   for variable th
1419d 61 74 20 66 6f 6c 6c 6f 77 2e 20 2a 2f 0a 20 20  at follow. */.  
1419e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
1419f 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 2f  utex;          /
141a0 2a 20 4d 75 74 65 78 20 66 6f 72 20 61 63 63 65  * Mutex for acce
141a1 73 73 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77  ssing the follow
141a2 69 6e 67 3a 20 2a 2f 0a 20 20 50 67 46 72 65 65  ing: */.  PgFree
141a3 73 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 20  slot *pFree;    
141a4 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65           /* Free
141a5 20 70 61 67 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a   page blocks */.
141a6 20 20 69 6e 74 20 6e 46 72 65 65 53 6c 6f 74 3b    int nFreeSlot;
141a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
141a8 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e   /* Number of un
141a9 75 73 65 64 20 70 63 61 63 68 65 20 73 6c 6f 74  used pcache slot
141aa 73 20 2a 2f 0a 20 20 2f 2a 20 54 68 65 20 66 6f  s */.  /* The fo
141ab 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 72 65  llowing value re
141ac 71 75 69 72 65 73 20 61 20 6d 75 74 65 78 20 74  quires a mutex t
141ad 6f 20 63 68 61 6e 67 65 2e 20 20 57 65 20 73 6b  o change.  We sk
141ae 69 70 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 0a  ip the mutex on.
141af 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 62 65 63    ** reading bec
141b0 61 75 73 65 20 28 31 29 20 6d 6f 73 74 20 70 6c  ause (1) most pl
141b1 61 74 66 6f 72 6d 73 20 72 65 61 64 20 61 20 33  atforms read a 3
141b2 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 61 74  2-bit integer at
141b3 6f 6d 69 63 61 6c 6c 79 20 61 6e 64 0a 20 20 2a  omically and.  *
141b4 2a 20 28 32 29 20 65 76 65 6e 20 69 66 20 61 6e  * (2) even if an
141b5 20 69 6e 63 6f 72 72 65 63 74 20 76 61 6c 75 65   incorrect value
141b6 20 69 73 20 72 65 61 64 2c 20 6e 6f 20 67 72 65   is read, no gre
141b7 61 74 20 68 61 72 6d 20 69 73 20 64 6f 6e 65 20  at harm is done 
141b8 73 69 6e 63 65 20 74 68 69 73 0a 20 20 2a 2a 20  since this.  ** 
141b9 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61  is really just a
141ba 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20  n optimization. 
141bb 2a 2f 0a 20 20 69 6e 74 20 62 55 6e 64 65 72 50  */.  int bUnderP
141bc 72 65 73 73 75 72 65 3b 20 20 20 20 20 20 20 20  ressure;        
141bd 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c      /* True if l
141be 6f 77 20 6f 6e 20 50 41 47 45 43 41 43 48 45 20  ow on PAGECACHE 
141bf 6d 65 6d 6f 72 79 20 2a 2f 0a 7d 20 70 63 61 63  memory */.} pcac
141c0 68 65 31 5f 67 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c  he1_g;../*.** Al
141c1 6c 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  l code in this f
141c2 69 6c 65 20 73 68 6f 75 6c 64 20 61 63 63 65 73  ile should acces
141c3 73 20 74 68 65 20 67 6c 6f 62 61 6c 20 73 74 72  s the global str
141c4 75 63 74 75 72 65 20 61 62 6f 76 65 20 76 69 61  ucture above via
141c5 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73 20 22 70   the.** alias "p
141c6 63 61 63 68 65 31 22 2e 20 54 68 69 73 20 65 6e  cache1". This en
141c7 73 75 72 65 73 20 74 68 61 74 20 74 68 65 20 57  sures that the W
141c8 53 44 20 65 6d 75 6c 61 74 69 6f 6e 20 69 73 20  SD emulation is 
141c9 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20 63 6f 6d  used when.** com
141ca 70 69 6c 69 6e 67 20 66 6f 72 20 73 79 73 74 65  piling for syste
141cb 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 73  ms that do not s
141cc 75 70 70 6f 72 74 20 72 65 61 6c 20 57 53 44 2e  upport real WSD.
141cd 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70 63 61 63  .*/.#define pcac
141ce 68 65 31 20 28 47 4c 4f 42 41 4c 28 73 74 72 75  he1 (GLOBAL(stru
141cf 63 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c 2c  ct PCacheGlobal,
141d0 20 70 63 61 63 68 65 31 5f 67 29 29 0a 0a 2f 2a   pcache1_g))../*
141d1 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20 65 6e  .** Macros to en
141d2 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 74 68  ter and leave th
141d3 65 20 50 43 61 63 68 65 20 4c 52 55 20 6d 75 74  e PCache LRU mut
141d4 65 78 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70  ex..*/.#define p
141d5 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78  cache1EnterMutex
141d6 28 58 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  (X) sqlite3_mute
141d7 78 5f 65 6e 74 65 72 28 28 58 29 2d 3e 6d 75 74  x_enter((X)->mut
141d8 65 78 29 0a 23 64 65 66 69 6e 65 20 70 63 61 63  ex).#define pcac
141d9 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 58 29  he1LeaveMutex(X)
141da 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
141db 65 61 76 65 28 28 58 29 2d 3e 6d 75 74 65 78 29  eave((X)->mutex)
141dc 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
141dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
141de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
141df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
141e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
141e1 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 50 61 67  */./******** Pag
141e2 65 20 41 6c 6c 6f 63 61 74 69 6f 6e 2f 53 51 4c  e Allocation/SQL
141e3 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48  ITE_CONFIG_PCACH
141e4 45 20 52 65 6c 61 74 65 64 20 46 75 6e 63 74 69  E Related Functi
141e5 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ons ************
141e6 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  **/../*.** This 
141e7 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
141e8 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61  ed during initia
141e9 6c 69 7a 61 74 69 6f 6e 20 69 66 20 61 20 73 74  lization if a st
141ea 61 74 69 63 20 62 75 66 66 65 72 20 69 73 20 0a  atic buffer is .
141eb 2a 2a 20 73 75 70 70 6c 69 65 64 20 74 6f 20 75  ** supplied to u
141ec 73 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 2d  se for the page-
141ed 63 61 63 68 65 20 62 79 20 70 61 73 73 69 6e 67  cache by passing
141ee 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46   the SQLITE_CONF
141ef 49 47 5f 50 41 47 45 43 41 43 48 45 0a 2a 2a 20  IG_PAGECACHE.** 
141f0 76 65 72 62 20 74 6f 20 73 71 6c 69 74 65 33 5f  verb to sqlite3_
141f1 63 6f 6e 66 69 67 28 29 2e 20 50 61 72 61 6d 65  config(). Parame
141f2 74 65 72 20 70 42 75 66 20 70 6f 69 6e 74 73 20  ter pBuf points 
141f3 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  to an allocation
141f4 20 6c 61 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 68   large.** enough
141f5 20 74 6f 20 63 6f 6e 74 61 69 6e 20 27 6e 27 20   to contain 'n' 
141f6 62 75 66 66 65 72 73 20 6f 66 20 27 73 7a 27 20  buffers of 'sz' 
141f7 62 79 74 65 73 20 65 61 63 68 2e 0a 2a 2a 0a 2a  bytes each..**.*
141f8 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
141f9 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73 71  s called from sq
141fa 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
141fb 28 29 20 61 6e 64 20 73 6f 20 69 74 20 69 73 20  () and so it is 
141fc 67 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f  guaranteed.** to
141fd 20 62 65 20 73 65 72 69 61 6c 69 7a 65 64 20 61   be serialized a
141fe 6c 72 65 61 64 79 2e 20 20 54 68 65 72 65 20 69  lready.  There i
141ff 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 66 75  s no need for fu
14200 72 74 68 65 72 20 6d 75 74 65 78 69 6e 67 2e 0a  rther mutexing..
14201 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14202 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43  E void sqlite3PC
14203 61 63 68 65 42 75 66 66 65 72 53 65 74 75 70 28  acheBufferSetup(
14204 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20  void *pBuf, int 
14205 73 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 66  sz, int n){.  if
14206 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e 69 74  ( pcache1.isInit
14207 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73 6c   ){.    PgFreesl
14208 6f 74 20 2a 70 3b 0a 20 20 20 20 73 7a 20 3d 20  ot *p;.    sz = 
14209 52 4f 55 4e 44 44 4f 57 4e 38 28 73 7a 29 3b 0a  ROUNDDOWN8(sz);.
1420a 20 20 20 20 70 63 61 63 68 65 31 2e 73 7a 53 6c      pcache1.szSl
1420b 6f 74 20 3d 20 73 7a 3b 0a 20 20 20 20 70 63 61  ot = sz;.    pca
1420c 63 68 65 31 2e 6e 53 6c 6f 74 20 3d 20 70 63 61  che1.nSlot = pca
1420d 63 68 65 31 2e 6e 46 72 65 65 53 6c 6f 74 20 3d  che1.nFreeSlot =
1420e 20 6e 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e   n;.    pcache1.
1420f 6e 52 65 73 65 72 76 65 20 3d 20 6e 3e 39 30 20  nReserve = n>90 
14210 3f 20 31 30 20 3a 20 28 6e 2f 31 30 20 2b 20 31  ? 10 : (n/10 + 1
14211 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70  );.    pcache1.p
14212 53 74 61 72 74 20 3d 20 70 42 75 66 3b 0a 20 20  Start = pBuf;.  
14213 20 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20    pcache1.pFree 
14214 3d 20 30 3b 0a 20 20 20 20 70 63 61 63 68 65 31  = 0;.    pcache1
14215 2e 62 55 6e 64 65 72 50 72 65 73 73 75 72 65 20  .bUnderPressure 
14216 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  = 0;.    while( 
14217 6e 2d 2d 20 29 7b 0a 20 20 20 20 20 20 70 20 3d  n-- ){.      p =
14218 20 28 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 42   (PgFreeslot*)pB
14219 75 66 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65  uf;.      p->pNe
1421a 78 74 20 3d 20 70 63 61 63 68 65 31 2e 70 46 72  xt = pcache1.pFr
1421b 65 65 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65  ee;.      pcache
1421c 31 2e 70 46 72 65 65 20 3d 20 70 3b 0a 20 20 20  1.pFree = p;.   
1421d 20 20 20 70 42 75 66 20 3d 20 28 76 6f 69 64 2a     pBuf = (void*
1421e 29 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b  )&((char*)pBuf)[
1421f 73 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  sz];.    }.    p
14220 63 61 63 68 65 31 2e 70 45 6e 64 20 3d 20 70 42  cache1.pEnd = pB
14221 75 66 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  uf;.  }.}../*.**
14222 20 4d 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e   Malloc function
14223 20 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 69   used within thi
14224 73 20 66 69 6c 65 20 74 6f 20 61 6c 6c 6f 63 61  s file to alloca
14225 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 74 68  te space from th
14226 65 20 62 75 66 66 65 72 0a 2a 2a 20 63 6f 6e 66  e buffer.** conf
14227 69 67 75 72 65 64 20 75 73 69 6e 67 20 73 71 6c  igured using sql
14228 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49  ite3_config(SQLI
14229 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41  TE_CONFIG_PAGECA
1422a 43 48 45 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20  CHE) option. If 
1422b 6e 6f 20 0a 2a 2a 20 73 75 63 68 20 62 75 66 66  no .** such buff
1422c 65 72 20 65 78 69 73 74 73 20 6f 72 20 74 68 65  er exists or the
1422d 72 65 20 69 73 20 6e 6f 20 73 70 61 63 65 20 6c  re is no space l
1422e 65 66 74 20 69 6e 20 69 74 2c 20 74 68 69 73 20  eft in it, this 
1422f 66 75 6e 63 74 69 6f 6e 20 66 61 6c 6c 73 20 0a  function falls .
14230 2a 2a 20 62 61 63 6b 20 74 6f 20 73 71 6c 69 74  ** back to sqlit
14231 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a  e3Malloc()..**.*
14232 2a 20 4d 75 6c 74 69 70 6c 65 20 74 68 72 65 61  * Multiple threa
14233 64 73 20 63 61 6e 20 72 75 6e 20 74 68 69 73 20  ds can run this 
14234 72 6f 75 74 69 6e 65 20 61 74 20 74 68 65 20 73  routine at the s
14235 61 6d 65 20 74 69 6d 65 2e 20 20 47 6c 6f 62 61  ame time.  Globa
14236 6c 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 69  l variables.** i
14237 6e 20 70 63 61 63 68 65 31 20 6e 65 65 64 20 74  n pcache1 need t
14238 6f 20 62 65 20 70 72 6f 74 65 63 74 65 64 20 76  o be protected v
14239 69 61 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61  ia mutex..*/.sta
1423a 74 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 65  tic void *pcache
1423b 31 41 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65  1Alloc(int nByte
1423c 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30  ){.  void *p = 0
1423d 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
1423e 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
1423f 64 28 70 63 61 63 68 65 31 2e 67 72 70 2e 6d 75  d(pcache1.grp.mu
14240 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
14241 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54  3StatusSet(SQLIT
14242 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43  E_STATUS_PAGECAC
14243 48 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b  HE_SIZE, nByte);
14244 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 70 63  .  if( nByte<=pc
14245 61 63 68 65 31 2e 73 7a 53 6c 6f 74 20 29 7b 0a  ache1.szSlot ){.
14246 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
14247 78 5f 65 6e 74 65 72 28 70 63 61 63 68 65 31 2e  x_enter(pcache1.
14248 6d 75 74 65 78 29 3b 0a 20 20 20 20 70 20 3d 20  mutex);.    p = 
14249 28 50 67 48 64 72 31 20 2a 29 70 63 61 63 68 65  (PgHdr1 *)pcache
1424a 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 69 66 28  1.pFree;.    if(
1424b 20 70 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63   p ){.      pcac
1424c 68 65 31 2e 70 46 72 65 65 20 3d 20 70 63 61 63  he1.pFree = pcac
1424d 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e 65 78 74  he1.pFree->pNext
1424e 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e  ;.      pcache1.
1424f 6e 46 72 65 65 53 6c 6f 74 2d 2d 3b 0a 20 20 20  nFreeSlot--;.   
14250 20 20 20 70 63 61 63 68 65 31 2e 62 55 6e 64 65     pcache1.bUnde
14251 72 50 72 65 73 73 75 72 65 20 3d 20 70 63 61 63  rPressure = pcac
14252 68 65 31 2e 6e 46 72 65 65 53 6c 6f 74 3c 70 63  he1.nFreeSlot<pc
14253 61 63 68 65 31 2e 6e 52 65 73 65 72 76 65 3b 0a  ache1.nReserve;.
14254 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 63        assert( pc
14255 61 63 68 65 31 2e 6e 46 72 65 65 53 6c 6f 74 3e  ache1.nFreeSlot>
14256 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
14257 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
14258 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
14259 41 43 48 45 5f 55 53 45 44 2c 20 31 29 3b 0a 20  ACHE_USED, 1);. 
1425a 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1425b 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61  _mutex_leave(pca
1425c 63 68 65 31 2e 6d 75 74 65 78 29 3b 0a 20 20 7d  che1.mutex);.  }
1425d 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20  .  if( p==0 ){. 
1425e 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 69 73 20     /* Memory is 
1425f 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e  not available in
14260 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46   the SQLITE_CONF
14261 49 47 5f 50 41 47 45 43 41 43 48 45 20 70 6f 6f  IG_PAGECACHE poo
14262 6c 2e 20 20 47 65 74 0a 20 20 20 20 2a 2a 20 69  l.  Get.    ** i
14263 74 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61  t from sqlite3Ma
14264 6c 6c 6f 63 20 69 6e 73 74 65 61 64 2e 0a 20 20  lloc instead..  
14265 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 73 71 6c    */.    p = sql
14266 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 65  ite3Malloc(nByte
14267 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
14268 45 5f 44 49 53 41 42 4c 45 5f 50 41 47 45 43 41  E_DISABLE_PAGECA
14269 43 48 45 5f 4f 56 45 52 46 4c 4f 57 5f 53 54 41  CHE_OVERFLOW_STA
1426a 54 53 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a  TS.    if( p ){.
1426b 20 20 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 73        int sz = s
1426c 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65  qlite3MallocSize
1426d 28 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  (p);.      sqlit
1426e 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
1426f 63 61 63 68 65 31 2e 6d 75 74 65 78 29 3b 0a 20  cache1.mutex);. 
14270 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74       sqlite3Stat
14271 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
14272 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56  TUS_PAGECACHE_OV
14273 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20 20 20  ERFLOW, sz);.   
14274 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
14275 5f 6c 65 61 76 65 28 70 63 61 63 68 65 31 2e 6d  _leave(pcache1.m
14276 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  utex);.    }.#en
14277 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 4d  dif.    sqlite3M
14278 65 6d 64 65 62 75 67 53 65 74 54 79 70 65 28 70  emdebugSetType(p
14279 2c 20 4d 45 4d 54 59 50 45 5f 50 43 41 43 48 45  , MEMTYPE_PCACHE
1427a 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1427b 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  p;.}../*.** Free
1427c 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75   an allocated bu
1427d 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 72  ffer obtained fr
1427e 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28  om pcache1Alloc(
1427f 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
14280 20 70 63 61 63 68 65 31 46 72 65 65 28 76 6f 69   pcache1Free(voi
14281 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 46 72  d *p){.  int nFr
14282 65 65 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  eed = 0;.  if( p
14283 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
14284 20 20 69 66 28 20 70 3e 3d 70 63 61 63 68 65 31    if( p>=pcache1
14285 2e 70 53 74 61 72 74 20 26 26 20 70 3c 70 63 61  .pStart && p<pca
14286 63 68 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 20  che1.pEnd ){.   
14287 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c   PgFreeslot *pSl
14288 6f 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ot;.    sqlite3_
14289 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63 61 63  mutex_enter(pcac
1428a 68 65 31 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  he1.mutex);.    
1428b 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
1428c 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50  (SQLITE_STATUS_P
1428d 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c 20 2d  AGECACHE_USED, -
1428e 31 29 3b 0a 20 20 20 20 70 53 6c 6f 74 20 3d 20  1);.    pSlot = 
1428f 28 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 3b 0a  (PgFreeslot*)p;.
14290 20 20 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74      pSlot->pNext
14291 20 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65   = pcache1.pFree
14292 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 46  ;.    pcache1.pF
14293 72 65 65 20 3d 20 70 53 6c 6f 74 3b 0a 20 20 20  ree = pSlot;.   
14294 20 70 63 61 63 68 65 31 2e 6e 46 72 65 65 53 6c   pcache1.nFreeSl
14295 6f 74 2b 2b 3b 0a 20 20 20 20 70 63 61 63 68 65  ot++;.    pcache
14296 31 2e 62 55 6e 64 65 72 50 72 65 73 73 75 72 65  1.bUnderPressure
14297 20 3d 20 70 63 61 63 68 65 31 2e 6e 46 72 65 65   = pcache1.nFree
14298 53 6c 6f 74 3c 70 63 61 63 68 65 31 2e 6e 52 65  Slot<pcache1.nRe
14299 73 65 72 76 65 3b 0a 20 20 20 20 61 73 73 65 72  serve;.    asser
1429a 74 28 20 70 63 61 63 68 65 31 2e 6e 46 72 65 65  t( pcache1.nFree
1429b 53 6c 6f 74 3c 3d 70 63 61 63 68 65 31 2e 6e 53  Slot<=pcache1.nS
1429c 6c 6f 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  lot );.    sqlit
1429d 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
1429e 63 61 63 68 65 31 2e 6d 75 74 65 78 29 3b 0a 20  cache1.mutex);. 
1429f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
142a0 72 74 28 20 73 71 6c 69 74 65 33 4d 65 6d 64 65  rt( sqlite3Memde
142a1 62 75 67 48 61 73 54 79 70 65 28 70 2c 20 4d 45  bugHasType(p, ME
142a2 4d 54 59 50 45 5f 50 43 41 43 48 45 29 20 29 3b  MTYPE_PCACHE) );
142a3 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 64  .    sqlite3Memd
142a4 65 62 75 67 53 65 74 54 79 70 65 28 70 2c 20 4d  ebugSetType(p, M
142a5 45 4d 54 59 50 45 5f 48 45 41 50 29 3b 0a 20 20  EMTYPE_HEAP);.  
142a6 20 20 6e 46 72 65 65 64 20 3d 20 73 71 6c 69 74    nFreed = sqlit
142a7 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b  e3MallocSize(p);
142a8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
142a9 44 49 53 41 42 4c 45 5f 50 41 47 45 43 41 43 48  DISABLE_PAGECACH
142aa 45 5f 4f 56 45 52 46 4c 4f 57 5f 53 54 41 54 53  E_OVERFLOW_STATS
142ab 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
142ac 65 78 5f 65 6e 74 65 72 28 70 63 61 63 68 65 31  ex_enter(pcache1
142ad 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c  .mutex);.    sql
142ae 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51  ite3StatusAdd(SQ
142af 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
142b0 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20  CACHE_OVERFLOW, 
142b1 2d 6e 46 72 65 65 64 29 3b 0a 20 20 20 20 73 71  -nFreed);.    sq
142b2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
142b3 65 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29  e(pcache1.mutex)
142b4 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c  ;.#endif.    sql
142b5 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
142b6 7d 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65  }.  return nFree
142b7 64 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  d;.}..#ifdef SQL
142b8 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ITE_ENABLE_MEMOR
142b9 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a  Y_MANAGEMENT./*.
142ba 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69  ** Return the si
142bb 7a 65 20 6f 66 20 61 20 70 63 61 63 68 65 20 61  ze of a pcache a
142bc 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61  llocation.*/.sta
142bd 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 4d  tic int pcache1M
142be 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b  emSize(void *p){
142bf 0a 20 20 69 66 28 20 70 3e 3d 70 63 61 63 68 65  .  if( p>=pcache
142c0 31 2e 70 53 74 61 72 74 20 26 26 20 70 3c 70 63  1.pStart && p<pc
142c1 61 63 68 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20  ache1.pEnd ){.  
142c2 20 20 72 65 74 75 72 6e 20 70 63 61 63 68 65 31    return pcache1
142c3 2e 73 7a 53 6c 6f 74 3b 0a 20 20 7d 65 6c 73 65  .szSlot;.  }else
142c4 7b 0a 20 20 20 20 69 6e 74 20 69 53 69 7a 65 3b  {.    int iSize;
142c5 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
142c6 69 74 65 33 4d 65 6d 64 65 62 75 67 48 61 73 54  ite3MemdebugHasT
142c7 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 50  ype(p, MEMTYPE_P
142c8 43 41 43 48 45 29 20 29 3b 0a 20 20 20 20 73 71  CACHE) );.    sq
142c9 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74  lite3MemdebugSet
142ca 54 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f  Type(p, MEMTYPE_
142cb 48 45 41 50 29 3b 0a 20 20 20 20 69 53 69 7a 65  HEAP);.    iSize
142cc 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
142cd 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 73 71 6c  Size(p);.    sql
142ce 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74 54  ite3MemdebugSetT
142cf 79 70 65 28 70 2c 20 4d 45 4d 54 59 50 45 5f 50  ype(p, MEMTYPE_P
142d0 43 41 43 48 45 29 3b 0a 20 20 20 20 72 65 74 75  CACHE);.    retu
142d1 72 6e 20 69 53 69 7a 65 3b 0a 20 20 7d 0a 7d 0a  rn iSize;.  }.}.
142d2 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
142d3 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
142d4 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a 2f 2a  ANAGEMENT */../*
142d5 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e  .** Allocate a n
142d6 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69  ew page object i
142d7 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61  nitially associa
142d8 74 65 64 20 77 69 74 68 20 63 61 63 68 65 20 70  ted with cache p
142d9 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  Cache..*/.static
142da 20 50 67 48 64 72 31 20 2a 70 63 61 63 68 65 31   PgHdr1 *pcache1
142db 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65  AllocPage(PCache
142dc 31 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67  1 *pCache){.  Pg
142dd 48 64 72 31 20 2a 70 20 3d 20 30 3b 0a 20 20 76  Hdr1 *p = 0;.  v
142de 6f 69 64 20 2a 70 50 67 3b 0a 0a 20 20 2f 2a 20  oid *pPg;..  /* 
142df 54 68 65 20 67 72 6f 75 70 20 6d 75 74 65 78 20  The group mutex 
142e0 6d 75 73 74 20 62 65 20 72 65 6c 65 61 73 65 64  must be released
142e1 20 62 65 66 6f 72 65 20 70 63 61 63 68 65 31 41   before pcache1A
142e2 6c 6c 6f 63 28 29 20 69 73 20 63 61 6c 6c 65 64  lloc() is called
142e3 2e 20 54 68 69 73 0a 20 20 2a 2a 20 69 73 20 62  . This.  ** is b
142e4 65 63 61 75 73 65 20 69 74 20 6d 61 79 20 63 61  ecause it may ca
142e5 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  ll sqlite3_relea
142e6 73 65 5f 6d 65 6d 6f 72 79 28 29 2c 20 77 68 69  se_memory(), whi
142e7 63 68 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  ch assumes that 
142e8 0a 20 20 2a 2a 20 74 68 69 73 20 6d 75 74 65 78  .  ** this mutex
142e9 20 69 73 20 6e 6f 74 20 68 65 6c 64 2e 20 2a 2f   is not held. */
142ea 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
142eb 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
142ec 61 63 68 65 2d 3e 70 47 72 6f 75 70 2d 3e 6d 75  ache->pGroup->mu
142ed 74 65 78 29 20 29 3b 0a 20 20 70 63 61 63 68 65  tex) );.  pcache
142ee 31 4c 65 61 76 65 4d 75 74 65 78 28 70 43 61 63  1LeaveMutex(pCac
142ef 68 65 2d 3e 70 47 72 6f 75 70 29 3b 0a 23 69 66  he->pGroup);.#if
142f0 64 65 66 20 53 51 4c 49 54 45 5f 50 43 41 43 48  def SQLITE_PCACH
142f1 45 5f 53 45 50 41 52 41 54 45 5f 48 45 41 44 45  E_SEPARATE_HEADE
142f2 52 0a 20 20 70 50 67 20 3d 20 70 63 61 63 68 65  R.  pPg = pcache
142f3 31 41 6c 6c 6f 63 28 70 43 61 63 68 65 2d 3e 73  1Alloc(pCache->s
142f4 7a 50 61 67 65 29 3b 0a 20 20 70 20 3d 20 73 71  zPage);.  p = sq
142f5 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 69 7a 65  lite3Malloc(size
142f6 6f 66 28 50 67 48 64 72 31 29 20 2b 20 70 43 61  of(PgHdr1) + pCa
142f7 63 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20  che->szExtra);. 
142f8 20 69 66 28 20 21 70 50 67 20 7c 7c 20 21 70 20   if( !pPg || !p 
142f9 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 46 72  ){.    pcache1Fr
142fa 65 65 28 70 50 67 29 3b 0a 20 20 20 20 73 71 6c  ee(pPg);.    sql
142fb 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
142fc 20 20 70 50 67 20 3d 20 30 3b 0a 20 20 7d 0a 23    pPg = 0;.  }.#
142fd 65 6c 73 65 0a 20 20 70 50 67 20 3d 20 70 63 61  else.  pPg = pca
142fe 63 68 65 31 41 6c 6c 6f 63 28 73 69 7a 65 6f 66  che1Alloc(sizeof
142ff 28 50 67 48 64 72 31 29 20 2b 20 70 43 61 63 68  (PgHdr1) + pCach
14300 65 2d 3e 73 7a 50 61 67 65 20 2b 20 70 43 61 63  e->szPage + pCac
14301 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 20  he->szExtra);.  
14302 70 20 3d 20 28 50 67 48 64 72 31 20 2a 29 26 28  p = (PgHdr1 *)&(
14303 28 75 38 20 2a 29 70 50 67 29 5b 70 43 61 63 68  (u8 *)pPg)[pCach
14304 65 2d 3e 73 7a 50 61 67 65 5d 3b 0a 23 65 6e 64  e->szPage];.#end
14305 69 66 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65  if.  pcache1Ente
14306 72 4d 75 74 65 78 28 70 43 61 63 68 65 2d 3e 70  rMutex(pCache->p
14307 47 72 6f 75 70 29 3b 0a 0a 20 20 69 66 28 20 70  Group);..  if( p
14308 50 67 20 29 7b 0a 20 20 20 20 70 2d 3e 70 61 67  Pg ){.    p->pag
14309 65 2e 70 42 75 66 20 3d 20 70 50 67 3b 0a 20 20  e.pBuf = pPg;.  
1430a 20 20 70 2d 3e 70 61 67 65 2e 70 45 78 74 72 61    p->page.pExtra
1430b 20 3d 20 26 70 5b 31 5d 3b 0a 20 20 20 20 69 66   = &p[1];.    if
1430c 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65  ( pCache->bPurge
1430d 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43  able ){.      pC
1430e 61 63 68 65 2d 3e 70 47 72 6f 75 70 2d 3e 6e 43  ache->pGroup->nC
1430f 75 72 72 65 6e 74 50 61 67 65 2b 2b 3b 0a 20 20  urrentPage++;.  
14310 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 70    }.    return p
14311 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
14312 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
14313 61 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 6c  a page object al
14314 6c 6f 63 61 74 65 64 20 62 79 20 70 63 61 63 68  located by pcach
14315 65 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a 2a  e1AllocPage()..*
14316 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72  *.** The pointer
14317 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62   is allowed to b
14318 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73  e NULL, which is
14319 20 70 72 75 64 65 6e 74 2e 20 20 42 75 74 20 69   prudent.  But i
1431a 74 20 74 75 72 6e 73 20 6f 75 74 0a 2a 2a 20 74  t turns out.** t
1431b 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  hat the current 
1431c 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68  implementation h
1431d 61 70 70 65 6e 73 20 74 6f 20 6e 65 76 65 72 20  appens to never 
1431e 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  call this routin
1431f 65 0a 2a 2a 20 77 69 74 68 20 61 20 4e 55 4c 4c  e.** with a NULL
14320 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 77 65 20   pointer, so we 
14321 6d 61 72 6b 20 74 68 65 20 4e 55 4c 4c 20 74 65  mark the NULL te
14322 73 74 20 77 69 74 68 20 41 4c 57 41 59 53 28 29  st with ALWAYS()
14323 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
14324 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65   pcache1FreePage
14325 28 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20 69  (PgHdr1 *p){.  i
14326 66 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b 0a  f( ALWAYS(p) ){.
14327 20 20 20 20 50 43 61 63 68 65 31 20 2a 70 43 61      PCache1 *pCa
14328 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b  che = p->pCache;
14329 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
1432a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1432b 70 2d 3e 70 43 61 63 68 65 2d 3e 70 47 72 6f 75  p->pCache->pGrou
1432c 70 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20  p->mutex) );.   
1432d 20 70 63 61 63 68 65 31 46 72 65 65 28 70 2d 3e   pcache1Free(p->
1432e 70 61 67 65 2e 70 42 75 66 29 3b 0a 23 69 66 64  page.pBuf);.#ifd
1432f 65 66 20 53 51 4c 49 54 45 5f 50 43 41 43 48 45  ef SQLITE_PCACHE
14330 5f 53 45 50 41 52 41 54 45 5f 48 45 41 44 45 52  _SEPARATE_HEADER
14331 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
14332 65 28 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  e(p);.#endif.   
14333 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75   if( pCache->bPu
14334 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  rgeable ){.     
14335 20 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70 2d   pCache->pGroup-
14336 3e 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b  >nCurrentPage--;
14337 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
14338 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 6e 63 74  .** Malloc funct
14339 69 6f 6e 20 75 73 65 64 20 62 79 20 53 51 4c 69  ion used by SQLi
1433a 74 65 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 61  te to obtain spa
1433b 63 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66  ce from the buff
1433c 65 72 20 63 6f 6e 66 69 67 75 72 65 64 0a 2a 2a  er configured.**
1433d 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 63   using sqlite3_c
1433e 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e  onfig(SQLITE_CON
1433f 46 49 47 5f 50 41 47 45 43 41 43 48 45 29 20 6f  FIG_PAGECACHE) o
14340 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 73 75 63  ption. If no suc
14341 68 20 62 75 66 66 65 72 0a 2a 2a 20 65 78 69 73  h buffer.** exis
14342 74 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ts, this functio
14343 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f 20  n falls back to 
14344 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e  sqlite3Malloc().
14345 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14346 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
14347 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 20 73  PageMalloc(int s
14348 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 63 61  z){.  return pca
14349 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b 0a 7d  che1Alloc(sz);.}
1434a 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20  ../*.** Free an 
1434b 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72  allocated buffer
1434c 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
1434d 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63  qlite3PageMalloc
1434e 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
1434f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
14350 65 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 20  e3PageFree(void 
14351 2a 70 29 7b 0a 20 20 70 63 61 63 68 65 31 46 72  *p){.  pcache1Fr
14352 65 65 28 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  ee(p);.}.../*.**
14353 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
14354 69 74 20 64 65 73 69 72 61 62 6c 65 20 74 6f 20  it desirable to 
14355 61 76 6f 69 64 20 61 6c 6c 6f 63 61 74 69 6e 67  avoid allocating
14356 20 61 20 6e 65 77 20 70 61 67 65 20 63 61 63 68   a new page cach
14357 65 0a 2a 2a 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a  e.** entry..**.*
14358 2a 20 49 66 20 6d 65 6d 6f 72 79 20 77 61 73 20  * If memory was 
14359 61 6c 6c 6f 63 61 74 65 64 20 73 70 65 63 69 66  allocated specif
1435a 69 63 61 6c 6c 79 20 74 6f 20 74 68 65 20 70 61  ically to the pa
1435b 67 65 20 63 61 63 68 65 20 75 73 69 6e 67 0a 2a  ge cache using.*
1435c 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  * SQLITE_CONFIG_
1435d 50 41 47 45 43 41 43 48 45 20 62 75 74 20 74 68  PAGECACHE but th
1435e 61 74 20 6d 65 6d 6f 72 79 20 68 61 73 20 61 6c  at memory has al
1435f 6c 20 62 65 65 6e 20 75 73 65 64 2c 20 74 68 65  l been used, the
14360 6e 0a 2a 2a 20 69 74 20 69 73 20 64 65 73 69 72  n.** it is desir
14361 61 62 6c 65 20 74 6f 20 61 76 6f 69 64 20 61 6c  able to avoid al
14362 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 70  locating a new p
14363 61 67 65 20 63 61 63 68 65 20 65 6e 74 72 79 20  age cache entry 
14364 62 65 63 61 75 73 65 0a 2a 2a 20 70 72 65 73 75  because.** presu
14365 6d 61 62 6c 79 20 53 51 4c 49 54 45 5f 43 4f 4e  mably SQLITE_CON
14366 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 77 61  FIG_PAGECACHE wa
14367 73 20 73 75 70 70 6f 73 65 20 74 6f 20 62 65 20  s suppose to be 
14368 73 75 66 66 69 63 69 65 6e 74 0a 2a 2a 20 66 6f  sufficient.** fo
14369 72 20 61 6c 6c 20 70 61 67 65 20 63 61 63 68 65  r all page cache
1436a 20 6e 65 65 64 73 20 61 6e 64 20 77 65 20 73 68   needs and we sh
1436b 6f 75 6c 64 20 6e 6f 74 20 6e 65 65 64 20 74 6f  ould not need to
1436c 20 73 70 69 6c 6c 20 74 68 65 0a 2a 2a 20 61 6c   spill the.** al
1436d 6c 6f 63 61 74 69 6f 6e 20 6f 6e 74 6f 20 74 68  location onto th
1436e 65 20 68 65 61 70 2e 0a 2a 2a 0a 2a 2a 20 4f 72  e heap..**.** Or
1436f 2c 20 74 68 65 20 68 65 61 70 20 69 73 20 75 73  , the heap is us
14370 65 64 20 66 6f 72 20 61 6c 6c 20 70 61 67 65 20  ed for all page 
14371 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 62 75 74  cache memory but
14372 20 74 68 65 20 68 65 61 70 20 69 73 0a 2a 2a 20   the heap is.** 
14373 75 6e 64 65 72 20 6d 65 6d 6f 72 79 20 70 72 65  under memory pre
14374 73 73 75 72 65 2c 20 74 68 65 6e 20 61 67 61 69  ssure, then agai
14375 6e 20 69 74 20 69 73 20 64 65 73 69 72 61 62 6c  n it is desirabl
14376 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a 20 61 6c  e to avoid.** al
14377 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 70  locating a new p
14378 61 67 65 20 63 61 63 68 65 20 65 6e 74 72 79 20  age cache entry 
14379 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69  in order to avoi
1437a 64 20 73 74 72 65 73 73 69 6e 67 0a 2a 2a 20 74  d stressing.** t
1437b 68 65 20 68 65 61 70 20 65 76 65 6e 20 66 75 72  he heap even fur
1437c 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ther..*/.static 
1437d 69 6e 74 20 70 63 61 63 68 65 31 55 6e 64 65 72  int pcache1Under
1437e 4d 65 6d 6f 72 79 50 72 65 73 73 75 72 65 28 50  MemoryPressure(P
1437f 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 29 7b  Cache1 *pCache){
14380 0a 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 6e  .  if( pcache1.n
14381 53 6c 6f 74 20 26 26 20 28 70 43 61 63 68 65 2d  Slot && (pCache-
14382 3e 73 7a 50 61 67 65 2b 70 43 61 63 68 65 2d 3e  >szPage+pCache->
14383 73 7a 45 78 74 72 61 29 3c 3d 70 63 61 63 68 65  szExtra)<=pcache
14384 31 2e 73 7a 53 6c 6f 74 20 29 7b 0a 20 20 20 20  1.szSlot ){.    
14385 72 65 74 75 72 6e 20 70 63 61 63 68 65 31 2e 62  return pcache1.b
14386 55 6e 64 65 72 50 72 65 73 73 75 72 65 3b 0a 20  UnderPressure;. 
14387 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
14388 72 6e 20 73 71 6c 69 74 65 33 48 65 61 70 4e 65  rn sqlite3HeapNe
14389 61 72 6c 79 46 75 6c 6c 28 29 3b 0a 20 20 7d 0a  arlyFull();.  }.
1438a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
1438b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1438c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1438d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1438e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1438f 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65  **/./******** Ge
14390 6e 65 72 61 6c 20 49 6d 70 6c 65 6d 65 6e 74 61  neral Implementa
14391 74 69 6f 6e 20 46 75 6e 63 74 69 6f 6e 73 20 2a  tion Functions *
14392 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14393 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14394 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ***/../*.** This
14395 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
14396 64 20 74 6f 20 72 65 73 69 7a 65 20 74 68 65 20  d to resize the 
14397 68 61 73 68 20 74 61 62 6c 65 20 75 73 65 64 20  hash table used 
14398 62 79 20 74 68 65 20 63 61 63 68 65 20 70 61 73  by the cache pas
14399 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69  sed.** as the fi
1439a 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  rst argument..**
1439b 0a 2a 2a 20 54 68 65 20 50 43 61 63 68 65 20 6d  .** The PCache m
1439c 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c  utex must be hel
1439d 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63  d when this func
1439e 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tion is called..
1439f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63  */.static int pc
143a0 61 63 68 65 31 52 65 73 69 7a 65 48 61 73 68 28  ache1ResizeHash(
143a1 50 43 61 63 68 65 31 20 2a 70 29 7b 0a 20 20 50  PCache1 *p){.  P
143a2 67 48 64 72 31 20 2a 2a 61 70 4e 65 77 3b 0a 20  gHdr1 **apNew;. 
143a3 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4e   unsigned int nN
143a4 65 77 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  ew;.  unsigned i
143a5 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28  nt i;..  assert(
143a6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
143a7 65 6c 64 28 70 2d 3e 70 47 72 6f 75 70 2d 3e 6d  eld(p->pGroup->m
143a8 75 74 65 78 29 20 29 3b 0a 0a 20 20 6e 4e 65 77  utex) );..  nNew
143a9 20 3d 20 70 2d 3e 6e 48 61 73 68 2a 32 3b 0a 20   = p->nHash*2;. 
143aa 20 69 66 28 20 6e 4e 65 77 3c 32 35 36 20 29 7b   if( nNew<256 ){
143ab 0a 20 20 20 20 6e 4e 65 77 20 3d 20 32 35 36 3b  .    nNew = 256;
143ac 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c  .  }..  pcache1L
143ad 65 61 76 65 4d 75 74 65 78 28 70 2d 3e 70 47 72  eaveMutex(p->pGr
143ae 6f 75 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  oup);.  if( p->n
143af 48 61 73 68 20 29 7b 20 73 71 6c 69 74 65 33 42  Hash ){ sqlite3B
143b0 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
143b1 28 29 3b 20 7d 0a 20 20 61 70 4e 65 77 20 3d 20  (); }.  apNew = 
143b2 28 50 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74  (PgHdr1 **)sqlit
143b3 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a  e3MallocZero(siz
143b4 65 6f 66 28 50 67 48 64 72 31 20 2a 29 2a 6e 4e  eof(PgHdr1 *)*nN
143b5 65 77 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48  ew);.  if( p->nH
143b6 61 73 68 20 29 7b 20 73 71 6c 69 74 65 33 45 6e  ash ){ sqlite3En
143b7 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
143b8 20 7d 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65   }.  pcache1Ente
143b9 72 4d 75 74 65 78 28 70 2d 3e 70 47 72 6f 75 70  rMutex(p->pGroup
143ba 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77 20 29  );.  if( apNew )
143bb 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
143bc 3c 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b  <p->nHash; i++){
143bd 0a 20 20 20 20 20 20 50 67 48 64 72 31 20 2a 70  .      PgHdr1 *p
143be 50 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48 64  Page;.      PgHd
143bf 72 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61  r1 *pNext = p->a
143c0 70 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20  pHash[i];.      
143c1 77 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20  while( (pPage = 
143c2 70 4e 65 78 74 29 21 3d 30 20 29 7b 0a 20 20 20  pNext)!=0 ){.   
143c3 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e       unsigned in
143c4 74 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65  t h = pPage->iKe
143c5 79 20 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20  y % nNew;.      
143c6 20 20 70 4e 65 78 74 20 3d 20 70 50 61 67 65 2d    pNext = pPage-
143c7 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20  >pNext;.        
143c8 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61  pPage->pNext = a
143c9 70 4e 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20  pNew[h];.       
143ca 20 61 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67   apNew[h] = pPag
143cb 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  e;.      }.    }
143cc 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
143cd 65 28 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20  e(p->apHash);.  
143ce 20 20 70 2d 3e 61 70 48 61 73 68 20 3d 20 61 70    p->apHash = ap
143cf 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73  New;.    p->nHas
143d0 68 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20  h = nNew;.  }.. 
143d1 20 72 65 74 75 72 6e 20 28 70 2d 3e 61 70 48 61   return (p->apHa
143d2 73 68 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a  sh ? SQLITE_OK :
143d3 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a   SQLITE_NOMEM);.
143d4 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
143d5 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69  nction is used i
143d6 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d  nternally to rem
143d7 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 50 61  ove the page pPa
143d8 67 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20  ge from the .** 
143d9 50 47 72 6f 75 70 20 4c 52 55 20 6c 69 73 74 2c  PGroup LRU list,
143da 20 69 66 20 69 73 20 70 61 72 74 20 6f 66 20 69   if is part of i
143db 74 2e 20 49 66 20 70 50 61 67 65 20 69 73 20 6e  t. If pPage is n
143dc 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 50  ot part of the P
143dd 47 72 6f 75 70 0a 2a 2a 20 4c 52 55 20 6c 69 73  Group.** LRU lis
143de 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e  t, then this fun
143df 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
143e0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 47 72 6f  ..**.** The PGro
143e1 75 70 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65  up mutex must be
143e2 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20   held when this 
143e3 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
143e4 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 50 61  ed..**.** If pPa
143e5 67 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20  ge is NULL then 
143e6 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
143e7 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74  a no-op..*/.stat
143e8 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 50  ic void pcache1P
143e9 69 6e 50 61 67 65 28 50 67 48 64 72 31 20 2a 70  inPage(PgHdr1 *p
143ea 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 31  Page){.  PCache1
143eb 20 2a 70 43 61 63 68 65 3b 0a 20 20 50 47 72 6f   *pCache;.  PGro
143ec 75 70 20 2a 70 47 72 6f 75 70 3b 0a 0a 20 20 69  up *pGroup;..  i
143ed 66 28 20 70 50 61 67 65 3d 3d 30 20 29 20 72 65  f( pPage==0 ) re
143ee 74 75 72 6e 3b 0a 20 20 70 43 61 63 68 65 20 3d  turn;.  pCache =
143ef 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a   pPage->pCache;.
143f0 20 20 70 47 72 6f 75 70 20 3d 20 70 43 61 63 68    pGroup = pCach
143f1 65 2d 3e 70 47 72 6f 75 70 3b 0a 20 20 61 73 73  e->pGroup;.  ass
143f2 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
143f3 65 78 5f 68 65 6c 64 28 70 47 72 6f 75 70 2d 3e  ex_held(pGroup->
143f4 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
143f5 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20  pPage->pLruNext 
143f6 7c 7c 20 70 50 61 67 65 3d 3d 70 47 72 6f 75 70  || pPage==pGroup
143f7 2d 3e 70 4c 72 75 54 61 69 6c 20 29 7b 0a 20 20  ->pLruTail ){.  
143f8 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72    if( pPage->pLr
143f9 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70  uPrev ){.      p
143fa 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d 3e  Page->pLruPrev->
143fb 70 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67 65  pLruNext = pPage
143fc 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20  ->pLruNext;.    
143fd 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d  }.    if( pPage-
143fe 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 20  >pLruNext ){.   
143ff 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65     pPage->pLruNe
14400 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70  xt->pLruPrev = p
14401 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b 0a  Page->pLruPrev;.
14402 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 47      }.    if( pG
14403 72 6f 75 70 2d 3e 70 4c 72 75 48 65 61 64 3d 3d  roup->pLruHead==
14404 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70  pPage ){.      p
14405 47 72 6f 75 70 2d 3e 70 4c 72 75 48 65 61 64 20  Group->pLruHead 
14406 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78  = pPage->pLruNex
14407 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
14408 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 54 61 69   pGroup->pLruTai
14409 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20  l==pPage ){.    
1440a 20 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 54 61    pGroup->pLruTa
1440b 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75  il = pPage->pLru
1440c 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Prev;.    }.    
1440d 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20  pPage->pLruNext 
1440e 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  = 0;.    pPage->
1440f 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20  pLruPrev = 0;.  
14410 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d    pPage->pCache-
14411 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b 0a  >nRecyclable--;.
14412 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65    }.}.../*.** Re
14413 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 73 75  move the page su
14414 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67  pplied as an arg
14415 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 68  ument from the h
14416 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28 50  ash table .** (P
14417 43 61 63 68 65 31 2e 61 70 48 61 73 68 20 73 74  Cache1.apHash st
14418 72 75 63 74 75 72 65 29 20 74 68 61 74 20 69 74  ructure) that it
14419 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74   is currently st
1441a 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54  ored in..**.** T
1441b 68 65 20 50 47 72 6f 75 70 20 6d 75 74 65 78 20  he PGroup mutex 
1441c 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65  must be held whe
1441d 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
1441e 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74  is called..*/.st
1441f 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
14420 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28  1RemoveFromHash(
14421 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a  PgHdr1 *pPage){.
14422 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
14423 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61  ;.  PCache1 *pCa
14424 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 61  che = pPage->pCa
14425 63 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a  che;.  PgHdr1 **
14426 70 70 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  pp;..  assert( s
14427 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14428 64 28 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70  d(pCache->pGroup
14429 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 68 20  ->mutex) );.  h 
1442a 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 25 20  = pPage->iKey % 
1442b 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20  pCache->nHash;. 
1442c 20 66 6f 72 28 70 70 3d 26 70 43 61 63 68 65 2d   for(pp=&pCache-
1442d 3e 61 70 48 61 73 68 5b 68 5d 3b 20 28 2a 70 70  >apHash[h]; (*pp
1442e 29 21 3d 70 50 61 67 65 3b 20 70 70 3d 26 28 2a  )!=pPage; pp=&(*
1442f 70 70 29 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 2a  pp)->pNext);.  *
14430 70 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e 65 78  pp = (*pp)->pNex
14431 74 3b 0a 0a 20 20 70 43 61 63 68 65 2d 3e 6e 50  t;..  pCache->nP
14432 61 67 65 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  age--;.}../*.** 
14433 49 66 20 74 68 65 72 65 20 61 72 65 20 63 75 72  If there are cur
14434 72 65 6e 74 6c 79 20 6d 6f 72 65 20 74 68 61 6e  rently more than
14435 20 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 20   nMaxPage pages 
14436 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a 2a  allocated, try.*
14437 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 67  * to recycle pag
14438 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 65  es to reduce the
14439 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 65   number allocate
1443a 64 20 74 6f 20 6e 4d 61 78 50 61 67 65 2e 0a 2a  d to nMaxPage..*
1443b 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
1443c 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50  ache1EnforceMaxP
1443d 61 67 65 28 50 47 72 6f 75 70 20 2a 70 47 72 6f  age(PGroup *pGro
1443e 75 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  up){.  assert( s
1443f 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14440 64 28 70 47 72 6f 75 70 2d 3e 6d 75 74 65 78 29  d(pGroup->mutex)
14441 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 47 72   );.  while( pGr
14442 6f 75 70 2d 3e 6e 43 75 72 72 65 6e 74 50 61 67  oup->nCurrentPag
14443 65 3e 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 61  e>pGroup->nMaxPa
14444 67 65 20 26 26 20 70 47 72 6f 75 70 2d 3e 70 4c  ge && pGroup->pL
14445 72 75 54 61 69 6c 20 29 7b 0a 20 20 20 20 50 67  ruTail ){.    Pg
14446 48 64 72 31 20 2a 70 20 3d 20 70 47 72 6f 75 70  Hdr1 *p = pGroup
14447 2d 3e 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20  ->pLruTail;.    
14448 61 73 73 65 72 74 28 20 70 2d 3e 70 43 61 63 68  assert( p->pCach
14449 65 2d 3e 70 47 72 6f 75 70 3d 3d 70 47 72 6f 75  e->pGroup==pGrou
1444a 70 20 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31  p );.    pcache1
1444b 50 69 6e 50 61 67 65 28 70 29 3b 0a 20 20 20 20  PinPage(p);.    
1444c 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f  pcache1RemoveFro
1444d 6d 48 61 73 68 28 70 29 3b 0a 20 20 20 20 70 63  mHash(p);.    pc
1444e 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 29  ache1FreePage(p)
1444f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
14450 69 73 63 61 72 64 20 61 6c 6c 20 70 61 67 65 73  iscard all pages
14451 20 66 72 6f 6d 20 63 61 63 68 65 20 70 43 61 63   from cache pCac
14452 68 65 20 77 69 74 68 20 61 20 70 61 67 65 20 6e  he with a page n
14453 75 6d 62 65 72 20 28 6b 65 79 20 76 61 6c 75 65  umber (key value
14454 29 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68  ) .** greater th
14455 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 69  an or equal to i
14456 4c 69 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e 65  Limit. Any pinne
14457 64 20 70 61 67 65 73 20 74 68 61 74 20 6d 65 65  d pages that mee
14458 74 20 74 68 69 73 20 0a 2a 2a 20 63 72 69 74 65  t this .** crite
14459 72 69 61 20 61 72 65 20 75 6e 70 69 6e 6e 65 64  ria are unpinned
1445a 20 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65   before they are
1445b 20 64 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a   discarded..**.*
1445c 2a 20 54 68 65 20 50 43 61 63 68 65 20 6d 75 74  * The PCache mut
1445d 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20  ex must be held 
1445e 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
1445f 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f  on is called..*/
14460 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
14461 63 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61  che1TruncateUnsa
14462 66 65 28 0a 20 20 50 43 61 63 68 65 31 20 2a 70  fe(.  PCache1 *p
14463 43 61 63 68 65 2c 20 20 20 20 20 20 20 20 20 20  Cache,          
14464 20 20 20 2f 2a 20 54 68 65 20 63 61 63 68 65 20     /* The cache 
14465 74 6f 20 74 72 75 6e 63 61 74 65 20 2a 2f 0a 20  to truncate */. 
14466 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c   unsigned int iL
14467 69 6d 69 74 20 20 20 20 20 20 20 20 20 20 2f 2a  imit          /*
14468 20 44 72 6f 70 20 70 61 67 65 73 20 77 69 74 68   Drop pages with
14469 20 74 68 69 73 20 70 67 6e 6f 20 6f 72 20 6c 61   this pgno or la
1446a 72 67 65 72 20 2a 2f 0a 29 7b 0a 20 20 54 45 53  rger */.){.  TES
1446b 54 4f 4e 4c 59 28 20 75 6e 73 69 67 6e 65 64 20  TONLY( unsigned 
1446c 69 6e 74 20 6e 50 61 67 65 20 3d 20 30 3b 20 29  int nPage = 0; )
1446d 20 20 2f 2a 20 54 6f 20 61 73 73 65 72 74 20 70    /* To assert p
1446e 43 61 63 68 65 2d 3e 6e 50 61 67 65 20 69 73 20  Cache->nPage is 
1446f 63 6f 72 72 65 63 74 20 2a 2f 0a 20 20 75 6e 73  correct */.  uns
14470 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a 20 20 61  igned int h;.  a
14471 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
14472 75 74 65 78 5f 68 65 6c 64 28 70 43 61 63 68 65  utex_held(pCache
14473 2d 3e 70 47 72 6f 75 70 2d 3e 6d 75 74 65 78 29  ->pGroup->mutex)
14474 20 29 3b 0a 20 20 66 6f 72 28 68 3d 30 3b 20 68   );.  for(h=0; h
14475 3c 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 20  <pCache->nHash; 
14476 68 2b 2b 29 7b 0a 20 20 20 20 50 67 48 64 72 31  h++){.    PgHdr1
14477 20 2a 2a 70 70 20 3d 20 26 70 43 61 63 68 65 2d   **pp = &pCache-
14478 3e 61 70 48 61 73 68 5b 68 5d 3b 20 0a 20 20 20  >apHash[h]; .   
14479 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 3b 0a   PgHdr1 *pPage;.
1447a 20 20 20 20 77 68 69 6c 65 28 20 28 70 50 61 67      while( (pPag
1447b 65 20 3d 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20  e = *pp)!=0 ){. 
1447c 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e       if( pPage->
1447d 69 4b 65 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a  iKey>=iLimit ){.
1447e 20 20 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e          pCache->
1447f 6e 50 61 67 65 2d 2d 3b 0a 20 20 20 20 20 20 20  nPage--;.       
14480 20 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e   *pp = pPage->pN
14481 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 63 61  ext;.        pca
14482 63 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67  che1PinPage(pPag
14483 65 29 3b 0a 20 20 20 20 20 20 20 20 70 63 61 63  e);.        pcac
14484 68 65 31 46 72 65 65 50 61 67 65 28 70 50 61 67  he1FreePage(pPag
14485 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  e);.      }else{
14486 0a 20 20 20 20 20 20 20 20 70 70 20 3d 20 26 70  .        pp = &p
14487 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Page->pNext;.   
14488 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 6e       TESTONLY( n
14489 50 61 67 65 2b 2b 3b 20 29 0a 20 20 20 20 20 20  Page++; ).      
1448a 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73  }.    }.  }.  as
1448b 73 65 72 74 28 20 70 43 61 63 68 65 2d 3e 6e 50  sert( pCache->nP
1448c 61 67 65 3d 3d 6e 50 61 67 65 20 29 3b 0a 7d 0a  age==nPage );.}.
1448d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1448e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1448f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14491 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14492 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69  /./******** sqli
14493 74 65 33 5f 70 63 61 63 68 65 20 4d 65 74 68 6f  te3_pcache Metho
14494 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ds *************
14495 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14496 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14497 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  */../*.** Implem
14498 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
14499 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78  sqlite3_pcache.x
1449a 49 6e 69 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  Init method..*/.
1449b 73 74 61 74 69 63 20 69 6e 74 20 70 63 61 63 68  static int pcach
1449c 65 31 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74  e1Init(void *Not
1449d 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Used){.  UNUSED_
1449e 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
1449f 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63  d);.  assert( pc
144a0 61 63 68 65 31 2e 69 73 49 6e 69 74 3d 3d 30 20  ache1.isInit==0 
144a1 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 63 61  );.  memset(&pca
144a2 63 68 65 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28  che1, 0, sizeof(
144a3 70 63 61 63 68 65 31 29 29 3b 0a 20 20 69 66 28  pcache1));.  if(
144a4 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
144a5 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20  nfig.bCoreMutex 
144a6 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 67  ){.    pcache1.g
144a7 72 70 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  rp.mutex = sqlit
144a8 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 53  e3_mutex_alloc(S
144a9 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
144aa 49 43 5f 4c 52 55 29 3b 0a 20 20 20 20 70 63 61  IC_LRU);.    pca
144ab 63 68 65 31 2e 6d 75 74 65 78 20 3d 20 73 71 6c  che1.mutex = sql
144ac 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
144ad 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
144ae 41 54 49 43 5f 50 4d 45 4d 29 3b 0a 20 20 7d 0a  ATIC_PMEM);.  }.
144af 20 20 70 63 61 63 68 65 31 2e 67 72 70 2e 6d 78    pcache1.grp.mx
144b0 50 69 6e 6e 65 64 20 3d 20 31 30 3b 0a 20 20 70  Pinned = 10;.  p
144b1 63 61 63 68 65 31 2e 69 73 49 6e 69 74 20 3d 20  cache1.isInit = 
144b2 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  1;.  return SQLI
144b3 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
144b4 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
144b5 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63  f the sqlite3_pc
144b6 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e 20 6d  ache.xShutdown m
144b7 65 74 68 6f 64 2e 0a 2a 2a 20 4e 6f 74 65 20 74  ethod..** Note t
144b8 68 61 74 20 74 68 65 20 73 74 61 74 69 63 20 6d  hat the static m
144b9 75 74 65 78 20 61 6c 6c 6f 63 61 74 65 64 20 69  utex allocated i
144ba 6e 20 78 49 6e 69 74 20 64 6f 65 73 20 0a 2a 2a  n xInit does .**
144bb 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
144bc 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  freed..*/.static
144bd 20 76 6f 69 64 20 70 63 61 63 68 65 31 53 68 75   void pcache1Shu
144be 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55  tdown(void *NotU
144bf 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  sed){.  UNUSED_P
144c0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
144c1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 61  );.  assert( pca
144c2 63 68 65 31 2e 69 73 49 6e 69 74 21 3d 30 20 29  che1.isInit!=0 )
144c3 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 63 61 63  ;.  memset(&pcac
144c4 68 65 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70  he1, 0, sizeof(p
144c5 63 61 63 68 65 31 29 29 3b 0a 7d 0a 0a 2f 2a 0a  cache1));.}../*.
144c6 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
144c7 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
144c8 5f 70 63 61 63 68 65 2e 78 43 72 65 61 74 65 20  _pcache.xCreate 
144c9 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c  method..**.** Al
144ca 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 63 61 63  locate a new cac
144cb 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71  he..*/.static sq
144cc 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 63  lite3_pcache *pc
144cd 61 63 68 65 31 43 72 65 61 74 65 28 69 6e 74 20  ache1Create(int 
144ce 73 7a 50 61 67 65 2c 20 69 6e 74 20 73 7a 45 78  szPage, int szEx
144cf 74 72 61 2c 20 69 6e 74 20 62 50 75 72 67 65 61  tra, int bPurgea
144d0 62 6c 65 29 7b 0a 20 20 50 43 61 63 68 65 31 20  ble){.  PCache1 
144d1 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 2f 2a  *pCache;      /*
144d2 20 54 68 65 20 6e 65 77 6c 79 20 63 72 65 61 74   The newly creat
144d3 65 64 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f  ed page cache */
144d4 0a 20 20 50 47 72 6f 75 70 20 2a 70 47 72 6f 75  .  PGroup *pGrou
144d5 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  p;       /* The 
144d6 67 72 6f 75 70 20 74 68 65 20 6e 65 77 20 70 61  group the new pa
144d7 67 65 20 63 61 63 68 65 20 77 69 6c 6c 20 62 65  ge cache will be
144d8 6c 6f 6e 67 20 74 6f 20 2a 2f 0a 20 20 69 6e 74  long to */.  int
144d9 20 73 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20   sz;            
144da 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 6d     /* Bytes of m
144db 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 20 74  emory required t
144dc 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 6e  o allocate the n
144dd 65 77 20 63 61 63 68 65 20 2a 2f 0a 0a 20 20 2f  ew cache */..  /
144de 2a 0a 20 20 2a 2a 20 54 68 65 20 73 65 70 65 72  *.  ** The seper
144df 61 74 65 43 61 63 68 65 20 76 61 72 69 61 62 6c  ateCache variabl
144e0 65 20 69 73 20 74 72 75 65 20 69 66 20 65 61 63  e is true if eac
144e1 68 20 50 43 61 63 68 65 20 68 61 73 20 69 74 73  h PCache has its
144e2 20 6f 77 6e 20 70 72 69 76 61 74 65 0a 20 20 2a   own private.  *
144e3 2a 20 50 47 72 6f 75 70 2e 20 20 49 6e 20 6f 74  * PGroup.  In ot
144e4 68 65 72 20 77 6f 72 64 73 2c 20 73 65 70 61 72  her words, separ
144e5 61 74 65 43 61 63 68 65 20 69 73 20 74 72 75 65  ateCache is true
144e6 20 66 6f 72 20 6d 6f 64 65 20 28 31 29 20 77 68   for mode (1) wh
144e7 65 72 65 20 6e 6f 0a 20 20 2a 2a 20 6d 75 74 65  ere no.  ** mute
144e8 78 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64  xing is required
144e9 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20  ..  **.  **   * 
144ea 20 41 6c 77 61 79 73 20 75 73 65 20 61 20 75 6e   Always use a un
144eb 69 66 69 65 64 20 63 61 63 68 65 20 28 6d 6f 64  ified cache (mod
144ec 65 2d 32 29 20 69 66 20 45 4e 41 42 4c 45 5f 4d  e-2) if ENABLE_M
144ed 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
144ee 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 20  .  **.  **   *  
144ef 41 6c 77 61 79 73 20 75 73 65 20 61 20 75 6e 69  Always use a uni
144f0 66 69 65 64 20 63 61 63 68 65 20 69 6e 20 73 69  fied cache in si
144f1 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70  ngle-threaded ap
144f2 70 6c 69 63 61 74 69 6f 6e 73 0a 20 20 2a 2a 0a  plications.  **.
144f3 20 20 2a 2a 20 20 20 2a 20 20 4f 74 68 65 72 77    **   *  Otherw
144f4 69 73 65 20 28 69 66 20 6d 75 6c 74 69 2d 74 68  ise (if multi-th
144f5 72 65 61 64 65 64 20 61 6e 64 20 45 4e 41 42 4c  readed and ENABL
144f6 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d  E_MEMORY_MANAGEM
144f7 45 4e 54 20 69 73 20 6f 66 66 29 0a 20 20 2a 2a  ENT is off).  **
144f8 20 20 20 20 20 20 75 73 65 20 73 65 70 61 72 61        use separa
144f9 74 65 20 63 61 63 68 65 73 20 28 6d 6f 64 65 2d  te caches (mode-
144fa 31 29 0a 20 20 2a 2f 0a 23 69 66 20 64 65 66 69  1).  */.#if defi
144fb 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
144fc 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d  E_MEMORY_MANAGEM
144fd 45 4e 54 29 20 7c 7c 20 53 51 4c 49 54 45 5f 54  ENT) || SQLITE_T
144fe 48 52 45 41 44 53 41 46 45 3d 3d 30 0a 20 20 63  HREADSAFE==0.  c
144ff 6f 6e 73 74 20 69 6e 74 20 73 65 70 61 72 61 74  onst int separat
14500 65 43 61 63 68 65 20 3d 20 30 3b 0a 23 65 6c 73  eCache = 0;.#els
14501 65 0a 20 20 69 6e 74 20 73 65 70 61 72 61 74 65  e.  int separate
14502 43 61 63 68 65 20 3d 20 73 71 6c 69 74 65 33 47  Cache = sqlite3G
14503 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72  lobalConfig.bCor
14504 65 4d 75 74 65 78 3e 30 3b 0a 23 65 6e 64 69 66  eMutex>0;.#endif
14505 0a 0a 20 20 61 73 73 65 72 74 28 20 28 73 7a 50  ..  assert( (szP
14506 61 67 65 20 26 20 28 73 7a 50 61 67 65 2d 31 29  age & (szPage-1)
14507 29 3d 3d 30 20 26 26 20 73 7a 50 61 67 65 3e 3d  )==0 && szPage>=
14508 35 31 32 20 26 26 20 73 7a 50 61 67 65 3c 3d 36  512 && szPage<=6
14509 35 35 33 36 20 29 3b 0a 20 20 61 73 73 65 72 74  5536 );.  assert
1450a 28 20 73 7a 45 78 74 72 61 20 3c 20 33 30 30 20  ( szExtra < 300 
1450b 29 3b 0a 0a 20 20 73 7a 20 3d 20 73 69 7a 65 6f  );..  sz = sizeo
1450c 66 28 50 43 61 63 68 65 31 29 20 2b 20 73 69 7a  f(PCache1) + siz
1450d 65 6f 66 28 50 47 72 6f 75 70 29 2a 73 65 70 61  eof(PGroup)*sepa
1450e 72 61 74 65 43 61 63 68 65 3b 0a 20 20 70 43 61  rateCache;.  pCa
1450f 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a  che = (PCache1 *
14510 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65  )sqlite3MallocZe
14511 72 6f 28 73 7a 29 3b 0a 20 20 69 66 28 20 70 43  ro(sz);.  if( pC
14512 61 63 68 65 20 29 7b 0a 20 20 20 20 69 66 28 20  ache ){.    if( 
14513 73 65 70 61 72 61 74 65 43 61 63 68 65 20 29 7b  separateCache ){
14514 0a 20 20 20 20 20 20 70 47 72 6f 75 70 20 3d 20  .      pGroup = 
14515 28 50 47 72 6f 75 70 2a 29 26 70 43 61 63 68 65  (PGroup*)&pCache
14516 5b 31 5d 3b 0a 20 20 20 20 20 20 70 47 72 6f 75  [1];.      pGrou
14517 70 2d 3e 6d 78 50 69 6e 6e 65 64 20 3d 20 31 30  p->mxPinned = 10
14518 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
14519 20 20 20 70 47 72 6f 75 70 20 3d 20 26 70 63 61     pGroup = &pca
1451a 63 68 65 31 2e 67 72 70 3b 0a 20 20 20 20 7d 0a  che1.grp;.    }.
1451b 20 20 20 20 70 43 61 63 68 65 2d 3e 70 47 72 6f      pCache->pGro
1451c 75 70 20 3d 20 70 47 72 6f 75 70 3b 0a 20 20 20  up = pGroup;.   
1451d 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20   pCache->szPage 
1451e 3d 20 73 7a 50 61 67 65 3b 0a 20 20 20 20 70 43  = szPage;.    pC
1451f 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 3d 20  ache->szExtra = 
14520 73 7a 45 78 74 72 61 3b 0a 20 20 20 20 70 43 61  szExtra;.    pCa
14521 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20  che->bPurgeable 
14522 3d 20 28 62 50 75 72 67 65 61 62 6c 65 20 3f 20  = (bPurgeable ? 
14523 31 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 20  1 : 0);.    if( 
14524 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20  bPurgeable ){.  
14525 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e      pCache->nMin
14526 20 3d 20 31 30 3b 0a 20 20 20 20 20 20 70 63 61   = 10;.      pca
14527 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 70  che1EnterMutex(p
14528 47 72 6f 75 70 29 3b 0a 20 20 20 20 20 20 70 47  Group);.      pG
14529 72 6f 75 70 2d 3e 6e 4d 69 6e 50 61 67 65 20 2b  roup->nMinPage +
1452a 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a  = pCache->nMin;.
1452b 20 20 20 20 20 20 70 47 72 6f 75 70 2d 3e 6d 78        pGroup->mx
1452c 50 69 6e 6e 65 64 20 3d 20 70 47 72 6f 75 70 2d  Pinned = pGroup-
1452d 3e 6e 4d 61 78 50 61 67 65 20 2b 20 31 30 20 2d  >nMaxPage + 10 -
1452e 20 70 47 72 6f 75 70 2d 3e 6e 4d 69 6e 50 61 67   pGroup->nMinPag
1452f 65 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  e;.      pcache1
14530 4c 65 61 76 65 4d 75 74 65 78 28 70 47 72 6f 75  LeaveMutex(pGrou
14531 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  p);.    }.  }.  
14532 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f  return (sqlite3_
14533 70 63 61 63 68 65 20 2a 29 70 43 61 63 68 65 3b  pcache *)pCache;
14534 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
14535 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
14536 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78  sqlite3_pcache.x
14537 43 61 63 68 65 73 69 7a 65 20 6d 65 74 68 6f 64  Cachesize method
14538 2e 20 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75  . .**.** Configu
14539 72 65 20 74 68 65 20 63 61 63 68 65 5f 73 69 7a  re the cache_siz
1453a 65 20 6c 69 6d 69 74 20 66 6f 72 20 61 20 63 61  e limit for a ca
1453b 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  che..*/.static v
1453c 6f 69 64 20 70 63 61 63 68 65 31 43 61 63 68 65  oid pcache1Cache
1453d 73 69 7a 65 28 73 71 6c 69 74 65 33 5f 70 63 61  size(sqlite3_pca
1453e 63 68 65 20 2a 70 2c 20 69 6e 74 20 6e 4d 61 78  che *p, int nMax
1453f 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43  ){.  PCache1 *pC
14540 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20  ache = (PCache1 
14541 2a 29 70 3b 0a 20 20 69 66 28 20 70 43 61 63 68  *)p;.  if( pCach
14542 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b  e->bPurgeable ){
14543 0a 20 20 20 20 50 47 72 6f 75 70 20 2a 70 47 72  .    PGroup *pGr
14544 6f 75 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 47  oup = pCache->pG
14545 72 6f 75 70 3b 0a 20 20 20 20 70 63 61 63 68 65  roup;.    pcache
14546 31 45 6e 74 65 72 4d 75 74 65 78 28 70 47 72 6f  1EnterMutex(pGro
14547 75 70 29 3b 0a 20 20 20 20 70 47 72 6f 75 70 2d  up);.    pGroup-
14548 3e 6e 4d 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d  >nMaxPage += (nM
14549 61 78 20 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61  ax - pCache->nMa
1454a 78 29 3b 0a 20 20 20 20 70 47 72 6f 75 70 2d 3e  x);.    pGroup->
1454b 6d 78 50 69 6e 6e 65 64 20 3d 20 70 47 72 6f 75  mxPinned = pGrou
1454c 70 2d 3e 6e 4d 61 78 50 61 67 65 20 2b 20 31 30  p->nMaxPage + 10
1454d 20 2d 20 70 47 72 6f 75 70 2d 3e 6e 4d 69 6e 50   - pGroup->nMinP
1454e 61 67 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d  age;.    pCache-
1454f 3e 6e 4d 61 78 20 3d 20 6e 4d 61 78 3b 0a 20 20  >nMax = nMax;.  
14550 20 20 70 43 61 63 68 65 2d 3e 6e 39 30 70 63 74    pCache->n90pct
14551 20 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 2a   = pCache->nMax*
14552 39 2f 31 30 3b 0a 20 20 20 20 70 63 61 63 68 65  9/10;.    pcache
14553 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28  1EnforceMaxPage(
14554 70 47 72 6f 75 70 29 3b 0a 20 20 20 20 70 63 61  pGroup);.    pca
14555 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 70  che1LeaveMutex(p
14556 47 72 6f 75 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Group);.  }.}../
14557 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
14558 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
14559 65 33 5f 70 63 61 63 68 65 2e 78 53 68 72 69 6e  e3_pcache.xShrin
1455a 6b 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a  k method. .**.**
1455b 20 46 72 65 65 20 75 70 20 61 73 20 6d 75 63 68   Free up as much
1455c 20 6d 65 6d 6f 72 79 20 61 73 20 70 6f 73 73 69   memory as possi
1455d 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
1455e 6f 69 64 20 70 63 61 63 68 65 31 53 68 72 69 6e  oid pcache1Shrin
1455f 6b 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  k(sqlite3_pcache
14560 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 31 20   *p){.  PCache1 
14561 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68  *pCache = (PCach
14562 65 31 2a 29 70 3b 0a 20 20 69 66 28 20 70 43 61  e1*)p;.  if( pCa
14563 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20  che->bPurgeable 
14564 29 7b 0a 20 20 20 20 50 47 72 6f 75 70 20 2a 70  ){.    PGroup *p
14565 47 72 6f 75 70 20 3d 20 70 43 61 63 68 65 2d 3e  Group = pCache->
14566 70 47 72 6f 75 70 3b 0a 20 20 20 20 69 6e 74 20  pGroup;.    int 
14567 73 61 76 65 64 4d 61 78 50 61 67 65 3b 0a 20 20  savedMaxPage;.  
14568 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75    pcache1EnterMu
14569 74 65 78 28 70 47 72 6f 75 70 29 3b 0a 20 20 20  tex(pGroup);.   
1456a 20 73 61 76 65 64 4d 61 78 50 61 67 65 20 3d 20   savedMaxPage = 
1456b 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 61 67 65  pGroup->nMaxPage
1456c 3b 0a 20 20 20 20 70 47 72 6f 75 70 2d 3e 6e 4d  ;.    pGroup->nM
1456d 61 78 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20  axPage = 0;.    
1456e 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61  pcache1EnforceMa
1456f 78 50 61 67 65 28 70 47 72 6f 75 70 29 3b 0a 20  xPage(pGroup);. 
14570 20 20 20 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50     pGroup->nMaxP
14571 61 67 65 20 3d 20 73 61 76 65 64 4d 61 78 50 61  age = savedMaxPa
14572 67 65 3b 0a 20 20 20 20 70 63 61 63 68 65 31 4c  ge;.    pcache1L
14573 65 61 76 65 4d 75 74 65 78 28 70 47 72 6f 75 70  eaveMutex(pGroup
14574 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
14575 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
14576 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63  f the sqlite3_pc
14577 61 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 20  ache.xPagecount 
14578 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74  method. .*/.stat
14579 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 50 61  ic int pcache1Pa
1457a 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f  gecount(sqlite3_
1457b 70 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e  pcache *p){.  in
1457c 74 20 6e 3b 0a 20 20 50 43 61 63 68 65 31 20 2a  t n;.  PCache1 *
1457d 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65  pCache = (PCache
1457e 31 2a 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45  1*)p;.  pcache1E
1457f 6e 74 65 72 4d 75 74 65 78 28 70 43 61 63 68 65  nterMutex(pCache
14580 2d 3e 70 47 72 6f 75 70 29 3b 0a 20 20 6e 20 3d  ->pGroup);.  n =
14581 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 3b 0a   pCache->nPage;.
14582 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
14583 74 65 78 28 70 43 61 63 68 65 2d 3e 70 47 72 6f  tex(pCache->pGro
14584 75 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b  up);.  return n;
14585 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
14586 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
14587 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78  sqlite3_pcache.x
14588 46 65 74 63 68 20 6d 65 74 68 6f 64 2e 20 0a 2a  Fetch method. .*
14589 2a 0a 2a 2a 20 46 65 74 63 68 20 61 20 70 61 67  *.** Fetch a pag
1458a 65 20 62 79 20 6b 65 79 20 76 61 6c 75 65 2e 0a  e by key value..
1458b 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f 72  **.** Whether or
1458c 20 6e 6f 74 20 61 20 6e 65 77 20 70 61 67 65 20   not a new page 
1458d 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61 74 65 64  may be allocated
1458e 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
1458f 6e 20 64 65 70 65 6e 64 73 20 6f 6e 0a 2a 2a 20  n depends on.** 
14590 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
14591 20 63 72 65 61 74 65 46 6c 61 67 20 61 72 67 75   createFlag argu
14592 6d 65 6e 74 2e 20 20 30 20 6d 65 61 6e 73 20 64  ment.  0 means d
14593 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 20 61  o not allocate a
14594 20 6e 65 77 0a 2a 2a 20 70 61 67 65 2e 20 20 31   new.** page.  1
14595 20 6d 65 61 6e 73 20 61 6c 6c 6f 63 61 74 65 20   means allocate 
14596 61 20 6e 65 77 20 70 61 67 65 20 69 66 20 73 70  a new page if sp
14597 61 63 65 20 69 73 20 65 61 73 69 6c 79 20 61 76  ace is easily av
14598 61 69 6c 61 62 6c 65 2e 20 20 32 20 0a 2a 2a 20  ailable.  2 .** 
14599 6d 65 61 6e 73 20 74 6f 20 74 72 79 20 72 65 61  means to try rea
1459a 6c 6c 79 20 68 61 72 64 20 74 6f 20 61 6c 6c 6f  lly hard to allo
1459b 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e  cate a new page.
1459c 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 6e 6f 6e  .**.** For a non
1459d 2d 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65  -purgeable cache
1459e 20 28 61 20 63 61 63 68 65 20 75 73 65 64 20 61   (a cache used a
1459f 73 20 74 68 65 20 73 74 6f 72 61 67 65 20 66 6f  s the storage fo
145a0 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a  r an in-memory.*
145a1 2a 20 64 61 74 61 62 61 73 65 29 20 74 68 65 72  * database) ther
145a2 65 20 69 73 20 72 65 61 6c 6c 79 20 6e 6f 20 64  e is really no d
145a3 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
145a4 6e 20 63 72 65 61 74 65 46 6c 61 67 20 31 20 61  n createFlag 1 a
145a5 6e 64 20 32 2e 20 20 53 6f 0a 2a 2a 20 74 68 65  nd 2.  So.** the
145a6 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
145a7 6e 20 28 70 63 61 63 68 65 2e 63 29 20 77 69 6c  n (pcache.c) wil
145a8 6c 20 6e 65 76 65 72 20 68 61 76 65 20 61 20 63  l never have a c
145a9 72 65 61 74 65 46 6c 61 67 20 6f 66 20 31 20 6f  reateFlag of 1 o
145aa 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 70 75 72 67 65  n.** a non-purge
145ab 61 62 6c 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a  able cache..**.*
145ac 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65  * There are thre
145ad 65 20 64 69 66 66 65 72 65 6e 74 20 61 70 70 72  e different appr
145ae 6f 61 63 68 65 73 20 74 6f 20 6f 62 74 61 69 6e  oaches to obtain
145af 69 6e 67 20 73 70 61 63 65 20 66 6f 72 20 61 20  ing space for a 
145b0 70 61 67 65 2c 0a 2a 2a 20 64 65 70 65 6e 64 69  page,.** dependi
145b1 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20  ng on the value 
145b2 6f 66 20 70 61 72 61 6d 65 74 65 72 20 63 72 65  of parameter cre
145b3 61 74 65 46 6c 61 67 20 28 77 68 69 63 68 20 6d  ateFlag (which m
145b4 61 79 20 62 65 20 30 2c 20 31 20 6f 72 20 32 29  ay be 0, 1 or 2)
145b5 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 52 65 67  ..**.**   1. Reg
145b6 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 76  ardless of the v
145b7 61 6c 75 65 20 6f 66 20 63 72 65 61 74 65 46 6c  alue of createFl
145b8 61 67 2c 20 74 68 65 20 63 61 63 68 65 20 69 73  ag, the cache is
145b9 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 61 20   searched for a 
145ba 0a 2a 2a 20 20 20 20 20 20 63 6f 70 79 20 6f 66  .**      copy of
145bb 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70   the requested p
145bc 61 67 65 2e 20 49 66 20 6f 6e 65 20 69 73 20 66  age. If one is f
145bd 6f 75 6e 64 2c 20 69 74 20 69 73 20 72 65 74 75  ound, it is retu
145be 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e  rned..**.**   2.
145bf 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 3d 3d   If createFlag==
145c0 30 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 69  0 and the page i
145c1 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e  s not already in
145c2 20 74 68 65 20 63 61 63 68 65 2c 20 4e 55 4c 4c   the cache, NULL
145c3 20 69 73 0a 2a 2a 20 20 20 20 20 20 72 65 74 75   is.**      retu
145c4 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e  rned..**.**   3.
145c5 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 69   If createFlag i
145c6 73 20 31 2c 20 61 6e 64 20 74 68 65 20 70 61 67  s 1, and the pag
145c7 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  e is not already
145c8 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74   in the cache, t
145c9 68 65 6e 0a 2a 2a 20 20 20 20 20 20 72 65 74 75  hen.**      retu
145ca 72 6e 20 4e 55 4c 4c 20 28 64 6f 20 6e 6f 74 20  rn NULL (do not 
145cb 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  allocate a new p
145cc 61 67 65 29 20 69 66 20 61 6e 79 20 6f 66 20 74  age) if any of t
145cd 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
145ce 20 20 20 20 20 63 6f 6e 64 69 74 69 6f 6e 73 20       conditions 
145cf 61 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20  are true:.**.** 
145d0 20 20 20 20 20 20 28 61 29 20 74 68 65 20 6e 75        (a) the nu
145d1 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69  mber of pages pi
145d2 6e 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68  nned by the cach
145d3 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  e is greater tha
145d4 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50  n.**           P
145d5 43 61 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a  Cache1.nMax, or.
145d6 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28 62 29 20  **.**       (b) 
145d7 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
145d8 67 65 73 20 70 69 6e 6e 65 64 20 62 79 20 74 68  ges pinned by th
145d9 65 20 63 61 63 68 65 20 69 73 20 67 72 65 61 74  e cache is great
145da 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20  er than.**      
145db 20 20 20 20 20 74 68 65 20 73 75 6d 20 6f 66 20       the sum of 
145dc 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 20 70 75 72  nMax for all pur
145dd 67 65 61 62 6c 65 20 63 61 63 68 65 73 2c 20 6c  geable caches, l
145de 65 73 73 20 74 68 65 20 73 75 6d 20 6f 66 20 0a  ess the sum of .
145df 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6e 4d 69  **           nMi
145e0 6e 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20  n for all other 
145e1 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73  purgeable caches
145e2 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 34 2e 20  , or.**.**   4. 
145e3 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66  If none of the f
145e4 69 72 73 74 20 74 68 72 65 65 20 63 6f 6e 64 69  irst three condi
145e5 74 69 6f 6e 73 20 61 70 70 6c 79 20 61 6e 64 20  tions apply and 
145e6 74 68 65 20 63 61 63 68 65 20 69 73 20 6d 61 72  the cache is mar
145e7 6b 65 64 0a 2a 2a 20 20 20 20 20 20 61 73 20 70  ked.**      as p
145e8 75 72 67 65 61 62 6c 65 2c 20 61 6e 64 20 69 66  urgeable, and if
145e9 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
145ea 6f 77 69 6e 67 20 69 73 20 74 72 75 65 3a 0a 2a  owing is true:.*
145eb 2a 0a 2a 2a 20 20 20 20 20 20 20 28 61 29 20 54  *.**       (a) T
145ec 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
145ed 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  es allocated for
145ee 20 74 68 65 20 63 61 63 68 65 20 69 73 20 61 6c   the cache is al
145ef 72 65 61 64 79 20 0a 2a 2a 20 20 20 20 20 20 20  ready .**       
145f0 20 20 20 20 50 43 61 63 68 65 31 2e 6e 4d 61 78      PCache1.nMax
145f1 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  , or.**.**      
145f2 20 28 62 29 20 54 68 65 20 6e 75 6d 62 65 72 20   (b) The number 
145f3 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74  of pages allocat
145f4 65 64 20 66 6f 72 20 61 6c 6c 20 70 75 72 67 65  ed for all purge
145f5 61 62 6c 65 20 63 61 63 68 65 73 20 69 73 0a 2a  able caches is.*
145f6 2a 20 20 20 20 20 20 20 20 20 20 20 61 6c 72 65  *           alre
145f7 61 64 79 20 65 71 75 61 6c 20 74 6f 20 6f 72 20  ady equal to or 
145f8 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65  greater than the
145f9 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f 72   sum of nMax for
145fa 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20   all.**         
145fb 20 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68    purgeable cach
145fc 65 73 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  es,.**.**       
145fd 28 63 29 20 54 68 65 20 73 79 73 74 65 6d 20 69  (c) The system i
145fe 73 20 75 6e 64 65 72 20 6d 65 6d 6f 72 79 20 70  s under memory p
145ff 72 65 73 73 75 72 65 20 61 6e 64 20 77 61 6e 74  ressure and want
14600 73 20 74 6f 20 61 76 6f 69 64 0a 2a 2a 20 20 20  s to avoid.**   
14601 20 20 20 20 20 20 20 20 75 6e 6e 65 63 65 73 73          unnecess
14602 61 72 79 20 70 61 67 65 73 20 63 61 63 68 65 20  ary pages cache 
14603 65 6e 74 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  entry allocation
14604 73 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 74 68 65  s.**.**      the
14605 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 63  n attempt to rec
14606 79 63 6c 65 20 61 20 70 61 67 65 20 66 72 6f 6d  ycle a page from
14607 20 74 68 65 20 4c 52 55 20 6c 69 73 74 2e 20 49   the LRU list. I
14608 66 20 69 74 20 69 73 20 74 68 65 20 72 69 67 68  f it is the righ
14609 74 0a 2a 2a 20 20 20 20 20 20 73 69 7a 65 2c 20  t.**      size, 
1460a 72 65 74 75 72 6e 20 74 68 65 20 72 65 63 79 63  return the recyc
1460b 6c 65 64 20 62 75 66 66 65 72 2e 20 4f 74 68 65  led buffer. Othe
1460c 72 77 69 73 65 2c 20 66 72 65 65 20 74 68 65 20  rwise, free the 
1460d 62 75 66 66 65 72 20 61 6e 64 0a 2a 2a 20 20 20  buffer and.**   
1460e 20 20 20 70 72 6f 63 65 65 64 20 74 6f 20 73 74     proceed to st
1460f 65 70 20 35 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 35  ep 5. .**.**   5
14610 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6c 6c  . Otherwise, all
14611 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e  ocate and return
14612 20 61 20 6e 65 77 20 70 61 67 65 20 62 75 66 66   a new page buff
14613 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71  er..*/.static sq
14614 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61 67  lite3_pcache_pag
14615 65 20 2a 70 63 61 63 68 65 31 46 65 74 63 68 28  e *pcache1Fetch(
14616 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  .  sqlite3_pcach
14617 65 20 2a 70 2c 20 0a 20 20 75 6e 73 69 67 6e 65  e *p, .  unsigne
14618 64 20 69 6e 74 20 69 4b 65 79 2c 20 0a 20 20 69  d int iKey, .  i
14619 6e 74 20 63 72 65 61 74 65 46 6c 61 67 0a 29 7b  nt createFlag.){
1461a 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
1461b 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68  nPinned;.  PCach
1461c 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43  e1 *pCache = (PC
1461d 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 47 72  ache1 *)p;.  PGr
1461e 6f 75 70 20 2a 70 47 72 6f 75 70 3b 0a 20 20 50  oup *pGroup;.  P
1461f 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 30  gHdr1 *pPage = 0
14620 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 61  ;..  assert( pCa
14621 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20  che->bPurgeable 
14622 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 21 3d 31  || createFlag!=1
14623 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
14624 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
14625 20 7c 7c 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e   || pCache->nMin
14626 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
14627 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61   pCache->bPurgea
14628 62 6c 65 3d 3d 30 20 7c 7c 20 70 43 61 63 68 65  ble==0 || pCache
14629 2d 3e 6e 4d 69 6e 3d 3d 31 30 20 29 3b 0a 20 20  ->nMin==10 );.  
1462a 61 73 73 65 72 74 28 20 70 43 61 63 68 65 2d 3e  assert( pCache->
1462b 6e 4d 69 6e 3d 3d 30 20 7c 7c 20 70 43 61 63 68  nMin==0 || pCach
1462c 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 3b  e->bPurgeable );
1462d 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d  .  pcache1EnterM
1462e 75 74 65 78 28 70 47 72 6f 75 70 20 3d 20 70 43  utex(pGroup = pC
1462f 61 63 68 65 2d 3e 70 47 72 6f 75 70 29 3b 0a 0a  ache->pGroup);..
14630 20 20 2f 2a 20 53 74 65 70 20 31 3a 20 53 65 61    /* Step 1: Sea
14631 72 63 68 20 74 68 65 20 68 61 73 68 20 74 61 62  rch the hash tab
14632 6c 65 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69  le for an existi
14633 6e 67 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 69  ng entry. */.  i
14634 66 28 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68  f( pCache->nHash
14635 3e 30 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e  >0 ){.    unsign
14636 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 79 20  ed int h = iKey 
14637 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b  % pCache->nHash;
14638 0a 20 20 20 20 66 6f 72 28 70 50 61 67 65 3d 70  .    for(pPage=p
14639 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d  Cache->apHash[h]
1463a 3b 20 70 50 61 67 65 26 26 70 50 61 67 65 2d 3e  ; pPage&&pPage->
1463b 69 4b 65 79 21 3d 69 4b 65 79 3b 20 70 50 61 67  iKey!=iKey; pPag
1463c 65 3d 70 50 61 67 65 2d 3e 70 4e 65 78 74 29 3b  e=pPage->pNext);
1463d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20  .  }..  /* Step 
1463e 32 3a 20 41 62 6f 72 74 20 69 66 20 6e 6f 20 65  2: Abort if no e
1463f 78 69 73 74 69 6e 67 20 70 61 67 65 20 69 73 20  xisting page is 
14640 66 6f 75 6e 64 20 61 6e 64 20 63 72 65 61 74 65  found and create
14641 46 6c 61 67 20 69 73 20 30 20 2a 2f 0a 20 20 69  Flag is 0 */.  i
14642 66 28 20 70 50 61 67 65 20 7c 7c 20 63 72 65 61  f( pPage || crea
14643 74 65 46 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20  teFlag==0 ){.   
14644 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28   pcache1PinPage(
14645 70 50 61 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f  pPage);.    goto
14646 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a   fetch_out;.  }.
14647 0a 20 20 2f 2a 20 54 68 65 20 70 47 72 6f 75 70  .  /* The pGroup
14648 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20   local variable 
14649 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79 20 62 65  will normally be
1464a 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 79 20   initialized by 
1464b 74 68 65 0a 20 20 2a 2a 20 70 63 61 63 68 65 31  the.  ** pcache1
1464c 45 6e 74 65 72 4d 75 74 65 78 28 29 20 6d 61 63  EnterMutex() mac
1464d 72 6f 20 61 62 6f 76 65 2e 20 20 42 75 74 20 69  ro above.  But i
1464e 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  f SQLITE_MUTEX_O
1464f 4d 49 54 20 69 73 20 64 65 66 69 6e 65 64 2c 0a  MIT is defined,.
14650 20 20 2a 2a 20 74 68 65 6e 20 70 63 61 63 68 65    ** then pcache
14651 31 45 6e 74 65 72 4d 75 74 65 78 28 29 20 69 73  1EnterMutex() is
14652 20 61 20 6e 6f 2d 6f 70 2c 20 73 6f 20 77 65 20   a no-op, so we 
14653 68 61 76 65 20 74 6f 20 69 6e 69 74 69 61 6c 69  have to initiali
14654 7a 65 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 63 61  ze the.  ** loca
14655 6c 20 76 61 72 69 61 62 6c 65 20 68 65 72 65 2e  l variable here.
14656 20 20 44 65 6c 61 79 69 6e 67 20 74 68 65 20 69    Delaying the i
14657 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66  nitialization of
14658 20 70 47 72 6f 75 70 20 69 73 20 61 6e 0a 20 20   pGroup is an.  
14659 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 3a  ** optimization:
1465a 20 20 54 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73    The common cas
1465b 65 20 69 73 20 74 6f 20 65 78 69 74 20 74 68 65  e is to exit the
1465c 20 6d 6f 64 75 6c 65 20 62 65 66 6f 72 65 20 72   module before r
1465d 65 61 63 68 69 6e 67 0a 20 20 2a 2a 20 74 68 69  eaching.  ** thi
1465e 73 20 70 6f 69 6e 74 2e 0a 20 20 2a 2f 0a 23 69  s point..  */.#i
1465f 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45  fdef SQLITE_MUTE
14660 58 5f 4f 4d 49 54 0a 20 20 70 47 72 6f 75 70 20  X_OMIT.  pGroup 
14661 3d 20 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70  = pCache->pGroup
14662 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 53  ;.#endif..  /* S
14663 74 65 70 20 33 3a 20 41 62 6f 72 74 20 69 66 20  tep 3: Abort if 
14664 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 31 20  createFlag is 1 
14665 62 75 74 20 74 68 65 20 63 61 63 68 65 20 69 73  but the cache is
14666 20 6e 65 61 72 6c 79 20 66 75 6c 6c 20 2a 2f 0a   nearly full */.
14667 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65    assert( pCache
14668 2d 3e 6e 50 61 67 65 20 3e 3d 20 70 43 61 63 68  ->nPage >= pCach
14669 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 20 29  e->nRecyclable )
1466a 3b 0a 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43  ;.  nPinned = pC
1466b 61 63 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43  ache->nPage - pC
1466c 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c  ache->nRecyclabl
1466d 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 47 72  e;.  assert( pGr
1466e 6f 75 70 2d 3e 6d 78 50 69 6e 6e 65 64 20 3d 3d  oup->mxPinned ==
1466f 20 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 61 67   pGroup->nMaxPag
14670 65 20 2b 20 31 30 20 2d 20 70 47 72 6f 75 70 2d  e + 10 - pGroup-
14671 3e 6e 4d 69 6e 50 61 67 65 20 29 3b 0a 20 20 61  >nMinPage );.  a
14672 73 73 65 72 74 28 20 70 43 61 63 68 65 2d 3e 6e  ssert( pCache->n
14673 39 30 70 63 74 20 3d 3d 20 70 43 61 63 68 65 2d  90pct == pCache-
14674 3e 6e 4d 61 78 2a 39 2f 31 30 20 29 3b 0a 20 20  >nMax*9/10 );.  
14675 69 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d 3d  if( createFlag==
14676 31 20 26 26 20 28 0a 20 20 20 20 20 20 20 20 6e  1 && (.        n
14677 50 69 6e 6e 65 64 3e 3d 70 47 72 6f 75 70 2d 3e  Pinned>=pGroup->
14678 6d 78 50 69 6e 6e 65 64 0a 20 20 20 20 20 7c 7c  mxPinned.     ||
14679 20 6e 50 69 6e 6e 65 64 3e 3d 70 43 61 63 68 65   nPinned>=pCache
1467a 2d 3e 6e 39 30 70 63 74 0a 20 20 20 20 20 7c 7c  ->n90pct.     ||
1467b 20 70 63 61 63 68 65 31 55 6e 64 65 72 4d 65 6d   pcache1UnderMem
1467c 6f 72 79 50 72 65 73 73 75 72 65 28 70 43 61 63  oryPressure(pCac
1467d 68 65 29 0a 20 20 29 29 7b 0a 20 20 20 20 67 6f  he).  )){.    go
1467e 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20  to fetch_out;.  
1467f 7d 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d  }..  if( pCache-
14680 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d 3e  >nPage>=pCache->
14681 6e 48 61 73 68 20 26 26 20 70 63 61 63 68 65 31  nHash && pcache1
14682 52 65 73 69 7a 65 48 61 73 68 28 70 43 61 63 68  ResizeHash(pCach
14683 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66  e) ){.    goto f
14684 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  etch_out;.  }.. 
14685 20 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79 20   /* Step 4. Try 
14686 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 67  to recycle a pag
14687 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61 63  e. */.  if( pCac
14688 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26  he->bPurgeable &
14689 26 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 54 61  & pGroup->pLruTa
1468a 69 6c 20 26 26 20 28 0a 20 20 20 20 20 20 20 20  il && (.        
1468b 20 28 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2b   (pCache->nPage+
1468c 31 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29  1>=pCache->nMax)
1468d 0a 20 20 20 20 20 20 7c 7c 20 70 47 72 6f 75 70  .      || pGroup
1468e 2d 3e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 3d  ->nCurrentPage>=
1468f 70 47 72 6f 75 70 2d 3e 6e 4d 61 78 50 61 67 65  pGroup->nMaxPage
14690 0a 20 20 20 20 20 20 7c 7c 20 70 63 61 63 68 65  .      || pcache
14691 31 55 6e 64 65 72 4d 65 6d 6f 72 79 50 72 65 73  1UnderMemoryPres
14692 73 75 72 65 28 70 43 61 63 68 65 29 0a 20 20 29  sure(pCache).  )
14693 29 7b 0a 20 20 20 20 50 43 61 63 68 65 31 20 2a  ){.    PCache1 *
14694 70 4f 74 68 65 72 3b 0a 20 20 20 20 70 50 61 67  pOther;.    pPag
14695 65 20 3d 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75  e = pGroup->pLru
14696 54 61 69 6c 3b 0a 20 20 20 20 70 63 61 63 68 65  Tail;.    pcache
14697 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28  1RemoveFromHash(
14698 70 50 61 67 65 29 3b 0a 20 20 20 20 70 63 61 63  pPage);.    pcac
14699 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67 65  he1PinPage(pPage
1469a 29 3b 0a 20 20 20 20 70 4f 74 68 65 72 20 3d 20  );.    pOther = 
1469b 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a 0a  pPage->pCache;..
1469c 20 20 20 20 2f 2a 20 57 65 20 77 61 6e 74 20 74      /* We want t
1469d 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 73 7a  o verify that sz
1469e 50 61 67 65 20 61 6e 64 20 73 7a 45 78 74 72 61  Page and szExtra
1469f 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 66 6f   are the same fo
146a0 72 20 70 4f 74 68 65 72 0a 20 20 20 20 2a 2a 20  r pOther.    ** 
146a1 61 6e 64 20 70 43 61 63 68 65 2e 20 20 41 73 73  and pCache.  Ass
146a2 65 72 74 20 74 68 61 74 20 77 65 20 63 61 6e 20  ert that we can 
146a3 76 65 72 69 66 79 20 74 68 69 73 20 62 79 20 63  verify this by c
146a4 6f 6d 70 61 72 69 6e 67 20 73 75 6d 73 2e 20 2a  omparing sums. *
146a5 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  /.    assert( (p
146a6 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 26 20  Cache->szPage & 
146a7 28 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 2d  (pCache->szPage-
146a8 31 29 29 3d 3d 30 20 26 26 20 70 43 61 63 68 65  1))==0 && pCache
146a9 2d 3e 73 7a 50 61 67 65 3e 3d 35 31 32 20 29 3b  ->szPage>=512 );
146aa 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 61  .    assert( pCa
146ab 63 68 65 2d 3e 73 7a 45 78 74 72 61 3c 35 31 32  che->szExtra<512
146ac 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
146ad 28 70 4f 74 68 65 72 2d 3e 73 7a 50 61 67 65 20  (pOther->szPage 
146ae 26 20 28 70 4f 74 68 65 72 2d 3e 73 7a 50 61 67  & (pOther->szPag
146af 65 2d 31 29 29 3d 3d 30 20 26 26 20 70 4f 74 68  e-1))==0 && pOth
146b0 65 72 2d 3e 73 7a 50 61 67 65 3e 3d 35 31 32 20  er->szPage>=512 
146b1 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
146b2 4f 74 68 65 72 2d 3e 73 7a 45 78 74 72 61 3c 35  Other->szExtra<5
146b3 31 32 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 70  12 );..    if( p
146b4 4f 74 68 65 72 2d 3e 73 7a 50 61 67 65 2b 70 4f  Other->szPage+pO
146b5 74 68 65 72 2d 3e 73 7a 45 78 74 72 61 20 21 3d  ther->szExtra !=
146b6 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 2b   pCache->szPage+
146b7 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20  pCache->szExtra 
146b8 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  ){.      pcache1
146b9 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b  FreePage(pPage);
146ba 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 30  .      pPage = 0
146bb 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
146bc 20 20 20 70 47 72 6f 75 70 2d 3e 6e 43 75 72 72     pGroup->nCurr
146bd 65 6e 74 50 61 67 65 20 2d 3d 20 28 70 4f 74 68  entPage -= (pOth
146be 65 72 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 2d  er->bPurgeable -
146bf 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61   pCache->bPurgea
146c0 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ble);.    }.  }.
146c1 0a 20 20 2f 2a 20 53 74 65 70 20 35 2e 20 49 66  .  /* Step 5. If
146c2 20 61 20 75 73 61 62 6c 65 20 70 61 67 65 20 62   a usable page b
146c3 75 66 66 65 72 20 68 61 73 20 73 74 69 6c 6c 20  uffer has still 
146c4 6e 6f 74 20 62 65 65 6e 20 66 6f 75 6e 64 2c 20  not been found, 
146c5 0a 20 20 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f  .  ** attempt to
146c6 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   allocate a new 
146c7 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28  one. .  */.  if(
146c8 20 21 70 50 61 67 65 20 29 7b 0a 20 20 20 20 69   !pPage ){.    i
146c9 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31  f( createFlag==1
146ca 20 29 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42   ) sqlite3BeginB
146cb 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
146cc 20 20 20 70 50 61 67 65 20 3d 20 70 63 61 63 68     pPage = pcach
146cd 65 31 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63  e1AllocPage(pCac
146ce 68 65 29 3b 0a 20 20 20 20 69 66 28 20 63 72 65  he);.    if( cre
146cf 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c  ateFlag==1 ) sql
146d0 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
146d1 6c 6f 63 28 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  loc();.  }..  if
146d2 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 75  ( pPage ){.    u
146d3 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20  nsigned int h = 
146d4 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e  iKey % pCache->n
146d5 48 61 73 68 3b 0a 20 20 20 20 70 43 61 63 68 65  Hash;.    pCache
146d6 2d 3e 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70  ->nPage++;.    p
146d7 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65  Page->iKey = iKe
146d8 79 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e  y;.    pPage->pN
146d9 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70  ext = pCache->ap
146da 48 61 73 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61  Hash[h];.    pPa
146db 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61  ge->pCache = pCa
146dc 63 68 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  che;.    pPage->
146dd 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20  pLruPrev = 0;.  
146de 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78    pPage->pLruNex
146df 74 20 3d 20 30 3b 0a 20 20 20 20 2a 28 76 6f 69  t = 0;.    *(voi
146e0 64 20 2a 2a 29 70 50 61 67 65 2d 3e 70 61 67 65  d **)pPage->page
146e1 2e 70 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 20  .pExtra = 0;.   
146e2 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b   pCache->apHash[
146e3 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a  h] = pPage;.  }.
146e4 0a 66 65 74 63 68 5f 6f 75 74 3a 0a 20 20 69 66  .fetch_out:.  if
146e5 28 20 70 50 61 67 65 20 26 26 20 69 4b 65 79 3e  ( pPage && iKey>
146e6 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20  pCache->iMaxKey 
146e7 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 69  ){.    pCache->i
146e8 4d 61 78 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20  MaxKey = iKey;. 
146e9 20 7d 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76   }.  pcache1Leav
146ea 65 4d 75 74 65 78 28 70 47 72 6f 75 70 29 3b 0a  eMutex(pGroup);.
146eb 20 20 72 65 74 75 72 6e 20 26 70 50 61 67 65 2d    return &pPage-
146ec 3e 70 61 67 65 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  >page;.}.../*.**
146ed 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
146ee 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70  of the sqlite3_p
146ef 63 61 63 68 65 2e 78 55 6e 70 69 6e 20 6d 65 74  cache.xUnpin met
146f0 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72 6b 20  hod..**.** Mark 
146f1 61 20 70 61 67 65 20 61 73 20 75 6e 70 69 6e 6e  a page as unpinn
146f2 65 64 20 28 65 6c 69 67 69 62 6c 65 20 66 6f 72  ed (eligible for
146f3 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20 72 65   asynchronous re
146f4 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f 0a 73 74 61  cycling)..*/.sta
146f5 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31  tic void pcache1
146f6 55 6e 70 69 6e 28 0a 20 20 73 71 6c 69 74 65 33  Unpin(.  sqlite3
146f7 5f 70 63 61 63 68 65 20 2a 70 2c 20 0a 20 20 73  _pcache *p, .  s
146f8 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 70 61  qlite3_pcache_pa
146f9 67 65 20 2a 70 50 67 2c 20 0a 20 20 69 6e 74 20  ge *pPg, .  int 
146fa 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 0a 29 7b  reuseUnlikely.){
146fb 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63  .  PCache1 *pCac
146fc 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29  he = (PCache1 *)
146fd 70 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 50 61  p;.  PgHdr1 *pPa
146fe 67 65 20 3d 20 28 50 67 48 64 72 31 20 2a 29 70  ge = (PgHdr1 *)p
146ff 50 67 3b 0a 20 20 50 47 72 6f 75 70 20 2a 70 47  Pg;.  PGroup *pG
14700 72 6f 75 70 20 3d 20 70 43 61 63 68 65 2d 3e 70  roup = pCache->p
14701 47 72 6f 75 70 3b 0a 20 0a 20 20 61 73 73 65 72  Group;. .  asser
14702 74 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65  t( pPage->pCache
14703 3d 3d 70 43 61 63 68 65 20 29 3b 0a 20 20 70 63  ==pCache );.  pc
14704 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
14705 70 47 72 6f 75 70 29 3b 0a 0a 20 20 2f 2a 20 49  pGroup);..  /* I
14706 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f  t is an error to
14707 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74   call this funct
14708 69 6f 6e 20 69 66 20 74 68 65 20 70 61 67 65 20  ion if the page 
14709 69 73 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a  is already .  **
1470a 20 70 61 72 74 20 6f 66 20 74 68 65 20 50 47 72   part of the PGr
1470b 6f 75 70 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20  oup LRU list..  
1470c 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
1470d 67 65 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20  ge->pLruPrev==0 
1470e 26 26 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65  && pPage->pLruNe
1470f 78 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  xt==0 );.  asser
14710 74 28 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 48  t( pGroup->pLruH
14711 65 61 64 21 3d 70 50 61 67 65 20 26 26 20 70 47  ead!=pPage && pG
14712 72 6f 75 70 2d 3e 70 4c 72 75 54 61 69 6c 21 3d  roup->pLruTail!=
14713 70 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20  pPage );..  if( 
14714 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c  reuseUnlikely ||
14715 20 70 47 72 6f 75 70 2d 3e 6e 43 75 72 72 65 6e   pGroup->nCurren
14716 74 50 61 67 65 3e 70 47 72 6f 75 70 2d 3e 6e 4d  tPage>pGroup->nM
14717 61 78 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63  axPage ){.    pc
14718 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48  ache1RemoveFromH
14719 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20  ash(pPage);.    
1471a 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28  pcache1FreePage(
1471b 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b  pPage);.  }else{
1471c 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20  .    /* Add the 
1471d 70 61 67 65 20 74 6f 20 74 68 65 20 50 47 72 6f  page to the PGro
1471e 75 70 20 4c 52 55 20 6c 69 73 74 2e 20 2a 2f 0a  up LRU list. */.
1471f 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 2d 3e      if( pGroup->
14720 70 4c 72 75 48 65 61 64 20 29 7b 0a 20 20 20 20  pLruHead ){.    
14721 20 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 48 65    pGroup->pLruHe
14722 61 64 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70  ad->pLruPrev = p
14723 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67  Page;.      pPag
14724 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 47  e->pLruNext = pG
14725 72 6f 75 70 2d 3e 70 4c 72 75 48 65 61 64 3b 0a  roup->pLruHead;.
14726 20 20 20 20 20 20 70 47 72 6f 75 70 2d 3e 70 4c        pGroup->pL
14727 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a  ruHead = pPage;.
14728 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
14729 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 54 61 69   pGroup->pLruTai
1472a 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20  l = pPage;.     
1472b 20 70 47 72 6f 75 70 2d 3e 70 4c 72 75 48 65 61   pGroup->pLruHea
1472c 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d  d = pPage;.    }
1472d 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65  .    pCache->nRe
1472e 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a  cyclable++;.  }.
1472f 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  .  pcache1LeaveM
14730 75 74 65 78 28 70 43 61 63 68 65 2d 3e 70 47 72  utex(pCache->pGr
14731 6f 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  oup);.}../*.** I
14732 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
14733 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
14734 63 68 65 2e 78 52 65 6b 65 79 20 6d 65 74 68 6f  che.xRekey metho
14735 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  d. .*/.static vo
14736 69 64 20 70 63 61 63 68 65 31 52 65 6b 65 79 28  id pcache1Rekey(
14737 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  .  sqlite3_pcach
14738 65 20 2a 70 2c 0a 20 20 73 71 6c 69 74 65 33 5f  e *p,.  sqlite3_
14739 70 63 61 63 68 65 5f 70 61 67 65 20 2a 70 50 67  pcache_page *pPg
1473a 2c 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ,.  unsigned int
1473b 20 69 4f 6c 64 2c 0a 20 20 75 6e 73 69 67 6e 65   iOld,.  unsigne
1473c 64 20 69 6e 74 20 69 4e 65 77 0a 29 7b 0a 20 20  d int iNew.){.  
1473d 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20  PCache1 *pCache 
1473e 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a  = (PCache1 *)p;.
1473f 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 20    PgHdr1 *pPage 
14740 3d 20 28 50 67 48 64 72 31 20 2a 29 70 50 67 3b  = (PgHdr1 *)pPg;
14741 0a 20 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a  .  PgHdr1 **pp;.
14742 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
14743 3b 20 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ; .  assert( pPa
14744 67 65 2d 3e 69 4b 65 79 3d 3d 69 4f 6c 64 20 29  ge->iKey==iOld )
14745 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
14746 65 2d 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68  e->pCache==pCach
14747 65 20 29 3b 0a 0a 20 20 70 63 61 63 68 65 31 45  e );..  pcache1E
14748 6e 74 65 72 4d 75 74 65 78 28 70 43 61 63 68 65  nterMutex(pCache
14749 2d 3e 70 47 72 6f 75 70 29 3b 0a 0a 20 20 68 20  ->pGroup);..  h 
1474a 3d 20 69 4f 6c 64 25 70 43 61 63 68 65 2d 3e 6e  = iOld%pCache->n
1474b 48 61 73 68 3b 0a 20 20 70 70 20 3d 20 26 70 43  Hash;.  pp = &pC
1474c 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b  ache->apHash[h];
1474d 0a 20 20 77 68 69 6c 65 28 20 28 2a 70 70 29 21  .  while( (*pp)!
1474e 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 70  =pPage ){.    pp
1474f 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74   = &(*pp)->pNext
14750 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20 70 50  ;.  }.  *pp = pP
14751 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 68  age->pNext;..  h
14752 20 3d 20 69 4e 65 77 25 70 43 61 63 68 65 2d 3e   = iNew%pCache->
14753 6e 48 61 73 68 3b 0a 20 20 70 50 61 67 65 2d 3e  nHash;.  pPage->
14754 69 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20 20 70  iKey = iNew;.  p
14755 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 70 43  Page->pNext = pC
14756 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b  ache->apHash[h];
14757 0a 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73  .  pCache->apHas
14758 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20  h[h] = pPage;.  
14759 69 66 28 20 69 4e 65 77 3e 70 43 61 63 68 65 2d  if( iNew>pCache-
1475a 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 20  >iMaxKey ){.    
1475b 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20  pCache->iMaxKey 
1475c 3d 20 69 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 70  = iNew;.  }..  p
1475d 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
1475e 28 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70 29  (pCache->pGroup)
1475f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
14760 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
14761 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e   sqlite3_pcache.
14762 78 54 72 75 6e 63 61 74 65 20 6d 65 74 68 6f 64  xTruncate method
14763 2e 20 0a 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 64  . .**.** Discard
14764 20 61 6c 6c 20 75 6e 70 69 6e 6e 65 64 20 70 61   all unpinned pa
14765 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65  ges in the cache
14766 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d   with a page num
14767 62 65 72 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20  ber equal to.** 
14768 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  or greater than 
14769 70 61 72 61 6d 65 74 65 72 20 69 4c 69 6d 69 74  parameter iLimit
1476a 2e 20 41 6e 79 20 70 69 6e 6e 65 64 20 70 61 67  . Any pinned pag
1476b 65 73 20 77 69 74 68 20 61 20 70 61 67 65 20 6e  es with a page n
1476c 75 6d 62 65 72 0a 2a 2a 20 65 71 75 61 6c 20 74  umber.** equal t
1476d 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61  o or greater tha
1476e 6e 20 69 4c 69 6d 69 74 20 61 72 65 20 69 6d 70  n iLimit are imp
1476f 6c 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64  licitly unpinned
14770 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
14771 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65   pcache1Truncate
14772 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20  (sqlite3_pcache 
14773 2a 70 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  *p, unsigned int
14774 20 69 4c 69 6d 69 74 29 7b 0a 20 20 50 43 61 63   iLimit){.  PCac
14775 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50  he1 *pCache = (P
14776 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 70 63  Cache1 *)p;.  pc
14777 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
14778 70 43 61 63 68 65 2d 3e 70 47 72 6f 75 70 29 3b  pCache->pGroup);
14779 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3c 3d 70  .  if( iLimit<=p
1477a 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29  Cache->iMaxKey )
1477b 7b 0a 20 20 20 20 70 63 61 63 68 65 31 54 72 75  {.    pcache1Tru
1477c 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43 61 63  ncateUnsafe(pCac
1477d 68 65 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20  he, iLimit);.   
1477e 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79   pCache->iMaxKey
1477f 20 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a 20 20 7d   = iLimit-1;.  }
14780 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  .  pcache1LeaveM
14781 75 74 65 78 28 70 43 61 63 68 65 2d 3e 70 47 72  utex(pCache->pGr
14782 6f 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  oup);.}../*.** I
14783 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
14784 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
14785 63 68 65 2e 78 44 65 73 74 72 6f 79 20 6d 65 74  che.xDestroy met
14786 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73 74  hod. .**.** Dest
14787 72 6f 79 20 61 20 63 61 63 68 65 20 61 6c 6c 6f  roy a cache allo
14788 63 61 74 65 64 20 75 73 69 6e 67 20 70 63 61 63  cated using pcac
14789 68 65 31 43 72 65 61 74 65 28 29 2e 0a 2a 2f 0a  he1Create()..*/.
1478a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
1478b 68 65 31 44 65 73 74 72 6f 79 28 73 71 6c 69 74  he1Destroy(sqlit
1478c 65 33 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a 20  e3_pcache *p){. 
1478d 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65   PCache1 *pCache
1478e 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b   = (PCache1 *)p;
1478f 0a 20 20 50 47 72 6f 75 70 20 2a 70 47 72 6f 75  .  PGroup *pGrou
14790 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 47 72 6f  p = pCache->pGro
14791 75 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  up;.  assert( pC
14792 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
14793 20 7c 7c 20 28 70 43 61 63 68 65 2d 3e 6e 4d 61   || (pCache->nMa
14794 78 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e  x==0 && pCache->
14795 6e 4d 69 6e 3d 3d 30 29 20 29 3b 0a 20 20 70 63  nMin==0) );.  pc
14796 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
14797 70 47 72 6f 75 70 29 3b 0a 20 20 70 63 61 63 68  pGroup);.  pcach
14798 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65  e1TruncateUnsafe
14799 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20 20 61  (pCache, 0);.  a
1479a 73 73 65 72 74 28 20 70 47 72 6f 75 70 2d 3e 6e  ssert( pGroup->n
1479b 4d 61 78 50 61 67 65 20 3e 3d 20 70 43 61 63 68  MaxPage >= pCach
1479c 65 2d 3e 6e 4d 61 78 20 29 3b 0a 20 20 70 47 72  e->nMax );.  pGr
1479d 6f 75 70 2d 3e 6e 4d 61 78 50 61 67 65 20 2d 3d  oup->nMaxPage -=
1479e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20   pCache->nMax;. 
1479f 20 61 73 73 65 72 74 28 20 70 47 72 6f 75 70 2d   assert( pGroup-
147a0 3e 6e 4d 69 6e 50 61 67 65 20 3e 3d 20 70 43 61  >nMinPage >= pCa
147a1 63 68 65 2d 3e 6e 4d 69 6e 20 29 3b 0a 20 20 70  che->nMin );.  p
147a2 47 72 6f 75 70 2d 3e 6e 4d 69 6e 50 61 67 65 20  Group->nMinPage 
147a3 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b  -= pCache->nMin;
147a4 0a 20 20 70 47 72 6f 75 70 2d 3e 6d 78 50 69 6e  .  pGroup->mxPin
147a5 6e 65 64 20 3d 20 70 47 72 6f 75 70 2d 3e 6e 4d  ned = pGroup->nM
147a6 61 78 50 61 67 65 20 2b 20 31 30 20 2d 20 70 47  axPage + 10 - pG
147a7 72 6f 75 70 2d 3e 6e 4d 69 6e 50 61 67 65 3b 0a  roup->nMinPage;.
147a8 20 20 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65    pcache1Enforce
147a9 4d 61 78 50 61 67 65 28 70 47 72 6f 75 70 29 3b  MaxPage(pGroup);
147aa 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  .  pcache1LeaveM
147ab 75 74 65 78 28 70 47 72 6f 75 70 29 3b 0a 20 20  utex(pGroup);.  
147ac 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 61  sqlite3_free(pCa
147ad 63 68 65 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20  che->apHash);.  
147ae 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 61  sqlite3_free(pCa
147af 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  che);.}../*.** T
147b0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
147b1 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 69 6e  called during in
147b2 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 28 73 71  itialization (sq
147b3 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
147b4 28 29 29 20 74 6f 0a 2a 2a 20 69 6e 73 74 61 6c  ()) to.** instal
147b5 6c 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 6c  l the default pl
147b6 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 6d 6f  uggable cache mo
147b7 64 75 6c 65 2c 20 61 73 73 75 6d 69 6e 67 20 74  dule, assuming t
147b8 68 65 20 75 73 65 72 20 68 61 73 20 6e 6f 74 0a  he user has not.
147b9 2a 2a 20 61 6c 72 65 61 64 79 20 70 72 6f 76 69  ** already provi
147ba 64 65 64 20 61 6e 20 61 6c 74 65 72 6e 61 74 69  ded an alternati
147bb 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ve..*/.SQLITE_PR
147bc 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
147bd 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61 75  e3PCacheSetDefau
147be 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  lt(void){.  stat
147bf 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
147c0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 32  _pcache_methods2
147c1 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20   defaultMethods 
147c2 3d 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20  = {.    1,      
147c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
147c4 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a   /* iVersion */.
147c5 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
147c6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
147c7 70 41 72 67 20 2a 2f 0a 20 20 20 20 70 63 61 63  pArg */.    pcac
147c8 68 65 31 49 6e 69 74 2c 20 20 20 20 20 20 20 20  he1Init,        
147c9 20 20 20 20 20 2f 2a 20 78 49 6e 69 74 20 2a 2f       /* xInit */
147ca 0a 20 20 20 20 70 63 61 63 68 65 31 53 68 75 74  .    pcache1Shut
147cb 64 6f 77 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a  down,         /*
147cc 20 78 53 68 75 74 64 6f 77 6e 20 2a 2f 0a 20 20   xShutdown */.  
147cd 20 20 70 63 61 63 68 65 31 43 72 65 61 74 65 2c    pcache1Create,
147ce 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
147cf 72 65 61 74 65 20 2a 2f 0a 20 20 20 20 70 63 61  reate */.    pca
147d0 63 68 65 31 43 61 63 68 65 73 69 7a 65 2c 20 20  che1Cachesize,  
147d1 20 20 20 20 20 20 2f 2a 20 78 43 61 63 68 65 73        /* xCaches
147d2 69 7a 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 68  ize */.    pcach
147d3 65 31 50 61 67 65 63 6f 75 6e 74 2c 20 20 20 20  e1Pagecount,    
147d4 20 20 20 20 2f 2a 20 78 50 61 67 65 63 6f 75 6e      /* xPagecoun
147d5 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31  t */.    pcache1
147d6 46 65 74 63 68 2c 20 20 20 20 20 20 20 20 20 20  Fetch,          
147d7 20 20 2f 2a 20 78 46 65 74 63 68 20 2a 2f 0a 20    /* xFetch */. 
147d8 20 20 20 70 63 61 63 68 65 31 55 6e 70 69 6e 2c     pcache1Unpin,
147d9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
147da 55 6e 70 69 6e 20 2a 2f 0a 20 20 20 20 70 63 61  Unpin */.    pca
147db 63 68 65 31 52 65 6b 65 79 2c 20 20 20 20 20 20  che1Rekey,      
147dc 20 20 20 20 20 20 2f 2a 20 78 52 65 6b 65 79 20        /* xRekey 
147dd 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 54 72  */.    pcache1Tr
147de 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20  uncate,         
147df 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a  /* xTruncate */.
147e0 20 20 20 20 70 63 61 63 68 65 31 44 65 73 74 72      pcache1Destr
147e1 6f 79 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  oy,          /* 
147e2 78 44 65 73 74 72 6f 79 20 2a 2f 0a 20 20 20 20  xDestroy */.    
147e3 70 63 61 63 68 65 31 53 68 72 69 6e 6b 20 20 20  pcache1Shrink   
147e4 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68 72           /* xShr
147e5 69 6e 6b 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71  ink */.  };.  sq
147e6 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c  lite3_config(SQL
147e7 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48  ITE_CONFIG_PCACH
147e8 45 32 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68  E2, &defaultMeth
147e9 6f 64 73 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20  ods);.}..#ifdef 
147ea 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
147eb 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a  MORY_MANAGEMENT.
147ec 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
147ed 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ion is called to
147ee 20 66 72 65 65 20 73 75 70 65 72 66 6c 75 6f 75   free superfluou
147ef 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  s dynamically al
147f0 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 0a 2a  located memory.*
147f1 2a 20 68 65 6c 64 20 62 79 20 74 68 65 20 70 61  * held by the pa
147f2 67 65 72 20 73 79 73 74 65 6d 2e 20 4d 65 6d 6f  ger system. Memo
147f3 72 79 20 69 6e 20 75 73 65 20 62 79 20 61 6e 79  ry in use by any
147f4 20 53 51 4c 69 74 65 20 70 61 67 65 72 20 61 6c   SQLite pager al
147f5 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 79 20 74 68  located.** by th
147f6 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64  e current thread
147f7 20 6d 61 79 20 62 65 20 73 71 6c 69 74 65 33 5f   may be sqlite3_
147f8 66 72 65 65 28 29 65 64 2e 0a 2a 2a 0a 2a 2a 20  free()ed..**.** 
147f9 6e 52 65 71 20 69 73 20 74 68 65 20 6e 75 6d 62  nReq is the numb
147fa 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d  er of bytes of m
147fb 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 2e 20  emory required. 
147fc 4f 6e 63 65 20 74 68 69 73 20 6d 75 63 68 20 68  Once this much h
147fd 61 73 0a 2a 2a 20 62 65 65 6e 20 72 65 6c 65 61  as.** been relea
147fe 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f  sed, the functio
147ff 6e 20 72 65 74 75 72 6e 73 2e 20 54 68 65 20 72  n returns. The r
14800 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74  eturn value is t
14801 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
14802 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 6f 66 20  .** of bytes of 
14803 6d 65 6d 6f 72 79 20 72 65 6c 65 61 73 65 64 2e  memory released.
14804 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14805 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
14806 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72  acheReleaseMemor
14807 79 28 69 6e 74 20 6e 52 65 71 29 7b 0a 20 20 69  y(int nReq){.  i
14808 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20  nt nFree = 0;.  
14809 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1480a 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 63  mutex_notheld(pc
1480b 61 63 68 65 31 2e 67 72 70 2e 6d 75 74 65 78 29  ache1.grp.mutex)
1480c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
1480d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
1480e 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65  eld(pcache1.mute
1480f 78 29 20 29 3b 0a 20 20 69 66 28 20 70 63 61 63  x) );.  if( pcac
14810 68 65 31 2e 70 53 74 61 72 74 3d 3d 30 20 29 7b  he1.pStart==0 ){
14811 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 3b 0a  .    PgHdr1 *p;.
14812 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72      pcache1Enter
14813 4d 75 74 65 78 28 26 70 63 61 63 68 65 31 2e 67  Mutex(&pcache1.g
14814 72 70 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  rp);.    while( 
14815 28 6e 52 65 71 3c 30 20 7c 7c 20 6e 46 72 65 65  (nReq<0 || nFree
14816 3c 6e 52 65 71 29 20 26 26 20 28 28 70 3d 70 63  <nReq) && ((p=pc
14817 61 63 68 65 31 2e 67 72 70 2e 70 4c 72 75 54 61  ache1.grp.pLruTa
14818 69 6c 29 21 3d 30 29 20 29 7b 0a 20 20 20 20 20  il)!=0) ){.     
14819 20 6e 46 72 65 65 20 2b 3d 20 70 63 61 63 68 65   nFree += pcache
1481a 31 4d 65 6d 53 69 7a 65 28 70 2d 3e 70 61 67 65  1MemSize(p->page
1481b 2e 70 42 75 66 29 3b 0a 23 69 66 64 65 66 20 53  .pBuf);.#ifdef S
1481c 51 4c 49 54 45 5f 50 43 41 43 48 45 5f 53 45 50  QLITE_PCACHE_SEP
1481d 41 52 41 54 45 5f 48 45 41 44 45 52 0a 20 20 20  ARATE_HEADER.   
1481e 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69     nFree += sqli
1481f 74 65 33 4d 65 6d 53 69 7a 65 28 70 29 3b 0a 23  te3MemSize(p);.#
14820 65 6e 64 69 66 0a 20 20 20 20 20 20 70 63 61 63  endif.      pcac
14821 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b 0a 20  he1PinPage(p);. 
14822 20 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f       pcache1Remo
14823 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20  veFromHash(p);. 
14824 20 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65       pcache1Free
14825 50 61 67 65 28 70 29 3b 0a 20 20 20 20 7d 0a 20  Page(p);.    }. 
14826 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d     pcache1LeaveM
14827 75 74 65 78 28 26 70 63 61 63 68 65 31 2e 67 72  utex(&pcache1.gr
14828 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  p);.  }.  return
14829 20 6e 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66   nFree;.}.#endif
1482a 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
1482b 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d  E_MEMORY_MANAGEM
1482c 45 4e 54 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53  ENT */..#ifdef S
1482d 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  QLITE_TEST./*.**
1482e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
1482f 73 20 75 73 65 64 20 62 79 20 74 65 73 74 20 70  s used by test p
14830 72 6f 63 65 64 75 72 65 73 20 74 6f 20 69 6e 73  rocedures to ins
14831 70 65 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61  pect the interna
14832 6c 20 73 74 61 74 65 0a 2a 2a 20 6f 66 20 74 68  l state.** of th
14833 65 20 67 6c 6f 62 61 6c 20 63 61 63 68 65 2e 0a  e global cache..
14834 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14835 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
14836 61 63 68 65 53 74 61 74 73 28 0a 20 20 69 6e 74  acheStats(.  int
14837 20 2a 70 6e 43 75 72 72 65 6e 74 2c 20 20 20 20   *pnCurrent,    
14838 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20    /* OUT: Total 
14839 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
1483a 63 61 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20  cached */.  int 
1483b 2a 70 6e 4d 61 78 2c 20 20 20 20 20 20 20 20 20  *pnMax,         
1483c 20 2f 2a 20 4f 55 54 3a 20 47 6c 6f 62 61 6c 20   /* OUT: Global 
1483d 6d 61 78 69 6d 75 6d 20 63 61 63 68 65 20 73 69  maximum cache si
1483e 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d  ze */.  int *pnM
1483f 69 6e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  in,          /* 
14840 4f 55 54 3a 20 53 75 6d 20 6f 66 20 50 43 61 63  OUT: Sum of PCac
14841 68 65 31 2e 6e 4d 69 6e 20 66 6f 72 20 70 75 72  he1.nMin for pur
14842 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f  geable caches */
14843 0a 20 20 69 6e 74 20 2a 70 6e 52 65 63 79 63 6c  .  int *pnRecycl
14844 61 62 6c 65 20 20 20 20 2f 2a 20 4f 55 54 3a 20  able    /* OUT: 
14845 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  Total number of 
14846 70 61 67 65 73 20 61 76 61 69 6c 61 62 6c 65 20  pages available 
14847 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 20 2a 2f  for recycling */
14848 0a 29 7b 0a 20 20 50 67 48 64 72 31 20 2a 70 3b  .){.  PgHdr1 *p;
14849 0a 20 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62  .  int nRecyclab
1484a 6c 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d  le = 0;.  for(p=
1484b 70 63 61 63 68 65 31 2e 67 72 70 2e 70 4c 72 75  pcache1.grp.pLru
1484c 48 65 61 64 3b 20 70 3b 20 70 3d 70 2d 3e 70 4c  Head; p; p=p->pL
1484d 72 75 4e 65 78 74 29 7b 0a 20 20 20 20 6e 52 65  ruNext){.    nRe
1484e 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 20 7d 0a  cyclable++;.  }.
1484f 20 20 2a 70 6e 43 75 72 72 65 6e 74 20 3d 20 70    *pnCurrent = p
14850 63 61 63 68 65 31 2e 67 72 70 2e 6e 43 75 72 72  cache1.grp.nCurr
14851 65 6e 74 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 61  entPage;.  *pnMa
14852 78 20 3d 20 28 69 6e 74 29 70 63 61 63 68 65 31  x = (int)pcache1
14853 2e 67 72 70 2e 6e 4d 61 78 50 61 67 65 3b 0a 20  .grp.nMaxPage;. 
14854 20 2a 70 6e 4d 69 6e 20 3d 20 28 69 6e 74 29 70   *pnMin = (int)p
14855 63 61 63 68 65 31 2e 67 72 70 2e 6e 4d 69 6e 50  cache1.grp.nMinP
14856 61 67 65 3b 0a 20 20 2a 70 6e 52 65 63 79 63 6c  age;.  *pnRecycl
14857 61 62 6c 65 20 3d 20 6e 52 65 63 79 63 6c 61 62  able = nRecyclab
14858 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  le;.}.#endif../*
14859 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1485a 64 20 6f 66 20 70 63 61 63 68 65 31 2e 63 20 2a  d of pcache1.c *
1485b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1485c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1485d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1485e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1485f 67 69 6e 20 66 69 6c 65 20 72 6f 77 73 65 74 2e  gin file rowset.
14860 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
14861 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14862 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
14863 0a 2a 2a 20 32 30 30 38 20 44 65 63 65 6d 62 65  .** 2008 Decembe
14864 72 20 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  r 3.**.** The au
14865 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
14866 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
14867 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
14868 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
14869 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
1486a 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
1486b 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
1486c 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
1486d 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
1486e 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
1486f 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
14870 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
14871 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
14872 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
14873 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
14874 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
14875 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
14876 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14877 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14878 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14879 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1487a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f  **.**.** This mo
1487b 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  dule implements 
1487c 61 6e 20 6f 62 6a 65 63 74 20 77 65 20 63 61 6c  an object we cal
1487d 6c 20 61 20 22 52 6f 77 53 65 74 22 2e 0a 2a 2a  l a "RowSet"..**
1487e 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65 74 20 6f  .** The RowSet o
1487f 62 6a 65 63 74 20 69 73 20 61 20 63 6f 6c 6c 65  bject is a colle
14880 63 74 69 6f 6e 20 6f 66 20 72 6f 77 69 64 73 2e  ction of rowids.
14881 20 20 52 6f 77 69 64 73 0a 2a 2a 20 61 72 65 20    Rowids.** are 
14882 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68  inserted into th
14883 65 20 52 6f 77 53 65 74 20 69 6e 20 61 6e 20 61  e RowSet in an a
14884 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 2e 20  rbitrary order. 
14885 20 49 6e 73 65 72 74 73 0a 2a 2a 20 63 61 6e 20   Inserts.** can 
14886 62 65 20 69 6e 74 65 72 6d 69 78 65 64 20 77 69  be intermixed wi
14887 74 68 20 74 65 73 74 73 20 74 6f 20 73 65 65 20  th tests to see 
14888 69 66 20 61 20 67 69 76 65 6e 20 72 6f 77 69 64  if a given rowid
14889 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65   has been.** pre
1488a 76 69 6f 75 73 6c 79 20 69 6e 73 65 72 74 65 64  viously inserted
1488b 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74   into the RowSet
1488c 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 6c  ..**.** After al
1488d 6c 20 69 6e 73 65 72 74 73 20 61 72 65 20 66 69  l inserts are fi
1488e 6e 69 73 68 65 64 2c 20 69 74 20 69 73 20 70 6f  nished, it is po
1488f 73 73 69 62 6c 65 20 74 6f 20 65 78 74 72 61 63  ssible to extrac
14890 74 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74  t the.** element
14891 73 20 6f 66 20 74 68 65 20 52 6f 77 53 65 74 20  s of the RowSet 
14892 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e  in sorted order.
14893 20 20 4f 6e 63 65 20 74 68 69 73 20 65 78 74 72    Once this extr
14894 61 63 74 69 6f 6e 0a 2a 2a 20 70 72 6f 63 65 73  action.** proces
14895 73 20 68 61 73 20 73 74 61 72 74 65 64 2c 20 6e  s has started, n
14896 6f 20 6e 65 77 20 65 6c 65 6d 65 6e 74 73 20 6d  o new elements m
14897 61 79 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a  ay be inserted..
14898 2a 2a 0a 2a 2a 20 48 65 6e 63 65 2c 20 74 68 65  **.** Hence, the
14899 20 70 72 69 6d 69 74 69 76 65 20 6f 70 65 72 61   primitive opera
1489a 74 69 6f 6e 73 20 66 6f 72 20 61 20 52 6f 77 53  tions for a RowS
1489b 65 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  et are:.**.**   
1489c 20 43 52 45 41 54 45 0a 2a 2a 20 20 20 20 49 4e   CREATE.**    IN
1489d 53 45 52 54 0a 2a 2a 20 20 20 20 54 45 53 54 0a  SERT.**    TEST.
1489e 2a 2a 20 20 20 20 53 4d 41 4c 4c 45 53 54 0a 2a  **    SMALLEST.*
1489f 2a 20 20 20 20 44 45 53 54 52 4f 59 0a 2a 2a 0a  *    DESTROY.**.
148a0 2a 2a 20 54 68 65 20 43 52 45 41 54 45 20 61 6e  ** The CREATE an
148a1 64 20 44 45 53 54 52 4f 59 20 70 72 69 6d 69 74  d DESTROY primit
148a2 69 76 65 73 20 61 72 65 20 74 68 65 20 63 6f 6e  ives are the con
148a3 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 65 73  structor and des
148a4 74 72 75 63 74 6f 72 2c 0a 2a 2a 20 6f 62 76 69  tructor,.** obvi
148a5 6f 75 73 6c 79 2e 20 20 54 68 65 20 49 4e 53 45  ously.  The INSE
148a6 52 54 20 70 72 69 6d 69 74 69 76 65 20 61 64 64  RT primitive add
148a7 73 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20  s a new element 
148a8 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a  to the RowSet..*
148a9 2a 20 54 45 53 54 20 63 68 65 63 6b 73 20 74 6f  * TEST checks to
148aa 20 73 65 65 20 69 66 20 61 6e 20 65 6c 65 6d 65   see if an eleme
148ab 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  nt is already in
148ac 20 74 68 65 20 52 6f 77 53 65 74 2e 20 20 53 4d   the RowSet.  SM
148ad 41 4c 4c 45 53 54 0a 2a 2a 20 65 78 74 72 61 63  ALLEST.** extrac
148ae 74 73 20 74 68 65 20 6c 65 61 73 74 20 76 61 6c  ts the least val
148af 75 65 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 53  ue from the RowS
148b0 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 4e  et..**.** The IN
148b1 53 45 52 54 20 70 72 69 6d 69 74 69 76 65 20 6d  SERT primitive m
148b2 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 64  ight allocate ad
148b3 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 2e  ditional memory.
148b4 20 20 4d 65 6d 6f 72 79 20 69 73 0a 2a 2a 20 61    Memory is.** a
148b5 6c 6c 6f 63 61 74 65 64 20 69 6e 20 63 68 75 6e  llocated in chun
148b6 6b 73 20 73 6f 20 6d 6f 73 74 20 49 4e 53 45 52  ks so most INSER
148b7 54 73 20 64 6f 20 6e 6f 20 61 6c 6c 6f 63 61 74  Ts do no allocat
148b8 69 6f 6e 2e 20 20 54 68 65 72 65 20 69 73 20 61  ion.  There is a
148b9 6e 20 0a 2a 2a 20 75 70 70 65 72 20 62 6f 75 6e  n .** upper boun
148ba 64 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66  d on the size of
148bb 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72   allocated memor
148bc 79 2e 20 20 4e 6f 20 6d 65 6d 6f 72 79 20 69 73  y.  No memory is
148bd 20 66 72 65 65 64 0a 2a 2a 20 75 6e 74 69 6c 20   freed.** until 
148be 44 45 53 54 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20 54  DESTROY..**.** T
148bf 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69 76  he TEST primitiv
148c0 65 20 69 6e 63 6c 75 64 65 73 20 61 20 22 62 61  e includes a "ba
148c1 74 63 68 22 20 6e 75 6d 62 65 72 2e 20 20 54 68  tch" number.  Th
148c2 65 20 54 45 53 54 20 70 72 69 6d 69 74 69 76 65  e TEST primitive
148c3 0a 2a 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 73 65  .** will only se
148c4 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20  e elements that 
148c5 77 65 72 65 20 69 6e 73 65 72 74 65 64 20 62 65  were inserted be
148c6 66 6f 72 65 20 74 68 65 20 6c 61 73 74 20 63 68  fore the last ch
148c7 61 6e 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 62  ange.** in the b
148c8 61 74 63 68 20 6e 75 6d 62 65 72 2e 20 20 49 6e  atch number.  In
148c9 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66   other words, if
148ca 20 61 6e 20 49 4e 53 45 52 54 20 6f 63 63 75 72   an INSERT occur
148cb 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 77 6f  s between.** two
148cc 20 54 45 53 54 73 20 77 68 65 72 65 20 74 68 65   TESTs where the
148cd 20 54 45 53 54 73 20 68 61 76 65 20 74 68 65 20   TESTs have the 
148ce 73 61 6d 65 20 62 61 74 63 68 20 6e 75 62 6d 65  same batch nubme
148cf 72 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 76  r, then the.** v
148d0 61 6c 75 65 20 61 64 64 65 64 20 62 79 20 74 68  alue added by th
148d1 65 20 49 4e 53 45 52 54 20 77 69 6c 6c 20 6e 6f  e INSERT will no
148d2 74 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f 20  t be visible to 
148d3 74 68 65 20 73 65 63 6f 6e 64 20 54 45 53 54 2e  the second TEST.
148d4 0a 2a 2a 20 54 68 65 20 69 6e 69 74 69 61 6c 20  .** The initial 
148d5 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20  batch number is 
148d6 7a 65 72 6f 2c 20 73 6f 20 69 66 20 74 68 65 20  zero, so if the 
148d7 76 65 72 79 20 66 69 72 73 74 20 54 45 53 54 20  very first TEST 
148d8 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 6e 6f  contains.** a no
148d9 6e 2d 7a 65 72 6f 20 62 61 74 63 68 20 6e 75 6d  n-zero batch num
148da 62 65 72 2c 20 69 74 20 77 69 6c 6c 20 73 65 65  ber, it will see
148db 20 61 6c 6c 20 70 72 69 6f 72 20 49 4e 53 45 52   all prior INSER
148dc 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e 53  Ts..**.** No INS
148dd 45 52 54 73 20 6d 61 79 20 6f 63 63 75 72 73 20  ERTs may occurs 
148de 61 66 74 65 72 20 61 20 53 4d 41 4c 4c 45 53 54  after a SMALLEST
148df 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20  .  An assertion 
148e0 77 69 6c 6c 20 66 61 69 6c 20 69 66 0a 2a 2a 20  will fail if.** 
148e1 74 68 61 74 20 69 73 20 61 74 74 65 6d 70 74 65  that is attempte
148e2 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 73  d..**.** The cos
148e3 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 69  t of an INSERT i
148e4 73 20 72 6f 75 67 68 6c 79 20 63 6f 6e 73 74 61  s roughly consta
148e5 6e 74 2e 20 20 28 53 6f 6d 65 74 69 6d 65 20 6e  nt.  (Sometime n
148e6 65 77 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 61 73  ew memory.** has
148e7 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64   to be allocated
148e8 20 6f 6e 20 61 6e 20 49 4e 53 45 52 54 2e 29 20   on an INSERT.) 
148e9 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54   The cost of a T
148ea 45 53 54 20 77 69 74 68 20 61 20 6e 65 77 0a 2a  EST with a new.*
148eb 2a 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69  * batch number i
148ec 73 20 4f 28 4e 6c 6f 67 4e 29 20 77 68 65 72 65  s O(NlogN) where
148ed 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72   N is the number
148ee 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20   of elements in 
148ef 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 54  the RowSet..** T
148f0 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54 45 53  he cost of a TES
148f1 54 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65  T using the same
148f2 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73   batch number is
148f3 20 4f 28 6c 6f 67 4e 29 2e 20 20 54 68 65 20 63   O(logN).  The c
148f4 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69  ost.** of the fi
148f5 72 73 74 20 53 4d 41 4c 4c 45 53 54 20 69 73 20  rst SMALLEST is 
148f6 4f 28 4e 6c 6f 67 4e 29 2e 20 20 53 65 63 6f 6e  O(NlogN).  Secon
148f7 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74  d and subsequent
148f8 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 70 72 69   SMALLEST.** pri
148f9 6d 69 74 69 76 65 73 20 61 72 65 20 63 6f 6e 73  mitives are cons
148fa 74 61 6e 74 20 74 69 6d 65 2e 20 20 54 68 65 20  tant time.  The 
148fb 63 6f 73 74 20 6f 66 20 44 45 53 54 52 4f 59 20  cost of DESTROY 
148fc 69 73 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20 54  is O(N)..**.** T
148fd 68 65 72 65 20 69 73 20 61 6e 20 61 64 64 65 64  here is an added
148fe 20 63 6f 73 74 20 6f 66 20 4f 28 4e 29 20 77 68   cost of O(N) wh
148ff 65 6e 20 73 77 69 74 63 68 69 6e 67 20 62 65 74  en switching bet
14900 77 65 65 6e 20 54 45 53 54 20 61 6e 64 0a 2a 2a  ween TEST and.**
14901 20 53 4d 41 4c 4c 45 53 54 20 70 72 69 6d 69 74   SMALLEST primit
14902 69 76 65 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  ives..*/.../*.**
14903 20 54 61 72 67 65 74 20 73 69 7a 65 20 66 6f 72   Target size for
14904 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e   allocation chun
14905 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52  ks..*/.#define R
14906 4f 57 53 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e  OWSET_ALLOCATION
14907 5f 53 49 5a 45 20 31 30 32 34 0a 0a 2f 2a 0a 2a  _SIZE 1024../*.*
14908 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * The number of 
14909 72 6f 77 73 65 74 20 65 6e 74 72 69 65 73 20 70  rowset entries p
1490a 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 68  er allocation ch
1490b 75 6e 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  unk..*/.#define 
1490c 52 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52  ROWSET_ENTRY_PER
1490d 5f 43 48 55 4e 4b 20 20 5c 0a 20 20 20 20 20 20  _CHUNK  \.      
1490e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1490f 20 28 28 52 4f 57 53 45 54 5f 41 4c 4c 4f 43 41   ((ROWSET_ALLOCA
14910 54 49 4f 4e 5f 53 49 5a 45 2d 38 29 2f 73 69 7a  TION_SIZE-8)/siz
14911 65 6f 66 28 73 74 72 75 63 74 20 52 6f 77 53 65  eof(struct RowSe
14912 74 45 6e 74 72 79 29 29 0a 0a 2f 2a 0a 2a 2a 20  tEntry))../*.** 
14913 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 61 20  Each entry in a 
14914 52 6f 77 53 65 74 20 69 73 20 61 6e 20 69 6e 73  RowSet is an ins
14915 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
14916 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 74 2e 0a 2a  lowing object..*
14917 2a 0a 2a 2a 20 54 68 69 73 20 73 61 6d 65 20 6f  *.** This same o
14918 62 6a 65 63 74 20 69 73 20 72 65 75 73 65 64 20  bject is reused 
14919 74 6f 20 73 74 6f 72 65 20 61 20 6c 69 6e 6b 65  to store a linke
1491a 64 20 6c 69 73 74 20 6f 66 20 74 72 65 65 73 20  d list of trees 
1491b 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 79 0a 2a  of RowSetEntry.*
1491c 2a 20 6f 62 6a 65 63 74 73 2e 20 20 49 6e 20 74  * objects.  In t
1491d 68 61 74 20 61 6c 74 65 72 6e 61 74 69 76 65 20  hat alternative 
1491e 75 73 65 2c 20 70 52 69 67 68 74 20 70 6f 69 6e  use, pRight poin
1491f 74 73 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65  ts to the next e
14920 6e 74 72 79 0a 2a 2a 20 69 6e 20 74 68 65 20 6c  ntry.** in the l
14921 69 73 74 2c 20 70 4c 65 66 74 20 70 6f 69 6e 74  ist, pLeft point
14922 73 20 74 6f 20 74 68 65 20 74 72 65 65 2c 20 61  s to the tree, a
14923 6e 64 20 76 20 69 73 20 75 6e 75 73 65 64 2e 20  nd v is unused. 
14924 20 54 68 65 0a 2a 2a 20 52 6f 77 53 65 74 2e 70   The.** RowSet.p
14925 46 6f 72 65 73 74 20 76 61 6c 75 65 20 70 6f 69  Forest value poi
14926 6e 74 73 20 74 6f 20 74 68 65 20 68 65 61 64 20  nts to the head 
14927 6f 66 20 74 68 69 73 20 66 6f 72 65 73 74 20 6c  of this forest l
14928 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52  ist..*/.struct R
14929 6f 77 53 65 74 45 6e 74 72 79 20 7b 20 20 20 20  owSetEntry {    
1492a 20 20 20 20 20 20 20 20 0a 20 20 69 36 34 20 76          .  i64 v
1492b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1492c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 4f 57 49           /* ROWI
1492d 44 20 76 61 6c 75 65 20 66 6f 72 20 74 68 69 73  D value for this
1492e 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75   entry */.  stru
1492f 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
14930 70 52 69 67 68 74 3b 20 20 20 2f 2a 20 52 69 67  pRight;   /* Rig
14931 68 74 20 73 75 62 74 72 65 65 20 28 6c 61 72 67  ht subtree (larg
14932 65 72 20 65 6e 74 72 69 65 73 29 20 6f 72 20 6c  er entries) or l
14933 69 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ist */.  struct 
14934 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65  RowSetEntry *pLe
14935 66 74 3b 20 20 20 20 2f 2a 20 4c 65 66 74 20 73  ft;    /* Left s
14936 75 62 74 72 65 65 20 28 73 6d 61 6c 6c 65 72 20  ubtree (smaller 
14937 65 6e 74 72 69 65 73 29 20 2a 2f 0a 7d 3b 0a 0a  entries) */.};..
14938 2f 2a 0a 2a 2a 20 52 6f 77 53 65 74 45 6e 74 72  /*.** RowSetEntr
14939 79 20 6f 62 6a 65 63 74 73 20 61 72 65 20 61 6c  y objects are al
1493a 6c 6f 63 61 74 65 64 20 69 6e 20 6c 61 72 67 65  located in large
1493b 20 63 68 75 6e 6b 73 20 28 69 6e 73 74 61 6e 63   chunks (instanc
1493c 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c  es of the.** fol
1493d 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
1493e 29 20 74 6f 20 72 65 64 75 63 65 20 6d 65 6d 6f  ) to reduce memo
1493f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 76  ry allocation ov
14940 65 72 68 65 61 64 2e 20 20 54 68 65 0a 2a 2a 20  erhead.  The.** 
14941 63 68 75 6e 6b 73 20 61 72 65 20 6b 65 70 74 20  chunks are kept 
14942 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  on a linked list
14943 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 63 61   so that they ca
14944 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64  n be deallocated
14945 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 52 6f 77  .** when the Row
14946 53 65 74 20 69 73 20 64 65 73 74 72 6f 79 65 64  Set is destroyed
14947 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53  ..*/.struct RowS
14948 65 74 43 68 75 6e 6b 20 7b 0a 20 20 73 74 72 75  etChunk {.  stru
14949 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a  ct RowSetChunk *
1494a 70 4e 65 78 74 43 68 75 6e 6b 3b 20 20 20 20 20  pNextChunk;     
1494b 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b     /* Next chunk
1494c 20 6f 6e 20 6c 69 73 74 20 6f 66 20 74 68 65 6d   on list of them
1494d 20 61 6c 6c 20 2a 2f 0a 20 20 73 74 72 75 63 74   all */.  struct
1494e 20 52 6f 77 53 65 74 45 6e 74 72 79 20 61 45 6e   RowSetEntry aEn
1494f 74 72 79 5b 52 4f 57 53 45 54 5f 45 4e 54 52 59  try[ROWSET_ENTRY
14950 5f 50 45 52 5f 43 48 55 4e 4b 5d 3b 20 2f 2a 20  _PER_CHUNK]; /* 
14951 41 6c 6c 6f 63 61 74 65 64 20 65 6e 74 72 69 65  Allocated entrie
14952 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  s */.};../*.** A
14953 20 52 6f 77 53 65 74 20 69 6e 20 61 6e 20 69 6e   RowSet in an in
14954 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
14955 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
14956 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 79 70 65 64  e..**.** A typed
14957 65 66 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ef of this struc
14958 74 75 72 65 20 69 66 20 66 6f 75 6e 64 20 69 6e  ture if found in
14959 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2f   sqliteInt.h..*/
1495a 0a 73 74 72 75 63 74 20 52 6f 77 53 65 74 20 7b  .struct RowSet {
1495b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
1495c 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 20 20  Chunk *pChunk;  
1495d 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c    /* List of all
1495e 20 63 68 75 6e 6b 20 61 6c 6c 6f 63 61 74 69 6f   chunk allocatio
1495f 6e 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  ns */.  sqlite3 
14960 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  *db;            
14961 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
14962 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14963 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f  n */.  struct Ro
14964 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72  wSetEntry *pEntr
14965 79 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  y;    /* List of
14966 20 65 6e 74 72 69 65 73 20 75 73 69 6e 67 20 70   entries using p
14967 52 69 67 68 74 20 2a 2f 0a 20 20 73 74 72 75 63  Right */.  struc
14968 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
14969 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 73  Last;     /* Las
1496a 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 70  t entry on the p
1496b 45 6e 74 72 79 20 6c 69 73 74 20 2a 2f 0a 20 20  Entry list */.  
1496c 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
1496d 72 79 20 2a 70 46 72 65 73 68 3b 20 20 20 20 2f  ry *pFresh;    /
1496e 2a 20 53 6f 75 72 63 65 20 6f 66 20 6e 65 77 20  * Source of new 
1496f 65 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 2a 2f  entry objects */
14970 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
14971 45 6e 74 72 79 20 2a 70 46 6f 72 65 73 74 3b 20  Entry *pForest; 
14972 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 62 69 6e    /* List of bin
14973 61 72 79 20 74 72 65 65 73 20 6f 66 20 65 6e 74  ary trees of ent
14974 72 69 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 46  ries */.  u16 nF
14975 72 65 73 68 3b 20 20 20 20 20 20 20 20 20 20 20  resh;           
14976 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
14977 65 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 6f 6e  er of objects on
14978 20 70 46 72 65 73 68 20 2a 2f 0a 20 20 75 38 20   pFresh */.  u8 
14979 72 73 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  rsFlags;        
1497a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
1497b 61 72 69 6f 75 73 20 66 6c 61 67 73 20 2a 2f 0a  arious flags */.
1497c 20 20 75 38 20 69 42 61 74 63 68 3b 20 20 20 20    u8 iBatch;    
1497d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1497e 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 73 65   /* Current inse
1497f 72 74 20 62 61 74 63 68 20 2a 2f 0a 7d 3b 0a 0a  rt batch */.};..
14980 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
14981 6c 75 65 73 20 66 6f 72 20 52 6f 77 53 65 74 2e  lues for RowSet.
14982 72 73 46 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69  rsFlags.*/.#defi
14983 6e 65 20 52 4f 57 53 45 54 5f 53 4f 52 54 45 44  ne ROWSET_SORTED
14984 20 20 30 78 30 31 20 20 20 2f 2a 20 54 72 75 65    0x01   /* True
14985 20 69 66 20 52 6f 77 53 65 74 2e 70 45 6e 74 72   if RowSet.pEntr
14986 79 20 69 73 20 73 6f 72 74 65 64 20 2a 2f 0a 23  y is sorted */.#
14987 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 4e 45  define ROWSET_NE
14988 58 54 20 20 20 20 30 78 30 32 20 20 20 2f 2a 20  XT    0x02   /* 
14989 54 72 75 65 20 69 66 20 73 71 6c 69 74 65 33 52  True if sqlite3R
1498a 6f 77 53 65 74 4e 65 78 74 28 29 20 68 61 73 20  owSetNext() has 
1498b 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a 0a  been called */..
1498c 2f 2a 0a 2a 2a 20 54 75 72 6e 20 62 75 6c 6b 20  /*.** Turn bulk 
1498d 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 52 6f  memory into a Ro
1498e 77 53 65 74 20 6f 62 6a 65 63 74 2e 20 20 4e 20  wSet object.  N 
1498f 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a  bytes of memory.
14990 2a 2a 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  ** are available
14991 20 61 74 20 70 53 70 61 63 65 2e 20 20 54 68 65   at pSpace.  The
14992 20 64 62 20 70 6f 69 6e 74 65 72 20 69 73 20 75   db pointer is u
14993 73 65 64 20 61 73 20 61 20 6d 65 6d 6f 72 79 20  sed as a memory 
14994 63 6f 6e 74 65 78 74 0a 2a 2a 20 66 6f 72 20 61  context.** for a
14995 6e 79 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c  ny subsequent al
14996 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6e  locations that n
14997 65 65 64 20 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a  eed to occur..**
14998 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
14999 72 20 74 6f 20 74 68 65 20 6e 65 77 20 52 6f 77  r to the new Row
1499a 53 65 74 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  Set object..**.*
1499b 2a 20 49 74 20 6d 75 73 74 20 62 65 20 74 68 65  * It must be the
1499c 20 63 61 73 65 20 74 68 61 74 20 4e 20 69 73 20   case that N is 
1499d 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 6d 61  sufficient to ma
1499e 6b 65 20 61 20 52 6f 77 73 65 74 2e 20 20 49 66  ke a Rowset.  If
1499f 20 6e 6f 74 0a 2a 2a 20 61 6e 20 61 73 73 65 72   not.** an asser
149a0 74 69 6f 6e 20 66 61 75 6c 74 20 6f 63 63 75 72  tion fault occur
149a1 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 4e 20 69  s..** .** If N i
149a2 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  s larger than th
149a3 65 20 6d 69 6e 69 6d 75 6d 2c 20 75 73 65 20 74  e minimum, use t
149a4 68 65 20 73 75 72 70 6c 75 73 20 61 73 20 61 6e  he surplus as an
149a5 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 61 6c 6c 6f   initial.** allo
149a6 63 61 74 69 6f 6e 20 6f 66 20 65 6e 74 72 69 65  cation of entrie
149a7 73 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 62  s available to b
149a8 65 20 66 69 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c  e filled..*/.SQL
149a9 49 54 45 5f 50 52 49 56 41 54 45 20 52 6f 77 53  ITE_PRIVATE RowS
149aa 65 74 20 2a 73 71 6c 69 74 65 33 52 6f 77 53 65  et *sqlite3RowSe
149ab 74 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64  tInit(sqlite3 *d
149ac 62 2c 20 76 6f 69 64 20 2a 70 53 70 61 63 65 2c  b, void *pSpace,
149ad 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 4e 29   unsigned int N)
149ae 7b 0a 20 20 52 6f 77 53 65 74 20 2a 70 3b 0a 20  {.  RowSet *p;. 
149af 20 61 73 73 65 72 74 28 20 4e 20 3e 3d 20 52 4f   assert( N >= RO
149b0 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 29 29  UND8(sizeof(*p))
149b1 20 29 3b 0a 20 20 70 20 3d 20 70 53 70 61 63 65   );.  p = pSpace
149b2 3b 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20  ;.  p->pChunk = 
149b3 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b  0;.  p->db = db;
149b4 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30  .  p->pEntry = 0
149b5 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30  ;.  p->pLast = 0
149b6 3b 0a 20 20 70 2d 3e 70 46 6f 72 65 73 74 20 3d  ;.  p->pForest =
149b7 20 30 3b 0a 20 20 70 2d 3e 70 46 72 65 73 68 20   0;.  p->pFresh 
149b8 3d 20 28 73 74 72 75 63 74 20 52 6f 77 53 65 74  = (struct RowSet
149b9 45 6e 74 72 79 2a 29 28 52 4f 55 4e 44 38 28 73  Entry*)(ROUND8(s
149ba 69 7a 65 6f 66 28 2a 70 29 29 20 2b 20 28 63 68  izeof(*p)) + (ch
149bb 61 72 2a 29 70 29 3b 0a 20 20 70 2d 3e 6e 46 72  ar*)p);.  p->nFr
149bc 65 73 68 20 3d 20 28 75 31 36 29 28 28 4e 20 2d  esh = (u16)((N -
149bd 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a   ROUND8(sizeof(*
149be 70 29 29 29 2f 73 69 7a 65 6f 66 28 73 74 72 75  p)))/sizeof(stru
149bf 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 29 29  ct RowSetEntry))
149c0 3b 0a 20 20 70 2d 3e 72 73 46 6c 61 67 73 20 3d  ;.  p->rsFlags =
149c1 20 52 4f 57 53 45 54 5f 53 4f 52 54 45 44 3b 0a   ROWSET_SORTED;.
149c2 20 20 70 2d 3e 69 42 61 74 63 68 20 3d 20 30 3b    p->iBatch = 0;
149c3 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
149c4 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65  /*.** Deallocate
149c5 20 61 6c 6c 20 63 68 75 6e 6b 73 20 66 72 6f 6d   all chunks from
149c6 20 61 20 52 6f 77 53 65 74 2e 20 20 54 68 69 73   a RowSet.  This
149c7 20 66 72 65 65 73 20 61 6c 6c 20 6d 65 6d 6f 72   frees all memor
149c8 79 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 52 6f  y that.** the Ro
149c9 77 53 65 74 20 68 61 73 20 61 6c 6c 6f 63 61 74  wSet has allocat
149ca 65 64 20 6f 76 65 72 20 69 74 73 20 6c 69 66 65  ed over its life
149cb 74 69 6d 65 2e 20 20 54 68 69 73 20 72 6f 75 74  time.  This rout
149cc 69 6e 65 20 69 73 0a 2a 2a 20 74 68 65 20 64 65  ine is.** the de
149cd 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65  structor for the
149ce 20 52 6f 77 53 65 74 2e 0a 2a 2f 0a 53 51 4c 49   RowSet..*/.SQLI
149cf 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
149d0 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65  sqlite3RowSetCle
149d1 61 72 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20  ar(RowSet *p){. 
149d2 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68   struct RowSetCh
149d3 75 6e 6b 20 2a 70 43 68 75 6e 6b 2c 20 2a 70 4e  unk *pChunk, *pN
149d4 65 78 74 43 68 75 6e 6b 3b 0a 20 20 66 6f 72 28  extChunk;.  for(
149d5 70 43 68 75 6e 6b 3d 70 2d 3e 70 43 68 75 6e 6b  pChunk=p->pChunk
149d6 3b 20 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b  ; pChunk; pChunk
149d7 20 3d 20 70 4e 65 78 74 43 68 75 6e 6b 29 7b 0a   = pNextChunk){.
149d8 20 20 20 20 70 4e 65 78 74 43 68 75 6e 6b 20 3d      pNextChunk =
149d9 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 43 68   pChunk->pNextCh
149da 75 6e 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  unk;.    sqlite3
149db 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 43  DbFree(p->db, pC
149dc 68 75 6e 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e  hunk);.  }.  p->
149dd 70 43 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d  pChunk = 0;.  p-
149de 3e 6e 46 72 65 73 68 20 3d 20 30 3b 0a 20 20 70  >nFresh = 0;.  p
149df 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20  ->pEntry = 0;.  
149e0 70 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20  p->pLast = 0;.  
149e1 70 2d 3e 70 46 6f 72 65 73 74 20 3d 20 30 3b 0a  p->pForest = 0;.
149e2 20 20 70 2d 3e 72 73 46 6c 61 67 73 20 3d 20 52    p->rsFlags = R
149e3 4f 57 53 45 54 5f 53 4f 52 54 45 44 3b 0a 7d 0a  OWSET_SORTED;.}.
149e4 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
149e5 61 20 6e 65 77 20 52 6f 77 53 65 74 45 6e 74 72  a new RowSetEntr
149e6 79 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 73  y object that is
149e7 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
149e8 20 74 68 65 0a 2a 2a 20 67 69 76 65 6e 20 52 6f   the.** given Ro
149e9 77 53 65 74 2e 20 20 52 65 74 75 72 6e 20 61 20  wSet.  Return a 
149ea 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
149eb 65 77 20 61 6e 64 20 63 6f 6d 70 6c 65 74 65 6c  ew and completel
149ec 79 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 0a  y uninitialized.
149ed 2a 2a 20 6f 62 6a 65 63 74 65 64 2e 0a 2a 2a 0a  ** objected..**.
149ee 2a 2a 20 49 6e 20 61 6e 20 4f 4f 4d 20 73 69 74  ** In an OOM sit
149ef 75 61 74 69 6f 6e 2c 20 74 68 65 20 52 6f 77 53  uation, the RowS
149f0 65 74 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  et.db->mallocFai
149f1 6c 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20  led flag is set 
149f2 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74  and this.** rout
149f3 69 6e 65 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c  ine returns NULL
149f4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  ..*/.static stru
149f5 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
149f6 72 6f 77 53 65 74 45 6e 74 72 79 41 6c 6c 6f 63  rowSetEntryAlloc
149f7 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 61  (RowSet *p){.  a
149f8 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
149f9 20 69 66 28 20 70 2d 3e 6e 46 72 65 73 68 3d 3d   if( p->nFresh==
149fa 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  0 ){.    struct 
149fb 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e 65  RowSetChunk *pNe
149fc 77 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71  w;.    pNew = sq
149fd 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
149fe 28 70 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a  (p->db, sizeof(*
149ff 70 4e 65 77 29 29 3b 0a 20 20 20 20 69 66 28 20  pNew));.    if( 
14a00 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pNew==0 ){.     
14a01 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
14a02 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74  .    pNew->pNext
14a03 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 43 68 75 6e  Chunk = p->pChun
14a04 6b 3b 0a 20 20 20 20 70 2d 3e 70 43 68 75 6e 6b  k;.    p->pChunk
14a05 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e   = pNew;.    p->
14a06 70 46 72 65 73 68 20 3d 20 70 4e 65 77 2d 3e 61  pFresh = pNew->a
14a07 45 6e 74 72 79 3b 0a 20 20 20 20 70 2d 3e 6e 46  Entry;.    p->nF
14a08 72 65 73 68 20 3d 20 52 4f 57 53 45 54 5f 45 4e  resh = ROWSET_EN
14a09 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 3b 0a 20  TRY_PER_CHUNK;. 
14a0a 20 7d 0a 20 20 70 2d 3e 6e 46 72 65 73 68 2d 2d   }.  p->nFresh--
14a0b 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 46  ;.  return p->pF
14a0c 72 65 73 68 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  resh++;.}../*.**
14a0d 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20 76 61   Insert a new va
14a0e 6c 75 65 20 69 6e 74 6f 20 61 20 52 6f 77 53 65  lue into a RowSe
14a0f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 6c  t..**.** The mal
14a10 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 6f  locFailed flag o
14a11 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  f the database c
14a12 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 73 65 74  onnection is set
14a13 20 69 66 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20   if a.** memory 
14a14 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73  allocation fails
14a15 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14a16 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
14a17 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f 77  RowSetInsert(Row
14a18 53 65 74 20 2a 70 2c 20 69 36 34 20 72 6f 77 69  Set *p, i64 rowi
14a19 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77  d){.  struct Row
14a1a 53 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79  SetEntry *pEntry
14a1b 3b 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 65 6e  ;  /* The new en
14a1c 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  try */.  struct 
14a1d 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 61  RowSetEntry *pLa
14a1e 73 74 3b 20 20 20 2f 2a 20 54 68 65 20 6c 61 73  st;   /* The las
14a1f 74 20 70 72 69 6f 72 20 65 6e 74 72 79 20 2a 2f  t prior entry */
14a20 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74  ..  /* This rout
14a21 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c  ine is never cal
14a22 6c 65 64 20 61 66 74 65 72 20 73 71 6c 69 74 65  led after sqlite
14a23 33 52 6f 77 53 65 74 4e 65 78 74 28 29 20 2a 2f  3RowSetNext() */
14a24 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20  .  assert( p!=0 
14a25 26 26 20 28 70 2d 3e 72 73 46 6c 61 67 73 20 26  && (p->rsFlags &
14a26 20 52 4f 57 53 45 54 5f 4e 45 58 54 29 3d 3d 30   ROWSET_NEXT)==0
14a27 20 29 3b 0a 0a 20 20 70 45 6e 74 72 79 20 3d 20   );..  pEntry = 
14a28 72 6f 77 53 65 74 45 6e 74 72 79 41 6c 6c 6f 63  rowSetEntryAlloc
14a29 28 70 29 3b 0a 20 20 69 66 28 20 70 45 6e 74 72  (p);.  if( pEntr
14a2a 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  y==0 ) return;. 
14a2b 20 70 45 6e 74 72 79 2d 3e 76 20 3d 20 72 6f 77   pEntry->v = row
14a2c 69 64 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 70 52  id;.  pEntry->pR
14a2d 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 4c 61 73  ight = 0;.  pLas
14a2e 74 20 3d 20 70 2d 3e 70 4c 61 73 74 3b 0a 20 20  t = p->pLast;.  
14a2f 69 66 28 20 70 4c 61 73 74 20 29 7b 0a 20 20 20  if( pLast ){.   
14a30 20 69 66 28 20 28 70 2d 3e 72 73 46 6c 61 67 73   if( (p->rsFlags
14a31 20 26 20 52 4f 57 53 45 54 5f 53 4f 52 54 45 44   & ROWSET_SORTED
14a32 29 21 3d 30 20 26 26 20 72 6f 77 69 64 3c 3d 70  )!=0 && rowid<=p
14a33 4c 61 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20 20  Last->v ){.     
14a34 20 70 2d 3e 72 73 46 6c 61 67 73 20 26 3d 20 7e   p->rsFlags &= ~
14a35 52 4f 57 53 45 54 5f 53 4f 52 54 45 44 3b 0a 20  ROWSET_SORTED;. 
14a36 20 20 20 7d 0a 20 20 20 20 70 4c 61 73 74 2d 3e     }.    pLast->
14a37 70 52 69 67 68 74 20 3d 20 70 45 6e 74 72 79 3b  pRight = pEntry;
14a38 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d  .  }else{.    p-
14a39 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79  >pEntry = pEntry
14a3a 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 61 73 74  ;.  }.  p->pLast
14a3b 20 3d 20 70 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a   = pEntry;.}../*
14a3c 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c 69  .** Merge two li
14a3d 73 74 73 20 6f 66 20 52 6f 77 53 65 74 45 6e 74  sts of RowSetEnt
14a3e 72 79 20 6f 62 6a 65 63 74 73 2e 20 20 52 65 6d  ry objects.  Rem
14a3f 6f 76 65 20 64 75 70 6c 69 63 61 74 65 73 2e 0a  ove duplicates..
14a40 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20  **.** The input 
14a41 6c 69 73 74 73 20 61 72 65 20 63 6f 6e 6e 65 63  lists are connec
14a42 74 65 64 20 76 69 61 20 70 52 69 67 68 74 20 70  ted via pRight p
14a43 6f 69 6e 74 65 72 73 20 61 6e 64 20 61 72 65 20  ointers and are 
14a44 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 6f 20 65  .** assumed to e
14a45 61 63 68 20 61 6c 72 65 61 64 79 20 62 65 20 69  ach already be i
14a46 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a  n sorted order..
14a47 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  */.static struct
14a48 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f   RowSetEntry *ro
14a49 77 53 65 74 45 6e 74 72 79 4d 65 72 67 65 28 0a  wSetEntryMerge(.
14a4a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
14a4b 6e 74 72 79 20 2a 70 41 2c 20 20 20 20 2f 2a 20  ntry *pA,    /* 
14a4c 46 69 72 73 74 20 73 6f 72 74 65 64 20 6c 69 73  First sorted lis
14a4d 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a  t to be merged *
14a4e 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  /.  struct RowSe
14a4f 74 45 6e 74 72 79 20 2a 70 42 20 20 20 20 20 2f  tEntry *pB     /
14a50 2a 20 53 65 63 6f 6e 64 20 73 6f 72 74 65 64 20  * Second sorted 
14a51 6c 69 73 74 20 74 6f 20 62 65 20 6d 65 72 67 65  list to be merge
14a52 64 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74  d */.){.  struct
14a53 20 52 6f 77 53 65 74 45 6e 74 72 79 20 68 65 61   RowSetEntry hea
14a54 64 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  d;.  struct RowS
14a55 65 74 45 6e 74 72 79 20 2a 70 54 61 69 6c 3b 0a  etEntry *pTail;.
14a56 0a 20 20 70 54 61 69 6c 20 3d 20 26 68 65 61 64  .  pTail = &head
14a57 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26  ;.  while( pA &&
14a58 20 70 42 20 29 7b 0a 20 20 20 20 61 73 73 65 72   pB ){.    asser
14a59 74 28 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30  t( pA->pRight==0
14a5a 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70   || pA->v<=pA->p
14a5b 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20  Right->v );.    
14a5c 61 73 73 65 72 74 28 20 70 42 2d 3e 70 52 69 67  assert( pB->pRig
14a5d 68 74 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d  ht==0 || pB->v<=
14a5e 70 42 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b  pB->pRight->v );
14a5f 0a 20 20 20 20 69 66 28 20 70 41 2d 3e 76 3c 70  .    if( pA->v<p
14a60 42 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 54  B->v ){.      pT
14a61 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 41  ail->pRight = pA
14a62 3b 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d  ;.      pA = pA-
14a63 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 70  >pRight;.      p
14a64 54 61 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52  Tail = pTail->pR
14a65 69 67 68 74 3b 0a 20 20 20 20 7d 65 6c 73 65 20  ight;.    }else 
14a66 69 66 28 20 70 42 2d 3e 76 3c 70 41 2d 3e 76 20  if( pB->v<pA->v 
14a67 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e  ){.      pTail->
14a68 70 52 69 67 68 74 20 3d 20 70 42 3b 0a 20 20 20  pRight = pB;.   
14a69 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 52 69 67     pB = pB->pRig
14a6a 68 74 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20  ht;.      pTail 
14a6b 3d 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b  = pTail->pRight;
14a6c 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
14a6d 20 20 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68    pA = pA->pRigh
14a6e 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  t;.    }.  }.  i
14a6f 66 28 20 70 41 20 29 7b 0a 20 20 20 20 61 73 73  f( pA ){.    ass
14a70 65 72 74 28 20 70 41 2d 3e 70 52 69 67 68 74 3d  ert( pA->pRight=
14a71 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d  =0 || pA->v<=pA-
14a72 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20  >pRight->v );.  
14a73 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20    pTail->pRight 
14a74 3d 20 70 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  = pA;.  }else{. 
14a75 20 20 20 61 73 73 65 72 74 28 20 70 42 3d 3d 30     assert( pB==0
14a76 20 7c 7c 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d   || pB->pRight==
14a77 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e  0 || pB->v<=pB->
14a78 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20  pRight->v );.   
14a79 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d   pTail->pRight =
14a7a 20 70 42 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   pB;.  }.  retur
14a7b 6e 20 68 65 61 64 2e 70 52 69 67 68 74 3b 0a 7d  n head.pRight;.}
14a7c 0a 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 20 61 6c 6c  ../*.** Sort all
14a7d 20 65 6c 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65   elements on the
14a7e 20 6c 69 73 74 20 6f 66 20 52 6f 77 53 65 74 45   list of RowSetE
14a7f 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 69 6e 74  ntry objects int
14a80 6f 20 6f 72 64 65 72 20 6f 66 0a 2a 2a 20 69 6e  o order of.** in
14a81 63 72 65 61 73 69 6e 67 20 76 2e 0a 2a 2f 20 0a  creasing v..*/ .
14a82 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52 6f  static struct Ro
14a83 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 65  wSetEntry *rowSe
14a84 74 45 6e 74 72 79 53 6f 72 74 28 73 74 72 75 63  tEntrySort(struc
14a85 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
14a86 49 6e 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  In){.  unsigned 
14a87 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
14a88 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4e 65  RowSetEntry *pNe
14a89 78 74 2c 20 2a 61 42 75 63 6b 65 74 5b 34 30 5d  xt, *aBucket[40]
14a8a 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 61 42 75 63  ;..  memset(aBuc
14a8b 6b 65 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61  ket, 0, sizeof(a
14a8c 42 75 63 6b 65 74 29 29 3b 0a 20 20 77 68 69 6c  Bucket));.  whil
14a8d 65 28 20 70 49 6e 20 29 7b 0a 20 20 20 20 70 4e  e( pIn ){.    pN
14a8e 65 78 74 20 3d 20 70 49 6e 2d 3e 70 52 69 67 68  ext = pIn->pRigh
14a8f 74 3b 0a 20 20 20 20 70 49 6e 2d 3e 70 52 69 67  t;.    pIn->pRig
14a90 68 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28  ht = 0;.    for(
14a91 69 3d 30 3b 20 61 42 75 63 6b 65 74 5b 69 5d 3b  i=0; aBucket[i];
14a92 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 49 6e   i++){.      pIn
14a93 20 3d 20 72 6f 77 53 65 74 45 6e 74 72 79 4d 65   = rowSetEntryMe
14a94 72 67 65 28 61 42 75 63 6b 65 74 5b 69 5d 2c 20  rge(aBucket[i], 
14a95 70 49 6e 29 3b 0a 20 20 20 20 20 20 61 42 75 63  pIn);.      aBuc
14a96 6b 65 74 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20  ket[i] = 0;.    
14a97 7d 0a 20 20 20 20 61 42 75 63 6b 65 74 5b 69 5d  }.    aBucket[i]
14a98 20 3d 20 70 49 6e 3b 0a 20 20 20 20 70 49 6e 20   = pIn;.    pIn 
14a99 3d 20 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70  = pNext;.  }.  p
14a9a 49 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d  In = 0;.  for(i=
14a9b 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42 75 63  0; i<sizeof(aBuc
14a9c 6b 65 74 29 2f 73 69 7a 65 6f 66 28 61 42 75 63  ket)/sizeof(aBuc
14a9d 6b 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20  ket[0]); i++){. 
14a9e 20 20 20 70 49 6e 20 3d 20 72 6f 77 53 65 74 45     pIn = rowSetE
14a9f 6e 74 72 79 4d 65 72 67 65 28 70 49 6e 2c 20 61  ntryMerge(pIn, a
14aa0 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 20 7d 0a  Bucket[i]);.  }.
14aa1 20 20 72 65 74 75 72 6e 20 70 49 6e 3b 0a 7d 0a    return pIn;.}.
14aa2 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75  ../*.** The inpu
14aa3 74 2c 20 70 49 6e 2c 20 69 73 20 61 20 62 69 6e  t, pIn, is a bin
14aa4 61 72 79 20 74 72 65 65 20 28 6f 72 20 73 75 62  ary tree (or sub
14aa5 74 72 65 65 29 20 6f 66 20 52 6f 77 53 65 74 45  tree) of RowSetE
14aa6 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a  ntry objects..**
14aa7 20 43 6f 6e 76 65 72 74 20 74 68 69 73 20 74 72   Convert this tr
14aa8 65 65 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 64  ee into a linked
14aa9 20 6c 69 73 74 20 63 6f 6e 6e 65 63 74 65 64 20   list connected 
14aaa 62 79 20 74 68 65 20 70 52 69 67 68 74 20 70 6f  by the pRight po
14aab 69 6e 74 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65  inters.** and re
14aac 74 75 72 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f  turn pointers to
14aad 20 74 68 65 20 66 69 72 73 74 20 61 6e 64 20 6c   the first and l
14aae 61 73 74 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  ast elements of 
14aaf 74 68 65 20 6e 65 77 20 6c 69 73 74 2e 0a 2a 2f  the new list..*/
14ab0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77  .static void row
14ab1 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 0a 20  SetTreeToList(. 
14ab2 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
14ab3 74 72 79 20 2a 70 49 6e 2c 20 20 20 20 20 20 20  try *pIn,       
14ab4 20 20 2f 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65    /* Root of the
14ab5 20 69 6e 70 75 74 20 74 72 65 65 20 2a 2f 0a 20   input tree */. 
14ab6 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
14ab7 74 72 79 20 2a 2a 70 70 46 69 72 73 74 2c 20 20  try **ppFirst,  
14ab8 20 20 2f 2a 20 57 72 69 74 65 20 68 65 61 64 20    /* Write head 
14ab9 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 6c 69  of the output li
14aba 73 74 20 68 65 72 65 20 2a 2f 0a 20 20 73 74 72  st here */.  str
14abb 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
14abc 2a 2a 70 70 4c 61 73 74 20 20 20 20 20 20 2f 2a  **ppLast      /*
14abd 20 57 72 69 74 65 20 74 61 69 6c 20 6f 66 20 74   Write tail of t
14abe 68 65 20 6f 75 74 70 75 74 20 6c 69 73 74 20 68  he output list h
14abf 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65  ere */.){.  asse
14ac0 72 74 28 20 70 49 6e 21 3d 30 20 29 3b 0a 20 20  rt( pIn!=0 );.  
14ac1 69 66 28 20 70 49 6e 2d 3e 70 4c 65 66 74 20 29  if( pIn->pLeft )
14ac2 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52 6f 77  {.    struct Row
14ac3 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20 20  SetEntry *p;.   
14ac4 20 72 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73   rowSetTreeToLis
14ac5 74 28 70 49 6e 2d 3e 70 4c 65 66 74 2c 20 70 70  t(pIn->pLeft, pp
14ac6 46 69 72 73 74 2c 20 26 70 29 3b 0a 20 20 20 20  First, &p);.    
14ac7 70 2d 3e 70 52 69 67 68 74 20 3d 20 70 49 6e 3b  p->pRight = pIn;
14ac8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70  .  }else{.    *p
14ac9 70 46 69 72 73 74 20 3d 20 70 49 6e 3b 0a 20 20  pFirst = pIn;.  
14aca 7d 0a 20 20 69 66 28 20 70 49 6e 2d 3e 70 52 69  }.  if( pIn->pRi
14acb 67 68 74 20 29 7b 0a 20 20 20 20 72 6f 77 53 65  ght ){.    rowSe
14acc 74 54 72 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d  tTreeToList(pIn-
14acd 3e 70 52 69 67 68 74 2c 20 26 70 49 6e 2d 3e 70  >pRight, &pIn->p
14ace 52 69 67 68 74 2c 20 70 70 4c 61 73 74 29 3b 0a  Right, ppLast);.
14acf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70    }else{.    *pp
14ad0 4c 61 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a  Last = pIn;.  }.
14ad1 20 20 61 73 73 65 72 74 28 20 28 2a 70 70 4c 61    assert( (*ppLa
14ad2 73 74 29 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29  st)->pRight==0 )
14ad3 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  ;.}.../*.** Conv
14ad4 65 72 74 20 61 20 73 6f 72 74 65 64 20 6c 69 73  ert a sorted lis
14ad5 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 28 63  t of elements (c
14ad6 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 52 69 67  onnected by pRig
14ad7 68 74 29 20 69 6e 74 6f 20 61 20 62 69 6e 61 72  ht) into a binar
14ad8 79 0a 2a 2a 20 74 72 65 65 20 77 69 74 68 20 64  y.** tree with d
14ad9 65 70 74 68 20 6f 66 20 69 44 65 70 74 68 2e 20  epth of iDepth. 
14ada 20 41 20 64 65 70 74 68 20 6f 66 20 31 20 6d 65   A depth of 1 me
14adb 61 6e 73 20 74 68 65 20 74 72 65 65 20 63 6f 6e  ans the tree con
14adc 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a  tains a single.*
14add 2a 20 6e 6f 64 65 20 74 61 6b 65 6e 20 66 72 6f  * node taken fro
14ade 6d 20 74 68 65 20 68 65 61 64 20 6f 66 20 2a 70  m the head of *p
14adf 70 4c 69 73 74 2e 20 20 41 20 64 65 70 74 68 20  pList.  A depth 
14ae0 6f 66 20 32 20 6d 65 61 6e 73 20 61 20 74 72 65  of 2 means a tre
14ae1 65 20 77 69 74 68 0a 2a 2a 20 74 68 72 65 65 20  e with.** three 
14ae2 6e 6f 64 65 73 2e 20 20 41 6e 64 20 73 6f 20 66  nodes.  And so f
14ae3 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20  orth..**.** Use 
14ae4 61 73 20 6d 61 6e 79 20 65 6e 74 72 69 65 73 20  as many entries 
14ae5 66 72 6f 6d 20 74 68 65 20 69 6e 70 75 74 20 6c  from the input l
14ae6 69 73 74 20 61 73 20 72 65 71 75 69 72 65 64 20  ist as required 
14ae7 61 6e 64 20 75 70 64 61 74 65 20 74 68 65 0a 2a  and update the.*
14ae8 2a 20 2a 70 70 4c 69 73 74 20 74 6f 20 70 6f 69  * *ppList to poi
14ae9 6e 74 20 74 6f 20 74 68 65 20 75 6e 75 73 65 64  nt to the unused
14aea 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65   elements of the
14aeb 20 6c 69 73 74 2e 20 20 49 66 20 74 68 65 20 69   list.  If the i
14aec 6e 70 75 74 0a 2a 2a 20 6c 69 73 74 20 63 6f 6e  nput.** list con
14aed 74 61 69 6e 73 20 74 6f 6f 20 66 65 77 20 65 6c  tains too few el
14aee 65 6d 65 6e 74 73 2c 20 74 68 65 6e 20 63 6f 6e  ements, then con
14aef 73 74 72 75 63 74 20 61 6e 20 69 6e 63 6f 6d 70  struct an incomp
14af0 6c 65 74 65 20 74 72 65 65 0a 2a 2a 20 61 6e 64  lete tree.** and
14af1 20 6c 65 61 76 65 20 2a 70 70 4c 69 73 74 20 73   leave *ppList s
14af2 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  et to NULL..**.*
14af3 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
14af4 65 72 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6f  er to the root o
14af5 66 20 74 68 65 20 63 6f 6e 73 74 72 75 63 74 65  f the constructe
14af6 64 20 62 69 6e 61 72 79 20 74 72 65 65 2e 0a 2a  d binary tree..*
14af7 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20  /.static struct 
14af8 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77  RowSetEntry *row
14af9 53 65 74 4e 44 65 65 70 54 72 65 65 28 0a 20 20  SetNDeepTree(.  
14afa 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
14afb 72 79 20 2a 2a 70 70 4c 69 73 74 2c 0a 20 20 69  ry **ppList,.  i
14afc 6e 74 20 69 44 65 70 74 68 0a 29 7b 0a 20 20 73  nt iDepth.){.  s
14afd 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
14afe 79 20 2a 70 3b 20 20 20 20 20 20 20 20 20 2f 2a  y *p;         /*
14aff 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 6e 65 77   Root of the new
14b00 20 74 72 65 65 20 2a 2f 0a 20 20 73 74 72 75 63   tree */.  struc
14b01 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
14b02 4c 65 66 74 3b 20 20 20 20 20 2f 2a 20 4c 65 66  Left;     /* Lef
14b03 74 20 73 75 62 74 72 65 65 20 2a 2f 0a 20 20 69  t subtree */.  i
14b04 66 28 20 2a 70 70 4c 69 73 74 3d 3d 30 20 29 7b  f( *ppList==0 ){
14b05 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
14b06 20 7d 0a 20 20 69 66 28 20 69 44 65 70 74 68 3d   }.  if( iDepth=
14b07 3d 31 20 29 7b 0a 20 20 20 20 70 20 3d 20 2a 70  =1 ){.    p = *p
14b08 70 4c 69 73 74 3b 0a 20 20 20 20 2a 70 70 4c 69  pList;.    *ppLi
14b09 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a  st = p->pRight;.
14b0a 20 20 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70      p->pLeft = p
14b0b 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20  ->pRight = 0;.  
14b0c 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d 0a    return p;.  }.
14b0d 20 20 70 4c 65 66 74 20 3d 20 72 6f 77 53 65 74    pLeft = rowSet
14b0e 4e 44 65 65 70 54 72 65 65 28 70 70 4c 69 73 74  NDeepTree(ppList
14b0f 2c 20 69 44 65 70 74 68 2d 31 29 3b 0a 20 20 70  , iDepth-1);.  p
14b10 20 3d 20 2a 70 70 4c 69 73 74 3b 0a 20 20 69 66   = *ppList;.  if
14b11 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ( p==0 ){.    re
14b12 74 75 72 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 0a  turn pLeft;.  }.
14b13 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65    p->pLeft = pLe
14b14 66 74 3b 0a 20 20 2a 70 70 4c 69 73 74 20 3d 20  ft;.  *ppList = 
14b15 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e  p->pRight;.  p->
14b16 70 52 69 67 68 74 20 3d 20 72 6f 77 53 65 74 4e  pRight = rowSetN
14b17 44 65 65 70 54 72 65 65 28 70 70 4c 69 73 74 2c  DeepTree(ppList,
14b18 20 69 44 65 70 74 68 2d 31 29 3b 0a 20 20 72 65   iDepth-1);.  re
14b19 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
14b1a 20 43 6f 6e 76 65 72 74 20 61 20 73 6f 72 74 65   Convert a sorte
14b1b 64 20 6c 69 73 74 20 6f 66 20 65 6c 65 6d 65 6e  d list of elemen
14b1c 74 73 20 69 6e 74 6f 20 61 20 62 69 6e 61 72 79  ts into a binary
14b1d 20 74 72 65 65 2e 20 4d 61 6b 65 20 74 68 65 20   tree. Make the 
14b1e 74 72 65 65 0a 2a 2a 20 61 73 20 64 65 65 70 20  tree.** as deep 
14b1f 61 73 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62  as it needs to b
14b20 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f  e in order to co
14b21 6e 74 61 69 6e 20 74 68 65 20 65 6e 74 69 72 65  ntain the entire
14b22 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63   list..*/.static
14b23 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
14b24 74 72 79 20 2a 72 6f 77 53 65 74 4c 69 73 74 54  try *rowSetListT
14b25 6f 54 72 65 65 28 73 74 72 75 63 74 20 52 6f 77  oTree(struct Row
14b26 53 65 74 45 6e 74 72 79 20 2a 70 4c 69 73 74 29  SetEntry *pList)
14b27 7b 0a 20 20 69 6e 74 20 69 44 65 70 74 68 3b 20  {.  int iDepth; 
14b28 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70            /* Dep
14b29 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 73  th of the tree s
14b2a 6f 20 66 61 72 20 2a 2f 0a 20 20 73 74 72 75 63  o far */.  struc
14b2b 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
14b2c 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65  ;       /* Curre
14b2d 6e 74 20 74 72 65 65 20 72 6f 6f 74 20 2a 2f 0a  nt tree root */.
14b2e 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
14b2f 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 2f  ntry *pLeft;   /
14b30 2a 20 4c 65 66 74 20 73 75 62 74 72 65 65 20 2a  * Left subtree *
14b31 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69  /..  assert( pLi
14b32 73 74 21 3d 30 20 29 3b 0a 20 20 70 20 3d 20 70  st!=0 );.  p = p
14b33 4c 69 73 74 3b 0a 20 20 70 4c 69 73 74 20 3d 20  List;.  pList = 
14b34 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e  p->pRight;.  p->
14b35 70 4c 65 66 74 20 3d 20 70 2d 3e 70 52 69 67 68  pLeft = p->pRigh
14b36 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 44 65  t = 0;.  for(iDe
14b37 70 74 68 3d 31 3b 20 70 4c 69 73 74 3b 20 69 44  pth=1; pList; iD
14b38 65 70 74 68 2b 2b 29 7b 0a 20 20 20 20 70 4c 65  epth++){.    pLe
14b39 66 74 20 3d 20 70 3b 0a 20 20 20 20 70 20 3d 20  ft = p;.    p = 
14b3a 70 4c 69 73 74 3b 0a 20 20 20 20 70 4c 69 73 74  pList;.    pList
14b3b 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20   = p->pRight;.  
14b3c 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65    p->pLeft = pLe
14b3d 66 74 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 68  ft;.    p->pRigh
14b3e 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 54  t = rowSetNDeepT
14b3f 72 65 65 28 26 70 4c 69 73 74 2c 20 69 44 65 70  ree(&pList, iDep
14b40 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  th);.  }.  retur
14b41 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61  n p;.}../*.** Ta
14b42 6b 65 20 61 6c 6c 20 74 68 65 20 65 6e 74 72 69  ke all the entri
14b43 65 73 20 6f 6e 20 70 2d 3e 70 45 6e 74 72 79 20  es on p->pEntry 
14b44 61 6e 64 20 6f 6e 20 74 68 65 20 74 72 65 65 73  and on the trees
14b45 20 69 6e 20 70 2d 3e 70 46 6f 72 65 73 74 20 61   in p->pForest a
14b46 6e 64 0a 2a 2a 20 73 6f 72 74 20 74 68 65 6d 20  nd.** sort them 
14b47 61 6c 6c 20 74 6f 67 65 74 68 65 72 20 69 6e 74  all together int
14b48 6f 20 6f 6e 65 20 62 69 67 20 6f 72 64 65 72 65  o one big ordere
14b49 64 20 6c 69 73 74 20 6f 6e 20 70 2d 3e 70 45 6e  d list on p->pEn
14b4a 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  try..**.** This 
14b4b 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f  routine should o
14b4c 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e  nly be called on
14b4d 63 65 20 69 6e 20 74 68 65 20 6c 69 66 65 20 6f  ce in the life o
14b4e 66 20 61 20 52 6f 77 53 65 74 2e 0a 2a 2f 0a 73  f a RowSet..*/.s
14b4f 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65  tatic void rowSe
14b50 74 54 6f 4c 69 73 74 28 52 6f 77 53 65 74 20 2a  tToList(RowSet *
14b51 70 29 7b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72  p){..  /* This r
14b52 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
14b53 20 6f 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a 20 20   only once */.  
14b54 61 73 73 65 72 74 28 20 70 21 3d 30 20 26 26 20  assert( p!=0 && 
14b55 28 70 2d 3e 72 73 46 6c 61 67 73 20 26 20 52 4f  (p->rsFlags & RO
14b56 57 53 45 54 5f 4e 45 58 54 29 3d 3d 30 20 29 3b  WSET_NEXT)==0 );
14b57 0a 0a 20 20 69 66 28 20 28 70 2d 3e 72 73 46 6c  ..  if( (p->rsFl
14b58 61 67 73 20 26 20 52 4f 57 53 45 54 5f 53 4f 52  ags & ROWSET_SOR
14b59 54 45 44 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70  TED)==0 ){.    p
14b5a 2d 3e 70 45 6e 74 72 79 20 3d 20 72 6f 77 53 65  ->pEntry = rowSe
14b5b 74 45 6e 74 72 79 53 6f 72 74 28 70 2d 3e 70 45  tEntrySort(p->pE
14b5c 6e 74 72 79 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ntry);.  }..  /*
14b5d 20 57 68 69 6c 65 20 74 68 69 73 20 6d 6f 64 75   While this modu
14b5e 6c 65 20 63 6f 75 6c 64 20 74 68 65 6f 72 65 74  le could theoret
14b5f 69 63 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 69  ically support i
14b60 74 2c 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74  t, sqlite3RowSet
14b61 4e 65 78 74 28 29 0a 20 20 2a 2a 20 69 73 20 6e  Next().  ** is n
14b62 65 76 65 72 20 63 61 6c 6c 65 64 20 61 66 74 65  ever called afte
14b63 72 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54  r sqlite3RowSetT
14b64 65 78 74 28 29 20 66 6f 72 20 74 68 65 20 73 61  ext() for the sa
14b65 6d 65 20 52 6f 77 53 65 74 2e 20 20 53 6f 0a 20  me RowSet.  So. 
14b66 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 65 76   ** there is nev
14b67 65 72 20 61 20 66 6f 72 65 73 74 20 74 6f 20 64  er a forest to d
14b68 65 61 6c 20 77 69 74 68 2e 20 20 53 68 6f 75 6c  eal with.  Shoul
14b69 64 20 74 68 69 73 20 63 68 61 6e 67 65 2c 20 73  d this change, s
14b6a 69 6d 70 6c 79 0a 20 20 2a 2a 20 72 65 6d 6f 76  imply.  ** remov
14b6b 65 20 74 68 65 20 61 73 73 65 72 74 28 29 20 61  e the assert() a
14b6c 6e 64 20 74 68 65 20 23 69 66 20 30 2e 20 2a 2f  nd the #if 0. */
14b6d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 46  .  assert( p->pF
14b6e 6f 72 65 73 74 3d 3d 30 20 29 3b 0a 23 69 66 20  orest==0 );.#if 
14b6f 30 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 46  0.  while( p->pF
14b70 6f 72 65 73 74 20 29 7b 0a 20 20 20 20 73 74 72  orest ){.    str
14b71 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
14b72 2a 70 54 72 65 65 20 3d 20 70 2d 3e 70 46 6f 72  *pTree = p->pFor
14b73 65 73 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20  est->pLeft;.    
14b74 69 66 28 20 70 54 72 65 65 20 29 7b 0a 20 20 20  if( pTree ){.   
14b75 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74     struct RowSet
14b76 45 6e 74 72 79 20 2a 70 48 65 61 64 2c 20 2a 70  Entry *pHead, *p
14b77 54 61 69 6c 3b 0a 20 20 20 20 20 20 72 6f 77 53  Tail;.      rowS
14b78 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70 54 72  etTreeToList(pTr
14b79 65 65 2c 20 26 70 48 65 61 64 2c 20 26 70 54 61  ee, &pHead, &pTa
14b7a 69 6c 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 45  il);.      p->pE
14b7b 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 45 6e 74  ntry = rowSetEnt
14b7c 72 79 4d 65 72 67 65 28 70 2d 3e 70 45 6e 74 72  ryMerge(p->pEntr
14b7d 79 2c 20 70 48 65 61 64 29 3b 0a 20 20 20 20 7d  y, pHead);.    }
14b7e 0a 20 20 20 20 70 2d 3e 70 46 6f 72 65 73 74 20  .    p->pForest 
14b7f 3d 20 70 2d 3e 70 46 6f 72 65 73 74 2d 3e 70 52  = p->pForest->pR
14b80 69 67 68 74 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  ight;.  }.#endif
14b81 0a 20 20 70 2d 3e 72 73 46 6c 61 67 73 20 7c 3d  .  p->rsFlags |=
14b82 20 52 4f 57 53 45 54 5f 4e 45 58 54 3b 20 20 2f   ROWSET_NEXT;  /
14b83 2a 20 56 65 72 69 66 79 20 74 68 69 73 20 72 6f  * Verify this ro
14b84 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63  utine is never c
14b85 61 6c 6c 65 64 20 61 67 61 69 6e 20 2a 2f 0a 7d  alled again */.}
14b86 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20  ../*.** Extract 
14b87 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6c 65  the smallest ele
14b88 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 52 6f  ment from the Ro
14b89 77 53 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 74  wSet..** Write t
14b8a 68 65 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20  he element into 
14b8b 2a 70 52 6f 77 69 64 2e 20 20 52 65 74 75 72 6e  *pRowid.  Return
14b8c 20 31 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20   1 on success.  
14b8d 52 65 74 75 72 6e 0a 2a 2a 20 30 20 69 66 20 74  Return.** 0 if t
14b8e 68 65 20 52 6f 77 53 65 74 20 69 73 20 61 6c 72  he RowSet is alr
14b8f 65 61 64 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a  eady empty..**.*
14b90 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75  * After this rou
14b91 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61  tine has been ca
14b92 6c 6c 65 64 2c 20 74 68 65 20 73 71 6c 69 74 65  lled, the sqlite
14b93 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 29 0a  3RowSetInsert().
14b94 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 6e  ** routine may n
14b95 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61  ot be called aga
14b96 69 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  in.  .*/.SQLITE_
14b97 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14b98 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 52 6f  te3RowSetNext(Ro
14b99 77 53 65 74 20 2a 70 2c 20 69 36 34 20 2a 70 52  wSet *p, i64 *pR
14b9a 6f 77 69 64 29 7b 0a 20 20 61 73 73 65 72 74 28  owid){.  assert(
14b9b 20 70 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 4d   p!=0 );..  /* M
14b9c 65 72 67 65 20 74 68 65 20 66 6f 72 65 73 74 20  erge the forest 
14b9d 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 6f  into a single so
14b9e 72 74 65 64 20 6c 69 73 74 20 6f 6e 20 66 69 72  rted list on fir
14b9f 73 74 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 66 28  st call */.  if(
14ba0 20 28 70 2d 3e 72 73 46 6c 61 67 73 20 26 20 52   (p->rsFlags & R
14ba1 4f 57 53 45 54 5f 4e 45 58 54 29 3d 3d 30 20 29  OWSET_NEXT)==0 )
14ba2 20 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 70 29   rowSetToList(p)
14ba3 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 74  ;..  /* Return t
14ba4 68 65 20 6e 65 78 74 20 65 6e 74 72 79 20 6f 6e  he next entry on
14ba5 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69   the list */.  i
14ba6 66 28 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a  f( p->pEntry ){.
14ba7 20 20 20 20 2a 70 52 6f 77 69 64 20 3d 20 70 2d      *pRowid = p-
14ba8 3e 70 45 6e 74 72 79 2d 3e 76 3b 0a 20 20 20 20  >pEntry->v;.    
14ba9 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70  p->pEntry = p->p
14baa 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20  Entry->pRight;. 
14bab 20 20 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 79     if( p->pEntry
14bac 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
14bad 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28  ite3RowSetClear(
14bae 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  p);.    }.    re
14baf 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  turn 1;.  }else{
14bb0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
14bb1 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63   }.}../*.** Chec
14bb2 6b 20 74 6f 20 73 65 65 20 69 66 20 65 6c 65 6d  k to see if elem
14bb3 65 6e 74 20 69 52 6f 77 69 64 20 77 61 73 20 69  ent iRowid was i
14bb4 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65  nserted into the
14bb5 20 72 6f 77 73 65 74 20 61 73 0a 2a 2a 20 70 61   rowset as.** pa
14bb6 72 74 20 6f 66 20 61 6e 79 20 69 6e 73 65 72 74  rt of any insert
14bb7 20 62 61 74 63 68 20 70 72 69 6f 72 20 74 6f 20   batch prior to 
14bb8 69 42 61 74 63 68 2e 20 20 52 65 74 75 72 6e 20  iBatch.  Return 
14bb9 31 20 6f 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66  1 or 0..**.** If
14bba 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72   this is the fir
14bbb 73 74 20 74 65 73 74 20 6f 66 20 61 20 6e 65 77  st test of a new
14bbc 20 62 61 74 63 68 20 61 6e 64 20 69 66 20 74 68   batch and if th
14bbd 65 72 65 20 65 78 69 73 74 20 65 6e 74 69 72 65  ere exist entire
14bbe 73 0a 2a 2a 20 6f 6e 20 70 52 6f 77 53 65 74 2d  s.** on pRowSet-
14bbf 3e 70 45 6e 74 72 79 2c 20 74 68 65 6e 20 73 6f  >pEntry, then so
14bc0 72 74 20 74 68 6f 73 65 20 65 6e 74 69 72 65 73  rt those entires
14bc1 20 69 6e 74 6f 20 74 68 65 20 66 6f 72 65 73 74   into the forest
14bc2 20 61 74 0a 2a 2a 20 70 52 6f 77 53 65 74 2d 3e   at.** pRowSet->
14bc3 70 46 6f 72 65 73 74 20 73 6f 20 74 68 61 74 20  pForest so that 
14bc4 74 68 65 79 20 63 61 6e 20 62 65 20 74 65 73 74  they can be test
14bc5 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
14bc6 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
14bc7 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77 53  3RowSetTest(RowS
14bc8 65 74 20 2a 70 52 6f 77 53 65 74 2c 20 75 38 20  et *pRowSet, u8 
14bc9 69 42 61 74 63 68 2c 20 73 71 6c 69 74 65 33 5f  iBatch, sqlite3_
14bca 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b 0a 20  int64 iRowid){. 
14bcb 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
14bcc 74 72 79 20 2a 70 2c 20 2a 70 54 72 65 65 3b 0a  try *p, *pTree;.
14bcd 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69  .  /* This routi
14bce 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c  ne is never call
14bcf 65 64 20 61 66 74 65 72 20 73 71 6c 69 74 65 33  ed after sqlite3
14bd0 52 6f 77 53 65 74 4e 65 78 74 28 29 20 2a 2f 0a  RowSetNext() */.
14bd1 20 20 61 73 73 65 72 74 28 20 70 52 6f 77 53 65    assert( pRowSe
14bd2 74 21 3d 30 20 26 26 20 28 70 52 6f 77 53 65 74  t!=0 && (pRowSet
14bd3 2d 3e 72 73 46 6c 61 67 73 20 26 20 52 4f 57 53  ->rsFlags & ROWS
14bd4 45 54 5f 4e 45 58 54 29 3d 3d 30 20 29 3b 0a 0a  ET_NEXT)==0 );..
14bd5 20 20 2f 2a 20 53 6f 72 74 20 65 6e 74 72 69 65    /* Sort entrie
14bd6 73 20 69 6e 74 6f 20 74 68 65 20 66 6f 72 65 73  s into the fores
14bd7 74 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 74  t on the first t
14bd8 65 73 74 20 6f 66 20 61 20 6e 65 77 20 62 61 74  est of a new bat
14bd9 63 68 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  ch .  */.  if( i
14bda 42 61 74 63 68 21 3d 70 52 6f 77 53 65 74 2d 3e  Batch!=pRowSet->
14bdb 69 42 61 74 63 68 20 29 7b 0a 20 20 20 20 70 20  iBatch ){.    p 
14bdc 3d 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72  = pRowSet->pEntr
14bdd 79 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a  y;.    if( p ){.
14bde 20 20 20 20 20 20 73 74 72 75 63 74 20 52 6f 77        struct Row
14bdf 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 50 72 65  SetEntry **ppPre
14be0 76 54 72 65 65 20 3d 20 26 70 52 6f 77 53 65 74  vTree = &pRowSet
14be1 2d 3e 70 46 6f 72 65 73 74 3b 0a 20 20 20 20 20  ->pForest;.     
14be2 20 69 66 28 20 28 70 52 6f 77 53 65 74 2d 3e 72   if( (pRowSet->r
14be3 73 46 6c 61 67 73 20 26 20 52 4f 57 53 45 54 5f  sFlags & ROWSET_
14be4 53 4f 52 54 45 44 29 3d 3d 30 20 29 7b 0a 20 20  SORTED)==0 ){.  
14be5 20 20 20 20 20 20 70 20 3d 20 72 6f 77 53 65 74        p = rowSet
14be6 45 6e 74 72 79 53 6f 72 74 28 70 29 3b 0a 20 20  EntrySort(p);.  
14be7 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28      }.      for(
14be8 70 54 72 65 65 20 3d 20 70 52 6f 77 53 65 74 2d  pTree = pRowSet-
14be9 3e 70 46 6f 72 65 73 74 3b 20 70 54 72 65 65 3b  >pForest; pTree;
14bea 20 70 54 72 65 65 3d 70 54 72 65 65 2d 3e 70 52   pTree=pTree->pR
14beb 69 67 68 74 29 7b 0a 20 20 20 20 20 20 20 20 70  ight){.        p
14bec 70 50 72 65 76 54 72 65 65 20 3d 20 26 70 54 72  pPrevTree = &pTr
14bed 65 65 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  ee->pRight;.    
14bee 20 20 20 20 69 66 28 20 70 54 72 65 65 2d 3e 70      if( pTree->p
14bef 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Left==0 ){.     
14bf0 20 20 20 20 20 70 54 72 65 65 2d 3e 70 4c 65 66       pTree->pLef
14bf1 74 20 3d 20 72 6f 77 53 65 74 4c 69 73 74 54 6f  t = rowSetListTo
14bf2 54 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20  Tree(p);.       
14bf3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
14bf4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
14bf5 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74     struct RowSet
14bf6 45 6e 74 72 79 20 2a 70 41 75 78 2c 20 2a 70 54  Entry *pAux, *pT
14bf7 61 69 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 72  ail;.          r
14bf8 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28  owSetTreeToList(
14bf9 70 54 72 65 65 2d 3e 70 4c 65 66 74 2c 20 26 70  pTree->pLeft, &p
14bfa 41 75 78 2c 20 26 70 54 61 69 6c 29 3b 0a 20 20  Aux, &pTail);.  
14bfb 20 20 20 20 20 20 20 20 70 54 72 65 65 2d 3e 70          pTree->p
14bfc 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  Left = 0;.      
14bfd 20 20 20 20 70 20 3d 20 72 6f 77 53 65 74 45 6e      p = rowSetEn
14bfe 74 72 79 4d 65 72 67 65 28 70 41 75 78 2c 20 70  tryMerge(pAux, p
14bff 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
14c00 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
14c01 54 72 65 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Tree==0 ){.     
14c02 20 20 20 2a 70 70 50 72 65 76 54 72 65 65 20 3d     *ppPrevTree =
14c03 20 70 54 72 65 65 20 3d 20 72 6f 77 53 65 74 45   pTree = rowSetE
14c04 6e 74 72 79 41 6c 6c 6f 63 28 70 52 6f 77 53 65  ntryAlloc(pRowSe
14c05 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t);.        if( 
14c06 70 54 72 65 65 20 29 7b 0a 20 20 20 20 20 20 20  pTree ){.       
14c07 20 20 20 70 54 72 65 65 2d 3e 76 20 3d 20 30 3b     pTree->v = 0;
14c08 0a 20 20 20 20 20 20 20 20 20 20 70 54 72 65 65  .          pTree
14c09 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20  ->pRight = 0;.  
14c0a 20 20 20 20 20 20 20 20 70 54 72 65 65 2d 3e 70          pTree->p
14c0b 4c 65 66 74 20 3d 20 72 6f 77 53 65 74 4c 69 73  Left = rowSetLis
14c0c 74 54 6f 54 72 65 65 28 70 29 3b 0a 20 20 20 20  tToTree(p);.    
14c0d 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
14c0e 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e      pRowSet->pEn
14c0f 74 72 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  try = 0;.      p
14c10 52 6f 77 53 65 74 2d 3e 70 4c 61 73 74 20 3d 20  RowSet->pLast = 
14c11 30 3b 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74  0;.      pRowSet
14c12 2d 3e 72 73 46 6c 61 67 73 20 7c 3d 20 52 4f 57  ->rsFlags |= ROW
14c13 53 45 54 5f 53 4f 52 54 45 44 3b 0a 20 20 20 20  SET_SORTED;.    
14c14 7d 0a 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 69  }.    pRowSet->i
14c15 42 61 74 63 68 20 3d 20 69 42 61 74 63 68 3b 0a  Batch = iBatch;.
14c16 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 74    }..  /* Test t
14c17 6f 20 73 65 65 20 69 66 20 74 68 65 20 69 52 6f  o see if the iRo
14c18 77 69 64 20 76 61 6c 75 65 20 61 70 70 65 61 72  wid value appear
14c19 73 20 61 6e 79 77 68 65 72 65 20 69 6e 20 74 68  s anywhere in th
14c1a 65 20 66 6f 72 65 73 74 2e 0a 20 20 2a 2a 20 52  e forest..  ** R
14c1b 65 74 75 72 6e 20 31 20 69 66 20 69 74 20 64 6f  eturn 1 if it do
14c1c 65 73 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e  es and 0 if not.
14c1d 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 54 72 65  .  */.  for(pTre
14c1e 65 20 3d 20 70 52 6f 77 53 65 74 2d 3e 70 46 6f  e = pRowSet->pFo
14c1f 72 65 73 74 3b 20 70 54 72 65 65 3b 20 70 54 72  rest; pTree; pTr
14c20 65 65 3d 70 54 72 65 65 2d 3e 70 52 69 67 68 74  ee=pTree->pRight
14c21 29 7b 0a 20 20 20 20 70 20 3d 20 70 54 72 65 65  ){.    p = pTree
14c22 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 77 68 69  ->pLeft;.    whi
14c23 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 20 20 69  le( p ){.      i
14c24 66 28 20 70 2d 3e 76 3c 69 52 6f 77 69 64 20 29  f( p->v<iRowid )
14c25 7b 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d  {.        p = p-
14c26 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 7d  >pRight;.      }
14c27 65 6c 73 65 20 69 66 28 20 70 2d 3e 76 3e 69 52  else if( p->v>iR
14c28 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20  owid ){.        
14c29 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20  p = p->pLeft;.  
14c2a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
14c2b 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
14c2c 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
14c2d 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
14c2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
14c2f 64 20 6f 66 20 72 6f 77 73 65 74 2e 63 20 2a 2a  d of rowset.c **
14c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
14c33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
14c34 67 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 63  gin file pager.c
14c35 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
14c36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
14c38 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
14c39 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
14c3a 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
14c3b 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
14c3c 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
14c3d 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
14c3e 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
14c3f 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
14c40 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
14c41 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
14c42 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
14c43 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
14c44 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
14c45 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
14c46 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
14c47 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
14c48 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
14c49 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
14c4a 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
14c4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c4f 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ****.** This is 
14c50 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
14c51 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 20 63  on of the page c
14c52 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 6f  ache subsystem o
14c53 72 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a  r "pager"..** .*
14c54 2a 20 54 68 65 20 70 61 67 65 72 20 69 73 20 75  * The pager is u
14c55 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 61 20  sed to access a 
14c56 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 66 69  database disk fi
14c57 6c 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e  le.  It implemen
14c58 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63 6f 6d  ts.** atomic com
14c59 6d 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b  mit and rollback
14c5a 20 74 68 72 6f 75 67 68 20 74 68 65 20 75 73 65   through the use
14c5b 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69   of a journal fi
14c5c 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 73 65  le that.** is se
14c5d 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20  parate from the 
14c5e 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
14c5f 54 68 65 20 70 61 67 65 72 20 61 6c 73 6f 20 69  The pager also i
14c60 6d 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65 0a 2a  mplements file.*
14c61 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70 72 65  * locking to pre
14c62 76 65 6e 74 20 74 77 6f 20 70 72 6f 63 65 73 73  vent two process
14c63 65 73 20 66 72 6f 6d 20 77 72 69 74 69 6e 67 20  es from writing 
14c64 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
14c65 65 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75 6c 74  e.** file simult
14c66 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f 6e 65  aneously, or one
14c67 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 72 65   process from re
14c68 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  ading the databa
14c69 73 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74  se while.** anot
14c6a 68 65 72 20 69 73 20 77 72 69 74 69 6e 67 2e 0a  her is writing..
14c6b 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
14c6c 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 2f 2a  E_OMIT_DISKIO./*
14c6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
14c6e 63 6c 75 64 65 20 77 61 6c 2e 68 20 69 6e 20 74  clude wal.h in t
14c6f 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 70 61 67  he middle of pag
14c70 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
14c71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
14c72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
14c73 67 69 6e 20 66 69 6c 65 20 77 61 6c 2e 68 20 2a  gin file wal.h *
14c74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
14c77 0a 2a 2a 20 32 30 31 30 20 46 65 62 72 75 61 72  .** 2010 Februar
14c78 79 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  y 1.**.** The au
14c79 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
14c7a 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
14c7b 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
14c7c 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
14c7d 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
14c7e 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
14c7f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
14c80 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
14c81 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
14c82 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
14c83 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
14c84 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
14c85 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
14c86 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
14c87 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
14c88 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
14c89 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
14c8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c8e 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65  **.** This heade
14c8f 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74  r file defines t
14c90 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  he interface to 
14c91 74 68 65 20 77 72 69 74 65 2d 61 68 65 61 64 20  the write-ahead 
14c92 6c 6f 67 67 69 6e 67 20 0a 2a 2a 20 73 79 73 74  logging .** syst
14c93 65 6d 2e 20 52 65 66 65 72 20 74 6f 20 74 68 65  em. Refer to the
14c94 20 63 6f 6d 6d 65 6e 74 73 20 62 65 6c 6f 77 20   comments below 
14c95 61 6e 64 20 74 68 65 20 68 65 61 64 65 72 20 63  and the header c
14c96 6f 6d 6d 65 6e 74 20 61 74 74 61 63 68 65 64 20  omment attached 
14c97 74 6f 20 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65  to .** the imple
14c98 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 65 61 63  mentation of eac
14c99 68 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 6c 6f  h function in lo
14c9a 67 2e 63 20 66 6f 72 20 66 75 72 74 68 65 72 20  g.c for further 
14c9b 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 0a 23 69 66  details..*/..#if
14c9c 6e 64 65 66 20 5f 57 41 4c 5f 48 5f 0a 23 64 65  ndef _WAL_H_.#de
14c9d 66 69 6e 65 20 5f 57 41 4c 5f 48 5f 0a 0a 0a 2f  fine _WAL_H_.../
14c9e 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 76 61 6c  * Additional val
14c9f 75 65 73 20 74 68 61 74 20 63 61 6e 20 62 65 20  ues that can be 
14ca0 61 64 64 65 64 20 74 6f 20 74 68 65 20 73 79 6e  added to the syn
14ca1 63 5f 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74  c_flags argument
14ca2 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 57 61   of.** sqlite3Wa
14ca3 6c 46 72 61 6d 65 73 28 29 3a 0a 2a 2f 0a 23 64  lFrames():.*/.#d
14ca4 65 66 69 6e 65 20 57 41 4c 5f 53 59 4e 43 5f 54  efine WAL_SYNC_T
14ca5 52 41 4e 53 41 43 54 49 4f 4e 53 20 20 30 78 32  RANSACTIONS  0x2
14ca6 30 20 20 20 2f 2a 20 53 79 6e 63 20 61 74 20 74  0   /* Sync at t
14ca7 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 74  he end of each t
14ca8 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64  ransaction */.#d
14ca9 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e  efine SQLITE_SYN
14caa 43 5f 4d 41 53 4b 20 20 20 20 20 20 20 30 78 31  C_MASK       0x1
14cab 33 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 66 20  3   /* Mask off 
14cac 74 68 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  the SQLITE_SYNC_
14cad 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 0a 23 69 66  * values */..#if
14cae 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
14caf 57 41 4c 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  WAL.# define sql
14cb0 69 74 65 33 57 61 6c 4f 70 65 6e 28 78 2c 79 2c  ite3WalOpen(x,y,
14cb1 7a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  z)              
14cb2 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
14cb3 73 71 6c 69 74 65 33 57 61 6c 4c 69 6d 69 74 28  sqlite3WalLimit(
14cb4 78 2c 79 29 0a 23 20 64 65 66 69 6e 65 20 73 71  x,y).# define sq
14cb5 6c 69 74 65 33 57 61 6c 43 6c 6f 73 65 28 77 2c  lite3WalClose(w,
14cb6 78 2c 79 2c 7a 29 20 20 20 20 20 20 20 20 20 20  x,y,z)          
14cb7 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
14cb8 20 73 71 6c 69 74 65 33 57 61 6c 42 65 67 69 6e   sqlite3WalBegin
14cb9 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28  ReadTransaction(
14cba 79 2c 7a 29 20 20 20 20 20 30 0a 23 20 64 65 66  y,z)     0.# def
14cbb 69 6e 65 20 73 71 6c 69 74 65 33 57 61 6c 45 6e  ine sqlite3WalEn
14cbc 64 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f 6e  dReadTransaction
14cbd 28 7a 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  (z).# define sql
14cbe 69 74 65 33 57 61 6c 52 65 61 64 28 76 2c 77 2c  ite3WalRead(v,w,
14cbf 78 2c 79 2c 7a 29 20 20 20 20 20 20 20 20 20 20  x,y,z)          
14cc0 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
14cc1 73 71 6c 69 74 65 33 57 61 6c 44 62 73 69 7a 65  sqlite3WalDbsize
14cc2 28 79 29 20 20 20 20 20 20 20 20 20 20 20 20 20  (y)             
14cc3 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
14cc4 6e 65 20 73 71 6c 69 74 65 33 57 61 6c 42 65 67  ne sqlite3WalBeg
14cc5 69 6e 57 72 69 74 65 54 72 61 6e 73 61 63 74 69  inWriteTransacti
14cc6 6f 6e 28 79 29 20 20 20 20 20 20 30 0a 23 20 64  on(y)      0.# d
14cc7 65 66 69 6e 65 20 73 71 6c 69 74 65 33 57 61 6c  efine sqlite3Wal
14cc8 45 6e 64 57 72 69 74 65 54 72 61 6e 73 61 63 74  EndWriteTransact
14cc9 69 6f 6e 28 78 29 20 20 20 20 20 20 20 20 30 0a  ion(x)        0.
14cca 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
14ccb 57 61 6c 55 6e 64 6f 28 78 2c 79 2c 7a 29 20 20  WalUndo(x,y,z)  
14ccc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14ccd 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
14cce 74 65 33 57 61 6c 53 61 76 65 70 6f 69 6e 74 28  te3WalSavepoint(
14ccf 79 2c 7a 29 0a 23 20 64 65 66 69 6e 65 20 73 71  y,z).# define sq
14cd0 6c 69 74 65 33 57 61 6c 53 61 76 65 70 6f 69 6e  lite3WalSavepoin
14cd1 74 55 6e 64 6f 28 79 2c 7a 29 20 20 20 20 20 20  tUndo(y,z)      
14cd2 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
14cd3 20 73 71 6c 69 74 65 33 57 61 6c 46 72 61 6d 65   sqlite3WalFrame
14cd4 73 28 75 2c 76 2c 77 2c 78 2c 79 2c 7a 29 20 20  s(u,v,w,x,y,z)  
14cd5 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66           0.# def
14cd6 69 6e 65 20 73 71 6c 69 74 65 33 57 61 6c 43 68  ine sqlite3WalCh
14cd7 65 63 6b 70 6f 69 6e 74 28 72 2c 73 2c 74 2c 75  eckpoint(r,s,t,u
14cd8 2c 76 2c 77 2c 78 2c 79 2c 7a 29 20 30 0a 23 20  ,v,w,x,y,z) 0.# 
14cd9 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 57 61  define sqlite3Wa
14cda 6c 43 61 6c 6c 62 61 63 6b 28 7a 29 20 20 20 20  lCallback(z)    
14cdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
14cdc 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
14cdd 33 57 61 6c 45 78 63 6c 75 73 69 76 65 4d 6f 64  3WalExclusiveMod
14cde 65 28 79 2c 7a 29 20 20 20 20 20 20 20 20 20 20  e(y,z)          
14cdf 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
14ce0 69 74 65 33 57 61 6c 48 65 61 70 4d 65 6d 6f 72  ite3WalHeapMemor
14ce1 79 28 7a 29 20 20 20 20 20 20 20 20 20 20 20 20  y(z)            
14ce2 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
14ce3 73 71 6c 69 74 65 33 57 61 6c 46 72 61 6d 65 73  sqlite3WalFrames
14ce4 69 7a 65 28 7a 29 20 20 20 20 20 20 20 20 20 20  ize(z)          
14ce5 20 20 20 20 20 20 20 20 30 0a 23 65 6c 73 65 0a          0.#else.
14ce6 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 53 41 56  .#define WAL_SAV
14ce7 45 50 4f 49 4e 54 5f 4e 44 41 54 41 20 34 0a 0a  EPOINT_NDATA 4..
14ce8 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f  /* Connection to
14ce9 20 61 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c   a write-ahead l
14cea 6f 67 20 28 57 41 4c 29 20 66 69 6c 65 2e 20 0a  og (WAL) file. .
14ceb 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20  ** There is one 
14cec 6f 62 6a 65 63 74 20 6f 66 20 74 68 69 73 20 74  object of this t
14ced 79 70 65 20 66 6f 72 20 65 61 63 68 20 70 61 67  ype for each pag
14cee 65 72 2e 20 0a 2a 2f 0a 74 79 70 65 64 65 66 20  er. .*/.typedef 
14cef 73 74 72 75 63 74 20 57 61 6c 20 57 61 6c 3b 0a  struct Wal Wal;.
14cf0 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 20 63 6c 6f  ./* Open and clo
14cf1 73 65 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  se a connection 
14cf2 74 6f 20 61 20 77 72 69 74 65 2d 61 68 65 61 64  to a write-ahead
14cf3 20 6c 6f 67 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f   log. */.SQLITE_
14cf4 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14cf5 74 65 33 57 61 6c 4f 70 65 6e 28 73 71 6c 69 74  te3WalOpen(sqlit
14cf6 65 33 5f 76 66 73 2a 2c 20 73 71 6c 69 74 65 33  e3_vfs*, sqlite3
14cf7 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 63 68  _file*, const ch
14cf8 61 72 20 2a 2c 20 69 6e 74 2c 20 69 36 34 2c 20  ar *, int, i64, 
14cf9 57 61 6c 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Wal**);.SQLITE_P
14cfa 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
14cfb 65 33 57 61 6c 43 6c 6f 73 65 28 57 61 6c 20 2a  e3WalClose(Wal *
14cfc 70 57 61 6c 2c 20 69 6e 74 20 73 79 6e 63 5f 66  pWal, int sync_f
14cfd 6c 61 67 73 2c 20 69 6e 74 2c 20 75 38 20 2a 29  lags, int, u8 *)
14cfe 3b 0a 0a 2f 2a 20 53 65 74 20 74 68 65 20 6c 69  ;../* Set the li
14cff 6d 69 74 69 6e 67 20 73 69 7a 65 20 6f 66 20 61  miting size of a
14d00 20 57 41 4c 20 66 69 6c 65 2e 20 2a 2f 0a 53 51   WAL file. */.SQ
14d01 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
14d02 64 20 73 71 6c 69 74 65 33 57 61 6c 4c 69 6d 69  d sqlite3WalLimi
14d03 74 28 57 61 6c 2a 2c 20 69 36 34 29 3b 0a 0a 2f  t(Wal*, i64);../
14d04 2a 20 55 73 65 64 20 62 79 20 72 65 61 64 65 72  * Used by reader
14d05 73 20 74 6f 20 6f 70 65 6e 20 28 6c 6f 63 6b 29  s to open (lock)
14d06 20 61 6e 64 20 63 6c 6f 73 65 20 28 75 6e 6c 6f   and close (unlo
14d07 63 6b 29 20 61 20 73 6e 61 70 73 68 6f 74 2e 20  ck) a snapshot. 
14d08 20 41 20 0a 2a 2a 20 73 6e 61 70 73 68 6f 74 20   A .** snapshot 
14d09 69 73 20 6c 69 6b 65 20 61 20 72 65 61 64 2d 74  is like a read-t
14d0a 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 74 20  ransaction.  It 
14d0b 69 73 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  is the state of 
14d0c 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
14d0d 61 74 20 61 6e 20 69 6e 73 74 61 6e 74 20 69 6e  at an instant in
14d0e 20 74 69 6d 65 2e 20 20 73 71 6c 69 74 65 33 57   time.  sqlite3W
14d0f 61 6c 4f 70 65 6e 53 6e 61 70 73 68 6f 74 20 67  alOpenSnapshot g
14d10 65 74 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  ets a read lock 
14d11 61 6e 64 0a 2a 2a 20 70 72 65 73 65 72 76 65 73  and.** preserves
14d12 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61   the current sta
14d13 74 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f  te even if the o
14d14 74 68 65 72 20 74 68 72 65 61 64 73 20 6f 72 20  ther threads or 
14d15 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20 77 72 69  processes.** wri
14d16 74 65 20 74 6f 20 6f 72 20 63 68 65 63 6b 70 6f  te to or checkpo
14d17 69 6e 74 20 74 68 65 20 57 41 4c 2e 20 20 73 71  int the WAL.  sq
14d18 6c 69 74 65 33 57 61 6c 43 6c 6f 73 65 53 6e 61  lite3WalCloseSna
14d19 70 73 68 6f 74 28 29 20 63 6c 6f 73 65 73 20 74  pshot() closes t
14d1a 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  he.** transactio
14d1b 6e 20 61 6e 64 20 72 65 6c 65 61 73 65 73 20 74  n and releases t
14d1c 68 65 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49  he lock..*/.SQLI
14d1d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
14d1e 71 6c 69 74 65 33 57 61 6c 42 65 67 69 6e 52 65  qlite3WalBeginRe
14d1f 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 57 61  adTransaction(Wa
14d20 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 20 2a 29 3b  l *pWal, int *);
14d21 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14d22 76 6f 69 64 20 73 71 6c 69 74 65 33 57 61 6c 45  void sqlite3WalE
14d23 6e 64 52 65 61 64 54 72 61 6e 73 61 63 74 69 6f  ndReadTransactio
14d24 6e 28 57 61 6c 20 2a 70 57 61 6c 29 3b 0a 0a 2f  n(Wal *pWal);../
14d25 2a 20 52 65 61 64 20 61 20 70 61 67 65 20 66 72  * Read a page fr
14d26 6f 6d 20 74 68 65 20 77 72 69 74 65 2d 61 68 65  om the write-ahe
14d27 61 64 20 6c 6f 67 2c 20 69 66 20 69 74 20 69 73  ad log, if it is
14d28 20 70 72 65 73 65 6e 74 2e 20 2a 2f 0a 53 51 4c   present. */.SQL
14d29 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
14d2a 73 71 6c 69 74 65 33 57 61 6c 52 65 61 64 28 57  sqlite3WalRead(W
14d2b 61 6c 20 2a 70 57 61 6c 2c 20 50 67 6e 6f 20 70  al *pWal, Pgno p
14d2c 67 6e 6f 2c 20 69 6e 74 20 2a 70 49 6e 57 61 6c  gno, int *pInWal
14d2d 2c 20 69 6e 74 20 6e 4f 75 74 2c 20 75 38 20 2a  , int nOut, u8 *
14d2e 70 4f 75 74 29 3b 0a 0a 2f 2a 20 49 66 20 74 68  pOut);../* If th
14d2f 65 20 57 41 4c 20 69 73 20 6e 6f 74 20 65 6d 70  e WAL is not emp
14d30 74 79 2c 20 72 65 74 75 72 6e 20 74 68 65 20 73  ty, return the s
14d31 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ize of the datab
14d32 61 73 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  ase. */.SQLITE_P
14d33 52 49 56 41 54 45 20 50 67 6e 6f 20 73 71 6c 69  RIVATE Pgno sqli
14d34 74 65 33 57 61 6c 44 62 73 69 7a 65 28 57 61 6c  te3WalDbsize(Wal
14d35 20 2a 70 57 61 6c 29 3b 0a 0a 2f 2a 20 4f 62 74   *pWal);../* Obt
14d36 61 69 6e 20 6f 72 20 72 65 6c 65 61 73 65 20 74  ain or release t
14d37 68 65 20 57 52 49 54 45 52 20 6c 6f 63 6b 2e 20  he WRITER lock. 
14d38 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14d39 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c  E int sqlite3Wal
14d3a 42 65 67 69 6e 57 72 69 74 65 54 72 61 6e 73 61  BeginWriteTransa
14d3b 63 74 69 6f 6e 28 57 61 6c 20 2a 70 57 61 6c 29  ction(Wal *pWal)
14d3c 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
14d3d 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 45   int sqlite3WalE
14d3e 6e 64 57 72 69 74 65 54 72 61 6e 73 61 63 74 69  ndWriteTransacti
14d3f 6f 6e 28 57 61 6c 20 2a 70 57 61 6c 29 3b 0a 0a  on(Wal *pWal);..
14d40 2f 2a 20 55 6e 64 6f 20 61 6e 79 20 66 72 61 6d  /* Undo any fram
14d41 65 73 20 77 72 69 74 74 65 6e 20 28 62 75 74 20  es written (but 
14d42 6e 6f 74 20 63 6f 6d 6d 69 74 74 65 64 29 20 74  not committed) t
14d43 6f 20 74 68 65 20 6c 6f 67 20 2a 2f 0a 53 51 4c  o the log */.SQL
14d44 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
14d45 73 71 6c 69 74 65 33 57 61 6c 55 6e 64 6f 28 57  sqlite3WalUndo(W
14d46 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 20 28 2a  al *pWal, int (*
14d47 78 55 6e 64 6f 29 28 76 6f 69 64 20 2a 2c 20 50  xUndo)(void *, P
14d48 67 6e 6f 29 2c 20 76 6f 69 64 20 2a 70 55 6e 64  gno), void *pUnd
14d49 6f 43 74 78 29 3b 0a 0a 2f 2a 20 52 65 74 75 72  oCtx);../* Retur
14d4a 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61  n an integer tha
14d4b 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 63 75  t records the cu
14d4c 72 72 65 6e 74 20 28 75 6e 63 6f 6d 6d 69 74 74  rrent (uncommitt
14d4d 65 64 29 20 77 72 69 74 65 0a 2a 2a 20 70 6f 73  ed) write.** pos
14d4e 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 57 41 4c  ition in the WAL
14d4f 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
14d50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 57  TE void sqlite3W
14d51 61 6c 53 61 76 65 70 6f 69 6e 74 28 57 61 6c 20  alSavepoint(Wal 
14d52 2a 70 57 61 6c 2c 20 75 33 32 20 2a 61 57 61 6c  *pWal, u32 *aWal
14d53 44 61 74 61 29 3b 0a 0a 2f 2a 20 4d 6f 76 65 20  Data);../* Move 
14d54 74 68 65 20 77 72 69 74 65 20 70 6f 73 69 74 69  the write positi
14d55 6f 6e 20 6f 66 20 74 68 65 20 57 41 4c 20 62 61  on of the WAL ba
14d56 63 6b 20 74 6f 20 69 46 72 61 6d 65 2e 20 20 43  ck to iFrame.  C
14d57 61 6c 6c 65 64 20 69 6e 0a 2a 2a 20 72 65 73 70  alled in.** resp
14d58 6f 6e 73 65 20 74 6f 20 61 20 52 4f 4c 4c 42 41  onse to a ROLLBA
14d59 43 4b 20 54 4f 20 63 6f 6d 6d 61 6e 64 2e 20 2a  CK TO command. *
14d5a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
14d5b 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 53   int sqlite3WalS
14d5c 61 76 65 70 6f 69 6e 74 55 6e 64 6f 28 57 61 6c  avepointUndo(Wal
14d5d 20 2a 70 57 61 6c 2c 20 75 33 32 20 2a 61 57 61   *pWal, u32 *aWa
14d5e 6c 44 61 74 61 29 3b 0a 0a 2f 2a 20 57 72 69 74  lData);../* Writ
14d5f 65 20 61 20 66 72 61 6d 65 20 6f 72 20 66 72 61  e a frame or fra
14d60 6d 65 73 20 74 6f 20 74 68 65 20 6c 6f 67 2e 20  mes to the log. 
14d61 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14d62 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c  E int sqlite3Wal
14d63 46 72 61 6d 65 73 28 57 61 6c 20 2a 70 57 61 6c  Frames(Wal *pWal
14d64 2c 20 69 6e 74 2c 20 50 67 48 64 72 20 2a 2c 20  , int, PgHdr *, 
14d65 50 67 6e 6f 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  Pgno, int, int);
14d66 0a 0a 2f 2a 20 43 6f 70 79 20 70 61 67 65 73 20  ../* Copy pages 
14d67 66 72 6f 6d 20 74 68 65 20 6c 6f 67 20 74 6f 20  from the log to 
14d68 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
14d69 65 20 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49  e */ .SQLITE_PRI
14d6a 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
14d6b 57 61 6c 43 68 65 63 6b 70 6f 69 6e 74 28 0a 20  WalCheckpoint(. 
14d6c 20 57 61 6c 20 2a 70 57 61 6c 2c 20 20 20 20 20   Wal *pWal,     
14d6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14d6e 20 2f 2a 20 57 72 69 74 65 2d 61 68 65 61 64 20   /* Write-ahead 
14d6f 6c 6f 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  log connection *
14d70 2f 0a 20 20 69 6e 74 20 65 4d 6f 64 65 2c 20 20  /.  int eMode,  
14d71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14d72 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 50 41      /* One of PA
14d73 53 53 49 56 45 2c 20 46 55 4c 4c 20 61 6e 64 20  SSIVE, FULL and 
14d74 52 45 53 54 41 52 54 20 2a 2f 0a 20 20 69 6e 74  RESTART */.  int
14d75 20 28 2a 78 42 75 73 79 29 28 76 6f 69 64 2a 29   (*xBusy)(void*)
14d76 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
14d77 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c  Function to call
14d78 20 77 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20   when busy */.  
14d79 76 6f 69 64 20 2a 70 42 75 73 79 41 72 67 2c 20  void *pBusyArg, 
14d7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14d7b 2f 2a 20 43 6f 6e 74 65 78 74 20 61 72 67 75 6d  /* Context argum
14d7c 65 6e 74 20 66 6f 72 20 78 42 75 73 79 48 61 6e  ent for xBusyHan
14d7d 64 6c 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 79  dler */.  int sy
14d7e 6e 63 5f 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nc_flags,       
14d7f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
14d80 67 73 20 74 6f 20 73 79 6e 63 20 64 62 20 66 69  gs to sync db fi
14d81 6c 65 20 77 69 74 68 20 28 6f 72 20 30 29 20 2a  le with (or 0) *
14d82 2f 0a 20 20 69 6e 74 20 6e 42 75 66 2c 20 20 20  /.  int nBuf,   
14d83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14d84 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62      /* Size of b
14d85 75 66 66 65 72 20 6e 42 75 66 20 2a 2f 0a 20 20  uffer nBuf */.  
14d86 75 38 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20  u8 *zBuf,       
14d87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14d88 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 62 75 66  /* Temporary buf
14d89 66 65 72 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20  fer to use */.  
14d8a 69 6e 74 20 2a 70 6e 4c 6f 67 2c 20 20 20 20 20  int *pnLog,     
14d8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14d8c 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f  /* OUT: Number o
14d8d 66 20 66 72 61 6d 65 73 20 69 6e 20 57 41 4c 20  f frames in WAL 
14d8e 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6b 70 74  */.  int *pnCkpt
14d8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14d90 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d       /* OUT: Num
14d91 62 65 72 20 6f 66 20 62 61 63 6b 66 69 6c 6c 65  ber of backfille
14d92 64 20 66 72 61 6d 65 73 20 69 6e 20 57 41 4c 20  d frames in WAL 
14d93 2a 2f 0a 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e  */.);../* Return
14d94 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 70 61   the value to pa
14d95 73 73 20 74 6f 20 61 20 73 71 6c 69 74 65 33 5f  ss to a sqlite3_
14d96 77 61 6c 5f 68 6f 6f 6b 20 63 61 6c 6c 62 61 63  wal_hook callbac
14d97 6b 2c 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72  k, the.** number
14d98 20 6f 66 20 66 72 61 6d 65 73 20 69 6e 20 74 68   of frames in th
14d99 65 20 57 41 4c 20 61 74 20 74 68 65 20 70 6f 69  e WAL at the poi
14d9a 6e 74 20 6f 66 20 74 68 65 20 6c 61 73 74 20 63  nt of the last c
14d9b 6f 6d 6d 69 74 20 73 69 6e 63 65 0a 2a 2a 20 73  ommit since.** s
14d9c 71 6c 69 74 65 33 57 61 6c 43 61 6c 6c 62 61 63  qlite3WalCallbac
14d9d 6b 28 29 20 77 61 73 20 63 61 6c 6c 65 64 2e 20  k() was called. 
14d9e 20 49 66 20 6e 6f 20 63 6f 6d 6d 69 74 73 20 68   If no commits h
14d9f 61 76 65 20 6f 63 63 75 72 72 65 64 20 73 69 6e  ave occurred sin
14da0 63 65 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 63  ce.** the last c
14da1 61 6c 6c 2c 20 74 68 65 6e 20 72 65 74 75 72 6e  all, then return
14da2 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52   0..*/.SQLITE_PR
14da3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
14da4 33 57 61 6c 43 61 6c 6c 62 61 63 6b 28 57 61 6c  3WalCallback(Wal
14da5 20 2a 70 57 61 6c 29 3b 0a 0a 2f 2a 20 54 65 6c   *pWal);../* Tel
14da6 6c 20 74 68 65 20 77 61 6c 20 6c 61 79 65 72 20  l the wal layer 
14da7 74 68 61 74 20 61 6e 20 45 58 43 4c 55 53 49 56  that an EXCLUSIV
14da8 45 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20  E lock has been 
14da9 6f 62 74 61 69 6e 65 64 20 28 6f 72 20 72 65 6c  obtained (or rel
14daa 65 61 73 65 64 29 0a 2a 2a 20 62 79 20 74 68 65  eased).** by the
14dab 20 70 61 67 65 72 20 6c 61 79 65 72 20 6f 6e 20   pager layer on 
14dac 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
14dad 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
14dae 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
14daf 57 61 6c 45 78 63 6c 75 73 69 76 65 4d 6f 64 65  WalExclusiveMode
14db0 28 57 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 20  (Wal *pWal, int 
14db1 6f 70 29 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20  op);../* Return 
14db2 74 72 75 65 20 69 66 20 74 68 65 20 61 72 67 75  true if the argu
14db3 6d 65 6e 74 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c  ment is non-NULL
14db4 20 61 6e 64 20 74 68 65 20 57 41 4c 20 6d 6f 64   and the WAL mod
14db5 75 6c 65 20 69 73 20 75 73 69 6e 67 0a 2a 2a 20  ule is using.** 
14db6 68 65 61 70 2d 6d 65 6d 6f 72 79 20 66 6f 72 20  heap-memory for 
14db7 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 2e 20 4f  the wal-index. O
14db8 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65  therwise, if the
14db9 20 61 72 67 75 6d 65 6e 74 20 69 73 20 4e 55 4c   argument is NUL
14dba 4c 20 6f 72 20 74 68 65 0a 2a 2a 20 57 41 4c 20  L or the.** WAL 
14dbb 6d 6f 64 75 6c 65 20 69 73 20 75 73 69 6e 67 20  module is using 
14dbc 73 68 61 72 65 64 2d 6d 65 6d 6f 72 79 2c 20 72  shared-memory, r
14dbd 65 74 75 72 6e 20 66 61 6c 73 65 2e 20 0a 2a 2f  eturn false. .*/
14dbe 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14dbf 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 48 65  int sqlite3WalHe
14dc0 61 70 4d 65 6d 6f 72 79 28 57 61 6c 20 2a 70 57  apMemory(Wal *pW
14dc1 61 6c 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  al);..#ifdef SQL
14dc2 49 54 45 5f 45 4e 41 42 4c 45 5f 5a 49 50 56 46  ITE_ENABLE_ZIPVF
14dc3 53 0a 2f 2a 20 49 66 20 74 68 65 20 57 41 4c 20  S./* If the WAL 
14dc4 66 69 6c 65 20 69 73 20 6e 6f 74 20 65 6d 70 74  file is not empt
14dc5 79 2c 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  y, return the nu
14dc6 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
14dc7 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 73 74 6f 72   content.** stor
14dc8 65 64 20 69 6e 20 65 61 63 68 20 66 72 61 6d 65  ed in each frame
14dc9 20 28 69 2e 65 2e 20 74 68 65 20 64 62 20 70 61   (i.e. the db pa
14dca 67 65 2d 73 69 7a 65 20 77 68 65 6e 20 74 68 65  ge-size when the
14dcb 20 57 41 4c 20 77 61 73 20 63 72 65 61 74 65 64   WAL was created
14dcc 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
14dcd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
14dce 57 61 6c 46 72 61 6d 65 73 69 7a 65 28 57 61 6c  WalFramesize(Wal
14dcf 20 2a 70 57 61 6c 29 3b 0a 23 65 6e 64 69 66 0a   *pWal);.#endif.
14dd0 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64 65  .#endif /* ifnde
14dd1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41  f SQLITE_OMIT_WA
14dd2 4c 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 5f  L */.#endif /* _
14dd3 57 41 4c 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  WAL_H_ */../****
14dd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
14dd5 66 20 77 61 6c 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  f wal.h ********
14dd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14dd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14dd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
14dd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
14dda 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
14ddb 65 66 74 20 6f 66 66 20 69 6e 20 70 61 67 65 72  eft off in pager
14ddc 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
14ddd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a  *********/.../**
14dde 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14ddf 2a 20 4e 4f 54 45 53 20 4f 4e 20 54 48 45 20 44  * NOTES ON THE D
14de0 45 53 49 47 4e 20 4f 46 20 54 48 45 20 50 41 47  ESIGN OF THE PAG
14de1 45 52 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ER *************
14de2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
14de3 2a 20 54 68 69 73 20 63 6f 6d 6d 65 6e 74 20 62  * This comment b
14de4 6c 6f 63 6b 20 64 65 73 63 72 69 62 65 73 20 69  lock describes i
14de5 6e 76 61 72 69 61 6e 74 73 20 74 68 61 74 20 68  nvariants that h
14de6 6f 6c 64 20 77 68 65 6e 20 75 73 69 6e 67 20 61  old when using a
14de7 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6a 6f 75   rollback.** jou
14de8 72 6e 61 6c 2e 20 20 54 68 65 73 65 20 69 6e 76  rnal.  These inv
14de9 61 72 69 61 6e 74 73 20 64 6f 20 6e 6f 74 20 61  ariants do not a
14dea 70 70 6c 79 20 66 6f 72 20 6a 6f 75 72 6e 61 6c  pply for journal
14deb 5f 6d 6f 64 65 3d 57 41 4c 2c 0a 2a 2a 20 6a 6f  _mode=WAL,.** jo
14dec 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d 4f 52  urnal_mode=MEMOR
14ded 59 2c 20 6f 72 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  Y, or journal_mo
14dee 64 65 3d 4f 46 46 2e 0a 2a 2a 0a 2a 2a 20 57 69  de=OFF..**.** Wi
14def 74 68 69 6e 20 74 68 69 73 20 63 6f 6d 6d 65 6e  thin this commen
14df0 74 20 62 6c 6f 63 6b 2c 20 61 20 70 61 67 65 20  t block, a page 
14df1 69 73 20 64 65 65 6d 65 64 20 74 6f 20 68 61 76  is deemed to hav
14df2 65 20 62 65 65 6e 20 73 79 6e 63 65 64 0a 2a 2a  e been synced.**
14df3 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61   automatically a
14df4 73 20 73 6f 6f 6e 20 61 73 20 69 74 20 69 73 20  s soon as it is 
14df5 77 72 69 74 74 65 6e 20 77 68 65 6e 20 50 52 41  written when PRA
14df6 47 4d 41 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d  GMA synchronous=
14df7 4f 46 46 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  OFF..** Otherwis
14df8 65 2c 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  e, the page is n
14df9 6f 74 20 73 79 6e 63 65 64 20 75 6e 74 69 6c 20  ot synced until 
14dfa 74 68 65 20 78 53 79 6e 63 20 6d 65 74 68 6f 64  the xSync method
14dfb 20 6f 66 20 74 68 65 20 56 46 53 0a 2a 2a 20 69   of the VFS.** i
14dfc 73 20 63 61 6c 6c 65 64 20 73 75 63 63 65 73 73  s called success
14dfd 66 75 6c 6c 79 20 6f 6e 20 74 68 65 20 66 69 6c  fully on the fil
14dfe 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
14dff 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 44 65 66   page..**.** Def
14e00 69 6e 69 74 69 6f 6e 3a 20 20 41 20 70 61 67 65  inition:  A page
14e01 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
14e02 20 66 69 6c 65 20 69 73 20 73 61 69 64 20 74 6f   file is said to
14e03 20 62 65 20 22 6f 76 65 72 77 72 69 74 65 61 62   be "overwriteab
14e04 6c 65 22 20 69 66 0a 2a 2a 20 6f 6e 65 20 6f 72  le" if.** one or
14e05 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 66 6f 6c   more of the fol
14e06 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 20  lowing are true 
14e07 61 62 6f 75 74 20 74 68 65 20 70 61 67 65 3a 0a  about the page:.
14e08 2a 2a 20 0a 2a 2a 20 20 20 20 20 28 61 29 20 20  ** .**     (a)  
14e09 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e  The original con
14e0a 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65  tent of the page
14e0b 20 61 73 20 69 74 20 77 61 73 20 61 74 20 74 68   as it was at th
14e0c 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a  e beginning of.*
14e0d 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 74  *          the t
14e0e 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 62  ransaction has b
14e0f 65 65 6e 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  een written into
14e10 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
14e11 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 20 20 20  urnal and.**    
14e12 20 20 20 20 20 20 73 79 6e 63 65 64 2e 0a 2a 2a        synced..**
14e13 20 0a 2a 2a 20 20 20 20 20 28 62 29 20 20 54 68   .**     (b)  Th
14e14 65 20 70 61 67 65 20 77 61 73 20 61 20 66 72 65  e page was a fre
14e15 65 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20  elist leaf page 
14e16 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
14e17 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  the transaction.
14e18 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 28 63 29 20  .** .**     (c) 
14e19 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   The page number
14e1a 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
14e1b 20 74 68 65 20 6c 61 72 67 65 73 74 20 70 61 67   the largest pag
14e1c 65 20 74 68 61 74 20 65 78 69 73 74 65 64 20 69  e that existed i
14e1d 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  n.**          th
14e1e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
14e1f 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
14e20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  the transaction.
14e21 0a 2a 2a 20 0a 2a 2a 20 28 31 29 20 41 20 70 61  .** .** (1) A pa
14e22 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ge of the databa
14e23 73 65 20 66 69 6c 65 20 69 73 20 6e 65 76 65 72  se file is never
14e24 20 6f 76 65 72 77 72 69 74 74 65 6e 20 75 6e 6c   overwritten unl
14e25 65 73 73 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a  ess one of the.*
14e26 2a 20 20 20 20 20 66 6f 6c 6c 6f 77 69 6e 67 20  *     following 
14e27 61 72 65 20 74 72 75 65 3a 0a 2a 2a 20 0a 2a 2a  are true:.** .**
14e28 20 20 20 20 20 28 61 29 20 54 68 65 20 70 61 67       (a) The pag
14e29 65 20 61 6e 64 20 61 6c 6c 20 6f 74 68 65 72 20  e and all other 
14e2a 70 61 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d  pages on the sam
14e2b 65 20 73 65 63 74 6f 72 20 61 72 65 20 6f 76 65  e sector are ove
14e2c 72 77 72 69 74 65 61 62 6c 65 2e 0a 2a 2a 20 0a  rwriteable..** .
14e2d 2a 2a 20 20 20 20 20 28 62 29 20 54 68 65 20 61  **     (b) The a
14e2e 74 6f 6d 69 63 20 70 61 67 65 20 77 72 69 74 65  tomic page write
14e2f 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
14e30 20 65 6e 61 62 6c 65 64 2c 20 61 6e 64 20 74 68   enabled, and th
14e31 65 20 65 6e 74 69 72 65 0a 2a 2a 20 20 20 20 20  e entire.**     
14e32 20 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20      transaction 
14e33 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 75  other than the u
14e34 70 64 61 74 65 20 6f 66 20 74 68 65 20 74 72 61  pdate of the tra
14e35 6e 73 61 63 74 69 6f 6e 20 73 65 71 75 65 6e 63  nsaction sequenc
14e36 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 6e 75 6d  e.**         num
14e37 62 65 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  ber consists of 
14e38 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 63 68  a single page ch
14e39 61 6e 67 65 2e 0a 2a 2a 20 0a 2a 2a 20 28 32 29  ange..** .** (2)
14e3a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   The content of 
14e3b 61 20 70 61 67 65 20 77 72 69 74 74 65 6e 20 69  a page written i
14e3c 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  nto the rollback
14e3d 20 6a 6f 75 72 6e 61 6c 20 65 78 61 63 74 6c 79   journal exactly
14e3e 20 6d 61 74 63 68 65 73 0a 2a 2a 20 20 20 20 20   matches.**     
14e3f 62 6f 74 68 20 74 68 65 20 63 6f 6e 74 65 6e 74  both the content
14e40 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
14e41 20 77 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61   when the rollba
14e42 63 6b 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 77  ck journal was w
14e43 72 69 74 74 65 6e 0a 2a 2a 20 20 20 20 20 61 6e  ritten.**     an
14e44 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 69 6e  d the content in
14e45 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 74   the database at
14e46 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
14e47 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a  f the current.**
14e48 20 20 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e       transaction
14e49 2e 0a 2a 2a 20 0a 2a 2a 20 28 33 29 20 57 72 69  ..** .** (3) Wri
14e4a 74 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62  tes to the datab
14e4b 61 73 65 20 66 69 6c 65 20 61 72 65 20 61 6e 20  ase file are an 
14e4c 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65  integer multiple
14e4d 20 6f 66 20 74 68 65 20 70 61 67 65 20 73 69 7a   of the page siz
14e4e 65 0a 2a 2a 20 20 20 20 20 69 6e 20 6c 65 6e 67  e.**     in leng
14e4f 74 68 20 61 6e 64 20 61 72 65 20 61 6c 69 67 6e  th and are align
14e50 65 64 20 6f 6e 20 61 20 70 61 67 65 20 62 6f 75  ed on a page bou
14e51 6e 64 61 72 79 2e 0a 2a 2a 20 0a 2a 2a 20 28 34  ndary..** .** (4
14e52 29 20 52 65 61 64 73 20 66 72 6f 6d 20 74 68 65  ) Reads from the
14e53 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
14e54 72 65 20 65 69 74 68 65 72 20 61 6c 69 67 6e 65  re either aligne
14e55 64 20 6f 6e 20 61 20 70 61 67 65 20 62 6f 75 6e  d on a page boun
14e56 64 61 72 79 20 61 6e 64 0a 2a 2a 20 20 20 20 20  dary and.**     
14e57 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69  an integer multi
14e58 70 6c 65 20 6f 66 20 74 68 65 20 70 61 67 65 20  ple of the page 
14e59 73 69 7a 65 20 69 6e 20 6c 65 6e 67 74 68 20 6f  size in length o
14e5a 72 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d  r are taken from
14e5b 20 74 68 65 0a 2a 2a 20 20 20 20 20 66 69 72 73   the.**     firs
14e5c 74 20 31 30 30 20 62 79 74 65 73 20 6f 66 20 74  t 100 bytes of t
14e5d 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
14e5e 2e 0a 2a 2a 20 0a 2a 2a 20 28 35 29 20 41 6c 6c  ..** .** (5) All
14e5f 20 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 64   writes to the d
14e60 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 72 65  atabase file are
14e61 20 73 79 6e 63 65 64 20 70 72 69 6f 72 20 74 6f   synced prior to
14e62 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
14e63 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 62 65 69  urnal.**     bei
14e64 6e 67 20 64 65 6c 65 74 65 64 2c 20 74 72 75 6e  ng deleted, trun
14e65 63 61 74 65 64 2c 20 6f 72 20 7a 65 72 6f 65 64  cated, or zeroed
14e66 2e 0a 2a 2a 20 0a 2a 2a 20 28 36 29 20 49 66 20  ..** .** (6) If 
14e67 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
14e68 20 66 69 6c 65 20 69 73 20 75 73 65 64 2c 20 74   file is used, t
14e69 68 65 6e 20 61 6c 6c 20 77 72 69 74 65 73 20 74  hen all writes t
14e6a 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
14e6b 69 6c 65 0a 2a 2a 20 20 20 20 20 61 72 65 20 73  ile.**     are s
14e6c 79 6e 63 65 64 20 70 72 69 6f 72 20 74 6f 20 74  ynced prior to t
14e6d 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
14e6e 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e  l being deleted.
14e6f 0a 2a 2a 20 0a 2a 2a 20 44 65 66 69 6e 69 74 69  .** .** Definiti
14e70 6f 6e 3a 20 54 77 6f 20 64 61 74 61 62 61 73 65  on: Two database
14e71 73 20 28 6f 72 20 74 68 65 20 73 61 6d 65 20 64  s (or the same d
14e72 61 74 61 62 61 73 65 20 61 74 20 74 77 6f 20 70  atabase at two p
14e73 6f 69 6e 74 73 20 69 74 20 74 69 6d 65 29 0a 2a  oints it time).*
14e74 2a 20 61 72 65 20 73 61 69 64 20 74 6f 20 62 65  * are said to be
14e75 20 22 6c 6f 67 69 63 61 6c 6c 79 20 65 71 75 69   "logically equi
14e76 76 61 6c 65 6e 74 22 20 69 66 20 74 68 65 79 20  valent" if they 
14e77 67 69 76 65 20 74 68 65 20 73 61 6d 65 20 61 6e  give the same an
14e78 73 77 65 72 20 74 6f 0a 2a 2a 20 61 6c 6c 20 71  swer to.** all q
14e79 75 65 72 69 65 73 2e 20 20 4e 6f 74 65 20 69 6e  ueries.  Note in
14e7a 20 70 61 72 74 69 63 75 6c 61 72 20 74 68 65 20   particular the 
14e7b 63 6f 6e 74 65 6e 74 20 6f 66 20 66 72 65 65 6c  content of freel
14e7c 69 73 74 20 6c 65 61 66 0a 2a 2a 20 70 61 67 65  ist leaf.** page
14e7d 73 20 63 61 6e 20 62 65 20 63 68 61 6e 67 65 64  s can be changed
14e7e 20 61 72 62 69 74 61 72 69 6c 79 20 77 69 74 68   arbitarily with
14e7f 6f 75 74 20 65 66 66 65 63 74 69 6e 67 20 74 68  out effecting th
14e80 65 20 6c 6f 67 69 63 61 6c 20 65 71 75 69 76 61  e logical equiva
14e81 6c 65 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20  lence.** of the 
14e82 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 0a 2a 2a  database..** .**
14e83 20 28 37 29 20 41 74 20 61 6e 79 20 74 69 6d 65   (7) At any time
14e84 2c 20 69 66 20 61 6e 79 20 73 75 62 73 65 74 2c  , if any subset,
14e85 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 65   including the e
14e86 6d 70 74 79 20 73 65 74 20 61 6e 64 20 74 68 65  mpty set and the
14e87 20 74 6f 74 61 6c 20 73 65 74 2c 0a 2a 2a 20 20   total set,.**  
14e88 20 20 20 6f 66 20 74 68 65 20 75 6e 73 79 6e 63     of the unsync
14e89 65 64 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20  ed changes to a 
14e8a 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
14e8b 20 61 72 65 20 72 65 6d 6f 76 65 64 20 61 6e 64   are removed and
14e8c 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 6a 6f 75   the .**     jou
14e8d 72 6e 61 6c 20 69 73 20 72 6f 6c 6c 65 64 20 62  rnal is rolled b
14e8e 61 63 6b 2c 20 74 68 65 20 72 65 73 75 6c 74 69  ack, the resulti
14e8f 6e 67 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ng database file
14e90 20 77 69 6c 6c 20 62 65 20 6c 6f 67 69 63 61 6c   will be logical
14e91 0a 2a 2a 20 20 20 20 20 65 71 75 69 76 61 6c 65  .**     equivale
14e92 6e 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  nt to the databa
14e93 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 62  se file at the b
14e94 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
14e95 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20  transaction..** 
14e96 0a 2a 2a 20 28 38 29 20 57 68 65 6e 20 61 20 74  .** (8) When a t
14e97 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f  ransaction is ro
14e98 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 20 78  lled back, the x
14e99 54 72 75 6e 63 61 74 65 20 6d 65 74 68 6f 64 20  Truncate method 
14e9a 6f 66 20 74 68 65 20 56 46 53 0a 2a 2a 20 20 20  of the VFS.**   
14e9b 20 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72    is called to r
14e9c 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 62  estore the datab
14e9d 61 73 65 20 66 69 6c 65 20 74 6f 20 74 68 65 20  ase file to the 
14e9e 73 61 6d 65 20 73 69 7a 65 20 69 74 20 77 61 73  same size it was
14e9f 20 61 74 0a 2a 2a 20 20 20 20 20 74 68 65 20 62   at.**     the b
14ea0 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
14ea1 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 28 49  transaction.  (I
14ea2 6e 20 73 6f 6d 65 20 56 46 53 65 73 2c 20 74 68  n some VFSes, th
14ea3 65 20 78 54 72 75 6e 63 61 74 65 0a 2a 2a 20 20  e xTruncate.**  
14ea4 20 20 20 6d 65 74 68 6f 64 20 69 73 20 61 20 6e     method is a n
14ea5 6f 2d 6f 70 2c 20 62 75 74 20 74 68 61 74 20 64  o-op, but that d
14ea6 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74  oes not change t
14ea7 68 65 20 66 61 63 74 20 74 68 65 20 53 51 4c 69  he fact the SQLi
14ea8 74 65 20 77 69 6c 6c 0a 2a 2a 20 20 20 20 20 69  te will.**     i
14ea9 6e 76 6f 6b 65 20 69 74 2e 29 0a 2a 2a 20 0a 2a  nvoke it.).** .*
14eaa 2a 20 28 39 29 20 57 68 65 6e 65 76 65 72 20 74  * (9) Whenever t
14eab 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
14eac 20 69 73 20 6d 6f 64 69 66 69 65 64 2c 20 61 74   is modified, at
14ead 20 6c 65 61 73 74 20 6f 6e 65 20 62 69 74 20 69   least one bit i
14eae 6e 20 74 68 65 20 72 61 6e 67 65 0a 2a 2a 20 20  n the range.**  
14eaf 20 20 20 6f 66 20 62 79 74 65 73 20 66 72 6f 6d     of bytes from
14eb0 20 32 34 20 74 68 72 6f 75 67 68 20 33 39 20 69   24 through 39 i
14eb1 6e 63 6c 75 73 69 76 65 20 77 69 6c 6c 20 62 65  nclusive will be
14eb2 20 63 68 61 6e 67 65 64 20 70 72 69 6f 72 20 74   changed prior t
14eb3 6f 20 72 65 6c 65 61 73 69 6e 67 0a 2a 2a 20 20  o releasing.**  
14eb4 20 20 20 74 68 65 20 45 58 43 4c 55 53 49 56 45     the EXCLUSIVE
14eb5 20 6c 6f 63 6b 2c 20 74 68 75 73 20 73 69 67 6e   lock, thus sign
14eb6 61 6c 69 6e 67 20 6f 74 68 65 72 20 63 6f 6e 6e  aling other conn
14eb7 65 63 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 73  ections on the s
14eb8 61 6d 65 0a 2a 2a 20 20 20 20 20 64 61 74 61 62  ame.**     datab
14eb9 61 73 65 20 74 6f 20 66 6c 75 73 68 20 74 68 65  ase to flush the
14eba 69 72 20 63 61 63 68 65 73 2e 0a 2a 2a 0a 2a 2a  ir caches..**.**
14ebb 20 28 31 30 29 20 54 68 65 20 70 61 74 74 65 72   (10) The patter
14ebc 6e 20 6f 66 20 62 69 74 73 20 69 6e 20 62 79 74  n of bits in byt
14ebd 65 73 20 32 34 20 74 68 72 6f 75 67 68 20 33 39  es 24 through 39
14ebe 20 73 68 61 6c 6c 20 6e 6f 74 20 72 65 70 65 61   shall not repea
14ebf 74 20 69 6e 20 6c 65 73 73 0a 2a 2a 20 20 20 20  t in less.**    
14ec0 20 20 74 68 61 6e 20 6f 6e 65 20 62 69 6c 6c 69    than one billi
14ec1 6f 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e  on transactions.
14ec2 0a 2a 2a 0a 2a 2a 20 28 31 31 29 20 41 20 64 61  .**.** (11) A da
14ec3 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 77  tabase file is w
14ec4 65 6c 6c 2d 66 6f 72 6d 65 64 20 61 74 20 74 68  ell-formed at th
14ec5 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20  e beginning and 
14ec6 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f  at the conclusio
14ec7 6e 0a 2a 2a 20 20 20 20 20 20 6f 66 20 65 76 65  n.**      of eve
14ec8 72 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  ry transaction..
14ec9 2a 2a 0a 2a 2a 20 28 31 32 29 20 41 6e 20 45 58  **.** (12) An EX
14eca 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20  CLUSIVE lock is 
14ecb 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61  held on the data
14ecc 62 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20 77  base file when w
14ecd 72 69 74 69 6e 67 20 74 6f 0a 2a 2a 20 20 20 20  riting to.**    
14ece 20 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66    the database f
14ecf 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 28 31 33 29 20  ile..**.** (13) 
14ed0 41 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 69 73  A SHARED lock is
14ed1 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74   held on the dat
14ed2 61 62 61 73 65 20 66 69 6c 65 20 77 68 69 6c 65  abase file while
14ed3 20 72 65 61 64 69 6e 67 20 61 6e 79 0a 2a 2a 20   reading any.** 
14ed4 20 20 20 20 20 63 6f 6e 74 65 6e 74 20 6f 75 74       content out
14ed5 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
14ed6 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   file..**.******
14ed7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14ed8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14ed9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14eda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14edb 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ********/../*.**
14edc 20 4d 61 63 72 6f 73 20 66 6f 72 20 74 72 6f 75   Macros for trou
14edd 62 6c 65 73 68 6f 6f 74 69 6e 67 2e 20 20 4e 6f  bleshooting.  No
14ede 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66  rmally turned of
14edf 66 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73  f.*/.#if 0.int s
14ee0 71 6c 69 74 65 33 50 61 67 65 72 54 72 61 63 65  qlite3PagerTrace
14ee1 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20  =1;  /* True to 
14ee2 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a  enable tracing *
14ee3 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  /.#define sqlite
14ee4 33 44 65 62 75 67 50 72 69 6e 74 66 20 70 72 69  3DebugPrintf pri
14ee5 6e 74 66 0a 23 64 65 66 69 6e 65 20 50 41 47 45  ntf.#define PAGE
14ee6 52 54 52 41 43 45 28 58 29 20 20 20 20 20 69 66  RTRACE(X)     if
14ee7 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72  ( sqlite3PagerTr
14ee8 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33 44 65  ace ){ sqlite3De
14ee9 62 75 67 50 72 69 6e 74 66 20 58 3b 20 7d 0a 23  bugPrintf X; }.#
14eea 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 50 41 47  else.#define PAG
14eeb 45 52 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69  ERTRACE(X).#endi
14eec 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  f../*.** The fol
14eed 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61 63 72 6f  lowing two macro
14eee 73 20 61 72 65 20 75 73 65 64 20 77 69 74 68 69  s are used withi
14eef 6e 20 74 68 65 20 50 41 47 45 52 54 52 41 43 45  n the PAGERTRACE
14ef0 28 29 20 6d 61 63 72 6f 73 20 61 62 6f 76 65 0a  () macros above.
14ef1 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f 75 74 20  ** to print out 
14ef2 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 73  file-descriptors
14ef3 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 45 52 49 44  . .**.** PAGERID
14ef4 28 29 20 74 61 6b 65 73 20 61 20 70 6f 69 6e 74  () takes a point
14ef5 65 72 20 74 6f 20 61 20 50 61 67 65 72 20 73 74  er to a Pager st
14ef6 72 75 63 74 20 61 73 20 69 74 73 20 61 72 67 75  ruct as its argu
14ef7 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 61 73 73  ment. The.** ass
14ef8 6f 63 69 61 74 65 64 20 66 69 6c 65 2d 64 65 73  ociated file-des
14ef9 63 72 69 70 74 6f 72 20 69 73 20 72 65 74 75 72  criptor is retur
14efa 6e 65 64 2e 20 46 49 4c 45 48 41 4e 44 4c 45 49  ned. FILEHANDLEI
14efb 44 28 29 20 74 61 6b 65 73 20 61 6e 20 73 71 6c  D() takes an sql
14efc 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 73 74 72  ite3_file.** str
14efd 75 63 74 20 61 73 20 69 74 73 20 61 72 67 75 6d  uct as its argum
14efe 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ent..*/.#define 
14eff 50 41 47 45 52 49 44 28 70 29 20 28 28 69 6e 74  PAGERID(p) ((int
14f00 29 28 70 2d 3e 66 64 29 29 0a 23 64 65 66 69 6e  )(p->fd)).#defin
14f01 65 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 66  e FILEHANDLEID(f
14f02 64 29 20 28 28 69 6e 74 29 66 64 29 0a 0a 2f 2a  d) ((int)fd)../*
14f03 0a 2a 2a 20 54 68 65 20 50 61 67 65 72 2e 65 53  .** The Pager.eS
14f04 74 61 74 65 20 76 61 72 69 61 62 6c 65 20 73 74  tate variable st
14f05 6f 72 65 73 20 74 68 65 20 63 75 72 72 65 6e 74  ores the current
14f06 20 27 73 74 61 74 65 27 20 6f 66 20 61 20 70 61   'state' of a pa
14f07 67 65 72 2e 20 41 0a 2a 2a 20 70 61 67 65 72 20  ger. A.** pager 
14f08 6d 61 79 20 62 65 20 69 6e 20 61 6e 79 20 6f 6e  may be in any on
14f09 65 20 6f 66 20 74 68 65 20 73 65 76 65 6e 20 73  e of the seven s
14f0a 74 61 74 65 73 20 73 68 6f 77 6e 20 69 6e 20 74  tates shown in t
14f0b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
14f0c 73 74 61 74 65 20 64 69 61 67 72 61 6d 2e 0a 2a  state diagram..*
14f0d 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  *.**            
14f0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14f0f 4f 50 45 4e 20 3c 2d 2d 2d 2d 2d 2d 2b 2d 2d 2d  OPEN <------+---
14f10 2d 2d 2d 2b 0a 2a 2a 20 20 20 20 20 20 20 20 20  ---+.**         
14f11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14f12 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 20 7c       |         |
14f13 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20        |.**      
14f14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14f15 20 20 20 20 20 20 20 20 56 20 20 20 20 20 20 20          V       
14f16 20 20 7c 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20    |      |.**   
14f17 20 20 20 20 20 20 20 20 20 20 20 20 2b 2d 2d 2d              +---
14f18 2d 2d 2d 2d 2d 2d 3e 20 52 45 41 44 45 52 2d 2d  ------> READER--
14f19 2d 2d 2d 2d 2d 2b 20 20 20 20 20 20 7c 0a 2a 2a  -----+      |.**
14f1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
14f1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
14f1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
14f1d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
14f1e 20 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20    |             
14f1f 20 56 20 20 20 20 20 20 20 20 20 20 20 20 20 20   V              
14f20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20    |.**          
14f21 20 20 20 20 20 7c 3c 2d 2d 2d 2d 2d 2d 2d 57 52       |<-------WR
14f22 49 54 45 52 5f 4c 4f 43 4b 45 44 2d 2d 2d 2d 2d  ITER_LOCKED-----
14f23 2d 3e 20 45 52 52 4f 52 0a 2a 2a 20 20 20 20 20  -> ERROR.**     
14f24 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 20 20            |     
14f25 20 20 20 20 20 20 20 20 20 7c 20 20 20 20 20 20           |      
14f26 20 20 20 20 20 20 20 20 20 20 5e 20 20 0a 2a 2a            ^  .**
14f27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
14f28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 56 20                V 
14f29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
14f2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
14f2b 20 20 7c 3c 2d 2d 2d 2d 2d 2d 57 52 49 54 45 52    |<------WRITER
14f2c 5f 43 41 43 48 45 4d 4f 44 2d 2d 2d 2d 2d 2d 2d  _CACHEMOD-------
14f2d 2d 3e 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ->|.**          
14f2e 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 20 20       |          
14f2f 20 20 20 20 7c 20 20 20 20 20 20 20 20 20 20 20      |           
14f30 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 20       |.**       
14f31 20 20 20 20 20 20 20 20 7c 20 20 20 20 20 20 20          |       
14f32 20 20 20 20 20 20 20 56 20 20 20 20 20 20 20 20         V        
14f33 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20          |.**    
14f34 20 20 20 20 20 20 20 20 20 20 20 7c 3c 2d 2d 2d             |<---
14f35 2d 2d 2d 2d 57 52 49 54 45 52 5f 44 42 4d 4f 44  ----WRITER_DBMOD
14f36 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 3e 7c 0a 2a 2a 20  ---------->|.** 
14f37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
14f38 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20               |  
14f39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 0a                |.
14f3a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
14f3b 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20   |              
14f3c 56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  V               
14f3d 20 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   |.**           
14f3e 20 20 20 20 2b 3c 2d 2d 2d 2d 2d 2d 57 52 49 54      +<------WRIT
14f3f 45 52 5f 46 49 4e 49 53 48 45 44 2d 2d 2d 2d 2d  ER_FINISHED-----
14f40 2d 2d 2d 3e 2b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4c  --->+.**.**.** L
14f41 69 73 74 20 6f 66 20 73 74 61 74 65 20 74 72 61  ist of state tra
14f42 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65  nsitions and the
14f43 20 43 20 5b 66 75 6e 63 74 69 6f 6e 5d 20 74 68   C [function] th
14f44 61 74 20 70 65 72 66 6f 72 6d 73 20 65 61 63 68  at performs each
14f45 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 4f 50 45 4e 20  :.** .**   OPEN 
14f46 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 3e 20               -> 
14f47 52 45 41 44 45 52 20 20 20 20 20 20 20 20 20 20  READER          
14f48 20 20 20 20 5b 73 71 6c 69 74 65 33 50 61 67 65      [sqlite3Page
14f49 72 53 68 61 72 65 64 4c 6f 63 6b 5d 0a 2a 2a 20  rSharedLock].** 
14f4a 20 20 52 45 41 44 45 52 20 20 20 20 20 20 20 20    READER        
14f4b 20 20 20 20 2d 3e 20 4f 50 45 4e 20 20 20 20 20      -> OPEN     
14f4c 20 20 20 20 20 20 20 20 20 20 20 5b 70 61 67 65             [page
14f4d 72 5f 75 6e 6c 6f 63 6b 5d 0a 2a 2a 0a 2a 2a 20  r_unlock].**.** 
14f4e 20 20 52 45 41 44 45 52 20 20 20 20 20 20 20 20    READER        
14f4f 20 20 20 20 2d 3e 20 57 52 49 54 45 52 5f 4c 4f      -> WRITER_LO
14f50 43 4b 45 44 20 20 20 20 20 20 20 5b 73 71 6c 69  CKED       [sqli
14f51 74 65 33 50 61 67 65 72 42 65 67 69 6e 5d 0a 2a  te3PagerBegin].*
14f52 2a 20 20 20 57 52 49 54 45 52 5f 4c 4f 43 4b 45  *   WRITER_LOCKE
14f53 44 20 20 20 20 20 2d 3e 20 57 52 49 54 45 52 5f  D     -> WRITER_
14f54 43 41 43 48 45 4d 4f 44 20 20 20 20 20 5b 70 61  CACHEMOD     [pa
14f55 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c  ger_open_journal
14f56 5d 0a 2a 2a 20 20 20 57 52 49 54 45 52 5f 43 41  ].**   WRITER_CA
14f57 43 48 45 4d 4f 44 20 20 20 2d 3e 20 57 52 49 54  CHEMOD   -> WRIT
14f58 45 52 5f 44 42 4d 4f 44 20 20 20 20 20 20 20 20  ER_DBMOD        
14f59 5b 73 79 6e 63 4a 6f 75 72 6e 61 6c 5d 0a 2a 2a  [syncJournal].**
14f5a 20 20 20 57 52 49 54 45 52 5f 44 42 4d 4f 44 20     WRITER_DBMOD 
14f5b 20 20 20 20 20 2d 3e 20 57 52 49 54 45 52 5f 46       -> WRITER_F
14f5c 49 4e 49 53 48 45 44 20 20 20 20 20 5b 73 71 6c  INISHED     [sql
14f5d 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50  ite3PagerCommitP
14f5e 68 61 73 65 4f 6e 65 5d 0a 2a 2a 20 20 20 57 52  haseOne].**   WR
14f5f 49 54 45 52 5f 2a 2a 2a 20 20 20 20 20 20 20 20  ITER_***        
14f60 2d 3e 20 52 45 41 44 45 52 20 20 20 20 20 20 20  -> READER       
14f61 20 20 20 20 20 20 20 5b 70 61 67 65 72 5f 65 6e         [pager_en
14f62 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 5d 0a 2a  d_transaction].*
14f63 2a 0a 2a 2a 20 20 20 57 52 49 54 45 52 5f 2a 2a  *.**   WRITER_**
14f64 2a 20 20 20 20 20 20 20 20 2d 3e 20 45 52 52 4f  *        -> ERRO
14f65 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  R               
14f66 5b 70 61 67 65 72 5f 65 72 72 6f 72 5d 0a 2a 2a  [pager_error].**
14f67 20 20 20 45 52 52 4f 52 20 20 20 20 20 20 20 20     ERROR        
14f68 20 20 20 20 20 2d 3e 20 4f 50 45 4e 20 20 20 20       -> OPEN    
14f69 20 20 20 20 20 20 20 20 20 20 20 20 5b 70 61 67              [pag
14f6a 65 72 5f 75 6e 6c 6f 63 6b 5d 0a 2a 2a 20 0a 2a  er_unlock].** .*
14f6b 2a 0a 2a 2a 20 20 4f 50 45 4e 3a 0a 2a 2a 0a 2a  *.**  OPEN:.**.*
14f6c 2a 20 20 20 20 54 68 65 20 70 61 67 65 72 20 73  *    The pager s
14f6d 74 61 72 74 73 20 75 70 20 69 6e 20 74 68 69 73  tarts up in this
14f6e 20 73 74 61 74 65 2e 20 4e 6f 74 68 69 6e 67 20   state. Nothing 
14f6f 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 69 6e  is guaranteed in
14f70 20 74 68 69 73 0a 2a 2a 20 20 20 20 73 74 61 74   this.**    stat
14f71 65 20 2d 20 74 68 65 20 66 69 6c 65 20 6d 61 79  e - the file may
14f72 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 6c   or may not be l
14f73 6f 63 6b 65 64 20 61 6e 64 20 74 68 65 20 64 61  ocked and the da
14f74 74 61 62 61 73 65 20 73 69 7a 65 20 69 73 0a 2a  tabase size is.*
14f75 2a 20 20 20 20 75 6e 6b 6e 6f 77 6e 2e 20 54 68  *    unknown. Th
14f76 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 6e  e database may n
14f77 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 72  ot be read or wr
14f78 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  itten..**.**    
14f79 2a 20 4e 6f 20 72 65 61 64 20 6f 72 20 77 72 69  * No read or wri
14f7a 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  te transaction i
14f7b 73 20 61 63 74 69 76 65 2e 0a 2a 2a 20 20 20 20  s active..**    
14f7c 2a 20 41 6e 79 20 6c 6f 63 6b 2c 20 6f 72 20 6e  * Any lock, or n
14f7d 6f 20 6c 6f 63 6b 20 61 74 20 61 6c 6c 2c 20 6d  o lock at all, m
14f7e 61 79 20 62 65 20 68 65 6c 64 20 6f 6e 20 74 68  ay be held on th
14f7f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
14f80 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 64 62 53  .**    * The dbS
14f81 69 7a 65 2c 20 64 62 4f 72 69 67 53 69 7a 65 20  ize, dbOrigSize 
14f82 61 6e 64 20 64 62 46 69 6c 65 53 69 7a 65 20 76  and dbFileSize v
14f83 61 72 69 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74  ariables may not
14f84 20 62 65 20 74 72 75 73 74 65 64 2e 0a 2a 2a 0a   be trusted..**.
14f85 2a 2a 20 20 52 45 41 44 45 52 3a 0a 2a 2a 0a 2a  **  READER:.**.*
14f86 2a 20 20 20 20 49 6e 20 74 68 69 73 20 73 74 61  *    In this sta
14f87 74 65 20 61 6c 6c 20 74 68 65 20 72 65 71 75 69  te all the requi
14f88 72 65 6d 65 6e 74 73 20 66 6f 72 20 72 65 61 64  rements for read
14f89 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65  ing the database
14f8a 20 69 6e 20 0a 2a 2a 20 20 20 20 72 6f 6c 6c 62   in .**    rollb
14f8b 61 63 6b 20 28 6e 6f 6e 2d 57 41 4c 29 20 6d 6f  ack (non-WAL) mo
14f8c 64 65 20 61 72 65 20 6d 65 74 2e 20 55 6e 6c 65  de are met. Unle
14f8d 73 73 20 74 68 65 20 70 61 67 65 72 20 69 73 20  ss the pager is 
14f8e 28 6f 72 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20  (or recently.** 
14f8f 20 20 20 77 61 73 29 20 69 6e 20 65 78 63 6c 75     was) in exclu
14f90 73 69 76 65 2d 6c 6f 63 6b 69 6e 67 20 6d 6f 64  sive-locking mod
14f91 65 2c 20 61 20 75 73 65 72 2d 6c 65 76 65 6c 20  e, a user-level 
14f92 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e  read transaction
14f93 20 69 73 20 0a 2a 2a 20 20 20 20 6f 70 65 6e 2e   is .**    open.
14f94 20 54 68 65 20 64 61 74 61 62 61 73 65 20 73 69   The database si
14f95 7a 65 20 69 73 20 6b 6e 6f 77 6e 20 69 6e 20 74  ze is known in t
14f96 68 69 73 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a  his state..**.**
14f97 20 20 20 20 41 20 63 6f 6e 6e 65 63 74 69 6f 6e      A connection
14f98 20 72 75 6e 6e 69 6e 67 20 77 69 74 68 20 6c 6f   running with lo
14f99 63 6b 69 6e 67 5f 6d 6f 64 65 3d 6e 6f 72 6d 61  cking_mode=norma
14f9a 6c 20 65 6e 74 65 72 73 20 74 68 69 73 20 73 74  l enters this st
14f9b 61 74 65 20 77 68 65 6e 0a 2a 2a 20 20 20 20 69  ate when.**    i
14f9c 74 20 6f 70 65 6e 73 20 61 20 72 65 61 64 2d 74  t opens a read-t
14f9d 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68  ransaction on th
14f9e 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 72  e database and r
14f9f 65 74 75 72 6e 73 20 74 6f 20 73 74 61 74 65 0a  eturns to state.
14fa0 2a 2a 20 20 20 20 4f 50 45 4e 20 61 66 74 65 72  **    OPEN after
14fa1 20 74 68 65 20 72 65 61 64 2d 74 72 61 6e 73 61   the read-transa
14fa2 63 74 69 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74  ction is complet
14fa3 65 64 2e 20 48 6f 77 65 76 65 72 20 61 20 63 6f  ed. However a co
14fa4 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 72  nnection.**    r
14fa5 75 6e 6e 69 6e 67 20 69 6e 20 6c 6f 63 6b 69 6e  unning in lockin
14fa6 67 5f 6d 6f 64 65 3d 65 78 63 6c 75 73 69 76 65  g_mode=exclusive
14fa7 20 28 69 6e 63 6c 75 64 69 6e 67 20 74 65 6d 70   (including temp
14fa8 20 64 61 74 61 62 61 73 65 73 29 20 72 65 6d 61   databases) rema
14fa9 69 6e 73 20 69 6e 0a 2a 2a 20 20 20 20 74 68 69  ins in.**    thi
14faa 73 20 73 74 61 74 65 20 65 76 65 6e 20 61 66 74  s state even aft
14fab 65 72 20 74 68 65 20 72 65 61 64 2d 74 72 61 6e  er the read-tran
14fac 73 61 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65  saction is close
14fad 64 2e 20 54 68 65 20 6f 6e 6c 79 20 77 61 79 0a  d. The only way.
14fae 2a 2a 20 20 20 20 61 20 6c 6f 63 6b 69 6e 67 5f  **    a locking_
14faf 6d 6f 64 65 3d 65 78 63 6c 75 73 69 76 65 20 63  mode=exclusive c
14fb0 6f 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 20 74 72  onnection can tr
14fb1 61 6e 73 69 74 69 6f 6e 20 66 72 6f 6d 20 52 45  ansition from RE
14fb2 41 44 45 52 20 74 6f 20 4f 50 45 4e 0a 2a 2a 20  ADER to OPEN.** 
14fb3 20 20 20 69 73 20 76 69 61 20 74 68 65 20 45 52     is via the ER
14fb4 52 4f 52 20 73 74 61 74 65 20 28 73 65 65 20 62  ROR state (see b
14fb5 65 6c 6f 77 29 2e 0a 2a 2a 20 0a 2a 2a 20 20 20  elow)..** .**   
14fb6 20 2a 20 41 20 72 65 61 64 20 74 72 61 6e 73 61   * A read transa
14fb7 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 61 63 74  ction may be act
14fb8 69 76 65 20 28 62 75 74 20 61 20 77 72 69 74 65  ive (but a write
14fb9 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e  -transaction can
14fba 6e 6f 74 29 2e 0a 2a 2a 20 20 20 20 2a 20 41 20  not)..**    * A 
14fbb 53 48 41 52 45 44 20 6f 72 20 67 72 65 61 74 65  SHARED or greate
14fbc 72 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f  r lock is held o
14fbd 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
14fbe 69 6c 65 2e 0a 2a 2a 20 20 20 20 2a 20 54 68 65  ile..**    * The
14fbf 20 64 62 53 69 7a 65 20 76 61 72 69 61 62 6c 65   dbSize variable
14fc0 20 6d 61 79 20 62 65 20 74 72 75 73 74 65 64 20   may be trusted 
14fc1 28 65 76 65 6e 20 69 66 20 61 20 75 73 65 72 2d  (even if a user-
14fc2 6c 65 76 65 6c 20 72 65 61 64 20 0a 2a 2a 20 20  level read .**  
14fc3 20 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20      transaction 
14fc4 69 73 20 6e 6f 74 20 61 63 74 69 76 65 29 2e 20  is not active). 
14fc5 54 68 65 20 64 62 4f 72 69 67 53 69 7a 65 20 61  The dbOrigSize a
14fc6 6e 64 20 64 62 46 69 6c 65 53 69 7a 65 20 76 61  nd dbFileSize va
14fc7 72 69 61 62 6c 65 73 0a 2a 2a 20 20 20 20 20 20  riables.**      
14fc8 6d 61 79 20 6e 6f 74 20 62 65 20 74 72 75 73 74  may not be trust
14fc9 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74  ed at this point
14fca 2e 0a 2a 2a 20 20 20 20 2a 20 49 66 20 74 68 65  ..**    * If the
14fcb 20 64 61 74 61 62 61 73 65 20 69 73 20 61 20 57   database is a W
14fcc 41 4c 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  AL database, the
14fcd 6e 20 74 68 65 20 57 41 4c 20 63 6f 6e 6e 65 63  n the WAL connec
14fce 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2e 0a 2a 2a  tion is open..**
14fcf 20 20 20 20 2a 20 45 76 65 6e 20 69 66 20 61 20      * Even if a 
14fd0 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e  read-transaction
14fd1 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 20 69 74   is not open, it
14fd2 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
14fd3 68 61 74 20 0a 2a 2a 20 20 20 20 20 20 74 68 65  hat .**      the
14fd4 72 65 20 69 73 20 6e 6f 20 68 6f 74 2d 6a 6f 75  re is no hot-jou
14fd5 72 6e 61 6c 20 69 6e 20 74 68 65 20 66 69 6c 65  rnal in the file
14fd6 2d 73 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 20  -system..**.**  
14fd7 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 3a 0a 2a  WRITER_LOCKED:.*
14fd8 2a 0a 2a 2a 20 20 20 20 54 68 65 20 70 61 67 65  *.**    The page
14fd9 72 20 6d 6f 76 65 73 20 74 6f 20 74 68 69 73 20  r moves to this 
14fda 73 74 61 74 65 20 66 72 6f 6d 20 52 45 41 44 45  state from READE
14fdb 52 20 77 68 65 6e 20 61 20 77 72 69 74 65 2d 74  R when a write-t
14fdc 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20  ransaction.**   
14fdd 20 69 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64   is first opened
14fde 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
14fdf 2e 20 49 6e 20 57 52 49 54 45 52 5f 4c 4f 43 4b  . In WRITER_LOCK
14fe0 45 44 20 73 74 61 74 65 2c 20 61 6c 6c 20 6c 6f  ED state, all lo
14fe1 63 6b 73 20 0a 2a 2a 20 20 20 20 72 65 71 75 69  cks .**    requi
14fe2 72 65 64 20 74 6f 20 73 74 61 72 74 20 61 20 77  red to start a w
14fe3 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
14fe4 20 61 72 65 20 68 65 6c 64 2c 20 62 75 74 20 6e   are held, but n
14fe5 6f 20 61 63 74 75 61 6c 20 0a 2a 2a 20 20 20 20  o actual .**    
14fe6 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 74 6f  modifications to
14fe7 20 74 68 65 20 63 61 63 68 65 20 6f 72 20 64 61   the cache or da
14fe8 74 61 62 61 73 65 20 68 61 76 65 20 74 61 6b 65  tabase have take
14fe9 6e 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 20  n place..**.**  
14fea 20 20 49 6e 20 72 6f 6c 6c 62 61 63 6b 20 6d 6f    In rollback mo
14feb 64 65 2c 20 61 20 52 45 53 45 52 56 45 44 20 6f  de, a RESERVED o
14fec 72 20 28 69 66 20 74 68 65 20 74 72 61 6e 73 61  r (if the transa
14fed 63 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64  ction was opened
14fee 20 77 69 74 68 20 0a 2a 2a 20 20 20 20 42 45 47   with .**    BEG
14fef 49 4e 20 45 58 43 4c 55 53 49 56 45 29 20 45 58  IN EXCLUSIVE) EX
14ff0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20  CLUSIVE lock is 
14ff1 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20  obtained on the 
14ff2 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 68  database file wh
14ff3 65 6e 0a 2a 2a 20 20 20 20 6d 6f 76 69 6e 67 20  en.**    moving 
14ff4 74 6f 20 74 68 69 73 20 73 74 61 74 65 2c 20 62  to this state, b
14ff5 75 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ut the journal f
14ff6 69 6c 65 20 69 73 20 6e 6f 74 20 77 72 69 74 74  ile is not writt
14ff7 65 6e 20 74 6f 20 6f 72 20 6f 70 65 6e 65 64 20  en to or opened 
14ff8 0a 2a 2a 20 20 20 20 74 6f 20 69 6e 20 74 68 69  .**    to in thi
14ff9 73 20 73 74 61 74 65 2e 20 49 66 20 74 68 65 20  s state. If the 
14ffa 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63  transaction is c
14ffb 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c  ommitted or roll
14ffc 65 64 20 62 61 63 6b 20 77 68 69 6c 65 20 0a 2a  ed back while .*
14ffd 2a 20 20 20 20 69 6e 20 57 52 49 54 45 52 5f 4c  *    in WRITER_L
14ffe 4f 43 4b 45 44 20 73 74 61 74 65 2c 20 61 6c 6c  OCKED state, all
14fff 20 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65   that is require
15000 64 20 69 73 20 74 6f 20 75 6e 6c 6f 63 6b 20 74  d is to unlock t
15001 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20  he database .** 
15002 20 20 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20     file..**.**  
15003 20 20 49 4e 20 57 41 4c 20 6d 6f 64 65 2c 20 57    IN WAL mode, W
15004 61 6c 42 65 67 69 6e 57 72 69 74 65 54 72 61 6e  alBeginWriteTran
15005 73 61 63 74 69 6f 6e 28 29 20 69 73 20 63 61 6c  saction() is cal
15006 6c 65 64 20 74 6f 20 6c 6f 63 6b 20 74 68 65 20  led to lock the 
15007 6c 6f 67 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20  log file..**    
15008 49 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  If the connectio
15009 6e 20 69 73 20 72 75 6e 6e 69 6e 67 20 77 69 74  n is running wit
1500a 68 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65  h locking_mode=e
1500b 78 63 6c 75 73 69 76 65 2c 20 61 6e 20 61 74 74  xclusive, an att
1500c 65 6d 70 74 0a 2a 2a 20 20 20 20 69 73 20 6d 61  empt.**    is ma
1500d 64 65 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 20  de to obtain an 
1500e 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f  EXCLUSIVE lock o
1500f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
15010 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  ile..**.**    * 
15011 41 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  A write transact
15012 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a  ion is active..*
15013 2a 20 20 20 20 2a 20 49 66 20 74 68 65 20 63 6f  *    * If the co
15014 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e  nnection is open
15015 20 69 6e 20 72 6f 6c 6c 62 61 63 6b 2d 6d 6f 64   in rollback-mod
15016 65 2c 20 61 20 52 45 53 45 52 56 45 44 20 6f 72  e, a RESERVED or
15017 20 67 72 65 61 74 65 72 20 0a 2a 2a 20 20 20 20   greater .**    
15018 20 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f    lock is held o
15019 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
1501a 69 6c 65 2e 0a 2a 2a 20 20 20 20 2a 20 49 66 20  ile..**    * If 
1501b 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  the connection i
1501c 73 20 6f 70 65 6e 20 69 6e 20 57 41 4c 2d 6d 6f  s open in WAL-mo
1501d 64 65 2c 20 61 20 57 41 4c 20 77 72 69 74 65 20  de, a WAL write 
1501e 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20  transaction.**  
1501f 20 20 20 20 69 73 20 6f 70 65 6e 20 28 69 2e 65      is open (i.e
15020 2e 20 73 71 6c 69 74 65 33 57 61 6c 42 65 67 69  . sqlite3WalBegi
15021 6e 57 72 69 74 65 54 72 61 6e 73 61 63 74 69 6f  nWriteTransactio
15022 6e 28 29 20 68 61 73 20 62 65 65 6e 20 73 75 63  n() has been suc
15023 63 65 73 73 66 75 6c 6c 79 0a 2a 2a 20 20 20 20  cessfully.**    
15024 20 20 63 61 6c 6c 65 64 29 2e 0a 2a 2a 20 20 20    called)..**   
15025 20 2a 20 54 68 65 20 64 62 53 69 7a 65 2c 20 64   * The dbSize, d
15026 62 4f 72 69 67 53 69 7a 65 20 61 6e 64 20 64 62  bOrigSize and db
15027 46 69 6c 65 53 69 7a 65 20 76 61 72 69 61 62 6c  FileSize variabl
15028 65 73 20 61 72 65 20 61 6c 6c 20 76 61 6c 69 64  es are all valid
15029 2e 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 63 6f  ..**    * The co
1502a 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61  ntents of the pa
1502b 67 65 72 20 63 61 63 68 65 20 68 61 76 65 20 6e  ger cache have n
1502c 6f 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  ot been modified
1502d 2e 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 6a 6f  ..**    * The jo
1502e 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f  urnal file may o
1502f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f 70 65  r may not be ope
15030 6e 2e 0a 2a 2a 20 20 20 20 2a 20 4e 6f 74 68 69  n..**    * Nothi
15031 6e 67 20 28 6e 6f 74 20 65 76 65 6e 20 74 68 65  ng (not even the
15032 20 66 69 72 73 74 20 68 65 61 64 65 72 29 20 68   first header) h
15033 61 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  as been written 
15034 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a  to the journal..
15035 2a 2a 0a 2a 2a 20 20 57 52 49 54 45 52 5f 43 41  **.**  WRITER_CA
15036 43 48 45 4d 4f 44 3a 0a 2a 2a 0a 2a 2a 20 20 20  CHEMOD:.**.**   
15037 20 41 20 70 61 67 65 72 20 6d 6f 76 65 73 20 66   A pager moves f
15038 72 6f 6d 20 57 52 49 54 45 52 5f 4c 4f 43 4b 45  rom WRITER_LOCKE
15039 44 20 73 74 61 74 65 20 74 6f 20 74 68 69 73 20  D state to this 
1503a 73 74 61 74 65 20 77 68 65 6e 20 61 20 70 61 67  state when a pag
1503b 65 20 69 73 0a 2a 2a 20 20 20 20 66 69 72 73 74  e is.**    first
1503c 20 6d 6f 64 69 66 69 65 64 20 62 79 20 74 68 65   modified by the
1503d 20 75 70 70 65 72 20 6c 61 79 65 72 2e 20 49 6e   upper layer. In
1503e 20 72 6f 6c 6c 62 61 63 6b 20 6d 6f 64 65 20 74   rollback mode t
1503f 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a  he journal file.
15040 2a 2a 20 20 20 20 69 73 20 6f 70 65 6e 65 64 20  **    is opened 
15041 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c  (if it is not al
15042 72 65 61 64 79 20 6f 70 65 6e 29 20 61 6e 64 20  ready open) and 
15043 61 20 68 65 61 64 65 72 20 77 72 69 74 74 65 6e  a header written
15044 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 73 74   to the.**    st
15045 61 72 74 20 6f 66 20 69 74 2e 20 54 68 65 20 64  art of it. The d
15046 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20  atabase file on 
15047 64 69 73 6b 20 68 61 73 20 6e 6f 74 20 62 65 65  disk has not bee
15048 6e 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a  n modified..**.*
15049 2a 20 20 20 20 2a 20 41 20 77 72 69 74 65 20 74  *    * A write t
1504a 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
1504b 74 69 76 65 2e 0a 2a 2a 20 20 20 20 2a 20 41 20  tive..**    * A 
1504c 52 45 53 45 52 56 45 44 20 6f 72 20 67 72 65 61  RESERVED or grea
1504d 74 65 72 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  ter lock is held
1504e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
1504f 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 2a 20 54   file..**    * T
15050 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
15051 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20  is open and the 
15052 66 69 72 73 74 20 68 65 61 64 65 72 20 68 61 73  first header has
15053 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 0a 2a   been written .*
15054 2a 20 20 20 20 20 20 74 6f 20 69 74 2c 20 62 75  *      to it, bu
15055 74 20 74 68 65 20 68 65 61 64 65 72 20 68 61 73  t the header has
15056 20 6e 6f 74 20 62 65 65 6e 20 73 79 6e 63 65 64   not been synced
15057 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 20 20 20 20   to disk..**    
15058 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  * The contents o
15059 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  f the page cache
1505a 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66   have been modif
1505b 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 57 52 49 54  ied..**.**  WRIT
1505c 45 52 5f 44 42 4d 4f 44 3a 0a 2a 2a 0a 2a 2a 20  ER_DBMOD:.**.** 
1505d 20 20 20 54 68 65 20 70 61 67 65 72 20 74 72 61     The pager tra
1505e 6e 73 69 74 69 6f 6e 73 20 66 72 6f 6d 20 57 52  nsitions from WR
1505f 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 20 69 6e  ITER_CACHEMOD in
15060 74 6f 20 57 52 49 54 45 52 5f 44 42 4d 4f 44 20  to WRITER_DBMOD 
15061 73 74 61 74 65 0a 2a 2a 20 20 20 20 77 68 65 6e  state.**    when
15062 20 69 74 20 6d 6f 64 69 66 69 65 73 20 74 68 65   it modifies the
15063 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
15064 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
15065 57 41 4c 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a  WAL connections.
15066 2a 2a 20 20 20 20 6e 65 76 65 72 20 65 6e 74 65  **    never ente
15067 72 20 74 68 69 73 20 73 74 61 74 65 20 28 73 69  r this state (si
15068 6e 63 65 20 74 68 65 79 20 64 6f 20 6e 6f 74 20  nce they do not 
15069 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62  modify the datab
1506a 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 20 20 20  ase file,.**    
1506b 6a 75 73 74 20 74 68 65 20 6c 6f 67 20 66 69 6c  just the log fil
1506c 65 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 41  e)..**.**    * A
1506d 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
1506e 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2a  on is active..**
1506f 20 20 20 20 2a 20 41 6e 20 45 58 43 4c 55 53 49      * An EXCLUSI
15070 56 45 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f  VE or greater lo
15071 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68  ck is held on th
15072 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
15073 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 6a 6f 75  .**    * The jou
15074 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65  rnal file is ope
15075 6e 20 61 6e 64 20 74 68 65 20 66 69 72 73 74 20  n and the first 
15076 68 65 61 64 65 72 20 68 61 73 20 62 65 65 6e 20  header has been 
15077 77 72 69 74 74 65 6e 20 0a 2a 2a 20 20 20 20 20  written .**     
15078 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f 20 64   and synced to d
15079 69 73 6b 2e 0a 2a 2a 20 20 20 20 2a 20 54 68 65  isk..**    * The
1507a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
1507b 20 70 61 67 65 20 63 61 63 68 65 20 68 61 76 65   page cache have
1507c 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 28   been modified (
1507d 61 6e 64 20 70 6f 73 73 69 62 6c 79 0a 2a 2a 20  and possibly.** 
1507e 20 20 20 20 20 77 72 69 74 74 65 6e 20 74 6f 20       written to 
1507f 64 69 73 6b 29 2e 0a 2a 2a 0a 2a 2a 20 20 57 52  disk)..**.**  WR
15080 49 54 45 52 5f 46 49 4e 49 53 48 45 44 3a 0a 2a  ITER_FINISHED:.*
15081 2a 0a 2a 2a 20 20 20 20 49 74 20 69 73 20 6e 6f  *.**    It is no
15082 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61  t possible for a
15083 20 57 41 4c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   WAL connection 
15084 74 6f 20 65 6e 74 65 72 20 74 68 69 73 20 73 74  to enter this st
15085 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 41 20  ate..**.**    A 
15086 72 6f 6c 6c 62 61 63 6b 2d 6d 6f 64 65 20 70 61  rollback-mode pa
15087 67 65 72 20 63 68 61 6e 67 65 73 20 74 6f 20 57  ger changes to W
15088 52 49 54 45 52 5f 46 49 4e 49 53 48 45 44 20 73  RITER_FINISHED s
15089 74 61 74 65 20 66 72 6f 6d 20 57 52 49 54 45 52  tate from WRITER
1508a 5f 44 42 4d 4f 44 0a 2a 2a 20 20 20 20 73 74 61  _DBMOD.**    sta
1508b 74 65 20 61 66 74 65 72 20 74 68 65 20 65 6e 74  te after the ent
1508c 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ire transaction 
1508d 68 61 73 20 62 65 65 6e 20 73 75 63 63 65 73 73  has been success
1508e 66 75 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e  fully written in
1508f 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 64 61 74  to the.**    dat
15090 61 62 61 73 65 20 66 69 6c 65 2e 20 49 6e 20 74  abase file. In t
15091 68 69 73 20 73 74 61 74 65 20 74 68 65 20 74 72  his state the tr
15092 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 62 65  ansaction may be
15093 20 63 6f 6d 6d 69 74 74 65 64 20 73 69 6d 70 6c   committed simpl
15094 79 0a 2a 2a 20 20 20 20 62 79 20 66 69 6e 61 6c  y.**    by final
15095 69 7a 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61  izing the journa
15096 6c 20 66 69 6c 65 2e 20 4f 6e 63 65 20 69 6e 20  l file. Once in 
15097 57 52 49 54 45 52 5f 46 49 4e 49 53 48 45 44 20  WRITER_FINISHED 
15098 73 74 61 74 65 2c 20 69 74 20 69 73 20 0a 2a 2a  state, it is .**
15099 20 20 20 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65      not possible
1509a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64   to modify the d
1509b 61 74 61 62 61 73 65 20 66 75 72 74 68 65 72 2e  atabase further.
1509c 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
1509d 74 68 65 20 75 70 70 65 72 20 0a 2a 2a 20 20 20  the upper .**   
1509e 20 6c 61 79 65 72 20 6d 75 73 74 20 65 69 74 68   layer must eith
1509f 65 72 20 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c  er commit or rol
150a0 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61  lback the transa
150a1 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  ction..**.**    
150a2 2a 20 41 20 77 72 69 74 65 20 74 72 61 6e 73 61  * A write transa
150a3 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e  ction is active.
150a4 0a 2a 2a 20 20 20 20 2a 20 41 6e 20 45 58 43 4c  .**    * An EXCL
150a5 55 53 49 56 45 20 6f 72 20 67 72 65 61 74 65 72  USIVE or greater
150a6 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e   lock is held on
150a7 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
150a8 6c 65 2e 0a 2a 2a 20 20 20 20 2a 20 41 6c 6c 20  le..**    * All 
150a9 77 72 69 74 69 6e 67 20 61 6e 64 20 73 79 6e 63  writing and sync
150aa 69 6e 67 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 61  ing of journal a
150ab 6e 64 20 64 61 74 61 62 61 73 65 20 64 61 74 61  nd database data
150ac 20 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a   has finished..*
150ad 2a 20 20 20 20 20 20 49 66 20 6e 6f 20 65 72 72  *      If no err
150ae 6f 72 20 6f 63 63 75 72 65 64 2c 20 61 6c 6c 20  or occured, all 
150af 74 68 61 74 20 72 65 6d 61 69 6e 73 20 69 73 20  that remains is 
150b0 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20  to finalize the 
150b1 6a 6f 75 72 6e 61 6c 20 74 6f 0a 2a 2a 20 20 20  journal to.**   
150b2 20 20 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 72     commit the tr
150b3 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 61 6e  ansaction. If an
150b4 20 65 72 72 6f 72 20 64 69 64 20 6f 63 63 75 72   error did occur
150b5 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 77 69 6c  , the caller wil
150b6 6c 20 6e 65 65 64 0a 2a 2a 20 20 20 20 20 20 74  l need.**      t
150b7 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74  o rollback the t
150b8 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a  ransaction. .**.
150b9 2a 2a 20 20 45 52 52 4f 52 3a 0a 2a 2a 0a 2a 2a  **  ERROR:.**.**
150ba 20 20 20 20 54 68 65 20 45 52 52 4f 52 20 73 74      The ERROR st
150bb 61 74 65 20 69 73 20 65 6e 74 65 72 65 64 20 77  ate is entered w
150bc 68 65 6e 20 61 6e 20 49 4f 20 6f 72 20 64 69 73  hen an IO or dis
150bd 6b 2d 66 75 6c 6c 20 65 72 72 6f 72 20 28 69 6e  k-full error (in
150be 63 6c 75 64 69 6e 67 0a 2a 2a 20 20 20 20 53 51  cluding.**    SQ
150bf 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d  LITE_IOERR_NOMEM
150c0 29 20 6f 63 63 75 72 73 20 61 74 20 61 20 70 6f  ) occurs at a po
150c1 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20  int in the code 
150c2 74 68 61 74 20 6d 61 6b 65 73 20 69 74 20 0a 2a  that makes it .*
150c3 2a 20 20 20 20 64 69 66 66 69 63 75 6c 74 20 74  *    difficult t
150c4 6f 20 62 65 20 73 75 72 65 20 74 68 61 74 20 74  o be sure that t
150c5 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67  he in-memory pag
150c6 65 72 20 73 74 61 74 65 20 28 63 61 63 68 65 20  er state (cache 
150c7 63 6f 6e 74 65 6e 74 73 2c 20 0a 2a 2a 20 20 20  contents, .**   
150c8 20 64 62 20 73 69 7a 65 20 65 74 63 2e 29 20 61   db size etc.) a
150c9 72 65 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 69  re consistent wi
150ca 74 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  th the contents 
150cb 6f 66 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  of the file-syst
150cc 65 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 54 65 6d  em..**.**    Tem
150cd 70 6f 72 61 72 79 20 70 61 67 65 72 20 66 69 6c  porary pager fil
150ce 65 73 20 6d 61 79 20 65 6e 74 65 72 20 74 68 65  es may enter the
150cf 20 45 52 52 4f 52 20 73 74 61 74 65 2c 20 62 75   ERROR state, bu
150d0 74 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65  t in-memory page
150d1 72 73 0a 2a 2a 20 20 20 20 63 61 6e 6e 6f 74 2e  rs.**    cannot.
150d2 0a 2a 2a 0a 2a 2a 20 20 20 20 46 6f 72 20 65 78  .**.**    For ex
150d3 61 6d 70 6c 65 2c 20 69 66 20 61 6e 20 49 4f 20  ample, if an IO 
150d4 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69  error occurs whi
150d5 6c 65 20 70 65 72 66 6f 72 6d 69 6e 67 20 61 20  le performing a 
150d6 72 6f 6c 6c 62 61 63 6b 2c 20 0a 2a 2a 20 20 20  rollback, .**   
150d7 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
150d8 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20   the page-cache 
150d9 6d 61 79 20 62 65 20 6c 65 66 74 20 69 6e 20 61  may be left in a
150da 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73  n inconsistent s
150db 74 61 74 65 2e 0a 2a 2a 20 20 20 20 41 74 20 74  tate..**    At t
150dc 68 69 73 20 70 6f 69 6e 74 20 69 74 20 77 6f 75  his point it wou
150dd 6c 64 20 62 65 20 64 61 6e 67 65 72 6f 75 73 20  ld be dangerous 
150de 74 6f 20 63 68 61 6e 67 65 20 62 61 63 6b 20 74  to change back t
150df 6f 20 52 45 41 44 45 52 20 73 74 61 74 65 0a 2a  o READER state.*
150e0 2a 20 20 20 20 28 61 73 20 75 73 75 61 6c 6c 79  *    (as usually
150e1 20 68 61 70 70 65 6e 73 20 61 66 74 65 72 20 61   happens after a
150e2 20 72 6f 6c 6c 62 61 63 6b 29 2e 20 41 6e 79 20   rollback). Any 
150e3 73 75 62 73 65 71 75 65 6e 74 20 72 65 61 64 65  subsequent reade
150e4 72 73 20 6d 69 67 68 74 0a 2a 2a 20 20 20 20 72  rs might.**    r
150e5 65 70 6f 72 74 20 64 61 74 61 62 61 73 65 20 63  eport database c
150e6 6f 72 72 75 70 74 69 6f 6e 20 28 64 75 65 20 74  orruption (due t
150e7 6f 20 74 68 65 20 69 6e 63 6f 6e 73 69 73 74 65  o the inconsiste
150e8 6e 74 20 63 61 63 68 65 29 2c 20 61 6e 64 20 69  nt cache), and i
150e9 66 0a 2a 2a 20 20 20 20 74 68 65 79 20 75 70 67  f.**    they upg
150ea 72 61 64 65 20 74 6f 20 77 72 69 74 65 72 73 2c  rade to writers,
150eb 20 74 68 65 79 20 6d 61 79 20 69 6e 61 64 76 65   they may inadve
150ec 72 74 65 6e 74 6c 79 20 63 6f 72 72 75 70 74 20  rtently corrupt 
150ed 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
150ee 20 20 20 66 69 6c 65 2e 20 54 6f 20 61 76 6f 69     file. To avoi
150ef 64 20 74 68 69 73 20 68 61 7a 61 72 64 2c 20 74  d this hazard, t
150f0 68 65 20 70 61 67 65 72 20 73 77 69 74 63 68 65  he pager switche
150f1 73 20 69 6e 74 6f 20 74 68 65 20 45 52 52 4f 52  s into the ERROR
150f2 20 73 74 61 74 65 0a 2a 2a 20 20 20 20 69 6e 73   state.**    ins
150f3 74 65 61 64 20 6f 66 20 52 45 41 44 45 52 20 66  tead of READER f
150f4 6f 6c 6c 6f 77 69 6e 67 20 73 75 63 68 20 61 6e  ollowing such an
150f5 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 20 20   error..**.**   
150f6 20 4f 6e 63 65 20 69 74 20 68 61 73 20 65 6e 74   Once it has ent
150f7 65 72 65 64 20 74 68 65 20 45 52 52 4f 52 20 73  ered the ERROR s
150f8 74 61 74 65 2c 20 61 6e 79 20 61 74 74 65 6d 70  tate, any attemp
150f9 74 20 74 6f 20 75 73 65 20 74 68 65 20 70 61 67  t to use the pag
150fa 65 72 0a 2a 2a 20 20 20 20 74 6f 20 72 65 61 64  er.**    to read
150fb 20 6f 72 20 77 72 69 74 65 20 64 61 74 61 20 72   or write data r
150fc 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2e  eturns an error.
150fd 20 45 76 65 6e 74 75 61 6c 6c 79 2c 20 6f 6e 63   Eventually, onc
150fe 65 20 61 6c 6c 20 0a 2a 2a 20 20 20 20 6f 75 74  e all .**    out
150ff 73 74 61 6e 64 69 6e 67 20 74 72 61 6e 73 61 63  standing transac
15100 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20  tions have been 
15101 61 62 61 6e 64 6f 6e 65 64 2c 20 74 68 65 20 70  abandoned, the p
15102 61 67 65 72 20 69 73 20 61 62 6c 65 20 74 6f 0a  ager is able to.
15103 2a 2a 20 20 20 20 74 72 61 6e 73 69 74 69 6f 6e  **    transition
15104 20 62 61 63 6b 20 74 6f 20 4f 50 45 4e 20 73 74   back to OPEN st
15105 61 74 65 2c 20 64 69 73 63 61 72 64 69 6e 67 20  ate, discarding 
15106 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
15107 74 68 65 20 0a 2a 2a 20 20 20 20 70 61 67 65 2d  the .**    page-
15108 63 61 63 68 65 20 61 6e 64 20 61 6e 79 20 6f 74  cache and any ot
15109 68 65 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74  her in-memory st
1510a 61 74 65 20 61 74 20 74 68 65 20 73 61 6d 65 20  ate at the same 
1510b 74 69 6d 65 2e 20 45 76 65 72 79 74 68 69 6e 67  time. Everything
1510c 0a 2a 2a 20 20 20 20 69 73 20 72 65 6c 6f 61 64  .**    is reload
1510d 65 64 20 66 72 6f 6d 20 64 69 73 6b 20 28 61 6e  ed from disk (an
1510e 64 2c 20 69 66 20 6e 65 63 65 73 73 61 72 79 2c  d, if necessary,
1510f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c   hot-journal rol
15110 6c 62 61 63 6b 20 70 65 66 6f 72 6d 65 64 29 0a  lback peformed).
15111 2a 2a 20 20 20 20 77 68 65 6e 20 61 20 72 65 61  **    when a rea
15112 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  d-transaction is
15113 20 6e 65 78 74 20 6f 70 65 6e 65 64 20 6f 6e 20   next opened on 
15114 74 68 65 20 70 61 67 65 72 20 28 74 72 61 6e 73  the pager (trans
15115 69 74 69 6f 6e 69 6e 67 0a 2a 2a 20 20 20 20 74  itioning.**    t
15116 68 65 20 70 61 67 65 72 20 69 6e 74 6f 20 52 45  he pager into RE
15117 41 44 45 52 20 73 74 61 74 65 29 2e 20 41 74 20  ADER state). At 
15118 74 68 61 74 20 70 6f 69 6e 74 20 74 68 65 20 73  that point the s
15119 79 73 74 65 6d 20 68 61 73 20 72 65 63 6f 76 65  ystem has recove
1511a 72 65 64 20 0a 2a 2a 20 20 20 20 66 72 6f 6d 20  red .**    from 
1511b 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a  the error..**.**
1511c 20 20 20 20 53 70 65 63 69 66 69 63 61 6c 6c 79      Specifically
1511d 2c 20 74 68 65 20 70 61 67 65 72 20 6a 75 6d 70  , the pager jump
1511e 73 20 69 6e 74 6f 20 74 68 65 20 45 52 52 4f 52  s into the ERROR
1511f 20 73 74 61 74 65 20 69 66 3a 0a 2a 2a 0a 2a 2a   state if:.**.**
15120 20 20 20 20 20 20 31 2e 20 41 6e 20 65 72 72 6f        1. An erro
15121 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 61  r occurs while a
15122 74 74 65 6d 70 74 69 6e 67 20 61 20 72 6f 6c 6c  ttempting a roll
15123 62 61 63 6b 2e 20 54 68 69 73 20 68 61 70 70 65  back. This happe
15124 6e 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ns in.**        
15125 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65   function sqlite
15126 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29  3PagerRollback()
15127 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 32 2e 20  ..**.**      2. 
15128 41 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  An error occurs 
15129 77 68 69 6c 65 20 61 74 74 65 6d 70 74 69 6e 67  while attempting
1512a 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 61 20 6a   to finalize a j
1512b 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20  ournal file.**  
1512c 20 20 20 20 20 20 20 66 6f 6c 6c 6f 77 69 6e 67         following
1512d 20 61 20 63 6f 6d 6d 69 74 20 69 6e 20 66 75 6e   a commit in fun
1512e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 50 61 67  ction sqlite3Pag
1512f 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  erCommitPhaseTwo
15130 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 33  ()..**.**      3
15131 2e 20 41 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  . An error occur
15132 73 20 77 68 69 6c 65 20 61 74 74 65 6d 70 74 69  s while attempti
15133 6e 67 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74  ng to write to t
15134 68 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 0a 2a 2a  he journal or.**
15135 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73           databas
15136 65 20 66 69 6c 65 20 69 6e 20 66 75 6e 63 74 69  e file in functi
15137 6f 6e 20 70 61 67 65 72 53 74 72 65 73 73 28 29  on pagerStress()
15138 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 66 72 65   in order to fre
15139 65 20 75 70 0a 2a 2a 20 20 20 20 20 20 20 20 20  e up.**         
1513a 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 20  memory..**.**   
1513b 20 49 6e 20 6f 74 68 65 72 20 63 61 73 65 73 2c   In other cases,
1513c 20 74 68 65 20 65 72 72 6f 72 20 69 73 20 72 65   the error is re
1513d 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 62 2d  turned to the b-
1513e 74 72 65 65 20 6c 61 79 65 72 2e 20 54 68 65 20  tree layer. The 
1513f 62 2d 74 72 65 65 0a 2a 2a 20 20 20 20 6c 61 79  b-tree.**    lay
15140 65 72 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73  er then attempts
15141 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65 72   a rollback oper
15142 61 74 69 6f 6e 2e 20 49 66 20 74 68 65 20 65 72  ation. If the er
15143 72 6f 72 20 63 6f 6e 64 69 74 69 6f 6e 20 0a 2a  ror condition .*
15144 2a 20 20 20 20 70 65 72 73 69 73 74 73 2c 20 74  *    persists, t
15145 68 65 20 70 61 67 65 72 20 65 6e 74 65 72 73 20  he pager enters 
15146 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 20  the ERROR state 
15147 76 69 61 20 63 6f 6e 64 69 74 69 6f 6e 20 28 31  via condition (1
15148 29 20 61 62 6f 76 65 2e 0a 2a 2a 0a 2a 2a 20 20  ) above..**.**  
15149 20 20 43 6f 6e 64 69 74 69 6f 6e 20 28 33 29 20    Condition (3) 
1514a 69 73 20 6e 65 63 65 73 73 61 72 79 20 62 65 63  is necessary bec
1514b 61 75 73 65 20 69 74 20 63 61 6e 20 62 65 20 74  ause it can be t
1514c 72 69 67 67 65 72 65 64 20 62 79 20 61 20 72 65  riggered by a re
1514d 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 20 20 20 73 74  ad-only.**    st
1514e 61 74 65 6d 65 6e 74 20 65 78 65 63 75 74 65 64  atement executed
1514f 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61   within a transa
15150 63 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63  ction. In this c
15151 61 73 65 2c 20 69 66 20 74 68 65 20 65 72 72 6f  ase, if the erro
15152 72 0a 2a 2a 20 20 20 20 63 6f 64 65 20 77 65 72  r.**    code wer
15153 65 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e 65  e simply returne
15154 64 20 74 6f 20 74 68 65 20 75 73 65 72 2c 20 74  d to the user, t
15155 68 65 20 62 2d 74 72 65 65 20 6c 61 79 65 72 20  he b-tree layer 
15156 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 20 20 20  would not.**    
15157 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61 74  automatically at
15158 74 65 6d 70 74 20 61 20 72 6f 6c 6c 62 61 63 6b  tempt a rollback
15159 2c 20 61 73 20 69 74 20 61 73 73 75 6d 65 73 20  , as it assumes 
1515a 74 68 61 74 20 61 6e 20 65 72 72 6f 72 20 69 6e  that an error in
1515b 20 61 0a 2a 2a 20 20 20 20 72 65 61 64 2d 6f 6e   a.**    read-on
1515c 6c 79 20 73 74 61 74 65 6d 65 6e 74 20 63 61 6e  ly statement can
1515d 6e 6f 74 20 6c 65 61 76 65 20 74 68 65 20 70 61  not leave the pa
1515e 67 65 72 20 69 6e 20 61 6e 20 69 6e 74 65 72 6e  ger in an intern
1515f 61 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e  ally inconsisten
15160 74 20 0a 2a 2a 20 20 20 20 73 74 61 74 65 2e 0a  t .**    state..
15161 2a 2a 0a 2a 2a 20 20 20 20 2a 20 54 68 65 20 50  **.**    * The P
15162 61 67 65 72 2e 65 72 72 43 6f 64 65 20 76 61 72  ager.errCode var
15163 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20  iable is set to 
15164 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20  something other 
15165 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a  than SQLITE_OK..
15166 2a 2a 20 20 20 20 2a 20 54 68 65 72 65 20 61 72  **    * There ar
15167 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 75  e one or more ou
15168 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65  tstanding refere
15169 6e 63 65 73 20 74 6f 20 70 61 67 65 73 20 28 61  nces to pages (a
1516a 66 74 65 72 20 74 68 65 0a 2a 2a 20 20 20 20 20  fter the.**     
1516b 20 6c 61 73 74 20 72 65 66 65 72 65 6e 63 65 20   last reference 
1516c 69 73 20 64 72 6f 70 70 65 64 20 74 68 65 20 70  is dropped the p
1516d 61 67 65 72 20 73 68 6f 75 6c 64 20 6d 6f 76 65  ager should move
1516e 20 62 61 63 6b 20 74 6f 20 4f 50 45 4e 20 73 74   back to OPEN st
1516f 61 74 65 29 2e 0a 2a 2a 20 20 20 20 2a 20 54 68  ate)..**    * Th
15170 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 61  e pager is not a
15171 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65  n in-memory page
15172 72 2e 0a 2a 2a 20 20 20 20 0a 2a 2a 0a 2a 2a 20  r..**    .**.** 
15173 4e 6f 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  Notes:.**.**   *
15174 20 41 20 70 61 67 65 72 20 69 73 20 6e 65 76 65   A pager is neve
15175 72 20 69 6e 20 57 52 49 54 45 52 5f 44 42 4d 4f  r in WRITER_DBMO
15176 44 20 6f 72 20 57 52 49 54 45 52 5f 46 49 4e 49  D or WRITER_FINI
15177 53 48 45 44 20 73 74 61 74 65 20 69 66 20 74 68  SHED state if th
15178 65 0a 2a 2a 20 20 20 20 20 63 6f 6e 6e 65 63 74  e.**     connect
15179 69 6f 6e 20 69 73 20 6f 70 65 6e 20 69 6e 20 57  ion is open in W
1517a 41 4c 20 6d 6f 64 65 2e 20 41 20 57 41 4c 20 63  AL mode. A WAL c
1517b 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 61 6c 77  onnection is alw
1517c 61 79 73 20 69 6e 20 6f 6e 65 0a 2a 2a 20 20 20  ays in one.**   
1517d 20 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 66    of the first f
1517e 6f 75 72 20 73 74 61 74 65 73 2e 0a 2a 2a 0a 2a  our states..**.*
1517f 2a 20 20 20 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20  *   * Normally, 
15180 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65  a connection ope
15181 6e 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d  n in exclusive m
15182 6f 64 65 20 69 73 20 6e 65 76 65 72 20 69 6e 20  ode is never in 
15183 50 41 47 45 52 5f 4f 50 45 4e 0a 2a 2a 20 20 20  PAGER_OPEN.**   
15184 20 20 73 74 61 74 65 2e 20 54 68 65 72 65 20 61    state. There a
15185 72 65 20 74 77 6f 20 65 78 63 65 70 74 69 6f 6e  re two exception
15186 73 3a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61  s: immediately a
15187 66 74 65 72 20 65 78 63 6c 75 73 69 76 65 2d 6d  fter exclusive-m
15188 6f 64 65 20 68 61 73 0a 2a 2a 20 20 20 20 20 62  ode has.**     b
15189 65 65 6e 20 74 75 72 6e 65 64 20 6f 6e 20 28 61  een turned on (a
1518a 6e 64 20 62 65 66 6f 72 65 20 61 6e 79 20 72 65  nd before any re
1518b 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e  ad or write tran
1518c 73 61 63 74 69 6f 6e 73 20 61 72 65 20 0a 2a 2a  sactions are .**
1518d 20 20 20 20 20 65 78 65 63 75 74 65 64 29 2c 20       executed), 
1518e 61 6e 64 20 77 68 65 6e 20 74 68 65 20 70 61 67  and when the pag
1518f 65 72 20 69 73 20 6c 65 61 76 69 6e 67 20 74 68  er is leaving th
15190 65 20 22 65 72 72 6f 72 20 73 74 61 74 65 22 2e  e "error state".
15191 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 53 65 65 20 61  .**.**   * See a
15192 6c 73 6f 3a 20 61 73 73 65 72 74 5f 70 61 67 65  lso: assert_page
15193 72 5f 73 74 61 74 65 28 29 2e 0a 2a 2f 0a 23 64  r_state()..*/.#d
15194 65 66 69 6e 65 20 50 41 47 45 52 5f 4f 50 45 4e  efine PAGER_OPEN
15195 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15196 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47 45    0.#define PAGE
15197 52 5f 52 45 41 44 45 52 20 20 20 20 20 20 20 20  R_READER        
15198 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
15199 65 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c  e PAGER_WRITER_L
1519a 4f 43 4b 45 44 20 20 20 20 20 20 20 20 20 32 0a  OCKED         2.
1519b 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 57 52  #define PAGER_WR
1519c 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 20 20 20  ITER_CACHEMOD   
1519d 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 50 41      3.#define PA
1519e 47 45 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44  GER_WRITER_DBMOD
1519f 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66            4.#def
151a0 69 6e 65 20 50 41 47 45 52 5f 57 52 49 54 45 52  ine PAGER_WRITER
151a1 5f 46 49 4e 49 53 48 45 44 20 20 20 20 20 20 20  _FINISHED       
151a2 35 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  5.#define PAGER_
151a3 45 52 52 4f 52 20 20 20 20 20 20 20 20 20 20 20  ERROR           
151a4 20 20 20 20 20 20 36 0a 0a 2f 2a 0a 2a 2a 20 54        6../*.** T
151a5 68 65 20 50 61 67 65 72 2e 65 4c 6f 63 6b 20 76  he Pager.eLock v
151a6 61 72 69 61 62 6c 65 20 69 73 20 61 6c 6d 6f 73  ariable is almos
151a7 74 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20  t always set to 
151a8 6f 6e 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66  one of the .** f
151a9 6f 6c 6c 6f 77 69 6e 67 20 6c 6f 63 6b 69 6e 67  ollowing locking
151aa 2d 73 74 61 74 65 73 2c 20 61 63 63 6f 72 64 69  -states, accordi
151ab 6e 67 20 74 6f 20 74 68 65 20 6c 6f 63 6b 20 63  ng to the lock c
151ac 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e  urrently held on
151ad 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
151ae 20 66 69 6c 65 3a 20 4e 4f 5f 4c 4f 43 4b 2c 20   file: NO_LOCK, 
151af 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53  SHARED_LOCK, RES
151b0 45 52 56 45 44 5f 4c 4f 43 4b 20 6f 72 20 45 58  ERVED_LOCK or EX
151b1 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 2e 0a 2a 2a  CLUSIVE_LOCK..**
151b2 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 69   This variable i
151b3 73 20 6b 65 70 74 20 75 70 20 74 6f 20 64 61 74  s kept up to dat
151b4 65 20 61 73 20 6c 6f 63 6b 73 20 61 72 65 20 74  e as locks are t
151b5 61 6b 65 6e 20 61 6e 64 20 72 65 6c 65 61 73 65  aken and release
151b6 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 61 67 65  d by.** the page
151b7 72 4c 6f 63 6b 44 62 28 29 20 61 6e 64 20 70 61  rLockDb() and pa
151b8 67 65 72 55 6e 6c 6f 63 6b 44 62 28 29 20 77 72  gerUnlockDb() wr
151b9 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  appers..**.** If
151ba 20 74 68 65 20 56 46 53 20 78 4c 6f 63 6b 28 29   the VFS xLock()
151bb 20 6f 72 20 78 55 6e 6c 6f 63 6b 28 29 20 72 65   or xUnlock() re
151bc 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 6f  turns an error o
151bd 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45  ther than SQLITE
151be 5f 42 55 53 59 0a 2a 2a 20 28 69 2e 65 2e 20 6f  _BUSY.** (i.e. o
151bf 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45  ne of the SQLITE
151c0 5f 49 4f 45 52 52 20 73 75 62 74 79 70 65 73 29  _IOERR subtypes)
151c1 2c 20 69 74 20 69 73 20 6e 6f 74 20 63 6c 65 61  , it is not clea
151c2 72 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  r whether or not
151c3 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f  .** the operatio
151c4 6e 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c  n was successful
151c5 2e 20 49 6e 20 74 68 65 73 65 20 63 69 72 63 75  . In these circu
151c6 6d 73 74 61 6e 63 65 73 20 70 61 67 65 72 4c 6f  mstances pagerLo
151c7 63 6b 44 62 28 29 20 61 6e 64 0a 2a 2a 20 70 61  ckDb() and.** pa
151c8 67 65 72 55 6e 6c 6f 63 6b 44 62 28 29 20 74 61  gerUnlockDb() ta
151c9 6b 65 20 61 20 63 6f 6e 73 65 72 76 61 74 69 76  ke a conservativ
151ca 65 20 61 70 70 72 6f 61 63 68 20 2d 20 65 4c 6f  e approach - eLo
151cb 63 6b 20 69 73 20 61 6c 77 61 79 73 20 75 70 64  ck is always upd
151cc 61 74 65 64 0a 2a 2a 20 77 68 65 6e 20 75 6e 6c  ated.** when unl
151cd 6f 63 6b 69 6e 67 20 74 68 65 20 66 69 6c 65 2c  ocking the file,
151ce 20 61 6e 64 20 6f 6e 6c 79 20 75 70 64 61 74 65   and only update
151cf 64 20 77 68 65 6e 20 6c 6f 63 6b 69 6e 67 20 74  d when locking t
151d0 68 65 20 66 69 6c 65 20 69 66 20 74 68 65 0a 2a  he file if the.*
151d1 2a 20 56 46 53 20 63 61 6c 6c 20 69 73 20 73 75  * VFS call is su
151d2 63 63 65 73 73 66 75 6c 2e 20 54 68 69 73 20 77  ccessful. This w
151d3 61 79 2c 20 74 68 65 20 50 61 67 65 72 2e 65 4c  ay, the Pager.eL
151d4 6f 63 6b 20 76 61 72 69 61 62 6c 65 20 6d 61 79  ock variable may
151d5 20 62 65 20 73 65 74 0a 2a 2a 20 74 6f 20 61 20   be set.** to a 
151d6 6c 65 73 73 20 65 78 63 6c 75 73 69 76 65 20 28  less exclusive (
151d7 6c 6f 77 65 72 29 20 76 61 6c 75 65 20 74 68 61  lower) value tha
151d8 6e 20 74 68 65 20 6c 6f 63 6b 20 74 68 61 74 20  n the lock that 
151d9 69 73 20 61 63 74 75 61 6c 6c 79 20 68 65 6c 64  is actually held
151da 0a 2a 2a 20 61 74 20 74 68 65 20 73 79 73 74 65  .** at the syste
151db 6d 20 6c 65 76 65 6c 2c 20 62 75 74 20 69 74 20  m level, but it 
151dc 69 73 20 6e 65 76 65 72 20 73 65 74 20 74 6f 20  is never set to 
151dd 61 20 6d 6f 72 65 20 65 78 63 6c 75 73 69 76 65  a more exclusive
151de 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   value..**.** Th
151df 69 73 20 69 73 20 75 73 75 61 6c 6c 79 20 73 61  is is usually sa
151e0 66 65 2e 20 49 66 20 61 6e 20 78 55 6e 6c 6f 63  fe. If an xUnloc
151e1 6b 20 66 61 69 6c 73 20 6f 72 20 61 70 70 65 61  k fails or appea
151e2 72 73 20 74 6f 20 66 61 69 6c 2c 20 74 68 65 72  rs to fail, ther
151e3 65 20 6d 61 79 20 0a 2a 2a 20 62 65 20 61 20 66  e may .** be a f
151e4 65 77 20 72 65 64 75 6e 64 61 6e 74 20 78 4c 6f  ew redundant xLo
151e5 63 6b 28 29 20 63 61 6c 6c 73 20 6f 72 20 61 20  ck() calls or a 
151e6 6c 6f 63 6b 20 6d 61 79 20 62 65 20 68 65 6c 64  lock may be held
151e7 20 66 6f 72 20 6c 6f 6e 67 65 72 20 74 68 61 6e   for longer than
151e8 0a 2a 2a 20 72 65 71 75 69 72 65 64 2c 20 62 75  .** required, bu
151e9 74 20 6e 6f 74 68 69 6e 67 20 72 65 61 6c 6c 79  t nothing really
151ea 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a   goes wrong..**.
151eb 2a 2a 20 54 68 65 20 65 78 63 65 70 74 69 6f 6e  ** The exception
151ec 20 69 73 20 77 68 65 6e 20 74 68 65 20 64 61 74   is when the dat
151ed 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 75 6e  abase file is un
151ee 6c 6f 63 6b 65 64 20 61 73 20 74 68 65 20 70 61  locked as the pa
151ef 67 65 72 20 6d 6f 76 65 73 0a 2a 2a 20 66 72 6f  ger moves.** fro
151f0 6d 20 45 52 52 4f 52 20 74 6f 20 4f 50 45 4e 20  m ERROR to OPEN 
151f1 73 74 61 74 65 2e 20 41 74 20 74 68 69 73 20 70  state. At this p
151f2 6f 69 6e 74 20 74 68 65 72 65 20 6d 61 79 20 62  oint there may b
151f3 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  e a hot-journal 
151f4 66 69 6c 65 20 0a 2a 2a 20 69 6e 20 74 68 65 20  file .** in the 
151f5 66 69 6c 65 2d 73 79 73 74 65 6d 20 74 68 61 74  file-system that
151f6 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c   needs to be rol
151f7 6c 65 64 20 62 61 63 6b 20 28 61 73 20 70 61 72  led back (as par
151f8 74 20 6f 66 20 61 20 4f 50 45 4e 2d 3e 53 48 41  t of a OPEN->SHA
151f9 52 45 44 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  RED.** transitio
151fa 6e 2c 20 62 79 20 74 68 65 20 73 61 6d 65 20 70  n, by the same p
151fb 61 67 65 72 20 6f 72 20 61 6e 79 20 6f 74 68 65  ager or any othe
151fc 72 29 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 20  r). If the call 
151fd 74 6f 20 78 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20  to xUnlock().** 
151fe 66 61 69 6c 73 20 61 74 20 74 68 69 73 20 70 6f  fails at this po
151ff 69 6e 74 20 61 6e 64 20 74 68 65 20 70 61 67 65  int and the page
15200 72 20 69 73 20 6c 65 66 74 20 68 6f 6c 64 69 6e  r is left holdin
15201 67 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  g an EXCLUSIVE l
15202 6f 63 6b 2c 20 74 68 69 73 0a 2a 2a 20 63 61 6e  ock, this.** can
15203 20 63 6f 6e 66 75 73 65 20 74 68 65 20 63 61 6c   confuse the cal
15204 6c 20 74 6f 20 78 43 68 65 63 6b 52 65 73 65 72  l to xCheckReser
15205 76 65 64 4c 6f 63 6b 28 29 20 63 61 6c 6c 20 6d  vedLock() call m
15206 61 64 65 20 6c 61 74 65 72 20 61 73 20 70 61 72  ade later as par
15207 74 0a 2a 2a 20 6f 66 20 68 6f 74 2d 6a 6f 75 72  t.** of hot-jour
15208 6e 61 6c 20 64 65 74 65 63 74 69 6f 6e 2e 0a 2a  nal detection..*
15209 2a 0a 2a 2a 20 78 43 68 65 63 6b 52 65 73 65 72  *.** xCheckReser
1520a 76 65 64 4c 6f 63 6b 28 29 20 69 73 20 64 65 66  vedLock() is def
1520b 69 6e 65 64 20 61 73 20 72 65 74 75 72 6e 69 6e  ined as returnin
1520c 67 20 74 72 75 65 20 22 69 66 20 74 68 65 72 65  g true "if there
1520d 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 0a   is a RESERVED .
1520e 2a 2a 20 6c 6f 63 6b 20 68 65 6c 64 20 62 79 20  ** lock held by 
1520f 74 68 69 73 20 70 72 6f 63 65 73 73 20 6f 72 20  this process or 
15210 61 6e 79 20 6f 74 68 65 72 73 22 2e 20 53 6f 20  any others". So 
15211 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
15212 63 6b 20 6d 61 79 20 0a 2a 2a 20 72 65 74 75 72  ck may .** retur
15213 6e 20 74 72 75 65 20 62 65 63 61 75 73 65 20 74  n true because t
15214 68 65 20 63 61 6c 6c 65 72 20 69 74 73 65 6c 66  he caller itself
15215 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 6e 20 45   is holding an E
15216 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 28 62  XCLUSIVE lock (b
15217 75 74 0a 2a 2a 20 64 6f 65 73 6e 27 74 20 6b 6e  ut.** doesn't kn
15218 6f 77 20 69 74 20 62 65 63 61 75 73 65 20 6f 66  ow it because of
15219 20 61 20 70 72 65 76 69 6f 75 73 20 65 72 72 6f   a previous erro
1521a 72 20 69 6e 20 78 55 6e 6c 6f 63 6b 29 2e 20 49  r in xUnlock). I
1521b 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 0a 2a  f this happens.*
1521c 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  * a hot-journal 
1521d 6d 61 79 20 62 65 20 6d 69 73 74 61 6b 65 6e 20  may be mistaken 
1521e 66 6f 72 20 61 20 6a 6f 75 72 6e 61 6c 20 62 65  for a journal be
1521f 69 6e 67 20 63 72 65 61 74 65 64 20 62 79 20 61  ing created by a
15220 6e 20 61 63 74 69 76 65 0a 2a 2a 20 74 72 61 6e  n active.** tran
15221 73 61 63 74 69 6f 6e 20 69 6e 20 61 6e 6f 74 68  saction in anoth
15222 65 72 20 70 72 6f 63 65 73 73 2c 20 63 61 75 73  er process, caus
15223 69 6e 67 20 53 51 4c 69 74 65 20 74 6f 20 72 65  ing SQLite to re
15224 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ad from the data
15225 62 61 73 65 0a 2a 2a 20 77 69 74 68 6f 75 74 20  base.** without 
15226 72 6f 6c 6c 69 6e 67 20 69 74 20 62 61 63 6b 2e  rolling it back.
15227 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 61  .**.** To work a
15228 72 6f 75 6e 64 20 74 68 69 73 2c 20 69 66 20 61  round this, if a
15229 20 63 61 6c 6c 20 74 6f 20 78 55 6e 6c 6f 63 6b   call to xUnlock
1522a 28 29 20 66 61 69 6c 73 20 77 68 65 6e 20 75 6e  () fails when un
1522b 6c 6f 63 6b 69 6e 67 20 74 68 65 0a 2a 2a 20 64  locking the.** d
1522c 61 74 61 62 61 73 65 20 69 6e 20 74 68 65 20 45  atabase in the E
1522d 52 52 4f 52 20 73 74 61 74 65 2c 20 50 61 67 65  RROR state, Page
1522e 72 2e 65 4c 6f 63 6b 20 69 73 20 73 65 74 20 74  r.eLock is set t
1522f 6f 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 2e 20  o UNKNOWN_LOCK. 
15230 49 74 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 63 68  It.** is only ch
15231 61 6e 67 65 64 20 62 61 63 6b 20 74 6f 20 61 20  anged back to a 
15232 72 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 73 74 61  real locking sta
15233 74 65 20 61 66 74 65 72 20 61 20 73 75 63 63 65  te after a succe
15234 73 73 66 75 6c 20 63 61 6c 6c 0a 2a 2a 20 74 6f  ssful call.** to
15235 20 78 4c 6f 63 6b 28 45 58 43 4c 55 53 49 56 45   xLock(EXCLUSIVE
15236 29 2e 20 41 6c 73 6f 2c 20 74 68 65 20 63 6f 64  ). Also, the cod
15237 65 20 74 6f 20 64 6f 20 74 68 65 20 4f 50 45 4e  e to do the OPEN
15238 2d 3e 53 48 41 52 45 44 20 73 74 61 74 65 20 74  ->SHARED state t
15239 72 61 6e 73 69 74 69 6f 6e 0a 2a 2a 20 6f 6d 69  ransition.** omi
1523a 74 73 20 74 68 65 20 63 68 65 63 6b 20 66 6f 72  ts the check for
1523b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 69   a hot-journal i
1523c 66 20 50 61 67 65 72 2e 65 4c 6f 63 6b 20 69 73  f Pager.eLock is
1523d 20 73 65 74 20 74 6f 20 55 4e 4b 4e 4f 57 4e 5f   set to UNKNOWN_
1523e 4c 4f 43 4b 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49  LOCK .** lock. I
1523f 6e 73 74 65 61 64 2c 20 69 74 20 61 73 73 75 6d  nstead, it assum
15240 65 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  es a hot-journal
15241 20 65 78 69 73 74 73 20 61 6e 64 20 6f 62 74 61   exists and obta
15242 69 6e 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45  ins an EXCLUSIVE
15243 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  .** lock on the 
15244 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 65  database file be
15245 66 6f 72 65 20 61 74 74 65 6d 70 74 69 6e 67 20  fore attempting 
15246 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e  to roll it back.
15247 20 53 65 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a   See function.**
15248 20 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b   PagerSharedLock
15249 28 29 20 66 6f 72 20 6d 6f 72 65 20 64 65 74 61  () for more deta
1524a 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 72 2e  il..**.** Pager.
1524b 65 4c 6f 63 6b 20 6d 61 79 20 6f 6e 6c 79 20 62  eLock may only b
1524c 65 20 73 65 74 20 74 6f 20 55 4e 4b 4e 4f 57 4e  e set to UNKNOWN
1524d 5f 4c 4f 43 4b 20 77 68 65 6e 20 74 68 65 20 70  _LOCK when the p
1524e 61 67 65 72 20 69 73 20 69 6e 20 0a 2a 2a 20 50  ager is in .** P
1524f 41 47 45 52 5f 4f 50 45 4e 20 73 74 61 74 65 2e  AGER_OPEN state.
15250 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 4b 4e  .*/.#define UNKN
15251 4f 57 4e 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20  OWN_LOCK        
15252 20 20 20 20 20 20 20 20 28 45 58 43 4c 55 53 49          (EXCLUSI
15253 56 45 5f 4c 4f 43 4b 2b 31 29 0a 0a 2f 2a 0a 2a  VE_LOCK+1)../*.*
15254 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 64 20 66  * A macro used f
15255 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20  or invoking the 
15256 63 6f 64 65 63 20 69 66 20 74 68 65 72 65 20 69  codec if there i
15257 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 20  s one.*/.#ifdef 
15258 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
15259 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 31  .# define CODEC1
1525a 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 5c 0a 20 20  (P,D,N,X,E) \.  
1525b 20 20 69 66 28 20 50 2d 3e 78 43 6f 64 65 63 20    if( P->xCodec 
1525c 26 26 20 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e  && P->xCodec(P->
1525d 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 29 3d 3d 30  pCodec,D,N,X)==0
1525e 20 29 7b 20 45 3b 20 7d 0a 23 20 64 65 66 69 6e   ){ E; }.# defin
1525f 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58  e CODEC2(P,D,N,X
15260 2c 45 2c 4f 29 20 5c 0a 20 20 20 20 69 66 28 20  ,E,O) \.    if( 
15261 50 2d 3e 78 43 6f 64 65 63 3d 3d 30 20 29 7b 20  P->xCodec==0 ){ 
15262 4f 3d 28 63 68 61 72 2a 29 44 3b 20 7d 65 6c 73  O=(char*)D; }els
15263 65 20 5c 0a 20 20 20 20 69 66 28 20 28 4f 3d 28  e \.    if( (O=(
15264 63 68 61 72 2a 29 28 50 2d 3e 78 43 6f 64 65 63  char*)(P->xCodec
15265 28 50 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58  (P->pCodec,D,N,X
15266 29 29 29 3d 3d 30 20 29 7b 20 45 3b 20 7d 0a 23  )))==0 ){ E; }.#
15267 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 43 4f  else.# define CO
15268 44 45 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20  DEC1(P,D,N,X,E) 
15269 20 20 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a 23 20    /* NO-OP */.# 
1526a 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c  define CODEC2(P,
1526b 44 2c 4e 2c 58 2c 45 2c 4f 29 20 4f 3d 28 63 68  D,N,X,E,O) O=(ch
1526c 61 72 2a 29 44 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ar*)D.#endif../*
1526d 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20  .** The maximum 
1526e 61 6c 6c 6f 77 65 64 20 73 65 63 74 6f 72 20 73  allowed sector s
1526f 69 7a 65 2e 20 36 34 4b 69 42 2e 20 49 66 20 74  ize. 64KiB. If t
15270 68 65 20 78 53 65 63 74 6f 72 73 69 7a 65 28 29  he xSectorsize()
15271 20 6d 65 74 68 6f 64 20 0a 2a 2a 20 72 65 74 75   method .** retu
15272 72 6e 73 20 61 20 76 61 6c 75 65 20 6c 61 72 67  rns a value larg
15273 65 72 20 74 68 61 6e 20 74 68 69 73 2c 20 74 68  er than this, th
15274 65 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49  en MAX_SECTOR_SI
15275 5a 45 20 69 73 20 75 73 65 64 20 69 6e 73 74 65  ZE is used inste
15276 61 64 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c  ad..** This coul
15277 64 20 63 6f 6e 63 65 69 76 61 62 6c 79 20 63 61  d conceivably ca
15278 75 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 66  use corruption f
15279 6f 6c 6c 6f 77 69 6e 67 20 61 20 70 6f 77 65 72  ollowing a power
1527a 20 66 61 69 6c 75 72 65 20 6f 6e 0a 2a 2a 20 73   failure on.** s
1527b 75 63 68 20 61 20 73 79 73 74 65 6d 2e 20 54 68  uch a system. Th
1527c 69 73 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  is is currently 
1527d 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 20  an undocumented 
1527e 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e  limit..*/.#defin
1527f 65 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a  e MAX_SECTOR_SIZ
15280 45 20 30 78 31 30 30 30 30 0a 0a 2f 2a 0a 2a 2a  E 0x10000../*.**
15281 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
15282 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
15283 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63  ructure is alloc
15284 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 61 63  ated for each ac
15285 74 69 76 65 0a 2a 2a 20 73 61 76 65 70 6f 69 6e  tive.** savepoin
15286 74 20 61 6e 64 20 73 74 61 74 65 6d 65 6e 74 20  t and statement 
15287 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 74  transaction in t
15288 68 65 20 73 79 73 74 65 6d 2e 20 41 6c 6c 20 73  he system. All s
15289 75 63 68 20 73 74 72 75 63 74 75 72 65 73 0a 2a  uch structures.*
1528a 2a 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  * are stored in 
1528b 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 70  the Pager.aSavep
1528c 6f 69 6e 74 5b 5d 20 61 72 72 61 79 2c 20 77 68  oint[] array, wh
1528d 69 63 68 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ich is allocated
1528e 20 61 6e 64 0a 2a 2a 20 72 65 73 69 7a 65 64 20   and.** resized 
1528f 75 73 69 6e 67 20 73 71 6c 69 74 65 33 52 65 61  using sqlite3Rea
15290 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 57 68  lloc()..**.** Wh
15291 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 20 69  en a savepoint i
15292 73 20 63 72 65 61 74 65 64 2c 20 74 68 65 20 50  s created, the P
15293 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48  agerSavepoint.iH
15294 64 72 4f 66 66 73 65 74 20 66 69 65 6c 64 20 69  drOffset field i
15295 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2e 20 49  s.** set to 0. I
15296 66 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64  f a journal-head
15297 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  er is written in
15298 74 6f 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  to the main jour
15299 6e 61 6c 20 77 68 69 6c 65 0a 2a 2a 20 74 68 65  nal while.** the
1529a 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 63   savepoint is ac
1529b 74 69 76 65 2c 20 74 68 65 6e 20 69 48 64 72 4f  tive, then iHdrO
1529c 66 66 73 65 74 20 69 73 20 73 65 74 20 74 6f 20  ffset is set to 
1529d 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 20  the byte offset 
1529e 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  .** immediately 
1529f 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 61  following the la
152a0 73 74 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72  st journal recor
152a1 64 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  d written into t
152a2 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e  he main.** journ
152a3 61 6c 20 62 65 66 6f 72 65 20 74 68 65 20 6a 6f  al before the jo
152a4 75 72 6e 61 6c 2d 68 65 61 64 65 72 2e 20 54 68  urnal-header. Th
152a5 69 73 20 69 73 20 72 65 71 75 69 72 65 64 20 64  is is required d
152a6 75 72 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 0a  uring savepoint.
152a7 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 28 73 65 65  ** rollback (see
152a8 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61   pagerPlaybackSa
152a9 76 65 70 6f 69 6e 74 28 29 29 2e 0a 2a 2f 0a 74  vepoint())..*/.t
152aa 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61  ypedef struct Pa
152ab 67 65 72 53 61 76 65 70 6f 69 6e 74 20 50 61 67  gerSavepoint Pag
152ac 65 72 53 61 76 65 70 6f 69 6e 74 3b 0a 73 74 72  erSavepoint;.str
152ad 75 63 74 20 50 61 67 65 72 53 61 76 65 70 6f 69  uct PagerSavepoi
152ae 6e 74 20 7b 0a 20 20 69 36 34 20 69 4f 66 66 73  nt {.  i64 iOffs
152af 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  et;             
152b0 20 20 20 20 2f 2a 20 53 74 61 72 74 69 6e 67 20      /* Starting 
152b1 6f 66 66 73 65 74 20 69 6e 20 6d 61 69 6e 20 6a  offset in main j
152b2 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20  ournal */.  i64 
152b3 69 48 64 72 4f 66 66 73 65 74 3b 20 20 20 20 20  iHdrOffset;     
152b4 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 65 20           /* See 
152b5 61 62 6f 76 65 20 2a 2f 0a 20 20 42 69 74 76 65  above */.  Bitve
152b6 63 20 2a 70 49 6e 53 61 76 65 70 6f 69 6e 74 3b  c *pInSavepoint;
152b7 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6f          /* Set o
152b8 66 20 70 61 67 65 73 20 69 6e 20 74 68 69 73 20  f pages in this 
152b9 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 50  savepoint */.  P
152ba 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20 20 20 20  gno nOrig;      
152bb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
152bc 72 69 67 69 6e 61 6c 20 6e 75 6d 62 65 72 20 6f  riginal number o
152bd 66 20 70 61 67 65 73 20 69 6e 20 66 69 6c 65 20  f pages in file 
152be 2a 2f 0a 20 20 50 67 6e 6f 20 69 53 75 62 52 65  */.  Pgno iSubRe
152bf 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
152c0 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 69    /* Index of fi
152c1 72 73 74 20 72 65 63 6f 72 64 20 69 6e 20 73 75  rst record in su
152c2 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 69 66  b-journal */.#if
152c3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
152c4 5f 57 41 4c 0a 20 20 75 33 32 20 61 57 61 6c 44  _WAL.  u32 aWalD
152c5 61 74 61 5b 57 41 4c 5f 53 41 56 45 50 4f 49 4e  ata[WAL_SAVEPOIN
152c6 54 5f 4e 44 41 54 41 5d 3b 20 20 20 20 20 20 20  T_NDATA];       
152c7 20 2f 2a 20 57 41 4c 20 73 61 76 65 70 6f 69 6e   /* WAL savepoin
152c8 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 23 65 6e  t context */.#en
152c9 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20  dif.};../*.** A 
152ca 6f 70 65 6e 20 70 61 67 65 20 63 61 63 68 65 20  open page cache 
152cb 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
152cc 66 20 73 74 72 75 63 74 20 50 61 67 65 72 2e 20  f struct Pager. 
152cd 41 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  A description of
152ce 0a 2a 2a 20 73 6f 6d 65 20 6f 66 20 74 68 65 20  .** some of the 
152cf 6d 6f 72 65 20 69 6d 70 6f 72 74 61 6e 74 20 6d  more important m
152d0 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 73 20  ember variables 
152d1 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 65  follows:.**.** e
152d2 53 74 61 74 65 0a 2a 2a 0a 2a 2a 20 20 20 54 68  State.**.**   Th
152d3 65 20 63 75 72 72 65 6e 74 20 27 73 74 61 74 65  e current 'state
152d4 27 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 6f  ' of the pager o
152d5 62 6a 65 63 74 2e 20 53 65 65 20 74 68 65 20 63  bject. See the c
152d6 6f 6d 6d 65 6e 74 20 61 6e 64 20 73 74 61 74 65  omment and state
152d7 0a 2a 2a 20 20 20 64 69 61 67 72 61 6d 20 61 62  .**   diagram ab
152d8 6f 76 65 20 66 6f 72 20 61 20 64 65 73 63 72 69  ove for a descri
152d9 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67  ption of the pag
152da 65 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  er state..**.** 
152db 65 4c 6f 63 6b 0a 2a 2a 0a 2a 2a 20 20 20 46 6f  eLock.**.**   Fo
152dc 72 20 61 20 72 65 61 6c 20 6f 6e 2d 64 69 73 6b  r a real on-disk
152dd 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 63   database, the c
152de 75 72 72 65 6e 74 20 6c 6f 63 6b 20 68 65 6c 64  urrent lock held
152df 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
152e0 20 66 69 6c 65 20 2d 0a 2a 2a 20 20 20 4e 4f 5f   file -.**   NO_
152e1 4c 4f 43 4b 2c 20 53 48 41 52 45 44 5f 4c 4f 43  LOCK, SHARED_LOC
152e2 4b 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  K, RESERVED_LOCK
152e3 20 6f 72 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f   or EXCLUSIVE_LO
152e4 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 6f 72 20  CK..**.**   For 
152e5 61 20 74 65 6d 70 6f 72 61 72 79 20 6f 72 20 69  a temporary or i
152e6 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
152e7 65 20 28 6e 65 69 74 68 65 72 20 6f 66 20 77 68  e (neither of wh
152e8 69 63 68 20 72 65 71 75 69 72 65 20 61 6e 79 0a  ich require any.
152e9 2a 2a 20 20 20 6c 6f 63 6b 73 29 2c 20 74 68 69  **   locks), thi
152ea 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 61 6c  s variable is al
152eb 77 61 79 73 20 73 65 74 20 74 6f 20 45 58 43 4c  ways set to EXCL
152ec 55 53 49 56 45 5f 4c 4f 43 4b 2e 20 53 69 6e 63  USIVE_LOCK. Sinc
152ed 65 20 73 75 63 68 0a 2a 2a 20 20 20 64 61 74 61  e such.**   data
152ee 62 61 73 65 73 20 61 6c 77 61 79 73 20 68 61 76  bases always hav
152ef 65 20 50 61 67 65 72 2e 65 78 63 6c 75 73 69 76  e Pager.exclusiv
152f0 65 4d 6f 64 65 3d 3d 31 2c 20 74 68 69 73 20 74  eMode==1, this t
152f1 72 69 63 6b 73 20 74 68 65 20 70 61 67 65 72 0a  ricks the pager.
152f2 2a 2a 20 20 20 6c 6f 67 69 63 20 69 6e 74 6f 20  **   logic into 
152f3 74 68 69 6e 6b 69 6e 67 20 74 68 61 74 20 69 74  thinking that it
152f4 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 6c 6c   already has all
152f5 20 74 68 65 20 6c 6f 63 6b 73 20 69 74 20 77 69   the locks it wi
152f6 6c 6c 20 65 76 65 72 0a 2a 2a 20 20 20 6e 65 65  ll ever.**   nee
152f7 64 20 28 61 6e 64 20 6e 6f 20 72 65 61 73 6f 6e  d (and no reason
152f8 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65 6d   to release them
152f9 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 73 6f  )..**.**   In so
152fa 6d 65 20 28 6f 62 73 63 75 72 65 29 20 63 69 72  me (obscure) cir
152fb 63 75 6d 73 74 61 6e 63 65 73 2c 20 74 68 69 73  cumstances, this
152fc 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20 61 6c   variable may al
152fd 73 6f 20 62 65 20 73 65 74 20 74 6f 0a 2a 2a 20  so be set to.** 
152fe 20 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 2e 20    UNKNOWN_LOCK. 
152ff 53 65 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20  See the comment 
15300 61 62 6f 76 65 20 74 68 65 20 23 64 65 66 69 6e  above the #defin
15301 65 20 6f 66 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43  e of UNKNOWN_LOC
15302 4b 20 66 6f 72 0a 2a 2a 20 20 20 64 65 74 61 69  K for.**   detai
15303 6c 73 2e 0a 2a 2a 0a 2a 2a 20 63 68 61 6e 67 65  ls..**.** change
15304 43 6f 75 6e 74 44 6f 6e 65 0a 2a 2a 0a 2a 2a 20  CountDone.**.** 
15305 20 20 54 68 69 73 20 62 6f 6f 6c 65 61 6e 20 76    This boolean v
15306 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20  ariable is used 
15307 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
15308 74 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75  t the change-cou
15309 6e 74 65 72 20 0a 2a 2a 20 20 20 28 74 68 65 20  nter .**   (the 
1530a 34 2d 62 79 74 65 20 68 65 61 64 65 72 20 66 69  4-byte header fi
1530b 65 6c 64 20 61 74 20 62 79 74 65 20 6f 66 66 73  eld at byte offs
1530c 65 74 20 32 34 20 6f 66 20 74 68 65 20 64 61 74  et 24 of the dat
1530d 61 62 61 73 65 20 66 69 6c 65 29 20 69 73 20 0a  abase file) is .
1530e 2a 2a 20 20 20 6e 6f 74 20 75 70 64 61 74 65 64  **   not updated
1530f 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e   more often than
15310 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 2a 2a 0a   necessary. .**.
15311 2a 2a 20 20 20 49 74 20 69 73 20 73 65 74 20 74  **   It is set t
15312 6f 20 74 72 75 65 20 77 68 65 6e 20 74 68 65 20  o true when the 
15313 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 66  change-counter f
15314 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 64 2c  ield is updated,
15315 20 77 68 69 63 68 20 0a 2a 2a 20 20 20 63 61 6e   which .**   can
15316 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20   only happen if 
15317 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
15318 6b 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65  k is held on the
15319 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
1531a 2a 2a 20 20 20 49 74 20 69 73 20 63 6c 65 61 72  **   It is clear
1531b 65 64 20 28 73 65 74 20 74 6f 20 66 61 6c 73 65  ed (set to false
1531c 29 20 77 68 65 6e 65 76 65 72 20 61 6e 20 65 78  ) whenever an ex
1531d 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20  clusive lock is 
1531e 0a 2a 2a 20 20 20 72 65 6c 69 6e 71 75 69 73 68  .**   relinquish
1531f 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ed on the databa
15320 73 65 20 66 69 6c 65 2e 20 45 61 63 68 20 74 69  se file. Each ti
15321 6d 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  me a transaction
15322 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2c 0a 2a   is committed,.*
15323 2a 20 20 20 54 68 65 20 63 68 61 6e 67 65 43 6f  *   The changeCo
15324 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 69 73 20  untDone flag is 
15325 69 6e 73 70 65 63 74 65 64 2e 20 49 66 20 69 74  inspected. If it
15326 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 77 6f   is true, the wo
15327 72 6b 20 6f 66 0a 2a 2a 20 20 20 75 70 64 61 74  rk of.**   updat
15328 69 6e 67 20 74 68 65 20 63 68 61 6e 67 65 2d 63  ing the change-c
15329 6f 75 6e 74 65 72 20 69 73 20 6f 6d 69 74 74 65  ounter is omitte
1532a 64 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e  d for the curren
1532b 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  t transaction..*
1532c 2a 0a 2a 2a 20 20 20 54 68 69 73 20 6d 65 63 68  *.**   This mech
1532d 61 6e 69 73 6d 20 6d 65 61 6e 73 20 74 68 61 74  anism means that
1532e 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e   when running in
1532f 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c   exclusive mode,
15330 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a   a connection .*
15331 2a 20 20 20 6e 65 65 64 20 6f 6e 6c 79 20 75 70  *   need only up
15332 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d  date the change-
15333 63 6f 75 6e 74 65 72 20 6f 6e 63 65 2c 20 66 6f  counter once, fo
15334 72 20 74 68 65 20 66 69 72 73 74 20 74 72 61 6e  r the first tran
15335 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 63 6f 6d  saction.**   com
15336 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 73 65  mitted..**.** se
15337 74 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a 20 20 20  tMaster.**.**   
15338 57 68 65 6e 20 50 61 67 65 72 43 6f 6d 6d 69 74  When PagerCommit
15339 50 68 61 73 65 4f 6e 65 28 29 20 69 73 20 63 61  PhaseOne() is ca
1533a 6c 6c 65 64 20 74 6f 20 63 6f 6d 6d 69 74 20 61  lled to commit a
1533b 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74   transaction, it
1533c 20 6d 61 79 0a 2a 2a 20 20 20 28 6f 72 20 6d 61   may.**   (or ma
1533d 79 20 6e 6f 74 29 20 73 70 65 63 69 66 79 20 61  y not) specify a
1533e 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20   master-journal 
1533f 6e 61 6d 65 20 74 6f 20 62 65 20 77 72 69 74 74  name to be writt
15340 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20  en into the .** 
15341 20 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62    journal file b
15342 65 66 6f 72 65 20 69 74 20 69 73 20 73 79 6e 63  efore it is sync
15343 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a  ed to disk..**.*
15344 2a 20 20 20 57 68 65 74 68 65 72 20 6f 72 20 6e  *   Whether or n
15345 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ot a journal fil
15346 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 6d 61 73  e contains a mas
15347 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e  ter-journal poin
15348 74 65 72 20 61 66 66 65 63 74 73 20 0a 2a 2a 20  ter affects .** 
15349 20 20 74 68 65 20 77 61 79 20 69 6e 20 77 68 69    the way in whi
1534a 63 68 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ch the journal f
1534b 69 6c 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64  ile is finalized
1534c 20 61 66 74 65 72 20 74 68 65 20 74 72 61 6e 73   after the trans
1534d 61 63 74 69 6f 6e 20 69 73 20 0a 2a 2a 20 20 20  action is .**   
1534e 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c  committed or rol
1534f 6c 65 64 20 62 61 63 6b 20 77 68 65 6e 20 72 75  led back when ru
15350 6e 6e 69 6e 67 20 69 6e 20 22 6a 6f 75 72 6e 61  nning in "journa
15351 6c 5f 6d 6f 64 65 3d 50 45 52 53 49 53 54 22 20  l_mode=PERSIST" 
15352 6d 6f 64 65 2e 0a 2a 2a 20 20 20 49 66 20 61 20  mode..**   If a 
15353 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65  journal file doe
15354 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20  s not contain a 
15355 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70  master-journal p
15356 6f 69 6e 74 65 72 2c 20 69 74 20 69 73 0a 2a 2a  ointer, it is.**
15357 20 20 20 66 69 6e 61 6c 69 7a 65 64 20 62 79 20     finalized by 
15358 6f 76 65 72 77 72 69 74 69 6e 67 20 74 68 65 20  overwriting the 
15359 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65  first journal he
1535a 61 64 65 72 20 77 69 74 68 20 7a 65 72 6f 65 73  ader with zeroes
1535b 2e 20 49 66 0a 2a 2a 20 20 20 69 74 20 64 6f 65  . If.**   it doe
1535c 73 20 63 6f 6e 74 61 69 6e 20 61 20 6d 61 73 74  s contain a mast
1535d 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74  er-journal point
1535e 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  er the journal f
1535f 69 6c 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64  ile is finalized
15360 20 0a 2a 2a 20 20 20 62 79 20 74 72 75 6e 63 61   .**   by trunca
15361 74 69 6e 67 20 69 74 20 74 6f 20 7a 65 72 6f 20  ting it to zero 
15362 62 79 74 65 73 2c 20 6a 75 73 74 20 61 73 20 69  bytes, just as i
15363 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  f the connection
15364 20 77 65 72 65 20 0a 2a 2a 20 20 20 72 75 6e 6e   were .**   runn
15365 69 6e 67 20 69 6e 20 22 6a 6f 75 72 6e 61 6c 5f  ing in "journal_
15366 6d 6f 64 65 3d 74 72 75 6e 63 61 74 65 22 20 6d  mode=truncate" m
15367 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 4a 6f 75  ode..**.**   Jou
15368 72 6e 61 6c 20 66 69 6c 65 73 20 74 68 61 74 20  rnal files that 
15369 63 6f 6e 74 61 69 6e 20 6d 61 73 74 65 72 20 6a  contain master j
1536a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 73 20  ournal pointers 
1536b 63 61 6e 6e 6f 74 20 62 65 20 66 69 6e 61 6c 69  cannot be finali
1536c 7a 65 64 0a 2a 2a 20 20 20 73 69 6d 70 6c 79 20  zed.**   simply 
1536d 62 79 20 6f 76 65 72 77 72 69 74 69 6e 67 20 74  by overwriting t
1536e 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c  he first journal
1536f 2d 68 65 61 64 65 72 20 77 69 74 68 20 7a 65 72  -header with zer
15370 6f 65 73 2c 20 61 73 20 74 68 65 0a 2a 2a 20 20  oes, as the.**  
15371 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
15372 70 6f 69 6e 74 65 72 20 63 6f 75 6c 64 20 69 6e  pointer could in
15373 74 65 72 66 65 72 65 20 77 69 74 68 20 68 6f 74  terfere with hot
15374 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63  -journal rollbac
15375 6b 20 6f 66 20 61 6e 79 0a 2a 2a 20 20 20 73 75  k of any.**   su
15376 62 73 65 71 75 65 6e 74 6c 79 20 69 6e 74 65 72  bsequently inter
15377 72 75 70 74 65 64 20 74 72 61 6e 73 61 63 74 69  rupted transacti
15378 6f 6e 20 74 68 61 74 20 72 65 75 73 65 73 20 74  on that reuses t
15379 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
1537a 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 66 6c 61  .**.**   The fla
1537b 67 20 69 73 20 63 6c 65 61 72 65 64 20 61 73 20  g is cleared as 
1537c 73 6f 6f 6e 20 61 73 20 74 68 65 20 6a 6f 75 72  soon as the jour
1537d 6e 61 6c 20 66 69 6c 65 20 69 73 20 66 69 6e 61  nal file is fina
1537e 6c 69 7a 65 64 20 28 65 69 74 68 65 72 0a 2a 2a  lized (either.**
1537f 20 20 20 62 79 20 50 61 67 65 72 43 6f 6d 6d 69     by PagerCommi
15380 74 50 68 61 73 65 54 77 6f 20 6f 72 20 50 61 67  tPhaseTwo or Pag
15381 65 72 52 6f 6c 6c 62 61 63 6b 29 2e 20 49 66 20  erRollback). If 
15382 61 6e 20 49 4f 20 65 72 72 6f 72 20 70 72 65 76  an IO error prev
15383 65 6e 74 73 20 74 68 65 0a 2a 2a 20 20 20 6a 6f  ents the.**   jo
15384 75 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20  urnal file from 
15385 62 65 69 6e 67 20 73 75 63 63 65 73 73 66 75 6c  being successful
15386 6c 79 20 66 69 6e 61 6c 69 7a 65 64 2c 20 74 68  ly finalized, th
15387 65 20 73 65 74 4d 61 73 74 65 72 20 66 6c 61 67  e setMaster flag
15388 0a 2a 2a 20 20 20 69 73 20 63 6c 65 61 72 65 64  .**   is cleared
15389 20 61 6e 79 77 61 79 20 28 61 6e 64 20 74 68 65   anyway (and the
1538a 20 70 61 67 65 72 20 77 69 6c 6c 20 6d 6f 76 65   pager will move
1538b 20 74 6f 20 45 52 52 4f 52 20 73 74 61 74 65 29   to ERROR state)
1538c 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e 6f 74 53 70 69  ..**.** doNotSpi
1538d 6c 6c 2c 20 64 6f 4e 6f 74 53 79 6e 63 53 70 69  ll, doNotSyncSpi
1538e 6c 6c 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 73 65  ll.**.**   These
1538f 20 74 77 6f 20 62 6f 6f 6c 65 61 6e 20 76 61 72   two boolean var
15390 69 61 62 6c 65 73 20 63 6f 6e 74 72 6f 6c 20 74  iables control t
15391 68 65 20 62 65 68 61 76 69 6f 75 72 20 6f 66 20  he behaviour of 
15392 63 61 63 68 65 2d 73 70 69 6c 6c 73 0a 2a 2a 20  cache-spills.** 
15393 20 20 28 63 61 6c 6c 73 20 6d 61 64 65 20 62 79    (calls made by
15394 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75   the pcache modu
15395 6c 65 20 74 6f 20 74 68 65 20 70 61 67 65 72 53  le to the pagerS
15396 74 72 65 73 73 28 29 20 72 6f 75 74 69 6e 65 20  tress() routine 
15397 74 6f 0a 2a 2a 20 20 20 77 72 69 74 65 20 63 61  to.**   write ca
15398 63 68 65 64 20 64 61 74 61 20 74 6f 20 74 68 65  ched data to the
15399 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 69 6e 20   file-system in 
1539a 6f 72 64 65 72 20 74 6f 20 66 72 65 65 20 75 70  order to free up
1539b 20 6d 65 6d 6f 72 79 29 2e 0a 2a 2a 0a 2a 2a 20   memory)..**.** 
1539c 20 20 57 68 65 6e 20 64 6f 4e 6f 74 53 70 69 6c    When doNotSpil
1539d 6c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 77  l is non-zero, w
1539e 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61  riting to the da
1539f 74 61 62 61 73 65 20 66 72 6f 6d 20 70 61 67 65  tabase from page
153a0 72 53 74 72 65 73 73 28 29 0a 2a 2a 20 20 20 69  rStress().**   i
153a1 73 20 64 69 73 61 62 6c 65 64 20 61 6c 74 6f 67  s disabled altog
153a2 65 74 68 65 72 2e 20 54 68 69 73 20 69 73 20 64  ether. This is d
153a3 6f 6e 65 20 69 6e 20 61 20 76 65 72 79 20 6f 62  one in a very ob
153a4 73 63 75 72 65 20 63 61 73 65 20 74 68 61 74 0a  scure case that.
153a5 2a 2a 20 20 20 63 6f 6d 65 73 20 75 70 20 64 75  **   comes up du
153a6 72 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 20 72  ring savepoint r
153a7 6f 6c 6c 62 61 63 6b 20 74 68 61 74 20 72 65 71  ollback that req
153a8 75 69 72 65 73 20 74 68 65 20 70 63 61 63 68 65  uires the pcache
153a9 20 6d 6f 64 75 6c 65 0a 2a 2a 20 20 20 74 6f 20   module.**   to 
153aa 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  allocate a new p
153ab 61 67 65 20 74 6f 20 70 72 65 76 65 6e 74 20 74  age to prevent t
153ac 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
153ad 66 72 6f 6d 20 62 65 69 6e 67 20 77 72 69 74 74  from being writt
153ae 65 6e 0a 2a 2a 20 20 20 77 68 69 6c 65 20 69 74  en.**   while it
153af 20 69 73 20 62 65 69 6e 67 20 74 72 61 76 65 72   is being traver
153b0 73 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 70  sed by code in p
153b1 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 2e  ager_playback().
153b2 0a 2a 2a 20 0a 2a 2a 20 20 20 49 66 20 64 6f 4e  .** .**   If doN
153b3 6f 74 53 79 6e 63 53 70 69 6c 6c 20 69 73 20 6e  otSyncSpill is n
153b4 6f 6e 2d 7a 65 72 6f 2c 20 77 72 69 74 69 6e 67  on-zero, writing
153b5 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
153b6 20 66 72 6f 6d 20 70 61 67 65 72 53 74 72 65 73   from pagerStres
153b7 73 28 29 0a 2a 2a 20 20 20 69 73 20 70 65 72 6d  s().**   is perm
153b8 69 74 74 65 64 2c 20 62 75 74 20 73 79 6e 63 69  itted, but synci
153b9 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ng the journal f
153ba 69 6c 65 20 69 73 20 6e 6f 74 2e 20 54 68 69 73  ile is not. This
153bb 20 66 6c 61 67 20 69 73 20 73 65 74 0a 2a 2a 20   flag is set.** 
153bc 20 20 62 79 20 73 71 6c 69 74 65 33 50 61 67 65    by sqlite3Page
153bd 72 57 72 69 74 65 28 29 20 77 68 65 6e 20 74 68  rWrite() when th
153be 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 65  e file-system se
153bf 63 74 6f 72 2d 73 69 7a 65 20 69 73 20 6c 61 72  ctor-size is lar
153c0 67 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 74 68  ger than.**   th
153c1 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2d  e database page-
153c2 73 69 7a 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  size in order to
153c3 20 70 72 65 76 65 6e 74 20 61 20 6a 6f 75 72 6e   prevent a journ
153c4 61 6c 20 73 79 6e 63 20 66 72 6f 6d 20 68 61 70  al sync from hap
153c5 70 65 6e 69 6e 67 20 0a 2a 2a 20 20 20 69 6e 20  pening .**   in 
153c6 62 65 74 77 65 65 6e 20 74 68 65 20 6a 6f 75 72  between the jour
153c7 6e 61 6c 6c 69 6e 67 20 6f 66 20 74 77 6f 20 70  nalling of two p
153c8 61 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65  ages on the same
153c9 20 73 65 63 74 6f 72 2e 20 0a 2a 2a 0a 2a 2a 20   sector. .**.** 
153ca 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 0a 2a 2a 0a  subjInMemory.**.
153cb 2a 2a 20 20 20 54 68 69 73 20 69 73 20 61 20 62  **   This is a b
153cc 6f 6f 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 2e  oolean variable.
153cd 20 49 66 20 74 72 75 65 2c 20 74 68 65 6e 20 61   If true, then a
153ce 6e 79 20 72 65 71 75 69 72 65 64 20 73 75 62 2d  ny required sub-
153cf 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 69 73 20  journal.**   is 
153d0 6f 70 65 6e 65 64 20 61 73 20 61 6e 20 69 6e 2d  opened as an in-
153d1 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66  memory journal f
153d2 69 6c 65 2e 20 49 66 20 66 61 6c 73 65 2c 20 74  ile. If false, t
153d3 68 65 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a  hen in-memory.**
153d4 20 20 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20     sub-journals 
153d5 61 72 65 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f  are only used fo
153d6 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65  r in-memory page
153d7 72 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  r files..**.**  
153d8 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 69   This variable i
153d9 73 20 75 70 64 61 74 65 64 20 62 79 20 74 68 65  s updated by the
153da 20 75 70 70 65 72 20 6c 61 79 65 72 20 65 61 63   upper layer eac
153db 68 20 74 69 6d 65 20 61 20 6e 65 77 20 0a 2a 2a  h time a new .**
153dc 20 20 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63     write-transac
153dd 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2e 0a  tion is opened..
153de 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65 2c 20 64 62  **.** dbSize, db
153df 4f 72 69 67 53 69 7a 65 2c 20 64 62 46 69 6c 65  OrigSize, dbFile
153e0 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 20 20 56 61 72  Size.**.**   Var
153e1 69 61 62 6c 65 20 64 62 53 69 7a 65 20 69 73 20  iable dbSize is 
153e2 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
153e3 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
153e4 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
153e5 0a 2a 2a 20 20 20 49 74 20 69 73 20 76 61 6c 69  .**   It is vali
153e6 64 20 69 6e 20 50 41 47 45 52 5f 52 45 41 44 45  d in PAGER_READE
153e7 52 20 61 6e 64 20 68 69 67 68 65 72 20 73 74 61  R and higher sta
153e8 74 65 73 20 28 61 6c 6c 20 73 74 61 74 65 73 20  tes (all states 
153e9 65 78 63 65 70 74 20 66 6f 72 0a 2a 2a 20 20 20  except for.**   
153ea 4f 50 45 4e 20 61 6e 64 20 45 52 52 4f 52 29 2e  OPEN and ERROR).
153eb 20 0a 2a 2a 0a 2a 2a 20 20 20 64 62 53 69 7a 65   .**.**   dbSize
153ec 20 69 73 20 73 65 74 20 62 61 73 65 64 20 6f 6e   is set based on
153ed 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
153ee 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
153ef 77 68 69 63 68 20 6d 61 79 20 62 65 20 0a 2a 2a  which may be .**
153f0 20 20 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74     larger than t
153f1 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64  he size of the d
153f2 61 74 61 62 61 73 65 20 28 74 68 65 20 76 61 6c  atabase (the val
153f3 75 65 20 73 74 6f 72 65 64 20 61 74 20 6f 66 66  ue stored at off
153f4 73 65 74 0a 2a 2a 20 20 20 32 38 20 6f 66 20 74  set.**   28 of t
153f5 68 65 20 64 61 74 61 62 61 73 65 20 68 65 61 64  he database head
153f6 65 72 20 62 79 20 74 68 65 20 62 74 72 65 65 29  er by the btree)
153f7 2e 20 49 66 20 74 68 65 20 73 69 7a 65 20 6f 66  . If the size of
153f8 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 20 20 69   the file.**   i
153f9 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72  s not an integer
153fa 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 74 68 65   multiple of the
153fb 20 70 61 67 65 2d 73 69 7a 65 2c 20 74 68 65 20   page-size, the 
153fc 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 0a  value stored in.
153fd 2a 2a 20 20 20 64 62 53 69 7a 65 20 69 73 20 72  **   dbSize is r
153fe 6f 75 6e 64 65 64 20 64 6f 77 6e 20 28 69 2e 65  ounded down (i.e
153ff 2e 20 61 20 35 4b 42 20 66 69 6c 65 20 77 69 74  . a 5KB file wit
15400 68 20 32 4b 20 70 61 67 65 2d 73 69 7a 65 20 68  h 2K page-size h
15401 61 73 20 64 62 53 69 7a 65 3d 3d 32 29 2e 0a 2a  as dbSize==2)..*
15402 2a 20 20 20 45 78 63 65 70 74 2c 20 61 6e 79 20  *   Except, any 
15403 66 69 6c 65 20 74 68 61 74 20 69 73 20 67 72 65  file that is gre
15404 61 74 65 72 20 74 68 61 6e 20 30 20 62 79 74 65  ater than 0 byte
15405 73 20 69 6e 20 73 69 7a 65 20 69 73 20 63 6f 6e  s in size is con
15406 73 69 64 65 72 65 64 0a 2a 2a 20 20 20 74 6f 20  sidered.**   to 
15407 68 61 76 65 20 61 74 20 6c 65 61 73 74 20 6f 6e  have at least on
15408 65 20 70 61 67 65 2e 20 28 69 2e 65 2e 20 61 20  e page. (i.e. a 
15409 31 4b 42 20 66 69 6c 65 20 77 69 74 68 20 32 4b  1KB file with 2K
1540a 20 70 61 67 65 2d 73 69 7a 65 20 6c 65 61 64 73   page-size leads
1540b 0a 2a 2a 20 20 20 74 6f 20 64 62 53 69 7a 65 3d  .**   to dbSize=
1540c 3d 31 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 75 72  =1)..**.**   Dur
1540d 69 6e 67 20 61 20 77 72 69 74 65 2d 74 72 61 6e  ing a write-tran
1540e 73 61 63 74 69 6f 6e 2c 20 69 66 20 70 61 67 65  saction, if page
1540f 73 20 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62  s with page-numb
15410 65 72 73 20 67 72 65 61 74 65 72 20 74 68 61 6e  ers greater than
15411 0a 2a 2a 20 20 20 64 62 53 69 7a 65 20 61 72 65  .**   dbSize are
15412 20 6d 6f 64 69 66 69 65 64 20 69 6e 20 74 68 65   modified in the
15413 20 63 61 63 68 65 2c 20 64 62 53 69 7a 65 20 69   cache, dbSize i
15414 73 20 75 70 64 61 74 65 64 20 61 63 63 6f 72 64  s updated accord
15415 69 6e 67 6c 79 2e 0a 2a 2a 20 20 20 53 69 6d 69  ingly..**   Simi
15416 6c 61 72 6c 79 2c 20 69 66 20 74 68 65 20 64 61  larly, if the da
15417 74 61 62 61 73 65 20 69 73 20 74 72 75 6e 63 61  tabase is trunca
15418 74 65 64 20 75 73 69 6e 67 20 50 61 67 65 72 54  ted using PagerT
15419 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29 2c 20  runcateImage(), 
1541a 0a 2a 2a 20 20 20 64 62 53 69 7a 65 20 69 73 20  .**   dbSize is 
1541b 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20  updated..**.**  
1541c 20 56 61 72 69 61 62 6c 65 73 20 64 62 4f 72 69   Variables dbOri
1541d 67 53 69 7a 65 20 61 6e 64 20 64 62 46 69 6c 65  gSize and dbFile
1541e 53 69 7a 65 20 61 72 65 20 76 61 6c 69 64 20 69  Size are valid i
1541f 6e 20 73 74 61 74 65 73 20 0a 2a 2a 20 20 20 50  n states .**   P
15420 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b  AGER_WRITER_LOCK
15421 45 44 20 61 6e 64 20 68 69 67 68 65 72 2e 20 64  ED and higher. d
15422 62 4f 72 69 67 53 69 7a 65 20 69 73 20 61 20 63  bOrigSize is a c
15423 6f 70 79 20 6f 66 20 74 68 65 20 64 62 53 69 7a  opy of the dbSiz
15424 65 0a 2a 2a 20 20 20 76 61 72 69 61 62 6c 65 20  e.**   variable 
15425 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
15426 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  the transaction.
15427 20 49 74 20 69 73 20 75 73 65 64 20 64 75 72 69   It is used duri
15428 6e 67 20 72 6f 6c 6c 62 61 63 6b 2c 0a 2a 2a 20  ng rollback,.** 
15429 20 20 61 6e 64 20 74 6f 20 64 65 74 65 72 6d 69    and to determi
1542a 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ne whether or no
1542b 74 20 70 61 67 65 73 20 6e 65 65 64 20 74 6f 20  t pages need to 
1542c 62 65 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 62 65  be journalled be
1542d 66 6f 72 65 0a 2a 2a 20 20 20 62 65 69 6e 67 20  fore.**   being 
1542e 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20  modified..**.** 
1542f 20 20 54 68 72 6f 75 67 68 6f 75 74 20 61 20 77    Throughout a w
15430 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
15431 2c 20 64 62 46 69 6c 65 53 69 7a 65 20 63 6f 6e  , dbFileSize con
15432 74 61 69 6e 73 20 74 68 65 20 73 69 7a 65 20 6f  tains the size o
15433 66 0a 2a 2a 20 20 20 74 68 65 20 66 69 6c 65 20  f.**   the file 
15434 6f 6e 20 64 69 73 6b 20 69 6e 20 70 61 67 65 73  on disk in pages
15435 2e 20 49 74 20 69 73 20 73 65 74 20 74 6f 20 61  . It is set to a
15436 20 63 6f 70 79 20 6f 66 20 64 62 53 69 7a 65 20   copy of dbSize 
15437 77 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20 77 72  when the.**   wr
15438 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite-transaction 
15439 69 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2c  is first opened,
1543a 20 61 6e 64 20 75 70 64 61 74 65 64 20 77 68 65   and updated whe
1543b 6e 20 56 46 53 20 63 61 6c 6c 73 20 61 72 65 20  n VFS calls are 
1543c 6d 61 64 65 0a 2a 2a 20 20 20 74 6f 20 77 72 69  made.**   to wri
1543d 74 65 20 6f 72 20 74 72 75 6e 63 61 74 65 20 74  te or truncate t
1543e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1543f 20 6f 6e 20 64 69 73 6b 2e 20 0a 2a 2a 0a 2a 2a   on disk. .**.**
15440 20 20 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 73     The only reas
15441 6f 6e 20 74 68 65 20 64 62 46 69 6c 65 53 69 7a  on the dbFileSiz
15442 65 20 76 61 72 69 61 62 6c 65 20 69 73 20 72 65  e variable is re
15443 71 75 69 72 65 64 20 69 73 20 74 6f 20 73 75 70  quired is to sup
15444 70 72 65 73 73 20 0a 2a 2a 20 20 20 75 6e 6e 65  press .**   unne
15445 63 65 73 73 61 72 79 20 63 61 6c 6c 73 20 74 6f  cessary calls to
15446 20 78 54 72 75 6e 63 61 74 65 28 29 20 61 66 74   xTruncate() aft
15447 65 72 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20  er committing a 
15448 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 2c  transaction. If,
15449 20 0a 2a 2a 20 20 20 77 68 65 6e 20 61 20 74 72   .**   when a tr
1544a 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d  ansaction is com
1544b 6d 69 74 74 65 64 2c 20 74 68 65 20 64 62 46 69  mitted, the dbFi
1544c 6c 65 53 69 7a 65 20 76 61 72 69 61 62 6c 65 20  leSize variable 
1544d 69 6e 64 69 63 61 74 65 73 20 0a 2a 2a 20 20 20  indicates .**   
1544e 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73  that the databas
1544f 65 20 66 69 6c 65 20 69 73 20 6c 61 72 67 65 72  e file is larger
15450 20 74 68 61 6e 20 74 68 65 20 64 61 74 61 62 61   than the databa
15451 73 65 20 69 6d 61 67 65 20 28 50 61 67 65 72 2e  se image (Pager.
15452 64 62 53 69 7a 65 29 2c 20 0a 2a 2a 20 20 20 70  dbSize), .**   p
15453 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 29 20  ager_truncate() 
15454 69 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 20 70  is called. The p
15455 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 29 20  ager_truncate() 
15456 63 61 6c 6c 20 75 73 65 73 20 78 46 69 6c 65 73  call uses xFiles
15457 69 7a 65 28 29 0a 2a 2a 20 20 20 74 6f 20 6d 65  ize().**   to me
15458 61 73 75 72 65 20 74 68 65 20 64 61 74 61 62 61  asure the databa
15459 73 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2c  se file on disk,
1545a 20 61 6e 64 20 74 68 65 6e 20 74 72 75 6e 63 61   and then trunca
1545b 74 65 73 20 69 74 20 69 66 20 72 65 71 75 69 72  tes it if requir
1545c 65 64 2e 0a 2a 2a 20 20 20 64 62 46 69 6c 65 53  ed..**   dbFileS
1545d 69 7a 65 20 69 73 20 6e 6f 74 20 75 73 65 64 20  ize is not used 
1545e 77 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63  when rolling bac
1545f 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  k a transaction.
15460 20 49 6e 20 74 68 69 73 20 63 61 73 65 0a 2a 2a   In this case.**
15461 20 20 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74     pager_truncat
15462 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 75 6e  e() is called un
15463 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20 28 77  conditionally (w
15464 68 69 63 68 20 6d 65 61 6e 73 20 74 68 65 72 65  hich means there
15465 20 6d 61 79 20 62 65 0a 2a 2a 20 20 20 61 20 63   may be.**   a c
15466 61 6c 6c 20 74 6f 20 78 46 69 6c 65 73 69 7a 65  all to xFilesize
15467 28 29 20 74 68 61 74 20 69 73 20 6e 6f 74 20 73  () that is not s
15468 74 72 69 63 74 6c 79 20 72 65 71 75 69 72 65 64  trictly required
15469 29 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 73  ). In either cas
1546a 65 2c 0a 2a 2a 20 20 20 70 61 67 65 72 5f 74 72  e,.**   pager_tr
1546b 75 6e 63 61 74 65 28 29 20 6d 61 79 20 63 61 75  uncate() may cau
1546c 73 65 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62  se the file to b
1546d 65 63 6f 6d 65 20 73 6d 61 6c 6c 65 72 20 6f 72  ecome smaller or
1546e 20 6c 61 72 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 64   larger..**.** d
1546f 62 48 69 6e 74 53 69 7a 65 0a 2a 2a 0a 2a 2a 20  bHintSize.**.** 
15470 20 20 54 68 65 20 64 62 48 69 6e 74 53 69 7a 65    The dbHintSize
15471 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65   variable is use
15472 64 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 6e  d to limit the n
15473 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 6d  umber of calls m
15474 61 64 65 20 74 6f 0a 2a 2a 20 20 20 74 68 65 20  ade to.**   the 
15475 56 46 53 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c  VFS xFileControl
15476 28 46 43 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e 54  (FCNTL_SIZE_HINT
15477 29 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a  ) method. .**.**
15478 20 20 20 64 62 48 69 6e 74 53 69 7a 65 20 69 73     dbHintSize is
15479 20 73 65 74 20 74 6f 20 61 20 63 6f 70 79 20 6f   set to a copy o
1547a 66 20 74 68 65 20 64 62 53 69 7a 65 20 76 61 72  f the dbSize var
1547b 69 61 62 6c 65 20 77 68 65 6e 20 61 0a 2a 2a 20  iable when a.** 
1547c 20 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74    write-transact
1547d 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 20 28 61  ion is opened (a
1547e 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20  t the same time 
1547f 61 73 20 64 62 46 69 6c 65 53 69 7a 65 20 61 6e  as dbFileSize an
15480 64 0a 2a 2a 20 20 20 64 62 4f 72 69 67 53 69 7a  d.**   dbOrigSiz
15481 65 29 2e 20 49 66 20 74 68 65 20 78 46 69 6c 65  e). If the xFile
15482 43 6f 6e 74 72 6f 6c 28 46 43 4e 54 4c 5f 53 49  Control(FCNTL_SI
15483 5a 45 5f 48 49 4e 54 29 20 6d 65 74 68 6f 64 20  ZE_HINT) method 
15484 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 20 20  is called,.**   
15485 64 62 48 69 6e 74 53 69 7a 65 20 69 73 20 69 6e  dbHintSize is in
15486 63 72 65 61 73 65 64 20 74 6f 20 74 68 65 20 6e  creased to the n
15487 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74  umber of pages t
15488 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 20 74  hat correspond t
15489 6f 20 74 68 65 0a 2a 2a 20 20 20 73 69 7a 65 2d  o the.**   size-
1548a 68 69 6e 74 20 70 61 73 73 65 64 20 74 6f 20 74  hint passed to t
1548b 68 65 20 6d 65 74 68 6f 64 20 63 61 6c 6c 2e 20  he method call. 
1548c 53 65 65 20 70 61 67 65 72 5f 77 72 69 74 65 5f  See pager_write_
1548d 70 61 67 65 6c 69 73 74 28 29 20 66 6f 72 20 0a  pagelist() for .
1548e 2a 2a 20 20 20 64 65 74 61 69 6c 73 2e 0a 2a 2a  **   details..**
1548f 0a 2a 2a 20 65 72 72 43 6f 64 65 0a 2a 2a 0a 2a  .** errCode.**.*
15490 2a 20 20 20 54 68 65 20 50 61 67 65 72 2e 65 72  *   The Pager.er
15491 72 43 6f 64 65 20 76 61 72 69 61 62 6c 65 20 69  rCode variable i
15492 73 20 6f 6e 6c 79 20 65 76 65 72 20 75 73 65 64  s only ever used
15493 20 69 6e 20 50 41 47 45 52 5f 45 52 52 4f 52 20   in PAGER_ERROR 
15494 73 74 61 74 65 2e 20 49 74 0a 2a 2a 20 20 20 69  state. It.**   i
15495 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 20 69 6e  s set to zero in
15496 20 61 6c 6c 20 6f 74 68 65 72 20 73 74 61 74 65   all other state
15497 73 2e 20 49 6e 20 50 41 47 45 52 5f 45 52 52 4f  s. In PAGER_ERRO
15498 52 20 73 74 61 74 65 2c 20 50 61 67 65 72 2e 65  R state, Pager.e
15499 72 72 43 6f 64 65 20 0a 2a 2a 20 20 20 69 73 20  rrCode .**   is 
1549a 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20 53 51  always set to SQ
1549b 4c 49 54 45 5f 46 55 4c 4c 2c 20 53 51 4c 49 54  LITE_FULL, SQLIT
1549c 45 5f 49 4f 45 52 52 20 6f 72 20 6f 6e 65 20 6f  E_IOERR or one o
1549d 66 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f 45  f the SQLITE_IOE
1549e 52 52 5f 58 58 58 20 0a 2a 2a 20 20 20 73 75 62  RR_XXX .**   sub
1549f 2d 63 6f 64 65 73 2e 0a 2a 2f 0a 73 74 72 75 63  -codes..*/.struc
154a0 74 20 50 61 67 65 72 20 7b 0a 20 20 73 71 6c 69  t Pager {.  sqli
154a1 74 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20  te3_vfs *pVfs;  
154a2 20 20 20 20 20 20 20 20 2f 2a 20 4f 53 20 66 75          /* OS fu
154a3 6e 63 74 69 6f 6e 73 20 74 6f 20 75 73 65 20 66  nctions to use f
154a4 6f 72 20 49 4f 20 2a 2f 0a 20 20 75 38 20 65 78  or IO */.  u8 ex
154a5 63 6c 75 73 69 76 65 4d 6f 64 65 3b 20 20 20 20  clusiveMode;    
154a6 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61         /* Boolea
154a7 6e 2e 20 54 72 75 65 20 69 66 20 6c 6f 63 6b 69  n. True if locki
154a8 6e 67 5f 6d 6f 64 65 3d 3d 45 58 43 4c 55 53 49  ng_mode==EXCLUSI
154a9 56 45 20 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e  VE */.  u8 journ
154aa 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20  alMode;         
154ab 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68      /* One of th
154ac 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  e PAGER_JOURNALM
154ad 4f 44 45 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a  ODE_* values */.
154ae 20 20 75 38 20 75 73 65 4a 6f 75 72 6e 61 6c 3b    u8 useJournal;
154af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
154b0 20 55 73 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20   Use a rollback 
154b1 6a 6f 75 72 6e 61 6c 20 6f 6e 20 74 68 69 73 20  journal on this 
154b2 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6e 6f 53  file */.  u8 noS
154b3 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  ync;            
154b4 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20        /* Do not 
154b5 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c  sync the journal
154b6 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 38   if true */.  u8
154b7 20 66 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20 20   fullSync;      
154b8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20            /* Do 
154b9 65 78 74 72 61 20 73 79 6e 63 73 20 6f 66 20 74  extra syncs of t
154ba 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72  he journal for r
154bb 6f 62 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20 75  obustness */.  u
154bc 38 20 63 6b 70 74 53 79 6e 63 46 6c 61 67 73 3b  8 ckptSyncFlags;
154bd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 59             /* SY
154be 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 53 59 4e  NC_NORMAL or SYN
154bf 43 5f 46 55 4c 4c 20 66 6f 72 20 63 68 65 63 6b  C_FULL for check
154c0 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20 77 61  point */.  u8 wa
154c1 6c 53 79 6e 63 46 6c 61 67 73 3b 20 20 20 20 20  lSyncFlags;     
154c2 20 20 20 20 20 20 20 2f 2a 20 53 59 4e 43 5f 4e         /* SYNC_N
154c3 4f 52 4d 41 4c 20 6f 72 20 53 59 4e 43 5f 46 55  ORMAL or SYNC_FU
154c4 4c 4c 20 66 6f 72 20 77 61 6c 20 77 72 69 74 65  LL for wal write
154c5 73 20 2a 2f 0a 20 20 75 38 20 73 79 6e 63 46 6c  s */.  u8 syncFl
154c6 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
154c7 20 20 20 2f 2a 20 53 59 4e 43 5f 4e 4f 52 4d 41     /* SYNC_NORMA
154c8 4c 20 6f 72 20 53 59 4e 43 5f 46 55 4c 4c 20 6f  L or SYNC_FULL o
154c9 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20 75 38  therwise */.  u8
154ca 20 74 65 6d 70 46 69 6c 65 3b 20 20 20 20 20 20   tempFile;      
154cb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69            /* zFi
154cc 6c 65 6e 61 6d 65 20 69 73 20 61 20 74 65 6d 70  lename is a temp
154cd 6f 72 61 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20  orary file */.  
154ce 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20  u8 readOnly;    
154cf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
154d0 72 75 65 20 66 6f 72 20 61 20 72 65 61 64 2d 6f  rue for a read-o
154d1 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  nly database */.
154d2 20 20 75 38 20 6d 65 6d 44 62 3b 20 20 20 20 20    u8 memDb;     
154d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
154d4 20 54 72 75 65 20 74 6f 20 69 6e 68 69 62 69 74   True to inhibit
154d5 20 61 6c 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f   all file I/O */
154d6 0a 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..  /***********
154d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
154d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
154d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
154da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
154db 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    ** The followi
154dc 6e 67 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 69 6e  ng block contain
154dd 73 20 74 68 6f 73 65 20 63 6c 61 73 73 20 6d 65  s those class me
154de 6d 62 65 72 73 20 74 68 61 74 20 63 68 61 6e 67  mbers that chang
154df 65 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 72 6f  e during.  ** ro
154e0 75 74 69 6e 65 20 6f 70 65 72 74 69 6f 6e 2e 20  utine opertion. 
154e1 20 43 6c 61 73 73 20 6d 65 6d 62 65 72 73 20 6e   Class members n
154e2 6f 74 20 69 6e 20 74 68 69 73 20 62 6c 6f 63 6b  ot in this block
154e3 20 61 72 65 20 65 69 74 68 65 72 20 66 69 78 65   are either fixe
154e4 64 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20  d.  ** when the 
154e5 70 61 67 65 72 20 69 73 20 66 69 72 73 74 20 63  pager is first c
154e6 72 65 61 74 65 64 20 6f 72 20 65 6c 73 65 20 6f  reated or else o
154e7 6e 6c 79 20 63 68 61 6e 67 65 20 77 68 65 6e 20  nly change when 
154e8 74 68 65 72 65 20 69 73 20 61 0a 20 20 2a 2a 20  there is a.  ** 
154e9 73 69 67 6e 69 66 69 63 61 6e 74 20 6d 6f 64 65  significant mode
154ea 20 63 68 61 6e 67 65 20 28 73 75 63 68 20 61 73   change (such as
154eb 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 70 61   changing the pa
154ec 67 65 5f 73 69 7a 65 2c 20 6c 6f 63 6b 69 6e 67  ge_size, locking
154ed 5f 6d 6f 64 65 2c 0a 20 20 2a 2a 20 6f 72 20 74  _mode,.  ** or t
154ee 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 29  he journal_mode)
154ef 2e 20 20 46 72 6f 6d 20 61 6e 6f 74 68 65 72 20  .  From another 
154f0 76 69 65 77 2c 20 74 68 65 73 65 20 63 6c 61 73  view, these clas
154f1 73 20 6d 65 6d 62 65 72 73 20 64 65 73 63 72 69  s members descri
154f2 62 65 0a 20 20 2a 2a 20 74 68 65 20 22 73 74 61  be.  ** the "sta
154f3 74 65 22 20 6f 66 20 74 68 65 20 70 61 67 65 72  te" of the pager
154f4 2c 20 77 68 69 6c 65 20 6f 74 68 65 72 20 63 6c  , while other cl
154f5 61 73 73 20 6d 65 6d 62 65 72 73 20 64 65 73 63  ass members desc
154f6 72 69 62 65 20 74 68 65 0a 20 20 2a 2a 20 22 63  ribe the.  ** "c
154f7 6f 6e 66 69 67 75 72 61 74 69 6f 6e 22 20 6f 66  onfiguration" of
154f8 20 74 68 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f   the pager..  */
154f9 0a 20 20 75 38 20 65 53 74 61 74 65 3b 20 20 20  .  u8 eState;   
154fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
154fb 2a 20 50 61 67 65 72 20 73 74 61 74 65 20 28 4f  * Pager state (O
154fc 50 45 4e 2c 20 52 45 41 44 45 52 2c 20 57 52 49  PEN, READER, WRI
154fd 54 45 52 5f 4c 4f 43 4b 45 44 2e 2e 29 20 2a 2f  TER_LOCKED..) */
154fe 0a 20 20 75 38 20 65 4c 6f 63 6b 3b 20 20 20 20  .  u8 eLock;    
154ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15500 2a 20 43 75 72 72 65 6e 74 20 6c 6f 63 6b 20 68  * Current lock h
15501 65 6c 64 20 6f 6e 20 64 61 74 61 62 61 73 65 20  eld on database 
15502 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 63 68 61  file */.  u8 cha
15503 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20  ngeCountDone;   
15504 20 20 20 20 20 20 2f 2a 20 53 65 74 20 61 66 74        /* Set aft
15505 65 72 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20  er incrementing 
15506 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
15507 65 72 20 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61  er */.  u8 setMa
15508 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  ster;           
15509 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
1550a 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 73 20 62 65   m-j name has be
1550b 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 72  en written to jr
1550c 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74  nl */.  u8 doNot
1550d 53 70 69 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  Spill;          
1550e 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 70      /* Do not sp
1550f 69 6c 6c 20 74 68 65 20 63 61 63 68 65 20 77 68  ill the cache wh
15510 65 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 2a 2f 0a 20  en non-zero */. 
15511 20 75 38 20 64 6f 4e 6f 74 53 79 6e 63 53 70 69   u8 doNotSyncSpi
15512 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ll;          /* 
15513 44 6f 20 6e 6f 74 20 64 6f 20 61 20 73 70 69 6c  Do not do a spil
15514 6c 20 74 68 61 74 20 72 65 71 75 69 72 65 73 20  l that requires 
15515 6a 72 6e 6c 20 73 79 6e 63 20 2a 2f 0a 20 20 75  jrnl sync */.  u
15516 38 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 3b 20  8 subjInMemory; 
15517 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
15518 75 65 20 74 6f 20 75 73 65 20 69 6e 2d 6d 65 6d  ue to use in-mem
15519 6f 72 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73  ory sub-journals
1551a 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 53 69 7a   */.  Pgno dbSiz
1551b 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1551c 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
1551d 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61  ages in the data
1551e 62 61 73 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 64  base */.  Pgno d
1551f 62 4f 72 69 67 53 69 7a 65 3b 20 20 20 20 20 20  bOrigSize;      
15520 20 20 20 20 20 20 2f 2a 20 64 62 53 69 7a 65 20        /* dbSize 
15521 62 65 66 6f 72 65 20 74 68 65 20 63 75 72 72 65  before the curre
15522 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a  nt transaction *
15523 2f 0a 20 20 50 67 6e 6f 20 64 62 46 69 6c 65 53  /.  Pgno dbFileS
15524 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
15525 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
15526 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  es in the databa
15527 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e  se file */.  Pgn
15528 6f 20 64 62 48 69 6e 74 53 69 7a 65 3b 20 20 20  o dbHintSize;   
15529 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
1552a 65 20 70 61 73 73 65 64 20 74 6f 20 46 43 4e 54  e passed to FCNT
1552b 4c 5f 53 49 5a 45 5f 48 49 4e 54 20 63 61 6c 6c  L_SIZE_HINT call
1552c 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64   */.  int errCod
1552d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1552e 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65    /* One of seve
1552f 72 61 6c 20 6b 69 6e 64 73 20 6f 66 20 65 72 72  ral kinds of err
15530 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  ors */.  int nRe
15531 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
15532 20 20 20 20 20 2f 2a 20 50 61 67 65 73 20 6a 6f       /* Pages jo
15533 75 72 6e 61 6c 6c 65 64 20 73 69 6e 63 65 20 6c  urnalled since l
15534 61 73 74 20 6a 2d 68 65 61 64 65 72 20 77 72 69  ast j-header wri
15535 74 74 65 6e 20 2a 2f 0a 20 20 75 33 32 20 63 6b  tten */.  u32 ck
15536 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 20 20 20  sumInit;        
15537 20 20 20 20 20 20 2f 2a 20 51 75 61 73 69 2d 72        /* Quasi-r
15538 61 6e 64 6f 6d 20 76 61 6c 75 65 20 61 64 64 65  andom value adde
15539 64 20 74 6f 20 65 76 65 72 79 20 63 68 65 63 6b  d to every check
1553a 73 75 6d 20 2a 2f 0a 20 20 75 33 32 20 6e 53 75  sum */.  u32 nSu
1553b 62 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20  bRec;           
1553c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1553d 66 20 72 65 63 6f 72 64 73 20 77 72 69 74 74 65  f records writte
1553e 6e 20 74 6f 20 73 75 62 2d 6a 6f 75 72 6e 61 6c  n to sub-journal
1553f 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49   */.  Bitvec *pI
15540 6e 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20  nJournal;       
15541 20 20 2f 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72    /* One bit for
15542 20 65 61 63 68 20 70 61 67 65 20 69 6e 20 74 68   each page in th
15543 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
15544 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  */.  sqlite3_fil
15545 65 20 2a 66 64 3b 20 20 20 20 20 20 20 20 20 20  e *fd;          
15546 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70   /* File descrip
15547 74 6f 72 20 66 6f 72 20 64 61 74 61 62 61 73 65  tor for database
15548 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
15549 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20  le *jfd;        
1554a 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69    /* File descri
1554b 70 74 6f 72 20 66 6f 72 20 6d 61 69 6e 20 6a 6f  ptor for main jo
1554c 75 72 6e 61 6c 20 2a 2f 0a 20 20 73 71 6c 69 74  urnal */.  sqlit
1554d 65 33 5f 66 69 6c 65 20 2a 73 6a 66 64 3b 20 20  e3_file *sjfd;  
1554e 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64         /* File d
1554f 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 73 75  escriptor for su
15550 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69  b-journal */.  i
15551 36 34 20 6a 6f 75 72 6e 61 6c 4f 66 66 3b 20 20  64 journalOff;  
15552 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
15553 72 72 65 6e 74 20 77 72 69 74 65 20 6f 66 66 73  rrent write offs
15554 65 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  et in the journa
15555 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20  l file */.  i64 
15556 6a 6f 75 72 6e 61 6c 48 64 72 3b 20 20 20 20 20  journalHdr;     
15557 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20          /* Byte 
15558 6f 66 66 73 65 74 20 74 6f 20 70 72 65 76 69 6f  offset to previo
15559 75 73 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  us journal heade
1555a 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62  r */.  sqlite3_b
1555b 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70 3b 20  ackup *pBackup; 
1555c 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1555d 20 6c 69 73 74 20 6f 66 20 6f 6e 67 6f 69 6e 67   list of ongoing
1555e 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 65   backup processe
1555f 73 20 2a 2f 0a 20 20 50 61 67 65 72 53 61 76 65  s */.  PagerSave
15560 70 6f 69 6e 74 20 2a 61 53 61 76 65 70 6f 69 6e  point *aSavepoin
15561 74 3b 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 61  t; /* Array of a
15562 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73  ctive savepoints
15563 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70   */.  int nSavep
15564 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  oint;           
15565 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
15566 6c 65 6d 65 6e 74 73 20 69 6e 20 61 53 61 76 65  lements in aSave
15567 70 6f 69 6e 74 5b 5d 20 2a 2f 0a 20 20 63 68 61  point[] */.  cha
15568 72 20 64 62 46 69 6c 65 56 65 72 73 5b 31 36 5d  r dbFileVers[16]
15569 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e  ;        /* Chan
1556a 67 65 73 20 77 68 65 6e 65 76 65 72 20 64 61 74  ges whenever dat
1556b 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67  abase file chang
1556c 65 73 20 2a 2f 0a 20 20 2f 2a 0a 20 20 2a 2a 20  es */.  /*.  ** 
1556d 45 6e 64 20 6f 66 20 74 68 65 20 72 6f 75 74 69  End of the routi
1556e 6e 65 6c 79 2d 63 68 61 6e 67 69 6e 67 20 63 6c  nely-changing cl
1556f 61 73 73 20 6d 65 6d 62 65 72 73 0a 20 20 2a 2a  ass members.  **
15570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15571 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15572 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15573 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15574 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 75 31  *********/..  u1
15575 36 20 6e 45 78 74 72 61 3b 20 20 20 20 20 20 20  6 nExtra;       
15576 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
15577 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73   this many bytes
15578 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f   to each in-memo
15579 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 31 36  ry page */.  i16
1557a 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20   nReserve;      
1557b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1557c 65 72 20 6f 66 20 75 6e 75 73 65 64 20 62 79 74  er of unused byt
1557d 65 73 20 61 74 20 65 6e 64 20 6f 66 20 65 61 63  es at end of eac
1557e 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 33 32 20  h page */.  u32 
1557f 76 66 73 46 6c 61 67 73 3b 20 20 20 20 20 20 20  vfsFlags;       
15580 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73          /* Flags
15581 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 66 73   for sqlite3_vfs
15582 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 75 33  .xOpen() */.  u3
15583 32 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20  2 sectorSize;   
15584 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73            /* Ass
15585 75 6d 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65  umed sector size
15586 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b   during rollback
15587 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65 53 69   */.  int pageSi
15588 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
15589 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1558a 79 74 65 73 20 69 6e 20 61 20 70 61 67 65 20 2a  ytes in a page *
1558b 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67 6e 6f 3b  /.  Pgno mxPgno;
1558c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1558d 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77  /* Maximum allow
1558e 65 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64  ed size of the d
1558f 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 36 34  atabase */.  i64
15590 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69   journalSizeLimi
15591 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  t;       /* Size
15592 20 6c 69 6d 69 74 20 66 6f 72 20 70 65 72 73 69   limit for persi
15593 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69  stent journal fi
15594 6c 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  les */.  char *z
15595 46 69 6c 65 6e 61 6d 65 3b 20 20 20 20 20 20 20  Filename;       
15596 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
15597 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
15598 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4a 6f  e */.  char *zJo
15599 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20  urnal;          
1559a 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
1559b 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a  e journal file *
1559c 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48  /.  int (*xBusyH
1559d 61 6e 64 6c 65 72 29 28 76 6f 69 64 2a 29 3b 20  andler)(void*); 
1559e 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63  /* Function to c
1559f 61 6c 6c 20 77 68 65 6e 20 62 75 73 79 20 2a 2f  all when busy */
155a0 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61  .  void *pBusyHa
155a1 6e 64 6c 65 72 41 72 67 3b 20 20 20 20 20 20 2f  ndlerArg;      /
155a2 2a 20 43 6f 6e 74 65 78 74 20 61 72 67 75 6d 65  * Context argume
155a3 6e 74 20 66 6f 72 20 78 42 75 73 79 48 61 6e 64  nt for xBusyHand
155a4 6c 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 53 74  ler */.  int aSt
155a5 61 74 5b 33 5d 3b 20 20 20 20 20 20 20 20 20 20  at[3];          
155a6 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 63 61       /* Total ca
155a7 63 68 65 20 68 69 74 73 2c 20 6d 69 73 73 65 73  che hits, misses
155a8 20 61 6e 64 20 77 72 69 74 65 73 20 2a 2f 0a 23   and writes */.#
155a9 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
155aa 54 0a 20 20 69 6e 74 20 6e 52 65 61 64 3b 20 20  T.  int nRead;  
155ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
155ac 2f 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 65  /* Database page
155ad 73 20 72 65 61 64 20 2a 2f 0a 23 65 6e 64 69 66  s read */.#endif
155ae 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 69 6e 69  .  void (*xReini
155af 74 65 72 29 28 44 62 50 61 67 65 2a 29 3b 20 2f  ter)(DbPage*); /
155b0 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  * Call this rout
155b1 69 6e 65 20 77 68 65 6e 20 72 65 6c 6f 61 64 69  ine when reloadi
155b2 6e 67 20 70 61 67 65 73 20 2a 2f 0a 23 69 66 64  ng pages */.#ifd
155b3 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f  ef SQLITE_HAS_CO
155b4 44 45 43 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43  DEC.  void *(*xC
155b5 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64  odec)(void*,void
155b6 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 20 2f 2a 20  *,Pgno,int); /* 
155b7 52 6f 75 74 69 6e 65 20 66 6f 72 20 65 6e 2f 64  Routine for en/d
155b8 65 63 6f 64 69 6e 67 20 64 61 74 61 20 2a 2f 0a  ecoding data */.
155b9 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 53    void (*xCodecS
155ba 69 7a 65 43 68 6e 67 29 28 76 6f 69 64 2a 2c 69  izeChng)(void*,i
155bb 6e 74 2c 69 6e 74 29 3b 20 2f 2a 20 4e 6f 74 69  nt,int); /* Noti
155bc 66 79 20 6f 66 20 70 61 67 65 20 73 69 7a 65 20  fy of page size 
155bd 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 76 6f 69  changes */.  voi
155be 64 20 28 2a 78 43 6f 64 65 63 46 72 65 65 29 28  d (*xCodecFree)(
155bf 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 20  void*);         
155c0 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f      /* Destructo
155c1 72 20 66 6f 72 20 74 68 65 20 63 6f 64 65 63 20  r for the codec 
155c2 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f 64 65  */.  void *pCode
155c3 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
155c4 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65   /* First argume
155c5 6e 74 20 74 6f 20 78 43 6f 64 65 63 2e 2e 2e 20  nt to xCodec... 
155c6 6d 65 74 68 6f 64 73 20 2a 2f 0a 23 65 6e 64 69  methods */.#endi
155c7 66 0a 20 20 63 68 61 72 20 2a 70 54 6d 70 53 70  f.  char *pTmpSp
155c8 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ace;            
155c9 2f 2a 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a  /* Pager.pageSiz
155ca 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65  e bytes of space
155cb 20 66 6f 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a   for tmp use */.
155cc 20 20 50 43 61 63 68 65 20 2a 70 50 43 61 63 68    PCache *pPCach
155cd 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
155ce 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65   Pointer to page
155cf 20 63 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f   cache object */
155d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
155d1 4f 4d 49 54 5f 57 41 4c 0a 20 20 57 61 6c 20 2a  OMIT_WAL.  Wal *
155d2 70 57 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20  pWal;           
155d3 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 2d         /* Write-
155d4 61 68 65 61 64 20 6c 6f 67 20 75 73 65 64 20 62  ahead log used b
155d5 79 20 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d  y "journal_mode=
155d6 77 61 6c 22 20 2a 2f 0a 20 20 63 68 61 72 20 2a  wal" */.  char *
155d7 7a 57 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20  zWal;           
155d8 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 6e 61        /* File na
155d9 6d 65 20 66 6f 72 20 77 72 69 74 65 2d 61 68 65  me for write-ahe
155da 61 64 20 6c 6f 67 20 2a 2f 0a 23 65 6e 64 69 66  ad log */.#endif
155db 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 65 78  .};../*.** Index
155dc 65 73 20 66 6f 72 20 75 73 65 20 77 69 74 68 20  es for use with 
155dd 50 61 67 65 72 2e 61 53 74 61 74 5b 5d 2e 20 54  Pager.aStat[]. T
155de 68 65 20 50 61 67 65 72 2e 61 53 74 61 74 5b 5d  he Pager.aStat[]
155df 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 0a   array contains.
155e0 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 61 63  ** the values ac
155e1 63 65 73 73 65 64 20 62 79 20 70 61 73 73 69 6e  cessed by passin
155e2 67 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55  g SQLITE_DBSTATU
155e3 53 5f 43 41 43 48 45 5f 48 49 54 2c 20 43 41 43  S_CACHE_HIT, CAC
155e4 48 45 5f 4d 49 53 53 20 0a 2a 2a 20 6f 72 20 43  HE_MISS .** or C
155e5 41 43 48 45 5f 57 52 49 54 45 20 74 6f 20 73 71  ACHE_WRITE to sq
155e6 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28  lite3_db_status(
155e7 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  )..*/.#define PA
155e8 47 45 52 5f 53 54 41 54 5f 48 49 54 20 20 20 30  GER_STAT_HIT   0
155e9 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53  .#define PAGER_S
155ea 54 41 54 5f 4d 49 53 53 20 20 31 0a 23 64 65 66  TAT_MISS  1.#def
155eb 69 6e 65 20 50 41 47 45 52 5f 53 54 41 54 5f 57  ine PAGER_STAT_W
155ec 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 54 68  RITE 2../*.** Th
155ed 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62  e following glob
155ee 61 6c 20 76 61 72 69 61 62 6c 65 73 20 68 6f 6c  al variables hol
155ef 64 20 63 6f 75 6e 74 65 72 73 20 75 73 65 64 20  d counters used 
155f0 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 70  for.** testing p
155f1 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 54  urposes only.  T
155f2 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 64  hese variables d
155f3 6f 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 0a 2a  o not exist in.*
155f4 2a 20 61 20 6e 6f 6e 2d 74 65 73 74 69 6e 67 20  * a non-testing 
155f5 62 75 69 6c 64 2e 20 20 54 68 65 73 65 20 76 61  build.  These va
155f6 72 69 61 62 6c 65 73 20 61 72 65 20 6e 6f 74 20  riables are not 
155f7 74 68 72 65 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a  thread-safe..*/.
155f8 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
155f9 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
155fa 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f  t sqlite3_pager_
155fb 72 65 61 64 64 62 5f 63 6f 75 6e 74 20 3d 20 30  readdb_count = 0
155fc 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  ;    /* Number o
155fd 66 20 66 75 6c 6c 20 70 61 67 65 73 20 72 65 61  f full pages rea
155fe 64 20 66 72 6f 6d 20 44 42 20 2a 2f 0a 53 51 4c  d from DB */.SQL
155ff 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
15600 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64  te3_pager_writed
15601 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 2f  b_count = 0;   /
15602 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c  * Number of full
15603 20 70 61 67 65 73 20 77 72 69 74 74 65 6e 20 74   pages written t
15604 6f 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  o DB */.SQLITE_A
15605 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  PI int sqlite3_p
15606 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e  ager_writej_coun
15607 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d  t = 0;    /* Num
15608 62 65 72 20 6f 66 20 70 61 67 65 73 20 77 72 69  ber of pages wri
15609 74 74 65 6e 20 74 6f 20 6a 6f 75 72 6e 61 6c 20  tten to journal 
1560a 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45  */.# define PAGE
1560b 52 5f 49 4e 43 52 28 76 29 20 20 76 2b 2b 0a 23  R_INCR(v)  v++.#
1560c 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 50 41  else.# define PA
1560d 47 45 52 5f 49 4e 43 52 28 76 29 0a 23 65 6e 64  GER_INCR(v).#end
1560e 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72  if..../*.** Jour
1560f 6e 61 6c 20 66 69 6c 65 73 20 62 65 67 69 6e 20  nal files begin 
15610 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69  with the followi
15611 6e 67 20 6d 61 67 69 63 20 73 74 72 69 6e 67 2e  ng magic string.
15612 20 20 54 68 65 20 64 61 74 61 0a 2a 2a 20 77 61    The data.** wa
15613 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
15614 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e 20 20 49 74  /dev/random.  It
15615 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 61 73   is used only as
15616 20 61 20 73 61 6e 69 74 79 20 63 68 65 63 6b 2e   a sanity check.
15617 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72  .**.** Since ver
15618 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 74 68 65 20  sion 2.8.0, the 
15619 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63  journal format c
1561a 6f 6e 74 61 69 6e 73 20 61 64 64 69 74 69 6f 6e  ontains addition
1561b 61 6c 20 73 61 6e 69 74 79 0a 2a 2a 20 63 68 65  al sanity.** che
1561c 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f  cking informatio
1561d 6e 2e 20 20 49 66 20 74 68 65 20 70 6f 77 65 72  n.  If the power
1561e 20 66 61 69 6c 73 20 77 68 69 6c 65 20 74 68 65   fails while the
1561f 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e   journal is bein
15620 67 0a 2a 2a 20 77 72 69 74 74 65 6e 2c 20 73 65  g.** written, se
15621 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 72 62 61 67  mi-random garbag
15622 65 20 64 61 74 61 20 6d 69 67 68 74 20 61 70 70  e data might app
15623 65 61 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  ear in the journ
15624 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 66 74 65 72  al.** file after
15625 20 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f 72   power is restor
15626 65 64 2e 20 20 49 66 20 61 6e 20 61 74 74 65 6d  ed.  If an attem
15627 70 74 20 69 73 20 74 68 65 6e 20 6d 61 64 65 0a  pt is then made.
15628 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 68 65 20 6a  ** to roll the j
15629 6f 75 72 6e 61 6c 20 62 61 63 6b 2c 20 74 68 65  ournal back, the
1562a 20 64 61 74 61 62 61 73 65 20 63 6f 75 6c 64 20   database could 
1562b 62 65 20 63 6f 72 72 75 70 74 65 64 2e 20 20 54  be corrupted.  T
1562c 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a  he additional.**
1562d 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   sanity checking
1562e 20 64 61 74 61 20 69 73 20 61 6e 20 61 74 74 65   data is an atte
1562f 6d 70 74 20 74 6f 20 64 69 73 63 6f 76 65 72 20  mpt to discover 
15630 74 68 65 20 67 61 72 62 61 67 65 20 69 6e 20 74  the garbage in t
15631 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e  he.** journal an
15632 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 2a 2a 0a  d ignore it..**.
15633 2a 2a 20 54 68 65 20 73 61 6e 69 74 79 20 63 68  ** The sanity ch
15634 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  ecking informati
15635 6f 6e 20 66 6f 72 20 74 68 65 20 6e 65 77 20 6a  on for the new j
15636 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f  ournal format co
15637 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 20 61 20 33  nsists.** of a 3
15638 32 2d 62 69 74 20 63 68 65 63 6b 73 75 6d 20 6f  2-bit checksum o
15639 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 64  n each page of d
1563a 61 74 61 2e 20 20 54 68 65 20 63 68 65 63 6b 73  ata.  The checks
1563b 75 6d 20 63 6f 76 65 72 73 20 62 6f 74 68 0a 2a  um covers both.*
1563c 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  * the page numbe
1563d 72 20 61 6e 64 20 74 68 65 20 70 50 61 67 65 72  r and the pPager
1563e 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  ->pageSize bytes
1563f 20 6f 66 20 64 61 74 61 20 66 6f 72 20 74 68 65   of data for the
15640 20 70 61 67 65 2e 0a 2a 2a 20 54 68 69 73 20 63   page..** This c
15641 6b 73 75 6d 20 69 73 20 69 6e 69 74 69 61 6c 69  ksum is initiali
15642 7a 65 64 20 74 6f 20 61 20 33 32 2d 62 69 74 20  zed to a 32-bit 
15643 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 74 68 61  random value tha
15644 74 20 61 70 70 65 61 72 73 20 69 6e 20 74 68 65  t appears in the
15645 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  .** journal file
15646 20 72 69 67 68 74 20 61 66 74 65 72 20 74 68 65   right after the
15647 20 68 65 61 64 65 72 2e 20 20 54 68 65 20 72 61   header.  The ra
15648 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 69 7a 65 72  ndom initializer
15649 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2c 0a 2a   is important,.*
1564a 2a 20 62 65 63 61 75 73 65 20 67 61 72 62 61 67  * because garbag
1564b 65 20 64 61 74 61 20 74 68 61 74 20 61 70 70 65  e data that appe
1564c 61 72 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f  ars at the end o
1564d 66 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c  f a journal is l
1564e 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 61 20 74 68  ikely.** data th
1564f 61 74 20 77 61 73 20 6f 6e 63 65 20 69 6e 20 6f  at was once in o
15650 74 68 65 72 20 66 69 6c 65 73 20 74 68 61 74 20  ther files that 
15651 68 61 76 65 20 6e 6f 77 20 62 65 65 6e 20 64 65  have now been de
15652 6c 65 74 65 64 2e 20 20 49 66 20 74 68 65 0a 2a  leted.  If the.*
15653 2a 20 67 61 72 62 61 67 65 20 64 61 74 61 20 63  * garbage data c
15654 61 6d 65 20 66 72 6f 6d 20 61 6e 20 6f 62 73 6f  ame from an obso
15655 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  lete journal fil
15656 65 2c 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73  e, the checksums
15657 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 63 6f 72   might.** be cor
15658 72 65 63 74 2e 20 20 42 75 74 20 62 79 20 69 6e  rect.  But by in
15659 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 63  itializing the c
1565a 68 65 63 6b 73 75 6d 20 74 6f 20 72 61 6e 64 6f  hecksum to rando
1565b 6d 20 76 61 6c 75 65 20 77 68 69 63 68 0a 2a 2a  m value which.**
1565c 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 6f   is different fo
1565d 72 20 65 76 65 72 79 20 6a 6f 75 72 6e 61 6c 2c  r every journal,
1565e 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20 74 68 61   we minimize tha
1565f 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69  t risk..*/.stati
15660 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
15661 20 63 68 61 72 20 61 4a 6f 75 72 6e 61 6c 4d 61   char aJournalMa
15662 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 30 78 64 39  gic[] = {.  0xd9
15663 2c 20 30 78 64 35 2c 20 30 78 30 35 2c 20 30 78  , 0xd5, 0x05, 0x
15664 66 39 2c 20 30 78 32 30 2c 20 30 78 61 31 2c 20  f9, 0x20, 0xa1, 
15665 30 78 36 33 2c 20 30 78 64 37 2c 0a 7d 3b 0a 0a  0x63, 0xd7,.};..
15666 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f  /*.** The size o
15667 66 20 74 68 65 20 6f 66 20 65 61 63 68 20 70 61  f the of each pa
15668 67 65 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65  ge record in the
15669 20 6a 6f 75 72 6e 61 6c 20 69 73 20 67 69 76 65   journal is give
1566a 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c  n by.** the foll
1566b 6f 77 69 6e 67 20 6d 61 63 72 6f 2e 0a 2a 2f 0a  owing macro..*/.
1566c 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f  #define JOURNAL_
1566d 50 47 5f 53 5a 28 70 50 61 67 65 72 29 20 20 28  PG_SZ(pPager)  (
1566e 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  (pPager->pageSiz
1566f 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a 20 54  e) + 8)../*.** T
15670 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  he journal heade
15671 72 20 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20  r size for this 
15672 70 61 67 65 72 2e 20 54 68 69 73 20 69 73 20 75  pager. This is u
15673 73 75 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 20  sually the same 
15674 0a 2a 2a 20 73 69 7a 65 20 61 73 20 61 20 73 69  .** size as a si
15675 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72  ngle disk sector
15676 2e 20 53 65 65 20 61 6c 73 6f 20 73 65 74 53 65  . See also setSe
15677 63 74 6f 72 53 69 7a 65 28 29 2e 0a 2a 2f 0a 23  ctorSize()..*/.#
15678 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 48  define JOURNAL_H
15679 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 28 70  DR_SZ(pPager) (p
1567a 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a  Pager->sectorSiz
1567b 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  e)../*.** The ma
1567c 63 72 6f 20 4d 45 4d 44 42 20 69 73 20 74 72 75  cro MEMDB is tru
1567d 65 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c  e if we are deal
1567e 69 6e 67 20 77 69 74 68 20 61 6e 20 69 6e 2d 6d  ing with an in-m
1567f 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e 0a  emory database..
15680 2a 2a 20 57 65 20 64 6f 20 74 68 69 73 20 61 73  ** We do this as
15681 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74   a macro so that
15682 20 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4f   if the SQLITE_O
15683 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 6d 61 63  MIT_MEMORYDB mac
15684 72 6f 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68  ro is set,.** th
15685 65 20 76 61 6c 75 65 20 6f 66 20 4d 45 4d 44 42  e value of MEMDB
15686 20 77 69 6c 6c 20 62 65 20 61 20 63 6f 6e 73 74   will be a const
15687 61 6e 74 20 61 6e 64 20 74 68 65 20 63 6f 6d 70  ant and the comp
15688 69 6c 65 72 20 77 69 6c 6c 20 6f 70 74 69 6d 69  iler will optimi
15689 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f 64 65 20 74  ze.** out code t
1568a 68 61 74 20 77 6f 75 6c 64 20 6e 65 76 65 72 20  hat would never 
1568b 65 78 65 63 75 74 65 2e 0a 2a 2f 0a 23 69 66 64  execute..*/.#ifd
1568c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d  ef SQLITE_OMIT_M
1568d 45 4d 4f 52 59 44 42 0a 23 20 64 65 66 69 6e 65  EMORYDB.# define
1568e 20 4d 45 4d 44 42 20 30 0a 23 65 6c 73 65 0a 23   MEMDB 0.#else.#
1568f 20 64 65 66 69 6e 65 20 4d 45 4d 44 42 20 70 50   define MEMDB pP
15690 61 67 65 72 2d 3e 6d 65 6d 44 62 0a 23 65 6e 64  ager->memDb.#end
15691 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  if../*.** The ma
15692 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61 67 65  ximum legal page
15693 20 6e 75 6d 62 65 72 20 69 73 20 28 32 5e 33 31   number is (2^31
15694 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e   - 1)..*/.#defin
15695 65 20 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f  e PAGER_MAX_PGNO
15696 20 32 31 34 37 34 38 33 36 34 37 0a 0a 2f 2a 0a   2147483647../*.
15697 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20  ** The argument 
15698 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73  to this macro is
15699 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74   a file descript
1569a 6f 72 20 28 74 79 70 65 20 73 71 6c 69 74 65 33  or (type sqlite3
1569b 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20 52 65 74 75  _file*)..** Retu
1569c 72 6e 20 30 20 69 66 20 69 74 20 69 73 20 6e 6f  rn 0 if it is no
1569d 74 20 6f 70 65 6e 2c 20 6f 72 20 6e 6f 6e 2d 7a  t open, or non-z
1569e 65 72 6f 20 28 62 75 74 20 6e 6f 74 20 31 29 20  ero (but not 1) 
1569f 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20  if it is..**.** 
156a0 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 20  This is so that 
156a1 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 6e 20  expressions can 
156a2 62 65 20 77 72 69 74 74 65 6e 20 61 73 3a 0a 2a  be written as:.*
156a3 2a 0a 2a 2a 20 20 20 69 66 28 20 69 73 4f 70 65  *.**   if( isOpe
156a4 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29  n(pPager->jfd) )
156a5 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69 6e 73 74  { ....**.** inst
156a6 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 69  ead of.**.**   i
156a7 66 28 20 70 50 61 67 65 72 2d 3e 6a 66 64 2d 3e  f( pPager->jfd->
156a8 70 4d 65 74 68 6f 64 73 20 29 7b 20 2e 2e 2e 0a  pMethods ){ ....
156a9 2a 2f 0a 23 64 65 66 69 6e 65 20 69 73 4f 70 65  */.#define isOpe
156aa 6e 28 70 46 64 29 20 28 28 70 46 64 29 2d 3e 70  n(pFd) ((pFd)->p
156ab 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a 0a 2a 2a 20  Methods)../*.** 
156ac 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
156ad 68 69 73 20 70 61 67 65 72 20 75 73 65 73 20 61  his pager uses a
156ae 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67   write-ahead log
156af 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20   instead of the 
156b0 75 73 75 61 6c 0a 2a 2a 20 72 6f 6c 6c 62 61 63  usual.** rollbac
156b1 6b 20 6a 6f 75 72 6e 61 6c 2e 20 4f 74 68 65 72  k journal. Other
156b2 77 69 73 65 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23  wise false..*/.#
156b3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
156b4 49 54 5f 57 41 4c 0a 73 74 61 74 69 63 20 69 6e  IT_WAL.static in
156b5 74 20 70 61 67 65 72 55 73 65 57 61 6c 28 50 61  t pagerUseWal(Pa
156b6 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
156b7 72 65 74 75 72 6e 20 28 70 50 61 67 65 72 2d 3e  return (pPager->
156b8 70 57 61 6c 21 3d 30 29 3b 0a 7d 0a 23 65 6c 73  pWal!=0);.}.#els
156b9 65 0a 23 20 64 65 66 69 6e 65 20 70 61 67 65 72  e.# define pager
156ba 55 73 65 57 61 6c 28 78 29 20 30 0a 23 20 64 65  UseWal(x) 0.# de
156bb 66 69 6e 65 20 70 61 67 65 72 52 6f 6c 6c 62 61  fine pagerRollba
156bc 63 6b 57 61 6c 28 78 29 20 30 0a 23 20 64 65 66  ckWal(x) 0.# def
156bd 69 6e 65 20 70 61 67 65 72 57 61 6c 46 72 61 6d  ine pagerWalFram
156be 65 73 28 76 2c 77 2c 78 2c 79 29 20 30 0a 23 20  es(v,w,x,y) 0.# 
156bf 64 65 66 69 6e 65 20 70 61 67 65 72 4f 70 65 6e  define pagerOpen
156c0 57 61 6c 49 66 50 72 65 73 65 6e 74 28 7a 29 20  WalIfPresent(z) 
156c1 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64 65 66 69  SQLITE_OK.# defi
156c2 6e 65 20 70 61 67 65 72 42 65 67 69 6e 52 65 61  ne pagerBeginRea
156c3 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 7a 29 20  dTransaction(z) 
156c4 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66  SQLITE_OK.#endif
156c5 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
156c6 20 0a 2f 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a   ./*.** Usage:.*
156c7 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 61  *.**   assert( a
156c8 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74  ssert_pager_stat
156c9 65 28 70 50 61 67 65 72 29 20 29 3b 0a 2a 2a 0a  e(pPager) );.**.
156ca 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
156cb 20 72 75 6e 73 20 6d 61 6e 79 20 61 73 73 65 72   runs many asser
156cc 74 73 20 74 6f 20 74 72 79 20 74 6f 20 66 69 6e  ts to try to fin
156cd 64 20 69 6e 63 6f 6e 73 69 73 74 65 6e 63 69 65  d inconsistencie
156ce 73 20 69 6e 0a 2a 2a 20 74 68 65 20 69 6e 74 65  s in.** the inte
156cf 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 74 68  rnal state of th
156d0 65 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 0a  e Pager object..
156d1 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 73  */.static int as
156d2 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65  sert_pager_state
156d3 28 50 61 67 65 72 20 2a 70 29 7b 0a 20 20 50 61  (Pager *p){.  Pa
156d4 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 3b  ger *pPager = p;
156d5 0a 0a 20 20 2f 2a 20 53 74 61 74 65 20 6d 75 73  ..  /* State mus
156d6 74 20 62 65 20 76 61 6c 69 64 2e 20 2a 2f 0a 20  t be valid. */. 
156d7 20 61 73 73 65 72 74 28 20 70 2d 3e 65 53 74 61   assert( p->eSta
156d8 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 0a 20  te==PAGER_OPEN. 
156d9 20 20 20 20 20 20 7c 7c 20 70 2d 3e 65 53 74 61        || p->eSta
156da 74 65 3d 3d 50 41 47 45 52 5f 52 45 41 44 45 52  te==PAGER_READER
156db 0a 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 65 53  .       || p->eS
156dc 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54  tate==PAGER_WRIT
156dd 45 52 5f 4c 4f 43 4b 45 44 0a 20 20 20 20 20 20  ER_LOCKED.      
156de 20 7c 7c 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50   || p->eState==P
156df 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48  AGER_WRITER_CACH
156e0 45 4d 4f 44 0a 20 20 20 20 20 20 20 7c 7c 20 70  EMOD.       || p
156e1 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f  ->eState==PAGER_
156e2 57 52 49 54 45 52 5f 44 42 4d 4f 44 0a 20 20 20  WRITER_DBMOD.   
156e3 20 20 20 20 7c 7c 20 70 2d 3e 65 53 74 61 74 65      || p->eState
156e4 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 46  ==PAGER_WRITER_F
156e5 49 4e 49 53 48 45 44 0a 20 20 20 20 20 20 20 7c  INISHED.       |
156e6 7c 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47  | p->eState==PAG
156e7 45 52 5f 45 52 52 4f 52 0a 20 20 29 3b 0a 0a 20  ER_ERROR.  );.. 
156e8 20 2f 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f   /* Regardless o
156e9 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74  f the current st
156ea 61 74 65 2c 20 61 20 74 65 6d 70 2d 66 69 6c 65  ate, a temp-file
156eb 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6c 77 61   connection alwa
156ec 79 73 20 62 65 68 61 76 65 73 0a 20 20 2a 2a 20  ys behaves.  ** 
156ed 61 73 20 69 66 20 69 74 20 68 61 73 20 61 6e 20  as if it has an 
156ee 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f  exclusive lock o
156ef 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
156f0 69 6c 65 2e 20 49 74 20 6e 65 76 65 72 20 75 70  ile. It never up
156f1 64 61 74 65 73 0a 20 20 2a 2a 20 74 68 65 20 63  dates.  ** the c
156f2 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 66 69  hange-counter fi
156f3 65 6c 64 2c 20 73 6f 20 74 68 65 20 63 68 61 6e  eld, so the chan
156f4 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67  geCountDone flag
156f5 20 69 73 20 61 6c 77 61 79 73 20 73 65 74 2e 0a   is always set..
156f6 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
156f7 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c  ->tempFile==0 ||
156f8 20 70 2d 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55   p->eLock==EXCLU
156f9 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  SIVE_LOCK );.  a
156fa 73 73 65 72 74 28 20 70 2d 3e 74 65 6d 70 46 69  ssert( p->tempFi
156fb 6c 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d  le==0 || pPager-
156fc 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65  >changeCountDone
156fd 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   );..  /* If the
156fe 20 75 73 65 4a 6f 75 72 6e 61 6c 20 66 6c 61 67   useJournal flag
156ff 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65 20 6a   is clear, the j
15700 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 6d 75 73 74  ournal-mode must
15701 20 62 65 20 22 4f 46 46 22 2e 20 0a 20 20 2a 2a   be "OFF". .  **
15702 20 41 6e 64 20 69 66 20 74 68 65 20 6a 6f 75 72   And if the jour
15703 6e 61 6c 2d 6d 6f 64 65 20 69 73 20 22 4f 46 46  nal-mode is "OFF
15704 22 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ", the journal f
15705 69 6c 65 20 6d 75 73 74 20 6e 6f 74 20 62 65 20  ile must not be 
15706 6f 70 65 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  open..  */.  ass
15707 65 72 74 28 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d  ert( p->journalM
15708 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
15709 41 4c 4d 4f 44 45 5f 4f 46 46 20 7c 7c 20 70 2d  ALMODE_OFF || p-
1570a 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20  >useJournal );. 
1570b 20 61 73 73 65 72 74 28 20 70 2d 3e 6a 6f 75 72   assert( p->jour
1570c 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a  nalMode!=PAGER_J
1570d 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 7c  OURNALMODE_OFF |
1570e 7c 20 21 69 73 4f 70 65 6e 28 70 2d 3e 6a 66 64  | !isOpen(p->jfd
1570f 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  ) );..  /* Check
15710 20 74 68 61 74 20 4d 45 4d 44 42 20 69 6d 70 6c   that MEMDB impl
15711 69 65 73 20 6e 6f 53 79 6e 63 2e 20 41 6e 64 20  ies noSync. And 
15712 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75  an in-memory jou
15713 72 6e 61 6c 2e 20 53 69 6e 63 65 20 0a 20 20 2a  rnal. Since .  *
15714 2a 20 74 68 69 73 20 6d 65 61 6e 73 20 61 6e 20  * this means an 
15715 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72 20  in-memory pager 
15716 70 65 72 66 6f 72 6d 73 20 6e 6f 20 49 4f 20 61  performs no IO a
15717 74 20 61 6c 6c 2c 20 69 74 20 63 61 6e 6e 6f 74  t all, it cannot
15718 20 65 6e 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2a   encounter .  **
15719 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 49   either SQLITE_I
1571a 4f 45 52 52 20 6f 72 20 53 51 4c 49 54 45 5f 46  OERR or SQLITE_F
1571b 55 4c 4c 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62  ULL during rollb
1571c 61 63 6b 20 6f 72 20 77 68 69 6c 65 20 66 69 6e  ack or while fin
1571d 61 6c 69 7a 69 6e 67 20 0a 20 20 2a 2a 20 61 20  alizing .  ** a 
1571e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 28 61  journal file. (a
1571f 6c 74 68 6f 75 67 68 20 74 68 65 20 69 6e 2d 6d  lthough the in-m
15720 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 69 6d  emory journal im
15721 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 61 79  plementation may
15722 20 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51   .  ** return SQ
15723 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d  LITE_IOERR_NOMEM
15724 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e   while the journ
15725 61 6c 20 66 69 6c 65 20 69 73 20 62 65 69 6e 67  al file is being
15726 20 77 72 69 74 74 65 6e 29 2e 20 49 74 20 0a 20   written). It . 
15727 20 2a 2a 20 69 73 20 74 68 65 72 65 66 6f 72 65   ** is therefore
15728 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f   not possible fo
15729 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70  r an in-memory p
1572a 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68  ager to enter th
1572b 65 20 45 52 52 4f 52 20 0a 20 20 2a 2a 20 73 74  e ERROR .  ** st
1572c 61 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ate..  */.  if( 
1572d 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 61 73 73  MEMDB ){.    ass
1572e 65 72 74 28 20 70 2d 3e 6e 6f 53 79 6e 63 20 29  ert( p->noSync )
1572f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ;.    assert( p-
15730 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
15731 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
15732 4f 46 46 20 0a 20 20 20 20 20 20 20 20 20 7c 7c  OFF .         ||
15733 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d   p->journalMode=
15734 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
15735 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 29  DE_MEMORY .    )
15736 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ;.    assert( p-
15737 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 5f 45  >eState!=PAGER_E
15738 52 52 4f 52 20 26 26 20 70 2d 3e 65 53 74 61 74  RROR && p->eStat
15739 65 21 3d 50 41 47 45 52 5f 4f 50 45 4e 20 29 3b  e!=PAGER_OPEN );
1573a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67  .    assert( pag
1573b 65 72 55 73 65 57 61 6c 28 70 29 3d 3d 30 20 29  erUseWal(p)==0 )
1573c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 63  ;.  }..  /* If c
1573d 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 69  hangeCountDone i
1573e 73 20 73 65 74 2c 20 61 20 52 45 53 45 52 56 45  s set, a RESERVE
1573f 44 20 6c 6f 63 6b 20 6f 72 20 67 72 65 61 74 65  D lock or greate
15740 72 20 6d 75 73 74 20 62 65 20 68 65 6c 64 0a 20  r must be held. 
15741 20 2a 2a 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e   ** on the file.
15742 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
15743 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
15744 75 6e 74 44 6f 6e 65 3d 3d 30 20 7c 7c 20 70 50  untDone==0 || pP
15745 61 67 65 72 2d 3e 65 4c 6f 63 6b 3e 3d 52 45 53  ager->eLock>=RES
15746 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  ERVED_LOCK );.  
15747 61 73 73 65 72 74 28 20 70 2d 3e 65 4c 6f 63 6b  assert( p->eLock
15748 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29  !=PENDING_LOCK )
15749 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e  ;..  switch( p->
1574a 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 63 61  eState ){.    ca
1574b 73 65 20 50 41 47 45 52 5f 4f 50 45 4e 3a 0a 20  se PAGER_OPEN:. 
1574c 20 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45       assert( !ME
1574d 4d 44 42 20 29 3b 0a 20 20 20 20 20 20 61 73 73  MDB );.      ass
1574e 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 72 72  ert( pPager->err
1574f 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  Code==SQLITE_OK 
15750 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
15751 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
15752 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70  fCount(pPager->p
15753 50 43 61 63 68 65 29 3d 3d 30 20 7c 7c 20 70 50  PCache)==0 || pP
15754 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29  ager->tempFile )
15755 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a  ;.      break;..
15756 20 20 20 20 63 61 73 65 20 50 41 47 45 52 5f 52      case PAGER_R
15757 45 41 44 45 52 3a 0a 20 20 20 20 20 20 61 73 73  EADER:.      ass
15758 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 72 72  ert( pPager->err
15759 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  Code==SQLITE_OK 
1575a 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1575b 20 70 2d 3e 65 4c 6f 63 6b 21 3d 55 4e 4b 4e 4f   p->eLock!=UNKNO
1575c 57 4e 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20  WN_LOCK );.     
1575d 20 61 73 73 65 72 74 28 20 70 2d 3e 65 4c 6f 63   assert( p->eLoc
1575e 6b 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  k>=SHARED_LOCK )
1575f 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a  ;.      break;..
15760 20 20 20 20 63 61 73 65 20 50 41 47 45 52 5f 57      case PAGER_W
15761 52 49 54 45 52 5f 4c 4f 43 4b 45 44 3a 0a 20 20  RITER_LOCKED:.  
15762 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65      assert( p->e
15763 4c 6f 63 6b 21 3d 55 4e 4b 4e 4f 57 4e 5f 4c 4f  Lock!=UNKNOWN_LO
15764 43 4b 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  CK );.      asse
15765 72 74 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43  rt( pPager->errC
15766 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  ode==SQLITE_OK )
15767 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 61 67  ;.      if( !pag
15768 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29  erUseWal(pPager)
15769 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
1576a 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 3e 3d 52 45  rt( p->eLock>=RE
1576b 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SERVED_LOCK );. 
1576c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
1576d 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53  ert( pPager->dbS
1576e 69 7a 65 3d 3d 70 50 61 67 65 72 2d 3e 64 62 4f  ize==pPager->dbO
1576f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 20 20  rigSize );.     
15770 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
15771 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d 70 50 61  >dbOrigSize==pPa
15772 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20  ger->dbFileSize 
15773 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
15774 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53   pPager->dbOrigS
15775 69 7a 65 3d 3d 70 50 61 67 65 72 2d 3e 64 62 48  ize==pPager->dbH
15776 69 6e 74 53 69 7a 65 20 29 3b 0a 20 20 20 20 20  intSize );.     
15777 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
15778 3e 73 65 74 4d 61 73 74 65 72 3d 3d 30 20 29 3b  >setMaster==0 );
15779 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20  .      break;.. 
1577a 20 20 20 63 61 73 65 20 50 41 47 45 52 5f 57 52     case PAGER_WR
1577b 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 3a 0a 20  ITER_CACHEMOD:. 
1577c 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
1577d 65 4c 6f 63 6b 21 3d 55 4e 4b 4e 4f 57 4e 5f 4c  eLock!=UNKNOWN_L
1577e 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 61 73 73  OCK );.      ass
1577f 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 72 72  ert( pPager->err
15780 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  Code==SQLITE_OK 
15781 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 61  );.      if( !pa
15782 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72  gerUseWal(pPager
15783 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
15784 49 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  It is possible t
15785 68 61 74 20 69 66 20 6a 6f 75 72 6e 61 6c 5f 6d  hat if journal_m
15786 6f 64 65 3d 77 61 6c 20 68 65 72 65 20 74 68 61  ode=wal here tha
15787 74 20 6e 65 69 74 68 65 72 20 74 68 65 0a 20 20  t neither the.  
15788 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c        ** journal
15789 20 66 69 6c 65 20 6e 6f 72 20 74 68 65 20 57 41   file nor the WA
1578a 4c 20 66 69 6c 65 20 61 72 65 20 6f 70 65 6e 2e  L file are open.
1578b 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 64 75   This happens du
1578c 72 69 6e 67 0a 20 20 20 20 20 20 20 20 2a 2a 20  ring.        ** 
1578d 61 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73  a rollback trans
1578e 61 63 74 69 6f 6e 20 74 68 61 74 20 73 77 69 74  action that swit
1578f 63 68 65 73 20 66 72 6f 6d 20 6a 6f 75 72 6e 61  ches from journa
15790 6c 5f 6d 6f 64 65 3d 6f 66 66 0a 20 20 20 20 20  l_mode=off.     
15791 20 20 20 2a 2a 20 74 6f 20 6a 6f 75 72 6e 61 6c     ** to journal
15792 5f 6d 6f 64 65 3d 77 61 6c 2e 0a 20 20 20 20 20  _mode=wal..     
15793 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73     */.        as
15794 73 65 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 3e 3d  sert( p->eLock>=
15795 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b  RESERVED_LOCK );
15796 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
15797 20 69 73 4f 70 65 6e 28 70 2d 3e 6a 66 64 29 20   isOpen(p->jfd) 
15798 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  .             ||
15799 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d   p->journalMode=
1579a 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
1579b 44 45 5f 4f 46 46 20 0a 20 20 20 20 20 20 20 20  DE_OFF .        
1579c 20 20 20 20 20 7c 7c 20 70 2d 3e 6a 6f 75 72 6e       || p->journ
1579d 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
1579e 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 0a 20  URNALMODE_WAL . 
1579f 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
157a0 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  }.      assert( 
157a1 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69  pPager->dbOrigSi
157a2 7a 65 3d 3d 70 50 61 67 65 72 2d 3e 64 62 46 69  ze==pPager->dbFi
157a3 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20  leSize );.      
157a4 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
157a5 64 62 4f 72 69 67 53 69 7a 65 3d 3d 70 50 61 67  dbOrigSize==pPag
157a6 65 72 2d 3e 64 62 48 69 6e 74 53 69 7a 65 20 29  er->dbHintSize )
157a7 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a  ;.      break;..
157a8 20 20 20 20 63 61 73 65 20 50 41 47 45 52 5f 57      case PAGER_W
157a9 52 49 54 45 52 5f 44 42 4d 4f 44 3a 0a 20 20 20  RITER_DBMOD:.   
157aa 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 4c     assert( p->eL
157ab 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ock==EXCLUSIVE_L
157ac 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 61 73 73  OCK );.      ass
157ad 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 72 72  ert( pPager->err
157ae 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  Code==SQLITE_OK 
157af 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
157b0 20 21 70 61 67 65 72 55 73 65 57 61 6c 28 70 50   !pagerUseWal(pP
157b1 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 61  ager) );.      a
157b2 73 73 65 72 74 28 20 70 2d 3e 65 4c 6f 63 6b 3e  ssert( p->eLock>
157b3 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
157b4 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
157b5 20 69 73 4f 70 65 6e 28 70 2d 3e 6a 66 64 29 20   isOpen(p->jfd) 
157b6 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70  .           || p
157b7 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
157b8 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
157b9 5f 4f 46 46 20 0a 20 20 20 20 20 20 20 20 20 20  _OFF .          
157ba 20 7c 7c 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f   || p->journalMo
157bb 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
157bc 4c 4d 4f 44 45 5f 57 41 4c 20 0a 20 20 20 20 20  LMODE_WAL .     
157bd 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
157be 28 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67  ( pPager->dbOrig
157bf 53 69 7a 65 3c 3d 70 50 61 67 65 72 2d 3e 64 62  Size<=pPager->db
157c0 48 69 6e 74 53 69 7a 65 20 29 3b 0a 20 20 20 20  HintSize );.    
157c1 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 63 61    break;..    ca
157c2 73 65 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f  se PAGER_WRITER_
157c3 46 49 4e 49 53 48 45 44 3a 0a 20 20 20 20 20 20  FINISHED:.      
157c4 61 73 73 65 72 74 28 20 70 2d 3e 65 4c 6f 63 6b  assert( p->eLock
157c5 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
157c6 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
157c7 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
157c8 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  e==SQLITE_OK );.
157c9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
157ca 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65  agerUseWal(pPage
157cb 72 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  r) );.      asse
157cc 72 74 28 20 69 73 4f 70 65 6e 28 70 2d 3e 6a 66  rt( isOpen(p->jf
157cd 64 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 7c  d) .           |
157ce 7c 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  | p->journalMode
157cf 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
157d0 4f 44 45 5f 4f 46 46 20 0a 20 20 20 20 20 20 20  ODE_OFF .       
157d1 20 20 20 20 7c 7c 20 70 2d 3e 6a 6f 75 72 6e 61      || p->journa
157d2 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
157d3 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 0a 20 20  RNALMODE_WAL .  
157d4 20 20 20 20 29 3b 0a 20 20 20 20 20 20 62 72 65      );.      bre
157d5 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 20 50 41  ak;..    case PA
157d6 47 45 52 5f 45 52 52 4f 52 3a 0a 20 20 20 20 20  GER_ERROR:.     
157d7 20 2f 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62   /* There must b
157d8 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f  e at least one o
157d9 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72  utstanding refer
157da 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 67 65  ence to the page
157db 72 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 69 6e  r if.      ** in
157dc 20 45 52 52 4f 52 20 73 74 61 74 65 2e 20 4f 74   ERROR state. Ot
157dd 68 65 72 77 69 73 65 20 74 68 65 20 70 61 67 65  herwise the page
157de 72 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c  r should have al
157df 72 65 61 64 79 20 64 72 6f 70 70 65 64 0a 20 20  ready dropped.  
157e0 20 20 20 20 2a 2a 20 62 61 63 6b 20 74 6f 20 4f      ** back to O
157e1 50 45 4e 20 73 74 61 74 65 2e 0a 20 20 20 20 20  PEN state..     
157e2 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
157e3 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
157e4 65 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  e!=SQLITE_OK );.
157e5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71        assert( sq
157e6 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f  lite3PcacheRefCo
157e7 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  unt(pPager->pPCa
157e8 63 68 65 29 3e 30 20 29 3b 0a 20 20 20 20 20 20  che)>0 );.      
157e9 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65  break;.  }..  re
157ea 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66  turn 1;.}.#endif
157eb 20 2f 2a 20 69 66 6e 64 65 66 20 4e 44 45 42 55   /* ifndef NDEBU
157ec 47 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c  G */..#ifdef SQL
157ed 49 54 45 5f 44 45 42 55 47 20 0a 2f 2a 0a 2a 2a  ITE_DEBUG ./*.**
157ee 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
157ef 72 20 74 6f 20 61 20 68 75 6d 61 6e 20 72 65 61  r to a human rea
157f0 64 61 62 6c 65 20 73 74 72 69 6e 67 20 69 6e 20  dable string in 
157f1 61 20 73 74 61 74 69 63 20 62 75 66 66 65 72 0a  a static buffer.
157f2 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ** containing th
157f3 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 50  e state of the P
157f4 61 67 65 72 20 6f 62 6a 65 63 74 20 70 61 73 73  ager object pass
157f5 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
157f6 74 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 69 6e  t. This.** is in
157f7 74 65 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65  tended to be use
157f8 64 20 77 69 74 68 69 6e 20 64 65 62 75 67 67 65  d within debugge
157f9 72 73 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  rs. For example,
157fa 20 61 73 20 61 6e 20 61 6c 74 65 72 6e 61 74 69   as an alternati
157fb 76 65 0a 2a 2a 20 74 6f 20 22 70 72 69 6e 74 20  ve.** to "print 
157fc 2a 70 50 61 67 65 72 22 20 69 6e 20 67 64 62 3a  *pPager" in gdb:
157fd 0a 2a 2a 0a 2a 2a 20 28 67 64 62 29 20 70 72 69  .**.** (gdb) pri
157fe 6e 74 66 20 22 25 73 22 2c 20 70 72 69 6e 74 5f  ntf "%s", print_
157ff 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67  pager_state(pPag
15800 65 72 29 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68  er).*/.static ch
15801 61 72 20 2a 70 72 69 6e 74 5f 70 61 67 65 72 5f  ar *print_pager_
15802 73 74 61 74 65 28 50 61 67 65 72 20 2a 70 29 7b  state(Pager *p){
15803 0a 20 20 73 74 61 74 69 63 20 63 68 61 72 20 7a  .  static char z
15804 52 65 74 5b 31 30 32 34 5d 3b 0a 0a 20 20 73 71  Ret[1024];..  sq
15805 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31  lite3_snprintf(1
15806 30 32 34 2c 20 7a 52 65 74 2c 0a 20 20 20 20 20  024, zRet,.     
15807 20 22 46 69 6c 65 6e 61 6d 65 3a 20 20 20 20 20   "Filename:     
15808 20 25 73 5c 6e 22 0a 20 20 20 20 20 20 22 53 74   %s\n".      "St
15809 61 74 65 3a 20 20 20 20 20 20 20 20 20 25 73 20  ate:         %s 
1580a 65 72 72 43 6f 64 65 3d 25 64 5c 6e 22 0a 20 20  errCode=%d\n".  
1580b 20 20 20 20 22 4c 6f 63 6b 3a 20 20 20 20 20 20      "Lock:      
1580c 20 20 20 20 25 73 5c 6e 22 0a 20 20 20 20 20 20      %s\n".      
1580d 22 4c 6f 63 6b 69 6e 67 20 6d 6f 64 65 3a 20 20  "Locking mode:  
1580e 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 25 73 5c  locking_mode=%s\
1580f 6e 22 0a 20 20 20 20 20 20 22 4a 6f 75 72 6e 61  n".      "Journa
15810 6c 20 6d 6f 64 65 3a 20 20 6a 6f 75 72 6e 61 6c  l mode:  journal
15811 5f 6d 6f 64 65 3d 25 73 5c 6e 22 0a 20 20 20 20  _mode=%s\n".    
15812 20 20 22 42 61 63 6b 69 6e 67 20 73 74 6f 72 65    "Backing store
15813 3a 20 74 65 6d 70 46 69 6c 65 3d 25 64 20 6d 65  : tempFile=%d me
15814 6d 44 62 3d 25 64 20 75 73 65 4a 6f 75 72 6e 61  mDb=%d useJourna
15815 6c 3d 25 64 5c 6e 22 0a 20 20 20 20 20 20 22 4a  l=%d\n".      "J
15816 6f 75 72 6e 61 6c 3a 20 20 20 20 20 20 20 6a 6f  ournal:       jo
15817 75 72 6e 61 6c 4f 66 66 3d 25 6c 6c 64 20 6a 6f  urnalOff=%lld jo
15818 75 72 6e 61 6c 48 64 72 3d 25 6c 6c 64 5c 6e 22  urnalHdr=%lld\n"
15819 0a 20 20 20 20 20 20 22 53 69 7a 65 3a 20 20 20  .      "Size:   
1581a 20 20 20 20 20 20 20 64 62 73 69 7a 65 3d 25 64         dbsize=%d
1581b 20 64 62 4f 72 69 67 53 69 7a 65 3d 25 64 20 64   dbOrigSize=%d d
1581c 62 46 69 6c 65 53 69 7a 65 3d 25 64 5c 6e 22 0a  bFileSize=%d\n".
1581d 20 20 20 20 20 20 2c 20 70 2d 3e 7a 46 69 6c 65        , p->zFile
1581e 6e 61 6d 65 0a 20 20 20 20 20 20 2c 20 70 2d 3e  name.      , p->
1581f 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50  eState==PAGER_OP
15820 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 3f 20  EN            ? 
15821 22 4f 50 45 4e 22 20 3a 0a 20 20 20 20 20 20 20  "OPEN" :.       
15822 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45   p->eState==PAGE
15823 52 5f 52 45 41 44 45 52 20 20 20 20 20 20 20 20  R_READER        
15824 20 20 3f 20 22 52 45 41 44 45 52 22 20 3a 0a 20    ? "READER" :. 
15825 20 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65         p->eState
15826 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c  ==PAGER_WRITER_L
15827 4f 43 4b 45 44 20 20 20 3f 20 22 57 52 49 54 45  OCKED   ? "WRITE
15828 52 5f 4c 4f 43 4b 45 44 22 20 3a 0a 20 20 20 20  R_LOCKED" :.    
15829 20 20 20 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50      p->eState==P
1582a 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48  AGER_WRITER_CACH
1582b 45 4d 4f 44 20 3f 20 22 57 52 49 54 45 52 5f 43  EMOD ? "WRITER_C
1582c 41 43 48 45 4d 4f 44 22 20 3a 0a 20 20 20 20 20  ACHEMOD" :.     
1582d 20 20 20 70 2d 3e 65 53 74 61 74 65 3d 3d 50 41     p->eState==PA
1582e 47 45 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44  GER_WRITER_DBMOD
1582f 20 20 20 20 3f 20 22 57 52 49 54 45 52 5f 44 42      ? "WRITER_DB
15830 4d 4f 44 22 20 3a 0a 20 20 20 20 20 20 20 20 70  MOD" :.        p
15831 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f  ->eState==PAGER_
15832 57 52 49 54 45 52 5f 46 49 4e 49 53 48 45 44 20  WRITER_FINISHED 
15833 3f 20 22 57 52 49 54 45 52 5f 46 49 4e 49 53 48  ? "WRITER_FINISH
15834 45 44 22 20 3a 0a 20 20 20 20 20 20 20 20 70 2d  ED" :.        p-
15835 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 45  >eState==PAGER_E
15836 52 52 4f 52 20 20 20 20 20 20 20 20 20 20 20 3f  RROR           ?
15837 20 22 45 52 52 4f 52 22 20 3a 20 22 3f 65 72 72   "ERROR" : "?err
15838 6f 72 3f 22 0a 20 20 20 20 20 20 2c 20 28 69 6e  or?".      , (in
15839 74 29 70 2d 3e 65 72 72 43 6f 64 65 0a 20 20 20  t)p->errCode.   
1583a 20 20 20 2c 20 70 2d 3e 65 4c 6f 63 6b 3d 3d 4e     , p->eLock==N
1583b 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 3f  O_LOCK         ?
1583c 20 22 4e 4f 5f 4c 4f 43 4b 22 20 3a 0a 20 20 20   "NO_LOCK" :.   
1583d 20 20 20 20 20 70 2d 3e 65 4c 6f 63 6b 3d 3d 52       p->eLock==R
1583e 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 3f  ESERVED_LOCK   ?
1583f 20 22 52 45 53 45 52 56 45 44 22 20 3a 0a 20 20   "RESERVED" :.  
15840 20 20 20 20 20 20 70 2d 3e 65 4c 6f 63 6b 3d 3d        p->eLock==
15841 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20  EXCLUSIVE_LOCK  
15842 3f 20 22 45 58 43 4c 55 53 49 56 45 22 20 3a 0a  ? "EXCLUSIVE" :.
15843 20 20 20 20 20 20 20 20 70 2d 3e 65 4c 6f 63 6b          p->eLock
15844 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20  ==SHARED_LOCK   
15845 20 20 3f 20 22 53 48 41 52 45 44 22 20 3a 0a 20    ? "SHARED" :. 
15846 20 20 20 20 20 20 20 70 2d 3e 65 4c 6f 63 6b 3d         p->eLock=
15847 3d 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 20 20  =UNKNOWN_LOCK   
15848 20 3f 20 22 55 4e 4b 4e 4f 57 4e 22 20 3a 20 22   ? "UNKNOWN" : "
15849 3f 65 72 72 6f 72 3f 22 0a 20 20 20 20 20 20 2c  ?error?".      ,
1584a 20 70 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64   p->exclusiveMod
1584b 65 20 3f 20 22 65 78 63 6c 75 73 69 76 65 22 20  e ? "exclusive" 
1584c 3a 20 22 6e 6f 72 6d 61 6c 22 0a 20 20 20 20 20  : "normal".     
1584d 20 2c 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64   , p->journalMod
1584e 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
1584f 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 3f 20  MODE_MEMORY   ? 
15850 22 6d 65 6d 6f 72 79 22 20 3a 0a 20 20 20 20 20  "memory" :.     
15851 20 20 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64     p->journalMod
15852 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
15853 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20 20 3f 20  MODE_OFF      ? 
15854 22 6f 66 66 22 20 3a 0a 20 20 20 20 20 20 20 20  "off" :.        
15855 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  p->journalMode==
15856 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
15857 45 5f 44 45 4c 45 54 45 20 20 20 3f 20 22 64 65  E_DELETE   ? "de
15858 6c 65 74 65 22 20 3a 0a 20 20 20 20 20 20 20 20  lete" :.        
15859 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  p->journalMode==
1585a 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
1585b 45 5f 50 45 52 53 49 53 54 20 20 3f 20 22 70 65  E_PERSIST  ? "pe
1585c 72 73 69 73 74 22 20 3a 0a 20 20 20 20 20 20 20  rsist" :.       
1585d 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d   p->journalMode=
1585e 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
1585f 44 45 5f 54 52 55 4e 43 41 54 45 20 3f 20 22 74  DE_TRUNCATE ? "t
15860 72 75 6e 63 61 74 65 22 20 3a 0a 20 20 20 20 20  runcate" :.     
15861 20 20 20 70 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64     p->journalMod
15862 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
15863 4d 4f 44 45 5f 57 41 4c 20 20 20 20 20 20 3f 20  MODE_WAL      ? 
15864 22 77 61 6c 22 20 3a 20 22 3f 65 72 72 6f 72 3f  "wal" : "?error?
15865 22 0a 20 20 20 20 20 20 2c 20 28 69 6e 74 29 70  ".      , (int)p
15866 2d 3e 74 65 6d 70 46 69 6c 65 2c 20 28 69 6e 74  ->tempFile, (int
15867 29 70 2d 3e 6d 65 6d 44 62 2c 20 28 69 6e 74 29  )p->memDb, (int)
15868 70 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 0a 20 20  p->useJournal.  
15869 20 20 20 20 2c 20 70 2d 3e 6a 6f 75 72 6e 61 6c      , p->journal
1586a 4f 66 66 2c 20 70 2d 3e 6a 6f 75 72 6e 61 6c 48  Off, p->journalH
1586b 64 72 0a 20 20 20 20 20 20 2c 20 28 69 6e 74 29  dr.      , (int)
1586c 70 2d 3e 64 62 53 69 7a 65 2c 20 28 69 6e 74 29  p->dbSize, (int)
1586d 70 2d 3e 64 62 4f 72 69 67 53 69 7a 65 2c 20 28  p->dbOrigSize, (
1586e 69 6e 74 29 70 2d 3e 64 62 46 69 6c 65 53 69 7a  int)p->dbFileSiz
1586f 65 0a 20 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e  e.  );..  return
15870 20 7a 52 65 74 3b 0a 7d 0a 23 65 6e 64 69 66 0a   zRet;.}.#endif.
15871 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
15872 75 65 20 69 66 20 69 74 20 69 73 20 6e 65 63 65  ue if it is nece
15873 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20 70  ssary to write p
15874 61 67 65 20 2a 70 50 67 20 69 6e 74 6f 20 74 68  age *pPg into th
15875 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a 2a  e sub-journal..*
15876 2a 20 41 20 70 61 67 65 20 6e 65 65 64 73 20 74  * A page needs t
15877 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74  o be written int
15878 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61  o the sub-journa
15879 6c 20 69 66 20 74 68 65 72 65 20 65 78 69 73 74  l if there exist
1587a 73 20 6f 6e 65 0a 2a 2a 20 6f 72 20 6d 6f 72 65  s one.** or more
1587b 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73   open savepoints
1587c 20 66 6f 72 20 77 68 69 63 68 3a 0a 2a 2a 0a 2a   for which:.**.*
1587d 2a 20 20 20 2a 20 54 68 65 20 70 61 67 65 2d 6e  *   * The page-n
1587e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20 74 68  umber is less th
1587f 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 50  an or equal to P
15880 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 6e 4f  agerSavepoint.nO
15881 72 69 67 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20  rig, and.**   * 
15882 54 68 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f  The bit correspo
15883 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 70 61 67  nding to the pag
15884 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20  e-number is not 
15885 73 65 74 20 69 6e 0a 2a 2a 20 20 20 20 20 50 61  set in.**     Pa
15886 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e  gerSavepoint.pIn
15887 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74  Savepoint..*/.st
15888 61 74 69 63 20 69 6e 74 20 73 75 62 6a 52 65 71  atic int subjReq
15889 75 69 72 65 73 50 61 67 65 28 50 67 48 64 72 20  uiresPage(PgHdr 
1588a 2a 70 50 67 29 7b 0a 20 20 50 67 6e 6f 20 70 67  *pPg){.  Pgno pg
1588b 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a  no = pPg->pgno;.
1588c 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20    Pager *pPager 
1588d 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20  = pPg->pPager;. 
1588e 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
1588f 30 3b 20 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61  0; i<pPager->nSa
15890 76 65 70 6f 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20  vepoint; i++){. 
15891 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e     PagerSavepoin
15892 74 20 2a 70 20 3d 20 26 70 50 61 67 65 72 2d 3e  t *p = &pPager->
15893 61 53 61 76 65 70 6f 69 6e 74 5b 69 5d 3b 0a 20  aSavepoint[i];. 
15894 20 20 20 69 66 28 20 70 2d 3e 6e 4f 72 69 67 3e     if( p->nOrig>
15895 3d 70 67 6e 6f 20 26 26 20 30 3d 3d 73 71 6c 69  =pgno && 0==sqli
15896 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 2d  te3BitvecTest(p-
15897 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 70  >pInSavepoint, p
15898 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 72 65  gno) ){.      re
15899 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
1589a 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
1589b 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
1589c 75 65 20 69 66 20 74 68 65 20 70 61 67 65 20 69  ue if the page i
1589d 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  s already in the
1589e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a   journal file..*
1589f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
158a0 65 49 6e 4a 6f 75 72 6e 61 6c 28 50 67 48 64 72  eInJournal(PgHdr
158a1 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e   *pPg){.  return
158a2 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65   sqlite3BitvecTe
158a3 73 74 28 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e  st(pPg->pPager->
158a4 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d  pInJournal, pPg-
158a5 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  >pgno);.}../*.**
158a6 20 52 65 61 64 20 61 20 33 32 2d 62 69 74 20 69   Read a 32-bit i
158a7 6e 74 65 67 65 72 20 66 72 6f 6d 20 74 68 65 20  nteger from the 
158a8 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 63 72  given file descr
158a9 69 70 74 6f 72 2e 20 20 53 74 6f 72 65 20 74 68  iptor.  Store th
158aa 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 68 61  e integer.** tha
158ab 74 20 69 73 20 72 65 61 64 20 69 6e 20 2a 70 52  t is read in *pR
158ac 65 73 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49  es.  Return SQLI
158ad 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68  TE_OK if everyth
158ae 69 6e 67 20 77 6f 72 6b 65 64 2c 20 6f 72 20 61  ing worked, or a
158af 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20  n.** error code 
158b0 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65  is something goe
158b1 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41  s wrong..**.** A
158b2 6c 6c 20 76 61 6c 75 65 73 20 61 72 65 20 73 74  ll values are st
158b3 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20 61 73 20  ored on disk as 
158b4 62 69 67 2d 65 6e 64 69 61 6e 2e 0a 2a 2f 0a 73  big-endian..*/.s
158b5 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 33 32  tatic int read32
158b6 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c  bits(sqlite3_fil
158b7 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65  e *fd, i64 offse
158b8 74 2c 20 75 33 32 20 2a 70 52 65 73 29 7b 0a 20  t, u32 *pRes){. 
158b9 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61   unsigned char a
158ba 63 5b 34 5d 3b 0a 20 20 69 6e 74 20 72 63 20 3d  c[4];.  int rc =
158bb 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 66   sqlite3OsRead(f
158bc 64 2c 20 61 63 2c 20 73 69 7a 65 6f 66 28 61 63  d, ac, sizeof(ac
158bd 29 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 69 66  ), offset);.  if
158be 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
158bf 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 73  ){.    *pRes = s
158c0 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 61  qlite3Get4byte(a
158c1 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  c);.  }.  return
158c2 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72   rc;.}../*.** Wr
158c3 69 74 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74  ite a 32-bit int
158c4 65 67 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69  eger into a stri
158c5 6e 67 20 62 75 66 66 65 72 20 69 6e 20 62 69 67  ng buffer in big
158c6 2d 65 6e 64 69 61 6e 20 62 79 74 65 20 6f 72 64  -endian byte ord
158c7 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70  er..*/.#define p
158c8 75 74 33 32 62 69 74 73 28 41 2c 42 29 20 20 73  ut32bits(A,B)  s
158c9 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 28  qlite3Put4byte((
158ca 75 38 2a 29 41 2c 42 29 0a 0a 0a 2f 2a 0a 2a 2a  u8*)A,B).../*.**
158cb 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 20   Write a 32-bit 
158cc 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 74 68 65  integer into the
158cd 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 63   given file desc
158ce 72 69 70 74 6f 72 2e 20 20 52 65 74 75 72 6e 20  riptor.  Return 
158cf 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20  SQLITE_OK.** on 
158d0 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72  success or an er
158d1 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65  ror code is some
158d2 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
158d3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
158d4 77 72 69 74 65 33 32 62 69 74 73 28 73 71 6c 69  write32bits(sqli
158d5 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69 36  te3_file *fd, i6
158d6 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 76 61  4 offset, u32 va
158d7 6c 29 7b 0a 20 20 63 68 61 72 20 61 63 5b 34 5d  l){.  char ac[4]
158d8 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 61 63  ;.  put32bits(ac
158d9 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e  , val);.  return
158da 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
158db 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66 66 73 65  fd, ac, 4, offse
158dc 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c  t);.}../*.** Unl
158dd 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65  ock the database
158de 20 66 69 6c 65 20 74 6f 20 6c 65 76 65 6c 20 65   file to level e
158df 4c 6f 63 6b 2c 20 77 68 69 63 68 20 6d 75 73 74  Lock, which must
158e0 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f   be either NO_LO
158e1 43 4b 0a 2a 2a 20 6f 72 20 53 48 41 52 45 44 5f  CK.** or SHARED_
158e2 4c 4f 43 4b 2e 20 52 65 67 61 72 64 6c 65 73 73  LOCK. Regardless
158e3 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e   of whether or n
158e4 6f 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 78  ot the call to x
158e5 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 73 75 63 63  Unlock().** succ
158e6 65 65 64 73 2c 20 73 65 74 20 74 68 65 20 50 61  eeds, set the Pa
158e7 67 65 72 2e 65 4c 6f 63 6b 20 76 61 72 69 61 62  ger.eLock variab
158e8 6c 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20  le to match the 
158e9 28 61 74 74 65 6d 70 74 65 64 29 20 6e 65 77 20  (attempted) new 
158ea 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65  lock..**.** Exce
158eb 70 74 2c 20 69 66 20 50 61 67 65 72 2e 65 4c 6f  pt, if Pager.eLo
158ec 63 6b 20 69 73 20 73 65 74 20 74 6f 20 55 4e 4b  ck is set to UNK
158ed 4e 4f 57 4e 5f 4c 4f 43 4b 20 77 68 65 6e 20 74  NOWN_LOCK when t
158ee 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a  his function is.
158ef 2a 2a 20 63 61 6c 6c 65 64 2c 20 64 6f 20 6e 6f  ** called, do no
158f0 74 20 6d 6f 64 69 66 79 20 69 74 2e 20 53 65 65  t modify it. See
158f1 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f   the comment abo
158f2 76 65 20 74 68 65 20 23 64 65 66 69 6e 65 20 6f  ve the #define o
158f3 66 20 0a 2a 2a 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f  f .** UNKNOWN_LO
158f4 43 4b 20 66 6f 72 20 61 6e 20 65 78 70 6c 61 6e  CK for an explan
158f5 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 2e 0a 2a  ation of this..*
158f6 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
158f7 65 72 55 6e 6c 6f 63 6b 44 62 28 50 61 67 65 72  erUnlockDb(Pager
158f8 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4c   *pPager, int eL
158f9 6f 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ock){.  int rc =
158fa 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61   SQLITE_OK;..  a
158fb 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e  ssert( !pPager->
158fc 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c  exclusiveMode ||
158fd 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d   pPager->eLock==
158fe 65 4c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72  eLock );.  asser
158ff 74 28 20 65 4c 6f 63 6b 3d 3d 4e 4f 5f 4c 4f 43  t( eLock==NO_LOC
15900 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 53 48 41 52  K || eLock==SHAR
15901 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  ED_LOCK );.  ass
15902 65 72 74 28 20 65 4c 6f 63 6b 21 3d 4e 4f 5f 4c  ert( eLock!=NO_L
15903 4f 43 4b 20 7c 7c 20 70 61 67 65 72 55 73 65 57  OCK || pagerUseW
15904 61 6c 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b  al(pPager)==0 );
15905 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50  .  if( isOpen(pP
15906 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20  ager->fd) ){.   
15907 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
15908 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f 63 6b 20 29 3b  >eLock>=eLock );
15909 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1590a 33 4f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72  3OsUnlock(pPager
1590b 2d 3e 66 64 2c 20 65 4c 6f 63 6b 29 3b 0a 20 20  ->fd, eLock);.  
1590c 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 4c    if( pPager->eL
1590d 6f 63 6b 21 3d 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43  ock!=UNKNOWN_LOC
1590e 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65  K ){.      pPage
1590f 72 2d 3e 65 4c 6f 63 6b 20 3d 20 28 75 38 29 65  r->eLock = (u8)e
15910 4c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Lock;.    }.    
15911 49 4f 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b  IOTRACE(("UNLOCK
15912 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65   %p %d\n", pPage
15913 72 2c 20 65 4c 6f 63 6b 29 29 0a 20 20 7d 0a 20  r, eLock)).  }. 
15914 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
15915 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 64 61  *.** Lock the da
15916 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6c  tabase file to l
15917 65 76 65 6c 20 65 4c 6f 63 6b 2c 20 77 68 69 63  evel eLock, whic
15918 68 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  h must be either
15919 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 0a 2a 2a   SHARED_LOCK,.**
1591a 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 6f   RESERVED_LOCK o
1591b 72 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  r EXCLUSIVE_LOCK
1591c 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  . If the caller 
1591d 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73  is successful, s
1591e 65 74 20 74 68 65 0a 2a 2a 20 50 61 67 65 72 2e  et the.** Pager.
1591f 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65 20 74  eLock variable t
15920 6f 20 74 68 65 20 6e 65 77 20 6c 6f 63 6b 69 6e  o the new lockin
15921 67 20 73 74 61 74 65 2e 20 0a 2a 2a 0a 2a 2a 20  g state. .**.** 
15922 45 78 63 65 70 74 2c 20 69 66 20 50 61 67 65 72  Except, if Pager
15923 2e 65 4c 6f 63 6b 20 69 73 20 73 65 74 20 74 6f  .eLock is set to
15924 20 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 77 68   UNKNOWN_LOCK wh
15925 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
15926 20 69 73 20 0a 2a 2a 20 63 61 6c 6c 65 64 2c 20   is .** called, 
15927 64 6f 20 6e 6f 74 20 6d 6f 64 69 66 79 20 69 74  do not modify it
15928 20 75 6e 6c 65 73 73 20 74 68 65 20 6e 65 77 20   unless the new 
15929 6c 6f 63 6b 69 6e 67 20 73 74 61 74 65 20 69 73  locking state is
1592a 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 2e   EXCLUSIVE_LOCK.
1592b 20 0a 2a 2a 20 53 65 65 20 74 68 65 20 63 6f 6d   .** See the com
1592c 6d 65 6e 74 20 61 62 6f 76 65 20 74 68 65 20 23  ment above the #
1592d 64 65 66 69 6e 65 20 6f 66 20 55 4e 4b 4e 4f 57  define of UNKNOW
1592e 4e 5f 4c 4f 43 4b 20 66 6f 72 20 61 6e 20 65 78  N_LOCK for an ex
1592f 70 6c 61 6e 61 74 69 6f 6e 20 0a 2a 2a 20 6f 66  planation .** of
15930 20 74 68 69 73 2e 0a 2a 2f 0a 73 74 61 74 69 63   this..*/.static
15931 20 69 6e 74 20 70 61 67 65 72 4c 6f 63 6b 44 62   int pagerLockDb
15932 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
15933 69 6e 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 6e  int eLock){.  in
15934 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
15935 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f  ;..  assert( eLo
15936 63 6b 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ck==SHARED_LOCK 
15937 7c 7c 20 65 4c 6f 63 6b 3d 3d 52 45 53 45 52 56  || eLock==RESERV
15938 45 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b  ED_LOCK || eLock
15939 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
1593a 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72   );.  if( pPager
1593b 2d 3e 65 4c 6f 63 6b 3c 65 4c 6f 63 6b 20 7c 7c  ->eLock<eLock ||
1593c 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d   pPager->eLock==
1593d 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b 20 29 7b 0a  UNKNOWN_LOCK ){.
1593e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1593f 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66  OsLock(pPager->f
15940 64 2c 20 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 69  d, eLock);.    i
15941 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
15942 20 26 26 20 28 70 50 61 67 65 72 2d 3e 65 4c 6f   && (pPager->eLo
15943 63 6b 21 3d 55 4e 4b 4e 4f 57 4e 5f 4c 4f 43 4b  ck!=UNKNOWN_LOCK
15944 7c 7c 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49  ||eLock==EXCLUSI
15945 56 45 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20  VE_LOCK) ){.    
15946 20 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 20    pPager->eLock 
15947 3d 20 28 75 38 29 65 4c 6f 63 6b 3b 0a 20 20 20  = (u8)eLock;.   
15948 20 20 20 49 4f 54 52 41 43 45 28 28 22 4c 4f 43     IOTRACE(("LOC
15949 4b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  K %p %d\n", pPag
1594a 65 72 2c 20 65 4c 6f 63 6b 29 29 0a 20 20 20 20  er, eLock)).    
1594b 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
1594c 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
1594d 20 66 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d   function determ
1594e 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20  ines whether or 
1594f 6e 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77  not the atomic-w
15950 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f  rite optimizatio
15951 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64  n.** can be used
15952 20 77 69 74 68 20 74 68 69 73 20 70 61 67 65 72   with this pager
15953 2e 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69  . The optimizati
15954 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69  on can be used i
15955 66 3a 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68  f:.**.**  (a) th
15956 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
15957 20 62 79 20 4f 73 44 65 76 69 63 65 43 68 61 72   by OsDeviceChar
15958 61 63 74 65 72 69 73 74 69 63 73 28 29 20 69 6e  acteristics() in
15959 64 69 63 61 74 65 73 20 74 68 61 74 0a 2a 2a 20  dicates that.** 
1595a 20 20 20 20 20 61 20 64 61 74 61 62 61 73 65 20       a database 
1595b 70 61 67 65 20 6d 61 79 20 62 65 20 77 72 69 74  page may be writ
1595c 74 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20  ten atomically, 
1595d 61 6e 64 0a 2a 2a 20 20 28 62 29 20 74 68 65 20  and.**  (b) the 
1595e 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
1595f 79 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29  y OsSectorSize()
15960 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72   is less than or
15961 20 65 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74   equal.**      t
15962 6f 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e  o the page size.
15963 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d  .**.** The optim
15964 69 7a 61 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20  ization is also 
15965 61 6c 77 61 79 73 20 65 6e 61 62 6c 65 64 20 66  always enabled f
15966 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  or temporary fil
15967 65 73 2e 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20  es. It is.** an 
15968 65 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68  error to call th
15969 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 70  is function if p
1596a 50 61 67 65 72 20 69 73 20 6f 70 65 6e 65 64 20  Pager is opened 
1596b 6f 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a  on an in-memory.
1596c 2a 2a 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  ** database..**.
1596d 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69  ** If the optimi
1596e 7a 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65  zation cannot be
1596f 20 75 73 65 64 2c 20 30 20 69 73 20 72 65 74 75   used, 0 is retu
15970 72 6e 65 64 2e 20 49 66 20 69 74 20 63 61 6e 20  rned. If it can 
15971 62 65 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e  be used,.** then
15972 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
15973 6e 65 64 20 69 73 20 74 68 65 20 73 69 7a 65 20  ned is the size 
15974 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
15975 69 6c 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63  ile when it.** c
15976 6f 6e 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b  ontains rollback
15977 20 64 61 74 61 20 66 6f 72 20 65 78 61 63 74 6c   data for exactl
15978 79 20 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23  y one page..*/.#
15979 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
1597a 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45  BLE_ATOMIC_WRITE
1597b 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c  .static int jrnl
1597c 42 75 66 66 65 72 53 69 7a 65 28 50 61 67 65 72  BufferSize(Pager
1597d 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73   *pPager){.  ass
1597e 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20  ert( !MEMDB );. 
1597f 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65   if( !pPager->te
15980 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e  mpFile ){.    in
15981 74 20 64 63 3b 20 20 20 20 20 20 20 20 20 20 20  t dc;           
15982 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15983 2f 2a 20 44 65 76 69 63 65 20 63 68 61 72 61 63  /* Device charac
15984 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 20  teristics */.   
15985 20 69 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 20   int nSector;   
15986 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15987 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a     /* Sector siz
15988 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50  e */.    int szP
15989 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
1598a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
1598b 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20  ge size */..    
1598c 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
1598d 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20  Pager->fd) );.  
1598e 20 20 64 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    dc = sqlite3Os
1598f 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
15990 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64  stics(pPager->fd
15991 29 3b 0a 20 20 20 20 6e 53 65 63 74 6f 72 20 3d  );.    nSector =
15992 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53   pPager->sectorS
15993 69 7a 65 3b 0a 20 20 20 20 73 7a 50 61 67 65 20  ize;.    szPage 
15994 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  = pPager->pageSi
15995 7a 65 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28  ze;..    assert(
15996 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
15997 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29  MIC512==(512>>8)
15998 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 53 51  );.    assert(SQ
15999 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
1599a 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29  C64K==(65536>>8)
1599b 29 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 64  );.    if( 0==(d
1599c 63 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  c&(SQLITE_IOCAP_
1599d 41 54 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e  ATOMIC|(szPage>>
1599e 38 29 29 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73  8)) || nSector>s
1599f 7a 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20  zPage) ){.      
159a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
159a1 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f    }..  return JO
159a2 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
159a3 67 65 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50  ger) + JOURNAL_P
159a4 47 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a  G_SZ(pPager);.}.
159a5 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66  #endif../*.** If
159a6 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
159a7 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 74  GES is defined t
159a8 68 65 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73  hen we do some s
159a9 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a  anity checking.*
159aa 2a 20 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75  * on the cache u
159ab 73 69 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63  sing a hash func
159ac 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75  tion.  This is u
159ad 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a  sed for testing.
159ae 2a 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ** and debugging
159af 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66   only..*/.#ifdef
159b0 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
159b1 47 45 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  GES./*.** Return
159b2 20 61 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f   a 32-bit hash o
159b3 66 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20  f the page data 
159b4 66 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74  for pPage..*/.st
159b5 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 64  atic u32 pager_d
159b6 61 74 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74  atahash(int nByt
159b7 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  e, unsigned char
159b8 20 2a 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20   *pData){.  u32 
159b9 68 61 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20  hash = 0;.  int 
159ba 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
159bb 6e 42 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  nByte; i++){.   
159bc 20 68 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30   hash = (hash*10
159bd 33 39 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a  39) + pData[i];.
159be 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73    }.  return has
159bf 68 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20  h;.}.static u32 
159c0 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 50  pager_pagehash(P
159c1 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20  gHdr *pPage){.  
159c2 72 65 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74  return pager_dat
159c3 61 68 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61  ahash(pPage->pPa
159c4 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28  ger->pageSize, (
159c5 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
159c6 70 50 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d  pPage->pData);.}
159c7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67  .static void pag
159c8 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28  er_set_pagehash(
159c9 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20  PgHdr *pPage){. 
159ca 20 70 50 61 67 65 2d 3e 70 61 67 65 48 61 73 68   pPage->pageHash
159cb 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73   = pager_pagehas
159cc 68 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a  h(pPage);.}../*.
159cd 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 50 41 47  ** The CHECK_PAG
159ce 45 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20  E macro takes a 
159cf 50 67 48 64 72 2a 20 61 73 20 61 6e 20 61 72 67  PgHdr* as an arg
159d0 75 6d 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 45  ument. If SQLITE
159d1 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20  _CHECK_PAGES.** 
159d2 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 20  is defined, and 
159d3 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65  NDEBUG is not de
159d4 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74  fined, an assert
159d5 28 29 20 73 74 61 74 65 6d 65 6e 74 20 63 68 65  () statement che
159d6 63 6b 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  cks.** that the 
159d7 70 61 67 65 20 69 73 20 65 69 74 68 65 72 20 64  page is either d
159d8 69 72 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61  irty or still ma
159d9 74 63 68 65 73 20 74 68 65 20 63 61 6c 63 75 6c  tches the calcul
159da 61 74 65 64 20 70 61 67 65 2d 68 61 73 68 2e 0a  ated page-hash..
159db 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b  */.#define CHECK
159dc 5f 50 41 47 45 28 78 29 20 63 68 65 63 6b 50 61  _PAGE(x) checkPa
159dd 67 65 28 78 29 0a 73 74 61 74 69 63 20 76 6f 69  ge(x).static voi
159de 64 20 63 68 65 63 6b 50 61 67 65 28 50 67 48 64  d checkPage(PgHd
159df 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72  r *pPg){.  Pager
159e0 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
159e1 70 50 61 67 65 72 3b 0a 20 20 61 73 73 65 72 74  pPager;.  assert
159e2 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65  ( pPager->eState
159e3 21 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 29 3b  !=PAGER_ERROR );
159e4 0a 20 20 61 73 73 65 72 74 28 20 28 70 50 67 2d  .  assert( (pPg-
159e5 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
159e6 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 67 65  TY) || pPg->page
159e7 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 67 65  Hash==pager_page
159e8 68 61 73 68 28 70 50 67 29 20 29 3b 0a 7d 0a 0a  hash(pPg) );.}..
159e9 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 70 61  #else.#define pa
159ea 67 65 72 5f 64 61 74 61 68 61 73 68 28 58 2c 59  ger_datahash(X,Y
159eb 29 20 20 30 0a 23 64 65 66 69 6e 65 20 70 61 67  )  0.#define pag
159ec 65 72 5f 70 61 67 65 68 61 73 68 28 58 29 20 20  er_pagehash(X)  
159ed 30 0a 23 64 65 66 69 6e 65 20 70 61 67 65 72 5f  0.#define pager_
159ee 73 65 74 5f 70 61 67 65 68 61 73 68 28 58 29 0a  set_pagehash(X).
159ef 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 41  #define CHECK_PA
159f0 47 45 28 78 29 0a 23 65 6e 64 69 66 20 20 2f 2a  GE(x).#endif  /*
159f1 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
159f2 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 68  GES */../*.** Wh
159f3 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65  en this is calle
159f4 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  d the journal fi
159f5 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 61  le for pager pPa
159f6 67 65 72 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  ger must be open
159f7 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
159f8 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72  on attempts to r
159f9 65 61 64 20 61 20 6d 61 73 74 65 72 20 6a 6f 75  ead a master jou
159fa 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 66  rnal file name f
159fb 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 65 6e 64 20  rom the .** end 
159fc 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e 64 2c  of the file and,
159fd 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   if successful, 
159fe 63 6f 70 69 65 73 20 69 74 20 69 6e 74 6f 20 6d  copies it into m
159ff 65 6d 6f 72 79 20 73 75 70 70 6c 69 65 64 20 0a  emory supplied .
15a00 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ** by the caller
15a01 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61  . See comments a
15a02 62 6f 76 65 20 77 72 69 74 65 4d 61 73 74 65 72  bove writeMaster
15a03 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72 20 74 68  Journal() for th
15a04 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 75 73 65 64  e format.** used
15a05 20 74 6f 20 73 74 6f 72 65 20 61 20 6d 61 73 74   to store a mast
15a06 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
15a07 6e 61 6d 65 20 61 74 20 74 68 65 20 65 6e 64 20  name at the end 
15a08 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  of a journal fil
15a09 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 65 72  e..**.** zMaster
15a0a 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61   must point to a
15a0b 20 62 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65   buffer of at le
15a0c 61 73 74 20 6e 4d 61 73 74 65 72 20 62 79 74 65  ast nMaster byte
15a0d 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a  s allocated by.*
15a0e 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 54 68  * the caller. Th
15a0f 69 73 20 73 68 6f 75 6c 64 20 62 65 20 73 71 6c  is should be sql
15a10 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e  ite3_vfs.mxPathn
15a11 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 75 72 65  ame+1 (to ensure
15a12 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 65 6e 6f   there is.** eno
15a13 75 67 68 20 73 70 61 63 65 20 74 6f 20 77 72 69  ugh space to wri
15a14 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  te the master jo
15a15 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 66 20  urnal name). If 
15a16 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
15a17 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 74 68  al.** name in th
15a18 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 6f 6e  e journal is lon
15a19 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 74 65 72  ger than nMaster
15a1a 20 62 79 74 65 73 20 28 69 6e 63 6c 75 64 69 6e   bytes (includin
15a1b 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69  g a.** nul-termi
15a1c 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 74 68 69  nator), then thi
15a1d 73 20 69 73 20 68 61 6e 64 6c 65 64 20 61 73 20  s is handled as 
15a1e 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75  if no master jou
15a1f 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 65 72  rnal name.** wer
15a20 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65  e present in the
15a21 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20   journal..**.** 
15a22 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  If a master jour
15a23 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73  nal file name is
15a24 20 70 72 65 73 65 6e 74 20 61 74 20 74 68 65 20   present at the 
15a25 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  end of the journ
15a26 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20 74 68 65 6e  al.** file, then
15a27 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 69 6e   it is copied in
15a28 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 70 6f  to the buffer po
15a29 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73  inted to by zMas
15a2a 74 65 72 2e 20 41 0a 2a 2a 20 6e 75 6c 2d 74 65  ter. A.** nul-te
15a2b 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 69 73  rminator byte is
15a2c 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65   appended to the
15a2d 20 62 75 66 66 65 72 20 66 6f 6c 6c 6f 77 69 6e   buffer followin
15a2e 67 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20  g the master.** 
15a2f 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d  journal file nam
15a30 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69  e..**.** If it i
15a31 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68 61  s determined tha
15a32 74 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72  t no master jour
15a33 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73  nal file name is
15a34 20 70 72 65 73 65 6e 74 20 0a 2a 2a 20 7a 4d 61   present .** zMa
15a35 73 74 65 72 5b 30 5d 20 69 73 20 73 65 74 20 74  ster[0] is set t
15a36 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f  o 0 and SQLITE_O
15a37 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  K returned..**.*
15a38 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
15a39 63 75 72 73 20 77 68 69 6c 65 20 72 65 61 64 69  curs while readi
15a3a 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  ng from the jour
15a3b 6e 61 6c 20 66 69 6c 65 2c 20 61 6e 20 53 51 4c  nal file, an SQL
15a3c 69 74 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64  ite.** error cod
15a3d 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
15a3e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61  /.static int rea
15a3f 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 73  dMasterJournal(s
15a40 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 72  qlite3_file *pJr
15a41 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d 61 73 74 65  nl, char *zMaste
15a42 72 2c 20 75 33 32 20 6e 4d 61 73 74 65 72 29 7b  r, u32 nMaster){
15a43 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
15a44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15a45 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
15a46 20 20 75 33 32 20 6c 65 6e 3b 20 20 20 20 20 20    u32 len;      
15a47 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15a48 4c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20  Length in bytes 
15a49 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  of master journa
15a4a 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 36 34 20  l name */.  i64 
15a4b 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20  szJ;            
15a4c 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20         /* Total 
15a4d 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66  size in bytes of
15a4e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 70 4a   journal file pJ
15a4f 72 6e 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73  rnl */.  u32 cks
15a50 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  um;             
15a51 20 20 20 20 2f 2a 20 4d 4a 20 63 68 65 63 6b 73      /* MJ checks
15a52 75 6d 20 76 61 6c 75 65 20 72 65 61 64 20 66 72  um value read fr
15a53 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20  om journal */.  
15a54 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20 20  u32 u;          
15a55 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e             /* Un
15a56 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e  signed loop coun
15a57 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ter */.  unsigne
15a58 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38 5d  d char aMagic[8]
15a59 3b 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72 20  ;   /* A buffer 
15a5a 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69  to hold the magi
15a5b 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 7a 4d  c header */.  zM
15a5c 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b  aster[0] = '\0';
15a5d 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
15a5e 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K!=(rc = sqlite3
15a5f 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a 72 6e 6c  OsFileSize(pJrnl
15a60 2c 20 26 73 7a 4a 29 29 0a 20 20 20 7c 7c 20 73  , &szJ)).   || s
15a61 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20 53 51 4c 49  zJ<16.   || SQLI
15a62 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61  TE_OK!=(rc = rea
15a63 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73  d32bits(pJrnl, s
15a64 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 29 0a 20 20  zJ-16, &len)).  
15a65 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61 73 74 65 72   || len>=nMaster
15a66 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f   .   || SQLITE_O
15a67 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62  K!=(rc = read32b
15a68 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31  its(pJrnl, szJ-1
15a69 32 2c 20 26 63 6b 73 75 6d 29 29 0a 20 20 20 7c  2, &cksum)).   |
15a6a 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  | SQLITE_OK!=(rc
15a6b 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
15a6c 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69 63 2c 20  (pJrnl, aMagic, 
15a6d 38 2c 20 73 7a 4a 2d 38 29 29 0a 20 20 20 7c 7c  8, szJ-8)).   ||
15a6e 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20   memcmp(aMagic, 
15a6f 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38  aJournalMagic, 8
15a70 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f  ).   || SQLITE_O
15a71 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K!=(rc = sqlite3
15a72 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 7a 4d  OsRead(pJrnl, zM
15a73 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73 7a 4a 2d  aster, len, szJ-
15a74 31 36 2d 6c 65 6e 29 29 0a 20 20 29 7b 0a 20 20  16-len)).  ){.  
15a75 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
15a76 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 74 68  ..  /* See if th
15a77 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 74 63 68  e checksum match
15a78 65 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  es the master jo
15a79 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20  urnal name */.  
15a7a 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 6e 3b 20  for(u=0; u<len; 
15a7b 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20  u++){.    cksum 
15a7c 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d 3b 0a 20  -= zMaster[u];. 
15a7d 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 6d 20 29   }.  if( cksum )
15a7e 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  {.    /* If the 
15a7f 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 6e 27 74  checksum doesn't
15a80 20 61 64 64 20 75 70 2c 20 74 68 65 6e 20 6f 6e   add up, then on
15a81 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65  e or more of the
15a82 20 64 69 73 6b 20 73 65 63 74 6f 72 73 0a 20 20   disk sectors.  
15a83 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20    ** containing 
15a84 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
15a85 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 63  al filename is c
15a86 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 20 6d  orrupted. This m
15a87 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 65 66 69  eans.    ** defi
15a88 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 61 63 6b  nitely roll back
15a89 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 75 72 6e  , so just return
15a8a 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 72   SQLITE_OK and r
15a8b 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 0a 20 20  eport a (nul).  
15a8c 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72    ** master-jour
15a8d 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 20 20  nal filename..  
15a8e 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 30    */.    len = 0
15a8f 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 5b  ;.  }.  zMaster[
15a90 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20  len] = '\0';.   
15a91 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
15a92 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  _OK;.}../*.** Re
15a93 74 75 72 6e 20 74 68 65 20 6f 66 66 73 65 74 20  turn the offset 
15a94 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 62 6f  of the sector bo
15a95 75 6e 64 61 72 79 20 61 74 20 6f 72 20 69 6d 6d  undary at or imm
15a96 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 66 6f 6c  ediately .** fol
15a97 6c 6f 77 69 6e 67 20 74 68 65 20 76 61 6c 75 65  lowing the value
15a98 20 69 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   in pPager->jour
15a99 6e 61 6c 4f 66 66 2c 20 61 73 73 75 6d 69 6e 67  nalOff, assuming
15a9a 20 61 20 73 65 63 74 6f 72 20 0a 2a 2a 20 73 69   a sector .** si
15a9b 7a 65 20 6f 66 20 70 50 61 67 65 72 2d 3e 73 65  ze of pPager->se
15a9c 63 74 6f 72 53 69 7a 65 20 62 79 74 65 73 2e 0a  ctorSize bytes..
15a9d 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f 72 20 61 20  **.** i.e for a 
15a9e 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 35  sector size of 5
15a9f 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67 65  12:.**.**   Page
15aa0 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 20 20 20  r.journalOff    
15aa1 20 20 20 20 20 20 52 65 74 75 72 6e 20 76 61 6c        Return val
15aa2 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  ue.**   --------
15aa3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
15aa4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
15aa5 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20 20 20  **   0          
15aa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
15aa7 0a 2a 2a 20 20 20 35 31 32 20 20 20 20 20 20 20  .**   512       
15aa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15aa9 35 31 32 0a 2a 2a 20 20 20 31 30 30 20 20 20 20  512.**   100    
15aaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15aab 20 20 20 35 31 32 0a 2a 2a 20 20 20 32 30 30 30     512.**   2000
15aac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15aad 20 20 20 20 20 20 32 30 34 38 0a 2a 2a 20 0a 2a        2048.** .*
15aae 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6a 6f 75  /.static i64 jou
15aaf 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 50 61  rnalHdrOffset(Pa
15ab0 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
15ab1 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a  i64 offset = 0;.
15ab2 20 20 69 36 34 20 63 20 3d 20 70 50 61 67 65 72    i64 c = pPager
15ab3 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20  ->journalOff;.  
15ab4 69 66 28 20 63 20 29 7b 0a 20 20 20 20 6f 66 66  if( c ){.    off
15ab5 73 65 74 20 3d 20 28 28 63 2d 31 29 2f 4a 4f 55  set = ((c-1)/JOU
15ab6 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
15ab7 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f 55 52 4e  er) + 1) * JOURN
15ab8 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
15ab9 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  );.  }.  assert(
15aba 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f   offset%JOURNAL_
15abb 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d  HDR_SZ(pPager)==
15abc 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  0 );.  assert( o
15abd 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 20 61 73  ffset>=c );.  as
15abe 73 65 72 74 28 20 28 6f 66 66 73 65 74 2d 63 29  sert( (offset-c)
15abf 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  <JOURNAL_HDR_SZ(
15ac0 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74  pPager) );.  ret
15ac1 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d 0a 0a 2f  urn offset;.}../
15ac2 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c  *.** The journal
15ac3 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70   file must be op
15ac4 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  en when this fun
15ac5 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e  ction is called.
15ac6 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
15ac7 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 20  tion is a no-op 
15ac8 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  if the journal f
15ac9 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  ile has not been
15aca 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 77   written to.** w
15acb 69 74 68 69 6e 20 74 68 65 20 63 75 72 72 65 6e  ithin the curren
15acc 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 69  t transaction (i
15acd 2e 65 2e 20 69 66 20 50 61 67 65 72 2e 6a 6f 75  .e. if Pager.jou
15ace 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e 0a 2a 2a 0a  rnalOff==0)..**.
15acf 2a 2a 20 49 66 20 64 6f 54 72 75 6e 63 61 74 65  ** If doTruncate
15ad0 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 20   is non-zero or 
15ad1 74 68 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61  the Pager.journa
15ad2 6c 53 69 7a 65 4c 69 6d 69 74 20 76 61 72 69 61  lSizeLimit varia
15ad3 62 6c 65 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f  ble is.** set to
15ad4 20 30 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74   0, then truncat
15ad5 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  e the journal fi
15ad6 6c 65 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73  le to zero bytes
15ad7 20 69 6e 20 73 69 7a 65 2e 20 4f 74 68 65 72 77   in size. Otherw
15ad8 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f 20 74 68 65  ise,.** zero the
15ad9 20 32 38 2d 62 79 74 65 20 68 65 61 64 65 72 20   28-byte header 
15ada 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
15adb 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
15adc 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65  . In either case
15add 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20 70 61 67  , .** if the pag
15ade 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 6e 6f 2d  er is not in no-
15adf 73 79 6e 63 20 6d 6f 64 65 2c 20 73 79 6e 63 20  sync mode, sync 
15ae0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
15ae1 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a   immediately .**
15ae2 20 61 66 74 65 72 20 77 72 69 74 69 6e 67 20 6f   after writing o
15ae3 72 20 74 72 75 6e 63 61 74 69 6e 67 20 69 74 2e  r truncating it.
15ae4 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61 67 65 72 2e  .**.** If Pager.
15ae5 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74  journalSizeLimit
15ae6 20 69 73 20 73 65 74 20 74 6f 20 61 20 70 6f 73   is set to a pos
15ae7 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a 65 72 6f 20  itive, non-zero 
15ae8 76 61 6c 75 65 2c 20 61 6e 64 0a 2a 2a 20 66 6f  value, and.** fo
15ae9 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 72 75 6e  llowing the trun
15aea 63 61 74 69 6f 6e 20 6f 72 20 7a 65 72 6f 69 6e  cation or zeroin
15aeb 67 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76  g described abov
15aec 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  e the size of th
15aed 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  e .** journal fi
15aee 6c 65 20 69 6e 20 62 79 74 65 73 20 69 73 20 6c  le in bytes is l
15aef 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 20  arger than this 
15af0 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 72 75 6e  value, then trun
15af1 63 61 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 72  cate the.** jour
15af2 6e 61 6c 20 66 69 6c 65 20 74 6f 20 50 61 67 65  nal file to Page
15af3 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d  r.journalSizeLim
15af4 69 74 20 62 79 74 65 73 2e 20 54 68 65 20 6a 6f  it bytes. The jo
15af5 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 0a  urnal file does.
15af6 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  ** not need to b
15af7 65 20 73 79 6e 63 65 64 20 66 6f 6c 6c 6f 77 69  e synced followi
15af8 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f  ng this operatio
15af9 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49  n..**.** If an I
15afa 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  O error occurs, 
15afb 61 62 61 6e 64 6f 6e 20 70 72 6f 63 65 73 73 69  abandon processi
15afc 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68  ng and return th
15afd 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 2e  e IO error code.
15afe 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72  .** Otherwise, r
15aff 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
15b00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 7a  .*/.static int z
15b01 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 50 61  eroJournalHdr(Pa
15b02 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
15b03 20 64 6f 54 72 75 6e 63 61 74 65 29 7b 0a 20 20   doTruncate){.  
15b04 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
15b05 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  OK;             
15b06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15b07 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
15b08 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73   */.  assert( is
15b09 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
15b0a 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  ) );.  if( pPage
15b0b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b  r->journalOff ){
15b0c 0a 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20 69  .    const i64 i
15b0d 4c 69 6d 69 74 20 3d 20 70 50 61 67 65 72 2d 3e  Limit = pPager->
15b0e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74  journalSizeLimit
15b0f 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61  ;    /* Local ca
15b10 63 68 65 20 6f 66 20 6a 73 6c 20 2a 2f 0a 0a 20  che of jsl */.. 
15b11 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 5a 45     IOTRACE(("JZE
15b12 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20 70 50 61  ROHDR %p\n", pPa
15b13 67 65 72 29 29 0a 20 20 20 20 69 66 28 20 64 6f  ger)).    if( do
15b14 54 72 75 6e 63 61 74 65 20 7c 7c 20 69 4c 69 6d  Truncate || iLim
15b15 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  it==0 ){.      r
15b16 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75  c = sqlite3OsTru
15b17 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66  ncate(pPager->jf
15b18 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  d, 0);.    }else
15b19 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 63  {.      static c
15b1a 6f 6e 73 74 20 63 68 61 72 20 7a 65 72 6f 48 64  onst char zeroHd
15b1b 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a 20 20 20  r[28] = {0};.   
15b1c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
15b1d 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a  sWrite(pPager->j
15b1e 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20 73 69 7a  fd, zeroHdr, siz
15b1f 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c 20 30 29  eof(zeroHdr), 0)
15b20 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
15b21 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
15b22 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63   !pPager->noSync
15b23 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
15b24 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61  qlite3OsSync(pPa
15b25 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 54 45  ger->jfd, SQLITE
15b26 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 7c 70  _SYNC_DATAONLY|p
15b27 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73  Pager->syncFlags
15b28 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
15b29 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74   At this point t
15b2a 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
15b2b 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 75 74 20  s committed but 
15b2c 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 0a  the write lock .
15b2d 20 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20      ** is still 
15b2e 68 65 6c 64 20 6f 6e 20 74 68 65 20 66 69 6c 65  held on the file
15b2f 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20  . If there is a 
15b30 73 69 7a 65 20 6c 69 6d 69 74 20 63 6f 6e 66 69  size limit confi
15b31 67 75 72 65 64 20 66 6f 72 20 0a 20 20 20 20 2a  gured for .    *
15b32 2a 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74  * the persistent
15b33 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65   journal and the
15b34 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 75   journal file cu
15b35 72 72 65 6e 74 6c 79 20 63 6f 6e 73 75 6d 65 73  rrently consumes
15b36 20 6d 6f 72 65 0a 20 20 20 20 2a 2a 20 73 70 61   more.    ** spa
15b37 63 65 20 74 68 61 6e 20 74 68 61 74 20 6c 69 6d  ce than that lim
15b38 69 74 20 61 6c 6c 6f 77 73 20 66 6f 72 2c 20 74  it allows for, t
15b39 72 75 6e 63 61 74 65 20 69 74 20 6e 6f 77 2e 20  runcate it now. 
15b3a 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  There is no need
15b3b 0a 20 20 20 20 2a 2a 20 74 6f 20 73 79 6e 63 20  .    ** to sync 
15b3c 74 68 65 20 66 69 6c 65 20 66 6f 6c 6c 6f 77 69  the file followi
15b3d 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f  ng this operatio
15b3e 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  n..    */.    if
15b3f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
15b40 26 26 20 69 4c 69 6d 69 74 3e 30 20 29 7b 0a 20  && iLimit>0 ){. 
15b41 20 20 20 20 20 69 36 34 20 73 7a 3b 0a 20 20 20       i64 sz;.   
15b42 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
15b43 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72  sFileSize(pPager
15b44 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b 0a 20 20 20  ->jfd, &sz);.   
15b45 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
15b46 45 5f 4f 4b 20 26 26 20 73 7a 3e 69 4c 69 6d 69  E_OK && sz>iLimi
15b47 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  t ){.        rc 
15b48 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  = sqlite3OsTrunc
15b49 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ate(pPager->jfd,
15b4a 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20   iLimit);.      
15b4b 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
15b4c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
15b4d 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * The journal fi
15b4e 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20  le must be open 
15b4f 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
15b50 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 20 6a  e is called. A j
15b51 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65 61 64 65 72  ournal.** header
15b52 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a   (JOURNAL_HDR_SZ
15b53 20 62 79 74 65 73 29 20 69 73 20 77 72 69 74 74   bytes) is writt
15b54 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72  en into the jour
15b55 6e 61 6c 20 66 69 6c 65 20 61 74 20 74 68 65 0a  nal file at the.
15b56 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74  ** current locat
15b57 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ion..**.** The f
15b58 6f 72 6d 61 74 20 66 6f 72 20 74 68 65 20 6a 6f  ormat for the jo
15b59 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20  urnal header is 
15b5a 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d  as follows:.** -
15b5b 20 38 20 62 79 74 65 73 3a 20 4d 61 67 69 63 20   8 bytes: Magic 
15b5c 69 64 65 6e 74 69 66 79 69 6e 67 20 6a 6f 75 72  identifying jour
15b5d 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 2d  nal format..** -
15b5e 20 34 20 62 79 74 65 73 3a 20 4e 75 6d 62 65 72   4 bytes: Number
15b5f 20 6f 66 20 72 65 63 6f 72 64 73 20 69 6e 20 6a   of records in j
15b60 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d 31 20 6e 6f  ournal, or -1 no
15b61 2d 73 79 6e 63 20 6d 6f 64 65 20 69 73 20 6f 6e  -sync mode is on
15b62 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20  ..** - 4 bytes: 
15b63 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 75 73  Random number us
15b64 65 64 20 66 6f 72 20 70 61 67 65 20 68 61 73 68  ed for page hash
15b65 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20  ..** - 4 bytes: 
15b66 49 6e 69 74 69 61 6c 20 64 61 74 61 62 61 73 65  Initial database
15b67 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2a 20   page count..** 
15b68 2d 20 34 20 62 79 74 65 73 3a 20 53 65 63 74 6f  - 4 bytes: Secto
15b69 72 20 73 69 7a 65 20 75 73 65 64 20 62 79 20 74  r size used by t
15b6a 68 65 20 70 72 6f 63 65 73 73 20 74 68 61 74 20  he process that 
15b6b 77 72 6f 74 65 20 74 68 69 73 20 6a 6f 75 72 6e  wrote this journ
15b6c 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73  al..** - 4 bytes
15b6d 3a 20 44 61 74 61 62 61 73 65 20 70 61 67 65 20  : Database page 
15b6e 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c  size..** .** Fol
15b6f 6c 6f 77 65 64 20 62 79 20 28 4a 4f 55 52 4e 41  lowed by (JOURNA
15b70 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29 20 62  L_HDR_SZ - 28) b
15b71 79 74 65 73 20 6f 66 20 75 6e 75 73 65 64 20 73  ytes of unused s
15b72 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  pace..*/.static 
15b73 69 6e 74 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c  int writeJournal
15b74 48 64 72 28 50 61 67 65 72 20 2a 70 50 61 67 65  Hdr(Pager *pPage
15b75 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  r){.  int rc = S
15b76 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
15b77 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
15b78 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 63 68  urn code */.  ch
15b79 61 72 20 2a 7a 48 65 61 64 65 72 20 3d 20 70 50  ar *zHeader = pP
15b7a 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b  ager->pTmpSpace;
15b7b 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 73    /* Temporary s
15b7c 70 61 63 65 20 75 73 65 64 20 74 6f 20 62 75 69  pace used to bui
15b7d 6c 64 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75  ld header */.  u
15b7e 33 32 20 6e 48 65 61 64 65 72 20 3d 20 28 75 33  32 nHeader = (u3
15b7f 32 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  2)pPager->pageSi
15b80 7a 65 3b 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75  ze;/* Size of bu
15b81 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20  ffer pointed to 
15b82 62 79 20 7a 48 65 61 64 65 72 20 2a 2f 0a 20 20  by zHeader */.  
15b83 75 33 32 20 6e 57 72 69 74 65 3b 20 20 20 20 20  u32 nWrite;     
15b84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15b85 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20      /* Bytes of 
15b86 68 65 61 64 65 72 20 73 65 63 74 6f 72 20 77 72  header sector wr
15b87 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  itten */.  int i
15b88 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
15b89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15b8a 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
15b8b 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  /..  assert( isO
15b8c 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
15b8d 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72   );      /* Jour
15b8e 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65  nal file must be
15b8f 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 69 66 28   open. */..  if(
15b90 20 6e 48 65 61 64 65 72 3e 4a 4f 55 52 4e 41 4c   nHeader>JOURNAL
15b91 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20  _HDR_SZ(pPager) 
15b92 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 3d  ){.    nHeader =
15b93 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28   JOURNAL_HDR_SZ(
15b94 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20  pPager);.  }..  
15b95 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
15b96 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74  active savepoint
15b97 73 20 61 6e 64 20 61 6e 79 20 6f 66 20 74 68 65  s and any of the
15b98 6d 20 77 65 72 65 20 63 72 65 61 74 65 64 20 0a  m were created .
15b99 20 20 2a 2a 20 73 69 6e 63 65 20 74 68 65 20 6d    ** since the m
15b9a 6f 73 74 20 72 65 63 65 6e 74 20 6a 6f 75 72 6e  ost recent journ
15b9b 61 6c 20 68 65 61 64 65 72 20 77 61 73 20 77 72  al header was wr
15b9c 69 74 74 65 6e 2c 20 75 70 64 61 74 65 20 74 68  itten, update th
15b9d 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 53 61 76  e .  ** PagerSav
15b9e 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65  epoint.iHdrOffse
15b9f 74 20 66 69 65 6c 64 73 20 6e 6f 77 2e 0a 20 20  t fields now..  
15ba0 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  */.  for(ii=0; i
15ba1 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70  i<pPager->nSavep
15ba2 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  oint; ii++){.   
15ba3 20 69 66 28 20 70 50 61 67 65 72 2d 3e 61 53 61   if( pPager->aSa
15ba4 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72  vepoint[ii].iHdr
15ba5 4f 66 66 73 65 74 3d 3d 30 20 29 7b 0a 20 20 20  Offset==0 ){.   
15ba6 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65     pPager->aSave
15ba7 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66  point[ii].iHdrOf
15ba8 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a  fset = pPager->j
15ba9 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 7d  ournalOff;.    }
15baa 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e  .  }..  pPager->
15bab 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70 50 61  journalHdr = pPa
15bac 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
15bad 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73  = journalHdrOffs
15bae 65 74 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 2f  et(pPager);..  /
15baf 2a 20 0a 20 20 2a 2a 20 57 72 69 74 65 20 74 68  * .  ** Write th
15bb0 65 20 6e 52 65 63 20 46 69 65 6c 64 20 2d 20 74  e nRec Field - t
15bb1 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
15bb2 65 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 66  e records that f
15bb3 6f 6c 6c 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20  ollow this.  ** 
15bb4 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20  journal header. 
15bb5 4e 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69  Normally, zero i
15bb6 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 69  s written to thi
15bb7 73 20 76 61 6c 75 65 20 61 74 20 74 68 69 73 20  s value at this 
15bb8 74 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72  time..  ** After
15bb9 20 74 68 65 20 72 65 63 6f 72 64 73 20 61 72 65   the records are
15bba 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6a 6f   added to the jo
15bbb 75 72 6e 61 6c 20 28 61 6e 64 20 74 68 65 20 6a  urnal (and the j
15bbc 6f 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a  ournal synced, .
15bbd 20 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d    ** if in full-
15bbe 73 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68 65 20  sync mode), the 
15bbf 7a 65 72 6f 20 69 73 20 6f 76 65 72 77 72 69 74  zero is overwrit
15bc0 74 65 6e 20 77 69 74 68 20 74 68 65 20 74 72 75  ten with the tru
15bc1 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66  e number.  ** of
15bc2 20 72 65 63 6f 72 64 73 20 28 73 65 65 20 73 79   records (see sy
15bc3 6e 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20  ncJournal())..  
15bc4 2a 2a 0a 20 20 2a 2a 20 41 20 66 61 73 74 65 72  **.  ** A faster
15bc5 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69 73 20   alternative is 
15bc6 74 6f 20 77 72 69 74 65 20 30 78 46 46 46 46 46  to write 0xFFFFF
15bc7 46 46 46 20 74 6f 20 74 68 65 20 6e 52 65 63 20  FFF to the nRec 
15bc8 66 69 65 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a  field. When.  **
15bc9 20 72 65 61 64 69 6e 67 20 74 68 65 20 6a 6f 75   reading the jou
15bca 72 6e 61 6c 20 74 68 69 73 20 76 61 6c 75 65 20  rnal this value 
15bcb 74 65 6c 6c 73 20 53 51 4c 69 74 65 20 74 6f 20  tells SQLite to 
15bcc 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 0a  assume that the.
15bcd 20 20 2a 2a 20 72 65 73 74 20 6f 66 20 74 68 65    ** rest of the
15bce 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f   journal file co
15bcf 6e 74 61 69 6e 73 20 76 61 6c 69 64 20 70 61 67  ntains valid pag
15bd0 65 20 72 65 63 6f 72 64 73 2e 20 54 68 69 73 20  e records. This 
15bd1 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20  assumption.  ** 
15bd2 69 73 20 64 61 6e 67 65 72 6f 75 73 2c 20 61 73  is dangerous, as
15bd3 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63   if a failure oc
15bd4 63 75 72 72 65 64 20 77 68 69 6c 73 74 20 77 72  curred whilst wr
15bd5 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75  iting to the jou
15bd6 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69  rnal.  ** file i
15bd7 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f  t may contain so
15bd8 6d 65 20 67 61 72 62 61 67 65 20 64 61 74 61 2e  me garbage data.
15bd9 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 20 73   There are two s
15bda 63 65 6e 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68  cenarios.  ** wh
15bdb 65 72 65 20 74 68 69 73 20 72 69 73 6b 20 63 61  ere this risk ca
15bdc 6e 20 62 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20  n be ignored:.  
15bdd 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e  **.  **   * When
15bde 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
15bdf 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43   no-sync mode. C
15be0 6f 72 72 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f  orruption can fo
15be1 6c 6c 6f 77 20 61 0a 20 20 2a 2a 20 20 20 20 20  llow a.  **     
15be2 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 69 6e  power failure in
15be3 20 74 68 69 73 20 63 61 73 65 20 61 6e 79 77 61   this case anywa
15be4 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a  y..  **.  **   *
15be5 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45   When the SQLITE
15be6 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45  _IOCAP_SAFE_APPE
15be7 4e 44 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20  ND flag is set. 
15be8 54 68 69 73 20 67 75 61 72 61 6e 74 65 65 73 0a  This guarantees.
15be9 20 20 2a 2a 20 20 20 20 20 74 68 61 74 20 67 61    **     that ga
15bea 72 62 61 67 65 20 64 61 74 61 20 69 73 20 6e 65  rbage data is ne
15beb 76 65 72 20 61 70 70 65 6e 64 65 64 20 74 6f 20  ver appended to 
15bec 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
15bed 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
15bee 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
15bef 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e  fd) || pPager->n
15bf0 6f 53 79 6e 63 20 29 3b 0a 20 20 69 66 28 20 70  oSync );.  if( p
15bf1 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 7c 7c  Pager->noSync ||
15bf2 20 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61   (pPager->journa
15bf3 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
15bf4 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29  RNALMODE_MEMORY)
15bf5 0a 20 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f  .   || (sqlite3O
15bf6 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  sDeviceCharacter
15bf7 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66  istics(pPager->f
15bf8 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  d)&SQLITE_IOCAP_
15bf9 53 41 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20  SAFE_APPEND) .  
15bfa 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 48  ){.    memcpy(zH
15bfb 65 61 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d  eader, aJournalM
15bfc 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f  agic, sizeof(aJo
15bfd 75 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20  urnalMagic));.  
15bfe 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65    put32bits(&zHe
15bff 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75  ader[sizeof(aJou
15c00 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 30 78 66  rnalMagic)], 0xf
15c01 66 66 66 66 66 66 66 29 3b 0a 20 20 7d 65 6c 73  fffffff);.  }els
15c02 65 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 7a 48  e{.    memset(zH
15c03 65 61 64 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66  eader, 0, sizeof
15c04 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b  (aJournalMagic)+
15c05 34 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  4);.  }..  /* Th
15c06 65 20 72 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68  e random check-h
15c07 61 73 68 20 69 6e 69 74 69 61 6c 69 73 65 72 20  ash initialiser 
15c08 2a 2f 20 0a 20 20 73 71 6c 69 74 65 33 5f 72 61  */ .  sqlite3_ra
15c09 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28  ndomness(sizeof(
15c0a 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69  pPager->cksumIni
15c0b 74 29 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73  t), &pPager->cks
15c0c 75 6d 49 6e 69 74 29 3b 0a 20 20 70 75 74 33 32  umInit);.  put32
15c0d 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69  bits(&zHeader[si
15c0e 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
15c0f 69 63 29 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e  ic)+4], pPager->
15c10 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a  cksumInit);.  /*
15c11 20 54 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74   The initial dat
15c12 61 62 61 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20  abase size */.  
15c13 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64  put32bits(&zHead
15c14 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  er[sizeof(aJourn
15c15 61 6c 4d 61 67 69 63 29 2b 38 5d 2c 20 70 50 61  alMagic)+8], pPa
15c16 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29  ger->dbOrigSize)
15c17 3b 0a 20 20 2f 2a 20 54 68 65 20 61 73 73 75 6d  ;.  /* The assum
15c18 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 66  ed sector size f
15c19 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  or this process 
15c1a 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26  */.  put32bits(&
15c1b 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
15c1c 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 32  JournalMagic)+12
15c1d 5d 2c 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f  ], pPager->secto
15c1e 72 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 54 68  rSize);..  /* Th
15c1f 65 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20  e page size */. 
15c20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61   put32bits(&zHea
15c21 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72  der[sizeof(aJour
15c22 6e 61 6c 4d 61 67 69 63 29 2b 31 36 5d 2c 20 70  nalMagic)+16], p
15c23 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29  Pager->pageSize)
15c24 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  ;..  /* Initiali
15c25 7a 69 6e 67 20 74 68 65 20 74 61 69 6c 20 6f 66  zing the tail of
15c26 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20 6e   the buffer is n
15c27 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 20 45  ot necessary.  E
15c28 76 65 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77  verything.  ** w
15c29 6f 72 6b 73 20 66 69 6e 64 20 69 66 20 74 68 65  orks find if the
15c2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 65 6d 73 65   following memse
15c2b 74 28 29 20 69 73 20 6f 6d 69 74 74 65 64 2e 20  t() is omitted. 
15c2c 20 42 75 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e   But initializin
15c2d 67 0a 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72  g.  ** the memor
15c2e 79 20 70 72 65 76 65 6e 74 73 20 76 61 6c 67 72  y prevents valgr
15c2f 69 6e 64 20 66 72 6f 6d 20 63 6f 6d 70 6c 61 69  ind from complai
15c30 6e 69 6e 67 2c 20 73 6f 20 77 65 20 61 72 65 20  ning, so we are 
15c31 77 69 6c 6c 69 6e 67 20 74 6f 0a 20 20 2a 2a 20  willing to.  ** 
15c32 74 61 6b 65 20 74 68 65 20 70 65 72 66 6f 72 6d  take the perform
15c33 61 6e 63 65 20 68 69 74 2e 0a 20 20 2a 2f 0a 20  ance hit..  */. 
15c34 20 6d 65 6d 73 65 74 28 26 7a 48 65 61 64 65 72   memset(&zHeader
15c35 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c  [sizeof(aJournal
15c36 4d 61 67 69 63 29 2b 32 30 5d 2c 20 30 2c 0a 20  Magic)+20], 0,. 
15c37 20 20 20 20 20 20 20 20 6e 48 65 61 64 65 72 2d          nHeader-
15c38 28 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c  (sizeof(aJournal
15c39 4d 61 67 69 63 29 2b 32 30 29 29 3b 0a 0a 20 20  Magic)+20));..  
15c3a 2f 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 69 74  /* In theory, it
15c3b 20 69 73 20 6f 6e 6c 79 20 6e 65 63 65 73 73 61   is only necessa
15c3c 72 79 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  ry to write the 
15c3d 32 38 20 62 79 74 65 73 20 74 68 61 74 20 74 68  28 bytes that th
15c3e 65 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20  e .  ** journal 
15c3f 68 65 61 64 65 72 20 63 6f 6e 73 75 6d 65 73 20  header consumes 
15c40 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
15c41 69 6c 65 20 68 65 72 65 2e 20 54 68 65 6e 20 69  ile here. Then i
15c42 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 20  ncrement the .  
15c43 2a 2a 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c  ** Pager.journal
15c44 4f 66 66 20 76 61 72 69 61 62 6c 65 20 62 79 20  Off variable by 
15c45 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 73  JOURNAL_HDR_SZ s
15c46 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 20  o that the next 
15c47 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 69 73 20  .  ** record is 
15c48 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66  written to the f
15c49 6f 6c 6c 6f 77 69 6e 67 20 73 65 63 74 6f 72 20  ollowing sector 
15c4a 28 6c 65 61 76 69 6e 67 20 61 20 67 61 70 20 69  (leaving a gap i
15c4b 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20  n the file.  ** 
15c4c 74 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6d 70  that will be imp
15c4d 6c 69 63 69 74 6c 79 20 66 69 6c 6c 65 64 20 69  licitly filled i
15c4e 6e 20 62 79 20 74 68 65 20 4f 53 29 2e 0a 20 20  n by the OS)..  
15c4f 2a 2a 0a 20 20 2a 2a 20 48 6f 77 65 76 65 72 20  **.  ** However 
15c50 69 74 20 68 61 73 20 62 65 65 6e 20 64 69 73 63  it has been disc
15c51 6f 76 65 72 65 64 20 74 68 61 74 20 6f 6e 20 73  overed that on s
15c52 6f 6d 65 20 73 79 73 74 65 6d 73 20 74 68 69 73  ome systems this
15c53 20 70 61 74 74 65 72 6e 20 63 61 6e 20 0a 20 20   pattern can .  
15c54 2a 2a 20 62 65 20 73 69 67 6e 69 66 69 63 61 6e  ** be significan
15c55 74 6c 79 20 73 6c 6f 77 65 72 20 74 68 61 6e 20  tly slower than 
15c56 63 6f 6e 74 69 67 75 6f 75 73 6c 79 20 77 72 69  contiguously wri
15c57 74 69 6e 67 20 64 61 74 61 20 74 6f 20 74 68 65  ting data to the
15c58 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 65 76 65 6e   file,.  ** even
15c59 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20 65   if that means e
15c5a 78 70 6c 69 63 69 74 6c 79 20 77 72 69 74 69 6e  xplicitly writin
15c5b 67 20 64 61 74 61 20 74 6f 20 74 68 65 20 62 6c  g data to the bl
15c5c 6f 63 6b 20 6f 66 20 0a 20 20 2a 2a 20 28 4a 4f  ock of .  ** (JO
15c5d 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32  URNAL_HDR_SZ - 2
15c5e 38 29 20 62 79 74 65 73 20 74 68 61 74 20 77 69  8) bytes that wi
15c5f 6c 6c 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 20  ll not be used. 
15c60 53 6f 20 74 68 61 74 20 69 73 20 77 68 61 74 0a  So that is what.
15c61 20 20 2a 2a 20 69 73 20 64 6f 6e 65 2e 20 0a 20    ** is done. . 
15c62 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6c 6f 6f   **.  ** The loo
15c63 70 20 69 73 20 72 65 71 75 69 72 65 64 20 68 65  p is required he
15c64 72 65 20 69 6e 20 63 61 73 65 20 74 68 65 20 73  re in case the s
15c65 65 63 74 6f 72 2d 73 69 7a 65 20 69 73 20 6c 61  ector-size is la
15c66 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20  rger than the . 
15c67 20 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67   ** database pag
15c68 65 20 73 69 7a 65 2e 20 53 69 6e 63 65 20 74 68  e size. Since th
15c69 65 20 7a 48 65 61 64 65 72 20 62 75 66 66 65 72  e zHeader buffer
15c6a 20 69 73 20 6f 6e 6c 79 20 50 61 67 65 72 2e 70   is only Pager.p
15c6b 61 67 65 53 69 7a 65 0a 20 20 2a 2a 20 62 79 74  ageSize.  ** byt
15c6c 65 73 20 69 6e 20 73 69 7a 65 2c 20 6d 6f 72 65  es in size, more
15c6d 20 74 68 61 6e 20 6f 6e 65 20 63 61 6c 6c 20 74   than one call t
15c6e 6f 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  o sqlite3OsWrite
15c6f 28 29 20 6d 61 79 20 62 65 20 72 65 71 75 69 72  () may be requir
15c70 65 64 0a 20 20 2a 2a 20 74 6f 20 70 6f 70 75 6c  ed.  ** to popul
15c71 61 74 65 20 74 68 65 20 65 6e 74 69 72 65 20 6a  ate the entire j
15c72 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65  ournal header se
15c73 63 74 6f 72 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f  ctor..  */ .  fo
15c74 72 28 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d  r(nWrite=0; rc==
15c75 53 51 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74  SQLITE_OK&&nWrit
15c76 65 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  e<JOURNAL_HDR_SZ
15c77 28 70 50 61 67 65 72 29 3b 20 6e 57 72 69 74 65  (pPager); nWrite
15c78 2b 3d 6e 48 65 61 64 65 72 29 7b 0a 20 20 20 20  +=nHeader){.    
15c79 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20 25  IOTRACE(("JHDR %
15c7a 70 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50  p %lld %d\n", pP
15c7b 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f  ager, pPager->jo
15c7c 75 72 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 65  urnalHdr, nHeade
15c7d 72 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  r)).    rc = sql
15c7e 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
15c7f 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72  er->jfd, zHeader
15c80 2c 20 6e 48 65 61 64 65 72 2c 20 70 50 61 67 65  , nHeader, pPage
15c81 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a  r->journalOff);.
15c82 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
15c83 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3c  er->journalHdr <
15c84 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
15c85 6c 4f 66 66 20 29 3b 0a 20 20 20 20 70 50 61 67  lOff );.    pPag
15c86 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b  er->journalOff +
15c87 3d 20 6e 48 65 61 64 65 72 3b 0a 20 20 7d 0a 0a  = nHeader;.  }..
15c88 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
15c89 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61  /*.** The journa
15c8a 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f  l file must be o
15c8b 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 69 73  pen when this is
15c8c 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e   called. A journ
15c8d 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 0a 2a  al header file.*
15c8e 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  * (JOURNAL_HDR_S
15c8f 5a 20 62 79 74 65 73 29 20 69 73 20 72 65 61 64  Z bytes) is read
15c90 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e   from the curren
15c91 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68  t location in th
15c92 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c  e journal.** fil
15c93 65 2e 20 54 68 65 20 63 75 72 72 65 6e 74 20 6c  e. The current l
15c94 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a  ocation in the j
15c95 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 67  ournal file is g
15c96 69 76 65 6e 20 62 79 0a 2a 2a 20 70 50 61 67 65  iven by.** pPage
15c97 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2e 20 53  r->journalOff. S
15c98 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76  ee comments abov
15c99 65 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65  e function write
15c9a 4a 6f 75 72 6e 61 6c 48 64 72 28 29 20 66 6f 72  JournalHdr() for
15c9b 0a 2a 2a 20 61 20 64 65 73 63 72 69 70 74 69 6f  .** a descriptio
15c9c 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n of the journal
15c9d 20 68 65 61 64 65 72 20 66 6f 72 6d 61 74 2e 0a   header format..
15c9e 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 68 65 61  **.** If the hea
15c9f 64 65 72 20 69 73 20 72 65 61 64 20 73 75 63 63  der is read succ
15ca0 65 73 73 66 75 6c 6c 79 2c 20 2a 70 4e 52 65 63  essfully, *pNRec
15ca1 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e   is set to the n
15ca2 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 70 61 67 65  umber of.** page
15ca3 20 72 65 63 6f 72 64 73 20 66 6f 6c 6c 6f 77 69   records followi
15ca4 6e 67 20 74 68 69 73 20 68 65 61 64 65 72 20 61  ng this header a
15ca5 6e 64 20 2a 70 44 62 53 69 7a 65 20 69 73 20 73  nd *pDbSize is s
15ca6 65 74 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f  et to the size o
15ca7 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  f the.** databas
15ca8 65 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 61  e before the tra
15ca9 6e 73 61 63 74 69 6f 6e 20 62 65 67 61 6e 2c 20  nsaction began, 
15caa 69 6e 20 70 61 67 65 73 2e 20 41 6c 73 6f 2c 20  in pages. Also, 
15cab 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69  pPager->cksumIni
15cac 74 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74  t.** is set to t
15cad 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72  he value read fr
15cae 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  om the journal h
15caf 65 61 64 65 72 2e 20 53 51 4c 49 54 45 5f 4f 4b  eader. SQLITE_OK
15cb0 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20   is returned.** 
15cb1 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a  in this case..**
15cb2 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e  .** If the journ
15cb3 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 20 61  al header file a
15cb4 70 70 65 61 72 73 20 74 6f 20 62 65 20 63 6f 72  ppears to be cor
15cb5 72 75 70 74 65 64 2c 20 53 51 4c 49 54 45 5f 44  rupted, SQLITE_D
15cb6 4f 4e 45 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e  ONE is.** return
15cb7 65 64 20 61 6e 64 20 2a 70 4e 52 65 63 20 61 6e  ed and *pNRec an
15cb8 64 20 2a 50 44 62 53 69 7a 65 20 61 72 65 20 75  d *PDbSize are u
15cb9 6e 64 65 66 69 6e 65 64 2e 20 20 49 66 20 4a 4f  ndefined.  If JO
15cba 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74  URNAL_HDR_SZ byt
15cbb 65 73 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20  es.** cannot be 
15cbc 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f  read from the jo
15cbd 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 20 65 72  urnal file an er
15cbe 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
15cbf 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rned..*/.static 
15cc0 69 6e 74 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48  int readJournalH
15cc1 64 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61  dr(.  Pager *pPa
15cc2 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  ger,            
15cc3 20 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65     /* Pager obje
15cc4 63 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 48 6f  ct */.  int isHo
15cc5 74 2c 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c  t,.  i64 journal
15cc6 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20  Size,           
15cc7 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
15cc8 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69   open journal fi
15cc9 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  le in bytes */. 
15cca 20 75 33 32 20 2a 70 4e 52 65 63 2c 20 20 20 20   u32 *pNRec,    
15ccb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15ccc 20 4f 55 54 3a 20 56 61 6c 75 65 20 72 65 61 64   OUT: Value read
15ccd 20 66 72 6f 6d 20 74 68 65 20 6e 52 65 63 20 66   from the nRec f
15cce 69 65 6c 64 20 2a 2f 0a 20 20 75 33 32 20 2a 70  ield */.  u32 *p
15ccf 44 62 53 69 7a 65 20 20 20 20 20 20 20 20 20 20  DbSize          
15cd0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56         /* OUT: V
15cd1 61 6c 75 65 20 6f 66 20 6f 72 69 67 69 6e 61 6c  alue of original
15cd2 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 66   database size f
15cd3 69 65 6c 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ield */.){.  int
15cd4 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
15cd5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
15cd6 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 6e  urn code */.  un
15cd7 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67  signed char aMag
15cd8 69 63 5b 38 5d 3b 20 20 20 20 20 2f 2a 20 41 20  ic[8];     /* A 
15cd9 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74  buffer to hold t
15cda 68 65 20 6d 61 67 69 63 20 68 65 61 64 65 72 20  he magic header 
15cdb 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66  */.  i64 iHdrOff
15cdc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
15cdd 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 6a    /* Offset of j
15cde 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 62 65  ournal header be
15cdf 69 6e 67 20 72 65 61 64 20 2a 2f 0a 0a 20 20 61  ing read */..  a
15ce0 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
15ce1 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20 20  ager->jfd) );   
15ce2 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66 69     /* Journal fi
15ce3 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e  le must be open.
15ce4 20 2a 2f 0a 0a 20 20 2f 2a 20 41 64 76 61 6e 63   */..  /* Advanc
15ce5 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f  e Pager.journalO
15ce6 66 66 20 74 6f 20 74 68 65 20 73 74 61 72 74 20  ff to the start 
15ce7 6f 66 20 74 68 65 20 6e 65 78 74 20 73 65 63 74  of the next sect
15ce8 6f 72 2e 20 49 66 20 74 68 65 0a 20 20 2a 2a 20  or. If the.  ** 
15ce9 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
15cea 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20 74 68  too small for th
15ceb 65 72 65 20 74 6f 20 62 65 20 61 20 68 65 61 64  ere to be a head
15cec 65 72 20 73 74 6f 72 65 64 20 61 74 20 74 68 69  er stored at thi
15ced 73 0a 20 20 2a 2a 20 70 6f 69 6e 74 2c 20 72 65  s.  ** point, re
15cee 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
15cef 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d  ..  */.  pPager-
15cf0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f  >journalOff = jo
15cf1 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70  urnalHdrOffset(p
15cf2 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 70 50  Pager);.  if( pP
15cf3 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
15cf4 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  +JOURNAL_HDR_SZ(
15cf5 70 50 61 67 65 72 29 20 3e 20 6a 6f 75 72 6e 61  pPager) > journa
15cf6 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 65 74  lSize ){.    ret
15cf7 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
15cf8 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66 20 3d  .  }.  iHdrOff =
15cf9 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
15cfa 4f 66 66 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20  Off;..  /* Read 
15cfb 69 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 62  in the first 8 b
15cfc 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 72  ytes of the jour
15cfd 6e 61 6c 20 68 65 61 64 65 72 2e 20 49 66 20 74  nal header. If t
15cfe 68 65 79 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68  hey do not match
15cff 0a 20 20 2a 2a 20 74 68 65 20 20 6d 61 67 69 63  .  ** the  magic
15d00 20 73 74 72 69 6e 67 20 66 6f 75 6e 64 20 61 74   string found at
15d01 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 61   the start of ea
15d02 63 68 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  ch journal heade
15d03 72 2c 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 53  r, return.  ** S
15d04 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 49 66 20 61  QLITE_DONE. If a
15d05 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
15d06 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  s, return an err
15d07 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 72 77 69  or code. Otherwi
15d08 73 65 2c 0a 20 20 2a 2a 20 70 72 6f 63 65 65 64  se,.  ** proceed
15d09 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 48  ..  */.  if( isH
15d0a 6f 74 20 7c 7c 20 69 48 64 72 4f 66 66 21 3d 70  ot || iHdrOff!=p
15d0b 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
15d0c 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  r ){.    rc = sq
15d0d 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67  lite3OsRead(pPag
15d0e 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c  er->jfd, aMagic,
15d0f 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 2c   sizeof(aMagic),
15d10 20 69 48 64 72 4f 66 66 29 3b 0a 20 20 20 20 69   iHdrOff);.    i
15d11 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72  f( rc ){.      r
15d12 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
15d13 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 61      if( memcmp(a
15d14 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d  Magic, aJournalM
15d15 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d 61  agic, sizeof(aMa
15d16 67 69 63 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  gic))!=0 ){.    
15d17 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15d18 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  DONE;.    }.  }.
15d19 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 66  .  /* Read the f
15d1a 69 72 73 74 20 74 68 72 65 65 20 33 32 2d 62 69  irst three 32-bi
15d1b 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  t fields of the 
15d1c 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 3a 20  journal header: 
15d1d 54 68 65 20 6e 52 65 63 0a 20 20 2a 2a 20 66 69  The nRec.  ** fi
15d1e 65 6c 64 2c 20 74 68 65 20 63 68 65 63 6b 73 75  eld, the checksu
15d1f 6d 2d 69 6e 69 74 69 61 6c 69 7a 65 72 20 61 6e  m-initializer an
15d20 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
15d21 69 7a 65 20 61 74 20 74 68 65 20 73 74 61 72 74  ize at the start
15d22 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 72 61  .  ** of the tra
15d23 6e 73 61 63 74 69 6f 6e 2e 20 52 65 74 75 72 6e  nsaction. Return
15d24 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
15d25 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20  f anything goes 
15d26 77 72 6f 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66  wrong..  */.  if
15d27 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
15d28 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50   = read32bits(pP
15d29 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f  ager->jfd, iHdrO
15d2a 66 66 2b 38 2c 20 70 4e 52 65 63 29 29 0a 20 20  ff+8, pNRec)).  
15d2b 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
15d2c 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
15d2d 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64  pPager->jfd, iHd
15d2e 72 4f 66 66 2b 31 32 2c 20 26 70 50 61 67 65 72  rOff+12, &pPager
15d2f 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 29 0a 20 20  ->cksumInit)).  
15d30 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
15d31 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
15d32 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64  pPager->jfd, iHd
15d33 72 4f 66 66 2b 31 36 2c 20 70 44 62 53 69 7a 65  rOff+16, pDbSize
15d34 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75  )).  ){.    retu
15d35 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66  rn rc;.  }..  if
15d36 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
15d37 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 75  lOff==0 ){.    u
15d38 33 32 20 69 50 61 67 65 53 69 7a 65 3b 20 20 20  32 iPageSize;   
15d39 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
15d3a 61 67 65 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f  age-size field o
15d3b 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  f journal header
15d3c 20 2a 2f 0a 20 20 20 20 75 33 32 20 69 53 65 63   */.    u32 iSec
15d3d 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 20  torSize;        
15d3e 20 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 2d 73       /* Sector-s
15d3f 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f 75  ize field of jou
15d40 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 0a  rnal header */..
15d41 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20      /* Read the 
15d42 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65  page-size and se
15d43 63 74 6f 72 2d 73 69 7a 65 20 6a 6f 75 72 6e 61  ctor-size journa
15d44 6c 20 68 65 61 64 65 72 20 66 69 65 6c 64 73 2e  l header fields.
15d45 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c 49   */.    if( SQLI
15d46 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61  TE_OK!=(rc = rea
15d47 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e  d32bits(pPager->
15d48 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 30 2c  jfd, iHdrOff+20,
15d49 20 26 69 53 65 63 74 6f 72 53 69 7a 65 29 29 0a   &iSectorSize)).
15d4a 20 20 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f       || SQLITE_O
15d4b 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62  K!=(rc = read32b
15d4c 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  its(pPager->jfd,
15d4d 20 69 48 64 72 4f 66 66 2b 32 34 2c 20 26 69 50   iHdrOff+24, &iP
15d4e 61 67 65 53 69 7a 65 29 29 0a 20 20 20 20 29 7b  ageSize)).    ){
15d4f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
15d50 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
15d51 56 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  Versions of SQLi
15d52 74 65 20 70 72 69 6f 72 20 74 6f 20 33 2e 35 2e  te prior to 3.5.
15d53 38 20 73 65 74 20 74 68 65 20 70 61 67 65 2d 73  8 set the page-s
15d54 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 74 68 65  ize field of the
15d55 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20  .    ** journal 
15d56 68 65 61 64 65 72 20 74 6f 20 7a 65 72 6f 2e 20  header to zero. 
15d57 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 61 73  In this case, as
15d58 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 50 61  sume that the Pa
15d59 67 65 72 2e 70 61 67 65 53 69 7a 65 0a 20 20 20  ger.pageSize.   
15d5a 20 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 73 20   ** variable is 
15d5b 61 6c 72 65 61 64 79 20 73 65 74 20 74 6f 20 74  already set to t
15d5c 68 65 20 63 6f 72 72 65 63 74 20 70 61 67 65 20  he correct page 
15d5d 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  size..    */.   
15d5e 20 69 66 28 20 69 50 61 67 65 53 69 7a 65 3d 3d   if( iPageSize==
15d5f 30 20 29 7b 0a 20 20 20 20 20 20 69 50 61 67 65  0 ){.      iPage
15d60 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 70  Size = pPager->p
15d61 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 0a  ageSize;.    }..
15d62 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61      /* Check tha
15d63 74 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 61  t the values rea
15d64 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 2d  d from the page-
15d65 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 2d  size and sector-
15d66 73 69 7a 65 20 66 69 65 6c 64 73 0a 20 20 20 20  size fields.    
15d67 2a 2a 20 61 72 65 20 77 69 74 68 69 6e 20 72 61  ** are within ra
15d68 6e 67 65 2e 20 54 6f 20 62 65 20 27 69 6e 20 72  nge. To be 'in r
15d69 61 6e 67 65 27 2c 20 62 6f 74 68 20 76 61 6c 75  ange', both valu
15d6a 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 61 20  es need to be a 
15d6b 70 6f 77 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20  power.    ** of 
15d6c 74 77 6f 20 67 72 65 61 74 65 72 20 74 68 61 6e  two greater than
15d6d 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 35 31 32   or equal to 512
15d6e 20 6f 72 20 33 32 2c 20 61 6e 64 20 6e 6f 74 20   or 32, and not 
15d6f 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65  greater than the
15d70 69 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70 65  ir .    ** respe
15d71 63 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74 69  ctive compile ti
15d72 6d 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69 74  me maximum limit
15d73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  s..    */.    if
15d74 28 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32 20  ( iPageSize<512 
15d75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d76 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3c   || iSectorSize<
15d77 33 32 0a 20 20 20 20 20 7c 7c 20 69 50 61 67 65  32.     || iPage
15d78 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  Size>SQLITE_MAX_
15d79 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69 53 65  PAGE_SIZE || iSe
15d7a 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 43  ctorSize>MAX_SEC
15d7b 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20 7c 7c  TOR_SIZE.     ||
15d7c 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29 26   ((iPageSize-1)&
15d7d 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20 20 20  iPageSize)!=0   
15d7e 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69 7a 65  || ((iSectorSize
15d7f 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a 65 29  -1)&iSectorSize)
15d80 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20  !=0 .    ){.    
15d81 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69 74 68    /* If the eith
15d82 65 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  er the page-size
15d83 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a 65 20   or sector-size 
15d84 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68  in the journal-h
15d85 65 61 64 65 72 20 69 73 20 0a 20 20 20 20 20 20  eader is .      
15d86 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68 65 6e  ** invalid, then
15d87 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 61   the process tha
15d88 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f 75 72  t wrote the jour
15d89 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73 74 20  nal-header must 
15d8a 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a 20 63  have .      ** c
15d8b 72 61 73 68 65 64 20 62 65 66 6f 72 65 20 74 68  rashed before th
15d8c 65 20 68 65 61 64 65 72 20 77 61 73 20 73 79 6e  e header was syn
15d8d 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ced. In this cas
15d8e 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67 20 0a  e stop reading .
15d8f 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75        ** the jou
15d90 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 0a  rnal file here..
15d91 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72        */.      r
15d92 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
15d93 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  E;.    }..    /*
15d94 20 55 70 64 61 74 65 20 74 68 65 20 70 61 67 65   Update the page
15d95 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74  -size to match t
15d96 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72  he value read fr
15d97 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20  om the journal. 
15d98 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20 74 65  .    ** Use a te
15d99 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 74  stcase() macro t
15d9a 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
15d9b 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20   malloc failure 
15d9c 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20 50  within .    ** P
15d9d 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28  agerSetPagesize(
15d9e 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20 20 20  ) is tested..   
15d9f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
15da0 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65  ite3PagerSetPage
15da1 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26 69 50  size(pPager, &iP
15da2 61 67 65 53 69 7a 65 2c 20 2d 31 29 3b 0a 20 20  ageSize, -1);.  
15da3 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d    testcase( rc!=
15da4 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20  SQLITE_OK );..  
15da5 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
15da6 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 2d 73  assumed sector-s
15da7 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65  ize to match the
15da8 20 76 61 6c 75 65 20 75 73 65 64 20 62 79 20 0a   value used by .
15da9 20 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65      ** the proce
15daa 73 73 20 74 68 61 74 20 63 72 65 61 74 65 64 20  ss that created 
15dab 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 66  this journal. If
15dac 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61   this journal wa
15dad 73 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64  s.    ** created
15dae 20 62 79 20 61 20 70 72 6f 63 65 73 73 20 6f 74   by a process ot
15daf 68 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e  her than this on
15db0 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  e, then this rou
15db1 74 69 6e 65 0a 20 20 20 20 2a 2a 20 69 73 20 62  tine.    ** is b
15db2 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d  eing called from
15db3 20 77 69 74 68 69 6e 20 70 61 67 65 72 5f 70 6c   within pager_pl
15db4 61 79 62 61 63 6b 28 29 2e 20 54 68 65 20 6c 6f  ayback(). The lo
15db5 63 61 6c 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a  cal value.    **
15db6 20 6f 66 20 50 61 67 65 72 2e 73 65 63 74 6f 72   of Pager.sector
15db7 53 69 7a 65 20 69 73 20 72 65 73 74 6f 72 65 64  Size is restored
15db8 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
15db9 68 61 74 20 72 6f 75 74 69 6e 65 2e 0a 20 20 20  hat routine..   
15dba 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   */.    pPager->
15dbb 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 69 53 65  sectorSize = iSe
15dbc 63 74 6f 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20  ctorSize;.  }.. 
15dbd 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
15dbe 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48  Off += JOURNAL_H
15dbf 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20  DR_SZ(pPager);. 
15dc0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
15dc1 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  /*.** Write the 
15dc2 73 75 70 70 6c 69 65 64 20 6d 61 73 74 65 72 20  supplied master 
15dc3 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74  journal name int
15dc4 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
15dc5 6c 65 20 66 6f 72 20 70 61 67 65 72 0a 2a 2a 20  le for pager.** 
15dc6 70 50 61 67 65 72 20 61 74 20 74 68 65 20 63 75  pPager at the cu
15dc7 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 20  rrent location. 
15dc8 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  The master journ
15dc9 61 6c 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20  al name must be 
15dca 74 68 65 20 6c 61 73 74 0a 2a 2a 20 74 68 69 6e  the last.** thin
15dcb 67 20 77 72 69 74 74 65 6e 20 74 6f 20 61 20 6a  g written to a j
15dcc 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20  ournal file. If 
15dcd 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
15dce 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20  full-sync mode, 
15dcf 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  the.** journal f
15dd0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69  ile descriptor i
15dd1 73 20 61 64 76 61 6e 63 65 64 20 74 6f 20 74 68  s advanced to th
15dd2 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20 62 6f  e next sector bo
15dd3 75 6e 64 61 72 79 20 62 65 66 6f 72 65 0a 2a 2a  undary before.**
15dd4 20 61 6e 79 74 68 69 6e 67 20 69 73 20 77 72 69   anything is wri
15dd5 74 74 65 6e 2e 20 54 68 65 20 66 6f 72 6d 61 74  tten. The format
15dd6 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 34   is:.**.**   + 4
15dd7 20 62 79 74 65 73 3a 20 50 41 47 45 52 5f 4d 4a   bytes: PAGER_MJ
15dd8 5f 50 47 4e 4f 2e 0a 2a 2a 20 20 20 2b 20 4e 20  _PGNO..**   + N 
15dd9 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f  bytes: Master jo
15dda 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69  urnal filename i
15ddb 6e 20 75 74 66 2d 38 2e 0a 2a 2a 20 20 20 2b 20  n utf-8..**   + 
15ddc 34 20 62 79 74 65 73 3a 20 4e 20 28 6c 65 6e 67  4 bytes: N (leng
15ddd 74 68 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75  th of master jou
15dde 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 20 62 79 74  rnal name in byt
15ddf 65 73 2c 20 6e 6f 20 6e 75 6c 2d 74 65 72 6d 69  es, no nul-termi
15de0 6e 61 74 6f 72 29 2e 0a 2a 2a 20 20 20 2b 20 34  nator)..**   + 4
15de1 20 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a   bytes: Master j
15de2 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63  ournal name chec
15de3 6b 73 75 6d 2e 0a 2a 2a 20 20 20 2b 20 38 20 62  ksum..**   + 8 b
15de4 79 74 65 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d 61  ytes: aJournalMa
15de5 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  gic[]..**.** The
15de6 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
15de7 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 69 73  page checksum is
15de8 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20   the sum of the 
15de9 62 79 74 65 73 20 69 6e 20 74 68 65 20 6d 61 73  bytes in the mas
15dea 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6e  ter.** journal n
15deb 61 6d 65 2c 20 77 68 65 72 65 20 65 61 63 68 20  ame, where each 
15dec 62 79 74 65 20 69 73 20 69 6e 74 65 72 70 72 65  byte is interpre
15ded 74 65 64 20 61 73 20 61 20 73 69 67 6e 65 64 20  ted as a signed 
15dee 38 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a  8-bit integer..*
15def 2a 0a 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72 20  *.** If zMaster 
15df0 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  is a NULL pointe
15df1 72 20 28 6f 63 63 75 72 73 20 66 6f 72 20 61 20  r (occurs for a 
15df2 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20  single database 
15df3 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a 2a  transaction), .*
15df4 2a 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61  * this call is a
15df5 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69   no-op..*/.stati
15df6 63 20 69 6e 74 20 77 72 69 74 65 4d 61 73 74 65  c int writeMaste
15df7 72 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a  rJournal(Pager *
15df8 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68  pPager, const ch
15df9 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20  ar *zMaster){.  
15dfa 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
15dfb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15dfc 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
15dfd 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 73 74 65 72  */.  int nMaster
15dfe 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
15dff 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
15e00 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 65  of string zMaste
15e01 72 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f  r */.  i64 iHdrO
15e02 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ff;             
15e03 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
15e04 74 20 6f 66 20 68 65 61 64 65 72 20 69 6e 20 6a  t of header in j
15e05 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20  ournal file */. 
15e06 20 69 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 20 20   i64 jrnlSize;  
15e07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15e08 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6a 6f 75    /* Size of jou
15e09 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73  rnal file on dis
15e0a 6b 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d  k */.  u32 cksum
15e0b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
15e0c 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b          /* Check
15e0d 73 75 6d 20 6f 66 20 73 74 72 69 6e 67 20 7a 4d  sum of string zM
15e0e 61 73 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65  aster */..  asse
15e0f 72 74 28 20 70 50 61 67 65 72 2d 3e 73 65 74 4d  rt( pPager->setM
15e10 61 73 74 65 72 3d 3d 30 20 29 3b 0a 20 20 61 73  aster==0 );.  as
15e11 73 65 72 74 28 20 21 70 61 67 65 72 55 73 65 57  sert( !pagerUseW
15e12 61 6c 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20  al(pPager) );.. 
15e13 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 0a 20   if( !zMaster . 
15e14 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75    || pPager->jou
15e15 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
15e16 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
15e17 52 59 20 0a 20 20 20 7c 7c 20 70 50 61 67 65 72  RY .   || pPager
15e18 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
15e19 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
15e1a 5f 4f 46 46 20 0a 20 20 29 7b 0a 20 20 20 20 72  _OFF .  ){.    r
15e1b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
15e1c 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73  .  }.  pPager->s
15e1d 65 74 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20  etMaster = 1;.  
15e1e 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
15e1f 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20  Pager->jfd) );. 
15e20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
15e21 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3c 3d 20 70  >journalHdr <= p
15e22 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
15e23 66 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75  f );..  /* Calcu
15e24 6c 61 74 65 20 74 68 65 20 6c 65 6e 67 74 68 20  late the length 
15e25 69 6e 20 62 79 74 65 73 20 61 6e 64 20 74 68 65  in bytes and the
15e26 20 63 68 65 63 6b 73 75 6d 20 6f 66 20 7a 4d 61   checksum of zMa
15e27 73 74 65 72 20 2a 2f 0a 20 20 66 6f 72 28 6e 4d  ster */.  for(nM
15e28 61 73 74 65 72 3d 30 3b 20 7a 4d 61 73 74 65 72  aster=0; zMaster
15e29 5b 6e 4d 61 73 74 65 72 5d 3b 20 6e 4d 61 73 74  [nMaster]; nMast
15e2a 65 72 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d  er++){.    cksum
15e2b 20 2b 3d 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73   += zMaster[nMas
15e2c 74 65 72 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ter];.  }..  /* 
15e2d 49 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20  If in full-sync 
15e2e 6d 6f 64 65 2c 20 61 64 76 61 6e 63 65 20 74 6f  mode, advance to
15e2f 20 74 68 65 20 6e 65 78 74 20 64 69 73 6b 20 73   the next disk s
15e30 65 63 74 6f 72 20 62 65 66 6f 72 65 20 77 72 69  ector before wri
15e31 74 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61  ting.  ** the ma
15e32 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster journal nam
15e33 65 2e 20 54 68 69 73 20 69 73 20 69 6e 20 63 61  e. This is in ca
15e34 73 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  se the previous 
15e35 70 61 67 65 20 77 72 69 74 74 65 6e 20 74 6f 0a  page written to.
15e36 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c    ** the journal
15e37 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
15e38 6e 20 73 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20  n synced..  */. 
15e39 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c   if( pPager->ful
15e3a 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 70 50 61  lSync ){.    pPa
15e3b 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
15e3c 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73  = journalHdrOffs
15e3d 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a  et(pPager);.  }.
15e3e 20 20 69 48 64 72 4f 66 66 20 3d 20 70 50 61 67    iHdrOff = pPag
15e3f 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a  er->journalOff;.
15e40 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20  .  /* Write the 
15e41 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 64  master journal d
15e42 61 74 61 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ata to the end o
15e43 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
15e44 6c 65 2e 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65  le. If.  ** an e
15e45 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74  rror occurs, ret
15e46 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f  urn the error co
15e47 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  de to the caller
15e48 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 30 20  ..  */.  if( (0 
15e49 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33 32  != (rc = write32
15e4a 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
15e4b 2c 20 69 48 64 72 4f 66 66 2c 20 50 41 47 45 52  , iHdrOff, PAGER
15e4c 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29  _MJ_PGNO(pPager)
15e4d 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20  ))).   || (0 != 
15e4e 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57  (rc = sqlite3OsW
15e4f 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  rite(pPager->jfd
15e50 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74  , zMaster, nMast
15e51 65 72 2c 20 69 48 64 72 4f 66 66 2b 34 29 29 29  er, iHdrOff+4)))
15e52 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63  .   || (0 != (rc
15e53 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70   = write32bits(p
15e54 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72  Pager->jfd, iHdr
15e55 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2c 20 6e  Off+4+nMaster, n
15e56 4d 61 73 74 65 72 29 29 29 0a 20 20 20 7c 7c 20  Master))).   || 
15e57 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74  (0 != (rc = writ
15e58 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e  e32bits(pPager->
15e59 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e  jfd, iHdrOff+4+n
15e5a 4d 61 73 74 65 72 2b 34 2c 20 63 6b 73 75 6d 29  Master+4, cksum)
15e5b 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28  )).   || (0 != (
15e5c 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
15e5d 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ite(pPager->jfd,
15e5e 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20   aJournalMagic, 
15e5f 38 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61  8, iHdrOff+4+nMa
15e60 73 74 65 72 2b 38 29 29 29 0a 20 20 29 7b 0a 20  ster+8))).  ){. 
15e61 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
15e62 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  }.  pPager->jour
15e63 6e 61 6c 4f 66 66 20 2b 3d 20 28 6e 4d 61 73 74  nalOff += (nMast
15e64 65 72 2b 32 30 29 3b 0a 0a 20 20 2f 2a 20 49 66  er+20);..  /* If
15e65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
15e66 20 70 65 72 69 73 74 65 6e 74 2d 6a 6f 75 72 6e   peristent-journ
15e67 61 6c 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68  al mode, then th
15e68 65 20 70 68 79 73 69 63 61 6c 20 0a 20 20 2a 2a  e physical .  **
15e69 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 6d 61   journal-file ma
15e6a 79 20 65 78 74 65 6e 64 20 70 61 73 74 20 74 68  y extend past th
15e6b 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73  e end of the mas
15e6c 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter-journal name
15e6d 0a 20 20 2a 2a 20 61 6e 64 20 38 20 62 79 74 65  .  ** and 8 byte
15e6e 73 20 6f 66 20 6d 61 67 69 63 20 64 61 74 61 20  s of magic data 
15e6f 6a 75 73 74 20 77 72 69 74 74 65 6e 20 74 6f 20  just written to 
15e70 74 68 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69  the file. This i
15e71 73 20 0a 20 20 2a 2a 20 64 61 6e 67 65 72 6f 75  s .  ** dangerou
15e72 73 20 62 65 63 61 75 73 65 20 74 68 65 20 63 6f  s because the co
15e73 64 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61  de to rollback a
15e74 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c   hot-journal fil
15e75 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20  e.  ** will not 
15e76 62 65 20 61 62 6c 65 20 74 6f 20 66 69 6e 64 20  be able to find 
15e77 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e  the master-journ
15e78 61 6c 20 6e 61 6d 65 20 74 6f 20 64 65 74 65 72  al name to deter
15e79 6d 69 6e 65 20 0a 20 20 2a 2a 20 77 68 65 74 68  mine .  ** wheth
15e7a 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f  er or not the jo
15e7b 75 72 6e 61 6c 20 69 73 20 68 6f 74 2e 20 0a 20  urnal is hot. . 
15e7c 20 2a 2a 0a 20 20 2a 2a 20 45 61 73 69 65 73 74   **.  ** Easiest
15e7d 20 74 68 69 6e 67 20 74 6f 20 64 6f 20 69 6e 20   thing to do in 
15e7e 74 68 69 73 20 73 63 65 6e 61 72 69 6f 20 69 73  this scenario is
15e7f 20 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65   to truncate the
15e80 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66   journal .  ** f
15e81 69 6c 65 20 74 6f 20 74 68 65 20 72 65 71 75 69  ile to the requi
15e82 72 65 64 20 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a  red size..  */ .
15e83 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
15e84 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73  =(rc = sqlite3Os
15e85 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d  FileSize(pPager-
15e86 3e 6a 66 64 2c 20 26 6a 72 6e 6c 53 69 7a 65 29  >jfd, &jrnlSize)
15e87 29 0a 20 20 20 26 26 20 6a 72 6e 6c 53 69 7a 65  ).   && jrnlSize
15e88 3e 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  >pPager->journal
15e89 4f 66 66 0a 20 20 29 7b 0a 20 20 20 20 72 63 20  Off.  ){.    rc 
15e8a 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  = sqlite3OsTrunc
15e8b 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ate(pPager->jfd,
15e8c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
15e8d 4f 66 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Off);.  }.  retu
15e8e 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
15e8f 46 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74  Find a page in t
15e90 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69  he hash table gi
15e91 76 65 6e 20 69 74 73 20 70 61 67 65 20 6e 75 6d  ven its page num
15e92 62 65 72 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 61  ber. Return.** a
15e93 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
15e94 70 61 67 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20  page or NULL if 
15e95 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61  the requested pa
15e96 67 65 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 6c  ge is not .** al
15e97 72 65 61 64 79 20 69 6e 20 6d 65 6d 6f 72 79 2e  ready in memory.
15e98 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72  .*/.static PgHdr
15e99 20 2a 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 50   *pager_lookup(P
15e9a 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67  ager *pPager, Pg
15e9b 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64  no pgno){.  PgHd
15e9c 72 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  r *p;           
15e9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15e9e 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f   Return value */
15e9f 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74  ..  /* It is not
15ea0 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 20   possible for a 
15ea1 63 61 6c 6c 20 74 6f 20 50 63 61 63 68 65 46 65  call to PcacheFe
15ea2 74 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74  tch() with creat
15ea3 65 46 6c 61 67 3d 3d 30 20 74 6f 0a 20 20 2a 2a  eFlag==0 to.  **
15ea4 20 66 61 69 6c 2c 20 73 69 6e 63 65 20 6e 6f 20   fail, since no 
15ea5 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63  attempt to alloc
15ea6 61 74 65 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f  ate dynamic memo
15ea7 72 79 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 2e  ry will be made.
15ea8 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71  .  */.  (void)sq
15ea9 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68  lite3PcacheFetch
15eaa 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
15eab 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a  , pgno, 0, &p);.
15eac 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
15ead 2a 0a 2a 2a 20 44 69 73 63 61 72 64 20 74 68 65  *.** Discard the
15eae 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73   entire contents
15eaf 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72   of the in-memor
15eb0 79 20 70 61 67 65 2d 63 61 63 68 65 2e 0a 2a 2f  y page-cache..*/
15eb1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67  .static void pag
15eb2 65 72 5f 72 65 73 65 74 28 50 61 67 65 72 20 2a  er_reset(Pager *
15eb3 70 50 61 67 65 72 29 7b 0a 20 20 73 71 6c 69 74  pPager){.  sqlit
15eb4 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 28  e3BackupRestart(
15eb5 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 29  pPager->pBackup)
15eb6 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68  ;.  sqlite3Pcach
15eb7 65 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70  eClear(pPager->p
15eb8 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  PCache);.}../*.*
15eb9 2a 20 46 72 65 65 20 61 6c 6c 20 73 74 72 75 63  * Free all struc
15eba 74 75 72 65 73 20 69 6e 20 74 68 65 20 50 61 67  tures in the Pag
15ebb 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20  er.aSavepoint[] 
15ebc 61 72 72 61 79 20 61 6e 64 20 73 65 74 20 62 6f  array and set bo
15ebd 74 68 0a 2a 2a 20 50 61 67 65 72 2e 61 53 61 76  th.** Pager.aSav
15ebe 65 70 6f 69 6e 74 20 61 6e 64 20 50 61 67 65 72  epoint and Pager
15ebf 2e 6e 53 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a  .nSavepoint to z
15ec0 65 72 6f 2e 20 43 6c 6f 73 65 20 74 68 65 20 73  ero. Close the s
15ec1 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66  ub-journal.** if
15ec2 20 69 74 20 69 73 20 6f 70 65 6e 20 61 6e 64 20   it is open and 
15ec3 74 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74  the pager is not
15ec4 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f   in exclusive mo
15ec5 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  de..*/.static vo
15ec6 69 64 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76  id releaseAllSav
15ec7 65 70 6f 69 6e 74 73 28 50 61 67 65 72 20 2a 70  epoints(Pager *p
15ec8 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 69 69  Pager){.  int ii
15ec9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
15eca 2f 2a 20 49 74 65 72 61 74 6f 72 20 66 6f 72 20  /* Iterator for 
15ecb 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20  looping through 
15ecc 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74  Pager.aSavepoint
15ecd 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20   */.  for(ii=0; 
15ece 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65  ii<pPager->nSave
15ecf 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20  point; ii++){.  
15ed0 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44    sqlite3BitvecD
15ed1 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61  estroy(pPager->a
15ed2 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49  Savepoint[ii].pI
15ed3 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d  nSavepoint);.  }
15ed4 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e  .  if( !pPager->
15ed5 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c  exclusiveMode ||
15ed6 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75   sqlite3IsMemJou
15ed7 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 73 6a 66  rnal(pPager->sjf
15ed8 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  d) ){.    sqlite
15ed9 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
15eda 3e 73 6a 66 64 29 3b 0a 20 20 7d 0a 20 20 73 71  >sjfd);.  }.  sq
15edb 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65  lite3_free(pPage
15edc 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 29 3b 0a  r->aSavepoint);.
15edd 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70    pPager->aSavep
15ede 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67  oint = 0;.  pPag
15edf 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d  er->nSavepoint =
15ee0 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53   0;.  pPager->nS
15ee1 75 62 52 65 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  ubRec = 0;.}../*
15ee2 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 69 74 20  .** Set the bit 
15ee3 6e 75 6d 62 65 72 20 70 67 6e 6f 20 69 6e 20 74  number pgno in t
15ee4 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  he PagerSavepoin
15ee5 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 0a  t.pInSavepoint .
15ee6 2a 2a 20 62 69 74 76 65 63 73 20 6f 66 20 61 6c  ** bitvecs of al
15ee7 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74  l open savepoint
15ee8 73 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  s. Return SQLITE
15ee9 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75  _OK if successfu
15eea 6c 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e  l.** or SQLITE_N
15eeb 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63  OMEM if a malloc
15eec 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e   failure occurs.
15eed 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
15eee 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74  ddToSavepointBit
15eef 76 65 63 73 28 50 61 67 65 72 20 2a 70 50 61 67  vecs(Pager *pPag
15ef0 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  er, Pgno pgno){.
15ef1 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20    int ii;       
15ef2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
15ef3 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
15ef4 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
15ef5 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65  _OK;       /* Re
15ef6 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 0a 20 20  sult code */..  
15ef7 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61  for(ii=0; ii<pPa
15ef8 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b  ger->nSavepoint;
15ef9 20 69 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65   ii++){.    Page
15efa 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20  rSavepoint *p = 
15efb 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f  &pPager->aSavepo
15efc 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28  int[ii];.    if(
15efd 20 70 67 6e 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20   pgno<=p->nOrig 
15efe 29 7b 0a 20 20 20 20 20 20 72 63 20 7c 3d 20 73  ){.      rc |= s
15eff 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
15f00 70 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c  p->pInSavepoint,
15f01 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 74 65   pgno);.      te
15f02 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49  stcase( rc==SQLI
15f03 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20  TE_NOMEM );.    
15f04 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
15f05 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53  LITE_OK || rc==S
15f06 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
15f07 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
15f08 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
15f09 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
15f0a 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 70  a no-op if the p
15f0b 61 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c 75  ager is in exclu
15f0c 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 6e 6f  sive mode and no
15f0d 74 0a 2a 2a 20 69 6e 20 74 68 65 20 45 52 52 4f  t.** in the ERRO
15f0e 52 20 73 74 61 74 65 2e 20 4f 74 68 65 72 77 69  R state. Otherwi
15f0f 73 65 2c 20 69 74 20 73 77 69 74 63 68 65 73 20  se, it switches 
15f10 74 68 65 20 70 61 67 65 72 20 74 6f 20 50 41 47  the pager to PAG
15f11 45 52 5f 4f 50 45 4e 0a 2a 2a 20 73 74 61 74 65  ER_OPEN.** state
15f12 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  ..**.** If the p
15f13 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 65  ager is not in e
15f14 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20  xclusive-access 
15f15 6d 6f 64 65 2c 20 74 68 65 20 64 61 74 61 62 61  mode, the databa
15f16 73 65 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 6f  se file is.** co
15f17 6d 70 6c 65 74 65 6c 79 20 75 6e 6c 6f 63 6b 65  mpletely unlocke
15f18 64 2e 20 49 66 20 74 68 65 20 66 69 6c 65 20 69  d. If the file i
15f19 73 20 75 6e 6c 6f 63 6b 65 64 20 61 6e 64 20 74  s unlocked and t
15f1a 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64  he file-system d
15f1b 6f 65 73 0a 2a 2a 20 6e 6f 74 20 65 78 68 69 62  oes.** not exhib
15f1c 69 74 20 74 68 65 20 55 4e 44 45 4c 45 54 41 42  it the UNDELETAB
15f1d 4c 45 5f 57 48 45 4e 5f 4f 50 45 4e 20 70 72 6f  LE_WHEN_OPEN pro
15f1e 70 65 72 74 79 2c 20 74 68 65 20 6a 6f 75 72 6e  perty, the journ
15f1f 61 6c 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 6c  al file is.** cl
15f20 6f 73 65 64 20 28 69 66 20 69 74 20 69 73 20 6f  osed (if it is o
15f21 70 65 6e 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  pen)..**.** If t
15f22 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 45  he pager is in E
15f23 52 52 4f 52 20 73 74 61 74 65 20 77 68 65 6e 20  RROR state when 
15f24 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
15f25 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 0a 2a 2a   called, the .**
15f26 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
15f27 20 70 61 67 65 72 20 63 61 63 68 65 20 61 72 65   pager cache are
15f28 20 64 69 73 63 61 72 64 65 64 20 62 65 66 6f 72   discarded befor
15f29 65 20 73 77 69 74 63 68 69 6e 67 20 62 61 63 6b  e switching back
15f2a 20 74 6f 20 0a 2a 2a 20 74 68 65 20 4f 50 45 4e   to .** the OPEN
15f2b 20 73 74 61 74 65 2e 20 52 65 67 61 72 64 6c 65   state. Regardle
15f2c 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 74 68  ss of whether th
15f2d 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 65 78  e pager is in ex
15f2e 63 6c 75 73 69 76 65 2d 6d 6f 64 65 0a 2a 2a 20  clusive-mode.** 
15f2f 6f 72 20 6e 6f 74 2c 20 61 6e 79 20 6a 6f 75 72  or not, any jour
15f30 6e 61 6c 20 66 69 6c 65 20 6c 65 66 74 20 69 6e  nal file left in
15f31 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d   the file-system
15f32 20 77 69 6c 6c 20 62 65 20 74 72 65 61 74 65 64   will be treated
15f33 0a 2a 2a 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75  .** as a hot-jou
15f34 72 6e 61 6c 20 61 6e 64 20 72 6f 6c 6c 65 64 20  rnal and rolled 
15f35 62 61 63 6b 20 74 68 65 20 6e 65 78 74 20 74 69  back the next ti
15f36 6d 65 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61  me a read-transa
15f37 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 70 65 6e  ction.** is open
15f38 65 64 20 28 62 79 20 74 68 69 73 20 6f 72 20 62  ed (by this or b
15f39 79 20 61 6e 79 20 6f 74 68 65 72 20 63 6f 6e 6e  y any other conn
15f3a 65 63 74 69 6f 6e 29 2e 0a 2a 2f 0a 73 74 61 74  ection)..*/.stat
15f3b 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 75 6e  ic void pager_un
15f3c 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67  lock(Pager *pPag
15f3d 65 72 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20  er){..  assert( 
15f3e 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d  pPager->eState==
15f3f 50 41 47 45 52 5f 52 45 41 44 45 52 20 0a 20 20  PAGER_READER .  
15f40 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e       || pPager->
15f41 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50  eState==PAGER_OP
15f42 45 4e 20 0a 20 20 20 20 20 20 20 7c 7c 20 70 50  EN .       || pP
15f43 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41  ager->eState==PA
15f44 47 45 52 5f 45 52 52 4f 52 20 0a 20 20 29 3b 0a  GER_ERROR .  );.
15f45 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  .  sqlite3Bitvec
15f46 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e  Destroy(pPager->
15f47 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 70  pInJournal);.  p
15f48 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
15f49 6c 20 3d 20 30 3b 0a 20 20 72 65 6c 65 61 73 65  l = 0;.  release
15f4a 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50  AllSavepoints(pP
15f4b 61 67 65 72 29 3b 0a 0a 20 20 69 66 28 20 70 61  ager);..  if( pa
15f4c 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72  gerUseWal(pPager
15f4d 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ) ){.    assert(
15f4e 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d   !isOpen(pPager-
15f4f 3e 6a 66 64 29 20 29 3b 0a 20 20 20 20 73 71 6c  >jfd) );.    sql
15f50 69 74 65 33 57 61 6c 45 6e 64 52 65 61 64 54 72  ite3WalEndReadTr
15f51 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72  ansaction(pPager
15f52 2d 3e 70 57 61 6c 29 3b 0a 20 20 20 20 70 50 61  ->pWal);.    pPa
15f53 67 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41  ger->eState = PA
15f54 47 45 52 5f 4f 50 45 4e 3b 0a 20 20 7d 65 6c 73  GER_OPEN;.  }els
15f55 65 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65  e if( !pPager->e
15f56 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a  xclusiveMode ){.
15f57 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20      int rc;     
15f58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15f59 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20    /* Error code 
15f5a 72 65 74 75 72 6e 65 64 20 62 79 20 70 61 67 65  returned by page
15f5b 72 55 6e 6c 6f 63 6b 44 62 28 29 20 2a 2f 0a 20  rUnlockDb() */. 
15f5c 20 20 20 69 6e 74 20 69 44 63 20 3d 20 69 73 4f     int iDc = isO
15f5d 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 3f  pen(pPager->fd)?
15f5e 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43  sqlite3OsDeviceC
15f5f 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70  haracteristics(p
15f60 50 61 67 65 72 2d 3e 66 64 29 3a 30 3b 0a 0a 20  Pager->fd):0;.. 
15f61 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6f 70 65     /* If the ope
15f62 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73 75  rating system su
15f63 70 70 6f 72 74 20 64 65 6c 65 74 69 6f 6e 20 6f  pport deletion o
15f64 66 20 6f 70 65 6e 20 66 69 6c 65 73 2c 20 74 68  f open files, th
15f65 65 6e 0a 20 20 20 20 2a 2a 20 63 6c 6f 73 65 20  en.    ** close 
15f66 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
15f67 20 77 68 65 6e 20 64 72 6f 70 70 69 6e 67 20 74   when dropping t
15f68 68 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b  he database lock
15f69 2e 20 20 4f 74 68 65 72 77 69 73 65 0a 20 20 20  .  Otherwise.   
15f6a 20 2a 2a 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e   ** another conn
15f6b 65 63 74 69 6f 6e 20 77 69 74 68 20 6a 6f 75 72  ection with jour
15f6c 6e 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65 74 65 20  nal_mode=delete 
15f6d 6d 69 67 68 74 20 64 65 6c 65 74 65 20 74 68 65  might delete the
15f6e 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 6f 75 74   file.    ** out
15f6f 20 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a   from under us..
15f70 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
15f71 74 28 20 28 50 41 47 45 52 5f 4a 4f 55 52 4e 41  t( (PAGER_JOURNA
15f72 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 26  LMODE_MEMORY   &
15f73 20 35 29 21 3d 31 20 29 3b 0a 20 20 20 20 61 73   5)!=1 );.    as
15f74 73 65 72 74 28 20 28 50 41 47 45 52 5f 4a 4f 55  sert( (PAGER_JOU
15f75 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20  RNALMODE_OFF    
15f76 20 20 26 20 35 29 21 3d 31 20 29 3b 0a 20 20 20    & 5)!=1 );.   
15f77 20 61 73 73 65 72 74 28 20 28 50 41 47 45 52 5f   assert( (PAGER_
15f78 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20  JOURNALMODE_WAL 
15f79 20 20 20 20 20 26 20 35 29 21 3d 31 20 29 3b 0a       & 5)!=1 );.
15f7a 20 20 20 20 61 73 73 65 72 74 28 20 28 50 41 47      assert( (PAG
15f7b 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44  ER_JOURNALMODE_D
15f7c 45 4c 45 54 45 20 20 20 26 20 35 29 21 3d 31 20  ELETE   & 5)!=1 
15f7d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
15f7e 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
15f7f 45 5f 54 52 55 4e 43 41 54 45 20 26 20 35 29 3d  E_TRUNCATE & 5)=
15f80 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =1 );.    assert
15f81 28 20 28 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ( (PAGER_JOURNAL
15f82 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 20 26 20  MODE_PERSIST  & 
15f83 35 29 3d 3d 31 20 29 3b 0a 20 20 20 20 69 66 28  5)==1 );.    if(
15f84 20 30 3d 3d 28 69 44 63 20 26 20 53 51 4c 49 54   0==(iDc & SQLIT
15f85 45 5f 49 4f 43 41 50 5f 55 4e 44 45 4c 45 54 41  E_IOCAP_UNDELETA
15f86 42 4c 45 5f 57 48 45 4e 5f 4f 50 45 4e 29 0a 20  BLE_WHEN_OPEN). 
15f87 20 20 20 20 7c 7c 20 31 21 3d 28 70 50 61 67 65      || 1!=(pPage
15f88 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 26  r->journalMode &
15f89 20 35 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20   5).    ){.     
15f8a 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
15f8b 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20  pPager->jfd);.  
15f8c 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
15f8d 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 74  he pager is in t
15f8e 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 20 61  he ERROR state a
15f8f 6e 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 75  nd the call to u
15f90 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61  nlock the databa
15f91 73 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 66  se.    ** file f
15f92 61 69 6c 73 2c 20 73 65 74 20 74 68 65 20 63 75  ails, set the cu
15f93 72 72 65 6e 74 20 6c 6f 63 6b 20 74 6f 20 55 4e  rrent lock to UN
15f94 4b 4e 4f 57 4e 5f 4c 4f 43 4b 2e 20 53 65 65 20  KNOWN_LOCK. See 
15f95 74 68 65 20 63 6f 6d 6d 65 6e 74 0a 20 20 20 20  the comment.    
15f96 2a 2a 20 61 62 6f 76 65 20 74 68 65 20 23 64 65  ** above the #de
15f97 66 69 6e 65 20 66 6f 72 20 55 4e 4b 4e 4f 57 4e  fine for UNKNOWN
15f98 5f 4c 4f 43 4b 20 66 6f 72 20 61 6e 20 65 78 70  _LOCK for an exp
15f99 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 77 68 79 20  lanation of why 
15f9a 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 6e  this.    ** is n
15f9b 65 63 65 73 73 61 72 79 2e 0a 20 20 20 20 2a 2f  ecessary..    */
15f9c 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 55  .    rc = pagerU
15f9d 6e 6c 6f 63 6b 44 62 28 70 50 61 67 65 72 2c 20  nlockDb(pPager, 
15f9e 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66  NO_LOCK);.    if
15f9f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
15fa0 26 26 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74  && pPager->eStat
15fa1 65 3d 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 29  e==PAGER_ERROR )
15fa2 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  {.      pPager->
15fa3 65 4c 6f 63 6b 20 3d 20 55 4e 4b 4e 4f 57 4e 5f  eLock = UNKNOWN_
15fa4 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  LOCK;.    }..   
15fa5 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 73 74   /* The pager st
15fa6 61 74 65 20 6d 61 79 20 62 65 20 63 68 61 6e 67  ate may be chang
15fa7 65 64 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 52  ed from PAGER_ER
15fa8 52 4f 52 20 74 6f 20 50 41 47 45 52 5f 4f 50 45  ROR to PAGER_OPE
15fa9 4e 20 68 65 72 65 0a 20 20 20 20 2a 2a 20 77 69  N here.    ** wi
15faa 74 68 6f 75 74 20 63 6c 65 61 72 69 6e 67 20 74  thout clearing t
15fab 68 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 54  he error code. T
15fac 68 69 73 20 69 73 20 69 6e 74 65 6e 74 69 6f 6e  his is intention
15fad 61 6c 20 2d 20 74 68 65 20 65 72 72 6f 72 0a 20  al - the error. 
15fae 20 20 20 2a 2a 20 63 6f 64 65 20 69 73 20 63 6c     ** code is cl
15faf 65 61 72 65 64 20 61 6e 64 20 74 68 65 20 63 61  eared and the ca
15fb0 63 68 65 20 72 65 73 65 74 20 69 6e 20 74 68 65  che reset in the
15fb1 20 62 6c 6f 63 6b 20 62 65 6c 6f 77 2e 0a 20 20   block below..  
15fb2 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
15fb3 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
15fb4 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 61   || pPager->eSta
15fb5 74 65 21 3d 50 41 47 45 52 5f 45 52 52 4f 52 20  te!=PAGER_ERROR 
15fb6 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63  );.    pPager->c
15fb7 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d  hangeCountDone =
15fb8 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
15fb9 65 53 74 61 74 65 20 3d 20 50 41 47 45 52 5f 4f  eState = PAGER_O
15fba 50 45 4e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  PEN;.  }..  /* I
15fbb 66 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20  f Pager.errCode 
15fbc 69 73 20 73 65 74 2c 20 74 68 65 20 63 6f 6e 74  is set, the cont
15fbd 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65  ents of the page
15fbe 72 20 63 61 63 68 65 20 63 61 6e 6e 6f 74 20 62  r cache cannot b
15fbf 65 0a 20 20 2a 2a 20 74 72 75 73 74 65 64 2e 20  e.  ** trusted. 
15fc0 4e 6f 77 20 74 68 61 74 20 74 68 65 72 65 20 61  Now that there a
15fc1 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e  re no outstandin
15fc2 67 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  g references to 
15fc3 74 68 65 20 70 61 67 65 72 2c 0a 20 20 2a 2a 20  the pager,.  ** 
15fc4 69 74 20 63 61 6e 20 73 61 66 65 6c 79 20 6d 6f  it can safely mo
15fc5 76 65 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52  ve back to PAGER
15fc6 5f 4f 50 45 4e 20 73 74 61 74 65 2e 20 54 68 69  _OPEN state. Thi
15fc7 73 20 68 61 70 70 65 6e 73 20 69 6e 20 62 6f 74  s happens in bot
15fc8 68 0a 20 20 2a 2a 20 6e 6f 72 6d 61 6c 20 61 6e  h.  ** normal an
15fc9 64 20 65 78 63 6c 75 73 69 76 65 2d 6c 6f 63 6b  d exclusive-lock
15fca 69 6e 67 20 6d 6f 64 65 2e 0a 20 20 2a 2f 0a 20  ing mode..  */. 
15fcb 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72   if( pPager->err
15fcc 43 6f 64 65 20 29 7b 0a 20 20 20 20 61 73 73 65  Code ){.    asse
15fcd 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20  rt( !MEMDB );.  
15fce 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50    pager_reset(pP
15fcf 61 67 65 72 29 3b 0a 20 20 20 20 70 50 61 67 65  ager);.    pPage
15fd0 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  r->changeCountDo
15fd1 6e 65 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d  ne = pPager->tem
15fd2 70 46 69 6c 65 3b 0a 20 20 20 20 70 50 61 67 65  pFile;.    pPage
15fd3 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41 47 45  r->eState = PAGE
15fd4 52 5f 4f 50 45 4e 3b 0a 20 20 20 20 70 50 61 67  R_OPEN;.    pPag
15fd5 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51  er->errCode = SQ
15fd6 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
15fd7 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
15fd8 66 66 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72  ff = 0;.  pPager
15fd9 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30  ->journalHdr = 0
15fda 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d  ;.  pPager->setM
15fdb 61 73 74 65 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  aster = 0;.}../*
15fdc 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
15fdd 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  n is called when
15fde 65 76 65 72 20 61 6e 20 49 4f 45 52 52 20 6f 72  ever an IOERR or
15fdf 20 46 55 4c 4c 20 65 72 72 6f 72 20 74 68 61 74   FULL error that
15fe0 20 72 65 71 75 69 72 65 73 0a 2a 2a 20 74 68 65   requires.** the
15fe1 20 70 61 67 65 72 20 74 6f 20 74 72 61 6e 73 69   pager to transi
15fe2 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 45 52  tion into the ER
15fe3 52 4f 52 20 73 74 61 74 65 20 6d 61 79 20 61 68  ROR state may ah
15fe4 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20  ve occurred..** 
15fe5 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
15fe6 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  nt is a pointer 
15fe7 74 6f 20 74 68 65 20 70 61 67 65 72 20 73 74 72  to the pager str
15fe8 75 63 74 75 72 65 2c 20 74 68 65 20 73 65 63 6f  ucture, the seco
15fe9 6e 64 20 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72  nd .** the error
15fea 2d 63 6f 64 65 20 61 62 6f 75 74 20 74 6f 20 62  -code about to b
15feb 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 20  e returned by a 
15fec 70 61 67 65 72 20 41 50 49 20 66 75 6e 63 74 69  pager API functi
15fed 6f 6e 2e 20 54 68 65 20 0a 2a 2a 20 76 61 6c 75  on. The .** valu
15fee 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 20  e returned is a 
15fef 63 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63 6f  copy of the seco
15ff0 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  nd argument to t
15ff1 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a  his function. .*
15ff2 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 63 6f  *.** If the seco
15ff3 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53  nd argument is S
15ff4 51 4c 49 54 45 5f 46 55 4c 4c 2c 20 53 51 4c 49  QLITE_FULL, SQLI
15ff5 54 45 5f 49 4f 45 52 52 20 6f 72 20 6f 6e 65 20  TE_IOERR or one 
15ff6 6f 66 20 74 68 65 0a 2a 2a 20 49 4f 45 52 52 20  of the.** IOERR 
15ff7 73 75 62 2d 63 6f 64 65 73 2c 20 74 68 65 20 70  sub-codes, the p
15ff8 61 67 65 72 20 65 6e 74 65 72 73 20 74 68 65 20  ager enters the 
15ff9 45 52 52 4f 52 20 73 74 61 74 65 20 61 6e 64 20  ERROR state and 
15ffa 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a  the error code.*
15ffb 2a 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 50  * is stored in P
15ffc 61 67 65 72 2e 65 72 72 43 6f 64 65 2e 20 57 68  ager.errCode. Wh
15ffd 69 6c 65 20 74 68 65 20 70 61 67 65 72 20 72 65  ile the pager re
15ffe 6d 61 69 6e 73 20 69 6e 20 74 68 65 20 45 52 52  mains in the ERR
15fff 4f 52 20 73 74 61 74 65 2c 0a 2a 2a 20 61 6c 6c  OR state,.** all
16000 20 6d 61 6a 6f 72 20 41 50 49 20 63 61 6c 6c 73   major API calls
16001 20 6f 6e 20 74 68 65 20 50 61 67 65 72 20 77 69   on the Pager wi
16002 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72  ll immediately r
16003 65 74 75 72 6e 20 50 61 67 65 72 2e 65 72 72 43  eturn Pager.errC
16004 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45  ode..**.** The E
16005 52 52 4f 52 20 73 74 61 74 65 20 69 6e 64 69 63  RROR state indic
16006 61 74 65 73 20 74 68 61 74 20 74 68 65 20 63 6f  ates that the co
16007 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61  ntents of the pa
16008 67 65 72 2d 63 61 63 68 65 20 0a 2a 2a 20 63 61  ger-cache .** ca
16009 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e  nnot be trusted.
1600a 20 54 68 69 73 20 73 74 61 74 65 20 63 61 6e 20   This state can 
1600b 62 65 20 63 6c 65 61 72 65 64 20 62 79 20 63 6f  be cleared by co
1600c 6d 70 6c 65 74 65 6c 79 20 64 69 73 63 61 72 64  mpletely discard
1600d 69 6e 67 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 74  ing .** the cont
1600e 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65  ents of the page
1600f 72 2d 63 61 63 68 65 2e 20 49 66 20 61 20 74 72  r-cache. If a tr
16010 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 61 63  ansaction was ac
16011 74 69 76 65 20 77 68 65 6e 0a 2a 2a 20 74 68 65  tive when.** the
16012 20 70 65 72 73 69 73 74 65 6e 74 20 65 72 72 6f   persistent erro
16013 72 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e  r occurred, then
16014 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
16015 75 72 6e 61 6c 20 6d 61 79 20 6e 65 65 64 0a 2a  urnal may need.*
16016 2a 20 74 6f 20 62 65 20 72 65 70 6c 61 79 65 64  * to be replayed
16017 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20   to restore the 
16018 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
16019 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 61  database file (a
1601a 73 20 69 66 0a 2a 2a 20 69 74 20 77 65 72 65 20  s if.** it were 
1601b 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a  a hot-journal)..
1601c 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
1601d 67 65 72 5f 65 72 72 6f 72 28 50 61 67 65 72 20  ger_error(Pager 
1601e 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 72 63 29  *pPager, int rc)
1601f 7b 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 72 63  {.  int rc2 = rc
16020 20 26 20 30 78 66 66 3b 0a 20 20 61 73 73 65 72   & 0xff;.  asser
16021 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
16022 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20   || !MEMDB );.  
16023 61 73 73 65 72 74 28 0a 20 20 20 20 20 20 20 70  assert(.       p
16024 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d  Pager->errCode==
16025 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20  SQLITE_FULL ||. 
16026 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72        pPager->er
16027 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b  rCode==SQLITE_OK
16028 20 7c 7c 0a 20 20 20 20 20 20 20 28 70 50 61 67   ||.       (pPag
16029 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 20 30 78  er->errCode & 0x
1602a 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52  ff)==SQLITE_IOER
1602b 52 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 32  R.  );.  if( rc2
1602c 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c  ==SQLITE_FULL ||
1602d 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 49 4f 45   rc2==SQLITE_IOE
1602e 52 52 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  RR ){.    pPager
1602f 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a  ->errCode = rc;.
16030 20 20 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61      pPager->eSta
16031 74 65 20 3d 20 50 41 47 45 52 5f 45 52 52 4f 52  te = PAGER_ERROR
16032 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
16033 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
16034 20 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20   routine ends a 
16035 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74  transaction. A t
16036 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 75 73  ransaction is us
16037 75 61 6c 6c 79 20 65 6e 64 65 64 20 62 79 20 0a  ually ended by .
16038 2a 2a 20 65 69 74 68 65 72 20 61 20 43 4f 4d 4d  ** either a COMM
16039 49 54 20 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b  IT or a ROLLBACK
1603a 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 69 73   operation. This
1603b 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 62 65 20   routine may be 
1603c 63 61 6c 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72  called .** after
1603d 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68   rollback of a h
1603e 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69  ot-journal, or i
1603f 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
16040 73 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a  s while opening.
16041 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ** the journal f
16042 69 6c 65 20 6f 72 20 77 72 69 74 69 6e 67 20 74  ile or writing t
16043 68 65 20 76 65 72 79 20 66 69 72 73 74 20 6a 6f  he very first jo
16044 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 6f 66 20  urnal-header of 
16045 61 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 72  a.** database tr
16046 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a  ansaction..** .*
16047 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
16048 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 69  s never called i
16049 6e 20 50 41 47 45 52 5f 45 52 52 4f 52 20 73 74  n PAGER_ERROR st
1604a 61 74 65 2e 20 49 66 20 69 74 20 69 73 20 63 61  ate. If it is ca
1604b 6c 6c 65 64 0a 2a 2a 20 69 6e 20 50 41 47 45 52  lled.** in PAGER
1604c 5f 4e 4f 4e 45 20 6f 72 20 50 41 47 45 52 5f 53  _NONE or PAGER_S
1604d 48 41 52 45 44 20 73 74 61 74 65 20 61 6e 64 20  HARED state and 
1604e 74 68 65 20 6c 6f 63 6b 20 68 65 6c 64 20 69 73  the lock held is
1604f 20 6c 65 73 73 0a 2a 2a 20 65 78 63 6c 75 73 69   less.** exclusi
16050 76 65 20 74 68 61 6e 20 61 20 52 45 53 45 52 56  ve than a RESERV
16051 45 44 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 61  ED lock, it is a
16052 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 4f 74   no-op..**.** Ot
16053 68 65 72 77 69 73 65 2c 20 61 6e 79 20 61 63 74  herwise, any act
16054 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61  ive savepoints a
16055 72 65 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a  re released..**.
16056 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61  ** If the journa
16057 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 2c 20  l file is open, 
16058 74 68 65 6e 20 69 74 20 69 73 20 22 66 69 6e 61  then it is "fina
16059 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 20 61 20 6a  lized". Once a j
1605a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 69 6c 65 20  ournal .** file 
1605b 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a  has been finaliz
1605c 65 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73  ed it is not pos
1605d 73 69 62 6c 65 20 74 6f 20 75 73 65 20 69 74 20  sible to use it 
1605e 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 61 20 0a  to roll back a .
1605f 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ** transaction. 
16060 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62 65 20 63  Nor will it be c
16061 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20  onsidered to be 
16062 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 79  a hot-journal by
16063 20 74 68 69 73 0a 2a 2a 20 6f 72 20 61 6e 79 20   this.** or any 
16064 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  other database c
16065 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 61 63 74  onnection. Exact
16066 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72 6e 61 6c  ly how a journal
16067 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 0a 2a 2a   is finalized.**
16068 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 74   depends on whet
16069 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 70  her or not the p
1606a 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20  ager is running 
1606b 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64  in exclusive mod
1606c 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 75 72  e and.** the cur
1606d 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64  rent journal-mod
1606e 65 20 28 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c  e (Pager.journal
1606f 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20 61 73 20  Mode value), as 
16070 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
16071 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45   journalMode==ME
16072 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 4a 6f 75 72  MORY.**     Jour
16073 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70  nal file descrip
16074 74 6f 72 20 69 73 20 73 69 6d 70 6c 79 20 63 6c  tor is simply cl
16075 6f 73 65 64 2e 20 54 68 69 73 20 64 65 73 74 72  osed. This destr
16076 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 20 20 20 69  oys an .**     i
16077 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c  n-memory journal
16078 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61  ..**.**   journa
16079 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 41 54 45 0a  lMode==TRUNCATE.
1607a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66  **     Journal f
1607b 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64  ile is truncated
1607c 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69   to zero bytes i
1607d 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  n size..**.**   
1607e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52  journalMode==PER
1607f 53 49 53 54 0a 2a 2a 20 20 20 20 20 54 68 65 20  SIST.**     The 
16080 66 69 72 73 74 20 32 38 20 62 79 74 65 73 20 6f  first 28 bytes o
16081 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
16082 6c 65 20 61 72 65 20 7a 65 72 6f 65 64 2e 20 54  le are zeroed. T
16083 68 69 73 20 69 6e 76 61 6c 69 64 61 74 65 73 0a  his invalidates.
16084 2a 2a 20 20 20 20 20 74 68 65 20 66 69 72 73 74  **     the first
16085 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
16086 69 6e 20 74 68 65 20 66 69 6c 65 2c 20 61 6e 64  in the file, and
16087 20 68 65 6e 63 65 20 74 68 65 20 65 6e 74 69 72   hence the entir
16088 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20  e journal.**    
16089 20 66 69 6c 65 2e 20 41 6e 20 69 6e 76 61 6c 69   file. An invali
1608a 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63  d journal file c
1608b 61 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20  annot be rolled 
1608c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f  back..**.**   jo
1608d 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 4c 45 54  urnalMode==DELET
1608e 45 0a 2a 2a 20 20 20 20 20 54 68 65 20 6a 6f 75  E.**     The jou
1608f 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f  rnal file is clo
16090 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 64 20  sed and deleted 
16091 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44  using sqlite3OsD
16092 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 20  elete()..**.**  
16093 20 20 20 49 66 20 74 68 65 20 70 61 67 65 72 20     If the pager 
16094 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78  is running in ex
16095 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68  clusive mode, th
16096 69 73 20 6d 65 74 68 6f 64 20 6f 66 20 66 69 6e  is method of fin
16097 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20 20 20 74  alizing.**     t
16098 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
16099 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e 20 49  is never used. I
1609a 6e 73 74 65 61 64 2c 20 69 66 20 74 68 65 20 6a  nstead, if the j
1609b 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 73 0a 2a 2a  ournalMode is.**
1609c 20 20 20 20 20 44 45 4c 45 54 45 20 61 6e 64 20       DELETE and 
1609d 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
1609e 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20  exclusive mode, 
1609f 74 68 65 20 6d 65 74 68 6f 64 20 64 65 73 63 72  the method descr
160a0 69 62 65 64 20 75 6e 64 65 72 0a 2a 2a 20 20 20  ibed under.**   
160a1 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50    journalMode==P
160a2 45 52 53 49 53 54 20 69 73 20 75 73 65 64 20 69  ERSIST is used i
160a3 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 66  nstead..**.** Af
160a4 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ter the journal 
160a5 69 73 20 66 69 6e 61 6c 69 7a 65 64 2c 20 74 68  is finalized, th
160a6 65 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74 6f  e pager moves to
160a7 20 50 41 47 45 52 5f 52 45 41 44 45 52 20 73 74   PAGER_READER st
160a8 61 74 65 2e 0a 2a 2a 20 49 66 20 72 75 6e 6e 69  ate..** If runni
160a9 6e 67 20 69 6e 20 6e 6f 6e 2d 65 78 63 6c 75 73  ng in non-exclus
160aa 69 76 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 6f 64  ive rollback mod
160ab 65 2c 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74  e, the lock on t
160ac 68 65 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20 64  he file is .** d
160ad 6f 77 6e 67 72 61 64 65 64 20 74 6f 20 61 20 53  owngraded to a S
160ae 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  HARED_LOCK..**.*
160af 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
160b0 65 74 75 72 6e 65 64 20 69 66 20 6e 6f 20 65 72  eturned if no er
160b1 72 6f 72 20 6f 63 63 75 72 73 2e 20 49 66 20 61  ror occurs. If a
160b2 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  n error occurs d
160b3 75 72 69 6e 67 0a 2a 2a 20 61 6e 79 20 6f 66 20  uring.** any of 
160b4 74 68 65 20 49 4f 20 6f 70 65 72 61 74 69 6f 6e  the IO operation
160b5 73 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68  s to finalize th
160b6 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f  e journal file o
160b7 72 20 75 6e 6c 6f 63 6b 20 74 68 65 0a 2a 2a 20  r unlock the.** 
160b8 64 61 74 61 62 61 73 65 20 74 68 65 6e 20 74 68  database then th
160b9 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20  e IO error code 
160ba 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  is returned to t
160bb 68 65 20 75 73 65 72 2e 20 49 66 20 74 68 65 20  he user. If the 
160bc 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f  .** operation to
160bd 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f   finalize the jo
160be 75 72 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c 73  urnal file fails
160bf 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 64 65 20  , then the code 
160c0 73 74 69 6c 6c 0a 2a 2a 20 74 72 69 65 73 20 74  still.** tries t
160c1 6f 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74  o unlock the dat
160c2 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 6e 6f  abase file if no
160c3 74 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d  t in exclusive m
160c4 6f 64 65 2e 20 49 66 20 74 68 65 0a 2a 2a 20 75  ode. If the.** u
160c5 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20  nlock operation 
160c6 66 61 69 6c 73 20 61 73 20 77 65 6c 6c 2c 20 74  fails as well, t
160c7 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 65 72  hen the first er
160c8 72 6f 72 20 63 6f 64 65 20 72 65 6c 61 74 65 64  ror code related
160c9 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74  .** to the first
160ca 20 65 72 72 6f 72 20 65 6e 63 6f 75 6e 74 65 72   error encounter
160cb 65 64 20 28 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ed (the journal 
160cc 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f 6e 65  finalization one
160cd 29 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  ) is.** returned
160ce 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
160cf 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61  pager_end_transa
160d0 63 74 69 6f 6e 28 50 61 67 65 72 20 2a 70 50 61  ction(Pager *pPa
160d1 67 65 72 2c 20 69 6e 74 20 68 61 73 4d 61 73 74  ger, int hasMast
160d2 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  er){.  int rc = 
160d3 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
160d4 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 72  /* Error code fr
160d5 6f 6d 20 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 6c  om journal final
160d6 69 7a 61 74 69 6f 6e 20 6f 70 65 72 61 74 69 6f  ization operatio
160d7 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 32 20 3d  n */.  int rc2 =
160d8 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
160d9 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 72  /* Error code fr
160da 6f 6d 20 64 62 20 66 69 6c 65 20 75 6e 6c 6f 63  om db file unloc
160db 6b 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 0a  k operation */..
160dc 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20    /* Do nothing 
160dd 69 66 20 74 68 65 20 70 61 67 65 72 20 64 6f 65  if the pager doe
160de 73 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 6f 70  s not have an op
160df 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  en write transac
160e0 74 69 6f 6e 0a 20 20 2a 2a 20 6f 72 20 61 74 20  tion.  ** or at 
160e1 6c 65 61 73 74 20 61 20 52 45 53 45 52 56 45 44  least a RESERVED
160e2 20 6c 6f 63 6b 2e 20 54 68 69 73 20 66 75 6e 63   lock. This func
160e3 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c  tion may be call
160e4 65 64 20 77 68 65 6e 20 74 68 65 72 65 0a 20 20  ed when there.  
160e5 2a 2a 20 69 73 20 6e 6f 20 77 72 69 74 65 2d 74  ** is no write-t
160e6 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 76  ransaction activ
160e7 65 20 62 75 74 20 61 20 52 45 53 45 52 56 45 44  e but a RESERVED
160e8 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b   or greater lock
160e9 20 69 73 0a 20 20 2a 2a 20 68 65 6c 64 20 75 6e   is.  ** held un
160ea 64 65 72 20 74 77 6f 20 63 69 72 63 75 6d 73 74  der two circumst
160eb 61 6e 63 65 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  ances:.  **.  **
160ec 20 20 20 31 2e 20 41 66 74 65 72 20 61 20 73 75     1. After a su
160ed 63 63 65 73 73 66 75 6c 20 68 6f 74 2d 6a 6f 75  ccessful hot-jou
160ee 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 69  rnal rollback, i
160ef 74 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68  t is called with
160f0 0a 20 20 2a 2a 20 20 20 20 20 20 65 53 74 61 74  .  **      eStat
160f1 65 3d 3d 50 41 47 45 52 5f 4e 4f 4e 45 20 61 6e  e==PAGER_NONE an
160f2 64 20 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49  d eLock==EXCLUSI
160f3 56 45 5f 4c 4f 43 4b 2e 0a 20 20 2a 2a 0a 20 20  VE_LOCK..  **.  
160f4 2a 2a 20 20 20 32 2e 20 49 66 20 61 20 63 6f 6e  **   2. If a con
160f5 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 6c 6f 63  nection with loc
160f6 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78 63 6c 75 73  king_mode=exclus
160f7 69 76 65 20 68 6f 6c 64 69 6e 67 20 61 6e 20 45  ive holding an E
160f8 58 43 4c 55 53 49 56 45 20 0a 20 20 2a 2a 20 20  XCLUSIVE .  **  
160f9 20 20 20 20 6c 6f 63 6b 20 73 77 69 74 63 68 65      lock switche
160fa 73 20 62 61 63 6b 20 74 6f 20 6c 6f 63 6b 69 6e  s back to lockin
160fb 67 5f 6d 6f 64 65 3d 6e 6f 72 6d 61 6c 20 61 6e  g_mode=normal an
160fc 64 20 74 68 65 6e 20 65 78 65 63 75 74 65 73 20  d then executes 
160fd 61 0a 20 20 2a 2a 20 20 20 20 20 20 72 65 61 64  a.  **      read
160fe 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68  -transaction, th
160ff 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
16100 61 6c 6c 65 64 20 77 69 74 68 20 65 53 74 61 74  alled with eStat
16101 65 3d 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20  e==PAGER_READER 
16102 0a 20 20 2a 2a 20 20 20 20 20 20 61 6e 64 20 65  .  **      and e
16103 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56 45 5f  Lock==EXCLUSIVE_
16104 4c 4f 43 4b 20 77 68 65 6e 20 74 68 65 20 72 65  LOCK when the re
16105 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ad-transaction i
16106 73 20 63 6c 6f 73 65 64 2e 0a 20 20 2a 2f 0a 20  s closed..  */. 
16107 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74 5f   assert( assert_
16108 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67  pager_state(pPag
16109 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  er) );.  assert(
1610a 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21   pPager->eState!
1610b 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 29 3b 0a  =PAGER_ERROR );.
1610c 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53    if( pPager->eS
1610d 74 61 74 65 3c 50 41 47 45 52 5f 57 52 49 54 45  tate<PAGER_WRITE
1610e 52 5f 4c 4f 43 4b 45 44 20 26 26 20 70 50 61 67  R_LOCKED && pPag
1610f 65 72 2d 3e 65 4c 6f 63 6b 3c 52 45 53 45 52 56  er->eLock<RESERV
16110 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72  ED_LOCK ){.    r
16111 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
16112 0a 20 20 7d 0a 0a 20 20 72 65 6c 65 61 73 65 41  .  }..  releaseA
16113 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50 61  llSavepoints(pPa
16114 67 65 72 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ger);.  assert( 
16115 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
16116 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 70  fd) || pPager->p
16117 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a  InJournal==0 );.
16118 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61    if( isOpen(pPa
16119 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20  ger->jfd) ){.   
1611a 20 61 73 73 65 72 74 28 20 21 70 61 67 65 72 55   assert( !pagerU
1611b 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 3b  seWal(pPager) );
1611c 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61 6c 69 7a  ..    /* Finaliz
1611d 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  e the journal fi
1611e 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73  le. */.    if( s
1611f 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e  qlite3IsMemJourn
16120 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  al(pPager->jfd) 
16121 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
16122 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
16123 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
16124 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29  NALMODE_MEMORY )
16125 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  ;.      sqlite3O
16126 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a  sClose(pPager->j
16127 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  fd);.    }else i
16128 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
16129 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
1612a 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41  URNALMODE_TRUNCA
1612b 54 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  TE ){.      if( 
1612c 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
1612d 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ff==0 ){.       
1612e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1612f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
16130 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
16131 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61  e3OsTruncate(pPa
16132 67 65 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20  ger->jfd, 0);.  
16133 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67      }.      pPag
16134 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
16135 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   0;.    }else if
16136 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
16137 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
16138 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54  RNALMODE_PERSIST
16139 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65  .      || (pPage
1613a 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
1613b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   && pPager->jour
1613c 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a  nalMode!=PAGER_J
1613d 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 29 0a  OURNALMODE_WAL).
1613e 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20      ){.      rc 
1613f 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72  = zeroJournalHdr
16140 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61 73 74  (pPager, hasMast
16141 65 72 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65  er);.      pPage
16142 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
16143 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
16144 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e      /* This bran
16145 63 68 20 6d 61 79 20 62 65 20 65 78 65 63 75 74  ch may be execut
16146 65 64 20 77 69 74 68 20 50 61 67 65 72 2e 6a 6f  ed with Pager.jo
16147 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f 52  urnalMode==MEMOR
16148 59 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 61 20  Y if.      ** a 
16149 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77 61 73 20  hot-journal was 
1614a 6a 75 73 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b  just rolled back
1614b 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
1614c 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20  he journal.     
1614d 20 2a 2a 20 66 69 6c 65 20 73 68 6f 75 6c 64 20   ** file should 
1614e 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 65  be closed and de
1614f 6c 65 74 65 64 2e 20 49 66 20 74 68 69 73 20 63  leted. If this c
16150 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 73  onnection writes
16151 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65   to.      ** the
16152 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
16153 69 74 20 77 69 6c 6c 20 64 6f 20 73 6f 20 75 73  it will do so us
16154 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  ing an in-memory
16155 20 6a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 20   journal. .     
16156 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 62 44   */.      int bD
16157 65 6c 65 74 65 20 3d 20 28 21 70 50 61 67 65 72  elete = (!pPager
16158 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26 20 73 71  ->tempFile && sq
16159 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 45 78 69 73  lite3JournalExis
1615a 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 29  ts(pPager->jfd))
1615b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1615c 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
1615d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
1615e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 0a 20  ALMODE_DELETE . 
1615f 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 50 61            || pPa
16160 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
16161 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
16162 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20  ODE_MEMORY .    
16163 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72         || pPager
16164 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
16165 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
16166 5f 57 41 4c 20 0a 20 20 20 20 20 20 29 3b 0a 20  _WAL .      );. 
16167 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c       sqlite3OsCl
16168 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  ose(pPager->jfd)
16169 3b 0a 20 20 20 20 20 20 69 66 28 20 62 44 65 6c  ;.      if( bDel
1616a 65 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ete ){.        r
1616b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c  c = sqlite3OsDel
1616c 65 74 65 28 70 50 61 67 65 72 2d 3e 70 56 66 73  ete(pPager->pVfs
1616d 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e  , pPager->zJourn
1616e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  al, 0);.      }.
1616f 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65      }.  }..#ifde
16170 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  f SQLITE_CHECK_P
16171 41 47 45 53 0a 20 20 73 71 6c 69 74 65 33 50 63  AGES.  sqlite3Pc
16172 61 63 68 65 49 74 65 72 61 74 65 44 69 72 74 79  acheIterateDirty
16173 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
16174 2c 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67 65  , pager_set_page
16175 68 61 73 68 29 3b 0a 20 20 69 66 28 20 70 50 61  hash);.  if( pPa
16176 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 30 20 26  ger->dbSize==0 &
16177 26 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52  & sqlite3PcacheR
16178 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e  efCount(pPager->
16179 70 50 43 61 63 68 65 29 3e 30 20 29 7b 0a 20 20  pPCache)>0 ){.  
1617a 20 20 50 67 48 64 72 20 2a 70 20 3d 20 70 61 67    PgHdr *p = pag
1617b 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72  er_lookup(pPager
1617c 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20 70 20  , 1);.    if( p 
1617d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 61 67 65  ){.      p->page
1617e 48 61 73 68 20 3d 20 30 3b 0a 20 20 20 20 20 20  Hash = 0;.      
1617f 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
16180 66 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  f(p);.    }.  }.
16181 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65  #endif..  sqlite
16182 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70  3BitvecDestroy(p
16183 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
16184 6c 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 49  l);.  pPager->pI
16185 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20  nJournal = 0;.  
16186 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30  pPager->nRec = 0
16187 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68  ;.  sqlite3Pcach
16188 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65 72  eCleanAll(pPager
16189 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 73 71  ->pPCache);.  sq
1618a 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e 63  lite3PcacheTrunc
1618b 61 74 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61  ate(pPager->pPCa
1618c 63 68 65 2c 20 70 50 61 67 65 72 2d 3e 64 62 53  che, pPager->dbS
1618d 69 7a 65 29 3b 0a 0a 20 20 69 66 28 20 70 61 67  ize);..  if( pag
1618e 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29  erUseWal(pPager)
1618f 20 29 7b 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20   ){.    /* Drop 
16190 74 68 65 20 57 41 4c 20 77 72 69 74 65 2d 6c 6f  the WAL write-lo
16191 63 6b 2c 20 69 66 20 61 6e 79 2e 20 41 6c 73 6f  ck, if any. Also
16192 2c 20 69 66 20 74 68 65 20 63 6f 6e 6e 65 63 74  , if the connect
16193 69 6f 6e 20 77 61 73 20 69 6e 20 0a 20 20 20 20  ion was in .    
16194 2a 2a 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d  ** locking_mode=
16195 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 62  exclusive mode b
16196 75 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 2c  ut is no longer,
16197 20 64 72 6f 70 20 74 68 65 20 45 58 43 4c 55 53   drop the EXCLUS
16198 49 56 45 20 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b  IVE .    ** lock
16199 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74   held on the dat
1619a 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20  abase file..    
1619b 2a 2f 0a 20 20 20 20 72 63 32 20 3d 20 73 71 6c  */.    rc2 = sql
1619c 69 74 65 33 57 61 6c 45 6e 64 57 72 69 74 65 54  ite3WalEndWriteT
1619d 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65  ransaction(pPage
1619e 72 2d 3e 70 57 61 6c 29 3b 0a 20 20 20 20 61 73  r->pWal);.    as
1619f 73 65 72 74 28 20 72 63 32 3d 3d 53 51 4c 49 54  sert( rc2==SQLIT
161a0 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66  E_OK );.  }.  if
161a1 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75  ( !pPager->exclu
161a2 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 26 26 20  siveMode .   && 
161a3 28 21 70 61 67 65 72 55 73 65 57 61 6c 28 70 50  (!pagerUseWal(pP
161a4 61 67 65 72 29 20 7c 7c 20 73 71 6c 69 74 65 33  ager) || sqlite3
161a5 57 61 6c 45 78 63 6c 75 73 69 76 65 4d 6f 64 65  WalExclusiveMode
161a6 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 30  (pPager->pWal, 0
161a7 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 63 32 20  )).  ){.    rc2 
161a8 3d 20 70 61 67 65 72 55 6e 6c 6f 63 6b 44 62 28  = pagerUnlockDb(
161a9 70 50 61 67 65 72 2c 20 53 48 41 52 45 44 5f 4c  pPager, SHARED_L
161aa 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 72  OCK);.    pPager
161ab 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e  ->changeCountDon
161ac 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 50 61  e = 0;.  }.  pPa
161ad 67 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41  ger->eState = PA
161ae 47 45 52 5f 52 45 41 44 45 52 3b 0a 20 20 70 50  GER_READER;.  pP
161af 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20  ager->setMaster 
161b0 3d 20 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 28  = 0;..  return (
161b1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 72 63  rc==SQLITE_OK?rc
161b2 32 3a 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  2:rc);.}../*.** 
161b3 45 78 65 63 75 74 65 20 61 20 72 6f 6c 6c 62 61  Execute a rollba
161b4 63 6b 20 69 66 20 61 20 74 72 61 6e 73 61 63 74  ck if a transact
161b5 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e  ion is active an
161b6 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 2a 2a  d unlock the .**
161b7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
161b8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  .**.** If the pa
161b9 67 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20  ger has already 
161ba 65 6e 74 65 72 65 64 20 74 68 65 20 45 52 52 4f  entered the ERRO
161bb 52 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74 20  R state, do not 
161bc 61 74 74 65 6d 70 74 20 0a 2a 2a 20 74 68 65 20  attempt .** the 
161bd 72 6f 6c 6c 62 61 63 6b 20 61 74 20 74 68 69 73  rollback at this
161be 20 74 69 6d 65 2e 20 49 6e 73 74 65 61 64 2c 20   time. Instead, 
161bf 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69  pager_unlock() i
161c0 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 0a 2a 2a  s called. The.**
161c1 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75   call to pager_u
161c2 6e 6c 6f 63 6b 28 29 20 77 69 6c 6c 20 64 69 73  nlock() will dis
161c3 63 61 72 64 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f  card all in-memo
161c4 72 79 20 70 61 67 65 73 2c 20 75 6e 6c 6f 63 6b  ry pages, unlock
161c5 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
161c6 20 66 69 6c 65 20 61 6e 64 20 6d 6f 76 65 20 74   file and move t
161c7 68 65 20 70 61 67 65 72 20 62 61 63 6b 20 74 6f  he pager back to
161c8 20 4f 50 45 4e 20 73 74 61 74 65 2e 20 49 66 20   OPEN state. If 
161c9 74 68 69 73 20 0a 2a 2a 20 6d 65 61 6e 73 20 74  this .** means t
161ca 68 61 74 20 74 68 65 72 65 20 69 73 20 61 20 68  hat there is a h
161cb 6f 74 2d 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20  ot-journal left 
161cc 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  in the file-syst
161cd 65 6d 2c 20 74 68 65 20 6e 65 78 74 20 0a 2a 2a  em, the next .**
161ce 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6f   connection to o
161cf 62 74 61 69 6e 20 61 20 73 68 61 72 65 64 20 6c  btain a shared l
161d0 6f 63 6b 20 6f 6e 20 74 68 65 20 70 61 67 65 72  ock on the pager
161d1 20 28 77 68 69 63 68 20 6d 61 79 20 62 65 20 74   (which may be t
161d2 68 69 73 20 6f 6e 65 29 20 0a 2a 2a 20 77 69 6c  his one) .** wil
161d3 6c 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0a  l roll it back..
161d4 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67  **.** If the pag
161d5 65 72 20 68 61 73 20 6e 6f 74 20 61 6c 72 65 61  er has not alrea
161d6 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 20 45  dy entered the E
161d7 52 52 4f 52 20 73 74 61 74 65 2c 20 62 75 74 20  RROR state, but 
161d8 61 6e 20 49 4f 20 6f 72 0a 2a 2a 20 6d 61 6c 6c  an IO or.** mall
161d9 6f 63 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  oc error occurs 
161da 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63  during a rollbac
161db 6b 2c 20 74 68 65 6e 20 74 68 69 73 20 77 69 6c  k, then this wil
161dc 6c 20 69 74 73 65 6c 66 20 63 61 75 73 65 20 0a  l itself cause .
161dd 2a 2a 20 74 68 65 20 70 61 67 65 72 20 74 6f 20  ** the pager to 
161de 65 6e 74 65 72 20 74 68 65 20 45 52 52 4f 52 20  enter the ERROR 
161df 73 74 61 74 65 2e 20 57 68 69 63 68 20 77 69 6c  state. Which wil
161e0 6c 20 62 65 20 63 6c 65 61 72 65 64 20 62 79 20  l be cleared by 
161e1 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 70  the.** call to p
161e2 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 2c 20 61  ager_unlock(), a
161e3 73 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76  s described abov
161e4 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
161e5 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64  d pagerUnlockAnd
161e6 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a  Rollback(Pager *
161e7 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 70  pPager){.  if( p
161e8 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d 50  Pager->eState!=P
161e9 41 47 45 52 5f 45 52 52 4f 52 20 26 26 20 70 50  AGER_ERROR && pP
161ea 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d 50 41  ager->eState!=PA
161eb 47 45 52 5f 4f 50 45 4e 20 29 7b 0a 20 20 20 20  GER_OPEN ){.    
161ec 61 73 73 65 72 74 28 20 61 73 73 65 72 74 5f 70  assert( assert_p
161ed 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 65  ager_state(pPage
161ee 72 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50  r) );.    if( pP
161ef 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41  ager->eState>=PA
161f0 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45  GER_WRITER_LOCKE
161f1 44 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  D ){.      sqlit
161f2 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
161f3 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73 71 6c  loc();.      sql
161f4 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
161f5 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  k(pPager);.     
161f6 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
161f7 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 7d  nMalloc();.    }
161f8 65 6c 73 65 20 69 66 28 20 21 70 50 61 67 65 72  else if( !pPager
161f9 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20  ->exclusiveMode 
161fa 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
161fb 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d   pPager->eState=
161fc 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20 29 3b  =PAGER_READER );
161fd 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65 6e 64  .      pager_end
161fe 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61  _transaction(pPa
161ff 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  ger, 0);.    }. 
16200 20 7d 0a 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63   }.  pager_unloc
16201 6b 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a  k(pPager);.}../*
16202 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 61 44  .** Parameter aD
16203 61 74 61 20 6d 75 73 74 20 70 6f 69 6e 74 20 74  ata must point t
16204 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 70 50  o a buffer of pP
16205 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62  ager->pageSize b
16206 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61 2e  ytes.** of data.
16207 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 72 65 74   Compute and ret
16208 75 72 6e 20 61 20 63 68 65 63 6b 73 75 6d 20 62  urn a checksum b
16209 61 73 65 64 20 6f 6e 74 20 74 68 65 20 63 6f 6e  ased ont the con
1620a 74 65 6e 74 73 20 6f 66 20 74 68 65 20 0a 2a 2a  tents of the .**
1620b 20 70 61 67 65 20 6f 66 20 64 61 74 61 20 61 6e   page of data an
1620c 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61  d the current va
1620d 6c 75 65 20 6f 66 20 70 50 61 67 65 72 2d 3e 63  lue of pPager->c
1620e 6b 73 75 6d 49 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20  ksumInit..**.** 
1620f 54 68 69 73 20 69 73 20 6e 6f 74 20 61 20 72 65  This is not a re
16210 61 6c 20 63 68 65 63 6b 73 75 6d 2e 20 49 74 20  al checksum. It 
16211 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74  is really just t
16212 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 0a 2a  he sum of the .*
16213 2a 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c  * random initial
16214 20 76 61 6c 75 65 20 28 70 50 61 67 65 72 2d 3e   value (pPager->
16215 63 6b 73 75 6d 49 6e 69 74 29 20 61 6e 64 20 65  cksumInit) and e
16216 76 65 72 79 20 32 30 30 74 68 20 62 79 74 65 0a  very 200th byte.
16217 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 64  ** of the page d
16218 61 74 61 2c 20 73 74 61 72 74 69 6e 67 20 77 69  ata, starting wi
16219 74 68 20 62 79 74 65 20 6f 66 66 73 65 74 20 28  th byte offset (
1621a 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
1621b 25 32 30 30 29 2e 0a 2a 2a 20 45 61 63 68 20 62  %200)..** Each b
1621c 79 74 65 20 69 73 20 69 6e 74 65 72 70 72 65 74  yte is interpret
1621d 65 64 20 61 73 20 61 6e 20 38 2d 62 69 74 20 75  ed as an 8-bit u
1621e 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e  nsigned integer.
1621f 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20  .**.** Changing 
16220 74 68 65 20 66 6f 72 6d 75 6c 61 20 75 73 65 64  the formula used
16221 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 69 73   to compute this
16222 20 63 68 65 63 6b 73 75 6d 20 72 65 73 75 6c 74   checksum result
16223 73 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6d  s in an.** incom
16224 70 61 74 69 62 6c 65 20 6a 6f 75 72 6e 61 6c 20  patible journal 
16225 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a  file format..**.
16226 2a 2a 20 49 66 20 6a 6f 75 72 6e 61 6c 20 63 6f  ** If journal co
16227 72 72 75 70 74 69 6f 6e 20 6f 63 63 75 72 73 20  rruption occurs 
16228 64 75 65 20 74 6f 20 61 20 70 6f 77 65 72 20 66  due to a power f
16229 61 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f 73 74  ailure, the most
1622a 20 6c 69 6b 65 6c 79 20 0a 2a 2a 20 73 63 65 6e   likely .** scen
1622b 61 72 69 6f 20 69 73 20 74 68 61 74 20 6f 6e 65  ario is that one
1622c 20 65 6e 64 20 6f 72 20 74 68 65 20 6f 74 68 65   end or the othe
1622d 72 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20  r of the record 
1622e 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 2e  will be changed.
1622f 20 0a 2a 2a 20 49 74 20 69 73 20 6d 75 63 68 20   .** It is much 
16230 6c 65 73 73 20 6c 69 6b 65 6c 79 20 74 68 61 74  less likely that
16231 20 74 68 65 20 74 77 6f 20 65 6e 64 73 20 6f 66   the two ends of
16232 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 63   the journal rec
16233 6f 72 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 63  ord will be.** c
16234 6f 72 72 65 63 74 20 61 6e 64 20 74 68 65 20 6d  orrect and the m
16235 69 64 64 6c 65 20 62 65 20 63 6f 72 72 75 70 74  iddle be corrupt
16236 2e 20 20 54 68 75 73 2c 20 74 68 69 73 20 22 63  .  Thus, this "c
16237 68 65 63 6b 73 75 6d 22 20 73 63 68 65 6d 65 2c  hecksum" scheme,
16238 0a 2a 2a 20 74 68 6f 75 67 68 20 66 61 73 74 20  .** though fast 
16239 61 6e 64 20 73 69 6d 70 6c 65 2c 20 63 61 74 63  and simple, catc
1623a 68 65 73 20 74 68 65 20 6d 6f 73 74 6c 79 20 6c  hes the mostly l
1623b 69 6b 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63 6f  ikely kind of co
1623c 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  rruption..*/.sta
1623d 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 63 6b  tic u32 pager_ck
1623e 73 75 6d 28 50 61 67 65 72 20 2a 70 50 61 67 65  sum(Pager *pPage
1623f 72 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61 44 61  r, const u8 *aDa
16240 74 61 29 7b 0a 20 20 75 33 32 20 63 6b 73 75 6d  ta){.  u32 cksum
16241 20 3d 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d   = pPager->cksum
16242 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Init;         /*
16243 20 43 68 65 63 6b 73 75 6d 20 76 61 6c 75 65 20   Checksum value 
16244 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69  to return */.  i
16245 6e 74 20 69 20 3d 20 70 50 61 67 65 72 2d 3e 70  nt i = pPager->p
16246 61 67 65 53 69 7a 65 2d 32 30 30 3b 20 20 20 20  ageSize-200;    
16247 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
16248 75 6e 74 65 72 20 2a 2f 0a 20 20 77 68 69 6c 65  unter */.  while
16249 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 63 6b 73  ( i>0 ){.    cks
1624a 75 6d 20 2b 3d 20 61 44 61 74 61 5b 69 5d 3b 0a  um += aData[i];.
1624b 20 20 20 20 69 20 2d 3d 20 32 30 30 3b 0a 20 20      i -= 200;.  
1624c 7d 0a 20 20 72 65 74 75 72 6e 20 63 6b 73 75 6d  }.  return cksum
1624d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f 72  ;.}../*.** Repor
1624e 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61  t the current pa
1624f 67 65 20 73 69 7a 65 20 61 6e 64 20 6e 75 6d 62  ge size and numb
16250 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20 62  er of reserved b
16251 79 74 65 73 20 62 61 63 6b 0a 2a 2a 20 74 6f 20  ytes back.** to 
16252 74 68 65 20 63 6f 64 65 63 2e 0a 2a 2f 0a 23 69  the codec..*/.#i
16253 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
16254 43 4f 44 45 43 0a 73 74 61 74 69 63 20 76 6f 69  CODEC.static voi
16255 64 20 70 61 67 65 72 52 65 70 6f 72 74 53 69 7a  d pagerReportSiz
16256 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  e(Pager *pPager)
16257 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  {.  if( pPager->
16258 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 20 29  xCodecSizeChng )
16259 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 78 43  {.    pPager->xC
1625a 6f 64 65 63 53 69 7a 65 43 68 6e 67 28 70 50 61  odecSizeChng(pPa
1625b 67 65 72 2d 3e 70 43 6f 64 65 63 2c 20 70 50 61  ger->pCodec, pPa
1625c 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a 20  ger->pageSize,. 
1625d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1625e 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29 70            (int)p
1625f 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 29  Pager->nReserve)
16260 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20  ;.  }.}.#else.# 
16261 64 65 66 69 6e 65 20 70 61 67 65 72 52 65 70 6f  define pagerRepo
16262 72 74 53 69 7a 65 28 58 29 20 20 20 20 20 2f 2a  rtSize(X)     /*
16263 20 4e 6f 2d 6f 70 20 69 66 20 77 65 20 64 6f 20   No-op if we do 
16264 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 20 63 6f  not support a co
16265 64 65 63 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f  dec */.#endif../
16266 2a 0a 2a 2a 20 52 65 61 64 20 61 20 73 69 6e 67  *.** Read a sing
16267 6c 65 20 70 61 67 65 20 66 72 6f 6d 20 65 69 74  le page from eit
16268 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  her the journal 
16269 66 69 6c 65 20 28 69 66 20 69 73 4d 61 69 6e 4a  file (if isMainJ
1626a 72 6e 6c 3d 3d 31 29 20 6f 72 0a 2a 2a 20 66 72  rnl==1) or.** fr
1626b 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  om the sub-journ
1626c 61 6c 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e  al (if isMainJrn
1626d 6c 3d 3d 30 29 20 61 6e 64 20 70 6c 61 79 62 61  l==0) and playba
1626e 63 6b 20 74 68 61 74 20 70 61 67 65 2e 0a 2a 2a  ck that page..**
1626f 20 54 68 65 20 70 61 67 65 20 62 65 67 69 6e 73   The page begins
16270 20 61 74 20 6f 66 66 73 65 74 20 2a 70 4f 66 66   at offset *pOff
16271 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c  set into the fil
16272 65 2e 20 54 68 65 20 2a 70 4f 66 66 73 65 74 0a  e. The *pOffset.
16273 2a 2a 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72  ** value is incr
16274 65 61 73 65 64 20 74 6f 20 74 68 65 20 73 74 61  eased to the sta
16275 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70  rt of the next p
16276 61 67 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  age in the journ
16277 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61  al..**.** The ma
16278 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  in rollback jour
16279 6e 61 6c 20 75 73 65 73 20 63 68 65 63 6b 73 75  nal uses checksu
1627a 6d 73 20 2d 20 74 68 65 20 73 74 61 74 65 6d 65  ms - the stateme
1627b 6e 74 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20  nt journal does 
1627c 0a 2a 2a 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49  .** not..**.** I
1627d 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  f the page numbe
1627e 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 72 65  r of the page re
1627f 63 6f 72 64 20 72 65 61 64 20 66 72 6f 6d 20 74  cord read from t
16280 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c  he (sub-)journal
16281 20 66 69 6c 65 0a 2a 2a 20 69 73 20 67 72 65 61   file.** is grea
16282 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72  ter than the cur
16283 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50 61  rent value of Pa
16284 67 65 72 2e 64 62 53 69 7a 65 2c 20 74 68 65 6e  ger.dbSize, then
16285 20 70 6c 61 79 62 61 63 6b 20 69 73 0a 2a 2a 20   playback is.** 
16286 73 6b 69 70 70 65 64 20 61 6e 64 20 53 51 4c 49  skipped and SQLI
16287 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
16288 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44 6f 6e  d..**.** If pDon
16289 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  e is not NULL, t
1628a 68 65 6e 20 69 74 20 69 73 20 61 20 72 65 63 6f  hen it is a reco
1628b 72 64 20 6f 66 20 70 61 67 65 73 20 74 68 61 74  rd of pages that
1628c 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a 2a   have already.**
1628d 20 62 65 65 6e 20 70 6c 61 79 65 64 20 62 61 63   been played bac
1628e 6b 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20  k.  If the page 
1628f 61 74 20 2a 70 4f 66 66 73 65 74 20 68 61 73 20  at *pOffset has 
16290 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 6c 61  already been pla
16291 79 65 64 20 62 61 63 6b 0a 2a 2a 20 28 69 66 20  yed back.** (if 
16292 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
16293 67 20 70 44 6f 6e 65 20 62 69 74 20 69 73 20 73  g pDone bit is s
16294 65 74 29 20 74 68 65 6e 20 73 6b 69 70 20 74 68  et) then skip th
16295 65 20 70 6c 61 79 62 61 63 6b 2e 0a 2a 2a 20 4d  e playback..** M
16296 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 44 6f  ake sure the pDo
16297 6e 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e  ne bit correspon
16298 64 69 6e 67 20 74 6f 20 74 68 65 20 2a 70 4f 66  ding to the *pOf
16299 66 73 65 74 20 70 61 67 65 20 69 73 20 73 65 74  fset page is set
1629a 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74  .** prior to ret
1629b 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66  urning..**.** If
1629c 20 74 68 65 20 70 61 67 65 20 72 65 63 6f 72 64   the page record
1629d 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79   is successfully
1629e 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 28   read from the (
1629f 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c  sub-)journal fil
162a0 65 0a 2a 2a 20 61 6e 64 20 70 6c 61 79 65 64 20  e.** and played 
162a1 62 61 63 6b 2c 20 74 68 65 6e 20 53 51 4c 49 54  back, then SQLIT
162a2 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
162a3 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72  . If an IO error
162a4 20 6f 63 63 75 72 73 0a 2a 2a 20 77 68 69 6c 65   occurs.** while
162a5 20 72 65 61 64 69 6e 67 20 74 68 65 20 72 65 63   reading the rec
162a6 6f 72 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75  ord from the (su
162a7 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  b-)journal file 
162a8 6f 72 20 77 68 69 6c 65 20 77 72 69 74 69 6e 67  or while writing
162a9 0a 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62  .** to the datab
162aa 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 74  ase file, then t
162ab 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  he IO error code
162ac 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66   is returned. If
162ad 20 64 61 74 61 0a 2a 2a 20 69 73 20 73 75 63 63   data.** is succ
162ae 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66 72  essfully read fr
162af 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75  om the (sub-)jou
162b0 72 6e 61 6c 20 66 69 6c 65 20 62 75 74 20 61 70  rnal file but ap
162b1 70 65 61 72 73 20 74 6f 20 62 65 0a 2a 2a 20 63  pears to be.** c
162b2 6f 72 72 75 70 74 65 64 2c 20 53 51 4c 49 54 45  orrupted, SQLITE
162b3 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e 65  _DONE is returne
162b4 64 2e 20 44 61 74 61 20 69 73 20 63 6f 6e 73 69  d. Data is consi
162b5 64 65 72 65 64 20 63 6f 72 72 75 70 74 65 64 20  dered corrupted 
162b6 69 6e 0a 2a 2a 20 74 77 6f 20 63 69 72 63 75 6d  in.** two circum
162b7 73 74 61 6e 63 65 73 3a 0a 2a 2a 20 0a 2a 2a 20  stances:.** .** 
162b8 20 20 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72    * If the recor
162b9 64 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73  d page-number is
162ba 20 69 6c 6c 65 67 61 6c 20 28 30 20 6f 72 20 50   illegal (0 or P
162bb 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 29 2c 20 6f  AGER_MJ_PGNO), o
162bc 72 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20  r.**   * If the 
162bd 72 65 63 6f 72 64 20 69 73 20 62 65 69 6e 67 20  record is being 
162be 72 6f 6c 6c 65 64 20 62 61 63 6b 20 66 72 6f 6d  rolled back from
162bf 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
162c0 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 61 6e  l file.**     an
162c1 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 66  d the checksum f
162c2 69 65 6c 64 20 64 6f 65 73 20 6e 6f 74 20 6d 61  ield does not ma
162c3 74 63 68 20 74 68 65 20 72 65 63 6f 72 64 20 63  tch the record c
162c4 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 65  ontent..**.** Ne
162c5 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74  ither of these t
162c6 77 6f 20 73 63 65 6e 61 72 69 6f 73 20 61 72 65  wo scenarios are
162c7 20 70 6f 73 73 69 62 6c 65 20 64 75 72 69 6e 67   possible during
162c8 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c   a savepoint rol
162c9 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  lback..**.** If 
162ca 74 68 69 73 20 69 73 20 61 20 73 61 76 65 70 6f  this is a savepo
162cb 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68  int rollback, th
162cc 65 6e 20 6d 65 6d 6f 72 79 20 6d 61 79 20 68 61  en memory may ha
162cd 76 65 20 74 6f 20 62 65 20 64 79 6e 61 6d 69 63  ve to be dynamic
162ce 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  ally.** allocate
162cf 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
162d0 6f 6e 2e 20 49 66 20 74 68 69 73 20 69 73 20 74  on. If this is t
162d1 68 65 20 63 61 73 65 20 61 6e 64 20 61 6e 20 61  he case and an a
162d2 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
162d3 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  .** SQLITE_NOMEM
162d4 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
162d5 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
162d6 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  r_playback_one_p
162d7 61 67 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50  age(.  Pager *pP
162d8 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20  ager,           
162d9 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65       /* The page
162da 72 20 62 65 69 6e 67 20 70 6c 61 79 65 64 20 62  r being played b
162db 61 63 6b 20 2a 2f 0a 20 20 69 36 34 20 2a 70 4f  ack */.  i64 *pO
162dc 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20  ffset,          
162dd 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
162de 20 6f 66 20 72 65 63 6f 72 64 20 74 6f 20 70 6c   of record to pl
162df 61 79 62 61 63 6b 20 2a 2f 0a 20 20 42 69 74 76  ayback */.  Bitv
162e0 65 63 20 2a 70 44 6f 6e 65 2c 20 20 20 20 20 20  ec *pDone,      
162e1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 74            /* Bit
162e2 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72  vec of pages alr
162e3 65 61 64 79 20 70 6c 61 79 65 64 20 62 61 63 6b  eady played back
162e4 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 61 69 6e   */.  int isMain
162e5 4a 72 6e 6c 2c 20 20 20 20 20 20 20 20 20 20 20  Jrnl,           
162e6 20 20 20 20 2f 2a 20 31 20 2d 3e 20 6d 61 69 6e      /* 1 -> main
162e7 20 6a 6f 75 72 6e 61 6c 2e 20 30 20 2d 3e 20 73   journal. 0 -> s
162e8 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20  ub-journal. */. 
162e9 20 69 6e 74 20 69 73 53 61 76 65 70 6e 74 20 20   int isSavepnt  
162ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
162eb 2a 20 54 72 75 65 20 66 6f 72 20 61 20 73 61 76  * True for a sav
162ec 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20  epoint rollback 
162ed 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
162ee 20 20 50 67 48 64 72 20 2a 70 50 67 3b 20 20 20    PgHdr *pPg;   
162ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
162f0 2f 2a 20 41 6e 20 65 78 69 73 74 69 6e 67 20 70  /* An existing p
162f1 61 67 65 20 69 6e 20 74 68 65 20 63 61 63 68 65  age in the cache
162f2 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b   */.  Pgno pgno;
162f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
162f4 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20      /* The page 
162f5 6e 75 6d 62 65 72 20 6f 66 20 61 20 70 61 67 65  number of a page
162f6 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20   in journal */. 
162f7 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20 20 20   u32 cksum;     
162f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
162f9 2a 20 43 68 65 63 6b 73 75 6d 20 75 73 65 64 20  * Checksum used 
162fa 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65 63 6b  for sanity check
162fb 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61  ing */.  char *a
162fc 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
162fd 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72         /* Tempor
162fe 61 72 79 20 73 74 6f 72 61 67 65 20 66 6f 72 20  ary storage for 
162ff 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 73 71  the page */.  sq
16300 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64 3b  lite3_file *jfd;
16301 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
16302 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
16303 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e  or for the journ
16304 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74  al file */.  int
16305 20 69 73 53 79 6e 63 65 64 3b 20 20 20 20 20 20   isSynced;      
16306 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
16307 75 65 20 69 66 20 6a 6f 75 72 6e 61 6c 20 70 61  ue if journal pa
16308 67 65 20 69 73 20 73 79 6e 63 65 64 20 2a 2f 0a  ge is synced */.
16309 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 4d 61  .  assert( (isMa
1630a 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30 20 29 3b  inJrnl&~1)==0 );
1630b 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 69 6e 4a        /* isMainJ
1630c 72 6e 6c 20 69 73 20 30 20 6f 72 20 31 20 2a 2f  rnl is 0 or 1 */
1630d 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 53 61  .  assert( (isSa
1630e 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20 29 3b 20  vepnt&~1)==0 ); 
1630f 20 20 20 20 20 20 2f 2a 20 69 73 53 61 76 65 70        /* isSavep
16310 6e 74 20 69 73 20 30 20 6f 72 20 31 20 2a 2f 0a  nt is 0 or 1 */.
16311 20 20 61 73 73 65 72 74 28 20 69 73 4d 61 69 6e    assert( isMain
16312 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65 20 29 3b  Jrnl || pDone );
16313 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65 20 61 6c       /* pDone al
16314 77 61 79 73 20 75 73 65 64 20 6f 6e 20 73 75 62  ways used on sub
16315 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 61  -journals */.  a
16316 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e 74  ssert( isSavepnt
16317 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20 29 3b 20   || pDone==0 ); 
16318 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65 76 65 72    /* pDone never
16319 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d 73 61 76   used on non-sav
1631a 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 61 44 61  epoint */..  aDa
1631b 74 61 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d  ta = pPager->pTm
1631c 70 53 70 61 63 65 3b 0a 20 20 61 73 73 65 72 74  pSpace;.  assert
1631d 28 20 61 44 61 74 61 20 29 3b 20 20 20 20 20 20  ( aData );      
1631e 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61     /* Temp stora
1631f 67 65 20 6d 75 73 74 20 68 61 76 65 20 61 6c 72  ge must have alr
16320 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61  eady been alloca
16321 74 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ted */.  assert(
16322 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61   pagerUseWal(pPa
16323 67 65 72 29 3d 3d 30 20 7c 7c 20 28 21 69 73 4d  ger)==0 || (!isM
16324 61 69 6e 4a 72 6e 6c 20 26 26 20 69 73 53 61 76  ainJrnl && isSav
16325 65 70 6e 74 29 20 29 3b 0a 0a 20 20 2f 2a 20 45  epnt) );..  /* E
16326 69 74 68 65 72 20 74 68 65 20 73 74 61 74 65 20  ither the state 
16327 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
16328 50 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43  PAGER_WRITER_CAC
16329 48 45 4d 4f 44 20 28 61 20 74 72 61 6e 73 61 63  HEMOD (a transac
1632a 74 69 6f 6e 20 0a 20 20 2a 2a 20 6f 72 20 73 61  tion .  ** or sa
1632b 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b  vepoint rollback
1632c 20 64 6f 6e 65 20 61 74 20 74 68 65 20 72 65 71   done at the req
1632d 75 65 73 74 20 6f 66 20 74 68 65 20 63 61 6c 6c  uest of the call
1632e 65 72 29 20 6f 72 20 74 68 69 73 20 69 73 0a 20  er) or this is. 
1632f 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61   ** a hot-journa
16330 6c 20 72 6f 6c 6c 62 61 63 6b 2e 20 49 66 20 69  l rollback. If i
16331 74 20 69 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e  t is a hot-journ
16332 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65  al rollback, the
16333 20 70 61 67 65 72 0a 20 20 2a 2a 20 69 73 20 69   pager.  ** is i
16334 6e 20 73 74 61 74 65 20 4f 50 45 4e 20 61 6e 64  n state OPEN and
16335 20 68 6f 6c 64 73 20 61 6e 20 45 58 43 4c 55 53   holds an EXCLUS
16336 49 56 45 20 6c 6f 63 6b 2e 20 48 6f 74 2d 6a 6f  IVE lock. Hot-jo
16337 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 0a 20  urnal rollback. 
16338 20 2a 2a 20 6f 6e 6c 79 20 72 65 61 64 73 20 66   ** only reads f
16339 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75  rom the main jou
1633a 72 6e 61 6c 2c 20 6e 6f 74 20 74 68 65 20 73 75  rnal, not the su
1633b 62 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2f 0a  b-journal..  */.
1633c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
1633d 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f  ->eState>=PAGER_
1633e 57 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 0a  WRITER_CACHEMOD.
1633f 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65         || (pPage
16340 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
16341 5f 4f 50 45 4e 20 26 26 20 70 50 61 67 65 72 2d  _OPEN && pPager-
16342 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56  >eLock==EXCLUSIV
16343 45 5f 4c 4f 43 4b 29 0a 20 20 29 3b 0a 20 20 61  E_LOCK).  );.  a
16344 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65  ssert( pPager->e
16345 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 57 52 49  State>=PAGER_WRI
16346 54 45 52 5f 43 41 43 48 45 4d 4f 44 20 7c 7c 20  TER_CACHEMOD || 
16347 69 73 4d 61 69 6e 4a 72 6e 6c 20 29 3b 0a 0a 20  isMainJrnl );.. 
16348 20 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67   /* Read the pag
16349 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67  e number and pag
1634a 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20  e data from the 
1634b 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a  journal or sub-j
1634c 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65  ournal.  ** file
1634d 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  . Return an erro
1634e 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61  r code to the ca
1634f 6c 6c 65 72 20 69 66 20 61 6e 20 49 4f 20 65 72  ller if an IO er
16350 72 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a 2f  ror occurs..  */
16351 0a 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e 4a  .  jfd = isMainJ
16352 72 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66  rnl ? pPager->jf
16353 64 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66 64  d : pPager->sjfd
16354 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62  ;.  rc = read32b
16355 69 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73 65  its(jfd, *pOffse
16356 74 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28  t, &pgno);.  if(
16357 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
16358 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63   return rc;.  rc
16359 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
1635a 28 6a 66 64 2c 20 28 75 38 2a 29 61 44 61 74 61  (jfd, (u8*)aData
1635b 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
1635c 7a 65 2c 20 28 2a 70 4f 66 66 73 65 74 29 2b 34  ze, (*pOffset)+4
1635d 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
1635e 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
1635f 72 63 3b 0a 20 20 2a 70 4f 66 66 73 65 74 20 2b  rc;.  *pOffset +
16360 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  = pPager->pageSi
16361 7a 65 20 2b 20 34 20 2b 20 69 73 4d 61 69 6e 4a  ze + 4 + isMainJ
16362 72 6e 6c 2a 34 3b 0a 0a 20 20 2f 2a 20 53 61 6e  rnl*4;..  /* San
16363 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20  ity checking on 
16364 74 68 65 20 70 61 67 65 2e 20 20 54 68 69 73 20  the page.  This 
16365 69 73 20 6d 6f 72 65 20 69 6d 70 6f 72 74 61 6e  is more importan
16366 74 20 74 68 61 74 20 49 20 6f 72 69 67 69 6e 61  t that I origina
16367 6c 6c 79 0a 20 20 2a 2a 20 74 68 6f 75 67 68 74  lly.  ** thought
16368 2e 20 20 49 66 20 61 20 70 6f 77 65 72 20 66 61  .  If a power fa
16369 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69  ilure occurs whi
1636a 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69  le the journal i
1636b 73 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c  s being written,
1636c 0a 20 20 2a 2a 20 69 74 20 63 6f 75 6c 64 20 63  .  ** it could c
1636d 61 75 73 65 20 69 6e 76 61 6c 69 64 20 64 61 74  ause invalid dat
1636e 61 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20  a to be written 
1636f 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  into the journal
16370 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 0a 20 20  .  We need to.  
16371 2a 2a 20 64 65 74 65 63 74 20 74 68 69 73 20 69  ** detect this i
16372 6e 76 61 6c 69 64 20 64 61 74 61 20 28 77 69 74  nvalid data (wit
16373 68 20 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69  h high probabili
16374 74 79 29 20 61 6e 64 20 69 67 6e 6f 72 65 20 69  ty) and ignore i
16375 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 67  t..  */.  if( pg
16376 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50  no==0 || pgno==P
16377 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
16378 67 65 72 29 20 29 7b 0a 20 20 20 20 61 73 73 65  ger) ){.    asse
16379 72 74 28 20 21 69 73 53 61 76 65 70 6e 74 20 29  rt( !isSavepnt )
1637a 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
1637b 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20  ITE_DONE;.  }.  
1637c 69 66 28 20 70 67 6e 6f 3e 28 50 67 6e 6f 29 70  if( pgno>(Pgno)p
1637d 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 7c 7c  Pager->dbSize ||
1637e 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65   sqlite3BitvecTe
1637f 73 74 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 20  st(pDone, pgno) 
16380 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
16381 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69  LITE_OK;.  }.  i
16382 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 29 7b  f( isMainJrnl ){
16383 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 33 32  .    rc = read32
16384 62 69 74 73 28 6a 66 64 2c 20 28 2a 70 4f 66 66  bits(jfd, (*pOff
16385 73 65 74 29 2d 34 2c 20 26 63 6b 73 75 6d 29 3b  set)-4, &cksum);
16386 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
16387 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28  turn rc;.    if(
16388 20 21 69 73 53 61 76 65 70 6e 74 20 26 26 20 70   !isSavepnt && p
16389 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65  ager_cksum(pPage
1638a 72 2c 20 28 75 38 2a 29 61 44 61 74 61 29 21 3d  r, (u8*)aData)!=
1638b 63 6b 73 75 6d 20 29 7b 0a 20 20 20 20 20 20 72  cksum ){.      r
1638c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
1638d 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  E;.    }.  }..  
1638e 2f 2a 20 49 66 20 74 68 69 73 20 70 61 67 65 20  /* If this page 
1638f 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
16390 20 70 6c 61 79 65 64 20 62 79 20 62 65 66 6f 72   played by befor
16391 65 20 64 75 72 69 6e 67 20 74 68 65 20 63 75 72  e during the cur
16392 72 65 6e 74 0a 20 20 2a 2a 20 72 6f 6c 6c 62 61  rent.  ** rollba
16393 63 6b 2c 20 74 68 65 6e 20 64 6f 6e 27 74 20 62  ck, then don't b
16394 6f 74 68 65 72 20 74 6f 20 70 6c 61 79 20 69 74  other to play it
16395 20 62 61 63 6b 20 61 67 61 69 6e 2e 0a 20 20 2a   back again..  *
16396 2f 0a 20 20 69 66 28 20 70 44 6f 6e 65 20 26 26  /.  if( pDone &&
16397 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69   (rc = sqlite3Bi
16398 74 76 65 63 53 65 74 28 70 44 6f 6e 65 2c 20 70  tvecSet(pDone, p
16399 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b  gno))!=SQLITE_OK
1639a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
1639b 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 68 65  c;.  }..  /* Whe
1639c 6e 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 20 70  n playing back p
1639d 61 67 65 20 31 2c 20 72 65 73 74 6f 72 65 20 74  age 1, restore t
1639e 68 65 20 6e 52 65 73 65 72 76 65 20 73 65 74 74  he nReserve sett
1639f 69 6e 67 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ing.  */.  if( p
163a0 67 6e 6f 3d 3d 31 20 26 26 20 70 50 61 67 65 72  gno==1 && pPager
163a1 2d 3e 6e 52 65 73 65 72 76 65 21 3d 28 28 75 38  ->nReserve!=((u8
163a2 2a 29 61 44 61 74 61 29 5b 32 30 5d 20 29 7b 0a  *)aData)[20] ){.
163a3 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 73      pPager->nRes
163a4 65 72 76 65 20 3d 20 28 28 75 38 2a 29 61 44 61  erve = ((u8*)aDa
163a5 74 61 29 5b 32 30 5d 3b 0a 20 20 20 20 70 61 67  ta)[20];.    pag
163a6 65 72 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61  erReportSize(pPa
163a7 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ger);.  }..  /* 
163a8 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  If the pager is 
163a9 69 6e 20 43 41 43 48 45 4d 4f 44 20 73 74 61 74  in CACHEMOD stat
163aa 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75  e, then there mu
163ab 73 74 20 62 65 20 61 20 63 6f 70 79 20 6f 66 20  st be a copy of 
163ac 74 68 69 73 0a 20 20 2a 2a 20 70 61 67 65 20 69  this.  ** page i
163ad 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68  n the pager cach
163ae 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  e. In this case 
163af 6a 75 73 74 20 75 70 64 61 74 65 20 74 68 65 20  just update the 
163b0 70 61 67 65 72 20 63 61 63 68 65 2c 0a 20 20 2a  pager cache,.  *
163b1 2a 20 6e 6f 74 20 74 68 65 20 64 61 74 61 62 61  * not the databa
163b2 73 65 20 66 69 6c 65 2e 20 54 68 65 20 70 61 67  se file. The pag
163b3 65 20 69 73 20 6c 65 66 74 20 6d 61 72 6b 65 64  e is left marked
163b4 20 64 69 72 74 79 20 69 6e 20 74 68 69 73 20 63   dirty in this c
163b5 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41  ase..  **.  ** A
163b6 6e 20 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 74  n exception to t
163b7 68 65 20 61 62 6f 76 65 20 72 75 6c 65 3a 20 49  he above rule: I
163b8 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  f the database i
163b9 73 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64  s in no-sync mod
163ba 65 0a 20 20 2a 2a 20 61 6e 64 20 61 20 70 61 67  e.  ** and a pag
163bb 65 20 69 73 20 6d 6f 76 65 64 20 64 75 72 69 6e  e is moved durin
163bc 67 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c  g an incremental
163bd 20 76 61 63 75 75 6d 20 74 68 65 6e 20 74 68 65   vacuum then the
163be 20 70 61 67 65 20 6d 61 79 0a 20 20 2a 2a 20 6e   page may.  ** n
163bf 6f 74 20 62 65 20 69 6e 20 74 68 65 20 70 61 67  ot be in the pag
163c0 65 72 20 63 61 63 68 65 2e 20 4c 61 74 65 72 3a  er cache. Later:
163c1 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f   if a malloc() o
163c2 72 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  r IO error occur
163c3 73 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 61 20  s.  ** during a 
163c4 4d 6f 76 65 70 61 67 65 28 29 20 63 61 6c 6c 2c  Movepage() call,
163c5 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d   then the page m
163c6 61 79 20 6e 6f 74 20 62 65 20 69 6e 20 74 68 65  ay not be in the
163c7 20 63 61 63 68 65 0a 20 20 2a 2a 20 65 69 74 68   cache.  ** eith
163c8 65 72 2e 20 53 6f 20 74 68 65 20 63 6f 6e 64 69  er. So the condi
163c9 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 69  tion described i
163ca 6e 20 74 68 65 20 61 62 6f 76 65 20 70 61 72 61  n the above para
163cb 67 72 61 70 68 20 69 73 20 6e 6f 74 0a 20 20 2a  graph is not.  *
163cc 2a 20 61 73 73 65 72 74 28 29 61 62 6c 65 2e 0a  * assert()able..
163cd 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 69 6e 20    **.  ** If in 
163ce 57 52 49 54 45 52 5f 44 42 4d 4f 44 2c 20 57 52  WRITER_DBMOD, WR
163cf 49 54 45 52 5f 46 49 4e 49 53 48 45 44 20 6f 72  ITER_FINISHED or
163d0 20 4f 50 45 4e 20 73 74 61 74 65 2c 20 74 68 65   OPEN state, the
163d1 6e 20 77 65 20 75 70 64 61 74 65 20 74 68 65 0a  n we update the.
163d2 20 20 2a 2a 20 70 61 67 65 72 20 63 61 63 68 65    ** pager cache
163d3 20 69 66 20 69 74 20 65 78 69 73 74 73 20 61 6e   if it exists an
163d4 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e  d the main file.
163d5 20 54 68 65 20 70 61 67 65 20 69 73 20 74 68 65   The page is the
163d6 6e 20 6d 61 72 6b 65 64 20 0a 20 20 2a 2a 20 6e  n marked .  ** n
163d7 6f 74 20 64 69 72 74 79 2e 20 53 69 6e 63 65 20  ot dirty. Since 
163d8 74 68 69 73 20 63 6f 64 65 20 69 73 20 6f 6e 6c  this code is onl
163d9 79 20 65 78 65 63 75 74 65 64 20 69 6e 20 50 41  y executed in PA
163da 47 45 52 5f 4f 50 45 4e 20 73 74 61 74 65 20 66  GER_OPEN state f
163db 6f 72 0a 20 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f  or.  ** a hot-jo
163dc 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20  urnal rollback, 
163dd 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  it is guaranteed
163de 20 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 63   that the page-c
163df 61 63 68 65 20 69 73 20 65 6d 70 74 79 0a 20 20  ache is empty.  
163e0 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65 72 20  ** if the pager 
163e1 69 73 20 69 6e 20 4f 50 45 4e 20 73 74 61 74 65  is in OPEN state
163e2 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69 63 6b  ..  **.  ** Tick
163e3 65 74 20 23 31 31 37 31 3a 20 20 54 68 65 20 73  et #1171:  The s
163e4 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
163e5 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 70   might contain p
163e6 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 68 61 74  age content that
163e7 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 65 72 65   is.  ** differe
163e8 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  nt from the page
163e9 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 68 65 20   content at the 
163ea 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61  start of the tra
163eb 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54  nsaction..  ** T
163ec 68 69 73 20 6f 63 63 75 72 73 20 77 68 65 6e 20  his occurs when 
163ed 61 20 70 61 67 65 20 69 73 20 63 68 61 6e 67 65  a page is change
163ee 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73  d prior to the s
163ef 74 61 72 74 20 6f 66 20 61 20 73 74 61 74 65 6d  tart of a statem
163f0 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 63 68  ent.  ** then ch
163f1 61 6e 67 65 64 20 61 67 61 69 6e 20 77 69 74 68  anged again with
163f2 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  in the statement
163f3 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20  .  When rolling 
163f4 62 61 63 6b 20 73 75 63 68 20 61 0a 20 20 2a 2a  back such a.  **
163f5 20 73 74 61 74 65 6d 65 6e 74 20 77 65 20 6d 75   statement we mu
163f6 73 74 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20  st not write to 
163f7 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74  the original dat
163f8 61 62 61 73 65 20 75 6e 6c 65 73 73 20 77 65 20  abase unless we 
163f9 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20 63 65  know.  ** for ce
163fa 72 74 61 69 6e 20 74 68 61 74 20 6f 72 69 67 69  rtain that origi
163fb 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e 74  nal page content
163fc 73 20 61 72 65 20 73 79 6e 63 65 64 20 69 6e 74  s are synced int
163fd 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62  o the main rollb
163fe 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c  ack.  ** journal
163ff 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20  .  Otherwise, a 
16400 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67 68 74  power loss might
16401 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 65 64 20   leave modified 
16402 64 61 74 61 20 69 6e 20 74 68 65 0a 20 20 2a 2a  data in the.  **
16403 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
16404 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 72 79 20  ithout an entry 
16405 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  in the rollback 
16406 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 63 61 6e  journal that can
16407 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 20 74 68  .  ** restore th
16408 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74  e database to it
16409 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72 6d 2e  s original form.
1640a 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73    Two conditions
1640b 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 6d 65   must be.  ** me
1640c 74 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67  t before writing
1640d 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
1640e 20 66 69 6c 65 73 2e 20 28 31 29 20 74 68 65 20   files. (1) the 
1640f 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65  database must be
16410 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 20 28  .  ** locked.  (
16411 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20  2) we know that 
16412 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67  the original pag
16413 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 75 6c  e content is ful
16414 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a 20 69  ly synced.  ** i
16415 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  n the main journ
16416 61 6c 20 65 69 74 68 65 72 20 62 65 63 61 75 73  al either becaus
16417 65 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  e the page is no
16418 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 65 6c  t in cache or el
16419 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65  se.  ** the page
1641a 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 6e 65   is marked as ne
1641b 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a 2a 0a  edSync==0..  **.
1641c 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31 34 3a    ** 2008-04-14:
1641d 20 20 57 68 65 6e 20 61 74 74 65 6d 70 74 69 6e    When attemptin
1641e 67 20 74 6f 20 76 61 63 75 75 6d 20 61 20 63 6f  g to vacuum a co
1641f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66  rrupt database f
16420 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69 73 20  ile, it.  ** is 
16421 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 61 69 6c  possible to fail
16422 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20   a statement on 
16423 61 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20  a database that 
16424 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 65 78 69  does not yet exi
16425 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20  st..  ** Do not 
16426 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65  attempt to write
16427 20 69 66 20 64 61 74 61 62 61 73 65 20 66 69 6c   if database fil
16428 65 20 68 61 73 20 6e 65 76 65 72 20 62 65 65 6e  e has never been
16429 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20   opened..  */.  
1642a 69 66 28 20 70 61 67 65 72 55 73 65 57 61 6c 28  if( pagerUseWal(
1642b 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 70  pPager) ){.    p
1642c 50 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  Pg = 0;.  }else{
1642d 0a 20 20 20 20 70 50 67 20 3d 20 70 61 67 65 72  .    pPg = pager
1642e 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20  _lookup(pPager, 
1642f 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 61 73 73  pgno);.  }.  ass
16430 65 72 74 28 20 70 50 67 20 7c 7c 20 21 4d 45 4d  ert( pPg || !MEM
16431 44 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  DB );.  assert( 
16432 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d  pPager->eState!=
16433 50 41 47 45 52 5f 4f 50 45 4e 20 7c 7c 20 70 50  PAGER_OPEN || pP
16434 67 3d 3d 30 20 29 3b 0a 20 20 50 41 47 45 52 54  g==0 );.  PAGERT
16435 52 41 43 45 28 28 22 50 4c 41 59 42 41 43 4b 20  RACE(("PLAYBACK 
16436 25 64 20 70 61 67 65 20 25 64 20 68 61 73 68 28  %d page %d hash(
16437 25 30 38 78 29 20 25 73 5c 6e 22 2c 0a 20 20 20  %08x) %s\n",.   
16438 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28          PAGERID(
16439 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70  pPager), pgno, p
1643a 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50  ager_datahash(pP
1643b 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
1643c 28 75 38 2a 29 61 44 61 74 61 29 2c 0a 20 20 20  (u8*)aData),.   
1643d 20 20 20 20 20 20 20 20 28 69 73 4d 61 69 6e 4a          (isMainJ
1643e 72 6e 6c 3f 22 6d 61 69 6e 2d 6a 6f 75 72 6e 61  rnl?"main-journa
1643f 6c 22 3a 22 73 75 62 2d 6a 6f 75 72 6e 61 6c 22  l":"sub-journal"
16440 29 0a 20 20 29 29 3b 0a 20 20 69 66 28 20 69 73  ).  ));.  if( is
16441 4d 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 20  MainJrnl ){.    
16442 69 73 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65  isSynced = pPage
16443 72 2d 3e 6e 6f 53 79 6e 63 20 7c 7c 20 28 2a 70  r->noSync || (*p
16444 4f 66 66 73 65 74 20 3c 3d 20 70 50 61 67 65 72  Offset <= pPager
16445 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 29 3b 0a 20  ->journalHdr);. 
16446 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 73 53 79   }else{.    isSy
16447 6e 63 65 64 20 3d 20 28 70 50 67 3d 3d 30 20 7c  nced = (pPg==0 |
16448 7c 20 30 3d 3d 28 70 50 67 2d 3e 66 6c 61 67 73  | 0==(pPg->flags
16449 20 26 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59   & PGHDR_NEED_SY
1644a 4e 43 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  NC));.  }.  if( 
1644b 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
1644c 64 29 0a 20 20 20 26 26 20 28 70 50 61 67 65 72  d).   && (pPager
1644d 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f  ->eState>=PAGER_
1644e 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 7c 7c 20  WRITER_DBMOD || 
1644f 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d  pPager->eState==
16450 50 41 47 45 52 5f 4f 50 45 4e 29 0a 20 20 20 26  PAGER_OPEN).   &
16451 26 20 69 73 53 79 6e 63 65 64 0a 20 20 29 7b 0a  & isSynced.  ){.
16452 20 20 20 20 69 36 34 20 6f 66 73 74 20 3d 20 28      i64 ofst = (
16453 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61  pgno-1)*(i64)pPa
16454 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20  ger->pageSize;. 
16455 20 20 20 74 65 73 74 63 61 73 65 28 20 21 69 73     testcase( !is
16456 53 61 76 65 70 6e 74 20 26 26 20 70 50 67 21 3d  Savepnt && pPg!=
16457 30 20 26 26 20 28 70 50 67 2d 3e 66 6c 61 67 73  0 && (pPg->flags
16458 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
16459 29 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  )!=0 );.    asse
1645a 72 74 28 20 21 70 61 67 65 72 55 73 65 57 61 6c  rt( !pagerUseWal
1645b 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20  (pPager) );.    
1645c 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
1645d 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ite(pPager->fd, 
1645e 28 75 38 2a 29 61 44 61 74 61 2c 20 70 50 61 67  (u8*)aData, pPag
1645f 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66  er->pageSize, of
16460 73 74 29 3b 0a 20 20 20 20 69 66 28 20 70 67 6e  st);.    if( pgn
16461 6f 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  o>pPager->dbFile
16462 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 70 50  Size ){.      pP
16463 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65  ager->dbFileSize
16464 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 7d 0a 20   = pgno;.    }. 
16465 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70     if( pPager->p
16466 42 61 63 6b 75 70 20 29 7b 0a 20 20 20 20 20 20  Backup ){.      
16467 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 61  CODEC1(pPager, a
16468 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33 2c 20 72  Data, pgno, 3, r
16469 63 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b  c=SQLITE_NOMEM);
1646a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 61  .      sqlite3Ba
1646b 63 6b 75 70 55 70 64 61 74 65 28 70 50 61 67 65  ckupUpdate(pPage
1646c 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e 6f  r->pBackup, pgno
1646d 2c 20 28 75 38 2a 29 61 44 61 74 61 29 3b 0a 20  , (u8*)aData);. 
1646e 20 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67       CODEC2(pPag
1646f 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e 6f 2c  er, aData, pgno,
16470 20 37 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e 4f   7, rc=SQLITE_NO
16471 4d 45 4d 2c 20 61 44 61 74 61 29 3b 0a 20 20 20  MEM, aData);.   
16472 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21   }.  }else if( !
16473 69 73 4d 61 69 6e 4a 72 6e 6c 20 26 26 20 70 50  isMainJrnl && pP
16474 67 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49  g==0 ){.    /* I
16475 66 20 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c  f this is a roll
16476 62 61 63 6b 20 6f 66 20 61 20 73 61 76 65 70 6f  back of a savepo
16477 69 6e 74 20 61 6e 64 20 64 61 74 61 20 77 61 73  int and data was
16478 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 0a   not written to.
16479 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62      ** the datab
1647a 61 73 65 20 61 6e 64 20 74 68 65 20 70 61 67 65  ase and the page
1647b 20 69 73 20 6e 6f 74 20 69 6e 2d 6d 65 6d 6f 72   is not in-memor
1647c 79 2c 20 74 68 65 72 65 20 69 73 20 61 20 70 6f  y, there is a po
1647d 74 65 6e 74 69 61 6c 0a 20 20 20 20 2a 2a 20 70  tential.    ** p
1647e 72 6f 62 6c 65 6d 2e 20 57 68 65 6e 20 74 68 65  roblem. When the
1647f 20 70 61 67 65 20 69 73 20 6e 65 78 74 20 66 65   page is next fe
16480 74 63 68 65 64 20 62 79 20 74 68 65 20 62 2d 74  tched by the b-t
16481 72 65 65 20 6c 61 79 65 72 2c 20 69 74 20 0a 20  ree layer, it . 
16482 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 72 65     ** will be re
16483 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ad from the data
16484 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68  base file, which
16485 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20   may or may not 
16486 62 65 20 0a 20 20 20 20 2a 2a 20 63 75 72 72 65  be .    ** curre
16487 6e 74 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20  nt. .    **.    
16488 2a 2a 20 54 68 65 72 65 20 61 72 65 20 61 20 63  ** There are a c
16489 6f 75 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65  ouple of differe
1648a 6e 74 20 77 61 79 73 20 74 68 69 73 20 63 61 6e  nt ways this can
1648b 20 68 61 70 70 65 6e 2e 20 41 6c 6c 20 61 72 65   happen. All are
1648c 20 71 75 69 74 65 0a 20 20 20 20 2a 2a 20 6f 62   quite.    ** ob
1648d 73 63 75 72 65 2e 20 57 68 65 6e 20 72 75 6e 6e  scure. When runn
1648e 69 6e 67 20 69 6e 20 73 79 6e 63 68 72 6f 6e 6f  ing in synchrono
1648f 75 73 20 6d 6f 64 65 2c 20 74 68 69 73 20 63 61  us mode, this ca
16490 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 0a 20  n only happen . 
16491 20 20 20 2a 2a 20 69 66 20 74 68 65 20 70 61 67     ** if the pag
16492 65 20 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65  e is on the free
16493 2d 6c 69 73 74 20 61 74 20 74 68 65 20 73 74 61  -list at the sta
16494 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61  rt of the transa
16495 63 74 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20  ction, then.    
16496 2a 2a 20 70 6f 70 75 6c 61 74 65 64 2c 20 74 68  ** populated, th
16497 65 6e 20 6d 6f 76 65 64 20 75 73 69 6e 67 20 73  en moved using s
16498 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70  qlite3PagerMovep
16499 61 67 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20  age()..    **.  
1649a 20 20 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f    ** The solutio
1649b 6e 20 69 73 20 74 6f 20 61 64 64 20 61 6e 20 69  n is to add an i
1649c 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 74 6f  n-memory page to
1649d 20 74 68 65 20 63 61 63 68 65 20 63 6f 6e 74 61   the cache conta
1649e 69 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 74 68 65  ining.    ** the
1649f 20 64 61 74 61 20 6a 75 73 74 20 72 65 61 64 20   data just read 
164a0 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75  from the sub-jou
164a1 72 6e 61 6c 2e 20 4d 61 72 6b 20 74 68 65 20 70  rnal. Mark the p
164a2 61 67 65 20 61 73 20 64 69 72 74 79 20 0a 20 20  age as dirty .  
164a3 20 20 2a 2a 20 61 6e 64 20 69 66 20 74 68 65 20    ** and if the 
164a4 70 61 67 65 72 20 72 65 71 75 69 72 65 73 20 61  pager requires a
164a5 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 2c 20 74   journal-sync, t
164a6 68 65 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67  hen mark the pag
164a7 65 20 61 73 20 0a 20 20 20 20 2a 2a 20 72 65 71  e as .    ** req
164a8 75 69 72 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c  uiring a journal
164a9 2d 73 79 6e 63 20 62 65 66 6f 72 65 20 69 74 20  -sync before it 
164aa 69 73 20 77 72 69 74 74 65 6e 2e 0a 20 20 20 20  is written..    
164ab 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  */.    assert( i
164ac 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20  sSavepnt );.    
164ad 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
164ae 64 6f 4e 6f 74 53 70 69 6c 6c 3d 3d 30 20 29 3b  doNotSpill==0 );
164af 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e  .    pPager->doN
164b0 6f 74 53 70 69 6c 6c 2b 2b 3b 0a 20 20 20 20 72  otSpill++;.    r
164b1 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
164b2 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c 20  Acquire(pPager, 
164b3 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 3b 0a  pgno, &pPg, 1);.
164b4 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
164b5 65 72 2d 3e 64 6f 4e 6f 74 53 70 69 6c 6c 3d 3d  er->doNotSpill==
164b6 31 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  1 );.    pPager-
164b7 3e 64 6f 4e 6f 74 53 70 69 6c 6c 2d 2d 3b 0a 20  >doNotSpill--;. 
164b8 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
164b9 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
164ba 3b 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73  ;.    pPg->flags
164bb 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f   &= ~PGHDR_NEED_
164bc 52 45 41 44 3b 0a 20 20 20 20 73 71 6c 69 74 65  READ;.    sqlite
164bd 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79  3PcacheMakeDirty
164be 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28  (pPg);.  }.  if(
164bf 20 70 50 67 20 29 7b 0a 20 20 20 20 2f 2a 20 4e   pPg ){.    /* N
164c0 6f 20 70 61 67 65 20 73 68 6f 75 6c 64 20 65 76  o page should ev
164c1 65 72 20 62 65 20 65 78 70 6c 69 63 69 74 6c 79  er be explicitly
164c2 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 61   rolled back tha
164c3 74 20 69 73 20 69 6e 20 75 73 65 2c 20 65 78 63  t is in use, exc
164c4 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 70  ept.    ** for p
164c5 61 67 65 20 31 20 77 68 69 63 68 20 69 73 20 68  age 1 which is h
164c6 65 6c 64 20 69 6e 20 75 73 65 20 69 6e 20 6f 72  eld in use in or
164c7 64 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20  der to keep the 
164c8 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20  lock on the.    
164c9 2a 2a 20 64 61 74 61 62 61 73 65 20 61 63 74 69  ** database acti
164ca 76 65 2e 20 48 6f 77 65 76 65 72 20 73 75 63 68  ve. However such
164cb 20 61 20 70 61 67 65 20 6d 61 79 20 62 65 20 72   a page may be r
164cc 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 61 20  olled back as a 
164cd 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 6f 66  result.    ** of
164ce 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 72 72   an internal err
164cf 6f 72 20 72 65 73 75 6c 74 69 6e 67 20 69 6e 20  or resulting in 
164d0 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 63 61 6c  an automatic cal
164d1 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69  l to.    ** sqli
164d2 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
164d3 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 76  ()..    */.    v
164d4 6f 69 64 20 2a 70 44 61 74 61 3b 0a 20 20 20 20  oid *pData;.    
164d5 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61  pData = pPg->pDa
164d6 74 61 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70  ta;.    memcpy(p
164d7 44 61 74 61 2c 20 28 75 38 2a 29 61 44 61 74 61  Data, (u8*)aData
164d8 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
164d9 7a 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  ze);.    pPager-
164da 3e 78 52 65 69 6e 69 74 65 72 28 70 50 67 29 3b  >xReiniter(pPg);
164db 0a 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e 4a  .    if( isMainJ
164dc 72 6e 6c 20 26 26 20 28 21 69 73 53 61 76 65 70  rnl && (!isSavep
164dd 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74 3c 3d  nt || *pOffset<=
164de 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48  pPager->journalH
164df 64 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  dr) ){.      /* 
164e0 49 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  If the contents 
164e1 6f 66 20 74 68 69 73 20 70 61 67 65 20 77 65 72  of this page wer
164e2 65 20 6a 75 73 74 20 72 65 73 74 6f 72 65 64 20  e just restored 
164e3 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 0a 20  from the main . 
164e4 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20       ** journal 
164e5 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 20 63  file, then its c
164e6 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 61  ontent must be a
164e7 73 20 74 68 65 79 20 77 65 72 65 20 77 68 65 6e  s they were when
164e8 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74   the .      ** t
164e9 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 66  ransaction was f
164ea 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 6e 20  irst opened. In 
164eb 74 68 69 73 20 63 61 73 65 20 77 65 20 63 61 6e  this case we can
164ec 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 0a 20   mark the page. 
164ed 20 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 61 6e       ** as clean
164ee 2c 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 69  , since there wi
164ef 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 74 6f  ll be no need to
164f0 20 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 6f   write it out to
164f1 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61   the.      ** da
164f2 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 2a 2a  tabase..      **
164f3 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20  .      ** There 
164f4 69 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e  is one exception
164f5 20 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20 49   to this rule. I
164f6 66 20 74 68 65 20 70 61 67 65 20 69 73 20 62 65  f the page is be
164f7 69 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20  ing rolled.     
164f8 20 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72 74   ** back as part
164f9 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20   of a savepoint 
164fa 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20 72  (or statement) r
164fb 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20  ollback from an 
164fc 0a 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63  .      ** unsync
164fd 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68  ed portion of th
164fe 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66  e main journal f
164ff 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20  ile, then it is 
16500 6e 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20 2a  not safe.      *
16501 2a 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 61  * to mark the pa
16502 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68 69  ge as clean. Thi
16503 73 20 69 73 20 62 65 63 61 75 73 65 20 6d 61 72  s is because mar
16504 6b 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 73  king the page as
16505 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20  .      ** clean 
16506 77 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20 50  will clear the P
16507 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66  GHDR_NEED_SYNC f
16508 6c 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20 70  lag. Since the p
16509 61 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20  age is.      ** 
1650a 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 6a  already in the j
1650b 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65 63  ournal file (rec
1650c 6f 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e 70  orded in Pager.p
1650d 49 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20  InJournal) and. 
1650e 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44       ** the PGHD
1650f 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
16510 20 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66 20   is cleared, if 
16511 74 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74  the page is writ
16512 74 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  ten to.      ** 
16513 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 69  again within thi
16514 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69  s transaction, i
16515 74 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64  t will be marked
16516 20 61 73 20 64 69 72 74 79 20 62 75 74 0a 20 20   as dirty but.  
16517 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52      ** the PGHDR
16518 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20  _NEED_SYNC flag 
16519 77 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e  will not be set.
1651a 20 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20 70   It could then p
1651b 6f 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20  otentially.     
1651c 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 6f   ** be written o
1651d 75 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  ut into the data
1651e 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65  base file before
1651f 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   its journal fil
16520 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65  e.      ** segme
16521 6e 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49 66  nt is synced. If
16522 20 61 20 63 72 61 73 68 20 6f 63 63 75 72 73 20   a crash occurs 
16523 64 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77  during or follow
16524 69 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20 20  ing this,.      
16525 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  ** database corr
16526 75 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65  uption may ensue
16527 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
16528 20 61 73 73 65 72 74 28 20 21 70 61 67 65 72 55   assert( !pagerU
16529 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 3b  seWal(pPager) );
1652a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 63  .      sqlite3Pc
1652b 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 50  acheMakeClean(pP
1652c 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61  g);.    }.    pa
1652d 67 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68  ger_set_pagehash
1652e 28 70 50 67 29 3b 0a 0a 20 20 20 20 2f 2a 20 49  (pPg);..    /* I
1652f 66 20 74 68 69 73 20 77 61 73 20 70 61 67 65 20  f this was page 
16530 31 2c 20 74 68 65 6e 20 72 65 73 74 6f 72 65 20  1, then restore 
16531 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61 67  the value of Pag
16532 65 72 2e 64 62 46 69 6c 65 56 65 72 73 2e 0a 20  er.dbFileVers.. 
16533 20 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 62 65     ** Do this be
16534 66 6f 72 65 20 61 6e 79 20 64 65 63 6f 64 69 6e  fore any decodin
16535 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 67  g. */.    if( pg
16536 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 6d  no==1 ){.      m
16537 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64  emcpy(&pPager->d
16538 62 46 69 6c 65 56 65 72 73 2c 20 26 28 28 75 38  bFileVers, &((u8
16539 2a 29 70 44 61 74 61 29 5b 32 34 5d 2c 73 69 7a  *)pData)[24],siz
1653a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69  eof(pPager->dbFi
1653b 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 7d 0a  leVers));.    }.
1653c 0a 20 20 20 20 2f 2a 20 44 65 63 6f 64 65 20 74  .    /* Decode t
1653d 68 65 20 70 61 67 65 20 6a 75 73 74 20 72 65 61  he page just rea
1653e 64 20 66 72 6f 6d 20 64 69 73 6b 20 2a 2f 0a 20  d from disk */. 
1653f 20 20 20 43 4f 44 45 43 31 28 70 50 61 67 65 72     CODEC1(pPager
16540 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67  , pData, pPg->pg
16541 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54 45  no, 3, rc=SQLITE
16542 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 73 71 6c  _NOMEM);.    sql
16543 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73  ite3PcacheReleas
16544 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65  e(pPg);.  }.  re
16545 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
16546 2a 20 50 61 72 61 6d 65 74 65 72 20 7a 4d 61 73  * Parameter zMas
16547 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ter is the name 
16548 6f 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  of a master jour
16549 6e 61 6c 20 66 69 6c 65 2e 20 41 20 73 69 6e 67  nal file. A sing
1654a 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69  le journal.** fi
1654b 6c 65 20 74 68 61 74 20 72 65 66 65 72 72 65 64  le that referred
1654c 20 74 6f 20 74 68 65 20 6d 61 73 74 65 72 20 6a   to the master j
1654d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20  ournal file has 
1654e 6a 75 73 74 20 62 65 65 6e 20 72 6f 6c 6c 65 64  just been rolled
1654f 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 69 73 20 72   back..** This r
16550 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66  outine checks if
16551 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   it is possible 
16552 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61  to delete the ma
16553 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
16554 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65 73 20 73  e,.** and does s
16555 6f 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a  o if it is..**.*
16556 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4d 61 73 74  * Argument zMast
16557 65 72 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20  er may point to 
16558 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 2e  Pager.pTmpSpace.
16559 20 53 6f 20 74 68 61 74 20 62 75 66 66 65 72 20   So that buffer 
1655a 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 76 61 69 6c  is not .** avail
1655b 61 62 6c 65 20 66 6f 72 20 75 73 65 20 77 69 74  able for use wit
1655c 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hin this functio
1655d 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20  n..**.** When a 
1655e 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
1655f 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c 20  ile is created, 
16560 69 74 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20  it is populated 
16561 77 69 74 68 20 74 68 65 20 6e 61 6d 65 73 20 0a  with the names .
16562 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 20 69 74 73  ** of all of its
16563 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 73 2c   child journals,
16564 20 6f 6e 65 20 61 66 74 65 72 20 61 6e 6f 74 68   one after anoth
16565 65 72 2c 20 66 6f 72 6d 61 74 74 65 64 20 61 73  er, formatted as
16566 20 75 74 66 2d 38 20 0a 2a 2a 20 65 6e 63 6f 64   utf-8 .** encod
16567 65 64 20 74 65 78 74 2e 20 54 68 65 20 65 6e 64  ed text. The end
16568 20 6f 66 20 65 61 63 68 20 63 68 69 6c 64 20 6a   of each child j
16569 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6d  ournal file is m
1656a 61 72 6b 65 64 20 77 69 74 68 20 61 20 0a 2a 2a  arked with a .**
1656b 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20   nul-terminator 
1656c 62 79 74 65 20 28 30 78 30 30 29 2e 20 69 2e 65  byte (0x00). i.e
1656d 2e 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e  . the entire con
1656e 74 65 6e 74 73 20 6f 66 20 61 20 6d 61 73 74 65  tents of a maste
1656f 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c  r journal.** fil
16570 65 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74  e for a transact
16571 69 6f 6e 20 69 6e 76 6f 6c 76 69 6e 67 20 74 77  ion involving tw
16572 6f 20 64 61 74 61 62 61 73 65 73 20 6d 69 67 68  o databases migh
16573 74 20 62 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 2f  t be:.**.**   "/
16574 68 6f 6d 65 2f 62 69 6c 6c 2f 61 2e 64 62 2d 6a  home/bill/a.db-j
16575 6f 75 72 6e 61 6c 5c 78 30 30 2f 68 6f 6d 65 2f  ournal\x00/home/
16576 62 69 6c 6c 2f 62 2e 64 62 2d 6a 6f 75 72 6e 61  bill/b.db-journa
16577 6c 5c 78 30 30 22 0a 2a 2a 0a 2a 2a 20 41 20 6d  l\x00".**.** A m
16578 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
16579 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 64  le may only be d
1657a 65 6c 65 74 65 64 20 6f 6e 63 65 20 61 6c 6c 20  eleted once all 
1657b 6f 66 20 69 74 73 20 63 68 69 6c 64 20 0a 2a 2a  of its child .**
1657c 20 6a 6f 75 72 6e 61 6c 73 20 68 61 76 65 20 62   journals have b
1657d 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e  een rolled back.
1657e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
1657f 74 69 6f 6e 20 72 65 61 64 73 20 74 68 65 20 63  tion reads the c
16580 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6d  ontents of the m
16581 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69  aster-journal fi
16582 6c 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d 65 6d 6f  le into .** memo
16583 72 79 20 61 6e 64 20 6c 6f 6f 70 73 20 74 68 72  ry and loops thr
16584 6f 75 67 68 20 65 61 63 68 20 6f 66 20 74 68 65  ough each of the
16585 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 6e   child journal n
16586 61 6d 65 73 2e 20 46 6f 72 0a 2a 2a 20 65 61 63  ames. For.** eac
16587 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 2c  h child journal,
16588 20 69 74 20 63 68 65 63 6b 73 20 69 66 3a 0a 2a   it checks if:.*
16589 2a 0a 2a 2a 20 20 20 2a 20 69 66 20 74 68 65 20  *.**   * if the 
1658a 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 65 78  child journal ex
1658b 69 73 74 73 2c 20 61 6e 64 20 69 66 20 73 6f 0a  ists, and if so.
1658c 2a 2a 20 20 20 2a 20 69 66 20 74 68 65 20 63 68  **   * if the ch
1658d 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74  ild journal cont
1658e 61 69 6e 73 20 61 20 72 65 66 65 72 65 6e 63 65  ains a reference
1658f 20 74 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   to master journ
16590 61 6c 20 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20  al .**     file 
16591 7a 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a 20 49 66  zMaster.**.** If
16592 20 61 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c   a child journal
16593 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 20 74 68   can be found th
16594 61 74 20 6d 61 74 63 68 65 73 20 62 6f 74 68 20  at matches both 
16595 6f 66 20 74 68 65 20 63 72 69 74 65 72 69 61 0a  of the criteria.
16596 2a 2a 20 61 62 6f 76 65 2c 20 74 68 69 73 20 66  ** above, this f
16597 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
16598 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e  without doing an
16599 79 74 68 69 6e 67 2e 20 4f 74 68 65 72 77 69 73  ything. Otherwis
1659a 65 2c 20 69 66 0a 2a 2a 20 6e 6f 20 73 75 63 68  e, if.** no such
1659b 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63   child journal c
1659c 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 66 69 6c  an be found, fil
1659d 65 20 7a 4d 61 73 74 65 72 20 69 73 20 64 65 6c  e zMaster is del
1659e 65 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  eted from.** the
1659f 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 75 73 69   file-system usi
165a0 6e 67 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65  ng sqlite3OsDele
165a1 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  te()..**.** If a
165a2 6e 20 49 4f 20 65 72 72 6f 72 20 77 69 74 68 69  n IO error withi
165a3 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c  n this function,
165a4 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
165a5 73 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73  s returned. This
165a6 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c  .** function all
165a7 6f 63 61 74 65 73 20 6d 65 6d 6f 72 79 20 62 79  ocates memory by
165a8 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33   calling sqlite3
165a9 4d 61 6c 6c 6f 63 28 29 2e 20 49 66 20 61 6e 20  Malloc(). If an 
165aa 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 66 61  allocation.** fa
165ab 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  ils, SQLITE_NOME
165ac 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f  M is returned. O
165ad 74 68 65 72 77 69 73 65 2c 20 69 66 20 6e 6f 20  therwise, if no 
165ae 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 20 65 72 72  IO or malloc err
165af 6f 72 73 20 0a 2a 2a 20 6f 63 63 75 72 2c 20 53  ors .** occur, S
165b0 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
165b1 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f  rned..**.** TODO
165b2 3a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  : This function 
165b3 61 6c 6c 6f 63 61 74 65 73 20 61 20 73 69 6e 67  allocates a sing
165b4 6c 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f  le block of memo
165b5 72 79 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20 74 68  ry to load.** th
165b6 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74  e entire content
165b7 73 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20  s of the master 
165b8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68  journal file. Th
165b9 69 73 20 63 6f 75 6c 64 20 62 65 0a 2a 2a 20 61  is could be.** a
165ba 20 63 6f 75 70 6c 65 20 6f 66 20 6b 69 6c 6f 62   couple of kilob
165bb 79 74 65 73 20 6f 72 20 73 6f 20 2d 20 70 6f 74  ytes or so - pot
165bc 65 6e 74 69 61 6c 6c 79 20 6c 61 72 67 65 72 20  entially larger 
165bd 74 68 61 6e 20 74 68 65 20 70 61 67 65 20 0a 2a  than the page .*
165be 2a 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69  * size..*/.stati
165bf 63 20 69 6e 74 20 70 61 67 65 72 5f 64 65 6c 6d  c int pager_delm
165c0 61 73 74 65 72 28 50 61 67 65 72 20 2a 70 50 61  aster(Pager *pPa
165c1 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ger, const char 
165c2 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 73 71 6c  *zMaster){.  sql
165c3 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d  ite3_vfs *pVfs =
165c4 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20   pPager->pVfs;. 
165c5 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
165c6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
165c7 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73  turn code */.  s
165c8 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61  qlite3_file *pMa
165c9 73 74 65 72 3b 20 20 20 20 2f 2a 20 4d 61 6c 6c  ster;    /* Mall
165ca 6f 63 27 64 20 6d 61 73 74 65 72 2d 6a 6f 75 72  oc'd master-jour
165cb 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70  nal file descrip
165cc 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  tor */.  sqlite3
165cd 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 6e 61 6c 3b  _file *pJournal;
165ce 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 63     /* Malloc'd c
165cf 68 69 6c 64 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c  hild-journal fil
165d0 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a  e descriptor */.
165d1 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 4a    char *zMasterJ
165d2 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f 2a 20 43  ournal = 0; /* C
165d3 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61 73 74 65  ontents of maste
165d4 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a  r journal file *
165d5 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74 65 72 4a  /.  i64 nMasterJ
165d6 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 2f 2a  ournal;       /*
165d7 20 53 69 7a 65 20 6f 66 20 6d 61 73 74 65 72 20   Size of master 
165d8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
165d9 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c    char *zJournal
165da 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ;           /* P
165db 6f 69 6e 74 65 72 20 74 6f 20 6f 6e 65 20 6a 6f  ointer to one jo
165dc 75 72 6e 61 6c 20 77 69 74 68 69 6e 20 4d 4a 20  urnal within MJ 
165dd 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  file */.  char *
165de 7a 4d 61 73 74 65 72 50 74 72 3b 20 20 20 20 20  zMasterPtr;     
165df 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20      /* Space to 
165e0 68 6f 6c 64 20 4d 4a 20 66 69 6c 65 6e 61 6d 65  hold MJ filename
165e1 20 66 72 6f 6d 20 61 20 6a 6f 75 72 6e 61 6c 20   from a journal 
165e2 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  file */.  int nM
165e3 61 73 74 65 72 50 74 72 3b 20 20 20 20 20 20 20  asterPtr;       
165e4 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66      /* Amount of
165e5 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64   space allocated
165e6 20 74 6f 20 7a 4d 61 73 74 65 72 50 74 72 5b 5d   to zMasterPtr[]
165e7 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61   */..  /* Alloca
165e8 74 65 20 73 70 61 63 65 20 66 6f 72 20 62 6f 74  te space for bot
165e9 68 20 74 68 65 20 70 4a 6f 75 72 6e 61 6c 20 61  h the pJournal a
165ea 6e 64 20 70 4d 61 73 74 65 72 20 66 69 6c 65 20  nd pMaster file 
165eb 64 65 73 63 72 69 70 74 6f 72 73 2e 0a 20 20 2a  descriptors..  *
165ec 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  * If successful,
165ed 20 6f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72   open the master
165ee 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f   journal file fo
165ef 72 20 72 65 61 64 69 6e 67 2e 0a 20 20 2a 2f 0a  r reading..  */.
165f0 20 20 70 4d 61 73 74 65 72 20 3d 20 28 73 71 6c    pMaster = (sql
165f1 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69  ite3_file *)sqli
165f2 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56  te3MallocZero(pV
165f3 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32  fs->szOsFile * 2
165f4 29 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20  );.  pJournal = 
165f5 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
165f6 28 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29  (((u8 *)pMaster)
165f7 20 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c   + pVfs->szOsFil
165f8 65 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74  e);.  if( !pMast
165f9 65 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  er ){.    rc = S
165fa 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
165fb 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20  else{.    const 
165fc 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c  int flags = (SQL
165fd 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
165fe 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  Y|SQLITE_OPEN_MA
165ff 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20  STER_JOURNAL);. 
16600 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
16601 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73  sOpen(pVfs, zMas
16602 74 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c  ter, pMaster, fl
16603 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69  ags, 0);.  }.  i
16604 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
16605 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65   ) goto delmaste
16606 72 5f 6f 75 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 61  r_out;..  /* Loa
16607 64 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73  d the entire mas
16608 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
16609 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61   into space obta
1660a 69 6e 65 64 20 66 72 6f 6d 0a 20 20 2a 2a 20 73  ined from.  ** s
1660b 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
1660c 61 6e 64 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  and pointed to b
1660d 79 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  y zMasterJournal
1660e 2e 20 20 20 41 6c 73 6f 20 6f 62 74 61 69 6e 0a  .   Also obtain.
1660f 20 20 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20    ** sufficient 
16610 73 70 61 63 65 20 28 69 6e 20 7a 4d 61 73 74 65  space (in zMaste
16611 72 50 74 72 29 20 74 6f 20 68 6f 6c 64 20 74 68  rPtr) to hold th
16612 65 20 6e 61 6d 65 73 20 6f 66 20 6d 61 73 74 65  e names of maste
16613 72 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  r.  ** journal f
16614 69 6c 65 73 20 65 78 74 72 61 63 74 65 64 20 66  iles extracted f
16615 72 6f 6d 20 72 65 67 75 6c 61 72 20 72 6f 6c 6c  rom regular roll
16616 62 61 63 6b 2d 6a 6f 75 72 6e 61 6c 73 2e 0a 20  back-journals.. 
16617 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
16618 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4d 61  e3OsFileSize(pMa
16619 73 74 65 72 2c 20 26 6e 4d 61 73 74 65 72 4a 6f  ster, &nMasterJo
1661a 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28 20 72 63  urnal);.  if( rc
1661b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
1661c 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74  to delmaster_out
1661d 3b 0a 20 20 6e 4d 61 73 74 65 72 50 74 72 20 3d  ;.  nMasterPtr =
1661e 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d   pVfs->mxPathnam
1661f 65 2b 31 3b 0a 20 20 7a 4d 61 73 74 65 72 4a 6f  e+1;.  zMasterJo
16620 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 4d  urnal = sqlite3M
16621 61 6c 6c 6f 63 28 28 69 6e 74 29 6e 4d 61 73 74  alloc((int)nMast
16622 65 72 4a 6f 75 72 6e 61 6c 20 2b 20 6e 4d 61 73  erJournal + nMas
16623 74 65 72 50 74 72 20 2b 20 31 29 3b 0a 20 20 69  terPtr + 1);.  i
16624 66 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e  f( !zMasterJourn
16625 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  al ){.    rc = S
16626 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
16627 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f   goto delmaster_
16628 6f 75 74 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74  out;.  }.  zMast
16629 65 72 50 74 72 20 3d 20 26 7a 4d 61 73 74 65 72  erPtr = &zMaster
1662a 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a  Journal[nMasterJ
1662b 6f 75 72 6e 61 6c 2b 31 5d 3b 0a 20 20 72 63 20  ournal+1];.  rc 
1662c 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
1662d 70 4d 61 73 74 65 72 2c 20 7a 4d 61 73 74 65 72  pMaster, zMaster
1662e 4a 6f 75 72 6e 61 6c 2c 20 28 69 6e 74 29 6e 4d  Journal, (int)nM
1662f 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 30 29  asterJournal, 0)
16630 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
16631 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c  TE_OK ) goto del
16632 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 7a 4d  master_out;.  zM
16633 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61  asterJournal[nMa
16634 73 74 65 72 4a 6f 75 72 6e 61 6c 5d 20 3d 20 30  sterJournal] = 0
16635 3b 0a 0a 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20  ;..  zJournal = 
16636 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a  zMasterJournal;.
16637 20 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 6e    while( (zJourn
16638 61 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61  al-zMasterJourna
16639 6c 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61  l)<nMasterJourna
1663a 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 78 69  l ){.    int exi
1663b 73 74 73 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  sts;.    rc = sq
1663c 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56  lite3OsAccess(pV
1663d 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51  fs, zJournal, SQ
1663e 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53  LITE_ACCESS_EXIS
1663f 54 53 2c 20 26 65 78 69 73 74 73 29 3b 0a 20 20  TS, &exists);.  
16640 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16641 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  _OK ){.      got
16642 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b  o delmaster_out;
16643 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65  .    }.    if( e
16644 78 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 2f  xists ){.      /
16645 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75  * One of the jou
16646 72 6e 61 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f  rnals pointed to
16647 20 62 79 20 74 68 65 20 6d 61 73 74 65 72 20 6a   by the master j
16648 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 2e 0a 20  ournal exists.. 
16649 20 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 20       ** Open it 
1664a 61 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74 20  and check if it 
1664b 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d 61  points at the ma
1664c 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 66  ster journal. If
1664d 0a 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 72 65  .      ** so, re
1664e 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 65 6c  turn without del
1664f 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72  eting the master
16650 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20   journal file.. 
16651 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e       */.      in
16652 74 20 63 3b 0a 20 20 20 20 20 20 69 6e 74 20 66  t c;.      int f
16653 6c 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f  lags = (SQLITE_O
16654 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c  PEN_READONLY|SQL
16655 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
16656 55 52 4e 41 4c 29 3b 0a 20 20 20 20 20 20 72 63  URNAL);.      rc
16657 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e   = sqlite3OsOpen
16658 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c  (pVfs, zJournal,
16659 20 70 4a 6f 75 72 6e 61 6c 2c 20 66 6c 61 67 73   pJournal, flags
1665a 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
1665b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1665c 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65  .        goto de
1665d 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20  lmaster_out;.   
1665e 20 20 20 7d 0a 0a 20 20 20 20 20 20 72 63 20 3d     }..      rc =
1665f 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e   readMasterJourn
16660 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 7a 4d 61  al(pJournal, zMa
16661 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 74 65 72  sterPtr, nMaster
16662 50 74 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Ptr);.      sqli
16663 74 65 33 4f 73 43 6c 6f 73 65 28 70 4a 6f 75 72  te3OsClose(pJour
16664 6e 61 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20  nal);.      if( 
16665 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
16666 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65  .        goto de
16667 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20  lmaster_out;.   
16668 20 20 20 7d 0a 0a 20 20 20 20 20 20 63 20 3d 20     }..      c = 
16669 7a 4d 61 73 74 65 72 50 74 72 5b 30 5d 21 3d 30  zMasterPtr[0]!=0
1666a 20 26 26 20 73 74 72 63 6d 70 28 7a 4d 61 73 74   && strcmp(zMast
1666b 65 72 50 74 72 2c 20 7a 4d 61 73 74 65 72 29 3d  erPtr, zMaster)=
1666c 3d 30 3b 0a 20 20 20 20 20 20 69 66 28 20 63 20  =0;.      if( c 
1666d 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65  ){.        /* We
1666e 20 68 61 76 65 20 61 20 6d 61 74 63 68 2e 20 44   have a match. D
1666f 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65  o not delete the
16670 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
16671 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20  file. */.       
16672 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f   goto delmaster_
16673 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  out;.      }.   
16674 20 7d 0a 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20   }.    zJournal 
16675 2b 3d 20 28 73 71 6c 69 74 65 33 53 74 72 6c 65  += (sqlite3Strle
16676 6e 33 30 28 7a 4a 6f 75 72 6e 61 6c 29 2b 31 29  n30(zJournal)+1)
16677 3b 0a 20 20 7d 0a 20 0a 20 20 73 71 6c 69 74 65  ;.  }. .  sqlite
16678 33 4f 73 43 6c 6f 73 65 28 70 4d 61 73 74 65 72  3OsClose(pMaster
16679 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1667a 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20  3OsDelete(pVfs, 
1667b 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 0a 64 65  zMaster, 0);..de
1667c 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a 20 20 73  lmaster_out:.  s
1667d 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d 61 73  qlite3_free(zMas
1667e 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69  terJournal);.  i
1667f 66 28 20 70 4d 61 73 74 65 72 20 29 7b 0a 20 20  f( pMaster ){.  
16680 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
16681 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 61  (pMaster);.    a
16682 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70  ssert( !isOpen(p
16683 4a 6f 75 72 6e 61 6c 29 20 29 3b 0a 20 20 20 20  Journal) );.    
16684 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4d 61  sqlite3_free(pMa
16685 73 74 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ster);.  }.  ret
16686 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
16687 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
16688 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e 67  is used to chang
16689 65 20 74 68 65 20 61 63 74 75 61 6c 20 73 69 7a  e the actual siz
1668a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
1668b 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e 20 74 68  e .** file in th
1668c 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 20 54  e file-system. T
1668d 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73  his only happens
1668e 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67   when committing
1668f 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a   a transaction,.
16690 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61  ** or rolling ba
16691 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ck a transaction
16692 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 6f 6c 6c   (including roll
16693 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 2d 6a  ing back a hot-j
16694 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49  ournal)..**.** I
16695 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  f the main datab
16696 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20  ase file is not 
16697 6f 70 65 6e 2c 20 6f 72 20 74 68 65 20 70 61 67  open, or the pag
16698 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 65 69 74  er is not in eit
16699 68 65 72 0a 2a 2a 20 44 42 4d 4f 44 20 6f 72 20  her.** DBMOD or 
1669a 4f 50 45 4e 20 73 74 61 74 65 2c 20 74 68 69 73  OPEN state, this
1669b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
1669c 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c  o-op. Otherwise,
1669d 20 74 68 65 20 73 69 7a 65 20 0a 2a 2a 20 6f 66   the size .** of
1669e 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 68 61   the file is cha
1669f 6e 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61  nged to nPage pa
166a0 67 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65  ges (nPage*pPage
166a1 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65  r->pageSize byte
166a2 73 29 2e 20 0a 2a 2a 20 49 66 20 74 68 65 20 66  s). .** If the f
166a3 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 63  ile on disk is c
166a4 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20  urrently larger 
166a5 74 68 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73  than nPage pages
166a6 2c 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 56  , then use the V
166a7 46 53 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65 28  FS.** xTruncate(
166a8 29 20 6d 65 74 68 6f 64 20 74 6f 20 74 72 75 6e  ) method to trun
166a9 63 61 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f  cate it..**.** O
166aa 72 2c 20 69 74 20 6d 69 67 68 74 20 6d 69 67 68  r, it might migh
166ab 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68  t be the case th
166ac 61 74 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64  at the file on d
166ad 69 73 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74  isk is smaller t
166ae 68 61 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61  han .** nPage pa
166af 67 65 73 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74  ges. Some operat
166b0 69 6e 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65  ing system imple
166b1 6d 65 6e 74 61 74 69 6f 6e 73 20 63 61 6e 20 67  mentations can g
166b2 65 74 20 63 6f 6e 66 75 73 65 64 20 69 66 20 0a  et confused if .
166b3 2a 2a 20 79 6f 75 20 74 72 79 20 74 6f 20 74 72  ** you try to tr
166b4 75 6e 63 61 74 65 20 61 20 66 69 6c 65 20 74 6f  uncate a file to
166b5 20 73 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 20   some size that 
166b6 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69  is larger than i
166b7 74 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20  t .** currently 
166b8 69 73 2c 20 73 6f 20 64 65 74 65 63 74 20 74 68  is, so detect th
166b9 69 73 20 63 61 73 65 20 61 6e 64 20 77 72 69 74  is case and writ
166ba 65 20 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20  e a single zero 
166bb 62 79 74 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20  byte to .** the 
166bc 65 6e 64 20 6f 66 20 74 68 65 20 6e 65 77 20 66  end of the new f
166bd 69 6c 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a  ile instead..**.
166be 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
166bf 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
166c0 4f 4b 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72  OK. If an IO err
166c1 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
166c2 6d 6f 64 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65  modifying.** the
166c3 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
166c4 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72  return the error
166c5 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c   code to the cal
166c6 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ler..*/.static i
166c7 6e 74 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74  nt pager_truncat
166c8 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  e(Pager *pPager,
166c9 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20   Pgno nPage){.  
166ca 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
166cb 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  OK;.  assert( pP
166cc 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d 50 41  ager->eState!=PA
166cd 47 45 52 5f 45 52 52 4f 52 20 29 3b 0a 20 20 61  GER_ERROR );.  a
166ce 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65  ssert( pPager->e
166cf 53 74 61 74 65 21 3d 50 41 47 45 52 5f 52 45 41  State!=PAGER_REA
166d0 44 45 52 20 29 3b 0a 20 20 0a 20 20 69 66 28 20  DER );.  .  if( 
166d1 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
166d2 64 29 20 0a 20 20 20 26 26 20 28 70 50 61 67 65  d) .   && (pPage
166d3 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 52  r->eState>=PAGER
166d4 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 7c 7c  _WRITER_DBMOD ||
166d5 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d   pPager->eState=
166d6 3d 50 41 47 45 52 5f 4f 50 45 4e 29 20 0a 20 20  =PAGER_OPEN) .  
166d7 29 7b 0a 20 20 20 20 69 36 34 20 63 75 72 72 65  ){.    i64 curre
166d8 6e 74 53 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b  ntSize, newSize;
166d9 0a 20 20 20 20 69 6e 74 20 73 7a 50 61 67 65 20  .    int szPage 
166da 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  = pPager->pageSi
166db 7a 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ze;.    assert( 
166dc 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 45  pPager->eLock==E
166dd 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b  XCLUSIVE_LOCK );
166de 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73  .    /* TODO: Is
166df 20 69 74 20 73 61 66 65 20 74 6f 20 75 73 65 20   it safe to use 
166e0 50 61 67 65 72 2e 64 62 46 69 6c 65 53 69 7a 65  Pager.dbFileSize
166e1 20 68 65 72 65 3f 20 2a 2f 0a 20 20 20 20 72 63   here? */.    rc
166e2 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
166e3 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c  Size(pPager->fd,
166e4 20 26 63 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a   &currentSize);.
166e5 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 73 7a      newSize = sz
166e6 50 61 67 65 2a 28 69 36 34 29 6e 50 61 67 65 3b  Page*(i64)nPage;
166e7 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
166e8 49 54 45 5f 4f 4b 20 26 26 20 63 75 72 72 65 6e  ITE_OK && curren
166e9 74 53 69 7a 65 21 3d 6e 65 77 53 69 7a 65 20 29  tSize!=newSize )
166ea 7b 0a 20 20 20 20 20 20 69 66 28 20 63 75 72 72  {.      if( curr
166eb 65 6e 74 53 69 7a 65 3e 6e 65 77 53 69 7a 65 20  entSize>newSize 
166ec 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
166ed 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74  sqlite3OsTruncat
166ee 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6e 65  e(pPager->fd, ne
166ef 77 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 65  wSize);.      }e
166f0 6c 73 65 20 69 66 28 20 28 63 75 72 72 65 6e 74  lse if( (current
166f1 53 69 7a 65 2b 73 7a 50 61 67 65 29 3c 3d 6e 65  Size+szPage)<=ne
166f2 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20  wSize ){.       
166f3 20 63 68 61 72 20 2a 70 54 6d 70 20 3d 20 70 50   char *pTmp = pP
166f4 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b  ager->pTmpSpace;
166f5 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
166f6 70 54 6d 70 2c 20 30 2c 20 73 7a 50 61 67 65 29  pTmp, 0, szPage)
166f7 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
166f8 73 65 28 20 28 6e 65 77 53 69 7a 65 2d 73 7a 50  se( (newSize-szP
166f9 61 67 65 29 20 3d 3d 20 63 75 72 72 65 6e 74 53  age) == currentS
166fa 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 20 20 74  ize );.        t
166fb 65 73 74 63 61 73 65 28 20 28 6e 65 77 53 69 7a  estcase( (newSiz
166fc 65 2d 73 7a 50 61 67 65 29 20 3e 20 20 63 75 72  e-szPage) >  cur
166fd 72 65 6e 74 53 69 7a 65 20 29 3b 0a 20 20 20 20  rentSize );.    
166fe 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
166ff 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
16700 66 64 2c 20 70 54 6d 70 2c 20 73 7a 50 61 67 65  fd, pTmp, szPage
16701 2c 20 6e 65 77 53 69 7a 65 2d 73 7a 50 61 67 65  , newSize-szPage
16702 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
16703 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
16704 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50  OK ){.        pP
16705 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65  ager->dbFileSize
16706 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20   = nPage;.      
16707 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
16708 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
16709 2a 20 52 65 74 75 72 6e 20 61 20 73 61 6e 69 74  * Return a sanit
1670a 69 7a 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  ized version of 
1670b 74 68 65 20 73 65 63 74 6f 72 2d 73 69 7a 65 20  the sector-size 
1670c 6f 66 20 4f 53 20 66 69 6c 65 20 70 46 69 6c 65  of OS file pFile
1670d 2e 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20  . The.** return 
1670e 76 61 6c 75 65 20 69 73 20 67 75 61 72 61 6e 74  value is guarant
1670f 65 65 64 20 74 6f 20 6c 69 65 20 62 65 74 77 65  eed to lie betwe
16710 65 6e 20 33 32 20 61 6e 64 20 4d 41 58 5f 53 45  en 32 and MAX_SE
16711 43 54 4f 52 5f 53 49 5a 45 2e 0a 2a 2f 0a 53 51  CTOR_SIZE..*/.SQ
16712 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
16713 20 73 71 6c 69 74 65 33 53 65 63 74 6f 72 53 69   sqlite3SectorSi
16714 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ze(sqlite3_file 
16715 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 69  *pFile){.  int i
16716 52 65 74 20 3d 20 73 71 6c 69 74 65 33 4f 73 53  Ret = sqlite3OsS
16717 65 63 74 6f 72 53 69 7a 65 28 70 46 69 6c 65 29  ectorSize(pFile)
16718 3b 0a 20 20 69 66 28 20 69 52 65 74 3c 33 32 20  ;.  if( iRet<32 
16719 29 7b 0a 20 20 20 20 69 52 65 74 20 3d 20 35 31  ){.    iRet = 51
1671a 32 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69  2;.  }else if( i
1671b 52 65 74 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53  Ret>MAX_SECTOR_S
1671c 49 5a 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72  IZE ){.    asser
1671d 74 28 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49  t( MAX_SECTOR_SI
1671e 5a 45 3e 3d 35 31 32 20 29 3b 0a 20 20 20 20 69  ZE>=512 );.    i
1671f 52 65 74 20 3d 20 4d 41 58 5f 53 45 43 54 4f 52  Ret = MAX_SECTOR
16720 5f 53 49 5a 45 3b 0a 20 20 7d 0a 20 20 72 65 74  _SIZE;.  }.  ret
16721 75 72 6e 20 69 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a  urn iRet;.}../*.
16722 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c 75 65  ** Set the value
16723 20 6f 66 20 74 68 65 20 50 61 67 65 72 2e 73 65   of the Pager.se
16724 63 74 6f 72 53 69 7a 65 20 76 61 72 69 61 62 6c  ctorSize variabl
16725 65 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 0a  e for the given.
16726 2a 2a 20 70 61 67 65 72 20 62 61 73 65 64 20 6f  ** pager based o
16727 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  n the value retu
16728 72 6e 65 64 20 62 79 20 74 68 65 20 78 53 65 63  rned by the xSec
16729 74 6f 72 53 69 7a 65 20 6d 65 74 68 6f 64 0a 2a  torSize method.*
1672a 2a 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 64 61  * of the open da
1672b 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65  tabase file. The
1672c 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c   sector size wil
1672d 6c 20 62 65 20 75 73 65 64 20 75 73 65 64 20 0a  l be used used .
1672e 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ** to determine 
1672f 74 68 65 20 73 69 7a 65 20 61 6e 64 20 61 6c 69  the size and ali
16730 67 6e 6d 65 6e 74 20 6f 66 20 6a 6f 75 72 6e 61  gnment of journa
16731 6c 20 68 65 61 64 65 72 20 61 6e 64 20 0a 2a 2a  l header and .**
16732 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
16733 70 6f 69 6e 74 65 72 73 20 77 69 74 68 69 6e 20  pointers within 
16734 63 72 65 61 74 65 64 20 6a 6f 75 72 6e 61 6c 20  created journal 
16735 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  files..**.** For
16736 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   temporary files
16737 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 73   the effective s
16738 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20 61 6c  ector size is al
16739 77 61 79 73 20 35 31 32 20 62 79 74 65 73 2e 0a  ways 512 bytes..
1673a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
1673b 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d 70 6f 72 61   for non-tempora
1673c 72 79 20 66 69 6c 65 73 2c 20 74 68 65 20 65 66  ry files, the ef
1673d 66 65 63 74 69 76 65 20 73 65 63 74 6f 72 20 73  fective sector s
1673e 69 7a 65 20 69 73 0a 2a 2a 20 74 68 65 20 76 61  ize is.** the va
1673f 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
16740 74 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65 28  the xSectorSize(
16741 29 20 6d 65 74 68 6f 64 20 72 6f 75 6e 64 65 64  ) method rounded
16742 20 75 70 20 74 6f 20 33 32 20 69 66 0a 2a 2a 20   up to 32 if.** 
16743 69 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  it is less than 
16744 33 32 2c 20 6f 72 20 72 6f 75 6e 64 65 64 20 64  32, or rounded d
16745 6f 77 6e 20 74 6f 20 4d 41 58 5f 53 45 43 54 4f  own to MAX_SECTO
16746 52 5f 53 49 5a 45 20 69 66 20 69 74 0a 2a 2a 20  R_SIZE if it.** 
16747 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
16748 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 2e  MAX_SECTOR_SIZE.
16749 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  .**.** If the fi
1674a 6c 65 20 68 61 73 20 74 68 65 20 53 51 4c 49 54  le has the SQLIT
1674b 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52 53 41 46  E_IOCAP_POWERSAF
1674c 45 5f 4f 56 45 52 57 52 49 54 45 20 70 72 6f 70  E_OVERWRITE prop
1674d 65 72 74 79 2c 20 74 68 65 6e 20 73 65 74 0a 2a  erty, then set.*
1674e 2a 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20  * the effective 
1674f 73 65 63 74 6f 72 20 73 69 7a 65 20 74 6f 20 69  sector size to i
16750 74 73 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65  ts minimum value
16751 20 28 35 31 32 29 2e 20 20 54 68 65 20 70 75 72   (512).  The pur
16752 70 6f 73 65 20 6f 66 0a 2a 2a 20 70 50 61 67 65  pose of.** pPage
16753 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 69 73  r->sectorSize is
16754 20 74 6f 20 64 65 66 69 6e 65 20 74 68 65 20 22   to define the "
16755 62 6c 61 73 74 20 72 61 64 69 75 73 22 20 6f 66  blast radius" of
16756 20 62 79 74 65 73 20 74 68 61 74 0a 2a 2a 20 6d   bytes that.** m
16757 69 67 68 74 20 63 68 61 6e 67 65 20 69 66 20 61  ight change if a
16758 20 63 72 61 73 68 20 6f 63 63 75 72 73 20 77 68   crash occurs wh
16759 69 6c 65 20 77 72 69 74 69 6e 67 20 74 6f 20 61  ile writing to a
1675a 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 0a   single byte in.
1675b 2a 2a 20 74 68 61 74 20 72 61 6e 67 65 2e 20 20  ** that range.  
1675c 42 75 74 20 77 69 74 68 20 50 4f 57 45 52 53 41  But with POWERSA
1675d 46 45 5f 4f 56 45 52 57 52 49 54 45 2c 20 74 68  FE_OVERWRITE, th
1675e 65 20 62 6c 61 73 74 20 72 61 64 69 75 73 20 69  e blast radius i
1675f 73 20 7a 65 72 6f 0a 2a 2a 20 28 74 68 61 74 20  s zero.** (that 
16760 69 73 20 77 68 61 74 20 50 4f 57 45 52 53 41 46  is what POWERSAF
16761 45 5f 4f 56 45 52 57 52 49 54 45 20 6d 65 61 6e  E_OVERWRITE mean
16762 73 29 2c 20 73 6f 20 77 65 20 6d 69 6e 69 6d 69  s), so we minimi
16763 7a 65 20 74 68 65 20 73 65 63 74 6f 72 0a 2a 2a  ze the sector.**
16764 20 73 69 7a 65 2e 20 20 46 6f 72 20 62 61 63 6b   size.  For back
16765 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c  wards compatibil
16766 69 74 79 20 6f 66 20 74 68 65 20 72 6f 6c 6c 62  ity of the rollb
16767 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ack journal file
16768 20 66 6f 72 6d 61 74 2c 0a 2a 2a 20 77 65 20 63   format,.** we c
16769 61 6e 6e 6f 74 20 72 65 64 75 63 65 20 74 68 65  annot reduce the
1676a 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f   effective secto
1676b 72 20 73 69 7a 65 20 62 65 6c 6f 77 20 35 31 32  r size below 512
1676c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1676d 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 50   setSectorSize(P
1676e 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
1676f 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28   assert( isOpen(
16770 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70  pPager->fd) || p
16771 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
16772 29 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72  );..  if( pPager
16773 2d 3e 74 65 6d 70 46 69 6c 65 0a 20 20 20 7c 7c  ->tempFile.   ||
16774 20 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63   (sqlite3OsDevic
16775 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
16776 28 70 50 61 67 65 72 2d 3e 66 64 29 20 26 20 0a  (pPager->fd) & .
16777 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
16778 4c 49 54 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52  LITE_IOCAP_POWER
16779 53 41 46 45 5f 4f 56 45 52 57 52 49 54 45 29 21  SAFE_OVERWRITE)!
1677a 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 53  =0.  ){.    /* S
1677b 65 63 74 6f 72 20 73 69 7a 65 20 64 6f 65 73 6e  ector size doesn
1677c 27 74 20 6d 61 74 74 65 72 20 66 6f 72 20 74 65  't matter for te
1677d 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 41  mporary files. A
1677e 6c 73 6f 2c 20 74 68 65 20 66 69 6c 65 0a 20 20  lso, the file.  
1677f 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 68 61 76    ** may not hav
16780 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 79 65  e been opened ye
16781 74 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65  t, in which case
16782 20 74 68 65 20 4f 73 53 65 63 74 6f 72 53 69 7a   the OsSectorSiz
16783 65 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20  e().    ** call 
16784 77 69 6c 6c 20 73 65 67 66 61 75 6c 74 2e 20 2a  will segfault. *
16785 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65  /.    pPager->se
16786 63 74 6f 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a  ctorSize = 512;.
16787 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61    }else{.    pPa
16788 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20  ger->sectorSize 
16789 3d 20 73 71 6c 69 74 65 33 53 65 63 74 6f 72 53  = sqlite3SectorS
1678a 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b  ize(pPager->fd);
1678b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c  .  }.}../*.** Pl
1678c 61 79 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e  ayback the journ
1678d 61 6c 20 61 6e 64 20 74 68 75 73 20 72 65 73 74  al and thus rest
1678e 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ore the database
1678f 20 66 69 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20   file to.** the 
16790 73 74 61 74 65 20 69 74 20 77 61 73 20 69 6e 20  state it was in 
16791 62 65 66 6f 72 65 20 77 65 20 73 74 61 72 74 65  before we starte
16792 64 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 73  d making changes
16793 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f  .  .**.** The jo
16794 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61  urnal file forma
16795 74 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  t is as follows:
16796 20 0a 2a 2a 0a 2a 2a 20 20 28 31 29 20 20 38 20   .**.**  (1)  8 
16797 62 79 74 65 20 70 72 65 66 69 78 2e 20 20 41 20  byte prefix.  A 
16798 63 6f 70 79 20 6f 66 20 61 4a 6f 75 72 6e 61 6c  copy of aJournal
16799 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 20 20 28 32 29  Magic[]..**  (2)
1679a 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64    4 byte big-end
1679b 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63  ian integer whic
1679c 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  h is the number 
1679d 6f 66 20 76 61 6c 69 64 20 70 61 67 65 20 72 65  of valid page re
1679e 63 6f 72 64 73 0a 2a 2a 20 20 20 20 20 20 20 69  cords.**       i
1679f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20  n the journal.  
167a0 49 66 20 74 68 69 73 20 76 61 6c 75 65 20 69 73  If this value is
167a1 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 65   0xffffffff, the
167a2 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a  n compute the.**
167a3 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66         number of
167a4 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 72   page records fr
167a5 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 73  om the journal s
167a6 69 7a 65 2e 0a 2a 2a 20 20 28 33 29 20 20 34 20  ize..**  (3)  4 
167a7 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20  byte big-endian 
167a8 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73  integer which is
167a9 20 74 68 65 20 69 6e 69 74 69 61 6c 20 76 61 6c   the initial val
167aa 75 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 20  ue for the .**  
167ab 20 20 20 20 20 73 61 6e 69 74 79 20 63 68 65 63       sanity chec
167ac 6b 73 75 6d 2e 0a 2a 2a 20 20 28 34 29 20 20 34  ksum..**  (4)  4
167ad 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 68   byte integer wh
167ae 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ich is the numbe
167af 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 74 72  r of pages to tr
167b0 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 20 20  uncate the.**   
167b1 20 20 20 20 64 61 74 61 62 61 73 65 20 74 6f 20      database to 
167b2 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63  during a rollbac
167b3 6b 2e 0a 2a 2a 20 20 28 35 29 20 20 34 20 62 79  k..**  (5)  4 by
167b4 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e  te big-endian in
167b5 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74  teger which is t
167b6 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20  he sector size. 
167b7 20 54 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 20   The header.**  
167b8 20 20 20 20 20 69 73 20 74 68 69 73 20 6d 61 6e       is this man
167b9 79 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e  y bytes in size.
167ba 0a 2a 2a 20 20 28 36 29 20 20 34 20 62 79 74 65  .**  (6)  4 byte
167bb 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65   big-endian inte
167bc 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65  ger which is the
167bd 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 20   page size..**  
167be 28 37 29 20 20 7a 65 72 6f 20 70 61 64 64 69 6e  (7)  zero paddin
167bf 67 20 6f 75 74 20 74 6f 20 74 68 65 20 6e 65 78  g out to the nex
167c0 74 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 0a 2a  t sector size..*
167c1 2a 20 20 28 38 29 20 20 5a 65 72 6f 20 6f 72 20  *  (8)  Zero or 
167c2 6d 6f 72 65 20 70 61 67 65 73 20 69 6e 73 74 61  more pages insta
167c3 6e 63 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f  nces, each as fo
167c4 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20  llows:.**       
167c5 20 2b 20 20 34 20 62 79 74 65 20 70 61 67 65 20   +  4 byte page 
167c6 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20  number..**      
167c7 20 20 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67    +  pPager->pag
167c8 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64  eSize bytes of d
167c9 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b  ata..**        +
167ca 20 20 34 20 62 79 74 65 20 63 68 65 63 6b 73 75    4 byte checksu
167cb 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20  m.**.** When we 
167cc 73 70 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75  speak of the jou
167cd 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20  rnal header, we 
167ce 6d 65 61 6e 20 74 68 65 20 66 69 72 73 74 20 37  mean the first 7
167cf 20 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a   items above..**
167d0 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74   Each entry in t
167d1 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e  he journal is an
167d2 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
167d3 20 38 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a   8th item..**.**
167d4 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20   Call the value 
167d5 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20  from the second 
167d6 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20  bullet "nRec".  
167d7 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62  nRec is the numb
167d8 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70  er of.** valid p
167d9 61 67 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74  age entries in t
167da 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20  he journal.  In 
167db 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20  most cases, you 
167dc 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a  can compute the.
167dd 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63  ** value of nRec
167de 20 66 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f   from the size o
167df 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
167e0 6c 65 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f  le.  But if a po
167e1 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f  wer.** failure o
167e2 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 74 68  ccurred while th
167e3 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65  e journal was be
167e4 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20  ing written, it 
167e5 63 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20  could be the.** 
167e6 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73 69  case that the si
167e7 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ze of the journa
167e8 6c 20 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61  l file had alrea
167e9 64 79 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65  dy been increase
167ea 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74  d but.** the ext
167eb 72 61 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e  ra entries had n
167ec 6f 74 20 79 65 74 20 6d 61 64 65 20 69 74 20 73  ot yet made it s
167ed 61 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20  afely to disk.  
167ee 49 6e 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a  In such a case,.
167ef 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
167f0 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72  nRec computed fr
167f1 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65  om the file size
167f2 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61   would be too la
167f3 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61  rge.  For.** tha
167f4 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77  t reason, we alw
167f5 61 79 73 20 75 73 65 20 74 68 65 20 6e 52 65 63  ays use the nRec
167f6 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65   value in the he
167f7 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ader..**.** If t
167f8 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73  he nRec value is
167f9 20 30 78 66 66 66 66 66 66 66 66 20 69 74 20 6d   0xffffffff it m
167fa 65 61 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73  eans that nRec s
167fb 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65  hould be compute
167fc 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69  d.** from the fi
167fd 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76  le size.  This v
167fe 61 6c 75 65 20 69 73 20 75 73 65 64 20 77 68 65  alue is used whe
167ff 6e 20 74 68 65 20 75 73 65 72 20 73 65 6c 65 63  n the user selec
16800 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e  ts the.** no-syn
16801 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65  c option for the
16802 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77   journal.  A pow
16803 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64  er failure could
16804 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74   lead to corrupt
16805 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63  ion.** in this c
16806 61 73 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68  ase.  But for th
16807 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72  ings like tempor
16808 61 72 79 20 74 61 62 6c 65 20 28 77 68 69 63 68  ary table (which
16809 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65   will be.** dele
1680a 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77  ted when the pow
1680b 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 29 20  er is restored) 
1680c 77 65 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20  we don't care.  
1680d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  .**.** If the fi
1680e 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65  le opened as the
1680f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
16810 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d   not a well-form
16811 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  ed.** journal fi
16812 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65  le then all page
16813 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73  s up to the firs
16814 74 20 63 6f 72 72 75 70 74 65 64 20 70 61 67 65  t corrupted page
16815 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62   are rolled.** b
16816 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73  ack (or no pages
16817 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   if the journal 
16818 68 65 61 64 65 72 20 69 73 20 63 6f 72 72 75 70  header is corrup
16819 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61  ted). The journa
1681a 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65  l file.** is the
1681b 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51  n deleted and SQ
1681c 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64  LITE_OK returned
1681d 2c 20 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20  , just as if no 
1681e 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a  corruption had.*
1681f 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72  * been encounter
16820 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ed..**.** If an 
16821 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20  I/O or malloc() 
16822 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68  error occurs, th
16823 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69  e journal-file i
16824 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a  s not deleted.**
16825 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f   and an error co
16826 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
16827 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74 20  **.** The isHot 
16828 70 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61  parameter indica
16829 74 65 73 20 74 68 61 74 20 77 65 20 61 72 65 20  tes that we are 
1682a 74 72 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61  trying to rollba
1682b 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  ck a journal.** 
1682c 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 61 20  that might be a 
1682d 68 6f 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72  hot journal.  Or
1682e 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68  , it could be th
1682f 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69  at the journal i
16830 73 20 0a 2a 2a 20 70 72 65 73 65 72 76 65 64 20  s .** preserved 
16831 62 65 63 61 75 73 65 20 6f 66 20 4a 4f 55 52 4e  because of JOURN
16832 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 6f  ALMODE_PERSIST o
16833 72 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52  r JOURNALMODE_TR
16834 55 4e 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74 68  UNCATE..** If th
16835 65 20 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79  e journal really
16836 20 69 73 20 68 6f 74 2c 20 72 65 73 65 74 20 74   is hot, reset t
16837 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 70  he pager cache p
16838 72 69 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20  rior rolling.** 
16839 62 61 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74  back any content
1683a 2e 20 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61  .  If the journa
1683b 6c 20 69 73 20 6d 65 72 65 6c 79 20 70 65 72 73  l is merely pers
1683c 69 73 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74  istent, no reset
1683d 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a   is.** needed..*
1683e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
1683f 65 72 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 65  er_playback(Page
16840 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69  r *pPager, int i
16841 73 48 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  sHot){.  sqlite3
16842 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61  _vfs *pVfs = pPa
16843 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 34  ger->pVfs;.  i64
16844 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20   szJ;           
16845 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
16846 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
16847 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  e in bytes */.  
16848 75 33 32 20 6e 52 65 63 3b 20 20 20 20 20 20 20  u32 nRec;       
16849 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1684a 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20 69 6e  er of Records in
1684b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a   the journal */.
1684c 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 20    u32 u;        
1684d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e             /* Un
1684e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e  signed loop coun
1684f 74 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78  ter */.  Pgno mx
16850 50 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  Pg = 0;         
16851 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
16852 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 69   original file i
16853 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74  n pages */.  int
16854 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
16855 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
16856 63 6f 64 65 20 6f 66 20 61 20 73 75 62 72 6f 75  code of a subrou
16857 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65  tine */.  int re
16858 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20  s = 1;          
16859 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75     /* Value retu
1685a 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f  rned by sqlite3O
1685b 73 41 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 63  sAccess() */.  c
1685c 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30  har *zMaster = 0
1685d 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ;       /* Name 
1685e 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  of master journa
1685f 6c 20 66 69 6c 65 20 69 66 20 61 6e 79 20 2a 2f  l file if any */
16860 0a 20 20 69 6e 74 20 6e 65 65 64 50 61 67 65 72  .  int needPager
16861 52 65 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 54  Reset;      /* T
16862 72 75 65 20 74 6f 20 72 65 73 65 74 20 70 61 67  rue to reset pag
16863 65 20 70 72 69 6f 72 20 74 6f 20 66 69 72 73 74  e prior to first
16864 20 70 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20 2a   page rollback *
16865 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  /..  /* Figure o
16866 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f  ut how many reco
16867 72 64 73 20 61 72 65 20 69 6e 20 74 68 65 20 6a  rds are in the j
16868 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65  ournal.  Abort e
16869 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 65  arly if.  ** the
1686a 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 74   journal is empt
1686b 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  y..  */.  assert
1686c 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
1686d 3e 6a 66 64 29 20 29 3b 0a 20 20 72 63 20 3d 20  >jfd) );.  rc = 
1686e 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
1686f 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26  e(pPager->jfd, &
16870 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  szJ);.  if( rc!=
16871 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
16872 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61   goto end_playba
16873 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65  ck;.  }..  /* Re
16874 61 64 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  ad the master jo
16875 75 72 6e 61 6c 20 6e 61 6d 65 20 66 72 6f 6d 20  urnal name from 
16876 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 66 20  the journal, if 
16877 69 74 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20  it is present.. 
16878 20 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20   ** If a master 
16879 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d  journal file nam
1687a 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2c 20  e is specified, 
1687b 62 75 74 20 74 68 65 20 66 69 6c 65 20 69 73 20  but the file is 
1687c 6e 6f 74 0a 20 20 2a 2a 20 70 72 65 73 65 6e 74  not.  ** present
1687d 20 6f 6e 20 64 69 73 6b 2c 20 74 68 65 6e 20 74   on disk, then t
1687e 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f  he journal is no
1687f 74 20 68 6f 74 20 61 6e 64 20 64 6f 65 73 20 6e  t hot and does n
16880 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 0a 20 20  ot need to be.  
16881 2a 2a 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a  ** played back..
16882 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20    **.  ** TODO: 
16883 54 65 63 68 6e 69 63 61 6c 6c 79 20 74 68 65 20  Technically the 
16884 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 6e 20  following is an 
16885 65 72 72 6f 72 20 62 65 63 61 75 73 65 20 69 74  error because it
16886 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 20 20   assumes that.  
16887 2a 2a 20 62 75 66 66 65 72 20 50 61 67 65 72 2e  ** buffer Pager.
16888 70 54 6d 70 53 70 61 63 65 20 69 73 20 28 6d 78  pTmpSpace is (mx
16889 50 61 74 68 6e 61 6d 65 2b 31 29 20 62 79 74 65  Pathname+1) byte
1688a 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 69 2e 65  s or larger. i.e
1688b 2e 20 74 68 61 74 0a 20 20 2a 2a 20 28 70 50 61  . that.  ** (pPa
1688c 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3e 3d  ger->pageSize >=
1688d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d   pPager->pVfs->m
1688e 78 50 61 74 68 6e 61 6d 65 2b 31 29 2e 20 55 73  xPathname+1). Us
1688f 69 6e 67 20 6f 73 5f 75 6e 69 78 2e 63 2c 0a 20  ing os_unix.c,. 
16890 20 2a 2a 20 20 6d 78 50 61 74 68 6e 61 6d 65 20   **  mxPathname 
16891 69 73 20 35 31 32 2c 20 77 68 69 63 68 20 69 73  is 512, which is
16892 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
16893 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 61 62   minimum allowab
16894 6c 65 20 76 61 6c 75 65 0a 20 20 2a 2a 20 66 6f  le value.  ** fo
16895 72 20 70 61 67 65 53 69 7a 65 2e 0a 20 20 2a 2f  r pageSize..  */
16896 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61  .  zMaster = pPa
16897 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a  ger->pTmpSpace;.
16898 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65    rc = readMaste
16899 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d  rJournal(pPager-
1689a 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70  >jfd, zMaster, p
1689b 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50  Pager->pVfs->mxP
1689c 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 69 66  athname+1);.  if
1689d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1689e 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 29 7b  && zMaster[0] ){
1689f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
168a0 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 20  3OsAccess(pVfs, 
168a1 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f  zMaster, SQLITE_
168a2 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26  ACCESS_EXISTS, &
168a3 72 65 73 29 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73  res);.  }.  zMas
168a4 74 65 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 72  ter = 0;.  if( r
168a5 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
168a6 21 72 65 73 20 29 7b 0a 20 20 20 20 67 6f 74 6f  !res ){.    goto
168a7 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20   end_playback;. 
168a8 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75   }.  pPager->jou
168a9 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 6e  rnalOff = 0;.  n
168aa 65 65 64 50 61 67 65 72 52 65 73 65 74 20 3d 20  eedPagerReset = 
168ab 69 73 48 6f 74 3b 0a 0a 20 20 2f 2a 20 54 68 69  isHot;..  /* Thi
168ac 73 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61 74 65  s loop terminate
168ad 73 20 65 69 74 68 65 72 20 77 68 65 6e 20 61 20  s either when a 
168ae 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 29  readJournalHdr()
168af 20 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 72 5f   or .  ** pager_
168b0 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67  playback_one_pag
168b1 65 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73  e() call returns
168b2 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20   SQLITE_DONE or 
168b3 61 6e 20 49 4f 20 65 72 72 6f 72 20 0a 20 20 2a  an IO error .  *
168b4 2a 20 6f 63 63 75 72 73 2e 20 0a 20 20 2a 2f 0a  * occurs. .  */.
168b5 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20    while( 1 ){.  
168b6 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e 65    /* Read the ne
168b7 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  xt journal heade
168b8 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e  r from the journ
168b9 61 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65  al file.  If the
168ba 72 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f  re are.    ** no
168bb 74 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20 6c  t enough bytes l
168bc 65 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  eft in the journ
168bd 61 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 63 6f  al file for a co
168be 6d 70 6c 65 74 65 20 68 65 61 64 65 72 2c 20 6f  mplete header, o
168bf 72 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 63  r.    ** it is c
168c0 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 61  orrupted, then a
168c1 20 70 72 6f 63 65 73 73 20 6d 75 73 74 20 68 61   process must ha
168c2 76 65 20 66 61 69 6c 65 64 20 77 68 69 6c 65 20  ve failed while 
168c3 77 72 69 74 69 6e 67 20 69 74 2e 0a 20 20 20 20  writing it..    
168c4 2a 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74 65  ** This indicate
168c5 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e  s nothing more n
168c6 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65  eeds to be rolle
168c7 64 20 62 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20  d back..    */. 
168c8 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72     rc = readJour
168c9 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 69  nalHdr(pPager, i
168ca 73 48 6f 74 2c 20 73 7a 4a 2c 20 26 6e 52 65 63  sHot, szJ, &nRec
168cb 2c 20 26 6d 78 50 67 29 3b 0a 20 20 20 20 69 66  , &mxPg);.    if
168cc 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
168cd 29 7b 20 0a 20 20 20 20 20 20 69 66 28 20 72 63  ){ .      if( rc
168ce 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
168cf 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
168d0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d  LITE_OK;.      }
168d1 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  .      goto end_
168d2 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 7d 0a  playback;.    }.
168d3 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20  .    /* If nRec 
168d4 69 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 74  is 0xffffffff, t
168d5 68 65 6e 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c  hen this journal
168d6 20 77 61 73 20 63 72 65 61 74 65 64 20 62 79 20   was created by 
168d7 61 20 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a  a process.    **
168d8 20 77 6f 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73   working in no-s
168d9 79 6e 63 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d  ync mode. This m
168da 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 72 65  eans that the re
168db 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  st of the journa
168dc 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f  l.    ** file co
168dd 6e 73 69 73 74 73 20 6f 66 20 70 61 67 65 73 2c  nsists of pages,
168de 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f   there are no mo
168df 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  re journal heade
168e0 72 73 2e 20 43 6f 6d 70 75 74 65 0a 20 20 20 20  rs. Compute.    
168e1 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
168e2 6e 52 65 63 20 62 61 73 65 64 20 6f 6e 20 74 68  nRec based on th
168e3 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 20  is assumption.. 
168e4 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52     */.    if( nR
168e5 65 63 3d 3d 30 78 66 66 66 66 66 66 66 66 20 29  ec==0xffffffff )
168e6 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
168e7 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
168e8 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ff==JOURNAL_HDR_
168e9 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20  SZ(pPager) );.  
168ea 20 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29      nRec = (int)
168eb 28 28 73 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f  ((szJ - JOURNAL_
168ec 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 29 2f  HDR_SZ(pPager))/
168ed 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50  JOURNAL_PG_SZ(pP
168ee 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20  ager));.    }.. 
168ef 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73     /* If nRec is
168f0 20 30 20 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c   0 and this roll
168f1 62 61 63 6b 20 69 73 20 6f 66 20 61 20 74 72 61  back is of a tra
168f2 6e 73 61 63 74 69 6f 6e 20 63 72 65 61 74 65 64  nsaction created
168f3 20 62 79 20 74 68 69 73 0a 20 20 20 20 2a 2a 20   by this.    ** 
168f4 70 72 6f 63 65 73 73 20 61 6e 64 20 69 66 20 74  process and if t
168f5 68 69 73 20 69 73 20 74 68 65 20 66 69 6e 61 6c  his is the final
168f6 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 6a   header in the j
168f7 6f 75 72 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20  ournal, then it 
168f8 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61  means.    ** tha
168f9 74 20 74 68 69 73 20 70 61 72 74 20 6f 66 20 74  t this part of t
168fa 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62  he journal was b
168fb 65 69 6e 67 20 66 69 6c 6c 65 64 20 62 75 74 20  eing filled but 
168fc 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  has not yet been
168fd 0a 20 20 20 20 2a 2a 20 73 79 6e 63 65 64 20 74  .    ** synced t
168fe 6f 20 64 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65  o disk.  Compute
168ff 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
16900 61 67 65 73 20 62 61 73 65 64 20 6f 6e 20 74 68  ages based on th
16901 65 20 72 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20  e remaining.    
16902 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66  ** size of the f
16903 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ile..    **.    
16904 2a 2a 20 54 68 65 20 74 68 69 72 64 20 74 65 72  ** The third ter
16905 6d 20 6f 66 20 74 68 65 20 74 65 73 74 20 77 61  m of the test wa
16906 73 20 61 64 64 65 64 20 74 6f 20 66 69 78 20 74  s added to fix t
16907 69 63 6b 65 74 20 23 32 35 36 35 2e 0a 20 20 20  icket #2565..   
16908 20 2a 2a 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67   ** When rolling
16909 20 62 61 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72   back a hot jour
1690a 6e 61 6c 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77  nal, nRec==0 alw
1690b 61 79 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74  ays means that t
1690c 68 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63  he next.    ** c
1690d 68 75 6e 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72  hunk of the jour
1690e 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72  nal contains zer
1690f 6f 20 70 61 67 65 73 20 74 6f 20 62 65 20 72 6f  o pages to be ro
16910 6c 6c 65 64 20 62 61 63 6b 2e 20 20 42 75 74 0a  lled back.  But.
16911 20 20 20 20 2a 2a 20 77 68 65 6e 20 64 6f 69 6e      ** when doin
16912 67 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64  g a ROLLBACK and
16913 20 74 68 65 20 6e 52 65 63 3d 3d 30 20 63 68 75   the nRec==0 chu
16914 6e 6b 20 69 73 20 74 68 65 20 6c 61 73 74 20 63  nk is the last c
16915 68 75 6e 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 74  hunk in.    ** t
16916 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 6d  he journal, it m
16917 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6a 6f  eans that the jo
16918 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74  urnal might cont
16919 61 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 20  ain additional. 
1691a 20 20 20 2a 2a 20 70 61 67 65 73 20 74 68 61 74     ** pages that
1691b 20 6e 65 65 64 20 74 6f 20 62 65 20 72 6f 6c 6c   need to be roll
1691c 65 64 20 62 61 63 6b 20 61 6e 64 20 74 68 61 74  ed back and that
1691d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
1691e 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 68 6f  ages .    ** sho
1691f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 20  uld be computed 
16920 62 61 73 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75  based on the jou
16921 72 6e 61 6c 20 66 69 6c 65 20 73 69 7a 65 2e 0a  rnal file size..
16922 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e      */.    if( n
16923 52 65 63 3d 3d 30 20 26 26 20 21 69 73 48 6f 74  Rec==0 && !isHot
16924 20 26 26 0a 20 20 20 20 20 20 20 20 70 50 61 67   &&.        pPag
16925 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a  er->journalHdr+J
16926 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
16927 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a  ager)==pPager->j
16928 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20  ournalOff ){.   
16929 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28     nRec = (int)(
1692a 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a  (szJ - pPager->j
1692b 6f 75 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55  ournalOff) / JOU
1692c 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65  RNAL_PG_SZ(pPage
1692d 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  r));.    }..    
1692e 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68  /* If this is th
1692f 65 20 66 69 72 73 74 20 68 65 61 64 65 72 20 72  e first header r
16930 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75  ead from the jou
16931 72 6e 61 6c 2c 20 74 72 75 6e 63 61 74 65 20 74  rnal, truncate t
16932 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61  he.    ** databa
16933 73 65 20 66 69 6c 65 20 62 61 63 6b 20 74 6f 20  se file back to 
16934 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a  its original siz
16935 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
16936 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
16937 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44  lOff==JOURNAL_HD
16938 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a  R_SZ(pPager) ){.
16939 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
1693a 5f 74 72 75 6e 63 61 74 65 28 70 50 61 67 65 72  _truncate(pPager
1693b 2c 20 6d 78 50 67 29 3b 0a 20 20 20 20 20 20 69  , mxPg);.      i
1693c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1693d 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
1693e 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20   end_playback;. 
1693f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61       }.      pPa
16940 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d 78  ger->dbSize = mx
16941 50 67 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  Pg;.    }..    /
16942 2a 20 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20  * Copy original 
16943 70 61 67 65 73 20 6f 75 74 20 6f 66 20 74 68 65  pages out of the
16944 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 63   journal and bac
16945 6b 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 20 20  k into the .    
16946 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
16947 20 61 6e 64 2f 6f 72 20 70 61 67 65 20 63 61 63   and/or page cac
16948 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  he..    */.    f
16949 6f 72 28 75 3d 30 3b 20 75 3c 6e 52 65 63 3b 20  or(u=0; u<nRec; 
1694a 75 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  u++){.      if( 
1694b 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 20 29  needPagerReset )
1694c 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 5f  {.        pager_
1694d 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20  reset(pPager);. 
1694e 20 20 20 20 20 20 20 6e 65 65 64 50 61 67 65 72         needPager
1694f 52 65 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20  Reset = 0;.     
16950 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61   }.      rc = pa
16951 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65  ger_playback_one
16952 5f 70 61 67 65 28 70 50 61 67 65 72 2c 26 70 50  _page(pPager,&pP
16953 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
16954 2c 30 2c 31 2c 30 29 3b 0a 20 20 20 20 20 20 69  ,0,1,0);.      i
16955 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
16956 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
16957 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
16958 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61  ){.          pPa
16959 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
1695a 3d 20 73 7a 4a 3b 0a 20 20 20 20 20 20 20 20 20  = szJ;.         
1695b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1695c 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51  }else if( rc==SQ
1695d 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
1695e 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 20  _READ ){.       
1695f 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75     /* If the jou
16960 72 6e 61 6c 20 68 61 73 20 62 65 65 6e 20 74 72  rnal has been tr
16961 75 6e 63 61 74 65 64 2c 20 73 69 6d 70 6c 79 20  uncated, simply 
16962 73 74 6f 70 20 72 65 61 64 69 6e 67 20 61 6e 64  stop reading and
16963 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 72  .          ** pr
16964 6f 63 65 73 73 69 6e 67 20 74 68 65 20 6a 6f 75  ocessing the jou
16965 72 6e 61 6c 2e 20 54 68 69 73 20 6d 69 67 68 74  rnal. This might
16966 20 68 61 70 70 65 6e 20 69 66 20 74 68 65 20 6a   happen if the j
16967 6f 75 72 6e 61 6c 20 77 61 73 0a 20 20 20 20 20  ournal was.     
16968 20 20 20 20 20 2a 2a 20 6e 6f 74 20 63 6f 6d 70       ** not comp
16969 6c 65 74 65 6c 79 20 77 72 69 74 74 65 6e 20 61  letely written a
1696a 6e 64 20 73 79 6e 63 65 64 20 70 72 69 6f 72 20  nd synced prior 
1696b 74 6f 20 61 20 63 72 61 73 68 2e 20 20 49 6e 20  to a crash.  In 
1696c 74 68 61 74 0a 20 20 20 20 20 20 20 20 20 20 2a  that.          *
1696d 2a 20 63 61 73 65 2c 20 74 68 65 20 64 61 74 61  * case, the data
1696e 62 61 73 65 20 73 68 6f 75 6c 64 20 68 61 76 65  base should have
1696f 20 6e 65 76 65 72 20 62 65 65 6e 20 77 72 69 74   never been writ
16970 74 65 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 20  ten in the.     
16971 20 20 20 20 20 2a 2a 20 66 69 72 73 74 20 70 6c       ** first pl
16972 61 63 65 20 73 6f 20 69 74 20 69 73 20 4f 4b 20  ace so it is OK 
16973 74 6f 20 73 69 6d 70 6c 79 20 61 62 61 6e 64 6f  to simply abando
16974 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 20  n the rollback. 
16975 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  */.          rc 
16976 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
16977 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
16978 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 20 20  playback;.      
16979 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1697a 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20     /* If we are 
1697b 75 6e 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61  unable to rollba
1697c 63 6b 2c 20 71 75 69 74 20 61 6e 64 20 72 65 74  ck, quit and ret
1697d 75 72 6e 20 74 68 65 20 65 72 72 6f 72 0a 20 20  urn the error.  
1697e 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 2e          ** code.
1697f 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73    This will caus
16980 65 20 74 68 65 20 70 61 67 65 72 20 74 6f 20 65  e the pager to e
16981 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73  nter the error s
16982 74 61 74 65 0a 20 20 20 20 20 20 20 20 20 20 2a  tate.          *
16983 2a 20 73 6f 20 74 68 61 74 20 6e 6f 20 66 75 72  * so that no fur
16984 74 68 65 72 20 68 61 72 6d 20 77 69 6c 6c 20 62  ther harm will b
16985 65 20 64 6f 6e 65 2e 20 20 50 65 72 68 61 70 73  e done.  Perhaps
16986 20 74 68 65 20 6e 65 78 74 0a 20 20 20 20 20 20   the next.      
16987 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 74      ** process t
16988 6f 20 63 6f 6d 65 20 61 6c 6f 6e 67 20 77 69 6c  o come along wil
16989 6c 20 62 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c  l be able to rol
1698a 6c 62 61 63 6b 20 74 68 65 20 64 61 74 61 62 61  lback the databa
1698b 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  se..          */
1698c 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1698d 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20  end_playback;.  
1698e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1698f 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f      }.  }.  /*NO
16990 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 61 73 73  TREACHED*/.  ass
16991 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64 5f 70  ert( 0 );..end_p
16992 6c 61 79 62 61 63 6b 3a 0a 20 20 2f 2a 20 46 6f  layback:.  /* Fo
16993 6c 6c 6f 77 69 6e 67 20 61 20 72 6f 6c 6c 62 61  llowing a rollba
16994 63 6b 2c 20 74 68 65 20 64 61 74 61 62 61 73 65  ck, the database
16995 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20   file should be 
16996 62 61 63 6b 20 69 6e 20 69 74 73 20 6f 72 69 67  back in its orig
16997 69 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 74 65 20  inal.  ** state 
16998 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73 74 61  prior to the sta
16999 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61  rt of the transa
1699a 63 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f 6b 65  ction, so invoke
1699b 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45   the.  ** SQLITE
1699c 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e  _FCNTL_DB_UNCHAN
1699d 47 45 44 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c  GED file-control
1699e 20 6d 65 74 68 6f 64 20 74 6f 20 64 69 73 61 62   method to disab
1699f 6c 65 20 74 68 65 0a 20 20 2a 2a 20 61 73 73 65  le the.  ** asse
169a0 72 74 69 6f 6e 20 74 68 61 74 20 74 68 65 20 74  rtion that the t
169a1 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74  ransaction count
169a2 65 72 20 77 61 73 20 6d 6f 64 69 66 69 65 64 2e  er was modified.
169a3 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  .  */.#ifdef SQL
169a4 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20  ITE_DEBUG.  if( 
169a5 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74  pPager->fd->pMet
169a6 68 6f 64 73 20 29 7b 0a 20 20 20 20 73 71 6c 69  hods ){.    sqli
169a7 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c  te3OsFileControl
169a8 48 69 6e 74 28 70 50 61 67 65 72 2d 3e 66 64 2c  Hint(pPager->fd,
169a9 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f  SQLITE_FCNTL_DB_
169aa 55 4e 43 48 41 4e 47 45 44 2c 30 29 3b 0a 20 20  UNCHANGED,0);.  
169ab 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
169ac 66 20 74 68 69 73 20 70 6c 61 79 62 61 63 6b 20  f this playback 
169ad 69 73 20 68 61 70 70 65 6e 69 6e 67 20 61 75 74  is happening aut
169ae 6f 6d 61 74 69 63 61 6c 6c 79 20 61 73 20 61 20  omatically as a 
169af 72 65 73 75 6c 74 20 6f 66 20 61 6e 20 49 4f 20  result of an IO 
169b0 6f 72 20 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20  or .  ** malloc 
169b1 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72  error that occur
169b2 72 65 64 20 61 66 74 65 72 20 74 68 65 20 63 68  red after the ch
169b3 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 77 61 73  ange-counter was
169b4 20 75 70 64 61 74 65 64 20 62 75 74 20 0a 20 20   updated but .  
169b5 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 74 72  ** before the tr
169b6 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 63 6f  ansaction was co
169b7 6d 6d 69 74 74 65 64 2c 20 74 68 65 6e 20 74 68  mmitted, then th
169b8 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
169b9 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 63 61 74   .  ** modificat
169ba 69 6f 6e 20 6d 61 79 20 6a 75 73 74 20 68 61 76  ion may just hav
169bb 65 20 62 65 65 6e 20 72 65 76 65 72 74 65 64 2e  e been reverted.
169bc 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73   If this happens
169bd 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 0a 20   in exclusive . 
169be 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e 20 73   ** mode, then s
169bf 75 62 73 65 71 75 65 6e 74 20 74 72 61 6e 73 61  ubsequent transa
169c0 63 74 69 6f 6e 73 20 70 65 72 66 6f 72 6d 65 64  ctions performed
169c1 20 62 79 20 74 68 65 20 63 6f 6e 6e 65 63 74 69   by the connecti
169c2 6f 6e 20 77 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a  on will not.  **
169c3 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e   update the chan
169c4 67 65 2d 63 6f 75 6e 74 65 72 20 61 74 20 61 6c  ge-counter at al
169c5 6c 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 64  l. This may lead
169c6 20 74 6f 20 63 61 63 68 65 20 69 6e 63 6f 6e 73   to cache incons
169c7 69 73 74 65 6e 63 79 0a 20 20 2a 2a 20 70 72 6f  istency.  ** pro
169c8 62 6c 65 6d 73 20 66 6f 72 20 6f 74 68 65 72 20  blems for other 
169c9 70 72 6f 63 65 73 73 65 73 20 61 74 20 73 6f 6d  processes at som
169ca 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66  e point in the f
169cb 75 74 75 72 65 2e 20 53 6f 2c 20 6a 75 73 74 0a  uture. So, just.
169cc 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 69    ** in case thi
169cd 73 20 68 61 73 20 68 61 70 70 65 6e 65 64 2c 20  s has happened, 
169ce 63 6c 65 61 72 20 74 68 65 20 63 68 61 6e 67 65  clear the change
169cf 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 6e  CountDone flag n
169d0 6f 77 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65  ow..  */.  pPage
169d1 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  r->changeCountDo
169d2 6e 65 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d  ne = pPager->tem
169d3 70 46 69 6c 65 3b 0a 0a 20 20 69 66 28 20 72 63  pFile;..  if( rc
169d4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
169d5 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61     zMaster = pPa
169d6 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a  ger->pTmpSpace;.
169d7 20 20 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73      rc = readMas
169d8 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  terJournal(pPage
169d9 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c  r->jfd, zMaster,
169da 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d   pPager->pVfs->m
169db 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20  xPathname+1);.  
169dc 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d    testcase( rc!=
169dd 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d  SQLITE_OK );.  }
169de 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
169df 45 5f 4f 4b 0a 20 20 20 26 26 20 28 70 50 61 67  E_OK.   && (pPag
169e0 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47 45  er->eState>=PAGE
169e1 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 7c  R_WRITER_DBMOD |
169e2 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65  | pPager->eState
169e3 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 29 0a 20 20  ==PAGER_OPEN).  
169e4 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
169e5 74 65 33 50 61 67 65 72 53 79 6e 63 28 70 50 61  te3PagerSync(pPa
169e6 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ger);.  }.  if( 
169e7 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
169e8 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f  .    rc = pager_
169e9 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28  end_transaction(
169ea 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 5b  pPager, zMaster[
169eb 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20 20 20 74  0]!='\0');.    t
169ec 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c  estcase( rc!=SQL
169ed 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20  ITE_OK );.  }.  
169ee 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
169ef 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20  K && zMaster[0] 
169f0 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a  && res ){.    /*
169f1 20 49 66 20 74 68 65 72 65 20 77 61 73 20 61 20   If there was a 
169f2 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61  master journal a
169f3 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nd this routine 
169f4 77 69 6c 6c 20 72 65 74 75 72 6e 20 73 75 63 63  will return succ
169f5 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73 65 65 20  ess,.    ** see 
169f6 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c  if it is possibl
169f7 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20  e to delete the 
169f8 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a  master journal..
169f9 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
169fa 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 28  pager_delmaster(
169fb 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29  pPager, zMaster)
169fc 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
169fd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
169fe 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 50  .  }..  /* The P
169ff 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20  ager.sectorSize 
16a00 76 61 72 69 61 62 6c 65 20 6d 61 79 20 68 61 76  variable may hav
16a01 65 20 62 65 65 6e 20 75 70 64 61 74 65 64 20 77  e been updated w
16a02 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a  hile rolling.  *
16a03 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c  * back a journal
16a04 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72   created by a pr
16a05 6f 63 65 73 73 20 77 69 74 68 20 61 20 64 69 66  ocess with a dif
16a06 66 65 72 65 6e 74 20 73 65 63 74 6f 72 20 73 69  ferent sector si
16a07 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 52  ze.  ** value. R
16a08 65 73 65 74 20 69 74 20 74 6f 20 74 68 65 20 63  eset it to the c
16a09 6f 72 72 65 63 74 20 76 61 6c 75 65 20 66 6f 72  orrect value for
16a0a 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e 0a 20   this process.. 
16a0b 20 2a 2f 0a 20 20 73 65 74 53 65 63 74 6f 72 53   */.  setSectorS
16a0c 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72  ize(pPager);.  r
16a0d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
16a0e 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 63 6f 6e  .** Read the con
16a0f 74 65 6e 74 20 66 6f 72 20 70 61 67 65 20 70 50  tent for page pP
16a10 67 20 6f 75 74 20 6f 66 20 74 68 65 20 64 61 74  g out of the dat
16a11 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 69  abase file and i
16a12 6e 74 6f 20 0a 2a 2a 20 70 50 67 2d 3e 70 44 61  nto .** pPg->pDa
16a13 74 61 2e 20 41 20 73 68 61 72 65 64 20 6c 6f 63  ta. A shared loc
16a14 6b 20 6f 72 20 67 72 65 61 74 65 72 20 6d 75 73  k or greater mus
16a15 74 20 62 65 20 68 65 6c 64 20 6f 6e 20 74 68 65  t be held on the
16a16 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c   database.** fil
16a17 65 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75  e before this fu
16a18 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
16a19 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 67 65 20  ..**.** If page 
16a1a 31 20 69 73 20 72 65 61 64 2c 20 74 68 65 6e 20  1 is read, then 
16a1b 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61 67  the value of Pag
16a1c 65 72 2e 64 62 46 69 6c 65 56 65 72 73 5b 5d 20  er.dbFileVers[] 
16a1d 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 74 68 65  is set to.** the
16a1e 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d   value read from
16a1f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
16a20 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  le..**.** If an 
16a21 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  IO error occurs,
16a22 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72   then the IO err
16a23 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74  or is returned t
16a24 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a  o the caller..**
16a25 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49   Otherwise, SQLI
16a26 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
16a27 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
16a28 20 72 65 61 64 44 62 50 61 67 65 28 50 67 48 64   readDbPage(PgHd
16a29 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72  r *pPg){.  Pager
16a2a 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
16a2b 70 50 61 67 65 72 3b 20 2f 2a 20 50 61 67 65 72  pPager; /* Pager
16a2c 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74   object associat
16a2d 65 64 20 77 69 74 68 20 70 61 67 65 20 70 50 67  ed with page pPg
16a2e 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20   */.  Pgno pgno 
16a2f 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 20 20 20 20  = pPg->pgno;    
16a30 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
16a31 72 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 69  r to read */.  i
16a32 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
16a33 4b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  K;          /* R
16a34 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
16a35 69 6e 74 20 69 73 49 6e 57 61 6c 20 3d 20 30 3b  int isInWal = 0;
16a36 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16a37 54 72 75 65 20 69 66 20 70 61 67 65 20 69 73 20  True if page is 
16a38 69 6e 20 6c 6f 67 20 66 69 6c 65 20 2a 2f 0a 20  in log file */. 
16a39 20 69 6e 74 20 70 67 73 7a 20 3d 20 70 50 61 67   int pgsz = pPag
16a3a 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 20 2f 2a  er->pageSize; /*
16a3b 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
16a3c 20 74 6f 20 72 65 61 64 20 2a 2f 0a 0a 20 20 61   to read */..  a
16a3d 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65  ssert( pPager->e
16a3e 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 41  State>=PAGER_REA
16a3f 44 45 52 20 26 26 20 21 4d 45 4d 44 42 20 29 3b  DER && !MEMDB );
16a40 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65  .  assert( isOpe
16a41 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b  n(pPager->fd) );
16a42 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 21 69  ..  if( NEVER(!i
16a43 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
16a44 29 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  )) ){.    assert
16a45 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  ( pPager->tempFi
16a46 6c 65 20 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74  le );.    memset
16a47 28 70 50 67 2d 3e 70 44 61 74 61 2c 20 30 2c 20  (pPg->pData, 0, 
16a48 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
16a49 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
16a4a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
16a4b 69 66 28 20 70 61 67 65 72 55 73 65 57 61 6c 28  if( pagerUseWal(
16a4c 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 2f  pPager) ){.    /
16a4d 2a 20 54 72 79 20 74 6f 20 70 75 6c 6c 20 74 68  * Try to pull th
16a4e 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  e page from the 
16a4f 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 2e  write-ahead log.
16a50 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
16a51 69 74 65 33 57 61 6c 52 65 61 64 28 70 50 61 67  ite3WalRead(pPag
16a52 65 72 2d 3e 70 57 61 6c 2c 20 70 67 6e 6f 2c 20  er->pWal, pgno, 
16a53 26 69 73 49 6e 57 61 6c 2c 20 70 67 73 7a 2c 20  &isInWal, pgsz, 
16a54 70 50 67 2d 3e 70 44 61 74 61 29 3b 0a 20 20 7d  pPg->pData);.  }
16a55 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
16a56 45 5f 4f 4b 20 26 26 20 21 69 73 49 6e 57 61 6c  E_OK && !isInWal
16a57 20 29 7b 0a 20 20 20 20 69 36 34 20 69 4f 66 66   ){.    i64 iOff
16a58 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28  set = (pgno-1)*(
16a59 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65  i64)pPager->page
16a5a 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 73  Size;.    rc = s
16a5b 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61  qlite3OsRead(pPa
16a5c 67 65 72 2d 3e 66 64 2c 20 70 50 67 2d 3e 70 44  ger->fd, pPg->pD
16a5d 61 74 61 2c 20 70 67 73 7a 2c 20 69 4f 66 66 73  ata, pgsz, iOffs
16a5e 65 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  et);.    if( rc=
16a5f 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48  =SQLITE_IOERR_SH
16a60 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20  ORT_READ ){.    
16a61 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
16a62 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69  ;.    }.  }..  i
16a63 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20  f( pgno==1 ){.  
16a64 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
16a65 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 61 64    /* If the read
16a66 20 69 73 20 75 6e 73 75 63 63 65 73 73 66 75 6c   is unsuccessful
16a67 2c 20 73 65 74 20 74 68 65 20 64 62 46 69 6c 65  , set the dbFile
16a68 56 65 72 73 5b 5d 20 74 6f 20 73 6f 6d 65 74 68  Vers[] to someth
16a69 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 74 68 61  ing.      ** tha
16a6a 74 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  t will never be 
16a6b 61 20 76 61 6c 69 64 20 66 69 6c 65 20 76 65 72  a valid file ver
16a6c 73 69 6f 6e 2e 20 20 64 62 46 69 6c 65 56 65 72  sion.  dbFileVer
16a6d 73 5b 5d 20 69 73 20 61 20 63 6f 70 79 0a 20 20  s[] is a copy.  
16a6e 20 20 20 20 2a 2a 20 6f 66 20 62 79 74 65 73 20      ** of bytes 
16a6f 32 34 2e 2e 33 39 20 6f 66 20 74 68 65 20 64 61  24..39 of the da
16a70 74 61 62 61 73 65 2e 20 20 42 79 74 65 73 20 32  tabase.  Bytes 2
16a71 38 2e 2e 33 31 20 73 68 6f 75 6c 64 20 61 6c 77  8..31 should alw
16a72 61 79 73 20 62 65 0a 20 20 20 20 20 20 2a 2a 20  ays be.      ** 
16a73 7a 65 72 6f 20 6f 72 20 74 68 65 20 73 69 7a 65  zero or the size
16a74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
16a75 20 69 6e 20 70 61 67 65 2e 20 42 79 74 65 73 20   in page. Bytes 
16a76 33 32 2e 2e 33 35 20 61 6e 64 20 33 35 2e 2e 33  32..35 and 35..3
16a77 39 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f 75 6c  9.      ** shoul
16a78 64 20 62 65 20 70 61 67 65 20 6e 75 6d 62 65 72  d be page number
16a79 73 20 77 68 69 63 68 20 61 72 65 20 6e 65 76 65  s which are neve
16a7a 72 20 30 78 66 66 66 66 66 66 66 66 2e 20 20 53  r 0xffffffff.  S
16a7b 6f 20 66 69 6c 6c 69 6e 67 0a 20 20 20 20 20 20  o filling.      
16a7c 2a 2a 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  ** pPager->dbFil
16a7d 65 56 65 72 73 5b 5d 20 77 69 74 68 20 61 6c 6c  eVers[] with all
16a7e 20 30 78 66 66 20 62 79 74 65 73 20 73 68 6f 75   0xff bytes shou
16a7f 6c 64 20 73 75 66 66 69 63 65 2e 0a 20 20 20 20  ld suffice..    
16a80 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 46 6f    **.      ** Fo
16a81 72 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64  r an encrypted d
16a82 61 74 61 62 61 73 65 2c 20 74 68 65 20 73 69 74  atabase, the sit
16a83 75 61 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20 63  uation is more c
16a84 6f 6d 70 6c 65 78 3a 20 20 62 79 74 65 73 0a 20  omplex:  bytes. 
16a85 20 20 20 20 20 2a 2a 20 32 34 2e 2e 33 39 20 6f       ** 24..39 o
16a86 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  f the database a
16a87 72 65 20 77 68 69 74 65 20 6e 6f 69 73 65 2e 20  re white noise. 
16a88 20 42 75 74 20 74 68 65 20 70 72 6f 62 61 62 69   But the probabi
16a89 6c 69 74 79 20 6f 66 0a 20 20 20 20 20 20 2a 2a  lity of.      **
16a8a 20 77 68 69 74 65 20 6e 6f 69 73 69 6e 67 20 65   white noising e
16a8b 71 75 61 6c 69 6e 67 20 31 36 20 62 79 74 65 73  qualing 16 bytes
16a8c 20 6f 66 20 30 78 66 66 20 69 73 20 76 61 6e 69   of 0xff is vani
16a8d 73 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20 73 6f  shingly small so
16a8e 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 73 68 6f  .      ** we sho
16a8f 75 6c 64 20 73 74 69 6c 6c 20 62 65 20 6f 6b 2e  uld still be ok.
16a90 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
16a91 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 64  memset(pPager->d
16a92 62 46 69 6c 65 56 65 72 73 2c 20 30 78 66 66 2c  bFileVers, 0xff,
16a93 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e   sizeof(pPager->
16a94 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20  dbFileVers));.  
16a95 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75    }else{.      u
16a96 38 20 2a 64 62 46 69 6c 65 56 65 72 73 20 3d 20  8 *dbFileVers = 
16a97 26 28 28 75 38 2a 29 70 50 67 2d 3e 70 44 61 74  &((u8*)pPg->pDat
16a98 61 29 5b 32 34 5d 3b 0a 20 20 20 20 20 20 6d 65  a)[24];.      me
16a99 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62  mcpy(&pPager->db
16a9a 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65  FileVers, dbFile
16a9b 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 70 50 61  Vers, sizeof(pPa
16a9c 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29  ger->dbFileVers)
16a9d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 43  );.    }.  }.  C
16a9e 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 50  ODEC1(pPager, pP
16a9f 67 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20  g->pData, pgno, 
16aa0 33 2c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  3, rc = SQLITE_N
16aa1 4f 4d 45 4d 29 3b 0a 0a 20 20 50 41 47 45 52 5f  OMEM);..  PAGER_
16aa2 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67  INCR(sqlite3_pag
16aa3 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 29  er_readdb_count)
16aa4 3b 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70  ;.  PAGER_INCR(p
16aa5 50 61 67 65 72 2d 3e 6e 52 65 61 64 29 3b 0a 20  Pager->nRead);. 
16aa6 20 49 4f 54 52 41 43 45 28 28 22 50 47 49 4e 20   IOTRACE(("PGIN 
16aa7 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72  %p %d\n", pPager
16aa8 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 50 41 47 45  , pgno));.  PAGE
16aa9 52 54 52 41 43 45 28 28 22 46 45 54 43 48 20 25  RTRACE(("FETCH %
16aaa 64 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25  d page %d hash(%
16aab 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20  08x)\n",.       
16aac 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28          PAGERID(
16aad 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70  pPager), pgno, p
16aae 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50  ager_pagehash(pP
16aaf 67 29 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  g)));..  return 
16ab0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70 64  rc;.}../*.** Upd
16ab1 61 74 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ate the value of
16ab2 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e   the change-coun
16ab3 74 65 72 20 61 74 20 6f 66 66 73 65 74 73 20 32  ter at offsets 2
16ab4 34 20 61 6e 64 20 39 32 20 69 6e 0a 2a 2a 20 74  4 and 92 in.** t
16ab5 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 74 68  he header and th
16ab6 65 20 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e  e sqlite version
16ab7 20 6e 75 6d 62 65 72 20 61 74 20 6f 66 66 73 65   number at offse
16ab8 74 20 39 36 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  t 96..**.** This
16ab9 20 69 73 20 61 6e 20 75 6e 63 6f 6e 64 69 74 69   is an unconditi
16aba 6f 6e 61 6c 20 75 70 64 61 74 65 2e 20 20 53 65  onal update.  Se
16abb 65 20 61 6c 73 6f 20 74 68 65 20 70 61 67 65 72  e also the pager
16abc 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e  _incr_changecoun
16abd 74 65 72 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65  ter().** routine
16abe 20 77 68 69 63 68 20 6f 6e 6c 79 20 75 70 64 61   which only upda
16abf 74 65 73 20 74 68 65 20 63 68 61 6e 67 65 2d 63  tes the change-c
16ac0 6f 75 6e 74 65 72 20 69 66 20 74 68 65 20 75 70  ounter if the up
16ac1 64 61 74 65 20 69 73 20 61 63 74 75 61 6c 6c 79  date is actually
16ac2 0a 2a 2a 20 6e 65 65 64 65 64 2c 20 61 73 20 64  .** needed, as d
16ac3 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65  etermined by the
16ac4 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43   pPager->changeC
16ac5 6f 75 6e 74 44 6f 6e 65 20 73 74 61 74 65 20 76  ountDone state v
16ac6 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  ariable..*/.stat
16ac7 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 77 72  ic void pager_wr
16ac8 69 74 65 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65  ite_changecounte
16ac9 72 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20  r(PgHdr *pPg){. 
16aca 20 75 33 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e   u32 change_coun
16acb 74 65 72 3b 0a 0a 20 20 2f 2a 20 49 6e 63 72 65  ter;..  /* Incre
16acc 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6a  ment the value j
16acd 75 73 74 20 72 65 61 64 20 61 6e 64 20 77 72 69  ust read and wri
16ace 74 65 20 69 74 20 62 61 63 6b 20 74 6f 20 62 79  te it back to by
16acf 74 65 20 32 34 2e 20 2a 2f 0a 20 20 63 68 61 6e  te 24. */.  chan
16ad0 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20 73 71 6c  ge_counter = sql
16ad1 69 74 65 33 47 65 74 34 62 79 74 65 28 28 75 38  ite3Get4byte((u8
16ad2 2a 29 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 64  *)pPg->pPager->d
16ad3 62 46 69 6c 65 56 65 72 73 29 2b 31 3b 0a 20 20  bFileVers)+1;.  
16ad4 70 75 74 33 32 62 69 74 73 28 28 28 63 68 61 72  put32bits(((char
16ad5 2a 29 70 50 67 2d 3e 70 44 61 74 61 29 2b 32 34  *)pPg->pData)+24
16ad6 2c 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72  , change_counter
16ad7 29 3b 0a 0a 20 20 2f 2a 20 41 6c 73 6f 20 73 74  );..  /* Also st
16ad8 6f 72 65 20 74 68 65 20 53 51 4c 69 74 65 20 76  ore the SQLite v
16ad9 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 69 6e  ersion number in
16ada 20 62 79 74 65 73 20 39 36 2e 2e 39 39 20 61 6e   bytes 96..99 an
16adb 64 20 69 6e 0a 20 20 2a 2a 20 62 79 74 65 73 20  d in.  ** bytes 
16adc 39 32 2e 2e 39 35 20 73 74 6f 72 65 20 74 68 65  92..95 store the
16add 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
16ade 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 76 65  for which the ve
16adf 72 73 69 6f 6e 20 6e 75 6d 62 65 72 0a 20 20 2a  rsion number.  *
16ae0 2a 20 69 73 20 76 61 6c 69 64 2e 20 2a 2f 0a 20  * is valid. */. 
16ae1 20 70 75 74 33 32 62 69 74 73 28 28 28 63 68 61   put32bits(((cha
16ae2 72 2a 29 70 50 67 2d 3e 70 44 61 74 61 29 2b 39  r*)pPg->pData)+9
16ae3 32 2c 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65  2, change_counte
16ae4 72 29 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28  r);.  put32bits(
16ae5 28 28 63 68 61 72 2a 29 70 50 67 2d 3e 70 44 61  ((char*)pPg->pDa
16ae6 74 61 29 2b 39 36 2c 20 53 51 4c 49 54 45 5f 56  ta)+96, SQLITE_V
16ae7 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 29 3b 0a  ERSION_NUMBER);.
16ae8 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
16ae9 45 5f 4f 4d 49 54 5f 57 41 4c 0a 2f 2a 0a 2a 2a  E_OMIT_WAL./*.**
16aea 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
16aeb 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66  s invoked once f
16aec 6f 72 20 65 61 63 68 20 70 61 67 65 20 74 68 61  or each page tha
16aed 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  t has already be
16aee 65 6e 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69  en .** written i
16aef 6e 74 6f 20 74 68 65 20 6c 6f 67 20 66 69 6c 65  nto the log file
16af0 20 77 68 65 6e 20 61 20 57 41 4c 20 74 72 61 6e   when a WAL tran
16af1 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65  saction is rolle
16af2 64 20 62 61 63 6b 2e 0a 2a 2a 20 50 61 72 61 6d  d back..** Param
16af3 65 74 65 72 20 69 50 67 20 69 73 20 74 68 65 20  eter iPg is the 
16af4 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 73  page number of s
16af5 61 69 64 20 70 61 67 65 2e 20 54 68 65 20 70 43  aid page. The pC
16af6 74 78 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20  tx argument .** 
16af7 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 70 6f  is actually a po
16af8 69 6e 74 65 72 20 74 6f 20 74 68 65 20 50 61 67  inter to the Pag
16af9 65 72 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  er structure..**
16afa 0a 2a 2a 20 49 66 20 70 61 67 65 20 69 50 67 20  .** If page iPg 
16afb 69 73 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68  is present in th
16afc 65 20 63 61 63 68 65 2c 20 61 6e 64 20 68 61 73  e cache, and has
16afd 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   no outstanding 
16afe 72 65 66 65 72 65 6e 63 65 73 2c 0a 2a 2a 20 69  references,.** i
16aff 74 20 69 73 20 64 69 73 63 61 72 64 65 64 2e 20  t is discarded. 
16b00 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68  Otherwise, if th
16b01 65 72 65 20 61 72 65 20 6f 6e 65 20 6f 72 20 6d  ere are one or m
16b02 6f 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a  ore outstanding.
16b03 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 2c 20 74  ** references, t
16b04 68 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20  he page content 
16b05 69 73 20 72 65 6c 6f 61 64 65 64 20 66 72 6f 6d  is reloaded from
16b06 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49   the database. I
16b07 66 20 74 68 65 0a 2a 2a 20 61 74 74 65 6d 70 74  f the.** attempt
16b08 20 74 6f 20 72 65 6c 6f 61 64 20 63 6f 6e 74 65   to reload conte
16b09 6e 74 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  nt from the data
16b0a 62 61 73 65 20 69 73 20 72 65 71 75 69 72 65 64  base is required
16b0b 20 61 6e 64 20 66 61 69 6c 73 2c 20 0a 2a 2a 20   and fails, .** 
16b0c 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65  return an SQLite
16b0d 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 4f 74 68   error code. Oth
16b0e 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f  erwise, SQLITE_O
16b0f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  K..*/.static int
16b10 20 70 61 67 65 72 55 6e 64 6f 43 61 6c 6c 62 61   pagerUndoCallba
16b11 63 6b 28 76 6f 69 64 20 2a 70 43 74 78 2c 20 50  ck(void *pCtx, P
16b12 67 6e 6f 20 69 50 67 29 7b 0a 20 20 69 6e 74 20  gno iPg){.  int 
16b13 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
16b14 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20    Pager *pPager 
16b15 3d 20 28 50 61 67 65 72 20 2a 29 70 43 74 78 3b  = (Pager *)pCtx;
16b16 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a  .  PgHdr *pPg;..
16b17 20 20 70 50 67 20 3d 20 73 71 6c 69 74 65 33 50    pPg = sqlite3P
16b18 61 67 65 72 4c 6f 6f 6b 75 70 28 70 50 61 67 65  agerLookup(pPage
16b19 72 2c 20 69 50 67 29 3b 0a 20 20 69 66 28 20 70  r, iPg);.  if( p
16b1a 50 67 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71  Pg ){.    if( sq
16b1b 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52  lite3PcachePageR
16b1c 65 66 63 6f 75 6e 74 28 70 50 67 29 3d 3d 31 20  efcount(pPg)==1 
16b1d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
16b1e 50 63 61 63 68 65 44 72 6f 70 28 70 50 67 29 3b  PcacheDrop(pPg);
16b1f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
16b20 20 20 72 63 20 3d 20 72 65 61 64 44 62 50 61 67    rc = readDbPag
16b21 65 28 70 50 67 29 3b 0a 20 20 20 20 20 20 69 66  e(pPg);.      if
16b22 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
16b23 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65  ){.        pPage
16b24 72 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 50 67  r->xReiniter(pPg
16b25 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
16b26 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
16b27 65 66 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20  ef(pPg);.    }. 
16b28 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c   }..  /* Normall
16b29 79 2c 20 69 66 20 61 20 74 72 61 6e 73 61 63 74  y, if a transact
16b2a 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61  ion is rolled ba
16b2b 63 6b 2c 20 61 6e 79 20 62 61 63 6b 75 70 20 70  ck, any backup p
16b2c 72 6f 63 65 73 73 65 73 20 61 72 65 0a 20 20 2a  rocesses are.  *
16b2d 2a 20 75 70 64 61 74 65 64 20 61 73 20 64 61 74  * updated as dat
16b2e 61 20 69 73 20 63 6f 70 69 65 64 20 6f 75 74 20  a is copied out 
16b2f 6f 66 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  of the rollback 
16b30 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 6e 74 6f  journal and into
16b31 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61   the.  ** databa
16b32 73 65 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20  se. This is not 
16b33 67 65 6e 65 72 61 6c 6c 79 20 70 6f 73 73 69 62  generally possib
16b34 6c 65 20 77 69 74 68 20 61 20 57 41 4c 20 64 61  le with a WAL da
16b35 74 61 62 61 73 65 2c 20 61 73 0a 20 20 2a 2a 20  tabase, as.  ** 
16b36 72 6f 6c 6c 62 61 63 6b 20 69 6e 76 6f 6c 76 65  rollback involve
16b37 73 20 73 69 6d 70 6c 79 20 74 72 75 6e 63 61 74  s simply truncat
16b38 69 6e 67 20 74 68 65 20 6c 6f 67 20 66 69 6c 65  ing the log file
16b39 2e 20 54 68 65 72 65 66 6f 72 65 2c 20 69 66 20  . Therefore, if 
16b3a 6f 6e 65 0a 20 20 2a 2a 20 6f 72 20 6d 6f 72 65  one.  ** or more
16b3b 20 66 72 61 6d 65 73 20 68 61 76 65 20 61 6c 72   frames have alr
16b3c 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65  eady been writte
16b3d 6e 20 74 6f 20 74 68 65 20 6c 6f 67 20 28 61 6e  n to the log (an
16b3e 64 20 74 68 65 72 65 66 6f 72 65 20 0a 20 20 2a  d therefore .  *
16b3f 2a 20 61 6c 73 6f 20 63 6f 70 69 65 64 20 69 6e  * also copied in
16b40 74 6f 20 74 68 65 20 62 61 63 6b 75 70 20 64 61  to the backup da
16b41 74 61 62 61 73 65 73 29 20 61 73 20 70 61 72 74  tabases) as part
16b42 20 6f 66 20 74 68 69 73 20 74 72 61 6e 73 61 63   of this transac
16b43 74 69 6f 6e 2c 0a 20 20 2a 2a 20 74 68 65 20 62  tion,.  ** the b
16b44 61 63 6b 75 70 73 20 6d 75 73 74 20 62 65 20 72  ackups must be r
16b45 65 73 74 61 72 74 65 64 2e 0a 20 20 2a 2f 0a 20  estarted..  */. 
16b46 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65   sqlite3BackupRe
16b47 73 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70 42  start(pPager->pB
16b48 61 63 6b 75 70 29 3b 0a 0a 20 20 72 65 74 75 72  ackup);..  retur
16b49 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
16b4a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
16b4b 63 61 6c 6c 65 64 20 74 6f 20 72 6f 6c 6c 62 61  called to rollba
16b4c 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ck a transaction
16b4d 20 6f 6e 20 61 20 57 41 4c 20 64 61 74 61 62 61   on a WAL databa
16b4e 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  se..*/.static in
16b4f 74 20 70 61 67 65 72 52 6f 6c 6c 62 61 63 6b 57  t pagerRollbackW
16b50 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  al(Pager *pPager
16b51 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
16b52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b53 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 43       /* Return C
16b54 6f 64 65 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a  ode */.  PgHdr *
16b55 70 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  pList;          
16b56 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74           /* List
16b57 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 73 20   of dirty pages 
16b58 74 6f 20 72 65 76 65 72 74 20 2a 2f 0a 0a 20 20  to revert */..  
16b59 2f 2a 20 46 6f 72 20 61 6c 6c 20 70 61 67 65 73  /* For all pages
16b5a 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 74 68   in the cache th
16b5b 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79  at are currently
16b5c 20 64 69 72 74 79 20 6f 72 20 68 61 76 65 20 61   dirty or have a
16b5d 6c 72 65 61 64 79 0a 20 20 2a 2a 20 62 65 65 6e  lready.  ** been
16b5e 20 77 72 69 74 74 65 6e 20 28 62 75 74 20 6e 6f   written (but no
16b5f 74 20 63 6f 6d 6d 69 74 74 65 64 29 20 74 6f 20  t committed) to 
16b60 74 68 65 20 6c 6f 67 20 66 69 6c 65 2c 20 64 6f  the log file, do
16b61 20 6f 6e 65 20 6f 66 20 74 68 65 20 0a 20 20 2a   one of the .  *
16b62 2a 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 2a  * following:.  *
16b63 2a 0a 20 20 2a 2a 20 20 20 2b 20 44 69 73 63 61  *.  **   + Disca
16b64 72 64 20 74 68 65 20 63 61 63 68 65 64 20 70 61  rd the cached pa
16b65 67 65 20 28 69 66 20 72 65 66 63 6f 75 6e 74 3d  ge (if refcount=
16b66 3d 30 29 2c 20 6f 72 0a 20 20 2a 2a 20 20 20 2b  =0), or.  **   +
16b67 20 52 65 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e   Reload page con
16b68 74 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 64 61  tent from the da
16b69 74 61 62 61 73 65 20 28 69 66 20 72 65 66 63 6f  tabase (if refco
16b6a 75 6e 74 3e 30 29 2e 0a 20 20 2a 2f 0a 20 20 70  unt>0)..  */.  p
16b6b 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20  Pager->dbSize = 
16b6c 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69  pPager->dbOrigSi
16b6d 7a 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ze;.  rc = sqlit
16b6e 65 33 57 61 6c 55 6e 64 6f 28 70 50 61 67 65 72  e3WalUndo(pPager
16b6f 2d 3e 70 57 61 6c 2c 20 70 61 67 65 72 55 6e 64  ->pWal, pagerUnd
16b70 6f 43 61 6c 6c 62 61 63 6b 2c 20 28 76 6f 69 64  oCallback, (void
16b71 20 2a 29 70 50 61 67 65 72 29 3b 0a 20 20 70 4c   *)pPager);.  pL
16b72 69 73 74 20 3d 20 73 71 6c 69 74 65 33 50 63 61  ist = sqlite3Pca
16b73 63 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 61  cheDirtyList(pPa
16b74 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20  ger->pPCache);. 
16b75 20 77 68 69 6c 65 28 20 70 4c 69 73 74 20 26 26   while( pList &&
16b76 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
16b77 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 4e 65  {.    PgHdr *pNe
16b78 78 74 20 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72  xt = pList->pDir
16b79 74 79 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67  ty;.    rc = pag
16b7a 65 72 55 6e 64 6f 43 61 6c 6c 62 61 63 6b 28 28  erUndoCallback((
16b7b 76 6f 69 64 20 2a 29 70 50 61 67 65 72 2c 20 70  void *)pPager, p
16b7c 4c 69 73 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20  List->pgno);.   
16b7d 20 70 4c 69 73 74 20 3d 20 70 4e 65 78 74 3b 0a   pList = pNext;.
16b7e 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
16b7f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
16b80 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72  function is a wr
16b81 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 71 6c  apper around sql
16b82 69 74 65 33 57 61 6c 46 72 61 6d 65 73 28 29 2e  ite3WalFrames().
16b83 20 41 73 20 77 65 6c 6c 20 61 73 20 6c 6f 67 67   As well as logg
16b84 69 6e 67 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65  ing.** the conte
16b85 6e 74 73 20 6f 66 20 74 68 65 20 6c 69 73 74 20  nts of the list 
16b86 6f 66 20 70 61 67 65 73 20 68 65 61 64 65 64 20  of pages headed 
16b87 62 79 20 70 4c 69 73 74 20 28 63 6f 6e 6e 65 63  by pList (connec
16b88 74 65 64 20 62 79 20 70 44 69 72 74 79 29 2c 0a  ted by pDirty),.
16b89 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** this function
16b8a 20 6e 6f 74 69 66 69 65 73 20 61 6e 79 20 61 63   notifies any ac
16b8b 74 69 76 65 20 62 61 63 6b 75 70 20 70 72 6f 63  tive backup proc
16b8c 65 73 73 65 73 20 74 68 61 74 20 74 68 65 20 70  esses that the p
16b8d 61 67 65 73 20 68 61 76 65 0a 2a 2a 20 63 68 61  ages have.** cha
16b8e 6e 67 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65  nged. .**.** The
16b8f 20 6c 69 73 74 20 6f 66 20 70 61 67 65 73 20 70   list of pages p
16b90 61 73 73 65 64 20 69 6e 74 6f 20 74 68 69 73 20  assed into this 
16b91 72 6f 75 74 69 6e 65 20 69 73 20 61 6c 77 61 79  routine is alway
16b92 73 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 65  s sorted by page
16b93 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 48 65 6e 63   number..** Henc
16b94 65 2c 20 69 66 20 70 61 67 65 20 31 20 61 70 70  e, if page 1 app
16b95 65 61 72 73 20 61 6e 79 77 68 65 72 65 20 6f 6e  ears anywhere on
16b96 20 74 68 65 20 6c 69 73 74 2c 20 69 74 20 77 69   the list, it wi
16b97 6c 6c 20 62 65 20 74 68 65 20 66 69 72 73 74 20  ll be the first 
16b98 70 61 67 65 2e 0a 2a 2f 20 0a 73 74 61 74 69 63  page..*/ .static
16b99 20 69 6e 74 20 70 61 67 65 72 57 61 6c 46 72 61   int pagerWalFra
16b9a 6d 65 73 28 0a 20 20 50 61 67 65 72 20 2a 70 50  mes(.  Pager *pP
16b9b 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20  ager,           
16b9c 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20         /* Pager 
16b9d 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 50 67 48 64  object */.  PgHd
16b9e 72 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20  r *pList,       
16b9f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
16ba0 69 73 74 20 6f 66 20 66 72 61 6d 65 73 20 74 6f  ist of frames to
16ba1 20 6c 6f 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e   log */.  Pgno n
16ba2 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20  Truncate,       
16ba3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
16ba4 61 62 61 73 65 20 73 69 7a 65 20 61 66 74 65 72  abase size after
16ba5 20 74 68 69 73 20 63 6f 6d 6d 69 74 20 2a 2f 0a   this commit */.
16ba6 20 20 69 6e 74 20 69 73 43 6f 6d 6d 69 74 20 20    int isCommit  
16ba7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ba8 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
16ba9 73 20 69 73 20 61 20 63 6f 6d 6d 69 74 20 2a 2f  s is a commit */
16baa 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  .){.  int rc;   
16bab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bac 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
16bad 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c  code */.  int nL
16bae 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ist;            
16baf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
16bb0 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
16bb1 70 4c 69 73 74 20 2a 2f 0a 23 69 66 20 64 65 66  pList */.#if def
16bb2 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
16bb3 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  G) || defined(SQ
16bb4 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
16bb5 29 0a 20 20 50 67 48 64 72 20 2a 70 3b 20 20 20  ).  PgHdr *p;   
16bb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bb7 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
16bb8 6e 67 20 6f 76 65 72 20 70 61 67 65 73 20 2a 2f  ng over pages */
16bb9 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72  .#endif..  asser
16bba 74 28 20 70 50 61 67 65 72 2d 3e 70 57 61 6c 20  t( pPager->pWal 
16bbb 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69  );.  assert( pLi
16bbc 73 74 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c  st );.#ifdef SQL
16bbd 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 56  ITE_DEBUG.  /* V
16bbe 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 70  erify that the p
16bbf 61 67 65 20 6c 69 73 74 20 69 73 20 69 6e 20 61  age list is in a
16bc0 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a  ccending order *
16bc1 2f 0a 20 20 66 6f 72 28 70 3d 70 4c 69 73 74 3b  /.  for(p=pList;
16bc2 20 70 20 26 26 20 70 2d 3e 70 44 69 72 74 79 3b   p && p->pDirty;
16bc3 20 70 3d 70 2d 3e 70 44 69 72 74 79 29 7b 0a 20   p=p->pDirty){. 
16bc4 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 67     assert( p->pg
16bc5 6e 6f 20 3c 20 70 2d 3e 70 44 69 72 74 79 2d 3e  no < p->pDirty->
16bc6 70 67 6e 6f 20 29 3b 0a 20 20 7d 0a 23 65 6e 64  pgno );.  }.#end
16bc7 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4c  if..  assert( pL
16bc8 69 73 74 2d 3e 70 44 69 72 74 79 3d 3d 30 20 7c  ist->pDirty==0 |
16bc9 7c 20 69 73 43 6f 6d 6d 69 74 20 29 3b 0a 20 20  | isCommit );.  
16bca 69 66 28 20 69 73 43 6f 6d 6d 69 74 20 29 7b 0a  if( isCommit ){.
16bcb 20 20 20 20 2f 2a 20 49 66 20 61 20 57 41 4c 20      /* If a WAL 
16bcc 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 62  transaction is b
16bcd 65 69 6e 67 20 63 6f 6d 6d 69 74 74 65 64 2c 20  eing committed, 
16bce 74 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e  there is no poin
16bcf 74 20 69 6e 20 77 72 69 74 69 6e 67 0a 20 20 20  t in writing.   
16bd0 20 2a 2a 20 61 6e 79 20 70 61 67 65 73 20 77 69   ** any pages wi
16bd1 74 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20  th page numbers 
16bd2 67 72 65 61 74 65 72 20 74 68 61 6e 20 6e 54 72  greater than nTr
16bd3 75 6e 63 61 74 65 20 69 6e 74 6f 20 74 68 65 20  uncate into the 
16bd4 57 41 4c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a  WAL file..    **
16bd5 20 54 68 65 79 20 77 69 6c 6c 20 6e 65 76 65 72   They will never
16bd6 20 62 65 20 72 65 61 64 20 62 79 20 61 6e 79 20   be read by any 
16bd7 63 6c 69 65 6e 74 2e 20 53 6f 20 72 65 6d 6f 76  client. So remov
16bd8 65 20 74 68 65 6d 20 66 72 6f 6d 20 74 68 65 20  e them from the 
16bd9 70 44 69 72 74 79 0a 20 20 20 20 2a 2a 20 6c 69  pDirty.    ** li
16bda 73 74 20 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20  st here. */.    
16bdb 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 50 67  PgHdr *p;.    Pg
16bdc 48 64 72 20 2a 2a 70 70 4e 65 78 74 20 3d 20 26  Hdr **ppNext = &
16bdd 70 4c 69 73 74 3b 0a 20 20 20 20 6e 4c 69 73 74  pList;.    nList
16bde 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 3d   = 0;.    for(p=
16bdf 70 4c 69 73 74 3b 20 28 2a 70 70 4e 65 78 74 20  pList; (*ppNext 
16be0 3d 20 70 29 21 3d 30 3b 20 70 3d 70 2d 3e 70 44  = p)!=0; p=p->pD
16be1 69 72 74 79 29 7b 0a 20 20 20 20 20 20 69 66 28  irty){.      if(
16be2 20 70 2d 3e 70 67 6e 6f 3c 3d 6e 54 72 75 6e 63   p->pgno<=nTrunc
16be3 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ate ){.        p
16be4 70 4e 65 78 74 20 3d 20 26 70 2d 3e 70 44 69 72  pNext = &p->pDir
16be5 74 79 3b 0a 20 20 20 20 20 20 20 20 6e 4c 69 73  ty;.        nLis
16be6 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  t++;.      }.   
16be7 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
16be8 4c 69 73 74 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  List );.  }else{
16be9 0a 20 20 20 20 6e 4c 69 73 74 20 3d 20 31 3b 0a  .    nList = 1;.
16bea 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 61 53    }.  pPager->aS
16beb 74 61 74 5b 50 41 47 45 52 5f 53 54 41 54 5f 57  tat[PAGER_STAT_W
16bec 52 49 54 45 5d 20 2b 3d 20 6e 4c 69 73 74 3b 0a  RITE] += nList;.
16bed 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 70 67  .  if( pList->pg
16bee 6e 6f 3d 3d 31 20 29 20 70 61 67 65 72 5f 77 72  no==1 ) pager_wr
16bef 69 74 65 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65  ite_changecounte
16bf0 72 28 70 4c 69 73 74 29 3b 0a 20 20 72 63 20 3d  r(pList);.  rc =
16bf1 20 73 71 6c 69 74 65 33 57 61 6c 46 72 61 6d 65   sqlite3WalFrame
16bf2 73 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20  s(pPager->pWal, 
16bf3 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70  .      pPager->p
16bf4 61 67 65 53 69 7a 65 2c 20 70 4c 69 73 74 2c 20  ageSize, pList, 
16bf5 6e 54 72 75 6e 63 61 74 65 2c 20 69 73 43 6f 6d  nTruncate, isCom
16bf6 6d 69 74 2c 20 70 50 61 67 65 72 2d 3e 77 61 6c  mit, pPager->wal
16bf7 53 79 6e 63 46 6c 61 67 73 0a 20 20 29 3b 0a 20  SyncFlags.  );. 
16bf8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
16bf9 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 70 42  OK && pPager->pB
16bfa 61 63 6b 75 70 20 29 7b 0a 20 20 20 20 50 67 48  ackup ){.    PgH
16bfb 64 72 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70  dr *p;.    for(p
16bfc 3d 70 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e  =pList; p; p=p->
16bfd 70 44 69 72 74 79 29 7b 0a 20 20 20 20 20 20 73  pDirty){.      s
16bfe 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61  qlite3BackupUpda
16bff 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b  te(pPager->pBack
16c00 75 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 28 75 38  up, p->pgno, (u8
16c01 20 2a 29 70 2d 3e 70 44 61 74 61 29 3b 0a 20 20   *)p->pData);.  
16c02 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20    }.  }..#ifdef 
16c03 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
16c04 45 53 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c  ES.  pList = sql
16c05 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c  ite3PcacheDirtyL
16c06 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  ist(pPager->pPCa
16c07 63 68 65 29 3b 0a 20 20 66 6f 72 28 70 3d 70 4c  che);.  for(p=pL
16c08 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69  ist; p; p=p->pDi
16c09 72 74 79 29 7b 0a 20 20 20 20 70 61 67 65 72 5f  rty){.    pager_
16c0a 73 65 74 5f 70 61 67 65 68 61 73 68 28 70 29 3b  set_pagehash(p);
16c0b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72  .  }.#endif..  r
16c0c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
16c0d 2a 2a 20 42 65 67 69 6e 20 61 20 72 65 61 64 20  ** Begin a read 
16c0e 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74  transaction on t
16c0f 68 65 20 57 41 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68  he WAL..**.** Th
16c10 69 73 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20  is routine used 
16c11 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 22 70 61  to be called "pa
16c12 67 65 72 4f 70 65 6e 53 6e 61 70 73 68 6f 74 28  gerOpenSnapshot(
16c13 29 22 20 62 65 63 61 75 73 65 20 69 74 20 65 73  )" because it es
16c14 73 65 6e 74 69 61 6c 6c 79 0a 2a 2a 20 6d 61 6b  sentially.** mak
16c15 65 73 20 61 20 73 6e 61 70 73 68 6f 74 20 6f 66  es a snapshot of
16c16 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 74   the database at
16c17 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 6f 69   the current poi
16c18 6e 74 20 69 6e 20 74 69 6d 65 20 61 6e 64 20 70  nt in time and p
16c19 72 65 73 65 72 76 65 73 0a 2a 2a 20 74 68 61 74  reserves.** that
16c1a 20 73 6e 61 70 73 68 6f 74 20 66 6f 72 20 75 73   snapshot for us
16c1b 65 20 62 79 20 74 68 65 20 72 65 61 64 65 72 20  e by the reader 
16c1c 69 6e 20 73 70 69 74 65 20 6f 66 20 63 6f 6e 63  in spite of conc
16c1d 75 72 72 65 6e 74 6c 79 20 63 68 61 6e 67 65 73  urrently changes
16c1e 20 62 79 0a 2a 2a 20 6f 74 68 65 72 20 77 72 69   by.** other wri
16c1f 74 65 72 73 20 6f 72 20 63 68 65 63 6b 70 6f 69  ters or checkpoi
16c20 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  nters..*/.static
16c21 20 69 6e 74 20 70 61 67 65 72 42 65 67 69 6e 52   int pagerBeginR
16c22 65 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 50  eadTransaction(P
16c23 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
16c24 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
16c25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c26 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
16c27 2a 2f 0a 20 20 69 6e 74 20 63 68 61 6e 67 65 64  */.  int changed
16c28 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
16c29 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
16c2a 63 61 63 68 65 20 6d 75 73 74 20 62 65 20 72 65  cache must be re
16c2b 73 65 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  set */..  assert
16c2c 28 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50  ( pagerUseWal(pP
16c2d 61 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72  ager) );.  asser
16c2e 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74  t( pPager->eStat
16c2f 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20 7c 7c  e==PAGER_OPEN ||
16c30 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d   pPager->eState=
16c31 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20 29 3b  =PAGER_READER );
16c32 0a 0a 20 20 2f 2a 20 73 71 6c 69 74 65 33 57 61  ..  /* sqlite3Wa
16c33 6c 45 6e 64 52 65 61 64 54 72 61 6e 73 61 63 74  lEndReadTransact
16c34 69 6f 6e 28 29 20 77 61 73 20 6e 6f 74 20 63 61  ion() was not ca
16c35 6c 6c 65 64 20 66 6f 72 20 74 68 65 20 70 72 65  lled for the pre
16c36 76 69 6f 75 73 0a 20 20 2a 2a 20 74 72 61 6e 73  vious.  ** trans
16c37 61 63 74 69 6f 6e 20 69 6e 20 6c 6f 63 6b 69 6e  action in lockin
16c38 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 49 56 45  g_mode=EXCLUSIVE
16c39 2e 20 20 53 6f 20 63 61 6c 6c 20 69 74 20 6e 6f  .  So call it no
16c3a 77 2e 20 20 49 66 20 77 65 0a 20 20 2a 2a 20 61  w.  If we.  ** a
16c3b 72 65 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f  re in locking_mo
16c3c 64 65 3d 4e 4f 52 4d 41 4c 20 61 6e 64 20 45 6e  de=NORMAL and En
16c3d 64 52 65 61 64 28 29 20 77 61 73 20 70 72 65 76  dRead() was prev
16c3e 69 6f 75 73 6c 79 20 63 61 6c 6c 65 64 2c 0a 20  iously called,. 
16c3f 20 2a 2a 20 74 68 65 20 64 75 70 6c 69 63 61 74   ** the duplicat
16c40 65 20 63 61 6c 6c 20 69 73 20 68 61 72 6d 6c 65  e call is harmle
16c41 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ss..  */.  sqlit
16c42 65 33 57 61 6c 45 6e 64 52 65 61 64 54 72 61 6e  e3WalEndReadTran
16c43 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2d 3e  saction(pPager->
16c44 70 57 61 6c 29 3b 0a 0a 20 20 72 63 20 3d 20 73  pWal);..  rc = s
16c45 71 6c 69 74 65 33 57 61 6c 42 65 67 69 6e 52 65  qlite3WalBeginRe
16c46 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50  adTransaction(pP
16c47 61 67 65 72 2d 3e 70 57 61 6c 2c 20 26 63 68 61  ager->pWal, &cha
16c48 6e 67 65 64 29 3b 0a 20 20 69 66 28 20 72 63 21  nged);.  if( rc!
16c49 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 63 68  =SQLITE_OK || ch
16c4a 61 6e 67 65 64 20 29 7b 0a 20 20 20 20 70 61 67  anged ){.    pag
16c4b 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29  er_reset(pPager)
16c4c 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
16c4d 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  rc;.}.#endif../*
16c4e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
16c4f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 73 20 70  n is called as p
16c50 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73  art of the trans
16c51 69 74 69 6f 6e 20 66 72 6f 6d 20 50 41 47 45 52  ition from PAGER
16c52 5f 4f 50 45 4e 0a 2a 2a 20 74 6f 20 50 41 47 45  _OPEN.** to PAGE
16c53 52 5f 52 45 41 44 45 52 20 73 74 61 74 65 20 74  R_READER state t
16c54 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
16c55 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61  size of the data
16c56 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 69 6e 20  base file.** in 
16c57 70 61 67 65 73 20 28 61 73 73 75 6d 69 6e 67 20  pages (assuming 
16c58 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 75  the page size cu
16c59 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69  rrently stored i
16c5a 6e 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65  n Pager.pageSize
16c5b 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65  )..**.** If no e
16c5c 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 53 51 4c  rror occurs, SQL
16c5d 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
16c5e 65 64 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20  ed and the size 
16c5f 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  of the database.
16c60 2a 2a 20 69 6e 20 70 61 67 65 73 20 69 73 20 73  ** in pages is s
16c61 74 6f 72 65 64 20 69 6e 20 2a 70 6e 50 61 67 65  tored in *pnPage
16c62 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20  . Otherwise, an 
16c63 65 72 72 6f 72 20 63 6f 64 65 20 28 70 65 72 68  error code (perh
16c64 61 70 73 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f  aps.** SQLITE_IO
16c65 45 52 52 5f 46 53 54 41 54 29 20 69 73 20 72 65  ERR_FSTAT) is re
16c66 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 6e 50 61  turned and *pnPa
16c67 67 65 20 69 73 20 6c 65 66 74 20 75 6e 6d 6f 64  ge is left unmod
16c68 69 66 69 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ified..*/.static
16c69 20 69 6e 74 20 70 61 67 65 72 50 61 67 65 63 6f   int pagerPageco
16c6a 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65  unt(Pager *pPage
16c6b 72 2c 20 50 67 6e 6f 20 2a 70 6e 50 61 67 65 29  r, Pgno *pnPage)
16c6c 7b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20  {.  Pgno nPage; 
16c6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c6e 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
16c6f 72 65 74 75 72 6e 20 76 69 61 20 2a 70 6e 50 61  return via *pnPa
16c70 67 65 20 2a 2f 0a 0a 20 20 2f 2a 20 51 75 65 72  ge */..  /* Quer
16c71 79 20 74 68 65 20 57 41 4c 20 73 75 62 2d 73 79  y the WAL sub-sy
16c72 73 74 65 6d 20 66 6f 72 20 74 68 65 20 64 61 74  stem for the dat
16c73 61 62 61 73 65 20 73 69 7a 65 2e 20 54 68 65 20  abase size. The 
16c74 57 61 6c 44 62 73 69 7a 65 28 29 0a 20 20 2a 2a  WalDbsize().  **
16c75 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
16c76 73 20 7a 65 72 6f 20 69 66 20 74 68 65 20 57 41  s zero if the WA
16c77 4c 20 69 73 20 6e 6f 74 20 6f 70 65 6e 20 28 69  L is not open (i
16c78 2e 65 2e 20 50 61 67 65 72 2e 70 57 61 6c 3d 3d  .e. Pager.pWal==
16c79 30 29 2c 20 6f 72 0a 20 20 2a 2a 20 69 66 20 74  0), or.  ** if t
16c7a 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65  he database size
16c7b 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c   is not availabl
16c7c 65 2e 20 54 68 65 20 64 61 74 61 62 61 73 65 20  e. The database 
16c7d 73 69 7a 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a  size is not.  **
16c7e 20 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20   available from 
16c7f 74 68 65 20 57 41 4c 20 73 75 62 2d 73 79 73 74  the WAL sub-syst
16c80 65 6d 20 69 66 20 74 68 65 20 6c 6f 67 20 66 69  em if the log fi
16c81 6c 65 20 69 73 20 65 6d 70 74 79 20 6f 72 0a 20  le is empty or. 
16c82 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20   ** contains no 
16c83 76 61 6c 69 64 20 63 6f 6d 6d 69 74 74 65 64 20  valid committed 
16c84 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 20  transactions..  
16c85 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
16c86 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47  ger->eState==PAG
16c87 45 52 5f 4f 50 45 4e 20 29 3b 0a 20 20 61 73 73  ER_OPEN );.  ass
16c88 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f  ert( pPager->eLo
16c89 63 6b 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ck>=SHARED_LOCK 
16c8a 29 3b 0a 20 20 6e 50 61 67 65 20 3d 20 73 71 6c  );.  nPage = sql
16c8b 69 74 65 33 57 61 6c 44 62 73 69 7a 65 28 70 50  ite3WalDbsize(pP
16c8c 61 67 65 72 2d 3e 70 57 61 6c 29 3b 0a 0a 20 20  ager->pWal);..  
16c8d 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  /* If the databa
16c8e 73 65 20 73 69 7a 65 20 77 61 73 20 6e 6f 74 20  se size was not 
16c8f 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74  available from t
16c90 68 65 20 57 41 4c 20 73 75 62 2d 73 79 73 74 65  he WAL sub-syste
16c91 6d 2c 0a 20 20 2a 2a 20 64 65 74 65 72 6d 69 6e  m,.  ** determin
16c92 65 20 69 74 20 62 61 73 65 64 20 6f 6e 20 74 68  e it based on th
16c93 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  e size of the da
16c94 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20  tabase file. If 
16c95 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66  the size.  ** of
16c96 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
16c97 6c 65 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74  le is not an int
16c98 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66  eger multiple of
16c99 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 2c 0a   the page-size,.
16c9a 20 20 2a 2a 20 72 6f 75 6e 64 20 64 6f 77 6e 20    ** round down 
16c9b 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 70  to the nearest p
16c9c 61 67 65 2e 20 45 78 63 65 70 74 2c 20 61 6e 79  age. Except, any
16c9d 20 66 69 6c 65 20 6c 61 72 67 65 72 20 74 68 61   file larger tha
16c9e 6e 20 30 0a 20 20 2a 2a 20 62 79 74 65 73 20 69  n 0.  ** bytes i
16c9f 6e 20 73 69 7a 65 20 69 73 20 63 6f 6e 73 69 64  n size is consid
16ca0 65 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20  ered to contain 
16ca1 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 70 61 67  at least one pag
16ca2 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 50  e..  */.  if( nP
16ca3 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 36  age==0 ){.    i6
16ca4 34 20 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20  4 n = 0;        
16ca5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
16ca6 69 7a 65 20 6f 66 20 64 62 20 66 69 6c 65 20 69  ize of db file i
16ca7 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 61  n bytes */.    a
16ca8 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
16ca9 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61  ager->fd) || pPa
16caa 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b  ger->tempFile );
16cab 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28  .    if( isOpen(
16cac 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20  pPager->fd) ){. 
16cad 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71       int rc = sq
16cae 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
16caf 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 6e 29 3b  pPager->fd, &n);
16cb0 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
16cb1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
16cb2 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
16cb3 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
16cb4 20 6e 50 61 67 65 20 3d 20 28 50 67 6e 6f 29 28   nPage = (Pgno)(
16cb5 28 6e 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53  (n+pPager->pageS
16cb6 69 7a 65 2d 31 29 20 2f 20 70 50 61 67 65 72 2d  ize-1) / pPager-
16cb7 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 7d 0a  >pageSize);.  }.
16cb8 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72  .  /* If the cur
16cb9 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 70  rent number of p
16cba 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65  ages in the file
16cbb 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
16cbc 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6e 66 69 67   the.  ** config
16cbd 75 72 65 64 20 6d 61 78 69 6d 75 6d 20 70 61 67  ured maximum pag
16cbe 65 72 20 6e 75 6d 62 65 72 2c 20 69 6e 63 72 65  er number, incre
16cbf 61 73 65 20 74 68 65 20 61 6c 6c 6f 77 65 64 20  ase the allowed 
16cc0 6c 69 6d 69 74 20 73 6f 0a 20 20 2a 2a 20 74 68  limit so.  ** th
16cc1 61 74 20 74 68 65 20 66 69 6c 65 20 63 61 6e 20  at the file can 
16cc2 62 65 20 72 65 61 64 2e 0a 20 20 2a 2f 0a 20 20  be read..  */.  
16cc3 69 66 28 20 6e 50 61 67 65 3e 70 50 61 67 65 72  if( nPage>pPager
16cc4 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20 20  ->mxPgno ){.    
16cc5 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d  pPager->mxPgno =
16cc6 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a 20 20   (Pgno)nPage;.  
16cc7 7d 0a 0a 20 20 2a 70 6e 50 61 67 65 20 3d 20 6e  }..  *pnPage = n
16cc8 50 61 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 53  Page;.  return S
16cc9 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66  QLITE_OK;.}..#if
16cca 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16ccb 5f 57 41 4c 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  _WAL./*.** Check
16ccc 20 69 66 20 74 68 65 20 2a 2d 77 61 6c 20 66 69   if the *-wal fi
16ccd 6c 65 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f  le that correspo
16cce 6e 64 73 20 74 6f 20 74 68 65 20 64 61 74 61 62  nds to the datab
16ccf 61 73 65 20 6f 70 65 6e 65 64 20 62 79 20 70 50  ase opened by pP
16cd0 61 67 65 72 0a 2a 2a 20 65 78 69 73 74 73 20 69  ager.** exists i
16cd1 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  f the database i
16cd2 73 20 6e 6f 74 20 65 6d 70 79 2c 20 6f 72 20 76  s not empy, or v
16cd3 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 2a  erify that the *
16cd4 2d 77 61 6c 20 66 69 6c 65 20 64 6f 65 73 0a 2a  -wal file does.*
16cd5 2a 20 6e 6f 74 20 65 78 69 73 74 20 28 62 79 20  * not exist (by 
16cd6 64 65 6c 65 74 69 6e 67 20 69 74 29 20 69 66 20  deleting it) if 
16cd7 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
16cd8 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a  e is empty..**.*
16cd9 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
16cda 65 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 61  e is not empty a
16cdb 6e 64 20 74 68 65 20 2a 2d 77 61 6c 20 66 69 6c  nd the *-wal fil
16cdc 65 20 65 78 69 73 74 73 2c 20 6f 70 65 6e 20 74  e exists, open t
16cdd 68 65 20 70 61 67 65 72 0a 2a 2a 20 69 6e 20 57  he pager.** in W
16cde 41 4c 20 6d 6f 64 65 2e 20 20 49 66 20 74 68 65  AL mode.  If the
16cdf 20 64 61 74 61 62 61 73 65 20 69 73 20 65 6d 70   database is emp
16ce0 74 79 20 6f 72 20 69 66 20 6e 6f 20 2a 2d 77 61  ty or if no *-wa
16ce1 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e  l file exists an
16ce2 64 0a 2a 2a 20 69 66 20 6e 6f 20 65 72 72 6f 72  d.** if no error
16ce3 20 6f 63 63 75 72 73 2c 20 6d 61 6b 65 20 73 75   occurs, make su
16ce4 72 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c  re Pager.journal
16ce5 4d 6f 64 65 20 69 73 20 6e 6f 74 20 73 65 74 20  Mode is not set 
16ce6 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 4a 4f 55 52  to.** PAGER_JOUR
16ce7 4e 41 4c 4d 4f 44 45 5f 57 41 4c 2e 0a 2a 2a 0a  NALMODE_WAL..**.
16ce8 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
16ce9 5f 4f 4b 20 6f 72 20 61 6e 20 65 72 72 6f 72 20  _OK or an error 
16cea 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  code..**.** The 
16ceb 63 61 6c 6c 65 72 20 6d 75 73 74 20 68 6f 6c 64  caller must hold
16cec 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 6f   a SHARED lock o
16ced 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
16cee 69 6c 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73  ile to call this
16cef 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 42 65  .** function. Be
16cf0 63 61 75 73 65 20 61 6e 20 45 58 43 4c 55 53 49  cause an EXCLUSI
16cf1 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  VE lock on the d
16cf2 62 20 66 69 6c 65 20 69 73 20 72 65 71 75 69 72  b file is requir
16cf3 65 64 20 74 6f 20 64 65 6c 65 74 65 20 0a 2a 2a  ed to delete .**
16cf4 20 61 20 57 41 4c 20 6f 6e 20 61 20 6e 6f 6e 65   a WAL on a none
16cf5 2d 65 6d 70 74 79 20 64 61 74 61 62 61 73 65 2c  -empty database,
16cf6 20 74 68 69 73 20 65 6e 73 75 72 65 73 20 74 68   this ensures th
16cf7 65 72 65 20 69 73 20 6e 6f 20 72 61 63 65 20 63  ere is no race c
16cf8 6f 6e 64 69 74 69 6f 6e 20 0a 2a 2a 20 62 65 74  ondition .** bet
16cf9 77 65 65 6e 20 74 68 65 20 78 41 63 63 65 73 73  ween the xAccess
16cfa 28 29 20 62 65 6c 6f 77 20 61 6e 64 20 61 6e 20  () below and an 
16cfb 78 44 65 6c 65 74 65 28 29 20 62 65 69 6e 67 20  xDelete() being 
16cfc 65 78 65 63 75 74 65 64 20 62 79 20 73 6f 6d 65  executed by some
16cfd 20 0a 2a 2a 20 6f 74 68 65 72 20 63 6f 6e 6e 65   .** other conne
16cfe 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
16cff 20 69 6e 74 20 70 61 67 65 72 4f 70 65 6e 57 61   int pagerOpenWa
16d00 6c 49 66 50 72 65 73 65 6e 74 28 50 61 67 65 72  lIfPresent(Pager
16d01 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74   *pPager){.  int
16d02 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
16d03 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
16d04 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
16d05 5f 4f 50 45 4e 20 29 3b 0a 20 20 61 73 73 65 72  _OPEN );.  asser
16d06 74 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b  t( pPager->eLock
16d07 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  >=SHARED_LOCK );
16d08 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d  ..  if( !pPager-
16d09 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20  >tempFile ){.   
16d0a 20 69 6e 74 20 69 73 57 61 6c 3b 20 20 20 20 20   int isWal;     
16d0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16d0c 2a 20 54 72 75 65 20 69 66 20 57 41 4c 20 66 69  * True if WAL fi
16d0d 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20  le exists */.   
16d0e 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 20   Pgno nPage;    
16d0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16d10 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 64 61  * Size of the da
16d11 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a  tabase file */..
16d12 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 50 61      rc = pagerPa
16d13 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20  gecount(pPager, 
16d14 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  &nPage);.    if(
16d15 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
16d16 0a 20 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d  .    if( nPage==
16d17 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
16d18 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
16d19 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50  pPager->pVfs, pP
16d1a 61 67 65 72 2d 3e 7a 57 61 6c 2c 20 30 29 3b 0a  ager->zWal, 0);.
16d1b 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
16d1c 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54  LITE_IOERR_DELET
16d1d 45 5f 4e 4f 45 4e 54 20 29 20 72 63 20 3d 20 53  E_NOENT ) rc = S
16d1e 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
16d1f 69 73 57 61 6c 20 3d 20 30 3b 0a 20 20 20 20 7d  isWal = 0;.    }
16d20 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
16d21 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73   sqlite3OsAccess
16d22 28 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67  (.          pPag
16d23 65 72 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72  er->pVfs, pPager
16d24 2d 3e 7a 57 61 6c 2c 20 53 51 4c 49 54 45 5f 41  ->zWal, SQLITE_A
16d25 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 69  CCESS_EXISTS, &i
16d26 73 57 61 6c 0a 20 20 20 20 20 20 29 3b 0a 20 20  sWal.      );.  
16d27 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
16d28 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
16d29 20 20 20 69 66 28 20 69 73 57 61 6c 20 29 7b 0a     if( isWal ){.
16d2a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
16d2b 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50  ( sqlite3PcacheP
16d2c 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d  agecount(pPager-
16d2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a  >pPCache)==0 );.
16d2e 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
16d2f 69 74 65 33 50 61 67 65 72 4f 70 65 6e 57 61 6c  ite3PagerOpenWal
16d30 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20  (pPager, 0);.   
16d31 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61     }else if( pPa
16d32 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
16d33 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
16d34 4f 44 45 5f 57 41 4c 20 29 7b 0a 20 20 20 20 20  ODE_WAL ){.     
16d35 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
16d36 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a  alMode = PAGER_J
16d37 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54  OURNALMODE_DELET
16d38 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  E;.      }.    }
16d39 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
16d3a 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
16d3b 2a 20 50 6c 61 79 62 61 63 6b 20 73 61 76 65 70  * Playback savep
16d3c 6f 69 6e 74 20 70 53 61 76 65 70 6f 69 6e 74 2e  oint pSavepoint.
16d3d 20 4f 72 2c 20 69 66 20 70 53 61 76 65 70 6f 69   Or, if pSavepoi
16d3e 6e 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 70  nt==NULL, then p
16d3f 6c 61 79 62 61 63 6b 0a 2a 2a 20 74 68 65 20 65  layback.** the e
16d40 6e 74 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75  ntire master jou
16d41 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 63  rnal file. The c
16d42 61 73 65 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d  ase pSavepoint==
16d43 4e 55 4c 4c 20 6f 63 63 75 72 73 20 77 68 65 6e  NULL occurs when
16d44 20 0a 2a 2a 20 61 20 52 4f 4c 4c 42 41 43 4b 20   .** a ROLLBACK 
16d45 54 4f 20 63 6f 6d 6d 61 6e 64 20 69 73 20 69 6e  TO command is in
16d46 76 6f 6b 65 64 20 6f 6e 20 61 20 53 41 56 45 50  voked on a SAVEP
16d47 4f 49 4e 54 20 74 68 61 74 20 69 73 20 61 20 74  OINT that is a t
16d48 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 73  ransaction .** s
16d49 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20  avepoint..**.** 
16d4a 57 68 65 6e 20 70 53 61 76 65 70 6f 69 6e 74 20  When pSavepoint 
16d4b 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65 61  is not NULL (mea
16d4c 6e 69 6e 67 20 61 20 6e 6f 6e 2d 74 72 61 6e 73  ning a non-trans
16d4d 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74  action savepoint
16d4e 20 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20 72 6f   is .** being ro
16d4f 6c 6c 65 64 20 62 61 63 6b 29 2c 20 74 68 65 6e  lled back), then
16d50 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 6f   the rollback co
16d51 6e 73 69 73 74 73 20 6f 66 20 75 70 20 74 6f 20  nsists of up to 
16d52 74 68 72 65 65 20 73 74 61 67 65 73 2c 0a 2a 2a  three stages,.**
16d53 20 70 65 72 66 6f 72 6d 65 64 20 69 6e 20 74 68   performed in th
16d54 65 20 6f 72 64 65 72 20 73 70 65 63 69 66 69 65  e order specifie
16d55 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67  d:.**.**   * Pag
16d56 65 73 20 61 72 65 20 70 6c 61 79 65 64 20 62 61  es are played ba
16d57 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e  ck from the main
16d58 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e   journal startin
16d59 67 20 61 74 20 62 79 74 65 0a 2a 2a 20 20 20 20  g at byte.**    
16d5a 20 6f 66 66 73 65 74 20 50 61 67 65 72 53 61 76   offset PagerSav
16d5b 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 61  epoint.iOffset a
16d5c 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f  nd continuing to
16d5d 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61   .**     PagerSa
16d5e 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73  vepoint.iHdrOffs
16d5f 65 74 2c 20 6f 72 20 74 6f 20 74 68 65 20 65 6e  et, or to the en
16d60 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f  d of the main jo
16d61 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c  urnal.**     fil
16d62 65 20 69 66 20 50 61 67 65 72 53 61 76 65 70 6f  e if PagerSavepo
16d63 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69  int.iHdrOffset i
16d64 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20  s zero..**.**   
16d65 2a 20 49 66 20 50 61 67 65 72 53 61 76 65 70 6f  * If PagerSavepo
16d66 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69  int.iHdrOffset i
16d67 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e  s not zero, then
16d68 20 70 61 67 65 73 20 61 72 65 20 70 6c 61 79 65   pages are playe
16d69 64 0a 2a 2a 20 20 20 20 20 62 61 63 6b 20 73 74  d.**     back st
16d6a 61 72 74 69 6e 67 20 66 72 6f 6d 20 74 68 65 20  arting from the 
16d6b 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69  journal header i
16d6c 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f  mmediately follo
16d6d 77 69 6e 67 20 0a 2a 2a 20 20 20 20 20 50 61 67  wing .**     Pag
16d6e 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72  erSavepoint.iHdr
16d6f 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 65 6e  Offset to the en
16d70 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f  d of the main jo
16d71 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  urnal file..**.*
16d72 2a 20 20 20 2a 20 50 61 67 65 73 20 61 72 65 20  *   * Pages are 
16d73 74 68 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b  then played back
16d74 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f   from the sub-jo
16d75 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 74 61 72  urnal file, star
16d76 74 69 6e 67 0a 2a 2a 20 20 20 20 20 77 69 74 68  ting.**     with
16d77 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f   the PagerSavepo
16d78 69 6e 74 2e 69 53 75 62 52 65 63 20 61 6e 64 20  int.iSubRec and 
16d79 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68  continuing to th
16d7a 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 20 20 20 20  e end of.**     
16d7b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
16d7c 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 6f 75 67 68 6f  ..**.** Througho
16d7d 75 74 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  ut the rollback 
16d7e 70 72 6f 63 65 73 73 2c 20 65 61 63 68 20 74 69  process, each ti
16d7f 6d 65 20 61 20 70 61 67 65 20 69 73 20 72 6f 6c  me a page is rol
16d80 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 0a 2a 2a  led back, the.**
16d81 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62   corresponding b
16d82 69 74 20 69 73 20 73 65 74 20 69 6e 20 61 20 62  it is set in a b
16d83 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65 20  itvec structure 
16d84 28 76 61 72 69 61 62 6c 65 20 70 44 6f 6e 65 20  (variable pDone 
16d85 69 6e 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d  in the.** implem
16d86 65 6e 74 61 74 69 6f 6e 20 62 65 6c 6f 77 29 2e  entation below).
16d87 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f   This is used to
16d88 20 65 6e 73 75 72 65 20 74 68 61 74 20 61 20 70   ensure that a p
16d89 61 67 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72  age is only.** r
16d8a 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 66  olled back the f
16d8b 69 72 73 74 20 74 69 6d 65 20 69 74 20 69 73 20  irst time it is 
16d8c 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 6e 20 65  encountered in e
16d8d 69 74 68 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 2a  ither journal..*
16d8e 2a 0a 2a 2a 20 49 66 20 70 53 61 76 65 70 6f 69  *.** If pSavepoi
16d8f 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  nt is NULL, then
16d90 20 70 61 67 65 73 20 61 72 65 20 6f 6e 6c 79 20   pages are only 
16d91 70 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f 6d  played back from
16d92 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75   the main.** jou
16d93 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 65  rnal file. There
16d94 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20   is no need for 
16d95 61 20 62 69 74 76 65 63 20 69 6e 20 74 68 69 73  a bitvec in this
16d96 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20   case..**.** In 
16d97 65 69 74 68 65 72 20 63 61 73 65 2c 20 62 65 66  either case, bef
16d98 6f 72 65 20 70 6c 61 79 62 61 63 6b 20 63 6f 6d  ore playback com
16d99 6d 65 6e 63 65 73 20 74 68 65 20 50 61 67 65 72  mences the Pager
16d9a 2e 64 62 53 69 7a 65 20 76 61 72 69 61 62 6c 65  .dbSize variable
16d9b 0a 2a 2a 20 69 73 20 72 65 73 65 74 20 74 6f 20  .** is reset to 
16d9c 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 69  the value that i
16d9d 74 20 68 65 6c 64 20 61 74 20 74 68 65 20 73 74  t held at the st
16d9e 61 72 74 20 6f 66 20 74 68 65 20 73 61 76 65 70  art of the savep
16d9f 6f 69 6e 74 20 0a 2a 2a 20 28 6f 72 20 74 72 61  oint .** (or tra
16da0 6e 73 61 63 74 69 6f 6e 29 2e 20 4e 6f 20 70 61  nsaction). No pa
16da1 67 65 20 77 69 74 68 20 61 20 70 61 67 65 2d 6e  ge with a page-n
16da2 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 68  umber greater th
16da3 61 6e 20 74 68 69 73 20 76 61 6c 75 65 0a 2a 2a  an this value.**
16da4 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b 2e   is played back.
16da5 20 49 66 20 6f 6e 65 20 69 73 20 65 6e 63 6f 75   If one is encou
16da6 6e 74 65 72 65 64 20 69 74 20 69 73 20 73 69 6d  ntered it is sim
16da7 70 6c 79 20 73 6b 69 70 70 65 64 2e 0a 2a 2f 0a  ply skipped..*/.
16da8 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
16da9 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e  PlaybackSavepoin
16daa 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  t(Pager *pPager,
16dab 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20   PagerSavepoint 
16dac 2a 70 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20  *pSavepoint){.  
16dad 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20  i64 szJ;        
16dae 20 20 20 20 20 20 20 20 20 2f 2a 20 45 66 66 65           /* Effe
16daf 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68  ctive size of th
16db0 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a  e main journal *
16db1 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 3b  /.  i64 iHdrOff;
16db2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16db3 45 6e 64 20 6f 66 20 66 69 72 73 74 20 73 65 67  End of first seg
16db4 6d 65 6e 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f 75  ment of main-jou
16db5 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 2a 2f 0a  rnal records */.
16db6 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
16db7 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65  E_OK;      /* Re
16db8 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42  turn code */.  B
16db9 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 3d 20 30  itvec *pDone = 0
16dba 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 74 76 65  ;       /* Bitve
16dbb 63 20 74 6f 20 65 6e 73 75 72 65 20 70 61 67 65  c to ensure page
16dbc 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 6e  s played back on
16dbd 6c 79 20 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61 73  ly once */..  as
16dbe 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53  sert( pPager->eS
16dbf 74 61 74 65 21 3d 50 41 47 45 52 5f 45 52 52 4f  tate!=PAGER_ERRO
16dc0 52 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  R );.  assert( p
16dc1 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50  Pager->eState>=P
16dc2 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b  AGER_WRITER_LOCK
16dc3 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  ED );..  /* Allo
16dc4 63 61 74 65 20 61 20 62 69 74 76 65 63 20 74 6f  cate a bitvec to
16dc5 20 75 73 65 20 74 6f 20 73 74 6f 72 65 20 74 68   use to store th
16dc6 65 20 73 65 74 20 6f 66 20 70 61 67 65 73 20 72  e set of pages r
16dc7 6f 6c 6c 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20  olled back */.  
16dc8 69 66 28 20 70 53 61 76 65 70 6f 69 6e 74 20 29  if( pSavepoint )
16dc9 7b 0a 20 20 20 20 70 44 6f 6e 65 20 3d 20 73 71  {.    pDone = sq
16dca 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74  lite3BitvecCreat
16dcb 65 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f  e(pSavepoint->nO
16dcc 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20 21 70  rig);.    if( !p
16dcd 44 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20 72 65  Done ){.      re
16dce 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
16dcf 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  M;.    }.  }..  
16dd0 2f 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62  /* Set the datab
16dd1 61 73 65 20 73 69 7a 65 20 62 61 63 6b 20 74 6f  ase size back to
16dd2 20 74 68 65 20 76 61 6c 75 65 20 69 74 20 77 61   the value it wa
16dd3 73 20 62 65 66 6f 72 65 20 74 68 65 20 73 61 76  s before the sav
16dd4 65 70 6f 69 6e 74 20 0a 20 20 2a 2a 20 62 65 69  epoint .  ** bei
16dd5 6e 67 20 72 65 76 65 72 74 65 64 20 77 61 73 20  ng reverted was 
16dd6 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70  opened..  */.  p
16dd7 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20  Pager->dbSize = 
16dd8 70 53 61 76 65 70 6f 69 6e 74 20 3f 20 70 53 61  pSavepoint ? pSa
16dd9 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a  vepoint->nOrig :
16dda 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53   pPager->dbOrigS
16ddb 69 7a 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 63  ize;.  pPager->c
16ddc 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d  hangeCountDone =
16ddd 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
16dde 65 3b 0a 0a 20 20 69 66 28 20 21 70 53 61 76 65  e;..  if( !pSave
16ddf 70 6f 69 6e 74 20 26 26 20 70 61 67 65 72 55 73  point && pagerUs
16de0 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a  eWal(pPager) ){.
16de1 20 20 20 20 72 65 74 75 72 6e 20 70 61 67 65 72      return pager
16de2 52 6f 6c 6c 62 61 63 6b 57 61 6c 28 70 50 61 67  RollbackWal(pPag
16de3 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55  er);.  }..  /* U
16de4 73 65 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  se pPager->journ
16de5 61 6c 4f 66 66 20 61 73 20 74 68 65 20 65 66 66  alOff as the eff
16de6 65 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74  ective size of t
16de7 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b  he main rollback
16de8 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20  .  ** journal.  
16de9 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20  The actual file 
16dea 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65 72 20  might be larger 
16deb 74 68 61 6e 20 74 68 69 73 20 69 6e 0a 20 20 2a  than this in.  *
16dec 2a 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  * PAGER_JOURNALM
16ded 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 6f 72 20  ODE_TRUNCATE or 
16dee 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
16def 45 5f 50 45 52 53 49 53 54 2e 20 20 42 75 74 20  E_PERSIST.  But 
16df0 61 6e 79 74 68 69 6e 67 0a 20 20 2a 2a 20 70 61  anything.  ** pa
16df1 73 74 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  st pPager->journ
16df2 61 6c 4f 66 66 20 69 73 20 6f 66 66 2d 6c 69 6d  alOff is off-lim
16df3 69 74 73 20 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a  its to us..  */.
16df4 20 20 73 7a 4a 20 3d 20 70 50 61 67 65 72 2d 3e    szJ = pPager->
16df5 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 61 73  journalOff;.  as
16df6 73 65 72 74 28 20 70 61 67 65 72 55 73 65 57 61  sert( pagerUseWa
16df7 6c 28 70 50 61 67 65 72 29 3d 3d 30 20 7c 7c 20  l(pPager)==0 || 
16df8 73 7a 4a 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  szJ==0 );..  /* 
16df9 42 65 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e 67  Begin by rolling
16dfa 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 66 72   back records fr
16dfb 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  om the main jour
16dfc 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a  nal starting at.
16dfd 20 20 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f    ** PagerSavepo
16dfe 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20  int.iOffset and 
16dff 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68  continuing to th
16e00 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68  e next journal h
16e01 65 61 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 72  eader..  ** Ther
16e02 65 20 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72  e might be recor
16e03 64 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a  ds in the main j
16e04 6f 75 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65  ournal that have
16e05 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20   a page number. 
16e06 20 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e   ** greater than
16e07 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74   the current dat
16e08 61 62 61 73 65 20 73 69 7a 65 20 28 70 50 61 67  abase size (pPag
16e09 65 72 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20  er->dbSize) but 
16e0a 74 68 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c 20  those.  ** will 
16e0b 62 65 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d  be skipped autom
16e0c 61 74 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73  atically.  Pages
16e0d 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 70 44   are added to pD
16e0e 6f 6e 65 20 61 73 20 74 68 65 79 0a 20 20 2a 2a  one as they.  **
16e0f 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b   are played back
16e10 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61  ..  */.  if( pSa
16e11 76 65 70 6f 69 6e 74 20 26 26 20 21 70 61 67 65  vepoint && !page
16e12 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20  rUseWal(pPager) 
16e13 29 7b 0a 20 20 20 20 69 48 64 72 4f 66 66 20 3d  ){.    iHdrOff =
16e14 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64   pSavepoint->iHd
16e15 72 4f 66 66 73 65 74 20 3f 20 70 53 61 76 65 70  rOffset ? pSavep
16e16 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66 73 65 74  oint->iHdrOffset
16e17 20 3a 20 73 7a 4a 3b 0a 20 20 20 20 70 50 61 67   : szJ;.    pPag
16e18 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
16e19 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 4f 66   pSavepoint->iOf
16e1a 66 73 65 74 3b 0a 20 20 20 20 77 68 69 6c 65 28  fset;.    while(
16e1b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
16e1c 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  & pPager->journa
16e1d 6c 4f 66 66 3c 69 48 64 72 4f 66 66 20 29 7b 0a  lOff<iHdrOff ){.
16e1e 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
16e1f 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61  _playback_one_pa
16e20 67 65 28 70 50 61 67 65 72 2c 20 26 70 50 61 67  ge(pPager, &pPag
16e21 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20  er->journalOff, 
16e22 70 44 6f 6e 65 2c 20 31 2c 20 31 29 3b 0a 20 20  pDone, 1, 1);.  
16e23 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
16e24 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
16e25 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
16e26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
16e27 66 66 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  ff = 0;.  }..  /
16e28 2a 20 43 6f 6e 74 69 6e 75 65 20 72 6f 6c 6c 69  * Continue rolli
16e29 6e 67 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20  ng back records 
16e2a 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  out of the main 
16e2b 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67  journal starting
16e2c 20 61 74 0a 20 20 2a 2a 20 74 68 65 20 66 69 72   at.  ** the fir
16e2d 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  st journal heade
16e2e 72 20 73 65 65 6e 20 61 6e 64 20 63 6f 6e 74 69  r seen and conti
16e2f 6e 75 69 6e 67 20 75 6e 74 69 6c 20 74 68 65 20  nuing until the 
16e30 65 66 66 65 63 74 69 76 65 20 65 6e 64 0a 20 20  effective end.  
16e31 2a 2a 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a  ** of the main j
16e32 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 43 6f  ournal file.  Co
16e33 6e 74 69 6e 75 65 20 74 6f 20 73 6b 69 70 20 6f  ntinue to skip o
16e34 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65  ut-of-range page
16e35 73 20 61 6e 64 0a 20 20 2a 2a 20 63 6f 6e 74 69  s and.  ** conti
16e36 6e 75 65 20 61 64 64 69 6e 67 20 70 61 67 65 73  nue adding pages
16e37 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 6f 20   rolled back to 
16e38 70 44 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 77 68  pDone..  */.  wh
16e39 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
16e3a 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f  OK && pPager->jo
16e3b 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 20 29 7b 0a  urnalOff<szJ ){.
16e3c 20 20 20 20 75 33 32 20 69 69 3b 20 20 20 20 20      u32 ii;     
16e3d 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
16e3e 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75 33  ounter */.    u3
16e3f 32 20 6e 4a 52 65 63 20 3d 20 30 3b 20 20 20 20  2 nJRec = 0;    
16e40 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4a 6f   /* Number of Jo
16e41 75 72 6e 61 6c 20 52 65 63 6f 72 64 73 20 2a 2f  urnal Records */
16e42 0a 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a  .    u32 dummy;.
16e43 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75      rc = readJou
16e44 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20  rnalHdr(pPager, 
16e45 30 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20  0, szJ, &nJRec, 
16e46 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73  &dummy);.    ass
16e47 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
16e48 44 4f 4e 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 0a  DONE );..    /*.
16e49 20 20 20 20 2a 2a 20 54 68 65 20 22 70 50 61 67      ** The "pPag
16e4a 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a  er->journalHdr+J
16e4b 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
16e4c 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a  ager)==pPager->j
16e4d 6f 75 72 6e 61 6c 4f 66 66 22 0a 20 20 20 20 2a  ournalOff".    *
16e4e 2a 20 74 65 73 74 20 69 73 20 72 65 6c 61 74 65  * test is relate
16e4f 64 20 74 6f 20 74 69 63 6b 65 74 20 23 32 35 36  d to ticket #256
16e50 35 2e 20 20 53 65 65 20 74 68 65 20 64 69 73 63  5.  See the disc
16e51 75 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 20 20  ussion in the.  
16e52 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 62    ** pager_playb
16e53 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ack() function f
16e54 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
16e55 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 20 20 2a  formation..    *
16e56 2f 0a 20 20 20 20 69 66 28 20 6e 4a 52 65 63 3d  /.    if( nJRec=
16e57 3d 30 20 0a 20 20 20 20 20 26 26 20 70 50 61 67  =0 .     && pPag
16e58 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a  er->journalHdr+J
16e59 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
16e5a 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a  ager)==pPager->j
16e5b 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 20 20 29 7b  ournalOff.    ){
16e5c 0a 20 20 20 20 20 20 6e 4a 52 65 63 20 3d 20 28  .      nJRec = (
16e5d 75 33 32 29 28 28 73 7a 4a 20 2d 20 70 50 61 67  u32)((szJ - pPag
16e5e 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f  er->journalOff)/
16e5f 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50  JOURNAL_PG_SZ(pP
16e60 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20  ager));.    }.  
16e61 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d    for(ii=0; rc==
16e62 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c  SQLITE_OK && ii<
16e63 6e 4a 52 65 63 20 26 26 20 70 50 61 67 65 72 2d  nJRec && pPager-
16e64 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b  >journalOff<szJ;
16e65 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63   ii++){.      rc
16e66 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63   = pager_playbac
16e67 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65  k_one_page(pPage
16e68 72 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72  r, &pPager->jour
16e69 6e 61 6c 4f 66 66 2c 20 70 44 6f 6e 65 2c 20 31  nalOff, pDone, 1
16e6a 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 1);.    }.    
16e6b 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
16e6c 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20  TE_DONE );.  }. 
16e6d 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
16e6e 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72  ITE_OK || pPager
16e6f 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 3d 73 7a  ->journalOff>=sz
16e70 4a 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c  J );..  /* Final
16e71 6c 79 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 70 61  ly,  rollback pa
16e72 67 65 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62  ges from the sub
16e73 2d 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 67 65 20  -journal.  Page 
16e74 74 68 61 74 20 77 65 72 65 0a 20 20 2a 2a 20 70  that were.  ** p
16e75 72 65 76 69 6f 75 73 6c 79 20 72 6f 6c 6c 65 64  reviously rolled
16e76 20 62 61 63 6b 20 6f 75 74 20 6f 66 20 74 68 65   back out of the
16e77 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 28 61   main journal (a
16e78 6e 64 20 61 72 65 20 68 65 6e 63 65 20 69 6e 20  nd are hence in 
16e79 70 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 6c 6c  pDone).  ** will
16e7a 20 62 65 20 73 6b 69 70 70 65 64 2e 20 20 4f 75   be skipped.  Ou
16e7b 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73  t-of-range pages
16e7c 20 61 72 65 20 61 6c 73 6f 20 73 6b 69 70 70 65   are also skippe
16e7d 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53  d..  */.  if( pS
16e7e 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20  avepoint ){.    
16e7f 75 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 20  u32 ii;         
16e80 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
16e81 65 72 20 2a 2f 0a 20 20 20 20 69 36 34 20 6f 66  er */.    i64 of
16e82 66 73 65 74 20 3d 20 28 69 36 34 29 70 53 61 76  fset = (i64)pSav
16e83 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 2a  epoint->iSubRec*
16e84 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53  (4+pPager->pageS
16e85 69 7a 65 29 3b 0a 0a 20 20 20 20 69 66 28 20 70  ize);..    if( p
16e86 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65  agerUseWal(pPage
16e87 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  r) ){.      rc =
16e88 20 73 71 6c 69 74 65 33 57 61 6c 53 61 76 65 70   sqlite3WalSavep
16e89 6f 69 6e 74 55 6e 64 6f 28 70 50 61 67 65 72 2d  ointUndo(pPager-
16e8a 3e 70 57 61 6c 2c 20 70 53 61 76 65 70 6f 69 6e  >pWal, pSavepoin
16e8b 74 2d 3e 61 57 61 6c 44 61 74 61 29 3b 0a 20 20  t->aWalData);.  
16e8c 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d 70    }.    for(ii=p
16e8d 53 61 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52  Savepoint->iSubR
16e8e 65 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  ec; rc==SQLITE_O
16e8f 4b 20 26 26 20 69 69 3c 70 50 61 67 65 72 2d 3e  K && ii<pPager->
16e90 6e 53 75 62 52 65 63 3b 20 69 69 2b 2b 29 7b 0a  nSubRec; ii++){.
16e91 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 66        assert( of
16e92 66 73 65 74 3d 3d 28 69 36 34 29 69 69 2a 28 34  fset==(i64)ii*(4
16e93 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  +pPager->pageSiz
16e94 65 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  e) );.      rc =
16e95 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f   pager_playback_
16e96 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c  one_page(pPager,
16e97 20 26 6f 66 66 73 65 74 2c 20 70 44 6f 6e 65 2c   &offset, pDone,
16e98 20 30 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20   0, 1);.    }.  
16e99 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
16e9a 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d  LITE_DONE );.  }
16e9b 0a 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65  ..  sqlite3Bitve
16e9c 63 44 65 73 74 72 6f 79 28 70 44 6f 6e 65 29 3b  cDestroy(pDone);
16e9d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
16e9e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67  E_OK ){.    pPag
16e9f 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
16ea0 20 73 7a 4a 3b 0a 20 20 7d 0a 0a 20 20 72 65 74   szJ;.  }..  ret
16ea1 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
16ea2 20 43 68 61 6e 67 65 20 74 68 65 20 6d 61 78 69   Change the maxi
16ea3 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e  mum number of in
16ea4 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74 68  -memory pages th
16ea5 61 74 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a  at are allowed..
16ea6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
16ea7 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
16ea8 67 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28  gerSetCachesize(
16ea9 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
16eaa 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 73 71  nt mxPage){.  sq
16eab 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61  lite3PcacheSetCa
16eac 63 68 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e  chesize(pPager->
16ead 70 50 43 61 63 68 65 2c 20 6d 78 50 61 67 65 29  pPCache, mxPage)
16eae 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
16eaf 61 73 20 6d 75 63 68 20 6d 65 6d 6f 72 79 20 61  as much memory a
16eb0 73 20 70 6f 73 73 69 62 6c 65 20 66 72 6f 6d 20  s possible from 
16eb1 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51  the pager..*/.SQ
16eb2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
16eb3 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 68  d sqlite3PagerSh
16eb4 72 69 6e 6b 28 50 61 67 65 72 20 2a 70 50 61 67  rink(Pager *pPag
16eb5 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63  er){.  sqlite3Pc
16eb6 61 63 68 65 53 68 72 69 6e 6b 28 70 50 61 67 65  acheShrink(pPage
16eb7 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a  r->pPCache);.}..
16eb8 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 74 68 65  /*.** Adjust the
16eb9 20 72 6f 62 75 73 74 6e 65 73 73 20 6f 66 20 74   robustness of t
16eba 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 64  he database to d
16ebb 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20  amage due to OS 
16ebc 63 72 61 73 68 65 73 0a 2a 2a 20 6f 72 20 70 6f  crashes.** or po
16ebd 77 65 72 20 66 61 69 6c 75 72 65 73 20 62 79 20  wer failures by 
16ebe 63 68 61 6e 67 69 6e 67 20 74 68 65 20 6e 75 6d  changing the num
16ebf 62 65 72 20 6f 66 20 73 79 6e 63 73 28 29 73 20  ber of syncs()s 
16ec0 77 68 65 6e 20 77 72 69 74 69 6e 67 0a 2a 2a 20  when writing.** 
16ec1 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
16ec2 72 6e 61 6c 2e 20 20 54 68 65 72 65 20 61 72 65  rnal.  There are
16ec3 20 74 68 72 65 65 20 6c 65 76 65 6c 73 3a 0a 2a   three levels:.*
16ec4 2a 0a 2a 2a 20 20 20 20 4f 46 46 20 20 20 20 20  *.**    OFF     
16ec5 20 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28    sqlite3OsSync(
16ec6 29 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65  ) is never calle
16ec7 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  d.  This is the 
16ec8 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20  default.**      
16ec9 20 20 20 20 20 20 20 20 66 6f 72 20 74 65 6d 70          for temp
16eca 6f 72 61 72 79 20 61 6e 64 20 74 72 61 6e 73 69  orary and transi
16ecb 65 6e 74 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a  ent files..**.**
16ecc 20 20 20 20 4e 4f 52 4d 41 4c 20 20 20 20 54 68      NORMAL    Th
16ecd 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e  e journal is syn
16ece 63 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 20  ced once before 
16ecf 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e 20  writes begin on 
16ed0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
16ed1 20 20 20 20 64 61 74 61 62 61 73 65 2e 20 20 54      database.  T
16ed2 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20  his is normally 
16ed3 61 64 65 71 75 61 74 65 20 70 72 6f 74 65 63 74  adequate protect
16ed4 69 6f 6e 2c 20 62 75 74 0a 2a 2a 20 20 20 20 20  ion, but.**     
16ed5 20 20 20 20 20 20 20 20 20 69 74 20 69 73 20 74           it is t
16ed6 68 65 6f 72 65 74 69 63 61 6c 6c 79 20 70 6f 73  heoretically pos
16ed7 73 69 62 6c 65 2c 20 74 68 6f 75 67 68 20 76 65  sible, though ve
16ed8 72 79 20 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a 20  ry unlikely,.** 
16ed9 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 61               tha
16eda 74 20 61 6e 20 69 6e 6f 70 65 72 74 75 6e 65 20  t an inopertune 
16edb 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f  power failure co
16edc 75 6c 64 20 6c 65 61 76 65 20 74 68 65 20 6a 6f  uld leave the jo
16edd 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20  urnal.**        
16ede 20 20 20 20 20 20 69 6e 20 61 20 73 74 61 74 65        in a state
16edf 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63 61 75   which would cau
16ee0 73 65 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65  se damage to the
16ee1 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20   database.**    
16ee2 20 20 20 20 20 20 20 20 20 20 77 68 65 6e 20 69            when i
16ee3 74 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  t is rolled back
16ee4 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c 20  ..**.**    FULL 
16ee5 20 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c       The journal
16ee6 20 69 73 20 73 79 6e 63 65 64 20 74 77 69 63 65   is synced twice
16ee7 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 20 62   before writes b
16ee8 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20  egin on the.**  
16ee9 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61              data
16eea 62 61 73 65 20 28 77 69 74 68 20 73 6f 6d 65 20  base (with some 
16eeb 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
16eec 6d 61 74 69 6f 6e 20 2d 20 74 68 65 20 6e 52 65  mation - the nRe
16eed 63 20 66 69 65 6c 64 0a 2a 2a 20 20 20 20 20 20  c field.**      
16eee 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 6a          of the j
16eef 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2d 20  ournal header - 
16ef0 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20 69 6e  being written in
16ef1 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f   between the two
16ef2 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
16ef3 20 73 79 6e 63 73 29 2e 20 20 49 66 20 77 65 20   syncs).  If we 
16ef4 61 73 73 75 6d 65 20 74 68 61 74 20 77 72 69 74  assume that writ
16ef5 69 6e 67 20 61 0a 2a 2a 20 20 20 20 20 20 20 20  ing a.**        
16ef6 20 20 20 20 20 20 73 69 6e 67 6c 65 20 64 69 73        single dis
16ef7 6b 20 73 65 63 74 6f 72 20 69 73 20 61 74 6f 6d  k sector is atom
16ef8 69 63 2c 20 74 68 65 6e 20 74 68 69 73 20 6d 6f  ic, then this mo
16ef9 64 65 20 70 72 6f 76 69 64 65 73 0a 2a 2a 20 20  de provides.**  
16efa 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 75              assu
16efb 72 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 6a  rance that the j
16efc 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20  ournal will not 
16efd 62 65 20 63 6f 72 72 75 70 74 65 64 20 74 6f 20  be corrupted to 
16efe 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
16eff 20 20 20 20 70 6f 69 6e 74 20 6f 66 20 63 61 75      point of cau
16f00 73 69 6e 67 20 64 61 6d 61 67 65 20 74 6f 20 74  sing damage to t
16f01 68 65 20 64 61 74 61 62 61 73 65 20 64 75 72 69  he database duri
16f02 6e 67 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a  ng rollback..**.
16f03 2a 2a 20 54 68 65 20 61 62 6f 76 65 20 69 73 20  ** The above is 
16f04 66 6f 72 20 61 20 72 6f 6c 6c 62 61 63 6b 2d 6a  for a rollback-j
16f05 6f 75 72 6e 61 6c 20 6d 6f 64 65 2e 20 20 46 6f  ournal mode.  Fo
16f06 72 20 57 41 4c 20 6d 6f 64 65 2c 20 4f 46 46 20  r WAL mode, OFF 
16f07 63 6f 6e 74 69 6e 75 65 73 0a 2a 2a 20 74 6f 20  continues.** to 
16f08 6d 65 61 6e 20 74 68 61 74 20 6e 6f 20 73 79 6e  mean that no syn
16f09 63 73 20 65 76 65 72 20 6f 63 63 75 72 2e 20 20  cs ever occur.  
16f0a 4e 4f 52 4d 41 4c 20 6d 65 61 6e 73 20 74 68 61  NORMAL means tha
16f0b 74 20 74 68 65 20 57 41 4c 20 69 73 20 73 79 6e  t the WAL is syn
16f0c 63 65 64 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20  ced.** prior to 
16f0d 74 68 65 20 73 74 61 72 74 20 6f 66 20 63 68 65  the start of che
16f0e 63 6b 70 6f 69 6e 74 20 61 6e 64 20 74 68 61 74  ckpoint and that
16f0f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
16f10 6c 65 20 69 73 20 73 79 6e 63 65 64 0a 2a 2a 20  le is synced.** 
16f11 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f  at the conclusio
16f12 6e 20 6f 66 20 74 68 65 20 63 68 65 63 6b 70 6f  n of the checkpo
16f13 69 6e 74 20 69 66 20 74 68 65 20 65 6e 74 69 72  int if the entir
16f14 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
16f15 20 57 41 4c 0a 2a 2a 20 77 61 73 20 77 72 69 74   WAL.** was writ
16f16 74 65 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 68  ten back into th
16f17 65 20 64 61 74 61 62 61 73 65 2e 20 20 42 75 74  e database.  But
16f18 20 6e 6f 20 73 79 6e 63 20 6f 70 65 72 61 74 69   no sync operati
16f19 6f 6e 73 20 6f 63 63 75 72 20 66 6f 72 0a 2a 2a  ons occur for.**
16f1a 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 63 6f 6d   an ordinary com
16f1b 6d 69 74 20 69 6e 20 4e 4f 52 4d 41 4c 20 6d 6f  mit in NORMAL mo
16f1c 64 65 20 77 69 74 68 20 57 41 4c 2e 20 20 46 55  de with WAL.  FU
16f1d 4c 4c 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  LL means that th
16f1e 65 20 57 41 4c 0a 2a 2a 20 66 69 6c 65 20 69 73  e WAL.** file is
16f1f 20 73 79 6e 63 65 64 20 66 6f 6c 6c 6f 77 69 6e   synced followin
16f20 67 20 65 61 63 68 20 63 6f 6d 6d 69 74 20 6f 70  g each commit op
16f21 65 72 61 74 69 6f 6e 2c 20 69 6e 20 61 64 64 69  eration, in addi
16f22 74 69 6f 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 73  tion to the.** s
16f23 79 6e 63 73 20 61 73 73 6f 63 69 61 74 65 64 20  yncs associated 
16f24 77 69 74 68 20 4e 4f 52 4d 41 4c 2e 0a 2a 2a 0a  with NORMAL..**.
16f25 2a 2a 20 44 6f 20 6e 6f 74 20 63 6f 6e 66 75 73  ** Do not confus
16f26 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 46 55  e synchronous=FU
16f27 4c 4c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 53  LL with SQLITE_S
16f28 59 4e 43 5f 46 55 4c 4c 2e 20 20 54 68 65 0a 2a  YNC_FULL.  The.*
16f29 2a 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55  * SQLITE_SYNC_FU
16f2a 4c 4c 20 6d 61 63 72 6f 20 6d 65 61 6e 73 20 74  LL macro means t
16f2b 6f 20 75 73 65 20 74 68 65 20 4d 61 63 4f 53 58  o use the MacOSX
16f2c 2d 73 74 79 6c 65 20 66 75 6c 6c 2d 66 73 79 6e  -style full-fsyn
16f2d 63 0a 2a 2a 20 75 73 69 6e 67 20 66 63 6e 74 6c  c.** using fcntl
16f2e 28 46 5f 46 55 4c 4c 46 53 59 4e 43 29 2e 20 20  (F_FULLFSYNC).  
16f2f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d  SQLITE_SYNC_NORM
16f30 41 4c 20 6d 65 61 6e 73 20 74 6f 20 64 6f 20 61  AL means to do a
16f31 6e 0a 2a 2a 20 6f 72 64 69 6e 61 72 79 20 66 73  n.** ordinary fs
16f32 79 6e 63 28 29 20 63 61 6c 6c 2e 20 20 54 68 65  ync() call.  The
16f33 72 65 20 69 73 20 6e 6f 20 64 69 66 66 65 72 65  re is no differe
16f34 6e 63 65 20 62 65 74 77 65 65 6e 20 53 51 4c 49  nce between SQLI
16f35 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 2a 2a 20  TE_SYNC_FULL.** 
16f36 61 6e 64 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  and SQLITE_SYNC_
16f37 4e 4f 52 4d 41 4c 20 6f 6e 20 70 6c 61 74 66 6f  NORMAL on platfo
16f38 72 6d 73 20 6f 74 68 65 72 20 74 68 61 6e 20 4d  rms other than M
16f39 61 63 4f 53 58 2e 20 20 42 75 74 20 74 68 65 0a  acOSX.  But the.
16f3a 2a 2a 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 46  ** synchronous=F
16f3b 55 4c 4c 20 76 65 72 73 75 73 20 73 79 6e 63 68  ULL versus synch
16f3c 72 6f 6e 6f 75 73 3d 4e 4f 52 4d 41 4c 20 73 65  ronous=NORMAL se
16f3d 74 74 69 6e 67 20 64 65 74 65 72 6d 69 6e 65 73  tting determines
16f3e 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 78 53 79   when.** the xSy
16f3f 6e 63 20 70 72 69 6d 69 74 69 76 65 20 69 73 20  nc primitive is 
16f40 63 61 6c 6c 65 64 20 61 6e 64 20 69 73 20 72 65  called and is re
16f41 6c 65 76 61 6e 74 20 74 6f 20 61 6c 6c 20 70 6c  levant to all pl
16f42 61 74 66 6f 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 4e  atforms..**.** N
16f43 75 6d 65 72 69 63 20 76 61 6c 75 65 73 20 61 73  umeric values as
16f44 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
16f45 65 73 65 20 73 74 61 74 65 73 20 61 72 65 20 4f  ese states are O
16f46 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c  FF==1, NORMAL=2,
16f47 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a  .** and FULL=3..
16f48 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
16f49 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
16f4a 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56  GMAS.SQLITE_PRIV
16f4b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
16f4c 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 65  PagerSetSafetyLe
16f4d 76 65 6c 28 0a 20 20 50 61 67 65 72 20 2a 70 50  vel(.  Pager *pP
16f4e 61 67 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20  ager,        /* 
16f4f 54 68 65 20 70 61 67 65 72 20 74 6f 20 73 65 74  The pager to set
16f50 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 66 6f   safety level fo
16f51 72 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 76 65 6c  r */.  int level
16f52 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
16f53 50 52 41 47 4d 41 20 73 79 6e 63 68 72 6f 6e 6f  PRAGMA synchrono
16f54 75 73 2e 20 20 31 3d 4f 46 46 2c 20 32 3d 4e 4f  us.  1=OFF, 2=NO
16f55 52 4d 41 4c 2c 20 33 3d 46 55 4c 4c 20 2a 2f 20  RMAL, 3=FULL */ 
16f56 20 0a 20 20 69 6e 74 20 62 46 75 6c 6c 46 73 79   .  int bFullFsy
16f57 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 50 52 41  nc,       /* PRA
16f58 47 4d 41 20 66 75 6c 6c 66 73 79 6e 63 20 2a 2f  GMA fullfsync */
16f59 0a 20 20 69 6e 74 20 62 43 6b 70 74 46 75 6c 6c  .  int bCkptFull
16f5a 46 73 79 6e 63 20 20 20 20 2f 2a 20 50 52 41 47  Fsync    /* PRAG
16f5b 4d 41 20 63 68 65 63 6b 70 6f 69 6e 74 5f 66 75  MA checkpoint_fu
16f5c 6c 6c 66 73 79 6e 63 20 2a 2f 0a 29 7b 0a 20 20  llfsync */.){.  
16f5d 61 73 73 65 72 74 28 20 6c 65 76 65 6c 3e 3d 31  assert( level>=1
16f5e 20 26 26 20 6c 65 76 65 6c 3c 3d 33 20 29 3b 0a   && level<=3 );.
16f5f 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63    pPager->noSync
16f60 20 3d 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c 7c   =  (level==1 ||
16f61 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
16f62 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65  e) ?1:0;.  pPage
16f63 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 6c  r->fullSync = (l
16f64 65 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 67  evel==3 && !pPag
16f65 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31  er->tempFile) ?1
16f66 3a 30 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72  :0;.  if( pPager
16f67 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20  ->noSync ){.    
16f68 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67  pPager->syncFlag
16f69 73 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65  s = 0;.    pPage
16f6a 72 2d 3e 63 6b 70 74 53 79 6e 63 46 6c 61 67 73  r->ckptSyncFlags
16f6b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
16f6c 28 20 62 46 75 6c 6c 46 73 79 6e 63 20 29 7b 0a  ( bFullFsync ){.
16f6d 20 20 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63      pPager->sync
16f6e 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 53  Flags = SQLITE_S
16f6f 59 4e 43 5f 46 55 4c 4c 3b 0a 20 20 20 20 70 50  YNC_FULL;.    pP
16f70 61 67 65 72 2d 3e 63 6b 70 74 53 79 6e 63 46 6c  ager->ckptSyncFl
16f71 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e  ags = SQLITE_SYN
16f72 43 5f 46 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 20  C_FULL;.  }else 
16f73 69 66 28 20 62 43 6b 70 74 46 75 6c 6c 46 73 79  if( bCkptFullFsy
16f74 6e 63 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  nc ){.    pPager
16f75 2d 3e 73 79 6e 63 46 6c 61 67 73 20 3d 20 53 51  ->syncFlags = SQ
16f76 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
16f77 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 6b  ;.    pPager->ck
16f78 70 74 53 79 6e 63 46 6c 61 67 73 20 3d 20 53 51  ptSyncFlags = SQ
16f79 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3b 0a  LITE_SYNC_FULL;.
16f7a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61    }else{.    pPa
16f7b 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 20 3d  ger->syncFlags =
16f7c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52   SQLITE_SYNC_NOR
16f7d 4d 41 4c 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  MAL;.    pPager-
16f7e 3e 63 6b 70 74 53 79 6e 63 46 6c 61 67 73 20 3d  >ckptSyncFlags =
16f7f 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52   SQLITE_SYNC_NOR
16f80 4d 41 4c 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  MAL;.  }.  pPage
16f81 72 2d 3e 77 61 6c 53 79 6e 63 46 6c 61 67 73 20  r->walSyncFlags 
16f82 3d 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c  = pPager->syncFl
16f83 61 67 73 3b 0a 20 20 69 66 28 20 70 50 61 67 65  ags;.  if( pPage
16f84 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b 0a 20  r->fullSync ){. 
16f85 20 20 20 70 50 61 67 65 72 2d 3e 77 61 6c 53 79     pPager->walSy
16f86 6e 63 46 6c 61 67 73 20 7c 3d 20 57 41 4c 5f 53  ncFlags |= WAL_S
16f87 59 4e 43 5f 54 52 41 4e 53 41 43 54 49 4f 4e 53  YNC_TRANSACTIONS
16f88 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ;.  }.}.#endif..
16f89 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
16f8a 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ing global varia
16f8b 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  ble is increment
16f8c 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 20  ed whenever the 
16f8d 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 74 65 6d  library.** attem
16f8e 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 20 74 65  pts to open a te
16f8f 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 20 54  mporary file.  T
16f90 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
16f91 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74  is used for.** t
16f92 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79  esting and analy
16f93 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f 0a 23  sis only.  .*/.#
16f94 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
16f95 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
16f96 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d   sqlite3_opentem
16f97 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e  p_count = 0;.#en
16f98 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  dif../*.** Open 
16f99 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
16f9a 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  ..**.** Write th
16f9b 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
16f9c 72 20 69 6e 74 6f 20 2a 70 46 69 6c 65 2e 20 52  r into *pFile. R
16f9d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
16f9e 6f 6e 20 73 75 63 63 65 73 73 20 0a 2a 2a 20 6f  on success .** o
16f9f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72  r some other err
16fa0 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 66 61  or code if we fa
16fa1 69 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c 6c 20  il. The OS will 
16fa2 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a  automatically .*
16fa3 2a 20 64 65 6c 65 74 65 20 74 68 65 20 74 65 6d  * delete the tem
16fa4 70 6f 72 61 72 79 20 66 69 6c 65 20 77 68 65 6e  porary file when
16fa5 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a   it is closed..*
16fa6 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 70  *.** The flags p
16fa7 61 73 73 65 64 20 74 6f 20 74 68 65 20 56 46 53  assed to the VFS
16fa8 20 6c 61 79 65 72 20 78 4f 70 65 6e 28 29 20 63   layer xOpen() c
16fa9 61 6c 6c 20 61 72 65 20 74 68 6f 73 65 20 73 70  all are those sp
16faa 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 70 61  ecified.** by pa
16fab 72 61 6d 65 74 65 72 20 76 66 73 46 6c 61 67 73  rameter vfsFlags
16fac 20 4f 52 65 64 20 77 69 74 68 20 74 68 65 20 66   ORed with the f
16fad 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
16fae 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
16faf 52 45 41 44 57 52 49 54 45 0a 2a 2a 20 20 20 20  READWRITE.**    
16fb0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
16fb1 41 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54  ATE.**     SQLIT
16fb2 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
16fb3 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f  .**     SQLITE_O
16fb4 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
16fb5 45 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  E.*/.static int 
16fb6 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 0a 20  pagerOpentemp(. 
16fb7 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20   Pager *pPager, 
16fb8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
16fb9 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  ger object */.  
16fba 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46  sqlite3_file *pF
16fbb 69 6c 65 2c 20 20 2f 2a 20 57 72 69 74 65 20 74  ile,  /* Write t
16fbc 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
16fbd 6f 72 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  or here */.  int
16fbe 20 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 20   vfsFlags       
16fbf 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73     /* Flags pass
16fc0 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ed through to th
16fc1 65 20 56 46 53 20 2a 2f 0a 29 7b 0a 20 20 69 6e  e VFS */.){.  in
16fc2 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
16fc3 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
16fc4 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  de */..#ifdef SQ
16fc5 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69  LITE_TEST.  sqli
16fc6 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f 75  te3_opentemp_cou
16fc7 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73 65 64 20 66  nt++;  /* Used f
16fc8 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61  or testing and a
16fc9 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 20 2a 2f 0a  nalysis only */.
16fca 23 65 6e 64 69 66 0a 0a 20 20 76 66 73 46 6c 61  #endif..  vfsFla
16fcb 67 73 20 7c 3d 20 20 53 51 4c 49 54 45 5f 4f 50  gs |=  SQLITE_OP
16fcc 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53  EN_READWRITE | S
16fcd 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
16fce 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20  E |.            
16fcf 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c  SQLITE_OPEN_EXCL
16fd0 55 53 49 56 45 20 7c 20 53 51 4c 49 54 45 5f 4f  USIVE | SQLITE_O
16fd1 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
16fd2 45 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  E;.  rc = sqlite
16fd3 33 4f 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  3OsOpen(pPager->
16fd4 70 56 66 73 2c 20 30 2c 20 70 46 69 6c 65 2c 20  pVfs, 0, pFile, 
16fd5 76 66 73 46 6c 61 67 73 2c 20 30 29 3b 0a 20 20  vfsFlags, 0);.  
16fd6 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
16fd7 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28  TE_OK || isOpen(
16fd8 70 46 69 6c 65 29 20 29 3b 0a 20 20 72 65 74 75  pFile) );.  retu
16fd9 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
16fda 53 65 74 20 74 68 65 20 62 75 73 79 20 68 61 6e  Set the busy han
16fdb 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  dler function..*
16fdc 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 69  *.** The pager i
16fdd 6e 76 6f 6b 65 73 20 74 68 65 20 62 75 73 79 2d  nvokes the busy-
16fde 68 61 6e 64 6c 65 72 20 69 66 20 73 71 6c 69 74  handler if sqlit
16fdf 65 33 4f 73 4c 6f 63 6b 28 29 20 72 65 74 75 72  e3OsLock() retur
16fe0 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55  ns .** SQLITE_BU
16fe1 53 59 20 77 68 65 6e 20 74 72 79 69 6e 67 20 74  SY when trying t
16fe2 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d 20 6e  o upgrade from n
16fe3 6f 2d 6c 6f 63 6b 20 74 6f 20 61 20 53 48 41 52  o-lock to a SHAR
16fe4 45 44 20 6c 6f 63 6b 2c 0a 2a 2a 20 6f 72 20 77  ED lock,.** or w
16fe5 68 65 6e 20 74 72 79 69 6e 67 20 74 6f 20 75 70  hen trying to up
16fe6 67 72 61 64 65 20 66 72 6f 6d 20 61 20 52 45 53  grade from a RES
16fe7 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20 61 6e  ERVED lock to an
16fe8 20 45 58 43 4c 55 53 49 56 45 20 0a 2a 2a 20 6c   EXCLUSIVE .** l
16fe9 6f 63 6b 2e 20 49 74 20 64 6f 65 73 20 2a 6e 6f  ock. It does *no
16fea 74 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75  t* invoke the bu
16feb 73 79 20 68 61 6e 64 6c 65 72 20 77 68 65 6e 20  sy handler when 
16fec 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d 0a 2a  upgrading from.*
16fed 2a 20 53 48 41 52 45 44 20 74 6f 20 52 45 53 45  * SHARED to RESE
16fee 52 56 45 44 2c 20 6f 72 20 77 68 65 6e 20 75 70  RVED, or when up
16fef 67 72 61 64 69 6e 67 20 66 72 6f 6d 20 53 48 41  grading from SHA
16ff0 52 45 44 20 74 6f 20 45 58 43 4c 55 53 49 56 45  RED to EXCLUSIVE
16ff1 0a 2a 2a 20 28 77 68 69 63 68 20 6f 63 63 75 72  .** (which occur
16ff2 73 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75  s during hot-jou
16ff3 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 20  rnal rollback). 
16ff4 53 75 6d 6d 61 72 79 3a 0a 2a 2a 0a 2a 2a 20 20  Summary:.**.**  
16ff5 20 54 72 61 6e 73 69 74 69 6f 6e 20 20 20 20 20   Transition     
16ff6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ff7 20 20 20 7c 20 49 6e 76 6f 6b 65 73 20 78 42 75     | Invokes xBu
16ff8 73 79 48 61 6e 64 6c 65 72 0a 2a 2a 20 20 20 2d  syHandler.**   -
16ff9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
16ffa 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
16ffb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
16ffc 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 4e 4f 5f  -------.**   NO_
16ffd 4c 4f 43 4b 20 20 20 20 20 20 20 2d 3e 20 53 48  LOCK       -> SH
16ffe 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 20 7c  ARED_LOCK      |
16fff 20 59 65 73 0a 2a 2a 20 20 20 53 48 41 52 45 44   Yes.**   SHARED
17000 5f 4c 4f 43 4b 20 20 20 2d 3e 20 52 45 53 45 52  _LOCK   -> RESER
17001 56 45 44 5f 4c 4f 43 4b 20 20 20 20 7c 20 4e 6f  VED_LOCK    | No
17002 0a 2a 2a 20 20 20 53 48 41 52 45 44 5f 4c 4f 43  .**   SHARED_LOC
17003 4b 20 20 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  K   -> EXCLUSIVE
17004 5f 4c 4f 43 4b 20 20 20 7c 20 4e 6f 0a 2a 2a 20  _LOCK   | No.** 
17005 20 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20    RESERVED_LOCK 
17006 2d 3e 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  -> EXCLUSIVE_LOC
17007 4b 20 20 20 7c 20 59 65 73 0a 2a 2a 0a 2a 2a 20  K   | Yes.**.** 
17008 49 66 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64  If the busy-hand
17009 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  ler callback ret
1700a 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  urns non-zero, t
1700b 68 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20 72  he lock is .** r
1700c 65 74 72 69 65 64 2e 20 49 66 20 69 74 20 72 65  etried. If it re
1700d 74 75 72 6e 73 20 7a 65 72 6f 2c 20 74 68 65 6e  turns zero, then
1700e 20 74 68 65 20 53 51 4c 49 54 45 5f 42 55 53 59   the SQLITE_BUSY
1700f 20 65 72 72 6f 72 20 69 73 0a 2a 2a 20 72 65 74   error is.** ret
17010 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c  urned to the cal
17011 6c 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 72  ler of the pager
17012 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a   API function..*
17013 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
17014 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
17015 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72  erSetBusyhandler
17016 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  (.  Pager *pPage
17017 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
17018 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
17019 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e  r object */.  in
1701a 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 72  t (*xBusyHandler
1701b 29 28 76 6f 69 64 20 2a 29 2c 20 20 20 20 20 20  )(void *),      
1701c 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1701d 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 66 75   busy-handler fu
1701e 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64  nction */.  void
1701f 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72   *pBusyHandlerAr
17020 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g               
17021 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20   /* Argument to 
17022 70 61 73 73 20 74 6f 20 78 42 75 73 79 48 61 6e  pass to xBusyHan
17023 64 6c 65 72 20 2a 2f 0a 29 7b 0a 20 20 70 50 61  dler */.){.  pPa
17024 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65  ger->xBusyHandle
17025 72 20 3d 20 78 42 75 73 79 48 61 6e 64 6c 65 72  r = xBusyHandler
17026 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 42 75 73  ;.  pPager->pBus
17027 79 48 61 6e 64 6c 65 72 41 72 67 20 3d 20 70 42  yHandlerArg = pB
17028 75 73 79 48 61 6e 64 6c 65 72 41 72 67 3b 0a 0a  usyHandlerArg;..
17029 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61    if( isOpen(pPa
1702a 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20  ger->fd) ){.    
1702b 76 6f 69 64 20 2a 2a 61 70 20 3d 20 28 76 6f 69  void **ap = (voi
1702c 64 20 2a 2a 29 26 70 50 61 67 65 72 2d 3e 78 42  d **)&pPager->xB
1702d 75 73 79 48 61 6e 64 6c 65 72 3b 0a 20 20 20 20  usyHandler;.    
1702e 61 73 73 65 72 74 28 20 28 28 69 6e 74 28 2a 29  assert( ((int(*)
1702f 28 76 6f 69 64 20 2a 29 29 28 61 70 5b 30 5d 29  (void *))(ap[0])
17030 29 3d 3d 78 42 75 73 79 48 61 6e 64 6c 65 72 20  )==xBusyHandler 
17031 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 61  );.    assert( a
17032 70 5b 31 5d 3d 3d 70 42 75 73 79 48 61 6e 64 6c  p[1]==pBusyHandl
17033 65 72 41 72 67 20 29 3b 0a 20 20 20 20 73 71 6c  erArg );.    sql
17034 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f  ite3OsFileContro
17035 6c 48 69 6e 74 28 70 50 61 67 65 72 2d 3e 66 64  lHint(pPager->fd
17036 2c 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 42  , SQLITE_FCNTL_B
17037 55 53 59 48 41 4e 44 4c 45 52 2c 20 28 76 6f 69  USYHANDLER, (voi
17038 64 20 2a 29 61 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  d *)ap);.  }.}..
17039 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
1703a 20 70 61 67 65 20 73 69 7a 65 20 75 73 65 64 20   page size used 
1703b 62 79 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a  by the Pager obj
1703c 65 63 74 2e 20 54 68 65 20 6e 65 77 20 70 61 67  ect. The new pag
1703d 65 20 73 69 7a 65 20 0a 2a 2a 20 69 73 20 70 61  e size .** is pa
1703e 73 73 65 64 20 69 6e 20 2a 70 50 61 67 65 53 69  ssed in *pPageSi
1703f 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ze..**.** If the
17040 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65   pager is in the
17041 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 68 65   error state whe
17042 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
17043 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 0a 2a 2a  is called, it.**
17044 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 54 68 65   is a no-op. The
17045 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
17046 69 73 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  is the error sta
17047 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 28 69  te error code (i
17048 2e 65 2e 20 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53  .e. .** one of S
17049 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 61 6e 20  QLITE_IOERR, an 
1704a 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 78 78 78  SQLITE_IOERR_xxx
1704b 20 73 75 62 2d 63 6f 64 65 20 6f 72 20 53 51 4c   sub-code or SQL
1704c 49 54 45 5f 46 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a  ITE_FULL)..**.**
1704d 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61   Otherwise, if a
1704e 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ll of the follow
1704f 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 2a 2a  ing are true:.**
17050 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6e 65 77 20  .**   * the new 
17051 70 61 67 65 20 73 69 7a 65 20 28 76 61 6c 75 65  page size (value
17052 20 6f 66 20 2a 70 50 61 67 65 53 69 7a 65 29 20   of *pPageSize) 
17053 69 73 20 76 61 6c 69 64 20 28 61 20 70 6f 77 65  is valid (a powe
17054 72 20 0a 2a 2a 20 20 20 20 20 6f 66 20 74 77 6f  r .**     of two
17055 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64   between 512 and
17056 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45   SQLITE_MAX_PAGE
17057 5f 53 49 5a 45 2c 20 69 6e 63 6c 75 73 69 76 65  _SIZE, inclusive
17058 29 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a  ), and.**.**   *
17059 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75   there are no ou
1705a 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72  tstanding page r
1705b 65 66 65 72 65 6e 63 65 73 2c 20 61 6e 64 0a 2a  eferences, and.*
1705c 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74  *.**   * the dat
1705d 61 62 61 73 65 20 69 73 20 65 69 74 68 65 72 20  abase is either 
1705e 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  not an in-memory
1705f 20 64 61 74 61 62 61 73 65 20 6f 72 20 69 74 20   database or it 
17060 69 73 0a 2a 2a 20 20 20 20 20 61 6e 20 69 6e 2d  is.**     an in-
17061 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20  memory database 
17062 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 63  that currently c
17063 6f 6e 73 69 73 74 73 20 6f 66 20 7a 65 72 6f 20  onsists of zero 
17064 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 74 68 65  pages..**.** the
17065 6e 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65  n the pager obje
17066 63 74 20 70 61 67 65 20 73 69 7a 65 20 69 73 20  ct page size is 
17067 73 65 74 20 74 6f 20 2a 70 50 61 67 65 53 69 7a  set to *pPageSiz
17068 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
17069 70 61 67 65 20 73 69 7a 65 20 69 73 20 63 68 61  page size is cha
1706a 6e 67 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20  nged, then this 
1706b 66 75 6e 63 74 69 6f 6e 20 75 73 65 73 20 73 71  function uses sq
1706c 6c 69 74 65 33 50 61 67 65 72 4d 61 6c 6c 6f 63  lite3PagerMalloc
1706d 28 29 20 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e  () .** to obtain
1706e 20 61 20 6e 65 77 20 50 61 67 65 72 2e 70 54 6d   a new Pager.pTm
1706f 70 53 70 61 63 65 20 62 75 66 66 65 72 2e 20 49  pSpace buffer. I
17070 66 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f  f this allocatio
17071 6e 20 61 74 74 65 6d 70 74 20 0a 2a 2a 20 66 61  n attempt .** fa
17072 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  ils, SQLITE_NOME
17073 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  M is returned an
17074 64 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20  d the page size 
17075 72 65 6d 61 69 6e 73 20 75 6e 63 68 61 6e 67 65  remains unchange
17076 64 2e 20 0a 2a 2a 20 49 6e 20 61 6c 6c 20 6f 74  d. .** In all ot
17077 68 65 72 20 63 61 73 65 73 2c 20 53 51 4c 49 54  her cases, SQLIT
17078 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
17079 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  ..**.** If the p
1707a 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 20  age size is not 
1707b 63 68 61 6e 67 65 64 2c 20 65 69 74 68 65 72 20  changed, either 
1707c 62 65 63 61 75 73 65 20 6f 6e 65 20 6f 66 20 74  because one of t
1707d 68 65 20 65 6e 75 6d 65 72 61 74 65 64 0a 2a 2a  he enumerated.**
1707e 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76   conditions abov
1707f 65 20 69 73 20 6e 6f 74 20 74 72 75 65 2c 20 74  e is not true, t
17080 68 65 20 70 61 67 65 72 20 77 61 73 20 69 6e 20  he pager was in 
17081 65 72 72 6f 72 20 73 74 61 74 65 20 77 68 65 6e  error state when
17082 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f   this.** functio
17083 6e 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 6f 72  n was called, or
17084 20 62 65 63 61 75 73 65 20 74 68 65 20 6d 65 6d   because the mem
17085 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61  ory allocation a
17086 74 74 65 6d 70 74 20 66 61 69 6c 65 64 2c 20 0a  ttempt failed, .
17087 2a 2a 20 74 68 65 6e 20 2a 70 50 61 67 65 53 69  ** then *pPageSi
17088 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ze is set to the
17089 20 6f 6c 64 2c 20 72 65 74 61 69 6e 65 64 20 70   old, retained p
1708a 61 67 65 20 73 69 7a 65 20 62 65 66 6f 72 65 20  age size before 
1708b 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51  returning..*/.SQ
1708c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1708d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
1708e 50 61 67 65 73 69 7a 65 28 50 61 67 65 72 20 2a  Pagesize(Pager *
1708f 70 50 61 67 65 72 2c 20 75 33 32 20 2a 70 50 61  pPager, u32 *pPa
17090 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73  geSize, int nRes
17091 65 72 76 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  erve){.  int rc 
17092 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
17093 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73  /* It is not pos
17094 73 69 62 6c 65 20 74 6f 20 64 6f 20 61 20 66 75  sible to do a fu
17095 6c 6c 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f  ll assert_pager_
17096 73 74 61 74 65 28 29 20 68 65 72 65 2c 20 61 73  state() here, as
17097 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74   this.  ** funct
17098 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65  ion may be calle
17099 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 50 61  d from within Pa
1709a 67 65 72 4f 70 65 6e 28 29 2c 20 62 65 66 6f 72  gerOpen(), befor
1709b 65 20 74 68 65 20 73 74 61 74 65 0a 20 20 2a 2a  e the state.  **
1709c 20 6f 66 20 74 68 65 20 50 61 67 65 72 20 6f 62   of the Pager ob
1709d 6a 65 63 74 20 69 73 20 69 6e 74 65 72 6e 61 6c  ject is internal
1709e 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a 20  ly consistent.. 
1709f 20 2a 2a 0a 20 20 2a 2a 20 41 74 20 6f 6e 65 20   **.  ** At one 
170a0 70 6f 69 6e 74 20 74 68 69 73 20 66 75 6e 63 74  point this funct
170a1 69 6f 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20  ion returned an 
170a2 65 72 72 6f 72 20 69 66 20 74 68 65 20 70 61 67  error if the pag
170a3 65 72 20 77 61 73 20 69 6e 20 0a 20 20 2a 2a 20  er was in .  ** 
170a4 50 41 47 45 52 5f 45 52 52 4f 52 20 73 74 61 74  PAGER_ERROR stat
170a5 65 2e 20 42 75 74 20 73 69 6e 63 65 20 50 41 47  e. But since PAG
170a6 45 52 5f 45 52 52 4f 52 20 73 74 61 74 65 20 67  ER_ERROR state g
170a7 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 20  uarantees that. 
170a8 20 2a 2a 20 74 68 65 72 65 20 69 73 20 61 74 20   ** there is at 
170a9 6c 65 61 73 74 20 6f 6e 65 20 6f 75 74 73 74 61  least one outsta
170aa 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72  nding page refer
170ab 65 6e 63 65 2c 20 74 68 69 73 20 66 75 6e 63 74  ence, this funct
170ac 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 61 20 6e 6f  ion.  ** is a no
170ad 2d 6f 70 20 66 6f 72 20 74 68 61 74 20 63 61 73  -op for that cas
170ae 65 20 61 6e 79 68 6f 77 2e 0a 20 20 2a 2f 0a 0a  e anyhow..  */..
170af 20 20 75 33 32 20 70 61 67 65 53 69 7a 65 20 3d    u32 pageSize =
170b0 20 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20 20 61   *pPageSize;.  a
170b1 73 73 65 72 74 28 20 70 61 67 65 53 69 7a 65 3d  ssert( pageSize=
170b2 3d 30 20 7c 7c 20 28 70 61 67 65 53 69 7a 65 3e  =0 || (pageSize>
170b3 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a 65  =512 && pageSize
170b4 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  <=SQLITE_MAX_PAG
170b5 45 5f 53 49 5a 45 29 20 29 3b 0a 20 20 69 66 28  E_SIZE) );.  if(
170b6 20 28 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d   (pPager->memDb=
170b7 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62  =0 || pPager->db
170b8 53 69 7a 65 3d 3d 30 29 0a 20 20 20 26 26 20 73  Size==0).   && s
170b9 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43  qlite3PcacheRefC
170ba 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43  ount(pPager->pPC
170bb 61 63 68 65 29 3d 3d 30 20 0a 20 20 20 26 26 20  ache)==0 .   && 
170bc 70 61 67 65 53 69 7a 65 20 26 26 20 70 61 67 65  pageSize && page
170bd 53 69 7a 65 21 3d 28 75 33 32 29 70 50 61 67 65  Size!=(u32)pPage
170be 72 2d 3e 70 61 67 65 53 69 7a 65 20 0a 20 20 29  r->pageSize .  )
170bf 7b 0a 20 20 20 20 63 68 61 72 20 2a 70 4e 65 77  {.    char *pNew
170c0 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20   = NULL;        
170c1 20 20 20 20 20 2f 2a 20 4e 65 77 20 74 65 6d 70       /* New temp
170c2 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 69 36   space */.    i6
170c3 34 20 6e 42 79 74 65 20 3d 20 30 3b 0a 0a 20 20  4 nByte = 0;..  
170c4 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53    if( pPager->eS
170c5 74 61 74 65 3e 50 41 47 45 52 5f 4f 50 45 4e 20  tate>PAGER_OPEN 
170c6 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  && isOpen(pPager
170c7 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 20 20 72  ->fd) ){.      r
170c8 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
170c9 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64  eSize(pPager->fd
170ca 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 7d  , &nByte);.    }
170cb 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
170cc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
170cd 70 4e 65 77 20 3d 20 28 63 68 61 72 20 2a 29 73  pNew = (char *)s
170ce 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63  qlite3PageMalloc
170cf 28 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20  (pageSize);.    
170d0 20 20 69 66 28 20 21 70 4e 65 77 20 29 20 72 63    if( !pNew ) rc
170d1 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
170d2 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
170d3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
170d4 0a 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73  .      pager_res
170d5 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  et(pPager);.    
170d6 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
170d7 20 3d 20 28 50 67 6e 6f 29 28 28 6e 42 79 74 65   = (Pgno)((nByte
170d8 2b 70 61 67 65 53 69 7a 65 2d 31 29 2f 70 61 67  +pageSize-1)/pag
170d9 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 70 50  eSize);.      pP
170da 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3d  ager->pageSize =
170db 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20   pageSize;.     
170dc 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65   sqlite3PageFree
170dd 28 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61  (pPager->pTmpSpa
170de 63 65 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65  ce);.      pPage
170df 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 70  r->pTmpSpace = p
170e0 4e 65 77 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  New;.      sqlit
170e1 65 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53  e3PcacheSetPageS
170e2 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61  ize(pPager->pPCa
170e3 63 68 65 2c 20 70 61 67 65 53 69 7a 65 29 3b 0a  che, pageSize);.
170e4 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 50      }.  }..  *pP
170e5 61 67 65 53 69 7a 65 20 3d 20 70 50 61 67 65 72  ageSize = pPager
170e6 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 69 66  ->pageSize;.  if
170e7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
170e8 29 7b 0a 20 20 20 20 69 66 28 20 6e 52 65 73 65  ){.    if( nRese
170e9 72 76 65 3c 30 20 29 20 6e 52 65 73 65 72 76 65  rve<0 ) nReserve
170ea 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 73 65   = pPager->nRese
170eb 72 76 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  rve;.    assert(
170ec 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26 26 20   nReserve>=0 && 
170ed 6e 52 65 73 65 72 76 65 3c 31 30 30 30 20 29 3b  nReserve<1000 );
170ee 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65  .    pPager->nRe
170ef 73 65 72 76 65 20 3d 20 28 69 31 36 29 6e 52 65  serve = (i16)nRe
170f0 73 65 72 76 65 3b 0a 20 20 20 20 70 61 67 65 72  serve;.    pager
170f1 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67 65  ReportSize(pPage
170f2 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  r);.  }.  return
170f3 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
170f4 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
170f5 6f 20 74 68 65 20 22 74 65 6d 70 6f 72 61 72 79  o the "temporary
170f6 20 70 61 67 65 22 20 62 75 66 66 65 72 20 68 65   page" buffer he
170f7 6c 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a  ld internally.**
170f8 20 62 79 20 74 68 65 20 70 61 67 65 72 2e 20 20   by the pager.  
170f9 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72  This is a buffer
170fa 20 74 68 61 74 20 69 73 20 62 69 67 20 65 6e 6f   that is big eno
170fb 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a  ugh to hold the.
170fc 2a 2a 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e  ** entire conten
170fd 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20  t of a database 
170fe 70 61 67 65 2e 20 20 54 68 69 73 20 62 75 66 66  page.  This buff
170ff 65 72 20 69 73 20 75 73 65 64 20 69 6e 74 65 72  er is used inter
17100 6e 61 6c 6c 79 0a 2a 2a 20 64 75 72 69 6e 67 20  nally.** during 
17101 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 77 69 6c  rollback and wil
17102 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e  l be overwritten
17103 20 77 68 65 6e 65 76 65 72 20 61 20 72 6f 6c 6c   whenever a roll
17104 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20  back.** occurs. 
17105 20 42 75 74 20 6f 74 68 65 72 20 6d 6f 64 75 6c   But other modul
17106 65 73 20 61 72 65 20 66 72 65 65 20 74 6f 20 75  es are free to u
17107 73 65 20 69 74 20 74 6f 6f 2c 20 61 73 20 6c 6f  se it too, as lo
17108 6e 67 20 61 73 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c  ng as.** no roll
17109 62 61 63 6b 73 20 61 72 65 20 68 61 70 70 65 6e  backs are happen
1710a 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
1710b 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
1710c 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 61  ite3PagerTempSpa
1710d 63 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ce(Pager *pPager
1710e 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  ){.  return pPag
1710f 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 7d  er->pTmpSpace;.}
17110 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20  ../*.** Attempt 
17111 74 6f 20 73 65 74 20 74 68 65 20 6d 61 78 69 6d  to set the maxim
17112 75 6d 20 64 61 74 61 62 61 73 65 20 70 61 67 65  um database page
17113 20 63 6f 75 6e 74 20 69 66 20 6d 78 50 61 67 65   count if mxPage
17114 20 69 73 20 70 6f 73 69 74 69 76 65 2e 20 0a 2a   is positive. .*
17115 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65  * Make no change
17116 73 20 69 66 20 6d 78 50 61 67 65 20 69 73 20 7a  s if mxPage is z
17117 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 2e  ero or negative.
17118 20 20 41 6e 64 20 6e 65 76 65 72 20 72 65 64 75    And never redu
17119 63 65 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75  ce the.** maximu
1711a 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 62 65 6c  m page count bel
1711b 6f 77 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  ow the current s
1711c 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ize of the datab
1711d 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 61 72  ase..**.** Regar
1711e 64 6c 65 73 73 20 6f 66 20 6d 78 50 61 67 65 2c  dless of mxPage,
1711f 20 72 65 74 75 72 6e 20 74 68 65 20 63 75 72 72   return the curr
17120 65 6e 74 20 6d 61 78 69 6d 75 6d 20 70 61 67 65  ent maximum page
17121 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54   count..*/.SQLIT
17122 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
17123 6c 69 74 65 33 50 61 67 65 72 4d 61 78 50 61 67  lite3PagerMaxPag
17124 65 43 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50  eCount(Pager *pP
17125 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65  ager, int mxPage
17126 29 7b 0a 20 20 69 66 28 20 6d 78 50 61 67 65 3e  ){.  if( mxPage>
17127 30 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  0 ){.    pPager-
17128 3e 6d 78 50 67 6e 6f 20 3d 20 6d 78 50 61 67 65  >mxPgno = mxPage
17129 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1712a 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21 3d  pPager->eState!=
1712b 50 41 47 45 52 5f 4f 50 45 4e 20 29 3b 20 20 20  PAGER_OPEN );   
1712c 20 20 20 2f 2a 20 43 61 6c 6c 65 64 20 6f 6e 6c     /* Called onl
1712d 79 20 62 79 20 4f 50 5f 4d 61 78 50 67 63 6e 74  y by OP_MaxPgcnt
1712e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
1712f 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3e 3d 70 50  ager->mxPgno>=pP
17130 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 3b 20  ager->dbSize ); 
17131 20 2f 2a 20 4f 50 5f 4d 61 78 50 67 63 6e 74 20   /* OP_MaxPgcnt 
17132 65 6e 66 6f 72 63 65 73 20 74 68 69 73 20 2a 2f  enforces this */
17133 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72  .  return pPager
17134 2d 3e 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a  ->mxPgno;.}../*.
17135 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
17136 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
17137 20 61 72 65 20 75 73 65 64 20 74 6f 20 64 69 73   are used to dis
17138 61 62 6c 65 20 74 68 65 20 73 69 6d 75 6c 61 74  able the simulat
17139 65 64 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20  ed.** I/O error 
1713a 6d 65 63 68 61 6e 69 73 6d 2e 20 20 54 68 65 73  mechanism.  Thes
1713b 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75  e routines are u
1713c 73 65 64 20 74 6f 20 61 76 6f 69 64 20 73 69 6d  sed to avoid sim
1713d 75 6c 61 74 65 64 0a 2a 2a 20 65 72 72 6f 72 73  ulated.** errors
1713e 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65   in places where
1713f 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20   we do not care 
17140 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 0a 2a 2a  about errors..**
17141 0a 2a 2a 20 55 6e 6c 65 73 73 20 2d 44 53 51 4c  .** Unless -DSQL
17142 49 54 45 5f 54 45 53 54 3d 31 20 69 73 20 75 73  ITE_TEST=1 is us
17143 65 64 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e  ed, these routin
17144 65 73 20 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70  es are all no-op
17145 73 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74  s.** and generat
17146 65 20 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69  e no code..*/.#i
17147 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
17148 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65  .SQLITE_API exte
17149 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  rn int sqlite3_i
1714a 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b  o_error_pending;
1714b 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65  .SQLITE_API exte
1714c 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  rn int sqlite3_i
1714d 6f 5f 65 72 72 6f 72 5f 68 69 74 3b 0a 73 74 61  o_error_hit;.sta
1714e 74 69 63 20 69 6e 74 20 73 61 76 65 64 5f 63 6e  tic int saved_cn
1714f 74 3b 0a 76 6f 69 64 20 64 69 73 61 62 6c 65 5f  t;.void disable_
17150 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
17151 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 61 76  ors(void){.  sav
17152 65 64 5f 63 6e 74 20 3d 20 73 71 6c 69 74 65 33  ed_cnt = sqlite3
17153 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e  _io_error_pendin
17154 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f  g;.  sqlite3_io_
17155 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20  error_pending = 
17156 2d 31 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61 62 6c  -1;.}.void enabl
17157 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
17158 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73  rrors(void){.  s
17159 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
1715a 70 65 6e 64 69 6e 67 20 3d 20 73 61 76 65 64 5f  pending = saved_
1715b 63 6e 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64  cnt;.}.#else.# d
1715c 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69  efine disable_si
1715d 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
1715e 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61  s().# define ena
1715f 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
17160 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66  _errors().#endif
17161 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65  ../*.** Read the
17162 20 66 69 72 73 74 20 4e 20 62 79 74 65 73 20 66   first N bytes f
17163 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
17164 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e  g of the file in
17165 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74 68 61  to memory.** tha
17166 74 20 70 44 65 73 74 20 70 6f 69 6e 74 73 20 74  t pDest points t
17167 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  o. .**.** If the
17168 20 70 61 67 65 72 20 77 61 73 20 6f 70 65 6e 65   pager was opene
17169 64 20 6f 6e 20 61 20 74 72 61 6e 73 69 65 6e 74  d on a transient
1716a 20 66 69 6c 65 20 28 7a 46 69 6c 65 6e 61 6d 65   file (zFilename
1716b 3d 3d 22 22 29 2c 20 6f 72 0a 2a 2a 20 6f 70 65  ==""), or.** ope
1716c 6e 65 64 20 6f 6e 20 61 20 66 69 6c 65 20 6c 65  ned on a file le
1716d 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20  ss than N bytes 
1716e 69 6e 20 73 69 7a 65 2c 20 74 68 65 20 6f 75 74  in size, the out
1716f 70 75 74 20 62 75 66 66 65 72 20 69 73 0a 2a 2a  put buffer is.**
17170 20 7a 65 72 6f 65 64 20 61 6e 64 20 53 51 4c 49   zeroed and SQLI
17171 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20  TE_OK returned. 
17172 54 68 65 20 72 61 74 69 6f 6e 61 6c 65 20 66 6f  The rationale fo
17173 72 20 74 68 69 73 20 69 73 20 74 68 61 74 20 74  r this is that t
17174 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  his .** function
17175 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64   is used to read
17176 20 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72   database header
17177 73 2c 20 61 6e 64 20 61 20 6e 65 77 20 74 72 61  s, and a new tra
17178 6e 73 69 65 6e 74 20 6f 72 0a 2a 2a 20 7a 65 72  nsient or.** zer
17179 6f 20 73 69 7a 65 64 20 64 61 74 61 62 61 73 65  o sized database
1717a 20 68 61 73 20 61 20 68 65 61 64 65 72 20 74 68   has a header th
1717b 61 6e 20 63 6f 6e 73 69 73 74 73 20 65 6e 74 69  an consists enti
1717c 72 65 6c 79 20 6f 66 20 7a 65 72 6f 65 73 2e 0a  rely of zeroes..
1717d 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 49 4f 20  **.** If any IO 
1717e 65 72 72 6f 72 20 61 70 61 72 74 20 66 72 6f 6d  error apart from
1717f 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48   SQLITE_IOERR_SH
17180 4f 52 54 5f 52 45 41 44 20 69 73 20 65 6e 63 6f  ORT_READ is enco
17181 75 6e 74 65 72 65 64 2c 0a 2a 2a 20 74 68 65 20  untered,.** the 
17182 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
17183 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61  turned to the ca
17184 6c 6c 65 72 20 61 6e 64 20 74 68 65 20 63 6f 6e  ller and the con
17185 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20  tents of the.** 
17186 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 75 6e  output buffer un
17187 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  defined..*/.SQLI
17188 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
17189 71 6c 69 74 65 33 50 61 67 65 72 52 65 61 64 46  qlite3PagerReadF
1718a 69 6c 65 68 65 61 64 65 72 28 50 61 67 65 72 20  ileheader(Pager 
1718b 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 4e 2c 20  *pPager, int N, 
1718c 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
1718d 44 65 73 74 29 7b 0a 20 20 69 6e 74 20 72 63 20  Dest){.  int rc 
1718e 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 6d  = SQLITE_OK;.  m
1718f 65 6d 73 65 74 28 70 44 65 73 74 2c 20 30 2c 20  emset(pDest, 0, 
17190 4e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73  N);.  assert( is
17191 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29  Open(pPager->fd)
17192 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70   || pPager->temp
17193 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 54 68  File );..  /* Th
17194 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e  is routine is on
17195 6c 79 20 63 61 6c 6c 65 64 20 62 79 20 62 74 72  ly called by btr
17196 65 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61  ee immediately a
17197 66 74 65 72 20 63 72 65 61 74 69 6e 67 0a 20 20  fter creating.  
17198 2a 2a 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a  ** the Pager obj
17199 65 63 74 2e 20 20 54 68 65 72 65 20 68 61 73 20  ect.  There has 
1719a 6e 6f 74 20 62 65 65 6e 20 61 6e 20 6f 70 70 6f  not been an oppo
1719b 72 74 75 6e 69 74 79 20 74 6f 20 74 72 61 6e 73  rtunity to trans
1719c 69 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 57 41  ition.  ** to WA
1719d 4c 20 6d 6f 64 65 20 79 65 74 2e 0a 20 20 2a 2f  L mode yet..  */
1719e 0a 20 20 61 73 73 65 72 74 28 20 21 70 61 67 65  .  assert( !page
1719f 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20  rUseWal(pPager) 
171a0 29 3b 0a 0a 20 20 69 66 28 20 69 73 4f 70 65 6e  );..  if( isOpen
171a1 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a  (pPager->fd) ){.
171a2 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 44 42      IOTRACE(("DB
171a3 48 44 52 20 25 70 20 30 20 25 64 5c 6e 22 2c 20  HDR %p 0 %d\n", 
171a4 70 50 61 67 65 72 2c 20 4e 29 29 0a 20 20 20 20  pPager, N)).    
171a5 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65  rc = sqlite3OsRe
171a6 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70  ad(pPager->fd, p
171a7 44 65 73 74 2c 20 4e 2c 20 30 29 3b 0a 20 20 20  Dest, N, 0);.   
171a8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
171a9 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
171aa 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
171ab 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
171ac 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
171ad 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
171ae 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79  unction may only
171af 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   be called when 
171b0 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69  a read-transacti
171b1 6f 6e 20 69 73 20 6f 70 65 6e 20 6f 6e 0a 2a 2a  on is open on.**
171b2 20 74 68 65 20 70 61 67 65 72 2e 20 49 74 20 72   the pager. It r
171b3 65 74 75 72 6e 73 20 74 68 65 20 74 6f 74 61 6c  eturns the total
171b4 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
171b5 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
171b6 2e 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  ..**.** However,
171b7 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   if the file is 
171b8 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 3c 70  between 1 and <p
171b9 61 67 65 2d 73 69 7a 65 3e 20 62 79 74 65 73 20  age-size> bytes 
171ba 69 6e 20 73 69 7a 65 2c 20 74 68 65 6e 20 0a 2a  in size, then .*
171bb 2a 20 74 68 69 73 20 69 73 20 63 6f 6e 73 69 64  * this is consid
171bc 65 72 65 64 20 61 20 31 20 70 61 67 65 20 66 69  ered a 1 page fi
171bd 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
171be 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
171bf 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
171c0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
171c1 69 6e 74 20 2a 70 6e 50 61 67 65 29 7b 0a 20 20  int *pnPage){.  
171c2 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
171c3 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45  eState>=PAGER_RE
171c4 41 44 45 52 20 29 3b 0a 20 20 61 73 73 65 72 74  ADER );.  assert
171c5 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65  ( pPager->eState
171c6 21 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 46  !=PAGER_WRITER_F
171c7 49 4e 49 53 48 45 44 20 29 3b 0a 20 20 2a 70 6e  INISHED );.  *pn
171c8 50 61 67 65 20 3d 20 28 69 6e 74 29 70 50 61 67  Page = (int)pPag
171c9 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 7d 0a 0a 0a  er->dbSize;.}...
171ca 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74  /*.** Try to obt
171cb 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79  ain a lock of ty
171cc 70 65 20 6c 6f 63 6b 74 79 70 65 20 6f 6e 20 74  pe locktype on t
171cd 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
171ce 2e 20 49 66 0a 2a 2a 20 61 20 73 69 6d 69 6c 61  . If.** a simila
171cf 72 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63  r or greater loc
171d0 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c  k is already hel
171d1 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  d, this function
171d2 20 69 73 20 61 20 6e 6f 2d 6f 70 0a 2a 2a 20 28   is a no-op.** (
171d3 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45  returning SQLITE
171d4 5f 4f 4b 20 69 6d 6d 65 64 69 61 74 65 6c 79 29  _OK immediately)
171d5 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ..**.** Otherwis
171d6 65 2c 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 62  e, attempt to ob
171d7 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b 20 75 73  tain the lock us
171d8 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  ing sqlite3OsLoc
171d9 6b 28 29 2e 20 49 6e 76 6f 6b 65 20 0a 2a 2a 20  k(). Invoke .** 
171da 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  the busy callbac
171db 6b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69 73  k if the lock is
171dc 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f 74 20 61   currently not a
171dd 76 61 69 6c 61 62 6c 65 2e 20 52 65 70 65 61 74  vailable. Repeat
171de 20 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 62   .** until the b
171df 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  usy callback ret
171e0 75 72 6e 73 20 66 61 6c 73 65 20 6f 72 20 75 6e  urns false or un
171e1 74 69 6c 20 74 68 65 20 61 74 74 65 6d 70 74 20  til the attempt 
171e2 74 6f 20 0a 2a 2a 20 6f 62 74 61 69 6e 20 74 68  to .** obtain th
171e3 65 20 6c 6f 63 6b 20 73 75 63 63 65 65 64 73 2e  e lock succeeds.
171e4 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
171e5 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
171e6 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20  ss and an error 
171e7 63 6f 64 65 20 69 66 20 77 65 20 63 61 6e 6e 6f  code if we canno
171e8 74 20 6f 62 74 61 69 6e 0a 2a 2a 20 74 68 65 20  t obtain.** the 
171e9 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 6c 6f 63  lock. If the loc
171ea 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 73 75  k is obtained su
171eb 63 63 65 73 73 66 75 6c 6c 79 2c 20 73 65 74 20  ccessfully, set 
171ec 74 68 65 20 50 61 67 65 72 2e 73 74 61 74 65 20  the Pager.state 
171ed 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 74 6f 20  .** variable to 
171ee 6c 6f 63 6b 74 79 70 65 20 62 65 66 6f 72 65 20  locktype before 
171ef 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74  returning..*/.st
171f0 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77  atic int pager_w
171f1 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 50 61 67 65  ait_on_lock(Page
171f2 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6c  r *pPager, int l
171f3 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20  ocktype){.  int 
171f4 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
171f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
171f6 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
171f7 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74  */..  /* Check t
171f8 68 61 74 20 74 68 69 73 20 69 73 20 65 69 74 68  hat this is eith
171f9 65 72 20 61 20 6e 6f 2d 6f 70 20 28 62 65 63 61  er a no-op (beca
171fa 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65  use the requeste
171fb 64 20 6c 6f 63 6b 20 69 73 20 0a 20 20 2a 2a 20  d lock is .  ** 
171fc 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 72  already held, or
171fd 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 72 61 6e   one of the tran
171fe 73 69 73 74 69 6f 6e 73 20 74 68 61 74 20 74 68  sistions that th
171ff 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 20  e busy-handler. 
17200 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e 76 6f 6b   ** may be invok
17201 65 64 20 64 75 72 69 6e 67 2c 20 61 63 63 6f 72  ed during, accor
17202 64 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 6d 6d  ding to the comm
17203 65 6e 74 20 61 62 6f 76 65 0a 20 20 2a 2a 20 73  ent above.  ** s
17204 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75  qlite3PagerSetBu
17205 73 79 68 61 6e 64 6c 65 72 28 29 2e 0a 20 20 2a  syhandler()..  *
17206 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 50 61  /.  assert( (pPa
17207 67 65 72 2d 3e 65 4c 6f 63 6b 3e 3d 6c 6f 63 6b  ger->eLock>=lock
17208 74 79 70 65 29 0a 20 20 20 20 20 20 20 7c 7c 20  type).       || 
17209 28 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d  (pPager->eLock==
1720a 4e 4f 5f 4c 4f 43 4b 20 26 26 20 6c 6f 63 6b 74  NO_LOCK && lockt
1720b 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
1720c 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61  ).       || (pPa
1720d 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 53 45  ger->eLock==RESE
1720e 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 6c 6f 63  RVED_LOCK && loc
1720f 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
17210 5f 4c 4f 43 4b 29 0a 20 20 29 3b 0a 0a 20 20 64  _LOCK).  );..  d
17211 6f 20 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67  o {.    rc = pag
17212 65 72 4c 6f 63 6b 44 62 28 70 50 61 67 65 72 2c  erLockDb(pPager,
17213 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 7d 77   locktype);.  }w
17214 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  hile( rc==SQLITE
17215 5f 42 55 53 59 20 26 26 20 70 50 61 67 65 72 2d  _BUSY && pPager-
17216 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 28 70 50  >xBusyHandler(pP
17217 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c  ager->pBusyHandl
17218 65 72 41 72 67 29 20 29 3b 0a 20 20 72 65 74 75  erArg) );.  retu
17219 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1721a 46 75 6e 63 74 69 6f 6e 20 61 73 73 65 72 74 54  Function assertT
1721b 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e  runcateConstrain
1721c 74 28 70 50 61 67 65 72 29 20 63 68 65 63 6b 73  t(pPager) checks
1721d 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65   that one of the
1721e 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   .** following i
1721f 73 20 74 72 75 65 20 66 6f 72 20 61 6c 6c 20 64  s true for all d
17220 69 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65  irty pages curre
17221 6e 74 6c 79 20 69 6e 20 74 68 65 20 70 61 67 65  ntly in the page
17222 2d 63 61 63 68 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  -cache:.**.**   
17223 61 29 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62  a) The page numb
17224 65 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  er is less than 
17225 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
17226 73 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20  size of the .** 
17227 20 20 20 20 20 63 75 72 72 65 6e 74 20 64 61 74       current dat
17228 61 62 61 73 65 20 69 6d 61 67 65 2c 20 69 6e 20  abase image, in 
17229 70 61 67 65 73 2c 20 4f 52 0a 2a 2a 0a 2a 2a 20  pages, OR.**.** 
1722a 20 20 62 29 20 69 66 20 74 68 65 20 70 61 67 65    b) if the page
1722b 20 63 6f 6e 74 65 6e 74 20 77 65 72 65 20 77 72   content were wr
1722c 69 74 74 65 6e 20 61 74 20 74 68 69 73 20 74 69  itten at this ti
1722d 6d 65 2c 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74  me, it would not
1722e 0a 2a 2a 20 20 20 20 20 20 62 65 20 6e 65 63 65  .**      be nece
1722f 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20 74  ssary to write t
17230 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65  he current conte
17231 6e 74 20 6f 75 74 20 74 6f 20 74 68 65 20 73 75  nt out to the su
17232 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20  b-journal.**    
17233 20 20 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64    (as determined
17234 20 62 79 20 66 75 6e 63 74 69 6f 6e 20 73 75 62   by function sub
17235 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 29 29  jRequiresPage())
17236 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63  ..**.** If the c
17237 6f 6e 64 69 74 69 6f 6e 20 61 73 73 65 72 74 65  ondition asserte
17238 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
17239 6f 6e 20 77 65 72 65 20 6e 6f 74 20 74 72 75 65  on were not true
1723a 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 69 72  , and the.** dir
1723b 74 79 20 70 61 67 65 20 77 65 72 65 20 74 6f 20  ty page were to 
1723c 62 65 20 64 69 73 63 61 72 64 65 64 20 66 72 6f  be discarded fro
1723d 6d 20 74 68 65 20 63 61 63 68 65 20 76 69 61 20  m the cache via 
1723e 74 68 65 20 70 61 67 65 72 53 74 72 65 73 73 28  the pagerStress(
1723f 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 70 61  ).** routine, pa
17240 67 65 72 53 74 72 65 73 73 28 29 20 77 6f 75 6c  gerStress() woul
17241 64 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65 20  d not write the 
17242 63 75 72 72 65 6e 74 20 70 61 67 65 20 63 6f 6e  current page con
17243 74 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64  tent to.** the d
17244 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66  atabase file. If
17245 20 61 20 73 61 76 65 70 6f 69 6e 74 20 74 72 61   a savepoint tra
17246 6e 73 61 63 74 69 6f 6e 20 77 65 72 65 20 72 6f  nsaction were ro
17247 6c 6c 65 64 20 62 61 63 6b 20 61 66 74 65 72 0a  lled back after.
17248 2a 2a 20 74 68 69 73 20 68 61 70 70 65 6e 65 64  ** this happened
17249 2c 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 65  , the correct be
1724a 68 61 76 69 6f 75 72 20 77 6f 75 6c 64 20 62 65  haviour would be
1724b 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20   to restore the 
1724c 63 75 72 72 65 6e 74 0a 2a 2a 20 63 6f 6e 74 65  current.** conte
1724d 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20  nt of the page. 
1724e 48 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 74  However, since t
1724f 68 69 73 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e  his content is n
17250 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 20 65 69  ot present in ei
17251 74 68 65 72 0a 2a 2a 20 74 68 65 20 64 61 74 61  ther.** the data
17252 62 61 73 65 20 66 69 6c 65 20 6f 72 20 74 68 65  base file or the
17253 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20   portion of the 
17254 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
17255 20 61 6e 64 20 0a 2a 2a 20 73 75 62 2d 6a 6f 75   and .** sub-jou
17256 72 6e 61 6c 20 72 6f 6c 6c 65 64 20 62 61 63 6b  rnal rolled back
17257 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 6f 75   the content cou
17258 6c 64 20 6e 6f 74 20 62 65 20 72 65 73 74 6f 72  ld not be restor
17259 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 61  ed and the.** da
1725a 74 61 62 61 73 65 20 69 6d 61 67 65 20 77 6f 75  tabase image wou
1725b 6c 64 20 62 65 63 6f 6d 65 20 63 6f 72 72 75 70  ld become corrup
1725c 74 2e 20 49 74 20 69 73 20 74 68 65 72 65 66 6f  t. It is therefo
1725d 72 65 20 66 6f 72 74 75 6e 61 74 65 20 74 68 61  re fortunate tha
1725e 74 20 0a 2a 2a 20 74 68 69 73 20 63 69 72 63 75  t .** this circu
1725f 6d 73 74 61 6e 63 65 20 63 61 6e 6e 6f 74 20 61  mstance cannot a
17260 72 69 73 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  rise..*/.#if def
17261 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
17262 47 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  G).static void a
17263 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e  ssertTruncateCon
17264 73 74 72 61 69 6e 74 43 62 28 50 67 48 64 72 20  straintCb(PgHdr 
17265 2a 70 50 67 29 7b 0a 20 20 61 73 73 65 72 74 28  *pPg){.  assert(
17266 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44   pPg->flags&PGHD
17267 52 5f 44 49 52 54 59 20 29 3b 0a 20 20 61 73 73  R_DIRTY );.  ass
17268 65 72 74 28 20 21 73 75 62 6a 52 65 71 75 69 72  ert( !subjRequir
17269 65 73 50 61 67 65 28 70 50 67 29 20 7c 7c 20 70  esPage(pPg) || p
1726a 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 67 2d 3e 70  Pg->pgno<=pPg->p
1726b 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 3b  Pager->dbSize );
1726c 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  .}.static void a
1726d 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e  ssertTruncateCon
1726e 73 74 72 61 69 6e 74 28 50 61 67 65 72 20 2a 70  straint(Pager *p
1726f 50 61 67 65 72 29 7b 0a 20 20 73 71 6c 69 74 65  Pager){.  sqlite
17270 33 50 63 61 63 68 65 49 74 65 72 61 74 65 44 69  3PcacheIterateDi
17271 72 74 79 28 70 50 61 67 65 72 2d 3e 70 50 43 61  rty(pPager->pPCa
17272 63 68 65 2c 20 61 73 73 65 72 74 54 72 75 6e 63  che, assertTrunc
17273 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 29  ateConstraintCb)
17274 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  ;.}.#else.# defi
17275 6e 65 20 61 73 73 65 72 74 54 72 75 6e 63 61 74  ne assertTruncat
17276 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 67  eConstraint(pPag
17277 65 72 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  er).#endif../*.*
17278 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 69  * Truncate the i
17279 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
1727a 65 20 66 69 6c 65 20 69 6d 61 67 65 20 74 6f 20  e file image to 
1727b 6e 50 61 67 65 20 70 61 67 65 73 2e 20 54 68 69  nPage pages. Thi
1727c 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 64  s .** function d
1727d 6f 65 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79  oes not actually
1727e 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61   modify the data
1727f 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 64 69 73  base file on dis
17280 6b 2e 20 49 74 20 0a 2a 2a 20 6a 75 73 74 20 73  k. It .** just s
17281 65 74 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ets the internal
17282 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 70 61   state of the pa
17283 67 65 72 20 6f 62 6a 65 63 74 20 73 6f 20 74 68  ger object so th
17284 61 74 20 74 68 65 20 0a 2a 2a 20 74 72 75 6e 63  at the .** trunc
17285 61 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64 6f  ation will be do
17286 6e 65 20 77 68 65 6e 20 74 68 65 20 63 75 72 72  ne when the curr
17287 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
17288 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2f  is committed..*/
17289 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1728a 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
1728b 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 50  rTruncateImage(P
1728c 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67  ager *pPager, Pg
1728d 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 61 73 73  no nPage){.  ass
1728e 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53  ert( pPager->dbS
1728f 69 7a 65 3e 3d 6e 50 61 67 65 20 29 3b 0a 20 20  ize>=nPage );.  
17290 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
17291 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 57 52  eState>=PAGER_WR
17292 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 20 29 3b  ITER_CACHEMOD );
17293 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  .  pPager->dbSiz
17294 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 61 73 73  e = nPage;.  ass
17295 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74  ertTruncateConst
17296 72 61 69 6e 74 28 70 50 61 67 65 72 29 3b 0a 7d  raint(pPager);.}
17297 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  .../*.** This fu
17298 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
17299 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69   before attempti
1729a 6e 67 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  ng a hot-journal
1729b 20 72 6f 6c 6c 62 61 63 6b 2e 20 49 74 0a 2a 2a   rollback. It.**
1729c 20 73 79 6e 63 73 20 74 68 65 20 6a 6f 75 72 6e   syncs the journ
1729d 61 6c 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2c  al file to disk,
1729e 20 74 68 65 6e 20 73 65 74 73 20 70 50 61 67 65   then sets pPage
1729f 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 74 6f  r->journalHdr to
172a0 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20   the.** size of 
172a1 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
172a2 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 67   so that the pag
172a3 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 72 6f  er_playback() ro
172a4 75 74 69 6e 65 20 6b 6e 6f 77 73 0a 2a 2a 20 74  utine knows.** t
172a5 68 61 74 20 74 68 65 20 65 6e 74 69 72 65 20 6a  hat the entire j
172a6 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20  ournal file has 
172a7 62 65 65 6e 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a  been synced..**.
172a8 2a 2a 20 53 79 6e 63 69 6e 67 20 61 20 68 6f 74  ** Syncing a hot
172a9 2d 6a 6f 75 72 6e 61 6c 20 74 6f 20 64 69 73 6b  -journal to disk
172aa 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69   before attempti
172ab 6e 67 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61  ng to roll it ba
172ac 63 6b 20 65 6e 73 75 72 65 73 20 0a 2a 2a 20 74  ck ensures .** t
172ad 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 2d 66  hat if a power-f
172ae 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 64 75  ailure occurs du
172af 72 69 6e 67 20 74 68 65 20 72 6f 6c 6c 62 61 63  ring the rollbac
172b0 6b 2c 20 74 68 65 20 70 72 6f 63 65 73 73 20 74  k, the process t
172b1 68 61 74 0a 2a 2a 20 61 74 74 65 6d 70 74 73 20  hat.** attempts 
172b2 72 6f 6c 6c 62 61 63 6b 20 66 6f 6c 6c 6f 77 69  rollback followi
172b3 6e 67 20 73 79 73 74 65 6d 20 72 65 63 6f 76 65  ng system recove
172b4 72 79 20 73 65 65 73 20 74 68 65 20 73 61 6d 65  ry sees the same
172b5 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 63 6f 6e 74   journal.** cont
172b6 65 6e 74 20 61 73 20 74 68 69 73 20 70 72 6f 63  ent as this proc
172b7 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 76  ess..**.** If ev
172b8 65 72 79 74 68 69 6e 67 20 67 6f 65 73 20 61 73  erything goes as
172b9 20 70 6c 61 6e 6e 65 64 2c 20 53 51 4c 49 54 45   planned, SQLITE
172ba 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
172bb 20 4f 74 68 65 72 77 69 73 65 2c 20 0a 2a 2a 20   Otherwise, .** 
172bc 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
172bd 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  code..*/.static 
172be 69 6e 74 20 70 61 67 65 72 53 79 6e 63 48 6f 74  int pagerSyncHot
172bf 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70  Journal(Pager *p
172c0 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63  Pager){.  int rc
172c1 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
172c2 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53  if( !pPager->noS
172c3 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ync ){.    rc = 
172c4 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50  sqlite3OsSync(pP
172c5 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 54  ager->jfd, SQLIT
172c6 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a  E_SYNC_NORMAL);.
172c7 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
172c8 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
172c9 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
172ca 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66  eSize(pPager->jf
172cb 64 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72  d, &pPager->jour
172cc 6e 61 6c 48 64 72 29 3b 0a 20 20 7d 0a 20 20 72  nalHdr);.  }.  r
172cd 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
172ce 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20  ** Shutdown the 
172cf 70 61 67 65 20 63 61 63 68 65 2e 20 20 46 72 65  page cache.  Fre
172d0 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e 64  e all memory and
172d1 20 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65 73   close all files
172d2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 61  ..**.** If a tra
172d3 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 69 6e 20  nsaction was in 
172d4 70 72 6f 67 72 65 73 73 20 77 68 65 6e 20 74 68  progress when th
172d5 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
172d6 6c 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 74 72  lled, that.** tr
172d7 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c  ansaction is rol
172d8 6c 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c 20 6f  led back.  All o
172d9 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73  utstanding pages
172da 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64   are invalidated
172db 0a 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 6d 65  .** and their me
172dc 6d 6f 72 79 20 69 73 20 66 72 65 65 64 2e 20 20  mory is freed.  
172dd 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75  Any attempt to u
172de 73 65 20 61 20 70 61 67 65 20 61 73 73 6f 63 69  se a page associ
172df 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 69  ated.** with thi
172e0 73 20 70 61 67 65 20 63 61 63 68 65 20 61 66 74  s page cache aft
172e1 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  er this function
172e2 20 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 6c 69   returns will li
172e3 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 20 69  kely.** result i
172e4 6e 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a 2a  n a coredump..**
172e5 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
172e6 6e 20 61 6c 77 61 79 73 20 73 75 63 63 65 65 64  n always succeed
172e7 73 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74  s. If a transact
172e8 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e  ion is active an
172e9 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d   attempt.** is m
172ea 61 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62  ade to roll it b
172eb 61 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ack. If an error
172ec 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74   occurs during t
172ed 68 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a 20  he rollback .** 
172ee 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d 61  a hot journal ma
172ef 79 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65  y be left in the
172f0 20 66 69 6c 65 73 79 73 74 65 6d 20 62 75 74 20   filesystem but 
172f1 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  no error is retu
172f2 72 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 63  rned.** to the c
172f3 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  aller..*/.SQLITE
172f4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
172f5 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50  ite3PagerClose(P
172f6 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
172f7 20 75 38 20 2a 70 54 6d 70 20 3d 20 28 75 38 20   u8 *pTmp = (u8 
172f8 2a 29 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70  *)pPager->pTmpSp
172f9 61 63 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ace;..  assert( 
172fa 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61  assert_pager_sta
172fb 74 65 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20  te(pPager) );.  
172fc 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65  disable_simulate
172fd 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20  d_io_errors();. 
172fe 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e   sqlite3BeginBen
172ff 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 2f  ignMalloc();.  /
17300 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  * pPager->errCod
17301 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67  e = 0; */.  pPag
17302 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  er->exclusiveMod
17303 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53  e = 0;.#ifndef S
17304 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 20  QLITE_OMIT_WAL. 
17305 20 73 71 6c 69 74 65 33 57 61 6c 43 6c 6f 73 65   sqlite3WalClose
17306 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 70  (pPager->pWal, p
17307 50 61 67 65 72 2d 3e 63 6b 70 74 53 79 6e 63 46  Pager->ckptSyncF
17308 6c 61 67 73 2c 20 70 50 61 67 65 72 2d 3e 70 61  lags, pPager->pa
17309 67 65 53 69 7a 65 2c 20 70 54 6d 70 29 3b 0a 20  geSize, pTmp);. 
1730a 20 70 50 61 67 65 72 2d 3e 70 57 61 6c 20 3d 20   pPager->pWal = 
1730b 30 3b 0a 23 65 6e 64 69 66 0a 20 20 70 61 67 65  0;.#endif.  page
1730c 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b  r_reset(pPager);
1730d 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a  .  if( MEMDB ){.
1730e 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b      pager_unlock
1730f 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c 73  (pPager);.  }els
17310 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 69 74 20  e{.    /* If it 
17311 69 73 20 6f 70 65 6e 2c 20 73 79 6e 63 20 74 68  is open, sync th
17312 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62  e journal file b
17313 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 55 6e  efore calling Un
17314 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 2e  lockAndRollback.
17315 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20  .    ** If this 
17316 69 73 20 6e 6f 74 20 64 6f 6e 65 2c 20 74 68 65  is not done, the
17317 6e 20 61 6e 20 75 6e 73 79 6e 63 65 64 20 70 6f  n an unsynced po
17318 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 65  rtion of the ope
17319 6e 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 20 20 2a  n journal .    *
1731a 2a 20 66 69 6c 65 20 6d 61 79 20 62 65 20 70 6c  * file may be pl
1731b 61 79 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 74  ayed back into t
1731c 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66 20  he database. If 
1731d 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20  a power failure 
1731e 6f 63 63 75 72 73 20 0a 20 20 20 20 2a 2a 20 77  occurs .    ** w
1731f 68 69 6c 65 20 74 68 69 73 20 69 73 20 68 61 70  hile this is hap
17320 70 65 6e 69 6e 67 2c 20 74 68 65 20 64 61 74 61  pening, the data
17321 62 61 73 65 20 63 6f 75 6c 64 20 62 65 63 6f 6d  base could becom
17322 65 20 63 6f 72 72 75 70 74 2e 0a 20 20 20 20 2a  e corrupt..    *
17323 2a 0a 20 20 20 20 2a 2a 20 49 66 20 61 6e 20 65  *.    ** If an e
17324 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
17325 65 20 74 72 79 69 6e 67 20 74 6f 20 73 79 6e 63  e trying to sync
17326 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 73 68   the journal, sh
17327 69 66 74 20 74 68 65 20 70 61 67 65 72 0a 20 20  ift the pager.  
17328 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 45 52    ** into the ER
17329 52 4f 52 20 73 74 61 74 65 2e 20 54 68 69 73 20  ROR state. This 
1732a 63 61 75 73 65 73 20 55 6e 6c 6f 63 6b 41 6e 64  causes UnlockAnd
1732b 52 6f 6c 6c 62 61 63 6b 20 74 6f 20 75 6e 6c 6f  Rollback to unlo
1732c 63 6b 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61  ck the.    ** da
1732d 74 61 62 61 73 65 20 61 6e 64 20 63 6c 6f 73 65  tabase and close
1732e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1732f 65 20 77 69 74 68 6f 75 74 20 61 74 74 65 6d 70  e without attemp
17330 74 69 6e 67 20 74 6f 20 72 6f 6c 6c 20 69 74 0a  ting to roll it.
17331 20 20 20 20 2a 2a 20 62 61 63 6b 20 6f 72 20 66      ** back or f
17332 69 6e 61 6c 69 7a 65 20 69 74 2e 20 54 68 65 20  inalize it. The 
17333 6e 65 78 74 20 64 61 74 61 62 61 73 65 20 75 73  next database us
17334 65 72 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 20  er will have to 
17335 64 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 20  do hot-journal. 
17336 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 62     ** rollback b
17337 65 66 6f 72 65 20 61 63 63 65 73 73 69 6e 67 20  efore accessing 
17338 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
17339 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
1733a 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
1733b 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 70  >jfd) ){.      p
1733c 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65  ager_error(pPage
1733d 72 2c 20 70 61 67 65 72 53 79 6e 63 48 6f 74 4a  r, pagerSyncHotJ
1733e 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 29 3b  ournal(pPager));
1733f 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67 65 72  .    }.    pager
17340 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63  UnlockAndRollbac
17341 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20  k(pPager);.  }. 
17342 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
17343 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 65 6e 61  nMalloc();.  ena
17344 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
17345 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 50 41 47  _errors();.  PAG
17346 45 52 54 52 41 43 45 28 28 22 43 4c 4f 53 45 20  ERTRACE(("CLOSE 
17347 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70  %d\n", PAGERID(p
17348 50 61 67 65 72 29 29 29 3b 0a 20 20 49 4f 54 52  Pager)));.  IOTR
17349 41 43 45 28 28 22 43 4c 4f 53 45 20 25 70 5c 6e  ACE(("CLOSE %p\n
1734a 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 73 71  ", pPager)).  sq
1734b 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61  lite3OsClose(pPa
1734c 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 73 71 6c  ger->jfd);.  sql
1734d 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67  ite3OsClose(pPag
1734e 65 72 2d 3e 66 64 29 3b 0a 20 20 73 71 6c 69 74  er->fd);.  sqlit
1734f 65 33 50 61 67 65 46 72 65 65 28 70 54 6d 70 29  e3PageFree(pTmp)
17350 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68  ;.  sqlite3Pcach
17351 65 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 70  eClose(pPager->p
17352 50 43 61 63 68 65 29 3b 0a 0a 23 69 66 64 65 66  PCache);..#ifdef
17353 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
17354 43 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  C.  if( pPager->
17355 78 43 6f 64 65 63 46 72 65 65 20 29 20 70 50 61  xCodecFree ) pPa
17356 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 28  ger->xCodecFree(
17357 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 29 3b  pPager->pCodec);
17358 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72  .#endif..  asser
17359 74 28 20 21 70 50 61 67 65 72 2d 3e 61 53 61 76  t( !pPager->aSav
1735a 65 70 6f 69 6e 74 20 26 26 20 21 70 50 61 67 65  epoint && !pPage
1735b 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 3b  r->pInJournal );
1735c 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70  .  assert( !isOp
1735d 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
1735e 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  && !isOpen(pPage
1735f 72 2d 3e 73 6a 66 64 29 20 29 3b 0a 0a 20 20 73  r->sjfd) );..  s
17360 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67  qlite3_free(pPag
17361 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  er);.  return SQ
17362 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20  LITE_OK;.}..#if 
17363 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
17364 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
17365 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 52  TE_TEST)./*.** R
17366 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 6e  eturn the page n
17367 75 6d 62 65 72 20 66 6f 72 20 70 61 67 65 20 70  umber for page p
17368 50 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  Pg..*/.SQLITE_PR
17369 49 56 41 54 45 20 50 67 6e 6f 20 73 71 6c 69 74  IVATE Pgno sqlit
1736a 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65  e3PagerPagenumbe
1736b 72 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a  r(DbPage *pPg){.
1736c 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 67    return pPg->pg
1736d 6e 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  no;.}.#endif../*
1736e 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68  .** Increment th
1736f 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
17370 74 20 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0a  t for page pPg..
17371 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
17372 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
17373 67 65 72 52 65 66 28 44 62 50 61 67 65 20 2a 70  gerRef(DbPage *p
17374 50 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63  Pg){.  sqlite3Pc
17375 61 63 68 65 52 65 66 28 70 50 67 29 3b 0a 7d 0a  acheRef(pPg);.}.
17376 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20  ./*.** Sync the 
17377 6a 6f 75 72 6e 61 6c 2e 20 49 6e 20 6f 74 68 65  journal. In othe
17378 72 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 73 75  r words, make su
17379 72 65 20 61 6c 6c 20 74 68 65 20 70 61 67 65 73  re all the pages
1737a 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 62 65   that have.** be
1737b 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  en written to th
1737c 65 20 6a 6f 75 72 6e 61 6c 20 68 61 76 65 20 61  e journal have a
1737d 63 74 75 61 6c 6c 79 20 72 65 61 63 68 65 64 20  ctually reached 
1737e 74 68 65 20 73 75 72 66 61 63 65 20 6f 66 20 74  the surface of t
1737f 68 65 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 63  he.** disk and c
17380 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 69  an be restored i
17381 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61  n the event of a
17382 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c   hot-journal rol
17383 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  lback..**.** If 
17384 74 68 65 20 50 61 67 65 72 2e 6e 6f 53 79 6e 63  the Pager.noSync
17385 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
17386 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
17387 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20   is a no-op..** 
17388 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61  Otherwise, the a
17389 63 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20  ctions required 
1738a 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 6a 6f  depend on the jo
1738b 75 72 6e 61 6c 2d 6d 6f 64 65 20 61 6e 64 20 74  urnal-mode and t
1738c 68 65 20 0a 2a 2a 20 64 65 76 69 63 65 20 63 68  he .** device ch
1738d 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f 66  aracteristics of
1738e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d   the file-system
1738f 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  , as follows:.**
17390 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 6a  .**   * If the j
17391 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 61  ournal file is a
17392 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72  n in-memory jour
17393 6e 61 6c 20 66 69 6c 65 2c 20 6e 6f 20 61 63 74  nal file, no act
17394 69 6f 6e 20 6e 65 65 64 0a 2a 2a 20 20 20 20 20  ion need.**     
17395 62 65 20 74 61 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20  be taken..**.** 
17396 20 20 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69    * Otherwise, i
17397 66 20 74 68 65 20 64 65 76 69 63 65 20 64 6f 65  f the device doe
17398 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 74 68  s not support th
17399 65 20 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72  e SAFE_APPEND pr
1739a 6f 70 65 72 74 79 2c 0a 2a 2a 20 20 20 20 20 74  operty,.**     t
1739b 68 65 6e 20 74 68 65 20 6e 52 65 63 20 66 69 65  hen the nRec fie
1739c 6c 64 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  ld of the most r
1739d 65 63 65 6e 74 6c 79 20 77 72 69 74 74 65 6e 20  ecently written 
1739e 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a  journal header.*
1739f 2a 20 20 20 20 20 69 73 20 75 70 64 61 74 65 64  *     is updated
173a0 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20   to contain the 
173a1 6e 75 6d 62 65 72 20 6f 66 20 6a 6f 75 72 6e 61  number of journa
173a2 6c 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 68  l records that h
173a3 61 76 65 0a 2a 2a 20 20 20 20 20 62 65 65 6e 20  ave.**     been 
173a4 77 72 69 74 74 65 6e 20 66 6f 6c 6c 6f 77 69 6e  written followin
173a5 67 20 69 74 2e 20 49 66 20 74 68 65 20 70 61 67  g it. If the pag
173a6 65 72 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20  er is operating 
173a7 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 0a 2a 2a 20  in full-sync.** 
173a8 20 20 20 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74      mode, then t
173a9 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
173aa 69 73 20 73 79 6e 63 65 64 20 62 65 66 6f 72 65  is synced before
173ab 20 74 68 69 73 20 66 69 65 6c 64 20 69 73 20 75   this field is u
173ac 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20  pdated..**.**   
173ad 2a 20 49 66 20 74 68 65 20 64 65 76 69 63 65 20  * If the device 
173ae 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74  does not support
173af 20 74 68 65 20 53 45 51 55 45 4e 54 49 41 4c 20   the SEQUENTIAL 
173b0 70 72 6f 70 65 72 74 79 2c 20 74 68 65 6e 20 0a  property, then .
173b1 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c 20 66  **     journal f
173b2 69 6c 65 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a  ile is synced..*
173b3 2a 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70 73 65 75  *.** Or, in pseu
173b4 64 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a 2a 20 20  do-code:.**.**  
173b5 20 69 66 28 20 4e 4f 54 20 3c 69 6e 2d 6d 65 6d   if( NOT <in-mem
173b6 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e 20 29 7b 0a  ory journal> ){.
173b7 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53  **     if( NOT S
173b8 41 46 45 5f 41 50 50 45 4e 44 20 29 7b 0a 2a 2a  AFE_APPEND ){.**
173b9 20 20 20 20 20 20 20 69 66 28 20 3c 66 75 6c 6c         if( <full
173ba 2d 73 79 6e 63 20 6d 6f 64 65 3e 20 29 20 78 53  -sync mode> ) xS
173bb 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c  ync(<journal fil
173bc 65 3e 29 3b 0a 2a 2a 20 20 20 20 20 20 20 3c 75  e>);.**       <u
173bd 70 64 61 74 65 20 6e 52 65 63 20 66 69 65 6c 64  pdate nRec field
173be 3e 0a 2a 2a 20 20 20 20 20 7d 20 0a 2a 2a 20 20  >.**     } .**  
173bf 20 20 20 69 66 28 20 4e 4f 54 20 53 45 51 55 45     if( NOT SEQUE
173c0 4e 54 49 41 4c 20 29 20 78 53 79 6e 63 28 3c 6a  NTIAL ) xSync(<j
173c1 6f 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a  ournal file>);.*
173c2 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 73  *   }.**.** If s
173c3 75 63 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20  uccessful, this 
173c4 72 6f 75 74 69 6e 65 20 63 6c 65 61 72 73 20 74  routine clears t
173c5 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  he PGHDR_NEED_SY
173c6 4e 43 20 66 6c 61 67 20 6f 66 20 65 76 65 72 79  NC flag of every
173c7 20 0a 2a 2a 20 70 61 67 65 20 63 75 72 72 65 6e   .** page curren
173c8 74 6c 79 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f  tly held in memo
173c9 72 79 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  ry before return
173ca 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49  ing SQLITE_OK. I
173cb 66 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72  f an IO.** error
173cc 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c   is encountered,
173cd 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72   then the IO err
173ce 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
173cf 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ned to the calle
173d0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
173d1 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67   syncJournal(Pag
173d2 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
173d3 6e 65 77 48 64 72 29 7b 0a 20 20 69 6e 74 20 72  newHdr){.  int r
173d4 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
173d5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
173d6 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20  turn code */..  
173d7 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
173d8 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52  eState==PAGER_WR
173d9 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 0a 20 20  ITER_CACHEMOD.  
173da 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e       || pPager->
173db 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52  eState==PAGER_WR
173dc 49 54 45 52 5f 44 42 4d 4f 44 0a 20 20 29 3b 0a  ITER_DBMOD.  );.
173dd 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74    assert( assert
173de 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61  _pager_state(pPa
173df 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  ger) );.  assert
173e0 28 20 21 70 61 67 65 72 55 73 65 57 61 6c 28 70  ( !pagerUseWal(p
173e1 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 72 63 20  Pager) );..  rc 
173e2 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 45 78  = sqlite3PagerEx
173e3 63 6c 75 73 69 76 65 4c 6f 63 6b 28 70 50 61 67  clusiveLock(pPag
173e4 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  er);.  if( rc!=S
173e5 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
173e6 6e 20 72 63 3b 0a 0a 20 20 69 66 28 20 21 70 50  n rc;..  if( !pP
173e7 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a  ager->noSync ){.
173e8 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61      assert( !pPa
173e9 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b  ger->tempFile );
173ea 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28  .    if( isOpen(
173eb 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20  pPager->jfd) && 
173ec 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
173ed 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode!=PAGER_JOURN
173ee 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b  ALMODE_MEMORY ){
173ef 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74  .      const int
173f0 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73   iDc = sqlite3Os
173f1 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
173f2 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64  stics(pPager->fd
173f3 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
173f4 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
173f5 6a 66 64 29 20 29 3b 0a 0a 20 20 20 20 20 20 69  jfd) );..      i
173f6 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54  f( 0==(iDc&SQLIT
173f7 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50  E_IOCAP_SAFE_APP
173f8 45 4e 44 29 20 29 7b 0a 20 20 20 20 20 20 20 20  END) ){.        
173f9 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 64 65  /* This block de
173fa 61 6c 73 20 77 69 74 68 20 61 6e 20 6f 62 73 63  als with an obsc
173fb 75 72 65 20 70 72 6f 62 6c 65 6d 2e 20 49 66 20  ure problem. If 
173fc 74 68 65 20 6c 61 73 74 20 63 6f 6e 6e 65 63 74  the last connect
173fd 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ion.        ** t
173fe 68 61 74 20 77 72 6f 74 65 20 74 6f 20 74 68 69  hat wrote to thi
173ff 73 20 64 61 74 61 62 61 73 65 20 77 61 73 20 6f  s database was o
17400 70 65 72 61 74 69 6e 67 20 69 6e 20 70 65 72 73  perating in pers
17401 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 0a 20  istent-journal. 
17402 20 20 20 20 20 20 20 2a 2a 20 6d 6f 64 65 2c 20         ** mode, 
17403 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  then the journal
17404 20 66 69 6c 65 20 6d 61 79 20 61 74 20 74 68 69   file may at thi
17405 73 20 70 6f 69 6e 74 20 61 63 74 75 61 6c 6c 79  s point actually
17406 20 62 65 20 6c 61 72 67 65 72 0a 20 20 20 20 20   be larger.     
17407 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72     ** than Pager
17408 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 62 79 74 65  .journalOff byte
17409 73 2e 20 49 66 20 74 68 65 20 6e 65 78 74 20 74  s. If the next t
1740a 68 69 6e 67 20 69 6e 20 74 68 65 20 6a 6f 75 72  hing in the jour
1740b 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 66  nal.        ** f
1740c 69 6c 65 20 68 61 70 70 65 6e 73 20 74 6f 20 62  ile happens to b
1740d 65 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64  e a journal-head
1740e 65 72 20 28 77 72 69 74 74 65 6e 20 61 73 20 70  er (written as p
1740f 61 72 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20  art of the.     
17410 20 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 20 63     ** previous c
17411 6f 6e 6e 65 63 74 69 6f 6e 27 73 20 74 72 61 6e  onnection's tran
17412 73 61 63 74 69 6f 6e 29 2c 20 61 6e 64 20 61 20  saction), and a 
17413 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 2d 66  crash or power-f
17414 61 69 6c 75 72 65 20 0a 20 20 20 20 20 20 20 20  ailure .        
17415 2a 2a 20 6f 63 63 75 72 73 20 61 66 74 65 72 20  ** occurs after 
17416 6e 52 65 63 20 69 73 20 75 70 64 61 74 65 64 20  nRec is updated 
17417 62 75 74 20 62 65 66 6f 72 65 20 74 68 69 73 20  but before this 
17418 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65  connection write
17419 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  s .        ** an
1741a 79 74 68 69 6e 67 20 65 6c 73 65 20 74 6f 20 74  ything else to t
1741b 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1741c 28 6f 72 20 63 6f 6d 6d 69 74 73 2f 72 6f 6c 6c  (or commits/roll
1741d 73 20 62 61 63 6b 20 69 74 73 20 0a 20 20 20 20  s back its .    
1741e 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69      ** transacti
1741f 6f 6e 29 2c 20 74 68 65 6e 20 53 51 4c 69 74 65  on), then SQLite
17420 20 6d 61 79 20 62 65 63 6f 6d 65 20 63 6f 6e 66   may become conf
17421 75 73 65 64 20 77 68 65 6e 20 64 6f 69 6e 67 20  used when doing 
17422 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  the .        ** 
17423 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c  hot-journal roll
17424 62 61 63 6b 20 66 6f 6c 6c 6f 77 69 6e 67 20 72  back following r
17425 65 63 6f 76 65 72 79 2e 20 49 74 20 6d 61 79 20  ecovery. It may 
17426 72 6f 6c 6c 20 62 61 63 6b 20 61 6c 6c 0a 20 20  roll back all.  
17427 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 69 73        ** of this
17428 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 64 61 74   connections dat
17429 61 2c 20 74 68 65 6e 20 70 72 6f 63 65 65 64 20  a, then proceed 
1742a 74 6f 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20  to rolling back 
1742b 74 68 65 20 6f 6c 64 2c 0a 20 20 20 20 20 20 20  the old,.       
1742c 20 2a 2a 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20   ** out-of-date 
1742d 64 61 74 61 20 74 68 61 74 20 66 6f 6c 6c 6f 77  data that follow
1742e 73 20 69 74 2e 20 44 61 74 61 62 61 73 65 20 63  s it. Database c
1742f 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20  orruption..     
17430 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
17431 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20   To work around 
17432 74 68 69 73 2c 20 69 66 20 74 68 65 20 6a 6f 75  this, if the jou
17433 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 61  rnal file does a
17434 70 70 65 61 72 20 74 6f 20 63 6f 6e 74 61 69 6e  ppear to contain
17435 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 76 61  .        ** a va
17436 6c 69 64 20 68 65 61 64 65 72 20 66 6f 6c 6c 6f  lid header follo
17437 77 69 6e 67 20 50 61 67 65 72 2e 6a 6f 75 72 6e  wing Pager.journ
17438 61 6c 4f 66 66 2c 20 74 68 65 6e 20 77 72 69 74  alOff, then writ
17439 65 20 61 20 30 78 30 30 0a 20 20 20 20 20 20 20  e a 0x00.       
1743a 20 2a 2a 20 62 79 74 65 20 74 6f 20 74 68 65 20   ** byte to the 
1743b 73 74 61 72 74 20 6f 66 20 69 74 20 74 6f 20 70  start of it to p
1743c 72 65 76 65 6e 74 20 69 74 20 66 72 6f 6d 20 62  revent it from b
1743d 65 69 6e 67 20 72 65 63 6f 67 6e 69 7a 65 64 2e  eing recognized.
1743e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
1743f 20 20 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 20      ** Variable 
17440 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 20 69  iNextHdrOffset i
17441 73 20 73 65 74 20 74 6f 20 74 68 65 20 6f 66 66  s set to the off
17442 73 65 74 20 61 74 20 77 68 69 63 68 20 74 68 69  set at which thi
17443 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f  s.        ** pro
17444 62 6c 65 6d 61 74 69 63 20 68 65 61 64 65 72 20  blematic header 
17445 77 69 6c 6c 20 6f 63 63 75 72 2c 20 69 66 20 69  will occur, if i
17446 74 20 65 78 69 73 74 73 2e 20 61 4d 61 67 69 63  t exists. aMagic
17447 20 69 73 20 75 73 65 64 20 0a 20 20 20 20 20 20   is used .      
17448 20 20 2a 2a 20 61 73 20 61 20 74 65 6d 70 6f 72    ** as a tempor
17449 61 72 79 20 62 75 66 66 65 72 20 74 6f 20 69 6e  ary buffer to in
1744a 73 70 65 63 74 20 74 68 65 20 66 69 72 73 74 20  spect the first 
1744b 63 6f 75 70 6c 65 20 6f 66 20 62 79 74 65 73 20  couple of bytes 
1744c 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  of.        ** th
1744d 65 20 70 6f 74 65 6e 74 69 61 6c 20 6a 6f 75 72  e potential jour
1744e 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 20 20 20  nal header..    
1744f 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
17450 36 34 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65  64 iNextHdrOffse
17451 74 3b 0a 20 20 20 20 20 20 20 20 75 38 20 61 4d  t;.        u8 aM
17452 61 67 69 63 5b 38 5d 3b 0a 20 20 20 20 20 20 20  agic[8];.       
17453 20 75 38 20 7a 48 65 61 64 65 72 5b 73 69 7a 65   u8 zHeader[size
17454 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  of(aJournalMagic
17455 29 2b 34 5d 3b 0a 0a 20 20 20 20 20 20 20 20 6d  )+4];..        m
17456 65 6d 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61  emcpy(zHeader, a
17457 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69  JournalMagic, si
17458 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
17459 69 63 29 29 3b 0a 20 20 20 20 20 20 20 20 70 75  ic));.        pu
1745a 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72  t32bits(&zHeader
1745b 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c  [sizeof(aJournal
1745c 4d 61 67 69 63 29 5d 2c 20 70 50 61 67 65 72 2d  Magic)], pPager-
1745d 3e 6e 52 65 63 29 3b 0a 0a 20 20 20 20 20 20 20  >nRec);..       
1745e 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 20   iNextHdrOffset 
1745f 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73  = journalHdrOffs
17460 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  et(pPager);.    
17461 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17462 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 6a  OsRead(pPager->j
17463 66 64 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 69  fd, aMagic, 8, i
17464 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29 3b 0a  NextHdrOffset);.
17465 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
17466 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d  SQLITE_OK && 0==
17467 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61  memcmp(aMagic, a
17468 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 29  JournalMagic, 8)
17469 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74   ){.          st
1746a 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 7a 65  atic const u8 ze
1746b 72 6f 62 79 74 65 20 3d 20 30 3b 0a 20 20 20 20  robyte = 0;.    
1746c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1746d 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
1746e 2d 3e 6a 66 64 2c 20 26 7a 65 72 6f 62 79 74 65  ->jfd, &zerobyte
1746f 2c 20 31 2c 20 69 4e 65 78 74 48 64 72 4f 66 66  , 1, iNextHdrOff
17470 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  set);.        }.
17471 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
17472 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21  SQLITE_OK && rc!
17473 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48  =SQLITE_IOERR_SH
17474 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20  ORT_READ ){.    
17475 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
17476 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  .        }..    
17477 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
17478 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e 74 6f   nRec value into
17479 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1747a 65 20 68 65 61 64 65 72 2e 20 49 66 20 69 6e 0a  e header. If in.
1747b 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6c 6c 2d          ** full-
1747c 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65  synchronous mode
1747d 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e  , sync the journ
1747e 61 6c 20 66 69 72 73 74 2e 20 54 68 69 73 20 65  al first. This e
1747f 6e 73 75 72 65 73 20 74 68 61 74 0a 20 20 20 20  nsures that.    
17480 20 20 20 20 2a 2a 20 61 6c 6c 20 64 61 74 61 20      ** all data 
17481 68 61 73 20 72 65 61 6c 6c 79 20 68 69 74 20 74  has really hit t
17482 68 65 20 64 69 73 6b 20 62 65 66 6f 72 65 20 6e  he disk before n
17483 52 65 63 20 69 73 20 75 70 64 61 74 65 64 20 74  Rec is updated t
17484 6f 20 6d 61 72 6b 0a 20 20 20 20 20 20 20 20 2a  o mark.        *
17485 2a 20 69 74 20 61 73 20 61 20 63 61 6e 64 69 64  * it as a candid
17486 61 74 65 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b  ate for rollback
17487 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ..        **.   
17488 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20       ** This is 
17489 6e 6f 74 20 72 65 71 75 69 72 65 64 20 69 66 20  not required if 
1748a 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d  the persistent m
1748b 65 64 69 61 20 73 75 70 70 6f 72 74 73 20 74 68  edia supports th
1748c 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 41 46  e.        ** SAF
1748d 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74  E_APPEND propert
1748e 79 2e 20 42 65 63 61 75 73 65 20 69 6e 20 74 68  y. Because in th
1748f 69 73 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f  is case it is no
17490 74 20 70 6f 73 73 69 62 6c 65 20 0a 20 20 20 20  t possible .    
17491 20 20 20 20 2a 2a 20 66 6f 72 20 67 61 72 62 61      ** for garba
17492 67 65 20 64 61 74 61 20 74 6f 20 62 65 20 61 70  ge data to be ap
17493 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 66 69  pended to the fi
17494 6c 65 2c 20 74 68 65 20 6e 52 65 63 20 66 69 65  le, the nRec fie
17495 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73  ld.        ** is
17496 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20   populated with 
17497 30 78 46 46 46 46 46 46 46 46 20 77 68 65 6e 20  0xFFFFFFFF when 
17498 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  the journal head
17499 65 72 20 69 73 20 77 72 69 74 74 65 6e 0a 20 20  er is written.  
1749a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6e 65 76        ** and nev
1749b 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75  er needs to be u
1749c 70 64 61 74 65 64 2e 0a 20 20 20 20 20 20 20 20  pdated..        
1749d 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  */.        if( p
1749e 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20  Pager->fullSync 
1749f 26 26 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54  && 0==(iDc&SQLIT
174a0 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49  E_IOCAP_SEQUENTI
174a1 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  AL) ){.         
174a2 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 59   PAGERTRACE(("SY
174a3 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 25 64  NC journal of %d
174a4 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61  \n", PAGERID(pPa
174a5 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20 20 20  ger)));.        
174a6 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53 59 4e    IOTRACE(("JSYN
174a7 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29  C %p\n", pPager)
174a8 29 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ).          rc =
174a9 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70   sqlite3OsSync(p
174aa 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67  Pager->jfd, pPag
174ab 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 29 3b 0a  er->syncFlags);.
174ac 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
174ad 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
174ae 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
174af 20 7d 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41   }.        IOTRA
174b0 43 45 28 28 22 4a 48 44 52 20 25 70 20 25 6c 6c  CE(("JHDR %p %ll
174b1 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50  d\n", pPager, pP
174b2 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
174b3 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ));.        rc =
174b4 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
174b5 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61  .            pPa
174b6 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65  ger->jfd, zHeade
174b7 72 2c 20 73 69 7a 65 6f 66 28 7a 48 65 61 64 65  r, sizeof(zHeade
174b8 72 29 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  r), pPager->jour
174b9 6e 61 6c 48 64 72 0a 20 20 20 20 20 20 20 20 29  nalHdr.        )
174ba 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
174bb 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
174bc 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
174bd 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69  .      if( 0==(i
174be 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  Dc&SQLITE_IOCAP_
174bf 53 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20  SEQUENTIAL) ){. 
174c0 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43         PAGERTRAC
174c1 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c  E(("SYNC journal
174c2 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52   of %d\n", PAGER
174c3 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20  ID(pPager)));.  
174c4 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22        IOTRACE(("
174c5 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61  JSYNC %p\n", pPa
174c6 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 63  ger)).        rc
174c7 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63   = sqlite3OsSync
174c8 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50  (pPager->jfd, pP
174c9 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 7c  ager->syncFlags|
174ca 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 61   .          (pPa
174cb 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67 73 3d 3d  ger->syncFlags==
174cc 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c  SQLITE_SYNC_FULL
174cd 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54  ?SQLITE_SYNC_DAT
174ce 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20 20  AONLY:0).       
174cf 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
174d0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
174d1 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
174d2 20 7d 0a 0a 20 20 20 20 20 20 70 50 61 67 65 72   }..      pPager
174d3 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70  ->journalHdr = p
174d4 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
174d5 66 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 77  f;.      if( new
174d6 48 64 72 20 26 26 20 30 3d 3d 28 69 44 63 26 53  Hdr && 0==(iDc&S
174d7 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45  QLITE_IOCAP_SAFE
174d8 5f 41 50 50 45 4e 44 29 20 29 7b 0a 20 20 20 20  _APPEND) ){.    
174d9 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63      pPager->nRec
174da 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72 63   = 0;.        rc
174db 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48   = writeJournalH
174dc 64 72 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  dr(pPager);.    
174dd 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
174de 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
174df 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  c;.      }.    }
174e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61 67  else{.      pPag
174e1 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d  er->journalHdr =
174e2 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
174e3 4f 66 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  Off;.    }.  }..
174e4 20 20 2f 2a 20 55 6e 6c 65 73 73 20 74 68 65 20    /* Unless the 
174e5 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f 53 79  pager is in noSy
174e6 6e 63 20 6d 6f 64 65 2c 20 74 68 65 20 6a 6f 75  nc mode, the jou
174e7 72 6e 61 6c 20 66 69 6c 65 20 77 61 73 20 6a 75  rnal file was ju
174e8 73 74 20 0a 20 20 2a 2a 20 73 75 63 63 65 73 73  st .  ** success
174e9 66 75 6c 6c 79 20 73 79 6e 63 65 64 2e 20 45 69  fully synced. Ei
174ea 74 68 65 72 20 77 61 79 2c 20 63 6c 65 61 72 20  ther way, clear 
174eb 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53  the PGHDR_NEED_S
174ec 59 4e 43 20 66 6c 61 67 20 6f 6e 20 0a 20 20 2a  YNC flag on .  *
174ed 2a 20 61 6c 6c 20 70 61 67 65 73 2e 0a 20 20 2a  * all pages..  *
174ee 2f 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68  /.  sqlite3Pcach
174ef 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28  eClearSyncFlags(
174f0 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
174f1 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61  ;.  pPager->eSta
174f2 74 65 20 3d 20 50 41 47 45 52 5f 57 52 49 54 45  te = PAGER_WRITE
174f3 52 5f 44 42 4d 4f 44 3b 0a 20 20 61 73 73 65 72  R_DBMOD;.  asser
174f4 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f  t( assert_pager_
174f5 73 74 61 74 65 28 70 50 61 67 65 72 29 20 29 3b  state(pPager) );
174f6 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
174f7 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
174f8 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  e argument is th
174f9 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 6e  e first in a lin
174fa 6b 65 64 20 6c 69 73 74 20 6f 66 20 64 69 72 74  ked list of dirt
174fb 79 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65  y pages connecte
174fc 64 0a 2a 2a 20 62 79 20 74 68 65 20 50 67 48 64  d.** by the PgHd
174fd 72 2e 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72  r.pDirty pointer
174fe 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
174ff 77 72 69 74 65 73 20 65 61 63 68 20 6f 6e 65 20  writes each one 
17500 6f 66 20 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d  of the.** in-mem
17501 6f 72 79 20 70 61 67 65 73 20 69 6e 20 74 68 65  ory pages in the
17502 20 6c 69 73 74 20 74 6f 20 74 68 65 20 64 61 74   list to the dat
17503 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20  abase file. The 
17504 61 72 67 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20  argument may.** 
17505 62 65 20 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65  be NULL, represe
17506 6e 74 69 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c  nting an empty l
17507 69 73 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ist. In this cas
17508 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  e this function 
17509 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  is.** a no-op..*
1750a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 6d  *.** The pager m
1750b 75 73 74 20 68 6f 6c 64 20 61 74 20 6c 65 61 73  ust hold at leas
1750c 74 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  t a RESERVED loc
1750d 6b 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63  k when this func
1750e 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65  tion.** is calle
1750f 64 2e 20 42 65 66 6f 72 65 20 77 72 69 74 69 6e  d. Before writin
17510 67 20 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68  g anything to th
17511 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
17512 20 74 68 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73   this lock.** is
17513 20 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20   upgraded to an 
17514 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
17515 49 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e  If the lock cann
17516 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a  ot be obtained,.
17517 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69  ** SQLITE_BUSY i
17518 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e  s returned and n
17519 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65  o data is writte
1751a 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  n to the databas
1751b 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49  e file..** .** I
1751c 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 61  f the pager is a
1751d 20 74 65 6d 70 2d 66 69 6c 65 20 70 61 67 65 72   temp-file pager
1751e 20 61 6e 64 20 74 68 65 20 61 63 74 75 61 6c 20   and the actual 
1751f 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65  file-system file
17520 0a 2a 2a 20 69 73 20 6e 6f 74 20 79 65 74 20 6f  .** is not yet o
17521 70 65 6e 2c 20 69 74 20 69 73 20 63 72 65 61 74  pen, it is creat
17522 65 64 20 61 6e 64 20 6f 70 65 6e 65 64 20 62 65  ed and opened be
17523 66 6f 72 65 20 61 6e 79 20 64 61 74 61 20 69 73  fore any data is
17524 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 6f 75 74   .** written out
17525 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65  ..**.** Once the
17526 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 75   lock has been u
17527 70 67 72 61 64 65 64 20 61 6e 64 2c 20 69 66 20  pgraded and, if 
17528 6e 65 63 65 73 73 61 72 79 2c 20 74 68 65 20 66  necessary, the f
17529 69 6c 65 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74  ile opened,.** t
1752a 68 65 20 70 61 67 65 73 20 61 72 65 20 77 72 69  he pages are wri
1752b 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20  tten out to the 
1752c 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e  database file in
1752d 20 6c 69 73 74 20 6f 72 64 65 72 2e 20 57 72 69   list order. Wri
1752e 74 69 6e 67 0a 2a 2a 20 61 20 70 61 67 65 20 69  ting.** a page i
1752f 73 20 73 6b 69 70 70 65 64 20 69 66 20 69 74 20  s skipped if it 
17530 6d 65 65 74 73 20 65 69 74 68 65 72 20 6f 66 20  meets either of 
17531 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72  the following cr
17532 69 74 65 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20  iteria:.**.**   
17533 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65  * The page numbe
17534 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  r is greater tha
17535 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20  n Pager.dbSize, 
17536 6f 72 0a 2a 2a 20 20 20 2a 20 54 68 65 20 50 47  or.**   * The PG
17537 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66  HDR_DONT_WRITE f
17538 6c 61 67 20 69 73 20 73 65 74 20 6f 6e 20 74 68  lag is set on th
17539 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  e page..**.** If
1753a 20 77 72 69 74 69 6e 67 20 6f 75 74 20 61 20 70   writing out a p
1753b 61 67 65 20 63 61 75 73 65 73 20 74 68 65 20 64  age causes the d
1753c 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20  atabase file to 
1753d 67 72 6f 77 2c 20 50 61 67 65 72 2e 64 62 46 69  grow, Pager.dbFi
1753e 6c 65 53 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64  leSize.** is upd
1753f 61 74 65 64 20 61 63 63 6f 72 64 69 6e 67 6c 79  ated accordingly
17540 2e 20 49 66 20 70 61 67 65 20 31 20 69 73 20 77  . If page 1 is w
17541 72 69 74 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e  ritten out, then
17542 20 74 68 65 20 76 61 6c 75 65 20 63 61 63 68 65   the value cache
17543 64 0a 2a 2a 20 69 6e 20 50 61 67 65 72 2e 64 62  d.** in Pager.db
17544 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 20 75 70  FileVers[] is up
17545 64 61 74 65 64 20 74 6f 20 6d 61 74 63 68 20 74  dated to match t
17546 68 65 20 6e 65 77 20 76 61 6c 75 65 20 73 74 6f  he new value sto
17547 72 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61  red in.** the da
17548 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
17549 2a 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e 67  ** If everything
1754a 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20   is successful, 
1754b 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
1754c 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20  urned. If an IO 
1754d 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73  error .** occurs
1754e 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f  , an IO error co
1754f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  de is returned. 
17550 4f 72 2c 20 69 66 20 74 68 65 20 45 58 43 4c 55  Or, if the EXCLU
17551 53 49 56 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74  SIVE lock cannot
17552 0a 2a 2a 20 62 65 20 6f 62 74 61 69 6e 65 64 2c  .** be obtained,
17553 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20   SQLITE_BUSY is 
17554 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61  returned..*/.sta
17555 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72  tic int pager_wr
17556 69 74 65 5f 70 61 67 65 6c 69 73 74 28 50 61 67  ite_pagelist(Pag
17557 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 48 64  er *pPager, PgHd
17558 72 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74  r *pList){.  int
17559 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1755a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1755b 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
1755c 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66   */..  /* This f
1755d 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20  unction is only 
1755e 63 61 6c 6c 65 64 20 66 6f 72 20 72 6f 6c 6c 62  called for rollb
1755f 61 63 6b 20 70 61 67 65 72 73 20 69 6e 20 57 52  ack pagers in WR
17560 49 54 45 52 5f 44 42 4d 4f 44 20 73 74 61 74 65  ITER_DBMOD state
17561 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21  . */.  assert( !
17562 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67  pagerUseWal(pPag
17563 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  er) );.  assert(
17564 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d   pPager->eState=
17565 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 44 42  =PAGER_WRITER_DB
17566 4d 4f 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28  MOD );.  assert(
17567 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d   pPager->eLock==
17568 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29  EXCLUSIVE_LOCK )
17569 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66  ;..  /* If the f
1756a 69 6c 65 20 69 73 20 61 20 74 65 6d 70 2d 66 69  ile is a temp-fi
1756b 6c 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62  le has not yet b
1756c 65 65 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e  een opened, open
1756d 20 69 74 20 6e 6f 77 2e 20 49 74 0a 20 20 2a 2a   it now. It.  **
1756e 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
1756f 20 66 6f 72 20 72 63 20 74 6f 20 62 65 20 6f 74   for rc to be ot
17570 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  her than SQLITE_
17571 4f 4b 20 69 66 20 74 68 69 73 20 62 72 61 6e 63  OK if this branc
17572 68 0a 20 20 2a 2a 20 69 73 20 74 61 6b 65 6e 2c  h.  ** is taken,
17573 20 61 73 20 70 61 67 65 72 5f 77 61 69 74 5f 6f   as pager_wait_o
17574 6e 5f 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f  n_lock() is a no
17575 2d 6f 70 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c  -op for temp-fil
17576 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  es..  */.  if( !
17577 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
17578 64 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  d) ){.    assert
17579 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  ( pPager->tempFi
1757a 6c 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  le && rc==SQLITE
1757b 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  _OK );.    rc = 
1757c 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50  pagerOpentemp(pP
1757d 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 66 64  ager, pPager->fd
1757e 2c 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61  , pPager->vfsFla
1757f 67 73 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42  gs);.  }..  /* B
17580 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 20  efore the first 
17581 77 72 69 74 65 2c 20 67 69 76 65 20 74 68 65 20  write, give the 
17582 56 46 53 20 61 20 68 69 6e 74 20 6f 66 20 77 68  VFS a hint of wh
17583 61 74 20 74 68 65 20 66 69 6e 61 6c 0a 20 20 2a  at the final.  *
17584 2a 20 66 69 6c 65 20 73 69 7a 65 20 77 69 6c 6c  * file size will
17585 20 62 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65   be..  */.  asse
17586 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  rt( rc!=SQLITE_O
17587 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67  K || isOpen(pPag
17588 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 69 66 28  er->fd) );.  if(
17589 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1758a 26 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  & pPager->dbSize
1758b 3e 70 50 61 67 65 72 2d 3e 64 62 48 69 6e 74 53  >pPager->dbHintS
1758c 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ize ){.    sqlit
1758d 65 33 5f 69 6e 74 36 34 20 73 7a 46 69 6c 65 20  e3_int64 szFile 
1758e 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  = pPager->pageSi
1758f 7a 65 20 2a 20 28 73 71 6c 69 74 65 33 5f 69 6e  ze * (sqlite3_in
17590 74 36 34 29 70 50 61 67 65 72 2d 3e 64 62 53 69  t64)pPager->dbSi
17591 7a 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f  ze;.    sqlite3O
17592 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 48 69 6e 74  sFileControlHint
17593 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 53 51 4c  (pPager->fd, SQL
17594 49 54 45 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f 48  ITE_FCNTL_SIZE_H
17595 49 4e 54 2c 20 26 73 7a 46 69 6c 65 29 3b 0a 20  INT, &szFile);. 
17596 20 20 20 70 50 61 67 65 72 2d 3e 64 62 48 69 6e     pPager->dbHin
17597 74 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e  tSize = pPager->
17598 64 62 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 77  dbSize;.  }..  w
17599 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  hile( rc==SQLITE
1759a 5f 4f 4b 20 26 26 20 70 4c 69 73 74 20 29 7b 0a  _OK && pList ){.
1759b 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20      Pgno pgno = 
1759c 70 4c 69 73 74 2d 3e 70 67 6e 6f 3b 0a 0a 20 20  pList->pgno;..  
1759d 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
1759e 65 20 64 69 72 74 79 20 70 61 67 65 73 20 69 6e  e dirty pages in
1759f 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20   the page cache 
175a0 77 69 74 68 20 70 61 67 65 20 6e 75 6d 62 65 72  with page number
175a1 73 20 67 72 65 61 74 65 72 0a 20 20 20 20 2a 2a  s greater.    **
175a2 20 74 68 61 6e 20 50 61 67 65 72 2e 64 62 53 69   than Pager.dbSi
175a3 7a 65 2c 20 74 68 69 73 20 6d 65 61 6e 73 20 73  ze, this means s
175a4 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63  qlite3PagerTrunc
175a5 61 74 65 49 6d 61 67 65 28 29 20 77 61 73 20 63  ateImage() was c
175a6 61 6c 6c 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20  alled to.    ** 
175a7 6d 61 6b 65 20 74 68 65 20 66 69 6c 65 20 73 6d  make the file sm
175a8 61 6c 6c 65 72 20 28 70 72 65 73 75 6d 61 62 6c  aller (presumabl
175a9 79 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d  y by auto-vacuum
175aa 20 63 6f 64 65 29 2e 20 44 6f 20 6e 6f 74 20 77   code). Do not w
175ab 72 69 74 65 0a 20 20 20 20 2a 2a 20 61 6e 79 20  rite.    ** any 
175ac 73 75 63 68 20 70 61 67 65 73 20 74 6f 20 74 68  such pages to th
175ad 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20  e file..    **. 
175ae 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 64 6f 20 6e     ** Also, do n
175af 6f 74 20 77 72 69 74 65 20 6f 75 74 20 61 6e 79  ot write out any
175b0 20 70 61 67 65 20 74 68 61 74 20 68 61 73 20 74   page that has t
175b1 68 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52  he PGHDR_DONT_WR
175b2 49 54 45 20 66 6c 61 67 0a 20 20 20 20 2a 2a 20  ITE flag.    ** 
175b3 73 65 74 20 28 73 65 74 20 62 79 20 73 71 6c 69  set (set by sqli
175b4 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74  te3PagerDontWrit
175b5 65 28 29 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  e())..    */.   
175b6 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 65   if( pgno<=pPage
175b7 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 30 3d 3d  r->dbSize && 0==
175b8 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73 26 50 47  (pList->flags&PG
175b9 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 29 20  HDR_DONT_WRITE) 
175ba 29 7b 0a 20 20 20 20 20 20 69 36 34 20 6f 66 66  ){.      i64 off
175bb 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28  set = (pgno-1)*(
175bc 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65  i64)pPager->page
175bd 53 69 7a 65 3b 20 20 20 2f 2a 20 4f 66 66 73 65  Size;   /* Offse
175be 74 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20  t to write */.  
175bf 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 3b      char *pData;
175c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
175c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
175c2 20 20 20 2f 2a 20 44 61 74 61 20 74 6f 20 77 72     /* Data to wr
175c3 69 74 65 20 2a 2f 20 20 20 20 0a 0a 20 20 20 20  ite */    ..    
175c4 20 20 61 73 73 65 72 74 28 20 28 70 4c 69 73 74    assert( (pList
175c5 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
175c6 45 44 5f 53 59 4e 43 29 3d 3d 30 20 29 3b 0a 20  ED_SYNC)==0 );. 
175c7 20 20 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e       if( pList->
175c8 70 67 6e 6f 3d 3d 31 20 29 20 70 61 67 65 72 5f  pgno==1 ) pager_
175c9 77 72 69 74 65 5f 63 68 61 6e 67 65 63 6f 75 6e  write_changecoun
175ca 74 65 72 28 70 4c 69 73 74 29 3b 0a 0a 20 20 20  ter(pList);..   
175cb 20 20 20 2f 2a 20 45 6e 63 6f 64 65 20 74 68 65     /* Encode the
175cc 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20   database */.   
175cd 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65 72     CODEC2(pPager
175ce 2c 20 70 4c 69 73 74 2d 3e 70 44 61 74 61 2c 20  , pList->pData, 
175cf 70 67 6e 6f 2c 20 36 2c 20 72 65 74 75 72 6e 20  pgno, 6, return 
175d0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44  SQLITE_NOMEM, pD
175d1 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ata);..      /* 
175d2 57 72 69 74 65 20 6f 75 74 20 74 68 65 20 70 61  Write out the pa
175d3 67 65 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20  ge data. */.    
175d4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
175d5 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64  Write(pPager->fd
175d6 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d  , pData, pPager-
175d7 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65  >pageSize, offse
175d8 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66  t);..      /* If
175d9 20 70 61 67 65 20 31 20 77 61 73 20 6a 75 73 74   page 1 was just
175da 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65   written, update
175db 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72   Pager.dbFileVer
175dc 73 20 74 6f 20 6d 61 74 63 68 0a 20 20 20 20 20  s to match.     
175dd 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6e 6f   ** the value no
175de 77 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  w stored in the 
175df 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49  database file. I
175e0 66 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 0a  f writing this .
175e1 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 63 61        ** page ca
175e2 75 73 65 64 20 74 68 65 20 64 61 74 61 62 61 73  used the databas
175e3 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20  e file to grow, 
175e4 75 70 64 61 74 65 20 64 62 46 69 6c 65 53 69 7a  update dbFileSiz
175e5 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  e. .      */.   
175e6 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29     if( pgno==1 )
175e7 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  {.        memcpy
175e8 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  (&pPager->dbFile
175e9 56 65 72 73 2c 20 26 70 44 61 74 61 5b 32 34 5d  Vers, &pData[24]
175ea 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d  , sizeof(pPager-
175eb 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20  >dbFileVers));. 
175ec 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
175ed 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62   pgno>pPager->db
175ee 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20  FileSize ){.    
175ef 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69      pPager->dbFi
175f0 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20  leSize = pgno;. 
175f1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61       }.      pPa
175f2 67 65 72 2d 3e 61 53 74 61 74 5b 50 41 47 45 52  ger->aStat[PAGER
175f3 5f 53 54 41 54 5f 57 52 49 54 45 5d 2b 2b 3b 0a  _STAT_WRITE]++;.
175f4 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65  .      /* Update
175f5 20 61 6e 79 20 62 61 63 6b 75 70 20 6f 62 6a 65   any backup obje
175f6 63 74 73 20 63 6f 70 79 69 6e 67 20 74 68 65 20  cts copying the 
175f7 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 73  contents of this
175f8 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 20 20 20   pager. */.     
175f9 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70   sqlite3BackupUp
175fa 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61  date(pPager->pBa
175fb 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 75 38 2a  ckup, pgno, (u8*
175fc 29 70 4c 69 73 74 2d 3e 70 44 61 74 61 29 3b 0a  )pList->pData);.
175fd 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43  .      PAGERTRAC
175fe 45 28 28 22 53 54 4f 52 45 20 25 64 20 70 61 67  E(("STORE %d pag
175ff 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 5c  e %d hash(%08x)\
17600 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
17601 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70         PAGERID(p
17602 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61  Pager), pgno, pa
17603 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69  ger_pagehash(pLi
17604 73 74 29 29 29 3b 0a 20 20 20 20 20 20 49 4f 54  st)));.      IOT
17605 52 41 43 45 28 28 22 50 47 4f 55 54 20 25 70 20  RACE(("PGOUT %p 
17606 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70  %d\n", pPager, p
17607 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50 41 47  gno));.      PAG
17608 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f  ER_INCR(sqlite3_
17609 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f  pager_writedb_co
1760a 75 6e 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  unt);.    }else{
1760b 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43  .      PAGERTRAC
1760c 45 28 28 22 4e 4f 53 54 4f 52 45 20 25 64 20 70  E(("NOSTORE %d p
1760d 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52  age %d\n", PAGER
1760e 49 44 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f  ID(pPager), pgno
1760f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61  ));.    }.    pa
17610 67 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68  ger_set_pagehash
17611 28 70 4c 69 73 74 29 3b 0a 20 20 20 20 70 4c 69  (pList);.    pLi
17612 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72  st = pList->pDir
17613 74 79 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  ty;.  }..  retur
17614 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  n rc;.}../*.** E
17615 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 73  nsure that the s
17616 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ub-journal file 
17617 69 73 20 6f 70 65 6e 2e 20 49 66 20 69 74 20 69  is open. If it i
17618 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 2c 20  s already open, 
17619 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f  this .** functio
1761a 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  n is a no-op..**
1761b 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
1761c 20 72 65 74 75 72 6e 65 64 20 69 66 20 65 76 65   returned if eve
1761d 72 79 74 68 69 6e 67 20 67 6f 65 73 20 61 63 63  rything goes acc
1761e 6f 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e 2e 20  ording to plan. 
1761f 41 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f  An .** SQLITE_IO
17620 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f  ERR_XXX error co
17621 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  de is returned i
17622 66 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  f a call to sqli
17623 74 65 33 4f 73 4f 70 65 6e 28 29 20 0a 2a 2a 20  te3OsOpen() .** 
17624 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  fails..*/.static
17625 20 69 6e 74 20 6f 70 65 6e 53 75 62 4a 6f 75 72   int openSubJour
17626 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65  nal(Pager *pPage
17627 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  r){.  int rc = S
17628 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
17629 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  !isOpen(pPager->
1762a 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 69 66 28  sjfd) ){.    if(
1762b 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
1762c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
1762d 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 7c  NALMODE_MEMORY |
1762e 7c 20 70 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e  | pPager->subjIn
1762f 4d 65 6d 6f 72 79 20 29 7b 0a 20 20 20 20 20 20  Memory ){.      
17630 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61  sqlite3MemJourna
17631 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a  lOpen(pPager->sj
17632 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  fd);.    }else{.
17633 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
17634 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65 72 2c  Opentemp(pPager,
17635 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 53   pPager->sjfd, S
17636 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f  QLITE_OPEN_SUBJO
17637 55 52 4e 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20  URNAL);.    }.  
17638 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
17639 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61  ../*.** Append a
1763a 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 63   record of the c
1763b 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20  urrent state of 
1763c 70 61 67 65 20 70 50 67 20 74 6f 20 74 68 65 20  page pPg to the 
1763d 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 0a 2a 2a  sub-journal. .**
1763e 20 49 74 20 69 73 20 74 68 65 20 63 61 6c 6c 65   It is the calle
1763f 72 73 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  rs responsibilit
17640 79 20 74 6f 20 75 73 65 20 73 75 62 6a 52 65 71  y to use subjReq
17641 75 69 72 65 73 50 61 67 65 28 29 20 74 6f 20 63  uiresPage() to c
17642 68 65 63 6b 20 0a 2a 2a 20 74 68 61 74 20 69 74  heck .** that it
17643 20 69 73 20 72 65 61 6c 6c 79 20 72 65 71 75 69   is really requi
17644 72 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69  red before calli
17645 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ng this function
17646 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65  ..**.** If succe
17647 73 73 66 75 6c 2c 20 73 65 74 20 74 68 65 20 62  ssful, set the b
17648 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  it corresponding
17649 20 74 6f 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e   to pPg->pgno in
1764a 20 74 68 65 20 62 69 74 76 65 63 73 0a 2a 2a 20   the bitvecs.** 
1764b 66 6f 72 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76  for all open sav
1764c 65 70 6f 69 6e 74 73 20 62 65 66 6f 72 65 20 72  epoints before r
1764d 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  eturning..**.** 
1764e 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
1764f 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20  turns SQLITE_OK 
17650 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73  if everything is
17651 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61 6e 20   successful, an 
17652 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65  IO.** error code
17653 20 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 20   if the attempt 
17654 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20  to write to the 
17655 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 61 69 6c  sub-journal fail
17656 73 2c 20 6f 72 20 0a 2a 2a 20 53 51 4c 49 54 45  s, or .** SQLITE
17657 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c  _NOMEM if a mall
17658 6f 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20 73  oc fails while s
17659 65 74 74 69 6e 67 20 61 20 62 69 74 20 69 6e 20  etting a bit in 
1765a 61 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 62  a savepoint.** b
1765b 69 74 76 65 63 2e 0a 2a 2f 0a 73 74 61 74 69 63  itvec..*/.static
1765c 20 69 6e 74 20 73 75 62 6a 6f 75 72 6e 61 6c 50   int subjournalP
1765d 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b  age(PgHdr *pPg){
1765e 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1765f 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a  TE_OK;.  Pager *
17660 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50  pPager = pPg->pP
17661 61 67 65 72 3b 0a 20 20 69 66 28 20 70 50 61 67  ager;.  if( pPag
17662 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21  er->journalMode!
17663 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
17664 44 45 5f 4f 46 46 20 29 7b 0a 0a 20 20 20 20 2f  DE_OFF ){..    /
17665 2a 20 4f 70 65 6e 20 74 68 65 20 73 75 62 2d 6a  * Open the sub-j
17666 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 68 61  ournal, if it ha
17667 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 62 65  s not already be
17668 65 6e 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 20  en opened */.   
17669 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
1766a 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20  >useJournal );. 
1766b 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65     assert( isOpe
1766c 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c  n(pPager->jfd) |
1766d 7c 20 70 61 67 65 72 55 73 65 57 61 6c 28 70 50  | pagerUseWal(pP
1766e 61 67 65 72 29 20 29 3b 0a 20 20 20 20 61 73 73  ager) );.    ass
1766f 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67  ert( isOpen(pPag
17670 65 72 2d 3e 73 6a 66 64 29 20 7c 7c 20 70 50 61  er->sjfd) || pPa
17671 67 65 72 2d 3e 6e 53 75 62 52 65 63 3d 3d 30 20  ger->nSubRec==0 
17672 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
17673 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65  agerUseWal(pPage
17674 72 29 20 0a 20 20 20 20 20 20 20 20 20 7c 7c 20  r) .         || 
17675 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50  pageInJournal(pP
17676 67 29 20 0a 20 20 20 20 20 20 20 20 20 7c 7c 20  g) .         || 
17677 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72  pPg->pgno>pPager
17678 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 0a 20 20  ->dbOrigSize .  
17679 20 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 6f 70    );.    rc = op
1767a 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61  enSubJournal(pPa
1767b 67 65 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  ger);..    /* If
1767c 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
1767d 20 77 61 73 20 6f 70 65 6e 65 64 20 73 75 63 63   was opened succ
1767e 65 73 73 66 75 6c 6c 79 20 28 6f 72 20 77 61 73  essfully (or was
1767f 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 29 2c 0a   already open),.
17680 20 20 20 20 2a 2a 20 77 72 69 74 65 20 74 68 65      ** write the
17681 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 20   journal record 
17682 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 20  into the file.  
17683 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  */.    if( rc==S
17684 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17685 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20    void *pData = 
17686 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20  pPg->pData;.    
17687 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 28    i64 offset = (
17688 69 36 34 29 70 50 61 67 65 72 2d 3e 6e 53 75 62  i64)pPager->nSub
17689 52 65 63 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70  Rec*(4+pPager->p
1768a 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  ageSize);.      
1768b 63 68 61 72 20 2a 70 44 61 74 61 32 3b 0a 20 20  char *pData2;.  
1768c 0a 20 20 20 20 20 20 43 4f 44 45 43 32 28 70 50  .      CODEC2(pP
1768d 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67  ager, pData, pPg
1768e 2d 3e 70 67 6e 6f 2c 20 37 2c 20 72 65 74 75 72  ->pgno, 7, retur
1768f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20  n SQLITE_NOMEM, 
17690 70 44 61 74 61 32 29 3b 0a 20 20 20 20 20 20 50  pData2);.      P
17691 41 47 45 52 54 52 41 43 45 28 28 22 53 54 4d 54  AGERTRACE(("STMT
17692 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65  -JOURNAL %d page
17693 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28   %d\n", PAGERID(
17694 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67  pPager), pPg->pg
17695 6e 6f 29 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  no));.      rc =
17696 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61   write32bits(pPa
17697 67 65 72 2d 3e 73 6a 66 64 2c 20 6f 66 66 73 65  ger->sjfd, offse
17698 74 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20  t, pPg->pgno);. 
17699 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
1769a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1769b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1769c 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 73 6a  Write(pPager->sj
1769d 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50 61 67  fd, pData2, pPag
1769e 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66  er->pageSize, of
1769f 66 73 65 74 2b 34 29 3b 0a 20 20 20 20 20 20 7d  fset+4);.      }
176a0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
176a1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
176a2 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 53  {.    pPager->nS
176a3 75 62 52 65 63 2b 2b 3b 0a 20 20 20 20 61 73 73  ubRec++;.    ass
176a4 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53 61  ert( pPager->nSa
176a5 76 65 70 6f 69 6e 74 3e 30 20 29 3b 0a 20 20 20  vepoint>0 );.   
176a6 20 72 63 20 3d 20 61 64 64 54 6f 53 61 76 65 70   rc = addToSavep
176a7 6f 69 6e 74 42 69 74 76 65 63 73 28 70 50 61 67  ointBitvecs(pPag
176a8 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a  er, pPg->pgno);.
176a9 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
176aa 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
176ab 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
176ac 64 20 62 79 20 74 68 65 20 70 63 61 63 68 65 20  d by the pcache 
176ad 6c 61 79 65 72 20 77 68 65 6e 20 69 74 20 68 61  layer when it ha
176ae 73 20 72 65 61 63 68 65 64 20 73 6f 6d 65 0a 2a  s reached some.*
176af 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 79 20 6c 69  * soft memory li
176b0 6d 69 74 2e 20 54 68 65 20 66 69 72 73 74 20 61  mit. The first a
176b1 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69  rgument is a poi
176b2 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72 20  nter to a Pager 
176b3 6f 62 6a 65 63 74 0a 2a 2a 20 28 63 61 73 74 20  object.** (cast 
176b4 61 73 20 61 20 76 6f 69 64 2a 29 2e 20 54 68 65  as a void*). The
176b5 20 70 61 67 65 72 20 69 73 20 61 6c 77 61 79 73   pager is always
176b6 20 27 70 75 72 67 65 61 62 6c 65 27 20 28 6e 6f   'purgeable' (no
176b7 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a  t an in-memory.*
176b8 2a 20 64 61 74 61 62 61 73 65 29 2e 20 54 68 65  * database). The
176b9 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
176ba 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20   is a reference 
176bb 74 6f 20 61 20 70 61 67 65 20 74 68 61 74 20 69  to a page that i
176bc 73 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20  s .** currently 
176bd 64 69 72 74 79 20 62 75 74 20 68 61 73 20 6e 6f  dirty but has no
176be 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66   outstanding ref
176bf 65 72 65 6e 63 65 73 2e 20 54 68 65 20 70 61 67  erences. The pag
176c0 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61  e.** is always a
176c1 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
176c2 68 65 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20  he Pager object 
176c3 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
176c4 72 73 74 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  rst .** argument
176c5 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 62 20  ..**.** The job 
176c6 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  of this function
176c7 20 69 73 20 74 6f 20 6d 61 6b 65 20 70 50 67 20   is to make pPg 
176c8 63 6c 65 61 6e 20 62 79 20 77 72 69 74 69 6e 67  clean by writing
176c9 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a   its contents.**
176ca 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61   out to the data
176cb 62 61 73 65 20 66 69 6c 65 2c 20 69 66 20 70 6f  base file, if po
176cc 73 73 69 62 6c 65 2e 20 54 68 69 73 20 6d 61 79  ssible. This may
176cd 20 69 6e 76 6f 6c 76 65 20 73 79 6e 63 69 6e 67   involve syncing
176ce 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20   the.** journal 
176cf 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  file. .**.** If 
176d0 73 75 63 63 65 73 73 66 75 6c 2c 20 73 71 6c 69  successful, sqli
176d1 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65  te3PcacheMakeCle
176d2 61 6e 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f  an() is called o
176d3 6e 20 74 68 65 20 70 61 67 65 20 61 6e 64 0a 2a  n the page and.*
176d4 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  * SQLITE_OK retu
176d5 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65  rned. If an IO e
176d6 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
176d7 65 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65  e trying to make
176d8 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 63 6c 65   the.** page cle
176d9 61 6e 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72  an, the IO error
176da 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
176db 64 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 63  d. If the page c
176dc 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6d 61 64 65  annot be.** made
176dd 20 63 6c 65 61 6e 20 66 6f 72 20 73 6f 6d 65 20   clean for some 
176de 6f 74 68 65 72 20 72 65 61 73 6f 6e 2c 20 62 75  other reason, bu
176df 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72  t no error occur
176e0 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f  s, then SQLITE_O
176e1 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  K.** is returned
176e2 20 62 79 20 73 71 6c 69 74 65 33 50 63 61 63 68   by sqlite3Pcach
176e3 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 20  eMakeClean() is 
176e4 6e 6f 74 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73  not called..*/.s
176e5 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 53  tatic int pagerS
176e6 74 72 65 73 73 28 76 6f 69 64 20 2a 70 2c 20 50  tress(void *p, P
176e7 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61  gHdr *pPg){.  Pa
176e8 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 28 50  ger *pPager = (P
176e9 61 67 65 72 20 2a 29 70 3b 0a 20 20 69 6e 74 20  ager *)p;.  int 
176ea 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
176eb 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e  .  assert( pPg->
176ec 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20 29  pPager==pPager )
176ed 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d  ;.  assert( pPg-
176ee 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
176ef 54 59 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  TY );..  /* The 
176f0 64 6f 4e 6f 74 53 79 6e 63 53 70 69 6c 6c 20 66  doNotSyncSpill f
176f1 6c 61 67 20 69 73 20 73 65 74 20 64 75 72 69 6e  lag is set durin
176f2 67 20 74 69 6d 65 73 20 77 68 65 6e 20 64 6f 69  g times when doi
176f3 6e 67 20 61 20 73 79 6e 63 20 6f 66 0a 20 20 2a  ng a sync of.  *
176f4 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 61  * journal (and a
176f5 64 64 69 6e 67 20 61 20 6e 65 77 20 68 65 61 64  dding a new head
176f6 65 72 29 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77  er) is not allow
176f7 65 64 2e 20 20 54 68 69 73 20 6f 63 63 75 72 73  ed.  This occurs
176f8 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 63 61 6c  .  ** during cal
176f9 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67  ls to sqlite3Pag
176fa 65 72 57 72 69 74 65 28 29 20 77 68 69 6c 65 20  erWrite() while 
176fb 74 72 79 69 6e 67 20 74 6f 20 6a 6f 75 72 6e 61  trying to journa
176fc 6c 20 6d 75 6c 74 69 70 6c 65 0a 20 20 2a 2a 20  l multiple.  ** 
176fd 70 61 67 65 73 20 62 65 6c 6f 6e 67 69 6e 67 20  pages belonging 
176fe 74 6f 20 74 68 65 20 73 61 6d 65 20 73 65 63 74  to the same sect
176ff 6f 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  or..  **.  ** Th
17700 65 20 64 6f 4e 6f 74 53 70 69 6c 6c 20 66 6c 61  e doNotSpill fla
17701 67 20 69 6e 68 69 62 69 74 73 20 61 6c 6c 20 63  g inhibits all c
17702 61 63 68 65 20 73 70 69 6c 6c 69 6e 67 20 72 65  ache spilling re
17703 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74  gardless of whet
17704 68 65 72 0a 20 20 2a 2a 20 6f 72 20 6e 6f 74 20  her.  ** or not 
17705 61 20 73 79 6e 63 20 69 73 20 72 65 71 75 69 72  a sync is requir
17706 65 64 2e 20 20 54 68 69 73 20 69 73 20 73 65 74  ed.  This is set
17707 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61   during a rollba
17708 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 70  ck..  **.  ** Sp
17709 69 6c 6c 69 6e 67 20 69 73 20 61 6c 73 6f 20 70  illing is also p
1770a 72 6f 68 69 62 69 74 65 64 20 77 68 65 6e 20 69  rohibited when i
1770b 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65  n an error state
1770c 20 73 69 6e 63 65 20 74 68 61 74 20 63 6f 75 6c   since that coul
1770d 64 0a 20 20 2a 2a 20 6c 65 61 64 20 74 6f 20 64  d.  ** lead to d
1770e 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
1770f 6f 6e 2e 20 20 20 49 6e 20 74 68 65 20 63 75 72  on.   In the cur
17710 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74  rent implementat
17711 6f 6e 20 69 74 20 0a 20 20 2a 2a 20 69 73 20 69  on it .  ** is i
17712 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 73 71  mpossible for sq
17713 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68  lite3PcacheFetch
17714 28 29 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20  () to be called 
17715 77 69 74 68 20 63 72 65 61 74 65 46 6c 61 67 3d  with createFlag=
17716 3d 31 0a 20 20 2a 2a 20 77 68 69 6c 65 20 69 6e  =1.  ** while in
17717 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
17718 2c 20 68 65 6e 63 65 20 69 74 20 69 73 20 69 6d  , hence it is im
17719 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69  possible for thi
1771a 73 20 72 6f 75 74 69 6e 65 20 74 6f 0a 20 20 2a  s routine to.  *
1771b 2a 20 62 65 20 63 61 6c 6c 65 64 20 69 6e 20 74  * be called in t
1771c 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20  he error state. 
1771d 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20 77   Nevertheless, w
1771e 65 20 69 6e 63 6c 75 64 65 20 61 20 4e 45 56 45  e include a NEVE
1771f 52 28 29 0a 20 20 2a 2a 20 74 65 73 74 20 66 6f  R().  ** test fo
17720 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  r the error stat
17721 65 20 61 73 20 61 20 73 61 66 65 67 75 61 72 64  e as a safeguard
17722 20 61 67 61 69 6e 73 74 20 66 75 74 75 72 65 20   against future 
17723 63 68 61 6e 67 65 73 2e 0a 20 20 2a 2f 0a 20 20  changes..  */.  
17724 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72  if( NEVER(pPager
17725 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65 74  ->errCode) ) ret
17726 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
17727 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e   if( pPager->doN
17728 6f 74 53 70 69 6c 6c 20 29 20 72 65 74 75 72 6e  otSpill ) return
17729 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
1772a 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53  ( pPager->doNotS
1772b 79 6e 63 53 70 69 6c 6c 20 26 26 20 28 70 50 67  yncSpill && (pPg
1772c 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f  ->flags & PGHDR_
1772d 4e 45 45 44 5f 53 59 4e 43 29 21 3d 30 20 29 7b  NEED_SYNC)!=0 ){
1772e 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1772f 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 70 50  TE_OK;.  }..  pP
17730 67 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20  g->pDirty = 0;. 
17731 20 69 66 28 20 70 61 67 65 72 55 73 65 57 61 6c   if( pagerUseWal
17732 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20  (pPager) ){.    
17733 2f 2a 20 57 72 69 74 65 20 61 20 73 69 6e 67 6c  /* Write a singl
17734 65 20 66 72 61 6d 65 20 66 6f 72 20 74 68 69 73  e frame for this
17735 20 70 61 67 65 20 74 6f 20 74 68 65 20 6c 6f 67   page to the log
17736 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 75 62  . */.    if( sub
17737 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50  jRequiresPage(pP
17738 67 29 20 29 7b 20 0a 20 20 20 20 20 20 72 63 20  g) ){ .      rc 
17739 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65  = subjournalPage
1773a 28 70 50 67 29 3b 20 0a 20 20 20 20 7d 0a 20 20  (pPg); .    }.  
1773b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1773c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
1773d 3d 20 70 61 67 65 72 57 61 6c 46 72 61 6d 65 73  = pagerWalFrames
1773e 28 70 50 61 67 65 72 2c 20 70 50 67 2c 20 30 2c  (pPager, pPg, 0,
1773f 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c   0);.    }.  }el
17740 73 65 7b 0a 20 20 0a 20 20 20 20 2f 2a 20 53 79  se{.  .    /* Sy
17741 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  nc the journal f
17742 69 6c 65 20 69 66 20 72 65 71 75 69 72 65 64 2e  ile if required.
17743 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 67 2d   */.    if( pPg-
17744 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
17745 44 5f 53 59 4e 43 20 0a 20 20 20 20 20 7c 7c 20  D_SYNC .     || 
17746 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d  pPager->eState==
17747 50 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43  PAGER_WRITER_CAC
17748 48 45 4d 4f 44 0a 20 20 20 20 29 7b 0a 20 20 20  HEMOD.    ){.   
17749 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72     rc = syncJour
1774a 6e 61 6c 28 70 50 61 67 65 72 2c 20 31 29 3b 0a  nal(pPager, 1);.
1774b 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
1774c 49 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  If the page numb
1774d 65 72 20 6f 66 20 74 68 69 73 20 70 61 67 65 20  er of this page 
1774e 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  is larger than t
1774f 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20  he current size 
17750 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61  of.    ** the da
17751 74 61 62 61 73 65 20 69 6d 61 67 65 2c 20 69 74  tabase image, it
17752 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20   may need to be 
17753 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73  written to the s
17754 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20  ub-journal..    
17755 2a 2a 20 54 68 69 73 20 69 73 20 62 65 63 61 75  ** This is becau
17756 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 70  se the call to p
17757 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c  ager_write_pagel
17758 69 73 74 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c  ist() below will
17759 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 61 63 74 75   not.    ** actu
1775a 61 6c 6c 79 20 77 72 69 74 65 20 64 61 74 61 20  ally write data 
1775b 74 6f 20 74 68 65 20 66 69 6c 65 20 69 6e 20 74  to the file in t
1775c 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 2a 2a  his case..    **
1775d 0a 20 20 20 20 2a 2a 20 43 6f 6e 73 69 64 65 72  .    ** Consider
1775e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
1775f 65 71 75 65 6e 63 65 20 6f 66 20 65 76 65 6e 74  equence of event
17760 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  s:.    **.    **
17761 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 2a 2a     BEGIN;.    **
17762 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61       <journal pa
17763 67 65 20 58 3e 0a 20 20 20 20 2a 2a 20 20 20 20  ge X>.    **    
17764 20 3c 6d 6f 64 69 66 79 20 70 61 67 65 20 58 3e   <modify page X>
17765 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 41 56 45  .    **     SAVE
17766 50 4f 49 4e 54 20 73 70 3b 0a 20 20 20 20 2a 2a  POINT sp;.    **
17767 20 20 20 20 20 20 20 3c 73 68 72 69 6e 6b 20 64         <shrink d
17768 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20  atabase file to 
17769 59 20 70 61 67 65 73 3e 0a 20 20 20 20 2a 2a 20  Y pages>.    ** 
1776a 20 20 20 20 20 20 70 61 67 65 72 53 74 72 65 73        pagerStres
1776b 73 28 70 61 67 65 20 58 29 0a 20 20 20 20 2a 2a  s(page X).    **
1776c 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f       ROLLBACK TO
1776d 20 73 70 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20   sp;.    **.    
1776e 2a 2a 20 49 66 20 28 58 3e 59 29 2c 20 74 68 65  ** If (X>Y), the
1776f 6e 20 77 68 65 6e 20 70 61 67 65 72 53 74 72 65  n when pagerStre
17770 73 73 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67  ss is called pag
17771 65 20 58 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  e X will not be 
17772 77 72 69 74 74 65 6e 0a 20 20 20 20 2a 2a 20 6f  written.    ** o
17773 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ut to the databa
17774 73 65 20 66 69 6c 65 2c 20 62 75 74 20 77 69 6c  se file, but wil
17775 6c 20 62 65 20 64 72 6f 70 70 65 64 20 66 72 6f  l be dropped fro
17776 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65  m the cache. The
17777 6e 2c 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77  n,.    ** follow
17778 69 6e 67 20 74 68 65 20 22 52 4f 4c 4c 42 41 43  ing the "ROLLBAC
17779 4b 20 54 4f 20 73 70 22 20 73 74 61 74 65 6d 65  K TO sp" stateme
1777a 6e 74 2c 20 72 65 61 64 69 6e 67 20 70 61 67 65  nt, reading page
1777b 20 58 20 77 69 6c 6c 20 72 65 61 64 0a 20 20 20   X will read.   
1777c 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20 74 68   ** data from th
1777d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1777e 20 54 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68   This will be th
1777f 65 20 63 6f 70 79 20 6f 66 20 70 61 67 65 20 58  e copy of page X
17780 20 61 73 20 69 74 0a 20 20 20 20 2a 2a 20 77 61   as it.    ** wa
17781 73 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73  s when the trans
17782 61 63 74 69 6f 6e 20 73 74 61 72 74 65 64 2c 20  action started, 
17783 6e 6f 74 20 61 73 20 69 74 20 77 61 73 20 77 68  not as it was wh
17784 65 6e 20 22 53 41 56 45 50 4f 49 4e 54 20 73 70  en "SAVEPOINT sp
17785 22 0a 20 20 20 20 2a 2a 20 77 61 73 20 65 78 65  ".    ** was exe
17786 63 75 74 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20  cuted..    **.  
17787 20 20 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f    ** The solutio
17788 6e 20 69 73 20 74 6f 20 77 72 69 74 65 20 74 68  n is to write th
17789 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 20 66  e current data f
1778a 6f 72 20 70 61 67 65 20 58 20 69 6e 74 6f 20 74  or page X into t
1778b 68 65 20 0a 20 20 20 20 2a 2a 20 73 75 62 2d 6a  he .    ** sub-j
1778c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 6f 77 20  ournal file now 
1778d 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c  (if it is not al
1778e 72 65 61 64 79 20 74 68 65 72 65 29 2c 20 73 6f  ready there), so
1778f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 0a 20 20   that it will.  
17790 20 20 2a 2a 20 62 65 20 72 65 73 74 6f 72 65 64    ** be restored
17791 20 74 6f 20 69 74 73 20 63 75 72 72 65 6e 74 20   to its current 
17792 76 61 6c 75 65 20 77 68 65 6e 20 74 68 65 20 22  value when the "
17793 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20  ROLLBACK TO sp" 
17794 69 73 20 0a 20 20 20 20 2a 2a 20 65 78 65 63 75  is .    ** execu
17795 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ted..    */.    
17796 69 66 28 20 4e 45 56 45 52 28 0a 20 20 20 20 20  if( NEVER(.     
17797 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b     rc==SQLITE_OK
17798 20 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50   && pPg->pgno>pP
17799 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20  ager->dbSize && 
1779a 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65  subjRequiresPage
1779b 28 70 50 67 29 0a 20 20 20 20 29 20 29 7b 0a 20  (pPg).    ) ){. 
1779c 20 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f 75       rc = subjou
1779d 72 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a 20  rnalPage(pPg);. 
1779e 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 57     }.  .    /* W
1779f 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  rite the content
177a0 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 6f 75  s of the page ou
177a1 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  t to the databas
177a2 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 69  e file. */.    i
177a3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
177a4 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
177a5 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  ( (pPg->flags&PG
177a6 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3d 3d  HDR_NEED_SYNC)==
177a7 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 );.      rc = 
177a8 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65  pager_write_page
177a9 6c 69 73 74 28 70 50 61 67 65 72 2c 20 70 50 67  list(pPager, pPg
177aa 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
177ab 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65  /* Mark the page
177ac 20 61 73 20 63 6c 65 61 6e 2e 20 2a 2f 0a 20 20   as clean. */.  
177ad 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
177ae 4b 20 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52  K ){.    PAGERTR
177af 41 43 45 28 28 22 53 54 52 45 53 53 20 25 64 20  ACE(("STRESS %d 
177b0 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45  page %d\n", PAGE
177b1 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67  RID(pPager), pPg
177b2 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71  ->pgno));.    sq
177b3 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43  lite3PcacheMakeC
177b4 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 0a  lean(pPg);.  }..
177b5 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65    return pager_e
177b6 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29  rror(pPager, rc)
177b7 3b 20 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  ; .}.../*.** All
177b8 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61  ocate and initia
177b9 6c 69 7a 65 20 61 20 6e 65 77 20 50 61 67 65 72  lize a new Pager
177ba 20 6f 62 6a 65 63 74 20 61 6e 64 20 70 75 74 20   object and put 
177bb 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 0a  a pointer to it.
177bc 2a 2a 20 69 6e 20 2a 70 70 50 61 67 65 72 2e 20  ** in *ppPager. 
177bd 54 68 65 20 70 61 67 65 72 20 73 68 6f 75 6c 64  The pager should
177be 20 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 66   eventually be f
177bf 72 65 65 64 20 62 79 20 70 61 73 73 69 6e 67 20  reed by passing 
177c0 69 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  it.** to sqlite3
177c1 50 61 67 65 72 43 6c 6f 73 65 28 29 2e 0a 2a 2a  PagerClose()..**
177c2 0a 2a 2a 20 54 68 65 20 7a 46 69 6c 65 6e 61 6d  .** The zFilenam
177c3 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  e argument is th
177c4 65 20 70 61 74 68 20 74 6f 20 74 68 65 20 64 61  e path to the da
177c5 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f  tabase file to o
177c6 70 65 6e 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65  pen..** If zFile
177c7 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 65  name is NULL the
177c8 6e 20 61 20 72 61 6e 64 6f 6d 6c 79 2d 6e 61 6d  n a randomly-nam
177c9 65 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  ed temporary fil
177ca 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20  e is created.** 
177cb 61 6e 64 20 75 73 65 64 20 61 73 20 74 68 65 20  and used as the 
177cc 66 69 6c 65 20 74 6f 20 62 65 20 63 61 63 68 65  file to be cache
177cd 64 2e 20 54 65 6d 70 6f 72 61 72 79 20 66 69 6c  d. Temporary fil
177ce 65 73 20 61 72 65 20 62 65 20 64 65 6c 65 74 65  es are be delete
177cf 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c  d.** automatical
177d0 6c 79 20 77 68 65 6e 20 74 68 65 79 20 61 72 65  ly when they are
177d1 20 63 6c 6f 73 65 64 2e 20 49 66 20 7a 46 69 6c   closed. If zFil
177d2 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72  ename is ":memor
177d3 79 3a 22 20 74 68 65 6e 20 0a 2a 2a 20 61 6c 6c  y:" then .** all
177d4 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
177d5 68 65 6c 64 20 69 6e 20 63 61 63 68 65 2e 20 49  held in cache. I
177d6 74 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74  t is never writt
177d7 65 6e 20 74 6f 20 64 69 73 6b 2e 20 0a 2a 2a 20  en to disk. .** 
177d8 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  This can be used
177d9 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e   to implement an
177da 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
177db 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  ase..**.** The n
177dc 45 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20  Extra parameter 
177dd 73 70 65 63 69 66 69 65 73 20 74 68 65 20 6e 75  specifies the nu
177de 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
177df 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64   space allocated
177e0 0a 2a 2a 20 61 6c 6f 6e 67 20 77 69 74 68 20 65  .** along with e
177e1 61 63 68 20 70 61 67 65 20 72 65 66 65 72 65 6e  ach page referen
177e2 63 65 2e 20 54 68 69 73 20 73 70 61 63 65 20 69  ce. This space i
177e3 73 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74  s available to t
177e4 68 65 20 75 73 65 72 0a 2a 2a 20 76 69 61 20 74  he user.** via t
177e5 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  he sqlite3PagerG
177e6 65 74 45 78 74 72 61 28 29 20 41 50 49 2e 0a 2a  etExtra() API..*
177e7 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61  *.** The flags a
177e8 72 67 75 6d 65 6e 74 20 69 73 20 75 73 65 64 20  rgument is used 
177e9 74 6f 20 73 70 65 63 69 66 79 20 70 72 6f 70 65  to specify prope
177ea 72 74 69 65 73 20 74 68 61 74 20 61 66 66 65 63  rties that affec
177eb 74 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 69  t the.** operati
177ec 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 72 2e  on of the pager.
177ed 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20 70 61   It should be pa
177ee 73 73 65 64 20 73 6f 6d 65 20 62 69 74 77 69 73  ssed some bitwis
177ef 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 0a 2a 2a  e combination.**
177f0 20 6f 66 20 74 68 65 20 50 41 47 45 52 5f 2a 20   of the PAGER_* 
177f1 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  flags..**.** The
177f2 20 76 66 73 46 6c 61 67 73 20 70 61 72 61 6d 65   vfsFlags parame
177f3 74 65 72 20 69 73 20 61 20 62 69 74 6d 61 73 6b  ter is a bitmask
177f4 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20   to pass to the 
177f5 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 0a  flags parameter.
177f6 2a 2a 20 6f 66 20 74 68 65 20 78 4f 70 65 6e 28  ** of the xOpen(
177f7 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  ) method of the 
177f8 73 75 70 70 6c 69 65 64 20 56 46 53 20 77 68 65  supplied VFS whe
177f9 6e 20 6f 70 65 6e 69 6e 67 20 66 69 6c 65 73 2e  n opening files.
177fa 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70   .**.** If the p
177fb 61 67 65 72 20 6f 62 6a 65 63 74 20 69 73 20 61  ager object is a
177fc 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65  llocated and the
177fd 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 20   specified file 
177fe 6f 70 65 6e 65 64 20 0a 2a 2a 20 73 75 63 63 65  opened .** succe
177ff 73 73 66 75 6c 6c 79 2c 20 53 51 4c 49 54 45 5f  ssfully, SQLITE_
17800 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  OK is returned a
17801 6e 64 20 2a 70 70 50 61 67 65 72 20 73 65 74 20  nd *ppPager set 
17802 74 6f 20 70 6f 69 6e 74 20 74 6f 0a 2a 2a 20 74  to point to.** t
17803 68 65 20 6e 65 77 20 70 61 67 65 72 20 6f 62 6a  he new pager obj
17804 65 63 74 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ect. If an error
17805 20 6f 63 63 75 72 73 2c 20 2a 70 70 50 61 67 65   occurs, *ppPage
17806 72 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c  r is set to NULL
17807 0a 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20 63 6f  .** and error co
17808 64 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69  de returned. Thi
17809 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72  s function may r
1780a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
1780b 45 4d 0a 2a 2a 20 28 73 71 6c 69 74 65 33 4d 61  EM.** (sqlite3Ma
1780c 6c 6c 6f 63 28 29 20 69 73 20 75 73 65 64 20 74  lloc() is used t
1780d 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  o allocate memor
1780e 79 29 2c 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  y), SQLITE_CANTO
1780f 50 45 4e 20 6f 72 20 0a 2a 2a 20 76 61 72 69 6f  PEN or .** vario
17810 75 73 20 53 51 4c 49 54 45 5f 49 4f 5f 58 58 58  us SQLITE_IO_XXX
17811 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49   errors..*/.SQLI
17812 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
17813 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28  qlite3PagerOpen(
17814 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
17815 70 56 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 54  pVfs,       /* T
17816 68 65 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20  he virtual file 
17817 73 79 73 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f  system to use */
17818 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67  .  Pager **ppPag
17819 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  er,         /* O
1781a 55 54 3a 20 52 65 74 75 72 6e 20 74 68 65 20 50  UT: Return the P
1781b 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 68  ager structure h
1781c 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ere */.  const c
1781d 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
1781e 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
1781f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
17820 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  o open */.  int 
17821 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20  nExtra,         
17822 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 62 79       /* Extra by
17823 74 65 73 20 61 70 70 65 6e 64 20 74 6f 20 65 61  tes append to ea
17824 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67  ch in-memory pag
17825 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  e */.  int flags
17826 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
17827 2f 2a 20 66 6c 61 67 73 20 63 6f 6e 74 72 6f 6c  /* flags control
17828 6c 69 6e 67 20 74 68 69 73 20 66 69 6c 65 20 2a  ling this file *
17829 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73  /.  int vfsFlags
1782a 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
1782b 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72  flags passed thr
1782c 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f  ough to sqlite3_
1782d 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20  vfs.xOpen() */. 
1782e 20 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 29   void (*xReinit)
1782f 28 44 62 50 61 67 65 2a 29 20 2f 2a 20 46 75 6e  (DbPage*) /* Fun
17830 63 74 69 6f 6e 20 74 6f 20 72 65 69 6e 69 74 69  ction to reiniti
17831 61 6c 69 7a 65 20 70 61 67 65 73 20 2a 2f 0a 29  alize pages */.)
17832 7b 0a 20 20 75 38 20 2a 70 50 74 72 3b 0a 20 20  {.  u8 *pPtr;.  
17833 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
17834 30 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65  0;       /* Page
17835 72 20 6f 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f  r object to allo
17836 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20  cate and return 
17837 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  */.  int rc = SQ
17838 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a  LITE_OK;      /*
17839 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
1783a 20 20 69 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d    int tempFile =
1783b 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72   0;        /* Tr
1783c 75 65 20 66 6f 72 20 74 65 6d 70 20 66 69 6c 65  ue for temp file
1783d 73 20 28 69 6e 63 6c 2e 20 69 6e 2d 6d 65 6d 6f  s (incl. in-memo
1783e 72 79 20 66 69 6c 65 73 29 20 2a 2f 0a 20 20 69  ry files) */.  i
1783f 6e 74 20 6d 65 6d 44 62 20 3d 20 30 3b 20 20 20  nt memDb = 0;   
17840 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
17841 69 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e  if this is an in
17842 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a  -memory file */.
17843 20 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d    int readOnly =
17844 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72   0;        /* Tr
17845 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20  ue if this is a 
17846 72 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 2a  read-only file *
17847 2f 0a 20 20 69 6e 74 20 6a 6f 75 72 6e 61 6c 46  /.  int journalF
17848 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20  ileSize;     /* 
17849 42 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74  Bytes to allocat
1784a 65 20 66 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e  e for each journ
1784b 61 6c 20 66 64 20 2a 2f 0a 20 20 63 68 61 72 20  al fd */.  char 
1784c 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20  *zPathname = 0; 
1784d 20 20 20 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68      /* Full path
1784e 20 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c   to database fil
1784f 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 68  e */.  int nPath
17850 6e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20  name = 0;       
17851 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
17852 65 73 20 69 6e 20 7a 50 61 74 68 6e 61 6d 65 20  es in zPathname 
17853 2a 2f 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75 72  */.  int useJour
17854 6e 61 6c 20 3d 20 28 66 6c 61 67 73 20 26 20 50  nal = (flags & P
17855 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41  AGER_OMIT_JOURNA
17856 4c 29 3d 3d 30 3b 20 2f 2a 20 46 61 6c 73 65 20  L)==0; /* False 
17857 74 6f 20 6f 6d 69 74 20 6a 6f 75 72 6e 61 6c 20  to omit journal 
17858 2a 2f 0a 20 20 69 6e 74 20 70 63 61 63 68 65 53  */.  int pcacheS
17859 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 50 63 61  ize = sqlite3Pca
1785a 63 68 65 53 69 7a 65 28 29 3b 20 20 20 20 20 20  cheSize();      
1785b 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c   /* Bytes to all
1785c 6f 63 61 74 65 20 66 6f 72 20 50 43 61 63 68 65  ocate for PCache
1785d 20 2a 2f 0a 20 20 75 33 32 20 73 7a 50 61 67 65   */.  u32 szPage
1785e 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 44 45  Dflt = SQLITE_DE
1785f 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b  FAULT_PAGE_SIZE;
17860 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 70 61 67    /* Default pag
17861 65 20 73 69 7a 65 20 2a 2f 0a 20 20 63 6f 6e 73  e size */.  cons
17862 74 20 63 68 61 72 20 2a 7a 55 72 69 20 3d 20 30  t char *zUri = 0
17863 3b 20 20 20 20 2f 2a 20 55 52 49 20 61 72 67 73  ;    /* URI args
17864 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e   to copy */.  in
17865 74 20 6e 55 72 69 20 3d 20 30 3b 20 20 20 20 20  t nUri = 0;     
17866 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
17867 20 6f 66 20 62 79 74 65 73 20 6f 66 20 55 52 49   of bytes of URI
17868 20 61 72 67 73 20 61 74 20 2a 7a 55 72 69 20 2a   args at *zUri *
17869 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  /..  /* Figure o
1786a 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63  ut how much spac
1786b 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f  e is required fo
1786c 72 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66  r each journal f
1786d 69 6c 65 2d 68 61 6e 64 6c 65 0a 20 20 2a 2a 20  ile-handle.  ** 
1786e 28 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 6f  (there are two o
1786f 66 20 74 68 65 6d 2c 20 74 68 65 20 6d 61 69 6e  f them, the main
17870 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65   journal and the
17871 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 29 2e 20 54   sub-journal). T
17872 68 69 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20  his.  ** is the 
17873 6d 61 78 69 6d 75 6d 20 73 70 61 63 65 20 72 65  maximum space re
17874 71 75 69 72 65 64 20 66 6f 72 20 61 6e 20 69 6e  quired for an in
17875 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20  -memory journal 
17876 66 69 6c 65 20 68 61 6e 64 6c 65 20 0a 20 20 2a  file handle .  *
17877 2a 20 61 6e 64 20 61 20 72 65 67 75 6c 61 72 20  * and a regular 
17878 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e  journal file-han
17879 64 6c 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 61  dle. Note that a
1787a 20 22 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61   "regular journa
1787b 6c 2d 68 61 6e 64 6c 65 22 0a 20 20 2a 2a 20 6d  l-handle".  ** m
1787c 61 79 20 62 65 20 61 20 77 72 61 70 70 65 72 20  ay be a wrapper 
1787d 63 61 70 61 62 6c 65 20 6f 66 20 63 61 63 68 69  capable of cachi
1787e 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 6f 72  ng the first por
1787f 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72  tion of the jour
17880 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e  nal.  ** file in
17881 20 6d 65 6d 6f 72 79 20 74 6f 20 69 6d 70 6c 65   memory to imple
17882 6d 65 6e 74 20 74 68 65 20 61 74 6f 6d 69 63 2d  ment the atomic-
17883 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  write optimizati
17884 6f 6e 20 28 73 65 65 20 0a 20 20 2a 2a 20 73 6f  on (see .  ** so
17885 75 72 63 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61  urce file journa
17886 6c 2e 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  l.c)..  */.  if(
17887 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53   sqlite3JournalS
17888 69 7a 65 28 70 56 66 73 29 3e 73 71 6c 69 74 65  ize(pVfs)>sqlite
17889 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  3MemJournalSize(
1788a 29 20 29 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c  ) ){.    journal
1788b 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44  FileSize = ROUND
1788c 38 28 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  8(sqlite3Journal
1788d 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 7d  Size(pVfs));.  }
1788e 65 6c 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61  else{.    journa
1788f 6c 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e  lFileSize = ROUN
17890 44 38 28 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75  D8(sqlite3MemJou
17891 72 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 7d  rnalSize());.  }
17892 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f  ..  /* Set the o
17893 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 74  utput variable t
17894 6f 20 4e 55 4c 4c 20 69 6e 20 63 61 73 65 20 61  o NULL in case a
17895 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20  n error occurs. 
17896 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20  */.  *ppPager = 
17897 30 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  0;..#ifndef SQLI
17898 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42  TE_OMIT_MEMORYDB
17899 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 50  .  if( flags & P
1789a 41 47 45 52 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20  AGER_MEMORY ){. 
1789b 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b 0a 20 20     memDb = 1;.  
1789c 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20    if( zFilename 
1789d 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20  && zFilename[0] 
1789e 29 7b 0a 20 20 20 20 20 20 7a 50 61 74 68 6e 61  ){.      zPathna
1789f 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  me = sqlite3DbSt
178a0 72 44 75 70 28 30 2c 20 7a 46 69 6c 65 6e 61 6d  rDup(0, zFilenam
178a1 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 50  e);.      if( zP
178a2 61 74 68 6e 61 6d 65 3d 3d 30 20 20 29 20 72 65  athname==0  ) re
178a3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
178a4 4d 3b 0a 20 20 20 20 20 20 6e 50 61 74 68 6e 61  M;.      nPathna
178a5 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  me = sqlite3Strl
178a6 65 6e 33 30 28 7a 50 61 74 68 6e 61 6d 65 29 3b  en30(zPathname);
178a7 0a 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65  .      zFilename
178a8 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
178a9 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 43 6f 6d  #endif..  /* Com
178aa 70 75 74 65 20 61 6e 64 20 73 74 6f 72 65 20 74  pute and store t
178ab 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65  he full pathname
178ac 20 69 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64   in an allocated
178ad 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 0a   buffer pointed.
178ae 20 20 2a 2a 20 74 6f 20 62 79 20 7a 50 61 74 68    ** to by zPath
178af 6e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 50 61  name, length nPa
178b0 74 68 6e 61 6d 65 2e 20 4f 72 2c 20 69 66 20 74  thname. Or, if t
178b1 68 69 73 20 69 73 20 61 20 74 65 6d 70 6f 72 61  his is a tempora
178b2 72 79 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 6c 65  ry file,.  ** le
178b3 61 76 65 20 62 6f 74 68 20 6e 50 61 74 68 6e 61  ave both nPathna
178b4 6d 65 20 61 6e 64 20 7a 50 61 74 68 6e 61 6d 65  me and zPathname
178b5 20 73 65 74 20 74 6f 20 30 2e 0a 20 20 2a 2f 0a   set to 0..  */.
178b6 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20    if( zFilename 
178b7 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20  && zFilename[0] 
178b8 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
178b9 72 20 2a 7a 3b 0a 20 20 20 20 6e 50 61 74 68 6e  r *z;.    nPathn
178ba 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61  ame = pVfs->mxPa
178bb 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 7a 50  thname+1;.    zP
178bc 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65  athname = sqlite
178bd 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 30 2c 20  3DbMallocRaw(0, 
178be 6e 50 61 74 68 6e 61 6d 65 2a 32 29 3b 0a 20 20  nPathname*2);.  
178bf 20 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 3d    if( zPathname=
178c0 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
178c1 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
178c2 0a 20 20 20 20 7d 0a 20 20 20 20 7a 50 61 74 68  .    }.    zPath
178c3 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 20 2f 2a 20  name[0] = 0; /* 
178c4 4d 61 6b 65 20 73 75 72 65 20 69 6e 69 74 69 61  Make sure initia
178c5 6c 69 7a 65 64 20 65 76 65 6e 20 69 66 20 46 75  lized even if Fu
178c6 6c 6c 50 61 74 68 6e 61 6d 65 28 29 20 66 61 69  llPathname() fai
178c7 6c 73 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  ls */.    rc = s
178c8 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68  qlite3OsFullPath
178c9 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c 65  name(pVfs, zFile
178ca 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 2c  name, nPathname,
178cb 20 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20   zPathname);.   
178cc 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c   nPathname = sql
178cd 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 50 61  ite3Strlen30(zPa
178ce 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 20 3d  thname);.    z =
178cf 20 7a 55 72 69 20 3d 20 26 7a 46 69 6c 65 6e 61   zUri = &zFilena
178d0 6d 65 5b 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  me[sqlite3Strlen
178d1 33 30 28 7a 46 69 6c 65 6e 61 6d 65 29 2b 31 5d  30(zFilename)+1]
178d2 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 20  ;.    while( *z 
178d3 29 7b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 73 71  ){.      z += sq
178d4 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29  lite3Strlen30(z)
178d5 2b 31 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 73  +1;.      z += s
178d6 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
178d7 29 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e  )+1;.    }.    n
178d8 55 72 69 20 3d 20 28 69 6e 74 29 28 26 7a 5b 31  Uri = (int)(&z[1
178d9 5d 20 2d 20 7a 55 72 69 29 3b 0a 20 20 20 20 61  ] - zUri);.    a
178da 73 73 65 72 74 28 20 6e 55 72 69 3e 3d 30 20 29  ssert( nUri>=0 )
178db 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
178dc 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 50 61 74 68  LITE_OK && nPath
178dd 6e 61 6d 65 2b 38 3e 70 56 66 73 2d 3e 6d 78 50  name+8>pVfs->mxP
178de 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20  athname ){.     
178df 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20   /* This branch 
178e0 69 73 20 74 61 6b 65 6e 20 77 68 65 6e 20 74 68  is taken when th
178e1 65 20 6a 6f 75 72 6e 61 6c 20 70 61 74 68 20 72  e journal path r
178e2 65 71 75 69 72 65 64 20 62 79 0a 20 20 20 20 20  equired by.     
178e3 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65   ** the database
178e4 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 77 69   being opened wi
178e5 6c 6c 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20  ll be more than 
178e6 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65  pVfs->mxPathname
178e7 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 20  .      ** bytes 
178e8 69 6e 20 6c 65 6e 67 74 68 2e 20 54 68 69 73 20  in length. This 
178e9 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61  means the databa
178ea 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70 65  se cannot be ope
178eb 6e 65 64 2c 0a 20 20 20 20 20 20 2a 2a 20 61 73  ned,.      ** as
178ec 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20   it will not be 
178ed 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f 70 65 6e  possible to open
178ee 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
178ef 65 20 6f 72 20 65 76 65 6e 0a 20 20 20 20 20 20  e or even.      
178f0 2a 2a 20 63 68 65 63 6b 20 66 6f 72 20 61 20 68  ** check for a h
178f1 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72  ot-journal befor
178f2 65 20 72 65 61 64 69 6e 67 2e 0a 20 20 20 20 20  e reading..     
178f3 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53   */.      rc = S
178f4 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5f 42  QLITE_CANTOPEN_B
178f5 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  KPT;.    }.    i
178f6 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
178f7 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
178f8 33 44 62 46 72 65 65 28 30 2c 20 7a 50 61 74 68  3DbFree(0, zPath
178f9 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74  name);.      ret
178fa 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
178fb 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
178fc 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 20   memory for the 
178fd 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 2c  Pager structure,
178fe 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2c 20   PCache object, 
178ff 74 68 65 0a 20 20 2a 2a 20 74 68 72 65 65 20 66  the.  ** three f
17900 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c  ile descriptors,
17901 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
17902 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20  le name and the 
17903 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69  journal .  ** fi
17904 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 6c 61 79  le name. The lay
17905 6f 75 74 20 69 6e 20 6d 65 6d 6f 72 79 20 69 73  out in memory is
17906 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a   as follows:.  *
17907 2a 0a 20 20 2a 2a 20 20 20 20 20 50 61 67 65 72  *.  **     Pager
17908 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 20   object         
17909 20 20 20 20 20 20 20 20 20 20 20 28 73 69 7a 65             (size
1790a 6f 66 28 50 61 67 65 72 29 20 62 79 74 65 73 29  of(Pager) bytes)
1790b 0a 20 20 2a 2a 20 20 20 20 20 50 43 61 63 68 65  .  **     PCache
1790c 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 20   object         
1790d 20 20 20 20 20 20 20 20 20 20 28 73 71 6c 69 74            (sqlit
1790e 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 20 62  e3PcacheSize() b
1790f 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 44  ytes).  **     D
17910 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 6e  atabase file han
17911 64 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 28  dle            (
17912 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 62  pVfs->szOsFile b
17913 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 53  ytes).  **     S
17914 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ub-journal file 
17915 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20 28  handle         (
17916 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20  journalFileSize 
17917 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20  bytes).  **     
17918 4d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  Main journal fil
17919 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20  e handle        
1791a 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65  (journalFileSize
1791b 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20   bytes).  **    
1791c 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e   Database file n
1791d 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ame             
1791e 20 28 6e 50 61 74 68 6e 61 6d 65 2b 31 20 62 79   (nPathname+1 by
1791f 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 4a 6f  tes).  **     Jo
17920 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
17921 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6e                (n
17922 50 61 74 68 6e 61 6d 65 2b 38 2b 31 20 62 79 74  Pathname+8+1 byt
17923 65 73 29 0a 20 20 2a 2f 0a 20 20 70 50 74 72 20  es).  */.  pPtr 
17924 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 4d  = (u8 *)sqlite3M
17925 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20 20 52  allocZero(.    R
17926 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 50  OUND8(sizeof(*pP
17927 61 67 65 72 29 29 20 2b 20 20 20 20 20 20 2f 2a  ager)) +      /*
17928 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65   Pager structure
17929 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 28 70   */.    ROUND8(p
1792a 63 61 63 68 65 53 69 7a 65 29 20 2b 20 20 20 20  cacheSize) +    
1792b 20 20 20 20 20 20 20 2f 2a 20 50 43 61 63 68 65         /* PCache
1792c 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 52   object */.    R
1792d 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73  OUND8(pVfs->szOs
1792e 46 69 6c 65 29 20 2b 20 20 20 20 20 20 20 2f 2a  File) +       /*
1792f 20 54 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c   The main db fil
17930 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e 61 6c  e */.    journal
17931 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b 20 20  FileSize * 2 +  
17932 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
17933 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73  wo journal files
17934 20 2a 2f 20 0a 20 20 20 20 6e 50 61 74 68 6e 61   */ .    nPathna
17935 6d 65 20 2b 20 31 20 2b 20 6e 55 72 69 20 2b 20  me + 1 + nUri + 
17936 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65          /* zFile
17937 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6e 50 61 74  name */.    nPat
17938 68 6e 61 6d 65 20 2b 20 38 20 2b 20 32 20 20 20  hname + 8 + 2   
17939 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4a             /* zJ
1793a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 69 66 6e 64 65  ournal */.#ifnde
1793b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41  f SQLITE_OMIT_WA
1793c 4c 0a 20 20 20 20 2b 20 6e 50 61 74 68 6e 61 6d  L.    + nPathnam
1793d 65 20 2b 20 34 20 2b 20 32 20 20 20 20 20 20 20  e + 4 + 2       
1793e 20 20 20 20 20 2f 2a 20 7a 57 61 6c 20 2a 2f 0a       /* zWal */.
1793f 23 65 6e 64 69 66 0a 20 20 29 3b 0a 20 20 61 73  #endif.  );.  as
17940 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45  sert( EIGHT_BYTE
17941 5f 41 4c 49 47 4e 4d 45 4e 54 28 53 51 4c 49 54  _ALIGNMENT(SQLIT
17942 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 6a 6f 75  E_INT_TO_PTR(jou
17943 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 29 20 29  rnalFileSize)) )
17944 3b 0a 20 20 69 66 28 20 21 70 50 74 72 20 29 7b  ;.  if( !pPtr ){
17945 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
17946 65 65 28 30 2c 20 7a 50 61 74 68 6e 61 6d 65 29  ee(0, zPathname)
17947 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
17948 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
17949 20 70 50 61 67 65 72 20 3d 20 20 20 20 20 20 20   pPager =       
1794a 20 20 20 20 20 20 20 28 50 61 67 65 72 2a 29 28         (Pager*)(
1794b 70 50 74 72 29 3b 0a 20 20 70 50 61 67 65 72 2d  pPtr);.  pPager-
1794c 3e 70 50 43 61 63 68 65 20 3d 20 20 20 20 28 50  >pPCache =    (P
1794d 43 61 63 68 65 2a 29 28 70 50 74 72 20 2b 3d 20  Cache*)(pPtr += 
1794e 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70  ROUND8(sizeof(*p
1794f 50 61 67 65 72 29 29 29 3b 0a 20 20 70 50 61 67  Pager)));.  pPag
17950 65 72 2d 3e 66 64 20 3d 20 20 20 28 73 71 6c 69  er->fd =   (sqli
17951 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20  te3_file*)(pPtr 
17952 2b 3d 20 52 4f 55 4e 44 38 28 70 63 61 63 68 65  += ROUND8(pcache
17953 53 69 7a 65 29 29 3b 0a 20 20 70 50 61 67 65 72  Size));.  pPager
17954 2d 3e 73 6a 66 64 20 3d 20 28 73 71 6c 69 74 65  ->sjfd = (sqlite
17955 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d  3_file*)(pPtr +=
17956 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a   ROUND8(pVfs->sz
17957 4f 73 46 69 6c 65 29 29 3b 0a 20 20 70 50 61 67  OsFile));.  pPag
17958 65 72 2d 3e 6a 66 64 20 3d 20 20 28 73 71 6c 69  er->jfd =  (sqli
17959 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20  te3_file*)(pPtr 
1795a 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69  += journalFileSi
1795b 7a 65 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a  ze);.  pPager->z
1795c 46 69 6c 65 6e 61 6d 65 20 3d 20 20 20 20 28 63  Filename =    (c
1795d 68 61 72 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f  har*)(pPtr += jo
1795e 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a  urnalFileSize);.
1795f 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f    assert( EIGHT_
17960 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70  BYTE_ALIGNMENT(p
17961 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a  Pager->jfd) );..
17962 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65    /* Fill in the
17963 20 50 61 67 65 72 2e 7a 46 69 6c 65 6e 61 6d 65   Pager.zFilename
17964 20 61 6e 64 20 50 61 67 65 72 2e 7a 4a 6f 75 72   and Pager.zJour
17965 6e 61 6c 20 62 75 66 66 65 72 73 2c 20 69 66 20  nal buffers, if 
17966 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69  required. */.  i
17967 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b 0a  f( zPathname ){.
17968 20 20 20 20 61 73 73 65 72 74 28 20 6e 50 61 74      assert( nPat
17969 68 6e 61 6d 65 3e 30 20 29 3b 0a 20 20 20 20 70  hname>0 );.    p
1796a 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20  Pager->zJournal 
1796b 3d 20 20 20 28 63 68 61 72 2a 29 28 70 50 74 72  =   (char*)(pPtr
1796c 20 2b 3d 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20   += nPathname + 
1796d 31 20 2b 20 6e 55 72 69 29 3b 0a 20 20 20 20 6d  1 + nUri);.    m
1796e 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a 46  emcpy(pPager->zF
1796f 69 6c 65 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61  ilename, zPathna
17970 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a  me, nPathname);.
17971 20 20 20 20 69 66 28 20 6e 55 72 69 20 29 20 6d      if( nUri ) m
17972 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 7a  emcpy(&pPager->z
17973 46 69 6c 65 6e 61 6d 65 5b 6e 50 61 74 68 6e 61  Filename[nPathna
17974 6d 65 2b 31 5d 2c 20 7a 55 72 69 2c 20 6e 55 72  me+1], zUri, nUr
17975 69 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70  i);.    memcpy(p
17976 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c  Pager->zJournal,
17977 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74   zPathname, nPat
17978 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63  hname);.    memc
17979 70 79 28 26 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  py(&pPager->zJou
1797a 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65 5d 2c  rnal[nPathname],
1797b 20 22 2d 6a 6f 75 72 6e 61 6c 5c 30 30 30 22 2c   "-journal\000",
1797c 20 38 2b 32 29 3b 0a 20 20 20 20 73 71 6c 69 74   8+2);.    sqlit
1797d 65 33 46 69 6c 65 53 75 66 66 69 78 33 28 70 50  e3FileSuffix3(pP
1797e 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c  ager->zFilename,
1797f 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61   pPager->zJourna
17980 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  l);.#ifndef SQLI
17981 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 20 20 20 20  TE_OMIT_WAL.    
17982 70 50 61 67 65 72 2d 3e 7a 57 61 6c 20 3d 20 26  pPager->zWal = &
17983 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
17984 5b 6e 50 61 74 68 6e 61 6d 65 2b 38 2b 31 5d 3b  [nPathname+8+1];
17985 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67  .    memcpy(pPag
17986 65 72 2d 3e 7a 57 61 6c 2c 20 7a 50 61 74 68 6e  er->zWal, zPathn
17987 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b  ame, nPathname);
17988 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61  .    memcpy(&pPa
17989 67 65 72 2d 3e 7a 57 61 6c 5b 6e 50 61 74 68 6e  ger->zWal[nPathn
1798a 61 6d 65 5d 2c 20 22 2d 77 61 6c 5c 30 30 30 22  ame], "-wal\000"
1798b 2c 20 34 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69  , 4+1);.    sqli
1798c 74 65 33 46 69 6c 65 53 75 66 66 69 78 33 28 70  te3FileSuffix3(p
1798d 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65  Pager->zFilename
1798e 2c 20 70 50 61 67 65 72 2d 3e 7a 57 61 6c 29 3b  , pPager->zWal);
1798f 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69  .#endif.    sqli
17990 74 65 33 44 62 46 72 65 65 28 30 2c 20 7a 50 61  te3DbFree(0, zPa
17991 74 68 6e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70  thname);.  }.  p
17992 50 61 67 65 72 2d 3e 70 56 66 73 20 3d 20 70 56  Pager->pVfs = pV
17993 66 73 3b 0a 20 20 70 50 61 67 65 72 2d 3e 76 66  fs;.  pPager->vf
17994 73 46 6c 61 67 73 20 3d 20 76 66 73 46 6c 61 67  sFlags = vfsFlag
17995 73 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68  s;..  /* Open th
17996 65 20 70 61 67 65 72 20 66 69 6c 65 2e 0a 20 20  e pager file..  
17997 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61  */.  if( zFilena
17998 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b  me && zFilename[
17999 30 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 6f  0] ){.    int fo
1799a 75 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ut = 0;         
1799b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46             /* VF
1799c 53 20 66 6c 61 67 73 20 72 65 74 75 72 6e 65 64  S flags returned
1799d 20 62 79 20 78 4f 70 65 6e 28 29 20 2a 2f 0a 20   by xOpen() */. 
1799e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1799f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67  sOpen(pVfs, pPag
179a0 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 70  er->zFilename, p
179a1 50 61 67 65 72 2d 3e 66 64 2c 20 76 66 73 46 6c  Pager->fd, vfsFl
179a2 61 67 73 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20  ags, &fout);.   
179a3 20 61 73 73 65 72 74 28 20 21 6d 65 6d 44 62 20   assert( !memDb 
179a4 29 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79 20  );.    readOnly 
179a5 3d 20 28 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f  = (fout&SQLITE_O
179a6 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 0a  PEN_READONLY);..
179a7 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69      /* If the fi
179a8 6c 65 20 77 61 73 20 73 75 63 63 65 73 73 66 75  le was successfu
179a9 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 20 72  lly opened for r
179aa 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73  ead/write access
179ab 2c 0a 20 20 20 20 2a 2a 20 63 68 6f 6f 73 65 20  ,.    ** choose 
179ac 61 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73  a default page s
179ad 69 7a 65 20 69 6e 20 63 61 73 65 20 77 65 20 68  ize in case we h
179ae 61 76 65 20 74 6f 20 63 72 65 61 74 65 20 74 68  ave to create th
179af 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73  e.    ** databas
179b0 65 20 66 69 6c 65 2e 20 54 68 65 20 64 65 66 61  e file. The defa
179b1 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 69 73  ult page size is
179b2 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 3a   the maximum of:
179b3 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
179b4 20 20 2b 20 53 51 4c 49 54 45 5f 44 45 46 41 55    + SQLITE_DEFAU
179b5 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20  LT_PAGE_SIZE,.  
179b6 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76 61    **    + The va
179b7 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
179b8 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53  sqlite3OsSectorS
179b9 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20 20 20 20  ize().    **    
179ba 2b 20 54 68 65 20 6c 61 72 67 65 73 74 20 70 61  + The largest pa
179bb 67 65 20 73 69 7a 65 20 74 68 61 74 20 63 61 6e  ge size that can
179bc 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d   be written atom
179bd 69 63 61 6c 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20  ically..    */. 
179be 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
179bf 45 5f 4f 4b 20 26 26 20 21 72 65 61 64 4f 6e 6c  E_OK && !readOnl
179c0 79 20 29 7b 0a 20 20 20 20 20 20 73 65 74 53 65  y ){.      setSe
179c1 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29  ctorSize(pPager)
179c2 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 53  ;.      assert(S
179c3 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41  QLITE_DEFAULT_PA
179c4 47 45 5f 53 49 5a 45 3c 3d 53 51 4c 49 54 45 5f  GE_SIZE<=SQLITE_
179c5 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45  MAX_DEFAULT_PAGE
179c6 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 69 66  _SIZE);.      if
179c7 28 20 73 7a 50 61 67 65 44 66 6c 74 3c 70 50 61  ( szPageDflt<pPa
179c8 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20  ger->sectorSize 
179c9 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
179ca 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a  Pager->sectorSiz
179cb 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46  e>SQLITE_MAX_DEF
179cc 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 29  AULT_PAGE_SIZE )
179cd 7b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50 61  {.          szPa
179ce 67 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f  geDflt = SQLITE_
179cf 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45  MAX_DEFAULT_PAGE
179d0 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 7d  _SIZE;.        }
179d1 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
179d2 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 28 75 33  szPageDflt = (u3
179d3 32 29 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72  2)pPager->sector
179d4 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Size;.        }.
179d5 20 20 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53        }.#ifdef S
179d6 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f  QLITE_ENABLE_ATO
179d7 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 20 20  MIC_WRITE.      
179d8 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 44  {.        int iD
179d9 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76  c = sqlite3OsDev
179da 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
179db 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a  cs(pPager->fd);.
179dc 20 20 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a          int ii;.
179dd 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 53          assert(S
179de 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
179df 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29  IC512==(512>>8))
179e0 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
179e1 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54  (SQLITE_IOCAP_AT
179e2 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e  OMIC64K==(65536>
179e3 3e 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73  >8));.        as
179e4 73 65 72 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f  sert(SQLITE_MAX_
179e5 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
179e6 45 3c 3d 36 35 35 33 36 29 3b 0a 20 20 20 20 20  E<=65536);.     
179e7 20 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67 65     for(ii=szPage
179e8 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54 45  Dflt; ii<=SQLITE
179e9 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47  _MAX_DEFAULT_PAG
179ea 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32 29  E_SIZE; ii=ii*2)
179eb 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
179ec 69 44 63 26 28 53 51 4c 49 54 45 5f 49 4f 43 41  iDc&(SQLITE_IOCA
179ed 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e 38 29  P_ATOMIC|(ii>>8)
179ee 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
179ef 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 69 69   szPageDflt = ii
179f0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
179f1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
179f2 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d  #endif.    }.  }
179f3 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20  else{.    /* If 
179f4 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
179f5 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 69   is requested, i
179f6 74 20 69 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20  t is not opened 
179f7 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20  immediately..   
179f8 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65   ** In this case
179f9 20 77 65 20 61 63 63 65 70 74 20 74 68 65 20 64   we accept the d
179fa 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65  efault page size
179fb 20 61 6e 64 20 64 65 6c 61 79 20 61 63 74 75 61   and delay actua
179fc 6c 6c 79 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69  lly.    ** openi
179fd 6e 67 20 74 68 65 20 66 69 6c 65 20 75 6e 74 69  ng the file unti
179fe 6c 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c  l the first call
179ff 20 74 6f 20 4f 73 57 72 69 74 65 28 29 2e 0a 20   to OsWrite().. 
17a00 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69     **.    ** Thi
17a01 73 20 62 72 61 6e 63 68 20 69 73 20 61 6c 73 6f  s branch is also
17a02 20 72 75 6e 20 66 6f 72 20 61 6e 20 69 6e 2d 6d   run for an in-m
17a03 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e 20  emory database. 
17a04 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 20  An in-memory.   
17a05 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20   ** database is 
17a06 74 68 65 20 73 61 6d 65 20 61 73 20 61 20 74 65  the same as a te
17a07 6d 70 2d 66 69 6c 65 20 74 68 61 74 20 69 73 20  mp-file that is 
17a08 6e 65 76 65 72 20 77 72 69 74 74 65 6e 20 6f 75  never written ou
17a09 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69 73 6b  t to.    ** disk
17a0a 20 61 6e 64 20 75 73 65 73 20 61 6e 20 69 6e 2d   and uses an in-
17a0b 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20  memory rollback 
17a0c 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 20  journal..    */ 
17a0d 0a 20 20 20 20 74 65 6d 70 46 69 6c 65 20 3d 20  .    tempFile = 
17a0e 31 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65  1;.    pPager->e
17a0f 53 74 61 74 65 20 3d 20 50 41 47 45 52 5f 52 45  State = PAGER_RE
17a10 41 44 45 52 3b 0a 20 20 20 20 70 50 61 67 65 72  ADER;.    pPager
17a11 2d 3e 65 4c 6f 63 6b 20 3d 20 45 58 43 4c 55 53  ->eLock = EXCLUS
17a12 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65  IVE_LOCK;.    re
17a13 61 64 4f 6e 6c 79 20 3d 20 28 76 66 73 46 6c 61  adOnly = (vfsFla
17a14 67 73 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  gs&SQLITE_OPEN_R
17a15 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 7d 0a 0a 20  EADONLY);.  }.. 
17a16 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
17a17 67 20 63 61 6c 6c 20 74 6f 20 50 61 67 65 72 53  g call to PagerS
17a18 65 74 50 61 67 65 73 69 7a 65 28 29 20 73 65 72  etPagesize() ser
17a19 76 65 73 20 74 6f 20 73 65 74 20 74 68 65 20 76  ves to set the v
17a1a 61 6c 75 65 20 6f 66 20 0a 20 20 2a 2a 20 50 61  alue of .  ** Pa
17a1b 67 65 72 2e 70 61 67 65 53 69 7a 65 20 61 6e 64  ger.pageSize and
17a1c 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65   to allocate the
17a1d 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65   Pager.pTmpSpace
17a1e 20 62 75 66 66 65 72 2e 0a 20 20 2a 2f 0a 20 20   buffer..  */.  
17a1f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
17a20 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  K ){.    assert(
17a21 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d   pPager->memDb==
17a22 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  0 );.    rc = sq
17a23 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67  lite3PagerSetPag
17a24 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26 73  esize(pPager, &s
17a25 7a 50 61 67 65 44 66 6c 74 2c 20 2d 31 29 3b 0a  zPageDflt, -1);.
17a26 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63      testcase( rc
17a27 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  !=SQLITE_OK );. 
17a28 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65   }..  /* If an e
17a29 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 69 6e  rror occurred in
17a2a 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 62   either of the b
17a2b 6c 6f 63 6b 73 20 61 62 6f 76 65 2c 20 66 72 65  locks above, fre
17a2c 65 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65  e the .  ** Page
17a2d 72 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  r structure and 
17a2e 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a  close the file..
17a2f 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53    */.  if( rc!=S
17a30 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17a31 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d  assert( !pPager-
17a32 3e 70 54 6d 70 53 70 61 63 65 20 29 3b 0a 20 20  >pTmpSpace );.  
17a33 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
17a34 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20  (pPager->fd);.  
17a35 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
17a36 50 61 67 65 72 29 3b 0a 20 20 20 20 72 65 74 75  Pager);.    retu
17a37 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rn rc;.  }..  /*
17a38 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
17a39 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 2a  PCache object. *
17a3a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e 45 78 74  /.  assert( nExt
17a3b 72 61 3c 31 30 30 30 20 29 3b 0a 20 20 6e 45 78  ra<1000 );.  nEx
17a3c 74 72 61 20 3d 20 52 4f 55 4e 44 38 28 6e 45 78  tra = ROUND8(nEx
17a3d 74 72 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 50  tra);.  sqlite3P
17a3e 63 61 63 68 65 4f 70 65 6e 28 73 7a 50 61 67 65  cacheOpen(szPage
17a3f 44 66 6c 74 2c 20 6e 45 78 74 72 61 2c 20 21 6d  Dflt, nExtra, !m
17a40 65 6d 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20  emDb,.          
17a41 20 20 20 20 20 20 20 20 20 20 21 6d 65 6d 44 62            !memDb
17a42 3f 70 61 67 65 72 53 74 72 65 73 73 3a 30 2c 20  ?pagerStress:0, 
17a43 28 76 6f 69 64 20 2a 29 70 50 61 67 65 72 2c 20  (void *)pPager, 
17a44 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
17a45 3b 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28  ;..  PAGERTRACE(
17a46 28 22 4f 50 45 4e 20 25 64 20 25 73 5c 6e 22 2c  ("OPEN %d %s\n",
17a47 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 70 50   FILEHANDLEID(pP
17a48 61 67 65 72 2d 3e 66 64 29 2c 20 70 50 61 67 65  ager->fd), pPage
17a49 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 3b 0a  r->zFilename));.
17a4a 20 20 49 4f 54 52 41 43 45 28 28 22 4f 50 45 4e    IOTRACE(("OPEN
17a4b 20 25 70 20 25 73 5c 6e 22 2c 20 70 50 61 67 65   %p %s\n", pPage
17a4c 72 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65  r, pPager->zFile
17a4d 6e 61 6d 65 29 29 0a 0a 20 20 70 50 61 67 65 72  name))..  pPager
17a4e 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28  ->useJournal = (
17a4f 75 38 29 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20  u8)useJournal;. 
17a50 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74   /* pPager->stmt
17a51 4f 70 65 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f  Open = 0; */.  /
17a52 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e  * pPager->stmtIn
17a53 55 73 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a  Use = 0; */.  /*
17a54 20 70 50 61 67 65 72 2d 3e 6e 52 65 66 20 3d 20   pPager->nRef = 
17a55 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65  0; */.  /* pPage
17a56 72 2d 3e 73 74 6d 74 53 69 7a 65 20 3d 20 30 3b  r->stmtSize = 0;
17a57 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d   */.  /* pPager-
17a58 3e 73 74 6d 74 4a 53 69 7a 65 20 3d 20 30 3b 20  >stmtJSize = 0; 
17a59 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  */.  /* pPager->
17a5a 6e 50 61 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 20  nPage = 0; */.  
17a5b 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d  pPager->mxPgno =
17a5c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45   SQLITE_MAX_PAGE
17a5d 5f 43 4f 55 4e 54 3b 0a 20 20 2f 2a 20 70 50 61  _COUNT;.  /* pPa
17a5e 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47  ger->state = PAG
17a5f 45 52 5f 55 4e 4c 4f 43 4b 3b 20 2a 2f 0a 23 69  ER_UNLOCK; */.#i
17a60 66 20 30 0a 20 20 61 73 73 65 72 74 28 20 70 50  f 0.  assert( pP
17a61 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d 20 28  ager->state == (
17a62 74 65 6d 70 46 69 6c 65 20 3f 20 50 41 47 45 52  tempFile ? PAGER
17a63 5f 45 58 43 4c 55 53 49 56 45 20 3a 20 50 41 47  _EXCLUSIVE : PAG
17a64 45 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a 23 65  ER_UNLOCK) );.#e
17a65 6e 64 69 66 0a 20 20 2f 2a 20 70 50 61 67 65 72  ndif.  /* pPager
17a66 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b 20 2a  ->errMask = 0; *
17a67 2f 0a 20 20 70 50 61 67 65 72 2d 3e 74 65 6d 70  /.  pPager->temp
17a68 46 69 6c 65 20 3d 20 28 75 38 29 74 65 6d 70 46  File = (u8)tempF
17a69 69 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 74  ile;.  assert( t
17a6a 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c  empFile==PAGER_L
17a6b 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41  OCKINGMODE_NORMA
17a6c 4c 20 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20  L .          || 
17a6d 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f  tempFile==PAGER_
17a6e 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c  LOCKINGMODE_EXCL
17a6f 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72  USIVE );.  asser
17a70 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  t( PAGER_LOCKING
17a71 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3d 3d  MODE_EXCLUSIVE==
17a72 31 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65  1 );.  pPager->e
17a73 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28  xclusiveMode = (
17a74 75 38 29 74 65 6d 70 46 69 6c 65 3b 20 0a 20 20  u8)tempFile; .  
17a75 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
17a76 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 72  untDone = pPager
17a77 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50  ->tempFile;.  pP
17a78 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 28 75  ager->memDb = (u
17a79 38 29 6d 65 6d 44 62 3b 0a 20 20 70 50 61 67 65  8)memDb;.  pPage
17a7a 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 28 75  r->readOnly = (u
17a7b 38 29 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 61 73  8)readOnly;.  as
17a7c 73 65 72 74 28 20 75 73 65 4a 6f 75 72 6e 61 6c  sert( useJournal
17a7d 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70   || pPager->temp
17a7e 46 69 6c 65 20 29 3b 0a 20 20 70 50 61 67 65 72  File );.  pPager
17a7f 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 50 61 67 65  ->noSync = pPage
17a80 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 69  r->tempFile;.  i
17a81 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  f( pPager->noSyn
17a82 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  c ){.    assert(
17a83 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e   pPager->fullSyn
17a84 63 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  c==0 );.    asse
17a85 72 74 28 20 70 50 61 67 65 72 2d 3e 73 79 6e 63  rt( pPager->sync
17a86 46 6c 61 67 73 3d 3d 30 20 29 3b 0a 20 20 20 20  Flags==0 );.    
17a87 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
17a88 77 61 6c 53 79 6e 63 46 6c 61 67 73 3d 3d 30 20  walSyncFlags==0 
17a89 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
17a8a 50 61 67 65 72 2d 3e 63 6b 70 74 53 79 6e 63 46  Pager->ckptSyncF
17a8b 6c 61 67 73 3d 3d 30 20 29 3b 0a 20 20 7d 65 6c  lags==0 );.  }el
17a8c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  se{.    pPager->
17a8d 66 75 6c 6c 53 79 6e 63 20 3d 20 31 3b 0a 20 20  fullSync = 1;.  
17a8e 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c    pPager->syncFl
17a8f 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e  ags = SQLITE_SYN
17a90 43 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 20 20 70 50  C_NORMAL;.    pP
17a91 61 67 65 72 2d 3e 77 61 6c 53 79 6e 63 46 6c 61  ager->walSyncFla
17a92 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43  gs = SQLITE_SYNC
17a93 5f 4e 4f 52 4d 41 4c 20 7c 20 57 41 4c 5f 53 59  _NORMAL | WAL_SY
17a94 4e 43 5f 54 52 41 4e 53 41 43 54 49 4f 4e 53 3b  NC_TRANSACTIONS;
17a95 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 6b 70  .    pPager->ckp
17a96 74 53 79 6e 63 46 6c 61 67 73 20 3d 20 53 51 4c  tSyncFlags = SQL
17a97 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 3b  ITE_SYNC_NORMAL;
17a98 0a 20 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 72  .  }.  /* pPager
17a99 2d 3e 70 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f  ->pFirst = 0; */
17a9a 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46  .  /* pPager->pF
17a9b 69 72 73 74 53 79 6e 63 65 64 20 3d 20 30 3b 20  irstSynced = 0; 
17a9c 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  */.  /* pPager->
17a9d 70 4c 61 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20  pLast = 0; */.  
17a9e 70 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d  pPager->nExtra =
17a9f 20 28 75 31 36 29 6e 45 78 74 72 61 3b 0a 20 20   (u16)nExtra;.  
17aa0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53  pPager->journalS
17aa1 69 7a 65 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54  izeLimit = SQLIT
17aa2 45 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41  E_DEFAULT_JOURNA
17aa3 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 20  L_SIZE_LIMIT;.  
17aa4 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
17aa5 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 74 65  Pager->fd) || te
17aa6 6d 70 46 69 6c 65 20 29 3b 0a 20 20 73 65 74 53  mpFile );.  setS
17aa7 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72  ectorSize(pPager
17aa8 29 3b 0a 20 20 69 66 28 20 21 75 73 65 4a 6f 75  );.  if( !useJou
17aa9 72 6e 61 6c 20 29 7b 0a 20 20 20 20 70 50 61 67  rnal ){.    pPag
17aaa 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20  er->journalMode 
17aab 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  = PAGER_JOURNALM
17aac 4f 44 45 5f 4f 46 46 3b 0a 20 20 7d 65 6c 73 65  ODE_OFF;.  }else
17aad 20 69 66 28 20 6d 65 6d 44 62 20 29 7b 0a 20 20   if( memDb ){.  
17aae 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
17aaf 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f  lMode = PAGER_JO
17ab0 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59  URNALMODE_MEMORY
17ab1 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65  ;.  }.  /* pPage
17ab2 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 20  r->xBusyHandler 
17ab3 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61  = 0; */.  /* pPa
17ab4 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65  ger->pBusyHandle
17ab5 72 41 72 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 70  rArg = 0; */.  p
17ab6 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 72  Pager->xReiniter
17ab7 20 3d 20 78 52 65 69 6e 69 74 3b 0a 20 20 2f 2a   = xReinit;.  /*
17ab8 20 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e   memset(pPager->
17ab9 61 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66  aHash, 0, sizeof
17aba 28 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29 29  (pPager->aHash))
17abb 3b 20 2a 2f 0a 0a 20 20 2a 70 70 50 61 67 65 72  ; */..  *ppPager
17abc 20 3d 20 70 50 61 67 65 72 3b 0a 20 20 72 65 74   = pPager;.  ret
17abd 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
17abe 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  ..../*.** This f
17abf 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
17ac0 64 20 61 66 74 65 72 20 74 72 61 6e 73 69 74 69  d after transiti
17ac1 6f 6e 69 6e 67 20 66 72 6f 6d 20 50 41 47 45 52  oning from PAGER
17ac2 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a 20 50 41  _UNLOCK to.** PA
17ac3 47 45 52 5f 53 48 41 52 45 44 20 73 74 61 74 65  GER_SHARED state
17ac4 2e 20 49 74 20 74 65 73 74 73 20 69 66 20 74 68  . It tests if th
17ac5 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a 6f 75  ere is a hot jou
17ac6 72 6e 61 6c 20 70 72 65 73 65 6e 74 20 69 6e 0a  rnal present in.
17ac7 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  ** the file-syst
17ac8 65 6d 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e  em for the given
17ac9 20 70 61 67 65 72 2e 20 41 20 68 6f 74 20 6a 6f   pager. A hot jo
17aca 75 72 6e 61 6c 20 69 73 20 6f 6e 65 20 74 68 61  urnal is one tha
17acb 74 20 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62  t .** needs to b
17acc 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 41  e played back. A
17acd 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73  ccording to this
17ace 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 68 6f 74   function, a hot
17acf 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65  -journal.** file
17ad0 20 65 78 69 73 74 73 20 69 66 20 74 68 65 20 66   exists if the f
17ad1 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69  ollowing criteri
17ad2 61 20 61 72 65 20 6d 65 74 3a 0a 2a 2a 0a 2a 2a  a are met:.**.**
17ad3 20 20 20 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c     * The journal
17ad4 20 66 69 6c 65 20 65 78 69 73 74 73 20 69 6e 20   file exists in 
17ad5 74 68 65 20 66 69 6c 65 20 73 79 73 74 65 6d 2c  the file system,
17ad6 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 4e 6f 20 70   and.**   * No p
17ad7 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 61 20 52  rocess holds a R
17ad8 45 53 45 52 56 45 44 20 6f 72 20 67 72 65 61 74  ESERVED or great
17ad9 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  er lock on the d
17ada 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 61 6e  atabase file, an
17adb 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74  d.**   * The dat
17adc 61 62 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c  abase file itsel
17add 66 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  f is greater tha
17ade 6e 20 30 20 62 79 74 65 73 20 69 6e 20 73 69 7a  n 0 bytes in siz
17adf 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68  e, and.**   * Th
17ae0 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20  e first byte of 
17ae1 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
17ae2 20 65 78 69 73 74 73 20 61 6e 64 20 69 73 20 6e   exists and is n
17ae3 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 49  ot 0x00..**.** I
17ae4 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69  f the current si
17ae5 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
17ae6 73 65 20 66 69 6c 65 20 69 73 20 30 20 62 75 74  se file is 0 but
17ae7 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a   a journal file.
17ae8 2a 2a 20 65 78 69 73 74 73 2c 20 74 68 61 74 20  ** exists, that 
17ae9 69 73 20 70 72 6f 62 61 62 6c 79 20 61 6e 20 6f  is probably an o
17aea 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20  ld journal left 
17aeb 6f 76 65 72 20 66 72 6f 6d 20 61 20 70 72 69 6f  over from a prio
17aec 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 69  r.** database wi
17aed 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  th the same name
17aee 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
17aef 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
17af0 69 73 0a 2a 2a 20 6a 75 73 74 20 64 65 6c 65 74  is.** just delet
17af1 65 64 20 75 73 69 6e 67 20 4f 73 44 65 6c 65 74  ed using OsDelet
17af2 65 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73  e, *pExists is s
17af3 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49  et to 0 and SQLI
17af4 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75  TE_OK.** is retu
17af5 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rned..**.** This
17af6 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f   routine does no
17af7 74 20 63 68 65 63 6b 20 69 66 20 74 68 65 72 65  t check if there
17af8 20 69 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75   is a master jou
17af9 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a  rnal filename.**
17afa 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
17afb 68 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72  he file. If ther
17afc 65 20 69 73 2c 20 61 6e 64 20 74 68 61 74 20 6d  e is, and that m
17afd 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
17afe 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65  le.** does not e
17aff 78 69 73 74 2c 20 74 68 65 6e 20 74 68 65 20 6a  xist, then the j
17b00 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e  ournal file is n
17b01 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 2e 20 49  ot really hot. I
17b02 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 65 20 74  n this.** case t
17b03 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
17b04 20 72 65 74 75 72 6e 20 61 20 66 61 6c 73 65 2d   return a false-
17b05 70 6f 73 69 74 69 76 65 2e 20 54 68 65 20 70 61  positive. The pa
17b06 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 0a 2a  ger_playback().*
17b07 2a 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64  * routine will d
17b08 69 73 63 6f 76 65 72 20 74 68 61 74 20 74 68 65  iscover that the
17b09 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
17b0a 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 20   not really hot 
17b0b 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74  and .** will not
17b0c 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 0a   roll it back. .
17b0d 2a 2a 0a 2a 2a 20 49 66 20 61 20 68 6f 74 2d 6a  **.** If a hot-j
17b0e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 66  ournal file is f
17b0f 6f 75 6e 64 20 74 6f 20 65 78 69 73 74 2c 20 2a  ound to exist, *
17b10 70 45 78 69 73 74 73 20 69 73 20 73 65 74 20 74  pExists is set t
17b11 6f 20 31 20 61 6e 64 20 0a 2a 2a 20 53 51 4c 49  o 1 and .** SQLI
17b12 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20  TE_OK returned. 
17b13 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61  If no hot-journa
17b14 6c 20 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e  l file is presen
17b15 74 2c 20 2a 70 45 78 69 73 74 73 20 69 73 0a 2a  t, *pExists is.*
17b16 2a 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53  * set to 0 and S
17b17 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65  QLITE_OK returne
17b18 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f  d. If an IO erro
17b19 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74  r occurs while t
17b1a 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 64 65 74 65  rying.** to dete
17b1b 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72  rmine whether or
17b1c 20 6e 6f 74 20 61 20 68 6f 74 2d 6a 6f 75 72 6e   not a hot-journ
17b1d 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20  al file exists, 
17b1e 74 68 65 20 49 4f 20 65 72 72 6f 72 0a 2a 2a 20  the IO error.** 
17b1f 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
17b20 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f   and the value o
17b21 66 20 2a 70 45 78 69 73 74 73 20 69 73 20 75 6e  f *pExists is un
17b22 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  defined..*/.stat
17b23 69 63 20 69 6e 74 20 68 61 73 48 6f 74 4a 6f 75  ic int hasHotJou
17b24 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67  rnal(Pager *pPag
17b25 65 72 2c 20 69 6e 74 20 2a 70 45 78 69 73 74 73  er, int *pExists
17b26 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  ){.  sqlite3_vfs
17b27 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20   * const pVfs = 
17b28 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20  pPager->pVfs;.  
17b29 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
17b2a 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  OK;           /*
17b2b 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
17b2c 20 20 69 6e 74 20 65 78 69 73 74 73 20 3d 20 31    int exists = 1
17b2d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
17b2e 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6a 6f 75  /* True if a jou
17b2f 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65  rnal file is pre
17b30 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6a 72  sent */.  int jr
17b31 6e 6c 4f 70 65 6e 20 3d 20 21 21 69 73 4f 70 65  nlOpen = !!isOpe
17b32 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a  n(pPager->jfd);.
17b33 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
17b34 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b  r->useJournal );
17b35 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65  .  assert( isOpe
17b36 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b  n(pPager->fd) );
17b37 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
17b38 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
17b39 5f 4f 50 45 4e 20 29 3b 0a 0a 20 20 61 73 73 65  _OPEN );..  asse
17b3a 72 74 28 20 6a 72 6e 6c 4f 70 65 6e 3d 3d 30 20  rt( jrnlOpen==0 
17b3b 7c 7c 20 28 20 73 71 6c 69 74 65 33 4f 73 44 65  || ( sqlite3OsDe
17b3c 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
17b3d 69 63 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  ics(pPager->jfd)
17b3e 20 26 0a 20 20 20 20 53 51 4c 49 54 45 5f 49 4f   &.    SQLITE_IO
17b3f 43 41 50 5f 55 4e 44 45 4c 45 54 41 42 4c 45 5f  CAP_UNDELETABLE_
17b40 57 48 45 4e 5f 4f 50 45 4e 0a 20 20 29 29 3b 0a  WHEN_OPEN.  ));.
17b41 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20 30 3b  .  *pExists = 0;
17b42 0a 20 20 69 66 28 20 21 6a 72 6e 6c 4f 70 65 6e  .  if( !jrnlOpen
17b43 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
17b44 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66  ite3OsAccess(pVf
17b45 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  s, pPager->zJour
17b46 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45  nal, SQLITE_ACCE
17b47 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69 73  SS_EXISTS, &exis
17b48 74 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  ts);.  }.  if( r
17b49 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
17b4a 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 69 6e  exists ){.    in
17b4b 74 20 6c 6f 63 6b 65 64 20 3d 20 30 3b 20 20 20  t locked = 0;   
17b4c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
17b4d 65 20 69 66 20 73 6f 6d 65 20 70 72 6f 63 65 73  e if some proces
17b4e 73 20 68 6f 6c 64 73 20 61 20 52 45 53 45 52 56  s holds a RESERV
17b4f 45 44 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 20  ED lock */..    
17b50 2f 2a 20 52 61 63 65 20 63 6f 6e 64 69 74 69 6f  /* Race conditio
17b51 6e 20 68 65 72 65 3a 20 20 41 6e 6f 74 68 65 72  n here:  Another
17b52 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20 68   process might h
17b53 61 76 65 20 62 65 65 6e 20 68 6f 6c 64 69 6e 67  ave been holding
17b54 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 68 65 20   the.    ** the 
17b55 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 61 6e  RESERVED lock an
17b56 64 20 68 61 76 65 20 61 20 6a 6f 75 72 6e 61 6c  d have a journal
17b57 20 6f 70 65 6e 20 61 74 20 74 68 65 20 73 71 6c   open at the sql
17b58 69 74 65 33 4f 73 41 63 63 65 73 73 28 29 20 0a  ite3OsAccess() .
17b59 20 20 20 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76      ** call abov
17b5a 65 2c 20 62 75 74 20 74 68 65 6e 20 64 65 6c 65  e, but then dele
17b5b 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61  te the journal a
17b5c 6e 64 20 64 72 6f 70 20 74 68 65 20 6c 6f 63 6b  nd drop the lock
17b5d 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 77   before.    ** w
17b5e 65 20 67 65 74 20 74 6f 20 74 68 65 20 66 6f 6c  e get to the fol
17b5f 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 4f 73  lowing sqlite3Os
17b60 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
17b61 6b 28 29 20 63 61 6c 6c 2e 20 20 49 66 20 74 68  k() call.  If th
17b62 61 74 0a 20 20 20 20 2a 2a 20 69 73 20 74 68 65  at.    ** is the
17b63 20 63 61 73 65 2c 20 74 68 69 73 20 72 6f 75 74   case, this rout
17b64 69 6e 65 20 6d 69 67 68 74 20 74 68 69 6e 6b 20  ine might think 
17b65 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a  there is a hot j
17b66 6f 75 72 6e 61 6c 20 77 68 65 6e 0a 20 20 20 20  ournal when.    
17b67 2a 2a 20 69 6e 20 66 61 63 74 20 74 68 65 72 65  ** in fact there
17b68 20 69 73 20 6e 6f 6e 65 2e 20 20 54 68 69 73 20   is none.  This 
17b69 72 65 73 75 6c 74 73 20 69 6e 20 61 20 66 61 6c  results in a fal
17b6a 73 65 2d 70 6f 73 69 74 69 76 65 20 77 68 69 63  se-positive whic
17b6b 68 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65  h will.    ** be
17b6c 20 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 74   dealt with by t
17b6d 68 65 20 70 6c 61 79 62 61 63 6b 20 72 6f 75 74  he playback rout
17b6e 69 6e 65 2e 20 20 54 69 63 6b 65 74 20 23 33 38  ine.  Ticket #38
17b6f 38 33 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  83..    */.    r
17b70 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 68 65  c = sqlite3OsChe
17b71 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 70  ckReservedLock(p
17b72 50 61 67 65 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b  Pager->fd, &lock
17b73 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  ed);.    if( rc=
17b74 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c  =SQLITE_OK && !l
17b75 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 50  ocked ){.      P
17b76 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20  gno nPage;      
17b77 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
17b78 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
17b79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
17b7a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63  /..      /* Chec
17b7b 6b 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  k the size of th
17b7c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
17b7d 20 49 66 20 69 74 20 63 6f 6e 73 69 73 74 73 20   If it consists 
17b7e 6f 66 20 30 20 70 61 67 65 73 2c 0a 20 20 20 20  of 0 pages,.    
17b7f 20 20 2a 2a 20 74 68 65 6e 20 64 65 6c 65 74 65    ** then delete
17b80 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
17b81 65 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65  e. See the heade
17b82 72 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20  r comment above 
17b83 66 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 74 68  for .      ** th
17b84 65 20 72 65 61 73 6f 6e 69 6e 67 20 68 65 72 65  e reasoning here
17b85 2e 20 20 44 65 6c 65 74 65 20 74 68 65 20 6f 62  .  Delete the ob
17b86 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66  solete journal f
17b87 69 6c 65 20 75 6e 64 65 72 0a 20 20 20 20 20 20  ile under.      
17b88 2a 2a 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  ** a RESERVED lo
17b89 63 6b 20 74 6f 20 61 76 6f 69 64 20 72 61 63 65  ck to avoid race
17b8a 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 6e 64 20   conditions and 
17b8b 74 6f 20 61 76 6f 69 64 20 76 69 6f 6c 61 74 69  to avoid violati
17b8c 6e 67 0a 20 20 20 20 20 20 2a 2a 20 5b 48 33 33  ng.      ** [H33
17b8d 30 32 30 5d 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  020]..      */. 
17b8e 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 50       rc = pagerP
17b8f 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
17b90 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 20   &nPage);.      
17b91 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
17b92 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  K ){.        if(
17b93 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20   nPage==0 ){.   
17b94 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65         sqlite3Be
17b95 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
17b96 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
17b97 20 70 61 67 65 72 4c 6f 63 6b 44 62 28 70 50 61   pagerLockDb(pPa
17b98 67 65 72 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f  ger, RESERVED_LO
17b99 43 4b 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  CK)==SQLITE_OK )
17b9a 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  {.            sq
17b9b 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56  lite3OsDelete(pV
17b9c 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  fs, pPager->zJou
17b9d 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20  rnal, 0);.      
17b9e 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65        if( !pPage
17b9f 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
17ba0 20 29 20 70 61 67 65 72 55 6e 6c 6f 63 6b 44 62   ) pagerUnlockDb
17ba1 28 70 50 61 67 65 72 2c 20 53 48 41 52 45 44 5f  (pPager, SHARED_
17ba2 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 20  LOCK);.         
17ba3 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c   }.          sql
17ba4 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
17ba5 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 20 7d  loc();.        }
17ba6 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
17ba7 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  /* The journal f
17ba8 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20 6e  ile exists and n
17ba9 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  o other connecti
17baa 6f 6e 20 68 61 73 20 61 20 72 65 73 65 72 76 65  on has a reserve
17bab 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f  d.          ** o
17bac 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f  r greater lock o
17bad 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
17bae 69 6c 65 2e 20 4e 6f 77 20 63 68 65 63 6b 20 74  ile. Now check t
17baf 68 61 74 20 74 68 65 72 65 20 69 73 0a 20 20 20  hat there is.   
17bb0 20 20 20 20 20 20 20 2a 2a 20 61 74 20 6c 65 61         ** at lea
17bb1 73 74 20 6f 6e 65 20 6e 6f 6e 2d 7a 65 72 6f 20  st one non-zero 
17bb2 62 79 74 65 73 20 61 74 20 74 68 65 20 73 74 61  bytes at the sta
17bb3 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  rt of the journa
17bb4 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20  l file..        
17bb5 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73    ** If there is
17bb6 2c 20 74 68 65 6e 20 77 65 20 63 6f 6e 73 69 64  , then we consid
17bb7 65 72 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20  er this journal 
17bb8 74 6f 20 62 65 20 68 6f 74 2e 20 49 66 20 6e 6f  to be hot. If no
17bb9 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  t, .          **
17bba 20 69 74 20 63 61 6e 20 62 65 20 69 67 6e 6f 72   it can be ignor
17bbb 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  ed..          */
17bbc 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
17bbd 6a 72 6e 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20  jrnlOpen ){.    
17bbe 20 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d 20          int f = 
17bbf 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
17bc0 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  ONLY|SQLITE_OPEN
17bc1 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20  _MAIN_JOURNAL;. 
17bc2 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
17bc3 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56  sqlite3OsOpen(pV
17bc4 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  fs, pPager->zJou
17bc5 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66  rnal, pPager->jf
17bc6 64 2c 20 66 2c 20 26 66 29 3b 0a 20 20 20 20 20  d, f, &f);.     
17bc7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
17bc8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
17bc9 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
17bca 20 20 75 38 20 66 69 72 73 74 20 3d 20 30 3b 0a    u8 first = 0;.
17bcb 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
17bcc 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70   sqlite3OsRead(p
17bcd 50 61 67 65 72 2d 3e 6a 66 64 2c 20 28 76 6f 69  Pager->jfd, (voi
17bce 64 20 2a 29 26 66 69 72 73 74 2c 20 31 2c 20 30  d *)&first, 1, 0
17bcf 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
17bd0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f  f( rc==SQLITE_IO
17bd1 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29  ERR_SHORT_READ )
17bd2 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
17bd3 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
17bd4 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
17bd5 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 6a            if( !j
17bd6 72 6e 6c 4f 70 65 6e 20 29 7b 0a 20 20 20 20 20  rnlOpen ){.     
17bd7 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
17bd8 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e  OsClose(pPager->
17bd9 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20  jfd);.          
17bda 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
17bdb 2a 70 45 78 69 73 74 73 20 3d 20 28 66 69 72 73  *pExists = (firs
17bdc 74 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 20  t!=0);.         
17bdd 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53   }else if( rc==S
17bde 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 29  QLITE_CANTOPEN )
17bdf 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  {.            /*
17be0 20 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f 70   If we cannot op
17be1 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  en the rollback 
17be2 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20  journal file in 
17be3 6f 72 64 65 72 20 74 6f 20 73 65 65 20 69 66 0a  order to see if.
17be4 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69              ** i
17be5 74 73 20 68 61 73 20 61 20 7a 65 72 6f 20 68 65  ts has a zero he
17be6 61 64 65 72 2c 20 74 68 61 74 20 6d 69 67 68 74  ader, that might
17be7 20 62 65 20 64 75 65 20 74 6f 20 61 6e 20 49 2f   be due to an I/
17be8 4f 20 65 72 72 6f 72 2c 20 6f 72 0a 20 20 20 20  O error, or.    
17be9 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 6d 69          ** it mi
17bea 67 68 74 20 62 65 20 64 75 65 20 74 6f 20 74 68  ght be due to th
17beb 65 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e  e race condition
17bec 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65   described above
17bed 20 61 6e 64 20 69 6e 0a 20 20 20 20 20 20 20 20   and in.        
17bee 20 20 20 20 2a 2a 20 74 69 63 6b 65 74 20 23 33      ** ticket #3
17bef 38 38 33 2e 20 20 45 69 74 68 65 72 20 77 61 79  883.  Either way
17bf0 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68  , assume that th
17bf1 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68 6f 74  e journal is hot
17bf2 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  ..            **
17bf3 20 54 68 69 73 20 6d 69 67 68 74 20 62 65 20 61   This might be a
17bf4 20 66 61 6c 73 65 20 70 6f 73 69 74 69 76 65 2e   false positive.
17bf5 20 20 42 75 74 20 69 66 20 69 74 20 69 73 2c 20    But if it is, 
17bf6 74 68 65 6e 20 74 68 65 0a 20 20 20 20 20 20 20  then the.       
17bf7 20 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69       ** automati
17bf8 63 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 79 62 61  c journal playba
17bf9 63 6b 20 61 6e 64 20 72 65 63 6f 76 65 72 79 20  ck and recovery 
17bfa 6d 65 63 68 61 6e 69 73 6d 20 77 69 6c 6c 20 64  mechanism will d
17bfb 65 61 6c 0a 20 20 20 20 20 20 20 20 20 20 20 20  eal.            
17bfc 2a 2a 20 77 69 74 68 20 69 74 20 75 6e 64 65 72  ** with it under
17bfd 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
17bfe 63 6b 20 77 68 65 72 65 20 77 65 20 64 6f 20 6e  ck where we do n
17bff 6f 74 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20  ot need to.     
17c00 20 20 20 20 20 20 20 2a 2a 20 77 6f 72 72 79 20         ** worry 
17c01 73 6f 20 6d 75 63 68 20 77 69 74 68 20 72 61 63  so much with rac
17c02 65 20 63 6f 6e 64 69 74 69 6f 6e 73 2e 0a 20 20  e conditions..  
17c03 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
17c04 20 20 20 20 20 20 20 20 20 2a 70 45 78 69 73 74           *pExist
17c05 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  s = 1;.         
17c06 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
17c07 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  K;.          }. 
17c08 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
17c09 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
17c0a 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
17c0b 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
17c0c 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 6f 62 74  is called to obt
17c0d 61 69 6e 20 61 20 73 68 61 72 65 64 20 6c 6f 63  ain a shared loc
17c0e 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
17c0f 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 74 20 69 73  e file..** It is
17c10 20 69 6c 6c 65 67 61 6c 20 74 6f 20 63 61 6c 6c   illegal to call
17c11 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71   sqlite3PagerAcq
17c12 75 69 72 65 28 29 20 75 6e 74 69 6c 20 61 66 74  uire() until aft
17c13 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  er this function
17c14 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 73 75 63  .** has been suc
17c15 63 65 73 73 66 75 6c 6c 79 20 63 61 6c 6c 65 64  cessfully called
17c16 2e 20 49 66 20 61 20 73 68 61 72 65 64 2d 6c 6f  . If a shared-lo
17c17 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65  ck is already he
17c18 6c 64 20 77 68 65 6e 0a 2a 2a 20 74 68 69 73 20  ld when.** this 
17c19 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
17c1a 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f  ed, it is a no-o
17c1b 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  p..**.** The fol
17c1c 6c 6f 77 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  lowing operation
17c1d 73 20 61 72 65 20 61 6c 73 6f 20 70 65 72 66 6f  s are also perfo
17c1e 72 6d 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  rmed by this fun
17c1f 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 31  ction..**.**   1
17c20 29 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  ) If the pager i
17c21 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 50  s currently in P
17c22 41 47 45 52 5f 4f 50 45 4e 20 73 74 61 74 65 20  AGER_OPEN state 
17c23 28 6e 6f 20 6c 6f 63 6b 20 68 65 6c 64 0a 2a 2a  (no lock held.**
17c24 20 20 20 20 20 20 6f 6e 20 74 68 65 20 64 61 74        on the dat
17c25 61 62 61 73 65 20 66 69 6c 65 29 2c 20 74 68 65  abase file), the
17c26 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20  n an attempt is 
17c27 6d 61 64 65 20 74 6f 20 6f 62 74 61 69 6e 20 61  made to obtain a
17c28 0a 2a 2a 20 20 20 20 20 20 53 48 41 52 45 44 20  .**      SHARED 
17c29 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
17c2a 62 61 73 65 20 66 69 6c 65 2e 20 49 6d 6d 65 64  base file. Immed
17c2b 69 61 74 65 6c 79 20 61 66 74 65 72 20 6f 62 74  iately after obt
17c2c 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74  aining.**      t
17c2d 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20  he SHARED lock, 
17c2e 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  the file-system 
17c2f 69 73 20 63 68 65 63 6b 65 64 20 66 6f 72 20 61  is checked for a
17c30 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 0a 2a 2a   hot-journal,.**
17c31 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20 70        which is p
17c32 6c 61 79 65 64 20 62 61 63 6b 20 69 66 20 70 72  layed back if pr
17c33 65 73 65 6e 74 2e 20 46 6f 6c 6c 6f 77 69 6e 67  esent. Following
17c34 20 61 6e 79 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c   any hot-journal
17c35 20 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c 62 61   .**      rollba
17c36 63 6b 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ck, the contents
17c37 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 61 72   of the cache ar
17c38 65 20 76 61 6c 69 64 61 74 65 64 20 62 79 20 63  e validated by c
17c39 68 65 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20  hecking.**      
17c3a 74 68 65 20 27 63 68 61 6e 67 65 2d 63 6f 75 6e  the 'change-coun
17c3b 74 65 72 27 20 66 69 65 6c 64 20 6f 66 20 74 68  ter' field of th
17c3c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
17c3d 68 65 61 64 65 72 20 61 6e 64 0a 2a 2a 20 20 20  header and.**   
17c3e 20 20 20 64 69 73 63 61 72 64 65 64 20 69 66 20     discarded if 
17c3f 74 68 65 79 20 61 72 65 20 66 6f 75 6e 64 20 74  they are found t
17c40 6f 20 62 65 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a  o be invalid..**
17c41 0a 2a 2a 20 20 20 32 29 20 49 66 20 74 68 65 20  .**   2) If the 
17c42 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67  pager is running
17c43 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f   in exclusive-mo
17c44 64 65 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72  de, and there ar
17c45 65 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 20  e currently.**  
17c46 20 20 20 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69      no outstandi
17c47 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  ng references to
17c48 20 61 6e 79 20 70 61 67 65 73 2c 20 61 6e 64 20   any pages, and 
17c49 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20  is in the error 
17c4a 73 74 61 74 65 2c 0a 2a 2a 20 20 20 20 20 20 74  state,.**      t
17c4b 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69  hen an attempt i
17c4c 73 20 6d 61 64 65 20 74 6f 20 63 6c 65 61 72 20  s made to clear 
17c4d 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20  the error state 
17c4e 62 79 20 64 69 73 63 61 72 64 69 6e 67 0a 2a 2a  by discarding.**
17c4f 20 20 20 20 20 20 74 68 65 20 63 6f 6e 74 65 6e        the conten
17c50 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 63  ts of the page c
17c51 61 63 68 65 20 61 6e 64 20 72 6f 6c 6c 69 6e 67  ache and rolling
17c52 20 62 61 63 6b 20 61 6e 79 20 6f 70 65 6e 20 6a   back any open j
17c53 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 66  ournal.**      f
17c54 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 76  ile..**.** If ev
17c55 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63  erything is succ
17c56 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f  essful, SQLITE_O
17c57 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  K is returned. I
17c58 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 0a 2a  f an IO error .*
17c59 2a 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6c  * occurs while l
17c5a 6f 63 6b 69 6e 67 20 74 68 65 20 64 61 74 61 62  ocking the datab
17c5b 61 73 65 2c 20 63 68 65 63 6b 69 6e 67 20 66 6f  ase, checking fo
17c5c 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  r a hot-journal 
17c5d 66 69 6c 65 20 6f 72 20 0a 2a 2a 20 72 6f 6c 6c  file or .** roll
17c5e 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e  ing back a journ
17c5f 61 6c 20 66 69 6c 65 2c 20 74 68 65 20 49 4f 20  al file, the IO 
17c60 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
17c61 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  turned..*/.SQLIT
17c62 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
17c63 6c 69 74 65 33 50 61 67 65 72 53 68 61 72 65 64  lite3PagerShared
17c64 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67  Lock(Pager *pPag
17c65 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  er){.  int rc = 
17c66 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
17c67 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
17c68 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f  urn code */..  /
17c69 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
17c6a 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72  s only called fr
17c6b 6f 6d 20 62 2d 74 72 65 65 20 61 6e 64 20 6f 6e  om b-tree and on
17c6c 6c 79 20 77 68 65 6e 20 74 68 65 72 65 20 61 72  ly when there ar
17c6d 65 20 6e 6f 0a 20 20 2a 2a 20 6f 75 74 73 74 61  e no.  ** outsta
17c6e 6e 64 69 6e 67 20 70 61 67 65 73 2e 20 54 68 69  nding pages. Thi
17c6f 73 20 69 6d 70 6c 69 65 73 20 74 68 61 74 20 74  s implies that t
17c70 68 65 20 70 61 67 65 72 20 73 74 61 74 65 20 73  he pager state s
17c71 68 6f 75 6c 64 20 65 69 74 68 65 72 0a 20 20 2a  hould either.  *
17c72 2a 20 62 65 20 4f 50 45 4e 20 6f 72 20 52 45 41  * be OPEN or REA
17c73 44 45 52 2e 20 52 45 41 44 45 52 20 69 73 20 6f  DER. READER is o
17c74 6e 6c 79 20 70 6f 73 73 69 62 6c 65 20 69 66 20  nly possible if 
17c75 74 68 65 20 70 61 67 65 72 20 69 73 20 6f 72 20  the pager is or 
17c76 77 61 73 20 69 6e 20 0a 20 20 2a 2a 20 65 78 63  was in .  ** exc
17c77 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 6d 6f  lusive access mo
17c78 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  de..  */.  asser
17c79 74 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  t( sqlite3Pcache
17c7a 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d  RefCount(pPager-
17c7b 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a  >pPCache)==0 );.
17c7c 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74    assert( assert
17c7d 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61  _pager_state(pPa
17c7e 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  ger) );.  assert
17c7f 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65  ( pPager->eState
17c80 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20 7c 7c 20  ==PAGER_OPEN || 
17c81 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d  pPager->eState==
17c82 50 41 47 45 52 5f 52 45 41 44 45 52 20 29 3b 0a  PAGER_READER );.
17c83 20 20 69 66 28 20 4e 45 56 45 52 28 4d 45 4d 44    if( NEVER(MEMD
17c84 42 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72  B && pPager->err
17c85 43 6f 64 65 29 20 29 7b 20 72 65 74 75 72 6e 20  Code) ){ return 
17c86 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
17c87 20 7d 0a 0a 20 20 69 66 28 20 21 70 61 67 65 72   }..  if( !pager
17c88 55 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 26  UseWal(pPager) &
17c89 26 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65  & pPager->eState
17c8a 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20 29 7b 0a  ==PAGER_OPEN ){.
17c8b 20 20 20 20 69 6e 74 20 62 48 6f 74 4a 6f 75 72      int bHotJour
17c8c 6e 61 6c 20 3d 20 31 3b 20 20 20 20 20 20 20 20  nal = 1;        
17c8d 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
17c8e 72 65 20 65 78 69 73 74 73 20 61 20 68 6f 74 20  re exists a hot 
17c8f 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 2a 2f 0a  journal-file */.
17c90 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45  .    assert( !ME
17c91 4d 44 42 20 29 3b 0a 0a 20 20 20 20 72 63 20 3d  MDB );..    rc =
17c92 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c   pager_wait_on_l
17c93 6f 63 6b 28 70 50 61 67 65 72 2c 20 53 48 41 52  ock(pPager, SHAR
17c94 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66  ED_LOCK);.    if
17c95 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
17c96 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
17c97 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d   pPager->eLock==
17c98 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 70 50 61 67 65  NO_LOCK || pPage
17c99 72 2d 3e 65 4c 6f 63 6b 3d 3d 55 4e 4b 4e 4f 57  r->eLock==UNKNOW
17c9a 4e 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20  N_LOCK );.      
17c9b 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20  goto failed;.   
17c9c 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 20   }..    /* If a 
17c9d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69  journal file exi
17c9e 73 74 73 2c 20 61 6e 64 20 74 68 65 72 65 20 69  sts, and there i
17c9f 73 20 6e 6f 20 52 45 53 45 52 56 45 44 20 6c 6f  s no RESERVED lo
17ca0 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a  ck on the.    **
17ca1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
17ca2 74 68 65 6e 20 69 74 20 65 69 74 68 65 72 20 6e  then it either n
17ca3 65 65 64 73 20 74 6f 20 62 65 20 70 6c 61 79 65  eeds to be playe
17ca4 64 20 62 61 63 6b 20 6f 72 20 64 65 6c 65 74 65  d back or delete
17ca5 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  d..    */.    if
17ca6 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3c  ( pPager->eLock<
17ca7 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a  =SHARED_LOCK ){.
17ca8 20 20 20 20 20 20 72 63 20 3d 20 68 61 73 48 6f        rc = hasHo
17ca9 74 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c  tJournal(pPager,
17caa 20 26 62 48 6f 74 4a 6f 75 72 6e 61 6c 29 3b 0a   &bHotJournal);.
17cab 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
17cac 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
17cad 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64       goto failed
17cae 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
17caf 62 48 6f 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20  bHotJournal ){. 
17cb0 20 20 20 20 20 2f 2a 20 47 65 74 20 61 6e 20 45       /* Get an E
17cb1 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e  XCLUSIVE lock on
17cb2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
17cb3 6c 65 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e  le. At this poin
17cb4 74 20 69 74 20 69 73 0a 20 20 20 20 20 20 2a 2a  t it is.      **
17cb5 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20   important that 
17cb6 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
17cb7 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20  is not obtained 
17cb8 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 74 68  on the way to th
17cb9 65 0a 20 20 20 20 20 20 2a 2a 20 45 58 43 4c 55  e.      ** EXCLU
17cba 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74  SIVE lock. If it
17cbb 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 72 20 70   were, another p
17cbc 72 6f 63 65 73 73 20 6d 69 67 68 74 20 6f 70 65  rocess might ope
17cbd 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64  n the.      ** d
17cbe 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 64 65  atabase file, de
17cbf 74 65 63 74 20 74 68 65 20 52 45 53 45 52 56 45  tect the RESERVE
17cc0 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63  D lock, and conc
17cc1 6c 75 64 65 20 74 68 61 74 20 74 68 65 0a 20 20  lude that the.  
17cc2 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
17cc3 69 73 20 73 61 66 65 20 74 6f 20 72 65 61 64 20  is safe to read 
17cc4 77 68 69 6c 65 20 74 68 69 73 20 70 72 6f 63 65  while this proce
17cc5 73 73 20 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c  ss is still roll
17cc6 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 2a  ing the .      *
17cc7 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 61  * hot-journal ba
17cc8 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20  ck..      ** .  
17cc9 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65 20 74      ** Because t
17cca 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  he intermediate 
17ccb 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73  RESERVED lock is
17ccc 20 6e 6f 74 20 72 65 71 75 65 73 74 65 64 2c 20   not requested, 
17ccd 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68  any.      ** oth
17cce 65 72 20 70 72 6f 63 65 73 73 20 61 74 74 65 6d  er process attem
17ccf 70 74 69 6e 67 20 74 6f 20 61 63 63 65 73 73 20  pting to access 
17cd0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
17cd1 65 20 77 69 6c 6c 20 67 65 74 20 74 6f 20 0a 20  e will get to . 
17cd2 20 20 20 20 20 2a 2a 20 74 68 69 73 20 70 6f 69       ** this poi
17cd3 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 61  nt in the code a
17cd4 6e 64 20 66 61 69 6c 20 74 6f 20 6f 62 74 61 69  nd fail to obtai
17cd5 6e 20 69 74 73 20 6f 77 6e 20 45 58 43 4c 55 53  n its own EXCLUS
17cd6 49 56 45 20 6c 6f 63 6b 20 0a 20 20 20 20 20 20  IVE lock .      
17cd7 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ** on the databa
17cd8 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 2a  se file..      *
17cd9 2a 0a 20 20 20 20 20 20 2a 2a 20 55 6e 6c 65 73  *.      ** Unles
17cda 73 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69  s the pager is i
17cdb 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65  n locking_mode=e
17cdc 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74  xclusive mode, t
17cdd 68 65 20 6c 6f 63 6b 20 69 73 0a 20 20 20 20 20  he lock is.     
17cde 20 2a 2a 20 64 6f 77 6e 67 72 61 64 65 64 20 74   ** downgraded t
17cdf 6f 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 62 65  o SHARED_LOCK be
17ce0 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69  fore this functi
17ce1 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 20 20 20 20  on returns..    
17ce2 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20    */.      rc = 
17ce3 70 61 67 65 72 4c 6f 63 6b 44 62 28 70 50 61 67  pagerLockDb(pPag
17ce4 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  er, EXCLUSIVE_LO
17ce5 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  CK);.      if( r
17ce6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
17ce7 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69          goto fai
17ce8 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 0a 20  led;.      }. . 
17ce9 20 20 20 20 20 2f 2a 20 49 66 20 69 74 20 69 73       /* If it is
17cea 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65   not already ope
17ceb 6e 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 65  n and the file e
17cec 78 69 73 74 73 20 6f 6e 20 64 69 73 6b 2c 20 6f  xists on disk, o
17ced 70 65 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a  pen the .      *
17cee 2a 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 65  * journal for re
17cef 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e  ad/write access.
17cf0 20 57 72 69 74 65 20 61 63 63 65 73 73 20 69 73   Write access is
17cf1 20 72 65 71 75 69 72 65 64 20 62 65 63 61 75 73   required becaus
17cf2 65 20 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20 65  e .      ** in e
17cf3 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20  xclusive-access 
17cf4 6d 6f 64 65 20 74 68 65 20 66 69 6c 65 20 64 65  mode the file de
17cf5 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65  scriptor will be
17cf6 20 6b 65 70 74 20 6f 70 65 6e 20 0a 20 20 20 20   kept open .    
17cf7 20 20 2a 2a 20 61 6e 64 20 70 6f 73 73 69 62 6c    ** and possibl
17cf8 79 20 75 73 65 64 20 66 6f 72 20 61 20 74 72 61  y used for a tra
17cf9 6e 73 61 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f  nsaction later o
17cfa 6e 2e 20 41 6c 73 6f 2c 20 77 72 69 74 65 2d 61  n. Also, write-a
17cfb 63 63 65 73 73 20 0a 20 20 20 20 20 20 2a 2a 20  ccess .      ** 
17cfc 69 73 20 75 73 75 61 6c 6c 79 20 72 65 71 75 69  is usually requi
17cfd 72 65 64 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20  red to finalize 
17cfe 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 6e 20 6a  the journal in j
17cff 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 70 65 72 73  ournal_mode=pers
17d00 69 73 74 20 0a 20 20 20 20 20 20 2a 2a 20 6d 6f  ist .      ** mo
17d01 64 65 20 28 61 6e 64 20 61 6c 73 6f 20 66 6f 72  de (and also for
17d02 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 74 72   journal_mode=tr
17d03 75 6e 63 61 74 65 20 6f 6e 20 73 6f 6d 65 20 73  uncate on some s
17d04 79 73 74 65 6d 73 29 2e 0a 20 20 20 20 20 20 2a  ystems)..      *
17d05 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68  *.      ** If th
17d06 65 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e  e journal does n
17d07 6f 74 20 65 78 69 73 74 2c 20 69 74 20 75 73 75  ot exist, it usu
17d08 61 6c 6c 79 20 6d 65 61 6e 73 20 74 68 61 74 20  ally means that 
17d09 73 6f 6d 65 20 0a 20 20 20 20 20 20 2a 2a 20 6f  some .      ** o
17d0a 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ther connection 
17d0b 6d 61 6e 61 67 65 64 20 74 6f 20 67 65 74 20 69  managed to get i
17d0c 6e 20 61 6e 64 20 72 6f 6c 6c 20 69 74 20 62 61  n and roll it ba
17d0d 63 6b 20 62 65 66 6f 72 65 20 0a 20 20 20 20 20  ck before .     
17d0e 20 2a 2a 20 74 68 69 73 20 63 6f 6e 6e 65 63 74   ** this connect
17d0f 69 6f 6e 20 6f 62 74 61 69 6e 65 64 20 74 68 65  ion obtained the
17d10 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
17d11 61 62 6f 76 65 2e 20 4f 72 2c 20 69 74 20 0a 20  above. Or, it . 
17d12 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d 65 61 6e       ** may mean
17d13 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20   that the pager 
17d14 77 61 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72  was in the error
17d15 2d 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73  -state when this
17d16 0a 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69  .      ** functi
17d17 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 20 61 6e  on was called an
17d18 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  d the journal fi
17d19 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  le does not exis
17d1a 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  t..      */.    
17d1b 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50    if( !isOpen(pP
17d1c 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20  ager->jfd) ){.  
17d1d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66        sqlite3_vf
17d1e 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 3d  s * const pVfs =
17d1f 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20   pPager->pVfs;. 
17d20 20 20 20 20 20 20 20 69 6e 74 20 62 45 78 69 73         int bExis
17d21 74 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ts;             
17d22 20 2f 2a 20 54 72 75 65 20 69 66 20 6a 6f 75 72   /* True if jour
17d23 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 20  nal file exists 
17d24 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  */.        rc = 
17d25 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28  sqlite3OsAccess(
17d26 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 56 66  .            pVf
17d27 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  s, pPager->zJour
17d28 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45  nal, SQLITE_ACCE
17d29 53 53 5f 45 58 49 53 54 53 2c 20 26 62 45 78 69  SS_EXISTS, &bExi
17d2a 73 74 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66  sts);.        if
17d2b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
17d2c 26 26 20 62 45 78 69 73 74 73 20 29 7b 0a 20 20  && bExists ){.  
17d2d 20 20 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74          int fout
17d2e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
17d2f 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f  int f = SQLITE_O
17d30 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51  PEN_READWRITE|SQ
17d31 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a  LITE_OPEN_MAIN_J
17d32 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20  OURNAL;.        
17d33 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65    assert( !pPage
17d34 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20  r->tempFile );. 
17d35 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
17d36 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73  lite3OsOpen(pVfs
17d37 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e  , pPager->zJourn
17d38 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c  al, pPager->jfd,
17d39 20 66 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20 20   f, &fout);.    
17d3a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
17d3b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69  !=SQLITE_OK || i
17d3c 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
17d3d 64 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  d) );.          
17d3e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
17d3f 4b 20 26 26 20 66 6f 75 74 26 53 51 4c 49 54 45  K && fout&SQLITE
17d40 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 29  _OPEN_READONLY )
17d41 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  {.            rc
17d42 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50   = SQLITE_CANTOP
17d43 45 4e 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20  EN_BKPT;.       
17d44 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c       sqlite3OsCl
17d45 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  ose(pPager->jfd)
17d46 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
17d47 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
17d48 20 0a 20 20 20 20 20 20 2f 2a 20 50 6c 61 79 62   .      /* Playb
17d49 61 63 6b 20 61 6e 64 20 64 65 6c 65 74 65 20 74  ack and delete t
17d4a 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f  he journal.  Dro
17d4b 70 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77  p the database w
17d4c 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 6c 6f  rite.      ** lo
17d4d 63 6b 20 61 6e 64 20 72 65 61 63 71 75 69 72 65  ck and reacquire
17d4e 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 20   the read lock. 
17d4f 50 75 72 67 65 20 74 68 65 20 63 61 63 68 65 20  Purge the cache 
17d50 62 65 66 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20  before.      ** 
17d51 70 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65  playing back the
17d52 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20   hot-journal so 
17d53 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20 65 6e  that we don't en
17d54 64 20 75 70 20 77 69 74 68 0a 20 20 20 20 20 20  d up with.      
17d55 2a 2a 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65  ** an inconsiste
17d56 6e 74 20 63 61 63 68 65 2e 20 20 53 79 6e 63 20  nt cache.  Sync 
17d57 74 68 65 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20  the hot journal 
17d58 62 65 66 6f 72 65 20 70 6c 61 79 69 6e 67 0a 20  before playing. 
17d59 20 20 20 20 20 2a 2a 20 69 74 20 62 61 63 6b 20       ** it back 
17d5a 73 69 6e 63 65 20 74 68 65 20 70 72 6f 63 65 73  since the proces
17d5b 73 20 74 68 61 74 20 63 72 61 73 68 65 64 20 61  s that crashed a
17d5c 6e 64 20 6c 65 66 74 20 74 68 65 20 68 6f 74 20  nd left the hot 
17d5d 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a  journal.      **
17d5e 20 70 72 6f 62 61 62 6c 79 20 64 69 64 20 6e 6f   probably did no
17d5f 74 20 73 79 6e 63 20 69 74 20 61 6e 64 20 77 65  t sync it and we
17d60 20 61 72 65 20 72 65 71 75 69 72 65 64 20 74 6f   are required to
17d61 20 61 6c 77 61 79 73 20 73 79 6e 63 0a 20 20 20   always sync.   
17d62 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61     ** the journa
17d63 6c 20 62 65 66 6f 72 65 20 70 6c 61 79 69 6e 67  l before playing
17d64 20 69 74 20 62 61 63 6b 2e 0a 20 20 20 20 20 20   it back..      
17d65 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 73 4f  */.      if( isO
17d66 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
17d67 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
17d68 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
17d69 4b 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  K );.        rc 
17d6a 3d 20 70 61 67 65 72 53 79 6e 63 48 6f 74 4a 6f  = pagerSyncHotJo
17d6b 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20  urnal(pPager);. 
17d6c 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
17d6d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17d6e 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
17d6f 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72  _playback(pPager
17d70 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 1);.          
17d71 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 20 3d  pPager->eState =
17d72 20 50 41 47 45 52 5f 4f 50 45 4e 3b 0a 20 20 20   PAGER_OPEN;.   
17d73 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
17d74 73 65 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e  se if( !pPager->
17d75 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b  exclusiveMode ){
17d76 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 55 6e  .        pagerUn
17d77 6c 6f 63 6b 44 62 28 70 50 61 67 65 72 2c 20 53  lockDb(pPager, S
17d78 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20  HARED_LOCK);.   
17d79 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20     }..      if( 
17d7a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
17d7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73  .        /* This
17d7c 20 62 72 61 6e 63 68 20 69 73 20 74 61 6b 65 6e   branch is taken
17d7d 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   if an error occ
17d7e 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67  urs while trying
17d7f 20 74 6f 20 6f 70 65 6e 0a 20 20 20 20 20 20 20   to open.       
17d80 20 2a 2a 20 6f 72 20 72 6f 6c 6c 20 62 61 63 6b   ** or roll back
17d81 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77   a hot-journal w
17d82 68 69 6c 65 20 68 6f 6c 64 69 6e 67 20 61 6e 20  hile holding an 
17d83 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
17d84 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70  The.        ** p
17d85 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 72 6f  ager_unlock() ro
17d86 75 74 69 6e 65 20 77 69 6c 6c 20 62 65 20 63 61  utine will be ca
17d87 6c 6c 65 64 20 62 65 66 6f 72 65 20 72 65 74 75  lled before retu
17d88 72 6e 69 6e 67 20 74 6f 20 75 6e 6c 6f 63 6b 0a  rning to unlock.
17d89 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66          ** the f
17d8a 69 6c 65 2e 20 49 66 20 74 68 65 20 75 6e 6c 6f  ile. If the unlo
17d8b 63 6b 20 61 74 74 65 6d 70 74 20 66 61 69 6c 73  ck attempt fails
17d8c 2c 20 74 68 65 6e 20 50 61 67 65 72 2e 65 4c 6f  , then Pager.eLo
17d8d 63 6b 20 6d 75 73 74 20 62 65 0a 20 20 20 20 20  ck must be.     
17d8e 20 20 20 2a 2a 20 73 65 74 20 74 6f 20 55 4e 4b     ** set to UNK
17d8f 4e 4f 57 4e 5f 4c 4f 43 4b 20 28 73 65 65 20 74  NOWN_LOCK (see t
17d90 68 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65  he comment above
17d91 20 74 68 65 20 23 64 65 66 69 6e 65 20 66 6f 72   the #define for
17d92 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 55 4e 4b   .        ** UNK
17d93 4e 4f 57 4e 5f 4c 4f 43 4b 20 61 62 6f 76 65 20  NOWN_LOCK above 
17d94 66 6f 72 20 61 6e 20 65 78 70 6c 61 6e 61 74 69  for an explanati
17d95 6f 6e 29 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a  on). .        **
17d96 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 6e 20 6f  .        ** In o
17d97 72 64 65 72 20 74 6f 20 67 65 74 20 70 61 67 65  rder to get page
17d98 72 5f 75 6e 6c 6f 63 6b 28 29 20 74 6f 20 64 6f  r_unlock() to do
17d99 20 74 68 69 73 2c 20 73 65 74 20 50 61 67 65 72   this, set Pager
17d9a 2e 65 53 74 61 74 65 20 74 6f 0a 20 20 20 20 20  .eState to.     
17d9b 20 20 20 2a 2a 20 50 41 47 45 52 5f 45 52 52 4f     ** PAGER_ERRO
17d9c 52 20 6e 6f 77 2e 20 54 68 69 73 20 69 73 20 6e  R now. This is n
17d9d 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6f 75 6e  ot actually coun
17d9e 74 65 64 20 61 73 20 61 20 74 72 61 6e 73 69 74  ted as a transit
17d9f 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ion.        ** t
17da0 6f 20 45 52 52 4f 52 20 73 74 61 74 65 20 69 6e  o ERROR state in
17da1 20 74 68 65 20 73 74 61 74 65 20 64 69 61 67 72   the state diagr
17da2 61 6d 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66  am at the top of
17da3 20 74 68 69 73 20 66 69 6c 65 2c 0a 20 20 20 20   this file,.    
17da4 20 20 20 20 2a 2a 20 73 69 6e 63 65 20 77 65 20      ** since we 
17da5 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 73 61  know that the sa
17da6 6d 65 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72  me call to pager
17da7 5f 75 6e 6c 6f 63 6b 28 29 20 77 69 6c 6c 20 76  _unlock() will v
17da8 65 72 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 73  ery.        ** s
17da9 68 6f 72 74 6c 79 20 74 72 61 6e 73 69 74 69 6f  hortly transitio
17daa 6e 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65  n the pager obje
17dab 63 74 20 74 6f 20 74 68 65 20 4f 50 45 4e 20 73  ct to the OPEN s
17dac 74 61 74 65 2e 20 43 61 6c 6c 69 6e 67 0a 20 20  tate. Calling.  
17dad 20 20 20 20 20 20 2a 2a 20 61 73 73 65 72 74 5f        ** assert_
17dae 70 61 67 65 72 5f 73 74 61 74 65 28 29 20 77 6f  pager_state() wo
17daf 75 6c 64 20 66 61 69 6c 20 6e 6f 77 2c 20 61 73  uld fail now, as
17db0 20 69 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62   it should not b
17db1 65 20 70 6f 73 73 69 62 6c 65 0a 20 20 20 20 20  e possible.     
17db2 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e 20 45     ** to be in E
17db3 52 52 4f 52 20 73 74 61 74 65 20 77 68 65 6e 20  RROR state when 
17db4 74 68 65 72 65 20 61 72 65 20 7a 65 72 6f 20 6f  there are zero o
17db5 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20  utstanding page 
17db6 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 66 65  .        ** refe
17db7 72 65 6e 63 65 73 2e 0a 20 20 20 20 20 20 20 20  rences..        
17db8 2a 2f 0a 20 20 20 20 20 20 20 20 70 61 67 65 72  */.        pager
17db9 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72  _error(pPager, r
17dba 63 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  c);.        goto
17dbb 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d   failed;.      }
17dbc 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
17dbd 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d  pPager->eState==
17dbe 50 41 47 45 52 5f 4f 50 45 4e 20 29 3b 0a 20 20  PAGER_OPEN );.  
17dbf 20 20 20 20 61 73 73 65 72 74 28 20 28 70 50 61      assert( (pPa
17dc0 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 53 48 41 52  ger->eLock==SHAR
17dc1 45 44 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 20 20  ED_LOCK).       
17dc2 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e      || (pPager->
17dc3 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26  exclusiveMode &&
17dc4 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3e 53   pPager->eLock>S
17dc5 48 41 52 45 44 5f 4c 4f 43 4b 29 0a 20 20 20 20  HARED_LOCK).    
17dc6 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20    );.    }..    
17dc7 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d  if( !pPager->tem
17dc8 70 46 69 6c 65 20 0a 20 20 20 20 20 26 26 20 28  pFile .     && (
17dc9 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20  pPager->pBackup 
17dca 7c 7c 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  || sqlite3Pcache
17dcb 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72  Pagecount(pPager
17dcc 2d 3e 70 50 43 61 63 68 65 29 3e 30 29 20 0a 20  ->pPCache)>0) . 
17dcd 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54     ){.      /* T
17dce 68 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 68  he shared-lock h
17dcf 61 73 20 6a 75 73 74 20 62 65 65 6e 20 61 63 71  as just been acq
17dd0 75 69 72 65 64 20 6f 6e 20 74 68 65 20 64 61 74  uired on the dat
17dd1 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20  abase file.     
17dd2 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 61 72   ** and there ar
17dd3 65 20 61 6c 72 65 61 64 79 20 70 61 67 65 73 20  e already pages 
17dd4 69 6e 20 74 68 65 20 63 61 63 68 65 20 28 66 72  in the cache (fr
17dd5 6f 6d 20 61 20 70 72 65 76 69 6f 75 73 0a 20 20  om a previous.  
17dd6 20 20 20 20 2a 2a 20 72 65 61 64 20 6f 72 20 77      ** read or w
17dd7 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
17dd8 29 2e 20 20 43 68 65 63 6b 20 74 6f 20 73 65 65  ).  Check to see
17dd9 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
17dda 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20 62 65  .      ** has be
17ddb 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20 49 66  en modified.  If
17ddc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
17ddd 73 20 63 68 61 6e 67 65 64 2c 20 66 6c 75 73 68  s changed, flush
17dde 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 63 61   the.      ** ca
17ddf 63 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  che..      **.  
17de0 20 20 20 20 2a 2a 20 44 61 74 61 62 61 73 65 20      ** Database 
17de1 63 68 61 6e 67 65 73 20 69 73 20 64 65 74 65 63  changes is detec
17de2 74 65 64 20 62 79 20 6c 6f 6f 6b 69 6e 67 20 61  ted by looking a
17de3 74 20 31 35 20 62 79 74 65 73 20 62 65 67 69 6e  t 15 bytes begin
17de4 6e 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 74  ning.      ** at
17de5 20 6f 66 66 73 65 74 20 32 34 20 69 6e 74 6f 20   offset 24 into 
17de6 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 66  the file.  The f
17de7 69 72 73 74 20 34 20 6f 66 20 74 68 65 73 65 20  irst 4 of these 
17de8 31 36 20 62 79 74 65 73 20 61 72 65 0a 20 20 20  16 bytes are.   
17de9 20 20 20 2a 2a 20 61 20 33 32 2d 62 69 74 20 63     ** a 32-bit c
17dea 6f 75 6e 74 65 72 20 74 68 61 74 20 69 73 20 69  ounter that is i
17deb 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74 68 20  ncremented with 
17dec 65 61 63 68 20 63 68 61 6e 67 65 2e 20 20 54 68  each change.  Th
17ded 65 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72  e.      ** other
17dee 20 62 79 74 65 73 20 63 68 61 6e 67 65 20 72 61   bytes change ra
17def 6e 64 6f 6d 6c 79 20 77 69 74 68 20 65 61 63 68  ndomly with each
17df0 20 66 69 6c 65 20 63 68 61 6e 67 65 20 77 68 65   file change whe
17df1 6e 0a 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 64  n.      ** a cod
17df2 65 63 20 69 73 20 69 6e 20 75 73 65 2e 0a 20 20  ec is in use..  
17df3 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 2a 2a      ** .      **
17df4 20 54 68 65 72 65 20 69 73 20 61 20 76 61 6e 69   There is a vani
17df5 73 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20 63 68  shingly small ch
17df6 61 6e 63 65 20 74 68 61 74 20 61 20 63 68 61 6e  ance that a chan
17df7 67 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 0a  ge will not be .
17df8 20 20 20 20 20 20 2a 2a 20 64 65 74 65 63 74 65        ** detecte
17df9 64 2e 20 20 54 68 65 20 63 68 61 6e 63 65 20 6f  d.  The chance o
17dfa 66 20 61 6e 20 75 6e 64 65 74 65 63 74 65 64 20  f an undetected 
17dfb 63 68 61 6e 67 65 20 69 73 20 73 6f 20 73 6d 61  change is so sma
17dfc 6c 6c 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a  ll that.      **
17dfd 20 69 74 20 63 61 6e 20 62 65 20 6e 65 67 6c 65   it can be negle
17dfe 63 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  cted..      */. 
17dff 20 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 20       Pgno nPage 
17e00 3d 20 30 3b 0a 20 20 20 20 20 20 63 68 61 72 20  = 0;.      char 
17e01 64 62 46 69 6c 65 56 65 72 73 5b 73 69 7a 65 6f  dbFileVers[sizeo
17e02 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  f(pPager->dbFile
17e03 56 65 72 73 29 5d 3b 0a 0a 20 20 20 20 20 20 72  Vers)];..      r
17e04 63 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75  c = pagerPagecou
17e05 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 50 61 67  nt(pPager, &nPag
17e06 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  e);.      if( rc
17e07 20 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a   ) goto failed;.
17e08 0a 20 20 20 20 20 20 69 66 28 20 6e 50 61 67 65  .      if( nPage
17e09 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 49 4f  >0 ){.        IO
17e0a 54 52 41 43 45 28 28 22 43 4b 56 45 52 53 20 25  TRACE(("CKVERS %
17e0b 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  p %d\n", pPager,
17e0c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65   sizeof(dbFileVe
17e0d 72 73 29 29 29 3b 0a 20 20 20 20 20 20 20 20 72  rs)));.        r
17e0e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
17e0f 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 64  d(pPager->fd, &d
17e10 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f  bFileVers, sizeo
17e11 66 28 64 62 46 69 6c 65 56 65 72 73 29 2c 20 32  f(dbFileVers), 2
17e12 34 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  4);.        if( 
17e13 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
17e14 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
17e15 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20  failed;.        
17e16 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
17e17 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 64 62         memset(db
17e18 46 69 6c 65 56 65 72 73 2c 20 30 2c 20 73 69 7a  FileVers, 0, siz
17e19 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29  eof(dbFileVers))
17e1a 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
17e1b 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 50 61 67   if( memcmp(pPag
17e1c 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20  er->dbFileVers, 
17e1d 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65  dbFileVers, size
17e1e 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 21  of(dbFileVers))!
17e1f 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 61  =0 ){.        pa
17e20 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72  ger_reset(pPager
17e21 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
17e22 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72  ..    /* If ther
17e23 65 20 69 73 20 61 20 57 41 4c 20 66 69 6c 65 20  e is a WAL file 
17e24 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  in the file-syst
17e25 65 6d 2c 20 6f 70 65 6e 20 74 68 69 73 20 64 61  em, open this da
17e26 74 61 62 61 73 65 20 69 6e 20 57 41 4c 0a 20 20  tabase in WAL.  
17e27 20 20 2a 2a 20 6d 6f 64 65 2e 20 4f 74 68 65 72    ** mode. Other
17e28 77 69 73 65 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  wise, the follow
17e29 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  ing function cal
17e2a 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20  l is a no-op..  
17e2b 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61    */.    rc = pa
17e2c 67 65 72 4f 70 65 6e 57 61 6c 49 66 50 72 65 73  gerOpenWalIfPres
17e2d 65 6e 74 28 70 50 61 67 65 72 29 3b 0a 23 69 66  ent(pPager);.#if
17e2e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
17e2f 5f 57 41 4c 0a 20 20 20 20 61 73 73 65 72 74 28  _WAL.    assert(
17e30 20 70 50 61 67 65 72 2d 3e 70 57 61 6c 3d 3d 30   pPager->pWal==0
17e31 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   || rc==SQLITE_O
17e32 4b 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  K );.#endif.  }.
17e33 0a 20 20 69 66 28 20 70 61 67 65 72 55 73 65 57  .  if( pagerUseW
17e34 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20  al(pPager) ){.  
17e35 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
17e36 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72  LITE_OK );.    r
17e37 63 20 3d 20 70 61 67 65 72 42 65 67 69 6e 52 65  c = pagerBeginRe
17e38 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50  adTransaction(pP
17e39 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  ager);.  }..  if
17e3a 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65  ( pPager->eState
17e3b 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20 26 26 20  ==PAGER_OPEN && 
17e3c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
17e3d 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 50  .    rc = pagerP
17e3e 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
17e3f 20 26 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65   &pPager->dbSize
17e40 29 3b 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 64 3a  );.  }.. failed:
17e41 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
17e42 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65  E_OK ){.    asse
17e43 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20  rt( !MEMDB );.  
17e44 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70    pager_unlock(p
17e45 50 61 67 65 72 29 3b 0a 20 20 20 20 61 73 73 65  Pager);.    asse
17e46 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61  rt( pPager->eSta
17e47 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20 29  te==PAGER_OPEN )
17e48 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
17e49 50 61 67 65 72 2d 3e 65 53 74 61 74 65 20 3d 20  Pager->eState = 
17e4a 50 41 47 45 52 5f 52 45 41 44 45 52 3b 0a 20 20  PAGER_READER;.  
17e4b 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
17e4c 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72  ../*.** If the r
17e4d 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 68  eference count h
17e4e 61 73 20 72 65 61 63 68 65 64 20 7a 65 72 6f 2c  as reached zero,
17e4f 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63   rollback any ac
17e50 74 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74  tive.** transact
17e51 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74  ion and unlock t
17e52 68 65 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a 2a 20  he pager..**.** 
17e53 45 78 63 65 70 74 2c 20 69 6e 20 6c 6f 63 6b 69  Except, in locki
17e54 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 49 56  ng_mode=EXCLUSIV
17e55 45 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20  E when there is 
17e56 6e 6f 74 68 69 6e 67 20 74 6f 20 69 6e 0a 2a 2a  nothing to in.**
17e57 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
17e58 75 72 6e 61 6c 2c 20 74 68 65 20 75 6e 6c 6f 63  urnal, the unloc
17e59 6b 20 69 73 20 6e 6f 74 20 70 65 72 66 6f 72 6d  k is not perform
17e5a 65 64 20 61 6e 64 20 74 68 65 72 65 20 69 73 0a  ed and there is.
17e5b 2a 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 72 6f  ** nothing to ro
17e5c 6c 6c 62 61 63 6b 2c 20 73 6f 20 74 68 69 73 20  llback, so this 
17e5d 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
17e5e 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76  op..*/ .static v
17e5f 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49  oid pagerUnlockI
17e60 66 55 6e 75 73 65 64 28 50 61 67 65 72 20 2a 70  fUnused(Pager *p
17e61 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 28 73  Pager){.  if( (s
17e62 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43  qlite3PcacheRefC
17e63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43  ount(pPager->pPC
17e64 61 63 68 65 29 3d 3d 30 29 20 29 7b 0a 20 20 20  ache)==0) ){.   
17e65 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52   pagerUnlockAndR
17e66 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b  ollback(pPager);
17e67 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63  .  }.}../*.** Ac
17e68 71 75 69 72 65 20 61 20 72 65 66 65 72 65 6e 63  quire a referenc
17e69 65 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72  e to page number
17e6a 20 70 67 6e 6f 20 69 6e 20 70 61 67 65 72 20 70   pgno in pager p
17e6b 50 61 67 65 72 20 28 61 20 70 61 67 65 0a 2a 2a  Pager (a page.**
17e6c 20 72 65 66 65 72 65 6e 63 65 20 68 61 73 20 74   reference has t
17e6d 79 70 65 20 44 62 50 61 67 65 2a 29 2e 20 49 66  ype DbPage*). If
17e6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 72   the requested r
17e6f 65 66 65 72 65 6e 63 65 20 69 73 20 0a 2a 2a 20  eference is .** 
17e70 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 62 74  successfully obt
17e71 61 69 6e 65 64 2c 20 69 74 20 69 73 20 63 6f 70  ained, it is cop
17e72 69 65 64 20 74 6f 20 2a 70 70 50 61 67 65 20 61  ied to *ppPage a
17e73 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74  nd SQLITE_OK ret
17e74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
17e75 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61  the requested pa
17e76 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  ge is already in
17e77 20 74 68 65 20 63 61 63 68 65 2c 20 69 74 20 69   the cache, it i
17e78 73 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 20  s returned. .** 
17e79 4f 74 68 65 72 77 69 73 65 2c 20 61 20 6e 65 77  Otherwise, a new
17e7a 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20   page object is 
17e7b 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f  allocated and po
17e7c 70 75 6c 61 74 65 64 20 77 69 74 68 20 64 61 74  pulated with dat
17e7d 61 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 74  a.** read from t
17e7e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
17e7f 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c  . In some cases,
17e80 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75   the pcache modu
17e81 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 6f 6f 73 65  le may.** choose
17e82 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65   not to allocate
17e83 20 61 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65   a new page obje
17e84 63 74 20 61 6e 64 20 6d 61 79 20 72 65 75 73 65  ct and may reuse
17e85 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20   an existing.** 
17e86 6f 62 6a 65 63 74 20 77 69 74 68 20 6e 6f 20 6f  object with no o
17e87 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72  utstanding refer
17e88 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ences..**.** The
17e89 20 65 78 74 72 61 20 64 61 74 61 20 61 70 70 65   extra data appe
17e8a 6e 64 65 64 20 74 6f 20 61 20 70 61 67 65 20 69  nded to a page i
17e8b 73 20 61 6c 77 61 79 73 20 69 6e 69 74 69 61 6c  s always initial
17e8c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 20 74 68  ized to zeros th
17e8d 65 20 0a 2a 2a 20 66 69 72 73 74 20 74 69 6d 65  e .** first time
17e8e 20 61 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65   a page is loade
17e8f 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e 20 49  d into memory. I
17e90 66 20 74 68 65 20 70 61 67 65 20 72 65 71 75 65  f the page reque
17e91 73 74 65 64 20 69 73 20 0a 2a 2a 20 61 6c 72 65  sted is .** alre
17e92 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65  ady in the cache
17e93 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
17e94 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74  ion is called, t
17e95 68 65 6e 20 74 68 65 20 65 78 74 72 61 0a 2a 2a  hen the extra.**
17e96 20 64 61 74 61 20 69 73 20 6c 65 66 74 20 61 73   data is left as
17e97 20 69 74 20 77 61 73 20 77 68 65 6e 20 74 68 65   it was when the
17e98 20 70 61 67 65 20 6f 62 6a 65 63 74 20 77 61 73   page object was
17e99 20 6c 61 73 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a   last used..**.*
17e9a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
17e9b 65 20 69 6d 61 67 65 20 69 73 20 73 6d 61 6c 6c  e image is small
17e9c 65 72 20 74 68 61 6e 20 74 68 65 20 72 65 71 75  er than the requ
17e9d 65 73 74 65 64 20 70 61 67 65 20 6f 72 20 69 66  ested page or if
17e9e 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20   a .** non-zero 
17e9f 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 64 20  value is passed 
17ea0 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74  as the noContent
17ea1 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 74   parameter and t
17ea2 68 65 20 0a 2a 2a 20 72 65 71 75 65 73 74 65 64  he .** requested
17ea3 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72   page is not alr
17ea4 65 61 64 79 20 73 74 6f 72 65 64 20 69 6e 20 74  eady stored in t
17ea5 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20 6e  he cache, then n
17ea6 6f 20 0a 2a 2a 20 61 63 74 75 61 6c 20 64 69 73  o .** actual dis
17ea7 6b 20 72 65 61 64 20 6f 63 63 75 72 73 2e 20 49  k read occurs. I
17ea8 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
17ea9 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20  memory image of 
17eaa 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 69 73 20  the .** page is 
17eab 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61  initialized to a
17eac 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a  ll zeros. .**.**
17ead 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73   If noContent is
17eae 20 74 72 75 65 2c 20 69 74 20 6d 65 61 6e 73 20   true, it means 
17eaf 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 63  that we do not c
17eb0 61 72 65 20 61 62 6f 75 74 20 74 68 65 20 63 6f  are about the co
17eb1 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65  ntents.** of the
17eb2 20 70 61 67 65 2e 20 54 68 69 73 20 6f 63 63 75   page. This occu
17eb3 72 73 20 69 6e 20 74 77 6f 20 73 65 70 65 72 61  rs in two sepera
17eb4 74 65 20 73 63 65 6e 61 72 69 6f 73 3a 0a 2a 2a  te scenarios:.**
17eb5 0a 2a 2a 20 20 20 61 29 20 57 68 65 6e 20 72 65  .**   a) When re
17eb6 61 64 69 6e 67 20 61 20 66 72 65 65 2d 6c 69 73  ading a free-lis
17eb7 74 20 6c 65 61 66 20 70 61 67 65 20 66 72 6f 6d  t leaf page from
17eb8 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 61   the database, a
17eb9 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 57 68  nd.**.**   b) Wh
17eba 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 20 69  en a savepoint i
17ebb 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62  s being rolled b
17ebc 61 63 6b 20 61 6e 64 20 77 65 20 6e 65 65 64 20  ack and we need 
17ebd 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 20 20 20 20  to load.**      
17ebe 61 20 6e 65 77 20 70 61 67 65 20 69 6e 74 6f 20  a new page into 
17ebf 74 68 65 20 63 61 63 68 65 20 74 6f 20 62 65 20  the cache to be 
17ec0 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20  filled with the 
17ec1 64 61 74 61 20 72 65 61 64 0a 2a 2a 20 20 20 20  data read.**    
17ec2 20 20 66 72 6f 6d 20 74 68 65 20 73 61 76 65 70    from the savep
17ec3 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a  oint journal..**
17ec4 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74  .** If noContent
17ec5 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
17ec6 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64  he data returned
17ec7 20 69 73 20 7a 65 72 6f 65 64 20 69 6e 73 74 65   is zeroed inste
17ec8 61 64 20 6f 66 0a 2a 2a 20 62 65 69 6e 67 20 72  ad of.** being r
17ec9 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74  ead from the dat
17eca 61 62 61 73 65 2e 20 41 64 64 69 74 69 6f 6e 61  abase. Additiona
17ecb 6c 6c 79 2c 20 74 68 65 20 62 69 74 73 20 63 6f  lly, the bits co
17ecc 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74  rresponding.** t
17ecd 6f 20 70 67 6e 6f 20 69 6e 20 50 61 67 65 72 2e  o pgno in Pager.
17ece 70 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69 74 76  pInJournal (bitv
17ecf 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72 65  ec of pages alre
17ed0 61 64 79 20 77 72 69 74 74 65 6e 20 74 6f 20 74  ady written to t
17ed1 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  he.** journal fi
17ed2 6c 65 29 20 61 6e 64 20 74 68 65 20 50 61 67 65  le) and the Page
17ed3 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61  rSavepoint.pInSa
17ed4 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 20  vepoint bitvecs 
17ed5 6f 66 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 73  of any open.** s
17ed6 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 65  avepoints are se
17ed7 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 66  t. This means if
17ed8 20 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 64   the page is mad
17ed9 65 20 77 72 69 74 61 62 6c 65 20 61 74 20 61 6e  e writable at an
17eda 79 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 74 68  y.** point in th
17edb 65 20 66 75 74 75 72 65 2c 20 75 73 69 6e 67 20  e future, using 
17edc 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
17edd 33 50 61 67 65 72 57 72 69 74 65 28 29 2c 20 69  3PagerWrite(), i
17ede 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 77  ts contents.** w
17edf 69 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f 75 72 6e  ill not be journ
17ee0 61 6c 65 64 2e 20 54 68 69 73 20 73 61 76 65 73  aled. This saves
17ee1 20 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61   IO..**.** The a
17ee2 63 71 75 69 73 69 74 69 6f 6e 20 6d 69 67 68 74  cquisition might
17ee3 20 66 61 69 6c 20 66 6f 72 20 73 65 76 65 72 61   fail for severa
17ee4 6c 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e 20 61  l reasons.  In a
17ee5 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61 6e 20  ll cases,.** an 
17ee6 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f  appropriate erro
17ee7 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
17ee8 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 20 69  ed and *ppPage i
17ee9 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a  s set to NULL..*
17eea 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71  *.** See also sq
17eeb 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70  lite3PagerLookup
17eec 28 29 2e 20 20 42 6f 74 68 20 74 68 69 73 20 72  ().  Both this r
17eed 6f 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b 75  outine and Looku
17eee 70 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74  p() attempt.** t
17eef 6f 20 66 69 6e 64 20 61 20 70 61 67 65 20 69 6e  o find a page in
17ef0 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63   the in-memory c
17ef1 61 63 68 65 20 66 69 72 73 74 2e 20 20 49 66 20  ache first.  If 
17ef2 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20  the page is not 
17ef3 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 6d 65  already.** in me
17ef4 6d 6f 72 79 2c 20 74 68 69 73 20 72 6f 75 74 69  mory, this routi
17ef5 6e 65 20 67 6f 65 73 20 74 6f 20 64 69 73 6b 20  ne goes to disk 
17ef6 74 6f 20 72 65 61 64 20 69 74 20 69 6e 20 77 68  to read it in wh
17ef7 65 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29 0a 2a  ereas Lookup().*
17ef8 2a 20 6a 75 73 74 20 72 65 74 75 72 6e 73 20 30  * just returns 0
17ef9 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
17efa 61 63 71 75 69 72 65 73 20 61 20 72 65 61 64 2d  acquires a read-
17efb 6c 6f 63 6b 20 74 68 65 20 66 69 72 73 74 20 74  lock the first t
17efc 69 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20 74 6f  ime it.** has to
17efd 20 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61 6e 64   go to disk, and
17efe 20 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c 61 79   could also play
17eff 62 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 72  back an old jour
17f00 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72 79  nal if necessary
17f01 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b 75  ..** Since Looku
17f02 70 28 29 20 6e 65 76 65 72 20 67 6f 65 73 20 74  p() never goes t
17f03 6f 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 65 72  o disk, it never
17f04 20 68 61 73 20 74 6f 20 64 65 61 6c 20 77 69 74   has to deal wit
17f05 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a 6f  h locks.** or jo
17f06 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a  urnal files..*/.
17f07 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
17f08 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 41  nt sqlite3PagerA
17f09 63 71 75 69 72 65 28 0a 20 20 50 61 67 65 72 20  cquire(.  Pager 
17f0a 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 2f 2a  *pPager,      /*
17f0b 20 54 68 65 20 70 61 67 65 72 20 6f 70 65 6e 20   The pager open 
17f0c 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
17f0d 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  file */.  Pgno p
17f0e 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  gno,          /*
17f0f 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20   Page number to 
17f10 66 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 61 67  fetch */.  DbPag
17f11 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f  e **ppPage,    /
17f12 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65  * Write a pointe
17f13 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65  r to the page he
17f14 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f  re */.  int noCo
17f15 6e 74 65 6e 74 20 20 20 20 20 20 20 2f 2a 20 44  ntent       /* D
17f16 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 65 61  o not bother rea
17f17 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f  ding content fro
17f18 6d 20 64 69 73 6b 20 69 66 20 74 72 75 65 20 2a  m disk if true *
17f19 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
17f1a 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 20 20   PgHdr *pPg;..  
17f1b 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
17f1c 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45  eState>=PAGER_RE
17f1d 41 44 45 52 20 29 3b 0a 20 20 61 73 73 65 72 74  ADER );.  assert
17f1e 28 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73  ( assert_pager_s
17f1f 74 61 74 65 28 70 50 61 67 65 72 29 20 29 3b 0a  tate(pPager) );.
17f20 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29  .  if( pgno==0 )
17f21 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
17f22 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
17f23 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
17f24 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 74  he pager is in t
17f25 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20  he error state, 
17f26 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
17f27 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a 20 20  immediately. .  
17f28 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65  ** Otherwise, re
17f29 71 75 65 73 74 20 74 68 65 20 70 61 67 65 20 66  quest the page f
17f2a 72 6f 6d 20 74 68 65 20 50 43 61 63 68 65 20 6c  rom the PCache l
17f2b 61 79 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70  ayer. */.  if( p
17f2c 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d  Pager->errCode!=
17f2d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
17f2e 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72   rc = pPager->er
17f2f 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rCode;.  }else{.
17f30 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17f31 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67  PcacheFetch(pPag
17f32 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e  er->pPCache, pgn
17f33 6f 2c 20 31 2c 20 70 70 50 61 67 65 29 3b 0a 20  o, 1, ppPage);. 
17f34 20 7d 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51   }..  if( rc!=SQ
17f35 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f  LITE_OK ){.    /
17f36 2a 20 45 69 74 68 65 72 20 74 68 65 20 63 61 6c  * Either the cal
17f37 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 63 61 63  l to sqlite3Pcac
17f38 68 65 46 65 74 63 68 28 29 20 72 65 74 75 72 6e  heFetch() return
17f39 65 64 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 74  ed an error or t
17f3a 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 20  he.    ** pager 
17f3b 77 61 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74  was already in t
17f3c 68 65 20 65 72 72 6f 72 2d 73 74 61 74 65 20 77  he error-state w
17f3d 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
17f3e 6e 20 77 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20  n was called..  
17f3f 20 20 2a 2a 20 53 65 74 20 70 50 67 20 74 6f 20    ** Set pPg to 
17f40 30 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 74 68  0 and jump to th
17f41 65 20 65 78 63 65 70 74 69 6f 6e 20 68 61 6e 64  e exception hand
17f42 6c 65 72 2e 20 20 2a 2f 0a 20 20 20 20 70 50 67  ler.  */.    pPg
17f43 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 70   = 0;.    goto p
17f44 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72  ager_acquire_err
17f45 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
17f46 28 2a 70 70 50 61 67 65 29 2d 3e 70 67 6e 6f 3d  (*ppPage)->pgno=
17f47 3d 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72  =pgno );.  asser
17f48 74 28 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50  t( (*ppPage)->pP
17f49 61 67 65 72 3d 3d 70 50 61 67 65 72 20 7c 7c 20  ager==pPager || 
17f4a 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65  (*ppPage)->pPage
17f4b 72 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 28  r==0 );..  if( (
17f4c 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72  *ppPage)->pPager
17f4d 20 26 26 20 21 6e 6f 43 6f 6e 74 65 6e 74 20 29   && !noContent )
17f4e 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73  {.    /* In this
17f4f 20 63 61 73 65 20 74 68 65 20 70 63 61 63 68 65   case the pcache
17f50 20 61 6c 72 65 61 64 79 20 63 6f 6e 74 61 69 6e   already contain
17f51 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  s an initialized
17f52 20 63 6f 70 79 20 6f 66 0a 20 20 20 20 2a 2a 20   copy of.    ** 
17f53 74 68 65 20 70 61 67 65 2e 20 52 65 74 75 72 6e  the page. Return
17f54 20 77 69 74 68 6f 75 74 20 66 75 72 74 68 65 72   without further
17f55 20 61 64 6f 2e 20 20 2a 2f 0a 20 20 20 20 61 73   ado.  */.    as
17f56 73 65 72 74 28 20 70 67 6e 6f 3c 3d 50 41 47 45  sert( pgno<=PAGE
17f57 52 5f 4d 41 58 5f 50 47 4e 4f 20 26 26 20 70 67  R_MAX_PGNO && pg
17f58 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  no!=PAGER_MJ_PGN
17f59 4f 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20  O(pPager) );.   
17f5a 20 70 50 61 67 65 72 2d 3e 61 53 74 61 74 5b 50   pPager->aStat[P
17f5b 41 47 45 52 5f 53 54 41 54 5f 48 49 54 5d 2b 2b  AGER_STAT_HIT]++
17f5c 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
17f5d 49 54 45 5f 4f 4b 3b 0a 0a 20 20 7d 65 6c 73 65  ITE_OK;..  }else
17f5e 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67  {.    /* The pag
17f5f 65 72 20 63 61 63 68 65 20 68 61 73 20 63 72 65  er cache has cre
17f60 61 74 65 64 20 61 20 6e 65 77 20 70 61 67 65 2e  ated a new page.
17f61 20 49 74 73 20 63 6f 6e 74 65 6e 74 20 6e 65 65   Its content nee
17f62 64 73 20 74 6f 20 0a 20 20 20 20 2a 2a 20 62 65  ds to .    ** be
17f63 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 2a   initialized.  *
17f64 2f 0a 0a 20 20 20 20 70 50 67 20 3d 20 2a 70 70  /..    pPg = *pp
17f65 50 61 67 65 3b 0a 20 20 20 20 70 50 67 2d 3e 70  Page;.    pPg->p
17f66 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a  Pager = pPager;.
17f67 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 69  .    /* The maxi
17f68 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62 65 72 20  mum page number 
17f69 69 73 20 32 5e 33 31 2e 20 52 65 74 75 72 6e 20  is 2^31. Return 
17f6a 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69  SQLITE_CORRUPT i
17f6b 66 20 61 20 70 61 67 65 0a 20 20 20 20 2a 2a 20  f a page.    ** 
17f6c 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 74  number greater t
17f6d 68 61 6e 20 74 68 69 73 2c 20 6f 72 20 74 68 65  han this, or the
17f6e 20 75 6e 75 73 65 64 20 6c 6f 63 6b 69 6e 67 2d   unused locking-
17f6f 70 61 67 65 2c 20 69 73 20 72 65 71 75 65 73 74  page, is request
17f70 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  ed. */.    if( p
17f71 67 6e 6f 3e 50 41 47 45 52 5f 4d 41 58 5f 50 47  gno>PAGER_MAX_PG
17f72 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45  NO || pgno==PAGE
17f73 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72  R_MJ_PGNO(pPager
17f74 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  ) ){.      rc = 
17f75 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
17f76 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  KPT;.      goto 
17f77 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72  pager_acquire_er
17f78 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  r;.    }..    if
17f79 28 20 4d 45 4d 44 42 20 7c 7c 20 70 50 61 67 65  ( MEMDB || pPage
17f7a 72 2d 3e 64 62 53 69 7a 65 3c 70 67 6e 6f 20 7c  r->dbSize<pgno |
17f7b 7c 20 6e 6f 43 6f 6e 74 65 6e 74 20 7c 7c 20 21  | noContent || !
17f7c 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
17f7d 64 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  d) ){.      if( 
17f7e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6d 78 50  pgno>pPager->mxP
17f7f 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 72  gno ){.        r
17f80 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b  c = SQLITE_FULL;
17f81 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 70 61  .        goto pa
17f82 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b  ger_acquire_err;
17f83 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
17f84 66 28 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a  f( noContent ){.
17f85 20 20 20 20 20 20 20 20 2f 2a 20 46 61 69 6c 75          /* Failu
17f86 72 65 20 74 6f 20 73 65 74 20 74 68 65 20 62 69  re to set the bi
17f87 74 73 20 69 6e 20 74 68 65 20 49 6e 4a 6f 75 72  ts in the InJour
17f88 6e 61 6c 20 62 69 74 2d 76 65 63 74 6f 72 73 20  nal bit-vectors 
17f89 69 73 20 62 65 6e 69 67 6e 2e 0a 20 20 20 20 20  is benign..     
17f8a 20 20 20 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20     ** It merely 
17f8b 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 6d 69  means that we mi
17f8c 67 68 74 20 64 6f 20 73 6f 6d 65 20 65 78 74 72  ght do some extr
17f8d 61 20 77 6f 72 6b 20 74 6f 20 6a 6f 75 72 6e 61  a work to journa
17f8e 6c 20 61 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  l a .        ** 
17f8f 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 6e  page that does n
17f90 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6a 6f  ot need to be jo
17f91 75 72 6e 61 6c 65 64 2e 20 20 4e 65 76 65 72 74  urnaled.  Nevert
17f92 68 65 6c 65 73 73 2c 20 62 65 20 73 75 72 65 20  heless, be sure 
17f93 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74  .        ** to t
17f94 65 73 74 20 74 68 65 20 63 61 73 65 20 77 68 65  est the case whe
17f95 72 65 20 61 20 6d 61 6c 6c 6f 63 20 65 72 72 6f  re a malloc erro
17f96 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74  r occurs while t
17f97 72 79 69 6e 67 20 74 6f 20 73 65 74 20 0a 20 20  rying to set .  
17f98 20 20 20 20 20 20 2a 2a 20 61 20 62 69 74 20 69        ** a bit i
17f99 6e 20 61 20 62 69 74 20 76 65 63 74 6f 72 2e 0a  n a bit vector..
17f9a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
17f9b 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42     sqlite3BeginB
17f9c 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
17f9d 20 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3c         if( pgno<
17f9e 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53  =pPager->dbOrigS
17f9f 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ize ){.         
17fa0 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20   TESTONLY( rc = 
17fa1 29 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53  ) sqlite3BitvecS
17fa2 65 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  et(pPager->pInJo
17fa3 75 72 6e 61 6c 2c 20 70 67 6e 6f 29 3b 0a 20 20  urnal, pgno);.  
17fa4 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
17fa5 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
17fa6 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  EM );.        }.
17fa7 20 20 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59          TESTONLY
17fa8 28 20 72 63 20 3d 20 29 20 61 64 64 54 6f 53 61  ( rc = ) addToSa
17fa9 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70  vepointBitvecs(p
17faa 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20  Pager, pgno);.  
17fab 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
17fac 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
17fad 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
17fae 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
17faf 6f 63 28 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  oc();.      }.  
17fb0 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e      memset(pPg->
17fb1 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65 72  pData, 0, pPager
17fb2 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20  ->pageSize);.   
17fb3 20 20 20 49 4f 54 52 41 43 45 28 28 22 5a 45 52     IOTRACE(("ZER
17fb4 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  O %p %d\n", pPag
17fb5 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20  er, pgno));.    
17fb6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
17fb7 65 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72  ert( pPg->pPager
17fb8 3d 3d 70 50 61 67 65 72 20 29 3b 0a 20 20 20 20  ==pPager );.    
17fb9 20 20 70 50 61 67 65 72 2d 3e 61 53 74 61 74 5b    pPager->aStat[
17fba 50 41 47 45 52 5f 53 54 41 54 5f 4d 49 53 53 5d  PAGER_STAT_MISS]
17fbb 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72  ++;.      rc = r
17fbc 65 61 64 44 62 50 61 67 65 28 70 50 67 29 3b 0a  eadDbPage(pPg);.
17fbd 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
17fbe 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17fbf 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63     goto pager_ac
17fc0 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 20  quire_err;.     
17fc1 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67   }.    }.    pag
17fc2 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28  er_set_pagehash(
17fc3 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  pPg);.  }..  ret
17fc4 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  urn SQLITE_OK;..
17fc5 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72  pager_acquire_er
17fc6 72 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  r:.  assert( rc!
17fc7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
17fc8 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 73  if( pPg ){.    s
17fc9 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f 70  qlite3PcacheDrop
17fca 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 70 61 67  (pPg);.  }.  pag
17fcb 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64  erUnlockIfUnused
17fcc 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 2a 70 70  (pPager);..  *pp
17fcd 50 61 67 65 20 3d 20 30 3b 0a 20 20 72 65 74 75  Page = 0;.  retu
17fce 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
17fcf 41 63 71 75 69 72 65 20 61 20 70 61 67 65 20 69  Acquire a page i
17fd0 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 20  f it is already 
17fd1 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  in the in-memory
17fd2 20 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e   cache.  Do.** n
17fd3 6f 74 20 72 65 61 64 20 74 68 65 20 70 61 67 65  ot read the page
17fd4 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 74   from disk.  Ret
17fd5 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
17fd6 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f 72   the page,.** or
17fd7 20 30 20 69 66 20 74 68 65 20 70 61 67 65 20 69   0 if the page i
17fd8 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e 20  s not in cache. 
17fd9 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20  .**.** See also 
17fda 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
17fdb 29 2e 20 20 54 68 65 20 64 69 66 66 65 72 65 6e  ).  The differen
17fdc 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 20  ce between this 
17fdd 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73  routine.** and s
17fde 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29  qlite3PagerGet()
17fdf 20 69 73 20 74 68 61 74 20 5f 67 65 74 28 29 20   is that _get() 
17fe0 77 69 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64  will go to the d
17fe1 69 73 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20  isk and read.** 
17fe2 69 6e 20 74 68 65 20 70 61 67 65 20 69 66 20 74  in the page if t
17fe3 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  he page is not a
17fe4 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e  lready in cache.
17fe5 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a    This routine.*
17fe6 2a 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69  * returns NULL i
17fe7 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  f the page is no
17fe8 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66  t in cache or if
17fe9 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f   a disk I/O erro
17fea 72 20 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68  r .** has ever h
17feb 61 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  appened..*/.SQLI
17fec 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 61 67  TE_PRIVATE DbPag
17fed 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c  e *sqlite3PagerL
17fee 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61  ookup(Pager *pPa
17fef 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b  ger, Pgno pgno){
17ff0 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20  .  PgHdr *pPg = 
17ff1 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  0;.  assert( pPa
17ff2 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ger!=0 );.  asse
17ff3 72 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 20  rt( pgno!=0 );. 
17ff4 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
17ff5 3e 70 50 43 61 63 68 65 21 3d 30 20 29 3b 0a 20  >pPCache!=0 );. 
17ff6 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
17ff7 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 52  >eState>=PAGER_R
17ff8 45 41 44 45 52 20 26 26 20 70 50 61 67 65 72 2d  EADER && pPager-
17ff9 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52 5f 45  >eState!=PAGER_E
17ffa 52 52 4f 52 20 29 3b 0a 20 20 73 71 6c 69 74 65  RROR );.  sqlite
17ffb 33 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61  3PcacheFetch(pPa
17ffc 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67  ger->pPCache, pg
17ffd 6e 6f 2c 20 30 2c 20 26 70 50 67 29 3b 0a 20 20  no, 0, &pPg);.  
17ffe 72 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f  return pPg;.}../
17fff 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 70  *.** Release a p
18000 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a  age reference..*
18001 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62  *.** If the numb
18002 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73  er of references
18003 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f   to the page dro
18004 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20  p to zero, then 
18005 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61  the.** page is a
18006 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20  dded to the LRU 
18007 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20  list.  When all 
18008 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c  references to al
18009 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72  l pages.** are r
1800a 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62  eleased, a rollb
1800b 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74  ack occurs and t
1800c 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  he lock on the d
1800d 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65  atabase is.** re
1800e 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  moved..*/.SQLITE
1800f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
18010 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
18011 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20  DbPage *pPg){.  
18012 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50  if( pPg ){.    P
18013 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70  ager *pPager = p
18014 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20  Pg->pPager;.    
18015 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c  sqlite3PcacheRel
18016 65 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70  ease(pPg);.    p
18017 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73  agerUnlockIfUnus
18018 65 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a  ed(pPager);.  }.
18019 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
1801a 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
1801b 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
1801c 20 65 76 65 72 79 20 77 72 69 74 65 20 74 72 61   every write tra
1801d 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65  nsaction..** The
1801e 72 65 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20  re must already 
1801f 62 65 20 61 20 52 45 53 45 52 56 45 44 20 6f 72  be a RESERVED or
18020 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
18021 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
18022 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e 20 74 68  .** file when th
18023 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
18024 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e  lled..**.** Open
18025 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
18026 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 61 67  e for pager pPag
18027 65 72 20 61 6e 64 20 77 72 69 74 65 20 61 20 6a  er and write a j
18028 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a 2a  ournal header.**
18029 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
1802a 20 69 74 2e 20 49 66 20 74 68 65 72 65 20 61 72   it. If there ar
1802b 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f 69  e active savepoi
1802c 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65 20 73 75  nts, open the su
1802d 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 61 73 20  b-journal.** as 
1802e 77 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e 63 74  well. This funct
1802f 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  ion is only used
18030 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   when the journa
18031 6c 20 66 69 6c 65 20 69 73 20 62 65 69 6e 67 20  l file is being 
18032 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f 20 77 72  .** opened to wr
18033 69 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6c  ite a rollback l
18034 6f 67 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63  og for a transac
18035 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e 6f 74 20  tion. It is not 
18036 75 73 65 64 20 0a 2a 2a 20 77 68 65 6e 20 6f 70  used .** when op
18037 65 6e 69 6e 67 20 61 20 68 6f 74 20 6a 6f 75 72  ening a hot jour
18038 6e 61 6c 20 66 69 6c 65 20 74 6f 20 72 6f 6c 6c  nal file to roll
18039 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20   it back..**.** 
1803a 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  If the journal f
1803b 69 6c 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f  ile is already o
1803c 70 65 6e 20 28 61 73 20 69 74 20 6d 61 79 20 62  pen (as it may b
1803d 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d  e in exclusive m
1803e 6f 64 65 29 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  ode),.** then th
1803f 69 73 20 66 75 6e 63 74 69 6f 6e 20 6a 75 73 74  is function just
18040 20 77 72 69 74 65 73 20 61 20 6a 6f 75 72 6e 61   writes a journa
18041 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68 65 20  l header to the 
18042 73 74 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20  start of the.** 
18043 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 66 69 6c  already open fil
18044 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65  e. .**.** Whethe
18045 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75  r or not the jou
18046 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65  rnal file is ope
18047 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63  ned by this func
18048 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20 50 61 67  tion, the.** Pag
18049 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 69  er.pInJournal bi
1804a 74 76 65 63 20 73 74 72 75 63 74 75 72 65 20 69  tvec structure i
1804b 73 20 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a  s allocated..**.
1804c 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
1804d 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e  _OK if everythin
1804e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e  g is successful.
1804f 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75   Otherwise, retu
18050 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f  rn .** SQLITE_NO
18051 4d 45 4d 20 69 66 20 74 68 65 20 61 74 74 65 6d  MEM if the attem
18052 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 50  pt to allocate P
18053 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20  ager.pInJournal 
18054 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 61 6e  fails, or .** an
18055 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69   IO error code i
18056 66 20 6f 70 65 6e 69 6e 67 20 6f 72 20 77 72 69  f opening or wri
18057 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c  ting the journal
18058 20 66 69 6c 65 20 66 61 69 6c 73 2e 0a 2a 2f 0a   file fails..*/.
18059 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
1805a 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 50 61  _open_journal(Pa
1805b 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
1805c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1805d 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  OK;             
1805e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
1805f 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73  turn code */.  s
18060 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e  qlite3_vfs * con
18061 73 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 72  st pVfs = pPager
18062 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20 4c 6f 63  ->pVfs;   /* Loc
18063 61 6c 20 63 61 63 68 65 20 6f 66 20 76 66 73 20  al cache of vfs 
18064 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a 20 20 61 73  pointer */..  as
18065 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53  sert( pPager->eS
18066 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54  tate==PAGER_WRIT
18067 45 52 5f 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 61  ER_LOCKED );.  a
18068 73 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61  ssert( assert_pa
18069 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72  ger_state(pPager
1806a 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
1806b 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
1806c 6c 3d 3d 30 20 29 3b 0a 20 20 0a 20 20 2f 2a 20  l==0 );.  .  /* 
1806d 49 66 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68  If already in th
1806e 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 74  e error state, t
1806f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
18070 61 20 6e 6f 2d 6f 70 2e 20 20 42 75 74 20 6f 6e  a no-op.  But on
18071 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 72 20  .  ** the other 
18072 68 61 6e 64 2c 20 74 68 69 73 20 72 6f 75 74 69  hand, this routi
18073 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c  ne is never call
18074 65 64 20 69 66 20 77 65 20 61 72 65 20 61 6c 72  ed if we are alr
18075 65 61 64 79 20 69 6e 0a 20 20 2a 2a 20 61 6e 20  eady in.  ** an 
18076 65 72 72 6f 72 20 73 74 61 74 65 2e 20 2a 2f 0a  error state. */.
18077 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67    if( NEVER(pPag
18078 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 72  er->errCode) ) r
18079 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72  eturn pPager->er
1807a 72 43 6f 64 65 3b 0a 0a 20 20 69 66 28 20 21 70  rCode;..  if( !p
1807b 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65  agerUseWal(pPage
1807c 72 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f  r) && pPager->jo
1807d 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52  urnalMode!=PAGER
1807e 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46  _JOURNALMODE_OFF
1807f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
18080 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c  pInJournal = sql
18081 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65  ite3BitvecCreate
18082 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29  (pPager->dbSize)
18083 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  ;.    if( pPager
18084 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20  ->pInJournal==0 
18085 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
18086 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
18087 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 4f 70    }.  .    /* Op
18088 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  en the journal f
18089 69 6c 65 20 69 66 20 69 74 20 69 73 20 6e 6f 74  ile if it is not
1808a 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 2e 20 2a   already open. *
1808b 2f 0a 20 20 20 20 69 66 28 20 21 69 73 4f 70 65  /.    if( !isOpe
1808c 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29  n(pPager->jfd) )
1808d 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67  {.      if( pPag
1808e 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  er->journalMode=
1808f 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
18090 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20  DE_MEMORY ){.   
18091 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a       sqlite3MemJ
18092 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65  ournalOpen(pPage
18093 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 7d  r->jfd);.      }
18094 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f  else{.        co
18095 6e 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20  nst int flags = 
18096 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18097 20 20 2f 2a 20 56 46 53 20 66 6c 61 67 73 20 74    /* VFS flags t
18098 6f 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66  o open journal f
18099 69 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ile */.         
1809a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
1809b 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50  DWRITE|SQLITE_OP
1809c 45 4e 5f 43 52 45 41 54 45 7c 0a 20 20 20 20 20  EN_CREATE|.     
1809d 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 74 65       (pPager->te
1809e 6d 70 46 69 6c 65 20 3f 20 0a 20 20 20 20 20 20  mpFile ? .      
1809f 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50        (SQLITE_OP
180a0 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
180a1 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d  |SQLITE_OPEN_TEM
180a2 50 5f 4a 4f 55 52 4e 41 4c 29 3a 0a 20 20 20 20  P_JOURNAL):.    
180a3 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
180a4 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41  OPEN_MAIN_JOURNA
180a5 4c 29 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a  L).          );.
180a6 20 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f    #ifdef SQLITE_
180a7 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52  ENABLE_ATOMIC_WR
180a8 49 54 45 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ITE.        rc =
180a9 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f   sqlite3JournalO
180aa 70 65 6e 28 0a 20 20 20 20 20 20 20 20 20 20 20  pen(.           
180ab 20 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a   pVfs, pPager->z
180ac 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d  Journal, pPager-
180ad 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 6a 72 6e  >jfd, flags, jrn
180ae 6c 42 75 66 66 65 72 53 69 7a 65 28 70 50 61 67  lBufferSize(pPag
180af 65 72 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 20  er).        );. 
180b0 20 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 72   #else.        r
180b1 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
180b2 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  n(pVfs, pPager->
180b3 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72  zJournal, pPager
180b4 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30 29  ->jfd, flags, 0)
180b5 3b 0a 20 20 23 65 6e 64 69 66 0a 20 20 20 20 20  ;.  #endif.     
180b6 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
180b7 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
180b8 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  | isOpen(pPager-
180b9 3e 6a 66 64 29 20 29 3b 0a 20 20 20 20 7d 0a 20  >jfd) );.    }. 
180ba 20 0a 20 20 0a 20 20 20 20 2f 2a 20 57 72 69 74   .  .    /* Writ
180bb 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72  e the first jour
180bc 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68  nal header to th
180bd 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  e journal file a
180be 6e 64 20 6f 70 65 6e 20 0a 20 20 20 20 2a 2a 20  nd open .    ** 
180bf 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20  the sub-journal 
180c0 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20  if necessary..  
180c1 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d    */.    if( rc=
180c2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
180c3 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 65      /* TODO: Che
180c4 63 6b 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65  ck if all of the
180c5 73 65 20 61 72 65 20 72 65 61 6c 6c 79 20 72 65  se are really re
180c6 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 20 20 20  quired. */.     
180c7 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20   pPager->nRec = 
180c8 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  0;.      pPager-
180c9 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b  >journalOff = 0;
180ca 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73  .      pPager->s
180cb 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20  etMaster = 0;.  
180cc 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
180cd 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 20 20 20  nalHdr = 0;.    
180ce 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72    rc = writeJour
180cf 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a  nalHdr(pPager);.
180d0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
180d1 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
180d2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74  {.    sqlite3Bit
180d3 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65  vecDestroy(pPage
180d4 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a  r->pInJournal);.
180d5 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a      pPager->pInJ
180d6 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 7d 65  ournal = 0;.  }e
180d7 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
180d8 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d   pPager->eState=
180d9 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f  =PAGER_WRITER_LO
180da 43 4b 45 44 20 29 3b 0a 20 20 20 20 70 50 61 67  CKED );.    pPag
180db 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41 47  er->eState = PAG
180dc 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 45 4d  ER_WRITER_CACHEM
180dd 4f 44 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  OD;.  }..  retur
180de 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42  n rc;.}../*.** B
180df 65 67 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61  egin a write-tra
180e0 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20  nsaction on the 
180e1 73 70 65 63 69 66 69 65 64 20 70 61 67 65 72 20  specified pager 
180e2 6f 62 6a 65 63 74 2e 20 49 66 20 61 20 0a 2a 2a  object. If a .**
180e3 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
180e4 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  on has already b
180e5 65 65 6e 20 6f 70 65 6e 65 64 2c 20 74 68 69 73  een opened, this
180e6 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
180e7 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  o-op..**.** If t
180e8 68 65 20 65 78 46 6c 61 67 20 61 72 67 75 6d 65  he exFlag argume
180e9 6e 74 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65  nt is false, the
180ea 6e 20 61 63 71 75 69 72 65 20 61 74 20 6c 65 61  n acquire at lea
180eb 73 74 20 61 20 52 45 53 45 52 56 45 44 0a 2a 2a  st a RESERVED.**
180ec 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
180ed 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 65  abase file. If e
180ee 78 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74  xFlag is true, t
180ef 68 65 6e 20 61 63 71 75 69 72 65 20 61 74 20 6c  hen acquire at l
180f0 65 61 73 74 0a 2a 2a 20 61 6e 20 45 58 43 4c 55  east.** an EXCLU
180f1 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 73 75  SIVE lock. If su
180f2 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 61 6c 72  ch a lock is alr
180f3 65 61 64 79 20 68 65 6c 64 2c 20 6e 6f 20 6c 6f  eady held, no lo
180f4 63 6b 69 6e 67 20 0a 2a 2a 20 66 75 6e 63 74 69  cking .** functi
180f5 6f 6e 73 20 6e 65 65 64 20 62 65 20 63 61 6c 6c  ons need be call
180f6 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
180f7 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 72   subjInMemory ar
180f8 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 2d 7a 65  gument is non-ze
180f9 72 6f 2c 20 74 68 65 6e 20 61 6e 79 20 73 75 62  ro, then any sub
180fa 2d 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 65 64 0a  -journal opened.
180fb 2a 2a 20 77 69 74 68 69 6e 20 74 68 69 73 20 74  ** within this t
180fc 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20  ransaction will 
180fd 62 65 20 6f 70 65 6e 65 64 20 61 73 20 61 6e 20  be opened as an 
180fe 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2e 20  in-memory file. 
180ff 54 68 69 73 0a 2a 2a 20 68 61 73 20 6e 6f 20 65  This.** has no e
18100 66 66 65 63 74 20 69 66 20 74 68 65 20 73 75 62  ffect if the sub
18101 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 72 65  -journal is alre
18102 61 64 79 20 6f 70 65 6e 65 64 20 28 61 73 20 69  ady opened (as i
18103 74 20 6d 61 79 20 62 65 20 77 68 65 6e 0a 2a 2a  t may be when.**
18104 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c   running in excl
18105 75 73 69 76 65 20 6d 6f 64 65 29 20 6f 72 20 69  usive mode) or i
18106 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  f the transactio
18107 6e 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 69  n does not requi
18108 72 65 20 61 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72  re a.** sub-jour
18109 6e 61 6c 2e 20 49 66 20 74 68 65 20 73 75 62 6a  nal. If the subj
1810a 49 6e 4d 65 6d 6f 72 79 20 61 72 67 75 6d 65 6e  InMemory argumen
1810b 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20  t is zero, then 
1810c 61 6e 79 20 72 65 71 75 69 72 65 64 0a 2a 2a 20  any required.** 
1810d 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 69  sub-journal is i
1810e 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 2d 6d 65  mplemented in-me
1810f 6d 6f 72 79 20 69 66 20 70 50 61 67 65 72 20 69  mory if pPager i
18110 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  s an in-memory d
18111 61 74 61 62 61 73 65 2c 20 0a 2a 2a 20 6f 72 20  atabase, .** or 
18112 75 73 69 6e 67 20 61 20 74 65 6d 70 6f 72 61 72  using a temporar
18113 79 20 66 69 6c 65 20 6f 74 68 65 72 77 69 73 65  y file otherwise
18114 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18115 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
18116 61 67 65 72 42 65 67 69 6e 28 50 61 67 65 72 20  agerBegin(Pager 
18117 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 78 46  *pPager, int exF
18118 6c 61 67 2c 20 69 6e 74 20 73 75 62 6a 49 6e 4d  lag, int subjInM
18119 65 6d 6f 72 79 29 7b 0a 20 20 69 6e 74 20 72 63  emory){.  int rc
1811a 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
1811b 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72   if( pPager->err
1811c 43 6f 64 65 20 29 20 72 65 74 75 72 6e 20 70 50  Code ) return pP
1811d 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20  ager->errCode;. 
1811e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
1811f 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 52 5f 52  >eState>=PAGER_R
18120 45 41 44 45 52 20 26 26 20 70 50 61 67 65 72 2d  EADER && pPager-
18121 3e 65 53 74 61 74 65 3c 50 41 47 45 52 5f 45 52  >eState<PAGER_ER
18122 52 4f 52 20 29 3b 0a 20 20 70 50 61 67 65 72 2d  ROR );.  pPager-
18123 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 3d 20  >subjInMemory = 
18124 28 75 38 29 73 75 62 6a 49 6e 4d 65 6d 6f 72 79  (u8)subjInMemory
18125 3b 0a 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  ;..  if( ALWAYS(
18126 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d  pPager->eState==
18127 50 41 47 45 52 5f 52 45 41 44 45 52 29 20 29 7b  PAGER_READER) ){
18128 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
18129 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d  ger->pInJournal=
1812a 3d 30 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 70  =0 );..    if( p
1812b 61 67 65 72 55 73 65 57 61 6c 28 70 50 61 67 65  agerUseWal(pPage
1812c 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  r) ){.      /* I
1812d 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 63  f the pager is c
1812e 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 75 73 65  onfigured to use
1812f 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 65 78   locking_mode=ex
18130 63 6c 75 73 69 76 65 2c 20 61 6e 64 20 61 6e 0a  clusive, and an.
18131 20 20 20 20 20 20 2a 2a 20 65 78 63 6c 75 73 69        ** exclusi
18132 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  ve lock on the d
18133 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 61  atabase is not a
18134 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74  lready held, obt
18135 61 69 6e 20 69 74 20 6e 6f 77 2e 0a 20 20 20 20  ain it now..    
18136 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70    */.      if( p
18137 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65  Pager->exclusive
18138 4d 6f 64 65 20 26 26 20 73 71 6c 69 74 65 33 57  Mode && sqlite3W
18139 61 6c 45 78 63 6c 75 73 69 76 65 4d 6f 64 65 28  alExclusiveMode(
1813a 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 2d 31  pPager->pWal, -1
1813b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  ) ){.        rc 
1813c 3d 20 70 61 67 65 72 4c 6f 63 6b 44 62 28 70 50  = pagerLockDb(pP
1813d 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f  ager, EXCLUSIVE_
1813e 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69  LOCK);.        i
1813f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
18140 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
18141 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
18142 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
18143 65 33 57 61 6c 45 78 63 6c 75 73 69 76 65 4d 6f  e3WalExclusiveMo
18144 64 65 28 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c  de(pPager->pWal,
18145 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20   1);.      }..  
18146 20 20 20 20 2f 2a 20 47 72 61 62 20 74 68 65 20      /* Grab the 
18147 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68  write lock on th
18148 65 20 6c 6f 67 20 66 69 6c 65 2e 20 49 66 20 73  e log file. If s
18149 75 63 63 65 73 73 66 75 6c 2c 20 75 70 67 72 61  uccessful, upgra
1814a 64 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 50  de to.      ** P
1814b 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 74  AGER_RESERVED st
1814c 61 74 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ate. Otherwise, 
1814d 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
1814e 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c  code to the call
1814f 65 72 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65  er..      ** The
18150 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 73   busy-handler is
18151 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20   not invoked if 
18152 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  another connecti
18153 6f 6e 20 61 6c 72 65 61 64 79 0a 20 20 20 20 20  on already.     
18154 20 2a 2a 20 68 6f 6c 64 73 20 74 68 65 20 77 72   ** holds the wr
18155 69 74 65 2d 6c 6f 63 6b 2e 20 49 66 20 70 6f 73  ite-lock. If pos
18156 73 69 62 6c 65 2c 20 74 68 65 20 75 70 70 65 72  sible, the upper
18157 20 6c 61 79 65 72 20 77 69 6c 6c 20 63 61 6c 6c   layer will call
18158 20 69 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20   it..      */.  
18159 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1815a 57 61 6c 42 65 67 69 6e 57 72 69 74 65 54 72 61  WalBeginWriteTra
1815b 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2d  nsaction(pPager-
1815c 3e 70 57 61 6c 29 3b 0a 20 20 20 20 7d 65 6c 73  >pWal);.    }els
1815d 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 62 74 61  e{.      /* Obta
1815e 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  in a RESERVED lo
1815f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
18160 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20  se file. If the 
18161 65 78 46 6c 61 67 20 70 61 72 61 6d 65 74 65 72  exFlag parameter
18162 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74 72 75  .      ** is tru
18163 65 2c 20 74 68 65 6e 20 69 6d 6d 65 64 69 61 74  e, then immediat
18164 65 6c 79 20 75 70 67 72 61 64 65 20 74 68 69 73  ely upgrade this
18165 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45   to an EXCLUSIVE
18166 20 6c 6f 63 6b 2e 20 54 68 65 0a 20 20 20 20 20   lock. The.     
18167 20 2a 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72   ** busy-handler
18168 20 63 61 6c 6c 62 61 63 6b 20 63 61 6e 20 62 65   callback can be
18169 20 75 73 65 64 20 77 68 65 6e 20 75 70 67 72 61   used when upgra
1816a 64 69 6e 67 20 74 6f 20 74 68 65 20 45 58 43 4c  ding to the EXCL
1816b 55 53 49 56 45 0a 20 20 20 20 20 20 2a 2a 20 6c  USIVE.      ** l
1816c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20 77 68 65  ock, but not whe
1816d 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74 68 65 20  n obtaining the 
1816e 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 0a 20  RESERVED lock.. 
1816f 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63       */.      rc
18170 20 3d 20 70 61 67 65 72 4c 6f 63 6b 44 62 28 70   = pagerLockDb(p
18171 50 61 67 65 72 2c 20 52 45 53 45 52 56 45 44 5f  Pager, RESERVED_
18172 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28  LOCK);.      if(
18173 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
18174 26 20 65 78 46 6c 61 67 20 29 7b 0a 20 20 20 20  & exFlag ){.    
18175 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77      rc = pager_w
18176 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67  ait_on_lock(pPag
18177 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  er, EXCLUSIVE_LO
18178 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  CK);.      }.   
18179 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   }..    if( rc==
1817a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1817b 20 20 20 2f 2a 20 43 68 61 6e 67 65 20 74 6f 20     /* Change to 
1817c 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 73 74  WRITER_LOCKED st
1817d 61 74 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ate..      **.  
1817e 20 20 20 20 2a 2a 20 57 41 4c 20 6d 6f 64 65 20      ** WAL mode 
1817f 73 65 74 73 20 50 61 67 65 72 2e 65 53 74 61 74  sets Pager.eStat
18180 65 20 74 6f 20 50 41 47 45 52 5f 57 52 49 54 45  e to PAGER_WRITE
18181 52 5f 4c 4f 43 4b 45 44 20 6f 72 20 43 41 43 48  R_LOCKED or CACH
18182 45 4d 4f 44 0a 20 20 20 20 20 20 2a 2a 20 77 68  EMOD.      ** wh
18183 65 6e 20 69 74 20 68 61 73 20 61 6e 20 6f 70 65  en it has an ope
18184 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 62  n transaction, b
18185 75 74 20 6e 65 76 65 72 20 74 6f 20 44 42 4d 4f  ut never to DBMO
18186 44 20 6f 72 20 46 49 4e 49 53 48 45 44 2e 0a 20  D or FINISHED.. 
18187 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20       ** This is 
18188 62 65 63 61 75 73 65 20 69 6e 20 74 68 6f 73 65  because in those
18189 20 73 74 61 74 65 73 20 74 68 65 20 63 6f 64 65   states the code
1818a 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 73 61   to roll back sa
1818b 76 65 70 6f 69 6e 74 20 0a 20 20 20 20 20 20 2a  vepoint .      *
1818c 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 6d  * transactions m
1818d 61 79 20 63 6f 70 79 20 64 61 74 61 20 66 72 6f  ay copy data fro
1818e 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61  m the sub-journa
1818f 6c 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  l into the datab
18190 61 73 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 69  ase .      ** fi
18191 6c 65 20 61 73 20 77 65 6c 6c 20 61 73 20 69 6e  le as well as in
18192 74 6f 20 74 68 65 20 70 61 67 65 20 63 61 63 68  to the page cach
18193 65 2e 20 57 68 69 63 68 20 77 6f 75 6c 64 20 62  e. Which would b
18194 65 20 69 6e 63 6f 72 72 65 63 74 20 69 6e 20 0a  e incorrect in .
18195 20 20 20 20 20 20 2a 2a 20 57 41 4c 20 6d 6f 64        ** WAL mod
18196 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
18197 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65    pPager->eState
18198 20 3d 20 50 41 47 45 52 5f 57 52 49 54 45 52 5f   = PAGER_WRITER_
18199 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 20 20 70 50  LOCKED;.      pP
1819a 61 67 65 72 2d 3e 64 62 48 69 6e 74 53 69 7a 65  ager->dbHintSize
1819b 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a   = pPager->dbSiz
1819c 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  e;.      pPager-
1819d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 70 50  >dbFileSize = pP
1819e 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20  ager->dbSize;.  
1819f 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 4f 72      pPager->dbOr
181a0 69 67 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d  igSize = pPager-
181a1 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 20 20 70  >dbSize;.      p
181a2 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
181a3 66 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20  f = 0;.    }..  
181a4 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
181a5 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65  LITE_OK || pPage
181a6 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
181a7 5f 52 45 41 44 45 52 20 29 3b 0a 20 20 20 20 61  _READER );.    a
181a8 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
181a9 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e  E_OK || pPager->
181aa 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52  eState==PAGER_WR
181ab 49 54 45 52 5f 4c 4f 43 4b 45 44 20 29 3b 0a 20  ITER_LOCKED );. 
181ac 20 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72     assert( asser
181ad 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50  t_pager_state(pP
181ae 61 67 65 72 29 20 29 3b 0a 20 20 7d 0a 0a 20 20  ager) );.  }..  
181af 50 41 47 45 52 54 52 41 43 45 28 28 22 54 52 41  PAGERTRACE(("TRA
181b0 4e 53 41 43 54 49 4f 4e 20 25 64 5c 6e 22 2c 20  NSACTION %d\n", 
181b1 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29  PAGERID(pPager))
181b2 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
181b3 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20  }../*.** Mark a 
181b4 73 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67 65  single data page
181b5 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54   as writeable. T
181b6 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74  he page is writt
181b7 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20  en into the .** 
181b8 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20  main journal or 
181b9 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 72  sub-journal as r
181ba 65 71 75 69 72 65 64 2e 20 49 66 20 74 68 65 20  equired. If the 
181bb 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20  page is written 
181bc 69 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74  into.** one of t
181bd 68 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65  he journals, the
181be 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62   corresponding b
181bf 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65  it is set in the
181c0 20 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f   .** Pager.pInJo
181c1 75 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e 64  urnal bitvec and
181c2 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f   the PagerSavepo
181c3 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74  int.pInSavepoint
181c4 20 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 61   bitvecs.** of a
181c5 6e 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e  ny open savepoin
181c6 74 73 20 61 73 20 61 70 70 72 6f 70 72 69 61 74  ts as appropriat
181c7 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
181c8 20 70 61 67 65 72 5f 77 72 69 74 65 28 50 67 48   pager_write(PgH
181c9 64 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 64  dr *pPg){.  void
181ca 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70   *pData = pPg->p
181cb 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a 70  Data;.  Pager *p
181cc 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61  Pager = pPg->pPa
181cd 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ger;.  int rc = 
181ce 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a  SQLITE_OK;..  /*
181cf 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
181d0 20 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e 6c 65   not called unle
181d1 73 73 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73  ss a write-trans
181d2 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61  action has alrea
181d3 64 79 20 0a 20 20 2a 2a 20 62 65 65 6e 20 73 74  dy .  ** been st
181d4 61 72 74 65 64 2e 20 54 68 65 20 6a 6f 75 72 6e  arted. The journ
181d5 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f 72 20 6d  al file may or m
181d6 61 79 20 6e 6f 74 20 62 65 20 6f 70 65 6e 20 61  ay not be open a
181d7 74 20 74 68 69 73 20 70 6f 69 6e 74 2e 0a 20 20  t this point..  
181d8 2a 2a 20 49 74 20 69 73 20 6e 65 76 65 72 20 63  ** It is never c
181d9 61 6c 6c 65 64 20 69 6e 20 74 68 65 20 45 52 52  alled in the ERR
181da 4f 52 20 73 74 61 74 65 2e 0a 20 20 2a 2f 0a 20  OR state..  */. 
181db 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
181dc 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57  >eState==PAGER_W
181dd 52 49 54 45 52 5f 4c 4f 43 4b 45 44 0a 20 20 20  RITER_LOCKED.   
181de 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65      || pPager->e
181df 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49  State==PAGER_WRI
181e0 54 45 52 5f 43 41 43 48 45 4d 4f 44 0a 20 20 20  TER_CACHEMOD.   
181e1 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65      || pPager->e
181e2 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49  State==PAGER_WRI
181e3 54 45 52 5f 44 42 4d 4f 44 0a 20 20 29 3b 0a 20  TER_DBMOD.  );. 
181e4 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74 5f   assert( assert_
181e5 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67  pager_state(pPag
181e6 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  er) );..  /* If 
181e7 61 6e 20 65 72 72 6f 72 20 68 61 73 20 62 65 65  an error has bee
181e8 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 64 65 74  n previously det
181e9 65 63 74 65 64 2c 20 72 65 70 6f 72 74 20 74 68  ected, report th
181ea 65 20 73 61 6d 65 20 65 72 72 6f 72 0a 20 20 2a  e same error.  *
181eb 2a 20 61 67 61 69 6e 2e 20 54 68 69 73 20 73 68  * again. This sh
181ec 6f 75 6c 64 20 6e 6f 74 20 68 61 70 70 65 6e 2c  ould not happen,
181ed 20 62 75 74 20 74 68 65 20 63 68 65 63 6b 20 70   but the check p
181ee 72 6f 76 69 64 65 73 20 72 6f 62 75 73 74 6e 65  rovides robustne
181ef 73 73 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56  ss. */.  if( NEV
181f0 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  ER(pPager->errCo
181f1 64 65 29 20 29 20 20 72 65 74 75 72 6e 20 70 50  de) )  return pP
181f2 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a  ager->errCode;..
181f3 20 20 2f 2a 20 48 69 67 68 65 72 2d 6c 65 76 65    /* Higher-leve
181f4 6c 20 72 6f 75 74 69 6e 65 73 20 6e 65 76 65 72  l routines never
181f5 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74   call this funct
181f6 69 6f 6e 20 69 66 20 64 61 74 61 62 61 73 65 20  ion if database 
181f7 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 77 72 69 74  is not.  ** writ
181f8 61 62 6c 65 2e 20 20 42 75 74 20 63 68 65 63 6b  able.  But check
181f9 20 61 6e 79 77 61 79 2c 20 6a 75 73 74 20 66 6f   anyway, just fo
181fa 72 20 72 6f 62 75 73 74 6e 65 73 73 2e 20 2a 2f  r robustness. */
181fb 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61  .  if( NEVER(pPa
181fc 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29  ger->readOnly) )
181fd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50   return SQLITE_P
181fe 45 52 4d 3b 0a 0a 20 20 43 48 45 43 4b 5f 50 41  ERM;..  CHECK_PA
181ff 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 54  GE(pPg);..  /* T
18200 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
18201 6e 65 65 64 73 20 74 6f 20 62 65 20 6f 70 65 6e  needs to be open
18202 65 64 2e 20 48 69 67 68 65 72 20 6c 65 76 65 6c  ed. Higher level
18203 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65 20 61   routines have a
18204 6c 72 65 61 64 79 0a 20 20 2a 2a 20 6f 62 74 61  lready.  ** obta
18205 69 6e 65 64 20 74 68 65 20 6e 65 63 65 73 73 61  ined the necessa
18206 72 79 20 6c 6f 63 6b 73 20 74 6f 20 62 65 67 69  ry locks to begi
18207 6e 20 74 68 65 20 77 72 69 74 65 2d 74 72 61 6e  n the write-tran
18208 73 61 63 74 69 6f 6e 2c 20 62 75 74 20 74 68 65  saction, but the
18209 0a 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6a  .  ** rollback j
1820a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 6e 6f 74  ournal might not
1820b 20 79 65 74 20 62 65 20 6f 70 65 6e 2e 20 4f 70   yet be open. Op
1820c 65 6e 20 69 74 20 6e 6f 77 20 69 66 20 74 68 69  en it now if thi
1820d 73 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20  s is the case.. 
1820e 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 69 73   **.  ** This is
1820f 20 64 6f 6e 65 20 62 65 66 6f 72 65 20 63 61 6c   done before cal
18210 6c 69 6e 67 20 73 71 6c 69 74 65 33 50 63 61 63  ling sqlite3Pcac
18211 68 65 4d 61 6b 65 44 69 72 74 79 28 29 20 6f 6e  heMakeDirty() on
18212 20 74 68 65 20 70 61 67 65 2e 20 0a 20 20 2a 2a   the page. .  **
18213 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 69   Otherwise, if i
18214 74 20 77 65 72 65 20 64 6f 6e 65 20 61 66 74 65  t were done afte
18215 72 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65  r calling sqlite
18216 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79  3PcacheMakeDirty
18217 28 29 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 61 6e  (), then.  ** an
18218 20 65 72 72 6f 72 20 6d 69 67 68 74 20 6f 63 63   error might occ
18219 75 72 20 61 6e 64 20 74 68 65 20 70 61 67 65 72  ur and the pager
1821a 20 77 6f 75 6c 64 20 65 6e 64 20 75 70 20 69 6e   would end up in
1821b 20 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 73   WRITER_LOCKED s
1821c 74 61 74 65 0a 20 20 2a 2a 20 77 69 74 68 20 70  tate.  ** with p
1821d 61 67 65 73 20 6d 61 72 6b 65 64 20 61 73 20 64  ages marked as d
1821e 69 72 74 79 20 69 6e 20 74 68 65 20 63 61 63 68  irty in the cach
1821f 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50  e..  */.  if( pP
18220 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41  ager->eState==PA
18221 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45  GER_WRITER_LOCKE
18222 44 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61  D ){.    rc = pa
18223 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c  ger_open_journal
18224 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66  (pPager);.    if
18225 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
18226 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
18227 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
18228 72 2d 3e 65 53 74 61 74 65 3e 3d 50 41 47 45 52  r->eState>=PAGER
18229 5f 57 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44  _WRITER_CACHEMOD
1822a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 73   );.  assert( as
1822b 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65  sert_pager_state
1822c 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 2f  (pPager) );..  /
1822d 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20  * Mark the page 
1822e 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 74 68  as dirty.  If th
1822f 65 20 70 61 67 65 20 68 61 73 20 61 6c 72 65 61  e page has alrea
18230 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 0a  dy been written.
18231 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72    ** to the jour
18232 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e 20  nal then we can 
18233 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61  return right awa
18234 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  y..  */.  sqlite
18235 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79  3PcacheMakeDirty
18236 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61 67  (pPg);.  if( pag
18237 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20  eInJournal(pPg) 
18238 26 26 20 21 73 75 62 6a 52 65 71 75 69 72 65 73  && !subjRequires
18239 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20  Page(pPg) ){.   
1823a 20 61 73 73 65 72 74 28 20 21 70 61 67 65 72 55   assert( !pagerU
1823b 73 65 57 61 6c 28 70 50 61 67 65 72 29 20 29 3b  seWal(pPager) );
1823c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 0a 20 20 20  .  }else{.  .   
1823d 20 2f 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74   /* The transact
1823e 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20  ion journal now 
1823f 65 78 69 73 74 73 20 61 6e 64 20 77 65 20 68 61  exists and we ha
18240 76 65 20 61 20 52 45 53 45 52 56 45 44 20 6f 72  ve a RESERVED or
18241 20 61 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55   an.    ** EXCLU
18242 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  SIVE lock on the
18243 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
18244 69 6c 65 2e 20 20 57 72 69 74 65 20 74 68 65 20  ile.  Write the 
18245 63 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0a  current page to.
18246 20 20 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73      ** the trans
18247 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69  action journal i
18248 66 20 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72  f it is not ther
18249 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a  e already..    *
1824a 2f 0a 20 20 20 20 69 66 28 20 21 70 61 67 65 49  /.    if( !pageI
1824b 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26  nJournal(pPg) &&
1824c 20 21 70 61 67 65 72 55 73 65 57 61 6c 28 70 50   !pagerUseWal(pP
1824d 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 61  ager) ){.      a
1824e 73 73 65 72 74 28 20 70 61 67 65 72 55 73 65 57  ssert( pagerUseW
1824f 61 6c 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b  al(pPager)==0 );
18250 0a 20 20 20 20 20 20 69 66 28 20 70 50 67 2d 3e  .      if( pPg->
18251 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62  pgno<=pPager->db
18252 4f 72 69 67 53 69 7a 65 20 26 26 20 69 73 4f 70  OrigSize && isOp
18253 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
18254 29 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 63  ){.        u32 c
18255 6b 73 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68  ksum;.        ch
18256 61 72 20 2a 70 44 61 74 61 32 3b 0a 20 20 20 20  ar *pData2;.    
18257 20 20 20 20 69 36 34 20 69 4f 66 66 20 3d 20 70      i64 iOff = p
18258 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
18259 66 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 57  f;..        /* W
1825a 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 77  e should never w
1825b 72 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72  rite to the jour
1825c 6e 61 6c 20 66 69 6c 65 20 74 68 65 20 70 61 67  nal file the pag
1825d 65 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a  e that.        *
1825e 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64  * contains the d
1825f 61 74 61 62 61 73 65 20 6c 6f 63 6b 73 2e 20 20  atabase locks.  
18260 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73  The following as
18261 73 65 72 74 20 76 65 72 69 66 69 65 73 0a 20 20  sert verifies.  
18262 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65        ** that we
18263 20 64 6f 20 6e 6f 74 2e 20 2a 2f 0a 20 20 20 20   do not. */.    
18264 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d      assert( pPg-
18265 3e 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f  >pgno!=PAGER_MJ_
18266 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 3b 0a  PGNO(pPager) );.
18267 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
18268 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
18269 48 64 72 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75  Hdr<=pPager->jou
1826a 72 6e 61 6c 4f 66 66 20 29 3b 0a 20 20 20 20 20  rnalOff );.     
1826b 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 65 72     CODEC2(pPager
1826c 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67  , pData, pPg->pg
1826d 6e 6f 2c 20 37 2c 20 72 65 74 75 72 6e 20 53 51  no, 7, return SQ
1826e 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74  LITE_NOMEM, pDat
1826f 61 32 29 3b 0a 20 20 20 20 20 20 20 20 63 6b 73  a2);.        cks
18270 75 6d 20 3d 20 70 61 67 65 72 5f 63 6b 73 75 6d  um = pager_cksum
18271 28 70 50 61 67 65 72 2c 20 28 75 38 2a 29 70 44  (pPager, (u8*)pD
18272 61 74 61 32 29 3b 0a 0a 20 20 20 20 20 20 20 20  ata2);..        
18273 2f 2a 20 45 76 65 6e 20 69 66 20 61 6e 20 49 4f  /* Even if an IO
18274 20 6f 72 20 64 69 73 6b 66 75 6c 6c 20 65 72 72   or diskfull err
18275 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
18276 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68 65 0a  journalling the.
18277 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20          ** page 
18278 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20 61 62 6f  in the block abo
18279 76 65 2c 20 73 65 74 20 74 68 65 20 6e 65 65 64  ve, set the need
1827a 2d 73 79 6e 63 20 66 6c 61 67 20 66 6f 72 20 74  -sync flag for t
1827b 68 65 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20  he page..       
1827c 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77   ** Otherwise, w
1827d 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74  hen the transact
1827e 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61  ion is rolled ba
1827f 63 6b 2c 20 74 68 65 20 6c 6f 67 69 63 20 69 6e  ck, the logic in
18280 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6c 61 79  .        ** play
18281 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20  back_one_page() 
18282 77 69 6c 6c 20 74 68 69 6e 6b 20 74 68 61 74 20  will think that 
18283 74 68 65 20 70 61 67 65 20 6e 65 65 64 73 20 74  the page needs t
18284 6f 20 62 65 20 72 65 73 74 6f 72 65 64 0a 20 20  o be restored.  
18285 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20        ** in the 
18286 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 41  database file. A
18287 6e 64 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f  nd if an IO erro
18288 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64  r occurs while d
18289 6f 69 6e 67 20 73 6f 2c 0a 20 20 20 20 20 20 20  oing so,.       
1828a 20 2a 2a 20 74 68 65 6e 20 63 6f 72 72 75 70 74   ** then corrupt
1828b 69 6f 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 2e 0a  ion may follow..
1828c 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1828d 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d     pPg->flags |=
1828e 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
1828f 3b 0a 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ;..        rc = 
18290 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67  write32bits(pPag
18291 65 72 2d 3e 6a 66 64 2c 20 69 4f 66 66 2c 20 70  er->jfd, iOff, p
18292 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20  Pg->pgno);.     
18293 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
18294 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
18295 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
18296 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50  qlite3OsWrite(pP
18297 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 61 74 61  ager->jfd, pData
18298 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  2, pPager->pageS
18299 69 7a 65 2c 20 69 4f 66 66 2b 34 29 3b 0a 20 20  ize, iOff+4);.  
1829a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1829b 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
1829c 20 72 63 3b 0a 20 20 20 20 20 20 20 20 72 63 20   rc;.        rc 
1829d 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50  = write32bits(pP
1829e 61 67 65 72 2d 3e 6a 66 64 2c 20 69 4f 66 66 2b  ager->jfd, iOff+
1829f 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
182a0 2b 34 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20 20  +4, cksum);.    
182a1 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
182a2 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
182a3 63 3b 0a 0a 20 20 20 20 20 20 20 20 49 4f 54 52  c;..        IOTR
182a4 41 43 45 28 28 22 4a 4f 55 54 20 25 70 20 25 64  ACE(("JOUT %p %d
182a5 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61   %lld %d\n", pPa
182a6 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  ger, pPg->pgno, 
182a7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
182a8 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
182a9 6c 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e 70 61  lOff, pPager->pa
182aa 67 65 53 69 7a 65 29 29 3b 0a 20 20 20 20 20 20  geSize));.      
182ab 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c    PAGER_INCR(sql
182ac 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65  ite3_pager_write
182ad 6a 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20  j_count);.      
182ae 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4a    PAGERTRACE(("J
182af 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25  OURNAL %d page %
182b0 64 20 6e 65 65 64 53 79 6e 63 3d 25 64 20 68 61  d needSync=%d ha
182b1 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20  sh(%08x)\n",.   
182b2 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49            PAGERI
182b3 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e  D(pPager), pPg->
182b4 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20  pgno, .         
182b5 20 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 67 73      ((pPg->flags
182b6 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
182b7 29 3f 31 3a 30 29 2c 20 70 61 67 65 72 5f 70 61  )?1:0), pager_pa
182b8 67 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a  gehash(pPg)));..
182b9 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
182ba 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 38 20  journalOff += 8 
182bb 2b 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  + pPager->pageSi
182bc 7a 65 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67  ze;.        pPag
182bd 65 72 2d 3e 6e 52 65 63 2b 2b 3b 0a 20 20 20 20  er->nRec++;.    
182be 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
182bf 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d  er->pInJournal!=
182c0 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  0 );.        rc 
182c1 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53  = sqlite3BitvecS
182c2 65 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  et(pPager->pInJo
182c3 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f  urnal, pPg->pgno
182c4 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  );.        testc
182c5 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ase( rc==SQLITE_
182c6 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20  NOMEM );.       
182c7 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
182c8 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51  ITE_OK || rc==SQ
182c9 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20  LITE_NOMEM );.  
182ca 20 20 20 20 20 20 72 63 20 7c 3d 20 61 64 64 54        rc |= addT
182cb 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63  oSavepointBitvec
182cc 73 28 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70  s(pPager, pPg->p
182cd 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66  gno);.        if
182ce 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
182cf 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  ){.          ass
182d0 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
182d1 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20  NOMEM );.       
182d2 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
182d3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
182d4 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28  lse{.        if(
182d5 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 21   pPager->eState!
182d6 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 44 42  =PAGER_WRITER_DB
182d7 4d 4f 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20  MOD ){.         
182d8 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50   pPg->flags |= P
182d9 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a  GHDR_NEED_SYNC;.
182da 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
182db 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 41    PAGERTRACE(("A
182dc 50 50 45 4e 44 20 25 64 20 70 61 67 65 20 25 64  PPEND %d page %d
182dd 20 6e 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c   needSync=%d\n",
182de 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
182df 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
182e0 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20  , pPg->pgno,.   
182e1 20 20 20 20 20 20 20 20 20 20 20 20 28 28 70 50              ((pP
182e2 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
182e3 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 29 29  EED_SYNC)?1:0)))
182e4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
182e5 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65    .    /* If the
182e6 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e   statement journ
182e7 61 6c 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74  al is open and t
182e8 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69  he page is not i
182e9 6e 20 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65  n it,.    ** the
182ea 6e 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72  n write the curr
182eb 65 6e 74 20 70 61 67 65 20 74 6f 20 74 68 65 20  ent page to the 
182ec 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61  statement journa
182ed 6c 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20  l.  Note that.  
182ee 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65    ** the stateme
182ef 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61  nt journal forma
182f0 74 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74  t differs from t
182f1 68 65 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72  he standard jour
182f2 6e 61 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a  nal format.    *
182f3 2a 20 69 6e 20 74 68 61 74 20 69 74 20 6f 6d 69  * in that it omi
182f4 74 73 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73  ts the checksums
182f5 20 61 6e 64 20 74 68 65 20 68 65 61 64 65 72 2e   and the header.
182f6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
182f7 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65  subjRequiresPage
182f8 28 70 50 67 29 20 29 7b 0a 20 20 20 20 20 20 72  (pPg) ){.      r
182f9 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61  c = subjournalPa
182fa 67 65 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20  ge(pPg);.    }. 
182fb 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20   }..  /* Update 
182fc 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a  the database siz
182fd 65 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20  e and return..  
182fe 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  */.  if( pPager-
182ff 3e 64 62 53 69 7a 65 3c 70 50 67 2d 3e 70 67 6e  >dbSize<pPg->pgn
18300 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  o ){.    pPager-
18301 3e 64 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70  >dbSize = pPg->p
18302 67 6e 6f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  gno;.  }.  retur
18303 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  n rc;.}../*.** M
18304 61 72 6b 20 61 20 64 61 74 61 20 70 61 67 65 20  ark a data page 
18305 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68  as writeable. Th
18306 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20  is routine must 
18307 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65  be called before
18308 20 0a 2a 2a 20 6d 61 6b 69 6e 67 20 63 68 61 6e   .** making chan
18309 67 65 73 20 74 6f 20 61 20 70 61 67 65 2e 20 54  ges to a page. T
1830a 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63  he caller must c
1830b 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20  heck the return 
1830c 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 74 68 69  value .** of thi
1830d 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 62  s function and b
1830e 65 20 63 61 72 65 66 75 6c 20 6e 6f 74 20 74 6f  e careful not to
1830f 20 63 68 61 6e 67 65 20 61 6e 79 20 70 61 67 65   change any page
18310 20 64 61 74 61 20 75 6e 6c 65 73 73 20 0a 2a 2a   data unless .**
18311 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
18312 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e  turns SQLITE_OK.
18313 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65  .**.** The diffe
18314 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68  rence between th
18315 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20  is function and 
18316 70 61 67 65 72 5f 77 72 69 74 65 28 29 20 69 73  pager_write() is
18317 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 66 75   that this.** fu
18318 6e 63 74 69 6f 6e 20 61 6c 73 6f 20 64 65 61 6c  nction also deal
18319 73 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69  s with the speci
1831a 61 6c 20 63 61 73 65 20 77 68 65 72 65 20 32 20  al case where 2 
1831b 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 0a 2a 2a  or more pages.**
1831c 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65   fit on a single
1831d 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 49 6e   disk sector. In
1831e 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c 20 63   this case all c
1831f 6f 2d 72 65 73 69 64 65 6e 74 20 70 61 67 65 73  o-resident pages
18320 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65  .** must have be
18321 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  en written to th
18322 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62  e journal file b
18323 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
18324 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  .**.** If an err
18325 6f 72 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54  or occurs, SQLIT
18326 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f  E_NOMEM or an IO
18327 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
18328 65 74 75 72 6e 65 64 0a 2a 2a 20 61 73 20 61 70  eturned.** as ap
18329 70 72 6f 70 72 69 61 74 65 2e 20 4f 74 68 65 72  propriate. Other
1832a 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e  wise, SQLITE_OK.
1832b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1832c 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
1832d 67 65 72 57 72 69 74 65 28 44 62 50 61 67 65 20  gerWrite(DbPage 
1832e 2a 70 44 62 50 61 67 65 29 7b 0a 20 20 69 6e 74  *pDbPage){.  int
1832f 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
18330 0a 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d  ..  PgHdr *pPg =
18331 20 70 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65   pDbPage;.  Page
18332 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d  r *pPager = pPg-
18333 3e 70 50 61 67 65 72 3b 0a 20 20 50 67 6e 6f 20  >pPager;.  Pgno 
18334 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 20 3d  nPagePerSector =
18335 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72   (pPager->sector
18336 53 69 7a 65 2f 70 50 61 67 65 72 2d 3e 70 61 67  Size/pPager->pag
18337 65 53 69 7a 65 29 3b 0a 0a 20 20 61 73 73 65 72  eSize);..  asser
18338 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74  t( pPager->eStat
18339 65 3e 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f  e>=PAGER_WRITER_
1833a 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 61 73 73 65  LOCKED );.  asse
1833b 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61  rt( pPager->eSta
1833c 74 65 21 3d 50 41 47 45 52 5f 45 52 52 4f 52 20  te!=PAGER_ERROR 
1833d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 73 73  );.  assert( ass
1833e 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28  ert_pager_state(
1833f 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 69 66  pPager) );..  if
18340 28 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72  ( nPagePerSector
18341 3e 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e  >1 ){.    Pgno n
18342 50 61 67 65 43 6f 75 6e 74 3b 20 20 20 20 20 20  PageCount;      
18343 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
18344 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
18345 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
18346 0a 20 20 20 20 50 67 6e 6f 20 70 67 31 3b 20 20  .    Pgno pg1;  
18347 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18348 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20  * First page of 
18349 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 69  the sector pPg i
1834a 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20 2a 2f  s located on. */
1834b 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d  .    int nPage =
1834c 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   0;            /
1834d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
1834e 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 67  s starting at pg
1834f 31 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  1 to journal */.
18350 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20      int ii;     
18351 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18352 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
18353 0a 20 20 20 20 69 6e 74 20 6e 65 65 64 53 79 6e  .    int needSyn
18354 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  c = 0;         /
18355 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 70 61  * True if any pa
18356 67 65 20 68 61 73 20 50 47 48 44 52 5f 4e 45 45  ge has PGHDR_NEE
18357 44 5f 53 59 4e 43 20 2a 2f 0a 0a 20 20 20 20 2f  D_SYNC */..    /
18358 2a 20 53 65 74 20 74 68 65 20 64 6f 4e 6f 74 53  * Set the doNotS
18359 79 6e 63 53 70 69 6c 6c 20 66 6c 61 67 20 74 6f  yncSpill flag to
1835a 20 31 2e 20 54 68 69 73 20 69 73 20 62 65 63 61   1. This is beca
1835b 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 20 61 6c  use we cannot al
1835c 6c 6f 77 0a 20 20 20 20 2a 2a 20 61 20 6a 6f 75  low.    ** a jou
1835d 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 62  rnal header to b
1835e 65 20 77 72 69 74 74 65 6e 20 62 65 74 77 65 65  e written betwee
1835f 6e 20 74 68 65 20 70 61 67 65 73 20 6a 6f 75 72  n the pages jour
18360 6e 61 6c 65 64 20 62 79 0a 20 20 20 20 2a 2a 20  naled by.    ** 
18361 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20  this function.. 
18362 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
18363 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20  ( !MEMDB );.    
18364 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
18365 64 6f 4e 6f 74 53 79 6e 63 53 70 69 6c 6c 3d 3d  doNotSyncSpill==
18366 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  0 );.    pPager-
18367 3e 64 6f 4e 6f 74 53 79 6e 63 53 70 69 6c 6c 2b  >doNotSyncSpill+
18368 2b 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  +;..    /* This 
18369 74 72 69 63 6b 20 61 73 73 75 6d 65 73 20 74 68  trick assumes th
1836a 61 74 20 62 6f 74 68 20 74 68 65 20 70 61 67 65  at both the page
1836b 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72  -size and sector
1836c 2d 73 69 7a 65 20 61 72 65 0a 20 20 20 20 2a 2a  -size are.    **
1836d 20 61 6e 20 69 6e 74 65 67 65 72 20 70 6f 77 65   an integer powe
1836e 72 20 6f 66 20 32 2e 20 49 74 20 73 65 74 73 20  r of 2. It sets 
1836f 76 61 72 69 61 62 6c 65 20 70 67 31 20 74 6f 20  variable pg1 to 
18370 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 0a 20  the identifier. 
18371 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 72     ** of the fir
18372 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73  st page of the s
18373 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63  ector pPg is loc
18374 61 74 65 64 20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  ated on..    */.
18375 20 20 20 20 70 67 31 20 3d 20 28 28 70 50 67 2d      pg1 = ((pPg-
18376 3e 70 67 6e 6f 2d 31 29 20 26 20 7e 28 6e 50 61  >pgno-1) & ~(nPa
18377 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29 29 20  gePerSector-1)) 
18378 2b 20 31 3b 0a 0a 20 20 20 20 6e 50 61 67 65 43  + 1;..    nPageC
18379 6f 75 6e 74 20 3d 20 70 50 61 67 65 72 2d 3e 64  ount = pPager->d
1837a 62 53 69 7a 65 3b 0a 20 20 20 20 69 66 28 20 70  bSize;.    if( p
1837b 50 67 2d 3e 70 67 6e 6f 3e 6e 50 61 67 65 43 6f  Pg->pgno>nPageCo
1837c 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61  unt ){.      nPa
1837d 67 65 20 3d 20 28 70 50 67 2d 3e 70 67 6e 6f 20  ge = (pPg->pgno 
1837e 2d 20 70 67 31 29 2b 31 3b 0a 20 20 20 20 7d 65  - pg1)+1;.    }e
1837f 6c 73 65 20 69 66 28 20 28 70 67 31 2b 6e 50 61  lse if( (pg1+nPa
18380 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29 3e 6e  gePerSector-1)>n
18381 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20  PageCount ){.   
18382 20 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65     nPage = nPage
18383 43 6f 75 6e 74 2b 31 2d 70 67 31 3b 0a 20 20 20  Count+1-pg1;.   
18384 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50   }else{.      nP
18385 61 67 65 20 3d 20 6e 50 61 67 65 50 65 72 53 65  age = nPagePerSe
18386 63 74 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ctor;.    }.    
18387 61 73 73 65 72 74 28 6e 50 61 67 65 3e 30 29 3b  assert(nPage>0);
18388 0a 20 20 20 20 61 73 73 65 72 74 28 70 67 31 3c  .    assert(pg1<
18389 3d 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20  =pPg->pgno);.   
1838a 20 61 73 73 65 72 74 28 28 70 67 31 2b 6e 50 61   assert((pg1+nPa
1838b 67 65 29 3e 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a  ge)>pPg->pgno);.
1838c 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  .    for(ii=0; i
1838d 69 3c 6e 50 61 67 65 20 26 26 20 72 63 3d 3d 53  i<nPage && rc==S
1838e 51 4c 49 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b  QLITE_OK; ii++){
1838f 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 20 3d  .      Pgno pg =
18390 20 70 67 31 2b 69 69 3b 0a 20 20 20 20 20 20 50   pg1+ii;.      P
18391 67 48 64 72 20 2a 70 50 61 67 65 3b 0a 20 20 20  gHdr *pPage;.   
18392 20 20 20 69 66 28 20 70 67 3d 3d 70 50 67 2d 3e     if( pg==pPg->
18393 70 67 6e 6f 20 7c 7c 20 21 73 71 6c 69 74 65 33  pgno || !sqlite3
18394 42 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65  BitvecTest(pPage
18395 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70  r->pInJournal, p
18396 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  g) ){.        if
18397 28 20 70 67 21 3d 50 41 47 45 52 5f 4d 4a 5f 50  ( pg!=PAGER_MJ_P
18398 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20  GNO(pPager) ){. 
18399 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
1839a 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50  lite3PagerGet(pP
1839b 61 67 65 72 2c 20 70 67 2c 20 26 70 50 61 67 65  ager, pg, &pPage
1839c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
1839d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1839e 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  {.            rc
1839f 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 28 70   = pager_write(p
183a0 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Page);.         
183a1 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c     if( pPage->fl
183a2 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
183a3 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20  YNC ){.         
183a4 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20       needSync = 
183a5 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  1;.            }
183a6 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
183a7 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
183a8 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Page);.         
183a9 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
183aa 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 50     }else if( (pP
183ab 61 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b  age = pager_look
183ac 75 70 28 70 50 61 67 65 72 2c 20 70 67 29 29 21  up(pPager, pg))!
183ad 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  =0 ){.        if
183ae 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50  ( pPage->flags&P
183af 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29  GHDR_NEED_SYNC )
183b0 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 65 64  {.          need
183b1 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20  Sync = 1;.      
183b2 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
183b3 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
183b4 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  age);.      }.  
183b5 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
183b6 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  he PGHDR_NEED_SY
183b7 4e 43 20 66 6c 61 67 20 69 73 20 73 65 74 20 66  NC flag is set f
183b8 6f 72 20 61 6e 79 20 6f 66 20 74 68 65 20 6e 50  or any of the nP
183b9 61 67 65 20 70 61 67 65 73 20 0a 20 20 20 20 2a  age pages .    *
183ba 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 67  * starting at pg
183bb 31 2c 20 74 68 65 6e 20 69 74 20 6e 65 65 64 73  1, then it needs
183bc 20 74 6f 20 62 65 20 73 65 74 20 66 6f 72 20 61   to be set for a
183bd 6c 6c 20 6f 66 20 74 68 65 6d 2e 20 42 65 63 61  ll of them. Beca
183be 75 73 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 69  use.    ** writi
183bf 6e 67 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 65  ng to any of the
183c0 73 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 6d  se nPage pages m
183c1 61 79 20 64 61 6d 61 67 65 20 74 68 65 20 6f 74  ay damage the ot
183c2 68 65 72 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a  hers, the.    **
183c3 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75   journal file mu
183c4 73 74 20 63 6f 6e 74 61 69 6e 20 73 79 6e 63 28  st contain sync(
183c5 29 65 64 20 63 6f 70 69 65 73 20 6f 66 20 61 6c  )ed copies of al
183c6 6c 20 6f 66 20 74 68 65 6d 0a 20 20 20 20 2a 2a  l of them.    **
183c7 20 62 65 66 6f 72 65 20 61 6e 79 20 6f 66 20 74   before any of t
183c8 68 65 6d 20 63 61 6e 20 62 65 20 77 72 69 74 74  hem can be writt
183c9 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61  en out to the da
183ca 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20  tabase file..   
183cb 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
183cc 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 65 65  SQLITE_OK && nee
183cd 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 61  dSync ){.      a
183ce 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b  ssert( !MEMDB );
183cf 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b  .      for(ii=0;
183d0 20 69 69 3c 6e 50 61 67 65 3b 20 69 69 2b 2b 29   ii<nPage; ii++)
183d1 7b 0a 20 20 20 20 20 20 20 20 50 67 48 64 72 20  {.        PgHdr 
183d2 2a 70 50 61 67 65 20 3d 20 70 61 67 65 72 5f 6c  *pPage = pager_l
183d3 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67  ookup(pPager, pg
183d4 31 2b 69 69 29 3b 0a 20 20 20 20 20 20 20 20 69  1+ii);.        i
183d5 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20  f( pPage ){.    
183d6 20 20 20 20 20 20 70 50 61 67 65 2d 3e 66 6c 61        pPage->fla
183d7 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44  gs |= PGHDR_NEED
183d8 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20  _SYNC;.         
183d9 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
183da 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ef(pPage);.     
183db 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
183dc 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20   }..    assert( 
183dd 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e  pPager->doNotSyn
183de 63 53 70 69 6c 6c 3d 3d 31 20 29 3b 0a 20 20 20  cSpill==1 );.   
183df 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79   pPager->doNotSy
183e0 6e 63 53 70 69 6c 6c 2d 2d 3b 0a 20 20 7d 65 6c  ncSpill--;.  }el
183e1 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67  se{.    rc = pag
183e2 65 72 5f 77 72 69 74 65 28 70 44 62 50 61 67 65  er_write(pDbPage
183e3 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
183e4 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
183e5 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
183e6 70 61 67 65 20 67 69 76 65 6e 20 69 6e 20 74 68  page given in th
183e7 65 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 70  e argument was p
183e8 72 65 76 69 6f 75 73 6c 79 20 70 61 73 73 65 64  reviously passed
183e9 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61  .** to sqlite3Pa
183ea 67 65 72 57 72 69 74 65 28 29 2e 20 20 49 6e 20  gerWrite().  In 
183eb 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72 65 74  other words, ret
183ec 75 72 6e 20 54 52 55 45 20 69 66 20 69 74 20 69  urn TRUE if it i
183ed 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61 6e 67  s ok.** to chang
183ee 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
183ef 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69   the page..*/.#i
183f0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 51 4c  fndef NDEBUG.SQL
183f1 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
183f2 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
183f3 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 20 2a  iteable(DbPage *
183f4 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  pPg){.  return p
183f5 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
183f6 44 49 52 54 59 3b 0a 7d 0a 23 65 6e 64 69 66 0a  DIRTY;.}.#endif.
183f7 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f  ./*.** A call to
183f8 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65   this routine te
183f9 6c 6c 73 20 74 68 65 20 70 61 67 65 72 20 74 68  lls the pager th
183fa 61 74 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63  at it is not nec
183fb 65 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69  essary to.** wri
183fc 74 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69  te the informati
183fd 6f 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 20 62  on on page pPg b
183fe 61 63 6b 20 74 6f 20 74 68 65 20 64 69 73 6b 2c  ack to the disk,
183ff 20 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20   even though.** 
18400 74 68 61 74 20 70 61 67 65 20 6d 69 67 68 74 20  that page might 
18401 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72  be marked as dir
18402 74 79 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e  ty.  This happen
18403 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20  s, for example, 
18404 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65  when.** the page
18405 20 68 61 73 20 62 65 65 6e 20 61 64 64 65 64 20   has been added 
18406 61 73 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65  as a leaf of the
18407 20 66 72 65 65 6c 69 73 74 20 61 6e 64 20 73 6f   freelist and so
18408 20 69 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20   its.** content 
18409 6e 6f 20 6c 6f 6e 67 65 72 20 6d 61 74 74 65 72  no longer matter
1840a 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65  s..**.** The ove
1840b 72 6c 79 69 6e 67 20 73 6f 66 74 77 61 72 65 20  rlying software 
1840c 6c 61 79 65 72 20 63 61 6c 6c 73 20 74 68 69 73  layer calls this
1840d 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 61 6c   routine when al
1840e 6c 20 6f 66 20 74 68 65 20 64 61 74 61 0a 2a 2a  l of the data.**
1840f 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70 61   on the given pa
18410 67 65 20 69 73 20 75 6e 75 73 65 64 2e 20 54 68  ge is unused. Th
18411 65 20 70 61 67 65 72 20 6d 61 72 6b 73 20 74 68  e pager marks th
18412 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20  e page as clean 
18413 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 64 6f  so.** that it do
18414 65 73 20 6e 6f 74 20 67 65 74 20 77 72 69 74 74  es not get writt
18415 65 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a  en to disk..**.*
18416 2a 20 54 65 73 74 73 20 73 68 6f 77 20 74 68 61  * Tests show tha
18417 74 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74  t this optimizat
18418 69 6f 6e 20 63 61 6e 20 71 75 61 64 72 75 70 6c  ion can quadrupl
18419 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 6c  e the speed of l
1841a 61 72 67 65 20 0a 2a 2a 20 44 45 4c 45 54 45 20  arge .** DELETE 
1841b 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53  operations..*/.S
1841c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1841d 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44  id sqlite3PagerD
1841e 6f 6e 74 57 72 69 74 65 28 50 67 48 64 72 20 2a  ontWrite(PgHdr *
1841f 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70  pPg){.  Pager *p
18420 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61  Pager = pPg->pPa
18421 67 65 72 3b 0a 20 20 69 66 28 20 28 70 50 67 2d  ger;.  if( (pPg-
18422 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
18423 54 59 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6e  TY) && pPager->n
18424 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 7b 0a  Savepoint==0 ){.
18425 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28      PAGERTRACE((
18426 22 44 4f 4e 54 5f 57 52 49 54 45 20 70 61 67 65  "DONT_WRITE page
18427 20 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50   %d of %d\n", pP
18428 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44  g->pgno, PAGERID
18429 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20  (pPager)));.    
1842a 49 4f 54 52 41 43 45 28 28 22 43 4c 45 41 4e 20  IOTRACE(("CLEAN 
1842b 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72  %p %d\n", pPager
1842c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20  , pPg->pgno)).  
1842d 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20    pPg->flags |= 
1842e 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45  PGHDR_DONT_WRITE
1842f 3b 0a 20 20 20 20 70 61 67 65 72 5f 73 65 74 5f  ;.    pager_set_
18430 70 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a 20  pagehash(pPg);. 
18431 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
18432 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
18433 65 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20  ed to increment 
18434 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
18435 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 0a   database file .
18436 2a 2a 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65  ** change-counte
18437 72 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20 34  r, stored as a 4
18438 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e  -byte big-endian
18439 20 69 6e 74 65 67 65 72 20 73 74 61 72 74 69 6e   integer startin
1843a 67 20 61 74 20 0a 2a 2a 20 62 79 74 65 20 6f 66  g at .** byte of
1843b 66 73 65 74 20 32 34 20 6f 66 20 74 68 65 20 70  fset 24 of the p
1843c 61 67 65 72 20 66 69 6c 65 2e 20 20 54 68 65 20  ager file.  The 
1843d 73 65 63 6f 6e 64 61 72 79 20 63 68 61 6e 67 65  secondary change
1843e 20 63 6f 75 6e 74 65 72 20 61 74 0a 2a 2a 20 39   counter at.** 9
1843f 32 20 69 73 20 61 6c 73 6f 20 75 70 64 61 74 65  2 is also update
18440 64 2c 20 61 73 20 69 73 20 74 68 65 20 53 51 4c  d, as is the SQL
18441 69 74 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62  ite version numb
18442 65 72 20 61 74 20 6f 66 66 73 65 74 20 39 36 2e  er at offset 96.
18443 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 69 73 20  .**.** But this 
18444 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 66 20  only happens if 
18445 74 68 65 20 70 50 61 67 65 72 2d 3e 63 68 61 6e  the pPager->chan
18446 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67  geCountDone flag
18447 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 20 54 6f   is false..** To
18448 20 61 76 6f 69 64 20 65 78 63 65 73 73 20 63 68   avoid excess ch
18449 75 72 6e 69 6e 67 20 6f 66 20 70 61 67 65 20 31  urning of page 1
1844a 2c 20 74 68 65 20 75 70 64 61 74 65 20 6f 6e 6c  , the update onl
1844b 79 20 68 61 70 70 65 6e 73 20 6f 6e 63 65 2e 0a  y happens once..
1844c 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20  ** See also the 
1844d 70 61 67 65 72 5f 77 72 69 74 65 5f 63 68 61 6e  pager_write_chan
1844e 67 65 63 6f 75 6e 74 65 72 28 29 20 72 6f 75 74  gecounter() rout
1844f 69 6e 65 20 74 68 61 74 20 64 6f 65 73 20 61 6e  ine that does an
18450 20 0a 2a 2a 20 75 6e 63 6f 6e 64 69 74 69 6f 6e   .** uncondition
18451 61 6c 20 75 70 64 61 74 65 20 6f 66 20 74 68 65  al update of the
18452 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 73   change counters
18453 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69  ..**.** If the i
18454 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61 67  sDirectMode flag
18455 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74   is zero, then t
18456 68 69 73 20 69 73 20 64 6f 6e 65 20 62 79 20 63  his is done by c
18457 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74  alling .** sqlit
18458 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 6f  e3PagerWrite() o
18459 6e 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 6d  n page 1, then m
1845a 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63 6f 6e  odifying the con
1845b 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20  tents of the.** 
1845c 70 61 67 65 20 64 61 74 61 2e 20 49 6e 20 74 68  page data. In th
1845d 69 73 20 63 61 73 65 20 74 68 65 20 66 69 6c 65  is case the file
1845e 20 77 69 6c 6c 20 62 65 20 75 70 64 61 74 65 64   will be updated
1845f 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e   when the curren
18460 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e  t.** transaction
18461 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a   is committed..*
18462 2a 0a 2a 2a 20 54 68 65 20 69 73 44 69 72 65 63  *.** The isDirec
18463 74 4d 6f 64 65 20 66 6c 61 67 20 6d 61 79 20 6f  tMode flag may o
18464 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f 20  nly be non-zero 
18465 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 77  if the library w
18466 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77  as compiled.** w
18467 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 45  ith the SQLITE_E
18468 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49  NABLE_ATOMIC_WRI
18469 54 45 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64  TE macro defined
1846a 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 0a  . In this case,.
1846b 2a 2a 20 69 66 20 69 73 44 69 72 65 63 74 20 69  ** if isDirect i
1846c 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
1846d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1846e 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 64 69  le is updated di
1846f 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 77 72 69  rectly.** by wri
18470 74 69 6e 67 20 61 6e 20 75 70 64 61 74 65 64 20  ting an updated 
18471 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 20  version of page 
18472 31 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 74  1 using a call t
18473 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65  o the .** sqlite
18474 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e 63 74  3OsWrite() funct
18475 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
18476 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68  nt pager_incr_ch
18477 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67 65  angecounter(Page
18478 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69  r *pPager, int i
18479 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0a 20 20  sDirectMode){.  
1847a 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1847b 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  OK;..  assert( p
1847c 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50  Pager->eState==P
1847d 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48  AGER_WRITER_CACH
1847e 45 4d 4f 44 0a 20 20 20 20 20 20 20 7c 7c 20 70  EMOD.       || p
1847f 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50  Pager->eState==P
18480 41 47 45 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f  AGER_WRITER_DBMO
18481 44 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28  D.  );.  assert(
18482 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74   assert_pager_st
18483 61 74 65 28 70 50 61 67 65 72 29 20 29 3b 0a 0a  ate(pPager) );..
18484 20 20 2f 2a 20 44 65 63 6c 61 72 65 20 61 6e 64    /* Declare and
18485 20 69 6e 69 74 69 61 6c 69 7a 65 20 63 6f 6e 73   initialize cons
18486 74 61 6e 74 20 69 6e 74 65 67 65 72 20 27 69 73  tant integer 'is
18487 44 69 72 65 63 74 27 2e 20 49 66 20 74 68 65 0a  Direct'. If the.
18488 20 20 2a 2a 20 61 74 6f 6d 69 63 2d 77 72 69 74    ** atomic-writ
18489 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  e optimization i
1848a 73 20 65 6e 61 62 6c 65 64 20 69 6e 20 74 68 69  s enabled in thi
1848b 73 20 62 75 69 6c 64 2c 20 74 68 65 6e 20 69 73  s build, then is
1848c 44 69 72 65 63 74 0a 20 20 2a 2a 20 69 73 20 69  Direct.  ** is i
1848d 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 74 68  nitialized to th
1848e 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61  e value passed a
1848f 73 20 74 68 65 20 69 73 44 69 72 65 63 74 4d 6f  s the isDirectMo
18490 64 65 20 70 61 72 61 6d 65 74 65 72 0a 20 20 2a  de parameter.  *
18491 2a 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  * to this functi
18492 6f 6e 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  on. Otherwise, i
18493 74 20 69 73 20 61 6c 77 61 79 73 20 73 65 74 20  t is always set 
18494 74 6f 20 7a 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20  to zero..  **.  
18495 2a 2a 20 54 68 65 20 69 64 65 61 20 69 73 20 74  ** The idea is t
18496 68 61 74 20 69 66 20 74 68 65 20 61 74 6f 6d 69  hat if the atomi
18497 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61  c-write optimiza
18498 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 20 20 2a 2a  tion is not.  **
18499 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70   enabled at comp
1849a 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 20 63 6f  ile time, the co
1849b 6d 70 69 6c 65 72 20 63 61 6e 20 6f 6d 69 74 20  mpiler can omit 
1849c 74 68 65 20 74 65 73 74 73 20 6f 66 0a 20 20 2a  the tests of.  *
1849d 2a 20 27 69 73 44 69 72 65 63 74 27 20 62 65 6c  * 'isDirect' bel
1849e 6f 77 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 74  ow, as well as t
1849f 68 65 20 62 6c 6f 63 6b 20 65 6e 63 6c 6f 73 65  he block enclose
184a0 64 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 22 69  d in the.  ** "i
184a1 66 28 20 69 73 44 69 72 65 63 74 20 29 22 20 63  f( isDirect )" c
184a2 6f 6e 64 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23  ondition..  */.#
184a3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  ifndef SQLITE_EN
184a4 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54  ABLE_ATOMIC_WRIT
184a5 45 0a 23 20 64 65 66 69 6e 65 20 44 49 52 45 43  E.# define DIREC
184a6 54 5f 4d 4f 44 45 20 30 0a 20 20 61 73 73 65 72  T_MODE 0.  asser
184a7 74 28 20 69 73 44 69 72 65 63 74 4d 6f 64 65 3d  t( isDirectMode=
184a8 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =0 );.  UNUSED_P
184a9 41 52 41 4d 45 54 45 52 28 69 73 44 69 72 65 63  ARAMETER(isDirec
184aa 74 4d 6f 64 65 29 3b 0a 23 65 6c 73 65 0a 23 20  tMode);.#else.# 
184ab 64 65 66 69 6e 65 20 44 49 52 45 43 54 5f 4d 4f  define DIRECT_MO
184ac 44 45 20 69 73 44 69 72 65 63 74 4d 6f 64 65 0a  DE isDirectMode.
184ad 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 21 70  #endif..  if( !p
184ae 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75  Pager->changeCou
184af 6e 74 44 6f 6e 65 20 26 26 20 41 4c 57 41 59 53  ntDone && ALWAYS
184b0 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e  (pPager->dbSize>
184b1 30 29 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20  0) ){.    PgHdr 
184b2 2a 70 50 67 48 64 72 3b 20 20 20 20 20 20 20 20  *pPgHdr;        
184b3 20 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65 72          /* Refer
184b4 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 2a  ence to page 1 *
184b5 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  /..    assert( !
184b6 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
184b7 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65   && isOpen(pPage
184b8 72 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 20 20 2f  r->fd) );..    /
184b9 2a 20 4f 70 65 6e 20 70 61 67 65 20 31 20 6f 66  * Open page 1 of
184ba 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20 77 72   the file for wr
184bb 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63  iting. */.    rc
184bc 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
184bd 65 74 28 70 50 61 67 65 72 2c 20 31 2c 20 26 70  et(pPager, 1, &p
184be 50 67 48 64 72 29 3b 0a 20 20 20 20 61 73 73 65  PgHdr);.    asse
184bf 72 74 28 20 70 50 67 48 64 72 3d 3d 30 20 7c 7c  rt( pPgHdr==0 ||
184c0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
184c1 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 61 67  ;..    /* If pag
184c2 65 20 6f 6e 65 20 77 61 73 20 66 65 74 63 68 65  e one was fetche
184c3 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20  d successfully, 
184c4 61 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f  and this functio
184c5 6e 20 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20  n is not.    ** 
184c6 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 64 69 72  operating in dir
184c7 65 63 74 2d 6d 6f 64 65 2c 20 6d 61 6b 65 20 70  ect-mode, make p
184c8 61 67 65 20 31 20 77 72 69 74 61 62 6c 65 2e 20  age 1 writable. 
184c9 20 57 68 65 6e 20 6e 6f 74 20 69 6e 20 0a 20 20   When not in .  
184ca 20 20 2a 2a 20 64 69 72 65 63 74 20 6d 6f 64 65    ** direct mode
184cb 2c 20 70 61 67 65 20 31 20 69 73 20 61 6c 77 61  , page 1 is alwa
184cc 79 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65  ys held in cache
184cd 20 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 50   and hence the P
184ce 61 67 65 72 47 65 74 28 29 0a 20 20 20 20 2a 2a  agerGet().    **
184cf 20 61 62 6f 76 65 20 69 73 20 61 6c 77 61 79 73   above is always
184d0 20 73 75 63 63 65 73 73 66 75 6c 20 2d 20 68 65   successful - he
184d1 6e 63 65 20 74 68 65 20 41 4c 57 41 59 53 20 6f  nce the ALWAYS o
184d2 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 2e  n rc==SQLITE_OK.
184d3 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
184d4 21 44 49 52 45 43 54 5f 4d 4f 44 45 20 26 26 20  !DIRECT_MODE && 
184d5 41 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c 49 54  ALWAYS(rc==SQLIT
184d6 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 72  E_OK) ){.      r
184d7 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
184d8 57 72 69 74 65 28 70 50 67 48 64 72 29 3b 0a 20  Write(pPgHdr);. 
184d9 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63     }..    if( rc
184da 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
184db 20 20 20 20 20 2f 2a 20 41 63 74 75 61 6c 6c 79       /* Actually
184dc 20 64 6f 20 74 68 65 20 75 70 64 61 74 65 20 6f   do the update o
184dd 66 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75  f the change cou
184de 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 61  nter */.      pa
184df 67 65 72 5f 77 72 69 74 65 5f 63 68 61 6e 67 65  ger_write_change
184e0 63 6f 75 6e 74 65 72 28 70 50 67 48 64 72 29 3b  counter(pPgHdr);
184e1 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72 75  ..      /* If ru
184e2 6e 6e 69 6e 67 20 69 6e 20 64 69 72 65 63 74 20  nning in direct 
184e3 6d 6f 64 65 2c 20 77 72 69 74 65 20 74 68 65 20  mode, write the 
184e4 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61 67 65  contents of page
184e5 20 31 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20   1 to the file. 
184e6 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 44 49 52  */.      if( DIR
184e7 45 43 54 5f 4d 4f 44 45 20 29 7b 0a 20 20 20 20  ECT_MODE ){.    
184e8 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a      const void *
184e9 7a 42 75 66 3b 0a 20 20 20 20 20 20 20 20 61 73  zBuf;.        as
184ea 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62  sert( pPager->db
184eb 46 69 6c 65 53 69 7a 65 3e 30 20 29 3b 0a 20 20  FileSize>0 );.  
184ec 20 20 20 20 20 20 43 4f 44 45 43 32 28 70 50 61        CODEC2(pPa
184ed 67 65 72 2c 20 70 50 67 48 64 72 2d 3e 70 44 61  ger, pPgHdr->pDa
184ee 74 61 2c 20 31 2c 20 36 2c 20 72 63 3d 53 51 4c  ta, 1, 6, rc=SQL
184ef 49 54 45 5f 4e 4f 4d 45 4d 2c 20 7a 42 75 66 29  ITE_NOMEM, zBuf)
184f0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
184f1 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
184f2 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
184f3 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61  lite3OsWrite(pPa
184f4 67 65 72 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 70  ger->fd, zBuf, p
184f5 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c  Pager->pageSize,
184f6 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70   0);.          p
184f7 50 61 67 65 72 2d 3e 61 53 74 61 74 5b 50 41 47  Pager->aStat[PAG
184f8 45 52 5f 53 54 41 54 5f 57 52 49 54 45 5d 2b 2b  ER_STAT_WRITE]++
184f9 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
184fa 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
184fb 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
184fc 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67     pPager->chang
184fd 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a  eCountDone = 1;.
184fe 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
184ff 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
18500 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75  Pager->changeCou
18501 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20  ntDone = 1;.    
18502 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
18503 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 70 61  * Release the pa
18504 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20 2a 2f  ge reference. */
18505 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
18506 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a  rUnref(pPgHdr);.
18507 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
18508 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74  .}../*.** Sync t
18509 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1850a 20 74 6f 20 64 69 73 6b 2e 20 54 68 69 73 20 69   to disk. This i
1850b 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 69 6e  s a no-op for in
1850c 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
1850d 73 0a 2a 2a 20 6f 72 20 70 61 67 65 73 20 77 69  s.** or pages wi
1850e 74 68 20 74 68 65 20 50 61 67 65 72 2e 6e 6f 53  th the Pager.noS
1850f 79 6e 63 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2a  ync flag set..**
18510 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
18511 6c 2c 20 6f 72 20 69 66 20 63 61 6c 6c 65 64 20  l, or if called 
18512 6f 6e 20 61 20 70 61 67 65 72 20 66 6f 72 20 77  on a pager for w
18513 68 69 63 68 20 69 74 20 69 73 20 61 20 6e 6f 2d  hich it is a no-
18514 6f 70 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63  op, this.** func
18515 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c  tion returns SQL
18516 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73  ITE_OK. Otherwis
18517 65 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63  e, an IO error c
18518 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
18519 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1851a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
1851b 67 65 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70  gerSync(Pager *p
1851c 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63  Pager){.  int rc
1851d 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1851e 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53  if( !pPager->noS
1851f 79 6e 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ync ){.    asser
18520 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20  t( !MEMDB );.   
18521 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53   rc = sqlite3OsS
18522 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ync(pPager->fd, 
18523 70 50 61 67 65 72 2d 3e 73 79 6e 63 46 6c 61 67  pPager->syncFlag
18524 73 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  s);.  }else if( 
18525 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
18526 64 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  d) ){.    assert
18527 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20  ( !MEMDB );.    
18528 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69  rc = sqlite3OsFi
18529 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67 65 72  leControl(pPager
1852a 2d 3e 66 64 2c 20 53 51 4c 49 54 45 5f 46 43 4e  ->fd, SQLITE_FCN
1852b 54 4c 5f 53 59 4e 43 5f 4f 4d 49 54 54 45 44 2c  TL_SYNC_OMITTED,
1852c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d   0);.    if( rc=
1852d 3d 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44  =SQLITE_NOTFOUND
1852e 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
1852f 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
18530 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
18531 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
18532 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79  unction may only
18533 20 62 65 20 63 61 6c 6c 65 64 20 77 68 69 6c 65   be called while
18534 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63   a write-transac
18535 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 69  tion is active i
18536 6e 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2e 20 49  n.** rollback. I
18537 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  f the connection
18538 20 69 73 20 69 6e 20 57 41 4c 20 6d 6f 64 65 2c   is in WAL mode,
18539 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20   this call is a 
1853a 6e 6f 2d 6f 70 2e 20 0a 2a 2a 20 4f 74 68 65 72  no-op. .** Other
1853b 77 69 73 65 2c 20 69 66 20 74 68 65 20 63 6f 6e  wise, if the con
1853c 6e 65 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  nection does not
1853d 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 6e   already have an
1853e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
1853f 6f 6e 20 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  on .** the datab
18540 61 73 65 20 66 69 6c 65 2c 20 61 6e 20 61 74 74  ase file, an att
18541 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20  empt is made to 
18542 6f 62 74 61 69 6e 20 6f 6e 65 2e 0a 2a 2a 0a 2a  obtain one..**.*
18543 2a 20 49 66 20 74 68 65 20 45 58 43 4c 55 53 49  * If the EXCLUSI
18544 56 45 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61  VE lock is alrea
18545 64 79 20 68 65 6c 64 20 6f 72 20 74 68 65 20 61  dy held or the a
18546 74 74 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e  ttempt to obtain
18547 20 69 74 20 69 73 0a 2a 2a 20 73 75 63 63 65 73   it is.** succes
18548 73 66 75 6c 2c 20 6f 72 20 74 68 65 20 63 6f 6e  sful, or the con
18549 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 57 41  nection is in WA
1854a 4c 20 6d 6f 64 65 2c 20 53 51 4c 49 54 45 5f 4f  L mode, SQLITE_O
1854b 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  K is returned..*
1854c 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 65 69 74  * Otherwise, eit
1854d 68 65 72 20 53 51 4c 49 54 45 5f 42 55 53 59 20  her SQLITE_BUSY 
1854e 6f 72 20 61 6e 20 53 51 4c 49 54 45 5f 49 4f 45  or an SQLITE_IOE
1854f 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64  RR_XXX error cod
18550 65 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65  e is .** returne
18551 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
18552 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
18553 50 61 67 65 72 45 78 63 6c 75 73 69 76 65 4c 6f  PagerExclusiveLo
18554 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ck(Pager *pPager
18555 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
18556 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72  LITE_OK;.  asser
18557 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74  t( pPager->eStat
18558 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f  e==PAGER_WRITER_
18559 43 41 43 48 45 4d 4f 44 20 0a 20 20 20 20 20 20  CACHEMOD .      
1855a 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 61   || pPager->eSta
1855b 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52  te==PAGER_WRITER
1855c 5f 44 42 4d 4f 44 20 0a 20 20 20 20 20 20 20 7c  _DBMOD .       |
1855d 7c 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65  | pPager->eState
1855e 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c  ==PAGER_WRITER_L
1855f 4f 43 4b 45 44 20 0a 20 20 29 3b 0a 20 20 61 73  OCKED .  );.  as
18560 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67  sert( assert_pag
18561 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29  er_state(pPager)
18562 20 29 3b 0a 20 20 69 66 28 20 30 3d 3d 70 61 67   );.  if( 0==pag
18563 65 72 55 73 65 57 61 6c 28 70 50 61 67 65 72 29  erUseWal(pPager)
18564 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67   ){.    rc = pag
18565 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28  er_wait_on_lock(
18566 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56  pPager, EXCLUSIV
18567 45 5f 4c 4f 43 4b 29 3b 0a 20 20 7d 0a 20 20 72  E_LOCK);.  }.  r
18568 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
18569 2a 2a 20 53 79 6e 63 20 74 68 65 20 64 61 74 61  ** Sync the data
1856a 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68  base file for th
1856b 65 20 70 61 67 65 72 20 70 50 61 67 65 72 2e 20  e pager pPager. 
1856c 7a 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74  zMaster points t
1856d 6f 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66  o the name.** of
1856e 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
1856f 6c 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75  l file that shou
18570 6c 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e  ld be written in
18571 74 6f 20 74 68 65 20 69 6e 64 69 76 69 64 75 61  to the individua
18572 6c 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  l.** journal fil
18573 65 2e 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 62  e. zMaster may b
18574 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73  e NULL, which is
18575 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20   interpreted as 
18576 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75  no master.** jou
18577 72 6e 61 6c 20 28 61 20 73 69 6e 67 6c 65 20 64  rnal (a single d
18578 61 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74  atabase transact
18579 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ion)..**.** This
1857a 20 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73   routine ensures
1857b 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a   that:.**.**   *
1857c 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69   The database fi
1857d 6c 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65  le change-counte
1857e 72 20 69 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a  r is updated,.**
1857f 20 20 20 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c     * the journal
18580 20 69 73 20 73 79 6e 63 65 64 20 28 75 6e 6c 65   is synced (unle
18581 73 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72  ss the atomic-wr
18582 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ite optimization
18583 20 69 73 20 75 73 65 64 29 2c 0a 2a 2a 20 20 20   is used),.**   
18584 2a 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65  * all dirty page
18585 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 74 6f  s are written to
18586 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
18587 6c 65 2c 20 0a 2a 2a 20 20 20 2a 20 74 68 65 20  le, .**   * the 
18588 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
18589 20 74 72 75 6e 63 61 74 65 64 20 28 69 66 20 72   truncated (if r
1858a 65 71 75 69 72 65 64 29 2c 20 61 6e 64 0a 2a 2a  equired), and.**
1858b 20 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 73     * the databas
1858c 65 20 66 69 6c 65 20 73 79 6e 63 65 64 2e 20 0a  e file synced. .
1858d 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74  **.** The only t
1858e 68 69 6e 67 20 74 68 61 74 20 72 65 6d 61 69 6e  hing that remain
1858f 73 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20  s to commit the 
18590 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 74  transaction is t
18591 6f 20 66 69 6e 61 6c 69 7a 65 20 0a 2a 2a 20 28  o finalize .** (
18592 64 65 6c 65 74 65 2c 20 74 72 75 6e 63 61 74 65  delete, truncate
18593 20 6f 72 20 7a 65 72 6f 20 74 68 65 20 66 69 72   or zero the fir
18594 73 74 20 70 61 72 74 20 6f 66 29 20 74 68 65 20  st part of) the 
18595 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72  journal file (or
18596 20 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20   .** delete the 
18597 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
18598 69 6c 65 20 69 66 20 73 70 65 63 69 66 69 65 64  ile if specified
18599 29 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  )..**.** Note th
1859a 61 74 20 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e  at if zMaster==N
1859b 55 4c 4c 2c 20 74 68 69 73 20 64 6f 65 73 20 6e  ULL, this does n
1859c 6f 74 20 6f 76 65 72 77 72 69 74 65 20 61 20 70  ot overwrite a p
1859d 72 65 76 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a  revious value.**
1859e 20 70 61 73 73 65 64 20 74 6f 20 61 6e 20 73 71   passed to an sq
1859f 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
185a0 50 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e  PhaseOne() call.
185a1 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  .**.** If the fi
185a2 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 20 2d 20  nal parameter - 
185a3 6e 6f 53 79 6e 63 20 2d 20 69 73 20 74 72 75 65  noSync - is true
185a4 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 62  , then the datab
185a5 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66 0a  ase file itself.
185a6 2a 2a 20 69 73 20 6e 6f 74 20 73 79 6e 63 65 64  ** is not synced
185a7 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73  . The caller mus
185a8 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61  t call sqlite3Pa
185a9 67 65 72 53 79 6e 63 28 29 20 64 69 72 65 63 74  gerSync() direct
185aa 6c 79 20 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 68  ly to.** sync th
185ab 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
185ac 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 43  before calling C
185ad 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20  ommitPhaseTwo() 
185ae 74 6f 20 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a  to delete the.**
185af 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e   journal file in
185b0 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2f 0a 53   this case..*/.S
185b1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
185b2 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  t sqlite3PagerCo
185b3 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 0a 20 20  mmitPhaseOne(.  
185b4 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20  Pager *pPager,  
185b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185b6 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20  /* Pager object 
185b7 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
185b8 2a 7a 4d 61 73 74 65 72 2c 20 20 20 20 20 20 20  *zMaster,       
185b9 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e       /* If not N
185ba 55 4c 4c 2c 20 74 68 65 20 6d 61 73 74 65 72 20  ULL, the master 
185bb 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a  journal name */.
185bc 20 20 69 6e 74 20 6e 6f 53 79 6e 63 20 20 20 20    int noSync    
185bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185be 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69    /* True to omi
185bf 74 20 74 68 65 20 78 53 79 6e 63 20 6f 6e 20 74  t the xSync on t
185c0 68 65 20 64 62 20 66 69 6c 65 20 2a 2f 0a 29 7b  he db file */.){
185c1 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
185c2 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
185c3 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
185c4 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
185c5 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d  pPager->eState==
185c6 50 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43  PAGER_WRITER_LOC
185c7 4b 45 44 0a 20 20 20 20 20 20 20 7c 7c 20 70 50  KED.       || pP
185c8 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41  ager->eState==PA
185c9 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48 45  GER_WRITER_CACHE
185ca 4d 4f 44 0a 20 20 20 20 20 20 20 7c 7c 20 70 50  MOD.       || pP
185cb 61 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41  ager->eState==PA
185cc 47 45 52 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44  GER_WRITER_DBMOD
185cd 0a 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65  .       || pPage
185ce 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
185cf 5f 45 52 52 4f 52 0a 20 20 29 3b 0a 20 20 61 73  _ERROR.  );.  as
185d0 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67  sert( assert_pag
185d1 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29  er_state(pPager)
185d2 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 20 70   );..  /* If a p
185d3 72 69 6f 72 20 65 72 72 6f 72 20 6f 63 63 75 72  rior error occur
185d4 72 65 64 2c 20 72 65 70 6f 72 74 20 74 68 61 74  red, report that
185d5 20 65 72 72 6f 72 20 61 67 61 69 6e 2e 20 2a 2f   error again. */
185d6 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61  .  if( NEVER(pPa
185d7 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20  ger->errCode) ) 
185d8 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65  return pPager->e
185d9 72 72 43 6f 64 65 3b 0a 0a 20 20 50 41 47 45 52  rrCode;..  PAGER
185da 54 52 41 43 45 28 28 22 44 41 54 41 42 41 53 45  TRACE(("DATABASE
185db 20 53 59 4e 43 3a 20 46 69 6c 65 3d 25 73 20 7a   SYNC: File=%s z
185dc 4d 61 73 74 65 72 3d 25 73 20 6e 53 69 7a 65 3d  Master=%s nSize=
185dd 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 70 50  %d\n", .      pP
185de 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c  ager->zFilename,
185df 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72   zMaster, pPager
185e0 2d 3e 64 62 53 69 7a 65 29 29 3b 0a 0a 20 20 2f  ->dbSize));..  /
185e1 2a 20 49 66 20 6e 6f 20 64 61 74 61 62 61 73 65  * If no database
185e2 20 63 68 61 6e 67 65 73 20 68 61 76 65 20 62 65   changes have be
185e3 65 6e 20 6d 61 64 65 2c 20 72 65 74 75 72 6e 20  en made, return 
185e4 65 61 72 6c 79 2e 20 2a 2f 0a 20 20 69 66 28 20  early. */.  if( 
185e5 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3c 50  pPager->eState<P
185e6 41 47 45 52 5f 57 52 49 54 45 52 5f 43 41 43 48  AGER_WRITER_CACH
185e7 45 4d 4f 44 20 29 20 72 65 74 75 72 6e 20 53 51  EMOD ) return SQ
185e8 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20  LITE_OK;..  if( 
185e9 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 2f 2a 20  MEMDB ){.    /* 
185ea 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e  If this is an in
185eb 2d 6d 65 6d 6f 72 79 20 64 62 2c 20 6f 72 20 6e  -memory db, or n
185ec 6f 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65  o pages have bee
185ed 6e 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 6f 72  n written to, or
185ee 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 66 75 6e   this.    ** fun
185ef 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 64  ction has alread
185f0 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20 69  y been called, i
185f1 74 20 69 73 20 6d 6f 73 74 6c 79 20 61 20 6e 6f  t is mostly a no
185f2 2d 6f 70 2e 20 20 48 6f 77 65 76 65 72 2c 20 61  -op.  However, a
185f3 6e 79 0a 20 20 20 20 2a 2a 20 62 61 63 6b 75 70  ny.    ** backup
185f4 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6e 65 65   in progress nee
185f5 64 73 20 74 6f 20 62 65 20 72 65 73 74 61 72 74  ds to be restart
185f6 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ed..    */.    s
185f7 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 74  qlite3BackupRest
185f8 61 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 63  art(pPager->pBac
185f9 6b 75 70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  kup);.  }else{. 
185fa 20 20 20 69 66 28 20 70 61 67 65 72 55 73 65 57     if( pagerUseW
185fb 61 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20  al(pPager) ){.  
185fc 20 20 20 20 50 67 48 64 72 20 2a 70 4c 69 73 74      PgHdr *pList
185fd 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65   = sqlite3Pcache
185fe 44 69 72 74 79 4c 69 73 74 28 70 50 61 67 65 72  DirtyList(pPager
185ff 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20  ->pPCache);.    
18600 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 4f 6e    PgHdr *pPageOn
18601 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28  e = 0;.      if(
18602 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20   pList==0 ){.   
18603 20 20 20 20 20 2f 2a 20 4d 75 73 74 20 68 61 76       /* Must hav
18604 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 70  e at least one p
18605 61 67 65 20 66 6f 72 20 74 68 65 20 57 41 4c 20  age for the WAL 
18606 63 6f 6d 6d 69 74 20 66 6c 61 67 2e 0a 20 20 20  commit flag..   
18607 20 20 20 20 20 2a 2a 20 54 69 63 6b 65 74 20 5b       ** Ticket [
18608 32 64 31 61 35 63 36 37 64 66 63 32 33 36 33 65  2d1a5c67dfc2363e
18609 34 34 66 32 39 64 39 62 62 64 35 37 66 5d 20 32  44f29d9bbd57f] 2
1860a 30 31 31 2d 30 35 2d 31 38 20 2a 2f 0a 20 20 20  011-05-18 */.   
1860b 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1860c 33 50 61 67 65 72 47 65 74 28 70 50 61 67 65 72  3PagerGet(pPager
1860d 2c 20 31 2c 20 26 70 50 61 67 65 4f 6e 65 29 3b  , 1, &pPageOne);
1860e 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 20 3d  .        pList =
1860f 20 70 50 61 67 65 4f 6e 65 3b 0a 20 20 20 20 20   pPageOne;.     
18610 20 20 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 79     pList->pDirty
18611 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
18612 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
18613 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20  SQLITE_OK );.   
18614 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 4c     if( ALWAYS(pL
18615 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ist) ){.        
18616 72 63 20 3d 20 70 61 67 65 72 57 61 6c 46 72 61  rc = pagerWalFra
18617 6d 65 73 28 70 50 61 67 65 72 2c 20 70 4c 69 73  mes(pPager, pLis
18618 74 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  t, pPager->dbSiz
18619 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  e, 1);.      }. 
1861a 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
1861b 72 55 6e 72 65 66 28 70 50 61 67 65 4f 6e 65 29  rUnref(pPageOne)
1861c 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
1861d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1861e 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63       sqlite3Pcac
1861f 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65  heCleanAll(pPage
18620 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20  r->pPCache);.   
18621 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
18622 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c        /* The fol
18623 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 75 70 64  lowing block upd
18624 61 74 65 73 20 74 68 65 20 63 68 61 6e 67 65 2d  ates the change-
18625 63 6f 75 6e 74 65 72 2e 20 45 78 61 63 74 6c 79  counter. Exactly
18626 20 68 6f 77 20 69 74 0a 20 20 20 20 20 20 2a 2a   how it.      **
18627 20 64 6f 65 73 20 74 68 69 73 20 64 65 70 65 6e   does this depen
18628 64 73 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72  ds on whether or
18629 20 6e 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d   not the atomic-
1862a 75 70 64 61 74 65 20 6f 70 74 69 6d 69 7a 61 74  update optimizat
1862b 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 77 61 73  ion.      ** was
1862c 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70   enabled at comp
1862d 69 6c 65 20 74 69 6d 65 2c 20 61 6e 64 20 69 66  ile time, and if
1862e 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f   this transactio
1862f 6e 20 6d 65 65 74 73 20 74 68 65 20 0a 20 20 20  n meets the .   
18630 20 20 20 2a 2a 20 72 75 6e 74 69 6d 65 20 63 72     ** runtime cr
18631 69 74 65 72 69 61 20 74 6f 20 75 73 65 20 74 68  iteria to use th
18632 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 20 20  e operation: .  
18633 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
18634 20 20 20 2a 20 54 68 65 20 66 69 6c 65 2d 73 79     * The file-sy
18635 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 74 68  stem supports th
18636 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 70  e atomic-write p
18637 72 6f 70 65 72 74 79 20 66 6f 72 0a 20 20 20 20  roperty for.    
18638 20 20 2a 2a 20 20 20 20 20 20 62 6c 6f 63 6b 73    **      blocks
18639 20 6f 66 20 73 69 7a 65 20 70 61 67 65 2d 73 69   of size page-si
1863a 7a 65 2c 20 61 6e 64 20 0a 20 20 20 20 20 20 2a  ze, and .      *
1863b 2a 20 20 20 20 2a 20 54 68 69 73 20 63 6f 6d 6d  *    * This comm
1863c 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f  it is not part o
1863d 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 20 74  f a multi-file t
1863e 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 0a  ransaction, and.
1863f 20 20 20 20 20 20 2a 2a 20 20 20 20 2a 20 45 78        **    * Ex
18640 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 68  actly one page h
18641 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  as been modified
18642 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 68   and store in th
18643 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  e journal file..
18644 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
18645 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a  * If the optimiz
18646 61 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 65 6e  ation was not en
18647 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65  abled at compile
18648 20 74 69 6d 65 2c 20 74 68 65 6e 20 74 68 65 0a   time, then the.
18649 20 20 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 69        ** pager_i
1864a 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65  ncr_changecounte
1864b 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  r() function is 
1864c 63 61 6c 6c 65 64 20 74 6f 20 75 70 64 61 74 65  called to update
1864d 20 74 68 65 20 63 68 61 6e 67 65 0a 20 20 20 20   the change.    
1864e 20 20 2a 2a 20 63 6f 75 6e 74 65 72 20 69 6e 20    ** counter in 
1864f 27 69 6e 64 69 72 65 63 74 2d 6d 6f 64 65 27 2e  'indirect-mode'.
18650 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61   If the optimiza
18651 74 69 6f 6e 20 69 73 20 63 6f 6d 70 69 6c 65 64  tion is compiled
18652 20 69 6e 20 62 75 74 0a 20 20 20 20 20 20 2a 2a   in but.      **
18653 20 69 73 20 6e 6f 74 20 61 70 70 6c 69 63 61 62   is not applicab
18654 6c 65 20 74 6f 20 74 68 69 73 20 74 72 61 6e 73  le to this trans
18655 61 63 74 69 6f 6e 2c 20 63 61 6c 6c 20 73 71 6c  action, call sql
18656 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74  ite3JournalCreat
18657 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  e().      ** to 
18658 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6a 6f  make sure the jo
18659 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 61  urnal file has a
1865a 63 74 75 61 6c 6c 79 20 62 65 65 6e 20 63 72 65  ctually been cre
1865b 61 74 65 64 2c 20 74 68 65 6e 20 63 61 6c 6c 0a  ated, then call.
1865c 20 20 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 69        ** pager_i
1865d 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65  ncr_changecounte
1865e 72 28 29 20 74 6f 20 75 70 64 61 74 65 20 74 68  r() to update th
1865f 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
18660 20 69 6e 20 69 6e 64 69 72 65 63 74 0a 20 20 20   in indirect.   
18661 20 20 20 2a 2a 20 6d 6f 64 65 2e 20 0a 20 20 20     ** mode. .   
18662 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f     **.      ** O
18663 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65  therwise, if the
18664 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
18665 20 62 6f 74 68 20 65 6e 61 62 6c 65 64 20 61 6e   both enabled an
18666 64 20 61 70 70 6c 69 63 61 62 6c 65 2c 0a 20 20  d applicable,.  
18667 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 61 6c 6c      ** then call
18668 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e   pager_incr_chan
18669 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 75  gecounter() to u
1866a 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65  pdate the change
1866b 2d 63 6f 75 6e 74 65 72 0a 20 20 20 20 20 20 2a  -counter.      *
1866c 2a 20 69 6e 20 27 64 69 72 65 63 74 27 20 6d 6f  * in 'direct' mo
1866d 64 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  de. In this case
1866e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1866f 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 0a  e will never be.
18670 20 20 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64        ** created
18671 20 66 6f 72 20 74 68 69 73 20 74 72 61 6e 73 61   for this transa
18672 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a  ction..      */.
18673 20 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f    #ifdef SQLITE_
18674 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52  ENABLE_ATOMIC_WR
18675 49 54 45 0a 20 20 20 20 20 20 50 67 48 64 72 20  ITE.      PgHdr 
18676 2a 70 50 67 3b 0a 20 20 20 20 20 20 61 73 73 65  *pPg;.      asse
18677 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65  rt( isOpen(pPage
18678 72 2d 3e 6a 66 64 29 20 0a 20 20 20 20 20 20 20  r->jfd) .       
18679 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a      || pPager->j
1867a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45  ournalMode==PAGE
1867b 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
1867c 46 20 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c  F .           ||
1867d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
1867e 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
1867f 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 0a 20 20 20  NALMODE_WAL .   
18680 20 20 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20     );.      if( 
18681 21 7a 4d 61 73 74 65 72 20 26 26 20 69 73 4f 70  !zMaster && isOp
18682 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
18683 0a 20 20 20 20 20 20 20 26 26 20 70 50 61 67 65  .       && pPage
18684 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 6a  r->journalOff==j
18685 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 28 70 50  rnlBufferSize(pP
18686 61 67 65 72 29 20 0a 20 20 20 20 20 20 20 26 26  ager) .       &&
18687 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e   pPager->dbSize>
18688 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53  =pPager->dbOrigS
18689 69 7a 65 0a 20 20 20 20 20 20 20 26 26 20 28 30  ize.       && (0
1868a 3d 3d 28 70 50 67 20 3d 20 73 71 6c 69 74 65 33  ==(pPg = sqlite3
1868b 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28  PcacheDirtyList(
1868c 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
1868d 29 20 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70 44 69  ) || 0==pPg->pDi
1868e 72 74 79 29 0a 20 20 20 20 20 20 29 7b 0a 20 20  rty).      ){.  
1868f 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20        /* Update 
18690 74 68 65 20 64 62 20 66 69 6c 65 20 63 68 61 6e  the db file chan
18691 67 65 20 63 6f 75 6e 74 65 72 20 76 69 61 20 74  ge counter via t
18692 68 65 20 64 69 72 65 63 74 2d 77 72 69 74 65 20  he direct-write 
18693 6d 65 74 68 6f 64 2e 20 54 68 65 20 0a 20 20 20  method. The .   
18694 20 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e       ** followin
18695 67 20 63 61 6c 6c 20 77 69 6c 6c 20 6d 6f 64 69  g call will modi
18696 66 79 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  fy the in-memory
18697 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
18698 6f 66 20 70 61 67 65 20 31 20 0a 20 20 20 20 20  of page 1 .     
18699 20 20 20 2a 2a 20 74 6f 20 69 6e 63 6c 75 64 65     ** to include
1869a 20 74 68 65 20 75 70 64 61 74 65 64 20 63 68 61   the updated cha
1869b 6e 67 65 20 63 6f 75 6e 74 65 72 20 61 6e 64 20  nge counter and 
1869c 74 68 65 6e 20 77 72 69 74 65 20 70 61 67 65 20  then write page 
1869d 31 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 69  1 .        ** di
1869e 72 65 63 74 6c 79 20 74 6f 20 74 68 65 20 64 61  rectly to the da
1869f 74 61 62 61 73 65 20 66 69 6c 65 2e 20 42 65 63  tabase file. Bec
186a0 61 75 73 65 20 6f 66 20 74 68 65 20 61 74 6f 6d  ause of the atom
186a1 69 63 2d 77 72 69 74 65 20 0a 20 20 20 20 20 20  ic-write .      
186a2 20 20 2a 2a 20 70 72 6f 70 65 72 74 79 20 6f 66    ** property of
186a3 20 74 68 65 20 68 6f 73 74 20 66 69 6c 65 2d 73   the host file-s
186a4 79 73 74 65 6d 2c 20 74 68 69 73 20 69 73 20 73  ystem, this is s
186a5 61 66 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  afe..        */.
186a6 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67          rc = pag
186a7 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f  er_incr_changeco
186a8 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 31 29  unter(pPager, 1)
186a9 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
186aa 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
186ab 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65  te3JournalCreate
186ac 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20  (pPager->jfd);. 
186ad 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
186ae 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
186af 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
186b0 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e  _incr_changecoun
186b1 74 65 72 28 70 50 61 67 65 72 2c 20 30 29 3b 0a  ter(pPager, 0);.
186b2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
186b3 7d 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 20 20  }.  #else.      
186b4 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f  rc = pager_incr_
186b5 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50  changecounter(pP
186b6 61 67 65 72 2c 20 30 29 3b 0a 20 20 23 65 6e 64  ager, 0);.  #end
186b7 69 66 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  if.      if( rc!
186b8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
186b9 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f  o commit_phase_o
186ba 6e 65 5f 65 78 69 74 3b 0a 20 20 0a 20 20 20 20  ne_exit;.  .    
186bb 20 20 2f 2a 20 49 66 20 74 68 69 73 20 74 72 61    /* If this tra
186bc 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6d 61 64  nsaction has mad
186bd 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  e the database s
186be 6d 61 6c 6c 65 72 2c 20 74 68 65 6e 20 61 6c 6c  maller, then all
186bf 20 70 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20   pages.      ** 
186c0 62 65 69 6e 67 20 64 69 73 63 61 72 64 65 64 20  being discarded 
186c1 62 79 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f  by the truncatio
186c2 6e 20 6d 75 73 74 20 62 65 20 77 72 69 74 74 65  n must be writte
186c3 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n to the journal
186c4 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e 0a  .      ** file..
186c5 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
186c6 2a 20 42 65 66 6f 72 65 20 72 65 61 64 69 6e 67  * Before reading
186c7 20 74 68 65 20 70 61 67 65 73 20 77 69 74 68 20   the pages with 
186c8 70 61 67 65 20 6e 75 6d 62 65 72 73 20 6c 61 72  page numbers lar
186c9 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20  ger than the .  
186ca 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 76      ** current v
186cb 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62  alue of Pager.db
186cc 53 69 7a 65 2c 20 73 65 74 20 64 62 53 69 7a 65  Size, set dbSize
186cd 20 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61 6c   back to the val
186ce 75 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74  ue.      ** that
186cf 20 69 74 20 74 6f 6f 6b 20 61 74 20 74 68 65 20   it took at the 
186d0 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61  start of the tra
186d1 6e 73 61 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77  nsaction. Otherw
186d2 69 73 65 2c 20 74 68 65 0a 20 20 20 20 20 20 2a  ise, the.      *
186d3 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  * calls to sqlit
186d4 65 33 50 61 67 65 72 47 65 74 28 29 20 72 65 74  e3PagerGet() ret
186d5 75 72 6e 20 7a 65 72 6f 65 64 20 70 61 67 65 73  urn zeroed pages
186d6 20 69 6e 73 74 65 61 64 20 6f 66 20 0a 20 20 20   instead of .   
186d7 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 64 61     ** reading da
186d8 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ta from the data
186d9 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20  base file..     
186da 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50   */.      if( pP
186db 61 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61  ager->dbSize<pPa
186dc 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20  ger->dbOrigSize 
186dd 0a 20 20 20 20 20 20 20 26 26 20 70 50 61 67 65  .       && pPage
186de 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d  r->journalMode!=
186df 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
186e0 45 5f 4f 46 46 0a 20 20 20 20 20 20 29 7b 0a 20  E_OFF.      ){. 
186e1 20 20 20 20 20 20 20 50 67 6e 6f 20 69 3b 20 20         Pgno i;  
186e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
186e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
186e4 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
186e5 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20  iable */.       
186e6 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 6b 69   const Pgno iSki
186e7 70 20 3d 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  p = PAGER_MJ_PGN
186e8 4f 28 70 50 61 67 65 72 29 3b 20 2f 2a 20 50 65  O(pPager); /* Pe
186e9 6e 64 69 6e 67 20 6c 6f 63 6b 20 70 61 67 65 20  nding lock page 
186ea 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  */.        const
186eb 20 50 67 6e 6f 20 64 62 53 69 7a 65 20 3d 20 70   Pgno dbSize = p
186ec 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 20 20  Pager->dbSize;  
186ed 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
186ee 20 69 6d 61 67 65 20 73 69 7a 65 20 2a 2f 20 0a   image size */ .
186ef 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
186f0 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d  dbSize = pPager-
186f1 3e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20 20 20  >dbOrigSize;.   
186f2 20 20 20 20 20 66 6f 72 28 20 69 3d 64 62 53 69       for( i=dbSi
186f3 7a 65 2b 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d  ze+1; i<=pPager-
186f4 3e 64 62 4f 72 69 67 53 69 7a 65 3b 20 69 2b 2b  >dbOrigSize; i++
186f5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
186f6 28 20 21 73 71 6c 69 74 65 33 42 69 74 76 65 63  ( !sqlite3Bitvec
186f7 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e  Test(pPager->pIn
186f8 4a 6f 75 72 6e 61 6c 2c 20 69 29 20 26 26 20 69  Journal, i) && i
186f9 21 3d 69 53 6b 69 70 20 29 7b 0a 20 20 20 20 20  !=iSkip ){.     
186fa 20 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50         PgHdr *pP
186fb 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
186fc 20 2f 2a 20 50 61 67 65 20 74 6f 20 6a 6f 75 72   /* Page to jour
186fd 6e 61 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  nal */.         
186fe 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
186ff 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20  agerGet(pPager, 
18700 69 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 20 20  i, &pPage);.    
18701 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
18702 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
18703 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e   commit_phase_on
18704 65 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20  e_exit;.        
18705 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
18706 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
18707 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
18708 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
18709 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  (pPage);.       
1870a 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1870b 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f  ITE_OK ) goto co
1870c 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65  mmit_phase_one_e
1870d 78 69 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  xit;.          }
1870e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1870f 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a     pPager->dbSiz
18710 65 20 3d 20 64 62 53 69 7a 65 3b 0a 20 20 20 20  e = dbSize;.    
18711 20 20 7d 20 0a 20 20 0a 20 20 20 20 20 20 2f 2a    } .  .      /*
18712 20 57 72 69 74 65 20 74 68 65 20 6d 61 73 74 65   Write the maste
18713 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69  r journal name i
18714 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  nto the journal 
18715 66 69 6c 65 2e 20 49 66 20 61 20 6d 61 73 74 65  file. If a maste
18716 72 20 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72  r .      ** jour
18717 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 68 61  nal file name ha
18718 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77  s already been w
18719 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f  ritten to the jo
1871a 75 72 6e 61 6c 20 66 69 6c 65 2c 20 0a 20 20 20  urnal file, .   
1871b 20 20 20 2a 2a 20 6f 72 20 69 66 20 7a 4d 61 73     ** or if zMas
1871c 74 65 72 20 69 73 20 4e 55 4c 4c 20 28 6e 6f 20  ter is NULL (no 
1871d 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 29 2c  master journal),
1871e 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 20   then this call 
1871f 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20  is a no-op..    
18720 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20    */.      rc = 
18721 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e  writeMasterJourn
18722 61 6c 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74  al(pPager, zMast
18723 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  er);.      if( r
18724 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
18725 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65  oto commit_phase
18726 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 0a 20 20  _one_exit;.  .  
18727 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20      /* Sync the 
18728 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64  journal file and
18729 20 77 72 69 74 65 20 61 6c 6c 20 64 69 72 74 79   write all dirty
1872a 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 64 61   pages to the da
1872b 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 2a 2a  tabase..      **
1872c 20 49 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 75   If the atomic-u
1872d 70 64 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  pdate optimizati
1872e 6f 6e 20 69 73 20 62 65 69 6e 67 20 75 73 65 64  on is being used
1872f 2c 20 74 68 69 73 20 73 79 6e 63 20 77 69 6c 6c  , this sync will
18730 20 6e 6f 74 20 0a 20 20 20 20 20 20 2a 2a 20 63   not .      ** c
18731 72 65 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61  reate the journa
18732 6c 20 66 69 6c 65 20 6f 72 20 70 65 72 66 6f 72  l file or perfor
18733 6d 20 61 6e 79 20 72 65 61 6c 20 49 4f 2e 0a 20  m any real IO.. 
18734 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
18735 20 42 65 63 61 75 73 65 20 74 68 65 20 63 68 61   Because the cha
18736 6e 67 65 2d 63 6f 75 6e 74 65 72 20 70 61 67 65  nge-counter page
18737 20 77 61 73 20 6a 75 73 74 20 6d 6f 64 69 66 69   was just modifi
18738 65 64 2c 20 75 6e 6c 65 73 73 20 74 68 65 0a 20  ed, unless the. 
18739 20 20 20 20 20 2a 2a 20 61 74 6f 6d 69 63 2d 75       ** atomic-u
1873a 70 64 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  pdate optimizati
1873b 6f 6e 20 69 73 20 75 73 65 64 20 69 74 20 69 73  on is used it is
1873c 20 61 6c 6d 6f 73 74 20 63 65 72 74 61 69 6e 20   almost certain 
1873d 74 68 61 74 20 74 68 65 0a 20 20 20 20 20 20 2a  that the.      *
1873e 2a 20 6a 6f 75 72 6e 61 6c 20 72 65 71 75 69 72  * journal requir
1873f 65 73 20 61 20 73 79 6e 63 20 68 65 72 65 2e 20  es a sync here. 
18740 48 6f 77 65 76 65 72 2c 20 69 6e 20 6c 6f 63 6b  However, in lock
18741 69 6e 67 5f 6d 6f 64 65 3d 65 78 63 6c 75 73 69  ing_mode=exclusi
18742 76 65 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20 61  ve.      ** on a
18743 20 73 79 73 74 65 6d 20 75 6e 64 65 72 20 6d 65   system under me
18744 6d 6f 72 79 20 70 72 65 73 73 75 72 65 20 69 74  mory pressure it
18745 20 69 73 20 6a 75 73 74 20 70 6f 73 73 69 62 6c   is just possibl
18746 65 20 74 68 61 74 20 74 68 69 73 20 69 73 20 0a  e that this is .
18747 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20 74 68 65        ** not the
18748 20 63 61 73 65 2e 20 49 6e 20 74 68 69 73 20 63   case. In this c
18749 61 73 65 20 69 74 20 69 73 20 6c 69 6b 65 6c 79  ase it is likely
1874a 20 65 6e 6f 75 67 68 20 74 68 61 74 20 74 68 65   enough that the
1874b 20 72 65 64 75 6e 64 61 6e 74 0a 20 20 20 20 20   redundant.     
1874c 20 2a 2a 20 78 53 79 6e 63 28 29 20 63 61 6c 6c   ** xSync() call
1874d 20 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64   will be changed
1874e 20 74 6f 20 61 20 6e 6f 2d 6f 70 20 62 79 20 74   to a no-op by t
1874f 68 65 20 4f 53 20 61 6e 79 68 6f 77 2e 20 0a 20  he OS anyhow. . 
18750 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63       */.      rc
18751 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70   = syncJournal(p
18752 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20  Pager, 0);.     
18753 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
18754 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74  OK ) goto commit
18755 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b  _phase_one_exit;
18756 0a 20 20 0a 20 20 20 20 20 20 72 63 20 3d 20 70  .  .      rc = p
18757 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c  ager_write_pagel
18758 69 73 74 28 70 50 61 67 65 72 2c 73 71 6c 69 74  ist(pPager,sqlit
18759 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73  e3PcacheDirtyLis
1875a 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
1875b 65 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  e));.      if( r
1875c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1875d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1875e 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc!=SQLITE_IOERR
1875f 5f 42 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20  _BLOCKED );.    
18760 20 20 20 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f      goto commit_
18761 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a  phase_one_exit;.
18762 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
18763 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e  lite3PcacheClean
18764 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61  All(pPager->pPCa
18765 63 68 65 29 3b 0a 20 20 0a 20 20 20 20 20 20 2f  che);.  .      /
18766 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f 6e  * If the file on
18767 20 64 69 73 6b 20 69 73 20 6e 6f 74 20 74 68 65   disk is not the
18768 20 73 61 6d 65 20 73 69 7a 65 20 61 73 20 74 68   same size as th
18769 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65  e database image
1876a 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20  ,.      ** then 
1876b 75 73 65 20 70 61 67 65 72 5f 74 72 75 6e 63 61  use pager_trunca
1876c 74 65 20 74 6f 20 67 72 6f 77 20 6f 72 20 73 68  te to grow or sh
1876d 72 69 6e 6b 20 74 68 65 20 66 69 6c 65 20 68 65  rink the file he
1876e 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  re..      */.   
1876f 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64     if( pPager->d
18770 62 53 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 64  bSize!=pPager->d
18771 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20  bFileSize ){.   
18772 20 20 20 20 20 50 67 6e 6f 20 6e 4e 65 77 20 3d       Pgno nNew =
18773 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20   pPager->dbSize 
18774 2d 20 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  - (pPager->dbSiz
18775 65 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  e==PAGER_MJ_PGNO
18776 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20  (pPager));.     
18777 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
18778 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
18779 5f 57 52 49 54 45 52 5f 44 42 4d 4f 44 20 29 3b  _WRITER_DBMOD );
1877a 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61  .        rc = pa
1877b 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50 61  ger_truncate(pPa
1877c 67 65 72 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20  ger, nNew);.    
1877d 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1877e 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d  TE_OK ) goto com
1877f 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78  mit_phase_one_ex
18780 69 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20  it;.      }.  . 
18781 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c       /* Finally,
18782 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61   sync the databa
18783 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20  se file. */.    
18784 20 20 69 66 28 20 21 6e 6f 53 79 6e 63 20 29 7b    if( !noSync ){
18785 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
18786 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 70  lite3PagerSync(p
18787 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a  Pager);.      }.
18788 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22        IOTRACE(("
18789 44 42 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50  DBSYNC %p\n", pP
1878a 61 67 65 72 29 29 0a 20 20 20 20 7d 0a 20 20 7d  ager)).    }.  }
1878b 0a 0a 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f  ..commit_phase_o
1878c 6e 65 5f 65 78 69 74 3a 0a 20 20 69 66 28 20 72  ne_exit:.  if( r
1878d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1878e 21 70 61 67 65 72 55 73 65 57 61 6c 28 70 50 61  !pagerUseWal(pPa
1878f 67 65 72 29 20 29 7b 0a 20 20 20 20 70 50 61 67  ger) ){.    pPag
18790 65 72 2d 3e 65 53 74 61 74 65 20 3d 20 50 41 47  er->eState = PAG
18791 45 52 5f 57 52 49 54 45 52 5f 46 49 4e 49 53 48  ER_WRITER_FINISH
18792 45 44 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ED;.  }.  return
18793 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57   rc;.}.../*.** W
18794 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
18795 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65  n is called, the
18796 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68   database file h
18797 61 73 20 62 65 65 6e 20 63 6f 6d 70 6c 65 74 65  as been complete
18798 6c 79 0a 2a 2a 20 75 70 64 61 74 65 64 20 74 6f  ly.** updated to
18799 20 72 65 66 6c 65 63 74 20 74 68 65 20 63 68 61   reflect the cha
1879a 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 65  nges made by the
1879b 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
1879c 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 79 6e 63  tion and.** sync
1879d 65 64 20 74 6f 20 64 69 73 6b 2e 20 54 68 65 20  ed to disk. The 
1879e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 74 69  journal file sti
1879f 6c 6c 20 65 78 69 73 74 73 20 69 6e 20 74 68 65  ll exists in the
187a0 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 0a 2a 2a   file-system .**
187a1 20 74 68 6f 75 67 68 2c 20 61 6e 64 20 69 66 20   though, and if 
187a2 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73  a failure occurs
187a3 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69   at this point i
187a4 74 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c  t will eventuall
187a5 79 0a 2a 2a 20 62 65 20 75 73 65 64 20 61 73 20  y.** be used as 
187a6 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e  a hot-journal an
187a7 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72  d the current tr
187a8 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 65 64  ansaction rolled
187a9 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   back..**.** Thi
187aa 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c  s function final
187ab 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c  izes the journal
187ac 20 66 69 6c 65 2c 20 65 69 74 68 65 72 20 62 79   file, either by
187ad 20 64 65 6c 65 74 69 6e 67 2c 20 0a 2a 2a 20 74   deleting, .** t
187ae 72 75 6e 63 61 74 69 6e 67 20 6f 72 20 70 61 72  runcating or par
187af 74 69 61 6c 6c 79 20 7a 65 72 6f 69 6e 67 20 69  tially zeroing i
187b0 74 2c 20 73 6f 20 74 68 61 74 20 69 74 20 63 61  t, so that it ca
187b1 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 0a 2a 2a  nnot be used .**
187b2 20 66 6f 72 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c   for hot-journal
187b3 20 72 6f 6c 6c 62 61 63 6b 2e 20 4f 6e 63 65 20   rollback. Once 
187b4 74 68 69 73 20 69 73 20 64 6f 6e 65 20 74 68 65  this is done the
187b5 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a   transaction is.
187b6 2a 2a 20 69 72 72 65 76 6f 63 61 62 6c 79 20 63  ** irrevocably c
187b7 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  ommitted..**.** 
187b8 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
187b9 72 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20  rs, an IO error 
187ba 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
187bb 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 0a 2a   and the pager.*
187bc 2a 20 6d 6f 76 65 73 20 69 6e 74 6f 20 74 68 65  * moves into the
187bd 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 4f 74   error state. Ot
187be 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f  herwise, SQLITE_
187bf 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  OK is returned..
187c0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
187c1 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
187c2 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  erCommitPhaseTwo
187c3 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
187c4 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
187c5 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
187c6 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
187c7 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20  n code */..  /* 
187c8 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f  This routine sho
187c9 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65  uld not be calle
187ca 64 20 69 66 20 61 20 70 72 69 6f 72 20 65 72 72  d if a prior err
187cb 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e  or has occurred.
187cc 0a 20 20 2a 2a 20 42 75 74 20 69 66 20 28 64 75  .  ** But if (du
187cd 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72  e to a coding er
187ce 72 6f 72 20 65 6c 73 65 77 68 65 72 65 20 69 6e  ror elsewhere in
187cf 20 74 68 65 20 73 79 73 74 65 6d 29 20 69 74 20   the system) it 
187d0 64 6f 65 73 20 67 65 74 0a 20 20 2a 2a 20 63 61  does get.  ** ca
187d1 6c 6c 65 64 2c 20 6a 75 73 74 20 72 65 74 75 72  lled, just retur
187d2 6e 20 74 68 65 20 73 61 6d 65 20 65 72 72 6f 72  n the same error
187d3 20 63 6f 64 65 20 77 69 74 68 6f 75 74 20 64 6f   code without do
187d4 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 2a 2f  ing anything. */
187d5 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61  .  if( NEVER(pPa
187d6 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20  ger->errCode) ) 
187d7 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65  return pPager->e
187d8 72 72 43 6f 64 65 3b 0a 0a 20 20 61 73 73 65 72  rrCode;..  asser
187d9 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74  t( pPager->eStat
187da 65 3d 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f  e==PAGER_WRITER_
187db 4c 4f 43 4b 45 44 0a 20 20 20 20 20 20 20 7c 7c  LOCKED.       ||
187dc 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d   pPager->eState=
187dd 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 46 49  =PAGER_WRITER_FI
187de 4e 49 53 48 45 44 0a 20 20 20 20 20 20 20 7c 7c  NISHED.       ||
187df 20 28 70 61 67 65 72 55 73 65 57 61 6c 28 70 50   (pagerUseWal(pP
187e0 61 67 65 72 29 20 26 26 20 70 50 61 67 65 72 2d  ager) && pPager-
187e1 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57  >eState==PAGER_W
187e2 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 29 0a  RITER_CACHEMOD).
187e3 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61    );.  assert( a
187e4 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74  ssert_pager_stat
187e5 65 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20  e(pPager) );..  
187e6 2f 2a 20 41 6e 20 6f 70 74 69 6d 69 7a 61 74 69  /* An optimizati
187e7 6f 6e 2e 20 49 66 20 74 68 65 20 64 61 74 61 62  on. If the datab
187e8 61 73 65 20 77 61 73 20 6e 6f 74 20 61 63 74 75  ase was not actu
187e9 61 6c 6c 79 20 6d 6f 64 69 66 69 65 64 20 64 75  ally modified du
187ea 72 69 6e 67 0a 20 20 2a 2a 20 74 68 69 73 20 74  ring.  ** this t
187eb 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20  ransaction, the 
187ec 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67  pager is running
187ed 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f   in exclusive-mo
187ee 64 65 20 61 6e 64 20 69 73 0a 20 20 2a 2a 20 75  de and is.  ** u
187ef 73 69 6e 67 20 70 65 72 73 69 73 74 65 6e 74 20  sing persistent 
187f0 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 6e 20 74  journals, then t
187f1 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
187f2 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20  a no-op..  **.  
187f3 2a 2a 20 54 68 65 20 73 74 61 72 74 20 6f 66 20  ** The start of 
187f4 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
187f5 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61   currently conta
187f6 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 6a 6f 75  ins a single jou
187f7 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65  rnal .  ** heade
187f8 72 20 77 69 74 68 20 74 68 65 20 6e 52 65 63 20  r with the nRec 
187f9 66 69 65 6c 64 20 73 65 74 20 74 6f 20 30 2e 20  field set to 0. 
187fa 49 66 20 73 75 63 68 20 61 20 6a 6f 75 72 6e 61  If such a journa
187fb 6c 20 69 73 20 75 73 65 64 20 61 73 0a 20 20 2a  l is used as.  *
187fc 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  * a hot-journal 
187fd 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e  during hot-journ
187fe 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 30 20 63  al rollback, 0 c
187ff 68 61 6e 67 65 73 20 77 69 6c 6c 20 62 65 20 6d  hanges will be m
18800 61 64 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20  ade.  ** to the 
18801 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 53  database file. S
18802 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65  o there is no ne
18803 65 64 20 74 6f 20 7a 65 72 6f 20 74 68 65 20 6a  ed to zero the j
18804 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61  ournal .  ** hea
18805 64 65 72 2e 20 53 69 6e 63 65 20 74 68 65 20 70  der. Since the p
18806 61 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c 75  ager is in exclu
18807 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 72 65  sive mode, there
18808 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a   is no need.  **
18809 20 74 6f 20 64 72 6f 70 20 61 6e 79 20 6c 6f 63   to drop any loc
1880a 6b 73 20 65 69 74 68 65 72 2e 0a 20 20 2a 2f 0a  ks either..  */.
1880b 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 53    if( pPager->eS
1880c 74 61 74 65 3d 3d 50 41 47 45 52 5f 57 52 49 54  tate==PAGER_WRIT
1880d 45 52 5f 4c 4f 43 4b 45 44 20 0a 20 20 20 26 26  ER_LOCKED .   &&
1880e 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69   pPager->exclusi
1880f 76 65 4d 6f 64 65 20 0a 20 20 20 26 26 20 70 50  veMode .   && pP
18810 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
18811 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
18812 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 29  MODE_PERSIST.  )
18813 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
18814 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
18815 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  ==JOURNAL_HDR_SZ
18816 28 70 50 61 67 65 72 29 20 7c 7c 20 21 70 50 61  (pPager) || !pPa
18817 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
18818 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65  );.    pPager->e
18819 53 74 61 74 65 20 3d 20 50 41 47 45 52 5f 52 45  State = PAGER_RE
1881a 41 44 45 52 3b 0a 20 20 20 20 72 65 74 75 72 6e  ADER;.    return
1881b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
1881c 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22  .  PAGERTRACE(("
1881d 43 4f 4d 4d 49 54 20 25 64 5c 6e 22 2c 20 50 41  COMMIT %d\n", PA
1881e 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b  GERID(pPager)));
1881f 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e  .  rc = pager_en
18820 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50  d_transaction(pP
18821 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65  ager, pPager->se
18822 74 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 75  tMaster);.  retu
18823 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70  rn pager_error(p
18824 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f  Pager, rc);.}../
18825 2a 0a 2a 2a 20 49 66 20 61 20 77 72 69 74 65 20  *.** If a write 
18826 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f  transaction is o
18827 70 65 6e 2c 20 74 68 65 6e 20 61 6c 6c 20 63 68  pen, then all ch
18828 61 6e 67 65 73 20 6d 61 64 65 20 77 69 74 68 69  anges made withi
18829 6e 20 74 68 65 20 0a 2a 2a 20 74 72 61 6e 73 61  n the .** transa
1882a 63 74 69 6f 6e 20 61 72 65 20 72 65 76 65 72 74  ction are revert
1882b 65 64 20 61 6e 64 20 74 68 65 20 63 75 72 72 65  ed and the curre
1882c 6e 74 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63  nt write-transac
1882d 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a  tion is closed..
1882e 2a 2a 20 54 68 65 20 70 61 67 65 72 20 66 61 6c  ** The pager fal
1882f 6c 73 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52  ls back to PAGER
18830 5f 52 45 41 44 45 52 20 73 74 61 74 65 20 69 66  _READER state if
18831 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20   successful, or 
18832 50 41 47 45 52 5f 45 52 52 4f 52 0a 2a 2a 20 73  PAGER_ERROR.** s
18833 74 61 74 65 20 69 66 20 61 6e 20 65 72 72 6f 72  tate if an error
18834 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 49   occurs..**.** I
18835 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 61  f the pager is a
18836 6c 72 65 61 64 79 20 69 6e 20 50 41 47 45 52 5f  lready in PAGER_
18837 45 52 52 4f 52 20 73 74 61 74 65 20 77 68 65 6e  ERROR state when
18838 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
18839 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 69 74 20  s called,.** it 
1883a 72 65 74 75 72 6e 73 20 50 61 67 65 72 2e 65 72  returns Pager.er
1883b 72 43 6f 64 65 20 69 6d 6d 65 64 69 61 74 65 6c  rCode immediatel
1883c 79 2e 20 4e 6f 20 77 6f 72 6b 20 69 73 20 70 65  y. No work is pe
1883d 72 66 6f 72 6d 65 64 20 69 6e 20 74 68 69 73 20  rformed in this 
1883e 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65  case..**.** Othe
1883f 72 77 69 73 65 2c 20 69 6e 20 72 6f 6c 6c 62 61  rwise, in rollba
18840 63 6b 20 6d 6f 64 65 2c 20 74 68 69 73 20 66 75  ck mode, this fu
18841 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20  nction performs 
18842 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a  two functions:.*
18843 2a 0a 2a 2a 20 20 20 31 29 20 49 74 20 72 6f 6c  *.**   1) It rol
18844 6c 73 20 62 61 63 6b 20 74 68 65 20 6a 6f 75 72  ls back the jour
18845 6e 61 6c 20 66 69 6c 65 2c 20 72 65 73 74 6f 72  nal file, restor
18846 69 6e 67 20 61 6c 6c 20 64 61 74 61 62 61 73 65  ing all database
18847 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 20 20   file and .**   
18848 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63     in-memory cac
18849 68 65 20 70 61 67 65 73 20 74 6f 20 74 68 65 20  he pages to the 
1884a 73 74 61 74 65 20 74 68 65 79 20 77 65 72 65 20  state they were 
1884b 69 6e 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e  in when the tran
1884c 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20  saction.**      
1884d 77 61 73 20 6f 70 65 6e 65 64 2c 20 61 6e 64 0a  was opened, and.
1884e 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 74 20 66 69  **.**   2) It fi
1884f 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72  nalizes the jour
18850 6e 61 6c 20 66 69 6c 65 2c 20 73 6f 20 74 68 61  nal file, so tha
18851 74 20 69 74 20 69 73 20 6e 6f 74 20 75 73 65 64  t it is not used
18852 20 66 6f 72 20 68 6f 74 0a 2a 2a 20 20 20 20 20   for hot.**     
18853 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6e 79   rollback at any
18854 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75   point in the fu
18855 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 61  ture..**.** Fina
18856 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  lization of the 
18857 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 74 61  journal file (ta
18858 73 6b 20 32 29 20 69 73 20 6f 6e 6c 79 20 70 65  sk 2) is only pe
18859 72 66 6f 72 6d 65 64 20 69 66 20 74 68 65 20 0a  rformed if the .
1885a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 73  ** rollback is s
1885b 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a 0a 2a 2a  uccessful..**.**
1885c 20 49 6e 20 57 41 4c 20 6d 6f 64 65 2c 20 61 6c   In WAL mode, al
1885d 6c 20 63 61 63 68 65 2d 65 6e 74 72 69 65 73 20  l cache-entries 
1885e 63 6f 6e 74 61 69 6e 69 6e 67 20 64 61 74 61 20  containing data 
1885f 6d 6f 64 69 66 69 65 64 20 77 69 74 68 69 6e 20  modified within 
18860 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74  the.** current t
18861 72 61 6e 73 61 63 74 69 6f 6e 20 61 72 65 20 65  ransaction are e
18862 69 74 68 65 72 20 65 78 70 65 6c 6c 65 64 20 66  ither expelled f
18863 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 6f 72  rom the cache or
18864 20 72 65 76 65 72 74 65 64 20 74 6f 0a 2a 2a 20   reverted to.** 
18865 74 68 65 69 72 20 70 72 65 2d 74 72 61 6e 73 61  their pre-transa
18866 63 74 69 6f 6e 20 73 74 61 74 65 20 62 79 20 72  ction state by r
18867 65 2d 72 65 61 64 69 6e 67 20 64 61 74 61 20 66  e-reading data f
18868 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
18869 20 6f 72 0a 2a 2a 20 57 41 4c 20 66 69 6c 65 73   or.** WAL files
1886a 2e 20 54 68 65 20 57 41 4c 20 74 72 61 6e 73 61  . The WAL transa
1886b 63 74 69 6f 6e 20 69 73 20 74 68 65 6e 20 63 6c  ction is then cl
1886c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  osed..*/.SQLITE_
1886d 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1886e 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
1886f 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
18870 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
18871 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
18872 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
18873 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 41 47 45  n code */.  PAGE
18874 52 54 52 41 43 45 28 28 22 52 4f 4c 4c 42 41 43  RTRACE(("ROLLBAC
18875 4b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  K %d\n", PAGERID
18876 28 70 50 61 67 65 72 29 29 29 3b 0a 0a 20 20 2f  (pPager)));..  /
18877 2a 20 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28  * PagerRollback(
18878 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20  ) is a no-op if 
18879 63 61 6c 6c 65 64 20 69 6e 20 52 45 41 44 45 52  called in READER
1887a 20 6f 72 20 4f 50 45 4e 20 73 74 61 74 65 2e 20   or OPEN state. 
1887b 49 66 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65  If.  ** the page
1887c 72 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  r is already in 
1887d 74 68 65 20 45 52 52 4f 52 20 73 74 61 74 65 2c  the ERROR state,
1887e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 69 73   the rollback is
1887f 20 6e 6f 74 20 0a 20 20 2a 2a 20 61 74 74 65 6d   not .  ** attem
18880 70 74 65 64 20 68 65 72 65 2e 20 49 6e 73 74 65  pted here. Inste
18881 61 64 2c 20 74 68 65 20 65 72 72 6f 72 20 63 6f  ad, the error co
18882 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74  de is returned t
18883 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20  o the caller..  
18884 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 61 73 73  */.  assert( ass
18885 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28  ert_pager_state(
18886 70 50 61 67 65 72 29 20 29 3b 0a 20 20 69 66 28  pPager) );.  if(
18887 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3d   pPager->eState=
18888 3d 50 41 47 45 52 5f 45 52 52 4f 52 20 29 20 72  =PAGER_ERROR ) r
18889 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72  eturn pPager->er
1888a 72 43 6f 64 65 3b 0a 20 20 69 66 28 20 70 50 61  rCode;.  if( pPa
1888b 67 65 72 2d 3e 65 53 74 61 74 65 3c 3d 50 41 47  ger->eState<=PAG
1888c 45 52 5f 52 45 41 44 45 52 20 29 20 72 65 74 75  ER_READER ) retu
1888d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20  rn SQLITE_OK;.. 
1888e 20 69 66 28 20 70 61 67 65 72 55 73 65 57 61 6c   if( pagerUseWal
1888f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20  (pPager) ){.    
18890 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 72 63 20  int rc2;.    rc 
18891 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61  = sqlite3PagerSa
18892 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 2c 20  vepoint(pPager, 
18893 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
18894 43 4b 2c 20 2d 31 29 3b 0a 20 20 20 20 72 63 32  CK, -1);.    rc2
18895 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61   = pager_end_tra
18896 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c  nsaction(pPager,
18897 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
18898 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  er);.    if( rc=
18899 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20  =SQLITE_OK ) rc 
1889a 3d 20 72 63 32 3b 0a 20 20 7d 65 6c 73 65 20 69  = rc2;.  }else i
1889b 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  f( !isOpen(pPage
1889c 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65  r->jfd) || pPage
1889d 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52  r->eState==PAGER
1889e 5f 57 52 49 54 45 52 5f 4c 4f 43 4b 45 44 20 29  _WRITER_LOCKED )
1889f 7b 0a 20 20 20 20 69 6e 74 20 65 53 74 61 74 65  {.    int eState
188a0 20 3d 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74   = pPager->eStat
188a1 65 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  e;.    rc = page
188a2 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
188a3 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20  n(pPager, 0);.  
188a4 20 20 69 66 28 20 21 4d 45 4d 44 42 20 26 26 20    if( !MEMDB && 
188a5 65 53 74 61 74 65 3e 50 41 47 45 52 5f 57 52 49  eState>PAGER_WRI
188a6 54 45 52 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 20  TER_LOCKED ){.  
188a7 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6e 20      /* This can 
188a8 68 61 70 70 65 6e 20 75 73 69 6e 67 20 6a 6f 75  happen using jou
188a9 72 6e 61 6c 5f 6d 6f 64 65 3d 6f 66 66 2e 20 4d  rnal_mode=off. M
188aa 6f 76 65 20 74 68 65 20 70 61 67 65 72 20 74 6f  ove the pager to
188ab 20 74 68 65 20 65 72 72 6f 72 20 0a 20 20 20 20   the error .    
188ac 20 20 2a 2a 20 73 74 61 74 65 20 74 6f 20 69 6e    ** state to in
188ad 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20  dicate that the 
188ae 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
188af 63 61 63 68 65 20 6d 61 79 20 6e 6f 74 20 62 65  cache may not be
188b0 20 74 72 75 73 74 65 64 2e 0a 20 20 20 20 20 20   trusted..      
188b1 2a 2a 20 41 6e 79 20 61 63 74 69 76 65 20 72 65  ** Any active re
188b2 61 64 65 72 73 20 77 69 6c 6c 20 67 65 74 20 53  aders will get S
188b3 51 4c 49 54 45 5f 41 42 4f 52 54 2e 0a 20 20 20  QLITE_ABORT..   
188b4 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 67     */.      pPag
188b5 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51  er->errCode = SQ
188b6 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 20 20  LITE_ABORT;.    
188b7 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65    pPager->eState
188b8 20 3d 20 50 41 47 45 52 5f 45 52 52 4f 52 3b 0a   = PAGER_ERROR;.
188b9 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
188ba 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
188bb 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70      rc = pager_p
188bc 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20  layback(pPager, 
188bd 30 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  0);.  }..  asser
188be 74 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74  t( pPager->eStat
188bf 65 3d 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20  e==PAGER_READER 
188c0 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  || rc!=SQLITE_OK
188c1 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63   );.  assert( rc
188c2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72  ==SQLITE_OK || r
188c3 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 20  c==SQLITE_FULL. 
188c4 20 20 20 20 20 20 20 20 20 7c 7c 20 72 63 3d 3d           || rc==
188c5 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
188c6 28 72 63 26 30 78 46 46 29 3d 3d 53 51 4c 49 54  (rc&0xFF)==SQLIT
188c7 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20 2f 2a  E_IOERR );..  /*
188c8 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
188c9 75 72 73 20 64 75 72 69 6e 67 20 61 20 52 4f 4c  urs during a ROL
188ca 4c 42 41 43 4b 2c 20 77 65 20 63 61 6e 20 6e 6f  LBACK, we can no
188cb 20 6c 6f 6e 67 65 72 20 74 72 75 73 74 20 74 68   longer trust th
188cc 65 20 70 61 67 65 72 0a 20 20 2a 2a 20 63 61 63  e pager.  ** cac
188cd 68 65 2e 20 53 6f 20 63 61 6c 6c 20 70 61 67 65  he. So call page
188ce 72 5f 65 72 72 6f 72 28 29 20 6f 6e 20 74 68 65  r_error() on the
188cf 20 77 61 79 20 6f 75 74 20 74 6f 20 6d 61 6b 65   way out to make
188d0 20 61 6e 79 20 65 72 72 6f 72 20 70 65 72 73 69   any error persi
188d1 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 72 65  stent..  */.  re
188d2 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72  turn pager_error
188d3 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a  (pPager, rc);.}.
188d4 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
188d5 55 45 20 69 66 20 74 68 65 20 64 61 74 61 62 61  UE if the databa
188d6 73 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65  se file is opene
188d7 64 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 52 65  d read-only.  Re
188d8 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 66  turn FALSE.** if
188d9 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
188da 20 28 69 6e 20 74 68 65 6f 72 79 29 20 77 72 69   (in theory) wri
188db 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  table..*/.SQLITE
188dc 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69  _PRIVATE u8 sqli
188dd 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e  te3PagerIsreadon
188de 6c 79 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ly(Pager *pPager
188df 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  ){.  return pPag
188e0 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 3b 0a 7d 0a  er->readOnly;.}.
188e1 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
188e2 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65  e number of refe
188e3 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61  rences to the pa
188e4 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ger..*/.SQLITE_P
188e5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
188e6 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28  e3PagerRefcount(
188e7 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
188e8 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
188e9 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70  PcacheRefCount(p
188ea 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b  Pager->pPCache);
188eb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
188ec 20 74 68 65 20 61 70 70 72 6f 78 69 6d 61 74 65   the approximate
188ed 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
188ee 20 6f 66 20 6d 65 6d 6f 72 79 20 63 75 72 72 65   of memory curre
188ef 6e 74 6c 79 0a 2a 2a 20 75 73 65 64 20 62 79 20  ntly.** used by 
188f0 74 68 65 20 70 61 67 65 72 20 61 6e 64 20 69 74  the pager and it
188f1 73 20 61 73 73 6f 63 69 61 74 65 64 20 63 61 63  s associated cac
188f2 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  he..*/.SQLITE_PR
188f3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
188f4 33 50 61 67 65 72 4d 65 6d 55 73 65 64 28 50 61  3PagerMemUsed(Pa
188f5 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
188f6 69 6e 74 20 70 65 72 50 61 67 65 53 69 7a 65 20  int perPageSize 
188f7 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  = pPager->pageSi
188f8 7a 65 20 2b 20 70 50 61 67 65 72 2d 3e 6e 45 78  ze + pPager->nEx
188f9 74 72 61 20 2b 20 73 69 7a 65 6f 66 28 50 67 48  tra + sizeof(PgH
188fa 64 72 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  dr).            
188fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
188fc 20 20 20 20 20 20 20 20 20 2b 20 35 2a 73 69 7a           + 5*siz
188fd 65 6f 66 28 76 6f 69 64 2a 29 3b 0a 20 20 72 65  eof(void*);.  re
188fe 74 75 72 6e 20 70 65 72 50 61 67 65 53 69 7a 65  turn perPageSize
188ff 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61  *sqlite3PcachePa
18900 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e  gecount(pPager->
18901 70 50 43 61 63 68 65 29 0a 20 20 20 20 20 20 20  pPCache).       
18902 20 20 20 20 2b 20 73 71 6c 69 74 65 33 4d 61 6c      + sqlite3Mal
18903 6c 6f 63 53 69 7a 65 28 70 50 61 67 65 72 29 0a  locSize(pPager).
18904 20 20 20 20 20 20 20 20 20 20 20 2b 20 70 50 61             + pPa
18905 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d  ger->pageSize;.}
18906 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
18907 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66  he number of ref
18908 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 73  erences to the s
18909 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a 2a  pecified page..*
1890a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1890b 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
1890c 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62  rPageRefcount(Db
1890d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20  Page *pPage){.  
1890e 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 63  return sqlite3Pc
1890f 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74  achePageRefcount
18910 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 23 69 66 64  (pPage);.}..#ifd
18911 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f  ef SQLITE_TEST./
18912 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
18913 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  e is used for te
18914 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73  sting and analys
18915 69 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  is only..*/.SQLI
18916 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 2a  TE_PRIVATE int *
18917 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 61 74  sqlite3PagerStat
18918 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  s(Pager *pPager)
18919 7b 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 61  {.  static int a
1891a 5b 31 31 5d 3b 0a 20 20 61 5b 30 5d 20 3d 20 73  [11];.  a[0] = s
1891b 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43  qlite3PcacheRefC
1891c 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43  ount(pPager->pPC
1891d 61 63 68 65 29 3b 0a 20 20 61 5b 31 5d 20 3d 20  ache);.  a[1] = 
1891e 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67  sqlite3PcachePag
1891f 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70  ecount(pPager->p
18920 50 43 61 63 68 65 29 3b 0a 20 20 61 5b 32 5d 20  PCache);.  a[2] 
18921 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47  = sqlite3PcacheG
18922 65 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 67  etCachesize(pPag
18923 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20  er->pPCache);.  
18924 61 5b 33 5d 20 3d 20 70 50 61 67 65 72 2d 3e 65  a[3] = pPager->e
18925 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45  State==PAGER_OPE
18926 4e 20 3f 20 2d 31 20 3a 20 28 69 6e 74 29 20 70  N ? -1 : (int) p
18927 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20  Pager->dbSize;. 
18928 20 61 5b 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[4] = pPager->
18929 65 53 74 61 74 65 3b 0a 20 20 61 5b 35 5d 20 3d  eState;.  a[5] =
1892a 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
1892b 3b 0a 20 20 61 5b 36 5d 20 3d 20 70 50 61 67 65  ;.  a[6] = pPage
1892c 72 2d 3e 61 53 74 61 74 5b 50 41 47 45 52 5f 53  r->aStat[PAGER_S
1892d 54 41 54 5f 48 49 54 5d 3b 0a 20 20 61 5b 37 5d  TAT_HIT];.  a[7]
1892e 20 3d 20 70 50 61 67 65 72 2d 3e 61 53 74 61 74   = pPager->aStat
1892f 5b 50 41 47 45 52 5f 53 54 41 54 5f 4d 49 53 53  [PAGER_STAT_MISS
18930 5d 3b 0a 20 20 61 5b 38 5d 20 3d 20 30 3b 20 20  ];.  a[8] = 0;  
18931 2f 2a 20 55 73 65 64 20 74 6f 20 62 65 20 70 50  /* Used to be pP
18932 61 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20  ager->nOvfl */. 
18933 20 61 5b 39 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[9] = pPager->
18934 6e 52 65 61 64 3b 0a 20 20 61 5b 31 30 5d 20 3d  nRead;.  a[10] =
18935 20 70 50 61 67 65 72 2d 3e 61 53 74 61 74 5b 50   pPager->aStat[P
18936 41 47 45 52 5f 53 54 41 54 5f 57 52 49 54 45 5d  AGER_STAT_WRITE]
18937 3b 0a 20 20 72 65 74 75 72 6e 20 61 3b 0a 7d 0a  ;.  return a;.}.
18938 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61  #endif../*.** Pa
18939 72 61 6d 65 74 65 72 20 65 53 74 61 74 20 6d 75  rameter eStat mu
1893a 73 74 20 62 65 20 65 69 74 68 65 72 20 53 51 4c  st be either SQL
1893b 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43  ITE_DBSTATUS_CAC
1893c 48 45 5f 48 49 54 20 6f 72 0a 2a 2a 20 53 51 4c  HE_HIT or.** SQL
1893d 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43  ITE_DBSTATUS_CAC
1893e 48 45 5f 4d 49 53 53 2e 20 42 65 66 6f 72 65 20  HE_MISS. Before 
1893f 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 6e 56 61  returning, *pnVa
18940 6c 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  l is incremented
18941 20 62 79 20 74 68 65 0a 2a 2a 20 63 75 72 72 65   by the.** curre
18942 6e 74 20 63 61 63 68 65 20 68 69 74 20 6f 72 20  nt cache hit or 
18943 6d 69 73 73 20 63 6f 75 6e 74 2c 20 61 63 63 6f  miss count, acco
18944 72 64 69 6e 67 20 74 6f 20 74 68 65 20 76 61 6c  rding to the val
18945 75 65 20 6f 66 20 65 53 74 61 74 2e 20 49 66 20  ue of eStat. If 
18946 74 68 65 20 0a 2a 2a 20 72 65 73 65 74 20 70 61  the .** reset pa
18947 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a  rameter is non-z
18948 65 72 6f 2c 20 74 68 65 20 63 61 63 68 65 20 68  ero, the cache h
18949 69 74 20 6f 72 20 6d 69 73 73 20 63 6f 75 6e 74  it or miss count
1894a 20 69 73 20 7a 65 72 6f 65 64 20 62 65 66 6f 72   is zeroed befor
1894b 65 20 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 2e  e .** returning.
1894c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1894d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
1894e 61 67 65 72 43 61 63 68 65 53 74 61 74 28 50 61  agerCacheStat(Pa
1894f 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
18950 20 65 53 74 61 74 2c 20 69 6e 74 20 72 65 73 65   eStat, int rese
18951 74 2c 20 69 6e 74 20 2a 70 6e 56 61 6c 29 7b 0a  t, int *pnVal){.
18952 0a 20 20 61 73 73 65 72 74 28 20 65 53 74 61 74  .  assert( eStat
18953 3d 3d 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55  ==SQLITE_DBSTATU
18954 53 5f 43 41 43 48 45 5f 48 49 54 0a 20 20 20 20  S_CACHE_HIT.    
18955 20 20 20 7c 7c 20 65 53 74 61 74 3d 3d 53 51 4c     || eStat==SQL
18956 49 54 45 5f 44 42 53 54 41 54 55 53 5f 43 41 43  ITE_DBSTATUS_CAC
18957 48 45 5f 4d 49 53 53 0a 20 20 20 20 20 20 20 7c  HE_MISS.       |
18958 7c 20 65 53 74 61 74 3d 3d 53 51 4c 49 54 45 5f  | eStat==SQLITE_
18959 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f 57  DBSTATUS_CACHE_W
1895a 52 49 54 45 0a 20 20 29 3b 0a 0a 20 20 61 73 73  RITE.  );..  ass
1895b 65 72 74 28 20 53 51 4c 49 54 45 5f 44 42 53 54  ert( SQLITE_DBST
1895c 41 54 55 53 5f 43 41 43 48 45 5f 48 49 54 2b 31  ATUS_CACHE_HIT+1
1895d 3d 3d 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55  ==SQLITE_DBSTATU
1895e 53 5f 43 41 43 48 45 5f 4d 49 53 53 20 29 3b 0a  S_CACHE_MISS );.
1895f 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45    assert( SQLITE
18960 5f 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f  _DBSTATUS_CACHE_
18961 48 49 54 2b 32 3d 3d 53 51 4c 49 54 45 5f 44 42  HIT+2==SQLITE_DB
18962 53 54 41 54 55 53 5f 43 41 43 48 45 5f 57 52 49  STATUS_CACHE_WRI
18963 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  TE );.  assert( 
18964 50 41 47 45 52 5f 53 54 41 54 5f 48 49 54 3d 3d  PAGER_STAT_HIT==
18965 30 20 26 26 20 50 41 47 45 52 5f 53 54 41 54 5f  0 && PAGER_STAT_
18966 4d 49 53 53 3d 3d 31 20 26 26 20 50 41 47 45 52  MISS==1 && PAGER
18967 5f 53 54 41 54 5f 57 52 49 54 45 3d 3d 32 20 29  _STAT_WRITE==2 )
18968 3b 0a 0a 20 20 2a 70 6e 56 61 6c 20 2b 3d 20 70  ;..  *pnVal += p
18969 50 61 67 65 72 2d 3e 61 53 74 61 74 5b 65 53 74  Pager->aStat[eSt
1896a 61 74 20 2d 20 53 51 4c 49 54 45 5f 44 42 53 54  at - SQLITE_DBST
1896b 41 54 55 53 5f 43 41 43 48 45 5f 48 49 54 5d 3b  ATUS_CACHE_HIT];
1896c 0a 20 20 69 66 28 20 72 65 73 65 74 20 29 7b 0a  .  if( reset ){.
1896d 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 74 61      pPager->aSta
1896e 74 5b 65 53 74 61 74 20 2d 20 53 51 4c 49 54 45  t[eStat - SQLITE
1896f 5f 44 42 53 54 41 54 55 53 5f 43 41 43 48 45 5f  _DBSTATUS_CACHE_
18970 48 49 54 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  HIT] = 0;.  }.}.
18971 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
18972 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 6e  ue if this is an
18973 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72   in-memory pager
18974 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18975 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
18976 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65  agerIsMemdb(Page
18977 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65  r *pPager){.  re
18978 74 75 72 6e 20 4d 45 4d 44 42 3b 0a 7d 0a 0a 2f  turn MEMDB;.}../
18979 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20  *.** Check that 
1897a 74 68 65 72 65 20 61 72 65 20 61 74 20 6c 65 61  there are at lea
1897b 73 74 20 6e 53 61 76 65 70 6f 69 6e 74 20 73 61  st nSavepoint sa
1897c 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e 2e 20 49  vepoints open. I
1897d 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 63  f there are.** c
1897e 75 72 72 65 6e 74 6c 79 20 6c 65 73 73 20 74 68  urrently less th
1897f 61 6e 20 6e 53 61 76 65 70 6f 69 6e 74 73 20 6f  an nSavepoints o
18980 70 65 6e 2c 20 74 68 65 6e 20 6f 70 65 6e 20 6f  pen, then open o
18981 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61 76 65 70  ne or more savep
18982 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 6d 61 6b 65  oints.** to make
18983 20 75 70 20 74 68 65 20 64 69 66 66 65 72 65 6e   up the differen
18984 63 65 2e 20 49 66 20 74 68 65 20 6e 75 6d 62 65  ce. If the numbe
18985 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 20  r of savepoints 
18986 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 71  is already.** eq
18987 75 61 6c 20 74 6f 20 6e 53 61 76 65 70 6f 69 6e  ual to nSavepoin
18988 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e  t, then this fun
18989 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
1898a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d  ..**.** If a mem
1898b 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
1898c 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d  ails, SQLITE_NOM
1898d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  EM is returned. 
1898e 49 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a 2a 20  If an error .** 
1898f 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6f 70 65  occurs while ope
18990 6e 69 6e 67 20 74 68 65 20 73 75 62 2d 6a 6f 75  ning the sub-jou
18991 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20  rnal file, then 
18992 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  an IO error code
18993 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e   is.** returned.
18994 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49   Otherwise, SQLI
18995 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45  TE_OK..*/.SQLITE
18996 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
18997 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 76  ite3PagerOpenSav
18998 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50  epoint(Pager *pP
18999 61 67 65 72 2c 20 69 6e 74 20 6e 53 61 76 65 70  ager, int nSavep
1899a 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20  oint){.  int rc 
1899b 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
1899c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1899d 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
1899e 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72 72  e */.  int nCurr
1899f 65 6e 74 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53  ent = pPager->nS
189a0 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20  avepoint;       
189a1 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d 62   /* Current numb
189a2 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 73  er of savepoints
189a3 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
189a4 50 61 67 65 72 2d 3e 65 53 74 61 74 65 3e 3d 50  Pager->eState>=P
189a5 41 47 45 52 5f 57 52 49 54 45 52 5f 4c 4f 43 4b  AGER_WRITER_LOCK
189a6 45 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ED );.  assert( 
189a7 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61  assert_pager_sta
189a8 74 65 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20  te(pPager) );.. 
189a9 20 69 66 28 20 6e 53 61 76 65 70 6f 69 6e 74 3e   if( nSavepoint>
189aa 6e 43 75 72 72 65 6e 74 20 26 26 20 70 50 61 67  nCurrent && pPag
189ab 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29  er->useJournal )
189ac 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20  {.    int ii;   
189ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
189ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
189af 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
189b0 6c 65 20 2a 2f 0a 20 20 20 20 50 61 67 65 72 53  le */.    PagerS
189b1 61 76 65 70 6f 69 6e 74 20 2a 61 4e 65 77 3b 20  avepoint *aNew; 
189b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
189b3 20 20 2f 2a 20 4e 65 77 20 50 61 67 65 72 2e 61    /* New Pager.a
189b4 53 61 76 65 70 6f 69 6e 74 20 61 72 72 61 79 20  Savepoint array 
189b5 2a 2f 0a 0a 20 20 20 20 2f 2a 20 47 72 6f 77 20  */..    /* Grow 
189b6 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 70  the Pager.aSavep
189b7 6f 69 6e 74 20 61 72 72 61 79 20 75 73 69 6e 67  oint array using
189b8 20 72 65 61 6c 6c 6f 63 28 29 2e 20 52 65 74 75   realloc(). Retu
189b9 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a  rn SQLITE_NOMEM.
189ba 20 20 20 20 2a 2a 20 69 66 20 74 68 65 20 61 6c      ** if the al
189bb 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2e 20  location fails. 
189bc 4f 74 68 65 72 77 69 73 65 2c 20 7a 65 72 6f 20  Otherwise, zero 
189bd 74 68 65 20 6e 65 77 20 70 6f 72 74 69 6f 6e 20  the new portion 
189be 69 6e 20 63 61 73 65 20 61 20 0a 20 20 20 20 2a  in case a .    *
189bf 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  * malloc failure
189c0 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 70 6f   occurs while po
189c1 70 75 6c 61 74 69 6e 67 20 69 74 20 69 6e 20 74  pulating it in t
189c2 68 65 20 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f 70  he for(...) loop
189c3 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20   below..    */. 
189c4 20 20 20 61 4e 65 77 20 3d 20 28 50 61 67 65 72     aNew = (Pager
189c5 53 61 76 65 70 6f 69 6e 74 20 2a 29 73 71 6c 69  Savepoint *)sqli
189c6 74 65 33 52 65 61 6c 6c 6f 63 28 0a 20 20 20 20  te3Realloc(.    
189c7 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76      pPager->aSav
189c8 65 70 6f 69 6e 74 2c 20 73 69 7a 65 6f 66 28 50  epoint, sizeof(P
189c9 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29 2a 6e  agerSavepoint)*n
189ca 53 61 76 65 70 6f 69 6e 74 0a 20 20 20 20 29 3b  Savepoint.    );
189cb 0a 20 20 20 20 69 66 28 20 21 61 4e 65 77 20 29  .    if( !aNew )
189cc 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
189cd 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
189ce 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 61   }.    memset(&a
189cf 4e 65 77 5b 6e 43 75 72 72 65 6e 74 5d 2c 20 30  New[nCurrent], 0
189d0 2c 20 28 6e 53 61 76 65 70 6f 69 6e 74 2d 6e 43  , (nSavepoint-nC
189d1 75 72 72 65 6e 74 29 20 2a 20 73 69 7a 65 6f 66  urrent) * sizeof
189d2 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29  (PagerSavepoint)
189d3 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 61  );.    pPager->a
189d4 53 61 76 65 70 6f 69 6e 74 20 3d 20 61 4e 65 77  Savepoint = aNew
189d5 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61  ;..    /* Popula
189d6 74 65 20 74 68 65 20 50 61 67 65 72 53 61 76 65  te the PagerSave
189d7 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65 73  point structures
189d8 20 6a 75 73 74 20 61 6c 6c 6f 63 61 74 65 64 2e   just allocated.
189d9 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e   */.    for(ii=n
189da 43 75 72 72 65 6e 74 3b 20 69 69 3c 6e 53 61 76  Current; ii<nSav
189db 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  epoint; ii++){. 
189dc 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 6e 4f       aNew[ii].nO
189dd 72 69 67 20 3d 20 70 50 61 67 65 72 2d 3e 64 62  rig = pPager->db
189de 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20  Size;.      if( 
189df 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
189e0 66 64 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a  fd) && pPager->j
189e1 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 29 7b 0a 20  ournalOff>0 ){. 
189e2 20 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e         aNew[ii].
189e3 69 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72  iOffset = pPager
189e4 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20  ->journalOff;.  
189e5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
189e6 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66     aNew[ii].iOff
189e7 73 65 74 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44  set = JOURNAL_HD
189e8 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20  R_SZ(pPager);.  
189e9 20 20 20 20 7d 0a 20 20 20 20 20 20 61 4e 65 77      }.      aNew
189ea 5b 69 69 5d 2e 69 53 75 62 52 65 63 20 3d 20 70  [ii].iSubRec = p
189eb 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b 0a  Pager->nSubRec;.
189ec 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 70        aNew[ii].p
189ed 49 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 73 71  InSavepoint = sq
189ee 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74  lite3BitvecCreat
189ef 65 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  e(pPager->dbSize
189f0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 61 4e  );.      if( !aN
189f1 65 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f  ew[ii].pInSavepo
189f2 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72  int ){.        r
189f3 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
189f4 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  EM;.      }.    
189f5 20 20 69 66 28 20 70 61 67 65 72 55 73 65 57 61    if( pagerUseWa
189f6 6c 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20  l(pPager) ){.   
189f7 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 53       sqlite3WalS
189f8 61 76 65 70 6f 69 6e 74 28 70 50 61 67 65 72 2d  avepoint(pPager-
189f9 3e 70 57 61 6c 2c 20 61 4e 65 77 5b 69 69 5d 2e  >pWal, aNew[ii].
189fa 61 57 61 6c 44 61 74 61 29 3b 0a 20 20 20 20 20  aWalData);.     
189fb 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d   }.      pPager-
189fc 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 69 69  >nSavepoint = ii
189fd 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  +1;.    }.    as
189fe 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53  sert( pPager->nS
189ff 61 76 65 70 6f 69 6e 74 3d 3d 6e 53 61 76 65 70  avepoint==nSavep
18a00 6f 69 6e 74 20 29 3b 0a 20 20 20 20 61 73 73 65  oint );.    asse
18a01 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72  rtTruncateConstr
18a02 61 69 6e 74 28 70 50 61 67 65 72 29 3b 0a 20 20  aint(pPager);.  
18a03 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
18a04 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
18a05 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
18a06 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20   to rollback or 
18a07 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29  release (commit)
18a08 20 61 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a   a savepoint..**
18a09 20 54 68 65 20 73 61 76 65 70 6f 69 6e 74 20 74   The savepoint t
18a0a 6f 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c  o release or rol
18a0b 6c 62 61 63 6b 20 6e 65 65 64 20 6e 6f 74 20 62  lback need not b
18a0c 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  e the most recen
18a0d 74 6c 79 20 0a 2a 2a 20 63 72 65 61 74 65 64 20  tly .** created 
18a0e 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a  savepoint..**.**
18a0f 20 50 61 72 61 6d 65 74 65 72 20 6f 70 20 69 73   Parameter op is
18a10 20 61 6c 77 61 79 73 20 65 69 74 68 65 72 20 53   always either S
18a11 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
18a12 4b 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52  K or SAVEPOINT_R
18a13 45 4c 45 41 53 45 2e 0a 2a 2a 20 49 66 20 69 74  ELEASE..** If it
18a14 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45   is SAVEPOINT_RE
18a15 4c 45 41 53 45 2c 20 74 68 65 6e 20 72 65 6c 65  LEASE, then rele
18a16 61 73 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20  ase and destroy 
18a17 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 77 69  the savepoint wi
18a18 74 68 0a 2a 2a 20 69 6e 64 65 78 20 69 53 61 76  th.** index iSav
18a19 65 70 6f 69 6e 74 2e 20 49 66 20 69 74 20 69 73  epoint. If it is
18a1a 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42   SAVEPOINT_ROLLB
18a1b 41 43 4b 2c 20 74 68 65 6e 20 72 6f 6c 6c 62 61  ACK, then rollba
18a1c 63 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 0a 2a  ck all changes.*
18a1d 2a 20 74 68 61 74 20 68 61 76 65 20 6f 63 63 75  * that have occu
18a1e 72 72 65 64 20 73 69 6e 63 65 20 74 68 65 20 73  rred since the s
18a1f 70 65 63 69 66 69 65 64 20 73 61 76 65 70 6f 69  pecified savepoi
18a20 6e 74 20 77 61 73 20 63 72 65 61 74 65 64 2e 0a  nt was created..
18a21 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 76 65 70 6f  **.** The savepo
18a22 69 6e 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20  int to rollback 
18a23 6f 72 20 72 65 6c 65 61 73 65 20 69 73 20 69 64  or release is id
18a24 65 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61  entified by para
18a25 6d 65 74 65 72 20 0a 2a 2a 20 69 53 61 76 65 70  meter .** iSavep
18a26 6f 69 6e 74 2e 20 41 20 76 61 6c 75 65 20 6f 66  oint. A value of
18a27 20 30 20 6d 65 61 6e 73 20 74 6f 20 6f 70 65 72   0 means to oper
18a28 61 74 65 20 6f 6e 20 74 68 65 20 6f 75 74 65 72  ate on the outer
18a29 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 0a 2a  most savepoint.*
18a2a 2a 20 28 74 68 65 20 66 69 72 73 74 20 63 72 65  * (the first cre
18a2b 61 74 65 64 29 2e 20 41 20 76 61 6c 75 65 20 6f  ated). A value o
18a2c 66 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f  f (Pager.nSavepo
18a2d 69 6e 74 2d 31 29 20 6d 65 61 6e 73 20 6f 70 65  int-1) means ope
18a2e 72 61 74 65 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d  rate.** on the m
18a2f 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72 65  ost recently cre
18a30 61 74 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20  ated savepoint. 
18a31 49 66 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73  If iSavepoint is
18a32 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a   greater than.**
18a33 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69   (Pager.nSavepoi
18a34 6e 74 2d 31 29 2c 20 74 68 65 6e 20 74 68 69 73  nt-1), then this
18a35 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
18a36 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  o-op..**.** If a
18a37 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20   negative value 
18a38 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69  is passed to thi
18a39 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e  s function, then
18a3a 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20   the current.** 
18a3b 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72  transaction is r
18a3c 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73  olled back. This
18a3d 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 74 6f   is different to
18a3e 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c   calling .** sql
18a3f 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
18a40 6b 28 29 20 62 65 63 61 75 73 65 20 74 68 69 73  k() because this
18a41 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e   function does n
18a42 6f 74 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20  ot terminate.** 
18a43 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
18a44 6f 72 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61  or unlock the da
18a45 74 61 62 61 73 65 2c 20 69 74 20 6a 75 73 74 20  tabase, it just 
18a46 72 65 73 74 6f 72 65 73 20 74 68 65 20 0a 2a 2a  restores the .**
18a47 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
18a48 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73   database to its
18a49 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 2e   original state.
18a4a 20 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 79 20 63   .**.** In any c
18a4b 61 73 65 2c 20 61 6c 6c 20 73 61 76 65 70 6f 69  ase, all savepoi
18a4c 6e 74 73 20 77 69 74 68 20 61 6e 20 69 6e 64 65  nts with an inde
18a4d 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69  x greater than i
18a4e 53 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 61 72  Savepoint .** ar
18a4f 65 20 64 65 73 74 72 6f 79 65 64 2e 20 49 66 20  e destroyed. If 
18a50 74 68 69 73 20 69 73 20 61 20 72 65 6c 65 61 73  this is a releas
18a51 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6f 70 3d  e operation (op=
18a52 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41  =SAVEPOINT_RELEA
18a53 53 45 29 2c 0a 2a 2a 20 74 68 65 6e 20 73 61 76  SE),.** then sav
18a54 65 70 6f 69 6e 74 20 69 53 61 76 65 70 6f 69 6e  epoint iSavepoin
18a55 74 20 69 73 20 61 6c 73 6f 20 64 65 73 74 72 6f  t is also destro
18a56 79 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  yed..**.** This 
18a57 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74  function may ret
18a58 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
18a59 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   if a memory all
18a5a 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a  ocation fails,.*
18a5b 2a 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72  * or an IO error
18a5c 20 63 6f 64 65 20 69 66 20 61 6e 20 49 4f 20 65   code if an IO e
18a5d 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
18a5e 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61  e rolling back a
18a5f 20 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e 20   .** savepoint. 
18a60 49 66 20 6e 6f 20 65 72 72 6f 72 73 20 6f 63 63  If no errors occ
18a61 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  ur, SQLITE_OK is
18a62 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 53   returned..*/ .S
18a63 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
18a64 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61  t sqlite3PagerSa
18a65 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
18a66 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69  Pager, int op, i
18a67 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a  nt iSavepoint){.
18a68 20 20 69 6e 74 20 72 63 20 3d 20 70 50 61 67 65    int rc = pPage
18a69 72 2d 3e 65 72 72 43 6f 64 65 3b 20 20 20 20 20  r->errCode;     
18a6a 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
18a6b 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 6f   */..  assert( o
18a6c 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c  p==SAVEPOINT_REL
18a6d 45 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45  EASE || op==SAVE
18a6e 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29  POINT_ROLLBACK )
18a6f 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 61 76  ;.  assert( iSav
18a70 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 6f 70 3d  epoint>=0 || op=
18a71 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42  =SAVEPOINT_ROLLB
18a72 41 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 72 63  ACK );..  if( rc
18a73 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
18a74 53 61 76 65 70 6f 69 6e 74 3c 70 50 61 67 65 72  Savepoint<pPager
18a75 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a  ->nSavepoint ){.
18a76 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20      int ii;     
18a77 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
18a78 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20  or variable */. 
18a79 20 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20 20     int nNew;    
18a7a 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
18a7b 6f 66 20 72 65 6d 61 69 6e 69 6e 67 20 73 61 76  of remaining sav
18a7c 65 70 6f 69 6e 74 73 20 61 66 74 65 72 20 74 68  epoints after th
18a7d 69 73 20 6f 70 2e 20 2a 2f 0a 0a 20 20 20 20 2f  is op. */..    /
18a7e 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77  * Figure out how
18a7f 20 6d 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73   many savepoints
18a80 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65 20 61   will still be a
18a81 63 74 69 76 65 20 61 66 74 65 72 20 74 68 69 73  ctive after this
18a82 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f  .    ** operatio
18a83 6e 2e 20 53 74 6f 72 65 20 74 68 69 73 20 76 61  n. Store this va
18a84 6c 75 65 20 69 6e 20 6e 4e 65 77 2e 20 54 68 65  lue in nNew. The
18a85 6e 20 66 72 65 65 20 72 65 73 6f 75 72 63 65 73  n free resources
18a86 20 61 73 73 6f 63 69 61 74 65 64 20 0a 20 20 20   associated .   
18a87 20 2a 2a 20 77 69 74 68 20 61 6e 79 20 73 61 76   ** with any sav
18a88 65 70 6f 69 6e 74 73 20 74 68 61 74 20 61 72 65  epoints that are
18a89 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 74 68   destroyed by th
18a8a 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20  is operation..  
18a8b 20 20 2a 2f 0a 20 20 20 20 6e 4e 65 77 20 3d 20    */.    nNew = 
18a8c 69 53 61 76 65 70 6f 69 6e 74 20 2b 20 28 28 20  iSavepoint + (( 
18a8d 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45  op==SAVEPOINT_RE
18a8e 4c 45 41 53 45 20 29 20 3f 20 30 20 3a 20 31 29  LEASE ) ? 0 : 1)
18a8f 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e 4e 65  ;.    for(ii=nNe
18a90 77 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53  w; ii<pPager->nS
18a91 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b  avepoint; ii++){
18a92 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69  .      sqlite3Bi
18a93 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67  tvecDestroy(pPag
18a94 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69  er->aSavepoint[i
18a95 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29  i].pInSavepoint)
18a96 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67  ;.    }.    pPag
18a97 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d  er->nSavepoint =
18a98 20 6e 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a 20 49   nNew;..    /* I
18a99 66 20 74 68 69 73 20 69 73 20 61 20 72 65 6c 65  f this is a rele
18a9a 61 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72  ase of the outer
18a9b 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 2c 20  most savepoint, 
18a9c 74 72 75 6e 63 61 74 65 20 0a 20 20 20 20 2a 2a  truncate .    **
18a9d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
18a9e 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69   to zero bytes i
18a9f 6e 20 73 69 7a 65 2e 20 2a 2f 0a 20 20 20 20 69  n size. */.    i
18aa0 66 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54  f( op==SAVEPOINT
18aa1 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20  _RELEASE ){.    
18aa2 20 20 69 66 28 20 6e 4e 65 77 3d 3d 30 20 26 26    if( nNew==0 &&
18aa3 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
18aa4 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 20  sjfd) ){.       
18aa5 20 2f 2a 20 4f 6e 6c 79 20 74 72 75 6e 63 61 74   /* Only truncat
18aa6 65 20 69 66 20 69 74 20 69 73 20 61 6e 20 69 6e  e if it is an in
18aa7 2d 6d 65 6d 6f 72 79 20 73 75 62 2d 6a 6f 75 72  -memory sub-jour
18aa8 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  nal. */.        
18aa9 69 66 28 20 73 71 6c 69 74 65 33 49 73 4d 65 6d  if( sqlite3IsMem
18aaa 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e  Journal(pPager->
18aab 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 20  sjfd) ){.       
18aac 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
18aad 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72  sTruncate(pPager
18aae 2d 3e 73 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20  ->sjfd, 0);.    
18aaf 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
18ab0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
18ab1 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
18ab2 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63   pPager->nSubRec
18ab3 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
18ab4 20 20 7d 0a 20 20 20 20 2f 2a 20 45 6c 73 65 20    }.    /* Else 
18ab5 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 61  this is a rollba
18ab6 63 6b 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70 6c  ck operation, pl
18ab7 61 79 62 61 63 6b 20 74 68 65 20 73 70 65 63 69  ayback the speci
18ab8 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 0a  fied savepoint..
18ab9 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69      ** If this i
18aba 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 2c 20 69  s a temp-file, i
18abb 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68  t is possible th
18abc 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  at the journal f
18abd 69 6c 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e  ile has.    ** n
18abe 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e  ot yet been open
18abf 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
18ac0 20 74 68 65 72 65 20 68 61 76 65 20 62 65 65 6e   there have been
18ac1 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 0a 20   no changes to. 
18ac2 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61     ** the databa
18ac3 73 65 20 66 69 6c 65 2c 20 73 6f 20 74 68 65 20  se file, so the 
18ac4 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69  playback operati
18ac5 6f 6e 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65  on can be skippe
18ac6 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 6c  d..    */.    el
18ac7 73 65 20 69 66 28 20 70 61 67 65 72 55 73 65 57  se if( pagerUseW
18ac8 61 6c 28 70 50 61 67 65 72 29 20 7c 7c 20 69 73  al(pPager) || is
18ac9 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
18aca 29 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72  ) ){.      Pager
18acb 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65  Savepoint *pSave
18acc 70 6f 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30  point = (nNew==0
18acd 29 3f 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61  )?0:&pPager->aSa
18ace 76 65 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b  vepoint[nNew-1];
18acf 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
18ad0 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69  rPlaybackSavepoi
18ad1 6e 74 28 70 50 61 67 65 72 2c 20 70 53 61 76 65  nt(pPager, pSave
18ad2 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 61 73  point);.      as
18ad3 73 65 72 74 28 72 63 21 3d 53 51 4c 49 54 45 5f  sert(rc!=SQLITE_
18ad4 44 4f 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  DONE);.    }.  }
18ad5 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
18ad6 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
18ad7 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65  he full pathname
18ad8 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
18ad9 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 63   file..**.** Exc
18ada 65 70 74 2c 20 69 66 20 74 68 65 20 70 61 67 65  ept, if the page
18adb 72 20 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 6f  r is in-memory o
18adc 6e 6c 79 2c 20 74 68 65 6e 20 72 65 74 75 72 6e  nly, then return
18add 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67   an empty string
18ade 20 69 66 0a 2a 2a 20 6e 75 6c 6c 49 66 4d 65 6d   if.** nullIfMem
18adf 44 62 20 69 73 20 74 72 75 65 2e 20 20 54 68 69  Db is true.  Thi
18ae0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
18ae1 6c 65 64 20 77 69 74 68 20 6e 75 6c 6c 49 66 4d  led with nullIfM
18ae2 65 6d 44 62 3d 3d 31 20 77 68 65 6e 0a 2a 2a 20  emDb==1 when.** 
18ae3 75 73 65 64 20 74 6f 20 72 65 70 6f 72 74 20 74  used to report t
18ae4 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 74  he filename to t
18ae5 68 65 20 75 73 65 72 2c 20 66 6f 72 20 63 6f 6d  he user, for com
18ae6 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20  patibility with 
18ae7 6c 65 67 61 63 79 0a 2a 2a 20 62 65 68 61 76 69  legacy.** behavi
18ae8 6f 72 2e 20 20 42 75 74 20 77 68 65 6e 20 74 68  or.  But when th
18ae9 65 20 42 74 72 65 65 20 6e 65 65 64 73 20 74 6f  e Btree needs to
18aea 20 6b 6e 6f 77 20 74 68 65 20 66 69 6c 65 6e 61   know the filena
18aeb 6d 65 20 66 6f 72 20 6d 61 74 63 68 69 6e 67 20  me for matching 
18aec 74 6f 0a 2a 2a 20 73 68 61 72 65 64 20 63 61 63  to.** shared cac
18aed 68 65 2c 20 69 74 20 75 73 65 73 20 6e 75 6c 6c  he, it uses null
18aee 49 66 4d 65 6d 44 62 3d 3d 30 20 73 6f 20 74 68  IfMemDb==0 so th
18aef 61 74 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  at in-memory dat
18af0 61 62 61 73 65 73 20 63 61 6e 0a 2a 2a 20 70 61  abases can.** pa
18af1 72 74 69 63 69 70 61 74 65 20 69 6e 20 73 68 61  rticipate in sha
18af2 72 65 64 2d 63 61 63 68 65 2e 0a 2a 2f 0a 53 51  red-cache..*/.SQ
18af3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
18af4 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
18af5 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50 61  PagerFilename(Pa
18af6 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
18af7 20 6e 75 6c 6c 49 66 4d 65 6d 44 62 29 7b 0a 20   nullIfMemDb){. 
18af8 20 72 65 74 75 72 6e 20 28 6e 75 6c 6c 49 66 4d   return (nullIfM
18af9 65 6d 44 62 20 26 26 20 70 50 61 67 65 72 2d 3e  emDb && pPager->
18afa 6d 65 6d 44 62 29 20 3f 20 22 22 20 3a 20 70 50  memDb) ? "" : pP
18afb 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b  ager->zFilename;
18afc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
18afd 20 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75   the VFS structu
18afe 72 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 72  re for the pager
18aff 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18b00 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ATE const sqlite
18b01 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61  3_vfs *sqlite3Pa
18b02 67 65 72 56 66 73 28 50 61 67 65 72 20 2a 70 50  gerVfs(Pager *pP
18b03 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
18b04 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a  pPager->pVfs;.}.
18b05 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
18b06 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f  e file handle fo
18b07 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  r the database f
18b08 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a  ile associated.*
18b09 2a 20 77 69 74 68 20 74 68 65 20 70 61 67 65 72  * with the pager
18b0a 2e 20 20 54 68 69 73 20 6d 69 67 68 74 20 72 65  .  This might re
18b0b 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65  turn NULL if the
18b0c 20 66 69 6c 65 20 68 61 73 0a 2a 2a 20 6e 6f 74   file has.** not
18b0d 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64   yet been opened
18b0e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18b0f 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ATE sqlite3_file
18b10 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69   *sqlite3PagerFi
18b11 6c 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  le(Pager *pPager
18b12 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  ){.  return pPag
18b13 65 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  er->fd;.}../*.**
18b14 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c   Return the full
18b15 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65   pathname of the
18b16 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a   journal file..*
18b17 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18b18 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
18b19 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c  ite3PagerJournal
18b1a 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67  name(Pager *pPag
18b1b 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  er){.  return pP
18b1c 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a  ager->zJournal;.
18b1d 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
18b1e 74 72 75 65 20 69 66 20 66 73 79 6e 63 28 29 20  true if fsync() 
18b1f 63 61 6c 6c 73 20 61 72 65 20 64 69 73 61 62 6c  calls are disabl
18b20 65 64 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  ed for this page
18b21 72 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45  r.  Return FALSE
18b22 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28 29 73 20  .** if fsync()s 
18b23 61 72 65 20 65 78 65 63 75 74 65 64 20 6e 6f 72  are executed nor
18b24 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  mally..*/.SQLITE
18b25 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
18b26 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28  ite3PagerNosync(
18b27 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
18b28 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
18b29 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69 66 64  >noSync;.}..#ifd
18b2a 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f  ef SQLITE_HAS_CO
18b2b 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72  DEC./*.** Set or
18b2c 20 72 65 74 72 69 65 76 65 20 74 68 65 20 63 6f   retrieve the co
18b2d 64 65 63 20 66 6f 72 20 74 68 69 73 20 70 61 67  dec for this pag
18b2e 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  er.*/.SQLITE_PRI
18b2f 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
18b30 33 50 61 67 65 72 53 65 74 43 6f 64 65 63 28 0a  3PagerSetCodec(.
18b31 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c    Pager *pPager,
18b32 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43 6f 64 65  .  void *(*xCode
18b33 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50  c)(void*,void*,P
18b34 67 6e 6f 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 64  gno,int),.  void
18b35 20 28 2a 78 43 6f 64 65 63 53 69 7a 65 43 68 6e   (*xCodecSizeChn
18b36 67 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74  g)(void*,int,int
18b37 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64  ),.  void (*xCod
18b38 65 63 46 72 65 65 29 28 76 6f 69 64 2a 29 2c 0a  ecFree)(void*),.
18b39 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 0a 29    void *pCodec.)
18b3a 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  {.  if( pPager->
18b3b 78 43 6f 64 65 63 46 72 65 65 20 29 20 70 50 61  xCodecFree ) pPa
18b3c 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 28  ger->xCodecFree(
18b3d 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 29 3b  pPager->pCodec);
18b3e 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65  .  pPager->xCode
18b3f 63 20 3d 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44  c = pPager->memD
18b40 62 20 3f 20 30 20 3a 20 78 43 6f 64 65 63 3b 0a  b ? 0 : xCodec;.
18b41 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63    pPager->xCodec
18b42 53 69 7a 65 43 68 6e 67 20 3d 20 78 43 6f 64 65  SizeChng = xCode
18b43 63 53 69 7a 65 43 68 6e 67 3b 0a 20 20 70 50 61  cSizeChng;.  pPa
18b44 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20  ger->xCodecFree 
18b45 3d 20 78 43 6f 64 65 63 46 72 65 65 3b 0a 20 20  = xCodecFree;.  
18b46 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 20 3d  pPager->pCodec =
18b47 20 70 43 6f 64 65 63 3b 0a 20 20 70 61 67 65 72   pCodec;.  pager
18b48 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67 65  ReportSize(pPage
18b49 72 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  r);.}.SQLITE_PRI
18b4a 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
18b4b 65 33 50 61 67 65 72 47 65 74 43 6f 64 65 63 28  e3PagerGetCodec(
18b4c 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
18b4d 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
18b4e 3e 70 43 6f 64 65 63 3b 0a 7d 0a 23 65 6e 64 69  >pCodec;.}.#endi
18b4f 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
18b50 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
18b51 4d 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65  M./*.** Move the
18b52 20 70 61 67 65 20 70 50 67 20 74 6f 20 6c 6f 63   page pPg to loc
18b53 61 74 69 6f 6e 20 70 67 6e 6f 20 69 6e 20 74 68  ation pgno in th
18b54 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e file..**.** Th
18b55 65 72 65 20 6d 75 73 74 20 62 65 20 6e 6f 20 72  ere must be no r
18b56 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65  eferences to the
18b57 20 70 61 67 65 20 70 72 65 76 69 6f 75 73 6c 79   page previously
18b58 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 70   located at.** p
18b59 67 6e 6f 20 28 77 68 69 63 68 20 77 65 20 63 61  gno (which we ca
18b5a 6c 6c 20 70 50 67 4f 6c 64 29 20 74 68 6f 75 67  ll pPgOld) thoug
18b5b 68 20 74 68 61 74 20 70 61 67 65 20 69 73 20 61  h that page is a
18b5c 6c 6c 6f 77 65 64 20 74 6f 20 62 65 0a 2a 2a 20  llowed to be.** 
18b5d 69 6e 20 63 61 63 68 65 2e 20 20 49 66 20 74 68  in cache.  If th
18b5e 65 20 70 61 67 65 20 70 72 65 76 69 6f 75 73 6c  e page previousl
18b5f 79 20 6c 6f 63 61 74 65 64 20 61 74 20 70 67 6e  y located at pgn
18b60 6f 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  o is not already
18b61 0a 2a 2a 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62  .** in the rollb
18b62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20  ack journal, it 
18b63 69 73 20 6e 6f 74 20 70 75 74 20 74 68 65 72 65  is not put there
18b64 20 62 79 20 62 79 20 74 68 69 73 20 72 6f 75 74   by by this rout
18b65 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72  ine..**.** Refer
18b66 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67  ences to the pag
18b67 65 20 70 50 67 20 72 65 6d 61 69 6e 20 76 61 6c  e pPg remain val
18b68 69 64 2e 20 55 70 64 61 74 69 6e 67 20 61 6e 79  id. Updating any
18b69 0a 2a 2a 20 6d 65 74 61 2d 64 61 74 61 20 61 73  .** meta-data as
18b6a 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 50  sociated with pP
18b6b 67 20 28 69 2e 65 2e 20 64 61 74 61 20 73 74 6f  g (i.e. data sto
18b6c 72 65 64 20 69 6e 20 74 68 65 20 6e 45 78 74 72  red in the nExtr
18b6d 61 20 62 79 74 65 73 0a 2a 2a 20 61 6c 6c 6f 63  a bytes.** alloc
18b6e 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20  ated along with 
18b6f 74 68 65 20 70 61 67 65 29 20 69 73 20 74 68 65  the page) is the
18b70 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20   responsibility 
18b71 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a  of the caller..*
18b72 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69  *.** A transacti
18b73 6f 6e 20 6d 75 73 74 20 62 65 20 61 63 74 69 76  on must be activ
18b74 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  e when this rout
18b75 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 49  ine is called. I
18b76 74 20 75 73 65 64 20 74 6f 20 62 65 0a 2a 2a 20  t used to be.** 
18b77 72 65 71 75 69 72 65 64 20 74 68 61 74 20 61 20  required that a 
18b78 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
18b79 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 61 63  ction was not ac
18b7a 74 69 76 65 2c 20 62 75 74 20 74 68 69 73 20 72  tive, but this r
18b7b 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a 20 68 61  estriction.** ha
18b7c 73 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 28  s been removed (
18b7d 43 52 45 41 54 45 20 49 4e 44 45 58 20 6e 65 65  CREATE INDEX nee
18b7e 64 73 20 74 6f 20 6d 6f 76 65 20 61 20 70 61 67  ds to move a pag
18b7f 65 20 77 68 65 6e 20 61 20 73 74 61 74 65 6d 65  e when a stateme
18b80 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  nt.** transactio
18b81 6e 20 69 73 20 61 63 74 69 76 65 29 2e 0a 2a 2a  n is active)..**
18b82 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 75 72 74  .** If the fourt
18b83 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 73 43 6f  h argument, isCo
18b84 6d 6d 69 74 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72  mmit, is non-zer
18b85 6f 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61 67  o, then this pag
18b86 65 20 69 73 20 62 65 69 6e 67 0a 2a 2a 20 6d 6f  e is being.** mo
18b87 76 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61  ved as part of a
18b88 20 64 61 74 61 62 61 73 65 20 72 65 6f 72 67 61   database reorga
18b89 6e 69 7a 61 74 69 6f 6e 20 6a 75 73 74 20 62 65  nization just be
18b8a 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63  fore the transac
18b8b 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 62 65 69 6e  tion .** is bein
18b8c 67 20 63 6f 6d 6d 69 74 74 65 64 2e 20 49 6e 20  g committed. In 
18b8d 74 68 69 73 20 63 61 73 65 2c 20 69 74 20 69 73  this case, it is
18b8e 20 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 74   guaranteed that
18b8f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61   the database pa
18b90 67 65 20 0a 2a 2a 20 70 50 67 20 72 65 66 65 72  ge .** pPg refer
18b91 73 20 74 6f 20 77 69 6c 6c 20 6e 6f 74 20 62 65  s to will not be
18b92 20 77 72 69 74 74 65 6e 20 74 6f 20 61 67 61 69   written to agai
18b93 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 72  n within this tr
18b94 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ansaction..**.**
18b95 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
18b96 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ay return SQLITE
18b97 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f 20  _NOMEM or an IO 
18b98 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e  error code if an
18b99 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73   error.** occurs
18b9a 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20  . Otherwise, it 
18b9b 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
18b9c 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  K..*/.SQLITE_PRI
18b9d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
18b9e 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 50 61  PagerMovepage(Pa
18b9f 67 65 72 20 2a 70 50 61 67 65 72 2c 20 44 62 50  ger *pPager, DbP
18ba0 61 67 65 20 2a 70 50 67 2c 20 50 67 6e 6f 20 70  age *pPg, Pgno p
18ba1 67 6e 6f 2c 20 69 6e 74 20 69 73 43 6f 6d 6d 69  gno, int isCommi
18ba2 74 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67  t){.  PgHdr *pPg
18ba3 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  Old;            
18ba4 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 62     /* The page b
18ba5 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e  eing overwritten
18ba6 2e 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 65 65 64  . */.  Pgno need
18ba7 53 79 6e 63 50 67 6e 6f 20 3d 20 30 3b 20 20 20  SyncPgno = 0;   
18ba8 20 20 20 20 2f 2a 20 4f 6c 64 20 76 61 6c 75 65      /* Old value
18ba9 20 6f 66 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 69   of pPg->pgno, i
18baa 66 20 73 79 6e 63 20 69 73 20 72 65 71 75 69 72  f sync is requir
18bab 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  ed */.  int rc; 
18bac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18bad 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
18bae 6f 64 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6f 72  ode */.  Pgno or
18baf 69 67 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 20  igPgno;         
18bb0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69        /* The ori
18bb1 67 69 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 65  ginal page numbe
18bb2 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  r */..  assert( 
18bb3 70 50 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  pPg->nRef>0 );. 
18bb4 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
18bb5 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57  >eState==PAGER_W
18bb6 52 49 54 45 52 5f 43 41 43 48 45 4d 4f 44 0a 20  RITER_CACHEMOD. 
18bb7 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d        || pPager-
18bb8 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f 57  >eState==PAGER_W
18bb9 52 49 54 45 52 5f 44 42 4d 4f 44 0a 20 20 29 3b  RITER_DBMOD.  );
18bba 0a 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72  .  assert( asser
18bbb 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50  t_pager_state(pP
18bbc 61 67 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  ager) );..  /* I
18bbd 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20 61 62  n order to be ab
18bbe 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20  le to rollback, 
18bbf 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  an in-memory dat
18bc0 61 62 61 73 65 20 6d 75 73 74 20 6a 6f 75 72 6e  abase must journ
18bc1 61 6c 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65  al.  ** the page
18bc2 20 77 65 20 61 72 65 20 6d 6f 76 69 6e 67 20 66   we are moving f
18bc3 72 6f 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  rom..  */.  if( 
18bc4 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 72 63 20  MEMDB ){.    rc 
18bc5 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
18bc6 69 74 65 28 70 50 67 29 3b 0a 20 20 20 20 69 66  ite(pPg);.    if
18bc7 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
18bc8 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
18bc9 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6d 6f  he page being mo
18bca 76 65 64 20 69 73 20 64 69 72 74 79 20 61 6e 64  ved is dirty and
18bcb 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 61   has not been sa
18bcc 76 65 64 20 62 79 20 74 68 65 20 6c 61 74 65 73  ved by the lates
18bcd 74 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74  t.  ** savepoint
18bce 2c 20 74 68 65 6e 20 73 61 76 65 20 74 68 65 20  , then save the 
18bcf 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73  current contents
18bd0 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e 74   of the page int
18bd1 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 2d  o the .  ** sub-
18bd2 6a 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54 68 69  journal now. Thi
18bd3 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f  s is required to
18bd4 20 68 61 6e 64 6c 65 20 74 68 65 20 66 6f 6c 6c   handle the foll
18bd5 6f 77 69 6e 67 20 73 63 65 6e 61 72 69 6f 3a 0a  owing scenario:.
18bd6 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49    **.  **   BEGI
18bd7 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75  N;.  **     <jou
18bd8 72 6e 61 6c 20 70 61 67 65 20 58 2c 20 74 68 65  rnal page X, the
18bd9 6e 20 6d 6f 64 69 66 79 20 69 74 20 69 6e 20 6d  n modify it in m
18bda 65 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20 20 20  emory>.  **     
18bdb 53 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20  SAVEPOINT one;. 
18bdc 20 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 65 20   **       <Move 
18bdd 70 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 74 69  page X to locati
18bde 6f 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20 20 52  on Y>.  **     R
18bdf 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 3b 0a  OLLBACK TO one;.
18be0 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 61 67    **.  ** If pag
18be1 65 20 58 20 77 65 72 65 20 6e 6f 74 20 77 72 69  e X were not wri
18be2 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 2d  tten to the sub-
18be3 6a 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20 69 74  journal here, it
18be4 20 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a 2a 20   would not.  ** 
18be5 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72  be possible to r
18be6 65 73 74 6f 72 65 20 69 74 73 20 63 6f 6e 74 65  estore its conte
18be7 6e 74 73 20 77 68 65 6e 20 74 68 65 20 22 52 4f  nts when the "RO
18be8 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22 0a 20  LLBACK TO one". 
18be9 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65   ** statement we
18bea 72 65 20 69 73 20 70 72 6f 63 65 73 73 65 64 2e  re is processed.
18beb 0a 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62 6a 6f  .  **.  ** subjo
18bec 75 72 6e 61 6c 50 61 67 65 28 29 20 6d 61 79 20  urnalPage() may 
18bed 6e 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  need to allocate
18bee 20 73 70 61 63 65 20 74 6f 20 73 74 6f 72 65 20   space to store 
18bef 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f 0a 20  pPg->pgno into. 
18bf0 20 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   ** one or more 
18bf1 73 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63  savepoint bitvec
18bf2 73 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 72  s. This is the r
18bf3 65 61 73 6f 6e 20 74 68 69 73 20 66 75 6e 63 74  eason this funct
18bf4 69 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72 65 74  ion.  ** may ret
18bf5 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
18bf6 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67  ..  */.  if( pPg
18bf7 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49  ->flags&PGHDR_DI
18bf8 52 54 59 0a 20 20 20 26 26 20 73 75 62 6a 52 65  RTY.   && subjRe
18bf9 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 0a  quiresPage(pPg).
18bfa 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21     && SQLITE_OK!
18bfb 3d 28 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61  =(rc = subjourna
18bfc 6c 50 61 67 65 28 70 50 67 29 29 0a 20 20 29 7b  lPage(pPg)).  ){
18bfd 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
18bfe 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43    }..  PAGERTRAC
18bff 45 28 28 22 4d 4f 56 45 20 25 64 20 70 61 67 65  E(("MOVE %d page
18c00 20 25 64 20 28 6e 65 65 64 53 79 6e 63 3d 25 64   %d (needSync=%d
18c01 29 20 6d 6f 76 65 73 20 74 6f 20 25 64 5c 6e 22  ) moves to %d\n"
18c02 2c 20 0a 20 20 20 20 20 20 50 41 47 45 52 49 44  , .      PAGERID
18c03 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70  (pPager), pPg->p
18c04 67 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c 61 67 73  gno, (pPg->flags
18c05 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
18c06 29 3f 31 3a 30 2c 20 70 67 6e 6f 29 29 3b 0a 20  )?1:0, pgno));. 
18c07 20 49 4f 54 52 41 43 45 28 28 22 4d 4f 56 45 20   IOTRACE(("MOVE 
18c08 25 70 20 25 64 20 25 64 5c 6e 22 2c 20 70 50 61  %p %d %d\n", pPa
18c09 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  ger, pPg->pgno, 
18c0a 70 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20 49 66 20  pgno))..  /* If 
18c0b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64  the journal need
18c0c 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64  s to be sync()ed
18c0d 20 62 65 66 6f 72 65 20 70 61 67 65 20 70 50 67   before page pPg
18c0e 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a 2a 20  ->pgno can.  ** 
18c0f 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 73  be written to, s
18c10 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f 20 69  tore pPg->pgno i
18c11 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  n local variable
18c12 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e 0a 20   needSyncPgno.. 
18c13 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20   **.  ** If the 
18c14 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73  isCommit flag is
18c15 20 73 65 74 2c 20 74 68 65 72 65 20 69 73 20 6e   set, there is n
18c16 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62  o need to rememb
18c17 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65  er that.  ** the
18c18 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74   journal needs t
18c19 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 62 65  o be sync()ed be
18c1a 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 61  fore database pa
18c1b 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a 20 20  ge pPg->pgno .  
18c1c 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74 65  ** can be writte
18c1d 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72  n to. The caller
18c1e 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f   has already pro
18c1f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69  mised not to wri
18c20 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f 0a 20  te to it..  */. 
18c21 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67 73   if( (pPg->flags
18c22 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
18c23 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74 20 29  ) && !isCommit )
18c24 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63 50 67  {.    needSyncPg
18c25 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a  no = pPg->pgno;.
18c26 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65      assert( page
18c27 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 7c  InJournal(pPg) |
18c28 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67  | pPg->pgno>pPag
18c29 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29  er->dbOrigSize )
18c2a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
18c2b 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  g->flags&PGHDR_D
18c2c 49 52 54 59 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f  IRTY );.  }..  /
18c2d 2a 20 49 66 20 74 68 65 20 63 61 63 68 65 20 63  * If the cache c
18c2e 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65 20 77  ontains a page w
18c2f 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 72 20  ith page-number 
18c30 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 74 0a  pgno, remove it.
18c31 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 68 61    ** from its ha
18c32 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f 2c 20  sh chain. Also, 
18c33 69 66 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45  if the PGHDR_NEE
18c34 44 5f 53 59 4e 43 20 66 6c 61 67 20 77 61 73 20  D_SYNC flag was 
18c35 73 65 74 20 66 6f 72 20 0a 20 20 2a 2a 20 70 61  set for .  ** pa
18c36 67 65 20 70 67 6e 6f 20 62 65 66 6f 72 65 20 74  ge pgno before t
18c37 68 65 20 27 6d 6f 76 65 27 20 6f 70 65 72 61 74  he 'move' operat
18c38 69 6f 6e 2c 20 69 74 20 6e 65 65 64 73 20 74 6f  ion, it needs to
18c39 20 62 65 20 72 65 74 61 69 6e 65 64 20 0a 20 20   be retained .  
18c3a 2a 2a 20 66 6f 72 20 74 68 65 20 70 61 67 65 20  ** for the page 
18c3b 6d 6f 76 65 64 20 74 68 65 72 65 2e 0a 20 20 2a  moved there..  *
18c3c 2f 0a 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 26  /.  pPg->flags &
18c3d 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  = ~PGHDR_NEED_SY
18c3e 4e 43 3b 0a 20 20 70 50 67 4f 6c 64 20 3d 20 70  NC;.  pPgOld = p
18c3f 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67  ager_lookup(pPag
18c40 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 61 73 73  er, pgno);.  ass
18c41 65 72 74 28 20 21 70 50 67 4f 6c 64 20 7c 7c 20  ert( !pPgOld || 
18c42 70 50 67 4f 6c 64 2d 3e 6e 52 65 66 3d 3d 31 20  pPgOld->nRef==1 
18c43 29 3b 0a 20 20 69 66 28 20 70 50 67 4f 6c 64 20  );.  if( pPgOld 
18c44 29 7b 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67  ){.    pPg->flag
18c45 73 20 7c 3d 20 28 70 50 67 4f 6c 64 2d 3e 66 6c  s |= (pPgOld->fl
18c46 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
18c47 59 4e 43 29 3b 0a 20 20 20 20 69 66 28 20 4d 45  YNC);.    if( ME
18c48 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  MDB ){.      /* 
18c49 44 6f 20 6e 6f 74 20 64 69 73 63 61 72 64 20 70  Do not discard p
18c4a 61 67 65 73 20 66 72 6f 6d 20 61 6e 20 69 6e 2d  ages from an in-
18c4b 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20  memory database 
18c4c 73 69 6e 63 65 20 77 65 20 6d 69 67 68 74 0a 20  since we might. 
18c4d 20 20 20 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20       ** need to 
18c4e 72 6f 6c 6c 62 61 63 6b 20 6c 61 74 65 72 2e 20  rollback later. 
18c4f 20 4a 75 73 74 20 6d 6f 76 65 20 74 68 65 20 70   Just move the p
18c50 61 67 65 20 6f 75 74 20 6f 66 20 74 68 65 20 77  age out of the w
18c51 61 79 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  ay. */.      sql
18c52 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70  ite3PcacheMove(p
18c53 50 67 4f 6c 64 2c 20 70 50 61 67 65 72 2d 3e 64  PgOld, pPager->d
18c54 62 53 69 7a 65 2b 31 29 3b 0a 20 20 20 20 7d 65  bSize+1);.    }e
18c55 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
18c56 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 50 67  e3PcacheDrop(pPg
18c57 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Old);.    }.  }.
18c58 0a 20 20 6f 72 69 67 50 67 6e 6f 20 3d 20 70 50  .  origPgno = pP
18c59 67 2d 3e 70 67 6e 6f 3b 0a 20 20 73 71 6c 69 74  g->pgno;.  sqlit
18c5a 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67  e3PcacheMove(pPg
18c5b 2c 20 70 67 6e 6f 29 3b 0a 20 20 73 71 6c 69 74  , pgno);.  sqlit
18c5c 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74  e3PcacheMakeDirt
18c5d 79 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 46 6f  y(pPg);..  /* Fo
18c5e 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  r an in-memory d
18c5f 61 74 61 62 61 73 65 2c 20 6d 61 6b 65 20 73 75  atabase, make su
18c60 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  re the original 
18c61 70 61 67 65 20 63 6f 6e 74 69 6e 75 65 73 0a 20  page continues. 
18c62 20 2a 2a 20 74 6f 20 65 78 69 73 74 2c 20 69 6e   ** to exist, in
18c63 20 63 61 73 65 20 74 68 65 20 74 72 61 6e 73 61   case the transa
18c64 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 72  ction needs to r
18c65 6f 6c 6c 20 62 61 63 6b 2e 20 20 55 73 65 20 70  oll back.  Use p
18c66 50 67 4f 6c 64 0a 20 20 2a 2a 20 61 73 20 74 68  PgOld.  ** as th
18c67 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20  e original page 
18c68 73 69 6e 63 65 20 69 74 20 68 61 73 20 61 6c 72  since it has alr
18c69 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61  eady been alloca
18c6a 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ted..  */.  if( 
18c6b 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 61 73 73  MEMDB ){.    ass
18c6c 65 72 74 28 20 70 50 67 4f 6c 64 20 29 3b 0a 20  ert( pPgOld );. 
18c6d 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
18c6e 4d 6f 76 65 28 70 50 67 4f 6c 64 2c 20 6f 72 69  Move(pPgOld, ori
18c6f 67 50 67 6e 6f 29 3b 0a 20 20 20 20 73 71 6c 69  gPgno);.    sqli
18c70 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
18c71 67 4f 6c 64 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  gOld);.  }..  if
18c72 28 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 29  ( needSyncPgno )
18c73 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65 65 64  {.    /* If need
18c74 53 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f 6e 2d  SyncPgno is non-
18c75 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6a  zero, then the j
18c76 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 65 64  ournal file need
18c77 73 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20  s to be .    ** 
18c78 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20  sync()ed before 
18c79 61 6e 79 20 64 61 74 61 20 69 73 20 77 72 69 74  any data is writ
18c7a 74 65 6e 20 74 6f 20 64 61 74 61 62 61 73 65 20  ten to database 
18c7b 66 69 6c 65 20 70 61 67 65 20 6e 65 65 64 53 79  file page needSy
18c7c 6e 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20 43  ncPgno..    ** C
18c7d 75 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73 75 63  urrently, no suc
18c7e 68 20 70 61 67 65 20 65 78 69 73 74 73 20 69 6e  h page exists in
18c7f 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20   the page-cache 
18c80 61 6e 64 20 74 68 65 20 0a 20 20 20 20 2a 2a 20  and the .    ** 
18c81 22 69 73 20 6a 6f 75 72 6e 61 6c 65 64 22 20 62  "is journaled" b
18c82 69 74 76 65 63 20 66 6c 61 67 20 68 61 73 20 62  itvec flag has b
18c83 65 65 6e 20 73 65 74 2e 20 54 68 69 73 20 6e 65  een set. This ne
18c84 65 64 73 20 74 6f 20 62 65 20 72 65 6d 65 64 69  eds to be remedi
18c85 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 6c 6f 61  ed by.    ** loa
18c86 64 69 6e 67 20 74 68 65 20 70 61 67 65 20 69 6e  ding the page in
18c87 74 6f 20 74 68 65 20 70 61 67 65 72 2d 63 61 63  to the pager-cac
18c88 68 65 20 61 6e 64 20 73 65 74 74 69 6e 67 20 74  he and setting t
18c89 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  he PGHDR_NEED_SY
18c8a 4e 43 0a 20 20 20 20 2a 2a 20 66 6c 61 67 2e 0a  NC.    ** flag..
18c8b 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66      **.    ** If
18c8c 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20   the attempt to 
18c8d 6c 6f 61 64 20 74 68 65 20 70 61 67 65 20 69 6e  load the page in
18c8e 74 6f 20 74 68 65 20 70 61 67 65 2d 63 61 63 68  to the page-cach
18c8f 65 20 66 61 69 6c 73 2c 20 28 64 75 65 0a 20 20  e fails, (due.  
18c90 20 20 2a 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f 63    ** to a malloc
18c91 28 29 20 6f 72 20 49 4f 20 66 61 69 6c 75 72 65  () or IO failure
18c92 29 2c 20 63 6c 65 61 72 20 74 68 65 20 62 69 74  ), clear the bit
18c93 20 69 6e 20 74 68 65 20 70 49 6e 4a 6f 75 72 6e   in the pInJourn
18c94 61 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 61  al[].    ** arra
18c95 79 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  y. Otherwise, if
18c96 20 74 68 65 20 70 61 67 65 20 69 73 20 6c 6f 61   the page is loa
18c97 64 65 64 20 61 6e 64 20 77 72 69 74 74 65 6e 20  ded and written 
18c98 61 67 61 69 6e 20 69 6e 0a 20 20 20 20 2a 2a 20  again in.    ** 
18c99 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
18c9a 2c 20 69 74 20 6d 61 79 20 62 65 20 77 72 69 74  , it may be writ
18c9b 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62  ten to the datab
18c9c 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 0a  ase file before.
18c9d 20 20 20 20 2a 2a 20 69 74 20 69 73 20 73 79 6e      ** it is syn
18c9e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ced into the jou
18c9f 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20  rnal file. This 
18ca0 77 61 79 2c 20 69 74 20 6d 61 79 20 65 6e 64 20  way, it may end 
18ca1 75 70 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65  up in.    ** the
18ca2 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 77   journal file tw
18ca3 69 63 65 2c 20 62 75 74 20 74 68 61 74 20 69 73  ice, but that is
18ca4 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d 2e 0a   not a problem..
18ca5 20 20 20 20 2a 2f 0a 20 20 20 20 50 67 48 64 72      */.    PgHdr
18ca6 20 2a 70 50 67 48 64 72 3b 0a 20 20 20 20 72 63   *pPgHdr;.    rc
18ca7 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
18ca8 65 74 28 70 50 61 67 65 72 2c 20 6e 65 65 64 53  et(pPager, needS
18ca9 79 6e 63 50 67 6e 6f 2c 20 26 70 50 67 48 64 72  yncPgno, &pPgHdr
18caa 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
18cab 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
18cac 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 50 67    if( needSyncPg
18cad 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72  no<=pPager->dbOr
18cae 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  igSize ){.      
18caf 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
18cb0 2d 3e 70 54 6d 70 53 70 61 63 65 21 3d 30 20 29  ->pTmpSpace!=0 )
18cb1 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
18cb2 33 42 69 74 76 65 63 43 6c 65 61 72 28 70 50 61  3BitvecClear(pPa
18cb3 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c  ger->pInJournal,
18cb4 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c 20 70   needSyncPgno, p
18cb5 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
18cb6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
18cb7 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
18cb8 7d 0a 20 20 20 20 70 50 67 48 64 72 2d 3e 66 6c  }.    pPgHdr->fl
18cb9 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45  ags |= PGHDR_NEE
18cba 44 5f 53 59 4e 43 3b 0a 20 20 20 20 73 71 6c 69  D_SYNC;.    sqli
18cbb 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72  te3PcacheMakeDir
18cbc 74 79 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20  ty(pPgHdr);.    
18cbd 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
18cbe 66 28 70 50 67 48 64 72 29 3b 0a 20 20 7d 0a 0a  f(pPgHdr);.  }..
18cbf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
18cc0 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  OK;.}.#endif../*
18cc1 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
18cc2 6e 74 65 72 20 74 6f 20 74 68 65 20 64 61 74 61  nter to the data
18cc3 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69   for the specifi
18cc4 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49  ed page..*/.SQLI
18cc5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
18cc6 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  *sqlite3PagerGet
18cc7 44 61 74 61 28 44 62 50 61 67 65 20 2a 70 50 67  Data(DbPage *pPg
18cc8 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67  ){.  assert( pPg
18cc9 2d 3e 6e 52 65 66 3e 30 20 7c 7c 20 70 50 67 2d  ->nRef>0 || pPg-
18cca 3e 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29  >pPager->memDb )
18ccb 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e  ;.  return pPg->
18ccc 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pData;.}../*.** 
18ccd 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
18cce 20 74 6f 20 74 68 65 20 50 61 67 65 72 2e 6e 45   to the Pager.nE
18ccf 78 74 72 61 20 62 79 74 65 73 20 6f 66 20 22 65  xtra bytes of "e
18cd0 78 74 72 61 22 20 73 70 61 63 65 20 0a 2a 2a 20  xtra" space .** 
18cd1 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20  allocated along 
18cd2 77 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69  with the specifi
18cd3 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49  ed page..*/.SQLI
18cd4 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
18cd5 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  *sqlite3PagerGet
18cd6 45 78 74 72 61 28 44 62 50 61 67 65 20 2a 70 50  Extra(DbPage *pP
18cd7 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67  g){.  return pPg
18cd8 2d 3e 70 45 78 74 72 61 3b 0a 7d 0a 0a 2f 2a 0a  ->pExtra;.}../*.
18cd9 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 6c  ** Get/set the l
18cda 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20  ocking-mode for 
18cdb 74 68 69 73 20 70 61 67 65 72 2e 20 50 61 72 61  this pager. Para
18cdc 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d 75 73 74  meter eMode must
18cdd 20 62 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41   be one.** of PA
18cde 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
18cdf 51 55 45 52 59 2c 20 50 41 47 45 52 5f 4c 4f 43  QUERY, PAGER_LOC
18ce0 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20  KINGMODE_NORMAL 
18ce1 6f 72 20 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43  or .** PAGER_LOC
18ce2 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49  KINGMODE_EXCLUSI
18ce3 56 45 2e 20 49 66 20 74 68 65 20 70 61 72 61 6d  VE. If the param
18ce4 65 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 55 45  eter is not _QUE
18ce5 52 59 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  RY, then.** the 
18ce6 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 69 73 20  locking-mode is 
18ce7 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65  set to the value
18ce8 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a   specified..**.*
18ce9 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76  * The returned v
18cea 61 6c 75 65 20 69 73 20 65 69 74 68 65 72 20 50  alue is either P
18ceb 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
18cec 5f 4e 4f 52 4d 41 4c 20 6f 72 0a 2a 2a 20 50 41  _NORMAL or.** PA
18ced 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
18cee 45 58 43 4c 55 53 49 56 45 2c 20 69 6e 64 69 63  EXCLUSIVE, indic
18cef 61 74 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e  ating the curren
18cf0 74 20 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61  t (possibly upda
18cf1 74 65 64 29 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d  ted).** locking-
18cf2 6d 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  mode..*/.SQLITE_
18cf3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
18cf4 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d  te3PagerLockingM
18cf5 6f 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ode(Pager *pPage
18cf6 72 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20  r, int eMode){. 
18cf7 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d   assert( eMode==
18cf8 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
18cf9 45 5f 51 55 45 52 59 0a 20 20 20 20 20 20 20 20  E_QUERY.        
18cfa 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41      || eMode==PA
18cfb 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
18cfc 4e 4f 52 4d 41 4c 0a 20 20 20 20 20 20 20 20 20  NORMAL.         
18cfd 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47     || eMode==PAG
18cfe 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45  ER_LOCKINGMODE_E
18cff 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 73  XCLUSIVE );.  as
18d00 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b  sert( PAGER_LOCK
18d01 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 3c 30 20  INGMODE_QUERY<0 
18d02 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  );.  assert( PAG
18d03 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e  ER_LOCKINGMODE_N
18d04 4f 52 4d 41 4c 3e 3d 30 20 26 26 20 50 41 47 45  ORMAL>=0 && PAGE
18d05 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58  R_LOCKINGMODE_EX
18d06 43 4c 55 53 49 56 45 3e 3d 30 20 29 3b 0a 20 20  CLUSIVE>=0 );.  
18d07 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
18d08 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c  exclusiveMode ||
18d09 20 30 3d 3d 73 71 6c 69 74 65 33 57 61 6c 48 65   0==sqlite3WalHe
18d0a 61 70 4d 65 6d 6f 72 79 28 70 50 61 67 65 72 2d  apMemory(pPager-
18d0b 3e 70 57 61 6c 29 20 29 3b 0a 20 20 69 66 28 20  >pWal) );.  if( 
18d0c 65 4d 6f 64 65 3e 3d 30 20 26 26 20 21 70 50 61  eMode>=0 && !pPa
18d0d 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26  ger->tempFile &&
18d0e 20 21 73 71 6c 69 74 65 33 57 61 6c 48 65 61 70   !sqlite3WalHeap
18d0f 4d 65 6d 6f 72 79 28 70 50 61 67 65 72 2d 3e 70  Memory(pPager->p
18d10 57 61 6c 29 20 29 7b 0a 20 20 20 20 70 50 61 67  Wal) ){.    pPag
18d11 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  er->exclusiveMod
18d12 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20  e = (u8)eMode;. 
18d13 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74   }.  return (int
18d14 29 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69  )pPager->exclusi
18d15 76 65 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  veMode;.}../*.**
18d16 20 53 65 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c   Set the journal
18d17 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70  -mode for this p
18d18 61 67 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20  ager. Parameter 
18d19 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e  eMode must be on
18d1a 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 50  e of:.**.**    P
18d1b 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
18d1c 5f 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 50 41  _DELETE.**    PA
18d1d 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
18d1e 54 52 55 4e 43 41 54 45 0a 2a 2a 20 20 20 20 50  TRUNCATE.**    P
18d1f 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
18d20 5f 50 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 50  _PERSIST.**    P
18d21 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
18d22 5f 4f 46 46 0a 2a 2a 20 20 20 20 50 41 47 45 52  _OFF.**    PAGER
18d23 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d  _JOURNALMODE_MEM
18d24 4f 52 59 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f  ORY.**    PAGER_
18d25 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 0a  JOURNALMODE_WAL.
18d26 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61  **.** The journa
18d27 6c 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20  lmode is set to 
18d28 74 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66  the value specif
18d29 69 65 64 20 69 66 20 74 68 65 20 63 68 61 6e 67  ied if the chang
18d2a 65 20 69 73 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a  e is allowed..**
18d2b 20 54 68 65 20 63 68 61 6e 67 65 20 6d 61 79 20   The change may 
18d2c 62 65 20 64 69 73 61 6c 6c 6f 77 65 64 20 66 6f  be disallowed fo
18d2d 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  r the following 
18d2e 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20  reasons:.**.**  
18d2f 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79   *  An in-memory
18d30 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 6f 6e   database can on
18d31 6c 79 20 68 61 76 65 20 69 74 73 20 6a 6f 75 72  ly have its jour
18d32 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74 6f 20  nal_mode set to 
18d33 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f 72 20  _OFF.**      or 
18d34 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a 20 20  _MEMORY..**.**  
18d35 20 2a 20 20 54 65 6d 70 6f 72 61 72 79 20 64 61   *  Temporary da
18d36 74 61 62 61 73 65 73 20 63 61 6e 6e 6f 74 20 68  tabases cannot h
18d37 61 76 65 20 5f 57 41 4c 20 6a 6f 75 72 6e 61 6c  ave _WAL journal
18d38 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  mode..**.** The 
18d39 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 61 74  returned indicat
18d3a 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 28 70  e the current (p
18d3b 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64 29  ossibly updated)
18d3c 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e 0a 2a   journal-mode..*
18d3d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18d3e 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
18d3f 72 53 65 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28  rSetJournalMode(
18d40 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
18d41 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 75 38 20  nt eMode){.  u8 
18d42 65 4f 6c 64 20 3d 20 70 50 61 67 65 72 2d 3e 6a  eOld = pPager->j
18d43 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 20 2f  ournalMode;    /
18d44 2a 20 50 72 69 6f 72 20 6a 6f 75 72 6e 61 6c 6d  * Prior journalm
18d45 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53  ode */..#ifdef S
18d46 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a  QLITE_DEBUG.  /*
18d47 20 54 68 65 20 70 72 69 6e 74 5f 70 61 67 65 72   The print_pager
18d48 5f 73 74 61 74 65 28 29 20 72 6f 75 74 69 6e 65  _state() routine
18d49 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20   is intended to 
18d4a 62 65 20 75 73 65 64 20 62 79 20 74 68 65 20 64  be used by the d
18d4b 65 62 75 67 67 65 72 0a 20 20 2a 2a 20 6f 6e 6c  ebugger.  ** onl
18d4c 79 2e 20 20 57 65 20 69 6e 76 6f 6b 65 20 69 74  y.  We invoke it
18d4d 20 6f 6e 63 65 20 68 65 72 65 20 74 6f 20 73 75   once here to su
18d4e 70 70 72 65 73 73 20 61 20 63 6f 6d 70 69 6c 65  ppress a compile
18d4f 72 20 77 61 72 6e 69 6e 67 2e 20 2a 2f 0a 20 20  r warning. */.  
18d50 70 72 69 6e 74 5f 70 61 67 65 72 5f 73 74 61 74  print_pager_stat
18d51 65 28 70 50 61 67 65 72 29 3b 0a 23 65 6e 64 69  e(pPager);.#endi
18d52 66 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 65 4d 6f  f...  /* The eMo
18d53 64 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  de parameter is 
18d54 61 6c 77 61 79 73 20 76 61 6c 69 64 20 2a 2f 0a  always valid */.
18d55 20 20 61 73 73 65 72 74 28 20 20 20 20 20 20 65    assert(      e
18d56 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
18d57 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 20  NALMODE_DELETE. 
18d58 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
18d59 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
18d5a 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a  ALMODE_TRUNCATE.
18d5b 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
18d5c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
18d5d 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a  NALMODE_PERSIST.
18d5e 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
18d5f 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
18d60 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20  NALMODE_OFF .   
18d61 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64           || eMod
18d62 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
18d63 4d 4f 44 45 5f 57 41 4c 20 0a 20 20 20 20 20 20  MODE_WAL .      
18d64 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d        || eMode==
18d65 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
18d66 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 0a 20 20 2f  E_MEMORY );..  /
18d67 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
18d68 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72  s only called fr
18d69 6f 6d 20 74 68 65 20 4f 50 5f 4a 6f 75 72 6e 61  om the OP_Journa
18d6a 6c 4d 6f 64 65 20 6f 70 63 6f 64 65 2c 20 61 6e  lMode opcode, an
18d6b 64 0a 20 20 2a 2a 20 74 68 65 20 6c 6f 67 69 63  d.  ** the logic
18d6c 20 74 68 65 72 65 20 77 69 6c 6c 20 6e 65 76 65   there will neve
18d6d 72 20 61 6c 6c 6f 77 20 61 20 74 65 6d 70 6f 72  r allow a tempor
18d6e 61 72 79 20 66 69 6c 65 20 74 6f 20 62 65 20 63  ary file to be c
18d6f 68 61 6e 67 65 64 0a 20 20 2a 2a 20 74 6f 20 57  hanged.  ** to W
18d70 41 4c 20 6d 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20  AL mode..  */.  
18d71 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
18d72 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20 65  tempFile==0 || e
18d73 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode!=PAGER_JOUR
18d74 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 29 3b 0a 0a  NALMODE_WAL );..
18d75 20 20 2f 2a 20 44 6f 20 61 6c 6c 6f 77 20 74 68    /* Do allow th
18d76 65 20 6a 6f 75 72 6e 61 6c 6d 6f 64 65 20 6f 66  e journalmode of
18d77 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
18d78 74 61 62 61 73 65 20 74 6f 20 62 65 20 73 65 74  tabase to be set
18d79 20 74 6f 0a 20 20 2a 2a 20 61 6e 79 74 68 69 6e   to.  ** anythin
18d7a 67 20 6f 74 68 65 72 20 74 68 61 6e 20 4d 45 4d  g other than MEM
18d7b 4f 52 59 20 6f 72 20 4f 46 46 0a 20 20 2a 2f 0a  ORY or OFF.  */.
18d7c 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20    if( MEMDB ){. 
18d7d 20 20 20 61 73 73 65 72 74 28 20 65 4f 6c 64 3d     assert( eOld=
18d7e 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
18d7f 44 45 5f 4d 45 4d 4f 52 59 20 7c 7c 20 65 4f 6c  DE_MEMORY || eOl
18d80 64 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  d==PAGER_JOURNAL
18d81 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 20 20  MODE_OFF );.    
18d82 69 66 28 20 65 4d 6f 64 65 21 3d 50 41 47 45 52  if( eMode!=PAGER
18d83 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d  _JOURNALMODE_MEM
18d84 4f 52 59 20 26 26 20 65 4d 6f 64 65 21 3d 50 41  ORY && eMode!=PA
18d85 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
18d86 4f 46 46 20 29 7b 0a 20 20 20 20 20 20 65 4d 6f  OFF ){.      eMo
18d87 64 65 20 3d 20 65 4f 6c 64 3b 0a 20 20 20 20 7d  de = eOld;.    }
18d88 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 4d 6f 64  .  }..  if( eMod
18d89 65 21 3d 65 4f 6c 64 20 29 7b 0a 0a 20 20 20 20  e!=eOld ){..    
18d8a 2f 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6a 6f  /* Change the jo
18d8b 75 72 6e 61 6c 20 6d 6f 64 65 2e 20 2a 2f 0a 20  urnal mode. */. 
18d8c 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
18d8d 72 2d 3e 65 53 74 61 74 65 21 3d 50 41 47 45 52  r->eState!=PAGER
18d8e 5f 45 52 52 4f 52 20 29 3b 0a 20 20 20 20 70 50  _ERROR );.    pP
18d8f 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
18d90 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 0a  e = (u8)eMode;..
18d91 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 72 61 6e      /* When tran
18d92 73 69 73 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20  sistioning from 
18d93 54 52 55 4e 43 41 54 45 20 6f 72 20 50 45 52 53  TRUNCATE or PERS
18d94 49 53 54 20 74 6f 20 61 6e 79 20 6f 74 68 65 72  IST to any other
18d95 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20   journal.    ** 
18d96 6d 6f 64 65 20 65 78 63 65 70 74 20 57 41 4c 2c  mode except WAL,
18d97 20 75 6e 6c 65 73 73 20 74 68 65 20 70 61 67 65   unless the page
18d98 72 20 69 73 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f  r is in locking_
18d99 6d 6f 64 65 3d 65 78 63 6c 75 73 69 76 65 20 6d  mode=exclusive m
18d9a 6f 64 65 2c 0a 20 20 20 20 2a 2a 20 64 65 6c 65  ode,.    ** dele
18d9b 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  te the journal f
18d9c 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ile..    */.    
18d9d 61 73 73 65 72 74 28 20 28 50 41 47 45 52 5f 4a  assert( (PAGER_J
18d9e 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43  OURNALMODE_TRUNC
18d9f 41 54 45 20 26 20 35 29 3d 3d 31 20 29 3b 0a 20  ATE & 5)==1 );. 
18da0 20 20 20 61 73 73 65 72 74 28 20 28 50 41 47 45     assert( (PAGE
18da1 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45  R_JOURNALMODE_PE
18da2 52 53 49 53 54 20 26 20 35 29 3d 3d 31 20 29 3b  RSIST & 5)==1 );
18da3 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 50 41  .    assert( (PA
18da4 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
18da5 44 45 4c 45 54 45 20 26 20 35 29 3d 3d 30 20 29  DELETE & 5)==0 )
18da6 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 50  ;.    assert( (P
18da7 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
18da8 5f 4d 45 4d 4f 52 59 20 26 20 35 29 3d 3d 34 20  _MEMORY & 5)==4 
18da9 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
18daa 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
18dab 45 5f 4f 46 46 20 26 20 35 29 3d 3d 30 20 29 3b  E_OFF & 5)==0 );
18dac 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 50 41  .    assert( (PA
18dad 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
18dae 57 41 4c 20 26 20 35 29 3d 3d 35 20 29 3b 0a 0a  WAL & 5)==5 );..
18daf 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70      assert( isOp
18db0 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c  en(pPager->fd) |
18db1 7c 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73  | pPager->exclus
18db2 69 76 65 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69  iveMode );.    i
18db3 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c  f( !pPager->excl
18db4 75 73 69 76 65 4d 6f 64 65 20 26 26 20 28 65 4f  usiveMode && (eO
18db5 6c 64 20 26 20 35 29 3d 3d 31 20 26 26 20 28 65  ld & 5)==1 && (e
18db6 4d 6f 64 65 20 26 20 31 29 3d 3d 30 20 29 7b 0a  Mode & 1)==0 ){.
18db7 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69  .      /* In thi
18db8 73 20 63 61 73 65 20 77 65 20 77 6f 75 6c 64 20  s case we would 
18db9 6c 69 6b 65 20 74 6f 20 64 65 6c 65 74 65 20 74  like to delete t
18dba 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
18dbb 20 49 66 20 69 74 20 69 73 0a 20 20 20 20 20 20   If it is.      
18dbc 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2c  ** not possible,
18dbd 20 74 68 65 6e 20 74 68 61 74 20 69 73 20 6e 6f   then that is no
18dbe 74 20 61 20 70 72 6f 62 6c 65 6d 2e 20 44 65 6c  t a problem. Del
18dbf 65 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61  eting the journa
18dc0 6c 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20  l file.      ** 
18dc1 68 65 72 65 20 69 73 20 61 6e 20 6f 70 74 69 6d  here is an optim
18dc2 69 7a 61 74 69 6f 6e 20 6f 6e 6c 79 2e 0a 20 20  ization only..  
18dc3 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
18dc4 42 65 66 6f 72 65 20 64 65 6c 65 74 69 6e 67 20  Before deleting 
18dc5 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
18dc6 2c 20 6f 62 74 61 69 6e 20 61 20 52 45 53 45 52  , obtain a RESER
18dc7 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a  VED lock on the.
18dc8 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73        ** databas
18dc9 65 20 66 69 6c 65 2e 20 54 68 69 73 20 65 6e 73  e file. This ens
18dca 75 72 65 73 20 74 68 61 74 20 74 68 65 20 6a 6f  ures that the jo
18dcb 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f  urnal file is no
18dcc 74 20 64 65 6c 65 74 65 64 0a 20 20 20 20 20 20  t deleted.      
18dcd 2a 2a 20 77 68 69 6c 65 20 69 74 20 69 73 20 69  ** while it is i
18dce 6e 20 75 73 65 20 62 79 20 73 6f 6d 65 20 6f 74  n use by some ot
18dcf 68 65 72 20 63 6c 69 65 6e 74 2e 0a 20 20 20 20  her client..    
18dd0 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
18dd1 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72  e3OsClose(pPager
18dd2 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 69 66  ->jfd);.      if
18dd3 28 20 70 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3e  ( pPager->eLock>
18dd4 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29  =RESERVED_LOCK )
18dd5 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
18dd6 33 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65 72  3OsDelete(pPager
18dd7 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  ->pVfs, pPager->
18dd8 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20  zJournal, 0);.  
18dd9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
18dda 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49     int rc = SQLI
18ddb 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 69  TE_OK;.        i
18ddc 6e 74 20 73 74 61 74 65 20 3d 20 70 50 61 67 65  nt state = pPage
18ddd 72 2d 3e 65 53 74 61 74 65 3b 0a 20 20 20 20 20  r->eState;.     
18dde 20 20 20 61 73 73 65 72 74 28 20 73 74 61 74 65     assert( state
18ddf 3d 3d 50 41 47 45 52 5f 4f 50 45 4e 20 7c 7c 20  ==PAGER_OPEN || 
18de0 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 41  state==PAGER_REA
18de1 44 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 69  DER );.        i
18de2 66 28 20 73 74 61 74 65 3d 3d 50 41 47 45 52 5f  f( state==PAGER_
18de3 4f 50 45 4e 20 29 7b 0a 20 20 20 20 20 20 20 20  OPEN ){.        
18de4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
18de5 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 70 50  gerSharedLock(pP
18de6 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d  ager);.        }
18de7 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
18de8 67 65 72 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47  ger->eState==PAG
18de9 45 52 5f 52 45 41 44 45 52 20 29 7b 0a 20 20 20  ER_READER ){.   
18dea 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72         assert( r
18deb 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c==SQLITE_OK );.
18dec 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70            rc = p
18ded 61 67 65 72 4c 6f 63 6b 44 62 28 70 50 61 67 65  agerLockDb(pPage
18dee 72 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  r, RESERVED_LOCK
18def 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
18df0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
18df1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
18df2 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c      sqlite3OsDel
18df3 65 74 65 28 70 50 61 67 65 72 2d 3e 70 56 66 73  ete(pPager->pVfs
18df4 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e  , pPager->zJourn
18df5 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  al, 0);.        
18df6 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  }.        if( rc
18df7 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73  ==SQLITE_OK && s
18df8 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 41 44  tate==PAGER_READ
18df9 45 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ER ){.          
18dfa 70 61 67 65 72 55 6e 6c 6f 63 6b 44 62 28 70 50  pagerUnlockDb(pP
18dfb 61 67 65 72 2c 20 53 48 41 52 45 44 5f 4c 4f 43  ager, SHARED_LOC
18dfc 4b 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  K);.        }els
18dfd 65 20 69 66 28 20 73 74 61 74 65 3d 3d 50 41 47  e if( state==PAG
18dfe 45 52 5f 4f 50 45 4e 20 29 7b 0a 20 20 20 20 20  ER_OPEN ){.     
18dff 20 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63       pager_unloc
18e00 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  k(pPager);.     
18e01 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73     }.        ass
18e02 65 72 74 28 20 73 74 61 74 65 3d 3d 70 50 61 67  ert( state==pPag
18e03 65 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 20  er->eState );.  
18e04 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
18e05 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65  .  /* Return the
18e06 20 6e 65 77 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64   new journal mod
18e07 65 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 69  e */.  return (i
18e08 6e 74 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  nt)pPager->journ
18e09 61 6c 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  alMode;.}../*.**
18e0a 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72   Return the curr
18e0b 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65  ent journal mode
18e0c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18e0d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
18e0e 61 67 65 72 47 65 74 4a 6f 75 72 6e 61 6c 4d 6f  agerGetJournalMo
18e0f 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  de(Pager *pPager
18e10 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74  ){.  return (int
18e11 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  )pPager->journal
18e12 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  Mode;.}../*.** R
18e13 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
18e14 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 61 20  e pager is in a 
18e15 73 74 61 74 65 20 77 68 65 72 65 20 69 74 20 69  state where it i
18e16 73 20 4f 4b 20 74 6f 20 63 68 61 6e 67 65 20 74  s OK to change t
18e17 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 6d 6f 64  he.** journalmod
18e18 65 2e 20 20 4a 6f 75 72 6e 61 6c 6d 6f 64 65 20  e.  Journalmode 
18e19 63 68 61 6e 67 65 73 20 63 61 6e 20 6f 6e 6c 79  changes can only
18e1a 20 68 61 70 70 65 6e 20 77 68 65 6e 20 74 68 65   happen when the
18e1b 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20   database.** is 
18e1c 75 6e 6d 6f 64 69 66 69 65 64 2e 0a 2a 2f 0a 53  unmodified..*/.S
18e1d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
18e1e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 6b  t sqlite3PagerOk
18e1f 54 6f 43 68 61 6e 67 65 4a 6f 75 72 6e 61 6c 4d  ToChangeJournalM
18e20 6f 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ode(Pager *pPage
18e21 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 61 73  r){.  assert( as
18e22 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65  sert_pager_state
18e23 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 69 66  (pPager) );.  if
18e24 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65  ( pPager->eState
18e25 3e 3d 50 41 47 45 52 5f 57 52 49 54 45 52 5f 43  >=PAGER_WRITER_C
18e26 41 43 48 45 4d 4f 44 20 29 20 72 65 74 75 72 6e  ACHEMOD ) return
18e27 20 30 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28   0;.  if( NEVER(
18e28 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
18e29 66 64 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a  fd) && pPager->j
18e2a 6f 75 72 6e 61 6c 4f 66 66 3e 30 29 20 29 20 72  ournalOff>0) ) r
18e2b 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72  eturn 0;.  retur
18e2c 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  n 1;.}../*.** Ge
18e2d 74 2f 73 65 74 20 74 68 65 20 73 69 7a 65 2d 6c  t/set the size-l
18e2e 69 6d 69 74 20 75 73 65 64 20 66 6f 72 20 70 65  imit used for pe
18e2f 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c  rsistent journal
18e30 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65   files..**.** Se
18e31 74 74 69 6e 67 20 74 68 65 20 73 69 7a 65 20 6c  tting the size l
18e32 69 6d 69 74 20 74 6f 20 2d 31 20 6d 65 61 6e 73  imit to -1 means
18e33 20 6e 6f 20 6c 69 6d 69 74 20 69 73 20 65 6e 66   no limit is enf
18e34 6f 72 63 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 74  orced..** An att
18e35 65 6d 70 74 20 74 6f 20 73 65 74 20 61 20 6c 69  empt to set a li
18e36 6d 69 74 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e  mit smaller than
18e37 20 2d 31 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a   -1 is a no-op..
18e38 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18e39 45 20 69 36 34 20 73 71 6c 69 74 65 33 50 61 67  E i64 sqlite3Pag
18e3a 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d  erJournalSizeLim
18e3b 69 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  it(Pager *pPager
18e3c 2c 20 69 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20  , i64 iLimit){. 
18e3d 20 69 66 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20   if( iLimit>=-1 
18e3e 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a  ){.    pPager->j
18e3f 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20  ournalSizeLimit 
18e40 3d 20 69 4c 69 6d 69 74 3b 0a 20 20 20 20 73 71  = iLimit;.    sq
18e41 6c 69 74 65 33 57 61 6c 4c 69 6d 69 74 28 70 50  lite3WalLimit(pP
18e42 61 67 65 72 2d 3e 70 57 61 6c 2c 20 69 4c 69 6d  ager->pWal, iLim
18e43 69 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  it);.  }.  retur
18e44 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  n pPager->journa
18e45 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a 0a 2f  lSizeLimit;.}../
18e46 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
18e47 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 50 61  inter to the pPa
18e48 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76 61 72  ger->pBackup var
18e49 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63 6b 75  iable. The backu
18e4a 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e 20 62  p module.** in b
18e4b 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61 69 6e  ackup.c maintain
18e4c 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  s the content of
18e4d 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 2e 20   this variable. 
18e4e 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a 20 75  This module.** u
18e4f 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c 79 20  ses it opaquely 
18e50 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74  as an argument t
18e51 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52  o sqlite3BackupR
18e52 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a 2a 20  estart() and.** 
18e53 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64  sqlite3BackupUpd
18e54 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53  ate() only..*/.S
18e55 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
18e56 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73  lite3_backup **s
18e57 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b 75  qlite3PagerBacku
18e58 70 50 74 72 28 50 61 67 65 72 20 2a 70 50 61 67  pPtr(Pager *pPag
18e59 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 26 70  er){.  return &p
18e5a 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 3b 0a  Pager->pBackup;.
18e5b 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
18e5c 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 2f 2a  E_OMIT_VACUUM./*
18e5d 0a 2a 2a 20 55 6e 6c 65 73 73 20 74 68 69 73 20  .** Unless this 
18e5e 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  is an in-memory 
18e5f 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74  or temporary dat
18e60 61 62 61 73 65 2c 20 63 6c 65 61 72 20 74 68 65  abase, clear the
18e61 20 70 61 67 65 72 20 63 61 63 68 65 2e 0a 2a 2f   pager cache..*/
18e62 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18e63 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
18e64 72 43 6c 65 61 72 43 61 63 68 65 28 50 61 67 65  rClearCache(Page
18e65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66  r *pPager){.  if
18e66 28 20 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67  ( !MEMDB && pPag
18e67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20  er->tempFile==0 
18e68 29 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50  ) pager_reset(pP
18e69 61 67 65 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  ager);.}.#endif.
18e6a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18e6b 4f 4d 49 54 5f 57 41 4c 0a 2f 2a 0a 2a 2a 20 54  OMIT_WAL./*.** T
18e6c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
18e6d 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20  called when the 
18e6e 75 73 65 72 20 69 6e 76 6f 6b 65 73 20 22 50 52  user invokes "PR
18e6f 41 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70 6f  AGMA wal_checkpo
18e70 69 6e 74 22 2c 0a 2a 2a 20 22 50 52 41 47 4d 41  int",.** "PRAGMA
18e71 20 77 61 6c 5f 62 6c 6f 63 6b 69 6e 67 5f 63 68   wal_blocking_ch
18e72 65 63 6b 70 6f 69 6e 74 22 20 6f 72 20 63 61 6c  eckpoint" or cal
18e73 6c 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 77  ls the sqlite3_w
18e74 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 28 29 0a  al_checkpoint().
18e75 2a 2a 20 6f 72 20 77 61 6c 5f 62 6c 6f 63 6b 69  ** or wal_blocki
18e76 6e 67 5f 63 68 65 63 6b 70 6f 69 6e 74 28 29 20  ng_checkpoint() 
18e77 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  API functions..*
18e78 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 65  *.** Parameter e
18e79 4d 6f 64 65 20 69 73 20 6f 6e 65 20 6f 66 20 53  Mode is one of S
18e7a 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54  QLITE_CHECKPOINT
18e7b 5f 50 41 53 53 49 56 45 2c 20 46 55 4c 4c 20 6f  _PASSIVE, FULL o
18e7c 72 20 52 45 53 54 41 52 54 2e 0a 2a 2f 0a 53 51  r RESTART..*/.SQ
18e7d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
18e7e 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 68 65   sqlite3PagerChe
18e7f 63 6b 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  ckpoint(Pager *p
18e80 50 61 67 65 72 2c 20 69 6e 74 20 65 4d 6f 64 65  Pager, int eMode
18e81 2c 20 69 6e 74 20 2a 70 6e 4c 6f 67 2c 20 69 6e  , int *pnLog, in
18e82 74 20 2a 70 6e 43 6b 70 74 29 7b 0a 20 20 69 6e  t *pnCkpt){.  in
18e83 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
18e84 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
18e85 70 57 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d  pWal ){.    rc =
18e86 20 73 71 6c 69 74 65 33 57 61 6c 43 68 65 63 6b   sqlite3WalCheck
18e87 70 6f 69 6e 74 28 70 50 61 67 65 72 2d 3e 70 57  point(pPager->pW
18e88 61 6c 2c 20 65 4d 6f 64 65 2c 0a 20 20 20 20 20  al, eMode,.     
18e89 20 20 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79     pPager->xBusy
18e8a 48 61 6e 64 6c 65 72 2c 20 70 50 61 67 65 72 2d  Handler, pPager-
18e8b 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67  >pBusyHandlerArg
18e8c 2c 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72  ,.        pPager
18e8d 2d 3e 63 6b 70 74 53 79 6e 63 46 6c 61 67 73 2c  ->ckptSyncFlags,
18e8e 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
18e8f 65 2c 20 28 75 38 20 2a 29 70 50 61 67 65 72 2d  e, (u8 *)pPager-
18e90 3e 70 54 6d 70 53 70 61 63 65 2c 0a 20 20 20 20  >pTmpSpace,.    
18e91 20 20 20 20 70 6e 4c 6f 67 2c 20 70 6e 43 6b 70      pnLog, pnCkp
18e92 74 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 20 20 72  t.    );.  }.  r
18e93 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 53 51 4c  eturn rc;.}..SQL
18e94 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
18e95 73 71 6c 69 74 65 33 50 61 67 65 72 57 61 6c 43  sqlite3PagerWalC
18e96 61 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70  allback(Pager *p
18e97 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  Pager){.  return
18e98 20 73 71 6c 69 74 65 33 57 61 6c 43 61 6c 6c 62   sqlite3WalCallb
18e99 61 63 6b 28 70 50 61 67 65 72 2d 3e 70 57 61 6c  ack(pPager->pWal
18e9a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
18e9b 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 75  rn true if the u
18e9c 6e 64 65 72 6c 79 69 6e 67 20 56 46 53 20 66 6f  nderlying VFS fo
18e9d 72 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65  r the given page
18e9e 72 20 73 75 70 70 6f 72 74 73 20 74 68 65 0a 2a  r supports the.*
18e9f 2a 20 70 72 69 6d 69 74 69 76 65 73 20 6e 65 63  * primitives nec
18ea0 65 73 73 61 72 79 20 66 6f 72 20 77 72 69 74 65  essary for write
18ea1 2d 61 68 65 61 64 20 6c 6f 67 67 69 6e 67 2e 0a  -ahead logging..
18ea2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18ea3 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
18ea4 65 72 57 61 6c 53 75 70 70 6f 72 74 65 64 28 50  erWalSupported(P
18ea5 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
18ea6 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
18ea7 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68  o_methods *pMeth
18ea8 6f 64 73 20 3d 20 70 50 61 67 65 72 2d 3e 66 64  ods = pPager->fd
18ea9 2d 3e 70 4d 65 74 68 6f 64 73 3b 0a 20 20 72 65  ->pMethods;.  re
18eaa 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 78 63  turn pPager->exc
18eab 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 28 70  lusiveMode || (p
18eac 4d 65 74 68 6f 64 73 2d 3e 69 56 65 72 73 69 6f  Methods->iVersio
18ead 6e 3e 3d 32 20 26 26 20 70 4d 65 74 68 6f 64 73  n>=2 && pMethods
18eae 2d 3e 78 53 68 6d 4d 61 70 29 3b 0a 7d 0a 0a 2f  ->xShmMap);.}../
18eaf 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  *.** Attempt to 
18eb0 74 61 6b 65 20 61 6e 20 65 78 63 6c 75 73 69 76  take an exclusiv
18eb1 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  e lock on the da
18eb2 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20  tabase file. If 
18eb3 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 0a 2a  a PENDING lock.*
18eb4 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 69 6e  * is obtained in
18eb5 73 74 65 61 64 2c 20 69 6d 6d 65 64 69 61 74 65  stead, immediate
18eb6 6c 79 20 72 65 6c 65 61 73 65 20 69 74 2e 0a 2a  ly release it..*
18eb7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
18eb8 65 72 45 78 63 6c 75 73 69 76 65 4c 6f 63 6b 28  erExclusiveLock(
18eb9 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
18eba 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
18ebb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18ebc 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
18ebd 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
18ebe 50 61 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 53 48  Pager->eLock==SH
18ebf 41 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 50 61  ARED_LOCK || pPa
18ec0 67 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c  ger->eLock==EXCL
18ec1 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20  USIVE_LOCK );.  
18ec2 72 63 20 3d 20 70 61 67 65 72 4c 6f 63 6b 44 62  rc = pagerLockDb
18ec3 28 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49  (pPager, EXCLUSI
18ec4 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20  VE_LOCK);.  if( 
18ec5 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
18ec6 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 61  .    /* If the a
18ec7 74 74 65 6d 70 74 20 74 6f 20 67 72 61 62 20 74  ttempt to grab t
18ec8 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  he exclusive loc
18ec9 6b 20 66 61 69 6c 65 64 2c 20 72 65 6c 65 61 73  k failed, releas
18eca 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 70 65  e the .    ** pe
18ecb 6e 64 69 6e 67 20 6c 6f 63 6b 20 74 68 61 74 20  nding lock that 
18ecc 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20 6f 62  may have been ob
18ecd 74 61 69 6e 65 64 20 69 6e 73 74 65 61 64 2e 20  tained instead. 
18ece 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 55 6e 6c   */.    pagerUnl
18ecf 6f 63 6b 44 62 28 70 50 61 67 65 72 2c 20 53 48  ockDb(pPager, SH
18ed0 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 7d 0a  ARED_LOCK);.  }.
18ed1 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
18ed2 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69  ./*.** Call sqli
18ed3 74 65 33 57 61 6c 4f 70 65 6e 28 29 20 74 6f 20  te3WalOpen() to 
18ed4 6f 70 65 6e 20 74 68 65 20 57 41 4c 20 68 61 6e  open the WAL han
18ed5 64 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67 65  dle. If the page
18ed6 72 20 69 73 20 69 6e 20 0a 2a 2a 20 65 78 63 6c  r is in .** excl
18ed7 75 73 69 76 65 2d 6c 6f 63 6b 69 6e 67 20 6d 6f  usive-locking mo
18ed8 64 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  de when this fun
18ed9 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c  ction is called,
18eda 20 74 61 6b 65 20 61 6e 20 45 58 43 4c 55 53 49   take an EXCLUSI
18edb 56 45 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 68  VE.** lock on th
18edc 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
18edd 61 6e 64 20 75 73 65 20 68 65 61 70 2d 6d 65 6d  and use heap-mem
18ede 6f 72 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ory to store the
18edf 20 77 61 6c 2d 69 6e 64 65 78 0a 2a 2a 20 69 6e   wal-index.** in
18ee0 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 75 73 65  . Otherwise, use
18ee1 20 74 68 65 20 6e 6f 72 6d 61 6c 20 73 68 61 72   the normal shar
18ee2 65 64 2d 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74  ed-memory..*/.st
18ee3 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 4f 70  atic int pagerOp
18ee4 65 6e 57 61 6c 28 50 61 67 65 72 20 2a 70 50 61  enWal(Pager *pPa
18ee5 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ger){.  int rc =
18ee6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61   SQLITE_OK;..  a
18ee7 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70  ssert( pPager->p
18ee8 57 61 6c 3d 3d 30 20 26 26 20 70 50 61 67 65 72  Wal==0 && pPager
18ee9 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 29 3b  ->tempFile==0 );
18eea 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
18eeb 72 2d 3e 65 4c 6f 63 6b 3d 3d 53 48 41 52 45 44  r->eLock==SHARED
18eec 5f 4c 4f 43 4b 20 7c 7c 20 70 50 61 67 65 72 2d  _LOCK || pPager-
18eed 3e 65 4c 6f 63 6b 3d 3d 45 58 43 4c 55 53 49 56  >eLock==EXCLUSIV
18eee 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20  E_LOCK );..  /* 
18eef 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  If the pager is 
18ef0 61 6c 72 65 61 64 79 20 69 6e 20 65 78 63 6c 75  already in exclu
18ef1 73 69 76 65 2d 6d 6f 64 65 2c 20 74 68 65 20 57  sive-mode, the W
18ef2 41 4c 20 6d 6f 64 75 6c 65 20 77 69 6c 6c 20 75  AL module will u
18ef3 73 65 20 0a 20 20 2a 2a 20 68 65 61 70 2d 6d 65  se .  ** heap-me
18ef4 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 77 61 6c  mory for the wal
18ef5 2d 69 6e 64 65 78 20 69 6e 73 74 65 61 64 20 6f  -index instead o
18ef6 66 20 74 68 65 20 56 46 53 20 73 68 61 72 65 64  f the VFS shared
18ef7 2d 6d 65 6d 6f 72 79 20 0a 20 20 2a 2a 20 69 6d  -memory .  ** im
18ef8 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 54 61  plementation. Ta
18ef9 6b 65 20 74 68 65 20 65 78 63 6c 75 73 69 76 65  ke the exclusive
18efa 20 6c 6f 63 6b 20 6e 6f 77 2c 20 62 65 66 6f 72   lock now, befor
18efb 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 57 41  e opening the WA
18efc 4c 0a 20 20 2a 2a 20 66 69 6c 65 2c 20 74 6f 20  L.  ** file, to 
18efd 6d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 69  make sure this i
18efe 73 20 73 61 66 65 2e 0a 20 20 2a 2f 0a 20 20 69  s safe..  */.  i
18eff 66 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75  f( pPager->exclu
18f00 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20  siveMode ){.    
18f01 72 63 20 3d 20 70 61 67 65 72 45 78 63 6c 75 73  rc = pagerExclus
18f02 69 76 65 4c 6f 63 6b 28 70 50 61 67 65 72 29 3b  iveLock(pPager);
18f03 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20  .  }..  /* Open 
18f04 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  the connection t
18f05 6f 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e 20  o the log file. 
18f06 49 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f  If this operatio
18f07 6e 20 66 61 69 6c 73 2c 20 0a 20 20 2a 2a 20 28  n fails, .  ** (
18f08 65 2e 67 2e 20 64 75 65 20 74 6f 20 6d 61 6c 6c  e.g. due to mall
18f09 6f 63 28 29 20 66 61 69 6c 75 72 65 29 2c 20 72  oc() failure), r
18f0a 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
18f0b 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ode..  */.  if( 
18f0c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
18f0d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
18f0e 33 57 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d  3WalOpen(pPager-
18f0f 3e 70 56 66 73 2c 20 0a 20 20 20 20 20 20 20 20  >pVfs, .        
18f10 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67  pPager->fd, pPag
18f11 65 72 2d 3e 7a 57 61 6c 2c 20 70 50 61 67 65 72  er->zWal, pPager
18f12 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 2c  ->exclusiveMode,
18f13 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
18f14 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69  >journalSizeLimi
18f15 74 2c 20 26 70 50 61 67 65 72 2d 3e 70 57 61 6c  t, &pPager->pWal
18f16 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 72  .    );.  }..  r
18f17 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
18f18 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  .** The caller m
18f19 75 73 74 20 62 65 20 68 6f 6c 64 69 6e 67 20 61  ust be holding a
18f1a 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 20   SHARED lock on 
18f1b 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
18f1c 65 20 74 6f 20 63 61 6c 6c 0a 2a 2a 20 74 68 69  e to call.** thi
18f1d 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  s function..**.*
18f1e 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 70  * If the pager p
18f1f 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72  assed as the fir
18f20 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f  st argument is o
18f21 70 65 6e 20 6f 6e 20 61 20 72 65 61 6c 20 64 61  pen on a real da
18f22 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 28  tabase.** file (
18f23 6e 6f 74 20 61 20 74 65 6d 70 20 66 69 6c 65 20  not a temp file 
18f24 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  or an in-memory 
18f25 64 61 74 61 62 61 73 65 29 2c 20 61 6e 64 20 74  database), and t
18f26 68 65 20 57 41 4c 20 66 69 6c 65 0a 2a 2a 20 69  he WAL file.** i
18f27 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70  s not already op
18f28 65 6e 2c 20 6d 61 6b 65 20 61 6e 20 61 74 74 65  en, make an atte
18f29 6d 70 74 20 74 6f 20 6f 70 65 6e 20 69 74 20 6e  mpt to open it n
18f2a 6f 77 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  ow. If successfu
18f2b 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c  l,.** return SQL
18f2c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 65 72  ITE_OK. If an er
18f2d 72 6f 72 20 6f 63 63 75 72 73 20 6f 72 20 74 68  ror occurs or th
18f2e 65 20 56 46 53 20 75 73 65 64 20 62 79 20 74 68  e VFS used by th
18f2f 65 20 70 61 67 65 72 20 64 6f 65 73 20 0a 2a 2a  e pager does .**
18f30 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 74 68 65   not support the
18f31 20 78 53 68 6d 58 58 58 28 29 20 6d 65 74 68 6f   xShmXXX() metho
18f32 64 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72  ds, return an er
18f33 72 6f 72 20 63 6f 64 65 2e 20 2a 70 62 4f 70 65  ror code. *pbOpe
18f34 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 6d 6f 64 69  n is.** not modi
18f35 66 69 65 64 20 69 6e 20 65 69 74 68 65 72 20 63  fied in either c
18f36 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ase..**.** If th
18f37 65 20 70 61 67 65 72 20 69 73 20 6f 70 65 6e 20  e pager is open 
18f38 6f 6e 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 28  on a temp-file (
18f39 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  or in-memory dat
18f3a 61 62 61 73 65 29 2c 20 6f 72 20 69 66 0a 2a 2a  abase), or if.**
18f3b 20 74 68 65 20 57 41 4c 20 66 69 6c 65 20 69 73   the WAL file is
18f3c 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 2c 20 73   already open, s
18f3d 65 74 20 2a 70 62 4f 70 65 6e 20 74 6f 20 31 20  et *pbOpen to 1 
18f3e 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  and return SQLIT
18f3f 45 5f 4f 4b 0a 2a 2a 20 77 69 74 68 6f 75 74 20  E_OK.** without 
18f40 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 0a  doing anything..
18f41 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18f42 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
18f43 65 72 4f 70 65 6e 57 61 6c 28 0a 20 20 50 61 67  erOpenWal(.  Pag
18f44 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20  er *pPager,     
18f45 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18f46 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  Pager object */.
18f47 20 20 69 6e 74 20 2a 70 62 4f 70 65 6e 20 20 20    int *pbOpen   
18f48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18f49 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f    /* OUT: Set to
18f4a 20 74 72 75 65 20 69 66 20 63 61 6c 6c 20 69 73   true if call is
18f4b 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 29 7b 0a 20   a no-op */.){. 
18f4c 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
18f4d 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
18f4e 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
18f4f 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 61 73  */..  assert( as
18f50 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65  sert_pager_state
18f51 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 61 73  (pPager) );.  as
18f52 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 53  sert( pPager->eS
18f53 74 61 74 65 3d 3d 50 41 47 45 52 5f 4f 50 45 4e  tate==PAGER_OPEN
18f54 20 20 20 7c 7c 20 70 62 4f 70 65 6e 20 29 3b 0a     || pbOpen );.
18f55 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
18f56 2d 3e 65 53 74 61 74 65 3d 3d 50 41 47 45 52 5f  ->eState==PAGER_
18f57 52 45 41 44 45 52 20 7c 7c 20 21 70 62 4f 70 65  READER || !pbOpe
18f58 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  n );.  assert( p
18f59 62 4f 70 65 6e 3d 3d 30 20 7c 7c 20 2a 70 62 4f  bOpen==0 || *pbO
18f5a 70 65 6e 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  pen==0 );.  asse
18f5b 72 74 28 20 70 62 4f 70 65 6e 21 3d 30 20 7c 7c  rt( pbOpen!=0 ||
18f5c 20 28 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46   (!pPager->tempF
18f5d 69 6c 65 20 26 26 20 21 70 50 61 67 65 72 2d 3e  ile && !pPager->
18f5e 70 57 61 6c 29 20 29 3b 0a 0a 20 20 69 66 28 20  pWal) );..  if( 
18f5f 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
18f60 65 20 26 26 20 21 70 50 61 67 65 72 2d 3e 70 57  e && !pPager->pW
18f61 61 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 21 73  al ){.    if( !s
18f62 71 6c 69 74 65 33 50 61 67 65 72 57 61 6c 53 75  qlite3PagerWalSu
18f63 70 70 6f 72 74 65 64 28 70 50 61 67 65 72 29 20  pported(pPager) 
18f64 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
18f65 43 41 4e 54 4f 50 45 4e 3b 0a 0a 20 20 20 20 2f  CANTOPEN;..    /
18f66 2a 20 43 6c 6f 73 65 20 61 6e 79 20 72 6f 6c 6c  * Close any roll
18f67 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 70 72 65  back journal pre
18f68 76 69 6f 75 73 6c 79 20 6f 70 65 6e 20 2a 2f 0a  viously open */.
18f69 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
18f6a 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b  se(pPager->jfd);
18f6b 0a 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72  ..    rc = pager
18f6c 4f 70 65 6e 57 61 6c 28 70 50 61 67 65 72 29 3b  OpenWal(pPager);
18f6d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
18f6e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
18f6f 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
18f70 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52  ode = PAGER_JOUR
18f71 4e 41 4c 4d 4f 44 45 5f 57 41 4c 3b 0a 20 20 20  NALMODE_WAL;.   
18f72 20 20 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74     pPager->eStat
18f73 65 20 3d 20 50 41 47 45 52 5f 4f 50 45 4e 3b 0a  e = PAGER_OPEN;.
18f74 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
18f75 20 20 20 2a 70 62 4f 70 65 6e 20 3d 20 31 3b 0a     *pbOpen = 1;.
18f76 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
18f77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
18f78 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
18f79 65 64 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20  ed to close the 
18f7a 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68  connection to th
18f7b 65 20 6c 6f 67 20 66 69 6c 65 20 70 72 69 6f 72  e log file prior
18f7c 0a 2a 2a 20 74 6f 20 73 77 69 74 63 68 69 6e 67  .** to switching
18f7d 20 66 72 6f 6d 20 57 41 4c 20 74 6f 20 72 6f 6c   from WAL to rol
18f7e 6c 62 61 63 6b 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a  lback mode..**.*
18f7f 2a 20 42 65 66 6f 72 65 20 63 6c 6f 73 69 6e 67  * Before closing
18f80 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 2c 20 74   the log file, t
18f81 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74  his function att
18f82 65 6d 70 74 73 20 74 6f 20 74 61 6b 65 20 61 6e  empts to take an
18f83 20 0a 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c   .** EXCLUSIVE l
18f84 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
18f85 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 69  ase file. If thi
18f86 73 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61  s cannot be obta
18f87 69 6e 65 64 2c 20 61 6e 0a 2a 2a 20 65 72 72 6f  ined, an.** erro
18f88 72 20 28 53 51 4c 49 54 45 5f 42 55 53 59 29 20  r (SQLITE_BUSY) 
18f89 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
18f8a 74 68 65 20 6c 6f 67 20 63 6f 6e 6e 65 63 74 69  the log connecti
18f8b 6f 6e 20 69 73 20 6e 6f 74 20 63 6c 6f 73 65 64  on is not closed
18f8c 2e 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66  ..** If successf
18f8d 75 6c 2c 20 74 68 65 20 45 58 43 4c 55 53 49 56  ul, the EXCLUSIV
18f8e 45 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65  E lock is not re
18f8f 6c 65 61 73 65 64 20 62 65 66 6f 72 65 20 72 65  leased before re
18f90 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49  turning..*/.SQLI
18f91 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
18f92 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65  qlite3PagerClose
18f93 57 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65  Wal(Pager *pPage
18f94 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  r){.  int rc = S
18f95 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73  QLITE_OK;..  ass
18f96 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ert( pPager->jou
18f97 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
18f98 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20  JOURNALMODE_WAL 
18f99 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
18f9a 6c 6f 67 20 66 69 6c 65 20 69 73 20 6e 6f 74 20  log file is not 
18f9b 61 6c 72 65 61 64 79 20 6f 70 65 6e 2c 20 62 75  already open, bu
18f9c 74 20 64 6f 65 73 20 65 78 69 73 74 20 69 6e 20  t does exist in 
18f9d 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c  the file-system,
18f9e 0a 20 20 2a 2a 20 69 74 20 6d 61 79 20 6e 65 65  .  ** it may nee
18f9f 64 20 74 6f 20 62 65 20 63 68 65 63 6b 70 6f 69  d to be checkpoi
18fa0 6e 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 20  nted before the 
18fa1 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 20 73  connection can s
18fa2 77 69 74 63 68 20 74 6f 0a 20 20 2a 2a 20 72 6f  witch to.  ** ro
18fa3 6c 6c 62 61 63 6b 20 6d 6f 64 65 2e 20 4f 70 65  llback mode. Ope
18fa4 6e 20 69 74 20 6e 6f 77 20 73 6f 20 74 68 69 73  n it now so this
18fa5 20 63 61 6e 20 68 61 70 70 65 6e 2e 0a 20 20 2a   can happen..  *
18fa6 2f 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d  /.  if( !pPager-
18fa7 3e 70 57 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74  >pWal ){.    int
18fa8 20 6c 6f 67 65 78 69 73 74 73 20 3d 20 30 3b 0a   logexists = 0;.
18fa9 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 4c 6f      rc = pagerLo
18faa 63 6b 44 62 28 70 50 61 67 65 72 2c 20 53 48 41  ckDb(pPager, SHA
18fab 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69  RED_LOCK);.    i
18fac 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
18fad 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
18fae 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 0a  qlite3OsAccess(.
18faf 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
18fb0 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  ->pVfs, pPager->
18fb1 7a 57 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43  zWal, SQLITE_ACC
18fb2 45 53 53 5f 45 58 49 53 54 53 2c 20 26 6c 6f 67  ESS_EXISTS, &log
18fb3 65 78 69 73 74 73 0a 20 20 20 20 20 20 29 3b 0a  exists.      );.
18fb4 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
18fb5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c  ==SQLITE_OK && l
18fb6 6f 67 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20  ogexists ){.    
18fb7 20 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65 6e    rc = pagerOpen
18fb8 57 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20  Wal(pPager);.   
18fb9 20 7d 0a 20 20 7d 0a 20 20 20 20 0a 20 20 2f 2a   }.  }.    .  /*
18fba 20 43 68 65 63 6b 70 6f 69 6e 74 20 61 6e 64 20   Checkpoint and 
18fbb 63 6c 6f 73 65 20 74 68 65 20 6c 6f 67 2e 20 42  close the log. B
18fbc 65 63 61 75 73 65 20 61 6e 20 45 58 43 4c 55 53  ecause an EXCLUS
18fbd 49 56 45 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  IVE lock is held
18fbe 20 6f 6e 0a 20 20 2a 2a 20 74 68 65 20 64 61 74   on.  ** the dat
18fbf 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20  abase file, the 
18fc0 6c 6f 67 20 61 6e 64 20 6c 6f 67 2d 73 75 6d 6d  log and log-summ
18fc1 61 72 79 20 66 69 6c 65 73 20 77 69 6c 6c 20 62  ary files will b
18fc2 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a 2f 0a  e deleted..  */.
18fc3 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
18fc4 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 70  _OK && pPager->p
18fc5 57 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  Wal ){.    rc = 
18fc6 70 61 67 65 72 45 78 63 6c 75 73 69 76 65 4c 6f  pagerExclusiveLo
18fc7 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  ck(pPager);.    
18fc8 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
18fc9 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
18fca 73 71 6c 69 74 65 33 57 61 6c 43 6c 6f 73 65 28  sqlite3WalClose(
18fcb 70 50 61 67 65 72 2d 3e 70 57 61 6c 2c 20 70 50  pPager->pWal, pP
18fcc 61 67 65 72 2d 3e 63 6b 70 74 53 79 6e 63 46 6c  ager->ckptSyncFl
18fcd 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ags,.           
18fce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18fcf 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
18fd0 2c 20 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 70  , (u8*)pPager->p
18fd1 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 20  TmpSpace);.     
18fd2 20 70 50 61 67 65 72 2d 3e 70 57 61 6c 20 3d 20   pPager->pWal = 
18fd3 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  0;.    }.  }.  r
18fd4 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e  eturn rc;.}..#en
18fd5 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f  dif /* !SQLITE_O
18fd6 4d 49 54 5f 57 41 4c 20 2a 2f 0a 0a 23 69 66 64  MIT_WAL */..#ifd
18fd7 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
18fd8 5f 5a 49 50 56 46 53 0a 2f 2a 0a 2a 2a 20 41 20  _ZIPVFS./*.** A 
18fd9 72 65 61 64 2d 6c 6f 63 6b 20 6d 75 73 74 20 62  read-lock must b
18fda 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 70 61  e held on the pa
18fdb 67 65 72 20 77 68 65 6e 20 74 68 69 73 20 66 75  ger when this fu
18fdc 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
18fdd 2e 20 49 66 0a 2a 2a 20 74 68 65 20 70 61 67 65  . If.** the page
18fde 72 20 69 73 20 69 6e 20 57 41 4c 20 6d 6f 64 65  r is in WAL mode
18fdf 20 61 6e 64 20 74 68 65 20 57 41 4c 20 66 69 6c   and the WAL fil
18fe0 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74  e currently cont
18fe1 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  ains one or more
18fe2 0a 2a 2a 20 66 72 61 6d 65 73 2c 20 72 65 74 75  .** frames, retu
18fe3 72 6e 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62  rn the size in b
18fe4 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65  ytes of the page
18fe5 20 69 6d 61 67 65 73 20 73 74 6f 72 65 64 20 77   images stored w
18fe6 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20 57 41 4c  ithin the.** WAL
18fe7 20 66 72 61 6d 65 73 2e 20 4f 74 68 65 72 77 69   frames. Otherwi
18fe8 73 65 2c 20 69 66 20 74 68 69 73 20 69 73 20 6e  se, if this is n
18fe9 6f 74 20 61 20 57 41 4c 20 64 61 74 61 62 61 73  ot a WAL databas
18fea 65 20 6f 72 20 74 68 65 20 57 41 4c 20 66 69 6c  e or the WAL fil
18feb 65 0a 2a 2a 20 69 73 20 65 6d 70 74 79 2c 20 72  e.** is empty, r
18fec 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49  eturn 0..*/.SQLI
18fed 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
18fee 71 6c 69 74 65 33 50 61 67 65 72 57 61 6c 46 72  qlite3PagerWalFr
18fef 61 6d 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70  amesize(Pager *p
18ff0 50 61 67 65 72 29 7b 0a 20 20 61 73 73 65 72 74  Pager){.  assert
18ff1 28 20 70 50 61 67 65 72 2d 3e 65 53 74 61 74 65  ( pPager->eState
18ff2 3d 3d 50 41 47 45 52 5f 52 45 41 44 45 52 20 29  ==PAGER_READER )
18ff3 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ;.  return sqlit
18ff4 65 33 57 61 6c 46 72 61 6d 65 73 69 7a 65 28 70  e3WalFramesize(p
18ff5 50 61 67 65 72 2d 3e 70 57 61 6c 29 3b 0a 7d 0a  Pager->pWal);.}.
18ff6 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
18ff7 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a  QLITE_HAS_CODEC.
18ff8 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
18ff9 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ion is called by
18ffa 20 74 68 65 20 77 61 6c 20 6d 6f 64 75 6c 65 20   the wal module 
18ffb 77 68 65 6e 20 77 72 69 74 69 6e 67 20 70 61 67  when writing pag
18ffc 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 6e 74  e content.** int
18ffd 6f 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e 0a  o the log file..
18ffe 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
18fff 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ion returns a po
19000 69 6e 74 65 72 20 74 6f 20 61 20 62 75 66 66 65  inter to a buffe
19001 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  r containing the
19002 20 65 6e 63 72 79 70 74 65 64 0a 2a 2a 20 70 61   encrypted.** pa
19003 67 65 20 63 6f 6e 74 65 6e 74 2e 20 49 66 20 61  ge content. If a
19004 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74   malloc fails, t
19005 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
19006 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f   return NULL..*/
19007 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19008 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
19009 65 72 43 6f 64 65 63 28 50 67 48 64 72 20 2a 70  erCodec(PgHdr *p
1900a 50 67 29 7b 0a 20 20 76 6f 69 64 20 2a 61 44 61  Pg){.  void *aDa
1900b 74 61 20 3d 20 30 3b 0a 20 20 43 4f 44 45 43 32  ta = 0;.  CODEC2
1900c 28 70 50 67 2d 3e 70 50 61 67 65 72 2c 20 70 50  (pPg->pPager, pP
1900d 67 2d 3e 70 44 61 74 61 2c 20 70 50 67 2d 3e 70  g->pData, pPg->p
1900e 67 6e 6f 2c 20 36 2c 20 72 65 74 75 72 6e 20 30  gno, 6, return 0
1900f 2c 20 61 44 61 74 61 29 3b 0a 20 20 72 65 74 75  , aData);.  retu
19010 72 6e 20 61 44 61 74 61 3b 0a 7d 0a 23 65 6e 64  rn aData;.}.#end
19011 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 48 41 53  if /* SQLITE_HAS
19012 5f 43 4f 44 45 43 20 2a 2f 0a 0a 23 65 6e 64 69  _CODEC */..#endi
19013 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
19014 5f 44 49 53 4b 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a  _DISKIO */../***
19015 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
19016 6f 66 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a  of pager.c *****
19017 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19018 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19019 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1901a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1901b 6e 20 66 69 6c 65 20 77 61 6c 2e 63 20 2a 2a 2a  n file wal.c ***
1901c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1901d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1901e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
1901f 2a 20 32 30 31 30 20 46 65 62 72 75 61 72 79 20  * 2010 February 
19020 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  1.**.** The auth
19021 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
19022 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
19023 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
19024 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
19025 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
19026 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
19027 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
19028 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
19029 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
1902a 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
1902b 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1902c 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1902d 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
1902e 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1902f 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
19030 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
19031 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
19032 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19033 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19034 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19035 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19036 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
19037 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d   contains the im
19038 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
19039 61 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f  a write-ahead lo
1903a 67 20 28 57 41 4c 29 20 75 73 65 64 20 69 6e 20  g (WAL) used in 
1903b 0a 2a 2a 20 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64  .** "journal_mod
1903c 65 3d 57 41 4c 22 20 6d 6f 64 65 2e 0a 2a 2a 0a  e=WAL" mode..**.
1903d 2a 2a 20 57 52 49 54 45 2d 41 48 45 41 44 20 4c  ** WRITE-AHEAD L
1903e 4f 47 20 28 57 41 4c 29 20 46 49 4c 45 20 46 4f  OG (WAL) FILE FO
1903f 52 4d 41 54 0a 2a 2a 0a 2a 2a 20 41 20 57 41 4c  RMAT.**.** A WAL
19040 20 66 69 6c 65 20 63 6f 6e 73 69 73 74 73 20 6f   file consists o
19041 66 20 61 20 68 65 61 64 65 72 20 66 6f 6c 6c 6f  f a header follo
19042 77 65 64 20 62 79 20 7a 65 72 6f 20 6f 72 20 6d  wed by zero or m
19043 6f 72 65 20 22 66 72 61 6d 65 73 22 2e 0a 2a 2a  ore "frames"..**
19044 20 45 61 63 68 20 66 72 61 6d 65 20 72 65 63 6f   Each frame reco
19045 72 64 73 20 74 68 65 20 72 65 76 69 73 65 64 20  rds the revised 
19046 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 73 69 6e  content of a sin
19047 67 6c 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68  gle page from th
19048 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69  e.** database fi
19049 6c 65 2e 20 20 41 6c 6c 20 63 68 61 6e 67 65 73  le.  All changes
1904a 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
1904b 20 61 72 65 20 72 65 63 6f 72 64 65 64 20 62 79   are recorded by
1904c 20 77 72 69 74 69 6e 67 0a 2a 2a 20 66 72 61 6d   writing.** fram
1904d 65 73 20 69 6e 74 6f 20 74 68 65 20 57 41 4c 2e  es into the WAL.
1904e 20 20 54 72 61 6e 73 61 63 74 69 6f 6e 73 20 63    Transactions c
1904f 6f 6d 6d 69 74 20 77 68 65 6e 20 61 20 66 72 61  ommit when a fra
19050 6d 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 68  me is written th
19051 61 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61  at.** contains a
19052 20 63 6f 6d 6d 69 74 20 6d 61 72 6b 65 72 2e 20   commit marker. 
19053 20 41 20 73 69 6e 67 6c 65 20 57 41 4c 20 63 61   A single WAL ca
19054 6e 20 61 6e 64 20 75 73 75 61 6c 6c 79 20 64 6f  n and usually do
19055 65 73 20 72 65 63 6f 72 64 20 0a 2a 2a 20 6d 75  es record .** mu
19056 6c 74 69 70 6c 65 20 74 72 61 6e 73 61 63 74 69  ltiple transacti
19057 6f 6e 73 2e 20 20 50 65 72 69 6f 64 69 63 61 6c  ons.  Periodical
19058 6c 79 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ly, the content 
19059 6f 66 20 74 68 65 20 57 41 4c 20 69 73 0a 2a 2a  of the WAL is.**
1905a 20 74 72 61 6e 73 66 65 72 72 65 64 20 62 61 63   transferred bac
1905b 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  k into the datab
1905c 61 73 65 20 66 69 6c 65 20 69 6e 20 61 6e 20 6f  ase file in an o
1905d 70 65 72 61 74 69 6f 6e 20 63 61 6c 6c 65 64 20  peration called 
1905e 61 0a 2a 2a 20 22 63 68 65 63 6b 70 6f 69 6e 74  a.** "checkpoint
1905f 22 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c  "..**.** A singl
19060 65 20 57 41 4c 20 66 69 6c 65 20 63 61 6e 20 62  e WAL file can b
19061 65 20 75 73 65 64 20 6d 75 6c 74 69 70 6c 65 20  e used multiple 
19062 74 69 6d 65 73 2e 20 20 49 6e 20 6f 74 68 65 72  times.  In other
19063 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a 2a 20 57   words, the.** W
19064 41 4c 20 63 61 6e 20 66 69 6c 6c 20 75 70 20 77  AL can fill up w
19065 69 74 68 20 66 72 61 6d 65 73 20 61 6e 64 20 74  ith frames and t
19066 68 65 6e 20 62 65 20 63 68 65 63 6b 70 6f 69 6e  hen be checkpoin
19067 74 65 64 20 61 6e 64 20 74 68 65 6e 20 6e 65 77  ted and then new
19068 0a 2a 2a 20 66 72 61 6d 65 73 20 63 61 6e 20 6f  .** frames can o
19069 76 65 72 77 72 69 74 65 20 74 68 65 20 6f 6c 64  verwrite the old
1906a 20 6f 6e 65 73 2e 20 20 41 20 57 41 4c 20 61 6c   ones.  A WAL al
1906b 77 61 79 73 20 67 72 6f 77 73 20 66 72 6f 6d 20  ways grows from 
1906c 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 74 6f 77  beginning.** tow
1906d 61 72 64 20 74 68 65 20 65 6e 64 2e 20 20 43 68  ard the end.  Ch
1906e 65 63 6b 73 75 6d 73 20 61 6e 64 20 63 6f 75 6e  ecksums and coun
1906f 74 65 72 73 20 61 74 74 61 63 68 65 64 20 74 6f  ters attached to
19070 20 65 61 63 68 20 66 72 61 6d 65 20 61 72 65 0a   each frame are.
19071 2a 2a 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  ** used to deter
19072 6d 69 6e 65 20 77 68 69 63 68 20 66 72 61 6d 65  mine which frame
19073 73 20 77 69 74 68 69 6e 20 74 68 65 20 57 41 4c  s within the WAL
19074 20 61 72 65 20 76 61 6c 69 64 20 61 6e 64 20 77   are valid and w
19075 68 69 63 68 0a 2a 2a 20 61 72 65 20 6c 65 66 74  hich.** are left
19076 6f 76 65 72 73 20 66 72 6f 6d 20 70 72 69 6f 72  overs from prior
19077 20 63 68 65 63 6b 70 6f 69 6e 74 73 2e 0a 2a 2a   checkpoints..**
19078 0a 2a 2a 20 54 68 65 20 57 41 4c 20 68 65 61 64  .** The WAL head
19079 65 72 20 69 73 20 33 32 20 62 79 74 65 73 20 69  er is 32 bytes i
1907a 6e 20 73 69 7a 65 20 61 6e 64 20 63 6f 6e 73 69  n size and consi
1907b 73 74 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  sts of the follo
1907c 77 69 6e 67 20 65 69 67 68 74 0a 2a 2a 20 62 69  wing eight.** bi
1907d 67 2d 65 6e 64 69 61 6e 20 33 32 2d 62 69 74 20  g-endian 32-bit 
1907e 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  unsigned integer
1907f 20 76 61 6c 75 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   values:.**.**  
19080 20 20 20 30 3a 20 4d 61 67 69 63 20 6e 75 6d 62     0: Magic numb
19081 65 72 2e 20 20 30 78 33 37 37 66 30 36 38 32 20  er.  0x377f0682 
19082 6f 72 20 30 78 33 37 37 66 30 36 38 33 0a 2a 2a  or 0x377f0683.**
19083 20 20 20 20 20 34 3a 20 46 69 6c 65 20 66 6f 72       4: File for
19084 6d 61 74 20 76 65 72 73 69 6f 6e 2e 20 20 43 75  mat version.  Cu
19085 72 72 65 6e 74 6c 79 20 33 30 30 37 30 30 30 0a  rrently 3007000.
19086 2a 2a 20 20 20 20 20 38 3a 20 44 61 74 61 62 61  **     8: Databa
19087 73 65 20 70 61 67 65 20 73 69 7a 65 2e 20 20 45  se page size.  E
19088 78 61 6d 70 6c 65 3a 20 31 30 32 34 0a 2a 2a 20  xample: 1024.** 
19089 20 20 20 31 32 3a 20 43 68 65 63 6b 70 6f 69 6e     12: Checkpoin
1908a 74 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65  t sequence numbe
1908b 72 0a 2a 2a 20 20 20 20 31 36 3a 20 53 61 6c 74  r.**    16: Salt
1908c 2d 31 2c 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67  -1, random integ
1908d 65 72 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77  er incremented w
1908e 69 74 68 20 65 61 63 68 20 63 68 65 63 6b 70 6f  ith each checkpo
1908f 69 6e 74 0a 2a 2a 20 20 20 20 32 30 3a 20 53 61  int.**    20: Sa
19090 6c 74 2d 32 2c 20 61 20 64 69 66 66 65 72 65 6e  lt-2, a differen
19091 74 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72  t random integer
19092 20 63 68 61 6e 67 69 6e 67 20 77 69 74 68 20 65   changing with e
19093 61 63 68 20 63 6b 70 74 0a 2a 2a 20 20 20 20 32  ach ckpt.**    2
19094 34 3a 20 43 68 65 63 6b 73 75 6d 2d 31 20 28 66  4: Checksum-1 (f
19095 69 72 73 74 20 70 61 72 74 20 6f 66 20 63 68 65  irst part of che
19096 63 6b 73 75 6d 20 66 6f 72 20 66 69 72 73 74 20  cksum for first 
19097 32 34 20 62 79 74 65 73 20 6f 66 20 68 65 61 64  24 bytes of head
19098 65 72 29 2e 0a 2a 2a 20 20 20 20 32 38 3a 20 43  er)..**    28: C
19099 68 65 63 6b 73 75 6d 2d 32 20 28 73 65 63 6f 6e  hecksum-2 (secon
1909a 64 20 70 61 72 74 20 6f 66 20 63 68 65 63 6b 73  d part of checks
1909b 75 6d 20 66 6f 72 20 66 69 72 73 74 20 32 34 20  um for first 24 
1909c 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 29  bytes of header)
1909d 2e 0a 2a 2a 0a 2a 2a 20 49 6d 6d 65 64 69 61 74  ..**.** Immediat
1909e 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  ely following th
1909f 65 20 77 61 6c 2d 68 65 61 64 65 72 20 61 72 65  e wal-header are
190a0 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 66 72   zero or more fr
190a1 61 6d 65 73 2e 20 45 61 63 68 0a 2a 2a 20 66 72  ames. Each.** fr
190a2 61 6d 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  ame consists of 
190a3 61 20 32 34 2d 62 79 74 65 20 66 72 61 6d 65 2d  a 24-byte frame-
190a4 68 65 61 64 65 72 20 66 6f 6c 6c 6f 77 65 64 20  header followed 
190a5 62 79 20 61 20 3c 70 61 67 65 2d 73 69 7a 65 3e  by a <page-size>
190a6 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 70 61 67   bytes.** of pag
190a7 65 20 64 61 74 61 2e 20 54 68 65 20 66 72 61 6d  e data. The fram
190a8 65 2d 68 65 61 64 65 72 20 69 73 20 73 69 78 20  e-header is six 
190a9 62 69 67 2d 65 6e 64 69 61 6e 20 33 32 2d 62 69  big-endian 32-bi
190aa 74 20 75 6e 73 69 67 6e 65 64 20 0a 2a 2a 20 69  t unsigned .** i
190ab 6e 74 65 67 65 72 20 76 61 6c 75 65 73 2c 20 61  nteger values, a
190ac 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
190ad 20 20 20 20 20 30 3a 20 50 61 67 65 20 6e 75 6d       0: Page num
190ae 62 65 72 2e 0a 2a 2a 20 20 20 20 20 34 3a 20 46  ber..**     4: F
190af 6f 72 20 63 6f 6d 6d 69 74 20 72 65 63 6f 72 64  or commit record
190b0 73 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  s, the size of t
190b1 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67  he database imag
190b2 65 20 69 6e 20 70 61 67 65 73 20 0a 2a 2a 20 20  e in pages .**  
190b3 20 20 20 20 20 20 61 66 74 65 72 20 74 68 65 20        after the 
190b4 63 6f 6d 6d 69 74 2e 20 46 6f 72 20 61 6c 6c 20  commit. For all 
190b5 6f 74 68 65 72 20 72 65 63 6f 72 64 73 2c 20 7a  other records, z
190b6 65 72 6f 2e 0a 2a 2a 20 20 20 20 20 38 3a 20 53  ero..**     8: S
190b7 61 6c 74 2d 31 20 28 63 6f 70 69 65 64 20 66 72  alt-1 (copied fr
190b8 6f 6d 20 74 68 65 20 68 65 61 64 65 72 29 0a 2a  om the header).*
190b9 2a 20 20 20 20 31 32 3a 20 53 61 6c 74 2d 32 20  *    12: Salt-2 
190ba 28 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65  (copied from the
190bb 20 68 65 61 64 65 72 29 0a 2a 2a 20 20 20 20 31   header).**    1
190bc 36 3a 20 43 68 65 63 6b 73 75 6d 2d 31 2e 0a 2a  6: Checksum-1..*
190bd 2a 20 20 20 20 32 30 3a 20 43 68 65 63 6b 73 75  *    20: Checksu
190be 6d 2d 32 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 72 61  m-2..**.** A fra
190bf 6d 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  me is considered
190c0 20 76 61 6c 69 64 20 69 66 20 61 6e 64 20 6f 6e   valid if and on
190c1 6c 79 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ly if the follow
190c2 69 6e 67 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61  ing conditions a
190c3 72 65 0a 2a 2a 20 74 72 75 65 3a 0a 2a 2a 0a 2a  re.** true:.**.*
190c4 2a 20 20 20 20 28 31 29 20 54 68 65 20 73 61 6c  *    (1) The sal
190c5 74 2d 31 20 61 6e 64 20 73 61 6c 74 2d 32 20 76  t-1 and salt-2 v
190c6 61 6c 75 65 73 20 69 6e 20 74 68 65 20 66 72 61  alues in the fra
190c7 6d 65 2d 68 65 61 64 65 72 20 6d 61 74 63 68 0a  me-header match.
190c8 2a 2a 20 20 20 20 20 20 20 20 73 61 6c 74 20 76  **        salt v
190c9 61 6c 75 65 73 20 69 6e 20 74 68 65 20 77 61 6c  alues in the wal
190ca 2d 68 65 61 64 65 72 0a 2a 2a 0a 2a 2a 20 20 20  -header.**.**   
190cb 20 28 32 29 20 54 68 65 20 63 68 65 63 6b 73 75   (2) The checksu
190cc 6d 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20  m values in the 
190cd 66 69 6e 61 6c 20 38 20 62 79 74 65 73 20 6f 66  final 8 bytes of
190ce 20 74 68 65 20 66 72 61 6d 65 2d 68 65 61 64 65   the frame-heade
190cf 72 0a 2a 2a 20 20 20 20 20 20 20 20 65 78 61 63  r.**        exac
190d0 74 6c 79 20 6d 61 74 63 68 20 74 68 65 20 63 68  tly match the ch
190d1 65 63 6b 73 75 6d 20 63 6f 6d 70 75 74 65 64 20  ecksum computed 
190d2 63 6f 6e 73 65 63 75 74 69 76 65 6c 79 20 6f 6e  consecutively on
190d3 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 57   the.**        W
190d4 41 4c 20 68 65 61 64 65 72 20 61 6e 64 20 74 68  AL header and th
190d5 65 20 66 69 72 73 74 20 38 20 62 79 74 65 73 20  e first 8 bytes 
190d6 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  and the content 
190d7 6f 66 20 61 6c 6c 20 66 72 61 6d 65 73 0a 2a 2a  of all frames.**
190d8 20 20 20 20 20 20 20 20 75 70 20 74 6f 20 61 6e          up to an
190d9 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20  d including the 
190da 63 75 72 72 65 6e 74 20 66 72 61 6d 65 2e 0a 2a  current frame..*
190db 2a 0a 2a 2a 20 54 68 65 20 63 68 65 63 6b 73 75  *.** The checksu
190dc 6d 20 69 73 20 63 6f 6d 70 75 74 65 64 20 75 73  m is computed us
190dd 69 6e 67 20 33 32 2d 62 69 74 20 62 69 67 2d 65  ing 32-bit big-e
190de 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 73 20 69  ndian integers i
190df 66 20 74 68 65 0a 2a 2a 20 6d 61 67 69 63 20 6e  f the.** magic n
190e0 75 6d 62 65 72 20 69 6e 20 74 68 65 20 66 69 72  umber in the fir
190e1 73 74 20 34 20 62 79 74 65 73 20 6f 66 20 74 68  st 4 bytes of th
190e2 65 20 57 41 4c 20 69 73 20 30 78 33 37 37 66 30  e WAL is 0x377f0
190e3 36 38 33 20 61 6e 64 20 69 74 0a 2a 2a 20 69 73  683 and it.** is
190e4 20 63 6f 6d 70 75 74 65 64 20 75 73 69 6e 67 20   computed using 
190e5 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 69 66  little-endian if
190e6 20 74 68 65 20 6d 61 67 69 63 20 6e 75 6d 62 65   the magic numbe
190e7 72 20 69 73 20 30 78 33 37 37 66 30 36 38 32 2e  r is 0x377f0682.
190e8 0a 2a 2a 20 54 68 65 20 63 68 65 63 6b 73 75 6d  .** The checksum
190e9 20 76 61 6c 75 65 73 20 61 72 65 20 61 6c 77 61   values are alwa
190ea 79 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ys stored in the
190eb 20 66 72 61 6d 65 20 68 65 61 64 65 72 20 69 6e   frame header in
190ec 20 61 0a 2a 2a 20 62 69 67 2d 65 6e 64 69 61 6e   a.** big-endian
190ed 20 66 6f 72 6d 61 74 20 72 65 67 61 72 64 6c 65   format regardle
190ee 73 73 20 6f 66 20 77 68 69 63 68 20 62 79 74 65  ss of which byte
190ef 20 6f 72 64 65 72 20 69 73 20 75 73 65 64 20 74   order is used t
190f0 6f 20 63 6f 6d 70 75 74 65 0a 2a 2a 20 74 68 65  o compute.** the
190f1 20 63 68 65 63 6b 73 75 6d 2e 20 20 54 68 65 20   checksum.  The 
190f2 63 68 65 63 6b 73 75 6d 20 69 73 20 63 6f 6d 70  checksum is comp
190f3 75 74 65 64 20 62 79 20 69 6e 74 65 72 70 72 65  uted by interpre
190f4 74 69 6e 67 20 74 68 65 20 69 6e 70 75 74 20 61  ting the input a
190f5 73 0a 2a 2a 20 61 6e 20 65 76 65 6e 20 6e 75 6d  s.** an even num
190f6 62 65 72 20 6f 66 20 75 6e 73 69 67 6e 65 64 20  ber of unsigned 
190f7 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 73 3a  32-bit integers:
190f8 20 78 5b 30 5d 20 74 68 72 6f 75 67 68 20 78 5b   x[0] through x[
190f9 4e 5d 2e 20 20 54 68 65 0a 2a 2a 20 61 6c 67 6f  N].  The.** algo
190fa 72 69 74 68 6d 20 75 73 65 64 20 66 6f 72 20 74  rithm used for t
190fb 68 65 20 63 68 65 63 6b 73 75 6d 20 69 73 20 61  he checksum is a
190fc 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 0a 2a  s follows:.** .*
190fd 2a 20 20 20 66 6f 72 20 69 20 66 72 6f 6d 20 30  *   for i from 0
190fe 20 74 6f 20 6e 2d 31 20 73 74 65 70 20 32 3a 0a   to n-1 step 2:.
190ff 2a 2a 20 20 20 20 20 73 30 20 2b 3d 20 78 5b 69  **     s0 += x[i
19100 5d 20 2b 20 73 31 3b 0a 2a 2a 20 20 20 20 20 73  ] + s1;.**     s
19101 31 20 2b 3d 20 78 5b 69 2b 31 5d 20 2b 20 73 30  1 += x[i+1] + s0
19102 3b 0a 2a 2a 20 20 20 65 6e 64 66 6f 72 0a 2a 2a  ;.**   endfor.**
19103 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 73 30  .** Note that s0
19104 20 61 6e 64 20 73 31 20 61 72 65 20 62 6f 74 68   and s1 are both
19105 20 77 65 69 67 68 74 65 64 20 63 68 65 63 6b 73   weighted checks
19106 75 6d 73 20 75 73 69 6e 67 20 66 69 62 6f 6e 61  ums using fibona
19107 63 63 69 20 77 65 69 67 68 74 73 0a 2a 2a 20 69  cci weights.** i
19108 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20  n reverse order 
19109 28 74 68 65 20 6c 61 72 67 65 73 74 20 66 69 62  (the largest fib
1910a 6f 6e 61 63 63 69 20 77 65 69 67 68 74 20 6f 63  onacci weight oc
1910b 63 75 72 73 20 6f 6e 20 74 68 65 20 66 69 72 73  curs on the firs
1910c 74 20 65 6c 65 6d 65 6e 74 0a 2a 2a 20 6f 66 20  t element.** of 
1910d 74 68 65 20 73 65 71 75 65 6e 63 65 20 62 65 69  the sequence bei
1910e 6e 67 20 73 75 6d 6d 65 64 2e 29 20 20 54 68 65  ng summed.)  The
1910f 20 73 31 20 76 61 6c 75 65 20 73 70 61 6e 73 20   s1 value spans 
19110 61 6c 6c 20 33 32 2d 62 69 74 20 0a 2a 2a 20 74  all 32-bit .** t
19111 65 72 6d 73 20 6f 66 20 74 68 65 20 73 65 71 75  erms of the sequ
19112 65 6e 63 65 20 77 68 65 72 65 61 73 20 73 30 20  ence whereas s0 
19113 6f 6d 69 74 73 20 74 68 65 20 66 69 6e 61 6c 20  omits the final 
19114 74 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 61  term..**.** On a
19115 20 63 68 65 63 6b 70 6f 69 6e 74 2c 20 74 68 65   checkpoint, the
19116 20 57 41 4c 20 69 73 20 66 69 72 73 74 20 56 46   WAL is first VF
19117 53 2e 78 53 79 6e 63 2d 65 64 2c 20 74 68 65 6e  S.xSync-ed, then
19118 20 76 61 6c 69 64 20 63 6f 6e 74 65 6e 74 20 6f   valid content o
19119 66 20 74 68 65 0a 2a 2a 20 57 41 4c 20 69 73 20  f the.** WAL is 
1911a 74 72 61 6e 73 66 65 72 72 65 64 20 69 6e 74 6f  transferred into
1911b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74   the database, t
1911c 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
1911d 20 69 73 20 56 46 53 2e 78 53 79 6e 63 2d 65 64   is VFS.xSync-ed
1911e 2e 0a 2a 2a 20 54 68 65 20 56 46 53 2e 78 53 79  ..** The VFS.xSy
1911f 6e 63 20 6f 70 65 72 61 74 69 6f 6e 73 20 73 65  nc operations se
19120 72 76 65 20 61 73 20 77 72 69 74 65 20 62 61 72  rve as write bar
19121 72 69 65 72 73 20 2d 20 61 6c 6c 20 77 72 69 74  riers - all writ
19122 65 73 20 6c 61 75 6e 63 68 65 64 0a 2a 2a 20 62  es launched.** b
19123 65 66 6f 72 65 20 74 68 65 20 78 53 79 6e 63 20  efore the xSync 
19124 6d 75 73 74 20 63 6f 6d 70 6c 65 74 65 20 62 65  must complete be
19125 66 6f 72 65 20 61 6e 79 20 77 72 69 74 65 20 74  fore any write t
19126 68 61 74 20 6c 61 75 6e 63 68 65 73 20 61 66 74  hat launches aft
19127 65 72 20 74 68 65 0a 2a 2a 20 78 53 79 6e 63 20  er the.** xSync 
19128 62 65 67 69 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41 66  begins..**.** Af
19129 74 65 72 20 65 61 63 68 20 63 68 65 63 6b 70 6f  ter each checkpo
1912a 69 6e 74 2c 20 74 68 65 20 73 61 6c 74 2d 31 20  int, the salt-1 
1912b 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 6d 65  value is increme
1912c 6e 74 65 64 20 61 6e 64 20 74 68 65 20 73 61 6c  nted and the sal
1912d 74 2d 32 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20  t-2.** value is 
1912e 72 61 6e 64 6f 6d 69 7a 65 64 2e 20 20 54 68 69  randomized.  Thi
1912f 73 20 70 72 65 76 65 6e 74 73 20 6f 6c 64 20 61  s prevents old a
19130 6e 64 20 6e 65 77 20 66 72 61 6d 65 73 20 69 6e  nd new frames in
19131 20 74 68 65 20 57 41 4c 20 66 72 6f 6d 0a 2a 2a   the WAL from.**
19132 20 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65   being considere
19133 64 20 76 61 6c 69 64 20 61 74 20 74 68 65 20 73  d valid at the s
19134 61 6d 65 20 74 69 6d 65 20 61 6e 64 20 62 65 69  ame time and bei
19135 6e 67 20 63 68 65 63 6b 70 6f 69 6e 74 69 6e 67  ng checkpointing
19136 20 74 6f 67 65 74 68 65 72 0a 2a 2a 20 66 6f 6c   together.** fol
19137 6c 6f 77 69 6e 67 20 61 20 63 72 61 73 68 2e 0a  lowing a crash..
19138 2a 2a 0a 2a 2a 20 52 45 41 44 45 52 20 41 4c 47  **.** READER ALG
19139 4f 52 49 54 48 4d 0a 2a 2a 0a 2a 2a 20 54 6f 20  ORITHM.**.** To 
1913a 72 65 61 64 20 61 20 70 61 67 65 20 66 72 6f 6d  read a page from
1913b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 63   the database (c
1913c 61 6c 6c 20 69 74 20 70 61 67 65 20 6e 75 6d 62  all it page numb
1913d 65 72 20 50 29 2c 20 61 20 72 65 61 64 65 72 0a  er P), a reader.
1913e 2a 2a 20 66 69 72 73 74 20 63 68 65 63 6b 73 20  ** first checks 
1913f 74 68 65 20 57 41 4c 20 74 6f 20 73 65 65 20 69  the WAL to see i
19140 66 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 70 61  f it contains pa
19141 67 65 20 50 2e 20 20 49 66 20 73 6f 2c 20 74 68  ge P.  If so, th
19142 65 6e 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 76  en the.** last v
19143 61 6c 69 64 20 69 6e 73 74 61 6e 63 65 20 6f 66  alid instance of
19144 20 70 61 67 65 20 50 20 74 68 61 74 20 69 73 20   page P that is 
19145 61 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20  a followed by a 
19146 63 6f 6d 6d 69 74 20 66 72 61 6d 65 0a 2a 2a 20  commit frame.** 
19147 6f 72 20 69 73 20 61 20 63 6f 6d 6d 69 74 20 66  or is a commit f
19148 72 61 6d 65 20 69 74 73 65 6c 66 20 62 65 63 6f  rame itself beco
19149 6d 65 73 20 74 68 65 20 76 61 6c 75 65 20 72 65  mes the value re
1914a 61 64 2e 20 20 49 66 20 74 68 65 20 57 41 4c 0a  ad.  If the WAL.
1914b 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 63  ** contains no c
1914c 6f 70 69 65 73 20 6f 66 20 70 61 67 65 20 50 20  opies of page P 
1914d 74 68 61 74 20 61 72 65 20 76 61 6c 69 64 20 61  that are valid a
1914e 6e 64 20 77 68 69 63 68 20 61 72 65 20 61 20 63  nd which are a c
1914f 6f 6d 6d 69 74 0a 2a 2a 20 66 72 61 6d 65 20 6f  ommit.** frame o
19150 72 20 61 72 65 20 66 6f 6c 6c 6f 77 65 64 20 62  r are followed b
19151 79 20 61 20 63 6f 6d 6d 69 74 20 66 72 61 6d 65  y a commit frame
19152 2c 20 74 68 65 6e 20 70 61 67 65 20 50 20 69 73  , then page P is
19153 20 72 65 61 64 20 66 72 6f 6d 0a 2a 2a 20 74 68   read from.** th
19154 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
19155 0a 2a 2a 0a 2a 2a 20 54 6f 20 73 74 61 72 74 20  .**.** To start 
19156 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69  a read transacti
19157 6f 6e 2c 20 74 68 65 20 72 65 61 64 65 72 20 72  on, the reader r
19158 65 63 6f 72 64 73 20 74 68 65 20 69 6e 64 65 78  ecords the index
19159 20 6f 66 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20   of the last.** 
1915a 76 61 6c 69 64 20 66 72 61 6d 65 20 69 6e 20 74  valid frame in t
1915b 68 65 20 57 41 4c 2e 20 20 54 68 65 20 72 65 61  he WAL.  The rea
1915c 64 65 72 20 75 73 65 73 20 74 68 69 73 20 72 65  der uses this re
1915d 63 6f 72 64 65 64 20 22 6d 78 46 72 61 6d 65 22  corded "mxFrame"
1915e 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 61 6c   value.** for al
1915f 6c 20 73 75 62 73 65 71 75 65 6e 74 20 72 65 61  l subsequent rea
19160 64 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 4e  d operations.  N
19161 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  ew transactions 
19162 63 61 6e 20 62 65 20 61 70 70 65 6e 64 65 64 0a  can be appended.
19163 2a 2a 20 74 6f 20 74 68 65 20 57 41 4c 2c 20 62  ** to the WAL, b
19164 75 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68  ut as long as th
19165 65 20 72 65 61 64 65 72 20 75 73 65 73 20 69 74  e reader uses it
19166 73 20 6f 72 69 67 69 6e 61 6c 20 6d 78 46 72 61  s original mxFra
19167 6d 65 20 76 61 6c 75 65 0a 2a 2a 20 61 6e 64 20  me value.** and 
19168 69 67 6e 6f 72 65 73 20 74 68 65 20 6e 65 77 6c  ignores the newl
19169 79 20 61 70 70 65 6e 64 65 64 20 63 6f 6e 74 65  y appended conte
1916a 6e 74 2c 20 69 74 20 77 69 6c 6c 20 73 65 65 20  nt, it will see 
1916b 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 73 6e 61  a consistent sna
1916c 70 73 68 6f 74 0a 2a 2a 20 6f 66 20 74 68 65 20  pshot.** of the 
1916d 64 61 74 61 62 61 73 65 20 66 72 6f 6d 20 61 20  database from a 
1916e 73 69 6e 67 6c 65 20 70 6f 69 6e 74 20 69 6e 20  single point in 
1916f 74 69 6d 65 2e 20 20 54 68 69 73 20 74 65 63 68  time.  This tech
19170 6e 69 71 75 65 20 61 6c 6c 6f 77 73 0a 2a 2a 20  nique allows.** 
19171 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 63 75 72 72  multiple concurr
19172 65 6e 74 20 72 65 61 64 65 72 73 20 74 6f 20 76  ent readers to v
19173 69 65 77 20 64 69 66 66 65 72 65 6e 74 20 76 65  iew different ve
19174 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 61  rsions of the da
19175 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 74 65 6e  tabase.** conten
19176 74 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79  t simultaneously
19177 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 61 64  ..**.** The read
19178 65 72 20 61 6c 67 6f 72 69 74 68 6d 20 69 6e 20  er algorithm in 
19179 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 72  the previous par
1917a 61 67 72 61 70 68 73 20 77 6f 72 6b 73 20 63 6f  agraphs works co
1917b 72 72 65 63 74 6c 79 2c 20 62 75 74 20 0a 2a 2a  rrectly, but .**
1917c 20 62 65 63 61 75 73 65 20 66 72 61 6d 65 73 20   because frames 
1917d 66 6f 72 20 70 61 67 65 20 50 20 63 61 6e 20 61  for page P can a
1917e 70 70 65 61 72 20 61 6e 79 77 68 65 72 65 20 77  ppear anywhere w
1917f 69 74 68 69 6e 20 74 68 65 20 57 41 4c 2c 20 74  ithin the WAL, t
19180 68 65 0a 2a 2a 20 72 65 61 64 65 72 20 68 61 73  he.** reader has
19181 20 74 6f 20 73 63 61 6e 20 74 68 65 20 65 6e 74   to scan the ent
19182 69 72 65 20 57 41 4c 20 6c 6f 6f 6b 69 6e 67 20  ire WAL looking 
19183 66 6f 72 20 70 61 67 65 20 50 20 66 72 61 6d 65  for page P frame
19184 73 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 57 41  s.  If the.** WA
19185 4c 20 69 73 20 6c 61 72 67 65 20 28 6d 75 6c 74  L is large (mult
19186 69 70 6c 65 20 6d 65 67 61 62 79 74 65 73 20 69  iple megabytes i
19187 73 20 74 79 70 69 63 61 6c 29 20 74 68 61 74 20  s typical) that 
19188 73 63 61 6e 20 63 61 6e 20 62 65 20 73 6c 6f 77  scan can be slow
19189 2c 0a 2a 2a 20 61 6e 64 20 72 65 61 64 20 70 65  ,.** and read pe
1918a 72 66 6f 72 6d 61 6e 63 65 20 73 75 66 66 65 72  rformance suffer
1918b 73 2e 20 20 54 6f 20 6f 76 65 72 63 6f 6d 65 20  s.  To overcome 
1918c 74 68 69 73 20 70 72 6f 62 6c 65 6d 2c 20 61 20  this problem, a 
1918d 73 65 70 61 72 61 74 65 0a 2a 2a 20 64 61 74 61  separate.** data
1918e 20 73 74 72 75 63 74 75 72 65 20 63 61 6c 6c 65   structure calle
1918f 64 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20  d the wal-index 
19190 69 73 20 6d 61 69 6e 74 61 69 6e 65 64 20 74 6f  is maintained to
19191 20 65 78 70 65 64 69 74 65 20 74 68 65 0a 2a 2a   expedite the.**
19192 20 73 65 61 72 63 68 20 66 6f 72 20 66 72 61 6d   search for fram
19193 65 73 20 6f 66 20 61 20 70 61 72 74 69 63 75 6c  es of a particul
19194 61 72 20 70 61 67 65 2e 0a 2a 2a 20 0a 2a 2a 20  ar page..** .** 
19195 57 41 4c 2d 49 4e 44 45 58 20 46 4f 52 4d 41 54  WAL-INDEX FORMAT
19196 0a 2a 2a 0a 2a 2a 20 43 6f 6e 63 65 70 74 75 61  .**.** Conceptua
19197 6c 6c 79 2c 20 74 68 65 20 77 61 6c 2d 69 6e 64  lly, the wal-ind
19198 65 78 20 69 73 20 73 68 61 72 65 64 20 6d 65 6d  ex is shared mem
19199 6f 72 79 2c 20 74 68 6f 75 67 68 20 56 46 53 20  ory, though VFS 
1919a 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a  implementations.
1919b 2a 2a 20 6d 69 67 68 74 20 63 68 6f 6f 73 65 20  ** might choose 
1919c 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1919d 20 77 61 6c 2d 69 6e 64 65 78 20 75 73 69 6e 67   wal-index using
1919e 20 61 20 6d 6d 61 70 70 65 64 20 66 69 6c 65 2e   a mmapped file.
1919f 20 20 42 65 63 61 75 73 65 0a 2a 2a 20 74 68 65    Because.** the
191a0 20 77 61 6c 2d 69 6e 64 65 78 20 69 73 20 73 68   wal-index is sh
191a1 61 72 65 64 20 6d 65 6d 6f 72 79 2c 20 53 51 4c  ared memory, SQL
191a2 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70  ite does not sup
191a3 70 6f 72 74 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64  port journal_mod
191a4 65 3d 57 41 4c 20 0a 2a 2a 20 6f 6e 20 61 20 6e  e=WAL .** on a n
191a5 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 73 74 65  etwork filesyste
191a6 6d 2e 20 20 41 6c 6c 20 75 73 65 72 73 20 6f 66  m.  All users of
191a7 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75   the database mu
191a8 73 74 20 62 65 20 61 62 6c 65 20 74 6f 0a 2a 2a  st be able to.**
191a9 20 73 68 61 72 65 20 6d 65 6d 6f 72 79 2e 0a 2a   share memory..*
191aa 2a 0a 2a 2a 20 54 68 65 20 77 61 6c 2d 69 6e 64  *.** The wal-ind
191ab 65 78 20 69 73 20 74 72 61 6e 73 69 65 6e 74 2e  ex is transient.
191ac 20 20 41 66 74 65 72 20 61 20 63 72 61 73 68 2c    After a crash,
191ad 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 63   the wal-index c
191ae 61 6e 20 28 61 6e 64 20 73 68 6f 75 6c 64 0a 2a  an (and should.*
191af 2a 20 62 65 29 20 72 65 63 6f 6e 73 74 72 75 63  * be) reconstruc
191b0 74 65 64 20 66 72 6f 6d 20 74 68 65 20 6f 72 69  ted from the ori
191b1 67 69 6e 61 6c 20 57 41 4c 20 66 69 6c 65 2e 20  ginal WAL file. 
191b2 20 49 6e 20 66 61 63 74 2c 20 74 68 65 20 56 46   In fact, the VF
191b3 53 20 69 73 20 72 65 71 75 69 72 65 64 0a 2a 2a  S is required.**
191b4 20 74 6f 20 65 69 74 68 65 72 20 74 72 75 6e 63   to either trunc
191b5 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20  ate or zero the 
191b6 68 65 61 64 65 72 20 6f 66 20 74 68 65 20 77 61  header of the wa
191b7 6c 2d 69 6e 64 65 78 20 77 68 65 6e 20 74 68 65  l-index when the
191b8 20 6c 61 73 74 0a 2a 2a 20 63 6f 6e 6e 65 63 74   last.** connect
191b9 69 6f 6e 20 74 6f 20 69 74 20 63 6c 6f 73 65 73  ion to it closes
191ba 2e 20 20 42 65 63 61 75 73 65 20 74 68 65 20 77  .  Because the w
191bb 61 6c 2d 69 6e 64 65 78 20 69 73 20 74 72 61 6e  al-index is tran
191bc 73 69 65 6e 74 2c 20 69 74 20 63 61 6e 0a 2a 2a  sient, it can.**
191bd 20 75 73 65 20 61 6e 20 61 72 63 68 69 74 65 63   use an architec
191be 74 75 72 65 2d 73 70 65 63 69 66 69 63 20 66 6f  ture-specific fo
191bf 72 6d 61 74 3b 20 69 74 20 64 6f 65 73 20 6e 6f  rmat; it does no
191c0 74 20 68 61 76 65 20 74 6f 20 62 65 20 63 72 6f  t have to be cro
191c1 73 73 2d 70 6c 61 74 66 6f 72 6d 2e 0a 2a 2a 20  ss-platform..** 
191c2 48 65 6e 63 65 2c 20 75 6e 6c 69 6b 65 20 74 68  Hence, unlike th
191c3 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 57  e database and W
191c4 41 4c 20 66 69 6c 65 20 66 6f 72 6d 61 74 73 20  AL file formats 
191c5 77 68 69 63 68 20 73 74 6f 72 65 20 61 6c 6c 20  which store all 
191c6 76 61 6c 75 65 73 0a 2a 2a 20 61 73 20 62 69 67  values.** as big
191c7 20 65 6e 64 69 61 6e 2c 20 74 68 65 20 77 61 6c   endian, the wal
191c8 2d 69 6e 64 65 78 20 63 61 6e 20 73 74 6f 72 65  -index can store
191c9 20 6d 75 6c 74 69 2d 62 79 74 65 20 76 61 6c 75   multi-byte valu
191ca 65 73 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65  es in the native
191cb 0a 2a 2a 20 62 79 74 65 20 6f 72 64 65 72 20 6f  .** byte order o
191cc 66 20 74 68 65 20 68 6f 73 74 20 63 6f 6d 70 75  f the host compu
191cd 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ter..**.** The p
191ce 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 77 61  urpose of the wa
191cf 6c 2d 69 6e 64 65 78 20 69 73 20 74 6f 20 61 6e  l-index is to an
191d0 73 77 65 72 20 74 68 69 73 20 71 75 65 73 74 69  swer this questi
191d1 6f 6e 20 71 75 69 63 6b 6c 79 3a 20 20 47 69 76  on quickly:  Giv
191d2 65 6e 0a 2a 2a 20 61 20 70 61 67 65 20 6e 75 6d  en.** a page num
191d3 62 65 72 20 50 20 61 6e 64 20 61 20 6d 61 78 69  ber P and a maxi
191d4 6d 75 6d 20 66 72 61 6d 65 20 69 6e 64 65 78 20  mum frame index 
191d5 4d 2c 20 72 65 74 75 72 6e 20 74 68 65 20 69 6e  M, return the in
191d6 64 65 78 20 6f 66 20 74 68 65 20 0a 2a 2a 20 6c  dex of the .** l
191d7 61 73 74 20 66 72 61 6d 65 20 69 6e 20 74 68 65  ast frame in the
191d8 20 77 61 6c 20 62 65 66 6f 72 65 20 66 72 61 6d   wal before fram
191d9 65 20 4d 20 66 6f 72 20 70 61 67 65 20 50 20 69  e M for page P i
191da 6e 20 74 68 65 20 57 41 4c 2c 20 6f 72 20 72 65  n the WAL, or re
191db 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 20 69 66 20  turn.** NULL if 
191dc 74 68 65 72 65 20 61 72 65 20 6e 6f 20 66 72 61  there are no fra
191dd 6d 65 73 20 66 6f 72 20 70 61 67 65 20 50 20 69  mes for page P i
191de 6e 20 74 68 65 20 57 41 4c 20 70 72 69 6f 72 20  n the WAL prior 
191df 74 6f 20 4d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  to M..**.** The 
191e0 77 61 6c 2d 69 6e 64 65 78 20 63 6f 6e 73 69 73  wal-index consis
191e1 74 73 20 6f 66 20 61 20 68 65 61 64 65 72 20 72  ts of a header r
191e2 65 67 69 6f 6e 2c 20 66 6f 6c 6c 6f 77 65 64 20  egion, followed 
191e3 62 79 20 61 6e 20 6f 6e 65 20 6f 72 0a 2a 2a 20  by an one or.** 
191e4 6d 6f 72 65 20 69 6e 64 65 78 20 62 6c 6f 63 6b  more index block
191e5 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77  s.  .**.** The w
191e6 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20  al-index header 
191e7 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 6f 74  contains the tot
191e8 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 61  al number of fra
191e9 6d 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 57  mes within the W
191ea 41 4c 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 78 46  AL.** in the mxF
191eb 72 61 6d 65 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a  rame field..**.*
191ec 2a 20 45 61 63 68 20 69 6e 64 65 78 20 62 6c 6f  * Each index blo
191ed 63 6b 20 65 78 63 65 70 74 20 66 6f 72 20 74 68  ck except for th
191ee 65 20 66 69 72 73 74 20 63 6f 6e 74 61 69 6e 73  e first contains
191ef 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20   information on 
191f0 0a 2a 2a 20 48 41 53 48 54 41 42 4c 45 5f 4e 50  .** HASHTABLE_NP
191f1 41 47 45 20 66 72 61 6d 65 73 2e 20 54 68 65 20  AGE frames. The 
191f2 66 69 72 73 74 20 69 6e 64 65 78 20 62 6c 6f 63  first index bloc
191f3 6b 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72  k contains infor
191f4 6d 61 74 69 6f 6e 20 6f 6e 0a 2a 2a 20 48 41 53  mation on.** HAS
191f5 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45  HTABLE_NPAGE_ONE
191f6 20 66 72 61 6d 65 73 2e 20 54 68 65 20 76 61 6c   frames. The val
191f7 75 65 73 20 6f 66 20 48 41 53 48 54 41 42 4c 45  ues of HASHTABLE
191f8 5f 4e 50 41 47 45 5f 4f 4e 45 20 61 6e 64 20 0a  _NPAGE_ONE and .
191f9 2a 2a 20 48 41 53 48 54 41 42 4c 45 5f 4e 50 41  ** HASHTABLE_NPA
191fa 47 45 20 61 72 65 20 73 65 6c 65 63 74 65 64 20  GE are selected 
191fb 73 6f 20 74 68 61 74 20 74 6f 67 65 74 68 65 72  so that together
191fc 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 68   the wal-index h
191fd 65 61 64 65 72 20 61 6e 64 0a 2a 2a 20 66 69 72  eader and.** fir
191fe 73 74 20 69 6e 64 65 78 20 62 6c 6f 63 6b 20 61  st index block a
191ff 72 65 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65  re the same size
19200 20 61 73 20 61 6c 6c 20 6f 74 68 65 72 20 69 6e   as all other in
19201 64 65 78 20 62 6c 6f 63 6b 73 20 69 6e 20 74 68  dex blocks in th
19202 65 0a 2a 2a 20 77 61 6c 2d 69 6e 64 65 78 2e 0a  e.** wal-index..
19203 2a 2a 0a 2a 2a 20 45 61 63 68 20 69 6e 64 65 78  **.** Each index
19204 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20   block contains 
19205 74 77 6f 20 73 65 63 74 69 6f 6e 73 2c 20 61 20  two sections, a 
19206 70 61 67 65 2d 6d 61 70 70 69 6e 67 20 74 68 61  page-mapping tha
19207 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a  t contains the.*
19208 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  * database page 
19209 6e 75 6d 62 65 72 20 61 73 73 6f 63 69 61 74 65  number associate
1920a 64 20 77 69 74 68 20 65 61 63 68 20 77 61 6c 20  d with each wal 
1920b 66 72 61 6d 65 2c 20 61 6e 64 20 61 20 68 61 73  frame, and a has
1920c 68 2d 74 61 62 6c 65 20 0a 2a 2a 20 74 68 61 74  h-table .** that
1920d 20 61 6c 6c 6f 77 73 20 72 65 61 64 65 72 73 20   allows readers 
1920e 74 6f 20 71 75 65 72 79 20 61 6e 20 69 6e 64 65  to query an inde
1920f 78 20 62 6c 6f 63 6b 20 66 6f 72 20 61 20 73 70  x block for a sp
19210 65 63 69 66 69 63 20 70 61 67 65 20 6e 75 6d 62  ecific page numb
19211 65 72 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65 2d  er..** The page-
19212 6d 61 70 70 69 6e 67 20 69 73 20 61 6e 20 61 72  mapping is an ar
19213 72 61 79 20 6f 66 20 48 41 53 48 54 41 42 4c 45  ray of HASHTABLE
19214 5f 4e 50 41 47 45 20 28 6f 72 20 48 41 53 48 54  _NPAGE (or HASHT
19215 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45 0a 2a  ABLE_NPAGE_ONE.*
19216 2a 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  * for the first 
19217 69 6e 64 65 78 20 62 6c 6f 63 6b 29 20 33 32 2d  index block) 32-
19218 62 69 74 20 70 61 67 65 20 6e 75 6d 62 65 72 73  bit page numbers
19219 2e 20 54 68 65 20 66 69 72 73 74 20 65 6e 74 72  . The first entr
1921a 79 20 69 6e 20 74 68 65 20 0a 2a 2a 20 66 69 72  y in the .** fir
1921b 73 74 20 69 6e 64 65 78 2d 62 6c 6f 63 6b 20 63  st index-block c
1921c 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74 61  ontains the data
1921d 62 61 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72  base page number
1921e 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
1921f 6f 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 66  o the.** first f
19220 72 61 6d 65 20 69 6e 20 74 68 65 20 57 41 4c 20  rame in the WAL 
19221 66 69 6c 65 2e 20 54 68 65 20 66 69 72 73 74 20  file. The first 
19222 65 6e 74 72 79 20 69 6e 20 74 68 65 20 73 65 63  entry in the sec
19223 6f 6e 64 20 69 6e 64 65 78 20 62 6c 6f 63 6b 0a  ond index block.
19224 2a 2a 20 69 6e 20 74 68 65 20 57 41 4c 20 66 69  ** in the WAL fi
19225 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  le corresponds t
19226 6f 20 74 68 65 20 28 48 41 53 48 54 41 42 4c 45  o the (HASHTABLE
19227 5f 4e 50 41 47 45 5f 4f 4e 45 2b 31 29 74 68 20  _NPAGE_ONE+1)th 
19228 66 72 61 6d 65 20 69 6e 0a 2a 2a 20 74 68 65 20  frame in.** the 
19229 6c 6f 67 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a  log, and so on..
1922a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20 69  **.** The last i
1922b 6e 64 65 78 20 62 6c 6f 63 6b 20 69 6e 20 61 20  ndex block in a 
1922c 77 61 6c 2d 69 6e 64 65 78 20 75 73 75 61 6c 6c  wal-index usuall
1922d 79 20 63 6f 6e 74 61 69 6e 73 20 6c 65 73 73 20  y contains less 
1922e 74 68 61 6e 20 74 68 65 20 66 75 6c 6c 0a 2a 2a  than the full.**
1922f 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 48   complement of H
19230 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 20 28  ASHTABLE_NPAGE (
19231 6f 72 20 48 41 53 48 54 41 42 4c 45 5f 4e 50 41  or HASHTABLE_NPA
19232 47 45 5f 4f 4e 45 29 20 70 61 67 65 2d 6e 75 6d  GE_ONE) page-num
19233 62 65 72 73 2c 0a 2a 2a 20 64 65 70 65 6e 64 69  bers,.** dependi
19234 6e 67 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e  ng on the conten
19235 74 73 20 6f 66 20 74 68 65 20 57 41 4c 20 66 69  ts of the WAL fi
19236 6c 65 2e 20 54 68 69 73 20 64 6f 65 73 20 6e 6f  le. This does no
19237 74 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a 20  t change the.** 
19238 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f  allocated size o
19239 66 20 74 68 65 20 70 61 67 65 2d 6d 61 70 70 69  f the page-mappi
1923a 6e 67 20 61 72 72 61 79 20 2d 20 74 68 65 20 70  ng array - the p
1923b 61 67 65 2d 6d 61 70 70 69 6e 67 20 61 72 72 61  age-mapping arra
1923c 79 20 6d 65 72 65 6c 79 0a 2a 2a 20 63 6f 6e 74  y merely.** cont
1923d 61 69 6e 73 20 75 6e 75 73 65 64 20 65 6e 74 72  ains unused entr
1923e 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 6e 20  ies..**.** Even 
1923f 77 69 74 68 6f 75 74 20 75 73 69 6e 67 20 74 68  without using th
19240 65 20 68 61 73 68 20 74 61 62 6c 65 2c 20 74 68  e hash table, th
19241 65 20 6c 61 73 74 20 66 72 61 6d 65 20 66 6f 72  e last frame for
19242 20 70 61 67 65 20 50 0a 2a 2a 20 63 61 6e 20 62   page P.** can b
19243 65 20 66 6f 75 6e 64 20 62 79 20 73 63 61 6e 6e  e found by scann
19244 69 6e 67 20 74 68 65 20 70 61 67 65 2d 6d 61 70  ing the page-map
19245 70 69 6e 67 20 73 65 63 74 69 6f 6e 73 20 6f 66  ping sections of
19246 20 65 61 63 68 20 69 6e 64 65 78 20 62 6c 6f 63   each index bloc
19247 6b 0a 2a 2a 20 73 74 61 72 74 69 6e 67 20 77 69  k.** starting wi
19248 74 68 20 74 68 65 20 6c 61 73 74 20 69 6e 64 65  th the last inde
19249 78 20 62 6c 6f 63 6b 20 61 6e 64 20 6d 6f 76 69  x block and movi
1924a 6e 67 20 74 6f 77 61 72 64 20 74 68 65 20 66 69  ng toward the fi
1924b 72 73 74 2c 20 61 6e 64 0a 2a 2a 20 77 69 74 68  rst, and.** with
1924c 69 6e 20 65 61 63 68 20 69 6e 64 65 78 20 62 6c  in each index bl
1924d 6f 63 6b 2c 20 73 74 61 72 74 69 6e 67 20 61 74  ock, starting at
1924e 20 74 68 65 20 65 6e 64 20 61 6e 64 20 6d 6f 76   the end and mov
1924f 69 6e 67 20 74 6f 77 61 72 64 20 74 68 65 0a 2a  ing toward the.*
19250 2a 20 62 65 67 69 6e 6e 69 6e 67 2e 20 20 54 68  * beginning.  Th
19251 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 74 68  e first entry th
19252 61 74 20 65 71 75 61 6c 73 20 50 20 63 6f 72 72  at equals P corr
19253 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 66  esponds to the f
19254 72 61 6d 65 0a 2a 2a 20 68 6f 6c 64 69 6e 67 20  rame.** holding 
19255 74 68 65 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20  the content for 
19256 74 68 61 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a  that page..**.**
19257 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   The hash table 
19258 63 6f 6e 73 69 73 74 73 20 6f 66 20 48 41 53 48  consists of HASH
19259 54 41 42 4c 45 5f 4e 53 4c 4f 54 20 31 36 2d 62  TABLE_NSLOT 16-b
1925a 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65  it unsigned inte
1925b 67 65 72 73 2e 0a 2a 2a 20 48 41 53 48 54 41 42  gers..** HASHTAB
1925c 4c 45 5f 4e 53 4c 4f 54 20 3d 20 32 2a 48 41 53  LE_NSLOT = 2*HAS
1925d 48 54 41 42 4c 45 5f 4e 50 41 47 45 2c 20 61 6e  HTABLE_NPAGE, an
1925e 64 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20 65  d there is one e
1925f 6e 74 72 79 20 69 6e 20 74 68 65 0a 2a 2a 20 68  ntry in the.** h
19260 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 65 61  ash table for ea
19261 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69  ch page number i
19262 6e 20 74 68 65 20 6d 61 70 70 69 6e 67 20 73 65  n the mapping se
19263 63 74 69 6f 6e 2c 20 73 6f 20 74 68 65 20 68 61  ction, so the ha
19264 73 68 20 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20  sh .** table is 
19265 6e 65 76 65 72 20 6d 6f 72 65 20 74 68 61 6e 20  never more than 
19266 68 61 6c 66 20 66 75 6c 6c 2e 20 20 54 68 65 20  half full.  The 
19267 65 78 70 65 63 74 65 64 20 6e 75 6d 62 65 72 20  expected number 
19268 6f 66 20 63 6f 6c 6c 69 73 69 6f 6e 73 20 0a 2a  of collisions .*
19269 2a 20 70 72 69 6f 72 20 74 6f 20 66 69 6e 64 69  * prior to findi
1926a 6e 67 20 61 20 6d 61 74 63 68 20 69 73 20 31 2e  ng a match is 1.
1926b 20 20 45 61 63 68 20 65 6e 74 72 79 20 6f 66 20    Each entry of 
1926c 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69  the hash table i
1926d 73 20 61 6e 0a 2a 2a 20 31 2d 62 61 73 65 64 20  s an.** 1-based 
1926e 69 6e 64 65 78 20 6f 66 20 61 6e 20 65 6e 74 72  index of an entr
1926f 79 20 69 6e 20 74 68 65 20 6d 61 70 70 69 6e 67  y in the mapping
19270 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20   section of the 
19271 73 61 6d 65 0a 2a 2a 20 69 6e 64 65 78 20 62 6c  same.** index bl
19272 6f 63 6b 2e 20 20 20 4c 65 74 20 4b 20 62 65 20  ock.   Let K be 
19273 74 68 65 20 31 2d 62 61 73 65 64 20 69 6e 64 65  the 1-based inde
19274 78 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74  x of the largest
19275 20 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 74 68 65   entry in.** the
19276 20 6d 61 70 70 69 6e 67 20 73 65 63 74 69 6f 6e   mapping section
19277 2e 20 20 28 46 6f 72 20 69 6e 64 65 78 20 62 6c  .  (For index bl
19278 6f 63 6b 73 20 6f 74 68 65 72 20 74 68 61 6e 20  ocks other than 
19279 74 68 65 20 6c 61 73 74 2c 20 4b 20 77 69 6c 6c  the last, K will
1927a 0a 2a 2a 20 61 6c 77 61 79 73 20 62 65 20 65 78  .** always be ex
1927b 61 63 74 6c 79 20 48 41 53 48 54 41 42 4c 45 5f  actly HASHTABLE_
1927c 4e 50 41 47 45 20 28 34 30 39 36 29 20 61 6e 64  NPAGE (4096) and
1927d 20 66 6f 72 20 74 68 65 20 6c 61 73 74 20 69 6e   for the last in
1927e 64 65 78 20 62 6c 6f 63 6b 0a 2a 2a 20 4b 20 77  dex block.** K w
1927f 69 6c 6c 20 62 65 20 28 6d 78 46 72 61 6d 65 25  ill be (mxFrame%
19280 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 29  HASHTABLE_NPAGE)
19281 2e 29 20 20 55 6e 75 73 65 64 20 73 6c 6f 74 73  .)  Unused slots
19282 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 62   of the hash tab
19283 6c 65 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 20  le.** contain a 
19284 76 61 6c 75 65 20 6f 66 20 30 2e 0a 2a 2a 0a 2a  value of 0..**.*
19285 2a 20 54 6f 20 6c 6f 6f 6b 20 66 6f 72 20 70 61  * To look for pa
19286 67 65 20 50 20 69 6e 20 74 68 65 20 68 61 73 68  ge P in the hash
19287 20 74 61 62 6c 65 2c 20 66 69 72 73 74 20 63 6f   table, first co
19288 6d 70 75 74 65 20 61 20 68 61 73 68 20 69 4b 65  mpute a hash iKe
19289 79 20 6f 6e 0a 2a 2a 20 50 20 61 73 20 66 6f 6c  y on.** P as fol
1928a 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lows:.**.**     
1928b 20 69 4b 65 79 20 3d 20 28 50 20 2a 20 33 38 33   iKey = (P * 383
1928c 29 20 25 20 48 41 53 48 54 41 42 4c 45 5f 4e 53  ) % HASHTABLE_NS
1928d 4c 4f 54 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 73  LOT.**.** Then s
1928e 74 61 72 74 20 73 63 61 6e 6e 69 6e 67 20 65 6e  tart scanning en
1928f 74 72 69 65 73 20 6f 66 20 74 68 65 20 68 61 73  tries of the has
19290 68 20 74 61 62 6c 65 2c 20 73 74 61 72 74 69 6e  h table, startin
19291 67 20 77 69 74 68 20 69 4b 65 79 0a 2a 2a 20 28  g with iKey.** (
19292 77 72 61 70 70 69 6e 67 20 61 72 6f 75 6e 64 20  wrapping around 
19293 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  to the beginning
19294 20 77 68 65 6e 20 74 68 65 20 65 6e 64 20 6f 66   when the end of
19295 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
19296 69 73 0a 2a 2a 20 72 65 61 63 68 65 64 29 20 75  is.** reached) u
19297 6e 74 69 6c 20 61 6e 20 75 6e 75 73 65 64 20 68  ntil an unused h
19298 61 73 68 20 73 6c 6f 74 20 69 73 20 66 6f 75 6e  ash slot is foun
19299 64 2e 20 4c 65 74 20 74 68 65 20 66 69 72 73 74  d. Let the first
1929a 20 75 6e 75 73 65 64 20 73 6c 6f 74 0a 2a 2a 20   unused slot.** 
1929b 62 65 20 61 74 20 69 6e 64 65 78 20 69 55 6e 75  be at index iUnu
1929c 73 65 64 2e 20 20 28 69 55 6e 75 73 65 64 20 6d  sed.  (iUnused m
1929d 69 67 68 74 20 62 65 20 6c 65 73 73 20 74 68 61  ight be less tha
1929e 6e 20 69 4b 65 79 20 69 66 20 74 68 65 72 65 20  n iKey if there 
1929f 77 61 73 0a 2a 2a 20 77 72 61 70 2d 61 72 6f 75  was.** wrap-arou
192a0 6e 64 2e 29 20 42 65 63 61 75 73 65 20 74 68 65  nd.) Because the
192a1 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 6e   hash table is n
192a2 65 76 65 72 20 6d 6f 72 65 20 74 68 61 6e 20 68  ever more than h
192a3 61 6c 66 20 66 75 6c 6c 2c 0a 2a 2a 20 74 68 65  alf full,.** the
192a4 20 73 65 61 72 63 68 20 69 73 20 67 75 61 72 61   search is guara
192a5 6e 74 65 65 64 20 74 6f 20 65 76 65 6e 74 75 61  nteed to eventua
192a6 6c 6c 79 20 68 69 74 20 61 6e 20 75 6e 75 73 65  lly hit an unuse
192a7 64 20 65 6e 74 72 79 2e 20 20 4c 65 74 20 0a 2a  d entry.  Let .*
192a8 2a 20 69 4d 61 78 20 62 65 20 74 68 65 20 76 61  * iMax be the va
192a9 6c 75 65 20 62 65 74 77 65 65 6e 20 69 4b 65 79  lue between iKey
192aa 20 61 6e 64 20 69 55 6e 75 73 65 64 2c 20 63 6c   and iUnused, cl
192ab 6f 73 65 73 74 20 74 6f 20 69 55 6e 75 73 65 64  osest to iUnused
192ac 2c 0a 2a 2a 20 77 68 65 72 65 20 61 48 61 73 68  ,.** where aHash
192ad 5b 69 4d 61 78 5d 3d 3d 50 2e 20 20 49 66 20 74  [iMax]==P.  If t
192ae 68 65 72 65 20 69 73 20 6e 6f 20 69 4d 61 78 20  here is no iMax 
192af 65 6e 74 72 79 20 28 69 66 20 74 68 65 72 65 20  entry (if there 
192b0 65 78 69 73 74 73 0a 2a 2a 20 6e 6f 20 68 61 73  exists.** no has
192b1 68 20 73 6c 6f 74 20 73 75 63 68 20 74 68 61 74  h slot such that
192b2 20 61 48 61 73 68 5b 69 5d 3d 3d 70 29 20 74 68   aHash[i]==p) th
192b3 65 6e 20 70 61 67 65 20 50 20 69 73 20 6e 6f 74  en page P is not
192b4 20 69 6e 20 74 68 65 0a 2a 2a 20 63 75 72 72 65   in the.** curre
192b5 6e 74 20 69 6e 64 65 78 20 62 6c 6f 63 6b 2e 20  nt index block. 
192b6 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 69   Otherwise the i
192b7 4d 61 78 2d 74 68 20 6d 61 70 70 69 6e 67 20 65  Max-th mapping e
192b8 6e 74 72 79 20 6f 66 20 74 68 65 0a 2a 2a 20 63  ntry of the.** c
192b9 75 72 72 65 6e 74 20 69 6e 64 65 78 20 62 6c 6f  urrent index blo
192ba 63 6b 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  ck corresponds t
192bb 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  o the last entry
192bc 20 74 68 61 74 20 72 65 66 65 72 65 6e 63 65 73   that references
192bd 20 0a 2a 2a 20 70 61 67 65 20 50 2e 0a 2a 2a 0a   .** page P..**.
192be 2a 2a 20 41 20 68 61 73 68 20 73 65 61 72 63 68  ** A hash search
192bf 20 62 65 67 69 6e 73 20 77 69 74 68 20 74 68 65   begins with the
192c0 20 6c 61 73 74 20 69 6e 64 65 78 20 62 6c 6f 63   last index bloc
192c1 6b 20 61 6e 64 20 6d 6f 76 65 73 20 74 6f 77 61  k and moves towa
192c2 72 64 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  rd the.** first 
192c3 69 6e 64 65 78 20 62 6c 6f 63 6b 2c 20 6c 6f 6f  index block, loo
192c4 6b 69 6e 67 20 66 6f 72 20 65 6e 74 72 69 65 73  king for entries
192c5 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
192c6 6f 20 70 61 67 65 20 50 2e 20 20 4f 6e 0a 2a 2a  o page P.  On.**
192c7 20 61 76 65 72 61 67 65 2c 20 6f 6e 6c 79 20 74   average, only t
192c8 77 6f 20 6f 72 20 74 68 72 65 65 20 73 6c 6f 74  wo or three slot
192c9 73 20 69 6e 20 65 61 63 68 20 69 6e 64 65 78 20  s in each index 
192ca 62 6c 6f 63 6b 20 6e 65 65 64 20 74 6f 20 62 65  block need to be
192cb 0a 2a 2a 20 65 78 61 6d 69 6e 65 64 20 69 6e 20  .** examined in 
192cc 6f 72 64 65 72 20 74 6f 20 65 69 74 68 65 72 20  order to either 
192cd 66 69 6e 64 20 74 68 65 20 6c 61 73 74 20 65 6e  find the last en
192ce 74 72 79 20 66 6f 72 20 70 61 67 65 20 50 2c 20  try for page P, 
192cf 6f 72 20 74 6f 0a 2a 2a 20 65 73 74 61 62 6c 69  or to.** establi
192d0 73 68 20 74 68 61 74 20 6e 6f 20 73 75 63 68 20  sh that no such 
192d1 65 6e 74 72 79 20 65 78 69 73 74 73 20 69 6e 20  entry exists in 
192d2 74 68 65 20 62 6c 6f 63 6b 2e 20 20 45 61 63 68  the block.  Each
192d3 20 69 6e 64 65 78 20 62 6c 6f 63 6b 0a 2a 2a 20   index block.** 
192d4 68 6f 6c 64 73 20 6f 76 65 72 20 34 30 30 30 20  holds over 4000 
192d5 65 6e 74 72 69 65 73 2e 20 20 53 6f 20 74 77 6f  entries.  So two
192d6 20 6f 72 20 74 68 72 65 65 20 69 6e 64 65 78 20   or three index 
192d7 62 6c 6f 63 6b 73 20 61 72 65 20 73 75 66 66 69  blocks are suffi
192d8 63 69 65 6e 74 0a 2a 2a 20 74 6f 20 63 6f 76 65  cient.** to cove
192d9 72 20 61 20 74 79 70 69 63 61 6c 20 31 30 20 6d  r a typical 10 m
192da 65 67 61 62 79 74 65 20 57 41 4c 20 66 69 6c 65  egabyte WAL file
192db 2c 20 61 73 73 75 6d 69 6e 67 20 31 4b 20 70 61  , assuming 1K pa
192dc 67 65 73 2e 20 20 38 20 6f 72 20 31 30 0a 2a 2a  ges.  8 or 10.**
192dd 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20 28 6f 6e   comparisons (on
192de 20 61 76 65 72 61 67 65 29 20 73 75 66 66 69 63   average) suffic
192df 65 20 74 6f 20 65 69 74 68 65 72 20 6c 6f 63 61  e to either loca
192e0 74 65 20 61 20 66 72 61 6d 65 20 69 6e 20 74 68  te a frame in th
192e1 65 0a 2a 2a 20 57 41 4c 20 6f 72 20 74 6f 20 65  e.** WAL or to e
192e2 73 74 61 62 6c 69 73 68 20 74 68 61 74 20 74 68  stablish that th
192e3 65 20 66 72 61 6d 65 20 64 6f 65 73 20 6e 6f 74  e frame does not
192e4 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 57 41   exist in the WA
192e5 4c 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 6d  L.  This.** is m
192e6 75 63 68 20 66 61 73 74 65 72 20 74 68 61 6e 20  uch faster than 
192e7 73 63 61 6e 6e 69 6e 67 20 74 68 65 20 65 6e 74  scanning the ent
192e8 69 72 65 20 31 30 4d 42 20 57 41 4c 2e 0a 2a 2a  ire 10MB WAL..**
192e9 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 65 6e  .** Note that en
192ea 74 72 69 65 73 20 61 72 65 20 61 64 64 65 64 20  tries are added 
192eb 69 6e 20 6f 72 64 65 72 20 6f 66 20 69 6e 63 72  in order of incr
192ec 65 61 73 69 6e 67 20 4b 2e 20 20 48 65 6e 63 65  easing K.  Hence
192ed 2c 20 6f 6e 65 0a 2a 2a 20 72 65 61 64 65 72 20  , one.** reader 
192ee 6d 69 67 68 74 20 62 65 20 75 73 69 6e 67 20 73  might be using s
192ef 6f 6d 65 20 76 61 6c 75 65 20 4b 30 20 61 6e 64  ome value K0 and
192f0 20 61 20 73 65 63 6f 6e 64 20 72 65 61 64 65 72   a second reader
192f1 20 74 68 61 74 20 73 74 61 72 74 65 64 0a 2a 2a   that started.**
192f2 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65   at a later time
192f3 20 28 61 66 74 65 72 20 61 64 64 69 74 69 6f 6e   (after addition
192f4 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  al transactions 
192f5 77 65 72 65 20 61 64 64 65 64 20 74 6f 20 74 68  were added to th
192f6 65 20 57 41 4c 0a 2a 2a 20 61 6e 64 20 74 6f 20  e WAL.** and to 
192f7 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 29 20 6d  the wal-index) m
192f8 69 67 68 74 20 62 65 20 75 73 69 6e 67 20 61 20  ight be using a 
192f9 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 20  different value 
192fa 4b 31 2c 20 77 68 65 72 65 20 4b 31 3e 4b 30 2e  K1, where K1>K0.
192fb 0a 2a 2a 20 42 6f 74 68 20 72 65 61 64 65 72 73  .** Both readers
192fc 20 63 61 6e 20 75 73 65 20 74 68 65 20 73 61 6d   can use the sam
192fd 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64  e hash table and
192fe 20 6d 61 70 70 69 6e 67 20 73 65 63 74 69 6f 6e   mapping section
192ff 20 74 6f 20 67 65 74 0a 2a 2a 20 74 68 65 20 63   to get.** the c
19300 6f 72 72 65 63 74 20 72 65 73 75 6c 74 2e 20 20  orrect result.  
19301 54 68 65 72 65 20 6d 61 79 20 62 65 20 65 6e 74  There may be ent
19302 72 69 65 73 20 69 6e 20 74 68 65 20 68 61 73 68  ries in the hash
19303 20 74 61 62 6c 65 20 77 69 74 68 0a 2a 2a 20 4b   table with.** K
19304 3e 4b 30 20 62 75 74 20 74 6f 20 74 68 65 20 66  >K0 but to the f
19305 69 72 73 74 20 72 65 61 64 65 72 2c 20 74 68 6f  irst reader, tho
19306 73 65 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20  se entries will 
19307 61 70 70 65 61 72 20 74 6f 20 62 65 20 75 6e 75  appear to be unu
19308 73 65 64 0a 2a 2a 20 73 6c 6f 74 73 20 69 6e 20  sed.** slots in 
19309 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 61  the hash table a
1930a 6e 64 20 73 6f 20 74 68 65 20 66 69 72 73 74 20  nd so the first 
1930b 72 65 61 64 65 72 20 77 69 6c 6c 20 67 65 74 20  reader will get 
1930c 61 6e 20 61 6e 73 77 65 72 20 61 73 0a 2a 2a 20  an answer as.** 
1930d 69 66 20 6e 6f 20 76 61 6c 75 65 73 20 67 72 65  if no values gre
1930e 61 74 65 72 20 74 68 61 6e 20 4b 30 20 68 61 64  ater than K0 had
1930f 20 65 76 65 72 20 62 65 65 6e 20 69 6e 73 65 72   ever been inser
19310 74 65 64 20 69 6e 74 6f 20 74 68 65 20 68 61 73  ted into the has
19311 68 20 74 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68  h table.** in th
19312 65 20 66 69 72 73 74 20 70 6c 61 63 65 20 2d 20  e first place - 
19313 77 68 69 63 68 20 69 73 20 77 68 61 74 20 72 65  which is what re
19314 61 64 65 72 20 6f 6e 65 20 77 61 6e 74 73 2e 20  ader one wants. 
19315 20 4d 65 61 6e 77 68 69 6c 65 2c 20 74 68 65 0a   Meanwhile, the.
19316 2a 2a 20 73 65 63 6f 6e 64 20 72 65 61 64 65 72  ** second reader
19317 20 75 73 69 6e 67 20 4b 31 20 77 69 6c 6c 20 73   using K1 will s
19318 65 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 76 61  ee additional va
19319 6c 75 65 73 20 74 68 61 74 20 77 65 72 65 20 69  lues that were i
1931a 6e 73 65 72 74 65 64 0a 2a 2a 20 6c 61 74 65 72  nserted.** later
1931b 2c 20 77 68 69 63 68 20 69 73 20 65 78 61 63 74  , which is exact
1931c 6c 79 20 77 68 61 74 20 72 65 61 64 65 72 20 74  ly what reader t
1931d 77 6f 20 77 61 6e 74 73 2e 20 20 0a 2a 2a 0a 2a  wo wants.  .**.*
1931e 2a 20 57 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63  * When a rollbac
1931f 6b 20 6f 63 63 75 72 73 2c 20 74 68 65 20 76 61  k occurs, the va
19320 6c 75 65 20 6f 66 20 4b 20 69 73 20 64 65 63 72  lue of K is decr
19321 65 61 73 65 64 2e 20 48 61 73 68 20 74 61 62 6c  eased. Hash tabl
19322 65 20 65 6e 74 72 69 65 73 0a 2a 2a 20 74 68 61  e entries.** tha
19323 74 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20  t correspond to 
19324 66 72 61 6d 65 73 20 67 72 65 61 74 65 72 20 74  frames greater t
19325 68 61 6e 20 74 68 65 20 6e 65 77 20 4b 20 76 61  han the new K va
19326 6c 75 65 20 61 72 65 20 72 65 6d 6f 76 65 64 0a  lue are removed.
19327 2a 2a 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68  ** from the hash
19328 20 74 61 62 6c 65 20 61 74 20 74 68 69 73 20 70   table at this p
19329 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  oint..*/.#ifndef
1932a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c   SQLITE_OMIT_WAL
1932b 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 63 65 20 6f  .../*.** Trace o
1932c 75 74 70 75 74 20 6d 61 63 72 6f 73 0a 2a 2f 0a  utput macros.*/.
1932d 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
1932e 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 66 69  TE_TEST) && defi
1932f 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
19330 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
19331 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 54   int sqlite3WalT
19332 72 61 63 65 20 3d 20 30 3b 0a 23 20 64 65 66 69  race = 0;.# defi
19333 6e 65 20 57 41 4c 54 52 41 43 45 28 58 29 20 20  ne WALTRACE(X)  
19334 69 66 28 73 71 6c 69 74 65 33 57 61 6c 54 72 61  if(sqlite3WalTra
19335 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  ce) sqlite3Debug
19336 50 72 69 6e 74 66 20 58 0a 23 65 6c 73 65 0a 23  Printf X.#else.#
19337 20 64 65 66 69 6e 65 20 57 41 4c 54 52 41 43 45   define WALTRACE
19338 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  (X).#endif../*.*
19339 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 28 61  * The maximum (a
1933a 6e 64 20 6f 6e 6c 79 29 20 76 65 72 73 69 6f 6e  nd only) version
1933b 73 20 6f 66 20 74 68 65 20 77 61 6c 20 61 6e 64  s of the wal and
1933c 20 77 61 6c 2d 69 6e 64 65 78 20 66 6f 72 6d 61   wal-index forma
1933d 74 73 0a 2a 2a 20 74 68 61 74 20 6d 61 79 20 62  ts.** that may b
1933e 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 62 79  e interpreted by
1933f 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   this version of
19340 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 49   SQLite..**.** I
19341 66 20 61 20 63 6c 69 65 6e 74 20 62 65 67 69 6e  f a client begin
19342 73 20 72 65 63 6f 76 65 72 69 6e 67 20 61 20 57  s recovering a W
19343 41 4c 20 66 69 6c 65 20 61 6e 64 20 66 69 6e 64  AL file and find
19344 73 20 74 68 61 74 20 28 61 29 20 74 68 65 20 63  s that (a) the c
19345 68 65 63 6b 73 75 6d 0a 2a 2a 20 76 61 6c 75 65  hecksum.** value
19346 73 20 69 6e 20 74 68 65 20 77 61 6c 2d 68 65 61  s in the wal-hea
19347 64 65 72 20 61 72 65 20 63 6f 72 72 65 63 74 20  der are correct 
19348 61 6e 64 20 28 62 29 20 74 68 65 20 76 65 72 73  and (b) the vers
19349 69 6f 6e 20 66 69 65 6c 64 20 69 73 20 6e 6f 74  ion field is not
1934a 0a 2a 2a 20 57 41 4c 5f 4d 41 58 5f 56 45 52 53  .** WAL_MAX_VERS
1934b 49 4f 4e 2c 20 72 65 63 6f 76 65 72 79 20 66 61  ION, recovery fa
1934c 69 6c 73 20 61 6e 64 20 53 51 4c 69 74 65 20 72  ils and SQLite r
1934d 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 43 41  eturns SQLITE_CA
1934e 4e 54 4f 50 45 4e 2e 0a 2a 2a 0a 2a 2a 20 53 69  NTOPEN..**.** Si
1934f 6d 69 6c 61 72 6c 79 2c 20 69 66 20 61 20 63 6c  milarly, if a cl
19350 69 65 6e 74 20 73 75 63 63 65 73 73 66 75 6c 6c  ient successfull
19351 79 20 72 65 61 64 73 20 61 20 77 61 6c 2d 69 6e  y reads a wal-in
19352 64 65 78 20 68 65 61 64 65 72 20 28 69 2e 65 2e  dex header (i.e.
19353 20 74 68 65 20 0a 2a 2a 20 63 68 65 63 6b 73 75   the .** checksu
19354 6d 20 74 65 73 74 20 69 73 20 73 75 63 63 65 73  m test is succes
19355 73 66 75 6c 29 20 61 6e 64 20 66 69 6e 64 73 20  sful) and finds 
19356 74 68 61 74 20 74 68 65 20 76 65 72 73 69 6f 6e  that the version
19357 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 0a 2a 2a   field is not.**
19358 20 57 41 4c 49 4e 44 45 58 5f 4d 41 58 5f 56 45   WALINDEX_MAX_VE
19359 52 53 49 4f 4e 2c 20 74 68 65 6e 20 6e 6f 20 72  RSION, then no r
1935a 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ead-transaction 
1935b 69 73 20 6f 70 65 6e 65 64 20 61 6e 64 20 53 51  is opened and SQ
1935c 4c 69 74 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20  Lite.** returns 
1935d 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 2e  SQLITE_CANTOPEN.
1935e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f  .*/.#define WAL_
1935f 4d 41 58 5f 56 45 52 53 49 4f 4e 20 20 20 20 20  MAX_VERSION     
19360 20 33 30 30 37 30 30 30 0a 23 64 65 66 69 6e 65   3007000.#define
19361 20 57 41 4c 49 4e 44 45 58 5f 4d 41 58 5f 56 45   WALINDEX_MAX_VE
19362 52 53 49 4f 4e 20 33 30 30 37 30 30 30 0a 0a 2f  RSION 3007000../
19363 2a 0a 2a 2a 20 49 6e 64 69 63 65 73 20 6f 66 20  *.** Indices of 
19364 76 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20  various locking 
19365 62 79 74 65 73 2e 20 20 20 57 41 4c 5f 4e 52 45  bytes.   WAL_NRE
19366 41 44 45 52 20 69 73 20 74 68 65 20 6e 75 6d 62  ADER is the numb
19367 65 72 0a 2a 2a 20 6f 66 20 61 76 61 69 6c 61 62  er.** of availab
19368 6c 65 20 72 65 61 64 65 72 20 6c 6f 63 6b 73 20  le reader locks 
19369 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 61 74  and should be at
1936a 20 6c 65 61 73 74 20 33 2e 0a 2a 2f 0a 23 64 65   least 3..*/.#de
1936b 66 69 6e 65 20 57 41 4c 5f 57 52 49 54 45 5f 4c  fine WAL_WRITE_L
1936c 4f 43 4b 20 20 20 20 20 20 20 20 20 30 0a 23 64  OCK         0.#d
1936d 65 66 69 6e 65 20 57 41 4c 5f 41 4c 4c 5f 42 55  efine WAL_ALL_BU
1936e 54 5f 57 52 49 54 45 20 20 20 20 20 20 31 0a 23  T_WRITE      1.#
1936f 64 65 66 69 6e 65 20 57 41 4c 5f 43 4b 50 54 5f  define WAL_CKPT_
19370 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 31 0a  LOCK          1.
19371 23 64 65 66 69 6e 65 20 57 41 4c 5f 52 45 43 4f  #define WAL_RECO
19372 56 45 52 5f 4c 4f 43 4b 20 20 20 20 20 20 20 32  VER_LOCK       2
19373 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 52 45 41  .#define WAL_REA
19374 44 5f 4c 4f 43 4b 28 49 29 20 20 20 20 20 20 20  D_LOCK(I)       
19375 28 33 2b 28 49 29 29 0a 23 64 65 66 69 6e 65 20  (3+(I)).#define 
19376 57 41 4c 5f 4e 52 45 41 44 45 52 20 20 20 20 20  WAL_NREADER     
19377 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 53         (SQLITE_S
19378 48 4d 5f 4e 4c 4f 43 4b 2d 33 29 0a 0a 0a 2f 2a  HM_NLOCK-3).../*
19379 20 4f 62 6a 65 63 74 20 64 65 63 6c 61 72 61 74   Object declarat
1937a 69 6f 6e 73 20 2a 2f 0a 74 79 70 65 64 65 66 20  ions */.typedef 
1937b 73 74 72 75 63 74 20 57 61 6c 49 6e 64 65 78 48  struct WalIndexH
1937c 64 72 20 57 61 6c 49 6e 64 65 78 48 64 72 3b 0a  dr WalIndexHdr;.
1937d 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57  typedef struct W
1937e 61 6c 49 74 65 72 61 74 6f 72 20 57 61 6c 49 74  alIterator WalIt
1937f 65 72 61 74 6f 72 3b 0a 74 79 70 65 64 65 66 20  erator;.typedef 
19380 73 74 72 75 63 74 20 57 61 6c 43 6b 70 74 49 6e  struct WalCkptIn
19381 66 6f 20 57 61 6c 43 6b 70 74 49 6e 66 6f 3b 0a  fo WalCkptInfo;.
19382 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
19383 6f 77 69 6e 67 20 6f 62 6a 65 63 74 20 68 6f 6c  owing object hol
19384 64 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ds a copy of the
19385 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65   wal-index heade
19386 72 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a  r content..**.**
19387 20 54 68 65 20 61 63 74 75 61 6c 20 68 65 61 64   The actual head
19388 65 72 20 69 6e 20 74 68 65 20 77 61 6c 2d 69 6e  er in the wal-in
19389 64 65 78 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  dex consists of 
1938a 74 77 6f 20 63 6f 70 69 65 73 20 6f 66 20 74 68  two copies of th
1938b 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 2e 0a 2a 2a  is.** object..**
1938c 0a 2a 2a 20 54 68 65 20 73 7a 50 61 67 65 20 76  .** The szPage v
1938d 61 6c 75 65 20 63 61 6e 20 62 65 20 61 6e 79 20  alue can be any 
1938e 70 6f 77 65 72 20 6f 66 20 32 20 62 65 74 77 65  power of 2 betwe
1938f 65 6e 20 35 31 32 20 61 6e 64 20 33 32 37 36 38  en 512 and 32768
19390 2c 20 69 6e 63 6c 75 73 69 76 65 2e 0a 2a 2a 20  , inclusive..** 
19391 4f 72 20 69 74 20 63 61 6e 20 62 65 20 31 20 74  Or it can be 1 t
19392 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20 36 35  o represent a 65
19393 35 33 36 2d 62 79 74 65 20 70 61 67 65 2e 20 20  536-byte page.  
19394 54 68 65 20 6c 61 74 74 65 72 20 63 61 73 65 20  The latter case 
19395 77 61 73 0a 2a 2a 20 61 64 64 65 64 20 69 6e 20  was.** added in 
19396 33 2e 37 2e 31 20 77 68 65 6e 20 73 75 70 70 6f  3.7.1 when suppo
19397 72 74 20 66 6f 72 20 36 34 4b 20 70 61 67 65 73  rt for 64K pages
19398 20 77 61 73 20 61 64 64 65 64 2e 20 20 0a 2a 2f   was added.  .*/
19399 0a 73 74 72 75 63 74 20 57 61 6c 49 6e 64 65 78  .struct WalIndex
1939a 48 64 72 20 7b 0a 20 20 75 33 32 20 69 56 65 72  Hdr {.  u32 iVer
1939b 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20  sion;           
1939c 20 20 20 20 20 20 20 20 2f 2a 20 57 61 6c 2d 69          /* Wal-i
1939d 6e 64 65 78 20 76 65 72 73 69 6f 6e 20 2a 2f 0a  ndex version */.
1939e 20 20 75 33 32 20 75 6e 75 73 65 64 3b 20 20 20    u32 unused;   
1939f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
193a0 20 20 2f 2a 20 55 6e 75 73 65 64 20 28 70 61 64    /* Unused (pad
193a1 64 69 6e 67 29 20 66 69 65 6c 64 20 2a 2f 0a 20  ding) field */. 
193a2 20 75 33 32 20 69 43 68 61 6e 67 65 3b 20 20 20   u32 iChange;   
193a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
193a4 20 2f 2a 20 43 6f 75 6e 74 65 72 20 69 6e 63 72   /* Counter incr
193a5 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74 72 61  emented each tra
193a6 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38  nsaction */.  u8
193a7 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20   isInit;        
193a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
193a9 20 31 20 77 68 65 6e 20 69 6e 69 74 69 61 6c 69   1 when initiali
193aa 7a 65 64 20 2a 2f 0a 20 20 75 38 20 62 69 67 45  zed */.  u8 bigE
193ab 6e 64 43 6b 73 75 6d 3b 20 20 20 20 20 20 20 20  ndCksum;        
193ac 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
193ad 20 69 66 20 63 68 65 63 6b 73 75 6d 73 20 69 6e   if checksums in
193ae 20 57 41 4c 20 61 72 65 20 62 69 67 2d 65 6e 64   WAL are big-end
193af 69 61 6e 20 2a 2f 0a 20 20 75 31 36 20 73 7a 50  ian */.  u16 szP
193b0 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
193b1 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
193b2 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 20 69  base page size i
193b3 6e 20 62 79 74 65 73 2e 20 31 3d 3d 36 34 4b 20  n bytes. 1==64K 
193b4 2a 2f 0a 20 20 75 33 32 20 6d 78 46 72 61 6d 65  */.  u32 mxFrame
193b5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
193b6 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
193b7 20 6c 61 73 74 20 76 61 6c 69 64 20 66 72 61 6d   last valid fram
193b8 65 20 69 6e 20 74 68 65 20 57 41 4c 20 2a 2f 0a  e in the WAL */.
193b9 20 20 75 33 32 20 6e 50 61 67 65 3b 20 20 20 20    u32 nPage;    
193ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
193bb 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 61 74    /* Size of dat
193bc 61 62 61 73 65 20 69 6e 20 70 61 67 65 73 20 2a  abase in pages *
193bd 2f 0a 20 20 75 33 32 20 61 46 72 61 6d 65 43 6b  /.  u32 aFrameCk
193be 73 75 6d 5b 32 5d 3b 20 20 20 20 20 20 20 20 20  sum[2];         
193bf 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20      /* Checksum 
193c0 6f 66 20 6c 61 73 74 20 66 72 61 6d 65 20 69 6e  of last frame in
193c1 20 6c 6f 67 20 2a 2f 0a 20 20 75 33 32 20 61 53   log */.  u32 aS
193c2 61 6c 74 5b 32 5d 3b 20 20 20 20 20 20 20 20 20  alt[2];         
193c3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 77 6f            /* Two
193c4 20 73 61 6c 74 20 76 61 6c 75 65 73 20 63 6f 70   salt values cop
193c5 69 65 64 20 66 72 6f 6d 20 57 41 4c 20 68 65 61  ied from WAL hea
193c6 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 61 43 6b  der */.  u32 aCk
193c7 73 75 6d 5b 32 5d 3b 20 20 20 20 20 20 20 20 20  sum[2];         
193c8 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63           /* Chec
193c9 6b 73 75 6d 20 6f 76 65 72 20 61 6c 6c 20 70 72  ksum over all pr
193ca 69 6f 72 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b  ior fields */.};
193cb 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 70 79 20 6f  ../*.** A copy o
193cc 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
193cd 6f 62 6a 65 63 74 20 6f 63 63 75 72 73 20 69 6e  object occurs in
193ce 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 69   the wal-index i
193cf 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 66 6f  mmediately.** fo
193d0 6c 6c 6f 77 69 6e 67 20 74 68 65 20 73 65 63 6f  llowing the seco
193d1 6e 64 20 63 6f 70 79 20 6f 66 20 74 68 65 20 57  nd copy of the W
193d2 61 6c 49 6e 64 65 78 48 64 72 2e 20 20 54 68 69  alIndexHdr.  Thi
193d3 73 20 6f 62 6a 65 63 74 20 73 74 6f 72 65 73 0a  s object stores.
193d4 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75  ** information u
193d5 73 65 64 20 62 79 20 63 68 65 63 6b 70 6f 69 6e  sed by checkpoin
193d6 74 2e 0a 2a 2a 0a 2a 2a 20 6e 42 61 63 6b 66 69  t..**.** nBackfi
193d7 6c 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ll is the number
193d8 20 6f 66 20 66 72 61 6d 65 73 20 69 6e 20 74 68   of frames in th
193d9 65 20 57 41 4c 20 74 68 61 74 20 68 61 76 65 20  e WAL that have 
193da 62 65 65 6e 20 77 72 69 74 74 65 6e 0a 2a 2a 20  been written.** 
193db 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61  back into the da
193dc 74 61 62 61 73 65 2e 20 28 57 65 20 63 61 6c 6c  tabase. (We call
193dd 20 74 68 65 20 61 63 74 20 6f 66 20 6d 6f 76 69   the act of movi
193de 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20  ng content from 
193df 57 41 4c 20 74 6f 0a 2a 2a 20 64 61 74 61 62 61  WAL to.** databa
193e0 73 65 20 22 62 61 63 6b 66 69 6c 6c 69 6e 67 22  se "backfilling"
193e1 2e 29 20 20 54 68 65 20 6e 42 61 63 6b 66 69 6c  .)  The nBackfil
193e2 6c 20 6e 75 6d 62 65 72 20 69 73 20 6e 65 76 65  l number is neve
193e3 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a  r greater than.*
193e4 2a 20 57 61 6c 49 6e 64 65 78 48 64 72 2e 6d 78  * WalIndexHdr.mx
193e5 46 72 61 6d 65 2e 20 20 6e 42 61 63 6b 66 69 6c  Frame.  nBackfil
193e6 6c 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 69 6e  l can only be in
193e7 63 72 65 61 73 65 64 20 62 79 20 74 68 72 65 61  creased by threa
193e8 64 73 0a 2a 2a 20 68 6f 6c 64 69 6e 67 20 74 68  ds.** holding th
193e9 65 20 57 41 4c 5f 43 4b 50 54 5f 4c 4f 43 4b 20  e WAL_CKPT_LOCK 
193ea 6c 6f 63 6b 20 28 77 68 69 63 68 20 69 6e 63 6c  lock (which incl
193eb 75 64 65 73 20 61 20 72 65 63 6f 76 65 72 79 20  udes a recovery 
193ec 74 68 72 65 61 64 29 2e 0a 2a 2a 20 48 6f 77 65  thread)..** Howe
193ed 76 65 72 2c 20 61 20 57 41 4c 5f 57 52 49 54 45  ver, a WAL_WRITE
193ee 5f 4c 4f 43 4b 20 74 68 72 65 61 64 20 63 61 6e  _LOCK thread can
193ef 20 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 20   move the value 
193f0 6f 66 20 6e 42 61 63 6b 66 69 6c 6c 20 66 72 6f  of nBackfill fro
193f1 6d 0a 2a 2a 20 6d 78 46 72 61 6d 65 20 62 61 63  m.** mxFrame bac
193f2 6b 20 74 6f 20 7a 65 72 6f 20 77 68 65 6e 20 74  k to zero when t
193f3 68 65 20 57 41 4c 20 69 73 20 72 65 73 65 74 2e  he WAL is reset.
193f4 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20  .**.** There is 
193f5 6f 6e 65 20 65 6e 74 72 79 20 69 6e 20 61 52 65  one entry in aRe
193f6 61 64 4d 61 72 6b 5b 5d 20 66 6f 72 20 65 61 63  adMark[] for eac
193f7 68 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 20 20  h reader lock.  
193f8 49 66 20 61 20 72 65 61 64 65 72 0a 2a 2a 20 68  If a reader.** h
193f9 6f 6c 64 73 20 72 65 61 64 2d 6c 6f 63 6b 20 4b  olds read-lock K
193fa 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  , then the value
193fb 20 69 6e 20 61 52 65 61 64 4d 61 72 6b 5b 4b 5d   in aReadMark[K]
193fc 20 69 73 20 6e 6f 20 67 72 65 61 74 65 72 20 74   is no greater t
193fd 68 61 6e 0a 2a 2a 20 74 68 65 20 6d 78 46 72 61  han.** the mxFra
193fe 6d 65 20 66 6f 72 20 74 68 61 74 20 72 65 61 64  me for that read
193ff 65 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 52  er.  The value R
19400 45 41 44 4d 41 52 4b 5f 4e 4f 54 5f 55 53 45 44  EADMARK_NOT_USED
19401 20 28 30 78 66 66 66 66 66 66 66 66 29 0a 2a 2a   (0xffffffff).**
19402 20 66 6f 72 20 61 6e 79 20 61 52 65 61 64 4d 61   for any aReadMa
19403 72 6b 5b 5d 20 6d 65 61 6e 73 20 74 68 61 74 20  rk[] means that 
19404 65 6e 74 72 79 20 69 73 20 75 6e 75 73 65 64 2e  entry is unused.
19405 20 20 61 52 65 61 64 4d 61 72 6b 5b 30 5d 20 69    aReadMark[0] i
19406 73 20 0a 2a 2a 20 61 20 73 70 65 63 69 61 6c 20  s .** a special 
19407 63 61 73 65 3b 20 69 74 73 20 76 61 6c 75 65 20  case; its value 
19408 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 61 6e  is never used an
19409 64 20 69 74 20 65 78 69 73 74 73 20 61 73 20 61  d it exists as a
1940a 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 0a 2a 2a   place-holder.**
1940b 20 74 6f 20 61 76 6f 69 64 20 68 61 76 69 6e 67   to avoid having
1940c 20 74 6f 20 6f 66 66 73 65 74 20 61 52 65 61 64   to offset aRead
1940d 4d 61 72 6b 5b 5d 20 69 6e 64 65 78 73 20 62 79  Mark[] indexs by
1940e 20 6f 6e 65 2e 20 20 52 65 61 64 65 72 73 20 68   one.  Readers h
1940f 6f 6c 64 69 6e 67 0a 2a 2a 20 57 41 4c 5f 52 45  olding.** WAL_RE
19410 41 44 5f 4c 4f 43 4b 28 30 29 20 61 6c 77 61 79  AD_LOCK(0) alway
19411 73 20 69 67 6e 6f 72 65 20 74 68 65 20 65 6e 74  s ignore the ent
19412 69 72 65 20 57 41 4c 20 61 6e 64 20 72 65 61 64  ire WAL and read
19413 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20   all content.** 
19414 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 74 68  directly from th
19415 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  e database..**.*
19416 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 61  * The value of a
19417 52 65 61 64 4d 61 72 6b 5b 4b 5d 20 6d 61 79 20  ReadMark[K] may 
19418 6f 6e 6c 79 20 62 65 20 63 68 61 6e 67 65 64 20  only be changed 
19419 62 79 20 61 20 74 68 72 65 61 64 20 74 68 61 74  by a thread that
1941a 0a 2a 2a 20 69 73 20 68 6f 6c 64 69 6e 67 20 61  .** is holding a
1941b 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
1941c 20 6f 6e 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43   on WAL_READ_LOC
1941d 4b 28 4b 29 2e 20 20 54 68 75 73 2c 20 74 68 65  K(K).  Thus, the
1941e 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 61 52 65   value of.** aRe
1941f 61 64 4d 61 72 6b 5b 4b 5d 20 63 61 6e 6e 6f 74  adMark[K] cannot
19420 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 74   changed while t
19421 68 65 72 65 20 69 73 20 61 20 72 65 61 64 65 72  here is a reader
19422 20 69 73 20 75 73 69 6e 67 20 74 68 61 74 20 6d   is using that m
19423 61 72 6b 0a 2a 2a 20 73 69 6e 63 65 20 74 68 65  ark.** since the
19424 20 72 65 61 64 65 72 20 77 69 6c 6c 20 62 65 20   reader will be 
19425 68 6f 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64  holding a shared
19426 20 6c 6f 63 6b 20 6f 6e 20 57 41 4c 5f 52 45 41   lock on WAL_REA
19427 44 5f 4c 4f 43 4b 28 4b 29 2e 0a 2a 2a 0a 2a 2a  D_LOCK(K)..**.**
19428 20 54 68 65 20 63 68 65 63 6b 70 6f 69 6e 74 65   The checkpointe
19429 72 20 6d 61 79 20 6f 6e 6c 79 20 74 72 61 6e 73  r may only trans
1942a 66 65 72 20 66 72 61 6d 65 73 20 66 72 6f 6d 20  fer frames from 
1942b 57 41 4c 20 74 6f 20 64 61 74 61 62 61 73 65 20  WAL to database 
1942c 77 68 65 72 65 0a 2a 2a 20 74 68 65 20 66 72 61  where.** the fra
1942d 6d 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6c  me numbers are l
1942e 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ess than or equa
1942f 6c 20 74 6f 20 65 76 65 72 79 20 61 52 65 61 64  l to every aRead
19430 4d 61 72 6b 5b 5d 20 74 68 61 74 20 69 73 0a 2a  Mark[] that is.*
19431 2a 20 69 6e 20 75 73 65 20 28 74 68 61 74 20 69  * in use (that i
19432 73 2c 20 65 76 65 72 79 20 61 52 65 61 64 4d 61  s, every aReadMa
19433 72 6b 5b 6a 5d 20 66 6f 72 20 77 68 69 63 68 20  rk[j] for which 
19434 74 68 65 72 65 20 69 73 20 61 20 63 6f 72 72 65  there is a corre
19435 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 57 41 4c 5f  sponding.** WAL_
19436 52 45 41 44 5f 4c 4f 43 4b 28 6a 29 29 2e 20 20  READ_LOCK(j)).  
19437 4e 65 77 20 72 65 61 64 65 72 73 20 28 75 73 75  New readers (usu
19438 61 6c 6c 79 29 20 70 69 63 6b 20 74 68 65 20 61  ally) pick the a
19439 52 65 61 64 4d 61 72 6b 5b 5d 20 77 69 74 68 20  ReadMark[] with 
1943a 74 68 65 0a 2a 2a 20 6c 61 72 67 65 73 74 20 76  the.** largest v
1943b 61 6c 75 65 20 61 6e 64 20 77 69 6c 6c 20 69 6e  alue and will in
1943c 63 72 65 61 73 65 20 61 6e 20 75 6e 75 73 65 64  crease an unused
1943d 20 61 52 65 61 64 4d 61 72 6b 5b 5d 20 74 6f 20   aReadMark[] to 
1943e 6d 78 46 72 61 6d 65 20 69 66 20 74 68 65 72 65  mxFrame if there
1943f 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  .** is not alrea
19440 64 79 20 61 6e 20 61 52 65 61 64 4d 61 72 6b 5b  dy an aReadMark[
19441 5d 20 65 71 75 61 6c 20 74 6f 20 6d 78 46 72 61  ] equal to mxFra
19442 6d 65 2e 20 20 54 68 65 20 65 78 63 65 70 74 69  me.  The excepti
19443 6f 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 70 72 65  on to the.** pre
19444 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 20 69  vious sentence i
19445 73 20 77 68 65 6e 20 6e 42 61 63 6b 66 69 6c 6c  s when nBackfill
19446 20 65 71 75 61 6c 73 20 6d 78 46 72 61 6d 65 20   equals mxFrame 
19447 28 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 65 76  (meaning that ev
19448 65 72 79 74 68 69 6e 67 0a 2a 2a 20 69 6e 20 74  erything.** in t
19449 68 65 20 57 41 4c 20 68 61 73 20 62 65 65 6e 20  he WAL has been 
1944a 62 61 63 6b 66 69 6c 6c 65 64 20 69 6e 74 6f 20  backfilled into 
1944b 74 68 65 20 64 61 74 61 62 61 73 65 29 20 74 68  the database) th
1944c 65 6e 20 6e 65 77 20 72 65 61 64 65 72 73 0a 2a  en new readers.*
1944d 2a 20 77 69 6c 6c 20 63 68 6f 6f 73 65 20 61 52  * will choose aR
1944e 65 61 64 4d 61 72 6b 5b 30 5d 20 77 68 69 63 68  eadMark[0] which
1944f 20 68 61 73 20 76 61 6c 75 65 20 30 20 61 6e 64   has value 0 and
19450 20 68 65 6e 63 65 20 73 75 63 68 20 72 65 61 64   hence such read
19451 65 72 20 77 69 6c 6c 0a 2a 2a 20 67 65 74 20 61  er will.** get a
19452 6c 6c 20 74 68 65 69 72 20 61 6c 6c 20 63 6f 6e  ll their all con
19453 74 65 6e 74 20 64 69 72 65 63 74 6c 79 20 66 72  tent directly fr
19454 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
19455 66 69 6c 65 20 61 6e 64 20 69 67 6e 6f 72 65 20  file and ignore 
19456 0a 2a 2a 20 74 68 65 20 57 41 4c 2e 0a 2a 2a 0a  .** the WAL..**.
19457 2a 2a 20 57 72 69 74 65 72 73 20 6e 6f 72 6d 61  ** Writers norma
19458 6c 6c 79 20 61 70 70 65 6e 64 20 6e 65 77 20 66  lly append new f
19459 72 61 6d 65 73 20 74 6f 20 74 68 65 20 65 6e 64  rames to the end
1945a 20 6f 66 20 74 68 65 20 57 41 4c 2e 20 20 48 6f   of the WAL.  Ho
1945b 77 65 76 65 72 2c 0a 2a 2a 20 69 66 20 6e 42 61  wever,.** if nBa
1945c 63 6b 66 69 6c 6c 20 65 71 75 61 6c 73 20 6d 78  ckfill equals mx
1945d 46 72 61 6d 65 20 28 6d 65 61 6e 69 6e 67 20 74  Frame (meaning t
1945e 68 61 74 20 61 6c 6c 20 57 41 4c 20 63 6f 6e 74  hat all WAL cont
1945f 65 6e 74 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20  ent has been.** 
19460 77 72 69 74 74 65 6e 20 62 61 63 6b 20 69 6e 74  written back int
19461 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 29 20  o the database) 
19462 61 6e 64 20 69 66 20 6e 6f 20 72 65 61 64 65 72  and if no reader
19463 73 20 61 72 65 20 75 73 69 6e 67 20 74 68 65 20  s are using the 
19464 57 41 4c 0a 2a 2a 20 28 69 6e 20 6f 74 68 65 72  WAL.** (in other
19465 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 72 65   words, if there
19466 20 61 72 65 20 6e 6f 20 57 41 4c 5f 52 45 41 44   are no WAL_READ
19467 5f 4c 4f 43 4b 28 69 29 20 77 68 65 72 65 20 69  _LOCK(i) where i
19468 3e 30 29 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  >0) then.** the 
19469 77 72 69 74 65 72 20 77 69 6c 6c 20 66 69 72 73  writer will firs
1946a 74 20 22 72 65 73 65 74 22 20 74 68 65 20 57 41  t "reset" the WA
1946b 4c 20 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65  L back to the be
1946c 67 69 6e 6e 69 6e 67 20 61 6e 64 20 73 74 61 72  ginning and star
1946d 74 0a 2a 2a 20 77 72 69 74 69 6e 67 20 6e 65 77  t.** writing new
1946e 20 63 6f 6e 74 65 6e 74 20 62 65 67 69 6e 6e 69   content beginni
1946f 6e 67 20 61 74 20 66 72 61 6d 65 20 31 2e 0a 2a  ng at frame 1..*
19470 2a 0a 2a 2a 20 57 65 20 61 73 73 75 6d 65 20 74  *.** We assume t
19471 68 61 74 20 33 32 2d 62 69 74 20 6c 6f 61 64 73  hat 32-bit loads
19472 20 61 72 65 20 61 74 6f 6d 69 63 20 61 6e 64 20   are atomic and 
19473 73 6f 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20  so no locks are 
19474 6e 65 65 64 65 64 20 69 6e 0a 2a 2a 20 6f 72 64  needed in.** ord
19475 65 72 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20  er to read from 
19476 61 6e 79 20 61 52 65 61 64 4d 61 72 6b 5b 5d 20  any aReadMark[] 
19477 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 72 75  entries..*/.stru
19478 63 74 20 57 61 6c 43 6b 70 74 49 6e 66 6f 20 7b  ct WalCkptInfo {
19479 0a 20 20 75 33 32 20 6e 42 61 63 6b 66 69 6c 6c  .  u32 nBackfill
1947a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1947b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1947c 57 41 4c 20 66 72 61 6d 65 73 20 62 61 63 6b 66  WAL frames backf
1947d 69 6c 6c 65 64 20 69 6e 74 6f 20 44 42 20 2a 2f  illed into DB */
1947e 0a 20 20 75 33 32 20 61 52 65 61 64 4d 61 72 6b  .  u32 aReadMark
1947f 5b 57 41 4c 5f 4e 52 45 41 44 45 52 5d 3b 20 20  [WAL_NREADER];  
19480 20 20 20 2f 2a 20 52 65 61 64 65 72 20 6d 61 72     /* Reader mar
19481 6b 73 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65  ks */.};.#define
19482 20 52 45 41 44 4d 41 52 4b 5f 4e 4f 54 5f 55 53   READMARK_NOT_US
19483 45 44 20 20 30 78 66 66 66 66 66 66 66 66 0a 0a  ED  0xffffffff..
19484 0a 2f 2a 20 41 20 62 6c 6f 63 6b 20 6f 66 20 57  ./* A block of W
19485 41 4c 49 4e 44 45 58 5f 4c 4f 43 4b 5f 52 45 53  ALINDEX_LOCK_RES
19486 45 52 56 45 44 20 62 79 74 65 73 20 62 65 67 69  ERVED bytes begi
19487 6e 6e 69 6e 67 20 61 74 0a 2a 2a 20 57 41 4c 49  nning at.** WALI
19488 4e 44 45 58 5f 4c 4f 43 4b 5f 4f 46 46 53 45 54  NDEX_LOCK_OFFSET
19489 20 69 73 20 72 65 73 65 72 76 65 64 20 66 6f 72   is reserved for
1948a 20 6c 6f 63 6b 73 2e 20 53 69 6e 63 65 20 73 6f   locks. Since so
1948b 6d 65 20 73 79 73 74 65 6d 73 0a 2a 2a 20 6f 6e  me systems.** on
1948c 6c 79 20 73 75 70 70 6f 72 74 20 6d 61 6e 64 61  ly support manda
1948d 74 6f 72 79 20 66 69 6c 65 2d 6c 6f 63 6b 73 2c  tory file-locks,
1948e 20 77 65 20 64 6f 20 6e 6f 74 20 72 65 61 64 20   we do not read 
1948f 6f 72 20 77 72 69 74 65 20 64 61 74 61 0a 2a 2a  or write data.**
19490 20 66 72 6f 6d 20 74 68 65 20 72 65 67 69 6f 6e   from the region
19491 20 6f 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20   of the file on 
19492 77 68 69 63 68 20 6c 6f 63 6b 73 20 61 72 65 20  which locks are 
19493 61 70 70 6c 69 65 64 2e 0a 2a 2f 0a 23 64 65 66  applied..*/.#def
19494 69 6e 65 20 57 41 4c 49 4e 44 45 58 5f 4c 4f 43  ine WALINDEX_LOC
19495 4b 5f 4f 46 46 53 45 54 20 20 20 28 73 69 7a 65  K_OFFSET   (size
19496 6f 66 28 57 61 6c 49 6e 64 65 78 48 64 72 29 2a  of(WalIndexHdr)*
19497 32 20 2b 20 73 69 7a 65 6f 66 28 57 61 6c 43 6b  2 + sizeof(WalCk
19498 70 74 49 6e 66 6f 29 29 0a 23 64 65 66 69 6e 65  ptInfo)).#define
19499 20 57 41 4c 49 4e 44 45 58 5f 4c 4f 43 4b 5f 52   WALINDEX_LOCK_R
1949a 45 53 45 52 56 45 44 20 31 36 0a 23 64 65 66 69  ESERVED 16.#defi
1949b 6e 65 20 57 41 4c 49 4e 44 45 58 5f 48 44 52 5f  ne WALINDEX_HDR_
1949c 53 49 5a 45 20 20 20 20 20 20 28 57 41 4c 49 4e  SIZE      (WALIN
1949d 44 45 58 5f 4c 4f 43 4b 5f 4f 46 46 53 45 54 2b  DEX_LOCK_OFFSET+
1949e 57 41 4c 49 4e 44 45 58 5f 4c 4f 43 4b 5f 52 45  WALINDEX_LOCK_RE
1949f 53 45 52 56 45 44 29 0a 0a 2f 2a 20 53 69 7a 65  SERVED)../* Size
194a0 20 6f 66 20 68 65 61 64 65 72 20 62 65 66 6f 72   of header befor
194a1 65 20 65 61 63 68 20 66 72 61 6d 65 20 69 6e 20  e each frame in 
194a2 77 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  wal */.#define W
194a3 41 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49 5a 45  AL_FRAME_HDRSIZE
194a4 20 32 34 0a 0a 2f 2a 20 53 69 7a 65 20 6f 66 20   24../* Size of 
194a5 77 72 69 74 65 20 61 68 65 61 64 20 6c 6f 67 20  write ahead log 
194a6 68 65 61 64 65 72 2c 20 69 6e 63 6c 75 64 69 6e  header, includin
194a7 67 20 63 68 65 63 6b 73 75 6d 2e 20 2a 2f 0a 2f  g checksum. */./
194a8 2a 20 23 64 65 66 69 6e 65 20 57 41 4c 5f 48 44  * #define WAL_HD
194a9 52 53 49 5a 45 20 32 34 20 2a 2f 0a 23 64 65 66  RSIZE 24 */.#def
194aa 69 6e 65 20 57 41 4c 5f 48 44 52 53 49 5a 45 20  ine WAL_HDRSIZE 
194ab 33 32 0a 0a 2f 2a 20 57 41 4c 20 6d 61 67 69 63  32../* WAL magic
194ac 20 76 61 6c 75 65 2e 20 45 69 74 68 65 72 20 74   value. Either t
194ad 68 69 73 20 76 61 6c 75 65 2c 20 6f 72 20 74 68  his value, or th
194ae 65 20 73 61 6d 65 20 76 61 6c 75 65 20 77 69 74  e same value wit
194af 68 20 74 68 65 20 6c 65 61 73 74 0a 2a 2a 20 73  h the least.** s
194b0 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74 20 61  ignificant bit a
194b1 6c 73 6f 20 73 65 74 20 28 57 41 4c 5f 4d 41 47  lso set (WAL_MAG
194b2 49 43 20 7c 20 30 78 30 30 30 30 30 30 30 31 29  IC | 0x00000001)
194b3 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 33 32   is stored in 32
194b4 2d 62 69 74 0a 2a 2a 20 62 69 67 2d 65 6e 64 69  -bit.** big-endi
194b5 61 6e 20 66 6f 72 6d 61 74 20 69 6e 20 74 68 65  an format in the
194b6 20 66 69 72 73 74 20 34 20 62 79 74 65 73 20 6f   first 4 bytes o
194b7 66 20 61 20 57 41 4c 20 66 69 6c 65 2e 0a 2a 2a  f a WAL file..**
194b8 0a 2a 2a 20 49 66 20 74 68 65 20 4c 53 42 20 69  .** If the LSB i
194b9 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  s set, then the 
194ba 63 68 65 63 6b 73 75 6d 73 20 66 6f 72 20 65 61  checksums for ea
194bb 63 68 20 66 72 61 6d 65 20 77 69 74 68 69 6e 20  ch frame within 
194bc 74 68 65 20 57 41 4c 0a 2a 2a 20 66 69 6c 65 20  the WAL.** file 
194bd 61 72 65 20 63 61 6c 63 75 6c 61 74 65 64 20 62  are calculated b
194be 79 20 74 72 65 61 74 69 6e 67 20 61 6c 6c 20 64  y treating all d
194bf 61 74 61 20 61 73 20 61 6e 20 61 72 72 61 79 20  ata as an array 
194c0 6f 66 20 33 32 2d 62 69 74 20 0a 2a 2a 20 62 69  of 32-bit .** bi
194c1 67 2d 65 6e 64 69 61 6e 20 77 6f 72 64 73 2e 20  g-endian words. 
194c2 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79 20  Otherwise, they 
194c3 61 72 65 20 63 61 6c 63 75 6c 61 74 65 64 20 62  are calculated b
194c4 79 20 69 6e 74 65 72 70 72 65 74 69 6e 67 20 0a  y interpreting .
194c5 2a 2a 20 61 6c 6c 20 64 61 74 61 20 61 73 20 33  ** all data as 3
194c6 32 2d 62 69 74 20 6c 69 74 74 6c 65 2d 65 6e 64  2-bit little-end
194c7 69 61 6e 20 77 6f 72 64 73 2e 0a 2a 2f 0a 23 64  ian words..*/.#d
194c8 65 66 69 6e 65 20 57 41 4c 5f 4d 41 47 49 43 20  efine WAL_MAGIC 
194c9 30 78 33 37 37 66 30 36 38 32 0a 0a 2f 2a 0a 2a  0x377f0682../*.*
194ca 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6f 66 66  * Return the off
194cb 73 65 74 20 6f 66 20 66 72 61 6d 65 20 69 46 72  set of frame iFr
194cc 61 6d 65 20 69 6e 20 74 68 65 20 77 72 69 74 65  ame in the write
194cd 2d 61 68 65 61 64 20 6c 6f 67 20 66 69 6c 65 2c  -ahead log file,
194ce 20 0a 2a 2a 20 61 73 73 75 6d 69 6e 67 20 61 20   .** assuming a 
194cf 64 61 74 61 62 61 73 65 20 70 61 67 65 20 73 69  database page si
194d0 7a 65 20 6f 66 20 73 7a 50 61 67 65 20 62 79 74  ze of szPage byt
194d1 65 73 2e 20 54 68 65 20 6f 66 66 73 65 74 20 72  es. The offset r
194d2 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 74 6f  eturned.** is to
194d3 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
194d4 65 20 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f  e write-ahead lo
194d5 67 20 66 72 61 6d 65 2d 68 65 61 64 65 72 2e 0a  g frame-header..
194d6 2a 2f 0a 23 64 65 66 69 6e 65 20 77 61 6c 46 72  */.#define walFr
194d7 61 6d 65 4f 66 66 73 65 74 28 69 46 72 61 6d 65  ameOffset(iFrame
194d8 2c 20 73 7a 50 61 67 65 29 20 28 20 20 20 20 20  , szPage) (     
194d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
194da 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 57 41            \.  WA
194db 4c 5f 48 44 52 53 49 5a 45 20 2b 20 28 28 69 46  L_HDRSIZE + ((iF
194dc 72 61 6d 65 29 2d 31 29 2a 28 69 36 34 29 28 28  rame)-1)*(i64)((
194dd 73 7a 50 61 67 65 29 2b 57 41 4c 5f 46 52 41 4d  szPage)+WAL_FRAM
194de 45 5f 48 44 52 53 49 5a 45 29 20 20 20 20 20 20  E_HDRSIZE)      
194df 20 20 20 5c 0a 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e     \.)../*.** An
194e0 20 6f 70 65 6e 20 77 72 69 74 65 2d 61 68 65 61   open write-ahea
194e1 64 20 6c 6f 67 20 66 69 6c 65 20 69 73 20 72 65  d log file is re
194e2 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 6e 20  presented by an 
194e3 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 0a  instance of the.
194e4 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a  ** following obj
194e5 65 63 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57  ect..*/.struct W
194e6 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  al {.  sqlite3_v
194e7 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20  fs *pVfs;       
194e8 20 20 2f 2a 20 54 68 65 20 56 46 53 20 75 73 65    /* The VFS use
194e9 64 20 74 6f 20 63 72 65 61 74 65 20 70 44 62 46  d to create pDbF
194ea 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  d */.  sqlite3_f
194eb 69 6c 65 20 2a 70 44 62 46 64 3b 20 20 20 20 20  ile *pDbFd;     
194ec 20 20 2f 2a 20 46 69 6c 65 20 68 61 6e 64 6c 65    /* File handle
194ed 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73   for the databas
194ee 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69  e file */.  sqli
194ef 74 65 33 5f 66 69 6c 65 20 2a 70 57 61 6c 46 64  te3_file *pWalFd
194f0 3b 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 68  ;      /* File h
194f1 61 6e 64 6c 65 20 66 6f 72 20 57 41 4c 20 66 69  andle for WAL fi
194f2 6c 65 20 2a 2f 0a 20 20 75 33 32 20 69 43 61 6c  le */.  u32 iCal
194f3 6c 62 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20  lback;          
194f4 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 70     /* Value to p
194f5 61 73 73 20 74 6f 20 6c 6f 67 20 63 61 6c 6c 62  ass to log callb
194f6 61 63 6b 20 28 6f 72 20 30 29 20 2a 2f 0a 20 20  ack (or 0) */.  
194f7 69 36 34 20 6d 78 57 61 6c 53 69 7a 65 3b 20 20  i64 mxWalSize;  
194f8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
194f9 75 6e 63 61 74 65 20 57 41 4c 20 74 6f 20 74 68  uncate WAL to th
194fa 69 73 20 73 69 7a 65 20 75 70 6f 6e 20 72 65 73  is size upon res
194fb 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 57 69 44  et */.  int nWiD
194fc 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  ata;            
194fd 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72     /* Size of ar
194fe 72 61 79 20 61 70 57 69 44 61 74 61 20 2a 2f 0a  ray apWiData */.
194ff 20 20 69 6e 74 20 73 7a 46 69 72 73 74 42 6c 6f    int szFirstBlo
19500 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ck;          /* 
19501 53 69 7a 65 20 6f 66 20 66 69 72 73 74 20 62 6c  Size of first bl
19502 6f 63 6b 20 77 72 69 74 74 65 6e 20 74 6f 20 57  ock written to W
19503 41 4c 20 66 69 6c 65 20 2a 2f 0a 20 20 76 6f 6c  AL file */.  vol
19504 61 74 69 6c 65 20 75 33 32 20 2a 2a 61 70 57 69  atile u32 **apWi
19505 44 61 74 61 3b 20 20 20 2f 2a 20 50 6f 69 6e 74  Data;   /* Point
19506 65 72 20 74 6f 20 77 61 6c 2d 69 6e 64 65 78 20  er to wal-index 
19507 63 6f 6e 74 65 6e 74 20 69 6e 20 6d 65 6d 6f 72  content in memor
19508 79 20 2a 2f 0a 20 20 75 33 32 20 73 7a 50 61 67  y */.  u32 szPag
19509 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1950a 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 70 61    /* Database pa
1950b 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 31 36  ge size */.  i16
1950c 20 72 65 61 64 4c 6f 63 6b 3b 20 20 20 20 20 20   readLock;      
1950d 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68          /* Which
1950e 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 62 65   read lock is be
1950f 69 6e 67 20 68 65 6c 64 2e 20 20 2d 31 20 66 6f  ing held.  -1 fo
19510 72 20 6e 6f 6e 65 20 2a 2f 0a 20 20 75 38 20 73  r none */.  u8 s
19511 79 6e 63 46 6c 61 67 73 3b 20 20 20 20 20 20 20  yncFlags;       
19512 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
19513 74 6f 20 75 73 65 20 74 6f 20 73 79 6e 63 20 68  to use to sync h
19514 65 61 64 65 72 20 77 72 69 74 65 73 20 2a 2f 0a  eader writes */.
19515 20 20 75 38 20 65 78 63 6c 75 73 69 76 65 4d 6f    u8 exclusiveMo
19516 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  de;          /* 
19517 4e 6f 6e 2d 7a 65 72 6f 20 69 66 20 63 6f 6e 6e  Non-zero if conn
19518 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 65 78 63  ection is in exc
19519 6c 75 73 69 76 65 20 6d 6f 64 65 20 2a 2f 0a 20  lusive mode */. 
1951a 20 75 38 20 77 72 69 74 65 4c 6f 63 6b 3b 20 20   u8 writeLock;  
1951b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1951c 72 75 65 20 69 66 20 69 6e 20 61 20 77 72 69 74  rue if in a writ
1951d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f  e transaction */
1951e 0a 20 20 75 38 20 63 6b 70 74 4c 6f 63 6b 3b 20  .  u8 ckptLock; 
1951f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19520 20 54 72 75 65 20 69 66 20 68 6f 6c 64 69 6e 67   True if holding
19521 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 20 6c 6f   a checkpoint lo
19522 63 6b 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f  ck */.  u8 readO
19523 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  nly;            
19524 20 20 20 2f 2a 20 57 41 4c 5f 52 44 57 52 2c 20     /* WAL_RDWR, 
19525 57 41 4c 5f 52 44 4f 4e 4c 59 2c 20 6f 72 20 57  WAL_RDONLY, or W
19526 41 4c 5f 53 48 4d 5f 52 44 4f 4e 4c 59 20 2a 2f  AL_SHM_RDONLY */
19527 0a 20 20 75 38 20 74 72 75 6e 63 61 74 65 4f 6e  .  u8 truncateOn
19528 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 2f 2a  Commit;       /*
19529 20 54 72 75 65 20 74 6f 20 74 72 75 6e 63 61 74   True to truncat
1952a 65 20 57 41 4c 20 66 69 6c 65 20 6f 6e 20 63 6f  e WAL file on co
1952b 6d 6d 69 74 20 2a 2f 0a 20 20 75 38 20 73 79 6e  mmit */.  u8 syn
1952c 63 48 65 61 64 65 72 3b 20 20 20 20 20 20 20 20  cHeader;        
1952d 20 20 20 20 20 2f 2a 20 46 73 79 6e 63 20 74 68       /* Fsync th
1952e 65 20 57 41 4c 20 68 65 61 64 65 72 20 69 66 20  e WAL header if 
1952f 74 72 75 65 20 2a 2f 0a 20 20 75 38 20 70 61 64  true */.  u8 pad
19530 54 6f 53 65 63 74 6f 72 42 6f 75 6e 64 61 72 79  ToSectorBoundary
19531 3b 20 20 20 20 2f 2a 20 50 61 64 20 74 72 61 6e  ;    /* Pad tran
19532 73 61 63 74 69 6f 6e 73 20 6f 75 74 20 74 6f 20  sactions out to 
19533 74 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20  the next sector 
19534 2a 2f 0a 20 20 57 61 6c 49 6e 64 65 78 48 64 72  */.  WalIndexHdr
19535 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20 20   hdr;           
19536 2f 2a 20 57 61 6c 2d 69 6e 64 65 78 20 68 65 61  /* Wal-index hea
19537 64 65 72 20 66 6f 72 20 63 75 72 72 65 6e 74 20  der for current 
19538 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20  transaction */. 
19539 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 57 61   const char *zWa
1953a 6c 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e  lName;      /* N
1953b 61 6d 65 20 6f 66 20 57 41 4c 20 66 69 6c 65 20  ame of WAL file 
1953c 2a 2f 0a 20 20 75 33 32 20 6e 43 6b 70 74 3b 20  */.  u32 nCkpt; 
1953d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1953e 2f 2a 20 43 68 65 63 6b 70 6f 69 6e 74 20 73 65  /* Checkpoint se
1953f 71 75 65 6e 63 65 20 63 6f 75 6e 74 65 72 20 69  quence counter i
19540 6e 20 74 68 65 20 77 61 6c 2d 68 65 61 64 65 72  n the wal-header
19541 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
19542 45 5f 44 45 42 55 47 0a 20 20 75 38 20 6c 6f 63  E_DEBUG.  u8 loc
19543 6b 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20  kError;         
19544 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
19545 61 20 6c 6f 63 6b 69 6e 67 20 65 72 72 6f 72 20  a locking error 
19546 68 61 73 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a  has occurred */.
19547 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  #endif.};../*.**
19548 20 43 61 6e 64 69 64 61 74 65 20 76 61 6c 75 65   Candidate value
19549 73 20 66 6f 72 20 57 61 6c 2e 65 78 63 6c 75 73  s for Wal.exclus
1954a 69 76 65 4d 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66  iveMode..*/.#def
1954b 69 6e 65 20 57 41 4c 5f 4e 4f 52 4d 41 4c 5f 4d  ine WAL_NORMAL_M
1954c 4f 44 45 20 20 20 20 20 30 0a 23 64 65 66 69 6e  ODE     0.#defin
1954d 65 20 57 41 4c 5f 45 58 43 4c 55 53 49 56 45 5f  e WAL_EXCLUSIVE_
1954e 4d 4f 44 45 20 20 31 20 20 20 20 20 0a 23 64 65  MODE  1     .#de
1954f 66 69 6e 65 20 57 41 4c 5f 48 45 41 50 4d 45 4d  fine WAL_HEAPMEM
19550 4f 52 59 5f 4d 4f 44 45 20 32 0a 0a 2f 2a 0a 2a  ORY_MODE 2../*.*
19551 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65  * Possible value
19552 73 20 66 6f 72 20 57 41 4c 2e 72 65 61 64 4f 6e  s for WAL.readOn
19553 6c 79 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 41  ly.*/.#define WA
19554 4c 5f 52 44 57 52 20 20 20 20 20 20 20 20 30 20  L_RDWR        0 
19555 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 72 65 61     /* Normal rea
19556 64 2f 77 72 69 74 65 20 63 6f 6e 6e 65 63 74 69  d/write connecti
19557 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 41  on */.#define WA
19558 4c 5f 52 44 4f 4e 4c 59 20 20 20 20 20 20 31 20  L_RDONLY      1 
19559 20 20 20 2f 2a 20 54 68 65 20 57 41 4c 20 66 69     /* The WAL fi
1955a 6c 65 20 69 73 20 72 65 61 64 6f 6e 6c 79 20 2a  le is readonly *
1955b 2f 0a 23 64 65 66 69 6e 65 20 57 41 4c 5f 53 48  /.#define WAL_SH
1955c 4d 5f 52 44 4f 4e 4c 59 20 20 32 20 20 20 20 2f  M_RDONLY  2    /
1955d 2a 20 54 68 65 20 53 48 4d 20 66 69 6c 65 20 69  * The SHM file i
1955e 73 20 72 65 61 64 6f 6e 6c 79 20 2a 2f 0a 0a 2f  s readonly */../
1955f 2a 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 20 6f  *.** Each page o
19560 66 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20  f the wal-index 
19561 6d 61 70 70 69 6e 67 20 63 6f 6e 74 61 69 6e 73  mapping contains
19562 20 61 20 68 61 73 68 2d 74 61 62 6c 65 20 6d 61   a hash-table ma
19563 64 65 20 75 70 20 6f 66 0a 2a 2a 20 61 6e 20 61  de up of.** an a
19564 72 72 61 79 20 6f 66 20 48 41 53 48 54 41 42 4c  rray of HASHTABL
19565 45 5f 4e 53 4c 4f 54 20 65 6c 65 6d 65 6e 74 73  E_NSLOT elements
19566 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
19567 67 20 74 79 70 65 2e 0a 2a 2f 0a 74 79 70 65 64  g type..*/.typed
19568 65 66 20 75 31 36 20 68 74 5f 73 6c 6f 74 3b 0a  ef u16 ht_slot;.
19569 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75  ./*.** This stru
1956a 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f  cture is used to
1956b 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 74   implement an it
1956c 65 72 61 74 6f 72 20 74 68 61 74 20 6c 6f 6f 70  erator that loop
1956d 73 20 74 68 72 6f 75 67 68 0a 2a 2a 20 61 6c 6c  s through.** all
1956e 20 66 72 61 6d 65 73 20 69 6e 20 74 68 65 20 57   frames in the W
1956f 41 4c 20 69 6e 20 64 61 74 61 62 61 73 65 20 70  AL in database p
19570 61 67 65 20 6f 72 64 65 72 2e 20 57 68 65 72 65  age order. Where
19571 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 66 72 61   two or more fra
19572 6d 65 73 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  mes.** correspon
19573 64 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61  d to the same da
19574 74 61 62 61 73 65 20 70 61 67 65 2c 20 74 68 65  tabase page, the
19575 20 69 74 65 72 61 74 6f 72 20 76 69 73 69 74 73   iterator visits
19576 20 6f 6e 6c 79 20 74 68 65 20 0a 2a 2a 20 66 72   only the .** fr
19577 61 6d 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  ame most recentl
19578 79 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  y written to the
19579 20 57 41 4c 20 28 69 6e 20 6f 74 68 65 72 20 77   WAL (in other w
1957a 6f 72 64 73 2c 20 74 68 65 20 66 72 61 6d 65 20  ords, the frame 
1957b 77 69 74 68 0a 2a 2a 20 74 68 65 20 6c 61 72 67  with.** the larg
1957c 65 73 74 20 69 6e 64 65 78 29 2e 0a 2a 2a 0a 2a  est index)..**.*
1957d 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20  * The internals 
1957e 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
1957f 65 20 61 72 65 20 6f 6e 6c 79 20 61 63 63 65 73  e are only acces
19580 73 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a 20 20 20  sed by:.**.**   
19581 77 61 6c 49 74 65 72 61 74 6f 72 49 6e 69 74 28  walIteratorInit(
19582 29 20 2d 20 43 72 65 61 74 65 20 61 20 6e 65 77  ) - Create a new
19583 20 69 74 65 72 61 74 6f 72 2c 0a 2a 2a 20 20 20   iterator,.**   
19584 77 61 6c 49 74 65 72 61 74 6f 72 4e 65 78 74 28  walIteratorNext(
19585 29 20 2d 20 53 74 65 70 20 61 6e 20 69 74 65 72  ) - Step an iter
19586 61 74 6f 72 2c 0a 2a 2a 20 20 20 77 61 6c 49 74  ator,.**   walIt
19587 65 72 61 74 6f 72 46 72 65 65 28 29 20 2d 20 46  eratorFree() - F
19588 72 65 65 20 61 6e 20 69 74 65 72 61 74 6f 72 2e  ree an iterator.
19589 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
1958a 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 75 73 65  tionality is use
1958b 64 20 62 79 20 74 68 65 20 63 68 65 63 6b 70 6f  d by the checkpo
1958c 69 6e 74 20 63 6f 64 65 20 28 73 65 65 20 77 61  int code (see wa
1958d 6c 43 68 65 63 6b 70 6f 69 6e 74 28 29 29 2e 0a  lCheckpoint())..
1958e 2a 2f 0a 73 74 72 75 63 74 20 57 61 6c 49 74 65  */.struct WalIte
1958f 72 61 74 6f 72 20 7b 0a 20 20 69 6e 74 20 69 50  rator {.  int iP
19590 72 69 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20  rior;           
19591 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73            /* Las
19592 74 20 72 65 73 75 6c 74 20 72 65 74 75 72 6e 65  t result returne
19593 64 20 66 72 6f 6d 20 74 68 65 20 69 74 65 72 61  d from the itera
19594 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65  tor */.  int nSe
19595 67 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20  gment;          
19596 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
19597 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
19598 20 61 53 65 67 6d 65 6e 74 5b 5d 20 2a 2f 0a 20   aSegment[] */. 
19599 20 73 74 72 75 63 74 20 57 61 6c 53 65 67 6d 65   struct WalSegme
1959a 6e 74 20 7b 0a 20 20 20 20 69 6e 74 20 69 4e 65  nt {.    int iNe
1959b 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  xt;             
1959c 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 73         /* Next s
1959d 6c 6f 74 20 69 6e 20 61 49 6e 64 65 78 5b 5d 20  lot in aIndex[] 
1959e 6e 6f 74 20 79 65 74 20 72 65 74 75 72 6e 65 64  not yet returned
1959f 20 2a 2f 0a 20 20 20 20 68 74 5f 73 6c 6f 74 20   */.    ht_slot 
195a0 2a 61 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20  *aIndex;        
195a1 20 20 20 20 20 20 2f 2a 20 69 30 2c 20 69 31 2c        /* i0, i1,
195a2 20 69 32 2e 2e 2e 20 73 75 63 68 20 74 68 61 74   i2... such that
195a3 20 61 50 67 6e 6f 5b 69 4e 5d 20 61 73 63 65 6e   aPgno[iN] ascen
195a4 64 20 2a 2f 0a 20 20 20 20 75 33 32 20 2a 61 50  d */.    u32 *aP
195a5 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  gno;            
195a6 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
195a7 6f 66 20 70 61 67 65 20 6e 75 6d 62 65 72 73 2e  of page numbers.
195a8 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 6e 74   */.    int nEnt
195a9 72 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ry;             
195aa 20 20 20 20 20 20 2f 2a 20 4e 72 2e 20 6f 66 20        /* Nr. of 
195ab 65 6e 74 72 69 65 73 20 69 6e 20 61 50 67 6e 6f  entries in aPgno
195ac 5b 5d 20 61 6e 64 20 61 49 6e 64 65 78 5b 5d 20  [] and aIndex[] 
195ad 2a 2f 0a 20 20 20 20 69 6e 74 20 69 5a 65 72 6f  */.    int iZero
195ae 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
195af 20 20 20 20 20 2f 2a 20 46 72 61 6d 65 20 6e 75       /* Frame nu
195b0 6d 62 65 72 20 61 73 73 6f 63 69 61 74 65 64 20  mber associated 
195b1 77 69 74 68 20 61 50 67 6e 6f 5b 30 5d 20 2a 2f  with aPgno[0] */
195b2 0a 20 20 7d 20 61 53 65 67 6d 65 6e 74 5b 31 5d  .  } aSegment[1]
195b3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
195b4 20 20 20 2f 2a 20 4f 6e 65 20 66 6f 72 20 65 76     /* One for ev
195b5 65 72 79 20 33 32 4b 42 20 70 61 67 65 20 69 6e  ery 32KB page in
195b6 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 2a   the wal-index *
195b7 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69  /.};../*.** Defi
195b8 6e 65 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  ne the parameter
195b9 73 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61  s of the hash ta
195ba 62 6c 65 73 20 69 6e 20 74 68 65 20 77 61 6c 2d  bles in the wal-
195bb 69 6e 64 65 78 20 66 69 6c 65 2e 20 54 68 65 72  index file. Ther
195bc 65 0a 2a 2a 20 69 73 20 61 20 68 61 73 68 2d 74  e.** is a hash-t
195bd 61 62 6c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65  able following e
195be 76 65 72 79 20 48 41 53 48 54 41 42 4c 45 5f 4e  very HASHTABLE_N
195bf 50 41 47 45 20 70 61 67 65 20 6e 75 6d 62 65 72  PAGE page number
195c0 73 20 69 6e 20 74 68 65 0a 2a 2a 20 77 61 6c 2d  s in the.** wal-
195c1 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 43 68 61  index..**.** Cha
195c2 6e 67 69 6e 67 20 61 6e 79 20 6f 66 20 74 68 65  nging any of the
195c3 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 6c  se constants wil
195c4 6c 20 61 6c 74 65 72 20 74 68 65 20 77 61 6c 2d  l alter the wal-
195c5 69 6e 64 65 78 20 66 6f 72 6d 61 74 20 61 6e 64  index format and
195c6 0a 2a 2a 20 63 72 65 61 74 65 20 69 6e 63 6f 6d  .** create incom
195c7 70 61 74 69 62 69 6c 69 74 69 65 73 2e 0a 2a 2f  patibilities..*/
195c8 0a 23 64 65 66 69 6e 65 20 48 41 53 48 54 41 42  .#define HASHTAB
195c9 4c 45 5f 4e 50 41 47 45 20 20 20 20 20 20 34 30  LE_NPAGE      40
195ca 39 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20  96              
195cb 20 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 70 6f     /* Must be po
195cc 77 65 72 20 6f 66 20 32 20 2a 2f 0a 23 64 65 66  wer of 2 */.#def
195cd 69 6e 65 20 48 41 53 48 54 41 42 4c 45 5f 48 41  ine HASHTABLE_HA
195ce 53 48 5f 31 20 20 20 20 20 33 38 33 20 20 20 20  SH_1     383    
195cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
195d0 20 53 68 6f 75 6c 64 20 62 65 20 70 72 69 6d 65   Should be prime
195d1 20 2a 2f 0a 23 64 65 66 69 6e 65 20 48 41 53 48   */.#define HASH
195d2 54 41 42 4c 45 5f 4e 53 4c 4f 54 20 20 20 20 20  TABLE_NSLOT     
195d3 20 28 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47   (HASHTABLE_NPAG
195d4 45 2a 32 29 20 20 2f 2a 20 4d 75 73 74 20 62 65  E*2)  /* Must be
195d5 20 61 20 70 6f 77 65 72 20 6f 66 20 32 20 2a 2f   a power of 2 */
195d6 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 62 6c 6f  ../* .** The blo
195d7 63 6b 20 6f 66 20 70 61 67 65 20 6e 75 6d 62 65  ck of page numbe
195d8 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  rs associated wi
195d9 74 68 20 74 68 65 20 66 69 72 73 74 20 68 61 73  th the first has
195da 68 2d 74 61 62 6c 65 20 69 6e 20 61 0a 2a 2a 20  h-table in a.** 
195db 77 61 6c 2d 69 6e 64 65 78 20 69 73 20 73 6d 61  wal-index is sma
195dc 6c 6c 65 72 20 74 68 61 6e 20 75 73 75 61 6c 2e  ller than usual.
195dd 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 74   This is so that
195de 20 74 68 65 72 65 20 69 73 20 61 20 63 6f 6d 70   there is a comp
195df 6c 65 74 65 0a 2a 2a 20 68 61 73 68 2d 74 61 62  lete.** hash-tab
195e0 6c 65 20 6f 6e 20 65 61 63 68 20 61 6c 69 67 6e  le on each align
195e1 65 64 20 33 32 4b 42 20 70 61 67 65 20 6f 66 20  ed 32KB page of 
195e2 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 2e 0a 2a  the wal-index..*
195e3 2f 0a 23 64 65 66 69 6e 65 20 48 41 53 48 54 41  /.#define HASHTA
195e4 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45 20 20 28  BLE_NPAGE_ONE  (
195e5 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 20  HASHTABLE_NPAGE 
195e6 2d 20 28 57 41 4c 49 4e 44 45 58 5f 48 44 52 5f  - (WALINDEX_HDR_
195e7 53 49 5a 45 2f 73 69 7a 65 6f 66 28 75 33 32 29  SIZE/sizeof(u32)
195e8 29 29 0a 0a 2f 2a 20 54 68 65 20 77 61 6c 2d 69  ))../* The wal-i
195e9 6e 64 65 78 20 69 73 20 64 69 76 69 64 65 64 20  ndex is divided 
195ea 69 6e 74 6f 20 70 61 67 65 73 20 6f 66 20 57 41  into pages of WA
195eb 4c 49 4e 44 45 58 5f 50 47 53 5a 20 62 79 74 65  LINDEX_PGSZ byte
195ec 73 20 65 61 63 68 2e 20 2a 2f 0a 23 64 65 66 69  s each. */.#defi
195ed 6e 65 20 57 41 4c 49 4e 44 45 58 5f 50 47 53 5a  ne WALINDEX_PGSZ
195ee 20 20 20 28 20 20 20 20 20 20 20 20 20 20 20 20     (            
195ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
195f0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
195f1 20 20 20 73 69 7a 65 6f 66 28 68 74 5f 73 6c 6f     sizeof(ht_slo
195f2 74 29 2a 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c  t)*HASHTABLE_NSL
195f3 4f 54 20 2b 20 48 41 53 48 54 41 42 4c 45 5f 4e  OT + HASHTABLE_N
195f4 50 41 47 45 2a 73 69 7a 65 6f 66 28 75 33 32 29  PAGE*sizeof(u32)
195f5 20 5c 0a 29 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61   \.)../*.** Obta
195f6 69 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  in a pointer to 
195f7 74 68 65 20 69 50 61 67 65 27 74 68 20 70 61 67  the iPage'th pag
195f8 65 20 6f 66 20 74 68 65 20 77 61 6c 2d 69 6e 64  e of the wal-ind
195f9 65 78 2e 20 54 68 65 20 77 61 6c 2d 69 6e 64 65  ex. The wal-inde
195fa 78 0a 2a 2a 20 69 73 20 62 72 6f 6b 65 6e 20 69  x.** is broken i
195fb 6e 74 6f 20 70 61 67 65 73 20 6f 66 20 57 41 4c  nto pages of WAL
195fc 49 4e 44 45 58 5f 50 47 53 5a 20 62 79 74 65 73  INDEX_PGSZ bytes
195fd 2e 20 57 61 6c 2d 69 6e 64 65 78 20 70 61 67 65  . Wal-index page
195fe 73 20 61 72 65 0a 2a 2a 20 6e 75 6d 62 65 72 65  s are.** numbere
195ff 64 20 66 72 6f 6d 20 7a 65 72 6f 2e 0a 2a 2a 0a  d from zero..**.
19600 2a 2a 20 49 66 20 74 68 69 73 20 63 61 6c 6c 20  ** If this call 
19601 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a  is successful, *
19602 70 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f  ppPage is set to
19603 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 77 61   point to the wa
19604 6c 2d 69 6e 64 65 78 0a 2a 2a 20 70 61 67 65 20  l-index.** page 
19605 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  and SQLITE_OK is
19606 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e   returned. If an
19607 20 65 72 72 6f 72 20 28 61 6e 20 4f 4f 4d 20 6f   error (an OOM o
19608 72 20 56 46 53 20 65 72 72 6f 72 29 20 6f 63 63  r VFS error) occ
19609 75 72 73 2c 0a 2a 2a 20 74 68 65 6e 20 61 6e 20  urs,.** then an 
1960a 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
1960b 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  e is returned an
1960c 64 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74  d *ppPage is set
1960d 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63   to 0..*/.static
1960e 20 69 6e 74 20 77 61 6c 49 6e 64 65 78 50 61 67   int walIndexPag
1960f 65 28 57 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74  e(Wal *pWal, int
19610 20 69 50 61 67 65 2c 20 76 6f 6c 61 74 69 6c 65   iPage, volatile
19611 20 75 33 32 20 2a 2a 70 70 50 61 67 65 29 7b 0a   u32 **ppPage){.
19612 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
19613 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 45 6e 6c 61  E_OK;..  /* Enla
19614 72 67 65 20 74 68 65 20 70 57 61 6c 2d 3e 61 70  rge the pWal->ap
19615 57 69 44 61 74 61 5b 5d 20 61 72 72 61 79 20 69  WiData[] array i
19616 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20  f required */.  
19617 69 66 28 20 70 57 61 6c 2d 3e 6e 57 69 44 61 74  if( pWal->nWiDat
19618 61 3c 3d 69 50 61 67 65 20 29 7b 0a 20 20 20 20  a<=iPage ){.    
19619 69 6e 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65  int nByte = size
1961a 6f 66 28 75 33 32 2a 29 2a 28 69 50 61 67 65 2b  of(u32*)*(iPage+
1961b 31 29 3b 0a 20 20 20 20 76 6f 6c 61 74 69 6c 65  1);.    volatile
1961c 20 75 33 32 20 2a 2a 61 70 4e 65 77 3b 0a 20 20   u32 **apNew;.  
1961d 20 20 61 70 4e 65 77 20 3d 20 28 76 6f 6c 61 74    apNew = (volat
1961e 69 6c 65 20 75 33 32 20 2a 2a 29 73 71 6c 69 74  ile u32 **)sqlit
1961f 65 33 5f 72 65 61 6c 6c 6f 63 28 28 76 6f 69 64  e3_realloc((void
19620 20 2a 29 70 57 61 6c 2d 3e 61 70 57 69 44 61 74   *)pWal->apWiDat
19621 61 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 69  a, nByte);.    i
19622 66 28 20 21 61 70 4e 65 77 20 29 7b 0a 20 20 20  f( !apNew ){.   
19623 20 20 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a     *ppPage = 0;.
19624 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
19625 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
19626 0a 20 20 20 20 6d 65 6d 73 65 74 28 28 76 6f 69  .    memset((voi
19627 64 2a 29 26 61 70 4e 65 77 5b 70 57 61 6c 2d 3e  d*)&apNew[pWal->
19628 6e 57 69 44 61 74 61 5d 2c 20 30 2c 0a 20 20 20  nWiData], 0,.   
19629 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 75          sizeof(u
1962a 33 32 2a 29 2a 28 69 50 61 67 65 2b 31 2d 70 57  32*)*(iPage+1-pW
1962b 61 6c 2d 3e 6e 57 69 44 61 74 61 29 29 3b 0a 20  al->nWiData));. 
1962c 20 20 20 70 57 61 6c 2d 3e 61 70 57 69 44 61 74     pWal->apWiDat
1962d 61 20 3d 20 61 70 4e 65 77 3b 0a 20 20 20 20 70  a = apNew;.    p
1962e 57 61 6c 2d 3e 6e 57 69 44 61 74 61 20 3d 20 69  Wal->nWiData = i
1962f 50 61 67 65 2b 31 3b 0a 20 20 7d 0a 0a 20 20 2f  Page+1;.  }..  /
19630 2a 20 52 65 71 75 65 73 74 20 61 20 70 6f 69 6e  * Request a poin
19631 74 65 72 20 74 6f 20 74 68 65 20 72 65 71 75 69  ter to the requi
19632 72 65 64 20 70 61 67 65 20 66 72 6f 6d 20 74 68  red page from th
19633 65 20 56 46 53 20 2a 2f 0a 20 20 69 66 28 20 70  e VFS */.  if( p
19634 57 61 6c 2d 3e 61 70 57 69 44 61 74 61 5b 69 50  Wal->apWiData[iP
19635 61 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 69  age]==0 ){.    i
19636 66 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69  f( pWal->exclusi
19637 76 65 4d 6f 64 65 3d 3d 57 41 4c 5f 48 45 41 50  veMode==WAL_HEAP
19638 4d 45 4d 4f 52 59 5f 4d 4f 44 45 20 29 7b 0a 20  MEMORY_MODE ){. 
19639 20 20 20 20 20 70 57 61 6c 2d 3e 61 70 57 69 44       pWal->apWiD
1963a 61 74 61 5b 69 50 61 67 65 5d 20 3d 20 28 75 33  ata[iPage] = (u3
1963b 32 20 76 6f 6c 61 74 69 6c 65 20 2a 29 73 71 6c  2 volatile *)sql
1963c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 57  ite3MallocZero(W
1963d 41 4c 49 4e 44 45 58 5f 50 47 53 5a 29 3b 0a 20  ALINDEX_PGSZ);. 
1963e 20 20 20 20 20 69 66 28 20 21 70 57 61 6c 2d 3e       if( !pWal->
1963f 61 70 57 69 44 61 74 61 5b 69 50 61 67 65 5d 20  apWiData[iPage] 
19640 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ) rc = SQLITE_NO
19641 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  MEM;.    }else{.
19642 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
19643 65 33 4f 73 53 68 6d 4d 61 70 28 70 57 61 6c 2d  e3OsShmMap(pWal-
19644 3e 70 44 62 46 64 2c 20 69 50 61 67 65 2c 20 57  >pDbFd, iPage, W
19645 41 4c 49 4e 44 45 58 5f 50 47 53 5a 2c 20 0a 20  ALINDEX_PGSZ, . 
19646 20 20 20 20 20 20 20 20 20 70 57 61 6c 2d 3e 77           pWal->w
19647 72 69 74 65 4c 6f 63 6b 2c 20 28 76 6f 69 64 20  riteLock, (void 
19648 76 6f 6c 61 74 69 6c 65 20 2a 2a 29 26 70 57 61  volatile **)&pWa
19649 6c 2d 3e 61 70 57 69 44 61 74 61 5b 69 50 61 67  l->apWiData[iPag
1964a 65 5d 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  e].      );.    
1964b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1964c 5f 52 45 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20  _READONLY ){.   
1964d 20 20 20 20 20 70 57 61 6c 2d 3e 72 65 61 64 4f       pWal->readO
1964e 6e 6c 79 20 7c 3d 20 57 41 4c 5f 53 48 4d 5f 52  nly |= WAL_SHM_R
1964f 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 20 20 72  DONLY;.        r
19650 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
19651 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
19652 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 57  ..  *ppPage = pW
19653 61 6c 2d 3e 61 70 57 69 44 61 74 61 5b 69 50 61  al->apWiData[iPa
19654 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ge];.  assert( i
19655 50 61 67 65 3d 3d 30 20 7c 7c 20 2a 70 70 50 61  Page==0 || *ppPa
19656 67 65 20 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45  ge || rc!=SQLITE
19657 5f 4f 4b 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  _OK );.  return 
19658 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
19659 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
1965a 20 74 68 65 20 57 61 6c 43 6b 70 74 49 6e 66 6f   the WalCkptInfo
1965b 20 73 74 72 75 63 74 75 72 65 20 69 6e 20 74 68   structure in th
1965c 65 20 77 61 6c 2d 69 6e 64 65 78 2e 0a 2a 2f 0a  e wal-index..*/.
1965d 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65 20  static volatile 
1965e 57 61 6c 43 6b 70 74 49 6e 66 6f 20 2a 77 61 6c  WalCkptInfo *wal
1965f 43 6b 70 74 49 6e 66 6f 28 57 61 6c 20 2a 70 57  CkptInfo(Wal *pW
19660 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  al){.  assert( p
19661 57 61 6c 2d 3e 6e 57 69 44 61 74 61 3e 30 20 26  Wal->nWiData>0 &
19662 26 20 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 61  & pWal->apWiData
19663 5b 30 5d 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  [0] );.  return 
19664 28 76 6f 6c 61 74 69 6c 65 20 57 61 6c 43 6b 70  (volatile WalCkp
19665 74 49 6e 66 6f 2a 29 26 28 70 57 61 6c 2d 3e 61  tInfo*)&(pWal->a
19666 70 57 69 44 61 74 61 5b 30 5d 5b 73 69 7a 65 6f  pWiData[0][sizeo
19667 66 28 57 61 6c 49 6e 64 65 78 48 64 72 29 2f 32  f(WalIndexHdr)/2
19668 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ]);.}../*.** Ret
19669 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
1966a 20 74 68 65 20 57 61 6c 49 6e 64 65 78 48 64 72   the WalIndexHdr
1966b 20 73 74 72 75 63 74 75 72 65 20 69 6e 20 74 68   structure in th
1966c 65 20 77 61 6c 2d 69 6e 64 65 78 2e 0a 2a 2f 0a  e wal-index..*/.
1966d 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65 20  static volatile 
1966e 57 61 6c 49 6e 64 65 78 48 64 72 20 2a 77 61 6c  WalIndexHdr *wal
1966f 49 6e 64 65 78 48 64 72 28 57 61 6c 20 2a 70 57  IndexHdr(Wal *pW
19670 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  al){.  assert( p
19671 57 61 6c 2d 3e 6e 57 69 44 61 74 61 3e 30 20 26  Wal->nWiData>0 &
19672 26 20 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 61  & pWal->apWiData
19673 5b 30 5d 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  [0] );.  return 
19674 28 76 6f 6c 61 74 69 6c 65 20 57 61 6c 49 6e 64  (volatile WalInd
19675 65 78 48 64 72 2a 29 70 57 61 6c 2d 3e 61 70 57  exHdr*)pWal->apW
19676 69 44 61 74 61 5b 30 5d 3b 0a 7d 0a 0a 2f 2a 0a  iData[0];.}../*.
19677 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20  ** The argument 
19678 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 6d 75  to this macro mu
19679 73 74 20 62 65 20 6f 66 20 74 79 70 65 20 75 33  st be of type u3
1967a 32 2e 20 4f 6e 20 61 20 6c 69 74 74 6c 65 2d 65  2. On a little-e
1967b 6e 64 69 61 6e 0a 2a 2a 20 61 72 63 68 69 74 65  ndian.** archite
1967c 63 74 75 72 65 2c 20 69 74 20 72 65 74 75 72 6e  cture, it return
1967d 73 20 74 68 65 20 75 33 32 20 76 61 6c 75 65 20  s the u32 value 
1967e 74 68 61 74 20 72 65 73 75 6c 74 73 20 66 72 6f  that results fro
1967f 6d 20 69 6e 74 65 72 70 72 65 74 69 6e 67 0a 2a  m interpreting.*
19680 2a 20 74 68 65 20 34 20 62 79 74 65 73 20 61 73  * the 4 bytes as
19681 20 61 20 62 69 67 2d 65 6e 64 69 61 6e 20 76 61   a big-endian va
19682 6c 75 65 2e 20 4f 6e 20 61 20 62 69 67 2d 65 6e  lue. On a big-en
19683 64 69 61 6e 20 61 72 63 68 69 74 65 63 74 75 72  dian architectur
19684 65 2c 20 69 74 0a 2a 2a 20 72 65 74 75 72 6e 73  e, it.** returns
19685 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20   the value that 
19686 77 6f 75 6c 64 20 62 65 20 70 72 6f 64 75 63 65  would be produce
19687 64 20 62 79 20 69 6e 74 65 70 72 65 74 69 6e 67  d by intepreting
19688 20 74 68 65 20 34 20 62 79 74 65 73 0a 2a 2a 20   the 4 bytes.** 
19689 6f 66 20 74 68 65 20 69 6e 70 75 74 20 76 61 6c  of the input val
1968a 75 65 20 61 73 20 61 20 6c 69 74 74 6c 65 2d 65  ue as a little-e
1968b 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a  ndian integer..*
1968c 2f 0a 23 64 65 66 69 6e 65 20 42 59 54 45 53 57  /.#define BYTESW
1968d 41 50 33 32 28 78 29 20 28 20 5c 0a 20 20 20 20  AP32(x) ( \.    
1968e 28 28 28 78 29 26 30 78 30 30 30 30 30 30 46 46  (((x)&0x000000FF
1968f 29 3c 3c 32 34 29 20 2b 20 28 28 28 78 29 26 30  )<<24) + (((x)&0
19690 78 30 30 30 30 46 46 30 30 29 3c 3c 38 29 20 20  x0000FF00)<<8)  
19691 5c 0a 20 20 2b 20 28 28 28 78 29 26 30 78 30 30  \.  + (((x)&0x00
19692 46 46 30 30 30 30 29 3e 3e 38 29 20 20 2b 20 28  FF0000)>>8)  + (
19693 28 28 78 29 26 30 78 46 46 30 30 30 30 30 30 29  ((x)&0xFF000000)
19694 3e 3e 32 34 29 20 5c 0a 29 0a 0a 2f 2a 0a 2a 2a  >>24) \.)../*.**
19695 20 47 65 6e 65 72 61 74 65 20 6f 72 20 65 78 74   Generate or ext
19696 65 6e 64 20 61 6e 20 38 20 62 79 74 65 20 63 68  end an 8 byte ch
19697 65 63 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 20  ecksum based on 
19698 74 68 65 20 64 61 74 61 20 69 6e 20 0a 2a 2a 20  the data in .** 
19699 61 72 72 61 79 20 61 42 79 74 65 5b 5d 20 61 6e  array aByte[] an
1969a 64 20 74 68 65 20 69 6e 69 74 69 61 6c 20 76 61  d the initial va
1969b 6c 75 65 73 20 6f 66 20 61 49 6e 5b 30 5d 20 61  lues of aIn[0] a
1969c 6e 64 20 61 49 6e 5b 31 5d 20 28 6f 72 0a 2a 2a  nd aIn[1] (or.**
1969d 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 73 20   initial values 
1969e 6f 66 20 30 20 61 6e 64 20 30 20 69 66 20 61 49  of 0 and 0 if aI
1969f 6e 3d 3d 4e 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a 20  n==NULL)..**.** 
196a0 54 68 65 20 63 68 65 63 6b 73 75 6d 20 69 73 20  The checksum is 
196a1 77 72 69 74 74 65 6e 20 62 61 63 6b 20 69 6e 74  written back int
196a2 6f 20 61 4f 75 74 5b 5d 20 62 65 66 6f 72 65 20  o aOut[] before 
196a3 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a  returning..**.**
196a4 20 6e 42 79 74 65 20 6d 75 73 74 20 62 65 20 61   nByte must be a
196a5 20 70 6f 73 69 74 69 76 65 20 6d 75 6c 74 69 70   positive multip
196a6 6c 65 20 6f 66 20 38 2e 0a 2a 2f 0a 73 74 61 74  le of 8..*/.stat
196a7 69 63 20 76 6f 69 64 20 77 61 6c 43 68 65 63 6b  ic void walCheck
196a8 73 75 6d 42 79 74 65 73 28 0a 20 20 69 6e 74 20  sumBytes(.  int 
196a9 6e 61 74 69 76 65 43 6b 73 75 6d 2c 20 2f 2a 20  nativeCksum, /* 
196aa 54 72 75 65 20 66 6f 72 20 6e 61 74 69 76 65 20  True for native 
196ab 62 79 74 65 2d 6f 72 64 65 72 2c 20 66 61 6c 73  byte-order, fals
196ac 65 20 66 6f 72 20 6e 6f 6e 2d 6e 61 74 69 76 65  e for non-native
196ad 20 2a 2f 0a 20 20 75 38 20 2a 61 2c 20 20 20 20   */.  u8 *a,    
196ae 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e         /* Conten
196af 74 20 74 6f 20 62 65 20 63 68 65 63 6b 73 75 6d  t to be checksum
196b0 6d 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  med */.  int nBy
196b1 74 65 2c 20 20 20 20 20 20 20 2f 2a 20 42 79 74  te,       /* Byt
196b2 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e  es of content in
196b3 20 61 5b 5d 2e 20 20 4d 75 73 74 20 62 65 20 61   a[].  Must be a
196b4 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e 20   multiple of 8. 
196b5 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 33 32 20 2a  */.  const u32 *
196b6 61 49 6e 2c 20 20 2f 2a 20 49 6e 69 74 69 61 6c  aIn,  /* Initial
196b7 20 63 68 65 63 6b 73 75 6d 20 76 61 6c 75 65 20   checksum value 
196b8 69 6e 70 75 74 20 2a 2f 0a 20 20 75 33 32 20 2a  input */.  u32 *
196b9 61 4f 75 74 20 20 20 20 20 20 20 20 2f 2a 20 4f  aOut        /* O
196ba 55 54 3a 20 46 69 6e 61 6c 20 63 68 65 63 6b 73  UT: Final checks
196bb 75 6d 20 76 61 6c 75 65 20 6f 75 74 70 75 74 20  um value output 
196bc 2a 2f 0a 29 7b 0a 20 20 75 33 32 20 73 31 2c 20  */.){.  u32 s1, 
196bd 73 32 3b 0a 20 20 75 33 32 20 2a 61 44 61 74 61  s2;.  u32 *aData
196be 20 3d 20 28 75 33 32 20 2a 29 61 3b 0a 20 20 75   = (u32 *)a;.  u
196bf 33 32 20 2a 61 45 6e 64 20 3d 20 28 75 33 32 20  32 *aEnd = (u32 
196c0 2a 29 26 61 5b 6e 42 79 74 65 5d 3b 0a 0a 20 20  *)&a[nByte];..  
196c1 69 66 28 20 61 49 6e 20 29 7b 0a 20 20 20 20 73  if( aIn ){.    s
196c2 31 20 3d 20 61 49 6e 5b 30 5d 3b 0a 20 20 20 20  1 = aIn[0];.    
196c3 73 32 20 3d 20 61 49 6e 5b 31 5d 3b 0a 20 20 7d  s2 = aIn[1];.  }
196c4 65 6c 73 65 7b 0a 20 20 20 20 73 31 20 3d 20 73  else{.    s1 = s
196c5 32 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 61 73  2 = 0;.  }..  as
196c6 73 65 72 74 28 20 6e 42 79 74 65 3e 3d 38 20 29  sert( nByte>=8 )
196c7 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 42 79  ;.  assert( (nBy
196c8 74 65 26 30 78 30 30 30 30 30 30 30 37 29 3d 3d  te&0x00000007)==
196c9 30 20 29 3b 0a 0a 20 20 69 66 28 20 6e 61 74 69  0 );..  if( nati
196ca 76 65 43 6b 73 75 6d 20 29 7b 0a 20 20 20 20 64  veCksum ){.    d
196cb 6f 20 7b 0a 20 20 20 20 20 20 73 31 20 2b 3d 20  o {.      s1 += 
196cc 2a 61 44 61 74 61 2b 2b 20 2b 20 73 32 3b 0a 20  *aData++ + s2;. 
196cd 20 20 20 20 20 73 32 20 2b 3d 20 2a 61 44 61 74       s2 += *aDat
196ce 61 2b 2b 20 2b 20 73 31 3b 0a 20 20 20 20 7d 77  a++ + s1;.    }w
196cf 68 69 6c 65 28 20 61 44 61 74 61 3c 61 45 6e 64  hile( aData<aEnd
196d0 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
196d1 20 64 6f 20 7b 0a 20 20 20 20 20 20 73 31 20 2b   do {.      s1 +
196d2 3d 20 42 59 54 45 53 57 41 50 33 32 28 61 44 61  = BYTESWAP32(aDa
196d3 74 61 5b 30 5d 29 20 2b 20 73 32 3b 0a 20 20 20  ta[0]) + s2;.   
196d4 20 20 20 73 32 20 2b 3d 20 42 59 54 45 53 57 41     s2 += BYTESWA
196d5 50 33 32 28 61 44 61 74 61 5b 31 5d 29 20 2b 20  P32(aData[1]) + 
196d6 73 31 3b 0a 20 20 20 20 20 20 61 44 61 74 61 20  s1;.      aData 
196d7 2b 3d 20 32 3b 0a 20 20 20 20 7d 77 68 69 6c 65  += 2;.    }while
196d8 28 20 61 44 61 74 61 3c 61 45 6e 64 20 29 3b 0a  ( aData<aEnd );.
196d9 20 20 7d 0a 0a 20 20 61 4f 75 74 5b 30 5d 20 3d    }..  aOut[0] =
196da 20 73 31 3b 0a 20 20 61 4f 75 74 5b 31 5d 20 3d   s1;.  aOut[1] =
196db 20 73 32 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76   s2;.}..static v
196dc 6f 69 64 20 77 61 6c 53 68 6d 42 61 72 72 69 65  oid walShmBarrie
196dd 72 28 57 61 6c 20 2a 70 57 61 6c 29 7b 0a 20 20  r(Wal *pWal){.  
196de 69 66 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73  if( pWal->exclus
196df 69 76 65 4d 6f 64 65 21 3d 57 41 4c 5f 48 45 41  iveMode!=WAL_HEA
196e0 50 4d 45 4d 4f 52 59 5f 4d 4f 44 45 20 29 7b 0a  PMEMORY_MODE ){.
196e1 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53 68 6d      sqlite3OsShm
196e2 42 61 72 72 69 65 72 28 70 57 61 6c 2d 3e 70 44  Barrier(pWal->pD
196e3 62 46 64 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  bFd);.  }.}../*.
196e4 2a 2a 20 57 72 69 74 65 20 74 68 65 20 68 65 61  ** Write the hea
196e5 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  der information 
196e6 69 6e 20 70 57 61 6c 2d 3e 68 64 72 20 69 6e 74  in pWal->hdr int
196e7 6f 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 2e  o the wal-index.
196e8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 68 65 63 6b  .**.** The check
196e9 73 75 6d 20 6f 6e 20 70 57 61 6c 2d 3e 68 64 72  sum on pWal->hdr
196ea 20 69 73 20 75 70 64 61 74 65 64 20 62 65 66 6f   is updated befo
196eb 72 65 20 69 74 20 69 73 20 77 72 69 74 74 65 6e  re it is written
196ec 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
196ed 20 77 61 6c 49 6e 64 65 78 57 72 69 74 65 48 64   walIndexWriteHd
196ee 72 28 57 61 6c 20 2a 70 57 61 6c 29 7b 0a 20 20  r(Wal *pWal){.  
196ef 76 6f 6c 61 74 69 6c 65 20 57 61 6c 49 6e 64 65  volatile WalInde
196f0 78 48 64 72 20 2a 61 48 64 72 20 3d 20 77 61 6c  xHdr *aHdr = wal
196f1 49 6e 64 65 78 48 64 72 28 70 57 61 6c 29 3b 0a  IndexHdr(pWal);.
196f2 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 43 6b 73    const int nCks
196f3 75 6d 20 3d 20 6f 66 66 73 65 74 6f 66 28 57 61  um = offsetof(Wa
196f4 6c 49 6e 64 65 78 48 64 72 2c 20 61 43 6b 73 75  lIndexHdr, aCksu
196f5 6d 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  m);..  assert( p
196f6 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 20 29  Wal->writeLock )
196f7 3b 0a 20 20 70 57 61 6c 2d 3e 68 64 72 2e 69 73  ;.  pWal->hdr.is
196f8 49 6e 69 74 20 3d 20 31 3b 0a 20 20 70 57 61 6c  Init = 1;.  pWal
196f9 2d 3e 68 64 72 2e 69 56 65 72 73 69 6f 6e 20 3d  ->hdr.iVersion =
196fa 20 57 41 4c 49 4e 44 45 58 5f 4d 41 58 5f 56 45   WALINDEX_MAX_VE
196fb 52 53 49 4f 4e 3b 0a 20 20 77 61 6c 43 68 65 63  RSION;.  walChec
196fc 6b 73 75 6d 42 79 74 65 73 28 31 2c 20 28 75 38  ksumBytes(1, (u8
196fd 2a 29 26 70 57 61 6c 2d 3e 68 64 72 2c 20 6e 43  *)&pWal->hdr, nC
196fe 6b 73 75 6d 2c 20 30 2c 20 70 57 61 6c 2d 3e 68  ksum, 0, pWal->h
196ff 64 72 2e 61 43 6b 73 75 6d 29 3b 0a 20 20 6d 65  dr.aCksum);.  me
19700 6d 63 70 79 28 28 76 6f 69 64 20 2a 29 26 61 48  mcpy((void *)&aH
19701 64 72 5b 31 5d 2c 20 28 76 6f 69 64 20 2a 29 26  dr[1], (void *)&
19702 70 57 61 6c 2d 3e 68 64 72 2c 20 73 69 7a 65 6f  pWal->hdr, sizeo
19703 66 28 57 61 6c 49 6e 64 65 78 48 64 72 29 29 3b  f(WalIndexHdr));
19704 0a 20 20 77 61 6c 53 68 6d 42 61 72 72 69 65 72  .  walShmBarrier
19705 28 70 57 61 6c 29 3b 0a 20 20 6d 65 6d 63 70 79  (pWal);.  memcpy
19706 28 28 76 6f 69 64 20 2a 29 26 61 48 64 72 5b 30  ((void *)&aHdr[0
19707 5d 2c 20 28 76 6f 69 64 20 2a 29 26 70 57 61 6c  ], (void *)&pWal
19708 2d 3e 68 64 72 2c 20 73 69 7a 65 6f 66 28 57 61  ->hdr, sizeof(Wa
19709 6c 49 6e 64 65 78 48 64 72 29 29 3b 0a 7d 0a 0a  lIndexHdr));.}..
1970a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
1970b 69 6f 6e 20 65 6e 63 6f 64 65 73 20 61 20 73 69  ion encodes a si
1970c 6e 67 6c 65 20 66 72 61 6d 65 20 68 65 61 64 65  ngle frame heade
1970d 72 20 61 6e 64 20 77 72 69 74 65 73 20 69 74 20  r and writes it 
1970e 74 6f 20 61 20 62 75 66 66 65 72 0a 2a 2a 20 73  to a buffer.** s
1970f 75 70 70 6c 69 65 64 20 62 79 20 74 68 65 20 63  upplied by the c
19710 61 6c 6c 65 72 2e 20 41 20 66 72 61 6d 65 2d 68  aller. A frame-h
19711 65 61 64 65 72 20 69 73 20 6d 61 64 65 20 75 70  eader is made up
19712 20 6f 66 20 61 20 73 65 72 69 65 73 20 6f 66 20   of a series of 
19713 0a 2a 2a 20 34 2d 62 79 74 65 20 62 69 67 2d 65  .** 4-byte big-e
19714 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 73 2c 20  ndian integers, 
19715 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
19716 2a 20 20 20 20 20 30 3a 20 50 61 67 65 20 6e 75  *     0: Page nu
19717 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 34 3a 20  mber..**     4: 
19718 46 6f 72 20 63 6f 6d 6d 69 74 20 72 65 63 6f 72  For commit recor
19719 64 73 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20  ds, the size of 
1971a 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61  the database ima
1971b 67 65 20 69 6e 20 70 61 67 65 73 20 0a 2a 2a 20  ge in pages .** 
1971c 20 20 20 20 20 20 20 61 66 74 65 72 20 74 68 65         after the
1971d 20 63 6f 6d 6d 69 74 2e 20 46 6f 72 20 61 6c 6c   commit. For all
1971e 20 6f 74 68 65 72 20 72 65 63 6f 72 64 73 2c 20   other records, 
1971f 7a 65 72 6f 2e 0a 2a 2a 20 20 20 20 20 38 3a 20  zero..**     8: 
19720 53 61 6c 74 2d 31 20 28 63 6f 70 69 65 64 20 66  Salt-1 (copied f
19721 72 6f 6d 20 74 68 65 20 77 61 6c 2d 68 65 61 64  rom the wal-head
19722 65 72 29 0a 2a 2a 20 20 20 20 31 32 3a 20 53 61  er).**    12: Sa
19723 6c 74 2d 32 20 28 63 6f 70 69 65 64 20 66 72 6f  lt-2 (copied fro
19724 6d 20 74 68 65 20 77 61 6c 2d 68 65 61 64 65 72  m the wal-header
19725 29 0a 2a 2a 20 20 20 20 31 36 3a 20 43 68 65 63  ).**    16: Chec
19726 6b 73 75 6d 2d 31 2e 0a 2a 2a 20 20 20 20 32 30  ksum-1..**    20
19727 3a 20 43 68 65 63 6b 73 75 6d 2d 32 2e 0a 2a 2f  : Checksum-2..*/
19728 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 61 6c  .static void wal
19729 45 6e 63 6f 64 65 46 72 61 6d 65 28 0a 20 20 57  EncodeFrame(.  W
1972a 61 6c 20 2a 70 57 61 6c 2c 20 20 20 20 20 20 20  al *pWal,       
1972b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1972c 2a 20 54 68 65 20 77 72 69 74 65 2d 61 68 65 61  * The write-ahea
1972d 64 20 6c 6f 67 20 2a 2f 0a 20 20 75 33 32 20 69  d log */.  u32 i
1972e 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20  Page,           
1972f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
19730 74 61 62 61 73 65 20 70 61 67 65 20 6e 75 6d 62  tabase page numb
19731 65 72 20 66 6f 72 20 66 72 61 6d 65 20 2a 2f 0a  er for frame */.
19732 20 20 75 33 32 20 6e 54 72 75 6e 63 61 74 65 2c    u32 nTruncate,
19733 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19734 20 20 2f 2a 20 4e 65 77 20 64 62 20 73 69 7a 65    /* New db size
19735 20 28 6f 72 20 30 20 66 6f 72 20 6e 6f 6e 2d 63   (or 0 for non-c
19736 6f 6d 6d 69 74 20 66 72 61 6d 65 73 29 20 2a 2f  ommit frames) */
19737 0a 20 20 75 38 20 2a 61 44 61 74 61 2c 20 20 20  .  u8 *aData,   
19738 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19739 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1973a 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20   page data */.  
1973b 75 38 20 2a 61 46 72 61 6d 65 20 20 20 20 20 20  u8 *aFrame      
1973c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1973d 2f 2a 20 4f 55 54 3a 20 57 72 69 74 65 20 65 6e  /* OUT: Write en
1973e 63 6f 64 65 64 20 66 72 61 6d 65 20 68 65 72 65  coded frame here
1973f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 61 74   */.){.  int nat
19740 69 76 65 43 6b 73 75 6d 3b 20 20 20 20 20 20 20  iveCksum;       
19741 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
19742 20 66 6f 72 20 6e 61 74 69 76 65 20 62 79 74 65   for native byte
19743 2d 6f 72 64 65 72 20 63 68 65 63 6b 73 75 6d 73  -order checksums
19744 20 2a 2f 0a 20 20 75 33 32 20 2a 61 43 6b 73 75   */.  u32 *aCksu
19745 6d 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46  m = pWal->hdr.aF
19746 72 61 6d 65 43 6b 73 75 6d 3b 0a 20 20 61 73 73  rameCksum;.  ass
19747 65 72 74 28 20 57 41 4c 5f 46 52 41 4d 45 5f 48  ert( WAL_FRAME_H
19748 44 52 53 49 5a 45 3d 3d 32 34 20 29 3b 0a 20 20  DRSIZE==24 );.  
19749 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28  sqlite3Put4byte(
1974a 26 61 46 72 61 6d 65 5b 30 5d 2c 20 69 50 61 67  &aFrame[0], iPag
1974b 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 75 74  e);.  sqlite3Put
1974c 34 62 79 74 65 28 26 61 46 72 61 6d 65 5b 34 5d  4byte(&aFrame[4]
1974d 2c 20 6e 54 72 75 6e 63 61 74 65 29 3b 0a 20 20  , nTruncate);.  
1974e 6d 65 6d 63 70 79 28 26 61 46 72 61 6d 65 5b 38  memcpy(&aFrame[8
1974f 5d 2c 20 70 57 61 6c 2d 3e 68 64 72 2e 61 53 61  ], pWal->hdr.aSa
19750 6c 74 2c 20 38 29 3b 0a 0a 20 20 6e 61 74 69 76  lt, 8);..  nativ
19751 65 43 6b 73 75 6d 20 3d 20 28 70 57 61 6c 2d 3e  eCksum = (pWal->
19752 68 64 72 2e 62 69 67 45 6e 64 43 6b 73 75 6d 3d  hdr.bigEndCksum=
19753 3d 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41  =SQLITE_BIGENDIA
19754 4e 29 3b 0a 20 20 77 61 6c 43 68 65 63 6b 73 75  N);.  walChecksu
19755 6d 42 79 74 65 73 28 6e 61 74 69 76 65 43 6b 73  mBytes(nativeCks
19756 75 6d 2c 20 61 46 72 61 6d 65 2c 20 38 2c 20 61  um, aFrame, 8, a
19757 43 6b 73 75 6d 2c 20 61 43 6b 73 75 6d 29 3b 0a  Cksum, aCksum);.
19758 20 20 77 61 6c 43 68 65 63 6b 73 75 6d 42 79 74    walChecksumByt
19759 65 73 28 6e 61 74 69 76 65 43 6b 73 75 6d 2c 20  es(nativeCksum, 
1975a 61 44 61 74 61 2c 20 70 57 61 6c 2d 3e 73 7a 50  aData, pWal->szP
1975b 61 67 65 2c 20 61 43 6b 73 75 6d 2c 20 61 43 6b  age, aCksum, aCk
1975c 73 75 6d 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  sum);..  sqlite3
1975d 50 75 74 34 62 79 74 65 28 26 61 46 72 61 6d 65  Put4byte(&aFrame
1975e 5b 31 36 5d 2c 20 61 43 6b 73 75 6d 5b 30 5d 29  [16], aCksum[0])
1975f 3b 0a 20 20 73 71 6c 69 74 65 33 50 75 74 34 62  ;.  sqlite3Put4b
19760 79 74 65 28 26 61 46 72 61 6d 65 5b 32 30 5d 2c  yte(&aFrame[20],
19761 20 61 43 6b 73 75 6d 5b 31 5d 29 3b 0a 7d 0a 0a   aCksum[1]);.}..
19762 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73  /*.** Check to s
19763 65 65 20 69 66 20 74 68 65 20 66 72 61 6d 65 20  ee if the frame 
19764 77 69 74 68 20 68 65 61 64 65 72 20 69 6e 20 61  with header in a
19765 46 72 61 6d 65 5b 5d 20 61 6e 64 20 63 6f 6e 74  Frame[] and cont
19766 65 6e 74 0a 2a 2a 20 69 6e 20 61 44 61 74 61 5b  ent.** in aData[
19767 5d 20 69 73 20 76 61 6c 69 64 2e 20 20 49 66 20  ] is valid.  If 
19768 69 74 20 69 73 20 61 20 76 61 6c 69 64 20 66 72  it is a valid fr
19769 61 6d 65 2c 20 66 69 6c 6c 20 2a 70 69 50 61 67  ame, fill *piPag
1976a 65 20 61 6e 64 0a 2a 2a 20 2a 70 6e 54 72 75 6e  e and.** *pnTrun
1976b 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20  cate and return 
1976c 74 72 75 65 2e 20 20 52 65 74 75 72 6e 20 69 66  true.  Return if
1976d 20 74 68 65 20 66 72 61 6d 65 20 69 73 20 6e 6f   the frame is no
1976e 74 20 76 61 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74  t valid..*/.stat
1976f 69 63 20 69 6e 74 20 77 61 6c 44 65 63 6f 64 65  ic int walDecode
19770 46 72 61 6d 65 28 0a 20 20 57 61 6c 20 2a 70 57  Frame(.  Wal *pW
19771 61 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  al,             
19772 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
19773 77 72 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 20  write-ahead log 
19774 2a 2f 0a 20 20 75 33 32 20 2a 70 69 50 61 67 65  */.  u32 *piPage
19775 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
19776 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 44 61 74       /* OUT: Dat
19777 61 62 61 73 65 20 70 61 67 65 20 6e 75 6d 62 65  abase page numbe
19778 72 20 66 6f 72 20 66 72 61 6d 65 20 2a 2f 0a 20  r for frame */. 
19779 20 75 33 32 20 2a 70 6e 54 72 75 6e 63 61 74 65   u32 *pnTruncate
1977a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1977b 20 2f 2a 20 4f 55 54 3a 20 4e 65 77 20 64 62 20   /* OUT: New db 
1977c 73 69 7a 65 20 28 6f 72 20 30 20 69 66 20 6e 6f  size (or 0 if no
1977d 74 20 63 6f 6d 6d 69 74 29 20 2a 2f 0a 20 20 75  t commit) */.  u
1977e 38 20 2a 61 44 61 74 61 2c 20 20 20 20 20 20 20  8 *aData,       
1977f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19780 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67  * Pointer to pag
19781 65 20 64 61 74 61 20 28 66 6f 72 20 63 68 65 63  e data (for chec
19782 6b 73 75 6d 29 20 2a 2f 0a 20 20 75 38 20 2a 61  ksum) */.  u8 *a
19783 46 72 61 6d 65 20 20 20 20 20 20 20 20 20 20 20  Frame           
19784 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72             /* Fr
19785 61 6d 65 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20  ame data */.){. 
19786 20 69 6e 74 20 6e 61 74 69 76 65 43 6b 73 75 6d   int nativeCksum
19787 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
19788 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6e 61 74   /* True for nat
19789 69 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20 63  ive byte-order c
1978a 68 65 63 6b 73 75 6d 73 20 2a 2f 0a 20 20 75 33  hecksums */.  u3
1978b 32 20 2a 61 43 6b 73 75 6d 20 3d 20 70 57 61 6c  2 *aCksum = pWal
1978c 2d 3e 68 64 72 2e 61 46 72 61 6d 65 43 6b 73 75  ->hdr.aFrameCksu
1978d 6d 3b 0a 20 20 75 33 32 20 70 67 6e 6f 3b 20 20  m;.  u32 pgno;  
1978e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1978f 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d       /* Page num
19790 62 65 72 20 6f 66 20 74 68 65 20 66 72 61 6d 65  ber of the frame
19791 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 57 41   */.  assert( WA
19792 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49 5a 45 3d  L_FRAME_HDRSIZE=
19793 3d 32 34 20 29 3b 0a 0a 20 20 2f 2a 20 41 20 66  =24 );..  /* A f
19794 72 61 6d 65 20 69 73 20 6f 6e 6c 79 20 76 61 6c  rame is only val
19795 69 64 20 69 66 20 74 68 65 20 73 61 6c 74 20 76  id if the salt v
19796 61 6c 75 65 73 20 69 6e 20 74 68 65 20 66 72 61  alues in the fra
19797 6d 65 2d 68 65 61 64 65 72 0a 20 20 2a 2a 20 6d  me-header.  ** m
19798 61 74 63 68 20 74 68 65 20 73 61 6c 74 20 76 61  atch the salt va
19799 6c 75 65 73 20 69 6e 20 74 68 65 20 77 61 6c 2d  lues in the wal-
1979a 68 65 61 64 65 72 2e 20 0a 20 20 2a 2f 0a 20 20  header. .  */.  
1979b 69 66 28 20 6d 65 6d 63 6d 70 28 26 70 57 61 6c  if( memcmp(&pWal
1979c 2d 3e 68 64 72 2e 61 53 61 6c 74 2c 20 26 61 46  ->hdr.aSalt, &aF
1979d 72 61 6d 65 5b 38 5d 2c 20 38 29 21 3d 30 20 29  rame[8], 8)!=0 )
1979e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
1979f 20 20 7d 0a 0a 20 20 2f 2a 20 41 20 66 72 61 6d    }..  /* A fram
197a0 65 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20  e is only valid 
197a1 69 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  if the page numb
197a2 65 72 20 69 73 20 63 72 65 61 74 65 72 20 74 68  er is creater th
197a3 61 6e 20 7a 65 72 6f 2e 0a 20 20 2a 2f 0a 20 20  an zero..  */.  
197a4 70 67 6e 6f 20 3d 20 73 71 6c 69 74 65 33 47 65  pgno = sqlite3Ge
197a5 74 34 62 79 74 65 28 26 61 46 72 61 6d 65 5b 30  t4byte(&aFrame[0
197a6 5d 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d  ]);.  if( pgno==
197a7 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
197a8 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 20 66  0;.  }..  /* A f
197a9 72 61 6d 65 20 69 73 20 6f 6e 6c 79 20 76 61 6c  rame is only val
197aa 69 64 20 69 66 20 61 20 63 68 65 63 6b 73 75 6d  id if a checksum
197ab 20 6f 66 20 74 68 65 20 57 41 4c 20 68 65 61 64   of the WAL head
197ac 65 72 2c 0a 20 20 2a 2a 20 61 6c 6c 20 70 72 69  er,.  ** all pri
197ad 6f 72 20 66 72 61 6d 73 2c 20 74 68 65 20 66 69  or frams, the fi
197ae 72 73 74 20 31 36 20 62 79 74 65 73 20 6f 66 20  rst 16 bytes of 
197af 74 68 69 73 20 66 72 61 6d 65 2d 68 65 61 64 65  this frame-heade
197b0 72 2c 20 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65  r, .  ** and the
197b1 20 66 72 61 6d 65 2d 64 61 74 61 20 6d 61 74 63   frame-data matc
197b2 68 65 73 20 74 68 65 20 63 68 65 63 6b 73 75 6d  hes the checksum
197b3 20 69 6e 20 74 68 65 20 6c 61 73 74 20 38 20 0a   in the last 8 .
197b4 20 20 2a 2a 20 62 79 74 65 73 20 6f 66 20 74 68    ** bytes of th
197b5 69 73 20 66 72 61 6d 65 2d 68 65 61 64 65 72 2e  is frame-header.
197b6 0a 20 20 2a 2f 0a 20 20 6e 61 74 69 76 65 43 6b  .  */.  nativeCk
197b7 73 75 6d 20 3d 20 28 70 57 61 6c 2d 3e 68 64 72  sum = (pWal->hdr
197b8 2e 62 69 67 45 6e 64 43 6b 73 75 6d 3d 3d 53 51  .bigEndCksum==SQ
197b9 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 29 3b  LITE_BIGENDIAN);
197ba 0a 20 20 77 61 6c 43 68 65 63 6b 73 75 6d 42 79  .  walChecksumBy
197bb 74 65 73 28 6e 61 74 69 76 65 43 6b 73 75 6d 2c  tes(nativeCksum,
197bc 20 61 46 72 61 6d 65 2c 20 38 2c 20 61 43 6b 73   aFrame, 8, aCks
197bd 75 6d 2c 20 61 43 6b 73 75 6d 29 3b 0a 20 20 77  um, aCksum);.  w
197be 61 6c 43 68 65 63 6b 73 75 6d 42 79 74 65 73 28  alChecksumBytes(
197bf 6e 61 74 69 76 65 43 6b 73 75 6d 2c 20 61 44 61  nativeCksum, aDa
197c0 74 61 2c 20 70 57 61 6c 2d 3e 73 7a 50 61 67 65  ta, pWal->szPage
197c1 2c 20 61 43 6b 73 75 6d 2c 20 61 43 6b 73 75 6d  , aCksum, aCksum
197c2 29 3b 0a 20 20 69 66 28 20 61 43 6b 73 75 6d 5b  );.  if( aCksum[
197c3 30 5d 21 3d 73 71 6c 69 74 65 33 47 65 74 34 62  0]!=sqlite3Get4b
197c4 79 74 65 28 26 61 46 72 61 6d 65 5b 31 36 5d 29  yte(&aFrame[16])
197c5 20 0a 20 20 20 7c 7c 20 61 43 6b 73 75 6d 5b 31   .   || aCksum[1
197c6 5d 21 3d 73 71 6c 69 74 65 33 47 65 74 34 62 79  ]!=sqlite3Get4by
197c7 74 65 28 26 61 46 72 61 6d 65 5b 32 30 5d 29 20  te(&aFrame[20]) 
197c8 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 43 68 65  .  ){.    /* Che
197c9 63 6b 73 75 6d 20 66 61 69 6c 65 64 2e 20 2a 2f  cksum failed. */
197ca 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
197cb 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 72   }..  /* If we r
197cc 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c  each this point,
197cd 20 74 68 65 20 66 72 61 6d 65 20 69 73 20 76 61   the frame is va
197ce 6c 69 64 2e 20 20 52 65 74 75 72 6e 20 74 68 65  lid.  Return the
197cf 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a   page number.  *
197d0 2a 20 61 6e 64 20 74 68 65 20 6e 65 77 20 64 61  * and the new da
197d1 74 61 62 61 73 65 20 73 69 7a 65 2e 0a 20 20 2a  tabase size..  *
197d2 2f 0a 20 20 2a 70 69 50 61 67 65 20 3d 20 70 67  /.  *piPage = pg
197d3 6e 6f 3b 0a 20 20 2a 70 6e 54 72 75 6e 63 61 74  no;.  *pnTruncat
197d4 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62  e = sqlite3Get4b
197d5 79 74 65 28 26 61 46 72 61 6d 65 5b 34 5d 29 3b  yte(&aFrame[4]);
197d6 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
197d7 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
197d8 49 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 66  ITE_TEST) && def
197d9 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
197da 47 29 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 73 20 6f  G)./*.** Names o
197db 66 20 6c 6f 63 6b 73 2e 20 20 54 68 69 73 20 72  f locks.  This r
197dc 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74  outine is used t
197dd 6f 20 70 72 6f 76 69 64 65 20 64 65 62 75 67 67  o provide debugg
197de 69 6e 67 20 6f 75 74 70 75 74 20 61 6e 64 20 69  ing output and i
197df 73 20 6e 6f 74 0a 2a 2a 20 61 20 70 61 72 74 20  s not.** a part 
197e0 6f 66 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 62  of an ordinary b
197e1 75 69 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  uild..*/.static 
197e2 63 6f 6e 73 74 20 63 68 61 72 20 2a 77 61 6c 4c  const char *walL
197e3 6f 63 6b 4e 61 6d 65 28 69 6e 74 20 6c 6f 63 6b  ockName(int lock
197e4 49 64 78 29 7b 0a 20 20 69 66 28 20 6c 6f 63 6b  Idx){.  if( lock
197e5 49 64 78 3d 3d 57 41 4c 5f 57 52 49 54 45 5f 4c  Idx==WAL_WRITE_L
197e6 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  OCK ){.    retur
197e7 6e 20 22 57 52 49 54 45 2d 4c 4f 43 4b 22 3b 0a  n "WRITE-LOCK";.
197e8 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b    }else if( lock
197e9 49 64 78 3d 3d 57 41 4c 5f 43 4b 50 54 5f 4c 4f  Idx==WAL_CKPT_LO
197ea 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  CK ){.    return
197eb 20 22 43 4b 50 54 2d 4c 4f 43 4b 22 3b 0a 20 20   "CKPT-LOCK";.  
197ec 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 49 64  }else if( lockId
197ed 78 3d 3d 57 41 4c 5f 52 45 43 4f 56 45 52 5f 4c  x==WAL_RECOVER_L
197ee 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  OCK ){.    retur
197ef 6e 20 22 52 45 43 4f 56 45 52 2d 4c 4f 43 4b 22  n "RECOVER-LOCK"
197f0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
197f1 74 61 74 69 63 20 63 68 61 72 20 7a 4e 61 6d 65  tatic char zName
197f2 5b 31 35 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65  [15];.    sqlite
197f3 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
197f4 66 28 7a 4e 61 6d 65 29 2c 20 7a 4e 61 6d 65 2c  f(zName), zName,
197f5 20 22 52 45 41 44 2d 4c 4f 43 4b 5b 25 64 5d 22   "READ-LOCK[%d]"
197f6 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
197f7 20 20 20 20 20 20 20 6c 6f 63 6b 49 64 78 2d 57         lockIdx-W
197f8 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 30 29 29  AL_READ_LOCK(0))
197f9 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 4e 61  ;.    return zNa
197fa 6d 65 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  me;.  }.}.#endif
197fb 20 2f 2a 64 65 66 69 6e 65 64 28 53 51 4c 49 54   /*defined(SQLIT
197fc 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e  E_TEST) || defin
197fd 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
197fe 20 2a 2f 0a 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20   */.    ../*.** 
197ff 53 65 74 20 6f 72 20 72 65 6c 65 61 73 65 20 6c  Set or release l
19800 6f 63 6b 73 20 6f 6e 20 74 68 65 20 57 41 4c 2e  ocks on the WAL.
19801 20 20 4c 6f 63 6b 73 20 61 72 65 20 65 69 74 68    Locks are eith
19802 65 72 20 73 68 61 72 65 64 20 6f 72 20 65 78 63  er shared or exc
19803 6c 75 73 69 76 65 2e 0a 2a 2a 20 41 20 6c 6f 63  lusive..** A loc
19804 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6d 6f 76 65  k cannot be move
19805 64 20 64 69 72 65 63 74 6c 79 20 62 65 74 77 65  d directly betwe
19806 65 6e 20 73 68 61 72 65 64 20 61 6e 64 20 65 78  en shared and ex
19807 63 6c 75 73 69 76 65 20 2d 20 69 74 20 6d 75 73  clusive - it mus
19808 74 20 67 6f 0a 2a 2a 20 74 68 72 6f 75 67 68 20  t go.** through 
19809 74 68 65 20 75 6e 6c 6f 63 6b 65 64 20 73 74 61  the unlocked sta
1980a 74 65 20 66 69 72 73 74 2e 0a 2a 2a 0a 2a 2a 20  te first..**.** 
1980b 49 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d  In locking_mode=
1980c 45 58 43 4c 55 53 49 56 45 2c 20 61 6c 6c 20 6f  EXCLUSIVE, all o
1980d 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  f these routines
1980e 20 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70 73 2e 0a   become no-ops..
1980f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61  */.static int wa
19810 6c 4c 6f 63 6b 53 68 61 72 65 64 28 57 61 6c 20  lLockShared(Wal 
19811 2a 70 57 61 6c 2c 20 69 6e 74 20 6c 6f 63 6b 49  *pWal, int lockI
19812 64 78 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  dx){.  int rc;. 
19813 20 69 66 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75   if( pWal->exclu
19814 73 69 76 65 4d 6f 64 65 20 29 20 72 65 74 75 72  siveMode ) retur
19815 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72  n SQLITE_OK;.  r
19816 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 68 6d  c = sqlite3OsShm
19817 4c 6f 63 6b 28 70 57 61 6c 2d 3e 70 44 62 46 64  Lock(pWal->pDbFd
19818 2c 20 6c 6f 63 6b 49 64 78 2c 20 31 2c 0a 20 20  , lockIdx, 1,.  
19819 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1981a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 48 4d        SQLITE_SHM
1981b 5f 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53  _LOCK | SQLITE_S
1981c 48 4d 5f 53 48 41 52 45 44 29 3b 0a 20 20 57 41  HM_SHARED);.  WA
1981d 4c 54 52 41 43 45 28 28 22 57 41 4c 25 70 3a 20  LTRACE(("WAL%p: 
1981e 61 63 71 75 69 72 65 20 53 48 41 52 45 44 2d 25  acquire SHARED-%
1981f 73 20 25 73 5c 6e 22 2c 20 70 57 61 6c 2c 0a 20  s %s\n", pWal,. 
19820 20 20 20 20 20 20 20 20 20 20 20 77 61 6c 4c 6f             walLo
19821 63 6b 4e 61 6d 65 28 6c 6f 63 6b 49 64 78 29 2c  ckName(lockIdx),
19822 20 72 63 20 3f 20 22 66 61 69 6c 65 64 22 20 3a   rc ? "failed" :
19823 20 22 6f 6b 22 29 29 3b 0a 20 20 56 56 41 5f 4f   "ok"));.  VVA_O
19824 4e 4c 59 28 20 70 57 61 6c 2d 3e 6c 6f 63 6b 45  NLY( pWal->lockE
19825 72 72 6f 72 20 3d 20 28 75 38 29 28 72 63 21 3d  rror = (u8)(rc!=
19826 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21  SQLITE_OK && rc!
19827 3d 53 51 4c 49 54 45 5f 42 55 53 59 29 3b 20 29  =SQLITE_BUSY); )
19828 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
19829 73 74 61 74 69 63 20 76 6f 69 64 20 77 61 6c 55  static void walU
1982a 6e 6c 6f 63 6b 53 68 61 72 65 64 28 57 61 6c 20  nlockShared(Wal 
1982b 2a 70 57 61 6c 2c 20 69 6e 74 20 6c 6f 63 6b 49  *pWal, int lockI
1982c 64 78 29 7b 0a 20 20 69 66 28 20 70 57 61 6c 2d  dx){.  if( pWal-
1982d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29  >exclusiveMode )
1982e 20 72 65 74 75 72 6e 3b 0a 20 20 28 76 6f 69 64   return;.  (void
1982f 29 73 71 6c 69 74 65 33 4f 73 53 68 6d 4c 6f 63  )sqlite3OsShmLoc
19830 6b 28 70 57 61 6c 2d 3e 70 44 62 46 64 2c 20 6c  k(pWal->pDbFd, l
19831 6f 63 6b 49 64 78 2c 20 31 2c 0a 20 20 20 20 20  ockIdx, 1,.     
19832 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19833 20 20 20 20 53 51 4c 49 54 45 5f 53 48 4d 5f 55      SQLITE_SHM_U
19834 4e 4c 4f 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53  NLOCK | SQLITE_S
19835 48 4d 5f 53 48 41 52 45 44 29 3b 0a 20 20 57 41  HM_SHARED);.  WA
19836 4c 54 52 41 43 45 28 28 22 57 41 4c 25 70 3a 20  LTRACE(("WAL%p: 
19837 72 65 6c 65 61 73 65 20 53 48 41 52 45 44 2d 25  release SHARED-%
19838 73 5c 6e 22 2c 20 70 57 61 6c 2c 20 77 61 6c 4c  s\n", pWal, walL
19839 6f 63 6b 4e 61 6d 65 28 6c 6f 63 6b 49 64 78 29  ockName(lockIdx)
1983a 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  ));.}.static int
1983b 20 77 61 6c 4c 6f 63 6b 45 78 63 6c 75 73 69 76   walLockExclusiv
1983c 65 28 57 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74  e(Wal *pWal, int
1983d 20 6c 6f 63 6b 49 64 78 2c 20 69 6e 74 20 6e 29   lockIdx, int n)
1983e 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66  {.  int rc;.  if
1983f 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69 76  ( pWal->exclusiv
19840 65 4d 6f 64 65 20 29 20 72 65 74 75 72 6e 20 53  eMode ) return S
19841 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72 63 20 3d  QLITE_OK;.  rc =
19842 20 73 71 6c 69 74 65 33 4f 73 53 68 6d 4c 6f 63   sqlite3OsShmLoc
19843 6b 28 70 57 61 6c 2d 3e 70 44 62 46 64 2c 20 6c  k(pWal->pDbFd, l
19844 6f 63 6b 49 64 78 2c 20 6e 2c 0a 20 20 20 20 20  ockIdx, n,.     
19845 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19846 20 20 20 53 51 4c 49 54 45 5f 53 48 4d 5f 4c 4f     SQLITE_SHM_LO
19847 43 4b 20 7c 20 53 51 4c 49 54 45 5f 53 48 4d 5f  CK | SQLITE_SHM_
19848 45 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 57 41  EXCLUSIVE);.  WA
19849 4c 54 52 41 43 45 28 28 22 57 41 4c 25 70 3a 20  LTRACE(("WAL%p: 
1984a 61 63 71 75 69 72 65 20 45 58 43 4c 55 53 49 56  acquire EXCLUSIV
1984b 45 2d 25 73 20 63 6e 74 3d 25 64 20 25 73 5c 6e  E-%s cnt=%d %s\n
1984c 22 2c 20 70 57 61 6c 2c 0a 20 20 20 20 20 20 20  ", pWal,.       
1984d 20 20 20 20 20 77 61 6c 4c 6f 63 6b 4e 61 6d 65       walLockName
1984e 28 6c 6f 63 6b 49 64 78 29 2c 20 6e 2c 20 72 63  (lockIdx), n, rc
1984f 20 3f 20 22 66 61 69 6c 65 64 22 20 3a 20 22 6f   ? "failed" : "o
19850 6b 22 29 29 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59  k"));.  VVA_ONLY
19851 28 20 70 57 61 6c 2d 3e 6c 6f 63 6b 45 72 72 6f  ( pWal->lockErro
19852 72 20 3d 20 28 75 38 29 28 72 63 21 3d 53 51 4c  r = (u8)(rc!=SQL
19853 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51  ITE_OK && rc!=SQ
19854 4c 49 54 45 5f 42 55 53 59 29 3b 20 29 0a 20 20  LITE_BUSY); ).  
19855 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 73 74 61  return rc;.}.sta
19856 74 69 63 20 76 6f 69 64 20 77 61 6c 55 6e 6c 6f  tic void walUnlo
19857 63 6b 45 78 63 6c 75 73 69 76 65 28 57 61 6c 20  ckExclusive(Wal 
19858 2a 70 57 61 6c 2c 20 69 6e 74 20 6c 6f 63 6b 49  *pWal, int lockI
19859 64 78 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 66  dx, int n){.  if
1985a 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69 76  ( pWal->exclusiv
1985b 65 4d 6f 64 65 20 29 20 72 65 74 75 72 6e 3b 0a  eMode ) return;.
1985c 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 4f    (void)sqlite3O
1985d 73 53 68 6d 4c 6f 63 6b 28 70 57 61 6c 2d 3e 70  sShmLock(pWal->p
1985e 44 62 46 64 2c 20 6c 6f 63 6b 49 64 78 2c 20 6e  DbFd, lockIdx, n
1985f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
19860 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
19861 45 5f 53 48 4d 5f 55 4e 4c 4f 43 4b 20 7c 20 53  E_SHM_UNLOCK | S
19862 51 4c 49 54 45 5f 53 48 4d 5f 45 58 43 4c 55 53  QLITE_SHM_EXCLUS
19863 49 56 45 29 3b 0a 20 20 57 41 4c 54 52 41 43 45  IVE);.  WALTRACE
19864 28 28 22 57 41 4c 25 70 3a 20 72 65 6c 65 61 73  (("WAL%p: releas
19865 65 20 45 58 43 4c 55 53 49 56 45 2d 25 73 20 63  e EXCLUSIVE-%s c
19866 6e 74 3d 25 64 5c 6e 22 2c 20 70 57 61 6c 2c 0a  nt=%d\n", pWal,.
19867 20 20 20 20 20 20 20 20 20 20 20 20 20 77 61 6c               wal
19868 4c 6f 63 6b 4e 61 6d 65 28 6c 6f 63 6b 49 64 78  LockName(lockIdx
19869 29 2c 20 6e 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ), n));.}../*.**
1986a 20 43 6f 6d 70 75 74 65 20 61 20 68 61 73 68 20   Compute a hash 
1986b 6f 6e 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72  on a page number
1986c 2e 20 20 54 68 65 20 72 65 73 75 6c 74 69 6e 67  .  The resulting
1986d 20 68 61 73 68 20 76 61 6c 75 65 20 6d 75 73 74   hash value must
1986e 20 6c 61 6e 64 0a 2a 2a 20 62 65 74 77 65 65 6e   land.** between
1986f 20 30 20 61 6e 64 20 28 48 41 53 48 54 41 42 4c   0 and (HASHTABL
19870 45 5f 4e 53 4c 4f 54 2d 31 29 2e 20 20 54 68 65  E_NSLOT-1).  The
19871 20 77 61 6c 48 61 73 68 4e 65 78 74 28 29 20 66   walHashNext() f
19872 75 6e 63 74 69 6f 6e 20 61 64 76 61 6e 63 65 73  unction advances
19873 0a 2a 2a 20 74 68 65 20 68 61 73 68 20 74 6f 20  .** the hash to 
19874 74 68 65 20 6e 65 78 74 20 76 61 6c 75 65 20 69  the next value i
19875 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61  n the event of a
19876 20 63 6f 6c 6c 69 73 69 6f 6e 2e 0a 2a 2f 0a 73   collision..*/.s
19877 74 61 74 69 63 20 69 6e 74 20 77 61 6c 48 61 73  tatic int walHas
19878 68 28 75 33 32 20 69 50 61 67 65 29 7b 0a 20 20  h(u32 iPage){.  
19879 61 73 73 65 72 74 28 20 69 50 61 67 65 3e 30 20  assert( iPage>0 
1987a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 48 41  );.  assert( (HA
1987b 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54 20 26 20  SHTABLE_NSLOT & 
1987c 28 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54  (HASHTABLE_NSLOT
1987d 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 72 65 74  -1))==0 );.  ret
1987e 75 72 6e 20 28 69 50 61 67 65 2a 48 41 53 48 54  urn (iPage*HASHT
1987f 41 42 4c 45 5f 48 41 53 48 5f 31 29 20 26 20 28  ABLE_HASH_1) & (
19880 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54 2d  HASHTABLE_NSLOT-
19881 31 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  1);.}.static int
19882 20 77 61 6c 4e 65 78 74 48 61 73 68 28 69 6e 74   walNextHash(int
19883 20 69 50 72 69 6f 72 48 61 73 68 29 7b 0a 20 20   iPriorHash){.  
19884 72 65 74 75 72 6e 20 28 69 50 72 69 6f 72 48 61  return (iPriorHa
19885 73 68 2b 31 29 26 28 48 41 53 48 54 41 42 4c 45  sh+1)&(HASHTABLE
19886 5f 4e 53 4c 4f 54 2d 31 29 3b 0a 7d 0a 0a 2f 2a  _NSLOT-1);.}../*
19887 20 0a 2a 2a 20 52 65 74 75 72 6e 20 70 6f 69 6e   .** Return poin
19888 74 65 72 73 20 74 6f 20 74 68 65 20 68 61 73 68  ters to the hash
19889 20 74 61 62 6c 65 20 61 6e 64 20 70 61 67 65 20   table and page 
1988a 6e 75 6d 62 65 72 20 61 72 72 61 79 20 73 74 6f  number array sto
1988b 72 65 64 20 6f 6e 0a 2a 2a 20 70 61 67 65 20 69  red on.** page i
1988c 48 61 73 68 20 6f 66 20 74 68 65 20 77 61 6c 2d  Hash of the wal-
1988d 69 6e 64 65 78 2e 20 54 68 65 20 77 61 6c 2d 69  index. The wal-i
1988e 6e 64 65 78 20 69 73 20 62 72 6f 6b 65 6e 20 69  ndex is broken i
1988f 6e 74 6f 20 33 32 4b 42 20 70 61 67 65 73 0a 2a  nto 32KB pages.*
19890 2a 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74  * numbered start
19891 69 6e 67 20 66 72 6f 6d 20 30 2e 0a 2a 2a 0a 2a  ing from 0..**.*
19892 2a 20 53 65 74 20 6f 75 74 70 75 74 20 76 61 72  * Set output var
19893 69 61 62 6c 65 20 2a 70 61 48 61 73 68 20 74 6f  iable *paHash to
19894 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 74   point to the st
19895 61 72 74 20 6f 66 20 74 68 65 20 68 61 73 68 20  art of the hash 
19896 74 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20  table.** in the 
19897 77 61 6c 2d 69 6e 64 65 78 20 66 69 6c 65 2e 20  wal-index file. 
19898 53 65 74 20 2a 70 69 5a 65 72 6f 20 74 6f 20 6f  Set *piZero to o
19899 6e 65 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65  ne less than the
1989a 20 66 72 61 6d 65 20 0a 2a 2a 20 6e 75 6d 62 65   frame .** numbe
1989b 72 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 66  r of the first f
1989c 72 61 6d 65 20 69 6e 64 65 78 65 64 20 62 79 20  rame indexed by 
1989d 74 68 69 73 20 68 61 73 68 20 74 61 62 6c 65 2e  this hash table.
1989e 20 49 66 20 61 0a 2a 2a 20 73 6c 6f 74 20 69 6e   If a.** slot in
1989f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
198a0 69 73 20 73 65 74 20 74 6f 20 4e 2c 20 69 74 20  is set to N, it 
198a1 72 65 66 65 72 73 20 74 6f 20 66 72 61 6d 65 20  refers to frame 
198a2 6e 75 6d 62 65 72 20 0a 2a 2a 20 28 2a 70 69 5a  number .** (*piZ
198a3 65 72 6f 2b 4e 29 20 69 6e 20 74 68 65 20 6c 6f  ero+N) in the lo
198a4 67 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 61 6c 6c 79  g..**.** Finally
198a5 2c 20 73 65 74 20 2a 70 61 50 67 6e 6f 20 73 6f  , set *paPgno so
198a6 20 74 68 61 74 20 2a 70 61 50 67 6e 6f 5b 31 5d   that *paPgno[1]
198a7 20 69 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d   is the page num
198a8 62 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69  ber of the.** fi
198a9 72 73 74 20 66 72 61 6d 65 20 69 6e 64 65 78 65  rst frame indexe
198aa 64 20 62 79 20 74 68 65 20 68 61 73 68 20 74 61  d by the hash ta
198ab 62 6c 65 2c 20 66 72 61 6d 65 20 28 2a 70 69 5a  ble, frame (*piZ
198ac 65 72 6f 2b 31 29 2e 0a 2a 2f 0a 73 74 61 74 69  ero+1)..*/.stati
198ad 63 20 69 6e 74 20 77 61 6c 48 61 73 68 47 65 74  c int walHashGet
198ae 28 0a 20 20 57 61 6c 20 2a 70 57 61 6c 2c 20 20  (.  Wal *pWal,  
198af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
198b0 20 20 20 20 2f 2a 20 57 41 4c 20 68 61 6e 64 6c      /* WAL handl
198b1 65 20 2a 2f 0a 20 20 69 6e 74 20 69 48 61 73 68  e */.  int iHash
198b2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
198b3 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 20 74         /* Find t
198b4 68 65 20 69 48 61 73 68 27 74 68 20 74 61 62 6c  he iHash'th tabl
198b5 65 20 2a 2f 0a 20 20 76 6f 6c 61 74 69 6c 65 20  e */.  volatile 
198b6 68 74 5f 73 6c 6f 74 20 2a 2a 70 61 48 61 73 68  ht_slot **paHash
198b7 2c 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50  ,      /* OUT: P
198b8 6f 69 6e 74 65 72 20 74 6f 20 68 61 73 68 20 69  ointer to hash i
198b9 6e 64 65 78 20 2a 2f 0a 20 20 76 6f 6c 61 74 69  ndex */.  volati
198ba 6c 65 20 75 33 32 20 2a 2a 70 61 50 67 6e 6f 2c  le u32 **paPgno,
198bb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
198bc 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67  : Pointer to pag
198bd 65 20 6e 75 6d 62 65 72 20 61 72 72 61 79 20 2a  e number array *
198be 2f 0a 20 20 75 33 32 20 2a 70 69 5a 65 72 6f 20  /.  u32 *piZero 
198bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
198c0 20 20 20 20 2f 2a 20 4f 55 54 3a 20 46 72 61 6d      /* OUT: Fram
198c1 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
198c2 68 20 2a 70 61 50 67 6e 6f 5b 30 5d 20 2a 2f 0a  h *paPgno[0] */.
198c3 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
198c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
198c5 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
198c6 6f 64 65 20 2a 2f 0a 20 20 76 6f 6c 61 74 69 6c  ode */.  volatil
198c7 65 20 75 33 32 20 2a 61 50 67 6e 6f 3b 0a 0a 20  e u32 *aPgno;.. 
198c8 20 72 63 20 3d 20 77 61 6c 49 6e 64 65 78 50 61   rc = walIndexPa
198c9 67 65 28 70 57 61 6c 2c 20 69 48 61 73 68 2c 20  ge(pWal, iHash, 
198ca 26 61 50 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72  &aPgno);.  asser
198cb 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
198cc 20 7c 7c 20 69 48 61 73 68 3e 30 20 29 3b 0a 0a   || iHash>0 );..
198cd 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
198ce 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 33 32 20 69  _OK ){.    u32 i
198cf 5a 65 72 6f 3b 0a 20 20 20 20 76 6f 6c 61 74 69  Zero;.    volati
198d0 6c 65 20 68 74 5f 73 6c 6f 74 20 2a 61 48 61 73  le ht_slot *aHas
198d1 68 3b 0a 0a 20 20 20 20 61 48 61 73 68 20 3d 20  h;..    aHash = 
198d2 28 76 6f 6c 61 74 69 6c 65 20 68 74 5f 73 6c 6f  (volatile ht_slo
198d3 74 20 2a 29 26 61 50 67 6e 6f 5b 48 41 53 48 54  t *)&aPgno[HASHT
198d4 41 42 4c 45 5f 4e 50 41 47 45 5d 3b 0a 20 20 20  ABLE_NPAGE];.   
198d5 20 69 66 28 20 69 48 61 73 68 3d 3d 30 20 29 7b   if( iHash==0 ){
198d6 0a 20 20 20 20 20 20 61 50 67 6e 6f 20 3d 20 26  .      aPgno = &
198d7 61 50 67 6e 6f 5b 57 41 4c 49 4e 44 45 58 5f 48  aPgno[WALINDEX_H
198d8 44 52 5f 53 49 5a 45 2f 73 69 7a 65 6f 66 28 75  DR_SIZE/sizeof(u
198d9 33 32 29 5d 3b 0a 20 20 20 20 20 20 69 5a 65 72  32)];.      iZer
198da 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  o = 0;.    }else
198db 7b 0a 20 20 20 20 20 20 69 5a 65 72 6f 20 3d 20  {.      iZero = 
198dc 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f  HASHTABLE_NPAGE_
198dd 4f 4e 45 20 2b 20 28 69 48 61 73 68 2d 31 29 2a  ONE + (iHash-1)*
198de 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 3b  HASHTABLE_NPAGE;
198df 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2a 70  .    }.  .    *p
198e0 61 50 67 6e 6f 20 3d 20 26 61 50 67 6e 6f 5b 2d  aPgno = &aPgno[-
198e1 31 5d 3b 0a 20 20 20 20 2a 70 61 48 61 73 68 20  1];.    *paHash 
198e2 3d 20 61 48 61 73 68 3b 0a 20 20 20 20 2a 70 69  = aHash;.    *pi
198e3 5a 65 72 6f 20 3d 20 69 5a 65 72 6f 3b 0a 20 20  Zero = iZero;.  
198e4 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
198e5 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
198e6 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  he number of the
198e7 20 77 61 6c 2d 69 6e 64 65 78 20 70 61 67 65 20   wal-index page 
198e8 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
198e9 65 20 68 61 73 68 2d 74 61 62 6c 65 0a 2a 2a 20  e hash-table.** 
198ea 61 6e 64 20 70 61 67 65 2d 6e 75 6d 62 65 72 20  and page-number 
198eb 61 72 72 61 79 20 74 68 61 74 20 63 6f 6e 74 61  array that conta
198ec 69 6e 20 65 6e 74 72 69 65 73 20 63 6f 72 72 65  in entries corre
198ed 73 70 6f 6e 64 69 6e 67 20 74 6f 20 57 41 4c 20  sponding to WAL 
198ee 66 72 61 6d 65 0a 2a 2a 20 69 46 72 61 6d 65 2e  frame.** iFrame.
198ef 20 54 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 69   The wal-index i
198f0 73 20 62 72 6f 6b 65 6e 20 75 70 20 69 6e 74 6f  s broken up into
198f1 20 33 32 4b 42 20 70 61 67 65 73 2e 20 57 61 6c   32KB pages. Wal
198f2 2d 69 6e 64 65 78 20 70 61 67 65 73 20 0a 2a 2a  -index pages .**
198f3 20 61 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74   are numbered st
198f4 61 72 74 69 6e 67 20 66 72 6f 6d 20 30 2e 0a 2a  arting from 0..*
198f5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61 6c  /.static int wal
198f6 46 72 61 6d 65 50 61 67 65 28 75 33 32 20 69 46  FramePage(u32 iF
198f7 72 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 48 61  rame){.  int iHa
198f8 73 68 20 3d 20 28 69 46 72 61 6d 65 2b 48 41 53  sh = (iFrame+HAS
198f9 48 54 41 42 4c 45 5f 4e 50 41 47 45 2d 48 41 53  HTABLE_NPAGE-HAS
198fa 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45  HTABLE_NPAGE_ONE
198fb 2d 31 29 20 2f 20 48 41 53 48 54 41 42 4c 45 5f  -1) / HASHTABLE_
198fc 4e 50 41 47 45 3b 0a 20 20 61 73 73 65 72 74 28  NPAGE;.  assert(
198fd 20 28 69 48 61 73 68 3d 3d 30 20 7c 7c 20 69 46   (iHash==0 || iF
198fe 72 61 6d 65 3e 48 41 53 48 54 41 42 4c 45 5f 4e  rame>HASHTABLE_N
198ff 50 41 47 45 5f 4f 4e 45 29 0a 20 20 20 20 20 20  PAGE_ONE).      
19900 20 26 26 20 28 69 48 61 73 68 3e 3d 31 20 7c 7c   && (iHash>=1 ||
19901 20 69 46 72 61 6d 65 3c 3d 48 41 53 48 54 41 42   iFrame<=HASHTAB
19902 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45 29 0a 20 20  LE_NPAGE_ONE).  
19903 20 20 20 20 20 26 26 20 28 69 48 61 73 68 3c 3d       && (iHash<=
19904 31 20 7c 7c 20 69 46 72 61 6d 65 3e 28 48 41 53  1 || iFrame>(HAS
19905 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45  HTABLE_NPAGE_ONE
19906 2b 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45  +HASHTABLE_NPAGE
19907 29 29 0a 20 20 20 20 20 20 20 26 26 20 28 69 48  )).       && (iH
19908 61 73 68 3e 3d 32 20 7c 7c 20 69 46 72 61 6d 65  ash>=2 || iFrame
19909 3c 3d 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47  <=HASHTABLE_NPAG
1990a 45 5f 4f 4e 45 2b 48 41 53 48 54 41 42 4c 45 5f  E_ONE+HASHTABLE_
1990b 4e 50 41 47 45 29 0a 20 20 20 20 20 20 20 26 26  NPAGE).       &&
1990c 20 28 69 48 61 73 68 3c 3d 32 20 7c 7c 20 69 46   (iHash<=2 || iF
1990d 72 61 6d 65 3e 28 48 41 53 48 54 41 42 4c 45 5f  rame>(HASHTABLE_
1990e 4e 50 41 47 45 5f 4f 4e 45 2b 32 2a 48 41 53 48  NPAGE_ONE+2*HASH
1990f 54 41 42 4c 45 5f 4e 50 41 47 45 29 29 0a 20 20  TABLE_NPAGE)).  
19910 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 48 61 73  );.  return iHas
19911 68 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  h;.}../*.** Retu
19912 72 6e 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  rn the page numb
19913 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  er associated wi
19914 74 68 20 66 72 61 6d 65 20 69 46 72 61 6d 65 20  th frame iFrame 
19915 69 6e 20 74 68 69 73 20 57 41 4c 2e 0a 2a 2f 0a  in this WAL..*/.
19916 73 74 61 74 69 63 20 75 33 32 20 77 61 6c 46 72  static u32 walFr
19917 61 6d 65 50 67 6e 6f 28 57 61 6c 20 2a 70 57 61  amePgno(Wal *pWa
19918 6c 2c 20 75 33 32 20 69 46 72 61 6d 65 29 7b 0a  l, u32 iFrame){.
19919 20 20 69 6e 74 20 69 48 61 73 68 20 3d 20 77 61    int iHash = wa
1991a 6c 46 72 61 6d 65 50 61 67 65 28 69 46 72 61 6d  lFramePage(iFram
1991b 65 29 3b 0a 20 20 69 66 28 20 69 48 61 73 68 3d  e);.  if( iHash=
1991c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
1991d 20 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 61 5b   pWal->apWiData[
1991e 30 5d 5b 57 41 4c 49 4e 44 45 58 5f 48 44 52 5f  0][WALINDEX_HDR_
1991f 53 49 5a 45 2f 73 69 7a 65 6f 66 28 75 33 32 29  SIZE/sizeof(u32)
19920 20 2b 20 69 46 72 61 6d 65 20 2d 20 31 5d 3b 0a   + iFrame - 1];.
19921 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 57 61    }.  return pWa
19922 6c 2d 3e 61 70 57 69 44 61 74 61 5b 69 48 61 73  l->apWiData[iHas
19923 68 5d 5b 28 69 46 72 61 6d 65 2d 31 2d 48 41 53  h][(iFrame-1-HAS
19924 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45  HTABLE_NPAGE_ONE
19925 29 25 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47  )%HASHTABLE_NPAG
19926 45 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d  E];.}../*.** Rem
19927 6f 76 65 20 65 6e 74 72 69 65 73 20 66 72 6f 6d  ove entries from
19928 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
19929 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 57 41  that point to WA
1992a 4c 20 73 6c 6f 74 73 20 67 72 65 61 74 65 72 0a  L slots greater.
1992b 2a 2a 20 74 68 61 6e 20 70 57 61 6c 2d 3e 68 64  ** than pWal->hd
1992c 72 2e 6d 78 46 72 61 6d 65 2e 0a 2a 2a 0a 2a 2a  r.mxFrame..**.**
1992d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
1992e 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 76 65  s called wheneve
1992f 72 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72  r pWal->hdr.mxFr
19930 61 6d 65 20 69 73 20 64 65 63 72 65 61 73 65 64  ame is decreased
19931 20 64 75 65 0a 2a 2a 20 74 6f 20 61 20 72 6f 6c   due.** to a rol
19932 6c 62 61 63 6b 20 6f 72 20 73 61 76 65 70 6f 69  lback or savepoi
19933 6e 74 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 6d 6f 73  nt..**.** At mos
19934 74 20 6f 6e 6c 79 20 74 68 65 20 68 61 73 68 20  t only the hash 
19935 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  table containing
19936 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61   pWal->hdr.mxFra
19937 6d 65 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 2a  me needs to be.*
19938 2a 20 75 70 64 61 74 65 64 2e 20 20 41 6e 79 20  * updated.  Any 
19939 6c 61 74 65 72 20 68 61 73 68 20 74 61 62 6c 65  later hash table
1993a 73 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61  s will be automa
1993b 74 69 63 61 6c 6c 79 20 63 6c 65 61 72 65 64 20  tically cleared 
1993c 77 68 65 6e 0a 2a 2a 20 70 57 61 6c 2d 3e 68 64  when.** pWal->hd
1993d 72 2e 6d 78 46 72 61 6d 65 20 61 64 76 61 6e 63  r.mxFrame advanc
1993e 65 73 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 20  es to the point 
1993f 77 68 65 72 65 20 74 68 6f 73 65 20 68 61 73 68  where those hash
19940 20 74 61 62 6c 65 73 20 61 72 65 0a 2a 2a 20 61   tables are.** a
19941 63 74 75 61 6c 6c 79 20 6e 65 65 64 65 64 2e 0a  ctually needed..
19942 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
19943 61 6c 43 6c 65 61 6e 75 70 48 61 73 68 28 57 61  alCleanupHash(Wa
19944 6c 20 2a 70 57 61 6c 29 7b 0a 20 20 76 6f 6c 61  l *pWal){.  vola
19945 74 69 6c 65 20 68 74 5f 73 6c 6f 74 20 2a 61 48  tile ht_slot *aH
19946 61 73 68 20 3d 20 30 3b 20 20 20 20 2f 2a 20 50  ash = 0;    /* P
19947 6f 69 6e 74 65 72 20 74 6f 20 68 61 73 68 20 74  ointer to hash t
19948 61 62 6c 65 20 74 6f 20 63 6c 65 61 72 20 2a 2f  able to clear */
19949 0a 20 20 76 6f 6c 61 74 69 6c 65 20 75 33 32 20  .  volatile u32 
1994a 2a 61 50 67 6e 6f 20 3d 20 30 3b 20 20 20 20 20  *aPgno = 0;     
1994b 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
1994c 72 20 61 72 72 61 79 20 66 6f 72 20 68 61 73 68  r array for hash
1994d 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 33 32 20   table */.  u32 
1994e 69 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20 20  iZero = 0;      
1994f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
19950 72 61 6d 65 20 3d 3d 20 28 61 48 61 73 68 5b 78  rame == (aHash[x
19951 5d 2b 69 5a 65 72 6f 29 20 2a 2f 0a 20 20 69 6e  ]+iZero) */.  in
19952 74 20 69 4c 69 6d 69 74 20 3d 20 30 3b 20 20 20  t iLimit = 0;   
19953 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19954 20 5a 65 72 6f 20 76 61 6c 75 65 73 20 67 72 65   Zero values gre
19955 61 74 65 72 20 74 68 61 6e 20 74 68 69 73 20 2a  ater than this *
19956 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20  /.  int nByte;  
19957 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19958 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
19959 20 62 79 74 65 73 20 74 6f 20 7a 65 72 6f 20 69   bytes to zero i
1995a 6e 20 61 50 67 6e 6f 5b 5d 20 2a 2f 0a 20 20 69  n aPgno[] */.  i
1995b 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
1995c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1995d 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72 61 74  * Used to iterat
1995e 65 20 74 68 72 6f 75 67 68 20 61 48 61 73 68 5b  e through aHash[
1995f 5d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  ] */..  assert( 
19960 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 20  pWal->writeLock 
19961 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70  );.  testcase( p
19962 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65  Wal->hdr.mxFrame
19963 3d 3d 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47  ==HASHTABLE_NPAG
19964 45 5f 4f 4e 45 2d 31 20 29 3b 0a 20 20 74 65 73  E_ONE-1 );.  tes
19965 74 63 61 73 65 28 20 70 57 61 6c 2d 3e 68 64 72  tcase( pWal->hdr
19966 2e 6d 78 46 72 61 6d 65 3d 3d 48 41 53 48 54 41  .mxFrame==HASHTA
19967 42 4c 45 5f 4e 50 41 47 45 5f 4f 4e 45 20 29 3b  BLE_NPAGE_ONE );
19968 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 57 61  .  testcase( pWa
19969 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 3d 3d  l->hdr.mxFrame==
1996a 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 5f  HASHTABLE_NPAGE_
1996b 4f 4e 45 2b 31 20 29 3b 0a 0a 20 20 69 66 28 20  ONE+1 );..  if( 
1996c 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d  pWal->hdr.mxFram
1996d 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a  e==0 ) return;..
1996e 20 20 2f 2a 20 4f 62 74 61 69 6e 20 70 6f 69 6e    /* Obtain poin
1996f 74 65 72 73 20 74 6f 20 74 68 65 20 68 61 73 68  ters to the hash
19970 2d 74 61 62 6c 65 20 61 6e 64 20 70 61 67 65 2d  -table and page-
19971 6e 75 6d 62 65 72 20 61 72 72 61 79 20 63 6f 6e  number array con
19972 74 61 69 6e 69 6e 67 20 0a 20 20 2a 2a 20 74 68  taining .  ** th
19973 65 20 65 6e 74 72 79 20 74 68 61 74 20 63 6f 72  e entry that cor
19974 72 65 73 70 6f 6e 64 73 20 74 6f 20 66 72 61 6d  responds to fram
19975 65 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72  e pWal->hdr.mxFr
19976 61 6d 65 2e 20 49 74 20 69 73 20 67 75 61 72 61  ame. It is guara
19977 6e 74 65 65 64 0a 20 20 2a 2a 20 74 68 61 74 20  nteed.  ** that 
19978 74 68 65 20 70 61 67 65 20 73 61 69 64 20 68 61  the page said ha
19979 73 68 2d 74 61 62 6c 65 20 61 6e 64 20 61 72 72  sh-table and arr
1997a 61 79 20 72 65 73 69 64 65 20 6f 6e 20 69 73 20  ay reside on is 
1997b 61 6c 72 65 61 64 79 20 6d 61 70 70 65 64 2e 0a  already mapped..
1997c 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
1997d 57 61 6c 2d 3e 6e 57 69 44 61 74 61 3e 77 61 6c  Wal->nWiData>wal
1997e 46 72 61 6d 65 50 61 67 65 28 70 57 61 6c 2d 3e  FramePage(pWal->
1997f 68 64 72 2e 6d 78 46 72 61 6d 65 29 20 29 3b 0a  hdr.mxFrame) );.
19980 20 20 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e    assert( pWal->
19981 61 70 57 69 44 61 74 61 5b 77 61 6c 46 72 61 6d  apWiData[walFram
19982 65 50 61 67 65 28 70 57 61 6c 2d 3e 68 64 72 2e  ePage(pWal->hdr.
19983 6d 78 46 72 61 6d 65 29 5d 20 29 3b 0a 20 20 77  mxFrame)] );.  w
19984 61 6c 48 61 73 68 47 65 74 28 70 57 61 6c 2c 20  alHashGet(pWal, 
19985 77 61 6c 46 72 61 6d 65 50 61 67 65 28 70 57 61  walFramePage(pWa
19986 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 29 2c  l->hdr.mxFrame),
19987 20 26 61 48 61 73 68 2c 20 26 61 50 67 6e 6f 2c   &aHash, &aPgno,
19988 20 26 69 5a 65 72 6f 29 3b 0a 0a 20 20 2f 2a 20   &iZero);..  /* 
19989 5a 65 72 6f 20 61 6c 6c 20 68 61 73 68 2d 74 61  Zero all hash-ta
1998a 62 6c 65 20 65 6e 74 72 69 65 73 20 74 68 61 74  ble entries that
1998b 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 66   correspond to f
1998c 72 61 6d 65 20 6e 75 6d 62 65 72 73 20 67 72 65  rame numbers gre
1998d 61 74 65 72 0a 20 20 2a 2a 20 74 68 61 6e 20 70  ater.  ** than p
1998e 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65  Wal->hdr.mxFrame
1998f 2e 0a 20 20 2a 2f 0a 20 20 69 4c 69 6d 69 74 20  ..  */.  iLimit 
19990 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72  = pWal->hdr.mxFr
19991 61 6d 65 20 2d 20 69 5a 65 72 6f 3b 0a 20 20 61  ame - iZero;.  a
19992 73 73 65 72 74 28 20 69 4c 69 6d 69 74 3e 30 20  ssert( iLimit>0 
19993 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
19994 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54 3b  HASHTABLE_NSLOT;
19995 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61   i++){.    if( a
19996 48 61 73 68 5b 69 5d 3e 69 4c 69 6d 69 74 20 29  Hash[i]>iLimit )
19997 7b 0a 20 20 20 20 20 20 61 48 61 73 68 5b 69 5d  {.      aHash[i]
19998 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
19999 20 20 0a 20 20 2f 2a 20 5a 65 72 6f 20 74 68 65    .  /* Zero the
1999a 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
1999b 61 50 67 6e 6f 20 61 72 72 61 79 20 74 68 61 74  aPgno array that
1999c 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 66   correspond to f
1999d 72 61 6d 65 73 20 77 69 74 68 0a 20 20 2a 2a 20  rames with.  ** 
1999e 66 72 61 6d 65 20 6e 75 6d 62 65 72 73 20 67 72  frame numbers gr
1999f 65 61 74 65 72 20 74 68 61 6e 20 70 57 61 6c 2d  eater than pWal-
199a0 3e 68 64 72 2e 6d 78 46 72 61 6d 65 2e 20 0a 20  >hdr.mxFrame. . 
199a1 20 2a 2f 0a 20 20 6e 42 79 74 65 20 3d 20 28 69   */.  nByte = (i
199a2 6e 74 29 28 28 63 68 61 72 20 2a 29 61 48 61 73  nt)((char *)aHas
199a3 68 20 2d 20 28 63 68 61 72 20 2a 29 26 61 50 67  h - (char *)&aPg
199a4 6e 6f 5b 69 4c 69 6d 69 74 2b 31 5d 29 3b 0a 20  no[iLimit+1]);. 
199a5 20 6d 65 6d 73 65 74 28 28 76 6f 69 64 20 2a 29   memset((void *)
199a6 26 61 50 67 6e 6f 5b 69 4c 69 6d 69 74 2b 31 5d  &aPgno[iLimit+1]
199a7 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 0a 23 69  , 0, nByte);..#i
199a8 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
199a9 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53  LE_EXPENSIVE_ASS
199aa 45 52 54 0a 20 20 2f 2a 20 56 65 72 69 66 79 20  ERT.  /* Verify 
199ab 74 68 61 74 20 74 68 65 20 65 76 65 72 79 20 65  that the every e
199ac 6e 74 72 79 20 69 6e 20 74 68 65 20 6d 61 70 70  ntry in the mapp
199ad 69 6e 67 20 72 65 67 69 6f 6e 20 69 73 20 73 74  ing region is st
199ae 69 6c 6c 20 72 65 61 63 68 61 62 6c 65 0a 20 20  ill reachable.  
199af 2a 2a 20 76 69 61 20 74 68 65 20 68 61 73 68 20  ** via the hash 
199b0 74 61 62 6c 65 20 65 76 65 6e 20 61 66 74 65 72  table even after
199b1 20 74 68 65 20 63 6c 65 61 6e 75 70 2e 0a 20 20   the cleanup..  
199b2 2a 2f 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 20  */.  if( iLimit 
199b3 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20  ){.    int i;   
199b4 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
199b5 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 69  counter */.    i
199b6 6e 74 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20  nt iKey;        
199b7 2f 2a 20 48 61 73 68 20 6b 65 79 20 2a 2f 0a 20  /* Hash key */. 
199b8 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 69     for(i=1; i<=i
199b9 4c 69 6d 69 74 3b 20 69 2b 2b 29 7b 0a 20 20 20  Limit; i++){.   
199ba 20 20 20 66 6f 72 28 69 4b 65 79 3d 77 61 6c 48     for(iKey=walH
199bb 61 73 68 28 61 50 67 6e 6f 5b 69 5d 29 3b 20 61  ash(aPgno[i]); a
199bc 48 61 73 68 5b 69 4b 65 79 5d 3b 20 69 4b 65 79  Hash[iKey]; iKey
199bd 3d 77 61 6c 4e 65 78 74 48 61 73 68 28 69 4b 65  =walNextHash(iKe
199be 79 29 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  y)){.        if(
199bf 20 61 48 61 73 68 5b 69 4b 65 79 5d 3d 3d 69 20   aHash[iKey]==i 
199c0 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d  ) break;.      }
199c1 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61  .      assert( a
199c2 48 61 73 68 5b 69 4b 65 79 5d 3d 3d 69 20 29 3b  Hash[iKey]==i );
199c3 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
199c4 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42  f /* SQLITE_ENAB
199c5 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53  LE_EXPENSIVE_ASS
199c6 45 52 54 20 2a 2f 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  ERT */.}.../*.**
199c7 20 53 65 74 20 61 6e 20 65 6e 74 72 79 20 69 6e   Set an entry in
199c8 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 74   the wal-index t
199c9 68 61 74 20 77 69 6c 6c 20 6d 61 70 20 64 61 74  hat will map dat
199ca 61 62 61 73 65 20 70 61 67 65 20 6e 75 6d 62 65  abase page numbe
199cb 72 0a 2a 2a 20 70 50 61 67 65 20 69 6e 74 6f 20  r.** pPage into 
199cc 57 41 4c 20 66 72 61 6d 65 20 69 46 72 61 6d 65  WAL frame iFrame
199cd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
199ce 77 61 6c 49 6e 64 65 78 41 70 70 65 6e 64 28 57  walIndexAppend(W
199cf 61 6c 20 2a 70 57 61 6c 2c 20 75 33 32 20 69 46  al *pWal, u32 iF
199d0 72 61 6d 65 2c 20 75 33 32 20 69 50 61 67 65 29  rame, u32 iPage)
199d1 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
199d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
199d3 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
199d4 64 65 20 2a 2f 0a 20 20 75 33 32 20 69 5a 65 72  de */.  u32 iZer
199d5 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  o = 0;          
199d6 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6c          /* One l
199d7 65 73 73 20 74 68 61 6e 20 66 72 61 6d 65 20 6e  ess than frame n
199d8 75 6d 62 65 72 20 6f 66 20 61 50 67 6e 6f 5b 31  umber of aPgno[1
199d9 5d 20 2a 2f 0a 20 20 76 6f 6c 61 74 69 6c 65 20  ] */.  volatile 
199da 75 33 32 20 2a 61 50 67 6e 6f 20 3d 20 30 3b 20  u32 *aPgno = 0; 
199db 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
199dc 75 6d 62 65 72 20 61 72 72 61 79 20 2a 2f 0a 20  umber array */. 
199dd 20 76 6f 6c 61 74 69 6c 65 20 68 74 5f 73 6c 6f   volatile ht_slo
199de 74 20 2a 61 48 61 73 68 20 3d 20 30 3b 20 20 20  t *aHash = 0;   
199df 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 2a   /* Hash table *
199e0 2f 0a 0a 20 20 72 63 20 3d 20 77 61 6c 48 61 73  /..  rc = walHas
199e1 68 47 65 74 28 70 57 61 6c 2c 20 77 61 6c 46 72  hGet(pWal, walFr
199e2 61 6d 65 50 61 67 65 28 69 46 72 61 6d 65 29 2c  amePage(iFrame),
199e3 20 26 61 48 61 73 68 2c 20 26 61 50 67 6e 6f 2c   &aHash, &aPgno,
199e4 20 26 69 5a 65 72 6f 29 3b 0a 0a 20 20 2f 2a 20   &iZero);..  /* 
199e5 41 73 73 75 6d 69 6e 67 20 74 68 65 20 77 61 6c  Assuming the wal
199e6 2d 69 6e 64 65 78 20 66 69 6c 65 20 77 61 73 20  -index file was 
199e7 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6d 61 70  successfully map
199e8 70 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68  ped, populate th
199e9 65 0a 20 20 2a 2a 20 70 61 67 65 20 6e 75 6d 62  e.  ** page numb
199ea 65 72 20 61 72 72 61 79 20 61 6e 64 20 68 61 73  er array and has
199eb 68 20 74 61 62 6c 65 20 65 6e 74 72 79 2e 0a 20  h table entry.. 
199ec 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
199ed 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
199ee 6e 74 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20  nt iKey;        
199ef 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
199f0 48 61 73 68 20 74 61 62 6c 65 20 6b 65 79 20 2a  Hash table key *
199f1 2f 0a 20 20 20 20 69 6e 74 20 69 64 78 3b 20 20  /.    int idx;  
199f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
199f3 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
199f4 77 72 69 74 65 20 74 6f 20 68 61 73 68 2d 74 61  write to hash-ta
199f5 62 6c 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 20 20  ble slot */.    
199f6 69 6e 74 20 6e 43 6f 6c 6c 69 64 65 3b 20 20 20  int nCollide;   
199f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
199f8 20 4e 75 6d 62 65 72 20 6f 66 20 68 61 73 68 20   Number of hash 
199f9 63 6f 6c 6c 69 73 69 6f 6e 73 20 2a 2f 0a 0a 20  collisions */.. 
199fa 20 20 20 69 64 78 20 3d 20 69 46 72 61 6d 65 20     idx = iFrame 
199fb 2d 20 69 5a 65 72 6f 3b 0a 20 20 20 20 61 73 73  - iZero;.    ass
199fc 65 72 74 28 20 69 64 78 20 3c 3d 20 48 41 53 48  ert( idx <= HASH
199fd 54 41 42 4c 45 5f 4e 53 4c 4f 54 2f 32 20 2b 20  TABLE_NSLOT/2 + 
199fe 31 20 29 3b 0a 20 20 20 20 0a 20 20 20 20 2f 2a  1 );.    .    /*
199ff 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
19a00 66 69 72 73 74 20 65 6e 74 72 79 20 74 6f 20 62  first entry to b
19a01 65 20 61 64 64 65 64 20 74 6f 20 74 68 69 73 20  e added to this 
19a02 68 61 73 68 2d 74 61 62 6c 65 2c 20 7a 65 72 6f  hash-table, zero
19a03 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74 69   the.    ** enti
19a04 72 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e  re hash table an
19a05 64 20 61 50 67 6e 6f 5b 5d 20 61 72 72 61 79 20  d aPgno[] array 
19a06 62 65 66 6f 72 65 20 70 72 6f 63 65 64 69 6e 67  before proceding
19a07 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  . .    */.    if
19a08 28 20 69 64 78 3d 3d 31 20 29 7b 0a 20 20 20 20  ( idx==1 ){.    
19a09 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 69    int nByte = (i
19a0a 6e 74 29 28 28 75 38 20 2a 29 26 61 48 61 73 68  nt)((u8 *)&aHash
19a0b 5b 48 41 53 48 54 41 42 4c 45 5f 4e 53 4c 4f 54  [HASHTABLE_NSLOT
19a0c 5d 20 2d 20 28 75 38 20 2a 29 26 61 50 67 6e 6f  ] - (u8 *)&aPgno
19a0d 5b 31 5d 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73  [1]);.      mems
19a0e 65 74 28 28 76 6f 69 64 2a 29 26 61 50 67 6e 6f  et((void*)&aPgno
19a0f 5b 31 5d 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a  [1], 0, nByte);.
19a10 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
19a11 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20 61 50   the entry in aP
19a12 67 6e 6f 5b 5d 20 69 73 20 61 6c 72 65 61 64 79  gno[] is already
19a13 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 70   set, then the p
19a14 72 65 76 69 6f 75 73 20 77 72 69 74 65 72 0a 20  revious writer. 
19a15 20 20 20 2a 2a 20 6d 75 73 74 20 68 61 76 65 20     ** must have 
19a16 65 78 69 74 65 64 20 75 6e 65 78 70 65 63 74 65  exited unexpecte
19a17 64 6c 79 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  dly in the middl
19a18 65 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69  e of a transacti
19a19 6f 6e 20 28 61 66 74 65 72 0a 20 20 20 20 2a 2a  on (after.    **
19a1a 20 77 72 69 74 69 6e 67 20 6f 6e 65 20 6f 72 20   writing one or 
19a1b 6d 6f 72 65 20 64 69 72 74 79 20 70 61 67 65 73  more dirty pages
19a1c 20 74 6f 20 74 68 65 20 57 41 4c 20 74 6f 20 66   to the WAL to f
19a1d 72 65 65 20 75 70 20 6d 65 6d 6f 72 79 29 2e 20  ree up memory). 
19a1e 0a 20 20 20 20 2a 2a 20 52 65 6d 6f 76 65 20 74  .    ** Remove t
19a1f 68 65 20 72 65 6d 6e 61 6e 74 73 20 6f 66 20 74  he remnants of t
19a20 68 61 74 20 77 72 69 74 65 72 73 20 75 6e 63 6f  hat writers unco
19a21 6d 6d 69 74 74 65 64 20 74 72 61 6e 73 61 63 74  mmitted transact
19a22 69 6f 6e 20 66 72 6f 6d 20 0a 20 20 20 20 2a 2a  ion from .    **
19a23 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65 20   the hash-table 
19a24 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 61  before writing a
19a25 6e 79 20 6e 65 77 20 65 6e 74 72 69 65 73 2e 0a  ny new entries..
19a26 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 61      */.    if( a
19a27 50 67 6e 6f 5b 69 64 78 5d 20 29 7b 0a 20 20 20  Pgno[idx] ){.   
19a28 20 20 20 77 61 6c 43 6c 65 61 6e 75 70 48 61 73     walCleanupHas
19a29 68 28 70 57 61 6c 29 3b 0a 20 20 20 20 20 20 61  h(pWal);.      a
19a2a 73 73 65 72 74 28 20 21 61 50 67 6e 6f 5b 69 64  ssert( !aPgno[id
19a2b 78 5d 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  x] );.    }..   
19a2c 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 61 50   /* Write the aP
19a2d 67 6e 6f 5b 5d 20 61 72 72 61 79 20 65 6e 74 72  gno[] array entr
19a2e 79 20 61 6e 64 20 74 68 65 20 68 61 73 68 2d 74  y and the hash-t
19a2f 61 62 6c 65 20 73 6c 6f 74 2e 20 2a 2f 0a 20 20  able slot. */.  
19a30 20 20 6e 43 6f 6c 6c 69 64 65 20 3d 20 69 64 78    nCollide = idx
19a31 3b 0a 20 20 20 20 66 6f 72 28 69 4b 65 79 3d 77  ;.    for(iKey=w
19a32 61 6c 48 61 73 68 28 69 50 61 67 65 29 3b 20 61  alHash(iPage); a
19a33 48 61 73 68 5b 69 4b 65 79 5d 3b 20 69 4b 65 79  Hash[iKey]; iKey
19a34 3d 77 61 6c 4e 65 78 74 48 61 73 68 28 69 4b 65  =walNextHash(iKe
19a35 79 29 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28  y)){.      if( (
19a36 6e 43 6f 6c 6c 69 64 65 2d 2d 29 3d 3d 30 20 29  nCollide--)==0 )
19a37 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
19a38 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
19a39 20 7d 0a 20 20 20 20 61 50 67 6e 6f 5b 69 64 78   }.    aPgno[idx
19a3a 5d 20 3d 20 69 50 61 67 65 3b 0a 20 20 20 20 61  ] = iPage;.    a
19a3b 48 61 73 68 5b 69 4b 65 79 5d 20 3d 20 28 68 74  Hash[iKey] = (ht
19a3c 5f 73 6c 6f 74 29 69 64 78 3b 0a 0a 23 69 66 64  _slot)idx;..#ifd
19a3d 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
19a3e 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52  _EXPENSIVE_ASSER
19a3f 54 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20  T.    /* Verify 
19a40 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20  that the number 
19a41 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  of entries in th
19a42 65 20 68 61 73 68 20 74 61 62 6c 65 20 65 78 61  e hash table exa
19a43 63 74 6c 79 20 65 71 75 61 6c 73 0a 20 20 20 20  ctly equals.    
19a44 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
19a45 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
19a46 6d 61 70 70 69 6e 67 20 72 65 67 69 6f 6e 2e 0a  mapping region..
19a47 20 20 20 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20      */.    {.   
19a48 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20     int i;       
19a49 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
19a4a 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ter */.      int
19a4b 20 6e 45 6e 74 72 79 20 3d 20 30 3b 20 20 2f 2a   nEntry = 0;  /*
19a4c 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
19a4d 65 73 20 69 6e 20 74 68 65 20 68 61 73 68 20 74  es in the hash t
19a4e 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 66 6f  able */.      fo
19a4f 72 28 69 3d 30 3b 20 69 3c 48 41 53 48 54 41 42  r(i=0; i<HASHTAB
19a50 4c 45 5f 4e 53 4c 4f 54 3b 20 69 2b 2b 29 7b 20  LE_NSLOT; i++){ 
19a51 69 66 28 20 61 48 61 73 68 5b 69 5d 20 29 20 6e  if( aHash[i] ) n
19a52 45 6e 74 72 79 2b 2b 3b 20 7d 0a 20 20 20 20 20  Entry++; }.     
19a53 20 61 73 73 65 72 74 28 20 6e 45 6e 74 72 79 3d   assert( nEntry=
19a54 3d 69 64 78 20 29 3b 0a 20 20 20 20 7d 0a 0a 20  =idx );.    }.. 
19a55 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61     /* Verify tha
19a56 74 20 74 68 65 20 65 76 65 72 79 20 65 6e 74 72  t the every entr
19a57 79 20 69 6e 20 74 68 65 20 6d 61 70 70 69 6e 67  y in the mapping
19a58 20 72 65 67 69 6f 6e 20 69 73 20 72 65 61 63 68   region is reach
19a59 61 62 6c 65 0a 20 20 20 20 2a 2a 20 76 69 61 20  able.    ** via 
19a5a 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 20  the hash table. 
19a5b 20 54 68 69 73 20 74 75 72 6e 73 20 6f 75 74 20   This turns out 
19a5c 74 6f 20 62 65 20 61 20 72 65 61 6c 6c 79 2c 20  to be a really, 
19a5d 72 65 61 6c 6c 79 20 65 78 70 65 6e 73 69 76 65  really expensive
19a5e 0a 20 20 20 20 2a 2a 20 74 68 69 6e 67 20 74 6f  .    ** thing to
19a5f 20 63 68 65 63 6b 2c 20 73 6f 20 6f 6e 6c 79 20   check, so only 
19a60 64 6f 20 74 68 69 73 20 6f 63 63 61 73 69 6f 6e  do this occasion
19a61 61 6c 6c 79 20 2d 20 6e 6f 74 20 6f 6e 20 65 76  ally - not on ev
19a62 65 72 79 0a 20 20 20 20 2a 2a 20 69 74 65 72 61  ery.    ** itera
19a63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tion..    */.   
19a64 20 69 66 28 20 28 69 64 78 26 30 78 33 66 66 29   if( (idx&0x3ff)
19a65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ==0 ){.      int
19a66 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   i;           /*
19a67 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
19a68 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  .      for(i=1; 
19a69 69 3c 3d 69 64 78 3b 20 69 2b 2b 29 7b 0a 20 20  i<=idx; i++){.  
19a6a 20 20 20 20 20 20 66 6f 72 28 69 4b 65 79 3d 77        for(iKey=w
19a6b 61 6c 48 61 73 68 28 61 50 67 6e 6f 5b 69 5d 29  alHash(aPgno[i])
19a6c 3b 20 61 48 61 73 68 5b 69 4b 65 79 5d 3b 20 69  ; aHash[iKey]; i
19a6d 4b 65 79 3d 77 61 6c 4e 65 78 74 48 61 73 68 28  Key=walNextHash(
19a6e 69 4b 65 79 29 29 7b 0a 20 20 20 20 20 20 20 20  iKey)){.        
19a6f 20 20 69 66 28 20 61 48 61 73 68 5b 69 4b 65 79    if( aHash[iKey
19a70 5d 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20  ]==i ) break;.  
19a71 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
19a72 61 73 73 65 72 74 28 20 61 48 61 73 68 5b 69 4b  assert( aHash[iK
19a73 65 79 5d 3d 3d 69 20 29 3b 0a 20 20 20 20 20 20  ey]==i );.      
19a74 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f  }.    }.#endif /
19a75 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
19a76 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54  EXPENSIVE_ASSERT
19a77 20 2a 2f 0a 20 20 7d 0a 0a 0a 20 20 72 65 74 75   */.  }...  retu
19a78 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
19a79 20 52 65 63 6f 76 65 72 20 74 68 65 20 77 61 6c   Recover the wal
19a7a 2d 69 6e 64 65 78 20 62 79 20 72 65 61 64 69 6e  -index by readin
19a7b 67 20 74 68 65 20 77 72 69 74 65 2d 61 68 65 61  g the write-ahea
19a7c 64 20 6c 6f 67 20 66 69 6c 65 2e 20 0a 2a 2a 0a  d log file. .**.
19a7d 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
19a7e 66 69 72 73 74 20 74 72 69 65 73 20 74 6f 20 65  first tries to e
19a7f 73 74 61 62 6c 69 73 68 20 61 6e 20 65 78 63 6c  stablish an excl
19a80 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68  usive lock on th
19a81 65 0a 2a 2a 20 77 61 6c 2d 69 6e 64 65 78 20 74  e.** wal-index t
19a82 6f 20 70 72 65 76 65 6e 74 20 6f 74 68 65 72 20  o prevent other 
19a83 74 68 72 65 61 64 73 2f 70 72 6f 63 65 73 73 65  threads/processe
19a84 73 20 66 72 6f 6d 20 64 6f 69 6e 67 20 61 6e 79  s from doing any
19a85 74 68 69 6e 67 0a 2a 2a 20 77 69 74 68 20 74 68  thing.** with th
19a86 65 20 57 41 4c 20 6f 72 20 77 61 6c 2d 69 6e 64  e WAL or wal-ind
19a87 65 78 20 77 68 69 6c 65 20 72 65 63 6f 76 65 72  ex while recover
19a88 79 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20 54  y is running.  T
19a89 68 65 0a 2a 2a 20 57 41 4c 5f 52 45 43 4f 56 45  he.** WAL_RECOVE
19a8a 52 5f 4c 4f 43 4b 20 69 73 20 61 6c 73 6f 20 68  R_LOCK is also h
19a8b 65 6c 64 20 73 6f 20 74 68 61 74 20 6f 74 68 65  eld so that othe
19a8c 72 20 74 68 72 65 61 64 73 20 77 69 6c 6c 20 6b  r threads will k
19a8d 6e 6f 77 0a 2a 2a 20 74 68 61 74 20 74 68 69 73  now.** that this
19a8e 20 74 68 72 65 61 64 20 69 73 20 72 75 6e 6e 69   thread is runni
19a8f 6e 67 20 72 65 63 6f 76 65 72 79 2e 20 20 49 66  ng recovery.  If
19a90 20 75 6e 61 62 6c 65 20 74 6f 20 65 73 74 61 62   unable to estab
19a91 6c 69 73 68 0a 2a 2a 20 74 68 65 20 6e 65 63 65  lish.** the nece
19a92 73 73 61 72 79 20 6c 6f 63 6b 73 2c 20 74 68 69  ssary locks, thi
19a93 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
19a94 73 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a  s SQLITE_BUSY..*
19a95 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61 6c  /.static int wal
19a96 49 6e 64 65 78 52 65 63 6f 76 65 72 28 57 61 6c  IndexRecover(Wal
19a97 20 2a 70 57 61 6c 29 7b 0a 20 20 69 6e 74 20 72   *pWal){.  int r
19a98 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
19a99 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
19a9a 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69  turn Code */.  i
19a9b 36 34 20 6e 53 69 7a 65 3b 20 20 20 20 20 20 20  64 nSize;       
19a9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19a9d 2a 20 53 69 7a 65 20 6f 66 20 6c 6f 67 20 66 69  * Size of log fi
19a9e 6c 65 20 2a 2f 0a 20 20 75 33 32 20 61 46 72 61  le */.  u32 aFra
19a9f 6d 65 43 6b 73 75 6d 5b 32 5d 20 3d 20 7b 30 2c  meCksum[2] = {0,
19aa0 20 30 7d 3b 0a 20 20 69 6e 74 20 69 4c 6f 63 6b   0};.  int iLock
19aa1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
19aa2 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 6f         /* Lock o
19aa3 66 66 73 65 74 20 74 6f 20 6c 6f 63 6b 20 66 6f  ffset to lock fo
19aa4 72 20 63 68 65 63 6b 70 6f 69 6e 74 20 2a 2f 0a  r checkpoint */.
19aa5 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b 20 20 20 20    int nLock;    
19aa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19aa7 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c    /* Number of l
19aa8 6f 63 6b 73 20 74 6f 20 68 6f 6c 64 20 2a 2f 0a  ocks to hold */.
19aa9 0a 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 6e 20  .  /* Obtain an 
19aaa 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f  exclusive lock o
19aab 6e 20 61 6c 6c 20 62 79 74 65 20 69 6e 20 74 68  n all byte in th
19aac 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20  e locking range 
19aad 6e 6f 74 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a  not already.  **
19aae 20 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 20 63   locked by the c
19aaf 61 6c 6c 65 72 2e 20 54 68 65 20 63 61 6c 6c 65  aller. The calle
19ab0 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  r is guaranteed 
19ab1 74 6f 20 68 61 76 65 20 6c 6f 63 6b 65 64 20 74  to have locked t
19ab2 68 65 0a 20 20 2a 2a 20 57 41 4c 5f 57 52 49 54  he.  ** WAL_WRIT
19ab3 45 5f 4c 4f 43 4b 20 62 79 74 65 2c 20 61 6e 64  E_LOCK byte, and
19ab4 20 6d 61 79 20 68 61 76 65 20 61 6c 73 6f 20 6c   may have also l
19ab5 6f 63 6b 65 64 20 74 68 65 20 57 41 4c 5f 43 4b  ocked the WAL_CK
19ab6 50 54 5f 4c 4f 43 4b 20 62 79 74 65 2e 0a 20 20  PT_LOCK byte..  
19ab7 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
19ab8 2c 20 74 68 65 20 73 61 6d 65 20 62 79 74 65 73  , the same bytes
19ab9 20 74 68 61 74 20 61 72 65 20 6c 6f 63 6b 65 64   that are locked
19aba 20 68 65 72 65 20 61 72 65 20 75 6e 6c 6f 63 6b   here are unlock
19abb 65 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 74  ed before.  ** t
19abc 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
19abd 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  urns..  */.  ass
19abe 65 72 74 28 20 70 57 61 6c 2d 3e 63 6b 70 74 4c  ert( pWal->ckptL
19abf 6f 63 6b 3d 3d 31 20 7c 7c 20 70 57 61 6c 2d 3e  ock==1 || pWal->
19ac0 63 6b 70 74 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20  ckptLock==0 );. 
19ac1 20 61 73 73 65 72 74 28 20 57 41 4c 5f 41 4c 4c   assert( WAL_ALL
19ac2 5f 42 55 54 5f 57 52 49 54 45 3d 3d 57 41 4c 5f  _BUT_WRITE==WAL_
19ac3 57 52 49 54 45 5f 4c 4f 43 4b 2b 31 20 29 3b 0a  WRITE_LOCK+1 );.
19ac4 20 20 61 73 73 65 72 74 28 20 57 41 4c 5f 43 4b    assert( WAL_CK
19ac5 50 54 5f 4c 4f 43 4b 3d 3d 57 41 4c 5f 41 4c 4c  PT_LOCK==WAL_ALL
19ac6 5f 42 55 54 5f 57 52 49 54 45 20 29 3b 0a 20 20  _BUT_WRITE );.  
19ac7 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 77 72  assert( pWal->wr
19ac8 69 74 65 4c 6f 63 6b 20 29 3b 0a 20 20 69 4c 6f  iteLock );.  iLo
19ac9 63 6b 20 3d 20 57 41 4c 5f 41 4c 4c 5f 42 55 54  ck = WAL_ALL_BUT
19aca 5f 57 52 49 54 45 20 2b 20 70 57 61 6c 2d 3e 63  _WRITE + pWal->c
19acb 6b 70 74 4c 6f 63 6b 3b 0a 20 20 6e 4c 6f 63 6b  kptLock;.  nLock
19acc 20 3d 20 53 51 4c 49 54 45 5f 53 48 4d 5f 4e 4c   = SQLITE_SHM_NL
19acd 4f 43 4b 20 2d 20 69 4c 6f 63 6b 3b 0a 20 20 72  OCK - iLock;.  r
19ace 63 20 3d 20 77 61 6c 4c 6f 63 6b 45 78 63 6c 75  c = walLockExclu
19acf 73 69 76 65 28 70 57 61 6c 2c 20 69 4c 6f 63 6b  sive(pWal, iLock
19ad0 2c 20 6e 4c 6f 63 6b 29 3b 0a 20 20 69 66 28 20  , nLock);.  if( 
19ad1 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  rc ){.    return
19ad2 20 72 63 3b 0a 20 20 7d 0a 20 20 57 41 4c 54 52   rc;.  }.  WALTR
19ad3 41 43 45 28 28 22 57 41 4c 25 70 3a 20 72 65 63  ACE(("WAL%p: rec
19ad4 6f 76 65 72 79 20 62 65 67 69 6e 2e 2e 2e 5c 6e  overy begin...\n
19ad5 22 2c 20 70 57 61 6c 29 29 3b 0a 0a 20 20 6d 65  ", pWal));..  me
19ad6 6d 73 65 74 28 26 70 57 61 6c 2d 3e 68 64 72 2c  mset(&pWal->hdr,
19ad7 20 30 2c 20 73 69 7a 65 6f 66 28 57 61 6c 49 6e   0, sizeof(WalIn
19ad8 64 65 78 48 64 72 29 29 3b 0a 0a 20 20 72 63 20  dexHdr));..  rc 
19ad9 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53  = sqlite3OsFileS
19ada 69 7a 65 28 70 57 61 6c 2d 3e 70 57 61 6c 46 64  ize(pWal->pWalFd
19adb 2c 20 26 6e 53 69 7a 65 29 3b 0a 20 20 69 66 28  , &nSize);.  if(
19adc 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
19add 7b 0a 20 20 20 20 67 6f 74 6f 20 72 65 63 6f 76  {.    goto recov
19ade 65 72 79 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 0a  ery_error;.  }..
19adf 20 20 69 66 28 20 6e 53 69 7a 65 3e 57 41 4c 5f    if( nSize>WAL_
19ae0 48 44 52 53 49 5a 45 20 29 7b 0a 20 20 20 20 75  HDRSIZE ){.    u
19ae1 38 20 61 42 75 66 5b 57 41 4c 5f 48 44 52 53 49  8 aBuf[WAL_HDRSI
19ae2 5a 45 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ZE];         /* 
19ae3 42 75 66 66 65 72 20 74 6f 20 6c 6f 61 64 20 57  Buffer to load W
19ae4 41 4c 20 68 65 61 64 65 72 20 69 6e 74 6f 20 2a  AL header into *
19ae5 2f 0a 20 20 20 20 75 38 20 2a 61 46 72 61 6d 65  /.    u8 *aFrame
19ae6 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
19ae7 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20      /* Malloc'd 
19ae8 62 75 66 66 65 72 20 74 6f 20 6c 6f 61 64 20 65  buffer to load e
19ae9 6e 74 69 72 65 20 66 72 61 6d 65 20 2a 2f 0a 20  ntire frame */. 
19aea 20 20 20 69 6e 74 20 73 7a 46 72 61 6d 65 3b 20     int szFrame; 
19aeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19aec 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
19aed 74 65 73 20 69 6e 20 62 75 66 66 65 72 20 61 46  tes in buffer aF
19aee 72 61 6d 65 5b 5d 20 2a 2f 0a 20 20 20 20 75 38  rame[] */.    u8
19aef 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20   *aData;        
19af0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
19af1 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 20 70  ointer to data p
19af2 61 72 74 20 6f 66 20 61 46 72 61 6d 65 20 62 75  art of aFrame bu
19af3 66 66 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ffer */.    int 
19af4 69 46 72 61 6d 65 3b 20 20 20 20 20 20 20 20 20  iFrame;         
19af5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
19af6 65 78 20 6f 66 20 6c 61 73 74 20 66 72 61 6d 65  ex of last frame
19af7 20 72 65 61 64 20 2a 2f 0a 20 20 20 20 69 36 34   read */.    i64
19af8 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20   iOffset;       
19af9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
19afa 78 74 20 6f 66 66 73 65 74 20 74 6f 20 72 65 61  xt offset to rea
19afb 64 20 66 72 6f 6d 20 6c 6f 67 20 66 69 6c 65 20  d from log file 
19afc 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 61 67  */.    int szPag
19afd 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
19afe 20 20 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a       /* Page siz
19aff 65 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  e according to t
19b00 68 65 20 6c 6f 67 20 2a 2f 0a 20 20 20 20 75 33  he log */.    u3
19b01 32 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20  2 magic;        
19b02 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
19b03 61 67 69 63 20 76 61 6c 75 65 20 72 65 61 64 20  agic value read 
19b04 66 72 6f 6d 20 57 41 4c 20 68 65 61 64 65 72 20  from WAL header 
19b05 2a 2f 0a 20 20 20 20 75 33 32 20 76 65 72 73 69  */.    u32 versi
19b06 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  on;             
19b07 20 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 76 61       /* Magic va
19b08 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 57 41  lue read from WA
19b09 4c 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20  L header */.    
19b0a 69 6e 74 20 69 73 56 61 6c 69 64 3b 20 20 20 20  int isValid;    
19b0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19b0c 20 54 72 75 65 20 69 66 20 74 68 69 73 20 66 72   True if this fr
19b0d 61 6d 65 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a  ame is valid */.
19b0e 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 69 6e 20  .    /* Read in 
19b0f 74 68 65 20 57 41 4c 20 68 65 61 64 65 72 2e 20  the WAL header. 
19b10 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  */.    rc = sqli
19b11 74 65 33 4f 73 52 65 61 64 28 70 57 61 6c 2d 3e  te3OsRead(pWal->
19b12 70 57 61 6c 46 64 2c 20 61 42 75 66 2c 20 57 41  pWalFd, aBuf, WA
19b13 4c 5f 48 44 52 53 49 5a 45 2c 20 30 29 3b 0a 20  L_HDRSIZE, 0);. 
19b14 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
19b15 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f  E_OK ){.      go
19b16 74 6f 20 72 65 63 6f 76 65 72 79 5f 65 72 72 6f  to recovery_erro
19b17 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  r;.    }..    /*
19b18 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
19b19 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f   page size is no
19b1a 74 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f  t a power of two
19b1b 2c 20 6f 72 20 69 73 20 67 72 65 61 74 65 72 20  , or is greater 
19b1c 74 68 61 6e 0a 20 20 20 20 2a 2a 20 53 51 4c 49  than.    ** SQLI
19b1d 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
19b1e 2c 20 63 6f 6e 63 6c 75 64 65 20 74 68 61 74 20  , conclude that 
19b1f 74 68 65 20 57 41 4c 20 66 69 6c 65 20 63 6f 6e  the WAL file con
19b20 74 61 69 6e 73 20 6e 6f 20 76 61 6c 69 64 20 0a  tains no valid .
19b21 20 20 20 20 2a 2a 20 64 61 74 61 2e 20 53 69 6d      ** data. Sim
19b22 69 6c 61 72 6c 79 2c 20 69 66 20 74 68 65 20 27  ilarly, if the '
19b23 6d 61 67 69 63 27 20 76 61 6c 75 65 20 69 73 20  magic' value is 
19b24 69 6e 76 61 6c 69 64 2c 20 69 67 6e 6f 72 65 20  invalid, ignore 
19b25 74 68 65 20 77 68 6f 6c 65 0a 20 20 20 20 2a 2a  the whole.    **
19b26 20 57 41 4c 20 66 69 6c 65 2e 0a 20 20 20 20 2a   WAL file..    *
19b27 2f 0a 20 20 20 20 6d 61 67 69 63 20 3d 20 73 71  /.    magic = sq
19b28 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 26 61  lite3Get4byte(&a
19b29 42 75 66 5b 30 5d 29 3b 0a 20 20 20 20 73 7a 50  Buf[0]);.    szP
19b2a 61 67 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74  age = sqlite3Get
19b2b 34 62 79 74 65 28 26 61 42 75 66 5b 38 5d 29 3b  4byte(&aBuf[8]);
19b2c 0a 20 20 20 20 69 66 28 20 28 6d 61 67 69 63 26  .    if( (magic&
19b2d 30 78 46 46 46 46 46 46 46 45 29 21 3d 57 41 4c  0xFFFFFFFE)!=WAL
19b2e 5f 4d 41 47 49 43 20 0a 20 20 20 20 20 7c 7c 20  _MAGIC .     || 
19b2f 73 7a 50 61 67 65 26 28 73 7a 50 61 67 65 2d 31  szPage&(szPage-1
19b30 29 20 0a 20 20 20 20 20 7c 7c 20 73 7a 50 61 67  ) .     || szPag
19b31 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  e>SQLITE_MAX_PAG
19b32 45 5f 53 49 5a 45 20 0a 20 20 20 20 20 7c 7c 20  E_SIZE .     || 
19b33 73 7a 50 61 67 65 3c 35 31 32 20 0a 20 20 20 20  szPage<512 .    
19b34 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 69  ){.      goto fi
19b35 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a 20 20  nished;.    }.  
19b36 20 20 70 57 61 6c 2d 3e 68 64 72 2e 62 69 67 45    pWal->hdr.bigE
19b37 6e 64 43 6b 73 75 6d 20 3d 20 28 75 38 29 28 6d  ndCksum = (u8)(m
19b38 61 67 69 63 26 30 78 30 30 30 30 30 30 30 31 29  agic&0x00000001)
19b39 3b 0a 20 20 20 20 70 57 61 6c 2d 3e 73 7a 50 61  ;.    pWal->szPa
19b3a 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20 20  ge = szPage;.   
19b3b 20 70 57 61 6c 2d 3e 6e 43 6b 70 74 20 3d 20 73   pWal->nCkpt = s
19b3c 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 26  qlite3Get4byte(&
19b3d 61 42 75 66 5b 31 32 5d 29 3b 0a 20 20 20 20 6d  aBuf[12]);.    m
19b3e 65 6d 63 70 79 28 26 70 57 61 6c 2d 3e 68 64 72  emcpy(&pWal->hdr
19b3f 2e 61 53 61 6c 74 2c 20 26 61 42 75 66 5b 31 36  .aSalt, &aBuf[16
19b40 5d 2c 20 38 29 3b 0a 0a 20 20 20 20 2f 2a 20 56  ], 8);..    /* V
19b41 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 57  erify that the W
19b42 41 4c 20 68 65 61 64 65 72 20 63 68 65 63 6b 73  AL header checks
19b43 75 6d 20 69 73 20 63 6f 72 72 65 63 74 20 2a 2f  um is correct */
19b44 0a 20 20 20 20 77 61 6c 43 68 65 63 6b 73 75 6d  .    walChecksum
19b45 42 79 74 65 73 28 70 57 61 6c 2d 3e 68 64 72 2e  Bytes(pWal->hdr.
19b46 62 69 67 45 6e 64 43 6b 73 75 6d 3d 3d 53 51 4c  bigEndCksum==SQL
19b47 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 2c 20 0a  ITE_BIGENDIAN, .
19b48 20 20 20 20 20 20 20 20 61 42 75 66 2c 20 57 41          aBuf, WA
19b49 4c 5f 48 44 52 53 49 5a 45 2d 32 2a 34 2c 20 30  L_HDRSIZE-2*4, 0
19b4a 2c 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46 72 61  , pWal->hdr.aFra
19b4b 6d 65 43 6b 73 75 6d 0a 20 20 20 20 29 3b 0a 20  meCksum.    );. 
19b4c 20 20 20 69 66 28 20 70 57 61 6c 2d 3e 68 64 72     if( pWal->hdr
19b4d 2e 61 46 72 61 6d 65 43 6b 73 75 6d 5b 30 5d 21  .aFrameCksum[0]!
19b4e 3d 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65  =sqlite3Get4byte
19b4f 28 26 61 42 75 66 5b 32 34 5d 29 0a 20 20 20 20  (&aBuf[24]).    
19b50 20 7c 7c 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46   || pWal->hdr.aF
19b51 72 61 6d 65 43 6b 73 75 6d 5b 31 5d 21 3d 73 71  rameCksum[1]!=sq
19b52 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 26 61  lite3Get4byte(&a
19b53 42 75 66 5b 32 38 5d 29 0a 20 20 20 20 29 7b 0a  Buf[28]).    ){.
19b54 20 20 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73        goto finis
19b55 68 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  hed;.    }..    
19b56 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  /* Verify that t
19b57 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65  he version numbe
19b58 72 20 6f 6e 20 74 68 65 20 57 41 4c 20 66 6f 72  r on the WAL for
19b59 6d 61 74 20 69 73 20 6f 6e 65 20 74 68 61 74 0a  mat is one that.
19b5a 20 20 20 20 2a 2a 20 61 72 65 20 61 62 6c 65 20      ** are able 
19b5b 74 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 2a 2f  to understand */
19b5c 0a 20 20 20 20 76 65 72 73 69 6f 6e 20 3d 20 73  .    version = s
19b5d 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 26  qlite3Get4byte(&
19b5e 61 42 75 66 5b 34 5d 29 3b 0a 20 20 20 20 69 66  aBuf[4]);.    if
19b5f 28 20 76 65 72 73 69 6f 6e 21 3d 57 41 4c 5f 4d  ( version!=WAL_M
19b60 41 58 5f 56 45 52 53 49 4f 4e 20 29 7b 0a 20 20  AX_VERSION ){.  
19b61 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
19b62 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 3b 0a 20  CANTOPEN_BKPT;. 
19b63 20 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68       goto finish
19b64 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ed;.    }..    /
19b65 2a 20 4d 61 6c 6c 6f 63 20 61 20 62 75 66 66 65  * Malloc a buffe
19b66 72 20 74 6f 20 72 65 61 64 20 66 72 61 6d 65 73  r to read frames
19b67 20 69 6e 74 6f 2e 20 2a 2f 0a 20 20 20 20 73 7a   into. */.    sz
19b68 46 72 61 6d 65 20 3d 20 73 7a 50 61 67 65 20 2b  Frame = szPage +
19b69 20 57 41 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49   WAL_FRAME_HDRSI
19b6a 5a 45 3b 0a 20 20 20 20 61 46 72 61 6d 65 20 3d  ZE;.    aFrame =
19b6b 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 5f 6d   (u8 *)sqlite3_m
19b6c 61 6c 6c 6f 63 28 73 7a 46 72 61 6d 65 29 3b 0a  alloc(szFrame);.
19b6d 20 20 20 20 69 66 28 20 21 61 46 72 61 6d 65 20      if( !aFrame 
19b6e 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
19b6f 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
19b70 20 20 67 6f 74 6f 20 72 65 63 6f 76 65 72 79 5f    goto recovery_
19b71 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20  error;.    }.   
19b72 20 61 44 61 74 61 20 3d 20 26 61 46 72 61 6d 65   aData = &aFrame
19b73 5b 57 41 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49  [WAL_FRAME_HDRSI
19b74 5a 45 5d 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61  ZE];..    /* Rea
19b75 64 20 61 6c 6c 20 66 72 61 6d 65 73 20 66 72 6f  d all frames fro
19b76 6d 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e 20  m the log file. 
19b77 2a 2f 0a 20 20 20 20 69 46 72 61 6d 65 20 3d 20  */.    iFrame = 
19b78 30 3b 0a 20 20 20 20 66 6f 72 28 69 4f 66 66 73  0;.    for(iOffs
19b79 65 74 3d 57 41 4c 5f 48 44 52 53 49 5a 45 3b 20  et=WAL_HDRSIZE; 
19b7a 28 69 4f 66 66 73 65 74 2b 73 7a 46 72 61 6d 65  (iOffset+szFrame
19b7b 29 3c 3d 6e 53 69 7a 65 3b 20 69 4f 66 66 73 65  )<=nSize; iOffse
19b7c 74 2b 3d 73 7a 46 72 61 6d 65 29 7b 0a 20 20 20  t+=szFrame){.   
19b7d 20 20 20 75 33 32 20 70 67 6e 6f 3b 20 20 20 20     u32 pgno;    
19b7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19b7f 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 65 20  * Database page 
19b80 6e 75 6d 62 65 72 20 66 6f 72 20 66 72 61 6d 65  number for frame
19b81 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 6e 54   */.      u32 nT
19b82 72 75 6e 63 61 74 65 3b 20 20 20 20 20 20 20 20  runcate;        
19b83 20 20 20 20 20 20 2f 2a 20 64 62 73 69 7a 65 20        /* dbsize 
19b84 66 69 65 6c 64 20 66 72 6f 6d 20 66 72 61 6d 65  field from frame
19b85 20 68 65 61 64 65 72 20 2a 2f 0a 0a 20 20 20 20   header */..    
19b86 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 64 65    /* Read and de
19b87 63 6f 64 65 20 74 68 65 20 6e 65 78 74 20 6c 6f  code the next lo
19b88 67 20 66 72 61 6d 65 2e 20 2a 2f 0a 20 20 20 20  g frame. */.    
19b89 20 20 69 46 72 61 6d 65 2b 2b 3b 0a 20 20 20 20    iFrame++;.    
19b8a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
19b8b 52 65 61 64 28 70 57 61 6c 2d 3e 70 57 61 6c 46  Read(pWal->pWalF
19b8c 64 2c 20 61 46 72 61 6d 65 2c 20 73 7a 46 72 61  d, aFrame, szFra
19b8d 6d 65 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20  me, iOffset);.  
19b8e 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
19b8f 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20  TE_OK ) break;. 
19b90 20 20 20 20 20 69 73 56 61 6c 69 64 20 3d 20 77       isValid = w
19b91 61 6c 44 65 63 6f 64 65 46 72 61 6d 65 28 70 57  alDecodeFrame(pW
19b92 61 6c 2c 20 26 70 67 6e 6f 2c 20 26 6e 54 72 75  al, &pgno, &nTru
19b93 6e 63 61 74 65 2c 20 61 44 61 74 61 2c 20 61 46  ncate, aData, aF
19b94 72 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28  rame);.      if(
19b95 20 21 69 73 56 61 6c 69 64 20 29 20 62 72 65 61   !isValid ) brea
19b96 6b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 61  k;.      rc = wa
19b97 6c 49 6e 64 65 78 41 70 70 65 6e 64 28 70 57 61  lIndexAppend(pWa
19b98 6c 2c 20 69 46 72 61 6d 65 2c 20 70 67 6e 6f 29  l, iFrame, pgno)
19b99 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
19b9a 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61  SQLITE_OK ) brea
19b9b 6b 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  k;..      /* If 
19b9c 6e 54 72 75 6e 63 61 74 65 20 69 73 20 6e 6f 6e  nTruncate is non
19b9d 2d 7a 65 72 6f 2c 20 74 68 69 73 20 69 73 20 61  -zero, this is a
19b9e 20 63 6f 6d 6d 69 74 20 72 65 63 6f 72 64 2e 20   commit record. 
19b9f 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 54 72  */.      if( nTr
19ba0 75 6e 63 61 74 65 20 29 7b 0a 20 20 20 20 20 20  uncate ){.      
19ba1 20 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72    pWal->hdr.mxFr
19ba2 61 6d 65 20 3d 20 69 46 72 61 6d 65 3b 0a 20 20  ame = iFrame;.  
19ba3 20 20 20 20 20 20 70 57 61 6c 2d 3e 68 64 72 2e        pWal->hdr.
19ba4 6e 50 61 67 65 20 3d 20 6e 54 72 75 6e 63 61 74  nPage = nTruncat
19ba5 65 3b 0a 20 20 20 20 20 20 20 20 70 57 61 6c 2d  e;.        pWal-
19ba6 3e 68 64 72 2e 73 7a 50 61 67 65 20 3d 20 28 75  >hdr.szPage = (u
19ba7 31 36 29 28 28 73 7a 50 61 67 65 26 30 78 66 66  16)((szPage&0xff
19ba8 30 30 29 20 7c 20 28 73 7a 50 61 67 65 3e 3e 31  00) | (szPage>>1
19ba9 36 29 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73  6));.        tes
19baa 74 63 61 73 65 28 20 73 7a 50 61 67 65 3c 3d 33  tcase( szPage<=3
19bab 32 37 36 38 20 29 3b 0a 20 20 20 20 20 20 20 20  2768 );.        
19bac 74 65 73 74 63 61 73 65 28 20 73 7a 50 61 67 65  testcase( szPage
19bad 3e 3d 36 35 35 33 36 20 29 3b 0a 20 20 20 20 20  >=65536 );.     
19bae 20 20 20 61 46 72 61 6d 65 43 6b 73 75 6d 5b 30     aFrameCksum[0
19baf 5d 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46  ] = pWal->hdr.aF
19bb0 72 61 6d 65 43 6b 73 75 6d 5b 30 5d 3b 0a 20 20  rameCksum[0];.  
19bb1 20 20 20 20 20 20 61 46 72 61 6d 65 43 6b 73 75        aFrameCksu
19bb2 6d 5b 31 5d 20 3d 20 70 57 61 6c 2d 3e 68 64 72  m[1] = pWal->hdr
19bb3 2e 61 46 72 61 6d 65 43 6b 73 75 6d 5b 31 5d 3b  .aFrameCksum[1];
19bb4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
19bb5 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
19bb6 28 61 46 72 61 6d 65 29 3b 0a 20 20 7d 0a 0a 66  (aFrame);.  }..f
19bb7 69 6e 69 73 68 65 64 3a 0a 20 20 69 66 28 20 72  inished:.  if( r
19bb8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
19bb9 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 57 61 6c      volatile Wal
19bba 43 6b 70 74 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b  CkptInfo *pInfo;
19bbb 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
19bbc 70 57 61 6c 2d 3e 68 64 72 2e 61 46 72 61 6d 65  pWal->hdr.aFrame
19bbd 43 6b 73 75 6d 5b 30 5d 20 3d 20 61 46 72 61 6d  Cksum[0] = aFram
19bbe 65 43 6b 73 75 6d 5b 30 5d 3b 0a 20 20 20 20 70  eCksum[0];.    p
19bbf 57 61 6c 2d 3e 68 64 72 2e 61 46 72 61 6d 65 43  Wal->hdr.aFrameC
19bc0 6b 73 75 6d 5b 31 5d 20 3d 20 61 46 72 61 6d 65  ksum[1] = aFrame
19bc1 43 6b 73 75 6d 5b 31 5d 3b 0a 20 20 20 20 77 61  Cksum[1];.    wa
19bc2 6c 49 6e 64 65 78 57 72 69 74 65 48 64 72 28 70  lIndexWriteHdr(p
19bc3 57 61 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65  Wal);..    /* Re
19bc4 73 65 74 20 74 68 65 20 63 68 65 63 6b 70 6f 69  set the checkpoi
19bc5 6e 74 2d 68 65 61 64 65 72 2e 20 54 68 69 73 20  nt-header. This 
19bc6 69 73 20 73 61 66 65 20 62 65 63 61 75 73 65 20  is safe because 
19bc7 74 68 69 73 20 74 68 72 65 61 64 20 69 73 20 0a  this thread is .
19bc8 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 6c 79      ** currently
19bc9 20 68 6f 6c 64 69 6e 67 20 6c 6f 63 6b 73 20 74   holding locks t
19bca 68 61 74 20 65 78 63 6c 75 64 65 20 61 6c 6c 20  hat exclude all 
19bcb 6f 74 68 65 72 20 72 65 61 64 65 72 73 2c 20 77  other readers, w
19bcc 72 69 74 65 72 73 20 61 6e 64 0a 20 20 20 20 2a  riters and.    *
19bcd 2a 20 63 68 65 63 6b 70 6f 69 6e 74 65 72 73 2e  * checkpointers.
19bce 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 6e 66  .    */.    pInf
19bcf 6f 20 3d 20 77 61 6c 43 6b 70 74 49 6e 66 6f 28  o = walCkptInfo(
19bd0 70 57 61 6c 29 3b 0a 20 20 20 20 70 49 6e 66 6f  pWal);.    pInfo
19bd1 2d 3e 6e 42 61 63 6b 66 69 6c 6c 20 3d 20 30 3b  ->nBackfill = 0;
19bd2 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 61 52 65 61  .    pInfo->aRea
19bd3 64 4d 61 72 6b 5b 30 5d 20 3d 20 30 3b 0a 20 20  dMark[0] = 0;.  
19bd4 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 57 41 4c    for(i=1; i<WAL
19bd5 5f 4e 52 45 41 44 45 52 3b 20 69 2b 2b 29 20 70  _NREADER; i++) p
19bd6 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61 72 6b 5b  Info->aReadMark[
19bd7 69 5d 20 3d 20 52 45 41 44 4d 41 52 4b 5f 4e 4f  i] = READMARK_NO
19bd8 54 5f 55 53 45 44 3b 0a 20 20 20 20 69 66 28 20  T_USED;.    if( 
19bd9 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d  pWal->hdr.mxFram
19bda 65 20 29 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64  e ) pInfo->aRead
19bdb 4d 61 72 6b 5b 31 5d 20 3d 20 70 57 61 6c 2d 3e  Mark[1] = pWal->
19bdc 68 64 72 2e 6d 78 46 72 61 6d 65 3b 0a 0a 20 20  hdr.mxFrame;..  
19bdd 20 20 2f 2a 20 49 66 20 6d 6f 72 65 20 74 68 61    /* If more tha
19bde 6e 20 6f 6e 65 20 66 72 61 6d 65 20 77 61 73 20  n one frame was 
19bdf 72 65 63 6f 76 65 72 65 64 20 66 72 6f 6d 20 74  recovered from t
19be0 68 65 20 6c 6f 67 20 66 69 6c 65 2c 20 72 65 70  he log file, rep
19be1 6f 72 74 20 61 6e 0a 20 20 20 20 2a 2a 20 65 76  ort an.    ** ev
19be2 65 6e 74 20 76 69 61 20 73 71 6c 69 74 65 33 5f  ent via sqlite3_
19be3 6c 6f 67 28 29 2e 20 54 68 69 73 20 69 73 20 74  log(). This is t
19be4 6f 20 68 65 6c 70 20 77 69 74 68 20 69 64 65 6e  o help with iden
19be5 74 69 66 79 69 6e 67 20 70 65 72 66 6f 72 6d 61  tifying performa
19be6 6e 63 65 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c  nce.    ** probl
19be7 65 6d 73 20 63 61 75 73 65 64 20 62 79 20 61 70  ems caused by ap
19be8 70 6c 69 63 61 74 69 6f 6e 73 20 72 6f 75 74 69  plications routi
19be9 6e 65 6c 79 20 73 68 75 74 74 69 6e 67 20 64 6f  nely shutting do
19bea 77 6e 20 77 69 74 68 6f 75 74 0a 20 20 20 20 2a  wn without.    *
19beb 2a 20 63 68 65 63 6b 70 6f 69 6e 74 69 6e 67 20  * checkpointing 
19bec 74 68 65 20 6c 6f 67 20 66 69 6c 65 2e 0a 20 20  the log file..  
19bed 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 57 61    */.    if( pWa
19bee 6c 2d 3e 68 64 72 2e 6e 50 61 67 65 20 29 7b 0a  l->hdr.nPage ){.
19bef 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f        sqlite3_lo
19bf0 67 28 53 51 4c 49 54 45 5f 4f 4b 2c 20 22 52 65  g(SQLITE_OK, "Re
19bf1 63 6f 76 65 72 65 64 20 25 64 20 66 72 61 6d 65  covered %d frame
19bf2 73 20 66 72 6f 6d 20 57 41 4c 20 66 69 6c 65 20  s from WAL file 
19bf3 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70  %s",.          p
19bf4 57 61 6c 2d 3e 68 64 72 2e 6e 50 61 67 65 2c 20  Wal->hdr.nPage, 
19bf5 70 57 61 6c 2d 3e 7a 57 61 6c 4e 61 6d 65 0a 20  pWal->zWalName. 
19bf6 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20       );.    }.  
19bf7 7d 0a 0a 72 65 63 6f 76 65 72 79 5f 65 72 72 6f  }..recovery_erro
19bf8 72 3a 0a 20 20 57 41 4c 54 52 41 43 45 28 28 22  r:.  WALTRACE(("
19bf9 57 41 4c 25 70 3a 20 72 65 63 6f 76 65 72 79 20  WAL%p: recovery 
19bfa 25 73 5c 6e 22 2c 20 70 57 61 6c 2c 20 72 63 20  %s\n", pWal, rc 
19bfb 3f 20 22 66 61 69 6c 65 64 22 20 3a 20 22 6f 6b  ? "failed" : "ok
19bfc 22 29 29 3b 0a 20 20 77 61 6c 55 6e 6c 6f 63 6b  "));.  walUnlock
19bfd 45 78 63 6c 75 73 69 76 65 28 70 57 61 6c 2c 20  Exclusive(pWal, 
19bfe 69 4c 6f 63 6b 2c 20 6e 4c 6f 63 6b 29 3b 0a 20  iLock, nLock);. 
19bff 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
19c00 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70  *.** Close an op
19c01 65 6e 20 77 61 6c 2d 69 6e 64 65 78 2e 0a 2a 2f  en wal-index..*/
19c02 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 61 6c  .static void wal
19c03 49 6e 64 65 78 43 6c 6f 73 65 28 57 61 6c 20 2a  IndexClose(Wal *
19c04 70 57 61 6c 2c 20 69 6e 74 20 69 73 44 65 6c 65  pWal, int isDele
19c05 74 65 29 7b 0a 20 20 69 66 28 20 70 57 61 6c 2d  te){.  if( pWal-
19c06 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3d 3d  >exclusiveMode==
19c07 57 41 4c 5f 48 45 41 50 4d 45 4d 4f 52 59 5f 4d  WAL_HEAPMEMORY_M
19c08 4f 44 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ODE ){.    int i
19c09 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
19c0a 3c 70 57 61 6c 2d 3e 6e 57 69 44 61 74 61 3b 20  <pWal->nWiData; 
19c0b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
19c0c 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20 2a  te3_free((void *
19c0d 29 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 61 5b  )pWal->apWiData[
19c0e 69 5d 29 3b 0a 20 20 20 20 20 20 70 57 61 6c 2d  i]);.      pWal-
19c0f 3e 61 70 57 69 44 61 74 61 5b 69 5d 20 3d 20 30  >apWiData[i] = 0
19c10 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
19c11 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53 68  .    sqlite3OsSh
19c12 6d 55 6e 6d 61 70 28 70 57 61 6c 2d 3e 70 44 62  mUnmap(pWal->pDb
19c13 46 64 2c 20 69 73 44 65 6c 65 74 65 29 3b 0a 20  Fd, isDelete);. 
19c14 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65   }.}../* .** Ope
19c15 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  n a connection t
19c16 6f 20 74 68 65 20 57 41 4c 20 66 69 6c 65 20 7a  o the WAL file z
19c17 57 61 6c 4e 61 6d 65 2e 20 54 68 65 20 64 61 74  WalName. The dat
19c18 61 62 61 73 65 20 66 69 6c 65 20 6d 75 73 74 20  abase file must 
19c19 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 20 6f  .** already be o
19c1a 70 65 6e 65 64 20 6f 6e 20 63 6f 6e 6e 65 63 74  pened on connect
19c1b 69 6f 6e 20 70 44 62 46 64 2e 20 54 68 65 20 62  ion pDbFd. The b
19c1c 75 66 66 65 72 20 74 68 61 74 20 7a 57 61 6c 4e  uffer that zWalN
19c1d 61 6d 65 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f  ame points.** to
19c1e 20 6d 75 73 74 20 72 65 6d 61 69 6e 20 76 61 6c   must remain val
19c1f 69 64 20 66 6f 72 20 74 68 65 20 6c 69 66 65 74  id for the lifet
19c20 69 6d 65 20 6f 66 20 74 68 65 20 72 65 74 75 72  ime of the retur
19c21 6e 65 64 20 57 61 6c 2a 20 68 61 6e 64 6c 65 2e  ned Wal* handle.
19c22 0a 2a 2a 0a 2a 2a 20 41 20 53 48 41 52 45 44 20  .**.** A SHARED 
19c23 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 62 65 20 68  lock should be h
19c24 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62  eld on the datab
19c25 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20 74 68  ase file when th
19c26 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69  is function.** i
19c27 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 20 70 75  s called. The pu
19c28 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 53 48  rpose of this SH
19c29 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 74 6f 20  ARED lock is to 
19c2a 70 72 65 76 65 6e 74 20 61 6e 79 20 6f 74 68 65  prevent any othe
19c2b 72 0a 2a 2a 20 63 6c 69 65 6e 74 20 66 72 6f 6d  r.** client from
19c2c 20 75 6e 6c 69 6e 6b 69 6e 67 20 74 68 65 20 57   unlinking the W
19c2d 41 4c 20 6f 72 20 77 61 6c 2d 69 6e 64 65 78 20  AL or wal-index 
19c2e 66 69 6c 65 2e 20 49 66 20 61 6e 6f 74 68 65 72  file. If another
19c2f 20 70 72 6f 63 65 73 73 0a 2a 2a 20 77 65 72 65   process.** were
19c30 20 74 6f 20 64 6f 20 74 68 69 73 20 6a 75 73 74   to do this just
19c31 20 61 66 74 65 72 20 74 68 69 73 20 63 6c 69 65   after this clie
19c32 6e 74 20 6f 70 65 6e 65 64 20 6f 6e 65 20 6f 66  nt opened one of
19c33 20 74 68 65 73 65 20 66 69 6c 65 73 2c 20 74 68   these files, th
19c34 65 0a 2a 2a 20 73 79 73 74 65 6d 20 77 6f 75 6c  e.** system woul
19c35 64 20 62 65 20 62 61 64 6c 79 20 62 72 6f 6b 65  d be badly broke
19c36 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  n..**.** If the 
19c37 6c 6f 67 20 66 69 6c 65 20 69 73 20 73 75 63 63  log file is succ
19c38 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c  essfully opened,
19c39 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
19c3a 74 75 72 6e 65 64 20 61 6e 64 20 0a 2a 2a 20 2a  turned and .** *
19c3b 70 70 57 61 6c 20 69 73 20 73 65 74 20 74 6f 20  ppWal is set to 
19c3c 70 6f 69 6e 74 20 74 6f 20 61 20 6e 65 77 20 57  point to a new W
19c3d 41 4c 20 68 61 6e 64 6c 65 2e 20 49 66 20 61 6e  AL handle. If an
19c3e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a   error occurs,.*
19c3f 2a 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  * an SQLite erro
19c40 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
19c41 65 64 20 61 6e 64 20 2a 70 70 57 61 6c 20 69 73  ed and *ppWal is
19c42 20 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 65 64   left unmodified
19c43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19c44 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
19c45 61 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65  alOpen(.  sqlite
19c46 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20  3_vfs *pVfs,    
19c47 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76 66 73            /* vfs
19c48 20 6d 6f 64 75 6c 65 20 74 6f 20 6f 70 65 6e 20   module to open 
19c49 77 61 6c 20 61 6e 64 20 77 61 6c 2d 69 6e 64 65  wal and wal-inde
19c4a 78 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  x */.  sqlite3_f
19c4b 69 6c 65 20 2a 70 44 62 46 64 2c 20 20 20 20 20  ile *pDbFd,     
19c4c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70         /* The op
19c4d 65 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  en database file
19c4e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
19c4f 20 2a 7a 57 61 6c 4e 61 6d 65 2c 20 20 20 20 20   *zWalName,     
19c50 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
19c51 20 74 68 65 20 57 41 4c 20 66 69 6c 65 20 2a 2f   the WAL file */
19c52 0a 20 20 69 6e 74 20 62 4e 6f 53 68 6d 2c 20 20  .  int bNoShm,  
19c53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19c54 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 72 75     /* True to ru
19c55 6e 20 69 6e 20 68 65 61 70 2d 6d 65 6d 6f 72 79  n in heap-memory
19c56 20 6d 6f 64 65 20 2a 2f 0a 20 20 69 36 34 20 6d   mode */.  i64 m
19c57 78 57 61 6c 53 69 7a 65 2c 20 20 20 20 20 20 20  xWalSize,       
19c58 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
19c59 75 6e 63 61 74 65 20 57 41 4c 20 74 6f 20 74 68  uncate WAL to th
19c5a 69 73 20 73 69 7a 65 20 6f 6e 20 72 65 73 65 74  is size on reset
19c5b 20 2a 2f 0a 20 20 57 61 6c 20 2a 2a 70 70 57 61   */.  Wal **ppWa
19c5c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
19c5d 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 6c        /* OUT: Al
19c5e 6c 6f 63 61 74 65 64 20 57 61 6c 20 68 61 6e 64  located Wal hand
19c5f 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  le */.){.  int r
19c60 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
19c61 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
19c62 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 57  turn Code */.  W
19c63 61 6c 20 2a 70 52 65 74 3b 20 20 20 20 20 20 20  al *pRet;       
19c64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19c65 2a 20 4f 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f  * Object to allo
19c66 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20  cate and return 
19c67 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20  */.  int flags; 
19c68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19c69 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61       /* Flags pa
19c6a 73 73 65 64 20 74 6f 20 4f 73 4f 70 65 6e 28 29  ssed to OsOpen()
19c6b 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 7a   */..  assert( z
19c6c 57 61 6c 4e 61 6d 65 20 26 26 20 7a 57 61 6c 4e  WalName && zWalN
19c6d 61 6d 65 5b 30 5d 20 29 3b 0a 20 20 61 73 73 65  ame[0] );.  asse
19c6e 72 74 28 20 70 44 62 46 64 20 29 3b 0a 0a 20 20  rt( pDbFd );..  
19c6f 2f 2a 20 49 6e 20 74 68 65 20 61 6d 61 6c 67 61  /* In the amalga
19c70 6d 61 74 69 6f 6e 2c 20 74 68 65 20 6f 73 5f 75  mation, the os_u
19c71 6e 69 78 2e 63 20 61 6e 64 20 6f 73 5f 77 69 6e  nix.c and os_win
19c72 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20  .c source files 
19c73 63 6f 6d 65 20 62 65 66 6f 72 65 0a 20 20 2a 2a  come before.  **
19c74 20 74 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c   this source fil
19c75 65 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20  e.  Verify that 
19c76 74 68 65 20 23 64 65 66 69 6e 65 73 20 6f 66 20  the #defines of 
19c77 74 68 65 20 6c 6f 63 6b 69 6e 67 20 62 79 74 65  the locking byte
19c78 20 6f 66 66 73 65 74 73 0a 20 20 2a 2a 20 69 6e   offsets.  ** in
19c79 20 6f 73 5f 75 6e 69 78 2e 63 20 61 6e 64 20 6f   os_unix.c and o
19c7a 73 5f 77 69 6e 2e 63 20 61 67 72 65 65 20 77 69  s_win.c agree wi
19c7b 74 68 20 74 68 65 20 57 41 4c 49 4e 44 45 58 5f  th the WALINDEX_
19c7c 4c 4f 43 4b 5f 4f 46 46 53 45 54 20 76 61 6c 75  LOCK_OFFSET valu
19c7d 65 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 57  e..  */.#ifdef W
19c7e 49 4e 5f 53 48 4d 5f 42 41 53 45 0a 20 20 61 73  IN_SHM_BASE.  as
19c7f 73 65 72 74 28 20 57 49 4e 5f 53 48 4d 5f 42 41  sert( WIN_SHM_BA
19c80 53 45 3d 3d 57 41 4c 49 4e 44 45 58 5f 4c 4f 43  SE==WALINDEX_LOC
19c81 4b 5f 4f 46 46 53 45 54 20 29 3b 0a 23 65 6e 64  K_OFFSET );.#end
19c82 69 66 0a 23 69 66 64 65 66 20 55 4e 49 58 5f 53  if.#ifdef UNIX_S
19c83 48 4d 5f 42 41 53 45 0a 20 20 61 73 73 65 72 74  HM_BASE.  assert
19c84 28 20 55 4e 49 58 5f 53 48 4d 5f 42 41 53 45 3d  ( UNIX_SHM_BASE=
19c85 3d 57 41 4c 49 4e 44 45 58 5f 4c 4f 43 4b 5f 4f  =WALINDEX_LOCK_O
19c86 46 46 53 45 54 20 29 3b 0a 23 65 6e 64 69 66 0a  FFSET );.#endif.
19c87 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
19c88 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 73  an instance of s
19c89 74 72 75 63 74 20 57 61 6c 20 74 6f 20 72 65 74  truct Wal to ret
19c8a 75 72 6e 2e 20 2a 2f 0a 20 20 2a 70 70 57 61 6c  urn. */.  *ppWal
19c8b 20 3d 20 30 3b 0a 20 20 70 52 65 74 20 3d 20 28   = 0;.  pRet = (
19c8c 57 61 6c 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  Wal*)sqlite3Mall
19c8d 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 57 61  ocZero(sizeof(Wa
19c8e 6c 29 20 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46  l) + pVfs->szOsF
19c8f 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 70 52 65  ile);.  if( !pRe
19c90 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
19c91 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
19c92 7d 0a 0a 20 20 70 52 65 74 2d 3e 70 56 66 73 20  }..  pRet->pVfs 
19c93 3d 20 70 56 66 73 3b 0a 20 20 70 52 65 74 2d 3e  = pVfs;.  pRet->
19c94 70 57 61 6c 46 64 20 3d 20 28 73 71 6c 69 74 65  pWalFd = (sqlite
19c95 33 5f 66 69 6c 65 20 2a 29 26 70 52 65 74 5b 31  3_file *)&pRet[1
19c96 5d 3b 0a 20 20 70 52 65 74 2d 3e 70 44 62 46 64  ];.  pRet->pDbFd
19c97 20 3d 20 70 44 62 46 64 3b 0a 20 20 70 52 65 74   = pDbFd;.  pRet
19c98 2d 3e 72 65 61 64 4c 6f 63 6b 20 3d 20 2d 31 3b  ->readLock = -1;
19c99 0a 20 20 70 52 65 74 2d 3e 6d 78 57 61 6c 53 69  .  pRet->mxWalSi
19c9a 7a 65 20 3d 20 6d 78 57 61 6c 53 69 7a 65 3b 0a  ze = mxWalSize;.
19c9b 20 20 70 52 65 74 2d 3e 7a 57 61 6c 4e 61 6d 65    pRet->zWalName
19c9c 20 3d 20 7a 57 61 6c 4e 61 6d 65 3b 0a 20 20 70   = zWalName;.  p
19c9d 52 65 74 2d 3e 73 79 6e 63 48 65 61 64 65 72 20  Ret->syncHeader 
19c9e 3d 20 31 3b 0a 20 20 70 52 65 74 2d 3e 70 61 64  = 1;.  pRet->pad
19c9f 54 6f 53 65 63 74 6f 72 42 6f 75 6e 64 61 72 79  ToSectorBoundary
19ca0 20 3d 20 31 3b 0a 20 20 70 52 65 74 2d 3e 65 78   = 1;.  pRet->ex
19ca1 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 62  clusiveMode = (b
19ca2 4e 6f 53 68 6d 20 3f 20 57 41 4c 5f 48 45 41 50  NoShm ? WAL_HEAP
19ca3 4d 45 4d 4f 52 59 5f 4d 4f 44 45 3a 20 57 41 4c  MEMORY_MODE: WAL
19ca4 5f 4e 4f 52 4d 41 4c 5f 4d 4f 44 45 29 3b 0a 0a  _NORMAL_MODE);..
19ca5 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 68    /* Open file h
19ca6 61 6e 64 6c 65 20 6f 6e 20 74 68 65 20 77 72 69  andle on the wri
19ca7 74 65 2d 61 68 65 61 64 20 6c 6f 67 20 66 69 6c  te-ahead log fil
19ca8 65 2e 20 2a 2f 0a 20 20 66 6c 61 67 73 20 3d 20  e. */.  flags = 
19ca9 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  (SQLITE_OPEN_REA
19caa 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50  DWRITE|SQLITE_OP
19cab 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c 49 54 45  EN_CREATE|SQLITE
19cac 5f 4f 50 45 4e 5f 57 41 4c 29 3b 0a 20 20 72 63  _OPEN_WAL);.  rc
19cad 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e   = sqlite3OsOpen
19cae 28 70 56 66 73 2c 20 7a 57 61 6c 4e 61 6d 65 2c  (pVfs, zWalName,
19caf 20 70 52 65 74 2d 3e 70 57 61 6c 46 64 2c 20 66   pRet->pWalFd, f
19cb0 6c 61 67 73 2c 20 26 66 6c 61 67 73 29 3b 0a 20  lags, &flags);. 
19cb1 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
19cb2 4f 4b 20 26 26 20 66 6c 61 67 73 26 53 51 4c 49  OK && flags&SQLI
19cb3 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
19cb4 20 29 7b 0a 20 20 20 20 70 52 65 74 2d 3e 72 65   ){.    pRet->re
19cb5 61 64 4f 6e 6c 79 20 3d 20 57 41 4c 5f 52 44 4f  adOnly = WAL_RDO
19cb6 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  NLY;.  }..  if( 
19cb7 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
19cb8 0a 20 20 20 20 77 61 6c 49 6e 64 65 78 43 6c 6f  .    walIndexClo
19cb9 73 65 28 70 52 65 74 2c 20 30 29 3b 0a 20 20 20  se(pRet, 0);.   
19cba 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
19cbb 70 52 65 74 2d 3e 70 57 61 6c 46 64 29 3b 0a 20  pRet->pWalFd);. 
19cbc 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
19cbd 70 52 65 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  pRet);.  }else{.
19cbe 20 20 20 20 69 6e 74 20 69 44 43 20 3d 20 73 71      int iDC = sq
19cbf 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61  lite3OsDeviceCha
19cc0 72 61 63 74 65 72 69 73 74 69 63 73 28 70 52 65  racteristics(pRe
19cc1 74 2d 3e 70 57 61 6c 46 64 29 3b 0a 20 20 20 20  t->pWalFd);.    
19cc2 69 66 28 20 69 44 43 20 26 20 53 51 4c 49 54 45  if( iDC & SQLITE
19cc3 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41  _IOCAP_SEQUENTIA
19cc4 4c 20 29 7b 20 70 52 65 74 2d 3e 73 79 6e 63 48  L ){ pRet->syncH
19cc5 65 61 64 65 72 20 3d 20 30 3b 20 7d 0a 20 20 20  eader = 0; }.   
19cc6 20 69 66 28 20 69 44 43 20 26 20 53 51 4c 49 54   if( iDC & SQLIT
19cc7 45 5f 49 4f 43 41 50 5f 50 4f 57 45 52 53 41 46  E_IOCAP_POWERSAF
19cc8 45 5f 4f 56 45 52 57 52 49 54 45 20 29 7b 0a 20  E_OVERWRITE ){. 
19cc9 20 20 20 20 20 70 52 65 74 2d 3e 70 61 64 54 6f       pRet->padTo
19cca 53 65 63 74 6f 72 42 6f 75 6e 64 61 72 79 20 3d  SectorBoundary =
19ccb 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70   0;.    }.    *p
19ccc 70 57 61 6c 20 3d 20 70 52 65 74 3b 0a 20 20 20  pWal = pRet;.   
19ccd 20 57 41 4c 54 52 41 43 45 28 28 22 57 41 4c 25   WALTRACE(("WAL%
19cce 64 3a 20 6f 70 65 6e 65 64 5c 6e 22 2c 20 70 52  d: opened\n", pR
19ccf 65 74 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  et));.  }.  retu
19cd0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
19cd1 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20  Change the size 
19cd2 74 6f 20 77 68 69 63 68 20 74 68 65 20 57 41 4c  to which the WAL
19cd3 20 66 69 6c 65 20 69 73 20 74 72 75 63 61 74 65   file is trucate
19cd4 64 20 6f 6e 20 65 61 63 68 20 72 65 73 65 74 2e  d on each reset.
19cd5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19cd6 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 57  TE void sqlite3W
19cd7 61 6c 4c 69 6d 69 74 28 57 61 6c 20 2a 70 57 61  alLimit(Wal *pWa
19cd8 6c 2c 20 69 36 34 20 69 4c 69 6d 69 74 29 7b 0a  l, i64 iLimit){.
19cd9 20 20 69 66 28 20 70 57 61 6c 20 29 20 70 57 61    if( pWal ) pWa
19cda 6c 2d 3e 6d 78 57 61 6c 53 69 7a 65 20 3d 20 69  l->mxWalSize = i
19cdb 4c 69 6d 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Limit;.}../*.** 
19cdc 46 69 6e 64 20 74 68 65 20 73 6d 61 6c 6c 65 73  Find the smalles
19cdd 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 75  t page number ou
19cde 74 20 6f 66 20 61 6c 6c 20 70 61 67 65 73 20 68  t of all pages h
19cdf 65 6c 64 20 69 6e 20 74 68 65 20 57 41 4c 20 74  eld in the WAL t
19ce0 68 61 74 0a 2a 2a 20 68 61 73 20 6e 6f 74 20 62  hat.** has not b
19ce1 65 65 6e 20 72 65 74 75 72 6e 65 64 20 62 79 20  een returned by 
19ce2 61 6e 79 20 70 72 69 6f 72 20 69 6e 76 6f 63 61  any prior invoca
19ce3 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6d 65 74  tion of this met
19ce4 68 6f 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 61  hod on the.** sa
19ce5 6d 65 20 57 61 6c 49 74 65 72 61 74 6f 72 20 6f  me WalIterator o
19ce6 62 6a 65 63 74 2e 20 20 20 57 72 69 74 65 20 69  bject.   Write i
19ce7 6e 74 6f 20 2a 70 69 46 72 61 6d 65 20 74 68 65  nto *piFrame the
19ce8 20 66 72 61 6d 65 20 69 6e 64 65 78 20 77 68 65   frame index whe
19ce9 72 65 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 20  re.** that page 
19cea 77 61 73 20 6c 61 73 74 20 77 72 69 74 74 65 6e  was last written
19ceb 20 69 6e 74 6f 20 74 68 65 20 57 41 4c 2e 20 20   into the WAL.  
19cec 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 50 61  Write into *piPa
19ced 67 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e  ge the page.** n
19cee 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  umber..**.** Ret
19cef 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73  urn 0 on success
19cf0 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20  .  If there are 
19cf1 6e 6f 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  no pages in the 
19cf2 57 41 4c 20 77 69 74 68 20 61 20 70 61 67 65 0a  WAL with a page.
19cf3 2a 2a 20 6e 75 6d 62 65 72 20 6c 61 72 67 65 72  ** number larger
19cf4 20 74 68 61 6e 20 2a 70 69 50 61 67 65 2c 20 74   than *piPage, t
19cf5 68 65 6e 20 72 65 74 75 72 6e 20 31 2e 0a 2a 2f  hen return 1..*/
19cf6 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61 6c 49  .static int walI
19cf7 74 65 72 61 74 6f 72 4e 65 78 74 28 0a 20 20 57  teratorNext(.  W
19cf8 61 6c 49 74 65 72 61 74 6f 72 20 2a 70 2c 20 20  alIterator *p,  
19cf9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19cfa 49 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 33  Iterator */.  u3
19cfb 32 20 2a 70 69 50 61 67 65 2c 20 20 20 20 20 20  2 *piPage,      
19cfc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
19cfd 55 54 3a 20 54 68 65 20 70 61 67 65 20 6e 75 6d  UT: The page num
19cfe 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20  ber of the next 
19cff 70 61 67 65 20 2a 2f 0a 20 20 75 33 32 20 2a 70  page */.  u32 *p
19d00 69 46 72 61 6d 65 20 20 20 20 20 20 20 20 20 20  iFrame          
19d01 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
19d02 57 61 6c 20 66 72 61 6d 65 20 69 6e 64 65 78 20  Wal frame index 
19d03 6f 66 20 6e 65 78 74 20 70 61 67 65 20 2a 2f 0a  of next page */.
19d04 29 7b 0a 20 20 75 33 32 20 69 4d 69 6e 3b 20 20  ){.  u32 iMin;  
19d05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19d06 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 70 67 6e     /* Result pgn
19d07 6f 20 6d 75 73 74 20 62 65 20 67 72 65 61 74 65  o must be greate
19d08 72 20 74 68 61 6e 20 69 4d 69 6e 20 2a 2f 0a 20  r than iMin */. 
19d09 20 75 33 32 20 69 52 65 74 20 3d 20 30 78 46 46   u32 iRet = 0xFF
19d0a 46 46 46 46 46 46 3b 20 20 20 20 20 20 20 20 2f  FFFFFF;        /
19d0b 2a 20 30 78 66 66 66 66 66 66 66 66 20 69 73 20  * 0xffffffff is 
19d0c 6e 65 76 65 72 20 61 20 76 61 6c 69 64 20 70 61  never a valid pa
19d0d 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69  ge number */.  i
19d0e 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
19d0f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19d10 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f  For looping thro
19d11 75 67 68 20 73 65 67 6d 65 6e 74 73 20 2a 2f 0a  ugh segments */.
19d12 0a 20 20 69 4d 69 6e 20 3d 20 70 2d 3e 69 50 72  .  iMin = p->iPr
19d13 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ior;.  assert( i
19d14 4d 69 6e 3c 30 78 66 66 66 66 66 66 66 66 20 29  Min<0xffffffff )
19d15 3b 0a 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 53 65  ;.  for(i=p->nSe
19d16 67 6d 65 6e 74 2d 31 3b 20 69 3e 3d 30 3b 20 69  gment-1; i>=0; i
19d17 2d 2d 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  --){.    struct 
19d18 57 61 6c 53 65 67 6d 65 6e 74 20 2a 70 53 65 67  WalSegment *pSeg
19d19 6d 65 6e 74 20 3d 20 26 70 2d 3e 61 53 65 67 6d  ment = &p->aSegm
19d1a 65 6e 74 5b 69 5d 3b 0a 20 20 20 20 77 68 69 6c  ent[i];.    whil
19d1b 65 28 20 70 53 65 67 6d 65 6e 74 2d 3e 69 4e 65  e( pSegment->iNe
19d1c 78 74 3c 70 53 65 67 6d 65 6e 74 2d 3e 6e 45 6e  xt<pSegment->nEn
19d1d 74 72 79 20 29 7b 0a 20 20 20 20 20 20 75 33 32  try ){.      u32
19d1e 20 69 50 67 20 3d 20 70 53 65 67 6d 65 6e 74 2d   iPg = pSegment-
19d1f 3e 61 50 67 6e 6f 5b 70 53 65 67 6d 65 6e 74 2d  >aPgno[pSegment-
19d20 3e 61 49 6e 64 65 78 5b 70 53 65 67 6d 65 6e 74  >aIndex[pSegment
19d21 2d 3e 69 4e 65 78 74 5d 5d 3b 0a 20 20 20 20 20  ->iNext]];.     
19d22 20 69 66 28 20 69 50 67 3e 69 4d 69 6e 20 29 7b   if( iPg>iMin ){
19d23 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 50 67  .        if( iPg
19d24 3c 69 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20  <iRet ){.       
19d25 20 20 20 69 52 65 74 20 3d 20 69 50 67 3b 0a 20     iRet = iPg;. 
19d26 20 20 20 20 20 20 20 20 20 2a 70 69 46 72 61 6d           *piFram
19d27 65 20 3d 20 70 53 65 67 6d 65 6e 74 2d 3e 69 5a  e = pSegment->iZ
19d28 65 72 6f 20 2b 20 70 53 65 67 6d 65 6e 74 2d 3e  ero + pSegment->
19d29 61 49 6e 64 65 78 5b 70 53 65 67 6d 65 6e 74 2d  aIndex[pSegment-
19d2a 3e 69 4e 65 78 74 5d 3b 0a 20 20 20 20 20 20 20  >iNext];.       
19d2b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
19d2c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
19d2d 70 53 65 67 6d 65 6e 74 2d 3e 69 4e 65 78 74 2b  pSegment->iNext+
19d2e 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  +;.    }.  }..  
19d2f 2a 70 69 50 61 67 65 20 3d 20 70 2d 3e 69 50 72  *piPage = p->iPr
19d30 69 6f 72 20 3d 20 69 52 65 74 3b 0a 20 20 72 65  ior = iRet;.  re
19d31 74 75 72 6e 20 28 69 52 65 74 3d 3d 30 78 46 46  turn (iRet==0xFF
19d32 46 46 46 46 46 46 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  FFFFFF);.}../*.*
19d33 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
19d34 6d 65 72 67 65 73 20 74 77 6f 20 73 6f 72 74 65  merges two sorte
19d35 64 20 6c 69 73 74 73 20 69 6e 74 6f 20 61 20 73  d lists into a s
19d36 69 6e 67 6c 65 20 73 6f 72 74 65 64 20 6c 69 73  ingle sorted lis
19d37 74 2e 0a 2a 2a 0a 2a 2a 20 61 4c 65 66 74 5b 5d  t..**.** aLeft[]
19d38 20 61 6e 64 20 61 52 69 67 68 74 5b 5d 20 61 72   and aRight[] ar
19d39 65 20 61 72 72 61 79 73 20 6f 66 20 69 6e 64 69  e arrays of indi
19d3a 63 65 73 2e 20 20 54 68 65 20 73 6f 72 74 20 6b  ces.  The sort k
19d3b 65 79 20 69 73 0a 2a 2a 20 61 43 6f 6e 74 65 6e  ey is.** aConten
19d3c 74 5b 61 4c 65 66 74 5b 5d 5d 20 61 6e 64 20 61  t[aLeft[]] and a
19d3d 43 6f 6e 74 65 6e 74 5b 61 52 69 67 68 74 5b 5d  Content[aRight[]
19d3e 5d 2e 20 20 55 70 6f 6e 20 65 6e 74 72 79 2c 20  ].  Upon entry, 
19d3f 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  the following.**
19d40 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 66   is guaranteed f
19d41 6f 72 20 61 6c 6c 20 4a 3c 4b 3a 0a 2a 2a 0a 2a  or all J<K:.**.*
19d42 2a 20 20 20 20 20 20 20 20 61 43 6f 6e 74 65 6e  *        aConten
19d43 74 5b 61 4c 65 66 74 5b 4a 5d 5d 20 3c 20 61 43  t[aLeft[J]] < aC
19d44 6f 6e 74 65 6e 74 5b 61 4c 65 66 74 5b 4b 5d 5d  ontent[aLeft[K]]
19d45 0a 2a 2a 20 20 20 20 20 20 20 20 61 43 6f 6e 74  .**        aCont
19d46 65 6e 74 5b 61 52 69 67 68 74 5b 4a 5d 5d 20 3c  ent[aRight[J]] <
19d47 20 61 43 6f 6e 74 65 6e 74 5b 61 52 69 67 68 74   aContent[aRight
19d48 5b 4b 5d 5d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  [K]].**.** This 
19d49 72 6f 75 74 69 6e 65 20 6f 76 65 72 77 72 69 74  routine overwrit
19d4a 65 73 20 61 52 69 67 68 74 5b 5d 20 77 69 74 68  es aRight[] with
19d4b 20 61 20 6e 65 77 20 28 70 72 6f 62 61 62 6c 79   a new (probably
19d4c 20 6c 6f 6e 67 65 72 29 20 73 65 71 75 65 6e 63   longer) sequenc
19d4d 65 0a 2a 2a 20 6f 66 20 69 6e 64 69 63 65 73 20  e.** of indices 
19d4e 73 75 63 68 20 74 68 61 74 20 74 68 65 20 61 52  such that the aR
19d4f 69 67 68 74 5b 5d 20 63 6f 6e 74 61 69 6e 73 20  ight[] contains 
19d50 65 76 65 72 79 20 69 6e 64 65 78 20 74 68 61 74  every index that
19d51 20 61 70 70 65 61 72 73 20 69 6e 0a 2a 2a 20 65   appears in.** e
19d52 69 74 68 65 72 20 61 4c 65 66 74 5b 5d 20 6f 72  ither aLeft[] or
19d53 20 74 68 65 20 6f 6c 64 20 61 52 69 67 68 74 5b   the old aRight[
19d54 5d 20 61 6e 64 20 73 75 63 68 20 74 68 61 74 20  ] and such that 
19d55 74 68 65 20 73 65 63 6f 6e 64 20 63 6f 6e 64 69  the second condi
19d56 74 69 6f 6e 0a 2a 2a 20 61 62 6f 76 65 20 69 73  tion.** above is
19d57 20 73 74 69 6c 6c 20 6d 65 74 2e 0a 2a 2a 0a 2a   still met..**.*
19d58 2a 20 54 68 65 20 61 43 6f 6e 74 65 6e 74 5b 61  * The aContent[a
19d59 4c 65 66 74 5b 58 5d 5d 20 76 61 6c 75 65 73 20  Left[X]] values 
19d5a 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 20 66  will be unique f
19d5b 6f 72 20 61 6c 6c 20 58 2e 20 20 41 6e 64 20 74  or all X.  And t
19d5c 68 65 0a 2a 2a 20 61 43 6f 6e 74 65 6e 74 5b 61  he.** aContent[a
19d5d 52 69 67 68 74 5b 58 5d 5d 20 76 61 6c 75 65 73  Right[X]] values
19d5e 20 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 20   will be unique 
19d5f 74 6f 6f 2e 20 20 42 75 74 20 74 68 65 72 65 20  too.  But there 
19d60 6d 69 67 68 74 20 62 65 0a 2a 2a 20 6f 6e 65 20  might be.** one 
19d61 6f 72 20 6d 6f 72 65 20 63 6f 6d 62 69 6e 61 74  or more combinat
19d62 69 6f 6e 73 20 6f 66 20 58 20 61 6e 64 20 59 20  ions of X and Y 
19d63 73 75 63 68 20 74 68 61 74 0a 2a 2a 0a 2a 2a 20  such that.**.** 
19d64 20 20 20 20 20 61 4c 65 66 74 5b 58 5d 21 3d 61       aLeft[X]!=a
19d65 52 69 67 68 74 5b 59 5d 20 20 26 26 20 20 61 43  Right[Y]  &&  aC
19d66 6f 6e 74 65 6e 74 5b 61 4c 65 66 74 5b 58 5d 5d  ontent[aLeft[X]]
19d67 20 3d 3d 20 61 43 6f 6e 74 65 6e 74 5b 61 52 69   == aContent[aRi
19d68 67 68 74 5b 59 5d 5d 0a 2a 2a 0a 2a 2a 20 57 68  ght[Y]].**.** Wh
19d69 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c  en that happens,
19d6a 20 6f 6d 69 74 20 74 68 65 20 61 4c 65 66 74 5b   omit the aLeft[
19d6b 58 5d 20 61 6e 64 20 75 73 65 20 74 68 65 20 61  X] and use the a
19d6c 52 69 67 68 74 5b 59 5d 20 69 6e 64 65 78 2e 0a  Right[Y] index..
19d6d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
19d6e 61 6c 4d 65 72 67 65 28 0a 20 20 63 6f 6e 73 74  alMerge(.  const
19d6f 20 75 33 32 20 2a 61 43 6f 6e 74 65 6e 74 2c 20   u32 *aContent, 
19d70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
19d71 67 65 73 20 69 6e 20 77 61 6c 20 2d 20 6b 65 79  ges in wal - key
19d72 73 20 66 6f 72 20 74 68 65 20 73 6f 72 74 20 2a  s for the sort *
19d73 2f 0a 20 20 68 74 5f 73 6c 6f 74 20 2a 61 4c 65  /.  ht_slot *aLe
19d74 66 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ft,             
19d75 20 20 20 20 2f 2a 20 49 4e 3a 20 4c 65 66 74 20      /* IN: Left 
19d76 68 61 6e 64 20 69 6e 70 75 74 20 6c 69 73 74 20  hand input list 
19d77 2a 2f 0a 20 20 69 6e 74 20 6e 4c 65 66 74 2c 20  */.  int nLeft, 
19d78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19d79 20 20 20 20 20 2f 2a 20 49 4e 3a 20 45 6c 65 6d       /* IN: Elem
19d7a 65 6e 74 73 20 69 6e 20 61 72 72 61 79 20 2a 70  ents in array *p
19d7b 61 4c 65 66 74 20 2a 2f 0a 20 20 68 74 5f 73 6c  aLeft */.  ht_sl
19d7c 6f 74 20 2a 2a 70 61 52 69 67 68 74 2c 20 20 20  ot **paRight,   
19d7d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e             /* IN
19d7e 2f 4f 55 54 3a 20 52 69 67 68 74 20 68 61 6e 64  /OUT: Right hand
19d7f 20 69 6e 70 75 74 20 6c 69 73 74 20 2a 2f 0a 20   input list */. 
19d80 20 69 6e 74 20 2a 70 6e 52 69 67 68 74 2c 20 20   int *pnRight,  
19d81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19d82 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 45 6c 65 6d   /* IN/OUT: Elem
19d83 65 6e 74 73 20 69 6e 20 2a 70 61 52 69 67 68 74  ents in *paRight
19d84 20 2a 2f 0a 20 20 68 74 5f 73 6c 6f 74 20 2a 61   */.  ht_slot *a
19d85 54 6d 70 20 20 20 20 20 20 20 20 20 20 20 20 20  Tmp             
19d86 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61        /* Tempora
19d87 72 79 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a  ry buffer */.){.
19d88 20 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 30 3b    int iLeft = 0;
19d89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19d8a 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 64    /* Current ind
19d8b 65 78 20 69 6e 20 61 4c 65 66 74 20 2a 2f 0a 20  ex in aLeft */. 
19d8c 20 69 6e 74 20 69 52 69 67 68 74 20 3d 20 30 3b   int iRight = 0;
19d8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19d8e 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 64 65   /* Current inde
19d8f 78 20 69 6e 20 61 52 69 67 68 74 20 2a 2f 0a 20  x in aRight */. 
19d90 20 69 6e 74 20 69 4f 75 74 20 3d 20 30 3b 20 20   int iOut = 0;  
19d91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19d92 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 64 65   /* Current inde
19d93 78 20 69 6e 20 6f 75 74 70 75 74 20 62 75 66 66  x in output buff
19d94 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 69 67  er */.  int nRig
19d95 68 74 20 3d 20 2a 70 6e 52 69 67 68 74 3b 0a 20  ht = *pnRight;. 
19d96 20 68 74 5f 73 6c 6f 74 20 2a 61 52 69 67 68 74   ht_slot *aRight
19d97 20 3d 20 2a 70 61 52 69 67 68 74 3b 0a 0a 20 20   = *paRight;..  
19d98 61 73 73 65 72 74 28 20 6e 4c 65 66 74 3e 30 20  assert( nLeft>0 
19d99 26 26 20 6e 52 69 67 68 74 3e 30 20 29 3b 0a 20  && nRight>0 );. 
19d9a 20 77 68 69 6c 65 28 20 69 52 69 67 68 74 3c 6e   while( iRight<n
19d9b 52 69 67 68 74 20 7c 7c 20 69 4c 65 66 74 3c 6e  Right || iLeft<n
19d9c 4c 65 66 74 20 29 7b 0a 20 20 20 20 68 74 5f 73  Left ){.    ht_s
19d9d 6c 6f 74 20 6c 6f 67 70 61 67 65 3b 0a 20 20 20  lot logpage;.   
19d9e 20 50 67 6e 6f 20 64 62 70 61 67 65 3b 0a 0a 20   Pgno dbpage;.. 
19d9f 20 20 20 69 66 28 20 28 69 4c 65 66 74 3c 6e 4c     if( (iLeft<nL
19da0 65 66 74 29 20 0a 20 20 20 20 20 26 26 20 28 69  eft) .     && (i
19da1 52 69 67 68 74 3e 3d 6e 52 69 67 68 74 20 7c 7c  Right>=nRight ||
19da2 20 61 43 6f 6e 74 65 6e 74 5b 61 4c 65 66 74 5b   aContent[aLeft[
19da3 69 4c 65 66 74 5d 5d 3c 61 43 6f 6e 74 65 6e 74  iLeft]]<aContent
19da4 5b 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d 5d  [aRight[iRight]]
19da5 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6c  ).    ){.      l
19da6 6f 67 70 61 67 65 20 3d 20 61 4c 65 66 74 5b 69  ogpage = aLeft[i
19da7 4c 65 66 74 2b 2b 5d 3b 0a 20 20 20 20 7d 65 6c  Left++];.    }el
19da8 73 65 7b 0a 20 20 20 20 20 20 6c 6f 67 70 61 67  se{.      logpag
19da9 65 20 3d 20 61 52 69 67 68 74 5b 69 52 69 67 68  e = aRight[iRigh
19daa 74 2b 2b 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  t++];.    }.    
19dab 64 62 70 61 67 65 20 3d 20 61 43 6f 6e 74 65 6e  dbpage = aConten
19dac 74 5b 6c 6f 67 70 61 67 65 5d 3b 0a 0a 20 20 20  t[logpage];..   
19dad 20 61 54 6d 70 5b 69 4f 75 74 2b 2b 5d 20 3d 20   aTmp[iOut++] = 
19dae 6c 6f 67 70 61 67 65 3b 0a 20 20 20 20 69 66 28  logpage;.    if(
19daf 20 69 4c 65 66 74 3c 6e 4c 65 66 74 20 26 26 20   iLeft<nLeft && 
19db0 61 43 6f 6e 74 65 6e 74 5b 61 4c 65 66 74 5b 69  aContent[aLeft[i
19db1 4c 65 66 74 5d 5d 3d 3d 64 62 70 61 67 65 20 29  Left]]==dbpage )
19db2 20 69 4c 65 66 74 2b 2b 3b 0a 0a 20 20 20 20 61   iLeft++;..    a
19db3 73 73 65 72 74 28 20 69 4c 65 66 74 3e 3d 6e 4c  ssert( iLeft>=nL
19db4 65 66 74 20 7c 7c 20 61 43 6f 6e 74 65 6e 74 5b  eft || aContent[
19db5 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d 3e 64 62  aLeft[iLeft]]>db
19db6 70 61 67 65 20 29 3b 0a 20 20 20 20 61 73 73 65  page );.    asse
19db7 72 74 28 20 69 52 69 67 68 74 3e 3d 6e 52 69 67  rt( iRight>=nRig
19db8 68 74 20 7c 7c 20 61 43 6f 6e 74 65 6e 74 5b 61  ht || aContent[a
19db9 52 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 3e 64  Right[iRight]]>d
19dba 62 70 61 67 65 20 29 3b 0a 20 20 7d 0a 0a 20 20  bpage );.  }..  
19dbb 2a 70 61 52 69 67 68 74 20 3d 20 61 4c 65 66 74  *paRight = aLeft
19dbc 3b 0a 20 20 2a 70 6e 52 69 67 68 74 20 3d 20 69  ;.  *pnRight = i
19dbd 4f 75 74 3b 0a 20 20 6d 65 6d 63 70 79 28 61 4c  Out;.  memcpy(aL
19dbe 65 66 74 2c 20 61 54 6d 70 2c 20 73 69 7a 65 6f  eft, aTmp, sizeo
19dbf 66 28 61 54 6d 70 5b 30 5d 29 2a 69 4f 75 74 29  f(aTmp[0])*iOut)
19dc0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 20  ;.}../*.** Sort 
19dc1 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20  the elements in 
19dc2 6c 69 73 74 20 61 4c 69 73 74 20 75 73 69 6e 67  list aList using
19dc3 20 61 43 6f 6e 74 65 6e 74 5b 5d 20 61 73 20 74   aContent[] as t
19dc4 68 65 20 73 6f 72 74 20 6b 65 79 2e 0a 2a 2a 20  he sort key..** 
19dc5 52 65 6d 6f 76 65 20 65 6c 65 6d 65 6e 74 73 20  Remove elements 
19dc6 77 69 74 68 20 64 75 70 6c 69 63 61 74 65 20 6b  with duplicate k
19dc7 65 79 73 2c 20 70 72 65 66 65 72 72 69 6e 67 20  eys, preferring 
19dc8 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 6c  to keep the.** l
19dc9 61 72 67 65 72 20 61 4c 69 73 74 5b 5d 20 76 61  arger aList[] va
19dca 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  lues..**.** The 
19dcb 61 4c 69 73 74 5b 5d 20 65 6e 74 72 69 65 73 20  aList[] entries 
19dcc 61 72 65 20 69 6e 64 69 63 65 73 20 69 6e 74 6f  are indices into
19dcd 20 61 43 6f 6e 74 65 6e 74 5b 5d 2e 20 20 54 68   aContent[].  Th
19dce 65 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 61  e values in.** a
19dcf 4c 69 73 74 5b 5d 20 61 72 65 20 74 6f 20 62 65  List[] are to be
19dd0 20 73 6f 72 74 65 64 20 73 6f 20 74 68 61 74 20   sorted so that 
19dd1 66 6f 72 20 61 6c 6c 20 4a 3c 4b 3a 0a 2a 2a 0a  for all J<K:.**.
19dd2 2a 2a 20 20 20 20 20 20 61 43 6f 6e 74 65 6e 74  **      aContent
19dd3 5b 61 4c 69 73 74 5b 4a 5d 5d 20 3c 20 61 43 6f  [aList[J]] < aCo
19dd4 6e 74 65 6e 74 5b 61 4c 69 73 74 5b 4b 5d 5d 0a  ntent[aList[K]].
19dd5 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e 79 20 58 20  **.** For any X 
19dd6 61 6e 64 20 59 20 73 75 63 68 20 74 68 61 74 0a  and Y such that.
19dd7 2a 2a 0a 2a 2a 20 20 20 20 20 20 61 43 6f 6e 74  **.**      aCont
19dd8 65 6e 74 5b 61 4c 69 73 74 5b 58 5d 5d 20 3d 3d  ent[aList[X]] ==
19dd9 20 61 43 6f 6e 74 65 6e 74 5b 61 4c 69 73 74 5b   aContent[aList[
19dda 59 5d 5d 0a 2a 2a 0a 2a 2a 20 4b 65 65 70 20 74  Y]].**.** Keep t
19ddb 68 65 20 6c 61 72 67 65 72 20 6f 66 20 74 68 65  he larger of the
19ddc 20 74 77 6f 20 76 61 6c 75 65 73 20 61 4c 69 73   two values aLis
19ddd 74 5b 58 5d 20 61 6e 64 20 61 4c 69 73 74 5b 59  t[X] and aList[Y
19dde 5d 20 61 6e 64 20 64 69 73 63 61 72 64 0a 2a 2a  ] and discard.**
19ddf 20 74 68 65 20 73 6d 61 6c 6c 65 72 2e 0a 2a 2f   the smaller..*/
19de0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 61 6c  .static void wal
19de1 4d 65 72 67 65 73 6f 72 74 28 0a 20 20 63 6f 6e  Mergesort(.  con
19de2 73 74 20 75 33 32 20 2a 61 43 6f 6e 74 65 6e 74  st u32 *aContent
19de3 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
19de4 50 61 67 65 73 20 69 6e 20 77 61 6c 20 2a 2f 0a  Pages in wal */.
19de5 20 20 68 74 5f 73 6c 6f 74 20 2a 61 42 75 66 66    ht_slot *aBuff
19de6 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  er,             
19de7 20 20 2f 2a 20 42 75 66 66 65 72 20 6f 66 20 61    /* Buffer of a
19de8 74 20 6c 65 61 73 74 20 2a 70 6e 4c 69 73 74 20  t least *pnList 
19de9 69 74 65 6d 73 20 74 6f 20 75 73 65 20 2a 2f 0a  items to use */.
19dea 20 20 68 74 5f 73 6c 6f 74 20 2a 61 4c 69 73 74    ht_slot *aList
19deb 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
19dec 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 4c 69 73    /* IN/OUT: Lis
19ded 74 20 74 6f 20 73 6f 72 74 20 2a 2f 0a 20 20 69  t to sort */.  i
19dee 6e 74 20 2a 70 6e 4c 69 73 74 20 20 20 20 20 20  nt *pnList      
19def 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19df0 2a 20 49 4e 2f 4f 55 54 3a 20 4e 75 6d 62 65 72  * IN/OUT: Number
19df1 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20   of elements in 
19df2 61 4c 69 73 74 5b 5d 20 2a 2f 0a 29 7b 0a 20 20  aList[] */.){.  
19df3 73 74 72 75 63 74 20 53 75 62 6c 69 73 74 20 7b  struct Sublist {
19df4 0a 20 20 20 20 69 6e 74 20 6e 4c 69 73 74 3b 20  .    int nList; 
19df5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19df6 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
19df7 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 4c 69 73  elements in aLis
19df8 74 20 2a 2f 0a 20 20 20 20 68 74 5f 73 6c 6f 74  t */.    ht_slot
19df9 20 2a 61 4c 69 73 74 3b 20 20 20 20 20 20 20 20   *aList;        
19dfa 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
19dfb 72 20 74 6f 20 73 75 62 2d 6c 69 73 74 20 63 6f  r to sub-list co
19dfc 6e 74 65 6e 74 20 2a 2f 0a 20 20 7d 3b 0a 0a 20  ntent */.  };.. 
19dfd 20 63 6f 6e 73 74 20 69 6e 74 20 6e 4c 69 73 74   const int nList
19dfe 20 3d 20 2a 70 6e 4c 69 73 74 3b 20 20 20 20 20   = *pnList;     
19dff 20 2f 2a 20 53 69 7a 65 20 6f 66 20 69 6e 70 75   /* Size of inpu
19e00 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  t list */.  int 
19e01 6e 4d 65 72 67 65 20 3d 20 30 3b 20 20 20 20 20  nMerge = 0;     
19e02 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
19e03 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74  umber of element
19e04 73 20 69 6e 20 6c 69 73 74 20 61 4d 65 72 67 65  s in list aMerge
19e05 20 2a 2f 0a 20 20 68 74 5f 73 6c 6f 74 20 2a 61   */.  ht_slot *a
19e06 4d 65 72 67 65 20 3d 20 30 3b 20 20 20 20 20 20  Merge = 0;      
19e07 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f        /* List to
19e08 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 20 20   be merged */.  
19e09 69 6e 74 20 69 4c 69 73 74 3b 20 20 20 20 20 20  int iList;      
19e0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e0b 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 69 6e  /* Index into in
19e0c 70 75 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  put list */.  in
19e0d 74 20 69 53 75 62 20 3d 20 30 3b 20 20 20 20 20  t iSub = 0;     
19e0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19e0f 20 49 6e 64 65 78 20 69 6e 74 6f 20 61 53 75 62   Index into aSub
19e10 20 61 72 72 61 79 20 2a 2f 0a 20 20 73 74 72 75   array */.  stru
19e11 63 74 20 53 75 62 6c 69 73 74 20 61 53 75 62 5b  ct Sublist aSub[
19e12 31 33 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 41  13];        /* A
19e13 72 72 61 79 20 6f 66 20 73 75 62 2d 6c 69 73 74  rray of sub-list
19e14 73 20 2a 2f 0a 0a 20 20 6d 65 6d 73 65 74 28 61  s */..  memset(a
19e15 53 75 62 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61  Sub, 0, sizeof(a
19e16 53 75 62 29 29 3b 0a 20 20 61 73 73 65 72 74 28  Sub));.  assert(
19e17 20 6e 4c 69 73 74 3c 3d 48 41 53 48 54 41 42 4c   nList<=HASHTABL
19e18 45 5f 4e 50 41 47 45 20 26 26 20 6e 4c 69 73 74  E_NPAGE && nList
19e19 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
19e1a 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47 45 3d  HASHTABLE_NPAGE=
19e1b 3d 28 31 3c 3c 28 41 72 72 61 79 53 69 7a 65 28  =(1<<(ArraySize(
19e1c 61 53 75 62 29 2d 31 29 29 20 29 3b 0a 0a 20 20  aSub)-1)) );..  
19e1d 66 6f 72 28 69 4c 69 73 74 3d 30 3b 20 69 4c 69  for(iList=0; iLi
19e1e 73 74 3c 6e 4c 69 73 74 3b 20 69 4c 69 73 74 2b  st<nList; iList+
19e1f 2b 29 7b 0a 20 20 20 20 6e 4d 65 72 67 65 20 3d  +){.    nMerge =
19e20 20 31 3b 0a 20 20 20 20 61 4d 65 72 67 65 20 3d   1;.    aMerge =
19e21 20 26 61 4c 69 73 74 5b 69 4c 69 73 74 5d 3b 0a   &aList[iList];.
19e22 20 20 20 20 66 6f 72 28 69 53 75 62 3d 30 3b 20      for(iSub=0; 
19e23 69 4c 69 73 74 20 26 20 28 31 3c 3c 69 53 75 62  iList & (1<<iSub
19e24 29 3b 20 69 53 75 62 2b 2b 29 7b 0a 20 20 20 20  ); iSub++){.    
19e25 20 20 73 74 72 75 63 74 20 53 75 62 6c 69 73 74    struct Sublist
19e26 20 2a 70 20 3d 20 26 61 53 75 62 5b 69 53 75 62   *p = &aSub[iSub
19e27 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ];.      assert(
19e28 20 70 2d 3e 61 4c 69 73 74 20 26 26 20 70 2d 3e   p->aList && p->
19e29 6e 4c 69 73 74 3c 3d 28 31 3c 3c 69 53 75 62 29  nList<=(1<<iSub)
19e2a 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
19e2b 28 20 70 2d 3e 61 4c 69 73 74 3d 3d 26 61 4c 69  ( p->aList==&aLi
19e2c 73 74 5b 69 4c 69 73 74 26 7e 28 28 32 3c 3c 69  st[iList&~((2<<i
19e2d 53 75 62 29 2d 31 29 5d 20 29 3b 0a 20 20 20 20  Sub)-1)] );.    
19e2e 20 20 77 61 6c 4d 65 72 67 65 28 61 43 6f 6e 74    walMerge(aCont
19e2f 65 6e 74 2c 20 70 2d 3e 61 4c 69 73 74 2c 20 70  ent, p->aList, p
19e30 2d 3e 6e 4c 69 73 74 2c 20 26 61 4d 65 72 67 65  ->nList, &aMerge
19e31 2c 20 26 6e 4d 65 72 67 65 2c 20 61 42 75 66 66  , &nMerge, aBuff
19e32 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  er);.    }.    a
19e33 53 75 62 5b 69 53 75 62 5d 2e 61 4c 69 73 74 20  Sub[iSub].aList 
19e34 3d 20 61 4d 65 72 67 65 3b 0a 20 20 20 20 61 53  = aMerge;.    aS
19e35 75 62 5b 69 53 75 62 5d 2e 6e 4c 69 73 74 20 3d  ub[iSub].nList =
19e36 20 6e 4d 65 72 67 65 3b 0a 20 20 7d 0a 0a 20 20   nMerge;.  }..  
19e37 66 6f 72 28 69 53 75 62 2b 2b 3b 20 69 53 75 62  for(iSub++; iSub
19e38 3c 41 72 72 61 79 53 69 7a 65 28 61 53 75 62 29  <ArraySize(aSub)
19e39 3b 20 69 53 75 62 2b 2b 29 7b 0a 20 20 20 20 69  ; iSub++){.    i
19e3a 66 28 20 6e 4c 69 73 74 20 26 20 28 31 3c 3c 69  f( nList & (1<<i
19e3b 53 75 62 29 20 29 7b 0a 20 20 20 20 20 20 73 74  Sub) ){.      st
19e3c 72 75 63 74 20 53 75 62 6c 69 73 74 20 2a 70 20  ruct Sublist *p 
19e3d 3d 20 26 61 53 75 62 5b 69 53 75 62 5d 3b 0a 20  = &aSub[iSub];. 
19e3e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
19e3f 6e 4c 69 73 74 3c 3d 28 31 3c 3c 69 53 75 62 29  nList<=(1<<iSub)
19e40 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
19e41 28 20 70 2d 3e 61 4c 69 73 74 3d 3d 26 61 4c 69  ( p->aList==&aLi
19e42 73 74 5b 6e 4c 69 73 74 26 7e 28 28 32 3c 3c 69  st[nList&~((2<<i
19e43 53 75 62 29 2d 31 29 5d 20 29 3b 0a 20 20 20 20  Sub)-1)] );.    
19e44 20 20 77 61 6c 4d 65 72 67 65 28 61 43 6f 6e 74    walMerge(aCont
19e45 65 6e 74 2c 20 70 2d 3e 61 4c 69 73 74 2c 20 70  ent, p->aList, p
19e46 2d 3e 6e 4c 69 73 74 2c 20 26 61 4d 65 72 67 65  ->nList, &aMerge
19e47 2c 20 26 6e 4d 65 72 67 65 2c 20 61 42 75 66 66  , &nMerge, aBuff
19e48 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  er);.    }.  }. 
19e49 20 61 73 73 65 72 74 28 20 61 4d 65 72 67 65 3d   assert( aMerge=
19e4a 3d 61 4c 69 73 74 20 29 3b 0a 20 20 2a 70 6e 4c  =aList );.  *pnL
19e4b 69 73 74 20 3d 20 6e 4d 65 72 67 65 3b 0a 0a 23  ist = nMerge;..#
19e4c 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
19e4d 55 47 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69  UG.  {.    int i
19e4e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69  ;.    for(i=1; i
19e4f 3c 2a 70 6e 4c 69 73 74 3b 20 69 2b 2b 29 7b 0a  <*pnList; i++){.
19e50 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61 43        assert( aC
19e51 6f 6e 74 65 6e 74 5b 61 4c 69 73 74 5b 69 5d 5d  ontent[aList[i]]
19e52 20 3e 20 61 43 6f 6e 74 65 6e 74 5b 61 4c 69 73   > aContent[aLis
19e53 74 5b 69 2d 31 5d 5d 20 29 3b 0a 20 20 20 20 7d  t[i-1]] );.    }
19e54 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
19e55 2a 20 0a 2a 2a 20 46 72 65 65 20 61 6e 20 69 74  * .** Free an it
19e56 65 72 61 74 6f 72 20 61 6c 6c 6f 63 61 74 65 64  erator allocated
19e57 20 62 79 20 77 61 6c 49 74 65 72 61 74 6f 72 49   by walIteratorI
19e58 6e 69 74 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  nit()..*/.static
19e59 20 76 6f 69 64 20 77 61 6c 49 74 65 72 61 74 6f   void walIterato
19e5a 72 46 72 65 65 28 57 61 6c 49 74 65 72 61 74 6f  rFree(WalIterato
19e5b 72 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  r *p){.  sqlite3
19e5c 53 63 72 61 74 63 68 46 72 65 65 28 70 29 3b 0a  ScratchFree(p);.
19e5d 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75  }../*.** Constru
19e5e 63 74 20 61 20 57 61 6c 49 6e 74 65 72 61 74 6f  ct a WalInterato
19e5f 72 20 6f 62 6a 65 63 74 20 74 68 61 74 20 63 61  r object that ca
19e60 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6c 6f 6f  n be used to loo
19e61 70 20 6f 76 65 72 20 61 6c 6c 20 0a 2a 2a 20 70  p over all .** p
19e62 61 67 65 73 20 69 6e 20 74 68 65 20 57 41 4c 20  ages in the WAL 
19e63 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64  in ascending ord
19e64 65 72 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  er. The caller m
19e65 75 73 74 20 68 6f 6c 64 20 74 68 65 20 63 68 65  ust hold the che
19e66 63 6b 70 6f 69 6e 74 0a 2a 2a 20 6c 6f 63 6b 2e  ckpoint.** lock.
19e67 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73  .**.** On succes
19e68 73 2c 20 6d 61 6b 65 20 2a 70 70 20 70 6f 69 6e  s, make *pp poin
19e69 74 20 74 6f 20 74 68 65 20 6e 65 77 6c 79 20 61  t to the newly a
19e6a 6c 6c 6f 63 61 74 65 64 20 57 61 6c 49 6e 74 65  llocated WalInte
19e6b 72 61 74 6f 72 20 6f 62 6a 65 63 74 0a 2a 2a 20  rator object.** 
19e6c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
19e6d 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74  . Otherwise, ret
19e6e 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  urn an error cod
19e6f 65 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74 69  e. If this routi
19e70 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 6e  ne.** returns an
19e71 20 65 72 72 6f 72 2c 20 74 68 65 20 76 61 6c 75   error, the valu
19e72 65 20 6f 66 20 2a 70 70 20 69 73 20 75 6e 64 65  e of *pp is unde
19e73 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  fined..**.** The
19e74 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65   calling routine
19e75 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 20 77   should invoke w
19e76 61 6c 49 74 65 72 61 74 6f 72 46 72 65 65 28 29  alIteratorFree()
19e77 20 74 6f 20 64 65 73 74 72 6f 79 20 74 68 65 0a   to destroy the.
19e78 2a 2a 20 57 61 6c 49 74 65 72 61 74 6f 72 20 6f  ** WalIterator o
19e79 62 6a 65 63 74 20 77 68 65 6e 20 69 74 20 68 61  bject when it ha
19e7a 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20  s finished with 
19e7b 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  it..*/.static in
19e7c 74 20 77 61 6c 49 74 65 72 61 74 6f 72 49 6e 69  t walIteratorIni
19e7d 74 28 57 61 6c 20 2a 70 57 61 6c 2c 20 57 61 6c  t(Wal *pWal, Wal
19e7e 49 74 65 72 61 74 6f 72 20 2a 2a 70 70 29 7b 0a  Iterator **pp){.
19e7f 20 20 57 61 6c 49 74 65 72 61 74 6f 72 20 2a 70    WalIterator *p
19e80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
19e81 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75    /* Return valu
19e82 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65 67 6d  e */.  int nSegm
19e83 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ent;            
19e84 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
19e85 20 6f 66 20 73 65 67 6d 65 6e 74 73 20 74 6f 20   of segments to 
19e86 6d 65 72 67 65 20 2a 2f 0a 20 20 75 33 32 20 69  merge */.  u32 i
19e87 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Last;           
19e88 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
19e89 73 74 20 66 72 61 6d 65 20 69 6e 20 6c 6f 67 20  st frame in log 
19e8a 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20  */.  int nByte; 
19e8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e8c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
19e8d 66 20 62 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63  f bytes to alloc
19e8e 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  ate */.  int i; 
19e8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e90 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
19e91 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
19e92 0a 20 20 68 74 5f 73 6c 6f 74 20 2a 61 54 6d 70  .  ht_slot *aTmp
19e93 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
19e94 20 20 20 2f 2a 20 54 65 6d 70 20 73 70 61 63 65     /* Temp space
19e95 20 75 73 65 64 20 62 79 20 6d 65 72 67 65 2d 73   used by merge-s
19e96 6f 72 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  ort */.  int rc 
19e97 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
19e98 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
19e99 72 6e 20 43 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a  rn Code */..  /*
19e9a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e   This routine on
19e9b 6c 79 20 72 75 6e 73 20 77 68 69 6c 65 20 68 6f  ly runs while ho
19e9c 6c 64 69 6e 67 20 74 68 65 20 63 68 65 63 6b 70  lding the checkp
19e9d 6f 69 6e 74 20 6c 6f 63 6b 2e 20 41 6e 64 0a 20  oint lock. And. 
19e9e 20 2a 2a 20 69 74 20 6f 6e 6c 79 20 72 75 6e 73   ** it only runs
19e9f 20 69 66 20 74 68 65 72 65 20 69 73 20 61 63 74   if there is act
19ea0 75 61 6c 6c 79 20 63 6f 6e 74 65 6e 74 20 69 6e  ually content in
19ea1 20 74 68 65 20 6c 6f 67 20 28 6d 78 46 72 61 6d   the log (mxFram
19ea2 65 3e 30 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  e>0)..  */.  ass
19ea3 65 72 74 28 20 70 57 61 6c 2d 3e 63 6b 70 74 4c  ert( pWal->ckptL
19ea4 6f 63 6b 20 26 26 20 70 57 61 6c 2d 3e 68 64 72  ock && pWal->hdr
19ea5 2e 6d 78 46 72 61 6d 65 3e 30 20 29 3b 0a 20 20  .mxFrame>0 );.  
19ea6 69 4c 61 73 74 20 3d 20 70 57 61 6c 2d 3e 68 64  iLast = pWal->hd
19ea7 72 2e 6d 78 46 72 61 6d 65 3b 0a 0a 20 20 2f 2a  r.mxFrame;..  /*
19ea8 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20   Allocate space 
19ea9 66 6f 72 20 74 68 65 20 57 61 6c 49 74 65 72 61  for the WalItera
19eaa 74 6f 72 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20  tor object. */. 
19eab 20 6e 53 65 67 6d 65 6e 74 20 3d 20 77 61 6c 46   nSegment = walF
19eac 72 61 6d 65 50 61 67 65 28 69 4c 61 73 74 29 20  ramePage(iLast) 
19ead 2b 20 31 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73  + 1;.  nByte = s
19eae 69 7a 65 6f 66 28 57 61 6c 49 74 65 72 61 74 6f  izeof(WalIterato
19eaf 72 29 20 0a 20 20 20 20 20 20 20 20 2b 20 28 6e  r) .        + (n
19eb0 53 65 67 6d 65 6e 74 2d 31 29 2a 73 69 7a 65 6f  Segment-1)*sizeo
19eb1 66 28 73 74 72 75 63 74 20 57 61 6c 53 65 67 6d  f(struct WalSegm
19eb2 65 6e 74 29 0a 20 20 20 20 20 20 20 20 2b 20 69  ent).        + i
19eb3 4c 61 73 74 2a 73 69 7a 65 6f 66 28 68 74 5f 73  Last*sizeof(ht_s
19eb4 6c 6f 74 29 3b 0a 20 20 70 20 3d 20 28 57 61 6c  lot);.  p = (Wal
19eb5 49 74 65 72 61 74 6f 72 20 2a 29 73 71 6c 69 74  Iterator *)sqlit
19eb6 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28  e3ScratchMalloc(
19eb7 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 21 70  nByte);.  if( !p
19eb8 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
19eb9 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
19eba 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20  .  memset(p, 0, 
19ebb 6e 42 79 74 65 29 3b 0a 20 20 70 2d 3e 6e 53 65  nByte);.  p->nSe
19ebc 67 6d 65 6e 74 20 3d 20 6e 53 65 67 6d 65 6e 74  gment = nSegment
19ebd 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ;..  /* Allocate
19ebe 20 74 65 6d 70 6f 72 61 72 79 20 73 70 61 63 65   temporary space
19ebf 20 75 73 65 64 20 62 79 20 74 68 65 20 6d 65 72   used by the mer
19ec0 67 65 2d 73 6f 72 74 20 72 6f 75 74 69 6e 65 2e  ge-sort routine.
19ec1 20 54 68 69 73 20 62 6c 6f 63 6b 0a 20 20 2a 2a   This block.  **
19ec2 20 6f 66 20 6d 65 6d 6f 72 79 20 77 69 6c 6c 20   of memory will 
19ec3 62 65 20 66 72 65 65 64 20 62 65 66 6f 72 65 20  be freed before 
19ec4 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  this function re
19ec5 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 61 54  turns..  */.  aT
19ec6 6d 70 20 3d 20 28 68 74 5f 73 6c 6f 74 20 2a 29  mp = (ht_slot *)
19ec7 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61  sqlite3ScratchMa
19ec8 6c 6c 6f 63 28 0a 20 20 20 20 20 20 73 69 7a 65  lloc(.      size
19ec9 6f 66 28 68 74 5f 73 6c 6f 74 29 20 2a 20 28 69  of(ht_slot) * (i
19eca 4c 61 73 74 3e 48 41 53 48 54 41 42 4c 45 5f 4e  Last>HASHTABLE_N
19ecb 50 41 47 45 3f 48 41 53 48 54 41 42 4c 45 5f 4e  PAGE?HASHTABLE_N
19ecc 50 41 47 45 3a 69 4c 61 73 74 29 0a 20 20 29 3b  PAGE:iLast).  );
19ecd 0a 20 20 69 66 28 20 21 61 54 6d 70 20 29 7b 0a  .  if( !aTmp ){.
19ece 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
19ecf 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 66 6f  NOMEM;.  }..  fo
19ed0 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=0; rc==SQLIT
19ed1 45 5f 4f 4b 20 26 26 20 69 3c 6e 53 65 67 6d 65  E_OK && i<nSegme
19ed2 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 6f  nt; i++){.    vo
19ed3 6c 61 74 69 6c 65 20 68 74 5f 73 6c 6f 74 20 2a  latile ht_slot *
19ed4 61 48 61 73 68 3b 0a 20 20 20 20 75 33 32 20 69  aHash;.    u32 i
19ed5 5a 65 72 6f 3b 0a 20 20 20 20 76 6f 6c 61 74 69  Zero;.    volati
19ed6 6c 65 20 75 33 32 20 2a 61 50 67 6e 6f 3b 0a 0a  le u32 *aPgno;..
19ed7 20 20 20 20 72 63 20 3d 20 77 61 6c 48 61 73 68      rc = walHash
19ed8 47 65 74 28 70 57 61 6c 2c 20 69 2c 20 26 61 48  Get(pWal, i, &aH
19ed9 61 73 68 2c 20 26 61 50 67 6e 6f 2c 20 26 69 5a  ash, &aPgno, &iZ
19eda 65 72 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ero);.    if( rc
19edb 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
19edc 20 20 20 20 20 69 6e 74 20 6a 3b 20 20 20 20 20       int j;     
19edd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ede 20 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 72 69   /* Counter vari
19edf 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e  able */.      in
19ee0 74 20 6e 45 6e 74 72 79 3b 20 20 20 20 20 20 20  t nEntry;       
19ee1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
19ee2 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
19ee3 6e 20 74 68 69 73 20 73 65 67 6d 65 6e 74 20 2a  n this segment *
19ee4 2f 0a 20 20 20 20 20 20 68 74 5f 73 6c 6f 74 20  /.      ht_slot 
19ee5 2a 61 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20  *aIndex;        
19ee6 20 20 20 20 2f 2a 20 53 6f 72 74 65 64 20 69 6e      /* Sorted in
19ee7 64 65 78 20 66 6f 72 20 74 68 69 73 20 73 65 67  dex for this seg
19ee8 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20 20 20 61  ment */..      a
19ee9 50 67 6e 6f 2b 2b 3b 0a 20 20 20 20 20 20 69 66  Pgno++;.      if
19eea 28 20 28 69 2b 31 29 3d 3d 6e 53 65 67 6d 65 6e  ( (i+1)==nSegmen
19eeb 74 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 45 6e  t ){.        nEn
19eec 74 72 79 20 3d 20 28 69 6e 74 29 28 69 4c 61 73  try = (int)(iLas
19eed 74 20 2d 20 69 5a 65 72 6f 29 3b 0a 20 20 20 20  t - iZero);.    
19eee 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
19eef 20 6e 45 6e 74 72 79 20 3d 20 28 69 6e 74 29 28   nEntry = (int)(
19ef0 28 75 33 32 2a 29 61 48 61 73 68 20 2d 20 28 75  (u32*)aHash - (u
19ef1 33 32 2a 29 61 50 67 6e 6f 29 3b 0a 20 20 20 20  32*)aPgno);.    
19ef2 20 20 7d 0a 20 20 20 20 20 20 61 49 6e 64 65 78    }.      aIndex
19ef3 20 3d 20 26 28 28 68 74 5f 73 6c 6f 74 20 2a 29   = &((ht_slot *)
19ef4 26 70 2d 3e 61 53 65 67 6d 65 6e 74 5b 70 2d 3e  &p->aSegment[p->
19ef5 6e 53 65 67 6d 65 6e 74 5d 29 5b 69 5a 65 72 6f  nSegment])[iZero
19ef6 5d 3b 0a 20 20 20 20 20 20 69 5a 65 72 6f 2b 2b  ];.      iZero++
19ef7 3b 0a 20 20 0a 20 20 20 20 20 20 66 6f 72 28 6a  ;.  .      for(j
19ef8 3d 30 3b 20 6a 3c 6e 45 6e 74 72 79 3b 20 6a 2b  =0; j<nEntry; j+
19ef9 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 49 6e 64  +){.        aInd
19efa 65 78 5b 6a 5d 20 3d 20 28 68 74 5f 73 6c 6f 74  ex[j] = (ht_slot
19efb 29 6a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  )j;.      }.    
19efc 20 20 77 61 6c 4d 65 72 67 65 73 6f 72 74 28 28    walMergesort((
19efd 75 33 32 20 2a 29 61 50 67 6e 6f 2c 20 61 54 6d  u32 *)aPgno, aTm
19efe 70 2c 20 61 49 6e 64 65 78 2c 20 26 6e 45 6e 74  p, aIndex, &nEnt
19eff 72 79 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 53  ry);.      p->aS
19f00 65 67 6d 65 6e 74 5b 69 5d 2e 69 5a 65 72 6f 20  egment[i].iZero 
19f01 3d 20 69 5a 65 72 6f 3b 0a 20 20 20 20 20 20 70  = iZero;.      p
19f02 2d 3e 61 53 65 67 6d 65 6e 74 5b 69 5d 2e 6e 45  ->aSegment[i].nE
19f03 6e 74 72 79 20 3d 20 6e 45 6e 74 72 79 3b 0a 20  ntry = nEntry;. 
19f04 20 20 20 20 20 70 2d 3e 61 53 65 67 6d 65 6e 74       p->aSegment
19f05 5b 69 5d 2e 61 49 6e 64 65 78 20 3d 20 61 49 6e  [i].aIndex = aIn
19f06 64 65 78 3b 0a 20 20 20 20 20 20 70 2d 3e 61 53  dex;.      p->aS
19f07 65 67 6d 65 6e 74 5b 69 5d 2e 61 50 67 6e 6f 20  egment[i].aPgno 
19f08 3d 20 28 75 33 32 20 2a 29 61 50 67 6e 6f 3b 0a  = (u32 *)aPgno;.
19f09 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
19f0a 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28 61  te3ScratchFree(a
19f0b 54 6d 70 29 3b 0a 0a 20 20 69 66 28 20 72 63 21  Tmp);..  if( rc!
19f0c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
19f0d 20 20 77 61 6c 49 74 65 72 61 74 6f 72 46 72 65    walIteratorFre
19f0e 65 28 70 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 20  e(p);.  }.  *pp 
19f0f 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  = p;.  return rc
19f10 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  ;.}../*.** Attem
19f11 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65  pt to obtain the
19f12 20 65 78 63 6c 75 73 69 76 65 20 57 41 4c 20 6c   exclusive WAL l
19f13 6f 63 6b 20 64 65 66 69 6e 65 64 20 62 79 20 70  ock defined by p
19f14 61 72 61 6d 65 74 65 72 73 20 6c 6f 63 6b 49 64  arameters lockId
19f15 78 20 61 6e 64 0a 2a 2a 20 6e 2e 20 49 66 20 74  x and.** n. If t
19f16 68 65 20 61 74 74 65 6d 70 74 20 66 61 69 6c 73  he attempt fails
19f17 20 61 6e 64 20 70 61 72 61 6d 65 74 65 72 20 78   and parameter x
19f18 42 75 73 79 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  Busy is not NULL
19f19 2c 20 74 68 65 6e 20 69 74 20 69 73 20 61 0a 2a  , then it is a.*
19f1a 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 66  * busy-handler f
19f1b 75 6e 63 74 69 6f 6e 2e 20 49 6e 76 6f 6b 65 20  unction. Invoke 
19f1c 69 74 20 61 6e 64 20 72 65 74 72 79 20 74 68 65  it and retry the
19f1d 20 6c 6f 63 6b 20 75 6e 74 69 6c 20 65 69 74 68   lock until eith
19f1e 65 72 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69  er the.** lock i
19f1f 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f  s successfully o
19f20 62 74 61 69 6e 65 64 20 6f 72 20 74 68 65 20 62  btained or the b
19f21 75 73 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75  usy-handler retu
19f22 72 6e 73 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63  rns 0..*/.static
19f23 20 69 6e 74 20 77 61 6c 42 75 73 79 4c 6f 63 6b   int walBusyLock
19f24 28 0a 20 20 57 61 6c 20 2a 70 57 61 6c 2c 20 20  (.  Wal *pWal,  
19f25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19f26 20 20 20 20 2f 2a 20 57 41 4c 20 63 6f 6e 6e 65      /* WAL conne
19f27 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 28  ction */.  int (
19f28 2a 78 42 75 73 79 29 28 76 6f 69 64 2a 29 2c 20  *xBusy)(void*), 
19f29 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75             /* Fu
19f2a 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77  nction to call w
19f2b 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f  hen busy */.  vo
19f2c 69 64 20 2a 70 42 75 73 79 41 72 67 2c 20 20 20  id *pBusyArg,   
19f2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19f2e 20 43 6f 6e 74 65 78 74 20 61 72 67 75 6d 65 6e   Context argumen
19f2f 74 20 66 6f 72 20 78 42 75 73 79 48 61 6e 64 6c  t for xBusyHandl
19f30 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b  er */.  int lock
19f31 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  Idx,            
19f32 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
19f33 74 20 6f 66 20 66 69 72 73 74 20 62 79 74 65 20  t of first byte 
19f34 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74  to lock */.  int
19f35 20 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20   n              
19f36 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19f37 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
19f38 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 20  to lock */.){.  
19f39 69 6e 74 20 72 63 3b 0a 20 20 64 6f 20 7b 0a 20  int rc;.  do {. 
19f3a 20 20 20 72 63 20 3d 20 77 61 6c 4c 6f 63 6b 45     rc = walLockE
19f3b 78 63 6c 75 73 69 76 65 28 70 57 61 6c 2c 20 6c  xclusive(pWal, l
19f3c 6f 63 6b 49 64 78 2c 20 6e 29 3b 0a 20 20 7d 77  ockIdx, n);.  }w
19f3d 68 69 6c 65 28 20 78 42 75 73 79 20 26 26 20 72  hile( xBusy && r
19f3e 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26  c==SQLITE_BUSY &
19f3f 26 20 78 42 75 73 79 28 70 42 75 73 79 41 72 67  & xBusy(pBusyArg
19f40 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ) );.  return rc
19f41 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63  ;.}../*.** The c
19f42 61 63 68 65 20 6f 66 20 74 68 65 20 77 61 6c 2d  ache of the wal-
19f43 69 6e 64 65 78 20 68 65 61 64 65 72 20 6d 75 73  index header mus
19f44 74 20 62 65 20 76 61 6c 69 64 20 74 6f 20 63 61  t be valid to ca
19f45 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ll this function
19f46 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
19f47 70 61 67 65 2d 73 69 7a 65 20 69 6e 20 62 79 74  page-size in byt
19f48 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 64  es used by the d
19f49 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74  atabase..*/.stat
19f4a 69 63 20 69 6e 74 20 77 61 6c 50 61 67 65 73 69  ic int walPagesi
19f4b 7a 65 28 57 61 6c 20 2a 70 57 61 6c 29 7b 0a 20  ze(Wal *pWal){. 
19f4c 20 72 65 74 75 72 6e 20 28 70 57 61 6c 2d 3e 68   return (pWal->h
19f4d 64 72 2e 73 7a 50 61 67 65 26 30 78 66 65 30 30  dr.szPage&0xfe00
19f4e 29 20 2b 20 28 28 70 57 61 6c 2d 3e 68 64 72 2e  ) + ((pWal->hdr.
19f4f 73 7a 50 61 67 65 26 30 78 30 30 30 31 29 3c 3c  szPage&0x0001)<<
19f50 31 36 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  16);.}../*.** Co
19f51 70 79 20 61 73 20 6d 75 63 68 20 63 6f 6e 74 65  py as much conte
19f52 6e 74 20 61 73 20 77 65 20 63 61 6e 20 66 72 6f  nt as we can fro
19f53 6d 20 74 68 65 20 57 41 4c 20 62 61 63 6b 20 69  m the WAL back i
19f54 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
19f55 20 66 69 6c 65 0a 2a 2a 20 69 6e 20 72 65 73 70   file.** in resp
19f56 6f 6e 73 65 20 74 6f 20 61 6e 20 73 71 6c 69 74  onse to an sqlit
19f57 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e  e3_wal_checkpoin
19f58 74 28 29 20 72 65 71 75 65 73 74 20 6f 72 20 74  t() request or t
19f59 68 65 20 65 71 75 69 76 61 6c 65 6e 74 2e 0a 2a  he equivalent..*
19f5a 2a 0a 2a 2a 20 54 68 65 20 61 6d 6f 75 6e 74 20  *.** The amount 
19f5b 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 63  of information c
19f5c 6f 70 69 65 73 20 66 72 6f 6d 20 57 41 4c 20 74  opies from WAL t
19f5d 6f 20 64 61 74 61 62 61 73 65 20 6d 69 67 68 74  o database might
19f5e 20 62 65 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 62   be limited.** b
19f5f 79 20 61 63 74 69 76 65 20 72 65 61 64 65 72 73  y active readers
19f60 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
19f61 77 69 6c 6c 20 6e 65 76 65 72 20 6f 76 65 72 77  will never overw
19f62 72 69 74 65 20 61 20 64 61 74 61 62 61 73 65 20  rite a database 
19f63 70 61 67 65 0a 2a 2a 20 74 68 61 74 20 61 20 63  page.** that a c
19f64 6f 6e 63 75 72 72 65 6e 74 20 72 65 61 64 65 72  oncurrent reader
19f65 20 6d 69 67 68 74 20 62 65 20 75 73 69 6e 67 2e   might be using.
19f66 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 49 2f 4f 20 62  .**.** All I/O b
19f67 61 72 72 69 65 72 20 6f 70 65 72 61 74 69 6f 6e  arrier operation
19f68 73 20 28 61 2e 6b 2e 61 20 66 73 79 6e 63 73 29  s (a.k.a fsyncs)
19f69 20 6f 63 63 75 72 20 69 6e 20 74 68 69 73 20 72   occur in this r
19f6a 6f 75 74 69 6e 65 20 77 68 65 6e 0a 2a 2a 20 53  outine when.** S
19f6b 51 4c 69 74 65 20 69 73 20 69 6e 20 57 41 4c 2d  QLite is in WAL-
19f6c 6d 6f 64 65 20 69 6e 20 73 79 6e 63 68 72 6f 6e  mode in synchron
19f6d 6f 75 73 3d 4e 4f 52 4d 41 4c 2e 20 20 54 68 61  ous=NORMAL.  Tha
19f6e 74 20 6d 65 61 6e 73 20 74 68 61 74 20 69 66 20  t means that if 
19f6f 0a 2a 2a 20 63 68 65 63 6b 70 6f 69 6e 74 73 20  .** checkpoints 
19f70 61 72 65 20 61 6c 77 61 79 73 20 72 75 6e 20 62  are always run b
19f71 79 20 61 20 62 61 63 6b 67 72 6f 75 6e 64 20 74  y a background t
19f72 68 72 65 61 64 20 6f 72 20 62 61 63 6b 67 72 6f  hread or backgro
19f73 75 6e 64 20 0a 2a 2a 20 70 72 6f 63 65 73 73 2c  und .** process,
19f74 20 66 6f 72 65 67 72 6f 75 6e 64 20 74 68 72 65   foreground thre
19f75 61 64 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 62  ads will never b
19f76 6c 6f 63 6b 20 6f 6e 20 61 20 6c 65 6e 67 74 68  lock on a length
19f77 79 20 66 73 79 6e 63 20 63 61 6c 6c 2e 0a 2a 2a  y fsync call..**
19f78 0a 2a 2a 20 46 73 79 6e 63 20 69 73 20 63 61 6c  .** Fsync is cal
19f79 6c 65 64 20 6f 6e 20 74 68 65 20 57 41 4c 20 62  led on the WAL b
19f7a 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 63 6f  efore writing co
19f7b 6e 74 65 6e 74 20 6f 75 74 20 6f 66 20 74 68 65  ntent out of the
19f7c 20 57 41 4c 20 61 6e 64 0a 2a 2a 20 69 6e 74 6f   WAL and.** into
19f7d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
19f7e 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61  This ensures tha
19f7f 74 20 69 66 20 74 68 65 20 6e 65 77 20 63 6f 6e  t if the new con
19f80 74 65 6e 74 20 69 73 20 70 65 72 73 69 73 74 65  tent is persiste
19f81 6e 74 0a 2a 2a 20 69 6e 20 74 68 65 20 57 41 4c  nt.** in the WAL
19f82 20 61 6e 64 20 63 61 6e 20 62 65 20 72 65 63 6f   and can be reco
19f83 76 65 72 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20  vered following 
19f84 61 20 70 6f 77 65 72 2d 6c 6f 73 73 20 6f 72 20  a power-loss or 
19f85 68 61 72 64 20 72 65 73 65 74 2e 0a 2a 2a 0a 2a  hard reset..**.*
19f86 2a 20 46 73 79 6e 63 20 69 73 20 61 6c 73 6f 20  * Fsync is also 
19f87 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 64 61  called on the da
19f88 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 28  tabase file if (
19f89 61 6e 64 20 6f 6e 6c 79 20 69 66 29 20 74 68 65  and only if) the
19f8a 20 65 6e 74 69 72 65 0a 2a 2a 20 57 41 4c 20 63   entire.** WAL c
19f8b 6f 6e 74 65 6e 74 20 69 73 20 63 6f 70 69 65 64  ontent is copied
19f8c 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61   into the databa
19f8d 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 73  se file.  This s
19f8e 65 63 6f 6e 64 20 66 73 79 6e 63 20 6d 61 6b 65  econd fsync make
19f8f 73 0a 2a 2a 20 69 74 20 73 61 66 65 20 74 6f 20  s.** it safe to 
19f90 64 65 6c 65 74 65 20 74 68 65 20 57 41 4c 20 73  delete the WAL s
19f91 69 6e 63 65 20 74 68 65 20 6e 65 77 20 63 6f 6e  ince the new con
19f92 74 65 6e 74 20 77 69 6c 6c 20 70 65 72 73 69 73  tent will persis
19f93 74 20 69 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61  t in the.** data
19f94 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  base file..**.**
19f95 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 75 73   This routine us
19f96 65 73 20 61 6e 64 20 75 70 64 61 74 65 73 20 74  es and updates t
19f97 68 65 20 6e 42 61 63 6b 66 69 6c 6c 20 66 69 65  he nBackfill fie
19f98 6c 64 20 6f 66 20 74 68 65 20 77 61 6c 2d 69 6e  ld of the wal-in
19f99 64 65 78 20 68 65 61 64 65 72 2e 0a 2a 2a 20 54  dex header..** T
19f9a 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  his is the only 
19f9b 72 6f 75 74 69 6e 65 20 74 68 61 20 77 69 6c 6c  routine tha will
19f9c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 76 61   increase the va
19f9d 6c 75 65 20 6f 66 20 6e 42 61 63 6b 66 69 6c 6c  lue of nBackfill
19f9e 2e 20 20 0a 2a 2a 20 28 41 20 57 41 4c 20 72 65  .  .** (A WAL re
19f9f 73 65 74 20 6f 72 20 72 65 63 6f 76 65 72 79 20  set or recovery 
19fa0 77 69 6c 6c 20 72 65 76 65 72 74 20 6e 42 61 63  will revert nBac
19fa1 6b 66 69 6c 6c 20 74 6f 20 7a 65 72 6f 2c 20 62  kfill to zero, b
19fa2 75 74 20 6e 6f 74 20 69 6e 63 72 65 61 73 65 0a  ut not increase.
19fa3 2a 2a 20 69 74 73 20 76 61 6c 75 65 2e 29 0a 2a  ** its value.).*
19fa4 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20  *.** The caller 
19fa5 6d 75 73 74 20 62 65 20 68 6f 6c 64 69 6e 67 20  must be holding 
19fa6 73 75 66 66 69 63 69 65 6e 74 20 6c 6f 63 6b 73  sufficient locks
19fa7 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20   to ensure that 
19fa8 6e 6f 20 6f 74 68 65 72 0a 2a 2a 20 63 68 65 63  no other.** chec
19fa9 6b 70 6f 69 6e 74 20 69 73 20 72 75 6e 6e 69 6e  kpoint is runnin
19faa 67 20 28 69 6e 20 61 6e 79 20 6f 74 68 65 72 20  g (in any other 
19fab 74 68 72 65 61 64 20 6f 72 20 70 72 6f 63 65 73  thread or proces
19fac 73 29 20 61 74 20 74 68 65 20 73 61 6d 65 0a 2a  s) at the same.*
19fad 2a 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69  * time..*/.stati
19fae 63 20 69 6e 74 20 77 61 6c 43 68 65 63 6b 70 6f  c int walCheckpo
19faf 69 6e 74 28 0a 20 20 57 61 6c 20 2a 70 57 61 6c  int(.  Wal *pWal
19fb0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
19fb1 20 20 20 20 20 20 20 2f 2a 20 57 61 6c 20 63 6f         /* Wal co
19fb2 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  nnection */.  in
19fb3 74 20 65 4d 6f 64 65 2c 20 20 20 20 20 20 20 20  t eMode,        
19fb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19fb5 20 4f 6e 65 20 6f 66 20 50 41 53 53 49 56 45 2c   One of PASSIVE,
19fb6 20 46 55 4c 4c 20 6f 72 20 52 45 53 54 41 52 54   FULL or RESTART
19fb7 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73   */.  int (*xBus
19fb8 79 43 61 6c 6c 29 28 76 6f 69 64 2a 29 2c 20 20  yCall)(void*),  
19fb9 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f        /* Functio
19fba 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20 62  n to call when b
19fbb 75 73 79 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  usy */.  void *p
19fbc 42 75 73 79 41 72 67 2c 20 20 20 20 20 20 20 20  BusyArg,        
19fbd 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74           /* Cont
19fbe 65 78 74 20 61 72 67 75 6d 65 6e 74 20 66 6f 72  ext argument for
19fbf 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f   xBusyHandler */
19fc0 0a 20 20 69 6e 74 20 73 79 6e 63 5f 66 6c 61 67  .  int sync_flag
19fc1 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
19fc2 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f 72 20     /* Flags for 
19fc3 4f 73 53 79 6e 63 28 29 20 28 6f 72 20 30 29 20  OsSync() (or 0) 
19fc4 2a 2f 0a 20 20 75 38 20 2a 7a 42 75 66 20 20 20  */.  u8 *zBuf   
19fc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19fc6 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72       /* Temporar
19fc7 79 20 62 75 66 66 65 72 20 74 6f 20 75 73 65 20  y buffer to use 
19fc8 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20  */.){.  int rc; 
19fc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19fca 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
19fcb 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  n code */.  int 
19fcc 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  szPage;         
19fcd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
19fce 61 74 61 62 61 73 65 20 70 61 67 65 2d 73 69 7a  atabase page-siz
19fcf 65 20 2a 2f 0a 20 20 57 61 6c 49 74 65 72 61 74  e */.  WalIterat
19fd0 6f 72 20 2a 70 49 74 65 72 20 3d 20 30 3b 20 20  or *pIter = 0;  
19fd1 20 20 20 20 20 20 20 2f 2a 20 57 61 6c 20 69 74         /* Wal it
19fd2 65 72 61 74 6f 72 20 63 6f 6e 74 65 78 74 20 2a  erator context *
19fd3 2f 0a 20 20 75 33 32 20 69 44 62 70 61 67 65 20  /.  u32 iDbpage 
19fd4 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
19fd5 20 20 20 20 2f 2a 20 4e 65 78 74 20 64 61 74 61      /* Next data
19fd6 62 61 73 65 20 70 61 67 65 20 74 6f 20 77 72 69  base page to wri
19fd7 74 65 20 2a 2f 0a 20 20 75 33 32 20 69 46 72 61  te */.  u32 iFra
19fd8 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  me = 0;         
19fd9 20 20 20 20 20 20 20 20 2f 2a 20 57 61 6c 20 66          /* Wal f
19fda 72 61 6d 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  rame containing 
19fdb 64 61 74 61 20 66 6f 72 20 69 44 62 70 61 67 65  data for iDbpage
19fdc 20 2a 2f 0a 20 20 75 33 32 20 6d 78 53 61 66 65   */.  u32 mxSafe
19fdd 46 72 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  Frame;          
19fde 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 66 72 61        /* Max fra
19fdf 6d 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 62  me that can be b
19fe0 61 63 6b 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 75  ackfilled */.  u
19fe1 33 32 20 6d 78 50 61 67 65 3b 20 20 20 20 20 20  32 mxPage;      
19fe2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19fe3 2a 20 4d 61 78 20 64 61 74 61 62 61 73 65 20 70  * Max database p
19fe4 61 67 65 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a  age to write */.
19fe5 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
19fe6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19fe7 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
19fe8 72 20 2a 2f 0a 20 20 76 6f 6c 61 74 69 6c 65 20  r */.  volatile 
19fe9 57 61 6c 43 6b 70 74 49 6e 66 6f 20 2a 70 49 6e  WalCkptInfo *pIn
19fea 66 6f 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 68  fo;    /* The ch
19feb 65 63 6b 70 6f 69 6e 74 20 73 74 61 74 75 73 20  eckpoint status 
19fec 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20  information */. 
19fed 20 69 6e 74 20 28 2a 78 42 75 73 79 29 28 76 6f   int (*xBusy)(vo
19fee 69 64 2a 29 20 3d 20 30 3b 20 20 20 20 20 20 20  id*) = 0;       
19fef 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20   /* Function to 
19ff0 63 61 6c 6c 20 77 68 65 6e 20 77 61 69 74 69 6e  call when waitin
19ff1 67 20 66 6f 72 20 6c 6f 63 6b 73 20 2a 2f 0a 0a  g for locks */..
19ff2 20 20 73 7a 50 61 67 65 20 3d 20 77 61 6c 50 61    szPage = walPa
19ff3 67 65 73 69 7a 65 28 70 57 61 6c 29 3b 0a 20 20  gesize(pWal);.  
19ff4 74 65 73 74 63 61 73 65 28 20 73 7a 50 61 67 65  testcase( szPage
19ff5 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 74 65 73  <=32768 );.  tes
19ff6 74 63 61 73 65 28 20 73 7a 50 61 67 65 3e 3d 36  tcase( szPage>=6
19ff7 35 35 33 36 20 29 3b 0a 20 20 70 49 6e 66 6f 20  5536 );.  pInfo 
19ff8 3d 20 77 61 6c 43 6b 70 74 49 6e 66 6f 28 70 57  = walCkptInfo(pW
19ff9 61 6c 29 3b 0a 20 20 69 66 28 20 70 49 6e 66 6f  al);.  if( pInfo
19ffa 2d 3e 6e 42 61 63 6b 66 69 6c 6c 3e 3d 70 57 61  ->nBackfill>=pWa
19ffb 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 20 29  l->hdr.mxFrame )
19ffc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
19ffd 4b 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  K;..  /* Allocat
19ffe 65 20 74 68 65 20 69 74 65 72 61 74 6f 72 20 2a  e the iterator *
19fff 2f 0a 20 20 72 63 20 3d 20 77 61 6c 49 74 65 72  /.  rc = walIter
1a000 61 74 6f 72 49 6e 69 74 28 70 57 61 6c 2c 20 26  atorInit(pWal, &
1a001 70 49 74 65 72 29 3b 0a 20 20 69 66 28 20 72 63  pIter);.  if( rc
1a002 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1a003 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1a004 7d 0a 20 20 61 73 73 65 72 74 28 20 70 49 74 65  }.  assert( pIte
1a005 72 20 29 3b 0a 0a 20 20 69 66 28 20 65 4d 6f 64  r );..  if( eMod
1a006 65 21 3d 53 51 4c 49 54 45 5f 43 48 45 43 4b 50  e!=SQLITE_CHECKP
1a007 4f 49 4e 54 5f 50 41 53 53 49 56 45 20 29 20 78  OINT_PASSIVE ) x
1a008 42 75 73 79 20 3d 20 78 42 75 73 79 43 61 6c 6c  Busy = xBusyCall
1a009 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20  ;..  /* Compute 
1a00a 69 6e 20 6d 78 53 61 66 65 46 72 61 6d 65 20 74  in mxSafeFrame t
1a00b 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
1a00c 6c 61 73 74 20 66 72 61 6d 65 20 6f 66 20 74 68  last frame of th
1a00d 65 20 57 41 4c 20 74 68 61 74 20 69 73 0a 20 20  e WAL that is.  
1a00e 2a 2a 20 73 61 66 65 20 74 6f 20 77 72 69 74 65  ** safe to write
1a00f 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61   into the databa
1a010 73 65 2e 20 20 46 72 61 6d 65 73 20 62 65 79 6f  se.  Frames beyo
1a011 6e 64 20 6d 78 53 61 66 65 46 72 61 6d 65 20 6d  nd mxSafeFrame m
1a012 69 67 68 74 0a 20 20 2a 2a 20 6f 76 65 72 77 72  ight.  ** overwr
1a013 69 74 65 20 64 61 74 61 62 61 73 65 20 70 61 67  ite database pag
1a014 65 73 20 74 68 61 74 20 61 72 65 20 69 6e 20 75  es that are in u
1a015 73 65 20 62 79 20 61 63 74 69 76 65 20 72 65 61  se by active rea
1a016 64 65 72 73 20 61 6e 64 20 74 68 75 73 0a 20 20  ders and thus.  
1a017 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 62 61 63  ** cannot be bac
1a018 6b 66 69 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65  kfilled from the
1a019 20 57 41 4c 2e 0a 20 20 2a 2f 0a 20 20 6d 78 53   WAL..  */.  mxS
1a01a 61 66 65 46 72 61 6d 65 20 3d 20 70 57 61 6c 2d  afeFrame = pWal-
1a01b 3e 68 64 72 2e 6d 78 46 72 61 6d 65 3b 0a 20 20  >hdr.mxFrame;.  
1a01c 6d 78 50 61 67 65 20 3d 20 70 57 61 6c 2d 3e 68  mxPage = pWal->h
1a01d 64 72 2e 6e 50 61 67 65 3b 0a 20 20 66 6f 72 28  dr.nPage;.  for(
1a01e 69 3d 31 3b 20 69 3c 57 41 4c 5f 4e 52 45 41 44  i=1; i<WAL_NREAD
1a01f 45 52 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 33  ER; i++){.    u3
1a020 32 20 79 20 3d 20 70 49 6e 66 6f 2d 3e 61 52 65  2 y = pInfo->aRe
1a021 61 64 4d 61 72 6b 5b 69 5d 3b 0a 20 20 20 20 69  adMark[i];.    i
1a022 66 28 20 6d 78 53 61 66 65 46 72 61 6d 65 3e 79  f( mxSafeFrame>y
1a023 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
1a024 28 20 79 3c 3d 70 57 61 6c 2d 3e 68 64 72 2e 6d  ( y<=pWal->hdr.m
1a025 78 46 72 61 6d 65 20 29 3b 0a 20 20 20 20 20 20  xFrame );.      
1a026 72 63 20 3d 20 77 61 6c 42 75 73 79 4c 6f 63 6b  rc = walBusyLock
1a027 28 70 57 61 6c 2c 20 78 42 75 73 79 2c 20 70 42  (pWal, xBusy, pB
1a028 75 73 79 41 72 67 2c 20 57 41 4c 5f 52 45 41 44  usyArg, WAL_READ
1a029 5f 4c 4f 43 4b 28 69 29 2c 20 31 29 3b 0a 20 20  _LOCK(i), 1);.  
1a02a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1a02b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1a02c 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61 72   pInfo->aReadMar
1a02d 6b 5b 69 5d 20 3d 20 28 69 3d 3d 31 20 3f 20 6d  k[i] = (i==1 ? m
1a02e 78 53 61 66 65 46 72 61 6d 65 20 3a 20 52 45 41  xSafeFrame : REA
1a02f 44 4d 41 52 4b 5f 4e 4f 54 5f 55 53 45 44 29 3b  DMARK_NOT_USED);
1a030 0a 20 20 20 20 20 20 20 20 77 61 6c 55 6e 6c 6f  .        walUnlo
1a031 63 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61 6c  ckExclusive(pWal
1a032 2c 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28  , WAL_READ_LOCK(
1a033 69 29 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 65  i), 1);.      }e
1a034 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  lse if( rc==SQLI
1a035 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20  TE_BUSY ){.     
1a036 20 20 20 6d 78 53 61 66 65 46 72 61 6d 65 20 3d     mxSafeFrame =
1a037 20 79 3b 0a 20 20 20 20 20 20 20 20 78 42 75 73   y;.        xBus
1a038 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c  y = 0;.      }el
1a039 73 65 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  se{.        goto
1a03a 20 77 61 6c 63 68 65 63 6b 70 6f 69 6e 74 5f 6f   walcheckpoint_o
1a03b 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ut;.      }.    
1a03c 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 49 6e  }.  }..  if( pIn
1a03d 66 6f 2d 3e 6e 42 61 63 6b 66 69 6c 6c 3c 6d 78  fo->nBackfill<mx
1a03e 53 61 66 65 46 72 61 6d 65 0a 20 20 20 26 26 20  SafeFrame.   && 
1a03f 28 72 63 20 3d 20 77 61 6c 42 75 73 79 4c 6f 63  (rc = walBusyLoc
1a040 6b 28 70 57 61 6c 2c 20 78 42 75 73 79 2c 20 70  k(pWal, xBusy, p
1a041 42 75 73 79 41 72 67 2c 20 57 41 4c 5f 52 45 41  BusyArg, WAL_REA
1a042 44 5f 4c 4f 43 4b 28 30 29 2c 20 31 29 29 3d 3d  D_LOCK(0), 1))==
1a043 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 29 7b 0a 20  SQLITE_OK.  ){. 
1a044 20 20 20 69 36 34 20 6e 53 69 7a 65 3b 20 20 20     i64 nSize;   
1a045 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a046 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 69 7a 65   /* Current size
1a047 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c   of database fil
1a048 65 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 42 61  e */.    u32 nBa
1a049 63 6b 66 69 6c 6c 20 3d 20 70 49 6e 66 6f 2d 3e  ckfill = pInfo->
1a04a 6e 42 61 63 6b 66 69 6c 6c 3b 0a 0a 20 20 20 20  nBackfill;..    
1a04b 2f 2a 20 53 79 6e 63 20 74 68 65 20 57 41 4c 20  /* Sync the WAL 
1a04c 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 20 20 69  to disk */.    i
1a04d 66 28 20 73 79 6e 63 5f 66 6c 61 67 73 20 29 7b  f( sync_flags ){
1a04e 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1a04f 74 65 33 4f 73 53 79 6e 63 28 70 57 61 6c 2d 3e  te3OsSync(pWal->
1a050 70 57 61 6c 46 64 2c 20 73 79 6e 63 5f 66 6c 61  pWalFd, sync_fla
1a051 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  gs);.    }..    
1a052 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  /* If the databa
1a053 73 65 20 66 69 6c 65 20 6d 61 79 20 67 72 6f 77  se file may grow
1a054 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20   as a result of 
1a055 74 68 69 73 20 63 68 65 63 6b 70 6f 69 6e 74 2c  this checkpoint,
1a056 20 68 69 6e 74 0a 20 20 20 20 2a 2a 20 61 62 6f   hint.    ** abo
1a057 75 74 20 74 68 65 20 65 76 65 6e 74 75 61 6c 20  ut the eventual 
1a058 73 69 7a 65 20 6f 66 20 74 68 65 20 64 62 20 66  size of the db f
1a059 69 6c 65 20 74 6f 20 74 68 65 20 56 46 53 20 6c  ile to the VFS l
1a05a 61 79 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  ayer. .    */.  
1a05b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1a05c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 36 34  _OK ){.      i64
1a05d 20 6e 52 65 71 20 3d 20 28 28 69 36 34 29 6d 78   nReq = ((i64)mx
1a05e 50 61 67 65 20 2a 20 73 7a 50 61 67 65 29 3b 0a  Page * szPage);.
1a05f 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1a060 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 57 61  e3OsFileSize(pWa
1a061 6c 2d 3e 70 44 62 46 64 2c 20 26 6e 53 69 7a 65  l->pDbFd, &nSize
1a062 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
1a063 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 53  =SQLITE_OK && nS
1a064 69 7a 65 3c 6e 52 65 71 20 29 7b 0a 20 20 20 20  ize<nReq ){.    
1a065 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69 6c      sqlite3OsFil
1a066 65 43 6f 6e 74 72 6f 6c 48 69 6e 74 28 70 57 61  eControlHint(pWa
1a067 6c 2d 3e 70 44 62 46 64 2c 20 53 51 4c 49 54 45  l->pDbFd, SQLITE
1a068 5f 46 43 4e 54 4c 5f 53 49 5a 45 5f 48 49 4e 54  _FCNTL_SIZE_HINT
1a069 2c 20 26 6e 52 65 71 29 3b 0a 20 20 20 20 20 20  , &nReq);.      
1a06a 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
1a06b 49 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20  Iterate through 
1a06c 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
1a06d 74 68 65 20 57 41 4c 2c 20 63 6f 70 79 69 6e 67  the WAL, copying
1a06e 20 64 61 74 61 20 74 6f 20 74 68 65 20 64 62 20   data to the db 
1a06f 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 77 68 69  file. */.    whi
1a070 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
1a071 4b 20 26 26 20 30 3d 3d 77 61 6c 49 74 65 72 61  K && 0==walItera
1a072 74 6f 72 4e 65 78 74 28 70 49 74 65 72 2c 20 26  torNext(pIter, &
1a073 69 44 62 70 61 67 65 2c 20 26 69 46 72 61 6d 65  iDbpage, &iFrame
1a074 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69  ) ){.      i64 i
1a075 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 61 73  Offset;.      as
1a076 73 65 72 74 28 20 77 61 6c 46 72 61 6d 65 50 67  sert( walFramePg
1a077 6e 6f 28 70 57 61 6c 2c 20 69 46 72 61 6d 65 29  no(pWal, iFrame)
1a078 3d 3d 69 44 62 70 61 67 65 20 29 3b 0a 20 20 20  ==iDbpage );.   
1a079 20 20 20 69 66 28 20 69 46 72 61 6d 65 3c 3d 6e     if( iFrame<=n
1a07a 42 61 63 6b 66 69 6c 6c 20 7c 7c 20 69 46 72 61  Backfill || iFra
1a07b 6d 65 3e 6d 78 53 61 66 65 46 72 61 6d 65 20 7c  me>mxSafeFrame |
1a07c 7c 20 69 44 62 70 61 67 65 3e 6d 78 50 61 67 65  | iDbpage>mxPage
1a07d 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
1a07e 20 20 20 69 4f 66 66 73 65 74 20 3d 20 77 61 6c     iOffset = wal
1a07f 46 72 61 6d 65 4f 66 66 73 65 74 28 69 46 72 61  FrameOffset(iFra
1a080 6d 65 2c 20 73 7a 50 61 67 65 29 20 2b 20 57 41  me, szPage) + WA
1a081 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49 5a 45 3b  L_FRAME_HDRSIZE;
1a082 0a 20 20 20 20 20 20 2f 2a 20 74 65 73 74 63 61  .      /* testca
1a083 73 65 28 20 49 53 5f 42 49 47 5f 49 4e 54 28 69  se( IS_BIG_INT(i
1a084 4f 66 66 73 65 74 29 20 29 3b 20 2f 2f 20 72 65  Offset) ); // re
1a085 71 75 69 72 65 73 20 61 20 34 47 69 42 20 57 41  quires a 4GiB WA
1a086 4c 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 20 20  L file */.      
1a087 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65  rc = sqlite3OsRe
1a088 61 64 28 70 57 61 6c 2d 3e 70 57 61 6c 46 64 2c  ad(pWal->pWalFd,
1a089 20 7a 42 75 66 2c 20 73 7a 50 61 67 65 2c 20 69   zBuf, szPage, i
1a08a 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 69  Offset);.      i
1a08b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1a08c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
1a08d 69 4f 66 66 73 65 74 20 3d 20 28 69 44 62 70 61  iOffset = (iDbpa
1a08e 67 65 2d 31 29 2a 28 69 36 34 29 73 7a 50 61 67  ge-1)*(i64)szPag
1a08f 65 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  e;.      testcas
1a090 65 28 20 49 53 5f 42 49 47 5f 49 4e 54 28 69 4f  e( IS_BIG_INT(iO
1a091 66 66 73 65 74 29 20 29 3b 0a 20 20 20 20 20 20  ffset) );.      
1a092 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
1a093 69 74 65 28 70 57 61 6c 2d 3e 70 44 62 46 64 2c  ite(pWal->pDbFd,
1a094 20 7a 42 75 66 2c 20 73 7a 50 61 67 65 2c 20 69   zBuf, szPage, i
1a095 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 69  Offset);.      i
1a096 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1a097 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a   ) break;.    }.
1a098 0a 20 20 20 20 2f 2a 20 49 66 20 77 6f 72 6b 20  .    /* If work 
1a099 77 61 73 20 61 63 74 75 61 6c 6c 79 20 61 63 63  was actually acc
1a09a 6f 6d 70 6c 69 73 68 65 64 2e 2e 2e 20 2a 2f 0a  omplished... */.
1a09b 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1a09c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
1a09d 66 28 20 6d 78 53 61 66 65 46 72 61 6d 65 3d 3d  f( mxSafeFrame==
1a09e 77 61 6c 49 6e 64 65 78 48 64 72 28 70 57 61 6c  walIndexHdr(pWal
1a09f 29 2d 3e 6d 78 46 72 61 6d 65 20 29 7b 0a 20 20  )->mxFrame ){.  
1a0a0 20 20 20 20 20 20 69 36 34 20 73 7a 44 62 20 3d        i64 szDb =
1a0a1 20 70 57 61 6c 2d 3e 68 64 72 2e 6e 50 61 67 65   pWal->hdr.nPage
1a0a2 2a 28 69 36 34 29 73 7a 50 61 67 65 3b 0a 20 20  *(i64)szPage;.  
1a0a3 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1a0a4 49 53 5f 42 49 47 5f 49 4e 54 28 73 7a 44 62 29  IS_BIG_INT(szDb)
1a0a5 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   );.        rc =
1a0a6 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61   sqlite3OsTrunca
1a0a7 74 65 28 70 57 61 6c 2d 3e 70 44 62 46 64 2c 20  te(pWal->pDbFd, 
1a0a8 73 7a 44 62 29 3b 0a 20 20 20 20 20 20 20 20 69  szDb);.        i
1a0a9 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1a0aa 20 26 26 20 73 79 6e 63 5f 66 6c 61 67 73 20 29   && sync_flags )
1a0ab 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
1a0ac 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70   sqlite3OsSync(p
1a0ad 57 61 6c 2d 3e 70 44 62 46 64 2c 20 73 79 6e 63  Wal->pDbFd, sync
1a0ae 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20  _flags);.       
1a0af 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
1a0b0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1a0b1 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 49  OK ){.        pI
1a0b2 6e 66 6f 2d 3e 6e 42 61 63 6b 66 69 6c 6c 20 3d  nfo->nBackfill =
1a0b3 20 6d 78 53 61 66 65 46 72 61 6d 65 3b 0a 20 20   mxSafeFrame;.  
1a0b4 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
1a0b5 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20   /* Release the 
1a0b6 72 65 61 64 65 72 20 6c 6f 63 6b 20 68 65 6c 64  reader lock held
1a0b7 20 77 68 69 6c 65 20 62 61 63 6b 66 69 6c 6c 69   while backfilli
1a0b8 6e 67 20 2a 2f 0a 20 20 20 20 77 61 6c 55 6e 6c  ng */.    walUnl
1a0b9 6f 63 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61  ockExclusive(pWa
1a0ba 6c 2c 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b  l, WAL_READ_LOCK
1a0bb 28 30 29 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20  (0), 1);.  }..  
1a0bc 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42  if( rc==SQLITE_B
1a0bd 55 53 59 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65  USY ){.    /* Re
1a0be 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 63  set the return c
1a0bf 6f 64 65 20 73 6f 20 61 73 20 6e 6f 74 20 74 6f  ode so as not to
1a0c0 20 72 65 70 6f 72 74 20 61 20 63 68 65 63 6b 70   report a checkp
1a0c1 6f 69 6e 74 20 66 61 69 6c 75 72 65 0a 20 20 20  oint failure.   
1a0c2 20 2a 2a 20 6a 75 73 74 20 62 65 63 61 75 73 65   ** just because
1a0c3 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76   there are activ
1a0c4 65 20 72 65 61 64 65 72 73 2e 20 20 2a 2f 0a 20  e readers.  */. 
1a0c5 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
1a0c6 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  K;.  }..  /* If 
1a0c7 74 68 69 73 20 69 73 20 61 6e 20 53 51 4c 49 54  this is an SQLIT
1a0c8 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 52 45 53  E_CHECKPOINT_RES
1a0c9 54 41 52 54 20 6f 70 65 72 61 74 69 6f 6e 2c 20  TART operation, 
1a0ca 61 6e 64 20 74 68 65 20 65 6e 74 69 72 65 20 77  and the entire w
1a0cb 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 68 61 73  al.  ** file has
1a0cc 20 62 65 65 6e 20 63 6f 70 69 65 64 20 69 6e 74   been copied int
1a0cd 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
1a0ce 69 6c 65 2c 20 74 68 65 6e 20 62 6c 6f 63 6b 20  ile, then block 
1a0cf 75 6e 74 69 6c 20 61 6c 6c 0a 20 20 2a 2a 20 72  until all.  ** r
1a0d0 65 61 64 65 72 73 20 68 61 76 65 20 66 69 6e 69  eaders have fini
1a0d1 73 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 77  shed using the w
1a0d2 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 65 6e  al file. This en
1a0d3 73 75 72 65 73 20 74 68 61 74 20 74 68 65 20 6e  sures that the n
1a0d4 65 78 74 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73  ext.  ** process
1a0d5 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65   to write to the
1a0d6 20 64 61 74 61 62 61 73 65 20 72 65 73 74 61 72   database restar
1a0d7 74 73 20 74 68 65 20 77 61 6c 20 66 69 6c 65 2e  ts the wal file.
1a0d8 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  .  */.  if( rc==
1a0d9 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 4d 6f  SQLITE_OK && eMo
1a0da 64 65 21 3d 53 51 4c 49 54 45 5f 43 48 45 43 4b  de!=SQLITE_CHECK
1a0db 50 4f 49 4e 54 5f 50 41 53 53 49 56 45 20 29 7b  POINT_PASSIVE ){
1a0dc 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 57 61  .    assert( pWa
1a0dd 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 20 29 3b 0a  l->writeLock );.
1a0de 20 20 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e 6e      if( pInfo->n
1a0df 42 61 63 6b 66 69 6c 6c 3c 70 57 61 6c 2d 3e 68  Backfill<pWal->h
1a0e0 64 72 2e 6d 78 46 72 61 6d 65 20 29 7b 0a 20 20  dr.mxFrame ){.  
1a0e1 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1a0e2 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 20  BUSY;.    }else 
1a0e3 69 66 28 20 65 4d 6f 64 65 3d 3d 53 51 4c 49 54  if( eMode==SQLIT
1a0e4 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 52 45 53  E_CHECKPOINT_RES
1a0e5 54 41 52 54 20 29 7b 0a 20 20 20 20 20 20 61 73  TART ){.      as
1a0e6 73 65 72 74 28 20 6d 78 53 61 66 65 46 72 61 6d  sert( mxSafeFram
1a0e7 65 3d 3d 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46  e==pWal->hdr.mxF
1a0e8 72 61 6d 65 20 29 3b 0a 20 20 20 20 20 20 72 63  rame );.      rc
1a0e9 20 3d 20 77 61 6c 42 75 73 79 4c 6f 63 6b 28 70   = walBusyLock(p
1a0ea 57 61 6c 2c 20 78 42 75 73 79 2c 20 70 42 75 73  Wal, xBusy, pBus
1a0eb 79 41 72 67 2c 20 57 41 4c 5f 52 45 41 44 5f 4c  yArg, WAL_READ_L
1a0ec 4f 43 4b 28 31 29 2c 20 57 41 4c 5f 4e 52 45 41  OCK(1), WAL_NREA
1a0ed 44 45 52 2d 31 29 3b 0a 20 20 20 20 20 20 69 66  DER-1);.      if
1a0ee 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1a0ef 29 7b 0a 20 20 20 20 20 20 20 20 77 61 6c 55 6e  ){.        walUn
1a0f0 6c 6f 63 6b 45 78 63 6c 75 73 69 76 65 28 70 57  lockExclusive(pW
1a0f1 61 6c 2c 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43  al, WAL_READ_LOC
1a0f2 4b 28 31 29 2c 20 57 41 4c 5f 4e 52 45 41 44 45  K(1), WAL_NREADE
1a0f3 52 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  R-1);.      }.  
1a0f4 20 20 7d 0a 20 20 7d 0a 0a 20 77 61 6c 63 68 65    }.  }.. walche
1a0f5 63 6b 70 6f 69 6e 74 5f 6f 75 74 3a 0a 20 20 77  ckpoint_out:.  w
1a0f6 61 6c 49 74 65 72 61 74 6f 72 46 72 65 65 28 70  alIteratorFree(p
1a0f7 49 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  Iter);.  return 
1a0f8 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  rc;.}../*.** If 
1a0f9 74 68 65 20 57 41 4c 20 66 69 6c 65 20 69 73 20  the WAL file is 
1a0fa 63 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72  currently larger
1a0fb 20 74 68 61 6e 20 6e 4d 61 78 20 62 79 74 65 73   than nMax bytes
1a0fc 20 69 6e 20 73 69 7a 65 2c 20 74 72 75 6e 63 61   in size, trunca
1a0fd 74 65 0a 2a 2a 20 69 74 20 74 6f 20 65 78 61 63  te.** it to exac
1a0fe 74 6c 79 20 6e 4d 61 78 20 62 79 74 65 73 2e 20  tly nMax bytes. 
1a0ff 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
1a100 72 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73  rs while doing s
1a101 6f 2c 20 69 67 6e 6f 72 65 20 69 74 2e 0a 2a 2f  o, ignore it..*/
1a102 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 61 6c  .static void wal
1a103 4c 69 6d 69 74 53 69 7a 65 28 57 61 6c 20 2a 70  LimitSize(Wal *p
1a104 57 61 6c 2c 20 69 36 34 20 6e 4d 61 78 29 7b 0a  Wal, i64 nMax){.
1a105 20 20 69 36 34 20 73 7a 3b 0a 20 20 69 6e 74 20    i64 sz;.  int 
1a106 72 78 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67  rx;.  sqlite3Beg
1a107 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
1a108 3b 0a 20 20 72 78 20 3d 20 73 71 6c 69 74 65 33  ;.  rx = sqlite3
1a109 4f 73 46 69 6c 65 53 69 7a 65 28 70 57 61 6c 2d  OsFileSize(pWal-
1a10a 3e 70 57 61 6c 46 64 2c 20 26 73 7a 29 3b 0a 20  >pWalFd, &sz);. 
1a10b 20 69 66 28 20 72 78 3d 3d 53 51 4c 49 54 45 5f   if( rx==SQLITE_
1a10c 4f 4b 20 26 26 20 28 73 7a 20 3e 20 6e 4d 61 78  OK && (sz > nMax
1a10d 20 29 20 29 7b 0a 20 20 20 20 72 78 20 3d 20 73   ) ){.    rx = s
1a10e 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
1a10f 28 70 57 61 6c 2d 3e 70 57 61 6c 46 64 2c 20 6e  (pWal->pWalFd, n
1a110 4d 61 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Max);.  }.  sqli
1a111 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
1a112 6f 63 28 29 3b 0a 20 20 69 66 28 20 72 78 20 29  oc();.  if( rx )
1a113 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f  {.    sqlite3_lo
1a114 67 28 72 78 2c 20 22 63 61 6e 6e 6f 74 20 6c 69  g(rx, "cannot li
1a115 6d 69 74 20 57 41 4c 20 73 69 7a 65 3a 20 25 73  mit WAL size: %s
1a116 22 2c 20 70 57 61 6c 2d 3e 7a 57 61 6c 4e 61 6d  ", pWal->zWalNam
1a117 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  e);.  }.}../*.**
1a118 20 43 6c 6f 73 65 20 61 20 63 6f 6e 6e 65 63 74   Close a connect
1a119 69 6f 6e 20 74 6f 20 61 20 6c 6f 67 20 66 69 6c  ion to a log fil
1a11a 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1a11b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1a11c 57 61 6c 43 6c 6f 73 65 28 0a 20 20 57 61 6c 20  WalClose(.  Wal 
1a11d 2a 70 57 61 6c 2c 20 20 20 20 20 20 20 20 20 20  *pWal,          
1a11e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
1a11f 61 6c 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a 20  al to close */. 
1a120 20 69 6e 74 20 73 79 6e 63 5f 66 6c 61 67 73 2c   int sync_flags,
1a121 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a122 20 2f 2a 20 46 6c 61 67 73 20 74 6f 20 70 61 73   /* Flags to pas
1a123 73 20 74 6f 20 4f 73 53 79 6e 63 28 29 20 28 6f  s to OsSync() (o
1a124 72 20 30 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  r 0) */.  int nB
1a125 75 66 2c 0a 20 20 75 38 20 2a 7a 42 75 66 20 20  uf,.  u8 *zBuf  
1a126 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a127 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20        /* Buffer 
1a128 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42 75 66  of at least nBuf
1a129 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 69   bytes */.){.  i
1a12a 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1a12b 4b 3b 0a 20 20 69 66 28 20 70 57 61 6c 20 29 7b  K;.  if( pWal ){
1a12c 0a 20 20 20 20 69 6e 74 20 69 73 44 65 6c 65 74  .    int isDelet
1a12d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
1a12e 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 75 6e     /* True to un
1a12f 6c 69 6e 6b 20 77 61 6c 20 61 6e 64 20 77 61 6c  link wal and wal
1a130 2d 69 6e 64 65 78 20 66 69 6c 65 73 20 2a 2f 0a  -index files */.
1a131 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 45 58  .    /* If an EX
1a132 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 63 61 6e  CLUSIVE lock can
1a133 20 62 65 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20   be obtained on 
1a134 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1a135 65 20 28 75 73 69 6e 67 20 74 68 65 0a 20 20 20  e (using the.   
1a136 20 2a 2a 20 6f 72 64 69 6e 61 72 79 2c 20 72 6f   ** ordinary, ro
1a137 6c 6c 62 61 63 6b 2d 6d 6f 64 65 20 6c 6f 63 6b  llback-mode lock
1a138 69 6e 67 20 6d 65 74 68 6f 64 73 2c 20 74 68 69  ing methods, thi
1a139 73 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61  s guarantees tha
1a13a 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6e  t the.    ** con
1a13b 6e 65 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 74  nection associat
1a13c 65 64 20 77 69 74 68 20 74 68 69 73 20 6c 6f 67  ed with this log
1a13d 20 66 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c   file is the onl
1a13e 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 0a  y connection to.
1a13f 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62      ** the datab
1a140 61 73 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ase. In this cas
1a141 65 20 63 68 65 63 6b 70 6f 69 6e 74 20 74 68 65  e checkpoint the
1a142 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 75 6e   database and un
1a143 6c 69 6e 6b 20 62 6f 74 68 0a 20 20 20 20 2a 2a  link both.    **
1a144 20 74 68 65 20 77 61 6c 20 61 6e 64 20 77 61 6c   the wal and wal
1a145 2d 69 6e 64 65 78 20 66 69 6c 65 73 2e 0a 20 20  -index files..  
1a146 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
1a147 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69  EXCLUSIVE lock i
1a148 73 20 6e 6f 74 20 72 65 6c 65 61 73 65 64 20 62  s not released b
1a149 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
1a14a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
1a14b 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70   sqlite3OsLock(p
1a14c 57 61 6c 2d 3e 70 44 62 46 64 2c 20 53 51 4c 49  Wal->pDbFd, SQLI
1a14d 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56  TE_LOCK_EXCLUSIV
1a14e 45 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  E);.    if( rc==
1a14f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1a150 20 20 20 69 66 28 20 70 57 61 6c 2d 3e 65 78 63     if( pWal->exc
1a151 6c 75 73 69 76 65 4d 6f 64 65 3d 3d 57 41 4c 5f  lusiveMode==WAL_
1a152 4e 4f 52 4d 41 4c 5f 4d 4f 44 45 20 29 7b 0a 20  NORMAL_MODE ){. 
1a153 20 20 20 20 20 20 20 70 57 61 6c 2d 3e 65 78 63         pWal->exc
1a154 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 57 41 4c  lusiveMode = WAL
1a155 5f 45 58 43 4c 55 53 49 56 45 5f 4d 4f 44 45 3b  _EXCLUSIVE_MODE;
1a156 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
1a157 63 20 3d 20 73 71 6c 69 74 65 33 57 61 6c 43 68  c = sqlite3WalCh
1a158 65 63 6b 70 6f 69 6e 74 28 0a 20 20 20 20 20 20  eckpoint(.      
1a159 20 20 20 20 70 57 61 6c 2c 20 53 51 4c 49 54 45      pWal, SQLITE
1a15a 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 50 41 53 53  _CHECKPOINT_PASS
1a15b 49 56 45 2c 20 30 2c 20 30 2c 20 73 79 6e 63 5f  IVE, 0, 0, sync_
1a15c 66 6c 61 67 73 2c 20 6e 42 75 66 2c 20 7a 42 75  flags, nBuf, zBu
1a15d 66 2c 20 30 2c 20 30 0a 20 20 20 20 20 20 29 3b  f, 0, 0.      );
1a15e 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
1a15f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1a160 20 20 20 20 69 6e 74 20 62 50 65 72 73 69 73 74      int bPersist
1a161 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 73   = -1;.        s
1a162 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74  qlite3OsFileCont
1a163 72 6f 6c 48 69 6e 74 28 0a 20 20 20 20 20 20 20  rolHint(.       
1a164 20 20 20 20 20 70 57 61 6c 2d 3e 70 44 62 46 64       pWal->pDbFd
1a165 2c 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50  , SQLITE_FCNTL_P
1a166 45 52 53 49 53 54 5f 57 41 4c 2c 20 26 62 50 65  ERSIST_WAL, &bPe
1a167 72 73 69 73 74 0a 20 20 20 20 20 20 20 20 29 3b  rsist.        );
1a168 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 50 65  .        if( bPe
1a169 72 73 69 73 74 21 3d 31 20 29 7b 0a 20 20 20 20  rsist!=1 ){.    
1a16a 20 20 20 20 20 20 2f 2a 20 54 72 79 20 74 6f 20        /* Try to 
1a16b 64 65 6c 65 74 65 20 74 68 65 20 57 41 4c 20 66  delete the WAL f
1a16c 69 6c 65 20 69 66 20 74 68 65 20 63 68 65 63 6b  ile if the check
1a16d 70 6f 69 6e 74 20 63 6f 6d 70 6c 65 74 65 64 20  point completed 
1a16e 61 6e 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  and.          **
1a16f 20 66 73 79 6e 65 64 20 28 72 63 3d 3d 53 51 4c   fsyned (rc==SQL
1a170 49 54 45 5f 4f 4b 29 20 61 6e 64 20 69 66 20 77  ITE_OK) and if w
1a171 65 20 61 72 65 20 6e 6f 74 20 69 6e 20 70 65 72  e are not in per
1a172 73 69 73 74 65 6e 74 2d 77 61 6c 0a 20 20 20 20  sistent-wal.    
1a173 20 20 20 20 20 20 2a 2a 20 6d 6f 64 65 20 28 21        ** mode (!
1a174 62 50 65 72 73 69 73 74 29 20 2a 2f 0a 20 20 20  bPersist) */.   
1a175 20 20 20 20 20 20 20 69 73 44 65 6c 65 74 65 20         isDelete 
1a176 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  = 1;.        }el
1a177 73 65 20 69 66 28 20 70 57 61 6c 2d 3e 6d 78 57  se if( pWal->mxW
1a178 61 6c 53 69 7a 65 3e 3d 30 20 29 7b 0a 20 20 20  alSize>=0 ){.   
1a179 20 20 20 20 20 20 20 2f 2a 20 54 72 79 20 74 6f         /* Try to
1a17a 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 57 41   truncate the WA
1a17b 4c 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62  L file to zero b
1a17c 79 74 65 73 20 69 66 20 74 68 65 20 63 68 65 63  ytes if the chec
1a17d 6b 70 6f 69 6e 74 0a 20 20 20 20 20 20 20 20 20  kpoint.         
1a17e 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 20 61 6e   ** completed an
1a17f 64 20 66 73 79 6e 63 65 64 20 28 72 63 3d 3d 53  d fsynced (rc==S
1a180 51 4c 49 54 45 5f 4f 4b 29 20 61 6e 64 20 77 65  QLITE_OK) and we
1a181 20 61 72 65 20 69 6e 20 70 65 72 73 69 73 74 65   are in persiste
1a182 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  nt.          ** 
1a183 57 41 4c 20 6d 6f 64 65 20 28 62 50 65 72 73 69  WAL mode (bPersi
1a184 73 74 29 20 61 6e 64 20 69 66 20 74 68 65 20 50  st) and if the P
1a185 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69  RAGMA journal_si
1a186 7a 65 5f 6c 69 6d 69 74 20 69 73 20 61 0a 20 20  ze_limit is a.  
1a187 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f 6e 2d 6e          ** non-n
1a188 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 28 70  egative value (p
1a189 57 61 6c 2d 3e 6d 78 57 61 6c 53 69 7a 65 3e 3d  Wal->mxWalSize>=
1a18a 30 29 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 77  0).  Note that w
1a18b 65 20 74 72 75 6e 63 61 74 65 0a 20 20 20 20 20  e truncate.     
1a18c 20 20 20 20 20 2a 2a 20 74 6f 20 7a 65 72 6f 20       ** to zero 
1a18d 62 79 74 65 73 20 61 73 20 74 72 75 6e 63 61 74  bytes as truncat
1a18e 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ing to the journ
1a18f 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 20 6d 69  al_size_limit mi
1a190 67 68 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ght.          **
1a191 20 6c 65 61 76 65 20 61 20 63 6f 72 72 75 70 74   leave a corrupt
1a192 20 57 41 4c 20 66 69 6c 65 20 6f 6e 20 64 69 73   WAL file on dis
1a193 6b 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  k. */.          
1a194 77 61 6c 4c 69 6d 69 74 53 69 7a 65 28 70 57 61  walLimitSize(pWa
1a195 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d  l, 0);.        }
1a196 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
1a197 20 20 20 20 77 61 6c 49 6e 64 65 78 43 6c 6f 73      walIndexClos
1a198 65 28 70 57 61 6c 2c 20 69 73 44 65 6c 65 74 65  e(pWal, isDelete
1a199 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  );.    sqlite3Os
1a19a 43 6c 6f 73 65 28 70 57 61 6c 2d 3e 70 57 61 6c  Close(pWal->pWal
1a19b 46 64 29 3b 0a 20 20 20 20 69 66 28 20 69 73 44  Fd);.    if( isD
1a19c 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20 73  elete ){.      s
1a19d 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
1a19e 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20  nMalloc();.     
1a19f 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
1a1a0 28 70 57 61 6c 2d 3e 70 56 66 73 2c 20 70 57 61  (pWal->pVfs, pWa
1a1a1 6c 2d 3e 7a 57 61 6c 4e 61 6d 65 2c 20 30 29 3b  l->zWalName, 0);
1a1a2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e  .      sqlite3En
1a1a3 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
1a1a4 0a 20 20 20 20 7d 0a 20 20 20 20 57 41 4c 54 52  .    }.    WALTR
1a1a5 41 43 45 28 28 22 57 41 4c 25 70 3a 20 63 6c 6f  ACE(("WAL%p: clo
1a1a6 73 65 64 5c 6e 22 2c 20 70 57 61 6c 29 29 3b 0a  sed\n", pWal));.
1a1a7 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1a1a8 28 28 76 6f 69 64 20 2a 29 70 57 61 6c 2d 3e 61  ((void *)pWal->a
1a1a9 70 57 69 44 61 74 61 29 3b 0a 20 20 20 20 73 71  pWiData);.    sq
1a1aa 6c 69 74 65 33 5f 66 72 65 65 28 70 57 61 6c 29  lite3_free(pWal)
1a1ab 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1a1ac 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20  c;.}../*.** Try 
1a1ad 74 6f 20 72 65 61 64 20 74 68 65 20 77 61 6c 2d  to read the wal-
1a1ae 69 6e 64 65 78 20 68 65 61 64 65 72 2e 20 20 52  index header.  R
1a1af 65 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65  eturn 0 on succe
1a1b0 73 73 20 61 6e 64 20 31 20 69 66 0a 2a 2a 20 74  ss and 1 if.** t
1a1b1 68 65 72 65 20 69 73 20 61 20 70 72 6f 62 6c 65  here is a proble
1a1b2 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 61 6c  m..**.** The wal
1a1b3 2d 69 6e 64 65 78 20 69 73 20 69 6e 20 73 68 61  -index is in sha
1a1b4 72 65 64 20 6d 65 6d 6f 72 79 2e 20 20 41 6e 6f  red memory.  Ano
1a1b5 74 68 65 72 20 74 68 72 65 61 64 20 6f 72 20 70  ther thread or p
1a1b6 72 6f 63 65 73 73 20 6d 69 67 68 74 0a 2a 2a 20  rocess might.** 
1a1b7 62 65 20 77 72 69 74 69 6e 67 20 74 68 65 20 68  be writing the h
1a1b8 65 61 64 65 72 20 61 74 20 74 68 65 20 73 61 6d  eader at the sam
1a1b9 65 20 74 69 6d 65 20 74 68 69 73 20 70 72 6f 63  e time this proc
1a1ba 65 64 75 72 65 20 69 73 20 74 72 79 69 6e 67 20  edure is trying 
1a1bb 74 6f 0a 2a 2a 20 72 65 61 64 20 69 74 2c 20 77  to.** read it, w
1a1bc 68 69 63 68 20 6d 69 67 68 74 20 72 65 73 75 6c  hich might resul
1a1bd 74 20 69 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e  t in inconsisten
1a1be 63 79 2e 20 20 41 20 64 69 72 74 79 20 72 65 61  cy.  A dirty rea
1a1bf 64 20 69 73 20 64 65 74 65 63 74 65 64 0a 2a 2a  d is detected.**
1a1c0 20 62 79 20 76 65 72 69 66 79 69 6e 67 20 74 68   by verifying th
1a1c1 61 74 20 62 6f 74 68 20 63 6f 70 69 65 73 20 6f  at both copies o
1a1c2 66 20 74 68 65 20 68 65 61 64 65 72 20 61 72 65  f the header are
1a1c3 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20 61 6c   the same and al
1a1c4 73 6f 20 62 79 0a 2a 2a 20 61 20 63 68 65 63 6b  so by.** a check
1a1c5 73 75 6d 20 6f 6e 20 74 68 65 20 68 65 61 64 65  sum on the heade
1a1c6 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 64 20  r..**.** If and 
1a1c7 6f 6e 6c 79 20 69 66 20 74 68 65 20 72 65 61 64  only if the read
1a1c8 20 69 73 20 63 6f 6e 73 69 73 74 65 6e 74 20 61   is consistent a
1a1c9 6e 64 20 74 68 65 20 68 65 61 64 65 72 20 69 73  nd the header is
1a1ca 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a   different from.
1a1cb 2a 2a 20 70 57 61 6c 2d 3e 68 64 72 2c 20 74 68  ** pWal->hdr, th
1a1cc 65 6e 20 70 57 61 6c 2d 3e 68 64 72 20 69 73 20  en pWal->hdr is 
1a1cd 75 70 64 61 74 65 64 20 74 6f 20 74 68 65 20 63  updated to the c
1a1ce 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65  ontent of the ne
1a1cf 77 20 68 65 61 64 65 72 0a 2a 2a 20 61 6e 64 20  w header.** and 
1a1d0 2a 70 43 68 61 6e 67 65 64 20 69 73 20 73 65 74  *pChanged is set
1a1d1 20 74 6f 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   to 1..**.** If 
1a1d2 74 68 65 20 63 68 65 63 6b 73 75 6d 20 63 61 6e  the checksum can
1a1d3 6e 6f 74 20 62 65 20 76 65 72 69 66 69 65 64 20  not be verified 
1a1d4 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e  return non-zero.
1a1d5 20 49 66 20 74 68 65 20 68 65 61 64 65 72 0a 2a   If the header.*
1a1d6 2a 20 69 73 20 72 65 61 64 20 73 75 63 63 65 73  * is read succes
1a1d7 73 66 75 6c 6c 79 20 61 6e 64 20 74 68 65 20 63  sfully and the c
1a1d8 68 65 63 6b 73 75 6d 20 76 65 72 69 66 69 65 64  hecksum verified
1a1d9 2c 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e 0a 2a  , return zero..*
1a1da 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 61 6c  /.static int wal
1a1db 49 6e 64 65 78 54 72 79 48 64 72 28 57 61 6c 20  IndexTryHdr(Wal 
1a1dc 2a 70 57 61 6c 2c 20 69 6e 74 20 2a 70 43 68 61  *pWal, int *pCha
1a1dd 6e 67 65 64 29 7b 0a 20 20 75 33 32 20 61 43 6b  nged){.  u32 aCk
1a1de 73 75 6d 5b 32 5d 3b 20 20 20 20 20 20 20 20 20  sum[2];         
1a1df 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63           /* Chec
1a1e0 6b 73 75 6d 20 6f 6e 20 74 68 65 20 68 65 61 64  ksum on the head
1a1e1 65 72 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20  er content */.  
1a1e2 57 61 6c 49 6e 64 65 78 48 64 72 20 68 31 2c 20  WalIndexHdr h1, 
1a1e3 68 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  h2;             
1a1e4 2f 2a 20 54 77 6f 20 63 6f 70 69 65 73 20 6f 66  /* Two copies of
1a1e5 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6e 74   the header cont
1a1e6 65 6e 74 20 2a 2f 0a 20 20 57 61 6c 49 6e 64 65  ent */.  WalInde
1a1e7 78 48 64 72 20 76 6f 6c 61 74 69 6c 65 20 2a 61  xHdr volatile *a
1a1e8 48 64 72 3b 20 20 20 20 20 2f 2a 20 48 65 61 64  Hdr;     /* Head
1a1e9 65 72 20 69 6e 20 73 68 61 72 65 64 20 6d 65 6d  er in shared mem
1a1ea 6f 72 79 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65  ory */..  /* The
1a1eb 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74   first page of t
1a1ec 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 6d 75 73  he wal-index mus
1a1ed 74 20 62 65 20 6d 61 70 70 65 64 20 61 74 20 74  t be mapped at t
1a1ee 68 69 73 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20  his point. */.  
1a1ef 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 6e 57  assert( pWal->nW
1a1f0 69 44 61 74 61 3e 30 20 26 26 20 70 57 61 6c 2d  iData>0 && pWal-
1a1f1 3e 61 70 57 69 44 61 74 61 5b 30 5d 20 29 3b 0a  >apWiData[0] );.
1a1f2 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 68  .  /* Read the h
1a1f3 65 61 64 65 72 2e 20 54 68 69 73 20 6d 69 67 68  eader. This migh
1a1f4 74 20 68 61 70 70 65 6e 20 63 6f 6e 63 75 72 72  t happen concurr
1a1f5 65 6e 74 6c 79 20 77 69 74 68 20 61 20 77 72 69  ently with a wri
1a1f6 74 65 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 73  te to the.  ** s
1a1f7 61 6d 65 20 61 72 65 61 20 6f 66 20 73 68 61 72  ame area of shar
1a1f8 65 64 20 6d 65 6d 6f 72 79 20 6f 6e 20 61 20 64  ed memory on a d
1a1f9 69 66 66 65 72 65 6e 74 20 43 50 55 20 69 6e 20  ifferent CPU in 
1a1fa 61 20 53 4d 50 2c 0a 20 20 2a 2a 20 6d 65 61 6e  a SMP,.  ** mean
1a1fb 69 6e 67 20 69 74 20 69 73 20 70 6f 73 73 69 62  ing it is possib
1a1fc 6c 65 20 74 68 61 74 20 61 6e 20 69 6e 63 6f 6e  le that an incon
1a1fd 73 69 73 74 65 6e 74 20 73 6e 61 70 73 68 6f 74  sistent snapshot
1a1fe 20 69 73 20 72 65 61 64 0a 20 20 2a 2a 20 66 72   is read.  ** fr
1a1ff 6f 6d 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20  om the file. If 
1a200 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20 72 65  this happens, re
1a201 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 20  turn non-zero.. 
1a202 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 72 65 20 61   **.  ** There a
1a203 72 65 20 74 77 6f 20 63 6f 70 69 65 73 20 6f 66  re two copies of
1a204 20 74 68 65 20 68 65 61 64 65 72 20 61 74 20 74   the header at t
1a205 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
1a206 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 2e 0a 20  the wal-index.. 
1a207 20 2a 2a 20 57 68 65 6e 20 72 65 61 64 69 6e 67   ** When reading
1a208 2c 20 72 65 61 64 20 5b 30 5d 20 66 69 72 73 74  , read [0] first
1a209 20 74 68 65 6e 20 5b 31 5d 2e 20 20 57 72 69 74   then [1].  Writ
1a20a 65 73 20 61 72 65 20 69 6e 20 74 68 65 20 72 65  es are in the re
1a20b 76 65 72 73 65 20 6f 72 64 65 72 2e 0a 20 20 2a  verse order..  *
1a20c 2a 20 4d 65 6d 6f 72 79 20 62 61 72 72 69 65 72  * Memory barrier
1a20d 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 70 72  s are used to pr
1a20e 65 76 65 6e 74 20 74 68 65 20 63 6f 6d 70 69 6c  event the compil
1a20f 65 72 20 6f 72 20 74 68 65 20 68 61 72 64 77 61  er or the hardwa
1a210 72 65 20 66 72 6f 6d 0a 20 20 2a 2a 20 72 65 6f  re from.  ** reo
1a211 72 64 65 72 69 6e 67 20 74 68 65 20 72 65 61 64  rdering the read
1a212 73 20 61 6e 64 20 77 72 69 74 65 73 2e 0a 20 20  s and writes..  
1a213 2a 2f 0a 20 20 61 48 64 72 20 3d 20 77 61 6c 49  */.  aHdr = walI
1a214 6e 64 65 78 48 64 72 28 70 57 61 6c 29 3b 0a 20  ndexHdr(pWal);. 
1a215 20 6d 65 6d 63 70 79 28 26 68 31 2c 20 28 76 6f   memcpy(&h1, (vo
1a216 69 64 20 2a 29 26 61 48 64 72 5b 30 5d 2c 20 73  id *)&aHdr[0], s
1a217 69 7a 65 6f 66 28 68 31 29 29 3b 0a 20 20 77 61  izeof(h1));.  wa
1a218 6c 53 68 6d 42 61 72 72 69 65 72 28 70 57 61 6c  lShmBarrier(pWal
1a219 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 68 32 2c  );.  memcpy(&h2,
1a21a 20 28 76 6f 69 64 20 2a 29 26 61 48 64 72 5b 31   (void *)&aHdr[1
1a21b 5d 2c 20 73 69 7a 65 6f 66 28 68 32 29 29 3b 0a  ], sizeof(h2));.
1a21c 0a 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 26 68  .  if( memcmp(&h
1a21d 31 2c 20 26 68 32 2c 20 73 69 7a 65 6f 66 28 68  1, &h2, sizeof(h
1a21e 31 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 65  1))!=0 ){.    re
1a21f 74 75 72 6e 20 31 3b 20 20 20 2f 2a 20 44 69 72  turn 1;   /* Dir
1a220 74 79 20 72 65 61 64 20 2a 2f 0a 20 20 7d 20 20  ty read */.  }  
1a221 0a 20 20 69 66 28 20 68 31 2e 69 73 49 6e 69 74  .  if( h1.isInit
1a222 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
1a223 6e 20 31 3b 20 20 20 2f 2a 20 4d 61 6c 66 6f 72  n 1;   /* Malfor
1a224 6d 65 64 20 68 65 61 64 65 72 20 2d 20 70 72 6f  med header - pro
1a225 62 61 62 6c 79 20 61 6c 6c 20 7a 65 72 6f 73 20  bably all zeros 
1a226 2a 2f 0a 20 20 7d 0a 20 20 77 61 6c 43 68 65 63  */.  }.  walChec
1a227 6b 73 75 6d 42 79 74 65 73 28 31 2c 20 28 75 38  ksumBytes(1, (u8
1a228 2a 29 26 68 31 2c 20 73 69 7a 65 6f 66 28 68 31  *)&h1, sizeof(h1
1a229 29 2d 73 69 7a 65 6f 66 28 68 31 2e 61 43 6b 73  )-sizeof(h1.aCks
1a22a 75 6d 29 2c 20 30 2c 20 61 43 6b 73 75 6d 29 3b  um), 0, aCksum);
1a22b 0a 20 20 69 66 28 20 61 43 6b 73 75 6d 5b 30 5d  .  if( aCksum[0]
1a22c 21 3d 68 31 2e 61 43 6b 73 75 6d 5b 30 5d 20 7c  !=h1.aCksum[0] |
1a22d 7c 20 61 43 6b 73 75 6d 5b 31 5d 21 3d 68 31 2e  | aCksum[1]!=h1.
1a22e 61 43 6b 73 75 6d 5b 31 5d 20 29 7b 0a 20 20 20  aCksum[1] ){.   
1a22f 20 72 65 74 75 72 6e 20 31 3b 20 20 20 2f 2a 20   return 1;   /* 
1a230 43 68 65 63 6b 73 75 6d 20 64 6f 65 73 20 6e 6f  Checksum does no
1a231 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 7d 0a 0a  t match */.  }..
1a232 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 26 70 57    if( memcmp(&pW
1a233 61 6c 2d 3e 68 64 72 2c 20 26 68 31 2c 20 73 69  al->hdr, &h1, si
1a234 7a 65 6f 66 28 57 61 6c 49 6e 64 65 78 48 64 72  zeof(WalIndexHdr
1a235 29 29 20 29 7b 0a 20 20 20 20 2a 70 43 68 61 6e  )) ){.    *pChan
1a236 67 65 64 20 3d 20 31 3b 0a 20 20 20 20 6d 65 6d  ged = 1;.    mem
1a237 63 70 79 28 26 70 57 61 6c 2d 3e 68 64 72 2c 20  cpy(&pWal->hdr, 
1a238 26 68 31 2c 20 73 69 7a 65 6f 66 28 57 61 6c 49  &h1, sizeof(WalI
1a239 6e 64 65 78 48 64 72 29 29 3b 0a 20 20 20 20 70  ndexHdr));.    p
1a23a 57 61 6c 2d 3e 73 7a 50 61 67 65 20 3d 20 28 70  Wal->szPage = (p
1a23b 57 61 6c 2d 3e 68 64 72 2e 73 7a 50 61 67 65 26  Wal->hdr.szPage&
1a23c 30 78 66 65 30 30 29 20 2b 20 28 28 70 57 61 6c  0xfe00) + ((pWal
1a23d 2d 3e 68 64 72 2e 73 7a 50 61 67 65 26 30 78 30  ->hdr.szPage&0x0
1a23e 30 30 31 29 3c 3c 31 36 29 3b 0a 20 20 20 20 74  001)<<16);.    t
1a23f 65 73 74 63 61 73 65 28 20 70 57 61 6c 2d 3e 73  estcase( pWal->s
1a240 7a 50 61 67 65 3c 3d 33 32 37 36 38 20 29 3b 0a  zPage<=32768 );.
1a241 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 57      testcase( pW
1a242 61 6c 2d 3e 73 7a 50 61 67 65 3e 3d 36 35 35 33  al->szPage>=6553
1a243 36 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  6 );.  }..  /* T
1a244 68 65 20 68 65 61 64 65 72 20 77 61 73 20 73 75  he header was su
1a245 63 63 65 73 73 66 75 6c 6c 79 20 72 65 61 64 2e  ccessfully read.
1a246 20 52 65 74 75 72 6e 20 7a 65 72 6f 2e 20 2a 2f   Return zero. */
1a247 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
1a248 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 77  /*.** Read the w
1a249 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20  al-index header 
1a24a 66 72 6f 6d 20 74 68 65 20 77 61 6c 2d 69 6e 64  from the wal-ind
1a24b 65 78 20 61 6e 64 20 69 6e 74 6f 20 70 57 61 6c  ex and into pWal
1a24c 2d 3e 68 64 72 2e 0a 2a 2a 20 49 66 20 74 68 65  ->hdr..** If the
1a24d 20 77 61 6c 2d 68 65 61 64 65 72 20 61 70 70 65   wal-header appe
1a24e 61 72 73 20 74 6f 20 62 65 20 63 6f 72 72 75 70  ars to be corrup
1a24f 74 2c 20 74 72 79 20 74 6f 20 72 65 63 6f 6e 73  t, try to recons
1a250 74 72 75 63 74 20 74 68 65 0a 2a 2a 20 77 61 6c  truct the.** wal
1a251 2d 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20  -index from the 
1a252 57 41 4c 20 62 65 66 6f 72 65 20 72 65 74 75 72  WAL before retur
1a253 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 20  ning..**.** Set 
1a254 2a 70 43 68 61 6e 67 65 64 20 74 6f 20 31 20 69  *pChanged to 1 i
1a255 66 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20  f the wal-index 
1a256 68 65 61 64 65 72 20 76 61 6c 75 65 20 69 6e 20  header value in 
1a257 70 57 61 6c 2d 3e 68 64 72 20 69 73 0a 2a 2a 20  pWal->hdr is.** 
1a258 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
1a259 6f 70 65 72 74 69 6f 6e 2e 20 20 49 66 20 70 57  opertion.  If pW
1a25a 61 6c 2d 3e 68 64 72 20 69 73 20 75 6e 63 68 61  al->hdr is uncha
1a25b 6e 67 65 64 2c 20 73 65 74 20 2a 70 43 68 61 6e  nged, set *pChan
1a25c 67 65 64 0a 2a 2a 20 74 6f 20 30 2e 0a 2a 2a 0a  ged.** to 0..**.
1a25d 2a 2a 20 49 66 20 74 68 65 20 77 61 6c 2d 69 6e  ** If the wal-in
1a25e 64 65 78 20 68 65 61 64 65 72 20 69 73 20 73 75  dex header is su
1a25f 63 63 65 73 73 66 75 6c 6c 79 20 72 65 61 64 2c  ccessfully read,
1a260 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1a261 4b 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  K. .** Otherwise
1a262 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
1a263 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   code..*/.static
1a264 20 69 6e 74 20 77 61 6c 49 6e 64 65 78 52 65 61   int walIndexRea
1a265 64 48 64 72 28 57 61 6c 20 2a 70 57 61 6c 2c 20  dHdr(Wal *pWal, 
1a266 69 6e 74 20 2a 70 43 68 61 6e 67 65 64 29 7b 0a  int *pChanged){.
1a267 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
1a268 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a269 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
1a26a 20 2a 2f 0a 20 20 69 6e 74 20 62 61 64 48 64 72   */.  int badHdr
1a26b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1a26c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
1a26d 20 61 20 68 65 61 64 65 72 20 72 65 61 64 20 66   a header read f
1a26e 61 69 6c 65 64 20 2a 2f 0a 20 20 76 6f 6c 61 74  ailed */.  volat
1a26f 69 6c 65 20 75 33 32 20 2a 70 61 67 65 30 3b 20  ile u32 *page0; 
1a270 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68             /* Ch
1a271 75 6e 6b 20 6f 66 20 77 61 6c 2d 69 6e 64 65 78  unk of wal-index
1a272 20 63 6f 6e 74 61 69 6e 69 6e 67 20 68 65 61 64   containing head
1a273 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 45 6e 73 75  er */..  /* Ensu
1a274 72 65 20 74 68 61 74 20 70 61 67 65 20 30 20 6f  re that page 0 o
1a275 66 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20  f the wal-index 
1a276 28 74 68 65 20 70 61 67 65 20 74 68 61 74 20 63  (the page that c
1a277 6f 6e 74 61 69 6e 73 20 74 68 65 20 0a 20 20 2a  ontains the .  *
1a278 2a 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64  * wal-index head
1a279 65 72 29 20 69 73 20 6d 61 70 70 65 64 2e 20 52  er) is mapped. R
1a27a 65 74 75 72 6e 20 65 61 72 6c 79 20 69 66 20 61  eturn early if a
1a27b 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 68  n error occurs h
1a27c 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ere..  */.  asse
1a27d 72 74 28 20 70 43 68 61 6e 67 65 64 20 29 3b 0a  rt( pChanged );.
1a27e 20 20 72 63 20 3d 20 77 61 6c 49 6e 64 65 78 50    rc = walIndexP
1a27f 61 67 65 28 70 57 61 6c 2c 20 30 2c 20 26 70 61  age(pWal, 0, &pa
1a280 67 65 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ge0);.  if( rc!=
1a281 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1a282 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 3b   return rc;.  };
1a283 0a 20 20 61 73 73 65 72 74 28 20 70 61 67 65 30  .  assert( page0
1a284 20 7c 7c 20 70 57 61 6c 2d 3e 77 72 69 74 65 4c   || pWal->writeL
1a285 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  ock==0 );..  /* 
1a286 49 66 20 74 68 65 20 66 69 72 73 74 20 70 61 67  If the first pag
1a287 65 20 6f 66 20 74 68 65 20 77 61 6c 2d 69 6e 64  e of the wal-ind
1a288 65 78 20 68 61 73 20 62 65 65 6e 20 6d 61 70 70  ex has been mapp
1a289 65 64 2c 20 74 72 79 20 74 6f 20 72 65 61 64 20  ed, try to read 
1a28a 74 68 65 0a 20 20 2a 2a 20 77 61 6c 2d 69 6e 64  the.  ** wal-ind
1a28b 65 78 20 68 65 61 64 65 72 20 69 6d 6d 65 64 69  ex header immedi
1a28c 61 74 65 6c 79 2c 20 77 69 74 68 6f 75 74 20 68  ately, without h
1a28d 6f 6c 64 69 6e 67 20 61 6e 79 20 6c 6f 63 6b 2e  olding any lock.
1a28e 20 54 68 69 73 20 75 73 75 61 6c 6c 79 0a 20 20   This usually.  
1a28f 2a 2a 20 77 6f 72 6b 73 2c 20 62 75 74 20 6d 61  ** works, but ma
1a290 79 20 66 61 69 6c 20 69 66 20 74 68 65 20 77 61  y fail if the wa
1a291 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20 69  l-index header i
1a292 73 20 63 6f 72 72 75 70 74 20 6f 72 20 63 75 72  s corrupt or cur
1a293 72 65 6e 74 6c 79 20 0a 20 20 2a 2a 20 62 65 69  rently .  ** bei
1a294 6e 67 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61  ng modified by a
1a295 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 6f 72  nother thread or
1a296 20 70 72 6f 63 65 73 73 2e 0a 20 20 2a 2f 0a 20   process..  */. 
1a297 20 62 61 64 48 64 72 20 3d 20 28 70 61 67 65 30   badHdr = (page0
1a298 20 3f 20 77 61 6c 49 6e 64 65 78 54 72 79 48 64   ? walIndexTryHd
1a299 72 28 70 57 61 6c 2c 20 70 43 68 61 6e 67 65 64  r(pWal, pChanged
1a29a 29 20 3a 20 31 29 3b 0a 0a 20 20 2f 2a 20 49 66  ) : 1);..  /* If
1a29b 20 74 68 65 20 66 69 72 73 74 20 61 74 74 65 6d   the first attem
1a29c 70 74 20 66 61 69 6c 65 64 2c 20 69 74 20 6d 69  pt failed, it mi
1a29d 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 64 75  ght have been du
1a29e 65 20 74 6f 20 61 20 72 61 63 65 0a 20 20 2a 2a  e to a race.  **
1a29f 20 77 69 74 68 20 61 20 77 72 69 74 65 72 2e 20   with a writer. 
1a2a0 20 53 6f 20 67 65 74 20 61 20 57 52 49 54 45 20   So get a WRITE 
1a2a1 6c 6f 63 6b 20 61 6e 64 20 74 72 79 20 61 67 61  lock and try aga
1a2a2 69 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  in..  */.  asser
1a2a3 74 28 20 62 61 64 48 64 72 3d 3d 30 20 7c 7c 20  t( badHdr==0 || 
1a2a4 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 3d  pWal->writeLock=
1a2a5 3d 30 20 29 3b 0a 20 20 69 66 28 20 62 61 64 48  =0 );.  if( badH
1a2a6 64 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 57  dr ){.    if( pW
1a2a7 61 6c 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 20 57  al->readOnly & W
1a2a8 41 4c 5f 53 48 4d 5f 52 44 4f 4e 4c 59 20 29 7b  AL_SHM_RDONLY ){
1a2a9 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
1a2aa 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 77 61 6c 4c  E_OK==(rc = walL
1a2ab 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c 2c 20  ockShared(pWal, 
1a2ac 57 41 4c 5f 57 52 49 54 45 5f 4c 4f 43 4b 29 29  WAL_WRITE_LOCK))
1a2ad 20 29 7b 0a 20 20 20 20 20 20 20 20 77 61 6c 55   ){.        walU
1a2ae 6e 6c 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c  nlockShared(pWal
1a2af 2c 20 57 41 4c 5f 57 52 49 54 45 5f 4c 4f 43 4b  , WAL_WRITE_LOCK
1a2b0 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
1a2b1 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5f  SQLITE_READONLY_
1a2b2 52 45 43 4f 56 45 52 59 3b 0a 20 20 20 20 20 20  RECOVERY;.      
1a2b3 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  }.    }else if( 
1a2b4 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
1a2b5 20 77 61 6c 4c 6f 63 6b 45 78 63 6c 75 73 69 76   walLockExclusiv
1a2b6 65 28 70 57 61 6c 2c 20 57 41 4c 5f 57 52 49 54  e(pWal, WAL_WRIT
1a2b7 45 5f 4c 4f 43 4b 2c 20 31 29 29 20 29 7b 0a 20  E_LOCK, 1)) ){. 
1a2b8 20 20 20 20 20 70 57 61 6c 2d 3e 77 72 69 74 65       pWal->write
1a2b9 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20  Lock = 1;.      
1a2ba 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28  if( SQLITE_OK==(
1a2bb 72 63 20 3d 20 77 61 6c 49 6e 64 65 78 50 61 67  rc = walIndexPag
1a2bc 65 28 70 57 61 6c 2c 20 30 2c 20 26 70 61 67 65  e(pWal, 0, &page
1a2bd 30 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62  0)) ){.        b
1a2be 61 64 48 64 72 20 3d 20 77 61 6c 49 6e 64 65 78  adHdr = walIndex
1a2bf 54 72 79 48 64 72 28 70 57 61 6c 2c 20 70 43 68  TryHdr(pWal, pCh
1a2c0 61 6e 67 65 64 29 3b 0a 20 20 20 20 20 20 20 20  anged);.        
1a2c1 69 66 28 20 62 61 64 48 64 72 20 29 7b 0a 20 20  if( badHdr ){.  
1a2c2 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
1a2c3 65 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64  e wal-index head
1a2c4 65 72 20 69 73 20 73 74 69 6c 6c 20 6d 61 6c 66  er is still malf
1a2c5 6f 72 6d 65 64 20 65 76 65 6e 20 77 68 69 6c 65  ormed even while
1a2c6 20 68 6f 6c 64 69 6e 67 0a 20 20 20 20 20 20 20   holding.       
1a2c7 20 20 20 2a 2a 20 61 20 57 52 49 54 45 20 6c 6f     ** a WRITE lo
1a2c8 63 6b 2c 20 69 74 20 63 61 6e 20 6f 6e 6c 79 20  ck, it can only 
1a2c9 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 68 65  mean that the he
1a2ca 61 64 65 72 20 69 73 20 63 6f 72 72 75 70 74 65  ader is corrupte
1a2cb 64 20 61 6e 64 0a 20 20 20 20 20 20 20 20 20 20  d and.          
1a2cc 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  ** needs to be r
1a2cd 65 63 6f 6e 73 74 72 75 63 74 65 64 2e 20 20 53  econstructed.  S
1a2ce 6f 20 72 75 6e 20 72 65 63 6f 76 65 72 79 20 74  o run recovery t
1a2cf 6f 20 64 6f 20 65 78 61 63 74 6c 79 20 74 68 61  o do exactly tha
1a2d0 74 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  t..          */.
1a2d1 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 77            rc = w
1a2d2 61 6c 49 6e 64 65 78 52 65 63 6f 76 65 72 28 70  alIndexRecover(p
1a2d3 57 61 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Wal);.          
1a2d4 2a 70 43 68 61 6e 67 65 64 20 3d 20 31 3b 0a 20  *pChanged = 1;. 
1a2d5 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1a2d6 0a 20 20 20 20 20 20 70 57 61 6c 2d 3e 77 72 69  .      pWal->wri
1a2d7 74 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20  teLock = 0;.    
1a2d8 20 20 77 61 6c 55 6e 6c 6f 63 6b 45 78 63 6c 75    walUnlockExclu
1a2d9 73 69 76 65 28 70 57 61 6c 2c 20 57 41 4c 5f 57  sive(pWal, WAL_W
1a2da 52 49 54 45 5f 4c 4f 43 4b 2c 20 31 29 3b 0a 20  RITE_LOCK, 1);. 
1a2db 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
1a2dc 66 20 74 68 65 20 68 65 61 64 65 72 20 69 73 20  f the header is 
1a2dd 72 65 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c  read successfull
1a2de 79 2c 20 63 68 65 63 6b 20 74 68 65 20 76 65 72  y, check the ver
1a2df 73 69 6f 6e 20 6e 75 6d 62 65 72 20 74 6f 20 6d  sion number to m
1a2e0 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20 74 68  ake.  ** sure th
1a2e1 65 20 77 61 6c 2d 69 6e 64 65 78 20 77 61 73 20  e wal-index was 
1a2e2 6e 6f 74 20 63 6f 6e 73 74 72 75 63 74 65 64 20  not constructed 
1a2e3 77 69 74 68 20 73 6f 6d 65 20 66 75 74 75 72 65  with some future
1a2e4 20 66 6f 72 6d 61 74 20 74 68 61 74 0a 20 20 2a   format that.  *
1a2e5 2a 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * this version o
1a2e6 66 20 53 51 4c 69 74 65 20 63 61 6e 6e 6f 74 20  f SQLite cannot 
1a2e7 75 6e 64 65 72 73 74 61 6e 64 2e 0a 20 20 2a 2f  understand..  */
1a2e8 0a 20 20 69 66 28 20 62 61 64 48 64 72 3d 3d 30  .  if( badHdr==0
1a2e9 20 26 26 20 70 57 61 6c 2d 3e 68 64 72 2e 69 56   && pWal->hdr.iV
1a2ea 65 72 73 69 6f 6e 21 3d 57 41 4c 49 4e 44 45 58  ersion!=WALINDEX
1a2eb 5f 4d 41 58 5f 56 45 52 53 49 4f 4e 20 29 7b 0a  _MAX_VERSION ){.
1a2ec 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1a2ed 43 41 4e 54 4f 50 45 4e 5f 42 4b 50 54 3b 0a 20  CANTOPEN_BKPT;. 
1a2ee 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
1a2ef 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  .}../*.** This i
1a2f0 73 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 74  s the value that
1a2f1 20 77 61 6c 54 72 79 42 65 67 69 6e 52 65 61 64   walTryBeginRead
1a2f2 20 72 65 74 75 72 6e 73 20 77 68 65 6e 20 69 74   returns when it
1a2f3 20 6e 65 65 64 73 20 74 6f 0a 2a 2a 20 62 65 20   needs to.** be 
1a2f4 72 65 74 72 69 65 64 2e 0a 2a 2f 0a 23 64 65 66  retried..*/.#def
1a2f5 69 6e 65 20 57 41 4c 5f 52 45 54 52 59 20 20 28  ine WAL_RETRY  (
1a2f6 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  -1)../*.** Attem
1a2f7 70 74 20 74 6f 20 73 74 61 72 74 20 61 20 72 65  pt to start a re
1a2f8 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ad transaction. 
1a2f9 20 54 68 69 73 20 6d 69 67 68 74 20 66 61 69 6c   This might fail
1a2fa 20 64 75 65 20 74 6f 20 61 20 72 61 63 65 20 6f   due to a race o
1a2fb 72 0a 2a 2a 20 6f 74 68 65 72 20 74 72 61 6e 73  r.** other trans
1a2fc 69 65 6e 74 20 63 6f 6e 64 69 74 69 6f 6e 2e 20  ient condition. 
1a2fd 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65   When that happe
1a2fe 6e 73 2c 20 69 74 20 72 65 74 75 72 6e 73 20 57  ns, it returns W
1a2ff 41 4c 5f 52 45 54 52 59 20 74 6f 0a 2a 2a 20 69  AL_RETRY to.** i
1a300 6e 64 69 63 61 74 65 20 74 6f 20 74 68 65 20 63  ndicate to the c
1a301 61 6c 6c 65 72 20 74 68 61 74 20 69 74 20 69 73  aller that it is
1a302 20 73 61 66 65 20 74 6f 20 72 65 74 72 79 20 69   safe to retry i
1a303 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a  mmediately..**.*
1a304 2a 20 4f 6e 20 73 75 63 63 65 73 73 20 72 65 74  * On success ret
1a305 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 20  urn SQLITE_OK.  
1a306 4f 6e 20 61 20 70 65 72 6d 61 6e 65 6e 74 20 66  On a permanent f
1a307 61 69 6c 75 72 65 20 28 73 75 63 68 20 61 6e 0a  ailure (such an.
1a308 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20 6f 72 20  ** I/O error or 
1a309 61 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 62  an SQLITE_BUSY b
1a30a 65 63 61 75 73 65 20 61 6e 6f 74 68 65 72 20 70  ecause another p
1a30b 72 6f 63 65 73 73 20 69 73 20 72 75 6e 6e 69 6e  rocess is runnin
1a30c 67 0a 2a 2a 20 72 65 63 6f 76 65 72 79 29 20 72  g.** recovery) r
1a30d 65 74 75 72 6e 20 61 20 70 6f 73 69 74 69 76 65  eturn a positive
1a30e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a   error code..**.
1a30f 2a 2a 20 54 68 65 20 75 73 65 57 61 6c 20 70 61  ** The useWal pa
1a310 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 20  rameter is true 
1a311 74 6f 20 66 6f 72 63 65 20 74 68 65 20 75 73 65  to force the use
1a312 20 6f 66 20 74 68 65 20 57 41 4c 20 61 6e 64 20   of the WAL and 
1a313 64 69 73 61 62 6c 65 0a 2a 2a 20 74 68 65 20 63  disable.** the c
1a314 61 73 65 20 77 68 65 72 65 20 74 68 65 20 57 41  ase where the WA
1a315 4c 20 69 73 20 62 79 70 61 73 73 65 64 20 62 65  L is bypassed be
1a316 63 61 75 73 65 20 69 74 20 68 61 73 20 62 65 65  cause it has bee
1a317 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20  n completely.** 
1a318 63 68 65 63 6b 70 6f 69 6e 74 65 64 2e 20 20 49  checkpointed.  I
1a319 66 20 75 73 65 57 61 6c 3d 3d 30 20 74 68 65 6e  f useWal==0 then
1a31a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   this routine ca
1a31b 6c 6c 73 20 77 61 6c 49 6e 64 65 78 52 65 61 64  lls walIndexRead
1a31c 48 64 72 28 29 20 0a 2a 2a 20 74 6f 20 6d 61 6b  Hdr() .** to mak
1a31d 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  e a copy of the 
1a31e 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72  wal-index header
1a31f 20 69 6e 74 6f 20 70 57 61 6c 2d 3e 68 64 72 2e   into pWal->hdr.
1a320 20 20 49 66 20 74 68 65 20 0a 2a 2a 20 77 61 6c    If the .** wal
1a321 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20 68 61  -index header ha
1a322 73 20 63 68 61 6e 67 65 64 2c 20 2a 70 43 68 61  s changed, *pCha
1a323 6e 67 65 64 20 69 73 20 73 65 74 20 74 6f 20 31  nged is set to 1
1a324 20 28 61 73 20 61 6e 20 69 6e 64 69 63 61 74 69   (as an indicati
1a325 6f 6e 20 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61  on .** to the ca
1a326 6c 6c 65 72 20 74 68 61 74 20 74 68 65 20 6c 6f  ller that the lo
1a327 63 61 6c 20 70 61 67 65 74 20 63 61 63 68 65 20  cal paget cache 
1a328 69 73 20 6f 62 73 6f 6c 65 74 65 20 61 6e 64 20  is obsolete and 
1a329 6e 65 65 64 73 20 74 6f 20 62 65 20 0a 2a 2a 20  needs to be .** 
1a32a 66 6c 75 73 68 65 64 2e 29 20 20 57 68 65 6e 20  flushed.)  When 
1a32b 75 73 65 57 61 6c 3d 3d 31 2c 20 74 68 65 20 77  useWal==1, the w
1a32c 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20  al-index header 
1a32d 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 61 6c  is assumed to al
1a32e 72 65 61 64 79 0a 2a 2a 20 62 65 20 6c 6f 61 64  ready.** be load
1a32f 65 64 20 61 6e 64 20 74 68 65 20 70 43 68 61 6e  ed and the pChan
1a330 67 65 64 20 70 61 72 61 6d 65 74 65 72 20 69 73  ged parameter is
1a331 20 75 6e 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54   unused..**.** T
1a332 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 73  he caller must s
1a333 65 74 20 74 68 65 20 63 6e 74 20 70 61 72 61 6d  et the cnt param
1a334 65 74 65 72 20 74 6f 20 74 68 65 20 6e 75 6d 62  eter to the numb
1a335 65 72 20 6f 66 20 70 72 69 6f 72 20 63 61 6c 6c  er of prior call
1a336 73 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72 6f 75  s to.** this rou
1a337 74 69 6e 65 20 64 75 72 69 6e 67 20 74 68 65 20  tine during the 
1a338 63 75 72 72 65 6e 74 20 72 65 61 64 20 61 74 74  current read att
1a339 65 6d 70 74 20 74 68 61 74 20 72 65 74 75 72 6e  empt that return
1a33a 65 64 20 57 41 4c 5f 52 45 54 52 59 2e 0a 2a 2a  ed WAL_RETRY..**
1a33b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   This routine wi
1a33c 6c 6c 20 73 74 61 72 74 20 74 61 6b 69 6e 67 20  ll start taking 
1a33d 6d 6f 72 65 20 61 67 67 72 65 73 73 69 76 65 20  more aggressive 
1a33e 6d 65 61 73 75 72 65 73 20 74 6f 20 63 6c 65 61  measures to clea
1a33f 72 20 74 68 65 0a 2a 2a 20 72 61 63 65 20 63 6f  r the.** race co
1a340 6e 64 69 74 69 6f 6e 73 20 61 66 74 65 72 20 6d  nditions after m
1a341 75 6c 74 69 70 6c 65 20 57 41 4c 5f 52 45 54 52  ultiple WAL_RETR
1a342 59 20 72 65 74 75 72 6e 73 2c 20 61 6e 64 20 61  Y returns, and a
1a343 66 74 65 72 20 61 6e 20 65 78 63 65 73 73 69 76  fter an excessiv
1a344 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65  e.** number of e
1a345 72 72 6f 72 73 20 77 69 6c 6c 20 75 6c 74 69 6d  rrors will ultim
1a346 61 74 65 6c 79 20 72 65 74 75 72 6e 20 53 51 4c  ately return SQL
1a347 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 2e 20 20 54  ITE_PROTOCOL.  T
1a348 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 4f  he.** SQLITE_PRO
1a349 54 4f 43 4f 4c 20 72 65 74 75 72 6e 20 69 6e 64  TOCOL return ind
1a34a 69 63 61 74 65 73 20 74 68 61 74 20 73 6f 6d 65  icates that some
1a34b 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68   other process h
1a34c 61 73 20 67 6f 6e 65 20 72 6f 67 75 65 0a 2a 2a  as gone rogue.**
1a34d 20 61 6e 64 20 69 73 20 6e 6f 74 20 68 6f 6e 6f   and is not hono
1a34e 72 69 6e 67 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ring the locking
1a34f 20 70 72 6f 74 6f 63 6f 6c 2e 20 20 54 68 65 72   protocol.  Ther
1a350 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67  e is a vanishing
1a351 6c 79 20 73 6d 61 6c 6c 0a 2a 2a 20 63 68 61 6e  ly small.** chan
1a352 63 65 20 74 68 61 74 20 53 51 4c 49 54 45 5f 50  ce that SQLITE_P
1a353 52 4f 54 4f 43 4f 4c 20 63 6f 75 6c 64 20 62 65  ROTOCOL could be
1a354 20 72 65 74 75 72 6e 65 64 20 62 65 63 61 75 73   returned becaus
1a355 65 20 6f 66 20 61 20 72 75 6e 20 6f 66 20 72 65  e of a run of re
1a356 61 6c 6c 79 0a 2a 2a 20 62 61 64 20 6c 75 63 6b  ally.** bad luck
1a357 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 6c   when there is l
1a358 6f 74 73 20 6f 66 20 63 6f 6e 74 65 6e 74 69 6f  ots of contentio
1a359 6e 20 66 6f 72 20 74 68 65 20 77 61 6c 2d 69 6e  n for the wal-in
1a35a 64 65 78 2c 20 62 75 74 20 74 68 61 74 0a 2a 2a  dex, but that.**
1a35b 20 70 6f 73 73 69 62 69 6c 69 74 79 20 69 73 20   possibility is 
1a35c 73 6f 20 73 6d 61 6c 6c 20 74 68 61 74 20 69 74  so small that it
1a35d 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79 20 6e   can be safely n
1a35e 65 67 6c 65 63 74 65 64 2c 20 77 65 20 62 65 6c  eglected, we bel
1a35f 69 65 76 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73  ieve..**.** On s
1a360 75 63 63 65 73 73 2c 20 74 68 69 73 20 72 6f 75  uccess, this rou
1a361 74 69 6e 65 20 6f 62 74 61 69 6e 73 20 61 20 72  tine obtains a r
1a362 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 0a 2a 2a 20  ead lock on .** 
1a363 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 70 57  WAL_READ_LOCK(pW
1a364 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 29 2e 20 20  al->readLock).  
1a365 54 68 65 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f  The pWal->readLo
1a366 63 6b 20 69 6e 74 65 67 65 72 20 69 73 0a 2a 2a  ck integer is.**
1a367 20 69 6e 20 74 68 65 20 72 61 6e 67 65 20 30 20   in the range 0 
1a368 3c 3d 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63  <= pWal->readLoc
1a369 6b 20 3c 20 57 41 4c 5f 4e 52 45 41 44 45 52 2e  k < WAL_NREADER.
1a36a 20 20 49 66 20 70 57 61 6c 2d 3e 72 65 61 64 4c    If pWal->readL
1a36b 6f 63 6b 3d 3d 28 2d 31 29 0a 2a 2a 20 74 68 61  ock==(-1).** tha
1a36c 74 20 6d 65 61 6e 73 20 74 68 65 20 57 61 6c 20  t means the Wal 
1a36d 64 6f 65 73 20 6e 6f 74 20 68 6f 6c 64 20 61 6e  does not hold an
1a36e 79 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 54 68  y read lock.  Th
1a36f 65 20 72 65 61 64 65 72 20 6d 75 73 74 20 6e 6f  e reader must no
1a370 74 0a 2a 2a 20 61 63 63 65 73 73 20 61 6e 79 20  t.** access any 
1a371 64 61 74 61 62 61 73 65 20 70 61 67 65 20 74 68  database page th
1a372 61 74 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62  at is modified b
1a373 79 20 61 20 57 41 4c 20 66 72 61 6d 65 20 75 70  y a WAL frame up
1a374 20 74 6f 20 61 6e 64 0a 2a 2a 20 69 6e 63 6c 75   to and.** inclu
1a375 64 69 6e 67 20 66 72 61 6d 65 20 6e 75 6d 62 65  ding frame numbe
1a376 72 20 61 52 65 61 64 4d 61 72 6b 5b 70 57 61 6c  r aReadMark[pWal
1a377 2d 3e 72 65 61 64 4c 6f 63 6b 5d 2e 20 20 54 68  ->readLock].  Th
1a378 65 20 72 65 61 64 65 72 20 77 69 6c 6c 0a 2a 2a  e reader will.**
1a379 20 75 73 65 20 57 41 4c 20 66 72 61 6d 65 73 20   use WAL frames 
1a37a 75 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64  up to and includ
1a37b 69 6e 67 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78  ing pWal->hdr.mx
1a37c 46 72 61 6d 65 20 69 66 20 70 57 61 6c 2d 3e 72  Frame if pWal->r
1a37d 65 61 64 4c 6f 63 6b 3e 30 0a 2a 2a 20 4f 72 20  eadLock>0.** Or 
1a37e 69 66 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63  if pWal->readLoc
1a37f 6b 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 72  k==0, then the r
1a380 65 61 64 65 72 20 77 69 6c 6c 20 69 67 6e 6f 72  eader will ignor
1a381 65 20 74 68 65 20 57 41 4c 0a 2a 2a 20 63 6f 6d  e the WAL.** com
1a382 70 6c 65 74 65 6c 79 20 61 6e 64 20 67 65 74 20  pletely and get 
1a383 61 6c 6c 20 63 6f 6e 74 65 6e 74 20 64 69 72 65  all content dire
1a384 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 64 61  ctly from the da
1a385 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20  tabase file..** 
1a386 49 66 20 74 68 65 20 75 73 65 57 61 6c 20 70 61  If the useWal pa
1a387 72 61 6d 65 74 65 72 20 69 73 20 31 20 74 68 65  rameter is 1 the
1a388 6e 20 74 68 65 20 57 41 4c 20 77 69 6c 6c 20 6e  n the WAL will n
1a389 65 76 65 72 20 62 65 20 69 67 6e 6f 72 65 64 20  ever be ignored 
1a38a 61 6e 64 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  and.** this rout
1a38b 69 6e 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20  ine will always 
1a38c 73 65 74 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f  set pWal->readLo
1a38d 63 6b 3e 30 20 6f 6e 20 73 75 63 63 65 73 73 2e  ck>0 on success.
1a38e 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 72 65 61  .** When the rea
1a38f 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  d transaction is
1a390 20 63 6f 6d 70 6c 65 74 65 64 2c 20 74 68 65 20   completed, the 
1a391 63 61 6c 6c 65 72 20 6d 75 73 74 20 72 65 6c 65  caller must rele
1a392 61 73 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20  ase the.** lock 
1a393 6f 6e 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b  on WAL_READ_LOCK
1a394 28 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 29  (pWal->readLock)
1a395 20 61 6e 64 20 73 65 74 20 70 57 61 6c 2d 3e 72   and set pWal->r
1a396 65 61 64 4c 6f 63 6b 20 74 6f 20 2d 31 2e 0a 2a  eadLock to -1..*
1a397 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1a398 65 20 75 73 65 73 20 74 68 65 20 6e 42 61 63 6b  e uses the nBack
1a399 66 69 6c 6c 20 61 6e 64 20 61 52 65 61 64 4d 61  fill and aReadMa
1a39a 72 6b 5b 5d 20 66 69 65 6c 64 73 20 6f 66 20 74  rk[] fields of t
1a39b 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 74 6f 20  he header.** to 
1a39c 73 65 6c 65 63 74 20 61 20 70 61 72 74 69 63 75  select a particu
1a39d 6c 61 72 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43  lar WAL_READ_LOC
1a39e 4b 28 29 20 74 68 61 74 20 73 74 72 69 76 65 73  K() that strives
1a39f 20 74 6f 20 6c 65 74 20 74 68 65 0a 2a 2a 20 63   to let the.** c
1a3a0 68 65 63 6b 70 6f 69 6e 74 20 70 72 6f 63 65 73  heckpoint proces
1a3a1 73 20 64 6f 20 61 73 20 6d 75 63 68 20 77 6f 72  s do as much wor
1a3a2 6b 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 20 20  k as possible.  
1a3a3 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67  This routine mig
1a3a4 68 74 0a 2a 2a 20 75 70 64 61 74 65 20 76 61 6c  ht.** update val
1a3a5 75 65 73 20 6f 66 20 74 68 65 20 61 52 65 61 64  ues of the aRead
1a3a6 4d 61 72 6b 5b 5d 20 61 72 72 61 79 20 69 6e 20  Mark[] array in 
1a3a7 74 68 65 20 68 65 61 64 65 72 2c 20 62 75 74 20  the header, but 
1a3a8 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 73 6f  if it does.** so
1a3a9 20 69 74 20 74 61 6b 65 73 20 63 61 72 65 20 74   it takes care t
1a3aa 6f 20 68 6f 6c 64 20 61 6e 20 65 78 63 6c 75 73  o hold an exclus
1a3ab 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ive lock on the 
1a3ac 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a  corresponding.**
1a3ad 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 29   WAL_READ_LOCK()
1a3ae 20 77 68 69 6c 65 20 63 68 61 6e 67 69 6e 67 20   while changing 
1a3af 76 61 6c 75 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  values..*/.stati
1a3b0 63 20 69 6e 74 20 77 61 6c 54 72 79 42 65 67 69  c int walTryBegi
1a3b1 6e 52 65 61 64 28 57 61 6c 20 2a 70 57 61 6c 2c  nRead(Wal *pWal,
1a3b2 20 69 6e 74 20 2a 70 43 68 61 6e 67 65 64 2c 20   int *pChanged, 
1a3b3 69 6e 74 20 75 73 65 57 61 6c 2c 20 69 6e 74 20  int useWal, int 
1a3b4 63 6e 74 29 7b 0a 20 20 76 6f 6c 61 74 69 6c 65  cnt){.  volatile
1a3b5 20 57 61 6c 43 6b 70 74 49 6e 66 6f 20 2a 70 49   WalCkptInfo *pI
1a3b6 6e 66 6f 3b 20 20 20 20 2f 2a 20 43 68 65 63 6b  nfo;    /* Check
1a3b7 70 6f 69 6e 74 20 69 6e 66 6f 72 6d 61 74 69 6f  point informatio
1a3b8 6e 20 69 6e 20 77 61 6c 2d 69 6e 64 65 78 20 2a  n in wal-index *
1a3b9 2f 0a 20 20 75 33 32 20 6d 78 52 65 61 64 4d 61  /.  u32 mxReadMa
1a3ba 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rk;             
1a3bb 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 61      /* Largest a
1a3bc 52 65 61 64 4d 61 72 6b 5b 5d 20 76 61 6c 75 65  ReadMark[] value
1a3bd 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 49 3b 20 20   */.  int mxI;  
1a3be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a3bf 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1a3c0 66 20 6c 61 72 67 65 73 74 20 61 52 65 61 64 4d  f largest aReadM
1a3c1 61 72 6b 5b 5d 20 76 61 6c 75 65 20 2a 2f 0a 20  ark[] value */. 
1a3c2 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
1a3c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a3c4 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
1a3c5 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   */.  int rc = S
1a3c6 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
1a3c7 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
1a3c8 63 6f 64 65 20 20 2a 2f 0a 0a 20 20 61 73 73 65  code  */..  asse
1a3c9 72 74 28 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f  rt( pWal->readLo
1a3ca 63 6b 3c 30 20 29 3b 20 20 20 20 20 2f 2a 20 4e  ck<0 );     /* N
1a3cb 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6c 6f 63  ot currently loc
1a3cc 6b 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 61 6b  ked */..  /* Tak
1a3cd 65 20 73 74 65 70 73 20 74 6f 20 61 76 6f 69 64  e steps to avoid
1a3ce 20 73 70 69 6e 6e 69 6e 67 20 66 6f 72 65 76 65   spinning foreve
1a3cf 72 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  r if there is a 
1a3d0 70 72 6f 74 6f 63 6f 6c 20 65 72 72 6f 72 2e 0a  protocol error..
1a3d1 20 20 2a 2a 0a 20 20 2a 2a 20 43 69 72 63 75 6d    **.  ** Circum
1a3d2 73 74 61 6e 63 65 73 20 74 68 61 74 20 63 61 75  stances that cau
1a3d3 73 65 20 61 20 52 45 54 52 59 20 73 68 6f 75 6c  se a RETRY shoul
1a3d4 64 20 6f 6e 6c 79 20 6c 61 73 74 20 66 6f 72 20  d only last for 
1a3d5 74 68 65 20 62 72 69 65 66 65 73 74 0a 20 20 2a  the briefest.  *
1a3d6 2a 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74  * instances of t
1a3d7 69 6d 65 2e 20 20 4e 6f 20 49 2f 4f 20 6f 72 20  ime.  No I/O or 
1a3d8 6f 74 68 65 72 20 73 79 73 74 65 6d 20 63 61 6c  other system cal
1a3d9 6c 73 20 61 72 65 20 64 6f 6e 65 20 77 68 69 6c  ls are done whil
1a3da 65 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 63 6b 73  e the.  ** locks
1a3db 20 61 72 65 20 68 65 6c 64 2c 20 73 6f 20 74 68   are held, so th
1a3dc 65 20 6c 6f 63 6b 73 20 73 68 6f 75 6c 64 20 6e  e locks should n
1a3dd 6f 74 20 62 65 20 68 65 6c 64 20 66 6f 72 20 76  ot be held for v
1a3de 65 72 79 20 6c 6f 6e 67 2e 20 42 75 74 20 0a 20  ery long. But . 
1a3df 20 2a 2a 20 69 66 20 77 65 20 61 72 65 20 75 6e   ** if we are un
1a3e0 6c 75 63 6b 79 2c 20 61 6e 6f 74 68 65 72 20 70  lucky, another p
1a3e1 72 6f 63 65 73 73 20 74 68 61 74 20 69 73 20 68  rocess that is h
1a3e2 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 6b 20 6d 69  olding a lock mi
1a3e3 67 68 74 20 67 65 74 0a 20 20 2a 2a 20 70 61 67  ght get.  ** pag
1a3e4 65 64 20 6f 75 74 20 6f 72 20 74 61 6b 65 20 61  ed out or take a
1a3e5 20 70 61 67 65 2d 66 61 75 6c 74 20 74 68 61 74   page-fault that
1a3e6 20 69 73 20 74 69 6d 65 2d 63 6f 6e 73 75 6d 69   is time-consumi
1a3e7 6e 67 20 74 6f 20 72 65 73 6f 6c 76 65 2c 20 0a  ng to resolve, .
1a3e8 20 20 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 20    ** during the 
1a3e9 66 65 77 20 6e 61 6e 6f 73 65 63 6f 6e 64 73 20  few nanoseconds 
1a3ea 74 68 61 74 20 69 74 20 69 73 20 68 6f 6c 64 69  that it is holdi
1a3eb 6e 67 20 74 68 65 20 6c 6f 63 6b 2e 20 20 49 6e  ng the lock.  In
1a3ec 20 74 68 61 74 20 63 61 73 65 2c 0a 20 20 2a 2a   that case,.  **
1a3ed 20 69 74 20 6d 69 67 68 74 20 74 61 6b 65 20 6c   it might take l
1a3ee 6f 6e 67 65 72 20 74 68 61 6e 20 6e 6f 72 6d 61  onger than norma
1a3ef 6c 20 66 6f 72 20 74 68 65 20 6c 6f 63 6b 20 74  l for the lock t
1a3f0 6f 20 66 72 65 65 2e 0a 20 20 2a 2a 0a 20 20 2a  o free..  **.  *
1a3f1 2a 20 41 66 74 65 72 20 35 20 52 45 54 52 59 73  * After 5 RETRYs
1a3f2 2c 20 77 65 20 62 65 67 69 6e 20 63 61 6c 6c 69  , we begin calli
1a3f3 6e 67 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65  ng sqlite3OsSlee
1a3f4 70 28 29 2e 20 20 54 68 65 20 66 69 72 73 74 20  p().  The first 
1a3f5 66 65 77 0a 20 20 2a 2a 20 63 61 6c 6c 73 20 74  few.  ** calls t
1a3f6 6f 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70  o sqlite3OsSleep
1a3f7 28 29 20 68 61 76 65 20 61 20 64 65 6c 61 79 20  () have a delay 
1a3f8 6f 66 20 31 20 6d 69 63 72 6f 73 65 63 6f 6e 64  of 1 microsecond
1a3f9 2e 20 20 52 65 61 6c 6c 79 20 74 68 69 73 0a 20  .  Really this. 
1a3fa 20 2a 2a 20 69 73 20 6d 6f 72 65 20 6f 66 20 61   ** is more of a
1a3fb 20 73 63 68 65 64 75 6c 65 72 20 79 69 65 6c 64   scheduler yield
1a3fc 20 74 68 61 6e 20 61 6e 20 61 63 74 75 61 6c 20   than an actual 
1a3fd 64 65 6c 61 79 2e 20 20 42 75 74 20 6f 6e 20 74  delay.  But on t
1a3fe 68 65 20 31 30 74 68 0a 20 20 2a 2a 20 61 6e 20  he 10th.  ** an 
1a3ff 73 75 62 73 65 71 75 65 6e 74 20 72 65 74 72 69  subsequent retri
1a400 65 73 2c 20 74 68 65 20 64 65 6c 61 79 73 20 73  es, the delays s
1a401 74 61 72 74 20 62 65 63 6f 6d 69 6e 67 20 6c 6f  tart becoming lo
1a402 6e 67 65 72 20 61 6e 64 20 6c 6f 6e 67 65 72 2c  nger and longer,
1a403 20 0a 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 6f   .  ** so that o
1a404 6e 20 74 68 65 20 31 30 30 74 68 20 28 61 6e 64  n the 100th (and
1a405 20 6c 61 73 74 29 20 52 45 54 52 59 20 77 65 20   last) RETRY we 
1a406 64 65 6c 61 79 20 66 6f 72 20 32 31 20 6d 69 6c  delay for 21 mil
1a407 6c 69 73 65 63 6f 6e 64 73 2e 0a 20 20 2a 2a 20  liseconds..  ** 
1a408 54 68 65 20 74 6f 74 61 6c 20 64 65 6c 61 79 20  The total delay 
1a409 74 69 6d 65 20 62 65 66 6f 72 65 20 67 69 76 69  time before givi
1a40a 6e 67 20 75 70 20 69 73 20 6c 65 73 73 20 74 68  ng up is less th
1a40b 61 6e 20 31 20 73 65 63 6f 6e 64 2e 0a 20 20 2a  an 1 second..  *
1a40c 2f 0a 20 20 69 66 28 20 63 6e 74 3e 35 20 29 7b  /.  if( cnt>5 ){
1a40d 0a 20 20 20 20 69 6e 74 20 6e 44 65 6c 61 79 20  .    int nDelay 
1a40e 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 1;            
1a40f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 75            /* Pau
1a410 73 65 20 74 69 6d 65 20 69 6e 20 6d 69 63 72 6f  se time in micro
1a411 73 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 20 20 69  seconds */.    i
1a412 66 28 20 63 6e 74 3e 31 30 30 20 29 7b 0a 20 20  f( cnt>100 ){.  
1a413 20 20 20 20 56 56 41 5f 4f 4e 4c 59 28 20 70 57      VVA_ONLY( pW
1a414 61 6c 2d 3e 6c 6f 63 6b 45 72 72 6f 72 20 3d 20  al->lockError = 
1a415 31 3b 20 29 0a 20 20 20 20 20 20 72 65 74 75 72  1; ).      retur
1a416 6e 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f  n SQLITE_PROTOCO
1a417 4c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  L;.    }.    if(
1a418 20 63 6e 74 3e 3d 31 30 20 29 20 6e 44 65 6c 61   cnt>=10 ) nDela
1a419 79 20 3d 20 28 63 6e 74 2d 39 29 2a 32 33 38 3b  y = (cnt-9)*238;
1a41a 20 20 2f 2a 20 4d 61 78 20 64 65 6c 61 79 20 32    /* Max delay 2
1a41b 31 6d 73 2e 20 54 6f 74 61 6c 20 64 65 6c 61 79  1ms. Total delay
1a41c 20 39 39 36 6d 73 20 2a 2f 0a 20 20 20 20 73 71   996ms */.    sq
1a41d 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 70 57 61  lite3OsSleep(pWa
1a41e 6c 2d 3e 70 56 66 73 2c 20 6e 44 65 6c 61 79 29  l->pVfs, nDelay)
1a41f 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 75 73  ;.  }..  if( !us
1a420 65 57 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d  eWal ){.    rc =
1a421 20 77 61 6c 49 6e 64 65 78 52 65 61 64 48 64 72   walIndexReadHdr
1a422 28 70 57 61 6c 2c 20 70 43 68 61 6e 67 65 64 29  (pWal, pChanged)
1a423 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1a424 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20  LITE_BUSY ){.   
1a425 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69     /* If there i
1a426 73 20 6e 6f 74 20 61 20 72 65 63 6f 76 65 72 79  s not a recovery
1a427 20 72 75 6e 6e 69 6e 67 20 69 6e 20 61 6e 6f 74   running in anot
1a428 68 65 72 20 74 68 72 65 61 64 20 6f 72 20 70 72  her thread or pr
1a429 6f 63 65 73 73 0a 20 20 20 20 20 20 2a 2a 20 74  ocess.      ** t
1a42a 68 65 6e 20 63 6f 6e 76 65 72 74 20 42 55 53 59  hen convert BUSY
1a42b 20 65 72 72 6f 72 73 20 74 6f 20 57 41 4c 5f 52   errors to WAL_R
1a42c 45 54 52 59 2e 20 20 49 66 20 72 65 63 6f 76 65  ETRY.  If recove
1a42d 72 79 20 69 73 20 6b 6e 6f 77 6e 20 74 6f 0a 20  ry is known to. 
1a42e 20 20 20 20 20 2a 2a 20 62 65 20 72 75 6e 6e 69       ** be runni
1a42f 6e 67 2c 20 63 6f 6e 76 65 72 74 20 42 55 53 59  ng, convert BUSY
1a430 20 74 6f 20 42 55 53 59 5f 52 45 43 4f 56 45 52   to BUSY_RECOVER
1a431 59 2e 20 20 54 68 65 72 65 20 69 73 20 61 20 72  Y.  There is a r
1a432 61 63 65 20 68 65 72 65 0a 20 20 20 20 20 20 2a  ace here.      *
1a433 2a 20 77 68 69 63 68 20 6d 69 67 68 74 20 63 61  * which might ca
1a434 75 73 65 20 57 41 4c 5f 52 45 54 52 59 20 74 6f  use WAL_RETRY to
1a435 20 62 65 20 72 65 74 75 72 6e 65 64 20 65 76 65   be returned eve
1a436 6e 20 69 66 20 42 55 53 59 5f 52 45 43 4f 56 45  n if BUSY_RECOVE
1a437 52 59 0a 20 20 20 20 20 20 2a 2a 20 77 6f 75 6c  RY.      ** woul
1a438 64 20 62 65 20 74 65 63 68 6e 69 63 61 6c 6c 79  d be technically
1a439 20 63 6f 72 72 65 63 74 2e 20 20 42 75 74 20 74   correct.  But t
1a43a 68 65 20 72 61 63 65 20 69 73 20 62 65 6e 69 67  he race is benig
1a43b 6e 20 73 69 6e 63 65 20 77 69 74 68 0a 20 20 20  n since with.   
1a43c 20 20 20 2a 2a 20 57 41 4c 5f 52 45 54 52 59 20     ** WAL_RETRY 
1a43d 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  this routine wil
1a43e 6c 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69  l be called agai
1a43f 6e 20 61 6e 64 20 77 69 6c 6c 20 70 72 6f 62 61  n and will proba
1a440 62 6c 79 20 62 65 0a 20 20 20 20 20 20 2a 2a 20  bly be.      ** 
1a441 72 69 67 68 74 20 6f 6e 20 74 68 65 20 73 65 63  right on the sec
1a442 6f 6e 64 20 69 74 65 72 61 74 69 6f 6e 2e 0a 20  ond iteration.. 
1a443 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
1a444 28 20 70 57 61 6c 2d 3e 61 70 57 69 44 61 74 61  ( pWal->apWiData
1a445 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  [0]==0 ){.      
1a446 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68    /* This branch
1a447 20 69 73 20 74 61 6b 65 6e 20 77 68 65 6e 20 74   is taken when t
1a448 68 65 20 78 53 68 6d 4d 61 70 28 29 20 6d 65 74  he xShmMap() met
1a449 68 6f 64 20 72 65 74 75 72 6e 73 20 53 51 4c 49  hod returns SQLI
1a44a 54 45 5f 42 55 53 59 2e 0a 20 20 20 20 20 20 20  TE_BUSY..       
1a44b 20 2a 2a 20 57 65 20 61 73 73 75 6d 65 20 74 68   ** We assume th
1a44c 69 73 20 69 73 20 61 20 74 72 61 6e 73 69 65 6e  is is a transien
1a44d 74 20 63 6f 6e 64 69 74 69 6f 6e 2c 20 73 6f 20  t condition, so 
1a44e 72 65 74 75 72 6e 20 57 41 4c 5f 52 45 54 52 59  return WAL_RETRY
1a44f 2e 20 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  . The.        **
1a450 20 78 53 68 6d 4d 61 70 28 29 20 69 6d 70 6c 65   xShmMap() imple
1a451 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 64 20 62  mentation used b
1a452 79 20 74 68 65 20 64 65 66 61 75 6c 74 20 75 6e  y the default un
1a453 69 78 20 61 6e 64 20 77 69 6e 33 32 20 56 46 53  ix and win32 VFS
1a454 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 6f 64   .        ** mod
1a455 75 6c 65 73 20 6d 61 79 20 72 65 74 75 72 6e 20  ules may return 
1a456 53 51 4c 49 54 45 5f 42 55 53 59 20 64 75 65 20  SQLITE_BUSY due 
1a457 74 6f 20 61 20 72 61 63 65 20 63 6f 6e 64 69 74  to a race condit
1a458 69 6f 6e 20 69 6e 20 74 68 65 20 0a 20 20 20 20  ion in the .    
1a459 20 20 20 20 2a 2a 20 63 6f 64 65 20 74 68 61 74      ** code that
1a45a 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74   determines whet
1a45b 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 73  her or not the s
1a45c 68 61 72 65 64 2d 6d 65 6d 6f 72 79 20 72 65 67  hared-memory reg
1a45d 69 6f 6e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  ion .        ** 
1a45e 6d 75 73 74 20 62 65 20 7a 65 72 6f 65 64 20 62  must be zeroed b
1a45f 65 66 6f 72 65 20 74 68 65 20 72 65 71 75 65 73  efore the reques
1a460 74 65 64 20 70 61 67 65 20 69 73 20 72 65 74 75  ted page is retu
1a461 72 6e 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f  rned..        */
1a462 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 57 41  .        rc = WA
1a463 4c 5f 52 45 54 52 59 3b 0a 20 20 20 20 20 20 7d  L_RETRY;.      }
1a464 65 6c 73 65 20 69 66 28 20 53 51 4c 49 54 45 5f  else if( SQLITE_
1a465 4f 4b 3d 3d 28 72 63 20 3d 20 77 61 6c 4c 6f 63  OK==(rc = walLoc
1a466 6b 53 68 61 72 65 64 28 70 57 61 6c 2c 20 57 41  kShared(pWal, WA
1a467 4c 5f 52 45 43 4f 56 45 52 5f 4c 4f 43 4b 29 29  L_RECOVER_LOCK))
1a468 20 29 7b 0a 20 20 20 20 20 20 20 20 77 61 6c 55   ){.        walU
1a469 6e 6c 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c  nlockShared(pWal
1a46a 2c 20 57 41 4c 5f 52 45 43 4f 56 45 52 5f 4c 4f  , WAL_RECOVER_LO
1a46b 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  CK);.        rc 
1a46c 3d 20 57 41 4c 5f 52 45 54 52 59 3b 0a 20 20 20  = WAL_RETRY;.   
1a46d 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d     }else if( rc=
1a46e 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a  =SQLITE_BUSY ){.
1a46f 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1a470 49 54 45 5f 42 55 53 59 5f 52 45 43 4f 56 45 52  ITE_BUSY_RECOVER
1a471 59 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  Y;.      }.    }
1a472 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1a473 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1a474 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
1a475 0a 20 20 7d 0a 0a 20 20 70 49 6e 66 6f 20 3d 20  .  }..  pInfo = 
1a476 77 61 6c 43 6b 70 74 49 6e 66 6f 28 70 57 61 6c  walCkptInfo(pWal
1a477 29 3b 0a 20 20 69 66 28 20 21 75 73 65 57 61 6c  );.  if( !useWal
1a478 20 26 26 20 70 49 6e 66 6f 2d 3e 6e 42 61 63 6b   && pInfo->nBack
1a479 66 69 6c 6c 3d 3d 70 57 61 6c 2d 3e 68 64 72 2e  fill==pWal->hdr.
1a47a 6d 78 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 2f  mxFrame ){.    /
1a47b 2a 20 54 68 65 20 57 41 4c 20 68 61 73 20 62 65  * The WAL has be
1a47c 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 20 62 61  en completely ba
1a47d 63 6b 66 69 6c 6c 65 64 20 28 6f 72 20 69 74 20  ckfilled (or it 
1a47e 69 73 20 65 6d 70 74 79 29 2e 0a 20 20 20 20 2a  is empty)..    *
1a47f 2a 20 61 6e 64 20 63 61 6e 20 62 65 20 73 61 66  * and can be saf
1a480 65 6c 79 20 69 67 6e 6f 72 65 64 2e 0a 20 20 20  ely ignored..   
1a481 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 77 61 6c   */.    rc = wal
1a482 4c 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c 2c  LockShared(pWal,
1a483 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 30   WAL_READ_LOCK(0
1a484 29 29 3b 0a 20 20 20 20 77 61 6c 53 68 6d 42 61  ));.    walShmBa
1a485 72 72 69 65 72 28 70 57 61 6c 29 3b 0a 20 20 20  rrier(pWal);.   
1a486 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1a487 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  OK ){.      if( 
1a488 6d 65 6d 63 6d 70 28 28 76 6f 69 64 20 2a 29 77  memcmp((void *)w
1a489 61 6c 49 6e 64 65 78 48 64 72 28 70 57 61 6c 29  alIndexHdr(pWal)
1a48a 2c 20 26 70 57 61 6c 2d 3e 68 64 72 2c 20 73 69  , &pWal->hdr, si
1a48b 7a 65 6f 66 28 57 61 6c 49 6e 64 65 78 48 64 72  zeof(WalIndexHdr
1a48c 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  )) ){.        /*
1a48d 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20   It is not safe 
1a48e 74 6f 20 61 6c 6c 6f 77 20 74 68 65 20 72 65 61  to allow the rea
1a48f 64 65 72 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20  der to continue 
1a490 68 65 72 65 20 69 66 20 66 72 61 6d 65 73 0a 20  here if frames. 
1a491 20 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 68 61         ** may ha
1a492 76 65 20 62 65 65 6e 20 61 70 70 65 6e 64 65 64  ve been appended
1a493 20 74 6f 20 74 68 65 20 6c 6f 67 20 62 65 66 6f   to the log befo
1a494 72 65 20 52 45 41 44 5f 4c 4f 43 4b 28 30 29 20  re READ_LOCK(0) 
1a495 77 61 73 20 6f 62 74 61 69 6e 65 64 2e 0a 20 20  was obtained..  
1a496 20 20 20 20 20 20 2a 2a 20 57 68 65 6e 20 68 6f        ** When ho
1a497 6c 64 69 6e 67 20 52 45 41 44 5f 4c 4f 43 4b 28  lding READ_LOCK(
1a498 30 29 2c 20 74 68 65 20 72 65 61 64 65 72 20 69  0), the reader i
1a499 67 6e 6f 72 65 73 20 74 68 65 20 65 6e 74 69 72  gnores the entir
1a49a 65 20 6c 6f 67 20 66 69 6c 65 2c 0a 20 20 20 20  e log file,.    
1a49b 20 20 20 20 2a 2a 20 77 68 69 63 68 20 69 6d 70      ** which imp
1a49c 6c 69 65 73 20 74 68 61 74 20 74 68 65 20 64 61  lies that the da
1a49d 74 61 62 61 73 65 20 66 69 6c 65 20 63 6f 6e 74  tabase file cont
1a49e 61 69 6e 73 20 61 20 74 72 75 73 74 77 6f 72 74  ains a trustwort
1a49f 68 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 6e  hy.        ** sn
1a4a0 61 70 73 68 6f 54 2e 20 53 69 6e 63 65 20 68 6f  apshoT. Since ho
1a4a1 6c 64 69 6e 67 20 52 45 41 44 5f 4c 4f 43 4b 28  lding READ_LOCK(
1a4a2 30 29 20 70 72 65 76 65 6e 74 73 20 61 20 63 68  0) prevents a ch
1a4a3 65 63 6b 70 6f 69 6e 74 20 66 72 6f 6d 0a 20 20  eckpoint from.  
1a4a4 20 20 20 20 20 20 2a 2a 20 68 61 70 70 65 6e 69        ** happeni
1a4a5 6e 67 2c 20 74 68 69 73 20 69 73 20 75 73 75 61  ng, this is usua
1a4a6 6c 6c 79 20 63 6f 72 72 65 63 74 2e 0a 20 20 20  lly correct..   
1a4a7 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
1a4a8 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69 66 20 66  ** However, if f
1a4a9 72 61 6d 65 73 20 68 61 76 65 20 62 65 65 6e 20  rames have been 
1a4aa 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20  appended to the 
1a4ab 6c 6f 67 20 28 6f 72 20 69 66 20 74 68 65 20 6c  log (or if the l
1a4ac 6f 67 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  og .        ** i
1a4ad 73 20 77 72 61 70 70 65 64 20 61 6e 64 20 77 72  s wrapped and wr
1a4ae 69 74 74 65 6e 20 66 6f 72 20 74 68 61 74 20 6d  itten for that m
1a4af 61 74 74 65 72 29 20 62 65 66 6f 72 65 20 74 68  atter) before th
1a4b0 65 20 52 45 41 44 5f 4c 4f 43 4b 28 30 29 0a 20  e READ_LOCK(0). 
1a4b1 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6f 62 74         ** is obt
1a4b2 61 69 6e 65 64 2c 20 74 68 61 74 20 69 73 20 6e  ained, that is n
1a4b3 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 74  ot necessarily t
1a4b4 72 75 65 2e 20 41 20 63 68 65 63 6b 70 6f 69 6e  rue. A checkpoin
1a4b5 74 65 72 20 6d 61 79 0a 20 20 20 20 20 20 20 20  ter may.        
1a4b6 2a 2a 20 68 61 76 65 20 73 74 61 72 74 65 64 20  ** have started 
1a4b7 74 6f 20 62 61 63 6b 66 69 6c 6c 20 74 68 65 20  to backfill the 
1a4b8 61 70 70 65 6e 64 65 64 20 66 72 61 6d 65 73 20  appended frames 
1a4b9 62 75 74 20 63 72 61 73 68 65 64 20 62 65 66 6f  but crashed befo
1a4ba 72 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74  re.        ** it
1a4bb 20 66 69 6e 69 73 68 65 64 2e 20 4c 65 61 76 69   finished. Leavi
1a4bc 6e 67 20 61 20 63 6f 72 72 75 70 74 20 69 6d 61  ng a corrupt ima
1a4bd 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ge in the databa
1a4be 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20  se file..       
1a4bf 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 61 6c 55   */.        walU
1a4c0 6e 6c 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c  nlockShared(pWal
1a4c1 2c 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28  , WAL_READ_LOCK(
1a4c2 30 29 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  0));.        ret
1a4c3 75 72 6e 20 57 41 4c 5f 52 45 54 52 59 3b 0a 20  urn WAL_RETRY;. 
1a4c4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 57 61       }.      pWa
1a4c5 6c 2d 3e 72 65 61 64 4c 6f 63 6b 20 3d 20 30 3b  l->readLock = 0;
1a4c6 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
1a4c7 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c  LITE_OK;.    }el
1a4c8 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  se if( rc!=SQLIT
1a4c9 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20  E_BUSY ){.      
1a4ca 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
1a4cb 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65  .  }..  /* If we
1a4cc 20 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 69   get this far, i
1a4cd 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  t means that the
1a4ce 20 72 65 61 64 65 72 20 77 69 6c 6c 20 77 61 6e   reader will wan
1a4cf 74 20 74 6f 20 75 73 65 0a 20 20 2a 2a 20 74 68  t to use.  ** th
1a4d0 65 20 57 41 4c 20 74 6f 20 67 65 74 20 61 74 20  e WAL to get at 
1a4d1 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 63  content from rec
1a4d2 65 6e 74 20 63 6f 6d 6d 69 74 73 2e 20 20 54 68  ent commits.  Th
1a4d3 65 20 6a 6f 62 20 6e 6f 77 20 69 73 0a 20 20 2a  e job now is.  *
1a4d4 2a 20 74 6f 20 73 65 6c 65 63 74 20 6f 6e 65 20  * to select one 
1a4d5 6f 66 20 74 68 65 20 61 52 65 61 64 4d 61 72 6b  of the aReadMark
1a4d6 5b 5d 20 65 6e 74 72 69 65 73 20 74 68 61 74 20  [] entries that 
1a4d7 69 73 20 63 6c 6f 73 65 73 74 20 74 6f 0a 20 20  is closest to.  
1a4d8 2a 2a 20 62 75 74 20 6e 6f 74 20 65 78 63 65 65  ** but not excee
1a4d9 64 69 6e 67 20 70 57 61 6c 2d 3e 68 64 72 2e 6d  ding pWal->hdr.m
1a4da 78 46 72 61 6d 65 20 61 6e 64 20 6c 6f 63 6b 20  xFrame and lock 
1a4db 74 68 61 74 20 65 6e 74 72 79 2e 0a 20 20 2a 2f  that entry..  */
1a4dc 0a 20 20 6d 78 52 65 61 64 4d 61 72 6b 20 3d 20  .  mxReadMark = 
1a4dd 30 3b 0a 20 20 6d 78 49 20 3d 20 30 3b 0a 20 20  0;.  mxI = 0;.  
1a4de 66 6f 72 28 69 3d 31 3b 20 69 3c 57 41 4c 5f 4e  for(i=1; i<WAL_N
1a4df 52 45 41 44 45 52 3b 20 69 2b 2b 29 7b 0a 20 20  READER; i++){.  
1a4e0 20 20 75 33 32 20 74 68 69 73 4d 61 72 6b 20 3d    u32 thisMark =
1a4e1 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61 72   pInfo->aReadMar
1a4e2 6b 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 6d 78  k[i];.    if( mx
1a4e3 52 65 61 64 4d 61 72 6b 3c 3d 74 68 69 73 4d 61  ReadMark<=thisMa
1a4e4 72 6b 20 26 26 20 74 68 69 73 4d 61 72 6b 3c 3d  rk && thisMark<=
1a4e5 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d  pWal->hdr.mxFram
1a4e6 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  e ){.      asser
1a4e7 74 28 20 74 68 69 73 4d 61 72 6b 21 3d 52 45 41  t( thisMark!=REA
1a4e8 44 4d 41 52 4b 5f 4e 4f 54 5f 55 53 45 44 20 29  DMARK_NOT_USED )
1a4e9 3b 0a 20 20 20 20 20 20 6d 78 52 65 61 64 4d 61  ;.      mxReadMa
1a4ea 72 6b 20 3d 20 74 68 69 73 4d 61 72 6b 3b 0a 20  rk = thisMark;. 
1a4eb 20 20 20 20 20 6d 78 49 20 3d 20 69 3b 0a 20 20       mxI = i;.  
1a4ec 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 54 68 65    }.  }.  /* The
1a4ed 72 65 20 77 61 73 20 6f 6e 63 65 20 61 6e 20 22  re was once an "
1a4ee 69 66 22 20 68 65 72 65 2e 20 54 68 65 20 65 78  if" here. The ex
1a4ef 74 72 61 20 22 7b 22 20 69 73 20 74 6f 20 70 72  tra "{" is to pr
1a4f0 65 73 65 72 76 65 20 69 6e 64 65 6e 74 61 74 69  eserve indentati
1a4f1 6f 6e 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 69  on. */.  {.    i
1a4f2 66 28 20 28 70 57 61 6c 2d 3e 72 65 61 64 4f 6e  f( (pWal->readOn
1a4f3 6c 79 20 26 20 57 41 4c 5f 53 48 4d 5f 52 44 4f  ly & WAL_SHM_RDO
1a4f4 4e 4c 59 29 3d 3d 30 0a 20 20 20 20 20 26 26 20  NLY)==0.     && 
1a4f5 28 6d 78 52 65 61 64 4d 61 72 6b 3c 70 57 61 6c  (mxReadMark<pWal
1a4f6 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 20 7c 7c  ->hdr.mxFrame ||
1a4f7 20 6d 78 49 3d 3d 30 29 0a 20 20 20 20 29 7b 0a   mxI==0).    ){.
1a4f8 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69        for(i=1; i
1a4f9 3c 57 41 4c 5f 4e 52 45 41 44 45 52 3b 20 69 2b  <WAL_NREADER; i+
1a4fa 2b 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  +){.        rc =
1a4fb 20 77 61 6c 4c 6f 63 6b 45 78 63 6c 75 73 69 76   walLockExclusiv
1a4fc 65 28 70 57 61 6c 2c 20 57 41 4c 5f 52 45 41 44  e(pWal, WAL_READ
1a4fd 5f 4c 4f 43 4b 28 69 29 2c 20 31 29 3b 0a 20 20  _LOCK(i), 1);.  
1a4fe 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
1a4ff 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1a500 20 20 20 20 20 6d 78 52 65 61 64 4d 61 72 6b 20       mxReadMark 
1a501 3d 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61  = pInfo->aReadMa
1a502 72 6b 5b 69 5d 20 3d 20 70 57 61 6c 2d 3e 68 64  rk[i] = pWal->hd
1a503 72 2e 6d 78 46 72 61 6d 65 3b 0a 20 20 20 20 20  r.mxFrame;.     
1a504 20 20 20 20 20 6d 78 49 20 3d 20 69 3b 0a 20 20       mxI = i;.  
1a505 20 20 20 20 20 20 20 20 77 61 6c 55 6e 6c 6f 63          walUnloc
1a506 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61 6c 2c  kExclusive(pWal,
1a507 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 69   WAL_READ_LOCK(i
1a508 29 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20  ), 1);.         
1a509 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1a50a 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51  }else if( rc!=SQ
1a50b 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20  LITE_BUSY ){.   
1a50c 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1a50d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1a50e 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
1a50f 28 20 6d 78 49 3d 3d 30 20 29 7b 0a 20 20 20 20  ( mxI==0 ){.    
1a510 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
1a511 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 28 70 57  LITE_BUSY || (pW
1a512 61 6c 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 20 57  al->readOnly & W
1a513 41 4c 5f 53 48 4d 5f 52 44 4f 4e 4c 59 29 21 3d  AL_SHM_RDONLY)!=
1a514 30 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  0 );.      retur
1a515 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  n rc==SQLITE_BUS
1a516 59 20 3f 20 57 41 4c 5f 52 45 54 52 59 20 3a 20  Y ? WAL_RETRY : 
1a517 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5f  SQLITE_READONLY_
1a518 43 41 4e 54 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a  CANTLOCK;.    }.
1a519 0a 20 20 20 20 72 63 20 3d 20 77 61 6c 4c 6f 63  .    rc = walLoc
1a51a 6b 53 68 61 72 65 64 28 70 57 61 6c 2c 20 57 41  kShared(pWal, WA
1a51b 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 6d 78 49 29  L_READ_LOCK(mxI)
1a51c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  );.    if( rc ){
1a51d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
1a51e 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 3f 20  ==SQLITE_BUSY ? 
1a51f 57 41 4c 5f 52 45 54 52 59 20 3a 20 72 63 3b 0a  WAL_RETRY : rc;.
1a520 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 4e 6f 77      }.    /* Now
1a521 20 74 68 61 74 20 74 68 65 20 72 65 61 64 2d 6c   that the read-l
1a522 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74  ock has been obt
1a523 61 69 6e 65 64 2c 20 63 68 65 63 6b 20 74 68 61  ained, check tha
1a524 74 20 6e 65 69 74 68 65 72 20 74 68 65 0a 20 20  t neither the.  
1a525 20 20 2a 2a 20 76 61 6c 75 65 20 69 6e 20 74 68    ** value in th
1a526 65 20 61 52 65 61 64 4d 61 72 6b 5b 5d 20 61 72  e aReadMark[] ar
1a527 72 61 79 20 6f 72 20 74 68 65 20 63 6f 6e 74 65  ray or the conte
1a528 6e 74 73 20 6f 66 20 74 68 65 20 77 61 6c 2d 69  nts of the wal-i
1a529 6e 64 65 78 0a 20 20 20 20 2a 2a 20 68 65 61 64  ndex.    ** head
1a52a 65 72 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e  er have changed.
1a52b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
1a52c 74 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74  t is necessary t
1a52d 6f 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65  o check that the
1a52e 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65   wal-index heade
1a52f 72 20 64 69 64 20 6e 6f 74 20 63 68 61 6e 67 65  r did not change
1a530 0a 20 20 20 20 2a 2a 20 62 65 74 77 65 65 6e 20  .    ** between 
1a531 74 68 65 20 74 69 6d 65 20 69 74 20 77 61 73 20  the time it was 
1a532 72 65 61 64 20 61 6e 64 20 77 68 65 6e 20 74 68  read and when th
1a533 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 77 61  e shared-lock wa
1a534 73 20 6f 62 74 61 69 6e 65 64 0a 20 20 20 20 2a  s obtained.    *
1a535 2a 20 6f 6e 20 57 41 4c 5f 52 45 41 44 5f 4c 4f  * on WAL_READ_LO
1a536 43 4b 28 6d 78 49 29 20 77 61 73 20 6f 62 74 61  CK(mxI) was obta
1a537 69 6e 65 64 20 74 6f 20 61 63 63 6f 75 6e 74 20  ined to account 
1a538 66 6f 72 20 74 68 65 20 70 6f 73 73 69 62 69 6c  for the possibil
1a539 69 74 79 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  ity.    ** that 
1a53a 74 68 65 20 6c 6f 67 20 66 69 6c 65 20 6d 61 79  the log file may
1a53b 20 68 61 76 65 20 62 65 65 6e 20 77 72 61 70 70   have been wrapp
1a53c 65 64 20 62 79 20 61 20 77 72 69 74 65 72 2c 20  ed by a writer, 
1a53d 6f 72 20 74 68 61 74 20 66 72 61 6d 65 73 0a 20  or that frames. 
1a53e 20 20 20 2a 2a 20 74 68 61 74 20 6f 63 63 75 72     ** that occur
1a53f 20 6c 61 74 65 72 20 69 6e 20 74 68 65 20 6c 6f   later in the lo
1a540 67 20 74 68 61 6e 20 70 57 61 6c 2d 3e 68 64 72  g than pWal->hdr
1a541 2e 6d 78 46 72 61 6d 65 20 6d 61 79 20 68 61 76  .mxFrame may hav
1a542 65 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 63 6f  e been.    ** co
1a543 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 64 61  pied into the da
1a544 74 61 62 61 73 65 20 62 79 20 61 20 63 68 65 63  tabase by a chec
1a545 6b 70 6f 69 6e 74 65 72 2e 20 49 66 20 65 69 74  kpointer. If eit
1a546 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 68 69  her of these thi
1a547 6e 67 73 0a 20 20 20 20 2a 2a 20 68 61 70 70 65  ngs.    ** happe
1a548 6e 65 64 2c 20 74 68 65 6e 20 72 65 61 64 69 6e  ned, then readin
1a549 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77  g the database w
1a54a 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 20  ith the current 
1a54b 76 61 6c 75 65 20 6f 66 0a 20 20 20 20 2a 2a 20  value of.    ** 
1a54c 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d  pWal->hdr.mxFram
1a54d 65 20 72 69 73 6b 73 20 72 65 61 64 69 6e 67 20  e risks reading 
1a54e 61 20 63 6f 72 72 75 70 74 65 64 20 73 6e 61 70  a corrupted snap
1a54f 73 68 6f 74 2e 20 53 6f 2c 20 72 65 74 72 79 0a  shot. So, retry.
1a550 20 20 20 20 2a 2a 20 69 6e 73 74 65 61 64 2e 0a      ** instead..
1a551 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
1a552 69 73 20 64 6f 65 73 20 6e 6f 74 20 67 75 61 72  is does not guar
1a553 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 63  antee that the c
1a554 6f 70 79 20 6f 66 20 74 68 65 20 77 61 6c 2d 69  opy of the wal-i
1a555 6e 64 65 78 20 68 65 61 64 65 72 20 69 73 20 75  ndex header is u
1a556 70 20 74 6f 0a 20 20 20 20 2a 2a 20 64 61 74 65  p to.    ** date
1a557 20 62 65 66 6f 72 65 20 70 72 6f 63 65 65 64 69   before proceedi
1a558 6e 67 2e 20 54 68 61 74 20 77 6f 75 6c 64 20 6e  ng. That would n
1a559 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 20 77  ot be possible w
1a55a 69 74 68 6f 75 74 20 73 6f 6d 65 68 6f 77 0a 20  ithout somehow. 
1a55b 20 20 20 2a 2a 20 62 6c 6f 63 6b 69 6e 67 20 77     ** blocking w
1a55c 72 69 74 65 72 73 2e 20 49 74 20 6f 6e 6c 79 20  riters. It only 
1a55d 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20  guarantees that 
1a55e 61 20 64 61 6e 67 65 72 6f 75 73 20 63 68 65 63  a dangerous chec
1a55f 6b 70 6f 69 6e 74 20 6f 72 20 0a 20 20 20 20 2a  kpoint or .    *
1a560 2a 20 6c 6f 67 2d 77 72 61 70 20 28 65 69 74 68  * log-wrap (eith
1a561 65 72 20 6f 66 20 77 68 69 63 68 20 77 6f 75 6c  er of which woul
1a562 64 20 72 65 71 75 69 72 65 20 61 6e 20 65 78 63  d require an exc
1a563 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 0a 20  lusive lock on. 
1a564 20 20 20 2a 2a 20 57 41 4c 5f 52 45 41 44 5f 4c     ** WAL_READ_L
1a565 4f 43 4b 28 6d 78 49 29 29 20 68 61 73 20 6e 6f  OCK(mxI)) has no
1a566 74 20 6f 63 63 75 72 72 65 64 20 73 69 6e 63 65  t occurred since
1a567 20 74 68 65 20 73 6e 61 70 73 68 6f 74 20 77 61   the snapshot wa
1a568 73 20 76 61 6c 69 64 2e 0a 20 20 20 20 2a 2f 0a  s valid..    */.
1a569 20 20 20 20 77 61 6c 53 68 6d 42 61 72 72 69 65      walShmBarrie
1a56a 72 28 70 57 61 6c 29 3b 0a 20 20 20 20 69 66 28  r(pWal);.    if(
1a56b 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64 4d 61 72   pInfo->aReadMar
1a56c 6b 5b 6d 78 49 5d 21 3d 6d 78 52 65 61 64 4d 61  k[mxI]!=mxReadMa
1a56d 72 6b 0a 20 20 20 20 20 7c 7c 20 6d 65 6d 63 6d  rk.     || memcm
1a56e 70 28 28 76 6f 69 64 20 2a 29 77 61 6c 49 6e 64  p((void *)walInd
1a56f 65 78 48 64 72 28 70 57 61 6c 29 2c 20 26 70 57  exHdr(pWal), &pW
1a570 61 6c 2d 3e 68 64 72 2c 20 73 69 7a 65 6f 66 28  al->hdr, sizeof(
1a571 57 61 6c 49 6e 64 65 78 48 64 72 29 29 0a 20 20  WalIndexHdr)).  
1a572 20 20 29 7b 0a 20 20 20 20 20 20 77 61 6c 55 6e    ){.      walUn
1a573 6c 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c 2c  lockShared(pWal,
1a574 20 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 6d   WAL_READ_LOCK(m
1a575 78 49 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75  xI));.      retu
1a576 72 6e 20 57 41 4c 5f 52 45 54 52 59 3b 0a 20 20  rn WAL_RETRY;.  
1a577 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
1a578 73 73 65 72 74 28 20 6d 78 52 65 61 64 4d 61 72  ssert( mxReadMar
1a579 6b 3c 3d 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46  k<=pWal->hdr.mxF
1a57a 72 61 6d 65 20 29 3b 0a 20 20 20 20 20 20 70 57  rame );.      pW
1a57b 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 20 3d 20 28  al->readLock = (
1a57c 69 31 36 29 6d 78 49 3b 0a 20 20 20 20 7d 0a 20  i16)mxI;.    }. 
1a57d 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1a57e 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 61  }../*.** Begin a
1a57f 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f   read transactio
1a580 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  n on the databas
1a581 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
1a582 75 74 69 6e 65 20 75 73 65 64 20 74 6f 20 62 65  utine used to be
1a583 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 4f   called sqlite3O
1a584 70 65 6e 53 6e 61 70 73 68 6f 74 28 29 20 61 6e  penSnapshot() an
1a585 64 20 77 69 74 68 20 67 6f 6f 64 20 72 65 61 73  d with good reas
1a586 6f 6e 3a 0a 2a 2a 20 69 74 20 74 61 6b 65 73 20  on:.** it takes 
1a587 61 20 73 6e 61 70 73 68 6f 74 20 6f 66 20 74 68  a snapshot of th
1a588 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 57  e state of the W
1a589 41 4c 20 61 6e 64 20 77 61 6c 2d 69 6e 64 65 78  AL and wal-index
1a58a 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
1a58b 0a 2a 2a 20 69 6e 73 74 61 6e 74 20 69 6e 20 74  .** instant in t
1a58c 69 6d 65 2e 20 20 54 68 65 20 63 75 72 72 65 6e  ime.  The curren
1a58d 74 20 74 68 72 65 61 64 20 77 69 6c 6c 20 63 6f  t thread will co
1a58e 6e 74 69 6e 75 65 20 74 6f 20 75 73 65 20 74 68  ntinue to use th
1a58f 69 73 20 73 6e 61 70 73 68 6f 74 2e 0a 2a 2a 20  is snapshot..** 
1a590 4f 74 68 65 72 20 74 68 72 65 61 64 73 20 6d 69  Other threads mi
1a591 67 68 74 20 61 70 70 65 6e 64 20 6e 65 77 20 63  ght append new c
1a592 6f 6e 74 65 6e 74 20 74 6f 20 74 68 65 20 57 41  ontent to the WA
1a593 4c 20 61 6e 64 20 77 61 6c 2d 69 6e 64 65 78 20  L and wal-index 
1a594 62 75 74 0a 2a 2a 20 74 68 61 74 20 65 78 74 72  but.** that extr
1a595 61 20 63 6f 6e 74 65 6e 74 20 69 73 20 69 67 6e  a content is ign
1a596 6f 72 65 64 20 62 79 20 74 68 65 20 63 75 72 72  ored by the curr
1a597 65 6e 74 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a  ent thread..**.*
1a598 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
1a599 65 20 63 6f 6e 74 65 6e 74 73 20 68 61 76 65 20  e contents have 
1a59a 63 68 61 6e 67 65 73 20 73 69 6e 63 65 20 74 68  changes since th
1a59b 65 20 70 72 65 76 69 6f 75 73 20 72 65 61 64 0a  e previous read.
1a59c 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  ** transaction, 
1a59d 74 68 65 6e 20 2a 70 43 68 61 6e 67 65 64 20 69  then *pChanged i
1a59e 73 20 73 65 74 20 74 6f 20 31 20 62 65 66 6f 72  s set to 1 befor
1a59f 65 20 72 65 74 75 72 6e 69 6e 67 2e 20 20 54 68  e returning.  Th
1a5a0 65 0a 2a 2a 20 50 61 67 65 72 20 6c 61 79 65 72  e.** Pager layer
1a5a1 20 77 69 6c 6c 20 75 73 65 20 74 68 69 73 20 74   will use this t
1a5a2 6f 20 6b 6e 6f 77 20 74 68 61 74 20 69 73 20 63  o know that is c
1a5a3 61 63 68 65 20 69 73 20 73 74 61 6c 65 20 61 6e  ache is stale an
1a5a4 64 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65  d.** needs to be
1a5a5 20 66 6c 75 73 68 65 64 2e 0a 2a 2f 0a 53 51 4c   flushed..*/.SQL
1a5a6 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1a5a7 73 71 6c 69 74 65 33 57 61 6c 42 65 67 69 6e 52  sqlite3WalBeginR
1a5a8 65 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 57  eadTransaction(W
1a5a9 61 6c 20 2a 70 57 61 6c 2c 20 69 6e 74 20 2a 70  al *pWal, int *p
1a5aa 43 68 61 6e 67 65 64 29 7b 0a 20 20 69 6e 74 20  Changed){.  int 
1a5ab 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
1a5ac 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1a5ad 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
1a5ae 69 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20 20 20  int cnt = 0;    
1a5af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a5b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 54 72 79  /* Number of Try
1a5b1 42 65 67 69 6e 52 65 61 64 20 61 74 74 65 6d 70  BeginRead attemp
1a5b2 74 73 20 2a 2f 0a 0a 20 20 64 6f 7b 0a 20 20 20  ts */..  do{.   
1a5b3 20 72 63 20 3d 20 77 61 6c 54 72 79 42 65 67 69   rc = walTryBegi
1a5b4 6e 52 65 61 64 28 70 57 61 6c 2c 20 70 43 68 61  nRead(pWal, pCha
1a5b5 6e 67 65 64 2c 20 30 2c 20 2b 2b 63 6e 74 29 3b  nged, 0, ++cnt);
1a5b6 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 57  .  }while( rc==W
1a5b7 41 4c 5f 52 45 54 52 59 20 29 3b 0a 20 20 74 65  AL_RETRY );.  te
1a5b8 73 74 63 61 73 65 28 20 28 72 63 26 30 78 66 66  stcase( (rc&0xff
1a5b9 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29  )==SQLITE_BUSY )
1a5ba 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 28 72  ;.  testcase( (r
1a5bb 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f  c&0xff)==SQLITE_
1a5bc 49 4f 45 52 52 20 29 3b 0a 20 20 74 65 73 74 63  IOERR );.  testc
1a5bd 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ase( rc==SQLITE_
1a5be 50 52 4f 54 4f 43 4f 4c 20 29 3b 0a 20 20 74 65  PROTOCOL );.  te
1a5bf 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49  stcase( rc==SQLI
1a5c0 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 65 74 75 72  TE_OK );.  retur
1a5c1 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  n rc;.}../*.** F
1a5c2 69 6e 69 73 68 20 77 69 74 68 20 61 20 72 65 61  inish with a rea
1a5c3 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  d transaction.  
1a5c4 41 6c 6c 20 74 68 69 73 20 64 6f 65 73 20 69 73  All this does is
1a5c5 20 72 65 6c 65 61 73 65 20 74 68 65 0a 2a 2a 20   release the.** 
1a5c6 72 65 61 64 2d 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51  read-lock..*/.SQ
1a5c7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1a5c8 64 20 73 71 6c 69 74 65 33 57 61 6c 45 6e 64 52  d sqlite3WalEndR
1a5c9 65 61 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 57  eadTransaction(W
1a5ca 61 6c 20 2a 70 57 61 6c 29 7b 0a 20 20 73 71 6c  al *pWal){.  sql
1a5cb 69 74 65 33 57 61 6c 45 6e 64 57 72 69 74 65 54  ite3WalEndWriteT
1a5cc 72 61 6e 73 61 63 74 69 6f 6e 28 70 57 61 6c 29  ransaction(pWal)
1a5cd 3b 0a 20 20 69 66 28 20 70 57 61 6c 2d 3e 72 65  ;.  if( pWal->re
1a5ce 61 64 4c 6f 63 6b 3e 3d 30 20 29 7b 0a 20 20 20  adLock>=0 ){.   
1a5cf 20 77 61 6c 55 6e 6c 6f 63 6b 53 68 61 72 65 64   walUnlockShared
1a5d0 28 70 57 61 6c 2c 20 57 41 4c 5f 52 45 41 44 5f  (pWal, WAL_READ_
1a5d1 4c 4f 43 4b 28 70 57 61 6c 2d 3e 72 65 61 64 4c  LOCK(pWal->readL
1a5d2 6f 63 6b 29 29 3b 0a 20 20 20 20 70 57 61 6c 2d  ock));.    pWal-
1a5d3 3e 72 65 61 64 4c 6f 63 6b 20 3d 20 2d 31 3b 0a  >readLock = -1;.
1a5d4 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61    }.}../*.** Rea
1a5d5 64 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68  d a page from th
1a5d6 65 20 57 41 4c 2c 20 69 66 20 69 74 20 69 73 20  e WAL, if it is 
1a5d7 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 57  present in the W
1a5d8 41 4c 20 61 6e 64 20 69 66 20 74 68 65 20 0a 2a  AL and if the .*
1a5d9 2a 20 63 75 72 72 65 6e 74 20 72 65 61 64 20 74  * current read t
1a5da 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
1a5db 6e 66 69 67 75 72 65 64 20 74 6f 20 75 73 65 20  nfigured to use 
1a5dc 74 68 65 20 57 41 4c 2e 20 20 0a 2a 2a 0a 2a 2a  the WAL.  .**.**
1a5dd 20 54 68 65 20 2a 70 49 6e 57 61 6c 20 69 73 20   The *pInWal is 
1a5de 73 65 74 20 74 6f 20 31 20 69 66 20 74 68 65 20  set to 1 if the 
1a5df 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69  requested page i
1a5e0 73 20 69 6e 20 74 68 65 20 57 41 4c 20 61 6e 64  s in the WAL and
1a5e1 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6c 6f 61  .** has been loa
1a5e2 64 65 64 2e 20 20 4f 72 20 2a 70 49 6e 57 61 6c  ded.  Or *pInWal
1a5e3 20 69 73 20 73 65 74 20 74 6f 20 30 20 69 66 20   is set to 0 if 
1a5e4 74 68 65 20 70 61 67 65 20 77 61 73 20 6e 6f 74  the page was not
1a5e5 20 69 6e 20 0a 2a 2a 20 74 68 65 20 57 41 4c 20   in .** the WAL 
1a5e6 61 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65 20  and needs to be 
1a5e7 72 65 61 64 20 6f 75 74 20 6f 66 20 74 68 65 20  read out of the 
1a5e8 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c  database..*/.SQL
1a5e9 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1a5ea 73 71 6c 69 74 65 33 57 61 6c 52 65 61 64 28 0a  sqlite3WalRead(.
1a5eb 20 20 57 61 6c 20 2a 70 57 61 6c 2c 20 20 20 20    Wal *pWal,    
1a5ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a5ed 20 20 2f 2a 20 57 41 4c 20 68 61 6e 64 6c 65 20    /* WAL handle 
1a5ee 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  */.  Pgno pgno, 
1a5ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a5f0 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
1a5f1 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20   page number to 
1a5f2 72 65 61 64 20 64 61 74 61 20 66 6f 72 20 2a 2f  read data for */
1a5f3 0a 20 20 69 6e 74 20 2a 70 49 6e 57 61 6c 2c 20  .  int *pInWal, 
1a5f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a5f5 20 20 20 2f 2a 20 4f 55 54 3a 20 54 72 75 65 20     /* OUT: True 
1a5f6 69 66 20 64 61 74 61 20 69 73 20 72 65 61 64 20  if data is read 
1a5f7 66 72 6f 6d 20 57 41 4c 20 2a 2f 0a 20 20 69 6e  from WAL */.  in
1a5f8 74 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20  t nOut,         
1a5f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a5fa 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20   Size of buffer 
1a5fb 70 4f 75 74 20 69 6e 20 62 79 74 65 73 20 2a 2f  pOut in bytes */
1a5fc 0a 20 20 75 38 20 2a 70 4f 75 74 20 20 20 20 20  .  u8 *pOut     
1a5fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a5fe 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20     /* Buffer to 
1a5ff 77 72 69 74 65 20 70 61 67 65 20 64 61 74 61 20  write page data 
1a600 74 6f 20 2a 2f 0a 29 7b 0a 20 20 75 33 32 20 69  to */.){.  u32 i
1a601 52 65 61 64 20 3d 20 30 3b 20 20 20 20 20 20 20  Read = 0;       
1a602 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
1a603 20 21 3d 30 2c 20 57 41 4c 20 66 72 61 6d 65 20   !=0, WAL frame 
1a604 74 6f 20 72 65 74 75 72 6e 20 64 61 74 61 20 66  to return data f
1a605 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 69 4c 61  rom */.  u32 iLa
1a606 73 74 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 6d  st = pWal->hdr.m
1a607 78 46 72 61 6d 65 3b 20 20 2f 2a 20 4c 61 73 74  xFrame;  /* Last
1a608 20 70 61 67 65 20 69 6e 20 57 41 4c 20 66 6f 72   page in WAL for
1a609 20 74 68 69 73 20 72 65 61 64 65 72 20 2a 2f 0a   this reader */.
1a60a 20 20 69 6e 74 20 69 48 61 73 68 3b 20 20 20 20    int iHash;    
1a60b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a60c 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f    /* Used to loo
1a60d 70 20 74 68 72 6f 75 67 68 20 4e 20 68 61 73 68  p through N hash
1a60e 20 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a   tables */..  /*
1a60f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1a610 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20   only be called 
1a611 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 72 65  from within a re
1a612 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ad transaction. 
1a613 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 57 61  */.  assert( pWa
1a614 6c 2d 3e 72 65 61 64 4c 6f 63 6b 3e 3d 30 20 7c  l->readLock>=0 |
1a615 7c 20 70 57 61 6c 2d 3e 6c 6f 63 6b 45 72 72 6f  | pWal->lockErro
1a616 72 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  r );..  /* If th
1a617 65 20 22 6c 61 73 74 20 70 61 67 65 22 20 66 69  e "last page" fi
1a618 65 6c 64 20 6f 66 20 74 68 65 20 77 61 6c 2d 69  eld of the wal-i
1a619 6e 64 65 78 20 68 65 61 64 65 72 20 73 6e 61 70  ndex header snap
1a61a 73 68 6f 74 20 69 73 20 30 2c 20 74 68 65 6e 0a  shot is 0, then.
1a61b 20 20 2a 2a 20 6e 6f 20 64 61 74 61 20 77 69 6c    ** no data wil
1a61c 6c 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74  l be read from t
1a61d 68 65 20 77 61 6c 20 75 6e 64 65 72 20 61 6e 79  he wal under any
1a61e 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73 2e 20   circumstances. 
1a61f 52 65 74 75 72 6e 20 65 61 72 6c 79 0a 20 20 2a  Return early.  *
1a620 2a 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 61  * in this case a
1a621 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
1a622 6e 2e 20 20 4c 69 6b 65 77 69 73 65 2c 20 69 66  n.  Likewise, if
1a623 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 3d   pWal->readLock=
1a624 3d 30 2c 20 0a 20 20 2a 2a 20 74 68 65 6e 20 74  =0, .  ** then t
1a625 68 65 20 57 41 4c 20 69 73 20 69 67 6e 6f 72 65  he WAL is ignore
1a626 64 20 62 79 20 74 68 65 20 72 65 61 64 65 72 20  d by the reader 
1a627 73 6f 20 72 65 74 75 72 6e 20 65 61 72 6c 79 2c  so return early,
1a628 20 61 73 20 69 66 20 74 68 65 20 0a 20 20 2a 2a   as if the .  **
1a629 20 57 41 4c 20 77 65 72 65 20 65 6d 70 74 79 2e   WAL were empty.
1a62a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 4c 61 73  .  */.  if( iLas
1a62b 74 3d 3d 30 20 7c 7c 20 70 57 61 6c 2d 3e 72 65  t==0 || pWal->re
1a62c 61 64 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20  adLock==0 ){.   
1a62d 20 2a 70 49 6e 57 61 6c 20 3d 20 30 3b 0a 20 20   *pInWal = 0;.  
1a62e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1a62f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65  OK;.  }..  /* Se
1a630 61 72 63 68 20 74 68 65 20 68 61 73 68 20 74 61  arch the hash ta
1a631 62 6c 65 20 6f 72 20 74 61 62 6c 65 73 20 66 6f  ble or tables fo
1a632 72 20 61 6e 20 65 6e 74 72 79 20 6d 61 74 63 68  r an entry match
1a633 69 6e 67 20 70 61 67 65 20 6e 75 6d 62 65 72 0a  ing page number.
1a634 20 20 2a 2a 20 70 67 6e 6f 2e 20 45 61 63 68 20    ** pgno. Each 
1a635 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  iteration of the
1a636 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 28 29   following for()
1a637 20 6c 6f 6f 70 20 73 65 61 72 63 68 65 73 20 6f   loop searches o
1a638 6e 65 0a 20 20 2a 2a 20 68 61 73 68 20 74 61 62  ne.  ** hash tab
1a639 6c 65 20 28 65 61 63 68 20 68 61 73 68 20 74 61  le (each hash ta
1a63a 62 6c 65 20 69 6e 64 65 78 65 73 20 75 70 20 74  ble indexes up t
1a63b 6f 20 48 41 53 48 54 41 42 4c 45 5f 4e 50 41 47  o HASHTABLE_NPAG
1a63c 45 20 66 72 61 6d 65 73 29 2e 0a 20 20 2a 2a 0a  E frames)..  **.
1a63d 20 20 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 6d    ** This code m
1a63e 69 67 68 74 20 72 75 6e 20 63 6f 6e 63 75 72 72  ight run concurr
1a63f 65 6e 74 6c 79 20 74 6f 20 74 68 65 20 63 6f 64  ently to the cod
1a640 65 20 69 6e 20 77 61 6c 49 6e 64 65 78 41 70 70  e in walIndexApp
1a641 65 6e 64 28 29 0a 20 20 2a 2a 20 74 68 61 74 20  end().  ** that 
1a642 61 64 64 73 20 65 6e 74 72 69 65 73 20 74 6f 20  adds entries to 
1a643 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 28 61  the wal-index (a
1a644 6e 64 20 70 6f 73 73 69 62 6c 79 20 74 6f 20 74  nd possibly to t
1a645 68 69 73 20 68 61 73 68 20 0a 20 20 2a 2a 20 74  his hash .  ** t
1a646 61 62 6c 65 29 2e 20 54 68 69 73 20 6d 65 61 6e  able). This mean
1a647 73 20 74 68 65 20 76 61 6c 75 65 20 6a 75 73 74  s the value just
1a648 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 68   read from the h
1a649 61 73 68 20 0a 20 20 2a 2a 20 73 6c 6f 74 20 28  ash .  ** slot (
1a64a 61 48 61 73 68 5b 69 4b 65 79 5d 29 20 6d 61 79  aHash[iKey]) may
1a64b 20 68 61 76 65 20 62 65 65 6e 20 61 64 64 65 64   have been added
1a64c 20 62 65 66 6f 72 65 20 6f 72 20 61 66 74 65 72   before or after
1a64d 20 74 68 65 20 0a 20 20 2a 2a 20 63 75 72 72 65   the .  ** curre
1a64e 6e 74 20 72 65 61 64 20 74 72 61 6e 73 61 63 74  nt read transact
1a64f 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e 20  ion was opened. 
1a650 56 61 6c 75 65 73 20 61 64 64 65 64 20 61 66 74  Values added aft
1a651 65 72 20 74 68 65 0a 20 20 2a 2a 20 72 65 61 64  er the.  ** read
1a652 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73   transaction was
1a653 20 6f 70 65 6e 65 64 20 6d 61 79 20 68 61 76 65   opened may have
1a654 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 69 6e   been written in
1a655 63 6f 72 72 65 63 74 6c 79 20 2d 0a 20 20 2a 2a  correctly -.  **
1a656 20 69 2e 65 2e 20 74 68 65 73 65 20 73 6c 6f 74   i.e. these slot
1a657 73 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 67 61  s may contain ga
1a658 72 62 61 67 65 20 64 61 74 61 2e 20 48 6f 77 65  rbage data. Howe
1a659 76 65 72 2c 20 77 65 20 61 73 73 75 6d 65 0a 20  ver, we assume. 
1a65a 20 2a 2a 20 74 68 61 74 20 61 6e 79 20 73 6c 6f   ** that any slo
1a65b 74 73 20 77 72 69 74 74 65 6e 20 62 65 66 6f 72  ts written befor
1a65c 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 65  e the current re
1a65d 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  ad transaction w
1a65e 61 73 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 20 72  as.  ** opened r
1a65f 65 6d 61 69 6e 20 75 6e 6d 6f 64 69 66 69 65 64  emain unmodified
1a660 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20  ..  **.  ** For 
1a661 74 68 65 20 72 65 61 73 6f 6e 73 20 61 62 6f 76  the reasons abov
1a662 65 2c 20 74 68 65 20 69 66 28 2e 2e 2e 29 20 63  e, the if(...) c
1a663 6f 6e 64 69 74 69 6f 6e 20 66 65 61 74 75 72 65  ondition feature
1a664 64 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 0a 20  d in the inner. 
1a665 20 2a 2a 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20   ** loop of the 
1a666 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20  following block 
1a667 69 73 20 6d 6f 72 65 20 73 74 72 69 6e 67 65 6e  is more stringen
1a668 74 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20  t that would be 
1a669 72 65 71 75 69 72 65 64 20 0a 20 20 2a 2a 20 69  required .  ** i
1a66a 66 20 77 65 20 68 61 64 20 65 78 63 6c 75 73 69  f we had exclusi
1a66b 76 65 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  ve access to the
1a66c 20 68 61 73 68 2d 74 61 62 6c 65 3a 0a 20 20 2a   hash-table:.  *
1a66d 2a 0a 20 20 2a 2a 20 20 20 28 61 50 67 6e 6f 5b  *.  **   (aPgno[
1a66e 69 46 72 61 6d 65 5d 3d 3d 70 67 6e 6f 29 3a 20  iFrame]==pgno): 
1a66f 0a 20 20 2a 2a 20 20 20 20 20 54 68 69 73 20 63  .  **     This c
1a670 6f 6e 64 69 74 69 6f 6e 20 66 69 6c 74 65 72 73  ondition filters
1a671 20 6f 75 74 20 6e 6f 72 6d 61 6c 20 68 61 73 68   out normal hash
1a672 2d 74 61 62 6c 65 20 63 6f 6c 6c 69 73 69 6f 6e  -table collision
1a673 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 28  s..  **.  **   (
1a674 69 46 72 61 6d 65 3c 3d 69 4c 61 73 74 29 3a 20  iFrame<=iLast): 
1a675 0a 20 20 2a 2a 20 20 20 20 20 54 68 69 73 20 63  .  **     This c
1a676 6f 6e 64 69 74 69 6f 6e 20 66 69 6c 74 65 72 73  ondition filters
1a677 20 6f 75 74 20 65 6e 74 72 69 65 73 20 74 68 61   out entries tha
1a678 74 20 77 65 72 65 20 61 64 64 65 64 20 74 6f 20  t were added to 
1a679 74 68 65 20 68 61 73 68 0a 20 20 2a 2a 20 20 20  the hash.  **   
1a67a 20 20 74 61 62 6c 65 20 61 66 74 65 72 20 74 68    table after th
1a67b 65 20 63 75 72 72 65 6e 74 20 72 65 61 64 2d 74  e current read-t
1a67c 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 64 20 73  ransaction had s
1a67d 74 61 72 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 66  tarted..  */.  f
1a67e 6f 72 28 69 48 61 73 68 3d 77 61 6c 46 72 61 6d  or(iHash=walFram
1a67f 65 50 61 67 65 28 69 4c 61 73 74 29 3b 20 69 48  ePage(iLast); iH
1a680 61 73 68 3e 3d 30 20 26 26 20 69 52 65 61 64 3d  ash>=0 && iRead=
1a681 3d 30 3b 20 69 48 61 73 68 2d 2d 29 7b 0a 20 20  =0; iHash--){.  
1a682 20 20 76 6f 6c 61 74 69 6c 65 20 68 74 5f 73 6c    volatile ht_sl
1a683 6f 74 20 2a 61 48 61 73 68 3b 20 20 20 20 20 20  ot *aHash;      
1a684 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 68 61  /* Pointer to ha
1a685 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  sh table */.    
1a686 76 6f 6c 61 74 69 6c 65 20 75 33 32 20 2a 61 50  volatile u32 *aP
1a687 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  gno;          /*
1a688 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 72 72 61   Pointer to arra
1a689 79 20 6f 66 20 70 61 67 65 20 6e 75 6d 62 65 72  y of page number
1a68a 73 20 2a 2f 0a 20 20 20 20 75 33 32 20 69 5a 65  s */.    u32 iZe
1a68b 72 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ro;             
1a68c 20 20 20 20 20 20 20 2f 2a 20 46 72 61 6d 65 20         /* Frame 
1a68d 6e 75 6d 62 65 72 20 63 6f 72 72 65 73 70 6f 6e  number correspon
1a68e 64 69 6e 67 20 74 6f 20 61 50 67 6e 6f 5b 30 5d  ding to aPgno[0]
1a68f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4b 65 79   */.    int iKey
1a690 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1a691 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 73 6c        /* Hash sl
1a692 6f 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20  ot index */.    
1a693 69 6e 74 20 6e 43 6f 6c 6c 69 64 65 3b 20 20 20  int nCollide;   
1a694 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a695 20 4e 75 6d 62 65 72 20 6f 66 20 68 61 73 68 20   Number of hash 
1a696 63 6f 6c 6c 69 73 69 6f 6e 73 20 72 65 6d 61 69  collisions remai
1a697 6e 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ning */.    int 
1a698 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
1a699 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72            /* Err
1a69a 6f 72 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 20 20  or code */..    
1a69b 72 63 20 3d 20 77 61 6c 48 61 73 68 47 65 74 28  rc = walHashGet(
1a69c 70 57 61 6c 2c 20 69 48 61 73 68 2c 20 26 61 48  pWal, iHash, &aH
1a69d 61 73 68 2c 20 26 61 50 67 6e 6f 2c 20 26 69 5a  ash, &aPgno, &iZ
1a69e 65 72 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ero);.    if( rc
1a69f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1a6a0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1a6a1 20 20 20 20 7d 0a 20 20 20 20 6e 43 6f 6c 6c 69      }.    nColli
1a6a2 64 65 20 3d 20 48 41 53 48 54 41 42 4c 45 5f 4e  de = HASHTABLE_N
1a6a3 53 4c 4f 54 3b 0a 20 20 20 20 66 6f 72 28 69 4b  SLOT;.    for(iK
1a6a4 65 79 3d 77 61 6c 48 61 73 68 28 70 67 6e 6f 29  ey=walHash(pgno)
1a6a5 3b 20 61 48 61 73 68 5b 69 4b 65 79 5d 3b 20 69  ; aHash[iKey]; i
1a6a6 4b 65 79 3d 77 61 6c 4e 65 78 74 48 61 73 68 28  Key=walNextHash(
1a6a7 69 4b 65 79 29 29 7b 0a 20 20 20 20 20 20 75 33  iKey)){.      u3
1a6a8 32 20 69 46 72 61 6d 65 20 3d 20 61 48 61 73 68  2 iFrame = aHash
1a6a9 5b 69 4b 65 79 5d 20 2b 20 69 5a 65 72 6f 3b 0a  [iKey] + iZero;.
1a6aa 20 20 20 20 20 20 69 66 28 20 69 46 72 61 6d 65        if( iFrame
1a6ab 3c 3d 69 4c 61 73 74 20 26 26 20 61 50 67 6e 6f  <=iLast && aPgno
1a6ac 5b 61 48 61 73 68 5b 69 4b 65 79 5d 5d 3d 3d 70  [aHash[iKey]]==p
1a6ad 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  gno ){.        /
1a6ae 2a 20 61 73 73 65 72 74 28 20 69 46 72 61 6d 65  * assert( iFrame
1a6af 3e 69 52 65 61 64 20 29 3b 20 2d 2d 20 6e 6f 74  >iRead ); -- not
1a6b0 20 74 72 75 65 20 69 66 20 74 68 65 72 65 20 69   true if there i
1a6b1 73 20 63 6f 72 72 75 70 74 69 6f 6e 20 2a 2f 0a  s corruption */.
1a6b2 20 20 20 20 20 20 20 20 69 52 65 61 64 20 3d 20          iRead = 
1a6b3 69 46 72 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a  iFrame;.      }.
1a6b4 20 20 20 20 20 20 69 66 28 20 28 6e 43 6f 6c 6c        if( (nColl
1a6b5 69 64 65 2d 2d 29 3d 3d 30 20 29 7b 0a 20 20 20  ide--)==0 ){.   
1a6b6 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1a6b7 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1a6b8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1a6b9 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54   }..#ifdef SQLIT
1a6ba 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49  E_ENABLE_EXPENSI
1a6bb 56 45 5f 41 53 53 45 52 54 0a 20 20 2f 2a 20 49  VE_ASSERT.  /* I
1a6bc 66 20 65 78 70 65 6e 73 69 76 65 20 61 73 73 65  f expensive asse
1a6bd 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20  rt() statements 
1a6be 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 64  are available, d
1a6bf 6f 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63  o a linear searc
1a6c0 68 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 77 61  h.  ** of the wa
1a6c1 6c 2d 69 6e 64 65 78 20 66 69 6c 65 20 63 6f 6e  l-index file con
1a6c2 74 65 6e 74 2e 20 4d 61 6b 65 20 73 75 72 65 20  tent. Make sure 
1a6c3 74 68 65 20 72 65 73 75 6c 74 73 20 61 67 72 65  the results agre
1a6c4 65 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20  e with the.  ** 
1a6c5 72 65 73 75 6c 74 20 6f 62 74 61 69 6e 65 64 20  result obtained 
1a6c6 75 73 69 6e 67 20 74 68 65 20 68 61 73 68 20 69  using the hash i
1a6c7 6e 64 65 78 65 73 20 61 62 6f 76 65 2e 20 20 2a  ndexes above.  *
1a6c8 2f 0a 20 20 7b 0a 20 20 20 20 75 33 32 20 69 52  /.  {.    u32 iR
1a6c9 65 61 64 32 20 3d 20 30 3b 0a 20 20 20 20 75 33  ead2 = 0;.    u3
1a6ca 32 20 69 54 65 73 74 3b 0a 20 20 20 20 66 6f 72  2 iTest;.    for
1a6cb 28 69 54 65 73 74 3d 69 4c 61 73 74 3b 20 69 54  (iTest=iLast; iT
1a6cc 65 73 74 3e 30 3b 20 69 54 65 73 74 2d 2d 29 7b  est>0; iTest--){
1a6cd 0a 20 20 20 20 20 20 69 66 28 20 77 61 6c 46 72  .      if( walFr
1a6ce 61 6d 65 50 67 6e 6f 28 70 57 61 6c 2c 20 69 54  amePgno(pWal, iT
1a6cf 65 73 74 29 3d 3d 70 67 6e 6f 20 29 7b 0a 20 20  est)==pgno ){.  
1a6d0 20 20 20 20 20 20 69 52 65 61 64 32 20 3d 20 69        iRead2 = i
1a6d1 54 65 73 74 3b 0a 20 20 20 20 20 20 20 20 62 72  Test;.        br
1a6d2 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1a6d3 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69   }.    assert( i
1a6d4 52 65 61 64 3d 3d 69 52 65 61 64 32 20 29 3b 0a  Read==iRead2 );.
1a6d5 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
1a6d6 20 49 66 20 69 52 65 61 64 20 69 73 20 6e 6f 6e   If iRead is non
1a6d7 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 69  -zero, then it i
1a6d8 73 20 74 68 65 20 6c 6f 67 20 66 72 61 6d 65 20  s the log frame 
1a6d9 6e 75 6d 62 65 72 20 74 68 61 74 20 63 6f 6e 74  number that cont
1a6da 61 69 6e 73 20 74 68 65 0a 20 20 2a 2a 20 72 65  ains the.  ** re
1a6db 71 75 69 72 65 64 20 70 61 67 65 2e 20 52 65 61  quired page. Rea
1a6dc 64 20 61 6e 64 20 72 65 74 75 72 6e 20 64 61 74  d and return dat
1a6dd 61 20 66 72 6f 6d 20 74 68 65 20 6c 6f 67 20 66  a from the log f
1a6de 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ile..  */.  if( 
1a6df 69 52 65 61 64 20 29 7b 0a 20 20 20 20 69 6e 74  iRead ){.    int
1a6e0 20 73 7a 3b 0a 20 20 20 20 69 36 34 20 69 4f 66   sz;.    i64 iOf
1a6e1 66 73 65 74 3b 0a 20 20 20 20 73 7a 20 3d 20 70  fset;.    sz = p
1a6e2 57 61 6c 2d 3e 68 64 72 2e 73 7a 50 61 67 65 3b  Wal->hdr.szPage;
1a6e3 0a 20 20 20 20 73 7a 20 3d 20 28 73 7a 26 30 78  .    sz = (sz&0x
1a6e4 66 65 30 30 29 20 2b 20 28 28 73 7a 26 30 78 30  fe00) + ((sz&0x0
1a6e5 30 30 31 29 3c 3c 31 36 29 3b 0a 20 20 20 20 74  001)<<16);.    t
1a6e6 65 73 74 63 61 73 65 28 20 73 7a 3c 3d 33 32 37  estcase( sz<=327
1a6e7 36 38 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  68 );.    testca
1a6e8 73 65 28 20 73 7a 3e 3d 36 35 35 33 36 20 29 3b  se( sz>=65536 );
1a6e9 0a 20 20 20 20 69 4f 66 66 73 65 74 20 3d 20 77  .    iOffset = w
1a6ea 61 6c 46 72 61 6d 65 4f 66 66 73 65 74 28 69 52  alFrameOffset(iR
1a6eb 65 61 64 2c 20 73 7a 29 20 2b 20 57 41 4c 5f 46  ead, sz) + WAL_F
1a6ec 52 41 4d 45 5f 48 44 52 53 49 5a 45 3b 0a 20 20  RAME_HDRSIZE;.  
1a6ed 20 20 2a 70 49 6e 57 61 6c 20 3d 20 31 3b 0a 20    *pInWal = 1;. 
1a6ee 20 20 20 2f 2a 20 74 65 73 74 63 61 73 65 28 20     /* testcase( 
1a6ef 49 53 5f 42 49 47 5f 49 4e 54 28 69 4f 66 66 73  IS_BIG_INT(iOffs
1a6f0 65 74 29 20 29 3b 20 2f 2f 20 72 65 71 75 69 72  et) ); // requir
1a6f1 65 73 20 61 20 34 47 69 42 20 57 41 4c 20 2a 2f  es a 4GiB WAL */
1a6f2 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
1a6f3 74 65 33 4f 73 52 65 61 64 28 70 57 61 6c 2d 3e  te3OsRead(pWal->
1a6f4 70 57 61 6c 46 64 2c 20 70 4f 75 74 2c 20 28 6e  pWalFd, pOut, (n
1a6f5 4f 75 74 3e 73 7a 20 3f 20 73 7a 20 3a 20 6e 4f  Out>sz ? sz : nO
1a6f6 75 74 29 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20  ut), iOffset);. 
1a6f7 20 7d 0a 0a 20 20 2a 70 49 6e 57 61 6c 20 3d 20   }..  *pInWal = 
1a6f8 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  0;.  return SQLI
1a6f9 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 20 0a 2a  TE_OK;.}.../* .*
1a6fa 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a  * Return the siz
1a6fb 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
1a6fc 65 20 69 6e 20 70 61 67 65 73 20 28 6f 72 20 7a  e in pages (or z
1a6fd 65 72 6f 2c 20 69 66 20 75 6e 6b 6e 6f 77 6e 29  ero, if unknown)
1a6fe 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a6ff 41 54 45 20 50 67 6e 6f 20 73 71 6c 69 74 65 33  ATE Pgno sqlite3
1a700 57 61 6c 44 62 73 69 7a 65 28 57 61 6c 20 2a 70  WalDbsize(Wal *p
1a701 57 61 6c 29 7b 0a 20 20 69 66 28 20 70 57 61 6c  Wal){.  if( pWal
1a702 20 26 26 20 41 4c 57 41 59 53 28 70 57 61 6c 2d   && ALWAYS(pWal-
1a703 3e 72 65 61 64 4c 6f 63 6b 3e 3d 30 29 20 29 7b  >readLock>=0) ){
1a704 0a 20 20 20 20 72 65 74 75 72 6e 20 70 57 61 6c  .    return pWal
1a705 2d 3e 68 64 72 2e 6e 50 61 67 65 3b 0a 20 20 7d  ->hdr.nPage;.  }
1a706 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
1a707 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ./* .** This fun
1a708 63 74 69 6f 6e 20 73 74 61 72 74 73 20 61 20 77  ction starts a w
1a709 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
1a70a 20 6f 6e 20 74 68 65 20 57 41 4c 2e 0a 2a 2a 0a   on the WAL..**.
1a70b 2a 2a 20 41 20 72 65 61 64 20 74 72 61 6e 73 61  ** A read transa
1a70c 63 74 69 6f 6e 20 6d 75 73 74 20 68 61 76 65 20  ction must have 
1a70d 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73 74 61  already been sta
1a70e 72 74 65 64 20 62 79 20 61 20 70 72 69 6f 72 20  rted by a prior 
1a70f 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  call.** to sqlit
1a710 65 33 57 61 6c 42 65 67 69 6e 52 65 61 64 54 72  e3WalBeginReadTr
1a711 61 6e 73 61 63 74 69 6f 6e 28 29 2e 0a 2a 2a 0a  ansaction()..**.
1a712 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  ** If another th
1a713 72 65 61 64 20 6f 72 20 70 72 6f 63 65 73 73 20  read or process 
1a714 68 61 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  has written into
1a715 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69   the database si
1a716 6e 63 65 0a 2a 2a 20 74 68 65 20 72 65 61 64 20  nce.** the read 
1a717 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20  transaction was 
1a718 73 74 61 72 74 65 64 2c 20 74 68 65 6e 20 69 74  started, then it
1a719 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
1a71a 20 66 6f 72 20 74 68 69 73 0a 2a 2a 20 74 68 72   for this.** thr
1a71b 65 61 64 20 74 6f 20 77 72 69 74 65 20 61 73 20  ead to write as 
1a71c 64 6f 69 6e 67 20 73 6f 20 77 6f 75 6c 64 20 63  doing so would c
1a71d 61 75 73 65 20 61 20 66 6f 72 6b 2e 20 20 53 6f  ause a fork.  So
1a71e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
1a71f 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
1a720 42 55 53 59 20 69 6e 20 74 68 61 74 20 63 61 73  BUSY in that cas
1a721 65 20 61 6e 64 20 6e 6f 20 77 72 69 74 65 20 74  e and no write t
1a722 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74  ransaction is st
1a723 61 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  arted..**.** The
1a724 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 61  re can only be a
1a725 20 73 69 6e 67 6c 65 20 77 72 69 74 65 72 20 61   single writer a
1a726 63 74 69 76 65 20 61 74 20 61 20 74 69 6d 65 2e  ctive at a time.
1a727 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a728 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61  TE int sqlite3Wa
1a729 6c 42 65 67 69 6e 57 72 69 74 65 54 72 61 6e 73  lBeginWriteTrans
1a72a 61 63 74 69 6f 6e 28 57 61 6c 20 2a 70 57 61 6c  action(Wal *pWal
1a72b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
1a72c 2f 2a 20 43 61 6e 6e 6f 74 20 73 74 61 72 74 20  /* Cannot start 
1a72d 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  a write transact
1a72e 69 6f 6e 20 77 69 74 68 6f 75 74 20 66 69 72 73  ion without firs
1a72f 74 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 61 64  t holding a read
1a730 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  .  ** transactio
1a731 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  n. */.  assert( 
1a732 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 3e 3d  pWal->readLock>=
1a733 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 57 61 6c  0 );..  if( pWal
1a734 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20  ->readOnly ){.  
1a735 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1a736 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20  READONLY;.  }.. 
1a737 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 77 72 69   /* Only one wri
1a738 74 65 72 20 61 6c 6c 6f 77 65 64 20 61 74 20 61  ter allowed at a
1a739 20 74 69 6d 65 2e 20 20 47 65 74 20 74 68 65 20   time.  Get the 
1a73a 77 72 69 74 65 20 6c 6f 63 6b 2e 20 20 52 65 74  write lock.  Ret
1a73b 75 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f  urn.  ** SQLITE_
1a73c 42 55 53 59 20 69 66 20 75 6e 61 62 6c 65 2e 0a  BUSY if unable..
1a73d 20 20 2a 2f 0a 20 20 72 63 20 3d 20 77 61 6c 4c    */.  rc = walL
1a73e 6f 63 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61  ockExclusive(pWa
1a73f 6c 2c 20 57 41 4c 5f 57 52 49 54 45 5f 4c 4f 43  l, WAL_WRITE_LOC
1a740 4b 2c 20 31 29 3b 0a 20 20 69 66 28 20 72 63 20  K, 1);.  if( rc 
1a741 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
1a742 3b 0a 20 20 7d 0a 20 20 70 57 61 6c 2d 3e 77 72  ;.  }.  pWal->wr
1a743 69 74 65 4c 6f 63 6b 20 3d 20 31 3b 0a 0a 20 20  iteLock = 1;..  
1a744 2f 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 63 6f  /* If another co
1a745 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 77 72 69  nnection has wri
1a746 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61  tten to the data
1a747 62 61 73 65 20 66 69 6c 65 20 73 69 6e 63 65 20  base file since 
1a748 74 68 65 0a 20 20 2a 2a 20 74 69 6d 65 20 74 68  the.  ** time th
1a749 65 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69  e read transacti
1a74a 6f 6e 20 6f 6e 20 74 68 69 73 20 63 6f 6e 6e 65  on on this conne
1a74b 63 74 69 6f 6e 20 77 61 73 20 73 74 61 72 74 65  ction was starte
1a74c 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65  d, then.  ** the
1a74d 20 77 72 69 74 65 20 69 73 20 64 69 73 61 6c 6c   write is disall
1a74e 6f 77 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  owed..  */.  if(
1a74f 20 6d 65 6d 63 6d 70 28 26 70 57 61 6c 2d 3e 68   memcmp(&pWal->h
1a750 64 72 2c 20 28 76 6f 69 64 20 2a 29 77 61 6c 49  dr, (void *)walI
1a751 6e 64 65 78 48 64 72 28 70 57 61 6c 29 2c 20 73  ndexHdr(pWal), s
1a752 69 7a 65 6f 66 28 57 61 6c 49 6e 64 65 78 48 64  izeof(WalIndexHd
1a753 72 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 77 61  r))!=0 ){.    wa
1a754 6c 55 6e 6c 6f 63 6b 45 78 63 6c 75 73 69 76 65  lUnlockExclusive
1a755 28 70 57 61 6c 2c 20 57 41 4c 5f 57 52 49 54 45  (pWal, WAL_WRITE
1a756 5f 4c 4f 43 4b 2c 20 31 29 3b 0a 20 20 20 20 70  _LOCK, 1);.    p
1a757 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 20 3d  Wal->writeLock =
1a758 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   0;.    rc = SQL
1a759 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a 20  ITE_BUSY;.  }.. 
1a75a 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1a75b 2a 0a 2a 2a 20 45 6e 64 20 61 20 77 72 69 74 65  *.** End a write
1a75c 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54   transaction.  T
1a75d 68 65 20 63 6f 6d 6d 69 74 20 68 61 73 20 61 6c  he commit has al
1a75e 72 65 61 64 79 20 62 65 65 6e 20 64 6f 6e 65 2e  ready been done.
1a75f 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e    This.** routin
1a760 65 20 6d 65 72 65 6c 79 20 72 65 6c 65 61 73 65  e merely release
1a761 73 20 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53  s the lock..*/.S
1a762 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1a763 74 20 73 71 6c 69 74 65 33 57 61 6c 45 6e 64 57  t sqlite3WalEndW
1a764 72 69 74 65 54 72 61 6e 73 61 63 74 69 6f 6e 28  riteTransaction(
1a765 57 61 6c 20 2a 70 57 61 6c 29 7b 0a 20 20 69 66  Wal *pWal){.  if
1a766 28 20 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63  ( pWal->writeLoc
1a767 6b 20 29 7b 0a 20 20 20 20 77 61 6c 55 6e 6c 6f  k ){.    walUnlo
1a768 63 6b 45 78 63 6c 75 73 69 76 65 28 70 57 61 6c  ckExclusive(pWal
1a769 2c 20 57 41 4c 5f 57 52 49 54 45 5f 4c 4f 43 4b  , WAL_WRITE_LOCK
1a76a 2c 20 31 29 3b 0a 20 20 20 20 70 57 61 6c 2d 3e  , 1);.    pWal->
1a76b 77 72 69 74 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20  writeLock = 0;. 
1a76c 20 20 20 70 57 61 6c 2d 3e 74 72 75 6e 63 61 74     pWal->truncat
1a76d 65 4f 6e 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20  eOnCommit = 0;. 
1a76e 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
1a76f 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
1a770 49 66 20 61 6e 79 20 64 61 74 61 20 68 61 73 20  If any data has 
1a771 62 65 65 6e 20 77 72 69 74 74 65 6e 20 28 62 75  been written (bu
1a772 74 20 6e 6f 74 20 63 6f 6d 6d 69 74 74 65 64 29  t not committed)
1a773 20 74 6f 20 74 68 65 20 6c 6f 67 20 66 69 6c 65   to the log file
1a774 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69  , this.** functi
1a775 6f 6e 20 6d 6f 76 65 73 20 74 68 65 20 77 72 69  on moves the wri
1a776 74 65 2d 70 6f 69 6e 74 65 72 20 62 61 63 6b 20  te-pointer back 
1a777 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  to the start of 
1a778 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  the transaction.
1a779 0a 2a 2a 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61  .**.** Additiona
1a77a 6c 6c 79 2c 20 74 68 65 20 63 61 6c 6c 62 61 63  lly, the callbac
1a77b 6b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e  k function is in
1a77c 76 6f 6b 65 64 20 66 6f 72 20 65 61 63 68 20 66  voked for each f
1a77d 72 61 6d 65 20 77 72 69 74 74 65 6e 0a 2a 2a 20  rame written.** 
1a77e 74 6f 20 74 68 65 20 57 41 4c 20 73 69 6e 63 65  to the WAL since
1a77f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
1a780 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49  e transaction. I
1a781 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72  f the callback r
1a782 65 74 75 72 6e 73 0a 2a 2a 20 6f 74 68 65 72 20  eturns.** other 
1a783 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 2c 20  than SQLITE_OK, 
1a784 69 74 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65  it is not invoke
1a785 64 20 61 67 61 69 6e 20 61 6e 64 20 74 68 65 20  d again and the 
1a786 65 72 72 6f 72 20 63 6f 64 65 20 69 73 0a 2a 2a  error code is.**
1a787 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
1a788 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f   caller..**.** O
1a789 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65  therwise, if the
1a78a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
1a78b 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 72 65 74 75  on does not retu
1a78c 72 6e 20 61 6e 20 65 72 72 6f 72 2c 20 74 68 69  rn an error, thi
1a78d 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65  s.** function re
1a78e 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e  turns SQLITE_OK.
1a78f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a790 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61  TE int sqlite3Wa
1a791 6c 55 6e 64 6f 28 57 61 6c 20 2a 70 57 61 6c 2c  lUndo(Wal *pWal,
1a792 20 69 6e 74 20 28 2a 78 55 6e 64 6f 29 28 76 6f   int (*xUndo)(vo
1a793 69 64 20 2a 2c 20 50 67 6e 6f 29 2c 20 76 6f 69  id *, Pgno), voi
1a794 64 20 2a 70 55 6e 64 6f 43 74 78 29 7b 0a 20 20  d *pUndoCtx){.  
1a795 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1a796 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53  OK;.  if( ALWAYS
1a797 28 70 57 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b  (pWal->writeLock
1a798 29 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4d  ) ){.    Pgno iM
1a799 61 78 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 6d  ax = pWal->hdr.m
1a79a 78 46 72 61 6d 65 3b 0a 20 20 20 20 50 67 6e 6f  xFrame;.    Pgno
1a79b 20 69 46 72 61 6d 65 3b 0a 20 20 0a 20 20 20 20   iFrame;.  .    
1a79c 2f 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 63  /* Restore the c
1a79d 6c 69 65 6e 74 73 20 63 61 63 68 65 20 6f 66 20  lients cache of 
1a79e 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 68 65  the wal-index he
1a79f 61 64 65 72 20 74 6f 20 74 68 65 20 73 74 61 74  ader to the stat
1a7a0 65 20 69 74 0a 20 20 20 20 2a 2a 20 77 61 73 20  e it.    ** was 
1a7a1 69 6e 20 62 65 66 6f 72 65 20 74 68 65 20 63 6c  in before the cl
1a7a2 69 65 6e 74 20 62 65 67 61 6e 20 77 72 69 74 69  ient began writi
1a7a3 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ng to the databa
1a7a4 73 65 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  se. .    */.    
1a7a5 6d 65 6d 63 70 79 28 26 70 57 61 6c 2d 3e 68 64  memcpy(&pWal->hd
1a7a6 72 2c 20 28 76 6f 69 64 20 2a 29 77 61 6c 49 6e  r, (void *)walIn
1a7a7 64 65 78 48 64 72 28 70 57 61 6c 29 2c 20 73 69  dexHdr(pWal), si
1a7a8 7a 65 6f 66 28 57 61 6c 49 6e 64 65 78 48 64 72  zeof(WalIndexHdr
1a7a9 29 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 46 72  ));..    for(iFr
1a7aa 61 6d 65 3d 70 57 61 6c 2d 3e 68 64 72 2e 6d 78  ame=pWal->hdr.mx
1a7ab 46 72 61 6d 65 2b 31 3b 20 0a 20 20 20 20 20 20  Frame+1; .      
1a7ac 20 20 41 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c    ALWAYS(rc==SQL
1a7ad 49 54 45 5f 4f 4b 29 20 26 26 20 69 46 72 61 6d  ITE_OK) && iFram
1a7ae 65 3c 3d 69 4d 61 78 3b 20 0a 20 20 20 20 20 20  e<=iMax; .      
1a7af 20 20 69 46 72 61 6d 65 2b 2b 0a 20 20 20 20 29    iFrame++.    )
1a7b0 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20  {.      /* This 
1a7b1 63 61 6c 6c 20 63 61 6e 6e 6f 74 20 66 61 69 6c  call cannot fail
1a7b2 2e 20 55 6e 6c 65 73 73 20 74 68 65 20 70 61 67  . Unless the pag
1a7b3 65 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20  e for which the 
1a7b4 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 20 20  page number.    
1a7b5 20 20 2a 2a 20 69 73 20 70 61 73 73 65 64 20 61    ** is passed a
1a7b6 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
1a7b7 75 6d 65 6e 74 20 69 73 20 28 61 29 20 69 6e 20  ument is (a) in 
1a7b8 74 68 65 20 63 61 63 68 65 20 61 6e 64 20 0a 20  the cache and . 
1a7b9 20 20 20 20 20 2a 2a 20 28 62 29 20 68 61 73 20       ** (b) has 
1a7ba 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72  an outstanding r
1a7bb 65 66 65 72 65 6e 63 65 2c 20 74 68 65 6e 20 78  eference, then x
1a7bc 55 6e 64 6f 20 69 73 20 65 69 74 68 65 72 20 61  Undo is either a
1a7bd 20 6e 6f 2d 6f 70 0a 20 20 20 20 20 20 2a 2a 20   no-op.      ** 
1a7be 28 69 66 20 28 61 29 20 69 73 20 66 61 6c 73 65  (if (a) is false
1a7bf 29 20 6f 72 20 73 69 6d 70 6c 79 20 65 78 70 65  ) or simply expe
1a7c0 6c 73 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d  ls the page from
1a7c1 20 74 68 65 20 63 61 63 68 65 20 28 69 66 20 28   the cache (if (
1a7c2 62 29 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 66  b).      ** is f
1a7c3 61 6c 73 65 29 2e 0a 20 20 20 20 20 20 2a 2a 0a  alse)..      **.
1a7c4 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20        ** If the 
1a7c5 75 70 70 65 72 20 6c 61 79 65 72 20 69 73 20 64  upper layer is d
1a7c6 6f 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c  oing a rollback,
1a7c7 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65   it is guarantee
1a7c8 64 20 74 68 61 74 20 74 68 65 72 65 0a 20 20 20  d that there.   
1a7c9 20 20 20 2a 2a 20 61 72 65 20 6e 6f 20 6f 75 74     ** are no out
1a7ca 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e  standing referen
1a7cb 63 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 20  ces to any page 
1a7cc 6f 74 68 65 72 20 74 68 61 6e 20 70 61 67 65 20  other than page 
1a7cd 31 2e 20 41 6e 64 0a 20 20 20 20 20 20 2a 2a 20  1. And.      ** 
1a7ce 70 61 67 65 20 31 20 69 73 20 6e 65 76 65 72 20  page 1 is never 
1a7cf 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6c  written to the l
1a7d0 6f 67 20 75 6e 74 69 6c 20 74 68 65 20 74 72 61  og until the tra
1a7d1 6e 73 61 63 74 69 6f 6e 20 69 73 0a 20 20 20 20  nsaction is.    
1a7d2 20 20 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20    ** committed. 
1a7d3 41 73 20 61 20 72 65 73 75 6c 74 2c 20 74 68 65  As a result, the
1a7d4 20 63 61 6c 6c 20 74 6f 20 78 55 6e 64 6f 20 6d   call to xUndo m
1a7d5 61 79 20 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 20  ay not fail..   
1a7d6 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65     */.      asse
1a7d7 72 74 28 20 77 61 6c 46 72 61 6d 65 50 67 6e 6f  rt( walFramePgno
1a7d8 28 70 57 61 6c 2c 20 69 46 72 61 6d 65 29 21 3d  (pWal, iFrame)!=
1a7d9 31 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  1 );.      rc = 
1a7da 78 55 6e 64 6f 28 70 55 6e 64 6f 43 74 78 2c 20  xUndo(pUndoCtx, 
1a7db 77 61 6c 46 72 61 6d 65 50 67 6e 6f 28 70 57 61  walFramePgno(pWa
1a7dc 6c 2c 20 69 46 72 61 6d 65 29 29 3b 0a 20 20 20  l, iFrame));.   
1a7dd 20 7d 0a 20 20 20 20 69 66 28 20 69 4d 61 78 21   }.    if( iMax!
1a7de 3d 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61  =pWal->hdr.mxFra
1a7df 6d 65 20 29 20 77 61 6c 43 6c 65 61 6e 75 70 48  me ) walCleanupH
1a7e0 61 73 68 28 70 57 61 6c 29 3b 0a 20 20 7d 0a 20  ash(pWal);.  }. 
1a7e1 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
1a7e2 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 65 74 75  ITE_OK );.  retu
1a7e3 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  rn rc;.}../* .**
1a7e4 20 41 72 67 75 6d 65 6e 74 20 61 57 61 6c 44 61   Argument aWalDa
1a7e5 74 61 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f  ta must point to
1a7e6 20 61 6e 20 61 72 72 61 79 20 6f 66 20 57 41 4c   an array of WAL
1a7e7 5f 53 41 56 45 50 4f 49 4e 54 5f 4e 44 41 54 41  _SAVEPOINT_NDATA
1a7e8 20 75 33 32 20 0a 2a 2a 20 76 61 6c 75 65 73 2e   u32 .** values.
1a7e9 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 70   This function p
1a7ea 6f 70 75 6c 61 74 65 73 20 74 68 65 20 61 72 72  opulates the arr
1a7eb 61 79 20 77 69 74 68 20 76 61 6c 75 65 73 20 72  ay with values r
1a7ec 65 71 75 69 72 65 64 20 74 6f 20 0a 2a 2a 20 22  equired to .** "
1a7ed 72 6f 6c 6c 62 61 63 6b 22 20 74 68 65 20 77 72  rollback" the wr
1a7ee 69 74 65 20 70 6f 73 69 74 69 6f 6e 20 6f 66 20  ite position of 
1a7ef 74 68 65 20 57 41 4c 20 68 61 6e 64 6c 65 20 62  the WAL handle b
1a7f0 61 63 6b 20 74 6f 20 74 68 65 20 63 75 72 72 65  ack to the curre
1a7f1 6e 74 20 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20  nt .** point in 
1a7f2 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 73  the event of a s
1a7f3 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63  avepoint rollbac
1a7f4 6b 20 28 76 69 61 20 57 61 6c 53 61 76 65 70 6f  k (via WalSavepo
1a7f5 69 6e 74 55 6e 64 6f 28 29 29 2e 0a 2a 2f 0a 53  intUndo())..*/.S
1a7f6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1a7f7 69 64 20 73 71 6c 69 74 65 33 57 61 6c 53 61 76  id sqlite3WalSav
1a7f8 65 70 6f 69 6e 74 28 57 61 6c 20 2a 70 57 61 6c  epoint(Wal *pWal
1a7f9 2c 20 75 33 32 20 2a 61 57 61 6c 44 61 74 61 29  , u32 *aWalData)
1a7fa 7b 0a 20 20 61 73 73 65 72 74 28 20 70 57 61 6c  {.  assert( pWal
1a7fb 2d 3e 77 72 69 74 65 4c 6f 63 6b 20 29 3b 0a 20  ->writeLock );. 
1a7fc 20 61 57 61 6c 44 61 74 61 5b 30 5d 20 3d 20 70   aWalData[0] = p
1a7fd 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65  Wal->hdr.mxFrame
1a7fe 3b 0a 20 20 61 57 61 6c 44 61 74 61 5b 31 5d 20  ;.  aWalData[1] 
1a7ff 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46 72 61  = pWal->hdr.aFra
1a800 6d 65 43 6b 73 75 6d 5b 30 5d 3b 0a 20 20 61 57  meCksum[0];.  aW
1a801 61 6c 44 61 74 61 5b 32 5d 20 3d 20 70 57 61 6c  alData[2] = pWal
1a802 2d 3e 68 64 72 2e 61 46 72 61 6d 65 43 6b 73 75  ->hdr.aFrameCksu
1a803 6d 5b 31 5d 3b 0a 20 20 61 57 61 6c 44 61 74 61  m[1];.  aWalData
1a804 5b 33 5d 20 3d 20 70 57 61 6c 2d 3e 6e 43 6b 70  [3] = pWal->nCkp
1a805 74 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 4d 6f 76  t;.}../* .** Mov
1a806 65 20 74 68 65 20 77 72 69 74 65 20 70 6f 73 69  e the write posi
1a807 74 69 6f 6e 20 6f 66 20 74 68 65 20 57 41 4c 20  tion of the WAL 
1a808 62 61 63 6b 20 74 6f 20 74 68 65 20 70 6f 69 6e  back to the poin
1a809 74 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 0a  t identified by.
1a80a 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  ** the values in
1a80b 20 74 68 65 20 61 57 61 6c 44 61 74 61 5b 5d 20   the aWalData[] 
1a80c 61 72 72 61 79 2e 20 61 57 61 6c 44 61 74 61 20  array. aWalData 
1a80d 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e  must point to an
1a80e 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 57 41 4c   array.** of WAL
1a80f 5f 53 41 56 45 50 4f 49 4e 54 5f 4e 44 41 54 41  _SAVEPOINT_NDATA
1a810 20 75 33 32 20 76 61 6c 75 65 73 20 74 68 61 74   u32 values that
1a811 20 68 61 73 20 62 65 65 6e 20 70 72 65 76 69 6f   has been previo
1a812 75 73 6c 79 20 70 6f 70 75 6c 61 74 65 64 0a 2a  usly populated.*
1a813 2a 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 57  * by a call to W
1a814 61 6c 53 61 76 65 70 6f 69 6e 74 28 29 2e 0a 2a  alSavepoint()..*
1a815 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a816 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 53   int sqlite3WalS
1a817 61 76 65 70 6f 69 6e 74 55 6e 64 6f 28 57 61 6c  avepointUndo(Wal
1a818 20 2a 70 57 61 6c 2c 20 75 33 32 20 2a 61 57 61   *pWal, u32 *aWa
1a819 6c 44 61 74 61 29 7b 0a 20 20 69 6e 74 20 72 63  lData){.  int rc
1a81a 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
1a81b 20 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 77   assert( pWal->w
1a81c 72 69 74 65 4c 6f 63 6b 20 29 3b 0a 20 20 61 73  riteLock );.  as
1a81d 73 65 72 74 28 20 61 57 61 6c 44 61 74 61 5b 33  sert( aWalData[3
1a81e 5d 21 3d 70 57 61 6c 2d 3e 6e 43 6b 70 74 20 7c  ]!=pWal->nCkpt |
1a81f 7c 20 61 57 61 6c 44 61 74 61 5b 30 5d 3c 3d 70  | aWalData[0]<=p
1a820 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65  Wal->hdr.mxFrame
1a821 20 29 3b 0a 0a 20 20 69 66 28 20 61 57 61 6c 44   );..  if( aWalD
1a822 61 74 61 5b 33 5d 21 3d 70 57 61 6c 2d 3e 6e 43  ata[3]!=pWal->nC
1a823 6b 70 74 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  kpt ){.    /* Th
1a824 69 73 20 73 61 76 65 70 6f 69 6e 74 20 77 61 73  is savepoint was
1a825 20 6f 70 65 6e 65 64 20 69 6d 6d 65 64 69 61 74   opened immediat
1a826 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 77 72  ely after the wr
1a827 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a  ite-transaction.
1a828 20 20 20 20 2a 2a 20 77 61 73 20 73 74 61 72 74      ** was start
1a829 65 64 2e 20 52 69 67 68 74 20 61 66 74 65 72 20  ed. Right after 
1a82a 74 68 61 74 2c 20 74 68 65 20 77 72 69 74 65 72  that, the writer
1a82b 20 64 65 63 69 64 65 64 20 74 6f 20 77 72 61 70   decided to wrap
1a82c 20 61 72 6f 75 6e 64 0a 20 20 20 20 2a 2a 20 74   around.    ** t
1a82d 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  o the start of t
1a82e 68 65 20 6c 6f 67 2e 20 55 70 64 61 74 65 20 74  he log. Update t
1a82f 68 65 20 73 61 76 65 70 6f 69 6e 74 20 76 61 6c  he savepoint val
1a830 75 65 73 20 74 6f 20 6d 61 74 63 68 2e 0a 20 20  ues to match..  
1a831 20 20 2a 2f 0a 20 20 20 20 61 57 61 6c 44 61 74    */.    aWalDat
1a832 61 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 61 57  a[0] = 0;.    aW
1a833 61 6c 44 61 74 61 5b 33 5d 20 3d 20 70 57 61 6c  alData[3] = pWal
1a834 2d 3e 6e 43 6b 70 74 3b 0a 20 20 7d 0a 0a 20 20  ->nCkpt;.  }..  
1a835 69 66 28 20 61 57 61 6c 44 61 74 61 5b 30 5d 3c  if( aWalData[0]<
1a836 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d  pWal->hdr.mxFram
1a837 65 20 29 7b 0a 20 20 20 20 70 57 61 6c 2d 3e 68  e ){.    pWal->h
1a838 64 72 2e 6d 78 46 72 61 6d 65 20 3d 20 61 57 61  dr.mxFrame = aWa
1a839 6c 44 61 74 61 5b 30 5d 3b 0a 20 20 20 20 70 57  lData[0];.    pW
1a83a 61 6c 2d 3e 68 64 72 2e 61 46 72 61 6d 65 43 6b  al->hdr.aFrameCk
1a83b 73 75 6d 5b 30 5d 20 3d 20 61 57 61 6c 44 61 74  sum[0] = aWalDat
1a83c 61 5b 31 5d 3b 0a 20 20 20 20 70 57 61 6c 2d 3e  a[1];.    pWal->
1a83d 68 64 72 2e 61 46 72 61 6d 65 43 6b 73 75 6d 5b  hdr.aFrameCksum[
1a83e 31 5d 20 3d 20 61 57 61 6c 44 61 74 61 5b 32 5d  1] = aWalData[2]
1a83f 3b 0a 20 20 20 20 77 61 6c 43 6c 65 61 6e 75 70  ;.    walCleanup
1a840 48 61 73 68 28 70 57 61 6c 29 3b 0a 20 20 7d 0a  Hash(pWal);.  }.
1a841 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1a842 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
1a843 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
1a844 6a 75 73 74 20 62 65 66 6f 72 65 20 77 72 69 74  just before writ
1a845 69 6e 67 20 61 20 73 65 74 20 6f 66 20 66 72 61  ing a set of fra
1a846 6d 65 73 20 74 6f 20 74 68 65 20 6c 6f 67 0a 2a  mes to the log.*
1a847 2a 20 66 69 6c 65 20 28 73 65 65 20 73 71 6c 69  * file (see sqli
1a848 74 65 33 57 61 6c 46 72 61 6d 65 73 28 29 29 2e  te3WalFrames()).
1a849 20 49 74 20 63 68 65 63 6b 73 20 74 6f 20 73 65   It checks to se
1a84a 65 20 69 66 2c 20 69 6e 73 74 65 61 64 20 6f 66  e if, instead of
1a84b 20 61 70 70 65 6e 64 69 6e 67 0a 2a 2a 20 74 6f   appending.** to
1a84c 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 67   the current log
1a84d 20 66 69 6c 65 2c 20 69 74 20 69 73 20 70 6f 73   file, it is pos
1a84e 73 69 62 6c 65 20 74 6f 20 6f 76 65 72 77 72 69  sible to overwri
1a84f 74 65 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  te the start of 
1a850 74 68 65 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20  the.** existing 
1a851 6c 6f 67 20 66 69 6c 65 20 77 69 74 68 20 74 68  log file with th
1a852 65 20 6e 65 77 20 66 72 61 6d 65 73 20 28 69 2e  e new frames (i.
1a853 65 2e 20 22 72 65 73 65 74 22 20 74 68 65 20 6c  e. "reset" the l
1a854 6f 67 29 2e 20 49 66 20 73 6f 2c 0a 2a 2a 20 69  og). If so,.** i
1a855 74 20 73 65 74 73 20 70 57 61 6c 2d 3e 68 64 72  t sets pWal->hdr
1a856 2e 6d 78 46 72 61 6d 65 20 74 6f 20 30 2e 20 4f  .mxFrame to 0. O
1a857 74 68 65 72 77 69 73 65 2c 20 70 57 61 6c 2d 3e  therwise, pWal->
1a858 68 64 72 2e 6d 78 46 72 61 6d 65 20 69 73 20 6c  hdr.mxFrame is l
1a859 65 66 74 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64  eft.** unchanged
1a85a 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  ..**.** SQLITE_O
1a85b 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  K is returned if
1a85c 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 65 6e 63   no error is enc
1a85d 6f 75 6e 74 65 72 65 64 20 28 72 65 67 61 72 64  ountered (regard
1a85e 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 0a  less of whether.
1a85f 2a 2a 20 6f 72 20 6e 6f 74 20 70 57 61 6c 2d 3e  ** or not pWal->
1a860 68 64 72 2e 6d 78 46 72 61 6d 65 20 69 73 20 6d  hdr.mxFrame is m
1a861 6f 64 69 66 69 65 64 29 2e 20 41 6e 20 53 51 4c  odified). An SQL
1a862 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  ite error code i
1a863 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 66  s returned.** if
1a864 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
1a865 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1a866 77 61 6c 52 65 73 74 61 72 74 4c 6f 67 28 57 61  walRestartLog(Wa
1a867 6c 20 2a 70 57 61 6c 29 7b 0a 20 20 69 6e 74 20  l *pWal){.  int 
1a868 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1a869 20 20 69 6e 74 20 63 6e 74 3b 0a 0a 20 20 69 66    int cnt;..  if
1a86a 28 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b  ( pWal->readLock
1a86b 3d 3d 30 20 29 7b 0a 20 20 20 20 76 6f 6c 61 74  ==0 ){.    volat
1a86c 69 6c 65 20 57 61 6c 43 6b 70 74 49 6e 66 6f 20  ile WalCkptInfo 
1a86d 2a 70 49 6e 66 6f 20 3d 20 77 61 6c 43 6b 70 74  *pInfo = walCkpt
1a86e 49 6e 66 6f 28 70 57 61 6c 29 3b 0a 20 20 20 20  Info(pWal);.    
1a86f 61 73 73 65 72 74 28 20 70 49 6e 66 6f 2d 3e 6e  assert( pInfo->n
1a870 42 61 63 6b 66 69 6c 6c 3d 3d 70 57 61 6c 2d 3e  Backfill==pWal->
1a871 68 64 72 2e 6d 78 46 72 61 6d 65 20 29 3b 0a 20  hdr.mxFrame );. 
1a872 20 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e 6e 42     if( pInfo->nB
1a873 61 63 6b 66 69 6c 6c 3e 30 20 29 7b 0a 20 20 20  ackfill>0 ){.   
1a874 20 20 20 75 33 32 20 73 61 6c 74 31 3b 0a 20 20     u32 salt1;.  
1a875 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64      sqlite3_rand
1a876 6f 6d 6e 65 73 73 28 34 2c 20 26 73 61 6c 74 31  omness(4, &salt1
1a877 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 61  );.      rc = wa
1a878 6c 4c 6f 63 6b 45 78 63 6c 75 73 69 76 65 28 70  lLockExclusive(p
1a879 57 61 6c 2c 20 57 41 4c 5f 52 45 41 44 5f 4c 4f  Wal, WAL_READ_LO
1a87a 43 4b 28 31 29 2c 20 57 41 4c 5f 4e 52 45 41 44  CK(1), WAL_NREAD
1a87b 45 52 2d 31 29 3b 0a 20 20 20 20 20 20 69 66 28  ER-1);.      if(
1a87c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1a87d 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
1a87e 61 6c 6c 20 72 65 61 64 65 72 73 20 61 72 65 20  all readers are 
1a87f 75 73 69 6e 67 20 57 41 4c 5f 52 45 41 44 5f 4c  using WAL_READ_L
1a880 4f 43 4b 28 30 29 20 28 69 6e 20 6f 74 68 65 72  OCK(0) (in other
1a881 20 77 6f 72 64 73 20 69 66 20 6e 6f 0a 20 20 20   words if no.   
1a882 20 20 20 20 20 2a 2a 20 72 65 61 64 65 72 73 20       ** readers 
1a883 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 75 73  are currently us
1a884 69 6e 67 20 74 68 65 20 57 41 4c 29 2c 20 74 68  ing the WAL), th
1a885 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  en the transacti
1a886 6f 6e 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 66  ons.        ** f
1a887 72 61 6d 65 73 20 77 69 6c 6c 20 6f 76 65 72 77  rames will overw
1a888 72 69 74 65 20 74 68 65 20 73 74 61 72 74 20 6f  rite the start o
1a889 66 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 6c  f the existing l
1a88a 6f 67 2e 20 55 70 64 61 74 65 20 74 68 65 0a 20  og. Update the. 
1a88b 20 20 20 20 20 20 20 2a 2a 20 77 61 6c 2d 69 6e         ** wal-in
1a88c 64 65 78 20 68 65 61 64 65 72 20 74 6f 20 72 65  dex header to re
1a88d 66 6c 65 63 74 20 74 68 69 73 2e 0a 20 20 20 20  flect this..    
1a88e 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
1a88f 2a 20 49 6e 20 74 68 65 6f 72 79 20 69 74 20 77  * In theory it w
1a890 6f 75 6c 64 20 62 65 20 4f 6b 20 74 6f 20 75 70  ould be Ok to up
1a891 64 61 74 65 20 74 68 65 20 63 61 63 68 65 20 6f  date the cache o
1a892 66 20 74 68 65 20 68 65 61 64 65 72 20 6f 6e 6c  f the header onl
1a893 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 74 20  y.        ** at 
1a894 74 68 69 73 20 70 6f 69 6e 74 2e 20 42 75 74 20  this point. But 
1a895 75 70 64 61 74 69 6e 67 20 74 68 65 20 61 63 74  updating the act
1a896 75 61 6c 20 77 61 6c 2d 69 6e 64 65 78 20 68 65  ual wal-index he
1a897 61 64 65 72 20 69 73 20 61 6c 73 6f 0a 20 20 20  ader is also.   
1a898 20 20 20 20 20 2a 2a 20 73 61 66 65 20 61 6e 64       ** safe and
1a899 20 6d 65 61 6e 73 20 74 68 65 72 65 20 69 73 20   means there is 
1a89a 6e 6f 20 73 70 65 63 69 61 6c 20 63 61 73 65 20  no special case 
1a89b 66 6f 72 20 73 71 6c 69 74 65 33 57 61 6c 55 6e  for sqlite3WalUn
1a89c 64 6f 28 29 0a 20 20 20 20 20 20 20 20 2a 2a 20  do().        ** 
1a89d 74 6f 20 68 61 6e 64 6c 65 20 69 66 20 74 68 69  to handle if thi
1a89e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  s transaction is
1a89f 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 20 20   rolled back..  
1a8a0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1a8a1 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
1a8a2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
1a8a3 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
1a8a4 20 20 20 20 20 20 75 33 32 20 2a 61 53 61 6c 74        u32 *aSalt
1a8a5 20 3d 20 70 57 61 6c 2d 3e 68 64 72 2e 61 53 61   = pWal->hdr.aSa
1a8a6 6c 74 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 67  lt;       /* Big
1a8a7 2d 65 6e 64 69 61 6e 20 73 61 6c 74 20 76 61 6c  -endian salt val
1a8a8 75 65 73 20 2a 2f 0a 0a 20 20 20 20 20 20 20 20  ues */..        
1a8a9 70 57 61 6c 2d 3e 6e 43 6b 70 74 2b 2b 3b 0a 20  pWal->nCkpt++;. 
1a8aa 20 20 20 20 20 20 20 70 57 61 6c 2d 3e 68 64 72         pWal->hdr
1a8ab 2e 6d 78 46 72 61 6d 65 20 3d 20 30 3b 0a 20 20  .mxFrame = 0;.  
1a8ac 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 75 74        sqlite3Put
1a8ad 34 62 79 74 65 28 28 75 38 2a 29 26 61 53 61 6c  4byte((u8*)&aSal
1a8ae 74 5b 30 5d 2c 20 31 20 2b 20 73 71 6c 69 74 65  t[0], 1 + sqlite
1a8af 33 47 65 74 34 62 79 74 65 28 28 75 38 2a 29 26  3Get4byte((u8*)&
1a8b0 61 53 61 6c 74 5b 30 5d 29 29 3b 0a 20 20 20 20  aSalt[0]));.    
1a8b1 20 20 20 20 61 53 61 6c 74 5b 31 5d 20 3d 20 73      aSalt[1] = s
1a8b2 61 6c 74 31 3b 0a 20 20 20 20 20 20 20 20 77 61  alt1;.        wa
1a8b3 6c 49 6e 64 65 78 57 72 69 74 65 48 64 72 28 70  lIndexWriteHdr(p
1a8b4 57 61 6c 29 3b 0a 20 20 20 20 20 20 20 20 70 49  Wal);.        pI
1a8b5 6e 66 6f 2d 3e 6e 42 61 63 6b 66 69 6c 6c 20 3d  nfo->nBackfill =
1a8b6 20 30 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 66   0;.        pInf
1a8b7 6f 2d 3e 61 52 65 61 64 4d 61 72 6b 5b 31 5d 20  o->aReadMark[1] 
1a8b8 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  = 0;.        for
1a8b9 28 69 3d 32 3b 20 69 3c 57 41 4c 5f 4e 52 45 41  (i=2; i<WAL_NREA
1a8ba 44 45 52 3b 20 69 2b 2b 29 20 70 49 6e 66 6f 2d  DER; i++) pInfo-
1a8bb 3e 61 52 65 61 64 4d 61 72 6b 5b 69 5d 20 3d 20  >aReadMark[i] = 
1a8bc 52 45 41 44 4d 41 52 4b 5f 4e 4f 54 5f 55 53 45  READMARK_NOT_USE
1a8bd 44 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  D;.        asser
1a8be 74 28 20 70 49 6e 66 6f 2d 3e 61 52 65 61 64 4d  t( pInfo->aReadM
1a8bf 61 72 6b 5b 30 5d 3d 3d 30 20 29 3b 0a 20 20 20  ark[0]==0 );.   
1a8c0 20 20 20 20 20 77 61 6c 55 6e 6c 6f 63 6b 45 78       walUnlockEx
1a8c1 63 6c 75 73 69 76 65 28 70 57 61 6c 2c 20 57 41  clusive(pWal, WA
1a8c2 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 31 29 2c 20  L_READ_LOCK(1), 
1a8c3 57 41 4c 5f 4e 52 45 41 44 45 52 2d 31 29 3b 0a  WAL_NREADER-1);.
1a8c4 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1a8c5 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc!=SQLITE_BUSY 
1a8c6 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
1a8c7 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
1a8c8 20 20 7d 0a 20 20 20 20 77 61 6c 55 6e 6c 6f 63    }.    walUnloc
1a8c9 6b 53 68 61 72 65 64 28 70 57 61 6c 2c 20 57 41  kShared(pWal, WA
1a8ca 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 30 29 29 3b  L_READ_LOCK(0));
1a8cb 0a 20 20 20 20 70 57 61 6c 2d 3e 72 65 61 64 4c  .    pWal->readL
1a8cc 6f 63 6b 20 3d 20 2d 31 3b 0a 20 20 20 20 63 6e  ock = -1;.    cn
1a8cd 74 20 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20  t = 0;.    do{. 
1a8ce 20 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64       int notUsed
1a8cf 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 61 6c  ;.      rc = wal
1a8d0 54 72 79 42 65 67 69 6e 52 65 61 64 28 70 57 61  TryBeginRead(pWa
1a8d1 6c 2c 20 26 6e 6f 74 55 73 65 64 2c 20 31 2c 20  l, &notUsed, 1, 
1a8d2 2b 2b 63 6e 74 29 3b 0a 20 20 20 20 7d 77 68 69  ++cnt);.    }whi
1a8d3 6c 65 28 20 72 63 3d 3d 57 41 4c 5f 52 45 54 52  le( rc==WAL_RETR
1a8d4 59 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Y );.    assert(
1a8d5 20 28 72 63 26 30 78 66 66 29 21 3d 53 51 4c 49   (rc&0xff)!=SQLI
1a8d6 54 45 5f 42 55 53 59 20 29 3b 20 2f 2a 20 42 55  TE_BUSY ); /* BU
1a8d7 53 59 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  SY not possible 
1a8d8 77 68 65 6e 20 75 73 65 57 61 6c 3d 3d 31 20 2a  when useWal==1 *
1a8d9 2f 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  /.    testcase( 
1a8da 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54  (rc&0xff)==SQLIT
1a8db 45 5f 49 4f 45 52 52 20 29 3b 0a 20 20 20 20 74  E_IOERR );.    t
1a8dc 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c  estcase( rc==SQL
1a8dd 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 29 3b 0a  ITE_PROTOCOL );.
1a8de 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63      testcase( rc
1a8df 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
1a8e0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1a8e1 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61  }../*.** Informa
1a8e2 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63  tion about the c
1a8e3 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20  urrent state of 
1a8e4 74 68 65 20 57 41 4c 20 66 69 6c 65 20 61 6e 64  the WAL file and
1a8e5 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 20 6e 65   where.** the ne
1a8e6 78 74 20 66 73 79 6e 63 20 73 68 6f 75 6c 64 20  xt fsync should 
1a8e7 6f 63 63 75 72 20 2d 20 70 61 73 73 65 64 20 66  occur - passed f
1a8e8 72 6f 6d 20 73 71 6c 69 74 65 33 57 61 6c 46 72  rom sqlite3WalFr
1a8e9 61 6d 65 73 28 29 20 69 6e 74 6f 0a 2a 2a 20 77  ames() into.** w
1a8ea 61 6c 57 72 69 74 65 54 6f 4c 6f 67 28 29 2e 0a  alWriteToLog()..
1a8eb 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
1a8ec 74 20 57 61 6c 57 72 69 74 65 72 20 7b 0a 20 20  t WalWriter {.  
1a8ed 57 61 6c 20 2a 70 57 61 6c 3b 20 20 20 20 20 20  Wal *pWal;      
1a8ee 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a8ef 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 57 41 4c  The complete WAL
1a8f0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a   information */.
1a8f1 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1a8f2 70 46 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f  pFd;           /
1a8f3 2a 20 54 68 65 20 57 41 4c 20 66 69 6c 65 20 74  * The WAL file t
1a8f4 6f 20 77 68 69 63 68 20 77 65 20 77 72 69 74 65  o which we write
1a8f5 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
1a8f6 74 36 34 20 69 53 79 6e 63 50 6f 69 6e 74 3b 20  t64 iSyncPoint; 
1a8f7 20 20 20 2f 2a 20 46 73 79 6e 63 20 61 74 20 74     /* Fsync at t
1a8f8 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20  his offset */.  
1a8f9 69 6e 74 20 73 79 6e 63 46 6c 61 67 73 3b 20 20  int syncFlags;  
1a8fa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a8fb 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20 66 73  Flags for the fs
1a8fc 79 6e 63 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50  ync */.  int szP
1a8fd 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
1a8fe 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
1a8ff 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 7d 20 57   one page */.} W
1a900 61 6c 57 72 69 74 65 72 3b 0a 0a 2f 2a 0a 2a 2a  alWriter;../*.**
1a901 20 57 72 69 74 65 20 69 41 6d 74 20 62 79 74 65   Write iAmt byte
1a902 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e 74  s of content int
1a903 6f 20 74 68 65 20 57 41 4c 20 66 69 6c 65 20 62  o the WAL file b
1a904 65 67 69 6e 6e 69 6e 67 20 61 74 20 69 4f 66 66  eginning at iOff
1a905 73 65 74 2e 0a 2a 2a 20 44 6f 20 61 20 73 79 6e  set..** Do a syn
1a906 63 20 77 68 65 6e 20 63 72 6f 73 73 69 6e 67 20  c when crossing 
1a907 74 68 65 20 70 2d 3e 69 53 79 6e 63 50 6f 69 6e  the p->iSyncPoin
1a908 74 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a  t boundary..**.*
1a909 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * In other words
1a90a 2c 20 69 66 20 69 53 79 6e 63 50 6f 69 6e 74 20  , if iSyncPoint 
1a90b 69 73 20 69 6e 20 62 65 74 77 65 65 6e 20 69 4f  is in between iO
1a90c 66 66 73 65 74 20 61 6e 64 20 69 4f 66 66 73 65  ffset and iOffse
1a90d 74 2b 69 41 6d 74 2c 0a 2a 2a 20 66 69 72 73 74  t+iAmt,.** first
1a90e 20 77 72 69 74 65 20 74 68 65 20 70 61 72 74 20   write the part 
1a90f 62 65 66 6f 72 65 20 69 53 79 6e 63 50 6f 69 6e  before iSyncPoin
1a910 74 2c 20 74 68 65 6e 20 73 79 6e 63 2c 20 74 68  t, then sync, th
1a911 65 6e 20 77 72 69 74 65 20 74 68 65 0a 2a 2a 20  en write the.** 
1a912 72 65 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rest..*/.static 
1a913 69 6e 74 20 77 61 6c 57 72 69 74 65 54 6f 4c 6f  int walWriteToLo
1a914 67 28 0a 20 20 57 61 6c 57 72 69 74 65 72 20 2a  g(.  WalWriter *
1a915 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
1a916 2f 2a 20 57 41 4c 20 74 6f 20 77 72 69 74 65 20  /* WAL to write 
1a917 74 6f 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43  to */.  void *pC
1a918 6f 6e 74 65 6e 74 2c 20 20 20 20 20 20 20 20 20  ontent,         
1a919 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 74 6f     /* Content to
1a91a 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20   be written */. 
1a91b 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20   int iAmt,      
1a91c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1a91d 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
1a91e 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c  o write */.  sql
1a91f 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 73  ite3_int64 iOffs
1a920 65 74 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74  et      /* Start
1a921 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69 73   writing at this
1a922 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20   offset */.){.  
1a923 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 69 4f  int rc;.  if( iO
1a924 66 66 73 65 74 3c 70 2d 3e 69 53 79 6e 63 50 6f  ffset<p->iSyncPo
1a925 69 6e 74 20 26 26 20 69 4f 66 66 73 65 74 2b 69  int && iOffset+i
1a926 41 6d 74 3e 3d 70 2d 3e 69 53 79 6e 63 50 6f 69  Amt>=p->iSyncPoi
1a927 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 46  nt ){.    int iF
1a928 69 72 73 74 41 6d 74 20 3d 20 28 69 6e 74 29 28  irstAmt = (int)(
1a929 70 2d 3e 69 53 79 6e 63 50 6f 69 6e 74 20 2d 20  p->iSyncPoint - 
1a92a 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 72 63  iOffset);.    rc
1a92b 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
1a92c 65 28 70 2d 3e 70 46 64 2c 20 70 43 6f 6e 74 65  e(p->pFd, pConte
1a92d 6e 74 2c 20 69 46 69 72 73 74 41 6d 74 2c 20 69  nt, iFirstAmt, i
1a92e 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 69 66 28  Offset);.    if(
1a92f 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
1a930 0a 20 20 20 20 69 4f 66 66 73 65 74 20 2b 3d 20  .    iOffset += 
1a931 69 46 69 72 73 74 41 6d 74 3b 0a 20 20 20 20 69  iFirstAmt;.    i
1a932 41 6d 74 20 2d 3d 20 69 46 69 72 73 74 41 6d 74  Amt -= iFirstAmt
1a933 3b 0a 20 20 20 20 70 43 6f 6e 74 65 6e 74 20 3d  ;.    pContent =
1a934 20 28 76 6f 69 64 2a 29 28 69 46 69 72 73 74 41   (void*)(iFirstA
1a935 6d 74 20 2b 20 28 63 68 61 72 2a 29 70 43 6f 6e  mt + (char*)pCon
1a936 74 65 6e 74 29 3b 0a 20 20 20 20 61 73 73 65 72  tent);.    asser
1a937 74 28 20 70 2d 3e 73 79 6e 63 46 6c 61 67 73 20  t( p->syncFlags 
1a938 26 20 28 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e  & (SQLITE_SYNC_N
1a939 4f 52 4d 41 4c 7c 53 51 4c 49 54 45 5f 53 59 4e  ORMAL|SQLITE_SYN
1a93a 43 5f 46 55 4c 4c 29 20 29 3b 0a 20 20 20 20 72  C_FULL) );.    r
1a93b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e  c = sqlite3OsSyn
1a93c 63 28 70 2d 3e 70 46 64 2c 20 70 2d 3e 73 79 6e  c(p->pFd, p->syn
1a93d 63 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28  cFlags);.    if(
1a93e 20 69 41 6d 74 3d 3d 30 20 7c 7c 20 72 63 20 29   iAmt==0 || rc )
1a93f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
1a940 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1a941 57 72 69 74 65 28 70 2d 3e 70 46 64 2c 20 70 43  Write(p->pFd, pC
1a942 6f 6e 74 65 6e 74 2c 20 69 41 6d 74 2c 20 69 4f  ontent, iAmt, iO
1a943 66 66 73 65 74 29 3b 0a 20 20 72 65 74 75 72 6e  ffset);.  return
1a944 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72   rc;.}../*.** Wr
1a945 69 74 65 20 6f 75 74 20 61 20 73 69 6e 67 6c 65  ite out a single
1a946 20 66 72 61 6d 65 20 6f 66 20 74 68 65 20 57 41   frame of the WA
1a947 4c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  L.*/.static int 
1a948 77 61 6c 57 72 69 74 65 4f 6e 65 46 72 61 6d 65  walWriteOneFrame
1a949 28 0a 20 20 57 61 6c 57 72 69 74 65 72 20 2a 70  (.  WalWriter *p
1a94a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1a94b 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 69 74  /* Where to writ
1a94c 65 20 74 68 65 20 66 72 61 6d 65 20 2a 2f 0a 20  e the frame */. 
1a94d 20 50 67 48 64 72 20 2a 70 50 61 67 65 2c 20 20   PgHdr *pPage,  
1a94e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a94f 54 68 65 20 70 61 67 65 20 6f 66 20 74 68 65 20  The page of the 
1a950 66 72 61 6d 65 20 74 6f 20 62 65 20 77 72 69 74  frame to be writ
1a951 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 72  ten */.  int nTr
1a952 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20  uncate,         
1a953 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 6d       /* The comm
1a954 69 74 20 66 6c 61 67 2e 20 20 55 73 75 61 6c 6c  it flag.  Usuall
1a955 79 20 30 2e 20 20 3e 30 20 66 6f 72 20 63 6f 6d  y 0.  >0 for com
1a956 6d 69 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  mit */.  sqlite3
1a957 5f 69 6e 74 36 34 20 69 4f 66 66 73 65 74 20 20  _int64 iOffset  
1a958 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66       /* Byte off
1a959 73 65 74 20 61 74 20 77 68 69 63 68 20 74 6f 20  set at which to 
1a95a 77 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  write */.){.  in
1a95b 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
1a95c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a95d 20 52 65 73 75 6c 74 20 63 6f 64 65 20 66 72 6f   Result code fro
1a95e 6d 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a  m subfunctions *
1a95f 2f 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b  /.  void *pData;
1a960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a961 20 20 20 20 2f 2a 20 44 61 74 61 20 61 63 74 75      /* Data actu
1a962 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 2a 2f 0a  ally written */.
1a963 20 20 75 38 20 61 46 72 61 6d 65 5b 57 41 4c 5f    u8 aFrame[WAL_
1a964 46 52 41 4d 45 5f 48 44 52 53 49 5a 45 5d 3b 20  FRAME_HDRSIZE]; 
1a965 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 61    /* Buffer to a
1a966 73 73 65 6d 62 6c 65 20 66 72 61 6d 65 2d 68 65  ssemble frame-he
1a967 61 64 65 72 20 69 6e 20 2a 2f 0a 23 69 66 20 64  ader in */.#if d
1a968 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41  efined(SQLITE_HA
1a969 53 5f 43 4f 44 45 43 29 0a 20 20 69 66 28 20 28  S_CODEC).  if( (
1a96a 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50  pData = sqlite3P
1a96b 61 67 65 72 43 6f 64 65 63 28 70 50 61 67 65 29  agerCodec(pPage)
1a96c 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  )==0 ) return SQ
1a96d 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 23 65 6c 73  LITE_NOMEM;.#els
1a96e 65 0a 20 20 70 44 61 74 61 20 3d 20 70 50 61 67  e.  pData = pPag
1a96f 65 2d 3e 70 44 61 74 61 3b 0a 23 65 6e 64 69 66  e->pData;.#endif
1a970 0a 20 20 77 61 6c 45 6e 63 6f 64 65 46 72 61 6d  .  walEncodeFram
1a971 65 28 70 2d 3e 70 57 61 6c 2c 20 70 50 61 67 65  e(p->pWal, pPage
1a972 2d 3e 70 67 6e 6f 2c 20 6e 54 72 75 6e 63 61 74  ->pgno, nTruncat
1a973 65 2c 20 70 44 61 74 61 2c 20 61 46 72 61 6d 65  e, pData, aFrame
1a974 29 3b 0a 20 20 72 63 20 3d 20 77 61 6c 57 72 69  );.  rc = walWri
1a975 74 65 54 6f 4c 6f 67 28 70 2c 20 61 46 72 61 6d  teToLog(p, aFram
1a976 65 2c 20 73 69 7a 65 6f 66 28 61 46 72 61 6d 65  e, sizeof(aFrame
1a977 29 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 69  ), iOffset);.  i
1a978 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1a979 63 3b 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68  c;.  /* Write th
1a97a 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20  e page data */. 
1a97b 20 72 63 20 3d 20 77 61 6c 57 72 69 74 65 54 6f   rc = walWriteTo
1a97c 4c 6f 67 28 70 2c 20 70 44 61 74 61 2c 20 70 2d  Log(p, pData, p-
1a97d 3e 73 7a 50 61 67 65 2c 20 69 4f 66 66 73 65 74  >szPage, iOffset
1a97e 2b 73 69 7a 65 6f 66 28 61 46 72 61 6d 65 29 29  +sizeof(aFrame))
1a97f 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1a980 0a 0a 2f 2a 20 0a 2a 2a 20 57 72 69 74 65 20 61  ../* .** Write a
1a981 20 73 65 74 20 6f 66 20 66 72 61 6d 65 73 20 74   set of frames t
1a982 6f 20 74 68 65 20 6c 6f 67 2e 20 54 68 65 20 63  o the log. The c
1a983 61 6c 6c 65 72 20 6d 75 73 74 20 68 6f 6c 64 20  aller must hold 
1a984 74 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 0a 2a  the write-lock.*
1a985 2a 20 6f 6e 20 74 68 65 20 6c 6f 67 20 66 69 6c  * on the log fil
1a986 65 20 28 6f 62 74 61 69 6e 65 64 20 75 73 69 6e  e (obtained usin
1a987 67 20 73 71 6c 69 74 65 33 57 61 6c 42 65 67 69  g sqlite3WalBegi
1a988 6e 57 72 69 74 65 54 72 61 6e 73 61 63 74 69 6f  nWriteTransactio
1a989 6e 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  n())..*/.SQLITE_
1a98a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1a98b 74 65 33 57 61 6c 46 72 61 6d 65 73 28 0a 20 20  te3WalFrames(.  
1a98c 57 61 6c 20 2a 70 57 61 6c 2c 20 20 20 20 20 20  Wal *pWal,      
1a98d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a98e 2f 2a 20 57 61 6c 20 68 61 6e 64 6c 65 20 74 6f  /* Wal handle to
1a98f 20 77 72 69 74 65 20 74 6f 20 2a 2f 0a 20 20 69   write to */.  i
1a990 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20  nt szPage,      
1a991 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a992 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 65 2d  * Database page-
1a993 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 2a 2f  size in bytes */
1a994 0a 20 20 50 67 48 64 72 20 2a 70 4c 69 73 74 2c  .  PgHdr *pList,
1a995 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a996 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69     /* List of di
1a997 72 74 79 20 70 61 67 65 73 20 74 6f 20 77 72 69  rty pages to wri
1a998 74 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 54 72  te */.  Pgno nTr
1a999 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20  uncate,         
1a99a 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
1a99b 61 73 65 20 73 69 7a 65 20 61 66 74 65 72 20 74  ase size after t
1a99c 68 69 73 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20  his commit */.  
1a99d 69 6e 74 20 69 73 43 6f 6d 6d 69 74 2c 20 20 20  int isCommit,   
1a99e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a99f 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
1a9a0 69 73 20 61 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20  is a commit */. 
1a9a1 20 69 6e 74 20 73 79 6e 63 5f 66 6c 61 67 73 20   int sync_flags 
1a9a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a9a3 20 2f 2a 20 46 6c 61 67 73 20 74 6f 20 70 61 73   /* Flags to pas
1a9a4 73 20 74 6f 20 4f 73 53 79 6e 63 28 29 20 28 6f  s to OsSync() (o
1a9a5 72 20 30 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  r 0) */.){.  int
1a9a6 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
1a9a7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a9a8 55 73 65 64 20 74 6f 20 63 61 74 63 68 20 72 65  Used to catch re
1a9a9 74 75 72 6e 20 63 6f 64 65 73 20 2a 2f 0a 20 20  turn codes */.  
1a9aa 75 33 32 20 69 46 72 61 6d 65 3b 20 20 20 20 20  u32 iFrame;     
1a9ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a9ac 2f 2a 20 4e 65 78 74 20 66 72 61 6d 65 20 61 64  /* Next frame ad
1a9ad 64 72 65 73 73 20 2a 2f 0a 20 20 50 67 48 64 72  dress */.  PgHdr
1a9ae 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   *p;            
1a9af 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
1a9b0 65 72 61 74 6f 72 20 74 6f 20 72 75 6e 20 74 68  erator to run th
1a9b1 72 6f 75 67 68 20 70 4c 69 73 74 20 77 69 74 68  rough pList with
1a9b2 2e 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 4c  . */.  PgHdr *pL
1a9b3 61 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ast = 0;        
1a9b4 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66         /* Last f
1a9b5 72 61 6d 65 20 69 6e 20 6c 69 73 74 20 2a 2f 0a  rame in list */.
1a9b6 20 20 69 6e 74 20 6e 45 78 74 72 61 20 3d 20 30    int nExtra = 0
1a9b7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1a9b8 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
1a9b9 78 74 72 61 20 63 6f 70 69 65 73 20 6f 66 20 6c  xtra copies of l
1a9ba 61 73 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  ast page */.  in
1a9bb 74 20 73 7a 46 72 61 6d 65 3b 20 20 20 20 20 20  t szFrame;      
1a9bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a9bd 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20 73   The size of a s
1a9be 69 6e 67 6c 65 20 66 72 61 6d 65 20 2a 2f 0a 20  ingle frame */. 
1a9bf 20 69 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20   i64 iOffset;   
1a9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a9c1 20 2f 2a 20 4e 65 78 74 20 62 79 74 65 20 74 6f   /* Next byte to
1a9c2 20 77 72 69 74 65 20 69 6e 20 57 41 4c 20 66 69   write in WAL fi
1a9c3 6c 65 20 2a 2f 0a 20 20 57 61 6c 57 72 69 74 65  le */.  WalWrite
1a9c4 72 20 77 3b 20 20 20 20 20 20 20 20 20 20 20 20  r w;            
1a9c5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77          /* The w
1a9c6 72 69 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65  riter */..  asse
1a9c7 72 74 28 20 70 4c 69 73 74 20 29 3b 0a 20 20 61  rt( pList );.  a
1a9c8 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 77 72 69  ssert( pWal->wri
1a9c9 74 65 4c 6f 63 6b 20 29 3b 0a 0a 20 20 2f 2a 20  teLock );..  /* 
1a9ca 49 66 20 74 68 69 73 20 66 72 61 6d 65 20 73 65  If this frame se
1a9cb 74 20 63 6f 6d 70 6c 65 74 65 73 20 61 20 74 72  t completes a tr
1a9cc 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20  ansaction, then 
1a9cd 6e 54 72 75 6e 63 61 74 65 3e 30 2e 20 20 49 66  nTruncate>0.  If
1a9ce 0a 20 20 2a 2a 20 6e 54 72 75 6e 63 61 74 65 3d  .  ** nTruncate=
1a9cf 3d 30 20 74 68 65 6e 20 74 68 69 73 20 66 72 61  =0 then this fra
1a9d0 6d 65 20 73 65 74 20 64 6f 65 73 20 6e 6f 74 20  me set does not 
1a9d1 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 74 72 61  complete the tra
1a9d2 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 61  nsaction. */.  a
1a9d3 73 73 65 72 74 28 20 28 69 73 43 6f 6d 6d 69 74  ssert( (isCommit
1a9d4 21 3d 30 29 3d 3d 28 6e 54 72 75 6e 63 61 74 65  !=0)==(nTruncate
1a9d5 21 3d 30 29 20 29 3b 0a 0a 23 69 66 20 64 65 66  !=0) );..#if def
1a9d6 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
1a9d7 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  ) && defined(SQL
1a9d8 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 20 69  ITE_DEBUG).  { i
1a9d9 6e 74 20 63 6e 74 3b 20 66 6f 72 28 63 6e 74 3d  nt cnt; for(cnt=
1a9da 30 2c 20 70 3d 70 4c 69 73 74 3b 20 70 3b 20 70  0, p=pList; p; p
1a9db 3d 70 2d 3e 70 44 69 72 74 79 2c 20 63 6e 74 2b  =p->pDirty, cnt+
1a9dc 2b 29 7b 7d 0a 20 20 20 20 57 41 4c 54 52 41 43  +){}.    WALTRAC
1a9dd 45 28 28 22 57 41 4c 25 70 3a 20 66 72 61 6d 65  E(("WAL%p: frame
1a9de 20 77 72 69 74 65 20 62 65 67 69 6e 2e 20 25 64   write begin. %d
1a9df 20 66 72 61 6d 65 73 2e 20 6d 78 46 72 61 6d 65   frames. mxFrame
1a9e0 3d 25 64 2e 20 25 73 5c 6e 22 2c 0a 20 20 20 20  =%d. %s\n",.    
1a9e1 20 20 20 20 20 20 20 20 20 20 70 57 61 6c 2c 20            pWal, 
1a9e2 63 6e 74 2c 20 70 57 61 6c 2d 3e 68 64 72 2e 6d  cnt, pWal->hdr.m
1a9e3 78 46 72 61 6d 65 2c 20 69 73 43 6f 6d 6d 69 74  xFrame, isCommit
1a9e4 20 3f 20 22 43 6f 6d 6d 69 74 22 20 3a 20 22 53   ? "Commit" : "S
1a9e5 70 69 6c 6c 22 29 29 3b 0a 20 20 7d 0a 23 65 6e  pill"));.  }.#en
1a9e6 64 69 66 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66  dif..  /* See if
1a9e7 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   it is possible 
1a9e8 74 6f 20 77 72 69 74 65 20 74 68 65 73 65 20 66  to write these f
1a9e9 72 61 6d 65 73 20 69 6e 74 6f 20 74 68 65 20 73  rames into the s
1a9ea 74 61 72 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a  tart of the.  **
1a9eb 20 6c 6f 67 20 66 69 6c 65 2c 20 69 6e 73 74 65   log file, inste
1a9ec 61 64 20 6f 66 20 61 70 70 65 6e 64 69 6e 67 20  ad of appending 
1a9ed 74 6f 20 69 74 20 61 74 20 70 57 61 6c 2d 3e 68  to it at pWal->h
1a9ee 64 72 2e 6d 78 46 72 61 6d 65 2e 0a 20 20 2a 2f  dr.mxFrame..  */
1a9ef 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
1a9f0 21 3d 28 72 63 20 3d 20 77 61 6c 52 65 73 74 61  !=(rc = walResta
1a9f1 72 74 4c 6f 67 28 70 57 61 6c 29 29 20 29 7b 0a  rtLog(pWal)) ){.
1a9f2 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1a9f3 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73   }..  /* If this
1a9f4 20 69 73 20 74 68 65 20 66 69 72 73 74 20 66 72   is the first fr
1a9f5 61 6d 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  ame written into
1a9f6 20 74 68 65 20 6c 6f 67 2c 20 77 72 69 74 65 20   the log, write 
1a9f7 74 68 65 20 57 41 4c 0a 20 20 2a 2a 20 68 65 61  the WAL.  ** hea
1a9f8 64 65 72 20 74 6f 20 74 68 65 20 73 74 61 72 74  der to the start
1a9f9 20 6f 66 20 74 68 65 20 57 41 4c 20 66 69 6c 65   of the WAL file
1a9fa 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61  . See comments a
1a9fb 74 20 74 68 65 20 74 6f 70 20 6f 66 0a 20 20 2a  t the top of.  *
1a9fc 2a 20 74 68 69 73 20 73 6f 75 72 63 65 20 66 69  * this source fi
1a9fd 6c 65 20 66 6f 72 20 61 20 64 65 73 63 72 69 70  le for a descrip
1a9fe 74 69 6f 6e 20 6f 66 20 74 68 65 20 57 41 4c 20  tion of the WAL 
1a9ff 68 65 61 64 65 72 20 66 6f 72 6d 61 74 2e 0a 20  header format.. 
1aa00 20 2a 2f 0a 20 20 69 46 72 61 6d 65 20 3d 20 70   */.  iFrame = p
1aa01 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65  Wal->hdr.mxFrame
1aa02 3b 0a 20 20 69 66 28 20 69 46 72 61 6d 65 3d 3d  ;.  if( iFrame==
1aa03 30 20 29 7b 0a 20 20 20 20 75 38 20 61 57 61 6c  0 ){.    u8 aWal
1aa04 48 64 72 5b 57 41 4c 5f 48 44 52 53 49 5a 45 5d  Hdr[WAL_HDRSIZE]
1aa05 3b 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72  ;      /* Buffer
1aa06 20 74 6f 20 61 73 73 65 6d 62 6c 65 20 77 61 6c   to assemble wal
1aa07 2d 68 65 61 64 65 72 20 69 6e 20 2a 2f 0a 20 20  -header in */.  
1aa08 20 20 75 33 32 20 61 43 6b 73 75 6d 5b 32 5d 3b    u32 aCksum[2];
1aa09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aa0a 2f 2a 20 43 68 65 63 6b 73 75 6d 20 66 6f 72 20  /* Checksum for 
1aa0b 77 61 6c 2d 68 65 61 64 65 72 20 2a 2f 0a 0a 20  wal-header */.. 
1aa0c 20 20 20 73 71 6c 69 74 65 33 50 75 74 34 62 79     sqlite3Put4by
1aa0d 74 65 28 26 61 57 61 6c 48 64 72 5b 30 5d 2c 20  te(&aWalHdr[0], 
1aa0e 28 57 41 4c 5f 4d 41 47 49 43 20 7c 20 53 51 4c  (WAL_MAGIC | SQL
1aa0f 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 29 29 3b  ITE_BIGENDIAN));
1aa10 0a 20 20 20 20 73 71 6c 69 74 65 33 50 75 74 34  .    sqlite3Put4
1aa11 62 79 74 65 28 26 61 57 61 6c 48 64 72 5b 34 5d  byte(&aWalHdr[4]
1aa12 2c 20 57 41 4c 5f 4d 41 58 5f 56 45 52 53 49 4f  , WAL_MAX_VERSIO
1aa13 4e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  N);.    sqlite3P
1aa14 75 74 34 62 79 74 65 28 26 61 57 61 6c 48 64 72  ut4byte(&aWalHdr
1aa15 5b 38 5d 2c 20 73 7a 50 61 67 65 29 3b 0a 20 20  [8], szPage);.  
1aa16 20 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74    sqlite3Put4byt
1aa17 65 28 26 61 57 61 6c 48 64 72 5b 31 32 5d 2c 20  e(&aWalHdr[12], 
1aa18 70 57 61 6c 2d 3e 6e 43 6b 70 74 29 3b 0a 20 20  pWal->nCkpt);.  
1aa19 20 20 69 66 28 20 70 57 61 6c 2d 3e 6e 43 6b 70    if( pWal->nCkp
1aa1a 74 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 5f 72  t==0 ) sqlite3_r
1aa1b 61 6e 64 6f 6d 6e 65 73 73 28 38 2c 20 70 57 61  andomness(8, pWa
1aa1c 6c 2d 3e 68 64 72 2e 61 53 61 6c 74 29 3b 0a 20  l->hdr.aSalt);. 
1aa1d 20 20 20 6d 65 6d 63 70 79 28 26 61 57 61 6c 48     memcpy(&aWalH
1aa1e 64 72 5b 31 36 5d 2c 20 70 57 61 6c 2d 3e 68 64  dr[16], pWal->hd
1aa1f 72 2e 61 53 61 6c 74 2c 20 38 29 3b 0a 20 20 20  r.aSalt, 8);.   
1aa20 20 77 61 6c 43 68 65 63 6b 73 75 6d 42 79 74 65   walChecksumByte
1aa21 73 28 31 2c 20 61 57 61 6c 48 64 72 2c 20 57 41  s(1, aWalHdr, WA
1aa22 4c 5f 48 44 52 53 49 5a 45 2d 32 2a 34 2c 20 30  L_HDRSIZE-2*4, 0
1aa23 2c 20 61 43 6b 73 75 6d 29 3b 0a 20 20 20 20 73  , aCksum);.    s
1aa24 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 26  qlite3Put4byte(&
1aa25 61 57 61 6c 48 64 72 5b 32 34 5d 2c 20 61 43 6b  aWalHdr[24], aCk
1aa26 73 75 6d 5b 30 5d 29 3b 0a 20 20 20 20 73 71 6c  sum[0]);.    sql
1aa27 69 74 65 33 50 75 74 34 62 79 74 65 28 26 61 57  ite3Put4byte(&aW
1aa28 61 6c 48 64 72 5b 32 38 5d 2c 20 61 43 6b 73 75  alHdr[28], aCksu
1aa29 6d 5b 31 5d 29 3b 0a 20 20 20 20 0a 20 20 20 20  m[1]);.    .    
1aa2a 70 57 61 6c 2d 3e 73 7a 50 61 67 65 20 3d 20 73  pWal->szPage = s
1aa2b 7a 50 61 67 65 3b 0a 20 20 20 20 70 57 61 6c 2d  zPage;.    pWal-
1aa2c 3e 68 64 72 2e 62 69 67 45 6e 64 43 6b 73 75 6d  >hdr.bigEndCksum
1aa2d 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44   = SQLITE_BIGEND
1aa2e 49 41 4e 3b 0a 20 20 20 20 70 57 61 6c 2d 3e 68  IAN;.    pWal->h
1aa2f 64 72 2e 61 46 72 61 6d 65 43 6b 73 75 6d 5b 30  dr.aFrameCksum[0
1aa30 5d 20 3d 20 61 43 6b 73 75 6d 5b 30 5d 3b 0a 20  ] = aCksum[0];. 
1aa31 20 20 20 70 57 61 6c 2d 3e 68 64 72 2e 61 46 72     pWal->hdr.aFr
1aa32 61 6d 65 43 6b 73 75 6d 5b 31 5d 20 3d 20 61 43  ameCksum[1] = aC
1aa33 6b 73 75 6d 5b 31 5d 3b 0a 20 20 20 20 70 57 61  ksum[1];.    pWa
1aa34 6c 2d 3e 74 72 75 6e 63 61 74 65 4f 6e 43 6f 6d  l->truncateOnCom
1aa35 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 20 20 72 63  mit = 1;..    rc
1aa36 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
1aa37 65 28 70 57 61 6c 2d 3e 70 57 61 6c 46 64 2c 20  e(pWal->pWalFd, 
1aa38 61 57 61 6c 48 64 72 2c 20 73 69 7a 65 6f 66 28  aWalHdr, sizeof(
1aa39 61 57 61 6c 48 64 72 29 2c 20 30 29 3b 0a 20 20  aWalHdr), 0);.  
1aa3a 20 20 57 41 4c 54 52 41 43 45 28 28 22 57 41 4c    WALTRACE(("WAL
1aa3b 25 70 3a 20 77 61 6c 2d 68 65 61 64 65 72 20 77  %p: wal-header w
1aa3c 72 69 74 65 20 25 73 5c 6e 22 2c 20 70 57 61 6c  rite %s\n", pWal
1aa3d 2c 20 72 63 20 3f 20 22 66 61 69 6c 65 64 22 20  , rc ? "failed" 
1aa3e 3a 20 22 6f 6b 22 29 29 3b 0a 20 20 20 20 69 66  : "ok"));.    if
1aa3f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1aa40 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1aa41 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  rc;.    }..    /
1aa42 2a 20 53 79 6e 63 20 74 68 65 20 68 65 61 64 65  * Sync the heade
1aa43 72 20 28 75 6e 6c 65 73 73 20 53 51 4c 49 54 45  r (unless SQLITE
1aa44 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41  _IOCAP_SEQUENTIA
1aa45 4c 20 69 73 20 74 72 75 65 20 6f 72 20 75 6e 6c  L is true or unl
1aa46 65 73 73 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 73  ess.    ** all s
1aa47 79 6e 63 69 6e 67 20 69 73 20 74 75 72 6e 65 64  yncing is turned
1aa48 20 6f 66 66 20 62 79 20 50 52 41 47 4d 41 20 73   off by PRAGMA s
1aa49 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 29 2e  ynchronous=OFF).
1aa4a 20 20 4f 74 68 65 72 77 69 73 65 0a 20 20 20 20    Otherwise.    
1aa4b 2a 2a 20 61 6e 20 6f 75 74 2d 6f 66 2d 6f 72 64  ** an out-of-ord
1aa4c 65 72 20 77 72 69 74 65 20 66 6f 6c 6c 6f 77 69  er write followi
1aa4d 6e 67 20 61 20 57 41 4c 20 72 65 73 74 61 72 74  ng a WAL restart
1aa4e 20 63 6f 75 6c 64 20 72 65 73 75 6c 74 20 69 6e   could result in
1aa4f 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65  .    ** database
1aa50 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 20 53 65   corruption.  Se
1aa51 65 20 74 68 65 20 74 69 63 6b 65 74 3a 0a 20 20  e the ticket:.  
1aa52 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
1aa53 68 74 74 70 3a 2f 2f 6c 6f 63 61 6c 68 6f 73 74  http://localhost
1aa54 3a 35 39 31 2f 73 71 6c 69 74 65 2f 69 6e 66 6f  :591/sqlite/info
1aa55 2f 66 66 35 62 65 37 33 64 65 65 0a 20 20 20 20  /ff5be73dee.    
1aa56 2a 2f 0a 20 20 20 20 69 66 28 20 70 57 61 6c 2d  */.    if( pWal-
1aa57 3e 73 79 6e 63 48 65 61 64 65 72 20 26 26 20 73  >syncHeader && s
1aa58 79 6e 63 5f 66 6c 61 67 73 20 29 7b 0a 20 20 20  ync_flags ){.   
1aa59 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1aa5a 73 53 79 6e 63 28 70 57 61 6c 2d 3e 70 57 61 6c  sSync(pWal->pWal
1aa5b 46 64 2c 20 73 79 6e 63 5f 66 6c 61 67 73 20 26  Fd, sync_flags &
1aa5c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4d 41 53   SQLITE_SYNC_MAS
1aa5d 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  K);.      if( rc
1aa5e 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
1aa5f 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74    }.  }.  assert
1aa60 28 20 28 69 6e 74 29 70 57 61 6c 2d 3e 73 7a 50  ( (int)pWal->szP
1aa61 61 67 65 3d 3d 73 7a 50 61 67 65 20 29 3b 0a 0a  age==szPage );..
1aa62 20 20 2f 2a 20 53 65 74 75 70 20 69 6e 66 6f 72    /* Setup infor
1aa63 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f  mation needed to
1aa64 20 77 72 69 74 65 20 66 72 61 6d 65 73 20 69 6e   write frames in
1aa65 74 6f 20 74 68 65 20 57 41 4c 20 2a 2f 0a 20 20  to the WAL */.  
1aa66 77 2e 70 57 61 6c 20 3d 20 70 57 61 6c 3b 0a 20  w.pWal = pWal;. 
1aa67 20 77 2e 70 46 64 20 3d 20 70 57 61 6c 2d 3e 70   w.pFd = pWal->p
1aa68 57 61 6c 46 64 3b 0a 20 20 77 2e 69 53 79 6e 63  WalFd;.  w.iSync
1aa69 50 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 77 2e 73  Point = 0;.  w.s
1aa6a 79 6e 63 46 6c 61 67 73 20 3d 20 73 79 6e 63 5f  yncFlags = sync_
1aa6b 66 6c 61 67 73 3b 0a 20 20 77 2e 73 7a 50 61 67  flags;.  w.szPag
1aa6c 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20 69 4f  e = szPage;.  iO
1aa6d 66 66 73 65 74 20 3d 20 77 61 6c 46 72 61 6d 65  ffset = walFrame
1aa6e 4f 66 66 73 65 74 28 69 46 72 61 6d 65 2b 31 2c  Offset(iFrame+1,
1aa6f 20 73 7a 50 61 67 65 29 3b 0a 20 20 73 7a 46 72   szPage);.  szFr
1aa70 61 6d 65 20 3d 20 73 7a 50 61 67 65 20 2b 20 57  ame = szPage + W
1aa71 41 4c 5f 46 52 41 4d 45 5f 48 44 52 53 49 5a 45  AL_FRAME_HDRSIZE
1aa72 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 61 6c  ;..  /* Write al
1aa73 6c 20 66 72 61 6d 65 73 20 69 6e 74 6f 20 74 68  l frames into th
1aa74 65 20 6c 6f 67 20 66 69 6c 65 20 65 78 61 63 74  e log file exact
1aa75 6c 79 20 6f 6e 63 65 20 2a 2f 0a 20 20 66 6f 72  ly once */.  for
1aa76 28 70 3d 70 4c 69 73 74 3b 20 70 3b 20 70 3d 70  (p=pList; p; p=p
1aa77 2d 3e 70 44 69 72 74 79 29 7b 0a 20 20 20 20 69  ->pDirty){.    i
1aa78 6e 74 20 6e 44 62 53 69 7a 65 3b 20 20 20 2f 2a  nt nDbSize;   /*
1aa79 20 30 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 50 6f   0 normally.  Po
1aa7a 73 69 74 69 76 65 20 3d 3d 20 63 6f 6d 6d 69 74  sitive == commit
1aa7b 20 66 6c 61 67 20 2a 2f 0a 20 20 20 20 69 46 72   flag */.    iFr
1aa7c 61 6d 65 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72  ame++;.    asser
1aa7d 74 28 20 69 4f 66 66 73 65 74 3d 3d 77 61 6c 46  t( iOffset==walF
1aa7e 72 61 6d 65 4f 66 66 73 65 74 28 69 46 72 61 6d  rameOffset(iFram
1aa7f 65 2c 20 73 7a 50 61 67 65 29 20 29 3b 0a 20 20  e, szPage) );.  
1aa80 20 20 6e 44 62 53 69 7a 65 20 3d 20 28 69 73 43    nDbSize = (isC
1aa81 6f 6d 6d 69 74 20 26 26 20 70 2d 3e 70 44 69 72  ommit && p->pDir
1aa82 74 79 3d 3d 30 29 20 3f 20 6e 54 72 75 6e 63 61  ty==0) ? nTrunca
1aa83 74 65 20 3a 20 30 3b 0a 20 20 20 20 72 63 20 3d  te : 0;.    rc =
1aa84 20 77 61 6c 57 72 69 74 65 4f 6e 65 46 72 61 6d   walWriteOneFram
1aa85 65 28 26 77 2c 20 70 2c 20 6e 44 62 53 69 7a 65  e(&w, p, nDbSize
1aa86 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  , iOffset);.    
1aa87 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
1aa88 72 63 3b 0a 20 20 20 20 70 4c 61 73 74 20 3d 20  rc;.    pLast = 
1aa89 70 3b 0a 20 20 20 20 69 4f 66 66 73 65 74 20 2b  p;.    iOffset +
1aa8a 3d 20 73 7a 46 72 61 6d 65 3b 0a 20 20 7d 0a 0a  = szFrame;.  }..
1aa8b 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
1aa8c 74 68 65 20 65 6e 64 20 6f 66 20 61 20 74 72 61  the end of a tra
1aa8d 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 77  nsaction, then w
1aa8e 65 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20  e might need to 
1aa8f 70 61 64 0a 20 20 2a 2a 20 74 68 65 20 74 72 61  pad.  ** the tra
1aa90 6e 73 61 63 74 69 6f 6e 20 61 6e 64 2f 6f 72 20  nsaction and/or 
1aa91 73 79 6e 63 20 74 68 65 20 57 41 4c 20 66 69 6c  sync the WAL fil
1aa92 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 50 61 64  e..  **.  ** Pad
1aa93 64 69 6e 67 20 61 6e 64 20 73 79 6e 63 69 6e 67  ding and syncing
1aa94 20 6f 6e 6c 79 20 6f 63 63 75 72 20 69 66 20 74   only occur if t
1aa95 68 69 73 20 73 65 74 20 6f 66 20 66 72 61 6d 65  his set of frame
1aa96 73 20 63 6f 6d 70 6c 65 74 65 20 61 0a 20 20 2a  s complete a.  *
1aa97 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e  * transaction an
1aa98 64 20 69 66 20 50 52 41 47 4d 41 20 73 79 6e 63  d if PRAGMA sync
1aa99 68 72 6f 6e 6f 75 73 3d 46 55 4c 4c 2e 20 20 49  hronous=FULL.  I
1aa9a 66 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 3d 4e  f synchronous==N
1aa9b 4f 52 4d 41 4c 0a 20 20 2a 2a 20 6f 72 20 73 79  ORMAL.  ** or sy
1aa9c 6e 63 68 6f 6e 6f 75 73 3d 3d 4f 46 46 2c 20 74  nchonous==OFF, t
1aa9d 68 65 6e 20 6e 6f 20 70 61 64 64 69 6e 67 20 6f  hen no padding o
1aa9e 72 20 73 79 6e 63 69 6e 67 20 61 72 65 20 6e 65  r syncing are ne
1aa9f 65 64 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  eded..  **.  ** 
1aaa0 49 66 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  If SQLITE_IOCAP_
1aaa1 50 4f 57 45 52 53 41 46 45 5f 4f 56 45 52 57 52  POWERSAFE_OVERWR
1aaa2 49 54 45 20 69 73 20 64 65 66 69 6e 65 64 2c 20  ITE is defined, 
1aaa3 74 68 65 6e 20 70 61 64 64 69 6e 67 20 69 73 20  then padding is 
1aaa4 6e 6f 74 0a 20 20 2a 2a 20 6e 65 65 64 65 64 20  not.  ** needed 
1aaa5 61 6e 64 20 6f 6e 6c 79 20 74 68 65 20 73 79 6e  and only the syn
1aaa6 63 20 69 73 20 64 6f 6e 65 2e 20 20 49 66 20 70  c is done.  If p
1aaa7 61 64 64 69 6e 67 20 69 73 20 6e 65 65 64 65 64  adding is needed
1aaa8 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20  , then the.  ** 
1aaa9 66 69 6e 61 6c 20 66 72 61 6d 65 20 69 73 20 72  final frame is r
1aaaa 65 70 65 61 74 65 64 20 28 77 69 74 68 20 69 74  epeated (with it
1aaab 73 20 63 6f 6d 6d 69 74 20 6d 61 72 6b 29 20 75  s commit mark) u
1aaac 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 73 65  ntil the next se
1aaad 63 74 6f 72 0a 20 20 2a 2a 20 62 6f 75 6e 64 61  ctor.  ** bounda
1aaae 72 79 20 69 73 20 63 72 6f 73 73 65 64 2e 20 20  ry is crossed.  
1aaaf 4f 6e 6c 79 20 74 68 65 20 70 61 72 74 20 6f 66  Only the part of
1aab0 20 74 68 65 20 57 41 4c 20 70 72 69 6f 72 20 74   the WAL prior t
1aab1 6f 20 74 68 65 20 6c 61 73 74 0a 20 20 2a 2a 20  o the last.  ** 
1aab2 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20  sector boundary 
1aab3 69 73 20 73 79 6e 63 65 64 3b 20 74 68 65 20 70  is synced; the p
1aab4 61 72 74 20 6f 66 20 74 68 65 20 6c 61 73 74 20  art of the last 
1aab5 66 72 61 6d 65 20 74 68 61 74 20 65 78 74 65 6e  frame that exten
1aab6 64 73 0a 20 20 2a 2a 20 70 61 73 74 20 74 68 65  ds.  ** past the
1aab7 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79   sector boundary
1aab8 20 69 73 20 77 72 69 74 74 65 6e 20 61 66 74 65   is written afte
1aab9 72 20 74 68 65 20 73 79 6e 63 2e 0a 20 20 2a 2f  r the sync..  */
1aaba 0a 20 20 69 66 28 20 69 73 43 6f 6d 6d 69 74 20  .  if( isCommit 
1aabb 26 26 20 28 73 79 6e 63 5f 66 6c 61 67 73 20 26  && (sync_flags &
1aabc 20 57 41 4c 5f 53 59 4e 43 5f 54 52 41 4e 53 41   WAL_SYNC_TRANSA
1aabd 43 54 49 4f 4e 53 29 21 3d 30 20 29 7b 0a 20 20  CTIONS)!=0 ){.  
1aabe 20 20 69 66 28 20 70 57 61 6c 2d 3e 70 61 64 54    if( pWal->padT
1aabf 6f 53 65 63 74 6f 72 42 6f 75 6e 64 61 72 79 20  oSectorBoundary 
1aac0 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 65 63  ){.      int sec
1aac1 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65  torSize = sqlite
1aac2 33 53 65 63 74 6f 72 53 69 7a 65 28 70 57 61 6c  3SectorSize(pWal
1aac3 2d 3e 70 57 61 6c 46 64 29 3b 0a 20 20 20 20 20  ->pWalFd);.     
1aac4 20 77 2e 69 53 79 6e 63 50 6f 69 6e 74 20 3d 20   w.iSyncPoint = 
1aac5 28 28 69 4f 66 66 73 65 74 2b 73 65 63 74 6f 72  ((iOffset+sector
1aac6 53 69 7a 65 2d 31 29 2f 73 65 63 74 6f 72 53 69  Size-1)/sectorSi
1aac7 7a 65 29 2a 73 65 63 74 6f 72 53 69 7a 65 3b 0a  ze)*sectorSize;.
1aac8 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 4f 66        while( iOf
1aac9 66 73 65 74 3c 77 2e 69 53 79 6e 63 50 6f 69 6e  fset<w.iSyncPoin
1aaca 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  t ){.        rc 
1aacb 3d 20 77 61 6c 57 72 69 74 65 4f 6e 65 46 72 61  = walWriteOneFra
1aacc 6d 65 28 26 77 2c 20 70 4c 61 73 74 2c 20 6e 54  me(&w, pLast, nT
1aacd 72 75 6e 63 61 74 65 2c 20 69 4f 66 66 73 65 74  runcate, iOffset
1aace 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
1aacf 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
1aad0 20 20 20 20 20 20 20 69 4f 66 66 73 65 74 20 2b         iOffset +
1aad1 3d 20 73 7a 46 72 61 6d 65 3b 0a 20 20 20 20 20  = szFrame;.     
1aad2 20 20 20 6e 45 78 74 72 61 2b 2b 3b 0a 20 20 20     nExtra++;.   
1aad3 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
1aad4 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1aad5 65 33 4f 73 53 79 6e 63 28 77 2e 70 46 64 2c 20  e3OsSync(w.pFd, 
1aad6 73 79 6e 63 5f 66 6c 61 67 73 20 26 20 53 51 4c  sync_flags & SQL
1aad7 49 54 45 5f 53 59 4e 43 5f 4d 41 53 4b 29 3b 0a  ITE_SYNC_MASK);.
1aad8 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1aad9 49 66 20 74 68 69 73 20 66 72 61 6d 65 20 73 65  If this frame se
1aada 74 20 63 6f 6d 70 6c 65 74 65 73 20 74 68 65 20  t completes the 
1aadb 66 69 72 73 74 20 74 72 61 6e 73 61 63 74 69 6f  first transactio
1aadc 6e 20 69 6e 20 74 68 65 20 57 41 4c 20 61 6e 64  n in the WAL and
1aadd 0a 20 20 2a 2a 20 69 66 20 50 52 41 47 4d 41 20  .  ** if PRAGMA 
1aade 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d  journal_size_lim
1aadf 69 74 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20  it is set, then 
1aae0 74 72 75 6e 63 61 74 65 20 74 68 65 20 57 41 4c  truncate the WAL
1aae1 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75   to the.  ** jou
1aae2 72 6e 61 6c 20 73 69 7a 65 20 6c 69 6d 69 74 2c  rnal size limit,
1aae3 20 69 66 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20   if possible..  
1aae4 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f 6d 6d 69  */.  if( isCommi
1aae5 74 20 26 26 20 70 57 61 6c 2d 3e 74 72 75 6e 63  t && pWal->trunc
1aae6 61 74 65 4f 6e 43 6f 6d 6d 69 74 20 26 26 20 70  ateOnCommit && p
1aae7 57 61 6c 2d 3e 6d 78 57 61 6c 53 69 7a 65 3e 3d  Wal->mxWalSize>=
1aae8 30 20 29 7b 0a 20 20 20 20 69 36 34 20 73 7a 20  0 ){.    i64 sz 
1aae9 3d 20 70 57 61 6c 2d 3e 6d 78 57 61 6c 53 69 7a  = pWal->mxWalSiz
1aaea 65 3b 0a 20 20 20 20 69 66 28 20 77 61 6c 46 72  e;.    if( walFr
1aaeb 61 6d 65 4f 66 66 73 65 74 28 69 46 72 61 6d 65  ameOffset(iFrame
1aaec 2b 6e 45 78 74 72 61 2b 31 2c 20 73 7a 50 61 67  +nExtra+1, szPag
1aaed 65 29 3e 70 57 61 6c 2d 3e 6d 78 57 61 6c 53 69  e)>pWal->mxWalSi
1aaee 7a 65 20 29 7b 0a 20 20 20 20 20 20 73 7a 20 3d  ze ){.      sz =
1aaef 20 77 61 6c 46 72 61 6d 65 4f 66 66 73 65 74 28   walFrameOffset(
1aaf0 69 46 72 61 6d 65 2b 6e 45 78 74 72 61 2b 31 2c  iFrame+nExtra+1,
1aaf1 20 73 7a 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a   szPage);.    }.
1aaf2 20 20 20 20 77 61 6c 4c 69 6d 69 74 53 69 7a 65      walLimitSize
1aaf3 28 70 57 61 6c 2c 20 73 7a 29 3b 0a 20 20 20 20  (pWal, sz);.    
1aaf4 70 57 61 6c 2d 3e 74 72 75 6e 63 61 74 65 4f 6e  pWal->truncateOn
1aaf5 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 0a  Commit = 0;.  }.
1aaf6 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 64 61 74  .  /* Append dat
1aaf7 61 20 74 6f 20 74 68 65 20 77 61 6c 2d 69 6e 64  a to the wal-ind
1aaf8 65 78 2e 20 49 74 20 69 73 20 6e 6f 74 20 6e 65  ex. It is not ne
1aaf9 63 65 73 73 61 72 79 20 74 6f 20 6c 6f 63 6b 20  cessary to lock 
1aafa 74 68 65 20 0a 20 20 2a 2a 20 77 61 6c 2d 69 6e  the .  ** wal-in
1aafb 64 65 78 20 74 6f 20 64 6f 20 74 68 69 73 20 61  dex to do this a
1aafc 73 20 74 68 65 20 53 51 4c 49 54 45 5f 53 48 4d  s the SQLITE_SHM
1aafd 5f 57 52 49 54 45 20 6c 6f 63 6b 20 68 65 6c 64  _WRITE lock held
1aafe 20 6f 6e 20 74 68 65 20 77 61 6c 2d 69 6e 64 65   on the wal-inde
1aaff 78 0a 20 20 2a 2a 20 67 75 61 72 61 6e 74 65 65  x.  ** guarantee
1ab00 73 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65  s that there are
1ab01 20 6e 6f 20 6f 74 68 65 72 20 77 72 69 74 65 72   no other writer
1ab02 73 2c 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 74  s, and no data t
1ab03 68 61 74 20 6d 61 79 0a 20 20 2a 2a 20 62 65 20  hat may.  ** be 
1ab04 69 6e 20 75 73 65 20 62 79 20 65 78 69 73 74 69  in use by existi
1ab05 6e 67 20 72 65 61 64 65 72 73 20 69 73 20 62 65  ng readers is be
1ab06 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e  ing overwritten.
1ab07 0a 20 20 2a 2f 0a 20 20 69 46 72 61 6d 65 20 3d  .  */.  iFrame =
1ab08 20 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61   pWal->hdr.mxFra
1ab09 6d 65 3b 0a 20 20 66 6f 72 28 70 3d 70 4c 69 73  me;.  for(p=pLis
1ab0a 74 3b 20 70 20 26 26 20 72 63 3d 3d 53 51 4c 49  t; p && rc==SQLI
1ab0b 54 45 5f 4f 4b 3b 20 70 3d 70 2d 3e 70 44 69 72  TE_OK; p=p->pDir
1ab0c 74 79 29 7b 0a 20 20 20 20 69 46 72 61 6d 65 2b  ty){.    iFrame+
1ab0d 2b 3b 0a 20 20 20 20 72 63 20 3d 20 77 61 6c 49  +;.    rc = walI
1ab0e 6e 64 65 78 41 70 70 65 6e 64 28 70 57 61 6c 2c  ndexAppend(pWal,
1ab0f 20 69 46 72 61 6d 65 2c 20 70 2d 3e 70 67 6e 6f   iFrame, p->pgno
1ab10 29 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20  );.  }.  while( 
1ab11 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1ab12 20 6e 45 78 74 72 61 3e 30 20 29 7b 0a 20 20 20   nExtra>0 ){.   
1ab13 20 69 46 72 61 6d 65 2b 2b 3b 0a 20 20 20 20 6e   iFrame++;.    n
1ab14 45 78 74 72 61 2d 2d 3b 0a 20 20 20 20 72 63 20  Extra--;.    rc 
1ab15 3d 20 77 61 6c 49 6e 64 65 78 41 70 70 65 6e 64  = walIndexAppend
1ab16 28 70 57 61 6c 2c 20 69 46 72 61 6d 65 2c 20 70  (pWal, iFrame, p
1ab17 4c 61 73 74 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d  Last->pgno);.  }
1ab18 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
1ab19 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20  TE_OK ){.    /* 
1ab1a 55 70 64 61 74 65 20 74 68 65 20 70 72 69 76 61  Update the priva
1ab1b 74 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 68  te copy of the h
1ab1c 65 61 64 65 72 2e 20 2a 2f 0a 20 20 20 20 70 57  eader. */.    pW
1ab1d 61 6c 2d 3e 68 64 72 2e 73 7a 50 61 67 65 20 3d  al->hdr.szPage =
1ab1e 20 28 75 31 36 29 28 28 73 7a 50 61 67 65 26 30   (u16)((szPage&0
1ab1f 78 66 66 30 30 29 20 7c 20 28 73 7a 50 61 67 65  xff00) | (szPage
1ab20 3e 3e 31 36 29 29 3b 0a 20 20 20 20 74 65 73 74  >>16));.    test
1ab21 63 61 73 65 28 20 73 7a 50 61 67 65 3c 3d 33 32  case( szPage<=32
1ab22 37 36 38 20 29 3b 0a 20 20 20 20 74 65 73 74 63  768 );.    testc
1ab23 61 73 65 28 20 73 7a 50 61 67 65 3e 3d 36 35 35  ase( szPage>=655
1ab24 33 36 20 29 3b 0a 20 20 20 20 70 57 61 6c 2d 3e  36 );.    pWal->
1ab25 68 64 72 2e 6d 78 46 72 61 6d 65 20 3d 20 69 46  hdr.mxFrame = iF
1ab26 72 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 69 73  rame;.    if( is
1ab27 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 20 20  Commit ){.      
1ab28 70 57 61 6c 2d 3e 68 64 72 2e 69 43 68 61 6e 67  pWal->hdr.iChang
1ab29 65 2b 2b 3b 0a 20 20 20 20 20 20 70 57 61 6c 2d  e++;.      pWal-
1ab2a 3e 68 64 72 2e 6e 50 61 67 65 20 3d 20 6e 54 72  >hdr.nPage = nTr
1ab2b 75 6e 63 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20  uncate;.    }.  
1ab2c 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
1ab2d 61 20 63 6f 6d 6d 69 74 2c 20 75 70 64 61 74 65  a commit, update
1ab2e 20 74 68 65 20 77 61 6c 2d 69 6e 64 65 78 20 68   the wal-index h
1ab2f 65 61 64 65 72 20 74 6f 6f 2e 20 2a 2f 0a 20 20  eader too. */.  
1ab30 20 20 69 66 28 20 69 73 43 6f 6d 6d 69 74 20 29    if( isCommit )
1ab31 7b 0a 20 20 20 20 20 20 77 61 6c 49 6e 64 65 78  {.      walIndex
1ab32 57 72 69 74 65 48 64 72 28 70 57 61 6c 29 3b 0a  WriteHdr(pWal);.
1ab33 20 20 20 20 20 20 70 57 61 6c 2d 3e 69 43 61 6c        pWal->iCal
1ab34 6c 62 61 63 6b 20 3d 20 69 46 72 61 6d 65 3b 0a  lback = iFrame;.
1ab35 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 57 41 4c      }.  }..  WAL
1ab36 54 52 41 43 45 28 28 22 57 41 4c 25 70 3a 20 66  TRACE(("WAL%p: f
1ab37 72 61 6d 65 20 77 72 69 74 65 20 25 73 5c 6e 22  rame write %s\n"
1ab38 2c 20 70 57 61 6c 2c 20 72 63 20 3f 20 22 66 61  , pWal, rc ? "fa
1ab39 69 6c 65 64 22 20 3a 20 22 6f 6b 22 29 29 3b 0a  iled" : "ok"));.
1ab3a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1ab3b 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  /* .** This rout
1ab3c 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ine is called to
1ab3d 20 69 6d 70 6c 65 6d 65 6e 74 20 73 71 6c 69 74   implement sqlit
1ab3e 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e  e3_wal_checkpoin
1ab3f 74 28 29 20 61 6e 64 0a 2a 2a 20 72 65 6c 61 74  t() and.** relat
1ab40 65 64 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a  ed interfaces..*
1ab41 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 43 48  *.** Obtain a CH
1ab42 45 43 4b 50 4f 49 4e 54 20 6c 6f 63 6b 20 61 6e  ECKPOINT lock an
1ab43 64 20 74 68 65 6e 20 62 61 63 6b 66 69 6c 6c 20  d then backfill 
1ab44 61 73 20 6d 75 63 68 20 69 6e 66 6f 72 6d 61 74  as much informat
1ab45 69 6f 6e 20 61 73 0a 2a 2a 20 77 65 20 63 61 6e  ion as.** we can
1ab46 20 66 72 6f 6d 20 57 41 4c 20 69 6e 74 6f 20 74   from WAL into t
1ab47 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  he database..**.
1ab48 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20  ** If parameter 
1ab49 78 42 75 73 79 20 69 73 20 6e 6f 74 20 4e 55 4c  xBusy is not NUL
1ab4a 4c 2c 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74  L, it is a point
1ab4b 65 72 20 74 6f 20 61 20 62 75 73 79 2d 68 61 6e  er to a busy-han
1ab4c 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b  dler.** callback
1ab4d 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
1ab4e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 75 6e  his function run
1ab4f 73 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63 68 65  s a blocking che
1ab50 63 6b 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  ckpoint..*/.SQLI
1ab51 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1ab52 71 6c 69 74 65 33 57 61 6c 43 68 65 63 6b 70 6f  qlite3WalCheckpo
1ab53 69 6e 74 28 0a 20 20 57 61 6c 20 2a 70 57 61 6c  int(.  Wal *pWal
1ab54 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1ab55 20 20 20 20 20 20 20 2f 2a 20 57 61 6c 20 63 6f         /* Wal co
1ab56 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  nnection */.  in
1ab57 74 20 65 4d 6f 64 65 2c 20 20 20 20 20 20 20 20  t eMode,        
1ab58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ab59 20 50 41 53 53 49 56 45 2c 20 46 55 4c 4c 20 6f   PASSIVE, FULL o
1ab5a 72 20 52 45 53 54 41 52 54 20 2a 2f 0a 20 20 69  r RESTART */.  i
1ab5b 6e 74 20 28 2a 78 42 75 73 79 29 28 76 6f 69 64  nt (*xBusy)(void
1ab5c 2a 29 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  *),            /
1ab5d 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61  * Function to ca
1ab5e 6c 6c 20 77 68 65 6e 20 62 75 73 79 20 2a 2f 0a  ll when busy */.
1ab5f 20 20 76 6f 69 64 20 2a 70 42 75 73 79 41 72 67    void *pBusyArg
1ab60 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1ab61 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 61 72 67    /* Context arg
1ab62 75 6d 65 6e 74 20 66 6f 72 20 78 42 75 73 79 48  ument for xBusyH
1ab63 61 6e 64 6c 65 72 20 2a 2f 0a 20 20 69 6e 74 20  andler */.  int 
1ab64 73 79 6e 63 5f 66 6c 61 67 73 2c 20 20 20 20 20  sync_flags,     
1ab65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
1ab66 6c 61 67 73 20 74 6f 20 73 79 6e 63 20 64 62 20  lags to sync db 
1ab67 66 69 6c 65 20 77 69 74 68 20 28 6f 72 20 30 29  file with (or 0)
1ab68 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 66 2c 20   */.  int nBuf, 
1ab69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ab6a 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
1ab6b 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65   temporary buffe
1ab6c 72 20 2a 2f 0a 20 20 75 38 20 2a 7a 42 75 66 2c  r */.  u8 *zBuf,
1ab6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ab6e 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72         /* Tempor
1ab6f 61 72 79 20 62 75 66 66 65 72 20 74 6f 20 75 73  ary buffer to us
1ab70 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4c 6f  e */.  int *pnLo
1ab71 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g,              
1ab72 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e         /* OUT: N
1ab73 75 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73 20  umber of frames 
1ab74 69 6e 20 57 41 4c 20 2a 2f 0a 20 20 69 6e 74 20  in WAL */.  int 
1ab75 2a 70 6e 43 6b 70 74 20 20 20 20 20 20 20 20 20  *pnCkpt         
1ab76 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1ab77 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61  UT: Number of ba
1ab78 63 6b 66 69 6c 6c 65 64 20 66 72 61 6d 65 73 20  ckfilled frames 
1ab79 69 6e 20 57 41 4c 20 2a 2f 0a 29 7b 0a 20 20 69  in WAL */.){.  i
1ab7a 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
1ab7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1ab7c 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
1ab7d 0a 20 20 69 6e 74 20 69 73 43 68 61 6e 67 65 64  .  int isChanged
1ab7e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1ab7f 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20     /* True if a 
1ab80 6e 65 77 20 77 61 6c 2d 69 6e 64 65 78 20 68 65  new wal-index he
1ab81 61 64 65 72 20 69 73 20 6c 6f 61 64 65 64 20 2a  ader is loaded *
1ab82 2f 0a 20 20 69 6e 74 20 65 4d 6f 64 65 32 20 3d  /.  int eMode2 =
1ab83 20 65 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20   eMode;         
1ab84 20 20 20 20 2f 2a 20 4d 6f 64 65 20 74 6f 20 70      /* Mode to p
1ab85 61 73 73 20 74 6f 20 77 61 6c 43 68 65 63 6b 70  ass to walCheckp
1ab86 6f 69 6e 74 28 29 20 2a 2f 0a 0a 20 20 61 73 73  oint() */..  ass
1ab87 65 72 74 28 20 70 57 61 6c 2d 3e 63 6b 70 74 4c  ert( pWal->ckptL
1ab88 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  ock==0 );.  asse
1ab89 72 74 28 20 70 57 61 6c 2d 3e 77 72 69 74 65 4c  rt( pWal->writeL
1ab8a 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28  ock==0 );..  if(
1ab8b 20 70 57 61 6c 2d 3e 72 65 61 64 4f 6e 6c 79 20   pWal->readOnly 
1ab8c 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
1ab8d 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 57 41 4c 54  READONLY;.  WALT
1ab8e 52 41 43 45 28 28 22 57 41 4c 25 70 3a 20 63 68  RACE(("WAL%p: ch
1ab8f 65 63 6b 70 6f 69 6e 74 20 62 65 67 69 6e 73 5c  eckpoint begins\
1ab90 6e 22 2c 20 70 57 61 6c 29 29 3b 0a 20 20 72 63  n", pWal));.  rc
1ab91 20 3d 20 77 61 6c 4c 6f 63 6b 45 78 63 6c 75 73   = walLockExclus
1ab92 69 76 65 28 70 57 61 6c 2c 20 57 41 4c 5f 43 4b  ive(pWal, WAL_CK
1ab93 50 54 5f 4c 4f 43 4b 2c 20 31 29 3b 0a 20 20 69  PT_LOCK, 1);.  i
1ab94 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2f 2a 20  f( rc ){.    /* 
1ab95 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69 73 20  Usually this is 
1ab96 53 51 4c 49 54 45 5f 42 55 53 59 20 6d 65 61 6e  SQLITE_BUSY mean
1ab97 69 6e 67 20 74 68 61 74 20 61 6e 6f 74 68 65 72  ing that another
1ab98 20 74 68 72 65 61 64 20 6f 72 20 70 72 6f 63 65   thread or proce
1ab99 73 73 0a 20 20 20 20 2a 2a 20 69 73 20 61 6c 72  ss.    ** is alr
1ab9a 65 61 64 79 20 72 75 6e 6e 69 6e 67 20 61 20 63  eady running a c
1ab9b 68 65 63 6b 70 6f 69 6e 74 2c 20 6f 72 20 6d 61  heckpoint, or ma
1ab9c 79 62 65 20 61 20 72 65 63 6f 76 65 72 79 2e 20  ybe a recovery. 
1ab9d 20 42 75 74 20 69 74 20 6d 69 67 68 74 0a 20 20   But it might.  
1ab9e 20 20 2a 2a 20 61 6c 73 6f 20 62 65 20 53 51 4c    ** also be SQL
1ab9f 49 54 45 5f 49 4f 45 52 52 2e 20 2a 2f 0a 20 20  ITE_IOERR. */.  
1aba0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
1aba1 0a 20 20 70 57 61 6c 2d 3e 63 6b 70 74 4c 6f 63  .  pWal->ckptLoc
1aba2 6b 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 49 66 20  k = 1;..  /* If 
1aba3 74 68 69 73 20 69 73 20 61 20 62 6c 6f 63 6b 69  this is a blocki
1aba4 6e 67 2d 63 68 65 63 6b 70 6f 69 6e 74 2c 20 74  ng-checkpoint, t
1aba5 68 65 6e 20 6f 62 74 61 69 6e 20 74 68 65 20 77  hen obtain the w
1aba6 72 69 74 65 2d 6c 6f 63 6b 20 61 73 20 77 65 6c  rite-lock as wel
1aba7 6c 0a 20 20 2a 2a 20 74 6f 20 70 72 65 76 65 6e  l.  ** to preven
1aba8 74 20 61 6e 79 20 77 72 69 74 65 72 73 20 66 72  t any writers fr
1aba9 6f 6d 20 72 75 6e 6e 69 6e 67 20 77 68 69 6c 65  om running while
1abaa 20 74 68 65 20 63 68 65 63 6b 70 6f 69 6e 74 20   the checkpoint 
1abab 69 73 20 75 6e 64 65 72 77 61 79 2e 0a 20 20 2a  is underway..  *
1abac 2a 20 54 68 69 73 20 68 61 73 20 74 6f 20 62 65  * This has to be
1abad 20 64 6f 6e 65 20 62 65 66 6f 72 65 20 74 68 65   done before the
1abae 20 63 61 6c 6c 20 74 6f 20 77 61 6c 49 6e 64 65   call to walInde
1abaf 78 52 65 61 64 48 64 72 28 29 20 62 65 6c 6f 77  xReadHdr() below
1abb0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74  ..  **.  ** If t
1abb1 68 65 20 77 72 69 74 65 72 20 6c 6f 63 6b 20 63  he writer lock c
1abb2 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65  annot be obtaine
1abb3 64 2c 20 74 68 65 6e 20 61 20 70 61 73 73 69 76  d, then a passiv
1abb4 65 20 63 68 65 63 6b 70 6f 69 6e 74 20 69 73 0a  e checkpoint is.
1abb5 20 20 2a 2a 20 72 75 6e 20 69 6e 73 74 65 61 64    ** run instead
1abb6 2e 20 53 69 6e 63 65 20 74 68 65 20 63 68 65 63  . Since the chec
1abb7 6b 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20  kpointer is not 
1abb8 68 6f 6c 64 69 6e 67 20 74 68 65 20 77 72 69 74  holding the writ
1abb9 65 72 20 6c 6f 63 6b 2c 0a 20 20 2a 2a 20 74 68  er lock,.  ** th
1abba 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74 20  ere is no point 
1abbb 69 6e 20 62 6c 6f 63 6b 69 6e 67 20 77 61 69 74  in blocking wait
1abbc 69 6e 67 20 66 6f 72 20 61 6e 79 20 72 65 61 64  ing for any read
1abbd 65 72 73 2e 20 41 73 73 75 6d 69 6e 67 20 6e 6f  ers. Assuming no
1abbe 20 0a 20 20 2a 2a 20 6f 74 68 65 72 20 65 72 72   .  ** other err
1abbf 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 69 73 20  or occurs, this 
1abc0 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 72 65  function will re
1abc1 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
1abc2 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a   to the caller..
1abc3 20 20 2a 2f 0a 20 20 69 66 28 20 65 4d 6f 64 65    */.  if( eMode
1abc4 21 3d 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f  !=SQLITE_CHECKPO
1abc5 49 4e 54 5f 50 41 53 53 49 56 45 20 29 7b 0a 20  INT_PASSIVE ){. 
1abc6 20 20 20 72 63 20 3d 20 77 61 6c 42 75 73 79 4c     rc = walBusyL
1abc7 6f 63 6b 28 70 57 61 6c 2c 20 78 42 75 73 79 2c  ock(pWal, xBusy,
1abc8 20 70 42 75 73 79 41 72 67 2c 20 57 41 4c 5f 57   pBusyArg, WAL_W
1abc9 52 49 54 45 5f 4c 4f 43 4b 2c 20 31 29 3b 0a 20  RITE_LOCK, 1);. 
1abca 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1abcb 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 57  E_OK ){.      pW
1abcc 61 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 20 3d 20  al->writeLock = 
1abcd 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  1;.    }else if(
1abce 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59   rc==SQLITE_BUSY
1abcf 20 29 7b 0a 20 20 20 20 20 20 65 4d 6f 64 65 32   ){.      eMode2
1abd0 20 3d 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 50   = SQLITE_CHECKP
1abd1 4f 49 4e 54 5f 50 41 53 53 49 56 45 3b 0a 20 20  OINT_PASSIVE;.  
1abd2 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1abd3 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  OK;.    }.  }.. 
1abd4 20 2f 2a 20 52 65 61 64 20 74 68 65 20 77 61 6c   /* Read the wal
1abd5 2d 69 6e 64 65 78 20 68 65 61 64 65 72 2e 20 2a  -index header. *
1abd6 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
1abd7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
1abd8 3d 20 77 61 6c 49 6e 64 65 78 52 65 61 64 48 64  = walIndexReadHd
1abd9 72 28 70 57 61 6c 2c 20 26 69 73 43 68 61 6e 67  r(pWal, &isChang
1abda 65 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  ed);.  }..  /* C
1abdb 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 74 68  opy data from th
1abdc 65 20 6c 6f 67 20 74 6f 20 74 68 65 20 64 61 74  e log to the dat
1abdd 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20  abase file. */. 
1abde 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1abdf 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 57  OK ){.    if( pW
1abe0 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61 6d 65 20  al->hdr.mxFrame 
1abe1 26 26 20 77 61 6c 50 61 67 65 73 69 7a 65 28 70  && walPagesize(p
1abe2 57 61 6c 29 21 3d 6e 42 75 66 20 29 7b 0a 20 20  Wal)!=nBuf ){.  
1abe3 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1abe4 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1abe5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
1abe6 63 20 3d 20 77 61 6c 43 68 65 63 6b 70 6f 69 6e  c = walCheckpoin
1abe7 74 28 70 57 61 6c 2c 20 65 4d 6f 64 65 32 2c 20  t(pWal, eMode2, 
1abe8 78 42 75 73 79 2c 20 70 42 75 73 79 41 72 67 2c  xBusy, pBusyArg,
1abe9 20 73 79 6e 63 5f 66 6c 61 67 73 2c 20 7a 42 75   sync_flags, zBu
1abea 66 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  f);.    }..    /
1abeb 2a 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63  * If no error oc
1abec 63 75 72 72 65 64 2c 20 73 65 74 20 74 68 65 20  curred, set the 
1abed 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 73  output variables
1abee 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d  . */.    if( rc=
1abef 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63  =SQLITE_OK || rc
1abf0 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b  ==SQLITE_BUSY ){
1abf1 0a 20 20 20 20 20 20 69 66 28 20 70 6e 4c 6f 67  .      if( pnLog
1abf2 20 29 20 2a 70 6e 4c 6f 67 20 3d 20 28 69 6e 74   ) *pnLog = (int
1abf3 29 70 57 61 6c 2d 3e 68 64 72 2e 6d 78 46 72 61  )pWal->hdr.mxFra
1abf4 6d 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 6e  me;.      if( pn
1abf5 43 6b 70 74 20 29 20 2a 70 6e 43 6b 70 74 20 3d  Ckpt ) *pnCkpt =
1abf6 20 28 69 6e 74 29 28 77 61 6c 43 6b 70 74 49 6e   (int)(walCkptIn
1abf7 66 6f 28 70 57 61 6c 29 2d 3e 6e 42 61 63 6b 66  fo(pWal)->nBackf
1abf8 69 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ill);.    }.  }.
1abf9 0a 20 20 69 66 28 20 69 73 43 68 61 6e 67 65 64  .  if( isChanged
1abfa 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20   ){.    /* If a 
1abfb 6e 65 77 20 77 61 6c 2d 69 6e 64 65 78 20 68 65  new wal-index he
1abfc 61 64 65 72 20 77 61 73 20 6c 6f 61 64 65 64 20  ader was loaded 
1abfd 62 65 66 6f 72 65 20 74 68 65 20 63 68 65 63 6b  before the check
1abfe 70 6f 69 6e 74 20 77 61 73 20 0a 20 20 20 20 2a  point was .    *
1abff 2a 20 70 65 72 66 6f 72 6d 65 64 2c 20 74 68 65  * performed, the
1ac00 6e 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68  n the pager-cach
1ac01 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
1ac02 68 20 70 57 61 6c 20 69 73 20 6e 6f 77 0a 20 20  h pWal is now.  
1ac03 20 20 2a 2a 20 6f 75 74 20 6f 66 20 64 61 74 65    ** out of date
1ac04 2e 20 53 6f 20 7a 65 72 6f 20 74 68 65 20 63 61  . So zero the ca
1ac05 63 68 65 64 20 77 61 6c 2d 69 6e 64 65 78 20 68  ched wal-index h
1ac06 65 61 64 65 72 20 74 6f 20 65 6e 73 75 72 65 20  eader to ensure 
1ac07 74 68 61 74 0a 20 20 20 20 2a 2a 20 6e 65 78 74  that.    ** next
1ac08 20 74 69 6d 65 20 74 68 65 20 70 61 67 65 72 20   time the pager 
1ac09 6f 70 65 6e 73 20 61 20 73 6e 61 70 73 68 6f 74  opens a snapshot
1ac0a 20 6f 6e 20 74 68 69 73 20 64 61 74 61 62 61 73   on this databas
1ac0b 65 20 69 74 20 6b 6e 6f 77 73 20 74 68 61 74 0a  e it knows that.
1ac0c 20 20 20 20 2a 2a 20 74 68 65 20 63 61 63 68 65      ** the cache
1ac0d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 73   needs to be res
1ac0e 65 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d  et..    */.    m
1ac0f 65 6d 73 65 74 28 26 70 57 61 6c 2d 3e 68 64 72  emset(&pWal->hdr
1ac10 2c 20 30 2c 20 73 69 7a 65 6f 66 28 57 61 6c 49  , 0, sizeof(WalI
1ac11 6e 64 65 78 48 64 72 29 29 3b 0a 20 20 7d 0a 0a  ndexHdr));.  }..
1ac12 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65    /* Release the
1ac13 20 6c 6f 63 6b 73 2e 20 2a 2f 0a 20 20 73 71 6c   locks. */.  sql
1ac14 69 74 65 33 57 61 6c 45 6e 64 57 72 69 74 65 54  ite3WalEndWriteT
1ac15 72 61 6e 73 61 63 74 69 6f 6e 28 70 57 61 6c 29  ransaction(pWal)
1ac16 3b 0a 20 20 77 61 6c 55 6e 6c 6f 63 6b 45 78 63  ;.  walUnlockExc
1ac17 6c 75 73 69 76 65 28 70 57 61 6c 2c 20 57 41 4c  lusive(pWal, WAL
1ac18 5f 43 4b 50 54 5f 4c 4f 43 4b 2c 20 31 29 3b 0a  _CKPT_LOCK, 1);.
1ac19 20 20 70 57 61 6c 2d 3e 63 6b 70 74 4c 6f 63 6b    pWal->ckptLock
1ac1a 20 3d 20 30 3b 0a 20 20 57 41 4c 54 52 41 43 45   = 0;.  WALTRACE
1ac1b 28 28 22 57 41 4c 25 70 3a 20 63 68 65 63 6b 70  (("WAL%p: checkp
1ac1c 6f 69 6e 74 20 25 73 5c 6e 22 2c 20 70 57 61 6c  oint %s\n", pWal
1ac1d 2c 20 72 63 20 3f 20 22 66 61 69 6c 65 64 22 20  , rc ? "failed" 
1ac1e 3a 20 22 6f 6b 22 29 29 3b 0a 20 20 72 65 74 75  : "ok"));.  retu
1ac1f 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rn (rc==SQLITE_O
1ac20 4b 20 26 26 20 65 4d 6f 64 65 21 3d 65 4d 6f 64  K && eMode!=eMod
1ac21 65 32 20 3f 20 53 51 4c 49 54 45 5f 42 55 53 59  e2 ? SQLITE_BUSY
1ac22 20 3a 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 20 52 65   : rc);.}../* Re
1ac23 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 74  turn the value t
1ac24 6f 20 70 61 73 73 20 74 6f 20 61 20 73 71 6c 69  o pass to a sqli
1ac25 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b 20 63 61 6c  te3_wal_hook cal
1ac26 6c 62 61 63 6b 2c 20 74 68 65 0a 2a 2a 20 6e 75  lback, the.** nu
1ac27 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73 20 69  mber of frames i
1ac28 6e 20 74 68 65 20 57 41 4c 20 61 74 20 74 68 65  n the WAL at the
1ac29 20 70 6f 69 6e 74 20 6f 66 20 74 68 65 20 6c 61   point of the la
1ac2a 73 74 20 63 6f 6d 6d 69 74 20 73 69 6e 63 65 0a  st commit since.
1ac2b 2a 2a 20 73 71 6c 69 74 65 33 57 61 6c 43 61 6c  ** sqlite3WalCal
1ac2c 6c 62 61 63 6b 28 29 20 77 61 73 20 63 61 6c 6c  lback() was call
1ac2d 65 64 2e 20 20 49 66 20 6e 6f 20 63 6f 6d 6d 69  ed.  If no commi
1ac2e 74 73 20 68 61 76 65 20 6f 63 63 75 72 72 65 64  ts have occurred
1ac2f 20 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20 6c 61   since.** the la
1ac30 73 74 20 63 61 6c 6c 2c 20 74 68 65 6e 20 72 65  st call, then re
1ac31 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54  turn 0..*/.SQLIT
1ac32 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1ac33 6c 69 74 65 33 57 61 6c 43 61 6c 6c 62 61 63 6b  lite3WalCallback
1ac34 28 57 61 6c 20 2a 70 57 61 6c 29 7b 0a 20 20 75  (Wal *pWal){.  u
1ac35 33 32 20 72 65 74 20 3d 20 30 3b 0a 20 20 69 66  32 ret = 0;.  if
1ac36 28 20 70 57 61 6c 20 29 7b 0a 20 20 20 20 72 65  ( pWal ){.    re
1ac37 74 20 3d 20 70 57 61 6c 2d 3e 69 43 61 6c 6c 62  t = pWal->iCallb
1ac38 61 63 6b 3b 0a 20 20 20 20 70 57 61 6c 2d 3e 69  ack;.    pWal->i
1ac39 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20  Callback = 0;.  
1ac3a 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29  }.  return (int)
1ac3b 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ret;.}../*.** Th
1ac3c 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
1ac3d 61 6c 6c 65 64 20 74 6f 20 63 68 61 6e 67 65 20  alled to change 
1ac3e 74 68 65 20 57 41 4c 20 73 75 62 73 79 73 74 65  the WAL subsyste
1ac3f 6d 20 69 6e 74 6f 20 6f 72 20 6f 75 74 0a 2a 2a  m into or out.**
1ac40 20 6f 66 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65   of locking_mode
1ac41 3d 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2a 0a 2a  =EXCLUSIVE..**.*
1ac42 2a 20 49 66 20 6f 70 20 69 73 20 7a 65 72 6f 2c  * If op is zero,
1ac43 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 74 6f   then attempt to
1ac44 20 63 68 61 6e 67 65 20 66 72 6f 6d 20 6c 6f 63   change from loc
1ac45 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53  king_mode=EXCLUS
1ac46 49 56 45 0a 2a 2a 20 69 6e 74 6f 20 6c 6f 63 6b  IVE.** into lock
1ac47 69 6e 67 5f 6d 6f 64 65 3d 4e 4f 52 4d 41 4c 2e  ing_mode=NORMAL.
1ac48 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61    This means tha
1ac49 74 20 77 65 20 6d 75 73 74 20 61 63 71 75 69 72  t we must acquir
1ac4a 65 20 61 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 74  e a lock.** on t
1ac4b 68 65 20 70 57 61 6c 2d 3e 72 65 61 64 4c 6f 63  he pWal->readLoc
1ac4c 6b 20 62 79 74 65 2e 20 20 49 66 20 74 68 65 20  k byte.  If the 
1ac4d 57 41 4c 20 69 73 20 61 6c 72 65 61 64 79 20 69  WAL is already i
1ac4e 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 4e  n locking_mode=N
1ac4f 4f 52 4d 41 4c 0a 2a 2a 20 6f 72 20 69 66 20 74  ORMAL.** or if t
1ac50 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f  he acquisition o
1ac51 66 20 74 68 65 20 6c 6f 63 6b 20 66 61 69 6c 73  f the lock fails
1ac52 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 30 2e  , then return 0.
1ac53 20 20 49 66 20 74 68 65 0a 2a 2a 20 74 72 61 6e    If the.** tran
1ac54 73 69 74 69 6f 6e 20 6f 75 74 20 6f 66 20 65 78  sition out of ex
1ac55 63 6c 75 73 69 76 65 2d 6d 6f 64 65 20 69 73 20  clusive-mode is 
1ac56 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75  successful, retu
1ac57 72 6e 20 31 2e 20 20 54 68 69 73 0a 2a 2a 20 6f  rn 1.  This.** o
1ac58 70 65 72 61 74 69 6f 6e 20 6d 75 73 74 20 6f 63  peration must oc
1ac59 63 75 72 20 77 68 69 6c 65 20 74 68 65 20 70 61  cur while the pa
1ac5a 67 65 72 20 69 73 20 73 74 69 6c 6c 20 68 6f 6c  ger is still hol
1ac5b 64 69 6e 67 20 74 68 65 20 65 78 63 6c 75 73 69  ding the exclusi
1ac5c 76 65 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 68  ve.** lock on th
1ac5d 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1ac5e 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6f  file..**.** If o
1ac5f 70 20 69 73 20 6f 6e 65 2c 20 74 68 65 6e 20 63  p is one, then c
1ac60 68 61 6e 67 65 20 66 72 6f 6d 20 6c 6f 63 6b 69  hange from locki
1ac61 6e 67 5f 6d 6f 64 65 3d 4e 4f 52 4d 41 4c 20 69  ng_mode=NORMAL i
1ac62 6e 74 6f 20 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 5f  nto .** locking_
1ac63 6d 6f 64 65 3d 45 58 43 4c 55 53 49 56 45 2e 20  mode=EXCLUSIVE. 
1ac64 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74   This means that
1ac65 20 74 68 65 20 70 57 61 6c 2d 3e 72 65 61 64 4c   the pWal->readL
1ac66 6f 63 6b 20 6d 75 73 74 0a 2a 2a 20 62 65 20 72  ock must.** be r
1ac67 65 6c 65 61 73 65 64 2e 20 20 52 65 74 75 72 6e  eleased.  Return
1ac68 20 31 20 69 66 20 74 68 65 20 74 72 61 6e 73 69   1 if the transi
1ac69 74 69 6f 6e 20 69 73 20 6d 61 64 65 20 61 6e 64  tion is made and
1ac6a 20 30 20 69 66 20 74 68 65 0a 2a 2a 20 57 41 4c   0 if the.** WAL
1ac6b 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 65   is already in e
1ac6c 78 63 6c 75 73 69 76 65 2d 6c 6f 63 6b 69 6e 67  xclusive-locking
1ac6d 20 6d 6f 64 65 20 2d 20 6d 65 61 6e 69 6e 67 20   mode - meaning 
1ac6e 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 72 6f 75  that this.** rou
1ac6f 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
1ac70 20 20 54 68 65 20 70 61 67 65 72 20 6d 75 73 74    The pager must
1ac71 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 74 68   already hold th
1ac72 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  e exclusive lock
1ac73 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20  .** on the main 
1ac74 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 65  database file be
1ac75 66 6f 72 65 20 69 6e 76 6f 6b 69 6e 67 20 74 68  fore invoking th
1ac76 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a  is operation..**
1ac77 0a 2a 2a 20 49 66 20 6f 70 20 69 73 20 6e 65 67  .** If op is neg
1ac78 61 74 69 76 65 2c 20 74 68 65 6e 20 64 6f 20 61  ative, then do a
1ac79 20 64 72 79 2d 72 75 6e 20 6f 66 20 74 68 65 20   dry-run of the 
1ac7a 6f 70 3d 3d 31 20 63 61 73 65 20 62 75 74 20 64  op==1 case but d
1ac7b 6f 0a 2a 2a 20 6e 6f 74 20 61 63 74 75 61 6c 6c  o.** not actuall
1ac7c 79 20 63 68 61 6e 67 65 20 61 6e 79 74 68 69 6e  y change anythin
1ac7d 67 2e 20 54 68 65 20 70 61 67 65 72 20 75 73 65  g. The pager use
1ac7e 73 20 74 68 69 73 20 74 6f 20 73 65 65 20 69 66  s this to see if
1ac7f 20 69 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 61 63   it.** should ac
1ac80 71 75 69 72 65 20 74 68 65 20 64 61 74 61 62 61  quire the databa
1ac81 73 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  se exclusive loc
1ac82 6b 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b  k prior to invok
1ac83 69 6e 67 0a 2a 2a 20 74 68 65 20 6f 70 3d 3d 31  ing.** the op==1
1ac84 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   case..*/.SQLITE
1ac85 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1ac86 69 74 65 33 57 61 6c 45 78 63 6c 75 73 69 76 65  ite3WalExclusive
1ac87 4d 6f 64 65 28 57 61 6c 20 2a 70 57 61 6c 2c 20  Mode(Wal *pWal, 
1ac88 69 6e 74 20 6f 70 29 7b 0a 20 20 69 6e 74 20 72  int op){.  int r
1ac89 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 61  c;.  assert( pWa
1ac8a 6c 2d 3e 77 72 69 74 65 4c 6f 63 6b 3d 3d 30 20  l->writeLock==0 
1ac8b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 61  );.  assert( pWa
1ac8c 6c 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  l->exclusiveMode
1ac8d 21 3d 57 41 4c 5f 48 45 41 50 4d 45 4d 4f 52 59  !=WAL_HEAPMEMORY
1ac8e 5f 4d 4f 44 45 20 7c 7c 20 6f 70 3d 3d 2d 31 20  _MODE || op==-1 
1ac8f 29 3b 0a 0a 20 20 2f 2a 20 70 57 61 6c 2d 3e 72  );..  /* pWal->r
1ac90 65 61 64 4c 6f 63 6b 20 69 73 20 75 73 75 61 6c  eadLock is usual
1ac91 6c 79 20 73 65 74 2c 20 62 75 74 20 6d 69 67 68  ly set, but migh
1ac92 74 20 62 65 20 2d 31 20 69 66 20 74 68 65 72 65  t be -1 if there
1ac93 20 77 61 73 20 61 20 0a 20 20 2a 2a 20 70 72 69   was a .  ** pri
1ac94 6f 72 20 65 72 72 6f 72 20 77 68 69 6c 65 20 61  or error while a
1ac95 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 61 63 71  ttempting to acq
1ac96 75 69 72 65 20 61 72 65 20 72 65 61 64 2d 6c 6f  uire are read-lo
1ac97 63 6b 2e 20 54 68 69 73 20 63 61 6e 6e 6f 74 20  ck. This cannot 
1ac98 0a 20 20 2a 2a 20 68 61 70 70 65 6e 20 69 66 20  .  ** happen if 
1ac99 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  the connection i
1ac9a 73 20 61 63 74 75 61 6c 6c 79 20 69 6e 20 65 78  s actually in ex
1ac9b 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 28 61 73  clusive mode (as
1ac9c 20 6e 6f 20 78 53 68 6d 4c 6f 63 6b 0a 20 20 2a   no xShmLock.  *
1ac9d 2a 20 6c 6f 63 6b 73 20 61 72 65 20 74 61 6b 65  * locks are take
1ac9e 6e 20 69 6e 20 74 68 69 73 20 63 61 73 65 29 2e  n in this case).
1ac9f 20 4e 6f 72 20 73 68 6f 75 6c 64 20 74 68 65 20   Nor should the 
1aca0 70 61 67 65 72 20 61 74 74 65 6d 70 74 20 74 6f  pager attempt to
1aca1 0a 20 20 2a 2a 20 75 70 67 72 61 64 65 20 74 6f  .  ** upgrade to
1aca2 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 20   exclusive-mode 
1aca3 66 6f 6c 6c 6f 77 69 6e 67 20 73 75 63 68 20 61  following such a
1aca4 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20  n error..  */.  
1aca5 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 72 65  assert( pWal->re
1aca6 61 64 4c 6f 63 6b 3e 3d 30 20 7c 7c 20 70 57 61  adLock>=0 || pWa
1aca7 6c 2d 3e 6c 6f 63 6b 45 72 72 6f 72 20 29 3b 0a  l->lockError );.
1aca8 20 20 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e    assert( pWal->
1aca9 72 65 61 64 4c 6f 63 6b 3e 3d 30 20 7c 7c 20 28  readLock>=0 || (
1acaa 6f 70 3c 3d 30 20 26 26 20 70 57 61 6c 2d 3e 65  op<=0 && pWal->e
1acab 78 63 6c 75 73 69 76 65 4d 6f 64 65 3d 3d 30 29  xclusiveMode==0)
1acac 20 29 3b 0a 0a 20 20 69 66 28 20 6f 70 3d 3d 30   );..  if( op==0
1acad 20 29 7b 0a 20 20 20 20 69 66 28 20 70 57 61 6c   ){.    if( pWal
1acae 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20  ->exclusiveMode 
1acaf 29 7b 0a 20 20 20 20 20 20 70 57 61 6c 2d 3e 65  ){.      pWal->e
1acb0 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 30  xclusiveMode = 0
1acb1 3b 0a 20 20 20 20 20 20 69 66 28 20 77 61 6c 4c  ;.      if( walL
1acb2 6f 63 6b 53 68 61 72 65 64 28 70 57 61 6c 2c 20  ockShared(pWal, 
1acb3 57 41 4c 5f 52 45 41 44 5f 4c 4f 43 4b 28 70 57  WAL_READ_LOCK(pW
1acb4 61 6c 2d 3e 72 65 61 64 4c 6f 63 6b 29 29 21 3d  al->readLock))!=
1acb5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1acb6 20 20 20 20 20 70 57 61 6c 2d 3e 65 78 63 6c 75       pWal->exclu
1acb7 73 69 76 65 4d 6f 64 65 20 3d 20 31 3b 0a 20 20  siveMode = 1;.  
1acb8 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d      }.      rc =
1acb9 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69 76 65   pWal->exclusive
1acba 4d 6f 64 65 3d 3d 30 3b 0a 20 20 20 20 7d 65 6c  Mode==0;.    }el
1acbb 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6c 72  se{.      /* Alr
1acbc 65 61 64 79 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f  eady in locking_
1acbd 6d 6f 64 65 3d 4e 4f 52 4d 41 4c 20 2a 2f 0a 20  mode=NORMAL */. 
1acbe 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
1acbf 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f   }.  }else if( o
1acc0 70 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  p>0 ){.    asser
1acc1 74 28 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69  t( pWal->exclusi
1acc2 76 65 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 20  veMode==0 );.   
1acc3 20 61 73 73 65 72 74 28 20 70 57 61 6c 2d 3e 72   assert( pWal->r
1acc4 65 61 64 4c 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20  eadLock>=0 );.  
1acc5 20 20 77 61 6c 55 6e 6c 6f 63 6b 53 68 61 72 65    walUnlockShare
1acc6 64 28 70 57 61 6c 2c 20 57 41 4c 5f 52 45 41 44  d(pWal, WAL_READ
1acc7 5f 4c 4f 43 4b 28 70 57 61 6c 2d 3e 72 65 61 64  _LOCK(pWal->read
1acc8 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 70 57 61 6c  Lock));.    pWal
1acc9 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20  ->exclusiveMode 
1acca 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b  = 1;.    rc = 1;
1accb 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
1accc 20 3d 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73 69   = pWal->exclusi
1accd 76 65 4d 6f 64 65 3d 3d 30 3b 0a 20 20 7d 0a 20  veMode==0;.  }. 
1acce 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1accf 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  * .** Return tru
1acd0 65 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e  e if the argumen
1acd1 74 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c 20 61 6e  t is non-NULL an
1acd2 64 20 74 68 65 20 57 41 4c 20 6d 6f 64 75 6c 65  d the WAL module
1acd3 20 69 73 20 75 73 69 6e 67 0a 2a 2a 20 68 65 61   is using.** hea
1acd4 70 2d 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65  p-memory for the
1acd5 20 77 61 6c 2d 69 6e 64 65 78 2e 20 4f 74 68 65   wal-index. Othe
1acd6 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 61 72  rwise, if the ar
1acd7 67 75 6d 65 6e 74 20 69 73 20 4e 55 4c 4c 20 6f  gument is NULL o
1acd8 72 20 74 68 65 0a 2a 2a 20 57 41 4c 20 6d 6f 64  r the.** WAL mod
1acd9 75 6c 65 20 69 73 20 75 73 69 6e 67 20 73 68 61  ule is using sha
1acda 72 65 64 2d 6d 65 6d 6f 72 79 2c 20 72 65 74 75  red-memory, retu
1acdb 72 6e 20 66 61 6c 73 65 2e 20 0a 2a 2f 0a 53 51  rn false. .*/.SQ
1acdc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1acdd 20 73 71 6c 69 74 65 33 57 61 6c 48 65 61 70 4d   sqlite3WalHeapM
1acde 65 6d 6f 72 79 28 57 61 6c 20 2a 70 57 61 6c 29  emory(Wal *pWal)
1acdf 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 57 61 6c  {.  return (pWal
1ace0 20 26 26 20 70 57 61 6c 2d 3e 65 78 63 6c 75 73   && pWal->exclus
1ace1 69 76 65 4d 6f 64 65 3d 3d 57 41 4c 5f 48 45 41  iveMode==WAL_HEA
1ace2 50 4d 45 4d 4f 52 59 5f 4d 4f 44 45 20 29 3b 0a  PMEMORY_MODE );.
1ace3 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
1ace4 5f 45 4e 41 42 4c 45 5f 5a 49 50 56 46 53 0a 2f  _ENABLE_ZIPVFS./
1ace5 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75  *.** If the argu
1ace6 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  ment is not NULL
1ace7 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61  , it points to a
1ace8 20 57 61 6c 20 6f 62 6a 65 63 74 20 74 68 61 74   Wal object that
1ace9 20 68 6f 6c 64 73 20 61 0a 2a 2a 20 72 65 61 64   holds a.** read
1acea 2d 6c 6f 63 6b 2e 20 54 68 69 73 20 66 75 6e 63  -lock. This func
1aceb 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
1acec 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2d 73   database page-s
1aced 69 7a 65 20 69 66 20 69 74 20 69 73 20 6b 6e 6f  ize if it is kno
1acee 77 6e 2c 0a 2a 2a 20 6f 72 20 7a 65 72 6f 20 69  wn,.** or zero i
1acef 66 20 69 74 20 69 73 20 6e 6f 74 20 28 6f 72 20  f it is not (or 
1acf0 69 66 20 70 57 61 6c 20 69 73 20 4e 55 4c 4c 29  if pWal is NULL)
1acf1 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1acf2 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
1acf3 61 6c 46 72 61 6d 65 73 69 7a 65 28 57 61 6c 20  alFramesize(Wal 
1acf4 2a 70 57 61 6c 29 7b 0a 20 20 61 73 73 65 72 74  *pWal){.  assert
1acf5 28 20 70 57 61 6c 3d 3d 30 20 7c 7c 20 70 57 61  ( pWal==0 || pWa
1acf6 6c 2d 3e 72 65 61 64 4c 6f 63 6b 3e 3d 30 20 29  l->readLock>=0 )
1acf7 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 57 61 6c  ;.  return (pWal
1acf8 20 3f 20 70 57 61 6c 2d 3e 73 7a 50 61 67 65 20   ? pWal->szPage 
1acf9 3a 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  : 0);.}.#endif..
1acfa 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65  #endif /* #ifnde
1acfb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41  f SQLITE_OMIT_WA
1acfc 4c 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  L */../*********
1acfd 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 77 61 6c  ***** End of wal
1acfe 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1acff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad01 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1ad02 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1ad03 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a   btmutex.c *****
1ad04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad06 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
1ad07 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a   August 27.**.**
1ad08 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1ad09 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1ad0a 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1ad0b 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1ad0c 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1ad0d 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1ad0e 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1ad0f 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1ad10 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1ad11 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1ad12 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1ad13 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1ad14 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1ad15 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1ad16 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1ad17 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1ad18 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1ad19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
1ad1e 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
1ad1f 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20  ns code used to 
1ad20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65  implement mutexe
1ad21 73 20 6f 6e 20 42 74 72 65 65 20 6f 62 6a 65 63  s on Btree objec
1ad22 74 73 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65  ts..** This code
1ad23 20 72 65 61 6c 6c 79 20 62 65 6c 6f 6e 67 73 20   really belongs 
1ad24 69 6e 20 62 74 72 65 65 2e 63 2e 20 20 42 75 74  in btree.c.  But
1ad25 20 62 74 72 65 65 2e 63 20 69 73 20 67 65 74 74   btree.c is gett
1ad26 69 6e 67 20 74 6f 6f 0a 2a 2a 20 62 69 67 20 61  ing too.** big a
1ad27 6e 64 20 77 65 20 77 61 6e 74 20 74 6f 20 62 72  nd we want to br
1ad28 65 61 6b 20 69 74 20 64 6f 77 6e 20 73 6f 6d 65  eak it down some
1ad29 2e 20 20 54 68 69 73 20 70 61 63 6b 61 67 65 64  .  This packaged
1ad2a 20 73 65 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20   seemed like.** 
1ad2b 61 20 67 6f 6f 64 20 62 72 65 61 6b 6f 75 74 2e  a good breakout.
1ad2c 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
1ad2d 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 65  *** Include btre
1ad2e 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69  eInt.h in the mi
1ad2f 64 64 6c 65 20 6f 66 20 62 74 6d 75 74 65 78 2e  ddle of btmutex.
1ad30 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1ad31 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1ad32 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62  *** Begin file b
1ad33 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  treeInt.h ******
1ad34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad36 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41  **/./*.** 2004 A
1ad37 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65  pril 6.**.** The
1ad38 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
1ad39 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
1ad3a 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
1ad3b 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
1ad3c 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
1ad3d 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
1ad3e 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1ad3f 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1ad40 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1ad41 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1ad42 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1ad43 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1ad44 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
1ad45 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
1ad46 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
1ad47 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
1ad48 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
1ad49 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ad4d 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
1ad4e 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  le implements a 
1ad4f 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62  external (disk-b
1ad50 61 73 65 64 29 20 64 61 74 61 62 61 73 65 20 75  ased) database u
1ad51 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20  sing BTrees..** 
1ad52 46 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64  For a detailed d
1ad53 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 42 54 72  iscussion of BTr
1ad54 65 65 73 2c 20 72 65 66 65 72 20 74 6f 0a 2a 2a  ees, refer to.**
1ad55 0a 2a 2a 20 20 20 20 20 44 6f 6e 61 6c 64 20 45  .**     Donald E
1ad56 2e 20 4b 6e 75 74 68 2c 20 54 48 45 20 41 52 54  . Knuth, THE ART
1ad57 20 4f 46 20 43 4f 4d 50 55 54 45 52 20 50 52 4f   OF COMPUTER PRO
1ad58 47 52 41 4d 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65  GRAMMING, Volume
1ad59 20 33 3a 0a 2a 2a 20 20 20 20 20 22 53 6f 72 74   3:.**     "Sort
1ad5a 69 6e 67 20 41 6e 64 20 53 65 61 72 63 68 69 6e  ing And Searchin
1ad5b 67 22 2c 20 70 61 67 65 73 20 34 37 33 2d 34 38  g", pages 473-48
1ad5c 30 2e 20 41 64 64 69 73 6f 6e 2d 57 65 73 6c 65  0. Addison-Wesle
1ad5d 79 0a 2a 2a 20 20 20 20 20 50 75 62 6c 69 73 68  y.**     Publish
1ad5e 69 6e 67 20 43 6f 6d 70 61 6e 79 2c 20 52 65 61  ing Company, Rea
1ad5f 64 69 6e 67 2c 20 4d 61 73 73 61 63 68 75 73 65  ding, Massachuse
1ad60 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62  tts..**.** The b
1ad61 61 73 69 63 20 69 64 65 61 20 69 73 20 74 68 61  asic idea is tha
1ad62 74 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74  t each page of t
1ad63 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  he file contains
1ad64 20 4e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 65   N database.** e
1ad65 6e 74 72 69 65 73 20 61 6e 64 20 4e 2b 31 20 70  ntries and N+1 p
1ad66 6f 69 6e 74 65 72 73 20 74 6f 20 73 75 62 70 61  ointers to subpa
1ad67 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d  ges..**.**   ---
1ad68 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1ad69 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1ad6a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1ad6b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
1ad6c 20 20 20 7c 20 20 50 74 72 28 30 29 20 7c 20 4b     |  Ptr(0) | K
1ad6d 65 79 28 30 29 20 7c 20 50 74 72 28 31 29 20 7c  ey(0) | Ptr(1) |
1ad6e 20 4b 65 79 28 31 29 20 7c 20 2e 2e 2e 20 7c 20   Key(1) | ... | 
1ad6f 4b 65 79 28 4e 2d 31 29 20 7c 20 50 74 72 28 4e  Key(N-1) | Ptr(N
1ad70 29 20 7c 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  ) |.**   -------
1ad71 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1ad72 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1ad73 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1ad74 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20  ---------.**.** 
1ad75 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20  All of the keys 
1ad76 6f 6e 20 74 68 65 20 70 61 67 65 20 74 68 61 74  on the page that
1ad77 20 50 74 72 28 30 29 20 70 6f 69 6e 74 73 20 74   Ptr(0) points t
1ad78 6f 20 68 61 76 65 20 76 61 6c 75 65 73 20 6c 65  o have values le
1ad79 73 73 0a 2a 2a 20 74 68 61 6e 20 4b 65 79 28 30  ss.** than Key(0
1ad7a 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b  ).  All of the k
1ad7b 65 79 73 20 6f 6e 20 70 61 67 65 20 50 74 72 28  eys on page Ptr(
1ad7c 31 29 20 61 6e 64 20 69 74 73 20 73 75 62 70 61  1) and its subpa
1ad7d 67 65 73 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75  ges have.** valu
1ad7e 65 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  es greater than 
1ad7f 4b 65 79 28 30 29 20 61 6e 64 20 6c 65 73 73 20  Key(0) and less 
1ad80 74 68 61 6e 20 4b 65 79 28 31 29 2e 20 20 41 6c  than Key(1).  Al
1ad81 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 0a 2a 2a  l of the keys.**
1ad82 20 6f 6e 20 50 74 72 28 4e 29 20 61 6e 64 20 69   on Ptr(N) and i
1ad83 74 73 20 73 75 62 70 61 67 65 73 20 68 61 76 65  ts subpages have
1ad84 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20   values greater 
1ad85 74 68 61 6e 20 4b 65 79 28 4e 2d 31 29 2e 20 20  than Key(N-1).  
1ad86 41 6e 64 0a 2a 2a 20 73 6f 20 66 6f 72 74 68 2e  And.** so forth.
1ad87 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 69 6e 67 20 61  .**.** Finding a
1ad88 20 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79 20   particular key 
1ad89 72 65 71 75 69 72 65 73 20 72 65 61 64 69 6e 67  requires reading
1ad8a 20 4f 28 6c 6f 67 28 4d 29 29 20 70 61 67 65 73   O(log(M)) pages
1ad8b 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 64 69   from the .** di
1ad8c 73 6b 20 77 68 65 72 65 20 4d 20 69 73 20 74 68  sk where M is th
1ad8d 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
1ad8e 69 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 2e  ies in the tree.
1ad8f 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69  .**.** In this i
1ad90 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 61  mplementation, a
1ad91 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 63 61 6e   single file can
1ad92 20 68 6f 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f 72   hold one or mor
1ad93 65 20 73 65 70 61 72 61 74 65 20 0a 2a 2a 20 42  e separate .** B
1ad94 54 72 65 65 73 2e 20 20 45 61 63 68 20 42 54 72  Trees.  Each BTr
1ad95 65 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64  ee is identified
1ad96 20 62 79 20 74 68 65 20 69 6e 64 65 78 20 6f 66   by the index of
1ad97 20 69 74 73 20 72 6f 6f 74 20 70 61 67 65 2e 20   its root page. 
1ad98 20 54 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20   The.** key and 
1ad99 64 61 74 61 20 66 6f 72 20 61 6e 79 20 65 6e 74  data for any ent
1ad9a 72 79 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20  ry are combined 
1ad9b 74 6f 20 66 6f 72 6d 20 74 68 65 20 22 70 61 79  to form the "pay
1ad9c 6c 6f 61 64 22 2e 20 20 41 0a 2a 2a 20 66 69 78  load".  A.** fix
1ad9d 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79  ed amount of pay
1ad9e 6c 6f 61 64 20 63 61 6e 20 62 65 20 63 61 72 72  load can be carr
1ad9f 69 65 64 20 64 69 72 65 63 74 6c 79 20 6f 6e 20  ied directly on 
1ada0 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
1ada1 70 61 67 65 2e 20 20 49 66 20 74 68 65 20 70 61  page.  If the pa
1ada2 79 6c 6f 61 64 20 69 73 20 6c 61 72 67 65 72 20  yload is larger 
1ada3 74 68 61 6e 20 74 68 65 20 70 72 65 73 65 74 20  than the preset 
1ada4 61 6d 6f 75 6e 74 20 74 68 65 6e 20 73 75 72 70  amount then surp
1ada5 6c 75 73 0a 2a 2a 20 62 79 74 65 73 20 61 72 65  lus.** bytes are
1ada6 20 73 74 6f 72 65 64 20 6f 6e 20 6f 76 65 72 66   stored on overf
1ada7 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20  low pages.  The 
1ada8 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 6e 20 65  payload for an e
1ada9 6e 74 72 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20  ntry.** and the 
1adaa 70 72 65 63 65 64 69 6e 67 20 70 6f 69 6e 74 65  preceding pointe
1adab 72 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74  r are combined t
1adac 6f 20 66 6f 72 6d 20 61 20 22 43 65 6c 6c 22 2e  o form a "Cell".
1adad 20 20 45 61 63 68 20 0a 2a 2a 20 70 61 67 65 20    Each .** page 
1adae 68 61 73 20 61 20 73 6d 61 6c 6c 20 68 65 61 64  has a small head
1adaf 65 72 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e  er which contain
1adb0 73 20 74 68 65 20 50 74 72 28 4e 29 20 70 6f 69  s the Ptr(N) poi
1adb1 6e 74 65 72 20 61 6e 64 20 6f 74 68 65 72 0a 2a  nter and other.*
1adb2 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 75  * information su
1adb3 63 68 20 61 73 20 74 68 65 20 73 69 7a 65 20 6f  ch as the size o
1adb4 66 20 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a  f key and data..
1adb5 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 54 20 44 45 54  **.** FORMAT DET
1adb6 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  AILS.**.** The f
1adb7 69 6c 65 20 69 73 20 64 69 76 69 64 65 64 20 69  ile is divided i
1adb8 6e 74 6f 20 70 61 67 65 73 2e 20 20 54 68 65 20  nto pages.  The 
1adb9 66 69 72 73 74 20 70 61 67 65 20 69 73 20 63 61  first page is ca
1adba 6c 6c 65 64 20 70 61 67 65 20 31 2c 0a 2a 2a 20  lled page 1,.** 
1adbb 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 70 61  the second is pa
1adbc 67 65 20 32 2c 20 61 6e 64 20 73 6f 20 66 6f 72  ge 2, and so for
1adbd 74 68 2e 20 20 41 20 70 61 67 65 20 6e 75 6d 62  th.  A page numb
1adbe 65 72 20 6f 66 20 7a 65 72 6f 20 69 6e 64 69 63  er of zero indic
1adbf 61 74 65 73 0a 2a 2a 20 22 6e 6f 20 73 75 63 68  ates.** "no such
1adc0 20 70 61 67 65 22 2e 20 20 54 68 65 20 70 61 67   page".  The pag
1adc1 65 20 73 69 7a 65 20 63 61 6e 20 62 65 20 61 6e  e size can be an
1adc2 79 20 70 6f 77 65 72 20 6f 66 20 32 20 62 65 74  y power of 2 bet
1adc3 77 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35  ween 512 and 655
1adc4 33 36 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65  36..** Each page
1adc5 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61   can be either a
1adc6 20 62 74 72 65 65 20 70 61 67 65 2c 20 61 20 66   btree page, a f
1adc7 72 65 65 6c 69 73 74 20 70 61 67 65 2c 20 61 6e  reelist page, an
1adc8 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67   overflow.** pag
1adc9 65 2c 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 2d  e, or a pointer-
1adca 6d 61 70 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  map page..**.** 
1adcb 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69  The first page i
1adcc 73 20 61 6c 77 61 79 73 20 61 20 62 74 72 65 65  s always a btree
1adcd 20 70 61 67 65 2e 20 20 54 68 65 20 66 69 72 73   page.  The firs
1adce 74 20 31 30 30 20 62 79 74 65 73 20 6f 66 20 74  t 100 bytes of t
1adcf 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 67 65  he first.** page
1add0 20 63 6f 6e 74 61 69 6e 20 61 20 73 70 65 63 69   contain a speci
1add1 61 6c 20 68 65 61 64 65 72 20 28 74 68 65 20 22  al header (the "
1add2 66 69 6c 65 20 68 65 61 64 65 72 22 29 20 74 68  file header") th
1add3 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65  at describes the
1add4 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 66 6f   file..** The fo
1add5 72 6d 61 74 20 6f 66 20 74 68 65 20 66 69 6c 65  rmat of the file
1add6 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66 6f   header is as fo
1add7 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f  llows:.**.**   O
1add8 46 46 53 45 54 20 20 20 53 49 5a 45 20 20 20 20  FFSET   SIZE    
1add9 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20  DESCRIPTION.**  
1adda 20 20 20 20 30 20 20 20 20 20 20 31 36 20 20 20      0      16   
1addb 20 20 48 65 61 64 65 72 20 73 74 72 69 6e 67 3a    Header string:
1addc 20 22 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20   "SQLite format 
1addd 33 5c 30 30 30 22 0a 2a 2a 20 20 20 20 20 31 36  3\000".**     16
1adde 20 20 20 20 20 20 20 32 20 20 20 20 20 50 61 67         2     Pag
1addf 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2e  e size in bytes.
1ade0 20 20 0a 2a 2a 20 20 20 20 20 31 38 20 20 20 20    .**     18    
1ade1 20 20 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f     1     File fo
1ade2 72 6d 61 74 20 77 72 69 74 65 20 76 65 72 73 69  rmat write versi
1ade3 6f 6e 0a 2a 2a 20 20 20 20 20 31 39 20 20 20 20  on.**     19    
1ade4 20 20 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f     1     File fo
1ade5 72 6d 61 74 20 72 65 61 64 20 76 65 72 73 69 6f  rmat read versio
1ade6 6e 0a 2a 2a 20 20 20 20 20 32 30 20 20 20 20 20  n.**     20     
1ade7 20 20 31 20 20 20 20 20 42 79 74 65 73 20 6f 66    1     Bytes of
1ade8 20 75 6e 75 73 65 64 20 73 70 61 63 65 20 61 74   unused space at
1ade9 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68   the end of each
1adea 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 32 31 20   page.**     21 
1adeb 20 20 20 20 20 20 31 20 20 20 20 20 4d 61 78 20        1     Max 
1adec 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64  embedded payload
1aded 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20   fraction.**    
1adee 20 32 32 20 20 20 20 20 20 20 31 20 20 20 20 20   22       1     
1adef 4d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79  Min embedded pay
1adf0 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a  load fraction.**
1adf1 20 20 20 20 20 32 33 20 20 20 20 20 20 20 31 20       23       1 
1adf2 20 20 20 20 4d 69 6e 20 6c 65 61 66 20 70 61 79      Min leaf pay
1adf3 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a  load fraction.**
1adf4 20 20 20 20 20 32 34 20 20 20 20 20 20 20 34 20       24       4 
1adf5 20 20 20 20 46 69 6c 65 20 63 68 61 6e 67 65 20      File change 
1adf6 63 6f 75 6e 74 65 72 0a 2a 2a 20 20 20 20 20 32  counter.**     2
1adf7 38 20 20 20 20 20 20 20 34 20 20 20 20 20 52 65  8       4     Re
1adf8 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72  served for futur
1adf9 65 20 75 73 65 0a 2a 2a 20 20 20 20 20 33 32 20  e use.**     32 
1adfa 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73        4     Firs
1adfb 74 20 66 72 65 65 6c 69 73 74 20 70 61 67 65 0a  t freelist page.
1adfc 2a 2a 20 20 20 20 20 33 36 20 20 20 20 20 20 20  **     36       
1adfd 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20  4     Number of 
1adfe 66 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 69  freelist pages i
1adff 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 20 20  n the file.**   
1ae00 20 20 34 30 20 20 20 20 20 20 36 30 20 20 20 20    40      60    
1ae01 20 31 35 20 34 2d 62 79 74 65 20 6d 65 74 61 20   15 4-byte meta 
1ae02 76 61 6c 75 65 73 20 70 61 73 73 65 64 20 74 6f  values passed to
1ae03 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 0a 2a   higher layers.*
1ae04 2a 0a 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20  *.**     40     
1ae05 20 20 34 20 20 20 20 20 53 63 68 65 6d 61 20 63    4     Schema c
1ae06 6f 6f 6b 69 65 0a 2a 2a 20 20 20 20 20 34 34 20  ookie.**     44 
1ae07 20 20 20 20 20 20 34 20 20 20 20 20 46 69 6c 65        4     File
1ae08 20 66 6f 72 6d 61 74 20 6f 66 20 73 63 68 65 6d   format of schem
1ae09 61 20 6c 61 79 65 72 0a 2a 2a 20 20 20 20 20 34  a layer.**     4
1ae0a 38 20 20 20 20 20 20 20 34 20 20 20 20 20 53 69  8       4     Si
1ae0b 7a 65 20 6f 66 20 70 61 67 65 20 63 61 63 68 65  ze of page cache
1ae0c 0a 2a 2a 20 20 20 20 20 35 32 20 20 20 20 20 20  .**     52      
1ae0d 20 34 20 20 20 20 20 4c 61 72 67 65 73 74 20 72   4     Largest r
1ae0e 6f 6f 74 2d 70 61 67 65 20 28 61 75 74 6f 2f 69  oot-page (auto/i
1ae0f 6e 63 72 5f 76 61 63 75 75 6d 29 0a 2a 2a 20 20  ncr_vacuum).**  
1ae10 20 20 20 35 36 20 20 20 20 20 20 20 34 20 20 20     56       4   
1ae11 20 20 31 3d 55 54 46 2d 38 20 32 3d 55 54 46 31    1=UTF-8 2=UTF1
1ae12 36 6c 65 20 33 3d 55 54 46 31 36 62 65 0a 2a 2a  6le 3=UTF16be.**
1ae13 20 20 20 20 20 36 30 20 20 20 20 20 20 20 34 20       60       4 
1ae14 20 20 20 20 55 73 65 72 20 76 65 72 73 69 6f 6e      User version
1ae15 0a 2a 2a 20 20 20 20 20 36 34 20 20 20 20 20 20  .**     64      
1ae16 20 34 20 20 20 20 20 49 6e 63 72 65 6d 65 6e 74   4     Increment
1ae17 61 6c 20 76 61 63 75 75 6d 20 6d 6f 64 65 0a 2a  al vacuum mode.*
1ae18 2a 20 20 20 20 20 36 38 20 20 20 20 20 20 20 34  *     68       4
1ae19 20 20 20 20 20 75 6e 75 73 65 64 0a 2a 2a 20 20       unused.**  
1ae1a 20 20 20 37 32 20 20 20 20 20 20 20 34 20 20 20     72       4   
1ae1b 20 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20    unused.**     
1ae1c 37 36 20 20 20 20 20 20 20 34 20 20 20 20 20 75  76       4     u
1ae1d 6e 75 73 65 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20  nused.**.** All 
1ae1e 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 76  of the integer v
1ae1f 61 6c 75 65 73 20 61 72 65 20 62 69 67 2d 65 6e  alues are big-en
1ae20 64 69 61 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69  dian (most signi
1ae21 66 69 63 61 6e 74 20 62 79 74 65 20 66 69 72 73  ficant byte firs
1ae22 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  t)..**.** The fi
1ae23 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65  le change counte
1ae24 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  r is incremented
1ae25 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   when the databa
1ae26 73 65 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a  se is changed.**
1ae27 20 54 68 69 73 20 63 6f 75 6e 74 65 72 20 61 6c   This counter al
1ae28 6c 6f 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65  lows other proce
1ae29 73 73 65 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65  sses to know whe
1ae2a 6e 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 63  n the file has c
1ae2b 68 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68  hanged.** and th
1ae2c 75 73 20 77 68 65 6e 20 74 68 65 79 20 6e 65 65  us when they nee
1ae2d 64 20 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72  d to flush their
1ae2e 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   cache..**.** Th
1ae2f 65 20 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70  e max embedded p
1ae30 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20  ayload fraction 
1ae31 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  is the amount of
1ae32 20 74 68 65 20 74 6f 74 61 6c 20 75 73 61 62 6c   the total usabl
1ae33 65 0a 2a 2a 20 73 70 61 63 65 20 69 6e 20 61 20  e.** space in a 
1ae34 70 61 67 65 20 74 68 61 74 20 63 61 6e 20 62 65  page that can be
1ae35 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 61 20 73   consumed by a s
1ae36 69 6e 67 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73  ingle cell for s
1ae37 74 61 6e 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65  tandard.** B-tre
1ae38 65 20 28 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29  e (non-LEAFDATA)
1ae39 20 74 61 62 6c 65 73 2e 20 20 41 20 76 61 6c 75   tables.  A valu
1ae3a 65 20 6f 66 20 32 35 35 20 6d 65 61 6e 73 20 31  e of 255 means 1
1ae3b 30 30 25 2e 20 20 54 68 65 20 64 65 66 61 75 6c  00%.  The defaul
1ae3c 74 0a 2a 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74  t.** is to limit
1ae3d 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c   the maximum cel
1ae3e 6c 20 73 69 7a 65 20 73 6f 20 74 68 61 74 20 61  l size so that a
1ae3f 74 20 6c 65 61 73 74 20 34 20 63 65 6c 6c 73 20  t least 4 cells 
1ae40 77 69 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f  will fit.** on o
1ae41 6e 65 20 70 61 67 65 2e 20 20 54 68 75 73 20 74  ne page.  Thus t
1ae42 68 65 20 64 65 66 61 75 6c 74 20 6d 61 78 20 65  he default max e
1ae43 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20  mbedded payload 
1ae44 66 72 61 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a  fraction is 64..
1ae45 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 79  **.** If the pay
1ae46 6c 6f 61 64 20 66 6f 72 20 61 20 63 65 6c 6c 20  load for a cell 
1ae47 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  is larger than t
1ae48 68 65 20 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20  he max payload, 
1ae49 74 68 65 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61  then extra.** pa
1ae4a 79 6c 6f 61 64 20 69 73 20 73 70 69 6c 6c 65 64  yload is spilled
1ae4b 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   to overflow pag
1ae4c 65 73 2e 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65  es.  Once an ove
1ae4d 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 61 6c  rflow page is al
1ae4e 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d  located,.** as m
1ae4f 61 6e 79 20 62 79 74 65 73 20 61 73 20 70 6f 73  any bytes as pos
1ae50 73 69 62 6c 65 20 61 72 65 20 6d 6f 76 65 64 20  sible are moved 
1ae51 69 6e 74 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f  into the overflo
1ae52 77 20 70 61 67 65 73 20 77 69 74 68 6f 75 74 20  w pages without 
1ae53 6c 65 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63  letting.** the c
1ae54 65 6c 6c 20 73 69 7a 65 20 64 72 6f 70 20 62 65  ell size drop be
1ae55 6c 6f 77 20 74 68 65 20 6d 69 6e 20 65 6d 62 65  low the min embe
1ae56 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61  dded payload fra
1ae57 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ction..**.** The
1ae58 20 6d 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61   min leaf payloa
1ae59 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 6c 69  d fraction is li
1ae5a 6b 65 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64  ke the min embed
1ae5b 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63  ded payload frac
1ae5c 74 69 6f 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74  tion.** except t
1ae5d 68 61 74 20 69 74 20 61 70 70 6c 69 65 73 20 74  hat it applies t
1ae5e 6f 20 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 20  o leaf nodes in 
1ae5f 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 2e  a LEAFDATA tree.
1ae60 20 20 54 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a    The maximum.**
1ae61 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f   payload fractio
1ae62 6e 20 66 6f 72 20 61 20 4c 45 41 46 44 41 54 41  n for a LEAFDATA
1ae63 20 74 72 65 65 20 69 73 20 61 6c 77 61 79 73 20   tree is always 
1ae64 31 30 30 25 20 28 6f 72 20 32 35 35 29 20 61 6e  100% (or 255) an
1ae65 64 20 69 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63  d it.** not spec
1ae66 69 66 69 65 64 20 69 6e 20 74 68 65 20 68 65 61  ified in the hea
1ae67 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  der..**.** Each 
1ae68 62 74 72 65 65 20 70 61 67 65 73 20 69 73 20 64  btree pages is d
1ae69 69 76 69 64 65 64 20 69 6e 74 6f 20 74 68 72 65  ivided into thre
1ae6a 65 20 73 65 63 74 69 6f 6e 73 3a 20 20 54 68 65  e sections:  The
1ae6b 20 68 65 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20   header, the.** 
1ae6c 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72  cell pointer arr
1ae6d 61 79 2c 20 61 6e 64 20 74 68 65 20 63 65 6c 6c  ay, and the cell
1ae6e 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20   content area.  
1ae6f 50 61 67 65 20 31 20 61 6c 73 6f 20 68 61 73 20  Page 1 also has 
1ae70 61 20 31 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69  a 100-byte.** fi
1ae71 6c 65 20 68 65 61 64 65 72 20 74 68 61 74 20 6f  le header that o
1ae72 63 63 75 72 73 20 62 65 66 6f 72 65 20 74 68 65  ccurs before the
1ae73 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a   page header..**
1ae74 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d  .**      |------
1ae75 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20  ----------|.**  
1ae76 20 20 20 20 7c 20 66 69 6c 65 20 68 65 61 64 65      | file heade
1ae77 72 20 20 20 20 7c 20 20 20 31 30 30 20 62 79 74  r    |   100 byt
1ae78 65 73 2e 20 20 50 61 67 65 20 31 20 6f 6e 6c 79  es.  Page 1 only
1ae79 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d  ..**      |-----
1ae7a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20  -----------|.** 
1ae7b 20 20 20 20 20 7c 20 70 61 67 65 20 68 65 61 64       | page head
1ae7c 65 72 20 20 20 20 7c 20 20 20 38 20 62 79 74 65  er    |   8 byte
1ae7d 73 20 66 6f 72 20 6c 65 61 76 65 73 2e 20 20 31  s for leaves.  1
1ae7e 32 20 62 79 74 65 73 20 66 6f 72 20 69 6e 74 65  2 bytes for inte
1ae7f 72 69 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20  rior nodes.**   
1ae80 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     |------------
1ae81 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20  ----|.**      | 
1ae82 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c  cell pointer   |
1ae83 20 20 20 7c 20 20 32 20 62 79 74 65 73 20 70 65     |  2 bytes pe
1ae84 72 20 63 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20  r cell.  Sorted 
1ae85 6f 72 64 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c  order..**      |
1ae86 20 61 72 72 61 79 20 20 20 20 20 20 20 20 20 20   array          
1ae87 7c 20 20 20 7c 20 20 47 72 6f 77 73 20 64 6f 77  |   |  Grows dow
1ae88 6e 77 61 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20  nward.**      | 
1ae89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
1ae8a 20 20 20 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d     v.**      |--
1ae8b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a  --------------|.
1ae8c 2a 2a 20 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f  **      | unallo
1ae8d 63 61 74 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20  cated    |.**   
1ae8e 20 20 20 7c 20 73 70 61 63 65 20 20 20 20 20 20     | space      
1ae8f 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d      |.**      |-
1ae90 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c  ---------------|
1ae91 20 20 20 5e 20 20 47 72 6f 77 73 20 75 70 77 61     ^  Grows upwa
1ae92 72 64 73 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65  rds.**      | ce
1ae93 6c 6c 20 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20  ll content   |  
1ae94 20 7c 20 20 41 72 62 69 74 72 61 72 79 20 6f 72   |  Arbitrary or
1ae95 64 65 72 20 69 6e 74 65 72 73 70 65 72 73 65 64  der interspersed
1ae96 20 77 69 74 68 20 66 72 65 65 62 6c 6f 63 6b 73   with freeblocks
1ae97 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 65 61  ..**      | area
1ae98 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c             |   |
1ae99 20 20 61 6e 64 20 66 72 65 65 20 73 70 61 63 65    and free space
1ae9a 20 66 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20   fragments..**  
1ae9b 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d      |-----------
1ae9c 2d 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65  -----|.**.** The
1ae9d 20 70 61 67 65 20 68 65 61 64 65 72 73 20 6c 6f   page headers lo
1ae9e 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  oks like this:.*
1ae9f 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20  *.**   OFFSET   
1aea0 53 49 5a 45 20 20 20 20 20 44 45 53 43 52 49 50  SIZE     DESCRIP
1aea1 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20  TION.**      0  
1aea2 20 20 20 20 20 31 20 20 20 20 20 20 46 6c 61 67       1      Flag
1aea3 73 2e 20 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a  s. 1: intkey, 2:
1aea4 20 7a 65 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65   zerodata, 4: le
1aea5 61 66 64 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a  afdata, 8: leaf.
1aea6 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20  **      1       
1aea7 32 20 20 20 20 20 20 62 79 74 65 20 6f 66 66 73  2      byte offs
1aea8 65 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  et to the first 
1aea9 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20  freeblock.**    
1aeaa 20 20 33 20 20 20 20 20 20 20 32 20 20 20 20 20    3       2     
1aeab 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   number of cells
1aeac 20 6f 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a   on this page.**
1aead 20 20 20 20 20 20 35 20 20 20 20 20 20 20 32 20        5       2 
1aeae 20 20 20 20 20 66 69 72 73 74 20 62 79 74 65 20       first byte 
1aeaf 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  of the cell cont
1aeb0 65 6e 74 20 61 72 65 61 0a 2a 2a 20 20 20 20 20  ent area.**     
1aeb1 20 37 20 20 20 20 20 20 20 31 20 20 20 20 20 20   7       1      
1aeb2 6e 75 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65  number of fragme
1aeb3 6e 74 65 64 20 66 72 65 65 20 62 79 74 65 73 0a  nted free bytes.
1aeb4 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20 20 20  **      8       
1aeb5 34 20 20 20 20 20 20 52 69 67 68 74 20 63 68 69  4      Right chi
1aeb6 6c 64 20 28 74 68 65 20 50 74 72 28 4e 29 20 76  ld (the Ptr(N) v
1aeb7 61 6c 75 65 29 2e 20 20 4f 6d 69 74 74 65 64 20  alue).  Omitted 
1aeb8 6f 6e 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a  on leaves..**.**
1aeb9 20 54 68 65 20 66 6c 61 67 73 20 64 65 66 69 6e   The flags defin
1aeba 65 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20  e the format of 
1aebb 74 68 69 73 20 62 74 72 65 65 20 70 61 67 65 2e  this btree page.
1aebc 20 20 54 68 65 20 6c 65 61 66 20 66 6c 61 67 20    The leaf flag 
1aebd 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68  means that.** th
1aebe 69 73 20 70 61 67 65 20 68 61 73 20 6e 6f 20 63  is page has no c
1aebf 68 69 6c 64 72 65 6e 2e 20 20 54 68 65 20 7a 65  hildren.  The ze
1aec0 72 6f 64 61 74 61 20 66 6c 61 67 20 6d 65 61 6e  rodata flag mean
1aec1 73 20 74 68 61 74 20 74 68 69 73 20 70 61 67 65  s that this page
1aec2 20 63 61 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79   carries.** only
1aec3 20 6b 65 79 73 20 61 6e 64 20 6e 6f 20 64 61 74   keys and no dat
1aec4 61 2e 20 20 54 68 65 20 69 6e 74 6b 65 79 20 66  a.  The intkey f
1aec5 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74  lag means that t
1aec6 68 65 20 6b 65 79 20 69 73 20 61 20 69 6e 74 65  he key is a inte
1aec7 67 65 72 0a 2a 2a 20 77 68 69 63 68 20 69 73 20  ger.** which is 
1aec8 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6b 65  stored in the ke
1aec9 79 20 73 69 7a 65 20 65 6e 74 72 79 20 6f 66 20  y size entry of 
1aeca 74 68 65 20 63 65 6c 6c 20 68 65 61 64 65 72 20  the cell header 
1aecb 72 61 74 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a  rather than in.*
1aecc 2a 20 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72  * the payload ar
1aecd 65 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65  ea..**.** The ce
1aece 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79  ll pointer array
1aecf 20 62 65 67 69 6e 73 20 6f 6e 20 74 68 65 20 66   begins on the f
1aed0 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20  irst byte after 
1aed1 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2e  the page header.
1aed2 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69  .** The cell poi
1aed3 6e 74 65 72 20 61 72 72 61 79 20 63 6f 6e 74 61  nter array conta
1aed4 69 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65  ins zero or more
1aed5 20 32 2d 62 79 74 65 20 6e 75 6d 62 65 72 73 20   2-byte numbers 
1aed6 77 68 69 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66  which are.** off
1aed7 73 65 74 73 20 66 72 6f 6d 20 74 68 65 20 62 65  sets from the be
1aed8 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70  ginning of the p
1aed9 61 67 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20  age to the cell 
1aeda 63 6f 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 63  content in the c
1aedb 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61  ell.** content a
1aedc 72 65 61 2e 20 20 54 68 65 20 63 65 6c 6c 20 70  rea.  The cell p
1aedd 6f 69 6e 74 65 72 73 20 6f 63 63 75 72 20 69 6e  ointers occur in
1aede 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20   sorted order.  
1aedf 54 68 65 20 73 79 73 74 65 6d 20 73 74 72 69 76  The system striv
1aee0 65 73 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72  es.** to keep fr
1aee1 65 65 20 73 70 61 63 65 20 61 66 74 65 72 20 74  ee space after t
1aee2 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69  he last cell poi
1aee3 6e 74 65 72 20 73 6f 20 74 68 61 74 20 6e 65 77  nter so that new
1aee4 20 63 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65   cells can.** be
1aee5 20 65 61 73 69 6c 79 20 61 64 64 65 64 20 77 69   easily added wi
1aee6 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20  thout having to 
1aee7 64 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70  defragment the p
1aee8 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20  age..**.** Cell 
1aee9 63 6f 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65  content is store
1aeea 64 20 61 74 20 74 68 65 20 76 65 72 79 20 65 6e  d at the very en
1aeeb 64 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e  d of the page an
1aeec 64 20 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74  d grows toward t
1aeed 68 65 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20  he.** beginning 
1aeee 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a  of the page..**.
1aeef 2a 2a 20 55 6e 75 73 65 64 20 73 70 61 63 65 20  ** Unused space 
1aef0 77 69 74 68 69 6e 20 74 68 65 20 63 65 6c 6c 20  within the cell 
1aef1 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20  content area is 
1aef2 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61  collected into a
1aef3 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a   linked list of.
1aef4 2a 2a 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20  ** freeblocks.  
1aef5 45 61 63 68 20 66 72 65 65 62 6c 6f 63 6b 20 69  Each freeblock i
1aef6 73 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 74  s at least 4 byt
1aef7 65 73 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 65  es in size.  The
1aef8 20 62 79 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20   byte offset.** 
1aef9 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 65  to the first fre
1aefa 65 62 6c 6f 63 6b 20 69 73 20 67 69 76 65 6e 20  eblock is given 
1aefb 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 20  in the header.  
1aefc 46 72 65 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72  Freeblocks occur
1aefd 20 69 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e   in.** increasin
1aefe 67 20 6f 72 64 65 72 2e 20 20 42 65 63 61 75 73  g order.  Becaus
1aeff 65 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75  e a freeblock mu
1af00 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 34  st be at least 4
1af01 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a   bytes in size,.
1af02 2a 2a 20 61 6e 79 20 67 72 6f 75 70 20 6f 66 20  ** any group of 
1af03 33 20 6f 72 20 66 65 77 65 72 20 75 6e 75 73 65  3 or fewer unuse
1af04 64 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 63  d bytes in the c
1af05 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61  ell content area
1af06 20 63 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74   cannot.** exist
1af07 20 6f 6e 20 74 68 65 20 66 72 65 65 62 6c 6f 63   on the freebloc
1af08 6b 20 63 68 61 69 6e 2e 20 20 41 20 67 72 6f 75  k chain.  A grou
1af09 70 20 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20  p of 3 or fewer 
1af0a 66 72 65 65 20 62 79 74 65 73 20 69 73 20 63 61  free bytes is ca
1af0b 6c 6c 65 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65  lled.** a fragme
1af0c 6e 74 2e 20 20 54 68 65 20 74 6f 74 61 6c 20 6e  nt.  The total n
1af0d 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
1af0e 6e 20 61 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20  n all fragments 
1af0f 69 73 20 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20  is recorded..** 
1af10 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 64  in the page head
1af11 65 72 20 61 74 20 6f 66 66 73 65 74 20 37 2e 0a  er at offset 7..
1af12 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20  **.**    SIZE   
1af13 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20   DESCRIPTION.** 
1af14 20 20 20 20 20 32 20 20 20 20 20 42 79 74 65 20       2     Byte 
1af15 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 6e 65  offset of the ne
1af16 78 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20  xt freeblock.** 
1af17 20 20 20 20 20 32 20 20 20 20 20 42 79 74 65 73       2     Bytes
1af18 20 69 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f   in this freeblo
1af19 63 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61  ck.**.** Cells a
1af1a 72 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c  re of variable l
1af1b 65 6e 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72  ength.  Cells ar
1af1c 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
1af1d 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
1af1e 61 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20  a at.** the end 
1af1f 6f 66 20 74 68 65 20 70 61 67 65 2e 20 20 50 6f  of the page.  Po
1af20 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 63 65  inters to the ce
1af21 6c 6c 73 20 61 72 65 20 69 6e 20 74 68 65 20 63  lls are in the c
1af22 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61  ell pointer arra
1af23 79 0a 2a 2a 20 74 68 61 74 20 69 6d 6d 65 64 69  y.** that immedi
1af24 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68  ately follows th
1af25 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 20 20  e page header.  
1af26 43 65 6c 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63  Cells is not nec
1af27 65 73 73 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74  essarily.** cont
1af28 69 67 75 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64  iguous or in ord
1af29 65 72 2c 20 62 75 74 20 63 65 6c 6c 20 70 6f 69  er, but cell poi
1af2a 6e 74 65 72 73 20 61 72 65 20 63 6f 6e 74 69 67  nters are contig
1af2b 75 6f 75 73 20 61 6e 64 20 69 6e 20 6f 72 64 65  uous and in orde
1af2c 72 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f  r..**.** Cell co
1af2d 6e 74 65 6e 74 20 6d 61 6b 65 73 20 75 73 65 20  ntent makes use 
1af2e 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67  of variable leng
1af2f 74 68 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20  th integers.  A 
1af30 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67  variable.** leng
1af31 74 68 20 69 6e 74 65 67 65 72 20 69 73 20 31 20  th integer is 1 
1af32 74 6f 20 39 20 62 79 74 65 73 20 77 68 65 72 65  to 9 bytes where
1af33 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74   the lower 7 bit
1af34 73 20 6f 66 20 65 61 63 68 20 0a 2a 2a 20 62 79  s of each .** by
1af35 74 65 20 61 72 65 20 75 73 65 64 2e 20 20 54 68  te are used.  Th
1af36 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73  e integer consis
1af37 74 73 20 6f 66 20 61 6c 6c 20 62 79 74 65 73 20  ts of all bytes 
1af38 74 68 61 74 20 68 61 76 65 20 62 69 74 20 38 20  that have bit 8 
1af39 73 65 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66  set and.** the f
1af3a 69 72 73 74 20 62 79 74 65 20 77 69 74 68 20 62  irst byte with b
1af3b 69 74 20 38 20 63 6c 65 61 72 2e 20 20 54 68 65  it 8 clear.  The
1af3c 20 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e   most significan
1af3d 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 69 6e  t byte of the in
1af3e 74 65 67 65 72 0a 2a 2a 20 61 70 70 65 61 72 73  teger.** appears
1af3f 20 66 69 72 73 74 2e 20 20 41 20 76 61 72 69 61   first.  A varia
1af40 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
1af41 65 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f  er may not be mo
1af42 72 65 20 74 68 61 6e 20 39 20 62 79 74 65 73 20  re than 9 bytes 
1af43 6c 6f 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70  long..** As a sp
1af44 65 63 69 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20  ecial case, all 
1af45 38 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 39  8 bytes of the 9
1af46 74 68 20 62 79 74 65 20 61 72 65 20 75 73 65 64  th byte are used
1af47 20 61 73 20 64 61 74 61 2e 20 20 54 68 69 73 0a   as data.  This.
1af48 2a 2a 20 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62  ** allows a 64-b
1af49 69 74 20 69 6e 74 65 67 65 72 20 74 6f 20 62 65  it integer to be
1af4a 20 65 6e 63 6f 64 65 64 20 69 6e 20 39 20 62 79   encoded in 9 by
1af4b 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78  tes..**.**    0x
1af4c 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  00              
1af4d 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20          becomes 
1af4e 20 30 78 30 30 30 30 30 30 30 30 0a 2a 2a 20 20   0x00000000.**  
1af4f 20 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20    0x7f          
1af50 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f              beco
1af51 6d 65 73 20 20 30 78 30 30 30 30 30 30 37 66 0a  mes  0x0000007f.
1af52 2a 2a 20 20 20 20 30 78 38 31 20 30 78 30 30 20  **    0x81 0x00 
1af53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1af54 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30  becomes  0x00000
1af55 30 38 30 0a 2a 2a 20 20 20 20 30 78 38 32 20 30  080.**    0x82 0
1af56 78 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20  x00             
1af57 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30      becomes  0x0
1af58 30 30 30 30 31 30 30 0a 2a 2a 20 20 20 20 30 78  0000100.**    0x
1af59 38 30 20 30 78 37 66 20 20 20 20 20 20 20 20 20  80 0x7f         
1af5a 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20          becomes 
1af5b 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20   0x0000007f.**  
1af5c 20 20 30 78 38 61 20 30 78 39 31 20 30 78 64 31    0x8a 0x91 0xd1
1af5d 20 30 78 61 63 20 30 78 37 38 20 20 62 65 63 6f   0xac 0x78  beco
1af5e 6d 65 73 20 20 30 78 31 32 33 34 35 36 37 38 0a  mes  0x12345678.
1af5f 2a 2a 20 20 20 20 30 78 38 31 20 30 78 38 31 20  **    0x81 0x81 
1af60 30 78 38 31 20 30 78 38 31 20 30 78 30 31 20 20  0x81 0x81 0x01  
1af61 62 65 63 6f 6d 65 73 20 20 30 78 31 30 32 30 34  becomes  0x10204
1af62 30 38 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62  081.**.** Variab
1af63 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le length intege
1af64 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20  rs are used for 
1af65 72 6f 77 69 64 73 20 61 6e 64 20 74 6f 20 68 6f  rowids and to ho
1af66 6c 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ld the number of
1af67 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 6b 65 79  .** bytes of key
1af68 20 61 6e 64 20 64 61 74 61 20 69 6e 20 61 20 62   and data in a b
1af69 74 72 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a  tree cell..**.**
1af6a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   The content of 
1af6b 61 20 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b  a cell looks lik
1af6c 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
1af6d 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 50   SIZE    DESCRIP
1af6e 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20  TION.**      4  
1af6f 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f     Page number o
1af70 66 20 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64  f the left child
1af71 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 6c 65 61  . Omitted if lea
1af72 66 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a  f flag is set..*
1af73 2a 20 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d  *     var    Num
1af74 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
1af75 64 61 74 61 2e 20 4f 6d 69 74 74 65 64 20 69 66  data. Omitted if
1af76 20 74 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c   the zerodata fl
1af77 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20  ag is set..**   
1af78 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20    var    Number 
1af79 6f 66 20 62 79 74 65 73 20 6f 66 20 6b 65 79 2e  of bytes of key.
1af7a 20 4f 72 20 74 68 65 20 6b 65 79 20 69 74 73 65   Or the key itse
1af7b 6c 66 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61  lf if intkey fla
1af7c 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20  g is set..**    
1af7d 20 20 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0a    *     Payload.
1af7e 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 46 69  **      4     Fi
1af7f 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  rst page of the 
1af80 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20  overflow chain. 
1af81 20 4f 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f   Omitted if no o
1af82 76 65 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76  verflow.**.** Ov
1af83 65 72 66 6c 6f 77 20 70 61 67 65 73 20 66 6f 72  erflow pages for
1af84 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e  m a linked list.
1af85 20 20 45 61 63 68 20 70 61 67 65 20 65 78 63 65    Each page exce
1af86 70 74 20 74 68 65 20 6c 61 73 74 20 69 73 20 63  pt the last is c
1af87 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c  ompletely.** fil
1af88 6c 65 64 20 77 69 74 68 20 64 61 74 61 20 28 70  led with data (p
1af89 61 67 65 73 69 7a 65 20 2d 20 34 20 62 79 74 65  agesize - 4 byte
1af8a 73 29 2e 20 20 54 68 65 20 6c 61 73 74 20 70 61  s).  The last pa
1af8b 67 65 20 63 61 6e 20 68 61 76 65 20 61 73 20 6c  ge can have as l
1af8c 69 74 74 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79  ittle.** as 1 by
1af8d 74 65 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a  te of data..**.*
1af8e 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53  *    SIZE    DES
1af8f 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  CRIPTION.**     
1af90 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62   4     Page numb
1af91 65 72 20 6f 66 20 6e 65 78 74 20 6f 76 65 72 66  er of next overf
1af92 6c 6f 77 20 70 61 67 65 0a 2a 2a 20 20 20 20 20  low page.**     
1af93 20 2a 20 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a   *     Data.**.*
1af94 2a 20 46 72 65 65 6c 69 73 74 20 70 61 67 65 73  * Freelist pages
1af95 20 63 6f 6d 65 20 69 6e 20 74 77 6f 20 73 75 62   come in two sub
1af96 74 79 70 65 73 3a 20 74 72 75 6e 6b 20 70 61 67  types: trunk pag
1af97 65 73 20 61 6e 64 20 6c 65 61 66 20 70 61 67 65  es and leaf page
1af98 73 2e 20 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20  s.  The.** file 
1af99 68 65 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f  header points to
1af9a 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20   the first in a 
1af9b 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74  linked list of t
1af9c 72 75 6e 6b 20 70 61 67 65 2e 20 20 45 61 63 68  runk page.  Each
1af9d 20 74 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70   trunk.** page p
1af9e 6f 69 6e 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c  oints to multipl
1af9f 65 20 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54  e leaf pages.  T
1afa0 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20  he content of a 
1afa1 6c 65 61 66 20 70 61 67 65 20 69 73 0a 2a 2a 20  leaf page is.** 
1afa2 75 6e 73 70 65 63 69 66 69 65 64 2e 20 20 41 20  unspecified.  A 
1afa3 74 72 75 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73  trunk page looks
1afa4 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
1afa5 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53  *    SIZE    DES
1afa6 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  CRIPTION.**     
1afa7 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62   4     Page numb
1afa8 65 72 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b  er of next trunk
1afa9 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 34 20   page.**      4 
1afaa 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65      Number of le
1afab 61 66 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74  af pointers on t
1afac 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20  his page.**     
1afad 20 2a 20 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d   *     zero or m
1afae 6f 72 65 20 70 61 67 65 73 20 6e 75 6d 62 65 72  ore pages number
1afaf 73 20 6f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a  s of leaves.*/..
1afb0 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ./* The followin
1afb1 67 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d  g value is the m
1afb2 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65  aximum cell size
1afb3 20 61 73 73 75 6d 69 6e 67 20 61 20 6d 61 78 69   assuming a maxi
1afb4 6d 75 6d 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65  mum page.** size
1afb5 20 67 69 76 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a   give above..*/.
1afb6 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f  #define MX_CELL_
1afb7 53 49 5a 45 28 70 42 74 29 20 20 28 28 69 6e 74  SIZE(pBt)  ((int
1afb8 29 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d  )(pBt->pageSize-
1afb9 38 29 29 0a 0a 2f 2a 20 54 68 65 20 6d 61 78 69  8))../* The maxi
1afba 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65  mum number of ce
1afbb 6c 6c 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20  lls on a single 
1afbc 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61  page of the data
1afbd 62 61 73 65 2e 20 20 54 68 69 73 0a 2a 2a 20 61  base.  This.** a
1afbe 73 73 75 6d 65 73 20 61 20 6d 69 6e 69 6d 75 6d  ssumes a minimum
1afbf 20 63 65 6c 6c 20 73 69 7a 65 20 6f 66 20 36 20   cell size of 6 
1afc0 62 79 74 65 73 20 20 28 34 20 62 79 74 65 73 20  bytes  (4 bytes 
1afc1 66 6f 72 20 74 68 65 20 63 65 6c 6c 20 69 74 73  for the cell its
1afc2 65 6c 66 0a 2a 2a 20 70 6c 75 73 20 32 20 62 79  elf.** plus 2 by
1afc3 74 65 73 20 66 6f 72 20 74 68 65 20 69 6e 64 65  tes for the inde
1afc4 78 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e  x to the cell in
1afc5 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
1afc6 29 2e 20 20 53 75 63 68 0a 2a 2a 20 73 6d 61 6c  ).  Such.** smal
1afc7 6c 20 63 65 6c 6c 73 20 77 69 6c 6c 20 62 65 20  l cells will be 
1afc8 72 61 72 65 2c 20 62 75 74 20 74 68 65 79 20 61  rare, but they a
1afc9 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a  re possible..*/.
1afca 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 28  #define MX_CELL(
1afcb 70 42 74 29 20 28 28 70 42 74 2d 3e 70 61 67 65  pBt) ((pBt->page
1afcc 53 69 7a 65 2d 38 29 2f 36 29 0a 0a 2f 2a 20 46  Size-8)/6)../* F
1afcd 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
1afce 6f 6e 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73  ons */.typedef s
1afcf 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 4d 65  truct MemPage Me
1afd0 6d 50 61 67 65 3b 0a 74 79 70 65 64 65 66 20 73  mPage;.typedef s
1afd1 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 42 74 4c  truct BtLock BtL
1afd2 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ock;../*.** This
1afd3 20 69 73 20 61 20 6d 61 67 69 63 20 73 74 72 69   is a magic stri
1afd4 6e 67 20 74 68 61 74 20 61 70 70 65 61 72 73 20  ng that appears 
1afd5 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
1afd6 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c   of every.** SQL
1afd7 69 74 65 20 64 61 74 61 62 61 73 65 20 69 6e 20  ite database in 
1afd8 6f 72 64 65 72 20 74 6f 20 69 64 65 6e 74 69 66  order to identif
1afd9 79 20 74 68 65 20 66 69 6c 65 20 61 73 20 61 20  y the file as a 
1afda 72 65 61 6c 20 64 61 74 61 62 61 73 65 2e 0a 2a  real database..*
1afdb 2a 0a 2a 2a 20 59 6f 75 20 63 61 6e 20 63 68 61  *.** You can cha
1afdc 6e 67 65 20 74 68 69 73 20 76 61 6c 75 65 20 61  nge this value a
1afdd 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 62  t compile-time b
1afde 79 20 73 70 65 63 69 66 79 69 6e 67 20 61 0a 2a  y specifying a.*
1afdf 2a 20 2d 44 53 51 4c 49 54 45 5f 46 49 4c 45 5f  * -DSQLITE_FILE_
1afe0 48 45 41 44 45 52 3d 22 2e 2e 2e 22 20 6f 6e 20  HEADER="..." on 
1afe1 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d  the compiler com
1afe2 6d 61 6e 64 2d 6c 69 6e 65 2e 20 20 54 68 65 0a  mand-line.  The.
1afe3 2a 2a 20 68 65 61 64 65 72 20 6d 75 73 74 20 62  ** header must b
1afe4 65 20 65 78 61 63 74 6c 79 20 31 36 20 62 79 74  e exactly 16 byt
1afe5 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65  es including the
1afe6 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72   zero-terminator
1afe7 20 73 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e   so.** the strin
1afe8 67 20 69 74 73 65 6c 66 20 73 68 6f 75 6c 64 20  g itself should 
1afe9 62 65 20 31 35 20 63 68 61 72 61 63 74 65 72 73  be 15 characters
1afea 20 6c 6f 6e 67 2e 20 20 49 66 20 79 6f 75 20 63   long.  If you c
1afeb 68 61 6e 67 65 0a 2a 2a 20 74 68 65 20 68 65 61  hange.** the hea
1afec 64 65 72 2c 20 74 68 65 6e 20 79 6f 75 72 20 63  der, then your c
1afed 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 20 77 69  ustom library wi
1afee 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74  ll not be able t
1afef 6f 20 72 65 61 64 20 0a 2a 2a 20 64 61 74 61 62  o read .** datab
1aff0 61 73 65 73 20 67 65 6e 65 72 61 74 65 64 20 62  ases generated b
1aff1 79 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 74  y the standard t
1aff2 6f 6f 6c 73 20 61 6e 64 20 74 68 65 20 73 74 61  ools and the sta
1aff3 6e 64 61 72 64 20 74 6f 6f 6c 73 0a 2a 2a 20 77  ndard tools.** w
1aff4 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20  ill not be able 
1aff5 74 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 65  to read database
1aff6 73 20 63 72 65 61 74 65 64 20 62 79 20 79 6f 75  s created by you
1aff7 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 72 79  r custom library
1aff8 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
1aff9 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 20  ITE_FILE_HEADER 
1affa 2f 2a 20 31 32 33 34 35 36 37 38 39 20 31 32 33  /* 123456789 123
1affb 34 35 36 20 2a 2f 0a 23 20 20 64 65 66 69 6e 65  456 */.#  define
1affc 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41   SQLITE_FILE_HEA
1affd 44 45 52 20 22 53 51 4c 69 74 65 20 66 6f 72 6d  DER "SQLite form
1affe 61 74 20 33 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a  at 3".#endif../*
1afff 0a 2a 2a 20 50 61 67 65 20 74 79 70 65 20 66 6c  .** Page type fl
1b000 61 67 73 2e 20 20 41 6e 20 4f 52 65 64 20 63 6f  ags.  An ORed co
1b001 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65  mbination of the
1b002 73 65 20 66 6c 61 67 73 20 61 70 70 65 61 72 20  se flags appear 
1b003 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  as the.** first 
1b004 62 79 74 65 20 6f 66 20 6f 6e 2d 64 69 73 6b 20  byte of on-disk 
1b005 69 6d 61 67 65 20 6f 66 20 65 76 65 72 79 20 42  image of every B
1b006 54 72 65 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 64  Tree page..*/.#d
1b007 65 66 69 6e 65 20 50 54 46 5f 49 4e 54 4b 45 59  efine PTF_INTKEY
1b008 20 20 20 20 30 78 30 31 0a 23 64 65 66 69 6e 65      0x01.#define
1b009 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 20 30   PTF_ZERODATA  0
1b00a 78 30 32 0a 23 64 65 66 69 6e 65 20 50 54 46 5f  x02.#define PTF_
1b00b 4c 45 41 46 44 41 54 41 20 20 30 78 30 34 0a 23  LEAFDATA  0x04.#
1b00c 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 20  define PTF_LEAF 
1b00d 20 20 20 20 20 30 78 30 38 0a 0a 2f 2a 0a 2a 2a       0x08../*.**
1b00e 20 41 73 20 65 61 63 68 20 70 61 67 65 20 6f 66   As each page of
1b00f 20 74 68 65 20 66 69 6c 65 20 69 73 20 6c 6f 61   the file is loa
1b010 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2c  ded into memory,
1b011 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
1b012 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  the following.**
1b013 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 70   structure is ap
1b014 70 65 6e 64 65 64 20 61 6e 64 20 69 6e 69 74 69  pended and initi
1b015 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2e 20  alized to zero. 
1b016 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
1b017 73 74 6f 72 65 73 0a 2a 2a 20 69 6e 66 6f 72 6d  stores.** inform
1b018 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
1b019 70 61 67 65 20 74 68 61 74 20 69 73 20 64 65 63  page that is dec
1b01a 6f 64 65 64 20 66 72 6f 6d 20 74 68 65 20 72 61  oded from the ra
1b01b 77 20 66 69 6c 65 20 70 61 67 65 2e 0a 2a 2a 0a  w file page..**.
1b01c 2a 2a 20 54 68 65 20 70 50 61 72 65 6e 74 20 66  ** The pParent f
1b01d 69 65 6c 64 20 70 6f 69 6e 74 73 20 62 61 63 6b  ield points back
1b01e 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70   to the parent p
1b01f 61 67 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77  age.  This allow
1b020 73 20 75 73 20 74 6f 0a 2a 2a 20 77 61 6c 6b 20  s us to.** walk 
1b021 75 70 20 74 68 65 20 42 54 72 65 65 20 66 72 6f  up the BTree fro
1b022 6d 20 61 6e 79 20 6c 65 61 66 20 74 6f 20 74 68  m any leaf to th
1b023 65 20 72 6f 6f 74 2e 20 20 43 61 72 65 20 6d 75  e root.  Care mu
1b024 73 74 20 62 65 20 74 61 6b 65 6e 20 74 6f 0a 2a  st be taken to.*
1b025 2a 20 75 6e 72 65 66 28 29 20 74 68 65 20 70 61  * unref() the pa
1b026 72 65 6e 74 20 70 61 67 65 20 70 6f 69 6e 74 65  rent page pointe
1b027 72 20 77 68 65 6e 20 74 68 69 73 20 70 61 67 65  r when this page
1b028 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65   is no longer re
1b029 66 65 72 65 6e 63 65 64 2e 0a 2a 2a 20 54 68 65  ferenced..** The
1b02a 20 70 61 67 65 44 65 73 74 72 75 63 74 6f 72 28   pageDestructor(
1b02b 29 20 72 6f 75 74 69 6e 65 20 68 61 6e 64 6c 65  ) routine handle
1b02c 73 20 74 68 61 74 20 63 68 6f 72 65 2e 0a 2a 2a  s that chore..**
1b02d 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f 20 61 6c  .** Access to al
1b02e 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73  l fields of this
1b02f 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 6f   structure is co
1b030 6e 74 72 6f 6c 6c 65 64 20 62 79 20 74 68 65 20  ntrolled by the 
1b031 6d 75 74 65 78 0a 2a 2a 20 73 74 6f 72 65 64 20  mutex.** stored 
1b032 69 6e 20 4d 65 6d 50 61 67 65 2e 70 42 74 2d 3e  in MemPage.pBt->
1b033 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74  mutex..*/.struct
1b034 20 4d 65 6d 50 61 67 65 20 7b 0a 20 20 75 38 20   MemPage {.  u8 
1b035 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20  isInit;         
1b036 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 65    /* True if pre
1b037 76 69 6f 75 73 6c 79 20 69 6e 69 74 69 61 6c 69  viously initiali
1b038 7a 65 64 2e 20 4d 55 53 54 20 42 45 20 46 49 52  zed. MUST BE FIR
1b039 53 54 21 20 2a 2f 0a 20 20 75 38 20 6e 4f 76 65  ST! */.  u8 nOve
1b03a 72 66 6c 6f 77 3b 20 20 20 20 20 20 20 20 2f 2a  rflow;        /*
1b03b 20 4e 75 6d 62 65 72 20 6f 66 20 6f 76 65 72 66   Number of overf
1b03c 6c 6f 77 20 63 65 6c 6c 20 62 6f 64 69 65 73 20  low cell bodies 
1b03d 69 6e 20 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20  in aCell[] */.  
1b03e 75 38 20 69 6e 74 4b 65 79 3b 20 20 20 20 20 20  u8 intKey;      
1b03f 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1b040 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 73 20 73  intkey flag is s
1b041 65 74 20 2a 2f 0a 20 20 75 38 20 6c 65 61 66 3b  et */.  u8 leaf;
1b042 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b043 54 72 75 65 20 69 66 20 6c 65 61 66 20 66 6c 61  True if leaf fla
1b044 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 38  g is set */.  u8
1b045 20 68 61 73 44 61 74 61 3b 20 20 20 20 20 20 20   hasData;       
1b046 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
1b047 69 73 20 70 61 67 65 20 73 74 6f 72 65 73 20 64  is page stores d
1b048 61 74 61 20 2a 2f 0a 20 20 75 38 20 68 64 72 4f  ata */.  u8 hdrO
1b049 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a  ffset;        /*
1b04a 20 31 30 30 20 66 6f 72 20 70 61 67 65 20 31 2e   100 for page 1.
1b04b 20 20 30 20 6f 74 68 65 72 77 69 73 65 20 2a 2f    0 otherwise */
1b04c 0a 20 20 75 38 20 63 68 69 6c 64 50 74 72 53 69  .  u8 childPtrSi
1b04d 7a 65 3b 20 20 20 20 20 2f 2a 20 30 20 69 66 20  ze;     /* 0 if 
1b04e 6c 65 61 66 3d 3d 31 2e 20 20 34 20 69 66 20 6c  leaf==1.  4 if l
1b04f 65 61 66 3d 3d 30 20 2a 2f 0a 20 20 75 38 20 6d  eaf==0 */.  u8 m
1b050 61 78 31 62 79 74 65 50 61 79 6c 6f 61 64 3b 20  ax1bytePayload; 
1b051 20 2f 2a 20 6d 69 6e 28 6d 61 78 4c 6f 63 61 6c   /* min(maxLocal
1b052 2c 31 32 37 29 20 2a 2f 0a 20 20 75 31 36 20 6d  ,127) */.  u16 m
1b053 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20  axLocal;        
1b054 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68 61  /* Copy of BtSha
1b055 72 65 64 2e 6d 61 78 4c 6f 63 61 6c 20 6f 72 20  red.maxLocal or 
1b056 42 74 53 68 61 72 65 64 2e 6d 61 78 4c 65 61 66  BtShared.maxLeaf
1b057 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63   */.  u16 minLoc
1b058 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  al;        /* Co
1b059 70 79 20 6f 66 20 42 74 53 68 61 72 65 64 2e 6d  py of BtShared.m
1b05a 69 6e 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68 61  inLocal or BtSha
1b05b 72 65 64 2e 6d 69 6e 4c 65 61 66 20 2a 2f 0a 20  red.minLeaf */. 
1b05c 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 3b   u16 cellOffset;
1b05d 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
1b05e 6e 20 61 44 61 74 61 20 6f 66 20 66 69 72 73 74  n aData of first
1b05f 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f   cell pointer */
1b060 0a 20 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20  .  u16 nFree;   
1b061 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1b062 72 20 6f 66 20 66 72 65 65 20 62 79 74 65 73 20  r of free bytes 
1b063 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20  on the page */. 
1b064 20 75 31 36 20 6e 43 65 6c 6c 3b 20 20 20 20 20   u16 nCell;     
1b065 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1b066 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69 73  of cells on this
1b067 20 70 61 67 65 2c 20 6c 6f 63 61 6c 20 61 6e 64   page, local and
1b068 20 6f 76 66 6c 20 2a 2f 0a 20 20 75 31 36 20 6d   ovfl */.  u16 m
1b069 61 73 6b 50 61 67 65 3b 20 20 20 20 20 20 20 20  askPage;        
1b06a 2f 2a 20 4d 61 73 6b 20 66 6f 72 20 70 61 67 65  /* Mask for page
1b06b 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 75 31 36   offset */.  u16
1b06c 20 61 69 4f 76 66 6c 5b 35 5d 3b 20 20 20 20 20   aiOvfl[5];     
1b06d 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 65 20    /* Insert the 
1b06e 69 2d 74 68 20 6f 76 65 72 66 6c 6f 77 20 63 65  i-th overflow ce
1b06f 6c 6c 20 62 65 66 6f 72 65 20 74 68 65 20 61 69  ll before the ai
1b070 4f 76 66 6c 2d 74 68 0a 20 20 20 20 20 20 20 20  Ovfl-th.        
1b071 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
1b072 2a 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f 77 20 63  * non-overflow c
1b073 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 61 70 4f  ell */.  u8 *apO
1b074 76 66 6c 5b 35 5d 3b 20 20 20 20 20 20 20 2f 2a  vfl[5];       /*
1b075 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   Pointers to the
1b076 20 62 6f 64 79 20 6f 66 20 6f 76 65 72 66 6c 6f   body of overflo
1b077 77 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 42 74 53  w cells */.  BtS
1b078 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20  hared *pBt;     
1b079 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
1b07a 42 74 53 68 61 72 65 64 20 74 68 61 74 20 74 68  BtShared that th
1b07b 69 73 20 70 61 67 65 20 69 73 20 70 61 72 74 20  is page is part 
1b07c 6f 66 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61 74  of */.  u8 *aDat
1b07d 61 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  a;           /* 
1b07e 50 6f 69 6e 74 65 72 20 74 6f 20 64 69 73 6b 20  Pointer to disk 
1b07f 69 6d 61 67 65 20 6f 66 20 74 68 65 20 70 61 67  image of the pag
1b080 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a  e data */.  u8 *
1b081 61 44 61 74 61 45 6e 64 3b 20 20 20 20 20 20 20  aDataEnd;       
1b082 20 2f 2a 20 4f 6e 65 20 62 79 74 65 20 70 61 73   /* One byte pas
1b083 74 20 74 68 65 20 65 6e 64 20 6f 66 20 75 73 61  t the end of usa
1b084 62 6c 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 38  ble data */.  u8
1b085 20 2a 61 43 65 6c 6c 49 64 78 3b 20 20 20 20 20   *aCellIdx;     
1b086 20 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 69     /* The cell i
1b087 6e 64 65 78 20 61 72 65 61 20 2a 2f 0a 20 20 44  ndex area */.  D
1b088 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20  bPage *pDbPage; 
1b089 20 20 20 20 2f 2a 20 50 61 67 65 72 20 70 61 67      /* Pager pag
1b08a 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67  e handle */.  Pg
1b08b 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20  no pgno;        
1b08c 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
1b08d 72 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 20  r for this page 
1b08e 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
1b08f 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 6d 61 67 65   in-memory image
1b090 20 6f 66 20 61 20 64 69 73 6b 20 70 61 67 65 20   of a disk page 
1b091 68 61 73 20 74 68 65 20 61 75 78 69 6c 69 61 72  has the auxiliar
1b092 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70  y information ap
1b093 70 65 6e 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65  pended.** to the
1b094 20 65 6e 64 2e 20 20 45 58 54 52 41 5f 53 49 5a   end.  EXTRA_SIZ
1b095 45 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  E is the number 
1b096 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63  of bytes of spac
1b097 65 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64  e needed to hold
1b098 0a 2a 2a 20 74 68 61 74 20 65 78 74 72 61 20 69  .** that extra i
1b099 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23  nformation..*/.#
1b09a 64 65 66 69 6e 65 20 45 58 54 52 41 5f 53 49 5a  define EXTRA_SIZ
1b09b 45 20 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65  E sizeof(MemPage
1b09c 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 6e 6b 65  )../*.** A linke
1b09d 64 20 6c 69 73 74 20 6f 66 20 74 68 65 20 66 6f  d list of the fo
1b09e 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
1b09f 65 73 20 69 73 20 73 74 6f 72 65 64 20 61 74 20  es is stored at 
1b0a0 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 2e 0a  BtShared.pLock..
1b0a1 2a 2a 20 4c 6f 63 6b 73 20 61 72 65 20 61 64 64  ** Locks are add
1b0a2 65 64 20 28 6f 72 20 75 70 67 72 61 64 65 64 20  ed (or upgraded 
1b0a3 66 72 6f 6d 20 52 45 41 44 5f 4c 4f 43 4b 20 74  from READ_LOCK t
1b0a4 6f 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 77 68  o WRITE_LOCK) wh
1b0a5 65 6e 20 61 20 63 75 72 73 6f 72 20 0a 2a 2a 20  en a cursor .** 
1b0a6 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65  is opened on the
1b0a7 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74   table with root
1b0a8 20 70 61 67 65 20 42 74 53 68 61 72 65 64 2e 69   page BtShared.i
1b0a9 54 61 62 6c 65 2e 20 4c 6f 63 6b 73 20 61 72 65  Table. Locks are
1b0aa 20 72 65 6d 6f 76 65 64 0a 2a 2a 20 66 72 6f 6d   removed.** from
1b0ab 20 74 68 69 73 20 6c 69 73 74 20 77 68 65 6e 20   this list when 
1b0ac 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
1b0ad 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f   committed or ro
1b0ae 6c 6c 65 64 20 62 61 63 6b 2c 20 6f 72 20 77 68  lled back, or wh
1b0af 65 6e 0a 2a 2a 20 61 20 62 74 72 65 65 20 68 61  en.** a btree ha
1b0b0 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e 0a  ndle is closed..
1b0b1 2a 2f 0a 73 74 72 75 63 74 20 42 74 4c 6f 63 6b  */.struct BtLock
1b0b2 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72   {.  Btree *pBtr
1b0b3 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 74  ee;        /* Bt
1b0b4 72 65 65 20 68 61 6e 64 6c 65 20 68 6f 6c 64 69  ree handle holdi
1b0b5 6e 67 20 74 68 69 73 20 6c 6f 63 6b 20 2a 2f 0a  ng this lock */.
1b0b6 20 20 50 67 6e 6f 20 69 54 61 62 6c 65 3b 20 20    Pgno iTable;  
1b0b7 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20          /* Root 
1b0b8 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 2a 2f  page of table */
1b0b9 0a 20 20 75 38 20 65 4c 6f 63 6b 3b 20 20 20 20  .  u8 eLock;    
1b0ba 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 41 44           /* READ
1b0bb 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c  _LOCK or WRITE_L
1b0bc 4f 43 4b 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20  OCK */.  BtLock 
1b0bd 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 2f  *pNext;        /
1b0be 2a 20 4e 65 78 74 20 69 6e 20 42 74 53 68 61 72  * Next in BtShar
1b0bf 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 20 2a 2f  ed.pLock list */
1b0c0 0a 7d 3b 0a 0a 2f 2a 20 43 61 6e 64 69 64 61 74  .};../* Candidat
1b0c1 65 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 4c  e values for BtL
1b0c2 6f 63 6b 2e 65 4c 6f 63 6b 20 2a 2f 0a 23 64 65  ock.eLock */.#de
1b0c3 66 69 6e 65 20 52 45 41 44 5f 4c 4f 43 4b 20 20  fine READ_LOCK  
1b0c4 20 20 20 31 0a 23 64 65 66 69 6e 65 20 57 52 49     1.#define WRI
1b0c5 54 45 5f 4c 4f 43 4b 20 20 20 20 32 0a 0a 2f 2a  TE_LOCK    2../*
1b0c6 20 41 20 42 74 72 65 65 20 68 61 6e 64 6c 65 0a   A Btree handle.
1b0c7 2a 2a 0a 2a 2a 20 41 20 64 61 74 61 62 61 73 65  **.** A database
1b0c8 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e 74   connection cont
1b0c9 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ains a pointer t
1b0ca 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  o an instance of
1b0cb 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65 63 74 20  .** this object 
1b0cc 66 6f 72 20 65 76 65 72 79 20 64 61 74 61 62 61  for every databa
1b0cd 73 65 20 66 69 6c 65 20 74 68 61 74 20 69 74 20  se file that it 
1b0ce 68 61 73 20 6f 70 65 6e 2e 20 20 54 68 69 73 20  has open.  This 
1b0cf 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20  structure.** is 
1b0d0 6f 70 61 71 75 65 20 74 6f 20 74 68 65 20 64 61  opaque to the da
1b0d1 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1b0d2 6e 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65  n.  The database
1b0d3 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 6e   connection cann
1b0d4 6f 74 0a 2a 2a 20 73 65 65 20 74 68 65 20 69 6e  ot.** see the in
1b0d5 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20  ternals of this 
1b0d6 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 6f 6e  structure and on
1b0d7 6c 79 20 64 65 61 6c 73 20 77 69 74 68 20 70 6f  ly deals with po
1b0d8 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69  inters to.** thi
1b0d9 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  s structure..**.
1b0da 2a 2a 20 46 6f 72 20 73 6f 6d 65 20 64 61 74 61  ** For some data
1b0db 62 61 73 65 20 66 69 6c 65 73 2c 20 74 68 65 20  base files, the 
1b0dc 73 61 6d 65 20 75 6e 64 65 72 6c 79 69 6e 67 20  same underlying 
1b0dd 64 61 74 61 62 61 73 65 20 63 61 63 68 65 20 6d  database cache m
1b0de 69 67 68 74 20 62 65 20 0a 2a 2a 20 73 68 61 72  ight be .** shar
1b0df 65 64 20 62 65 74 77 65 65 6e 20 6d 75 6c 74 69  ed between multi
1b0e0 70 6c 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e  ple connections.
1b0e1 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20    In that case, 
1b0e2 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  each connection.
1b0e3 2a 2a 20 68 61 73 20 69 74 20 6f 77 6e 20 69 6e  ** has it own in
1b0e4 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
1b0e5 62 6a 65 63 74 2e 20 20 42 75 74 20 65 61 63 68  bject.  But each
1b0e6 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
1b0e7 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e  s object.** poin
1b0e8 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 42  ts to the same B
1b0e9 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 20  tShared object. 
1b0ea 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 61   The database ca
1b0eb 63 68 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73  che and the.** s
1b0ec 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64  chema associated
1b0ed 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
1b0ee 73 65 20 66 69 6c 65 20 61 72 65 20 61 6c 6c 20  se file are all 
1b0ef 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e  contained within
1b0f0 0a 2a 2a 20 74 68 65 20 42 74 53 68 61 72 65 64  .** the BtShared
1b0f1 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41   object..**.** A
1b0f2 6c 6c 20 66 69 65 6c 64 73 20 69 6e 20 74 68 69  ll fields in thi
1b0f3 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
1b0f4 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 73  accessed under s
1b0f5 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2a  qlite3.mutex..**
1b0f6 20 54 68 65 20 70 42 74 20 70 6f 69 6e 74 65 72   The pBt pointer
1b0f7 20 69 74 73 65 6c 66 20 6d 61 79 20 6e 6f 74 20   itself may not 
1b0f8 62 65 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65  be changed while
1b0f9 20 74 68 65 72 65 20 65 78 69 73 74 73 20 63 75   there exists cu
1b0fa 72 73 6f 72 73 20 0a 2a 2a 20 69 6e 20 74 68 65  rsors .** in the
1b0fb 20 72 65 66 65 72 65 6e 63 65 64 20 42 74 53 68   referenced BtSh
1b0fc 61 72 65 64 20 74 68 61 74 20 70 6f 69 6e 74 20  ared that point 
1b0fd 62 61 63 6b 20 74 6f 20 74 68 69 73 20 42 74 72  back to this Btr
1b0fe 65 65 20 73 69 6e 63 65 20 74 68 6f 73 65 0a 2a  ee since those.*
1b0ff 2a 20 63 75 72 73 6f 72 73 20 68 61 76 65 20 74  * cursors have t
1b100 6f 20 67 6f 20 74 68 72 6f 75 67 68 20 74 68 69  o go through thi
1b101 73 20 42 74 72 65 65 20 74 6f 20 66 69 6e 64 20  s Btree to find 
1b102 74 68 65 69 72 20 42 74 53 68 61 72 65 64 20 61  their BtShared a
1b103 6e 64 0a 2a 2a 20 74 68 65 79 20 6f 66 74 65 6e  nd.** they often
1b104 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 68   do so without h
1b105 6f 6c 64 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d  olding sqlite3.m
1b106 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  utex..*/.struct 
1b107 42 74 72 65 65 20 7b 0a 20 20 73 71 6c 69 74 65  Btree {.  sqlite
1b108 33 20 2a 64 62 3b 20 20 20 20 20 20 20 2f 2a 20  3 *db;       /* 
1b109 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
1b10a 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20  nection holding 
1b10b 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20  this btree */.  
1b10c 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20  BtShared *pBt;  
1b10d 20 20 20 2f 2a 20 53 68 61 72 61 62 6c 65 20 63     /* Sharable c
1b10e 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 62  ontent of this b
1b10f 74 72 65 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54  tree */.  u8 inT
1b110 72 61 6e 73 3b 20 20 20 20 20 20 20 20 2f 2a 20  rans;        /* 
1b111 54 52 41 4e 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e  TRANS_NONE, TRAN
1b112 53 5f 52 45 41 44 20 6f 72 20 54 52 41 4e 53 5f  S_READ or TRANS_
1b113 57 52 49 54 45 20 2a 2f 0a 20 20 75 38 20 73 68  WRITE */.  u8 sh
1b114 61 72 61 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a  arable;       /*
1b115 20 54 72 75 65 20 69 66 20 77 65 20 63 61 6e 20   True if we can 
1b116 73 68 61 72 65 20 70 42 74 20 77 69 74 68 20 61  share pBt with a
1b117 6e 6f 74 68 65 72 20 64 62 20 2a 2f 0a 20 20 75  nother db */.  u
1b118 38 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20  8 locked;       
1b119 20 20 2f 2a 20 54 72 75 65 20 69 66 20 64 62 20    /* True if db 
1b11a 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 70 42  currently has pB
1b11b 74 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e  t locked */.  in
1b11c 74 20 77 61 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20  t wantToLock;   
1b11d 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65   /* Number of ne
1b11e 73 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 73 71  sted calls to sq
1b11f 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
1b120 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b  ) */.  int nBack
1b121 75 70 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  up;       /* Num
1b122 62 65 72 20 6f 66 20 62 61 63 6b 75 70 20 6f 70  ber of backup op
1b123 65 72 61 74 69 6f 6e 73 20 72 65 61 64 69 6e 67  erations reading
1b124 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20   this btree */. 
1b125 20 42 74 72 65 65 20 2a 70 4e 65 78 74 3b 20 20   Btree *pNext;  
1b126 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f      /* List of o
1b127 74 68 65 72 20 73 68 61 72 61 62 6c 65 20 42 74  ther sharable Bt
1b128 72 65 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61  rees from the sa
1b129 6d 65 20 64 62 20 2a 2f 0a 20 20 42 74 72 65 65  me db */.  Btree
1b12a 20 2a 70 50 72 65 76 3b 20 20 20 20 20 20 2f 2a   *pPrev;      /*
1b12b 20 42 61 63 6b 20 70 6f 69 6e 74 65 72 20 6f 66   Back pointer of
1b12c 20 74 68 65 20 73 61 6d 65 20 6c 69 73 74 20 2a   the same list *
1b12d 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1b12e 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
1b12f 48 45 0a 20 20 42 74 4c 6f 63 6b 20 6c 6f 63 6b  HE.  BtLock lock
1b130 3b 20 20 20 20 20 20 20 2f 2a 20 4f 62 6a 65 63  ;       /* Objec
1b131 74 20 75 73 65 64 20 74 6f 20 6c 6f 63 6b 20 70  t used to lock p
1b132 61 67 65 20 31 20 2a 2f 0a 23 65 6e 64 69 66 0a  age 1 */.#endif.
1b133 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 74 72 65 65 2e  };../*.** Btree.
1b134 69 6e 54 72 61 6e 73 20 6d 61 79 20 74 61 6b 65  inTrans may take
1b135 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
1b136 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a  owing values..**
1b137 0a 2a 2a 20 49 66 20 74 68 65 20 73 68 61 72 65  .** If the share
1b138 64 2d 64 61 74 61 20 65 78 74 65 6e 73 69 6f 6e  d-data extension
1b139 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65   is enabled, the
1b13a 72 65 20 6d 61 79 20 62 65 20 6d 75 6c 74 69 70  re may be multip
1b13b 6c 65 20 75 73 65 72 73 0a 2a 2a 20 6f 66 20 74  le users.** of t
1b13c 68 65 20 42 74 72 65 65 20 73 74 72 75 63 74 75  he Btree structu
1b13d 72 65 2e 20 41 74 20 6d 6f 73 74 20 6f 6e 65 20  re. At most one 
1b13e 6f 66 20 74 68 65 73 65 20 6d 61 79 20 6f 70 65  of these may ope
1b13f 6e 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61  n a write transa
1b140 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20 61 6e  ction,.** but an
1b141 79 20 6e 75 6d 62 65 72 20 6d 61 79 20 68 61 76  y number may hav
1b142 65 20 61 63 74 69 76 65 20 72 65 61 64 20 74 72  e active read tr
1b143 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 23  ansactions..*/.#
1b144 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 4e 4f 4e  define TRANS_NON
1b145 45 20 20 30 0a 23 64 65 66 69 6e 65 20 54 52 41  E  0.#define TRA
1b146 4e 53 5f 52 45 41 44 20 20 31 0a 23 64 65 66 69  NS_READ  1.#defi
1b147 6e 65 20 54 52 41 4e 53 5f 57 52 49 54 45 20 32  ne TRANS_WRITE 2
1b148 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
1b149 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
1b14a 63 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 20  ct represents a 
1b14b 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20  single database 
1b14c 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 41 20 73  file..** .** A s
1b14d 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 66  ingle database f
1b14e 69 6c 65 20 63 61 6e 20 62 65 20 69 6e 20 75 73  ile can be in us
1b14f 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  e at the same ti
1b150 6d 65 20 62 79 20 74 77 6f 0a 2a 2a 20 6f 72 20  me by two.** or 
1b151 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 63 6f  more database co
1b152 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 57 68 65 6e  nnections.  When
1b153 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63 6f 6e   two or more con
1b154 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a 2a 20  nections are.** 
1b155 73 68 61 72 69 6e 67 20 74 68 65 20 73 61 6d 65  sharing the same
1b156 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
1b157 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  each connection 
1b158 68 61 73 20 69 74 20 6f 77 6e 0a 2a 2a 20 70 72  has it own.** pr
1b159 69 76 61 74 65 20 42 74 72 65 65 20 6f 62 6a 65  ivate Btree obje
1b15a 63 74 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20  ct for the file 
1b15b 61 6e 64 20 65 61 63 68 20 6f 66 20 74 68 6f 73  and each of thos
1b15c 65 20 42 74 72 65 65 73 20 70 6f 69 6e 74 73 0a  e Btrees points.
1b15d 2a 2a 20 74 6f 20 74 68 69 73 20 6f 6e 65 20 42  ** to this one B
1b15e 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 20  tShared object. 
1b15f 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 69   BtShared.nRef i
1b160 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  s the number of.
1b161 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63  ** connections c
1b162 75 72 72 65 6e 74 6c 79 20 73 68 61 72 69 6e 67  urrently sharing
1b163 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 66   this database f
1b164 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64  ile..**.** Field
1b165 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74  s in this struct
1b166 75 72 65 20 61 72 65 20 61 63 63 65 73 73 65 64  ure are accessed
1b167 20 75 6e 64 65 72 20 74 68 65 20 42 74 53 68 61   under the BtSha
1b168 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 6d 75 74  red.mutex.** mut
1b169 65 78 2c 20 65 78 63 65 70 74 20 66 6f 72 20 6e  ex, except for n
1b16a 52 65 66 20 61 6e 64 20 70 4e 65 78 74 20 77 68  Ref and pNext wh
1b16b 69 63 68 20 61 72 65 20 61 63 63 65 73 73 65 64  ich are accessed
1b16c 20 75 6e 64 65 72 20 74 68 65 0a 2a 2a 20 67 6c   under the.** gl
1b16d 6f 62 61 6c 20 53 51 4c 49 54 45 5f 4d 55 54 45  obal SQLITE_MUTE
1b16e 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20  X_STATIC_MASTER 
1b16f 6d 75 74 65 78 2e 20 20 54 68 65 20 70 50 61 67  mutex.  The pPag
1b170 65 72 20 66 69 65 6c 64 0a 2a 2a 20 6d 61 79 20  er field.** may 
1b171 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20  not be modified 
1b172 6f 6e 63 65 20 69 74 20 69 73 20 69 6e 69 74 69  once it is initi
1b173 61 6c 6c 79 20 73 65 74 20 61 73 20 6c 6f 6e 67  ally set as long
1b174 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a 20 54   as nRef>0..** T
1b175 68 65 20 70 53 63 68 65 6d 61 20 66 69 65 6c 64  he pSchema field
1b176 20 6d 61 79 20 62 65 20 73 65 74 20 6f 6e 63 65   may be set once
1b177 20 75 6e 64 65 72 20 42 74 53 68 61 72 65 64 2e   under BtShared.
1b178 6d 75 74 65 78 20 61 6e 64 0a 2a 2a 20 74 68 65  mutex and.** the
1b179 72 65 61 66 74 65 72 20 69 73 20 75 6e 63 68 61  reafter is uncha
1b17a 6e 67 65 64 20 61 73 20 6c 6f 6e 67 20 61 73 20  nged as long as 
1b17b 6e 52 65 66 3e 30 2e 0a 2a 2a 0a 2a 2a 20 69 73  nRef>0..**.** is
1b17c 50 65 6e 64 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  Pending:.**.**  
1b17d 20 49 66 20 61 20 42 74 53 68 61 72 65 64 20 63   If a BtShared c
1b17e 6c 69 65 6e 74 20 66 61 69 6c 73 20 74 6f 20 6f  lient fails to o
1b17f 62 74 61 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f  btain a write-lo
1b180 63 6b 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65  ck on a database
1b181 0a 2a 2a 20 20 20 74 61 62 6c 65 20 28 62 65 63  .**   table (bec
1b182 61 75 73 65 20 74 68 65 72 65 20 65 78 69 73 74  ause there exist
1b183 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65  s one or more re
1b184 61 64 2d 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20  ad-locks on the 
1b185 74 61 62 6c 65 29 2c 0a 2a 2a 20 20 20 74 68 65  table),.**   the
1b186 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 65 6e   shared-cache en
1b187 74 65 72 73 20 27 70 65 6e 64 69 6e 67 2d 6c 6f  ters 'pending-lo
1b188 63 6b 27 20 73 74 61 74 65 20 61 6e 64 20 69 73  ck' state and is
1b189 50 65 6e 64 69 6e 67 20 69 73 0a 2a 2a 20 20 20  Pending is.**   
1b18a 73 65 74 20 74 6f 20 74 72 75 65 2e 0a 2a 2a 0a  set to true..**.
1b18b 2a 2a 20 20 20 54 68 65 20 73 68 61 72 65 64 2d  **   The shared-
1b18c 63 61 63 68 65 20 6c 65 61 76 65 73 20 74 68 65  cache leaves the
1b18d 20 27 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 27 20   'pending lock' 
1b18e 73 74 61 74 65 20 77 68 65 6e 20 65 69 74 68 65  state when eithe
1b18f 72 20 6f 66 0a 2a 2a 20 20 20 74 68 65 20 66 6f  r of.**   the fo
1b190 6c 6c 6f 77 69 6e 67 20 6f 63 63 75 72 3a 0a 2a  llowing occur:.*
1b191 2a 0a 2a 2a 20 20 20 20 20 31 29 20 54 68 65 20  *.**     1) The 
1b192 63 75 72 72 65 6e 74 20 77 72 69 74 65 72 20 28  current writer (
1b193 42 74 53 68 61 72 65 64 2e 70 57 72 69 74 65 72  BtShared.pWriter
1b194 29 20 63 6f 6e 63 6c 75 64 65 73 20 69 74 73 20  ) concludes its 
1b195 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 4f 52 0a  transaction, OR.
1b196 2a 2a 20 20 20 20 20 32 29 20 54 68 65 20 6e 75  **     2) The nu
1b197 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68 65  mber of locks he
1b198 6c 64 20 62 79 20 6f 74 68 65 72 20 63 6f 6e 6e  ld by other conn
1b199 65 63 74 69 6f 6e 73 20 64 72 6f 70 73 20 74 6f  ections drops to
1b19a 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 77   zero..**.**   w
1b19b 68 69 6c 65 20 69 6e 20 74 68 65 20 27 70 65 6e  hile in the 'pen
1b19c 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74 65  ding-lock' state
1b19d 2c 20 6e 6f 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  , no connection 
1b19e 6d 61 79 20 73 74 61 72 74 20 61 20 6e 65 77 0a  may start a new.
1b19f 2a 2a 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e  **   transaction
1b1a0 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 66  ..**.**   This f
1b1a1 65 61 74 75 72 65 20 69 73 20 69 6e 63 6c 75 64  eature is includ
1b1a2 65 64 20 74 6f 20 68 65 6c 70 20 70 72 65 76 65  ed to help preve
1b1a3 6e 74 20 77 72 69 74 65 72 2d 73 74 61 72 76 61  nt writer-starva
1b1a4 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  tion..*/.struct 
1b1a5 42 74 53 68 61 72 65 64 20 7b 0a 20 20 50 61 67  BtShared {.  Pag
1b1a6 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20  er *pPager;     
1b1a7 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 63     /* The page c
1b1a8 61 63 68 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ache */.  sqlite
1b1a9 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
1b1aa 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
1b1ab 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79  ection currently
1b1ac 20 75 73 69 6e 67 20 74 68 69 73 20 42 74 72 65   using this Btre
1b1ad 65 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20  e */.  BtCursor 
1b1ae 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20  *pCursor;    /* 
1b1af 41 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 70  A list of all op
1b1b0 65 6e 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20  en cursors */.  
1b1b1 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b  MemPage *pPage1;
1b1b2 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70        /* First p
1b1b3 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  age of the datab
1b1b4 61 73 65 20 2a 2f 0a 20 20 75 38 20 6f 70 65 6e  ase */.  u8 open
1b1b5 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 2f  Flags;         /
1b1b6 2a 20 46 6c 61 67 73 20 74 6f 20 73 71 6c 69 74  * Flags to sqlit
1b1b7 65 33 42 74 72 65 65 4f 70 65 6e 28 29 20 2a 2f  e3BtreeOpen() */
1b1b8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1b1b9 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
1b1ba 20 20 75 38 20 61 75 74 6f 56 61 63 75 75 6d 3b    u8 autoVacuum;
1b1bb 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1b1bc 69 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69  if auto-vacuum i
1b1bd 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 75  s enabled */.  u
1b1be 38 20 69 6e 63 72 56 61 63 75 75 6d 3b 20 20 20  8 incrVacuum;   
1b1bf 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1b1c0 69 6e 63 72 2d 76 61 63 75 75 6d 20 69 73 20 65  incr-vacuum is e
1b1c1 6e 61 62 6c 65 64 20 2a 2f 0a 23 65 6e 64 69 66  nabled */.#endif
1b1c2 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 61 63 74  .  u8 inTransact
1b1c3 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 54 72 61 6e  ion;     /* Tran
1b1c4 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 2a 2f  saction state */
1b1c5 0a 20 20 75 38 20 6d 61 78 31 62 79 74 65 50 61  .  u8 max1bytePa
1b1c6 79 6c 6f 61 64 3b 20 20 20 2f 2a 20 4d 61 78 69  yload;   /* Maxi
1b1c7 6d 75 6d 20 66 69 72 73 74 20 62 79 74 65 20 6f  mum first byte o
1b1c8 66 20 63 65 6c 6c 20 66 6f 72 20 61 20 31 2d 62  f cell for a 1-b
1b1c9 79 74 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20  yte payload */. 
1b1ca 20 75 31 36 20 62 74 73 46 6c 61 67 73 3b 20 20   u16 btsFlags;  
1b1cb 20 20 20 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61         /* Boolea
1b1cc 6e 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 53  n parameters.  S
1b1cd 65 65 20 42 54 53 5f 2a 20 6d 61 63 72 6f 73 20  ee BTS_* macros 
1b1ce 62 65 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 6d  below */.  u16 m
1b1cf 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20  axLocal;        
1b1d0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61   /* Maximum loca
1b1d1 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e  l payload in non
1b1d2 2d 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 73  -LEAFDATA tables
1b1d3 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63   */.  u16 minLoc
1b1d4 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  al;         /* M
1b1d5 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79  inimum local pay
1b1d6 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46  load in non-LEAF
1b1d7 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20  DATA tables */. 
1b1d8 20 75 31 36 20 6d 61 78 4c 65 61 66 3b 20 20 20   u16 maxLeaf;   
1b1d9 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
1b1da 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20  m local payload 
1b1db 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 61  in a LEAFDATA ta
1b1dc 62 6c 65 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e  ble */.  u16 min
1b1dd 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f  Leaf;          /
1b1de 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20  * Minimum local 
1b1df 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41  payload in a LEA
1b1e0 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20  FDATA table */. 
1b1e1 20 75 33 32 20 70 61 67 65 53 69 7a 65 3b 20 20   u32 pageSize;  
1b1e2 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20         /* Total 
1b1e3 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
1b1e4 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 75  on a page */.  u
1b1e5 33 32 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20  32 usableSize;  
1b1e6 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1b1e7 66 20 75 73 61 62 6c 65 20 62 79 74 65 73 20 6f  f usable bytes o
1b1e8 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20  n each page */. 
1b1e9 20 69 6e 74 20 6e 54 72 61 6e 73 61 63 74 69 6f   int nTransactio
1b1ea 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  n;     /* Number
1b1eb 20 6f 66 20 6f 70 65 6e 20 74 72 61 6e 73 61 63   of open transac
1b1ec 74 69 6f 6e 73 20 28 72 65 61 64 20 2b 20 77 72  tions (read + wr
1b1ed 69 74 65 29 20 2a 2f 0a 20 20 75 33 32 20 6e 50  ite) */.  u32 nP
1b1ee 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
1b1ef 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
1b1f0 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  es in the databa
1b1f1 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53  se */.  void *pS
1b1f2 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a  chema;        /*
1b1f3 20 50 6f 69 6e 74 65 72 20 74 6f 20 73 70 61 63   Pointer to spac
1b1f4 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73  e allocated by s
1b1f5 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d  qlite3BtreeSchem
1b1f6 61 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  a() */.  void (*
1b1f7 78 46 72 65 65 53 63 68 65 6d 61 29 28 76 6f 69  xFreeSchema)(voi
1b1f8 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63  d*);  /* Destruc
1b1f9 74 6f 72 20 66 6f 72 20 42 74 53 68 61 72 65 64  tor for BtShared
1b1fa 2e 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71  .pSchema */.  sq
1b1fb 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
1b1fc 65 78 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72  ex; /* Non-recur
1b1fd 73 69 76 65 20 6d 75 74 65 78 20 72 65 71 75 69  sive mutex requi
1b1fe 72 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68  red to access th
1b1ff 69 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 42  is object */.  B
1b200 69 74 76 65 63 20 2a 70 48 61 73 43 6f 6e 74 65  itvec *pHasConte
1b201 6e 74 3b 20 20 2f 2a 20 53 65 74 20 6f 66 20 70  nt;  /* Set of p
1b202 61 67 65 73 20 6d 6f 76 65 64 20 74 6f 20 66 72  ages moved to fr
1b203 65 65 2d 6c 69 73 74 20 74 68 69 73 20 74 72 61  ee-list this tra
1b204 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e  nsaction */.#ifn
1b205 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1b206 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69  SHARED_CACHE.  i
1b207 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
1b208 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1b209 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  f references to 
1b20a 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a  this structure *
1b20b 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 4e  /.  BtShared *pN
1b20c 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78  ext;      /* Nex
1b20d 74 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66 20 73  t on a list of s
1b20e 68 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64  harable BtShared
1b20f 20 73 74 72 75 63 74 73 20 2a 2f 0a 20 20 42 74   structs */.  Bt
1b210 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20 20 20 20  Lock *pLock;    
1b211 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6c      /* List of l
1b212 6f 63 6b 73 20 68 65 6c 64 20 6f 6e 20 74 68 69  ocks held on thi
1b213 73 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 73  s shared-btree s
1b214 74 72 75 63 74 20 2a 2f 0a 20 20 42 74 72 65 65  truct */.  Btree
1b215 20 2a 70 57 72 69 74 65 72 3b 20 20 20 20 20 20   *pWriter;      
1b216 20 2f 2a 20 42 74 72 65 65 20 77 69 74 68 20 63   /* Btree with c
1b217 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 20 77 72  urrently open wr
1b218 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite transaction 
1b219 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 38 20 2a  */.#endif.  u8 *
1b21a 70 54 6d 70 53 70 61 63 65 3b 20 20 20 20 20 20  pTmpSpace;      
1b21b 20 20 2f 2a 20 42 74 53 68 61 72 65 64 2e 70 61    /* BtShared.pa
1b21c 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20  geSize bytes of 
1b21d 73 70 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73  space for tmp us
1b21e 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  e */.};../*.** A
1b21f 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f  llowed values fo
1b220 72 20 42 74 53 68 61 72 65 64 2e 62 74 73 46 6c  r BtShared.btsFl
1b221 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42  ags.*/.#define B
1b222 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 20 20 20 20  TS_READ_ONLY    
1b223 20 20 20 20 30 78 30 30 30 31 20 20 20 2f 2a 20      0x0001   /* 
1b224 55 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20  Underlying file 
1b225 69 73 20 72 65 61 64 6f 6e 6c 79 20 2a 2f 0a 23  is readonly */.#
1b226 64 65 66 69 6e 65 20 42 54 53 5f 50 41 47 45 53  define BTS_PAGES
1b227 49 5a 45 5f 46 49 58 45 44 20 20 20 30 78 30 30  IZE_FIXED   0x00
1b228 30 32 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a  02   /* Page siz
1b229 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20  e can no longer 
1b22a 62 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64  be changed */.#d
1b22b 65 66 69 6e 65 20 42 54 53 5f 53 45 43 55 52 45  efine BTS_SECURE
1b22c 5f 44 45 4c 45 54 45 20 20 20 20 30 78 30 30 30  _DELETE    0x000
1b22d 34 20 20 20 2f 2a 20 50 52 41 47 4d 41 20 73 65  4   /* PRAGMA se
1b22e 63 75 72 65 5f 64 65 6c 65 74 65 20 69 73 20 65  cure_delete is e
1b22f 6e 61 62 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e  nabled */.#defin
1b230 65 20 42 54 53 5f 49 4e 49 54 49 41 4c 4c 59 5f  e BTS_INITIALLY_
1b231 45 4d 50 54 59 20 20 30 78 30 30 30 38 20 20 20  EMPTY  0x0008   
1b232 2f 2a 20 44 61 74 61 62 61 73 65 20 77 61 73 20  /* Database was 
1b233 65 6d 70 74 79 20 61 74 20 74 72 61 6e 73 20 73  empty at trans s
1b234 74 61 72 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tart */.#define 
1b235 42 54 53 5f 4e 4f 5f 57 41 4c 20 20 20 20 20 20  BTS_NO_WAL      
1b236 20 20 20 20 20 30 78 30 30 31 30 20 20 20 2f 2a       0x0010   /*
1b237 20 44 6f 20 6e 6f 74 20 6f 70 65 6e 20 77 72 69   Do not open wri
1b238 74 65 2d 61 68 65 61 64 2d 6c 6f 67 20 66 69 6c  te-ahead-log fil
1b239 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  es */.#define BT
1b23a 53 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20  S_EXCLUSIVE     
1b23b 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a 20 70     0x0020   /* p
1b23c 57 72 69 74 65 72 20 68 61 73 20 61 6e 20 65 78  Writer has an ex
1b23d 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a  clusive lock */.
1b23e 23 64 65 66 69 6e 65 20 42 54 53 5f 50 45 4e 44  #define BTS_PEND
1b23f 49 4e 47 20 20 20 20 20 20 20 20 20 20 30 78 30  ING          0x0
1b240 30 34 30 20 20 20 2f 2a 20 57 61 69 74 69 6e 67  040   /* Waiting
1b241 20 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b 73 20   for read-locks 
1b242 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 0a 2f 2a 0a  to clear */../*.
1b243 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
1b244 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
1b245 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
1b246 64 20 74 6f 20 68 6f 6c 64 20 69 6e 66 6f 72 6d  d to hold inform
1b247 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 61  ation.** about a
1b248 20 63 65 6c 6c 2e 20 20 54 68 65 20 70 61 72 73   cell.  The pars
1b249 65 43 65 6c 6c 50 74 72 28 29 20 66 75 6e 63 74  eCellPtr() funct
1b24a 69 6f 6e 20 66 69 6c 6c 73 20 69 6e 20 74 68 69  ion fills in thi
1b24b 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 62  s structure.** b
1b24c 61 73 65 64 20 6f 6e 20 69 6e 66 6f 72 6d 61 74  ased on informat
1b24d 69 6f 6e 20 65 78 74 72 61 63 74 20 66 72 6f 6d  ion extract from
1b24e 20 74 68 65 20 72 61 77 20 64 69 73 6b 20 70 61   the raw disk pa
1b24f 67 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ge..*/.typedef s
1b250 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 43  truct CellInfo C
1b251 65 6c 6c 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20  ellInfo;.struct 
1b252 43 65 6c 6c 49 6e 66 6f 20 7b 0a 20 20 69 36 34  CellInfo {.  i64
1b253 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 54   nKey;      /* T
1b254 68 65 20 6b 65 79 20 66 6f 72 20 49 4e 54 4b 45  he key for INTKE
1b255 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 6e 75 6d  Y tables, or num
1b256 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
1b257 6b 65 79 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65  key */.  u8 *pCe
1b258 6c 6c 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  ll;     /* Point
1b259 65 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 20  er to the start 
1b25a 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  of cell content 
1b25b 2a 2f 0a 20 20 75 33 32 20 6e 44 61 74 61 3b 20  */.  u32 nData; 
1b25c 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1b25d 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 2a   bytes of data *
1b25e 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f 61 64  /.  u32 nPayload
1b25f 3b 20 20 2f 2a 20 54 6f 74 61 6c 20 61 6d 6f 75  ;  /* Total amou
1b260 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 2a 2f  nt of payload */
1b261 0a 20 20 75 31 36 20 6e 48 65 61 64 65 72 3b 20  .  u16 nHeader; 
1b262 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
1b263 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65   cell content he
1b264 61 64 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f  ader in bytes */
1b265 0a 20 20 75 31 36 20 6e 4c 6f 63 61 6c 3b 20 20  .  u16 nLocal;  
1b266 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 70    /* Amount of p
1b267 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61  ayload held loca
1b268 6c 6c 79 20 2a 2f 0a 20 20 75 31 36 20 69 4f 76  lly */.  u16 iOv
1b269 65 72 66 6c 6f 77 3b 20 2f 2a 20 4f 66 66 73 65  erflow; /* Offse
1b26a 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61  t to overflow pa
1b26b 67 65 20 6e 75 6d 62 65 72 2e 20 20 5a 65 72 6f  ge number.  Zero
1b26c 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20   if no overflow 
1b26d 2a 2f 0a 20 20 75 31 36 20 6e 53 69 7a 65 3b 20  */.  u16 nSize; 
1b26e 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
1b26f 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
1b270 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74 72  on the main b-tr
1b271 65 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f  ee page */.};../
1b272 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70  *.** Maximum dep
1b273 74 68 20 6f 66 20 61 6e 20 53 51 4c 69 74 65 20  th of an SQLite 
1b274 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 65  B-Tree structure
1b275 2e 20 41 6e 79 20 42 2d 54 72 65 65 20 64 65 65  . Any B-Tree dee
1b276 70 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 69 73  per than.** this
1b277 20 77 69 6c 6c 20 62 65 20 64 65 63 6c 61 72 65   will be declare
1b278 64 20 63 6f 72 72 75 70 74 2e 20 54 68 69 73 20  d corrupt. This 
1b279 76 61 6c 75 65 20 69 73 20 63 61 6c 63 75 6c 61  value is calcula
1b27a 74 65 64 20 62 61 73 65 64 20 6f 6e 20 61 0a 2a  ted based on a.*
1b27b 2a 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 61  * maximum databa
1b27c 73 65 20 73 69 7a 65 20 6f 66 20 32 5e 33 31 20  se size of 2^31 
1b27d 70 61 67 65 73 20 61 20 6d 69 6e 69 6d 75 6d 20  pages a minimum 
1b27e 66 61 6e 6f 75 74 20 6f 66 20 32 20 66 6f 72 20  fanout of 2 for 
1b27f 61 0a 2a 2a 20 72 6f 6f 74 2d 6e 6f 64 65 20 61  a.** root-node a
1b280 6e 64 20 33 20 66 6f 72 20 61 6c 6c 20 6f 74 68  nd 3 for all oth
1b281 65 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65  er internal node
1b282 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72  s..**.** If a tr
1b283 65 65 20 74 68 61 74 20 61 70 70 65 61 72 73 20  ee that appears 
1b284 74 6f 20 62 65 20 74 61 6c 6c 65 72 20 74 68 61  to be taller tha
1b285 6e 20 74 68 69 73 20 69 73 20 65 6e 63 6f 75 6e  n this is encoun
1b286 74 65 72 65 64 2c 20 69 74 20 69 73 0a 2a 2a 20  tered, it is.** 
1b287 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65  assumed that the
1b288 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72   database is cor
1b289 72 75 70 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  rupt..*/.#define
1b28a 20 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45   BTCURSOR_MAX_DE
1b28b 50 54 48 20 32 30 0a 0a 2f 2a 0a 2a 2a 20 41 20  PTH 20../*.** A 
1b28c 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e  cursor is a poin
1b28d 74 65 72 20 74 6f 20 61 20 70 61 72 74 69 63 75  ter to a particu
1b28e 6c 61 72 20 65 6e 74 72 79 20 77 69 74 68 69 6e  lar entry within
1b28f 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a   a particular.**
1b290 20 62 2d 74 72 65 65 20 77 69 74 68 69 6e 20 61   b-tree within a
1b291 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
1b292 2a 2a 0a 2a 2a 20 54 68 65 20 65 6e 74 72 79 20  **.** The entry 
1b293 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  is identified by
1b294 20 69 74 73 20 4d 65 6d 50 61 67 65 20 61 6e 64   its MemPage and
1b295 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a   the index in.**
1b296 20 4d 65 6d 50 61 67 65 2e 61 43 65 6c 6c 5b 5d   MemPage.aCell[]
1b297 20 6f 66 20 74 68 65 20 65 6e 74 72 79 2e 0a 2a   of the entry..*
1b298 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61  *.** A single da
1b299 74 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20  tabase file can 
1b29a 62 65 20 73 68 61 72 65 64 20 62 79 20 74 77 6f  be shared by two
1b29b 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 63   more database c
1b29c 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 62  onnections,.** b
1b29d 75 74 20 63 75 72 73 6f 72 73 20 63 61 6e 6e 6f  ut cursors canno
1b29e 74 20 62 65 20 73 68 61 72 65 64 2e 20 20 45 61  t be shared.  Ea
1b29f 63 68 20 63 75 72 73 6f 72 20 69 73 20 61 73 73  ch cursor is ass
1b2a0 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 0a 2a  ociated with a.*
1b2a1 2a 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74  * particular dat
1b2a2 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1b2a3 20 69 64 65 6e 74 69 66 69 65 64 20 42 74 43 75   identified BtCu
1b2a4 72 73 6f 72 2e 70 42 74 72 65 65 2e 64 62 2e 0a  rsor.pBtree.db..
1b2a5 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e 20  **.** Fields in 
1b2a6 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61  this structure a
1b2a7 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65  re accessed unde
1b2a8 72 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6d  r the BtShared.m
1b2a9 75 74 65 78 0a 2a 2a 20 66 6f 75 6e 64 20 61 74  utex.** found at
1b2aa 20 73 65 6c 66 2d 3e 70 42 74 2d 3e 6d 75 74 65   self->pBt->mute
1b2ab 78 2e 20 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74  x. .*/.struct Bt
1b2ac 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 72 65 65  Cursor {.  Btree
1b2ad 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 20 20   *pBtree;       
1b2ae 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 72 65       /* The Btre
1b2af 65 20 74 6f 20 77 68 69 63 68 20 74 68 69 73 20  e to which this 
1b2b0 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 2a  cursor belongs *
1b2b1 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  /.  BtShared *pB
1b2b2 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
1b2b3 20 54 68 65 20 42 74 53 68 61 72 65 64 20 74 68   The BtShared th
1b2b4 69 73 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73  is cursor points
1b2b5 20 74 6f 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f   to */.  BtCurso
1b2b6 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76  r *pNext, *pPrev
1b2b7 3b 20 20 2f 2a 20 46 6f 72 6d 73 20 61 20 6c 69  ;  /* Forms a li
1b2b8 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c  nked list of all
1b2b9 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 73 74   cursors */.  st
1b2ba 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  ruct KeyInfo *pK
1b2bb 65 79 49 6e 66 6f 3b 20 2f 2a 20 41 72 67 75 6d  eyInfo; /* Argum
1b2bc 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 63 6f  ent passed to co
1b2bd 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
1b2be 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  n */.#ifndef SQL
1b2bf 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
1b2c0 42 0a 20 20 50 67 6e 6f 20 2a 61 4f 76 65 72 66  B.  Pgno *aOverf
1b2c1 6c 6f 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  low;          /*
1b2c2 20 43 61 63 68 65 20 6f 66 20 6f 76 65 72 66 6c   Cache of overfl
1b2c3 6f 77 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e  ow page location
1b2c4 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 50 67  s */.#endif.  Pg
1b2c5 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20 20  no pgnoRoot;    
1b2c6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
1b2c7 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69 73  oot page of this
1b2c8 20 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c 69 74   tree */.  sqlit
1b2c9 65 33 5f 69 6e 74 36 34 20 63 61 63 68 65 64 52  e3_int64 cachedR
1b2ca 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20 72 6f  owid; /* Next ro
1b2cb 77 69 64 20 63 61 63 68 65 2e 20 20 30 20 6d 65  wid cache.  0 me
1b2cc 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20 2a 2f  ans not valid */
1b2cd 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f  .  CellInfo info
1b2ce 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1b2cf 41 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 63  A parse of the c
1b2d0 65 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 6e 74  ell we are point
1b2d1 69 6e 67 20 61 74 20 2a 2f 0a 20 20 69 36 34 20  ing at */.  i64 
1b2d2 6e 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20  nKey;        /* 
1b2d3 53 69 7a 65 20 6f 66 20 70 4b 65 79 2c 20 6f 72  Size of pKey, or
1b2d4 20 6c 61 73 74 20 69 6e 74 65 67 65 72 20 6b 65   last integer ke
1b2d5 79 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b 65  y */.  void *pKe
1b2d6 79 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64  y;      /* Saved
1b2d7 20 6b 65 79 20 74 68 61 74 20 77 61 73 20 63 75   key that was cu
1b2d8 72 73 6f 72 27 73 20 6c 61 73 74 20 6b 6e 6f 77  rsor's last know
1b2d9 6e 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 20 20  n position */.  
1b2da 69 6e 74 20 73 6b 69 70 4e 65 78 74 3b 20 20 20  int skipNext;   
1b2db 20 2f 2a 20 50 72 65 76 28 29 20 69 73 20 6e 6f   /* Prev() is no
1b2dc 6f 70 20 69 66 20 6e 65 67 61 74 69 76 65 2e 20  op if negative. 
1b2dd 4e 65 78 74 28 29 20 69 73 20 6e 6f 6f 70 20 69  Next() is noop i
1b2de 66 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 20  f positive */.  
1b2df 75 38 20 77 72 46 6c 61 67 3b 20 20 20 20 20 20  u8 wrFlag;      
1b2e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1b2e1 65 20 69 66 20 77 72 69 74 61 62 6c 65 20 2a 2f  e if writable */
1b2e2 0a 20 20 75 38 20 61 74 4c 61 73 74 3b 20 20 20  .  u8 atLast;   
1b2e3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b2e4 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  Cursor pointing 
1b2e5 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72  to the last entr
1b2e6 79 20 2a 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e  y */.  u8 validN
1b2e7 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  Key;            
1b2e8 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 66 6f   /* True if info
1b2e9 2e 6e 4b 65 79 20 69 73 20 76 61 6c 69 64 20 2a  .nKey is valid *
1b2ea 2f 0a 20 20 75 38 20 65 53 74 61 74 65 3b 20 20  /.  u8 eState;  
1b2eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1b2ec 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 55 52 53   One of the CURS
1b2ed 4f 52 5f 58 58 58 20 63 6f 6e 73 74 61 6e 74 73  OR_XXX constants
1b2ee 20 28 73 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a   (see below) */.
1b2ef 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1b2f0 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 75  MIT_INCRBLOB.  u
1b2f1 38 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64  8 isIncrblobHand
1b2f2 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  le;      /* True
1b2f3 20 69 66 20 74 68 69 73 20 63 75 72 73 6f 72 20   if this cursor 
1b2f4 69 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20 68  is an incr. io h
1b2f5 61 6e 64 6c 65 20 2a 2f 0a 23 65 6e 64 69 66 0a  andle */.#endif.
1b2f6 20 20 75 38 20 68 69 6e 74 73 3b 20 20 20 20 20    u8 hints;     
1b2f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b2f8 20 20 20 20 20 20 20 20 2f 2a 20 41 73 20 63 6f          /* As co
1b2f9 6e 66 69 67 75 72 65 64 20 62 79 20 43 75 72 73  nfigured by Curs
1b2fa 6f 72 53 65 74 48 69 6e 74 73 28 29 20 2a 2f 0a  orSetHints() */.
1b2fb 20 20 69 31 36 20 69 50 61 67 65 3b 20 20 20 20    i16 iPage;    
1b2fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b2fd 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
1b2fe 20 6f 66 20 63 75 72 72 65 6e 74 20 70 61 67 65   of current page
1b2ff 20 69 6e 20 61 70 50 61 67 65 20 2a 2f 0a 20 20   in apPage */.  
1b300 75 31 36 20 61 69 49 64 78 5b 42 54 43 55 52 53  u16 aiIdx[BTCURS
1b301 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20  OR_MAX_DEPTH];  
1b302 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
1b303 20 69 6e 64 65 78 20 69 6e 20 61 70 50 61 67 65   index in apPage
1b304 5b 69 5d 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65  [i] */.  MemPage
1b305 20 2a 61 70 50 61 67 65 5b 42 54 43 55 52 53 4f   *apPage[BTCURSO
1b306 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 2f  R_MAX_DEPTH];  /
1b307 2a 20 50 61 67 65 73 20 66 72 6f 6d 20 72 6f 6f  * Pages from roo
1b308 74 20 74 6f 20 63 75 72 72 65 6e 74 20 70 61 67  t to current pag
1b309 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50  e */.};../*.** P
1b30a 6f 74 65 6e 74 69 61 6c 20 76 61 6c 75 65 73 20  otential values 
1b30b 66 6f 72 20 42 74 43 75 72 73 6f 72 2e 65 53 74  for BtCursor.eSt
1b30c 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f  ate..**.** CURSO
1b30d 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75  R_VALID:.**   Cu
1b30e 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 61  rsor points to a
1b30f 20 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 67 65   valid entry. ge
1b310 74 50 61 79 6c 6f 61 64 28 29 20 65 74 63 2e 20  tPayload() etc. 
1b311 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 2e 0a 2a  may be called..*
1b312 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41  *.** CURSOR_INVA
1b313 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f 72  LID:.**   Cursor
1b314 20 64 6f 65 73 20 6e 6f 74 20 70 6f 69 6e 74 20   does not point 
1b315 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79  to a valid entry
1b316 2e 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65  . This can happe
1b317 6e 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 20  n (for example) 
1b318 0a 2a 2a 20 20 20 62 65 63 61 75 73 65 20 74 68  .**   because th
1b319 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79  e table is empty
1b31a 20 6f 72 20 62 65 63 61 75 73 65 20 42 74 72 65   or because Btre
1b31b 65 43 75 72 73 6f 72 46 69 72 73 74 28 29 20 68  eCursorFirst() h
1b31c 61 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 20  as not been.**  
1b31d 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43   called..**.** C
1b31e 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
1b31f 4b 3a 0a 2a 2a 20 20 20 54 68 65 20 74 61 62 6c  K:.**   The tabl
1b320 65 20 74 68 61 74 20 74 68 69 73 20 63 75 72 73  e that this curs
1b321 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e  or was opened on
1b322 20 73 74 69 6c 6c 20 65 78 69 73 74 73 2c 20 62   still exists, b
1b323 75 74 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20  ut has been .** 
1b324 20 20 6d 6f 64 69 66 69 65 64 20 73 69 6e 63 65    modified since
1b325 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 20   the cursor was 
1b326 6c 61 73 74 20 75 73 65 64 2e 20 54 68 65 20 63  last used. The c
1b327 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69  ursor position i
1b328 73 20 73 61 76 65 64 0a 2a 2a 20 20 20 69 6e 20  s saved.**   in 
1b329 76 61 72 69 61 62 6c 65 73 20 42 74 43 75 72 73  variables BtCurs
1b32a 6f 72 2e 70 4b 65 79 20 61 6e 64 20 42 74 43 75  or.pKey and BtCu
1b32b 72 73 6f 72 2e 6e 4b 65 79 2e 20 57 68 65 6e 20  rsor.nKey. When 
1b32c 61 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 0a  a cursor is in .
1b32d 2a 2a 20 20 20 74 68 69 73 20 73 74 61 74 65 2c  **   this state,
1b32e 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
1b32f 73 69 74 69 6f 6e 28 29 20 63 61 6e 20 62 65 20  sition() can be 
1b330 63 61 6c 6c 65 64 20 74 6f 20 61 74 74 65 6d 70  called to attemp
1b331 74 20 74 6f 0a 2a 2a 20 20 20 73 65 65 6b 20 74  t to.**   seek t
1b332 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  he cursor to the
1b333 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e 2e   saved position.
1b334 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 46 41  .**.** CURSOR_FA
1b335 55 4c 54 3a 0a 2a 2a 20 20 20 41 20 75 6e 72 65  ULT:.**   A unre
1b336 63 6f 76 65 72 61 62 6c 65 20 65 72 72 6f 72 20  coverable error 
1b337 28 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 72  (an I/O error or
1b338 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72   a malloc failur
1b339 65 29 20 68 61 73 20 6f 63 63 75 72 72 65 64 0a  e) has occurred.
1b33a 2a 2a 20 20 20 6f 6e 20 61 20 64 69 66 66 65 72  **   on a differ
1b33b 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ent connection t
1b33c 68 61 74 20 73 68 61 72 65 73 20 74 68 65 20 42  hat shares the B
1b33d 74 53 68 61 72 65 64 20 63 61 63 68 65 20 77 69  tShared cache wi
1b33e 74 68 20 74 68 69 73 0a 2a 2a 20 20 20 63 75 72  th this.**   cur
1b33f 73 6f 72 2e 20 20 54 68 65 20 65 72 72 6f 72 20  sor.  The error 
1b340 68 61 73 20 6c 65 66 74 20 74 68 65 20 63 61 63  has left the cac
1b341 68 65 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69  he in an inconsi
1b342 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a 2a 20  stent state..** 
1b343 20 20 44 6f 20 6e 6f 74 68 69 6e 67 20 65 6c 73    Do nothing els
1b344 65 20 77 69 74 68 20 74 68 69 73 20 63 75 72 73  e with this curs
1b345 6f 72 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74  or.  Any attempt
1b346 20 74 6f 20 75 73 65 20 74 68 65 20 63 75 72 73   to use the curs
1b347 6f 72 0a 2a 2a 20 20 20 73 68 6f 75 6c 64 20 72  or.**   should r
1b348 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20  eturn the error 
1b349 63 6f 64 65 20 73 74 6f 72 65 64 20 69 6e 20 42  code stored in B
1b34a 74 43 75 72 73 6f 72 2e 73 6b 69 70 0a 2a 2f 0a  tCursor.skip.*/.
1b34b 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 49  #define CURSOR_I
1b34c 4e 56 41 4c 49 44 20 20 20 20 20 20 20 20 20 20  NVALID          
1b34d 20 30 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f   0.#define CURSO
1b34e 52 5f 56 41 4c 49 44 20 20 20 20 20 20 20 20 20  R_VALID         
1b34f 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 43 55      1.#define CU
1b350 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
1b351 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65         2.#define
1b352 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20   CURSOR_FAULT   
1b353 20 20 20 20 20 20 20 20 20 20 33 0a 0a 2f 2a 20            3../* 
1b354 0a 2a 2a 20 54 68 65 20 64 61 74 61 62 61 73 65  .** The database
1b355 20 70 61 67 65 20 74 68 65 20 50 45 4e 44 49 4e   page the PENDIN
1b356 47 5f 42 59 54 45 20 6f 63 63 75 70 69 65 73 2e  G_BYTE occupies.
1b357 20 54 68 69 73 20 70 61 67 65 20 69 73 20 6e 65   This page is ne
1b358 76 65 72 20 75 73 65 64 2e 0a 2a 2f 0a 23 20 64  ver used..*/.# d
1b359 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59  efine PENDING_BY
1b35a 54 45 5f 50 41 47 45 28 70 42 74 29 20 50 41 47  TE_PAGE(pBt) PAG
1b35b 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 42 74 29 0a  ER_MJ_PGNO(pBt).
1b35c 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63  ./*.** These mac
1b35d 72 6f 73 20 64 65 66 69 6e 65 20 74 68 65 20 6c  ros define the l
1b35e 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70  ocation of the p
1b35f 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79  ointer-map entry
1b360 20 66 6f 72 20 61 20 0a 2a 2a 20 64 61 74 61 62   for a .** datab
1b361 61 73 65 20 70 61 67 65 2e 20 54 68 65 20 66 69  ase page. The fi
1b362 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
1b363 65 61 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62  each is the numb
1b364 65 72 20 6f 66 20 75 73 61 62 6c 65 0a 2a 2a 20  er of usable.** 
1b365 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61  bytes on each pa
1b366 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ge of the databa
1b367 73 65 20 28 6f 66 74 65 6e 20 31 30 32 34 29 2e  se (often 1024).
1b368 20 54 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74   The second is t
1b369 68 65 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65  he.** page numbe
1b36a 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 20 69 6e 20  r to look up in 
1b36b 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e  the pointer map.
1b36c 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 50 41  .**.** PTRMAP_PA
1b36d 47 45 4e 4f 20 72 65 74 75 72 6e 73 20 74 68 65  GENO returns the
1b36e 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 6e   database page n
1b36f 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 6f 69  umber of the poi
1b370 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 70 61 67 65  nter-map.** page
1b371 20 74 68 61 74 20 73 74 6f 72 65 73 20 74 68 65   that stores the
1b372 20 72 65 71 75 69 72 65 64 20 70 6f 69 6e 74 65   required pointe
1b373 72 2e 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46  r. PTRMAP_PTROFF
1b374 53 45 54 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74  SET returns.** t
1b375 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65  he offset of the
1b376 20 72 65 71 75 65 73 74 65 64 20 6d 61 70 20 65   requested map e
1b377 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ntry..**.** If t
1b378 68 65 20 70 67 6e 6f 20 61 72 67 75 6d 65 6e 74  he pgno argument
1b379 20 70 61 73 73 65 64 20 74 6f 20 50 54 52 4d 41   passed to PTRMA
1b37a 50 5f 50 41 47 45 4e 4f 20 69 73 20 61 20 70 6f  P_PAGENO is a po
1b37b 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 0a  inter-map page,.
1b37c 2a 2a 20 74 68 65 6e 20 70 67 6e 6f 20 69 73 20  ** then pgno is 
1b37d 72 65 74 75 72 6e 65 64 2e 20 53 6f 20 28 70 67  returned. So (pg
1b37e 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e  no==PTRMAP_PAGEN
1b37f 4f 28 70 67 73 7a 2c 20 70 67 6e 6f 29 29 20 63  O(pgsz, pgno)) c
1b380 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f  an be.** used to
1b381 20 74 65 73 74 20 69 66 20 70 67 6e 6f 20 69 73   test if pgno is
1b382 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70   a pointer-map p
1b383 61 67 65 2e 20 50 54 52 4d 41 50 5f 49 53 50 41  age. PTRMAP_ISPA
1b384 47 45 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a  GE implements.**
1b385 20 74 68 69 73 20 74 65 73 74 2e 0a 2a 2f 0a 23   this test..*/.#
1b386 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 50 41  define PTRMAP_PA
1b387 47 45 4e 4f 28 70 42 74 2c 20 70 67 6e 6f 29 20  GENO(pBt, pgno) 
1b388 70 74 72 6d 61 70 50 61 67 65 6e 6f 28 70 42 74  ptrmapPageno(pBt
1b389 2c 20 70 67 6e 6f 29 0a 23 64 65 66 69 6e 65 20  , pgno).#define 
1b38a 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54  PTRMAP_PTROFFSET
1b38b 28 70 67 70 74 72 6d 61 70 2c 20 70 67 6e 6f 29  (pgptrmap, pgno)
1b38c 20 28 35 2a 28 70 67 6e 6f 2d 70 67 70 74 72 6d   (5*(pgno-pgptrm
1b38d 61 70 2d 31 29 29 0a 23 64 65 66 69 6e 65 20 50  ap-1)).#define P
1b38e 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74  TRMAP_ISPAGE(pBt
1b38f 2c 20 70 67 6e 6f 29 20 28 50 54 52 4d 41 50 5f  , pgno) (PTRMAP_
1b390 50 41 47 45 4e 4f 28 28 70 42 74 29 2c 28 70 67  PAGENO((pBt),(pg
1b391 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29 29 0a 0a 2f  no))==(pgno))../
1b392 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72  *.** The pointer
1b393 20 6d 61 70 20 69 73 20 61 20 6c 6f 6f 6b 75 70   map is a lookup
1b394 20 74 61 62 6c 65 20 74 68 61 74 20 69 64 65 6e   table that iden
1b395 74 69 66 69 65 73 20 74 68 65 20 70 61 72 65 6e  tifies the paren
1b396 74 20 70 61 67 65 20 66 6f 72 0a 2a 2a 20 65 61  t page for.** ea
1b397 63 68 20 63 68 69 6c 64 20 70 61 67 65 20 69 6e  ch child page in
1b398 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1b399 6c 65 2e 20 20 54 68 65 20 70 61 72 65 6e 74 20  le.  The parent 
1b39a 70 61 67 65 20 69 73 20 74 68 65 20 70 61 67 65  page is the page
1b39b 20 74 68 61 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e   that.** contain
1b39c 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
1b39d 68 65 20 63 68 69 6c 64 2e 20 20 45 76 65 72 79  he child.  Every
1b39e 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74   page in the dat
1b39f 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 73 0a 2a  abase contains.*
1b3a0 2a 20 30 20 6f 72 20 31 20 70 61 72 65 6e 74 20  * 0 or 1 parent 
1b3a1 70 61 67 65 73 2e 20 20 28 49 6e 20 74 68 69 73  pages.  (In this
1b3a2 20 63 6f 6e 74 65 78 74 20 27 64 61 74 61 62 61   context 'databa
1b3a3 73 65 20 70 61 67 65 27 20 72 65 66 65 72 73 0a  se page' refers.
1b3a4 2a 2a 20 74 6f 20 61 6e 79 20 70 61 67 65 20 74  ** to any page t
1b3a5 68 61 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20  hat is not part 
1b3a6 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  of the pointer m
1b3a7 61 70 20 69 74 73 65 6c 66 2e 29 20 20 45 61 63  ap itself.)  Eac
1b3a8 68 20 70 6f 69 6e 74 65 72 20 6d 61 70 0a 2a 2a  h pointer map.**
1b3a9 20 65 6e 74 72 79 20 63 6f 6e 73 69 73 74 73 20   entry consists 
1b3aa 6f 66 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65  of a single byte
1b3ab 20 27 74 79 70 65 27 20 61 6e 64 20 61 20 34 20   'type' and a 4 
1b3ac 62 79 74 65 20 70 61 72 65 6e 74 20 70 61 67 65  byte parent page
1b3ad 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 54 68 65 20   number..** The 
1b3ae 50 54 52 4d 41 50 5f 58 58 58 20 69 64 65 6e 74  PTRMAP_XXX ident
1b3af 69 66 69 65 72 73 20 62 65 6c 6f 77 20 61 72 65  ifiers below are
1b3b0 20 74 68 65 20 76 61 6c 69 64 20 74 79 70 65 73   the valid types
1b3b1 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 75 72 70  ..**.** The purp
1b3b2 6f 73 65 20 6f 66 20 74 68 65 20 70 6f 69 6e 74  ose of the point
1b3b3 65 72 20 6d 61 70 20 69 73 20 74 6f 20 66 61 63  er map is to fac
1b3b4 69 6c 69 74 79 20 6d 6f 76 69 6e 67 20 70 61 67  ility moving pag
1b3b5 65 73 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 70  es from one.** p
1b3b6 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66  osition in the f
1b3b7 69 6c 65 20 74 6f 20 61 6e 6f 74 68 65 72 20 61  ile to another a
1b3b8 73 20 70 61 72 74 20 6f 66 20 61 75 74 6f 76 61  s part of autova
1b3b9 63 75 75 6d 2e 20 20 57 68 65 6e 20 61 20 70 61  cuum.  When a pa
1b3ba 67 65 0a 2a 2a 20 69 73 20 6d 6f 76 65 64 2c 20  ge.** is moved, 
1b3bb 74 68 65 20 70 6f 69 6e 74 65 72 20 69 6e 20 69  the pointer in i
1b3bc 74 73 20 70 61 72 65 6e 74 20 6d 75 73 74 20 62  ts parent must b
1b3bd 65 20 75 70 64 61 74 65 64 20 74 6f 20 70 6f 69  e updated to poi
1b3be 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 77  nt to the.** new
1b3bf 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 20   location.  The 
1b3c0 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 75  pointer map is u
1b3c1 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68  sed to locate th
1b3c2 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 71 75  e parent page qu
1b3c3 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 54 52  ickly..**.** PTR
1b3c4 4d 41 50 5f 52 4f 4f 54 50 41 47 45 3a 20 54 68  MAP_ROOTPAGE: Th
1b3c5 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  e database page 
1b3c6 69 73 20 61 20 72 6f 6f 74 2d 70 61 67 65 2e 20  is a root-page. 
1b3c7 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20  The page-number 
1b3c8 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20  is not.**       
1b3c9 20 20 20 20 20 20 20 20 20 20 20 75 73 65 64 20             used 
1b3ca 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a  in this case..**
1b3cb 0a 2a 2a 20 50 54 52 4d 41 50 5f 46 52 45 45 50  .** PTRMAP_FREEP
1b3cc 41 47 45 3a 20 54 68 65 20 64 61 74 61 62 61 73  AGE: The databas
1b3cd 65 20 70 61 67 65 20 69 73 20 61 6e 20 75 6e 75  e page is an unu
1b3ce 73 65 64 20 28 66 72 65 65 29 20 70 61 67 65 2e  sed (free) page.
1b3cf 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72   The page-number
1b3d0 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20   .**            
1b3d1 20 20 20 20 20 20 69 73 20 6e 6f 74 20 75 73 65        is not use
1b3d2 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  d in this case..
1b3d3 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45  **.** PTRMAP_OVE
1b3d4 52 46 4c 4f 57 31 3a 20 54 68 65 20 64 61 74 61  RFLOW1: The data
1b3d5 62 61 73 65 20 70 61 67 65 20 69 73 20 74 68 65  base page is the
1b3d6 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20 61   first page in a
1b3d7 20 6c 69 73 74 20 6f 66 20 0a 2a 2a 20 20 20 20   list of .**    
1b3d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
1b3d9 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54  verflow pages. T
1b3da 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69  he page number i
1b3db 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61  dentifies the pa
1b3dc 67 65 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20  ge that.**      
1b3dd 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
1b3de 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 77  tains the cell w
1b3df 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ith a pointer to
1b3e0 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 70   this overflow p
1b3e1 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  age..**.** PTRMA
1b3e2 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 20 54 68 65  P_OVERFLOW2: The
1b3e3 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69   database page i
1b3e4 73 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 72 20  s the second or 
1b3e5 6c 61 74 65 72 20 70 61 67 65 20 69 6e 20 61 20  later page in a 
1b3e6 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20  list of.**      
1b3e7 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 76 65               ove
1b3e8 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65  rflow pages. The
1b3e9 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 64 65   page-number ide
1b3ea 6e 74 69 66 69 65 73 20 74 68 65 20 70 72 65 76  ntifies the prev
1b3eb 69 6f 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ious.**         
1b3ec 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20 69            page i
1b3ed 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  n the overflow p
1b3ee 61 67 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  age list..**.** 
1b3ef 50 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 54 68  PTRMAP_BTREE: Th
1b3f0 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  e database page 
1b3f1 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f 74 20 62 74  is a non-root bt
1b3f2 72 65 65 20 70 61 67 65 2e 20 54 68 65 20 70 61  ree page. The pa
1b3f3 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20  ge number.**    
1b3f4 20 20 20 20 20 20 20 20 20 20 20 69 64 65 6e 74             ident
1b3f5 69 66 69 65 73 20 74 68 65 20 70 61 72 65 6e 74  ifies the parent
1b3f6 20 70 61 67 65 20 69 6e 20 74 68 65 20 62 74 72   page in the btr
1b3f7 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ee..*/.#define P
1b3f8 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 31  TRMAP_ROOTPAGE 1
1b3f9 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f  .#define PTRMAP_
1b3fa 46 52 45 45 50 41 47 45 20 32 0a 23 64 65 66 69  FREEPAGE 2.#defi
1b3fb 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  ne PTRMAP_OVERFL
1b3fc 4f 57 31 20 33 0a 23 64 65 66 69 6e 65 20 50 54  OW1 3.#define PT
1b3fd 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 34  RMAP_OVERFLOW2 4
1b3fe 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f  .#define PTRMAP_
1b3ff 42 54 52 45 45 20 35 0a 0a 2f 2a 20 41 20 62 75  BTREE 5../* A bu
1b400 6e 63 68 20 6f 66 20 61 73 73 65 72 74 28 29 20  nch of assert() 
1b401 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 63 68  statements to ch
1b402 65 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74  eck the transact
1b403 69 6f 6e 20 73 74 61 74 65 20 76 61 72 69 61 62  ion state variab
1b404 6c 65 73 0a 2a 2a 20 6f 66 20 68 61 6e 64 6c 65  les.** of handle
1b405 20 70 20 28 74 79 70 65 20 42 74 72 65 65 2a 29   p (type Btree*)
1b406 20 61 72 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20   are internally 
1b407 63 6f 6e 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 23  consistent..*/.#
1b408 64 65 66 69 6e 65 20 62 74 72 65 65 49 6e 74 65  define btreeInte
1b409 67 72 69 74 79 28 70 29 20 5c 0a 20 20 61 73 73  grity(p) \.  ass
1b40a 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54  ert( p->pBt->inT
1b40b 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54 52 41 4e  ransaction!=TRAN
1b40c 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d 3e 70 42 74  S_NONE || p->pBt
1b40d 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d  ->nTransaction==
1b40e 30 20 29 3b 20 5c 0a 20 20 61 73 73 65 72 74 28  0 ); \.  assert(
1b40f 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73   p->pBt->inTrans
1b410 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69 6e 54 72 61  action>=p->inTra
1b411 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a 2a 2a 20 54  ns ); .../*.** T
1b412 68 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20  he ISAUTOVACUUM 
1b413 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 77 69  macro is used wi
1b414 74 68 69 6e 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e  thin balance_non
1b415 72 6f 6f 74 28 29 20 74 6f 20 64 65 74 65 72 6d  root() to determ
1b416 69 6e 65 0a 2a 2a 20 69 66 20 74 68 65 20 64 61  ine.** if the da
1b417 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20  tabase supports 
1b418 61 75 74 6f 2d 76 61 63 75 75 6d 20 6f 72 20 6e  auto-vacuum or n
1b419 6f 74 2e 20 42 65 63 61 75 73 65 20 69 74 20 69  ot. Because it i
1b41a 73 20 75 73 65 64 0a 2a 2a 20 77 69 74 68 69 6e  s used.** within
1b41b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
1b41c 68 61 74 20 69 73 20 61 6e 20 61 72 67 75 6d 65  hat is an argume
1b41d 6e 74 20 74 6f 20 61 6e 6f 74 68 65 72 20 6d 61  nt to another ma
1b41e 63 72 6f 20 0a 2a 2a 20 28 73 71 6c 69 74 65 4d  cro .** (sqliteM
1b41f 61 6c 6c 6f 63 52 61 77 29 2c 20 69 74 20 69 73  allocRaw), it is
1b420 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
1b421 20 75 73 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c   use conditional
1b422 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e 0a 2a 2a   compilation..**
1b423 20 53 6f 2c 20 74 68 69 73 20 6d 61 63 72 6f 20   So, this macro 
1b424 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73 74 65  is defined inste
1b425 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ad..*/.#ifndef S
1b426 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
1b427 41 43 55 55 4d 0a 23 64 65 66 69 6e 65 20 49 53  ACUUM.#define IS
1b428 41 55 54 4f 56 41 43 55 55 4d 20 28 70 42 74 2d  AUTOVACUUM (pBt-
1b429 3e 61 75 74 6f 56 61 63 75 75 6d 29 0a 23 65 6c  >autoVacuum).#el
1b42a 73 65 0a 23 64 65 66 69 6e 65 20 49 53 41 55 54  se.#define ISAUT
1b42b 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66  OVACUUM 0.#endif
1b42c 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74  .../*.** This st
1b42d 72 75 63 74 75 72 65 20 69 73 20 70 61 73 73 65  ructure is passe
1b42e 64 20 61 72 6f 75 6e 64 20 74 68 72 6f 75 67 68  d around through
1b42f 20 61 6c 6c 20 74 68 65 20 73 61 6e 69 74 79 20   all the sanity 
1b430 63 68 65 63 6b 69 6e 67 20 72 6f 75 74 69 6e 65  checking routine
1b431 73 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f  s.** in order to
1b432 20 6b 65 65 70 20 74 72 61 63 6b 20 6f 66 20 73   keep track of s
1b433 6f 6d 65 20 67 6c 6f 62 61 6c 20 73 74 61 74 65  ome global state
1b434 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a   information..**
1b435 0a 2a 2a 20 54 68 65 20 61 52 65 66 5b 5d 20 61  .** The aRef[] a
1b436 72 72 61 79 20 69 73 20 61 6c 6c 6f 63 61 74 65  rray is allocate
1b437 64 20 73 6f 20 74 68 61 74 20 74 68 65 72 65 20  d so that there 
1b438 69 73 20 31 20 62 69 74 20 66 6f 72 20 65 61 63  is 1 bit for eac
1b439 68 20 70 61 67 65 20 69 6e 0a 2a 2a 20 74 68 65  h page in.** the
1b43a 20 64 61 74 61 62 61 73 65 2e 20 41 73 20 74 68   database. As th
1b43b 65 20 69 6e 74 65 67 72 69 74 79 2d 63 68 65 63  e integrity-chec
1b43c 6b 20 70 72 6f 63 65 65 64 73 2c 20 66 6f 72 20  k proceeds, for 
1b43d 65 61 63 68 20 70 61 67 65 20 75 73 65 64 20 69  each page used i
1b43e 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  n.** the databas
1b43f 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  e the correspond
1b440 69 6e 67 20 62 69 74 20 69 73 20 73 65 74 2e 20  ing bit is set. 
1b441 54 68 69 73 20 61 6c 6c 6f 77 73 20 69 6e 74 65  This allows inte
1b442 67 72 69 74 79 2d 63 68 65 63 6b 20 74 6f 20 0a  grity-check to .
1b443 2a 2a 20 64 65 74 65 63 74 20 70 61 67 65 73 20  ** detect pages 
1b444 74 68 61 74 20 61 72 65 20 75 73 65 64 20 74 77  that are used tw
1b445 69 63 65 20 61 6e 64 20 6f 72 70 68 61 6e 65 64  ice and orphaned
1b446 20 70 61 67 65 73 20 28 62 6f 74 68 20 6f 66 20   pages (both of 
1b447 77 68 69 63 68 20 0a 2a 2a 20 69 6e 64 69 63 61  which .** indica
1b448 74 65 20 63 6f 72 72 75 70 74 69 6f 6e 29 2e 0a  te corruption)..
1b449 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
1b44a 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 49 6e  t IntegrityCk In
1b44b 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72 75 63  tegrityCk;.struc
1b44c 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 7b 0a  t IntegrityCk {.
1b44d 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b    BtShared *pBt;
1b44e 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65 65 20      /* The tree 
1b44f 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20 6f 75  being checked ou
1b450 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70 50  t */.  Pager *pP
1b451 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68 65 20  ager;    /* The 
1b452 61 73 73 6f 63 69 61 74 65 64 20 70 61 67 65 72  associated pager
1b453 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73 69 62  .  Also accessib
1b454 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61 67 65  le by pBt->pPage
1b455 72 20 2a 2f 0a 20 20 75 38 20 2a 61 50 67 52 65  r */.  u8 *aPgRe
1b456 66 3b 20 20 20 20 20 20 20 2f 2a 20 31 20 62 69  f;       /* 1 bi
1b457 74 20 70 65 72 20 70 61 67 65 20 69 6e 20 74 68  t per page in th
1b458 65 20 64 62 20 28 73 65 65 20 61 62 6f 76 65 29  e db (see above)
1b459 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65   */.  Pgno nPage
1b45a 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
1b45b 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
1b45c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
1b45d 69 6e 74 20 6d 78 45 72 72 3b 20 20 20 20 20 20  int mxErr;      
1b45e 20 20 2f 2a 20 53 74 6f 70 20 61 63 63 75 6d 75    /* Stop accumu
1b45f 6c 61 74 69 6e 67 20 65 72 72 6f 72 73 20 77 68  lating errors wh
1b460 65 6e 20 74 68 69 73 20 72 65 61 63 68 65 73 20  en this reaches 
1b461 7a 65 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  zero */.  int nE
1b462 72 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  rr;         /* N
1b463 75 6d 62 65 72 20 6f 66 20 6d 65 73 73 61 67 65  umber of message
1b464 73 20 77 72 69 74 74 65 6e 20 74 6f 20 7a 45 72  s written to zEr
1b465 72 4d 73 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20  rMsg so far */. 
1b466 20 69 6e 74 20 6d 61 6c 6c 6f 63 46 61 69 6c 65   int mallocFaile
1b467 64 3b 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 20 61  d; /* A memory a
1b468 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20  llocation error 
1b469 68 61 73 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a  has occurred */.
1b46a 20 20 53 74 72 41 63 63 75 6d 20 65 72 72 4d 73    StrAccum errMs
1b46b 67 3b 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74  g;  /* Accumulat
1b46c 65 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73  e the error mess
1b46d 61 67 65 20 74 65 78 74 20 68 65 72 65 20 2a 2f  age text here */
1b46e 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69  .};../*.** Routi
1b46f 6e 65 73 20 74 6f 20 72 65 61 64 20 6f 72 20 77  nes to read or w
1b470 72 69 74 65 20 61 20 74 77 6f 2d 20 61 6e 64 20  rite a two- and 
1b471 66 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e  four-byte big-en
1b472 64 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c  dian integer val
1b473 75 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ues..*/.#define 
1b474 67 65 74 32 62 79 74 65 28 78 29 20 20 20 28 28  get2byte(x)   ((
1b475 78 29 5b 30 5d 3c 3c 38 20 7c 20 28 78 29 5b 31  x)[0]<<8 | (x)[1
1b476 5d 29 0a 23 64 65 66 69 6e 65 20 70 75 74 32 62  ]).#define put2b
1b477 79 74 65 28 70 2c 76 29 20 28 28 70 29 5b 30 5d  yte(p,v) ((p)[0]
1b478 20 3d 20 28 75 38 29 28 28 76 29 3e 3e 38 29 2c   = (u8)((v)>>8),
1b479 20 28 70 29 5b 31 5d 20 3d 20 28 75 38 29 28 76   (p)[1] = (u8)(v
1b47a 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74 34 62  )).#define get4b
1b47b 79 74 65 20 73 71 6c 69 74 65 33 47 65 74 34 62  yte sqlite3Get4b
1b47c 79 74 65 0a 23 64 65 66 69 6e 65 20 70 75 74 34  yte.#define put4
1b47d 62 79 74 65 20 73 71 6c 69 74 65 33 50 75 74 34  byte sqlite3Put4
1b47e 62 79 74 65 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  byte../*********
1b47f 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72  ***** End of btr
1b480 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  eeInt.h ********
1b481 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b482 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b483 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1b484 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
1b485 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
1b486 66 66 20 69 6e 20 62 74 6d 75 74 65 78 2e 63 20  ff in btmutex.c 
1b487 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b488 2a 2a 2a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ****/.#ifndef SQ
1b489 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
1b48a 5f 43 41 43 48 45 0a 23 69 66 20 53 51 4c 49 54  _CACHE.#if SQLIT
1b48b 45 5f 54 48 52 45 41 44 53 41 46 45 0a 0a 2f 2a  E_THREADSAFE../*
1b48c 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 42  .** Obtain the B
1b48d 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 61 73  tShared mutex as
1b48e 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 42 2d  sociated with B-
1b48f 54 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 41  Tree handle p. A
1b490 6c 73 6f 2c 0a 2a 2a 20 73 65 74 20 42 74 53 68  lso,.** set BtSh
1b491 61 72 65 64 2e 64 62 20 74 6f 20 74 68 65 20 64  ared.db to the d
1b492 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 61  atabase handle a
1b493 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70  ssociated with p
1b494 20 61 6e 64 20 74 68 65 0a 2a 2a 20 70 2d 3e 6c   and the.** p->l
1b495 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 20 74 6f  ocked boolean to
1b496 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   true..*/.static
1b497 20 76 6f 69 64 20 6c 6f 63 6b 42 74 72 65 65 4d   void lockBtreeM
1b498 75 74 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a  utex(Btree *p){.
1b499 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63    assert( p->loc
1b49a 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  ked==0 );.  asse
1b49b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1b49c 78 5f 6e 6f 74 68 65 6c 64 28 70 2d 3e 70 42 74  x_notheld(p->pBt
1b49d 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
1b49e 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1b49f 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e  tex_held(p->db->
1b4a0 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 73 71 6c  mutex) );..  sql
1b4a1 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1b4a2 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b  (p->pBt->mutex);
1b4a3 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20  .  p->pBt->db = 
1b4a4 70 2d 3e 64 62 3b 0a 20 20 70 2d 3e 6c 6f 63 6b  p->db;.  p->lock
1b4a5 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ed = 1;.}../*.**
1b4a6 20 52 65 6c 65 61 73 65 20 74 68 65 20 42 74 53   Release the BtS
1b4a7 68 61 72 65 64 20 6d 75 74 65 78 20 61 73 73 6f  hared mutex asso
1b4a8 63 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 72  ciated with B-Tr
1b4a9 65 65 20 68 61 6e 64 6c 65 20 70 20 61 6e 64 0a  ee handle p and.
1b4aa 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 70 2d 3e  ** clear the p->
1b4ab 6c 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 2e 0a  locked boolean..
1b4ac 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  */.static void u
1b4ad 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28  nlockBtreeMutex(
1b4ae 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53  Btree *p){.  BtS
1b4af 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
1b4b0 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70  pBt;.  assert( p
1b4b1 2d 3e 6c 6f 63 6b 65 64 3d 3d 31 20 29 3b 0a 20  ->locked==1 );. 
1b4b2 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1b4b3 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
1b4b4 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
1b4b5 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1b4b6 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
1b4b7 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
1b4b8 74 28 20 70 2d 3e 64 62 3d 3d 70 42 74 2d 3e 64  t( p->db==pBt->d
1b4b9 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  b );..  sqlite3_
1b4ba 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 42 74 2d  mutex_leave(pBt-
1b4bb 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f  >mutex);.  p->lo
1b4bc 63 6b 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  cked = 0;.}../*.
1b4bd 2a 2a 20 45 6e 74 65 72 20 61 20 6d 75 74 65 78  ** Enter a mutex
1b4be 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 42 54   on the given BT
1b4bf 72 65 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  ree object..**.*
1b4c0 2a 20 49 66 20 74 68 65 20 6f 62 6a 65 63 74 20  * If the object 
1b4c1 69 73 20 6e 6f 74 20 73 68 61 72 61 62 6c 65 2c  is not sharable,
1b4c2 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 20 69   then no mutex i
1b4c3 73 20 65 76 65 72 20 72 65 71 75 69 72 65 64 0a  s ever required.
1b4c4 2a 2a 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74  ** and this rout
1b4c5 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20  ine is a no-op. 
1b4c6 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   The underlying 
1b4c7 6d 75 74 65 78 20 69 73 20 6e 6f 6e 2d 72 65 63  mutex is non-rec
1b4c8 75 72 73 69 76 65 2e 0a 2a 2a 20 42 75 74 20 77  ursive..** But w
1b4c9 65 20 6b 65 65 70 20 61 20 72 65 66 65 72 65 6e  e keep a referen
1b4ca 63 65 20 63 6f 75 6e 74 20 69 6e 20 42 74 72 65  ce count in Btre
1b4cb 65 2e 77 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20  e.wantToLock so 
1b4cc 74 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20  the behavior.** 
1b4cd 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  of this interfac
1b4ce 65 20 69 73 20 72 65 63 75 72 73 69 76 65 2e 0a  e is recursive..
1b4cf 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 64  **.** To avoid d
1b4d0 65 61 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69 70  eadlocks, multip
1b4d1 6c 65 20 42 74 72 65 65 73 20 61 72 65 20 6c 6f  le Btrees are lo
1b4d2 63 6b 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65  cked in the same
1b4d3 20 6f 72 64 65 72 0a 2a 2a 20 62 79 20 61 6c 6c   order.** by all
1b4d4 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1b4d5 74 69 6f 6e 73 2e 20 20 54 68 65 20 70 2d 3e 70  tions.  The p->p
1b4d6 4e 65 78 74 20 69 73 20 61 20 6c 69 73 74 20 6f  Next is a list o
1b4d7 66 20 6f 74 68 65 72 0a 2a 2a 20 42 74 72 65 65  f other.** Btree
1b4d8 73 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74  s belonging to t
1b4d9 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
1b4da 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74   connection as t
1b4db 68 65 20 70 20 42 74 72 65 65 0a 2a 2a 20 77 68  he p Btree.** wh
1b4dc 69 63 68 20 6e 65 65 64 20 74 6f 20 62 65 20 6c  ich need to be l
1b4dd 6f 63 6b 65 64 20 61 66 74 65 72 20 70 2e 20 20  ocked after p.  
1b4de 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 67 65 74  If we cannot get
1b4df 20 61 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c   a lock on.** p,
1b4e0 20 74 68 65 6e 20 66 69 72 73 74 20 75 6e 6c 6f   then first unlo
1b4e1 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74  ck all of the ot
1b4e2 68 65 72 73 20 6f 6e 20 70 2d 3e 70 4e 65 78 74  hers on p->pNext
1b4e3 2c 20 74 68 65 6e 20 77 61 69 74 0a 2a 2a 20 66  , then wait.** f
1b4e4 6f 72 20 74 68 65 20 6c 6f 63 6b 20 74 6f 20 62  or the lock to b
1b4e5 65 63 6f 6d 65 20 61 76 61 69 6c 61 62 6c 65 20  ecome available 
1b4e6 6f 6e 20 70 2c 20 74 68 65 6e 20 72 65 6c 6f 63  on p, then reloc
1b4e7 6b 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20  k all of the.** 
1b4e8 73 75 62 73 65 71 75 65 6e 74 20 42 74 72 65 65  subsequent Btree
1b4e9 73 20 74 68 61 74 20 64 65 73 69 72 65 20 61 20  s that desire a 
1b4ea 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lock..*/.SQLITE_
1b4eb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1b4ec 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42  ite3BtreeEnter(B
1b4ed 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 72 65  tree *p){.  Btre
1b4ee 65 20 2a 70 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a  e *pLater;..  /*
1b4ef 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69   Some basic sani
1b4f0 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74  ty checking on t
1b4f1 68 65 20 42 74 72 65 65 2e 20 20 54 68 65 20 6c  he Btree.  The l
1b4f2 69 73 74 20 6f 66 20 42 74 72 65 65 73 0a 20 20  ist of Btrees.  
1b4f3 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  ** connected by 
1b4f4 70 4e 65 78 74 20 61 6e 64 20 70 50 72 65 76 20  pNext and pPrev 
1b4f5 73 68 6f 75 6c 64 20 62 65 20 69 6e 20 73 6f 72  should be in sor
1b4f6 74 65 64 20 6f 72 64 65 72 20 62 79 0a 20 20 2a  ted order by.  *
1b4f7 2a 20 42 74 72 65 65 2e 70 42 74 20 76 61 6c 75  * Btree.pBt valu
1b4f8 65 2e 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20  e. All elements 
1b4f9 6f 66 20 74 68 65 20 6c 69 73 74 20 73 68 6f 75  of the list shou
1b4fa 6c 64 20 62 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a  ld belong to.  *
1b4fb 2a 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65  * the same conne
1b4fc 63 74 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61 72  ction. Only shar
1b4fd 65 64 20 42 74 72 65 65 73 20 61 72 65 20 6f 6e  ed Btrees are on
1b4fe 20 74 68 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 20   the list. */.  
1b4ff 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74  assert( p->pNext
1b500 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d  ==0 || p->pNext-
1b501 3e 70 42 74 3e 70 2d 3e 70 42 74 20 29 3b 0a 20  >pBt>p->pBt );. 
1b502 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65   assert( p->pPre
1b503 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76  v==0 || p->pPrev
1b504 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a  ->pBt<p->pBt );.
1b505 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65    assert( p->pNe
1b506 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78  xt==0 || p->pNex
1b507 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a  t->db==p->db );.
1b508 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72    assert( p->pPr
1b509 65 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65  ev==0 || p->pPre
1b50a 76 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a  v->db==p->db );.
1b50b 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61    assert( p->sha
1b50c 72 61 62 6c 65 20 7c 7c 20 28 70 2d 3e 70 4e 65  rable || (p->pNe
1b50d 78 74 3d 3d 30 20 26 26 20 70 2d 3e 70 50 72 65  xt==0 && p->pPre
1b50e 76 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 43  v==0) );..  /* C
1b50f 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 69 6e 67  heck for locking
1b510 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 2a 2f 0a   consistency */.
1b511 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f    assert( !p->lo
1b512 63 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54  cked || p->wantT
1b513 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73  oLock>0 );.  ass
1b514 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65  ert( p->sharable
1b515 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63   || p->wantToLoc
1b516 6b 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 65  k==0 );..  /* We
1b517 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20   should already 
1b518 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74  hold a lock on t
1b519 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
1b51a 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65  ection */.  asse
1b51b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1b51c 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
1b51d 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 55 6e  tex) );..  /* Un
1b51e 6c 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73  less the databas
1b51f 65 20 69 73 20 73 68 61 72 61 62 6c 65 20 61 6e  e is sharable an
1b520 64 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e  d unlocked, then
1b521 20 42 74 53 68 61 72 65 64 2e 64 62 0a 20 20 2a   BtShared.db.  *
1b522 2a 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79  * should already
1b523 20 62 65 20 73 65 74 20 63 6f 72 72 65 63 74 6c   be set correctl
1b524 79 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  y. */.  assert( 
1b525 28 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26  (p->locked==0 &&
1b526 20 70 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c 7c   p->sharable) ||
1b527 20 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e   p->pBt->db==p->
1b528 64 62 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 2d  db );..  if( !p-
1b529 3e 73 68 61 72 61 62 6c 65 20 29 20 72 65 74 75  >sharable ) retu
1b52a 72 6e 3b 0a 20 20 70 2d 3e 77 61 6e 74 54 6f 4c  rn;.  p->wantToL
1b52b 6f 63 6b 2b 2b 3b 0a 20 20 69 66 28 20 70 2d 3e  ock++;.  if( p->
1b52c 6c 6f 63 6b 65 64 20 29 20 72 65 74 75 72 6e 3b  locked ) return;
1b52d 0a 0a 20 20 2f 2a 20 49 6e 20 6d 6f 73 74 20 63  ..  /* In most c
1b52e 61 73 65 73 2c 20 77 65 20 73 68 6f 75 6c 64 20  ases, we should 
1b52f 62 65 20 61 62 6c 65 20 74 6f 20 61 63 71 75 69  be able to acqui
1b530 72 65 20 74 68 65 20 6c 6f 63 6b 20 77 65 0a 20  re the lock we. 
1b531 20 2a 2a 20 77 61 6e 74 20 77 69 74 68 6f 75 74   ** want without
1b532 20 68 61 76 69 6e 67 20 74 6f 20 67 6f 20 74 68   having to go th
1b533 72 6f 75 67 68 74 20 74 68 65 20 61 73 63 65 6e  rought the ascen
1b534 64 69 6e 67 20 6c 6f 63 6b 0a 20 20 2a 2a 20 70  ding lock.  ** p
1b535 72 6f 63 65 64 75 72 65 20 74 68 61 74 20 66 6f  rocedure that fo
1b536 6c 6c 6f 77 73 2e 20 20 4a 75 73 74 20 62 65 20  llows.  Just be 
1b537 73 75 72 65 20 6e 6f 74 20 74 6f 20 62 6c 6f 63  sure not to bloc
1b538 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  k..  */.  if( sq
1b539 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
1b53a 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3d 3d  p->pBt->mutex)==
1b53b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1b53c 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d   p->pBt->db = p-
1b53d 3e 64 62 3b 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b  >db;.    p->lock
1b53e 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75  ed = 1;.    retu
1b53f 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f  rn;.  }..  /* To
1b540 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2c   avoid deadlock,
1b541 20 66 69 72 73 74 20 72 65 6c 65 61 73 65 20 61   first release a
1b542 6c 6c 20 6c 6f 63 6b 73 20 77 69 74 68 20 61 20  ll locks with a 
1b543 6c 61 72 67 65 72 0a 20 20 2a 2a 20 42 74 53 68  larger.  ** BtSh
1b544 61 72 65 64 20 61 64 64 72 65 73 73 2e 20 20 54  ared address.  T
1b545 68 65 6e 20 61 63 71 75 69 72 65 20 6f 75 72 20  hen acquire our 
1b546 6c 6f 63 6b 2e 20 20 54 68 65 6e 20 72 65 61 63  lock.  Then reac
1b547 71 75 69 72 65 0a 20 20 2a 2a 20 74 68 65 20 6f  quire.  ** the o
1b548 74 68 65 72 20 42 74 53 68 61 72 65 64 20 6c 6f  ther BtShared lo
1b549 63 6b 73 20 74 68 61 74 20 77 65 20 75 73 65 64  cks that we used
1b54a 20 74 6f 20 68 6f 6c 64 20 69 6e 20 61 73 63 65   to hold in asce
1b54b 6e 64 69 6e 67 0a 20 20 2a 2a 20 6f 72 64 65 72  nding.  ** order
1b54c 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 61  ..  */.  for(pLa
1b54d 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c  ter=p->pNext; pL
1b54e 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61  ater; pLater=pLa
1b54f 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ter->pNext){.   
1b550 20 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d   assert( pLater-
1b551 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 20  >sharable );.   
1b552 20 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d   assert( pLater-
1b553 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 4c 61  >pNext==0 || pLa
1b554 74 65 72 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e  ter->pNext->pBt>
1b555 70 4c 61 74 65 72 2d 3e 70 42 74 20 29 3b 0a 20  pLater->pBt );. 
1b556 20 20 20 61 73 73 65 72 74 28 20 21 70 4c 61 74     assert( !pLat
1b557 65 72 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c  er->locked || pL
1b558 61 74 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  ater->wantToLock
1b559 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  >0 );.    if( pL
1b55a 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a  ater->locked ){.
1b55b 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65        unlockBtre
1b55c 65 4d 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a  eMutex(pLater);.
1b55d 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6c 6f 63 6b      }.  }.  lock
1b55e 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20  BtreeMutex(p);. 
1b55f 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70   for(pLater=p->p
1b560 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c  Next; pLater; pL
1b561 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65  ater=pLater->pNe
1b562 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 61  xt){.    if( pLa
1b563 74 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20  ter->wantToLock 
1b564 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72  ){.      lockBtr
1b565 65 65 4d 75 74 65 78 28 70 4c 61 74 65 72 29 3b  eeMutex(pLater);
1b566 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
1b567 0a 2a 2a 20 45 78 69 74 20 74 68 65 20 72 65 63  .** Exit the rec
1b568 75 72 73 69 76 65 20 6d 75 74 65 78 20 6f 6e 20  ursive mutex on 
1b569 61 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49  a Btree..*/.SQLI
1b56a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1b56b 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1b56c 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69  e(Btree *p){.  i
1b56d 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29  f( p->sharable )
1b56e 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  {.    assert( p-
1b56f 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b  >wantToLock>0 );
1b570 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  .    p->wantToLo
1b571 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d  ck--;.    if( p-
1b572 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29  >wantToLock==0 )
1b573 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74  {.      unlockBt
1b574 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20  reeMutex(p);.   
1b575 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65   }.  }.}..#ifnde
1b576 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52  f NDEBUG./*.** R
1b577 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68  eturn true if th
1b578 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78  e BtShared mutex
1b579 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20   is held on the 
1b57a 62 74 72 65 65 2c 20 6f 72 20 69 66 20 74 68 65  btree, or if the
1b57b 0a 2a 2a 20 42 2d 54 72 65 65 20 69 73 20 6e 6f  .** B-Tree is no
1b57c 74 20 6d 61 72 6b 65 64 20 61 73 20 73 68 61 72  t marked as shar
1b57d 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  able..**.** This
1b57e 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
1b57f 20 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69   only from withi
1b580 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  n assert() state
1b581 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ments..*/.SQLITE
1b582 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1b583 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
1b584 74 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  tex(Btree *p){. 
1b585 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72   assert( p->shar
1b586 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f  able==0 || p->lo
1b587 63 6b 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 77 61  cked==0 || p->wa
1b588 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20  ntToLock>0 );.  
1b589 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61  assert( p->shara
1b58a 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63  ble==0 || p->loc
1b58b 6b 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 3d  ked==0 || p->db=
1b58c 3d 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 20  =p->pBt->db );. 
1b58d 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72   assert( p->shar
1b58e 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f  able==0 || p->lo
1b58f 63 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  cked==0 || sqlit
1b590 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
1b591 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
1b592 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61    assert( p->sha
1b593 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c  rable==0 || p->l
1b594 6f 63 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69  ocked==0 || sqli
1b595 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1b596 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
1b597 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 73 68  .  return (p->sh
1b598 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e  arable==0 || p->
1b599 6c 6f 63 6b 65 64 29 3b 0a 7d 0a 23 65 6e 64 69  locked);.}.#endi
1b59a 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  f...#ifndef SQLI
1b59b 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
1b59c 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64  ./*.** Enter and
1b59d 20 6c 65 61 76 65 20 61 20 6d 75 74 65 78 20 6f   leave a mutex o
1b59e 6e 20 61 20 42 74 72 65 65 20 67 69 76 65 6e 20  n a Btree given 
1b59f 61 20 63 75 72 73 6f 72 20 6f 77 6e 65 64 20 62  a cursor owned b
1b5a0 79 20 74 68 61 74 0a 2a 2a 20 42 74 72 65 65 2e  y that.** Btree.
1b5a1 20 20 54 68 65 73 65 20 65 6e 74 72 79 20 70 6f    These entry po
1b5a2 69 6e 74 73 20 61 72 65 20 75 73 65 64 20 62 79  ints are used by
1b5a3 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f   incremental I/O
1b5a4 20 61 6e 64 20 63 61 6e 20 62 65 0a 2a 2a 20 6f   and can be.** o
1b5a5 6d 69 74 74 65 64 20 69 66 20 74 68 61 74 20 6d  mitted if that m
1b5a6 6f 64 75 6c 65 20 69 73 20 6e 6f 74 20 75 73 65  odule is not use
1b5a7 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1b5a8 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1b5a9 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f  3BtreeEnterCurso
1b5aa 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  r(BtCursor *pCur
1b5ab 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ){.  sqlite3Btre
1b5ac 65 45 6e 74 65 72 28 70 43 75 72 2d 3e 70 42 74  eEnter(pCur->pBt
1b5ad 72 65 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  ree);.}.SQLITE_P
1b5ae 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1b5af 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72  te3BtreeLeaveCur
1b5b0 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43  sor(BtCursor *pC
1b5b1 75 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74  ur){.  sqlite3Bt
1b5b2 72 65 65 4c 65 61 76 65 28 70 43 75 72 2d 3e 70  reeLeave(pCur->p
1b5b3 42 74 72 65 65 29 3b 0a 7d 0a 23 65 6e 64 69 66  Btree);.}.#endif
1b5b4 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1b5b5 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f 2a  INCRBLOB */.../*
1b5b6 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75  .** Enter the mu
1b5b7 74 65 78 20 6f 6e 20 65 76 65 72 79 20 42 74 72  tex on every Btr
1b5b8 65 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ee associated wi
1b5b9 74 68 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a  th a database.**
1b5ba 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68   connection.  Th
1b5bb 69 73 20 69 73 20 6e 65 65 64 65 64 20 28 66 6f  is is needed (fo
1b5bc 72 20 65 78 61 6d 70 6c 65 29 20 70 72 69 6f 72  r example) prior
1b5bd 20 74 6f 20 70 61 72 73 69 6e 67 0a 2a 2a 20 61   to parsing.** a
1b5be 20 73 74 61 74 65 6d 65 6e 74 20 73 69 6e 63 65   statement since
1b5bf 20 77 65 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70   we will be comp
1b5c0 61 72 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20  aring table and 
1b5c1 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a 20  column names.** 
1b5c2 61 67 61 69 6e 73 74 20 61 6c 6c 20 73 63 68 65  against all sche
1b5c3 6d 61 73 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f  mas and we do no
1b5c4 74 20 77 61 6e 74 20 74 68 6f 73 65 20 73 63 68  t want those sch
1b5c5 65 6d 61 73 20 62 65 69 6e 67 0a 2a 2a 20 72 65  emas being.** re
1b5c6 73 65 74 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64  set out from und
1b5c7 65 72 20 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  er us..**.** The
1b5c8 72 65 20 69 73 20 61 20 63 6f 72 72 65 73 70 6f  re is a correspo
1b5c9 6e 64 69 6e 67 20 6c 65 61 76 65 2d 61 6c 6c 20  nding leave-all 
1b5ca 70 72 6f 63 65 64 75 72 65 73 2e 0a 2a 2a 0a 2a  procedures..**.*
1b5cb 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65  * Enter the mute
1b5cc 78 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67  xes in accending
1b5cd 20 6f 72 64 65 72 20 62 79 20 42 74 53 68 61 72   order by BtShar
1b5ce 65 64 20 70 6f 69 6e 74 65 72 20 61 64 64 72 65  ed pointer addre
1b5cf 73 73 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 74  ss.** to avoid t
1b5d0 68 65 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f  he possibility o
1b5d1 66 20 64 65 61 64 6c 6f 63 6b 20 77 68 65 6e 20  f deadlock when 
1b5d2 74 77 6f 20 74 68 72 65 61 64 73 20 77 69 74 68  two threads with
1b5d3 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20  .** two or more 
1b5d4 62 74 72 65 65 73 20 69 6e 20 63 6f 6d 6d 6f 6e  btrees in common
1b5d5 20 62 6f 74 68 20 74 72 79 20 74 6f 20 6c 6f 63   both try to loc
1b5d6 6b 20 61 6c 6c 20 74 68 65 69 72 20 62 74 72 65  k all their btre
1b5d7 65 73 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 6d  es.** at the sam
1b5d8 65 20 69 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51  e instant..*/.SQ
1b5d9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1b5da 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  d sqlite3BtreeEn
1b5db 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a  terAll(sqlite3 *
1b5dc 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  db){.  int i;.  
1b5dd 42 74 72 65 65 20 2a 70 3b 0a 20 20 61 73 73 65  Btree *p;.  asse
1b5de 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1b5df 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
1b5e0 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ) );.  for(i=0; 
1b5e1 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
1b5e2 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62  .    p = db->aDb
1b5e3 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  [i].pBt;.    if(
1b5e4 20 70 20 29 20 73 71 6c 69 74 65 33 42 74 72 65   p ) sqlite3Btre
1b5e5 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 7d 0a 7d  eEnter(p);.  }.}
1b5e6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1b5e7 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
1b5e8 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65  eLeaveAll(sqlite
1b5e9 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b  3 *db){.  int i;
1b5ea 0a 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 61  .  Btree *p;.  a
1b5eb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1b5ec 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
1b5ed 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d  tex) );.  for(i=
1b5ee 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
1b5ef 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e  +){.    p = db->
1b5f0 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
1b5f1 69 66 28 20 70 20 29 20 73 71 6c 69 74 65 33 42  if( p ) sqlite3B
1b5f2 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
1b5f3 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  }.}../*.** Retur
1b5f4 6e 20 74 72 75 65 20 69 66 20 61 20 70 61 72 74  n true if a part
1b5f5 69 63 75 6c 61 72 20 42 74 72 65 65 20 72 65 71  icular Btree req
1b5f6 75 69 72 65 73 20 61 20 6c 6f 63 6b 2e 20 20 52  uires a lock.  R
1b5f7 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 0a 2a  eturn FALSE if.*
1b5f8 2a 20 6e 6f 20 6c 6f 63 6b 20 69 73 20 65 76 65  * no lock is eve
1b5f9 72 20 72 65 71 75 69 72 65 64 20 73 69 6e 63 65  r required since
1b5fa 20 69 74 20 69 73 20 6e 6f 74 20 73 68 61 72 61   it is not shara
1b5fb 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
1b5fc 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1b5fd 65 33 42 74 72 65 65 53 68 61 72 61 62 6c 65 28  e3BtreeSharable(
1b5fe 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 74  Btree *p){.  ret
1b5ff 75 72 6e 20 70 2d 3e 73 68 61 72 61 62 6c 65 3b  urn p->sharable;
1b600 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  .}..#ifndef NDEB
1b601 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  UG./*.** Return 
1b602 74 72 75 65 20 69 66 20 74 68 65 20 63 75 72 72  true if the curr
1b603 65 6e 74 20 74 68 72 65 61 64 20 68 6f 6c 64 73  ent thread holds
1b604 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
1b605 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65  nnection.** mute
1b606 78 20 61 6e 64 20 61 6c 6c 20 72 65 71 75 69 72  x and all requir
1b607 65 64 20 42 74 53 68 61 72 65 64 20 6d 75 74 65  ed BtShared mute
1b608 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  xes..**.** This 
1b609 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
1b60a 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20  inside assert() 
1b60b 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e  statements only.
1b60c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1b60d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1b60e 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
1b60f 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  es(sqlite3 *db){
1b610 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
1b611 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68  !sqlite3_mutex_h
1b612 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
1b613 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
1b614 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
1b615 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
1b616 20 20 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20      Btree *p;.  
1b617 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d    p = db->aDb[i]
1b618 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20  .pBt;.    if( p 
1b619 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 26  && p->sharable &
1b61a 26 0a 20 20 20 20 20 20 20 20 20 28 70 2d 3e 77  &.         (p->w
1b61b 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20  antToLock==0 || 
1b61c 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68  !sqlite3_mutex_h
1b61d 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65  eld(p->pBt->mute
1b61e 78 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  x)) ){.      ret
1b61f 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 0;.    }.  }
1b620 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23  .  return 1;.}.#
1b621 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20  endif /* NDEBUG 
1b622 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  */..#ifndef NDEB
1b623 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  UG./*.** Return 
1b624 74 72 75 65 20 69 66 20 74 68 65 20 63 6f 72 72  true if the corr
1b625 65 63 74 20 6d 75 74 65 78 65 73 20 61 72 65 20  ect mutexes are 
1b626 68 65 6c 64 20 66 6f 72 20 61 63 63 65 73 73 69  held for accessi
1b627 6e 67 20 74 68 65 0a 2a 2a 20 64 62 2d 3e 61 44  ng the.** db->aD
1b628 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 20 73  b[iDb].pSchema s
1b629 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 6d  tructure.  The m
1b62a 75 74 65 78 65 73 20 72 65 71 75 69 72 65 64 20  utexes required 
1b62b 66 6f 72 20 73 63 68 65 6d 61 0a 2a 2a 20 61 63  for schema.** ac
1b62c 63 65 73 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20  cess are:.**.** 
1b62d 20 20 28 31 29 20 54 68 65 20 6d 75 74 65 78 20    (1) The mutex 
1b62e 6f 6e 20 64 62 0a 2a 2a 20 20 20 28 32 29 20 69  on db.**   (2) i
1b62f 66 20 69 44 62 21 3d 31 2c 20 74 68 65 6e 20 74  f iDb!=1, then t
1b630 68 65 20 6d 75 74 65 78 20 6f 6e 20 64 62 2d 3e  he mutex on db->
1b631 61 44 62 5b 69 44 62 5d 2e 70 42 74 2e 0a 2a 2a  aDb[iDb].pBt..**
1b632 0a 2a 2a 20 49 66 20 70 53 63 68 65 6d 61 20 69  .** If pSchema i
1b633 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
1b634 20 69 44 62 20 69 73 20 63 6f 6d 70 75 74 65 64   iDb is computed
1b635 20 66 72 6f 6d 20 70 53 63 68 65 6d 61 20 61 6e   from pSchema an
1b636 64 0a 2a 2a 20 64 62 20 75 73 69 6e 67 20 73 71  d.** db using sq
1b637 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
1b638 65 78 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ex()..*/.SQLITE_
1b639 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1b63a 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65  te3SchemaMutexHe
1b63b 6c 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ld(sqlite3 *db, 
1b63c 69 6e 74 20 69 44 62 2c 20 53 63 68 65 6d 61 20  int iDb, Schema 
1b63d 2a 70 53 63 68 65 6d 61 29 7b 0a 20 20 42 74 72  *pSchema){.  Btr
1b63e 65 65 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  ee *p;.  assert(
1b63f 20 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20   db!=0 );.  if( 
1b640 70 53 63 68 65 6d 61 20 29 20 69 44 62 20 3d 20  pSchema ) iDb = 
1b641 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1b642 6e 64 65 78 28 64 62 2c 20 70 53 63 68 65 6d 61  ndex(db, pSchema
1b643 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  );.  assert( iDb
1b644 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
1b645 44 62 20 29 3b 0a 20 20 69 66 28 20 21 73 71 6c  Db );.  if( !sql
1b646 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1b647 64 62 2d 3e 6d 75 74 65 78 29 20 29 20 72 65 74  db->mutex) ) ret
1b648 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 69 44 62  urn 0;.  if( iDb
1b649 3d 3d 31 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ==1 ) return 1;.
1b64a 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44    p = db->aDb[iD
1b64b 62 5d 2e 70 42 74 3b 0a 20 20 61 73 73 65 72 74  b].pBt;.  assert
1b64c 28 20 70 21 3d 30 20 29 3b 0a 20 20 72 65 74 75  ( p!=0 );.  retu
1b64d 72 6e 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d  rn p->sharable==
1b64e 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d  0 || p->locked==
1b64f 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e  1;.}.#endif /* N
1b650 44 45 42 55 47 20 2a 2f 0a 0a 23 65 6c 73 65 20  DEBUG */..#else 
1b651 2f 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  /* SQLITE_THREAD
1b652 53 41 46 45 3e 30 20 61 62 6f 76 65 2e 20 20 53  SAFE>0 above.  S
1b653 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
1b654 3d 3d 30 20 62 65 6c 6f 77 20 2a 2f 0a 2f 2a 0a  ==0 below */./*.
1b655 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
1b656 20 61 72 65 20 73 70 65 63 69 61 6c 20 63 61 73   are special cas
1b657 65 73 20 66 6f 72 20 6d 75 74 65 78 20 65 6e 74  es for mutex ent
1b658 65 72 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20  er routines for 
1b659 75 73 65 0a 2a 2a 20 69 6e 20 73 69 6e 67 6c 65  use.** in single
1b65a 20 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63   threaded applic
1b65b 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20  ations that use 
1b65c 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 45  shared cache.  E
1b65d 78 63 65 70 74 20 66 6f 72 0a 2a 2a 20 74 68 65  xcept for.** the
1b65e 73 65 20 74 77 6f 20 72 6f 75 74 69 6e 65 73 2c  se two routines,
1b65f 20 61 6c 6c 20 6d 75 74 65 78 20 6f 70 65 72 61   all mutex opera
1b660 74 69 6f 6e 73 20 61 72 65 20 6e 6f 2d 6f 70 73  tions are no-ops
1b661 20 69 6e 20 74 68 61 74 20 63 61 73 65 20 61 6e   in that case an
1b662 64 0a 2a 2a 20 61 72 65 20 6e 75 6c 6c 20 23 64  d.** are null #d
1b663 65 66 69 6e 65 73 20 69 6e 20 62 74 72 65 65 2e  efines in btree.
1b664 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 68 61 72  h..**.** If shar
1b665 65 64 20 63 61 63 68 65 20 69 73 20 64 69 73 61  ed cache is disa
1b666 62 6c 65 64 2c 20 74 68 65 6e 20 61 6c 6c 20 62  bled, then all b
1b667 74 72 65 65 20 6d 75 74 65 78 20 72 6f 75 74 69  tree mutex routi
1b668 6e 65 73 2c 20 69 6e 63 6c 75 64 69 6e 67 0a 2a  nes, including.*
1b669 2a 20 74 68 65 20 6f 6e 65 73 20 62 65 6c 6f 77  * the ones below
1b66a 2c 20 61 72 65 20 6e 6f 2d 6f 70 73 20 61 6e 64  , are no-ops and
1b66b 20 61 72 65 20 6e 75 6c 6c 20 23 64 65 66 69 6e   are null #defin
1b66c 65 73 20 69 6e 20 62 74 72 65 65 2e 68 2e 0a 2a  es in btree.h..*
1b66d 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
1b66e 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
1b66f 72 65 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a  reeEnter(Btree *
1b670 70 29 7b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62  p){.  p->pBt->db
1b671 20 3d 20 70 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49   = p->db;.}.SQLI
1b672 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1b673 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1b674 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62  rAll(sqlite3 *db
1b675 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  ){.  int i;.  fo
1b676 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62  r(i=0; i<db->nDb
1b677 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65  ; i++){.    Btre
1b678 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69  e *p = db->aDb[i
1b679 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70  ].pBt;.    if( p
1b67a 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 42 74   ){.      p->pBt
1b67b 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20  ->db = p->db;.  
1b67c 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66    }.  }.}.#endif
1b67d 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 54 48   /* if SQLITE_TH
1b67e 52 45 41 44 53 41 46 45 20 2a 2f 0a 23 65 6e 64  READSAFE */.#end
1b67f 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c  if /* ifndef SQL
1b680 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
1b681 43 41 43 48 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  CACHE */../*****
1b682 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
1b683 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a   btmutex.c *****
1b684 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b685 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b686 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
1b687 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
1b688 66 69 6c 65 20 62 74 72 65 65 2e 63 20 2a 2a 2a  file btree.c ***
1b689 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b68a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b68b 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
1b68c 32 30 30 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a  2004 April 6.**.
1b68d 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
1b68e 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
1b68f 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
1b690 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
1b691 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
1b692 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
1b693 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1b694 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1b695 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1b696 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1b697 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1b698 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1b699 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
1b69a 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1b69b 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
1b69c 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
1b69d 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
1b69e 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1b69f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
1b6a3 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65  his file impleme
1b6a4 6e 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28  nts a external (
1b6a5 64 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61  disk-based) data
1b6a6 62 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65  base using BTree
1b6a7 73 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 68 65  s..** See the he
1b6a8 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20  ader comment on 
1b6a9 22 62 74 72 65 65 49 6e 74 2e 68 22 20 66 6f 72  "btreeInt.h" for
1b6aa 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
1b6ab 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c  rmation..** Incl
1b6ac 75 64 69 6e 67 20 61 20 64 65 73 63 72 69 70 74  uding a descript
1b6ad 69 6f 6e 20 6f 66 20 66 69 6c 65 20 66 6f 72 6d  ion of file form
1b6ae 61 74 20 61 6e 64 20 61 6e 20 6f 76 65 72 76 69  at and an overvi
1b6af 65 77 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 2e  ew of operation.
1b6b0 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68  .*/../*.** The h
1b6b1 65 61 64 65 72 20 73 74 72 69 6e 67 20 74 68 61  eader string tha
1b6b2 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65  t appears at the
1b6b3 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76   beginning of ev
1b6b4 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61  ery.** SQLite da
1b6b5 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  tabase..*/.stati
1b6b6 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61  c const char zMa
1b6b7 67 69 63 48 65 61 64 65 72 5b 5d 20 3d 20 53 51  gicHeader[] = SQ
1b6b8 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52  LITE_FILE_HEADER
1b6b9 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69  ;../*.** Set thi
1b6ba 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  s global variabl
1b6bb 65 20 74 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65  e to 1 to enable
1b6bc 20 74 72 61 63 69 6e 67 20 75 73 69 6e 67 20 74   tracing using t
1b6bd 68 65 20 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72  he TRACE.** macr
1b6be 6f 2e 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20  o..*/.#if 0.int 
1b6bf 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 61 63  sqlite3BtreeTrac
1b6c0 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f  e=1;  /* True to
1b6c1 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20   enable tracing 
1b6c2 2a 2f 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43  */.# define TRAC
1b6c3 45 28 58 29 20 20 69 66 28 73 71 6c 69 74 65 33  E(X)  if(sqlite3
1b6c4 42 74 72 65 65 54 72 61 63 65 29 7b 70 72 69 6e  BtreeTrace){prin
1b6c5 74 66 20 58 3b 66 66 6c 75 73 68 28 73 74 64 6f  tf X;fflush(stdo
1b6c6 75 74 29 3b 7d 0a 23 65 6c 73 65 0a 23 20 64 65  ut);}.#else.# de
1b6c7 66 69 6e 65 20 54 52 41 43 45 28 58 29 0a 23 65  fine TRACE(X).#e
1b6c8 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72  ndif../*.** Extr
1b6c9 61 63 74 20 61 20 32 2d 62 79 74 65 20 62 69 67  act a 2-byte big
1b6ca 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20  -endian integer 
1b6cb 66 72 6f 6d 20 61 6e 20 61 72 72 61 79 20 6f 66  from an array of
1b6cc 20 75 6e 73 69 67 6e 65 64 20 62 79 74 65 73 2e   unsigned bytes.
1b6cd 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65 20 76  .** But if the v
1b6ce 61 6c 75 65 20 69 73 20 7a 65 72 6f 2c 20 6d 61  alue is zero, ma
1b6cf 6b 65 20 69 74 20 36 35 35 33 36 2e 0a 2a 2a 0a  ke it 65536..**.
1b6d0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1b6d1 69 73 20 75 73 65 64 20 74 6f 20 65 78 74 72 61  is used to extra
1b6d2 63 74 20 74 68 65 20 22 6f 66 66 73 65 74 20 74  ct the "offset t
1b6d3 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61  o cell content a
1b6d4 72 65 61 22 20 76 61 6c 75 65 0a 2a 2a 20 66 72  rea" value.** fr
1b6d5 6f 6d 20 74 68 65 20 68 65 61 64 65 72 20 6f 66  om the header of
1b6d6 20 61 20 62 74 72 65 65 20 70 61 67 65 2e 20 20   a btree page.  
1b6d7 49 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65  If the page size
1b6d8 20 69 73 20 36 35 35 33 36 20 61 6e 64 20 74 68   is 65536 and th
1b6d9 65 20 70 61 67 65 0a 2a 2a 20 69 73 20 65 6d 70  e page.** is emp
1b6da 74 79 2c 20 74 68 65 20 6f 66 66 73 65 74 20 73  ty, the offset s
1b6db 68 6f 75 6c 64 20 62 65 20 36 35 35 33 36 2c 20  hould be 65536, 
1b6dc 62 75 74 20 74 68 65 20 32 2d 62 79 74 65 20 76  but the 2-byte v
1b6dd 61 6c 75 65 20 73 74 6f 72 65 73 20 7a 65 72 6f  alue stores zero
1b6de 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
1b6df 65 20 6d 61 6b 65 73 20 74 68 65 20 6e 65 63 65  e makes the nece
1b6e0 73 73 61 72 79 20 61 64 6a 75 73 74 6d 65 6e 74  ssary adjustment
1b6e1 20 74 6f 20 36 35 35 33 36 2e 0a 2a 2f 0a 23 64   to 65536..*/.#d
1b6e2 65 66 69 6e 65 20 67 65 74 32 62 79 74 65 4e 6f  efine get2byteNo
1b6e3 74 5a 65 72 6f 28 58 29 20 20 28 28 28 28 28 69  tZero(X)  (((((i
1b6e4 6e 74 29 67 65 74 32 62 79 74 65 28 58 29 29 2d  nt)get2byte(X))-
1b6e5 31 29 26 30 78 66 66 66 66 29 2b 31 29 0a 0a 23  1)&0xffff)+1)..#
1b6e6 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1b6e7 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
1b6e8 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20  /*.** A list of 
1b6e9 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 73  BtShared objects
1b6ea 20 74 68 61 74 20 61 72 65 20 65 6c 69 67 69 62   that are eligib
1b6eb 6c 65 20 66 6f 72 20 70 61 72 74 69 63 69 70 61  le for participa
1b6ec 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61 72 65  tion.** in share
1b6ed 64 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 76  d cache.  This v
1b6ee 61 72 69 61 62 6c 65 20 68 61 73 20 66 69 6c 65  ariable has file
1b6ef 20 73 63 6f 70 65 20 64 75 72 69 6e 67 20 6e 6f   scope during no
1b6f0 72 6d 61 6c 20 62 75 69 6c 64 73 2c 0a 2a 2a 20  rmal builds,.** 
1b6f1 62 75 74 20 74 68 65 20 74 65 73 74 20 68 61 72  but the test har
1b6f2 6e 65 73 73 20 6e 65 65 64 73 20 74 6f 20 61 63  ness needs to ac
1b6f3 63 65 73 73 20 69 74 20 73 6f 20 77 65 20 6d 61  cess it so we ma
1b6f4 6b 65 20 69 74 20 67 6c 6f 62 61 6c 20 66 6f 72  ke it global for
1b6f5 20 0a 2a 2a 20 74 65 73 74 20 62 75 69 6c 64 73   .** test builds
1b6f6 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74  ..**.** Access t
1b6f7 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20  o this variable 
1b6f8 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 20  is protected by 
1b6f9 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
1b6fa 54 49 43 5f 4d 41 53 54 45 52 2e 0a 2a 2f 0a 23  TIC_MASTER..*/.#
1b6fb 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1b6fc 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
1b6fd 20 42 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54   BtShared *SQLIT
1b6fe 45 5f 57 53 44 20 73 71 6c 69 74 65 33 53 68 61  E_WSD sqlite3Sha
1b6ff 72 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30  redCacheList = 0
1b700 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 42  ;.#else.static B
1b701 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f  tShared *SQLITE_
1b702 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 65  WSD sqlite3Share
1b703 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a  dCacheList = 0;.
1b704 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a  #endif.#endif /*
1b705 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
1b706 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69  RED_CACHE */..#i
1b707 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1b708 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f  T_SHARED_CACHE./
1b709 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64  *.** Enable or d
1b70a 69 73 61 62 6c 65 20 74 68 65 20 73 68 61 72 65  isable the share
1b70b 64 20 70 61 67 65 72 20 61 6e 64 20 73 63 68 65  d pager and sche
1b70c 6d 61 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a  ma features..**.
1b70d 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1b70e 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e  has no effect on
1b70f 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61   existing databa
1b710 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a  se connections..
1b711 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 63 61  ** The shared ca
1b712 63 68 65 20 73 65 74 74 69 6e 67 20 65 66 66 65  che setting effe
1b713 63 74 73 20 6f 6e 6c 79 20 66 75 74 75 72 65 20  cts only future 
1b714 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69  calls to.** sqli
1b715 74 65 33 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69  te3_open(), sqli
1b716 74 65 33 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72  te3_open16(), or
1b717 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
1b718 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ()..*/.SQLITE_AP
1b719 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e  I int sqlite3_en
1b71a 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
1b71b 65 28 69 6e 74 20 65 6e 61 62 6c 65 29 7b 0a 20  e(int enable){. 
1b71c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
1b71d 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 65  nfig.sharedCache
1b71e 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65  Enabled = enable
1b71f 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1b720 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  E_OK;.}.#endif..
1b721 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1b722 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
1b723 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20  E.  /*.  ** The 
1b724 66 75 6e 63 74 69 6f 6e 73 20 71 75 65 72 79 53  functions queryS
1b725 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
1b726 6f 63 6b 28 29 2c 20 73 65 74 53 68 61 72 65 64  ock(), setShared
1b727 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29  CacheTableLock()
1b728 2c 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72  ,.  ** and clear
1b729 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61  AllSharedCacheTa
1b72a 62 6c 65 4c 6f 63 6b 73 28 29 0a 20 20 2a 2a 20  bleLocks().  ** 
1b72b 6d 61 6e 69 70 75 6c 61 74 65 20 65 6e 74 72 69  manipulate entri
1b72c 65 73 20 69 6e 20 74 68 65 20 42 74 53 68 61 72  es in the BtShar
1b72d 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20  ed.pLock linked 
1b72e 6c 69 73 74 20 75 73 65 64 20 74 6f 20 73 74 6f  list used to sto
1b72f 72 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63  re.  ** shared-c
1b730 61 63 68 65 20 74 61 62 6c 65 20 6c 65 76 65 6c  ache table level
1b731 20 6c 6f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c   locks. If the l
1b732 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c  ibrary is compil
1b733 65 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a  ed with the.  **
1b734 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 65   shared-cache fe
1b735 61 74 75 72 65 20 64 69 73 61 62 6c 65 64 2c 20  ature disabled, 
1b736 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e  then there is on
1b737 6c 79 20 65 76 65 72 20 6f 6e 65 20 75 73 65 72  ly ever one user
1b738 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 42 74  .  ** of each Bt
1b739 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65  Shared structure
1b73a 20 61 6e 64 20 73 6f 20 74 68 69 73 20 6c 6f 63   and so this loc
1b73b 6b 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65  king is not nece
1b73c 73 73 61 72 79 2e 20 0a 20 20 2a 2a 20 53 6f 20  ssary. .  ** So 
1b73d 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 6b 20  define the lock 
1b73e 72 65 6c 61 74 65 64 20 66 75 6e 63 74 69 6f 6e  related function
1b73f 73 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a  s as no-ops..  *
1b740 2f 0a 20 20 23 64 65 66 69 6e 65 20 71 75 65 72  /.  #define quer
1b741 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  ySharedCacheTabl
1b742 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c  eLock(a,b,c) SQL
1b743 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65  ITE_OK.  #define
1b744 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54   setSharedCacheT
1b745 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20  ableLock(a,b,c) 
1b746 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66  SQLITE_OK.  #def
1b747 69 6e 65 20 63 6c 65 61 72 41 6c 6c 53 68 61 72  ine clearAllShar
1b748 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
1b749 73 28 61 29 0a 20 20 23 64 65 66 69 6e 65 20 64  s(a).  #define d
1b74a 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61 72 65  owngradeAllShare
1b74b 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73  dCacheTableLocks
1b74c 28 61 29 0a 20 20 23 64 65 66 69 6e 65 20 68 61  (a).  #define ha
1b74d 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  sSharedCacheTabl
1b74e 65 4c 6f 63 6b 28 61 2c 62 2c 63 2c 64 29 20 31  eLock(a,b,c,d) 1
1b74f 0a 20 20 23 64 65 66 69 6e 65 20 68 61 73 52 65  .  #define hasRe
1b750 61 64 43 6f 6e 66 6c 69 63 74 73 28 61 2c 20 62  adConflicts(a, b
1b751 29 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ) 0.#endif..#ifn
1b752 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1b753 53 48 41 52 45 44 5f 43 41 43 48 45 0a 0a 23 69  SHARED_CACHE..#i
1b754 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1b755 47 0a 2f 2a 0a 2a 2a 2a 2a 20 54 68 69 73 20 66  G./*.**** This f
1b756 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20  unction is only 
1b757 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  used as part of 
1b758 61 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74  an assert() stat
1b759 65 6d 65 6e 74 2e 20 2a 2a 2a 0a 2a 2a 0a 2a 2a  ement. ***.**.**
1b75a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
1b75b 20 70 42 74 72 65 65 20 68 6f 6c 64 73 20 74 68   pBtree holds th
1b75c 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73  e required locks
1b75d 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74   to read or writ
1b75e 65 20 74 6f 20 74 68 65 20 0a 2a 2a 20 74 61 62  e to the .** tab
1b75f 6c 65 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67  le with root pag
1b760 65 20 69 52 6f 6f 74 2e 20 20 20 52 65 74 75 72  e iRoot.   Retur
1b761 6e 20 31 20 69 66 20 69 74 20 64 6f 65 73 20 61  n 1 if it does a
1b762 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a  nd 0 if not..**.
1b763 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  ** For example, 
1b764 77 68 65 6e 20 77 72 69 74 69 6e 67 20 74 6f 20  when writing to 
1b765 61 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f  a table with roo
1b766 74 2d 70 61 67 65 20 69 52 6f 6f 74 20 76 69 61  t-page iRoot via
1b767 20 0a 2a 2a 20 42 74 72 65 65 20 63 6f 6e 6e 65   .** Btree conne
1b768 63 74 69 6f 6e 20 70 42 74 72 65 65 3a 0a 2a 2a  ction pBtree:.**
1b769 0a 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20 68  .**    assert( h
1b76a 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62  asSharedCacheTab
1b76b 6c 65 4c 6f 63 6b 28 70 42 74 72 65 65 2c 20 69  leLock(pBtree, i
1b76c 52 6f 6f 74 2c 20 30 2c 20 57 52 49 54 45 5f 4c  Root, 0, WRITE_L
1b76d 4f 43 4b 29 20 29 3b 0a 2a 2a 0a 2a 2a 20 57 68  OCK) );.**.** Wh
1b76e 65 6e 20 77 72 69 74 69 6e 67 20 74 6f 20 61 6e  en writing to an
1b76f 20 69 6e 64 65 78 20 74 68 61 74 20 72 65 73 69   index that resi
1b770 64 65 73 20 69 6e 20 61 20 73 68 61 72 61 62 6c  des in a sharabl
1b771 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20  e database, the 
1b772 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c  .** caller shoul
1b773 64 20 68 61 76 65 20 66 69 72 73 74 20 6f 62 74  d have first obt
1b774 61 69 6e 65 64 20 61 20 6c 6f 63 6b 20 73 70 65  ained a lock spe
1b775 63 69 66 79 69 6e 67 20 74 68 65 20 72 6f 6f 74  cifying the root
1b776 20 70 61 67 65 20 6f 66 0a 2a 2a 20 74 68 65 20   page of.** the 
1b777 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61  corresponding ta
1b778 62 6c 65 2e 20 54 68 69 73 20 6d 61 6b 65 73 20  ble. This makes 
1b779 74 68 69 6e 67 73 20 61 20 62 69 74 20 6d 6f 72  things a bit mor
1b77a 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2c 0a 2a  e complicated,.*
1b77b 2a 20 61 73 20 74 68 69 73 20 6d 6f 64 75 6c 65  * as this module
1b77c 20 74 72 65 61 74 73 20 65 61 63 68 20 74 61 62   treats each tab
1b77d 6c 65 20 61 73 20 61 20 73 65 70 61 72 61 74 65  le as a separate
1b77e 20 73 74 72 75 63 74 75 72 65 2e 20 54 6f 20 64   structure. To d
1b77f 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 74 68 65 20  etermine.** the 
1b780 74 61 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64  table correspond
1b781 69 6e 67 20 74 6f 20 74 68 65 20 69 6e 64 65 78  ing to the index
1b782 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 20   being written, 
1b783 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  this.** function
1b784 20 68 61 73 20 74 6f 20 73 65 61 72 63 68 20 74   has to search t
1b785 68 72 6f 75 67 68 20 74 68 65 20 64 61 74 61 62  hrough the datab
1b786 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a  ase schema..**.*
1b787 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 61 20 6c  * Instead of a l
1b788 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ock on the table
1b789 2f 69 6e 64 65 78 20 72 6f 6f 74 65 64 20 61 74  /index rooted at
1b78a 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 74 68 65   page iRoot, the
1b78b 20 63 61 6c 6c 65 72 20 6d 61 79 0a 2a 2a 20 68   caller may.** h
1b78c 6f 6c 64 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  old a write-lock
1b78d 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 20 74   on the schema t
1b78e 61 62 6c 65 20 28 72 6f 6f 74 20 70 61 67 65 20  able (root page 
1b78f 31 29 2e 20 54 68 69 73 20 69 73 20 61 6c 73 6f  1). This is also
1b790 0a 2a 2a 20 61 63 63 65 70 74 61 62 6c 65 2e 0a  .** acceptable..
1b791 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61  */.static int ha
1b792 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  sSharedCacheTabl
1b793 65 4c 6f 63 6b 28 0a 20 20 42 74 72 65 65 20 2a  eLock(.  Btree *
1b794 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 20  pBtree,         
1b795 2f 2a 20 48 61 6e 64 6c 65 20 74 68 61 74 20 6d  /* Handle that m
1b796 75 73 74 20 68 6f 6c 64 20 6c 6f 63 6b 20 2a 2f  ust hold lock */
1b797 0a 20 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 20  .  Pgno iRoot,  
1b798 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f            /* Roo
1b799 74 20 70 61 67 65 20 6f 66 20 62 2d 74 72 65 65  t page of b-tree
1b79a 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 64 65   */.  int isInde
1b79b 78 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  x,           /* 
1b79c 54 72 75 65 20 69 66 20 69 52 6f 6f 74 20 69 73  True if iRoot is
1b79d 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 61 6e 20   the root of an 
1b79e 69 6e 64 65 78 20 62 2d 74 72 65 65 20 2a 2f 0a  index b-tree */.
1b79f 20 20 69 6e 74 20 65 4c 6f 63 6b 54 79 70 65 20    int eLockType 
1b7a0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 71 75           /* Requ
1b7a1 69 72 65 64 20 6c 6f 63 6b 20 74 79 70 65 20 28  ired lock type (
1b7a2 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49  READ_LOCK or WRI
1b7a3 54 45 5f 4c 4f 43 4b 29 20 2a 2f 0a 29 7b 0a 20  TE_LOCK) */.){. 
1b7a4 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
1b7a5 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 42 74   = (Schema *)pBt
1b7a6 72 65 65 2d 3e 70 42 74 2d 3e 70 53 63 68 65 6d  ree->pBt->pSchem
1b7a7 61 3b 0a 20 20 50 67 6e 6f 20 69 54 61 62 20 3d  a;.  Pgno iTab =
1b7a8 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c   0;.  BtLock *pL
1b7a9 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ock;..  /* If th
1b7aa 69 73 20 64 61 74 61 62 61 73 65 20 69 73 20 6e  is database is n
1b7ab 6f 74 20 73 68 61 72 65 61 62 6c 65 2c 20 6f 72  ot shareable, or
1b7ac 20 69 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69   if the client i
1b7ad 73 20 72 65 61 64 69 6e 67 0a 20 20 2a 2a 20 61  s reading.  ** a
1b7ae 6e 64 20 68 61 73 20 74 68 65 20 72 65 61 64 2d  nd has the read-
1b7af 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67  uncommitted flag
1b7b0 20 73 65 74 2c 20 74 68 65 6e 20 6e 6f 20 6c 6f   set, then no lo
1b7b1 63 6b 20 69 73 20 72 65 71 75 69 72 65 64 2e 20  ck is required. 
1b7b2 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  .  ** Return tru
1b7b3 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20  e immediately.. 
1b7b4 20 2a 2f 0a 20 20 69 66 28 20 28 70 42 74 72 65   */.  if( (pBtre
1b7b5 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 29 0a  e->sharable==0).
1b7b6 20 20 20 7c 7c 20 28 65 4c 6f 63 6b 54 79 70 65     || (eLockType
1b7b7 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 26 26 20 28  ==READ_LOCK && (
1b7b8 70 42 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 67  pBtree->db->flag
1b7b9 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55  s & SQLITE_ReadU
1b7ba 6e 63 6f 6d 6d 69 74 74 65 64 29 29 0a 20 20 29  ncommitted)).  )
1b7bb 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  {.    return 1;.
1b7bc 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
1b7bd 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69   client is readi
1b7be 6e 67 20 20 6f 72 20 77 72 69 74 69 6e 67 20 61  ng  or writing a
1b7bf 6e 20 69 6e 64 65 78 20 61 6e 64 20 74 68 65 20  n index and the 
1b7c0 73 63 68 65 6d 61 20 69 73 0a 20 20 2a 2a 20 6e  schema is.  ** n
1b7c1 6f 74 20 6c 6f 61 64 65 64 2c 20 74 68 65 6e 20  ot loaded, then 
1b7c2 69 74 20 69 73 20 74 6f 6f 20 64 69 66 66 69 63  it is too diffic
1b7c3 75 6c 74 20 74 6f 20 61 63 74 75 61 6c 6c 79 20  ult to actually 
1b7c4 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 0a  check to see if.
1b7c5 20 20 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 74    ** the correct
1b7c6 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e   locks are held.
1b7c7 20 20 53 6f 20 64 6f 20 6e 6f 74 20 62 6f 74 68    So do not both
1b7c8 65 72 20 2d 20 6a 75 73 74 20 72 65 74 75 72 6e  er - just return
1b7c9 20 74 72 75 65 2e 0a 20 20 2a 2a 20 54 68 69 73   true..  ** This
1b7ca 20 63 61 73 65 20 64 6f 65 73 20 6e 6f 74 20 63   case does not c
1b7cb 6f 6d 65 20 75 70 20 76 65 72 79 20 6f 66 74 65  ome up very ofte
1b7cc 6e 20 61 6e 79 68 6f 77 2e 0a 20 20 2a 2f 0a 20  n anyhow..  */. 
1b7cd 20 69 66 28 20 69 73 49 6e 64 65 78 20 26 26 20   if( isIndex && 
1b7ce 28 21 70 53 63 68 65 6d 61 20 7c 7c 20 28 70 53  (!pSchema || (pS
1b7cf 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 44 42 5f  chema->flags&DB_
1b7d0 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 3d 3d 30  SchemaLoaded)==0
1b7d1 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
1b7d2 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67  1;.  }..  /* Fig
1b7d3 75 72 65 20 6f 75 74 20 74 68 65 20 72 6f 6f 74  ure out the root
1b7d4 2d 70 61 67 65 20 74 68 61 74 20 74 68 65 20 6c  -page that the l
1b7d5 6f 63 6b 20 73 68 6f 75 6c 64 20 62 65 20 68 65  ock should be he
1b7d6 6c 64 20 6f 6e 2e 20 46 6f 72 20 74 61 62 6c 65  ld on. For table
1b7d7 0a 20 20 2a 2a 20 62 2d 74 72 65 65 73 2c 20 74  .  ** b-trees, t
1b7d8 68 69 73 20 69 73 20 6a 75 73 74 20 74 68 65 20  his is just the 
1b7d9 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65  root page of the
1b7da 20 62 2d 74 72 65 65 20 62 65 69 6e 67 20 72 65   b-tree being re
1b7db 61 64 20 6f 72 0a 20 20 2a 2a 20 77 72 69 74 74  ad or.  ** writt
1b7dc 65 6e 2e 20 46 6f 72 20 69 6e 64 65 78 20 62 2d  en. For index b-
1b7dd 74 72 65 65 73 2c 20 69 74 20 69 73 20 74 68 65  trees, it is the
1b7de 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68   root page of th
1b7df 65 20 61 73 73 6f 63 69 61 74 65 64 0a 20 20 2a  e associated.  *
1b7e0 2a 20 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 69  * table.  */.  i
1b7e1 66 28 20 69 73 49 6e 64 65 78 20 29 7b 0a 20 20  f( isIndex ){.  
1b7e2 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20    HashElem *p;. 
1b7e3 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48     for(p=sqliteH
1b7e4 61 73 68 46 69 72 73 74 28 26 70 53 63 68 65 6d  ashFirst(&pSchem
1b7e5 61 2d 3e 69 64 78 48 61 73 68 29 3b 20 70 3b 20  a->idxHash); p; 
1b7e6 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  p=sqliteHashNext
1b7e7 28 70 29 29 7b 0a 20 20 20 20 20 20 49 6e 64 65  (p)){.      Inde
1b7e8 78 20 2a 70 49 64 78 20 3d 20 28 49 6e 64 65 78  x *pIdx = (Index
1b7e9 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74   *)sqliteHashDat
1b7ea 61 28 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20  a(p);.      if( 
1b7eb 70 49 64 78 2d 3e 74 6e 75 6d 3d 3d 28 69 6e 74  pIdx->tnum==(int
1b7ec 29 69 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20  )iRoot ){.      
1b7ed 20 20 69 54 61 62 20 3d 20 70 49 64 78 2d 3e 70    iTab = pIdx->p
1b7ee 54 61 62 6c 65 2d 3e 74 6e 75 6d 3b 0a 20 20 20  Table->tnum;.   
1b7ef 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
1b7f0 73 65 7b 0a 20 20 20 20 69 54 61 62 20 3d 20 69  se{.    iTab = i
1b7f1 52 6f 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Root;.  }..  /* 
1b7f2 53 65 61 72 63 68 20 66 6f 72 20 74 68 65 20 72  Search for the r
1b7f3 65 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 45 69  equired lock. Ei
1b7f4 74 68 65 72 20 61 20 77 72 69 74 65 2d 6c 6f 63  ther a write-loc
1b7f5 6b 20 6f 6e 20 72 6f 6f 74 2d 70 61 67 65 20 69  k on root-page i
1b7f6 54 61 62 2c 20 61 20 0a 20 20 2a 2a 20 77 72 69  Tab, a .  ** wri
1b7f7 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73  te-lock on the s
1b7f8 63 68 65 6d 61 20 74 61 62 6c 65 2c 20 6f 72 20  chema table, or 
1b7f9 28 69 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69  (if the client i
1b7fa 73 20 72 65 61 64 69 6e 67 29 20 61 0a 20 20 2a  s reading) a.  *
1b7fb 2a 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 69  * read-lock on i
1b7fc 54 61 62 20 77 69 6c 6c 20 73 75 66 66 69 63 65  Tab will suffice
1b7fd 2e 20 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e  . Return 1 if an
1b7fe 79 20 6f 66 20 74 68 65 73 65 20 61 72 65 20 66  y of these are f
1b7ff 6f 75 6e 64 2e 20 20 2a 2f 0a 20 20 66 6f 72 28  ound.  */.  for(
1b800 70 4c 6f 63 6b 3d 70 42 74 72 65 65 2d 3e 70 42  pLock=pBtree->pB
1b801 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b  t->pLock; pLock;
1b802 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e   pLock=pLock->pN
1b803 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c  ext){.    if( pL
1b804 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 42 74  ock->pBtree==pBt
1b805 72 65 65 20 0a 20 20 20 20 20 26 26 20 28 70 4c  ree .     && (pL
1b806 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61  ock->iTable==iTa
1b807 62 20 7c 7c 20 28 70 4c 6f 63 6b 2d 3e 65 4c 6f  b || (pLock->eLo
1b808 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 26  ck==WRITE_LOCK &
1b809 26 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d  & pLock->iTable=
1b80a 3d 31 29 29 0a 20 20 20 20 20 26 26 20 70 4c 6f  =1)).     && pLo
1b80b 63 6b 2d 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f 63 6b  ck->eLock>=eLock
1b80c 54 79 70 65 20 0a 20 20 20 20 29 7b 0a 20 20 20  Type .    ){.   
1b80d 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1b80e 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 61 69   }.  }..  /* Fai
1b80f 6c 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20  led to find the 
1b810 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 2a  required lock. *
1b811 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  /.  return 0;.}.
1b812 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1b813 5f 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 64 65  _DEBUG */..#ifde
1b814 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f  f SQLITE_DEBUG./
1b815 2a 0a 2a 2a 2a 2a 20 54 68 69 73 20 66 75 6e 63  *.**** This func
1b816 74 69 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 64  tion may be used
1b817 20 61 73 20 70 61 72 74 20 6f 66 20 61 73 73 65   as part of asse
1b818 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20  rt() statements 
1b819 6f 6e 6c 79 2e 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  only. ****.**.**
1b81a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
1b81b 69 74 20 77 6f 75 6c 64 20 62 65 20 69 6c 6c 65  it would be ille
1b81c 67 61 6c 20 66 6f 72 20 70 42 74 72 65 65 20 74  gal for pBtree t
1b81d 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 74 68 65  o write into the
1b81e 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  .** table or ind
1b81f 65 78 20 72 6f 6f 74 65 64 20 61 74 20 69 52 6f  ex rooted at iRo
1b820 6f 74 20 62 65 63 61 75 73 65 20 6f 74 68 65 72  ot because other
1b821 20 73 68 61 72 65 64 20 63 6f 6e 6e 65 63 74 69   shared connecti
1b822 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 69 6d 75 6c  ons are.** simul
1b823 74 61 6e 65 6f 75 73 6c 79 20 72 65 61 64 69 6e  taneously readin
1b824 67 20 74 68 61 74 20 73 61 6d 65 20 74 61 62 6c  g that same tabl
1b825 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a  e or index..**.*
1b826 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20  * It is illegal 
1b827 66 6f 72 20 70 42 74 72 65 65 20 74 6f 20 77 72  for pBtree to wr
1b828 69 74 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65  ite if some othe
1b829 72 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 74  r Btree object t
1b82a 68 61 74 0a 2a 2a 20 73 68 61 72 65 73 20 74 68  hat.** shares th
1b82b 65 20 73 61 6d 65 20 42 74 53 68 61 72 65 64 20  e same BtShared 
1b82c 6f 62 6a 65 63 74 20 69 73 20 63 75 72 72 65 6e  object is curren
1b82d 74 6c 79 20 72 65 61 64 69 6e 67 20 6f 72 20 77  tly reading or w
1b82e 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 69 52  riting.** the iR
1b82f 6f 6f 74 20 74 61 62 6c 65 2e 20 20 45 78 63 65  oot table.  Exce
1b830 70 74 2c 20 69 66 20 74 68 65 20 6f 74 68 65 72  pt, if the other
1b831 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 68 61   Btree object ha
1b832 73 20 74 68 65 0a 2a 2a 20 72 65 61 64 2d 75 6e  s the.** read-un
1b833 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 73  committed flag s
1b834 65 74 2c 20 74 68 65 6e 20 69 74 20 69 73 20 4f  et, then it is O
1b835 4b 20 66 6f 72 20 74 68 65 20 6f 74 68 65 72 20  K for the other 
1b836 6f 62 6a 65 63 74 20 74 6f 0a 2a 2a 20 68 61 76  object to.** hav
1b837 65 20 61 20 72 65 61 64 20 63 75 72 73 6f 72 2e  e a read cursor.
1b838 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  .**.** For examp
1b839 6c 65 2c 20 62 65 66 6f 72 65 20 77 72 69 74 69  le, before writi
1b83a 6e 67 20 74 6f 20 61 6e 79 20 70 61 72 74 20 6f  ng to any part o
1b83b 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69  f the table or i
1b83c 6e 64 65 78 0a 2a 2a 20 72 6f 6f 74 65 64 20 61  ndex.** rooted a
1b83d 74 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 6f 6e  t page iRoot, on
1b83e 65 20 73 68 6f 75 6c 64 20 63 61 6c 6c 3a 0a 2a  e should call:.*
1b83f 2a 0a 2a 2a 20 20 20 20 61 73 73 65 72 74 28 20  *.**    assert( 
1b840 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74  !hasReadConflict
1b841 73 28 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 29  s(pBtree, iRoot)
1b842 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e   );.*/.static in
1b843 74 20 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63  t hasReadConflic
1b844 74 73 28 42 74 72 65 65 20 2a 70 42 74 72 65 65  ts(Btree *pBtree
1b845 2c 20 50 67 6e 6f 20 69 52 6f 6f 74 29 7b 0a 20  , Pgno iRoot){. 
1b846 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20   BtCursor *p;.  
1b847 66 6f 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 42  for(p=pBtree->pB
1b848 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70  t->pCursor; p; p
1b849 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  =p->pNext){.    
1b84a 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d  if( p->pgnoRoot=
1b84b 3d 69 52 6f 6f 74 20 0a 20 20 20 20 20 26 26 20  =iRoot .     && 
1b84c 70 2d 3e 70 42 74 72 65 65 21 3d 70 42 74 72 65  p->pBtree!=pBtre
1b84d 65 0a 20 20 20 20 20 26 26 20 30 3d 3d 28 70 2d  e.     && 0==(p-
1b84e 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61  >pBtree->db->fla
1b84f 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61 64  gs & SQLITE_Read
1b850 55 6e 63 6f 6d 6d 69 74 74 65 64 29 0a 20 20 20  Uncommitted).   
1b851 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1b852 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
1b853 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64  return 0;.}.#end
1b854 69 66 20 20 20 20 2f 2a 20 23 69 66 64 65 66 20  if    /* #ifdef 
1b855 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a  SQLITE_DEBUG */.
1b856 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 6f 20  ./*.** Query to 
1b857 73 65 65 20 69 66 20 42 74 72 65 65 20 68 61 6e  see if Btree han
1b858 64 6c 65 20 70 20 6d 61 79 20 6f 62 74 61 69 6e  dle p may obtain
1b859 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20   a lock of type 
1b85a 65 4c 6f 63 6b 20 0a 2a 2a 20 28 52 45 41 44 5f  eLock .** (READ_
1b85b 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f  LOCK or WRITE_LO
1b85c 43 4b 29 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  CK) on the table
1b85d 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20   with root-page 
1b85e 69 54 61 62 2e 20 52 65 74 75 72 6e 0a 2a 2a 20  iTab. Return.** 
1b85f 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 65  SQLITE_OK if the
1b860 20 6c 6f 63 6b 20 6d 61 79 20 62 65 20 6f 62 74   lock may be obt
1b861 61 69 6e 65 64 20 28 62 79 20 63 61 6c 6c 69 6e  ained (by callin
1b862 67 0a 2a 2a 20 73 65 74 53 68 61 72 65 64 43 61  g.** setSharedCa
1b863 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2c  cheTableLock()),
1b864 20 6f 72 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45   or SQLITE_LOCKE
1b865 44 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61  D if not..*/.sta
1b866 74 69 63 20 69 6e 74 20 71 75 65 72 79 53 68 61  tic int querySha
1b867 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
1b868 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f  k(Btree *p, Pgno
1b869 20 69 54 61 62 2c 20 75 38 20 65 4c 6f 63 6b 29   iTab, u8 eLock)
1b86a 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
1b86b 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74  t = p->pBt;.  Bt
1b86c 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20  Lock *pIter;..  
1b86d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
1b86e 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
1b86f 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65  ) );.  assert( e
1b870 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20  Lock==READ_LOCK 
1b871 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f  || eLock==WRITE_
1b872 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
1b873 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20  ( p->db!=0 );.  
1b874 61 73 73 65 72 74 28 20 21 28 70 2d 3e 64 62 2d  assert( !(p->db-
1b875 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65  >flags&SQLITE_Re
1b876 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 7c 7c  adUncommitted)||
1b877 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43  eLock==WRITE_LOC
1b878 4b 7c 7c 69 54 61 62 3d 3d 31 20 29 3b 0a 20 20  K||iTab==1 );.  
1b879 0a 20 20 2f 2a 20 49 66 20 72 65 71 75 65 73 74  .  /* If request
1b87a 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ing a write-lock
1b87b 2c 20 74 68 65 6e 20 74 68 65 20 42 74 72 65 65  , then the Btree
1b87c 20 6d 75 73 74 20 68 61 76 65 20 61 6e 20 6f 70   must have an op
1b87d 65 6e 20 77 72 69 74 65 0a 20 20 2a 2a 20 74 72  en write.  ** tr
1b87e 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 69  ansaction on thi
1b87f 73 20 66 69 6c 65 2e 20 41 6e 64 2c 20 6f 62 76  s file. And, obv
1b880 69 6f 75 73 6c 79 2c 20 66 6f 72 20 74 68 69 73  iously, for this
1b881 20 74 6f 20 62 65 20 73 6f 20 74 68 65 72 65 20   to be so there 
1b882 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 61 6e  .  ** must be an
1b883 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e   open write tran
1b884 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 66  saction on the f
1b885 69 6c 65 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f  ile itself..  */
1b886 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b  .  assert( eLock
1b887 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 28  ==READ_LOCK || (
1b888 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 20  p==pBt->pWriter 
1b889 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  && p->inTrans==T
1b88a 52 41 4e 53 5f 57 52 49 54 45 29 20 29 3b 0a 20  RANS_WRITE) );. 
1b88b 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d   assert( eLock==
1b88c 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 42 74  READ_LOCK || pBt
1b88d 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
1b88e 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a  =TRANS_WRITE );.
1b88f 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75    .  /* This rou
1b890 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20  tine is a no-op 
1b891 69 66 20 74 68 65 20 73 68 61 72 65 64 2d 63 61  if the shared-ca
1b892 63 68 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c  che is not enabl
1b893 65 64 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e  ed */.  if( !p->
1b894 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20  sharable ){.    
1b895 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1b896 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 73  ;.  }..  /* If s
1b897 6f 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63  ome other connec
1b898 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20  tion is holding 
1b899 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
1b89a 6b 2c 20 74 68 65 0a 20 20 2a 2a 20 72 65 71 75  k, the.  ** requ
1b89b 65 73 74 65 64 20 6c 6f 63 6b 20 6d 61 79 20 6e  ested lock may n
1b89c 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a  ot be obtained..
1b89d 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e    */.  if( pBt->
1b89e 70 57 72 69 74 65 72 21 3d 70 20 26 26 20 28 70  pWriter!=p && (p
1b89f 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42  Bt->btsFlags & B
1b8a0 54 53 5f 45 58 43 4c 55 53 49 56 45 29 21 3d 30  TS_EXCLUSIVE)!=0
1b8a1 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43   ){.    sqlite3C
1b8a2 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64  onnectionBlocked
1b8a3 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e 70 57 72  (p->db, pBt->pWr
1b8a4 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20 20 20 72  iter->db);.    r
1b8a5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  eturn SQLITE_LOC
1b8a6 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b  KED_SHAREDCACHE;
1b8a7 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70 49 74 65  .  }..  for(pIte
1b8a8 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49  r=pBt->pLock; pI
1b8a9 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72  ter; pIter=pIter
1b8aa 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 2f 2a  ->pNext){.    /*
1b8ab 20 54 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 28   The condition (
1b8ac 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c  pIter->eLock!=eL
1b8ad 6f 63 6b 29 20 69 6e 20 74 68 65 20 66 6f 6c 6c  ock) in the foll
1b8ae 6f 77 69 6e 67 20 69 66 28 2e 2e 2e 29 20 0a 20  owing if(...) . 
1b8af 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20     ** statement 
1b8b0 69 73 20 61 20 73 69 6d 70 6c 69 66 69 63 61 74  is a simplificat
1b8b1 69 6f 6e 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20  ion of:.    **. 
1b8b2 20 20 20 2a 2a 20 20 20 28 65 4c 6f 63 6b 3d 3d     **   (eLock==
1b8b3 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c 20 70 49  WRITE_LOCK || pI
1b8b4 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54  ter->eLock==WRIT
1b8b5 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 2a 2a 0a 20  E_LOCK).    **. 
1b8b6 20 20 20 2a 2a 20 73 69 6e 63 65 20 77 65 20 6b     ** since we k
1b8b7 6e 6f 77 20 74 68 61 74 20 69 66 20 65 4c 6f 63  now that if eLoc
1b8b8 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 2c 20 74  k==WRITE_LOCK, t
1b8b9 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e  hen no other con
1b8ba 6e 65 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 6d  nection.    ** m
1b8bb 61 79 20 68 6f 6c 64 20 61 20 57 52 49 54 45 5f  ay hold a WRITE_
1b8bc 4c 4f 43 4b 20 6f 6e 20 61 6e 79 20 74 61 62 6c  LOCK on any tabl
1b8bd 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 28  e in this file (
1b8be 73 69 6e 63 65 20 74 68 65 72 65 20 63 61 6e 0a  since there can.
1b8bf 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 62 65 20 61      ** only be a
1b8c0 20 73 69 6e 67 6c 65 20 77 72 69 74 65 72 29 2e   single writer).
1b8c1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
1b8c2 72 74 28 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b  rt( pIter->eLock
1b8c3 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70  ==READ_LOCK || p
1b8c4 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49  Iter->eLock==WRI
1b8c5 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61  TE_LOCK );.    a
1b8c6 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45  ssert( eLock==RE
1b8c7 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 74 65 72  AD_LOCK || pIter
1b8c8 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c 7c 20 70  ->pBtree==p || p
1b8c9 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41  Iter->eLock==REA
1b8ca 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28  D_LOCK);.    if(
1b8cb 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 21 3d   pIter->pBtree!=
1b8cc 70 20 26 26 20 70 49 74 65 72 2d 3e 69 54 61 62  p && pIter->iTab
1b8cd 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 49 74 65  le==iTab && pIte
1b8ce 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 20  r->eLock!=eLock 
1b8cf 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1b8d0 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65  ConnectionBlocke
1b8d1 64 28 70 2d 3e 64 62 2c 20 70 49 74 65 72 2d 3e  d(p->db, pIter->
1b8d2 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 20  pBtree->db);.   
1b8d3 20 20 20 69 66 28 20 65 4c 6f 63 6b 3d 3d 57 52     if( eLock==WR
1b8d4 49 54 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  ITE_LOCK ){.    
1b8d5 20 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70      assert( p==p
1b8d6 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20  Bt->pWriter );. 
1b8d7 20 20 20 20 20 20 20 70 42 74 2d 3e 62 74 73 46         pBt->btsF
1b8d8 6c 61 67 73 20 7c 3d 20 42 54 53 5f 50 45 4e 44  lags |= BTS_PEND
1b8d9 49 4e 47 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ING;.      }.   
1b8da 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1b8db 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41  _LOCKED_SHAREDCA
1b8dc 43 48 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  CHE;.    }.  }. 
1b8dd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1b8de 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21  K;.}.#endif /* !
1b8df 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
1b8e0 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66  ED_CACHE */..#if
1b8e1 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1b8e2 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a  _SHARED_CACHE./*
1b8e3 0a 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f  .** Add a lock o
1b8e4 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68  n the table with
1b8e5 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c   root-page iTabl
1b8e6 65 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d  e to the shared-
1b8e7 62 74 72 65 65 20 75 73 65 64 0a 2a 2a 20 62 79  btree used.** by
1b8e8 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e   Btree handle p.
1b8e9 20 50 61 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b   Parameter eLock
1b8ea 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
1b8eb 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a  READ_LOCK or .**
1b8ec 20 57 52 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a   WRITE_LOCK..**.
1b8ed 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
1b8ee 20 61 73 73 75 6d 65 73 20 74 68 65 20 66 6f 6c   assumes the fol
1b8ef 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
1b8f0 28 61 29 20 54 68 65 20 73 70 65 63 69 66 69 65  (a) The specifie
1b8f1 64 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 70  d Btree object p
1b8f2 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f   is connected to
1b8f3 20 61 20 73 68 61 72 61 62 6c 65 0a 2a 2a 20 20   a sharable.**  
1b8f4 20 20 20 20 20 64 61 74 61 62 61 73 65 20 28 6f       database (o
1b8f5 6e 65 20 77 69 74 68 20 74 68 65 20 42 74 53 68  ne with the BtSh
1b8f6 61 72 65 64 2e 73 68 61 72 61 62 6c 65 20 66 6c  ared.sharable fl
1b8f7 61 67 20 73 65 74 29 2c 20 61 6e 64 0a 2a 2a 0a  ag set), and.**.
1b8f8 2a 2a 20 20 20 28 62 29 20 4e 6f 20 6f 74 68 65  **   (b) No othe
1b8f9 72 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 20  r Btree objects 
1b8fa 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 74 68 61 74  hold a lock that
1b8fb 20 63 6f 6e 66 6c 69 63 74 73 0a 2a 2a 20 20 20   conflicts.**   
1b8fc 20 20 20 20 77 69 74 68 20 74 68 65 20 72 65 71      with the req
1b8fd 75 65 73 74 65 64 20 6c 6f 63 6b 20 28 69 2e 65  uested lock (i.e
1b8fe 2e 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63  . querySharedCac
1b8ff 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 68 61  heTableLock() ha
1b900 73 0a 2a 2a 20 20 20 20 20 20 20 61 6c 72 65 61  s.**       alrea
1b901 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 61  dy been called a
1b902 6e 64 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49  nd returned SQLI
1b903 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 53 51  TE_OK)..**.** SQ
1b904 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
1b905 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63 6b 20  ned if the lock 
1b906 69 73 20 61 64 64 65 64 20 73 75 63 63 65 73 73  is added success
1b907 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f 4e 4f  fully. SQLITE_NO
1b908 4d 45 4d 20 0a 2a 2a 20 69 73 20 72 65 74 75 72  MEM .** is retur
1b909 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20  ned if a malloc 
1b90a 61 74 74 65 6d 70 74 20 66 61 69 6c 73 2e 0a 2a  attempt fails..*
1b90b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74  /.static int set
1b90c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
1b90d 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50  Lock(Btree *p, P
1b90e 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65  gno iTable, u8 e
1b90f 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65  Lock){.  BtShare
1b910 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
1b911 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b  .  BtLock *pLock
1b912 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a   = 0;.  BtLock *
1b913 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  pIter;..  assert
1b914 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
1b915 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20  ldsMutex(p) );. 
1b916 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d   assert( eLock==
1b917 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f  READ_LOCK || eLo
1b918 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29  ck==WRITE_LOCK )
1b919 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64  ;.  assert( p->d
1b91a 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 20  b!=0 );..  /* A 
1b91b 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20  connection with 
1b91c 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69  the read-uncommi
1b91d 74 74 65 64 20 66 6c 61 67 20 73 65 74 20 77 69  tted flag set wi
1b91e 6c 6c 20 6e 65 76 65 72 20 74 72 79 20 74 6f 0a  ll never try to.
1b91f 20 20 2a 2a 20 6f 62 74 61 69 6e 20 61 20 72 65    ** obtain a re
1b920 61 64 2d 6c 6f 63 6b 20 75 73 69 6e 67 20 74 68  ad-lock using th
1b921 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65  is function. The
1b922 20 6f 6e 6c 79 20 72 65 61 64 2d 6c 6f 63 6b 20   only read-lock 
1b923 6f 62 74 61 69 6e 65 64 0a 20 20 2a 2a 20 62 79  obtained.  ** by
1b924 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e   a connection in
1b925 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65   read-uncommitte
1b926 64 20 6d 6f 64 65 20 69 73 20 6f 6e 20 74 68 65  d mode is on the
1b927 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 0a   sqlite_master .
1b928 20 20 2a 2a 20 74 61 62 6c 65 2c 20 61 6e 64 20    ** table, and 
1b929 74 68 61 74 20 6c 6f 63 6b 20 69 73 20 6f 62 74  that lock is obt
1b92a 61 69 6e 65 64 20 69 6e 20 42 74 72 65 65 42 65  ained in BtreeBe
1b92b 67 69 6e 54 72 61 6e 73 28 29 2e 20 20 2a 2f 0a  ginTrans().  */.
1b92c 20 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 2d    assert( 0==(p-
1b92d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  >db->flags&SQLIT
1b92e 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65  E_ReadUncommitte
1b92f 64 29 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49  d) || eLock==WRI
1b930 54 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a  TE_LOCK );..  /*
1b931 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   This function s
1b932 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61  hould only be ca
1b933 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 61 62  lled on a sharab
1b934 6c 65 20 62 2d 74 72 65 65 20 61 66 74 65 72 20  le b-tree after 
1b935 69 74 20 0a 20 20 2a 2a 20 68 61 73 20 62 65 65  it .  ** has bee
1b936 6e 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68 61  n determined tha
1b937 74 20 6e 6f 20 6f 74 68 65 72 20 62 2d 74 72 65  t no other b-tre
1b938 65 20 68 6f 6c 64 73 20 61 20 63 6f 6e 66 6c 69  e holds a confli
1b939 63 74 69 6e 67 20 6c 6f 63 6b 2e 20 20 2a 2f 0a  cting lock.  */.
1b93a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61    assert( p->sha
1b93b 72 61 62 6c 65 20 29 3b 0a 20 20 61 73 73 65 72  rable );.  asser
1b93c 74 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75  t( SQLITE_OK==qu
1b93d 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61  erySharedCacheTa
1b93e 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c  bleLock(p, iTabl
1b93f 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20  e, eLock) );..  
1b940 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 68 20  /* First search 
1b941 74 68 65 20 6c 69 73 74 20 66 6f 72 20 61 6e 20  the list for an 
1b942 65 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20 6f 6e  existing lock on
1b943 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a   this table. */.
1b944 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d    for(pIter=pBt-
1b945 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70  >pLock; pIter; p
1b946 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78  Iter=pIter->pNex
1b947 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65  t){.    if( pIte
1b948 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c  r->iTable==iTabl
1b949 65 20 26 26 20 70 49 74 65 72 2d 3e 70 42 74 72  e && pIter->pBtr
1b94a 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 70  ee==p ){.      p
1b94b 4c 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a 20 20  Lock = pIter;.  
1b94c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1b94d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
1b94e 65 20 61 62 6f 76 65 20 73 65 61 72 63 68 20 64  e above search d
1b94f 69 64 20 6e 6f 74 20 66 69 6e 64 20 61 20 42 74  id not find a Bt
1b950 4c 6f 63 6b 20 73 74 72 75 63 74 20 61 73 73 6f  Lock struct asso
1b951 63 69 61 74 69 6e 67 20 42 74 72 65 65 20 70 0a  ciating Btree p.
1b952 20 20 2a 2a 20 77 69 74 68 20 74 61 62 6c 65 20    ** with table 
1b953 69 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61 74 65  iTable, allocate
1b954 20 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20 69 74   one and link it
1b955 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 2e 0a   into the list..
1b956 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c 6f 63    */.  if( !pLoc
1b957 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d  k ){.    pLock =
1b958 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c 69 74   (BtLock *)sqlit
1b959 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a  e3MallocZero(siz
1b95a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a 20 20  eof(BtLock));.  
1b95b 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a    if( !pLock ){.
1b95c 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1b95d 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
1b95e 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54 61 62  .    pLock->iTab
1b95f 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20  le = iTable;.   
1b960 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20 3d   pLock->pBtree =
1b961 20 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70   p;.    pLock->p
1b962 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63  Next = pBt->pLoc
1b963 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c 6f 63  k;.    pBt->pLoc
1b964 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a  k = pLock;.  }..
1b965 20 20 2f 2a 20 53 65 74 20 74 68 65 20 42 74 4c    /* Set the BtL
1b966 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69 61 62  ock.eLock variab
1b967 6c 65 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75  le to the maximu
1b968 6d 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  m of the current
1b969 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64 20 74   lock.  ** and t
1b96a 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
1b96b 6b 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 66  k. This means if
1b96c 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77 61   a write-lock wa
1b96d 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 0a 20  s already held. 
1b96e 20 2a 2a 20 61 6e 64 20 61 20 72 65 61 64 2d 6c   ** and a read-l
1b96f 6f 63 6b 20 72 65 71 75 65 73 74 65 64 2c 20 77  ock requested, w
1b970 65 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72 65 63  e don't incorrec
1b971 74 6c 79 20 64 6f 77 6e 67 72 61 64 65 20 74 68  tly downgrade th
1b972 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61  e lock..  */.  a
1b973 73 73 65 72 74 28 20 57 52 49 54 45 5f 4c 4f 43  ssert( WRITE_LOC
1b974 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b 0a 20  K>READ_LOCK );. 
1b975 20 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f 63 6b   if( eLock>pLock
1b976 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70  ->eLock ){.    p
1b977 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 65 4c  Lock->eLock = eL
1b978 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  ock;.  }..  retu
1b979 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1b97a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54  #endif /* !SQLIT
1b97b 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
1b97c 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  CHE */..#ifndef 
1b97d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
1b97e 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 52  ED_CACHE./*.** R
1b97f 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 74  elease all the t
1b980 61 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f 63 6b  able locks (lock
1b981 73 20 6f 62 74 61 69 6e 65 64 20 76 69 61 20 63  s obtained via c
1b982 61 6c 6c 73 20 74 6f 0a 2a 2a 20 74 68 65 20 73  alls to.** the s
1b983 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62  etSharedCacheTab
1b984 6c 65 4c 6f 63 6b 28 29 20 70 72 6f 63 65 64 75  leLock() procedu
1b985 72 65 29 20 68 65 6c 64 20 62 79 20 42 74 72 65  re) held by Btre
1b986 65 20 6f 62 6a 65 63 74 20 70 2e 0a 2a 2a 0a 2a  e object p..**.*
1b987 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1b988 61 73 73 75 6d 65 73 20 74 68 61 74 20 42 74 72  assumes that Btr
1b989 65 65 20 70 20 68 61 73 20 61 6e 20 6f 70 65 6e  ee p has an open
1b98a 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 0a   read or write .
1b98b 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ** transaction. 
1b98c 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2c 20  If it does not, 
1b98d 74 68 65 6e 20 74 68 65 20 42 54 53 5f 50 45 4e  then the BTS_PEN
1b98e 44 49 4e 47 20 66 6c 61 67 0a 2a 2a 20 6d 61 79  DING flag.** may
1b98f 20 62 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 20   be incorrectly 
1b990 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 73 74 61 74  cleared..*/.stat
1b991 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41 6c 6c  ic void clearAll
1b992 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
1b993 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 29 7b  Locks(Btree *p){
1b994 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
1b995 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c   = p->pBt;.  BtL
1b996 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d 20 26  ock **ppIter = &
1b997 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 61  pBt->pLock;..  a
1b998 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
1b999 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29  reeHoldsMutex(p)
1b99a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
1b99b 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30 3d 3d  >sharable || 0==
1b99c 2a 70 70 49 74 65 72 20 29 3b 0a 20 20 61 73 73  *ppIter );.  ass
1b99d 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e  ert( p->inTrans>
1b99e 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 2a  0 );..  while( *
1b99f 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20 42 74  ppIter ){.    Bt
1b9a0 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a 70  Lock *pLock = *p
1b9a1 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73 65 72  pIter;.    asser
1b9a2 74 28 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67  t( (pBt->btsFlag
1b9a3 73 20 26 20 42 54 53 5f 45 58 43 4c 55 53 49 56  s & BTS_EXCLUSIV
1b9a4 45 29 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57  E)==0 || pBt->pW
1b9a5 72 69 74 65 72 3d 3d 70 4c 6f 63 6b 2d 3e 70 42  riter==pLock->pB
1b9a6 74 72 65 65 20 29 3b 0a 20 20 20 20 61 73 73 65  tree );.    asse
1b9a7 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65  rt( pLock->pBtre
1b9a8 65 2d 3e 69 6e 54 72 61 6e 73 3e 3d 70 4c 6f 63  e->inTrans>=pLoc
1b9a9 6b 2d 3e 65 4c 6f 63 6b 20 29 3b 0a 20 20 20 20  k->eLock );.    
1b9aa 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65  if( pLock->pBtre
1b9ab 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 2a 70  e==p ){.      *p
1b9ac 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e 70  pIter = pLock->p
1b9ad 4e 65 78 74 3b 0a 20 20 20 20 20 20 61 73 73 65  Next;.      asse
1b9ae 72 74 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c  rt( pLock->iTabl
1b9af 65 21 3d 31 20 7c 7c 20 70 4c 6f 63 6b 3d 3d 26  e!=1 || pLock==&
1b9b0 70 2d 3e 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 20  p->lock );.     
1b9b1 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 69 54 61 62   if( pLock->iTab
1b9b2 6c 65 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  le!=1 ){.       
1b9b3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c   sqlite3_free(pL
1b9b4 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ock);.      }.  
1b9b5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
1b9b6 70 49 74 65 72 20 3d 20 26 70 4c 6f 63 6b 2d 3e  pIter = &pLock->
1b9b7 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  pNext;.    }.  }
1b9b8 0a 0a 20 20 61 73 73 65 72 74 28 20 28 70 42 74  ..  assert( (pBt
1b9b9 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53  ->btsFlags & BTS
1b9ba 5f 50 45 4e 44 49 4e 47 29 3d 3d 30 20 7c 7c 20  _PENDING)==0 || 
1b9bb 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a  pBt->pWriter );.
1b9bc 20 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74    if( pBt->pWrit
1b9bd 65 72 3d 3d 70 20 29 7b 0a 20 20 20 20 70 42 74  er==p ){.    pBt
1b9be 2d 3e 70 57 72 69 74 65 72 20 3d 20 30 3b 0a 20  ->pWriter = 0;. 
1b9bf 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73     pBt->btsFlags
1b9c0 20 26 3d 20 7e 28 42 54 53 5f 45 58 43 4c 55 53   &= ~(BTS_EXCLUS
1b9c1 49 56 45 7c 42 54 53 5f 50 45 4e 44 49 4e 47 29  IVE|BTS_PENDING)
1b9c2 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42  ;.  }else if( pB
1b9c3 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  t->nTransaction=
1b9c4 3d 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69  =2 ){.    /* Thi
1b9c5 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
1b9c6 6c 6c 65 64 20 77 68 65 6e 20 42 74 72 65 65 20  lled when Btree 
1b9c7 70 20 69 73 20 63 6f 6e 63 6c 75 64 69 6e 67 20  p is concluding 
1b9c8 69 74 73 20 0a 20 20 20 20 2a 2a 20 74 72 61 6e  its .    ** tran
1b9c9 73 61 63 74 69 6f 6e 2e 20 49 66 20 74 68 65 72  saction. If ther
1b9ca 65 20 63 75 72 72 65 6e 74 6c 79 20 65 78 69 73  e currently exis
1b9cb 74 73 20 61 20 77 72 69 74 65 72 2c 20 61 6e 64  ts a writer, and
1b9cc 20 70 20 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a   p is not.    **
1b9cd 20 74 68 61 74 20 77 72 69 74 65 72 2c 20 74 68   that writer, th
1b9ce 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  en the number of
1b9cf 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 63   locks held by c
1b9d0 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 74 68 65 72  onnections other
1b9d1 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 68 65  .    ** than the
1b9d2 20 77 72 69 74 65 72 20 6d 75 73 74 20 62 65 20   writer must be 
1b9d3 61 62 6f 75 74 20 74 6f 20 64 72 6f 70 20 74 6f  about to drop to
1b9d4 20 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 20 63   zero. In this c
1b9d5 61 73 65 0a 20 20 20 20 2a 2a 20 73 65 74 20 74  ase.    ** set t
1b9d6 68 65 20 42 54 53 5f 50 45 4e 44 49 4e 47 20 66  he BTS_PENDING f
1b9d7 6c 61 67 20 74 6f 20 30 2e 0a 20 20 20 20 2a 2a  lag to 0..    **
1b9d8 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65  .    ** If there
1b9d9 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
1b9da 79 20 61 20 77 72 69 74 65 72 2c 20 74 68 65 6e  y a writer, then
1b9db 20 42 54 53 5f 50 45 4e 44 49 4e 47 20 6d 75 73   BTS_PENDING mus
1b9dc 74 0a 20 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f  t.    ** be zero
1b9dd 20 61 6c 72 65 61 64 79 2e 20 53 6f 20 74 68 69   already. So thi
1b9de 73 20 6e 65 78 74 20 6c 69 6e 65 20 69 73 20 68  s next line is h
1b9df 61 72 6d 6c 65 73 73 20 69 6e 20 74 68 61 74 20  armless in that 
1b9e0 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  case..    */.   
1b9e1 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26   pBt->btsFlags &
1b9e2 3d 20 7e 42 54 53 5f 50 45 4e 44 49 4e 47 3b 0a  = ~BTS_PENDING;.
1b9e3 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
1b9e4 73 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 6e 67  s function chang
1b9e5 65 73 20 61 6c 6c 20 77 72 69 74 65 2d 6c 6f 63  es all write-loc
1b9e6 6b 73 20 68 65 6c 64 20 62 79 20 42 74 72 65 65  ks held by Btree
1b9e7 20 70 20 69 6e 74 6f 20 72 65 61 64 2d 6c 6f 63   p into read-loc
1b9e8 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ks..*/.static vo
1b9e9 69 64 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53  id downgradeAllS
1b9ea 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
1b9eb 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 29 7b 0a  ocks(Btree *p){.
1b9ec 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
1b9ed 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20  = p->pBt;.  if( 
1b9ee 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20  pBt->pWriter==p 
1b9ef 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70  ){.    BtLock *p
1b9f0 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70  Lock;.    pBt->p
1b9f1 57 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Writer = 0;.    
1b9f2 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 3d  pBt->btsFlags &=
1b9f3 20 7e 28 42 54 53 5f 45 58 43 4c 55 53 49 56 45   ~(BTS_EXCLUSIVE
1b9f4 7c 42 54 53 5f 50 45 4e 44 49 4e 47 29 3b 0a 20  |BTS_PENDING);. 
1b9f5 20 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 42 74     for(pLock=pBt
1b9f6 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20  ->pLock; pLock; 
1b9f7 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65  pLock=pLock->pNe
1b9f8 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  xt){.      asser
1b9f9 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3d  t( pLock->eLock=
1b9fa 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c  =READ_LOCK || pL
1b9fb 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29  ock->pBtree==p )
1b9fc 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 65  ;.      pLock->e
1b9fd 4c 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b  Lock = READ_LOCK
1b9fe 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23  ;.    }.  }.}..#
1b9ff 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1ba00 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
1ba01 45 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69  E */..static voi
1ba02 64 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65  d releasePage(Me
1ba03 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b 20 20  mPage *pPage);  
1ba04 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72  /* Forward refer
1ba05 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 2a 2a  ence */../*.****
1ba06 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1ba07 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 6f 66  s used inside of
1ba08 20 61 73 73 65 72 74 28 29 20 6f 6e 6c 79 20 2a   assert() only *
1ba09 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 56 65 72 69 66 79  ***.**.** Verify
1ba0a 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72   that the cursor
1ba0b 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78   holds the mutex
1ba0c 20 6f 6e 20 69 74 73 20 42 74 53 68 61 72 65 64   on its BtShared
1ba0d 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
1ba0e 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 69  E_DEBUG.static i
1ba0f 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  nt cursorHoldsMu
1ba10 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a 70 29  tex(BtCursor *p)
1ba11 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
1ba12 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
1ba13 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a  >pBt->mutex);.}.
1ba14 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66  #endif...#ifndef
1ba15 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
1ba16 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e 76 61  RBLOB./*.** Inva
1ba17 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66  lidate the overf
1ba18 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61  low page-list ca
1ba19 63 68 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70  che for cursor p
1ba1a 43 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a  Cur, if any..*/.
1ba1b 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61  static void inva
1ba1c 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61  lidateOverflowCa
1ba1d 63 68 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43  che(BtCursor *pC
1ba1e 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63  ur){.  assert( c
1ba1f 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
1ba20 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 74  pCur) );.  sqlit
1ba21 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 61 4f  e3_free(pCur->aO
1ba22 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72  verflow);.  pCur
1ba23 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b  ->aOverflow = 0;
1ba24 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69  .}../*.** Invali
1ba25 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f  date the overflo
1ba26 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68  w page-list cach
1ba27 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73 6f 72  e for all cursor
1ba28 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20 74  s opened.** on t
1ba29 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 20  he shared btree 
1ba2a 73 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a 2a  structure pBt..*
1ba2b 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
1ba2c 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66  validateAllOverf
1ba2d 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61 72 65  lowCache(BtShare
1ba2e 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72  d *pBt){.  BtCur
1ba2f 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  sor *p;.  assert
1ba30 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1ba31 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
1ba32 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d   );.  for(p=pBt-
1ba33 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70  >pCursor; p; p=p
1ba34 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e  ->pNext){.    in
1ba35 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77  validateOverflow
1ba36 43 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a  Cache(p);.  }.}.
1ba37 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
1ba38 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62  tion is called b
1ba39 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e 67 20  efore modifying 
1ba3a 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
1ba3b 61 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 69 6e  a table.** to in
1ba3c 76 61 6c 69 64 61 74 65 20 61 6e 79 20 69 6e 63  validate any inc
1ba3d 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 74 68  rblob cursors th
1ba3e 61 74 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74  at are open on t
1ba3f 68 65 0a 2a 2a 20 72 6f 77 20 6f 72 20 6f 6e 65  he.** row or one
1ba40 20 6f 66 20 74 68 65 20 72 6f 77 73 20 62 65 69   of the rows bei
1ba41 6e 67 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a  ng modified..**.
1ba42 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 69  ** If argument i
1ba43 73 43 6c 65 61 72 54 61 62 6c 65 20 69 73 20 74  sClearTable is t
1ba44 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 65 6e  rue, then the en
1ba45 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66  tire contents of
1ba46 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 69 73   the.** table is
1ba47 20 61 62 6f 75 74 20 74 6f 20 62 65 20 64 65 6c   about to be del
1ba48 65 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  eted. In this ca
1ba49 73 65 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c  se invalidate al
1ba4a 6c 20 69 6e 63 72 62 6c 6f 62 0a 2a 2a 20 63 75  l incrblob.** cu
1ba4b 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 61 6e  rsors open on an
1ba4c 79 20 72 6f 77 20 77 69 74 68 69 6e 20 74 68 65  y row within the
1ba4d 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74   table with root
1ba4e 2d 70 61 67 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a  -page pgnoRoot..
1ba4f 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
1ba50 20 69 66 20 61 72 67 75 6d 65 6e 74 20 69 73 43   if argument isC
1ba51 6c 65 61 72 54 61 62 6c 65 20 69 73 20 66 61 6c  learTable is fal
1ba52 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77  se, then the row
1ba53 20 77 69 74 68 0a 2a 2a 20 72 6f 77 69 64 20 69   with.** rowid i
1ba54 52 6f 77 20 69 73 20 62 65 69 6e 67 20 72 65 70  Row is being rep
1ba55 6c 61 63 65 64 20 6f 72 20 64 65 6c 65 74 65 64  laced or deleted
1ba56 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 69  . In this case i
1ba57 6e 76 61 6c 69 64 61 74 65 0a 2a 2a 20 6f 6e 6c  nvalidate.** onl
1ba58 79 20 74 68 6f 73 65 20 69 6e 63 72 62 6c 6f 62  y those incrblob
1ba59 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e   cursors open on
1ba5a 20 74 68 61 74 20 73 70 65 63 69 66 69 63 20 72   that specific r
1ba5b 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ow..*/.static vo
1ba5c 69 64 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63  id invalidateInc
1ba5d 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 0a 20 20  rblobCursors(.  
1ba5e 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20  Btree *pBtree,  
1ba5f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
1ba60 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20  atabase file to 
1ba61 63 68 65 63 6b 20 2a 2f 0a 20 20 69 36 34 20 69  check */.  i64 i
1ba62 52 6f 77 2c 20 20 20 20 20 20 20 20 20 20 20 20  Row,            
1ba63 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20     /* The rowid 
1ba64 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 63 68  that might be ch
1ba65 61 6e 67 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  anging */.  int 
1ba66 69 73 43 6c 65 61 72 54 61 62 6c 65 20 20 20 20  isClearTable    
1ba67 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
1ba68 6c 6c 20 72 6f 77 73 20 61 72 65 20 62 65 69 6e  ll rows are bein
1ba69 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 29 7b 0a  g deleted */.){.
1ba6a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20    BtCursor *p;. 
1ba6b 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1ba6c 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20   pBtree->pBt;.  
1ba6d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
1ba6e 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
1ba6f 42 74 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28  Btree) );.  for(
1ba70 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20  p=pBt->pCursor; 
1ba71 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a  p; p=p->pNext){.
1ba72 20 20 20 20 69 66 28 20 70 2d 3e 69 73 49 6e 63      if( p->isInc
1ba73 72 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 28  rblobHandle && (
1ba74 69 73 43 6c 65 61 72 54 61 62 6c 65 20 7c 7c 20  isClearTable || 
1ba75 70 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 52  p->info.nKey==iR
1ba76 6f 77 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  ow) ){.      p->
1ba77 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f  eState = CURSOR_
1ba78 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 20  INVALID;.    }. 
1ba79 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 20 20 2f 2a   }.}..#else.  /*
1ba7a 20 53 74 75 62 20 66 75 6e 63 74 69 6f 6e 73 20   Stub functions 
1ba7b 77 68 65 6e 20 49 4e 43 52 42 4c 4f 42 20 69 73  when INCRBLOB is
1ba7c 20 6f 6d 69 74 74 65 64 20 2a 2f 0a 20 20 23 64   omitted */.  #d
1ba7d 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65  efine invalidate
1ba7e 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29  OverflowCache(x)
1ba7f 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c  .  #define inval
1ba80 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77  idateAllOverflow
1ba81 43 61 63 68 65 28 78 29 0a 20 20 23 64 65 66 69  Cache(x).  #defi
1ba82 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63  ne invalidateInc
1ba83 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 78 2c 79  rblobCursors(x,y
1ba84 2c 7a 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ,z).#endif /* SQ
1ba85 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
1ba86 4f 42 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  OB */../*.** Set
1ba87 20 62 69 74 20 70 67 6e 6f 20 6f 66 20 74 68 65   bit pgno of the
1ba88 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f   BtShared.pHasCo
1ba89 6e 74 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68  ntent bitvec. Th
1ba8a 69 73 20 69 73 20 63 61 6c 6c 65 64 20 0a 2a 2a  is is called .**
1ba8b 20 77 68 65 6e 20 61 20 70 61 67 65 20 74 68 61   when a page tha
1ba8c 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e  t previously con
1ba8d 74 61 69 6e 65 64 20 64 61 74 61 20 62 65 63 6f  tained data beco
1ba8e 6d 65 73 20 61 20 66 72 65 65 2d 6c 69 73 74 20  mes a free-list 
1ba8f 6c 65 61 66 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a  leaf .** page..*
1ba90 2a 0a 2a 2a 20 54 68 65 20 42 74 53 68 61 72 65  *.** The BtShare
1ba91 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69  d.pHasContent bi
1ba92 74 76 65 63 20 65 78 69 73 74 73 20 74 6f 20 77  tvec exists to w
1ba93 6f 72 6b 20 61 72 6f 75 6e 64 20 61 6e 20 6f 62  ork around an ob
1ba94 73 63 75 72 65 0a 2a 2a 20 62 75 67 20 63 61 75  scure.** bug cau
1ba95 73 65 64 20 62 79 20 74 68 65 20 69 6e 74 65 72  sed by the inter
1ba96 61 63 74 69 6f 6e 20 6f 66 20 74 77 6f 20 75 73  action of two us
1ba97 65 66 75 6c 20 49 4f 20 6f 70 74 69 6d 69 7a 61  eful IO optimiza
1ba98 74 69 6f 6e 73 20 73 75 72 72 6f 75 6e 64 69 6e  tions surroundin
1ba99 67 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 6c  g.** free-list l
1ba9a 65 61 66 20 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a  eaf pages:.**.**
1ba9b 20 20 20 31 29 20 57 68 65 6e 20 61 6c 6c 20 64     1) When all d
1ba9c 61 74 61 20 69 73 20 64 65 6c 65 74 65 64 20 66  ata is deleted f
1ba9d 72 6f 6d 20 61 20 70 61 67 65 20 61 6e 64 20 74  rom a page and t
1ba9e 68 65 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a  he page becomes.
1ba9f 2a 2a 20 20 20 20 20 20 61 20 66 72 65 65 2d 6c  **      a free-l
1baa0 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74  ist leaf page, t
1baa1 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 77  he page is not w
1baa2 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61  ritten to the da
1baa3 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 28  tabase.**      (
1baa4 61 73 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61  as free-list lea
1baa5 66 20 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20  f pages contain 
1baa6 6e 6f 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61  no meaningful da
1baa7 74 61 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0a 2a  ta). Sometimes.*
1baa8 2a 20 20 20 20 20 20 73 75 63 68 20 61 20 70 61  *      such a pa
1baa9 67 65 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a  ge is not even j
1baaa 6f 75 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74  ournalled (as it
1baab 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64   will not be mod
1baac 69 66 69 65 64 2c 0a 2a 2a 20 20 20 20 20 20 77  ified,.**      w
1baad 68 79 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61  hy bother journa
1baae 6c 6c 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a  lling it?)..**.*
1baaf 2a 20 20 20 32 29 20 57 68 65 6e 20 61 20 66 72  *   2) When a fr
1bab0 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67  ee-list leaf pag
1bab1 65 20 69 73 20 72 65 75 73 65 64 2c 20 69 74 73  e is reused, its
1bab2 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20   content is not 
1bab3 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f  read.**      fro
1bab4 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f  m the database o
1bab5 72 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  r written to the
1bab6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 77   journal file (w
1bab7 68 79 20 73 68 6f 75 6c 64 20 69 74 0a 2a 2a 20  hy should it.** 
1bab8 20 20 20 20 20 62 65 2c 20 69 66 20 69 74 20 69       be, if it i
1bab9 73 20 6e 6f 74 20 61 74 20 61 6c 6c 20 6d 65 61  s not at all mea
1baba 6e 69 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a  ningful?)..**.**
1babb 20 42 79 20 74 68 65 6d 73 65 6c 76 65 73 2c 20   By themselves, 
1babc 74 68 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69  these optimizati
1babd 6f 6e 73 20 77 6f 72 6b 20 66 69 6e 65 20 61 6e  ons work fine an
1babe 64 20 70 72 6f 76 69 64 65 20 61 20 68 61 6e 64  d provide a hand
1babf 79 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65  y.** performance
1bac0 20 62 6f 6f 73 74 20 74 6f 20 62 75 6c 6b 20 64   boost to bulk d
1bac1 65 6c 65 74 65 20 6f 72 20 69 6e 73 65 72 74 20  elete or insert 
1bac2 6f 70 65 72 61 74 69 6f 6e 73 2e 20 48 6f 77 65  operations. Howe
1bac3 76 65 72 2c 20 69 66 0a 2a 2a 20 61 20 70 61 67  ver, if.** a pag
1bac4 65 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68  e is moved to th
1bac5 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20  e free-list and 
1bac6 74 68 65 6e 20 72 65 75 73 65 64 20 77 69 74 68  then reused with
1bac7 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74  in the same.** t
1bac8 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 70 72  ransaction, a pr
1bac9 6f 62 6c 65 6d 20 63 6f 6d 65 73 20 75 70 2e 20  oblem comes up. 
1baca 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  If the page is n
1bacb 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68  ot journalled wh
1bacc 65 6e 0a 2a 2a 20 69 74 20 69 73 20 6d 6f 76 65  en.** it is move
1bacd 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69  d to the free-li
1bace 73 74 20 61 6e 64 20 69 74 20 69 73 20 61 6c 73  st and it is als
1bacf 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64  o not journalled
1bad0 20 77 68 65 6e 20 69 74 0a 2a 2a 20 69 73 20 65   when it.** is e
1bad1 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68  xtracted from th
1bad2 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20  e free-list and 
1bad3 72 65 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65  reused, then the
1bad4 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 0a 2a   original data.*
1bad5 2a 20 6d 61 79 20 62 65 20 6c 6f 73 74 2e 20 49  * may be lost. I
1bad6 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61  n the event of a
1bad7 20 72 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 6d 61   rollback, it ma
1bad8 79 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c  y not be possibl
1bad9 65 0a 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20  e.** to restore 
1bada 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
1badb 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e  its original con
1badc 66 69 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  figuration..**.*
1badd 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69  * The solution i
1bade 73 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70  s the BtShared.p
1badf 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65  HasContent bitve
1bae0 63 2e 20 57 68 65 6e 65 76 65 72 20 61 20 70 61  c. Whenever a pa
1bae1 67 65 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 64 20  ge is .** moved 
1bae2 74 6f 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65  to become a free
1bae3 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c  -list leaf page,
1bae4 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
1bae5 6e 67 20 62 69 74 20 69 73 0a 2a 2a 20 73 65 74  ng bit is.** set
1bae6 20 69 6e 20 74 68 65 20 62 69 74 76 65 63 2e 20   in the bitvec. 
1bae7 57 68 65 6e 65 76 65 72 20 61 20 6c 65 61 66 20  Whenever a leaf 
1bae8 70 61 67 65 20 69 73 20 65 78 74 72 61 63 74 65  page is extracte
1bae9 64 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d  d from the free-
1baea 6c 69 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a  list,.** optimiz
1baeb 61 74 69 6f 6e 20 32 20 61 62 6f 76 65 20 69 73  ation 2 above is
1baec 20 6f 6d 69 74 74 65 64 20 69 66 20 74 68 65 20   omitted if the 
1baed 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69  corresponding bi
1baee 74 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20  t is already.** 
1baef 73 65 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e  set in BtShared.
1baf0 70 48 61 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65  pHasContent. The
1baf1 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
1baf2 20 62 69 74 76 65 63 20 61 72 65 20 63 6c 65 61   bitvec are clea
1baf3 72 65 64 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e  red.** at the en
1baf4 64 20 6f 66 20 65 76 65 72 79 20 74 72 61 6e 73  d of every trans
1baf5 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  action..*/.stati
1baf6 63 20 69 6e 74 20 62 74 72 65 65 53 65 74 48 61  c int btreeSetHa
1baf7 73 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65  sContent(BtShare
1baf8 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e  d *pBt, Pgno pgn
1baf9 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  o){.  int rc = S
1bafa 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
1bafb 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e  !pBt->pHasConten
1bafc 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  t ){.    assert(
1bafd 20 70 67 6e 6f 3c 3d 70 42 74 2d 3e 6e 50 61 67   pgno<=pBt->nPag
1bafe 65 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 48  e );.    pBt->pH
1baff 61 73 43 6f 6e 74 65 6e 74 20 3d 20 73 71 6c 69  asContent = sqli
1bb00 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28  te3BitvecCreate(
1bb01 70 42 74 2d 3e 6e 50 61 67 65 29 3b 0a 20 20 20  pBt->nPage);.   
1bb02 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43   if( !pBt->pHasC
1bb03 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20  ontent ){.      
1bb04 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
1bb05 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  M;.    }.  }.  i
1bb06 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1bb07 20 26 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 65   && pgno<=sqlite
1bb08 33 42 69 74 76 65 63 53 69 7a 65 28 70 42 74 2d  3BitvecSize(pBt-
1bb09 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 20 29 7b  >pHasContent) ){
1bb0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1bb0b 33 42 69 74 76 65 63 53 65 74 28 70 42 74 2d 3e  3BitvecSet(pBt->
1bb0c 70 48 61 73 43 6f 6e 74 65 6e 74 2c 20 70 67 6e  pHasContent, pgn
1bb0d 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  o);.  }.  return
1bb0e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75   rc;.}../*.** Qu
1bb0f 65 72 79 20 74 68 65 20 42 74 53 68 61 72 65 64  ery the BtShared
1bb10 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 76 65 63  .pHasContent vec
1bb11 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  tor..**.** This 
1bb12 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
1bb13 65 64 20 77 68 65 6e 20 61 20 66 72 65 65 2d 6c  ed when a free-l
1bb14 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 69 73  ist leaf page is
1bb15 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
1bb16 65 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 66  e.** free-list f
1bb17 6f 72 20 72 65 75 73 65 2e 20 49 74 20 72 65 74  or reuse. It ret
1bb18 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 69 74  urns false if it
1bb19 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 74 72   is safe to retr
1bb1a 69 65 76 65 20 74 68 65 0a 2a 2a 20 70 61 67 65  ieve the.** page
1bb1b 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20   from the pager 
1bb1c 6c 61 79 65 72 20 77 69 74 68 20 74 68 65 20 27  layer with the '
1bb1d 6e 6f 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61 67  no-content' flag
1bb1e 20 73 65 74 2e 20 54 72 75 65 20 6f 74 68 65 72   set. True other
1bb1f 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  wise..*/.static 
1bb20 69 6e 74 20 62 74 72 65 65 47 65 74 48 61 73 43  int btreeGetHasC
1bb21 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 20  ontent(BtShared 
1bb22 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29  *pBt, Pgno pgno)
1bb23 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 20 3d 20  {.  Bitvec *p = 
1bb24 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74  pBt->pHasContent
1bb25 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26 26  ;.  return (p &&
1bb26 20 28 70 67 6e 6f 3e 73 71 6c 69 74 65 33 42 69   (pgno>sqlite3Bi
1bb27 74 76 65 63 53 69 7a 65 28 70 29 20 7c 7c 20 73  tvecSize(p) || s
1bb28 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
1bb29 28 70 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a  (p, pgno)));.}..
1bb2a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 28 64 65 73  /*.** Clear (des
1bb2b 74 72 6f 79 29 20 74 68 65 20 42 74 53 68 61 72  troy) the BtShar
1bb2c 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62  ed.pHasContent b
1bb2d 69 74 76 65 63 2e 20 54 68 69 73 20 73 68 6f 75  itvec. This shou
1bb2e 6c 64 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64  ld be.** invoked
1bb2f 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69   at the conclusi
1bb30 6f 6e 20 6f 66 20 65 61 63 68 20 77 72 69 74 65  on of each write
1bb31 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f  -transaction..*/
1bb32 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 74 72  .static void btr
1bb33 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e  eeClearHasConten
1bb34 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  t(BtShared *pBt)
1bb35 7b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65  {.  sqlite3Bitve
1bb36 63 44 65 73 74 72 6f 79 28 70 42 74 2d 3e 70 48  cDestroy(pBt->pH
1bb37 61 73 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 42  asContent);.  pB
1bb38 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d  t->pHasContent =
1bb39 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76   0;.}../*.** Sav
1bb3a 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75  e the current cu
1bb3b 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 6e  rsor position in
1bb3c 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 20 42   the variables B
1bb3d 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a  tCursor.nKey .**
1bb3e 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 70 4b   and BtCursor.pK
1bb3f 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 27 73  ey. The cursor's
1bb40 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 6f   state is set to
1bb41 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
1bb42 45 45 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  EEK..**.** The c
1bb43 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72  aller must ensur
1bb44 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  e that the curso
1bb45 72 20 69 73 20 76 61 6c 69 64 20 28 68 61 73 20  r is valid (has 
1bb46 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
1bb47 41 4c 49 44 29 0a 2a 2a 20 70 72 69 6f 72 20 74  ALID).** prior t
1bb48 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72  o calling this r
1bb49 6f 75 74 69 6e 65 2e 20 20 0a 2a 2f 0a 73 74 61  outine.  .*/.sta
1bb4a 74 69 63 20 69 6e 74 20 73 61 76 65 43 75 72 73  tic int saveCurs
1bb4b 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72  orPosition(BtCur
1bb4c 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e  sor *pCur){.  in
1bb4d 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
1bb4e 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70   CURSOR_VALID==p
1bb4f 43 75 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20  Cur->eState );. 
1bb50 20 61 73 73 65 72 74 28 20 30 3d 3d 70 43 75 72   assert( 0==pCur
1bb51 2d 3e 70 4b 65 79 20 29 3b 0a 20 20 61 73 73 65  ->pKey );.  asse
1bb52 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
1bb53 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 0a 20  utex(pCur) );.. 
1bb54 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1bb55 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20  eeKeySize(pCur, 
1bb56 26 70 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20  &pCur->nKey);.  
1bb57 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1bb58 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 4b 65 79  TE_OK );  /* Key
1bb59 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61  Size() cannot fa
1bb5a 69 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74  il */..  /* If t
1bb5b 68 69 73 20 69 73 20 61 6e 20 69 6e 74 4b 65 79  his is an intKey
1bb5c 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 74 68 65   table, then the
1bb5d 20 61 62 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42   above call to B
1bb5e 74 72 65 65 4b 65 79 53 69 7a 65 28 29 0a 20 20  treeKeySize().  
1bb5f 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20 69 6e  ** stores the in
1bb60 74 65 67 65 72 20 6b 65 79 20 69 6e 20 70 43 75  teger key in pCu
1bb61 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73  r->nKey. In this
1bb62 20 63 61 73 65 20 74 68 69 73 20 76 61 6c 75 65   case this value
1bb63 20 69 73 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61   is.  ** all tha
1bb64 74 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 4f  t is required. O
1bb65 74 68 65 72 77 69 73 65 2c 20 69 66 20 70 43 75  therwise, if pCu
1bb66 72 20 69 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e  r is not open on
1bb67 20 61 6e 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20   an intKey.  ** 
1bb68 74 61 62 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c  table, then mall
1bb69 6f 63 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64  oc space for and
1bb6a 20 73 74 6f 72 65 20 74 68 65 20 70 43 75 72 2d   store the pCur-
1bb6b 3e 6e 4b 65 79 20 62 79 74 65 73 20 6f 66 20 6b  >nKey bytes of k
1bb6c 65 79 20 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20  ey .  ** data.. 
1bb6d 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 70 43 75   */.  if( 0==pCu
1bb6e 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e  r->apPage[0]->in
1bb6f 74 4b 65 79 20 29 7b 0a 20 20 20 20 76 6f 69 64  tKey ){.    void
1bb70 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33   *pKey = sqlite3
1bb71 4d 61 6c 6c 6f 63 28 20 28 69 6e 74 29 70 43 75  Malloc( (int)pCu
1bb72 72 2d 3e 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69  r->nKey );.    i
1bb73 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 20  f( pKey ){.     
1bb74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1bb75 65 65 4b 65 79 28 70 43 75 72 2c 20 30 2c 20 28  eeKey(pCur, 0, (
1bb76 69 6e 74 29 70 43 75 72 2d 3e 6e 4b 65 79 2c 20  int)pCur->nKey, 
1bb77 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 69 66 28  pKey);.      if(
1bb78 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1bb79 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e  {.        pCur->
1bb7a 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20  pKey = pKey;.   
1bb7b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1bb7c 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
1bb7d 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Key);.      }.  
1bb7e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
1bb7f 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
1bb80 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73  ;.    }.  }.  as
1bb81 73 65 72 74 28 20 21 70 43 75 72 2d 3e 61 70 50  sert( !pCur->apP
1bb82 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c  age[0]->intKey |
1bb83 7c 20 21 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b  | !pCur->pKey );
1bb84 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
1bb85 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74  TE_OK ){.    int
1bb86 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   i;.    for(i=0;
1bb87 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b   i<=pCur->iPage;
1bb88 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c   i++){.      rel
1bb89 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61  easePage(pCur->a
1bb8a 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20  pPage[i]);.     
1bb8b 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d   pCur->apPage[i]
1bb8c 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
1bb8d 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31  pCur->iPage = -1
1bb8e 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61  ;.    pCur->eSta
1bb8f 74 65 20 3d 20 43 55 52 53 4f 52 5f 52 45 51 55  te = CURSOR_REQU
1bb90 49 52 45 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20  IRESEEK;.  }..  
1bb91 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c  invalidateOverfl
1bb92 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20  owCache(pCur);. 
1bb93 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1bb94 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 70 6f  *.** Save the po
1bb95 73 69 74 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63  sitions of all c
1bb96 75 72 73 6f 72 73 20 28 65 78 63 65 70 74 20 70  ursors (except p
1bb97 45 78 63 65 70 74 29 20 74 68 61 74 20 61 72 65  Except) that are
1bb98 20 6f 70 65 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20   open on.** the 
1bb99 74 61 62 6c 65 20 20 77 69 74 68 20 72 6f 6f 74  table  with root
1bb9a 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55 73 75  -page iRoot. Usu
1bb9b 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61  ally, this is ca
1bb9c 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f 72 65  lled just before
1bb9d 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78 63 65   cursor.** pExce
1bb9e 70 74 20 69 73 20 75 73 65 64 20 74 6f 20 6d 6f  pt is used to mo
1bb9f 64 69 66 79 20 74 68 65 20 74 61 62 6c 65 20 28  dify the table (
1bba0 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72  BtreeDelete() or
1bba1 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 29 2e   BtreeInsert()).
1bba2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
1bba3 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 42 74  aveAllCursors(Bt
1bba4 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e  Shared *pBt, Pgn
1bba5 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72 73 6f  o iRoot, BtCurso
1bba6 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20 20 42  r *pExcept){.  B
1bba7 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73  tCursor *p;.  as
1bba8 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1bba9 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
1bbaa 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
1bbab 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c 7c 20  ( pExcept==0 || 
1bbac 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d 70 42  pExcept->pBt==pB
1bbad 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74  t );.  for(p=pBt
1bbae 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d  ->pCursor; p; p=
1bbaf 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  p->pNext){.    i
1bbb0 66 28 20 70 21 3d 70 45 78 63 65 70 74 20 26 26  f( p!=pExcept &&
1bbb1 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d   (0==iRoot || p-
1bbb2 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74  >pgnoRoot==iRoot
1bbb3 29 20 26 26 20 0a 20 20 20 20 20 20 20 20 70 2d  ) && .        p-
1bbb4 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
1bbb5 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 69  VALID ){.      i
1bbb6 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75 72 73  nt rc = saveCurs
1bbb7 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b 0a 20  orPosition(p);. 
1bbb8 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f       if( SQLITE_
1bbb9 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20  OK!=rc ){.      
1bbba 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1bbbb 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1bbbc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1bbbd 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61  K;.}../*.** Clea
1bbbe 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75  r the current cu
1bbbf 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a  rsor position..*
1bbc0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1bbc1 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
1bbc2 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74  eeClearCursor(Bt
1bbc3 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
1bbc4 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
1bbc5 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
1bbc6 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
1bbc7 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20  e(pCur->pKey);. 
1bbc8 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b   pCur->pKey = 0;
1bbc9 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20  .  pCur->eState 
1bbca 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
1bbcb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68  ;.}../*.** In th
1bbcc 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 42 74  is version of Bt
1bbcd 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b 65 79 20  reeMoveto, pKey 
1bbce 69 73 20 61 20 70 61 63 6b 65 64 20 69 6e 64 65  is a packed inde
1bbcf 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73 75 63 68  x record.** such
1bbd0 20 61 73 20 69 73 20 67 65 6e 65 72 61 74 65 64   as is generated
1bbd1 20 62 79 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52   by the OP_MakeR
1bbd2 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 20 20 55  ecord opcode.  U
1bbd3 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20 72 65 63  npack the.** rec
1bbd4 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 63 61 6c  ord and then cal
1bbd5 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  l BtreeMovetoUnp
1bbd6 61 63 6b 65 64 28 29 20 74 6f 20 64 6f 20 74 68  acked() to do th
1bbd7 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 73 74 61 74 69  e work..*/.stati
1bbd8 63 20 69 6e 74 20 62 74 72 65 65 4d 6f 76 65 74  c int btreeMovet
1bbd9 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  o(.  BtCursor *p
1bbda 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 72 73  Cur,     /* Curs
1bbdb 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 62  or open on the b
1bbdc 74 72 65 65 20 74 6f 20 62 65 20 73 65 61 72 63  tree to be searc
1bbdd 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  hed */.  const v
1bbde 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20  oid *pKey,   /* 
1bbdf 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20 74 68  Packed key if th
1bbe0 65 20 62 74 72 65 65 20 69 73 20 61 6e 20 69 6e  e btree is an in
1bbe1 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65  dex */.  i64 nKe
1bbe2 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y,           /* 
1bbe3 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20  Integer key for 
1bbe4 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20 6f 66  tables.  Size of
1bbe5 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69 63 65   pKey for indice
1bbe6 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 2c  s */.  int bias,
1bbe7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69             /* Bi
1bbe8 61 73 20 73 65 61 72 63 68 20 74 6f 20 74 68 65  as search to the
1bbe9 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69   high end */.  i
1bbea 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 20  nt *pRes        
1bbeb 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 61 72     /* Write sear
1bbec 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  ch results here 
1bbed 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20  */.){.  int rc; 
1bbee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bbef 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63 6f 64     /* Status cod
1bbf0 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52  e */.  UnpackedR
1bbf1 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 20  ecord *pIdxKey; 
1bbf2 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e    /* Unpacked in
1bbf3 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 63 68 61  dex key */.  cha
1bbf4 72 20 61 53 70 61 63 65 5b 31 35 30 5d 3b 20 20  r aSpace[150];  
1bbf5 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20          /* Temp 
1bbf6 73 70 61 63 65 20 66 6f 72 20 70 49 64 78 4b 65  space for pIdxKe
1bbf7 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61 20 6d  y - to avoid a m
1bbf8 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20  alloc */.  char 
1bbf9 2a 70 46 72 65 65 20 3d 20 30 3b 0a 0a 20 20 69  *pFree = 0;..  i
1bbfa 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 61  f( pKey ){.    a
1bbfb 73 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36  ssert( nKey==(i6
1bbfc 34 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20  4)(int)nKey );. 
1bbfd 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c     pIdxKey = sql
1bbfe 69 74 65 33 56 64 62 65 41 6c 6c 6f 63 55 6e 70  ite3VdbeAllocUnp
1bbff 61 63 6b 65 64 52 65 63 6f 72 64 28 0a 20 20 20  ackedRecord(.   
1bc00 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 49       pCur->pKeyI
1bc01 6e 66 6f 2c 20 61 53 70 61 63 65 2c 20 73 69 7a  nfo, aSpace, siz
1bc02 65 6f 66 28 61 53 70 61 63 65 29 2c 20 26 70 46  eof(aSpace), &pF
1bc03 72 65 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69  ree.    );.    i
1bc04 66 28 20 70 49 64 78 4b 65 79 3d 3d 30 20 29 20  f( pIdxKey==0 ) 
1bc05 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1bc06 4d 45 4d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  MEM;.    sqlite3
1bc07 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b  VdbeRecordUnpack
1bc08 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2c  (pCur->pKeyInfo,
1bc09 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70 4b 65 79   (int)nKey, pKey
1bc0a 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 65  , pIdxKey);.  }e
1bc0b 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b 65 79  lse{.    pIdxKey
1bc0c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 20 3d   = 0;.  }.  rc =
1bc0d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
1bc0e 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 75 72  etoUnpacked(pCur
1bc0f 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 79 2c  , pIdxKey, nKey,
1bc10 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a 20 20   bias, pRes);.  
1bc11 69 66 28 20 70 46 72 65 65 20 29 7b 0a 20 20 20  if( pFree ){.   
1bc12 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
1bc13 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 64  Cur->pKeyInfo->d
1bc14 62 2c 20 70 46 72 65 65 29 3b 0a 20 20 7d 0a 20  b, pFree);.  }. 
1bc15 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1bc16 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 65  *.** Restore the
1bc17 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 70   cursor to the p
1bc18 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20 69  osition it was i
1bc19 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20 74  n (or as close t
1bc1a 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a 2a  o as possible).*
1bc1b 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73 6f  * when saveCurso
1bc1c 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73 20  rPosition() was 
1bc1d 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68 61  called. Note tha
1bc1e 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c 65  t this call dele
1bc1f 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76 65  tes the .** save
1bc20 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f 20  d position info 
1bc21 73 74 6f 72 65 64 20 62 79 20 73 61 76 65 43 75  stored by saveCu
1bc22 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c 20  rsorPosition(), 
1bc23 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a  so there can be.
1bc24 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 65  ** at most one e
1bc25 66 66 65 63 74 69 76 65 20 72 65 73 74 6f 72 65  ffective restore
1bc26 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29  CursorPosition()
1bc27 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63 68   call after each
1bc28 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72 50   .** saveCursorP
1bc29 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73 74  osition()..*/.st
1bc2a 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 52 65  atic int btreeRe
1bc2b 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74  storeCursorPosit
1bc2c 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ion(BtCursor *pC
1bc2d 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ur){.  int rc;. 
1bc2e 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
1bc2f 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
1bc30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
1bc31 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f  r->eState>=CURSO
1bc32 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b  R_REQUIRESEEK );
1bc33 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74  .  if( pCur->eSt
1bc34 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c  ate==CURSOR_FAUL
1bc35 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  T ){.    return 
1bc36 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a  pCur->skipNext;.
1bc37 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74 61    }.  pCur->eSta
1bc38 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41  te = CURSOR_INVA
1bc39 4c 49 44 3b 0a 20 20 72 63 20 3d 20 62 74 72 65  LID;.  rc = btre
1bc3a 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 43  eMoveto(pCur, pC
1bc3b 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d 3e  ur->pKey, pCur->
1bc3c 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d 3e  nKey, 0, &pCur->
1bc3d 73 6b 69 70 4e 65 78 74 29 3b 0a 20 20 69 66 28  skipNext);.  if(
1bc3e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1bc3f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
1bc40 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a  ee(pCur->pKey);.
1bc41 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d      pCur->pKey =
1bc42 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   0;.    assert( 
1bc43 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
1bc44 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43  RSOR_VALID || pC
1bc45 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
1bc46 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20  OR_INVALID );.  
1bc47 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1bc48 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f 72  ..#define restor
1bc49 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
1bc4a 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 74  p) \.  (p->eStat
1bc4b 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52  e>=CURSOR_REQUIR
1bc4c 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 20  ESEEK ? \.      
1bc4d 20 20 20 62 74 72 65 65 52 65 73 74 6f 72 65 43     btreeRestoreC
1bc4e 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29  ursorPosition(p)
1bc4f 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53 51   : \.         SQ
1bc50 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a 20  LITE_OK)../*.** 
1bc51 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65  Determine whethe
1bc52 72 20 6f 72 20 6e 6f 74 20 61 20 63 75 72 73 6f  r or not a curso
1bc53 72 20 68 61 73 20 6d 6f 76 65 64 20 66 72 6f 6d  r has moved from
1bc54 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 74   the position it
1bc55 0a 2a 2a 20 77 61 73 20 6c 61 73 74 20 70 6c 61  .** was last pla
1bc56 63 65 64 20 61 74 2e 20 20 43 75 72 73 6f 72 73  ced at.  Cursors
1bc57 20 63 61 6e 20 6d 6f 76 65 20 77 68 65 6e 20 74   can move when t
1bc58 68 65 20 72 6f 77 20 74 68 65 79 20 61 72 65 20  he row they are 
1bc59 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 61 74 20 69  pointing.** at i
1bc5a 73 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66 72  s deleted out fr
1bc5b 6f 6d 20 75 6e 64 65 72 20 74 68 65 6d 2e 0a 2a  om under them..*
1bc5c 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1bc5d 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72  e returns an err
1bc5e 6f 72 20 63 6f 64 65 20 69 66 20 73 6f 6d 65 74  or code if somet
1bc5f 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e  hing goes wrong.
1bc60 20 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72    The.** integer
1bc61 20 2a 70 48 61 73 4d 6f 76 65 64 20 69 73 20 73   *pHasMoved is s
1bc62 65 74 20 74 6f 20 6f 6e 65 20 69 66 20 74 68 65  et to one if the
1bc63 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 65   cursor has move
1bc64 64 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a  d and 0 if not..
1bc65 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1bc66 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1bc67 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 64  eeCursorHasMoved
1bc68 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
1bc69 20 69 6e 74 20 2a 70 48 61 73 4d 6f 76 65 64 29   int *pHasMoved)
1bc6a 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72  {.  int rc;..  r
1bc6b 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f  c = restoreCurso
1bc6c 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b  rPosition(pCur);
1bc6d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
1bc6e 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b   *pHasMoved = 1;
1bc6f 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
1bc70 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e    }.  if( pCur->
1bc71 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56  eState!=CURSOR_V
1bc72 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 73 6b  ALID || pCur->sk
1bc73 69 70 4e 65 78 74 21 3d 30 20 29 7b 0a 20 20 20  ipNext!=0 ){.   
1bc74 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b   *pHasMoved = 1;
1bc75 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70  .  }else{.    *p
1bc76 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a 20 20  HasMoved = 0;.  
1bc77 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
1bc78 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  E_OK;.}..#ifndef
1bc79 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1bc7a 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 69  OVACUUM./*.** Gi
1bc7b 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d 62 65  ven a page numbe
1bc7c 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72 20 64  r of a regular d
1bc7d 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 72 65  atabase page, re
1bc7e 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a  turn the page.**
1bc7f 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
1bc80 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65  pointer-map page
1bc81 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
1bc82 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  he entry for the
1bc83 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 20 6e  .** input page n
1bc84 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  umber..**.** Ret
1bc85 75 72 6e 20 30 20 28 6e 6f 74 20 61 20 76 61 6c  urn 0 (not a val
1bc86 69 64 20 70 61 67 65 29 20 66 6f 72 20 70 67 6e  id page) for pgn
1bc87 6f 3d 3d 31 20 73 69 6e 63 65 20 74 68 65 72 65  o==1 since there
1bc88 20 69 73 0a 2a 2a 20 6e 6f 20 70 6f 69 6e 74 65   is.** no pointe
1bc89 72 20 6d 61 70 20 61 73 73 6f 63 69 61 74 65 64  r map associated
1bc8a 20 77 69 74 68 20 70 61 67 65 20 31 2e 20 20 54   with page 1.  T
1bc8b 68 65 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65  he integrity_che
1bc8c 63 6b 20 6c 6f 67 69 63 0a 2a 2a 20 72 65 71 75  ck logic.** requ
1bc8d 69 72 65 73 20 74 68 61 74 20 70 74 72 6d 61 70  ires that ptrmap
1bc8e 50 61 67 65 6e 6f 28 2a 2c 31 29 21 3d 31 2e 0a  Pageno(*,1)!=1..
1bc8f 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70  */.static Pgno p
1bc90 74 72 6d 61 70 50 61 67 65 6e 6f 28 42 74 53 68  trmapPageno(BtSh
1bc91 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20  ared *pBt, Pgno 
1bc92 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 6e 50 61  pgno){.  int nPa
1bc93 67 65 73 50 65 72 4d 61 70 50 61 67 65 3b 0a 20  gesPerMapPage;. 
1bc94 20 50 67 6e 6f 20 69 50 74 72 4d 61 70 2c 20 72   Pgno iPtrMap, r
1bc95 65 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  et;.  assert( sq
1bc96 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1bc97 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
1bc98 20 20 69 66 28 20 70 67 6e 6f 3c 32 20 29 20 72    if( pgno<2 ) r
1bc99 65 74 75 72 6e 20 30 3b 0a 20 20 6e 50 61 67 65  eturn 0;.  nPage
1bc9a 73 50 65 72 4d 61 70 50 61 67 65 20 3d 20 28 70  sPerMapPage = (p
1bc9b 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35  Bt->usableSize/5
1bc9c 29 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d  )+1;.  iPtrMap =
1bc9d 20 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73   (pgno-2)/nPages
1bc9e 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 72 65  PerMapPage;.  re
1bc9f 74 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e 50 61  t = (iPtrMap*nPa
1bca0 67 65 73 50 65 72 4d 61 70 50 61 67 65 29 20 2b  gesPerMapPage) +
1bca1 20 32 3b 20 0a 20 20 69 66 28 20 72 65 74 3d 3d   2; .  if( ret==
1bca2 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
1bca3 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65  E(pBt) ){.    re
1bca4 74 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  t++;.  }.  retur
1bca5 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n ret;.}../*.** 
1bca6 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69  Write an entry i
1bca7 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20  nto the pointer 
1bca8 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  map..**.** This 
1bca9 72 6f 75 74 69 6e 65 20 75 70 64 61 74 65 73 20  routine updates 
1bcaa 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  the pointer map 
1bcab 65 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e  entry for page n
1bcac 75 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73  umber 'key'.** s
1bcad 6f 20 74 68 61 74 20 69 74 20 6d 61 70 73 20 74  o that it maps t
1bcae 6f 20 74 79 70 65 20 27 65 54 79 70 65 27 20 61  o type 'eType' a
1bcaf 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e  nd parent page n
1bcb0 75 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a  umber 'pgno'..**
1bcb1 0a 2a 2a 20 49 66 20 2a 70 52 43 20 69 73 20 69  .** If *pRC is i
1bcb2 6e 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72  nitially non-zer
1bcb3 6f 20 28 6e 6f 6e 2d 53 51 4c 49 54 45 5f 4f 4b  o (non-SQLITE_OK
1bcb4 29 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  ) then this rout
1bcb5 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f  ine is.** a no-o
1bcb6 70 2e 20 20 49 66 20 61 6e 20 65 72 72 6f 72 20  p.  If an error 
1bcb7 6f 63 63 75 72 73 2c 20 74 68 65 20 61 70 70 72  occurs, the appr
1bcb8 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f  opriate error co
1bcb9 64 65 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a  de is written.**
1bcba 20 69 6e 74 6f 20 2a 70 52 43 2e 0a 2a 2f 0a 73   into *pRC..*/.s
1bcbb 74 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61  tatic void ptrma
1bcbc 70 50 75 74 28 42 74 53 68 61 72 65 64 20 2a 70  pPut(BtShared *p
1bcbd 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38  Bt, Pgno key, u8
1bcbe 20 65 54 79 70 65 2c 20 50 67 6e 6f 20 70 61 72   eType, Pgno par
1bcbf 65 6e 74 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a  ent, int *pRC){.
1bcc0 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67    DbPage *pDbPag
1bcc1 65 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74  e;  /* The point
1bcc2 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20  er map page */. 
1bcc3 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20   u8 *pPtrmap;   
1bcc4 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65     /* The pointe
1bcc5 72 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a 20 20  r map data */.  
1bcc6 50 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20 20 20  Pgno iPtrmap;   
1bcc7 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72    /* The pointer
1bcc8 20 6d 61 70 20 70 61 67 65 20 6e 75 6d 62 65 72   map page number
1bcc9 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74   */.  int offset
1bcca 3b 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65  ;       /* Offse
1bccb 74 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70  t in pointer map
1bccc 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72   page */.  int r
1bccd 63 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  c;           /* 
1bcce 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  Return code from
1bccf 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f   subfunctions */
1bcd0 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72  ..  if( *pRC ) r
1bcd1 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74  eturn;..  assert
1bcd2 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1bcd3 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
1bcd4 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73   );.  /* The mas
1bcd5 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65  ter-journal page
1bcd6 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76   number must nev
1bcd7 65 72 20 62 65 20 75 73 65 64 20 61 73 20 61 20  er be used as a 
1bcd8 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65  pointer map page
1bcd9 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d   */.  assert( 0=
1bcda 3d 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70  =PTRMAP_ISPAGE(p
1bcdb 42 74 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  Bt, PENDING_BYTE
1bcdc 5f 50 41 47 45 28 70 42 74 29 29 20 29 3b 0a 0a  _PAGE(pBt)) );..
1bcdd 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61    assert( pBt->a
1bcde 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 69  utoVacuum );.  i
1bcdf 66 28 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20  f( key==0 ){.   
1bce0 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43   *pRC = SQLITE_C
1bce1 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
1bce2 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
1bce3 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f  Ptrmap = PTRMAP_
1bce4 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29  PAGENO(pBt, key)
1bce5 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1bce6 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50  PagerGet(pBt->pP
1bce7 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26  ager, iPtrmap, &
1bce8 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20  pDbPage);.  if( 
1bce9 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1bcea 0a 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a  .    *pRC = rc;.
1bceb 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
1bcec 20 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41    offset = PTRMA
1bced 50 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72  P_PTROFFSET(iPtr
1bcee 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28  map, key);.  if(
1bcef 20 6f 66 66 73 65 74 3c 30 20 29 7b 0a 20 20 20   offset<0 ){.   
1bcf0 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43   *pRC = SQLITE_C
1bcf1 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
1bcf2 20 67 6f 74 6f 20 70 74 72 6d 61 70 5f 65 78 69   goto ptrmap_exi
1bcf3 74 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  t;.  }.  assert(
1bcf4 20 6f 66 66 73 65 74 20 3c 3d 20 28 69 6e 74 29   offset <= (int)
1bcf5 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d  pBt->usableSize-
1bcf6 35 20 29 3b 0a 20 20 70 50 74 72 6d 61 70 20 3d  5 );.  pPtrmap =
1bcf7 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61   (u8 *)sqlite3Pa
1bcf8 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50 61  gerGetData(pDbPa
1bcf9 67 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70  ge);..  if( eTyp
1bcfa 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 65  e!=pPtrmap[offse
1bcfb 74 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28 26  t] || get4byte(&
1bcfc 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31  pPtrmap[offset+1
1bcfd 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 20  ])!=parent ){.  
1bcfe 20 20 54 52 41 43 45 28 28 22 50 54 52 4d 41 50    TRACE(("PTRMAP
1bcff 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 64  _UPDATE: %d->(%d
1bd00 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 54  ,%d)\n", key, eT
1bd01 79 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a 20  ype, parent));. 
1bd02 20 20 20 2a 70 52 43 3d 20 72 63 20 3d 20 73 71     *pRC= rc = sq
1bd03 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
1bd04 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66  pDbPage);.    if
1bd05 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1bd06 29 7b 0a 20 20 20 20 20 20 70 50 74 72 6d 61 70  ){.      pPtrmap
1bd07 5b 6f 66 66 73 65 74 5d 20 3d 20 65 54 79 70 65  [offset] = eType
1bd08 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65  ;.      put4byte
1bd09 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74  (&pPtrmap[offset
1bd0a 2b 31 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20 20  +1], parent);.  
1bd0b 20 20 7d 0a 20 20 7d 0a 0a 70 74 72 6d 61 70 5f    }.  }..ptrmap_
1bd0c 65 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 50  exit:.  sqlite3P
1bd0d 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67  agerUnref(pDbPag
1bd0e 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  e);.}../*.** Rea
1bd0f 64 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20  d an entry from 
1bd10 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e  the pointer map.
1bd11 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
1bd12 69 6e 65 20 72 65 74 72 69 65 76 65 73 20 74 68  ine retrieves th
1bd13 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e  e pointer map en
1bd14 74 72 79 20 66 6f 72 20 70 61 67 65 20 27 6b 65  try for page 'ke
1bd15 79 27 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74  y', writing.** t
1bd16 68 65 20 74 79 70 65 20 61 6e 64 20 70 61 72 65  he type and pare
1bd17 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74  nt page number t
1bd18 6f 20 2a 70 45 54 79 70 65 20 61 6e 64 20 2a 70  o *pEType and *p
1bd19 50 67 6e 6f 20 72 65 73 70 65 63 74 69 76 65 6c  Pgno respectivel
1bd1a 79 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63  y..** An error c
1bd1b 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
1bd1c 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65  if something goe
1bd1d 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69  s wrong, otherwi
1bd1e 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f  se SQLITE_OK..*/
1bd1f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d  .static int ptrm
1bd20 61 70 47 65 74 28 42 74 53 68 61 72 65 64 20 2a  apGet(BtShared *
1bd21 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75  pBt, Pgno key, u
1bd22 38 20 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f 20  8 *pEType, Pgno 
1bd23 2a 70 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67  *pPgno){.  DbPag
1bd24 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 2f 2a  e *pDbPage;   /*
1bd25 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   The pointer map
1bd26 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69   page */.  int i
1bd27 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a  Ptrmap;       /*
1bd28 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67   Pointer map pag
1bd29 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20  e index */.  u8 
1bd2a 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20  *pPtrmap;       
1bd2b 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70  /* Pointer map p
1bd2c 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e  age data */.  in
1bd2d 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20  t offset;       
1bd2e 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 65 6e   /* Offset of en
1bd2f 74 72 79 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d  try in pointer m
1bd30 61 70 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a  ap */.  int rc;.
1bd31 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1bd32 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
1bd33 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  t->mutex) );..  
1bd34 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50  iPtrmap = PTRMAP
1bd35 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79  _PAGENO(pBt, key
1bd36 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1bd37 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70  3PagerGet(pBt->p
1bd38 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20  Pager, iPtrmap, 
1bd39 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28  &pDbPage);.  if(
1bd3a 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 72 65   rc!=0 ){.    re
1bd3b 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70  turn rc;.  }.  p
1bd3c 50 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 73  Ptrmap = (u8 *)s
1bd3d 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
1bd3e 74 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 20  ta(pDbPage);..  
1bd3f 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f  offset = PTRMAP_
1bd40 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d 61  PTROFFSET(iPtrma
1bd41 70 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28 20 6f  p, key);.  if( o
1bd42 66 66 73 65 74 3c 30 20 29 7b 0a 20 20 20 20 73  ffset<0 ){.    s
1bd43 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
1bd44 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 72  (pDbPage);.    r
1bd45 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
1bd46 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20  RUPT_BKPT;.  }. 
1bd47 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 20   assert( offset 
1bd48 3c 3d 20 28 69 6e 74 29 70 42 74 2d 3e 75 73 61  <= (int)pBt->usa
1bd49 62 6c 65 53 69 7a 65 2d 35 20 29 3b 0a 20 20 61  bleSize-5 );.  a
1bd4a 73 73 65 72 74 28 20 70 45 54 79 70 65 21 3d 30  ssert( pEType!=0
1bd4b 20 29 3b 0a 20 20 2a 70 45 54 79 70 65 20 3d 20   );.  *pEType = 
1bd4c 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 3b  pPtrmap[offset];
1bd4d 0a 20 20 69 66 28 20 70 50 67 6e 6f 20 29 20 2a  .  if( pPgno ) *
1bd4e 70 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65  pPgno = get4byte
1bd4f 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74  (&pPtrmap[offset
1bd50 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  +1]);..  sqlite3
1bd51 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61  PagerUnref(pDbPa
1bd52 67 65 29 3b 0a 20 20 69 66 28 20 2a 70 45 54 79  ge);.  if( *pETy
1bd53 70 65 3c 31 20 7c 7c 20 2a 70 45 54 79 70 65 3e  pe<1 || *pEType>
1bd54 35 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  5 ) return SQLIT
1bd55 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
1bd56 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1bd57 4f 4b 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20  OK;.}..#else /* 
1bd58 69 66 20 64 65 66 69 6e 65 64 20 53 51 4c 49 54  if defined SQLIT
1bd59 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
1bd5a 4d 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 70  M */.  #define p
1bd5b 74 72 6d 61 70 50 75 74 28 77 2c 78 2c 79 2c 7a  trmapPut(w,x,y,z
1bd5c 2c 72 63 29 0a 20 20 23 64 65 66 69 6e 65 20 70  ,rc).  #define p
1bd5d 74 72 6d 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a  trmapGet(w,x,y,z
1bd5e 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64  ) SQLITE_OK.  #d
1bd5f 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 74 4f  efine ptrmapPutO
1bd60 76 66 6c 50 74 72 28 78 2c 20 79 2c 20 72 63 29  vflPtr(x, y, rc)
1bd61 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47  .#endif../*.** G
1bd62 69 76 65 6e 20 61 20 62 74 72 65 65 20 70 61 67  iven a btree pag
1bd63 65 20 61 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64  e and a cell ind
1bd64 65 78 20 28 30 20 6d 65 61 6e 73 20 74 68 65 20  ex (0 means the 
1bd65 66 69 72 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a  first cell on.**
1bd66 20 74 68 65 20 70 61 67 65 2c 20 31 20 6d 65 61   the page, 1 mea
1bd67 6e 73 20 74 68 65 20 73 65 63 6f 6e 64 20 63 65  ns the second ce
1bd68 6c 6c 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68  ll, and so forth
1bd69 29 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  ) return a point
1bd6a 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c  er.** to the cel
1bd6b 6c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a  l content..**.**
1bd6c 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f   This routine wo
1bd6d 72 6b 73 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67  rks only for pag
1bd6e 65 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 63  es that do not c
1bd6f 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20  ontain overflow 
1bd70 63 65 6c 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  cells..*/.#defin
1bd71 65 20 66 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20  e findCell(P,I) 
1bd72 5c 0a 20 20 28 28 50 29 2d 3e 61 44 61 74 61 20  \.  ((P)->aData 
1bd73 2b 20 28 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65  + ((P)->maskPage
1bd74 20 26 20 67 65 74 32 62 79 74 65 28 26 28 50 29   & get2byte(&(P)
1bd75 2d 3e 61 43 65 6c 6c 49 64 78 5b 32 2a 28 49 29  ->aCellIdx[2*(I)
1bd76 5d 29 29 29 0a 23 64 65 66 69 6e 65 20 66 69 6e  ]))).#define fin
1bd77 64 43 65 6c 6c 76 32 28 44 2c 4d 2c 4f 2c 49 29  dCellv2(D,M,O,I)
1bd78 20 28 44 2b 28 4d 26 67 65 74 32 62 79 74 65 28   (D+(M&get2byte(
1bd79 44 2b 28 4f 2b 32 2a 28 49 29 29 29 29 29 0a 0a  D+(O+2*(I)))))..
1bd7a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f  ./*.** This a mo
1bd7b 72 65 20 63 6f 6d 70 6c 65 78 20 76 65 72 73 69  re complex versi
1bd7c 6f 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29  on of findCell()
1bd7d 20 74 68 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a   that works for.
1bd7e 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 64 6f  ** pages that do
1bd7f 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f   contain overflo
1bd80 77 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74  w cells..*/.stat
1bd81 69 63 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66  ic u8 *findOverf
1bd82 6c 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20  lowCell(MemPage 
1bd83 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c  *pPage, int iCel
1bd84 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61  l){.  int i;.  a
1bd85 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1bd86 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
1bd87 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
1bd88 20 20 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e    for(i=pPage->n
1bd89 4f 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30  Overflow-1; i>=0
1bd8a 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20  ; i--){.    int 
1bd8b 6b 3b 0a 20 20 20 20 6b 20 3d 20 70 50 61 67 65  k;.    k = pPage
1bd8c 2d 3e 61 69 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20  ->aiOvfl[i];.   
1bd8d 20 69 66 28 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b   if( k<=iCell ){
1bd8e 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 69 43  .      if( k==iC
1bd8f 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ell ){.        r
1bd90 65 74 75 72 6e 20 70 50 61 67 65 2d 3e 61 70 4f  eturn pPage->apO
1bd91 76 66 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a  vfl[i];.      }.
1bd92 20 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20        iCell--;. 
1bd93 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
1bd94 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  n findCell(pPage
1bd95 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a  , iCell);.}../*.
1bd96 2a 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20  ** Parse a cell 
1bd97 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e  content block an
1bd98 64 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65  d fill in the Ce
1bd99 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  llInfo structure
1bd9a 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20  .  There.** are 
1bd9b 74 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  two versions of 
1bd9c 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20  this function.  
1bd9d 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29  btreeParseCell()
1bd9e 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 6c   takes a .** cel
1bd9f 6c 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 73  l index as the s
1bda0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 61  econd argument a
1bda1 6e 64 20 62 74 72 65 65 50 61 72 73 65 43 65 6c  nd btreeParseCel
1bda2 6c 50 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73  lPtr() .** takes
1bda3 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1bda4 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65  e body of the ce
1bda5 6c 6c 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64  ll as its second
1bda6 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
1bda7 20 57 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c   Within this fil
1bda8 65 2c 20 74 68 65 20 70 61 72 73 65 43 65 6c 6c  e, the parseCell
1bda9 28 29 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20  () macro can be 
1bdaa 63 61 6c 6c 65 64 20 69 6e 73 74 65 61 64 20 6f  called instead o
1bdab 66 0a 2a 2a 20 62 74 72 65 65 50 61 72 73 65 43  f.** btreeParseC
1bdac 65 6c 6c 50 74 72 28 29 2e 20 55 73 69 6e 67 20  ellPtr(). Using 
1bdad 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20  some compilers, 
1bdae 74 68 69 73 20 77 69 6c 6c 20 62 65 20 66 61 73  this will be fas
1bdaf 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ter..*/.static v
1bdb0 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 43 65  oid btreeParseCe
1bdb1 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65  llPtr(.  MemPage
1bdb2 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20   *pPage,        
1bdb3 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e   /* Page contain
1bdb4 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a  ing the cell */.
1bdb5 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20    u8 *pCell,    
1bdb6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
1bdb7 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c  nter to the cell
1bdb8 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c   text. */.  Cell
1bdb9 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20  Info *pInfo     
1bdba 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74      /* Fill in t
1bdbb 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f  his structure */
1bdbc 0a 29 7b 0a 20 20 75 31 36 20 6e 3b 20 20 20 20  .){.  u16 n;    
1bdbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1bdbe 20 4e 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e   Number bytes in
1bdbf 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65   cell content he
1bdc0 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50  ader */.  u32 nP
1bdc1 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20  ayload;         
1bdc2 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1bdc3 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79  ytes of cell pay
1bdc4 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72  load */..  asser
1bdc5 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1bdc6 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
1bdc7 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70  ->mutex) );..  p
1bdc8 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43  Info->pCell = pC
1bdc9 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ell;.  assert( p
1bdca 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c  Page->leaf==0 ||
1bdcb 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20   pPage->leaf==1 
1bdcc 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e  );.  n = pPage->
1bdcd 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20  childPtrSize;.  
1bdce 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70  assert( n==4-4*p
1bdcf 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20  Page->leaf );.  
1bdd0 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  if( pPage->intKe
1bdd1 79 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61  y ){.    if( pPa
1bdd2 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20  ge->hasData ){. 
1bdd3 20 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72       n += getVar
1bdd4 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c  int32(&pCell[n],
1bdd5 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20   nPayload);.    
1bdd6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61  }else{.      nPa
1bdd7 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d  yload = 0;.    }
1bdd8 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72  .    n += getVar
1bdd9 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28  int(&pCell[n], (
1bdda 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65  u64*)&pInfo->nKe
1bddb 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e  y);.    pInfo->n
1bddc 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b  Data = nPayload;
1bddd 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49  .  }else{.    pI
1bdde 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a  nfo->nData = 0;.
1bddf 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69      n += getVari
1bde0 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20  nt32(&pCell[n], 
1bde1 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70  nPayload);.    p
1bde2 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61  Info->nKey = nPa
1bde3 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e  yload;.  }.  pIn
1bde4 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e  fo->nPayload = n
1bde5 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f  Payload;.  pInfo
1bde6 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20  ->nHeader = n;. 
1bde7 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 79 6c   testcase( nPayl
1bde8 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c  oad==pPage->maxL
1bde9 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 61  ocal );.  testca
1bdea 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50  se( nPayload==pP
1bdeb 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20  age->maxLocal+1 
1bdec 29 3b 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 28  );.  if( likely(
1bded 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d  nPayload<=pPage-
1bdee 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20 20  >maxLocal) ){.  
1bdef 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65    /* This is the
1bdf0 20 28 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63   (easy) common c
1bdf1 61 73 65 20 77 68 65 72 65 20 74 68 65 20 65 6e  ase where the en
1bdf2 74 69 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74  tire payload fit
1bdf3 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20  s.    ** on the 
1bdf4 6c 6f 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20  local page.  No 
1bdf5 6f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75  overflow is requ
1bdf6 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ired..    */.   
1bdf7 20 69 66 28 20 28 70 49 6e 66 6f 2d 3e 6e 53 69   if( (pInfo->nSi
1bdf8 7a 65 20 3d 20 28 75 31 36 29 28 6e 2b 6e 50 61  ze = (u16)(n+nPa
1bdf9 79 6c 6f 61 64 29 29 3c 34 20 29 20 70 49 6e 66  yload))<4 ) pInf
1bdfa 6f 2d 3e 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20  o->nSize = 4;.  
1bdfb 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20    pInfo->nLocal 
1bdfc 3d 20 28 75 31 36 29 6e 50 61 79 6c 6f 61 64 3b  = (u16)nPayload;
1bdfd 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65  .    pInfo->iOve
1bdfe 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 7d 65 6c  rflow = 0;.  }el
1bdff 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  se{.    /* If th
1be00 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 6e  e payload will n
1be01 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 6c  ot fit completel
1be02 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70  y on the local p
1be03 61 67 65 2c 20 77 65 20 68 61 76 65 0a 20 20 20  age, we have.   
1be04 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f   ** to decide ho
1be05 77 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20  w much to store 
1be06 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20  locally and how 
1be07 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e  much to spill on
1be08 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c  to.    ** overfl
1be09 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 73  ow pages.  The s
1be0a 74 72 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69  trategy is to mi
1be0b 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e  nimize the amoun
1be0c 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 20  t of unused.    
1be0d 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 72  ** space on over
1be0e 66 6c 6f 77 20 70 61 67 65 73 20 77 68 69 6c 65  flow pages while
1be0f 20 6b 65 65 70 69 6e 67 20 74 68 65 20 61 6d 6f   keeping the amo
1be10 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 6f  unt of local sto
1be11 72 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 62  rage.    ** in b
1be12 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20  etween minLocal 
1be13 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20  and maxLocal..  
1be14 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 6e    **.    ** Warn
1be15 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 74  ing:  changing t
1be16 68 65 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 20  he way overflow 
1be17 70 61 79 6c 6f 61 64 20 69 73 20 64 69 73 74 72  payload is distr
1be18 69 62 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 20  ibuted in any.  
1be19 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 65    ** way will re
1be1a 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6d  sult in an incom
1be1b 70 61 74 69 62 6c 65 20 66 69 6c 65 20 66 6f 72  patible file for
1be1c 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  mat..    */.    
1be1d 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f  int minLocal;  /
1be1e 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74  * Minimum amount
1be1f 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64   of payload held
1be20 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20   locally */.    
1be21 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f  int maxLocal;  /
1be22 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74  * Maximum amount
1be23 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64   of payload held
1be24 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20   locally */.    
1be25 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 20 2f  int surplus;   /
1be26 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f  * Overflow paylo
1be27 61 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  ad available for
1be28 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 2a   local storage *
1be29 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20  /..    minLocal 
1be2a 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61  = pPage->minLoca
1be2b 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20  l;.    maxLocal 
1be2c 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61  = pPage->maxLoca
1be2d 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75 73 20 3d  l;.    surplus =
1be2e 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 61   minLocal + (nPa
1be2f 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c  yload - minLocal
1be30 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  )%(pPage->pBt->u
1be31 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a  sableSize - 4);.
1be32 20 20 20 20 74 65 73 74 63 61 73 65 28 20 73 75      testcase( su
1be33 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20  rplus==maxLocal 
1be34 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
1be35 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63   surplus==maxLoc
1be36 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20  al+1 );.    if( 
1be37 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f  surplus <= maxLo
1be38 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70 49 6e  cal ){.      pIn
1be39 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31  fo->nLocal = (u1
1be3a 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d  6)surplus;.    }
1be3b 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 66  else{.      pInf
1be3c 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36  o->nLocal = (u16
1be3d 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d  )minLocal;.    }
1be3e 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65  .    pInfo->iOve
1be3f 72 66 6c 6f 77 20 3d 20 28 75 31 36 29 28 70 49  rflow = (u16)(pI
1be40 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29  nfo->nLocal + n)
1be41 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69  ;.    pInfo->nSi
1be42 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 65  ze = pInfo->iOve
1be43 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d  rflow + 4;.  }.}
1be44 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65 43 65  .#define parseCe
1be45 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c  ll(pPage, iCell,
1be46 20 70 49 6e 66 6f 29 20 5c 0a 20 20 62 74 72 65   pInfo) \.  btre
1be47 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70  eParseCellPtr((p
1be48 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28  Page), findCell(
1be49 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29  (pPage), (iCell)
1be4a 29 2c 20 28 70 49 6e 66 6f 29 29 0a 73 74 61 74  ), (pInfo)).stat
1be4b 69 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72  ic void btreePar
1be4c 73 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67  seCell(.  MemPag
1be4d 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20  e *pPage,       
1be4e 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69    /* Page contai
1be4f 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f  ning the cell */
1be50 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20  .  int iCell,   
1be51 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1be52 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46  e cell index.  F
1be53 69 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a  irst cell is 0 *
1be54 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49  /.  CellInfo *pI
1be55 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46  nfo         /* F
1be56 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75  ill in this stru
1be57 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61  cture */.){.  pa
1be58 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  rseCell(pPage, i
1be59 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a  Cell, pInfo);.}.
1be5a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  ./*.** Compute t
1be5b 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
1be5c 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20  of bytes that a 
1be5d 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68  Cell needs in th
1be5e 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61  e cell.** data a
1be5f 72 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65  rea of the btree
1be60 2d 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75  -page.  The retu
1be61 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64  rn number includ
1be62 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64  es the cell.** d
1be63 61 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74  ata header and t
1be64 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64  he local payload
1be65 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76  , but not any ov
1be66 65 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a  erflow page or.*
1be67 2a 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64  * the space used
1be68 20 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69   by the cell poi
1be69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  nter..*/.static 
1be6a 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  u16 cellSizePtr(
1be6b 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
1be6c 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38  u8 *pCell){.  u8
1be6d 20 2a 70 49 74 65 72 20 3d 20 26 70 43 65 6c 6c   *pIter = &pCell
1be6e 5b 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72  [pPage->childPtr
1be6f 53 69 7a 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69  Size];.  u32 nSi
1be70 7a 65 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ze;..#ifdef SQLI
1be71 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 54 68  TE_DEBUG.  /* Th
1be72 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
1be73 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
1be74 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20  n should always 
1be75 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 0a 20  be the same as. 
1be76 20 2a 2a 20 74 68 65 20 28 43 65 6c 6c 49 6e 66   ** the (CellInf
1be77 6f 2e 6e 53 69 7a 65 29 20 76 61 6c 75 65 20 66  o.nSize) value f
1be78 6f 75 6e 64 20 62 79 20 64 6f 69 6e 67 20 61 20  ound by doing a 
1be79 66 75 6c 6c 20 70 61 72 73 65 20 6f 66 20 74 68  full parse of th
1be7a 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20  e.  ** cell. If 
1be7b 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20  SQLITE_DEBUG is 
1be7c 64 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65  defined, an asse
1be7d 72 74 28 29 20 61 74 20 74 68 65 20 62 6f 74 74  rt() at the bott
1be7e 6f 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20  om of.  ** this 
1be7f 66 75 6e 63 74 69 6f 6e 20 76 65 72 69 66 69 65  function verifie
1be80 73 20 74 68 61 74 20 74 68 69 73 20 69 6e 76 61  s that this inva
1be81 72 69 61 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f  riant is not vio
1be82 6c 61 74 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c  lated. */.  Cell
1be83 49 6e 66 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a  Info debuginfo;.
1be84 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c    btreeParseCell
1be85 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c  Ptr(pPage, pCell
1be86 2c 20 26 64 65 62 75 67 69 6e 66 6f 29 3b 0a 23  , &debuginfo);.#
1be87 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70 50 61  endif..  if( pPa
1be88 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20  ge->intKey ){.  
1be89 20 20 75 38 20 2a 70 45 6e 64 3b 0a 20 20 20 20    u8 *pEnd;.    
1be8a 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61  if( pPage->hasDa
1be8b 74 61 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65  ta ){.      pIte
1be8c 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32  r += getVarint32
1be8d 28 70 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0a  (pIter, nSize);.
1be8e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1be8f 20 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20   nSize = 0;.    
1be90 7d 0a 0a 20 20 20 20 2f 2a 20 70 49 74 65 72 20  }..    /* pIter 
1be91 6e 6f 77 20 70 6f 69 6e 74 73 20 61 74 20 74 68  now points at th
1be92 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  e 64-bit integer
1be93 20 6b 65 79 20 76 61 6c 75 65 2c 20 61 20 76 61   key value, a va
1be94 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 0a 20  riable length . 
1be95 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 54     ** integer. T
1be96 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
1be97 63 6b 20 6d 6f 76 65 73 20 70 49 74 65 72 20 74  ck moves pIter t
1be98 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 66  o point at the f
1be99 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 2a 2a  irst byte.    **
1be9a 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
1be9b 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20   the key value. 
1be9c 2a 2f 0a 20 20 20 20 70 45 6e 64 20 3d 20 26 70  */.    pEnd = &p
1be9d 49 74 65 72 5b 39 5d 3b 0a 20 20 20 20 77 68 69  Iter[9];.    whi
1be9e 6c 65 28 20 28 2a 70 49 74 65 72 2b 2b 29 26 30  le( (*pIter++)&0
1be9f 78 38 30 20 26 26 20 70 49 74 65 72 3c 70 45 6e  x80 && pIter<pEn
1bea0 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  d );.  }else{.  
1bea1 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61    pIter += getVa
1bea2 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53  rint32(pIter, nS
1bea3 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 74 65 73  ize);.  }..  tes
1bea4 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50  tcase( nSize==pP
1bea5 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b  age->maxLocal );
1bea6 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69  .  testcase( nSi
1bea7 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f  ze==pPage->maxLo
1bea8 63 61 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e  cal+1 );.  if( n
1bea9 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c  Size>pPage->maxL
1beaa 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20  ocal ){.    int 
1beab 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65  minLocal = pPage
1beac 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20  ->minLocal;.    
1bead 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c  nSize = minLocal
1beae 20 2b 20 28 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c   + (nSize - minL
1beaf 6f 63 61 6c 29 20 25 20 28 70 50 61 67 65 2d 3e  ocal) % (pPage->
1beb0 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
1beb1 2d 20 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61  - 4);.    testca
1beb2 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65  se( nSize==pPage
1beb3 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20  ->maxLocal );.  
1beb4 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a    testcase( nSiz
1beb5 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63  e==pPage->maxLoc
1beb6 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20  al+1 );.    if( 
1beb7 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78  nSize>pPage->max
1beb8 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 6e  Local ){.      n
1beb9 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b  Size = minLocal;
1beba 0a 20 20 20 20 7d 0a 20 20 20 20 6e 53 69 7a 65  .    }.    nSize
1bebb 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 6e 53 69   += 4;.  }.  nSi
1bebc 7a 65 20 2b 3d 20 28 75 33 32 29 28 70 49 74 65  ze += (u32)(pIte
1bebd 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f  r - pCell);..  /
1bebe 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69  * The minimum si
1bebf 7a 65 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20 69  ze of any cell i
1bec0 73 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20  s 4 bytes. */.  
1bec1 69 66 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a 20  if( nSize<4 ){. 
1bec2 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20     nSize = 4;.  
1bec3 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69  }..  assert( nSi
1bec4 7a 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53  ze==debuginfo.nS
1bec5 69 7a 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ize );.  return 
1bec6 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 0a 23  (u16)nSize;.}..#
1bec7 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
1bec8 55 47 0a 2f 2a 20 54 68 69 73 20 76 61 72 69 61  UG./* This varia
1bec9 74 69 6f 6e 20 6f 6e 20 63 65 6c 6c 53 69 7a 65  tion on cellSize
1beca 50 74 72 28 29 20 69 73 20 75 73 65 64 20 69 6e  Ptr() is used in
1becb 73 69 64 65 20 6f 66 20 61 73 73 65 72 74 28 29  side of assert()
1becc 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 6f   statements.** o
1becd 6e 6c 79 2e 20 2a 2f 0a 73 74 61 74 69 63 20 75  nly. */.static u
1bece 31 36 20 63 65 6c 6c 53 69 7a 65 28 4d 65 6d 50  16 cellSize(MemP
1becf 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20  age *pPage, int 
1bed0 69 43 65 6c 6c 29 7b 0a 20 20 72 65 74 75 72 6e  iCell){.  return
1bed1 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
1bed2 67 65 2c 20 66 69 6e 64 43 65 6c 6c 28 70 50 61  ge, findCell(pPa
1bed3 67 65 2c 20 69 43 65 6c 6c 29 29 3b 0a 7d 0a 23  ge, iCell));.}.#
1bed4 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
1bed5 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
1bed6 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 74  ACUUM./*.** If t
1bed7 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c 2c 20 70  he cell pCell, p
1bed8 61 72 74 20 6f 66 20 70 61 67 65 20 70 50 61 67  art of page pPag
1bed9 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69  e contains a poi
1beda 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76  nter.** to an ov
1bedb 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73  erflow page, ins
1bedc 65 72 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 74  ert an entry int
1bedd 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  o the pointer-ma
1bede 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6f 76 65  p.** for the ove
1bedf 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a 73  rflow page..*/.s
1bee0 74 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61  tatic void ptrma
1bee1 70 50 75 74 4f 76 66 6c 50 74 72 28 4d 65 6d 50  pPutOvflPtr(MemP
1bee2 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a  age *pPage, u8 *
1bee3 70 43 65 6c 6c 2c 20 69 6e 74 20 2a 70 52 43 29  pCell, int *pRC)
1bee4 7b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66  {.  CellInfo inf
1bee5 6f 3b 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20  o;.  if( *pRC ) 
1bee6 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
1bee7 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b 0a 20 20  ( pCell!=0 );.  
1bee8 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  btreeParseCellPt
1bee9 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20  r(pPage, pCell, 
1beea 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 74  &info);.  assert
1beeb 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70  ( (info.nData+(p
1beec 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69  Page->intKey?0:i
1beed 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66 6f  nfo.nKey))==info
1beee 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 69  .nPayload );.  i
1beef 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  f( info.iOverflo
1bef0 77 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f 76  w ){.    Pgno ov
1bef1 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  fl = get4byte(&p
1bef2 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66  Cell[info.iOverf
1bef3 6c 6f 77 5d 29 3b 0a 20 20 20 20 70 74 72 6d 61  low]);.    ptrma
1bef4 70 50 75 74 28 70 50 61 67 65 2d 3e 70 42 74 2c  pPut(pPage->pBt,
1bef5 20 6f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56   ovfl, PTRMAP_OV
1bef6 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d 3e  ERFLOW1, pPage->
1bef7 70 67 6e 6f 2c 20 70 52 43 29 3b 0a 20 20 7d 0a  pgno, pRC);.  }.
1bef8 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  }.#endif.../*.**
1bef9 20 44 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20   Defragment the 
1befa 70 61 67 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c  page given.  All
1befb 20 43 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64   Cells are moved
1befc 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f   to the.** end o
1befd 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 61  f the page and a
1befe 6c 6c 20 66 72 65 65 20 73 70 61 63 65 20 69 73  ll free space is
1beff 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20   collected into 
1bf00 6f 6e 65 0a 2a 2a 20 62 69 67 20 46 72 65 65 42  one.** big FreeB
1bf01 6c 6b 20 74 68 61 74 20 6f 63 63 75 72 73 20 69  lk that occurs i
1bf02 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 68 65  n between the he
1bf03 61 64 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a  ader and cell.**
1bf04 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 61   pointer array a
1bf05 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  nd the cell cont
1bf06 65 6e 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 61  ent area..*/.sta
1bf07 74 69 63 20 69 6e 74 20 64 65 66 72 61 67 6d 65  tic int defragme
1bf08 6e 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a  ntPage(MemPage *
1bf09 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b  pPage){.  int i;
1bf0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bf0b 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
1bf0c 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63  nter */.  int pc
1bf0d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1bf0e 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
1bf0f 6f 66 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 2a  of a i-th cell *
1bf10 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20  /.  int hdr;    
1bf11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1bf12 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20  * Offset to the 
1bf13 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20  page header */. 
1bf14 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20   int size;      
1bf15 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1bf16 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f  ize of a cell */
1bf17 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a  .  int usableSiz
1bf18 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
1bf19 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c   Number of usabl
1bf1a 65 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67  e bytes on a pag
1bf1b 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f  e */.  int cellO
1bf1c 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20  ffset;          
1bf1d 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74    /* Offset to t
1bf1e 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  he cell pointer 
1bf1f 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63  array */.  int c
1bf20 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  brk;            
1bf21 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
1bf22 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  to the cell cont
1bf23 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e  ent area */.  in
1bf24 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20  t nCell;        
1bf25 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1bf26 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74  er of cells on t
1bf27 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73  he page */.  uns
1bf28 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61  igned char *data
1bf29 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  ;       /* The p
1bf2a 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e  age data */.  un
1bf2b 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d  signed char *tem
1bf2c 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70  p;       /* Temp
1bf2d 20 61 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63   area for cell c
1bf2e 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  ontent */.  int 
1bf2f 69 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 20  iCellFirst;     
1bf30 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
1bf31 61 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 69  allowable cell i
1bf32 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43  ndex */.  int iC
1bf33 65 6c 6c 4c 61 73 74 3b 20 20 20 20 20 20 20 20  ellLast;        
1bf34 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73       /* Last pos
1bf35 73 69 62 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78  sible cell index
1bf36 20 2a 2f 0a 0a 0a 20 20 61 73 73 65 72 74 28 20   */...  assert( 
1bf37 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
1bf38 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
1bf39 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73  DbPage) );.  ass
1bf3a 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21  ert( pPage->pBt!
1bf3b 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1bf3c 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
1bf3d 6c 65 53 69 7a 65 20 3c 3d 20 53 51 4c 49 54 45  leSize <= SQLITE
1bf3e 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 29  _MAX_PAGE_SIZE )
1bf3f 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
1bf40 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20  e->nOverflow==0 
1bf41 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
1bf42 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1bf43 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
1bf44 78 29 20 29 3b 0a 20 20 74 65 6d 70 20 3d 20 73  x) );.  temp = s
1bf45 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70 53  qlite3PagerTempS
1bf46 70 61 63 65 28 70 50 61 67 65 2d 3e 70 42 74 2d  pace(pPage->pBt-
1bf47 3e 70 50 61 67 65 72 29 3b 0a 20 20 64 61 74 61  >pPager);.  data
1bf48 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b   = pPage->aData;
1bf49 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e  .  hdr = pPage->
1bf4a 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 63 65 6c  hdrOffset;.  cel
1bf4b 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d  lOffset = pPage-
1bf4c 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 6e  >cellOffset;.  n
1bf4d 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43  Cell = pPage->nC
1bf4e 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  ell;.  assert( n
1bf4f 43 65 6c 6c 3d 3d 67 65 74 32 62 79 74 65 28 26  Cell==get2byte(&
1bf50 64 61 74 61 5b 68 64 72 2b 33 5d 29 20 29 3b 0a  data[hdr+3]) );.
1bf51 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70    usableSize = p
1bf52 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
1bf53 65 53 69 7a 65 3b 0a 20 20 63 62 72 6b 20 3d 20  eSize;.  cbrk = 
1bf54 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
1bf55 64 72 2b 35 5d 29 3b 0a 20 20 6d 65 6d 63 70 79  dr+5]);.  memcpy
1bf56 28 26 74 65 6d 70 5b 63 62 72 6b 5d 2c 20 26 64  (&temp[cbrk], &d
1bf57 61 74 61 5b 63 62 72 6b 5d 2c 20 75 73 61 62 6c  ata[cbrk], usabl
1bf58 65 53 69 7a 65 20 2d 20 63 62 72 6b 29 3b 0a 20  eSize - cbrk);. 
1bf59 20 63 62 72 6b 20 3d 20 75 73 61 62 6c 65 53 69   cbrk = usableSi
1bf5a 7a 65 3b 0a 20 20 69 43 65 6c 6c 46 69 72 73 74  ze;.  iCellFirst
1bf5b 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20   = cellOffset + 
1bf5c 32 2a 6e 43 65 6c 6c 3b 0a 20 20 69 43 65 6c 6c  2*nCell;.  iCell
1bf5d 4c 61 73 74 20 3d 20 75 73 61 62 6c 65 53 69 7a  Last = usableSiz
1bf5e 65 20 2d 20 34 3b 0a 20 20 66 6f 72 28 69 3d 30  e - 4;.  for(i=0
1bf5f 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b  ; i<nCell; i++){
1bf60 0a 20 20 20 20 75 38 20 2a 70 41 64 64 72 3b 20  .    u8 *pAddr; 
1bf61 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 20      /* The i-th 
1bf62 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a  cell pointer */.
1bf63 20 20 20 20 70 41 64 64 72 20 3d 20 26 64 61 74      pAddr = &dat
1bf64 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 69  a[cellOffset + i
1bf65 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67 65  *2];.    pc = ge
1bf66 74 32 62 79 74 65 28 70 41 64 64 72 29 3b 0a 20  t2byte(pAddr);. 
1bf67 20 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d     testcase( pc=
1bf68 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20  =iCellFirst );. 
1bf69 20 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d     testcase( pc=
1bf6a 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 23 69  =iCellLast );.#i
1bf6b 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
1bf6c 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a  E_ENABLE_OVERSIZ
1bf6d 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 20  E_CELL_CHECK).  
1bf6e 20 20 2f 2a 20 54 68 65 73 65 20 63 6f 6e 64 69    /* These condi
1bf6f 74 69 6f 6e 73 20 68 61 76 65 20 61 6c 72 65 61  tions have alrea
1bf70 64 79 20 62 65 65 6e 20 76 65 72 69 66 69 65 64  dy been verified
1bf71 20 69 6e 20 62 74 72 65 65 49 6e 69 74 50 61 67   in btreeInitPag
1bf72 65 28 29 0a 20 20 20 20 2a 2a 20 69 66 20 53 51  e().    ** if SQ
1bf73 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52  LITE_ENABLE_OVER
1bf74 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 20  SIZE_CELL_CHECK 
1bf75 69 73 20 64 65 66 69 6e 65 64 20 0a 20 20 20 20  is defined .    
1bf76 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 3c 69 43  */.    if( pc<iC
1bf77 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69  ellFirst || pc>i
1bf78 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20  CellLast ){.    
1bf79 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1bf7a 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1bf7b 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 61    }.#endif.    a
1bf7c 73 73 65 72 74 28 20 70 63 3e 3d 69 43 65 6c 6c  ssert( pc>=iCell
1bf7d 46 69 72 73 74 20 26 26 20 70 63 3c 3d 69 43 65  First && pc<=iCe
1bf7e 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 20 73 69  llLast );.    si
1bf7f 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72  ze = cellSizePtr
1bf80 28 70 50 61 67 65 2c 20 26 74 65 6d 70 5b 70 63  (pPage, &temp[pc
1bf81 5d 29 3b 0a 20 20 20 20 63 62 72 6b 20 2d 3d 20  ]);.    cbrk -= 
1bf82 73 69 7a 65 3b 0a 23 69 66 20 64 65 66 69 6e 65  size;.#if define
1bf83 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
1bf84 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48  OVERSIZE_CELL_CH
1bf85 45 43 4b 29 0a 20 20 20 20 69 66 28 20 63 62 72  ECK).    if( cbr
1bf86 6b 3c 69 43 65 6c 6c 46 69 72 73 74 20 29 7b 0a  k<iCellFirst ){.
1bf87 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1bf88 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1bf89 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20  ;.    }.#else.  
1bf8a 20 20 69 66 28 20 63 62 72 6b 3c 69 43 65 6c 6c    if( cbrk<iCell
1bf8b 46 69 72 73 74 20 7c 7c 20 70 63 2b 73 69 7a 65  First || pc+size
1bf8c 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20  >usableSize ){. 
1bf8d 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1bf8e 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1bf8f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
1bf90 20 20 61 73 73 65 72 74 28 20 63 62 72 6b 2b 73    assert( cbrk+s
1bf91 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20  ize<=usableSize 
1bf92 26 26 20 63 62 72 6b 3e 3d 69 43 65 6c 6c 46 69  && cbrk>=iCellFi
1bf93 72 73 74 20 29 3b 0a 20 20 20 20 74 65 73 74 63  rst );.    testc
1bf94 61 73 65 28 20 63 62 72 6b 2b 73 69 7a 65 3d 3d  ase( cbrk+size==
1bf95 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20  usableSize );.  
1bf96 20 20 74 65 73 74 63 61 73 65 28 20 70 63 2b 73    testcase( pc+s
1bf97 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20  ize==usableSize 
1bf98 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64  );.    memcpy(&d
1bf99 61 74 61 5b 63 62 72 6b 5d 2c 20 26 74 65 6d 70  ata[cbrk], &temp
1bf9a 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 20  [pc], size);.   
1bf9b 20 70 75 74 32 62 79 74 65 28 70 41 64 64 72 2c   put2byte(pAddr,
1bf9c 20 63 62 72 6b 29 3b 0a 20 20 7d 0a 20 20 61 73   cbrk);.  }.  as
1bf9d 73 65 72 74 28 20 63 62 72 6b 3e 3d 69 43 65 6c  sert( cbrk>=iCel
1bf9e 6c 46 69 72 73 74 20 29 3b 0a 20 20 70 75 74 32  lFirst );.  put2
1bf9f 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
1bfa0 5d 2c 20 63 62 72 6b 29 3b 0a 20 20 64 61 74 61  ], cbrk);.  data
1bfa1 5b 68 64 72 2b 31 5d 20 3d 20 30 3b 0a 20 20 64  [hdr+1] = 0;.  d
1bfa2 61 74 61 5b 68 64 72 2b 32 5d 20 3d 20 30 3b 0a  ata[hdr+2] = 0;.
1bfa3 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20    data[hdr+7] = 
1bfa4 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74  0;.  memset(&dat
1bfa5 61 5b 69 43 65 6c 6c 46 69 72 73 74 5d 2c 20 30  a[iCellFirst], 0
1bfa6 2c 20 63 62 72 6b 2d 69 43 65 6c 6c 46 69 72 73  , cbrk-iCellFirs
1bfa7 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  t);.  assert( sq
1bfa8 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
1bfa9 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
1bfaa 50 61 67 65 29 20 29 3b 0a 20 20 69 66 28 20 63  Page) );.  if( c
1bfab 62 72 6b 2d 69 43 65 6c 6c 46 69 72 73 74 21 3d  brk-iCellFirst!=
1bfac 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a  pPage->nFree ){.
1bfad 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1bfae 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
1bfaf 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
1bfb0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
1bfb1 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20   Allocate nByte 
1bfb2 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 66  bytes of space f
1bfb3 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 42  rom within the B
1bfb4 2d 54 72 65 65 20 70 61 67 65 20 70 61 73 73 65  -Tree page passe
1bfb5 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73  d.** as the firs
1bfb6 74 20 61 72 67 75 6d 65 6e 74 2e 20 57 72 69 74  t argument. Writ
1bfb7 65 20 69 6e 74 6f 20 2a 70 49 64 78 20 74 68 65  e into *pIdx the
1bfb8 20 69 6e 64 65 78 20 69 6e 74 6f 20 70 50 61 67   index into pPag
1bfb9 65 2d 3e 61 44 61 74 61 5b 5d 0a 2a 2a 20 6f 66  e->aData[].** of
1bfba 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20   the first byte 
1bfbb 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61  of allocated spa
1bfbc 63 65 2e 20 52 65 74 75 72 6e 20 65 69 74 68 65  ce. Return eithe
1bfbd 72 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 0a 2a  r SQLITE_OK or.*
1bfbe 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  * an error code 
1bfbf 28 75 73 75 61 6c 6c 79 20 53 51 4c 49 54 45 5f  (usually SQLITE_
1bfc0 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20  CORRUPT)..**.** 
1bfc1 54 68 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61  The caller guara
1bfc2 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 72 65  ntees that there
1bfc3 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 73   is sufficient s
1bfc4 70 61 63 65 20 74 6f 20 6d 61 6b 65 20 74 68 65  pace to make the
1bfc5 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  .** allocation. 
1bfc6 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69   This routine mi
1bfc7 67 68 74 20 6e 65 65 64 20 74 6f 20 64 65 66 72  ght need to defr
1bfc8 61 67 6d 65 6e 74 20 69 6e 20 6f 72 64 65 72 20  agment in order 
1bfc9 74 6f 20 62 72 69 6e 67 0a 2a 2a 20 61 6c 6c 20  to bring.** all 
1bfca 74 68 65 20 73 70 61 63 65 20 74 6f 67 65 74 68  the space togeth
1bfcb 65 72 2c 20 68 6f 77 65 76 65 72 2e 20 20 54 68  er, however.  Th
1bfcc 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
1bfcd 61 76 6f 69 64 20 75 73 69 6e 67 0a 2a 2a 20 74  avoid using.** t
1bfce 68 65 20 66 69 72 73 74 20 74 77 6f 20 62 79 74  he first two byt
1bfcf 65 73 20 70 61 73 74 20 74 68 65 20 63 65 6c 6c  es past the cell
1bfd0 20 70 6f 69 6e 74 65 72 20 61 72 65 61 20 73 69   pointer area si
1bfd1 6e 63 65 20 70 72 65 73 75 6d 61 62 6c 79 20 74  nce presumably t
1bfd2 68 69 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  his.** allocatio
1bfd3 6e 20 69 73 20 62 65 69 6e 67 20 6d 61 64 65 20  n is being made 
1bfd4 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 73 65  in order to inse
1bfd5 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 2c 20 73  rt a new cell, s
1bfd6 6f 20 77 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73  o we will.** als
1bfd7 6f 20 65 6e 64 20 75 70 20 6e 65 65 64 69 6e 67  o end up needing
1bfd8 20 61 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e   a new cell poin
1bfd9 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ter..*/.static i
1bfda 6e 74 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65  nt allocateSpace
1bfdb 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
1bfdc 20 69 6e 74 20 6e 42 79 74 65 2c 20 69 6e 74 20   int nByte, int 
1bfdd 2a 70 49 64 78 29 7b 0a 20 20 63 6f 6e 73 74 20  *pIdx){.  const 
1bfde 69 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65 2d  int hdr = pPage-
1bfdf 3e 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 2f  >hdrOffset;    /
1bfe0 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66  * Local cache of
1bfe1 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
1bfe2 74 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73  t */.  u8 * cons
1bfe3 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e  t data = pPage->
1bfe4 61 44 61 74 61 3b 20 20 20 20 20 20 2f 2a 20 4c  aData;      /* L
1bfe5 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70 50  ocal cache of pP
1bfe6 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20  age->aData */.  
1bfe7 69 6e 74 20 6e 46 72 61 67 3b 20 20 20 20 20 20  int nFrag;      
1bfe8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bfe9 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1bfea 66 20 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74  f fragmented byt
1bfeb 65 73 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20  es on pPage */. 
1bfec 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 20   int top;       
1bfed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bfee 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62        /* First b
1bfef 79 74 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74  yte of cell cont
1bff0 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e  ent area */.  in
1bff1 74 20 67 61 70 3b 20 20 20 20 20 20 20 20 2f 2a  t gap;        /*
1bff2 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 67   First byte of g
1bff3 61 70 20 62 65 74 77 65 65 6e 20 63 65 6c 6c 20  ap between cell 
1bff4 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 63 65 6c  pointers and cel
1bff5 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69  l content */.  i
1bff6 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 2f  nt rc;         /
1bff7 2a 20 49 6e 74 65 67 65 72 20 72 65 74 75 72 6e  * Integer return
1bff8 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 75   code */.  int u
1bff9 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a 20 55 73  sableSize; /* Us
1bffa 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 74 68 65  able size of the
1bffb 20 70 61 67 65 20 2a 2f 0a 20 20 0a 20 20 61 73   page */.  .  as
1bffc 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
1bffd 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
1bffe 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
1bfff 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
1c000 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72  ->pBt );.  asser
1c001 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1c002 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
1c003 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
1c004 73 65 72 74 28 20 6e 42 79 74 65 3e 3d 30 20 29  sert( nByte>=0 )
1c005 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65  ;  /* Minimum ce
1c006 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a  ll size is 4 */.
1c007 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
1c008 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65 20 29 3b  >nFree>=nByte );
1c009 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
1c00a 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29  ->nOverflow==0 )
1c00b 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d  ;.  usableSize =
1c00c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61   pPage->pBt->usa
1c00d 62 6c 65 53 69 7a 65 3b 0a 20 20 61 73 73 65 72  bleSize;.  asser
1c00e 74 28 20 6e 42 79 74 65 20 3c 20 75 73 61 62 6c  t( nByte < usabl
1c00f 65 53 69 7a 65 2d 38 20 29 3b 0a 0a 20 20 6e 46  eSize-8 );..  nF
1c010 72 61 67 20 3d 20 64 61 74 61 5b 68 64 72 2b 37  rag = data[hdr+7
1c011 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ];.  assert( pPa
1c012 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d  ge->cellOffset =
1c013 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70  = hdr + 12 - 4*p
1c014 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20  Page->leaf );.  
1c015 67 61 70 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c  gap = pPage->cel
1c016 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67  lOffset + 2*pPag
1c017 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 74 6f 70 20  e->nCell;.  top 
1c018 3d 20 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 72  = get2byteNotZer
1c019 6f 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b  o(&data[hdr+5]);
1c01a 0a 20 20 69 66 28 20 67 61 70 3e 74 6f 70 20 29  .  if( gap>top )
1c01b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1c01c 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 74  ORRUPT_BKPT;.  t
1c01d 65 73 74 63 61 73 65 28 20 67 61 70 2b 32 3d 3d  estcase( gap+2==
1c01e 74 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 61 73  top );.  testcas
1c01f 65 28 20 67 61 70 2b 31 3d 3d 74 6f 70 20 29 3b  e( gap+1==top );
1c020 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70  .  testcase( gap
1c021 3d 3d 74 6f 70 20 29 3b 0a 0a 20 20 69 66 28 20  ==top );..  if( 
1c022 6e 46 72 61 67 3e 3d 36 30 20 29 7b 0a 20 20 20  nFrag>=60 ){.   
1c023 20 2f 2a 20 41 6c 77 61 79 73 20 64 65 66 72 61   /* Always defra
1c024 67 6d 65 6e 74 20 68 69 67 68 6c 79 20 66 72 61  gment highly fra
1c025 67 6d 65 6e 74 65 64 20 70 61 67 65 73 20 2a 2f  gmented pages */
1c026 0a 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 67  .    rc = defrag
1c027 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b  mentPage(pPage);
1c028 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
1c029 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70  turn rc;.    top
1c02a 20 3d 20 67 65 74 32 62 79 74 65 4e 6f 74 5a 65   = get2byteNotZe
1c02b 72 6f 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29  ro(&data[hdr+5])
1c02c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67 61  ;.  }else if( ga
1c02d 70 2b 32 3c 3d 74 6f 70 20 29 7b 0a 20 20 20 20  p+2<=top ){.    
1c02e 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 66 72  /* Search the fr
1c02f 65 65 6c 69 73 74 20 6c 6f 6f 6b 69 6e 67 20 66  eelist looking f
1c030 6f 72 20 61 20 66 72 65 65 20 73 6c 6f 74 20 62  or a free slot b
1c031 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 61 74  ig enough to sat
1c032 69 73 66 79 20 0a 20 20 20 20 2a 2a 20 74 68 65  isfy .    ** the
1c033 20 72 65 71 75 65 73 74 2e 20 54 68 65 20 61 6c   request. The al
1c034 6c 6f 63 61 74 69 6f 6e 20 69 73 20 6d 61 64 65  location is made
1c035 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20   from the first 
1c036 66 72 65 65 20 73 6c 6f 74 20 69 6e 20 0a 20 20  free slot in .  
1c037 20 20 2a 2a 20 74 68 65 20 6c 69 73 74 20 74 68    ** the list th
1c038 61 74 20 69 73 20 6c 61 72 67 65 20 65 6e 6f 75  at is large enou
1c039 67 68 20 74 6f 20 61 63 63 6f 6d 61 64 61 74 65  gh to accomadate
1c03a 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   it..    */.    
1c03b 69 6e 74 20 70 63 2c 20 61 64 64 72 3b 0a 20 20  int pc, addr;.  
1c03c 20 20 66 6f 72 28 61 64 64 72 3d 68 64 72 2b 31    for(addr=hdr+1
1c03d 3b 20 28 70 63 20 3d 20 67 65 74 32 62 79 74 65  ; (pc = get2byte
1c03e 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29 3e 30  (&data[addr]))>0
1c03f 3b 20 61 64 64 72 3d 70 63 29 7b 0a 20 20 20 20  ; addr=pc){.    
1c040 20 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20    int size;     
1c041 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
1c042 66 20 74 68 65 20 66 72 65 65 20 73 6c 6f 74 20  f the free slot 
1c043 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 63 3e  */.      if( pc>
1c044 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 7c 7c 20  usableSize-4 || 
1c045 70 63 3c 61 64 64 72 2b 34 20 29 7b 0a 20 20 20  pc<addr+4 ){.   
1c046 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1c047 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1c048 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
1c049 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26  ize = get2byte(&
1c04a 64 61 74 61 5b 70 63 2b 32 5d 29 3b 0a 20 20 20  data[pc+2]);.   
1c04b 20 20 20 69 66 28 20 73 69 7a 65 3e 3d 6e 42 79     if( size>=nBy
1c04c 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  te ){.        in
1c04d 74 20 78 20 3d 20 73 69 7a 65 20 2d 20 6e 42 79  t x = size - nBy
1c04e 74 65 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74  te;.        test
1c04f 63 61 73 65 28 20 78 3d 3d 34 20 29 3b 0a 20 20  case( x==4 );.  
1c050 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1c051 78 3d 3d 33 20 29 3b 0a 20 20 20 20 20 20 20 20  x==3 );.        
1c052 69 66 28 20 78 3c 34 20 29 7b 0a 20 20 20 20 20  if( x<4 ){.     
1c053 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74       /* Remove t
1c054 68 65 20 73 6c 6f 74 20 66 72 6f 6d 20 74 68 65  he slot from the
1c055 20 66 72 65 65 2d 6c 69 73 74 2e 20 55 70 64 61   free-list. Upda
1c056 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  te the number of
1c057 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 72  .          ** fr
1c058 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73 20 77  agmented bytes w
1c059 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 2e 20  ithin the page. 
1c05a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d  */.          mem
1c05b 63 70 79 28 26 64 61 74 61 5b 61 64 64 72 5d 2c  cpy(&data[addr],
1c05c 20 26 64 61 74 61 5b 70 63 5d 2c 20 32 29 3b 0a   &data[pc], 2);.
1c05d 20 20 20 20 20 20 20 20 20 20 64 61 74 61 5b 68            data[h
1c05e 64 72 2b 37 5d 20 3d 20 28 75 38 29 28 6e 46 72  dr+7] = (u8)(nFr
1c05f 61 67 20 2b 20 78 29 3b 0a 20 20 20 20 20 20 20  ag + x);.       
1c060 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65 2b   }else if( size+
1c061 70 63 20 3e 20 75 73 61 62 6c 65 53 69 7a 65 20  pc > usableSize 
1c062 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
1c063 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
1c064 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20  PT_BKPT;.       
1c065 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1c066 20 20 2f 2a 20 54 68 65 20 73 6c 6f 74 20 72 65    /* The slot re
1c067 6d 61 69 6e 73 20 6f 6e 20 74 68 65 20 66 72 65  mains on the fre
1c068 65 2d 6c 69 73 74 2e 20 52 65 64 75 63 65 20 69  e-list. Reduce i
1c069 74 73 20 73 69 7a 65 20 74 6f 20 61 63 63 6f 75  ts size to accou
1c06a 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  nt.          ** 
1c06b 66 6f 72 20 74 68 65 20 70 6f 72 74 69 6f 6e 20  for the portion 
1c06c 75 73 65 64 20 62 79 20 74 68 65 20 6e 65 77 20  used by the new 
1c06d 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 2a 2f 0a 20  allocation. */. 
1c06e 20 20 20 20 20 20 20 20 20 70 75 74 32 62 79 74           put2byt
1c06f 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 2c 20 78  e(&data[pc+2], x
1c070 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1c071 20 20 20 20 20 2a 70 49 64 78 20 3d 20 70 63 20       *pIdx = pc 
1c072 2b 20 78 3b 0a 20 20 20 20 20 20 20 20 72 65 74  + x;.        ret
1c073 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
1c074 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1c075 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20  ..  /* Check to 
1c076 6d 61 6b 65 20 73 75 72 65 20 74 68 65 72 65 20  make sure there 
1c077 69 73 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20  is enough space 
1c078 69 6e 20 74 68 65 20 67 61 70 20 74 6f 20 73 61  in the gap to sa
1c079 74 69 73 66 79 0a 20 20 2a 2a 20 74 68 65 20 61  tisfy.  ** the a
1c07a 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 49 66 20 6e  llocation.  If n
1c07b 6f 74 2c 20 64 65 66 72 61 67 6d 65 6e 74 2e 0a  ot, defragment..
1c07c 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28    */.  testcase(
1c07d 20 67 61 70 2b 32 2b 6e 42 79 74 65 3d 3d 74 6f   gap+2+nByte==to
1c07e 70 20 29 3b 0a 20 20 69 66 28 20 67 61 70 2b 32  p );.  if( gap+2
1c07f 2b 6e 42 79 74 65 3e 74 6f 70 20 29 7b 0a 20 20  +nByte>top ){.  
1c080 20 20 72 63 20 3d 20 64 65 66 72 61 67 6d 65 6e    rc = defragmen
1c081 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  tPage(pPage);.  
1c082 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
1c083 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70 20 3d 20  n rc;.    top = 
1c084 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 6f 28  get2byteNotZero(
1c085 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20  &data[hdr+5]);. 
1c086 20 20 20 61 73 73 65 72 74 28 20 67 61 70 2b 6e     assert( gap+n
1c087 42 79 74 65 3c 3d 74 6f 70 20 29 3b 0a 20 20 7d  Byte<=top );.  }
1c088 0a 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ...  /* Allocate
1c089 20 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65   memory from the
1c08a 20 67 61 70 20 69 6e 20 62 65 74 77 65 65 6e 20   gap in between 
1c08b 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  the cell pointer
1c08c 20 61 72 72 61 79 0a 20 20 2a 2a 20 61 6e 64 20   array.  ** and 
1c08d 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
1c08e 20 61 72 65 61 2e 20 20 54 68 65 20 62 74 72 65   area.  The btre
1c08f 65 49 6e 69 74 50 61 67 65 28 29 20 63 61 6c 6c  eInitPage() call
1c090 20 68 61 73 20 61 6c 72 65 61 64 79 0a 20 20 2a   has already.  *
1c091 2a 20 76 61 6c 69 64 61 74 65 64 20 74 68 65 20  * validated the 
1c092 66 72 65 65 6c 69 73 74 2e 20 20 47 69 76 65 6e  freelist.  Given
1c093 20 74 68 61 74 20 74 68 65 20 66 72 65 65 6c 69   that the freeli
1c094 73 74 20 69 73 20 76 61 6c 69 64 2c 20 74 68 65  st is valid, the
1c095 72 65 0a 20 20 2a 2a 20 69 73 20 6e 6f 20 77 61  re.  ** is no wa
1c096 79 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f 63  y that the alloc
1c097 61 74 69 6f 6e 20 63 61 6e 20 65 78 74 65 6e 64  ation can extend
1c098 20 6f 66 66 20 74 68 65 20 65 6e 64 20 6f 66 20   off the end of 
1c099 74 68 65 20 70 61 67 65 2e 0a 20 20 2a 2a 20 54  the page..  ** T
1c09a 68 65 20 61 73 73 65 72 74 28 29 20 62 65 6c 6f  he assert() belo
1c09b 77 20 76 65 72 69 66 69 65 73 20 74 68 65 20 70  w verifies the p
1c09c 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65  revious sentence
1c09d 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 20 2d 3d 20  ..  */.  top -= 
1c09e 6e 42 79 74 65 3b 0a 20 20 70 75 74 32 62 79 74  nByte;.  put2byt
1c09f 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20  e(&data[hdr+5], 
1c0a0 74 6f 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20  top);.  assert( 
1c0a1 74 6f 70 2b 6e 42 79 74 65 20 3c 3d 20 28 69 6e  top+nByte <= (in
1c0a2 74 29 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  t)pPage->pBt->us
1c0a3 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 2a 70  ableSize );.  *p
1c0a4 49 64 78 20 3d 20 74 6f 70 3b 0a 20 20 72 65 74  Idx = top;.  ret
1c0a5 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1c0a6 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
1c0a7 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20   section of the 
1c0a8 70 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f 20  pPage->aData to 
1c0a9 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a  the freelist..**
1c0aa 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20   The first byte 
1c0ab 6f 66 20 74 68 65 20 6e 65 77 20 66 72 65 65 20  of the new free 
1c0ac 62 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d 3e  block is pPage->
1c0ad 61 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a 20  aDisk[start].** 
1c0ae 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20  and the size of 
1c0af 74 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73 69  the block is "si
1c0b0 7a 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a  ze" bytes..**.**
1c0b1 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66 66   Most of the eff
1c0b2 6f 72 74 20 68 65 72 65 20 69 73 20 69 6e 76 6f  ort here is invo
1c0b3 6c 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69 6e  lved in coalesin
1c0b4 67 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66 72  g adjacent.** fr
1c0b5 65 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 61  ee blocks into a
1c0b6 20 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65 65   single big free
1c0b7 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69   block..*/.stati
1c0b8 63 20 69 6e 74 20 66 72 65 65 53 70 61 63 65 28  c int freeSpace(
1c0b9 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
1c0ba 69 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20 73  int start, int s
1c0bb 69 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 72  ize){.  int addr
1c0bc 2c 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a 20  , pbegin, hdr;. 
1c0bd 20 69 6e 74 20 69 4c 61 73 74 3b 20 20 20 20 20   int iLast;     
1c0be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c0bf 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 6f     /* Largest po
1c0c0 73 73 69 62 6c 65 20 66 72 65 65 62 6c 6f 63 6b  ssible freeblock
1c0c1 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 75 6e 73   offset */.  uns
1c0c2 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61  igned char *data
1c0c3 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b   = pPage->aData;
1c0c4 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
1c0c5 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61  e->pBt!=0 );.  a
1c0c6 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
1c0c7 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
1c0c8 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
1c0c9 3b 0a 20 20 61 73 73 65 72 74 28 20 73 74 61 72  ;.  assert( star
1c0ca 74 3e 3d 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  t>=pPage->hdrOff
1c0cb 73 65 74 2b 36 2b 70 50 61 67 65 2d 3e 63 68 69  set+6+pPage->chi
1c0cc 6c 64 50 74 72 53 69 7a 65 20 29 3b 0a 20 20 61  ldPtrSize );.  a
1c0cd 73 73 65 72 74 28 20 28 73 74 61 72 74 20 2b 20  ssert( (start + 
1c0ce 73 69 7a 65 29 20 3c 3d 20 28 69 6e 74 29 70 50  size) <= (int)pP
1c0cf 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
1c0d0 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74  Size );.  assert
1c0d1 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1c0d2 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
1c0d3 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
1c0d4 65 72 74 28 20 73 69 7a 65 3e 3d 30 20 29 3b 20  ert( size>=0 ); 
1c0d5 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c    /* Minimum cel
1c0d6 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 0a  l size is 4 */..
1c0d7 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42 74    if( pPage->pBt
1c0d8 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53  ->btsFlags & BTS
1c0d9 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 29  _SECURE_DELETE )
1c0da 7b 0a 20 20 20 20 2f 2a 20 4f 76 65 72 77 72 69  {.    /* Overwri
1c0db 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f 72  te deleted infor
1c0dc 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f  mation with zero
1c0dd 73 20 77 68 65 6e 20 74 68 65 20 73 65 63 75 72  s when the secur
1c0de 65 5f 64 65 6c 65 74 65 0a 20 20 20 20 2a 2a 20  e_delete.    ** 
1c0df 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65  option is enable
1c0e0 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28  d */.    memset(
1c0e1 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30 2c  &data[start], 0,
1c0e2 20 73 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f   size);.  }..  /
1c0e3 2a 20 41 64 64 20 74 68 65 20 73 70 61 63 65 20  * Add the space 
1c0e4 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 6c 69  back into the li
1c0e5 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 66 72 65  nked list of fre
1c0e6 65 62 6c 6f 63 6b 73 2e 20 20 4e 6f 74 65 20 74  eblocks.  Note t
1c0e7 68 61 74 0a 20 20 2a 2a 20 65 76 65 6e 20 74 68  hat.  ** even th
1c0e8 6f 75 67 68 20 74 68 65 20 66 72 65 65 62 6c 6f  ough the freeblo
1c0e9 63 6b 20 6c 69 73 74 20 77 61 73 20 63 68 65 63  ck list was chec
1c0ea 6b 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74  ked by btreeInit
1c0eb 50 61 67 65 28 29 2c 0a 20 20 2a 2a 20 62 74 72  Page(),.  ** btr
1c0ec 65 65 49 6e 69 74 50 61 67 65 28 29 20 64 69 64  eeInitPage() did
1c0ed 20 6e 6f 74 20 64 65 74 65 63 74 20 6f 76 65 72   not detect over
1c0ee 6c 61 70 70 69 6e 67 20 63 65 6c 6c 73 20 6f 72  lapping cells or
1c0ef 0a 20 20 2a 2a 20 66 72 65 65 62 6c 6f 63 6b 73  .  ** freeblocks
1c0f0 20 74 68 61 74 20 6f 76 65 72 6c 61 70 70 65 64   that overlapped
1c0f1 20 63 65 6c 6c 73 2e 20 20 20 4e 6f 72 20 64 6f   cells.   Nor do
1c0f2 65 73 20 69 74 20 64 65 74 65 63 74 20 77 68 65  es it detect whe
1c0f3 6e 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 20  n the.  ** cell 
1c0f4 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 65 78 63  content area exc
1c0f5 65 65 64 73 20 74 68 65 20 76 61 6c 75 65 20 69  eeds the value i
1c0f6 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65  n the page heade
1c0f7 72 2e 20 20 49 66 20 74 68 65 73 65 0a 20 20 2a  r.  If these.  *
1c0f8 2a 20 73 69 74 75 61 74 69 6f 6e 73 20 61 72 69  * situations ari
1c0f9 73 65 2c 20 74 68 65 6e 20 73 75 62 73 65 71 75  se, then subsequ
1c0fa 65 6e 74 20 69 6e 73 65 72 74 20 6f 70 65 72 61  ent insert opera
1c0fb 74 69 6f 6e 73 20 6d 69 67 68 74 20 63 6f 72 72  tions might corr
1c0fc 75 70 74 0a 20 20 2a 2a 20 74 68 65 20 66 72 65  upt.  ** the fre
1c0fd 65 6c 69 73 74 2e 20 20 53 6f 20 77 65 20 64 6f  elist.  So we do
1c0fe 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 66   need to check f
1c0ff 6f 72 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 68  or corruption wh
1c100 69 6c 65 20 73 63 61 6e 6e 69 6e 67 0a 20 20 2a  ile scanning.  *
1c101 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a  * the freelist..
1c102 20 20 2a 2f 0a 20 20 68 64 72 20 3d 20 70 50 61    */.  hdr = pPa
1c103 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20  ge->hdrOffset;. 
1c104 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31 3b   addr = hdr + 1;
1c105 0a 20 20 69 4c 61 73 74 20 3d 20 70 50 61 67 65  .  iLast = pPage
1c106 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ->pBt->usableSiz
1c107 65 20 2d 20 34 3b 0a 20 20 61 73 73 65 72 74 28  e - 4;.  assert(
1c108 20 73 74 61 72 74 3c 3d 69 4c 61 73 74 20 29 3b   start<=iLast );
1c109 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69  .  while( (pbegi
1c10a 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  n = get2byte(&da
1c10b 74 61 5b 61 64 64 72 5d 29 29 3c 73 74 61 72 74  ta[addr]))<start
1c10c 20 26 26 20 70 62 65 67 69 6e 3e 30 20 29 7b 0a   && pbegin>0 ){.
1c10d 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 3c 61      if( pbegin<a
1c10e 64 64 72 2b 34 20 29 7b 0a 20 20 20 20 20 20 72  ddr+4 ){.      r
1c10f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
1c110 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
1c111 0a 20 20 20 20 61 64 64 72 20 3d 20 70 62 65 67  .    addr = pbeg
1c112 69 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 62  in;.  }.  if( pb
1c113 65 67 69 6e 3e 69 4c 61 73 74 20 29 7b 0a 20 20  egin>iLast ){.  
1c114 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1c115 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1c116 7d 0a 20 20 61 73 73 65 72 74 28 20 70 62 65 67  }.  assert( pbeg
1c117 69 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65 67 69  in>addr || pbegi
1c118 6e 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32 62 79  n==0 );.  put2by
1c119 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20  te(&data[addr], 
1c11a 73 74 61 72 74 29 3b 0a 20 20 70 75 74 32 62 79  start);.  put2by
1c11b 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c  te(&data[start],
1c11c 20 70 62 65 67 69 6e 29 3b 0a 20 20 70 75 74 32   pbegin);.  put2
1c11d 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74  byte(&data[start
1c11e 2b 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 70 50  +2], size);.  pP
1c11f 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 70 50 61  age->nFree = pPa
1c120 67 65 2d 3e 6e 46 72 65 65 20 2b 20 28 75 31 36  ge->nFree + (u16
1c121 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 6f 61  )size;..  /* Coa
1c122 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66  lesce adjacent f
1c123 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20  ree blocks */.  
1c124 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31 3b 0a  addr = hdr + 1;.
1c125 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e    while( (pbegin
1c126 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
1c127 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a 20  a[addr]))>0 ){. 
1c128 20 20 20 69 6e 74 20 70 6e 65 78 74 2c 20 70 73     int pnext, ps
1c129 69 7a 65 2c 20 78 3b 0a 20 20 20 20 61 73 73 65  ize, x;.    asse
1c12a 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20  rt( pbegin>addr 
1c12b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
1c12c 62 65 67 69 6e 20 3c 3d 20 28 69 6e 74 29 70 50  begin <= (int)pP
1c12d 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
1c12e 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 70 6e  Size-4 );.    pn
1c12f 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 28 26  ext = get2byte(&
1c130 64 61 74 61 5b 70 62 65 67 69 6e 5d 29 3b 0a 20  data[pbegin]);. 
1c131 20 20 20 70 73 69 7a 65 20 3d 20 67 65 74 32 62     psize = get2b
1c132 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e  yte(&data[pbegin
1c133 2b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 62  +2]);.    if( pb
1c134 65 67 69 6e 20 2b 20 70 73 69 7a 65 20 2b 20 33  egin + psize + 3
1c135 20 3e 3d 20 70 6e 65 78 74 20 26 26 20 70 6e 65   >= pnext && pne
1c136 78 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e  xt>0 ){.      in
1c137 74 20 66 72 61 67 20 3d 20 70 6e 65 78 74 20 2d  t frag = pnext -
1c138 20 28 70 62 65 67 69 6e 2b 70 73 69 7a 65 29 3b   (pbegin+psize);
1c139 0a 20 20 20 20 20 20 69 66 28 20 28 66 72 61 67  .      if( (frag
1c13a 3c 30 29 20 7c 7c 20 28 66 72 61 67 3e 28 69 6e  <0) || (frag>(in
1c13b 74 29 64 61 74 61 5b 68 64 72 2b 37 5d 29 20 29  t)data[hdr+7]) )
1c13c 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
1c13d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1c13e 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20  BKPT;.      }.  
1c13f 20 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20      data[hdr+7] 
1c140 2d 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20 20  -= (u8)frag;.   
1c141 20 20 20 78 20 3d 20 67 65 74 32 62 79 74 65 28     x = get2byte(
1c142 26 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a 20  &data[pnext]);. 
1c143 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64       put2byte(&d
1c144 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29 3b  ata[pbegin], x);
1c145 0a 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78 74  .      x = pnext
1c146 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74   + get2byte(&dat
1c147 61 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70 62  a[pnext+2]) - pb
1c148 65 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74 32  egin;.      put2
1c149 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69  byte(&data[pbegi
1c14a 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d 65  n+2], x);.    }e
1c14b 6c 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72 20  lse{.      addr 
1c14c 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d 0a  = pbegin;.    }.
1c14d 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
1c14e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72   cell content ar
1c14f 65 61 20 62 65 67 69 6e 73 20 77 69 74 68 20 61  ea begins with a
1c150 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d 6f   freeblock, remo
1c151 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20  ve it. */.  if( 
1c152 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61 74  data[hdr+1]==dat
1c153 61 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74 61  a[hdr+5] && data
1c154 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68 64  [hdr+2]==data[hd
1c155 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20  r+6] ){.    int 
1c156 74 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e 20  top;.    pbegin 
1c157 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
1c158 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d 65  [hdr+1]);.    me
1c159 6d 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b 31  mcpy(&data[hdr+1
1c15a 5d 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e 5d  ], &data[pbegin]
1c15b 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d 20  , 2);.    top = 
1c15c 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
1c15d 64 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79 74  dr+5]) + get2byt
1c15e 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32  e(&data[pbegin+2
1c15f 5d 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65  ]);.    put2byte
1c160 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74  (&data[hdr+5], t
1c161 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  op);.  }.  asser
1c162 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
1c163 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
1c164 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
1c165 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1c166 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64  ;.}../*.** Decod
1c167 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65  e the flags byte
1c168 20 28 74 68 65 20 66 69 72 73 74 20 62 79 74 65   (the first byte
1c169 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 20   of the header) 
1c16a 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 6e  for a page.** an
1c16b 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 65  d initialize fie
1c16c 6c 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61  lds of the MemPa
1c16d 67 65 20 73 74 72 75 63 74 75 72 65 20 61 63 63  ge structure acc
1c16e 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20  ordingly..**.** 
1c16f 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69  Only the followi
1c170 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20  ng combinations 
1c171 61 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20  are supported.  
1c172 41 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72 65  Anything differe
1c173 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20  nt.** indicates 
1c174 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61  a corrupt databa
1c175 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20  se files:.**.** 
1c176 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 4f          PTF_ZERO
1c177 44 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20 20  DATA.**         
1c178 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 50  PTF_ZERODATA | P
1c179 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20 20  TF_LEAF.**      
1c17a 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20     PTF_LEAFDATA 
1c17b 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a 20  | PTF_INTKEY.** 
1c17c 20 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46          PTF_LEAF
1c17d 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45  DATA | PTF_INTKE
1c17e 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f 0a  Y | PTF_LEAF.*/.
1c17f 73 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f 64  static int decod
1c180 65 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 2a  eFlags(MemPage *
1c181 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 42  pPage, int flagB
1c182 79 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64  yte){.  BtShared
1c183 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 20   *pBt;     /* A 
1c184 63 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e 70  copy of pPage->p
1c185 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  Bt */..  assert(
1c186 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
1c187 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f 3d  t==(pPage->pgno=
1c188 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29 3b  =1 ? 100 : 0) );
1c189 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1c18a 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
1c18b 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
1c18c 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65 61   );.  pPage->lea
1c18d 66 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79 74  f = (u8)(flagByt
1c18e 65 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28 20  e>>3);  assert( 
1c18f 50 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c 33  PTF_LEAF == 1<<3
1c190 20 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20 26   );.  flagByte &
1c191 3d 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20 70  = ~PTF_LEAF;.  p
1c192 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69  Page->childPtrSi
1c193 7a 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d 3e  ze = 4-4*pPage->
1c194 6c 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70 50  leaf;.  pBt = pP
1c195 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20  age->pBt;.  if( 
1c196 66 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f 4c  flagByte==(PTF_L
1c197 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e  EAFDATA | PTF_IN
1c198 54 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50 61  TKEY) ){.    pPa
1c199 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a  ge->intKey = 1;.
1c19a 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 61      pPage->hasDa
1c19b 74 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66  ta = pPage->leaf
1c19c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78  ;.    pPage->max
1c19d 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78  Local = pBt->max
1c19e 4c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d  Leaf;.    pPage-
1c19f 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d  >minLocal = pBt-
1c1a0 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73  >minLeaf;.  }els
1c1a1 65 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d  e if( flagByte==
1c1a2 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b 0a  PTF_ZERODATA ){.
1c1a3 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65      pPage->intKe
1c1a4 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65  y = 0;.    pPage
1c1a5 2d 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a 20  ->hasData = 0;. 
1c1a6 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63     pPage->maxLoc
1c1a7 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63  al = pBt->maxLoc
1c1a8 61 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d  al;.    pPage->m
1c1a9 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d  inLocal = pBt->m
1c1aa 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 65  inLocal;.  }else
1c1ab 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1c1ac 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1c1ad 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6d  ;.  }.  pPage->m
1c1ae 61 78 31 62 79 74 65 50 61 79 6c 6f 61 64 20 3d  ax1bytePayload =
1c1af 20 70 42 74 2d 3e 6d 61 78 31 62 79 74 65 50 61   pBt->max1bytePa
1c1b0 79 6c 6f 61 64 3b 0a 20 20 72 65 74 75 72 6e 20  yload;.  return 
1c1b1 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
1c1b2 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74  .** Initialize t
1c1b3 68 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66  he auxiliary inf
1c1b4 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64  ormation for a d
1c1b5 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a  isk block..**.**
1c1b6 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
1c1b7 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49  K on success.  I
1c1b8 66 20 77 65 20 73 65 65 20 74 68 61 74 20 74 68  f we see that th
1c1b9 65 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e  e page does.** n
1c1ba 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c  ot contain a wel
1c1bb 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73  l-formed databas
1c1bc 65 20 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74  e page, then ret
1c1bd 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43  urn .** SQLITE_C
1c1be 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68  ORRUPT.  Note th
1c1bf 61 74 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53  at a return of S
1c1c0 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f  QLITE_OK does no
1c1c1 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74  t.** guarantee t
1c1c2 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20  hat the page is 
1c1c3 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74  well-formed.  It
1c1c4 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74   only shows that
1c1c5 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f  .** we failed to
1c1c6 20 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72   detect any corr
1c1c7 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  uption..*/.stati
1c1c8 63 20 69 6e 74 20 62 74 72 65 65 49 6e 69 74 50  c int btreeInitP
1c1c9 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  age(MemPage *pPa
1c1ca 67 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20  ge){..  assert( 
1c1cb 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b  pPage->pBt!=0 );
1c1cc 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1c1cd 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
1c1ce 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
1c1cf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
1c1d0 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74  age->pgno==sqlit
1c1d1 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65  e3PagerPagenumbe
1c1d2 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  r(pPage->pDbPage
1c1d3 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
1c1d4 50 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50  Page == sqlite3P
1c1d5 61 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61  agerGetExtra(pPa
1c1d6 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
1c1d7 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
1c1d8 3e 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65  >aData == sqlite
1c1d9 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50  3PagerGetData(pP
1c1da 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
1c1db 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e  ..  if( !pPage->
1c1dc 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 31  isInit ){.    u1
1c1dd 36 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20  6 pc;           
1c1de 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61   /* Address of a
1c1df 20 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69   freeblock withi
1c1e0 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d  n pPage->aData[]
1c1e1 20 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 3b 20   */.    u8 hdr; 
1c1e2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66             /* Of
1c1e3 66 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e  fset to beginnin
1c1e4 67 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 72  g of page header
1c1e5 20 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 61   */.    u8 *data
1c1e6 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71  ;          /* Eq
1c1e7 75 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44  ual to pPage->aD
1c1e8 61 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 61  ata */.    BtSha
1c1e9 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20  red *pBt;       
1c1ea 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72   /* The main btr
1c1eb 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  ee structure */.
1c1ec 20 20 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69      int usableSi
1c1ed 7a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74  ze;    /* Amount
1c1ee 20 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 65   of usable space
1c1ef 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f   on each page */
1c1f0 0a 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66  .    u16 cellOff
1c1f1 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65  set;    /* Offse
1c1f2 74 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20  t from start of 
1c1f3 70 61 67 65 20 74 6f 20 66 69 72 73 74 20 63 65  page to first ce
1c1f4 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20  ll pointer */.  
1c1f5 20 20 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20    int nFree;    
1c1f6 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1c1f7 66 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f  f unused bytes o
1c1f8 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20  n the page */.  
1c1f9 20 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20    int top;      
1c1fa 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79       /* First by
1c1fb 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63  te of the cell c
1c1fc 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20  ontent area */. 
1c1fd 20 20 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73     int iCellFirs
1c1fe 74 3b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61  t;    /* First a
1c1ff 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 6f 72  llowable cell or
1c200 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65   freeblock offse
1c201 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 65  t */.    int iCe
1c202 6c 6c 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c  llLast;     /* L
1c203 61 73 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c  ast possible cel
1c204 6c 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f  l or freeblock o
1c205 66 66 73 65 74 20 2a 2f 0a 0a 20 20 20 20 70 42  ffset */..    pB
1c206 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a  t = pPage->pBt;.
1c207 0a 20 20 20 20 68 64 72 20 3d 20 70 50 61 67 65  .    hdr = pPage
1c208 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 20  ->hdrOffset;.   
1c209 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
1c20a 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 65  Data;.    if( de
1c20b 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c  codeFlags(pPage,
1c20c 20 64 61 74 61 5b 68 64 72 5d 29 20 29 20 72 65   data[hdr]) ) re
1c20d 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
1c20e 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 61 73  UPT_BKPT;.    as
1c20f 73 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53  sert( pBt->pageS
1c210 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d  ize>=512 && pBt-
1c211 3e 70 61 67 65 53 69 7a 65 3c 3d 36 35 35 33 36  >pageSize<=65536
1c212 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d   );.    pPage->m
1c213 61 73 6b 50 61 67 65 20 3d 20 28 75 31 36 29 28  askPage = (u16)(
1c214 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20  pBt->pageSize - 
1c215 31 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e  1);.    pPage->n
1c216 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20  Overflow = 0;.  
1c217 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70    usableSize = p
1c218 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a  Bt->usableSize;.
1c219 20 20 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f      pPage->cellO
1c21a 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f 66 66 73  ffset = cellOffs
1c21b 65 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20  et = hdr + 12 - 
1c21c 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20  4*pPage->leaf;. 
1c21d 20 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 45     pPage->aDataE
1c21e 6e 64 20 3d 20 26 64 61 74 61 5b 75 73 61 62 6c  nd = &data[usabl
1c21f 65 53 69 7a 65 5d 3b 0a 20 20 20 20 70 50 61 67  eSize];.    pPag
1c220 65 2d 3e 61 43 65 6c 6c 49 64 78 20 3d 20 26 64  e->aCellIdx = &d
1c221 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 5d 3b  ata[cellOffset];
1c222 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62  .    top = get2b
1c223 79 74 65 4e 6f 74 5a 65 72 6f 28 26 64 61 74 61  yteNotZero(&data
1c224 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70 50  [hdr+5]);.    pP
1c225 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67 65 74  age->nCell = get
1c226 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
1c227 33 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61  3]);.    if( pPa
1c228 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45 4c  ge->nCell>MX_CEL
1c229 4c 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  L(pBt) ){.      
1c22a 2f 2a 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c 73  /* To many cells
1c22b 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 70 61   for a single pa
1c22c 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 6d 75  ge.  The page mu
1c22d 73 74 20 62 65 20 63 6f 72 72 75 70 74 20 2a 2f  st be corrupt */
1c22e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
1c22f 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
1c230 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73  T;.    }.    tes
1c231 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 43  tcase( pPage->nC
1c232 65 6c 6c 3d 3d 4d 58 5f 43 45 4c 4c 28 70 42 74  ell==MX_CELL(pBt
1c233 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 6d  ) );..    /* A m
1c234 61 6c 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73  alformed databas
1c235 65 20 70 61 67 65 20 6d 69 67 68 74 20 63 61 75  e page might cau
1c236 73 65 20 75 73 20 74 6f 20 72 65 61 64 20 70 61  se us to read pa
1c237 73 74 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a  st the end.    *
1c238 2a 20 6f 66 20 70 61 67 65 20 77 68 65 6e 20 70  * of page when p
1c239 61 72 73 69 6e 67 20 61 20 63 65 6c 6c 2e 20 20  arsing a cell.  
1c23a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
1c23b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
1c23c 63 6b 20 6f 66 20 63 6f 64 65 20 63 68 65 63 6b  ck of code check
1c23d 73 20 65 61 72 6c 79 20 74 6f 20 73 65 65 20 69  s early to see i
1c23e 66 20 61 20 63 65 6c 6c 20 65 78 74 65 6e 64 73  f a cell extends
1c23f 0a 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 65  .    ** past the
1c240 20 65 6e 64 20 6f 66 20 61 20 70 61 67 65 20 62   end of a page b
1c241 6f 75 6e 64 61 72 79 20 61 6e 64 20 63 61 75 73  oundary and caus
1c242 65 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  es SQLITE_CORRUP
1c243 54 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20  T to be .    ** 
1c244 72 65 74 75 72 6e 65 64 20 69 66 20 69 74 20 64  returned if it d
1c245 6f 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  oes..    */.    
1c246 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c  iCellFirst = cel
1c247 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67  lOffset + 2*pPag
1c248 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 43  e->nCell;.    iC
1c249 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c 65  ellLast = usable
1c24a 53 69 7a 65 20 2d 20 34 3b 0a 23 69 66 20 64 65  Size - 4;.#if de
1c24b 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
1c24c 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c  BLE_OVERSIZE_CEL
1c24d 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 7b 0a 20  L_CHECK).    {. 
1c24e 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20       int i;     
1c24f 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
1c250 69 6e 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f  into the cell po
1c251 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20  inter array */. 
1c252 20 20 20 20 20 69 6e 74 20 73 7a 3b 20 20 20 20       int sz;    
1c253 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
1c254 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 20  f a cell */..   
1c255 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c     if( !pPage->l
1c256 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 2d  eaf ) iCellLast-
1c257 2d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  -;.      for(i=0
1c258 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ; i<pPage->nCell
1c259 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
1c25a 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  pc = get2byte(&d
1c25b 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 2b 69  ata[cellOffset+i
1c25c 2a 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65  *2]);.        te
1c25d 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c  stcase( pc==iCel
1c25e 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20  lFirst );.      
1c25f 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d    testcase( pc==
1c260 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20  iCellLast );.   
1c261 20 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c       if( pc<iCel
1c262 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65  lFirst || pc>iCe
1c263 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20  llLast ){.      
1c264 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1c265 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
1c266 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1c267 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50    sz = cellSizeP
1c268 74 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b  tr(pPage, &data[
1c269 70 63 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65  pc]);.        te
1c26a 73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 75  stcase( pc+sz==u
1c26b 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20  sableSize );.   
1c26c 20 20 20 20 20 69 66 28 20 70 63 2b 73 7a 3e 75       if( pc+sz>u
1c26d 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20  sableSize ){.   
1c26e 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
1c26f 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
1c270 54 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  T;.        }.   
1c271 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21     }.      if( !
1c272 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 43  pPage->leaf ) iC
1c273 65 6c 6c 4c 61 73 74 2b 2b 3b 0a 20 20 20 20 7d  ellLast++;.    }
1c274 20 20 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f    .#endif..    /
1c275 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74 6f  * Compute the to
1c276 74 61 6c 20 66 72 65 65 20 73 70 61 63 65 20 6f  tal free space o
1c277 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20  n the page */.  
1c278 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28    pc = get2byte(
1c279 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20  &data[hdr+1]);. 
1c27a 20 20 20 6e 46 72 65 65 20 3d 20 64 61 74 61 5b     nFree = data[
1c27b 68 64 72 2b 37 5d 20 2b 20 74 6f 70 3b 0a 20 20  hdr+7] + top;.  
1c27c 20 20 77 68 69 6c 65 28 20 70 63 3e 30 20 29 7b    while( pc>0 ){
1c27d 0a 20 20 20 20 20 20 75 31 36 20 6e 65 78 74 2c  .      u16 next,
1c27e 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28   size;.      if(
1c27f 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c   pc<iCellFirst |
1c280 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29  | pc>iCellLast )
1c281 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61  {.        /* Sta
1c282 72 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b  rt of free block
1c283 20 69 73 20 6f 66 66 20 74 68 65 20 70 61 67 65   is off the page
1c284 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75   */.        retu
1c285 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
1c286 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d  T_BKPT; .      }
1c287 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65  .      next = ge
1c288 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 5d  t2byte(&data[pc]
1c289 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20  );.      size = 
1c28a 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  get2byte(&data[p
1c28b 63 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28  c+2]);.      if(
1c28c 20 28 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 74   (next>0 && next
1c28d 3c 3d 70 63 2b 73 69 7a 65 2b 33 29 20 7c 7c 20  <=pc+size+3) || 
1c28e 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69  pc+size>usableSi
1c28f 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ze ){.        /*
1c290 20 46 72 65 65 20 62 6c 6f 63 6b 73 20 6d 75 73   Free blocks mus
1c291 74 20 62 65 20 69 6e 20 61 73 63 65 6e 64 69 6e  t be in ascendin
1c292 67 20 6f 72 64 65 72 2e 20 41 6e 64 20 74 68 65  g order. And the
1c293 20 6c 61 73 74 20 62 79 74 65 20 6f 66 0a 20 20   last byte of.  
1c294 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 72 65        ** the fre
1c295 65 2d 62 6c 6f 63 6b 20 6d 75 73 74 20 6c 69 65  e-block must lie
1c296 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
1c297 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20   page.  */.     
1c298 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1c299 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a  _CORRUPT_BKPT; .
1c29a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 46        }.      nF
1c29b 72 65 65 20 3d 20 6e 46 72 65 65 20 2b 20 73 69  ree = nFree + si
1c29c 7a 65 3b 0a 20 20 20 20 20 20 70 63 20 3d 20 6e  ze;.      pc = n
1c29d 65 78 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ext;.    }..    
1c29e 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  /* At this point
1c29f 2c 20 6e 46 72 65 65 20 63 6f 6e 74 61 69 6e 73  , nFree contains
1c2a0 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20   the sum of the 
1c2a1 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 73 74  offset to the st
1c2a2 61 72 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  art.    ** of th
1c2a3 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 20 61  e cell-content a
1c2a4 72 65 61 20 70 6c 75 73 20 74 68 65 20 6e 75 6d  rea plus the num
1c2a5 62 65 72 20 6f 66 20 66 72 65 65 20 62 79 74 65  ber of free byte
1c2a6 73 20 77 69 74 68 69 6e 0a 20 20 20 20 2a 2a 20  s within.    ** 
1c2a7 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74  the cell-content
1c2a8 20 61 72 65 61 2e 20 49 66 20 74 68 69 73 20 69   area. If this i
1c2a9 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  s greater than t
1c2aa 68 65 20 75 73 61 62 6c 65 2d 73 69 7a 65 0a 20  he usable-size. 
1c2ab 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 67     ** of the pag
1c2ac 65 2c 20 74 68 65 6e 20 74 68 65 20 70 61 67 65  e, then the page
1c2ad 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 74   must be corrupt
1c2ae 65 64 2e 20 54 68 69 73 20 63 68 65 63 6b 20 61  ed. This check a
1c2af 6c 73 6f 0a 20 20 20 20 2a 2a 20 73 65 72 76 65  lso.    ** serve
1c2b0 73 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74  s to verify that
1c2b1 20 74 68 65 20 6f 66 66 73 65 74 20 74 6f 20 74   the offset to t
1c2b2 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
1c2b3 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 0a 20 20 20  cell-content.   
1c2b4 20 2a 2a 20 61 72 65 61 2c 20 61 63 63 6f 72 64   ** area, accord
1c2b5 69 6e 67 20 74 6f 20 74 68 65 20 70 61 67 65 20  ing to the page 
1c2b6 68 65 61 64 65 72 2c 20 6c 69 65 73 20 77 69 74  header, lies wit
1c2b7 68 69 6e 20 74 68 65 20 70 61 67 65 2e 0a 20 20  hin the page..  
1c2b8 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 46 72    */.    if( nFr
1c2b9 65 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b  ee>usableSize ){
1c2ba 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
1c2bb 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
1c2bc 54 3b 20 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  T; .    }.    pP
1c2bd 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 28 75 31  age->nFree = (u1
1c2be 36 29 28 6e 46 72 65 65 20 2d 20 69 43 65 6c 6c  6)(nFree - iCell
1c2bf 46 69 72 73 74 29 3b 0a 20 20 20 20 70 50 61 67  First);.    pPag
1c2c0 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20  e->isInit = 1;. 
1c2c1 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
1c2c2 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
1c2c3 53 65 74 20 75 70 20 61 20 72 61 77 20 70 61 67  Set up a raw pag
1c2c4 65 20 73 6f 20 74 68 61 74 20 69 74 20 6c 6f 6f  e so that it loo
1c2c5 6b 73 20 6c 69 6b 65 20 61 20 64 61 74 61 62 61  ks like a databa
1c2c6 73 65 20 70 61 67 65 20 68 6f 6c 64 69 6e 67 0a  se page holding.
1c2c7 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 73 2e 0a 2a  ** no entries..*
1c2c8 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 7a 65  /.static void ze
1c2c9 72 6f 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a  roPage(MemPage *
1c2ca 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 73  pPage, int flags
1c2cb 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ){.  unsigned ch
1c2cc 61 72 20 2a 64 61 74 61 20 3d 20 70 50 61 67 65  ar *data = pPage
1c2cd 2d 3e 61 44 61 74 61 3b 0a 20 20 42 74 53 68 61  ->aData;.  BtSha
1c2ce 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65  red *pBt = pPage
1c2cf 2d 3e 70 42 74 3b 0a 20 20 75 38 20 68 64 72 20  ->pBt;.  u8 hdr 
1c2d0 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  = pPage->hdrOffs
1c2d1 65 74 3b 0a 20 20 75 31 36 20 66 69 72 73 74 3b  et;.  u16 first;
1c2d2 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1c2d3 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62  te3PagerPagenumb
1c2d4 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  er(pPage->pDbPag
1c2d5 65 29 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20  e)==pPage->pgno 
1c2d6 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
1c2d7 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72  ite3PagerGetExtr
1c2d8 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  a(pPage->pDbPage
1c2d9 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67  ) == (void*)pPag
1c2da 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  e );.  assert( s
1c2db 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
1c2dc 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  ta(pPage->pDbPag
1c2dd 65 29 20 3d 3d 20 64 61 74 61 20 29 3b 0a 20 20  e) == data );.  
1c2de 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
1c2df 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
1c2e0 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
1c2e1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
1c2e2 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1c2e3 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
1c2e4 20 69 66 28 20 70 42 74 2d 3e 62 74 73 46 6c 61   if( pBt->btsFla
1c2e5 67 73 20 26 20 42 54 53 5f 53 45 43 55 52 45 5f  gs & BTS_SECURE_
1c2e6 44 45 4c 45 54 45 20 29 7b 0a 20 20 20 20 6d 65  DELETE ){.    me
1c2e7 6d 73 65 74 28 26 64 61 74 61 5b 68 64 72 5d 2c  mset(&data[hdr],
1c2e8 20 30 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53   0, pBt->usableS
1c2e9 69 7a 65 20 2d 20 68 64 72 29 3b 0a 20 20 7d 0a  ize - hdr);.  }.
1c2ea 20 20 64 61 74 61 5b 68 64 72 5d 20 3d 20 28 63    data[hdr] = (c
1c2eb 68 61 72 29 66 6c 61 67 73 3b 0a 20 20 66 69 72  har)flags;.  fir
1c2ec 73 74 20 3d 20 68 64 72 20 2b 20 38 20 2b 20 34  st = hdr + 8 + 4
1c2ed 2a 28 28 66 6c 61 67 73 26 50 54 46 5f 4c 45 41  *((flags&PTF_LEA
1c2ee 46 29 3d 3d 30 20 3f 31 3a 30 29 3b 0a 20 20 6d  F)==0 ?1:0);.  m
1c2ef 65 6d 73 65 74 28 26 64 61 74 61 5b 68 64 72 2b  emset(&data[hdr+
1c2f0 31 5d 2c 20 30 2c 20 34 29 3b 0a 20 20 64 61 74  1], 0, 4);.  dat
1c2f1 61 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20  a[hdr+7] = 0;.  
1c2f2 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  put2byte(&data[h
1c2f3 64 72 2b 35 5d 2c 20 70 42 74 2d 3e 75 73 61 62  dr+5], pBt->usab
1c2f4 6c 65 53 69 7a 65 29 3b 0a 20 20 70 50 61 67 65  leSize);.  pPage
1c2f5 2d 3e 6e 46 72 65 65 20 3d 20 28 75 31 36 29 28  ->nFree = (u16)(
1c2f6 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
1c2f7 2d 20 66 69 72 73 74 29 3b 0a 20 20 64 65 63 6f  - first);.  deco
1c2f8 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 66  deFlags(pPage, f
1c2f9 6c 61 67 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e  lags);.  pPage->
1c2fa 68 64 72 4f 66 66 73 65 74 20 3d 20 68 64 72 3b  hdrOffset = hdr;
1c2fb 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66  .  pPage->cellOf
1c2fc 66 73 65 74 20 3d 20 66 69 72 73 74 3b 0a 20 20  fset = first;.  
1c2fd 70 50 61 67 65 2d 3e 61 44 61 74 61 45 6e 64 20  pPage->aDataEnd 
1c2fe 3d 20 26 64 61 74 61 5b 70 42 74 2d 3e 75 73 61  = &data[pBt->usa
1c2ff 62 6c 65 53 69 7a 65 5d 3b 0a 20 20 70 50 61 67  bleSize];.  pPag
1c300 65 2d 3e 61 43 65 6c 6c 49 64 78 20 3d 20 26 64  e->aCellIdx = &d
1c301 61 74 61 5b 66 69 72 73 74 5d 3b 0a 20 20 70 50  ata[first];.  pP
1c302 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d  age->nOverflow =
1c303 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42   0;.  assert( pB
1c304 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 32  t->pageSize>=512
1c305 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 7a   && pBt->pageSiz
1c306 65 3c 3d 36 35 35 33 36 20 29 3b 0a 20 20 70 50  e<=65536 );.  pP
1c307 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20  age->maskPage = 
1c308 28 75 31 36 29 28 70 42 74 2d 3e 70 61 67 65 53  (u16)(pBt->pageS
1c309 69 7a 65 20 2d 20 31 29 3b 0a 20 20 70 50 61 67  ize - 1);.  pPag
1c30a 65 2d 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20  e->nCell = 0;.  
1c30b 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20  pPage->isInit = 
1c30c 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  1;.}.../*.** Con
1c30d 76 65 72 74 20 61 20 44 62 50 61 67 65 20 6f 62  vert a DbPage ob
1c30e 74 61 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 20  tained from the 
1c30f 70 61 67 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d  pager into a Mem
1c310 50 61 67 65 20 75 73 65 64 20 62 79 0a 2a 2a 20  Page used by.** 
1c311 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 2e  the btree layer.
1c312 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 61  .*/.static MemPa
1c313 67 65 20 2a 62 74 72 65 65 50 61 67 65 46 72 6f  ge *btreePageFro
1c314 6d 44 62 50 61 67 65 28 44 62 50 61 67 65 20 2a  mDbPage(DbPage *
1c315 70 44 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 67  pDbPage, Pgno pg
1c316 6e 6f 2c 20 42 74 53 68 61 72 65 64 20 2a 70 42  no, BtShared *pB
1c317 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  t){.  MemPage *p
1c318 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a  Page = (MemPage*
1c319 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  )sqlite3PagerGet
1c31a 45 78 74 72 61 28 70 44 62 50 61 67 65 29 3b 0a  Extra(pDbPage);.
1c31b 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d    pPage->aData =
1c31c 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
1c31d 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20  Data(pDbPage);. 
1c31e 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 20   pPage->pDbPage 
1c31f 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 70 50 61  = pDbPage;.  pPa
1c320 67 65 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20  ge->pBt = pBt;. 
1c321 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70   pPage->pgno = p
1c322 67 6e 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64  gno;.  pPage->hd
1c323 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d  rOffset = pPage-
1c324 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a  >pgno==1 ? 100 :
1c325 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61   0;.  return pPa
1c326 67 65 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  ge; .}../*.** Ge
1c327 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68  t a page from th
1c328 65 20 70 61 67 65 72 2e 20 20 49 6e 69 74 69 61  e pager.  Initia
1c329 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65  lize the MemPage
1c32a 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50  .pBt and.** MemP
1c32b 61 67 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e  age.aData elemen
1c32c 74 73 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a  ts if needed..**
1c32d 0a 2a 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e  .** If the noCon
1c32e 74 65 6e 74 20 66 6c 61 67 20 69 73 20 73 65 74  tent flag is set
1c32f 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
1c330 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61  we do not care a
1c331 62 6f 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74  bout.** the cont
1c332 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 20  ent of the page 
1c333 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 20 53  at this time.  S
1c334 6f 20 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74  o do not go to t
1c335 68 65 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65  he disk.** to fe
1c336 74 63 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e  tch the content.
1c337 20 20 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74    Just fill in t
1c338 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 20  he content with 
1c339 7a 65 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a  zeros for now..*
1c33a 2a 20 49 66 20 69 6e 20 74 68 65 20 66 75 74 75  * If in the futu
1c33b 72 65 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 74  re we call sqlit
1c33c 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 6f  e3PagerWrite() o
1c33d 6e 20 74 68 69 73 20 70 61 67 65 2c 20 74 68 61  n this page, tha
1c33e 74 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 61  t.** means we ha
1c33f 76 65 20 73 74 61 72 74 65 64 20 74 6f 20 62 65  ve started to be
1c340 20 63 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 74   concerned about
1c341 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 65   content and the
1c342 20 64 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68   disk.** read sh
1c343 6f 75 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68  ould occur at th
1c344 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61  at point..*/.sta
1c345 74 69 63 20 69 6e 74 20 62 74 72 65 65 47 65 74  tic int btreeGet
1c346 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64  Page(.  BtShared
1c347 20 2a 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20   *pBt,       /* 
1c348 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50  The btree */.  P
1c349 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20  gno pgno,       
1c34a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1c34b 20 74 68 65 20 70 61 67 65 20 74 6f 20 66 65 74   the page to fet
1c34c 63 68 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  ch */.  MemPage 
1c34d 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20  **ppPage,    /* 
1c34e 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20  Return the page 
1c34f 69 6e 20 74 68 69 73 20 70 61 72 61 6d 65 74 65  in this paramete
1c350 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e  r */.  int noCon
1c351 74 65 6e 74 20 20 20 20 20 20 20 20 2f 2a 20 44  tent        /* D
1c352 6f 20 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65 20  o not load page 
1c353 63 6f 6e 74 65 6e 74 20 69 66 20 74 72 75 65 20  content if true 
1c354 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
1c355 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67    DbPage *pDbPag
1c356 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  e;..  assert( sq
1c357 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1c358 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
1c359 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1c35a 67 65 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e  gerAcquire(pBt->
1c35b 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44  pPager, pgno, (D
1c35c 62 50 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65  bPage**)&pDbPage
1c35d 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20  , noContent);.  
1c35e 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
1c35f 72 63 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20  rc;.  *ppPage = 
1c360 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50  btreePageFromDbP
1c361 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e  age(pDbPage, pgn
1c362 6f 2c 20 70 42 74 29 3b 0a 20 20 72 65 74 75 72  o, pBt);.  retur
1c363 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1c364 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61  /*.** Retrieve a
1c365 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70   page from the p
1c366 61 67 65 72 20 63 61 63 68 65 2e 20 49 66 20 74  ager cache. If t
1c367 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67  he requested pag
1c368 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65  e is not.** alre
1c369 61 64 79 20 69 6e 20 74 68 65 20 70 61 67 65 72  ady in the pager
1c36a 20 63 61 63 68 65 20 72 65 74 75 72 6e 20 4e 55   cache return NU
1c36b 4c 4c 2e 20 49 6e 69 74 69 61 6c 69 7a 65 20 74  LL. Initialize t
1c36c 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61  he MemPage.pBt a
1c36d 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44  nd.** MemPage.aD
1c36e 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20  ata elements if 
1c36f 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  needed..*/.stati
1c370 63 20 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 65  c MemPage *btree
1c371 50 61 67 65 4c 6f 6f 6b 75 70 28 42 74 53 68 61  PageLookup(BtSha
1c372 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70  red *pBt, Pgno p
1c373 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a  gno){.  DbPage *
1c374 70 44 62 50 61 67 65 3b 0a 20 20 61 73 73 65 72  pDbPage;.  asser
1c375 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1c376 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
1c377 29 20 29 3b 0a 20 20 70 44 62 50 61 67 65 20 3d  ) );.  pDbPage =
1c378 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f   sqlite3PagerLoo
1c379 6b 75 70 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  kup(pBt->pPager,
1c37a 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 44   pgno);.  if( pD
1c37b 62 50 61 67 65 20 29 7b 0a 20 20 20 20 72 65 74  bPage ){.    ret
1c37c 75 72 6e 20 62 74 72 65 65 50 61 67 65 46 72 6f  urn btreePageFro
1c37d 6d 44 62 50 61 67 65 28 70 44 62 50 61 67 65 2c  mDbPage(pDbPage,
1c37e 20 70 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 7d   pgno, pBt);.  }
1c37f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
1c380 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
1c381 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
1c382 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61  abase file in pa
1c383 67 65 73 2e 20 49 66 20 74 68 65 72 65 20 69 73  ges. If there is
1c384 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20   any kind of.** 
1c385 65 72 72 6f 72 2c 20 72 65 74 75 72 6e 20 28 28  error, return ((
1c386 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 2d 31 29  unsigned int)-1)
1c387 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f  ..*/.static Pgno
1c388 20 62 74 72 65 65 50 61 67 65 63 6f 75 6e 74 28   btreePagecount(
1c389 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
1c38a 20 20 72 65 74 75 72 6e 20 70 42 74 2d 3e 6e 50    return pBt->nP
1c38b 61 67 65 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  age;.}.SQLITE_PR
1c38c 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65  IVATE u32 sqlite
1c38d 33 42 74 72 65 65 4c 61 73 74 50 61 67 65 28 42  3BtreeLastPage(B
1c38e 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65  tree *p){.  asse
1c38f 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
1c390 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b  HoldsMutex(p) );
1c391 0a 20 20 61 73 73 65 72 74 28 20 28 28 70 2d 3e  .  assert( ((p->
1c392 70 42 74 2d 3e 6e 50 61 67 65 29 26 30 78 38 30  pBt->nPage)&0x80
1c393 30 30 30 30 30 29 3d 3d 30 20 29 3b 0a 20 20 72  00000)==0 );.  r
1c394 65 74 75 72 6e 20 28 69 6e 74 29 62 74 72 65 65  eturn (int)btree
1c395 50 61 67 65 63 6f 75 6e 74 28 70 2d 3e 70 42 74  Pagecount(p->pBt
1c396 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20  );.}../*.** Get 
1c397 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
1c398 70 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69 61  pager and initia
1c399 6c 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20 72  lize it.  This r
1c39a 6f 75 74 69 6e 65 20 69 73 20 6a 75 73 74 20 61  outine is just a
1c39b 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20  .** convenience 
1c39c 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73  wrapper around s
1c39d 65 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74 6f  eparate calls to
1c39e 20 62 74 72 65 65 47 65 74 50 61 67 65 28 29 20   btreeGetPage() 
1c39f 61 6e 64 20 0a 2a 2a 20 62 74 72 65 65 49 6e 69  and .** btreeIni
1c3a0 74 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49  tPage()..**.** I
1c3a1 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
1c3a2 73 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  s, then the valu
1c3a3 65 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74  e *ppPage is set
1c3a4 20 74 6f 20 69 73 20 75 6e 64 65 66 69 6e 65 64   to is undefined
1c3a5 2e 20 49 74 0a 2a 2a 20 6d 61 79 20 72 65 6d 61  . It.** may rema
1c3a6 69 6e 20 75 6e 63 68 61 6e 67 65 64 2c 20 6f 72  in unchanged, or
1c3a7 20 69 74 20 6d 61 79 20 62 65 20 73 65 74 20 74   it may be set t
1c3a8 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 76 61 6c  o an invalid val
1c3a9 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ue..*/.static in
1c3aa 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65  t getAndInitPage
1c3ab 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  (.  BtShared *pB
1c3ac 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  t,          /* T
1c3ad 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1c3ae 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c   */.  Pgno pgno,
1c3af 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1c3b0 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 65  mber of the page
1c3b1 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65 6d   to get */.  Mem
1c3b2 50 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20 20  Page **ppPage   
1c3b3 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70    /* Write the p
1c3b4 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72 65  age pointer here
1c3b5 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
1c3b6 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1c3b7 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
1c3b8 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  t->mutex) );..  
1c3b9 69 66 28 20 70 67 6e 6f 3e 62 74 72 65 65 50 61  if( pgno>btreePa
1c3ba 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a  gecount(pBt) ){.
1c3bb 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1c3bc 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1c3bd 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
1c3be 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  btreeGetPage(pBt
1c3bf 2c 20 70 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20  , pgno, ppPage, 
1c3c0 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  0);.    if( rc==
1c3c1 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1c3c2 20 20 20 72 63 20 3d 20 62 74 72 65 65 49 6e 69     rc = btreeIni
1c3c3 74 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a  tPage(*ppPage);.
1c3c4 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1c3c5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1c3c6 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a     releasePage(*
1c3c7 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d  ppPage);.      }
1c3c8 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 74 65  .    }.  }..  te
1c3c9 73 74 63 61 73 65 28 20 70 67 6e 6f 3d 3d 30 20  stcase( pgno==0 
1c3ca 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 67 6e  );.  assert( pgn
1c3cb 6f 21 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  o!=0 || rc==SQLI
1c3cc 54 45 5f 43 4f 52 52 55 50 54 20 29 3b 0a 20 20  TE_CORRUPT );.  
1c3cd 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1c3ce 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 4d 65  .** Release a Me
1c3cf 6d 50 61 67 65 2e 20 20 54 68 69 73 20 73 68 6f  mPage.  This sho
1c3d0 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e  uld be called on
1c3d1 63 65 20 66 6f 72 20 65 61 63 68 20 70 72 69 6f  ce for each prio
1c3d2 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 62 74 72  r.** call to btr
1c3d3 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f 0a 73 74  eeGetPage..*/.st
1c3d4 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73  atic void releas
1c3d5 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70  ePage(MemPage *p
1c3d6 50 61 67 65 29 7b 0a 20 20 69 66 28 20 70 50 61  Page){.  if( pPa
1c3d7 67 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  ge ){.    assert
1c3d8 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 29  ( pPage->aData )
1c3d9 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
1c3da 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20  age->pBt );.    
1c3db 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
1c3dc 61 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61  agerGetExtra(pPa
1c3dd 67 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20  ge->pDbPage) == 
1c3de 28 76 6f 69 64 2a 29 70 50 61 67 65 20 29 3b 0a  (void*)pPage );.
1c3df 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
1c3e0 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
1c3e1 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3d  pPage->pDbPage)=
1c3e2 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 20 29 3b  =pPage->aData );
1c3e3 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
1c3e4 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1c3e5 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
1c3e6 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  x) );.    sqlite
1c3e7 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67  3PagerUnref(pPag
1c3e8 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d  e->pDbPage);.  }
1c3e9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67  .}../*.** During
1c3ea 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 77 68 65   a rollback, whe
1c3eb 6e 20 74 68 65 20 70 61 67 65 72 20 72 65 6c 6f  n the pager relo
1c3ec 61 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ads information 
1c3ed 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65 0a 2a  into the cache.*
1c3ee 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 61  * so that the ca
1c3ef 63 68 65 20 69 73 20 72 65 73 74 6f 72 65 64 20  che is restored 
1c3f0 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20  to its original 
1c3f1 73 74 61 74 65 20 61 74 20 74 68 65 20 73 74 61  state at the sta
1c3f2 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 74 72 61  rt of.** the tra
1c3f3 6e 73 61 63 74 69 6f 6e 2c 20 66 6f 72 20 65 61  nsaction, for ea
1c3f4 63 68 20 70 61 67 65 20 72 65 73 74 6f 72 65 64  ch page restored
1c3f5 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1c3f6 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54   called..**.** T
1c3f7 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 65 64  his routine need
1c3f8 73 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 65  s to reset the e
1c3f9 78 74 72 61 20 64 61 74 61 20 73 65 63 74 69 6f  xtra data sectio
1c3fa 6e 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  n at the end of 
1c3fb 74 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f 20 61  the.** page to a
1c3fc 67 72 65 65 20 77 69 74 68 20 74 68 65 20 72 65  gree with the re
1c3fd 73 74 6f 72 65 64 20 64 61 74 61 2e 0a 2a 2f 0a  stored data..*/.
1c3fe 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65  static void page
1c3ff 52 65 69 6e 69 74 28 44 62 50 61 67 65 20 2a 70  Reinit(DbPage *p
1c400 44 61 74 61 29 7b 0a 20 20 4d 65 6d 50 61 67 65  Data){.  MemPage
1c401 20 2a 70 50 61 67 65 3b 0a 20 20 70 50 61 67 65   *pPage;.  pPage
1c402 20 3d 20 28 4d 65 6d 50 61 67 65 20 2a 29 73 71   = (MemPage *)sq
1c403 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74  lite3PagerGetExt
1c404 72 61 28 70 44 61 74 61 29 3b 0a 20 20 61 73 73  ra(pData);.  ass
1c405 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
1c406 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 44  rPageRefcount(pD
1c407 61 74 61 29 3e 30 20 29 3b 0a 20 20 69 66 28 20  ata)>0 );.  if( 
1c408 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 7b  pPage->isInit ){
1c409 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
1c40a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1c40b 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
1c40c 78 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d  x) );.    pPage-
1c40d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20  >isInit = 0;.   
1c40e 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65   if( sqlite3Page
1c40f 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 44  rPageRefcount(pD
1c410 61 74 61 29 3e 31 20 29 7b 0a 20 20 20 20 20 20  ata)>1 ){.      
1c411 2f 2a 20 70 50 61 67 65 20 6d 69 67 68 74 20 6e  /* pPage might n
1c412 6f 74 20 62 65 20 61 20 62 74 72 65 65 20 70 61  ot be a btree pa
1c413 67 65 3b 20 20 69 74 20 6d 69 67 68 74 20 62 65  ge;  it might be
1c414 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
1c415 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 70 74  e.      ** or pt
1c416 72 6d 61 70 20 70 61 67 65 20 6f 72 20 61 20 66  rmap page or a f
1c417 72 65 65 20 70 61 67 65 2e 20 20 49 6e 20 74 68  ree page.  In th
1c418 6f 73 65 20 63 61 73 65 73 2c 20 74 68 65 20 66  ose cases, the f
1c419 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 20 20 2a  ollowing.      *
1c41a 2a 20 63 61 6c 6c 20 74 6f 20 62 74 72 65 65 49  * call to btreeI
1c41b 6e 69 74 50 61 67 65 28 29 20 77 69 6c 6c 20 6c  nitPage() will l
1c41c 69 6b 65 6c 79 20 72 65 74 75 72 6e 20 53 51 4c  ikely return SQL
1c41d 49 54 45 5f 43 4f 52 52 55 50 54 2e 0a 20 20 20  ITE_CORRUPT..   
1c41e 20 20 20 2a 2a 20 42 75 74 20 6e 6f 20 68 61 72     ** But no har
1c41f 6d 20 69 73 20 64 6f 6e 65 20 62 79 20 74 68 69  m is done by thi
1c420 73 2e 20 20 41 6e 64 20 69 74 20 69 73 20 76 65  s.  And it is ve
1c421 72 79 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61  ry important tha
1c422 74 0a 20 20 20 20 20 20 2a 2a 20 62 74 72 65 65  t.      ** btree
1c423 49 6e 69 74 50 61 67 65 28 29 20 62 65 20 63 61  InitPage() be ca
1c424 6c 6c 65 64 20 6f 6e 20 65 76 65 72 79 20 62 74  lled on every bt
1c425 72 65 65 20 70 61 67 65 20 73 6f 20 77 65 20 6d  ree page so we m
1c426 61 6b 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  ake.      ** the
1c427 20 63 61 6c 6c 20 66 6f 72 20 65 76 65 72 79 20   call for every 
1c428 70 61 67 65 20 74 68 61 74 20 63 6f 6d 65 73 20  page that comes 
1c429 69 6e 20 66 6f 72 20 72 65 2d 69 6e 69 74 69 6e  in for re-initin
1c42a 67 2e 20 2a 2f 0a 20 20 20 20 20 20 62 74 72 65  g. */.      btre
1c42b 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29  eInitPage(pPage)
1c42c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
1c42d 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20  *.** Invoke the 
1c42e 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f 72  busy handler for
1c42f 20 61 20 62 74 72 65 65 2e 0a 2a 2f 0a 73 74 61   a btree..*/.sta
1c430 74 69 63 20 69 6e 74 20 62 74 72 65 65 49 6e 76  tic int btreeInv
1c431 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 76  okeBusyHandler(v
1c432 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 42 74  oid *pArg){.  Bt
1c433 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 28 42  Shared *pBt = (B
1c434 74 53 68 61 72 65 64 2a 29 70 41 72 67 3b 0a 20  tShared*)pArg;. 
1c435 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 64 62   assert( pBt->db
1c436 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
1c437 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1c438 28 70 42 74 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  (pBt->db->mutex)
1c439 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c   );.  return sql
1c43a 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61  ite3InvokeBusyHa
1c43b 6e 64 6c 65 72 28 26 70 42 74 2d 3e 64 62 2d 3e  ndler(&pBt->db->
1c43c 62 75 73 79 48 61 6e 64 6c 65 72 29 3b 0a 7d 0a  busyHandler);.}.
1c43d 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 64 61  ./*.** Open a da
1c43e 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20  tabase file..** 
1c43f 0a 2a 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73  .** zFilename is
1c440 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1c441 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
1c442 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73   If zFilename is
1c443 20 4e 55 4c 4c 0a 2a 2a 20 74 68 65 6e 20 61 6e   NULL.** then an
1c444 20 65 70 68 65 6d 65 72 61 6c 20 64 61 74 61 62   ephemeral datab
1c445 61 73 65 20 69 73 20 63 72 65 61 74 65 64 2e 20  ase is created. 
1c446 20 54 68 65 20 65 70 68 65 6d 65 72 61 6c 20 64   The ephemeral d
1c447 61 74 61 62 61 73 65 20 6d 69 67 68 74 0a 2a 2a  atabase might.**
1c448 20 62 65 20 65 78 63 6c 75 73 69 76 65 6c 79 20   be exclusively 
1c449 69 6e 20 6d 65 6d 6f 72 79 2c 20 6f 72 20 69 74  in memory, or it
1c44a 20 6d 69 67 68 74 20 75 73 65 20 61 20 64 69 73   might use a dis
1c44b 6b 2d 62 61 73 65 64 20 6d 65 6d 6f 72 79 20 63  k-based memory c
1c44c 61 63 68 65 2e 0a 2a 2a 20 45 69 74 68 65 72 20  ache..** Either 
1c44d 77 61 79 2c 20 74 68 65 20 65 70 68 65 6d 65 72  way, the ephemer
1c44e 61 6c 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c  al database will
1c44f 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   be automaticall
1c450 79 20 64 65 6c 65 74 65 64 20 0a 2a 2a 20 77 68  y deleted .** wh
1c451 65 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  en sqlite3BtreeC
1c452 6c 6f 73 65 28 29 20 69 73 20 63 61 6c 6c 65 64  lose() is called
1c453 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 46 69 6c 65  ..**.** If zFile
1c454 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79  name is ":memory
1c455 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d 65  :" then an in-me
1c456 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 69 73  mory database is
1c457 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 68 61 74   created.** that
1c458 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
1c459 79 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e  y destroyed when
1c45a 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a   it is closed..*
1c45b 2a 0a 2a 2a 20 54 68 65 20 22 66 6c 61 67 73 22  *.** The "flags"
1c45c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20   parameter is a 
1c45d 62 69 74 6d 61 73 6b 20 74 68 61 74 20 6d 69 67  bitmask that mig
1c45e 68 74 20 63 6f 6e 74 61 69 6e 20 62 69 74 73 20  ht contain bits 
1c45f 6c 69 6b 65 0a 2a 2a 20 42 54 52 45 45 5f 4f 4d  like.** BTREE_OM
1c460 49 54 5f 4a 4f 55 52 4e 41 4c 20 61 6e 64 2f 6f  IT_JOURNAL and/o
1c461 72 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 2e 0a  r BTREE_MEMORY..
1c462 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74  **.** If the dat
1c463 61 62 61 73 65 20 69 73 20 61 6c 72 65 61 64 79  abase is already
1c464 20 6f 70 65 6e 65 64 20 69 6e 20 74 68 65 20 73   opened in the s
1c465 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ame database con
1c466 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 77  nection.** and w
1c467 65 20 61 72 65 20 69 6e 20 73 68 61 72 65 64 20  e are in shared 
1c468 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e  cache mode, then
1c469 20 74 68 65 20 6f 70 65 6e 20 77 69 6c 6c 20 66   the open will f
1c46a 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53  ail with an.** S
1c46b 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
1c46c 20 65 72 72 6f 72 2e 20 20 57 65 20 63 61 6e 6e   error.  We cann
1c46d 6f 74 20 61 6c 6c 6f 77 20 74 77 6f 20 6f 72 20  ot allow two or 
1c46e 6d 6f 72 65 20 42 74 53 68 61 72 65 64 0a 2a 2a  more BtShared.**
1c46f 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20   objects in the 
1c470 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
1c471 6e 6e 65 63 74 69 6f 6e 20 73 69 6e 63 65 20 64  nnection since d
1c472 6f 69 6e 67 20 73 6f 20 77 69 6c 6c 20 6c 65 61  oing so will lea
1c473 64 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c 65 6d 73  d.** to problems
1c474 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a   with locking..*
1c475 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1c476 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1c477 65 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  eOpen(.  sqlite3
1c478 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
1c479 20 2f 2a 20 56 46 53 20 74 6f 20 75 73 65 20 66   /* VFS to use f
1c47a 6f 72 20 74 68 69 73 20 62 2d 74 72 65 65 20 2a  or this b-tree *
1c47b 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1c47c 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e  zFilename,  /* N
1c47d 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ame of the file 
1c47e 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 42  containing the B
1c47f 54 72 65 65 20 64 61 74 61 62 61 73 65 20 2a 2f  Tree database */
1c480 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
1c481 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73             /* As
1c482 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73  sociated databas
1c483 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74  e handle */.  Bt
1c484 72 65 65 20 2a 2a 70 70 42 74 72 65 65 2c 20 20  ree **ppBtree,  
1c485 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
1c486 20 74 6f 20 6e 65 77 20 42 74 72 65 65 20 6f 62   to new Btree ob
1c487 6a 65 63 74 20 77 72 69 74 74 65 6e 20 68 65 72  ject written her
1c488 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  e */.  int flags
1c489 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1c48a 2a 20 4f 70 74 69 6f 6e 73 20 2a 2f 0a 20 20 69  * Options */.  i
1c48b 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 20  nt vfsFlags     
1c48c 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
1c48d 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74  passed through t
1c48e 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f  o sqlite3_vfs.xO
1c48f 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a 20 20 42 74  pen() */.){.  Bt
1c490 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 30 3b  Shared *pBt = 0;
1c491 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c492 53 68 61 72 65 64 20 70 61 72 74 20 6f 66 20 62  Shared part of b
1c493 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
1c494 2f 0a 20 20 42 74 72 65 65 20 2a 70 3b 20 20 20  /.  Btree *p;   
1c495 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c496 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20     /* Handle to 
1c497 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69  return */.  sqli
1c498 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
1c499 4f 70 65 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 72  Open = 0;  /* Pr
1c49a 65 76 65 6e 74 73 20 61 20 72 61 63 65 20 63 6f  events a race co
1c49b 6e 64 69 74 69 6f 6e 2e 20 54 69 63 6b 65 74 20  ndition. Ticket 
1c49c 23 33 35 33 37 20 2a 2f 0a 20 20 69 6e 74 20 72  #3537 */.  int r
1c49d 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
1c49e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
1c49f 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68  ult code from th
1c4a0 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  is function */. 
1c4a1 20 75 38 20 6e 52 65 73 65 72 76 65 3b 20 20 20   u8 nReserve;   
1c4a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c4a3 2f 2a 20 42 79 74 65 20 6f 66 20 75 6e 75 73 65  /* Byte of unuse
1c4a4 64 20 73 70 61 63 65 20 6f 6e 20 65 61 63 68 20  d space on each 
1c4a5 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  page */.  unsign
1c4a6 65 64 20 63 68 61 72 20 7a 44 62 48 65 61 64 65  ed char zDbHeade
1c4a7 72 5b 31 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61  r[100];  /* Data
1c4a8 62 61 73 65 20 68 65 61 64 65 72 20 63 6f 6e 74  base header cont
1c4a9 65 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 54 72 75  ent */..  /* Tru
1c4aa 65 20 69 66 20 6f 70 65 6e 69 6e 67 20 61 6e 20  e if opening an 
1c4ab 65 70 68 65 6d 65 72 61 6c 2c 20 74 65 6d 70 6f  ephemeral, tempo
1c4ac 72 61 72 79 20 64 61 74 61 62 61 73 65 20 2a 2f  rary database */
1c4ad 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 54  .  const int isT
1c4ae 65 6d 70 44 62 20 3d 20 7a 46 69 6c 65 6e 61 6d  empDb = zFilenam
1c4af 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d  e==0 || zFilenam
1c4b0 65 5b 30 5d 3d 3d 30 3b 0a 0a 20 20 2f 2a 20 53  e[0]==0;..  /* S
1c4b1 65 74 20 74 68 65 20 76 61 72 69 61 62 6c 65 20  et the variable 
1c4b2 69 73 4d 65 6d 64 62 20 74 6f 20 74 72 75 65 20  isMemdb to true 
1c4b3 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  for an in-memory
1c4b4 20 64 61 74 61 62 61 73 65 2c 20 6f 72 20 0a 20   database, or . 
1c4b5 20 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20 61 20   ** false for a 
1c4b6 66 69 6c 65 2d 62 61 73 65 64 20 64 61 74 61 62  file-based datab
1c4b7 61 73 65 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66  ase..  */.#ifdef
1c4b8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d   SQLITE_OMIT_MEM
1c4b9 4f 52 59 44 42 0a 20 20 63 6f 6e 73 74 20 69 6e  ORYDB.  const in
1c4ba 74 20 69 73 4d 65 6d 64 62 20 3d 20 30 3b 0a 23  t isMemdb = 0;.#
1c4bb 65 6c 73 65 0a 20 20 63 6f 6e 73 74 20 69 6e 74  else.  const int
1c4bc 20 69 73 4d 65 6d 64 62 20 3d 20 28 7a 46 69 6c   isMemdb = (zFil
1c4bd 65 6e 61 6d 65 20 26 26 20 73 74 72 63 6d 70 28  ename && strcmp(
1c4be 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 3a 6d 65 6d  zFilename, ":mem
1c4bf 6f 72 79 3a 22 29 3d 3d 30 29 0a 20 20 20 20 20  ory:")==0).     
1c4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c4c1 20 20 7c 7c 20 28 69 73 54 65 6d 70 44 62 20 26    || (isTempDb &
1c4c2 26 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d  & sqlite3TempInM
1c4c3 65 6d 6f 72 79 28 64 62 29 29 0a 20 20 20 20 20  emory(db)).     
1c4c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c4c5 20 20 7c 7c 20 28 76 66 73 46 6c 61 67 73 20 26    || (vfsFlags &
1c4c6 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 45 4d   SQLITE_OPEN_MEM
1c4c7 4f 52 59 29 21 3d 30 3b 0a 23 65 6e 64 69 66 0a  ORY)!=0;.#endif.
1c4c8 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30  .  assert( db!=0
1c4c9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56   );.  assert( pV
1c4ca 66 73 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  fs!=0 );.  asser
1c4cb 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1c4cc 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
1c4cd 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 66   );.  assert( (f
1c4ce 6c 61 67 73 26 30 78 66 66 29 3d 3d 66 6c 61 67  lags&0xff)==flag
1c4cf 73 20 29 3b 20 20 20 2f 2a 20 66 6c 61 67 73 20  s );   /* flags 
1c4d0 66 69 74 20 69 6e 20 38 20 62 69 74 73 20 2a 2f  fit in 8 bits */
1c4d1 0a 0a 20 20 2f 2a 20 4f 6e 6c 79 20 61 20 42 54  ..  /* Only a BT
1c4d2 52 45 45 5f 53 49 4e 47 4c 45 20 64 61 74 61 62  REE_SINGLE datab
1c4d3 61 73 65 20 63 61 6e 20 62 65 20 42 54 52 45 45  ase can be BTREE
1c4d4 5f 55 4e 4f 52 44 45 52 45 44 20 2a 2f 0a 20 20  _UNORDERED */.  
1c4d5 61 73 73 65 72 74 28 20 28 66 6c 61 67 73 20 26  assert( (flags &
1c4d6 20 42 54 52 45 45 5f 55 4e 4f 52 44 45 52 45 44   BTREE_UNORDERED
1c4d7 29 3d 3d 30 20 7c 7c 20 28 66 6c 61 67 73 20 26  )==0 || (flags &
1c4d8 20 42 54 52 45 45 5f 53 49 4e 47 4c 45 29 21 3d   BTREE_SINGLE)!=
1c4d9 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 20 42 54 52  0 );..  /* A BTR
1c4da 45 45 5f 53 49 4e 47 4c 45 20 64 61 74 61 62 61  EE_SINGLE databa
1c4db 73 65 20 69 73 20 61 6c 77 61 79 73 20 61 20 74  se is always a t
1c4dc 65 6d 70 6f 72 61 72 79 20 61 6e 64 2f 6f 72 20  emporary and/or 
1c4dd 65 70 68 65 6d 65 72 61 6c 20 2a 2f 0a 20 20 61  ephemeral */.  a
1c4de 73 73 65 72 74 28 20 28 66 6c 61 67 73 20 26 20  ssert( (flags & 
1c4df 42 54 52 45 45 5f 53 49 4e 47 4c 45 29 3d 3d 30  BTREE_SINGLE)==0
1c4e0 20 7c 7c 20 69 73 54 65 6d 70 44 62 20 29 3b 0a   || isTempDb );.
1c4e1 0a 20 20 69 66 28 20 69 73 4d 65 6d 64 62 20 29  .  if( isMemdb )
1c4e2 7b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 42  {.    flags |= B
1c4e3 54 52 45 45 5f 4d 45 4d 4f 52 59 3b 0a 20 20 7d  TREE_MEMORY;.  }
1c4e4 0a 20 20 69 66 28 20 28 76 66 73 46 6c 61 67 73  .  if( (vfsFlags
1c4e5 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d   & SQLITE_OPEN_M
1c4e6 41 49 4e 5f 44 42 29 21 3d 30 20 26 26 20 28 69  AIN_DB)!=0 && (i
1c4e7 73 4d 65 6d 64 62 20 7c 7c 20 69 73 54 65 6d 70  sMemdb || isTemp
1c4e8 44 62 29 20 29 7b 0a 20 20 20 20 76 66 73 46 6c  Db) ){.    vfsFl
1c4e9 61 67 73 20 3d 20 28 76 66 73 46 6c 61 67 73 20  ags = (vfsFlags 
1c4ea 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  & ~SQLITE_OPEN_M
1c4eb 41 49 4e 5f 44 42 29 20 7c 20 53 51 4c 49 54 45  AIN_DB) | SQLITE
1c4ec 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20  _OPEN_TEMP_DB;. 
1c4ed 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33   }.  p = sqlite3
1c4ee 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f  MallocZero(sizeo
1c4ef 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69 66 28  f(Btree));.  if(
1c4f0 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72   !p ){.    retur
1c4f1 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
1c4f2 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61 6e 73    }.  p->inTrans
1c4f3 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20   = TRANS_NONE;. 
1c4f4 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 23 69 66   p->db = db;.#if
1c4f5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1c4f6 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20  _SHARED_CACHE.  
1c4f7 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 20 3d  p->lock.pBtree =
1c4f8 20 70 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 69 54   p;.  p->lock.iT
1c4f9 61 62 6c 65 20 3d 20 31 3b 0a 23 65 6e 64 69 66  able = 1;.#endif
1c4fa 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
1c4fb 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
1c4fc 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66  D_CACHE) && !def
1c4fd 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1c4fe 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20  _DISKIO).  /*.  
1c4ff 2a 2a 20 49 66 20 74 68 69 73 20 42 74 72 65 65  ** If this Btree
1c500 20 69 73 20 61 20 63 61 6e 64 69 64 61 74 65 20   is a candidate 
1c501 66 6f 72 20 73 68 61 72 65 64 20 63 61 63 68 65  for shared cache
1c502 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 6e  , try to find an
1c503 0a 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 42  .  ** existing B
1c504 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74  tShared object t
1c505 68 61 74 20 77 65 20 63 61 6e 20 73 68 61 72 65  hat we can share
1c506 20 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28   with.  */.  if(
1c507 20 69 73 54 65 6d 70 44 62 3d 3d 30 20 26 26 20   isTempDb==0 && 
1c508 28 69 73 4d 65 6d 64 62 3d 3d 30 20 7c 7c 20 28  (isMemdb==0 || (
1c509 76 66 73 46 6c 61 67 73 26 53 51 4c 49 54 45 5f  vfsFlags&SQLITE_
1c50a 4f 50 45 4e 5f 55 52 49 29 21 3d 30 29 20 29 7b  OPEN_URI)!=0) ){
1c50b 0a 20 20 20 20 69 66 28 20 76 66 73 46 6c 61 67  .    if( vfsFlag
1c50c 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
1c50d 53 48 41 52 45 44 43 41 43 48 45 20 29 7b 0a 20  SHAREDCACHE ){. 
1c50e 20 20 20 20 20 69 6e 74 20 6e 46 75 6c 6c 50 61       int nFullPa
1c50f 74 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d  thname = pVfs->m
1c510 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20  xPathname+1;.   
1c511 20 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 50 61     char *zFullPa
1c512 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  thname = sqlite3
1c513 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 50 61 74 68  Malloc(nFullPath
1c514 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 4d 55 54  name);.      MUT
1c515 45 58 5f 4c 4f 47 49 43 28 20 73 71 6c 69 74 65  EX_LOGIC( sqlite
1c516 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68  3_mutex *mutexSh
1c517 61 72 65 64 3b 20 29 0a 20 20 20 20 20 20 70 2d  ared; ).      p-
1c518 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20  >sharable = 1;. 
1c519 20 20 20 20 20 69 66 28 20 21 7a 46 75 6c 6c 50       if( !zFullP
1c51a 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20  athname ){.     
1c51b 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1c51c 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  p);.        retu
1c51d 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1c51e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1c51f 66 28 20 69 73 4d 65 6d 64 62 20 29 7b 0a 20 20  f( isMemdb ){.  
1c520 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 46 75        memcpy(zFu
1c521 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 7a 46 69 6c  llPathname, zFil
1c522 65 6e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74  ename, sqlite3St
1c523 72 6c 65 6e 33 30 28 7a 46 69 6c 65 6e 61 6d 65  rlen30(zFilename
1c524 29 2b 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  )+1);.      }els
1c525 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  e{.        rc = 
1c526 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74  sqlite3OsFullPat
1c527 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c  hname(pVfs, zFil
1c528 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  ename,.         
1c529 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c52a 20 20 20 20 20 20 20 20 20 20 6e 46 75 6c 6c 50            nFullP
1c52b 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 50 61  athname, zFullPa
1c52c 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  thname);.       
1c52d 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
1c52e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
1c52f 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  e(zFullPathname)
1c530 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
1c531 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20  te3_free(p);.   
1c532 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1c533 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1c534 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54    }.#if SQLITE_T
1c535 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 20 20  HREADSAFE.      
1c536 6d 75 74 65 78 4f 70 65 6e 20 3d 20 73 71 6c 69  mutexOpen = sqli
1c537 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
1c538 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
1c539 43 5f 4f 50 45 4e 29 3b 0a 20 20 20 20 20 20 73  C_OPEN);.      s
1c53a 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
1c53b 65 72 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20  er(mutexOpen);. 
1c53c 20 20 20 20 20 6d 75 74 65 78 53 68 61 72 65 64       mutexShared
1c53d 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
1c53e 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
1c53f 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
1c540 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
1c541 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
1c542 78 53 68 61 72 65 64 29 3b 0a 23 65 6e 64 69 66  xShared);.#endif
1c543 0a 20 20 20 20 20 20 66 6f 72 28 70 42 74 3d 47  .      for(pBt=G
1c544 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c  LOBAL(BtShared*,
1c545 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63  sqlite3SharedCac
1c546 68 65 4c 69 73 74 29 3b 20 70 42 74 3b 20 70 42  heList); pBt; pB
1c547 74 3d 70 42 74 2d 3e 70 4e 65 78 74 29 7b 0a 20  t=pBt->pNext){. 
1c548 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1c549 42 74 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20  Bt->nRef>0 );.  
1c54a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 74 72        if( 0==str
1c54b 63 6d 70 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d  cmp(zFullPathnam
1c54c 65 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 46  e, sqlite3PagerF
1c54d 69 6c 65 6e 61 6d 65 28 70 42 74 2d 3e 70 50 61  ilename(pBt->pPa
1c54e 67 65 72 2c 20 30 29 29 0a 20 20 20 20 20 20 20  ger, 0)).       
1c54f 20 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c            && sql
1c550 69 74 65 33 50 61 67 65 72 56 66 73 28 70 42 74  ite3PagerVfs(pBt
1c551 2d 3e 70 50 61 67 65 72 29 3d 3d 70 56 66 73 20  ->pPager)==pVfs 
1c552 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
1c553 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 20 20   iDb;.          
1c554 66 6f 72 28 69 44 62 3d 64 62 2d 3e 6e 44 62 2d  for(iDb=db->nDb-
1c555 31 3b 20 69 44 62 3e 3d 30 3b 20 69 44 62 2d 2d  1; iDb>=0; iDb--
1c556 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 42  ){.            B
1c557 74 72 65 65 20 2a 70 45 78 69 73 74 69 6e 67 20  tree *pExisting 
1c558 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
1c559 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  Bt;.            
1c55a 69 66 28 20 70 45 78 69 73 74 69 6e 67 20 26 26  if( pExisting &&
1c55b 20 70 45 78 69 73 74 69 6e 67 2d 3e 70 42 74 3d   pExisting->pBt=
1c55c 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20  =pBt ){.        
1c55d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
1c55e 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53  tex_leave(mutexS
1c55f 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 20 20  hared);.        
1c560 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
1c561 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 4f  tex_leave(mutexO
1c562 70 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pen);.          
1c563 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
1c564 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b  (zFullPathname);
1c565 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  .              s
1c566 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
1c567 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
1c568 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53  turn SQLITE_CONS
1c569 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20  TRAINT;.        
1c56a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1c56b 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  }.          p->p
1c56c 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20  Bt = pBt;.      
1c56d 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b      pBt->nRef++;
1c56e 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
1c56f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1c570 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
1c571 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
1c572 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20  texShared);.    
1c573 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
1c574 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20  FullPathname);. 
1c575 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49     }.#ifdef SQLI
1c576 54 45 5f 44 45 42 55 47 0a 20 20 20 20 65 6c 73  TE_DEBUG.    els
1c577 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 64  e{.      /* In d
1c578 65 62 75 67 20 6d 6f 64 65 2c 20 77 65 20 6d 61  ebug mode, we ma
1c579 72 6b 20 61 6c 6c 20 70 65 72 73 69 73 74 65 6e  rk all persisten
1c57a 74 20 64 61 74 61 62 61 73 65 73 20 61 73 20 73  t databases as s
1c57b 68 61 72 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a  harable.      **
1c57c 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 79 20   even when they 
1c57d 61 72 65 20 6e 6f 74 2e 20 20 54 68 69 73 20 65  are not.  This e
1c57e 78 65 72 63 69 73 65 73 20 74 68 65 20 6c 6f 63  xercises the loc
1c57f 6b 69 6e 67 20 63 6f 64 65 20 61 6e 64 0a 20 20  king code and.  
1c580 20 20 20 20 2a 2a 20 67 69 76 65 73 20 6d 6f 72      ** gives mor
1c581 65 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 66 6f  e opportunity fo
1c582 72 20 61 73 73 65 72 74 73 28 73 71 6c 69 74 65  r asserts(sqlite
1c583 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 29 0a  3_mutex_held()).
1c584 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65        ** stateme
1c585 6e 74 73 20 74 6f 20 66 69 6e 64 20 6c 6f 63 6b  nts to find lock
1c586 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20  ing problems..  
1c587 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e      */.      p->
1c588 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20  sharable = 1;.  
1c589 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23    }.#endif.  }.#
1c58a 65 6e 64 69 66 0a 20 20 69 66 28 20 70 42 74 3d  endif.  if( pBt=
1c58b 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20  =0 ){.    /*.   
1c58c 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   ** The followin
1c58d 67 20 61 73 73 65 72 74 73 20 6d 61 6b 65 20 73  g asserts make s
1c58e 75 72 65 20 74 68 61 74 20 73 74 72 75 63 74 75  ure that structu
1c58f 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20  res used by the 
1c590 62 74 72 65 65 20 61 72 65 0a 20 20 20 20 2a 2a  btree are.    **
1c591 20 74 68 65 20 72 69 67 68 74 20 73 69 7a 65 2e   the right size.
1c592 20 20 54 68 69 73 20 69 73 20 74 6f 20 67 75 61    This is to gua
1c593 72 64 20 61 67 61 69 6e 73 74 20 73 69 7a 65 20  rd against size 
1c594 63 68 61 6e 67 65 73 20 74 68 61 74 20 72 65 73  changes that res
1c595 75 6c 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20  ult.    ** when 
1c596 63 6f 6d 70 69 6c 69 6e 67 20 6f 6e 20 61 20 64  compiling on a d
1c597 69 66 66 65 72 65 6e 74 20 61 72 63 68 69 74 65  ifferent archite
1c598 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  cture..    */.  
1c599 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
1c59a 28 69 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65  (i64)==8 || size
1c59b 6f 66 28 69 36 34 29 3d 3d 34 20 29 3b 0a 20 20  of(i64)==4 );.  
1c59c 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
1c59d 28 75 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65  (u64)==8 || size
1c59e 6f 66 28 75 36 34 29 3d 3d 34 20 29 3b 0a 20 20  of(u64)==4 );.  
1c59f 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
1c5a0 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20 20 20  (u32)==4 );.    
1c5a1 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75  assert( sizeof(u
1c5a2 31 36 29 3d 3d 32 20 29 3b 0a 20 20 20 20 61 73  16)==2 );.    as
1c5a3 73 65 72 74 28 20 73 69 7a 65 6f 66 28 50 67 6e  sert( sizeof(Pgn
1c5a4 6f 29 3d 3d 34 20 29 3b 0a 20 20 0a 20 20 20 20  o)==4 );.  .    
1c5a5 70 42 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  pBt = sqlite3Mal
1c5a6 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28  locZero( sizeof(
1c5a7 2a 70 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28  *pBt) );.    if(
1c5a8 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20   pBt==0 ){.     
1c5a9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
1c5aa 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  EM;.      goto b
1c5ab 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  tree_open_out;. 
1c5ac 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71     }.    rc = sq
1c5ad 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 70  lite3PagerOpen(p
1c5ae 56 66 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 65  Vfs, &pBt->pPage
1c5af 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20  r, zFilename,.  
1c5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c5b1 20 20 20 20 20 20 20 20 45 58 54 52 41 5f 53 49          EXTRA_SI
1c5b2 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 6c  ZE, flags, vfsFl
1c5b3 61 67 73 2c 20 70 61 67 65 52 65 69 6e 69 74 29  ags, pageReinit)
1c5b4 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1c5b5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1c5b6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1c5b7 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72  erReadFileheader
1c5b8 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 73 69 7a  (pBt->pPager,siz
1c5b9 65 6f 66 28 7a 44 62 48 65 61 64 65 72 29 2c 7a  eof(zDbHeader),z
1c5ba 44 62 48 65 61 64 65 72 29 3b 0a 20 20 20 20 7d  DbHeader);.    }
1c5bb 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1c5bc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1c5bd 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f  goto btree_open_
1c5be 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  out;.    }.    p
1c5bf 42 74 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d 20  Bt->openFlags = 
1c5c0 28 75 38 29 66 6c 61 67 73 3b 0a 20 20 20 20 70  (u8)flags;.    p
1c5c1 42 74 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20  Bt->db = db;.   
1c5c2 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
1c5c3 42 75 73 79 68 61 6e 64 6c 65 72 28 70 42 74 2d  Busyhandler(pBt-
1c5c4 3e 70 50 61 67 65 72 2c 20 62 74 72 65 65 49 6e  >pPager, btreeIn
1c5c5 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 2c  vokeBusyHandler,
1c5c6 20 70 42 74 29 3b 0a 20 20 20 20 70 2d 3e 70 42   pBt);.    p->pB
1c5c7 74 20 3d 20 70 42 74 3b 0a 20 20 0a 20 20 20 20  t = pBt;.  .    
1c5c8 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30  pBt->pCursor = 0
1c5c9 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65  ;.    pBt->pPage
1c5ca 31 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73  1 = 0;.    if( s
1c5cb 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61  qlite3PagerIsrea
1c5cc 64 6f 6e 6c 79 28 70 42 74 2d 3e 70 50 61 67 65  donly(pBt->pPage
1c5cd 72 29 20 29 20 70 42 74 2d 3e 62 74 73 46 6c 61  r) ) pBt->btsFla
1c5ce 67 73 20 7c 3d 20 42 54 53 5f 52 45 41 44 5f 4f  gs |= BTS_READ_O
1c5cf 4e 4c 59 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  NLY;.#ifdef SQLI
1c5d0 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45  TE_SECURE_DELETE
1c5d1 0a 20 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61  .    pBt->btsFla
1c5d2 67 73 20 7c 3d 20 42 54 53 5f 53 45 43 55 52 45  gs |= BTS_SECURE
1c5d3 5f 44 45 4c 45 54 45 3b 0a 23 65 6e 64 69 66 0a  _DELETE;.#endif.
1c5d4 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a      pBt->pageSiz
1c5d5 65 20 3d 20 28 7a 44 62 48 65 61 64 65 72 5b 31  e = (zDbHeader[1
1c5d6 36 5d 3c 3c 38 29 20 7c 20 28 7a 44 62 48 65 61  6]<<8) | (zDbHea
1c5d7 64 65 72 5b 31 37 5d 3c 3c 31 36 29 3b 0a 20 20  der[17]<<16);.  
1c5d8 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53    if( pBt->pageS
1c5d9 69 7a 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d 3e  ize<512 || pBt->
1c5da 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f  pageSize>SQLITE_
1c5db 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20 20  MAX_PAGE_SIZE.  
1c5dc 20 20 20 20 20 20 20 7c 7c 20 28 28 70 42 74 2d         || ((pBt-
1c5dd 3e 70 61 67 65 53 69 7a 65 2d 31 29 26 70 42 74  >pageSize-1)&pBt
1c5de 2d 3e 70 61 67 65 53 69 7a 65 29 21 3d 30 20 29  ->pageSize)!=0 )
1c5df 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67  {.      pBt->pag
1c5e0 65 53 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e 64  eSize = 0;.#ifnd
1c5e1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1c5e2 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
1c5e3 2f 2a 20 49 66 20 74 68 65 20 6d 61 67 69 63 20  /* If the magic 
1c5e4 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20  name ":memory:" 
1c5e5 77 69 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 69  will create an i
1c5e6 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
1c5e7 65 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a  e, then.      **
1c5e8 20 6c 65 61 76 65 20 74 68 65 20 61 75 74 6f 56   leave the autoV
1c5e9 61 63 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 20  acuum mode at 0 
1c5ea 28 64 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63  (do not auto-vac
1c5eb 75 75 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 20  uum), even if.  
1c5ec 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45      ** SQLITE_DE
1c5ed 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d  FAULT_AUTOVACUUM
1c5ee 20 69 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 65   is true. On the
1c5ef 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 66 0a   other hand, if.
1c5f0 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f        ** SQLITE_
1c5f1 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61  OMIT_MEMORYDB ha
1c5f2 73 20 62 65 65 6e 20 64 65 66 69 6e 65 64 2c 20  s been defined, 
1c5f3 74 68 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20  then ":memory:" 
1c5f4 69 73 20 6a 75 73 74 20 61 0a 20 20 20 20 20 20  is just a.      
1c5f5 2a 2a 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d  ** regular file-
1c5f6 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61  name. In this ca
1c5f7 73 65 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75  se the auto-vacu
1c5f8 75 6d 20 61 70 70 6c 69 65 73 20 61 73 20 70 65  um applies as pe
1c5f9 72 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20  r normal..      
1c5fa 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 46 69  */.      if( zFi
1c5fb 6c 65 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 6d  lename && !isMem
1c5fc 64 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42  db ){.        pB
1c5fd 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20  t->autoVacuum = 
1c5fe 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f  (SQLITE_DEFAULT_
1c5ff 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a  AUTOVACUUM ? 1 :
1c600 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74   0);.        pBt
1c601 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28  ->incrVacuum = (
1c602 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41  SQLITE_DEFAULT_A
1c603 55 54 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31  UTOVACUUM==2 ? 1
1c604 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23   : 0);.      }.#
1c605 65 6e 64 69 66 0a 20 20 20 20 20 20 6e 52 65 73  endif.      nRes
1c606 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65  erve = 0;.    }e
1c607 6c 73 65 7b 0a 20 20 20 20 20 20 6e 52 65 73 65  lse{.      nRese
1c608 72 76 65 20 3d 20 7a 44 62 48 65 61 64 65 72 5b  rve = zDbHeader[
1c609 32 30 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e  20];.      pBt->
1c60a 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f  btsFlags |= BTS_
1c60b 50 41 47 45 53 49 5a 45 5f 46 49 58 45 44 3b 0a  PAGESIZE_FIXED;.
1c60c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1c60d 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
1c60e 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61       pBt->autoVa
1c60f 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65  cuum = (get4byte
1c610 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b  (&zDbHeader[36 +
1c611 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20   4*4])?1:0);.   
1c612 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75     pBt->incrVacu
1c613 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26  um = (get4byte(&
1c614 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 37  zDbHeader[36 + 7
1c615 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 69  *4])?1:0);.#endi
1c616 66 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  f.    }.    rc =
1c617 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
1c618 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50  Pagesize(pBt->pP
1c619 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65  ager, &pBt->page
1c61a 53 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 3b  Size, nReserve);
1c61b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f  .    if( rc ) go
1c61c 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75  to btree_open_ou
1c61d 74 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62  t;.    pBt->usab
1c61e 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61  leSize = pBt->pa
1c61f 67 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76  geSize - nReserv
1c620 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  e;.    assert( (
1c621 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 20  pBt->pageSize & 
1c622 37 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62  7)==0 );  /* 8-b
1c623 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66  yte alignment of
1c624 20 70 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20   pageSize */.   
1c625 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
1c626 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
1c627 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69  _CACHE) && !defi
1c628 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
1c629 44 49 53 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 41  DISKIO).    /* A
1c62a 64 64 20 74 68 65 20 6e 65 77 20 42 74 53 68 61  dd the new BtSha
1c62b 72 65 64 20 6f 62 6a 65 63 74 20 74 6f 20 74 68  red object to th
1c62c 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 68  e linked list sh
1c62d 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 73  arable BtShareds
1c62e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1c62f 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a   p->sharable ){.
1c630 20 20 20 20 20 20 4d 55 54 45 58 5f 4c 4f 47 49        MUTEX_LOGI
1c631 43 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  C( sqlite3_mutex
1c632 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 20 29   *mutexShared; )
1c633 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66  .      pBt->nRef
1c634 20 3d 20 31 3b 0a 20 20 20 20 20 20 4d 55 54 45   = 1;.      MUTE
1c635 58 5f 4c 4f 47 49 43 28 20 6d 75 74 65 78 53 68  X_LOGIC( mutexSh
1c636 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75  ared = sqlite3Mu
1c637 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
1c638 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
1c639 54 45 52 29 3b 29 0a 20 20 20 20 20 20 69 66 28  TER);).      if(
1c63a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
1c63b 46 45 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f  FE && sqlite3Glo
1c63c 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d  balConfig.bCoreM
1c63d 75 74 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20  utex ){.        
1c63e 70 42 74 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c  pBt->mutex = sql
1c63f 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
1c640 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
1c641 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
1c642 42 74 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a  Bt->mutex==0 ){.
1c643 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
1c644 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
1c645 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f         db->mallo
1c646 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20  cFailed = 0;.   
1c647 20 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65         goto btre
1c648 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20  e_open_out;.    
1c649 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1c64a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1c64b 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61  x_enter(mutexSha
1c64c 72 65 64 29 3b 0a 20 20 20 20 20 20 70 42 74 2d  red);.      pBt-
1c64d 3e 70 4e 65 78 74 20 3d 20 47 4c 4f 42 41 4c 28  >pNext = GLOBAL(
1c64e 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65  BtShared*,sqlite
1c64f 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74  3SharedCacheList
1c650 29 3b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28  );.      GLOBAL(
1c651 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65  BtShared*,sqlite
1c652 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74  3SharedCacheList
1c653 29 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 73  ) = pBt;.      s
1c654 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1c655 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b  ve(mutexShared);
1c656 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
1c657 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  }..#if !defined(
1c658 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
1c659 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65  ED_CACHE) && !de
1c65a 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1c65b 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 20 49  T_DISKIO).  /* I
1c65c 66 20 74 68 65 20 6e 65 77 20 42 74 72 65 65 20  f the new Btree 
1c65d 75 73 65 73 20 61 20 73 68 61 72 61 62 6c 65 20  uses a sharable 
1c65e 70 42 74 53 68 61 72 65 64 2c 20 74 68 65 6e 20  pBtShared, then 
1c65f 6c 69 6e 6b 20 74 68 65 20 6e 65 77 0a 20 20 2a  link the new.  *
1c660 2a 20 42 74 72 65 65 20 69 6e 74 6f 20 74 68 65  * Btree into the
1c661 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73 68 61   list of all sha
1c662 72 61 62 6c 65 20 42 74 72 65 65 73 20 66 6f 72  rable Btrees for
1c663 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63   the same connec
1c664 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 6c  tion..  ** The l
1c665 69 73 74 20 69 73 20 6b 65 70 74 20 69 6e 20 61  ist is kept in a
1c666 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62  scending order b
1c667 79 20 70 42 74 20 61 64 64 72 65 73 73 2e 0a 20  y pBt address.. 
1c668 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 68 61   */.  if( p->sha
1c669 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74  rable ){.    int
1c66a 20 69 3b 0a 20 20 20 20 42 74 72 65 65 20 2a 70   i;.    Btree *p
1c66b 53 69 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  Sib;.    for(i=0
1c66c 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
1c66d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 53  ){.      if( (pS
1c66e 69 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  ib = db->aDb[i].
1c66f 70 42 74 29 21 3d 30 20 26 26 20 70 53 69 62 2d  pBt)!=0 && pSib-
1c670 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20  >sharable ){.   
1c671 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 69 62       while( pSib
1c672 2d 3e 70 50 72 65 76 20 29 7b 20 70 53 69 62 20  ->pPrev ){ pSib 
1c673 3d 20 70 53 69 62 2d 3e 70 50 72 65 76 3b 20 7d  = pSib->pPrev; }
1c674 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e  .        if( p->
1c675 70 42 74 3c 70 53 69 62 2d 3e 70 42 74 20 29 7b  pBt<pSib->pBt ){
1c676 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e  .          p->pN
1c677 65 78 74 20 3d 20 70 53 69 62 3b 0a 20 20 20 20  ext = pSib;.    
1c678 20 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d        p->pPrev =
1c679 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53   0;.          pS
1c67a 69 62 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20  ib->pPrev = p;. 
1c67b 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1c67c 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
1c67d 53 69 62 2d 3e 70 4e 65 78 74 20 26 26 20 70 53  Sib->pNext && pS
1c67e 69 62 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3c 70  ib->pNext->pBt<p
1c67f 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20  ->pBt ){.       
1c680 20 20 20 20 20 70 53 69 62 20 3d 20 70 53 69 62       pSib = pSib
1c681 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20  ->pNext;.       
1c682 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70     }.          p
1c683 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 2d 3e  ->pNext = pSib->
1c684 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20  pNext;.         
1c685 20 70 2d 3e 70 50 72 65 76 20 3d 20 70 53 69 62   p->pPrev = pSib
1c686 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
1c687 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20  p->pNext ){.    
1c688 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74          p->pNext
1c689 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20  ->pPrev = p;.   
1c68a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1c68b 20 20 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 3d     pSib->pNext =
1c68c 20 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   p;.        }.  
1c68d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1c68e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
1c68f 65 6e 64 69 66 0a 20 20 2a 70 70 42 74 72 65 65  endif.  *ppBtree
1c690 20 3d 20 70 3b 0a 0a 62 74 72 65 65 5f 6f 70 65   = p;..btree_ope
1c691 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 21  n_out:.  if( rc!
1c692 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1c693 20 20 69 66 28 20 70 42 74 20 26 26 20 70 42 74    if( pBt && pBt
1c694 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20  ->pPager ){.    
1c695 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c    sqlite3PagerCl
1c696 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29  ose(pBt->pPager)
1c697 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
1c698 74 65 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20  te3_free(pBt);. 
1c699 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1c69a 70 29 3b 0a 20 20 20 20 2a 70 70 42 74 72 65 65  p);.    *ppBtree
1c69b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
1c69c 20 20 20 2f 2a 20 49 66 20 74 68 65 20 42 2d 54     /* If the B-T
1c69d 72 65 65 20 77 61 73 20 73 75 63 63 65 73 73 66  ree was successf
1c69e 75 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 73 65 74  ully opened, set
1c69f 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65   the pager-cache
1c6a0 20 73 69 7a 65 20 74 6f 20 74 68 65 0a 20 20 20   size to the.   
1c6a1 20 2a 2a 20 64 65 66 61 75 6c 74 20 76 61 6c 75   ** default valu
1c6a2 65 2e 20 45 78 63 65 70 74 2c 20 77 68 65 6e 20  e. Except, when 
1c6a3 6f 70 65 6e 69 6e 67 20 6f 6e 20 61 6e 20 65 78  opening on an ex
1c6a4 69 73 74 69 6e 67 20 73 68 61 72 65 64 20 70 61  isting shared pa
1c6a5 67 65 72 2d 63 61 63 68 65 2c 0a 20 20 20 20 2a  ger-cache,.    *
1c6a6 2a 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20  * do not change 
1c6a7 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20  the pager-cache 
1c6a8 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  size..    */.   
1c6a9 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65   if( sqlite3Btre
1c6aa 65 53 63 68 65 6d 61 28 70 2c 20 30 2c 20 30 29  eSchema(p, 0, 0)
1c6ab 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
1c6ac 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68  ite3PagerSetCach
1c6ad 65 73 69 7a 65 28 70 2d 3e 70 42 74 2d 3e 70 50  esize(p->pBt->pP
1c6ae 61 67 65 72 2c 20 53 51 4c 49 54 45 5f 44 45 46  ager, SQLITE_DEF
1c6af 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 29  AULT_CACHE_SIZE)
1c6b0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
1c6b1 28 20 6d 75 74 65 78 4f 70 65 6e 20 29 7b 0a 20  ( mutexOpen ){. 
1c6b2 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
1c6b3 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 75  e3_mutex_held(mu
1c6b4 74 65 78 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20  texOpen) );.    
1c6b5 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1c6b6 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a  ave(mutexOpen);.
1c6b7 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1c6b8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d  .}../*.** Decrem
1c6b9 65 6e 74 20 74 68 65 20 42 74 53 68 61 72 65 64  ent the BtShared
1c6ba 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e 20 20  .nRef counter.  
1c6bb 57 68 65 6e 20 69 74 20 72 65 61 63 68 65 73 20  When it reaches 
1c6bc 7a 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65 20  zero,.** remove 
1c6bd 74 68 65 20 42 74 53 68 61 72 65 64 20 73 74 72  the BtShared str
1c6be 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68 65 20  ucture from the 
1c6bf 73 68 61 72 69 6e 67 20 6c 69 73 74 2e 20 20 52  sharing list.  R
1c6c0 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66  eturn.** true if
1c6c1 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e 52   the BtShared.nR
1c6c2 65 66 20 63 6f 75 6e 74 65 72 20 72 65 61 63 68  ef counter reach
1c6c3 65 73 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75  es zero and retu
1c6c4 72 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69  rn.** false if i
1c6c5 74 20 69 73 20 73 74 69 6c 6c 20 70 6f 73 69 74  t is still posit
1c6c6 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ive..*/.static i
1c6c7 6e 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61  nt removeFromSha
1c6c8 72 69 6e 67 4c 69 73 74 28 42 74 53 68 61 72 65  ringList(BtShare
1c6c9 64 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65 66  d *pBt){.#ifndef
1c6ca 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
1c6cb 52 45 44 5f 43 41 43 48 45 0a 20 20 4d 55 54 45  RED_CACHE.  MUTE
1c6cc 58 5f 4c 4f 47 49 43 28 20 73 71 6c 69 74 65 33  X_LOGIC( sqlite3
1c6cd 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3b  _mutex *pMaster;
1c6ce 20 29 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70   ).  BtShared *p
1c6cf 4c 69 73 74 3b 0a 20 20 69 6e 74 20 72 65 6d 6f  List;.  int remo
1c6d0 76 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65  ved = 0;..  asse
1c6d1 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1c6d2 78 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d 3e 6d  x_notheld(pBt->m
1c6d3 75 74 65 78 29 20 29 3b 0a 20 20 4d 55 54 45 58  utex) );.  MUTEX
1c6d4 5f 4c 4f 47 49 43 28 20 70 4d 61 73 74 65 72 20  _LOGIC( pMaster 
1c6d5 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  = sqlite3MutexAl
1c6d6 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
1c6d7 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b  _STATIC_MASTER);
1c6d8 20 29 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   ).  sqlite3_mut
1c6d9 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72  ex_enter(pMaster
1c6da 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d 2d  );.  pBt->nRef--
1c6db 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 52 65  ;.  if( pBt->nRe
1c6dc 66 3c 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  f<=0 ){.    if( 
1c6dd 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a  GLOBAL(BtShared*
1c6de 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61  ,sqlite3SharedCa
1c6df 63 68 65 4c 69 73 74 29 3d 3d 70 42 74 20 29 7b  cheList)==pBt ){
1c6e0 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 74  .      GLOBAL(Bt
1c6e1 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53  Shared*,sqlite3S
1c6e2 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 20  haredCacheList) 
1c6e3 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20  = pBt->pNext;.  
1c6e4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
1c6e5 4c 69 73 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74  List = GLOBAL(Bt
1c6e6 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53  Shared*,sqlite3S
1c6e7 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b  haredCacheList);
1c6e8 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 41 4c  .      while( AL
1c6e9 57 41 59 53 28 70 4c 69 73 74 29 20 26 26 20 70  WAYS(pList) && p
1c6ea 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d 70 42 74  List->pNext!=pBt
1c6eb 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73   ){.        pLis
1c6ec 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a  t=pList->pNext;.
1c6ed 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
1c6ee 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20  ( ALWAYS(pList) 
1c6ef 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74  ){.        pList
1c6f0 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70  ->pNext = pBt->p
1c6f1 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Next;.      }.  
1c6f2 20 20 7d 0a 20 20 20 20 69 66 28 20 53 51 4c 49    }.    if( SQLI
1c6f3 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 29 7b  TE_THREADSAFE ){
1c6f4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
1c6f5 75 74 65 78 5f 66 72 65 65 28 70 42 74 2d 3e 6d  utex_free(pBt->m
1c6f6 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20  utex);.    }.   
1c6f7 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b 0a 20 20   removed = 1;.  
1c6f8 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
1c6f9 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29  x_leave(pMaster)
1c6fa 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 6d 6f 76  ;.  return remov
1c6fb 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75  ed;.#else.  retu
1c6fc 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  rn 1;.#endif.}..
1c6fd 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
1c6fe 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 70  pBt->pTmpSpace p
1c6ff 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f  oints to an allo
1c700 63 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d 58  cation of .** MX
1c701 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20  _CELL_SIZE(pBt) 
1c702 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  bytes..*/.static
1c703 20 76 6f 69 64 20 61 6c 6c 6f 63 61 74 65 54 65   void allocateTe
1c704 6d 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64  mpSpace(BtShared
1c705 20 2a 70 42 74 29 7b 0a 20 20 69 66 28 20 21 70   *pBt){.  if( !p
1c706 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 7b  Bt->pTmpSpace ){
1c707 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 70  .    pBt->pTmpSp
1c708 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67  ace = sqlite3Pag
1c709 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61  eMalloc( pBt->pa
1c70a 67 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 7d 0a  geSize );.  }.}.
1c70b 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20  ./*.** Free the 
1c70c 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 61  pBt->pTmpSpace a
1c70d 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61  llocation.*/.sta
1c70e 74 69 63 20 76 6f 69 64 20 66 72 65 65 54 65 6d  tic void freeTem
1c70f 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 20  pSpace(BtShared 
1c710 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  *pBt){.  sqlite3
1c711 50 61 67 65 46 72 65 65 28 20 70 42 74 2d 3e 70  PageFree( pBt->p
1c712 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 70 42 74  TmpSpace);.  pBt
1c713 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 30 3b  ->pTmpSpace = 0;
1c714 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
1c715 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  an open database
1c716 20 61 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20   and invalidate 
1c717 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a  all cursors..*/.
1c718 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1c719 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
1c71a 6c 6f 73 65 28 42 74 72 65 65 20 2a 70 29 7b 0a  lose(Btree *p){.
1c71b 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
1c71c 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75  = p->pBt;.  BtCu
1c71d 72 73 6f 72 20 2a 70 43 75 72 3b 0a 0a 20 20 2f  rsor *pCur;..  /
1c71e 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73  * Close all curs
1c71f 6f 72 73 20 6f 70 65 6e 65 64 20 76 69 61 20 74  ors opened via t
1c720 68 69 73 20 68 61 6e 64 6c 65 2e 20 20 2a 2f 0a  his handle.  */.
1c721 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1c722 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
1c723 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1c724 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1c725 72 28 70 29 3b 0a 20 20 70 43 75 72 20 3d 20 70  r(p);.  pCur = p
1c726 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77  Bt->pCursor;.  w
1c727 68 69 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20  hile( pCur ){.  
1c728 20 20 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70    BtCursor *pTmp
1c729 20 3d 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75   = pCur;.    pCu
1c72a 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b  r = pCur->pNext;
1c72b 0a 20 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70  .    if( pTmp->p
1c72c 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20  Btree==p ){.    
1c72d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c    sqlite3BtreeCl
1c72e 6f 73 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b  oseCursor(pTmp);
1c72f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1c730 20 52 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63   Rollback any ac
1c731 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  tive transaction
1c732 20 61 6e 64 20 66 72 65 65 20 74 68 65 20 68 61   and free the ha
1c733 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a  ndle structure..
1c734 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f    ** The call to
1c735 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c   sqlite3BtreeRol
1c736 6c 62 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e  lback() drops an
1c737 79 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65  y table-locks he
1c738 6c 64 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20  ld by.  ** this 
1c739 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73  handle..  */.  s
1c73a 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62  qlite3BtreeRollb
1c73b 61 63 6b 28 70 2c 20 53 51 4c 49 54 45 5f 4f 4b  ack(p, SQLITE_OK
1c73c 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
1c73d 65 4c 65 61 76 65 28 70 29 3b 0a 0a 20 20 2f 2a  eLeave(p);..  /*
1c73e 20 49 66 20 74 68 65 72 65 20 61 72 65 20 73 74   If there are st
1c73f 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74 73 74 61  ill other outsta
1c740 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73  nding references
1c741 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62   to the shared-b
1c742 74 72 65 65 0a 20 20 2a 2a 20 73 74 72 75 63 74  tree.  ** struct
1c743 75 72 65 2c 20 72 65 74 75 72 6e 20 6e 6f 77 2e  ure, return now.
1c744 20 54 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f   The remainder o
1c745 66 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65  f this procedure
1c746 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a 20 75 70   cleans .  ** up
1c747 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65   the shared-btre
1c748 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  e..  */.  assert
1c749 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d  ( p->wantToLock=
1c74a 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 3d  =0 && p->locked=
1c74b 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 70 2d 3e  =0 );.  if( !p->
1c74c 73 68 61 72 61 62 6c 65 20 7c 7c 20 72 65 6d 6f  sharable || remo
1c74d 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73  veFromSharingLis
1c74e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 2f 2a  t(pBt) ){.    /*
1c74f 20 54 68 65 20 70 42 74 20 69 73 20 6e 6f 20 6c   The pBt is no l
1c750 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20 73 68 61  onger on the sha
1c751 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f 20 77 65  ring list, so we
1c752 20 63 61 6e 20 61 63 63 65 73 73 0a 20 20 20 20   can access.    
1c753 2a 2a 20 69 74 20 77 69 74 68 6f 75 74 20 68 61  ** it without ha
1c754 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20 74 68 65  ving to hold the
1c755 20 6d 75 74 65 78 2e 0a 20 20 20 20 2a 2a 0a 20   mutex..    **. 
1c756 20 20 20 2a 2a 20 43 6c 65 61 6e 20 6f 75 74 20     ** Clean out 
1c757 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 42  and delete the B
1c758 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a  tShared object..
1c759 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
1c75a 74 28 20 21 70 42 74 2d 3e 70 43 75 72 73 6f 72  t( !pBt->pCursor
1c75b 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50   );.    sqlite3P
1c75c 61 67 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70  agerClose(pBt->p
1c75d 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20  Pager);.    if( 
1c75e 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61  pBt->xFreeSchema
1c75f 20 26 26 20 70 42 74 2d 3e 70 53 63 68 65 6d 61   && pBt->pSchema
1c760 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 78   ){.      pBt->x
1c761 46 72 65 65 53 63 68 65 6d 61 28 70 42 74 2d 3e  FreeSchema(pBt->
1c762 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a  pSchema);.    }.
1c763 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1c764 65 28 30 2c 20 70 42 74 2d 3e 70 53 63 68 65 6d  e(0, pBt->pSchem
1c765 61 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70  a);.    freeTemp
1c766 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20  Space(pBt);.    
1c767 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74  sqlite3_free(pBt
1c768 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  );.  }..#ifndef 
1c769 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
1c76a 45 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65 72  ED_CACHE.  asser
1c76b 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  t( p->wantToLock
1c76c 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
1c76d 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b   p->locked==0 );
1c76e 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20  .  if( p->pPrev 
1c76f 29 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78  ) p->pPrev->pNex
1c770 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20  t = p->pNext;.  
1c771 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 70  if( p->pNext ) p
1c772 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d  ->pNext->pPrev =
1c773 20 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 69   p->pPrev;.#endi
1c774 66 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  f..  sqlite3_fre
1c775 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  e(p);.  return S
1c776 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1c777 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c 69  ** Change the li
1c778 6d 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65  mit on the numbe
1c779 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 77  r of pages allow
1c77a 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e  ed in the cache.
1c77b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  .**.** The maxim
1c77c 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 63  um number of cac
1c77d 68 65 20 70 61 67 65 73 20 69 73 20 73 65 74 20  he pages is set 
1c77e 74 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 0a  to the absolute.
1c77f 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61  ** value of mxPa
1c780 67 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 69  ge.  If mxPage i
1c781 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20  s negative, the 
1c782 70 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70  pager will.** op
1c783 65 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e 6f  erate asynchrono
1c784 75 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 6e  usly - it will n
1c785 6f 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 73  ot stop to do fs
1c786 79 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e 73  ync()s.** to ins
1c787 75 72 65 20 64 61 74 61 20 69 73 20 77 72 69 74  ure data is writ
1c788 74 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b 20  ten to the disk 
1c789 73 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a 2a  surface before.*
1c78a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 54  * continuing.  T
1c78b 72 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 6c  ransactions stil
1c78c 6c 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 72  l work if synchr
1c78d 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a 2a  onous is off,.**
1c78e 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73   and the databas
1c78f 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 72  e cannot be corr
1c790 75 70 74 65 64 20 69 66 20 74 68 69 73 20 70 72  upted if this pr
1c791 6f 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 73  ogram.** crashes
1c792 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 6f 70  .  But if the op
1c793 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63  erating system c
1c794 72 61 73 68 65 73 20 6f 72 20 74 68 65 72 65 20  rashes or there 
1c795 69 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 20  is.** an abrupt 
1c796 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 68  power failure wh
1c797 65 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69  en synchronous i
1c798 73 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61 62  s off, the datab
1c799 61 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 20  ase.** could be 
1c79a 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e  left in an incon
1c79b 73 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 65  sistent and unre
1c79c 63 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65 2e  coverable state.
1c79d 0a 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 20  .** Synchronous 
1c79e 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74  is on by default
1c79f 20 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72   so database cor
1c7a0 72 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a  ruption is not.*
1c7a1 2a 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f 72  * normally a wor
1c7a2 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ry..*/.SQLITE_PR
1c7a3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1c7a4 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69  3BtreeSetCacheSi
1c7a5 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  ze(Btree *p, int
1c7a6 20 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53 68   mxPage){.  BtSh
1c7a7 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
1c7a8 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  Bt;.  assert( sq
1c7a9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1c7aa 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
1c7ab 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
1c7ac 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69  Enter(p);.  sqli
1c7ad 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65  te3PagerSetCache
1c7ae 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72  size(pBt->pPager
1c7af 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c  , mxPage);.  sql
1c7b0 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1c7b1 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
1c7b2 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
1c7b3 43 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 64  Change the way d
1c7b4 61 74 61 20 69 73 20 73 79 6e 63 65 64 20 74 6f  ata is synced to
1c7b5 20 64 69 73 6b 20 69 6e 20 6f 72 64 65 72 20 74   disk in order t
1c7b6 6f 20 69 6e 63 72 65 61 73 65 20 6f 72 20 64 65  o increase or de
1c7b7 63 72 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77 65  crease.** how we
1c7b8 6c 6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ll the database 
1c7b9 72 65 73 69 73 74 73 20 64 61 6d 61 67 65 20 64  resists damage d
1c7ba 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73  ue to OS crashes
1c7bb 20 61 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66 61   and power.** fa
1c7bc 69 6c 75 72 65 73 2e 20 20 4c 65 76 65 6c 20 31  ilures.  Level 1
1c7bd 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
1c7be 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e 6f  asynchronous (no
1c7bf 20 73 79 6e 63 73 28 29 20 6f 63 63 75 72 20 61   syncs() occur a
1c7c0 6e 64 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61  nd.** there is a
1c7c1 20 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74   high probabilit
1c7c2 79 20 6f 66 20 64 61 6d 61 67 65 29 20 20 4c 65  y of damage)  Le
1c7c3 76 65 6c 20 32 20 69 73 20 74 68 65 20 64 65 66  vel 2 is the def
1c7c4 61 75 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a 20  ault.  There.** 
1c7c5 69 73 20 61 20 76 65 72 79 20 6c 6f 77 20 62 75  is a very low bu
1c7c6 74 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61  t non-zero proba
1c7c7 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65  bility of damage
1c7c8 2e 20 20 4c 65 76 65 6c 20 33 20 72 65 64 75 63  .  Level 3 reduc
1c7c9 65 73 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61 62  es the.** probab
1c7ca 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 20  ility of damage 
1c7cb 74 6f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75 74  to near zero but
1c7cc 20 77 69 74 68 20 61 20 77 72 69 74 65 20 70 65   with a write pe
1c7cd 72 66 6f 72 6d 61 6e 63 65 20 72 65 64 75 63 74  rformance reduct
1c7ce 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ion..*/.#ifndef 
1c7cf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45  SQLITE_OMIT_PAGE
1c7d0 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45  R_PRAGMAS.SQLITE
1c7d1 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1c7d2 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65  ite3BtreeSetSafe
1c7d3 74 79 4c 65 76 65 6c 28 0a 20 20 42 74 72 65 65  tyLevel(.  Btree
1c7d4 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
1c7d5 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 74    /* The btree t
1c7d6 6f 20 73 65 74 20 74 68 65 20 73 61 66 65 74 79  o set the safety
1c7d7 20 6c 65 76 65 6c 20 6f 6e 20 2a 2f 0a 20 20 69   level on */.  i
1c7d8 6e 74 20 6c 65 76 65 6c 2c 20 20 20 20 20 20 20  nt level,       
1c7d9 20 20 20 20 20 20 2f 2a 20 50 52 41 47 4d 41 20        /* PRAGMA 
1c7da 73 79 6e 63 68 72 6f 6e 6f 75 73 2e 20 20 31 3d  synchronous.  1=
1c7db 4f 46 46 2c 20 32 3d 4e 4f 52 4d 41 4c 2c 20 33  OFF, 2=NORMAL, 3
1c7dc 3d 46 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 66  =FULL */.  int f
1c7dd 75 6c 6c 53 79 6e 63 2c 20 20 20 20 20 20 20 20  ullSync,        
1c7de 20 20 2f 2a 20 50 52 41 47 4d 41 20 66 75 6c 6c    /* PRAGMA full
1c7df 66 73 79 6e 63 2e 20 2a 2f 0a 20 20 69 6e 74 20  fsync. */.  int 
1c7e0 63 6b 70 74 46 75 6c 6c 53 79 6e 63 20 20 20 20  ckptFullSync    
1c7e1 20 20 20 2f 2a 20 50 52 41 47 4d 41 20 63 68 65     /* PRAGMA che
1c7e2 63 6b 70 6f 69 6e 74 5f 66 75 6c 6c 66 79 6e 63  ckpoint_fullfync
1c7e3 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65   */.){.  BtShare
1c7e4 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
1c7e5 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1c7e6 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
1c7e7 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
1c7e8 20 61 73 73 65 72 74 28 20 6c 65 76 65 6c 3e 3d   assert( level>=
1c7e9 31 20 26 26 20 6c 65 76 65 6c 3c 3d 33 20 29 3b  1 && level<=3 );
1c7ea 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
1c7eb 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74  nter(p);.  sqlit
1c7ec 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79  e3PagerSetSafety
1c7ed 4c 65 76 65 6c 28 70 42 74 2d 3e 70 50 61 67 65  Level(pBt->pPage
1c7ee 72 2c 20 6c 65 76 65 6c 2c 20 66 75 6c 6c 53 79  r, level, fullSy
1c7ef 6e 63 2c 20 63 6b 70 74 46 75 6c 6c 53 79 6e 63  nc, ckptFullSync
1c7f0 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
1c7f1 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
1c7f2 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1c7f3 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
1c7f4 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
1c7f5 65 20 67 69 76 65 6e 20 62 74 72 65 65 20 69 73  e given btree is
1c7f6 20 73 65 74 20 74 6f 20 73 61 66 65 74 79 20 6c   set to safety l
1c7f7 65 76 65 6c 20 31 2e 20 20 49 6e 20 6f 74 68 65  evel 1.  In othe
1c7f8 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 72 65 74 75  r.** words, retu
1c7f9 72 6e 20 54 52 55 45 20 69 66 20 6e 6f 20 73 79  rn TRUE if no sy
1c7fa 6e 63 28 29 20 6f 63 63 75 72 73 20 6f 6e 20 74  nc() occurs on t
1c7fb 68 65 20 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a  he disk files..*
1c7fc 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1c7fd 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1c7fe 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 42 74  eSyncDisabled(Bt
1c7ff 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61  ree *p){.  BtSha
1c800 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
1c801 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61  t;.  int rc;.  a
1c802 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1c803 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d  utex_held(p->db-
1c804 3e 6d 75 74 65 78 29 20 29 3b 20 20 0a 20 20 73  >mutex) );  .  s
1c805 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1c806 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  (p);.  assert( p
1c807 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65  Bt && pBt->pPage
1c808 72 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  r );.  rc = sqli
1c809 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 70  te3PagerNosync(p
1c80a 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 73  Bt->pPager);.  s
1c80b 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1c80c 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
1c80d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
1c80e 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 61  e the default pa
1c80f 67 65 73 20 73 69 7a 65 20 61 6e 64 20 74 68 65  ges size and the
1c810 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72   number of reser
1c811 76 65 64 20 62 79 74 65 73 20 70 65 72 20 70 61  ved bytes per pa
1c812 67 65 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 74 68  ge..** Or, if th
1c813 65 20 70 61 67 65 20 73 69 7a 65 20 68 61 73 20  e page size has 
1c814 61 6c 72 65 61 64 79 20 62 65 65 6e 20 66 69 78  already been fix
1c815 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ed, return SQLIT
1c816 45 5f 52 45 41 44 4f 4e 4c 59 20 0a 2a 2a 20 77  E_READONLY .** w
1c817 69 74 68 6f 75 74 20 63 68 61 6e 67 69 6e 67 20  ithout changing 
1c818 61 6e 79 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  anything..**.** 
1c819 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 6d 75  The page size mu
1c81a 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66  st be a power of
1c81b 20 32 20 62 65 74 77 65 65 6e 20 35 31 32 20 61   2 between 512 a
1c81c 6e 64 20 36 35 35 33 36 2e 20 20 49 66 20 74 68  nd 65536.  If th
1c81d 65 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 73  e page.** size s
1c81e 75 70 70 6c 69 65 64 20 64 6f 65 73 20 6e 6f 74  upplied does not
1c81f 20 6d 65 65 74 20 74 68 69 73 20 63 6f 6e 73 74   meet this const
1c820 72 61 69 6e 74 20 74 68 65 6e 20 74 68 65 20 70  raint then the p
1c821 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 0a  age size is not.
1c822 2a 2a 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a  ** changed..**.*
1c823 2a 20 50 61 67 65 20 73 69 7a 65 73 20 61 72 65  * Page sizes are
1c824 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 74 6f 20   constrained to 
1c825 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77  be a power of tw
1c826 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 72 65  o so that the re
1c827 67 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 64  gion.** of the d
1c828 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65  atabase file use
1c829 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 28 62  d for locking (b
1c82a 65 67 69 6e 6e 69 6e 67 20 61 74 20 50 45 4e 44  eginning at PEND
1c82b 49 4e 47 5f 42 59 54 45 2c 0a 2a 2a 20 74 68 65  ING_BYTE,.** the
1c82c 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73 74   first byte past
1c82d 20 74 68 65 20 31 47 42 20 62 6f 75 6e 64 61 72   the 1GB boundar
1c82e 79 2c 20 30 78 34 30 30 30 30 30 30 30 29 20 6e  y, 0x40000000) n
1c82f 65 65 64 73 20 74 6f 20 6f 63 63 75 72 0a 2a 2a  eeds to occur.**
1c830 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
1c831 67 20 6f 66 20 61 20 70 61 67 65 2e 0a 2a 2a 0a  g of a page..**.
1c832 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20  ** If parameter 
1c833 6e 52 65 73 65 72 76 65 20 69 73 20 6c 65 73 73  nReserve is less
1c834 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e   than zero, then
1c835 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
1c836 65 73 65 72 76 65 64 0a 2a 2a 20 62 79 74 65 73  eserved.** bytes
1c837 20 70 65 72 20 70 61 67 65 20 69 73 20 6c 65 66   per page is lef
1c838 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a  t unchanged..**.
1c839 2a 2a 20 49 66 20 74 68 65 20 69 46 69 78 21 3d  ** If the iFix!=
1c83a 30 20 74 68 65 6e 20 74 68 65 20 42 54 53 5f 50  0 then the BTS_P
1c83b 41 47 45 53 49 5a 45 5f 46 49 58 45 44 20 66 6c  AGESIZE_FIXED fl
1c83c 61 67 20 69 73 20 73 65 74 20 73 6f 20 74 68 61  ag is set so tha
1c83d 74 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 0a  t the page size.
1c83e 2a 2a 20 61 6e 64 20 61 75 74 6f 76 61 63 75 75  ** and autovacuu
1c83f 6d 20 6d 6f 64 65 20 63 61 6e 20 6e 6f 20 6c 6f  m mode can no lo
1c840 6e 67 65 72 20 62 65 20 63 68 61 6e 67 65 64 2e  nger be changed.
1c841 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1c842 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1c843 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42  reeSetPageSize(B
1c844 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 70 61 67  tree *p, int pag
1c845 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65  eSize, int nRese
1c846 72 76 65 2c 20 69 6e 74 20 69 46 69 78 29 7b 0a  rve, int iFix){.
1c847 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1c848 45 5f 4f 4b 3b 0a 20 20 42 74 53 68 61 72 65 64  E_OK;.  BtShared
1c849 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
1c84a 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 72    assert( nReser
1c84b 76 65 3e 3d 2d 31 20 26 26 20 6e 52 65 73 65 72  ve>=-1 && nReser
1c84c 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 73 71 6c  ve<=255 );.  sql
1c84d 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
1c84e 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 62 74  );.  if( pBt->bt
1c84f 73 46 6c 61 67 73 20 26 20 42 54 53 5f 50 41 47  sFlags & BTS_PAG
1c850 45 53 49 5a 45 5f 46 49 58 45 44 20 29 7b 0a 20  ESIZE_FIXED ){. 
1c851 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
1c852 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74  eave(p);.    ret
1c853 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  urn SQLITE_READO
1c854 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  NLY;.  }.  if( n
1c855 52 65 73 65 72 76 65 3c 30 20 29 7b 0a 20 20 20  Reserve<0 ){.   
1c856 20 6e 52 65 73 65 72 76 65 20 3d 20 70 42 74 2d   nReserve = pBt-
1c857 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d  >pageSize - pBt-
1c858 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 7d  >usableSize;.  }
1c859 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65  .  assert( nRese
1c85a 72 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 72  rve>=0 && nReser
1c85b 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 69 66 28  ve<=255 );.  if(
1c85c 20 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26   pageSize>=512 &
1c85d 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49  & pageSize<=SQLI
1c85e 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
1c85f 20 26 26 0a 20 20 20 20 20 20 20 20 28 28 70 61   &&.        ((pa
1c860 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69  geSize-1)&pageSi
1c861 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73  ze)==0 ){.    as
1c862 73 65 72 74 28 20 28 70 61 67 65 53 69 7a 65 20  sert( (pageSize 
1c863 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61  & 7)==0 );.    a
1c864 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 50 61  ssert( !pBt->pPa
1c865 67 65 31 20 26 26 20 21 70 42 74 2d 3e 70 43 75  ge1 && !pBt->pCu
1c866 72 73 6f 72 20 29 3b 0a 20 20 20 20 70 42 74 2d  rsor );.    pBt-
1c867 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 33 32  >pageSize = (u32
1c868 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 66  )pageSize;.    f
1c869 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42 74  reeTempSpace(pBt
1c86a 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71  );.  }.  rc = sq
1c86b 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67  lite3PagerSetPag
1c86c 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65  esize(pBt->pPage
1c86d 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a  r, &pBt->pageSiz
1c86e 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20  e, nReserve);.  
1c86f 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
1c870 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  = pBt->pageSize 
1c871 2d 20 28 75 31 36 29 6e 52 65 73 65 72 76 65 3b  - (u16)nReserve;
1c872 0a 20 20 69 66 28 20 69 46 69 78 20 29 20 70 42  .  if( iFix ) pB
1c873 74 2d 3e 62 74 73 46 6c 61 67 73 20 7c 3d 20 42  t->btsFlags |= B
1c874 54 53 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 45  TS_PAGESIZE_FIXE
1c875 44 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  D;.  sqlite3Btre
1c876 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
1c877 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1c878 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72   Return the curr
1c879 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61  ently defined pa
1c87a 67 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54  ge size.*/.SQLIT
1c87b 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1c87c 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
1c87d 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b  eSize(Btree *p){
1c87e 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74  .  return p->pBt
1c87f 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a 23  ->pageSize;.}..#
1c880 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
1c881 45 5f 48 41 53 5f 43 4f 44 45 43 29 20 7c 7c 20  E_HAS_CODEC) || 
1c882 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
1c883 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73  EBUG)./*.** This
1c884 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 69 6d   function is sim
1c885 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33 42  ilar to sqlite3B
1c886 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28 29  treeGetReserve()
1c887 2c 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74  , except that it
1c888 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20  .** may only be 
1c889 63 61 6c 6c 65 64 20 69 66 20 69 74 20 69 73 20  called if it is 
1c88a 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20  guaranteed that 
1c88b 74 68 65 20 62 2d 74 72 65 65 20 6d 75 74 65 78  the b-tree mutex
1c88c 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 68   is already.** h
1c88d 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  eld..**.** This 
1c88e 69 73 20 75 73 65 66 75 6c 20 69 6e 20 6f 6e 65  is useful in one
1c88f 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 69 6e   special case in
1c890 20 74 68 65 20 62 61 63 6b 75 70 20 41 50 49 20   the backup API 
1c891 63 6f 64 65 20 77 68 65 72 65 20 69 74 20 69 73  code where it is
1c892 0a 2a 2a 20 6b 6e 6f 77 6e 20 74 68 61 74 20 74  .** known that t
1c893 68 65 20 73 68 61 72 65 64 20 62 2d 74 72 65 65  he shared b-tree
1c894 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 2c 20   mutex is held, 
1c895 62 75 74 20 74 68 65 20 6d 75 74 65 78 20 6f 6e  but the mutex on
1c896 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73   the .** databas
1c897 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 6f 77  e handle that ow
1c898 6e 73 20 2a 70 20 69 73 20 6e 6f 74 2e 20 49 6e  ns *p is not. In
1c899 20 74 68 69 73 20 63 61 73 65 20 69 66 20 73 71   this case if sq
1c89a 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
1c89b 29 0a 2a 2a 20 77 65 72 65 20 74 6f 20 62 65 20  ).** were to be 
1c89c 63 61 6c 6c 65 64 2c 20 69 74 20 6d 69 67 68 74  called, it might
1c89d 20 63 6f 6c 6c 69 64 65 20 77 69 74 68 20 73 6f   collide with so
1c89e 6d 65 20 6f 74 68 65 72 20 6f 70 65 72 61 74 69  me other operati
1c89f 6f 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 74  on on the.** dat
1c8a0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61  abase handle tha
1c8a1 74 20 6f 77 6e 73 20 2a 70 2c 20 63 61 75 73 69  t owns *p, causi
1c8a2 6e 67 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68  ng undefined beh
1c8a3 61 76 69 6f 75 72 2e 0a 2a 2f 0a 53 51 4c 49 54  aviour..*/.SQLIT
1c8a4 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1c8a5 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 65 73  lite3BtreeGetRes
1c8a6 65 72 76 65 4e 6f 4d 75 74 65 78 28 42 74 72 65  erveNoMutex(Btre
1c8a7 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  e *p){.  assert(
1c8a8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1c8a9 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65  eld(p->pBt->mute
1c8aa 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  x) );.  return p
1c8ab 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  ->pBt->pageSize 
1c8ac 2d 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  - p->pBt->usable
1c8ad 53 69 7a 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  Size;.}.#endif /
1c8ae 2a 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  * SQLITE_HAS_COD
1c8af 45 43 20 7c 7c 20 53 51 4c 49 54 45 5f 44 45 42  EC || SQLITE_DEB
1c8b0 55 47 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69  UG */..#if !defi
1c8b1 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
1c8b2 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c  PAGER_PRAGMAS) |
1c8b3 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  | !defined(SQLIT
1c8b4 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 0a 2f  E_OMIT_VACUUM)./
1c8b5 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1c8b6 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
1c8b7 6f 66 20 73 70 61 63 65 20 61 74 20 74 68 65 20  of space at the 
1c8b8 65 6e 64 20 6f 66 20 65 76 65 72 79 20 70 61 67  end of every pag
1c8b9 65 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 69 6e  e that.** are in
1c8ba 74 65 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20 75  tentually left u
1c8bb 6e 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 20  nused.  This is 
1c8bc 74 68 65 20 22 72 65 73 65 72 76 65 64 22 20 73  the "reserved" s
1c8bd 70 61 63 65 20 74 68 61 74 20 69 73 0a 2a 2a 20  pace that is.** 
1c8be 73 6f 6d 65 74 69 6d 65 73 20 75 73 65 64 20 62  sometimes used b
1c8bf 79 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f  y extensions..*/
1c8c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1c8c1 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
1c8c2 47 65 74 52 65 73 65 72 76 65 28 42 74 72 65 65  GetReserve(Btree
1c8c3 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20   *p){.  int n;. 
1c8c4 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
1c8c5 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e  er(p);.  n = p->
1c8c6 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20  pBt->pageSize - 
1c8c7 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  p->pBt->usableSi
1c8c8 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  ze;.  sqlite3Btr
1c8c9 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
1c8ca 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn n;.}../*.**
1c8cb 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d   Set the maximum
1c8cc 20 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 20   page count for 
1c8cd 61 20 64 61 74 61 62 61 73 65 20 69 66 20 6d 78  a database if mx
1c8ce 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65  Page is positive
1c8cf 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 20  ..** No changes 
1c8d0 61 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50 61  are made if mxPa
1c8d1 67 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61 74  ge is 0 or negat
1c8d2 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65  ive..** Regardle
1c8d3 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20  ss of the value 
1c8d4 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72  of mxPage, retur
1c8d5 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61  n the maximum pa
1c8d6 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c  ge count..*/.SQL
1c8d7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1c8d8 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50  sqlite3BtreeMaxP
1c8d9 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20 2a  ageCount(Btree *
1c8da 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a  p, int mxPage){.
1c8db 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74    int n;.  sqlit
1c8dc 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
1c8dd 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61  .  n = sqlite3Pa
1c8de 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28  gerMaxPageCount(
1c8df 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  p->pBt->pPager, 
1c8e0 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74  mxPage);.  sqlit
1c8e1 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
1c8e2 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
1c8e3 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 42 54  /*.** Set the BT
1c8e4 53 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20  S_SECURE_DELETE 
1c8e5 66 6c 61 67 20 69 66 20 6e 65 77 46 6c 61 67 20  flag if newFlag 
1c8e6 69 73 20 30 20 6f 72 20 31 2e 20 20 49 66 20 6e  is 0 or 1.  If n
1c8e7 65 77 46 6c 61 67 20 69 73 20 2d 31 2c 0a 2a 2a  ewFlag is -1,.**
1c8e8 20 74 68 65 6e 20 6d 61 6b 65 20 6e 6f 20 63 68   then make no ch
1c8e9 61 6e 67 65 73 2e 20 20 41 6c 77 61 79 73 20 72  anges.  Always r
1c8ea 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20  eturn the value 
1c8eb 6f 66 20 74 68 65 20 42 54 53 5f 53 45 43 55 52  of the BTS_SECUR
1c8ec 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 73 65 74 74  E_DELETE.** sett
1c8ed 69 6e 67 20 61 66 74 65 72 20 74 68 65 20 63 68  ing after the ch
1c8ee 61 6e 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ange..*/.SQLITE_
1c8ef 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1c8f0 74 65 33 42 74 72 65 65 53 65 63 75 72 65 44 65  te3BtreeSecureDe
1c8f1 6c 65 74 65 28 42 74 72 65 65 20 2a 70 2c 20 69  lete(Btree *p, i
1c8f2 6e 74 20 6e 65 77 46 6c 61 67 29 7b 0a 20 20 69  nt newFlag){.  i
1c8f3 6e 74 20 62 3b 0a 20 20 69 66 28 20 70 3d 3d 30  nt b;.  if( p==0
1c8f4 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 73   ) return 0;.  s
1c8f5 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1c8f6 28 70 29 3b 0a 20 20 69 66 28 20 6e 65 77 46 6c  (p);.  if( newFl
1c8f7 61 67 3e 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e  ag>=0 ){.    p->
1c8f8 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 3d  pBt->btsFlags &=
1c8f9 20 7e 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c   ~BTS_SECURE_DEL
1c8fa 45 54 45 3b 0a 20 20 20 20 69 66 28 20 6e 65 77  ETE;.    if( new
1c8fb 46 6c 61 67 20 29 20 70 2d 3e 70 42 74 2d 3e 62  Flag ) p->pBt->b
1c8fc 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 53  tsFlags |= BTS_S
1c8fd 45 43 55 52 45 5f 44 45 4c 45 54 45 3b 0a 20 20  ECURE_DELETE;.  
1c8fe 7d 20 0a 20 20 62 20 3d 20 28 70 2d 3e 70 42 74  } .  b = (p->pBt
1c8ff 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53  ->btsFlags & BTS
1c900 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 29 21  _SECURE_DELETE)!
1c901 3d 30 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  =0;.  sqlite3Btr
1c902 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
1c903 74 75 72 6e 20 62 3b 0a 7d 0a 23 65 6e 64 69 66  turn b;.}.#endif
1c904 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
1c905 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
1c906 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69  RAGMAS) || !defi
1c907 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
1c908 56 41 43 55 55 4d 29 20 2a 2f 0a 0a 2f 2a 0a 2a  VACUUM) */../*.*
1c909 2a 20 43 68 61 6e 67 65 20 74 68 65 20 27 61 75  * Change the 'au
1c90a 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65  to-vacuum' prope
1c90b 72 74 79 20 6f 66 20 74 68 65 20 64 61 74 61 62  rty of the datab
1c90c 61 73 65 2e 20 49 66 20 74 68 65 20 27 61 75 74  ase. If the 'aut
1c90d 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 70 61 72 61  oVacuum'.** para
1c90e 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72  meter is non-zer
1c90f 6f 2c 20 74 68 65 6e 20 61 75 74 6f 2d 76 61 63  o, then auto-vac
1c910 75 75 6d 20 6d 6f 64 65 20 69 73 20 65 6e 61 62  uum mode is enab
1c911 6c 65 64 2e 20 49 66 20 7a 65 72 6f 2c 20 69 74  led. If zero, it
1c912 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64 2e  .** is disabled.
1c913 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c   The default val
1c914 75 65 20 66 6f 72 20 74 68 65 20 61 75 74 6f 2d  ue for the auto-
1c915 76 61 63 75 75 6d 20 70 72 6f 70 65 72 74 79 20  vacuum property 
1c916 69 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65  is .** determine
1c917 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f  d by the SQLITE_
1c918 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55  DEFAULT_AUTOVACU
1c919 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 53 51 4c  UM macro..*/.SQL
1c91a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1c91b 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41  sqlite3BtreeSetA
1c91c 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20  utoVacuum(Btree 
1c91d 2a 70 2c 20 69 6e 74 20 61 75 74 6f 56 61 63 75  *p, int autoVacu
1c91e 75 6d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  um){.#ifdef SQLI
1c91f 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
1c920 55 4d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  UM.  return SQLI
1c921 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 65 6c  TE_READONLY;.#el
1c922 73 65 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  se.  BtShared *p
1c923 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69  Bt = p->pBt;.  i
1c924 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1c925 4b 3b 0a 20 20 75 38 20 61 76 20 3d 20 28 75 38  K;.  u8 av = (u8
1c926 29 61 75 74 6f 56 61 63 75 75 6d 3b 0a 0a 20 20  )autoVacuum;..  
1c927 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1c928 72 28 70 29 3b 0a 20 20 69 66 28 20 28 70 42 74  r(p);.  if( (pBt
1c929 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53  ->btsFlags & BTS
1c92a 5f 50 41 47 45 53 49 5a 45 5f 46 49 58 45 44 29  _PAGESIZE_FIXED)
1c92b 21 3d 30 20 26 26 20 28 61 76 20 3f 31 3a 30 29  !=0 && (av ?1:0)
1c92c 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  !=pBt->autoVacuu
1c92d 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  m ){.    rc = SQ
1c92e 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20  LITE_READONLY;. 
1c92f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d   }else{.    pBt-
1c930 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76  >autoVacuum = av
1c931 20 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e   ?1:0;.    pBt->
1c932 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d  incrVacuum = av=
1c933 3d 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73  =2 ?1:0;.  }.  s
1c934 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1c935 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
1c936 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
1c937 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c  * Return the val
1c938 75 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d  ue of the 'auto-
1c939 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79  vacuum' property
1c93a 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d  . If auto-vacuum
1c93b 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20   is .** enabled 
1c93c 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f  1 is returned. O
1c93d 74 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 53  therwise 0..*/.S
1c93e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1c93f 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  t sqlite3BtreeGe
1c940 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65  tAutoVacuum(Btre
1c941 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51  e *p){.#ifdef SQ
1c942 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1c943 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54  CUUM.  return BT
1c944 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e  REE_AUTOVACUUM_N
1c945 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74  ONE;.#else.  int
1c946 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74   rc;.  sqlite3Bt
1c947 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72  reeEnter(p);.  r
1c948 63 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70  c = (.    (!p->p
1c949 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f  Bt->autoVacuum)?
1c94a 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d  BTREE_AUTOVACUUM
1c94b 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e  _NONE:.    (!p->
1c94c 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29  pBt->incrVacuum)
1c94d 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55  ?BTREE_AUTOVACUU
1c94e 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45  M_FULL:.    BTRE
1c94f 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43  E_AUTOVACUUM_INC
1c950 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  R.  );.  sqlite3
1c951 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
1c952 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64   return rc;.#end
1c953 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  if.}.../*.** Get
1c954 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
1c955 70 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 61  pPage1 of the da
1c956 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68  tabase file.  Th
1c957 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20  is will.** also 
1c958 61 63 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f  acquire a readlo
1c959 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e  ck on that file.
1c95a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  .**.** SQLITE_OK
1c95b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20   is returned on 
1c95c 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 65  success.  If the
1c95d 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a   file is not a.*
1c95e 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61  * well-formed da
1c95f 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65  tabase file, the
1c960 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
1c961 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
1c962 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20   SQLITE_BUSY is 
1c963 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20  returned if the 
1c964 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b  database is lock
1c965 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  ed.  SQLITE_NOME
1c966 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  M.** is returned
1c967 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f   if we run out o
1c968 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74  f memory. .*/.st
1c969 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72  atic int lockBtr
1c96a 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ee(BtShared *pBt
1c96b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
1c96c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
1c96d 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 73 75  ult code from su
1c96e 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20  bfunctions */.  
1c96f 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b  MemPage *pPage1;
1c970 20 20 20 20 20 2f 2a 20 50 61 67 65 20 31 20 6f       /* Page 1 o
1c971 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
1c972 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61  ile */.  int nPa
1c973 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ge;           /*
1c974 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
1c975 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1c976 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 46   */.  int nPageF
1c977 69 6c 65 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75  ile = 0;   /* Nu
1c978 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
1c979 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1c97a 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67  le */.  int nPag
1c97b 65 48 65 61 64 65 72 3b 20 20 20 20 20 2f 2a 20  eHeader;     /* 
1c97c 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
1c97d 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1c97e 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 68 64 72  according to hdr
1c97f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73   */..  assert( s
1c980 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1c981 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
1c982 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e  .  assert( pBt->
1c983 70 50 61 67 65 31 3d 3d 30 20 29 3b 0a 20 20 72  pPage1==0 );.  r
1c984 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1c985 53 68 61 72 65 64 4c 6f 63 6b 28 70 42 74 2d 3e  SharedLock(pBt->
1c986 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 72  pPager);.  if( r
1c987 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
1c988 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d  eturn rc;.  rc =
1c989 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42   btreeGetPage(pB
1c98a 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c 20 30  t, 1, &pPage1, 0
1c98b 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
1c98c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
1c98d 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 20 73 6f 6d  rc;..  /* Do som
1c98e 65 20 63 68 65 63 6b 69 6e 67 20 74 6f 20 68 65  e checking to he
1c98f 6c 70 20 69 6e 73 75 72 65 20 74 68 65 20 66 69  lp insure the fi
1c990 6c 65 20 77 65 20 6f 70 65 6e 65 64 20 72 65 61  le we opened rea
1c991 6c 6c 79 20 69 73 0a 20 20 2a 2a 20 61 20 76 61  lly is.  ** a va
1c992 6c 69 64 20 64 61 74 61 62 61 73 65 20 66 69 6c  lid database fil
1c993 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e 50 61 67 65  e. .  */.  nPage
1c994 20 3d 20 6e 50 61 67 65 48 65 61 64 65 72 20 3d   = nPageHeader =
1c995 20 67 65 74 34 62 79 74 65 28 32 38 2b 28 75 38   get4byte(28+(u8
1c996 2a 29 70 50 61 67 65 31 2d 3e 61 44 61 74 61 29  *)pPage1->aData)
1c997 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72  ;.  sqlite3Pager
1c998 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70  Pagecount(pBt->p
1c999 50 61 67 65 72 2c 20 26 6e 50 61 67 65 46 69 6c  Pager, &nPageFil
1c99a 65 29 3b 0a 20 20 69 66 28 20 6e 50 61 67 65 3d  e);.  if( nPage=
1c99b 3d 30 20 7c 7c 20 6d 65 6d 63 6d 70 28 32 34 2b  =0 || memcmp(24+
1c99c 28 75 38 2a 29 70 50 61 67 65 31 2d 3e 61 44 61  (u8*)pPage1->aDa
1c99d 74 61 2c 20 39 32 2b 28 75 38 2a 29 70 50 61 67  ta, 92+(u8*)pPag
1c99e 65 31 2d 3e 61 44 61 74 61 2c 34 29 21 3d 30 20  e1->aData,4)!=0 
1c99f 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 6e  ){.    nPage = n
1c9a0 50 61 67 65 46 69 6c 65 3b 0a 20 20 7d 0a 20 20  PageFile;.  }.  
1c9a1 69 66 28 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20  if( nPage>0 ){. 
1c9a2 20 20 20 75 33 32 20 70 61 67 65 53 69 7a 65 3b     u32 pageSize;
1c9a3 0a 20 20 20 20 75 33 32 20 75 73 61 62 6c 65 53  .    u32 usableS
1c9a4 69 7a 65 3b 0a 20 20 20 20 75 38 20 2a 70 61 67  ize;.    u8 *pag
1c9a5 65 31 20 3d 20 70 50 61 67 65 31 2d 3e 61 44 61  e1 = pPage1->aDa
1c9a6 74 61 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ta;.    rc = SQL
1c9a7 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 20 20  ITE_NOTADB;.    
1c9a8 69 66 28 20 6d 65 6d 63 6d 70 28 70 61 67 65 31  if( memcmp(page1
1c9a9 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20  , zMagicHeader, 
1c9aa 31 36 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  16)!=0 ){.      
1c9ab 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f  goto page1_init_
1c9ac 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a 23  failed;.    }..#
1c9ad 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
1c9ae 54 5f 57 41 4c 0a 20 20 20 20 69 66 28 20 70 61  T_WAL.    if( pa
1c9af 67 65 31 5b 31 38 5d 3e 31 20 29 7b 0a 20 20 20  ge1[18]>1 ){.   
1c9b0 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73     pBt->btsFlags
1c9b1 20 7c 3d 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c   |= BTS_READ_ONL
1c9b2 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  Y;.    }.    if(
1c9b3 20 70 61 67 65 31 5b 31 39 5d 3e 31 20 29 7b 0a   page1[19]>1 ){.
1c9b4 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31        goto page1
1c9b5 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20  _init_failed;.  
1c9b6 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 69 66    }.#else.    if
1c9b7 28 20 70 61 67 65 31 5b 31 38 5d 3e 32 20 29 7b  ( page1[18]>2 ){
1c9b8 0a 20 20 20 20 20 20 70 42 74 2d 3e 62 74 73 46  .      pBt->btsF
1c9b9 6c 61 67 73 20 7c 3d 20 42 54 53 5f 52 45 41 44  lags |= BTS_READ
1c9ba 5f 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 20  _ONLY;.    }.   
1c9bb 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3e 32   if( page1[19]>2
1c9bc 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70   ){.      goto p
1c9bd 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64  age1_init_failed
1c9be 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1c9bf 49 66 20 74 68 65 20 77 72 69 74 65 20 76 65 72  If the write ver
1c9c0 73 69 6f 6e 20 69 73 20 73 65 74 20 74 6f 20 32  sion is set to 2
1c9c1 2c 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  , this database 
1c9c2 73 68 6f 75 6c 64 20 62 65 20 61 63 63 65 73 73  should be access
1c9c3 65 64 0a 20 20 20 20 2a 2a 20 69 6e 20 57 41 4c  ed.    ** in WAL
1c9c4 20 6d 6f 64 65 2e 20 49 66 20 74 68 65 20 6c 6f   mode. If the lo
1c9c5 67 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  g is not already
1c9c6 20 6f 70 65 6e 2c 20 6f 70 65 6e 20 69 74 20 6e   open, open it n
1c9c7 6f 77 2e 20 54 68 65 6e 20 0a 20 20 20 20 2a 2a  ow. Then .    **
1c9c8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1c9c9 4b 20 61 6e 64 20 72 65 74 75 72 6e 20 77 69 74  K and return wit
1c9ca 68 6f 75 74 20 70 6f 70 75 6c 61 74 69 6e 67 20  hout populating 
1c9cb 42 74 53 68 61 72 65 64 2e 70 50 61 67 65 31 2e  BtShared.pPage1.
1c9cc 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c  .    ** The call
1c9cd 65 72 20 64 65 74 65 63 74 73 20 74 68 69 73 20  er detects this 
1c9ce 61 6e 64 20 63 61 6c 6c 73 20 74 68 69 73 20 66  and calls this f
1c9cf 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 2e 20 54  unction again. T
1c9d0 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 72 65  his is.    ** re
1c9d1 71 75 69 72 65 64 20 61 73 20 74 68 65 20 76 65  quired as the ve
1c9d2 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 20 31 20  rsion of page 1 
1c9d3 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65  currently in the
1c9d4 20 70 61 67 65 31 20 62 75 66 66 65 72 0a 20 20   page1 buffer.  
1c9d5 20 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20    ** may not be 
1c9d6 74 68 65 20 6c 61 74 65 73 74 20 76 65 72 73 69  the latest versi
1c9d7 6f 6e 20 2d 20 74 68 65 72 65 20 6d 61 79 20 62  on - there may b
1c9d8 65 20 61 20 6e 65 77 65 72 20 6f 6e 65 20 69 6e  e a newer one in
1c9d9 20 74 68 65 20 6c 6f 67 0a 20 20 20 20 2a 2a 20   the log.    ** 
1c9da 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  file..    */.   
1c9db 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3d 3d   if( page1[19]==
1c9dc 32 20 26 26 20 28 70 42 74 2d 3e 62 74 73 46 6c  2 && (pBt->btsFl
1c9dd 61 67 73 20 26 20 42 54 53 5f 4e 4f 5f 57 41 4c  ags & BTS_NO_WAL
1c9de 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e  )==0 ){.      in
1c9df 74 20 69 73 4f 70 65 6e 20 3d 20 30 3b 0a 20 20  t isOpen = 0;.  
1c9e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1c9e1 50 61 67 65 72 4f 70 65 6e 57 61 6c 28 70 42 74  PagerOpenWal(pBt
1c9e2 2d 3e 70 50 61 67 65 72 2c 20 26 69 73 4f 70 65  ->pPager, &isOpe
1c9e3 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  n);.      if( rc
1c9e4 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1c9e5 20 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65         goto page
1c9e6 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20  1_init_failed;. 
1c9e7 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69       }else if( i
1c9e8 73 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  sOpen==0 ){.    
1c9e9 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
1c9ea 70 50 61 67 65 31 29 3b 0a 20 20 20 20 20 20 20  pPage1);.       
1c9eb 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1c9ec 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  K;.      }.     
1c9ed 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 54   rc = SQLITE_NOT
1c9ee 41 44 42 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  ADB;.    }.#endi
1c9ef 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61  f..    /* The ma
1c9f0 78 69 6d 75 6d 20 65 6d 62 65 64 64 65 64 20 66  ximum embedded f
1c9f1 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  raction must be 
1c9f2 65 78 61 63 74 6c 79 20 32 35 25 2e 20 20 41 6e  exactly 25%.  An
1c9f3 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20  d the minimum.  
1c9f4 20 20 2a 2a 20 65 6d 62 65 64 64 65 64 20 66 72    ** embedded fr
1c9f5 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 31  action must be 1
1c9f6 32 2e 35 25 20 66 6f 72 20 62 6f 74 68 20 6c 65  2.5% for both le
1c9f7 61 66 2d 64 61 74 61 20 61 6e 64 20 6e 6f 6e 2d  af-data and non-
1c9f8 6c 65 61 66 2d 64 61 74 61 2e 0a 20 20 20 20 2a  leaf-data..    *
1c9f9 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 64  * The original d
1c9fa 65 73 69 67 6e 20 61 6c 6c 6f 77 65 64 20 74 68  esign allowed th
1c9fb 65 73 65 20 61 6d 6f 75 6e 74 73 20 74 6f 20 76  ese amounts to v
1c9fc 61 72 79 2c 20 62 75 74 20 61 73 20 6f 66 0a 20  ary, but as of. 
1c9fd 20 20 20 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e     ** version 3.
1c9fe 36 2e 30 2c 20 77 65 20 72 65 71 75 69 72 65 20  6.0, we require 
1c9ff 74 68 65 6d 20 74 6f 20 62 65 20 66 69 78 65 64  them to be fixed
1ca00 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1ca01 20 6d 65 6d 63 6d 70 28 26 70 61 67 65 31 5b 32   memcmp(&page1[2
1ca02 31 5d 2c 20 22 5c 31 30 30 5c 30 34 30 5c 30 34  1], "\100\040\04
1ca03 30 22 2c 33 29 21 3d 30 20 29 7b 0a 20 20 20 20  0",3)!=0 ){.    
1ca04 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69    goto page1_ini
1ca05 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a  t_failed;.    }.
1ca06 20 20 20 20 70 61 67 65 53 69 7a 65 20 3d 20 28      pageSize = (
1ca07 70 61 67 65 31 5b 31 36 5d 3c 3c 38 29 20 7c 20  page1[16]<<8) | 
1ca08 28 70 61 67 65 31 5b 31 37 5d 3c 3c 31 36 29 3b  (page1[17]<<16);
1ca09 0a 20 20 20 20 69 66 28 20 28 28 70 61 67 65 53  .    if( ((pageS
1ca0a 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29  ize-1)&pageSize)
1ca0b 21 3d 30 0a 20 20 20 20 20 7c 7c 20 70 61 67 65  !=0.     || page
1ca0c 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  Size>SQLITE_MAX_
1ca0d 50 41 47 45 5f 53 49 5a 45 20 0a 20 20 20 20 20  PAGE_SIZE .     
1ca0e 7c 7c 20 70 61 67 65 53 69 7a 65 3c 3d 32 35 36  || pageSize<=256
1ca0f 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 67   .    ){.      g
1ca10 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66  oto page1_init_f
1ca11 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20  ailed;.    }.   
1ca12 20 61 73 73 65 72 74 28 20 28 70 61 67 65 53 69   assert( (pageSi
1ca13 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20  ze & 7)==0 );.  
1ca14 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70    usableSize = p
1ca15 61 67 65 53 69 7a 65 20 2d 20 70 61 67 65 31 5b  ageSize - page1[
1ca16 32 30 5d 3b 0a 20 20 20 20 69 66 28 20 28 75 33  20];.    if( (u3
1ca17 32 29 70 61 67 65 53 69 7a 65 21 3d 70 42 74 2d  2)pageSize!=pBt-
1ca18 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20  >pageSize ){.   
1ca19 20 20 20 2f 2a 20 41 66 74 65 72 20 72 65 61 64     /* After read
1ca1a 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 61  ing the first pa
1ca1b 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ge of the databa
1ca1c 73 65 20 61 73 73 75 6d 69 6e 67 20 61 20 70 61  se assuming a pa
1ca1d 67 65 20 73 69 7a 65 0a 20 20 20 20 20 20 2a 2a  ge size.      **
1ca1e 20 6f 66 20 42 74 53 68 61 72 65 64 2e 70 61 67   of BtShared.pag
1ca1f 65 53 69 7a 65 2c 20 77 65 20 68 61 76 65 20 64  eSize, we have d
1ca20 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 20 74  iscovered that t
1ca21 68 65 20 70 61 67 65 2d 73 69 7a 65 20 69 73 0a  he page-size is.
1ca22 20 20 20 20 20 20 2a 2a 20 61 63 74 75 61 6c 6c        ** actuall
1ca23 79 20 70 61 67 65 53 69 7a 65 2e 20 55 6e 6c 6f  y pageSize. Unlo
1ca24 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  ck the database,
1ca25 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 61 67   leave pBt->pPag
1ca26 65 31 20 61 74 0a 20 20 20 20 20 20 2a 2a 20 7a  e1 at.      ** z
1ca27 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 20 53  ero and return S
1ca28 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 20 63 61  QLITE_OK. The ca
1ca29 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c 6c 20 74  ller will call t
1ca2a 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20  his function.   
1ca2b 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 69 74 68     ** again with
1ca2c 20 74 68 65 20 63 6f 72 72 65 63 74 20 70 61 67   the correct pag
1ca2d 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20 20 2a 2f  e-size..      */
1ca2e 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
1ca2f 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20 20  ge(pPage1);.    
1ca30 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a    pBt->usableSiz
1ca31 65 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a  e = usableSize;.
1ca32 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53        pBt->pageS
1ca33 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 3b 0a  ize = pageSize;.
1ca34 20 20 20 20 20 20 66 72 65 65 54 65 6d 70 53 70        freeTempSp
1ca35 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20  ace(pBt);.      
1ca36 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1ca37 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74  rSetPagesize(pBt
1ca38 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e  ->pPager, &pBt->
1ca39 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20  pageSize,.      
1ca3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ca3b 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67               pag
1ca3c 65 53 69 7a 65 2d 75 73 61 62 6c 65 53 69 7a 65  eSize-usableSize
1ca3d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1ca3e 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rc;.    }.    if
1ca3f 28 20 28 70 42 74 2d 3e 64 62 2d 3e 66 6c 61 67  ( (pBt->db->flag
1ca40 73 20 26 20 53 51 4c 49 54 45 5f 52 65 63 6f 76  s & SQLITE_Recov
1ca41 65 72 79 4d 6f 64 65 29 3d 3d 30 20 26 26 20 6e  eryMode)==0 && n
1ca42 50 61 67 65 3e 6e 50 61 67 65 46 69 6c 65 20 29  Page>nPageFile )
1ca43 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
1ca44 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1ca45 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67  ;.      goto pag
1ca46 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a  e1_init_failed;.
1ca47 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 73      }.    if( us
1ca48 61 62 6c 65 53 69 7a 65 3c 34 38 30 20 29 7b 0a  ableSize<480 ){.
1ca49 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31        goto page1
1ca4a 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20  _init_failed;.  
1ca4b 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70 61 67    }.    pBt->pag
1ca4c 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65  eSize = pageSize
1ca4d 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c  ;.    pBt->usabl
1ca4e 65 53 69 7a 65 20 3d 20 75 73 61 62 6c 65 53 69  eSize = usableSi
1ca4f 7a 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ze;.#ifndef SQLI
1ca50 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
1ca51 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f  UM.    pBt->auto
1ca52 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79  Vacuum = (get4by
1ca53 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b 20 34  te(&page1[36 + 4
1ca54 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 70  *4])?1:0);.    p
1ca55 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d  Bt->incrVacuum =
1ca56 20 28 67 65 74 34 62 79 74 65 28 26 70 61 67 65   (get4byte(&page
1ca57 31 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30  1[36 + 7*4])?1:0
1ca58 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20  );.#endif.  }.. 
1ca59 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73 20   /* maxLocal is 
1ca5a 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75  the maximum amou
1ca5b 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 74 6f  nt of payload to
1ca5c 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 66   store locally f
1ca5d 6f 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e 20  or.  ** a cell. 
1ca5e 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73   Make sure it is
1ca5f 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73 6f   small enough so
1ca60 20 74 68 61 74 20 61 74 20 6c 65 61 73 74 20 6d   that at least m
1ca61 69 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 65  inFanout.  ** ce
1ca62 6c 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66 69 74  lls can will fit
1ca63 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 57   on one page.  W
1ca64 65 20 61 73 73 75 6d 65 20 61 20 31 30 2d 62 79  e assume a 10-by
1ca65 74 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a  te page header..
1ca66 20 20 2a 2a 20 42 65 73 69 64 65 73 20 74 68 65    ** Besides the
1ca67 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 20 63 65   payload, the ce
1ca68 6c 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a 20  ll must store:. 
1ca69 20 2a 2a 20 20 20 20 20 32 2d 62 79 74 65 20 70   **     2-byte p
1ca6a 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65  ointer to the ce
1ca6b 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79  ll.  **     4-by
1ca6c 74 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72  te child pointer
1ca6d 0a 20 20 2a 2a 20 20 20 20 20 39 2d 62 79 74 65  .  **     9-byte
1ca6e 20 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a 2a   nKey value.  **
1ca6f 20 20 20 20 20 34 2d 62 79 74 65 20 6e 44 61 74       4-byte nDat
1ca70 61 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20  a value.  **    
1ca71 20 34 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f 77   4-byte overflow
1ca72 20 70 61 67 65 20 70 6f 69 6e 74 65 72 0a 20 20   page pointer.  
1ca73 2a 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f 6e  ** So a cell con
1ca74 73 69 73 74 73 20 6f 66 20 61 20 32 2d 62 79 74  sists of a 2-byt
1ca75 65 20 70 6f 69 6e 74 65 72 2c 20 61 20 68 65 61  e pointer, a hea
1ca76 64 65 72 20 77 68 69 63 68 20 69 73 20 61 73 20  der which is as 
1ca77 6d 75 63 68 20 61 73 0a 20 20 2a 2a 20 31 37 20  much as.  ** 17 
1ca78 62 79 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f  bytes long, 0 to
1ca79 20 4e 20 62 79 74 65 73 20 6f 66 20 70 61 79 6c   N bytes of payl
1ca7a 6f 61 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69  oad, and an opti
1ca7b 6f 6e 61 6c 20 34 20 62 79 74 65 20 6f 76 65 72  onal 4 byte over
1ca7c 66 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20 70  flow.  ** page p
1ca7d 6f 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70  ointer..  */.  p
1ca7e 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28  Bt->maxLocal = (
1ca7f 75 31 36 29 28 28 70 42 74 2d 3e 75 73 61 62 6c  u16)((pBt->usabl
1ca80 65 53 69 7a 65 2d 31 32 29 2a 36 34 2f 32 35 35  eSize-12)*64/255
1ca81 20 2d 20 32 33 29 3b 0a 20 20 70 42 74 2d 3e 6d   - 23);.  pBt->m
1ca82 69 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 28  inLocal = (u16)(
1ca83 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  (pBt->usableSize
1ca84 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 33  -12)*32/255 - 23
1ca85 29 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65 61  );.  pBt->maxLea
1ca86 66 20 3d 20 28 75 31 36 29 28 70 42 74 2d 3e 75  f = (u16)(pBt->u
1ca87 73 61 62 6c 65 53 69 7a 65 20 2d 20 33 35 29 3b  sableSize - 35);
1ca88 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 20  .  pBt->minLeaf 
1ca89 3d 20 28 75 31 36 29 28 28 70 42 74 2d 3e 75 73  = (u16)((pBt->us
1ca8a 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f  ableSize-12)*32/
1ca8b 32 35 35 20 2d 20 32 33 29 3b 0a 20 20 69 66 28  255 - 23);.  if(
1ca8c 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3e 31   pBt->maxLocal>1
1ca8d 32 37 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 6d  27 ){.    pBt->m
1ca8e 61 78 31 62 79 74 65 50 61 79 6c 6f 61 64 20 3d  ax1bytePayload =
1ca8f 20 31 32 37 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   127;.  }else{. 
1ca90 20 20 20 70 42 74 2d 3e 6d 61 78 31 62 79 74 65     pBt->max1byte
1ca91 50 61 79 6c 6f 61 64 20 3d 20 28 75 38 29 70 42  Payload = (u8)pB
1ca92 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 20 7d  t->maxLocal;.  }
1ca93 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e  .  assert( pBt->
1ca94 6d 61 78 4c 65 61 66 20 2b 20 32 33 20 3c 3d 20  maxLeaf + 23 <= 
1ca95 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74  MX_CELL_SIZE(pBt
1ca96 29 20 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67  ) );.  pBt->pPag
1ca97 65 31 20 3d 20 70 50 61 67 65 31 3b 0a 20 20 70  e1 = pPage1;.  p
1ca98 42 74 2d 3e 6e 50 61 67 65 20 3d 20 6e 50 61 67  Bt->nPage = nPag
1ca99 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  e;.  return SQLI
1ca9a 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 31 5f 69 6e  TE_OK;..page1_in
1ca9b 69 74 5f 66 61 69 6c 65 64 3a 0a 20 20 72 65 6c  it_failed:.  rel
1ca9c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31 29  easePage(pPage1)
1ca9d 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20  ;.  pBt->pPage1 
1ca9e 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  = 0;.  return rc
1ca9f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ;.}../*.** If th
1caa0 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74  ere are no outst
1caa1 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 20 61  anding cursors a
1caa2 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 69 6e  nd we are not in
1caa3 20 74 68 65 20 6d 69 64 64 6c 65 0a 2a 2a 20 6f   the middle.** o
1caa4 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
1caa5 62 75 74 20 74 68 65 72 65 20 69 73 20 61 20 72  but there is a r
1caa6 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ead lock on the 
1caa7 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a 2a  database, then.*
1caa8 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 75  * this routine u
1caa9 6e 72 65 66 73 20 74 68 65 20 66 69 72 73 74 20  nrefs the first 
1caaa 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61  page of the data
1caab 62 61 73 65 20 66 69 6c 65 20 77 68 69 63 68 20  base file which 
1caac 0a 2a 2a 20 68 61 73 20 74 68 65 20 65 66 66 65  .** has the effe
1caad 63 74 20 6f 66 20 72 65 6c 65 61 73 69 6e 67 20  ct of releasing 
1caae 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a  the read lock..*
1caaf 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73  *.** If there is
1cab0 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
1cab1 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68 69 73  n progress, this
1cab2 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
1cab3 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  -op..*/.static v
1cab4 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49  oid unlockBtreeI
1cab5 66 55 6e 75 73 65 64 28 42 74 53 68 61 72 65 64  fUnused(BtShared
1cab6 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65 72 74   *pBt){.  assert
1cab7 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1cab8 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
1cab9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42   );.  assert( pB
1caba 74 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 7c 7c  t->pCursor==0 ||
1cabb 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
1cabc 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29  ion>TRANS_NONE )
1cabd 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e 54  ;.  if( pBt->inT
1cabe 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e  ransaction==TRAN
1cabf 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d 3e 70  S_NONE && pBt->p
1cac0 50 61 67 65 31 21 3d 30 20 29 7b 0a 20 20 20 20  Page1!=0 ){.    
1cac1 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61  assert( pBt->pPa
1cac2 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20  ge1->aData );.  
1cac3 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1cac4 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70  3PagerRefcount(p
1cac5 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 31 20 29  Bt->pPager)==1 )
1cac6 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  ;.    assert( pB
1cac7 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
1cac8 20 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50   );.    releaseP
1cac9 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 31 29  age(pBt->pPage1)
1caca 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65  ;.    pBt->pPage
1cacb 31 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  1 = 0;.  }.}../*
1cacc 0a 2a 2a 20 49 66 20 70 42 74 20 70 6f 69 6e 74  .** If pBt point
1cacd 73 20 74 6f 20 61 6e 20 65 6d 70 74 79 20 66 69  s to an empty fi
1cace 6c 65 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20  le then convert 
1cacf 74 68 61 74 20 65 6d 70 74 79 20 66 69 6c 65 0a  that empty file.
1cad0 2a 2a 20 69 6e 74 6f 20 61 20 6e 65 77 20 65 6d  ** into a new em
1cad1 70 74 79 20 64 61 74 61 62 61 73 65 20 62 79 20  pty database by 
1cad2 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65  initializing the
1cad3 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 0a 2a   first page of.*
1cad4 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  * the database..
1cad5 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65  */.static int ne
1cad6 77 44 61 74 61 62 61 73 65 28 42 74 53 68 61 72  wDatabase(BtShar
1cad7 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 6d 50  ed *pBt){.  MemP
1cad8 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e 73 69  age *pP1;.  unsi
1cad9 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b  gned char *data;
1cada 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
1cadb 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1cadc 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
1cadd 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 42  tex) );.  if( pB
1cade 74 2d 3e 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20  t->nPage>0 ){.  
1cadf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1cae0 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 31 20 3d 20  OK;.  }.  pP1 = 
1cae1 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 61  pBt->pPage1;.  a
1cae2 73 73 65 72 74 28 20 70 50 31 21 3d 30 20 29 3b  ssert( pP1!=0 );
1cae3 0a 20 20 64 61 74 61 20 3d 20 70 50 31 2d 3e 61  .  data = pP1->a
1cae4 44 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c  Data;.  rc = sql
1cae5 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
1cae6 50 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  P1->pDbPage);.  
1cae7 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
1cae8 72 63 3b 0a 20 20 6d 65 6d 63 70 79 28 64 61 74  rc;.  memcpy(dat
1cae9 61 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c  a, zMagicHeader,
1caea 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65   sizeof(zMagicHe
1caeb 61 64 65 72 29 29 3b 0a 20 20 61 73 73 65 72 74  ader));.  assert
1caec 28 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48  ( sizeof(zMagicH
1caed 65 61 64 65 72 29 3d 3d 31 36 20 29 3b 0a 20 20  eader)==16 );.  
1caee 64 61 74 61 5b 31 36 5d 20 3d 20 28 75 38 29 28  data[16] = (u8)(
1caef 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3e  (pBt->pageSize>>
1caf0 38 29 26 30 78 66 66 29 3b 0a 20 20 64 61 74 61  8)&0xff);.  data
1caf1 5b 31 37 5d 20 3d 20 28 75 38 29 28 28 70 42 74  [17] = (u8)((pBt
1caf2 2d 3e 70 61 67 65 53 69 7a 65 3e 3e 31 36 29 26  ->pageSize>>16)&
1caf3 30 78 66 66 29 3b 0a 20 20 64 61 74 61 5b 31 38  0xff);.  data[18
1caf4 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 5b 31 39  ] = 1;.  data[19
1caf5 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28  ] = 1;.  assert(
1caf6 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
1caf7 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  <=pBt->pageSize 
1caf8 26 26 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  && pBt->usableSi
1caf9 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e 70 61 67  ze+255>=pBt->pag
1cafa 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b 32  eSize);.  data[2
1cafb 30 5d 20 3d 20 28 75 38 29 28 70 42 74 2d 3e 70  0] = (u8)(pBt->p
1cafc 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75  ageSize - pBt->u
1cafd 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20 64 61  sableSize);.  da
1cafe 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a 20 20 64  ta[21] = 64;.  d
1caff 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b 0a 20 20  ata[22] = 32;.  
1cb00 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 3b 0a 20  data[23] = 32;. 
1cb01 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 32 34   memset(&data[24
1cb02 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 3b 0a 20  ], 0, 100-24);. 
1cb03 20 7a 65 72 6f 50 61 67 65 28 70 50 31 2c 20 50   zeroPage(pP1, P
1cb04 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45  TF_INTKEY|PTF_LE
1cb05 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 20  AF|PTF_LEAFDATA 
1cb06 29 3b 0a 20 20 70 42 74 2d 3e 62 74 73 46 6c 61  );.  pBt->btsFla
1cb07 67 73 20 7c 3d 20 42 54 53 5f 50 41 47 45 53 49  gs |= BTS_PAGESI
1cb08 5a 45 5f 46 49 58 45 44 3b 0a 23 69 66 6e 64 65  ZE_FIXED;.#ifnde
1cb09 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1cb0a 54 4f 56 41 43 55 55 4d 0a 20 20 61 73 73 65 72  TOVACUUM.  asser
1cb0b 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  t( pBt->autoVacu
1cb0c 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 75  um==1 || pBt->au
1cb0d 74 6f 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20  toVacuum==0 );. 
1cb0e 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e   assert( pBt->in
1cb0f 63 72 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70  crVacuum==1 || p
1cb10 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d  Bt->incrVacuum==
1cb11 30 20 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28  0 );.  put4byte(
1cb12 26 64 61 74 61 5b 33 36 20 2b 20 34 2a 34 5d 2c  &data[36 + 4*4],
1cb13 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
1cb14 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 64  );.  put4byte(&d
1cb15 61 74 61 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 70  ata[36 + 7*4], p
1cb16 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3b  Bt->incrVacuum);
1cb17 0a 23 65 6e 64 69 66 0a 20 20 70 42 74 2d 3e 6e  .#endif.  pBt->n
1cb18 50 61 67 65 20 3d 20 31 3b 0a 20 20 64 61 74 61  Page = 1;.  data
1cb19 5b 33 31 5d 20 3d 20 31 3b 0a 20 20 72 65 74 75  [31] = 1;.  retu
1cb1a 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1cb1b 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
1cb1c 65 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65  e the first page
1cb1d 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1cb1e 20 66 69 6c 65 20 28 63 72 65 61 74 69 6e 67 20   file (creating 
1cb1f 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  a database.** co
1cb20 6e 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73 69  nsisting of a si
1cb21 6e 67 6c 65 20 70 61 67 65 20 61 6e 64 20 6e 6f  ngle page and no
1cb22 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 29   schema objects)
1cb23 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  . Return SQLITE_
1cb24 4f 4b 0a 2a 2a 20 69 66 20 73 75 63 63 65 73 73  OK.** if success
1cb25 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74  ful, or an SQLit
1cb26 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68  e error code oth
1cb27 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  erwise..*/.SQLIT
1cb28 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1cb29 6c 69 74 65 33 42 74 72 65 65 4e 65 77 44 62 28  lite3BtreeNewDb(
1cb2a 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74  Btree *p){.  int
1cb2b 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74   rc;.  sqlite3Bt
1cb2c 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70  reeEnter(p);.  p
1cb2d 2d 3e 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 30  ->pBt->nPage = 0
1cb2e 3b 0a 20 20 72 63 20 3d 20 6e 65 77 44 61 74 61  ;.  rc = newData
1cb2f 62 61 73 65 28 70 2d 3e 70 42 74 29 3b 0a 20 20  base(p->pBt);.  
1cb30 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1cb31 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
1cb32 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65  c;.}../*.** Atte
1cb33 6d 70 74 20 74 6f 20 73 74 61 72 74 20 61 20 6e  mpt to start a n
1cb34 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ew transaction. 
1cb35 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  A write-transact
1cb36 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 72 74 65  ion.** is starte
1cb37 64 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20  d if the second 
1cb38 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a  argument is nonz
1cb39 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 61  ero, otherwise a
1cb3a 20 72 65 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61   read-.** transa
1cb3b 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 73  ction.  If the s
1cb3c 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
1cb3d 73 20 32 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20  s 2 or more and 
1cb3e 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61  exclusive.** tra
1cb3f 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72  nsaction is star
1cb40 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61  ted, meaning tha
1cb41 74 20 6e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65  t no other proce
1cb42 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a  ss is allowed.**
1cb43 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64   to access the d
1cb44 61 74 61 62 61 73 65 2e 20 20 41 20 70 72 65 65  atabase.  A pree
1cb45 78 69 73 74 69 6e 67 20 74 72 61 6e 73 61 63 74  xisting transact
1cb46 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a  ion may not be.*
1cb47 2a 20 75 70 67 72 61 64 65 64 20 74 6f 20 65 78  * upgraded to ex
1cb48 63 6c 75 73 69 76 65 20 62 79 20 63 61 6c 6c 69  clusive by calli
1cb49 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ng this routine 
1cb4a 61 20 73 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20  a second time - 
1cb4b 74 68 65 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69  the.** exclusivi
1cb4c 74 79 20 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72  ty flag only wor
1cb4d 6b 73 20 66 6f 72 20 61 20 6e 65 77 20 74 72 61  ks for a new tra
1cb4e 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nsaction..**.** 
1cb4f 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  A write-transact
1cb50 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72  ion must be star
1cb51 74 65 64 20 62 65 66 6f 72 65 20 61 74 74 65 6d  ted before attem
1cb52 70 74 69 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68  pting any .** ch
1cb53 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 61 74  anges to the dat
1cb54 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20  abase.  None of 
1cb55 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  the following ro
1cb56 75 74 69 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20  utines .** will 
1cb57 77 6f 72 6b 20 75 6e 6c 65 73 73 20 61 20 74 72  work unless a tr
1cb58 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61  ansaction is sta
1cb59 72 74 65 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a  rted first:.**.*
1cb5a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  *      sqlite3Bt
1cb5b 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 29  reeCreateTable()
1cb5c 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
1cb5d 42 74 72 65 65 43 72 65 61 74 65 49 6e 64 65 78  BtreeCreateIndex
1cb5e 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74  ().**      sqlit
1cb5f 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c  e3BtreeClearTabl
1cb60 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  e().**      sqli
1cb61 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c  te3BtreeDropTabl
1cb62 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  e().**      sqli
1cb63 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 29  te3BtreeInsert()
1cb64 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
1cb65 42 74 72 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a  BtreeDelete().**
1cb66 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
1cb67 65 65 55 70 64 61 74 65 4d 65 74 61 28 29 0a 2a  eeUpdateMeta().*
1cb68 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 69 74 69  *.** If an initi
1cb69 61 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63  al attempt to ac
1cb6a 71 75 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 66  quire the lock f
1cb6b 61 69 6c 73 20 62 65 63 61 75 73 65 20 6f 66 20  ails because of 
1cb6c 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a  lock contention.
1cb6d 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 62  ** and the datab
1cb6e 61 73 65 20 77 61 73 20 70 72 65 76 69 6f 75 73  ase was previous
1cb6f 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65  ly unlocked, the
1cb70 6e 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73  n invoke the bus
1cb71 79 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20  y handler.** if 
1cb72 74 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 42  there is one.  B
1cb73 75 74 20 69 66 20 74 68 65 72 65 20 77 61 73 20  ut if there was 
1cb74 70 72 65 76 69 6f 75 73 6c 79 20 61 20 72 65 61  previously a rea
1cb75 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a  d-lock, do not.*
1cb76 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73  * invoke the bus
1cb77 79 20 68 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74  y handler - just
1cb78 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
1cb79 55 53 59 2e 20 20 53 51 4c 49 54 45 5f 42 55 53  USY.  SQLITE_BUS
1cb7a 59 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65  Y is .** returne
1cb7b 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20  d when there is 
1cb7c 61 6c 72 65 61 64 79 20 61 20 72 65 61 64 2d 6c  already a read-l
1cb7d 6f 63 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ock in order to 
1cb7e 61 76 6f 69 64 20 61 20 64 65 61 64 6c 6f 63 6b  avoid a deadlock
1cb7f 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20  ..**.** Suppose 
1cb80 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 70 72  there are two pr
1cb81 6f 63 65 73 73 65 73 20 41 20 61 6e 64 20 42 2e  ocesses A and B.
1cb82 20 20 41 20 68 61 73 20 61 20 72 65 61 64 20 6c    A has a read l
1cb83 6f 63 6b 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a  ock and B has.**
1cb84 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b   a reserved lock
1cb85 2e 20 20 42 20 74 72 69 65 73 20 74 6f 20 70 72  .  B tries to pr
1cb86 6f 6d 6f 74 65 20 74 6f 20 65 78 63 6c 75 73 69  omote to exclusi
1cb87 76 65 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65  ve but is blocke
1cb88 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20  d because.** of 
1cb89 41 27 73 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20  A's read lock.  
1cb8a 41 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f  A tries to promo
1cb8b 74 65 20 74 6f 20 72 65 73 65 72 76 65 64 20 62  te to reserved b
1cb8c 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79  ut is blocked by
1cb8d 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68   B..** One or th
1cb8e 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 74  e other of the t
1cb8f 77 6f 20 70 72 6f 63 65 73 73 65 73 20 6d 75 73  wo processes mus
1cb90 74 20 67 69 76 65 20 77 61 79 20 6f 72 20 74 68  t give way or th
1cb91 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f  ere can be.** no
1cb92 20 70 72 6f 67 72 65 73 73 2e 20 20 42 79 20 72   progress.  By r
1cb93 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f  eturning SQLITE_
1cb94 42 55 53 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76  BUSY and not inv
1cb95 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 63  oking the busy c
1cb96 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20  allback.** when 
1cb97 41 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 20  A already has a 
1cb98 72 65 61 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e  read lock, we en
1cb99 63 6f 75 72 61 67 65 20 41 20 74 6f 20 67 69 76  courage A to giv
1cb9a 65 20 75 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a  e up and let B.*
1cb9b 2a 20 70 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51  * proceed..*/.SQ
1cb9c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1cb9d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
1cb9e 69 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70  inTrans(Btree *p
1cb9f 2c 20 69 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20  , int wrflag){. 
1cba0 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b   sqlite3 *pBlock
1cba1 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64   = 0;.  BtShared
1cba2 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
1cba3 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1cba4 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33  E_OK;..  sqlite3
1cba5 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
1cba6 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28   btreeIntegrity(
1cba7 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  p);..  /* If the
1cba8 20 62 74 72 65 65 20 69 73 20 61 6c 72 65 61 64   btree is alread
1cba9 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61  y in a write-tra
1cbaa 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a  nsaction, or it.
1cbab 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20    ** is already 
1cbac 69 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61  in a read-transa
1cbad 63 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 64  ction and a read
1cbae 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a  -transaction.  *
1cbaf 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20  * is requested, 
1cbb0 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  this is a no-op.
1cbb1 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69  .  */.  if( p->i
1cbb2 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52  nTrans==TRANS_WR
1cbb3 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61  ITE || (p->inTra
1cbb4 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 26  ns==TRANS_READ &
1cbb5 26 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 20  & !wrflag) ){.  
1cbb6 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67    goto trans_beg
1cbb7 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72  un;.  }..  /* Wr
1cbb8 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  ite transactions
1cbb9 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c   are not possibl
1cbba 65 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79  e on a read-only
1cbbb 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69   database */.  i
1cbbc 66 28 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67  f( (pBt->btsFlag
1cbbd 73 20 26 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c  s & BTS_READ_ONL
1cbbe 59 29 21 3d 30 20 26 26 20 77 72 66 6c 61 67 20  Y)!=0 && wrflag 
1cbbf 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
1cbc0 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20  TE_READONLY;.   
1cbc1 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75   goto trans_begu
1cbc2 6e 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  n;.  }..#ifndef 
1cbc3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
1cbc4 45 44 5f 43 41 43 48 45 0a 20 20 2f 2a 20 49 66  ED_CACHE.  /* If
1cbc5 20 61 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73   another databas
1cbc6 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61 6c 72  e handle has alr
1cbc7 65 61 64 79 20 6f 70 65 6e 65 64 20 61 20 77 72  eady opened a wr
1cbc8 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite transaction 
1cbc9 0a 20 20 2a 2a 20 6f 6e 20 74 68 69 73 20 73 68  .  ** on this sh
1cbca 61 72 65 64 2d 62 74 72 65 65 20 73 74 72 75 63  ared-btree struc
1cbcb 74 75 72 65 20 61 6e 64 20 61 20 73 65 63 6f 6e  ture and a secon
1cbcc 64 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  d write transact
1cbcd 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 65 71 75  ion is.  ** requ
1cbce 65 73 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51  ested, return SQ
1cbcf 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20 2a  LITE_LOCKED..  *
1cbd0 2f 0a 20 20 69 66 28 20 28 77 72 66 6c 61 67 20  /.  if( (wrflag 
1cbd1 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  && pBt->inTransa
1cbd2 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
1cbd3 54 45 29 0a 20 20 20 7c 7c 20 28 70 42 74 2d 3e  TE).   || (pBt->
1cbd4 62 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 50  btsFlags & BTS_P
1cbd5 45 4e 44 49 4e 47 29 21 3d 30 0a 20 20 29 7b 0a  ENDING)!=0.  ){.
1cbd6 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 70 42 74      pBlock = pBt
1cbd7 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 3b 0a 20  ->pWriter->db;. 
1cbd8 20 7d 65 6c 73 65 20 69 66 28 20 77 72 66 6c 61   }else if( wrfla
1cbd9 67 3e 31 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63  g>1 ){.    BtLoc
1cbda 6b 20 2a 70 49 74 65 72 3b 0a 20 20 20 20 66 6f  k *pIter;.    fo
1cbdb 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f  r(pIter=pBt->pLo
1cbdc 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72  ck; pIter; pIter
1cbdd 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a  =pIter->pNext){.
1cbde 20 20 20 20 20 20 69 66 28 20 70 49 74 65 72 2d        if( pIter-
1cbdf 3e 70 42 74 72 65 65 21 3d 70 20 29 7b 0a 20 20  >pBtree!=p ){.  
1cbe0 20 20 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 70        pBlock = p
1cbe1 49 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  Iter->pBtree->db
1cbe2 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
1cbe3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1cbe4 20 7d 0a 20 20 69 66 28 20 70 42 6c 6f 63 6b 20   }.  if( pBlock 
1cbe5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f  ){.    sqlite3Co
1cbe6 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28  nnectionBlocked(
1cbe7 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b 29 3b 0a  p->db, pBlock);.
1cbe8 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1cbe9 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43  LOCKED_SHAREDCAC
1cbea 48 45 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61  HE;.    goto tra
1cbeb 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 23 65  ns_begun;.  }.#e
1cbec 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6e 79 20 72  ndif..  /* Any r
1cbed 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 65 61 64  ead-only or read
1cbee 2d 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69  -write transacti
1cbef 6f 6e 20 69 6d 70 6c 69 65 73 20 61 20 72 65 61  on implies a rea
1cbf0 64 2d 6c 6f 63 6b 20 6f 6e 20 0a 20 20 2a 2a 20  d-lock on .  ** 
1cbf1 70 61 67 65 20 31 2e 20 53 6f 20 69 66 20 73 6f  page 1. So if so
1cbf2 6d 65 20 6f 74 68 65 72 20 73 68 61 72 65 64 2d  me other shared-
1cbf3 63 61 63 68 65 20 63 6c 69 65 6e 74 20 61 6c 72  cache client alr
1cbf4 65 61 64 79 20 68 61 73 20 61 20 77 72 69 74 65  eady has a write
1cbf5 2d 6c 6f 63 6b 20 0a 20 20 2a 2a 20 6f 6e 20 70  -lock .  ** on p
1cbf6 61 67 65 20 31 2c 20 74 68 65 20 74 72 61 6e 73  age 1, the trans
1cbf7 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65  action cannot be
1cbf8 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 72 63   opened. */.  rc
1cbf9 20 3d 20 71 75 65 72 79 53 68 61 72 65 64 43 61   = querySharedCa
1cbfa 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  cheTableLock(p, 
1cbfb 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41  MASTER_ROOT, REA
1cbfc 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 53  D_LOCK);.  if( S
1cbfd 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 20 67  QLITE_OK!=rc ) g
1cbfe 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b  oto trans_begun;
1cbff 0a 0a 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67  ..  pBt->btsFlag
1cc00 73 20 26 3d 20 7e 42 54 53 5f 49 4e 49 54 49 41  s &= ~BTS_INITIA
1cc01 4c 4c 59 5f 45 4d 50 54 59 3b 0a 20 20 69 66 28  LLY_EMPTY;.  if(
1cc02 20 70 42 74 2d 3e 6e 50 61 67 65 3d 3d 30 20 29   pBt->nPage==0 )
1cc03 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20 7c   pBt->btsFlags |
1cc04 3d 20 42 54 53 5f 49 4e 49 54 49 41 4c 4c 59 5f  = BTS_INITIALLY_
1cc05 45 4d 50 54 59 3b 0a 20 20 64 6f 20 7b 0a 20 20  EMPTY;.  do {.  
1cc06 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f 63 6b 42 74    /* Call lockBt
1cc07 72 65 65 28 29 20 75 6e 74 69 6c 20 65 69 74 68  ree() until eith
1cc08 65 72 20 70 42 74 2d 3e 70 50 61 67 65 31 20 69  er pBt->pPage1 i
1cc09 73 20 70 6f 70 75 6c 61 74 65 64 20 6f 72 0a 20  s populated or. 
1cc0a 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 72 65 65 28     ** lockBtree(
1cc0b 29 20 72 65 74 75 72 6e 73 20 73 6f 6d 65 74 68  ) returns someth
1cc0c 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 53  ing other than S
1cc0d 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f 63 6b 42 74  QLITE_OK. lockBt
1cc0e 72 65 65 28 29 0a 20 20 20 20 2a 2a 20 6d 61 79  ree().    ** may
1cc0f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1cc10 4b 20 62 75 74 20 6c 65 61 76 65 20 70 42 74 2d  K but leave pBt-
1cc11 3e 70 50 61 67 65 31 20 73 65 74 20 74 6f 20 30  >pPage1 set to 0
1cc12 20 69 66 20 61 66 74 65 72 0a 20 20 20 20 2a 2a   if after.    **
1cc13 20 72 65 61 64 69 6e 67 20 70 61 67 65 20 31 20   reading page 1 
1cc14 69 74 20 64 69 73 63 6f 76 65 72 73 20 74 68 61  it discovers tha
1cc15 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20  t the page-size 
1cc16 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1cc17 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 69 73 20  .    ** file is 
1cc18 6e 6f 74 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  not pBt->pageSiz
1cc19 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  e. In this case 
1cc1a 6c 6f 63 6b 42 74 72 65 65 28 29 20 77 69 6c 6c  lockBtree() will
1cc1b 20 75 70 64 61 74 65 0a 20 20 20 20 2a 2a 20 70   update.    ** p
1cc1c 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 74 6f 20  Bt->pageSize to 
1cc1d 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66  the page-size of
1cc1e 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73   the file on dis
1cc1f 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68  k..    */.    wh
1cc20 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 67 65 31  ile( pBt->pPage1
1cc21 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b  ==0 && SQLITE_OK
1cc22 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42 74 72 65  ==(rc = lockBtre
1cc23 65 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 20 20  e(pBt)) );..    
1cc24 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1cc25 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20  K && wrflag ){. 
1cc26 20 20 20 20 20 69 66 28 20 28 70 42 74 2d 3e 62       if( (pBt->b
1cc27 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 52 45  tsFlags & BTS_RE
1cc28 41 44 5f 4f 4e 4c 59 29 21 3d 30 20 29 7b 0a 20  AD_ONLY)!=0 ){. 
1cc29 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
1cc2a 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20  TE_READONLY;.   
1cc2b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1cc2c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1cc2d 67 65 72 42 65 67 69 6e 28 70 42 74 2d 3e 70 50  gerBegin(pBt->pP
1cc2e 61 67 65 72 2c 77 72 66 6c 61 67 3e 31 2c 73 71  ager,wrflag>1,sq
1cc2f 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72  lite3TempInMemor
1cc30 79 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20  y(p->db));.     
1cc31 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1cc32 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1cc33 20 20 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61    rc = newDataba
1cc34 73 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20  se(pBt);.       
1cc35 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
1cc36 0a 20 20 0a 20 20 20 20 69 66 28 20 72 63 21 3d  .  .    if( rc!=
1cc37 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1cc38 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66     unlockBtreeIf
1cc39 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20  Unused(pBt);.   
1cc3a 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 28 72 63   }.  }while( (rc
1cc3b 26 30 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 42  &0xFF)==SQLITE_B
1cc3c 55 53 59 20 26 26 20 70 42 74 2d 3e 69 6e 54 72  USY && pBt->inTr
1cc3d 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
1cc3e 5f 4e 4f 4e 45 20 26 26 0a 20 20 20 20 20 20 20  _NONE &&.       
1cc3f 20 20 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75     btreeInvokeBu
1cc40 73 79 48 61 6e 64 6c 65 72 28 70 42 74 29 20 29  syHandler(pBt) )
1cc41 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
1cc42 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
1cc43 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
1cc44 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20  ANS_NONE ){.    
1cc45 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74    pBt->nTransact
1cc46 69 6f 6e 2b 2b 3b 0a 23 69 66 6e 64 65 66 20 53  ion++;.#ifndef S
1cc47 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
1cc48 44 5f 43 41 43 48 45 0a 20 20 20 20 20 20 69 66  D_CACHE.      if
1cc49 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b  ( p->sharable ){
1cc4a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1cc4b 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 3d   p->lock.pBtree=
1cc4c 3d 70 20 26 26 20 70 2d 3e 6c 6f 63 6b 2e 69 54  =p && p->lock.iT
1cc4d 61 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20  able==1 );.     
1cc4e 20 20 20 70 2d 3e 6c 6f 63 6b 2e 65 4c 6f 63 6b     p->lock.eLock
1cc4f 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0a 20 20   = READ_LOCK;.  
1cc50 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 70 4e        p->lock.pN
1cc51 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b  ext = pBt->pLock
1cc52 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 70  ;.        pBt->p
1cc53 4c 6f 63 6b 20 3d 20 26 70 2d 3e 6c 6f 63 6b 3b  Lock = &p->lock;
1cc54 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
1cc55 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54      }.    p->inT
1cc56 72 61 6e 73 20 3d 20 28 77 72 66 6c 61 67 3f 54  rans = (wrflag?T
1cc57 52 41 4e 53 5f 57 52 49 54 45 3a 54 52 41 4e 53  RANS_WRITE:TRANS
1cc58 5f 52 45 41 44 29 3b 0a 20 20 20 20 69 66 28 20  _READ);.    if( 
1cc59 70 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e  p->inTrans>pBt->
1cc5a 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b  inTransaction ){
1cc5b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72  .      pBt->inTr
1cc5c 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69  ansaction = p->i
1cc5d 6e 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a 20 20  nTrans;.    }.  
1cc5e 20 20 69 66 28 20 77 72 66 6c 61 67 20 29 7b 0a    if( wrflag ){.
1cc5f 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70        MemPage *p
1cc60 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61  Page1 = pBt->pPa
1cc61 67 65 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ge1;.#ifndef SQL
1cc62 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
1cc63 43 41 43 48 45 0a 20 20 20 20 20 20 61 73 73 65  CACHE.      asse
1cc64 72 74 28 20 21 70 42 74 2d 3e 70 57 72 69 74 65  rt( !pBt->pWrite
1cc65 72 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e  r );.      pBt->
1cc66 70 57 72 69 74 65 72 20 3d 20 70 3b 0a 20 20 20  pWriter = p;.   
1cc67 20 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73     pBt->btsFlags
1cc68 20 26 3d 20 7e 42 54 53 5f 45 58 43 4c 55 53 49   &= ~BTS_EXCLUSI
1cc69 56 45 3b 0a 20 20 20 20 20 20 69 66 28 20 77 72  VE;.      if( wr
1cc6a 66 6c 61 67 3e 31 20 29 20 70 42 74 2d 3e 62 74  flag>1 ) pBt->bt
1cc6b 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f 45 58  sFlags |= BTS_EX
1cc6c 43 4c 55 53 49 56 45 3b 0a 23 65 6e 64 69 66 0a  CLUSIVE;.#endif.
1cc6d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
1cc6e 20 64 62 2d 73 69 7a 65 20 68 65 61 64 65 72 20   db-size header 
1cc6f 66 69 65 6c 64 20 69 73 20 69 6e 63 6f 72 72 65  field is incorre
1cc70 63 74 20 28 61 73 20 69 74 20 6d 61 79 20 62 65  ct (as it may be
1cc71 20 69 66 20 61 6e 20 6f 6c 64 0a 20 20 20 20 20   if an old.     
1cc72 20 2a 2a 20 63 6c 69 65 6e 74 20 68 61 73 20 62   ** client has b
1cc73 65 65 6e 20 77 72 69 74 69 6e 67 20 74 68 65 20  een writing the 
1cc74 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 2c 20  database file), 
1cc75 75 70 64 61 74 65 20 69 74 20 6e 6f 77 2e 20 44  update it now. D
1cc76 6f 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 74 68  oing.      ** th
1cc77 69 73 20 73 6f 6f 6e 65 72 20 72 61 74 68 65 72  is sooner rather
1cc78 20 74 68 61 6e 20 6c 61 74 65 72 20 6d 65 61 6e   than later mean
1cc79 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  s the database s
1cc7a 69 7a 65 20 63 61 6e 20 73 61 66 65 6c 79 20 0a  ize can safely .
1cc7b 20 20 20 20 20 20 2a 2a 20 72 65 2d 72 65 61 64        ** re-read
1cc7c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69   the database si
1cc7d 7a 65 20 66 72 6f 6d 20 70 61 67 65 20 31 20 69  ze from page 1 i
1cc7e 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 6f 72  f a savepoint or
1cc7f 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20   transaction.   
1cc80 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6f     ** rollback o
1cc81 63 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 65  ccurs within the
1cc82 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20   transaction..  
1cc83 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
1cc84 20 70 42 74 2d 3e 6e 50 61 67 65 21 3d 67 65 74   pBt->nPage!=get
1cc85 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
1cc86 44 61 74 61 5b 32 38 5d 29 20 29 7b 0a 20 20 20  Data[28]) ){.   
1cc87 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1cc88 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67  3PagerWrite(pPag
1cc89 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  e1->pDbPage);.  
1cc8a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
1cc8b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1cc8c 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
1cc8d 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 32 38 5d  Page1->aData[28]
1cc8e 2c 20 70 42 74 2d 3e 6e 50 61 67 65 29 3b 0a 20  , pBt->nPage);. 
1cc8f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1cc90 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 74 72 61  .    }.  }...tra
1cc91 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 69 66 28 20  ns_begun:.  if( 
1cc92 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1cc93 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 2f   wrflag ){.    /
1cc94 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 61 6b 65  * This call make
1cc95 73 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20  s sure that the 
1cc96 70 61 67 65 72 20 68 61 73 20 74 68 65 20 63 6f  pager has the co
1cc97 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66 0a  rrect number of.
1cc98 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73 61 76 65      ** open save
1cc99 70 6f 69 6e 74 73 2e 20 49 66 20 74 68 65 20 73  points. If the s
1cc9a 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
1cc9b 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
1cc9c 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 68 65  0 and.    ** the
1cc9d 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20   sub-journal is 
1cc9e 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e  not already open
1cc9f 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 62  , then it will b
1cca0 65 20 6f 70 65 6e 65 64 20 68 65 72 65 2e 0a 20  e opened here.. 
1cca1 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73     */.    rc = s
1cca2 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53  qlite3PagerOpenS
1cca3 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50  avepoint(pBt->pP
1cca4 61 67 65 72 2c 20 70 2d 3e 64 62 2d 3e 6e 53 61  ager, p->db->nSa
1cca5 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a 0a 20  vepoint);.  }.. 
1cca6 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28   btreeIntegrity(
1cca7 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  p);.  sqlite3Btr
1cca8 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
1cca9 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e  turn rc;.}..#ifn
1ccaa 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1ccab 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f 2a 0a 2a  AUTOVACUUM../*.*
1ccac 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74 65  * Set the pointe
1ccad 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f  r-map entries fo
1ccae 72 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f  r all children o
1ccaf 66 20 70 61 67 65 20 70 50 61 67 65 2e 20 41 6c  f page pPage. Al
1ccb0 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 67 65 20  so, if.** pPage 
1ccb1 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c 73 20 74  contains cells t
1ccb2 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65  hat point to ove
1ccb3 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 73 65 74  rflow pages, set
1ccb4 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20   the pointer.** 
1ccb5 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20  map entries for 
1ccb6 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  the overflow pag
1ccb7 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 73  es as well..*/.s
1ccb8 74 61 74 69 63 20 69 6e 74 20 73 65 74 43 68 69  tatic int setChi
1ccb9 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d 50 61 67  ldPtrmaps(MemPag
1ccba 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 6e 74  e *pPage){.  int
1ccbb 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
1ccbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ccbd 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 72 69 61  /* Counter varia
1ccbe 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65  ble */.  int nCe
1ccbf 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
1ccc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1ccc1 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69  umber of cells i
1ccc2 6e 20 70 61 67 65 20 70 50 61 67 65 20 2a 2f 0a  n page pPage */.
1ccc3 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
1ccc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ccc5 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
1ccc6 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65  ode */.  BtShare
1ccc7 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  d *pBt = pPage->
1ccc8 70 42 74 3b 0a 20 20 75 38 20 69 73 49 6e 69 74  pBt;.  u8 isInit
1ccc9 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73  Orig = pPage->is
1ccca 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e  Init;.  Pgno pgn
1cccb 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3b  o = pPage->pgno;
1cccc 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1cccd 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1ccce 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
1cccf 29 20 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65  ) );.  rc = btre
1ccd0 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29  eInitPage(pPage)
1ccd1 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
1ccd2 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74  TE_OK ){.    got
1ccd3 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d  o set_child_ptrm
1ccd4 61 70 73 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6e  aps_out;.  }.  n
1ccd5 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43  Cell = pPage->nC
1ccd6 65 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  ell;..  for(i=0;
1ccd7 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a   i<nCell; i++){.
1ccd8 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20      u8 *pCell = 
1ccd9 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
1ccda 69 29 3b 0a 0a 20 20 20 20 70 74 72 6d 61 70 50  i);..    ptrmapP
1ccdb 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 2c  utOvflPtr(pPage,
1ccdc 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 0a 20   pCell, &rc);.. 
1ccdd 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c     if( !pPage->l
1ccde 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50 67 6e  eaf ){.      Pgn
1ccdf 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65  o childPgno = ge
1cce0 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 20  t4byte(pCell);. 
1cce1 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70       ptrmapPut(p
1cce2 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50  Bt, childPgno, P
1cce3 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e  TRMAP_BTREE, pgn
1cce4 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20  o, &rc);.    }. 
1cce5 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65   }..  if( !pPage
1cce6 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 67  ->leaf ){.    Pg
1cce7 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67  no childPgno = g
1cce8 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
1cce9 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
1ccea 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
1cceb 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 63  ptrmapPut(pBt, c
1ccec 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 4d 41 50  hildPgno, PTRMAP
1cced 5f 42 54 52 45 45 2c 20 70 67 6e 6f 2c 20 26 72  _BTREE, pgno, &r
1ccee 63 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63 68 69  c);.  }..set_chi
1ccef 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3a 0a  ld_ptrmaps_out:.
1ccf0 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20    pPage->isInit 
1ccf1 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20 20  = isInitOrig;.  
1ccf2 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1ccf3 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20 6f 6e  .** Somewhere on
1ccf4 20 70 50 61 67 65 20 69 73 20 61 20 70 6f 69 6e   pPage is a poin
1ccf5 74 65 72 20 74 6f 20 70 61 67 65 20 69 46 72 6f  ter to page iFro
1ccf6 6d 2e 20 20 4d 6f 64 69 66 79 20 74 68 69 73 20  m.  Modify this 
1ccf7 70 6f 69 6e 74 65 72 20 73 6f 0a 2a 2a 20 74 68  pointer so.** th
1ccf8 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  at it points to 
1ccf9 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72 20 65  iTo. Parameter e
1ccfa 54 79 70 65 20 64 65 73 63 72 69 62 65 73 20 74  Type describes t
1ccfb 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69 6e 74  he type of point
1ccfc 65 72 20 74 6f 0a 2a 2a 20 62 65 20 6d 6f 64 69  er to.** be modi
1ccfd 66 69 65 64 2c 20 61 73 20 20 66 6f 6c 6c 6f 77  fied, as  follow
1ccfe 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f  s:.**.** PTRMAP_
1ccff 42 54 52 45 45 3a 20 20 20 20 20 70 50 61 67 65  BTREE:     pPage
1cd00 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 67 65   is a btree-page
1cd01 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f  . The pointer po
1cd02 69 6e 74 73 20 61 74 20 61 20 63 68 69 6c 64 20  ints at a child 
1cd03 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1cd04 20 20 20 20 20 20 70 61 67 65 20 6f 66 20 70 50        page of pP
1cd05 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  age..**.** PTRMA
1cd06 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70 50 61  P_OVERFLOW1: pPa
1cd07 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61  ge is a btree-pa
1cd08 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20  ge. The pointer 
1cd09 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f 76 65  points at an ove
1cd0a 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20  rflow.**        
1cd0b 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20             page 
1cd0c 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 6f 6e  pointed to by on
1cd0d 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 6f  e of the cells o
1cd0e 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50  n pPage..**.** P
1cd0f 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a  TRMAP_OVERFLOW2:
1cd10 20 70 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65   pPage is an ove
1cd11 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68 65 20  rflow-page. The 
1cd12 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61  pointer points a
1cd13 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20  t the next.**   
1cd14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cd15 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e  overflow page in
1cd16 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74   the list..*/.st
1cd17 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 66 79 50  atic int modifyP
1cd18 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d 50 61  agePointer(MemPa
1cd19 67 65 20 2a 70 50 61 67 65 2c 20 50 67 6e 6f 20  ge *pPage, Pgno 
1cd1a 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54 6f 2c  iFrom, Pgno iTo,
1cd1b 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20 61 73   u8 eType){.  as
1cd1c 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1cd1d 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
1cd1e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
1cd1f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1cd20 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
1cd21 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
1cd22 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70 65 3d   );.  if( eType=
1cd23 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57  =PTRMAP_OVERFLOW
1cd24 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  2 ){.    /* The 
1cd25 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77 61 79  pointer is alway
1cd26 73 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79  s the first 4 by
1cd27 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 20  tes of the page 
1cd28 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a  in this case.  *
1cd29 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34 62 79  /.    if( get4by
1cd2a 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29  te(pPage->aData)
1cd2b 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20  !=iFrom ){.     
1cd2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1cd2d 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
1cd2e 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74 65 28   }.    put4byte(
1cd2f 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54  pPage->aData, iT
1cd30 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  o);.  }else{.   
1cd31 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67 20 3d   u8 isInitOrig =
1cd32 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a   pPage->isInit;.
1cd33 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69      int i;.    i
1cd34 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 62  nt nCell;..    b
1cd35 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61  treeInitPage(pPa
1cd36 67 65 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d  ge);.    nCell =
1cd37 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a   pPage->nCell;..
1cd38 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
1cd39 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Cell; i++){.    
1cd3a 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69    u8 *pCell = fi
1cd3b 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29  ndCell(pPage, i)
1cd3c 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70  ;.      if( eTyp
1cd3d 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  e==PTRMAP_OVERFL
1cd3e 4f 57 31 20 29 7b 0a 20 20 20 20 20 20 20 20 43  OW1 ){.        C
1cd3f 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20  ellInfo info;.  
1cd40 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 65        btreeParse
1cd41 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70  CellPtr(pPage, p
1cd42 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  Cell, &info);.  
1cd43 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69        if( info.i
1cd44 4f 76 65 72 66 6c 6f 77 0a 20 20 20 20 20 20 20  Overflow.       
1cd45 20 20 26 26 20 70 43 65 6c 6c 2b 69 6e 66 6f 2e    && pCell+info.
1cd46 69 4f 76 65 72 66 6c 6f 77 2b 33 3c 3d 70 50 61  iOverflow+3<=pPa
1cd47 67 65 2d 3e 61 44 61 74 61 2b 70 50 61 67 65 2d  ge->aData+pPage-
1cd48 3e 6d 61 73 6b 50 61 67 65 0a 20 20 20 20 20 20  >maskPage.      
1cd49 20 20 20 26 26 20 69 46 72 6f 6d 3d 3d 67 65 74     && iFrom==get
1cd4a 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66  4byte(&pCell[inf
1cd4b 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 0a 20 20  o.iOverflow]).  
1cd4c 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
1cd4d 20 20 20 70 75 74 34 62 79 74 65 28 26 70 43 65     put4byte(&pCe
1cd4e 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
1cd4f 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20  w], iTo);.      
1cd50 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1cd51 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
1cd52 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 67 65  {.        if( ge
1cd53 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3d 3d 69  t4byte(pCell)==i
1cd54 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  From ){.        
1cd55 20 20 70 75 74 34 62 79 74 65 28 70 43 65 6c 6c    put4byte(pCell
1cd56 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20  , iTo);.        
1cd57 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1cd58 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
1cd59 0a 20 20 0a 20 20 20 20 69 66 28 20 69 3d 3d 6e  .  .    if( i==n
1cd5a 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66  Cell ){.      if
1cd5b 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f  ( eType!=PTRMAP_
1cd5c 42 54 52 45 45 20 7c 7c 20 0a 20 20 20 20 20 20  BTREE || .      
1cd5d 20 20 20 20 67 65 74 34 62 79 74 65 28 26 70 50      get4byte(&pP
1cd5e 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
1cd5f 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 21  ->hdrOffset+8])!
1cd60 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20  =iFrom ){.      
1cd61 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1cd62 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1cd63 20 20 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34      }.      put4
1cd64 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61  byte(&pPage->aDa
1cd65 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  ta[pPage->hdrOff
1cd66 73 65 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20 20  set+8], iTo);.  
1cd67 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 2d 3e    }..    pPage->
1cd68 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f  isInit = isInitO
1cd69 72 69 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  rig;.  }.  retur
1cd6a 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1cd6b 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20  ./*.** Move the 
1cd6c 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 70 61  open database pa
1cd6d 67 65 20 70 44 62 50 61 67 65 20 74 6f 20 6c 6f  ge pDbPage to lo
1cd6e 63 61 74 69 6f 6e 20 69 46 72 65 65 50 61 67 65  cation iFreePage
1cd6f 20 69 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61   in the .** data
1cd70 62 61 73 65 2e 20 54 68 65 20 70 44 62 50 61 67  base. The pDbPag
1cd71 65 20 72 65 66 65 72 65 6e 63 65 20 72 65 6d 61  e reference rema
1cd72 69 6e 73 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a  ins valid..**.**
1cd73 20 54 68 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c   The isCommit fl
1cd74 61 67 20 69 6e 64 69 63 61 74 65 73 20 74 68 61  ag indicates tha
1cd75 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65  t there is no ne
1cd76 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74  ed to remember t
1cd77 68 61 74 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e  hat.** the journ
1cd78 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73  al needs to be s
1cd79 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 64  ync()ed before d
1cd7a 61 74 61 62 61 73 65 20 70 61 67 65 20 70 44 62  atabase page pDb
1cd7b 50 61 67 65 2d 3e 70 67 6e 6f 20 0a 2a 2a 20 63  Page->pgno .** c
1cd7c 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f  an be written to
1cd7d 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 68 61 73  . The caller has
1cd7e 20 61 6c 72 65 61 64 79 20 70 72 6f 6d 69 73 65   already promise
1cd7f 64 20 6e 6f 74 20 74 6f 20 77 72 69 74 65 20 74  d not to write t
1cd80 6f 20 74 68 61 74 0a 2a 2a 20 70 61 67 65 2e 0a  o that.** page..
1cd81 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
1cd82 6c 6f 63 61 74 65 50 61 67 65 28 0a 20 20 42 74  locatePage(.  Bt
1cd83 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20  Shared *pBt,    
1cd84 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20         /* Btree 
1cd85 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 44  */.  MemPage *pD
1cd86 62 50 61 67 65 2c 20 20 20 20 20 20 20 20 2f 2a  bPage,        /*
1cd87 20 4f 70 65 6e 20 70 61 67 65 20 74 6f 20 6d 6f   Open page to mo
1cd88 76 65 20 2a 2f 0a 20 20 75 38 20 65 54 79 70 65  ve */.  u8 eType
1cd89 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1cd8a 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20   /* Pointer map 
1cd8b 27 74 79 70 65 27 20 65 6e 74 72 79 20 66 6f 72  'type' entry for
1cd8c 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67   pDbPage */.  Pg
1cd8d 6e 6f 20 69 50 74 72 50 61 67 65 2c 20 20 20 20  no iPtrPage,    
1cd8e 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
1cd8f 72 20 6d 61 70 20 27 70 61 67 65 2d 6e 6f 27 20  r map 'page-no' 
1cd90 65 6e 74 72 79 20 66 6f 72 20 70 44 62 50 61 67  entry for pDbPag
1cd91 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 46 72 65  e */.  Pgno iFre
1cd92 65 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20  ePage,          
1cd93 2f 2a 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20  /* The location 
1cd94 74 6f 20 6d 6f 76 65 20 70 44 62 50 61 67 65 20  to move pDbPage 
1cd95 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f  to */.  int isCo
1cd96 6d 6d 69 74 20 20 20 20 20 20 20 20 20 20 20 20  mmit            
1cd97 20 2f 2a 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61   /* isCommit fla
1cd98 67 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69  g passed to sqli
1cd99 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65  te3PagerMovepage
1cd9a 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65   */.){.  MemPage
1cd9b 20 2a 70 50 74 72 50 61 67 65 3b 20 20 20 2f 2a   *pPtrPage;   /*
1cd9c 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63   The page that c
1cd9d 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65  ontains a pointe
1cd9e 72 20 74 6f 20 70 44 62 50 61 67 65 20 2a 2f 0a  r to pDbPage */.
1cd9f 20 20 50 67 6e 6f 20 69 44 62 50 61 67 65 20 3d    Pgno iDbPage =
1cda0 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a   pDbPage->pgno;.
1cda1 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20    Pager *pPager 
1cda2 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20  = pBt->pPager;. 
1cda3 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65   int rc;..  asse
1cda4 72 74 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41  rt( eType==PTRMA
1cda5 50 5f 4f 56 45 52 46 4c 4f 57 32 20 7c 7c 20 65  P_OVERFLOW2 || e
1cda6 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45  Type==PTRMAP_OVE
1cda7 52 46 4c 4f 57 31 20 7c 7c 20 0a 20 20 20 20 20  RFLOW1 || .     
1cda8 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42   eType==PTRMAP_B
1cda9 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50  TREE || eType==P
1cdaa 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29  TRMAP_ROOTPAGE )
1cdab 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
1cdac 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1cdad 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
1cdae 61 73 73 65 72 74 28 20 70 44 62 50 61 67 65 2d  assert( pDbPage-
1cdaf 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a 0a 20 20  >pBt==pBt );..  
1cdb0 2f 2a 20 4d 6f 76 65 20 70 61 67 65 20 69 44 62  /* Move page iDb
1cdb1 50 61 67 65 20 66 72 6f 6d 20 69 74 73 20 63 75  Page from its cu
1cdb2 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 74  rrent location t
1cdb3 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 46  o page number iF
1cdb4 72 65 65 50 61 67 65 20 2a 2f 0a 20 20 54 52 41  reePage */.  TRA
1cdb5 43 45 28 28 22 41 55 54 4f 56 41 43 55 55 4d 3a  CE(("AUTOVACUUM:
1cdb6 20 4d 6f 76 69 6e 67 20 25 64 20 74 6f 20 66 72   Moving %d to fr
1cdb7 65 65 20 70 61 67 65 20 25 64 20 28 70 74 72 20  ee page %d (ptr 
1cdb8 70 61 67 65 20 25 64 20 74 79 70 65 20 25 64 29  page %d type %d)
1cdb9 5c 6e 22 2c 20 0a 20 20 20 20 20 20 69 44 62 50  \n", .      iDbP
1cdba 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20  age, iFreePage, 
1cdbb 69 50 74 72 50 61 67 65 2c 20 65 54 79 70 65 29  iPtrPage, eType)
1cdbc 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1cdbd 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 70  3PagerMovepage(p
1cdbe 50 61 67 65 72 2c 20 70 44 62 50 61 67 65 2d 3e  Pager, pDbPage->
1cdbf 70 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61  pDbPage, iFreePa
1cdc0 67 65 2c 20 69 73 43 6f 6d 6d 69 74 29 3b 0a 20  ge, isCommit);. 
1cdc1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1cdc2 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
1cdc3 20 72 63 3b 0a 20 20 7d 0a 20 20 70 44 62 50 61   rc;.  }.  pDbPa
1cdc4 67 65 2d 3e 70 67 6e 6f 20 3d 20 69 46 72 65 65  ge->pgno = iFree
1cdc5 50 61 67 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 70  Page;..  /* If p
1cdc6 44 62 50 61 67 65 20 77 61 73 20 61 20 62 74 72  DbPage was a btr
1cdc7 65 65 2d 70 61 67 65 2c 20 74 68 65 6e 20 69 74  ee-page, then it
1cdc8 20 6d 61 79 20 68 61 76 65 20 63 68 69 6c 64 20   may have child 
1cdc9 70 61 67 65 73 20 61 6e 64 2f 6f 72 20 63 65 6c  pages and/or cel
1cdca 6c 73 0a 20 20 2a 2a 20 74 68 61 74 20 70 6f 69  ls.  ** that poi
1cdcb 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70  nt to overflow p
1cdcc 61 67 65 73 2e 20 54 68 65 20 70 6f 69 6e 74 65  ages. The pointe
1cdcd 72 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f  r map entries fo
1cdce 72 20 61 6c 6c 20 74 68 65 73 65 0a 20 20 2a 2a  r all these.  **
1cdcf 20 70 61 67 65 73 20 6e 65 65 64 20 74 6f 20 62   pages need to b
1cdd0 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a  e changed..  **.
1cdd1 20 20 2a 2a 20 49 66 20 70 44 62 50 61 67 65 20    ** If pDbPage 
1cdd2 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  is an overflow p
1cdd3 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 66 69  age, then the fi
1cdd4 72 73 74 20 34 20 62 79 74 65 73 20 6d 61 79 20  rst 4 bytes may 
1cdd5 73 74 6f 72 65 20 61 0a 20 20 2a 2a 20 70 6f 69  store a.  ** poi
1cdd6 6e 74 65 72 20 74 6f 20 61 20 73 75 62 73 65 71  nter to a subseq
1cdd7 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61  uent overflow pa
1cdd8 67 65 2e 20 49 66 20 74 68 69 73 20 69 73 20 74  ge. If this is t
1cdd9 68 65 20 63 61 73 65 2c 20 74 68 65 6e 0a 20 20  he case, then.  
1cdda 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  ** the pointer m
1cddb 61 70 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75  ap needs to be u
1cddc 70 64 61 74 65 64 20 66 6f 72 20 74 68 65 20 73  pdated for the s
1cddd 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c  ubsequent overfl
1cdde 6f 77 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20  ow page..  */.  
1cddf 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41  if( eType==PTRMA
1cde0 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65  P_BTREE || eType
1cde1 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  ==PTRMAP_ROOTPAG
1cde2 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 65  E ){.    rc = se
1cde3 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 70 44  tChildPtrmaps(pD
1cde4 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  bPage);.    if( 
1cde5 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1cde6 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
1cde7 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
1cde8 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 4f 76  .    Pgno nextOv
1cde9 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 70 44  fl = get4byte(pD
1cdea 62 50 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20  bPage->aData);. 
1cdeb 20 20 20 69 66 28 20 6e 65 78 74 4f 76 66 6c 21     if( nextOvfl!
1cdec 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d  =0 ){.      ptrm
1cded 61 70 50 75 74 28 70 42 74 2c 20 6e 65 78 74 4f  apPut(pBt, nextO
1cdee 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52  vfl, PTRMAP_OVER
1cdef 46 4c 4f 57 32 2c 20 69 46 72 65 65 50 61 67 65  FLOW2, iFreePage
1cdf0 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 69 66  , &rc);.      if
1cdf1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1cdf2 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
1cdf3 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
1cdf4 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69    }.  }..  /* Fi
1cdf5 78 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70  x the database p
1cdf6 6f 69 6e 74 65 72 20 6f 6e 20 70 61 67 65 20 69  ointer on page i
1cdf7 50 74 72 50 61 67 65 20 74 68 61 74 20 70 6f 69  PtrPage that poi
1cdf8 6e 74 65 64 20 61 74 20 69 44 62 50 61 67 65 20  nted at iDbPage 
1cdf9 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 69 74 20  so.  ** that it 
1cdfa 70 6f 69 6e 74 73 20 61 74 20 69 46 72 65 65 50  points at iFreeP
1cdfb 61 67 65 2e 20 41 6c 73 6f 20 66 69 78 20 74 68  age. Also fix th
1cdfc 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e  e pointer map en
1cdfd 74 72 79 20 66 6f 72 0a 20 20 2a 2a 20 69 50 74  try for.  ** iPt
1cdfe 72 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  rPage..  */.  if
1cdff 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f  ( eType!=PTRMAP_
1ce00 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20  ROOTPAGE ){.    
1ce01 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
1ce02 65 28 70 42 74 2c 20 69 50 74 72 50 61 67 65 2c  e(pBt, iPtrPage,
1ce03 20 26 70 50 74 72 50 61 67 65 2c 20 30 29 3b 0a   &pPtrPage, 0);.
1ce04 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1ce05 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
1ce06 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
1ce07 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1ce08 50 61 67 65 72 57 72 69 74 65 28 70 50 74 72 50  PagerWrite(pPtrP
1ce09 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
1ce0a 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1ce0b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
1ce0c 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50 61  leasePage(pPtrPa
1ce0d 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ge);.      retur
1ce0e 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
1ce0f 72 63 20 3d 20 6d 6f 64 69 66 79 50 61 67 65 50  rc = modifyPageP
1ce10 6f 69 6e 74 65 72 28 70 50 74 72 50 61 67 65 2c  ointer(pPtrPage,
1ce11 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 65 50   iDbPage, iFreeP
1ce12 61 67 65 2c 20 65 54 79 70 65 29 3b 0a 20 20 20  age, eType);.   
1ce13 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 74   releasePage(pPt
1ce14 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  rPage);.    if( 
1ce15 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1ce16 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74  .      ptrmapPut
1ce17 28 70 42 74 2c 20 69 46 72 65 65 50 61 67 65 2c  (pBt, iFreePage,
1ce18 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 67 65   eType, iPtrPage
1ce19 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20  , &rc);.    }.  
1ce1a 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1ce1b 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63  ../* Forward dec
1ce1c 6c 61 72 61 74 69 6f 6e 20 72 65 71 75 69 72 65  laration require
1ce1d 64 20 62 79 20 69 6e 63 72 56 61 63 75 75 6d 53  d by incrVacuumS
1ce1e 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63  tep(). */.static
1ce1f 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 72   int allocateBtr
1ce20 65 65 50 61 67 65 28 42 74 53 68 61 72 65 64 20  eePage(BtShared 
1ce21 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50  *, MemPage **, P
1ce22 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29  gno *, Pgno, u8)
1ce23 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d  ;../*.** Perform
1ce24 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f   a single step o
1ce25 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c  f an incremental
1ce26 2d 76 61 63 75 75 6d 2e 20 49 66 20 73 75 63 63  -vacuum. If succ
1ce27 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72  essful,.** retur
1ce28 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20  n SQLITE_OK. If 
1ce29 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b  there is no work
1ce2a 20 74 6f 20 64 6f 20 28 61 6e 64 20 74 68 65 72   to do (and ther
1ce2b 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e  efore no.** poin
1ce2c 74 20 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68 69  t in calling thi
1ce2d 73 20 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e  s function again
1ce2e 29 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ), return SQLITE
1ce2f 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72  _DONE..**.** Mor
1ce30 65 20 73 70 65 63 69 66 69 63 6c 79 2c 20 74 68  e specificly, th
1ce31 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65  is function atte
1ce32 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e  mpts to re-organ
1ce33 69 7a 65 20 74 68 65 20 0a 2a 2a 20 64 61 74 61  ize the .** data
1ce34 62 61 73 65 20 73 6f 20 74 68 61 74 20 74 68 65  base so that the
1ce35 20 6c 61 73 74 20 70 61 67 65 20 6f 66 20 74 68   last page of th
1ce36 65 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79  e file currently
1ce37 20 69 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e 6f   in use.** is no
1ce38 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a   longer in use..
1ce39 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46 69  **.** If the nFi
1ce3a 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  n parameter is n
1ce3b 6f 6e 2d 7a 65 72 6f 2c 20 74 68 69 73 20 66 75  on-zero, this fu
1ce3c 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 0a 2a  nction assumes.*
1ce3d 2a 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65  * that the calle
1ce3e 72 20 77 69 6c 6c 20 6b 65 65 70 20 63 61 6c 6c  r will keep call
1ce3f 69 6e 67 20 69 6e 63 72 56 61 63 75 75 6d 53 74  ing incrVacuumSt
1ce40 65 70 28 29 20 75 6e 74 69 6c 0a 2a 2a 20 69 74  ep() until.** it
1ce41 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
1ce42 44 4f 4e 45 20 6f 72 20 61 6e 20 65 72 72 6f 72  DONE or an error
1ce43 2c 20 61 6e 64 20 74 68 61 74 20 6e 46 69 6e 20  , and that nFin 
1ce44 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72  is the.** number
1ce45 20 6f 66 20 70 61 67 65 73 20 74 68 65 20 64 61   of pages the da
1ce46 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c  tabase file will
1ce47 20 63 6f 6e 74 61 69 6e 20 61 66 74 65 72 20 74   contain after t
1ce48 68 69 73 20 0a 2a 2a 20 70 72 6f 63 65 73 73 20  his .** process 
1ce49 69 73 20 63 6f 6d 70 6c 65 74 65 2e 20 20 49 66  is complete.  If
1ce4a 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 69   nFin is zero, i
1ce4b 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
1ce4c 74 0a 2a 2a 20 69 6e 63 72 56 61 63 75 75 6d 53  t.** incrVacuumS
1ce4d 74 65 70 28 29 20 77 69 6c 6c 20 62 65 20 63 61  tep() will be ca
1ce4e 6c 6c 65 64 20 61 20 66 69 6e 69 74 65 20 61 6d  lled a finite am
1ce4f 6f 75 6e 74 20 6f 66 20 74 69 6d 65 73 0a 2a 2a  ount of times.**
1ce50 20 77 68 69 63 68 20 6d 61 79 20 6f 72 20 6d 61   which may or ma
1ce51 79 20 6e 6f 74 20 65 6d 70 74 79 20 74 68 65 20  y not empty the 
1ce52 66 72 65 65 6c 69 73 74 2e 20 20 41 20 66 75 6c  freelist.  A ful
1ce53 6c 20 61 75 74 6f 76 61 63 75 75 6d 0a 2a 2a 20  l autovacuum.** 
1ce54 68 61 73 20 6e 46 69 6e 3e 30 2e 20 20 41 20 22  has nFin>0.  A "
1ce55 50 52 41 47 4d 41 20 69 6e 63 72 65 6d 65 6e 74  PRAGMA increment
1ce56 61 6c 5f 76 61 63 75 75 6d 22 20 68 61 73 20 6e  al_vacuum" has n
1ce57 46 69 6e 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 74 69  Fin==0..*/.stati
1ce58 63 20 69 6e 74 20 69 6e 63 72 56 61 63 75 75 6d  c int incrVacuum
1ce59 53 74 65 70 28 42 74 53 68 61 72 65 64 20 2a 70  Step(BtShared *p
1ce5a 42 74 2c 20 50 67 6e 6f 20 6e 46 69 6e 2c 20 50  Bt, Pgno nFin, P
1ce5b 67 6e 6f 20 69 4c 61 73 74 50 67 29 7b 0a 20 20  gno iLastPg){.  
1ce5c 50 67 6e 6f 20 6e 46 72 65 65 4c 69 73 74 3b 20  Pgno nFreeList; 
1ce5d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1ce5e 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 69  ber of pages sti
1ce5f 6c 6c 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c  ll on the free-l
1ce60 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ist */.  int rc;
1ce61 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1ce62 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1ce63 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
1ce64 61 73 73 65 72 74 28 20 69 4c 61 73 74 50 67 3e  assert( iLastPg>
1ce65 6e 46 69 6e 20 29 3b 0a 0a 20 20 69 66 28 20 21  nFin );..  if( !
1ce66 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42  PTRMAP_ISPAGE(pB
1ce67 74 2c 20 69 4c 61 73 74 50 67 29 20 26 26 20 69  t, iLastPg) && i
1ce68 4c 61 73 74 50 67 21 3d 50 45 4e 44 49 4e 47 5f  LastPg!=PENDING_
1ce69 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
1ce6a 7b 0a 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a  {.    u8 eType;.
1ce6b 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67      Pgno iPtrPag
1ce6c 65 3b 0a 0a 20 20 20 20 6e 46 72 65 65 4c 69 73  e;..    nFreeLis
1ce6d 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42  t = get4byte(&pB
1ce6e 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
1ce6f 5b 33 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e  [36]);.    if( n
1ce70 46 72 65 65 4c 69 73 74 3d 3d 30 20 29 7b 0a 20  FreeList==0 ){. 
1ce71 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1ce72 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a  TE_DONE;.    }..
1ce73 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47      rc = ptrmapG
1ce74 65 74 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c  et(pBt, iLastPg,
1ce75 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61   &eType, &iPtrPa
1ce76 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  ge);.    if( rc!
1ce77 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1ce78 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1ce79 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 54 79     }.    if( eTy
1ce7a 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50  pe==PTRMAP_ROOTP
1ce7b 41 47 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74  AGE ){.      ret
1ce7c 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
1ce7d 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a  PT_BKPT;.    }..
1ce7e 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50      if( eType==P
1ce7f 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29  TRMAP_FREEPAGE )
1ce80 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 46 69 6e  {.      if( nFin
1ce81 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
1ce82 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67  * Remove the pag
1ce83 65 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73  e from the files
1ce84 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73   free-list. This
1ce85 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
1ce86 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20 6e  .        ** if n
1ce87 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e  Fin is non-zero.
1ce88 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74   In that case, t
1ce89 68 65 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c  he free-list wil
1ce8a 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  l be.        ** 
1ce8b 74 72 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72  truncated to zer
1ce8c 6f 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e  o after this fun
1ce8d 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73  ction returns, s
1ce8e 6f 20 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20  o it doesn't .  
1ce8f 20 20 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20        ** matter 
1ce90 69 66 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74  if it still cont
1ce91 61 69 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67  ains some garbag
1ce92 65 20 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20  e entries..     
1ce93 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67     */.        Pg
1ce94 6e 6f 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20  no iFreePg;.    
1ce95 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72      MemPage *pFr
1ce96 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63  eePg;.        rc
1ce97 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65   = allocateBtree
1ce98 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 65  Page(pBt, &pFree
1ce99 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c  Pg, &iFreePg, iL
1ce9a 61 73 74 50 67 2c 20 31 29 3b 0a 20 20 20 20 20  astPg, 1);.     
1ce9b 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1ce9c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1ce9d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1ce9e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61       }.        a
1ce9f 73 73 65 72 74 28 20 69 46 72 65 65 50 67 3d 3d  ssert( iFreePg==
1cea0 69 4c 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20  iLastPg );.     
1cea1 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1cea2 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d  FreePg);.      }
1cea3 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  .    } else {.  
1cea4 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67      Pgno iFreePg
1cea5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1cea6 20 49 6e 64 65 78 20 6f 66 20 66 72 65 65 20 70   Index of free p
1cea7 61 67 65 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73  age to move pLas
1cea8 74 50 67 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20  tPg to */.      
1cea9 4d 65 6d 50 61 67 65 20 2a 70 4c 61 73 74 50 67  MemPage *pLastPg
1ceaa 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74  ;..      rc = bt
1ceab 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
1ceac 69 4c 61 73 74 50 67 2c 20 26 70 4c 61 73 74 50  iLastPg, &pLastP
1cead 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  g, 0);.      if(
1ceae 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1ceaf 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
1ceb0 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20   rc;.      }..  
1ceb1 20 20 20 20 2f 2a 20 49 66 20 6e 46 69 6e 20 69      /* If nFin i
1ceb2 73 20 7a 65 72 6f 2c 20 74 68 69 73 20 6c 6f 6f  s zero, this loo
1ceb3 70 20 72 75 6e 73 20 65 78 61 63 74 6c 79 20 6f  p runs exactly o
1ceb4 6e 63 65 20 61 6e 64 20 70 61 67 65 20 70 4c 61  nce and page pLa
1ceb5 73 74 50 67 0a 20 20 20 20 20 20 2a 2a 20 69 73  stPg.      ** is
1ceb6 20 73 77 61 70 70 65 64 20 77 69 74 68 20 74 68   swapped with th
1ceb7 65 20 66 69 72 73 74 20 66 72 65 65 20 70 61 67  e first free pag
1ceb8 65 20 70 75 6c 6c 65 64 20 6f 66 66 20 74 68 65  e pulled off the
1ceb9 20 66 72 65 65 20 6c 69 73 74 2e 0a 20 20 20 20   free list..    
1ceba 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f 6e    **.      ** On
1cebb 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c   the other hand,
1cebc 20 69 66 20 6e 46 69 6e 20 69 73 20 67 72 65 61   if nFin is grea
1cebd 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74  ter than zero, t
1cebe 68 65 6e 20 6b 65 65 70 0a 20 20 20 20 20 20 2a  hen keep.      *
1cebf 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20  * looping until 
1cec0 61 20 66 72 65 65 2d 70 61 67 65 20 6c 6f 63 61  a free-page loca
1cec1 74 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 66  ted within the f
1cec2 69 72 73 74 20 6e 46 69 6e 20 70 61 67 65 73 0a  irst nFin pages.
1cec3 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20        ** of the 
1cec4 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 2e 0a 20  file is found.. 
1cec5 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f       */.      do
1cec6 20 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61   {.        MemPa
1cec7 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20  ge *pFreePg;.   
1cec8 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61       rc = alloca
1cec9 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
1ceca 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65   &pFreePg, &iFre
1cecb 65 50 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ePg, 0, 0);.    
1cecc 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1cecd 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1cece 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1cecf 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 20  LastPg);.       
1ced0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1ced1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1ced2 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 65  releasePage(pFre
1ced3 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 69  ePg);.      }whi
1ced4 6c 65 28 20 6e 46 69 6e 21 3d 30 20 26 26 20 69  le( nFin!=0 && i
1ced5 46 72 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a 20  FreePg>nFin );. 
1ced6 20 20 20 20 20 61 73 73 65 72 74 28 20 69 46 72       assert( iFr
1ced7 65 65 50 67 3c 69 4c 61 73 74 50 67 20 29 3b 0a  eePg<iLastPg );.
1ced8 20 20 20 20 20 20 0a 20 20 20 20 20 20 72 63 20        .      rc 
1ced9 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
1ceda 69 74 65 28 70 4c 61 73 74 50 67 2d 3e 70 44 62  ite(pLastPg->pDb
1cedb 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Page);.      if(
1cedc 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1cedd 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72  {.        rc = r
1cede 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c  elocatePage(pBt,
1cedf 20 70 4c 61 73 74 50 67 2c 20 65 54 79 70 65 2c   pLastPg, eType,
1cee0 20 69 50 74 72 50 61 67 65 2c 20 69 46 72 65 65   iPtrPage, iFree
1cee1 50 67 2c 20 6e 46 69 6e 21 3d 30 29 3b 0a 20 20  Pg, nFin!=0);.  
1cee2 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65      }.      rele
1cee3 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 67 29  asePage(pLastPg)
1cee4 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
1cee5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1cee6 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1cee7 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1cee8 7d 0a 0a 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30  }..  if( nFin==0
1cee9 20 29 7b 0a 20 20 20 20 69 4c 61 73 74 50 67 2d   ){.    iLastPg-
1ceea 2d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c  -;.    while( iL
1ceeb 61 73 74 50 67 3d 3d 50 45 4e 44 49 4e 47 5f 42  astPg==PENDING_B
1ceec 59 54 45 5f 50 41 47 45 28 70 42 74 29 7c 7c 50  YTE_PAGE(pBt)||P
1ceed 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74  TRMAP_ISPAGE(pBt
1ceee 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20  , iLastPg) ){.  
1ceef 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49      if( PTRMAP_I
1cef0 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74  SPAGE(pBt, iLast
1cef1 50 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 4d  Pg) ){.        M
1cef2 65 6d 50 61 67 65 20 2a 70 50 67 3b 0a 20 20 20  emPage *pPg;.   
1cef3 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47       rc = btreeG
1cef4 65 74 50 61 67 65 28 70 42 74 2c 20 69 4c 61 73  etPage(pBt, iLas
1cef5 74 50 67 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20  tPg, &pPg, 0);. 
1cef6 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
1cef7 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1cef8 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
1cef9 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1cefa 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1cefb 61 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70  agerWrite(pPg->p
1cefc 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
1cefd 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 67   releasePage(pPg
1cefe 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
1ceff 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1cf00 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
1cf01 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   rc;.        }. 
1cf02 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 4c 61       }.      iLa
1cf03 73 74 50 67 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20  stPg--;.    }.  
1cf04 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72    sqlite3PagerTr
1cf05 75 6e 63 61 74 65 49 6d 61 67 65 28 70 42 74 2d  uncateImage(pBt-
1cf06 3e 70 50 61 67 65 72 2c 20 69 4c 61 73 74 50 67  >pPager, iLastPg
1cf07 29 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 50 61 67  );.    pBt->nPag
1cf08 65 20 3d 20 69 4c 61 73 74 50 67 3b 0a 20 20 7d  e = iLastPg;.  }
1cf09 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1cf0a 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20  _OK;.}../*.** A 
1cf0b 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
1cf0c 6e 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64  n must be opened
1cf0d 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20   before calling 
1cf0e 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  this function..*
1cf0f 2a 20 49 74 20 70 65 72 66 6f 72 6d 73 20 61 20  * It performs a 
1cf10 73 69 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20 77  single unit of w
1cf11 6f 72 6b 20 74 6f 77 61 72 64 73 20 61 6e 20 69  ork towards an i
1cf12 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75  ncremental vacuu
1cf13 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  m..**.** If the 
1cf14 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75  incremental vacu
1cf15 75 6d 20 69 73 20 66 69 6e 69 73 68 65 64 20 61  um is finished a
1cf16 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69  fter this functi
1cf17 6f 6e 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 53  on has run,.** S
1cf18 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65  QLITE_DONE is re
1cf19 74 75 72 6e 65 64 2e 20 49 66 20 69 74 20 69 73  turned. If it is
1cf1a 20 6e 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 62   not finished, b
1cf1b 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75  ut no error occu
1cf1c 72 72 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f  rred,.** SQLITE_
1cf1d 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  OK is returned. 
1cf1e 4f 74 68 65 72 77 69 73 65 20 61 6e 20 53 51 4c  Otherwise an SQL
1cf1f 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20  ite error code. 
1cf20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1cf21 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1cf22 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 42 74  reeIncrVacuum(Bt
1cf23 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  ree *p){.  int r
1cf24 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  c;.  BtShared *p
1cf25 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20  Bt = p->pBt;..  
1cf26 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1cf27 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r(p);.  assert( 
1cf28 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
1cf29 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  on==TRANS_WRITE 
1cf2a 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  && p->inTrans==T
1cf2b 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20  RANS_WRITE );.  
1cf2c 69 66 28 20 21 70 42 74 2d 3e 61 75 74 6f 56 61  if( !pBt->autoVa
1cf2d 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d  cuum ){.    rc =
1cf2e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
1cf2f 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c  }else{.    inval
1cf30 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77  idateAllOverflow
1cf31 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 20 20  Cache(pBt);.    
1cf32 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53  rc = incrVacuumS
1cf33 74 65 70 28 70 42 74 2c 20 30 2c 20 62 74 72 65  tep(pBt, 0, btre
1cf34 65 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 29  ePagecount(pBt))
1cf35 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1cf36 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1cf37 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1cf38 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 61  erWrite(pBt->pPa
1cf39 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  ge1->pDbPage);. 
1cf3a 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
1cf3b 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
1cf3c 61 5b 32 38 5d 2c 20 70 42 74 2d 3e 6e 50 61 67  a[28], pBt->nPag
1cf3d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
1cf3e 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1cf3f 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
1cf40 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
1cf41 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
1cf42 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71 6c 69  ed prior to sqli
1cf43 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 20 77  te3PagerCommit w
1cf44 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f  hen a transactio
1cf45 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 65 64  n.** is commited
1cf46 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76 61 63   for an auto-vac
1cf47 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a  uum database..**
1cf48 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b  .** If SQLITE_OK
1cf49 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68   is returned, th
1cf4a 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73 20 73  en *pnTrunc is s
1cf4b 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72  et to the number
1cf4c 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74 68 65   of pages.** the
1cf4d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 73   database file s
1cf4e 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61 74  hould be truncat
1cf4f 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74 68 65  ed to during the
1cf50 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e   commit process.
1cf51 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 64 61   .** i.e. the da
1cf52 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20  tabase has been 
1cf53 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f 20 74  reorganized so t
1cf54 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66 69 72  hat only the fir
1cf55 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a 20 70  st *pnTrunc.** p
1cf56 61 67 65 73 20 61 72 65 20 69 6e 20 75 73 65 2e  ages are in use.
1cf57 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
1cf58 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28  utoVacuumCommit(
1cf59 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
1cf5a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1cf5b 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70  E_OK;.  Pager *p
1cf5c 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61  Pager = pBt->pPa
1cf5d 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28  ger;.  VVA_ONLY(
1cf5e 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 71 6c 69   int nRef = sqli
1cf5f 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74  te3PagerRefcount
1cf60 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 20 61  (pPager) );..  a
1cf61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1cf62 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
1cf63 75 74 65 78 29 20 29 3b 0a 20 20 69 6e 76 61 6c  utex) );.  inval
1cf64 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77  idateAllOverflow
1cf65 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 61 73  Cache(pBt);.  as
1cf66 73 65 72 74 28 70 42 74 2d 3e 61 75 74 6f 56 61  sert(pBt->autoVa
1cf67 63 75 75 6d 29 3b 0a 20 20 69 66 28 20 21 70 42  cuum);.  if( !pB
1cf68 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 29 7b  t->incrVacuum ){
1cf69 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e 3b 20  .    Pgno nFin; 
1cf6a 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1cf6b 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 64 61  r of pages in da
1cf6c 74 61 62 61 73 65 20 61 66 74 65 72 20 61 75 74  tabase after aut
1cf6d 6f 76 61 63 75 75 6d 69 6e 67 20 2a 2f 0a 20 20  ovacuuming */.  
1cf6e 20 20 50 67 6e 6f 20 6e 46 72 65 65 3b 20 20 20    Pgno nFree;   
1cf6f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1cf70 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66  f pages on the f
1cf71 72 65 65 6c 69 73 74 20 69 6e 69 74 69 61 6c 6c  reelist initiall
1cf72 79 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 50  y */.    Pgno nP
1cf73 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 4e  trmap;      /* N
1cf74 75 6d 62 65 72 20 6f 66 20 50 74 72 4d 61 70 20  umber of PtrMap 
1cf75 70 61 67 65 73 20 74 6f 20 62 65 20 66 72 65 65  pages to be free
1cf76 64 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 69 46  d */.    Pgno iF
1cf77 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ree;        /* T
1cf78 68 65 20 6e 65 78 74 20 70 61 67 65 20 74 6f 20  he next page to 
1cf79 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20  be freed */.    
1cf7a 69 6e 74 20 6e 45 6e 74 72 79 3b 20 20 20 20 20  int nEntry;     
1cf7b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1cf7c 65 6e 74 72 69 65 73 20 6f 6e 20 6f 6e 65 20 70  entries on one p
1cf7d 74 72 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20  trmap page */.  
1cf7e 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20    Pgno nOrig;   
1cf7f 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
1cf80 20 73 69 7a 65 20 62 65 66 6f 72 65 20 66 72 65   size before fre
1cf81 65 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 6e 4f 72  eing */..    nOr
1cf82 69 67 20 3d 20 62 74 72 65 65 50 61 67 65 63 6f  ig = btreePageco
1cf83 75 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 69 66  unt(pBt);.    if
1cf84 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28  ( PTRMAP_ISPAGE(
1cf85 70 42 74 2c 20 6e 4f 72 69 67 29 20 7c 7c 20 6e  pBt, nOrig) || n
1cf86 4f 72 69 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59  Orig==PENDING_BY
1cf87 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a  TE_PAGE(pBt) ){.
1cf88 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e        /* It is n
1cf89 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63  ot possible to c
1cf8a 72 65 61 74 65 20 61 20 64 61 74 61 62 61 73 65  reate a database
1cf8b 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 66   for which the f
1cf8c 69 6e 61 6c 20 70 61 67 65 0a 20 20 20 20 20 20  inal page.      
1cf8d 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 70  ** is either a p
1cf8e 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20  ointer-map page 
1cf8f 6f 72 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 62  or the pending-b
1cf90 79 74 65 20 70 61 67 65 2e 20 49 66 20 6f 6e 65  yte page. If one
1cf91 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65 6e 63  .      ** is enc
1cf92 6f 75 6e 74 65 72 65 64 2c 20 74 68 69 73 20 69  ountered, this i
1cf93 6e 64 69 63 61 74 65 73 20 63 6f 72 72 75 70 74  ndicates corrupt
1cf94 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ion..      */.  
1cf95 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1cf96 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
1cf97 20 20 20 20 7d 0a 0a 20 20 20 20 6e 46 72 65 65      }..    nFree
1cf98 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 74   = get4byte(&pBt
1cf99 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  ->pPage1->aData[
1cf9a 33 36 5d 29 3b 0a 20 20 20 20 6e 45 6e 74 72 79  36]);.    nEntry
1cf9b 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   = pBt->usableSi
1cf9c 7a 65 2f 35 3b 0a 20 20 20 20 6e 50 74 72 6d 61  ze/5;.    nPtrma
1cf9d 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67  p = (nFree-nOrig
1cf9e 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70  +PTRMAP_PAGENO(p
1cf9f 42 74 2c 20 6e 4f 72 69 67 29 2b 6e 45 6e 74 72  Bt, nOrig)+nEntr
1cfa0 79 29 2f 6e 45 6e 74 72 79 3b 0a 20 20 20 20 6e  y)/nEntry;.    n
1cfa1 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20 6e 46  Fin = nOrig - nF
1cfa2 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b 0a 20  ree - nPtrmap;. 
1cfa3 20 20 20 69 66 28 20 6e 4f 72 69 67 3e 50 45 4e     if( nOrig>PEN
1cfa4 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
1cfa5 42 74 29 20 26 26 20 6e 46 69 6e 3c 50 45 4e 44  Bt) && nFin<PEND
1cfa6 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
1cfa7 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 69 6e  t) ){.      nFin
1cfa8 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68  --;.    }.    wh
1cfa9 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41  ile( PTRMAP_ISPA
1cfaa 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20 7c 7c  GE(pBt, nFin) ||
1cfab 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42   nFin==PENDING_B
1cfac 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b  YTE_PAGE(pBt) ){
1cfad 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20  .      nFin--;. 
1cfae 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 46 69     }.    if( nFi
1cfaf 6e 3e 6e 4f 72 69 67 20 29 20 72 65 74 75 72 6e  n>nOrig ) return
1cfb0 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1cfb1 42 4b 50 54 3b 0a 0a 20 20 20 20 66 6f 72 28 69  BKPT;..    for(i
1cfb2 46 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46 72 65  Free=nOrig; iFre
1cfb3 65 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d 53 51  e>nFin && rc==SQ
1cfb4 4c 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65 2d 2d  LITE_OK; iFree--
1cfb5 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 6e  ){.      rc = in
1cfb6 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 42 74  crVacuumStep(pBt
1cfb7 2c 20 6e 46 69 6e 2c 20 69 46 72 65 65 29 3b 0a  , nFin, iFree);.
1cfb8 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 72      }.    if( (r
1cfb9 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 7c  c==SQLITE_DONE |
1cfba 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29  | rc==SQLITE_OK)
1cfbb 20 26 26 20 6e 46 72 65 65 3e 30 20 29 7b 0a 20   && nFree>0 ){. 
1cfbc 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1cfbd 33 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d  3PagerWrite(pBt-
1cfbe 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65  >pPage1->pDbPage
1cfbf 29 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74  );.      put4byt
1cfc0 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e  e(&pBt->pPage1->
1cfc1 61 44 61 74 61 5b 33 32 5d 2c 20 30 29 3b 0a 20  aData[32], 0);. 
1cfc2 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
1cfc3 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
1cfc4 61 5b 33 36 5d 2c 20 30 29 3b 0a 20 20 20 20 20  a[36], 0);.     
1cfc5 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e   put4byte(&pBt->
1cfc6 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 32 38  pPage1->aData[28
1cfc7 5d 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20 20 20  ], nFin);.      
1cfc8 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e  sqlite3PagerTrun
1cfc9 63 61 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70  cateImage(pBt->p
1cfca 50 61 67 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20  Pager, nFin);.  
1cfcb 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65 20 3d      pBt->nPage =
1cfcc 20 6e 46 69 6e 3b 0a 20 20 20 20 7d 0a 20 20 20   nFin;.    }.   
1cfcd 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1cfce 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
1cfcf 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
1cfd0 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a  (pPager);.    }.
1cfd1 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e    }..  assert( n
1cfd2 52 65 66 3d 3d 73 71 6c 69 74 65 33 50 61 67 65  Ref==sqlite3Page
1cfd3 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72  rRefcount(pPager
1cfd4 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ) );.  return rc
1cfd5 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66  ;.}..#else /* if
1cfd6 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1cfd7 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 23  _AUTOVACUUM */.#
1cfd8 20 64 65 66 69 6e 65 20 73 65 74 43 68 69 6c 64   define setChild
1cfd9 50 74 72 6d 61 70 73 28 78 29 20 53 51 4c 49 54  Ptrmaps(x) SQLIT
1cfda 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  E_OK.#endif../*.
1cfdb 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1cfdc 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70  does the first p
1cfdd 68 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 68  hase of a two-ph
1cfde 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69  ase commit.  Thi
1cfdf 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75  s routine.** cau
1cfe0 73 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a  ses a rollback j
1cfe1 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 65  ournal to be cre
1cfe2 61 74 65 64 20 28 69 66 20 69 74 20 64 6f 65 73  ated (if it does
1cfe3 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69   not already exi
1cfe4 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c  st).** and popul
1cfe5 61 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67 68  ated with enough
1cfe6 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20   information so 
1cfe7 74 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 20  that if a power 
1cfe8 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74  loss occurs.** t
1cfe9 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 20  he database can 
1cfea 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69  be restored to i
1cfeb 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74  ts original stat
1cfec 65 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61 63  e by playing bac
1cfed 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c  k.** the journal
1cfee 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e 74  .  Then the cont
1cfef 65 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 72  ents of the jour
1cff0 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64 20  nal are flushed 
1cff1 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69  out to.** the di
1cff2 73 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20 6a  sk.  After the j
1cff3 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c 79  ournal is safely
1cff4 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 63   on oxide, the c
1cff5 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a  hanges to the.**
1cff6 20 64 61 74 61 62 61 73 65 20 61 72 65 20 77 72   database are wr
1cff7 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 64  itten into the d
1cff8 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
1cff9 20 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 64   flushed to oxid
1cffa 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64  e..** At the end
1cffb 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 74   of this call, t
1cffc 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
1cffd 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 73  nal still exists
1cffe 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20   on the.** disk 
1cfff 61 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c 6c  and we are still
1d000 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63   holding all loc
1d001 6b 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e 73  ks, so the trans
1d002 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a  action has not.*
1d003 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65  * committed.  Se
1d004 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  e sqlite3BtreeCo
1d005 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 66  mmitPhaseTwo() f
1d006 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68  or the second ph
1d007 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f  ase of the.** co
1d008 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a  mmit process..**
1d009 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73  .** This call is
1d00a 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77   a no-op if no w
1d00b 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
1d00c 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 63   is currently ac
1d00d 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a  tive on pBt..**.
1d00e 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 79  ** Otherwise, sy
1d00f 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20  nc the database 
1d010 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74 72  file for the btr
1d011 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 20  ee pBt. zMaster 
1d012 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65  points to.** the
1d013 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65   name of a maste
1d014 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74  r journal file t
1d015 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72  hat should be wr
1d016 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a  itten into the.*
1d017 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75  * individual jou
1d018 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73  rnal file, or is
1d019 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e   NULL, indicatin
1d01a 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72  g no master jour
1d01b 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69  nal file .** (si
1d01c 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72  ngle database tr
1d01d 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a  ansaction)..**.*
1d01e 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63  * When this is c
1d01f 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 65  alled, the maste
1d020 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64  r journal should
1d021 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62 65   already have be
1d022 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 70  en.** created, p
1d023 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68  opulated with th
1d024 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74  is journal point
1d025 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f  er and synced to
1d026 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63   disk..**.** Onc
1d027 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69 6e  e this is routin
1d028 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c 20  e has returned, 
1d029 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72  the only thing r
1d02a 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69  equired to commi
1d02b 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d 74  t.** the write-t
1d02c 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 74  ransaction for t
1d02d 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c  his database fil
1d02e 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74  e is to delete t
1d02f 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 53  he journal..*/.S
1d030 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1d031 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
1d032 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72  mmitPhaseOne(Btr
1d033 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61  ee *p, const cha
1d034 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69  r *zMaster){.  i
1d035 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1d036 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72  K;.  if( p->inTr
1d037 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
1d038 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64   ){.    BtShared
1d039 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
1d03a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1d03b 45 6e 74 65 72 28 70 29 3b 0a 23 69 66 6e 64 65  Enter(p);.#ifnde
1d03c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1d03d 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
1d03e 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
1d03f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61   ){.      rc = a
1d040 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28  utoVacuumCommit(
1d041 70 42 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  pBt);.      if( 
1d042 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1d043 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1d044 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
1d045 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1d046 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1d047 23 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20  #endif.    rc = 
1d048 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
1d049 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2d 3e  itPhaseOne(pBt->
1d04a 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 2c  pPager, zMaster,
1d04b 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
1d04c 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
1d04d 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1d04e 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
1d04f 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
1d050 20 66 72 6f 6d 20 62 6f 74 68 20 42 74 72 65 65   from both Btree
1d051 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29  CommitPhaseTwo()
1d052 20 61 6e 64 20 42 74 72 65 65 52 6f 6c 6c 62 61   and BtreeRollba
1d053 63 6b 28 29 0a 2a 2a 20 61 74 20 74 68 65 20 63  ck().** at the c
1d054 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 61 20 74  onclusion of a t
1d055 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  ransaction..*/.s
1d056 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65  tatic void btree
1d057 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 42  EndTransaction(B
1d058 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68  tree *p){.  BtSh
1d059 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
1d05a 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  Bt;.  assert( sq
1d05b 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
1d05c 75 74 65 78 28 70 29 20 29 3b 0a 0a 20 20 62 74  utex(p) );..  bt
1d05d 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65  reeClearHasConte
1d05e 6e 74 28 70 42 74 29 3b 0a 20 20 69 66 28 20 70  nt(pBt);.  if( p
1d05f 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f  ->inTrans>TRANS_
1d060 4e 4f 4e 45 20 26 26 20 70 2d 3e 64 62 2d 3e 61  NONE && p->db->a
1d061 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29  ctiveVdbeCnt>1 )
1d062 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72  {.    /* If ther
1d063 65 20 61 72 65 20 6f 74 68 65 72 20 61 63 74 69  e are other acti
1d064 76 65 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68  ve statements th
1d065 61 74 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 69  at belong to thi
1d066 73 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a  s database.    *
1d067 2a 20 68 61 6e 64 6c 65 2c 20 64 6f 77 6e 67 72  * handle, downgr
1d068 61 64 65 20 74 6f 20 61 20 72 65 61 64 2d 6f 6e  ade to a read-on
1d069 6c 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ly transaction. 
1d06a 54 68 65 20 6f 74 68 65 72 20 73 74 61 74 65 6d  The other statem
1d06b 65 6e 74 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20  ents.    ** may 
1d06c 73 74 69 6c 6c 20 62 65 20 72 65 61 64 69 6e 67  still be reading
1d06d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
1d06e 73 65 2e 20 20 2a 2f 0a 20 20 20 20 64 6f 77 6e  se.  */.    down
1d06f 67 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61  gradeAllSharedCa
1d070 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29  cheTableLocks(p)
1d071 3b 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73  ;.    p->inTrans
1d072 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20   = TRANS_READ;. 
1d073 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49   }else{.    /* I
1d074 66 20 74 68 65 20 68 61 6e 64 6c 65 20 68 61 64  f the handle had
1d075 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 74 72 61   any kind of tra
1d076 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 64  nsaction open, d
1d077 65 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 20  ecrement the .  
1d078 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
1d079 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 68   count of the sh
1d07a 61 72 65 64 20 62 74 72 65 65 2e 20 49 66 20 74  ared btree. If t
1d07b 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63  he transaction c
1d07c 6f 75 6e 74 20 0a 20 20 20 20 2a 2a 20 72 65 61  ount .    ** rea
1d07d 63 68 65 73 20 30 2c 20 73 65 74 20 74 68 65 20  ches 0, set the 
1d07e 73 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20  shared state to 
1d07f 54 52 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20  TRANS_NONE. The 
1d080 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75  unlockBtreeIfUnu
1d081 73 65 64 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c  sed().    ** cal
1d082 6c 20 62 65 6c 6f 77 20 77 69 6c 6c 20 75 6e 6c  l below will unl
1d083 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2e 20 20  ock the pager.  
1d084 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e  */.    if( p->in
1d085 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e  Trans!=TRANS_NON
1d086 45 20 29 7b 0a 20 20 20 20 20 20 63 6c 65 61 72  E ){.      clear
1d087 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61  AllSharedCacheTa
1d088 62 6c 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20 20  bleLocks(p);.   
1d089 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63     pBt->nTransac
1d08a 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 20 20 69 66  tion--;.      if
1d08b 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73  ( 0==pBt->nTrans
1d08c 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20  action ){.      
1d08d 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63    pBt->inTransac
1d08e 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e  tion = TRANS_NON
1d08f 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  E;.      }.    }
1d090 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65  ..    /* Set the
1d091 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
1d092 74 69 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 52  tion state to TR
1d093 41 4e 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c  ANS_NONE and unl
1d094 6f 63 6b 20 74 68 65 20 0a 20 20 20 20 2a 2a 20  ock the .    ** 
1d095 70 61 67 65 72 20 69 66 20 74 68 69 73 20 63 61  pager if this ca
1d096 6c 6c 20 63 6c 6f 73 65 64 20 74 68 65 20 6f 6e  ll closed the on
1d097 6c 79 20 72 65 61 64 20 6f 72 20 77 72 69 74 65  ly read or write
1d098 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 2a   transaction.  *
1d099 2f 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73  /.    p->inTrans
1d09a 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20   = TRANS_NONE;. 
1d09b 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66     unlockBtreeIf
1d09c 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 7d  Unused(pBt);.  }
1d09d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69  ..  btreeIntegri
1d09e 74 79 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ty(p);.}../*.** 
1d09f 43 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73  Commit the trans
1d0a0 61 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79  action currently
1d0a1 20 69 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a   in progress..**
1d0a2 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1d0a3 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
1d0a4 73 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20  second phase of 
1d0a5 61 20 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74  a 2-phase commit
1d0a6 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65  .  The.** sqlite
1d0a7 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
1d0a8 65 4f 6e 65 28 29 20 72 6f 75 74 69 6e 65 20 64  eOne() routine d
1d0a9 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70 68  oes the first ph
1d0aa 61 73 65 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a  ase and should.*
1d0ab 2a 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 69  * be invoked pri
1d0ac 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68  or to calling th
1d0ad 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 65  is routine.  The
1d0ae 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
1d0af 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 0a 2a 2a  mitPhaseOne().**
1d0b0 20 72 6f 75 74 69 6e 65 20 64 69 64 20 61 6c 6c   routine did all
1d0b1 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 77 72 69   the work of wri
1d0b2 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ting information
1d0b3 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61 6e 64   out to disk and
1d0b4 20 66 6c 75 73 68 69 6e 67 20 74 68 65 0a 2a 2a   flushing the.**
1d0b5 20 63 6f 6e 74 65 6e 74 73 20 73 6f 20 74 68 61   contents so tha
1d0b6 74 20 74 68 65 79 20 61 72 65 20 77 72 69 74 74  t they are writt
1d0b7 65 6e 20 6f 6e 74 6f 20 74 68 65 20 64 69 73 6b  en onto the disk
1d0b8 20 70 6c 61 74 74 65 72 2e 20 20 41 6c 6c 20 74   platter.  All t
1d0b9 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 68  his.** routine h
1d0ba 61 73 20 74 6f 20 64 6f 20 69 73 20 64 65 6c 65  as to do is dele
1d0bb 74 65 20 6f 72 20 74 72 75 6e 63 61 74 65 20 6f  te or truncate o
1d0bc 72 20 7a 65 72 6f 20 74 68 65 20 68 65 61 64 65  r zero the heade
1d0bd 72 20 69 6e 20 74 68 65 0a 2a 2a 20 74 68 65 20  r in the.** the 
1d0be 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
1d0bf 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20 74   (which causes t
1d0c0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74  he transaction t
1d0c1 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 0a 2a 2a  o commit) and.**
1d0c2 20 64 72 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a   drop locks..**.
1d0c3 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 66 20  ** Normally, if 
1d0c4 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
1d0c5 77 68 69 6c 65 20 74 68 65 20 70 61 67 65 72 20  while the pager 
1d0c6 6c 61 79 65 72 20 69 73 20 61 74 74 65 6d 70 74  layer is attempt
1d0c7 69 6e 67 20 74 6f 20 0a 2a 2a 20 66 69 6e 61 6c  ing to .** final
1d0c8 69 7a 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69  ize the underlyi
1d0c9 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c  ng journal file,
1d0ca 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
1d0cb 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20  eturns an error 
1d0cc 61 6e 64 0a 2a 2a 20 74 68 65 20 75 70 70 65 72  and.** the upper
1d0cd 20 6c 61 79 65 72 20 77 69 6c 6c 20 61 74 74 65   layer will atte
1d0ce 6d 70 74 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 20  mpt a rollback. 
1d0cf 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65 20  However, if the 
1d0d0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a  second argument.
1d0d1 2a 2a 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74  ** is non-zero t
1d0d2 68 65 6e 20 74 68 69 73 20 62 2d 74 72 65 65 20  hen this b-tree 
1d0d3 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 70  transaction is p
1d0d4 61 72 74 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66  art of a multi-f
1d0d5 69 6c 65 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74  ile .** transact
1d0d6 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ion. In this cas
1d0d7 65 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  e, the transacti
1d0d8 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  on has already b
1d0d9 65 65 6e 20 63 6f 6d 6d 69 74 74 65 64 20 0a 2a  een committed .*
1d0da 2a 20 28 62 79 20 64 65 6c 65 74 69 6e 67 20 61  * (by deleting a
1d0db 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1d0dc 66 69 6c 65 29 20 61 6e 64 20 74 68 65 20 63 61  file) and the ca
1d0dd 6c 6c 65 72 20 77 69 6c 6c 20 69 67 6e 6f 72 65  ller will ignore
1d0de 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69   this .** functi
1d0df 6f 6e 73 20 72 65 74 75 72 6e 20 63 6f 64 65 2e  ons return code.
1d0e0 20 53 6f 2c 20 65 76 65 6e 20 69 66 20 61 6e 20   So, even if an 
1d0e1 65 72 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20  error occurs in 
1d0e2 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 2c  the pager layer,
1d0e3 0a 2a 2a 20 72 65 73 65 74 20 74 68 65 20 62 2d  .** reset the b-
1d0e4 74 72 65 65 20 6f 62 6a 65 63 74 73 20 69 6e 74  tree objects int
1d0e5 65 72 6e 61 6c 20 73 74 61 74 65 20 74 6f 20 69  ernal state to i
1d0e6 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65  ndicate that the
1d0e7 20 77 72 69 74 65 0a 2a 2a 20 74 72 61 6e 73 61   write.** transa
1d0e8 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 63  ction has been c
1d0e9 6c 6f 73 65 64 2e 20 54 68 69 73 20 69 73 20 71  losed. This is q
1d0ea 75 69 74 65 20 73 61 66 65 2c 20 61 73 20 74 68  uite safe, as th
1d0eb 65 20 70 61 67 65 72 20 77 69 6c 6c 20 68 61 76  e pager will hav
1d0ec 65 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 65  e.** transitione
1d0ed 64 20 74 6f 20 74 68 65 20 65 72 72 6f 72 20 73  d to the error s
1d0ee 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tate..**.** This
1d0ef 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68   will release th
1d0f0 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20  e write lock on 
1d0f1 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1d0f2 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20  e.  If there.** 
1d0f3 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75  are no active cu
1d0f4 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72  rsors, it also r
1d0f5 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61 64  eleases the read
1d0f6 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45   lock..*/.SQLITE
1d0f7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1d0f8 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
1d0f9 68 61 73 65 54 77 6f 28 42 74 72 65 65 20 2a 70  haseTwo(Btree *p
1d0fa 2c 20 69 6e 74 20 62 43 6c 65 61 6e 75 70 29 7b  , int bCleanup){
1d0fb 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61  ..  if( p->inTra
1d0fc 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29  ns==TRANS_NONE )
1d0fd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1d0fe 4b 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  K;.  sqlite3Btre
1d0ff 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 62 74 72  eEnter(p);.  btr
1d100 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a  eeIntegrity(p);.
1d101 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e  .  /* If the han
1d102 64 6c 65 20 68 61 73 20 61 20 77 72 69 74 65 2d  dle has a write-
1d103 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e  transaction open
1d104 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 73 68 61  , commit the sha
1d105 72 65 64 2d 62 74 72 65 65 73 20 0a 20 20 2a 2a  red-btrees .  **
1d106 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64   transaction and
1d107 20 73 65 74 20 74 68 65 20 73 68 61 72 65 64 20   set the shared 
1d108 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 52  state to TRANS_R
1d109 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  EAD..  */.  if( 
1d10a 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
1d10b 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69  S_WRITE ){.    i
1d10c 6e 74 20 72 63 3b 0a 20 20 20 20 42 74 53 68 61  nt rc;.    BtSha
1d10d 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
1d10e 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  t;.    assert( p
1d10f 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
1d110 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  n==TRANS_WRITE )
1d111 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  ;.    assert( pB
1d112 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e  t->nTransaction>
1d113 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  0 );.    rc = sq
1d114 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
1d115 50 68 61 73 65 54 77 6f 28 70 42 74 2d 3e 70 50  PhaseTwo(pBt->pP
1d116 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72  ager);.    if( r
1d117 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c!=SQLITE_OK && 
1d118 62 43 6c 65 61 6e 75 70 3d 3d 30 20 29 7b 0a 20  bCleanup==0 ){. 
1d119 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1d11a 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 20  eLeave(p);.     
1d11b 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
1d11c 7d 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61  }.    pBt->inTra
1d11d 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53  nsaction = TRANS
1d11e 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 74  _READ;.  }..  bt
1d11f 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69 6f  reeEndTransactio
1d120 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  n(p);.  sqlite3B
1d121 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
1d122 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1d123 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f  ;.}../*.** Do bo
1d124 74 68 20 70 68 61 73 65 73 20 6f 66 20 61 20 63  th phases of a c
1d125 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ommit..*/.SQLITE
1d126 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1d127 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28  ite3BtreeCommit(
1d128 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74  Btree *p){.  int
1d129 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74   rc;.  sqlite3Bt
1d12a 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72  reeEnter(p);.  r
1d12b 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1d12c 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70  CommitPhaseOne(p
1d12d 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , 0);.  if( rc==
1d12e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1d12f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1d130 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  eeCommitPhaseTwo
1d131 28 70 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 73 71  (p, 0);.  }.  sq
1d132 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
1d133 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
1d134 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  .}..#ifndef NDEB
1d135 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  UG./*.** Return 
1d136 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 72  the number of wr
1d137 69 74 65 2d 63 75 72 73 6f 72 73 20 6f 70 65 6e  ite-cursors open
1d138 20 6f 6e 20 74 68 69 73 20 68 61 6e 64 6c 65 2e   on this handle.
1d139 20 54 68 69 73 20 69 73 20 66 6f 72 20 75 73 65   This is for use
1d13a 0a 2a 2a 20 69 6e 20 61 73 73 65 72 74 28 29 20  .** in assert() 
1d13b 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 73 6f 20  expressions, so 
1d13c 69 74 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69  it is only compi
1d13d 6c 65 64 20 69 66 20 4e 44 45 42 55 47 20 69 73  led if NDEBUG is
1d13e 20 6e 6f 74 0a 2a 2a 20 64 65 66 69 6e 65 64 2e   not.** defined.
1d13f 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70  .**.** For the p
1d140 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20  urposes of this 
1d141 72 6f 75 74 69 6e 65 2c 20 61 20 77 72 69 74 65  routine, a write
1d142 2d 63 75 72 73 6f 72 20 69 73 20 61 6e 79 20 63  -cursor is any c
1d143 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 69 73  ursor that.** is
1d144 20 63 61 70 61 62 6c 65 20 6f 66 20 77 72 69 74   capable of writ
1d145 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62  ing to the datab
1d146 73 65 2e 20 20 54 68 61 74 20 6d 65 61 6e 73 20  se.  That means 
1d147 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 0a 2a  the cursor was.*
1d148 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 6f 70 65  * originally ope
1d149 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 20  ned for writing 
1d14a 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 68  and the cursor h
1d14b 61 73 20 6e 6f 74 20 62 65 20 64 69 73 61 62 6c  as not be disabl
1d14c 65 64 0a 2a 2a 20 62 79 20 68 61 76 69 6e 67 20  ed.** by having 
1d14d 69 74 73 20 73 74 61 74 65 20 63 68 61 6e 67 65  its state change
1d14e 64 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c  d to CURSOR_FAUL
1d14f 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  T..*/.static int
1d150 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73 6f   countWriteCurso
1d151 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  rs(BtShared *pBt
1d152 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  ){.  BtCursor *p
1d153 43 75 72 3b 0a 20 20 69 6e 74 20 72 20 3d 20 30  Cur;.  int r = 0
1d154 3b 0a 20 20 66 6f 72 28 70 43 75 72 3d 70 42 74  ;.  for(pCur=pBt
1d155 2d 3e 70 43 75 72 73 6f 72 3b 20 70 43 75 72 3b  ->pCursor; pCur;
1d156 20 70 43 75 72 3d 70 43 75 72 2d 3e 70 4e 65 78   pCur=pCur->pNex
1d157 74 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72  t){.    if( pCur
1d158 2d 3e 77 72 46 6c 61 67 20 26 26 20 70 43 75 72  ->wrFlag && pCur
1d159 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52  ->eState!=CURSOR
1d15a 5f 46 41 55 4c 54 20 29 20 72 2b 2b 3b 20 0a 20  _FAULT ) r++; . 
1d15b 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d   }.  return r;.}
1d15c 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
1d15d 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
1d15e 20 74 68 65 20 73 74 61 74 65 20 74 6f 20 43 55   the state to CU
1d15f 52 53 4f 52 5f 46 41 55 4c 54 20 61 6e 64 20 74  RSOR_FAULT and t
1d160 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65  he error.** code
1d161 20 74 6f 20 65 72 72 43 6f 64 65 20 66 6f 72 20   to errCode for 
1d162 65 76 65 72 79 20 63 75 72 73 6f 72 20 6f 6e 20  every cursor on 
1d163 42 74 53 68 61 72 65 64 20 74 68 61 74 20 70 42  BtShared that pB
1d164 74 72 65 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63  tree.** referenc
1d165 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20  es..**.** Every 
1d166 63 75 72 73 6f 72 20 69 73 20 74 72 69 70 70 65  cursor is trippe
1d167 64 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 75 72  d, including cur
1d168 73 6f 72 73 20 74 68 61 74 20 62 65 6c 6f 6e 67  sors that belong
1d169 0a 2a 2a 20 74 6f 20 6f 74 68 65 72 20 64 61 74  .** to other dat
1d16a 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1d16b 73 20 74 68 61 74 20 68 61 70 70 65 6e 20 74 6f  s that happen to
1d16c 20 62 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20 74   be sharing.** t
1d16d 68 65 20 63 61 63 68 65 20 77 69 74 68 20 70 42  he cache with pB
1d16e 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tree..**.** This
1d16f 20 72 6f 75 74 69 6e 65 20 67 65 74 73 20 63 61   routine gets ca
1d170 6c 6c 65 64 20 77 68 65 6e 20 61 20 72 6f 6c 6c  lled when a roll
1d171 62 61 63 6b 20 6f 63 63 75 72 73 2e 0a 2a 2a 20  back occurs..** 
1d172 41 6c 6c 20 63 75 72 73 6f 72 73 20 75 73 69 6e  All cursors usin
1d173 67 20 74 68 65 20 73 61 6d 65 20 63 61 63 68 65  g the same cache
1d174 20 6d 75 73 74 20 62 65 20 74 72 69 70 70 65 64   must be tripped
1d175 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e 74 20 74  .** to prevent t
1d176 68 65 6d 20 66 72 6f 6d 20 74 72 79 69 6e 67 20  hem from trying 
1d177 74 6f 20 75 73 65 20 74 68 65 20 62 74 72 65 65  to use the btree
1d178 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 72 6f   after.** the ro
1d179 6c 6c 62 61 63 6b 2e 20 20 54 68 65 20 72 6f 6c  llback.  The rol
1d17a 6c 62 61 63 6b 20 6d 61 79 20 68 61 76 65 20 64  lback may have d
1d17b 65 6c 65 74 65 64 20 74 61 62 6c 65 73 0a 2a 2a  eleted tables.**
1d17c 20 6f 72 20 6d 6f 76 65 64 20 72 6f 6f 74 20 70   or moved root p
1d17d 61 67 65 73 2c 20 73 6f 20 69 74 20 69 73 20 6e  ages, so it is n
1d17e 6f 74 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f  ot sufficient to
1d17f 0a 2a 2a 20 73 61 76 65 20 74 68 65 20 73 74 61  .** save the sta
1d180 74 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72  te of the cursor
1d181 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75  .  The cursor mu
1d182 73 74 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64  st be.** invalid
1d183 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ated..*/.SQLITE_
1d184 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1d185 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c  ite3BtreeTripAll
1d186 43 75 72 73 6f 72 73 28 42 74 72 65 65 20 2a 70  Cursors(Btree *p
1d187 42 74 72 65 65 2c 20 69 6e 74 20 65 72 72 43 6f  Btree, int errCo
1d188 64 65 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20  de){.  BtCursor 
1d189 2a 70 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65  *p;.  if( pBtree
1d18a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
1d18b 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1d18c 72 28 70 42 74 72 65 65 29 3b 0a 20 20 66 6f 72  r(pBtree);.  for
1d18d 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e  (p=pBtree->pBt->
1d18e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d  pCursor; p; p=p-
1d18f 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74  >pNext){.    int
1d190 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   i;.    sqlite3B
1d191 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28  treeClearCursor(
1d192 70 29 3b 0a 20 20 20 20 70 2d 3e 65 53 74 61 74  p);.    p->eStat
1d193 65 20 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c 54  e = CURSOR_FAULT
1d194 3b 0a 20 20 20 20 70 2d 3e 73 6b 69 70 4e 65 78  ;.    p->skipNex
1d195 74 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 20  t = errCode;.   
1d196 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d 3e   for(i=0; i<=p->
1d197 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  iPage; i++){.   
1d198 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1d199 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20  ->apPage[i]);.  
1d19a 20 20 20 20 70 2d 3e 61 70 50 61 67 65 5b 69 5d      p->apPage[i]
1d19b 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
1d19c 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1d19d 61 76 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a 0a  ave(pBtree);.}..
1d19e 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 74  /*.** Rollback t
1d19f 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
1d1a0 6e 20 70 72 6f 67 72 65 73 73 2e 20 20 41 6c 6c  n progress.  All
1d1a1 20 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65   cursors will be
1d1a2 0a 2a 2a 20 69 6e 76 61 6c 69 64 65 64 20 62 79  .** invalided by
1d1a3 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e   this operation.
1d1a4 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f    Any attempt to
1d1a5 20 75 73 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a   use a cursor.**
1d1a6 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20 61   that was open a
1d1a7 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
1d1a8 6f 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f  of this operatio
1d1a9 6e 20 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a 2a  n will result.**
1d1aa 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a   in an error..**
1d1ab 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65  .** This will re
1d1ac 6c 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20  lease the write 
1d1ad 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
1d1ae 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74  base file.  If t
1d1af 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61  here.** are no a
1d1b0 63 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69  ctive cursors, i
1d1b1 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20  t also releases 
1d1b2 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a  the read lock..*
1d1b3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1d1b4 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1d1b5 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 20  eRollback(Btree 
1d1b6 2a 70 2c 20 69 6e 74 20 74 72 69 70 43 6f 64 65  *p, int tripCode
1d1b7 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  ){.  int rc;.  B
1d1b8 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
1d1b9 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65  ->pBt;.  MemPage
1d1ba 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 73 71 6c   *pPage1;..  sql
1d1bb 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
1d1bc 29 3b 0a 20 20 69 66 28 20 74 72 69 70 43 6f 64  );.  if( tripCod
1d1bd 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  e==SQLITE_OK ){.
1d1be 20 20 20 20 72 63 20 3d 20 74 72 69 70 43 6f 64      rc = tripCod
1d1bf 65 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f  e = saveAllCurso
1d1c0 72 73 28 70 42 74 2c 20 30 2c 20 30 29 3b 0a 20  rs(pBt, 0, 0);. 
1d1c1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
1d1c2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
1d1c3 20 20 69 66 28 20 74 72 69 70 43 6f 64 65 20 29    if( tripCode )
1d1c4 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  {.    sqlite3Btr
1d1c5 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73  eeTripAllCursors
1d1c6 28 70 2c 20 74 72 69 70 43 6f 64 65 29 3b 0a 20  (p, tripCode);. 
1d1c7 20 7d 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72   }.  btreeIntegr
1d1c8 69 74 79 28 70 29 3b 0a 0a 20 20 69 66 28 20 70  ity(p);..  if( p
1d1c9 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
1d1ca 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 69 6e  _WRITE ){.    in
1d1cb 74 20 72 63 32 3b 0a 0a 20 20 20 20 61 73 73 65  t rc2;..    asse
1d1cc 72 74 28 20 54 52 41 4e 53 5f 57 52 49 54 45 3d  rt( TRANS_WRITE=
1d1cd 3d 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74  =pBt->inTransact
1d1ce 69 6f 6e 20 29 3b 0a 20 20 20 20 72 63 32 20 3d  ion );.    rc2 =
1d1cf 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c   sqlite3PagerRol
1d1d0 6c 62 61 63 6b 28 70 42 74 2d 3e 70 50 61 67 65  lback(pBt->pPage
1d1d1 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 32 21  r);.    if( rc2!
1d1d2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1d1d3 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20      rc = rc2;.  
1d1d4 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20    }..    /* The 
1d1d5 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 76  rollback may hav
1d1d6 65 20 64 65 73 74 72 6f 79 65 64 20 74 68 65 20  e destroyed the 
1d1d7 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 76 61  pPage1->aData va
1d1d8 6c 75 65 2e 20 20 53 6f 0a 20 20 20 20 2a 2a 20  lue.  So.    ** 
1d1d9 63 61 6c 6c 20 62 74 72 65 65 47 65 74 50 61 67  call btreeGetPag
1d1da 65 28 29 20 6f 6e 20 70 61 67 65 20 31 20 61 67  e() on page 1 ag
1d1db 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20 20 20 20  ain to make.    
1d1dc 2a 2a 20 73 75 72 65 20 70 50 61 67 65 31 2d 3e  ** sure pPage1->
1d1dd 61 44 61 74 61 20 69 73 20 73 65 74 20 63 6f 72  aData is set cor
1d1de 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 69  rectly. */.    i
1d1df 66 28 20 62 74 72 65 65 47 65 74 50 61 67 65 28  f( btreeGetPage(
1d1e0 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c  pBt, 1, &pPage1,
1d1e1 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   0)==SQLITE_OK )
1d1e2 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67  {.      int nPag
1d1e3 65 20 3d 20 67 65 74 34 62 79 74 65 28 32 38 2b  e = get4byte(28+
1d1e4 28 75 38 2a 29 70 50 61 67 65 31 2d 3e 61 44 61  (u8*)pPage1->aDa
1d1e5 74 61 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  ta);.      testc
1d1e6 61 73 65 28 20 6e 50 61 67 65 3d 3d 30 20 29 3b  ase( nPage==0 );
1d1e7 0a 20 20 20 20 20 20 69 66 28 20 6e 50 61 67 65  .      if( nPage
1d1e8 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 50 61 67  ==0 ) sqlite3Pag
1d1e9 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d  erPagecount(pBt-
1d1ea 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29  >pPager, &nPage)
1d1eb 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1d1ec 28 20 70 42 74 2d 3e 6e 50 61 67 65 21 3d 6e 50  ( pBt->nPage!=nP
1d1ed 61 67 65 20 29 3b 0a 20 20 20 20 20 20 70 42 74  age );.      pBt
1d1ee 2d 3e 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 3b  ->nPage = nPage;
1d1ef 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
1d1f0 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20 20  ge(pPage1);.    
1d1f1 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 6f  }.    assert( co
1d1f2 75 6e 74 57 72 69 74 65 43 75 72 73 6f 72 73 28  untWriteCursors(
1d1f3 70 42 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70  pBt)==0 );.    p
1d1f4 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
1d1f5 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a  n = TRANS_READ;.
1d1f6 20 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e 64 54    }..  btreeEndT
1d1f7 72 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b 0a 20  ransaction(p);. 
1d1f8 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
1d1f9 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
1d1fa 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61  rc;.}../*.** Sta
1d1fb 72 74 20 61 20 73 74 61 74 65 6d 65 6e 74 20 73  rt a statement s
1d1fc 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54  ubtransaction. T
1d1fd 68 65 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f  he subtransactio
1d1fe 6e 20 63 61 6e 20 63 61 6e 20 62 65 20 72 6f 6c  n can can be rol
1d1ff 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 69 6e 64 65  led.** back inde
1d200 70 65 6e 64 65 6e 74 6c 79 20 6f 66 20 74 68 65  pendently of the
1d201 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69 6f   main transactio
1d202 6e 2e 20 59 6f 75 20 6d 75 73 74 20 73 74 61 72  n. You must star
1d203 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  t a transaction 
1d204 0a 2a 2a 20 62 65 66 6f 72 65 20 73 74 61 72 74  .** before start
1d205 69 6e 67 20 61 20 73 75 62 74 72 61 6e 73 61 63  ing a subtransac
1d206 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72 61  tion. The subtra
1d207 6e 73 61 63 74 69 6f 6e 20 69 73 20 65 6e 64 65  nsaction is ende
1d208 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  d automatically 
1d209 0a 2a 2a 20 69 66 20 74 68 65 20 6d 61 69 6e 20  .** if the main 
1d20a 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d  transaction comm
1d20b 69 74 73 20 6f 72 20 72 6f 6c 6c 73 20 62 61 63  its or rolls bac
1d20c 6b 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65 6d 65  k..**.** Stateme
1d20d 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f  nt subtransactio
1d20e 6e 73 20 61 72 65 20 75 73 65 64 20 61 72 6f 75  ns are used arou
1d20f 6e 64 20 69 6e 64 69 76 69 64 75 61 6c 20 53 51  nd individual SQ
1d210 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20  L statements.** 
1d211 74 68 61 74 20 61 72 65 20 63 6f 6e 74 61 69 6e  that are contain
1d212 65 64 20 77 69 74 68 69 6e 20 61 20 42 45 47 49  ed within a BEGI
1d213 4e 2e 2e 2e 43 4f 4d 4d 49 54 20 62 6c 6f 63 6b  N...COMMIT block
1d214 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72 61 69  .  If a constrai
1d215 6e 74 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 63 75  nt.** error occu
1d216 72 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 74  rs within the st
1d217 61 74 65 6d 65 6e 74 2c 20 74 68 65 20 65 66 66  atement, the eff
1d218 65 63 74 20 6f 66 20 74 68 61 74 20 6f 6e 65 20  ect of that one 
1d219 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e  statement.** can
1d21a 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20   be rolled back 
1d21b 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74  without having t
1d21c 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 65  o rollback the e
1d21d 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f  ntire transactio
1d21e 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 74 61 74 65  n..**.** A state
1d21f 6d 65 6e 74 20 73 75 62 2d 74 72 61 6e 73 61 63  ment sub-transac
1d220 74 69 6f 6e 20 69 73 20 69 6d 70 6c 65 6d 65 6e  tion is implemen
1d221 74 65 64 20 61 73 20 61 6e 20 61 6e 6f 6e 79 6d  ted as an anonym
1d222 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2e 20 54  ous savepoint. T
1d223 68 65 0a 2a 2a 20 76 61 6c 75 65 20 70 61 73 73  he.** value pass
1d224 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  ed as the second
1d225 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
1d226 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
1d227 66 20 73 61 76 65 70 6f 69 6e 74 73 2c 0a 2a 2a  f savepoints,.**
1d228 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 6e   including the n
1d229 65 77 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76  ew anonymous sav
1d22a 65 70 6f 69 6e 74 2c 20 6f 70 65 6e 20 6f 6e 20  epoint, open on 
1d22b 74 68 65 20 42 2d 54 72 65 65 2e 20 69 2e 65 2e  the B-Tree. i.e.
1d22c 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65   if there.** are
1d22d 20 6e 6f 20 61 63 74 69 76 65 20 73 61 76 65 70   no active savep
1d22e 6f 69 6e 74 73 20 61 6e 64 20 6e 6f 20 6f 74 68  oints and no oth
1d22f 65 72 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61  er statement-tra
1d230 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e 2c 0a  nsactions open,.
1d231 2a 2a 20 69 53 74 61 74 65 6d 65 6e 74 20 69 73  ** iStatement is
1d232 20 31 2e 20 54 68 69 73 20 61 6e 6f 6e 79 6d 6f   1. This anonymo
1d233 75 73 20 73 61 76 65 70 6f 69 6e 74 20 63 61 6e  us savepoint can
1d234 20 62 65 20 72 65 6c 65 61 73 65 64 20 6f 72 20   be released or 
1d235 72 6f 6c 6c 65 64 20 62 61 63 6b 0a 2a 2a 20 75  rolled back.** u
1d236 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33  sing the sqlite3
1d237 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 29  BtreeSavepoint()
1d238 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51   function..*/.SQ
1d239 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1d23a 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
1d23b 69 6e 53 74 6d 74 28 42 74 72 65 65 20 2a 70 2c  inStmt(Btree *p,
1d23c 20 69 6e 74 20 69 53 74 61 74 65 6d 65 6e 74 29   int iStatement)
1d23d 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74  {.  int rc;.  Bt
1d23e 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
1d23f 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42  >pBt;.  sqlite3B
1d240 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
1d241 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61  assert( p->inTra
1d242 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  ns==TRANS_WRITE 
1d243 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 42  );.  assert( (pB
1d244 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54  t->btsFlags & BT
1d245 53 5f 52 45 41 44 5f 4f 4e 4c 59 29 3d 3d 30 20  S_READ_ONLY)==0 
1d246 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74  );.  assert( iSt
1d247 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a 20 20 61  atement>0 );.  a
1d248 73 73 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e  ssert( iStatemen
1d249 74 3e 70 2d 3e 64 62 2d 3e 6e 53 61 76 65 70 6f  t>p->db->nSavepo
1d24a 69 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  int );.  assert(
1d24b 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
1d24c 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
1d24d 20 29 3b 0a 20 20 2f 2a 20 41 74 20 74 68 65 20   );.  /* At the 
1d24e 70 61 67 65 72 20 6c 65 76 65 6c 2c 20 61 20 73  pager level, a s
1d24f 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
1d250 74 69 6f 6e 20 69 73 20 61 20 73 61 76 65 70 6f  tion is a savepo
1d251 69 6e 74 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e  int with.  ** an
1d252 20 69 6e 64 65 78 20 67 72 65 61 74 65 72 20 74   index greater t
1d253 68 61 6e 20 61 6c 6c 20 73 61 76 65 70 6f 69 6e  han all savepoin
1d254 74 73 20 63 72 65 61 74 65 64 20 65 78 70 6c 69  ts created expli
1d255 63 69 74 6c 79 20 75 73 69 6e 67 0a 20 20 2a 2a  citly using.  **
1d256 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e   SQL statements.
1d257 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74   It is illegal t
1d258 6f 20 6f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20  o open, release 
1d259 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 0a  or rollback any.
1d25a 20 20 2a 2a 20 73 75 63 68 20 73 61 76 65 70 6f    ** such savepo
1d25b 69 6e 74 73 20 77 68 69 6c 65 20 74 68 65 20 73  ints while the s
1d25c 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
1d25d 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69  tion savepoint i
1d25e 73 20 61 63 74 69 76 65 2e 0a 20 20 2a 2f 0a 20  s active..  */. 
1d25f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1d260 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28  erOpenSavepoint(
1d261 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 53 74  pBt->pPager, iSt
1d262 61 74 65 6d 65 6e 74 29 3b 0a 20 20 73 71 6c 69  atement);.  sqli
1d263 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
1d264 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1d265 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ../*.** The seco
1d266 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  nd argument to t
1d267 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 70  his function, op
1d268 2c 20 69 73 20 61 6c 77 61 79 73 20 53 41 56 45  , is always SAVE
1d269 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 0a 2a  POINT_ROLLBACK.*
1d26a 2a 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52  * or SAVEPOINT_R
1d26b 45 4c 45 41 53 45 2e 20 54 68 69 73 20 66 75 6e  ELEASE. This fun
1d26c 63 74 69 6f 6e 20 65 69 74 68 65 72 20 72 65 6c  ction either rel
1d26d 65 61 73 65 73 20 6f 72 20 72 6f 6c 6c 73 20 62  eases or rolls b
1d26e 61 63 6b 20 74 68 65 0a 2a 2a 20 73 61 76 65 70  ack the.** savep
1d26f 6f 69 6e 74 20 69 64 65 6e 74 69 66 69 65 64 20  oint identified 
1d270 62 79 20 70 61 72 61 6d 65 74 65 72 20 69 53 61  by parameter iSa
1d271 76 65 70 6f 69 6e 74 2c 20 64 65 70 65 6e 64 69  vepoint, dependi
1d272 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20  ng on the value 
1d273 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a 2a 0a 2a 2a  .** of op..**.**
1d274 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 53 61 76 65   Normally, iSave
1d275 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 72  point is greater
1d276 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
1d277 6f 20 7a 65 72 6f 2e 20 48 6f 77 65 76 65 72 2c  o zero. However,
1d278 20 69 66 20 6f 70 20 69 73 0a 2a 2a 20 53 41 56   if op is.** SAV
1d279 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c  EPOINT_ROLLBACK,
1d27a 20 74 68 65 6e 20 69 53 61 76 65 70 6f 69 6e 74   then iSavepoint
1d27b 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 2d 31 2e   may also be -1.
1d27c 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
1d27d 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f  e .** contents o
1d27e 66 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61  f the entire tra
1d27f 6e 73 61 63 74 69 6f 6e 20 61 72 65 20 72 6f 6c  nsaction are rol
1d280 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20 69  led back. This i
1d281 73 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66  s different.** f
1d282 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c 20 74 72 61  rom a normal tra
1d283 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 62 61 63  nsaction rollbac
1d284 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63 6b 73 20 61  k, as no locks a
1d285 72 65 20 72 65 6c 65 61 73 65 64 20 61 6e 64 20  re released and 
1d286 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69  the.** transacti
1d287 6f 6e 20 72 65 6d 61 69 6e 73 20 6f 70 65 6e 2e  on remains open.
1d288 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1d289 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1d28a 72 65 65 53 61 76 65 70 6f 69 6e 74 28 42 74 72  reeSavepoint(Btr
1d28b 65 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69  ee *p, int op, i
1d28c 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a  nt iSavepoint){.
1d28d 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1d28e 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 26 26  E_OK;.  if( p &&
1d28f 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41   p->inTrans==TRA
1d290 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20  NS_WRITE ){.    
1d291 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
1d292 70 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 65  p->pBt;.    asse
1d293 72 74 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e  rt( op==SAVEPOIN
1d294 54 5f 52 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d  T_RELEASE || op=
1d295 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42  =SAVEPOINT_ROLLB
1d296 41 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ACK );.    asser
1d297 74 28 20 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30  t( iSavepoint>=0
1d298 20 7c 7c 20 28 69 53 61 76 65 70 6f 69 6e 74 3d   || (iSavepoint=
1d299 3d 2d 31 20 26 26 20 6f 70 3d 3d 53 41 56 45 50  =-1 && op==SAVEP
1d29a 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 20 29  OINT_ROLLBACK) )
1d29b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1d29c 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20  eeEnter(p);.    
1d29d 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1d29e 72 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e  rSavepoint(pBt->
1d29f 70 50 61 67 65 72 2c 20 6f 70 2c 20 69 53 61 76  pPager, op, iSav
1d2a0 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 69 66 28  epoint);.    if(
1d2a1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1d2a2 7b 0a 20 20 20 20 20 20 69 66 28 20 69 53 61 76  {.      if( iSav
1d2a3 65 70 6f 69 6e 74 3c 30 20 26 26 20 28 70 42 74  epoint<0 && (pBt
1d2a4 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54 53  ->btsFlags & BTS
1d2a5 5f 49 4e 49 54 49 41 4c 4c 59 5f 45 4d 50 54 59  _INITIALLY_EMPTY
1d2a6 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
1d2a7 70 42 74 2d 3e 6e 50 61 67 65 20 3d 20 30 3b 0a  pBt->nPage = 0;.
1d2a8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
1d2a9 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28 70   = newDatabase(p
1d2aa 42 74 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e  Bt);.      pBt->
1d2ab 6e 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65  nPage = get4byte
1d2ac 28 32 38 20 2b 20 70 42 74 2d 3e 70 50 61 67 65  (28 + pBt->pPage
1d2ad 31 2d 3e 61 44 61 74 61 29 3b 0a 0a 20 20 20 20  1->aData);..    
1d2ae 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
1d2af 65 20 73 69 7a 65 20 77 61 73 20 77 72 69 74 74  e size was writt
1d2b0 65 6e 20 69 6e 74 6f 20 74 68 65 20 6f 66 66 73  en into the offs
1d2b1 65 74 20 32 38 20 6f 66 20 74 68 65 20 68 65 61  et 28 of the hea
1d2b2 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 77 68 65  der.      ** whe
1d2b3 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  n the transactio
1d2b4 6e 20 73 74 61 72 74 65 64 2c 20 73 6f 20 77 65  n started, so we
1d2b5 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 76   know that the v
1d2b6 61 6c 75 65 20 61 74 20 6f 66 66 73 65 74 0a 20  alue at offset. 
1d2b7 20 20 20 20 20 2a 2a 20 32 38 20 69 73 20 6e 6f       ** 28 is no
1d2b8 6e 7a 65 72 6f 2e 20 2a 2f 0a 20 20 20 20 20 20  nzero. */.      
1d2b9 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 50 61  assert( pBt->nPa
1d2ba 67 65 3e 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20  ge>0 );.    }.  
1d2bb 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1d2bc 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65  ave(p);.  }.  re
1d2bd 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1d2be 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 63  * Create a new c
1d2bf 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 42 54  ursor for the BT
1d2c0 72 65 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 69  ree whose root i
1d2c1 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a  s on the page.**
1d2c2 20 69 54 61 62 6c 65 2e 20 49 66 20 61 20 72 65   iTable. If a re
1d2c3 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 20 69  ad-only cursor i
1d2c4 73 20 72 65 71 75 65 73 74 65 64 2c 20 69 74 20  s requested, it 
1d2c5 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a  is assumed that.
1d2c6 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 61 6c  ** the caller al
1d2c7 72 65 61 64 79 20 68 61 73 20 61 74 20 6c 65 61  ready has at lea
1d2c8 73 74 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74  st a read-only t
1d2c9 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 0a  ransaction open.
1d2ca 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ** on the databa
1d2cb 73 65 20 61 6c 72 65 61 64 79 2e 20 49 66 20 61  se already. If a
1d2cc 20 77 72 69 74 65 2d 63 75 72 73 6f 72 20 69 73   write-cursor is
1d2cd 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 6e   requested, then
1d2ce 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 69  .** the caller i
1d2cf 73 20 61 73 73 75 6d 65 64 20 74 6f 20 68 61 76  s assumed to hav
1d2d0 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 20  e an open write 
1d2d1 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a  transaction..**.
1d2d2 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d 30 2c  ** If wrFlag==0,
1d2d3 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72   then the cursor
1d2d4 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65   can only be use
1d2d5 64 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a  d for reading..*
1d2d6 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d 31 2c 20  * If wrFlag==1, 
1d2d7 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20  then the cursor 
1d2d8 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20  can be used for 
1d2d9 72 65 61 64 69 6e 67 20 6f 72 20 66 6f 72 0a 2a  reading or for.*
1d2da 2a 20 77 72 69 74 69 6e 67 20 69 66 20 6f 74 68  * writing if oth
1d2db 65 72 20 63 6f 6e 64 69 74 69 6f 6e 73 20 66 6f  er conditions fo
1d2dc 72 20 77 72 69 74 69 6e 67 20 61 72 65 20 61 6c  r writing are al
1d2dd 73 6f 20 6d 65 74 2e 20 20 54 68 65 73 65 0a 2a  so met.  These.*
1d2de 2a 20 61 72 65 20 74 68 65 20 63 6f 6e 64 69 74  * are the condit
1d2df 69 6f 6e 73 20 74 68 61 74 20 6d 75 73 74 20 62  ions that must b
1d2e0 65 20 6d 65 74 20 69 6e 20 6f 72 64 65 72 20 66  e met in order f
1d2e1 6f 72 20 77 72 69 74 69 6e 67 20 74 6f 0a 2a 2a  or writing to.**
1d2e2 20 62 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a   be allowed:.**.
1d2e3 2a 2a 20 31 3a 20 20 54 68 65 20 63 75 72 73 6f  ** 1:  The curso
1d2e4 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e  r must have been
1d2e5 20 6f 70 65 6e 65 64 20 77 69 74 68 20 77 72 46   opened with wrF
1d2e6 6c 61 67 3d 3d 31 0a 2a 2a 0a 2a 2a 20 32 3a 20  lag==1.**.** 2: 
1d2e7 20 4f 74 68 65 72 20 64 61 74 61 62 61 73 65 20   Other database 
1d2e8 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 74  connections that
1d2e9 20 73 68 61 72 65 20 74 68 65 20 73 61 6d 65 20   share the same 
1d2ea 70 61 67 65 72 20 63 61 63 68 65 0a 2a 2a 20 20  pager cache.**  
1d2eb 20 20 20 62 75 74 20 77 68 69 63 68 20 61 72 65     but which are
1d2ec 20 6e 6f 74 20 69 6e 20 74 68 65 20 52 45 41 44   not in the READ
1d2ed 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 73 74 61  _UNCOMMITTED sta
1d2ee 74 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 0a  te may not have.
1d2ef 2a 2a 20 20 20 20 20 63 75 72 73 6f 72 73 20 6f  **     cursors o
1d2f0 70 65 6e 20 77 69 74 68 20 77 72 46 6c 61 67 3d  pen with wrFlag=
1d2f1 3d 30 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74  =0 on the same t
1d2f2 61 62 6c 65 2e 20 20 4f 74 68 65 72 77 69 73 65  able.  Otherwise
1d2f3 0a 2a 2a 20 20 20 20 20 74 68 65 20 63 68 61 6e  .**     the chan
1d2f4 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 69 73  ges made by this
1d2f5 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 77 6f   write cursor wo
1d2f6 75 6c 64 20 62 65 20 76 69 73 69 62 6c 65 20 74  uld be visible t
1d2f7 6f 0a 2a 2a 20 20 20 20 20 74 68 65 20 72 65 61  o.**     the rea
1d2f8 64 20 63 75 72 73 6f 72 73 20 69 6e 20 74 68 65  d cursors in the
1d2f9 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20   other database 
1d2fa 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  connection..**.*
1d2fb 2a 20 33 3a 20 20 54 68 65 20 64 61 74 61 62 61  * 3:  The databa
1d2fc 73 65 20 6d 75 73 74 20 62 65 20 77 72 69 74 61  se must be writa
1d2fd 62 6c 65 20 28 6e 6f 74 20 6f 6e 20 72 65 61 64  ble (not on read
1d2fe 2d 6f 6e 6c 79 20 6d 65 64 69 61 29 0a 2a 2a 0a  -only media).**.
1d2ff 2a 2a 20 34 3a 20 20 54 68 65 72 65 20 6d 75 73  ** 4:  There mus
1d300 74 20 62 65 20 61 6e 20 61 63 74 69 76 65 20 74  t be an active t
1d301 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  ransaction..**.*
1d302 2a 20 4e 6f 20 63 68 65 63 6b 69 6e 67 20 69 73  * No checking is
1d303 20 64 6f 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75   done to make su
1d304 72 65 20 74 68 61 74 20 70 61 67 65 20 69 54 61  re that page iTa
1d305 62 6c 65 20 72 65 61 6c 6c 79 20 69 73 20 74 68  ble really is th
1d306 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f  e.** root page o
1d307 66 20 61 20 62 2d 74 72 65 65 2e 20 20 49 66 20  f a b-tree.  If 
1d308 69 74 20 69 73 20 6e 6f 74 2c 20 74 68 65 6e 20  it is not, then 
1d309 74 68 65 20 63 75 72 73 6f 72 20 61 63 71 75 69  the cursor acqui
1d30a 72 65 64 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20  red.** will not 
1d30b 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 2e 0a  work correctly..
1d30c 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75  **.** It is assu
1d30d 6d 65 64 20 74 68 61 74 20 74 68 65 20 73 71 6c  med that the sql
1d30e 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a  ite3BtreeCursorZ
1d30f 65 72 6f 28 29 20 68 61 73 20 62 65 65 6e 20 63  ero() has been c
1d310 61 6c 6c 65 64 0a 2a 2a 20 6f 6e 20 70 43 75 72  alled.** on pCur
1d311 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74   to initialize t
1d312 68 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20  he memory space 
1d313 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e  prior to invokin
1d314 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a  g this routine..
1d315 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74  */.static int bt
1d316 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72  reeCursor(.  Btr
1d317 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ee *p,          
1d318 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d319 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65      /* The btree
1d31a 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
1d31b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1d31c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d31d 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62  Root page of tab
1d31e 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20  le to open */.  
1d31f 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20  int wrFlag,     
1d320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d321 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77         /* 1 to w
1d322 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c  rite. 0 read-onl
1d323 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65  y */.  struct Ke
1d324 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c  yInfo *pKeyInfo,
1d325 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d326 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 63 6f   First arg to co
1d327 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
1d328 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20  n */.  BtCursor 
1d329 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20  *pCur           
1d32a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d32b 20 53 70 61 63 65 20 66 6f 72 20 6e 65 77 20 63   Space for new c
1d32c 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 42 74  ursor */.){.  Bt
1d32d 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
1d32e 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20  >pBt;           
1d32f 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 62       /* Shared b
1d330 2d 74 72 65 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  -tree handle */.
1d331 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1d332 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
1d333 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74  x(p) );.  assert
1d334 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 77  ( wrFlag==0 || w
1d335 72 46 6c 61 67 3d 3d 31 20 29 3b 0a 0a 20 20 2f  rFlag==1 );..  /
1d336 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1d337 61 73 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74  assert statement
1d338 73 20 76 65 72 69 66 79 20 74 68 61 74 20 69 66  s verify that if
1d339 20 74 68 69 73 20 69 73 20 61 20 73 68 61 72 61   this is a shara
1d33a 62 6c 65 20 0a 20 20 2a 2a 20 62 2d 74 72 65 65  ble .  ** b-tree
1d33b 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 63   database, the c
1d33c 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c  onnection is hol
1d33d 64 69 6e 67 20 74 68 65 20 72 65 71 75 69 72 65  ding the require
1d33e 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 2c 20 0a  d table locks, .
1d33f 20 20 2a 2a 20 61 6e 64 20 74 68 61 74 20 6e 6f    ** and that no
1d340 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f   other connectio
1d341 6e 20 68 61 73 20 61 6e 79 20 6f 70 65 6e 20 63  n has any open c
1d342 75 72 73 6f 72 20 74 68 61 74 20 63 6f 6e 66 6c  ursor that confl
1d343 69 63 74 73 20 77 69 74 68 20 0a 20 20 2a 2a 20  icts with .  ** 
1d344 74 68 69 73 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20  this lock.  */. 
1d345 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61 72   assert( hasShar
1d346 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
1d347 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 4b 65 79  (p, iTable, pKey
1d348 49 6e 66 6f 21 3d 30 2c 20 77 72 46 6c 61 67 2b  Info!=0, wrFlag+
1d349 31 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  1) );.  assert( 
1d34a 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 21 68 61  wrFlag==0 || !ha
1d34b 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70  sReadConflicts(p
1d34c 2c 20 69 54 61 62 6c 65 29 20 29 3b 0a 0a 20 20  , iTable) );..  
1d34d 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74  /* Assert that t
1d34e 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 70  he caller has op
1d34f 65 6e 65 64 20 74 68 65 20 72 65 71 75 69 72 65  ened the require
1d350 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 2a  d transaction. *
1d351 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  /.  assert( p->i
1d352 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e  nTrans>TRANS_NON
1d353 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77  E );.  assert( w
1d354 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 70 2d 3e 69  rFlag==0 || p->i
1d355 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52  nTrans==TRANS_WR
1d356 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ITE );.  assert(
1d357 20 70 42 74 2d 3e 70 50 61 67 65 31 20 26 26 20   pBt->pPage1 && 
1d358 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61  pBt->pPage1->aDa
1d359 74 61 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56  ta );..  if( NEV
1d35a 45 52 28 77 72 46 6c 61 67 20 26 26 20 28 70 42  ER(wrFlag && (pB
1d35b 74 2d 3e 62 74 73 46 6c 61 67 73 20 26 20 42 54  t->btsFlags & BT
1d35c 53 5f 52 45 41 44 5f 4f 4e 4c 59 29 21 3d 30 29  S_READ_ONLY)!=0)
1d35d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
1d35e 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a  QLITE_READONLY;.
1d35f 20 20 7d 0a 20 20 69 66 28 20 69 54 61 62 6c 65    }.  if( iTable
1d360 3d 3d 31 20 26 26 20 62 74 72 65 65 50 61 67 65  ==1 && btreePage
1d361 63 6f 75 6e 74 28 70 42 74 29 3d 3d 30 20 29 7b  count(pBt)==0 ){
1d362 0a 20 20 20 20 61 73 73 65 72 74 28 20 77 72 46  .    assert( wrF
1d363 6c 61 67 3d 3d 30 20 29 3b 0a 20 20 20 20 69 54  lag==0 );.    iT
1d364 61 62 6c 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  able = 0;.  }.. 
1d365 20 2f 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f 20   /* Now that no 
1d366 6f 74 68 65 72 20 65 72 72 6f 72 73 20 63 61 6e  other errors can
1d367 20 6f 63 63 75 72 2c 20 66 69 6e 69 73 68 20 66   occur, finish f
1d368 69 6c 6c 69 6e 67 20 69 6e 20 74 68 65 20 42 74  illing in the Bt
1d369 43 75 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72 69  Cursor.  ** vari
1d36a 61 62 6c 65 73 20 61 6e 64 20 6c 69 6e 6b 20 74  ables and link t
1d36b 68 65 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 74  he cursor into t
1d36c 68 65 20 42 74 53 68 61 72 65 64 20 6c 69 73 74  he BtShared list
1d36d 2e 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 70 67  .  */.  pCur->pg
1d36e 6e 6f 52 6f 6f 74 20 3d 20 28 50 67 6e 6f 29 69  noRoot = (Pgno)i
1d36f 54 61 62 6c 65 3b 0a 20 20 70 43 75 72 2d 3e 69  Table;.  pCur->i
1d370 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 70 43 75  Page = -1;.  pCu
1d371 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b  r->pKeyInfo = pK
1d372 65 79 49 6e 66 6f 3b 0a 20 20 70 43 75 72 2d 3e  eyInfo;.  pCur->
1d373 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 70 43  pBtree = p;.  pC
1d374 75 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20  ur->pBt = pBt;. 
1d375 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 3d 20   pCur->wrFlag = 
1d376 28 75 38 29 77 72 46 6c 61 67 3b 0a 20 20 70 43  (u8)wrFlag;.  pC
1d377 75 72 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d  ur->pNext = pBt-
1d378 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20  >pCursor;.  if( 
1d379 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20  pCur->pNext ){. 
1d37a 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e     pCur->pNext->
1d37b 70 50 72 65 76 20 3d 20 70 43 75 72 3b 0a 20 20  pPrev = pCur;.  
1d37c 7d 0a 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72  }.  pBt->pCursor
1d37d 20 3d 20 70 43 75 72 3b 0a 20 20 70 43 75 72 2d   = pCur;.  pCur-
1d37e 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52  >eState = CURSOR
1d37f 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 70 43 75 72  _INVALID;.  pCur
1d380 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 20 3d 20  ->cachedRowid = 
1d381 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  0;.  return SQLI
1d382 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f  TE_OK;.}.SQLITE_
1d383 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1d384 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a  te3BtreeCursor(.
1d385 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20    Btree *p,     
1d386 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d387 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d388 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20   The btree */.  
1d389 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20  int iTable,     
1d38a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d38b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1d38c 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c  oot page of tabl
1d38d 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69  e to open */.  i
1d38e 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20  nt wrFlag,      
1d38f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d390 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20             /* 1 
1d391 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 64  to write. 0 read
1d392 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63  -only */.  struc
1d393 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49  t KeyInfo *pKeyI
1d394 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20  nfo,            
1d395 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
1d396 61 72 67 20 74 6f 20 78 43 6f 6d 70 61 72 65 28  arg to xCompare(
1d397 29 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20  ) */.  BtCursor 
1d398 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20  *pCur           
1d399 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d39a 20 20 20 2f 2a 20 57 72 69 74 65 20 6e 65 77 20     /* Write new 
1d39b 63 75 72 73 6f 72 20 68 65 72 65 20 2a 2f 0a 29  cursor here */.)
1d39c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
1d39d 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
1d39e 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65  p);.  rc = btree
1d39f 43 75 72 73 6f 72 28 70 2c 20 69 54 61 62 6c 65  Cursor(p, iTable
1d3a0 2c 20 77 72 46 6c 61 67 2c 20 70 4b 65 79 49 6e  , wrFlag, pKeyIn
1d3a1 66 6f 2c 20 70 43 75 72 29 3b 0a 20 20 73 71 6c  fo, pCur);.  sql
1d3a2 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1d3a3 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1d3a4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1d3a5 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42 74  the size of a Bt
1d3a6 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 6e  Cursor object in
1d3a7 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   bytes..**.** Th
1d3a8 69 73 20 69 6e 74 65 72 66 61 63 65 73 20 69 73  is interfaces is
1d3a9 20 6e 65 65 64 65 64 20 73 6f 20 74 68 61 74 20   needed so that 
1d3aa 75 73 65 72 73 20 6f 66 20 63 75 72 73 6f 72 73  users of cursors
1d3ab 20 63 61 6e 20 70 72 65 61 6c 6c 6f 63 61 74 65   can preallocate
1d3ac 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 73  .** sufficient s
1d3ad 74 6f 72 61 67 65 20 74 6f 20 68 6f 6c 64 20 61  torage to hold a
1d3ae 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 42 74   cursor.  The Bt
1d3af 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 73  Cursor object is
1d3b0 20 6f 70 61 71 75 65 0a 2a 2a 20 74 6f 20 75 73   opaque.** to us
1d3b1 65 72 73 20 73 6f 20 74 68 65 79 20 63 61 6e 6e  ers so they cann
1d3b2 6f 74 20 64 6f 20 74 68 65 20 73 69 7a 65 6f 66  ot do the sizeof
1d3b3 28 29 20 74 68 65 6d 73 65 6c 76 65 73 20 2d 20  () themselves - 
1d3b4 74 68 65 79 20 6d 75 73 74 20 63 61 6c 6c 0a 2a  they must call.*
1d3b5 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a  * this routine..
1d3b6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1d3b7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1d3b8 65 65 43 75 72 73 6f 72 53 69 7a 65 28 76 6f 69  eeCursorSize(voi
1d3b9 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 4f 55  d){.  return ROU
1d3ba 4e 44 38 28 73 69 7a 65 6f 66 28 42 74 43 75 72  ND8(sizeof(BtCur
1d3bb 73 6f 72 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  sor));.}../*.** 
1d3bc 49 6e 69 74 69 61 6c 69 7a 65 20 6d 65 6d 6f 72  Initialize memor
1d3bd 79 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 63  y that will be c
1d3be 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 20  onverted into a 
1d3bf 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e  BtCursor object.
1d3c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 6d 70 6c  .**.** The simpl
1d3c1 65 20 61 70 70 72 6f 61 63 68 20 68 65 72 65 20  e approach here 
1d3c2 77 6f 75 6c 64 20 62 65 20 74 6f 20 6d 65 6d 73  would be to mems
1d3c3 65 74 28 29 20 74 68 65 20 65 6e 74 69 72 65 20  et() the entire 
1d3c4 6f 62 6a 65 63 74 0a 2a 2a 20 74 6f 20 7a 65 72  object.** to zer
1d3c5 6f 2e 20 20 42 75 74 20 69 74 20 74 75 72 6e 73  o.  But it turns
1d3c6 20 6f 75 74 20 74 68 61 74 20 74 68 65 20 61 70   out that the ap
1d3c7 50 61 67 65 5b 5d 20 61 6e 64 20 61 69 49 64 78  Page[] and aiIdx
1d3c8 5b 5d 20 61 72 72 61 79 73 0a 2a 2a 20 64 6f 20  [] arrays.** do 
1d3c9 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 7a  not need to be z
1d3ca 65 72 6f 65 64 20 61 6e 64 20 74 68 65 79 20 61  eroed and they a
1d3cb 72 65 20 6c 61 72 67 65 2c 20 73 6f 20 77 65 20  re large, so we 
1d3cc 63 61 6e 20 73 61 76 65 20 61 20 6c 6f 74 0a 2a  can save a lot.*
1d3cd 2a 20 6f 66 20 72 75 6e 2d 74 69 6d 65 20 62 79  * of run-time by
1d3ce 20 73 6b 69 70 70 69 6e 67 20 74 68 65 20 69 6e   skipping the in
1d3cf 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20  itialization of 
1d3d0 74 68 6f 73 65 20 65 6c 65 6d 65 6e 74 73 2e 0a  those elements..
1d3d1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1d3d2 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
1d3d3 72 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 42 74  reeCursorZero(Bt
1d3d4 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 6d 65  Cursor *p){.  me
1d3d5 6d 73 65 74 28 70 2c 20 30 2c 20 6f 66 66 73 65  mset(p, 0, offse
1d3d6 74 6f 66 28 42 74 43 75 72 73 6f 72 2c 20 69 50  tof(BtCursor, iP
1d3d7 61 67 65 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  age));.}../*.** 
1d3d8 53 65 74 20 74 68 65 20 63 61 63 68 65 64 20 72  Set the cached r
1d3d9 6f 77 69 64 20 76 61 6c 75 65 20 6f 66 20 65 76  owid value of ev
1d3da 65 72 79 20 63 75 72 73 6f 72 20 69 6e 20 74 68  ery cursor in th
1d3db 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
1d3dc 66 69 6c 65 0a 2a 2a 20 61 73 20 70 43 75 72 20  file.** as pCur 
1d3dd 61 6e 64 20 68 61 76 69 6e 67 20 74 68 65 20 73  and having the s
1d3de 61 6d 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  ame root page nu
1d3df 6d 62 65 72 20 61 73 20 70 43 75 72 2e 20 20 54  mber as pCur.  T
1d3e0 68 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 73  he value is.** s
1d3e1 65 74 20 74 6f 20 69 52 6f 77 69 64 2e 0a 2a 2a  et to iRowid..**
1d3e2 0a 2a 2a 20 4f 6e 6c 79 20 70 6f 73 69 74 69 76  .** Only positiv
1d3e3 65 20 72 6f 77 69 64 20 76 61 6c 75 65 73 20 61  e rowid values a
1d3e4 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 76 61  re considered va
1d3e5 6c 69 64 20 66 6f 72 20 74 68 69 73 20 63 61 63  lid for this cac
1d3e6 68 65 2e 0a 2a 2a 20 54 68 65 20 63 61 63 68 65  he..** The cache
1d3e7 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   is initialized 
1d3e8 74 6f 20 7a 65 72 6f 2c 20 69 6e 64 69 63 61 74  to zero, indicat
1d3e9 69 6e 67 20 61 6e 20 69 6e 76 61 6c 69 64 20 63  ing an invalid c
1d3ea 61 63 68 65 2e 0a 2a 2a 20 41 20 62 74 72 65 65  ache..** A btree
1d3eb 20 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 20   will work fine 
1d3ec 77 69 74 68 20 7a 65 72 6f 20 6f 72 20 6e 65 67  with zero or neg
1d3ed 61 74 69 76 65 20 72 6f 77 69 64 73 2e 20 20 57  ative rowids.  W
1d3ee 65 20 6a 75 73 74 20 63 61 6e 6e 6f 74 0a 2a 2a  e just cannot.**
1d3ef 20 63 61 63 68 65 20 7a 65 72 6f 20 6f 72 20 6e   cache zero or n
1d3f0 65 67 61 74 69 76 65 20 72 6f 77 69 64 73 2c 20  egative rowids, 
1d3f1 77 68 69 63 68 20 6d 65 61 6e 73 20 74 61 62 6c  which means tabl
1d3f2 65 73 20 74 68 61 74 20 75 73 65 20 7a 65 72 6f  es that use zero
1d3f3 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20   or.** negative 
1d3f4 72 6f 77 69 64 73 20 6d 69 67 68 74 20 72 75 6e  rowids might run
1d3f5 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72   a little slower
1d3f6 2e 20 20 42 75 74 20 69 6e 20 70 72 61 63 74 69  .  But in practi
1d3f7 63 65 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 6e  ce, zero.** or n
1d3f8 65 67 61 74 69 76 65 20 72 6f 77 69 64 73 20 61  egative rowids a
1d3f9 72 65 20 76 65 72 79 20 75 6e 63 6f 6d 6d 6f 6e  re very uncommon
1d3fa 20 73 6f 20 74 68 69 73 20 73 68 6f 75 6c 64 20   so this should 
1d3fb 6e 6f 74 20 62 65 20 61 20 70 72 6f 62 6c 65 6d  not be a problem
1d3fc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1d3fd 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1d3fe 42 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f  BtreeSetCachedRo
1d3ff 77 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43  wid(BtCursor *pC
1d400 75 72 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ur, sqlite3_int6
1d401 34 20 69 52 6f 77 69 64 29 7b 0a 20 20 42 74 43  4 iRowid){.  BtC
1d402 75 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 28  ursor *p;.  for(
1d403 70 3d 70 43 75 72 2d 3e 70 42 74 2d 3e 70 43 75  p=pCur->pBt->pCu
1d404 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  rsor; p; p=p->pN
1d405 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  ext){.    if( p-
1d406 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 70 43 75 72 2d  >pgnoRoot==pCur-
1d407 3e 70 67 6e 6f 52 6f 6f 74 20 29 20 70 2d 3e 63  >pgnoRoot ) p->c
1d408 61 63 68 65 64 52 6f 77 69 64 20 3d 20 69 52 6f  achedRowid = iRo
1d409 77 69 64 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  wid;.  }.  asser
1d40a 74 28 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52  t( pCur->cachedR
1d40b 6f 77 69 64 3d 3d 69 52 6f 77 69 64 20 29 3b 0a  owid==iRowid );.
1d40c 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1d40d 74 68 65 20 63 61 63 68 65 64 20 72 6f 77 69 64  the cached rowid
1d40e 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 63   for the given c
1d40f 75 72 73 6f 72 2e 20 20 41 20 6e 65 67 61 74 69  ursor.  A negati
1d410 76 65 20 6f 72 20 7a 65 72 6f 0a 2a 2a 20 72 65  ve or zero.** re
1d411 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63  turn value indic
1d412 61 74 65 73 20 74 68 61 74 20 74 68 65 20 72 6f  ates that the ro
1d413 77 69 64 20 63 61 63 68 65 20 69 73 20 69 6e 76  wid cache is inv
1d414 61 6c 69 64 20 61 6e 64 20 73 68 6f 75 6c 64 20  alid and should 
1d415 62 65 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20  be.** ignored.  
1d416 49 66 20 74 68 65 20 72 6f 77 69 64 20 63 61 63  If the rowid cac
1d417 68 65 20 68 61 73 20 6e 65 76 65 72 20 62 65 66  he has never bef
1d418 6f 72 65 20 62 65 65 6e 20 73 65 74 2c 20 74 68  ore been set, th
1d419 65 6e 20 61 0a 2a 2a 20 7a 65 72 6f 20 69 73 20  en a.** zero is 
1d41a 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
1d41b 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
1d41c 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
1d41d 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64 52  3BtreeGetCachedR
1d41e 6f 77 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70  owid(BtCursor *p
1d41f 43 75 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  Cur){.  return p
1d420 43 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64  Cur->cachedRowid
1d421 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
1d422 20 61 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20   a cursor.  The 
1d423 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  read lock on the
1d424 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
1d425 73 20 72 65 6c 65 61 73 65 64 0a 2a 2a 20 77 68  s released.** wh
1d426 65 6e 20 74 68 65 20 6c 61 73 74 20 63 75 72 73  en the last curs
1d427 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f  or is closed..*/
1d428 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1d429 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
1d42a 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 43 75  CloseCursor(BtCu
1d42b 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 42  rsor *pCur){.  B
1d42c 74 72 65 65 20 2a 70 42 74 72 65 65 20 3d 20 70  tree *pBtree = p
1d42d 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 69  Cur->pBtree;.  i
1d42e 66 28 20 70 42 74 72 65 65 20 29 7b 0a 20 20 20  f( pBtree ){.   
1d42f 20 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 53 68   int i;.    BtSh
1d430 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72  ared *pBt = pCur
1d431 2d 3e 70 42 74 3b 0a 20 20 20 20 73 71 6c 69 74  ->pBt;.    sqlit
1d432 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74  e3BtreeEnter(pBt
1d433 72 65 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ree);.    sqlite
1d434 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f  3BtreeClearCurso
1d435 72 28 70 43 75 72 29 3b 0a 20 20 20 20 69 66 28  r(pCur);.    if(
1d436 20 70 43 75 72 2d 3e 70 50 72 65 76 20 29 7b 0a   pCur->pPrev ){.
1d437 20 20 20 20 20 20 70 43 75 72 2d 3e 70 50 72 65        pCur->pPre
1d438 76 2d 3e 70 4e 65 78 74 20 3d 20 70 43 75 72 2d  v->pNext = pCur-
1d439 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73  >pNext;.    }els
1d43a 65 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 43  e{.      pBt->pC
1d43b 75 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70 4e  ursor = pCur->pN
1d43c 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ext;.    }.    i
1d43d 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 29  f( pCur->pNext )
1d43e 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4e  {.      pCur->pN
1d43f 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75  ext->pPrev = pCu
1d440 72 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 7d 0a  r->pPrev;.    }.
1d441 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d      for(i=0; i<=
1d442 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b  pCur->iPage; i++
1d443 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  ){.      release
1d444 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67  Page(pCur->apPag
1d445 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20  e[i]);.    }.   
1d446 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e   unlockBtreeIfUn
1d447 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 69  used(pBt);.    i
1d448 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f  nvalidateOverflo
1d449 77 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20  wCache(pCur);.  
1d44a 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 66 72 65    /* sqlite3_fre
1d44b 65 28 70 43 75 72 29 3b 20 2a 2f 0a 20 20 20 20  e(pCur); */.    
1d44c 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1d44d 65 28 70 42 74 72 65 65 29 3b 0a 20 20 7d 0a 20  e(pBtree);.  }. 
1d44e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1d44f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65  K;.}../*.** Make
1d450 20 73 75 72 65 20 74 68 65 20 42 74 43 75 72 73   sure the BtCurs
1d451 6f 72 2a 20 67 69 76 65 6e 20 69 6e 20 74 68 65  or* given in the
1d452 20 61 72 67 75 6d 65 6e 74 20 68 61 73 20 61 20   argument has a 
1d453 76 61 6c 69 64 0a 2a 2a 20 42 74 43 75 72 73 6f  valid.** BtCurso
1d454 72 2e 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  r.info structure
1d455 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20  .  If it is not 
1d456 61 6c 72 65 61 64 79 20 76 61 6c 69 64 2c 20 63  already valid, c
1d457 61 6c 6c 0a 2a 2a 20 62 74 72 65 65 50 61 72 73  all.** btreePars
1d458 65 43 65 6c 6c 28 29 20 74 6f 20 66 69 6c 6c 20  eCell() to fill 
1d459 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 42 74 43  it in..**.** BtC
1d45a 75 72 73 6f 72 2e 69 6e 66 6f 20 69 73 20 61 20  ursor.info is a 
1d45b 63 61 63 68 65 20 6f 66 20 74 68 65 20 69 6e 66  cache of the inf
1d45c 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20  ormation in the 
1d45d 63 75 72 72 65 6e 74 20 63 65 6c 6c 2e 0a 2a 2a  current cell..**
1d45e 20 55 73 69 6e 67 20 74 68 69 73 20 63 61 63 68   Using this cach
1d45f 65 20 72 65 64 75 63 65 73 20 74 68 65 20 6e 75  e reduces the nu
1d460 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f  mber of calls to
1d461 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28   btreeParseCell(
1d462 29 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 37 2d 30 36  )..**.** 2007-06
1d463 2d 32 35 3a 20 20 54 68 65 72 65 20 69 73 20 61  -25:  There is a
1d464 20 62 75 67 20 69 6e 20 73 6f 6d 65 20 76 65 72   bug in some ver
1d465 73 69 6f 6e 73 20 6f 66 20 4d 53 56 43 20 74 68  sions of MSVC th
1d466 61 74 20 63 61 75 73 65 20 74 68 65 0a 2a 2a 20  at cause the.** 
1d467 63 6f 6d 70 69 6c 65 72 20 74 6f 20 63 72 61 73  compiler to cras
1d468 68 20 77 68 65 6e 20 67 65 74 43 65 6c 6c 49 6e  h when getCellIn
1d469 66 6f 28 29 20 69 73 20 69 6d 70 6c 65 6d 65 6e  fo() is implemen
1d46a 74 65 64 20 61 73 20 61 20 6d 61 63 72 6f 2e 0a  ted as a macro..
1d46b 2a 2a 20 42 75 74 20 74 68 65 72 65 20 69 73 20  ** But there is 
1d46c 61 20 6d 65 61 73 75 72 65 61 62 6c 65 20 73 70  a measureable sp
1d46d 65 65 64 20 61 64 76 61 6e 74 61 67 65 20 74 6f  eed advantage to
1d46e 20 75 73 69 6e 67 20 74 68 65 20 6d 61 63 72 6f   using the macro
1d46f 20 6f 6e 20 67 63 63 0a 2a 2a 20 28 77 68 65 6e   on gcc.** (when
1d470 20 6c 65 73 73 20 63 6f 6d 70 69 6c 65 72 20 6f   less compiler o
1d471 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 6c 69 6b  ptimizations lik
1d472 65 20 2d 4f 73 20 6f 72 20 2d 4f 30 20 61 72 65  e -Os or -O0 are
1d473 20 75 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a   used and the.**
1d474 20 63 6f 6d 70 69 6c 65 72 20 69 73 20 6e 6f 74   compiler is not
1d475 20 64 6f 69 6e 67 20 61 67 72 65 73 73 69 76 65   doing agressive
1d476 20 69 6e 6c 69 6e 69 6e 67 2e 29 20 20 53 6f 20   inlining.)  So 
1d477 77 65 20 75 73 65 20 61 20 72 65 61 6c 20 66 75  we use a real fu
1d478 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 4d 53  nction.** for MS
1d479 56 43 20 61 6e 64 20 61 20 6d 61 63 72 6f 20 66  VC and a macro f
1d47a 6f 72 20 65 76 65 72 79 74 68 69 6e 67 20 65 6c  or everything el
1d47b 73 65 2e 20 20 54 69 63 6b 65 74 20 23 32 34 35  se.  Ticket #245
1d47c 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44  7..*/.#ifndef ND
1d47d 45 42 55 47 0a 20 20 73 74 61 74 69 63 20 76 6f  EBUG.  static vo
1d47e 69 64 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66  id assertCellInf
1d47f 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  o(BtCursor *pCur
1d480 29 7b 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20  ){.    CellInfo 
1d481 69 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 69 50  info;.    int iP
1d482 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67  age = pCur->iPag
1d483 65 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 69  e;.    memset(&i
1d484 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 69  nfo, 0, sizeof(i
1d485 6e 66 6f 29 29 3b 0a 20 20 20 20 62 74 72 65 65  nfo));.    btree
1d486 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e  ParseCell(pCur->
1d487 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 20 70  apPage[iPage], p
1d488 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65  Cur->aiIdx[iPage
1d489 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61  ], &info);.    a
1d48a 73 73 65 72 74 28 20 6d 65 6d 63 6d 70 28 26 69  ssert( memcmp(&i
1d48b 6e 66 6f 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f  nfo, &pCur->info
1d48c 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3d  , sizeof(info))=
1d48d 3d 30 20 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  =0 );.  }.#else.
1d48e 20 20 23 64 65 66 69 6e 65 20 61 73 73 65 72 74    #define assert
1d48f 43 65 6c 6c 49 6e 66 6f 28 78 29 0a 23 65 6e 64  CellInfo(x).#end
1d490 69 66 0a 23 69 66 64 65 66 20 5f 4d 53 43 5f 56  if.#ifdef _MSC_V
1d491 45 52 0a 20 20 2f 2a 20 55 73 65 20 61 20 72 65  ER.  /* Use a re
1d492 61 6c 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 4d  al function in M
1d493 53 56 43 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75  SVC to work arou
1d494 6e 64 20 62 75 67 73 20 69 6e 20 74 68 61 74 20  nd bugs in that 
1d495 63 6f 6d 70 69 6c 65 72 2e 20 2a 2f 0a 20 20 73  compiler. */.  s
1d496 74 61 74 69 63 20 76 6f 69 64 20 67 65 74 43 65  tatic void getCe
1d497 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20  llInfo(BtCursor 
1d498 2a 70 43 75 72 29 7b 0a 20 20 20 20 69 66 28 20  *pCur){.    if( 
1d499 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
1d49a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ==0 ){.      int
1d49b 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69   iPage = pCur->i
1d49c 50 61 67 65 3b 0a 20 20 20 20 20 20 62 74 72 65  Page;.      btre
1d49d 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d  eParseCell(pCur-
1d49e 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 70  >apPage[iPage],p
1d49f 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65  Cur->aiIdx[iPage
1d4a0 5d 2c 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a  ],&pCur->info);.
1d4a1 20 20 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69        pCur->vali
1d4a2 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 7d  dNKey = 1;.    }
1d4a3 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
1d4a4 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29  rtCellInfo(pCur)
1d4a5 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73  ;.    }.  }.#els
1d4a6 65 20 2f 2a 20 69 66 20 6e 6f 74 20 5f 4d 53 43  e /* if not _MSC
1d4a7 5f 56 45 52 20 2a 2f 0a 20 20 2f 2a 20 55 73 65  _VER */.  /* Use
1d4a8 20 61 20 6d 61 63 72 6f 20 69 6e 20 61 6c 6c 20   a macro in all 
1d4a9 6f 74 68 65 72 20 63 6f 6d 70 69 6c 65 72 73 20  other compilers 
1d4aa 73 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63  so that the func
1d4ab 74 69 6f 6e 20 69 73 20 69 6e 6c 69 6e 65 64 20  tion is inlined 
1d4ac 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 43 65  */.#define getCe
1d4ad 6c 6c 49 6e 66 6f 28 70 43 75 72 29 20 20 20 20  llInfo(pCur)    
1d4ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4b1 20 20 5c 0a 20 20 69 66 28 20 70 43 75 72 2d 3e    \.  if( pCur->
1d4b2 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b  info.nSize==0 ){
1d4b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4b6 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 69 50 61     \.    int iPa
1d4b7 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65  ge = pCur->iPage
1d4b8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1d4b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4bb 20 20 20 20 5c 0a 20 20 20 20 62 74 72 65 65 50      \.    btreeP
1d4bc 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61  arseCell(pCur->a
1d4bd 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 75  pPage[iPage],pCu
1d4be 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c  r->aiIdx[iPage],
1d4bf 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 5c 0a  &pCur->info); \.
1d4c0 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e      pCur->validN
1d4c1 4b 65 79 20 3d 20 31 3b 20 20 20 20 20 20 20 20  Key = 1;        
1d4c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
1d4c5 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20  .  }else{       
1d4c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4ca 5c 0a 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c  \.    assertCell
1d4cb 49 6e 66 6f 28 70 43 75 72 29 3b 20 20 20 20 20  Info(pCur);     
1d4cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d4cf 20 5c 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a   \.  }.#endif /*
1d4d0 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 0a 23 69   _MSC_VER */..#i
1d4d1 66 6e 64 65 66 20 4e 44 45 42 55 47 20 20 2f 2a  fndef NDEBUG  /*
1d4d2 20 54 68 65 20 6e 65 78 74 20 72 6f 75 74 69 6e   The next routin
1d4d3 65 20 75 73 65 64 20 6f 6e 6c 79 20 77 69 74 68  e used only with
1d4d4 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74  in assert() stat
1d4d5 65 6d 65 6e 74 73 20 2a 2f 0a 2f 2a 0a 2a 2a 20  ements */./*.** 
1d4d6 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
1d4d7 68 65 20 67 69 76 65 6e 20 42 74 43 75 72 73 6f  he given BtCurso
1d4d8 72 20 69 73 20 76 61 6c 69 64 2e 20 20 41 20 76  r is valid.  A v
1d4d9 61 6c 69 64 20 63 75 72 73 6f 72 20 69 73 20 6f  alid cursor is o
1d4da 6e 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 63 75  ne.** that is cu
1d4db 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
1d4dc 20 74 6f 20 61 20 72 6f 77 20 69 6e 20 61 20 28   to a row in a (
1d4dd 6e 6f 6e 2d 65 6d 70 74 79 29 20 74 61 62 6c 65  non-empty) table
1d4de 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 76  ..** This is a v
1d4df 65 72 69 66 69 63 61 74 69 6f 6e 20 72 6f 75 74  erification rout
1d4e0 69 6e 65 20 69 73 20 75 73 65 64 20 6f 6e 6c 79  ine is used only
1d4e1 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29   within assert()
1d4e2 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a   statements..*/.
1d4e3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1d4e4 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
1d4e5 75 72 73 6f 72 49 73 56 61 6c 69 64 28 42 74 43  ursorIsValid(BtC
1d4e6 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
1d4e7 72 65 74 75 72 6e 20 70 43 75 72 20 26 26 20 70  return pCur && p
1d4e8 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
1d4e9 53 4f 52 5f 56 41 4c 49 44 3b 0a 7d 0a 23 65 6e  SOR_VALID;.}.#en
1d4ea 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  dif /* NDEBUG */
1d4eb 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69  ../*.** Set *pSi
1d4ec 7a 65 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f  ze to the size o
1d4ed 66 20 74 68 65 20 62 75 66 66 65 72 20 6e 65 65  f the buffer nee
1d4ee 64 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ded to hold the 
1d4ef 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 74 68 65 20  value of.** the 
1d4f0 6b 65 79 20 66 6f 72 20 74 68 65 20 63 75 72 72  key for the curr
1d4f1 65 6e 74 20 65 6e 74 72 79 2e 20 20 49 66 20 74  ent entry.  If t
1d4f2 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74  he cursor is not
1d4f3 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 20   pointing.** to 
1d4f4 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2c 20 2a  a valid entry, *
1d4f5 70 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20  pSize is set to 
1d4f6 30 2e 20 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20  0. .**.** For a 
1d4f7 74 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 49  table with the I
1d4f8 4e 54 4b 45 59 20 66 6c 61 67 20 73 65 74 2c 20  NTKEY flag set, 
1d4f9 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
1d4fa 75 72 6e 73 20 74 68 65 20 6b 65 79 0a 2a 2a 20  urns the key.** 
1d4fb 69 74 73 65 6c 66 2c 20 6e 6f 74 20 74 68 65 20  itself, not the 
1d4fc 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
1d4fd 69 6e 20 74 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a  in the key..**.*
1d4fe 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73  * The caller mus
1d4ff 74 20 70 6f 73 69 74 69 6f 6e 20 74 68 65 20 63  t position the c
1d500 75 72 73 6f 72 20 70 72 69 6f 72 20 74 6f 20 69  ursor prior to i
1d501 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75  nvoking this rou
1d502 74 69 6e 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 69  tine..** .** Thi
1d503 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 6e 6f 74  s routine cannot
1d504 20 66 61 69 6c 2e 20 20 49 74 20 61 6c 77 61 79   fail.  It alway
1d505 73 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  s returns SQLITE
1d506 5f 4f 4b 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45  _OK.  .*/.SQLITE
1d507 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1d508 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
1d509 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
1d50a 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20   i64 *pSize){.  
1d50b 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
1d50c 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
1d50d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
1d50e 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
1d50f 5f 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72  _INVALID || pCur
1d510 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
1d511 5f 56 41 4c 49 44 20 29 3b 0a 20 20 69 66 28 20  _VALID );.  if( 
1d512 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55  pCur->eState!=CU
1d513 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20  RSOR_VALID ){.  
1d514 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20    *pSize = 0;.  
1d515 7d 65 6c 73 65 7b 0a 20 20 20 20 67 65 74 43 65  }else{.    getCe
1d516 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20  llInfo(pCur);.  
1d517 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 2d    *pSize = pCur-
1d518 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 7d 0a  >info.nKey;.  }.
1d519 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1d51a 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  OK;.}../*.** Set
1d51b 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 20 6e   *pSize to the n
1d51c 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
1d51d 66 20 64 61 74 61 20 69 6e 20 74 68 65 20 65 6e  f data in the en
1d51e 74 72 79 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f  try the.** curso
1d51f 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  r currently poin
1d520 74 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ts to..**.** The
1d521 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 67 75 61   caller must gua
1d522 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20  rantee that the 
1d523 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69  cursor is pointi
1d524 6e 67 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c  ng to a non-NULL
1d525 0a 2a 2a 20 76 61 6c 69 64 20 65 6e 74 72 79 2e  .** valid entry.
1d526 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
1d527 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72  , the calling pr
1d528 6f 63 65 64 75 72 65 20 6d 75 73 74 20 67 75 61  ocedure must gua
1d529 72 61 6e 74 65 65 0a 2a 2a 20 74 68 61 74 20 74  rantee.** that t
1d52a 68 65 20 63 75 72 73 6f 72 20 68 61 73 20 43 75  he cursor has Cu
1d52b 72 73 6f 72 2e 65 53 74 61 74 65 3d 3d 43 55 52  rsor.eState==CUR
1d52c 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a  SOR_VALID..**.**
1d52d 20 46 61 69 6c 75 72 65 20 69 73 20 6e 6f 74 20   Failure is not 
1d52e 70 6f 73 73 69 62 6c 65 2e 20 20 54 68 69 73 20  possible.  This 
1d52f 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20  function always 
1d530 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
1d531 4b 2e 0a 2a 2a 20 49 74 20 6d 69 67 68 74 20 6a  K..** It might j
1d532 75 73 74 20 61 73 20 77 65 6c 6c 20 62 65 20 61  ust as well be a
1d533 20 70 72 6f 63 65 64 75 72 65 20 28 72 65 74 75   procedure (retu
1d534 72 6e 69 6e 67 20 76 6f 69 64 29 20 62 75 74 20  rning void) but 
1d535 77 65 20 63 6f 6e 74 69 6e 75 65 0a 2a 2a 20 74  we continue.** t
1d536 6f 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65  o return an inte
1d537 67 65 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20  ger result code 
1d538 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 72  for historical r
1d539 65 61 73 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  easons..*/.SQLIT
1d53a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1d53b 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 53 69  lite3BtreeDataSi
1d53c 7a 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  ze(BtCursor *pCu
1d53d 72 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 7b 0a  r, u32 *pSize){.
1d53e 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
1d53f 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
1d540 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
1d541 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
1d542 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 67 65  OR_VALID );.  ge
1d543 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b  tCellInfo(pCur);
1d544 0a 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72  .  *pSize = pCur
1d545 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20  ->info.nData;.  
1d546 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1d547 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  ;.}../*.** Given
1d548 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
1d549 20 6f 66 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20   of an overflow 
1d54a 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61  page in the data
1d54b 62 61 73 65 20 28 70 61 72 61 6d 65 74 65 72 0a  base (parameter.
1d54c 2a 2a 20 6f 76 66 6c 29 2c 20 74 68 69 73 20 66  ** ovfl), this f
1d54d 75 6e 63 74 69 6f 6e 20 66 69 6e 64 73 20 74 68  unction finds th
1d54e 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  e page number of
1d54f 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69   the next page i
1d550 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64  n the .** linked
1d551 20 6c 69 73 74 20 6f 66 20 6f 76 65 72 66 6c 6f   list of overflo
1d552 77 20 70 61 67 65 73 2e 20 49 66 20 70 6f 73 73  w pages. If poss
1d553 69 62 6c 65 2c 20 69 74 20 75 73 65 73 20 74 68  ible, it uses th
1d554 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a  e auto-vacuum.**
1d555 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 64 61 74   pointer-map dat
1d556 61 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 61  a instead of rea
1d557 64 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74  ding the content
1d558 20 6f 66 20 70 61 67 65 20 6f 76 66 6c 20 74 6f   of page ovfl to
1d559 20 64 6f 20 73 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49   do so. .**.** I
1d55a 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
1d55b 73 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  s an SQLite erro
1d55c 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
1d55d 65 64 2e 20 4f 74 68 65 72 77 69 73 65 3a 0a 2a  ed. Otherwise:.*
1d55e 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 6e 75  *.** The page nu
1d55f 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74  mber of the next
1d560 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69   overflow page i
1d561 6e 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73  n the linked lis
1d562 74 20 69 73 20 0a 2a 2a 20 77 72 69 74 74 65 6e  t is .** written
1d563 20 74 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 2e 20   to *pPgnoNext. 
1d564 49 66 20 70 61 67 65 20 6f 76 66 6c 20 69 73 20  If page ovfl is 
1d565 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e  the last page in
1d566 20 69 74 73 20 6c 69 6e 6b 65 64 20 0a 2a 2a 20   its linked .** 
1d567 6c 69 73 74 2c 20 2a 70 50 67 6e 6f 4e 65 78 74  list, *pPgnoNext
1d568 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e   is set to zero.
1d569 20 0a 2a 2a 0a 2a 2a 20 49 66 20 70 70 50 61 67   .**.** If ppPag
1d56a 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 61  e is not NULL, a
1d56b 6e 64 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  nd a reference t
1d56c 6f 20 74 68 65 20 4d 65 6d 50 61 67 65 20 6f 62  o the MemPage ob
1d56d 6a 65 63 74 20 63 6f 72 72 65 73 70 6f 6e 64 69  ject correspondi
1d56e 6e 67 0a 2a 2a 20 74 6f 20 70 61 67 65 20 6e 75  ng.** to page nu
1d56f 6d 62 65 72 20 70 4f 76 66 6c 20 77 61 73 20 6f  mber pOvfl was o
1d570 62 74 61 69 6e 65 64 2c 20 74 68 65 6e 20 2a 70  btained, then *p
1d571 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f 20  pPage is set to 
1d572 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 0a 2a 2a  point to that.**
1d573 20 72 65 66 65 72 65 6e 63 65 2e 20 49 74 20 69   reference. It i
1d574 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69  s the responsibi
1d575 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c  lity of the call
1d576 65 72 20 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61  er to call relea
1d577 73 65 50 61 67 65 28 29 0a 2a 2a 20 6f 6e 20 2a  sePage().** on *
1d578 70 70 50 61 67 65 20 74 6f 20 66 72 65 65 20 74  ppPage to free t
1d579 68 65 20 72 65 66 65 72 65 6e 63 65 2e 20 49 6e  he reference. In
1d57a 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61   no reference wa
1d57b 73 20 6f 62 74 61 69 6e 65 64 20 28 62 65 63 61  s obtained (beca
1d57c 75 73 65 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74  use.** the point
1d57d 65 72 2d 6d 61 70 20 77 61 73 20 75 73 65 64 20  er-map was used 
1d57e 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 76 61  to obtain the va
1d57f 6c 75 65 20 66 6f 72 20 2a 70 50 67 6e 6f 4e 65  lue for *pPgnoNe
1d580 78 74 29 2c 20 74 68 65 6e 0a 2a 2a 20 2a 70 70  xt), then.** *pp
1d581 50 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 7a  Page is set to z
1d582 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ero..*/.static i
1d583 6e 74 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61  nt getOverflowPa
1d584 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ge(.  BtShared *
1d585 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  pBt,            
1d586 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
1d587 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e  se file */.  Pgn
1d588 6f 20 6f 76 66 6c 2c 20 20 20 20 20 20 20 20 20  o ovfl,         
1d589 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
1d58a 72 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61  rent overflow pa
1d58b 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 4d  ge number */.  M
1d58c 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c  emPage **ppPage,
1d58d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1d58e 55 54 3a 20 4d 65 6d 50 61 67 65 20 68 61 6e 64  UT: MemPage hand
1d58f 6c 65 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29  le (may be NULL)
1d590 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e   */.  Pgno *pPgn
1d591 6f 4e 65 78 74 20 20 20 20 20 20 20 20 20 20 20  oNext           
1d592 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 65 78 74 20     /* OUT: Next 
1d593 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75  overflow page nu
1d594 6d 62 65 72 20 2a 2f 0a 29 7b 0a 20 20 50 67 6e  mber */.){.  Pgn
1d595 6f 20 6e 65 78 74 20 3d 20 30 3b 0a 20 20 4d 65  o next = 0;.  Me
1d596 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30  mPage *pPage = 0
1d597 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
1d598 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72  ITE_OK;..  asser
1d599 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1d59a 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
1d59b 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 70 50  ) );.  assert(pP
1d59c 67 6e 6f 4e 65 78 74 29 3b 0a 0a 23 69 66 6e 64  gnoNext);..#ifnd
1d59d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1d59e 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20 54  UTOVACUUM.  /* T
1d59f 72 79 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e  ry to find the n
1d5a0 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ext page in the 
1d5a1 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 75 73  overflow list us
1d5a2 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 61 75 74  ing the.  ** aut
1d5a3 6f 76 61 63 75 75 6d 20 70 6f 69 6e 74 65 72 2d  ovacuum pointer-
1d5a4 6d 61 70 20 70 61 67 65 73 2e 20 47 75 65 73 73  map pages. Guess
1d5a5 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 70   that the next p
1d5a6 61 67 65 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65  age in .  ** the
1d5a7 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 69   overflow list i
1d5a8 73 20 70 61 67 65 20 6e 75 6d 62 65 72 20 28 6f  s page number (o
1d5a9 76 66 6c 2b 31 29 2e 20 49 66 20 74 68 61 74 20  vfl+1). If that 
1d5aa 67 75 65 73 73 20 74 75 72 6e 73 20 0a 20 20 2a  guess turns .  *
1d5ab 2a 20 6f 75 74 20 74 6f 20 62 65 20 77 72 6f 6e  * out to be wron
1d5ac 67 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20  g, fall back to 
1d5ad 6c 6f 61 64 69 6e 67 20 74 68 65 20 64 61 74 61  loading the data
1d5ae 20 6f 66 20 70 61 67 65 20 0a 20 20 2a 2a 20 6e   of page .  ** n
1d5af 75 6d 62 65 72 20 6f 76 66 6c 20 74 6f 20 64 65  umber ovfl to de
1d5b0 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 65 78 74  termine the next
1d5b1 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 20 20   page number..  
1d5b2 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75  */.  if( pBt->au
1d5b3 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
1d5b4 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 20 20 50  Pgno pgno;.    P
1d5b5 67 6e 6f 20 69 47 75 65 73 73 20 3d 20 6f 76 66  gno iGuess = ovf
1d5b6 6c 2b 31 3b 0a 20 20 20 20 75 38 20 65 54 79 70  l+1;.    u8 eTyp
1d5b7 65 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 50  e;..    while( P
1d5b8 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74  TRMAP_ISPAGE(pBt
1d5b9 2c 20 69 47 75 65 73 73 29 20 7c 7c 20 69 47 75  , iGuess) || iGu
1d5ba 65 73 73 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  ess==PENDING_BYT
1d5bb 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20  E_PAGE(pBt) ){. 
1d5bc 20 20 20 20 20 69 47 75 65 73 73 2b 2b 3b 0a 20       iGuess++;. 
1d5bd 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 69 47     }..    if( iG
1d5be 75 65 73 73 3c 3d 62 74 72 65 65 50 61 67 65 63  uess<=btreePagec
1d5bf 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20  ount(pBt) ){.   
1d5c0 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65     rc = ptrmapGe
1d5c1 74 28 70 42 74 2c 20 69 47 75 65 73 73 2c 20 26  t(pBt, iGuess, &
1d5c2 65 54 79 70 65 2c 20 26 70 67 6e 6f 29 3b 0a 20  eType, &pgno);. 
1d5c3 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
1d5c4 49 54 45 5f 4f 4b 20 26 26 20 65 54 79 70 65 3d  ITE_OK && eType=
1d5c5 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57  =PTRMAP_OVERFLOW
1d5c6 32 20 26 26 20 70 67 6e 6f 3d 3d 6f 76 66 6c 20  2 && pgno==ovfl 
1d5c7 29 7b 0a 20 20 20 20 20 20 20 20 6e 65 78 74 20  ){.        next 
1d5c8 3d 20 69 47 75 65 73 73 3b 0a 20 20 20 20 20 20  = iGuess;.      
1d5c9 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f    rc = SQLITE_DO
1d5ca 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  NE;.      }.    
1d5cb 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
1d5cc 61 73 73 65 72 74 28 20 6e 65 78 74 3d 3d 30 20  assert( next==0 
1d5cd 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  || rc==SQLITE_DO
1d5ce 4e 45 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  NE );.  if( rc==
1d5cf 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1d5d0 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61   rc = btreeGetPa
1d5d1 67 65 28 70 42 74 2c 20 6f 76 66 6c 2c 20 26 70  ge(pBt, ovfl, &p
1d5d2 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 61 73  Page, 0);.    as
1d5d3 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
1d5d4 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 3d 3d 30 20  _OK || pPage==0 
1d5d5 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
1d5d6 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1d5d7 20 20 6e 65 78 74 20 3d 20 67 65 74 34 62 79 74    next = get4byt
1d5d8 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 3b  e(pPage->aData);
1d5d9 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70  .    }.  }..  *p
1d5da 50 67 6e 6f 4e 65 78 74 20 3d 20 6e 65 78 74 3b  PgnoNext = next;
1d5db 0a 20 20 69 66 28 20 70 70 50 61 67 65 20 29 7b  .  if( ppPage ){
1d5dc 0a 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70  .    *ppPage = p
1d5dd 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Page;.  }else{. 
1d5de 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1d5df 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Page);.  }.  ret
1d5e0 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  urn (rc==SQLITE_
1d5e1 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  DONE ? SQLITE_OK
1d5e2 20 3a 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   : rc);.}../*.**
1d5e3 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20   Copy data from 
1d5e4 61 20 62 75 66 66 65 72 20 74 6f 20 61 20 70 61  a buffer to a pa
1d5e5 67 65 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 61  ge, or from a pa
1d5e6 67 65 20 74 6f 20 61 20 62 75 66 66 65 72 2e 0a  ge to a buffer..
1d5e7 2a 2a 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 69  **.** pPayload i
1d5e8 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 64  s a pointer to d
1d5e9 61 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 61  ata stored on da
1d5ea 74 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50  tabase page pDbP
1d5eb 61 67 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 6d  age..** If argum
1d5ec 65 6e 74 20 65 4f 70 20 69 73 20 66 61 6c 73 65  ent eOp is false
1d5ed 2c 20 74 68 65 6e 20 6e 42 79 74 65 20 62 79 74  , then nByte byt
1d5ee 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63  es of data are c
1d5ef 6f 70 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 50  opied.** from pP
1d5f0 61 79 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 75  ayload to the bu
1d5f1 66 66 65 72 20 70 6f 69 6e 74 65 64 20 61 74 20  ffer pointed at 
1d5f2 62 79 20 70 42 75 66 2e 20 49 66 20 65 4f 70 20  by pBuf. If eOp 
1d5f3 69 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e  is true,.** then
1d5f4 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
1d5f5 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f  te() is called o
1d5f6 6e 20 70 44 62 50 61 67 65 20 61 6e 64 20 6e 42  n pDbPage and nB
1d5f7 79 74 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20  yte bytes.** of 
1d5f8 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 20  data are copied 
1d5f9 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20  from the buffer 
1d5fa 70 42 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 64  pBuf to pPayload
1d5fb 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  ..**.** SQLITE_O
1d5fc 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e  K is returned on
1d5fd 20 73 75 63 63 65 73 73 2c 20 6f 74 68 65 72 77   success, otherw
1d5fe 69 73 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  ise an error cod
1d5ff 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1d600 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 20   copyPayload(.  
1d601 76 6f 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c 20  void *pPayload, 
1d602 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
1d603 6e 74 65 72 20 74 6f 20 70 61 67 65 20 64 61 74  nter to page dat
1d604 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75  a */.  void *pBu
1d605 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f,              
1d606 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62   /* Pointer to b
1d607 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  uffer */.  int n
1d608 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  Byte,           
1d609 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1d60a 66 20 62 79 74 65 73 20 74 6f 20 63 6f 70 79 20  f bytes to copy 
1d60b 2a 2f 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 20  */.  int eOp,   
1d60c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1d60d 2a 20 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f 6d  * 0 -> copy from
1d60e 20 70 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 79   page, 1 -> copy
1d60f 20 74 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 62   to page */.  Db
1d610 50 61 67 65 20 2a 70 44 62 50 61 67 65 20 20 20  Page *pDbPage   
1d611 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
1d612 63 6f 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 6c  containing pPayl
1d613 6f 61 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20  oad */.){.  if( 
1d614 65 4f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f  eOp ){.    /* Co
1d615 70 79 20 64 61 74 61 20 66 72 6f 6d 20 62 75 66  py data from buf
1d616 66 65 72 20 74 6f 20 70 61 67 65 20 28 61 20 77  fer to page (a w
1d617 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 20  rite operation) 
1d618 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  */.    int rc = 
1d619 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
1d61a 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  e(pDbPage);.    
1d61b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1d61c 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
1d61d 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
1d61e 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c  memcpy(pPayload,
1d61f 20 70 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a 20   pBuf, nByte);. 
1d620 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43   }else{.    /* C
1d621 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 70 61  opy data from pa
1d622 67 65 20 74 6f 20 62 75 66 66 65 72 20 28 61 20  ge to buffer (a 
1d623 72 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 20  read operation) 
1d624 2a 2f 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42  */.    memcpy(pB
1d625 75 66 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e 42  uf, pPayload, nB
1d626 79 74 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  yte);.  }.  retu
1d627 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1d628 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
1d629 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
1d62a 72 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74  read or overwrit
1d62b 65 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d  e payload inform
1d62c 61 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65  ation.** for the
1d62d 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20   entry that the 
1d62e 70 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 70  pCur cursor is p
1d62f 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 49 66 20 74  ointing to. If t
1d630 68 65 20 65 4f 70 0a 2a 2a 20 70 61 72 61 6d 65  he eOp.** parame
1d631 74 65 72 20 69 73 20 30 2c 20 74 68 69 73 20 69  ter is 0, this i
1d632 73 20 61 20 72 65 61 64 20 6f 70 65 72 61 74 69  s a read operati
1d633 6f 6e 20 28 64 61 74 61 20 63 6f 70 69 65 64 20  on (data copied 
1d634 69 6e 74 6f 0a 2a 2a 20 62 75 66 66 65 72 20 70  into.** buffer p
1d635 42 75 66 29 2e 20 49 66 20 69 74 20 69 73 20 6e  Buf). If it is n
1d636 6f 6e 2d 7a 65 72 6f 2c 20 61 20 77 72 69 74 65  on-zero, a write
1d637 20 28 64 61 74 61 20 63 6f 70 69 65 64 20 66 72   (data copied fr
1d638 6f 6d 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75  om.** buffer pBu
1d639 66 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61  f)..**.** A tota
1d63a 6c 20 6f 66 20 22 61 6d 74 22 20 62 79 74 65 73  l of "amt" bytes
1d63b 20 61 72 65 20 72 65 61 64 20 6f 72 20 77 72 69   are read or wri
1d63c 74 74 65 6e 20 62 65 67 69 6e 6e 69 6e 67 20 61  tten beginning a
1d63d 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 20 44  t "offset"..** D
1d63e 61 74 61 20 69 73 20 72 65 61 64 20 74 6f 20 6f  ata is read to o
1d63f 72 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65  r from the buffe
1d640 72 20 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68  r pBuf..**.** Th
1d641 65 20 63 6f 6e 74 65 6e 74 20 62 65 69 6e 67 20  e content being 
1d642 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 20  read or written 
1d643 6d 69 67 68 74 20 61 70 70 65 61 72 20 6f 6e 20  might appear on 
1d644 74 68 65 20 6d 61 69 6e 20 70 61 67 65 0a 2a 2a  the main page.**
1d645 20 6f 72 20 62 65 20 73 63 61 74 74 65 72 65 64   or be scattered
1d646 20 6f 75 74 20 6f 6e 20 6d 75 6c 74 69 70 6c 65   out on multiple
1d647 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
1d648 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42 74  .**.** If the Bt
1d649 43 75 72 73 6f 72 2e 69 73 49 6e 63 72 62 6c 6f  Cursor.isIncrblo
1d64a 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20  bHandle flag is 
1d64b 73 65 74 2c 20 61 6e 64 20 74 68 65 20 63 75 72  set, and the cur
1d64c 72 65 6e 74 0a 2a 2a 20 63 75 72 73 6f 72 20 65  rent.** cursor e
1d64d 6e 74 72 79 20 75 73 65 73 20 6f 6e 65 20 6f 72  ntry uses one or
1d64e 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70   more overflow p
1d64f 61 67 65 73 2c 20 74 68 69 73 20 66 75 6e 63 74  ages, this funct
1d650 69 6f 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 73  ion.** allocates
1d651 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 6c   space for and l
1d652 61 7a 69 6c 79 20 70 6f 70 6c 75 61 74 65 73 20  azily popluates 
1d653 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  the overflow pag
1d654 65 2d 6c 69 73 74 20 0a 2a 2a 20 63 61 63 68 65  e-list .** cache
1d655 20 61 72 72 61 79 20 28 42 74 43 75 72 73 6f 72   array (BtCursor
1d656 2e 61 4f 76 65 72 66 6c 6f 77 29 2e 20 53 75 62  .aOverflow). Sub
1d657 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 75 73  sequent calls us
1d658 65 20 74 68 69 73 0a 2a 2a 20 63 61 63 68 65 20  e this.** cache 
1d659 74 6f 20 6d 61 6b 65 20 73 65 65 6b 69 6e 67 20  to make seeking 
1d65a 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  to the supplied 
1d65b 6f 66 66 73 65 74 20 6d 6f 72 65 20 65 66 66 69  offset more effi
1d65c 63 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63  cient..**.** Onc
1d65d 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61  e an overflow pa
1d65e 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61  ge-list cache ha
1d65f 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  s been allocated
1d660 2c 20 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20 69  , it may be.** i
1d661 6e 76 61 6c 69 64 61 74 65 64 20 69 66 20 73 6f  nvalidated if so
1d662 6d 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 20  me other cursor 
1d663 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 73 61  writes to the sa
1d664 6d 65 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 0a  me table, or if.
1d665 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  ** the cursor is
1d666 20 6d 6f 76 65 64 20 74 6f 20 61 20 64 69 66 66   moved to a diff
1d667 65 72 65 6e 74 20 72 6f 77 2e 20 41 64 64 69 74  erent row. Addit
1d668 69 6f 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74 6f  ionally, in auto
1d669 2d 76 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65 2c  -vacuum.** mode,
1d66a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   the following e
1d66b 76 65 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c 69  vents may invali
1d66c 64 61 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77  date an overflow
1d66d 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65   page-list cache
1d66e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69  ..**.**   * An i
1d66f 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75  ncremental vacuu
1d670 6d 2c 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d 6d  m,.**   * A comm
1d671 69 74 20 69 6e 20 61 75 74 6f 5f 76 61 63 75 75  it in auto_vacuu
1d672 6d 3d 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a 2a  m="full" mode,.*
1d673 2a 20 20 20 2a 20 43 72 65 61 74 69 6e 67 20 61  *   * Creating a
1d674 20 74 61 62 6c 65 20 28 6d 61 79 20 72 65 71 75   table (may requ
1d675 69 72 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f 76  ire moving an ov
1d676 65 72 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a 2f  erflow page)..*/
1d677 0a 73 74 61 74 69 63 20 69 6e 74 20 61 63 63 65  .static int acce
1d678 73 73 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43  ssPayload(.  BtC
1d679 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20  ursor *pCur,    
1d67a 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e    /* Cursor poin
1d67b 74 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f  ting to entry to
1d67c 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20   read from */.  
1d67d 75 33 32 20 6f 66 66 73 65 74 2c 20 20 20 20 20  u32 offset,     
1d67e 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65       /* Begin re
1d67f 61 64 69 6e 67 20 74 68 69 73 20 66 61 72 20 69  ading this far i
1d680 6e 74 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20  nto payload */. 
1d681 20 75 33 32 20 61 6d 74 2c 20 20 20 20 20 20 20   u32 amt,       
1d682 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68        /* Read th
1d683 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 2a 2f  is many bytes */
1d684 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
1d685 20 2a 70 42 75 66 2c 20 2f 2a 20 57 72 69 74 65   *pBuf, /* Write
1d686 20 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20   the bytes into 
1d687 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 20 0a  this buffer */ .
1d688 20 20 69 6e 74 20 65 4f 70 20 20 20 20 20 20 20    int eOp       
1d689 20 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f 20 74         /* zero t
1d68a 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72 6f  o read. non-zero
1d68b 20 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a 29 7b   to write. */.){
1d68c 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
1d68d 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 69 6e   *aPayload;.  in
1d68e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
1d68f 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20  ;.  u32 nKey;.  
1d690 69 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20 20  int iIdx = 0;.  
1d691 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d  MemPage *pPage =
1d692 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
1d693 75 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 42  ur->iPage]; /* B
1d694 74 72 65 65 20 70 61 67 65 20 6f 66 20 63 75 72  tree page of cur
1d695 72 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20  rent entry */.  
1d696 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
1d697 70 43 75 72 2d 3e 70 42 74 3b 20 20 20 20 20 20  pCur->pBt;      
1d698 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
1d699 74 72 65 65 20 74 68 69 73 20 63 75 72 73 6f 72  tree this cursor
1d69a 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 0a   belongs to */..
1d69b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 20    assert( pPage 
1d69c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
1d69d 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
1d69e 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73  R_VALID );.  ass
1d69f 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ert( pCur->aiIdx
1d6a0 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50  [pCur->iPage]<pP
1d6a1 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  age->nCell );.  
1d6a2 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
1d6a3 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
1d6a4 3b 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f  ;..  getCellInfo
1d6a5 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79 6c 6f  (pCur);.  aPaylo
1d6a6 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  ad = pCur->info.
1d6a7 70 43 65 6c 6c 20 2b 20 70 43 75 72 2d 3e 69 6e  pCell + pCur->in
1d6a8 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 6e 4b  fo.nHeader;.  nK
1d6a9 65 79 20 3d 20 28 70 50 61 67 65 2d 3e 69 6e 74  ey = (pPage->int
1d6aa 4b 65 79 20 3f 20 30 20 3a 20 28 69 6e 74 29 70  Key ? 0 : (int)p
1d6ab 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 3b  Cur->info.nKey);
1d6ac 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6f 66  ..  if( NEVER(of
1d6ad 66 73 65 74 2b 61 6d 74 20 3e 20 6e 4b 65 79 2b  fset+amt > nKey+
1d6ae 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61  pCur->info.nData
1d6af 29 20 0a 20 20 20 7c 7c 20 26 61 50 61 79 6c 6f  ) .   || &aPaylo
1d6b0 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c  ad[pCur->info.nL
1d6b1 6f 63 61 6c 5d 20 3e 20 26 70 50 61 67 65 2d 3e  ocal] > &pPage->
1d6b2 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c  aData[pBt->usabl
1d6b3 65 53 69 7a 65 5d 0a 20 20 29 7b 0a 20 20 20 20  eSize].  ){.    
1d6b4 2f 2a 20 54 72 79 69 6e 67 20 74 6f 20 72 65 61  /* Trying to rea
1d6b5 64 20 6f 72 20 77 72 69 74 65 20 70 61 73 74 20  d or write past 
1d6b6 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64  the end of the d
1d6b7 61 74 61 20 69 73 20 61 6e 20 65 72 72 6f 72 20  ata is an error 
1d6b8 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  */.    return SQ
1d6b9 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
1d6ba 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65  T;.  }..  /* Che
1d6bb 63 6b 20 69 66 20 64 61 74 61 20 6d 75 73 74 20  ck if data must 
1d6bc 62 65 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20  be read/written 
1d6bd 74 6f 2f 66 72 6f 6d 20 74 68 65 20 62 74 72 65  to/from the btre
1d6be 65 20 70 61 67 65 20 69 74 73 65 6c 66 2e 20 2a  e page itself. *
1d6bf 2f 0a 20 20 69 66 28 20 6f 66 66 73 65 74 3c 70  /.  if( offset<p
1d6c0 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c  Cur->info.nLocal
1d6c1 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 20 3d 20   ){.    int a = 
1d6c2 61 6d 74 3b 0a 20 20 20 20 69 66 28 20 61 2b 6f  amt;.    if( a+o
1d6c3 66 66 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66 6f  ffset>pCur->info
1d6c4 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20  .nLocal ){.     
1d6c5 20 61 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e   a = pCur->info.
1d6c6 6e 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74 3b  nLocal - offset;
1d6c7 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
1d6c8 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61  copyPayload(&aPa
1d6c9 79 6c 6f 61 64 5b 6f 66 66 73 65 74 5d 2c 20 70  yload[offset], p
1d6ca 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 50 61  Buf, a, eOp, pPa
1d6cb 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ge->pDbPage);.  
1d6cc 20 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20    offset = 0;.  
1d6cd 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20    pBuf += a;.   
1d6ce 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d 65 6c   amt -= a;.  }el
1d6cf 73 65 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2d  se{.    offset -
1d6d0 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f  = pCur->info.nLo
1d6d1 63 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  cal;.  }..  if( 
1d6d2 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1d6d3 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 63 6f   amt>0 ){.    co
1d6d4 6e 73 74 20 75 33 32 20 6f 76 66 6c 53 69 7a 65  nst u32 ovflSize
1d6d5 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   = pBt->usableSi
1d6d6 7a 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 74 65  ze - 4;  /* Byte
1d6d7 73 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 6f 76  s content per ov
1d6d8 66 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 50  fl page */.    P
1d6d9 67 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a 0a 20  gno nextPage;.. 
1d6da 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65     nextPage = ge
1d6db 74 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61 64  t4byte(&aPayload
1d6dc 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63  [pCur->info.nLoc
1d6dd 61 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  al]);..#ifndef S
1d6de 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
1d6df 4c 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  LOB.    /* If th
1d6e0 65 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64  e isIncrblobHand
1d6e1 6c 65 20 66 6c 61 67 20 69 73 20 73 65 74 20 61  le flag is set a
1d6e2 6e 64 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e  nd the BtCursor.
1d6e3 61 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20 20 20  aOverflow[].    
1d6e4 2a 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  ** has not been 
1d6e5 61 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f 63  allocated, alloc
1d6e6 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 20  ate it now. The 
1d6e7 61 72 72 61 79 20 69 73 20 73 69 7a 65 64 20 61  array is sized a
1d6e8 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e 74  t.    ** one ent
1d6e9 72 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65 72  ry for each over
1d6ea 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65  flow page in the
1d6eb 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e   overflow chain.
1d6ec 20 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65   The.    ** page
1d6ed 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 66   number of the f
1d6ee 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61  irst overflow pa
1d6ef 67 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ge is stored in 
1d6f0 61 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a 20 20  aOverflow[0],.  
1d6f1 20 20 2a 2a 20 65 74 63 2e 20 41 20 76 61 6c 75    ** etc. A valu
1d6f2 65 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61 4f  e of 0 in the aO
1d6f3 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 20  verflow[] array 
1d6f4 6d 65 61 6e 73 20 22 6e 6f 74 20 79 65 74 20 6b  means "not yet k
1d6f5 6e 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28 74 68  nown".    ** (th
1d6f6 65 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69 6c  e cache is lazil
1d6f7 79 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20 20  y populated)..  
1d6f8 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 75    */.    if( pCu
1d6f9 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  r->isIncrblobHan
1d6fa 64 6c 65 20 26 26 20 21 70 43 75 72 2d 3e 61 4f  dle && !pCur->aO
1d6fb 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20  verflow ){.     
1d6fc 20 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 70 43   int nOvfl = (pC
1d6fd 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61  ur->info.nPayloa
1d6fe 64 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f  d-pCur->info.nLo
1d6ff 63 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 29 2f  cal+ovflSize-1)/
1d700 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20  ovflSize;.      
1d701 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20  pCur->aOverflow 
1d702 3d 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69 74 65  = (Pgno *)sqlite
1d703 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65  3MallocZero(size
1d704 6f 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 6c 29 3b  of(Pgno)*nOvfl);
1d705 0a 20 20 20 20 20 20 2f 2a 20 6e 4f 76 66 6c 20  .      /* nOvfl 
1d706 69 73 20 61 6c 77 61 79 73 20 70 6f 73 69 74 69  is always positi
1d707 76 65 2e 20 20 49 66 20 69 74 20 77 65 72 65 20  ve.  If it were 
1d708 7a 65 72 6f 2c 20 66 65 74 63 68 50 61 79 6c 6f  zero, fetchPaylo
1d709 61 64 20 77 6f 75 6c 64 20 68 61 76 65 0a 20 20  ad would have.  
1d70a 20 20 20 20 2a 2a 20 62 65 65 6e 20 75 73 65 64      ** been used
1d70b 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 69 73   instead of this
1d70c 20 72 6f 75 74 69 6e 65 2e 20 2a 2f 0a 20 20 20   routine. */.   
1d70d 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 6e 4f     if( ALWAYS(nO
1d70e 76 66 6c 29 20 26 26 20 21 70 43 75 72 2d 3e 61  vfl) && !pCur->a
1d70f 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  Overflow ){.    
1d710 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1d711 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
1d712 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
1d713 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  the overflow pag
1d714 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73  e-list cache has
1d715 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20   been allocated 
1d716 61 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 65  and the.    ** e
1d717 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 72  ntry for the fir
1d718 73 74 20 72 65 71 75 69 72 65 64 20 6f 76 65 72  st required over
1d719 66 6c 6f 77 20 70 61 67 65 20 69 73 20 76 61 6c  flow page is val
1d71a 69 64 2c 20 73 6b 69 70 0a 20 20 20 20 2a 2a 20  id, skip.    ** 
1d71b 64 69 72 65 63 74 6c 79 20 74 6f 20 69 74 2e 0a  directly to it..
1d71c 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
1d71d 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26  Cur->aOverflow &
1d71e 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f  & pCur->aOverflo
1d71f 77 5b 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a  w[offset/ovflSiz
1d720 65 5d 20 29 7b 0a 20 20 20 20 20 20 69 49 64 78  e] ){.      iIdx
1d721 20 3d 20 28 6f 66 66 73 65 74 2f 6f 76 66 6c 53   = (offset/ovflS
1d722 69 7a 65 29 3b 0a 20 20 20 20 20 20 6e 65 78 74  ize);.      next
1d723 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76  Page = pCur->aOv
1d724 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3b 0a 20 20  erflow[iIdx];.  
1d725 20 20 20 20 6f 66 66 73 65 74 20 3d 20 28 6f 66      offset = (of
1d726 66 73 65 74 25 6f 76 66 6c 53 69 7a 65 29 3b 0a  fset%ovflSize);.
1d727 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
1d728 20 20 66 6f 72 28 20 3b 20 72 63 3d 3d 53 51 4c    for( ; rc==SQL
1d729 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20  ITE_OK && amt>0 
1d72a 26 26 20 6e 65 78 74 50 61 67 65 3b 20 69 49 64  && nextPage; iId
1d72b 78 2b 2b 29 7b 0a 0a 23 69 66 6e 64 65 66 20 53  x++){..#ifndef S
1d72c 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
1d72d 4c 4f 42 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  LOB.      /* If 
1d72e 72 65 71 75 69 72 65 64 2c 20 70 6f 70 75 6c 61  required, popula
1d72f 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  te the overflow 
1d730 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e  page-list cache.
1d731 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43   */.      if( pC
1d732 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b  ur->aOverflow ){
1d733 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1d734 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77  !pCur->aOverflow
1d735 5b 69 49 64 78 5d 20 7c 7c 20 70 43 75 72 2d 3e  [iIdx] || pCur->
1d736 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3d  aOverflow[iIdx]=
1d737 3d 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20  =nextPage);.    
1d738 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66      pCur->aOverf
1d739 6c 6f 77 5b 69 49 64 78 5d 20 3d 20 6e 65 78 74  low[iIdx] = next
1d73a 50 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 23 65  Page;.      }.#e
1d73b 6e 64 69 66 0a 0a 20 20 20 20 20 20 69 66 28 20  ndif..      if( 
1d73c 6f 66 66 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65  offset>=ovflSize
1d73d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   ){.        /* T
1d73e 68 65 20 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74  he only reason t
1d73f 6f 20 72 65 61 64 20 74 68 69 73 20 70 61 67 65  o read this page
1d740 20 69 73 20 74 6f 20 6f 62 74 61 69 6e 20 74 68   is to obtain th
1d741 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a  e page.        *
1d742 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  * number for the
1d743 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68   next page in th
1d744 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e  e overflow chain
1d745 2e 20 54 68 65 20 70 61 67 65 0a 20 20 20 20 20  . The page.     
1d746 20 20 20 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f     ** data is no
1d747 74 20 72 65 71 75 69 72 65 64 2e 20 53 6f 20 66  t required. So f
1d748 69 72 73 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b  irst try to look
1d749 75 70 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a  up the overflow.
1d74a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 2d          ** page-
1d74b 6c 69 73 74 20 63 61 63 68 65 2c 20 69 66 20 61  list cache, if a
1d74c 6e 79 2c 20 74 68 65 6e 20 66 61 6c 6c 20 62 61  ny, then fall ba
1d74d 63 6b 20 74 6f 20 74 68 65 20 67 65 74 4f 76 65  ck to the getOve
1d74e 72 66 6c 6f 77 50 61 67 65 28 29 0a 20 20 20 20  rflowPage().    
1d74f 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e      ** function.
1d750 0a 20 20 20 20 20 20 20 20 2a 2f 0a 23 69 66 6e  .        */.#ifn
1d751 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1d752 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 20 20  INCRBLOB.       
1d753 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72   if( pCur->aOver
1d754 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f  flow && pCur->aO
1d755 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20  verflow[iIdx+1] 
1d756 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78  ){.          nex
1d757 74 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f  tPage = pCur->aO
1d758 76 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b  verflow[iIdx+1];
1d759 0a 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20  .        } else 
1d75a 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
1d75b 20 20 72 63 20 3d 20 67 65 74 4f 76 65 72 66 6c    rc = getOverfl
1d75c 6f 77 50 61 67 65 28 70 42 74 2c 20 6e 65 78 74  owPage(pBt, next
1d75d 50 61 67 65 2c 20 30 2c 20 26 6e 65 78 74 50 61  Page, 0, &nextPa
1d75e 67 65 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66  ge);.        off
1d75f 73 65 74 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 3b  set -= ovflSize;
1d760 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1d761 20 20 20 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f        /* Need to
1d762 20 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20   read this page 
1d763 70 72 6f 70 65 72 6c 79 2e 20 49 74 20 63 6f 6e  properly. It con
1d764 74 61 69 6e 73 20 73 6f 6d 65 20 6f 66 20 74 68  tains some of th
1d765 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 61 6e  e.        ** ran
1d766 67 65 20 6f 66 20 64 61 74 61 20 74 68 61 74 20  ge of data that 
1d767 69 73 20 62 65 69 6e 67 20 72 65 61 64 20 28 65  is being read (e
1d768 4f 70 3d 3d 30 29 20 6f 72 20 77 72 69 74 74 65  Op==0) or writte
1d769 6e 20 28 65 4f 70 21 3d 30 29 2e 0a 20 20 20 20  n (eOp!=0)..    
1d76a 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51      */.#ifdef SQ
1d76b 4c 49 54 45 5f 44 49 52 45 43 54 5f 4f 56 45 52  LITE_DIRECT_OVER
1d76c 46 4c 4f 57 5f 52 45 41 44 0a 20 20 20 20 20 20  FLOW_READ.      
1d76d 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1d76e 66 64 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  fd;.#endif.     
1d76f 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a     int a = amt;.
1d770 20 20 20 20 20 20 20 20 69 66 28 20 61 20 2b 20          if( a + 
1d771 6f 66 66 73 65 74 20 3e 20 6f 76 66 6c 53 69 7a  offset > ovflSiz
1d772 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  e ){.          a
1d773 20 3d 20 6f 76 66 6c 53 69 7a 65 20 2d 20 6f 66   = ovflSize - of
1d774 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  fset;.        }.
1d775 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1d776 49 52 45 43 54 5f 4f 56 45 52 46 4c 4f 57 5f 52  IRECT_OVERFLOW_R
1d777 45 41 44 0a 20 20 20 20 20 20 20 20 2f 2a 20 49  EAD.        /* I
1d778 66 20 61 6c 6c 20 74 68 65 20 66 6f 6c 6c 6f 77  f all the follow
1d779 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 20 20  ing are true:.  
1d77a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
1d77b 20 2a 2a 20 20 20 31 29 20 74 68 69 73 20 69 73   **   1) this is
1d77c 20 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f   a read operatio
1d77d 6e 2c 20 61 6e 64 20 0a 20 20 20 20 20 20 20 20  n, and .        
1d77e 2a 2a 20 20 20 32 29 20 64 61 74 61 20 69 73 20  **   2) data is 
1d77f 72 65 71 75 69 72 65 64 20 66 72 6f 6d 20 74 68  required from th
1d780 65 20 73 74 61 72 74 20 6f 66 20 74 68 69 73 20  e start of this 
1d781 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 61  overflow page, a
1d782 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  nd.        **   
1d783 33 29 20 74 68 65 20 64 61 74 61 62 61 73 65 20  3) the database 
1d784 69 73 20 66 69 6c 65 2d 62 61 63 6b 65 64 2c 20  is file-backed, 
1d785 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 20  and.        **  
1d786 20 34 29 20 74 68 65 72 65 20 69 73 20 6e 6f 20   4) there is no 
1d787 6f 70 65 6e 20 77 72 69 74 65 2d 74 72 61 6e 73  open write-trans
1d788 61 63 74 69 6f 6e 2c 20 61 6e 64 0a 20 20 20 20  action, and.    
1d789 20 20 20 20 2a 2a 20 20 20 35 29 20 74 68 65 20      **   5) the 
1d78a 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20  database is not 
1d78b 61 20 57 41 4c 20 64 61 74 61 62 61 73 65 2c 0a  a WAL database,.
1d78c 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
1d78d 20 20 20 2a 2a 20 74 68 65 6e 20 64 61 74 61 20     ** then data 
1d78e 63 61 6e 20 62 65 20 72 65 61 64 20 64 69 72 65  can be read dire
1d78f 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 64 61  ctly from the da
1d790 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 74 6f  tabase file into
1d791 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
1d792 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c 20 62  output buffer, b
1d793 79 70 61 73 73 69 6e 67 20 74 68 65 20 70 61 67  ypassing the pag
1d794 65 2d 63 61 63 68 65 20 61 6c 74 6f 67 65 74 68  e-cache altogeth
1d795 65 72 2e 20 54 68 69 73 20 73 70 65 65 64 73 0a  er. This speeds.
1d796 20 20 20 20 20 20 20 20 2a 2a 20 75 70 20 6c 6f          ** up lo
1d797 61 64 69 6e 67 20 6c 61 72 67 65 20 72 65 63 6f  ading large reco
1d798 72 64 73 20 74 68 61 74 20 73 70 61 6e 20 6d 61  rds that span ma
1d799 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  ny overflow page
1d79a 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  s..        */.  
1d79b 20 20 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 30        if( eOp==0
1d79c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d79d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d79e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d79f 28 31 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  (1) */.         
1d7a0 26 26 20 6f 66 66 73 65 74 3d 3d 30 20 20 20 20  && offset==0    
1d7a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d7a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d7a3 20 20 20 20 20 20 2f 2a 20 28 32 29 20 2a 2f 0a        /* (2) */.
1d7a4 20 20 20 20 20 20 20 20 20 26 26 20 70 42 74 2d           && pBt-
1d7a5 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d  >inTransaction==
1d7a6 54 52 41 4e 53 5f 52 45 41 44 20 20 20 20 20 20  TRANS_READ      
1d7a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1d7a8 2a 20 28 34 29 20 2a 2f 0a 20 20 20 20 20 20 20  * (4) */.       
1d7a9 20 20 26 26 20 28 66 64 20 3d 20 73 71 6c 69 74    && (fd = sqlit
1d7aa 65 33 50 61 67 65 72 46 69 6c 65 28 70 42 74 2d  e3PagerFile(pBt-
1d7ab 3e 70 50 61 67 65 72 29 29 2d 3e 70 4d 65 74 68  >pPager))->pMeth
1d7ac 6f 64 73 20 20 20 20 20 2f 2a 20 28 33 29 20 2a  ods     /* (3) *
1d7ad 2f 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 42  /.         && pB
1d7ae 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
1d7af 5b 31 39 5d 3d 3d 30 78 30 31 20 20 20 20 20 20  [19]==0x01      
1d7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d7b1 20 2f 2a 20 28 35 29 20 2a 2f 0a 20 20 20 20 20   /* (5) */.     
1d7b2 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20     ){.          
1d7b3 75 38 20 61 53 61 76 65 5b 34 5d 3b 0a 20 20 20  u8 aSave[4];.   
1d7b4 20 20 20 20 20 20 20 75 38 20 2a 61 57 72 69 74         u8 *aWrit
1d7b5 65 20 3d 20 26 70 42 75 66 5b 2d 34 5d 3b 0a 20  e = &pBuf[-4];. 
1d7b6 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
1d7b7 61 53 61 76 65 2c 20 61 57 72 69 74 65 2c 20 34  aSave, aWrite, 4
1d7b8 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  );.          rc 
1d7b9 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
1d7ba 66 64 2c 20 61 57 72 69 74 65 2c 20 61 2b 34 2c  fd, aWrite, a+4,
1d7bb 20 28 69 36 34 29 70 42 74 2d 3e 70 61 67 65 53   (i64)pBt->pageS
1d7bc 69 7a 65 2a 28 6e 65 78 74 50 61 67 65 2d 31 29  ize*(nextPage-1)
1d7bd 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78  );.          nex
1d7be 74 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65  tPage = get4byte
1d7bf 28 61 57 72 69 74 65 29 3b 0a 20 20 20 20 20 20  (aWrite);.      
1d7c0 20 20 20 20 6d 65 6d 63 70 79 28 61 57 72 69 74      memcpy(aWrit
1d7c1 65 2c 20 61 53 61 76 65 2c 20 34 29 3b 0a 20 20  e, aSave, 4);.  
1d7c2 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64        }else.#end
1d7c3 69 66 0a 0a 20 20 20 20 20 20 20 20 7b 0a 20 20  if..        {.  
1d7c4 20 20 20 20 20 20 20 20 44 62 50 61 67 65 20 2a          DbPage *
1d7c5 70 44 62 50 61 67 65 3b 0a 20 20 20 20 20 20 20  pDbPage;.       
1d7c6 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1d7c7 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61  agerGet(pBt->pPa
1d7c8 67 65 72 2c 20 6e 65 78 74 50 61 67 65 2c 20 26  ger, nextPage, &
1d7c9 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
1d7ca 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1d7cb 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1d7cc 20 20 20 20 20 61 50 61 79 6c 6f 61 64 20 3d 20       aPayload = 
1d7cd 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
1d7ce 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ata(pDbPage);.  
1d7cf 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61            nextPa
1d7d0 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 61 50  ge = get4byte(aP
1d7d1 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 20  ayload);.       
1d7d2 20 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61       rc = copyPa
1d7d3 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b  yload(&aPayload[
1d7d4 6f 66 66 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c  offset+4], pBuf,
1d7d5 20 61 2c 20 65 4f 70 2c 20 70 44 62 50 61 67 65   a, eOp, pDbPage
1d7d6 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
1d7d7 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
1d7d8 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  (pDbPage);.     
1d7d9 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d 20         offset = 
1d7da 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  0;.          }. 
1d7db 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1d7dc 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 20 20 20   amt -= a;.     
1d7dd 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20     pBuf += a;.  
1d7de 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1d7df 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1d7e0 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b  E_OK && amt>0 ){
1d7e1 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1d7e2 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1d7e3 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1d7e4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
1d7e5 70 61 72 74 20 6f 66 20 74 68 65 20 6b 65 79 20  part of the key 
1d7e6 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1d7e7 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45 78  cursor pCur.  Ex
1d7e8 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 62  actly.** "amt" b
1d7e9 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72 61  ytes will be tra
1d7ea 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42 75  nsfered into pBu
1d7eb 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 66  f[].  The transf
1d7ec 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 20  er.** begins at 
1d7ed 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20  "offset"..**.** 
1d7ee 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20  The caller must 
1d7ef 65 6e 73 75 72 65 20 74 68 61 74 20 70 43 75 72  ensure that pCur
1d7f0 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20   is pointing to 
1d7f1 61 20 76 61 6c 69 64 20 72 6f 77 0a 2a 2a 20 69  a valid row.** i
1d7f2 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a  n the table..**.
1d7f3 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
1d7f4 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f  _OK on success o
1d7f5 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  r an error code 
1d7f6 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73  if anything goes
1d7f7 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65  .** wrong.  An e
1d7f8 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
1d7f9 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22   if "offset+amt"
1d7fa 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a   is larger than.
1d7fb 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65  ** the available
1d7fc 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c   payload..*/.SQL
1d7fd 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1d7fe 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28  sqlite3BtreeKey(
1d7ff 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
1d800 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
1d801 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29  amt, void *pBuf)
1d802 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  {.  assert( curs
1d803 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
1d804 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r) );.  assert( 
1d805 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
1d806 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20  RSOR_VALID );.  
1d807 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50  assert( pCur->iP
1d808 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e  age>=0 && pCur->
1d809 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1d80a 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge] );.  assert(
1d80b 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
1d80c 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e  r->iPage]<pCur->
1d80d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1d80e 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  ge]->nCell );.  
1d80f 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 79  return accessPay
1d810 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73 65  load(pCur, offse
1d811 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65  t, amt, (unsigne
1d812 64 20 63 68 61 72 2a 29 70 42 75 66 2c 20 30 29  d char*)pBuf, 0)
1d813 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
1d814 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61  part of the data
1d815 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1d816 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45   cursor pCur.  E
1d817 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20  xactly.** "amt" 
1d818 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72  bytes will be tr
1d819 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42  ansfered into pB
1d81a 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73  uf[].  The trans
1d81b 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74  fer.** begins at
1d81c 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a   "offset"..**.**
1d81d 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
1d81e 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20  K on success or 
1d81f 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66  an error code if
1d820 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a   anything goes.*
1d821 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72  * wrong.  An err
1d822 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  or is returned i
1d823 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20 69  f "offset+amt" i
1d824 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a 2a  s larger than.**
1d825 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 70   the available p
1d826 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54  ayload..*/.SQLIT
1d827 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1d828 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 42  lite3BtreeData(B
1d829 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75  tCursor *pCur, u
1d82a 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61  32 offset, u32 a
1d82b 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b  mt, void *pBuf){
1d82c 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 6e  .  int rc;..#ifn
1d82d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1d82e 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28 20  INCRBLOB.  if ( 
1d82f 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
1d830 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a  RSOR_INVALID ){.
1d831 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1d832 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65 6e  E_ABORT;.  }.#en
1d833 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 63  dif..  assert( c
1d834 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
1d835 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20  pCur) );.  rc = 
1d836 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73  restoreCursorPos
1d837 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69  ition(pCur);.  i
1d838 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1d839 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1d83a 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
1d83b 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20  RSOR_VALID );.  
1d83c 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1d83d 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72  iPage>=0 && pCur
1d83e 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
1d83f 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73 73  Page] );.    ass
1d840 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ert( pCur->aiIdx
1d841 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43  [pCur->iPage]<pC
1d842 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
1d843 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29  >iPage]->nCell )
1d844 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65 73  ;.    rc = acces
1d845 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f  sPayload(pCur, o
1d846 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75 66  ffset, amt, pBuf
1d847 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 0);.  }.  retu
1d848 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1d849 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
1d84a 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f   to payload info
1d84b 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65  rmation from the
1d84c 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20   entry that the 
1d84d 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 6f 72 20  .** pCur cursor 
1d84e 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20  is pointing to. 
1d84f 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20   The pointer is 
1d850 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  to the beginning
1d851 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 69   of.** the key i
1d852 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 61 6e 64  f skipKey==0 and
1d853 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68   it points to th
1d854 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 64  e beginning of d
1d855 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 70 4b 65  ata if.** skipKe
1d856 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 6d 62 65  y==1.  The numbe
1d857 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 61 76  r of bytes of av
1d858 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 61 74 61  ailable key/data
1d859 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69   is written.** i
1d85a 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 66 20 2a  nto *pAmt.  If *
1d85b 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e 20 74 68  pAmt==0, then th
1d85c 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
1d85d 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a 2a 2a 20   will not be.** 
1d85e 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 2e  a valid pointer.
1d85f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
1d860 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 69 6d 69  ine is an optimi
1d861 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 73 20 63  zation.  It is c
1d862 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 20 65 6e  ommon for the en
1d863 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 6e 64 20  tire key.** and 
1d864 64 61 74 61 20 74 6f 20 66 69 74 20 6f 6e 20 74  data to fit on t
1d865 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 20 61 6e  he local page an
1d866 64 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62  d for there to b
1d867 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a  e no overflow.**
1d868 20 70 61 67 65 73 2e 20 20 57 68 65 6e 20 74 68   pages.  When th
1d869 61 74 20 69 73 20 73 6f 2c 20 74 68 69 73 20 72  at is so, this r
1d86a 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73  outine can be us
1d86b 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  ed to access the
1d86c 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 74 61  .** key and data
1d86d 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 6e 67 20   without making 
1d86e 61 20 63 6f 70 79 2e 20 20 49 66 20 74 68 65 20  a copy.  If the 
1d86f 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 74 61 20  key and/or data 
1d870 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f 20 6f  spills.** onto o
1d871 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74  verflow pages, t
1d872 68 65 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61  hen accessPayloa
1d873 64 28 29 20 6d 75 73 74 20 62 65 20 75 73 65 64  d() must be used
1d874 20 74 6f 20 72 65 61 73 73 65 6d 62 6c 65 0a 2a   to reassemble.*
1d875 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 61 20 61  * the key/data a
1d876 6e 64 20 63 6f 70 79 20 69 74 20 69 6e 74 6f 20  nd copy it into 
1d877 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 20 62  a preallocated b
1d878 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  uffer..**.** The
1d879 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65   pointer returne
1d87a 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e  d by this routin
1d87b 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 74 6c 79  e looks directly
1d87c 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65 64   into the cached
1d87d 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 68 65 20  .** page of the 
1d87e 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 64  database.  The d
1d87f 61 74 61 20 6d 69 67 68 74 20 63 68 61 6e 67 65  ata might change
1d880 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 6e 65 78   or move the nex
1d881 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 20 62 74  t time.** any bt
1d882 72 65 65 20 72 6f 75 74 69 6e 65 20 69 73 20 63  ree routine is c
1d883 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  alled..*/.static
1d884 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
1d885 63 68 61 72 20 2a 66 65 74 63 68 50 61 79 6c 6f  char *fetchPaylo
1d886 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  ad(.  BtCursor *
1d887 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75  pCur,      /* Cu
1d888 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f  rsor pointing to
1d889 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66   entry to read f
1d88a 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 41  rom */.  int *pA
1d88b 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  mt,           /*
1d88c 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65   Write the numbe
1d88d 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62  r of available b
1d88e 79 74 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 69  ytes here */.  i
1d88f 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 20 20 20  nt skipKey      
1d890 20 20 20 20 2f 2a 20 72 65 61 64 20 62 65 67 69      /* read begi
1d891 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 20 69 66  nning at data if
1d892 20 74 68 69 73 20 69 73 20 74 72 75 65 20 2a 2f   this is true */
1d893 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  .){.  unsigned c
1d894 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20  har *aPayload;. 
1d895 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
1d896 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20 75  .  u32 nKey;.  u
1d897 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 20 61 73  32 nLocal;..  as
1d898 73 65 72 74 28 20 70 43 75 72 21 3d 30 20 26 26  sert( pCur!=0 &&
1d899 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20   pCur->iPage>=0 
1d89a 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  && pCur->apPage[
1d89b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a 20  pCur->iPage]);. 
1d89c 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
1d89d 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
1d89e 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28  LID );.  assert(
1d89f 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
1d8a0 78 28 70 43 75 72 29 20 29 3b 0a 20 20 70 50 61  x(pCur) );.  pPa
1d8a1 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
1d8a2 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  e[pCur->iPage];.
1d8a3 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1d8a4 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
1d8a5 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  e]<pPage->nCell 
1d8a6 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  );.  if( NEVER(p
1d8a7 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d  Cur->info.nSize=
1d8a8 3d 30 29 20 29 7b 0a 20 20 20 20 62 74 72 65 65  =0) ){.    btree
1d8a9 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e  ParseCell(pCur->
1d8aa 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1d8ab 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 78  ge], pCur->aiIdx
1d8ac 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2c 0a 20  [pCur->iPage],. 
1d8ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d8ae 20 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a    &pCur->info);.
1d8af 20 20 7d 0a 20 20 61 50 61 79 6c 6f 61 64 20 3d    }.  aPayload =
1d8b0 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 43 65 6c   pCur->info.pCel
1d8b1 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 64 20 2b 3d  l;.  aPayload +=
1d8b2 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61   pCur->info.nHea
1d8b3 64 65 72 3b 0a 20 20 69 66 28 20 70 50 61 67 65  der;.  if( pPage
1d8b4 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20  ->intKey ){.    
1d8b5 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  nKey = 0;.  }els
1d8b6 65 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 28 69  e{.    nKey = (i
1d8b7 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b  nt)pCur->info.nK
1d8b8 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 6b  ey;.  }.  if( sk
1d8b9 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 50 61  ipKey ){.    aPa
1d8ba 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a 20  yload += nKey;. 
1d8bb 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72     nLocal = pCur
1d8bc 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20  ->info.nLocal - 
1d8bd 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  nKey;.  }else{. 
1d8be 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75 72     nLocal = pCur
1d8bf 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20  ->info.nLocal;. 
1d8c0 20 20 20 61 73 73 65 72 74 28 20 6e 4c 6f 63 61     assert( nLoca
1d8c1 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20 20 7d 0a 20  l<=nKey );.  }. 
1d8c2 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63 61 6c 3b   *pAmt = nLocal;
1d8c3 0a 20 20 72 65 74 75 72 6e 20 61 50 61 79 6c 6f  .  return aPaylo
1d8c4 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f  ad;.}.../*.** Fo
1d8c5 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  r the entry that
1d8c6 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 20   cursor pCur is 
1d8c7 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 75 72 6e  point to, return
1d8c8 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 79 74 65   as.** many byte
1d8c9 73 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 72 20  s of the key or 
1d8ca 64 61 74 61 20 61 73 20 61 72 65 20 61 76 61 69  data as are avai
1d8cb 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63  lable on the loc
1d8cc 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 70 61 67  al.** b-tree pag
1d8cd 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 6e 75  e.  Write the nu
1d8ce 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c  mber of availabl
1d8cf 65 20 62 79 74 65 73 20 69 6e 74 6f 20 2a 70 41  e bytes into *pA
1d8d0 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f  mt..**.** The po
1d8d1 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20 69  inter returned i
1d8d2 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 54 68  s ephemeral.  Th
1d8d3 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 79 20 6d  e key/data may m
1d8d4 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 64 65 73  ove.** or be des
1d8d5 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 20 6e 65  troyed on the ne
1d8d6 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20 42  xt call to any B
1d8d7 74 72 65 65 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a  tree routine,.**
1d8d8 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73   including calls
1d8d9 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 68 72 65   from other thre
1d8da 61 64 73 20 61 67 61 69 6e 73 74 20 74 68 65 20  ads against the 
1d8db 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a 2a 20 48  same cache..** H
1d8dc 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 20 6f 6e  ence, a mutex on
1d8dd 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 68   the BtShared sh
1d8de 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 70 72 69  ould be held pri
1d8df 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a  or to calling.**
1d8e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
1d8e1 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
1d8e2 6e 65 73 20 69 73 20 75 73 65 64 20 74 6f 20 67  nes is used to g
1d8e3 65 74 20 71 75 69 63 6b 20 61 63 63 65 73 73 20  et quick access 
1d8e4 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 74 61 0a  to key and data.
1d8e5 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e  ** in the common
1d8e6 20 63 61 73 65 20 77 68 65 72 65 20 6e 6f 20 6f   case where no o
1d8e7 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 72  verflow pages ar
1d8e8 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  e used..*/.SQLIT
1d8e9 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
1d8ea 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72  void *sqlite3Btr
1d8eb 65 65 4b 65 79 46 65 74 63 68 28 42 74 43 75 72  eeKeyFetch(BtCur
1d8ec 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  sor *pCur, int *
1d8ed 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20 76  pAmt){.  const v
1d8ee 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61 73  oid *p = 0;.  as
1d8ef 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1d8f0 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70  tex_held(pCur->p
1d8f1 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78  Btree->db->mutex
1d8f2 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63  ) );.  assert( c
1d8f3 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
1d8f4 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 41  pCur) );.  if( A
1d8f5 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74 61  LWAYS(pCur->eSta
1d8f6 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
1d8f7 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63 6f  ) ){.    p = (co
1d8f8 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50  nst void*)fetchP
1d8f9 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d  ayload(pCur, pAm
1d8fa 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74  t, 0);.  }.  ret
1d8fb 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49 54 45 5f  urn p;.}.SQLITE_
1d8fc 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f  PRIVATE const vo
1d8fd 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  id *sqlite3Btree
1d8fe 44 61 74 61 46 65 74 63 68 28 42 74 43 75 72 73  DataFetch(BtCurs
1d8ff 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70  or *pCur, int *p
1d900 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f  Amt){.  const vo
1d901 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61 73 73  id *p = 0;.  ass
1d902 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1d903 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42  ex_held(pCur->pB
1d904 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  tree->db->mutex)
1d905 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75   );.  assert( cu
1d906 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
1d907 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 41 4c  Cur) );.  if( AL
1d908 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74 61 74  WAYS(pCur->eStat
1d909 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 29  e==CURSOR_VALID)
1d90a 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63 6f 6e   ){.    p = (con
1d90b 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50 61  st void*)fetchPa
1d90c 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d 74  yload(pCur, pAmt
1d90d 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 1);.  }.  retu
1d90e 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  rn p;.}.../*.** 
1d90f 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
1d910 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20 63 68  down to a new ch
1d911 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65 20 6e  ild page.  The n
1d912 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20  ewPgno argument 
1d913 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e  is the.** page n
1d914 75 6d 62 65 72 20 6f 66 20 74 68 65 20 63 68 69  umber of the chi
1d915 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20  ld page to move 
1d916 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  to..**.** This f
1d917 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
1d918 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69  SQLITE_CORRUPT i
1d919 66 20 74 68 65 20 70 61 67 65 2d 68 65 61 64 65  f the page-heade
1d91a 72 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66  r flags field of
1d91b 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 68 69 6c  .** the new chil
1d91c 64 20 70 61 67 65 20 64 6f 65 73 20 6e 6f 74 20  d page does not 
1d91d 6d 61 74 63 68 20 74 68 65 20 66 6c 61 67 73 20  match the flags 
1d91e 66 69 65 6c 64 20 6f 66 20 74 68 65 20 70 61 72  field of the par
1d91f 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20 69 66 20  ent (i.e..** if 
1d920 61 6e 20 69 6e 74 6b 65 79 20 70 61 67 65 20 61  an intkey page a
1d921 70 70 65 61 72 73 20 74 6f 20 62 65 20 74 68 65  ppears to be the
1d922 20 70 61 72 65 6e 74 20 6f 66 20 61 20 6e 6f 6e   parent of a non
1d923 2d 69 6e 74 6b 65 79 20 70 61 67 65 2c 20 6f 72  -intkey page, or
1d924 0a 2a 2a 20 76 69 63 65 2d 76 65 72 73 61 29 2e  .** vice-versa).
1d925 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
1d926 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 43 75 72  oveToChild(BtCur
1d927 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6e  sor *pCur, u32 n
1d928 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72  ewPgno){.  int r
1d929 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70 43 75  c;.  int i = pCu
1d92a 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d 65 6d 50  r->iPage;.  MemP
1d92b 61 67 65 20 2a 70 4e 65 77 50 61 67 65 3b 0a 20  age *pNewPage;. 
1d92c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1d92d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a 20 20 61   pCur->pBt;..  a
1d92e 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
1d92f 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
1d930 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
1d931 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
1d932 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72  VALID );.  asser
1d933 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3c 42  t( pCur->iPage<B
1d934 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54  TCURSOR_MAX_DEPT
1d935 48 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d  H );.  if( pCur-
1d936 3e 69 50 61 67 65 3e 3d 28 42 54 43 55 52 53 4f  >iPage>=(BTCURSO
1d937 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31 29 20 29  R_MAX_DEPTH-1) )
1d938 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1d939 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1d93a 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 67 65 74  ;.  }.  rc = get
1d93b 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c  AndInitPage(pBt,
1d93c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e 65 77 50   newPgno, &pNewP
1d93d 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  age);.  if( rc )
1d93e 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 70 43   return rc;.  pC
1d93f 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b 31 5d 20  ur->apPage[i+1] 
1d940 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20 20 70 43  = pNewPage;.  pC
1d941 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31 5d 20 3d  ur->aiIdx[i+1] =
1d942 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67   0;.  pCur->iPag
1d943 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e  e++;..  pCur->in
1d944 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
1d945 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
1d946 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e 65 77 50  = 0;.  if( pNewP
1d947 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 7c 7c 20  age->nCell<1 || 
1d948 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74 4b 65 79  pNewPage->intKey
1d949 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69  !=pCur->apPage[i
1d94a 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  ]->intKey ){.   
1d94b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1d94c 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
1d94d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1d94e 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 30 0a 2f 2a  _OK;.}..#if 0./*
1d94f 0a 2a 2a 20 50 61 67 65 20 70 50 61 72 65 6e 74  .** Page pParent
1d950 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20   is an internal 
1d951 28 6e 6f 6e 2d 6c 65 61 66 29 20 74 72 65 65 20  (non-leaf) tree 
1d952 70 61 67 65 2e 20 54 68 69 73 20 66 75 6e 63 74  page. This funct
1d953 69 6f 6e 20 0a 2a 2a 20 61 73 73 65 72 74 73 20  ion .** asserts 
1d954 74 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72  that page number
1d955 20 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 6c   iChild is the l
1d956 65 66 74 2d 63 68 69 6c 64 20 69 66 20 74 68 65  eft-child if the
1d957 20 69 49 64 78 27 74 68 0a 2a 2a 20 63 65 6c 6c   iIdx'th.** cell
1d958 20 69 6e 20 70 61 67 65 20 70 50 61 72 65 6e 74   in page pParent
1d959 2e 20 4f 72 2c 20 69 66 20 69 49 64 78 20 69 73  . Or, if iIdx is
1d95a 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74 6f   equal to the to
1d95b 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  tal number of.**
1d95c 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e   cells in pParen
1d95d 74 2c 20 74 68 61 74 20 70 61 67 65 20 6e 75 6d  t, that page num
1d95e 62 65 72 20 69 43 68 69 6c 64 20 69 73 20 74 68  ber iChild is th
1d95f 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66  e right-child of
1d960 0a 2a 2a 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f  .** the page..*/
1d961 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73  .static void ass
1d962 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 4d  ertParentIndex(M
1d963 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c  emPage *pParent,
1d964 20 69 6e 74 20 69 49 64 78 2c 20 50 67 6e 6f 20   int iIdx, Pgno 
1d965 69 43 68 69 6c 64 29 7b 0a 20 20 61 73 73 65 72  iChild){.  asser
1d966 74 28 20 69 49 64 78 3c 3d 70 50 61 72 65 6e 74  t( iIdx<=pParent
1d967 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 69 66 28  ->nCell );.  if(
1d968 20 69 49 64 78 3d 3d 70 50 61 72 65 6e 74 2d 3e   iIdx==pParent->
1d969 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 61 73 73  nCell ){.    ass
1d96a 65 72 74 28 20 67 65 74 34 62 79 74 65 28 26 70  ert( get4byte(&p
1d96b 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50  Parent->aData[pP
1d96c 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74  arent->hdrOffset
1d96d 2b 38 5d 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a  +8])==iChild );.
1d96e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
1d96f 65 72 74 28 20 67 65 74 34 62 79 74 65 28 66 69  ert( get4byte(fi
1d970 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  ndCell(pParent, 
1d971 69 49 64 78 29 29 3d 3d 69 43 68 69 6c 64 20 29  iIdx))==iChild )
1d972 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20  ;.  }.}.#else.# 
1d973 20 64 65 66 69 6e 65 20 61 73 73 65 72 74 50 61   define assertPa
1d974 72 65 6e 74 49 6e 64 65 78 28 78 2c 79 2c 7a 29  rentIndex(x,y,z)
1d975 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20   .#endif../*.** 
1d976 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
1d977 75 70 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74  up to the parent
1d978 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75   page..**.** pCu
1d979 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 74 6f  r->idx is set to
1d97a 20 74 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 20   the cell index 
1d97b 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
1d97c 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  e pointer.** to 
1d97d 74 68 65 20 70 61 67 65 20 77 65 20 61 72 65 20  the page we are 
1d97e 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e 20 20 49 66  coming from.  If
1d97f 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 66   we are coming f
1d980 72 6f 6d 20 74 68 65 0a 2a 2a 20 72 69 67 68 74  rom the.** right
1d981 2d 6d 6f 73 74 20 63 68 69 6c 64 20 70 61 67 65  -most child page
1d982 20 74 68 65 6e 20 70 43 75 72 2d 3e 69 64 78 20   then pCur->idx 
1d983 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 6d 6f  is set to one mo
1d984 72 65 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 6c  re than.** the l
1d985 61 72 67 65 73 74 20 63 65 6c 6c 20 69 6e 64 65  argest cell inde
1d986 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  x..*/.static voi
1d987 64 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42  d moveToParent(B
1d988 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
1d989 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
1d98a 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
1d98b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
1d98c 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
1d98d 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73  OR_VALID );.  as
1d98e 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67  sert( pCur->iPag
1d98f 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  e>0 );.  assert(
1d990 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
1d991 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 0a 20  ur->iPage] );.. 
1d992 20 2f 2a 20 55 50 44 41 54 45 3a 20 49 74 20 69   /* UPDATE: It i
1d993 73 20 61 63 74 75 61 6c 6c 79 20 70 6f 73 73 69  s actually possi
1d994 62 6c 65 20 66 6f 72 20 74 68 65 20 63 6f 6e 64  ble for the cond
1d995 69 74 69 6f 6e 20 74 65 73 74 65 64 20 62 79 20  ition tested by 
1d996 74 68 65 20 61 73 73 65 72 74 0a 20 20 2a 2a 20  the assert.  ** 
1d997 62 65 6c 6f 77 20 74 6f 20 62 65 20 75 6e 74 72  below to be untr
1d998 75 65 20 69 66 20 74 68 65 20 64 61 74 61 62 61  ue if the databa
1d999 73 65 20 66 69 6c 65 20 69 73 20 63 6f 72 72 75  se file is corru
1d99a 70 74 2e 20 54 68 69 73 20 63 61 6e 20 6f 63 63  pt. This can occ
1d99b 75 72 20 69 66 0a 20 20 2a 2a 20 6f 6e 65 20 63  ur if.  ** one c
1d99c 75 72 73 6f 72 20 68 61 73 20 6d 6f 64 69 66 69  ursor has modifi
1d99d 65 64 20 70 61 67 65 20 70 50 61 72 65 6e 74 20  ed page pParent 
1d99e 77 68 69 6c 65 20 61 20 72 65 66 65 72 65 6e 63  while a referenc
1d99f 65 20 74 6f 20 69 74 20 69 73 20 68 65 6c 64 20  e to it is held 
1d9a0 0a 20 20 2a 2a 20 62 79 20 61 20 73 65 63 6f 6e  .  ** by a secon
1d9a1 64 20 63 75 72 73 6f 72 2e 20 57 68 69 63 68 20  d cursor. Which 
1d9a2 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20  can only happen 
1d9a3 69 66 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65  if a single page
1d9a4 20 69 73 20 6c 69 6e 6b 65 64 0a 20 20 2a 2a 20   is linked.  ** 
1d9a5 69 6e 74 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f  into more than o
1d9a6 6e 65 20 62 2d 74 72 65 65 20 73 74 72 75 63 74  ne b-tree struct
1d9a7 75 72 65 20 69 6e 20 61 20 63 6f 72 72 75 70 74  ure in a corrupt
1d9a8 20 64 61 74 61 62 61 73 65 2e 20 20 2a 2f 0a 23   database.  */.#
1d9a9 69 66 20 30 0a 20 20 61 73 73 65 72 74 50 61 72  if 0.  assertPar
1d9aa 65 6e 74 49 6e 64 65 78 28 0a 20 20 20 20 70 43  entIndex(.    pC
1d9ab 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
1d9ac 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20  >iPage-1], .    
1d9ad 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
1d9ae 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20  ->iPage-1], .   
1d9af 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
1d9b0 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 70 67 6e 6f  ur->iPage]->pgno
1d9b1 0a 20 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 74  .  );.#endif.  t
1d9b2 65 73 74 63 61 73 65 28 20 70 43 75 72 2d 3e 61  estcase( pCur->a
1d9b3 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
1d9b4 2d 31 5d 20 3e 20 70 43 75 72 2d 3e 61 70 50 61  -1] > pCur->apPa
1d9b5 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31  ge[pCur->iPage-1
1d9b6 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 72  ]->nCell );..  r
1d9b7 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d  eleasePage(pCur-
1d9b8 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
1d9b9 61 67 65 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69  age]);.  pCur->i
1d9ba 50 61 67 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e  Page--;.  pCur->
1d9bb 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a  info.nSize = 0;.
1d9bc 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
1d9bd 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  y = 0;.}../*.** 
1d9be 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
1d9bf 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
1d9c0 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 69 74 73  root page of its
1d9c1 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72   b-tree structur
1d9c2 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
1d9c3 74 61 62 6c 65 20 68 61 73 20 61 20 76 69 72 74  table has a virt
1d9c4 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2c 20 74  ual root page, t
1d9c5 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69  hen the cursor i
1d9c6 73 20 6d 6f 76 65 64 20 74 6f 20 70 6f 69 6e 74  s moved to point
1d9c7 0a 2a 2a 20 74 6f 20 74 68 65 20 76 69 72 74 75  .** to the virtu
1d9c8 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 73  al root page ins
1d9c9 74 65 61 64 20 6f 66 20 74 68 65 20 61 63 74 75  tead of the actu
1d9ca 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 41 20  al root page. A 
1d9cb 74 61 62 6c 65 20 68 61 73 20 61 0a 2a 2a 20 76  table has a.** v
1d9cc 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65  irtual root page
1d9cd 20 77 68 65 6e 20 74 68 65 20 61 63 74 75 61 6c   when the actual
1d9ce 20 72 6f 6f 74 20 70 61 67 65 20 63 6f 6e 74 61   root page conta
1d9cf 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 20 61 6e 64  ins no cells and
1d9d0 20 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 63 68   a .** single ch
1d9d1 69 6c 64 20 70 61 67 65 2e 20 54 68 69 73 20 63  ild page. This c
1d9d2 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 77  an only happen w
1d9d3 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 72 6f  ith the table ro
1d9d4 6f 74 65 64 20 61 74 20 70 61 67 65 20 31 2e 0a  oted at page 1..
1d9d5 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 2d 74  **.** If the b-t
1d9d6 72 65 65 20 73 74 72 75 63 74 75 72 65 20 69 73  ree structure is
1d9d7 20 65 6d 70 74 79 2c 20 74 68 65 20 63 75 72 73   empty, the curs
1d9d8 6f 72 20 73 74 61 74 65 20 69 73 20 73 65 74 20  or state is set 
1d9d9 74 6f 20 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e  to .** CURSOR_IN
1d9da 56 41 4c 49 44 2e 20 4f 74 68 65 72 77 69 73 65  VALID. Otherwise
1d9db 2c 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  , the cursor is 
1d9dc 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  set to point to 
1d9dd 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 63 65 6c  the first.** cel
1d9de 6c 20 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65  l located on the
1d9df 20 72 6f 6f 74 20 28 6f 72 20 76 69 72 74 75 61   root (or virtua
1d9e0 6c 20 72 6f 6f 74 29 20 70 61 67 65 20 61 6e 64  l root) page and
1d9e1 20 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74   the cursor stat
1d9e2 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 43  e.** is set to C
1d9e3 55 52 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a  URSOR_VALID..**.
1d9e4 2a 2a 20 49 66 20 74 68 69 73 20 66 75 6e 63 74  ** If this funct
1d9e5 69 6f 6e 20 72 65 74 75 72 6e 73 20 73 75 63 63  ion returns succ
1d9e6 65 73 73 66 75 6c 6c 79 2c 20 69 74 20 6d 61 79  essfully, it may
1d9e7 20 62 65 20 61 73 73 75 6d 65 64 20 74 68 61 74   be assumed that
1d9e8 20 74 68 65 0a 2a 2a 20 70 61 67 65 2d 68 65 61   the.** page-hea
1d9e9 64 65 72 20 66 6c 61 67 73 20 69 6e 64 69 63 61  der flags indica
1d9ea 74 65 20 74 68 61 74 20 74 68 65 20 5b 76 69 72  te that the [vir
1d9eb 74 75 61 6c 5d 20 72 6f 6f 74 2d 70 61 67 65 20  tual] root-page 
1d9ec 69 73 20 74 68 65 20 65 78 70 65 63 74 65 64 20  is the expected 
1d9ed 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 62 2d 74 72  .** kind of b-tr
1d9ee 65 65 20 70 61 67 65 20 28 69 2e 65 2e 20 69 66  ee page (i.e. if
1d9ef 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 74 68   when opening th
1d9f0 65 20 63 75 72 73 6f 72 20 74 68 65 20 63 61 6c  e cursor the cal
1d9f1 6c 65 72 20 64 69 64 20 6e 6f 74 0a 2a 2a 20 73  ler did not.** s
1d9f2 70 65 63 69 66 79 20 61 20 4b 65 79 49 6e 66 6f  pecify a KeyInfo
1d9f3 20 73 74 72 75 63 74 75 72 65 20 74 68 65 20 66   structure the f
1d9f4 6c 61 67 73 20 62 79 74 65 20 69 73 20 73 65 74  lags byte is set
1d9f5 20 74 6f 20 30 78 30 35 20 6f 72 20 30 78 30 44   to 0x05 or 0x0D
1d9f6 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20  ,.** indicating 
1d9f7 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2c 20  a table b-tree, 
1d9f8 6f 72 20 69 66 20 74 68 65 20 63 61 6c 6c 65 72  or if the caller
1d9f9 20 64 69 64 20 73 70 65 63 69 66 79 20 61 20 4b   did specify a K
1d9fa 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63  eyInfo .** struc
1d9fb 74 75 72 65 20 74 68 65 20 66 6c 61 67 73 20 62  ture the flags b
1d9fc 79 74 65 20 69 73 20 73 65 74 20 74 6f 20 30 78  yte is set to 0x
1d9fd 30 32 20 6f 72 20 30 78 30 41 2c 20 69 6e 64 69  02 or 0x0A, indi
1d9fe 63 61 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 0a  cating an index.
1d9ff 2a 2a 20 62 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73  ** b-tree)..*/.s
1da00 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f  tatic int moveTo
1da01 52 6f 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70  Root(BtCursor *p
1da02 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20  Cur){.  MemPage 
1da03 2a 70 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63  *pRoot;.  int rc
1da04 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1da05 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d  Btree *p = pCur-
1da06 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61  >pBtree;.  BtSha
1da07 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
1da08 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  t;..  assert( cu
1da09 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
1da0a 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
1da0b 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  ( CURSOR_INVALID
1da0c 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52   < CURSOR_REQUIR
1da0d 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72  ESEEK );.  asser
1da0e 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  t( CURSOR_VALID 
1da0f 20 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49    < CURSOR_REQUI
1da10 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65  RESEEK );.  asse
1da11 72 74 28 20 43 55 52 53 4f 52 5f 46 41 55 4c 54  rt( CURSOR_FAULT
1da12 20 20 20 3e 20 43 55 52 53 4f 52 5f 52 45 51 55     > CURSOR_REQU
1da13 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28  IRESEEK );.  if(
1da14 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43   pCur->eState>=C
1da15 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
1da16 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75  K ){.    if( pCu
1da17 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
1da18 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 20  R_FAULT ){.     
1da19 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73   assert( pCur->s
1da1a 6b 69 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f  kipNext!=SQLITE_
1da1b 4f 4b 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75  OK );.      retu
1da1c 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78  rn pCur->skipNex
1da1d 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  t;.    }.    sql
1da1e 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75  ite3BtreeClearCu
1da1f 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 7d 0a  rsor(pCur);.  }.
1da20 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61  .  if( pCur->iPa
1da21 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ge>=0 ){.    int
1da22 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b   i;.    for(i=1;
1da23 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b   i<=pCur->iPage;
1da24 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c   i++){.      rel
1da25 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61  easePage(pCur->a
1da26 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d  pPage[i]);.    }
1da27 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65  .    pCur->iPage
1da28 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
1da29 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74  ( pCur->pgnoRoot
1da2a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d  ==0 ){.    pCur-
1da2b 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52  >eState = CURSOR
1da2c 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 72 65  _INVALID;.    re
1da2d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1da2e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
1da2f 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65  = getAndInitPage
1da30 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f  (pBt, pCur->pgno
1da31 52 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 61 70 50  Root, &pCur->apP
1da32 61 67 65 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28  age[0]);.    if(
1da33 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1da34 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53  {.      pCur->eS
1da35 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e  tate = CURSOR_IN
1da36 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 72 65 74  VALID;.      ret
1da37 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
1da38 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20    pCur->iPage = 
1da39 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 43  0;..    /* If pC
1da3a 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 69 73 20  ur->pKeyInfo is 
1da3b 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74  not NULL, then t
1da3c 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74 20 6f  he caller that o
1da3d 70 65 6e 65 64 20 74 68 69 73 20 63 75 72 73 6f  pened this curso
1da3e 72 0a 20 20 20 20 2a 2a 20 65 78 70 65 63 74 65  r.    ** expecte
1da3f 64 20 74 6f 20 6f 70 65 6e 20 69 74 20 6f 6e 20  d to open it on 
1da40 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2e  an index b-tree.
1da41 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 70   Otherwise, if p
1da42 4b 65 79 49 6e 66 6f 20 69 73 0a 20 20 20 20 2a  KeyInfo is.    *
1da43 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 63 61 6c 6c  * NULL, the call
1da44 65 72 20 65 78 70 65 63 74 73 20 61 20 74 61 62  er expects a tab
1da45 6c 65 20 62 2d 74 72 65 65 2e 20 49 66 20 74 68  le b-tree. If th
1da46 69 73 20 69 73 20 6e 6f 74 20 74 68 65 20 63 61  is is not the ca
1da47 73 65 2c 0a 20 20 20 20 2a 2a 20 72 65 74 75 72  se,.    ** retur
1da48 6e 20 61 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  n an SQLITE_CORR
1da49 55 50 54 20 65 72 72 6f 72 2e 20 20 2a 2f 0a 20  UPT error.  */. 
1da4a 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
1da4b 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b  >apPage[0]->intK
1da4c 65 79 3d 3d 31 20 7c 7c 20 70 43 75 72 2d 3e 61  ey==1 || pCur->a
1da4d 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79  pPage[0]->intKey
1da4e 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28  ==0 );.    if( (
1da4f 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d  pCur->pKeyInfo==
1da50 30 29 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 65  0)!=pCur->apPage
1da51 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  [0]->intKey ){. 
1da52 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1da53 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1da54 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1da55 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65   Assert that the
1da56 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 6f 66   root page is of
1da57 20 74 68 65 20 63 6f 72 72 65 63 74 20 74 79 70   the correct typ
1da58 65 2e 20 54 68 69 73 20 6d 75 73 74 20 62 65 20  e. This must be 
1da59 74 68 65 0a 20 20 2a 2a 20 63 61 73 65 20 61 73  the.  ** case as
1da5a 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 68 69   the call to thi
1da5b 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  s function that 
1da5c 6c 6f 61 64 65 64 20 74 68 65 20 72 6f 6f 74 2d  loaded the root-
1da5d 70 61 67 65 20 28 65 69 74 68 65 72 0a 20 20 2a  page (either.  *
1da5e 2a 20 74 68 69 73 20 63 61 6c 6c 20 6f 72 20 61  * this call or a
1da5f 20 70 72 65 76 69 6f 75 73 20 69 6e 76 6f 63 61   previous invoca
1da60 74 69 6f 6e 29 20 77 6f 75 6c 64 20 68 61 76 65  tion) would have
1da61 20 64 65 74 65 63 74 65 64 20 63 6f 72 72 75 70   detected corrup
1da62 74 69 6f 6e 20 0a 20 20 2a 2a 20 69 66 20 74 68  tion .  ** if th
1da63 65 20 61 73 73 75 6d 70 74 69 6f 6e 20 77 65 72  e assumption wer
1da64 65 20 6e 6f 74 20 74 72 75 65 2c 20 61 6e 64 20  e not true, and 
1da65 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  it is not possib
1da66 6c 65 20 66 6f 72 20 74 68 65 20 66 6c 61 67 73  le for the flags
1da67 20 0a 20 20 2a 2a 20 62 79 74 65 20 74 6f 20 68   .  ** byte to h
1da68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  ave been modifie
1da69 64 20 77 68 69 6c 65 20 74 68 69 73 20 63 75 72  d while this cur
1da6a 73 6f 72 20 69 73 20 68 6f 6c 64 69 6e 67 20 61  sor is holding a
1da6b 20 72 65 66 65 72 65 6e 63 65 0a 20 20 2a 2a 20   reference.  ** 
1da6c 74 6f 20 74 68 65 20 70 61 67 65 2e 20 20 2a 2f  to the page.  */
1da6d 0a 20 20 70 52 6f 6f 74 20 3d 20 70 43 75 72 2d  .  pRoot = pCur-
1da6e 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20 61 73  >apPage[0];.  as
1da6f 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 70 67 6e  sert( pRoot->pgn
1da70 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f  o==pCur->pgnoRoo
1da71 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t );.  assert( p
1da72 52 6f 6f 74 2d 3e 69 73 49 6e 69 74 20 26 26 20  Root->isInit && 
1da73 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d  (pCur->pKeyInfo=
1da74 3d 30 29 3d 3d 70 52 6f 6f 74 2d 3e 69 6e 74 4b  =0)==pRoot->intK
1da75 65 79 20 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 61  ey );..  pCur->a
1da76 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 70  iIdx[0] = 0;.  p
1da77 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20  Cur->info.nSize 
1da78 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 61 74 4c  = 0;.  pCur->atL
1da79 61 73 74 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d  ast = 0;.  pCur-
1da7a 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a  >validNKey = 0;.
1da7b 0a 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 6e 43  .  if( pRoot->nC
1da7c 65 6c 6c 3d 3d 30 20 26 26 20 21 70 52 6f 6f 74  ell==0 && !pRoot
1da7d 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50 67  ->leaf ){.    Pg
1da7e 6e 6f 20 73 75 62 70 61 67 65 3b 0a 20 20 20 20  no subpage;.    
1da7f 69 66 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 21  if( pRoot->pgno!
1da80 3d 31 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =1 ) return SQLI
1da81 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1da82 0a 20 20 20 20 73 75 62 70 61 67 65 20 3d 20 67  .    subpage = g
1da83 65 74 34 62 79 74 65 28 26 70 52 6f 6f 74 2d 3e  et4byte(&pRoot->
1da84 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 72  aData[pRoot->hdr
1da85 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
1da86 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43  pCur->eState = C
1da87 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 20  URSOR_VALID;.   
1da88 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c   rc = moveToChil
1da89 64 28 70 43 75 72 2c 20 73 75 62 70 61 67 65 29  d(pCur, subpage)
1da8a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
1da8b 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 28 28  Cur->eState = ((
1da8c 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e 30 29 3f  pRoot->nCell>0)?
1da8d 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 43 55 52  CURSOR_VALID:CUR
1da8e 53 4f 52 5f 49 4e 56 41 4c 49 44 29 3b 0a 20 20  SOR_INVALID);.  
1da8f 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1da90 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65  ../*.** Move the
1da91 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20   cursor down to 
1da92 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65  the left-most le
1da93 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68  af entry beneath
1da94 20 74 68 65 0a 2a 2a 20 65 6e 74 72 79 20 74 6f   the.** entry to
1da95 20 77 68 69 63 68 20 69 74 20 69 73 20 63 75 72   which it is cur
1da96 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e  rently pointing.
1da97 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d  .**.** The left-
1da98 6d 6f 73 74 20 6c 65 61 66 20 69 73 20 74 68 65  most leaf is the
1da99 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20 73 6d   one with the sm
1da9a 61 6c 6c 65 73 74 20 6b 65 79 20 2d 20 74 68 65  allest key - the
1da9b 20 66 69 72 73 74 0a 2a 2a 20 69 6e 20 61 73 63   first.** in asc
1da9c 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f  ending order..*/
1da9d 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65  .static int move
1da9e 54 6f 4c 65 66 74 6d 6f 73 74 28 42 74 43 75 72  ToLeftmost(BtCur
1da9f 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67  sor *pCur){.  Pg
1daa0 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72  no pgno;.  int r
1daa1 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1daa2 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
1daa3 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ..  assert( curs
1daa4 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
1daa5 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r) );.  assert( 
1daa6 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
1daa7 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20  RSOR_VALID );.  
1daa8 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
1daa9 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 65 20  E_OK && !(pPage 
1daaa 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  = pCur->apPage[p
1daab 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65  Cur->iPage])->le
1daac 61 66 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  af ){.    assert
1daad 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  ( pCur->aiIdx[pC
1daae 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65  ur->iPage]<pPage
1daaf 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 70  ->nCell );.    p
1dab0 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 66  gno = get4byte(f
1dab1 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 70  indCell(pPage, p
1dab2 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
1dab3 3e 69 50 61 67 65 5d 29 29 3b 0a 20 20 20 20 72  >iPage]));.    r
1dab4 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28  c = moveToChild(
1dab5 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d  pCur, pgno);.  }
1dab6 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1dab7 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20  ./*.** Move the 
1dab8 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74  cursor down to t
1dab9 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65  he right-most le
1daba 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68  af entry beneath
1dabb 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f 20   the.** page to 
1dabc 77 68 69 63 68 20 69 74 20 69 73 20 63 75 72 72  which it is curr
1dabd 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 20  ently pointing. 
1dabe 20 4e 6f 74 69 63 65 20 74 68 65 20 64 69 66 66   Notice the diff
1dabf 65 72 65 6e 63 65 0a 2a 2a 20 62 65 74 77 65 65  erence.** betwee
1dac0 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74  n moveToLeftmost
1dac1 28 29 20 61 6e 64 20 6d 6f 76 65 54 6f 52 69 67  () and moveToRig
1dac2 68 74 6d 6f 73 74 28 29 2e 20 20 6d 6f 76 65 54  htmost().  moveT
1dac3 6f 4c 65 66 74 6d 6f 73 74 28 29 0a 2a 2a 20 66  oLeftmost().** f
1dac4 69 6e 64 73 20 74 68 65 20 6c 65 66 74 2d 6d 6f  inds the left-mo
1dac5 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68  st entry beneath
1dac6 20 74 68 65 20 2a 65 6e 74 72 79 2a 20 77 68 65   the *entry* whe
1dac7 72 65 61 73 20 6d 6f 76 65 54 6f 52 69 67 68 74  reas moveToRight
1dac8 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20  most().** finds 
1dac9 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65  the right-most e
1daca 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65  ntry beneath the
1dacb 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20 54   *page*..**.** T
1dacc 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e  he right-most en
1dacd 74 72 79 20 69 73 20 74 68 65 20 6f 6e 65 20 77  try is the one w
1dace 69 74 68 20 74 68 65 20 6c 61 72 67 65 73 74 20  ith the largest 
1dacf 6b 65 79 20 2d 20 74 68 65 20 6c 61 73 74 0a 2a  key - the last.*
1dad0 2a 20 6b 65 79 20 69 6e 20 61 73 63 65 6e 64 69  * key in ascendi
1dad1 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61  ng order..*/.sta
1dad2 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 69  tic int moveToRi
1dad3 67 68 74 6d 6f 73 74 28 42 74 43 75 72 73 6f 72  ghtmost(BtCursor
1dad4 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f 20   *pCur){.  Pgno 
1dad5 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20 3d  pgno;.  int rc =
1dad6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65   SQLITE_OK;.  Me
1dad7 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30  mPage *pPage = 0
1dad8 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
1dad9 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
1dada 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
1dadb 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
1dadc 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20  URSOR_VALID );. 
1dadd 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49   while( rc==SQLI
1dade 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 65  TE_OK && !(pPage
1dadf 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
1dae0 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c  pCur->iPage])->l
1dae1 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20  eaf ){.    pgno 
1dae2 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  = get4byte(&pPag
1dae3 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
1dae4 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20  hdrOffset+8]);. 
1dae5 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70     pCur->aiIdx[p
1dae6 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50  Cur->iPage] = pP
1dae7 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20  age->nCell;.    
1dae8 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64  rc = moveToChild
1dae9 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20  (pCur, pgno);.  
1daea 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
1daeb 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 43 75  TE_OK ){.    pCu
1daec 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
1daed 50 61 67 65 5d 20 3d 20 70 50 61 67 65 2d 3e 6e  Page] = pPage->n
1daee 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70 43 75 72  Cell-1;.    pCur
1daef 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30  ->info.nSize = 0
1daf0 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69  ;.    pCur->vali
1daf1 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20  dNKey = 0;.  }. 
1daf2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1daf3 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
1daf4 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65  r to the first e
1daf5 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c  ntry in the tabl
1daf6 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54  e.  Return SQLIT
1daf7 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65  E_OK.** on succe
1daf8 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73 20 74  ss.  Set *pRes t
1daf9 6f 20 30 20 69 66 20 74 68 65 20 63 75 72 73 6f  o 0 if the curso
1dafa 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e 74  r actually point
1dafb 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a  s to something.*
1dafc 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73 20 74  * or set *pRes t
1dafd 6f 20 31 20 69 66 20 74 68 65 20 74 61 62 6c 65  o 1 if the table
1dafe 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51   is empty..*/.SQ
1daff 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1db00 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72   sqlite3BtreeFir
1db01 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  st(BtCursor *pCu
1db02 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20  r, int *pRes){. 
1db03 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65   int rc;..  asse
1db04 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
1db05 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
1db06 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1db07 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d  mutex_held(pCur-
1db08 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74  >pBtree->db->mut
1db09 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 6d 6f  ex) );.  rc = mo
1db0a 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a  veToRoot(pCur);.
1db0b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1db0c 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70  _OK ){.    if( p
1db0d 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
1db0e 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20  SOR_INVALID ){. 
1db0f 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75       assert( pCu
1db10 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 30 20 7c  r->pgnoRoot==0 |
1db11 7c 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  | pCur->apPage[p
1db12 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65  Cur->iPage]->nCe
1db13 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a  ll==0 );.      *
1db14 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 7d 65  pRes = 1;.    }e
1db15 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
1db16 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  t( pCur->apPage[
1db17 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43  pCur->iPage]->nC
1db18 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 2a  ell>0 );.      *
1db19 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20  pRes = 0;.      
1db1a 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d  rc = moveToLeftm
1db1b 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 7d  ost(pCur);.    }
1db1c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1db1d 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65  ;.}../* Move the
1db1e 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6c   cursor to the l
1db1f 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ast entry in the
1db20 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20   table.  Return 
1db21 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20  SQLITE_OK.** on 
1db22 73 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70  success.  Set *p
1db23 52 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 20  Res to 0 if the 
1db24 63 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20  cursor actually 
1db25 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68  points to someth
1db26 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70  ing.** or set *p
1db27 52 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 20  Res to 1 if the 
1db28 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a  table is empty..
1db29 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1db2a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1db2b 65 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 20  eeLast(BtCursor 
1db2c 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73  *pCur, int *pRes
1db2d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 0a 20  ){.  int rc;. . 
1db2e 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
1db2f 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
1db30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
1db31 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1db32 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  pCur->pBtree->db
1db33 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f  ->mutex) );..  /
1db34 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20  * If the cursor 
1db35 61 6c 72 65 61 64 79 20 70 6f 69 6e 74 73 20 74  already points t
1db36 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  o the last entry
1db37 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  , this is a no-o
1db38 70 2e 20 2a 2f 0a 20 20 69 66 28 20 43 55 52 53  p. */.  if( CURS
1db39 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e  OR_VALID==pCur->
1db3a 65 53 74 61 74 65 20 26 26 20 70 43 75 72 2d 3e  eState && pCur->
1db3b 61 74 4c 61 73 74 20 29 7b 0a 23 69 66 64 65 66  atLast ){.#ifdef
1db3c 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
1db3d 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20    /* This block 
1db3e 73 65 72 76 65 73 20 74 6f 20 61 73 73 65 72 74  serves to assert
1db3f 28 29 20 74 68 61 74 20 74 68 65 20 63 75 72 73  () that the curs
1db40 6f 72 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 70  or really does p
1db41 6f 69 6e 74 20 0a 20 20 20 20 2a 2a 20 74 6f 20  oint .    ** to 
1db42 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69  the last entry i
1db43 6e 20 74 68 65 20 62 2d 74 72 65 65 2e 20 2a 2f  n the b-tree. */
1db44 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20  .    int ii;.   
1db45 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 43   for(ii=0; ii<pC
1db46 75 72 2d 3e 69 50 61 67 65 3b 20 69 69 2b 2b 29  ur->iPage; ii++)
1db47 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1db48 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 69 5d 3d  pCur->aiIdx[ii]=
1db49 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 69  =pCur->apPage[ii
1db4a 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20  ]->nCell );.    
1db4b 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  }.    assert( pC
1db4c 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
1db4d 69 50 61 67 65 5d 3d 3d 70 43 75 72 2d 3e 61 70  iPage]==pCur->ap
1db4e 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
1db4f 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b 0a 20 20  ]->nCell-1 );.  
1db50 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1db51 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1db52 67 65 5d 2d 3e 6c 65 61 66 20 29 3b 0a 23 65 6e  ge]->leaf );.#en
1db53 64 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53  dif.    return S
1db54 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
1db55 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74   rc = moveToRoot
1db56 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63  (pCur);.  if( rc
1db57 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1db58 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e     if( CURSOR_IN
1db59 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74  VALID==pCur->eSt
1db5a 61 74 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ate ){.      ass
1db5b 65 72 74 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52  ert( pCur->pgnoR
1db5c 6f 6f 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e  oot==0 || pCur->
1db5d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1db5e 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b  ge]->nCell==0 );
1db5f 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31  .      *pRes = 1
1db60 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1db61 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
1db62 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
1db63 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a  VALID );.      *
1db64 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20  pRes = 0;.      
1db65 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74  rc = moveToRight
1db66 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20  most(pCur);.    
1db67 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d    pCur->atLast =
1db68 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f   rc==SQLITE_OK ?
1db69 31 3a 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  1:0;.    }.  }. 
1db6a 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1db6b 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
1db6c 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69  r so that it poi
1db6d 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20  nts to an entry 
1db6e 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a  near the key .**
1db6f 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 49   specified by pI
1db70 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e  dxKey or intKey.
1db71 20 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 63     Return a succ
1db72 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ess code..**.** 
1db73 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65  For INTKEY table
1db74 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61  s, the intKey pa
1db75 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e  rameter is used.
1db76 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75    pIdxKey .** mu
1db77 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72  st be NULL.  For
1db78 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70   index tables, p
1db79 49 64 78 4b 65 79 20 69 73 20 75 73 65 64 20 61  IdxKey is used a
1db7a 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20  nd intKey.** is 
1db7b 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  ignored..**.** I
1db7c 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68  f an exact match
1db7d 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74   is not found, t
1db7e 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69  hen the cursor i
1db7f 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74  s always.** left
1db80 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c   pointing at a l
1db81 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20 77  eaf page which w
1db82 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e  ould hold the en
1db83 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72  try if it.** wer
1db84 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20  e present.  The 
1db85 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69  cursor might poi
1db86 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74  nt to an entry t
1db87 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66  hat comes.** bef
1db88 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74 68 65  ore or after the
1db89 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69   key..**.** An i
1db8a 6e 74 65 67 65 72 20 69 73 20 77 72 69 74 74 65  nteger is writte
1db8b 6e 20 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 69  n into *pRes whi
1db8c 63 68 20 69 73 20 74 68 65 20 72 65 73 75 6c 74  ch is the result
1db8d 20 6f 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67   of.** comparing
1db8e 20 74 68 65 20 6b 65 79 20 77 69 74 68 20 74 68   the key with th
1db8f 65 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68  e entry to which
1db90 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 0a   the cursor is .
1db91 2a 2a 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68  ** pointing.  Th
1db92 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 65  e meaning of the
1db93 20 69 6e 74 65 67 65 72 20 77 72 69 74 74 65 6e   integer written
1db94 20 69 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69   into.** *pRes i
1db95 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s as follows:.**
1db96 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20  .**     *pRes<0 
1db97 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20       The cursor 
1db98 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67  is left pointing
1db99 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61   at an entry tha
1db9a 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
1db9b 20 20 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72        is smaller
1db9c 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64   than intKey/pId
1db9d 78 4b 65 79 20 6f 72 20 69 66 20 74 68 65 20 74  xKey or if the t
1db9e 61 62 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a  able is empty.**
1db9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dba0 20 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72    and the cursor
1dba1 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65   is therefore le
1dba2 66 74 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68  ft point to noth
1dba3 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a  ing..**.**     *
1dba4 70 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20  pRes==0     The 
1dba5 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70  cursor is left p
1dba6 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e  ointing at an en
1dba7 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20  try that.**     
1dba8 20 20 20 20 20 20 20 20 20 20 20 20 20 65 78 61               exa
1dba9 63 74 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74  ctly matches int
1dbaa 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a  Key/pIdxKey..**.
1dbab 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20  **     *pRes>0  
1dbac 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69      The cursor i
1dbad 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
1dbae 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74  at an entry that
1dbaf 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1dbb0 20 20 20 20 20 69 73 20 6c 61 72 67 65 72 20 74       is larger t
1dbb1 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b  han intKey/pIdxK
1dbb2 65 79 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45  ey..**.*/.SQLITE
1dbb3 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1dbb4 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55  ite3BtreeMovetoU
1dbb5 6e 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72  npacked(.  BtCur
1dbb6 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20  sor *pCur,      
1dbb7 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f      /* The curso
1dbb8 72 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f  r to be moved */
1dbb9 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
1dbba 64 20 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55  d *pIdxKey, /* U
1dbbb 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65  npacked index ke
1dbbc 79 20 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65  y */.  i64 intKe
1dbbd 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y,              
1dbbe 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6b 65 79  /* The table key
1dbbf 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 69   */.  int biasRi
1dbc0 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ght,           /
1dbc1 2a 20 49 66 20 74 72 75 65 2c 20 62 69 61 73 20  * If true, bias 
1dbc2 74 68 65 20 73 65 61 72 63 68 20 74 6f 20 74 68  the search to th
1dbc3 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20  e high end */.  
1dbc4 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20  int *pRes       
1dbc5 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
1dbc6 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73  e search results
1dbc7 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
1dbc8 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
1dbc9 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
1dbca 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73  x(pCur) );.  ass
1dbcb 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1dbcc 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42  ex_held(pCur->pB
1dbcd 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  tree->db->mutex)
1dbce 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52   );.  assert( pR
1dbcf 65 73 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  es );.  assert( 
1dbd0 28 70 49 64 78 4b 65 79 3d 3d 30 29 3d 3d 28 70  (pIdxKey==0)==(p
1dbd1 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30  Cur->pKeyInfo==0
1dbd2 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ) );..  /* If th
1dbd3 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65  e cursor is alre
1dbd4 61 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 61  ady positioned a
1dbd5 74 20 74 68 65 20 70 6f 69 6e 74 20 77 65 20 61  t the point we a
1dbd6 72 65 20 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74  re trying.  ** t
1dbd7 6f 20 6d 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20  o move to, then 
1dbd8 6a 75 73 74 20 72 65 74 75 72 6e 20 77 69 74 68  just return with
1dbd9 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f  out doing any wo
1dbda 72 6b 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72  rk */.  if( pCur
1dbdb 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
1dbdc 5f 56 41 4c 49 44 20 26 26 20 70 43 75 72 2d 3e  _VALID && pCur->
1dbdd 76 61 6c 69 64 4e 4b 65 79 20 0a 20 20 20 26 26  validNKey .   &&
1dbde 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d   pCur->apPage[0]
1dbdf 2d 3e 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20  ->intKey .  ){. 
1dbe0 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66     if( pCur->inf
1dbe1 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29  o.nKey==intKey )
1dbe2 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20  {.      *pRes = 
1dbe3 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  0;.      return 
1dbe4 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
1dbe5 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61  .    if( pCur->a
1dbe6 74 4c 61 73 74 20 26 26 20 70 43 75 72 2d 3e 69  tLast && pCur->i
1dbe7 6e 66 6f 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20  nfo.nKey<intKey 
1dbe8 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d  ){.      *pRes =
1dbe9 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72   -1;.      retur
1dbea 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
1dbeb 20 7d 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d   }.  }..  rc = m
1dbec 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b  oveToRoot(pCur);
1dbed 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
1dbee 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
1dbef 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1dbf0 70 67 6e 6f 52 6f 6f 74 3d 3d 30 20 7c 7c 20 70  pgnoRoot==0 || p
1dbf1 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
1dbf2 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73  ->iPage] );.  as
1dbf3 73 65 72 74 28 20 70 43 75 72 2d 3e 70 67 6e 6f  sert( pCur->pgno
1dbf4 52 6f 6f 74 3d 3d 30 20 7c 7c 20 70 43 75 72 2d  Root==0 || pCur-
1dbf5 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
1dbf6 61 67 65 5d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a  age]->isInit );.
1dbf7 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1dbf8 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49  eState==CURSOR_I
1dbf9 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e  NVALID || pCur->
1dbfa 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1dbfb 67 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a  ge]->nCell>0 );.
1dbfc 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61    if( pCur->eSta
1dbfd 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c  te==CURSOR_INVAL
1dbfe 49 44 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20  ID ){.    *pRes 
1dbff 3d 20 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 74  = -1;.    assert
1dc00 28 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74  ( pCur->pgnoRoot
1dc01 3d 3d 30 20 7c 7c 20 70 43 75 72 2d 3e 61 70 50  ==0 || pCur->apP
1dc02 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
1dc03 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20  ->nCell==0 );.  
1dc04 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1dc05 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  OK;.  }.  assert
1dc06 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30  ( pCur->apPage[0
1dc07 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49 64  ]->intKey || pId
1dc08 78 4b 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b 3b  xKey );.  for(;;
1dc09 29 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c 20  ){.    int lwr, 
1dc0a 75 70 72 2c 20 69 64 78 3b 0a 20 20 20 20 50 67  upr, idx;.    Pg
1dc0b 6e 6f 20 63 68 6c 64 50 67 3b 0a 20 20 20 20 4d  no chldPg;.    M
1dc0c 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20  emPage *pPage = 
1dc0d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
1dc0e 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69  r->iPage];.    i
1dc0f 6e 74 20 63 3b 0a 0a 20 20 20 20 2f 2a 20 70 50  nt c;..    /* pP
1dc10 61 67 65 2d 3e 6e 43 65 6c 6c 20 6d 75 73 74 20  age->nCell must 
1dc11 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  be greater than 
1dc12 7a 65 72 6f 2e 20 49 66 20 74 68 69 73 20 69 73  zero. If this is
1dc13 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 0a 20   the root-page. 
1dc14 20 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72     ** the cursor
1dc15 20 77 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e   would have been
1dc16 20 49 4e 56 41 4c 49 44 20 61 62 6f 76 65 20 61   INVALID above a
1dc17 6e 64 20 74 68 69 73 20 66 6f 72 28 3b 3b 29 20  nd this for(;;) 
1dc18 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 6e 6f 74 20  loop.    ** not 
1dc19 72 75 6e 2e 20 49 66 20 74 68 69 73 20 69 73 20  run. If this is 
1dc1a 6e 6f 74 20 74 68 65 20 72 6f 6f 74 2d 70 61 67  not the root-pag
1dc1b 65 2c 20 74 68 65 6e 20 74 68 65 20 6d 6f 76 65  e, then the move
1dc1c 54 6f 43 68 69 6c 64 28 29 20 72 6f 75 74 69 6e  ToChild() routin
1dc1d 65 0a 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20 68  e.    ** would h
1dc1e 61 76 65 20 61 6c 72 65 61 64 79 20 64 65 74 65  ave already dete
1dc1f 63 74 65 64 20 64 62 20 63 6f 72 72 75 70 74 69  cted db corrupti
1dc20 6f 6e 2e 20 53 69 6d 69 6c 61 72 6c 79 2c 20 70  on. Similarly, p
1dc21 50 61 67 65 20 6d 75 73 74 0a 20 20 20 20 2a 2a  Page must.    **
1dc22 20 62 65 20 74 68 65 20 72 69 67 68 74 20 6b 69   be the right ki
1dc23 6e 64 20 28 69 6e 64 65 78 20 6f 72 20 74 61 62  nd (index or tab
1dc24 6c 65 29 20 6f 66 20 62 2d 74 72 65 65 20 70 61  le) of b-tree pa
1dc25 67 65 2e 20 4f 74 68 65 72 77 69 73 65 0a 20 20  ge. Otherwise.  
1dc26 20 20 2a 2a 20 61 20 6d 6f 76 65 54 6f 43 68 69    ** a moveToChi
1dc27 6c 64 28 29 20 6f 72 20 6d 6f 76 65 54 6f 52 6f  ld() or moveToRo
1dc28 6f 74 28 29 20 63 61 6c 6c 20 77 6f 75 6c 64 20  ot() call would 
1dc29 68 61 76 65 20 64 65 74 65 63 74 65 64 20 63 6f  have detected co
1dc2a 72 72 75 70 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20  rruption.  */.  
1dc2b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
1dc2c 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20  >nCell>0 );.    
1dc2d 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
1dc2e 6e 74 4b 65 79 3d 3d 28 70 49 64 78 4b 65 79 3d  ntKey==(pIdxKey=
1dc2f 3d 30 29 20 29 3b 0a 20 20 20 20 6c 77 72 20 3d  =0) );.    lwr =
1dc30 20 30 3b 0a 20 20 20 20 75 70 72 20 3d 20 70 50   0;.    upr = pP
1dc31 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20  age->nCell-1;.  
1dc32 20 20 69 66 28 20 62 69 61 73 52 69 67 68 74 20    if( biasRight 
1dc33 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61  ){.      pCur->a
1dc34 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
1dc35 5d 20 3d 20 28 75 31 36 29 28 69 64 78 20 3d 20  ] = (u16)(idx = 
1dc36 75 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  upr);.    }else{
1dc37 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49  .      pCur->aiI
1dc38 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20  dx[pCur->iPage] 
1dc39 3d 20 28 75 31 36 29 28 69 64 78 20 3d 20 28 75  = (u16)(idx = (u
1dc3a 70 72 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20  pr+lwr)/2);.    
1dc3b 7d 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20  }.    for(;;){. 
1dc3c 20 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20       u8 *pCell; 
1dc3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dc3e 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
1dc3f 74 65 72 20 74 6f 20 63 75 72 72 65 6e 74 20 63  ter to current c
1dc40 65 6c 6c 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a  ell in pPage */.
1dc41 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
1dc42 64 78 3d 3d 70 43 75 72 2d 3e 61 69 49 64 78 5b  dx==pCur->aiIdx[
1dc43 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a  pCur->iPage] );.
1dc44 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f        pCur->info
1dc45 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20  .nSize = 0;.    
1dc46 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65    pCell = findCe
1dc47 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b  ll(pPage, idx) +
1dc48 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72   pPage->childPtr
1dc49 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20  Size;.      if( 
1dc4a 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
1dc4b 0a 20 20 20 20 20 20 20 20 69 36 34 20 6e 43 65  .        i64 nCe
1dc4c 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69  llKey;.        i
1dc4d 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74  f( pPage->hasDat
1dc4e 61 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75  a ){.          u
1dc4f 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20  32 dummy;.      
1dc50 20 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74      pCell += get
1dc51 56 61 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20  Varint32(pCell, 
1dc52 64 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20  dummy);.        
1dc53 7d 0a 20 20 20 20 20 20 20 20 67 65 74 56 61 72  }.        getVar
1dc54 69 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a  int(pCell, (u64*
1dc55 29 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20  )&nCellKey);.   
1dc56 20 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65       if( nCellKe
1dc57 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  y==intKey ){.   
1dc58 20 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20         c = 0;.  
1dc59 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1dc5a 6e 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20  nCellKey<intKey 
1dc5b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d  ){.          c =
1dc5c 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c   -1;.        }el
1dc5d 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73  se{.          as
1dc5e 73 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69  sert( nCellKey>i
1dc5f 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20  ntKey );.       
1dc60 20 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20     c = +1;.     
1dc61 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 43 75     }.        pCu
1dc62 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31  r->validNKey = 1
1dc63 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e  ;.        pCur->
1dc64 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c  info.nKey = nCel
1dc65 6c 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73  lKey;.      }els
1dc66 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e{.        /* Th
1dc67 65 20 6d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72  e maximum suppor
1dc68 74 65 64 20 70 61 67 65 2d 73 69 7a 65 20 69 73  ted page-size is
1dc69 20 36 35 35 33 36 20 62 79 74 65 73 2e 20 54 68   65536 bytes. Th
1dc6a 69 73 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20  is means that.  
1dc6b 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6d 61 78        ** the max
1dc6c 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72  imum number of r
1dc6d 65 63 6f 72 64 20 62 79 74 65 73 20 73 74 6f 72  ecord bytes stor
1dc6e 65 64 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42  ed on an index B
1dc6f 2d 54 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a  -Tree.        **
1dc70 20 70 61 67 65 20 69 73 20 6c 65 73 73 20 74 68   page is less th
1dc71 61 6e 20 31 36 33 38 34 20 62 79 74 65 73 20 61  an 16384 bytes a
1dc72 6e 64 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64  nd may be stored
1dc73 20 61 73 20 61 20 32 2d 62 79 74 65 0a 20 20 20   as a 2-byte.   
1dc74 20 20 20 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20       ** varint. 
1dc75 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  This information
1dc76 20 69 73 20 75 73 65 64 20 74 6f 20 61 74 74 65   is used to atte
1dc77 6d 70 74 20 74 6f 20 61 76 6f 69 64 20 70 61 72  mpt to avoid par
1dc78 73 69 6e 67 20 0a 20 20 20 20 20 20 20 20 2a 2a  sing .        **
1dc79 20 74 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c   the entire cell
1dc7a 20 62 79 20 63 68 65 63 6b 69 6e 67 20 66 6f 72   by checking for
1dc7b 20 74 68 65 20 63 61 73 65 73 20 77 68 65 72 65   the cases where
1dc7c 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 0a   the record is .
1dc7d 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65          ** store
1dc7e 64 20 65 6e 74 69 72 65 6c 79 20 77 69 74 68 69  d entirely withi
1dc7f 6e 20 74 68 65 20 62 2d 74 72 65 65 20 70 61 67  n the b-tree pag
1dc80 65 20 62 79 20 69 6e 73 70 65 63 74 69 6e 67 20  e by inspecting 
1dc81 74 68 65 20 66 69 72 73 74 20 0a 20 20 20 20 20  the first .     
1dc82 20 20 20 2a 2a 20 32 20 62 79 74 65 73 20 6f 66     ** 2 bytes of
1dc83 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20   the cell..     
1dc84 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e     */.        in
1dc85 74 20 6e 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b  t nCell = pCell[
1dc86 30 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0];.        if( 
1dc87 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d 61  nCell<=pPage->ma
1dc88 78 31 62 79 74 65 50 61 79 6c 6f 61 64 0a 20 20  x1bytePayload.  
1dc89 20 20 20 20 20 20 20 2f 2a 20 26 26 20 28 70 43         /* && (pC
1dc8a 65 6c 6c 2b 6e 43 65 6c 6c 29 3c 70 50 61 67 65  ell+nCell)<pPage
1dc8b 2d 3e 61 44 61 74 61 45 6e 64 20 2a 2f 0a 20 20  ->aDataEnd */.  
1dc8c 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
1dc8d 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63     /* This branc
1dc8e 68 20 72 75 6e 73 20 69 66 20 74 68 65 20 72 65  h runs if the re
1dc8f 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 20  cord-size field 
1dc90 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 61  of the cell is a
1dc91 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 69  .          ** si
1dc92 6e 67 6c 65 20 62 79 74 65 20 76 61 72 69 6e 74  ngle byte varint
1dc93 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20   and the record 
1dc94 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e  fits entirely on
1dc95 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 20 20 20   the main.      
1dc96 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 70 61      ** b-tree pa
1dc97 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  ge.  */.        
1dc98 20 20 74 65 73 74 63 61 73 65 28 20 70 43 65 6c    testcase( pCel
1dc99 6c 2b 6e 43 65 6c 6c 2b 31 3d 3d 70 50 61 67 65  l+nCell+1==pPage
1dc9a 2d 3e 61 44 61 74 61 45 6e 64 20 29 3b 0a 20 20  ->aDataEnd );.  
1dc9b 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69          c = sqli
1dc9c 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d  te3VdbeRecordCom
1dc9d 70 61 72 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69  pare(nCell, (voi
1dc9e 64 2a 29 26 70 43 65 6c 6c 5b 31 5d 2c 20 70 49  d*)&pCell[1], pI
1dc9f 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20  dxKey);.        
1dca0 7d 65 6c 73 65 20 69 66 28 20 21 28 70 43 65 6c  }else if( !(pCel
1dca1 6c 5b 31 5d 20 26 20 30 78 38 30 29 20 0a 20 20  l[1] & 0x80) .  
1dca2 20 20 20 20 20 20 20 20 26 26 20 28 6e 43 65 6c          && (nCel
1dca3 6c 20 3d 20 28 28 6e 43 65 6c 6c 26 30 78 37 66  l = ((nCell&0x7f
1dca4 29 3c 3c 37 29 20 2b 20 70 43 65 6c 6c 5b 31 5d  )<<7) + pCell[1]
1dca5 29 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63  )<=pPage->maxLoc
1dca6 61 6c 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  al.          /* 
1dca7 26 26 20 28 70 43 65 6c 6c 2b 6e 43 65 6c 6c 2b  && (pCell+nCell+
1dca8 32 29 3c 3d 70 50 61 67 65 2d 3e 61 44 61 74 61  2)<=pPage->aData
1dca9 45 6e 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 29  End */.        )
1dcaa 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
1dcab 68 65 20 72 65 63 6f 72 64 2d 73 69 7a 65 20 66  he record-size f
1dcac 69 65 6c 64 20 69 73 20 61 20 32 20 62 79 74 65  ield is a 2 byte
1dcad 20 76 61 72 69 6e 74 20 61 6e 64 20 74 68 65 20   varint and the 
1dcae 72 65 63 6f 72 64 20 0a 20 20 20 20 20 20 20 20  record .        
1dcaf 20 20 2a 2a 20 66 69 74 73 20 65 6e 74 69 72 65    ** fits entire
1dcb0 6c 79 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62  ly on the main b
1dcb1 2d 74 72 65 65 20 70 61 67 65 2e 20 20 2a 2f 0a  -tree page.  */.
1dcb2 20 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61            testca
1dcb3 73 65 28 20 70 43 65 6c 6c 2b 6e 43 65 6c 6c 2b  se( pCell+nCell+
1dcb4 32 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74 61 45  2==pPage->aDataE
1dcb5 6e 64 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  nd );.          
1dcb6 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52  c = sqlite3VdbeR
1dcb7 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 65  ecordCompare(nCe
1dcb8 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 65 6c  ll, (void*)&pCel
1dcb9 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29 3b 0a  l[2], pIdxKey);.
1dcba 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1dcbb 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1dcbc 72 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f 76 65  record flows ove
1dcbd 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f  r onto one or mo
1dcbe 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  re overflow page
1dcbf 73 2e 20 49 6e 0a 20 20 20 20 20 20 20 20 20 20  s. In.          
1dcc0 2a 2a 20 74 68 69 73 20 63 61 73 65 20 74 68 65  ** this case the
1dcc1 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65 65 64   whole cell need
1dcc2 73 20 74 6f 20 62 65 20 70 61 72 73 65 64 2c 20  s to be parsed, 
1dcc3 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74  a buffer allocat
1dcc4 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  ed.          ** 
1dcc5 61 6e 64 20 61 63 63 65 73 73 50 61 79 6c 6f 61  and accessPayloa
1dcc6 64 28 29 20 75 73 65 64 20 74 6f 20 72 65 74 72  d() used to retr
1dcc7 69 65 76 65 20 74 68 65 20 72 65 63 6f 72 64 20  ieve the record 
1dcc8 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 20 20  into the.       
1dcc9 20 20 20 2a 2a 20 62 75 66 66 65 72 20 62 65 66     ** buffer bef
1dcca 6f 72 65 20 56 64 62 65 52 65 63 6f 72 64 43 6f  ore VdbeRecordCo
1dccb 6d 70 61 72 65 28 29 20 63 61 6e 20 62 65 20 63  mpare() can be c
1dccc 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20  alled. */.      
1dccd 20 20 20 20 76 6f 69 64 20 2a 70 43 65 6c 6c 4b      void *pCellK
1dcce 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 75 38  ey;.          u8
1dccf 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c 42 6f   * const pCellBo
1dcd0 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70 50 61  dy = pCell - pPa
1dcd1 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65  ge->childPtrSize
1dcd2 3b 0a 20 20 20 20 20 20 20 20 20 20 62 74 72 65  ;.          btre
1dcd3 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
1dcd4 61 67 65 2c 20 70 43 65 6c 6c 42 6f 64 79 2c 20  age, pCellBody, 
1dcd5 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20  &pCur->info);.  
1dcd6 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d 20          nCell = 
1dcd7 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e  (int)pCur->info.
1dcd8 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20  nKey;.          
1dcd9 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 74  pCellKey = sqlit
1dcda 65 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c 20  e3Malloc( nCell 
1dcdb 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
1dcdc 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a   pCellKey==0 ){.
1dcdd 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
1dcde 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1dcdf 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
1dce0 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20  moveto_finish;. 
1dce1 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1dce2 20 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 73       rc = access
1dce3 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 30 2c  Payload(pCur, 0,
1dce4 20 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 67 6e 65   nCell, (unsigne
1dce5 64 20 63 68 61 72 2a 29 70 43 65 6c 6c 4b 65 79  d char*)pCellKey
1dce6 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
1dce7 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
1dce8 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
1dce9 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20  ee(pCellKey);.  
1dcea 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d            goto m
1dceb 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20  oveto_finish;.  
1dcec 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1dced 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56      c = sqlite3V
1dcee 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65  dbeRecordCompare
1dcef 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b 65 79  (nCell, pCellKey
1dcf0 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20  , pIdxKey);.    
1dcf1 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
1dcf2 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20  ee(pCellKey);.  
1dcf3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1dcf4 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29        if( c==0 )
1dcf5 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50  {.        if( pP
1dcf6 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21  age->intKey && !
1dcf7 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
1dcf8 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69           lwr = i
1dcf9 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  dx;.          br
1dcfa 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  eak;.        }el
1dcfb 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70  se{.          *p
1dcfc 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  Res = 0;.       
1dcfd 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
1dcfe 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  K;.          got
1dcff 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b  o moveto_finish;
1dd00 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1dd01 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3c 30   }.      if( c<0
1dd02 20 29 7b 0a 20 20 20 20 20 20 20 20 6c 77 72 20   ){.        lwr 
1dd03 3d 20 69 64 78 2b 31 3b 0a 20 20 20 20 20 20 7d  = idx+1;.      }
1dd04 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75 70  else{.        up
1dd05 72 20 3d 20 69 64 78 2d 31 3b 0a 20 20 20 20 20  r = idx-1;.     
1dd06 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6c 77 72   }.      if( lwr
1dd07 3e 75 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20  >upr ){.        
1dd08 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1dd09 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78       pCur->aiIdx
1dd0a 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20  [pCur->iPage] = 
1dd0b 28 75 31 36 29 28 69 64 78 20 3d 20 28 6c 77 72  (u16)(idx = (lwr
1dd0c 2b 75 70 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a  +upr)/2);.    }.
1dd0d 20 20 20 20 61 73 73 65 72 74 28 20 6c 77 72 3d      assert( lwr=
1dd0e 3d 75 70 72 2b 31 20 7c 7c 20 28 70 50 61 67 65  =upr+1 || (pPage
1dd0f 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 50 61  ->intKey && !pPa
1dd10 67 65 2d 3e 6c 65 61 66 29 20 29 3b 0a 20 20 20  ge->leaf) );.   
1dd11 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
1dd12 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 66  isInit );.    if
1dd13 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b  ( pPage->leaf ){
1dd14 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20  .      chldPg = 
1dd15 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  0;.    }else if(
1dd16 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 65   lwr>=pPage->nCe
1dd17 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64  ll ){.      chld
1dd18 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  Pg = get4byte(&p
1dd19 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
1dd1a 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
1dd1b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1dd1c 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34     chldPg = get4
1dd1d 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50  byte(findCell(pP
1dd1e 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 20  age, lwr));.    
1dd1f 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 67  }.    if( chldPg
1dd20 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ==0 ){.      ass
1dd21 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ert( pCur->aiIdx
1dd22 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43  [pCur->iPage]<pC
1dd23 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
1dd24 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29  >iPage]->nCell )
1dd25 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20  ;.      *pRes = 
1dd26 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  c;.      rc = SQ
1dd27 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 67  LITE_OK;.      g
1dd28 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73  oto moveto_finis
1dd29 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75  h;.    }.    pCu
1dd2a 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
1dd2b 50 61 67 65 5d 20 3d 20 28 75 31 36 29 6c 77 72  Page] = (u16)lwr
1dd2c 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f  ;.    pCur->info
1dd2d 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20  .nSize = 0;.    
1dd2e 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
1dd2f 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f  = 0;.    rc = mo
1dd30 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20  veToChild(pCur, 
1dd31 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 66 28  chldPg);.    if(
1dd32 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 65 74   rc ) goto movet
1dd33 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 6d 6f  o_finish;.  }.mo
1dd34 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 20 72  veto_finish:.  r
1dd35 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
1dd36 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
1dd37 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  if the cursor is
1dd38 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61 74   not pointing at
1dd39 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 65   an entry of the
1dd3a 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 52   table..**.** TR
1dd3b 55 45 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72  UE will be retur
1dd3c 6e 65 64 20 61 66 74 65 72 20 61 20 63 61 6c 6c  ned after a call
1dd3d 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65   to sqlite3Btree
1dd3e 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a 2a 20  Next() moves.** 
1dd3f 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 65 6e  past the last en
1dd40 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65  try in the table
1dd41 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65   or sqlite3Btree
1dd42 50 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61 73  Prev() moves pas
1dd43 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 65  t.** the first e
1dd44 6e 74 72 79 2e 20 20 54 52 55 45 20 69 73 20 61  ntry.  TRUE is a
1dd45 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 20  lso returned if 
1dd46 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70  the table is emp
1dd47 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ty..*/.SQLITE_PR
1dd48 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1dd49 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 73  3BtreeEof(BtCurs
1dd4a 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20  or *pCur){.  /* 
1dd4b 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 74 68  TODO: What if th
1dd4c 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 43  e cursor is in C
1dd4d 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
1dd4e 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 6c 65 20  K but all table 
1dd4f 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 61 76  entries.  ** hav
1dd50 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 3f 20  e been deleted? 
1dd51 54 68 69 73 20 41 50 49 20 77 69 6c 6c 20 6e 65  This API will ne
1dd52 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 6f 20  ed to change to 
1dd53 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
1dd54 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 65 6c  code.  ** as wel
1dd55 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 61 6e  l as the boolean
1dd56 20 72 65 73 75 6c 74 20 76 61 6c 75 65 2e 0a 20   result value.. 
1dd57 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 43 55   */.  return (CU
1dd58 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 75 72  RSOR_VALID!=pCur
1dd59 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f 2a  ->eState);.}../*
1dd5a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 65 20  .** Advance the 
1dd5b 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6e 65  cursor to the ne
1dd5c 78 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  xt entry in the 
1dd5d 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a  database.  If.**
1dd5e 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 6e   successful then
1dd5f 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 49   set *pRes=0.  I
1dd60 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20  f the cursor.** 
1dd61 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e  was already poin
1dd62 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74  ting to the last
1dd63 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61   entry in the da
1dd64 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a  tabase before.**
1dd65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61   this routine wa
1dd66 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73  s called, then s
1dd67 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53  et *pRes=1..*/.S
1dd68 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1dd69 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  t sqlite3BtreeNe
1dd6a 78 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  xt(BtCursor *pCu
1dd6b 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20  r, int *pRes){. 
1dd6c 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69   int rc;.  int i
1dd6d 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  dx;.  MemPage *p
1dd6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  Page;..  assert(
1dd6f 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
1dd70 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20  x(pCur) );.  rc 
1dd71 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50  = restoreCursorP
1dd72 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20  osition(pCur);. 
1dd73 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1dd74 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
1dd75 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72   rc;.  }.  asser
1dd76 74 28 20 70 52 65 73 21 3d 30 20 29 3b 0a 20 20  t( pRes!=0 );.  
1dd77 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  if( CURSOR_INVAL
1dd78 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65  ID==pCur->eState
1dd79 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20   ){.    *pRes = 
1dd7a 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  1;.    return SQ
1dd7b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69  LITE_OK;.  }.  i
1dd7c 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78  f( pCur->skipNex
1dd7d 74 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d  t>0 ){.    pCur-
1dd7e 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 20  >skipNext = 0;. 
1dd7f 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20     *pRes = 0;.  
1dd80 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1dd81 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e  OK;.  }.  pCur->
1dd82 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20  skipNext = 0;.. 
1dd83 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61   pPage = pCur->a
1dd84 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
1dd85 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 43  e];.  idx = ++pC
1dd86 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
1dd87 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74  iPage];.  assert
1dd88 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20  ( pPage->isInit 
1dd89 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
1dd8a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
1dd8b 20 63 6f 72 72 75 70 74 2c 20 69 74 20 69 73 20   corrupt, it is 
1dd8c 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65  possible for the
1dd8d 20 76 61 6c 75 65 20 6f 66 20 69 64 78 20 0a 20   value of idx . 
1dd8e 20 2a 2a 20 74 6f 20 62 65 20 69 6e 76 61 6c 69   ** to be invali
1dd8f 64 20 68 65 72 65 2e 20 54 68 69 73 20 63 61 6e  d here. This can
1dd90 20 6f 6e 6c 79 20 6f 63 63 75 72 20 69 66 20 61   only occur if a
1dd91 20 73 65 63 6f 6e 64 20 63 75 72 73 6f 72 20 6d   second cursor m
1dd92 6f 64 69 66 69 65 73 0a 20 20 2a 2a 20 74 68 65  odifies.  ** the
1dd93 20 70 61 67 65 20 77 68 69 6c 65 20 63 75 72 73   page while curs
1dd94 6f 72 20 70 43 75 72 20 69 73 20 68 6f 6c 64 69  or pCur is holdi
1dd95 6e 67 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  ng a reference t
1dd96 6f 20 69 74 2e 20 57 68 69 63 68 20 63 61 6e 0a  o it. Which can.
1dd97 20 20 2a 2a 20 6f 6e 6c 79 20 68 61 70 70 65 6e    ** only happen
1dd98 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
1dd99 20 69 73 20 63 6f 72 72 75 70 74 20 69 6e 20 73   is corrupt in s
1dd9a 75 63 68 20 61 20 77 61 79 20 61 73 20 74 6f 20  uch a way as to 
1dd9b 6c 69 6e 6b 20 74 68 65 0a 20 20 2a 2a 20 70 61  link the.  ** pa
1dd9c 67 65 20 69 6e 74 6f 20 6d 6f 72 65 20 74 68 61  ge into more tha
1dd9d 6e 20 6f 6e 65 20 62 2d 74 72 65 65 20 73 74 72  n one b-tree str
1dd9e 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 74 65 73  ucture. */.  tes
1dd9f 74 63 61 73 65 28 20 69 64 78 3e 70 50 61 67 65  tcase( idx>pPage
1dda0 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 70 43  ->nCell );..  pC
1dda1 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d  ur->info.nSize =
1dda2 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69   0;.  pCur->vali
1dda3 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28  dNKey = 0;.  if(
1dda4 20 69 64 78 3e 3d 70 50 61 67 65 2d 3e 6e 43 65   idx>=pPage->nCe
1dda5 6c 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70  ll ){.    if( !p
1dda6 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
1dda7 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43      rc = moveToC
1dda8 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62  hild(pCur, get4b
1dda9 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74  yte(&pPage->aDat
1ddaa 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  a[pPage->hdrOffs
1ddab 65 74 2b 38 5d 29 29 3b 0a 20 20 20 20 20 20 69  et+8]));.      i
1ddac 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1ddad 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f  c;.      rc = mo
1ddae 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75  veToLeftmost(pCu
1ddaf 72 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20  r);.      *pRes 
1ddb0 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 0;.      retur
1ddb1 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
1ddb2 64 6f 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43  do{.      if( pC
1ddb3 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a  ur->iPage==0 ){.
1ddb4 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20          *pRes = 
1ddb5 31 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d  1;.        pCur-
1ddb6 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52  >eState = CURSOR
1ddb7 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20  _INVALID;.      
1ddb8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1ddb9 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OK;.      }.    
1ddba 20 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70    moveToParent(p
1ddbb 43 75 72 29 3b 0a 20 20 20 20 20 20 70 50 61 67  Cur);.      pPag
1ddbc 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
1ddbd 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20  [pCur->iPage];. 
1ddbe 20 20 20 7d 77 68 69 6c 65 28 20 70 43 75 72 2d     }while( pCur-
1ddbf 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
1ddc0 67 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c  ge]>=pPage->nCel
1ddc1 6c 20 29 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d  l );.    *pRes =
1ddc2 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67   0;.    if( pPag
1ddc3 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  e->intKey ){.   
1ddc4 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1ddc5 74 72 65 65 4e 65 78 74 28 70 43 75 72 2c 20 70  treeNext(pCur, p
1ddc6 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Res);.    }else{
1ddc7 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1ddc8 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20  TE_OK;.    }.   
1ddc9 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
1ddca 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 69    *pRes = 0;.  i
1ddcb 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  f( pPage->leaf )
1ddcc 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1ddcd 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63  ITE_OK;.  }.  rc
1ddce 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73   = moveToLeftmos
1ddcf 74 28 70 43 75 72 29 3b 0a 20 20 72 65 74 75 72  t(pCur);.  retur
1ddd0 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
1ddd1 53 74 65 70 20 74 68 65 20 63 75 72 73 6f 72 20  Step the cursor 
1ddd2 74 6f 20 74 68 65 20 62 61 63 6b 20 74 6f 20 74  to the back to t
1ddd3 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72  he previous entr
1ddd4 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  y in the databas
1ddd5 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 65 73  e.  If.** succes
1ddd6 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 2a 70  sful then set *p
1ddd7 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 20 63  Res=0.  If the c
1ddd8 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72  ursor.** was alr
1ddd9 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f  eady pointing to
1ddda 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
1dddb 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1dddc 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20   before.** this 
1dddd 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c  routine was call
1ddde 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 52  ed, then set *pR
1dddf 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  es=1..*/.SQLITE_
1dde0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1dde1 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73  te3BtreePrevious
1dde2 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
1dde3 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69   int *pRes){.  i
1dde4 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65  nt rc;.  MemPage
1dde5 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65   *pPage;..  asse
1dde6 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
1dde7 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
1dde8 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73  rc = restoreCurs
1dde9 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29  orPosition(pCur)
1ddea 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
1ddeb 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
1ddec 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 43  urn rc;.  }.  pC
1dded 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a  ur->atLast = 0;.
1ddee 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56    if( CURSOR_INV
1ddef 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61  ALID==pCur->eSta
1ddf0 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20  te ){.    *pRes 
1ddf1 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
1ddf2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1ddf3 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e   if( pCur->skipN
1ddf4 65 78 74 3c 30 20 29 7b 0a 20 20 20 20 70 43 75  ext<0 ){.    pCu
1ddf5 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b  r->skipNext = 0;
1ddf6 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a  .    *pRes = 0;.
1ddf7 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1ddf8 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72  E_OK;.  }.  pCur
1ddf9 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a  ->skipNext = 0;.
1ddfa 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d  .  pPage = pCur-
1ddfb 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
1ddfc 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  age];.  assert( 
1ddfd 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b  pPage->isInit );
1ddfe 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c  .  if( !pPage->l
1ddff 65 61 66 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  eaf ){.    int i
1de00 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78  dx = pCur->aiIdx
1de01 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20  [pCur->iPage];. 
1de02 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
1de03 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79  ild(pCur, get4by
1de04 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67  te(findCell(pPag
1de05 65 2c 20 69 64 78 29 29 29 3b 0a 20 20 20 20 69  e, idx)));.    i
1de06 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72  f( rc ){.      r
1de07 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
1de08 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52      rc = moveToR
1de09 69 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a  ightmost(pCur);.
1de0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 69    }else{.    whi
1de0b 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  le( pCur->aiIdx[
1de0c 70 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 30 20  pCur->iPage]==0 
1de0d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 75  ){.      if( pCu
1de0e 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20  r->iPage==0 ){. 
1de0f 20 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74         pCur->eSt
1de10 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56  ate = CURSOR_INV
1de11 41 4c 49 44 3b 0a 20 20 20 20 20 20 20 20 2a 70  ALID;.        *p
1de12 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  Res = 1;.       
1de13 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1de14 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  K;.      }.     
1de15 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43   moveToParent(pC
1de16 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ur);.    }.    p
1de17 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20  Cur->info.nSize 
1de18 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76  = 0;.    pCur->v
1de19 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20  alidNKey = 0;.. 
1de1a 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70     pCur->aiIdx[p
1de1b 43 75 72 2d 3e 69 50 61 67 65 5d 2d 2d 3b 0a 20  Cur->iPage]--;. 
1de1c 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d     pPage = pCur-
1de1d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
1de1e 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 70 50  age];.    if( pP
1de1f 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21  age->intKey && !
1de20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
1de21 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1de22 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70  3BtreePrevious(p
1de23 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 20 20  Cur, pRes);.    
1de24 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
1de25 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
1de26 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d   }.  }.  *pRes =
1de27 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   0;.  return rc;
1de28 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
1de29 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 66 72  te a new page fr
1de2a 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
1de2b 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  file..**.** The 
1de2c 6e 65 77 20 70 61 67 65 20 69 73 20 6d 61 72 6b  new page is mark
1de2d 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 28 49  ed as dirty.  (I
1de2e 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 73  n other words, s
1de2f 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
1de30 28 29 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64  ().** has alread
1de31 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e  y been called on
1de32 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 29 20   the new page.) 
1de33 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 68 61   The new page ha
1de34 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e 20 72  s also.** been r
1de35 65 66 65 72 65 6e 63 65 64 20 61 6e 64 20 74 68  eferenced and th
1de36 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e  e calling routin
1de37 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  e is responsible
1de38 20 66 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20   for calling.** 
1de39 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
1de3a 66 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20 70  f() on the new p
1de3b 61 67 65 20 77 68 65 6e 20 69 74 20 69 73 20 64  age when it is d
1de3c 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54  one..**.** SQLIT
1de3d 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
1de3e 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 41 6e   on success.  An
1de3f 79 20 6f 74 68 65 72 20 72 65 74 75 72 6e 20 76  y other return v
1de40 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 0a 2a  alue indicates.*
1de41 2a 20 61 6e 20 65 72 72 6f 72 2e 20 20 2a 70 70  * an error.  *pp
1de42 50 61 67 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20  Page and *pPgno 
1de43 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 69 6e  are undefined in
1de44 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 6e   the event of an
1de45 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20 6e 6f   error..** Do no
1de46 74 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33  t invoke sqlite3
1de47 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20  PagerUnref() on 
1de48 2a 70 70 50 61 67 65 20 69 66 20 61 6e 20 65 72  *ppPage if an er
1de49 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ror is returned.
1de4a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 6e  .**.** If the "n
1de4b 65 61 72 62 79 22 20 70 61 72 61 6d 65 74 65 72  earby" parameter
1de4c 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20   is not 0, then 
1de4d 61 20 28 66 65 65 62 6c 65 29 20 65 66 66 6f 72  a (feeble) effor
1de4e 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a 2a 2a  t is made to .**
1de4f 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65 20 63   locate a page c
1de50 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61 67 65  lose to the page
1de51 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62 79 22   number "nearby"
1de52 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75  .  This can be u
1de53 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 74 74  sed in an.** att
1de54 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72 65 6c  empt to keep rel
1de55 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f 73 65  ated pages close
1de56 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 20 69   to each other i
1de57 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
1de58 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 69 6e  ile,.** which in
1de59 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65 20 64   turn can make d
1de5a 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 66  atabase access f
1de5b 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  aster..**.** If 
1de5c 74 68 65 20 22 65 78 61 63 74 22 20 70 61 72 61  the "exact" para
1de5d 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20  meter is not 0, 
1de5e 61 6e 64 20 74 68 65 20 70 61 67 65 2d 6e 75 6d  and the page-num
1de5f 62 65 72 20 6e 65 61 72 62 79 20 65 78 69 73 74  ber nearby exist
1de60 73 20 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20 6f  s .** anywhere o
1de61 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c  n the free-list,
1de62 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 61 72   then it is guar
1de63 65 6e 74 65 65 64 20 74 6f 20 62 65 20 72 65 74  enteed to be ret
1de64 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a 20 69  urned. This.** i
1de65 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 61  s only used by a
1de66 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62  uto-vacuum datab
1de67 61 73 65 73 20 77 68 65 6e 20 61 6c 6c 6f 63 61  ases when alloca
1de68 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c 65  ting a new table
1de69 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1de6a 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67  allocateBtreePag
1de6b 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e(.  BtShared *p
1de6c 42 74 2c 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a  Bt, .  MemPage *
1de6d 2a 70 70 50 61 67 65 2c 20 0a 20 20 50 67 6e 6f  *ppPage, .  Pgno
1de6e 20 2a 70 50 67 6e 6f 2c 20 0a 20 20 50 67 6e 6f   *pPgno, .  Pgno
1de6f 20 6e 65 61 72 62 79 2c 0a 20 20 75 38 20 65 78   nearby,.  u8 ex
1de70 61 63 74 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65  act.){.  MemPage
1de71 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e 74 20   *pPage1;.  int 
1de72 72 63 3b 0a 20 20 75 33 32 20 6e 3b 20 20 20 20  rc;.  u32 n;    
1de73 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
1de74 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c  ges on the freel
1de75 69 73 74 20 2a 2f 0a 20 20 75 33 32 20 6b 3b 20  ist */.  u32 k; 
1de76 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1de77 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65 20 74   leaves on the t
1de78 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72 65 65  runk of the free
1de79 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  list */.  MemPag
1de7a 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20  e *pTrunk = 0;. 
1de7b 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65 76 54   MemPage *pPrevT
1de7c 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 50 67 6e 6f  runk = 0;.  Pgno
1de7d 20 6d 78 50 61 67 65 3b 20 20 20 20 20 2f 2a 20   mxPage;     /* 
1de7e 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 74 68  Total size of th
1de7f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1de80 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  */..  assert( sq
1de81 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1de82 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
1de83 20 20 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e    pPage1 = pBt->
1de84 70 50 61 67 65 31 3b 0a 20 20 6d 78 50 61 67 65  pPage1;.  mxPage
1de85 20 3d 20 62 74 72 65 65 50 61 67 65 63 6f 75 6e   = btreePagecoun
1de86 74 28 70 42 74 29 3b 0a 20 20 6e 20 3d 20 67 65  t(pBt);.  n = ge
1de87 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e  t4byte(&pPage1->
1de88 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 74 65  aData[36]);.  te
1de89 73 74 63 61 73 65 28 20 6e 3d 3d 6d 78 50 61 67  stcase( n==mxPag
1de8a 65 2d 31 20 29 3b 0a 20 20 69 66 28 20 6e 3e 3d  e-1 );.  if( n>=
1de8b 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20 72 65  mxPage ){.    re
1de8c 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
1de8d 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20  UPT_BKPT;.  }.  
1de8e 69 66 28 20 6e 3e 30 20 29 7b 0a 20 20 20 20 2f  if( n>0 ){.    /
1de8f 2a 20 54 68 65 72 65 20 61 72 65 20 70 61 67 65  * There are page
1de90 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73  s on the freelis
1de91 74 2e 20 20 52 65 75 73 65 20 6f 6e 65 20 6f 66  t.  Reuse one of
1de92 20 74 68 6f 73 65 20 70 61 67 65 73 2e 20 2a 2f   those pages. */
1de93 0a 20 20 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b  .    Pgno iTrunk
1de94 3b 0a 20 20 20 20 75 38 20 73 65 61 72 63 68 4c  ;.    u8 searchL
1de95 69 73 74 20 3d 20 30 3b 20 2f 2a 20 49 66 20 74  ist = 0; /* If t
1de96 68 65 20 66 72 65 65 2d 6c 69 73 74 20 6d 75 73  he free-list mus
1de97 74 20 62 65 20 73 65 61 72 63 68 65 64 20 66 6f  t be searched fo
1de98 72 20 27 6e 65 61 72 62 79 27 20 2a 2f 0a 20 20  r 'nearby' */.  
1de99 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65    .    /* If the
1de9a 20 27 65 78 61 63 74 27 20 70 61 72 61 6d 65 74   'exact' paramet
1de9b 65 72 20 77 61 73 20 74 72 75 65 20 61 6e 64 20  er was true and 
1de9c 61 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 70  a query of the p
1de9d 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 2a  ointer-map.    *
1de9e 2a 20 73 68 6f 77 73 20 74 68 61 74 20 74 68 65  * shows that the
1de9f 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 69   page 'nearby' i
1dea0 73 20 73 6f 6d 65 77 68 65 72 65 20 6f 6e 20 74  s somewhere on t
1dea1 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 68  he free-list, th
1dea2 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 6e  en.    ** the en
1dea3 74 69 72 65 2d 6c 69 73 74 20 77 69 6c 6c 20 62  tire-list will b
1dea4 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 74  e searched for t
1dea5 68 61 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f  hat page..    */
1dea6 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1dea7 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
1dea8 20 20 20 20 69 66 28 20 65 78 61 63 74 20 26 26      if( exact &&
1dea9 20 6e 65 61 72 62 79 3c 3d 6d 78 50 61 67 65 20   nearby<=mxPage 
1deaa 29 7b 0a 20 20 20 20 20 20 75 38 20 65 54 79 70  ){.      u8 eTyp
1deab 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e;.      assert(
1deac 20 6e 65 61 72 62 79 3e 30 20 29 3b 0a 20 20 20   nearby>0 );.   
1dead 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e     assert( pBt->
1deae 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20  autoVacuum );.  
1deaf 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47      rc = ptrmapG
1deb0 65 74 28 70 42 74 2c 20 6e 65 61 72 62 79 2c 20  et(pBt, nearby, 
1deb1 26 65 54 79 70 65 2c 20 30 29 3b 0a 20 20 20 20  &eType, 0);.    
1deb2 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
1deb3 6e 20 72 63 3b 0a 20 20 20 20 20 20 69 66 28 20  n rc;.      if( 
1deb4 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52  eType==PTRMAP_FR
1deb5 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20  EEPAGE ){.      
1deb6 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 31    searchList = 1
1deb7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1deb8 2a 70 50 67 6e 6f 20 3d 20 6e 65 61 72 62 79 3b  *pPgno = nearby;
1deb9 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
1deba 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20     /* Decrement 
1debb 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 63 6f  the free-list co
1debc 75 6e 74 20 62 79 20 31 2e 20 53 65 74 20 69 54  unt by 1. Set iT
1debd 72 75 6e 6b 20 74 6f 20 74 68 65 20 69 6e 64 65  runk to the inde
1debe 78 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  x of the.    ** 
1debf 66 69 72 73 74 20 66 72 65 65 2d 6c 69 73 74 20  first free-list 
1dec0 74 72 75 6e 6b 20 70 61 67 65 2e 20 69 50 72 65  trunk page. iPre
1dec1 76 54 72 75 6e 6b 20 69 73 20 69 6e 69 74 69 61  vTrunk is initia
1dec2 6c 6c 79 20 31 2e 0a 20 20 20 20 2a 2f 0a 20 20  lly 1..    */.  
1dec3 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1dec4 67 65 72 57 72 69 74 65 28 70 50 61 67 65 31 2d  gerWrite(pPage1-
1dec5 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  >pDbPage);.    i
1dec6 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1dec7 63 3b 0a 20 20 20 20 70 75 74 34 62 79 74 65 28  c;.    put4byte(
1dec8 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
1dec9 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20 20 20 20 2f  6], n-1);..    /
1deca 2a 20 54 68 65 20 63 6f 64 65 20 77 69 74 68 69  * The code withi
1decb 6e 20 74 68 69 73 20 6c 6f 6f 70 20 69 73 20 72  n this loop is r
1decc 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65 20 69 66 20  un only once if 
1decd 74 68 65 20 27 73 65 61 72 63 68 4c 69 73 74 27  the 'searchList'
1dece 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a   variable.    **
1decf 20 69 73 20 6e 6f 74 20 74 72 75 65 2e 20 4f 74   is not true. Ot
1ded0 68 65 72 77 69 73 65 2c 20 69 74 20 72 75 6e 73  herwise, it runs
1ded1 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 74   once for each t
1ded2 72 75 6e 6b 2d 70 61 67 65 20 6f 6e 20 74 68 65  runk-page on the
1ded3 0a 20 20 20 20 2a 2a 20 66 72 65 65 2d 6c 69 73  .    ** free-lis
1ded4 74 20 75 6e 74 69 6c 20 74 68 65 20 70 61 67 65  t until the page
1ded5 20 27 6e 65 61 72 62 79 27 20 69 73 20 6c 6f 63   'nearby' is loc
1ded6 61 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ated..    */.   
1ded7 20 64 6f 20 7b 0a 20 20 20 20 20 20 70 50 72 65   do {.      pPre
1ded8 76 54 72 75 6e 6b 20 3d 20 70 54 72 75 6e 6b 3b  vTrunk = pTrunk;
1ded9 0a 20 20 20 20 20 20 69 66 28 20 70 50 72 65 76  .      if( pPrev
1deda 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20  Trunk ){.       
1dedb 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79   iTrunk = get4by
1dedc 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e  te(&pPrevTrunk->
1dedd 61 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20  aData[0]);.     
1dede 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1dedf 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74  iTrunk = get4byt
1dee0 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
1dee1 5b 33 32 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20  [32]);.      }. 
1dee2 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
1dee3 54 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b  Trunk==mxPage );
1dee4 0a 20 20 20 20 20 20 69 66 28 20 69 54 72 75 6e  .      if( iTrun
1dee5 6b 3e 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20  k>mxPage ){.    
1dee6 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1dee7 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1dee8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1dee9 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74     rc = btreeGet
1deea 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e 6b  Page(pBt, iTrunk
1deeb 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20  , &pTrunk, 0);. 
1deec 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
1deed 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 70   rc ){.        p
1deee 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20  Trunk = 0;.     
1deef 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f     goto end_allo
1def0 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20  cate_page;.     
1def1 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
1def2 20 70 54 72 75 6e 6b 21 3d 30 20 29 3b 0a 20 20   pTrunk!=0 );.  
1def3 20 20 20 20 61 73 73 65 72 74 28 20 70 54 72 75      assert( pTru
1def4 6e 6b 2d 3e 61 44 61 74 61 21 3d 30 20 29 3b 0a  nk->aData!=0 );.
1def5 0a 20 20 20 20 20 20 6b 20 3d 20 67 65 74 34 62  .      k = get4b
1def6 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61  yte(&pTrunk->aDa
1def7 74 61 5b 34 5d 29 3b 20 2f 2a 20 23 20 6f 66 20  ta[4]); /* # of 
1def8 6c 65 61 76 65 73 20 6f 6e 20 74 68 69 73 20 74  leaves on this t
1def9 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 20 20 20  runk page */.   
1defa 20 20 20 69 66 28 20 6b 3d 3d 30 20 26 26 20 21     if( k==0 && !
1defb 73 65 61 72 63 68 4c 69 73 74 20 29 7b 0a 20 20  searchList ){.  
1defc 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75        /* The tru
1defd 6e 6b 20 68 61 73 20 6e 6f 20 6c 65 61 76 65 73  nk has no leaves
1defe 20 61 6e 64 20 74 68 65 20 6c 69 73 74 20 69 73   and the list is
1deff 20 6e 6f 74 20 62 65 69 6e 67 20 73 65 61 72 63   not being searc
1df00 68 65 64 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a  hed. .        **
1df01 20 53 6f 20 65 78 74 72 61 63 74 20 74 68 65 20   So extract the 
1df02 74 72 75 6e 6b 20 70 61 67 65 20 69 74 73 65 6c  trunk page itsel
1df03 66 20 61 6e 64 20 75 73 65 20 69 74 20 61 73 20  f and use it as 
1df04 74 68 65 20 6e 65 77 6c 79 20 0a 20 20 20 20 20  the newly .     
1df05 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20     ** allocated 
1df06 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  page */.        
1df07 61 73 73 65 72 74 28 20 70 50 72 65 76 54 72 75  assert( pPrevTru
1df08 6e 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  nk==0 );.       
1df09 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1df0a 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e  erWrite(pTrunk->
1df0b 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
1df0c 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
1df0d 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61        goto end_a
1df0e 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20  llocate_page;.  
1df0f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1df10 2a 70 50 67 6e 6f 20 3d 20 69 54 72 75 6e 6b 3b  *pPgno = iTrunk;
1df11 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
1df12 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
1df13 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61  2], &pTrunk->aDa
1df14 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20  ta[0], 4);.     
1df15 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72     *ppPage = pTr
1df16 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 70 54 72  unk;.        pTr
1df17 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  unk = 0;.       
1df18 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54   TRACE(("ALLOCAT
1df19 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 64  E: %d trunk - %d
1df1a 20 66 72 65 65 20 70 61 67 65 73 20 6c 65 66 74   free pages left
1df1b 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31  \n", *pPgno, n-1
1df1c 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  ));.      }else 
1df1d 69 66 28 20 6b 3e 28 75 33 32 29 28 70 42 74 2d  if( k>(u32)(pBt-
1df1e 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20  >usableSize/4 - 
1df1f 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  2) ){.        /*
1df20 20 56 61 6c 75 65 20 6f 66 20 6b 20 69 73 20 6f   Value of k is o
1df21 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 44 61  ut of range.  Da
1df22 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f  tabase corruptio
1df23 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20  n */.        rc 
1df24 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  = SQLITE_CORRUPT
1df25 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 67  _BKPT;.        g
1df26 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
1df27 5f 70 61 67 65 3b 0a 23 69 66 6e 64 65 66 20 53  _page;.#ifndef S
1df28 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
1df29 41 43 55 55 4d 0a 20 20 20 20 20 20 7d 65 6c 73  ACUUM.      }els
1df2a 65 20 69 66 28 20 73 65 61 72 63 68 4c 69 73 74  e if( searchList
1df2b 20 26 26 20 6e 65 61 72 62 79 3d 3d 69 54 72 75   && nearby==iTru
1df2c 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  nk ){.        /*
1df2d 20 54 68 65 20 6c 69 73 74 20 69 73 20 62 65 69   The list is bei
1df2e 6e 67 20 73 65 61 72 63 68 65 64 20 61 6e 64 20  ng searched and 
1df2f 74 68 69 73 20 74 72 75 6e 6b 20 70 61 67 65 20  this trunk page 
1df30 69 73 20 74 68 65 20 70 61 67 65 0a 20 20 20 20  is the page.    
1df31 20 20 20 20 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61      ** to alloca
1df32 74 65 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f  te, regardless o
1df33 66 20 77 68 65 74 68 65 72 20 69 74 20 68 61 73  f whether it has
1df34 20 6c 65 61 76 65 73 2e 0a 20 20 20 20 20 20 20   leaves..       
1df35 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65   */.        asse
1df36 72 74 28 20 2a 70 50 67 6e 6f 3d 3d 69 54 72 75  rt( *pPgno==iTru
1df37 6e 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 2a 70  nk );.        *p
1df38 70 50 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a  pPage = pTrunk;.
1df39 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69          searchLi
1df3a 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  st = 0;.        
1df3b 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1df3c 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70  rWrite(pTrunk->p
1df3d 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
1df3e 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
1df3f 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c       goto end_al
1df40 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20  locate_page;.   
1df41 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1df42 66 28 20 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20  f( k==0 ){.     
1df43 20 20 20 20 20 69 66 28 20 21 70 50 72 65 76 54       if( !pPrevT
1df44 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20  runk ){.        
1df45 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67      memcpy(&pPag
1df46 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26  e1->aData[32], &
1df47 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d  pTrunk->aData[0]
1df48 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 4);.          
1df49 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
1df4a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1df4b 61 67 65 72 57 72 69 74 65 28 70 50 72 65 76 54  agerWrite(pPrevT
1df4c 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a  runk->pDbPage);.
1df4d 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
1df4e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1df4f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67  .              g
1df50 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
1df51 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20  _page;.         
1df52 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
1df53 20 6d 65 6d 63 70 79 28 26 70 50 72 65 76 54 72   memcpy(&pPrevTr
1df54 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26  unk->aData[0], &
1df55 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d  pTrunk->aData[0]
1df56 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 4);.          
1df57 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  }.        }else{
1df58 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
1df59 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20  e trunk page is 
1df5a 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20  required by the 
1df5b 63 61 6c 6c 65 72 20 62 75 74 20 69 74 20 63 6f  caller but it co
1df5c 6e 74 61 69 6e 73 20 0a 20 20 20 20 20 20 20 20  ntains .        
1df5d 20 20 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f    ** pointers to
1df5e 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 76 65   free-list leave
1df5f 73 2e 20 54 68 65 20 66 69 72 73 74 20 6c 65 61  s. The first lea
1df60 66 20 62 65 63 6f 6d 65 73 20 61 20 74 72 75 6e  f becomes a trun
1df61 6b 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70  k.          ** p
1df62 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 73 65  age in this case
1df63 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
1df64 20 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65           MemPage
1df65 20 2a 70 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 20   *pNewTrunk;.   
1df66 20 20 20 20 20 20 20 50 67 6e 6f 20 69 4e 65 77         Pgno iNew
1df67 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65  Trunk = get4byte
1df68 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  (&pTrunk->aData[
1df69 38 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  8]);.          i
1df6a 66 28 20 69 4e 65 77 54 72 75 6e 6b 3e 6d 78 50  f( iNewTrunk>mxP
1df6b 61 67 65 20 29 7b 20 0a 20 20 20 20 20 20 20 20  age ){ .        
1df6c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1df6d 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1df6e 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
1df6f 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65  nd_allocate_page
1df70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1df71 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
1df72 28 20 69 4e 65 77 54 72 75 6e 6b 3d 3d 6d 78 50  ( iNewTrunk==mxP
1df73 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20  age );.         
1df74 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61   rc = btreeGetPa
1df75 67 65 28 70 42 74 2c 20 69 4e 65 77 54 72 75 6e  ge(pBt, iNewTrun
1df76 6b 2c 20 26 70 4e 65 77 54 72 75 6e 6b 2c 20 30  k, &pNewTrunk, 0
1df77 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
1df78 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1df79 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f  {.            go
1df7a 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f  to end_allocate_
1df7b 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20  page;.          
1df7c 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  }.          rc =
1df7d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
1df7e 74 65 28 70 4e 65 77 54 72 75 6e 6b 2d 3e 70 44  te(pNewTrunk->pD
1df7f 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
1df80 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1df81 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
1df82 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1df83 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20  NewTrunk);.     
1df84 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
1df85 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20  allocate_page;. 
1df86 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1df87 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65       memcpy(&pNe
1df88 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d  wTrunk->aData[0]
1df89 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61  , &pTrunk->aData
1df8a 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20  [0], 4);.       
1df8b 20 20 20 70 75 74 34 62 79 74 65 28 26 70 4e 65     put4byte(&pNe
1df8c 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d  wTrunk->aData[4]
1df8d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  , k-1);.        
1df8e 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 54 72    memcpy(&pNewTr
1df8f 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 26  unk->aData[8], &
1df90 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 31 32  pTrunk->aData[12
1df91 5d 2c 20 28 6b 2d 31 29 2a 34 29 3b 0a 20 20 20  ], (k-1)*4);.   
1df92 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
1df93 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20  ge(pNewTrunk);. 
1df94 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 50           if( !pP
1df95 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20  revTrunk ){.    
1df96 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1df97 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
1df98 69 74 65 61 62 6c 65 28 70 50 61 67 65 31 2d 3e  iteable(pPage1->
1df99 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20  pDbPage) );.    
1df9a 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65          put4byte
1df9b 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  (&pPage1->aData[
1df9c 33 32 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b  32], iNewTrunk);
1df9d 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
1df9e 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  {.            rc
1df9f 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
1dfa0 72 69 74 65 28 70 50 72 65 76 54 72 75 6e 6b 2d  rite(pPrevTrunk-
1dfa1 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
1dfa2 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b         if( rc ){
1dfa3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67  .              g
1dfa4 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
1dfa5 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20  _page;.         
1dfa6 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
1dfa7 20 70 75 74 34 62 79 74 65 28 26 70 50 72 65 76   put4byte(&pPrev
1dfa8 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c  Trunk->aData[0],
1dfa9 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20   iNewTrunk);.   
1dfaa 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1dfab 20 7d 0a 20 20 20 20 20 20 20 20 70 54 72 75 6e   }.        pTrun
1dfac 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 54  k = 0;.        T
1dfad 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a  RACE(("ALLOCATE:
1dfae 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 20 66   %d trunk - %d f
1dfaf 72 65 65 20 70 61 67 65 73 20 6c 65 66 74 5c 6e  ree pages left\n
1dfb0 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 29  ", *pPgno, n-1))
1dfb1 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7d  ;.#endif.      }
1dfb2 65 6c 73 65 20 69 66 28 20 6b 3e 30 20 29 7b 0a  else if( k>0 ){.
1dfb3 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61          /* Extra
1dfb4 63 74 20 61 20 6c 65 61 66 20 66 72 6f 6d 20 74  ct a leaf from t
1dfb5 68 65 20 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20  he trunk */.    
1dfb6 20 20 20 20 75 33 32 20 63 6c 6f 73 65 73 74 3b      u32 closest;
1dfb7 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 50  .        Pgno iP
1dfb8 61 67 65 3b 0a 20 20 20 20 20 20 20 20 75 6e 73  age;.        uns
1dfb9 69 67 6e 65 64 20 63 68 61 72 20 2a 61 44 61 74  igned char *aDat
1dfba 61 20 3d 20 70 54 72 75 6e 6b 2d 3e 61 44 61 74  a = pTrunk->aDat
1dfbb 61 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e  a;.        if( n
1dfbc 65 61 72 62 79 3e 30 20 29 7b 0a 20 20 20 20 20  earby>0 ){.     
1dfbd 20 20 20 20 20 75 33 32 20 69 3b 0a 20 20 20 20       u32 i;.    
1dfbe 20 20 20 20 20 20 69 6e 74 20 64 69 73 74 3b 0a        int dist;.
1dfbf 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73            closes
1dfc0 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  t = 0;.         
1dfc1 20 64 69 73 74 20 3d 20 73 71 6c 69 74 65 33 41   dist = sqlite3A
1dfc2 62 73 49 6e 74 33 32 28 67 65 74 34 62 79 74 65  bsInt32(get4byte
1dfc3 28 26 61 44 61 74 61 5b 38 5d 29 20 2d 20 6e 65  (&aData[8]) - ne
1dfc4 61 72 62 79 29 3b 0a 20 20 20 20 20 20 20 20 20  arby);.         
1dfc5 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69   for(i=1; i<k; i
1dfc6 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
1dfc7 20 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65   int d2 = sqlite
1dfc8 33 41 62 73 49 6e 74 33 32 28 67 65 74 34 62 79  3AbsInt32(get4by
1dfc9 74 65 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d  te(&aData[8+i*4]
1dfca 29 20 2d 20 6e 65 61 72 62 79 29 3b 0a 20 20 20  ) - nearby);.   
1dfcb 20 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c           if( d2<
1dfcc 64 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  dist ){.        
1dfcd 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20        closest = 
1dfce 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  i;.             
1dfcf 20 64 69 73 74 20 3d 20 64 32 3b 0a 20 20 20 20   dist = d2;.    
1dfd0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1dfd1 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65      }.        }e
1dfd2 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63  lse{.          c
1dfd3 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20  losest = 0;.    
1dfd4 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69      }..        i
1dfd5 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28  Page = get4byte(
1dfd6 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74  &aData[8+closest
1dfd7 2a 34 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65  *4]);.        te
1dfd8 73 74 63 61 73 65 28 20 69 50 61 67 65 3d 3d 6d  stcase( iPage==m
1dfd9 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20  xPage );.       
1dfda 20 69 66 28 20 69 50 61 67 65 3e 6d 78 50 61 67   if( iPage>mxPag
1dfdb 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  e ){.          r
1dfdc 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  c = SQLITE_CORRU
1dfdd 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20  PT_BKPT;.       
1dfde 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f     goto end_allo
1dfdf 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20  cate_page;.     
1dfe0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 74 65 73     }.        tes
1dfe1 74 63 61 73 65 28 20 69 50 61 67 65 3d 3d 6d 78  tcase( iPage==mx
1dfe2 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20 20  Page );.        
1dfe3 69 66 28 20 21 73 65 61 72 63 68 4c 69 73 74 20  if( !searchList 
1dfe4 7c 7c 20 69 50 61 67 65 3d 3d 6e 65 61 72 62 79  || iPage==nearby
1dfe5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   ){.          in
1dfe6 74 20 6e 6f 43 6f 6e 74 65 6e 74 3b 0a 20 20 20  t noContent;.   
1dfe7 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20         *pPgno = 
1dfe8 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20  iPage;.         
1dfe9 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54   TRACE(("ALLOCAT
1dfea 45 3a 20 25 64 20 77 61 73 20 6c 65 61 66 20 25  E: %d was leaf %
1dfeb 64 20 6f 66 20 25 64 20 6f 6e 20 74 72 75 6e 6b  d of %d on trunk
1dfec 20 25 64 22 0a 20 20 20 20 20 20 20 20 20 20 20   %d".           
1dfed 20 20 20 20 20 20 22 3a 20 25 64 20 6d 6f 72 65        ": %d more
1dfee 20 66 72 65 65 20 70 61 67 65 73 5c 6e 22 2c 0a   free pages\n",.
1dfef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dff0 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73 65 73 74   *pPgno, closest
1dff1 2b 31 2c 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e 70  +1, k, pTrunk->p
1dff2 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20  gno, n-1));.    
1dff3 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1dff4 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72  e3PagerWrite(pTr
1dff5 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  unk->pDbPage);. 
1dff6 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20           if( rc 
1dff7 29 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63  ) goto end_alloc
1dff8 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20  ate_page;.      
1dff9 20 20 20 20 69 66 28 20 63 6c 6f 73 65 73 74 3c      if( closest<
1dffa 6b 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20  k-1 ){.         
1dffb 20 20 20 6d 65 6d 63 70 79 28 26 61 44 61 74 61     memcpy(&aData
1dffc 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 2c 20 26  [8+closest*4], &
1dffd 61 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29  aData[4+k*4], 4)
1dffe 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1dfff 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65          put4byte
1e000 28 26 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29  (&aData[4], k-1)
1e001 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 43 6f  ;.          noCo
1e002 6e 74 65 6e 74 20 3d 20 21 62 74 72 65 65 47 65  ntent = !btreeGe
1e003 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c  tHasContent(pBt,
1e004 20 2a 70 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20   *pPgno);.      
1e005 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65      rc = btreeGe
1e006 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e  tPage(pBt, *pPgn
1e007 6f 2c 20 70 70 50 61 67 65 2c 20 6e 6f 43 6f 6e  o, ppPage, noCon
1e008 74 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20  tent);.         
1e009 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1e00a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
1e00b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1e00c 67 65 72 57 72 69 74 65 28 28 2a 70 70 50 61 67  gerWrite((*ppPag
1e00d 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  e)->pDbPage);.  
1e00e 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
1e00f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1e010 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c               rel
1e011 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65  easePage(*ppPage
1e012 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  );.            }
1e013 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1e014 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73         searchLis
1e015 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  t = 0;.        }
1e016 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
1e017 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 65 76  eleasePage(pPrev
1e018 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 70 50  Trunk);.      pP
1e019 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20  revTrunk = 0;.  
1e01a 20 20 7d 77 68 69 6c 65 28 20 73 65 61 72 63 68    }while( search
1e01b 4c 69 73 74 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  List );.  }else{
1e01c 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72  .    /* There ar
1e01d 65 20 6e 6f 20 70 61 67 65 73 20 6f 6e 20 74 68  e no pages on th
1e01e 65 20 66 72 65 65 6c 69 73 74 2c 20 73 6f 20 63  e freelist, so c
1e01f 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65  reate a new page
1e020 20 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 65   at the.    ** e
1e021 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a  nd of the file *
1e022 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
1e023 65 33 50 61 67 65 72 57 72 69 74 65 28 70 42 74  e3PagerWrite(pBt
1e024 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67  ->pPage1->pDbPag
1e025 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  e);.    if( rc )
1e026 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
1e027 70 42 74 2d 3e 6e 50 61 67 65 2b 2b 3b 0a 20 20  pBt->nPage++;.  
1e028 20 20 69 66 28 20 70 42 74 2d 3e 6e 50 61 67 65    if( pBt->nPage
1e029 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  ==PENDING_BYTE_P
1e02a 41 47 45 28 70 42 74 29 20 29 20 70 42 74 2d 3e  AGE(pBt) ) pBt->
1e02b 6e 50 61 67 65 2b 2b 3b 0a 0a 23 69 66 6e 64 65  nPage++;..#ifnde
1e02c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1e02d 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
1e02e 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
1e02f 20 26 26 20 50 54 52 4d 41 50 5f 49 53 50 41 47   && PTRMAP_ISPAG
1e030 45 28 70 42 74 2c 20 70 42 74 2d 3e 6e 50 61 67  E(pBt, pBt->nPag
1e031 65 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  e) ){.      /* I
1e032 66 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20  f *pPgno refers 
1e033 74 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70  to a pointer-map
1e034 20 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20   page, allocate 
1e035 74 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20  two new pages.  
1e036 20 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e      ** at the en
1e037 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e  d of the file in
1e038 73 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68  stead of one. Th
1e039 65 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65  e first allocate
1e03a 64 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20  d page.      ** 
1e03b 62 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f  becomes a new po
1e03c 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20  inter-map page, 
1e03d 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73  the second is us
1e03e 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ed by the caller
1e03f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1e040 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 20 3d 20   MemPage *pPg = 
1e041 30 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28  0;.      TRACE((
1e042 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72  "ALLOCATE: %d fr
1e043 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 28  om end of file (
1e044 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65  pointer-map page
1e045 29 5c 6e 22 2c 20 70 42 74 2d 3e 6e 50 61 67 65  )\n", pBt->nPage
1e046 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ));.      assert
1e047 28 20 70 42 74 2d 3e 6e 50 61 67 65 21 3d 50 45  ( pBt->nPage!=PE
1e048 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
1e049 70 42 74 29 20 29 3b 0a 20 20 20 20 20 20 72 63  pBt) );.      rc
1e04a 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
1e04b 70 42 74 2c 20 70 42 74 2d 3e 6e 50 61 67 65 2c  pBt, pBt->nPage,
1e04c 20 26 70 50 67 2c 20 31 29 3b 0a 20 20 20 20 20   &pPg, 1);.     
1e04d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1e04e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
1e04f 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
1e050 72 69 74 65 28 70 50 67 2d 3e 70 44 62 50 61 67  rite(pPg->pDbPag
1e051 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65  e);.        rele
1e052 61 73 65 50 61 67 65 28 70 50 67 29 3b 0a 20 20  asePage(pPg);.  
1e053 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1e054 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
1e055 20 20 20 20 20 20 70 42 74 2d 3e 6e 50 61 67 65        pBt->nPage
1e056 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42  ++;.      if( pB
1e057 74 2d 3e 6e 50 61 67 65 3d 3d 50 45 4e 44 49 4e  t->nPage==PENDIN
1e058 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
1e059 20 29 7b 20 70 42 74 2d 3e 6e 50 61 67 65 2b 2b   ){ pBt->nPage++
1e05a 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ; }.    }.#endif
1e05b 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 32 38  .    put4byte(28
1e05c 20 2b 20 28 75 38 2a 29 70 42 74 2d 3e 70 50 61   + (u8*)pBt->pPa
1e05d 67 65 31 2d 3e 61 44 61 74 61 2c 20 70 42 74 2d  ge1->aData, pBt-
1e05e 3e 6e 50 61 67 65 29 3b 0a 20 20 20 20 2a 70 50  >nPage);.    *pP
1e05f 67 6e 6f 20 3d 20 70 42 74 2d 3e 6e 50 61 67 65  gno = pBt->nPage
1e060 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a  ;..    assert( *
1e061 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42  pPgno!=PENDING_B
1e062 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b  YTE_PAGE(pBt) );
1e063 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47  .    rc = btreeG
1e064 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67  etPage(pBt, *pPg
1e065 6e 6f 2c 20 70 70 50 61 67 65 2c 20 31 29 3b 0a  no, ppPage, 1);.
1e066 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
1e067 75 72 6e 20 72 63 3b 0a 20 20 20 20 72 63 20 3d  urn rc;.    rc =
1e068 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
1e069 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44  te((*ppPage)->pD
1e06a 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  bPage);.    if( 
1e06b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1e06c 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
1e06d 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20  ge(*ppPage);.   
1e06e 20 7d 0a 20 20 20 20 54 52 41 43 45 28 28 22 41   }.    TRACE(("A
1e06f 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d  LLOCATE: %d from
1e070 20 65 6e 64 20 6f 66 20 66 69 6c 65 5c 6e 22 2c   end of file\n",
1e071 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a   *pPgno));.  }..
1e072 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f    assert( *pPgno
1e073 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  !=PENDING_BYTE_P
1e074 41 47 45 28 70 42 74 29 20 29 3b 0a 0a 65 6e 64  AGE(pBt) );..end
1e075 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3a 0a  _allocate_page:.
1e076 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
1e077 72 75 6e 6b 29 3b 0a 20 20 72 65 6c 65 61 73 65  runk);.  release
1e078 50 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29  Page(pPrevTrunk)
1e079 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
1e07a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
1e07b 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
1e07c 65 52 65 66 63 6f 75 6e 74 28 28 2a 70 70 50 61  eRefcount((*ppPa
1e07d 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3e 31 20  ge)->pDbPage)>1 
1e07e 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  ){.      release
1e07f 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20  Page(*ppPage);. 
1e080 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1e081 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1e082 0a 20 20 20 20 7d 0a 20 20 20 20 28 2a 70 70 50  .    }.    (*ppP
1e083 61 67 65 29 2d 3e 69 73 49 6e 69 74 20 3d 20 30  age)->isInit = 0
1e084 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a  ;.  }else{.    *
1e085 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a  ppPage = 0;.  }.
1e086 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
1e087 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 71 6c 69 74  LITE_OK || sqlit
1e088 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
1e089 6c 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44  le((*ppPage)->pD
1e08a 62 50 61 67 65 29 20 29 3b 0a 20 20 72 65 74 75  bPage) );.  retu
1e08b 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1e08c 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
1e08d 20 75 73 65 64 20 74 6f 20 61 64 64 20 70 61 67   used to add pag
1e08e 65 20 69 50 61 67 65 20 74 6f 20 74 68 65 20 64  e iPage to the d
1e08f 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 72 65  atabase file fre
1e090 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74 20 69  e-list. .** It i
1e091 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74  s assumed that t
1e092 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  he page is not a
1e093 6c 72 65 61 64 79 20 61 20 70 61 72 74 20 6f 66  lready a part of
1e094 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a   the free-list..
1e095 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  **.** The value 
1e096 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
1e097 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
1e098 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
1e099 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a 20 49  s optional..** I
1e09a 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 70  f the caller hap
1e09b 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61 20 70  pens to have a p
1e09c 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65  ointer to the Me
1e09d 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a 2a 2a  mPage object .**
1e09e 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
1e09f 6f 20 70 61 67 65 20 69 50 61 67 65 20 68 61 6e  o page iPage han
1e0a0 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73 73 20  dy, it may pass 
1e0a1 69 74 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  it as the second
1e0a2 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74 68 65   value. .** Othe
1e0a3 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20 70 61  rwise, it may pa
1e0a4 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49  ss NULL..**.** I
1e0a5 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  f a pointer to a
1e0a6 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20   MemPage object 
1e0a7 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65  is passed as the
1e0a8 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
1e0a9 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72 65 6e  ,.** its referen
1e0aa 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f 74 20  ce count is not 
1e0ab 61 6c 74 65 72 65 64 20 62 79 20 74 68 69 73 20  altered by this 
1e0ac 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
1e0ad 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 67 65  tic int freePage
1e0ae 32 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c  2(BtShared *pBt,
1e0af 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d 50 61   MemPage *pMemPa
1e0b0 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 29 7b  ge, Pgno iPage){
1e0b1 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75  .  MemPage *pTru
1e0b2 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  nk = 0;         
1e0b3 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 2d 6c         /* Free-l
1e0b4 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 2a  ist trunk page *
1e0b5 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b 20  /.  Pgno iTrunk 
1e0b6 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
1e0b7 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
1e0b8 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 2d 6c  number of free-l
1e0b9 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 2a  ist trunk page *
1e0ba 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  / .  MemPage *pP
1e0bb 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67  age1 = pBt->pPag
1e0bc 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61  e1;      /* Loca
1e0bd 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70  l reference to p
1e0be 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d 50 61  age 1 */.  MemPa
1e0bf 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20  ge *pPage;      
1e0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1e0c1 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66 72 65  * Page being fre
1e0c2 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 2e  ed. May be NULL.
1e0c3 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
1e0c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e0c5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
1e0c6 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69 6e  urn Code */.  in
1e0c7 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20  t nFree;        
1e0c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e0c9 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e 75 6d    /* Initial num
1e0ca 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20  ber of pages on 
1e0cb 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20  free-list */..  
1e0cc 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1e0cd 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
1e0ce 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
1e0cf 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b 0a 20  rt( iPage>1 );. 
1e0d0 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d 50 61   assert( !pMemPa
1e0d1 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65 2d 3e  ge || pMemPage->
1e0d2 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b 0a 0a  pgno==iPage );..
1e0d3 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65 20 29    if( pMemPage )
1e0d4 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 4d  {.    pPage = pM
1e0d5 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 6c 69  emPage;.    sqli
1e0d6 74 65 33 50 61 67 65 72 52 65 66 28 70 50 61 67  te3PagerRef(pPag
1e0d7 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d  e->pDbPage);.  }
1e0d8 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 20  else{.    pPage 
1e0d9 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f 6b 75  = btreePageLooku
1e0da 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a 20  p(pBt, iPage);. 
1e0db 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d 65   }..  /* Increme
1e0dc 6e 74 20 74 68 65 20 66 72 65 65 20 70 61 67 65  nt the free page
1e0dd 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 65 31   count on pPage1
1e0de 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
1e0df 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
1e0e0 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  ge1->pDbPage);. 
1e0e1 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 66   if( rc ) goto f
1e0e2 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 6e  reepage_out;.  n
1e0e3 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 65 28  Free = get4byte(
1e0e4 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
1e0e5 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28  6]);.  put4byte(
1e0e6 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
1e0e7 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a 0a 20  6], nFree+1);.. 
1e0e8 20 69 66 28 20 70 42 74 2d 3e 62 74 73 46 6c 61   if( pBt->btsFla
1e0e9 67 73 20 26 20 42 54 53 5f 53 45 43 55 52 45 5f  gs & BTS_SECURE_
1e0ea 44 45 4c 45 54 45 20 29 7b 0a 20 20 20 20 2f 2a  DELETE ){.    /*
1e0eb 20 49 66 20 74 68 65 20 73 65 63 75 72 65 5f 64   If the secure_d
1e0ec 65 6c 65 74 65 20 6f 70 74 69 6f 6e 20 69 73 20  elete option is 
1e0ed 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 0a 20 20  enabled, then.  
1e0ee 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 75 6c 6c    ** always full
1e0ef 79 20 6f 76 65 72 77 72 69 74 65 20 64 65 6c 65  y overwrite dele
1e0f0 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ted information 
1e0f1 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20  with zeros..    
1e0f2 2a 2f 0a 20 20 20 20 69 66 28 20 28 21 70 50 61  */.    if( (!pPa
1e0f3 67 65 20 26 26 20 28 28 72 63 20 3d 20 62 74 72  ge && ((rc = btr
1e0f4 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69  eeGetPage(pBt, i
1e0f5 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29  Page, &pPage, 0)
1e0f6 29 21 3d 30 29 20 29 0a 20 20 20 20 20 7c 7c 20  )!=0) ).     || 
1e0f7 20 20 20 20 20 20 20 20 20 20 20 28 28 72 63 20             ((rc 
1e0f8 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
1e0f9 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
1e0fa 67 65 29 29 21 3d 30 29 0a 20 20 20 20 29 7b 0a  ge))!=0).    ){.
1e0fb 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70        goto freep
1e0fc 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  age_out;.    }. 
1e0fd 20 20 20 6d 65 6d 73 65 74 28 70 50 61 67 65 2d     memset(pPage-
1e0fe 3e 61 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65  >aData, 0, pPage
1e0ff 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29  ->pBt->pageSize)
1e100 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
1e101 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70  he database supp
1e102 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d  orts auto-vacuum
1e103 2c 20 77 72 69 74 65 20 61 6e 20 65 6e 74 72 79  , write an entry
1e104 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d   in the pointer-
1e105 6d 61 70 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69  map.  ** to indi
1e106 63 61 74 65 20 74 68 61 74 20 74 68 65 20 70 61  cate that the pa
1e107 67 65 20 69 73 20 66 72 65 65 2e 0a 20 20 2a 2f  ge is free..  */
1e108 0a 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43  .  if( ISAUTOVAC
1e109 55 55 4d 20 29 7b 0a 20 20 20 20 70 74 72 6d 61  UUM ){.    ptrma
1e10a 70 50 75 74 28 70 42 74 2c 20 69 50 61 67 65 2c  pPut(pBt, iPage,
1e10b 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45   PTRMAP_FREEPAGE
1e10c 2c 20 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 69  , 0, &rc);.    i
1e10d 66 28 20 72 63 20 29 20 67 6f 74 6f 20 66 72 65  f( rc ) goto fre
1e10e 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  epage_out;.  }..
1e10f 20 20 2f 2a 20 4e 6f 77 20 6d 61 6e 69 70 75 6c    /* Now manipul
1e110 61 74 65 20 74 68 65 20 61 63 74 75 61 6c 20 64  ate the actual d
1e111 61 74 61 62 61 73 65 20 66 72 65 65 2d 6c 69 73  atabase free-lis
1e112 74 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 65  t structure. The
1e113 72 65 20 61 72 65 20 74 77 6f 0a 20 20 2a 2a 20  re are two.  ** 
1e114 70 6f 73 73 69 62 69 6c 69 74 69 65 73 2e 20 49  possibilities. I
1e115 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  f the free-list 
1e116 69 73 20 63 75 72 72 65 6e 74 6c 79 20 65 6d 70  is currently emp
1e117 74 79 2c 20 6f 72 20 69 66 20 74 68 65 20 66 69  ty, or if the fi
1e118 72 73 74 0a 20 20 2a 2a 20 74 72 75 6e 6b 20 70  rst.  ** trunk p
1e119 61 67 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d  age in the free-
1e11a 6c 69 73 74 20 69 73 20 66 75 6c 6c 2c 20 74 68  list is full, th
1e11b 65 6e 20 74 68 69 73 20 70 61 67 65 20 77 69 6c  en this page wil
1e11c 6c 20 62 65 63 6f 6d 65 20 61 0a 20 20 2a 2a 20  l become a.  ** 
1e11d 6e 65 77 20 66 72 65 65 2d 6c 69 73 74 20 74 72  new free-list tr
1e11e 75 6e 6b 20 70 61 67 65 2e 20 4f 74 68 65 72 77  unk page. Otherw
1e11f 69 73 65 2c 20 69 74 20 77 69 6c 6c 20 62 65 63  ise, it will bec
1e120 6f 6d 65 20 61 20 6c 65 61 66 20 6f 66 20 74 68  ome a leaf of th
1e121 65 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 72 75  e.  ** first tru
1e122 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 63  nk page in the c
1e123 75 72 72 65 6e 74 20 66 72 65 65 2d 6c 69 73 74  urrent free-list
1e124 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 74 65 73  . This block tes
1e125 74 73 20 69 66 20 69 74 0a 20 20 2a 2a 20 69 73  ts if it.  ** is
1e126 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 64 64   possible to add
1e127 20 74 68 65 20 70 61 67 65 20 61 73 20 61 20 6e   the page as a n
1e128 65 77 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61  ew free-list lea
1e129 66 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 46  f..  */.  if( nF
1e12a 72 65 65 21 3d 30 20 29 7b 0a 20 20 20 20 75 33  ree!=0 ){.    u3
1e12b 32 20 6e 4c 65 61 66 3b 20 20 20 20 20 20 20 20  2 nLeaf;        
1e12c 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69          /* Initi
1e12d 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 61  al number of lea
1e12e 66 20 63 65 6c 6c 73 20 6f 6e 20 74 72 75 6e 6b  f cells on trunk
1e12f 20 70 61 67 65 20 2a 2f 0a 0a 20 20 20 20 69 54   page */..    iT
1e130 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28  runk = get4byte(
1e131 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
1e132 32 5d 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 74  2]);.    rc = bt
1e133 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
1e134 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c  iTrunk, &pTrunk,
1e135 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21   0);.    if( rc!
1e136 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1e137 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67      goto freepag
1e138 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20  e_out;.    }..  
1e139 20 20 6e 4c 65 61 66 20 3d 20 67 65 74 34 62 79    nLeaf = get4by
1e13a 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  te(&pTrunk->aDat
1e13b 61 5b 34 5d 29 3b 0a 20 20 20 20 61 73 73 65 72  a[4]);.    asser
1e13c 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  t( pBt->usableSi
1e13d 7a 65 3e 33 32 20 29 3b 0a 20 20 20 20 69 66 28  ze>32 );.    if(
1e13e 20 6e 4c 65 61 66 20 3e 20 28 75 33 32 29 70 42   nLeaf > (u32)pB
1e13f 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20  t->usableSize/4 
1e140 2d 20 32 20 29 7b 0a 20 20 20 20 20 20 72 63 20  - 2 ){.      rc 
1e141 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  = SQLITE_CORRUPT
1e142 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74  _BKPT;.      got
1e143 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a  o freepage_out;.
1e144 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c      }.    if( nL
1e145 65 61 66 20 3c 20 28 75 33 32 29 70 42 74 2d 3e  eaf < (u32)pBt->
1e146 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38  usableSize/4 - 8
1e147 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20   ){.      /* In 
1e148 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20  this case there 
1e149 69 73 20 72 6f 6f 6d 20 6f 6e 20 74 68 65 20 74  is room on the t
1e14a 72 75 6e 6b 20 70 61 67 65 20 74 6f 20 69 6e 73  runk page to ins
1e14b 65 72 74 20 74 68 65 20 70 61 67 65 0a 20 20 20  ert the page.   
1e14c 20 20 20 2a 2a 20 62 65 69 6e 67 20 66 72 65 65     ** being free
1e14d 64 20 61 73 20 61 20 6e 65 77 20 6c 65 61 66 2e  d as a new leaf.
1e14e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
1e14f 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
1e150 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 6e   trunk page is n
1e151 6f 74 20 72 65 61 6c 6c 79 20 66 75 6c 6c 20 75  ot really full u
1e152 6e 74 69 6c 20 69 74 20 63 6f 6e 74 61 69 6e 73  ntil it contains
1e153 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 62 6c 65  .      ** usable
1e154 53 69 7a 65 2f 34 20 2d 20 32 20 65 6e 74 72 69  Size/4 - 2 entri
1e155 65 73 2c 20 6e 6f 74 20 75 73 61 62 6c 65 53 69  es, not usableSi
1e156 7a 65 2f 34 20 2d 20 38 20 65 6e 74 72 69 65 73  ze/4 - 8 entries
1e157 20 61 73 20 77 65 20 68 61 76 65 0a 20 20 20 20   as we have.    
1e158 20 20 2a 2a 20 63 6f 64 65 64 2e 20 20 42 75 74    ** coded.  But
1e159 20 64 75 65 20 74 6f 20 61 20 63 6f 64 69 6e 67   due to a coding
1e15a 20 65 72 72 6f 72 20 69 6e 20 76 65 72 73 69 6f   error in versio
1e15b 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 70 72 69  ns of SQLite pri
1e15c 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 33  or to.      ** 3
1e15d 2e 36 2e 30 2c 20 64 61 74 61 62 61 73 65 73 20  .6.0, databases 
1e15e 77 69 74 68 20 66 72 65 65 6c 69 73 74 20 74 72  with freelist tr
1e15f 75 6e 6b 20 70 61 67 65 73 20 68 6f 6c 64 69 6e  unk pages holdin
1e160 67 20 6d 6f 72 65 20 74 68 61 6e 0a 20 20 20 20  g more than.    
1e161 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f    ** usableSize/
1e162 34 20 2d 20 38 20 65 6e 74 72 69 65 73 20 77 69  4 - 8 entries wi
1e163 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 20 61  ll be reported a
1e164 73 20 63 6f 72 72 75 70 74 2e 20 20 49 6e 20 6f  s corrupt.  In o
1e165 72 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 74 6f  rder.      ** to
1e166 20 6d 61 69 6e 74 61 69 6e 20 62 61 63 6b 77 61   maintain backwa
1e167 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  rds compatibilit
1e168 79 20 77 69 74 68 20 6f 6c 64 65 72 20 76 65 72  y with older ver
1e169 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c  sions of SQLite,
1e16a 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 77 69 6c  .      ** we wil
1e16b 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 72 65  l continue to re
1e16c 73 74 72 69 63 74 20 74 68 65 20 6e 75 6d 62 65  strict the numbe
1e16d 72 20 6f 66 20 65 6e 74 72 69 65 73 20 74 6f 20  r of entries to 
1e16e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38  usableSize/4 - 8
1e16f 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 6e 6f  .      ** for no
1e170 77 2e 20 20 41 74 20 73 6f 6d 65 20 70 6f 69 6e  w.  At some poin
1e171 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 20  t in the future 
1e172 28 6f 6e 63 65 20 65 76 65 72 79 6f 6e 65 20 68  (once everyone h
1e173 61 73 20 75 70 67 72 61 64 65 64 0a 20 20 20 20  as upgraded.    
1e174 20 20 2a 2a 20 74 6f 20 33 2e 36 2e 30 20 6f 72    ** to 3.6.0 or
1e175 20 6c 61 74 65 72 29 20 77 65 20 73 68 6f 75 6c   later) we shoul
1e176 64 20 63 6f 6e 73 69 64 65 72 20 66 69 78 69 6e  d consider fixin
1e177 67 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 61  g the conditiona
1e178 6c 20 61 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a  l above.      **
1e179 20 74 6f 20 72 65 61 64 20 22 75 73 61 62 6c 65   to read "usable
1e17a 53 69 7a 65 2f 34 2d 32 22 20 69 6e 73 74 65 61  Size/4-2" instea
1e17b 64 20 6f 66 20 22 75 73 61 62 6c 65 53 69 7a 65  d of "usableSize
1e17c 2f 34 2d 38 22 2e 0a 20 20 20 20 20 20 2a 2f 0a  /4-8"..      */.
1e17d 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1e17e 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72  e3PagerWrite(pTr
1e17f 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  unk->pDbPage);. 
1e180 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
1e181 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1e182 20 20 70 75 74 34 62 79 74 65 28 26 70 54 72 75    put4byte(&pTru
1e183 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6e 4c  nk->aData[4], nL
1e184 65 61 66 2b 31 29 3b 0a 20 20 20 20 20 20 20 20  eaf+1);.        
1e185 70 75 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b  put4byte(&pTrunk
1e186 2d 3e 61 44 61 74 61 5b 38 2b 6e 4c 65 61 66 2a  ->aData[8+nLeaf*
1e187 34 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20  4], iPage);.    
1e188 20 20 20 20 69 66 28 20 70 50 61 67 65 20 26 26      if( pPage &&
1e189 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20   (pBt->btsFlags 
1e18a 26 20 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c  & BTS_SECURE_DEL
1e18b 45 54 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ETE)==0 ){.     
1e18c 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
1e18d 72 44 6f 6e 74 57 72 69 74 65 28 70 50 61 67 65  rDontWrite(pPage
1e18e 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
1e18f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63      }.        rc
1e190 20 3d 20 62 74 72 65 65 53 65 74 48 61 73 43 6f   = btreeSetHasCo
1e191 6e 74 65 6e 74 28 70 42 74 2c 20 69 50 61 67 65  ntent(pBt, iPage
1e192 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1e193 20 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41   TRACE(("FREE-PA
1e194 47 45 3a 20 25 64 20 6c 65 61 66 20 6f 6e 20 74  GE: %d leaf on t
1e195 72 75 6e 6b 20 70 61 67 65 20 25 64 5c 6e 22 2c  runk page %d\n",
1e196 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 70 54 72 75  pPage->pgno,pTru
1e197 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20  nk->pgno));.    
1e198 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f    goto freepage_
1e199 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  out;.    }.  }..
1e19a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20    /* If control 
1e19b 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70 6f  flows to this po
1e19c 69 6e 74 2c 20 74 68 65 6e 20 69 74 20 77 61 73  int, then it was
1e19d 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
1e19e 20 61 64 64 20 74 68 65 0a 20 20 2a 2a 20 74 68   add the.  ** th
1e19f 65 20 70 61 67 65 20 62 65 69 6e 67 20 66 72 65  e page being fre
1e1a0 65 64 20 61 73 20 61 20 6c 65 61 66 20 70 61 67  ed as a leaf pag
1e1a1 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74  e of the first t
1e1a2 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65  runk in the free
1e1a3 2d 6c 69 73 74 2e 0a 20 20 2a 2a 20 50 6f 73 73  -list..  ** Poss
1e1a4 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 68 65  ibly because the
1e1a5 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 65 6d   free-list is em
1e1a6 70 74 79 2c 20 6f 72 20 70 6f 73 73 69 62 6c 79  pty, or possibly
1e1a7 20 62 65 63 61 75 73 65 20 74 68 65 20 0a 20 20   because the .  
1e1a8 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 69  ** first trunk i
1e1a9 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  n the free-list 
1e1aa 69 73 20 66 75 6c 6c 2e 20 45 69 74 68 65 72 20  is full. Either 
1e1ab 77 61 79 2c 20 74 68 65 20 70 61 67 65 20 62 65  way, the page be
1e1ac 69 6e 67 20 66 72 65 65 64 0a 20 20 2a 2a 20 77  ing freed.  ** w
1e1ad 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e  ill become the n
1e1ae 65 77 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70  ew first trunk p
1e1af 61 67 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d  age in the free-
1e1b0 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  list..  */.  if(
1e1b1 20 70 50 61 67 65 3d 3d 30 20 26 26 20 53 51 4c   pPage==0 && SQL
1e1b2 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 62 74  ITE_OK!=(rc = bt
1e1b3 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
1e1b4 69 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30  iPage, &pPage, 0
1e1b5 29 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66  )) ){.    goto f
1e1b6 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d  reepage_out;.  }
1e1b7 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
1e1b8 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d  agerWrite(pPage-
1e1b9 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28  >pDbPage);.  if(
1e1ba 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1e1bb 7b 0a 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70  {.    goto freep
1e1bc 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 70  age_out;.  }.  p
1e1bd 75 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61  ut4byte(pPage->a
1e1be 44 61 74 61 2c 20 69 54 72 75 6e 6b 29 3b 0a 20  Data, iTrunk);. 
1e1bf 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65   put4byte(&pPage
1e1c0 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 30 29 3b 0a  ->aData[4], 0);.
1e1c1 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67    put4byte(&pPag
1e1c2 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69  e1->aData[32], i
1e1c3 50 61 67 65 29 3b 0a 20 20 54 52 41 43 45 28 28  Page);.  TRACE((
1e1c4 22 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6e  "FREE-PAGE: %d n
1e1c5 65 77 20 74 72 75 6e 6b 20 70 61 67 65 20 72 65  ew trunk page re
1e1c6 70 6c 61 63 69 6e 67 20 25 64 5c 6e 22 2c 20 70  placing %d\n", p
1e1c7 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 69 54 72 75  Page->pgno, iTru
1e1c8 6e 6b 29 29 3b 0a 0a 66 72 65 65 70 61 67 65 5f  nk));..freepage_
1e1c9 6f 75 74 3a 0a 20 20 69 66 28 20 70 50 61 67 65  out:.  if( pPage
1e1ca 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69   ){.    pPage->i
1e1cb 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20  sInit = 0;.  }. 
1e1cc 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
1e1cd 67 65 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61  ge);.  releasePa
1e1ce 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 72 65  ge(pTrunk);.  re
1e1cf 74 75 72 6e 20 72 63 3b 0a 7d 0a 73 74 61 74 69  turn rc;.}.stati
1e1d0 63 20 76 6f 69 64 20 66 72 65 65 50 61 67 65 28  c void freePage(
1e1d1 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
1e1d2 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 69 66 28  int *pRC){.  if(
1e1d3 20 28 2a 70 52 43 29 3d 3d 53 51 4c 49 54 45 5f   (*pRC)==SQLITE_
1e1d4 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d  OK ){.    *pRC =
1e1d5 20 66 72 65 65 50 61 67 65 32 28 70 50 61 67 65   freePage2(pPage
1e1d6 2d 3e 70 42 74 2c 20 70 50 61 67 65 2c 20 70 50  ->pBt, pPage, pP
1e1d7 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a  age->pgno);.  }.
1e1d8 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e  }../*.** Free an
1e1d9 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  y overflow pages
1e1da 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1e1db 20 74 68 65 20 67 69 76 65 6e 20 43 65 6c 6c 2e   the given Cell.
1e1dc 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
1e1dd 6c 65 61 72 43 65 6c 6c 28 4d 65 6d 50 61 67 65  learCell(MemPage
1e1de 20 2a 70 50 61 67 65 2c 20 75 6e 73 69 67 6e 65   *pPage, unsigne
1e1df 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 29 7b 0a  d char *pCell){.
1e1e0 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
1e1e1 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20  = pPage->pBt;.  
1e1e2 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20  CellInfo info;. 
1e1e3 20 50 67 6e 6f 20 6f 76 66 6c 50 67 6e 6f 3b 0a   Pgno ovflPgno;.
1e1e4 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20    int rc;.  int 
1e1e5 6e 4f 76 66 6c 3b 0a 20 20 75 33 32 20 6f 76 66  nOvfl;.  u32 ovf
1e1e6 6c 50 61 67 65 53 69 7a 65 3b 0a 0a 20 20 61 73  lPageSize;..  as
1e1e7 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1e1e8 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
1e1e9 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
1e1ea 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50   btreeParseCellP
1e1eb 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
1e1ec 20 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 69   &info);.  if( i
1e1ed 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 3d 3d 30  nfo.iOverflow==0
1e1ee 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
1e1ef 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f  QLITE_OK;  /* No
1e1f0 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
1e1f1 20 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20   Return without 
1e1f2 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 2a  doing anything *
1e1f3 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 65 6c  /.  }.  if( pCel
1e1f4 6c 2b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77  l+info.iOverflow
1e1f5 2b 33 20 3e 20 70 50 61 67 65 2d 3e 61 44 61 74  +3 > pPage->aDat
1e1f6 61 2b 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67  a+pPage->maskPag
1e1f7 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
1e1f8 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
1e1f9 4b 50 54 3b 20 20 2f 2a 20 43 65 6c 6c 20 65 78  KPT;  /* Cell ex
1e1fa 74 65 6e 64 73 20 70 61 73 74 20 65 6e 64 20 6f  tends past end o
1e1fb 66 20 70 61 67 65 20 2a 2f 0a 20 20 7d 0a 20 20  f page */.  }.  
1e1fc 6f 76 66 6c 50 67 6e 6f 20 3d 20 67 65 74 34 62  ovflPgno = get4b
1e1fd 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e  yte(&pCell[info.
1e1fe 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 61  iOverflow]);.  a
1e1ff 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62  ssert( pBt->usab
1e200 6c 65 53 69 7a 65 20 3e 20 34 20 29 3b 0a 20 20  leSize > 4 );.  
1e201 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 3d 20 70  ovflPageSize = p
1e202 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d  Bt->usableSize -
1e203 20 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 69   4;.  nOvfl = (i
1e204 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 2d 20 69  nfo.nPayload - i
1e205 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66  nfo.nLocal + ovf
1e206 6c 50 61 67 65 53 69 7a 65 20 2d 20 31 29 2f 6f  lPageSize - 1)/o
1e207 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a 20 20 61  vflPageSize;.  a
1e208 73 73 65 72 74 28 20 6f 76 66 6c 50 67 6e 6f 3d  ssert( ovflPgno=
1e209 3d 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b  =0 || nOvfl>0 );
1e20a 0a 20 20 77 68 69 6c 65 28 20 6e 4f 76 66 6c 2d  .  while( nOvfl-
1e20b 2d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4e  - ){.    Pgno iN
1e20c 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 4d 65 6d  ext = 0;.    Mem
1e20d 50 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b  Page *pOvfl = 0;
1e20e 0a 20 20 20 20 69 66 28 20 6f 76 66 6c 50 67 6e  .    if( ovflPgn
1e20f 6f 3c 32 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e  o<2 || ovflPgno>
1e210 62 74 72 65 65 50 61 67 65 63 6f 75 6e 74 28 70  btreePagecount(p
1e211 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  Bt) ){.      /* 
1e212 30 20 69 73 20 6e 6f 74 20 61 20 6c 65 67 61 6c  0 is not a legal
1e213 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64   page number and
1e214 20 70 61 67 65 20 31 20 63 61 6e 6e 6f 74 20 62   page 1 cannot b
1e215 65 20 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f  e an .      ** o
1e216 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 54 68  verflow page. Th
1e217 65 72 65 66 6f 72 65 20 69 66 20 6f 76 66 6c 50  erefore if ovflP
1e218 67 6e 6f 3c 32 20 6f 72 20 70 61 73 74 20 74 68  gno<2 or past th
1e219 65 20 65 6e 64 20 6f 66 20 74 68 65 20 0a 20 20  e end of the .  
1e21a 20 20 20 20 2a 2a 20 66 69 6c 65 20 74 68 65 20      ** file the 
1e21b 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65  database must be
1e21c 20 63 6f 72 72 75 70 74 2e 20 2a 2f 0a 20 20 20   corrupt. */.   
1e21d 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1e21e 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
1e21f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4f 76     }.    if( nOv
1e220 66 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  fl ){.      rc =
1e221 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65   getOverflowPage
1e222 28 70 42 74 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20  (pBt, ovflPgno, 
1e223 26 70 4f 76 66 6c 2c 20 26 69 4e 65 78 74 29 3b  &pOvfl, &iNext);
1e224 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20  .      if( rc ) 
1e225 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
1e226 0a 0a 20 20 20 20 69 66 28 20 28 20 70 4f 76 66  ..    if( ( pOvf
1e227 6c 20 7c 7c 20 28 28 70 4f 76 66 6c 20 3d 20 62  l || ((pOvfl = b
1e228 74 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70  treePageLookup(p
1e229 42 74 2c 20 6f 76 66 6c 50 67 6e 6f 29 29 21 3d  Bt, ovflPgno))!=
1e22a 30 29 20 29 0a 20 20 20 20 20 26 26 20 73 71 6c  0) ).     && sql
1e22b 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66  ite3PagerPageRef
1e22c 63 6f 75 6e 74 28 70 4f 76 66 6c 2d 3e 70 44 62  count(pOvfl->pDb
1e22d 50 61 67 65 29 21 3d 31 0a 20 20 20 20 29 7b 0a  Page)!=1.    ){.
1e22e 20 20 20 20 20 20 2f 2a 20 54 68 65 72 65 20 69        /* There i
1e22f 73 20 6e 6f 20 72 65 61 73 6f 6e 20 61 6e 79 20  s no reason any 
1e230 63 75 72 73 6f 72 20 73 68 6f 75 6c 64 20 68 61  cursor should ha
1e231 76 65 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e  ve an outstandin
1e232 67 20 72 65 66 65 72 65 6e 63 65 20 0a 20 20 20  g reference .   
1e233 20 20 20 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72     ** to an over
1e234 66 6c 6f 77 20 70 61 67 65 20 62 65 6c 6f 6e 67  flow page belong
1e235 69 6e 67 20 74 6f 20 61 20 63 65 6c 6c 20 74 68  ing to a cell th
1e236 61 74 20 69 73 20 62 65 69 6e 67 20 64 65 6c 65  at is being dele
1e237 74 65 64 2f 75 70 64 61 74 65 64 2e 0a 20 20 20  ted/updated..   
1e238 20 20 20 2a 2a 20 53 6f 20 69 66 20 74 68 65 72     ** So if ther
1e239 65 20 65 78 69 73 74 73 20 6d 6f 72 65 20 74 68  e exists more th
1e23a 61 6e 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65  an one reference
1e23b 20 74 6f 20 74 68 69 73 20 70 61 67 65 2c 20 74   to this page, t
1e23c 68 65 6e 20 69 74 20 0a 20 20 20 20 20 20 2a 2a  hen it .      **
1e23d 20 6d 75 73 74 20 6e 6f 74 20 72 65 61 6c 6c 79   must not really
1e23e 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20   be an overflow 
1e23f 70 61 67 65 20 61 6e 64 20 74 68 65 20 64 61 74  page and the dat
1e240 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 63 6f  abase must be co
1e241 72 72 75 70 74 2e 20 0a 20 20 20 20 20 20 2a 2a  rrupt. .      **
1e242 20 49 74 20 69 73 20 68 65 6c 70 66 75 6c 20 74   It is helpful t
1e243 6f 20 64 65 74 65 63 74 20 74 68 69 73 20 62 65  o detect this be
1e244 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 66 72 65  fore calling fre
1e245 65 50 61 67 65 32 28 29 2c 20 61 73 20 0a 20 20  ePage2(), as .  
1e246 20 20 20 20 2a 2a 20 66 72 65 65 50 61 67 65 32      ** freePage2
1e247 28 29 20 6d 61 79 20 7a 65 72 6f 20 74 68 65 20  () may zero the 
1e248 70 61 67 65 20 63 6f 6e 74 65 6e 74 73 20 69 66  page contents if
1e249 20 73 65 63 75 72 65 2d 64 65 6c 65 74 65 20 6d   secure-delete m
1e24a 6f 64 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20  ode is.      ** 
1e24b 65 6e 61 62 6c 65 64 2e 20 49 66 20 74 68 69 73  enabled. If this
1e24c 20 27 6f 76 65 72 66 6c 6f 77 27 20 70 61 67 65   'overflow' page
1e24d 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 61   happens to be a
1e24e 20 70 61 67 65 20 74 68 61 74 20 74 68 65 0a 20   page that the. 
1e24f 20 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 20 69       ** caller i
1e250 73 20 69 74 65 72 61 74 69 6e 67 20 74 68 72 6f  s iterating thro
1e251 75 67 68 20 6f 72 20 75 73 69 6e 67 20 69 6e 20  ugh or using in 
1e252 73 6f 6d 65 20 6f 74 68 65 72 20 77 61 79 2c 20  some other way, 
1e253 74 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 63 61  this.      ** ca
1e254 6e 20 62 65 20 70 72 6f 62 6c 65 6d 61 74 69 63  n be problematic
1e255 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1e256 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
1e257 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
1e258 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
1e259 20 66 72 65 65 50 61 67 65 32 28 70 42 74 2c 20   freePage2(pBt, 
1e25a 70 4f 76 66 6c 2c 20 6f 76 66 6c 50 67 6e 6f 29  pOvfl, ovflPgno)
1e25b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
1e25c 20 70 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20   pOvfl ){.      
1e25d 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
1e25e 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65  f(pOvfl->pDbPage
1e25f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
1e260 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
1e261 0a 20 20 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20  .    ovflPgno = 
1e262 69 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74  iNext;.  }.  ret
1e263 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1e264 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74  ../*.** Create t
1e265 68 65 20 62 79 74 65 20 73 65 71 75 65 6e 63 65  he byte sequence
1e266 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65   used to represe
1e267 6e 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61 67  nt a cell on pag
1e268 65 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20 77  e pPage.** and w
1e269 72 69 74 65 20 74 68 61 74 20 62 79 74 65 20 73  rite that byte s
1e26a 65 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 43 65  equence into pCe
1e26b 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 20  ll[].  Overflow 
1e26c 70 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c 6c  pages are.** all
1e26d 6f 63 61 74 65 64 20 61 6e 64 20 66 69 6c 6c 65  ocated and fille
1e26e 64 20 69 6e 20 61 73 20 6e 65 63 65 73 73 61 72  d in as necessar
1e26f 79 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  y.  The calling 
1e270 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 20  procedure.** is 
1e271 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
1e272 6d 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66 66  making sure suff
1e273 69 63 69 65 6e 74 20 73 70 61 63 65 20 68 61 73  icient space has
1e274 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a   been allocated.
1e275 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a  ** for pCell[]..
1e276 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
1e277 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 6e  pCell does not n
1e278 65 63 65 73 73 61 72 79 20 6e 65 65 64 20 74 6f  ecessary need to
1e279 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 70 50   point to the pP
1e27a 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61 72  age->aData.** ar
1e27b 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68 74  ea.  pCell might
1e27c 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 74   point to some t
1e27d 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65  emporary storage
1e27e 2e 20 20 54 68 65 20 63 65 6c 6c 20 77 69 6c 6c  .  The cell will
1e27f 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 63 74  .** be construct
1e280 65 64 20 69 6e 20 74 68 69 73 20 74 65 6d 70 6f  ed in this tempo
1e281 72 61 72 79 20 61 72 65 61 20 74 68 65 6e 20 63  rary area then c
1e282 6f 70 69 65 64 20 69 6e 74 6f 20 70 50 61 67 65  opied into pPage
1e283 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65 72  ->aData.** later
1e284 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1e285 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d 65  fillInCell(.  Me
1e286 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20  mPage *pPage,   
1e287 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e288 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f  The page that co
1e289 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c 20  ntains the cell 
1e28a 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
1e28b 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20  ar *pCell,      
1e28c 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20      /* Complete 
1e28d 74 65 78 74 20 6f 66 20 74 68 65 20 63 65 6c 6c  text of the cell
1e28e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
1e28f 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79   *pKey, i64 nKey
1e290 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20  ,    /* The key 
1e291 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
1e292 2a 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 74 61  *pData,int nData
1e293 2c 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20  ,   /* The data 
1e294 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20  */.  int nZero, 
1e295 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e296 20 20 20 20 2f 2a 20 45 78 74 72 61 20 7a 65 72      /* Extra zer
1e297 6f 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e  o bytes to appen
1e298 64 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20 20  d to pData */.  
1e299 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 20 20  int *pnSize     
1e29a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1e29b 2a 20 57 72 69 74 65 20 63 65 6c 6c 20 73 69 7a  * Write cell siz
1e29c 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  e here */.){.  i
1e29d 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 63  nt nPayload;.  c
1e29e 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0a 20  onst u8 *pSrc;. 
1e29f 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72 63   int nSrc, n, rc
1e2a0 3b 0a 20 20 69 6e 74 20 73 70 61 63 65 4c 65 66  ;.  int spaceLef
1e2a1 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f  t;.  MemPage *pO
1e2a2 76 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61  vfl = 0;.  MemPa
1e2a3 67 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 20 3d  ge *pToRelease =
1e2a4 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   0;.  unsigned c
1e2a5 68 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20 75  har *pPrior;.  u
1e2a6 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50  nsigned char *pP
1e2a7 61 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61 72  ayload;.  BtShar
1e2a8 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d  ed *pBt = pPage-
1e2a9 3e 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e  >pBt;.  Pgno pgn
1e2aa 6f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e 74  oOvfl = 0;.  int
1e2ab 20 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 6c 6c   nHeader;.  Cell
1e2ac 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61 73  Info info;..  as
1e2ad 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1e2ae 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
1e2af 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  pBt->mutex) );..
1e2b0 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 6e 6f    /* pPage is no
1e2b1 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 77 72  t necessarily wr
1e2b2 69 74 65 61 62 6c 65 20 73 69 6e 63 65 20 70 43  iteable since pC
1e2b3 65 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 75 78  ell might be aux
1e2b4 69 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 66 66  iliary.  ** buff
1e2b5 65 72 20 73 70 61 63 65 20 74 68 61 74 20 69 73  er space that is
1e2b6 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74   separate from t
1e2b7 68 65 20 70 50 61 67 65 20 62 75 66 66 65 72 20  he pPage buffer 
1e2b8 61 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 72 74  area */.  assert
1e2b9 28 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e 61  ( pCell<pPage->a
1e2ba 44 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26  Data || pCell>=&
1e2bb 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74  pPage->aData[pBt
1e2bc 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20  ->pageSize].    
1e2bd 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74          || sqlit
1e2be 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
1e2bf 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
1e2c0 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c  e) );..  /* Fill
1e2c1 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20   in the header. 
1e2c2 2a 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 30  */.  nHeader = 0
1e2c3 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e  ;.  if( !pPage->
1e2c4 6c 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 61  leaf ){.    nHea
1e2c5 64 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20  der += 4;.  }.  
1e2c6 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61  if( pPage->hasDa
1e2c7 74 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65  ta ){.    nHeade
1e2c8 72 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26  r += putVarint(&
1e2c9 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20  pCell[nHeader], 
1e2ca 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20  nData+nZero);.  
1e2cb 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61  }else{.    nData
1e2cc 20 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20   = nZero = 0;.  
1e2cd 7d 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70  }.  nHeader += p
1e2ce 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b  utVarint(&pCell[
1e2cf 6e 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a  nHeader], *(u64*
1e2d0 29 26 6e 4b 65 79 29 3b 0a 20 20 62 74 72 65 65  )&nKey);.  btree
1e2d1 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61  ParseCellPtr(pPa
1e2d2 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f  ge, pCell, &info
1e2d3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66  );.  assert( inf
1e2d4 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61 64  o.nHeader==nHead
1e2d5 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  er );.  assert( 
1e2d6 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 20  info.nKey==nKey 
1e2d7 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66  );.  assert( inf
1e2d8 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 28 6e  o.nData==(u32)(n
1e2d9 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a 20  Data+nZero) );. 
1e2da 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74   .  /* Fill in t
1e2db 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20  he payload */.  
1e2dc 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61  nPayload = nData
1e2dd 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20   + nZero;.  if( 
1e2de 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
1e2df 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 74  .    pSrc = pDat
1e2e0 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44  a;.    nSrc = nD
1e2e1 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 3d  ata;.    nData =
1e2e2 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 20   0;.  }else{ .  
1e2e3 20 20 69 66 28 20 4e 45 56 45 52 28 6e 4b 65 79    if( NEVER(nKey
1e2e4 3e 30 78 37 66 66 66 66 66 66 66 20 7c 7c 20 70  >0x7fffffff || p
1e2e5 4b 65 79 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20  Key==0) ){.     
1e2e6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1e2e7 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
1e2e8 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20   }.    nPayload 
1e2e9 2b 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20  += (int)nKey;.  
1e2ea 20 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20    pSrc = pKey;. 
1e2eb 20 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e     nSrc = (int)n
1e2ec 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69  Key;.  }.  *pnSi
1e2ed 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b  ze = info.nSize;
1e2ee 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69  .  spaceLeft = i
1e2ef 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50  nfo.nLocal;.  pP
1e2f0 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b  ayload = &pCell[
1e2f1 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69  nHeader];.  pPri
1e2f2 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f  or = &pCell[info
1e2f3 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20  .iOverflow];..  
1e2f4 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e  while( nPayload>
1e2f5 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61  0 ){.    if( spa
1e2f6 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 66  ceLeft==0 ){.#if
1e2f7 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1e2f8 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
1e2f9 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d 61    Pgno pgnoPtrma
1e2fa 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f 2a  p = pgnoOvfl; /*
1e2fb 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70   Overflow page p
1e2fc 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79  ointer-map entry
1e2fd 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 69   page */.      i
1e2fe 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
1e2ff 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f  um ){.        do
1e300 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e 6f  {.          pgno
1e301 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20  Ovfl++;.        
1e302 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20 20  } while( .      
1e303 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47      PTRMAP_ISPAG
1e304 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29  E(pBt, pgnoOvfl)
1e305 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45   || pgnoOvfl==PE
1e306 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
1e307 70 42 74 29 20 0a 20 20 20 20 20 20 20 20 29 3b  pBt) .        );
1e308 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
1e309 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63        rc = alloc
1e30a 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74  ateBtreePage(pBt
1e30b 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f 4f  , &pOvfl, &pgnoO
1e30c 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 30  vfl, pgnoOvfl, 0
1e30d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
1e30e 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
1e30f 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  M.      /* If th
1e310 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f  e database suppo
1e311 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c  rts auto-vacuum,
1e312 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20   and the second 
1e313 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20  or subsequent.  
1e314 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20      ** overflow 
1e315 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c  page is being al
1e316 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20  located, add an 
1e317 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69  entry to the poi
1e318 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a  nter-map.      *
1e319 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 20  * for that page 
1e31a 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20  now. .      **. 
1e31b 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20       ** If this 
1e31c 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 76 65  is the first ove
1e31d 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e  rflow page, then
1e31e 20 77 72 69 74 65 20 61 20 70 61 72 74 69 61 6c   write a partial
1e31f 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a   entry .      **
1e320 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d   to the pointer-
1e321 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69 74 65  map. If we write
1e322 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73   nothing to this
1e323 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f   pointer-map slo
1e324 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e  t,.      ** then
1e325 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20   the optimistic 
1e326 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70  overflow chain p
1e327 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65  rocessing in cle
1e328 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a  arCell().      *
1e329 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72  * may misinterpr
1e32a 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c  et the uninitial
1e32b 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20  ised values and 
1e32c 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20  delete the.     
1e32d 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20   ** wrong pages 
1e32e 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1e32f 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
1e330 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
1e331 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c  acuum && rc==SQL
1e332 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1e333 20 20 75 38 20 65 54 79 70 65 20 3d 20 28 70 67    u8 eType = (pg
1e334 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f  noPtrmap?PTRMAP_
1e335 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50  OVERFLOW2:PTRMAP
1e336 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20  _OVERFLOW1);.   
1e337 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70       ptrmapPut(p
1e338 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 54  Bt, pgnoOvfl, eT
1e339 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70 2c  ype, pgnoPtrmap,
1e33a 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 69   &rc);.        i
1e33b 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
1e33c 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1e33d 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d  Ovfl);.        }
1e33e 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
1e33f 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
1e340 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
1e341 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b  age(pToRelease);
1e342 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
1e343 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  rc;.      }..   
1e344 20 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65     /* If pToRele
1e345 61 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20  ase is not zero 
1e346 74 68 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 6e  than pPrior poin
1e347 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  ts into the data
1e348 20 61 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 6f   area.      ** o
1e349 66 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d  f pToRelease.  M
1e34a 61 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65  ake sure pToRele
1e34b 61 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69  ase is still wri
1e34c 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20  teable. */.     
1e34d 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 6c 65   assert( pToRele
1e34e 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  ase==0 || sqlite
1e34f 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
1e350 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44  e(pToRelease->pD
1e351 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20  bPage) );..     
1e352 20 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73   /* If pPrior is
1e353 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74   part of the dat
1e354 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c  a area of pPage,
1e355 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20   then make sure 
1e356 70 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69  pPage.      ** i
1e357 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c  s still writeabl
1e358 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  e */.      asser
1e359 74 28 20 70 50 72 69 6f 72 3c 70 50 61 67 65 2d  t( pPrior<pPage-
1e35a 3e 61 44 61 74 61 20 7c 7c 20 70 50 72 69 6f 72  >aData || pPrior
1e35b 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  >=&pPage->aData[
1e35c 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20  pBt->pageSize]. 
1e35d 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71             || sq
1e35e 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
1e35f 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
1e360 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20  Page) );..      
1e361 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c  put4byte(pPrior,
1e362 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20   pgnoOvfl);.    
1e363 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
1e364 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20  oRelease);.     
1e365 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f   pToRelease = pO
1e366 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f  vfl;.      pPrio
1e367 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61  r = pOvfl->aData
1e368 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65  ;.      put4byte
1e369 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20  (pPrior, 0);.   
1e36a 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70     pPayload = &p
1e36b 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a  Ovfl->aData[4];.
1e36c 20 20 20 20 20 20 73 70 61 63 65 4c 65 66 74 20        spaceLeft 
1e36d 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
1e36e 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20  e - 4;.    }.   
1e36f 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20   n = nPayload;. 
1e370 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65     if( n>spaceLe
1e371 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65  ft ) n = spaceLe
1e372 66 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70  ft;..    /* If p
1e373 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74  ToRelease is not
1e374 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c   zero than pPayl
1e375 6f 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20  oad points into 
1e376 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 20  the data area.  
1e377 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61    ** of pToRelea
1e378 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70  se.  Make sure p
1e379 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69  ToRelease is sti
1e37a 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f  ll writeable. */
1e37b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f  .    assert( pTo
1e37c 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71  Release==0 || sq
1e37d 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
1e37e 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65  eable(pToRelease
1e37f 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20  ->pDbPage) );.. 
1e380 20 20 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f 61     /* If pPayloa
1e381 64 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65  d is part of the
1e382 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 50   data area of pP
1e383 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73  age, then make s
1e384 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 2a 2a  ure pPage.    **
1e385 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61   is still writea
1e386 62 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  ble */.    asser
1e387 74 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 67  t( pPayload<pPag
1e388 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 79  e->aData || pPay
1e389 6c 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 44  load>=&pPage->aD
1e38a 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a  ata[pBt->pageSiz
1e38b 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  e].            |
1e38c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  | sqlite3PagerIs
1e38d 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
1e38e 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20  >pDbPage) );..  
1e38f 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a    if( nSrc>0 ){.
1e390 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63        if( n>nSrc
1e391 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20   ) n = nSrc;.   
1e392 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 20     assert( pSrc 
1e393 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
1e394 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20  pPayload, pSrc, 
1e395 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  n);.    }else{. 
1e396 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79       memset(pPay
1e397 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20  load, 0, n);.   
1e398 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20   }.    nPayload 
1e399 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f  -= n;.    pPaylo
1e39a 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72  ad += n;.    pSr
1e39b 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63  c += n;.    nSrc
1e39c 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65   -= n;.    space
1e39d 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69  Left -= n;.    i
1e39e 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20  f( nSrc==0 ){.  
1e39f 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61      nSrc = nData
1e3a0 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70  ;.      pSrc = p
1e3a1 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Data;.    }.  }.
1e3a2 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
1e3a3 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74  oRelease);.  ret
1e3a4 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1e3a5 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74  ../*.** Remove t
1e3a6 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f  he i-th cell fro
1e3a7 6d 20 70 50 61 67 65 2e 20 20 54 68 69 73 20 72  m pPage.  This r
1e3a8 6f 75 74 69 6e 65 20 65 66 66 65 63 74 73 20 70  outine effects p
1e3a9 50 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68  Page only..** Th
1e3aa 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69  e cell content i
1e3ab 73 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64  s not freed or d
1e3ac 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20  eallocated.  It 
1e3ad 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a  is assumed that.
1e3ae 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  ** the cell cont
1e3af 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70  ent has been cop
1e3b0 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c  ied someplace el
1e3b1 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  se.  This routin
1e3b2 65 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65  e just.** remove
1e3b3 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  s the reference 
1e3b4 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d  to the cell from
1e3b5 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73   pPage..**.** "s
1e3b6 7a 22 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e  z" must be the n
1e3b7 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
1e3b8 6e 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73  n the cell..*/.s
1e3b9 74 61 74 69 63 20 76 6f 69 64 20 64 72 6f 70 43  tatic void dropC
1e3ba 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  ell(MemPage *pPa
1e3bb 67 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e 74  ge, int idx, int
1e3bc 20 73 7a 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a   sz, int *pRC){.
1e3bd 20 20 75 33 32 20 70 63 3b 20 20 20 20 20 20 20    u32 pc;       
1e3be 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 63    /* Offset to c
1e3bf 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 20 63  ell content of c
1e3c0 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 65  ell being delete
1e3c1 64 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b  d */.  u8 *data;
1e3c2 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 2d         /* pPage-
1e3c3 3e 61 44 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a  >aData */.  u8 *
1e3c4 70 74 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 55  ptr;        /* U
1e3c5 73 65 64 20 74 6f 20 6d 6f 76 65 20 62 79 74 65  sed to move byte
1e3c6 73 20 61 72 6f 75 6e 64 20 77 69 74 68 69 6e 20  s around within 
1e3c7 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a  data[] */.  u8 *
1e3c8 65 6e 64 50 74 72 3b 20 20 20 20 20 2f 2a 20 45  endPtr;     /* E
1e3c9 6e 64 20 6f 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20  nd of loop */.  
1e3ca 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
1e3cb 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f  /* The return co
1e3cc 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b  de */.  int hdr;
1e3cd 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e          /* Begin
1e3ce 6e 69 6e 67 20 6f 66 20 74 68 65 20 68 65 61 64  ning of the head
1e3cf 65 72 2e 20 20 30 20 6d 6f 73 74 20 70 61 67 65  er.  0 most page
1e3d0 73 2e 20 20 31 30 30 20 70 61 67 65 20 31 20 2a  s.  100 page 1 *
1e3d1 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20  /..  if( *pRC ) 
1e3d2 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72  return;..  asser
1e3d3 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64 78  t( idx>=0 && idx
1e3d4 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b  <pPage->nCell );
1e3d5 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 63  .  assert( sz==c
1e3d6 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c 20 69  ellSize(pPage, i
1e3d7 64 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  dx) );.  assert(
1e3d8 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
1e3d9 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
1e3da 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73  pDbPage) );.  as
1e3db 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1e3dc 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
1e3dd 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
1e3de 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
1e3df 44 61 74 61 3b 0a 20 20 70 74 72 20 3d 20 26 70  Data;.  ptr = &p
1e3e0 50 61 67 65 2d 3e 61 43 65 6c 6c 49 64 78 5b 32  Page->aCellIdx[2
1e3e1 2a 69 64 78 5d 3b 0a 20 20 70 63 20 3d 20 67 65  *idx];.  pc = ge
1e3e2 74 32 62 79 74 65 28 70 74 72 29 3b 0a 20 20 68  t2byte(ptr);.  h
1e3e3 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f  dr = pPage->hdrO
1e3e4 66 66 73 65 74 3b 0a 20 20 74 65 73 74 63 61 73  ffset;.  testcas
1e3e5 65 28 20 70 63 3d 3d 67 65 74 32 62 79 74 65 28  e( pc==get2byte(
1e3e6 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 29 3b  &data[hdr+5]) );
1e3e7 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 63 2b  .  testcase( pc+
1e3e8 73 7a 3d 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e  sz==pPage->pBt->
1e3e9 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20  usableSize );.  
1e3ea 69 66 28 20 70 63 20 3c 20 28 75 33 32 29 67 65  if( pc < (u32)ge
1e3eb 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
1e3ec 2b 35 5d 29 20 7c 7c 20 70 63 2b 73 7a 20 3e 20  +5]) || pc+sz > 
1e3ed 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
1e3ee 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 2a 70  leSize ){.    *p
1e3ef 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52  RC = SQLITE_CORR
1e3f0 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 72 65  UPT_BKPT;.    re
1e3f1 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  turn;.  }.  rc =
1e3f2 20 66 72 65 65 53 70 61 63 65 28 70 50 61 67 65   freeSpace(pPage
1e3f3 2c 20 70 63 2c 20 73 7a 29 3b 0a 20 20 69 66 28  , pc, sz);.  if(
1e3f4 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 52 43 20   rc ){.    *pRC 
1e3f5 3d 20 72 63 3b 0a 20 20 20 20 72 65 74 75 72 6e  = rc;.    return
1e3f6 3b 0a 20 20 7d 0a 20 20 65 6e 64 50 74 72 20 3d  ;.  }.  endPtr =
1e3f7 20 26 70 50 61 67 65 2d 3e 61 43 65 6c 6c 49 64   &pPage->aCellId
1e3f8 78 5b 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  x[2*pPage->nCell
1e3f9 20 2d 20 32 5d 3b 0a 20 20 61 73 73 65 72 74 28   - 2];.  assert(
1e3fa 20 28 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f   (SQLITE_PTR_TO_
1e3fb 49 4e 54 28 70 74 72 29 26 31 29 3d 3d 30 20 29  INT(ptr)&1)==0 )
1e3fc 3b 20 20 2f 2a 20 70 74 72 20 69 73 20 61 6c 77  ;  /* ptr is alw
1e3fd 61 79 73 20 32 2d 62 79 74 65 20 61 6c 69 67 6e  ays 2-byte align
1e3fe 65 64 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 70  ed */.  while( p
1e3ff 74 72 3c 65 6e 64 50 74 72 20 29 7b 0a 20 20 20  tr<endPtr ){.   
1e400 20 2a 28 75 31 36 2a 29 70 74 72 20 3d 20 2a 28   *(u16*)ptr = *(
1e401 75 31 36 2a 29 26 70 74 72 5b 32 5d 3b 0a 20 20  u16*)&ptr[2];.  
1e402 20 20 70 74 72 20 2b 3d 20 32 3b 0a 20 20 7d 0a    ptr += 2;.  }.
1e403 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 2d    pPage->nCell--
1e404 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61  ;.  put2byte(&da
1e405 74 61 5b 68 64 72 2b 33 5d 2c 20 70 50 61 67 65  ta[hdr+3], pPage
1e406 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 50 61 67  ->nCell);.  pPag
1e407 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 32 3b 0a 7d  e->nFree += 2;.}
1e408 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61  ../*.** Insert a
1e409 20 6e 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50 61   new cell on pPa
1e40a 67 65 20 61 74 20 63 65 6c 6c 20 69 6e 64 65 78  ge at cell index
1e40b 20 22 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f 69   "i".  pCell poi
1e40c 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f  nts to the.** co
1e40d 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 6c  ntent of the cel
1e40e 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  l..**.** If the 
1e40f 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 77 69 6c  cell content wil
1e410 6c 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61 67  l fit on the pag
1e411 65 2c 20 74 68 65 6e 20 70 75 74 20 69 74 20 74  e, then put it t
1e412 68 65 72 65 2e 20 20 49 66 20 69 74 0a 2a 2a 20  here.  If it.** 
1e413 77 69 6c 6c 20 6e 6f 74 20 66 69 74 2c 20 74 68  will not fit, th
1e414 65 6e 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f  en make a copy o
1e415 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  f the cell conte
1e416 6e 74 20 69 6e 74 6f 20 70 54 65 6d 70 20 69 66  nt into pTemp if
1e417 0a 2a 2a 20 70 54 65 6d 70 20 69 73 20 6e 6f 74  .** pTemp is not
1e418 20 6e 75 6c 6c 2e 20 20 52 65 67 61 72 64 6c 65   null.  Regardle
1e419 73 73 20 6f 66 20 70 54 65 6d 70 2c 20 61 6c 6c  ss of pTemp, all
1e41a 6f 63 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72  ocate a new entr
1e41b 79 0a 2a 2a 20 69 6e 20 70 50 61 67 65 2d 3e 61  y.** in pPage->a
1e41c 70 4f 76 66 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65  pOvfl[] and make
1e41d 20 69 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   it point to the
1e41e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 28 65   cell content (e
1e41f 69 74 68 65 72 0a 2a 2a 20 69 6e 20 70 54 65 6d  ither.** in pTem
1e420 70 20 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61  p or the origina
1e421 6c 20 70 43 65 6c 6c 29 20 61 6e 64 20 61 6c 73  l pCell) and als
1e422 6f 20 72 65 63 6f 72 64 20 69 74 73 20 69 6e 64  o record its ind
1e423 65 78 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69  ex. .** Allocati
1e424 6e 67 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69  ng a new entry i
1e425 6e 20 70 50 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d  n pPage->aCell[]
1e426 20 69 6d 70 6c 69 65 73 20 74 68 61 74 20 0a 2a   implies that .*
1e427 2a 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  * pPage->nOverfl
1e428 6f 77 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  ow is incremente
1e429 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69  d..**.** If nSki
1e42a 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  p is non-zero, t
1e42b 68 65 6e 20 64 6f 20 6e 6f 74 20 63 6f 70 79 20  hen do not copy 
1e42c 74 68 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20  the first nSkip 
1e42d 62 79 74 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20  bytes of the.** 
1e42e 63 65 6c 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72  cell. The caller
1e42f 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20   will overwrite 
1e430 74 68 65 6d 20 61 66 74 65 72 20 74 68 69 73 20  them after this 
1e431 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
1e432 2e 20 49 66 0a 2a 2a 20 6e 53 6b 69 70 20 69 73  . If.** nSkip is
1e433 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
1e434 70 43 65 6c 6c 20 6d 61 79 20 6e 6f 74 20 70 6f  pCell may not po
1e435 69 6e 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69  int to an invali
1e436 64 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  d memory locatio
1e437 6e 20 0a 2a 2a 20 28 62 75 74 20 70 43 65 6c 6c  n .** (but pCell
1e438 2b 6e 53 6b 69 70 20 69 73 20 61 6c 77 61 79 73  +nSkip is always
1e439 20 76 61 6c 69 64 29 2e 0a 2a 2f 0a 73 74 61 74   valid)..*/.stat
1e43a 69 63 20 76 6f 69 64 20 69 6e 73 65 72 74 43 65  ic void insertCe
1e43b 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  ll(.  MemPage *p
1e43c 50 61 67 65 2c 20 20 20 2f 2a 20 50 61 67 65 20  Page,   /* Page 
1e43d 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72  into which we ar
1e43e 65 20 63 6f 70 79 69 6e 67 20 2a 2f 0a 20 20 69  e copying */.  i
1e43f 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20  nt i,           
1e440 20 2f 2a 20 4e 65 77 20 63 65 6c 6c 20 62 65 63   /* New cell bec
1e441 6f 6d 65 73 20 74 68 65 20 69 2d 74 68 20 63 65  omes the i-th ce
1e442 6c 6c 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a  ll of the page *
1e443 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20  /.  u8 *pCell,  
1e444 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74        /* Content
1e445 20 6f 66 20 74 68 65 20 6e 65 77 20 63 65 6c 6c   of the new cell
1e446 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 2c 20 20 20   */.  int sz,   
1e447 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
1e448 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e 20 70   of content in p
1e449 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 54  Cell */.  u8 *pT
1e44a 65 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  emp,        /* T
1e44b 65 6d 70 20 73 74 6f 72 61 67 65 20 73 70 61 63  emp storage spac
1e44c 65 20 66 6f 72 20 70 43 65 6c 6c 2c 20 69 66 20  e for pCell, if 
1e44d 6e 65 65 64 65 64 20 2a 2f 0a 20 20 50 67 6e 6f  needed */.  Pgno
1e44e 20 69 43 68 69 6c 64 2c 20 20 20 20 20 20 2f 2a   iChild,      /*
1e44f 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 72 65   If non-zero, re
1e450 70 6c 61 63 65 20 66 69 72 73 74 20 34 20 62 79  place first 4 by
1e451 74 65 73 20 77 69 74 68 20 74 68 69 73 20 76 61  tes with this va
1e452 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52  lue */.  int *pR
1e453 43 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  C          /* Re
1e454 61 64 20 61 6e 64 20 77 72 69 74 65 20 72 65 74  ad and write ret
1e455 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 68 65  urn code from he
1e456 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  re */.){.  int i
1e457 64 78 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  dx = 0;      /* 
1e458 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 6e  Where to write n
1e459 65 77 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  ew cell content 
1e45a 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69  in data[] */.  i
1e45b 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20  nt j;           
1e45c 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
1e45d 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 3b 20 20   */.  int end;  
1e45e 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
1e45f 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 6c   byte past the l
1e460 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  ast cell pointer
1e461 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20   in data[] */.  
1e462 69 6e 74 20 69 6e 73 3b 20 20 20 20 20 20 20 20  int ins;        
1e463 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 64 61    /* Index in da
1e464 74 61 5b 5d 20 77 68 65 72 65 20 6e 65 77 20 63  ta[] where new c
1e465 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73 20 69  ell pointer is i
1e466 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e 74  nserted */.  int
1e467 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 2f   cellOffset;   /
1e468 2a 20 41 64 64 72 65 73 73 20 6f 66 20 66 69 72  * Address of fir
1e469 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  st cell pointer 
1e46a 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 75  in data[] */.  u
1e46b 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20  8 *data;        
1e46c 20 2f 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20   /* The content 
1e46d 6f 66 20 74 68 65 20 77 68 6f 6c 65 20 70 61 67  of the whole pag
1e46e 65 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20  e */.  u8 *ptr; 
1e46f 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
1e470 20 66 6f 72 20 6d 6f 76 69 6e 67 20 69 6e 66 6f   for moving info
1e471 72 6d 61 74 69 6f 6e 20 61 72 6f 75 6e 64 20 69  rmation around i
1e472 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 75 38  n data[] */.  u8
1e473 20 2a 65 6e 64 50 74 72 3b 20 20 20 20 20 20 20   *endPtr;       
1e474 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6c 6f  /* End of the lo
1e475 6f 70 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 53 6b  op */..  int nSk
1e476 69 70 20 3d 20 28 69 43 68 69 6c 64 20 3f 20 34  ip = (iChild ? 4
1e477 20 3a 20 30 29 3b 0a 0a 20 20 69 66 28 20 2a 70   : 0);..  if( *p
1e478 52 43 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20  RC ) return;..  
1e479 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
1e47a 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b  i<=pPage->nCell+
1e47b 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
1e47c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
1e47d 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43  age->nCell<=MX_C
1e47e 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20  ELL(pPage->pBt) 
1e47f 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65  && MX_CELL(pPage
1e480 2d 3e 70 42 74 29 3c 3d 31 30 39 32 31 20 29 3b  ->pBt)<=10921 );
1e481 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
1e482 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3c 3d 41 72 72  ->nOverflow<=Arr
1e483 61 79 53 69 7a 65 28 70 50 61 67 65 2d 3e 61 70  aySize(pPage->ap
1e484 4f 76 66 6c 29 20 29 3b 0a 20 20 61 73 73 65 72  Ovfl) );.  asser
1e485 74 28 20 41 72 72 61 79 53 69 7a 65 28 70 50 61  t( ArraySize(pPa
1e486 67 65 2d 3e 61 70 4f 76 66 6c 29 3d 3d 41 72 72  ge->apOvfl)==Arr
1e487 61 79 53 69 7a 65 28 70 50 61 67 65 2d 3e 61 69  aySize(pPage->ai
1e488 4f 76 66 6c 29 20 29 3b 0a 20 20 61 73 73 65 72  Ovfl) );.  asser
1e489 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1e48a 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
1e48b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a  ->mutex) );.  /*
1e48c 20 54 68 65 20 63 65 6c 6c 20 73 68 6f 75 6c 64   The cell should
1e48d 20 6e 6f 72 6d 61 6c 6c 79 20 62 65 20 73 69 7a   normally be siz
1e48e 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 48  ed correctly.  H
1e48f 6f 77 65 76 65 72 2c 20 77 68 65 6e 20 6d 6f 76  owever, when mov
1e490 69 6e 67 20 61 0a 20 20 2a 2a 20 6d 61 6c 66 6f  ing a.  ** malfo
1e491 72 6d 65 64 20 63 65 6c 6c 20 66 72 6f 6d 20 61  rmed cell from a
1e492 20 6c 65 61 66 20 70 61 67 65 20 74 6f 20 61 6e   leaf page to an
1e493 20 69 6e 74 65 72 69 6f 72 20 70 61 67 65 2c 20   interior page, 
1e494 69 66 20 74 68 65 20 63 65 6c 6c 20 73 69 7a 65  if the cell size
1e495 0a 20 20 2a 2a 20 77 61 6e 74 65 64 20 74 6f 20  .  ** wanted to 
1e496 62 65 20 6c 65 73 73 20 74 68 61 6e 20 34 20 62  be less than 4 b
1e497 75 74 20 67 6f 74 20 72 6f 75 6e 64 65 64 20 75  ut got rounded u
1e498 70 20 74 6f 20 34 20 6f 6e 20 74 68 65 20 6c 65  p to 4 on the le
1e499 61 66 2c 20 74 68 65 6e 20 73 69 7a 65 0a 20 20  af, then size.  
1e49a 2a 2a 20 6d 69 67 68 74 20 62 65 20 6c 65 73 73  ** might be less
1e49b 20 74 68 61 6e 20 38 20 28 6c 65 61 66 2d 73 69   than 8 (leaf-si
1e49c 7a 65 20 2b 20 70 6f 69 6e 74 65 72 29 20 6f 6e  ze + pointer) on
1e49d 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f   the interior no
1e49e 64 65 2e 20 20 48 65 6e 63 65 0a 20 20 2a 2a 20  de.  Hence.  ** 
1e49f 74 68 65 20 74 65 72 6d 20 61 66 74 65 72 20 74  the term after t
1e4a0 68 65 20 7c 7c 20 69 6e 20 74 68 65 20 66 6f 6c  he || in the fol
1e4a1 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 28 29 2e  lowing assert().
1e4a2 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 7a   */.  assert( sz
1e4a3 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50  ==cellSizePtr(pP
1e4a4 61 67 65 2c 20 70 43 65 6c 6c 29 20 7c 7c 20 28  age, pCell) || (
1e4a5 73 7a 3d 3d 38 20 26 26 20 69 43 68 69 6c 64 3e  sz==8 && iChild>
1e4a6 30 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67  0) );.  if( pPag
1e4a7 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20  e->nOverflow || 
1e4a8 73 7a 2b 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65  sz+2>pPage->nFre
1e4a9 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65  e ){.    if( pTe
1e4aa 6d 70 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  mp ){.      memc
1e4ab 70 79 28 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20  py(pTemp+nSkip, 
1e4ac 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d  pCell+nSkip, sz-
1e4ad 6e 53 6b 69 70 29 3b 0a 20 20 20 20 20 20 70 43  nSkip);.      pC
1e4ae 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20  ell = pTemp;.   
1e4af 20 7d 0a 20 20 20 20 69 66 28 20 69 43 68 69 6c   }.    if( iChil
1e4b0 64 20 29 7b 0a 20 20 20 20 20 20 70 75 74 34 62  d ){.      put4b
1e4b1 79 74 65 28 70 43 65 6c 6c 2c 20 69 43 68 69 6c  yte(pCell, iChil
1e4b2 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 20  d);.    }.    j 
1e4b3 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  = pPage->nOverfl
1e4b4 6f 77 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72 74  ow++;.    assert
1e4b5 28 20 6a 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66  ( j<(int)(sizeof
1e4b6 28 70 50 61 67 65 2d 3e 61 70 4f 76 66 6c 29 2f  (pPage->apOvfl)/
1e4b7 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 70  sizeof(pPage->ap
1e4b8 4f 76 66 6c 5b 30 5d 29 29 20 29 3b 0a 20 20 20  Ovfl[0])) );.   
1e4b9 20 70 50 61 67 65 2d 3e 61 70 4f 76 66 6c 5b 6a   pPage->apOvfl[j
1e4ba 5d 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 20 70  ] = pCell;.    p
1e4bb 50 61 67 65 2d 3e 61 69 4f 76 66 6c 5b 6a 5d 20  Page->aiOvfl[j] 
1e4bc 3d 20 28 75 31 36 29 69 3b 0a 20 20 7d 65 6c 73  = (u16)i;.  }els
1e4bd 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  e{.    int rc = 
1e4be 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
1e4bf 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
1e4c0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
1e4c1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1e4c2 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20    *pRC = rc;.   
1e4c3 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
1e4c4 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
1e4c5 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
1e4c6 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50  able(pPage->pDbP
1e4c7 61 67 65 29 20 29 3b 0a 20 20 20 20 64 61 74 61  age) );.    data
1e4c8 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b   = pPage->aData;
1e4c9 0a 20 20 20 20 63 65 6c 6c 4f 66 66 73 65 74 20  .    cellOffset 
1e4ca 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66  = pPage->cellOff
1e4cb 73 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d 20 63  set;.    end = c
1e4cc 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50  ellOffset + 2*pP
1e4cd 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20  age->nCell;.    
1e4ce 69 6e 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74  ins = cellOffset
1e4cf 20 2b 20 32 2a 69 3b 0a 20 20 20 20 72 63 20 3d   + 2*i;.    rc =
1e4d0 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 70   allocateSpace(p
1e4d1 50 61 67 65 2c 20 73 7a 2c 20 26 69 64 78 29 3b  Page, sz, &idx);
1e4d2 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 20 2a  .    if( rc ){ *
1e4d3 70 52 43 20 3d 20 72 63 3b 20 72 65 74 75 72 6e  pRC = rc; return
1e4d4 3b 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 61  ; }.    /* The a
1e4d5 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 29 20 72  llocateSpace() r
1e4d6 6f 75 74 69 6e 65 20 67 75 61 72 61 6e 74 65 65  outine guarantee
1e4d7 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  s the following 
1e4d8 74 77 6f 20 70 72 6f 70 65 72 74 69 65 73 0a 20  two properties. 
1e4d9 20 20 20 2a 2a 20 69 66 20 69 74 20 72 65 74 75     ** if it retu
1e4da 72 6e 73 20 73 75 63 63 65 73 73 20 2a 2f 0a 20  rns success */. 
1e4db 20 20 20 61 73 73 65 72 74 28 20 69 64 78 20 3e     assert( idx >
1e4dc 3d 20 65 6e 64 2b 32 20 29 3b 0a 20 20 20 20 61  = end+2 );.    a
1e4dd 73 73 65 72 74 28 20 69 64 78 2b 73 7a 20 3c 3d  ssert( idx+sz <=
1e4de 20 28 69 6e 74 29 70 50 61 67 65 2d 3e 70 42 74   (int)pPage->pBt
1e4df 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a  ->usableSize );.
1e4e0 20 20 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c      pPage->nCell
1e4e1 2b 2b 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e  ++;.    pPage->n
1e4e2 46 72 65 65 20 2d 3d 20 28 75 31 36 29 28 32 20  Free -= (u16)(2 
1e4e3 2b 20 73 7a 29 3b 0a 20 20 20 20 6d 65 6d 63 70  + sz);.    memcp
1e4e4 79 28 26 64 61 74 61 5b 69 64 78 2b 6e 53 6b 69  y(&data[idx+nSki
1e4e5 70 5d 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c  p], pCell+nSkip,
1e4e6 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20   sz-nSkip);.    
1e4e7 69 66 28 20 69 43 68 69 6c 64 20 29 7b 0a 20 20  if( iChild ){.  
1e4e8 20 20 20 20 70 75 74 34 62 79 74 65 28 26 64 61      put4byte(&da
1e4e9 74 61 5b 69 64 78 5d 2c 20 69 43 68 69 6c 64 29  ta[idx], iChild)
1e4ea 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 74 72 20  ;.    }.    ptr 
1e4eb 3d 20 26 64 61 74 61 5b 65 6e 64 5d 3b 0a 20 20  = &data[end];.  
1e4ec 20 20 65 6e 64 50 74 72 20 3d 20 26 64 61 74 61    endPtr = &data
1e4ed 5b 69 6e 73 5d 3b 0a 20 20 20 20 61 73 73 65 72  [ins];.    asser
1e4ee 74 28 20 28 53 51 4c 49 54 45 5f 50 54 52 5f 54  t( (SQLITE_PTR_T
1e4ef 4f 5f 49 4e 54 28 70 74 72 29 26 31 29 3d 3d 30  O_INT(ptr)&1)==0
1e4f0 20 29 3b 20 20 2f 2a 20 70 74 72 20 69 73 20 61   );  /* ptr is a
1e4f1 6c 77 61 79 73 20 32 2d 62 79 74 65 20 61 6c 69  lways 2-byte ali
1e4f2 67 6e 65 64 20 2a 2f 0a 20 20 20 20 77 68 69 6c  gned */.    whil
1e4f3 65 28 20 70 74 72 3e 65 6e 64 50 74 72 20 29 7b  e( ptr>endPtr ){
1e4f4 0a 20 20 20 20 20 20 2a 28 75 31 36 2a 29 70 74  .      *(u16*)pt
1e4f5 72 20 3d 20 2a 28 75 31 36 2a 29 26 70 74 72 5b  r = *(u16*)&ptr[
1e4f6 2d 32 5d 3b 0a 20 20 20 20 20 20 70 74 72 20 2d  -2];.      ptr -
1e4f7 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  = 2;.    }.    p
1e4f8 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 6e  ut2byte(&data[in
1e4f9 73 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20 70 75  s], idx);.    pu
1e4fa 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 50 61  t2byte(&data[pPa
1e4fb 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 33 5d  ge->hdrOffset+3]
1e4fc 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b  , pPage->nCell);
1e4fd 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1e4fe 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
1e4ff 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70      if( pPage->p
1e500 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
1e501 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  {.      /* The c
1e502 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20  ell may contain 
1e503 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
1e504 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49  overflow page. I
1e505 66 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20  f so, write.    
1e506 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66    ** the entry f
1e507 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  or the overflow 
1e508 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f  page into the po
1e509 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20  inter map..     
1e50a 20 2a 2f 0a 20 20 20 20 20 20 70 74 72 6d 61 70   */.      ptrmap
1e50b 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65  PutOvflPtr(pPage
1e50c 2c 20 70 43 65 6c 6c 2c 20 70 52 43 29 3b 0a 20  , pCell, pRC);. 
1e50d 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
1e50e 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c  }../*.** Add a l
1e50f 69 73 74 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20  ist of cells to 
1e510 61 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67  a page.  The pag
1e511 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74  e should be init
1e512 69 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20  ially empty..** 
1e513 54 68 65 20 63 65 6c 6c 73 20 61 72 65 20 67 75  The cells are gu
1e514 61 72 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20  aranteed to fit 
1e515 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a  on the page..*/.
1e516 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65  static void asse
1e517 6d 62 6c 65 50 61 67 65 28 0a 20 20 4d 65 6d 50  mblePage(.  MemP
1e518 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a  age *pPage,   /*
1e519 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20   The page to be 
1e51a 61 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20  assemblied */.  
1e51b 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20  int nCell,      
1e51c 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20    /* The number 
1e51d 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20  of cells to add 
1e51e 74 6f 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a  to this page */.
1e51f 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20    u8 **apCell,  
1e520 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20      /* Pointers 
1e521 74 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a  to cell bodies *
1e522 2f 0a 20 20 75 31 36 20 2a 61 53 69 7a 65 20 20  /.  u16 *aSize  
1e523 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f        /* Sizes o
1e524 66 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29  f the cells */.)
1e525 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
1e526 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
1e527 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 70  unter */.  u8 *p
1e528 43 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f 2a 20  Cellptr;     /* 
1e529 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20  Address of next 
1e52a 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a  cell pointer */.
1e52b 20 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20    int cellbody; 
1e52c 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
1e52d 66 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f 64 79  f next cell body
1e52e 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74   */.  u8 * const
1e52f 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
1e530 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
1e531 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
1e532 64 61 74 61 20 66 6f 72 20 70 50 61 67 65 20 2a  data for pPage *
1e533 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64  /.  const int hd
1e534 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  r = pPage->hdrOf
1e535 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20  fset;           
1e536 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61  /* Offset of hea
1e537 64 65 72 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a  der on pPage */.
1e538 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55 73 61    const int nUsa
1e539 62 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74  ble = pPage->pBt
1e53a 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a  ->usableSize; /*
1e53b 20 55 73 61 62 6c 65 20 73 69 7a 65 20 6f 66 20   Usable size of 
1e53c 70 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72  page */..  asser
1e53d 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  t( pPage->nOverf
1e53e 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  low==0 );.  asse
1e53f 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1e540 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
1e541 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
1e542 73 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d 30 20  ssert( nCell>=0 
1e543 26 26 20 6e 43 65 6c 6c 3c 3d 28 69 6e 74 29 4d  && nCell<=(int)M
1e544 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42  X_CELL(pPage->pB
1e545 74 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 26  t).            &
1e546 26 20 28 69 6e 74 29 4d 58 5f 43 45 4c 4c 28 70  & (int)MX_CELL(p
1e547 50 61 67 65 2d 3e 70 42 74 29 3c 3d 31 30 39 32  Page->pBt)<=1092
1e548 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  1);.  assert( sq
1e549 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
1e54a 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
1e54b 50 61 67 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 43  Page) );..  /* C
1e54c 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 61  heck that the pa
1e54d 67 65 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  ge has just been
1e54e 20 7a 65 72 6f 65 64 20 62 79 20 7a 65 72 6f 50   zeroed by zeroP
1e54f 61 67 65 28 29 20 2a 2f 0a 20 20 61 73 73 65 72  age() */.  asser
1e550 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d  t( pPage->nCell=
1e551 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1e552 67 65 74 32 62 79 74 65 4e 6f 74 5a 65 72 6f 28  get2byteNotZero(
1e553 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3d 3d 6e  &data[hdr+5])==n
1e554 55 73 61 62 6c 65 20 29 3b 0a 0a 20 20 70 43 65  Usable );..  pCe
1e555 6c 6c 70 74 72 20 3d 20 26 70 50 61 67 65 2d 3e  llptr = &pPage->
1e556 61 43 65 6c 6c 49 64 78 5b 6e 43 65 6c 6c 2a 32  aCellIdx[nCell*2
1e557 5d 3b 0a 20 20 63 65 6c 6c 62 6f 64 79 20 3d 20  ];.  cellbody = 
1e558 6e 55 73 61 62 6c 65 3b 0a 20 20 66 6f 72 28 69  nUsable;.  for(i
1e559 3d 6e 43 65 6c 6c 2d 31 3b 20 69 3e 3d 30 3b 20  =nCell-1; i>=0; 
1e55a 69 2d 2d 29 7b 0a 20 20 20 20 75 31 36 20 73 7a  i--){.    u16 sz
1e55b 20 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20 20   = aSize[i];.   
1e55c 20 70 43 65 6c 6c 70 74 72 20 2d 3d 20 32 3b 0a   pCellptr -= 2;.
1e55d 20 20 20 20 63 65 6c 6c 62 6f 64 79 20 2d 3d 20      cellbody -= 
1e55e 73 7a 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65  sz;.    put2byte
1e55f 28 70 43 65 6c 6c 70 74 72 2c 20 63 65 6c 6c 62  (pCellptr, cellb
1e560 6f 64 79 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  ody);.    memcpy
1e561 28 26 64 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d  (&data[cellbody]
1e562 2c 20 61 70 43 65 6c 6c 5b 69 5d 2c 20 73 7a 29  , apCell[i], sz)
1e563 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62 79 74 65  ;.  }.  put2byte
1e564 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 6e  (&data[hdr+3], n
1e565 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32 62 79 74  Cell);.  put2byt
1e566 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20  e(&data[hdr+5], 
1e567 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61  cellbody);.  pPa
1e568 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 6e 43  ge->nFree -= (nC
1e569 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62 6c 65 20  ell*2 + nUsable 
1e56a 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70  - cellbody);.  p
1e56b 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28 75  Page->nCell = (u
1e56c 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a  16)nCell;.}../*.
1e56d 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
1e56e 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74 65   parameters dete
1e56f 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61  rmine how many a
1e570 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67 65  djacent pages ge
1e571 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e  t involved.** in
1e572 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65   a balancing ope
1e573 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74  ration.  NN is t
1e574 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69  he number of nei
1e575 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72  ghbors on either
1e576 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20   side.** of the 
1e577 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69 63  page that partic
1e578 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c  ipate in the bal
1e579 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ancing operation
1e57a 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20  .  NB is the.** 
1e57b 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
1e57c 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74 69  pages that parti
1e57d 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e  cipate, includin
1e57e 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61 67  g the target pag
1e57f 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67  e and.** NN neig
1e580 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20  hbors on either 
1e581 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  side..**.** The 
1e582 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66  minimum value of
1e583 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75   NN is 1 (of cou
1e584 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e  rse).  Increasin
1e585 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20  g NN above 1.** 
1e586 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65  (to 2 or 3) give
1e587 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f  s a modest impro
1e588 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54  vement in SELECT
1e589 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66   and DELETE perf
1e58a 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78  ormance.** in ex
1e58b 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72  change for a lar
1e58c 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20  ger degradation 
1e58d 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50  in INSERT and UP
1e58e 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65  DATE performance
1e58f 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f  ..** The value o
1e590 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20  f NN appears to 
1e591 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72 65  give the best re
1e592 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a  sults overall..*
1e593 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20  /.#define NN 1  
1e594 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1e595 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72  mber of neighbor
1e596 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65  s on either side
1e597 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65   of pPage */.#de
1e598 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29  fine NB (NN*2+1)
1e599 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70        /* Total p
1e59a 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e  ages involved in
1e59b 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a   the balance */.
1e59c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1e59d 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e  _OMIT_QUICKBALAN
1e59e 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65  CE./*.** This ve
1e59f 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e 63 65  rsion of balance
1e5a0 28 29 20 68 61 6e 64 6c 65 73 20 74 68 65 20 63  () handles the c
1e5a1 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20 63 61  ommon special ca
1e5a2 73 65 20 77 68 65 72 65 0a 2a 2a 20 61 20 6e 65  se where.** a ne
1e5a3 77 20 65 6e 74 72 79 20 69 73 20 62 65 69 6e 67  w entry is being
1e5a4 20 69 6e 73 65 72 74 65 64 20 6f 6e 20 74 68 65   inserted on the
1e5a5 20 65 78 74 72 65 6d 65 20 72 69 67 68 74 2d 65   extreme right-e
1e5a6 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72 65  nd of the.** tre
1e5a7 65 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64  e, in other word
1e5a8 73 2c 20 77 68 65 6e 20 74 68 65 20 6e 65 77 20  s, when the new 
1e5a9 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 63 6f 6d  entry will becom
1e5aa 65 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a  e the largest.**
1e5ab 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72   entry in the tr
1e5ac 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61  ee..**.** Instea
1e5ad 64 20 6f 66 20 74 72 79 69 6e 67 20 74 6f 20 62  d of trying to b
1e5ae 61 6c 61 6e 63 65 20 74 68 65 20 33 20 72 69 67  alance the 3 rig
1e5af 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 70 61 67  ht-most leaf pag
1e5b0 65 73 2c 20 6a 75 73 74 20 61 64 64 0a 2a 2a 20  es, just add.** 
1e5b1 61 20 6e 65 77 20 70 61 67 65 20 74 6f 20 74 68  a new page to th
1e5b2 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  e right-hand sid
1e5b3 65 20 61 6e 64 20 70 75 74 20 74 68 65 20 6f 6e  e and put the on
1e5b4 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 0a 2a  e new entry in.*
1e5b5 2a 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 68  * that page.  Th
1e5b6 69 73 20 6c 65 61 76 65 73 20 74 68 65 20 72 69  is leaves the ri
1e5b7 67 68 74 20 73 69 64 65 20 6f 66 20 74 68 65 20  ght side of the 
1e5b8 74 72 65 65 20 73 6f 6d 65 77 68 61 74 0a 2a 2a  tree somewhat.**
1e5b9 20 75 6e 62 61 6c 61 6e 63 65 64 2e 20 20 42 75   unbalanced.  Bu
1e5ba 74 20 6f 64 64 73 20 61 72 65 20 74 68 61 74 20  t odds are that 
1e5bb 77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72  we will be inser
1e5bc 74 69 6e 67 20 6e 65 77 20 65 6e 74 72 69 65 73  ting new entries
1e5bd 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 73  .** at the end s
1e5be 6f 6f 6e 20 61 66 74 65 72 77 61 72 64 73 20 73  oon afterwards s
1e5bf 6f 20 74 68 65 20 6e 65 61 72 6c 79 20 65 6d 70  o the nearly emp
1e5c0 74 79 20 70 61 67 65 20 77 69 6c 6c 20 71 75 69  ty page will qui
1e5c1 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 75 70 2e  ckly.** fill up.
1e5c2 20 20 4f 6e 20 61 76 65 72 61 67 65 2e 0a 2a 2a    On average..**
1e5c3 0a 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68 65  .** pPage is the
1e5c4 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68   leaf page which
1e5c5 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f   is the right-mo
1e5c6 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 74  st page in the t
1e5c7 72 65 65 2e 0a 2a 2a 20 70 50 61 72 65 6e 74 20  ree..** pParent 
1e5c8 69 73 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20  is its parent.  
1e5c9 70 50 61 67 65 20 6d 75 73 74 20 68 61 76 65 20  pPage must have 
1e5ca 61 20 73 69 6e 67 6c 65 20 6f 76 65 72 66 6c 6f  a single overflo
1e5cb 77 20 65 6e 74 72 79 0a 2a 2a 20 77 68 69 63 68  w entry.** which
1e5cc 20 69 73 20 61 6c 73 6f 20 74 68 65 20 72 69 67   is also the rig
1e5cd 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 6f 6e  ht-most entry on
1e5ce 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   the page..**.**
1e5cf 20 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66   The pSpace buff
1e5d0 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 73 74  er is used to st
1e5d1 6f 72 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20  ore a temporary 
1e5d2 63 6f 70 79 20 6f 66 20 74 68 65 20 64 69 76 69  copy of the divi
1e5d3 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 74 68 61 74  der.** cell that
1e5d4 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 65   will be inserte
1e5d5 64 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20  d into pParent. 
1e5d6 53 75 63 68 20 61 20 63 65 6c 6c 20 63 6f 6e 73  Such a cell cons
1e5d7 69 73 74 73 20 6f 66 20 61 20 34 0a 2a 2a 20 62  ists of a 4.** b
1e5d8 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  yte page number 
1e5d9 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 76 61  followed by a va
1e5da 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e  riable length in
1e5db 74 65 67 65 72 2e 20 49 6e 20 6f 74 68 65 72 0a  teger. In other.
1e5dc 2a 2a 20 77 6f 72 64 73 2c 20 61 74 20 6d 6f 73  ** words, at mos
1e5dd 74 20 31 33 20 62 79 74 65 73 2e 20 48 65 6e 63  t 13 bytes. Henc
1e5de 65 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66  e the pSpace buf
1e5df 66 65 72 20 6d 75 73 74 20 62 65 20 61 74 0a 2a  fer must be at.*
1e5e0 2a 20 6c 65 61 73 74 20 31 33 20 62 79 74 65 73  * least 13 bytes
1e5e1 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61   in size..*/.sta
1e5e2 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f  tic int balance_
1e5e3 71 75 69 63 6b 28 4d 65 6d 50 61 67 65 20 2a 70  quick(MemPage *p
1e5e4 50 61 72 65 6e 74 2c 20 4d 65 6d 50 61 67 65 20  Parent, MemPage 
1e5e5 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 53 70 61  *pPage, u8 *pSpa
1e5e6 63 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  ce){.  BtShared 
1e5e7 2a 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 50 61  *const pBt = pPa
1e5e8 67 65 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20 42  ge->pBt;    /* B
1e5e9 2d 54 72 65 65 20 44 61 74 61 62 61 73 65 20 2a  -Tree Database *
1e5ea 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65  /.  MemPage *pNe
1e5eb 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
1e5ec 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 6c           /* Newl
1e5ed 79 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65  y allocated page
1e5ee 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
1e5ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e5f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
1e5f1 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 50  turn Code */.  P
1e5f2 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20 20 20 20  gno pgnoNew;    
1e5f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e5f4 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62      /* Page numb
1e5f5 65 72 20 6f 66 20 70 4e 65 77 20 2a 2f 0a 0a 20  er of pNew */.. 
1e5f6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1e5f7 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
1e5f8 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
1e5f9 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
1e5fa 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
1e5fb 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62  ble(pParent->pDb
1e5fc 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72  Page) );.  asser
1e5fd 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  t( pPage->nOverf
1e5fe 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20  low==1 );..  /* 
1e5ff 54 68 69 73 20 65 72 72 6f 72 20 63 6f 6e 64 69  This error condi
1e600 74 69 6f 6e 20 69 73 20 6e 6f 77 20 63 61 75 67  tion is now caug
1e601 68 74 20 70 72 69 6f 72 20 74 6f 20 72 65 61 63  ht prior to reac
1e602 68 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69  hing this functi
1e603 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67  on */.  if( pPag
1e604 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 20 72 65  e->nCell==0 ) re
1e605 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
1e606 55 50 54 5f 42 4b 50 54 3b 0a 0a 20 20 2f 2a 20  UPT_BKPT;..  /* 
1e607 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  Allocate a new p
1e608 61 67 65 2e 20 54 68 69 73 20 70 61 67 65 20 77  age. This page w
1e609 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 72  ill become the r
1e60a 69 67 68 74 2d 73 69 62 6c 69 6e 67 20 6f 66 20  ight-sibling of 
1e60b 0a 20 20 2a 2a 20 70 50 61 67 65 2e 20 4d 61 6b  .  ** pPage. Mak
1e60c 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  e the parent pag
1e60d 65 20 77 72 69 74 61 62 6c 65 2c 20 73 6f 20 74  e writable, so t
1e60e 68 61 74 20 74 68 65 20 6e 65 77 20 64 69 76 69  hat the new divi
1e60f 64 65 72 20 63 65 6c 6c 0a 20 20 2a 2a 20 6d 61  der cell.  ** ma
1e610 79 20 62 65 20 69 6e 73 65 72 74 65 64 2e 20 49  y be inserted. I
1e611 66 20 62 6f 74 68 20 74 68 65 73 65 20 6f 70 65  f both these ope
1e612 72 61 74 69 6f 6e 73 20 61 72 65 20 73 75 63 63  rations are succ
1e613 65 73 73 66 75 6c 2c 20 70 72 6f 63 65 65 64 2e  essful, proceed.
1e614 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 61 6c 6c  .  */.  rc = all
1e615 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70  ocateBtreePage(p
1e616 42 74 2c 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f  Bt, &pNew, &pgno
1e617 4e 65 77 2c 20 30 2c 20 30 29 3b 0a 0a 20 20 69  New, 0, 0);..  i
1e618 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1e619 20 29 7b 0a 0a 20 20 20 20 75 38 20 2a 70 4f 75   ){..    u8 *pOu
1e61a 74 20 3d 20 26 70 53 70 61 63 65 5b 34 5d 3b 0a  t = &pSpace[4];.
1e61b 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20      u8 *pCell = 
1e61c 70 50 61 67 65 2d 3e 61 70 4f 76 66 6c 5b 30 5d  pPage->apOvfl[0]
1e61d 3b 0a 20 20 20 20 75 31 36 20 73 7a 43 65 6c 6c  ;.    u16 szCell
1e61e 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70   = cellSizePtr(p
1e61f 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  Page, pCell);.  
1e620 20 20 75 38 20 2a 70 53 74 6f 70 3b 0a 0a 20 20    u8 *pStop;..  
1e621 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1e622 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
1e623 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 29  e(pNew->pDbPage)
1e624 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1e625 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 3d  pPage->aData[0]=
1e626 3d 28 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46  =(PTF_INTKEY|PTF
1e627 5f 4c 45 41 46 44 41 54 41 7c 50 54 46 5f 4c 45  _LEAFDATA|PTF_LE
1e628 41 46 29 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50  AF) );.    zeroP
1e629 61 67 65 28 70 4e 65 77 2c 20 50 54 46 5f 49 4e  age(pNew, PTF_IN
1e62a 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 44 41 54  TKEY|PTF_LEAFDAT
1e62b 41 7c 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 20  A|PTF_LEAF);.   
1e62c 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 4e   assemblePage(pN
1e62d 65 77 2c 20 31 2c 20 26 70 43 65 6c 6c 2c 20 26  ew, 1, &pCell, &
1e62e 73 7a 43 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a  szCell);..    /*
1e62f 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 61   If this is an a
1e630 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62  uto-vacuum datab
1e631 61 73 65 2c 20 75 70 64 61 74 65 20 74 68 65 20  ase, update the 
1e632 70 6f 69 6e 74 65 72 20 6d 61 70 0a 20 20 20 20  pointer map.    
1e633 2a 2a 20 77 69 74 68 20 65 6e 74 72 69 65 73 20  ** with entries 
1e634 66 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67 65  for the new page
1e635 2c 20 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65  , and any pointe
1e636 72 20 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20  r from the .    
1e637 2a 2a 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70  ** cell on the p
1e638 61 67 65 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c  age to an overfl
1e639 6f 77 20 70 61 67 65 2e 20 49 66 20 65 69 74 68  ow page. If eith
1e63a 65 72 20 6f 66 20 74 68 65 73 65 0a 20 20 20 20  er of these.    
1e63b 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 73 20 66 61  ** operations fa
1e63c 69 6c 73 2c 20 74 68 65 20 72 65 74 75 72 6e 20  ils, the return 
1e63d 63 6f 64 65 20 69 73 20 73 65 74 2c 20 62 75 74  code is set, but
1e63e 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20   the contents.  
1e63f 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 65    ** of the pare
1e640 6e 74 20 70 61 67 65 20 61 72 65 20 73 74 69 6c  nt page are stil
1e641 6c 20 6d 61 6e 69 70 75 6c 61 74 65 64 20 62 79  l manipulated by
1e642 20 74 68 68 20 63 6f 64 65 20 62 65 6c 6f 77 2e   thh code below.
1e643 0a 20 20 20 20 2a 2a 20 54 68 61 74 20 69 73 20  .    ** That is 
1e644 4f 6b 2c 20 61 74 20 74 68 69 73 20 70 6f 69 6e  Ok, at this poin
1e645 74 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  t the parent pag
1e646 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  e is guaranteed 
1e647 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 6d 61 72  to.    ** be mar
1e648 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 52 65  ked as dirty. Re
1e649 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72  turning an error
1e64a 20 63 6f 64 65 20 77 69 6c 6c 20 63 61 75 73 65   code will cause
1e64b 20 61 0a 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 61   a.    ** rollba
1e64c 63 6b 2c 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20  ck, undoing any 
1e64d 63 68 61 6e 67 65 73 20 6d 61 64 65 20 74 6f 20  changes made to 
1e64e 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e  the parent page.
1e64f 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1e650 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a  ISAUTOVACUUM ){.
1e651 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28        ptrmapPut(
1e652 70 42 74 2c 20 70 67 6e 6f 4e 65 77 2c 20 50 54  pBt, pgnoNew, PT
1e653 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72  RMAP_BTREE, pPar
1e654 65 6e 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b  ent->pgno, &rc);
1e655 0a 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c  .      if( szCel
1e656 6c 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 6c  l>pNew->minLocal
1e657 20 29 7b 0a 20 20 20 20 20 20 20 20 70 74 72 6d   ){.        ptrm
1e658 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 4e 65  apPutOvflPtr(pNe
1e659 77 2c 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a  w, pCell, &rc);.
1e65a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1e65b 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61  .    /* Create a
1e65c 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 74 6f   divider cell to
1e65d 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 70 50 61   insert into pPa
1e65e 72 65 6e 74 2e 20 54 68 65 20 64 69 76 69 64 65  rent. The divide
1e65f 72 20 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 63 6f  r cell.    ** co
1e660 6e 73 69 73 74 73 20 6f 66 20 61 20 34 2d 62 79  nsists of a 4-by
1e661 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 28  te page number (
1e662 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
1e663 6f 66 20 70 50 61 67 65 29 20 61 6e 64 0a 20 20  of pPage) and.  
1e664 20 20 2a 2a 20 61 20 76 61 72 69 61 62 6c 65 20    ** a variable 
1e665 6c 65 6e 67 74 68 20 6b 65 79 20 76 61 6c 75 65  length key value
1e666 20 28 77 68 69 63 68 20 6d 75 73 74 20 62 65 20   (which must be 
1e667 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61  the same value a
1e668 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 6c 61 72  s the.    ** lar
1e669 67 65 73 74 20 6b 65 79 20 6f 6e 20 70 50 61 67  gest key on pPag
1e66a 65 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  e)..    **.    *
1e66b 2a 20 54 6f 20 66 69 6e 64 20 74 68 65 20 6c 61  * To find the la
1e66c 72 67 65 73 74 20 6b 65 79 20 76 61 6c 75 65 20  rgest key value 
1e66d 6f 6e 20 70 50 61 67 65 2c 20 66 69 72 73 74 20  on pPage, first 
1e66e 66 69 6e 64 20 74 68 65 20 72 69 67 68 74 2d 6d  find the right-m
1e66f 6f 73 74 20 0a 20 20 20 20 2a 2a 20 63 65 6c 6c  ost .    ** cell
1e670 20 6f 6e 20 70 50 61 67 65 2e 20 54 68 65 20 66   on pPage. The f
1e671 69 72 73 74 20 74 77 6f 20 66 69 65 6c 64 73 20  irst two fields 
1e672 6f 66 20 74 68 69 73 20 63 65 6c 6c 20 61 72 65  of this cell are
1e673 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 63   the .    ** rec
1e674 6f 72 64 2d 6c 65 6e 67 74 68 20 28 61 20 76 61  ord-length (a va
1e675 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e  riable length in
1e676 74 65 67 65 72 20 61 74 20 6d 6f 73 74 20 33 32  teger at most 32
1e677 2d 62 69 74 73 20 69 6e 20 73 69 7a 65 29 0a 20  -bits in size). 
1e678 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 6b 65     ** and the ke
1e679 79 20 76 61 6c 75 65 20 28 61 20 76 61 72 69 61  y value (a varia
1e67a 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble length integ
1e67b 65 72 2c 20 6d 61 79 20 68 61 76 65 20 61 6e 79  er, may have any
1e67c 20 76 61 6c 75 65 29 2e 0a 20 20 20 20 2a 2a 20   value)..    ** 
1e67d 54 68 65 20 66 69 72 73 74 20 6f 66 20 74 68 65  The first of the
1e67e 20 77 68 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f 70   while(...) loop
1e67f 73 20 62 65 6c 6f 77 20 73 6b 69 70 73 20 6f 76  s below skips ov
1e680 65 72 20 74 68 65 20 72 65 63 6f 72 64 2d 6c 65  er the record-le
1e681 6e 67 74 68 0a 20 20 20 20 2a 2a 20 66 69 65 6c  ngth.    ** fiel
1e682 64 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 77 68  d. The second wh
1e683 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f 70 20 63 6f  ile(...) loop co
1e684 70 69 65 73 20 74 68 65 20 6b 65 79 20 76 61 6c  pies the key val
1e685 75 65 20 66 72 6f 6d 20 74 68 65 0a 20 20 20 20  ue from the.    
1e686 2a 2a 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65  ** cell on pPage
1e687 20 69 6e 74 6f 20 74 68 65 20 70 53 70 61 63 65   into the pSpace
1e688 20 62 75 66 66 65 72 2e 0a 20 20 20 20 2a 2f 0a   buffer..    */.
1e689 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64      pCell = find
1e68a 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 50 61 67  Cell(pPage, pPag
1e68b 65 2d 3e 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 20  e->nCell-1);.   
1e68c 20 70 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b   pStop = &pCell[
1e68d 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28  9];.    while( (
1e68e 2a 28 70 43 65 6c 6c 2b 2b 29 26 30 78 38 30 29  *(pCell++)&0x80)
1e68f 20 26 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20   && pCell<pStop 
1e690 29 3b 0a 20 20 20 20 70 53 74 6f 70 20 3d 20 26  );.    pStop = &
1e691 70 43 65 6c 6c 5b 39 5d 3b 0a 20 20 20 20 77 68  pCell[9];.    wh
1e692 69 6c 65 28 20 28 28 2a 28 70 4f 75 74 2b 2b 29  ile( ((*(pOut++)
1e693 20 3d 20 2a 28 70 43 65 6c 6c 2b 2b 29 29 26 30   = *(pCell++))&0
1e694 78 38 30 29 20 26 26 20 70 43 65 6c 6c 3c 70 53  x80) && pCell<pS
1e695 74 6f 70 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49  top );..    /* I
1e696 6e 73 65 72 74 20 74 68 65 20 6e 65 77 20 64 69  nsert the new di
1e697 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20  vider cell into 
1e698 70 50 61 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20  pParent. */.    
1e699 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 72 65  insertCell(pPare
1e69a 6e 74 2c 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65  nt, pParent->nCe
1e69b 6c 6c 2c 20 70 53 70 61 63 65 2c 20 28 69 6e 74  ll, pSpace, (int
1e69c 29 28 70 4f 75 74 2d 70 53 70 61 63 65 29 2c 0a  )(pOut-pSpace),.
1e69d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
1e69e 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 26  , pPage->pgno, &
1e69f 72 63 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74  rc);..    /* Set
1e6a0 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64   the right-child
1e6a1 20 70 6f 69 6e 74 65 72 20 6f 66 20 70 50 61 72   pointer of pPar
1e6a2 65 6e 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  ent to point to 
1e6a3 74 68 65 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f  the new page. */
1e6a4 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70  .    put4byte(&p
1e6a5 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50  Parent->aData[pP
1e6a6 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74  arent->hdrOffset
1e6a7 2b 38 5d 2c 20 70 67 6e 6f 4e 65 77 29 3b 0a 20  +8], pgnoNew);. 
1e6a8 20 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65   .    /* Release
1e6a9 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 74   the reference t
1e6aa 6f 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 20  o the new page. 
1e6ab 2a 2f 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  */.    releasePa
1e6ac 67 65 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20  ge(pNew);.  }.. 
1e6ad 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
1e6ae 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1e6af 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45  MIT_QUICKBALANCE
1e6b0 20 2a 2f 0a 0a 23 69 66 20 30 0a 2f 2a 0a 2a 2a   */..#if 0./*.**
1e6b1 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64   This function d
1e6b2 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72 69 62 75  oes not contribu
1e6b3 74 65 20 61 6e 79 74 68 69 6e 67 20 74 6f 20 74  te anything to t
1e6b4 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20  he operation of 
1e6b5 53 51 4c 69 74 65 2e 0a 2a 2a 20 69 74 20 69 73  SQLite..** it is
1e6b6 20 73 6f 6d 65 74 69 6d 65 73 20 61 63 74 69 76   sometimes activ
1e6b7 61 74 65 64 20 74 65 6d 70 6f 72 61 72 69 6c 79  ated temporarily
1e6b8 20 77 68 69 6c 65 20 64 65 62 75 67 67 69 6e 67   while debugging
1e6b9 20 63 6f 64 65 20 72 65 73 70 6f 6e 73 69 62 6c   code responsibl
1e6ba 65 20 0a 2a 2a 20 66 6f 72 20 73 65 74 74 69 6e  e .** for settin
1e6bb 67 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e  g pointer-map en
1e6bc 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  tries..*/.static
1e6bd 20 69 6e 74 20 70 74 72 6d 61 70 43 68 65 63 6b   int ptrmapCheck
1e6be 50 61 67 65 73 28 4d 65 6d 50 61 67 65 20 2a 2a  Pages(MemPage **
1e6bf 61 70 50 61 67 65 2c 20 69 6e 74 20 6e 50 61 67  apPage, int nPag
1e6c0 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  e){.  int i, j;.
1e6c1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 50 61    for(i=0; i<nPa
1e6c2 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 67  ge; i++){.    Pg
1e6c3 6e 6f 20 6e 3b 0a 20 20 20 20 75 38 20 65 3b 0a  no n;.    u8 e;.
1e6c4 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61      MemPage *pPa
1e6c5 67 65 20 3d 20 61 70 50 61 67 65 5b 69 5d 3b 0a  ge = apPage[i];.
1e6c6 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42      BtShared *pB
1e6c7 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a  t = pPage->pBt;.
1e6c8 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
1e6c9 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 0a 20 20  e->isInit );..  
1e6ca 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50 61    for(j=0; j<pPa
1e6cb 67 65 2d 3e 6e 43 65 6c 6c 3b 20 6a 2b 2b 29 7b  ge->nCell; j++){
1e6cc 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20  .      CellInfo 
1e6cd 69 6e 66 6f 3b 0a 20 20 20 20 20 20 75 38 20 2a  info;.      u8 *
1e6ce 7a 3b 0a 20 20 20 20 20 0a 20 20 20 20 20 20 7a  z;.     .      z
1e6cf 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67   = findCell(pPag
1e6d0 65 2c 20 6a 29 3b 0a 20 20 20 20 20 20 62 74 72  e, j);.      btr
1e6d1 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70  eeParseCellPtr(p
1e6d2 50 61 67 65 2c 20 7a 2c 20 26 69 6e 66 6f 29 3b  Page, z, &info);
1e6d3 0a 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e  .      if( info.
1e6d4 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20  iOverflow ){.   
1e6d5 20 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d       Pgno ovfl =
1e6d6 20 67 65 74 34 62 79 74 65 28 26 7a 5b 69 6e 66   get4byte(&z[inf
1e6d7 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20  o.iOverflow]);. 
1e6d8 20 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 74         ptrmapGet
1e6d9 28 70 42 74 2c 20 6f 76 66 6c 2c 20 26 65 2c 20  (pBt, ovfl, &e, 
1e6da 26 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  &n);.        ass
1e6db 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70  ert( n==pPage->p
1e6dc 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50  gno && e==PTRMAP
1e6dd 5f 4f 56 45 52 46 4c 4f 57 31 20 29 3b 0a 20 20  _OVERFLOW1 );.  
1e6de 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1e6df 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
1e6e0 20 20 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69          Pgno chi
1e6e1 6c 64 20 3d 20 67 65 74 34 62 79 74 65 28 7a 29  ld = get4byte(z)
1e6e2 3b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 70  ;.        ptrmap
1e6e3 47 65 74 28 70 42 74 2c 20 63 68 69 6c 64 2c 20  Get(pBt, child, 
1e6e4 26 65 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 20  &e, &n);.       
1e6e5 20 61 73 73 65 72 74 28 20 6e 3d 3d 70 50 61 67   assert( n==pPag
1e6e6 65 2d 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54  e->pgno && e==PT
1e6e7 52 4d 41 50 5f 42 54 52 45 45 20 29 3b 0a 20 20  RMAP_BTREE );.  
1e6e8 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1e6e9 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
1e6ea 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 63   ){.      Pgno c
1e6eb 68 69 6c 64 20 3d 20 67 65 74 34 62 79 74 65 28  hild = get4byte(
1e6ec 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50  &pPage->aData[pP
1e6ed 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  age->hdrOffset+8
1e6ee 5d 29 3b 0a 20 20 20 20 20 20 70 74 72 6d 61 70  ]);.      ptrmap
1e6ef 47 65 74 28 70 42 74 2c 20 63 68 69 6c 64 2c 20  Get(pBt, child, 
1e6f0 26 65 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 61  &e, &n);.      a
1e6f1 73 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d  ssert( n==pPage-
1e6f2 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d  >pgno && e==PTRM
1e6f3 41 50 5f 42 54 52 45 45 20 29 3b 0a 20 20 20 20  AP_BTREE );.    
1e6f4 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  }.  }.  return 1
1e6f5 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1e6f6 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1e6f7 69 73 20 75 73 65 64 20 74 6f 20 63 6f 70 79 20  is used to copy 
1e6f8 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
1e6f9 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 64 65 20  the b-tree node 
1e6fa 73 74 6f 72 65 64 20 0a 2a 2a 20 6f 6e 20 70 61  stored .** on pa
1e6fb 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 65  ge pFrom to page
1e6fc 20 70 54 6f 2e 20 49 66 20 70 61 67 65 20 70 46   pTo. If page pF
1e6fd 72 6f 6d 20 77 61 73 20 6e 6f 74 20 61 20 6c 65  rom was not a le
1e6fe 61 66 20 70 61 67 65 2c 20 74 68 65 6e 0a 2a 2a  af page, then.**
1e6ff 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
1e700 20 65 6e 74 72 69 65 73 20 66 6f 72 20 65 61 63   entries for eac
1e701 68 20 63 68 69 6c 64 20 70 61 67 65 20 61 72 65  h child page are
1e702 20 75 70 64 61 74 65 64 20 73 6f 20 74 68 61 74   updated so that
1e703 20 74 68 65 0a 2a 2a 20 70 61 72 65 6e 74 20 70   the.** parent p
1e704 61 67 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68  age stored in th
1e705 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73  e pointer map is
1e706 20 70 61 67 65 20 70 54 6f 2e 20 49 66 20 70 46   page pTo. If pF
1e707 72 6f 6d 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a  rom contained.**
1e708 20 61 6e 79 20 63 65 6c 6c 73 20 77 69 74 68 20   any cells with 
1e709 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 6f  overflow page po
1e70a 69 6e 74 65 72 73 2c 20 74 68 65 6e 20 74 68 65  inters, then the
1e70b 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70   corresponding p
1e70c 6f 69 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e  ointer.** map en
1e70d 74 72 69 65 73 20 61 72 65 20 61 6c 73 6f 20 75  tries are also u
1e70e 70 64 61 74 65 64 20 73 6f 20 74 68 61 74 20 74  pdated so that t
1e70f 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69  he parent page i
1e710 73 20 70 61 67 65 20 70 54 6f 2e 0a 2a 2a 0a 2a  s page pTo..**.*
1e711 2a 20 49 66 20 70 46 72 6f 6d 20 69 73 20 63 75  * If pFrom is cu
1e712 72 72 65 6e 74 6c 79 20 63 61 72 72 79 69 6e 67  rrently carrying
1e713 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63 65   any overflow ce
1e714 6c 6c 73 20 28 65 6e 74 72 69 65 73 20 69 6e 20  lls (entries in 
1e715 74 68 65 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61  the.** MemPage.a
1e716 70 4f 76 66 6c 5b 5d 20 61 72 72 61 79 29 2c 20  pOvfl[] array), 
1e717 74 68 65 79 20 61 72 65 20 6e 6f 74 20 63 6f 70  they are not cop
1e718 69 65 64 20 74 6f 20 70 54 6f 2e 20 0a 2a 2a 0a  ied to pTo. .**.
1e719 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e  ** Before return
1e71a 69 6e 67 2c 20 70 61 67 65 20 70 54 6f 20 69 73  ing, page pTo is
1e71b 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64 20 75   reinitialized u
1e71c 73 69 6e 67 20 62 74 72 65 65 49 6e 69 74 50 61  sing btreeInitPa
1e71d 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ge()..**.** The 
1e71e 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f 66 20 74  performance of t
1e71f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
1e720 6e 6f 74 20 63 72 69 74 69 63 61 6c 2e 20 49 74  not critical. It
1e721 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79   is only used by
1e722 20 0a 2a 2a 20 74 68 65 20 62 61 6c 61 6e 63 65   .** the balance
1e723 5f 73 68 61 6c 6c 6f 77 65 72 28 29 20 61 6e 64  _shallower() and
1e724 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28   balance_deeper(
1e725 29 20 70 72 6f 63 65 64 75 72 65 73 2c 20 6e 65  ) procedures, ne
1e726 69 74 68 65 72 20 6f 66 0a 2a 2a 20 77 68 69 63  ither of.** whic
1e727 68 20 61 72 65 20 63 61 6c 6c 65 64 20 6f 66 74  h are called oft
1e728 65 6e 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20  en under normal 
1e729 63 69 72 63 75 6d 73 74 61 6e 63 65 73 2e 0a 2a  circumstances..*
1e72a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
1e72b 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 4d 65  pyNodeContent(Me
1e72c 6d 50 61 67 65 20 2a 70 46 72 6f 6d 2c 20 4d 65  mPage *pFrom, Me
1e72d 6d 50 61 67 65 20 2a 70 54 6f 2c 20 69 6e 74 20  mPage *pTo, int 
1e72e 2a 70 52 43 29 7b 0a 20 20 69 66 28 20 28 2a 70  *pRC){.  if( (*p
1e72f 52 43 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  RC)==SQLITE_OK )
1e730 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a  {.    BtShared *
1e731 20 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 46 72   const pBt = pFr
1e732 6f 6d 2d 3e 70 42 74 3b 0a 20 20 20 20 75 38 20  om->pBt;.    u8 
1e733 2a 20 63 6f 6e 73 74 20 61 46 72 6f 6d 20 3d 20  * const aFrom = 
1e734 70 46 72 6f 6d 2d 3e 61 44 61 74 61 3b 0a 20 20  pFrom->aData;.  
1e735 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 61 54 6f    u8 * const aTo
1e736 20 3d 20 70 54 6f 2d 3e 61 44 61 74 61 3b 0a 20   = pTo->aData;. 
1e737 20 20 20 69 6e 74 20 63 6f 6e 73 74 20 69 46 72     int const iFr
1e738 6f 6d 48 64 72 20 3d 20 70 46 72 6f 6d 2d 3e 68  omHdr = pFrom->h
1e739 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 69 6e  drOffset;.    in
1e73a 74 20 63 6f 6e 73 74 20 69 54 6f 48 64 72 20 3d  t const iToHdr =
1e73b 20 28 28 70 54 6f 2d 3e 70 67 6e 6f 3d 3d 31 29   ((pTo->pgno==1)
1e73c 20 3f 20 31 30 30 20 3a 20 30 29 3b 0a 20 20 20   ? 100 : 0);.   
1e73d 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e 74   int rc;.    int
1e73e 20 69 44 61 74 61 3b 0a 20 20 0a 20 20 0a 20 20   iData;.  .  .  
1e73f 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d    assert( pFrom-
1e740 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 61  >isInit );.    a
1e741 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 6e 46  ssert( pFrom->nF
1e742 72 65 65 3e 3d 69 54 6f 48 64 72 20 29 3b 0a 20  ree>=iToHdr );. 
1e743 20 20 20 61 73 73 65 72 74 28 20 67 65 74 32 62     assert( get2b
1e744 79 74 65 28 26 61 46 72 6f 6d 5b 69 46 72 6f 6d  yte(&aFrom[iFrom
1e745 48 64 72 2b 35 5d 29 20 3c 3d 20 28 69 6e 74 29  Hdr+5]) <= (int)
1e746 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
1e747 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 43 6f 70  );.  .    /* Cop
1e748 79 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 64  y the b-tree nod
1e749 65 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 70  e content from p
1e74a 61 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 67  age pFrom to pag
1e74b 65 20 70 54 6f 2e 20 2a 2f 0a 20 20 20 20 69 44  e pTo. */.    iD
1e74c 61 74 61 20 3d 20 67 65 74 32 62 79 74 65 28 26  ata = get2byte(&
1e74d 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35  aFrom[iFromHdr+5
1e74e 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  ]);.    memcpy(&
1e74f 61 54 6f 5b 69 44 61 74 61 5d 2c 20 26 61 46 72  aTo[iData], &aFr
1e750 6f 6d 5b 69 44 61 74 61 5d 2c 20 70 42 74 2d 3e  om[iData], pBt->
1e751 75 73 61 62 6c 65 53 69 7a 65 2d 69 44 61 74 61  usableSize-iData
1e752 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61  );.    memcpy(&a
1e753 54 6f 5b 69 54 6f 48 64 72 5d 2c 20 26 61 46 72  To[iToHdr], &aFr
1e754 6f 6d 5b 69 46 72 6f 6d 48 64 72 5d 2c 20 70 46  om[iFromHdr], pF
1e755 72 6f 6d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20  rom->cellOffset 
1e756 2b 20 32 2a 70 46 72 6f 6d 2d 3e 6e 43 65 6c 6c  + 2*pFrom->nCell
1e757 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 69  );.  .    /* Rei
1e758 6e 69 74 69 61 6c 69 7a 65 20 70 61 67 65 20 70  nitialize page p
1e759 54 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 63  To so that the c
1e75a 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 4d  ontents of the M
1e75b 65 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65  emPage structure
1e75c 0a 20 20 20 20 2a 2a 20 6d 61 74 63 68 20 74 68  .    ** match th
1e75d 65 20 6e 65 77 20 64 61 74 61 2e 20 54 68 65 20  e new data. The 
1e75e 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f  initialization o
1e75f 66 20 70 54 6f 20 63 61 6e 20 61 63 74 75 61 6c  f pTo can actual
1e760 6c 79 20 66 61 69 6c 20 75 6e 64 65 72 0a 20 20  ly fail under.  
1e761 20 20 2a 2a 20 66 61 69 72 6c 79 20 6f 62 73 63    ** fairly obsc
1e762 75 72 65 20 63 69 72 63 75 6d 73 74 61 6e 63 65  ure circumstance
1e763 73 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 69  s, even though i
1e764 74 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 69  t is a copy of i
1e765 6e 69 74 69 61 6c 69 7a 65 64 20 0a 20 20 20 20  nitialized .    
1e766 2a 2a 20 70 61 67 65 20 70 46 72 6f 6d 2e 0a 20  ** page pFrom.. 
1e767 20 20 20 2a 2f 0a 20 20 20 20 70 54 6f 2d 3e 69     */.    pTo->i
1e768 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 72  sInit = 0;.    r
1e769 63 20 3d 20 62 74 72 65 65 49 6e 69 74 50 61 67  c = btreeInitPag
1e76a 65 28 70 54 6f 29 3b 0a 20 20 20 20 69 66 28 20  e(pTo);.    if( 
1e76b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1e76c 0a 20 20 20 20 20 20 2a 70 52 43 20 3d 20 72 63  .      *pRC = rc
1e76d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
1e76e 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
1e76f 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75  If this is an au
1e770 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61  to-vacuum databa
1e771 73 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70  se, update the p
1e772 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69  ointer-map entri
1e773 65 73 0a 20 20 20 20 2a 2a 20 66 6f 72 20 61 6e  es.    ** for an
1e774 79 20 62 2d 74 72 65 65 20 6f 72 20 6f 76 65 72  y b-tree or over
1e775 66 6c 6f 77 20 70 61 67 65 73 20 74 68 61 74 20  flow pages that 
1e776 70 54 6f 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73  pTo now contains
1e777 20 74 68 65 20 70 6f 69 6e 74 65 72 73 20 74 6f   the pointers to
1e778 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1e779 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b   ISAUTOVACUUM ){
1e77a 0a 20 20 20 20 20 20 2a 70 52 43 20 3d 20 73 65  .      *pRC = se
1e77b 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 70 54  tChildPtrmaps(pT
1e77c 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  o);.    }.  }.}.
1e77d 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
1e77e 69 6e 65 20 72 65 64 69 73 74 72 69 62 75 74 65  ine redistribute
1e77f 73 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 69  s cells on the i
1e780 50 61 72 65 6e 74 49 64 78 27 74 68 20 63 68 69  ParentIdx'th chi
1e781 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 0a 2a 2a  ld of pParent.**
1e782 20 28 68 65 72 65 61 66 74 65 72 20 22 74 68 65   (hereafter "the
1e783 20 70 61 67 65 22 29 20 61 6e 64 20 75 70 20 74   page") and up t
1e784 6f 20 32 20 73 69 62 6c 69 6e 67 73 20 73 6f 20  o 2 siblings so 
1e785 74 68 61 74 20 61 6c 6c 20 70 61 67 65 73 20 68  that all pages h
1e786 61 76 65 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a  ave about the.**
1e787 20 73 61 6d 65 20 61 6d 6f 75 6e 74 20 6f 66 20   same amount of 
1e788 66 72 65 65 20 73 70 61 63 65 2e 20 55 73 75 61  free space. Usua
1e789 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 73 69 62  lly a single sib
1e78a 6c 69 6e 67 20 6f 6e 20 65 69 74 68 65 72 20 73  ling on either s
1e78b 69 64 65 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61  ide of the.** pa
1e78c 67 65 20 61 72 65 20 75 73 65 64 20 69 6e 20 74  ge are used in t
1e78d 68 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68  he balancing, th
1e78e 6f 75 67 68 20 62 6f 74 68 20 73 69 62 6c 69 6e  ough both siblin
1e78f 67 73 20 6d 69 67 68 74 20 63 6f 6d 65 20 66 72  gs might come fr
1e790 6f 6d 20 6f 6e 65 0a 2a 2a 20 73 69 64 65 20 69  om one.** side i
1e791 66 20 74 68 65 20 70 61 67 65 20 69 73 20 74 68  f the page is th
1e792 65 20 66 69 72 73 74 20 6f 72 20 6c 61 73 74 20  e first or last 
1e793 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61 72  child of its par
1e794 65 6e 74 2e 20 49 66 20 74 68 65 20 70 61 67 65  ent. If the page
1e795 20 0a 2a 2a 20 68 61 73 20 66 65 77 65 72 20 74   .** has fewer t
1e796 68 61 6e 20 32 20 73 69 62 6c 69 6e 67 73 20 28  han 2 siblings (
1e797 73 6f 6d 65 74 68 69 6e 67 20 77 68 69 63 68 20  something which 
1e798 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20  can only happen 
1e799 69 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 69  if the page.** i
1e79a 73 20 61 20 72 6f 6f 74 20 70 61 67 65 20 6f 72  s a root page or
1e79b 20 61 20 63 68 69 6c 64 20 6f 66 20 61 20 72 6f   a child of a ro
1e79c 6f 74 20 70 61 67 65 29 20 74 68 65 6e 20 61 6c  ot page) then al
1e79d 6c 20 61 76 61 69 6c 61 62 6c 65 20 73 69 62 6c  l available sibl
1e79e 69 6e 67 73 0a 2a 2a 20 70 61 72 74 69 63 69 70  ings.** particip
1e79f 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e  ate in the balan
1e7a0 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  cing..**.** The 
1e7a1 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e  number of siblin
1e7a2 67 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 6d  gs of the page m
1e7a3 69 67 68 74 20 62 65 20 69 6e 63 72 65 61 73 65  ight be increase
1e7a4 64 20 6f 72 20 64 65 63 72 65 61 73 65 64 20 62  d or decreased b
1e7a5 79 20 0a 2a 2a 20 6f 6e 65 20 6f 72 20 74 77 6f  y .** one or two
1e7a6 20 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f   in an effort to
1e7a7 20 6b 65 65 70 20 70 61 67 65 73 20 6e 65 61 72   keep pages near
1e7a8 6c 79 20 66 75 6c 6c 20 62 75 74 20 6e 6f 74 20  ly full but not 
1e7a9 6f 76 65 72 20 66 75 6c 6c 2e 20 0a 2a 2a 0a 2a  over full. .**.*
1e7aa 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e  * Note that when
1e7ab 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1e7ac 20 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f 66   called, some of
1e7ad 20 74 68 65 20 63 65 6c 6c 73 20 6f 6e 20 74 68   the cells on th
1e7ae 65 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20  e page.** might 
1e7af 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20  not actually be 
1e7b0 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67  stored in MemPag
1e7b1 65 2e 61 44 61 74 61 5b 5d 2e 20 54 68 69 73 20  e.aData[]. This 
1e7b2 63 61 6e 20 68 61 70 70 65 6e 0a 2a 2a 20 69 66  can happen.** if
1e7b3 20 74 68 65 20 70 61 67 65 20 69 73 20 6f 76 65   the page is ove
1e7b4 72 66 75 6c 6c 2e 20 54 68 69 73 20 72 6f 75 74  rfull. This rout
1e7b5 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68 61 74  ine ensures that
1e7b6 20 61 6c 6c 20 63 65 6c 6c 73 20 61 6c 6c 6f 63   all cells alloc
1e7b7 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 70  ated.** to the p
1e7b8 61 67 65 20 61 6e 64 20 69 74 73 20 73 69 62 6c  age and its sibl
1e7b9 69 6e 67 73 20 66 69 74 20 69 6e 74 6f 20 4d 65  ings fit into Me
1e7ba 6d 50 61 67 65 2e 61 44 61 74 61 5b 5d 20 62 65  mPage.aData[] be
1e7bb 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a  fore returning..
1e7bc 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63 6f 75  **.** In the cou
1e7bd 72 73 65 20 6f 66 20 62 61 6c 61 6e 63 69 6e 67  rse of balancing
1e7be 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 69 74   the page and it
1e7bf 73 20 73 69 62 6c 69 6e 67 73 2c 20 63 65 6c 6c  s siblings, cell
1e7c0 73 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e 73 65  s may be.** inse
1e7c1 72 74 65 64 20 69 6e 74 6f 20 6f 72 20 72 65 6d  rted into or rem
1e7c2 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61  oved from the pa
1e7c3 72 65 6e 74 20 70 61 67 65 20 28 70 50 61 72 65  rent page (pPare
1e7c4 6e 74 29 2e 20 44 6f 69 6e 67 20 73 6f 0a 2a 2a  nt). Doing so.**
1e7c5 20 6d 61 79 20 63 61 75 73 65 20 74 68 65 20 70   may cause the p
1e7c6 61 72 65 6e 74 20 70 61 67 65 20 74 6f 20 62 65  arent page to be
1e7c7 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f 72  come overfull or
1e7c8 20 75 6e 64 65 72 66 75 6c 6c 2e 20 49 66 20 74   underfull. If t
1e7c9 68 69 73 0a 2a 2a 20 68 61 70 70 65 6e 73 2c 20  his.** happens, 
1e7ca 69 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e  it is the respon
1e7cb 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20  sibility of the 
1e7cc 63 61 6c 6c 65 72 20 74 6f 20 69 6e 76 6f 6b 65  caller to invoke
1e7cd 20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20   the correct.** 
1e7ce 62 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74 69 6e  balancing routin
1e7cf 65 20 74 6f 20 66 69 78 20 74 68 69 73 20 70 72  e to fix this pr
1e7d0 6f 62 6c 65 6d 20 28 73 65 65 20 74 68 65 20 62  oblem (see the b
1e7d1 61 6c 61 6e 63 65 28 29 20 72 6f 75 74 69 6e 65  alance() routine
1e7d2 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69  ). .**.** If thi
1e7d3 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20  s routine fails 
1e7d4 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2c 20  for any reason, 
1e7d5 69 74 20 6d 69 67 68 74 20 6c 65 61 76 65 20 74  it might leave t
1e7d6 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69  he database.** i
1e7d7 6e 20 61 20 63 6f 72 72 75 70 74 65 64 20 73 74  n a corrupted st
1e7d8 61 74 65 2e 20 53 6f 20 69 66 20 74 68 69 73 20  ate. So if this 
1e7d9 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 2c 20 74  routine fails, t
1e7da 68 65 20 64 61 74 61 62 61 73 65 20 73 68 6f 75  he database shou
1e7db 6c 64 0a 2a 2a 20 62 65 20 72 6f 6c 6c 65 64 20  ld.** be rolled 
1e7dc 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  back..**.** The 
1e7dd 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 74  third argument t
1e7de 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c  o this function,
1e7df 20 61 4f 76 66 6c 53 70 61 63 65 2c 20 69 73 20   aOvflSpace, is 
1e7e0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 0a 2a  a pointer to a.*
1e7e1 2a 20 62 75 66 66 65 72 20 62 69 67 20 65 6e 6f  * buffer big eno
1e7e2 75 67 68 20 74 6f 20 68 6f 6c 64 20 6f 6e 65 20  ugh to hold one 
1e7e3 70 61 67 65 2e 20 49 66 20 77 68 69 6c 65 20 69  page. If while i
1e7e4 6e 73 65 72 74 69 6e 67 20 63 65 6c 6c 73 20 69  nserting cells i
1e7e5 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 0a 2a  nto the parent.*
1e7e6 2a 20 70 61 67 65 20 28 70 50 61 72 65 6e 74 29  * page (pParent)
1e7e7 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
1e7e8 20 62 65 63 6f 6d 65 73 20 6f 76 65 72 66 75 6c   becomes overful
1e7e9 6c 2c 20 74 68 69 73 20 62 75 66 66 65 72 20 69  l, this buffer i
1e7ea 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f  s.** used to sto
1e7eb 72 65 20 74 68 65 20 70 61 72 65 6e 74 27 73 20  re the parent's 
1e7ec 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 20  overflow cells. 
1e7ed 42 65 63 61 75 73 65 20 74 68 69 73 20 66 75 6e  Because this fun
1e7ee 63 74 69 6f 6e 20 69 6e 73 65 72 74 73 0a 2a 2a  ction inserts.**
1e7ef 20 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20 66 6f   a maximum of fo
1e7f0 75 72 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73  ur divider cells
1e7f1 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74   into the parent
1e7f2 20 70 61 67 65 2c 20 61 6e 64 20 74 68 65 20 6d   page, and the m
1e7f3 61 78 69 6d 75 6d 0a 2a 2a 20 73 69 7a 65 20 6f  aximum.** size o
1e7f4 66 20 61 20 63 65 6c 6c 20 73 74 6f 72 65 64 20  f a cell stored 
1e7f5 77 69 74 68 69 6e 20 61 6e 20 69 6e 74 65 72 6e  within an intern
1e7f6 61 6c 20 6e 6f 64 65 20 69 73 20 61 6c 77 61 79  al node is alway
1e7f7 73 20 6c 65 73 73 20 74 68 61 6e 20 31 2f 34 0a  s less than 1/4.
1e7f8 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2d 73  ** of the page-s
1e7f9 69 7a 65 2c 20 74 68 65 20 61 4f 76 66 6c 53 70  ize, the aOvflSp
1e7fa 61 63 65 5b 5d 20 62 75 66 66 65 72 20 69 73 20  ace[] buffer is 
1e7fb 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65  guaranteed to be
1e7fc 20 6c 61 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 68   large.** enough
1e7fd 20 66 6f 72 20 61 6c 6c 20 6f 76 65 72 66 6c 6f   for all overflo
1e7fe 77 20 63 65 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49  w cells..**.** I
1e7ff 66 20 61 4f 76 66 6c 53 70 61 63 65 20 69 73 20  f aOvflSpace is 
1e800 73 65 74 20 74 6f 20 61 20 6e 75 6c 6c 20 70 6f  set to a null po
1e801 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e 63  inter, this func
1e802 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 0a 2a 2a  tion returns .**
1e803 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a   SQLITE_NOMEM..*
1e804 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 4d  /.#if defined(_M
1e805 53 43 5f 56 45 52 29 20 26 26 20 5f 4d 53 43 5f  SC_VER) && _MSC_
1e806 56 45 52 20 3e 3d 20 31 37 30 30 20 26 26 20 64  VER >= 1700 && d
1e807 65 66 69 6e 65 64 28 5f 4d 5f 41 52 4d 29 0a 23  efined(_M_ARM).#
1e808 70 72 61 67 6d 61 20 6f 70 74 69 6d 69 7a 65 28  pragma optimize(
1e809 22 22 2c 20 6f 66 66 29 0a 23 65 6e 64 69 66 0a  "", off).#endif.
1e80a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e  static int balan
1e80b 63 65 5f 6e 6f 6e 72 6f 6f 74 28 0a 20 20 4d 65  ce_nonroot(.  Me
1e80c 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c 20  mPage *pParent, 
1e80d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e80e 20 50 61 72 65 6e 74 20 70 61 67 65 20 6f 66 20   Parent page of 
1e80f 73 69 62 6c 69 6e 67 73 20 62 65 69 6e 67 20 62  siblings being b
1e810 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 69 6e 74  alanced */.  int
1e811 20 69 50 61 72 65 6e 74 49 64 78 2c 20 20 20 20   iParentIdx,    
1e812 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e813 49 6e 64 65 78 20 6f 66 20 22 74 68 65 20 70 61  Index of "the pa
1e814 67 65 22 20 69 6e 20 70 50 61 72 65 6e 74 20 2a  ge" in pParent *
1e815 2f 0a 20 20 75 38 20 2a 61 4f 76 66 6c 53 70 61  /.  u8 *aOvflSpa
1e816 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ce,             
1e817 20 20 20 20 2f 2a 20 70 61 67 65 2d 73 69 7a 65      /* page-size
1e818 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20   bytes of space 
1e819 66 6f 72 20 70 61 72 65 6e 74 20 6f 76 66 6c 20  for parent ovfl 
1e81a 2a 2f 0a 20 20 69 6e 74 20 69 73 52 6f 6f 74 2c  */.  int isRoot,
1e81b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e81c 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1e81d 70 50 61 72 65 6e 74 20 69 73 20 61 20 72 6f 6f  pParent is a roo
1e81e 74 2d 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  t-page */.  int 
1e81f 62 42 75 6c 6b 20 20 20 20 20 20 20 20 20 20 20  bBulk           
1e820 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1e821 72 75 65 20 69 66 20 74 68 69 73 20 63 61 6c 6c  rue if this call
1e822 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 62 75   is part of a bu
1e823 6c 6b 20 6c 6f 61 64 20 2a 2f 0a 29 7b 0a 20 20  lk load */.){.  
1e824 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20  BtShared *pBt;  
1e825 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e826 54 68 65 20 77 68 6f 6c 65 20 64 61 74 61 62 61  The whole databa
1e827 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c  se */.  int nCel
1e828 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  l = 0;          
1e829 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1e82a 66 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c  f cells in apCel
1e82b 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61  l[] */.  int nMa
1e82c 78 43 65 6c 6c 73 20 3d 20 30 3b 20 20 20 20 20  xCells = 0;     
1e82d 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74        /* Allocat
1e82e 65 64 20 73 69 7a 65 20 6f 66 20 61 70 43 65 6c  ed size of apCel
1e82f 6c 2c 20 73 7a 43 65 6c 6c 2c 20 61 46 72 6f 6d  l, szCell, aFrom
1e830 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65 77 20  . */.  int nNew 
1e831 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
1e832 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1e833 20 70 61 67 65 73 20 69 6e 20 61 70 4e 65 77 5b   pages in apNew[
1e834 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b  ] */.  int nOld;
1e835 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e836 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1e837 20 70 61 67 65 73 20 69 6e 20 61 70 4f 6c 64 5b   pages in apOld[
1e838 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 2c  ] */.  int i, j,
1e839 20 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   k;             
1e83a 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1e83b 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 78  ters */.  int nx
1e83c 44 69 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  Div;            
1e83d 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 64         /* Next d
1e83e 69 76 69 64 65 72 20 73 6c 6f 74 20 69 6e 20 70  ivider slot in p
1e83f 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20  Parent->aCell[] 
1e840 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  */.  int rc = SQ
1e841 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
1e842 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20    /* The return 
1e843 63 6f 64 65 20 2a 2f 0a 20 20 75 31 36 20 6c 65  code */.  u16 le
1e844 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 20 20 20  afCorrection;   
1e845 20 20 20 20 20 20 20 2f 2a 20 34 20 69 66 20 70         /* 4 if p
1e846 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 2e 20  Page is a leaf. 
1e847 20 30 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 69   0 if not */.  i
1e848 6e 74 20 6c 65 61 66 44 61 74 61 3b 20 20 20 20  nt leafData;    
1e849 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1e84a 72 75 65 20 69 66 20 70 50 61 67 65 20 69 73 20  rue if pPage is 
1e84b 61 20 6c 65 61 66 20 6f 66 20 61 20 4c 45 41 46  a leaf of a LEAF
1e84c 44 41 54 41 20 74 72 65 65 20 2a 2f 0a 20 20 69  DATA tree */.  i
1e84d 6e 74 20 75 73 61 62 6c 65 53 70 61 63 65 3b 20  nt usableSpace; 
1e84e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
1e84f 79 74 65 73 20 69 6e 20 70 50 61 67 65 20 62 65  ytes in pPage be
1e850 79 6f 6e 64 20 74 68 65 20 68 65 61 64 65 72 20  yond the header 
1e851 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65 46 6c 61  */.  int pageFla
1e852 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
1e853 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 70 50    /* Value of pP
1e854 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 2a 2f  age->aData[0] */
1e855 0a 20 20 69 6e 74 20 73 75 62 74 6f 74 61 6c 3b  .  int subtotal;
1e856 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e857 2f 2a 20 53 75 62 74 6f 74 61 6c 20 6f 66 20 62  /* Subtotal of b
1e858 79 74 65 73 20 69 6e 20 63 65 6c 6c 73 20 6f 6e  ytes in cells on
1e859 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 20 20 69   one page */.  i
1e85a 6e 74 20 69 53 70 61 63 65 31 20 3d 20 30 3b 20  nt iSpace1 = 0; 
1e85b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
1e85c 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65  irst unused byte
1e85d 20 6f 66 20 61 53 70 61 63 65 31 5b 5d 20 2a 2f   of aSpace1[] */
1e85e 0a 20 20 69 6e 74 20 69 4f 76 66 6c 53 70 61 63  .  int iOvflSpac
1e85f 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
1e860 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 20  /* First unused 
1e861 62 79 74 65 20 6f 66 20 61 4f 76 66 6c 53 70 61  byte of aOvflSpa
1e862 63 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  ce[] */.  int sz
1e863 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20  Scratch;        
1e864 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
1e865 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79  f scratch memory
1e866 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 20   requested */.  
1e867 4d 65 6d 50 61 67 65 20 2a 61 70 4f 6c 64 5b 4e  MemPage *apOld[N
1e868 42 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  B];          /* 
1e869 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20  pPage and up to 
1e86a 74 77 6f 20 73 69 62 6c 69 6e 67 73 20 2a 2f 0a  two siblings */.
1e86b 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 43 6f 70    MemPage *apCop
1e86c 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 2f  y[NB];         /
1e86d 2a 20 50 72 69 76 61 74 65 20 63 6f 70 69 65 73  * Private copies
1e86e 20 6f 66 20 61 70 4f 6c 64 5b 5d 20 70 61 67 65   of apOld[] page
1e86f 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  s */.  MemPage *
1e870 61 70 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20  apNew[NB+2];    
1e871 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e 64      /* pPage and
1e872 20 75 70 20 74 6f 20 4e 42 20 73 69 62 6c 69 6e   up to NB siblin
1e873 67 73 20 61 66 74 65 72 20 62 61 6c 61 6e 63 69  gs after balanci
1e874 6e 67 20 2a 2f 0a 20 20 75 38 20 2a 70 52 69 67  ng */.  u8 *pRig
1e875 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ht;             
1e876 20 20 20 20 20 2f 2a 20 4c 6f 63 61 74 69 6f 6e       /* Location
1e877 20 69 6e 20 70 61 72 65 6e 74 20 6f 66 20 72 69   in parent of ri
1e878 67 68 74 2d 73 69 62 6c 69 6e 67 20 70 6f 69 6e  ght-sibling poin
1e879 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 61 70 44  ter */.  u8 *apD
1e87a 69 76 5b 4e 42 2d 31 5d 3b 20 20 20 20 20 20 20  iv[NB-1];       
1e87b 20 20 20 20 20 20 2f 2a 20 44 69 76 69 64 65 72        /* Divider
1e87c 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e   cells in pParen
1e87d 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 4e 65  t */.  int cntNe
1e87e 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20  w[NB+2];        
1e87f 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20      /* Index in 
1e880 61 43 65 6c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20  aCell[] of cell 
1e881 61 66 74 65 72 20 69 2d 74 68 20 70 61 67 65 20  after i-th page 
1e882 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e 65 77 5b 4e  */.  int szNew[N
1e883 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20  B+2];           
1e884 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 73 69    /* Combined si
1e885 7a 65 20 6f 66 20 63 65 6c 6c 73 20 70 6c 61 63  ze of cells plac
1e886 65 20 6f 6e 20 69 2d 74 68 20 70 61 67 65 20 2a  e on i-th page *
1e887 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 20  /.  u8 **apCell 
1e888 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
1e889 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73 20 62 65   /* All cells be
1e88a 67 69 6e 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a  gin balanced */.
1e88b 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20    u16 *szCell;  
1e88c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1e88d 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20  * Local size of 
1e88e 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43  all cells in apC
1e88f 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61  ell[] */.  u8 *a
1e890 53 70 61 63 65 31 3b 20 20 20 20 20 20 20 20 20  Space1;         
1e891 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
1e892 20 66 6f 72 20 63 6f 70 69 65 73 20 6f 66 20 64   for copies of d
1e893 69 76 69 64 65 72 73 20 63 65 6c 6c 73 20 2a 2f  ividers cells */
1e894 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20  .  Pgno pgno;   
1e895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e896 2f 2a 20 54 65 6d 70 20 76 61 72 20 74 6f 20 73  /* Temp var to s
1e897 74 6f 72 65 20 61 20 70 61 67 65 20 6e 75 6d 62  tore a page numb
1e898 65 72 20 69 6e 20 2a 2f 0a 0a 20 20 70 42 74 20  er in */..  pBt 
1e899 3d 20 70 50 61 72 65 6e 74 2d 3e 70 42 74 3b 0a  = pParent->pBt;.
1e89a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1e89b 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
1e89c 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
1e89d 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
1e89e 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
1e89f 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 20  arent->pDbPage) 
1e8a0 29 3b 0a 0a 23 69 66 20 30 0a 20 20 54 52 41 43  );..#if 0.  TRAC
1e8a1 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 62 65 67  E(("BALANCE: beg
1e8a2 69 6e 20 70 61 67 65 20 25 64 20 63 68 69 6c 64  in page %d child
1e8a3 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 61 67 65   of %d\n", pPage
1e8a4 2d 3e 70 67 6e 6f 2c 20 70 50 61 72 65 6e 74 2d  ->pgno, pParent-
1e8a5 3e 70 67 6e 6f 29 29 3b 0a 23 65 6e 64 69 66 0a  >pgno));.#endif.
1e8a6 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f  .  /* At this po
1e8a7 69 6e 74 20 70 50 61 72 65 6e 74 20 6d 61 79 20  int pParent may 
1e8a8 68 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65  have at most one
1e8a9 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2e 20   overflow cell. 
1e8aa 41 6e 64 20 69 66 0a 20 20 2a 2a 20 74 68 69 73  And if.  ** this
1e8ab 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 69   overflow cell i
1e8ac 73 20 70 72 65 73 65 6e 74 2c 20 69 74 20 6d 75  s present, it mu
1e8ad 73 74 20 62 65 20 74 68 65 20 63 65 6c 6c 20 77  st be the cell w
1e8ae 69 74 68 20 0a 20 20 2a 2a 20 69 6e 64 65 78 20  ith .  ** index 
1e8af 69 50 61 72 65 6e 74 49 64 78 2e 20 54 68 69 73  iParentIdx. This
1e8b0 20 73 63 65 6e 61 72 69 6f 20 63 6f 6d 65 73 20   scenario comes 
1e8b1 61 62 6f 75 74 20 77 68 65 6e 20 74 68 69 73 20  about when this 
1e8b2 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73  function.  ** is
1e8b3 20 63 61 6c 6c 65 64 20 28 69 6e 64 69 72 65 63   called (indirec
1e8b4 74 6c 79 29 20 66 72 6f 6d 20 73 71 6c 69 74 65  tly) from sqlite
1e8b5 33 42 74 72 65 65 44 65 6c 65 74 65 28 29 2e 0a  3BtreeDelete()..
1e8b6 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
1e8b7 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f  Parent->nOverflo
1e8b8 77 3d 3d 30 20 7c 7c 20 70 50 61 72 65 6e 74 2d  w==0 || pParent-
1e8b9 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 29 3b  >nOverflow==1 );
1e8ba 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65  .  assert( pPare
1e8bb 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30  nt->nOverflow==0
1e8bc 20 7c 7c 20 70 50 61 72 65 6e 74 2d 3e 61 69 4f   || pParent->aiO
1e8bd 76 66 6c 5b 30 5d 3d 3d 69 50 61 72 65 6e 74 49  vfl[0]==iParentI
1e8be 64 78 20 29 3b 0a 0a 20 20 69 66 28 20 21 61 4f  dx );..  if( !aO
1e8bf 76 66 6c 53 70 61 63 65 20 29 7b 0a 20 20 20 20  vflSpace ){.    
1e8c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1e8c1 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46  MEM;.  }..  /* F
1e8c2 69 6e 64 20 74 68 65 20 73 69 62 6c 69 6e 67 20  ind the sibling 
1e8c3 70 61 67 65 73 20 74 6f 20 62 61 6c 61 6e 63 65  pages to balance
1e8c4 2e 20 41 6c 73 6f 20 6c 6f 63 61 74 65 20 74 68  . Also locate th
1e8c5 65 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65  e cells in pPare
1e8c6 6e 74 20 0a 20 20 2a 2a 20 74 68 61 74 20 64 69  nt .  ** that di
1e8c7 76 69 64 65 20 74 68 65 20 73 69 62 6c 69 6e 67  vide the sibling
1e8c8 73 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 69 73  s. An attempt is
1e8c9 20 6d 61 64 65 20 74 6f 20 66 69 6e 64 20 4e 4e   made to find NN
1e8ca 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20 0a 20 20   siblings on .  
1e8cb 2a 2a 20 65 69 74 68 65 72 20 73 69 64 65 20 6f  ** either side o
1e8cc 66 20 70 50 61 67 65 2e 20 4d 6f 72 65 20 73 69  f pPage. More si
1e8cd 62 6c 69 6e 67 73 20 61 72 65 20 74 61 6b 65 6e  blings are taken
1e8ce 20 66 72 6f 6d 20 6f 6e 65 20 73 69 64 65 2c 20   from one side, 
1e8cf 68 6f 77 65 76 65 72 2c 20 0a 20 20 2a 2a 20 69  however, .  ** i
1e8d0 66 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65  f there are fewe
1e8d1 72 20 74 68 61 6e 20 4e 4e 20 73 69 62 6c 69 6e  r than NN siblin
1e8d2 67 73 20 6f 6e 20 74 68 65 20 6f 74 68 65 72 20  gs on the other 
1e8d3 73 69 64 65 2e 20 49 66 20 70 50 61 72 65 6e 74  side. If pParent
1e8d4 0a 20 20 2a 2a 20 68 61 73 20 4e 42 20 6f 72 20  .  ** has NB or 
1e8d5 66 65 77 65 72 20 63 68 69 6c 64 72 65 6e 20 74  fewer children t
1e8d6 68 65 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e  hen all children
1e8d7 20 6f 66 20 70 50 61 72 65 6e 74 20 61 72 65 20   of pParent are 
1e8d8 74 61 6b 65 6e 2e 20 20 0a 20 20 2a 2a 0a 20 20  taken.  .  **.  
1e8d9 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20 61 6c 73  ** This loop als
1e8da 6f 20 64 72 6f 70 73 20 74 68 65 20 64 69 76 69  o drops the divi
1e8db 64 65 72 20 63 65 6c 6c 73 20 66 72 6f 6d 20 74  der cells from t
1e8dc 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20  he parent page. 
1e8dd 54 68 69 73 0a 20 20 2a 2a 20 77 61 79 2c 20 74  This.  ** way, t
1e8de 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20  he remainder of 
1e8df 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65  the function doe
1e8e0 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 64 65  s not have to de
1e8e1 61 6c 20 77 69 74 68 20 61 6e 79 0a 20 20 2a 2a  al with any.  **
1e8e2 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20   overflow cells 
1e8e3 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  in the parent pa
1e8e4 67 65 2c 20 73 69 6e 63 65 20 69 66 20 61 6e 79  ge, since if any
1e8e5 20 65 78 69 73 74 65 64 20 74 68 65 79 20 77 69   existed they wi
1e8e6 6c 6c 0a 20 20 2a 2a 20 68 61 76 65 20 61 6c 72  ll.  ** have alr
1e8e7 65 61 64 79 20 62 65 65 6e 20 72 65 6d 6f 76 65  eady been remove
1e8e8 64 2e 0a 20 20 2a 2f 0a 20 20 69 20 3d 20 70 50  d..  */.  i = pP
1e8e9 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77  arent->nOverflow
1e8ea 20 2b 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c   + pParent->nCel
1e8eb 6c 3b 0a 20 20 69 66 28 20 69 3c 32 20 29 7b 0a  l;.  if( i<2 ){.
1e8ec 20 20 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20      nxDiv = 0;. 
1e8ed 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
1e8ee 72 74 28 20 62 42 75 6c 6b 3d 3d 30 20 7c 7c 20  rt( bBulk==0 || 
1e8ef 62 42 75 6c 6b 3d 3d 31 20 29 3b 0a 20 20 20 20  bBulk==1 );.    
1e8f0 69 66 28 20 69 50 61 72 65 6e 74 49 64 78 3d 3d  if( iParentIdx==
1e8f1 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
1e8f2 20 20 20 20 20 0a 20 20 20 20 20 20 6e 78 44 69       .      nxDi
1e8f3 76 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  v = 0;.    }else
1e8f4 20 69 66 28 20 69 50 61 72 65 6e 74 49 64 78 3d   if( iParentIdx=
1e8f5 3d 69 20 29 7b 0a 20 20 20 20 20 20 6e 78 44 69  =i ){.      nxDi
1e8f6 76 20 3d 20 69 2d 32 2b 62 42 75 6c 6b 3b 0a 20  v = i-2+bBulk;. 
1e8f7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1e8f8 61 73 73 65 72 74 28 20 62 42 75 6c 6b 3d 3d 30  assert( bBulk==0
1e8f9 20 29 3b 0a 20 20 20 20 20 20 6e 78 44 69 76 20   );.      nxDiv 
1e8fa 3d 20 69 50 61 72 65 6e 74 49 64 78 2d 31 3b 0a  = iParentIdx-1;.
1e8fb 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 32 2d      }.    i = 2-
1e8fc 62 42 75 6c 6b 3b 0a 20 20 7d 0a 20 20 6e 4f 6c  bBulk;.  }.  nOl
1e8fd 64 20 3d 20 69 2b 31 3b 0a 20 20 69 66 28 20 28  d = i+1;.  if( (
1e8fe 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d  i+nxDiv-pParent-
1e8ff 3e 6e 4f 76 65 72 66 6c 6f 77 29 3d 3d 70 50 61  >nOverflow)==pPa
1e900 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20  rent->nCell ){. 
1e901 20 20 20 70 52 69 67 68 74 20 3d 20 26 70 50 61     pRight = &pPa
1e902 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72  rent->aData[pPar
1e903 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  ent->hdrOffset+8
1e904 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
1e905 70 52 69 67 68 74 20 3d 20 66 69 6e 64 43 65 6c  pRight = findCel
1e906 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44  l(pParent, i+nxD
1e907 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65  iv-pParent->nOve
1e908 72 66 6c 6f 77 29 3b 0a 20 20 7d 0a 20 20 70 67  rflow);.  }.  pg
1e909 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 52  no = get4byte(pR
1e90a 69 67 68 74 29 3b 0a 20 20 77 68 69 6c 65 28 20  ight);.  while( 
1e90b 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 67 65  1 ){.    rc = ge
1e90c 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74  tAndInitPage(pBt
1e90d 2c 20 70 67 6e 6f 2c 20 26 61 70 4f 6c 64 5b 69  , pgno, &apOld[i
1e90e 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  ]);.    if( rc )
1e90f 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 61  {.      memset(a
1e910 70 4f 6c 64 2c 20 30 2c 20 28 69 2b 31 29 2a 73  pOld, 0, (i+1)*s
1e911 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 2a 29 29  izeof(MemPage*))
1e912 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c  ;.      goto bal
1e913 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  ance_cleanup;.  
1e914 20 20 7d 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c    }.    nMaxCell
1e915 73 20 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d  s += 1+apOld[i]-
1e916 3e 6e 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d  >nCell+apOld[i]-
1e917 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20  >nOverflow;.    
1e918 69 66 28 20 28 69 2d 2d 29 3d 3d 30 20 29 20 62  if( (i--)==0 ) b
1e919 72 65 61 6b 3b 0a 0a 20 20 20 20 69 66 28 20 69  reak;..    if( i
1e91a 2b 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e 74 2d  +nxDiv==pParent-
1e91b 3e 61 69 4f 76 66 6c 5b 30 5d 20 26 26 20 70 50  >aiOvfl[0] && pP
1e91c 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77  arent->nOverflow
1e91d 20 29 7b 0a 20 20 20 20 20 20 61 70 44 69 76 5b   ){.      apDiv[
1e91e 69 5d 20 3d 20 70 50 61 72 65 6e 74 2d 3e 61 70  i] = pParent->ap
1e91f 4f 76 66 6c 5b 30 5d 3b 0a 20 20 20 20 20 20 70  Ovfl[0];.      p
1e920 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 61  gno = get4byte(a
1e921 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20  pDiv[i]);.      
1e922 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 65 6c 6c 53  szNew[i] = cellS
1e923 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20  izePtr(pParent, 
1e924 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20  apDiv[i]);.     
1e925 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66   pParent->nOverf
1e926 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  low = 0;.    }el
1e927 73 65 7b 0a 20 20 20 20 20 20 61 70 44 69 76 5b  se{.      apDiv[
1e928 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  i] = findCell(pP
1e929 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70  arent, i+nxDiv-p
1e92a 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f  Parent->nOverflo
1e92b 77 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d  w);.      pgno =
1e92c 20 67 65 74 34 62 79 74 65 28 61 70 44 69 76 5b   get4byte(apDiv[
1e92d 69 5d 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77  i]);.      szNew
1e92e 5b 69 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74  [i] = cellSizePt
1e92f 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44 69 76  r(pParent, apDiv
1e930 5b 69 5d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  [i]);..      /* 
1e931 44 72 6f 70 20 74 68 65 20 63 65 6c 6c 20 66 72  Drop the cell fr
1e932 6f 6d 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  om the parent pa
1e933 67 65 2e 20 61 70 44 69 76 5b 69 5d 20 73 74 69  ge. apDiv[i] sti
1e934 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 0a 20 20 20  ll points to.   
1e935 20 20 20 2a 2a 20 74 68 65 20 63 65 6c 6c 20 77     ** the cell w
1e936 69 74 68 69 6e 20 74 68 65 20 70 61 72 65 6e 74  ithin the parent
1e937 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 69 74  , even though it
1e938 20 68 61 73 20 62 65 65 6e 20 64 72 6f 70 70 65   has been droppe
1e939 64 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73  d..      ** This
1e93a 20 69 73 20 73 61 66 65 20 62 65 63 61 75 73 65   is safe because
1e93b 20 64 72 6f 70 70 69 6e 67 20 61 20 63 65 6c 6c   dropping a cell
1e93c 20 6f 6e 6c 79 20 6f 76 65 72 77 72 69 74 65 73   only overwrites
1e93d 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20 20   the first.     
1e93e 20 2a 2a 20 66 6f 75 72 20 62 79 74 65 73 20 6f   ** four bytes o
1e93f 66 20 69 74 2c 20 61 6e 64 20 74 68 69 73 20 66  f it, and this f
1e940 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  unction does not
1e941 20 6e 65 65 64 20 74 68 65 20 66 69 72 73 74 0a   need the first.
1e942 20 20 20 20 20 20 2a 2a 20 66 6f 75 72 20 62 79        ** four by
1e943 74 65 73 20 6f 66 20 74 68 65 20 64 69 76 69 64  tes of the divid
1e944 65 72 20 63 65 6c 6c 2e 20 53 6f 20 74 68 65 20  er cell. So the 
1e945 70 6f 69 6e 74 65 72 20 69 73 20 73 61 66 65 20  pointer is safe 
1e946 74 6f 20 75 73 65 0a 20 20 20 20 20 20 2a 2a 20  to use.      ** 
1e947 6c 61 74 65 72 20 6f 6e 2e 20 20 0a 20 20 20 20  later on.  .    
1e948 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 42 75    **.      ** Bu
1e949 74 20 6e 6f 74 20 69 66 20 77 65 20 61 72 65 20  t not if we are 
1e94a 69 6e 20 73 65 63 75 72 65 2d 64 65 6c 65 74 65  in secure-delete
1e94b 20 6d 6f 64 65 2e 20 49 6e 20 73 65 63 75 72 65   mode. In secure
1e94c 2d 64 65 6c 65 74 65 20 6d 6f 64 65 2c 0a 20 20  -delete mode,.  
1e94d 20 20 20 20 2a 2a 20 74 68 65 20 64 72 6f 70 43      ** the dropC
1e94e 65 6c 6c 28 29 20 72 6f 75 74 69 6e 65 20 77 69  ell() routine wi
1e94f 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68 65  ll overwrite the
1e950 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 77 69 74   entire cell wit
1e951 68 20 7a 65 72 6f 65 73 2e 0a 20 20 20 20 20 20  h zeroes..      
1e952 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  ** In this case,
1e953 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 63 6f 70   temporarily cop
1e954 79 20 74 68 65 20 63 65 6c 6c 20 69 6e 74 6f 20  y the cell into 
1e955 74 68 65 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d  the aOvflSpace[]
1e956 0a 20 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72  .      ** buffer
1e957 2e 20 49 74 20 77 69 6c 6c 20 62 65 20 63 6f 70  . It will be cop
1e958 69 65 64 20 6f 75 74 20 61 67 61 69 6e 20 61 73  ied out again as
1e959 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 61 53 70   soon as the aSp
1e95a 61 63 65 5b 5d 20 62 75 66 66 65 72 0a 20 20 20  ace[] buffer.   
1e95b 20 20 20 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74     ** is allocat
1e95c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66  ed.  */.      if
1e95d 28 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20  ( pBt->btsFlags 
1e95e 26 20 42 54 53 5f 53 45 43 55 52 45 5f 44 45 4c  & BTS_SECURE_DEL
1e95f 45 54 45 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ETE ){.        i
1e960 6e 74 20 69 4f 66 66 3b 0a 0a 20 20 20 20 20 20  nt iOff;..      
1e961 20 20 69 4f 66 66 20 3d 20 53 51 4c 49 54 45 5f    iOff = SQLITE_
1e962 50 54 52 5f 54 4f 5f 49 4e 54 28 61 70 44 69 76  PTR_TO_INT(apDiv
1e963 5b 69 5d 29 20 2d 20 53 51 4c 49 54 45 5f 50 54  [i]) - SQLITE_PT
1e964 52 5f 54 4f 5f 49 4e 54 28 70 50 61 72 65 6e 74  R_TO_INT(pParent
1e965 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 20 20  ->aData);.      
1e966 20 20 69 66 28 20 28 69 4f 66 66 2b 73 7a 4e 65    if( (iOff+szNe
1e967 77 5b 69 5d 29 3e 28 69 6e 74 29 70 42 74 2d 3e  w[i])>(int)pBt->
1e968 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20  usableSize ){.  
1e969 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1e96a 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1e96b 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73  ;.          mems
1e96c 65 74 28 61 70 4f 6c 64 2c 20 30 2c 20 28 69 2b  et(apOld, 0, (i+
1e96d 31 29 2a 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67  1)*sizeof(MemPag
1e96e 65 2a 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20  e*));.          
1e96f 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
1e970 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d 65  anup;.        }e
1e971 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6d  lse{.          m
1e972 65 6d 63 70 79 28 26 61 4f 76 66 6c 53 70 61 63  emcpy(&aOvflSpac
1e973 65 5b 69 4f 66 66 5d 2c 20 61 70 44 69 76 5b 69  e[iOff], apDiv[i
1e974 5d 2c 20 73 7a 4e 65 77 5b 69 5d 29 3b 0a 20 20  ], szNew[i]);.  
1e975 20 20 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d          apDiv[i]
1e976 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b 61   = &aOvflSpace[a
1e977 70 44 69 76 5b 69 5d 2d 70 50 61 72 65 6e 74 2d  pDiv[i]-pParent-
1e978 3e 61 44 61 74 61 5d 3b 0a 20 20 20 20 20 20 20  >aData];.       
1e979 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
1e97a 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 72 65 6e   dropCell(pParen
1e97b 74 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61 72 65  t, i+nxDiv-pPare
1e97c 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2c 20 73  nt->nOverflow, s
1e97d 7a 4e 65 77 5b 69 5d 2c 20 26 72 63 29 3b 0a 20  zNew[i], &rc);. 
1e97e 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d     }.  }..  /* M
1e97f 61 6b 65 20 6e 4d 61 78 43 65 6c 6c 73 20 61 20  ake nMaxCells a 
1e980 6d 75 6c 74 69 70 6c 65 20 6f 66 20 34 20 69 6e  multiple of 4 in
1e981 20 6f 72 64 65 72 20 74 6f 20 70 72 65 73 65 72   order to preser
1e982 76 65 20 38 2d 62 79 74 65 0a 20 20 2a 2a 20 61  ve 8-byte.  ** a
1e983 6c 69 67 6e 6d 65 6e 74 20 2a 2f 0a 20 20 6e 4d  lignment */.  nM
1e984 61 78 43 65 6c 6c 73 20 3d 20 28 6e 4d 61 78 43  axCells = (nMaxC
1e985 65 6c 6c 73 20 2b 20 33 29 26 7e 33 3b 0a 0a 20  ells + 3)&~3;.. 
1e986 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74   /*.  ** Allocat
1e987 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65 6d 6f  e space for memo
1e988 72 79 20 73 74 72 75 63 74 75 72 65 73 0a 20 20  ry structures.  
1e989 2a 2f 0a 20 20 6b 20 3d 20 70 42 74 2d 3e 70 61  */.  k = pBt->pa
1e98a 67 65 53 69 7a 65 20 2b 20 52 4f 55 4e 44 38 28  geSize + ROUND8(
1e98b 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29  sizeof(MemPage))
1e98c 3b 0a 20 20 73 7a 53 63 72 61 74 63 68 20 3d 0a  ;.  szScratch =.
1e98d 20 20 20 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73         nMaxCells
1e98e 2a 73 69 7a 65 6f 66 28 75 38 2a 29 20 20 20 20  *sizeof(u8*)    
1e98f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e990 20 20 20 2f 2a 20 61 70 43 65 6c 6c 20 2a 2f 0a     /* apCell */.
1e991 20 20 20 20 20 2b 20 6e 4d 61 78 43 65 6c 6c 73       + nMaxCells
1e992 2a 73 69 7a 65 6f 66 28 75 31 36 29 20 20 20 20  *sizeof(u16)    
1e993 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e994 20 20 20 2f 2a 20 73 7a 43 65 6c 6c 20 2a 2f 0a     /* szCell */.
1e995 20 20 20 20 20 2b 20 70 42 74 2d 3e 70 61 67 65       + pBt->page
1e996 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20  Size            
1e997 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e998 20 20 20 2f 2a 20 61 53 70 61 63 65 31 20 2a 2f     /* aSpace1 */
1e999 0a 20 20 20 20 20 2b 20 6b 2a 6e 4f 6c 64 3b 20  .     + k*nOld; 
1e99a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e99b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e99c 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 70 69      /* Page copi
1e99d 65 73 20 28 61 70 43 6f 70 79 29 20 2a 2f 0a 20  es (apCopy) */. 
1e99e 20 61 70 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65   apCell = sqlite
1e99f 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 20  3ScratchMalloc( 
1e9a0 73 7a 53 63 72 61 74 63 68 20 29 3b 20 0a 20 20  szScratch ); .  
1e9a1 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20 29 7b  if( apCell==0 ){
1e9a2 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1e9a3 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f  _NOMEM;.    goto
1e9a4 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70   balance_cleanup
1e9a5 3b 0a 20 20 7d 0a 20 20 73 7a 43 65 6c 6c 20 3d  ;.  }.  szCell =
1e9a6 20 28 75 31 36 2a 29 26 61 70 43 65 6c 6c 5b 6e   (u16*)&apCell[n
1e9a7 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 20 61 53 70  MaxCells];.  aSp
1e9a8 61 63 65 31 20 3d 20 28 75 38 2a 29 26 73 7a 43  ace1 = (u8*)&szC
1e9a9 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a  ell[nMaxCells];.
1e9aa 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f    assert( EIGHT_
1e9ab 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 61  BYTE_ALIGNMENT(a
1e9ac 53 70 61 63 65 31 29 20 29 3b 0a 0a 20 20 2f 2a  Space1) );..  /*
1e9ad 0a 20 20 2a 2a 20 4c 6f 61 64 20 70 6f 69 6e 74  .  ** Load point
1e9ae 65 72 73 20 74 6f 20 61 6c 6c 20 63 65 6c 6c 73  ers to all cells
1e9af 20 6f 6e 20 73 69 62 6c 69 6e 67 20 70 61 67 65   on sibling page
1e9b0 73 20 61 6e 64 20 74 68 65 20 64 69 76 69 64 65  s and the divide
1e9b1 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74  r cells.  ** int
1e9b2 6f 20 74 68 65 20 6c 6f 63 61 6c 20 61 70 43 65  o the local apCe
1e9b3 6c 6c 5b 5d 20 61 72 72 61 79 2e 20 20 4d 61 6b  ll[] array.  Mak
1e9b4 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20  e copies of the 
1e9b5 64 69 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 20  divider cells.  
1e9b6 2a 2a 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62  ** into space ob
1e9b7 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 53 70 61  tained from aSpa
1e9b8 63 65 31 5b 5d 20 61 6e 64 20 72 65 6d 6f 76 65  ce1[] and remove
1e9b9 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c   the divider cel
1e9ba 6c 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50 61  ls.  ** from pPa
1e9bb 72 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  rent..  **.  ** 
1e9bc 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20  If the siblings 
1e9bd 61 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 67 65  are on leaf page
1e9be 73 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c  s, then the chil
1e9bf 64 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 74 68  d pointers of th
1e9c0 65 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 20 63  e.  ** divider c
1e9c1 65 6c 6c 73 20 61 72 65 20 73 74 72 69 70 70 65  ells are strippe
1e9c2 64 20 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c 73  d from the cells
1e9c3 20 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65   before they are
1e9c4 20 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e 74   copied.  ** int
1e9c5 6f 20 61 53 70 61 63 65 31 5b 5d 2e 20 20 49 6e  o aSpace1[].  In
1e9c6 20 74 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 63   this way, all c
1e9c7 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d  ells in apCell[]
1e9c8 20 61 72 65 20 77 69 74 68 6f 75 74 0a 20 20 2a   are without.  *
1e9c9 2a 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73  * child pointers
1e9ca 2e 20 20 49 66 20 73 69 62 6c 69 6e 67 73 20 61  .  If siblings a
1e9cb 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74  re not leaves, t
1e9cc 68 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a  hen all cell in.
1e9cd 20 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e    ** apCell[] in
1e9ce 63 6c 75 64 65 20 63 68 69 6c 64 20 70 6f 69 6e  clude child poin
1e9cf 74 65 72 73 2e 20 20 45 69 74 68 65 72 20 77 61  ters.  Either wa
1e9d0 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20  y, all cells in 
1e9d1 61 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72  apCell[].  ** ar
1e9d2 65 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20  e alike..  **.  
1e9d3 2a 2a 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f  ** leafCorrectio
1e9d4 6e 3a 20 20 34 20 69 66 20 70 50 61 67 65 20 69  n:  4 if pPage i
1e9d5 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20  s a leaf.  0 if 
1e9d6 70 50 61 67 65 20 69 73 20 6e 6f 74 20 61 20 6c  pPage is not a l
1e9d7 65 61 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20  eaf..  **       
1e9d8 6c 65 61 66 44 61 74 61 3a 20 20 31 20 69 66 20  leafData:  1 if 
1e9d9 70 50 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b  pPage holds key+
1e9da 64 61 74 61 20 61 6e 64 20 70 50 61 72 65 6e 74  data and pParent
1e9db 20 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73   holds only keys
1e9dc 2e 0a 20 20 2a 2f 0a 20 20 6c 65 61 66 43 6f 72  ..  */.  leafCor
1e9dd 72 65 63 74 69 6f 6e 20 3d 20 61 70 4f 6c 64 5b  rection = apOld[
1e9de 30 5d 2d 3e 6c 65 61 66 2a 34 3b 0a 20 20 6c 65  0]->leaf*4;.  le
1e9df 61 66 44 61 74 61 20 3d 20 61 70 4f 6c 64 5b 30  afData = apOld[0
1e9e0 5d 2d 3e 68 61 73 44 61 74 61 3b 0a 20 20 66 6f  ]->hasData;.  fo
1e9e1 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69  r(i=0; i<nOld; i
1e9e2 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 69 6d  ++){.    int lim
1e9e3 69 74 3b 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20  it;.    .    /* 
1e9e4 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e 79  Before doing any
1e9e5 74 68 69 6e 67 20 65 6c 73 65 2c 20 74 61 6b 65  thing else, take
1e9e6 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69   a copy of the i
1e9e7 27 74 68 20 6f 72 69 67 69 6e 61 6c 20 73 69 62  'th original sib
1e9e8 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 54 68 65 20  ling.    ** The 
1e9e9 72 65 73 74 20 6f 66 20 74 68 69 73 20 66 75 6e  rest of this fun
1e9ea 63 74 69 6f 6e 20 77 69 6c 6c 20 75 73 65 20 64  ction will use d
1e9eb 61 74 61 20 66 72 6f 6d 20 74 68 65 20 63 6f 70  ata from the cop
1e9ec 69 65 73 20 72 61 74 68 65 72 0a 20 20 20 20 2a  ies rather.    *
1e9ed 2a 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69  * that the origi
1e9ee 6e 61 6c 20 70 61 67 65 73 20 73 69 6e 63 65 20  nal pages since 
1e9ef 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67  the original pag
1e9f0 65 73 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68  es will be in th
1e9f1 65 0a 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73  e.    ** process
1e9f2 20 6f 66 20 62 65 69 6e 67 20 6f 76 65 72 77 72   of being overwr
1e9f3 69 74 74 65 6e 2e 20 20 2a 2f 0a 20 20 20 20 4d  itten.  */.    M
1e9f4 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61  emPage *pOld = a
1e9f5 70 43 6f 70 79 5b 69 5d 20 3d 20 28 4d 65 6d 50  pCopy[i] = (MemP
1e9f6 61 67 65 2a 29 26 61 53 70 61 63 65 31 5b 70 42  age*)&aSpace1[pB
1e9f7 74 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 6b 2a  t->pageSize + k*
1e9f8 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70  i];.    memcpy(p
1e9f9 4f 6c 64 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73  Old, apOld[i], s
1e9fa 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b  izeof(MemPage));
1e9fb 0a 20 20 20 20 70 4f 6c 64 2d 3e 61 44 61 74 61  .    pOld->aData
1e9fc 20 3d 20 28 76 6f 69 64 2a 29 26 70 4f 6c 64 5b   = (void*)&pOld[
1e9fd 31 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70  1];.    memcpy(p
1e9fe 4f 6c 64 2d 3e 61 44 61 74 61 2c 20 61 70 4f 6c  Old->aData, apOl
1e9ff 64 5b 69 5d 2d 3e 61 44 61 74 61 2c 20 70 42 74  d[i]->aData, pBt
1ea00 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20  ->pageSize);..  
1ea01 20 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e    limit = pOld->
1ea02 6e 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65  nCell+pOld->nOve
1ea03 72 66 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20 70  rflow;.    if( p
1ea04 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30  Old->nOverflow>0
1ea05 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d   ){.      for(j=
1ea06 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29  0; j<limit; j++)
1ea07 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
1ea08 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c  ( nCell<nMaxCell
1ea09 73 20 29 3b 0a 20 20 20 20 20 20 20 20 61 70 43  s );.        apC
1ea0a 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e  ell[nCell] = fin
1ea0b 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f  dOverflowCell(pO
1ea0c 6c 64 2c 20 6a 29 3b 0a 20 20 20 20 20 20 20 20  ld, j);.        
1ea0d 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20  szCell[nCell] = 
1ea0e 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4f 6c 64  cellSizePtr(pOld
1ea0f 2c 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29  , apCell[nCell])
1ea10 3b 0a 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 2b  ;.        nCell+
1ea11 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  +;.      }.    }
1ea12 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 38 20 2a  else{.      u8 *
1ea13 61 44 61 74 61 20 3d 20 70 4f 6c 64 2d 3e 61 44  aData = pOld->aD
1ea14 61 74 61 3b 0a 20 20 20 20 20 20 75 31 36 20 6d  ata;.      u16 m
1ea15 61 73 6b 50 61 67 65 20 3d 20 70 4f 6c 64 2d 3e  askPage = pOld->
1ea16 6d 61 73 6b 50 61 67 65 3b 0a 20 20 20 20 20 20  maskPage;.      
1ea17 75 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d  u16 cellOffset =
1ea18 20 70 4f 6c 64 2d 3e 63 65 6c 6c 4f 66 66 73 65   pOld->cellOffse
1ea19 74 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  t;.      for(j=0
1ea1a 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 7b  ; j<limit; j++){
1ea1b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1ea1c 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73   nCell<nMaxCells
1ea1d 20 29 3b 0a 20 20 20 20 20 20 20 20 61 70 43 65   );.        apCe
1ea1e 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64  ll[nCell] = find
1ea1f 43 65 6c 6c 76 32 28 61 44 61 74 61 2c 20 6d 61  Cellv2(aData, ma
1ea20 73 6b 50 61 67 65 2c 20 63 65 6c 6c 4f 66 66 73  skPage, cellOffs
1ea21 65 74 2c 20 6a 29 3b 0a 20 20 20 20 20 20 20 20  et, j);.        
1ea22 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20  szCell[nCell] = 
1ea23 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4f 6c 64  cellSizePtr(pOld
1ea24 2c 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29  , apCell[nCell])
1ea25 3b 0a 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 2b  ;.        nCell+
1ea26 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  +;.      }.    }
1ea27 20 20 20 20 20 20 20 0a 20 20 20 20 69 66 28 20         .    if( 
1ea28 69 3c 6e 4f 6c 64 2d 31 20 26 26 20 21 6c 65 61  i<nOld-1 && !lea
1ea29 66 44 61 74 61 29 7b 0a 20 20 20 20 20 20 75 31  fData){.      u1
1ea2a 36 20 73 7a 20 3d 20 28 75 31 36 29 73 7a 4e 65  6 sz = (u16)szNe
1ea2b 77 5b 69 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a  w[i];.      u8 *
1ea2c 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 61 73 73  pTemp;.      ass
1ea2d 65 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43  ert( nCell<nMaxC
1ea2e 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 73 7a  ells );.      sz
1ea2f 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a  Cell[nCell] = sz
1ea30 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20  ;.      pTemp = 
1ea31 26 61 53 70 61 63 65 31 5b 69 53 70 61 63 65 31  &aSpace1[iSpace1
1ea32 5d 3b 0a 20 20 20 20 20 20 69 53 70 61 63 65 31  ];.      iSpace1
1ea33 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 61 73   += sz;.      as
1ea34 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 6d  sert( sz<=pBt->m
1ea35 61 78 4c 6f 63 61 6c 2b 32 33 20 29 3b 0a 20 20  axLocal+23 );.  
1ea36 20 20 20 20 61 73 73 65 72 74 28 20 69 53 70 61      assert( iSpa
1ea37 63 65 31 20 3c 3d 20 28 69 6e 74 29 70 42 74 2d  ce1 <= (int)pBt-
1ea38 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20  >pageSize );.   
1ea39 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 2c     memcpy(pTemp,
1ea3a 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29 3b 0a   apDiv[i], sz);.
1ea3b 20 20 20 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65        apCell[nCe
1ea3c 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b 6c 65 61 66  ll] = pTemp+leaf
1ea3d 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20  Correction;.    
1ea3e 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f    assert( leafCo
1ea3f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 6c  rrection==0 || l
1ea40 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34  eafCorrection==4
1ea41 20 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c   );.      szCell
1ea42 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a 43 65 6c 6c  [nCell] = szCell
1ea43 5b 6e 43 65 6c 6c 5d 20 2d 20 6c 65 61 66 43 6f  [nCell] - leafCo
1ea44 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20  rrection;.      
1ea45 69 66 28 20 21 70 4f 6c 64 2d 3e 6c 65 61 66 20  if( !pOld->leaf 
1ea46 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
1ea47 74 28 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f  t( leafCorrectio
1ea48 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  n==0 );.        
1ea49 61 73 73 65 72 74 28 20 70 4f 6c 64 2d 3e 68 64  assert( pOld->hd
1ea4a 72 4f 66 66 73 65 74 3d 3d 30 20 29 3b 0a 20 20  rOffset==0 );.  
1ea4b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67        /* The rig
1ea4c 68 74 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68  ht pointer of th
1ea4d 65 20 63 68 69 6c 64 20 70 61 67 65 20 70 4f 6c  e child page pOl
1ea4e 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 6c 65  d becomes the le
1ea4f 66 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f  ft.        ** po
1ea50 69 6e 74 65 72 20 6f 66 20 74 68 65 20 64 69 76  inter of the div
1ea51 69 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 20 20 20  ider cell */.   
1ea52 20 20 20 20 20 6d 65 6d 63 70 79 28 61 70 43 65       memcpy(apCe
1ea53 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64  ll[nCell], &pOld
1ea54 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 34 29 3b 0a  ->aData[8], 4);.
1ea55 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1ea56 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61       assert( lea
1ea57 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29  fCorrection==4 )
1ea58 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 7a  ;.        if( sz
1ea59 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b  Cell[nCell]<4 ){
1ea5a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f  .          /* Do
1ea5b 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 6e 79 20 63   not allow any c
1ea5c 65 6c 6c 73 20 73 6d 61 6c 6c 65 72 20 74 68 61  ells smaller tha
1ea5d 6e 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20  n 4 bytes. */.  
1ea5e 20 20 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e          szCell[n
1ea5f 43 65 6c 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 20  Cell] = 4;.     
1ea60 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1ea61 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20     nCell++;.    
1ea62 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  }.  }..  /*.  **
1ea63 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20   Figure out the 
1ea64 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
1ea65 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 61  needed to hold a
1ea66 6c 6c 20 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a  ll nCell cells..
1ea67 20 20 2a 2a 20 53 74 6f 72 65 20 74 68 69 73 20    ** Store this 
1ea68 6e 75 6d 62 65 72 20 69 6e 20 22 6b 22 2e 20 20  number in "k".  
1ea69 41 6c 73 6f 20 63 6f 6d 70 75 74 65 20 73 7a 4e  Also compute szN
1ea6a 65 77 5b 5d 20 77 68 69 63 68 20 69 73 20 74 68  ew[] which is th
1ea6b 65 20 74 6f 74 61 6c 0a 20 20 2a 2a 20 73 69 7a  e total.  ** siz
1ea6c 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 6f  e of all cells o
1ea6d 6e 20 74 68 65 20 69 2d 74 68 20 70 61 67 65 20  n the i-th page 
1ea6e 61 6e 64 20 63 6e 74 4e 65 77 5b 5d 20 77 68 69  and cntNew[] whi
1ea6f 63 68 20 69 73 20 74 68 65 20 69 6e 64 65 78 0a  ch is the index.
1ea70 20 20 2a 2a 20 69 6e 20 61 70 43 65 6c 6c 5b 5d    ** in apCell[]
1ea71 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61   of the cell tha
1ea72 74 20 64 69 76 69 64 65 73 20 70 61 67 65 20 69  t divides page i
1ea73 20 66 72 6f 6d 20 70 61 67 65 20 69 2b 31 2e 20   from page i+1. 
1ea74 20 0a 20 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d   .  ** cntNew[k]
1ea75 20 73 68 6f 75 6c 64 20 65 71 75 61 6c 20 6e 43   should equal nC
1ea76 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56  ell..  **.  ** V
1ea77 61 6c 75 65 73 20 63 6f 6d 70 75 74 65 64 20 62  alues computed b
1ea78 79 20 74 68 69 73 20 62 6c 6f 63 6b 3a 0a 20 20  y this block:.  
1ea79 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  **.  **         
1ea7a 20 20 6b 3a 20 54 68 65 20 74 6f 74 61 6c 20 6e    k: The total n
1ea7b 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e 67  umber of sibling
1ea7c 20 70 61 67 65 73 0a 20 20 2a 2a 20 20 20 20 73   pages.  **    s
1ea7d 7a 4e 65 77 5b 69 5d 3a 20 53 70 61 63 65 64 20  zNew[i]: Spaced 
1ea7e 75 73 65 64 20 6f 6e 20 74 68 65 20 69 2d 74 68  used on the i-th
1ea7f 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20   sibling page.. 
1ea80 20 2a 2a 20 20 20 63 6e 74 4e 65 77 5b 69 5d 3a   **   cntNew[i]:
1ea81 20 49 6e 64 65 78 20 69 6e 20 61 70 43 65 6c 6c   Index in apCell
1ea82 5b 5d 20 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20  [] and szCell[] 
1ea83 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 63 65  for the first ce
1ea84 6c 6c 20 74 6f 0a 20 20 2a 2a 20 20 20 20 20 20  ll to.  **      
1ea85 20 20 20 20 20 20 20 20 74 68 65 20 72 69 67 68          the righ
1ea86 74 20 6f 66 20 74 68 65 20 69 2d 74 68 20 73 69  t of the i-th si
1ea87 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a  bling page..  **
1ea88 20 75 73 61 62 6c 65 53 70 61 63 65 3a 20 4e 75   usableSpace: Nu
1ea89 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
1ea8a 20 73 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65   space available
1ea8b 20 6f 6e 20 65 61 63 68 20 73 69 62 6c 69 6e 67   on each sibling
1ea8c 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75  ..  ** .  */.  u
1ea8d 73 61 62 6c 65 53 70 61 63 65 20 3d 20 70 42 74  sableSpace = pBt
1ea8e 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 31  ->usableSize - 1
1ea8f 32 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69  2 + leafCorrecti
1ea90 6f 6e 3b 0a 20 20 66 6f 72 28 73 75 62 74 6f 74  on;.  for(subtot
1ea91 61 6c 3d 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c  al=k=i=0; i<nCel
1ea92 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73  l; i++){.    ass
1ea93 65 72 74 28 20 69 3c 6e 4d 61 78 43 65 6c 6c 73  ert( i<nMaxCells
1ea94 20 29 3b 0a 20 20 20 20 73 75 62 74 6f 74 61 6c   );.    subtotal
1ea95 20 2b 3d 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20   += szCell[i] + 
1ea96 32 3b 0a 20 20 20 20 69 66 28 20 73 75 62 74 6f  2;.    if( subto
1ea97 74 61 6c 20 3e 20 75 73 61 62 6c 65 53 70 61 63  tal > usableSpac
1ea98 65 20 29 7b 0a 20 20 20 20 20 20 73 7a 4e 65 77  e ){.      szNew
1ea99 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c 20 2d  [k] = subtotal -
1ea9a 20 73 7a 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20   szCell[i];.    
1ea9b 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b    cntNew[k] = i;
1ea9c 0a 20 20 20 20 20 20 69 66 28 20 6c 65 61 66 44  .      if( leafD
1ea9d 61 74 61 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20  ata ){ i--; }.  
1ea9e 20 20 20 20 73 75 62 74 6f 74 61 6c 20 3d 20 30      subtotal = 0
1ea9f 3b 0a 20 20 20 20 20 20 6b 2b 2b 3b 0a 20 20 20  ;.      k++;.   
1eaa0 20 20 20 69 66 28 20 6b 3e 4e 42 2b 31 20 29 7b     if( k>NB+1 ){
1eaa1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
1eaa2 52 55 50 54 5f 42 4b 50 54 3b 20 67 6f 74 6f 20  RUPT_BKPT; goto 
1eaa3 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b  balance_cleanup;
1eaa4 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73   }.    }.  }.  s
1eaa5 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74  zNew[k] = subtot
1eaa6 61 6c 3b 0a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20  al;.  cntNew[k] 
1eaa7 3d 20 6e 43 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a  = nCell;.  k++;.
1eaa8 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 70  .  /*.  ** The p
1eaa9 61 63 6b 69 6e 67 20 63 6f 6d 70 75 74 65 64 20  acking computed 
1eaaa 62 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  by the previous 
1eaab 62 6c 6f 63 6b 20 69 73 20 62 69 61 73 65 64 20  block is biased 
1eaac 74 6f 77 61 72 64 20 74 68 65 20 73 69 62 6c 69  toward the sibli
1eaad 6e 67 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20  ngs.  ** on the 
1eaae 6c 65 66 74 20 73 69 64 65 2e 20 20 54 68 65 20  left side.  The 
1eaaf 6c 65 66 74 20 73 69 62 6c 69 6e 67 73 20 61 72  left siblings ar
1eab0 65 20 61 6c 77 61 79 73 20 6e 65 61 72 6c 79 20  e always nearly 
1eab1 66 75 6c 6c 2c 20 77 68 69 6c 65 20 74 68 65 0a  full, while the.
1eab2 20 20 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20    ** right-most 
1eab3 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65  sibling might be
1eab4 20 6e 65 61 72 6c 79 20 65 6d 70 74 79 2e 20 20   nearly empty.  
1eab5 54 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f  This block of co
1eab6 64 65 20 61 74 74 65 6d 70 74 73 0a 20 20 2a 2a  de attempts.  **
1eab7 20 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20 70   to adjust the p
1eab8 61 63 6b 69 6e 67 20 6f 66 20 73 69 62 6c 69 6e  acking of siblin
1eab9 67 73 20 74 6f 20 67 65 74 20 61 20 62 65 74 74  gs to get a bett
1eaba 65 72 20 62 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a  er balance..  **
1eabb 0a 20 20 2a 2a 20 54 68 69 73 20 61 64 6a 75 73  .  ** This adjus
1eabc 74 6d 65 6e 74 20 69 73 20 6d 6f 72 65 20 74 68  tment is more th
1eabd 61 6e 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69  an an optimizati
1eabe 6f 6e 2e 20 20 54 68 65 20 70 61 63 6b 69 6e 67  on.  The packing
1eabf 20 61 62 6f 76 65 20 6d 69 67 68 74 0a 20 20 2a   above might.  *
1eac0 2a 20 62 65 20 73 6f 20 6f 75 74 20 6f 66 20 62  * be so out of b
1eac1 61 6c 61 6e 63 65 20 61 73 20 74 6f 20 62 65 20  alance as to be 
1eac2 69 6c 6c 65 67 61 6c 2e 20 20 46 6f 72 20 65 78  illegal.  For ex
1eac3 61 6d 70 6c 65 2c 20 74 68 65 20 72 69 67 68 74  ample, the right
1eac4 2d 6d 6f 73 74 0a 20 20 2a 2a 20 73 69 62 6c 69  -most.  ** sibli
1eac5 6e 67 20 6d 69 67 68 74 20 62 65 20 63 6f 6d 70  ng might be comp
1eac6 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e 20 20 54  letely empty.  T
1eac7 68 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69  his adjustment i
1eac8 73 20 6e 6f 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a  s not optional..
1eac9 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6b 2d 31    */.  for(i=k-1
1eaca 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20  ; i>0; i--){.   
1eacb 20 69 6e 74 20 73 7a 52 69 67 68 74 20 3d 20 73   int szRight = s
1eacc 7a 4e 65 77 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a  zNew[i];  /* Siz
1eacd 65 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20  e of sibling on 
1eace 74 68 65 20 72 69 67 68 74 20 2a 2f 0a 20 20 20  the right */.   
1eacf 20 69 6e 74 20 73 7a 4c 65 66 74 20 3d 20 73 7a   int szLeft = sz
1ead0 4e 65 77 5b 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a  New[i-1]; /* Siz
1ead1 65 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20  e of sibling on 
1ead2 74 68 65 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20  the left */.    
1ead3 69 6e 74 20 72 3b 20 20 20 20 20 20 20 20 20 20  int r;          
1ead4 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
1ead5 72 69 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20  right-most cell 
1ead6 69 6e 20 6c 65 66 74 20 73 69 62 6c 69 6e 67 20  in left sibling 
1ead7 2a 2f 0a 20 20 20 20 69 6e 74 20 64 3b 20 20 20  */.    int d;   
1ead8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1ead9 64 65 78 20 6f 66 20 66 69 72 73 74 20 63 65 6c  dex of first cel
1eada 6c 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66  l to the left of
1eadb 20 72 69 67 68 74 20 73 69 62 6c 69 6e 67 20 2a   right sibling *
1eadc 2f 0a 0a 20 20 20 20 72 20 3d 20 63 6e 74 4e 65  /..    r = cntNe
1eadd 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20  w[i-1] - 1;.    
1eade 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66  d = r + 1 - leaf
1eadf 44 61 74 61 3b 0a 20 20 20 20 61 73 73 65 72 74  Data;.    assert
1eae0 28 20 64 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b  ( d<nMaxCells );
1eae1 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 3c 6e  .    assert( r<n
1eae2 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20  MaxCells );.    
1eae3 77 68 69 6c 65 28 20 73 7a 52 69 67 68 74 3d 3d  while( szRight==
1eae4 30 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 21 62  0 .       || (!b
1eae5 42 75 6c 6b 20 26 26 20 73 7a 52 69 67 68 74 2b  Bulk && szRight+
1eae6 73 7a 43 65 6c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c  szCell[d]+2<=szL
1eae7 65 66 74 2d 28 73 7a 43 65 6c 6c 5b 72 5d 2b 32  eft-(szCell[r]+2
1eae8 29 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  )) .    ){.     
1eae9 20 73 7a 52 69 67 68 74 20 2b 3d 20 73 7a 43 65   szRight += szCe
1eaea 6c 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20  ll[d] + 2;.     
1eaeb 20 73 7a 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c   szLeft -= szCel
1eaec 6c 5b 72 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20  l[r] + 2;.      
1eaed 63 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20  cntNew[i-1]--;. 
1eaee 20 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b       r = cntNew[
1eaef 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20  i-1] - 1;.      
1eaf0 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66  d = r + 1 - leaf
1eaf1 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Data;.    }.    
1eaf2 73 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67  szNew[i] = szRig
1eaf3 68 74 3b 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d  ht;.    szNew[i-
1eaf4 31 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d  1] = szLeft;.  }
1eaf5 0a 0a 20 20 2f 2a 20 45 69 74 68 65 72 20 77 65  ..  /* Either we
1eaf6 20 66 6f 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f   found one or mo
1eaf7 72 65 20 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77  re cells (cntnew
1eaf8 5b 30 5d 29 3e 30 29 20 6f 72 20 70 50 61 67 65  [0])>0) or pPage
1eaf9 20 69 73 0a 20 20 2a 2a 20 61 20 76 69 72 74 75   is.  ** a virtu
1eafa 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 41  al root page.  A
1eafb 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61   virtual root pa
1eafc 67 65 20 69 73 20 77 68 65 6e 20 74 68 65 20 72  ge is when the r
1eafd 65 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a 20 70 61  eal root.  ** pa
1eafe 67 65 20 69 73 20 70 61 67 65 20 31 20 61 6e 64  ge is page 1 and
1eaff 20 77 65 20 61 72 65 20 74 68 65 20 6f 6e 6c 79   we are the only
1eb00 20 63 68 69 6c 64 20 6f 66 20 74 68 61 74 20 70   child of that p
1eb01 61 67 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 55  age..  **.  ** U
1eb02 50 44 41 54 45 3a 20 20 54 68 65 20 61 73 73 65  PDATE:  The asse
1eb03 72 74 28 29 20 62 65 6c 6f 77 20 69 73 20 6e 6f  rt() below is no
1eb04 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 74 72  t necessarily tr
1eb05 75 65 20 69 66 20 74 68 65 20 64 61 74 61 62 61  ue if the databa
1eb06 73 65 0a 20 20 2a 2a 20 66 69 6c 65 20 69 73 20  se.  ** file is 
1eb07 63 6f 72 72 75 70 74 2e 20 20 54 68 65 20 63 6f  corrupt.  The co
1eb08 72 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20 62 65  rruption will be
1eb09 20 64 65 74 65 63 74 65 64 20 61 6e 64 20 72 65   detected and re
1eb0a 70 6f 72 74 65 64 20 6c 61 74 65 72 0a 20 20 2a  ported later.  *
1eb0b 2a 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 64  * in this proced
1eb0c 75 72 65 20 73 6f 20 74 68 65 72 65 20 69 73 20  ure so there is 
1eb0d 6e 6f 20 6e 65 65 64 20 74 6f 20 61 63 74 20 75  no need to act u
1eb0e 70 6f 6e 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f  pon it now..  */
1eb0f 0a 23 69 66 20 30 0a 20 20 61 73 73 65 72 74 28  .#if 0.  assert(
1eb10 20 63 6e 74 4e 65 77 5b 30 5d 3e 30 20 7c 7c 20   cntNew[0]>0 || 
1eb11 28 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 3d 3d  (pParent->pgno==
1eb12 31 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43  1 && pParent->nC
1eb13 65 6c 6c 3d 3d 30 29 20 29 3b 0a 23 65 6e 64 69  ell==0) );.#endi
1eb14 66 0a 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c  f..  TRACE(("BAL
1eb15 41 4e 43 45 3a 20 6f 6c 64 3a 20 25 64 20 25 64  ANCE: old: %d %d
1eb16 20 25 64 20 20 22 2c 0a 20 20 20 20 61 70 4f 6c   %d  ",.    apOl
1eb17 64 5b 30 5d 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20  d[0]->pgno, .   
1eb18 20 6e 4f 6c 64 3e 3d 32 20 3f 20 61 70 4f 6c 64   nOld>=2 ? apOld
1eb19 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 0a 20  [1]->pgno : 0,. 
1eb1a 20 20 20 6e 4f 6c 64 3e 3d 33 20 3f 20 61 70 4f     nOld>=3 ? apO
1eb1b 6c 64 5b 32 5d 2d 3e 70 67 6e 6f 20 3a 20 30 0a  ld[2]->pgno : 0.
1eb1c 20 20 29 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a    ));..  /*.  **
1eb1d 20 41 6c 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20   Allocate k new 
1eb1e 70 61 67 65 73 2e 20 20 52 65 75 73 65 20 6f 6c  pages.  Reuse ol
1eb1f 64 20 70 61 67 65 73 20 77 68 65 72 65 20 70 6f  d pages where po
1eb20 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69  ssible..  */.  i
1eb21 66 28 20 61 70 4f 6c 64 5b 30 5d 2d 3e 70 67 6e  f( apOld[0]->pgn
1eb22 6f 3c 3d 31 20 29 7b 0a 20 20 20 20 72 63 20 3d  o<=1 ){.    rc =
1eb23 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1eb24 42 4b 50 54 3b 0a 20 20 20 20 67 6f 74 6f 20 62  BKPT;.    goto b
1eb25 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a  alance_cleanup;.
1eb26 20 20 7d 0a 20 20 70 61 67 65 46 6c 61 67 73 20    }.  pageFlags 
1eb27 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 61 44 61 74  = apOld[0]->aDat
1eb28 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  a[0];.  for(i=0;
1eb29 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<k; i++){.    
1eb2a 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20  MemPage *pNew;. 
1eb2b 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 7b     if( i<nOld ){
1eb2c 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70  .      pNew = ap
1eb2d 4e 65 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b 69  New[i] = apOld[i
1eb2e 5d 3b 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b 69  ];.      apOld[i
1eb2f 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20  ] = 0;.      rc 
1eb30 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
1eb31 69 74 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67  ite(pNew->pDbPag
1eb32 65 29 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b  e);.      nNew++
1eb33 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
1eb34 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
1eb35 65 61 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65  eanup;.    }else
1eb36 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1eb37 69 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20  i>0 );.      rc 
1eb38 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50  = allocateBtreeP
1eb39 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20  age(pBt, &pNew, 
1eb3a 26 70 67 6e 6f 2c 20 28 62 42 75 6c 6b 20 3f 20  &pgno, (bBulk ? 
1eb3b 31 20 3a 20 70 67 6e 6f 29 2c 20 30 29 3b 0a 20  1 : pgno), 0);. 
1eb3c 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f       if( rc ) go
1eb3d 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
1eb3e 75 70 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b  up;.      apNew[
1eb3f 69 5d 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20  i] = pNew;.     
1eb40 20 6e 4e 65 77 2b 2b 3b 0a 0a 20 20 20 20 20 20   nNew++;..      
1eb41 2f 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74  /* Set the point
1eb42 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72  er-map entry for
1eb43 20 74 68 65 20 6e 65 77 20 73 69 62 6c 69 6e 67   the new sibling
1eb44 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20   page. */.      
1eb45 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d  if( ISAUTOVACUUM
1eb46 20 29 7b 0a 20 20 20 20 20 20 20 20 70 74 72 6d   ){.        ptrm
1eb47 61 70 50 75 74 28 70 42 74 2c 20 70 4e 65 77 2d  apPut(pBt, pNew-
1eb48 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54  >pgno, PTRMAP_BT
1eb49 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67  REE, pParent->pg
1eb4a 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20  no, &rc);.      
1eb4b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1eb4c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
1eb4d 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
1eb4e 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d  eanup;.        }
1eb4f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1eb50 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e   }..  /* Free an
1eb51 79 20 6f 6c 64 20 70 61 67 65 73 20 74 68 61 74  y old pages that
1eb52 20 77 65 72 65 20 6e 6f 74 20 72 65 75 73 65 64   were not reused
1eb53 20 61 73 20 6e 65 77 20 70 61 67 65 73 2e 0a 20   as new pages.. 
1eb54 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e   */.  while( i<n
1eb55 4f 6c 64 20 29 7b 0a 20 20 20 20 66 72 65 65 50  Old ){.    freeP
1eb56 61 67 65 28 61 70 4f 6c 64 5b 69 5d 2c 20 26 72  age(apOld[i], &r
1eb57 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  c);.    if( rc )
1eb58 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
1eb59 65 61 6e 75 70 3b 0a 20 20 20 20 72 65 6c 65 61  eanup;.    relea
1eb5a 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29  sePage(apOld[i])
1eb5b 3b 0a 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d  ;.    apOld[i] =
1eb5c 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d   0;.    i++;.  }
1eb5d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20  ..  /*.  ** Put 
1eb5e 74 68 65 20 6e 65 77 20 70 61 67 65 73 20 69 6e  the new pages in
1eb5f 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   accending order
1eb60 2e 20 20 54 68 69 73 20 68 65 6c 70 73 20 74 6f  .  This helps to
1eb61 0a 20 20 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69  .  ** keep entri
1eb62 65 73 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66  es in the disk f
1eb63 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 73 6f 20  ile in order so 
1eb64 74 68 61 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a  that a scan.  **
1eb65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 73   of the table is
1eb66 20 61 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74   a linear scan t
1eb67 68 72 6f 75 67 68 20 74 68 65 20 66 69 6c 65 2e  hrough the file.
1eb68 20 20 54 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74    That.  ** in t
1eb69 75 72 6e 20 68 65 6c 70 73 20 74 68 65 20 6f 70  urn helps the op
1eb6a 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 74  erating system t
1eb6b 6f 20 64 65 6c 69 76 65 72 20 70 61 67 65 73 0a  o deliver pages.
1eb6c 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 69    ** from the di
1eb6d 73 6b 20 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e  sk more rapidly.
1eb6e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28  .  **.  ** An O(
1eb6f 6e 5e 32 29 20 69 6e 73 65 72 74 69 6f 6e 20 73  n^2) insertion s
1eb70 6f 72 74 20 61 6c 67 6f 72 69 74 68 6d 20 69 73  ort algorithm is
1eb71 20 75 73 65 64 2c 20 62 75 74 20 73 69 6e 63 65   used, but since
1eb72 0a 20 20 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72  .  ** n is never
1eb73 20 6d 6f 72 65 20 74 68 61 6e 20 4e 42 20 28 61   more than NB (a
1eb74 20 73 6d 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29   small constant)
1eb75 2c 20 74 68 61 74 20 73 68 6f 75 6c 64 0a 20 20  , that should.  
1eb76 2a 2a 20 6e 6f 74 20 62 65 20 61 20 70 72 6f 62  ** not be a prob
1eb77 6c 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  lem..  **.  ** W
1eb78 68 65 6e 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20  hen NB==3, this 
1eb79 6f 6e 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  one optimization
1eb7a 20 6d 61 6b 65 73 20 74 68 65 20 64 61 74 61 62   makes the datab
1eb7b 61 73 65 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32  ase.  ** about 2
1eb7c 35 25 20 66 61 73 74 65 72 20 66 6f 72 20 6c 61  5% faster for la
1eb7d 72 67 65 20 69 6e 73 65 72 74 69 6f 6e 73 20 61  rge insertions a
1eb7e 6e 64 20 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20  nd deletions..  
1eb7f 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
1eb80 6b 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  k-1; i++){.    i
1eb81 6e 74 20 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b  nt minV = apNew[
1eb82 69 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 69 6e  i]->pgno;.    in
1eb83 74 20 6d 69 6e 49 20 3d 20 69 3b 0a 20 20 20 20  t minI = i;.    
1eb84 66 6f 72 28 6a 3d 69 2b 31 3b 20 6a 3c 6b 3b 20  for(j=i+1; j<k; 
1eb85 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
1eb86 61 70 4e 65 77 5b 6a 5d 2d 3e 70 67 6e 6f 3c 28  apNew[j]->pgno<(
1eb87 75 6e 73 69 67 6e 65 64 29 6d 69 6e 56 20 29 7b  unsigned)minV ){
1eb88 0a 20 20 20 20 20 20 20 20 6d 69 6e 49 20 3d 20  .        minI = 
1eb89 6a 3b 0a 20 20 20 20 20 20 20 20 6d 69 6e 56 20  j;.        minV 
1eb8a 3d 20 61 70 4e 65 77 5b 6a 5d 2d 3e 70 67 6e 6f  = apNew[j]->pgno
1eb8b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1eb8c 20 20 20 20 69 66 28 20 6d 69 6e 49 3e 69 20 29      if( minI>i )
1eb8d 7b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20  {.      MemPage 
1eb8e 2a 70 54 3b 0a 20 20 20 20 20 20 70 54 20 3d 20  *pT;.      pT = 
1eb8f 61 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20  apNew[i];.      
1eb90 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77  apNew[i] = apNew
1eb91 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20 61 70  [minI];.      ap
1eb92 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a  New[minI] = pT;.
1eb93 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43      }.  }.  TRAC
1eb94 45 28 28 22 6e 65 77 3a 20 25 64 28 25 64 29 20  E(("new: %d(%d) 
1eb95 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64  %d(%d) %d(%d) %d
1eb96 28 25 64 29 20 25 64 28 25 64 29 5c 6e 22 2c 0a  (%d) %d(%d)\n",.
1eb97 20 20 20 20 61 70 4e 65 77 5b 30 5d 2d 3e 70 67      apNew[0]->pg
1eb98 6e 6f 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20  no, szNew[0],.  
1eb99 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 61 70 4e 65    nNew>=2 ? apNe
1eb9a 77 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20  w[1]->pgno : 0, 
1eb9b 6e 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b  nNew>=2 ? szNew[
1eb9c 31 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77  1] : 0,.    nNew
1eb9d 3e 3d 33 20 3f 20 61 70 4e 65 77 5b 32 5d 2d 3e  >=3 ? apNew[2]->
1eb9e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d  pgno : 0, nNew>=
1eb9f 33 20 3f 20 73 7a 4e 65 77 5b 32 5d 20 3a 20 30  3 ? szNew[2] : 0
1eba0 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 34 20 3f 20  ,.    nNew>=4 ? 
1eba1 61 70 4e 65 77 5b 33 5d 2d 3e 70 67 6e 6f 20 3a  apNew[3]->pgno :
1eba2 20 30 2c 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a   0, nNew>=4 ? sz
1eba3 4e 65 77 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20  New[3] : 0,.    
1eba4 6e 4e 65 77 3e 3d 35 20 3f 20 61 70 4e 65 77 5b  nNew>=5 ? apNew[
1eba5 34 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e  4]->pgno : 0, nN
1eba6 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d  ew>=5 ? szNew[4]
1eba7 20 3a 20 30 29 29 3b 0a 0a 20 20 61 73 73 65 72   : 0));..  asser
1eba8 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
1eba9 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65  swriteable(pPare
1ebaa 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  nt->pDbPage) );.
1ebab 20 20 70 75 74 34 62 79 74 65 28 70 52 69 67 68    put4byte(pRigh
1ebac 74 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d  t, apNew[nNew-1]
1ebad 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20  ->pgno);..  /*. 
1ebae 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74 72   ** Evenly distr
1ebaf 69 62 75 74 65 20 74 68 65 20 64 61 74 61 20 69  ibute the data i
1ebb0 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73  n apCell[] acros
1ebb1 73 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 2e  s the new pages.
1ebb2 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69 76  .  ** Insert div
1ebb3 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20  ider cells into 
1ebb4 70 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65 73  pParent as neces
1ebb5 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d  sary..  */.  j =
1ebb6 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   0;.  for(i=0; i
1ebb7 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nNew; i++){.   
1ebb8 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68 65   /* Assemble the
1ebb9 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67   new sibling pag
1ebba 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67  e. */.    MemPag
1ebbb 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b  e *pNew = apNew[
1ebbc 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  i];.    assert( 
1ebbd 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20  j<nMaxCells );. 
1ebbe 20 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77     zeroPage(pNew
1ebbf 2c 20 70 61 67 65 46 6c 61 67 73 29 3b 0a 20 20  , pageFlags);.  
1ebc0 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70    assemblePage(p
1ebc1 4e 65 77 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a  New, cntNew[i]-j
1ebc2 2c 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73  , &apCell[j], &s
1ebc3 7a 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61  zCell[j]);.    a
1ebc4 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 65  ssert( pNew->nCe
1ebc5 6c 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31  ll>0 || (nNew==1
1ebc6 20 26 26 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30   && cntNew[0]==0
1ebc7 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ) );.    assert(
1ebc8 20 70 4e 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77   pNew->nOverflow
1ebc9 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 6a 20 3d 20  ==0 );..    j = 
1ebca 63 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20 20 20 20  cntNew[i];..    
1ebcb 2f 2a 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e  /* If the siblin
1ebcc 67 20 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64  g page assembled
1ebcd 20 61 62 6f 76 65 20 77 61 73 20 6e 6f 74 20 74   above was not t
1ebce 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69  he right-most si
1ebcf 62 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a 20 69 6e  bling,.    ** in
1ebd0 73 65 72 74 20 61 20 64 69 76 69 64 65 72 20 63  sert a divider c
1ebd1 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20 70 61 72  ell into the par
1ebd2 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f  ent page..    */
1ebd3 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e  .    assert( i<n
1ebd4 4e 65 77 2d 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c  New-1 || j==nCel
1ebd5 6c 20 29 3b 0a 20 20 20 20 69 66 28 20 6a 3c 6e  l );.    if( j<n
1ebd6 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 75 38  Cell ){.      u8
1ebd7 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75   *pCell;.      u
1ebd8 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20  8 *pTemp;.      
1ebd9 69 6e 74 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61  int sz;..      a
1ebda 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c  ssert( j<nMaxCel
1ebdb 6c 73 20 29 3b 0a 20 20 20 20 20 20 70 43 65 6c  ls );.      pCel
1ebdc 6c 20 3d 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20  l = apCell[j];. 
1ebdd 20 20 20 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c       sz = szCell
1ebde 5b 6a 5d 20 2b 20 6c 65 61 66 43 6f 72 72 65 63  [j] + leafCorrec
1ebdf 74 69 6f 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d  tion;.      pTem
1ebe0 70 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b  p = &aOvflSpace[
1ebe1 69 4f 76 66 6c 53 70 61 63 65 5d 3b 0a 20 20 20  iOvflSpace];.   
1ebe2 20 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 6c 65     if( !pNew->le
1ebe3 61 66 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  af ){.        me
1ebe4 6d 63 70 79 28 26 70 4e 65 77 2d 3e 61 44 61 74  mcpy(&pNew->aDat
1ebe5 61 5b 38 5d 2c 20 70 43 65 6c 6c 2c 20 34 29 3b  a[8], pCell, 4);
1ebe6 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
1ebe7 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20   leafData ){.   
1ebe8 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74       /* If the t
1ebe9 72 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61  ree is a leaf-da
1ebea 74 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65  ta tree, and the
1ebeb 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65   siblings are le
1ebec 61 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a  aves, .        *
1ebed 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20  * then there is 
1ebee 6e 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20  no divider cell 
1ebef 69 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73  in apCell[]. Ins
1ebf0 74 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65  tead, the divide
1ebf1 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65  r .        ** ce
1ebf2 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74  ll consists of t
1ebf3 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66  he integer key f
1ebf4 6f 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  or the right-mos
1ebf5 74 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20  t cell of .     
1ebf6 20 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e     ** the siblin
1ebf7 67 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64  g-page assembled
1ebf8 20 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20   above only..   
1ebf9 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1ebfa 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20  CellInfo info;. 
1ebfb 20 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20         j--;.    
1ebfc 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65      btreeParseCe
1ebfd 6c 6c 50 74 72 28 70 4e 65 77 2c 20 61 70 43 65  llPtr(pNew, apCe
1ebfe 6c 6c 5b 6a 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20  ll[j], &info);. 
1ebff 20 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70         pCell = p
1ec00 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 73 7a  Temp;.        sz
1ec01 20 3d 20 34 20 2b 20 70 75 74 56 61 72 69 6e 74   = 4 + putVarint
1ec02 28 26 70 43 65 6c 6c 5b 34 5d 2c 20 69 6e 66 6f  (&pCell[4], info
1ec03 2e 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20  .nKey);.        
1ec04 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20  pTemp = 0;.     
1ec05 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1ec06 70 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20  pCell -= 4;.    
1ec07 20 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 63      /* Obscure c
1ec08 61 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66  ase for non-leaf
1ec09 2d 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 20  -data trees: If 
1ec0a 74 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 6c  the cell at pCel
1ec0b 6c 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a 2a  l was.        **
1ec0c 20 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f 72   previously stor
1ec0d 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64  ed on a leaf nod
1ec0e 65 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f 72  e, and its repor
1ec0f 74 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a 20  ted size was 4. 
1ec10 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 2c         ** bytes,
1ec11 20 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 74   then it may act
1ec12 75 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72  ually be smaller
1ec13 20 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 20   than this .    
1ec14 20 20 20 20 2a 2a 20 28 73 65 65 20 62 74 72 65      ** (see btre
1ec15 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c  eParseCellPtr(),
1ec16 20 34 20 62 79 74 65 73 20 69 73 20 74 68 65 20   4 bytes is the 
1ec17 6d 69 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 0a  minimum size of.
1ec18 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 63          ** any c
1ec19 65 6c 6c 29 2e 20 42 75 74 20 69 74 20 69 73 20  ell). But it is 
1ec1a 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 70 61 73  important to pas
1ec1b 73 20 74 68 65 20 63 6f 72 72 65 63 74 20 73 69  s the correct si
1ec1c 7a 65 20 74 6f 20 0a 20 20 20 20 20 20 20 20 2a  ze to .        *
1ec1d 2a 20 69 6e 73 65 72 74 43 65 6c 6c 28 29 2c 20  * insertCell(), 
1ec1e 73 6f 20 72 65 70 61 72 73 65 20 74 68 65 20 63  so reparse the c
1ec1f 65 6c 6c 20 6e 6f 77 2e 0a 20 20 20 20 20 20 20  ell now..       
1ec20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e   **.        ** N
1ec21 6f 74 65 20 74 68 61 74 20 74 68 69 73 20 63 61  ote that this ca
1ec22 6e 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 69  n never happen i
1ec23 6e 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61  n an SQLite data
1ec24 20 66 69 6c 65 2c 20 61 73 20 61 6c 6c 0a 20 20   file, as all.  
1ec25 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 73 20 61        ** cells a
1ec26 72 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 79  re at least 4 by
1ec27 74 65 73 2e 20 49 74 20 6f 6e 6c 79 20 68 61 70  tes. It only hap
1ec28 70 65 6e 73 20 69 6e 20 62 2d 74 72 65 65 73 20  pens in b-trees 
1ec29 75 73 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  used.        ** 
1ec2a 74 6f 20 65 76 61 6c 75 61 74 65 20 22 49 4e 20  to evaluate "IN 
1ec2b 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 61 6e  (SELECT ...)" an
1ec2c 64 20 73 69 6d 69 6c 61 72 20 63 6c 61 75 73 65  d similar clause
1ec2d 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  s..        */.  
1ec2e 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c        if( szCell
1ec2f 5b 6a 5d 3d 3d 34 20 29 7b 0a 20 20 20 20 20 20  [j]==4 ){.      
1ec30 20 20 20 20 61 73 73 65 72 74 28 6c 65 61 66 43      assert(leafC
1ec31 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20  orrection==4);. 
1ec32 20 20 20 20 20 20 20 20 20 73 7a 20 3d 20 63 65           sz = ce
1ec33 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e  llSizePtr(pParen
1ec34 74 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20  t, pCell);.     
1ec35 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1ec36 20 20 20 69 4f 76 66 6c 53 70 61 63 65 20 2b 3d     iOvflSpace +=
1ec37 20 73 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72   sz;.      asser
1ec38 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 6d 61 78 4c  t( sz<=pBt->maxL
1ec39 6f 63 61 6c 2b 32 33 20 29 3b 0a 20 20 20 20 20  ocal+23 );.     
1ec3a 20 61 73 73 65 72 74 28 20 69 4f 76 66 6c 53 70   assert( iOvflSp
1ec3b 61 63 65 20 3c 3d 20 28 69 6e 74 29 70 42 74 2d  ace <= (int)pBt-
1ec3c 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20  >pageSize );.   
1ec3d 20 20 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50     insertCell(pP
1ec3e 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 70 43  arent, nxDiv, pC
1ec3f 65 6c 6c 2c 20 73 7a 2c 20 70 54 65 6d 70 2c 20  ell, sz, pTemp, 
1ec40 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26 72 63 29  pNew->pgno, &rc)
1ec41 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
1ec42 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
1ec43 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70   balance_cleanup
1ec44 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1ec45 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
1ec46 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d  iteable(pParent-
1ec47 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20  >pDbPage) );..  
1ec48 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 6e      j++;.      n
1ec49 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  xDiv++;.    }.  
1ec4a 7d 0a 20 20 61 73 73 65 72 74 28 20 6a 3d 3d 6e  }.  assert( j==n
1ec4b 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74  Cell );.  assert
1ec4c 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a 20 20 61 73  ( nOld>0 );.  as
1ec4d 73 65 72 74 28 20 6e 4e 65 77 3e 30 20 29 3b 0a  sert( nNew>0 );.
1ec4e 20 20 69 66 28 20 28 70 61 67 65 46 6c 61 67 73    if( (pageFlags
1ec4f 20 26 20 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20   & PTF_LEAF)==0 
1ec50 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43 68 69 6c  ){.    u8 *zChil
1ec51 64 20 3d 20 26 61 70 43 6f 70 79 5b 6e 4f 6c 64  d = &apCopy[nOld
1ec52 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 3b 0a 20  -1]->aData[8];. 
1ec53 20 20 20 6d 65 6d 63 70 79 28 26 61 70 4e 65 77     memcpy(&apNew
1ec54 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44 61 74 61 5b  [nNew-1]->aData[
1ec55 38 5d 2c 20 7a 43 68 69 6c 64 2c 20 34 29 3b 0a  8], zChild, 4);.
1ec56 20 20 7d 0a 0a 20 20 69 66 28 20 69 73 52 6f 6f    }..  if( isRoo
1ec57 74 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43  t && pParent->nC
1ec58 65 6c 6c 3d 3d 30 20 26 26 20 70 50 61 72 65 6e  ell==0 && pParen
1ec59 74 2d 3e 68 64 72 4f 66 66 73 65 74 3c 3d 61 70  t->hdrOffset<=ap
1ec5a 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20 29 7b  New[0]->nFree ){
1ec5b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74  .    /* The root
1ec5c 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74   page of the b-t
1ec5d 72 65 65 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73  ree now contains
1ec5e 20 6e 6f 20 63 65 6c 6c 73 2e 20 54 68 65 20 6f   no cells. The o
1ec5f 6e 6c 79 20 73 69 62 6c 69 6e 67 0a 20 20 20 20  nly sibling.    
1ec60 2a 2a 20 70 61 67 65 20 69 73 20 74 68 65 20 72  ** page is the r
1ec61 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 74 68  ight-child of th
1ec62 65 20 70 61 72 65 6e 74 2e 20 43 6f 70 79 20 74  e parent. Copy t
1ec63 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
1ec64 68 65 0a 20 20 20 20 2a 2a 20 63 68 69 6c 64 20  he.    ** child 
1ec65 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 61  page into the pa
1ec66 72 65 6e 74 2c 20 64 65 63 72 65 61 73 69 6e 67  rent, decreasing
1ec67 20 74 68 65 20 6f 76 65 72 61 6c 6c 20 68 65 69   the overall hei
1ec68 67 68 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a  ght of the.    *
1ec69 2a 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75  * b-tree structu
1ec6a 72 65 20 62 79 20 6f 6e 65 2e 20 54 68 69 73 20  re by one. This 
1ec6b 69 73 20 64 65 73 63 72 69 62 65 64 20 61 73 20  is described as 
1ec6c 74 68 65 20 22 62 61 6c 61 6e 63 65 2d 73 68 61  the "balance-sha
1ec6d 6c 6c 6f 77 65 72 22 0a 20 20 20 20 2a 2a 20 73  llower".    ** s
1ec6e 75 62 2d 61 6c 67 6f 72 69 74 68 6d 20 69 6e 20  ub-algorithm in 
1ec6f 73 6f 6d 65 20 64 6f 63 75 6d 65 6e 74 61 74 69  some documentati
1ec70 6f 6e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  on..    **.    *
1ec71 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  * If this is an 
1ec72 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
1ec73 62 61 73 65 2c 20 74 68 65 20 63 61 6c 6c 20 74  base, the call t
1ec74 6f 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e  o copyNodeConten
1ec75 74 28 29 20 0a 20 20 20 20 2a 2a 20 73 65 74 73  t() .    ** sets
1ec76 20 61 6c 6c 20 70 6f 69 6e 74 65 72 2d 6d 61 70   all pointer-map
1ec77 20 65 6e 74 72 69 65 73 20 63 6f 72 72 65 73 70   entries corresp
1ec78 6f 6e 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61  onding to databa
1ec79 73 65 20 69 6d 61 67 65 20 70 61 67 65 73 20 0a  se image pages .
1ec7a 20 20 20 20 2a 2a 20 66 6f 72 20 77 68 69 63 68      ** for which
1ec7b 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20   the pointer is 
1ec7c 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20 74 68  stored within th
1ec7d 65 20 63 6f 6e 74 65 6e 74 20 62 65 69 6e 67 20  e content being 
1ec7e 63 6f 70 69 65 64 2e 0a 20 20 20 20 2a 2a 0a 20  copied..    **. 
1ec7f 20 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64     ** The second
1ec80 20 61 73 73 65 72 74 20 62 65 6c 6f 77 20 76 65   assert below ve
1ec81 72 69 66 69 65 73 20 74 68 61 74 20 74 68 65 20  rifies that the 
1ec82 63 68 69 6c 64 20 70 61 67 65 20 69 73 20 64 65  child page is de
1ec83 66 72 61 67 6d 65 6e 74 65 64 0a 20 20 20 20 2a  fragmented.    *
1ec84 2a 20 28 69 74 20 6d 75 73 74 20 62 65 2c 20 61  * (it must be, a
1ec85 73 20 69 74 20 77 61 73 20 6a 75 73 74 20 72 65  s it was just re
1ec86 63 6f 6e 73 74 72 75 63 74 65 64 20 75 73 69 6e  constructed usin
1ec87 67 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 29  g assemblePage()
1ec88 29 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a 20 69  ). This.    ** i
1ec89 73 20 69 6d 70 6f 72 74 61 6e 74 20 69 66 20 74  s important if t
1ec8a 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 68  he parent page h
1ec8b 61 70 70 65 6e 73 20 74 6f 20 62 65 20 70 61 67  appens to be pag
1ec8c 65 20 31 20 6f 66 20 74 68 65 20 64 61 74 61 62  e 1 of the datab
1ec8d 61 73 65 0a 20 20 20 20 2a 2a 20 69 6d 61 67 65  ase.    ** image
1ec8e 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  .  */.    assert
1ec8f 28 20 6e 4e 65 77 3d 3d 31 20 29 3b 0a 20 20 20  ( nNew==1 );.   
1ec90 20 61 73 73 65 72 74 28 20 61 70 4e 65 77 5b 30   assert( apNew[0
1ec91 5d 2d 3e 6e 46 72 65 65 20 3d 3d 20 0a 20 20 20  ]->nFree == .   
1ec92 20 20 20 20 20 28 67 65 74 32 62 79 74 65 28 26       (get2byte(&
1ec93 61 70 4e 65 77 5b 30 5d 2d 3e 61 44 61 74 61 5b  apNew[0]->aData[
1ec94 35 5d 29 2d 61 70 4e 65 77 5b 30 5d 2d 3e 63 65  5])-apNew[0]->ce
1ec95 6c 6c 4f 66 66 73 65 74 2d 61 70 4e 65 77 5b 30  llOffset-apNew[0
1ec96 5d 2d 3e 6e 43 65 6c 6c 2a 32 29 20 0a 20 20 20  ]->nCell*2) .   
1ec97 20 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64 65   );.    copyNode
1ec98 43 6f 6e 74 65 6e 74 28 61 70 4e 65 77 5b 30 5d  Content(apNew[0]
1ec99 2c 20 70 50 61 72 65 6e 74 2c 20 26 72 63 29 3b  , pParent, &rc);
1ec9a 0a 20 20 20 20 66 72 65 65 50 61 67 65 28 61 70  .    freePage(ap
1ec9b 4e 65 77 5b 30 5d 2c 20 26 72 63 29 3b 0a 20 20  New[0], &rc);.  
1ec9c 7d 65 6c 73 65 20 69 66 28 20 49 53 41 55 54 4f  }else if( ISAUTO
1ec9d 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 2f 2a  VACUUM ){.    /*
1ec9e 20 46 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72   Fix the pointer
1ec9f 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72  -map entries for
1eca0 20 61 6c 6c 20 74 68 65 20 63 65 6c 6c 73 20 74   all the cells t
1eca1 68 61 74 20 77 65 72 65 20 73 68 69 66 74 65 64  hat were shifted
1eca2 20 61 72 6f 75 6e 64 2e 20 0a 20 20 20 20 2a 2a   around. .    **
1eca3 20 54 68 65 72 65 20 61 72 65 20 73 65 76 65 72   There are sever
1eca4 61 6c 20 64 69 66 66 65 72 65 6e 74 20 74 79 70  al different typ
1eca5 65 73 20 6f 66 20 70 6f 69 6e 74 65 72 2d 6d 61  es of pointer-ma
1eca6 70 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 6e  p entries that n
1eca7 65 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65  eed to.    ** be
1eca8 20 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 74   dealt with by t
1eca9 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 53 6f 6d  his routine. Som
1ecaa 65 20 6f 66 20 74 68 65 73 65 20 68 61 76 65 20  e of these have 
1ecab 62 65 65 6e 20 73 65 74 20 61 6c 72 65 61 64 79  been set already
1ecac 2c 20 62 75 74 0a 20 20 20 20 2a 2a 20 6d 61 6e  , but.    ** man
1ecad 79 20 68 61 76 65 20 6e 6f 74 2e 20 54 68 65 20  y have not. The 
1ecae 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 20 73  following is a s
1ecaf 75 6d 6d 61 72 79 3a 0a 20 20 20 20 2a 2a 0a 20  ummary:.    **. 
1ecb0 20 20 20 2a 2a 20 20 20 31 29 20 54 68 65 20 65     **   1) The e
1ecb1 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65  ntries associate
1ecb2 64 20 77 69 74 68 20 6e 65 77 20 73 69 62 6c 69  d with new sibli
1ecb3 6e 67 20 70 61 67 65 73 20 74 68 61 74 20 77 65  ng pages that we
1ecb4 72 65 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 20 20  re not.    **   
1ecb5 20 20 20 73 69 62 6c 69 6e 67 73 20 77 68 65 6e     siblings when
1ecb6 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   this function w
1ecb7 61 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 73 65  as called. These
1ecb8 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 20 20   have already.  
1ecb9 20 20 2a 2a 20 20 20 20 20 20 62 65 65 6e 20 73    **      been s
1ecba 65 74 2e 20 57 65 20 64 6f 6e 27 74 20 6e 65 65  et. We don't nee
1ecbb 64 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74  d to worry about
1ecbc 20 6f 6c 64 20 73 69 62 6c 69 6e 67 73 20 74 68   old siblings th
1ecbd 61 74 20 77 65 72 65 0a 20 20 20 20 2a 2a 20 20  at were.    **  
1ecbe 20 20 20 20 6d 6f 76 65 64 20 74 6f 20 74 68 65      moved to the
1ecbf 20 66 72 65 65 2d 6c 69 73 74 20 2d 20 74 68 65   free-list - the
1ecc0 20 66 72 65 65 50 61 67 65 28 29 20 63 6f 64 65   freePage() code
1ecc1 20 68 61 73 20 74 61 6b 65 6e 20 63 61 72 65 0a   has taken care.
1ecc2 20 20 20 20 2a 2a 20 20 20 20 20 20 6f 66 20 74      **      of t
1ecc3 68 6f 73 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  hose..    **.   
1ecc4 20 2a 2a 20 20 20 32 29 20 54 68 65 20 70 6f 69   **   2) The poi
1ecc5 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73  nter-map entries
1ecc6 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1ecc7 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66   the first overf
1ecc8 6c 6f 77 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  low.    **      
1ecc9 70 61 67 65 20 69 6e 20 61 6e 79 20 6f 76 65 72  page in any over
1ecca 66 6c 6f 77 20 63 68 61 69 6e 73 20 75 73 65 64  flow chains used
1eccb 20 62 79 20 6e 65 77 20 64 69 76 69 64 65 72 20   by new divider 
1eccc 63 65 6c 6c 73 2e 20 54 68 65 73 65 20 0a 20 20  cells. These .  
1eccd 20 20 2a 2a 20 20 20 20 20 20 68 61 76 65 20 61    **      have a
1ecce 6c 73 6f 20 61 6c 72 65 61 64 79 20 62 65 65 6e  lso already been
1eccf 20 74 61 6b 65 6e 20 63 61 72 65 20 6f 66 20 62   taken care of b
1ecd0 79 20 74 68 65 20 69 6e 73 65 72 74 43 65 6c 6c  y the insertCell
1ecd1 28 29 20 63 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a  () code..    **.
1ecd2 20 20 20 20 2a 2a 20 20 20 33 29 20 49 66 20 74      **   3) If t
1ecd3 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73  he sibling pages
1ecd4 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c   are not leaves,
1ecd5 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64 20   then the child 
1ecd6 70 61 67 65 73 20 6f 66 0a 20 20 20 20 2a 2a 20  pages of.    ** 
1ecd7 20 20 20 20 20 63 65 6c 6c 73 20 73 74 6f 72 65       cells store
1ecd8 64 20 6f 6e 20 74 68 65 20 73 69 62 6c 69 6e 67  d on the sibling
1ecd9 20 70 61 67 65 73 20 6d 61 79 20 6e 65 65 64 20   pages may need 
1ecda 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20  to be updated.. 
1ecdb 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 34     **.    **   4
1ecdc 29 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67  ) If the sibling
1ecdd 20 70 61 67 65 73 20 61 72 65 20 6e 6f 74 20 69   pages are not i
1ecde 6e 74 65 72 6e 61 6c 20 69 6e 74 6b 65 79 20 6e  nternal intkey n
1ecdf 6f 64 65 73 2c 20 74 68 65 6e 20 61 6e 79 0a 20  odes, then any. 
1ece0 20 20 20 2a 2a 20 20 20 20 20 20 6f 76 65 72 66     **      overf
1ece1 6c 6f 77 20 70 61 67 65 73 20 75 73 65 64 20 62  low pages used b
1ece2 79 20 74 68 65 73 65 20 63 65 6c 6c 73 20 6d 61  y these cells ma
1ece3 79 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64  y need to be upd
1ece4 61 74 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20  ated.    **     
1ece5 20 28 69 6e 74 65 72 6e 61 6c 20 69 6e 74 6b 65   (internal intke
1ece6 79 20 6e 6f 64 65 73 20 6e 65 76 65 72 20 63 6f  y nodes never co
1ece7 6e 74 61 69 6e 20 70 6f 69 6e 74 65 72 73 20 74  ntain pointers t
1ece8 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
1ece9 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  )..    **.    **
1ecea 20 20 20 35 29 20 49 66 20 74 68 65 20 73 69 62     5) If the sib
1eceb 6c 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 6e  ling pages are n
1ecec 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 65 6e 20  ot leaves, then 
1eced 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a  the pointer-map.
1ecee 20 20 20 20 2a 2a 20 20 20 20 20 20 65 6e 74 72      **      entr
1ecef 69 65 73 20 66 6f 72 20 74 68 65 20 72 69 67 68  ies for the righ
1ecf0 74 2d 63 68 69 6c 64 20 70 61 67 65 73 20 6f 66  t-child pages of
1ecf1 20 65 61 63 68 20 73 69 62 6c 69 6e 67 20 6d 61   each sibling ma
1ecf2 79 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 20 20  y need.    **   
1ecf3 20 20 20 74 6f 20 62 65 20 75 70 64 61 74 65 64     to be updated
1ecf4 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1ecf5 43 61 73 65 73 20 31 20 61 6e 64 20 32 20 61 72  Cases 1 and 2 ar
1ecf6 65 20 64 65 61 6c 74 20 77 69 74 68 20 61 62 6f  e dealt with abo
1ecf7 76 65 20 62 79 20 6f 74 68 65 72 20 63 6f 64 65  ve by other code
1ecf8 2e 20 54 68 65 20 6e 65 78 74 0a 20 20 20 20 2a  . The next.    *
1ecf9 2a 20 62 6c 6f 63 6b 20 64 65 61 6c 73 20 77 69  * block deals wi
1ecfa 74 68 20 63 61 73 65 73 20 33 20 61 6e 64 20 34  th cases 3 and 4
1ecfb 20 61 6e 64 20 74 68 65 20 6f 6e 65 20 61 66 74   and the one aft
1ecfc 65 72 20 74 68 61 74 2c 20 63 61 73 65 20 35 2e  er that, case 5.
1ecfd 20 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20 73 65   Since.    ** se
1ecfe 74 74 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 20  tting a pointer 
1ecff 6d 61 70 20 65 6e 74 72 79 20 69 73 20 61 20 72  map entry is a r
1ed00 65 6c 61 74 69 76 65 6c 79 20 65 78 70 65 6e 73  elatively expens
1ed01 69 76 65 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74  ive operation, t
1ed02 68 69 73 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20  his.    ** code 
1ed03 6f 6e 6c 79 20 73 65 74 73 20 70 6f 69 6e 74 65  only sets pointe
1ed04 72 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f  r map entries fo
1ed05 72 20 63 68 69 6c 64 20 6f 72 20 6f 76 65 72 66  r child or overf
1ed06 6c 6f 77 20 70 61 67 65 73 20 74 68 61 74 20 68  low pages that h
1ed07 61 76 65 0a 20 20 20 20 2a 2a 20 61 63 74 75 61  ave.    ** actua
1ed08 6c 6c 79 20 6d 6f 76 65 64 20 62 65 74 77 65 65  lly moved betwee
1ed09 6e 20 70 61 67 65 73 2e 20 20 2a 2f 0a 20 20 20  n pages.  */.   
1ed0a 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 20 3d   MemPage *pNew =
1ed0b 20 61 70 4e 65 77 5b 30 5d 3b 0a 20 20 20 20 4d   apNew[0];.    M
1ed0c 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61  emPage *pOld = a
1ed0d 70 43 6f 70 79 5b 30 5d 3b 0a 20 20 20 20 69 6e  pCopy[0];.    in
1ed0e 74 20 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 4f  t nOverflow = pO
1ed0f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20  ld->nOverflow;. 
1ed10 20 20 20 69 6e 74 20 69 4e 65 78 74 4f 6c 64 20     int iNextOld 
1ed11 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 20 2b 20  = pOld->nCell + 
1ed12 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69  nOverflow;.    i
1ed13 6e 74 20 69 4f 76 65 72 66 6c 6f 77 20 3d 20 28  nt iOverflow = (
1ed14 6e 4f 76 65 72 66 6c 6f 77 20 3f 20 70 4f 6c 64  nOverflow ? pOld
1ed15 2d 3e 61 69 4f 76 66 6c 5b 30 5d 20 3a 20 2d 31  ->aiOvfl[0] : -1
1ed16 29 3b 0a 20 20 20 20 6a 20 3d 20 30 3b 20 20 20  );.    j = 0;   
1ed17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ed18 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
1ed19 72 65 6e 74 20 27 6f 6c 64 27 20 73 69 62 6c 69  rent 'old' sibli
1ed1a 6e 67 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 6b  ng page */.    k
1ed1b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1ed1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ed1d 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 27 6e 65    /* Current 'ne
1ed1e 77 27 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20  w' sibling page 
1ed1f 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  */.    for(i=0; 
1ed20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20  i<nCell; i++){. 
1ed21 20 20 20 20 20 69 6e 74 20 69 73 44 69 76 69 64       int isDivid
1ed22 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68  er = 0;.      wh
1ed23 69 6c 65 28 20 69 3d 3d 69 4e 65 78 74 4f 6c 64  ile( i==iNextOld
1ed24 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43   ){.        /* C
1ed25 65 6c 6c 20 69 20 69 73 20 74 68 65 20 63 65 6c  ell i is the cel
1ed26 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f  l immediately fo
1ed27 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 74  llowing the last
1ed28 20 63 65 6c 6c 20 6f 6e 20 6f 6c 64 0a 20 20 20   cell on old.   
1ed29 20 20 20 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20       ** sibling 
1ed2a 70 61 67 65 20 6a 2e 20 49 66 20 74 68 65 20 73  page j. If the s
1ed2b 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20  iblings are not 
1ed2c 6c 65 61 66 20 70 61 67 65 73 20 6f 66 20 61 6e  leaf pages of an
1ed2d 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6b  .        ** intk
1ed2e 65 79 20 62 2d 74 72 65 65 2c 20 74 68 65 6e 20  ey b-tree, then 
1ed2f 63 65 6c 6c 20 69 20 77 61 73 20 61 20 64 69 76  cell i was a div
1ed30 69 64 65 72 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20  ider cell. */.  
1ed31 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 2b        assert( j+
1ed32 31 20 3c 20 41 72 72 61 79 53 69 7a 65 28 61 70  1 < ArraySize(ap
1ed33 43 6f 70 79 29 20 29 3b 0a 20 20 20 20 20 20 20  Copy) );.       
1ed34 20 61 73 73 65 72 74 28 20 6a 2b 31 20 3c 20 6e   assert( j+1 < n
1ed35 4f 6c 64 20 29 3b 0a 20 20 20 20 20 20 20 20 70  Old );.        p
1ed36 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 2b 2b 6a  Old = apCopy[++j
1ed37 5d 3b 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74  ];.        iNext
1ed38 4f 6c 64 20 3d 20 69 20 2b 20 21 6c 65 61 66 44  Old = i + !leafD
1ed39 61 74 61 20 2b 20 70 4f 6c 64 2d 3e 6e 43 65 6c  ata + pOld->nCel
1ed3a 6c 20 2b 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66  l + pOld->nOverf
1ed3b 6c 6f 77 3b 0a 20 20 20 20 20 20 20 20 69 66 28  low;.        if(
1ed3c 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77   pOld->nOverflow
1ed3d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 4f   ){.          nO
1ed3e 76 65 72 66 6c 6f 77 20 3d 20 70 4f 6c 64 2d 3e  verflow = pOld->
1ed3f 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 20  nOverflow;.     
1ed40 20 20 20 20 20 69 4f 76 65 72 66 6c 6f 77 20 3d       iOverflow =
1ed41 20 69 20 2b 20 21 6c 65 61 66 44 61 74 61 20 2b   i + !leafData +
1ed42 20 70 4f 6c 64 2d 3e 61 69 4f 76 66 6c 5b 30 5d   pOld->aiOvfl[0]
1ed43 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1ed44 20 20 20 20 69 73 44 69 76 69 64 65 72 20 3d 20      isDivider = 
1ed45 21 6c 65 61 66 44 61 74 61 3b 20 20 0a 20 20 20  !leafData;  .   
1ed46 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65     }..      asse
1ed47 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c  rt(nOverflow>0 |
1ed48 7c 20 69 4f 76 65 72 66 6c 6f 77 3c 69 20 29 3b  | iOverflow<i );
1ed49 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e 4f  .      assert(nO
1ed4a 76 65 72 66 6c 6f 77 3c 32 20 7c 7c 20 70 4f 6c  verflow<2 || pOl
1ed4b 64 2d 3e 61 69 4f 76 66 6c 5b 30 5d 3d 3d 70 4f  d->aiOvfl[0]==pO
1ed4c 6c 64 2d 3e 61 69 4f 76 66 6c 5b 31 5d 2d 31 29  ld->aiOvfl[1]-1)
1ed4d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e  ;.      assert(n
1ed4e 4f 76 65 72 66 6c 6f 77 3c 33 20 7c 7c 20 70 4f  Overflow<3 || pO
1ed4f 6c 64 2d 3e 61 69 4f 76 66 6c 5b 31 5d 3d 3d 70  ld->aiOvfl[1]==p
1ed50 4f 6c 64 2d 3e 61 69 4f 76 66 6c 5b 32 5d 2d 31  Old->aiOvfl[2]-1
1ed51 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d  );.      if( i==
1ed52 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20  iOverflow ){.   
1ed53 20 20 20 20 20 69 73 44 69 76 69 64 65 72 20 3d       isDivider =
1ed54 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   1;.        if( 
1ed55 28 2d 2d 6e 4f 76 65 72 66 6c 6f 77 29 3e 30 20  (--nOverflow)>0 
1ed56 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 4f 76  ){.          iOv
1ed57 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 20 20  erflow++;.      
1ed58 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20    }.      }..   
1ed59 20 20 20 69 66 28 20 69 3d 3d 63 6e 74 4e 65 77     if( i==cntNew
1ed5a 5b 6b 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  [k] ){.        /
1ed5b 2a 20 43 65 6c 6c 20 69 20 69 73 20 74 68 65 20  * Cell i is the 
1ed5c 63 65 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79  cell immediately
1ed5d 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c   following the l
1ed5e 61 73 74 20 63 65 6c 6c 20 6f 6e 20 6e 65 77 0a  ast cell on new.
1ed5f 20 20 20 20 20 20 20 20 2a 2a 20 73 69 62 6c 69          ** sibli
1ed60 6e 67 20 70 61 67 65 20 6b 2e 20 49 66 20 74 68  ng page k. If th
1ed61 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e  e siblings are n
1ed62 6f 74 20 6c 65 61 66 20 70 61 67 65 73 20 6f 66  ot leaf pages of
1ed63 20 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 69   an.        ** i
1ed64 6e 74 6b 65 79 20 62 2d 74 72 65 65 2c 20 74 68  ntkey b-tree, th
1ed65 65 6e 20 63 65 6c 6c 20 69 20 69 73 20 61 20 64  en cell i is a d
1ed66 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 20 2a 2f  ivider cell.  */
1ed67 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20  .        pNew = 
1ed68 61 70 4e 65 77 5b 2b 2b 6b 5d 3b 0a 20 20 20 20  apNew[++k];.    
1ed69 20 20 20 20 69 66 28 20 21 6c 65 61 66 44 61 74      if( !leafDat
1ed6a 61 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  a ) continue;.  
1ed6b 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
1ed6c 72 74 28 20 6a 3c 6e 4f 6c 64 20 29 3b 0a 20 20  rt( j<nOld );.  
1ed6d 20 20 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4e      assert( k<nN
1ed6e 65 77 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ew );..      /* 
1ed6f 49 66 20 74 68 65 20 63 65 6c 6c 20 77 61 73 20  If the cell was 
1ed70 6f 72 69 67 69 6e 61 6c 6c 79 20 64 69 76 69 64  originally divid
1ed71 65 72 20 63 65 6c 6c 20 28 61 6e 64 20 69 73 20  er cell (and is 
1ed72 6e 6f 74 20 6e 6f 77 29 20 6f 72 0a 20 20 20 20  not now) or.    
1ed73 20 20 2a 2a 20 61 6e 20 6f 76 65 72 66 6c 6f 77    ** an overflow
1ed74 20 63 65 6c 6c 2c 20 6f 72 20 69 66 20 74 68 65   cell, or if the
1ed75 20 63 65 6c 6c 20 77 61 73 20 6c 6f 63 61 74 65   cell was locate
1ed76 64 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74  d on a different
1ed77 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 20 20 2a   sibling.      *
1ed78 2a 20 70 61 67 65 20 62 65 66 6f 72 65 20 74 68  * page before th
1ed79 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65  e balancing, the
1ed7a 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  n the pointer ma
1ed7b 70 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69  p entries associ
1ed7c 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 77 69  ated.      ** wi
1ed7d 74 68 20 61 6e 79 20 63 68 69 6c 64 20 6f 72 20  th any child or 
1ed7e 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 6e  overflow pages n
1ed7f 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 65  eed to be update
1ed80 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  d.  */.      if(
1ed81 20 69 73 44 69 76 69 64 65 72 20 7c 7c 20 70 4f   isDivider || pO
1ed82 6c 64 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e  ld->pgno!=pNew->
1ed83 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20  pgno ){.        
1ed84 69 66 28 20 21 6c 65 61 66 43 6f 72 72 65 63 74  if( !leafCorrect
1ed85 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ion ){.         
1ed86 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
1ed87 67 65 74 34 62 79 74 65 28 61 70 43 65 6c 6c 5b  get4byte(apCell[
1ed88 69 5d 29 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  i]), PTRMAP_BTRE
1ed89 45 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26  E, pNew->pgno, &
1ed8a 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  rc);.        }. 
1ed8b 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c         if( szCel
1ed8c 6c 5b 69 5d 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f  l[i]>pNew->minLo
1ed8d 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  cal ){.         
1ed8e 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74   ptrmapPutOvflPt
1ed8f 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 69  r(pNew, apCell[i
1ed90 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20  ], &rc);.       
1ed91 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
1ed92 0a 0a 20 20 20 20 69 66 28 20 21 6c 65 61 66 43  ..    if( !leafC
1ed93 6f 72 72 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20  orrection ){.   
1ed94 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e     for(i=0; i<nN
1ed95 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ew; i++){.      
1ed96 20 20 75 33 32 20 6b 65 79 20 3d 20 67 65 74 34    u32 key = get4
1ed97 62 79 74 65 28 26 61 70 4e 65 77 5b 69 5d 2d 3e  byte(&apNew[i]->
1ed98 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20  aData[8]);.     
1ed99 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74     ptrmapPut(pBt
1ed9a 2c 20 6b 65 79 2c 20 50 54 52 4d 41 50 5f 42 54  , key, PTRMAP_BT
1ed9b 52 45 45 2c 20 61 70 4e 65 77 5b 69 5d 2d 3e 70  REE, apNew[i]->p
1ed9c 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20  gno, &rc);.     
1ed9d 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 0a   }.    }..#if 0.
1ed9e 20 20 20 20 2f 2a 20 54 68 65 20 70 74 72 6d 61      /* The ptrma
1ed9f 70 43 68 65 63 6b 50 61 67 65 73 28 29 20 63 6f  pCheckPages() co
1eda0 6e 74 61 69 6e 73 20 61 73 73 65 72 74 28 29 20  ntains assert() 
1eda1 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20  statements that 
1eda2 76 65 72 69 66 79 20 74 68 61 74 0a 20 20 20 20  verify that.    
1eda3 2a 2a 20 61 6c 6c 20 70 6f 69 6e 74 65 72 20 6d  ** all pointer m
1eda4 61 70 20 70 61 67 65 73 20 61 72 65 20 73 65 74  ap pages are set
1eda5 20 63 6f 72 72 65 63 74 6c 79 2e 20 54 68 69 73   correctly. This
1eda6 20 69 73 20 68 65 6c 70 66 75 6c 20 77 68 69 6c   is helpful whil
1eda7 65 20 0a 20 20 20 20 2a 2a 20 64 65 62 75 67 67  e .    ** debugg
1eda8 69 6e 67 2e 20 54 68 69 73 20 69 73 20 75 73 75  ing. This is usu
1eda9 61 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 62 65  ally disabled be
1edaa 63 61 75 73 65 20 61 20 63 6f 72 72 75 70 74 20  cause a corrupt 
1edab 64 61 74 61 62 61 73 65 20 6d 61 79 0a 20 20 20  database may.   
1edac 20 2a 2a 20 63 61 75 73 65 20 61 6e 20 61 73 73   ** cause an ass
1edad 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20  ert() statement 
1edae 74 6f 20 66 61 69 6c 2e 20 20 2a 2f 0a 20 20 20  to fail.  */.   
1edaf 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65   ptrmapCheckPage
1edb0 73 28 61 70 4e 65 77 2c 20 6e 4e 65 77 29 3b 0a  s(apNew, nNew);.
1edb1 20 20 20 20 70 74 72 6d 61 70 43 68 65 63 6b 50      ptrmapCheckP
1edb2 61 67 65 73 28 26 70 50 61 72 65 6e 74 2c 20 31  ages(&pParent, 1
1edb3 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20  );.#endif.  }.. 
1edb4 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74   assert( pParent
1edb5 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 54 52  ->isInit );.  TR
1edb6 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 66  ACE(("BALANCE: f
1edb7 69 6e 69 73 68 65 64 3a 20 6f 6c 64 3d 25 64 20  inished: old=%d 
1edb8 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c  new=%d cells=%d\
1edb9 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 6e 4f  n",.          nO
1edba 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29  ld, nNew, nCell)
1edbb 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c  );..  /*.  ** Cl
1edbc 65 61 6e 75 70 20 62 65 66 6f 72 65 20 72 65 74  eanup before ret
1edbd 75 72 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c  urning..  */.bal
1edbe 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20  ance_cleanup:.  
1edbf 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72  sqlite3ScratchFr
1edc0 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20 20 66 6f  ee(apCell);.  fo
1edc1 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69  r(i=0; i<nOld; i
1edc2 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65  ++){.    release
1edc3 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a  Page(apOld[i]);.
1edc4 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
1edc5 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nNew; i++){.   
1edc6 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4e   releasePage(apN
1edc7 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72  ew[i]);.  }..  r
1edc8 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 69 66 20  eturn rc;.}.#if 
1edc9 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52  defined(_MSC_VER
1edca 29 20 26 26 20 5f 4d 53 43 5f 56 45 52 20 3e 3d  ) && _MSC_VER >=
1edcb 20 31 37 30 30 20 26 26 20 64 65 66 69 6e 65 64   1700 && defined
1edcc 28 5f 4d 5f 41 52 4d 29 0a 23 70 72 61 67 6d 61  (_M_ARM).#pragma
1edcd 20 6f 70 74 69 6d 69 7a 65 28 22 22 2c 20 6f 6e   optimize("", on
1edce 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  ).#endif.../*.**
1edcf 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
1edd0 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  s called when th
1edd1 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61  e root page of a
1edd2 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72   b-tree structur
1edd3 65 20 69 73 0a 2a 2a 20 6f 76 65 72 66 75 6c 6c  e is.** overfull
1edd4 20 28 68 61 73 20 6f 6e 65 20 6f 72 20 6d 6f 72   (has one or mor
1edd5 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  e overflow pages
1edd6 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 63  )..**.** A new c
1edd7 68 69 6c 64 20 70 61 67 65 20 69 73 20 61 6c 6c  hild page is all
1edd8 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 63  ocated and the c
1edd9 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63  ontents of the c
1edda 75 72 72 65 6e 74 20 72 6f 6f 74 0a 2a 2a 20 70  urrent root.** p
1eddb 61 67 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 6f  age, including o
1eddc 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2c 20 61  verflow cells, a
1eddd 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74  re copied into t
1edde 68 65 20 63 68 69 6c 64 2e 20 54 68 65 20 72 6f  he child. The ro
1eddf 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 74 68  ot.** page is th
1ede0 65 6e 20 6f 76 65 72 77 72 69 74 74 65 6e 20 74  en overwritten t
1ede1 6f 20 6d 61 6b 65 20 69 74 20 61 6e 20 65 6d 70  o make it an emp
1ede2 74 79 20 70 61 67 65 20 77 69 74 68 20 74 68 65  ty page with the
1ede3 20 72 69 67 68 74 2d 63 68 69 6c 64 20 0a 2a 2a   right-child .**
1ede4 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 69 6e   pointer pointin
1ede5 67 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67  g to the new pag
1ede6 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65 20  e..**.** Before 
1ede7 72 65 74 75 72 6e 69 6e 67 2c 20 61 6c 6c 20 70  returning, all p
1ede8 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69  ointer-map entri
1ede9 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  es corresponding
1edea 20 74 6f 20 70 61 67 65 73 20 0a 2a 2a 20 74 68   to pages .** th
1edeb 61 74 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64  at the new child
1edec 2d 70 61 67 65 20 6e 6f 77 20 63 6f 6e 74 61 69  -page now contai
1eded 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61  ns pointers to a
1edee 72 65 20 75 70 64 61 74 65 64 2e 20 54 68 65 0a  re updated. The.
1edef 2a 2a 20 65 6e 74 72 79 20 63 6f 72 72 65 73 70  ** entry corresp
1edf0 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 6e 65  onding to the ne
1edf1 77 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 6f  w right-child po
1edf2 69 6e 74 65 72 20 6f 66 20 74 68 65 20 72 6f 6f  inter of the roo
1edf3 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 6c 73  t.** page is als
1edf4 6f 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a  o updated..**.**
1edf5 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
1edf6 2a 70 70 43 68 69 6c 64 20 69 73 20 73 65 74 20  *ppChild is set 
1edf7 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 66  to contain a ref
1edf8 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 63 68  erence to the ch
1edf9 69 6c 64 20 0a 2a 2a 20 70 61 67 65 20 61 6e 64  ild .** page and
1edfa 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
1edfb 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20  turned. In this 
1edfc 63 61 73 65 20 74 68 65 20 63 61 6c 6c 65 72 20  case the caller 
1edfd 69 73 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 74  is required.** t
1edfe 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 61  o call releasePa
1edff 67 65 28 29 20 6f 6e 20 2a 70 70 43 68 69 6c 64  ge() on *ppChild
1ee00 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 2e 20 49   exactly once. I
1ee01 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
1ee02 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63  s,.** an error c
1ee03 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
1ee04 61 6e 64 20 2a 70 70 43 68 69 6c 64 20 69 73 20  and *ppChild is 
1ee05 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74 61  set to 0..*/.sta
1ee06 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f  tic int balance_
1ee07 64 65 65 70 65 72 28 4d 65 6d 50 61 67 65 20 2a  deeper(MemPage *
1ee08 70 52 6f 6f 74 2c 20 4d 65 6d 50 61 67 65 20 2a  pRoot, MemPage *
1ee09 2a 70 70 43 68 69 6c 64 29 7b 0a 20 20 69 6e 74  *ppChild){.  int
1ee0a 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
1ee0b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1ee0c 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d  eturn value from
1ee0d 20 73 75 62 70 72 6f 63 65 64 75 72 65 73 20 2a   subprocedures *
1ee0e 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 43 68  /.  MemPage *pCh
1ee0f 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ild = 0;        
1ee10 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1ee11 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67   a new child pag
1ee12 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
1ee13 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 20  Child = 0;      
1ee14 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
1ee15 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20  mber of the new 
1ee16 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a 20 20  child page */.  
1ee17 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
1ee18 70 52 6f 6f 74 2d 3e 70 42 74 3b 20 20 20 20 2f  pRoot->pBt;    /
1ee19 2a 20 54 68 65 20 42 54 72 65 65 20 2a 2f 0a 0a  * The BTree */..
1ee1a 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d    assert( pRoot-
1ee1b 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a  >nOverflow>0 );.
1ee1c 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1ee1d 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
1ee1e 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f  ->mutex) );..  /
1ee1f 2a 20 4d 61 6b 65 20 70 52 6f 6f 74 2c 20 74 68  * Make pRoot, th
1ee20 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74  e root page of t
1ee21 68 65 20 62 2d 74 72 65 65 2c 20 77 72 69 74 61  he b-tree, writa
1ee22 62 6c 65 2e 20 41 6c 6c 6f 63 61 74 65 20 61 20  ble. Allocate a 
1ee23 6e 65 77 20 0a 20 20 2a 2a 20 70 61 67 65 20 74  new .  ** page t
1ee24 68 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20  hat will become 
1ee25 74 68 65 20 6e 65 77 20 72 69 67 68 74 2d 63 68  the new right-ch
1ee26 69 6c 64 20 6f 66 20 70 50 61 67 65 2e 20 43 6f  ild of pPage. Co
1ee27 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a  py the contents.
1ee28 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e 6f 64 65    ** of the node
1ee29 20 73 74 6f 72 65 64 20 6f 6e 20 70 52 6f 6f 74   stored on pRoot
1ee2a 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 20 63 68   into the new ch
1ee2b 69 6c 64 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20  ild page..  */. 
1ee2c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1ee2d 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70  erWrite(pRoot->p
1ee2e 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  DbPage);.  if( r
1ee2f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1ee30 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74      rc = allocat
1ee31 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 26  eBtreePage(pBt,&
1ee32 70 43 68 69 6c 64 2c 26 70 67 6e 6f 43 68 69 6c  pChild,&pgnoChil
1ee33 64 2c 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 30 29  d,pRoot->pgno,0)
1ee34 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64 65 43 6f  ;.    copyNodeCo
1ee35 6e 74 65 6e 74 28 70 52 6f 6f 74 2c 20 70 43 68  ntent(pRoot, pCh
1ee36 69 6c 64 2c 20 26 72 63 29 3b 0a 20 20 20 20 69  ild, &rc);.    i
1ee37 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20  f( ISAUTOVACUUM 
1ee38 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50  ){.      ptrmapP
1ee39 75 74 28 70 42 74 2c 20 70 67 6e 6f 43 68 69 6c  ut(pBt, pgnoChil
1ee3a 64 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c  d, PTRMAP_BTREE,
1ee3b 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 20 26 72   pRoot->pgno, &r
1ee3c 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  c);.    }.  }.  
1ee3d 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70  if( rc ){.    *p
1ee3e 70 43 68 69 6c 64 20 3d 20 30 3b 0a 20 20 20 20  pChild = 0;.    
1ee3f 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 68 69  releasePage(pChi
1ee40 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ld);.    return 
1ee41 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rc;.  }.  assert
1ee42 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
1ee43 77 72 69 74 65 61 62 6c 65 28 70 43 68 69 6c 64  writeable(pChild
1ee44 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
1ee45 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
1ee46 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
1ee47 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 20  pRoot->pDbPage) 
1ee48 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 68  );.  assert( pCh
1ee49 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 52 6f 6f  ild->nCell==pRoo
1ee4a 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 54  t->nCell );..  T
1ee4b 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20  RACE(("BALANCE: 
1ee4c 63 6f 70 79 20 72 6f 6f 74 20 25 64 20 69 6e 74  copy root %d int
1ee4d 6f 20 25 64 5c 6e 22 2c 20 70 52 6f 6f 74 2d 3e  o %d\n", pRoot->
1ee4e 70 67 6e 6f 2c 20 70 43 68 69 6c 64 2d 3e 70 67  pgno, pChild->pg
1ee4f 6e 6f 29 29 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79  no));..  /* Copy
1ee50 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65   the overflow ce
1ee51 6c 6c 73 20 66 72 6f 6d 20 70 52 6f 6f 74 20 74  lls from pRoot t
1ee52 6f 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 6d 65  o pChild */.  me
1ee53 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e 61 69 4f  mcpy(pChild->aiO
1ee54 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 61 69 4f 76  vfl, pRoot->aiOv
1ee55 66 6c 2c 0a 20 20 20 20 20 20 20 20 20 70 52 6f  fl,.         pRo
1ee56 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2a 73 69  ot->nOverflow*si
1ee57 7a 65 6f 66 28 70 52 6f 6f 74 2d 3e 61 69 4f 76  zeof(pRoot->aiOv
1ee58 66 6c 5b 30 5d 29 29 3b 0a 20 20 6d 65 6d 63 70  fl[0]));.  memcp
1ee59 79 28 70 43 68 69 6c 64 2d 3e 61 70 4f 76 66 6c  y(pChild->apOvfl
1ee5a 2c 20 70 52 6f 6f 74 2d 3e 61 70 4f 76 66 6c 2c  , pRoot->apOvfl,
1ee5b 0a 20 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d  .         pRoot-
1ee5c 3e 6e 4f 76 65 72 66 6c 6f 77 2a 73 69 7a 65 6f  >nOverflow*sizeo
1ee5d 66 28 70 52 6f 6f 74 2d 3e 61 70 4f 76 66 6c 5b  f(pRoot->apOvfl[
1ee5e 30 5d 29 29 3b 0a 20 20 70 43 68 69 6c 64 2d 3e  0]));.  pChild->
1ee5f 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 52 6f 6f  nOverflow = pRoo
1ee60 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 0a 20  t->nOverflow;.. 
1ee61 20 2f 2a 20 5a 65 72 6f 20 74 68 65 20 63 6f 6e   /* Zero the con
1ee62 74 65 6e 74 73 20 6f 66 20 70 52 6f 6f 74 2e 20  tents of pRoot. 
1ee63 54 68 65 6e 20 69 6e 73 74 61 6c 6c 20 70 43 68  Then install pCh
1ee64 69 6c 64 20 61 73 20 74 68 65 20 72 69 67 68 74  ild as the right
1ee65 2d 63 68 69 6c 64 2e 20 2a 2f 0a 20 20 7a 65 72  -child. */.  zer
1ee66 6f 50 61 67 65 28 70 52 6f 6f 74 2c 20 70 43 68  oPage(pRoot, pCh
1ee67 69 6c 64 2d 3e 61 44 61 74 61 5b 30 5d 20 26 20  ild->aData[0] & 
1ee68 7e 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 70 75  ~PTF_LEAF);.  pu
1ee69 74 34 62 79 74 65 28 26 70 52 6f 6f 74 2d 3e 61  t4byte(&pRoot->a
1ee6a 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 72 4f  Data[pRoot->hdrO
1ee6b 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 43 68  ffset+8], pgnoCh
1ee6c 69 6c 64 29 3b 0a 0a 20 20 2a 70 70 43 68 69 6c  ild);..  *ppChil
1ee6d 64 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20 72 65  d = pChild;.  re
1ee6e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1ee6f 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67  }../*.** The pag
1ee70 65 20 74 68 61 74 20 70 43 75 72 20 63 75 72 72  e that pCur curr
1ee71 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20  ently points to 
1ee72 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 6d 6f  has just been mo
1ee73 64 69 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f 6d  dified in.** som
1ee74 65 20 77 61 79 2e 20 54 68 69 73 20 66 75 6e 63  e way. This func
1ee75 74 69 6f 6e 20 66 69 67 75 72 65 73 20 6f 75 74  tion figures out
1ee76 20 69 66 20 74 68 69 73 20 6d 6f 64 69 66 69 63   if this modific
1ee77 61 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65 0a  ation means the.
1ee78 2a 2a 20 74 72 65 65 20 6e 65 65 64 73 20 74 6f  ** tree needs to
1ee79 20 62 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 6e   be balanced, an
1ee7a 64 20 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 68  d if so calls th
1ee7b 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 62 61  e appropriate ba
1ee7c 6c 61 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75 74  lancing .** rout
1ee7d 69 6e 65 2e 20 42 61 6c 61 6e 63 69 6e 67 20 72  ine. Balancing r
1ee7e 6f 75 74 69 6e 65 73 20 61 72 65 3a 0a 2a 2a 0a  outines are:.**.
1ee7f 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f 71 75 69  **   balance_qui
1ee80 63 6b 28 29 0a 2a 2a 20 20 20 62 61 6c 61 6e 63  ck().**   balanc
1ee81 65 5f 64 65 65 70 65 72 28 29 0a 2a 2a 20 20 20  e_deeper().**   
1ee82 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28  balance_nonroot(
1ee83 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ).*/.static int 
1ee84 62 61 6c 61 6e 63 65 28 42 74 43 75 72 73 6f 72  balance(BtCursor
1ee85 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72   *pCur){.  int r
1ee86 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1ee87 20 63 6f 6e 73 74 20 69 6e 74 20 6e 4d 69 6e 20   const int nMin 
1ee88 3d 20 70 43 75 72 2d 3e 70 42 74 2d 3e 75 73 61  = pCur->pBt->usa
1ee89 62 6c 65 53 69 7a 65 20 2a 20 32 20 2f 20 33 3b  bleSize * 2 / 3;
1ee8a 0a 20 20 75 38 20 61 42 61 6c 61 6e 63 65 51 75  .  u8 aBalanceQu
1ee8b 69 63 6b 53 70 61 63 65 5b 31 33 5d 3b 0a 20 20  ickSpace[13];.  
1ee8c 75 38 20 2a 70 46 72 65 65 20 3d 20 30 3b 0a 0a  u8 *pFree = 0;..
1ee8d 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20    TESTONLY( int 
1ee8e 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 5f 63 61  balance_quick_ca
1ee8f 6c 6c 65 64 20 3d 20 30 20 29 3b 0a 20 20 54 45  lled = 0 );.  TE
1ee90 53 54 4f 4e 4c 59 28 20 69 6e 74 20 62 61 6c 61  STONLY( int bala
1ee91 6e 63 65 5f 64 65 65 70 65 72 5f 63 61 6c 6c 65  nce_deeper_calle
1ee92 64 20 3d 20 30 20 29 3b 0a 0a 20 20 64 6f 20 7b  d = 0 );..  do {
1ee93 0a 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d  .    int iPage =
1ee94 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20   pCur->iPage;.  
1ee95 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
1ee96 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
1ee97 69 50 61 67 65 5d 3b 0a 0a 20 20 20 20 69 66 28  iPage];..    if(
1ee98 20 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20   iPage==0 ){.   
1ee99 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f     if( pPage->nO
1ee9a 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20  verflow ){.     
1ee9b 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70     /* The root p
1ee9c 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65  age of the b-tre
1ee9d 65 20 69 73 20 6f 76 65 72 66 75 6c 6c 2e 20 49  e is overfull. I
1ee9e 6e 20 74 68 69 73 20 63 61 73 65 20 63 61 6c 6c  n this case call
1ee9f 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
1eea0 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29  balance_deeper()
1eea1 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 72 65   function to cre
1eea2 61 74 65 20 61 20 6e 65 77 20 63 68 69 6c 64 20  ate a new child 
1eea3 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67  for the root-pag
1eea4 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64  e.        ** and
1eea5 20 63 6f 70 79 20 74 68 65 20 63 75 72 72 65 6e   copy the curren
1eea6 74 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  t contents of th
1eea7 65 20 72 6f 6f 74 2d 70 61 67 65 20 74 6f 20 69  e root-page to i
1eea8 74 2e 20 54 68 65 0a 20 20 20 20 20 20 20 20 2a  t. The.        *
1eea9 2a 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e  * next iteration
1eeaa 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 20   of the do-loop 
1eeab 77 69 6c 6c 20 62 61 6c 61 6e 63 65 20 74 68 65  will balance the
1eeac 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 20   child page..   
1eead 20 20 20 20 20 2a 2f 20 0a 20 20 20 20 20 20 20       */ .       
1eeae 20 61 73 73 65 72 74 28 20 28 62 61 6c 61 6e 63   assert( (balanc
1eeaf 65 5f 64 65 65 70 65 72 5f 63 61 6c 6c 65 64 2b  e_deeper_called+
1eeb0 2b 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  +)==0 );.       
1eeb1 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 64 65   rc = balance_de
1eeb2 65 70 65 72 28 70 50 61 67 65 2c 20 26 70 43 75  eper(pPage, &pCu
1eeb3 72 2d 3e 61 70 50 61 67 65 5b 31 5d 29 3b 0a 20  r->apPage[1]);. 
1eeb4 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
1eeb5 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1eeb6 20 20 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67        pCur->iPag
1eeb7 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  e = 1;.         
1eeb8 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20   pCur->aiIdx[0] 
1eeb9 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70  = 0;.          p
1eeba 43 75 72 2d 3e 61 69 49 64 78 5b 31 5d 20 3d 20  Cur->aiIdx[1] = 
1eebb 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  0;.          ass
1eebc 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67  ert( pCur->apPag
1eebd 65 5b 31 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20  e[1]->nOverflow 
1eebe 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1eebf 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1eec0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
1eec1 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
1eec2 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d  Page->nOverflow=
1eec3 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 6e 46 72  =0 && pPage->nFr
1eec4 65 65 3c 3d 6e 4d 69 6e 20 29 7b 0a 20 20 20 20  ee<=nMin ){.    
1eec5 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 65 6c    break;.    }el
1eec6 73 65 7b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67  se{.      MemPag
1eec7 65 20 2a 20 63 6f 6e 73 74 20 70 50 61 72 65 6e  e * const pParen
1eec8 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  t = pCur->apPage
1eec9 5b 69 50 61 67 65 2d 31 5d 3b 0a 20 20 20 20 20  [iPage-1];.     
1eeca 20 69 6e 74 20 63 6f 6e 73 74 20 69 49 64 78 20   int const iIdx 
1eecb 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50  = pCur->aiIdx[iP
1eecc 61 67 65 2d 31 5d 3b 0a 0a 20 20 20 20 20 20 72  age-1];..      r
1eecd 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1eece 57 72 69 74 65 28 70 50 61 72 65 6e 74 2d 3e 70  Write(pParent->p
1eecf 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  DbPage);.      i
1eed0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1eed1 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   ){.#ifndef SQLI
1eed2 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c  TE_OMIT_QUICKBAL
1eed3 41 4e 43 45 0a 20 20 20 20 20 20 20 20 69 66 28  ANCE.        if(
1eed4 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 0a   pPage->hasData.
1eed5 20 20 20 20 20 20 20 20 20 26 26 20 70 50 61 67           && pPag
1eed6 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 0a  e->nOverflow==1.
1eed7 20 20 20 20 20 20 20 20 20 26 26 20 70 50 61 67           && pPag
1eed8 65 2d 3e 61 69 4f 76 66 6c 5b 30 5d 3d 3d 70 50  e->aiOvfl[0]==pP
1eed9 61 67 65 2d 3e 6e 43 65 6c 6c 0a 20 20 20 20 20  age->nCell.     
1eeda 20 20 20 20 26 26 20 70 50 61 72 65 6e 74 2d 3e      && pParent->
1eedb 70 67 6e 6f 21 3d 31 0a 20 20 20 20 20 20 20 20  pgno!=1.        
1eedc 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65   && pParent->nCe
1eedd 6c 6c 3d 3d 69 49 64 78 0a 20 20 20 20 20 20 20  ll==iIdx.       
1eede 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
1eedf 20 43 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 71 75   Call balance_qu
1eee0 69 63 6b 28 29 20 74 6f 20 63 72 65 61 74 65 20  ick() to create 
1eee1 61 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 6f 66  a new sibling of
1eee2 20 70 50 61 67 65 20 6f 6e 20 77 68 69 63 68 0a   pPage on which.
1eee3 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20            ** to 
1eee4 73 74 6f 72 65 20 74 68 65 20 6f 76 65 72 66 6c  store the overfl
1eee5 6f 77 20 63 65 6c 6c 2e 20 62 61 6c 61 6e 63 65  ow cell. balance
1eee6 5f 71 75 69 63 6b 28 29 20 69 6e 73 65 72 74 73  _quick() inserts
1eee7 20 61 20 6e 65 77 20 63 65 6c 6c 0a 20 20 20 20   a new cell.    
1eee8 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 70 50        ** into pP
1eee9 61 72 65 6e 74 2c 20 77 68 69 63 68 20 6d 61 79  arent, which may
1eeea 20 63 61 75 73 65 20 70 50 61 72 65 6e 74 20 6f   cause pParent o
1eeeb 76 65 72 66 6c 6f 77 2e 20 49 66 20 74 68 69 73  verflow. If this
1eeec 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61  .          ** ha
1eeed 70 70 65 6e 73 2c 20 74 68 65 20 6e 65 78 74 20  ppens, the next 
1eeee 69 6e 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68  interation of th
1eeef 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62  e do-loop will b
1eef0 61 6c 61 6e 63 65 20 70 50 61 72 65 6e 74 20 0a  alance pParent .
1eef1 20 20 20 20 20 20 20 20 20 20 2a 2a 20 75 73 65            ** use
1eef2 20 65 69 74 68 65 72 20 62 61 6c 61 6e 63 65 5f   either balance_
1eef3 6e 6f 6e 72 6f 6f 74 28 29 20 6f 72 20 62 61 6c  nonroot() or bal
1eef4 61 6e 63 65 5f 64 65 65 70 65 72 28 29 2e 20 55  ance_deeper(). U
1eef5 6e 74 69 6c 20 74 68 69 73 0a 20 20 20 20 20 20  ntil this.      
1eef6 20 20 20 20 2a 2a 20 68 61 70 70 65 6e 73 2c 20      ** happens, 
1eef7 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c  the overflow cel
1eef8 6c 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74  l is stored in t
1eef9 68 65 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b  he aBalanceQuick
1eefa 53 70 61 63 65 5b 5d 0a 20 20 20 20 20 20 20 20  Space[].        
1eefb 20 20 2a 2a 20 62 75 66 66 65 72 2e 20 0a 20 20    ** buffer. .  
1eefc 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
1eefd 20 20 20 20 20 2a 2a 20 54 68 65 20 70 75 72 70       ** The purp
1eefe 6f 73 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  ose of the follo
1eeff 77 69 6e 67 20 61 73 73 65 72 74 28 29 20 69 73  wing assert() is
1ef00 20 74 6f 20 63 68 65 63 6b 20 74 68 61 74 20 6f   to check that o
1ef01 6e 6c 79 20 61 0a 20 20 20 20 20 20 20 20 20 20  nly a.          
1ef02 2a 2a 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20 74  ** single call t
1ef03 6f 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28  o balance_quick(
1ef04 29 20 69 73 20 6d 61 64 65 20 66 6f 72 20 65 61  ) is made for ea
1ef05 63 68 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 0a  ch call to this.
1ef06 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e            ** fun
1ef07 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 77  ction. If this w
1ef08 65 72 65 20 6e 6f 74 20 76 65 72 69 66 69 65 64  ere not verified
1ef09 2c 20 61 20 73 75 62 74 6c 65 20 62 75 67 20 69  , a subtle bug i
1ef0a 6e 76 6f 6c 76 69 6e 67 20 72 65 75 73 65 0a 20  nvolving reuse. 
1ef0b 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74           ** of t
1ef0c 68 65 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b  he aBalanceQuick
1ef0d 53 70 61 63 65 5b 5d 20 6d 69 67 68 74 20 73 6e  Space[] might sn
1ef0e 65 61 6b 20 69 6e 2e 0a 20 20 20 20 20 20 20 20  eak in..        
1ef0f 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61    */.          a
1ef10 73 73 65 72 74 28 20 28 62 61 6c 61 6e 63 65 5f  ssert( (balance_
1ef11 71 75 69 63 6b 5f 63 61 6c 6c 65 64 2b 2b 29 3d  quick_called++)=
1ef12 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =0 );.          
1ef13 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 71 75 69  rc = balance_qui
1ef14 63 6b 28 70 50 61 72 65 6e 74 2c 20 70 50 61 67  ck(pParent, pPag
1ef15 65 2c 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b  e, aBalanceQuick
1ef16 53 70 61 63 65 29 3b 0a 20 20 20 20 20 20 20 20  Space);.        
1ef17 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20  }else.#endif.   
1ef18 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20       {.         
1ef19 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65   /* In this case
1ef1a 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 6e  , call balance_n
1ef1b 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 72 65 64 69  onroot() to redi
1ef1c 73 74 72 69 62 75 74 65 20 63 65 6c 6c 73 0a 20  stribute cells. 
1ef1d 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 74 77           ** betw
1ef1e 65 65 6e 20 70 50 61 67 65 20 61 6e 64 20 75 70  een pPage and up
1ef1f 20 74 6f 20 32 20 6f 66 20 69 74 73 20 73 69 62   to 2 of its sib
1ef20 6c 69 6e 67 20 70 61 67 65 73 2e 20 54 68 69 73  ling pages. This
1ef21 20 69 6e 76 6f 6c 76 65 73 0a 20 20 20 20 20 20   involves.      
1ef22 20 20 20 20 2a 2a 20 6d 6f 64 69 66 79 69 6e 67      ** modifying
1ef23 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
1ef24 20 70 50 61 72 65 6e 74 2c 20 77 68 69 63 68 20   pParent, which 
1ef25 6d 61 79 20 63 61 75 73 65 20 70 50 61 72 65 6e  may cause pParen
1ef26 74 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 2a  t to.          *
1ef27 2a 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c  * become overful
1ef28 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20  l or underfull. 
1ef29 54 68 65 20 6e 65 78 74 20 69 74 65 72 61 74 69  The next iterati
1ef2a 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f  on of the do-loo
1ef2b 70 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77  p.          ** w
1ef2c 69 6c 6c 20 62 61 6c 61 6e 63 65 20 74 68 65 20  ill balance the 
1ef2d 70 61 72 65 6e 74 20 70 61 67 65 20 74 6f 20 63  parent page to c
1ef2e 6f 72 72 65 63 74 20 74 68 69 73 2e 0a 20 20 20  orrect this..   
1ef2f 20 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20         ** .     
1ef30 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 70       ** If the p
1ef31 61 72 65 6e 74 20 70 61 67 65 20 62 65 63 6f 6d  arent page becom
1ef32 65 73 20 6f 76 65 72 66 75 6c 6c 2c 20 74 68 65  es overfull, the
1ef33 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 6f   overflow cell o
1ef34 72 20 63 65 6c 6c 73 0a 20 20 20 20 20 20 20 20  r cells.        
1ef35 20 20 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 20    ** are stored 
1ef36 69 6e 20 74 68 65 20 70 53 70 61 63 65 20 62 75  in the pSpace bu
1ef37 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 69  ffer allocated i
1ef38 6d 6d 65 64 69 61 74 65 6c 79 20 62 65 6c 6f 77  mmediately below
1ef39 2e 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  . .          ** 
1ef3a 41 20 73 75 62 73 65 71 75 65 6e 74 20 69 74 65  A subsequent ite
1ef3b 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f  ration of the do
1ef3c 2d 6c 6f 6f 70 20 77 69 6c 6c 20 64 65 61 6c 20  -loop will deal 
1ef3d 77 69 74 68 20 74 68 69 73 20 62 79 0a 20 20 20  with this by.   
1ef3e 20 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 69 6e         ** callin
1ef3f 67 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  g balance_nonroo
1ef40 74 28 29 20 28 62 61 6c 61 6e 63 65 5f 64 65 65  t() (balance_dee
1ef41 70 65 72 28 29 20 6d 61 79 20 62 65 20 63 61 6c  per() may be cal
1ef42 6c 65 64 20 66 69 72 73 74 2c 0a 20 20 20 20 20  led first,.     
1ef43 20 20 20 20 20 2a 2a 20 62 75 74 20 69 74 20 64       ** but it d
1ef44 6f 65 73 6e 27 74 20 64 65 61 6c 20 77 69 74 68  oesn't deal with
1ef45 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20   overflow cells 
1ef46 2d 20 6a 75 73 74 20 6d 6f 76 65 73 20 74 68 65  - just moves the
1ef47 6d 20 74 6f 20 61 0a 20 20 20 20 20 20 20 20 20  m to a.         
1ef48 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 70 61   ** different pa
1ef49 67 65 29 2e 20 4f 6e 63 65 20 74 68 69 73 20 73  ge). Once this s
1ef4a 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74  ubsequent call t
1ef4b 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  o balance_nonroo
1ef4c 74 28 29 20 0a 20 20 20 20 20 20 20 20 20 20 2a  t() .          *
1ef4d 2a 20 68 61 73 20 63 6f 6d 70 6c 65 74 65 64 2c  * has completed,
1ef4e 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 72   it is safe to r
1ef4f 65 6c 65 61 73 65 20 74 68 65 20 70 53 70 61 63  elease the pSpac
1ef50 65 20 62 75 66 66 65 72 20 75 73 65 64 20 62 79  e buffer used by
1ef51 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68  .          ** th
1ef52 65 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2c  e previous call,
1ef53 20 61 73 20 74 68 65 20 6f 76 65 72 66 6c 6f 77   as the overflow
1ef54 20 63 65 6c 6c 20 64 61 74 61 20 77 69 6c 6c 20   cell data will 
1ef55 68 61 76 65 20 62 65 65 6e 20 0a 20 20 20 20 20  have been .     
1ef56 20 20 20 20 20 2a 2a 20 63 6f 70 69 65 64 20 65       ** copied e
1ef57 69 74 68 65 72 20 69 6e 74 6f 20 74 68 65 20 62  ither into the b
1ef58 6f 64 79 20 6f 66 20 61 20 64 61 74 61 62 61 73  ody of a databas
1ef59 65 20 70 61 67 65 20 6f 72 20 69 6e 74 6f 20 74  e page or into t
1ef5a 68 65 20 6e 65 77 0a 20 20 20 20 20 20 20 20 20  he new.         
1ef5b 20 2a 2a 20 70 53 70 61 63 65 20 62 75 66 66 65   ** pSpace buffe
1ef5c 72 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20  r passed to the 
1ef5d 6c 61 74 74 65 72 20 63 61 6c 6c 20 74 6f 20 62  latter call to b
1ef5e 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29  alance_nonroot()
1ef5f 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
1ef60 20 20 20 20 20 20 20 20 20 75 38 20 2a 70 53 70           u8 *pSp
1ef61 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67  ace = sqlite3Pag
1ef62 65 4d 61 6c 6c 6f 63 28 70 43 75 72 2d 3e 70 42  eMalloc(pCur->pB
1ef63 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20  t->pageSize);.  
1ef64 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c          rc = bal
1ef65 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 50 61  ance_nonroot(pPa
1ef66 72 65 6e 74 2c 20 69 49 64 78 2c 20 70 53 70 61  rent, iIdx, pSpa
1ef67 63 65 2c 20 69 50 61 67 65 3d 3d 31 2c 20 70 43  ce, iPage==1, pC
1ef68 75 72 2d 3e 68 69 6e 74 73 29 3b 0a 20 20 20 20  ur->hints);.    
1ef69 20 20 20 20 20 20 69 66 28 20 70 46 72 65 65 20        if( pFree 
1ef6a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  ){.            /
1ef6b 2a 20 49 66 20 70 46 72 65 65 20 69 73 20 6e 6f  * If pFree is no
1ef6c 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74  t NULL, it point
1ef6d 73 20 74 6f 20 74 68 65 20 70 53 70 61 63 65 20  s to the pSpace 
1ef6e 62 75 66 66 65 72 20 75 73 65 64 20 0a 20 20 20  buffer used .   
1ef6f 20 20 20 20 20 20 20 20 20 2a 2a 20 62 79 20 61           ** by a
1ef70 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74   previous call t
1ef71 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  o balance_nonroo
1ef72 74 28 29 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74  t(). Its content
1ef73 73 20 61 72 65 0a 20 20 20 20 20 20 20 20 20 20  s are.          
1ef74 20 20 2a 2a 20 6e 6f 77 20 73 74 6f 72 65 64 20    ** now stored 
1ef75 65 69 74 68 65 72 20 6f 6e 20 72 65 61 6c 20 64  either on real d
1ef76 61 74 61 62 61 73 65 20 70 61 67 65 73 20 6f 72  atabase pages or
1ef77 20 77 69 74 68 69 6e 20 74 68 65 20 0a 20 20 20   within the .   
1ef78 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 77 20           ** new 
1ef79 70 53 70 61 63 65 20 62 75 66 66 65 72 2c 20 73  pSpace buffer, s
1ef7a 6f 20 69 74 20 6d 61 79 20 62 65 20 73 61 66 65  o it may be safe
1ef7b 6c 79 20 66 72 65 65 64 20 68 65 72 65 2e 20 2a  ly freed here. *
1ef7c 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  /.            sq
1ef7d 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 46  lite3PageFree(pF
1ef7e 72 65 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ree);.          
1ef7f 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  }..          /* 
1ef80 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65  The pSpace buffe
1ef81 72 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20  r will be freed 
1ef82 61 66 74 65 72 20 74 68 65 20 6e 65 78 74 20 63  after the next c
1ef83 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 20 20 20  all to.         
1ef84 20 2a 2a 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72   ** balance_nonr
1ef85 6f 6f 74 28 29 2c 20 6f 72 20 6a 75 73 74 20 62  oot(), or just b
1ef86 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74  efore this funct
1ef87 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 77 68 69  ion returns, whi
1ef88 63 68 65 76 65 72 0a 20 20 20 20 20 20 20 20 20  chever.         
1ef89 20 2a 2a 20 63 6f 6d 65 73 20 66 69 72 73 74 2e   ** comes first.
1ef8a 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 46   */.          pF
1ef8b 72 65 65 20 3d 20 70 53 70 61 63 65 3b 0a 20 20  ree = pSpace;.  
1ef8c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1ef8d 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f  .      pPage->nO
1ef8e 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20  verflow = 0;..  
1ef8f 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20      /* The next 
1ef90 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  iteration of the
1ef91 20 64 6f 2d 6c 6f 6f 70 20 62 61 6c 61 6e 63 65   do-loop balance
1ef92 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  s the parent pag
1ef93 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65  e. */.      rele
1ef94 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
1ef95 20 20 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67        pCur->iPag
1ef96 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68  e--;.    }.  }wh
1ef97 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
1ef98 4f 4b 20 29 3b 0a 0a 20 20 69 66 28 20 70 46 72  OK );..  if( pFr
1ef99 65 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ee ){.    sqlite
1ef9a 33 50 61 67 65 46 72 65 65 28 70 46 72 65 65 29  3PageFree(pFree)
1ef9b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1ef9c 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73  c;.}.../*.** Ins
1ef9d 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64  ert a new record
1ef9e 20 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e   into the BTree.
1ef9f 20 20 54 68 65 20 6b 65 79 20 69 73 20 67 69 76    The key is giv
1efa0 65 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79  en by (pKey,nKey
1efa1 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74  ).** and the dat
1efa2 61 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70  a is given by (p
1efa3 44 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68  Data,nData).  Th
1efa4 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64  e cursor is used
1efa5 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69   only to.** defi
1efa6 6e 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 68  ne what table th
1efa7 65 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20  e record should 
1efa8 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  be inserted into
1efa9 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a  .  The cursor.**
1efaa 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
1efab 67 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f  g at a random lo
1efac 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f  cation..**.** Fo
1efad 72 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c  r an INTKEY tabl
1efae 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79  e, only the nKey
1efaf 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65   value of the ke
1efb0 79 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79  y is used.  pKey
1efb1 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20   is.** ignored. 
1efb2 20 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20   For a ZERODATA 
1efb3 74 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61  table, the pData
1efb4 20 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62   and nData are b
1efb5 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a  oth ignored..**.
1efb6 2a 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52 65  ** If the seekRe
1efb7 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69  sult parameter i
1efb8 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
1efb9 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 63 61   a successful ca
1efba 6c 6c 20 74 6f 0a 2a 2a 20 4d 6f 76 65 74 6f 55  ll to.** MovetoU
1efbb 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 73 65 65  npacked() to see
1efbc 6b 20 63 75 72 73 6f 72 20 70 43 75 72 20 74 6f  k cursor pCur to
1efbd 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 68 61   (pKey, nKey) ha
1efbe 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65  s already.** bee
1efbf 6e 20 70 65 72 66 6f 72 6d 65 64 2e 20 73 65 65  n performed. see
1efc0 6b 52 65 73 75 6c 74 20 69 73 20 74 68 65 20 73  kResult is the s
1efc1 65 61 72 63 68 20 72 65 73 75 6c 74 20 72 65 74  earch result ret
1efc2 75 72 6e 65 64 20 28 61 20 6e 65 67 61 74 69 76  urned (a negativ
1efc3 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 69 66 20 70  e.** number if p
1efc4 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e  Cur points at an
1efc5 20 65 6e 74 72 79 20 74 68 61 74 20 69 73 20 73   entry that is s
1efc6 6d 61 6c 6c 65 72 20 74 68 61 6e 20 28 70 4b 65  maller than (pKe
1efc7 79 2c 20 6e 4b 65 79 29 2c 20 6f 72 0a 2a 2a 20  y, nKey), or.** 
1efc8 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65  a positive value
1efc9 20 69 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20   if pCur points 
1efca 61 74 20 61 6e 20 65 74 72 79 20 74 68 61 74 20  at an etry that 
1efcb 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a  is larger than .
1efcc 2a 2a 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 29  ** (pKey, nKey))
1efcd 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  . .**.** If the 
1efce 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d  seekResult param
1efcf 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  eter is non-zero
1efd0 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65  , then the calle
1efd1 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61  r guarantees tha
1efd2 74 0a 2a 2a 20 63 75 72 73 6f 72 20 70 43 75 72  t.** cursor pCur
1efd3 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 61 74 20   is pointing at 
1efd4 74 68 65 20 65 78 69 73 74 69 6e 67 20 63 6f 70  the existing cop
1efd5 79 20 6f 66 20 61 20 72 6f 77 20 74 68 61 74 20  y of a row that 
1efd6 69 73 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72  is to be.** over
1efd7 77 72 69 74 74 65 6e 2e 20 20 49 66 20 74 68 65  written.  If the
1efd8 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61   seekResult para
1efd9 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e  meter is 0, then
1efda 20 63 75 72 73 6f 72 20 70 43 75 72 20 6d 61 79   cursor pCur may
1efdb 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 6e 79  .** point to any
1efdc 20 65 6e 74 72 79 20 6f 72 20 74 6f 20 6e 6f 20   entry or to no 
1efdd 65 6e 74 72 79 20 61 74 20 61 6c 6c 20 61 6e 64  entry at all and
1efde 20 73 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   so this functio
1efdf 6e 20 68 61 73 20 74 6f 20 73 65 65 6b 0a 2a 2a  n has to seek.**
1efe0 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 66 6f   the cursor befo
1efe1 72 65 20 74 68 65 20 6e 65 77 20 6b 65 79 20 63  re the new key c
1efe2 61 6e 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a  an be inserted..
1efe3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1efe4 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1efe5 65 65 49 6e 73 65 72 74 28 0a 20 20 42 74 43 75  eeInsert(.  BtCu
1efe6 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20  rsor *pCur,     
1efe7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1efe8 73 65 72 74 20 64 61 74 61 20 69 6e 74 6f 20 74  sert data into t
1efe9 68 65 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73  he table of this
1efea 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e   cursor */.  con
1efeb 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
1efec 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54  64 nKey,    /* T
1efed 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 6e 65  he key of the ne
1efee 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f  w record */.  co
1efef 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c  nst void *pData,
1eff0 20 69 6e 74 20 6e 44 61 74 61 2c 20 20 2f 2a 20   int nData,  /* 
1eff1 54 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 20  The data of the 
1eff2 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  new record */.  
1eff3 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20  int nZero,      
1eff4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1eff5 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72  * Number of extr
1eff6 61 20 30 20 62 79 74 65 73 20 74 6f 20 61 70 70  a 0 bytes to app
1eff7 65 6e 64 20 74 6f 20 64 61 74 61 20 2a 2f 0a 20  end to data */. 
1eff8 20 69 6e 74 20 61 70 70 65 6e 64 42 69 61 73 2c   int appendBias,
1eff9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1effa 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
1effb 69 73 20 6c 69 6b 65 6c 79 20 61 6e 20 61 70 70  is likely an app
1effc 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65  end */.  int see
1effd 6b 52 65 73 75 6c 74 20 20 20 20 20 20 20 20 20  kResult         
1effe 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
1efff 74 20 6f 66 20 70 72 69 6f 72 20 4d 6f 76 65 74  t of prior Movet
1f000 6f 55 6e 70 61 63 6b 65 64 28 29 20 63 61 6c 6c  oUnpacked() call
1f001 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
1f002 0a 20 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65  .  int loc = see
1f003 6b 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20  kResult;        
1f004 20 20 2f 2a 20 2d 31 3a 20 62 65 66 6f 72 65 20    /* -1: before 
1f005 64 65 73 69 72 65 64 20 6c 6f 63 61 74 69 6f 6e  desired location
1f006 20 20 2b 31 3a 20 61 66 74 65 72 20 2a 2f 0a 20    +1: after */. 
1f007 20 69 6e 74 20 73 7a 4e 65 77 20 3d 20 30 3b 0a   int szNew = 0;.
1f008 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d    int idx;.  Mem
1f009 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 42  Page *pPage;.  B
1f00a 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e  tree *p = pCur->
1f00b 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72  pBtree;.  BtShar
1f00c 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
1f00d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
1f00e 72 20 2a 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75 6e  r *oldCell;.  un
1f00f 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6e 65 77  signed char *new
1f010 43 65 6c 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28  Cell = 0;..  if(
1f011 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
1f012 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20  URSOR_FAULT ){. 
1f013 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
1f014 3e 73 6b 69 70 4e 65 78 74 21 3d 53 51 4c 49 54  >skipNext!=SQLIT
1f015 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 65 74 75  E_OK );.    retu
1f016 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78  rn pCur->skipNex
1f017 74 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  t;.  }..  assert
1f018 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
1f019 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73  ex(pCur) );.  as
1f01a 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c  sert( pCur->wrFl
1f01b 61 67 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61  ag && pBt->inTra
1f01c 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
1f01d 57 52 49 54 45 0a 20 20 20 20 20 20 20 20 20 20  WRITE.          
1f01e 20 20 20 20 26 26 20 28 70 42 74 2d 3e 62 74 73      && (pBt->bts
1f01f 46 6c 61 67 73 20 26 20 42 54 53 5f 52 45 41 44  Flags & BTS_READ
1f020 5f 4f 4e 4c 59 29 3d 3d 30 20 29 3b 0a 20 20 61  _ONLY)==0 );.  a
1f021 73 73 65 72 74 28 20 68 61 73 53 68 61 72 65 64  ssert( hasShared
1f022 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70  CacheTableLock(p
1f023 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74  , pCur->pgnoRoot
1f024 2c 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f  , pCur->pKeyInfo
1f025 21 3d 30 2c 20 32 29 20 29 3b 0a 0a 20 20 2f 2a  !=0, 2) );..  /*
1f026 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65   Assert that the
1f027 20 63 61 6c 6c 65 72 20 68 61 73 20 62 65 65 6e   caller has been
1f028 20 63 6f 6e 73 69 73 74 65 6e 74 2e 20 49 66 20   consistent. If 
1f029 74 68 69 73 20 63 75 72 73 6f 72 20 77 61 73 20  this cursor was 
1f02a 6f 70 65 6e 65 64 0a 20 20 2a 2a 20 65 78 70 65  opened.  ** expe
1f02b 63 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 62  cting an index b
1f02c 2d 74 72 65 65 2c 20 74 68 65 6e 20 74 68 65 20  -tree, then the 
1f02d 63 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62 65  caller should be
1f02e 20 69 6e 73 65 72 74 69 6e 67 20 62 6c 6f 62 0a   inserting blob.
1f02f 20 20 2a 2a 20 6b 65 79 73 20 77 69 74 68 20 6e    ** keys with n
1f030 6f 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74  o associated dat
1f031 61 2e 20 49 66 20 74 68 65 20 63 75 72 73 6f 72  a. If the cursor
1f032 20 77 61 73 20 6f 70 65 6e 65 64 20 65 78 70 65   was opened expe
1f033 63 74 69 6e 67 20 61 6e 0a 20 20 2a 2a 20 69 6e  cting an.  ** in
1f034 74 6b 65 79 20 74 61 62 6c 65 2c 20 74 68 65 20  tkey table, the 
1f035 63 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62 65  caller should be
1f036 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 65 67   inserting integ
1f037 65 72 20 6b 65 79 73 20 77 69 74 68 20 61 0a 20  er keys with a. 
1f038 20 2a 2a 20 62 6c 6f 62 20 6f 66 20 61 73 73 6f   ** blob of asso
1f039 63 69 61 74 65 64 20 64 61 74 61 2e 20 20 2a 2f  ciated data.  */
1f03a 0a 20 20 61 73 73 65 72 74 28 20 28 70 4b 65 79  .  assert( (pKey
1f03b 3d 3d 30 29 3d 3d 28 70 43 75 72 2d 3e 70 4b 65  ==0)==(pCur->pKe
1f03c 79 49 6e 66 6f 3d 3d 30 29 20 29 3b 0a 0a 20 20  yInfo==0) );..  
1f03d 2f 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69  /* Save the posi
1f03e 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68  tions of any oth
1f03f 65 72 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20  er cursors open 
1f040 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 0a 20  on this table.. 
1f041 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 73 6f 6d 65   **.  ** In some
1f042 20 63 61 73 65 73 2c 20 74 68 65 20 63 61 6c 6c   cases, the call
1f043 20 74 6f 20 62 74 72 65 65 4d 6f 76 65 74 6f 28   to btreeMoveto(
1f044 29 20 62 65 6c 6f 77 20 69 73 20 61 20 6e 6f 2d  ) below is a no-
1f045 6f 70 2e 20 46 6f 72 0a 20 20 2a 2a 20 65 78 61  op. For.  ** exa
1f046 6d 70 6c 65 2c 20 77 68 65 6e 20 69 6e 73 65 72  mple, when inser
1f047 74 69 6e 67 20 64 61 74 61 20 69 6e 74 6f 20 61  ting data into a
1f048 20 74 61 62 6c 65 20 77 69 74 68 20 61 75 74 6f   table with auto
1f049 2d 67 65 6e 65 72 61 74 65 64 20 69 6e 74 65 67  -generated integ
1f04a 65 72 0a 20 20 2a 2a 20 6b 65 79 73 2c 20 74 68  er.  ** keys, th
1f04b 65 20 56 44 42 45 20 6c 61 79 65 72 20 69 6e 76  e VDBE layer inv
1f04c 6f 6b 65 73 20 73 71 6c 69 74 65 33 42 74 72 65  okes sqlite3Btre
1f04d 65 4c 61 73 74 28 29 20 74 6f 20 66 69 67 75 72  eLast() to figur
1f04e 65 20 6f 75 74 20 74 68 65 20 0a 20 20 2a 2a 20  e out the .  ** 
1f04f 69 6e 74 65 67 65 72 20 6b 65 79 20 74 6f 20 75  integer key to u
1f050 73 65 2e 20 49 74 20 74 68 65 6e 20 63 61 6c 6c  se. It then call
1f051 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  s this function 
1f052 74 6f 20 61 63 74 75 61 6c 6c 79 20 69 6e 73 65  to actually inse
1f053 72 74 20 74 68 65 20 0a 20 20 2a 2a 20 64 61 74  rt the .  ** dat
1f054 61 20 69 6e 74 6f 20 74 68 65 20 69 6e 74 6b 65  a into the intke
1f055 79 20 42 2d 54 72 65 65 2e 20 49 6e 20 74 68 69  y B-Tree. In thi
1f056 73 20 63 61 73 65 20 62 74 72 65 65 4d 6f 76 65  s case btreeMove
1f057 74 6f 28 29 20 72 65 63 6f 67 6e 69 7a 65 73 0a  to() recognizes.
1f058 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 63 75    ** that the cu
1f059 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20  rsor is already 
1f05a 77 68 65 72 65 20 69 74 20 6e 65 65 64 73 20 74  where it needs t
1f05b 6f 20 62 65 20 61 6e 64 20 72 65 74 75 72 6e 73  o be and returns
1f05c 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 64 6f   without.  ** do
1f05d 69 6e 67 20 61 6e 79 20 77 6f 72 6b 2e 20 54 6f  ing any work. To
1f05e 20 61 76 6f 69 64 20 74 68 77 61 72 74 69 6e 67   avoid thwarting
1f05f 20 74 68 65 73 65 20 6f 70 74 69 6d 69 7a 61 74   these optimizat
1f060 69 6f 6e 73 2c 20 69 74 20 69 73 20 69 6d 70 6f  ions, it is impo
1f061 72 74 61 6e 74 0a 20 20 2a 2a 20 6e 6f 74 20 74  rtant.  ** not t
1f062 6f 20 63 6c 65 61 72 20 74 68 65 20 63 75 72 73  o clear the curs
1f063 6f 72 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20  or here..  */.  
1f064 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73  rc = saveAllCurs
1f065 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70  ors(pBt, pCur->p
1f066 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 3b 0a  gnoRoot, pCur);.
1f067 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
1f068 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  n rc;..  /* If t
1f069 68 69 73 20 69 73 20 61 6e 20 69 6e 73 65 72 74  his is an insert
1f06a 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 62 2d   into a table b-
1f06b 74 72 65 65 2c 20 69 6e 76 61 6c 69 64 61 74 65  tree, invalidate
1f06c 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 0a 20   any incrblob . 
1f06d 20 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65 6e   ** cursors open
1f06e 20 6f 6e 20 74 68 65 20 72 6f 77 20 62 65 69 6e   on the row bein
1f06f 67 20 72 65 70 6c 61 63 65 64 20 28 61 73 73 75  g replaced (assu
1f070 6d 69 6e 67 20 74 68 69 73 20 69 73 20 61 20 72  ming this is a r
1f071 65 70 6c 61 63 65 0a 20 20 2a 2a 20 6f 70 65 72  eplace.  ** oper
1f072 61 74 69 6f 6e 20 2d 20 69 66 20 69 74 20 69 73  ation - if it is
1f073 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77   not, the follow
1f074 69 6e 67 20 69 73 20 61 20 6e 6f 2d 6f 70 29 2e  ing is a no-op).
1f075 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d    */.  if( pCur-
1f076 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 29 7b 0a  >pKeyInfo==0 ){.
1f077 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 49 6e      invalidateIn
1f078 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c  crblobCursors(p,
1f079 20 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a   nKey, 0);.  }..
1f07a 20 20 69 66 28 20 21 6c 6f 63 20 29 7b 0a 20 20    if( !loc ){.  
1f07b 20 20 72 63 20 3d 20 62 74 72 65 65 4d 6f 76 65    rc = btreeMove
1f07c 74 6f 28 70 43 75 72 2c 20 70 4b 65 79 2c 20 6e  to(pCur, pKey, n
1f07d 4b 65 79 2c 20 61 70 70 65 6e 64 42 69 61 73 2c  Key, appendBias,
1f07e 20 26 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28 20   &loc);.    if( 
1f07f 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
1f080 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43    }.  assert( pC
1f081 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
1f082 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 28 70 43 75  OR_VALID || (pCu
1f083 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
1f084 52 5f 49 4e 56 41 4c 49 44 20 26 26 20 6c 6f 63  R_INVALID && loc
1f085 29 20 29 3b 0a 0a 20 20 70 50 61 67 65 20 3d 20  ) );..  pPage = 
1f086 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
1f087 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73  r->iPage];.  ass
1f088 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b  ert( pPage->intK
1f089 65 79 20 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b  ey || nKey>=0 );
1f08a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
1f08b 2d 3e 6c 65 61 66 20 7c 7c 20 21 70 50 61 67 65  ->leaf || !pPage
1f08c 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 54  ->intKey );..  T
1f08d 52 41 43 45 28 28 22 49 4e 53 45 52 54 3a 20 74  RACE(("INSERT: t
1f08e 61 62 6c 65 3d 25 64 20 6e 6b 65 79 3d 25 6c 6c  able=%d nkey=%ll
1f08f 64 20 6e 64 61 74 61 3d 25 64 20 70 61 67 65 3d  d ndata=%d page=
1f090 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20  %d %s\n",.      
1f091 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f      pCur->pgnoRo
1f092 6f 74 2c 20 6e 4b 65 79 2c 20 6e 44 61 74 61 2c  ot, nKey, nData,
1f093 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 0a 20 20   pPage->pgno,.  
1f094 20 20 20 20 20 20 20 20 6c 6f 63 3d 3d 30 20 3f          loc==0 ?
1f095 20 22 6f 76 65 72 77 72 69 74 65 22 20 3a 20 22   "overwrite" : "
1f096 6e 65 77 20 65 6e 74 72 79 22 29 29 3b 0a 20 20  new entry"));.  
1f097 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
1f098 73 49 6e 69 74 20 29 3b 0a 20 20 61 6c 6c 6f 63  sInit );.  alloc
1f099 61 74 65 54 65 6d 70 53 70 61 63 65 28 70 42 74  ateTempSpace(pBt
1f09a 29 3b 0a 20 20 6e 65 77 43 65 6c 6c 20 3d 20 70  );.  newCell = p
1f09b 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20  Bt->pTmpSpace;. 
1f09c 20 69 66 28 20 6e 65 77 43 65 6c 6c 3d 3d 30 20   if( newCell==0 
1f09d 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
1f09e 4e 4f 4d 45 4d 3b 0a 20 20 72 63 20 3d 20 66 69  NOMEM;.  rc = fi
1f09f 6c 6c 49 6e 43 65 6c 6c 28 70 50 61 67 65 2c 20  llInCell(pPage, 
1f0a0 6e 65 77 43 65 6c 6c 2c 20 70 4b 65 79 2c 20 6e  newCell, pKey, n
1f0a1 4b 65 79 2c 20 70 44 61 74 61 2c 20 6e 44 61 74  Key, pData, nDat
1f0a2 61 2c 20 6e 5a 65 72 6f 2c 20 26 73 7a 4e 65 77  a, nZero, &szNew
1f0a3 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f  );.  if( rc ) go
1f0a4 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20  to end_insert;. 
1f0a5 20 61 73 73 65 72 74 28 20 73 7a 4e 65 77 3d 3d   assert( szNew==
1f0a6 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67  cellSizePtr(pPag
1f0a7 65 2c 20 6e 65 77 43 65 6c 6c 29 20 29 3b 0a 20  e, newCell) );. 
1f0a8 20 61 73 73 65 72 74 28 20 73 7a 4e 65 77 20 3c   assert( szNew <
1f0a9 3d 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70  = MX_CELL_SIZE(p
1f0aa 42 74 29 20 29 3b 0a 20 20 69 64 78 20 3d 20 70  Bt) );.  idx = p
1f0ab 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
1f0ac 3e 69 50 61 67 65 5d 3b 0a 20 20 69 66 28 20 6c  >iPage];.  if( l
1f0ad 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20 75 31 36  oc==0 ){.    u16
1f0ae 20 73 7a 4f 6c 64 3b 0a 20 20 20 20 61 73 73 65   szOld;.    asse
1f0af 72 74 28 20 69 64 78 3c 70 50 61 67 65 2d 3e 6e  rt( idx<pPage->n
1f0b0 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d  Cell );.    rc =
1f0b1 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
1f0b2 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  te(pPage->pDbPag
1f0b3 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  e);.    if( rc )
1f0b4 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64  {.      goto end
1f0b5 5f 69 6e 73 65 72 74 3b 0a 20 20 20 20 7d 0a 20  _insert;.    }. 
1f0b6 20 20 20 6f 6c 64 43 65 6c 6c 20 3d 20 66 69 6e     oldCell = fin
1f0b7 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78  dCell(pPage, idx
1f0b8 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67  );.    if( !pPag
1f0b9 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20  e->leaf ){.     
1f0ba 20 6d 65 6d 63 70 79 28 6e 65 77 43 65 6c 6c 2c   memcpy(newCell,
1f0bb 20 6f 6c 64 43 65 6c 6c 2c 20 34 29 3b 0a 20 20   oldCell, 4);.  
1f0bc 20 20 7d 0a 20 20 20 20 73 7a 4f 6c 64 20 3d 20    }.    szOld = 
1f0bd 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67  cellSizePtr(pPag
1f0be 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20  e, oldCell);.   
1f0bf 20 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28   rc = clearCell(
1f0c0 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b  pPage, oldCell);
1f0c1 0a 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50  .    dropCell(pP
1f0c2 61 67 65 2c 20 69 64 78 2c 20 73 7a 4f 6c 64 2c  age, idx, szOld,
1f0c3 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72   &rc);.    if( r
1f0c4 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73  c ) goto end_ins
1f0c5 65 72 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ert;.  }else if(
1f0c6 20 6c 6f 63 3c 30 20 26 26 20 70 50 61 67 65 2d   loc<0 && pPage-
1f0c7 3e 6e 43 65 6c 6c 3e 30 20 29 7b 0a 20 20 20 20  >nCell>0 ){.    
1f0c8 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c  assert( pPage->l
1f0c9 65 61 66 20 29 3b 0a 20 20 20 20 69 64 78 20 3d  eaf );.    idx =
1f0ca 20 2b 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b 70   ++pCur->aiIdx[p
1f0cb 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 7d  Cur->iPage];.  }
1f0cc 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
1f0cd 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b  ( pPage->leaf );
1f0ce 0a 20 20 7d 0a 20 20 69 6e 73 65 72 74 43 65 6c  .  }.  insertCel
1f0cf 6c 28 70 50 61 67 65 2c 20 69 64 78 2c 20 6e 65  l(pPage, idx, ne
1f0d0 77 43 65 6c 6c 2c 20 73 7a 4e 65 77 2c 20 30 2c  wCell, szNew, 0,
1f0d1 20 30 2c 20 26 72 63 29 3b 0a 20 20 61 73 73 65   0, &rc);.  asse
1f0d2 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  rt( rc!=SQLITE_O
1f0d3 4b 20 7c 7c 20 70 50 61 67 65 2d 3e 6e 43 65 6c  K || pPage->nCel
1f0d4 6c 3e 30 20 7c 7c 20 70 50 61 67 65 2d 3e 6e 4f  l>0 || pPage->nO
1f0d5 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20  verflow>0 );..  
1f0d6 2f 2a 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 68  /* If no error h
1f0d7 61 73 20 6f 63 63 75 72 65 64 20 61 6e 64 20 70  as occured and p
1f0d8 50 61 67 65 20 68 61 73 20 61 6e 20 6f 76 65 72  Page has an over
1f0d9 66 6c 6f 77 20 63 65 6c 6c 2c 20 63 61 6c 6c 20  flow cell, call 
1f0da 62 61 6c 61 6e 63 65 28 29 20 0a 20 20 2a 2a 20  balance() .  ** 
1f0db 74 6f 20 72 65 64 69 73 74 72 69 62 75 74 65 20  to redistribute 
1f0dc 74 68 65 20 63 65 6c 6c 73 20 77 69 74 68 69 6e  the cells within
1f0dd 20 74 68 65 20 74 72 65 65 2e 20 53 69 6e 63 65   the tree. Since
1f0de 20 62 61 6c 61 6e 63 65 28 29 20 6d 61 79 20 6d   balance() may m
1f0df 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72  ove.  ** the cur
1f0e0 73 6f 72 2c 20 7a 65 72 6f 20 74 68 65 20 42 74  sor, zero the Bt
1f0e1 43 75 72 73 6f 72 2e 69 6e 66 6f 2e 6e 53 69 7a  Cursor.info.nSiz
1f0e2 65 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 76  e and BtCursor.v
1f0e3 61 6c 69 64 4e 4b 65 79 0a 20 20 2a 2a 20 76 61  alidNKey.  ** va
1f0e4 72 69 61 62 6c 65 73 2e 0a 20 20 2a 2a 0a 20 20  riables..  **.  
1f0e5 2a 2a 20 50 72 65 76 69 6f 75 73 20 76 65 72 73  ** Previous vers
1f0e6 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 63  ions of SQLite c
1f0e7 61 6c 6c 65 64 20 6d 6f 76 65 54 6f 52 6f 6f 74  alled moveToRoot
1f0e8 28 29 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 63  () to move the c
1f0e9 75 72 73 6f 72 0a 20 20 2a 2a 20 62 61 63 6b 20  ursor.  ** back 
1f0ea 74 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  to the root page
1f0eb 20 61 73 20 62 61 6c 61 6e 63 65 28 29 20 75 73   as balance() us
1f0ec 65 64 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65  ed to invalidate
1f0ed 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20   the contents.  
1f0ee 2a 2a 20 6f 66 20 42 74 43 75 72 73 6f 72 2e 61  ** of BtCursor.a
1f0ef 70 50 61 67 65 5b 5d 20 61 6e 64 20 42 74 43 75  pPage[] and BtCu
1f0f0 72 73 6f 72 2e 61 69 49 64 78 5b 5d 2e 20 49 6e  rsor.aiIdx[]. In
1f0f1 73 74 65 61 64 20 6f 66 20 64 6f 69 6e 67 20 74  stead of doing t
1f0f2 68 61 74 2c 0a 20 20 2a 2a 20 73 65 74 20 74 68  hat,.  ** set th
1f0f3 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 20 74  e cursor state t
1f0f4 6f 20 22 69 6e 76 61 6c 69 64 22 2e 20 54 68 69  o "invalid". Thi
1f0f5 73 20 6d 61 6b 65 73 20 63 6f 6d 6d 6f 6e 20 69  s makes common i
1f0f6 6e 73 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73  nsert operations
1f0f7 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20 66  .  ** slightly f
1f0f8 61 73 74 65 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  aster..  **.  **
1f0f9 20 54 68 65 72 65 20 69 73 20 61 20 73 75 62 74   There is a subt
1f0fa 6c 65 20 62 75 74 20 69 6d 70 6f 72 74 61 6e 74  le but important
1f0fb 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 68 65   optimization he
1f0fc 72 65 20 74 6f 6f 2e 20 57 68 65 6e 20 69 6e 73  re too. When ins
1f0fd 65 72 74 69 6e 67 0a 20 20 2a 2a 20 6d 75 6c 74  erting.  ** mult
1f0fe 69 70 6c 65 20 72 65 63 6f 72 64 73 20 69 6e 74  iple records int
1f0ff 6f 20 61 6e 20 69 6e 74 6b 65 79 20 62 2d 74 72  o an intkey b-tr
1f100 65 65 20 75 73 69 6e 67 20 61 20 73 69 6e 67 6c  ee using a singl
1f101 65 20 63 75 72 73 6f 72 20 28 61 73 20 63 61 6e  e cursor (as can
1f102 0a 20 20 2a 2a 20 68 61 70 70 65 6e 20 77 68 69  .  ** happen whi
1f103 6c 65 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e  le processing an
1f104 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e   "INSERT INTO ..
1f105 2e 20 53 45 4c 45 43 54 22 20 73 74 61 74 65 6d  . SELECT" statem
1f106 65 6e 74 29 2c 20 69 74 0a 20 20 2a 2a 20 69 73  ent), it.  ** is
1f107 20 61 64 76 61 6e 74 61 67 65 6f 75 73 20 74 6f   advantageous to
1f108 20 6c 65 61 76 65 20 74 68 65 20 63 75 72 73 6f   leave the curso
1f109 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  r pointing to th
1f10a 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 0a  e last entry in.
1f10b 20 20 2a 2a 20 74 68 65 20 62 2d 74 72 65 65 20    ** the b-tree 
1f10c 69 66 20 70 6f 73 73 69 62 6c 65 2e 20 49 66 20  if possible. If 
1f10d 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65  the cursor is le
1f10e 66 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  ft pointing to t
1f10f 68 65 20 6c 61 73 74 0a 20 20 2a 2a 20 65 6e 74  he last.  ** ent
1f110 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c  ry in the table,
1f111 20 61 6e 64 20 74 68 65 20 6e 65 78 74 20 72 6f   and the next ro
1f112 77 20 69 6e 73 65 72 74 65 64 20 68 61 73 20 61  w inserted has a
1f113 6e 20 69 6e 74 65 67 65 72 20 6b 65 79 0a 20 20  n integer key.  
1f114 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  ** larger than t
1f115 68 65 20 6c 61 72 67 65 73 74 20 65 78 69 73 74  he largest exist
1f116 69 6e 67 20 6b 65 79 2c 20 69 74 20 69 73 20 70  ing key, it is p
1f117 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e 73 65 72  ossible to inser
1f118 74 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 77  t the.  ** row w
1f119 69 74 68 6f 75 74 20 73 65 65 6b 69 6e 67 20 74  ithout seeking t
1f11a 68 65 20 63 75 72 73 6f 72 2e 20 54 68 69 73 20  he cursor. This 
1f11b 63 61 6e 20 62 65 20 61 20 62 69 67 20 70 65 72  can be a big per
1f11c 66 6f 72 6d 61 6e 63 65 20 62 6f 6f 73 74 2e 0a  formance boost..
1f11d 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 69 6e 66    */.  pCur->inf
1f11e 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70  o.nSize = 0;.  p
1f11f 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d  Cur->validNKey =
1f120 20 30 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   0;.  if( rc==SQ
1f121 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65  LITE_OK && pPage
1f122 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20  ->nOverflow ){. 
1f123 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28     rc = balance(
1f124 70 43 75 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d  pCur);..    /* M
1f125 75 73 74 20 6d 61 6b 65 20 73 75 72 65 20 6e 4f  ust make sure nO
1f126 76 65 72 66 6c 6f 77 20 69 73 20 72 65 73 65 74  verflow is reset
1f127 20 74 6f 20 7a 65 72 6f 20 65 76 65 6e 20 69 66   to zero even if
1f128 20 74 68 65 20 62 61 6c 61 6e 63 65 28 29 0a 20   the balance(). 
1f129 20 20 20 2a 2a 20 66 61 69 6c 73 2e 20 49 6e 74     ** fails. Int
1f12a 65 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63  ernal data struc
1f12b 74 75 72 65 20 63 6f 72 72 75 70 74 69 6f 6e 20  ture corruption 
1f12c 77 69 6c 6c 20 72 65 73 75 6c 74 20 6f 74 68 65  will result othe
1f12d 72 77 69 73 65 2e 20 0a 20 20 20 20 2a 2a 20 41  rwise. .    ** A
1f12e 6c 73 6f 2c 20 73 65 74 20 74 68 65 20 63 75 72  lso, set the cur
1f12f 73 6f 72 20 73 74 61 74 65 20 74 6f 20 69 6e 76  sor state to inv
1f130 61 6c 69 64 2e 20 54 68 69 73 20 73 74 6f 70 73  alid. This stops
1f131 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74   saveCursorPosit
1f132 69 6f 6e 28 29 0a 20 20 20 20 2a 2a 20 66 72 6f  ion().    ** fro
1f133 6d 20 74 72 79 69 6e 67 20 74 6f 20 73 61 76 65  m trying to save
1f134 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 6f 73   the current pos
1f135 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 75 72  ition of the cur
1f136 73 6f 72 2e 20 20 2a 2f 0a 20 20 20 20 70 43 75  sor.  */.    pCu
1f137 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
1f138 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72 66 6c 6f  iPage]->nOverflo
1f139 77 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d  w = 0;.    pCur-
1f13a 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52  >eState = CURSOR
1f13b 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 7d 0a 20 20  _INVALID;.  }.  
1f13c 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70  assert( pCur->ap
1f13d 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
1f13e 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20  ]->nOverflow==0 
1f13f 29 3b 0a 0a 65 6e 64 5f 69 6e 73 65 72 74 3a 0a  );..end_insert:.
1f140 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1f141 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65  /*.** Delete the
1f142 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20   entry that the 
1f143 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69  cursor is pointi
1f144 6e 67 20 74 6f 2e 20 20 54 68 65 20 63 75 72 73  ng to.  The curs
1f145 6f 72 0a 2a 2a 20 69 73 20 6c 65 66 74 20 70 6f  or.** is left po
1f146 69 6e 74 69 6e 67 20 61 74 20 61 20 61 72 62 69  inting at a arbi
1f147 74 72 61 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 0a  trary location..
1f148 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1f149 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1f14a 65 65 44 65 6c 65 74 65 28 42 74 43 75 72 73 6f  eeDelete(BtCurso
1f14b 72 20 2a 70 43 75 72 29 7b 0a 20 20 42 74 72 65  r *pCur){.  Btre
1f14c 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74  e *p = pCur->pBt
1f14d 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20  ree;.  BtShared 
1f14e 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 20 20  *pBt = p->pBt;  
1f14f 20 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 69              .  i
1f150 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
1f151 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f152 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
1f153 64 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  de */.  MemPage 
1f154 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  *pPage;         
1f155 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f156 50 61 67 65 20 74 6f 20 64 65 6c 65 74 65 20 63  Page to delete c
1f157 65 6c 6c 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 6e  ell from */.  un
1f158 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65  signed char *pCe
1f159 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
1f15a 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1f15b 20 63 65 6c 6c 20 74 6f 20 64 65 6c 65 74 65 20   cell to delete 
1f15c 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 49 64  */.  int iCellId
1f15d 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
1f15e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
1f15f 65 78 20 6f 66 20 63 65 6c 6c 20 74 6f 20 64 65  ex of cell to de
1f160 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43  lete */.  int iC
1f161 65 6c 6c 44 65 70 74 68 3b 20 20 20 20 20 20 20  ellDepth;       
1f162 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1f163 2a 20 44 65 70 74 68 20 6f 66 20 6e 6f 64 65 20  * Depth of node 
1f164 63 6f 6e 74 61 69 6e 69 6e 67 20 70 43 65 6c 6c  containing pCell
1f165 20 2a 2f 20 0a 0a 20 20 61 73 73 65 72 74 28 20   */ ..  assert( 
1f166 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
1f167 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
1f168 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73  rt( pBt->inTrans
1f169 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52  action==TRANS_WR
1f16a 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ITE );.  assert(
1f16b 20 28 70 42 74 2d 3e 62 74 73 46 6c 61 67 73 20   (pBt->btsFlags 
1f16c 26 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59 29  & BTS_READ_ONLY)
1f16d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
1f16e 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 29 3b   pCur->wrFlag );
1f16f 0a 20 20 61 73 73 65 72 74 28 20 68 61 73 53 68  .  assert( hasSh
1f170 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
1f171 63 6b 28 70 2c 20 70 43 75 72 2d 3e 70 67 6e 6f  ck(p, pCur->pgno
1f172 52 6f 6f 74 2c 20 70 43 75 72 2d 3e 70 4b 65 79  Root, pCur->pKey
1f173 49 6e 66 6f 21 3d 30 2c 20 32 29 20 29 3b 0a 20  Info!=0, 2) );. 
1f174 20 61 73 73 65 72 74 28 20 21 68 61 73 52 65 61   assert( !hasRea
1f175 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 70 43  dConflicts(p, pC
1f176 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 29 20 29 3b  ur->pgnoRoot) );
1f177 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 43  ..  if( NEVER(pC
1f178 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
1f179 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70  iPage]>=pCur->ap
1f17a 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
1f17b 5d 2d 3e 6e 43 65 6c 6c 29 20 0a 20 20 20 7c 7c  ]->nCell) .   ||
1f17c 20 4e 45 56 45 52 28 70 43 75 72 2d 3e 65 53 74   NEVER(pCur->eSt
1f17d 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate!=CURSOR_VALI
1f17e 44 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75  D).  ){.    retu
1f17f 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
1f180 20 20 2f 2a 20 53 6f 6d 65 74 68 69 6e 67 20 68    /* Something h
1f181 61 73 20 67 6f 6e 65 20 61 77 72 79 2e 20 2a 2f  as gone awry. */
1f182 0a 20 20 7d 0a 0a 20 20 69 43 65 6c 6c 44 65 70  .  }..  iCellDep
1f183 74 68 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65  th = pCur->iPage
1f184 3b 0a 20 20 69 43 65 6c 6c 49 64 78 20 3d 20 70  ;.  iCellIdx = p
1f185 43 75 72 2d 3e 61 69 49 64 78 5b 69 43 65 6c 6c  Cur->aiIdx[iCell
1f186 44 65 70 74 68 5d 3b 0a 20 20 70 50 61 67 65 20  Depth];.  pPage 
1f187 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69  = pCur->apPage[i
1f188 43 65 6c 6c 44 65 70 74 68 5d 3b 0a 20 20 70 43  CellDepth];.  pC
1f189 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  ell = findCell(p
1f18a 50 61 67 65 2c 20 69 43 65 6c 6c 49 64 78 29 3b  Page, iCellIdx);
1f18b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61  ..  /* If the pa
1f18c 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ge containing th
1f18d 65 20 65 6e 74 72 79 20 74 6f 20 64 65 6c 65 74  e entry to delet
1f18e 65 20 69 73 20 6e 6f 74 20 61 20 6c 65 61 66 20  e is not a leaf 
1f18f 70 61 67 65 2c 20 6d 6f 76 65 0a 20 20 2a 2a 20  page, move.  ** 
1f190 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68  the cursor to th
1f191 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20  e largest entry 
1f192 69 6e 20 74 68 65 20 74 72 65 65 20 74 68 61 74  in the tree that
1f193 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e   is smaller than
1f194 0a 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20  .  ** the entry 
1f195 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 20 54  being deleted. T
1f196 68 69 73 20 63 65 6c 6c 20 77 69 6c 6c 20 72 65  his cell will re
1f197 70 6c 61 63 65 20 74 68 65 20 63 65 6c 6c 20 62  place the cell b
1f198 65 69 6e 67 20 64 65 6c 65 74 65 64 0a 20 20 2a  eing deleted.  *
1f199 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72  * from the inter
1f19a 6e 61 6c 20 6e 6f 64 65 2e 20 54 68 65 20 27 70  nal node. The 'p
1f19b 72 65 76 69 6f 75 73 27 20 65 6e 74 72 79 20 69  revious' entry i
1f19c 73 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20  s used for this 
1f19d 69 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20  instead.  ** of 
1f19e 74 68 65 20 27 6e 65 78 74 27 20 65 6e 74 72 79  the 'next' entry
1f19f 2c 20 61 73 20 74 68 65 20 70 72 65 76 69 6f 75  , as the previou
1f1a0 73 20 65 6e 74 72 79 20 69 73 20 61 6c 77 61 79  s entry is alway
1f1a1 73 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 0a  s a part of the.
1f1a2 20 20 2a 2a 20 73 75 62 2d 74 72 65 65 20 68 65    ** sub-tree he
1f1a3 61 64 65 64 20 62 79 20 74 68 65 20 63 68 69 6c  aded by the chil
1f1a4 64 20 70 61 67 65 20 6f 66 20 74 68 65 20 63 65  d page of the ce
1f1a5 6c 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64  ll being deleted
1f1a6 2e 20 54 68 69 73 20 6d 61 6b 65 73 0a 20 20 2a  . This makes.  *
1f1a7 2a 20 62 61 6c 61 6e 63 69 6e 67 20 74 68 65 20  * balancing the 
1f1a8 74 72 65 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  tree following t
1f1a9 68 65 20 64 65 6c 65 74 65 20 6f 70 65 72 61 74  he delete operat
1f1aa 69 6f 6e 20 65 61 73 69 65 72 2e 20 20 2a 2f 0a  ion easier.  */.
1f1ab 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
1f1ac 61 66 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 6f  af ){.    int no
1f1ad 74 55 73 65 64 3b 0a 20 20 20 20 72 63 20 3d 20  tUsed;.    rc = 
1f1ae 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76  sqlite3BtreePrev
1f1af 69 6f 75 73 28 70 43 75 72 2c 20 26 6e 6f 74 55  ious(pCur, &notU
1f1b0 73 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63  sed);.    if( rc
1f1b1 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
1f1b2 7d 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65  }..  /* Save the
1f1b3 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e   positions of an
1f1b4 79 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20  y other cursors 
1f1b5 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62  open on this tab
1f1b6 6c 65 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 6d  le before.  ** m
1f1b7 61 6b 69 6e 67 20 61 6e 79 20 6d 6f 64 69 66 69  aking any modifi
1f1b8 63 61 74 69 6f 6e 73 2e 20 4d 61 6b 65 20 74 68  cations. Make th
1f1b9 65 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e  e page containin
1f1ba 67 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20 62  g the entry to b
1f1bb 65 20 0a 20 20 2a 2a 20 64 65 6c 65 74 65 64 20  e .  ** deleted 
1f1bc 77 72 69 74 61 62 6c 65 2e 20 54 68 65 6e 20 66  writable. Then f
1f1bd 72 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77  ree any overflow
1f1be 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65   pages associate
1f1bf 64 20 77 69 74 68 20 74 68 65 20 0a 20 20 2a 2a  d with the .  **
1f1c0 20 65 6e 74 72 79 20 61 6e 64 20 66 69 6e 61 6c   entry and final
1f1c1 6c 79 20 72 65 6d 6f 76 65 20 74 68 65 20 63 65  ly remove the ce
1f1c2 6c 6c 20 69 74 73 65 6c 66 20 66 72 6f 6d 20 77  ll itself from w
1f1c3 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 2e 20  ithin the page. 
1f1c4 20 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 61   .  */.  rc = sa
1f1c5 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74  veAllCursors(pBt
1f1c6 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74  , pCur->pgnoRoot
1f1c7 2c 20 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72  , pCur);.  if( r
1f1c8 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  c ) return rc;..
1f1c9 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
1f1ca 61 20 64 65 6c 65 74 65 20 6f 70 65 72 61 74 69  a delete operati
1f1cb 6f 6e 20 74 6f 20 72 65 6d 6f 76 65 20 61 20 72  on to remove a r
1f1cc 6f 77 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20  ow from a table 
1f1cd 62 2d 74 72 65 65 2c 0a 20 20 2a 2a 20 69 6e 76  b-tree,.  ** inv
1f1ce 61 6c 69 64 61 74 65 20 61 6e 79 20 69 6e 63 72  alidate any incr
1f1cf 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65  blob cursors ope
1f1d0 6e 20 6f 6e 20 74 68 65 20 72 6f 77 20 62 65 69  n on the row bei
1f1d1 6e 67 20 64 65 6c 65 74 65 64 2e 20 20 2a 2f 0a  ng deleted.  */.
1f1d2 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4b 65 79    if( pCur->pKey
1f1d3 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 69  Info==0 ){.    i
1f1d4 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f  nvalidateIncrblo
1f1d5 62 43 75 72 73 6f 72 73 28 70 2c 20 70 43 75 72  bCursors(p, pCur
1f1d6 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 29 3b  ->info.nKey, 0);
1f1d7 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c  .  }..  rc = sql
1f1d8 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
1f1d9 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Page->pDbPage);.
1f1da 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
1f1db 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 63 6c 65  n rc;.  rc = cle
1f1dc 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43  arCell(pPage, pC
1f1dd 65 6c 6c 29 3b 0a 20 20 64 72 6f 70 43 65 6c 6c  ell);.  dropCell
1f1de 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 49 64 78  (pPage, iCellIdx
1f1df 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50  , cellSizePtr(pP
1f1e0 61 67 65 2c 20 70 43 65 6c 6c 29 2c 20 26 72 63  age, pCell), &rc
1f1e1 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
1f1e2 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 49  turn rc;..  /* I
1f1e3 66 20 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74  f the cell delet
1f1e4 65 64 20 77 61 73 20 6e 6f 74 20 6c 6f 63 61 74  ed was not locat
1f1e5 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 70 61 67  ed on a leaf pag
1f1e6 65 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73  e, then the curs
1f1e7 6f 72 0a 20 20 2a 2a 20 69 73 20 63 75 72 72 65  or.  ** is curre
1f1e8 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f  ntly pointing to
1f1e9 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74   the largest ent
1f1ea 72 79 20 69 6e 20 74 68 65 20 73 75 62 2d 74 72  ry in the sub-tr
1f1eb 65 65 20 68 65 61 64 65 64 0a 20 20 2a 2a 20 62  ee headed.  ** b
1f1ec 79 20 74 68 65 20 63 68 69 6c 64 2d 70 61 67 65  y the child-page
1f1ed 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61   of the cell tha
1f1ee 74 20 77 61 73 20 6a 75 73 74 20 64 65 6c 65 74  t was just delet
1f1ef 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e 74 65 72  ed from an inter
1f1f0 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65 2e 20 54  nal.  ** node. T
1f1f1 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65  he cell from the
1f1f2 20 6c 65 61 66 20 6e 6f 64 65 20 6e 65 65 64 73   leaf node needs
1f1f3 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 74 6f 20   to be moved to 
1f1f4 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 20 20 2a  the internal.  *
1f1f5 2a 20 6e 6f 64 65 20 74 6f 20 72 65 70 6c 61 63  * node to replac
1f1f6 65 20 74 68 65 20 64 65 6c 65 74 65 64 20 63 65  e the deleted ce
1f1f7 6c 6c 2e 20 20 2a 2f 0a 20 20 69 66 28 20 21 70  ll.  */.  if( !p
1f1f8 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
1f1f9 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 65 61 66    MemPage *pLeaf
1f1fa 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
1f1fb 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
1f1fc 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 20    int nCell;.   
1f1fd 20 50 67 6e 6f 20 6e 20 3d 20 70 43 75 72 2d 3e   Pgno n = pCur->
1f1fe 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65 70 74  apPage[iCellDept
1f1ff 68 2b 31 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20  h+1]->pgno;.    
1f200 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
1f201 54 6d 70 3b 0a 0a 20 20 20 20 70 43 65 6c 6c 20  Tmp;..    pCell 
1f202 3d 20 66 69 6e 64 43 65 6c 6c 28 70 4c 65 61 66  = findCell(pLeaf
1f203 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31  , pLeaf->nCell-1
1f204 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 63  );.    nCell = c
1f205 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c 65 61 66  ellSizePtr(pLeaf
1f206 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 61 73  , pCell);.    as
1f207 73 65 72 74 28 20 4d 58 5f 43 45 4c 4c 5f 53 49  sert( MX_CELL_SI
1f208 5a 45 28 70 42 74 29 20 3e 3d 20 6e 43 65 6c 6c  ZE(pBt) >= nCell
1f209 20 29 3b 0a 0a 20 20 20 20 61 6c 6c 6f 63 61 74   );..    allocat
1f20a 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b  eTempSpace(pBt);
1f20b 0a 20 20 20 20 70 54 6d 70 20 3d 20 70 42 74 2d  .    pTmp = pBt-
1f20c 3e 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20 20 20  >pTmpSpace;..   
1f20d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1f20e 65 72 57 72 69 74 65 28 70 4c 65 61 66 2d 3e 70  erWrite(pLeaf->p
1f20f 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 6e 73  DbPage);.    ins
1f210 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  ertCell(pPage, i
1f211 43 65 6c 6c 49 64 78 2c 20 70 43 65 6c 6c 2d 34  CellIdx, pCell-4
1f212 2c 20 6e 43 65 6c 6c 2b 34 2c 20 70 54 6d 70 2c  , nCell+4, pTmp,
1f213 20 6e 2c 20 26 72 63 29 3b 0a 20 20 20 20 64 72   n, &rc);.    dr
1f214 6f 70 43 65 6c 6c 28 70 4c 65 61 66 2c 20 70 4c  opCell(pLeaf, pL
1f215 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 2c 20 6e 43  eaf->nCell-1, nC
1f216 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 69  ell, &rc);.    i
1f217 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1f218 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 61 6c  c;.  }..  /* Bal
1f219 61 6e 63 65 20 74 68 65 20 74 72 65 65 2e 20 49  ance the tree. I
1f21a 66 20 74 68 65 20 65 6e 74 72 79 20 64 65 6c 65  f the entry dele
1f21b 74 65 64 20 77 61 73 20 6c 6f 63 61 74 65 64 20  ted was located 
1f21c 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 65 2c 0a  on a leaf page,.
1f21d 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 75    ** then the cu
1f21e 72 73 6f 72 20 73 74 69 6c 6c 20 70 6f 69 6e 74  rsor still point
1f21f 73 20 74 6f 20 74 68 61 74 20 70 61 67 65 2e 20  s to that page. 
1f220 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
1f221 20 66 69 72 73 74 0a 20 20 2a 2a 20 63 61 6c 6c   first.  ** call
1f222 20 74 6f 20 62 61 6c 61 6e 63 65 28 29 20 72 65   to balance() re
1f223 70 61 69 72 73 20 74 68 65 20 74 72 65 65 2c 20  pairs the tree, 
1f224 61 6e 64 20 74 68 65 20 69 66 28 2e 2e 2e 29 20  and the if(...) 
1f225 63 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20 20 2a  condition is.  *
1f226 2a 20 6e 65 76 65 72 20 74 72 75 65 2e 0a 20 20  * never true..  
1f227 2a 2a 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73  **.  ** Otherwis
1f228 65 2c 20 69 66 20 74 68 65 20 65 6e 74 72 79 20  e, if the entry 
1f229 64 65 6c 65 74 65 64 20 77 61 73 20 6f 6e 20 61  deleted was on a
1f22a 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20  n internal node 
1f22b 70 61 67 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  page, then.  ** 
1f22c 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67  pCur is pointing
1f22d 20 74 6f 20 74 68 65 20 6c 65 61 66 20 70 61 67   to the leaf pag
1f22e 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 63  e from which a c
1f22f 65 6c 6c 20 77 61 73 20 72 65 6d 6f 76 65 64 20  ell was removed 
1f230 74 6f 0a 20 20 2a 2a 20 72 65 70 6c 61 63 65 20  to.  ** replace 
1f231 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 65 64  the cell deleted
1f232 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e   from the intern
1f233 61 6c 20 6e 6f 64 65 2e 20 54 68 69 73 20 69 73  al node. This is
1f234 20 73 6c 69 67 68 74 6c 79 0a 20 20 2a 2a 20 74   slightly.  ** t
1f235 72 69 63 6b 79 20 61 73 20 74 68 65 20 6c 65 61  ricky as the lea
1f236 66 20 6e 6f 64 65 20 6d 61 79 20 62 65 20 75 6e  f node may be un
1f237 64 65 72 66 75 6c 6c 2c 20 61 6e 64 20 74 68 65  derfull, and the
1f238 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 6d   internal node m
1f239 61 79 0a 20 20 2a 2a 20 62 65 20 65 69 74 68 65  ay.  ** be eithe
1f23a 72 20 75 6e 64 65 72 20 6f 72 20 6f 76 65 72 66  r under or overf
1f23b 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ull. In this cas
1f23c 65 20 72 75 6e 20 74 68 65 20 62 61 6c 61 6e 63  e run the balanc
1f23d 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20  ing algorithm.  
1f23e 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 61 66 20 6e  ** on the leaf n
1f23f 6f 64 65 20 66 69 72 73 74 2e 20 49 66 20 74 68  ode first. If th
1f240 65 20 62 61 6c 61 6e 63 65 20 70 72 6f 63 65 65  e balance procee
1f241 64 73 20 66 61 72 20 65 6e 6f 75 67 68 20 75 70  ds far enough up
1f242 20 74 68 65 0a 20 20 2a 2a 20 74 72 65 65 20 74   the.  ** tree t
1f243 68 61 74 20 77 65 20 63 61 6e 20 62 65 20 73 75  hat we can be su
1f244 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 6f 62  re that any prob
1f245 6c 65 6d 20 69 6e 20 74 68 65 20 69 6e 74 65 72  lem in the inter
1f246 6e 61 6c 20 6e 6f 64 65 20 68 61 73 0a 20 20 2a  nal node has.  *
1f247 2a 20 62 65 65 6e 20 63 6f 72 72 65 63 74 65 64  * been corrected
1f248 2c 20 73 6f 20 62 65 20 69 74 2e 20 4f 74 68 65  , so be it. Othe
1f249 72 77 69 73 65 2c 20 61 66 74 65 72 20 62 61 6c  rwise, after bal
1f24a 61 6e 63 69 6e 67 20 74 68 65 20 6c 65 61 66 20  ancing the leaf 
1f24b 6e 6f 64 65 2c 0a 20 20 2a 2a 20 77 61 6c 6b 20  node,.  ** walk 
1f24c 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 68  the cursor up th
1f24d 65 20 74 72 65 65 20 74 6f 20 74 68 65 20 69 6e  e tree to the in
1f24e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 61 6e 64 20  ternal node and 
1f24f 62 61 6c 61 6e 63 65 20 69 74 20 61 73 20 0a 20  balance it as . 
1f250 20 2a 2a 20 77 65 6c 6c 2e 20 20 2a 2f 0a 20 20   ** well.  */.  
1f251 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75  rc = balance(pCu
1f252 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  r);.  if( rc==SQ
1f253 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 75 72 2d  LITE_OK && pCur-
1f254 3e 69 50 61 67 65 3e 69 43 65 6c 6c 44 65 70 74  >iPage>iCellDept
1f255 68 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20  h ){.    while( 
1f256 70 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c  pCur->iPage>iCel
1f257 6c 44 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20  lDepth ){.      
1f258 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72  releasePage(pCur
1f259 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
1f25a 50 61 67 65 2d 2d 5d 29 3b 0a 20 20 20 20 7d 0a  Page--]);.    }.
1f25b 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65      rc = balance
1f25c 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69  (pCur);.  }..  i
1f25d 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1f25e 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f   ){.    moveToRo
1f25f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20  ot(pCur);.  }.  
1f260 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1f261 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
1f262 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57   BTree table.  W
1f263 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62  rite into *piTab
1f264 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e  le the page.** n
1f265 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f  umber for the ro
1f266 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e  ot page of the n
1f267 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ew table..**.** 
1f268 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 70 65  The type of type
1f269 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62   is determined b
1f26a 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61  y the flags para
1f26b 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65  meter.  Only the
1f26c 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  .** following va
1f26d 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72  lues of flags ar
1f26e 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75  e currently in u
1f26f 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65  se.  Other value
1f270 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d  s for.** flags m
1f271 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a  ight not work:.*
1f272 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49  *.**     BTREE_I
1f273 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46  NTKEY|BTREE_LEAF
1f274 44 41 54 41 20 20 20 20 20 55 73 65 64 20 66 6f  DATA     Used fo
1f275 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74  r SQL tables wit
1f276 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20  h rowid keys.** 
1f277 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41      BTREE_ZERODA
1f278 54 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TA              
1f279 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c      Used for SQL
1f27a 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74   indices.*/.stat
1f27b 69 63 20 69 6e 74 20 62 74 72 65 65 43 72 65 61  ic int btreeCrea
1f27c 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70  teTable(Btree *p
1f27d 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20  , int *piTable, 
1f27e 69 6e 74 20 63 72 65 61 74 65 54 61 62 46 6c 61  int createTabFla
1f27f 67 73 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  gs){.  BtShared 
1f280 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
1f281 20 4d 65 6d 50 61 67 65 20 2a 70 52 6f 6f 74 3b   MemPage *pRoot;
1f282 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74  .  Pgno pgnoRoot
1f283 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e  ;.  int rc;.  in
1f284 74 20 70 74 66 46 6c 61 67 73 3b 20 20 20 20 20  t ptfFlags;     
1f285 20 20 20 20 20 2f 2a 20 50 61 67 65 2d 74 79 70       /* Page-typ
1f286 65 20 66 6c 61 67 65 20 66 6f 72 20 74 68 65 20  e flage for the 
1f287 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 6e 65 77  root page of new
1f288 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73   table */..  ass
1f289 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1f28a 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29  eHoldsMutex(p) )
1f28b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d  ;.  assert( pBt-
1f28c 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d  >inTransaction==
1f28d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20  TRANS_WRITE );. 
1f28e 20 61 73 73 65 72 74 28 20 28 70 42 74 2d 3e 62   assert( (pBt->b
1f28f 74 73 46 6c 61 67 73 20 26 20 42 54 53 5f 52 45  tsFlags & BTS_RE
1f290 41 44 5f 4f 4e 4c 59 29 3d 3d 30 20 29 3b 0a 0a  AD_ONLY)==0 );..
1f291 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
1f292 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
1f293 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72  rc = allocateBtr
1f294 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 52 6f  eePage(pBt, &pRo
1f295 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f 74 2c 20 31  ot, &pgnoRoot, 1
1f296 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29  , 0);.  if( rc )
1f297 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
1f298 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28  .  }.#else.  if(
1f299 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
1f29a 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e   ){.    Pgno pgn
1f29b 6f 4d 6f 76 65 3b 20 20 20 20 20 20 2f 2a 20 4d  oMove;      /* M
1f29c 6f 76 65 20 61 20 70 61 67 65 20 68 65 72 65 20  ove a page here 
1f29d 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72  to make room for
1f29e 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 2a   the root-page *
1f29f 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  /.    MemPage *p
1f2a0 50 61 67 65 4d 6f 76 65 3b 20 2f 2a 20 54 68 65  PageMove; /* The
1f2a1 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 74 6f   page to move to
1f2a2 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 43 72 65  . */..    /* Cre
1f2a3 61 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c  ating a new tabl
1f2a4 65 20 6d 61 79 20 70 72 6f 62 61 62 6c 79 20 72  e may probably r
1f2a5 65 71 75 69 72 65 20 6d 6f 76 69 6e 67 20 61 6e  equire moving an
1f2a6 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61   existing databa
1f2a7 73 65 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6b  se.    ** to mak
1f2a8 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 6e  e room for the n
1f2a9 65 77 20 74 61 62 6c 65 73 20 72 6f 6f 74 20 70  ew tables root p
1f2aa 61 67 65 2e 20 49 6e 20 63 61 73 65 20 74 68 69  age. In case thi
1f2ab 73 20 70 61 67 65 20 74 75 72 6e 73 0a 20 20 20  s page turns.   
1f2ac 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 20 61 6e   ** out to be an
1f2ad 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20   overflow page, 
1f2ae 64 65 6c 65 74 65 20 61 6c 6c 20 6f 76 65 72 66  delete all overf
1f2af 6c 6f 77 20 70 61 67 65 2d 6d 61 70 20 63 61 63  low page-map cac
1f2b0 68 65 73 0a 20 20 20 20 2a 2a 20 68 65 6c 64 20  hes.    ** held 
1f2b1 62 79 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e  by open cursors.
1f2b2 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 76 61  .    */.    inva
1f2b3 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f  lidateAllOverflo
1f2b4 77 43 61 63 68 65 28 70 42 74 29 3b 0a 0a 20 20  wCache(pBt);..  
1f2b5 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 76 61    /* Read the va
1f2b6 6c 75 65 20 6f 66 20 6d 65 74 61 5b 33 5d 20 66  lue of meta[3] f
1f2b7 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
1f2b8 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68   to determine wh
1f2b9 65 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 72  ere the.    ** r
1f2ba 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  oot page of the 
1f2bb 6e 65 77 20 74 61 62 6c 65 20 73 68 6f 75 6c 64  new table should
1f2bc 20 67 6f 2e 20 6d 65 74 61 5b 33 5d 20 69 73 20   go. meta[3] is 
1f2bd 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74  the largest root
1f2be 2d 70 61 67 65 0a 20 20 20 20 2a 2a 20 63 72 65  -page.    ** cre
1f2bf 61 74 65 64 20 73 6f 20 66 61 72 2c 20 73 6f 20  ated so far, so 
1f2c0 74 68 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61 67  the new root-pag
1f2c1 65 20 69 73 20 28 6d 65 74 61 5b 33 5d 2b 31 29  e is (meta[3]+1)
1f2c2 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
1f2c3 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
1f2c4 28 70 2c 20 42 54 52 45 45 5f 4c 41 52 47 45 53  (p, BTREE_LARGES
1f2c5 54 5f 52 4f 4f 54 5f 50 41 47 45 2c 20 26 70 67  T_ROOT_PAGE, &pg
1f2c6 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 70 67 6e  noRoot);.    pgn
1f2c7 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20 20 20 2f 2a  oRoot++;..    /*
1f2c8 20 54 68 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61   The new root-pa
1f2c9 67 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c  ge may not be al
1f2ca 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 70 6f 69  located on a poi
1f2cb 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 6f  nter-map page, o
1f2cc 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 50 45 4e  r the.    ** PEN
1f2cd 44 49 4e 47 5f 42 59 54 45 20 70 61 67 65 2e 0a  DING_BYTE page..
1f2ce 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65      */.    while
1f2cf 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50 54 52 4d  ( pgnoRoot==PTRM
1f2d0 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70  AP_PAGENO(pBt, p
1f2d1 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a 20 20 20 20  gnoRoot) ||.    
1f2d2 20 20 20 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50 45      pgnoRoot==PE
1f2d3 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
1f2d4 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 70 67  pBt) ){.      pg
1f2d5 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20 20 20 7d 0a  noRoot++;.    }.
1f2d6 20 20 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f      assert( pgno
1f2d7 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a 20 20 20 20  Root>=3 );..    
1f2d8 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 70 61  /* Allocate a pa
1f2d9 67 65 2e 20 54 68 65 20 70 61 67 65 20 74 68 61  ge. The page tha
1f2da 74 20 63 75 72 72 65 6e 74 6c 79 20 72 65 73 69  t currently resi
1f2db 64 65 73 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20  des at pgnoRoot 
1f2dc 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65 20 6d  will.    ** be m
1f2dd 6f 76 65 64 20 74 6f 20 74 68 65 20 61 6c 6c 6f  oved to the allo
1f2de 63 61 74 65 64 20 70 61 67 65 20 28 75 6e 6c 65  cated page (unle
1f2df 73 73 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64  ss the allocated
1f2e0 20 70 61 67 65 20 68 61 70 70 65 6e 73 0a 20 20   page happens.  
1f2e1 20 20 2a 2a 20 74 6f 20 72 65 73 69 64 65 20 61    ** to reside a
1f2e2 74 20 70 67 6e 6f 52 6f 6f 74 29 2e 0a 20 20 20  t pgnoRoot)..   
1f2e3 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c   */.    rc = all
1f2e4 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70  ocateBtreePage(p
1f2e5 42 74 2c 20 26 70 50 61 67 65 4d 6f 76 65 2c 20  Bt, &pPageMove, 
1f2e6 26 70 67 6e 6f 4d 6f 76 65 2c 20 70 67 6e 6f 52  &pgnoMove, pgnoR
1f2e7 6f 6f 74 2c 20 31 29 3b 0a 20 20 20 20 69 66 28  oot, 1);.    if(
1f2e8 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1f2e9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
1f2ea 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  c;.    }..    if
1f2eb 28 20 70 67 6e 6f 4d 6f 76 65 21 3d 70 67 6e 6f  ( pgnoMove!=pgno
1f2ec 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a  Root ){.      /*
1f2ed 20 70 67 6e 6f 52 6f 6f 74 20 69 73 20 74 68 65   pgnoRoot is the
1f2ee 20 70 61 67 65 20 74 68 61 74 20 77 69 6c 6c 20   page that will 
1f2ef 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  be used for the 
1f2f0 72 6f 6f 74 2d 70 61 67 65 20 6f 66 0a 20 20 20  root-page of.   
1f2f1 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 74 61     ** the new ta
1f2f2 62 6c 65 20 28 61 73 73 75 6d 69 6e 67 20 61 6e  ble (assuming an
1f2f3 20 65 72 72 6f 72 20 64 69 64 20 6e 6f 74 20 6f   error did not o
1f2f4 63 63 75 72 29 2e 20 42 75 74 20 77 65 20 77 65  ccur). But we we
1f2f5 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f  re.      ** allo
1f2f6 63 61 74 65 64 20 70 67 6e 6f 4d 6f 76 65 2e 20  cated pgnoMove. 
1f2f7 49 66 20 72 65 71 75 69 72 65 64 20 28 69 2e 65  If required (i.e
1f2f8 2e 20 69 66 20 69 74 20 77 61 73 20 6e 6f 74 20  . if it was not 
1f2f9 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 20 20  allocated.      
1f2fa 2a 2a 20 62 79 20 65 78 74 65 6e 64 69 6e 67 20  ** by extending 
1f2fb 74 68 65 20 66 69 6c 65 29 2c 20 74 68 65 20 63  the file), the c
1f2fc 75 72 72 65 6e 74 20 70 61 67 65 20 61 74 20 70  urrent page at p
1f2fd 6f 73 69 74 69 6f 6e 20 70 67 6e 6f 4d 6f 76 65  osition pgnoMove
1f2fe 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 61 6c 72  .      ** is alr
1f2ff 65 61 64 79 20 6a 6f 75 72 6e 61 6c 65 64 2e 0a  eady journaled..
1f300 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 75        */.      u
1f301 38 20 65 54 79 70 65 20 3d 20 30 3b 0a 20 20 20  8 eType = 0;.   
1f302 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65     Pgno iPtrPage
1f303 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 72 65 6c   = 0;..      rel
1f304 65 61 73 65 50 61 67 65 28 70 50 61 67 65 4d 6f  easePage(pPageMo
1f305 76 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d  ve);..      /* M
1f306 6f 76 65 20 74 68 65 20 70 61 67 65 20 63 75 72  ove the page cur
1f307 72 65 6e 74 6c 79 20 61 74 20 70 67 6e 6f 52 6f  rently at pgnoRo
1f308 6f 74 20 74 6f 20 70 67 6e 6f 4d 6f 76 65 2e 20  ot to pgnoMove. 
1f309 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74  */.      rc = bt
1f30a 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
1f30b 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74  pgnoRoot, &pRoot
1f30c 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
1f30d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1f30e 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
1f30f 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
1f310 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74    rc = ptrmapGet
1f311 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20  (pBt, pgnoRoot, 
1f312 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61 67  &eType, &iPtrPag
1f313 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54  e);.      if( eT
1f314 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54  ype==PTRMAP_ROOT
1f315 50 41 47 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50  PAGE || eType==P
1f316 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29  TRMAP_FREEPAGE )
1f317 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
1f318 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
1f319 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  PT;.      }.    
1f31a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1f31b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
1f31c 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74  eleasePage(pRoot
1f31d 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
1f31e 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
1f31f 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79 70      assert( eTyp
1f320 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41  e!=PTRMAP_ROOTPA
1f321 47 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  GE );.      asse
1f322 72 74 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41  rt( eType!=PTRMA
1f323 50 5f 46 52 45 45 50 41 47 45 20 29 3b 0a 20 20  P_FREEPAGE );.  
1f324 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74      rc = relocat
1f325 65 50 61 67 65 28 70 42 74 2c 20 70 52 6f 6f 74  ePage(pBt, pRoot
1f326 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 67  , eType, iPtrPag
1f327 65 2c 20 70 67 6e 6f 4d 6f 76 65 2c 20 30 29 3b  e, pgnoMove, 0);
1f328 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
1f329 67 65 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20  ge(pRoot);..    
1f32a 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 20    /* Obtain the 
1f32b 70 61 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74  page at pgnoRoot
1f32c 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63   */.      if( rc
1f32d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1f32e 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1f32f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1f330 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
1f331 65 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c  e(pBt, pgnoRoot,
1f332 20 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20   &pRoot, 0);.   
1f333 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1f334 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1f335 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
1f336 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71   }.      rc = sq
1f337 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
1f338 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b  pRoot->pDbPage);
1f339 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1f33a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1f33b 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
1f33c 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20  pRoot);.        
1f33d 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
1f33e 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
1f33f 20 20 20 20 70 52 6f 6f 74 20 3d 20 70 50 61 67      pRoot = pPag
1f340 65 4d 6f 76 65 3b 0a 20 20 20 20 7d 20 0a 0a 20  eMove;.    } .. 
1f341 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65     /* Update the
1f342 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 61 6e 64   pointer-map and
1f343 20 6d 65 74 61 2d 64 61 74 61 20 77 69 74 68 20   meta-data with 
1f344 74 68 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61 67  the new root-pag
1f345 65 20 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 20  e number. */.   
1f346 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
1f347 70 67 6e 6f 52 6f 6f 74 2c 20 50 54 52 4d 41 50  pgnoRoot, PTRMAP
1f348 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20 26 72  _ROOTPAGE, 0, &r
1f349 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  c);.    if( rc )
1f34a 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  {.      releaseP
1f34b 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20  age(pRoot);.    
1f34c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1f34d 20 7d 0a 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20   }..    /* When 
1f34e 74 68 65 20 6e 65 77 20 72 6f 6f 74 20 70 61 67  the new root pag
1f34f 65 20 77 61 73 20 61 6c 6c 6f 63 61 74 65 64 2c  e was allocated,
1f350 20 70 61 67 65 20 31 20 77 61 73 20 6d 61 64 65   page 1 was made
1f351 20 77 72 69 74 61 62 6c 65 20 69 6e 0a 20 20 20   writable in.   
1f352 20 2a 2a 20 6f 72 64 65 72 20 65 69 74 68 65 72   ** order either
1f353 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65   to increase the
1f354 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 69   database filesi
1f355 7a 65 2c 20 6f 72 20 74 6f 20 64 65 63 72 65 6d  ze, or to decrem
1f356 65 6e 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 66  ent the.    ** f
1f357 72 65 65 6c 69 73 74 20 63 6f 75 6e 74 2e 20 20  reelist count.  
1f358 48 65 6e 63 65 2c 20 74 68 65 20 73 71 6c 69 74  Hence, the sqlit
1f359 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74  e3BtreeUpdateMet
1f35a 61 28 29 20 63 61 6c 6c 20 63 61 6e 6e 6f 74 20  a() call cannot 
1f35b 66 61 69 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  fail..    */.   
1f35c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1f35d 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
1f35e 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44  (pBt->pPage1->pD
1f35f 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 72 63  bPage) );.    rc
1f360 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55   = sqlite3BtreeU
1f361 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20  pdateMeta(p, 4, 
1f362 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69  pgnoRoot);.    i
1f363 66 28 20 4e 45 56 45 52 28 72 63 29 20 29 7b 0a  f( NEVER(rc) ){.
1f364 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
1f365 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20  e(pRoot);.      
1f366 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
1f367 0a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ..  }else{.    r
1f368 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65  c = allocateBtre
1f369 65 50 61 67 65 28 70 42 74 2c 20 26 70 52 6f 6f  ePage(pBt, &pRoo
1f36a 74 2c 20 26 70 67 6e 6f 52 6f 6f 74 2c 20 31 2c  t, &pgnoRoot, 1,
1f36b 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20   0);.    if( rc 
1f36c 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
1f36d 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74  .#endif.  assert
1f36e 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
1f36f 77 72 69 74 65 61 62 6c 65 28 70 52 6f 6f 74 2d  writeable(pRoot-
1f370 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69  >pDbPage) );.  i
1f371 66 28 20 63 72 65 61 74 65 54 61 62 46 6c 61 67  f( createTabFlag
1f372 73 20 26 20 42 54 52 45 45 5f 49 4e 54 4b 45 59  s & BTREE_INTKEY
1f373 20 29 7b 0a 20 20 20 20 70 74 66 46 6c 61 67 73   ){.    ptfFlags
1f374 20 3d 20 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20   = PTF_INTKEY | 
1f375 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50  PTF_LEAFDATA | P
1f376 54 46 5f 4c 45 41 46 3b 0a 20 20 7d 65 6c 73 65  TF_LEAF;.  }else
1f377 7b 0a 20 20 20 20 70 74 66 46 6c 61 67 73 20 3d  {.    ptfFlags =
1f378 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20   PTF_ZERODATA | 
1f379 50 54 46 5f 4c 45 41 46 3b 0a 20 20 7d 0a 20 20  PTF_LEAF;.  }.  
1f37a 7a 65 72 6f 50 61 67 65 28 70 52 6f 6f 74 2c 20  zeroPage(pRoot, 
1f37b 70 74 66 46 6c 61 67 73 29 3b 0a 20 20 73 71 6c  ptfFlags);.  sql
1f37c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
1f37d 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Root->pDbPage);.
1f37e 20 20 61 73 73 65 72 74 28 20 28 70 42 74 2d 3e    assert( (pBt->
1f37f 6f 70 65 6e 46 6c 61 67 73 20 26 20 42 54 52 45  openFlags & BTRE
1f380 45 5f 53 49 4e 47 4c 45 29 3d 3d 30 20 7c 7c 20  E_SINGLE)==0 || 
1f381 70 67 6e 6f 52 6f 6f 74 3d 3d 32 20 29 3b 0a 20  pgnoRoot==2 );. 
1f382 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74   *piTable = (int
1f383 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74  )pgnoRoot;.  ret
1f384 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1f385 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f386 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
1f387 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65  CreateTable(Btre
1f388 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62  e *p, int *piTab
1f389 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  le, int flags){.
1f38a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
1f38b 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
1f38c 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 72  ;.  rc = btreeCr
1f38d 65 61 74 65 54 61 62 6c 65 28 70 2c 20 70 69 54  eateTable(p, piT
1f38e 61 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20  able, flags);.  
1f38f 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1f390 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
1f391 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73  c;.}../*.** Eras
1f392 65 20 74 68 65 20 67 69 76 65 6e 20 64 61 74 61  e the given data
1f393 62 61 73 65 20 70 61 67 65 20 61 6e 64 20 61 6c  base page and al
1f394 6c 20 69 74 73 20 63 68 69 6c 64 72 65 6e 2e 20  l its children. 
1f395 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 70   Return.** the p
1f396 61 67 65 20 74 6f 20 74 68 65 20 66 72 65 65 6c  age to the freel
1f397 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ist..*/.static i
1f398 6e 74 20 63 6c 65 61 72 44 61 74 61 62 61 73 65  nt clearDatabase
1f399 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64  Page(.  BtShared
1f39a 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20   *pBt,          
1f39b 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 74 68   /* The BTree th
1f39c 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
1f39d 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  table */.  Pgno 
1f39e 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20  pgno,           
1f39f 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62      /* Page numb
1f3a0 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 20  er to clear */. 
1f3a1 20 69 6e 74 20 66 72 65 65 50 61 67 65 46 6c 61   int freePageFla
1f3a2 67 2c 20 20 20 20 20 20 20 20 2f 2a 20 44 65 61  g,        /* Dea
1f3a3 6c 6c 6f 63 61 74 65 20 70 61 67 65 20 69 66 20  llocate page if 
1f3a4 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  true */.  int *p
1f3a5 6e 43 68 61 6e 67 65 20 20 20 20 20 20 20 20 20  nChange         
1f3a6 20 20 20 2f 2a 20 41 64 64 20 6e 75 6d 62 65 72     /* Add number
1f3a7 20 6f 66 20 43 65 6c 6c 73 20 66 72 65 65 64 20   of Cells freed 
1f3a8 74 6f 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20  to this counter 
1f3a9 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20  */.){.  MemPage 
1f3aa 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 72 63  *pPage;.  int rc
1f3ab 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
1f3ac 72 20 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20  r *pCell;.  int 
1f3ad 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  i;..  assert( sq
1f3ae 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1f3af 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
1f3b0 20 20 69 66 28 20 70 67 6e 6f 3e 62 74 72 65 65    if( pgno>btree
1f3b1 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29  Pagecount(pBt) )
1f3b2 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1f3b3 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1f3b4 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65  ;.  }..  rc = ge
1f3b5 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74  tAndInitPage(pBt
1f3b6 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67 65 29 3b  , pgno, &pPage);
1f3b7 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
1f3b8 72 6e 20 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30  rn rc;.  for(i=0
1f3b9 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ; i<pPage->nCell
1f3ba 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43 65 6c  ; i++){.    pCel
1f3bb 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61  l = findCell(pPa
1f3bc 67 65 2c 20 69 29 3b 0a 20 20 20 20 69 66 28 20  ge, i);.    if( 
1f3bd 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
1f3be 20 20 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72        rc = clear
1f3bf 44 61 74 61 62 61 73 65 50 61 67 65 28 70 42 74  DatabasePage(pBt
1f3c0 2c 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c  , get4byte(pCell
1f3c1 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b  ), 1, pnChange);
1f3c2 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20  .      if( rc ) 
1f3c3 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61  goto cleardataba
1f3c4 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20  sepage_out;.    
1f3c5 7d 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72  }.    rc = clear
1f3c6 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c  Cell(pPage, pCel
1f3c7 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  l);.    if( rc )
1f3c8 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62   goto cleardatab
1f3c9 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d  asepage_out;.  }
1f3ca 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c  .  if( !pPage->l
1f3cb 65 61 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  eaf ){.    rc = 
1f3cc 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67  clearDatabasePag
1f3cd 65 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28  e(pBt, get4byte(
1f3ce 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 38 5d  &pPage->aData[8]
1f3cf 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b  ), 1, pnChange);
1f3d0 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f  .    if( rc ) go
1f3d1 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 65  to cleardatabase
1f3d2 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73  page_out;.  }els
1f3d3 65 20 69 66 28 20 70 6e 43 68 61 6e 67 65 20 29  e if( pnChange )
1f3d4 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
1f3d5 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20  age->intKey );. 
1f3d6 20 20 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d 20     *pnChange += 
1f3d7 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20  pPage->nCell;.  
1f3d8 7d 0a 20 20 69 66 28 20 66 72 65 65 50 61 67 65  }.  if( freePage
1f3d9 46 6c 61 67 20 29 7b 0a 20 20 20 20 66 72 65 65  Flag ){.    free
1f3da 50 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29  Page(pPage, &rc)
1f3db 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72  ;.  }else if( (r
1f3dc 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1f3dd 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62  Write(pPage->pDb
1f3de 50 61 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20  Page))==0 ){.   
1f3df 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c   zeroPage(pPage,
1f3e0 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d   pPage->aData[0]
1f3e1 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20   | PTF_LEAF);.  
1f3e2 7d 0a 0a 63 6c 65 61 72 64 61 74 61 62 61 73 65  }..cleardatabase
1f3e3 70 61 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c 65  page_out:.  rele
1f3e4 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
1f3e5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1f3e6 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c  /*.** Delete all
1f3e7 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
1f3e8 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65  m a single table
1f3e9 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1f3ea 2e 20 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a 20  .  iTable is.** 
1f3eb 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
1f3ec 6f 66 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74  of the root of t
1f3ed 68 65 20 74 61 62 6c 65 2e 20 20 41 66 74 65 72  he table.  After
1f3ee 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
1f3ef 74 75 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72 6f  turns,.** the ro
1f3f0 6f 74 20 70 61 67 65 20 69 73 20 65 6d 70 74 79  ot page is empty
1f3f1 2c 20 62 75 74 20 73 74 69 6c 6c 20 65 78 69 73  , but still exis
1f3f2 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ts..**.** This r
1f3f3 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c  outine will fail
1f3f4 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43   with SQLITE_LOC
1f3f5 4b 45 44 20 69 66 20 74 68 65 72 65 20 61 72 65  KED if there are
1f3f6 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65 61   any open.** rea
1f3f7 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65  d cursors on the
1f3f8 20 74 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77 72   table.  Open wr
1f3f9 69 74 65 20 63 75 72 73 6f 72 73 20 61 72 65 20  ite cursors are 
1f3fa 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20  moved to the.** 
1f3fb 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c  root of the tabl
1f3fc 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43 68  e..**.** If pnCh
1f3fd 61 6e 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  ange is not NULL
1f3fe 2c 20 74 68 65 6e 20 74 61 62 6c 65 20 69 54 61  , then table iTa
1f3ff 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6e 20 69  ble must be an i
1f400 6e 74 6b 65 79 20 74 61 62 6c 65 2e 20 54 68 65  ntkey table. The
1f401 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75  .** integer valu
1f402 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  e pointed to by 
1f403 70 6e 43 68 61 6e 67 65 20 69 73 20 69 6e 63 72  pnChange is incr
1f404 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e  emented by the n
1f405 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 72  umber of.** entr
1f406 69 65 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  ies in the table
1f407 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1f408 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
1f409 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 42  treeClearTable(B
1f40a 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61  tree *p, int iTa
1f40b 62 6c 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61 6e  ble, int *pnChan
1f40c 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ge){.  int rc;. 
1f40d 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1f40e 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74   p->pBt;.  sqlit
1f40f 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
1f410 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e  .  assert( p->in
1f411 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49  Trans==TRANS_WRI
1f412 54 45 20 29 3b 0a 0a 20 20 72 63 20 3d 20 73 61  TE );..  rc = sa
1f413 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74  veAllCursors(pBt
1f414 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20  , (Pgno)iTable, 
1f415 30 29 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54  0);..  if( SQLIT
1f416 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20  E_OK==rc ){.    
1f417 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c  /* Invalidate al
1f418 6c 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f  l incrblob curso
1f419 72 73 20 6f 70 65 6e 20 6f 6e 20 74 61 62 6c 65  rs open on table
1f41a 20 69 54 61 62 6c 65 20 28 61 73 73 75 6d 69 6e   iTable (assumin
1f41b 67 20 69 54 61 62 6c 65 0a 20 20 20 20 2a 2a 20  g iTable.    ** 
1f41c 69 73 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 61  is the root of a
1f41d 20 74 61 62 6c 65 20 62 2d 74 72 65 65 20 2d 20   table b-tree - 
1f41e 69 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 74 68  if it is not, th
1f41f 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c  e following call
1f420 20 69 73 0a 20 20 20 20 2a 2a 20 61 20 6e 6f 2d   is.    ** a no-
1f421 6f 70 29 2e 20 20 2a 2f 0a 20 20 20 20 69 6e 76  op).  */.    inv
1f422 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43  alidateIncrblobC
1f423 75 72 73 6f 72 73 28 70 2c 20 30 2c 20 31 29 3b  ursors(p, 0, 1);
1f424 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44  .    rc = clearD
1f425 61 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c  atabasePage(pBt,
1f426 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30   (Pgno)iTable, 0
1f427 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20 20 7d  , pnChange);.  }
1f428 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
1f429 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
1f42a 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  n rc;.}../*.** E
1f42b 72 61 73 65 20 61 6c 6c 20 69 6e 66 6f 72 6d 61  rase all informa
1f42c 74 69 6f 6e 20 69 6e 20 61 20 74 61 62 6c 65 20  tion in a table 
1f42d 61 6e 64 20 61 64 64 20 74 68 65 20 72 6f 6f 74  and add the root
1f42e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
1f42f 0a 2a 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74  .** the freelist
1f430 2e 20 20 45 78 63 65 70 74 2c 20 74 68 65 20 72  .  Except, the r
1f431 6f 6f 74 20 6f 66 20 74 68 65 20 70 72 69 6e 63  oot of the princ
1f432 69 70 6c 65 20 74 61 62 6c 65 20 28 74 68 65 20  iple table (the 
1f433 6f 6e 65 20 6f 6e 0a 2a 2a 20 70 61 67 65 20 31  one on.** page 1
1f434 29 20 69 73 20 6e 65 76 65 72 20 61 64 64 65 64  ) is never added
1f435 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74   to the freelist
1f436 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1f437 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20 77  tine will fail w
1f438 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  ith SQLITE_LOCKE
1f439 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61  D if there are a
1f43a 6e 79 20 6f 70 65 6e 0a 2a 2a 20 63 75 72 73 6f  ny open.** curso
1f43b 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e  rs on the table.
1f43c 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41  .**.** If AUTOVA
1f43d 43 55 55 4d 20 69 73 20 65 6e 61 62 6c 65 64 20  CUUM is enabled 
1f43e 61 6e 64 20 74 68 65 20 70 61 67 65 20 61 74 20  and the page at 
1f43f 69 54 61 62 6c 65 20 69 73 20 6e 6f 74 20 74 68  iTable is not th
1f440 65 20 6c 61 73 74 0a 2a 2a 20 72 6f 6f 74 20 70  e last.** root p
1f441 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  age in the datab
1f442 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 74  ase file, then t
1f443 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67  he last root pag
1f444 65 20 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61 74  e .** in the dat
1f445 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d 6f  abase file is mo
1f446 76 65 64 20 69 6e 74 6f 20 74 68 65 20 73 6c 6f  ved into the slo
1f447 74 20 66 6f 72 6d 65 72 6c 79 20 6f 63 63 75 70  t formerly occup
1f448 69 65 64 20 62 79 0a 2a 2a 20 69 54 61 62 6c 65  ied by.** iTable
1f449 20 61 6e 64 20 74 68 61 74 20 6c 61 73 74 20 73   and that last s
1f44a 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f 63 63  lot formerly occ
1f44b 75 70 69 65 64 20 62 79 20 74 68 65 20 6c 61 73  upied by the las
1f44c 74 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69  t root page.** i
1f44d 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66  s added to the f
1f44e 72 65 65 6c 69 73 74 20 69 6e 73 74 65 61 64 20  reelist instead 
1f44f 6f 66 20 69 54 61 62 6c 65 2e 20 20 49 6e 20 74  of iTable.  In t
1f450 68 69 73 20 73 61 79 2c 20 61 6c 6c 0a 2a 2a 20  his say, all.** 
1f451 72 6f 6f 74 20 70 61 67 65 73 20 61 72 65 20 6b  root pages are k
1f452 65 70 74 20 61 74 20 74 68 65 20 62 65 67 69 6e  ept at the begin
1f453 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61  ning of the data
1f454 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68  base file, which
1f455 0a 2a 2a 20 69 73 20 6e 65 63 65 73 73 61 72 79  .** is necessary
1f456 20 66 6f 72 20 41 55 54 4f 56 41 43 55 55 4d 20   for AUTOVACUUM 
1f457 74 6f 20 77 6f 72 6b 20 72 69 67 68 74 2e 20 20  to work right.  
1f458 2a 70 69 4d 6f 76 65 64 20 69 73 20 73 65 74 20  *piMoved is set 
1f459 74 6f 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20  to the .** page 
1f45a 6e 75 6d 62 65 72 20 74 68 61 74 20 75 73 65 64  number that used
1f45b 20 74 6f 20 62 65 20 74 68 65 20 6c 61 73 74 20   to be the last 
1f45c 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65  root page in the
1f45d 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 2a 2a 20   file before.** 
1f45e 74 68 65 20 6d 6f 76 65 2e 20 20 49 66 20 6e 6f  the move.  If no
1f45f 20 70 61 67 65 20 67 65 74 73 20 6d 6f 76 65 64   page gets moved
1f460 2c 20 2a 70 69 4d 6f 76 65 64 20 69 73 20 73 65  , *piMoved is se
1f461 74 20 74 6f 20 30 2e 0a 2a 2a 20 54 68 65 20 6c  t to 0..** The l
1f462 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 69 73  ast root page is
1f463 20 72 65 63 6f 72 64 65 64 20 69 6e 20 6d 65 74   recorded in met
1f464 61 5b 33 5d 20 61 6e 64 20 74 68 65 20 76 61 6c  a[3] and the val
1f465 75 65 20 6f 66 0a 2a 2a 20 6d 65 74 61 5b 33 5d  ue of.** meta[3]
1f466 20 69 73 20 75 70 64 61 74 65 64 20 62 79 20 74   is updated by t
1f467 68 69 73 20 70 72 6f 63 65 64 75 72 65 2e 0a 2a  his procedure..*
1f468 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72  /.static int btr
1f469 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65  eeDropTable(Btre
1f46a 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 6c  e *p, Pgno iTabl
1f46b 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65 64 29  e, int *piMoved)
1f46c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65  {.  int rc;.  Me
1f46d 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30  mPage *pPage = 0
1f46e 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
1f46f 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61  t = p->pBt;..  a
1f470 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
1f471 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29  reeHoldsMutex(p)
1f472 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
1f473 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f  >inTrans==TRANS_
1f474 57 52 49 54 45 20 29 3b 0a 0a 20 20 2f 2a 20 49  WRITE );..  /* I
1f475 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20  t is illegal to 
1f476 64 72 6f 70 20 61 20 74 61 62 6c 65 20 69 66 20  drop a table if 
1f477 61 6e 79 20 63 75 72 73 6f 72 73 20 61 72 65 20  any cursors are 
1f478 6f 70 65 6e 20 6f 6e 20 74 68 65 0a 20 20 2a 2a  open on the.  **
1f479 20 64 61 74 61 62 61 73 65 2e 20 54 68 69 73 20   database. This 
1f47a 69 73 20 62 65 63 61 75 73 65 20 69 6e 20 61 75  is because in au
1f47b 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 74  to-vacuum mode t
1f47c 68 65 20 62 61 63 6b 65 6e 64 20 6d 61 79 0a 20  he backend may. 
1f47d 20 2a 2a 20 6e 65 65 64 20 74 6f 20 6d 6f 76 65   ** need to move
1f47e 20 61 6e 6f 74 68 65 72 20 72 6f 6f 74 2d 70 61   another root-pa
1f47f 67 65 20 74 6f 20 66 69 6c 6c 20 61 20 67 61 70  ge to fill a gap
1f480 20 6c 65 66 74 20 62 79 20 74 68 65 20 64 65 6c   left by the del
1f481 65 74 65 64 0a 20 20 2a 2a 20 72 6f 6f 74 20 70  eted.  ** root p
1f482 61 67 65 2e 20 49 66 20 61 6e 20 6f 70 65 6e 20  age. If an open 
1f483 63 75 72 73 6f 72 20 77 61 73 20 75 73 69 6e 67  cursor was using
1f484 20 74 68 69 73 20 70 61 67 65 20 61 20 70 72 6f   this page a pro
1f485 62 6c 65 6d 20 77 6f 75 6c 64 20 0a 20 20 2a 2a  blem would .  **
1f486 20 6f 63 63 75 72 2e 0a 20 20 2a 2a 0a 20 20 2a   occur..  **.  *
1f487 2a 20 54 68 69 73 20 65 72 72 6f 72 20 69 73 20  * This error is 
1f488 63 61 75 67 68 74 20 6c 6f 6e 67 20 62 65 66 6f  caught long befo
1f489 72 65 20 63 6f 6e 74 72 6f 6c 20 72 65 61 63 68  re control reach
1f48a 65 73 20 74 68 69 73 20 70 6f 69 6e 74 2e 0a 20  es this point.. 
1f48b 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28   */.  if( NEVER(
1f48c 70 42 74 2d 3e 70 43 75 72 73 6f 72 29 20 29 7b  pBt->pCursor) ){
1f48d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e  .    sqlite3Conn
1f48e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d  ectionBlocked(p-
1f48f 3e 64 62 2c 20 70 42 74 2d 3e 70 43 75 72 73 6f  >db, pBt->pCurso
1f490 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a  r->pBtree->db);.
1f491 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1f492 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43  E_LOCKED_SHAREDC
1f493 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  ACHE;.  }..  rc 
1f494 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
1f495 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65  Bt, (Pgno)iTable
1f496 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a 20 20  , &pPage, 0);.  
1f497 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
1f498 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  rc;.  rc = sqlit
1f499 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c  e3BtreeClearTabl
1f49a 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b  e(p, iTable, 0);
1f49b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
1f49c 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
1f49d 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ge);.    return 
1f49e 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 4d 6f  rc;.  }..  *piMo
1f49f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20  ved = 0;..  if( 
1f4a0 69 54 61 62 6c 65 3e 31 20 29 7b 0a 23 69 66 64  iTable>1 ){.#ifd
1f4a1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1f4a2 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 66 72  UTOVACUUM.    fr
1f4a3 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 26 72  eePage(pPage, &r
1f4a4 63 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50  c);.    releaseP
1f4a5 61 67 65 28 70 50 61 67 65 29 3b 0a 23 65 6c 73  age(pPage);.#els
1f4a6 65 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61  e.    if( pBt->a
1f4a7 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20  utoVacuum ){.   
1f4a8 20 20 20 50 67 6e 6f 20 6d 61 78 52 6f 6f 74 50     Pgno maxRootP
1f4a9 67 6e 6f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  gno;.      sqlit
1f4aa 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70  e3BtreeGetMeta(p
1f4ab 2c 20 42 54 52 45 45 5f 4c 41 52 47 45 53 54 5f  , BTREE_LARGEST_
1f4ac 52 4f 4f 54 5f 50 41 47 45 2c 20 26 6d 61 78 52  ROOT_PAGE, &maxR
1f4ad 6f 6f 74 50 67 6e 6f 29 3b 0a 0a 20 20 20 20 20  ootPgno);..     
1f4ae 20 69 66 28 20 69 54 61 62 6c 65 3d 3d 6d 61 78   if( iTable==max
1f4af 52 6f 6f 74 50 67 6e 6f 20 29 7b 0a 20 20 20 20  RootPgno ){.    
1f4b0 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61      /* If the ta
1f4b1 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65  ble being droppe
1f4b2 64 20 69 73 20 74 68 65 20 74 61 62 6c 65 20 77  d is the table w
1f4b3 69 74 68 20 74 68 65 20 6c 61 72 67 65 73 74 20  ith the largest 
1f4b4 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20  root-page.      
1f4b5 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74    ** number in t
1f4b6 68 65 20 64 61 74 61 62 61 73 65 2c 20 70 75 74  he database, put
1f4b7 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f   the root page o
1f4b8 6e 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e  n the free list.
1f4b9 20 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20   .        */.   
1f4ba 20 20 20 20 20 66 72 65 65 50 61 67 65 28 70 50       freePage(pP
1f4bb 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20  age, &rc);.     
1f4bc 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1f4bd 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69  Page);.        i
1f4be 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1f4bf 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
1f4c0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
1f4c1 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
1f4c2 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
1f4c3 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70  able being dropp
1f4c4 65 64 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  ed does not have
1f4c5 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f   the largest roo
1f4c6 74 2d 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a  t-page.        *
1f4c7 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 20  * number in the 
1f4c8 64 61 74 61 62 61 73 65 2e 20 53 6f 20 6d 6f 76  database. So mov
1f4c9 65 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20  e the page that 
1f4ca 64 6f 65 73 20 69 6e 74 6f 20 74 68 65 20 0a 20  does into the . 
1f4cb 20 20 20 20 20 20 20 2a 2a 20 67 61 70 20 6c 65         ** gap le
1f4cc 66 74 20 62 79 20 74 68 65 20 64 65 6c 65 74 65  ft by the delete
1f4cd 64 20 72 6f 6f 74 2d 70 61 67 65 2e 0a 20 20 20  d root-page..   
1f4ce 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1f4cf 4d 65 6d 50 61 67 65 20 2a 70 4d 6f 76 65 3b 0a  MemPage *pMove;.
1f4d0 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
1f4d1 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
1f4d2 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65      rc = btreeGe
1f4d3 74 50 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f  tPage(pBt, maxRo
1f4d4 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20  otPgno, &pMove, 
1f4d5 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0);.        if( 
1f4d6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1f4d7 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
1f4d8 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
1f4d9 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c          rc = rel
1f4da 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20 70  ocatePage(pBt, p
1f4db 4d 6f 76 65 2c 20 50 54 52 4d 41 50 5f 52 4f 4f  Move, PTRMAP_ROO
1f4dc 54 50 41 47 45 2c 20 30 2c 20 69 54 61 62 6c 65  TPAGE, 0, iTable
1f4dd 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 72 65  , 0);.        re
1f4de 6c 65 61 73 65 50 61 67 65 28 70 4d 6f 76 65 29  leasePage(pMove)
1f4df 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
1f4e0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1f4e1 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
1f4e2 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rc;.        }.  
1f4e3 20 20 20 20 20 20 70 4d 6f 76 65 20 3d 20 30 3b        pMove = 0;
1f4e4 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74  .        rc = bt
1f4e5 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
1f4e6 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d  maxRootPgno, &pM
1f4e7 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ove, 0);.       
1f4e8 20 66 72 65 65 50 61 67 65 28 70 4d 6f 76 65 2c   freePage(pMove,
1f4e9 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 72   &rc);.        r
1f4ea 65 6c 65 61 73 65 50 61 67 65 28 70 4d 6f 76 65  eleasePage(pMove
1f4eb 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
1f4ec 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1f4ed 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
1f4ee 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   rc;.        }. 
1f4ef 20 20 20 20 20 20 20 2a 70 69 4d 6f 76 65 64 20         *piMoved 
1f4f0 3d 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20  = maxRootPgno;. 
1f4f1 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
1f4f2 20 53 65 74 20 74 68 65 20 6e 65 77 20 27 6d 61   Set the new 'ma
1f4f3 78 2d 72 6f 6f 74 2d 70 61 67 65 27 20 76 61 6c  x-root-page' val
1f4f4 75 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ue in the databa
1f4f5 73 65 20 68 65 61 64 65 72 2e 20 54 68 69 73 0a  se header. This.
1f4f6 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20        ** is the 
1f4f7 6f 6c 64 20 76 61 6c 75 65 20 6c 65 73 73 20 6f  old value less o
1f4f8 6e 65 2c 20 6c 65 73 73 20 6f 6e 65 20 6d 6f 72  ne, less one mor
1f4f9 65 20 69 66 20 74 68 61 74 20 68 61 70 70 65 6e  e if that happen
1f4fa 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 62 65  s to.      ** be
1f4fb 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d   a root-page num
1f4fc 62 65 72 2c 20 6c 65 73 73 20 6f 6e 65 20 61 67  ber, less one ag
1f4fd 61 69 6e 20 69 66 20 74 68 61 74 20 69 73 20 74  ain if that is t
1f4fe 68 65 0a 20 20 20 20 20 20 2a 2a 20 50 45 4e 44  he.      ** PEND
1f4ff 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 2e 0a 20  ING_BYTE_PAGE.. 
1f500 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6d 61       */.      ma
1f501 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20  xRootPgno--;.   
1f502 20 20 20 77 68 69 6c 65 28 20 6d 61 78 52 6f 6f     while( maxRoo
1f503 74 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42  tPgno==PENDING_B
1f504 59 54 45 5f 50 41 47 45 28 70 42 74 29 0a 20 20  YTE_PAGE(pBt).  
1f505 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 50 54             || PT
1f506 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c  RMAP_ISPAGE(pBt,
1f507 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 20 29 7b   maxRootPgno) ){
1f508 0a 20 20 20 20 20 20 20 20 6d 61 78 52 6f 6f 74  .        maxRoot
1f509 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a  Pgno--;.      }.
1f50a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d 61        assert( ma
1f50b 78 52 6f 6f 74 50 67 6e 6f 21 3d 50 45 4e 44 49  xRootPgno!=PENDI
1f50c 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
1f50d 29 20 29 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d  ) );..      rc =
1f50e 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
1f50f 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 6d 61  ateMeta(p, 4, ma
1f510 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20 20 20  xRootPgno);.    
1f511 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 72 65  }else{.      fre
1f512 65 50 61 67 65 28 70 50 61 67 65 2c 20 26 72 63  ePage(pPage, &rc
1f513 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  );.      release
1f514 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
1f515 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73   }.#endif.  }els
1f516 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 73 71 6c  e{.    /* If sql
1f517 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62  ite3BtreeDropTab
1f518 6c 65 20 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e  le was called on
1f519 20 70 61 67 65 20 31 2e 0a 20 20 20 20 2a 2a 20   page 1..    ** 
1f51a 54 68 69 73 20 72 65 61 6c 6c 79 20 6e 65 76 65  This really neve
1f51b 72 20 73 68 6f 75 6c 64 20 68 61 70 70 65 6e 20  r should happen 
1f51c 65 78 63 65 70 74 20 69 6e 20 61 20 63 6f 72 72  except in a corr
1f51d 75 70 74 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  upt.    ** datab
1f51e 61 73 65 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20  ase. .    */.   
1f51f 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c   zeroPage(pPage,
1f520 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f   PTF_INTKEY|PTF_
1f521 4c 45 41 46 20 29 3b 0a 20 20 20 20 72 65 6c 65  LEAF );.    rele
1f522 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
1f523 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1f524 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56    .}.SQLITE_PRIV
1f525 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
1f526 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74  treeDropTable(Bt
1f527 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62  ree *p, int iTab
1f528 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65 64  le, int *piMoved
1f529 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
1f52a 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1f52b 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65  (p);.  rc = btre
1f52c 65 44 72 6f 70 54 61 62 6c 65 28 70 2c 20 69 54  eDropTable(p, iT
1f52d 61 62 6c 65 2c 20 70 69 4d 6f 76 65 64 29 3b 0a  able, piMoved);.
1f52e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1f52f 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
1f530 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54   rc;.}.../*.** T
1f531 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
1f532 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20   only be called 
1f533 69 66 20 74 68 65 20 62 2d 74 72 65 65 20 63 6f  if the b-tree co
1f534 6e 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61 64 79  nnection already
1f535 0a 2a 2a 20 68 61 73 20 61 20 72 65 61 64 20 6f  .** has a read o
1f536 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  r write transact
1f537 69 6f 6e 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20  ion open on the 
1f538 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  database..**.** 
1f539 52 65 61 64 20 74 68 65 20 6d 65 74 61 2d 69 6e  Read the meta-in
1f53a 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 6f 66  formation out of
1f53b 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
1f53c 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73  .  Meta[0].** is
1f53d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66   the number of f
1f53e 72 65 65 20 70 61 67 65 73 20 63 75 72 72 65 6e  ree pages curren
1f53f 74 6c 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  tly in the datab
1f540 61 73 65 2e 20 20 4d 65 74 61 5b 31 5d 0a 2a 2a  ase.  Meta[1].**
1f541 20 74 68 72 6f 75 67 68 20 6d 65 74 61 5b 31 35   through meta[15
1f542 5d 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20  ] are available 
1f543 66 6f 72 20 75 73 65 20 62 79 20 68 69 67 68 65  for use by highe
1f544 72 20 6c 61 79 65 72 73 2e 20 20 4d 65 74 61 5b  r layers.  Meta[
1f545 30 5d 0a 2a 2a 20 69 73 20 72 65 61 64 2d 6f 6e  0].** is read-on
1f546 6c 79 2c 20 74 68 65 20 6f 74 68 65 72 73 20 61  ly, the others a
1f547 72 65 20 72 65 61 64 2f 77 72 69 74 65 2e 0a 2a  re read/write..*
1f548 2a 20 0a 2a 2a 20 54 68 65 20 73 63 68 65 6d 61  * .** The schema
1f549 20 6c 61 79 65 72 20 6e 75 6d 62 65 72 73 20 6d   layer numbers m
1f54a 65 74 61 20 76 61 6c 75 65 73 20 64 69 66 66 65  eta values diffe
1f54b 72 65 6e 74 6c 79 2e 20 20 41 74 20 74 68 65 20  rently.  At the 
1f54c 73 63 68 65 6d 61 0a 2a 2a 20 6c 61 79 65 72 20  schema.** layer 
1f54d 28 61 6e 64 20 74 68 65 20 53 65 74 43 6f 6f 6b  (and the SetCook
1f54e 69 65 20 61 6e 64 20 52 65 61 64 43 6f 6f 6b 69  ie and ReadCooki
1f54f 65 20 6f 70 63 6f 64 65 73 29 20 74 68 65 20 6e  e opcodes) the n
1f550 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 66 72 65 65  umber of.** free
1f551 20 70 61 67 65 73 20 69 73 20 6e 6f 74 20 76 69   pages is not vi
1f552 73 69 62 6c 65 2e 20 20 53 6f 20 43 6f 6f 6b 69  sible.  So Cooki
1f553 65 5b 30 5d 20 69 73 20 74 68 65 20 73 61 6d 65  e[0] is the same
1f554 20 61 73 20 4d 65 74 61 5b 31 5d 2e 0a 2a 2f 0a   as Meta[1]..*/.
1f555 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1f556 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
1f557 47 65 74 4d 65 74 61 28 42 74 72 65 65 20 2a 70  GetMeta(Btree *p
1f558 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 2a  , int idx, u32 *
1f559 70 4d 65 74 61 29 7b 0a 20 20 42 74 53 68 61 72  pMeta){.  BtShar
1f55a 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
1f55b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
1f55c 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73  eEnter(p);.  ass
1f55d 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e  ert( p->inTrans>
1f55e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20  TRANS_NONE );.  
1f55f 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 5f 4f  assert( SQLITE_O
1f560 4b 3d 3d 71 75 65 72 79 53 68 61 72 65 64 43 61  K==querySharedCa
1f561 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  cheTableLock(p, 
1f562 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41  MASTER_ROOT, REA
1f563 44 5f 4c 4f 43 4b 29 20 29 3b 0a 20 20 61 73 73  D_LOCK) );.  ass
1f564 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31  ert( pBt->pPage1
1f565 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64   );.  assert( id
1f566 78 3e 3d 30 20 26 26 20 69 64 78 3c 3d 31 35 20  x>=0 && idx<=15 
1f567 29 3b 0a 0a 20 20 2a 70 4d 65 74 61 20 3d 20 67  );..  *pMeta = g
1f568 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50  et4byte(&pBt->pP
1f569 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 20 2b  age1->aData[36 +
1f56a 20 69 64 78 2a 34 5d 29 3b 0a 0a 20 20 2f 2a 20   idx*4]);..  /* 
1f56b 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69  If auto-vacuum i
1f56c 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20 74 68  s disabled in th
1f56d 69 73 20 62 75 69 6c 64 20 61 6e 64 20 74 68 69  is build and thi
1f56e 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63  s is an auto-vac
1f56f 75 75 6d 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  uum.  ** databas
1f570 65 2c 20 6d 61 72 6b 20 74 68 65 20 64 61 74 61  e, mark the data
1f571 62 61 73 65 20 61 73 20 72 65 61 64 2d 6f 6e 6c  base as read-onl
1f572 79 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51  y.  */.#ifdef SQ
1f573 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1f574 43 55 55 4d 0a 20 20 69 66 28 20 69 64 78 3d 3d  CUUM.  if( idx==
1f575 42 54 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f  BTREE_LARGEST_RO
1f576 4f 54 5f 50 41 47 45 20 26 26 20 2a 70 4d 65 74  OT_PAGE && *pMet
1f577 61 3e 30 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e  a>0 ){.    pBt->
1f578 62 74 73 46 6c 61 67 73 20 7c 3d 20 42 54 53 5f  btsFlags |= BTS_
1f579 52 45 41 44 5f 4f 4e 4c 59 3b 0a 20 20 7d 0a 23  READ_ONLY;.  }.#
1f57a 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33  endif..  sqlite3
1f57b 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 7d  BtreeLeave(p);.}
1f57c 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 65  ../*.** Write me
1f57d 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62  ta-information b
1f57e 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74  ack into the dat
1f57f 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d 20  abase.  Meta[0] 
1f580 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20  is.** read-only 
1f581 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20 77  and may not be w
1f582 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ritten..*/.SQLIT
1f583 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1f584 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
1f585 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20 69  Meta(Btree *p, i
1f586 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65 74  nt idx, u32 iMet
1f587 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  a){.  BtShared *
1f588 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
1f589 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
1f58a 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  P1;.  int rc;.  
1f58b 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20 26  assert( idx>=1 &
1f58c 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 73  & idx<=15 );.  s
1f58d 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1f58e 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  (p);.  assert( p
1f58f 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
1f590 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65  _WRITE );.  asse
1f591 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 21  rt( pBt->pPage1!
1f592 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d 20 70 42  =0 );.  pP1 = pB
1f593 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
1f594 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1f595 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e  PagerWrite(pBt->
1f596 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29  pPage1->pDbPage)
1f597 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
1f598 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 75 74  TE_OK ){.    put
1f599 34 62 79 74 65 28 26 70 50 31 5b 33 36 20 2b 20  4byte(&pP1[36 + 
1f59a 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61 29 3b 0a  idx*4], iMeta);.
1f59b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1f59c 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
1f59d 20 20 20 69 66 28 20 69 64 78 3d 3d 42 54 52 45     if( idx==BTRE
1f59e 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 29 7b  E_INCR_VACUUM ){
1f59f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1f5a0 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 7c  Bt->autoVacuum |
1f5a1 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a 20 20  | iMeta==0 );.  
1f5a2 20 20 20 20 61 73 73 65 72 74 28 20 69 4d 65 74      assert( iMet
1f5a3 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 3d 3d 31  a==0 || iMeta==1
1f5a4 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69   );.      pBt->i
1f5a5 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 75 38 29  ncrVacuum = (u8)
1f5a6 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a 23 65 6e  iMeta;.    }.#en
1f5a7 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  dif.  }.  sqlite
1f5a8 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
1f5a9 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1f5aa 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1f5ab 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 2f  MIT_BTREECOUNT./
1f5ac 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  *.** The first a
1f5ad 72 67 75 6d 65 6e 74 2c 20 70 43 75 72 2c 20 69  rgument, pCur, i
1f5ae 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65  s a cursor opene
1f5af 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74 72 65 65  d on some b-tree
1f5b0 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a 2a 20 6e  . Count the.** n
1f5b1 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
1f5b2 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20 61   in the b-tree a
1f5b3 6e 64 20 77 72 69 74 65 20 74 68 65 20 72 65 73  nd write the res
1f5b4 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74 72 79 2e  ult to *pnEntry.
1f5b5 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  .**.** SQLITE_OK
1f5b6 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
1f5b7 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  the operation is
1f5b8 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 65 78   successfully ex
1f5b9 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f 74 68 65  ecuted. .** Othe
1f5ba 72 77 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72  rwise, if an err
1f5bb 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
1f5bc 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f 20 65 72  d (i.e. an IO er
1f5bd 72 6f 72 20 6f 72 20 64 61 74 61 62 61 73 65 0a  ror or database.
1f5be 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 29 20 61  ** corruption) a
1f5bf 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
1f5c0 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
1f5c1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1f5c2 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1f5c3 72 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f  reeCount(BtCurso
1f5c4 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 6e  r *pCur, i64 *pn
1f5c5 45 6e 74 72 79 29 7b 0a 20 20 69 36 34 20 6e 45  Entry){.  i64 nE
1f5c6 6e 74 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20  ntry = 0;       
1f5c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1f5c8 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
1f5c9 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79 20 2a 2f  n in *pnEntry */
1f5ca 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
1f5cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f5cc 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
1f5cd 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69 66 28  n code */..  if(
1f5ce 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 3d   pCur->pgnoRoot=
1f5cf 3d 30 20 29 7b 0a 20 20 20 20 2a 70 6e 45 6e 74  =0 ){.    *pnEnt
1f5d0 72 79 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  ry = 0;.    retu
1f5d1 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1f5d2 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52  }.  rc = moveToR
1f5d3 6f 6f 74 28 70 43 75 72 29 3b 0a 0a 20 20 2f 2a  oot(pCur);..  /*
1f5d4 20 55 6e 6c 65 73 73 20 61 6e 20 65 72 72 6f 72   Unless an error
1f5d5 20 6f 63 63 75 72 73 2c 20 74 68 65 20 66 6f 6c   occurs, the fol
1f5d6 6c 6f 77 69 6e 67 20 6c 6f 6f 70 20 72 75 6e 73  lowing loop runs
1f5d7 20 6f 6e 65 20 69 74 65 72 61 74 69 6f 6e 20 66   one iteration f
1f5d8 6f 72 20 65 61 63 68 0a 20 20 2a 2a 20 70 61 67  or each.  ** pag
1f5d9 65 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65 20  e in the B-Tree 
1f5da 73 74 72 75 63 74 75 72 65 20 28 6e 6f 74 20 69  structure (not i
1f5db 6e 63 6c 75 64 69 6e 67 20 6f 76 65 72 66 6c 6f  ncluding overflo
1f5dc 77 20 70 61 67 65 73 29 2e 20 0a 20 20 2a 2f 0a  w pages). .  */.
1f5dd 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c    while( rc==SQL
1f5de 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e  ITE_OK ){.    in
1f5df 74 20 69 49 64 78 3b 20 20 20 20 20 20 20 20 20  t iIdx;         
1f5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f5e1 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 68 69   /* Index of chi
1f5e2 6c 64 20 6e 6f 64 65 20 69 6e 20 70 61 72 65 6e  ld node in paren
1f5e3 74 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65  t */.    MemPage
1f5e4 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20   *pPage;        
1f5e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
1f5e6 75 72 72 65 6e 74 20 70 61 67 65 20 6f 66 20 74  urrent page of t
1f5e7 68 65 20 62 2d 74 72 65 65 20 2a 2f 0a 0a 20 20  he b-tree */..  
1f5e8 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
1f5e9 61 20 6c 65 61 66 20 70 61 67 65 20 6f 72 20 74  a leaf page or t
1f5ea 68 65 20 74 72 65 65 20 69 73 20 6e 6f 74 20 61  he tree is not a
1f5eb 6e 20 69 6e 74 2d 6b 65 79 20 74 72 65 65 2c 20  n int-key tree, 
1f5ec 74 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74 68 69  then .    ** thi
1f5ed 73 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20  s page contains 
1f5ee 63 6f 75 6e 74 61 62 6c 65 20 65 6e 74 72 69 65  countable entrie
1f5ef 73 2e 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65  s. Increment the
1f5f0 20 65 6e 74 72 79 20 63 6f 75 6e 74 65 72 0a 20   entry counter. 
1f5f1 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 6c     ** accordingl
1f5f2 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50  y..    */.    pP
1f5f3 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
1f5f4 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b  ge[pCur->iPage];
1f5f5 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
1f5f6 6c 65 61 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e  leaf || !pPage->
1f5f7 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  intKey ){.      
1f5f8 6e 45 6e 74 72 79 20 2b 3d 20 70 50 61 67 65 2d  nEntry += pPage-
1f5f9 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 7d 0a 0a 20  >nCell;.    }.. 
1f5fa 20 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 61     /* pPage is a
1f5fb 20 6c 65 61 66 20 6e 6f 64 65 2e 20 54 68 69 73   leaf node. This
1f5fc 20 6c 6f 6f 70 20 6e 61 76 69 67 61 74 65 73 20   loop navigates 
1f5fd 74 68 65 20 63 75 72 73 6f 72 20 73 6f 20 74 68  the cursor so th
1f5fe 61 74 20 69 74 20 0a 20 20 20 20 2a 2a 20 70 6f  at it .    ** po
1f5ff 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73  ints to the firs
1f600 74 20 69 6e 74 65 72 69 6f 72 20 63 65 6c 6c 20  t interior cell 
1f601 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74  that it points t
1f602 6f 20 74 68 65 20 70 61 72 65 6e 74 20 6f 66 0a  o the parent of.
1f603 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 78 74 20      ** the next 
1f604 70 61 67 65 20 69 6e 20 74 68 65 20 74 72 65 65  page in the tree
1f605 20 74 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65   that has not ye
1f606 74 20 62 65 65 6e 20 76 69 73 69 74 65 64 2e 20  t been visited. 
1f607 54 68 65 0a 20 20 20 20 2a 2a 20 70 43 75 72 2d  The.    ** pCur-
1f608 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
1f609 67 65 5d 20 76 61 6c 75 65 20 69 73 20 73 65 74  ge] value is set
1f60a 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66   to the index of
1f60b 20 74 68 65 20 70 61 72 65 6e 74 20 63 65 6c 6c   the parent cell
1f60c 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70  .    ** of the p
1f60d 61 67 65 2c 20 6f 72 20 74 6f 20 74 68 65 20 6e  age, or to the n
1f60e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69  umber of cells i
1f60f 6e 20 74 68 65 20 70 61 67 65 20 69 66 20 74 68  n the page if th
1f610 65 20 6e 65 78 74 20 70 61 67 65 0a 20 20 20 20  e next page.    
1f611 2a 2a 20 74 6f 20 76 69 73 69 74 20 69 73 20 74  ** to visit is t
1f612 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f  he right-child o
1f613 66 20 69 74 73 20 70 61 72 65 6e 74 2e 0a 20 20  f its parent..  
1f614 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 61    **.    ** If a
1f615 6c 6c 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  ll pages in the 
1f616 74 72 65 65 20 68 61 76 65 20 62 65 65 6e 20 76  tree have been v
1f617 69 73 69 74 65 64 2c 20 72 65 74 75 72 6e 20 53  isited, return S
1f618 51 4c 49 54 45 5f 4f 4b 20 74 6f 20 74 68 65 0a  QLITE_OK to the.
1f619 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 0a 20      ** caller.. 
1f61a 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50     */.    if( pP
1f61b 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
1f61c 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20     do {.        
1f61d 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d  if( pCur->iPage=
1f61e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1f61f 2f 2a 20 41 6c 6c 20 70 61 67 65 73 20 6f 66 20  /* All pages of 
1f620 74 68 65 20 62 2d 74 72 65 65 20 68 61 76 65 20  the b-tree have 
1f621 62 65 65 6e 20 76 69 73 69 74 65 64 2e 20 52 65  been visited. Re
1f622 74 75 72 6e 20 73 75 63 63 65 73 73 66 75 6c 6c  turn successfull
1f623 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  y. */.          
1f624 2a 70 6e 45 6e 74 72 79 20 3d 20 6e 45 6e 74 72  *pnEntry = nEntr
1f625 79 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  y;.          ret
1f626 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
1f627 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1f628 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43   moveToParent(pC
1f629 75 72 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c  ur);.      }whil
1f62a 65 20 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  e ( pCur->aiIdx[
1f62b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 43  pCur->iPage]>=pC
1f62c 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
1f62d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29  >iPage]->nCell )
1f62e 3b 0a 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61  ;..      pCur->a
1f62f 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
1f630 5d 2b 2b 3b 0a 20 20 20 20 20 20 70 50 61 67 65  ]++;.      pPage
1f631 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
1f632 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
1f633 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 73 63    }..    /* Desc
1f634 65 6e 64 20 74 6f 20 74 68 65 20 63 68 69 6c 64  end to the child
1f635 20 6e 6f 64 65 20 6f 66 20 74 68 65 20 63 65 6c   node of the cel
1f636 6c 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  l that the curso
1f637 72 20 63 75 72 72 65 6e 74 6c 79 20 0a 20 20 20  r currently .   
1f638 20 2a 2a 20 70 6f 69 6e 74 73 20 61 74 2e 20 54   ** points at. T
1f639 68 69 73 20 69 73 20 74 68 65 20 72 69 67 68 74  his is the right
1f63a 2d 63 68 69 6c 64 20 69 66 20 28 69 49 64 78 3d  -child if (iIdx=
1f63b 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 2e 0a  =pPage->nCell)..
1f63c 20 20 20 20 2a 2f 0a 20 20 20 20 69 49 64 78 20      */.    iIdx 
1f63d 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  = pCur->aiIdx[pC
1f63e 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20  ur->iPage];.    
1f63f 69 66 28 20 69 49 64 78 3d 3d 70 50 61 67 65 2d  if( iIdx==pPage-
1f640 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20  >nCell ){.      
1f641 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64  rc = moveToChild
1f642 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 65 28  (pCur, get4byte(
1f643 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50  &pPage->aData[pP
1f644 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  age->hdrOffset+8
1f645 5d 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ]));.    }else{.
1f646 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54        rc = moveT
1f647 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74  oChild(pCur, get
1f648 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70  4byte(findCell(p
1f649 50 61 67 65 2c 20 69 49 64 78 29 29 29 3b 0a 20  Page, iIdx)));. 
1f64a 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
1f64b 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75  n error has occu
1f64c 72 72 65 64 2e 20 52 65 74 75 72 6e 20 61 6e 20  rred. Return an 
1f64d 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20  error code. */. 
1f64e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
1f64f 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
1f650 72 6e 20 74 68 65 20 70 61 67 65 72 20 61 73 73  rn the pager ass
1f651 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 42  ociated with a B
1f652 54 72 65 65 2e 20 20 54 68 69 73 20 72 6f 75 74  Tree.  This rout
1f653 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 0a  ine is used for.
1f654 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64  ** testing and d
1f655 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a  ebugging only..*
1f656 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1f657 20 50 61 67 65 72 20 2a 73 71 6c 69 74 65 33 42   Pager *sqlite3B
1f658 74 72 65 65 50 61 67 65 72 28 42 74 72 65 65 20  treePager(Btree 
1f659 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d  *p){.  return p-
1f65a 3e 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 7d 0a  >pBt->pPager;.}.
1f65b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1f65c 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43  OMIT_INTEGRITY_C
1f65d 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e  HECK./*.** Appen
1f65e 64 20 61 20 6d 65 73 73 61 67 65 20 74 6f 20 74  d a message to t
1f65f 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
1f660 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74   string..*/.stat
1f661 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41 70 70  ic void checkApp
1f662 65 6e 64 4d 73 67 28 0a 20 20 49 6e 74 65 67 72  endMsg(.  Integr
1f663 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 0a 20  ityCk *pCheck,. 
1f664 20 63 68 61 72 20 2a 7a 4d 73 67 31 2c 0a 20 20   char *zMsg1,.  
1f665 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
1f666 6d 61 74 2c 0a 20 20 2e 2e 2e 0a 29 7b 0a 20 20  mat,.  ....){.  
1f667 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66  va_list ap;.  if
1f668 28 20 21 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72  ( !pCheck->mxErr
1f669 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 43 68   ) return;.  pCh
1f66a 65 63 6b 2d 3e 6d 78 45 72 72 2d 2d 3b 0a 20 20  eck->mxErr--;.  
1f66b 70 43 68 65 63 6b 2d 3e 6e 45 72 72 2b 2b 3b 0a  pCheck->nErr++;.
1f66c 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
1f66d 46 6f 72 6d 61 74 29 3b 0a 20 20 69 66 28 20 70  Format);.  if( p
1f66e 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e 6e 43  Check->errMsg.nC
1f66f 68 61 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  har ){.    sqlit
1f670 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
1f671 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67  (&pCheck->errMsg
1f672 2c 20 22 5c 6e 22 2c 20 31 29 3b 0a 20 20 7d 0a  , "\n", 1);.  }.
1f673 20 20 69 66 28 20 7a 4d 73 67 31 20 29 7b 0a 20    if( zMsg1 ){. 
1f674 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
1f675 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b  umAppend(&pCheck
1f676 2d 3e 65 72 72 4d 73 67 2c 20 7a 4d 73 67 31 2c  ->errMsg, zMsg1,
1f677 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69   -1);.  }.  sqli
1f678 74 65 33 56 58 50 72 69 6e 74 66 28 26 70 43 68  te3VXPrintf(&pCh
1f679 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 31 2c 20  eck->errMsg, 1, 
1f67a 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
1f67b 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69 66  va_end(ap);.  if
1f67c 28 20 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67  ( pCheck->errMsg
1f67d 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  .mallocFailed ){
1f67e 0a 20 20 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c  .    pCheck->mal
1f67f 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
1f680 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53   }.}.#endif /* S
1f681 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
1f682 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23  RITY_CHECK */..#
1f683 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f684 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
1f685 43 4b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  CK../*.** Return
1f686 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65   non-zero if the
1f687 20 62 69 74 20 69 6e 20 74 68 65 20 49 6e 74 65   bit in the Inte
1f688 67 72 69 74 79 43 6b 2e 61 50 67 52 65 66 5b 5d  grityCk.aPgRef[]
1f689 20 61 72 72 61 79 20 74 68 61 74 0a 2a 2a 20 63   array that.** c
1f68a 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 70 61  orresponds to pa
1f68b 67 65 20 69 50 67 20 69 73 20 61 6c 72 65 61 64  ge iPg is alread
1f68c 79 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  y set..*/.static
1f68d 20 69 6e 74 20 67 65 74 50 61 67 65 52 65 66 65   int getPageRefe
1f68e 72 65 6e 63 65 64 28 49 6e 74 65 67 72 69 74 79  renced(Integrity
1f68f 43 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f  Ck *pCheck, Pgno
1f690 20 69 50 67 29 7b 0a 20 20 61 73 73 65 72 74 28   iPg){.  assert(
1f691 20 69 50 67 3c 3d 70 43 68 65 63 6b 2d 3e 6e 50   iPg<=pCheck->nP
1f692 61 67 65 20 26 26 20 73 69 7a 65 6f 66 28 70 43  age && sizeof(pC
1f693 68 65 63 6b 2d 3e 61 50 67 52 65 66 5b 30 5d 29  heck->aPgRef[0])
1f694 3d 3d 31 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ==1 );.  return 
1f695 28 70 43 68 65 63 6b 2d 3e 61 50 67 52 65 66 5b  (pCheck->aPgRef[
1f696 69 50 67 2f 38 5d 20 26 20 28 31 20 3c 3c 20 28  iPg/8] & (1 << (
1f697 69 50 67 20 26 20 30 78 30 37 29 29 29 3b 0a 7d  iPg & 0x07)));.}
1f698 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
1f699 62 69 74 20 69 6e 20 74 68 65 20 49 6e 74 65 67  bit in the Integ
1f69a 72 69 74 79 43 6b 2e 61 50 67 52 65 66 5b 5d 20  rityCk.aPgRef[] 
1f69b 61 72 72 61 79 20 74 68 61 74 20 63 6f 72 72 65  array that corre
1f69c 73 70 6f 6e 64 73 20 74 6f 20 70 61 67 65 20 69  sponds to page i
1f69d 50 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  Pg..*/.static vo
1f69e 69 64 20 73 65 74 50 61 67 65 52 65 66 65 72 65  id setPageRefere
1f69f 6e 63 65 64 28 49 6e 74 65 67 72 69 74 79 43 6b  nced(IntegrityCk
1f6a0 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f 20 69   *pCheck, Pgno i
1f6a1 50 67 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  Pg){.  assert( i
1f6a2 50 67 3c 3d 70 43 68 65 63 6b 2d 3e 6e 50 61 67  Pg<=pCheck->nPag
1f6a3 65 20 26 26 20 73 69 7a 65 6f 66 28 70 43 68 65  e && sizeof(pChe
1f6a4 63 6b 2d 3e 61 50 67 52 65 66 5b 30 5d 29 3d 3d  ck->aPgRef[0])==
1f6a5 31 20 29 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 61  1 );.  pCheck->a
1f6a6 50 67 52 65 66 5b 69 50 67 2f 38 5d 20 7c 3d 20  PgRef[iPg/8] |= 
1f6a7 28 31 20 3c 3c 20 28 69 50 67 20 26 20 30 78 30  (1 << (iPg & 0x0
1f6a8 37 29 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41  7));.}.../*.** A
1f6a9 64 64 20 31 20 74 6f 20 74 68 65 20 72 65 66 65  dd 1 to the refe
1f6aa 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20  rence count for 
1f6ab 70 61 67 65 20 69 50 61 67 65 2e 20 20 49 66 20  page iPage.  If 
1f6ac 74 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f  this is the seco
1f6ad 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20  nd.** reference 
1f6ae 74 6f 20 74 68 65 20 70 61 67 65 2c 20 61 64 64  to the page, add
1f6af 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1f6b0 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e 7a 45 72  e to pCheck->zEr
1f6b1 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  rMsg..** Return 
1f6b2 31 20 69 66 20 74 68 65 72 65 20 61 72 65 20 32  1 if there are 2
1f6b3 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 65 72 65   ore more refere
1f6b4 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65  nces to the page
1f6b5 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 69 66 20   and 0 if.** if 
1f6b6 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73  this is the firs
1f6b7 74 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  t reference to t
1f6b8 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41  he page..**.** A
1f6b9 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 74 20 74  lso check that t
1f6ba 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69  he page number i
1f6bb 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a 2f 0a  s in bounds..*/.
1f6bc 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b  static int check
1f6bd 52 65 66 28 49 6e 74 65 67 72 69 74 79 43 6b 20  Ref(IntegrityCk 
1f6be 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f 20 69 50  *pCheck, Pgno iP
1f6bf 61 67 65 2c 20 63 68 61 72 20 2a 7a 43 6f 6e 74  age, char *zCont
1f6c0 65 78 74 29 7b 0a 20 20 69 66 28 20 69 50 61 67  ext){.  if( iPag
1f6c1 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b  e==0 ) return 1;
1f6c2 0a 20 20 69 66 28 20 69 50 61 67 65 3e 70 43 68  .  if( iPage>pCh
1f6c3 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b 0a 20 20  eck->nPage ){.  
1f6c4 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
1f6c5 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78  (pCheck, zContex
1f6c6 74 2c 20 22 69 6e 76 61 6c 69 64 20 70 61 67 65  t, "invalid page
1f6c7 20 6e 75 6d 62 65 72 20 25 64 22 2c 20 69 50 61   number %d", iPa
1f6c8 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ge);.    return 
1f6c9 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 74  1;.  }.  if( get
1f6ca 50 61 67 65 52 65 66 65 72 65 6e 63 65 64 28 70  PageReferenced(p
1f6cb 43 68 65 63 6b 2c 20 69 50 61 67 65 29 20 29 7b  Check, iPage) ){
1f6cc 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64  .    checkAppend
1f6cd 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e  Msg(pCheck, zCon
1f6ce 74 65 78 74 2c 20 22 32 6e 64 20 72 65 66 65 72  text, "2nd refer
1f6cf 65 6e 63 65 20 74 6f 20 70 61 67 65 20 25 64 22  ence to page %d"
1f6d0 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 72 65  , iPage);.    re
1f6d1 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 73 65  turn 1;.  }.  se
1f6d2 74 50 61 67 65 52 65 66 65 72 65 6e 63 65 64 28  tPageReferenced(
1f6d3 70 43 68 65 63 6b 2c 20 69 50 61 67 65 29 3b 0a  pCheck, iPage);.
1f6d4 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23    return 0;.}..#
1f6d5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f6d6 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a  IT_AUTOVACUUM./*
1f6d7 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74  .** Check that t
1f6d8 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  he entry in the 
1f6d9 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20  pointer-map for 
1f6da 70 61 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73  page iChild maps
1f6db 20 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61   to .** page iPa
1f6dc 72 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79  rent, pointer ty
1f6dd 70 65 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e  pe ptrType. If n
1f6de 6f 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72  ot, append an er
1f6df 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74  ror message.** t
1f6e0 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61  o pCheck..*/.sta
1f6e1 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74  tic void checkPt
1f6e2 72 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74  rmap(.  Integrit
1f6e3 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f  yCk *pCheck,   /
1f6e4 2a 20 49 6e 74 65 67 72 69 74 79 20 63 68 65 63  * Integrity chec
1f6e5 6b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50  k context */.  P
1f6e6 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20  gno iChild,     
1f6e7 20 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70        /* Child p
1f6e8 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  age number */.  
1f6e9 75 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20  u8 eType,       
1f6ea 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74         /* Expect
1f6eb 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74  ed pointer map t
1f6ec 79 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50  ype */.  Pgno iP
1f6ed 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20  arent,          
1f6ee 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e  /* Expected poin
1f6ef 74 65 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70  ter map parent p
1f6f0 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  age number */.  
1f6f1 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20  char *zContext  
1f6f2 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78         /* Contex
1f6f3 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75  t description (u
1f6f4 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73  sed for error ms
1f6f5 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  g) */.){.  int r
1f6f6 63 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54  c;.  u8 ePtrmapT
1f6f7 79 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72  ype;.  Pgno iPtr
1f6f8 6d 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63  mapParent;..  rc
1f6f9 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 43 68   = ptrmapGet(pCh
1f6fa 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64  eck->pBt, iChild
1f6fb 2c 20 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20  , &ePtrmapType, 
1f6fc 26 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b  &iPtrmapParent);
1f6fd 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
1f6fe 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
1f6ff 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
1f700 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49   || rc==SQLITE_I
1f701 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68  OERR_NOMEM ) pCh
1f702 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  eck->mallocFaile
1f703 64 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b  d = 1;.    check
1f704 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
1f705 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69  , zContext, "Fai
1f706 6c 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d  led to read ptrm
1f707 61 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69  ap key=%d", iChi
1f708 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ld);.    return;
1f709 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72  .  }..  if( ePtr
1f70a 6d 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c  mapType!=eType |
1f70b 7c 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21  | iPtrmapParent!
1f70c 3d 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  =iParent ){.    
1f70d 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
1f70e 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
1f70f 20 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72   .      "Bad ptr
1f710 20 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25   map entry key=%
1f711 64 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25  d expected=(%d,%
1f712 64 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c  d) got=(%d,%d)",
1f713 20 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20   .      iChild, 
1f714 65 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20  eType, iParent, 
1f715 65 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74  ePtrmapType, iPt
1f716 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d  rmapParent);.  }
1f717 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
1f718 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67   Check the integ
1f719 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65  rity of the free
1f71a 6c 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76  list or of an ov
1f71b 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74  erflow page list
1f71c 2e 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74  ..** Verify that
1f71d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
1f71e 61 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74  ages on the list
1f71f 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63   is N..*/.static
1f720 20 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28   void checkList(
1f721 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a  .  IntegrityCk *
1f722 70 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65  pCheck,  /* Inte
1f723 67 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63  grity checking c
1f724 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
1f725 69 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20  isFreeList,     
1f726 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20    /* True for a 
1f727 66 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65  freelist.  False
1f728 20 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61   for overflow pa
1f729 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  ge list */.  int
1f72a 20 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20   iPage,         
1f72b 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
1f72c 72 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65  r for first page
1f72d 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a   in the list */.
1f72e 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20    int N,        
1f72f 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63          /* Expec
1f730 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  ted number of pa
1f731 67 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20  ges in the list 
1f732 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74  */.  char *zCont
1f733 65 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  ext        /* Co
1f734 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20  ntext for error 
1f735 6d 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20  messages */.){. 
1f736 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78   int i;.  int ex
1f737 70 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e  pected = N;.  in
1f738 74 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65  t iFirst = iPage
1f739 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e  ;.  while( N-- >
1f73a 20 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78   0 && pCheck->mx
1f73b 45 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67  Err ){.    DbPag
1f73c 65 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20  e *pOvflPage;.  
1f73d 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
1f73e 2a 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20  *pOvflData;.    
1f73f 69 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20  if( iPage<1 ){. 
1f740 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64       checkAppend
1f741 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e  Msg(pCheck, zCon
1f742 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22  text,.         "
1f743 25 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d  %d of %d pages m
1f744 69 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72  issing from over
1f745 66 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69  flow list starti
1f746 6e 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20  ng at %d",.     
1f747 20 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74       N+1, expect
1f748 65 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20  ed, iFirst);.   
1f749 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1f74a 20 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66      if( checkRef
1f74b 28 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20  (pCheck, iPage, 
1f74c 7a 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61  zContext) ) brea
1f74d 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  k;.    if( sqlit
1f74e 65 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63  e3PagerGet(pChec
1f74f 6b 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f  k->pPager, (Pgno
1f750 29 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61  )iPage, &pOvflPa
1f751 67 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65  ge) ){.      che
1f752 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
1f753 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66  ck, zContext, "f
1f754 61 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67  ailed to get pag
1f755 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20  e %d", iPage);. 
1f756 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1f757 7d 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20  }.    pOvflData 
1f758 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
1f759 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47   *)sqlite3PagerG
1f75a 65 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65  etData(pOvflPage
1f75b 29 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65  );.    if( isFre
1f75c 65 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69  eList ){.      i
1f75d 6e 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28  nt n = get4byte(
1f75e 26 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a  &pOvflData[4]);.
1f75f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1f760 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
1f761 20 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d       if( pCheck-
1f762 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d  >pBt->autoVacuum
1f763 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
1f764 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20  kPtrmap(pCheck, 
1f765 69 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52  iPage, PTRMAP_FR
1f766 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74  EEPAGE, 0, zCont
1f767 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  ext);.      }.#e
1f768 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e  ndif.      if( n
1f769 3e 28 69 6e 74 29 70 43 68 65 63 6b 2d 3e 70 42  >(int)pCheck->pB
1f76a 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d  t->usableSize/4-
1f76b 32 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65  2 ){.        che
1f76c 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
1f76d 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20  ck, zContext,.  
1f76e 20 20 20 20 20 20 20 20 20 22 66 72 65 65 6c 69           "freeli
1f76f 73 74 20 6c 65 61 66 20 63 6f 75 6e 74 20 74 6f  st leaf count to
1f770 6f 20 62 69 67 20 6f 6e 20 70 61 67 65 20 25 64  o big on page %d
1f771 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20  ", iPage);.     
1f772 20 20 20 4e 2d 2d 3b 0a 20 20 20 20 20 20 7d 65     N--;.      }e
1f773 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72  lse{.        for
1f774 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b  (i=0; i<n; i++){
1f775 0a 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20  .          Pgno 
1f776 69 46 72 65 65 50 61 67 65 20 3d 20 67 65 74 34  iFreePage = get4
1f777 62 79 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b  byte(&pOvflData[
1f778 38 2b 69 2a 34 5d 29 3b 0a 23 69 66 6e 64 65 66  8+i*4]);.#ifndef
1f779 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1f77a 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 20 20  OVACUUM.        
1f77b 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42    if( pCheck->pB
1f77c 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
1f77d 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 65  .            che
1f77e 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c  ckPtrmap(pCheck,
1f77f 20 69 46 72 65 65 50 61 67 65 2c 20 50 54 52 4d   iFreePage, PTRM
1f780 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20  AP_FREEPAGE, 0, 
1f781 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20  zContext);.     
1f782 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
1f783 20 20 20 20 20 20 20 20 63 68 65 63 6b 52 65 66          checkRef
1f784 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61  (pCheck, iFreePa
1f785 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20  ge, zContext);. 
1f786 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1f787 20 4e 20 2d 3d 20 6e 3b 0a 20 20 20 20 20 20 7d   N -= n;.      }
1f788 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
1f789 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
1f78a 41 43 55 55 4d 0a 20 20 20 20 65 6c 73 65 7b 0a  ACUUM.    else{.
1f78b 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73        /* If this
1f78c 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72   database suppor
1f78d 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61  ts auto-vacuum a
1f78e 6e 64 20 69 50 61 67 65 20 69 73 20 6e 6f 74 20  nd iPage is not 
1f78f 74 68 65 20 6c 61 73 74 0a 20 20 20 20 20 20 2a  the last.      *
1f790 2a 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 6f  * page in this o
1f791 76 65 72 66 6c 6f 77 20 6c 69 73 74 2c 20 63 68  verflow list, ch
1f792 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 6f 69  eck that the poi
1f793 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66  nter-map entry f
1f794 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  or.      ** the 
1f795 66 6f 6c 6c 6f 77 69 6e 67 20 70 61 67 65 20 6d  following page m
1f796 61 74 63 68 65 73 20 69 50 61 67 65 2e 0a 20 20  atches iPage..  
1f797 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
1f798 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75   pCheck->pBt->au
1f799 74 6f 56 61 63 75 75 6d 20 26 26 20 4e 3e 30 20  toVacuum && N>0 
1f79a 29 7b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 67  ){.        i = g
1f79b 65 74 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74  et4byte(pOvflDat
1f79c 61 29 3b 0a 20 20 20 20 20 20 20 20 63 68 65 63  a);.        chec
1f79d 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20  kPtrmap(pCheck, 
1f79e 69 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  i, PTRMAP_OVERFL
1f79f 4f 57 32 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e  OW2, iPage, zCon
1f7a0 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  text);.      }. 
1f7a1 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
1f7a2 69 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65  iPage = get4byte
1f7a3 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 20  (pOvflData);.   
1f7a4 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
1f7a5 65 66 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20  ef(pOvflPage);. 
1f7a6 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53   }.}.#endif /* S
1f7a7 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
1f7a8 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23  RITY_CHECK */..#
1f7a9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f7aa 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
1f7ab 43 4b 0a 2f 2a 0a 2a 2a 20 44 6f 20 76 61 72 69  CK./*.** Do vari
1f7ac 6f 75 73 20 73 61 6e 69 74 79 20 63 68 65 63 6b  ous sanity check
1f7ad 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61  s on a single pa
1f7ae 67 65 20 6f 66 20 61 20 74 72 65 65 2e 20 20 52  ge of a tree.  R
1f7af 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 74 72 65  eturn.** the tre
1f7b0 65 20 64 65 70 74 68 2e 20 20 52 6f 6f 74 20 70  e depth.  Root p
1f7b1 61 67 65 73 20 72 65 74 75 72 6e 20 30 2e 20 20  ages return 0.  
1f7b2 50 61 72 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20  Parents of root 
1f7b3 70 61 67 65 73 0a 2a 2a 20 72 65 74 75 72 6e 20  pages.** return 
1f7b4 31 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e  1, and so forth.
1f7b5 0a 2a 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63 68  .** .** These ch
1f7b6 65 63 6b 73 20 61 72 65 20 64 6f 6e 65 3a 0a 2a  ecks are done:.*
1f7b7 2a 0a 2a 2a 20 20 20 20 20 20 31 2e 20 20 4d 61  *.**      1.  Ma
1f7b8 6b 65 20 73 75 72 65 20 74 68 61 74 20 63 65 6c  ke sure that cel
1f7b9 6c 73 20 61 6e 64 20 66 72 65 65 62 6c 6f 63 6b  ls and freeblock
1f7ba 73 20 64 6f 20 6e 6f 74 20 6f 76 65 72 6c 61 70  s do not overlap
1f7bb 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 75 74  .**          but
1f7bc 20 63 6f 6d 62 69 6e 65 20 74 6f 20 63 6f 6d 70   combine to comp
1f7bd 6c 65 74 65 6c 79 20 63 6f 76 65 72 20 74 68 65  letely cover the
1f7be 20 70 61 67 65 2e 0a 2a 2a 20 20 4e 4f 20 20 32   page..**  NO  2
1f7bf 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 63 65 6c  .  Make sure cel
1f7c0 6c 20 6b 65 79 73 20 61 72 65 20 69 6e 20 6f 72  l keys are in or
1f7c1 64 65 72 2e 0a 2a 2a 20 20 4e 4f 20 20 33 2e 20  der..**  NO  3. 
1f7c2 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65   Make sure no ke
1f7c3 79 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f  y is less than o
1f7c4 72 20 65 71 75 61 6c 20 74 6f 20 7a 4c 6f 77 65  r equal to zLowe
1f7c5 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 4e 4f 20 20  rBound..**  NO  
1f7c6 34 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f  4.  Make sure no
1f7c7 20 6b 65 79 20 69 73 20 67 72 65 61 74 65 72 20   key is greater 
1f7c8 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
1f7c9 20 7a 55 70 70 65 72 42 6f 75 6e 64 2e 0a 2a 2a   zUpperBound..**
1f7ca 20 20 20 20 20 20 35 2e 20 20 43 68 65 63 6b 20        5.  Check 
1f7cb 74 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66  the integrity of
1f7cc 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
1f7cd 0a 2a 2a 20 20 20 20 20 20 36 2e 20 20 52 65 63  .**      6.  Rec
1f7ce 75 72 73 69 76 65 6c 79 20 63 61 6c 6c 20 63 68  ursively call ch
1f7cf 65 63 6b 54 72 65 65 50 61 67 65 20 6f 6e 20 61  eckTreePage on a
1f7d0 6c 6c 20 63 68 69 6c 64 72 65 6e 2e 0a 2a 2a 20  ll children..** 
1f7d1 20 20 20 20 20 37 2e 20 20 56 65 72 69 66 79 20       7.  Verify 
1f7d2 74 68 61 74 20 74 68 65 20 64 65 70 74 68 20 6f  that the depth o
1f7d3 66 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 69  f all children i
1f7d4 73 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 20 20  s the same..**  
1f7d5 20 20 20 20 38 2e 20 20 4d 61 6b 65 20 73 75 72      8.  Make sur
1f7d6 65 20 74 68 69 73 20 70 61 67 65 20 69 73 20 61  e this page is a
1f7d7 74 20 6c 65 61 73 74 20 33 33 25 20 66 75 6c 6c  t least 33% full
1f7d8 20 6f 72 20 65 6c 73 65 20 69 74 20 69 73 0a 2a   or else it is.*
1f7d9 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 72  *          the r
1f7da 6f 6f 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e  oot of the tree.
1f7db 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
1f7dc 68 65 63 6b 54 72 65 65 50 61 67 65 28 0a 20 20  heckTreePage(.  
1f7dd 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68  IntegrityCk *pCh
1f7de 65 63 6b 2c 20 20 2f 2a 20 43 6f 6e 74 65 78 74  eck,  /* Context
1f7df 20 66 6f 72 20 74 68 65 20 73 61 6e 69 74 79 20   for the sanity 
1f7e0 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69  check */.  int i
1f7e1 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20  Page,           
1f7e2 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
1f7e3 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 63  of the page to c
1f7e4 68 65 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a  heck */.  char *
1f7e5 7a 50 61 72 65 6e 74 43 6f 6e 74 65 78 74 2c 20  zParentContext, 
1f7e6 2f 2a 20 50 61 72 65 6e 74 20 63 6f 6e 74 65 78  /* Parent contex
1f7e7 74 20 2a 2f 0a 20 20 69 36 34 20 2a 70 6e 50 61  t */.  i64 *pnPa
1f7e8 72 65 6e 74 4d 69 6e 4b 65 79 2c 20 0a 20 20 69  rentMinKey, .  i
1f7e9 36 34 20 2a 70 6e 50 61 72 65 6e 74 4d 61 78 4b  64 *pnParentMaxK
1f7ea 65 79 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20  ey.){.  MemPage 
1f7eb 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 69 2c  *pPage;.  int i,
1f7ec 20 72 63 2c 20 64 65 70 74 68 2c 20 64 32 2c 20   rc, depth, d2, 
1f7ed 70 67 6e 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e 74  pgno, cnt;.  int
1f7ee 20 68 64 72 2c 20 63 65 6c 6c 53 74 61 72 74 3b   hdr, cellStart;
1f7ef 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20  .  int nCell;.  
1f7f0 75 38 20 2a 64 61 74 61 3b 0a 20 20 42 74 53 68  u8 *data;.  BtSh
1f7f1 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 6e 74  ared *pBt;.  int
1f7f2 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63   usableSize;.  c
1f7f3 68 61 72 20 7a 43 6f 6e 74 65 78 74 5b 31 30 30  har zContext[100
1f7f4 5d 3b 0a 20 20 63 68 61 72 20 2a 68 69 74 20 3d  ];.  char *hit =
1f7f5 20 30 3b 0a 20 20 69 36 34 20 6e 4d 69 6e 4b 65   0;.  i64 nMinKe
1f7f6 79 20 3d 20 30 3b 0a 20 20 69 36 34 20 6e 4d 61  y = 0;.  i64 nMa
1f7f7 78 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 73 71 6c  xKey = 0;..  sql
1f7f8 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
1f7f9 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20  zeof(zContext), 
1f7fa 7a 43 6f 6e 74 65 78 74 2c 20 22 50 61 67 65 20  zContext, "Page 
1f7fb 25 64 3a 20 22 2c 20 69 50 61 67 65 29 3b 0a 0a  %d: ", iPage);..
1f7fc 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20    /* Check that 
1f7fd 74 68 65 20 70 61 67 65 20 65 78 69 73 74 73 0a  the page exists.
1f7fe 20 20 2a 2f 0a 20 20 70 42 74 20 3d 20 70 43 68    */.  pBt = pCh
1f7ff 65 63 6b 2d 3e 70 42 74 3b 0a 20 20 75 73 61 62  eck->pBt;.  usab
1f800 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73  leSize = pBt->us
1f801 61 62 6c 65 53 69 7a 65 3b 0a 20 20 69 66 28 20  ableSize;.  if( 
1f802 69 50 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72  iPage==0 ) retur
1f803 6e 20 30 3b 0a 20 20 69 66 28 20 63 68 65 63 6b  n 0;.  if( check
1f804 52 65 66 28 70 43 68 65 63 6b 2c 20 69 50 61 67  Ref(pCheck, iPag
1f805 65 2c 20 7a 50 61 72 65 6e 74 43 6f 6e 74 65 78  e, zParentContex
1f806 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  t) ) return 0;. 
1f807 20 69 66 28 20 28 72 63 20 3d 20 62 74 72 65 65   if( (rc = btree
1f808 47 65 74 50 61 67 65 28 70 42 74 2c 20 28 50 67  GetPage(pBt, (Pg
1f809 6e 6f 29 69 50 61 67 65 2c 20 26 70 50 61 67 65  no)iPage, &pPage
1f80a 2c 20 30 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  , 0))!=0 ){.    
1f80b 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
1f80c 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
1f80d 0a 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20  .       "unable 
1f80e 74 6f 20 67 65 74 20 74 68 65 20 70 61 67 65 2e  to get the page.
1f80f 20 65 72 72 6f 72 20 63 6f 64 65 3d 25 64 22 2c   error code=%d",
1f810 20 72 63 29 3b 0a 20 20 20 20 72 65 74 75 72 6e   rc);.    return
1f811 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c   0;.  }..  /* Cl
1f812 65 61 72 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e  ear MemPage.isIn
1f813 69 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  it to make sure 
1f814 74 68 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 64  the corruption d
1f815 65 74 65 63 74 69 6f 6e 20 63 6f 64 65 20 69 6e  etection code in
1f816 0a 20 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50  .  ** btreeInitP
1f817 61 67 65 28 29 20 69 73 20 65 78 65 63 75 74 65  age() is execute
1f818 64 2e 20 20 2a 2f 0a 20 20 70 50 61 67 65 2d 3e  d.  */.  pPage->
1f819 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 69 66  isInit = 0;.  if
1f81a 28 20 28 72 63 20 3d 20 62 74 72 65 65 49 6e 69  ( (rc = btreeIni
1f81b 74 50 61 67 65 28 70 50 61 67 65 29 29 21 3d 30  tPage(pPage))!=0
1f81c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1f81d 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52 55  rc==SQLITE_CORRU
1f81e 50 54 20 29 3b 20 20 2f 2a 20 54 68 65 20 6f 6e  PT );  /* The on
1f81f 6c 79 20 70 6f 73 73 69 62 6c 65 20 65 72 72 6f  ly possible erro
1f820 72 20 66 72 6f 6d 20 49 6e 69 74 50 61 67 65 20  r from InitPage 
1f821 2a 2f 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65  */.    checkAppe
1f822 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
1f823 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20  ontext, .       
1f824 20 20 20 20 20 20 20 20 20 20 20 20 22 62 74 72              "btr
1f825 65 65 49 6e 69 74 50 61 67 65 28 29 20 72 65 74  eeInitPage() ret
1f826 75 72 6e 73 20 65 72 72 6f 72 20 63 6f 64 65 20  urns error code 
1f827 25 64 22 2c 20 72 63 29 3b 0a 20 20 20 20 72 65  %d", rc);.    re
1f828 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
1f829 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
1f82a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
1f82b 6f 75 74 20 61 6c 6c 20 74 68 65 20 63 65 6c 6c  out all the cell
1f82c 73 2e 0a 20 20 2a 2f 0a 20 20 64 65 70 74 68 20  s..  */.  depth 
1f82d 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  = 0;.  for(i=0; 
1f82e 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 26  i<pPage->nCell &
1f82f 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 3b  & pCheck->mxErr;
1f830 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70   i++){.    u8 *p
1f831 43 65 6c 6c 3b 0a 20 20 20 20 75 33 32 20 73 7a  Cell;.    u32 sz
1f832 3b 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69  ;.    CellInfo i
1f833 6e 66 6f 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65  nfo;..    /* Che
1f834 63 6b 20 70 61 79 6c 6f 61 64 20 6f 76 65 72 66  ck payload overf
1f835 6c 6f 77 20 70 61 67 65 73 0a 20 20 20 20 2a 2f  low pages.    */
1f836 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
1f837 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f  rintf(sizeof(zCo
1f838 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74  ntext), zContext
1f839 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 22  ,.             "
1f83a 4f 6e 20 74 72 65 65 20 70 61 67 65 20 25 64 20  On tree page %d 
1f83b 63 65 6c 6c 20 25 64 3a 20 22 2c 20 69 50 61 67  cell %d: ", iPag
1f83c 65 2c 20 69 29 3b 0a 20 20 20 20 70 43 65 6c 6c  e, i);.    pCell
1f83d 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67   = findCell(pPag
1f83e 65 2c 69 29 3b 0a 20 20 20 20 62 74 72 65 65 50  e,i);.    btreeP
1f83f 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67  arseCellPtr(pPag
1f840 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29  e, pCell, &info)
1f841 3b 0a 20 20 20 20 73 7a 20 3d 20 69 6e 66 6f 2e  ;.    sz = info.
1f842 6e 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 21  nData;.    if( !
1f843 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 20  pPage->intKey ) 
1f844 73 7a 20 2b 3d 20 28 69 6e 74 29 69 6e 66 6f 2e  sz += (int)info.
1f845 6e 4b 65 79 3b 0a 20 20 20 20 2f 2a 20 46 6f 72  nKey;.    /* For
1f846 20 69 6e 74 4b 65 79 20 70 61 67 65 73 2c 20 63   intKey pages, c
1f847 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 6b 65  heck that the ke
1f848 79 73 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e  ys are in order.
1f849 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65 6c 73 65  .    */.    else
1f84a 20 69 66 28 20 69 3d 3d 30 20 29 20 6e 4d 69 6e   if( i==0 ) nMin
1f84b 4b 65 79 20 3d 20 6e 4d 61 78 4b 65 79 20 3d 20  Key = nMaxKey = 
1f84c 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 65  info.nKey;.    e
1f84d 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 69  lse{.      if( i
1f84e 6e 66 6f 2e 6e 4b 65 79 20 3c 3d 20 6e 4d 61 78  nfo.nKey <= nMax
1f84f 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 63  Key ){.        c
1f850 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
1f851 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  heck, zContext, 
1f852 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 52 6f  .            "Ro
1f853 77 69 64 20 25 6c 6c 64 20 6f 75 74 20 6f 66 20  wid %lld out of 
1f854 6f 72 64 65 72 20 28 70 72 65 76 69 6f 75 73 20  order (previous 
1f855 77 61 73 20 25 6c 6c 64 29 22 2c 20 69 6e 66 6f  was %lld)", info
1f856 2e 6e 4b 65 79 2c 20 6e 4d 61 78 4b 65 79 29 3b  .nKey, nMaxKey);
1f857 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e  .      }.      n
1f858 4d 61 78 4b 65 79 20 3d 20 69 6e 66 6f 2e 6e 4b  MaxKey = info.nK
1f859 65 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  ey;.    }.    as
1f85a 73 65 72 74 28 20 73 7a 3d 3d 69 6e 66 6f 2e 6e  sert( sz==info.n
1f85b 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 20 20 69  Payload );.    i
1f85c 66 28 20 28 73 7a 3e 69 6e 66 6f 2e 6e 4c 6f 63  f( (sz>info.nLoc
1f85d 61 6c 29 20 0a 20 20 20 20 20 26 26 20 28 26 70  al) .     && (&p
1f85e 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66  Cell[info.iOverf
1f85f 6c 6f 77 5d 3c 3d 26 70 50 61 67 65 2d 3e 61 44  low]<=&pPage->aD
1f860 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65 53  ata[pBt->usableS
1f861 69 7a 65 5d 29 0a 20 20 20 20 29 7b 0a 20 20 20  ize]).    ){.   
1f862 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 28     int nPage = (
1f863 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c  sz - info.nLocal
1f864 20 2b 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20   + usableSize - 
1f865 35 29 2f 28 75 73 61 62 6c 65 53 69 7a 65 20 2d  5)/(usableSize -
1f866 20 34 29 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20   4);.      Pgno 
1f867 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62  pgnoOvfl = get4b
1f868 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e  yte(&pCell[info.
1f869 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 23 69 66  iOverflow]);.#if
1f86a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1f86b 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
1f86c 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
1f86d 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20  acuum ){.       
1f86e 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68   checkPtrmap(pCh
1f86f 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 50  eck, pgnoOvfl, P
1f870 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c  TRMAP_OVERFLOW1,
1f871 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74   iPage, zContext
1f872 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
1f873 66 0a 20 20 20 20 20 20 63 68 65 63 6b 4c 69 73  f.      checkLis
1f874 74 28 70 43 68 65 63 6b 2c 20 30 2c 20 70 67 6e  t(pCheck, 0, pgn
1f875 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 2c 20 7a 43  oOvfl, nPage, zC
1f876 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 0a 0a  ontext);.    }..
1f877 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 73 61 6e      /* Check san
1f878 69 74 79 20 6f 66 20 6c 65 66 74 20 63 68 69 6c  ity of left chil
1f879 64 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20  d page..    */. 
1f87a 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c     if( !pPage->l
1f87b 65 61 66 20 29 7b 0a 20 20 20 20 20 20 70 67 6e  eaf ){.      pgn
1f87c 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65  o = get4byte(pCe
1f87d 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ll);.#ifndef SQL
1f87e 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
1f87f 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 42  UUM.      if( pB
1f880 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
1f881 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74  .        checkPt
1f882 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e  rmap(pCheck, pgn
1f883 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c  o, PTRMAP_BTREE,
1f884 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74   iPage, zContext
1f885 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
1f886 66 0a 20 20 20 20 20 20 64 32 20 3d 20 63 68 65  f.      d2 = che
1f887 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 65 63  ckTreePage(pChec
1f888 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65 78  k, pgno, zContex
1f889 74 2c 20 26 6e 4d 69 6e 4b 65 79 2c 20 69 3d 3d  t, &nMinKey, i==
1f88a 30 20 3f 20 4e 55 4c 4c 20 3a 20 26 6e 4d 61 78  0 ? NULL : &nMax
1f88b 4b 65 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20  Key);.      if( 
1f88c 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74 68  i>0 && d2!=depth
1f88d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
1f88e 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
1f88f 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43 68  k, zContext, "Ch
1f890 69 6c 64 20 70 61 67 65 20 64 65 70 74 68 20 64  ild page depth d
1f891 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20 20  iffers");.      
1f892 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d 20  }.      depth = 
1f893 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  d2;.    }.  }.. 
1f894 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
1f895 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20  f ){.    pgno = 
1f896 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
1f897 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  >aData[pPage->hd
1f898 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20  rOffset+8]);.   
1f899 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1f89a 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78  f(sizeof(zContex
1f89b 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20  t), zContext, . 
1f89c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f89d 20 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20      "On page %d 
1f89e 61 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20  at right child: 
1f89f 22 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64  ", iPage);.#ifnd
1f8a0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1f8a1 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66  UTOVACUUM.    if
1f8a2 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
1f8a3 6d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b  m ){.      check
1f8a4 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70  Ptrmap(pCheck, p
1f8a5 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  gno, PTRMAP_BTRE
1f8a6 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65  E, iPage, zConte
1f8a7 78 74 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  xt);.    }.#endi
1f8a8 66 0a 20 20 20 20 63 68 65 63 6b 54 72 65 65 50  f.    checkTreeP
1f8a9 61 67 65 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f  age(pCheck, pgno
1f8aa 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 4e 55 4c 4c  , zContext, NULL
1f8ab 2c 20 21 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  , !pPage->nCell 
1f8ac 3f 20 4e 55 4c 4c 20 3a 20 26 6e 4d 61 78 4b 65  ? NULL : &nMaxKe
1f8ad 79 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 46  y);.  }. .  /* F
1f8ae 6f 72 20 69 6e 74 4b 65 79 20 6c 65 61 66 20 70  or intKey leaf p
1f8af 61 67 65 73 2c 20 63 68 65 63 6b 20 74 68 61 74  ages, check that
1f8b0 20 74 68 65 20 6d 69 6e 2f 6d 61 78 20 6b 65 79   the min/max key
1f8b1 73 20 61 72 65 20 69 6e 20 6f 72 64 65 72 0a 20  s are in order. 
1f8b2 20 2a 2a 20 77 69 74 68 20 61 6e 79 20 6c 65 66   ** with any lef
1f8b3 74 2f 70 61 72 65 6e 74 2f 72 69 67 68 74 20 70  t/parent/right p
1f8b4 61 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ages..  */.  if(
1f8b5 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 26 26 20   pPage->leaf && 
1f8b6 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b  pPage->intKey ){
1f8b7 0a 20 20 20 20 2f 2a 20 69 66 20 77 65 20 61 72  .    /* if we ar
1f8b8 65 20 61 20 6c 65 66 74 20 63 68 69 6c 64 20 70  e a left child p
1f8b9 61 67 65 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  age */.    if( p
1f8ba 6e 50 61 72 65 6e 74 4d 69 6e 4b 65 79 20 29 7b  nParentMinKey ){
1f8bb 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20  .      /* if we 
1f8bc 61 72 65 20 74 68 65 20 6c 65 66 74 20 6d 6f 73  are the left mos
1f8bd 74 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a  t child page */.
1f8be 20 20 20 20 20 20 69 66 28 20 21 70 6e 50 61 72        if( !pnPar
1f8bf 65 6e 74 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20  entMaxKey ){.   
1f8c0 20 20 20 20 20 69 66 28 20 6e 4d 61 78 4b 65 79       if( nMaxKey
1f8c1 20 3e 20 2a 70 6e 50 61 72 65 6e 74 4d 69 6e 4b   > *pnParentMinK
1f8c2 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ey ){.          
1f8c3 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
1f8c4 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
1f8c5 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
1f8c6 22 52 6f 77 69 64 20 25 6c 6c 64 20 6f 75 74 20  "Rowid %lld out 
1f8c7 6f 66 20 6f 72 64 65 72 20 28 6d 61 78 20 6c 61  of order (max la
1f8c8 72 67 65 72 20 74 68 61 6e 20 70 61 72 65 6e 74  rger than parent
1f8c9 20 6d 69 6e 20 6f 66 20 25 6c 6c 64 29 22 2c 0a   min of %lld)",.
1f8ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 4d                nM
1f8cb 61 78 4b 65 79 2c 20 2a 70 6e 50 61 72 65 6e 74  axKey, *pnParent
1f8cc 4d 69 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20  MinKey);.       
1f8cd 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
1f8ce 20 20 20 20 20 20 20 20 69 66 28 20 6e 4d 69 6e          if( nMin
1f8cf 4b 65 79 20 3c 3d 20 2a 70 6e 50 61 72 65 6e 74  Key <= *pnParent
1f8d0 4d 69 6e 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  MinKey ){.      
1f8d1 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
1f8d2 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74  sg(pCheck, zCont
1f8d3 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ext, .          
1f8d4 20 20 20 20 22 52 6f 77 69 64 20 25 6c 6c 64 20      "Rowid %lld 
1f8d5 6f 75 74 20 6f 66 20 6f 72 64 65 72 20 28 6d 69  out of order (mi
1f8d6 6e 20 6c 65 73 73 20 74 68 61 6e 20 70 61 72 65  n less than pare
1f8d7 6e 74 20 6d 69 6e 20 6f 66 20 25 6c 6c 64 29 22  nt min of %lld)"
1f8d8 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1f8d9 6e 4d 69 6e 4b 65 79 2c 20 2a 70 6e 50 61 72 65  nMinKey, *pnPare
1f8da 6e 74 4d 69 6e 4b 65 79 29 3b 0a 20 20 20 20 20  ntMinKey);.     
1f8db 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
1f8dc 20 6e 4d 61 78 4b 65 79 20 3e 20 2a 70 6e 50 61   nMaxKey > *pnPa
1f8dd 72 65 6e 74 4d 61 78 4b 65 79 20 29 7b 0a 20 20  rentMaxKey ){.  
1f8de 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70          checkApp
1f8df 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
1f8e0 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20  Context, .      
1f8e1 20 20 20 20 20 20 20 20 22 52 6f 77 69 64 20 25          "Rowid %
1f8e2 6c 6c 64 20 6f 75 74 20 6f 66 20 6f 72 64 65 72  lld out of order
1f8e3 20 28 6d 61 78 20 6c 61 72 67 65 72 20 74 68 61   (max larger tha
1f8e4 6e 20 70 61 72 65 6e 74 20 6d 61 78 20 6f 66 20  n parent max of 
1f8e5 25 6c 6c 64 29 22 2c 0a 20 20 20 20 20 20 20 20  %lld)",.        
1f8e6 20 20 20 20 20 20 6e 4d 61 78 4b 65 79 2c 20 2a        nMaxKey, *
1f8e7 70 6e 50 61 72 65 6e 74 4d 61 78 4b 65 79 29 3b  pnParentMaxKey);
1f8e8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1f8e9 20 20 20 2a 70 6e 50 61 72 65 6e 74 4d 69 6e 4b     *pnParentMinK
1f8ea 65 79 20 3d 20 6e 4d 61 78 4b 65 79 3b 0a 20 20  ey = nMaxKey;.  
1f8eb 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 65 6c 73      }.    /* els
1f8ec 65 20 69 66 20 77 65 27 72 65 20 61 20 72 69 67  e if we're a rig
1f8ed 68 74 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f  ht child page */
1f8ee 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28 20  .    } else if( 
1f8ef 70 6e 50 61 72 65 6e 74 4d 61 78 4b 65 79 20 29  pnParentMaxKey )
1f8f0 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 4d 69 6e  {.      if( nMin
1f8f1 4b 65 79 20 3c 3d 20 2a 70 6e 50 61 72 65 6e 74  Key <= *pnParent
1f8f2 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  MaxKey ){.      
1f8f3 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
1f8f4 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78  (pCheck, zContex
1f8f5 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  t, .            
1f8f6 22 52 6f 77 69 64 20 25 6c 6c 64 20 6f 75 74 20  "Rowid %lld out 
1f8f7 6f 66 20 6f 72 64 65 72 20 28 6d 69 6e 20 6c 65  of order (min le
1f8f8 73 73 20 74 68 61 6e 20 70 61 72 65 6e 74 20 6d  ss than parent m
1f8f9 61 78 20 6f 66 20 25 6c 6c 64 29 22 2c 0a 20 20  ax of %lld)",.  
1f8fa 20 20 20 20 20 20 20 20 20 20 6e 4d 69 6e 4b 65            nMinKe
1f8fb 79 2c 20 2a 70 6e 50 61 72 65 6e 74 4d 61 78 4b  y, *pnParentMaxK
1f8fc 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ey);.      }.   
1f8fd 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65   }.  }..  /* Che
1f8fe 63 6b 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 20  ck for complete 
1f8ff 63 6f 76 65 72 61 67 65 20 6f 66 20 74 68 65 20  coverage of the 
1f900 70 61 67 65 0a 20 20 2a 2f 0a 20 20 64 61 74 61  page.  */.  data
1f901 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b   = pPage->aData;
1f902 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e  .  hdr = pPage->
1f903 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 68 69 74  hdrOffset;.  hit
1f904 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61   = sqlite3PageMa
1f905 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53  lloc( pBt->pageS
1f906 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 68 69 74  ize );.  if( hit
1f907 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43 68 65 63  ==0 ){.    pChec
1f908 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  k->mallocFailed 
1f909 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 1;.  }else{.  
1f90a 20 20 69 6e 74 20 63 6f 6e 74 65 6e 74 4f 66 66    int contentOff
1f90b 73 65 74 20 3d 20 67 65 74 32 62 79 74 65 4e 6f  set = get2byteNo
1f90c 74 5a 65 72 6f 28 26 64 61 74 61 5b 68 64 72 2b  tZero(&data[hdr+
1f90d 35 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  5]);.    assert(
1f90e 20 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 3c 3d   contentOffset<=
1f90f 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 20 20 2f  usableSize );  /
1f910 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74  * Enforced by bt
1f911 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f  reeInitPage() */
1f912 0a 20 20 20 20 6d 65 6d 73 65 74 28 68 69 74 2b  .    memset(hit+
1f913 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 2c 20 30  contentOffset, 0
1f914 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d 63 6f 6e  , usableSize-con
1f915 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20  tentOffset);.   
1f916 20 6d 65 6d 73 65 74 28 68 69 74 2c 20 31 2c 20   memset(hit, 1, 
1f917 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a  contentOffset);.
1f918 20 20 20 20 6e 43 65 6c 6c 20 3d 20 67 65 74 32      nCell = get2
1f919 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33  byte(&data[hdr+3
1f91a 5d 29 3b 0a 20 20 20 20 63 65 6c 6c 53 74 61 72  ]);.    cellStar
1f91b 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34  t = hdr + 12 - 4
1f91c 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20  *pPage->leaf;.  
1f91d 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65    for(i=0; i<nCe
1f91e 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ll; i++){.      
1f91f 69 6e 74 20 70 63 20 3d 20 67 65 74 32 62 79 74  int pc = get2byt
1f920 65 28 26 64 61 74 61 5b 63 65 6c 6c 53 74 61 72  e(&data[cellStar
1f921 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 75  t+i*2]);.      u
1f922 33 32 20 73 69 7a 65 20 3d 20 36 35 35 33 36 3b  32 size = 65536;
1f923 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20  .      int j;.  
1f924 20 20 20 20 69 66 28 20 70 63 3c 3d 75 73 61 62      if( pc<=usab
1f925 6c 65 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 20  leSize-4 ){.    
1f926 20 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53      size = cellS
1f927 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 64  izePtr(pPage, &d
1f928 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20  ata[pc]);.      
1f929 7d 0a 20 20 20 20 20 20 69 66 28 20 28 69 6e 74  }.      if( (int
1f92a 29 28 70 63 2b 73 69 7a 65 2d 31 29 3e 3d 75 73  )(pc+size-1)>=us
1f92b 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20  ableSize ){.    
1f92c 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
1f92d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20  sg(pCheck, 0, . 
1f92e 20 20 20 20 20 20 20 20 20 20 20 22 43 6f 72 72             "Corr
1f92f 75 70 74 69 6f 6e 20 64 65 74 65 63 74 65 64 20  uption detected 
1f930 69 6e 20 63 65 6c 6c 20 25 64 20 6f 6e 20 70 61  in cell %d on pa
1f931 67 65 20 25 64 22 2c 69 2c 69 50 61 67 65 29 3b  ge %d",i,iPage);
1f932 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1f933 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 63 2b 73        for(j=pc+s
1f934 69 7a 65 2d 31 3b 20 6a 3e 3d 70 63 3b 20 6a 2d  ize-1; j>=pc; j-
1f935 2d 29 20 68 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20  -) hit[j]++;.   
1f936 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
1f937 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
1f938 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 77  a[hdr+1]);.    w
1f939 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20 20  hile( i>0 ){.   
1f93a 20 20 20 69 6e 74 20 73 69 7a 65 2c 20 6a 3b 0a     int size, j;.
1f93b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3c        assert( i<
1f93c 3d 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b  =usableSize-4 );
1f93d 20 20 20 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64       /* Enforced
1f93e 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61 67   by btreeInitPag
1f93f 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a  e() */.      siz
1f940 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  e = get2byte(&da
1f941 74 61 5b 69 2b 32 5d 29 3b 0a 20 20 20 20 20 20  ta[i+2]);.      
1f942 61 73 73 65 72 74 28 20 69 2b 73 69 7a 65 3c 3d  assert( i+size<=
1f943 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 20 20 2f  usableSize );  /
1f944 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74  * Enforced by bt
1f945 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f  reeInitPage() */
1f946 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 69 2b 73  .      for(j=i+s
1f947 69 7a 65 2d 31 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d  ize-1; j>=i; j--
1f948 29 20 68 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20  ) hit[j]++;.    
1f949 20 20 6a 20 3d 20 67 65 74 32 62 79 74 65 28 26    j = get2byte(&
1f94a 64 61 74 61 5b 69 5d 29 3b 0a 20 20 20 20 20 20  data[i]);.      
1f94b 61 73 73 65 72 74 28 20 6a 3d 3d 30 20 7c 7c 20  assert( j==0 || 
1f94c 6a 3e 69 2b 73 69 7a 65 20 29 3b 20 20 2f 2a 20  j>i+size );  /* 
1f94d 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65  Enforced by btre
1f94e 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20  eInitPage() */. 
1f94f 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 3d       assert( j<=
1f950 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 20  usableSize-4 ); 
1f951 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79    /* Enforced by
1f952 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29   btreeInitPage()
1f953 20 2a 2f 0a 20 20 20 20 20 20 69 20 3d 20 6a 3b   */.      i = j;
1f954 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
1f955 3d 63 6e 74 3d 30 3b 20 69 3c 75 73 61 62 6c 65  =cnt=0; i<usable
1f956 53 69 7a 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Size; i++){.    
1f957 20 20 69 66 28 20 68 69 74 5b 69 5d 3d 3d 30 20    if( hit[i]==0 
1f958 29 7b 0a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b  ){.        cnt++
1f959 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
1f95a 28 20 68 69 74 5b 69 5d 3e 31 20 29 7b 0a 20 20  ( hit[i]>1 ){.  
1f95b 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e        checkAppen
1f95c 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 0a  dMsg(pCheck, 0,.
1f95d 20 20 20 20 20 20 20 20 20 20 22 4d 75 6c 74 69            "Multi
1f95e 70 6c 65 20 75 73 65 73 20 66 6f 72 20 62 79 74  ple uses for byt
1f95f 65 20 25 64 20 6f 66 20 70 61 67 65 20 25 64 22  e %d of page %d"
1f960 2c 20 69 2c 20 69 50 61 67 65 29 3b 0a 20 20 20  , i, iPage);.   
1f961 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1f962 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
1f963 28 20 63 6e 74 21 3d 64 61 74 61 5b 68 64 72 2b  ( cnt!=data[hdr+
1f964 37 5d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63  7] ){.      chec
1f965 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
1f966 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20  k, 0, .         
1f967 20 22 46 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20   "Fragmentation 
1f968 6f 66 20 25 64 20 62 79 74 65 73 20 72 65 70 6f  of %d bytes repo
1f969 72 74 65 64 20 61 73 20 25 64 20 6f 6e 20 70 61  rted as %d on pa
1f96a 67 65 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20  ge %d",.        
1f96b 20 20 63 6e 74 2c 20 64 61 74 61 5b 68 64 72 2b    cnt, data[hdr+
1f96c 37 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20  7], iPage);.    
1f96d 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 50  }.  }.  sqlite3P
1f96e 61 67 65 46 72 65 65 28 68 69 74 29 3b 0a 20 20  ageFree(hit);.  
1f96f 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
1f970 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 64 65 70  e);.  return dep
1f971 74 68 2b 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  th+1;.}.#endif /
1f972 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  * SQLITE_OMIT_IN
1f973 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f  TEGRITY_CHECK */
1f974 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1f975 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
1f976 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73  CHECK./*.** This
1f977 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 61 20   routine does a 
1f978 63 6f 6d 70 6c 65 74 65 20 63 68 65 63 6b 20 6f  complete check o
1f979 66 20 74 68 65 20 67 69 76 65 6e 20 42 54 72 65  f the given BTre
1f97a 65 20 66 69 6c 65 2e 20 20 61 52 6f 6f 74 5b 5d  e file.  aRoot[]
1f97b 20 69 73 0a 2a 2a 20 61 6e 20 61 72 72 61 79 20   is.** an array 
1f97c 6f 66 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73  of pages numbers
1f97d 20 77 65 72 65 20 65 61 63 68 20 70 61 67 65 20   were each page 
1f97e 6e 75 6d 62 65 72 20 69 73 20 74 68 65 20 72 6f  number is the ro
1f97f 6f 74 20 70 61 67 65 20 6f 66 0a 2a 2a 20 61 20  ot page of.** a 
1f980 74 61 62 6c 65 2e 20 20 6e 52 6f 6f 74 20 69 73  table.  nRoot is
1f981 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
1f982 6e 74 72 69 65 73 20 69 6e 20 61 52 6f 6f 74 2e  ntries in aRoot.
1f983 0a 2a 2a 0a 2a 2a 20 41 20 72 65 61 64 2d 6f 6e  .**.** A read-on
1f984 6c 79 20 6f 72 20 72 65 61 64 2d 77 72 69 74 65  ly or read-write
1f985 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73   transaction mus
1f986 74 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66 6f  t be opened befo
1f987 72 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68  re calling.** th
1f988 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  is function..**.
1f989 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d  ** Write the num
1f98a 62 65 72 20 6f 66 20 65 72 72 6f 72 20 73 65 65  ber of error see
1f98b 6e 20 69 6e 20 2a 70 6e 45 72 72 2e 20 20 45 78  n in *pnErr.  Ex
1f98c 63 65 70 74 20 66 6f 72 20 73 6f 6d 65 20 6d 65  cept for some me
1f98d 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69  mory.** allocati
1f98e 6f 6e 20 65 72 72 6f 72 73 2c 20 20 61 6e 20 65  on errors,  an e
1f98f 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 6c  rror message hel
1f990 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61  d in memory obta
1f991 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c  ined from.** mal
1f992 6c 6f 63 20 69 73 20 72 65 74 75 72 6e 65 64 20  loc is returned 
1f993 69 66 20 2a 70 6e 45 72 72 20 69 73 20 6e 6f 6e  if *pnErr is non
1f994 2d 7a 65 72 6f 2e 20 20 49 66 20 2a 70 6e 45 72  -zero.  If *pnEr
1f995 72 3d 3d 30 20 74 68 65 6e 20 4e 55 4c 4c 20 69  r==0 then NULL i
1f996 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 20  s.** returned.  
1f997 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  If a memory allo
1f998 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63  cation error occ
1f999 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74  urs, NULL is ret
1f99a 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
1f99b 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
1f99c 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 67  qlite3BtreeInteg
1f99d 72 69 74 79 43 68 65 63 6b 28 0a 20 20 42 74 72  rityCheck(.  Btr
1f99e 65 65 20 2a 70 2c 20 20 20 20 20 2f 2a 20 54 68  ee *p,     /* Th
1f99f 65 20 62 74 72 65 65 20 74 6f 20 62 65 20 63 68  e btree to be ch
1f9a0 65 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a  ecked */.  int *
1f9a1 61 52 6f 6f 74 2c 20 20 20 2f 2a 20 41 6e 20 61  aRoot,   /* An a
1f9a2 72 72 61 79 20 6f 66 20 72 6f 6f 74 20 70 61 67  rray of root pag
1f9a3 65 73 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 69  es numbers for i
1f9a4 6e 64 69 76 69 64 75 61 6c 20 74 72 65 65 73 20  ndividual trees 
1f9a5 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 2c 20  */.  int nRoot, 
1f9a6 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1f9a7 65 6e 74 72 69 65 73 20 69 6e 20 61 52 6f 6f 74  entries in aRoot
1f9a8 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72  [] */.  int mxEr
1f9a9 72 2c 20 20 20 20 2f 2a 20 53 74 6f 70 20 72 65  r,    /* Stop re
1f9aa 70 6f 72 74 69 6e 67 20 65 72 72 6f 72 73 20 61  porting errors a
1f9ab 66 74 65 72 20 74 68 69 73 20 6d 61 6e 79 20 2a  fter this many *
1f9ac 2f 0a 20 20 69 6e 74 20 2a 70 6e 45 72 72 20 20  /.  int *pnErr  
1f9ad 20 20 2f 2a 20 57 72 69 74 65 20 6e 75 6d 62 65    /* Write numbe
1f9ae 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65 6e  r of errors seen
1f9af 20 74 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c   to this variabl
1f9b0 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 6e 6f 20 69  e */.){.  Pgno i
1f9b1 3b 0a 20 20 69 6e 74 20 6e 52 65 66 3b 0a 20 20  ;.  int nRef;.  
1f9b2 49 6e 74 65 67 72 69 74 79 43 6b 20 73 43 68 65  IntegrityCk sChe
1f9b3 63 6b 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ck;.  BtShared *
1f9b4 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
1f9b5 63 68 61 72 20 7a 45 72 72 5b 31 30 30 5d 3b 0a  char zErr[100];.
1f9b6 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
1f9b7 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72  nter(p);.  asser
1f9b8 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52  t( p->inTrans>TR
1f9b9 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d  ANS_NONE && pBt-
1f9ba 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 54  >inTransaction>T
1f9bb 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 6e  RANS_NONE );.  n
1f9bc 52 65 66 20 3d 20 73 71 6c 69 74 65 33 50 61 67  Ref = sqlite3Pag
1f9bd 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e  erRefcount(pBt->
1f9be 70 50 61 67 65 72 29 3b 0a 20 20 73 43 68 65 63  pPager);.  sChec
1f9bf 6b 2e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 73  k.pBt = pBt;.  s
1f9c0 43 68 65 63 6b 2e 70 50 61 67 65 72 20 3d 20 70  Check.pPager = p
1f9c1 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73 43  Bt->pPager;.  sC
1f9c2 68 65 63 6b 2e 6e 50 61 67 65 20 3d 20 62 74 72  heck.nPage = btr
1f9c3 65 65 50 61 67 65 63 6f 75 6e 74 28 73 43 68 65  eePagecount(sChe
1f9c4 63 6b 2e 70 42 74 29 3b 0a 20 20 73 43 68 65 63  ck.pBt);.  sChec
1f9c5 6b 2e 6d 78 45 72 72 20 3d 20 6d 78 45 72 72 3b  k.mxErr = mxErr;
1f9c6 0a 20 20 73 43 68 65 63 6b 2e 6e 45 72 72 20 3d  .  sCheck.nErr =
1f9c7 20 30 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 61 6c   0;.  sCheck.mal
1f9c8 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20  locFailed = 0;. 
1f9c9 20 2a 70 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69   *pnErr = 0;.  i
1f9ca 66 28 20 73 43 68 65 63 6b 2e 6e 50 61 67 65 3d  f( sCheck.nPage=
1f9cb 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1f9cc 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
1f9cd 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1f9ce 7d 0a 0a 20 20 73 43 68 65 63 6b 2e 61 50 67 52  }..  sCheck.aPgR
1f9cf 65 66 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  ef = sqlite3Mall
1f9d0 6f 63 5a 65 72 6f 28 28 73 43 68 65 63 6b 2e 6e  ocZero((sCheck.n
1f9d1 50 61 67 65 20 2f 20 38 29 2b 20 31 29 3b 0a 20  Page / 8)+ 1);. 
1f9d2 20 69 66 28 20 21 73 43 68 65 63 6b 2e 61 50 67   if( !sCheck.aPg
1f9d3 52 65 66 20 29 7b 0a 20 20 20 20 2a 70 6e 45 72  Ref ){.    *pnEr
1f9d4 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74  r = 1;.    sqlit
1f9d5 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
1f9d6 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
1f9d7 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e 47   }.  i = PENDING
1f9d8 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 3b  _BYTE_PAGE(pBt);
1f9d9 0a 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63 6b  .  if( i<=sCheck
1f9da 2e 6e 50 61 67 65 20 29 20 73 65 74 50 61 67 65  .nPage ) setPage
1f9db 52 65 66 65 72 65 6e 63 65 64 28 26 73 43 68 65  Referenced(&sChe
1f9dc 63 6b 2c 20 69 29 3b 0a 20 20 73 71 6c 69 74 65  ck, i);.  sqlite
1f9dd 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73  3StrAccumInit(&s
1f9de 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a 45  Check.errMsg, zE
1f9df 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29  rr, sizeof(zErr)
1f9e0 2c 20 32 30 30 30 30 29 3b 0a 20 20 73 43 68 65  , 20000);.  sChe
1f9e1 63 6b 2e 65 72 72 4d 73 67 2e 75 73 65 4d 61 6c  ck.errMsg.useMal
1f9e2 6c 6f 63 20 3d 20 32 3b 0a 0a 20 20 2f 2a 20 43  loc = 2;..  /* C
1f9e3 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69  heck the integri
1f9e4 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69  ty of the freeli
1f9e5 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 6b 4c  st.  */.  checkL
1f9e6 69 73 74 28 26 73 43 68 65 63 6b 2c 20 31 2c 20  ist(&sCheck, 1, 
1f9e7 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70  get4byte(&pBt->p
1f9e8 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d  Page1->aData[32]
1f9e9 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 67  ),.            g
1f9ea 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50  et4byte(&pBt->pP
1f9eb 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29  age1->aData[36])
1f9ec 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 73 74  , "Main freelist
1f9ed 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  : ");..  /* Chec
1f9ee 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 73  k all the tables
1f9ef 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
1f9f0 3b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 20 26  ; (int)i<nRoot &
1f9f1 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20  & sCheck.mxErr; 
1f9f2 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52  i++){.    if( aR
1f9f3 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74  oot[i]==0 ) cont
1f9f4 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51  inue;.#ifndef SQ
1f9f5 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1f9f6 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74  CUUM.    if( pBt
1f9f7 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20  ->autoVacuum && 
1f9f8 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20  aRoot[i]>1 ){.  
1f9f9 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28      checkPtrmap(
1f9fa 26 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69  &sCheck, aRoot[i
1f9fb 5d 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41  ], PTRMAP_ROOTPA
1f9fc 47 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d  GE, 0, 0);.    }
1f9fd 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63  .#endif.    chec
1f9fe 6b 54 72 65 65 50 61 67 65 28 26 73 43 68 65 63  kTreePage(&sChec
1f9ff 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69  k, aRoot[i], "Li
1fa00 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74 73  st of tree roots
1fa01 3a 20 22 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 29  : ", NULL, NULL)
1fa02 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ;.  }..  /* Make
1fa03 20 73 75 72 65 20 65 76 65 72 79 20 70 61 67 65   sure every page
1fa04 20 69 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20   in the file is 
1fa05 72 65 66 65 72 65 6e 63 65 64 0a 20 20 2a 2f 0a  referenced.  */.
1fa06 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 43    for(i=1; i<=sC
1fa07 68 65 63 6b 2e 6e 50 61 67 65 20 26 26 20 73 43  heck.nPage && sC
1fa08 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b 29  heck.mxErr; i++)
1fa09 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
1fa0a 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
1fa0b 20 20 20 20 69 66 28 20 67 65 74 50 61 67 65 52      if( getPageR
1fa0c 65 66 65 72 65 6e 63 65 64 28 26 73 43 68 65 63  eferenced(&sChec
1fa0d 6b 2c 20 69 29 3d 3d 30 20 29 7b 0a 20 20 20 20  k, i)==0 ){.    
1fa0e 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
1fa0f 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 61  (&sCheck, 0, "Pa
1fa10 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 75  ge %d is never u
1fa11 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a  sed", i);.    }.
1fa12 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20 49 66 20  #else.    /* If 
1fa13 74 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70  the database sup
1fa14 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75  ports auto-vacuu
1fa15 6d 2c 20 6d 61 6b 65 20 73 75 72 65 20 6e 6f 20  m, make sure no 
1fa16 74 61 62 6c 65 73 20 63 6f 6e 74 61 69 6e 0a 20  tables contain. 
1fa17 20 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 73     ** references
1fa18 20 74 6f 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20   to pointer-map 
1fa19 70 61 67 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  pages..    */.  
1fa1a 20 20 69 66 28 20 67 65 74 50 61 67 65 52 65 66    if( getPageRef
1fa1b 65 72 65 6e 63 65 64 28 26 73 43 68 65 63 6b 2c  erenced(&sCheck,
1fa1c 20 69 29 3d 3d 30 20 26 26 20 0a 20 20 20 20 20   i)==0 && .     
1fa1d 20 20 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f    (PTRMAP_PAGENO
1fa1e 28 70 42 74 2c 20 69 29 21 3d 69 20 7c 7c 20 21  (pBt, i)!=i || !
1fa1f 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
1fa20 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41   ){.      checkA
1fa21 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b  ppendMsg(&sCheck
1fa22 2c 20 30 2c 20 22 50 61 67 65 20 25 64 20 69 73  , 0, "Page %d is
1fa23 20 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69 29   never used", i)
1fa24 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1fa25 67 65 74 50 61 67 65 52 65 66 65 72 65 6e 63 65  getPageReference
1fa26 64 28 26 73 43 68 65 63 6b 2c 20 69 29 21 3d 30  d(&sCheck, i)!=0
1fa27 20 26 26 20 0a 20 20 20 20 20 20 20 28 50 54 52   && .       (PTR
1fa28 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20  MAP_PAGENO(pBt, 
1fa29 69 29 3d 3d 69 20 26 26 20 70 42 74 2d 3e 61 75  i)==i && pBt->au
1fa2a 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20  toVacuum) ){.   
1fa2b 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
1fa2c 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50  g(&sCheck, 0, "P
1fa2d 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20  ointer map page 
1fa2e 25 64 20 69 73 20 72 65 66 65 72 65 6e 63 65 64  %d is referenced
1fa2f 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  ", i);.    }.#en
1fa30 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61  dif.  }..  /* Ma
1fa31 6b 65 20 73 75 72 65 20 74 68 69 73 20 61 6e 61  ke sure this ana
1fa32 6c 79 73 69 73 20 64 69 64 20 6e 6f 74 20 6c 65  lysis did not le
1fa33 61 76 65 20 61 6e 79 20 75 6e 72 65 66 28 29 20  ave any unref() 
1fa34 70 61 67 65 73 2e 0a 20 20 2a 2a 20 54 68 69 73  pages..  ** This
1fa35 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20   is an internal 
1fa36 63 6f 6e 73 69 73 74 65 6e 63 79 20 63 68 65 63  consistency chec
1fa37 6b 3b 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20  k; an integrity 
1fa38 63 68 65 63 6b 0a 20 20 2a 2a 20 6f 66 20 74 68  check.  ** of th
1fa39 65 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63  e integrity chec
1fa3a 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45  k..  */.  if( NE
1fa3b 56 45 52 28 6e 52 65 66 20 21 3d 20 73 71 6c 69  VER(nRef != sqli
1fa3c 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74  te3PagerRefcount
1fa3d 28 70 42 74 2d 3e 70 50 61 67 65 72 29 29 20 29  (pBt->pPager)) )
1fa3e 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e  {.    checkAppen
1fa3f 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c  dMsg(&sCheck, 0,
1fa40 20 0a 20 20 20 20 20 20 22 4f 75 74 73 74 61 6e   .      "Outstan
1fa41 64 69 6e 67 20 70 61 67 65 20 63 6f 75 6e 74 20  ding page count 
1fa42 67 6f 65 73 20 66 72 6f 6d 20 25 64 20 74 6f 20  goes from %d to 
1fa43 25 64 20 64 75 72 69 6e 67 20 74 68 69 73 20 61  %d during this a
1fa44 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 20 20 20  nalysis",.      
1fa45 6e 52 65 66 2c 20 73 71 6c 69 74 65 33 50 61 67  nRef, sqlite3Pag
1fa46 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e  erRefcount(pBt->
1fa47 70 50 61 67 65 72 29 0a 20 20 20 20 29 3b 0a 20  pPager).    );. 
1fa48 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 20   }..  /* Clean  
1fa49 75 70 20 61 6e 64 20 72 65 70 6f 72 74 20 65 72  up and report er
1fa4a 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  rors..  */.  sql
1fa4b 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1fa4c 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
1fa4d 65 28 73 43 68 65 63 6b 2e 61 50 67 52 65 66 29  e(sCheck.aPgRef)
1fa4e 3b 0a 20 20 69 66 28 20 73 43 68 65 63 6b 2e 6d  ;.  if( sCheck.m
1fa4f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1fa50 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
1fa51 75 6d 52 65 73 65 74 28 26 73 43 68 65 63 6b 2e  umReset(&sCheck.
1fa52 65 72 72 4d 73 67 29 3b 0a 20 20 20 20 2a 70 6e  errMsg);.    *pn
1fa53 45 72 72 20 3d 20 73 43 68 65 63 6b 2e 6e 45 72  Err = sCheck.nEr
1fa54 72 2b 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  r+1;.    return 
1fa55 30 3b 0a 20 20 7d 0a 20 20 2a 70 6e 45 72 72 20  0;.  }.  *pnErr 
1fa56 3d 20 73 43 68 65 63 6b 2e 6e 45 72 72 3b 0a 20  = sCheck.nErr;. 
1fa57 20 69 66 28 20 73 43 68 65 63 6b 2e 6e 45 72 72   if( sCheck.nErr
1fa58 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 53 74 72  ==0 ) sqlite3Str
1fa59 41 63 63 75 6d 52 65 73 65 74 28 26 73 43 68 65  AccumReset(&sChe
1fa5a 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 20 20 72 65  ck.errMsg);.  re
1fa5b 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72 41  turn sqlite3StrA
1fa5c 63 63 75 6d 46 69 6e 69 73 68 28 26 73 43 68 65  ccumFinish(&sChe
1fa5d 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 7d 0a 23 65  ck.errMsg);.}.#e
1fa5e 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1fa5f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
1fa60 45 43 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  ECK */../*.** Re
1fa61 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61  turn the full pa
1fa62 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 75 6e  thname of the un
1fa63 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61 73  derlying databas
1fa64 65 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 0a  e file.  Return.
1fa65 2a 2a 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69  ** an empty stri
1fa66 6e 67 20 69 66 20 74 68 65 20 64 61 74 61 62 61  ng if the databa
1fa67 73 65 20 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20  se is in-memory 
1fa68 6f 72 20 61 20 54 45 4d 50 20 64 61 74 61 62 61  or a TEMP databa
1fa69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  se..**.** The pa
1fa6a 67 65 72 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  ger filename is 
1fa6b 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e  invariant as lon
1fa6c 67 20 61 73 20 74 68 65 20 70 61 67 65 72 20 69  g as the pager i
1fa6d 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20  s.** open so it 
1fa6e 69 73 20 73 61 66 65 20 74 6f 20 61 63 63 65 73  is safe to acces
1fa6f 73 20 77 69 74 68 6f 75 74 20 74 68 65 20 42 74  s without the Bt
1fa70 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f  Shared mutex..*/
1fa71 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1fa72 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
1fa73 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e  te3BtreeGetFilen
1fa74 61 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  ame(Btree *p){. 
1fa75 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d   assert( p->pBt-
1fa76 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20  >pPager!=0 );.  
1fa77 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 61  return sqlite3Pa
1fa78 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 2d 3e 70  gerFilename(p->p
1fa79 42 74 2d 3e 70 50 61 67 65 72 2c 20 31 29 3b 0a  Bt->pPager, 1);.
1fa7a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1fa7b 74 68 65 20 70 61 74 68 6e 61 6d 65 20 6f 66 20  the pathname of 
1fa7c 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
1fa7d 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61   for this databa
1fa7e 73 65 2e 20 54 68 65 20 72 65 74 75 72 6e 0a 2a  se. The return.*
1fa7f 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20  * value of this 
1fa80 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 73  routine is the s
1fa81 61 6d 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f  ame regardless o
1fa82 66 20 77 68 65 74 68 65 72 20 74 68 65 20 6a 6f  f whether the jo
1fa83 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 68 61  urnal file.** ha
1fa84 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 6f  s been created o
1fa85 72 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  r not..**.** The
1fa86 20 70 61 67 65 72 20 6a 6f 75 72 6e 61 6c 20 66   pager journal f
1fa87 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61 72  ilename is invar
1fa88 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20  iant as long as 
1fa89 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a 20  the pager is.** 
1fa8a 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73 61  open so it is sa
1fa8b 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 69 74  fe to access wit
1fa8c 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 72 65  hout the BtShare
1fa8d 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49  d mutex..*/.SQLI
1fa8e 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
1fa8f 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74   char *sqlite3Bt
1fa90 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d  reeGetJournalnam
1fa91 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61  e(Btree *p){.  a
1fa92 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70  ssert( p->pBt->p
1fa93 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65  Pager!=0 );.  re
1fa94 74 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65  turn sqlite3Page
1fa95 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 2d 3e  rJournalname(p->
1fa96 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a  pBt->pPager);.}.
1fa97 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f  ./*.** Return no
1fa98 6e 2d 7a 65 72 6f 20 69 66 20 61 20 74 72 61 6e  n-zero if a tran
1fa99 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76  saction is activ
1fa9a 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1fa9b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1fa9c 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 42  BtreeIsInTrans(B
1fa9d 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65  tree *p){.  asse
1fa9e 72 74 28 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69  rt( p==0 || sqli
1fa9f 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1faa0 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
1faa1 20 20 72 65 74 75 72 6e 20 28 70 20 26 26 20 28    return (p && (
1faa2 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
1faa3 53 5f 57 52 49 54 45 29 29 3b 0a 7d 0a 0a 23 69  S_WRITE));.}..#i
1faa4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1faa5 54 5f 57 41 4c 0a 2f 2a 0a 2a 2a 20 52 75 6e 20  T_WAL./*.** Run 
1faa6 61 20 63 68 65 63 6b 70 6f 69 6e 74 20 6f 6e 20  a checkpoint on 
1faa7 74 68 65 20 42 74 72 65 65 20 70 61 73 73 65 64  the Btree passed
1faa8 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
1faa9 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52 65  gument..**.** Re
1faaa 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  turn SQLITE_LOCK
1faab 45 44 20 69 66 20 74 68 69 73 20 6f 72 20 61 6e  ED if this or an
1faac 79 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  y other connecti
1faad 6f 6e 20 68 61 73 20 61 6e 20 6f 70 65 6e 20 0a  on has an open .
1faae 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  ** transaction o
1faaf 6e 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63  n the shared-cac
1fab0 68 65 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  he the argument 
1fab1 42 74 72 65 65 20 69 73 20 63 6f 6e 6e 65 63 74  Btree is connect
1fab2 65 64 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 50 61 72  ed to..**.** Par
1fab3 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 69 73 20  ameter eMode is 
1fab4 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 43 48  one of SQLITE_CH
1fab5 45 43 4b 50 4f 49 4e 54 5f 50 41 53 53 49 56 45  ECKPOINT_PASSIVE
1fab6 2c 20 46 55 4c 4c 20 6f 72 20 52 45 53 54 41 52  , FULL or RESTAR
1fab7 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  T..*/.SQLITE_PRI
1fab8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1fab9 42 74 72 65 65 43 68 65 63 6b 70 6f 69 6e 74 28  BtreeCheckpoint(
1faba 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 65 4d  Btree *p, int eM
1fabb 6f 64 65 2c 20 69 6e 74 20 2a 70 6e 4c 6f 67 2c  ode, int *pnLog,
1fabc 20 69 6e 74 20 2a 70 6e 43 6b 70 74 29 7b 0a 20   int *pnCkpt){. 
1fabd 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1fabe 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  _OK;.  if( p ){.
1fabf 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42      BtShared *pB
1fac0 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20  t = p->pBt;.    
1fac1 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1fac2 72 28 70 29 3b 0a 20 20 20 20 69 66 28 20 70 42  r(p);.    if( pB
1fac3 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
1fac4 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a  !=TRANS_NONE ){.
1fac5 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1fac6 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 7d 65  E_LOCKED;.    }e
1fac7 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
1fac8 73 71 6c 69 74 65 33 50 61 67 65 72 43 68 65 63  sqlite3PagerChec
1fac9 6b 70 6f 69 6e 74 28 70 42 74 2d 3e 70 50 61 67  kpoint(pBt->pPag
1faca 65 72 2c 20 65 4d 6f 64 65 2c 20 70 6e 4c 6f 67  er, eMode, pnLog
1facb 2c 20 70 6e 43 6b 70 74 29 3b 0a 20 20 20 20 7d  , pnCkpt);.    }
1facc 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1facd 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20  eLeave(p);.  }. 
1face 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
1facf 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
1fad0 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61  rn non-zero if a
1fad1 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 65 29   read (or write)
1fad2 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1fad3 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54  active..*/.SQLIT
1fad4 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1fad5 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65  lite3BtreeIsInRe
1fad6 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70  adTrans(Btree *p
1fad7 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29  ){.  assert( p )
1fad8 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
1fad9 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1fada 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
1fadb 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e 54 72    return p->inTr
1fadc 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 3b  ans!=TRANS_NONE;
1fadd 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
1fade 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1fadf 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 42 74  reeIsInBackup(Bt
1fae0 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  ree *p){.  asser
1fae1 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74  t( p );.  assert
1fae2 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1fae3 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  held(p->db->mute
1fae4 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  x) );.  return p
1fae5 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a 7d 0a  ->nBackup!=0;.}.
1fae6 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
1fae7 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70  tion returns a p
1fae8 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f 62  ointer to a blob
1fae9 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63   of memory assoc
1faea 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 20  iated with.** a 
1faeb 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d 62 74  single shared-bt
1faec 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72 79 20  ree. The memory 
1faed 69 73 20 75 73 65 64 20 62 79 20 63 6c 69 65 6e  is used by clien
1faee 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73 20 6f  t code for its o
1faef 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 28  wn.** purposes (
1faf0 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20  for example, to 
1faf1 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c 65 76  store a high-lev
1faf2 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69  el schema associ
1faf3 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68  ated with .** th
1faf4 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 29 2e  e shared-btree).
1faf5 20 54 68 65 20 62 74 72 65 65 20 6c 61 79 65 72   The btree layer
1faf6 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72 65 6e   manages referen
1faf7 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73 73 75  ce counting issu
1faf8 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  es..**.** The fi
1faf9 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69 73  rst time this is
1fafa 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61   called on a sha
1fafb 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79 74 65  red-btree, nByte
1fafc 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  s bytes of memor
1fafd 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 61 74  y.** are allocat
1fafe 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e 64 20  ed, zeroed, and 
1faff 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
1fb00 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61 63 68  caller. For each
1fb01 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a 2a 20   subsequent .** 
1fb02 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65 73 20  call the nBytes 
1fb03 70 61 72 61 6d 65 74 65 72 20 69 73 20 69 67 6e  parameter is ign
1fb04 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69 6e 74  ored and a point
1fb05 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65 20 62  er to the same b
1fb06 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79  lob.** of memory
1fb07 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a 2a   returned. .**.*
1fb08 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65 73 20  * If the nBytes 
1fb09 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 20 61  parameter is 0 a
1fb0a 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d  nd the blob of m
1fb0b 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20 79 65  emory has not ye
1fb0c 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61  t been.** alloca
1fb0d 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f 69 6e  ted, a null poin
1fb0e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ter is returned.
1fb0f 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68 61 73   If the blob has
1fb10 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 2a 2a   already been.**
1fb11 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 69   allocated, it i
1fb12 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 6e 6f  s returned as no
1fb13 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73 74  rmal..**.** Just
1fb14 20 62 65 66 6f 72 65 20 74 68 65 20 73 68 61 72   before the shar
1fb15 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c 6f 73  ed-btree is clos
1fb16 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  ed, the function
1fb17 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 0a   passed as the .
1fb18 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d 65 6e  ** xFree argumen
1fb19 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72  t when the memor
1fb1a 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73  y allocation was
1fb1b 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b 65 64   made is invoked
1fb1c 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c 6f 62   on the .** blob
1fb1d 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65   of allocated me
1fb1e 6d 6f 72 79 2e 20 54 68 65 20 78 46 72 65 65 20  mory. The xFree 
1fb1f 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
1fb20 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  not call sqlite3
1fb21 5f 66 72 65 65 28 29 0a 2a 2a 20 6f 6e 20 74 68  _free().** on th
1fb22 65 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 62 74  e memory, the bt
1fb23 72 65 65 20 6c 61 79 65 72 20 64 6f 65 73 20 74  ree layer does t
1fb24 68 61 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  hat..*/.SQLITE_P
1fb25 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
1fb26 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28  ite3BtreeSchema(
1fb27 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 42  Btree *p, int nB
1fb28 79 74 65 73 2c 20 76 6f 69 64 28 2a 78 46 72 65  ytes, void(*xFre
1fb29 65 29 28 76 6f 69 64 20 2a 29 29 7b 0a 20 20 42  e)(void *)){.  B
1fb2a 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
1fb2b 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33  ->pBt;.  sqlite3
1fb2c 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
1fb2d 20 69 66 28 20 21 70 42 74 2d 3e 70 53 63 68 65   if( !pBt->pSche
1fb2e 6d 61 20 26 26 20 6e 42 79 74 65 73 20 29 7b 0a  ma && nBytes ){.
1fb2f 20 20 20 20 70 42 74 2d 3e 70 53 63 68 65 6d 61      pBt->pSchema
1fb30 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1fb31 6f 63 5a 65 72 6f 28 30 2c 20 6e 42 79 74 65 73  ocZero(0, nBytes
1fb32 29 3b 0a 20 20 20 20 70 42 74 2d 3e 78 46 72 65  );.    pBt->xFre
1fb33 65 53 63 68 65 6d 61 20 3d 20 78 46 72 65 65 3b  eSchema = xFree;
1fb34 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
1fb35 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
1fb36 65 74 75 72 6e 20 70 42 74 2d 3e 70 53 63 68 65  eturn pBt->pSche
1fb37 6d 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ma;.}../*.** Ret
1fb38 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
1fb39 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 69 66  D_SHAREDCACHE if
1fb3a 20 61 6e 6f 74 68 65 72 20 75 73 65 72 20 6f 66   another user of
1fb3b 20 74 68 65 20 73 61 6d 65 20 73 68 61 72 65 64   the same shared
1fb3c 20 0a 2a 2a 20 62 74 72 65 65 20 61 73 20 74 68   .** btree as th
1fb3d 65 20 61 72 67 75 6d 65 6e 74 20 68 61 6e 64 6c  e argument handl
1fb3e 65 20 68 6f 6c 64 73 20 61 6e 20 65 78 63 6c 75  e holds an exclu
1fb3f 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  sive lock on the
1fb40 20 0a 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74   .** sqlite_mast
1fb41 65 72 20 74 61 62 6c 65 2e 20 4f 74 68 65 72 77  er table. Otherw
1fb42 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  ise SQLITE_OK..*
1fb43 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1fb44 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1fb45 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 74  eSchemaLocked(Bt
1fb46 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  ree *p){.  int r
1fb47 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  c;.  assert( sql
1fb48 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1fb49 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
1fb4a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
1fb4b 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20  nter(p);.  rc = 
1fb4c 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65  querySharedCache
1fb4d 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41 53  TableLock(p, MAS
1fb4e 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41 44 5f 4c  TER_ROOT, READ_L
1fb4f 4f 43 4b 29 3b 0a 20 20 61 73 73 65 72 74 28 20  OCK);.  assert( 
1fb50 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
1fb51 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b   rc==SQLITE_LOCK
1fb52 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 29  ED_SHAREDCACHE )
1fb53 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
1fb54 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
1fb55 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69 66 6e 64  rn rc;.}...#ifnd
1fb56 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
1fb57 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a  HARED_CACHE./*.*
1fb58 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20  * Obtain a lock 
1fb59 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 68 6f  on the table who
1fb5a 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20  se root page is 
1fb5b 69 54 61 62 2e 20 20 54 68 65 0a 2a 2a 20 6c 6f  iTab.  The.** lo
1fb5c 63 6b 20 69 73 20 61 20 77 72 69 74 65 20 6c 6f  ck is a write lo
1fb5d 63 6b 20 69 66 20 69 73 57 72 69 74 65 6c 6f 63  ck if isWriteloc
1fb5e 6b 20 69 73 20 74 72 75 65 20 6f 72 20 61 20 72  k is true or a r
1fb5f 65 61 64 20 6c 6f 63 6b 0a 2a 2a 20 69 66 20 69  ead lock.** if i
1fb60 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53  t is false..*/.S
1fb61 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1fb62 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f  t sqlite3BtreeLo
1fb63 63 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70  ckTable(Btree *p
1fb64 2c 20 69 6e 74 20 69 54 61 62 2c 20 75 38 20 69  , int iTab, u8 i
1fb65 73 57 72 69 74 65 4c 6f 63 6b 29 7b 0a 20 20 69  sWriteLock){.  i
1fb66 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1fb67 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  K;.  assert( p->
1fb68 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e  inTrans!=TRANS_N
1fb69 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  ONE );.  if( p->
1fb6a 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20  sharable ){.    
1fb6b 75 38 20 6c 6f 63 6b 54 79 70 65 20 3d 20 52 45  u8 lockType = RE
1fb6c 41 44 5f 4c 4f 43 4b 20 2b 20 69 73 57 72 69 74  AD_LOCK + isWrit
1fb6d 65 4c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65 72  eLock;.    asser
1fb6e 74 28 20 52 45 41 44 5f 4c 4f 43 4b 2b 31 3d 3d  t( READ_LOCK+1==
1fb6f 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20  WRITE_LOCK );.  
1fb70 20 20 61 73 73 65 72 74 28 20 69 73 57 72 69 74    assert( isWrit
1fb71 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72  eLock==0 || isWr
1fb72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 0a 20  iteLock==1 );.. 
1fb73 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45     sqlite3BtreeE
1fb74 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 72 63 20  nter(p);.    rc 
1fb75 3d 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63  = querySharedCac
1fb76 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69  heTableLock(p, i
1fb77 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a  Tab, lockType);.
1fb78 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1fb79 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
1fb7a 63 20 3d 20 73 65 74 53 68 61 72 65 64 43 61 63  c = setSharedCac
1fb7b 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69  heTableLock(p, i
1fb7c 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a  Tab, lockType);.
1fb7d 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1fb7e 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
1fb7f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1fb80 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  .}.#endif..#ifnd
1fb81 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
1fb82 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 41 72  NCRBLOB./*.** Ar
1fb83 67 75 6d 65 6e 74 20 70 43 73 72 20 6d 75 73 74  gument pCsr must
1fb84 20 62 65 20 61 20 63 75 72 73 6f 72 20 6f 70 65   be a cursor ope
1fb85 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 20  ned for writing 
1fb86 6f 6e 20 61 6e 20 0a 2a 2a 20 49 4e 54 4b 45 59  on an .** INTKEY
1fb87 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79   table currently
1fb88 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 76   pointing at a v
1fb89 61 6c 69 64 20 74 61 62 6c 65 20 65 6e 74 72 79  alid table entry
1fb8a 2e 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  . .** This funct
1fb8b 69 6f 6e 20 6d 6f 64 69 66 69 65 73 20 74 68 65  ion modifies the
1fb8c 20 64 61 74 61 20 73 74 6f 72 65 64 20 61 73 20   data stored as 
1fb8d 70 61 72 74 20 6f 66 20 74 68 61 74 20 65 6e 74  part of that ent
1fb8e 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74  ry..**.** Only t
1fb8f 68 65 20 64 61 74 61 20 63 6f 6e 74 65 6e 74 20  he data content 
1fb90 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6d 6f 64 69  may only be modi
1fb91 66 69 65 64 2c 20 69 74 20 69 73 20 6e 6f 74 20  fied, it is not 
1fb92 70 6f 73 73 69 62 6c 65 20 74 6f 20 0a 2a 2a 20  possible to .** 
1fb93 63 68 61 6e 67 65 20 74 68 65 20 6c 65 6e 67 74  change the lengt
1fb94 68 20 6f 66 20 74 68 65 20 64 61 74 61 20 73 74  h of the data st
1fb95 6f 72 65 64 2e 20 49 66 20 74 68 69 73 20 66 75  ored. If this fu
1fb96 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
1fb97 20 77 69 74 68 0a 2a 2a 20 70 61 72 61 6d 65 74   with.** paramet
1fb98 65 72 73 20 74 68 61 74 20 61 74 74 65 6d 70 74  ers that attempt
1fb99 20 74 6f 20 77 72 69 74 65 20 70 61 73 74 20 74   to write past t
1fb9a 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 65 78  he end of the ex
1fb9b 69 73 74 69 6e 67 20 64 61 74 61 2c 0a 2a 2a 20  isting data,.** 
1fb9c 6e 6f 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73  no modifications
1fb9d 20 61 72 65 20 6d 61 64 65 20 61 6e 64 20 53 51   are made and SQ
1fb9e 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 73 20  LITE_CORRUPT is 
1fb9f 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
1fba0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1fba1 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 44  sqlite3BtreePutD
1fba2 61 74 61 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ata(BtCursor *pC
1fba3 73 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20  sr, u32 offset, 
1fba4 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 7a  u32 amt, void *z
1fba5 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61  ){.  int rc;.  a
1fba6 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
1fba7 64 73 4d 75 74 65 78 28 70 43 73 72 29 20 29 3b  dsMutex(pCsr) );
1fba8 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1fba9 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
1fbaa 73 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e  sr->pBtree->db->
1fbab 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
1fbac 72 74 28 20 70 43 73 72 2d 3e 69 73 49 6e 63 72  rt( pCsr->isIncr
1fbad 62 6c 6f 62 48 61 6e 64 6c 65 20 29 3b 0a 0a 20  blobHandle );.. 
1fbae 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72   rc = restoreCur
1fbaf 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 73 72  sorPosition(pCsr
1fbb0 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
1fbb1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
1fbb2 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61  turn rc;.  }.  a
1fbb3 73 73 65 72 74 28 20 70 43 73 72 2d 3e 65 53 74  ssert( pCsr->eSt
1fbb4 61 74 65 21 3d 43 55 52 53 4f 52 5f 52 45 51 55  ate!=CURSOR_REQU
1fbb5 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28  IRESEEK );.  if(
1fbb6 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d 43   pCsr->eState!=C
1fbb7 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20  URSOR_VALID ){. 
1fbb8 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1fbb9 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 0a 20 20 2f  _ABORT;.  }..  /
1fbba 2a 20 43 68 65 63 6b 20 73 6f 6d 65 20 61 73 73  * Check some ass
1fbbb 75 6d 70 74 69 6f 6e 73 3a 20 0a 20 20 2a 2a 20  umptions: .  ** 
1fbbc 20 20 28 61 29 20 74 68 65 20 63 75 72 73 6f 72    (a) the cursor
1fbbd 20 69 73 20 6f 70 65 6e 20 66 6f 72 20 77 72 69   is open for wri
1fbbe 74 69 6e 67 2c 0a 20 20 2a 2a 20 20 20 28 62 29  ting,.  **   (b)
1fbbf 20 74 68 65 72 65 20 69 73 20 61 20 72 65 61 64   there is a read
1fbc0 2f 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69  /write transacti
1fbc1 6f 6e 20 6f 70 65 6e 2c 0a 20 20 2a 2a 20 20 20  on open,.  **   
1fbc2 28 63 29 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  (c) the connecti
1fbc3 6f 6e 20 68 6f 6c 64 73 20 61 20 77 72 69 74 65  on holds a write
1fbc4 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62  -lock on the tab
1fbc5 6c 65 20 28 69 66 20 72 65 71 75 69 72 65 64 29  le (if required)
1fbc6 2c 0a 20 20 2a 2a 20 20 20 28 64 29 20 74 68 65  ,.  **   (d) the
1fbc7 72 65 20 61 72 65 20 6e 6f 20 63 6f 6e 66 6c 69  re are no confli
1fbc8 63 74 69 6e 67 20 72 65 61 64 2d 6c 6f 63 6b 73  cting read-locks
1fbc9 2c 20 61 6e 64 0a 20 20 2a 2a 20 20 20 28 65 29  , and.  **   (e)
1fbca 20 74 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e   the cursor poin
1fbcb 74 73 20 61 74 20 61 20 76 61 6c 69 64 20 72 6f  ts at a valid ro
1fbcc 77 20 6f 66 20 61 6e 20 69 6e 74 4b 65 79 20 74  w of an intKey t
1fbcd 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  able..  */.  if(
1fbce 20 21 70 43 73 72 2d 3e 77 72 46 6c 61 67 20 29   !pCsr->wrFlag )
1fbcf 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1fbd0 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  ITE_READONLY;.  
1fbd1 7d 0a 20 20 61 73 73 65 72 74 28 20 28 70 43 73  }.  assert( (pCs
1fbd2 72 2d 3e 70 42 74 2d 3e 62 74 73 46 6c 61 67 73  r->pBt->btsFlags
1fbd3 20 26 20 42 54 53 5f 52 45 41 44 5f 4f 4e 4c 59   & BTS_READ_ONLY
1fbd4 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20  )==0.           
1fbd5 20 20 20 26 26 20 70 43 73 72 2d 3e 70 42 74 2d     && pCsr->pBt-
1fbd6 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d  >inTransaction==
1fbd7 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20  TRANS_WRITE );. 
1fbd8 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61 72   assert( hasShar
1fbd9 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
1fbda 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2c 20 70  (pCsr->pBtree, p
1fbdb 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 30  Csr->pgnoRoot, 0
1fbdc 2c 20 32 29 20 29 3b 0a 20 20 61 73 73 65 72 74  , 2) );.  assert
1fbdd 28 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69  ( !hasReadConfli
1fbde 63 74 73 28 70 43 73 72 2d 3e 70 42 74 72 65 65  cts(pCsr->pBtree
1fbdf 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f 74  , pCsr->pgnoRoot
1fbe0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
1fbe1 43 73 72 2d 3e 61 70 50 61 67 65 5b 70 43 73 72  Csr->apPage[pCsr
1fbe2 2d 3e 69 50 61 67 65 5d 2d 3e 69 6e 74 4b 65 79  ->iPage]->intKey
1fbe3 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 61 63   );..  return ac
1fbe4 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 73 72  cessPayload(pCsr
1fbe5 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 28  , offset, amt, (
1fbe6 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
1fbe7 7a 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  z, 1);.}../* .**
1fbe8 20 53 65 74 20 61 20 66 6c 61 67 20 6f 6e 20 74   Set a flag on t
1fbe9 68 69 73 20 63 75 72 73 6f 72 20 74 6f 20 63 61  his cursor to ca
1fbea 63 68 65 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e  che the location
1fbeb 73 20 6f 66 20 70 61 67 65 73 20 66 72 6f 6d 20  s of pages from 
1fbec 74 68 65 20 0a 2a 2a 20 6f 76 65 72 66 6c 6f 77  the .** overflow
1fbed 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20 63 75   list for the cu
1fbee 72 72 65 6e 74 20 72 6f 77 2e 20 54 68 69 73 20  rrent row. This 
1fbef 69 73 20 75 73 65 64 20 62 79 20 63 75 72 73 6f  is used by curso
1fbf0 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72  rs opened.** for
1fbf1 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f   incremental blo
1fbf2 62 20 49 4f 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a  b IO only..**.**
1fbf3 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   This function s
1fbf4 65 74 73 20 61 20 66 6c 61 67 20 6f 6e 6c 79 2e  ets a flag only.
1fbf5 20 54 68 65 20 61 63 74 75 61 6c 20 70 61 67 65   The actual page
1fbf6 20 6c 6f 63 61 74 69 6f 6e 20 63 61 63 68 65 0a   location cache.
1fbf7 2a 2a 20 28 73 74 6f 72 65 64 20 69 6e 20 42 74  ** (stored in Bt
1fbf8 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77  Cursor.aOverflow
1fbf9 5b 5d 29 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  []) is allocated
1fbfa 20 61 6e 64 20 75 73 65 64 20 62 79 20 66 75 6e   and used by fun
1fbfb 63 74 69 6f 6e 0a 2a 2a 20 61 63 63 65 73 73 50  ction.** accessP
1fbfc 61 79 6c 6f 61 64 28 29 20 28 74 68 65 20 77 6f  ayload() (the wo
1fbfd 72 6b 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f  rker function fo
1fbfe 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  r sqlite3BtreeDa
1fbff 74 61 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69  ta() and.** sqli
1fc00 74 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28  te3BtreePutData(
1fc01 29 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ))..*/.SQLITE_PR
1fc02 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1fc03 65 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72  e3BtreeCacheOver
1fc04 66 6c 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 70  flow(BtCursor *p
1fc05 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Cur){.  assert( 
1fc06 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
1fc07 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
1fc08 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1fc09 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74  x_held(pCur->pBt
1fc0a 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  ree->db->mutex) 
1fc0b 29 3b 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 4f  );.  invalidateO
1fc0c 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75  verflowCache(pCu
1fc0d 72 29 3b 0a 20 20 70 43 75 72 2d 3e 69 73 49 6e  r);.  pCur->isIn
1fc0e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 3d 20 31  crblobHandle = 1
1fc0f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1fc10 2a 20 53 65 74 20 62 6f 74 68 20 74 68 65 20 22  * Set both the "
1fc11 72 65 61 64 20 76 65 72 73 69 6f 6e 22 20 28 73  read version" (s
1fc12 69 6e 67 6c 65 20 62 79 74 65 20 61 74 20 62 79  ingle byte at by
1fc13 74 65 20 6f 66 66 73 65 74 20 31 38 29 20 61 6e  te offset 18) an
1fc14 64 20 0a 2a 2a 20 22 77 72 69 74 65 20 76 65 72  d .** "write ver
1fc15 73 69 6f 6e 22 20 28 73 69 6e 67 6c 65 20 62 79  sion" (single by
1fc16 74 65 20 61 74 20 62 79 74 65 20 6f 66 66 73 65  te at byte offse
1fc17 74 20 31 39 29 20 66 69 65 6c 64 73 20 69 6e 20  t 19) fields in 
1fc18 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
1fc19 68 65 61 64 65 72 20 74 6f 20 69 56 65 72 73 69  header to iVersi
1fc1a 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
1fc1b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1fc1c 33 42 74 72 65 65 53 65 74 56 65 72 73 69 6f 6e  3BtreeSetVersion
1fc1d 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20  (Btree *pBtree, 
1fc1e 69 6e 74 20 69 56 65 72 73 69 6f 6e 29 7b 0a 20  int iVersion){. 
1fc1f 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
1fc20 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20   pBtree->pBt;.  
1fc21 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
1fc22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc23 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
1fc24 2f 0a 20 0a 20 20 61 73 73 65 72 74 28 20 69 56  /. .  assert( iV
1fc25 65 72 73 69 6f 6e 3d 3d 31 20 7c 7c 20 69 56 65  ersion==1 || iVe
1fc26 72 73 69 6f 6e 3d 3d 32 20 29 3b 0a 0a 20 20 2f  rsion==2 );..  /
1fc27 2a 20 49 66 20 73 65 74 74 69 6e 67 20 74 68 65  * If setting the
1fc28 20 76 65 72 73 69 6f 6e 20 66 69 65 6c 64 73 20   version fields 
1fc29 74 6f 20 31 2c 20 64 6f 20 6e 6f 74 20 61 75 74  to 1, do not aut
1fc2a 6f 6d 61 74 69 63 61 6c 6c 79 20 6f 70 65 6e 20  omatically open 
1fc2b 74 68 65 0a 20 20 2a 2a 20 57 41 4c 20 63 6f 6e  the.  ** WAL con
1fc2c 6e 65 63 74 69 6f 6e 2c 20 65 76 65 6e 20 69 66  nection, even if
1fc2d 20 74 68 65 20 76 65 72 73 69 6f 6e 20 66 69 65   the version fie
1fc2e 6c 64 73 20 61 72 65 20 63 75 72 72 65 6e 74 6c  lds are currentl
1fc2f 79 20 73 65 74 20 74 6f 20 32 2e 0a 20 20 2a 2f  y set to 2..  */
1fc30 0a 20 20 70 42 74 2d 3e 62 74 73 46 6c 61 67 73  .  pBt->btsFlags
1fc31 20 26 3d 20 7e 42 54 53 5f 4e 4f 5f 57 41 4c 3b   &= ~BTS_NO_WAL;
1fc32 0a 20 20 69 66 28 20 69 56 65 72 73 69 6f 6e 3d  .  if( iVersion=
1fc33 3d 31 20 29 20 70 42 74 2d 3e 62 74 73 46 6c 61  =1 ) pBt->btsFla
1fc34 67 73 20 7c 3d 20 42 54 53 5f 4e 4f 5f 57 41 4c  gs |= BTS_NO_WAL
1fc35 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
1fc36 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73  3BtreeBeginTrans
1fc37 28 70 42 74 72 65 65 2c 20 30 29 3b 0a 20 20 69  (pBtree, 0);.  i
1fc38 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1fc39 20 29 7b 0a 20 20 20 20 75 38 20 2a 61 44 61 74   ){.    u8 *aDat
1fc3a 61 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 2d  a = pBt->pPage1-
1fc3b 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20  >aData;.    if( 
1fc3c 61 44 61 74 61 5b 31 38 5d 21 3d 28 75 38 29 69  aData[18]!=(u8)i
1fc3d 56 65 72 73 69 6f 6e 20 7c 7c 20 61 44 61 74 61  Version || aData
1fc3e 5b 31 39 5d 21 3d 28 75 38 29 69 56 65 72 73 69  [19]!=(u8)iVersi
1fc3f 6f 6e 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  on ){.      rc =
1fc40 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
1fc41 69 6e 54 72 61 6e 73 28 70 42 74 72 65 65 2c 20  inTrans(pBtree, 
1fc42 32 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  2);.      if( rc
1fc43 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1fc44 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1fc45 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 42  te3PagerWrite(pB
1fc46 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61  t->pPage1->pDbPa
1fc47 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ge);.        if(
1fc48 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1fc49 7b 0a 20 20 20 20 20 20 20 20 20 20 61 44 61 74  {.          aDat
1fc4a 61 5b 31 38 5d 20 3d 20 28 75 38 29 69 56 65 72  a[18] = (u8)iVer
1fc4b 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 20 20  sion;.          
1fc4c 61 44 61 74 61 5b 31 39 5d 20 3d 20 28 75 38 29  aData[19] = (u8)
1fc4d 69 56 65 72 73 69 6f 6e 3b 0a 20 20 20 20 20 20  iVersion;.      
1fc4e 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1fc4f 7d 0a 20 20 7d 0a 0a 20 20 70 42 74 2d 3e 62 74  }.  }..  pBt->bt
1fc50 73 46 6c 61 67 73 20 26 3d 20 7e 42 54 53 5f 4e  sFlags &= ~BTS_N
1fc51 4f 5f 57 41 4c 3b 0a 20 20 72 65 74 75 72 6e 20  O_WAL;.  return 
1fc52 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 65 74  rc;.}../*.** set
1fc53 20 74 68 65 20 6d 61 73 6b 20 6f 66 20 68 69 6e   the mask of hin
1fc54 74 20 66 6c 61 67 73 20 66 6f 72 20 63 75 72 73  t flags for curs
1fc55 6f 72 20 70 43 73 72 2e 20 43 75 72 72 65 6e 74  or pCsr. Current
1fc56 6c 79 20 74 68 65 20 6f 6e 6c 79 20 76 61 6c 69  ly the only vali
1fc57 64 0a 2a 2a 20 76 61 6c 75 65 73 20 61 72 65 20  d.** values are 
1fc58 30 20 61 6e 64 20 42 54 52 45 45 5f 42 55 4c 4b  0 and BTREE_BULK
1fc59 4c 4f 41 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  LOAD..*/.SQLITE_
1fc5a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1fc5b 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48  ite3BtreeCursorH
1fc5c 69 6e 74 73 28 42 74 43 75 72 73 6f 72 20 2a 70  ints(BtCursor *p
1fc5d 43 73 72 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e  Csr, unsigned in
1fc5e 74 20 6d 61 73 6b 29 7b 0a 20 20 61 73 73 65 72  t mask){.  asser
1fc5f 74 28 20 6d 61 73 6b 3d 3d 42 54 52 45 45 5f 42  t( mask==BTREE_B
1fc60 55 4c 4b 4c 4f 41 44 20 7c 7c 20 6d 61 73 6b 3d  ULKLOAD || mask=
1fc61 3d 30 20 29 3b 0a 20 20 70 43 73 72 2d 3e 68 69  =0 );.  pCsr->hi
1fc62 6e 74 73 20 3d 20 6d 61 73 6b 3b 0a 7d 0a 0a 2f  nts = mask;.}../
1fc63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
1fc64 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a  nd of btree.c **
1fc65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1fc68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
1fc69 65 67 69 6e 20 66 69 6c 65 20 62 61 63 6b 75 70  egin file backup
1fc6a 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1fc6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1fc6d 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e 75 61 72  *.** 2009 Januar
1fc6e 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 28.**.** The a
1fc6f 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
1fc70 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
1fc71 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
1fc72 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
1fc73 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
1fc74 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
1fc75 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
1fc76 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
1fc77 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
1fc78 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
1fc79 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
1fc7a 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
1fc7b 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
1fc7c 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
1fc7d 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
1fc7e 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
1fc7f 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
1fc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc84 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
1fc85 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d   contains the im
1fc86 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
1fc87 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  the sqlite3_back
1fc88 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20 41 50 49  up_XXX() .** API
1fc89 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 74   functions and t
1fc8a 68 65 20 72 65 6c 61 74 65 64 20 66 65 61 74 75  he related featu
1fc8b 72 65 73 2e 0a 2a 2f 0a 0a 2f 2a 20 4d 61 63 72  res..*/../* Macr
1fc8c 6f 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 69  o to find the mi
1fc8d 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d  nimum of two num
1fc8e 65 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a  eric values..*/.
1fc8f 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65  #ifndef MIN.# de
1fc90 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28 28  fine MIN(x,y) ((
1fc91 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 29 0a  x)<(y)?(x):(y)).
1fc92 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 74  #endif../*.** St
1fc93 72 75 63 74 75 72 65 20 61 6c 6c 6f 63 61 74 65  ructure allocate
1fc94 64 20 66 6f 72 20 65 61 63 68 20 62 61 63 6b 75  d for each backu
1fc95 70 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a  p operation..*/.
1fc96 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 62  struct sqlite3_b
1fc97 61 63 6b 75 70 20 7b 0a 20 20 73 71 6c 69 74 65  ackup {.  sqlite
1fc98 33 2a 20 70 44 65 73 74 44 62 3b 20 20 20 20 20  3* pDestDb;     
1fc99 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f     /* Destinatio
1fc9a 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  n database handl
1fc9b 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 44  e */.  Btree *pD
1fc9c 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  est;            
1fc9d 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 62  /* Destination b
1fc9e 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f 0a 20 20  -tree file */.  
1fc9f 75 33 32 20 69 44 65 73 74 53 63 68 65 6d 61 3b  u32 iDestSchema;
1fca0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67           /* Orig
1fca1 69 6e 61 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b  inal schema cook
1fca2 69 65 20 69 6e 20 64 65 73 74 69 6e 61 74 69 6f  ie in destinatio
1fca3 6e 20 2a 2f 0a 20 20 69 6e 74 20 62 44 65 73 74  n */.  int bDest
1fca4 4c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20  Locked;         
1fca5 2f 2a 20 54 72 75 65 20 6f 6e 63 65 20 61 20 77  /* True once a w
1fca6 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
1fca7 20 69 73 20 6f 70 65 6e 20 6f 6e 20 70 44 65 73   is open on pDes
1fca8 74 20 2a 2f 0a 0a 20 20 50 67 6e 6f 20 69 4e 65  t */..  Pgno iNe
1fca9 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  xt;             
1fcaa 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
1fcab 6f 66 20 74 68 65 20 6e 65 78 74 20 73 6f 75 72  of the next sour
1fcac 63 65 20 70 61 67 65 20 74 6f 20 63 6f 70 79 20  ce page to copy 
1fcad 2a 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 53  */.  sqlite3* pS
1fcae 72 63 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a  rcDb;         /*
1fcaf 20 53 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   Source database
1fcb0 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72   handle */.  Btr
1fcb1 65 65 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20  ee *pSrc;       
1fcb2 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20        /* Source 
1fcb3 62 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f 0a 0a  b-tree file */..
1fcb4 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
1fcb5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61             /* Ba
1fcb6 63 6b 75 70 20 70 72 6f 63 65 73 73 20 65 72 72  ckup process err
1fcb7 6f 72 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a  or code */..  /*
1fcb8 20 54 68 65 73 65 20 74 77 6f 20 76 61 72 69 61   These two varia
1fcb9 62 6c 65 73 20 61 72 65 20 73 65 74 20 62 79 20  bles are set by 
1fcba 65 76 65 72 79 20 63 61 6c 6c 20 74 6f 20 62 61  every call to ba
1fcbb 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 54 68 65  ckup_step(). The
1fcbc 79 20 61 72 65 0a 20 20 2a 2a 20 72 65 61 64 20  y are.  ** read 
1fcbd 62 79 20 63 61 6c 6c 73 20 74 6f 20 62 61 63 6b  by calls to back
1fcbe 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61  up_remaining() a
1fcbf 6e 64 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f  nd backup_pageco
1fcc0 75 6e 74 28 29 2e 0a 20 20 2a 2f 0a 20 20 50 67  unt()..  */.  Pg
1fcc1 6e 6f 20 6e 52 65 6d 61 69 6e 69 6e 67 3b 20 20  no nRemaining;  
1fcc2 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1fcc3 20 6f 66 20 70 61 67 65 73 20 6c 65 66 74 20 74   of pages left t
1fcc4 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 50 67 6e 6f  o copy */.  Pgno
1fcc5 20 6e 50 61 67 65 63 6f 75 6e 74 3b 20 20 20 20   nPagecount;    
1fcc6 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
1fcc7 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f  mber of pages to
1fcc8 20 63 6f 70 79 20 2a 2f 0a 0a 20 20 69 6e 74 20   copy */..  int 
1fcc9 69 73 41 74 74 61 63 68 65 64 3b 20 20 20 20 20  isAttached;     
1fcca 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63       /* True onc
1fccb 65 20 62 61 63 6b 75 70 20 68 61 73 20 62 65 65  e backup has bee
1fccc 6e 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74  n registered wit
1fccd 68 20 70 61 67 65 72 20 2a 2f 0a 20 20 73 71 6c  h pager */.  sql
1fcce 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 4e 65  ite3_backup *pNe
1fccf 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20 62 61  xt;   /* Next ba
1fcd0 63 6b 75 70 20 61 73 73 6f 63 69 61 74 65 64 20  ckup associated 
1fcd1 77 69 74 68 20 73 6f 75 72 63 65 20 70 61 67 65  with source page
1fcd2 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  r */.};../*.** T
1fcd3 48 52 45 41 44 20 53 41 46 45 54 59 20 4e 4f 54  HREAD SAFETY NOT
1fcd4 45 53 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 6e 63 65  ES:.**.**   Once
1fcd5 20 69 74 20 68 61 73 20 62 65 65 6e 20 63 72 65   it has been cre
1fcd6 61 74 65 64 20 75 73 69 6e 67 20 62 61 63 6b 75  ated using backu
1fcd7 70 5f 69 6e 69 74 28 29 2c 20 61 20 73 69 6e 67  p_init(), a sing
1fcd8 6c 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  le sqlite3_backu
1fcd9 70 0a 2a 2a 20 20 20 73 74 72 75 63 74 75 72 65  p.**   structure
1fcda 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64   may be accessed
1fcdb 20 76 69 61 20 74 77 6f 20 67 72 6f 75 70 73 20   via two groups 
1fcdc 6f 66 20 74 68 72 65 61 64 2d 73 61 66 65 20 65  of thread-safe e
1fcdd 6e 74 72 79 20 70 6f 69 6e 74 73 3a 0a 2a 2a 0a  ntry points:.**.
1fcde 2a 2a 20 20 20 20 20 2a 20 56 69 61 20 74 68 65  **     * Via the
1fcdf 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
1fce0 58 58 58 28 29 20 41 50 49 20 66 75 6e 63 74 69  XXX() API functi
1fce1 6f 6e 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29  on backup_step()
1fce2 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 20 20 62   and .**       b
1fce3 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 20  ackup_finish(). 
1fce4 42 6f 74 68 20 74 68 65 73 65 20 66 75 6e 63 74  Both these funct
1fce5 69 6f 6e 73 20 6f 62 74 61 69 6e 20 74 68 65 20  ions obtain the 
1fce6 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 0a  source database.
1fce7 2a 2a 20 20 20 20 20 20 20 68 61 6e 64 6c 65 20  **       handle 
1fce8 6d 75 74 65 78 20 61 6e 64 20 74 68 65 20 6d 75  mutex and the mu
1fce9 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  tex associated w
1fcea 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 42  ith the source B
1fceb 74 53 68 61 72 65 64 20 0a 2a 2a 20 20 20 20 20  tShared .**     
1fcec 20 20 73 74 72 75 63 74 75 72 65 2c 20 69 6e 20    structure, in 
1fced 74 68 61 74 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a  that order..**.*
1fcee 2a 20 20 20 20 20 2a 20 56 69 61 20 74 68 65 20  *     * Via the 
1fcef 42 61 63 6b 75 70 55 70 64 61 74 65 28 29 20 61  BackupUpdate() a
1fcf0 6e 64 20 42 61 63 6b 75 70 52 65 73 74 61 72 74  nd BackupRestart
1fcf1 28 29 20 66 75 6e 63 74 69 6f 6e 73 2c 20 77 68  () functions, wh
1fcf2 69 63 68 20 61 72 65 0a 2a 2a 20 20 20 20 20 20  ich are.**      
1fcf3 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20   invoked by the 
1fcf4 70 61 67 65 72 20 6c 61 79 65 72 20 74 6f 20 72  pager layer to r
1fcf5 65 70 6f 72 74 20 76 61 72 69 6f 75 73 20 73 74  eport various st
1fcf6 61 74 65 20 63 68 61 6e 67 65 73 20 69 6e 0a 2a  ate changes in.*
1fcf7 2a 20 20 20 20 20 20 20 74 68 65 20 70 61 67 65  *       the page
1fcf8 20 63 61 63 68 65 20 61 73 73 6f 63 69 61 74 65   cache associate
1fcf9 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 72 63  d with the sourc
1fcfa 65 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20  e database. The 
1fcfb 6d 75 74 65 78 0a 2a 2a 20 20 20 20 20 20 20 61  mutex.**       a
1fcfc 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
1fcfd 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
1fcfe 73 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75  se BtShared stru
1fcff 63 74 75 72 65 20 77 69 6c 6c 20 61 6c 77 61 79  cture will alway
1fd00 73 20 0a 2a 2a 20 20 20 20 20 20 20 62 65 20 68  s .**       be h
1fd01 65 6c 64 20 77 68 65 6e 20 65 69 74 68 65 72 20  eld when either 
1fd02 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f  of these functio
1fd03 6e 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 2e 0a  ns are invoked..
1fd04 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 6f 74 68 65  **.**   The othe
1fd05 72 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  r sqlite3_backup
1fd06 5f 58 58 58 28 29 20 41 50 49 20 66 75 6e 63 74  _XXX() API funct
1fd07 69 6f 6e 73 2c 20 62 61 63 6b 75 70 5f 72 65 6d  ions, backup_rem
1fd08 61 69 6e 69 6e 67 28 29 20 61 6e 64 0a 2a 2a 20  aining() and.** 
1fd09 20 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75    backup_pagecou
1fd0a 6e 74 28 29 20 61 72 65 20 6e 6f 74 20 74 68 72  nt() are not thr
1fd0b 65 61 64 2d 73 61 66 65 20 66 75 6e 63 74 69 6f  ead-safe functio
1fd0c 6e 73 2e 20 49 66 20 74 68 65 79 20 61 72 65 20  ns. If they are 
1fd0d 63 61 6c 6c 65 64 0a 2a 2a 20 20 20 77 68 69 6c  called.**   whil
1fd0e 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72  e some other thr
1fd0f 65 61 64 20 69 73 20 63 61 6c 6c 69 6e 67 20 62  ead is calling b
1fd10 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6f 72 20  ackup_step() or 
1fd11 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2c  backup_finish(),
1fd12 0a 2a 2a 20 20 20 74 68 65 20 76 61 6c 75 65 73  .**   the values
1fd13 20 72 65 74 75 72 6e 65 64 20 6d 61 79 20 62 65   returned may be
1fd14 20 69 6e 76 61 6c 69 64 2e 20 54 68 65 72 65 20   invalid. There 
1fd15 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 61 20  is no way for a 
1fd16 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 42 61 63  call to.**   Bac
1fd17 6b 75 70 55 70 64 61 74 65 28 29 20 6f 72 20 42  kupUpdate() or B
1fd18 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 20 74  ackupRestart() t
1fd19 6f 20 69 6e 74 65 72 66 65 72 65 20 77 69 74 68  o interfere with
1fd1a 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e   backup_remainin
1fd1b 67 28 29 0a 2a 2a 20 20 20 6f 72 20 62 61 63 6b  g().**   or back
1fd1c 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a  up_pagecount()..
1fd1d 2a 2a 0a 2a 2a 20 20 20 44 65 70 65 6e 64 69 6e  **.**   Dependin
1fd1e 67 20 6f 6e 20 74 68 65 20 53 51 4c 69 74 65 20  g on the SQLite 
1fd1f 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 74  configuration, t
1fd20 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
1fd21 6c 65 73 20 61 6e 64 2f 6f 72 0a 2a 2a 20 20 20  les and/or.**   
1fd22 74 68 65 20 42 74 72 65 65 20 6f 62 6a 65 63 74  the Btree object
1fd23 73 20 6d 61 79 20 68 61 76 65 20 74 68 65 69 72  s may have their
1fd24 20 6f 77 6e 20 6d 75 74 65 78 65 73 20 74 68 61   own mutexes tha
1fd25 74 20 72 65 71 75 69 72 65 20 6c 6f 63 6b 69 6e  t require lockin
1fd26 67 2e 0a 2a 2a 20 20 20 4e 6f 6e 2d 73 68 61 72  g..**   Non-shar
1fd27 61 62 6c 65 20 42 74 72 65 65 73 20 28 69 6e 2d  able Btrees (in-
1fd28 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73  memory databases
1fd29 20 66 6f 72 20 65 78 61 6d 70 6c 65 29 2c 20 64   for example), d
1fd2a 6f 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20  o not have.**   
1fd2b 61 73 73 6f 63 69 61 74 65 64 20 6d 75 74 65 78  associated mutex
1fd2c 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  es..*/../*.** Re
1fd2d 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 63  turn a pointer c
1fd2e 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
1fd2f 64 61 74 61 62 61 73 65 20 7a 44 62 20 28 69 2e  database zDb (i.
1fd30 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70  e. "main", "temp
1fd31 22 29 0a 2a 2a 20 69 6e 20 63 6f 6e 6e 65 63 74  ").** in connect
1fd32 69 6f 6e 20 68 61 6e 64 6c 65 20 70 44 62 2e 20  ion handle pDb. 
1fd33 49 66 20 73 75 63 68 20 61 20 64 61 74 61 62 61  If such a databa
1fd34 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75  se cannot be fou
1fd35 6e 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  nd, return.** a 
1fd36 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 6e 64  NULL pointer and
1fd37 20 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20   write an error 
1fd38 6d 65 73 73 61 67 65 20 74 6f 20 70 45 72 72 6f  message to pErro
1fd39 72 44 62 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  rDb..**.** If th
1fd3a 65 20 22 74 65 6d 70 22 20 64 61 74 61 62 61 73  e "temp" databas
1fd3b 65 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20  e is requested, 
1fd3c 69 74 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62  it may need to b
1fd3d 65 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73  e opened by this
1fd3e 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 49   .** function. I
1fd3f 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
1fd40 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f  s while doing so
1fd41 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 77  , return 0 and w
1fd42 72 69 74 65 20 61 6e 20 0a 2a 2a 20 65 72 72 6f  rite an .** erro
1fd43 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 45 72  r message to pEr
1fd44 72 6f 72 44 62 2e 0a 2a 2f 0a 73 74 61 74 69 63  rorDb..*/.static
1fd45 20 42 74 72 65 65 20 2a 66 69 6e 64 42 74 72 65   Btree *findBtre
1fd46 65 28 73 71 6c 69 74 65 33 20 2a 70 45 72 72 6f  e(sqlite3 *pErro
1fd47 72 44 62 2c 20 73 71 6c 69 74 65 33 20 2a 70 44  rDb, sqlite3 *pD
1fd48 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
1fd49 44 62 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 73  Db){.  int i = s
1fd4a 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65  qlite3FindDbName
1fd4b 28 70 44 62 2c 20 7a 44 62 29 3b 0a 0a 20 20 69  (pDb, zDb);..  i
1fd4c 66 28 20 69 3d 3d 31 20 29 7b 0a 20 20 20 20 50  f( i==1 ){.    P
1fd4d 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 20 20  arse *pParse;.  
1fd4e 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20    int rc = 0;.  
1fd4f 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c 69 74    pParse = sqlit
1fd50 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f  e3StackAllocZero
1fd51 28 70 45 72 72 6f 72 44 62 2c 20 73 69 7a 65 6f  (pErrorDb, sizeo
1fd52 66 28 2a 70 50 61 72 73 65 29 29 3b 0a 20 20 20  f(*pParse));.   
1fd53 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20 29   if( pParse==0 )
1fd54 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1fd55 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 53  rror(pErrorDb, S
1fd56 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 22 6f 75  QLITE_NOMEM, "ou
1fd57 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20  t of memory");. 
1fd58 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1fd59 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73  _NOMEM;.    }els
1fd5a 65 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  e{.      pParse-
1fd5b 3e 64 62 20 3d 20 70 44 62 3b 0a 20 20 20 20 20  >db = pDb;.     
1fd5c 20 69 66 28 20 73 71 6c 69 74 65 33 4f 70 65 6e   if( sqlite3Open
1fd5d 54 65 6d 70 44 61 74 61 62 61 73 65 28 70 50 61  TempDatabase(pPa
1fd5e 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  rse) ){.        
1fd5f 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 45 72  sqlite3Error(pEr
1fd60 72 6f 72 44 62 2c 20 70 50 61 72 73 65 2d 3e 72  rorDb, pParse->r
1fd61 63 2c 20 22 25 73 22 2c 20 70 50 61 72 73 65 2d  c, "%s", pParse-
1fd62 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20  >zErrMsg);.     
1fd63 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
1fd64 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20  RROR;.      }.  
1fd65 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1fd66 65 28 70 45 72 72 6f 72 44 62 2c 20 70 50 61 72  e(pErrorDb, pPar
1fd67 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  se->zErrMsg);.  
1fd68 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b      sqlite3Stack
1fd69 46 72 65 65 28 70 45 72 72 6f 72 44 62 2c 20 70  Free(pErrorDb, p
1fd6a 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20  Parse);.    }.  
1fd6b 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
1fd6c 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
1fd6d 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 3c 30  }.  }..  if( i<0
1fd6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1fd6f 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 53  rror(pErrorDb, S
1fd70 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 75 6e  QLITE_ERROR, "un
1fd71 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 65 20 25  known database %
1fd72 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20 72 65  s", zDb);.    re
1fd73 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 72  turn 0;.  }..  r
1fd74 65 74 75 72 6e 20 70 44 62 2d 3e 61 44 62 5b 69  eturn pDb->aDb[i
1fd75 5d 2e 70 42 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ].pBt;.}../*.** 
1fd76 41 74 74 65 6d 70 74 20 74 6f 20 73 65 74 20 74  Attempt to set t
1fd77 68 65 20 70 61 67 65 20 73 69 7a 65 20 6f 66 20  he page size of 
1fd78 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
1fd79 74 6f 20 6d 61 74 63 68 20 74 68 65 20 70 61 67  to match the pag
1fd7a 65 20 73 69 7a 65 0a 2a 2a 20 6f 66 20 74 68 65  e size.** of the
1fd7b 20 73 6f 75 72 63 65 2e 0a 2a 2f 0a 73 74 61 74   source..*/.stat
1fd7c 69 63 20 69 6e 74 20 73 65 74 44 65 73 74 50 67  ic int setDestPg
1fd7d 73 7a 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  sz(sqlite3_backu
1fd7e 70 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  p *p){.  int rc;
1fd7f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
1fd80 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28  treeSetPageSize(
1fd81 70 2d 3e 70 44 65 73 74 2c 73 71 6c 69 74 65 33  p->pDest,sqlite3
1fd82 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65  BtreeGetPageSize
1fd83 28 70 2d 3e 70 53 72 63 29 2c 2d 31 2c 30 29 3b  (p->pSrc),-1,0);
1fd84 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1fd85 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 6e  ./*.** Create an
1fd86 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
1fd87 70 72 6f 63 65 73 73 20 74 6f 20 63 6f 70 79 20  process to copy 
1fd88 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
1fd89 7a 53 72 63 44 62 20 66 72 6f 6d 0a 2a 2a 20 63  zSrcDb from.** c
1fd8a 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65  onnection handle
1fd8b 20 70 53 72 63 44 62 20 74 6f 20 7a 44 65 73 74   pSrcDb to zDest
1fd8c 44 62 20 69 6e 20 70 44 65 73 74 44 62 2e 20 49  Db in pDestDb. I
1fd8d 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65  f successful, re
1fd8e 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  turn.** a pointe
1fd8f 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 71 6c  r to the new sql
1fd90 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65  ite3_backup obje
1fd91 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ct..**.** If an 
1fd92 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55  error occurs, NU
1fd93 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  LL is returned a
1fd94 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  nd an error code
1fd95 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61   and error messa
1fd96 67 65 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20  ge.** stored in 
1fd97 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
1fd98 70 44 65 73 74 44 62 2e 0a 2a 2f 0a 53 51 4c 49  pDestDb..*/.SQLI
1fd99 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 62  TE_API sqlite3_b
1fd9a 61 63 6b 75 70 20 2a 73 71 6c 69 74 65 33 5f 62  ackup *sqlite3_b
1fd9b 61 63 6b 75 70 5f 69 6e 69 74 28 0a 20 20 73 71  ackup_init(.  sq
1fd9c 6c 69 74 65 33 2a 20 70 44 65 73 74 44 62 2c 20  lite3* pDestDb, 
1fd9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fd9e 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
1fd9f 74 6f 20 77 72 69 74 65 20 74 6f 20 2a 2f 0a 20  to write to */. 
1fda0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 65   const char *zDe
1fda1 73 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 20  stDb,           
1fda2 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
1fda3 66 20 64 61 74 61 62 61 73 65 20 77 69 74 68 69  f database withi
1fda4 6e 20 70 44 65 73 74 44 62 20 2a 2f 0a 20 20 73  n pDestDb */.  s
1fda5 71 6c 69 74 65 33 2a 20 70 53 72 63 44 62 2c 20  qlite3* pSrcDb, 
1fda6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fda7 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
1fda8 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72   connection to r
1fda9 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 63 6f  ead from */.  co
1fdaa 6e 73 74 20 63 68 61 72 20 2a 7a 53 72 63 44 62  nst char *zSrcDb
1fdab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fdac 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64      /* Name of d
1fdad 61 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 70  atabase within p
1fdae 53 72 63 44 62 20 2a 2f 0a 29 7b 0a 20 20 73 71  SrcDb */.){.  sq
1fdaf 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b  lite3_backup *p;
1fdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fdb1 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
1fdb2 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20  return */..  /* 
1fdb3 4c 6f 63 6b 20 74 68 65 20 73 6f 75 72 63 65 20  Lock the source 
1fdb4 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  database handle.
1fdb5 20 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   The destination
1fdb6 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 68   database.  ** h
1fdb7 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 6c 6f 63  andle is not loc
1fdb8 6b 65 64 20 69 6e 20 74 68 69 73 20 72 6f 75 74  ked in this rout
1fdb9 69 6e 65 2c 20 62 75 74 20 69 74 20 69 73 20 6c  ine, but it is l
1fdba 6f 63 6b 65 64 20 69 6e 0a 20 20 2a 2a 20 73 71  ocked in.  ** sq
1fdbb 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
1fdbc 70 28 29 2e 20 54 68 65 20 75 73 65 72 20 69 73  p(). The user is
1fdbd 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 6e 73   required to ens
1fdbe 75 72 65 20 74 68 61 74 20 6e 6f 0a 20 20 2a 2a  ure that no.  **
1fdbf 20 6f 74 68 65 72 20 74 68 72 65 61 64 20 61 63   other thread ac
1fdc0 63 65 73 73 65 73 20 74 68 65 20 64 65 73 74 69  cesses the desti
1fdc1 6e 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66 6f  nation handle fo
1fdc2 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 0a 20  r the duration. 
1fdc3 20 2a 2a 20 6f 66 20 74 68 65 20 62 61 63 6b 75   ** of the backu
1fdc4 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e  p operation.  An
1fdc5 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65  y attempt to use
1fdc6 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
1fdc7 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63  .  ** database c
1fdc8 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20  onnection while 
1fdc9 61 20 62 61 63 6b 75 70 20 69 73 20 69 6e 20 70  a backup is in p
1fdca 72 6f 67 72 65 73 73 20 6d 61 79 20 63 61 75 73  rogress may caus
1fdcb 65 0a 20 20 2a 2a 20 61 20 6d 61 6c 66 75 6e 63  e.  ** a malfunc
1fdcc 74 69 6f 6e 20 6f 72 20 61 20 64 65 61 64 6c 6f  tion or a deadlo
1fdcd 63 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ck..  */.  sqlit
1fdce 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
1fdcf 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  SrcDb->mutex);. 
1fdd0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1fdd1 6e 74 65 72 28 70 44 65 73 74 44 62 2d 3e 6d 75  nter(pDestDb->mu
1fdd2 74 65 78 29 3b 0a 0a 20 20 69 66 28 20 70 53 72  tex);..  if( pSr
1fdd3 63 44 62 3d 3d 70 44 65 73 74 44 62 20 29 7b 0a  cDb==pDestDb ){.
1fdd4 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1fdd5 28 0a 20 20 20 20 20 20 20 20 70 44 65 73 74 44  (.        pDestD
1fdd6 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c  b, SQLITE_ERROR,
1fdd7 20 22 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73   "source and des
1fdd8 74 69 6e 61 74 69 6f 6e 20 6d 75 73 74 20 62 65  tination must be
1fdd9 20 64 69 73 74 69 6e 63 74 22 0a 20 20 20 20 29   distinct".    )
1fdda 3b 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d  ;.    p = 0;.  }
1fddb 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 41 6c  else {.    /* Al
1fddc 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72  locate space for
1fddd 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 62   a new sqlite3_b
1fdde 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 2e 2e 0a  ackup object....
1fddf 20 20 20 20 2a 2a 20 45 56 49 44 45 4e 43 45 2d      ** EVIDENCE-
1fde0 4f 46 3a 20 52 2d 36 34 38 35 32 2d 32 31 35 39  OF: R-64852-2159
1fde1 31 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 61  1 The sqlite3_ba
1fde2 63 6b 75 70 20 6f 62 6a 65 63 74 20 69 73 20 63  ckup object is c
1fde3 72 65 61 74 65 64 20 62 79 20 61 0a 20 20 20 20  reated by a.    
1fde4 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  ** call to sqlit
1fde5 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29  e3_backup_init()
1fde6 20 61 6e 64 20 69 73 20 64 65 73 74 72 6f 79 65   and is destroye
1fde7 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 20  d by a call to. 
1fde8 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61     ** sqlite3_ba
1fde9 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 20 2a  ckup_finish(). *
1fdea 2f 0a 20 20 20 20 70 20 3d 20 28 73 71 6c 69 74  /.    p = (sqlit
1fdeb 65 33 5f 62 61 63 6b 75 70 20 2a 29 73 71 6c 69  e3_backup *)sqli
1fdec 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69  te3MallocZero(si
1fded 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63  zeof(sqlite3_bac
1fdee 6b 75 70 29 29 3b 0a 20 20 20 20 69 66 28 20 21  kup));.    if( !
1fdef 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  p ){.      sqlit
1fdf0 65 33 45 72 72 6f 72 28 70 44 65 73 74 44 62 2c  e3Error(pDestDb,
1fdf1 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30   SQLITE_NOMEM, 0
1fdf2 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
1fdf3 2f 2a 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61  /* If the alloca
1fdf4 74 69 6f 6e 20 73 75 63 63 65 65 64 65 64 2c 20  tion succeeded, 
1fdf5 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e 65 77  populate the new
1fdf6 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 69 66   object. */.  if
1fdf7 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53  ( p ){.    p->pS
1fdf8 72 63 20 3d 20 66 69 6e 64 42 74 72 65 65 28 70  rc = findBtree(p
1fdf9 44 65 73 74 44 62 2c 20 70 53 72 63 44 62 2c 20  DestDb, pSrcDb, 
1fdfa 7a 53 72 63 44 62 29 3b 0a 20 20 20 20 70 2d 3e  zSrcDb);.    p->
1fdfb 70 44 65 73 74 20 3d 20 66 69 6e 64 42 74 72 65  pDest = findBtre
1fdfc 65 28 70 44 65 73 74 44 62 2c 20 70 44 65 73 74  e(pDestDb, pDest
1fdfd 44 62 2c 20 7a 44 65 73 74 44 62 29 3b 0a 20 20  Db, zDestDb);.  
1fdfe 20 20 70 2d 3e 70 44 65 73 74 44 62 20 3d 20 70    p->pDestDb = p
1fdff 44 65 73 74 44 62 3b 0a 20 20 20 20 70 2d 3e 70  DestDb;.    p->p
1fe00 53 72 63 44 62 20 3d 20 70 53 72 63 44 62 3b 0a  SrcDb = pSrcDb;.
1fe01 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31      p->iNext = 1
1fe02 3b 0a 20 20 20 20 70 2d 3e 69 73 41 74 74 61 63  ;.    p->isAttac
1fe03 68 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66  hed = 0;..    if
1fe04 28 20 30 3d 3d 70 2d 3e 70 53 72 63 20 7c 7c 20  ( 0==p->pSrc || 
1fe05 30 3d 3d 70 2d 3e 70 44 65 73 74 20 7c 7c 20 73  0==p->pDest || s
1fe06 65 74 44 65 73 74 50 67 73 7a 28 70 29 3d 3d 53  etDestPgsz(p)==S
1fe07 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
1fe08 20 20 20 20 20 2f 2a 20 4f 6e 65 20 28 6f 72 20       /* One (or 
1fe09 62 6f 74 68 29 20 6f 66 20 74 68 65 20 6e 61 6d  both) of the nam
1fe0a 65 64 20 64 61 74 61 62 61 73 65 73 20 64 69 64  ed databases did
1fe0b 20 6e 6f 74 20 65 78 69 73 74 20 6f 72 20 61 6e   not exist or an
1fe0c 20 4f 4f 4d 0a 20 20 20 20 20 20 2a 2a 20 65 72   OOM.      ** er
1fe0d 72 6f 72 20 77 61 73 20 68 69 74 2e 20 20 54 68  ror was hit.  Th
1fe0e 65 20 65 72 72 6f 72 20 68 61 73 20 61 6c 72 65  e error has alre
1fe0f 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e  ady been written
1fe10 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 20   into the.      
1fe11 2a 2a 20 70 44 65 73 74 44 62 20 68 61 6e 64 6c  ** pDestDb handl
1fe12 65 2e 20 20 41 6c 6c 20 74 68 61 74 20 69 73 20  e.  All that is 
1fe13 6c 65 66 74 20 74 6f 20 64 6f 20 68 65 72 65 20  left to do here 
1fe14 69 73 20 66 72 65 65 20 74 68 65 0a 20 20 20 20  is free the.    
1fe15 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63    ** sqlite3_bac
1fe16 6b 75 70 20 73 74 72 75 63 74 75 72 65 2e 0a 20  kup structure.. 
1fe17 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
1fe18 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20  lite3_free(p);. 
1fe19 20 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 20 20       p = 0;.    
1fe1a 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 29 7b  }.  }.  if( p ){
1fe1b 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e 6e 42  .    p->pSrc->nB
1fe1c 61 63 6b 75 70 2b 2b 3b 0a 20 20 7d 0a 0a 20 20  ackup++;.  }..  
1fe1d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1fe1e 61 76 65 28 70 44 65 73 74 44 62 2d 3e 6d 75 74  ave(pDestDb->mut
1fe1f 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ex);.  sqlite3_m
1fe20 75 74 65 78 5f 6c 65 61 76 65 28 70 53 72 63 44  utex_leave(pSrcD
1fe21 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
1fe22 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
1fe23 41 72 67 75 6d 65 6e 74 20 72 63 20 69 73 20 61  Argument rc is a
1fe24 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
1fe25 6f 64 65 2e 20 52 65 74 75 72 6e 20 74 72 75 65  ode. Return true
1fe26 20 69 66 20 74 68 69 73 20 65 72 72 6f 72 20 69   if this error i
1fe27 73 20 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64  s .** considered
1fe28 20 66 61 74 61 6c 20 69 66 20 65 6e 63 6f 75 6e   fatal if encoun
1fe29 74 65 72 65 64 20 64 75 72 69 6e 67 20 61 20 62  tered during a b
1fe2a 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e  ackup operation.
1fe2b 20 41 6c 6c 20 65 72 72 6f 72 73 0a 2a 2a 20 61   All errors.** a
1fe2c 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 66 61  re considered fa
1fe2d 74 61 6c 20 65 78 63 65 70 74 20 66 6f 72 20 53  tal except for S
1fe2e 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 20 53  QLITE_BUSY and S
1fe2f 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f  QLITE_LOCKED..*/
1fe30 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 46 61  .static int isFa
1fe31 74 61 6c 45 72 72 6f 72 28 69 6e 74 20 72 63 29  talError(int rc)
1fe32 7b 0a 20 20 72 65 74 75 72 6e 20 28 72 63 21 3d  {.  return (rc!=
1fe33 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21  SQLITE_OK && rc!
1fe34 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20  =SQLITE_BUSY && 
1fe35 41 4c 57 41 59 53 28 72 63 21 3d 53 51 4c 49 54  ALWAYS(rc!=SQLIT
1fe36 45 5f 4c 4f 43 4b 45 44 29 29 3b 0a 7d 0a 0a 2f  E_LOCKED));.}../
1fe37 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a  *.** Parameter z
1fe38 53 72 63 44 61 74 61 20 70 6f 69 6e 74 73 20 74  SrcData points t
1fe39 6f 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61  o a buffer conta
1fe3a 69 6e 69 6e 67 20 74 68 65 20 64 61 74 61 20 66  ining the data f
1fe3b 6f 72 20 0a 2a 2a 20 70 61 67 65 20 69 53 72 63  or .** page iSrc
1fe3c 50 67 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72  Pg from the sour
1fe3d 63 65 20 64 61 74 61 62 61 73 65 2e 20 43 6f 70  ce database. Cop
1fe3e 79 20 74 68 69 73 20 64 61 74 61 20 69 6e 74 6f  y this data into
1fe3f 20 74 68 65 20 0a 2a 2a 20 64 65 73 74 69 6e 61   the .** destina
1fe40 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 0a 2a  tion database..*
1fe41 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 63  /.static int bac
1fe42 6b 75 70 4f 6e 65 50 61 67 65 28 73 71 6c 69 74  kupOnePage(sqlit
1fe43 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 50 67  e3_backup *p, Pg
1fe44 6e 6f 20 69 53 72 63 50 67 2c 20 63 6f 6e 73 74  no iSrcPg, const
1fe45 20 75 38 20 2a 7a 53 72 63 44 61 74 61 29 7b 0a   u8 *zSrcData){.
1fe46 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20    Pager * const 
1fe47 70 44 65 73 74 50 61 67 65 72 20 3d 20 73 71 6c  pDestPager = sql
1fe48 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
1fe49 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73  ->pDest);.  cons
1fe4a 74 20 69 6e 74 20 6e 53 72 63 50 67 73 7a 20 3d  t int nSrcPgsz =
1fe4b 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
1fe4c 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53 72 63  PageSize(p->pSrc
1fe4d 29 3b 0a 20 20 69 6e 74 20 6e 44 65 73 74 50 67  );.  int nDestPg
1fe4e 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  sz = sqlite3Btre
1fe4f 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e  eGetPageSize(p->
1fe50 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73 74 20  pDest);.  const 
1fe51 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 4e 28  int nCopy = MIN(
1fe52 6e 53 72 63 50 67 73 7a 2c 20 6e 44 65 73 74 50  nSrcPgsz, nDestP
1fe53 67 73 7a 29 3b 0a 20 20 63 6f 6e 73 74 20 69 36  gsz);.  const i6
1fe54 34 20 69 45 6e 64 20 3d 20 28 69 36 34 29 69 53  4 iEnd = (i64)iS
1fe55 72 63 50 67 2a 28 69 36 34 29 6e 53 72 63 50 67  rcPg*(i64)nSrcPg
1fe56 73 7a 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  sz;.#ifdef SQLIT
1fe57 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 2f 2a  E_HAS_CODEC.  /*
1fe58 20 55 73 65 20 42 74 72 65 65 47 65 74 52 65 73   Use BtreeGetRes
1fe59 65 72 76 65 4e 6f 4d 75 74 65 78 28 29 20 66 6f  erveNoMutex() fo
1fe5a 72 20 74 68 65 20 73 6f 75 72 63 65 20 62 2d 74  r the source b-t
1fe5b 72 65 65 2c 20 61 73 20 61 6c 74 68 6f 75 67 68  ree, as although
1fe5c 20 69 74 20 69 73 0a 20 20 2a 2a 20 67 75 61 72   it is.  ** guar
1fe5d 61 6e 74 65 65 64 20 74 68 61 74 20 74 68 65 20  anteed that the 
1fe5e 73 68 61 72 65 64 2d 6d 75 74 65 78 20 69 73 20  shared-mutex is 
1fe5f 68 65 6c 64 20 62 79 20 74 68 69 73 20 74 68 72  held by this thr
1fe60 65 61 64 2c 20 68 61 6e 64 6c 65 0a 20 20 2a 2a  ead, handle.  **
1fe61 20 70 2d 3e 70 53 72 63 20 6d 61 79 20 6e 6f 74   p->pSrc may not
1fe62 20 61 63 74 75 61 6c 6c 79 20 62 65 20 74 68 65   actually be the
1fe63 20 6f 77 6e 65 72 2e 20 20 2a 2f 0a 20 20 69 6e   owner.  */.  in
1fe64 74 20 6e 53 72 63 52 65 73 65 72 76 65 20 3d 20  t nSrcReserve = 
1fe65 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52  sqlite3BtreeGetR
1fe66 65 73 65 72 76 65 4e 6f 4d 75 74 65 78 28 70 2d  eserveNoMutex(p-
1fe67 3e 70 53 72 63 29 3b 0a 20 20 69 6e 74 20 6e 44  >pSrc);.  int nD
1fe68 65 73 74 52 65 73 65 72 76 65 20 3d 20 73 71 6c  estReserve = sql
1fe69 69 74 65 33 42 74 72 65 65 47 65 74 52 65 73 65  ite3BtreeGetRese
1fe6a 72 76 65 28 70 2d 3e 70 44 65 73 74 29 3b 0a 23  rve(p->pDest);.#
1fe6b 65 6e 64 69 66 0a 20 20 69 6e 74 20 72 63 20 3d  endif.  int rc =
1fe6c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 36   SQLITE_OK;.  i6
1fe6d 34 20 69 4f 66 66 3b 0a 0a 20 20 61 73 73 65 72  4 iOff;..  asser
1fe6e 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  t( sqlite3BtreeG
1fe6f 65 74 52 65 73 65 72 76 65 4e 6f 4d 75 74 65 78  etReserveNoMutex
1fe70 28 70 2d 3e 70 53 72 63 29 3e 3d 30 20 29 3b 0a  (p->pSrc)>=0 );.
1fe71 20 20 61 73 73 65 72 74 28 20 70 2d 3e 62 44 65    assert( p->bDe
1fe72 73 74 4c 6f 63 6b 65 64 20 29 3b 0a 20 20 61 73  stLocked );.  as
1fe73 73 65 72 74 28 20 21 69 73 46 61 74 61 6c 45 72  sert( !isFatalEr
1fe74 72 6f 72 28 70 2d 3e 72 63 29 20 29 3b 0a 20 20  ror(p->rc) );.  
1fe75 61 73 73 65 72 74 28 20 69 53 72 63 50 67 21 3d  assert( iSrcPg!=
1fe76 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
1fe77 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20  E(p->pSrc->pBt) 
1fe78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53 72  );.  assert( zSr
1fe79 63 44 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 43  cData );..  /* C
1fe7a 61 74 63 68 20 74 68 65 20 63 61 73 65 20 77 68  atch the case wh
1fe7b 65 72 65 20 74 68 65 20 64 65 73 74 69 6e 61 74  ere the destinat
1fe7c 69 6f 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d  ion is an in-mem
1fe7d 6f 72 79 20 64 61 74 61 62 61 73 65 20 61 6e 64  ory database and
1fe7e 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 73   the.  ** page s
1fe7f 69 7a 65 73 20 6f 66 20 74 68 65 20 73 6f 75 72  izes of the sour
1fe80 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69  ce and destinati
1fe81 6f 6e 20 64 69 66 66 65 72 2e 20 0a 20 20 2a 2f  on differ. .  */
1fe82 0a 20 20 69 66 28 20 6e 53 72 63 50 67 73 7a 21  .  if( nSrcPgsz!
1fe83 3d 6e 44 65 73 74 50 67 73 7a 20 26 26 20 73 71  =nDestPgsz && sq
1fe84 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64  lite3PagerIsMemd
1fe85 62 28 70 44 65 73 74 50 61 67 65 72 29 20 29 7b  b(pDestPager) ){
1fe86 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1fe87 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a  _READONLY;.  }..
1fe88 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41  #ifdef SQLITE_HA
1fe89 53 5f 43 4f 44 45 43 0a 20 20 2f 2a 20 42 61 63  S_CODEC.  /* Bac
1fe8a 6b 75 70 20 69 73 20 6e 6f 74 20 70 6f 73 73 69  kup is not possi
1fe8b 62 6c 65 20 69 66 20 74 68 65 20 70 61 67 65 20  ble if the page 
1fe8c 73 69 7a 65 20 6f 66 20 74 68 65 20 64 65 73 74  size of the dest
1fe8d 69 6e 61 74 69 6f 6e 20 69 73 20 63 68 61 6e 67  ination is chang
1fe8e 69 6e 67 0a 20 20 2a 2a 20 61 6e 64 20 61 20 63  ing.  ** and a c
1fe8f 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65 2e 0a  odec is in use..
1fe90 20 20 2a 2f 0a 20 20 69 66 28 20 6e 53 72 63 50    */.  if( nSrcP
1fe91 67 73 7a 21 3d 6e 44 65 73 74 50 67 73 7a 20 26  gsz!=nDestPgsz &
1fe92 26 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  & sqlite3PagerGe
1fe93 74 43 6f 64 65 63 28 70 44 65 73 74 50 61 67 65  tCodec(pDestPage
1fe94 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 63 20  r)!=0 ){.    rc 
1fe95 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  = SQLITE_READONL
1fe96 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 61 63  Y;.  }..  /* Bac
1fe97 6b 75 70 20 69 73 20 6e 6f 74 20 70 6f 73 73 69  kup is not possi
1fe98 62 6c 65 20 69 66 20 74 68 65 20 6e 75 6d 62 65  ble if the numbe
1fe99 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 72 65  r of bytes of re
1fe9a 73 65 72 76 65 20 73 70 61 63 65 20 64 69 66 66  serve space diff
1fe9b 65 72 0a 20 20 2a 2a 20 62 65 74 77 65 65 6e 20  er.  ** between 
1fe9c 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69  source and desti
1fe9d 6e 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 72  nation.  If ther
1fe9e 65 20 69 73 20 61 20 64 69 66 66 65 72 65 6e 63  e is a differenc
1fe9f 65 2c 20 74 72 79 20 74 6f 0a 20 20 2a 2a 20 66  e, try to.  ** f
1fea0 69 78 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  ix the destinati
1fea1 6f 6e 20 74 6f 20 61 67 72 65 65 20 77 69 74 68  on to agree with
1fea2 20 74 68 65 20 73 6f 75 72 63 65 2e 20 20 49 66   the source.  If
1fea3 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 6f 73   that is not pos
1fea4 73 69 62 6c 65 2c 0a 20 20 2a 2a 20 74 68 65 6e  sible,.  ** then
1fea5 20 74 68 65 20 62 61 63 6b 75 70 20 63 61 6e 6e   the backup cann
1fea6 6f 74 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f  ot proceed..  */
1fea7 0a 20 20 69 66 28 20 6e 53 72 63 52 65 73 65 72  .  if( nSrcReser
1fea8 76 65 21 3d 6e 44 65 73 74 52 65 73 65 72 76 65  ve!=nDestReserve
1fea9 20 29 7b 0a 20 20 20 20 75 33 32 20 6e 65 77 50   ){.    u32 newP
1feaa 67 73 7a 20 3d 20 6e 53 72 63 50 67 73 7a 3b 0a  gsz = nSrcPgsz;.
1feab 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1feac 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
1fead 28 70 44 65 73 74 50 61 67 65 72 2c 20 26 6e 65  (pDestPager, &ne
1feae 77 50 67 73 7a 2c 20 6e 53 72 63 52 65 73 65 72  wPgsz, nSrcReser
1feaf 76 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  ve);.    if( rc=
1feb0 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 65  =SQLITE_OK && ne
1feb1 77 50 67 73 7a 21 3d 6e 53 72 63 50 67 73 7a 20  wPgsz!=nSrcPgsz 
1feb2 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45  ) rc = SQLITE_RE
1feb3 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 23 65 6e 64  ADONLY;.  }.#end
1feb4 69 66 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f  if..  /* This lo
1feb5 6f 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72  op runs once for
1feb6 20 65 61 63 68 20 64 65 73 74 69 6e 61 74 69 6f   each destinatio
1feb7 6e 20 70 61 67 65 20 73 70 61 6e 6e 65 64 20 62  n page spanned b
1feb8 79 20 74 68 65 20 73 6f 75 72 63 65 20 0a 20 20  y the source .  
1feb9 2a 2a 20 70 61 67 65 2e 20 46 6f 72 20 65 61 63  ** page. For eac
1feba 68 20 69 74 65 72 61 74 69 6f 6e 2c 20 76 61 72  h iteration, var
1febb 69 61 62 6c 65 20 69 4f 66 66 20 69 73 20 73 65  iable iOff is se
1febc 74 20 74 6f 20 74 68 65 20 62 79 74 65 20 6f 66  t to the byte of
1febd 66 73 65 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65  fset.  ** of the
1febe 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67   destination pag
1febf 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 4f  e..  */.  for(iO
1fec0 66 66 3d 69 45 6e 64 2d 28 69 36 34 29 6e 53 72  ff=iEnd-(i64)nSr
1fec1 63 50 67 73 7a 3b 20 72 63 3d 3d 53 51 4c 49 54  cPgsz; rc==SQLIT
1fec2 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69 45 6e  E_OK && iOff<iEn
1fec3 64 3b 20 69 4f 66 66 2b 3d 6e 44 65 73 74 50 67  d; iOff+=nDestPg
1fec4 73 7a 29 7b 0a 20 20 20 20 44 62 50 61 67 65 20  sz){.    DbPage 
1fec5 2a 70 44 65 73 74 50 67 20 3d 20 30 3b 0a 20 20  *pDestPg = 0;.  
1fec6 20 20 50 67 6e 6f 20 69 44 65 73 74 20 3d 20 28    Pgno iDest = (
1fec7 50 67 6e 6f 29 28 69 4f 66 66 2f 6e 44 65 73 74  Pgno)(iOff/nDest
1fec8 50 67 73 7a 29 2b 31 3b 0a 20 20 20 20 69 66 28  Pgsz)+1;.    if(
1fec9 20 69 44 65 73 74 3d 3d 50 45 4e 44 49 4e 47 5f   iDest==PENDING_
1feca 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65  BYTE_PAGE(p->pDe
1fecb 73 74 2d 3e 70 42 74 29 20 29 20 63 6f 6e 74 69  st->pBt) ) conti
1fecc 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 53 51 4c  nue;.    if( SQL
1fecd 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71  ITE_OK==(rc = sq
1fece 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 44  lite3PagerGet(pD
1fecf 65 73 74 50 61 67 65 72 2c 20 69 44 65 73 74 2c  estPager, iDest,
1fed0 20 26 70 44 65 73 74 50 67 29 29 0a 20 20 20 20   &pDestPg)).    
1fed1 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28   && SQLITE_OK==(
1fed2 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1fed3 72 57 72 69 74 65 28 70 44 65 73 74 50 67 29 29  rWrite(pDestPg))
1fed4 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 63 6f  .    ){.      co
1fed5 6e 73 74 20 75 38 20 2a 7a 49 6e 20 3d 20 26 7a  nst u8 *zIn = &z
1fed6 53 72 63 44 61 74 61 5b 69 4f 66 66 25 6e 53 72  SrcData[iOff%nSr
1fed7 63 50 67 73 7a 5d 3b 0a 20 20 20 20 20 20 75 38  cPgsz];.      u8
1fed8 20 2a 7a 44 65 73 74 44 61 74 61 20 3d 20 73 71   *zDestData = sq
1fed9 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
1feda 61 28 70 44 65 73 74 50 67 29 3b 0a 20 20 20 20  a(pDestPg);.    
1fedb 20 20 75 38 20 2a 7a 4f 75 74 20 3d 20 26 7a 44    u8 *zOut = &zD
1fedc 65 73 74 44 61 74 61 5b 69 4f 66 66 25 6e 44 65  estData[iOff%nDe
1fedd 73 74 50 67 73 7a 5d 3b 0a 0a 20 20 20 20 20 20  stPgsz];..      
1fede 2f 2a 20 43 6f 70 79 20 74 68 65 20 64 61 74 61  /* Copy the data
1fedf 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65   from the source
1fee0 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 64   page into the d
1fee1 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 2e  estination page.
1fee2 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 6e 20 63  .      ** Then c
1fee3 6c 65 61 72 20 74 68 65 20 42 74 72 65 65 20 6c  lear the Btree l
1fee4 61 79 65 72 20 4d 65 6d 50 61 67 65 2e 69 73 49  ayer MemPage.isI
1fee5 6e 69 74 20 66 6c 61 67 2e 20 42 6f 74 68 20 74  nit flag. Both t
1fee6 68 69 73 20 6d 6f 64 75 6c 65 0a 20 20 20 20 20  his module.     
1fee7 20 2a 2a 20 61 6e 64 20 74 68 65 20 70 61 67 65   ** and the page
1fee8 72 20 63 6f 64 65 20 75 73 65 20 74 68 69 73 20  r code use this 
1fee9 74 72 69 63 6b 20 28 63 6c 65 61 72 69 6e 67 20  trick (clearing 
1feea 74 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 20  the first byte. 
1feeb 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70       ** of the p
1feec 61 67 65 20 27 65 78 74 72 61 27 20 73 70 61 63  age 'extra' spac
1feed 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20  e to invalidate 
1feee 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 72 73  the Btree layers
1feef 0a 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 64  .      ** cached
1fef0 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 70 61   parse of the pa
1fef1 67 65 29 2e 20 4d 65 6d 50 61 67 65 2e 69 73 49  ge). MemPage.isI
1fef2 6e 69 74 20 69 73 20 6d 61 72 6b 65 64 20 0a 20  nit is marked . 
1fef3 20 20 20 20 20 2a 2a 20 22 4d 55 53 54 20 42 45       ** "MUST BE
1fef4 20 46 49 52 53 54 22 20 66 6f 72 20 74 68 69 73   FIRST" for this
1fef5 20 70 75 72 70 6f 73 65 2e 0a 20 20 20 20 20 20   purpose..      
1fef6 2a 2f 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  */.      memcpy(
1fef7 7a 4f 75 74 2c 20 7a 49 6e 2c 20 6e 43 6f 70 79  zOut, zIn, nCopy
1fef8 29 3b 0a 20 20 20 20 20 20 28 28 75 38 20 2a 29  );.      ((u8 *)
1fef9 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45  sqlite3PagerGetE
1fefa 78 74 72 61 28 70 44 65 73 74 50 67 29 29 5b 30  xtra(pDestPg))[0
1fefb 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  ] = 0;.    }.   
1fefc 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
1fefd 65 66 28 70 44 65 73 74 50 67 29 3b 0a 20 20 7d  ef(pDestPg);.  }
1fefe 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
1feff 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 6c 65  ../*.** If pFile
1ff00 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 6c 61   is currently la
1ff01 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65 20  rger than iSize 
1ff02 62 79 74 65 73 2c 20 74 68 65 6e 20 74 72 75 6e  bytes, then trun
1ff03 63 61 74 65 20 69 74 20 74 6f 0a 2a 2a 20 65 78  cate it to.** ex
1ff04 61 63 74 6c 79 20 69 53 69 7a 65 20 62 79 74 65  actly iSize byte
1ff05 73 2e 20 49 66 20 70 46 69 6c 65 20 69 73 20 6e  s. If pFile is n
1ff06 6f 74 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69  ot larger than i
1ff07 53 69 7a 65 20 62 79 74 65 73 2c 20 74 68 65 6e  Size bytes, then
1ff08 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f  .** this functio
1ff09 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  n is a no-op..**
1ff0a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
1ff0b 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69  E_OK if everythi
1ff0c 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  ng is successful
1ff0d 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65  , or an SQLite e
1ff0e 72 72 6f 72 20 0a 2a 2a 20 63 6f 64 65 20 69 66  rror .** code if
1ff0f 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
1ff10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1ff11 62 61 63 6b 75 70 54 72 75 6e 63 61 74 65 46 69  backupTruncateFi
1ff12 6c 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  le(sqlite3_file 
1ff13 2a 70 46 69 6c 65 2c 20 69 36 34 20 69 53 69 7a  *pFile, i64 iSiz
1ff14 65 29 7b 0a 20 20 69 36 34 20 69 43 75 72 72 65  e){.  i64 iCurre
1ff15 6e 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  nt;.  int rc = s
1ff16 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65  qlite3OsFileSize
1ff17 28 70 46 69 6c 65 2c 20 26 69 43 75 72 72 65 6e  (pFile, &iCurren
1ff18 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  t);.  if( rc==SQ
1ff19 4c 49 54 45 5f 4f 4b 20 26 26 20 69 43 75 72 72  LITE_OK && iCurr
1ff1a 65 6e 74 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20  ent>iSize ){.   
1ff1b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
1ff1c 72 75 6e 63 61 74 65 28 70 46 69 6c 65 2c 20 69  runcate(pFile, i
1ff1d 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Size);.  }.  ret
1ff1e 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1ff1f 20 52 65 67 69 73 74 65 72 20 74 68 69 73 20 62   Register this b
1ff20 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 77 69 74  ackup object wit
1ff21 68 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  h the associated
1ff22 20 73 6f 75 72 63 65 20 70 61 67 65 72 20 66 6f   source pager fo
1ff23 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 73 20 77  r.** callbacks w
1ff24 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 63 68  hen pages are ch
1ff25 61 6e 67 65 64 20 6f 72 20 74 68 65 20 63 61 63  anged or the cac
1ff26 68 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a  he invalidated..
1ff27 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
1ff28 74 74 61 63 68 42 61 63 6b 75 70 4f 62 6a 65 63  ttachBackupObjec
1ff29 74 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  t(sqlite3_backup
1ff2a 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f   *p){.  sqlite3_
1ff2b 62 61 63 6b 75 70 20 2a 2a 70 70 3b 0a 20 20 61  backup **pp;.  a
1ff2c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
1ff2d 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 2d  reeHoldsMutex(p-
1ff2e 3e 70 53 72 63 29 20 29 3b 0a 20 20 70 70 20 3d  >pSrc) );.  pp =
1ff2f 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63   sqlite3PagerBac
1ff30 6b 75 70 50 74 72 28 73 71 6c 69 74 65 33 42 74  kupPtr(sqlite3Bt
1ff31 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53 72 63  reePager(p->pSrc
1ff32 29 29 3b 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d  ));.  p->pNext =
1ff33 20 2a 70 70 3b 0a 20 20 2a 70 70 20 3d 20 70 3b   *pp;.  *pp = p;
1ff34 0a 20 20 70 2d 3e 69 73 41 74 74 61 63 68 65 64  .  p->isAttached
1ff35 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   = 1;.}../*.** C
1ff36 6f 70 79 20 6e 50 61 67 65 20 70 61 67 65 73 20  opy nPage pages 
1ff37 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20  from the source 
1ff38 62 2d 74 72 65 65 20 74 6f 20 74 68 65 20 64 65  b-tree to the de
1ff39 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  stination..*/.SQ
1ff3a 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1ff3b 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
1ff3c 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
1ff3d 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a  *p, int nPage){.
1ff3e 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20    int rc;.  int 
1ff3f 64 65 73 74 4d 6f 64 65 3b 20 20 20 20 20 20 20  destMode;       
1ff40 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 6a  /* Destination j
1ff41 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 2a 2f 0a 20  ournal mode */. 
1ff42 20 69 6e 74 20 70 67 73 7a 53 72 63 20 3d 20 30   int pgszSrc = 0
1ff43 3b 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70  ;    /* Source p
1ff44 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e  age size */.  in
1ff45 74 20 70 67 73 7a 44 65 73 74 20 3d 20 30 3b 20  t pgszDest = 0; 
1ff46 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e    /* Destination
1ff47 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20   page size */.. 
1ff48 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1ff49 6e 74 65 72 28 70 2d 3e 70 53 72 63 44 62 2d 3e  nter(p->pSrcDb->
1ff4a 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  mutex);.  sqlite
1ff4b 33 42 74 72 65 65 45 6e 74 65 72 28 70 2d 3e 70  3BtreeEnter(p->p
1ff4c 53 72 63 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  Src);.  if( p->p
1ff4d 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71  DestDb ){.    sq
1ff4e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1ff4f 72 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75  r(p->pDestDb->mu
1ff50 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  tex);.  }..  rc 
1ff51 3d 20 70 2d 3e 72 63 3b 0a 20 20 69 66 28 20 21  = p->rc;.  if( !
1ff52 69 73 46 61 74 61 6c 45 72 72 6f 72 28 72 63 29  isFatalError(rc)
1ff53 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 20   ){.    Pager * 
1ff54 63 6f 6e 73 74 20 70 53 72 63 50 61 67 65 72 20  const pSrcPager 
1ff55 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  = sqlite3BtreePa
1ff56 67 65 72 28 70 2d 3e 70 53 72 63 29 3b 20 20 20  ger(p->pSrc);   
1ff57 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 65    /* Source page
1ff58 72 20 2a 2f 0a 20 20 20 20 50 61 67 65 72 20 2a  r */.    Pager *
1ff59 20 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65   const pDestPage
1ff5a 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  r = sqlite3Btree
1ff5b 50 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b  Pager(p->pDest);
1ff5c 20 20 20 2f 2a 20 44 65 73 74 20 70 61 67 65 72     /* Dest pager
1ff5d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20   */.    int ii; 
1ff5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff5f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
1ff60 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20  erator variable 
1ff61 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 72 63 50  */.    int nSrcP
1ff62 61 67 65 20 3d 20 2d 31 3b 20 20 20 20 20 20 20  age = -1;       
1ff63 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
1ff64 65 20 6f 66 20 73 6f 75 72 63 65 20 64 62 20 69  e of source db i
1ff65 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 69  n pages */.    i
1ff66 6e 74 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 3d  nt bCloseTrans =
1ff67 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1ff68 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 72 63    /* True if src
1ff69 20 64 62 20 72 65 71 75 69 72 65 73 20 75 6e 6c   db requires unl
1ff6a 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 2f  ocking */..    /
1ff6b 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20  * If the source 
1ff6c 70 61 67 65 72 20 69 73 20 63 75 72 72 65 6e 74  pager is current
1ff6d 6c 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72  ly in a write-tr
1ff6e 61 6e 73 61 63 74 69 6f 6e 2c 20 72 65 74 75 72  ansaction, retur
1ff6f 6e 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f  n.    ** SQLITE_
1ff70 42 55 53 59 20 69 6d 6d 65 64 69 61 74 65 6c 79  BUSY immediately
1ff71 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1ff72 20 70 2d 3e 70 44 65 73 74 44 62 20 26 26 20 70   p->pDestDb && p
1ff73 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e 69 6e 54  ->pSrc->pBt->inT
1ff74 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e  ransaction==TRAN
1ff75 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20  S_WRITE ){.     
1ff76 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
1ff77 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  Y;.    }else{.  
1ff78 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1ff79 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  OK;.    }..    /
1ff7a 2a 20 4c 6f 63 6b 20 74 68 65 20 64 65 73 74 69  * Lock the desti
1ff7b 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2c  nation database,
1ff7c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 6c 6f   if it is not lo
1ff7d 63 6b 65 64 20 61 6c 72 65 61 64 79 2e 20 2a 2f  cked already. */
1ff7e 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
1ff7f 4f 4b 3d 3d 72 63 20 26 26 20 70 2d 3e 62 44 65  OK==rc && p->bDe
1ff80 73 74 4c 6f 63 6b 65 64 3d 3d 30 0a 20 20 20 20  stLocked==0.    
1ff81 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28   && SQLITE_OK==(
1ff82 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1ff83 65 42 65 67 69 6e 54 72 61 6e 73 28 70 2d 3e 70  eBeginTrans(p->p
1ff84 44 65 73 74 2c 20 32 29 29 20 0a 20 20 20 20 29  Dest, 2)) .    )
1ff85 7b 0a 20 20 20 20 20 20 70 2d 3e 62 44 65 73 74  {.      p->bDest
1ff86 4c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20  Locked = 1;.    
1ff87 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65    sqlite3BtreeGe
1ff88 74 4d 65 74 61 28 70 2d 3e 70 44 65 73 74 2c 20  tMeta(p->pDest, 
1ff89 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52  BTREE_SCHEMA_VER
1ff8a 53 49 4f 4e 2c 20 26 70 2d 3e 69 44 65 73 74 53  SION, &p->iDestS
1ff8b 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 0a 20  chema);.    }.. 
1ff8c 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69     /* If there i
1ff8d 73 20 6e 6f 20 6f 70 65 6e 20 72 65 61 64 2d 74  s no open read-t
1ff8e 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68  ransaction on th
1ff8f 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
1ff90 65 2c 20 6f 70 65 6e 0a 20 20 20 20 2a 2a 20 6f  e, open.    ** o
1ff91 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 74 72 61  ne now. If a tra
1ff92 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e  nsaction is open
1ff93 65 64 20 68 65 72 65 2c 20 74 68 65 6e 20 69 74  ed here, then it
1ff94 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64 0a   will be closed.
1ff95 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 68      ** before th
1ff96 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 74  is function exit
1ff97 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  s..    */.    if
1ff98 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1ff99 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 74 72  && 0==sqlite3Btr
1ff9a 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28  eeIsInReadTrans(
1ff9b 70 2d 3e 70 53 72 63 29 20 29 7b 0a 20 20 20 20  p->pSrc) ){.    
1ff9c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1ff9d 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 2d  reeBeginTrans(p-
1ff9e 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 20  >pSrc, 0);.     
1ff9f 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 3d 20 31   bCloseTrans = 1
1ffa0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1ffa1 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 62 61 63  Do not allow bac
1ffa2 6b 75 70 20 69 66 20 74 68 65 20 64 65 73 74 69  kup if the desti
1ffa3 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
1ffa4 69 73 20 69 6e 20 57 41 4c 20 6d 6f 64 65 0a 20  is in WAL mode. 
1ffa5 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 70 61     ** and the pa
1ffa6 67 65 20 73 69 7a 65 73 20 61 72 65 20 64 69 66  ge sizes are dif
1ffa7 66 65 72 65 6e 74 20 62 65 74 77 65 65 6e 20 73  ferent between s
1ffa8 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e  ource and destin
1ffa9 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 70 67 73  ation */.    pgs
1ffaa 7a 53 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74  zSrc = sqlite3Bt
1ffab 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
1ffac 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 70 67 73  ->pSrc);.    pgs
1ffad 7a 44 65 73 74 20 3d 20 73 71 6c 69 74 65 33 42  zDest = sqlite3B
1ffae 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28  treeGetPageSize(
1ffaf 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 20 20 64  p->pDest);.    d
1ffb0 65 73 74 4d 6f 64 65 20 3d 20 73 71 6c 69 74 65  estMode = sqlite
1ffb1 33 50 61 67 65 72 47 65 74 4a 6f 75 72 6e 61 6c  3PagerGetJournal
1ffb2 4d 6f 64 65 28 73 71 6c 69 74 65 33 42 74 72 65  Mode(sqlite3Btre
1ffb3 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29  ePager(p->pDest)
1ffb4 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  );.    if( SQLIT
1ffb5 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 64 65 73 74  E_OK==rc && dest
1ffb6 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
1ffb7 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 26 26 20 70  NALMODE_WAL && p
1ffb8 67 73 7a 53 72 63 21 3d 70 67 73 7a 44 65 73 74  gszSrc!=pgszDest
1ffb9 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
1ffba 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a  QLITE_READONLY;.
1ffbb 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
1ffbc 4e 6f 77 20 74 68 61 74 20 74 68 65 72 65 20 69  Now that there i
1ffbd 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e  s a read-lock on
1ffbe 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
1ffbf 62 61 73 65 2c 20 71 75 65 72 79 20 74 68 65 0a  base, query the.
1ffc0 20 20 20 20 2a 2a 20 73 6f 75 72 63 65 20 70 61      ** source pa
1ffc1 67 65 72 20 66 6f 72 20 74 68 65 20 6e 75 6d 62  ger for the numb
1ffc2 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
1ffc3 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20  he database..   
1ffc4 20 2a 2f 0a 20 20 20 20 6e 53 72 63 50 61 67 65   */.    nSrcPage
1ffc5 20 3d 20 28 69 6e 74 29 73 71 6c 69 74 65 33 42   = (int)sqlite3B
1ffc6 74 72 65 65 4c 61 73 74 50 61 67 65 28 70 2d 3e  treeLastPage(p->
1ffc7 70 53 72 63 29 3b 0a 20 20 20 20 61 73 73 65 72  pSrc);.    asser
1ffc8 74 28 20 6e 53 72 63 50 61 67 65 3e 3d 30 20 29  t( nSrcPage>=0 )
1ffc9 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20  ;.    for(ii=0; 
1ffca 28 6e 50 61 67 65 3c 30 20 7c 7c 20 69 69 3c 6e  (nPage<0 || ii<n
1ffcb 50 61 67 65 29 20 26 26 20 70 2d 3e 69 4e 65 78  Page) && p->iNex
1ffcc 74 3c 3d 28 50 67 6e 6f 29 6e 53 72 63 50 61 67  t<=(Pgno)nSrcPag
1ffcd 65 20 26 26 20 21 72 63 3b 20 69 69 2b 2b 29 7b  e && !rc; ii++){
1ffce 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e  .      const Pgn
1ffcf 6f 20 69 53 72 63 50 67 20 3d 20 70 2d 3e 69 4e  o iSrcPg = p->iN
1ffd0 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ext;            
1ffd1 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70       /* Source p
1ffd2 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  age number */.  
1ffd3 20 20 20 20 69 66 28 20 69 53 72 63 50 67 21 3d      if( iSrcPg!=
1ffd4 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
1ffd5 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20  E(p->pSrc->pBt) 
1ffd6 29 7b 0a 20 20 20 20 20 20 20 20 44 62 50 61 67  ){.        DbPag
1ffd7 65 20 2a 70 53 72 63 50 67 3b 20 20 20 20 20 20  e *pSrcPg;      
1ffd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ffd9 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65         /* Source
1ffda 20 70 61 67 65 20 6f 62 6a 65 63 74 20 2a 2f 0a   page object */.
1ffdb 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1ffdc 69 74 65 33 50 61 67 65 72 47 65 74 28 70 53 72  ite3PagerGet(pSr
1ffdd 63 50 61 67 65 72 2c 20 69 53 72 63 50 67 2c 20  cPager, iSrcPg, 
1ffde 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20  &pSrcPg);.      
1ffdf 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1ffe0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
1ffe1 20 72 63 20 3d 20 62 61 63 6b 75 70 4f 6e 65 50   rc = backupOneP
1ffe2 61 67 65 28 70 2c 20 69 53 72 63 50 67 2c 20 73  age(p, iSrcPg, s
1ffe3 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
1ffe4 74 61 28 70 53 72 63 50 67 29 29 3b 0a 20 20 20  ta(pSrcPg));.   
1ffe5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
1ffe6 67 65 72 55 6e 72 65 66 28 70 53 72 63 50 67 29  gerUnref(pSrcPg)
1ffe7 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1ffe8 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 69 4e 65    }.      p->iNe
1ffe9 78 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  xt++;.    }.    
1ffea 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1ffeb 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 50  K ){.      p->nP
1ffec 61 67 65 63 6f 75 6e 74 20 3d 20 6e 53 72 63 50  agecount = nSrcP
1ffed 61 67 65 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52  age;.      p->nR
1ffee 65 6d 61 69 6e 69 6e 67 20 3d 20 6e 53 72 63 50  emaining = nSrcP
1ffef 61 67 65 2b 31 2d 70 2d 3e 69 4e 65 78 74 3b 0a  age+1-p->iNext;.
1fff0 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 4e 65        if( p->iNe
1fff1 78 74 3e 28 50 67 6e 6f 29 6e 53 72 63 50 61 67  xt>(Pgno)nSrcPag
1fff2 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  e ){.        rc 
1fff3 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  = SQLITE_DONE;. 
1fff4 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21       }else if( !
1fff5 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 29 7b  p->isAttached ){
1fff6 0a 20 20 20 20 20 20 20 20 61 74 74 61 63 68 42  .        attachB
1fff7 61 63 6b 75 70 4f 62 6a 65 63 74 28 70 29 3b 0a  ackupObject(p);.
1fff8 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1fff9 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74  .    /* Update t
1fffa 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f  he schema versio
1fffb 6e 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 64  n field in the d
1fffc 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
1fffd 61 73 65 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a  ase. This.    **
1fffe 20 69 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65   is to make sure
1ffff 20 74 68 61 74 20 74 68 65 20 73 63 68 65 6d 61   that the schema
20000 2d 76 65 72 73 69 6f 6e 20 72 65 61 6c 6c 79 20  -version really 
20001 64 6f 65 73 20 63 68 61 6e 67 65 20 69 6e 0a 20  does change in. 
20002 20 20 20 2a 2a 20 74 68 65 20 63 61 73 65 20 77     ** the case w
20003 68 65 72 65 20 74 68 65 20 73 6f 75 72 63 65 20  here the source 
20004 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  and destination 
20005 64 61 74 61 62 61 73 65 73 20 68 61 76 65 20 74  databases have t
20006 68 65 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 73  he.    ** same s
20007 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 0a 20  chema version.. 
20008 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63     */.    if( rc
20009 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
2000a 0a 20 20 20 20 20 20 69 66 28 20 6e 53 72 63 50  .      if( nSrcP
2000b 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  age==0 ){.      
2000c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
2000d 72 65 65 4e 65 77 44 62 28 70 2d 3e 70 44 65 73  reeNewDb(p->pDes
2000e 74 29 3b 0a 20 20 20 20 20 20 20 20 6e 53 72 63  t);.        nSrc
2000f 50 61 67 65 20 3d 20 31 3b 0a 20 20 20 20 20 20  Page = 1;.      
20010 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  }.      if( rc==
20011 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d  SQLITE_OK || rc=
20012 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
20013 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
20014 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d  ite3BtreeUpdateM
20015 65 74 61 28 70 2d 3e 70 44 65 73 74 2c 31 2c 70  eta(p->pDest,1,p
20016 2d 3e 69 44 65 73 74 53 63 68 65 6d 61 2b 31 29  ->iDestSchema+1)
20017 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
20018 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
20019 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  K ){.        if(
2001a 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20   p->pDestDb ){. 
2001b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2001c 52 65 73 65 74 41 6c 6c 53 63 68 65 6d 61 73 4f  ResetAllSchemasO
2001d 66 43 6f 6e 6e 65 63 74 69 6f 6e 28 70 2d 3e 70  fConnection(p->p
2001e 44 65 73 74 44 62 29 3b 0a 20 20 20 20 20 20 20  DestDb);.       
2001f 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 64   }.        if( d
20020 65 73 74 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  estMode==PAGER_J
20021 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 29  OURNALMODE_WAL )
20022 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
20023 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
20024 56 65 72 73 69 6f 6e 28 70 2d 3e 70 44 65 73 74  Version(p->pDest
20025 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  , 2);.        }.
20026 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
20027 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
20028 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  ){.        int n
20029 44 65 73 74 54 72 75 6e 63 61 74 65 3b 0a 20 20  DestTruncate;.  
2002a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6e 44 65        /* Set nDe
2002b 73 74 54 72 75 6e 63 61 74 65 20 74 6f 20 74 68  stTruncate to th
2002c 65 20 66 69 6e 61 6c 20 6e 75 6d 62 65 72 20 6f  e final number o
2002d 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  f pages in the d
2002e 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20  estination.     
2002f 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20     ** database. 
20030 54 68 65 20 63 6f 6d 70 6c 69 63 61 74 69 6f 6e  The complication
20031 20 68 65 72 65 20 69 73 20 74 68 61 74 20 74 68   here is that th
20032 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61  e destination pa
20033 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 69  ge.        ** si
20034 7a 65 20 6d 61 79 20 62 65 20 64 69 66 66 65 72  ze may be differ
20035 65 6e 74 20 74 6f 20 74 68 65 20 73 6f 75 72 63  ent to the sourc
20036 65 20 70 61 67 65 20 73 69 7a 65 2e 20 0a 20 20  e page size. .  
20037 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
20038 20 2a 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63   ** If the sourc
20039 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 73  e page size is s
2003a 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20  maller than the 
2003b 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65  destination page
2003c 20 73 69 7a 65 2c 20 0a 20 20 20 20 20 20 20 20   size, .        
2003d 2a 2a 20 72 6f 75 6e 64 20 75 70 2e 20 49 6e 20  ** round up. In 
2003e 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61  this case the ca
2003f 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 54  ll to sqlite3OsT
20040 72 75 6e 63 61 74 65 28 29 20 62 65 6c 6f 77 20  runcate() below 
20041 77 69 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20  will.        ** 
20042 66 69 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20  fix the size of 
20043 74 68 65 20 66 69 6c 65 2e 20 48 6f 77 65 76 65  the file. Howeve
20044 72 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e  r it is importan
20045 74 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20 20 20  t to call.      
20046 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65    ** sqlite3Page
20047 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29  rTruncateImage()
20048 20 68 65 72 65 20 73 6f 20 74 68 61 74 20 61 6e   here so that an
20049 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 0a  y pages in the .
2004a 20 20 20 20 20 20 20 20 2a 2a 20 64 65 73 74 69          ** desti
2004b 6e 61 74 69 6f 6e 20 66 69 6c 65 20 74 68 61 74  nation file that
2004c 20 6c 69 65 20 62 65 79 6f 6e 64 20 74 68 65 20   lie beyond the 
2004d 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 70 61  nDestTruncate pa
2004e 67 65 20 6d 61 72 6b 20 61 72 65 0a 20 20 20 20  ge mark are.    
2004f 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65      ** journalle
20050 64 20 62 79 20 50 61 67 65 72 43 6f 6d 6d 69 74  d by PagerCommit
20051 50 68 61 73 65 4f 6e 65 28 29 20 62 65 66 6f 72  PhaseOne() befor
20052 65 20 74 68 65 79 20 61 72 65 20 64 65 73 74 72  e they are destr
20053 6f 79 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  oyed.        ** 
20054 62 79 20 74 68 65 20 66 69 6c 65 20 74 72 75 6e  by the file trun
20055 63 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20  cation..        
20056 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  */.        asser
20057 74 28 20 70 67 73 7a 53 72 63 3d 3d 73 71 6c 69  t( pgszSrc==sqli
20058 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53  te3BtreeGetPageS
20059 69 7a 65 28 70 2d 3e 70 53 72 63 29 20 29 3b 0a  ize(p->pSrc) );.
2005a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2005b 70 67 73 7a 44 65 73 74 3d 3d 73 71 6c 69 74 65  pgszDest==sqlite
2005c 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
2005d 65 28 70 2d 3e 70 44 65 73 74 29 20 29 3b 0a 20  e(p->pDest) );. 
2005e 20 20 20 20 20 20 20 69 66 28 20 70 67 73 7a 53         if( pgszS
2005f 72 63 3c 70 67 73 7a 44 65 73 74 20 29 7b 0a 20  rc<pgszDest ){. 
20060 20 20 20 20 20 20 20 20 20 69 6e 74 20 72 61 74           int rat
20061 69 6f 20 3d 20 70 67 73 7a 44 65 73 74 2f 70 67  io = pgszDest/pg
20062 73 7a 53 72 63 3b 0a 20 20 20 20 20 20 20 20 20  szSrc;.         
20063 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 3d   nDestTruncate =
20064 20 28 6e 53 72 63 50 61 67 65 2b 72 61 74 69 6f   (nSrcPage+ratio
20065 2d 31 29 2f 72 61 74 69 6f 3b 0a 20 20 20 20 20  -1)/ratio;.     
20066 20 20 20 20 20 69 66 28 20 6e 44 65 73 74 54 72       if( nDestTr
20067 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29 50 45 4e  uncate==(int)PEN
20068 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
20069 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20 29 7b  ->pDest->pBt) ){
2006a 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65  .            nDe
2006b 73 74 54 72 75 6e 63 61 74 65 2d 2d 3b 0a 20 20  stTruncate--;.  
2006c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2006d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2006e 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65     nDestTruncate
2006f 20 3d 20 6e 53 72 63 50 61 67 65 20 2a 20 28 70   = nSrcPage * (p
20070 67 73 7a 53 72 63 2f 70 67 73 7a 44 65 73 74 29  gszSrc/pgszDest)
20071 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
20072 20 20 20 20 61 73 73 65 72 74 28 20 6e 44 65 73      assert( nDes
20073 74 54 72 75 6e 63 61 74 65 3e 30 20 29 3b 0a 20  tTruncate>0 );. 
20074 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
20075 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65  gerTruncateImage
20076 28 70 44 65 73 74 50 61 67 65 72 2c 20 6e 44 65  (pDestPager, nDe
20077 73 74 54 72 75 6e 63 61 74 65 29 3b 0a 0a 20 20  stTruncate);..  
20078 20 20 20 20 20 20 69 66 28 20 70 67 73 7a 53 72        if( pgszSr
20079 63 3c 70 67 73 7a 44 65 73 74 20 29 7b 0a 20 20  c<pgszDest ){.  
2007a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
2007b 65 20 73 6f 75 72 63 65 20 70 61 67 65 2d 73 69  e source page-si
2007c 7a 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68  ze is smaller th
2007d 61 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  an the destinati
2007e 6f 6e 20 70 61 67 65 2d 73 69 7a 65 2c 0a 20 20  on page-size,.  
2007f 20 20 20 20 20 20 20 20 2a 2a 20 74 77 6f 20 65          ** two e
20080 78 74 72 61 20 74 68 69 6e 67 73 20 6d 61 79 20  xtra things may 
20081 6e 65 65 64 20 74 6f 20 68 61 70 70 65 6e 3a 0a  need to happen:.
20082 20 20 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20            **.   
20083 20 20 20 20 20 20 20 2a 2a 20 20 20 2a 20 54 68         **   * Th
20084 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 61  e destination ma
20085 79 20 6e 65 65 64 20 74 6f 20 62 65 20 74 72 75  y need to be tru
20086 6e 63 61 74 65 64 2c 20 61 6e 64 0a 20 20 20 20  ncated, and.    
20087 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
20088 20 20 20 2a 2a 20 20 20 2a 20 44 61 74 61 20 73     **   * Data s
20089 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 70 61 67  tored on the pag
2008a 65 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66  es immediately f
2008b 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 0a 20 20  ollowing the .  
2008c 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 70          **     p
2008d 65 6e 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65  ending-byte page
2008e 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   in the source d
2008f 61 74 61 62 61 73 65 20 6d 61 79 20 6e 65 65 64  atabase may need
20090 20 74 6f 20 62 65 0a 20 20 20 20 20 20 20 20 20   to be.         
20091 20 2a 2a 20 20 20 20 20 63 6f 70 69 65 64 20 69   **     copied i
20092 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74  nto the destinat
20093 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 0a 20 20  ion database..  
20094 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
20095 20 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20 69       const i64 i
20096 53 69 7a 65 20 3d 20 28 69 36 34 29 70 67 73 7a  Size = (i64)pgsz
20097 53 72 63 20 2a 20 28 69 36 34 29 6e 53 72 63 50  Src * (i64)nSrcP
20098 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 73  age;.          s
20099 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 20 63 6f  qlite3_file * co
2009a 6e 73 74 20 70 46 69 6c 65 20 3d 20 73 71 6c 69  nst pFile = sqli
2009b 74 65 33 50 61 67 65 72 46 69 6c 65 28 70 44 65  te3PagerFile(pDe
2009c 73 74 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20  stPager);.      
2009d 20 20 20 20 69 36 34 20 69 4f 66 66 3b 0a 20 20      i64 iOff;.  
2009e 20 20 20 20 20 20 20 20 69 36 34 20 69 45 6e 64          i64 iEnd
2009f 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  ;..          ass
200a0 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
200a1 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
200a2 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3d 3d 30  nDestTruncate==0
200a3 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
200a4 7c 7c 20 28 69 36 34 29 6e 44 65 73 74 54 72 75  || (i64)nDestTru
200a5 6e 63 61 74 65 2a 28 69 36 34 29 70 67 73 7a 44  ncate*(i64)pgszD
200a6 65 73 74 20 3e 3d 20 69 53 69 7a 65 20 7c 7c 20  est >= iSize || 
200a7 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  (.              
200a8 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3d    nDestTruncate=
200a9 3d 28 69 6e 74 29 28 50 45 4e 44 49 4e 47 5f 42  =(int)(PENDING_B
200aa 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65 73  YTE_PAGE(p->pDes
200ab 74 2d 3e 70 42 74 29 2d 31 29 0a 20 20 20 20 20  t->pBt)-1).     
200ac 20 20 20 20 20 20 20 20 26 26 20 69 53 69 7a 65          && iSize
200ad 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26  >=PENDING_BYTE &
200ae 26 20 69 53 69 7a 65 3c 3d 50 45 4e 44 49 4e 47  & iSize<=PENDING
200af 5f 42 59 54 45 2b 70 67 73 7a 44 65 73 74 0a 20  _BYTE+pgszDest. 
200b0 20 20 20 20 20 20 20 20 20 29 29 3b 0a 0a 20 20           ));..  
200b1 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
200b2 63 61 6c 6c 20 65 6e 73 75 72 65 73 20 74 68 61  call ensures tha
200b3 74 20 61 6c 6c 20 64 61 74 61 20 72 65 71 75 69  t all data requi
200b4 72 65 64 20 74 6f 20 72 65 63 72 65 61 74 65 20  red to recreate 
200b5 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 20 20 20  the original.   
200b6 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61         ** databa
200b7 73 65 20 68 61 73 20 62 65 65 6e 20 73 74 6f 72  se has been stor
200b8 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  ed in the journa
200b9 6c 20 66 6f 72 20 70 44 65 73 74 50 61 67 65 72  l for pDestPager
200ba 20 61 6e 64 20 74 68 65 0a 20 20 20 20 20 20 20   and the.       
200bb 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 73 79     ** journal sy
200bc 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 53 6f  nced to disk. So
200bd 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 77   at this point w
200be 65 20 6d 61 79 20 73 61 66 65 6c 79 20 6d 6f 64  e may safely mod
200bf 69 66 79 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ify.          **
200c0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
200c1 6c 65 20 69 6e 20 61 6e 79 20 77 61 79 2c 20 6b  le in any way, k
200c2 6e 6f 77 69 6e 67 20 74 68 61 74 20 69 66 20 61  nowing that if a
200c3 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 0a 20   power failure. 
200c4 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 63 63 75           ** occu
200c5 72 73 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  rs, the original
200c6 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62   database will b
200c7 65 20 72 65 63 6f 6e 73 74 72 75 63 74 65 64 20  e reconstructed 
200c8 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 20 20  from the .      
200c9 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66      ** journal f
200ca 69 6c 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20  ile.  */.       
200cb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
200cc 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f  agerCommitPhaseO
200cd 6e 65 28 70 44 65 73 74 50 61 67 65 72 2c 20 30  ne(pDestPager, 0
200ce 2c 20 31 29 3b 0a 0a 20 20 20 20 20 20 20 20 20  , 1);..         
200cf 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 65 78   /* Write the ex
200d0 74 72 61 20 70 61 67 65 73 20 61 6e 64 20 74 72  tra pages and tr
200d1 75 6e 63 61 74 65 20 74 68 65 20 64 61 74 61 62  uncate the datab
200d2 61 73 65 20 66 69 6c 65 20 61 73 20 72 65 71 75  ase file as requ
200d3 69 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  ired */.        
200d4 20 20 69 45 6e 64 20 3d 20 4d 49 4e 28 50 45 4e    iEnd = MIN(PEN
200d5 44 49 4e 47 5f 42 59 54 45 20 2b 20 70 67 73 7a  DING_BYTE + pgsz
200d6 44 65 73 74 2c 20 69 53 69 7a 65 29 3b 0a 20 20  Dest, iSize);.  
200d7 20 20 20 20 20 20 20 20 66 6f 72 28 0a 20 20 20          for(.   
200d8 20 20 20 20 20 20 20 20 20 69 4f 66 66 3d 50 45           iOff=PE
200d9 4e 44 49 4e 47 5f 42 59 54 45 2b 70 67 73 7a 53  NDING_BYTE+pgszS
200da 72 63 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20  rc; .           
200db 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
200dc 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 0a 20 20  & iOff<iEnd; .  
200dd 20 20 20 20 20 20 20 20 20 20 69 4f 66 66 2b 3d            iOff+=
200de 70 67 73 7a 53 72 63 0a 20 20 20 20 20 20 20 20  pgszSrc.        
200df 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20    ){.           
200e0 20 50 67 48 64 72 20 2a 70 53 72 63 50 67 20 3d   PgHdr *pSrcPg =
200e1 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   0;.            
200e2 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 72 63 50  const Pgno iSrcP
200e3 67 20 3d 20 28 50 67 6e 6f 29 28 28 69 4f 66 66  g = (Pgno)((iOff
200e4 2f 70 67 73 7a 53 72 63 29 2b 31 29 3b 0a 20 20  /pgszSrc)+1);.  
200e5 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
200e6 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
200e7 53 72 63 50 61 67 65 72 2c 20 69 53 72 63 50 67  SrcPager, iSrcPg
200e8 2c 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20  , &pSrcPg);.    
200e9 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
200ea 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
200eb 20 20 20 20 20 20 20 20 20 20 20 75 38 20 2a 7a             u8 *z
200ec 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61  Data = sqlite3Pa
200ed 67 65 72 47 65 74 44 61 74 61 28 70 53 72 63 50  gerGetData(pSrcP
200ee 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  g);.            
200ef 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
200f0 57 72 69 74 65 28 70 46 69 6c 65 2c 20 7a 44 61  Write(pFile, zDa
200f1 74 61 2c 20 70 67 73 7a 53 72 63 2c 20 69 4f 66  ta, pgszSrc, iOf
200f2 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  f);.            
200f3 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  }.            sq
200f4 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
200f5 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20  pSrcPg);.       
200f6 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
200f7 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
200f8 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
200f9 72 63 20 3d 20 62 61 63 6b 75 70 54 72 75 6e 63  rc = backupTrunc
200fa 61 74 65 46 69 6c 65 28 70 46 69 6c 65 2c 20 69  ateFile(pFile, i
200fb 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Size);.         
200fc 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   }..          /*
200fd 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 61   Sync the databa
200fe 73 65 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e  se file to disk.
200ff 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66   */.          if
20100 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
20101 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  ){.            r
20102 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
20103 53 79 6e 63 28 70 44 65 73 74 50 61 67 65 72 29  Sync(pDestPager)
20104 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
20105 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
20106 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
20107 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68  te3PagerCommitPh
20108 61 73 65 4f 6e 65 28 70 44 65 73 74 50 61 67 65  aseOne(pDestPage
20109 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  r, 0, 0);.      
2010a 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 20 20 20    }.    .       
2010b 20 2f 2a 20 46 69 6e 69 73 68 20 63 6f 6d 6d 69   /* Finish commi
2010c 74 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61  tting the transa
2010d 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 64 65 73  ction to the des
2010e 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
2010f 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66  e. */.        if
20110 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 0a  ( SQLITE_OK==rc.
20111 20 20 20 20 20 20 20 20 20 26 26 20 53 51 4c 49           && SQLI
20112 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
20113 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
20114 68 61 73 65 54 77 6f 28 70 2d 3e 70 44 65 73 74  haseTwo(p->pDest
20115 2c 20 30 29 29 0a 20 20 20 20 20 20 20 20 29 7b  , 0)).        ){
20116 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
20117 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20  SQLITE_DONE;.   
20118 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
20119 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49     }.  .    /* I
2011a 66 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 69 73  f bCloseTrans is
2011b 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 69 73   true, then this
2011c 20 66 75 6e 63 74 69 6f 6e 20 6f 70 65 6e 65 64   function opened
2011d 20 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74   a read transact
2011e 69 6f 6e 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68  ion.    ** on th
2011f 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
20120 65 2e 20 43 6c 6f 73 65 20 74 68 65 20 72 65 61  e. Close the rea
20121 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 65  d transaction he
20122 72 65 2e 20 54 68 65 72 65 20 69 73 0a 20 20 20  re. There is.   
20123 20 2a 2a 20 6e 6f 20 6e 65 65 64 20 74 6f 20 63   ** no need to c
20124 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20  heck the return 
20125 76 61 6c 75 65 73 20 6f 66 20 74 68 65 20 62 74  values of the bt
20126 72 65 65 20 6d 65 74 68 6f 64 73 20 68 65 72 65  ree methods here
20127 2c 20 61 73 0a 20 20 20 20 2a 2a 20 22 63 6f 6d  , as.    ** "com
20128 6d 69 74 74 69 6e 67 22 20 61 20 72 65 61 64 2d  mitting" a read-
20129 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 6f 6e  only transaction
2012a 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20   cannot fail..  
2012b 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 43 6c    */.    if( bCl
2012c 6f 73 65 54 72 61 6e 73 20 29 7b 0a 20 20 20 20  oseTrans ){.    
2012d 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20    TESTONLY( int 
2012e 72 63 32 20 29 3b 0a 20 20 20 20 20 20 54 45 53  rc2 );.      TES
2012f 54 4f 4e 4c 59 28 20 72 63 32 20 20 3d 20 29 20  TONLY( rc2  = ) 
20130 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
20131 69 74 50 68 61 73 65 4f 6e 65 28 70 2d 3e 70 53  itPhaseOne(p->pS
20132 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 54 45  rc, 0);.      TE
20133 53 54 4f 4e 4c 59 28 20 72 63 32 20 7c 3d 20 29  STONLY( rc2 |= )
20134 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
20135 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e 70  mitPhaseTwo(p->p
20136 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 61  Src, 0);.      a
20137 73 73 65 72 74 28 20 72 63 32 3d 3d 53 51 4c 49  ssert( rc2==SQLI
20138 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a 20  TE_OK );.    }. 
20139 20 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51   .    if( rc==SQ
2013a 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d  LITE_IOERR_NOMEM
2013b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
2013c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
2013d 20 7d 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 72   }.    p->rc = r
2013e 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  c;.  }.  if( p->
2013f 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73  pDestDb ){.    s
20140 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
20141 76 65 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d  ve(p->pDestDb->m
20142 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  utex);.  }.  sql
20143 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
20144 2d 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c 69 74  ->pSrc);.  sqlit
20145 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
20146 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29  ->pSrcDb->mutex)
20147 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
20148 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20  ../*.** Release 
20149 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 61 73  all resources as
2014a 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6e  sociated with an
2014b 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2a   sqlite3_backup*
2014c 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49   handle..*/.SQLI
2014d 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2014e 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
2014f 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
20150 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62  *p){.  sqlite3_b
20151 61 63 6b 75 70 20 2a 2a 70 70 3b 20 20 20 20 20  ackup **pp;     
20152 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
20153 74 72 20 74 6f 20 68 65 61 64 20 6f 66 20 70 61  tr to head of pa
20154 67 65 72 73 20 62 61 63 6b 75 70 20 6c 69 73 74  gers backup list
20155 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70   */.  sqlite3 *p
20156 53 72 63 44 62 3b 20 20 20 20 20 20 20 20 20 20  SrcDb;          
20157 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f             /* So
20158 75 72 63 65 20 64 61 74 61 62 61 73 65 20 63 6f  urce database co
20159 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  nnection */.  in
2015a 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
2015b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2015c 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72     /* Value to r
2015d 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 45  eturn */..  /* E
2015e 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 65 73  nter the mutexes
2015f 20 2a 2f 0a 20 20 69 66 28 20 70 3d 3d 30 20 29   */.  if( p==0 )
20160 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
20161 4b 3b 0a 20 20 70 53 72 63 44 62 20 3d 20 70 2d  K;.  pSrcDb = p-
20162 3e 70 53 72 63 44 62 3b 0a 20 20 73 71 6c 69 74  >pSrcDb;.  sqlit
20163 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
20164 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  SrcDb->mutex);. 
20165 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
20166 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69  er(p->pSrc);.  i
20167 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b  f( p->pDestDb ){
20168 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
20169 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 73  ex_enter(p->pDes
2016a 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  tDb->mutex);.  }
2016b 0a 0a 20 20 2f 2a 20 44 65 74 61 63 68 20 74 68  ..  /* Detach th
2016c 69 73 20 62 61 63 6b 75 70 20 66 72 6f 6d 20 74  is backup from t
2016d 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e  he source pager.
2016e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 44 65   */.  if( p->pDe
2016f 73 74 44 62 20 29 7b 0a 20 20 20 20 70 2d 3e 70  stDb ){.    p->p
20170 53 72 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d 3b 0a  Src->nBackup--;.
20171 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 73 41    }.  if( p->isA
20172 74 74 61 63 68 65 64 20 29 7b 0a 20 20 20 20 70  ttached ){.    p
20173 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  p = sqlite3Pager
20174 42 61 63 6b 75 70 50 74 72 28 73 71 6c 69 74 65  BackupPtr(sqlite
20175 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70  3BtreePager(p->p
20176 53 72 63 29 29 3b 0a 20 20 20 20 77 68 69 6c 65  Src));.    while
20177 28 20 2a 70 70 21 3d 70 20 29 7b 0a 20 20 20 20  ( *pp!=p ){.    
20178 20 20 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70    pp = &(*pp)->p
20179 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Next;.    }.    
2017a 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a  *pp = p->pNext;.
2017b 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74    }..  /* If a t
2017c 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74  ransaction is st
2017d 69 6c 6c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20  ill open on the 
2017e 42 74 72 65 65 2c 20 72 6f 6c 6c 20 69 74 20 62  Btree, roll it b
2017f 61 63 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ack. */.  sqlite
20180 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70  3BtreeRollback(p
20181 2d 3e 70 44 65 73 74 2c 20 53 51 4c 49 54 45 5f  ->pDest, SQLITE_
20182 4f 4b 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74  OK);..  /* Set t
20183 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66  he error code of
20184 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
20185 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
20186 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 28 70 2d 3e  . */.  rc = (p->
20187 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29  rc==SQLITE_DONE)
20188 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 70   ? SQLITE_OK : p
20189 2d 3e 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 45  ->rc;.  sqlite3E
2018a 72 72 6f 72 28 70 2d 3e 70 44 65 73 74 44 62 2c  rror(p->pDestDb,
2018b 20 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 45   rc, 0);..  /* E
2018c 78 69 74 20 74 68 65 20 6d 75 74 65 78 65 73 20  xit the mutexes 
2018d 61 6e 64 20 66 72 65 65 20 74 68 65 20 62 61 63  and free the bac
2018e 6b 75 70 20 63 6f 6e 74 65 78 74 20 73 74 72 75  kup context stru
2018f 63 74 75 72 65 2e 20 2a 2f 0a 20 20 69 66 28 20  cture. */.  if( 
20190 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20  p->pDestDb ){.  
20191 20 20 73 71 6c 69 74 65 33 4c 65 61 76 65 4d 75    sqlite3LeaveMu
20192 74 65 78 41 6e 64 43 6c 6f 73 65 5a 6f 6d 62 69  texAndCloseZombi
20193 65 28 70 2d 3e 70 44 65 73 74 44 62 29 3b 0a 20  e(p->pDestDb);. 
20194 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65   }.  sqlite3Btre
20195 65 4c 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b  eLeave(p->pSrc);
20196 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44  .  if( p->pDestD
20197 62 20 29 7b 0a 20 20 20 20 2f 2a 20 45 56 49 44  b ){.    /* EVID
20198 45 4e 43 45 2d 4f 46 3a 20 52 2d 36 34 38 35 32  ENCE-OF: R-64852
20199 2d 32 31 35 39 31 20 54 68 65 20 73 71 6c 69 74  -21591 The sqlit
2019a 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74  e3_backup object
2019b 20 69 73 20 63 72 65 61 74 65 64 20 62 79 20 61   is created by a
2019c 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20  .    ** call to 
2019d 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69  sqlite3_backup_i
2019e 6e 69 74 28 29 20 61 6e 64 20 69 73 20 64 65 73  nit() and is des
2019f 74 72 6f 79 65 64 20 62 79 20 61 20 63 61 6c 6c  troyed by a call
201a0 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74   to.    ** sqlit
201a1 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
201a2 28 29 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  (). */.    sqlit
201a3 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a  e3_free(p);.  }.
201a4 20 20 73 71 6c 69 74 65 33 4c 65 61 76 65 4d 75    sqlite3LeaveMu
201a5 74 65 78 41 6e 64 43 6c 6f 73 65 5a 6f 6d 62 69  texAndCloseZombi
201a6 65 28 70 53 72 63 44 62 29 3b 0a 20 20 72 65 74  e(pSrcDb);.  ret
201a7 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
201a8 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
201a9 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c  er of pages stil
201aa 6c 20 74 6f 20 62 65 20 62 61 63 6b 65 64 20 75  l to be backed u
201ab 70 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74  p as of the most
201ac 20 72 65 63 65 6e 74 0a 2a 2a 20 63 61 6c 6c 20   recent.** call 
201ad 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
201ae 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c  p_step()..*/.SQL
201af 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
201b0 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69  te3_backup_remai
201b1 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63  ning(sqlite3_bac
201b2 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  kup *p){.  retur
201b3 6e 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e 67 3b  n p->nRemaining;
201b4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
201b5 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   the total numbe
201b6 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
201b7 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
201b8 65 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74  e as of the most
201b9 20 0a 2a 2a 20 72 65 63 65 6e 74 20 63 61 6c 6c   .** recent call
201ba 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
201bb 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51  up_step()..*/.SQ
201bc 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
201bd 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65  ite3_backup_page
201be 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 61  count(sqlite3_ba
201bf 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75  ckup *p){.  retu
201c0 72 6e 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e 74  rn p->nPagecount
201c1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
201c2 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
201c3 65 64 20 61 66 74 65 72 20 74 68 65 20 63 6f 6e  ed after the con
201c4 74 65 6e 74 73 20 6f 66 20 70 61 67 65 20 69 50  tents of page iP
201c5 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 6f  age of the.** so
201c6 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61  urce database ha
201c7 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  ve been modified
201c8 2e 20 49 66 20 70 61 67 65 20 69 50 61 67 65 20  . If page iPage 
201c9 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
201ca 20 0a 2a 2a 20 63 6f 70 69 65 64 20 69 6e 74 6f   .** copied into
201cb 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
201cc 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20   database, then 
201cd 74 68 65 20 64 61 74 61 20 77 72 69 74 74 65 6e  the data written
201ce 20 74 6f 20 74 68 65 0a 2a 2a 20 64 65 73 74 69   to the.** desti
201cf 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 77 20 69 6e  nation is now in
201d0 76 61 6c 69 64 61 74 65 64 2e 20 54 68 65 20 64  validated. The d
201d1 65 73 74 69 6e 61 74 69 6f 6e 20 63 6f 70 79 20  estination copy 
201d2 6f 66 20 69 50 61 67 65 20 6e 65 65 64 73 0a 2a  of iPage needs.*
201d3 2a 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20  * to be updated 
201d4 77 69 74 68 20 74 68 65 20 6e 65 77 20 64 61 74  with the new dat
201d5 61 20 62 65 66 6f 72 65 20 74 68 65 20 62 61 63  a before the bac
201d6 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  kup operation is
201d7 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a  .** complete..**
201d8 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65  .** It is assume
201d9 64 20 74 68 61 74 20 74 68 65 20 6d 75 74 65 78  d that the mutex
201da 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
201db 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62   the BtShared ob
201dc 6a 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f  ject.** correspo
201dd 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75  nding to the sou
201de 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20  rce database is 
201df 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66  held when this f
201e0 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61  unction is.** ca
201e1 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lled..*/.SQLITE_
201e2 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
201e3 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65  ite3BackupUpdate
201e4 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
201e5 2a 70 42 61 63 6b 75 70 2c 20 50 67 6e 6f 20 69  *pBackup, Pgno i
201e6 50 61 67 65 2c 20 63 6f 6e 73 74 20 75 38 20 2a  Page, const u8 *
201e7 61 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 74 65  aData){.  sqlite
201e8 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20  3_backup *p;    
201e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
201ea 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
201eb 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70  ble */.  for(p=p
201ec 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e  Backup; p; p=p->
201ed 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65  pNext){.    asse
201ee 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
201ef 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e  x_held(p->pSrc->
201f0 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
201f1 20 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45     if( !isFatalE
201f2 72 72 6f 72 28 70 2d 3e 72 63 29 20 26 26 20 69  rror(p->rc) && i
201f3 50 61 67 65 3c 70 2d 3e 69 4e 65 78 74 20 29 7b  Page<p->iNext ){
201f4 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 61  .      /* The ba
201f5 63 6b 75 70 20 70 72 6f 63 65 73 73 20 70 20 68  ckup process p h
201f6 61 73 20 61 6c 72 65 61 64 79 20 63 6f 70 69 65  as already copie
201f7 64 20 70 61 67 65 20 69 50 61 67 65 2e 20 42 75  d page iPage. Bu
201f8 74 20 6e 6f 77 20 69 74 0a 20 20 20 20 20 20 2a  t now it.      *
201f9 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66  * has been modif
201fa 69 65 64 20 62 79 20 61 20 74 72 61 6e 73 61 63  ied by a transac
201fb 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72  tion on the sour
201fc 63 65 20 70 61 67 65 72 2e 20 43 6f 70 79 0a 20  ce pager. Copy. 
201fd 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20       ** the new 
201fe 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 62 61  data into the ba
201ff 63 6b 75 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ckup..      */. 
20200 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20       int rc;.   
20201 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 44     assert( p->pD
20202 65 73 74 44 62 20 29 3b 0a 20 20 20 20 20 20 73  estDb );.      s
20203 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
20204 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d  er(p->pDestDb->m
20205 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72 63 20  utex);.      rc 
20206 3d 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28  = backupOnePage(
20207 70 2c 20 69 50 61 67 65 2c 20 61 44 61 74 61 29  p, iPage, aData)
20208 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
20209 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70  mutex_leave(p->p
2020a 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a  DestDb->mutex);.
2020b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
2020c 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26  !=SQLITE_BUSY &&
2020d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b   rc!=SQLITE_LOCK
2020e 45 44 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ED );.      if( 
2020f 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
20210 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d  .        p->rc =
20211 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
20212 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
20213 52 65 73 74 61 72 74 20 74 68 65 20 62 61 63 6b  Restart the back
20214 75 70 20 70 72 6f 63 65 73 73 2e 20 54 68 69 73  up process. This
20215 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
20216 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 0a  the pager layer.
20217 2a 2a 20 64 65 74 65 63 74 73 20 74 68 61 74 20  ** detects that 
20218 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73  the database has
20219 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 62   been modified b
2021a 79 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 64 61  y an external da
2021b 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63  tabase.** connec
2021c 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61  tion. In this ca
2021d 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77  se there is no w
2021e 61 79 20 6f 66 20 6b 6e 6f 77 69 6e 67 20 77 68  ay of knowing wh
2021f 69 63 68 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61  ich of the.** pa
20220 67 65 73 20 74 68 61 74 20 68 61 76 65 20 62 65  ges that have be
20221 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74  en copied into t
20222 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  he destination d
20223 61 74 61 62 61 73 65 20 61 72 65 20 73 74 69 6c  atabase are stil
20224 6c 20 0a 2a 2a 20 76 61 6c 69 64 20 61 6e 64 20  l .** valid and 
20225 77 68 69 63 68 20 61 72 65 20 6e 6f 74 2c 20 73  which are not, s
20226 6f 20 74 68 65 20 65 6e 74 69 72 65 20 70 72 6f  o the entire pro
20227 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 62 65  cess needs to be
20228 20 72 65 73 74 61 72 74 65 64 2e 0a 2a 2a 0a 2a   restarted..**.*
20229 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20  * It is assumed 
2022a 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 20 61  that the mutex a
2022b 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2022c 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65  he BtShared obje
2022d 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64  ct.** correspond
2022e 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72 63  ing to the sourc
2022f 65 20 64 61 74 61 62 61 73 65 20 69 73 20 68 65  e database is he
20230 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  ld when this fun
20231 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c  ction is.** call
20232 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
20233 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
20234 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 28  e3BackupRestart(
20235 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
20236 70 42 61 63 6b 75 70 29 7b 0a 20 20 73 71 6c 69  pBackup){.  sqli
20237 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20  te3_backup *p;  
20238 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20239 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
2023a 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70  iable */.  for(p
2023b 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70  =pBackup; p; p=p
2023c 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73  ->pNext){.    as
2023d 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
2023e 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63  tex_held(p->pSrc
2023f 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
20240 0a 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20  .    p->iNext = 
20241 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65  1;.  }.}..#ifnde
20242 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41  f SQLITE_OMIT_VA
20243 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20  CUUM./*.** Copy 
20244 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e  the complete con
20245 74 65 6e 74 20 6f 66 20 70 42 74 46 72 6f 6d 20  tent of pBtFrom 
20246 69 6e 74 6f 20 70 42 74 54 6f 2e 20 20 41 20 74  into pBtTo.  A t
20247 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75  ransaction.** mu
20248 73 74 20 62 65 20 61 63 74 69 76 65 20 66 6f 72  st be active for
20249 20 62 6f 74 68 20 66 69 6c 65 73 2e 0a 2a 2a 0a   both files..**.
2024a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 66  ** The size of f
2024b 69 6c 65 20 70 54 6f 20 6d 61 79 20 62 65 20 72  ile pTo may be r
2024c 65 64 75 63 65 64 20 62 79 20 74 68 69 73 20 6f  educed by this o
2024d 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 61 6e 79  peration. If any
2024e 74 68 69 6e 67 20 0a 2a 2a 20 67 6f 65 73 20 77  thing .** goes w
2024f 72 6f 6e 67 2c 20 74 68 65 20 74 72 61 6e 73 61  rong, the transa
20250 63 74 69 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 20  ction on pTo is 
20251 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20  rolled back. If 
20252 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 20  successful, the 
20253 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
20254 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 65 66  is committed bef
20255 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
20256 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20257 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
20258 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20  eCopyFile(Btree 
20259 2a 70 54 6f 2c 20 42 74 72 65 65 20 2a 70 46 72  *pTo, Btree *pFr
2025a 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  om){.  int rc;. 
2025b 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
2025c 46 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Fd;             
2025d 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70   /* File descrip
2025e 74 6f 72 20 66 6f 72 20 64 61 74 61 62 61 73 65  tor for database
2025f 20 70 54 6f 20 2a 2f 0a 20 20 73 71 6c 69 74 65   pTo */.  sqlite
20260 33 5f 62 61 63 6b 75 70 20 62 3b 0a 20 20 73 71  3_backup b;.  sq
20261 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
20262 70 54 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  pTo);.  sqlite3B
20263 74 72 65 65 45 6e 74 65 72 28 70 46 72 6f 6d 29  treeEnter(pFrom)
20264 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
20265 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61  ite3BtreeIsInTra
20266 6e 73 28 70 54 6f 29 20 29 3b 0a 20 20 70 46 64  ns(pTo) );.  pFd
20267 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46   = sqlite3PagerF
20268 69 6c 65 28 73 71 6c 69 74 65 33 42 74 72 65 65  ile(sqlite3Btree
20269 50 61 67 65 72 28 70 54 6f 29 29 3b 0a 20 20 69  Pager(pTo));.  i
2026a 66 28 20 70 46 64 2d 3e 70 4d 65 74 68 6f 64 73  f( pFd->pMethods
2026b 20 29 7b 0a 20 20 20 20 69 36 34 20 6e 42 79 74   ){.    i64 nByt
2026c 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  e = sqlite3Btree
2026d 47 65 74 50 61 67 65 53 69 7a 65 28 70 46 72 6f  GetPageSize(pFro
2026e 6d 29 2a 28 69 36 34 29 73 71 6c 69 74 65 33 42  m)*(i64)sqlite3B
2026f 74 72 65 65 4c 61 73 74 50 61 67 65 28 70 46 72  treeLastPage(pFr
20270 6f 6d 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  om);.    rc = sq
20271 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
20272 6f 6c 28 70 46 64 2c 20 53 51 4c 49 54 45 5f 46  ol(pFd, SQLITE_F
20273 43 4e 54 4c 5f 4f 56 45 52 57 52 49 54 45 2c 20  CNTL_OVERWRITE, 
20274 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28  &nByte);.    if(
20275 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 54 46   rc==SQLITE_NOTF
20276 4f 55 4e 44 20 29 20 72 63 20 3d 20 53 51 4c 49  OUND ) rc = SQLI
20277 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 66 28 20 72  TE_OK;.    if( r
20278 63 20 29 20 67 6f 74 6f 20 63 6f 70 79 5f 66 69  c ) goto copy_fi
20279 6e 69 73 68 65 64 3b 0a 20 20 7d 0a 0a 20 20 2f  nished;.  }..  /
2027a 2a 20 53 65 74 20 75 70 20 61 6e 20 73 71 6c 69  * Set up an sqli
2027b 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63  te3_backup objec
2027c 74 2e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  t. sqlite3_backu
2027d 70 2e 70 44 65 73 74 44 62 20 6d 75 73 74 20 62  p.pDestDb must b
2027e 65 20 73 65 74 0a 20 20 2a 2a 20 74 6f 20 30 2e  e set.  ** to 0.
2027f 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79   This is used by
20280 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
20281 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f  ions of sqlite3_
20282 62 61 63 6b 75 70 5f 73 74 65 70 28 29 0a 20 20  backup_step().  
20283 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 62  ** and sqlite3_b
20284 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 74  ackup_finish() t
20285 6f 20 64 65 74 65 63 74 20 74 68 61 74 20 74 68  o detect that th
20286 65 79 20 61 72 65 20 62 65 69 6e 67 20 63 61 6c  ey are being cal
20287 6c 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68  led.  ** from th
20288 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 6e 6f 74  is function, not
20289 20 64 69 72 65 63 74 6c 79 20 62 79 20 74 68 65   directly by the
2028a 20 75 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 6d 65   user..  */.  me
2028b 6d 73 65 74 28 26 62 2c 20 30 2c 20 73 69 7a 65  mset(&b, 0, size
2028c 6f 66 28 62 29 29 3b 0a 20 20 62 2e 70 53 72 63  of(b));.  b.pSrc
2028d 44 62 20 3d 20 70 46 72 6f 6d 2d 3e 64 62 3b 0a  Db = pFrom->db;.
2028e 20 20 62 2e 70 53 72 63 20 3d 20 70 46 72 6f 6d    b.pSrc = pFrom
2028f 3b 0a 20 20 62 2e 70 44 65 73 74 20 3d 20 70 54  ;.  b.pDest = pT
20290 6f 3b 0a 20 20 62 2e 69 4e 65 78 74 20 3d 20 31  o;.  b.iNext = 1
20291 3b 0a 0a 20 20 2f 2a 20 30 78 37 46 46 46 46 46  ;..  /* 0x7FFFFF
20292 46 46 20 69 73 20 74 68 65 20 68 61 72 64 20 6c  FF is the hard l
20293 69 6d 69 74 20 66 6f 72 20 74 68 65 20 6e 75 6d  imit for the num
20294 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
20295 61 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  a database.  ** 
20296 66 69 6c 65 2e 20 42 79 20 70 61 73 73 69 6e 67  file. By passing
20297 20 74 68 69 73 20 61 73 20 74 68 65 20 6e 75 6d   this as the num
20298 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20  ber of pages to 
20299 63 6f 70 79 20 74 6f 0a 20 20 2a 2a 20 73 71 6c  copy to.  ** sql
2029a 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
2029b 28 29 2c 20 77 65 20 63 61 6e 20 67 75 61 72 61  (), we can guara
2029c 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 63 6f  ntee that the co
2029d 70 79 20 66 69 6e 69 73 68 65 73 20 0a 20 20 2a  py finishes .  *
2029e 2a 20 77 69 74 68 69 6e 20 61 20 73 69 6e 67 6c  * within a singl
2029f 65 20 63 61 6c 6c 20 28 75 6e 6c 65 73 73 20 61  e call (unless a
202a0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 29 2e  n error occurs).
202a1 20 54 68 65 20 61 73 73 65 72 74 28 29 20 73 74   The assert() st
202a2 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 63 68 65  atement.  ** che
202a3 63 6b 73 20 74 68 69 73 20 61 73 73 75 6d 70 74  cks this assumpt
202a4 69 6f 6e 20 2d 20 28 70 2d 3e 72 63 29 20 73 68  ion - (p->rc) sh
202a5 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 65  ould be set to e
202a6 69 74 68 65 72 20 53 51 4c 49 54 45 5f 44 4f 4e  ither SQLITE_DON
202a7 45 20 0a 20 20 2a 2a 20 6f 72 20 61 6e 20 65 72  E .  ** or an er
202a8 72 6f 72 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20  ror code..  */. 
202a9 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
202aa 73 74 65 70 28 26 62 2c 20 30 78 37 46 46 46 46  step(&b, 0x7FFFF
202ab 46 46 46 29 3b 0a 20 20 61 73 73 65 72 74 28 20  FFF);.  assert( 
202ac 62 2e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  b.rc!=SQLITE_OK 
202ad 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
202ae 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
202af 26 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  &b);.  if( rc==S
202b0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
202b1 70 54 6f 2d 3e 70 42 74 2d 3e 62 74 73 46 6c 61  pTo->pBt->btsFla
202b2 67 73 20 26 3d 20 7e 42 54 53 5f 50 41 47 45 53  gs &= ~BTS_PAGES
202b3 49 5a 45 5f 46 49 58 45 44 3b 0a 20 20 7d 65 6c  IZE_FIXED;.  }el
202b4 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  se{.    sqlite3P
202b5 61 67 65 72 43 6c 65 61 72 43 61 63 68 65 28 73  agerClearCache(s
202b6 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
202b7 28 62 2e 70 44 65 73 74 29 29 3b 0a 20 20 7d 0a  (b.pDest));.  }.
202b8 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
202b9 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73  e3BtreeIsInTrans
202ba 28 70 54 6f 29 3d 3d 30 20 29 3b 0a 63 6f 70 79  (pTo)==0 );.copy
202bb 5f 66 69 6e 69 73 68 65 64 3a 0a 20 20 73 71 6c  _finished:.  sql
202bc 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
202bd 46 72 6f 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33  From);.  sqlite3
202be 42 74 72 65 65 4c 65 61 76 65 28 70 54 6f 29 3b  BtreeLeave(pTo);
202bf 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
202c0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
202c1 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 2a 2f 0a  _OMIT_VACUUM */.
202c2 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
202c3 20 45 6e 64 20 6f 66 20 62 61 63 6b 75 70 2e 63   End of backup.c
202c4 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
202c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
202c7 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
202c8 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65   Begin file vdbe
202c9 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem.c **********
202ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
202cc 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20  ./*.** 2004 May 
202cd 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  26.**.** The aut
202ce 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
202cf 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
202d0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
202d1 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
202d2 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
202d3 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
202d4 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
202d5 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
202d6 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
202d7 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
202d8 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
202d9 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
202da 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
202db 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
202dc 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
202dd 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
202de 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
202df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
202e3 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
202e4 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
202e5 75 73 65 20 74 6f 20 6d 61 6e 69 70 75 6c 61 74  use to manipulat
202e6 65 20 22 4d 65 6d 22 20 73 74 72 75 63 74 75 72  e "Mem" structur
202e7 65 2e 20 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 73  e.  A "Mem".** s
202e8 74 6f 72 65 73 20 61 20 73 69 6e 67 6c 65 20 76  tores a single v
202e9 61 6c 75 65 20 69 6e 20 74 68 65 20 56 44 42 45  alue in the VDBE
202ea 2e 20 20 4d 65 6d 20 69 73 20 61 6e 20 6f 70 61  .  Mem is an opa
202eb 71 75 65 20 73 74 72 75 63 74 75 72 65 20 76 69  que structure vi
202ec 73 69 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 69  sible.** only wi
202ed 74 68 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20  thin the VDBE.  
202ee 49 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e  Interface routin
202ef 65 73 20 72 65 66 65 72 20 74 6f 20 61 20 4d 65  es refer to a Me
202f0 6d 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 6e  m using the.** n
202f1 61 6d 65 20 73 71 6c 69 74 65 5f 76 61 6c 75 65  ame sqlite_value
202f2 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 4d  .*/../*.** If pM
202f3 65 6d 20 69 73 20 61 6e 20 6f 62 6a 65 63 74 20  em is an object 
202f4 77 69 74 68 20 61 20 76 61 6c 69 64 20 73 74 72  with a valid str
202f5 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69  ing representati
202f6 6f 6e 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  on, this routine
202f7 0a 2a 2a 20 65 6e 73 75 72 65 73 20 74 68 65 20  .** ensures the 
202f8 69 6e 74 65 72 6e 61 6c 20 65 6e 63 6f 64 69 6e  internal encodin
202f9 67 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67  g for the string
202fa 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
202fb 69 73 0a 2a 2a 20 27 64 65 73 69 72 65 64 45 6e  is.** 'desiredEn
202fc 63 27 2c 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54  c', one of SQLIT
202fd 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 55  E_UTF8, SQLITE_U
202fe 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 54 45  TF16LE or SQLITE
202ff 5f 55 54 46 31 36 42 45 2e 0a 2a 2a 0a 2a 2a 20  _UTF16BE..**.** 
20300 49 66 20 70 4d 65 6d 20 69 73 20 6e 6f 74 20 61  If pMem is not a
20301 20 73 74 72 69 6e 67 20 6f 62 6a 65 63 74 2c 20   string object, 
20302 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  or the encoding 
20303 6f 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a  of the string.**
20304 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
20305 69 73 20 61 6c 72 65 61 64 79 20 73 74 6f 72 65  is already store
20306 64 20 75 73 69 6e 67 20 74 68 65 20 72 65 71 75  d using the requ
20307 65 73 74 65 64 20 65 6e 63 6f 64 69 6e 67 2c 20  ested encoding, 
20308 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75  then this.** rou
20309 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
2030a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  .**.** SQLITE_OK
2030b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
2030c 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  the conversion i
2030d 73 20 73 75 63 63 65 73 73 66 75 6c 20 28 6f 72  s successful (or
2030e 20 6e 6f 74 20 72 65 71 75 69 72 65 64 29 2e 0a   not required)..
2030f 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  ** SQLITE_NOMEM 
20310 6d 61 79 20 62 65 20 72 65 74 75 72 6e 65 64 20  may be returned 
20311 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  if a malloc() fa
20312 69 6c 73 20 64 75 72 69 6e 67 20 63 6f 6e 76 65  ils during conve
20313 72 73 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e  rsion.** between
20314 20 66 6f 72 6d 61 74 73 2e 0a 2a 2f 0a 53 51 4c   formats..*/.SQL
20315 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
20316 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
20317 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 2a 70  eEncoding(Mem *p
20318 4d 65 6d 2c 20 69 6e 74 20 64 65 73 69 72 65 64  Mem, int desired
20319 45 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Enc){.  int rc;.
2031a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
2031b 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65  >flags&MEM_RowSe
2031c 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  t)==0 );.  asser
2031d 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53  t( desiredEnc==S
2031e 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 64 65  QLITE_UTF8 || de
2031f 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45  siredEnc==SQLITE
20320 5f 55 54 46 31 36 4c 45 0a 20 20 20 20 20 20 20  _UTF16LE.       
20321 20 20 20 20 7c 7c 20 64 65 73 69 72 65 64 45 6e      || desiredEn
20322 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  c==SQLITE_UTF16B
20323 45 20 29 3b 0a 20 20 69 66 28 20 21 28 70 4d 65  E );.  if( !(pMe
20324 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72  m->flags&MEM_Str
20325 29 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d  ) || pMem->enc==
20326 64 65 73 69 72 65 64 45 6e 63 20 29 7b 0a 20 20  desiredEnc ){.  
20327 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
20328 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  OK;.  }.  assert
20329 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
2032a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
2032b 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
2032c 74 65 78 29 20 29 3b 0a 23 69 66 64 65 66 20 53  tex) );.#ifdef S
2032d 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
2032e 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2032f 5f 45 52 52 4f 52 3b 0a 23 65 6c 73 65 0a 0a 20  _ERROR;.#else.. 
20330 20 2f 2a 20 4d 65 6d 54 72 61 6e 73 6c 61 74 65   /* MemTranslate
20331 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51  () may return SQ
20332 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54  LITE_OK or SQLIT
20333 45 5f 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f 4d 45  E_NOMEM. If NOME
20334 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 20  M is returned,. 
20335 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 65 6e 63   ** then the enc
20336 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 76 61 6c  oding of the val
20337 75 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20  ue may not have 
20338 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20  changed..  */.  
20339 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
2033a 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 70 4d 65  MemTranslate(pMe
2033b 6d 2c 20 28 75 38 29 64 65 73 69 72 65 64 45 6e  m, (u8)desiredEn
2033c 63 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d  c);.  assert(rc=
2033d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 7c 7c  =SQLITE_OK    ||
2033e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
2033f 4d 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d  M);.  assert(rc=
20340 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 7c 7c  =SQLITE_OK    ||
20341 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69   pMem->enc!=desi
20342 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73 65 72  redEnc);.  asser
20343 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  t(rc==SQLITE_NOM
20344 45 4d 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d  EM || pMem->enc=
20345 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20  =desiredEnc);.  
20346 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69  return rc;.#endi
20347 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  f.}../*.** Make 
20348 73 75 72 65 20 70 4d 65 6d 2d 3e 7a 20 70 6f 69  sure pMem->z poi
20349 6e 74 73 20 74 6f 20 61 20 77 72 69 74 61 62 6c  nts to a writabl
2034a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20  e allocation of 
2034b 61 74 20 6c 65 61 73 74 20 0a 2a 2a 20 6e 20 62  at least .** n b
2034c 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ytes..**.** If t
2034d 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
2034e 74 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73  t passed to this
2034f 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 72 75   function is tru
20350 65 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 0a 2a  e, then memory.*
20351 2a 20 63 65 6c 6c 20 70 4d 65 6d 20 6d 75 73 74  * cell pMem must
20352 20 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e   contain a strin
20353 67 20 6f 72 20 62 6c 6f 62 2e 20 49 6e 20 74 68  g or blob. In th
20354 69 73 20 63 61 73 65 20 74 68 65 20 63 6f 6e 74  is case the cont
20355 65 6e 74 20 69 73 0a 2a 2a 20 70 72 65 73 65 72  ent is.** preser
20356 76 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ved. Otherwise, 
20357 69 66 20 74 68 65 20 74 68 69 72 64 20 70 61 72  if the third par
20358 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73 20 66  ameter to this f
20359 75 6e 63 74 69 6f 6e 20 69 73 20 66 61 6c 73 65  unction is false
2035a 2c 0a 2a 2a 20 61 6e 79 20 63 75 72 72 65 6e 74  ,.** any current
2035b 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20   string or blob 
2035c 76 61 6c 75 65 20 6d 61 79 20 62 65 20 64 69 73  value may be dis
2035d 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  carded..**.** Th
2035e 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73  is function sets
2035f 20 74 68 65 20 4d 45 4d 5f 44 79 6e 20 66 6c 61   the MEM_Dyn fla
20360 67 20 61 6e 64 20 63 6c 65 61 72 73 20 61 6e 79  g and clears any
20361 20 78 44 65 6c 20 63 61 6c 6c 62 61 63 6b 2e 0a   xDel callback..
20362 2a 2a 20 49 74 20 61 6c 73 6f 20 63 6c 65 61 72  ** It also clear
20363 73 20 4d 45 4d 5f 45 70 68 65 6d 20 61 6e 64 20  s MEM_Ephem and 
20364 4d 45 4d 5f 53 74 61 74 69 63 2e 20 49 66 20 74  MEM_Static. If t
20365 68 65 20 70 72 65 73 65 72 76 65 20 66 6c 61 67  he preserve flag
20366 20 69 73 20 0a 2a 2a 20 6e 6f 74 20 73 65 74 2c   is .** not set,
20367 20 4d 65 6d 2e 6e 20 69 73 20 7a 65 72 6f 65 64   Mem.n is zeroed
20368 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20369 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
2036a 64 62 65 4d 65 6d 47 72 6f 77 28 4d 65 6d 20 2a  dbeMemGrow(Mem *
2036b 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20 69 6e 74  pMem, int n, int
2036c 20 70 72 65 73 65 72 76 65 29 7b 0a 20 20 61 73   preserve){.  as
2036d 73 65 72 74 28 20 31 20 3e 3d 0a 20 20 20 20 28  sert( 1 >=.    (
2036e 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26  (pMem->zMalloc &
2036f 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3d  & pMem->zMalloc=
20370 3d 70 4d 65 6d 2d 3e 7a 29 20 3f 20 31 20 3a 20  =pMem->z) ? 1 : 
20371 30 29 20 2b 0a 20 20 20 20 28 28 28 70 4d 65 6d  0) +.    (((pMem
20372 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29  ->flags&MEM_Dyn)
20373 26 26 70 4d 65 6d 2d 3e 78 44 65 6c 29 20 3f 20  &&pMem->xDel) ? 
20374 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28  1 : 0) + .    ((
20375 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
20376 45 70 68 65 6d 29 20 3f 20 31 20 3a 20 30 29 20  Ephem) ? 1 : 0) 
20377 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66  + .    ((pMem->f
20378 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 63 29  lags&MEM_Static)
20379 20 3f 20 31 20 3a 20 30 29 0a 20 20 29 3b 0a 20   ? 1 : 0).  );. 
2037a 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
2037b 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74  flags&MEM_RowSet
2037c 29 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  )==0 );..  /* If
2037d 20 74 68 65 20 70 72 65 73 65 72 76 65 20 66 6c   the preserve fl
2037e 61 67 20 69 73 20 73 65 74 20 74 6f 20 74 72 75  ag is set to tru
2037f 65 2c 20 74 68 65 6e 20 74 68 65 20 6d 65 6d 6f  e, then the memo
20380 72 79 20 63 65 6c 6c 20 6d 75 73 74 20 61 6c 72  ry cell must alr
20381 65 61 64 79 0a 20 20 2a 2a 20 63 6f 6e 74 61 69  eady.  ** contai
20382 6e 20 61 20 76 61 6c 69 64 20 73 74 72 69 6e 67  n a valid string
20383 20 6f 72 20 62 6c 6f 62 20 76 61 6c 75 65 2e 20   or blob value. 
20384 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 72   */.  assert( pr
20385 65 73 65 72 76 65 3d 3d 30 20 7c 7c 20 70 4d 65  eserve==0 || pMe
20386 6d 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 42 6c  m->flags&(MEM_Bl
20387 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 3b 0a 0a  ob|MEM_Str) );..
20388 20 20 69 66 28 20 6e 3c 33 32 20 29 20 6e 20 3d    if( n<32 ) n =
20389 20 33 32 3b 0a 20 20 69 66 28 20 73 71 6c 69 74   32;.  if( sqlit
2038a 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  e3DbMallocSize(p
2038b 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a  Mem->db, pMem->z
2038c 4d 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20 20 20  Malloc)<n ){.   
2038d 20 69 66 28 20 70 72 65 73 65 72 76 65 20 26 26   if( preserve &&
2038e 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e   pMem->z==pMem->
2038f 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20  zMalloc ){.     
20390 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d   pMem->z = pMem-
20391 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74  >zMalloc = sqlit
20392 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65  e3DbReallocOrFre
20393 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d  e(pMem->db, pMem
20394 2d 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 70  ->z, n);.      p
20395 72 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20  reserve = 0;.   
20396 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
20397 6c 69 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d  lite3DbFree(pMem
20398 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c  ->db, pMem->zMal
20399 6c 6f 63 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d  loc);.      pMem
2039a 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69  ->zMalloc = sqli
2039b 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70  te3DbMallocRaw(p
2039c 4d 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20  Mem->db, n);.   
2039d 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d   }.  }..  if( pM
2039e 65 6d 2d 3e 7a 20 26 26 20 70 72 65 73 65 72 76  em->z && preserv
2039f 65 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  e && pMem->zMall
203a0 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70  oc && pMem->z!=p
203a1 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a  Mem->zMalloc ){.
203a2 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d      memcpy(pMem-
203a3 3e 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e  >zMalloc, pMem->
203a4 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d  z, pMem->n);.  }
203a5 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61  .  if( pMem->fla
203a6 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 4d  gs&MEM_Dyn && pM
203a7 65 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20  em->xDel ){.    
203a8 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 78 44  assert( pMem->xD
203a9 65 6c 21 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d  el!=SQLITE_DYNAM
203aa 49 43 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  IC );.    pMem->
203ab 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28 70 4d  xDel((void *)(pM
203ac 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d 0a 0a 20 20  em->z));.  }..  
203ad 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e  pMem->z = pMem->
203ae 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 66 28 20 70  zMalloc;.  if( p
203af 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20  Mem->z==0 ){.   
203b0 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
203b1 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65  EM_Null;.  }else
203b2 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  {.    pMem->flag
203b3 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d  s &= ~(MEM_Ephem
203b4 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20  |MEM_Static);.  
203b5 7d 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d  }.  pMem->xDel =
203b6 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 4d   0;.  return (pM
203b7 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 54 45 5f 4f  em->z ? SQLITE_O
203b8 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  K : SQLITE_NOMEM
203b9 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65  );.}../*.** Make
203ba 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 6f   the given Mem o
203bb 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e 20 20  bject MEM_Dyn.  
203bc 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
203bd 6d 61 6b 65 20 69 74 20 73 6f 0a 2a 2a 20 74 68  make it so.** th
203be 61 74 20 61 6e 79 20 54 45 58 54 20 6f 72 20 42  at any TEXT or B
203bf 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 69 73 20 73  LOB content is s
203c0 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20  tored in memory 
203c1 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a  obtained from.**
203c2 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49 6e 20 74   malloc().  In t
203c3 68 69 73 20 77 61 79 2c 20 77 65 20 6b 6e 6f 77  his way, we know
203c4 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79   that the memory
203c5 20 69 73 20 73 61 66 65 20 74 6f 20 62 65 0a 2a   is safe to be.*
203c6 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72  * overwritten or
203c7 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   altered..**.** 
203c8 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
203c9 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 53   on success or S
203ca 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 6d  QLITE_NOMEM if m
203cb 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a  alloc fails..*/.
203cc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
203cd 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
203ce 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 4d  mMakeWriteable(M
203cf 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74  em *pMem){.  int
203d0 20 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d   f;.  assert( pM
203d1 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
203d2 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
203d3 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
203d4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
203d5 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52  Mem->flags&MEM_R
203d6 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 45  owSet)==0 );.  E
203d7 78 70 61 6e 64 42 6c 6f 62 28 70 4d 65 6d 29 3b  xpandBlob(pMem);
203d8 0a 20 20 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61  .  f = pMem->fla
203d9 67 73 3b 0a 20 20 69 66 28 20 28 66 26 28 4d 45  gs;.  if( (f&(ME
203da 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29  M_Str|MEM_Blob))
203db 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65   && pMem->z!=pMe
203dc 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20  m->zMalloc ){.  
203dd 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
203de 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 70  eMemGrow(pMem, p
203df 4d 65 6d 2d 3e 6e 20 2b 20 32 2c 20 31 29 20 29  Mem->n + 2, 1) )
203e0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
203e1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
203e2 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70   }.    pMem->z[p
203e3 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 20  Mem->n] = 0;.   
203e4 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e   pMem->z[pMem->n
203e5 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65  +1] = 0;.    pMe
203e6 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  m->flags |= MEM_
203e7 54 65 72 6d 3b 0a 23 69 66 64 65 66 20 53 51 4c  Term;.#ifdef SQL
203e8 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 70 4d  ITE_DEBUG.    pM
203e9 65 6d 2d 3e 70 53 63 6f 70 79 46 72 6f 6d 20 3d  em->pScopyFrom =
203ea 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a   0;.#endif.  }..
203eb 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
203ec 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  OK;.}../*.** If 
203ed 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a 20 68  the given Mem* h
203ee 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64  as a zero-filled
203ef 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 20 69   tail, turn it i
203f0 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 79 0a  nto an ordinary.
203f1 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 20 69  ** blob stored i
203f2 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  n dynamically al
203f3 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a 2a  located space..*
203f4 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
203f5 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53  _OMIT_INCRBLOB.S
203f6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
203f7 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
203f8 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a  ExpandBlob(Mem *
203f9 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65  pMem){.  if( pMe
203fa 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a  m->flags & MEM_Z
203fb 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  ero ){.    int n
203fc 42 79 74 65 3b 0a 20 20 20 20 61 73 73 65 72 74  Byte;.    assert
203fd 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ( pMem->flags&ME
203fe 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 61 73  M_Blob );.    as
203ff 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
20400 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d  gs&MEM_RowSet)==
20401 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
20402 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pMem->db==0 || 
20403 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
20404 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74  ld(pMem->db->mut
20405 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53  ex) );..    /* S
20406 65 74 20 6e 42 79 74 65 20 74 6f 20 74 68 65 20  et nByte to the 
20407 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
20408 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72  required to stor
20409 65 20 74 68 65 20 65 78 70 61 6e 64 65 64 20 62  e the expanded b
2040a 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e 42 79 74  lob. */.    nByt
2040b 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d  e = pMem->n + pM
2040c 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20  em->u.nZero;.   
2040d 20 69 66 28 20 6e 42 79 74 65 3c 3d 30 20 29 7b   if( nByte<=0 ){
2040e 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 31  .      nByte = 1
2040f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
20410 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
20411 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20  ow(pMem, nByte, 
20412 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  1) ){.      retu
20413 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
20414 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 73  .    }..    mems
20415 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d  et(&pMem->z[pMem
20416 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75  ->n], 0, pMem->u
20417 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 70 4d 65  .nZero);.    pMe
20418 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e  m->n += pMem->u.
20419 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d  nZero;.    pMem-
2041a 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
2041b 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a  Zero|MEM_Term);.
2041c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
2041d 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
2041e 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  .../*.** Make su
2041f 72 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d  re the given Mem
20420 20 69 73 20 5c 75 30 30 30 30 20 74 65 72 6d 69   is \u0000 termi
20421 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nated..*/.SQLITE
20422 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
20423 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65  ite3VdbeMemNulTe
20424 72 6d 69 6e 61 74 65 28 4d 65 6d 20 2a 70 4d 65  rminate(Mem *pMe
20425 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  m){.  assert( pM
20426 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
20427 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
20428 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
20429 20 29 3b 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d   );.  if( (pMem-
2042a 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72  >flags & MEM_Ter
2042b 6d 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e  m)!=0 || (pMem->
2042c 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29  flags & MEM_Str)
2042d 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
2042e 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f  n SQLITE_OK;   /
2042f 2a 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20  * Nothing to do 
20430 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  */.  }.  if( sql
20431 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
20432 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c  pMem, pMem->n+2,
20433 20 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   1) ){.    retur
20434 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
20435 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d    }.  pMem->z[pM
20436 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d  em->n] = 0;.  pM
20437 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d  em->z[pMem->n+1]
20438 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c   = 0;.  pMem->fl
20439 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b  ags |= MEM_Term;
2043a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2043b 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  _OK;.}../*.** Ad
2043c 64 20 4d 45 4d 5f 53 74 72 20 74 6f 20 74 68 65  d MEM_Str to the
2043d 20 73 65 74 20 6f 66 20 72 65 70 72 65 73 65 6e   set of represen
2043e 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20  tations for the 
2043f 67 69 76 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62  given Mem.  Numb
20440 65 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65  ers.** are conve
20441 72 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74  rted using sqlit
20442 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 20 20  e3_snprintf().  
20443 43 6f 6e 76 65 72 74 69 6e 67 20 61 20 42 4c 4f  Converting a BLO
20444 42 20 74 6f 20 61 20 73 74 72 69 6e 67 0a 2a 2a  B to a string.**
20445 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
20446 2a 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70 72  ** Existing repr
20447 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d 5f  esentations MEM_
20448 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c  Int and MEM_Real
20449 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c   are *not* inval
2044a 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20  idated..**.** A 
2044b 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65 20 77  MEM_Null value w
2044c 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 61 73  ill never be pas
2044d 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63  sed to this func
2044e 74 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e 63 74  tion. This funct
2044f 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 64 20 66  ion is.** used f
20450 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 76 61  or converting va
20451 6c 75 65 73 20 74 6f 20 74 65 78 74 20 66 6f 72  lues to text for
20452 20 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 74 68   returning to th
20453 65 20 75 73 65 72 20 28 69 2e 65 2e 20 76 69 61  e user (i.e. via
20454 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  .** sqlite3_valu
20455 65 5f 74 65 78 74 28 29 29 2c 20 6f 72 20 66 6f  e_text()), or fo
20456 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20  r ensuring that 
20457 76 61 6c 75 65 73 20 74 6f 20 62 65 20 75 73 65  values to be use
20458 64 20 61 73 20 62 74 72 65 65 0a 2a 2a 20 6b 65  d as btree.** ke
20459 79 73 20 61 72 65 20 73 74 72 69 6e 67 73 2e 20  ys are strings. 
2045a 49 6e 20 74 68 65 20 66 6f 72 6d 65 72 20 63 61  In the former ca
2045b 73 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  se a NULL pointe
2045c 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 68  r is returned th
2045d 65 0a 2a 2a 20 75 73 65 72 20 61 6e 64 20 74 68  e.** user and th
2045e 65 20 6c 61 74 65 72 20 69 73 20 61 6e 20 69 6e  e later is an in
2045f 74 65 72 6e 61 6c 20 70 72 6f 67 72 61 6d 6d 69  ternal programmi
20460 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c  ng error..*/.SQL
20461 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
20462 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74  sqlite3VdbeMemSt
20463 72 69 6e 67 69 66 79 28 4d 65 6d 20 2a 70 4d 65  ringify(Mem *pMe
20464 6d 2c 20 69 6e 74 20 65 6e 63 29 7b 0a 20 20 69  m, int enc){.  i
20465 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
20466 4b 3b 0a 20 20 69 6e 74 20 66 67 20 3d 20 70 4d  K;.  int fg = pM
20467 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6e  em->flags;.  con
20468 73 74 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 33  st int nByte = 3
20469 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d  2;..  assert( pM
2046a 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
2046b 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
2046c 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
2046d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28   );.  assert( !(
2046e 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a  fg&MEM_Zero) );.
2046f 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 28    assert( !(fg&(
20470 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62  MEM_Str|MEM_Blob
20471 29 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  )) );.  assert( 
20472 66 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f  fg&(MEM_Int|MEM_
20473 52 65 61 6c 29 20 29 3b 0a 20 20 61 73 73 65 72  Real) );.  asser
20474 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  t( (pMem->flags&
20475 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
20476 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48  ;.  assert( EIGH
20477 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54  T_BYTE_ALIGNMENT
20478 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20 20 69 66  (pMem) );...  if
20479 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ( sqlite3VdbeMem
2047a 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65  Grow(pMem, nByte
2047b 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , 0) ){.    retu
2047c 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2047d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61  .  }..  /* For a
2047e 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65 72   Real or Integer
2047f 2c 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 70  , use sqlite3_mp
20480 72 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64 75  rintf() to produ
20481 63 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20 2a  ce the UTF-8.  *
20482 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  * string represe
20483 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76  ntation of the v
20484 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20 74  alue. Then, if t
20485 68 65 20 72 65 71 75 69 72 65 64 20 65 6e 63 6f  he required enco
20486 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54 46  ding.  ** is UTF
20487 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36 62  -16le or UTF-16b
20488 65 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74 69  e do a translati
20489 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 46  on..  ** .  ** F
2048a 49 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64 20  IX ME: It would 
2048b 62 65 20 62 65 74 74 65 72 20 69 66 20 73 71 6c  be better if sql
2048c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20  ite3_snprintf() 
2048d 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36 2e  could do UTF-16.
2048e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20 26  .  */.  if( fg &
2048f 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
20490 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
20491 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c  (nByte, pMem->z,
20492 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75   "%lld", pMem->u
20493 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  .i);.  }else{.  
20494 20 20 61 73 73 65 72 74 28 20 66 67 20 26 20 4d    assert( fg & M
20495 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 73  EM_Real );.    s
20496 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
20497 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20  nByte, pMem->z, 
20498 22 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d 3e  "%!.15g", pMem->
20499 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e  r);.  }.  pMem->
2049a 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
2049b 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20  n30(pMem->z);.  
2049c 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pMem->enc = SQLI
2049d 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d 2d  TE_UTF8;.  pMem-
2049e 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74  >flags |= MEM_St
2049f 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 71  r|MEM_Term;.  sq
204a0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
204a1 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e  ncoding(pMem, en
204a2 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  c);.  return rc;
204a3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79  .}../*.** Memory
204a4 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 61   cell pMem conta
204a5 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20  ins the context 
204a6 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  of an aggregate 
204a7 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  function..** Thi
204a8 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20  s routine calls 
204a9 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 74  the finalize met
204aa 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 6e  hod for that fun
204ab 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 72  ction.  The.** r
204ac 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 67  esult of the agg
204ad 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 64  regate is stored
204ae 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e   back into pMem.
204af 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
204b0 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68  LITE_ERROR if th
204b1 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 6f  e finalizer repo
204b2 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 53  rts an error.  S
204b3 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65  QLITE_OK.** othe
204b4 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rwise..*/.SQLITE
204b5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
204b6 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c  ite3VdbeMemFinal
204b7 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46  ize(Mem *pMem, F
204b8 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b 0a  uncDef *pFunc){.
204b9 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
204ba 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57 41  E_OK;.  if( ALWA
204bb 59 53 28 70 46 75 6e 63 20 26 26 20 70 46 75 6e  YS(pFunc && pFun
204bc 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 29 20 29 7b  c->xFinalize) ){
204bd 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  .    sqlite3_con
204be 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 61 73  text ctx;.    as
204bf 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
204c0 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d  gs & MEM_Null)!=
204c1 30 20 7c 7c 20 70 46 75 6e 63 3d 3d 70 4d 65 6d  0 || pFunc==pMem
204c2 2d 3e 75 2e 70 44 65 66 20 29 3b 0a 20 20 20 20  ->u.pDef );.    
204c3 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
204c4 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
204c5 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
204c6 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
204c7 20 20 6d 65 6d 73 65 74 28 26 63 74 78 2c 20 30    memset(&ctx, 0
204c8 2c 20 73 69 7a 65 6f 66 28 63 74 78 29 29 3b 0a  , sizeof(ctx));.
204c9 20 20 20 20 63 74 78 2e 73 2e 66 6c 61 67 73 20      ctx.s.flags 
204ca 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
204cb 63 74 78 2e 73 2e 64 62 20 3d 20 70 4d 65 6d 2d  ctx.s.db = pMem-
204cc 3e 64 62 3b 0a 20 20 20 20 63 74 78 2e 70 4d 65  >db;.    ctx.pMe
204cd 6d 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 63 74  m = pMem;.    ct
204ce 78 2e 70 46 75 6e 63 20 3d 20 70 46 75 6e 63 3b  x.pFunc = pFunc;
204cf 0a 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69 6e  .    pFunc->xFin
204d0 61 6c 69 7a 65 28 26 63 74 78 29 3b 20 2f 2a 20  alize(&ctx); /* 
204d1 49 4d 50 3a 20 52 2d 32 34 35 30 35 2d 32 33 32  IMP: R-24505-232
204d2 33 30 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  30 */.    assert
204d3 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67  ( 0==(pMem->flag
204d4 73 26 4d 45 4d 5f 44 79 6e 29 20 26 26 20 21 70  s&MEM_Dyn) && !p
204d5 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20  Mem->xDel );.   
204d6 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
204d7 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a  Mem->db, pMem->z
204d8 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 6d 65 6d  Malloc);.    mem
204d9 63 70 79 28 70 4d 65 6d 2c 20 26 63 74 78 2e 73  cpy(pMem, &ctx.s
204da 2c 20 73 69 7a 65 6f 66 28 63 74 78 2e 73 29 29  , sizeof(ctx.s))
204db 3b 0a 20 20 20 20 72 63 20 3d 20 63 74 78 2e 69  ;.    rc = ctx.i
204dc 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 72 65  sError;.  }.  re
204dd 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
204de 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 79 20  * If the memory 
204df 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 61 20  cell contains a 
204e0 73 74 72 69 6e 67 20 76 61 6c 75 65 20 74 68 61  string value tha
204e1 74 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20  t must be freed 
204e2 62 79 0a 2a 2a 20 69 6e 76 6f 6b 69 6e 67 20 61  by.** invoking a
204e3 6e 20 65 78 74 65 72 6e 61 6c 20 63 61 6c 6c 62  n external callb
204e4 61 63 6b 2c 20 66 72 65 65 20 69 74 20 6e 6f 77  ack, free it now
204e5 2e 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 66  . Calling this f
204e6 75 6e 63 74 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20  unction.** does 
204e7 6e 6f 74 20 66 72 65 65 20 61 6e 79 20 4d 65 6d  not free any Mem
204e8 2e 7a 4d 61 6c 6c 6f 63 20 62 75 66 66 65 72 2e  .zMalloc buffer.
204e9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
204ea 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
204eb 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74  dbeMemReleaseExt
204ec 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 7b 0a 20  ernal(Mem *p){. 
204ed 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 3d 3d   assert( p->db==
204ee 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
204ef 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
204f0 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
204f1 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20  ->flags&MEM_Agg 
204f2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
204f3 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c  beMemFinalize(p,
204f4 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20   p->u.pDef);.   
204f5 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61   assert( (p->fla
204f6 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30  gs & MEM_Agg)==0
204f7 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   );.    sqlite3V
204f8 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 29  dbeMemRelease(p)
204f9 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  ;.  }else if( p-
204fa 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 20 26  >flags&MEM_Dyn &
204fb 26 20 70 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20  & p->xDel ){.   
204fc 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61   assert( (p->fla
204fd 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d  gs&MEM_RowSet)==
204fe 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
204ff 20 70 2d 3e 78 44 65 6c 21 3d 53 51 4c 49 54 45   p->xDel!=SQLITE
20500 5f 44 59 4e 41 4d 49 43 20 29 3b 0a 20 20 20 20  _DYNAMIC );.    
20501 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29  p->xDel((void *)
20502 70 2d 3e 7a 29 3b 0a 20 20 20 20 70 2d 3e 78 44  p->z);.    p->xD
20503 65 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20  el = 0;.  }else 
20504 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d  if( p->flags&MEM
20505 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 73  _RowSet ){.    s
20506 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61  qlite3RowSetClea
20507 72 28 70 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b  r(p->u.pRowSet);
20508 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e  .  }else if( p->
20509 66 6c 61 67 73 26 4d 45 4d 5f 46 72 61 6d 65 20  flags&MEM_Frame 
2050a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2050b 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 29 3b  beMemSetNull(p);
2050c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
2050d 6c 65 61 73 65 20 61 6e 79 20 6d 65 6d 6f 72 79  lease any memory
2050e 20 68 65 6c 64 20 62 79 20 74 68 65 20 4d 65 6d   held by the Mem
2050f 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 76 65  . This may leave
20510 20 74 68 65 20 4d 65 6d 20 69 6e 20 61 6e 0a 2a   the Mem in an.*
20511 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73  * inconsistent s
20512 74 61 74 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c  tate, for exampl
20513 65 20 77 69 74 68 20 28 4d 65 6d 2e 7a 3d 3d 30  e with (Mem.z==0
20514 29 20 61 6e 64 0a 2a 2a 20 28 4d 65 6d 2e 74 79  ) and.** (Mem.ty
20515 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 29  pe==SQLITE_TEXT)
20516 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20517 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
20518 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d  VdbeMemRelease(M
20519 65 6d 20 2a 70 29 7b 0a 20 20 56 64 62 65 4d 65  em *p){.  VdbeMe
2051a 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a 20 20 73  mRelease(p);.  s
2051b 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e  qlite3DbFree(p->
2051c 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b  db, p->zMalloc);
2051d 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a 20 20 70  .  p->z = 0;.  p
2051e 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20  ->zMalloc = 0;. 
2051f 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 7d 0a   p->xDel = 0;.}.
20520 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61  ./*.** Convert a
20521 20 36 34 2d 62 69 74 20 49 45 45 45 20 64 6f 75   64-bit IEEE dou
20522 62 6c 65 20 69 6e 74 6f 20 61 20 36 34 2d 62 69  ble into a 64-bi
20523 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
20524 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f 75 62  ..** If the doub
20525 6c 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c  le is too large,
20526 20 72 65 74 75 72 6e 20 30 78 38 30 30 30 30 30   return 0x800000
20527 30 30 30 30 30 30 30 30 30 30 2e 0a 2a 2a 0a 2a  0000000000..**.*
20528 2a 20 4d 6f 73 74 20 73 79 73 74 65 6d 73 20 61  * Most systems a
20529 70 70 65 61 72 20 74 6f 20 64 6f 20 74 68 69 73  ppear to do this
2052a 20 73 69 6d 70 6c 79 20 62 79 20 61 73 73 69 67   simply by assig
2052b 6e 69 6e 67 0a 2a 2a 20 76 61 72 69 61 62 6c 65  ning.** variable
2052c 73 20 61 6e 64 20 77 69 74 68 6f 75 74 20 74 68  s and without th
2052d 65 20 65 78 74 72 61 20 72 61 6e 67 65 20 74 65  e extra range te
2052e 73 74 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65  sts.  But.** the
2052f 72 65 20 61 72 65 20 72 65 70 6f 72 74 73 20 74  re are reports t
20530 68 61 74 20 77 69 6e 64 6f 77 73 20 74 68 72 6f  hat windows thro
20531 77 73 20 61 6e 20 65 78 70 65 63 74 69 6f 6e 0a  ws an expection.
20532 2a 2a 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69  ** if the floati
20533 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69  ng point value i
20534 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20  s out of range. 
20535 28 53 65 65 20 74 69 63 6b 65 74 20 23 32 38 38  (See ticket #288
20536 30 2e 29 0a 2a 2a 20 42 65 63 61 75 73 65 20 77  0.).** Because w
20537 65 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 6c 65 74  e do not complet
20538 65 6c 79 20 75 6e 64 65 72 73 74 61 6e 64 20 74  ely understand t
20539 68 65 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20 77  he problem, we w
2053a 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20  ill.** take the 
2053b 63 6f 6e 73 65 72 76 61 74 69 76 65 20 61 70 70  conservative app
2053c 72 6f 61 63 68 20 61 6e 64 20 61 6c 77 61 79 73  roach and always
2053d 20 64 6f 20 72 61 6e 67 65 20 74 65 73 74 73 0a   do range tests.
2053e 2a 2a 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70  ** before attemp
2053f 74 69 6e 67 20 74 68 65 20 63 6f 6e 76 65 72 73  ting the convers
20540 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
20541 36 34 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34  64 doubleToInt64
20542 28 64 6f 75 62 6c 65 20 72 29 7b 0a 23 69 66 64  (double r){.#ifd
20543 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
20544 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
20545 2f 2a 20 57 68 65 6e 20 66 6c 6f 61 74 69 6e 67  /* When floating
20546 2d 70 6f 69 6e 74 20 69 73 20 6f 6d 69 74 74 65  -point is omitte
20547 64 2c 20 64 6f 75 62 6c 65 20 61 6e 64 20 69 6e  d, double and in
20548 74 36 34 20 61 72 65 20 74 68 65 20 73 61 6d 65  t64 are the same
20549 20 74 68 69 6e 67 20 2a 2f 0a 20 20 72 65 74 75   thing */.  retu
2054a 72 6e 20 72 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a  rn r;.#else.  /*
2054b 0a 20 20 2a 2a 20 4d 61 6e 79 20 63 6f 6d 70 69  .  ** Many compi
2054c 6c 65 72 73 20 77 65 20 65 6e 63 6f 75 6e 74 65  lers we encounte
2054d 72 20 64 6f 20 6e 6f 74 20 64 65 66 69 6e 65 20  r do not define 
2054e 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68  constants for th
2054f 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75 6d 20 61  e.  ** minimum a
20550 6e 64 20 6d 61 78 69 6d 75 6d 20 36 34 2d 62 69  nd maximum 64-bi
20551 74 20 69 6e 74 65 67 65 72 73 2c 20 6f 72 20 74  t integers, or t
20552 68 65 79 20 64 65 66 69 6e 65 20 74 68 65 6d 0a  hey define them.
20553 20 20 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e    ** inconsisten
20554 74 6c 79 2e 20 20 41 6e 64 20 6d 61 6e 79 20 64  tly.  And many d
20555 6f 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64  o not understand
20556 20 74 68 65 20 22 4c 4c 22 20 6e 6f 74 61 74 69   the "LL" notati
20557 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77 65 20 64  on..  ** So we d
20558 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e 20 73 74  efine our own st
20559 61 74 69 63 20 63 6f 6e 73 74 61 6e 74 73 20 68  atic constants h
2055a 65 72 65 20 75 73 69 6e 67 20 6e 6f 74 68 69 6e  ere using nothin
2055b 67 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68  g.  ** larger th
2055c 61 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65  an a 32-bit inte
2055d 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e 0a 20 20  ger constant..  
2055e 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  */.  static cons
2055f 74 20 69 36 34 20 6d 61 78 49 6e 74 20 3d 20 4c  t i64 maxInt = L
20560 41 52 47 45 53 54 5f 49 4e 54 36 34 3b 0a 20 20  ARGEST_INT64;.  
20561 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 36 34  static const i64
20562 20 6d 69 6e 49 6e 74 20 3d 20 53 4d 41 4c 4c 45   minInt = SMALLE
20563 53 54 5f 49 4e 54 36 34 3b 0a 0a 20 20 69 66 28  ST_INT64;..  if(
20564 20 72 3c 28 64 6f 75 62 6c 65 29 6d 69 6e 49 6e   r<(double)minIn
20565 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
20566 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 20  minInt;.  }else 
20567 69 66 28 20 72 3e 28 64 6f 75 62 6c 65 29 6d 61  if( r>(double)ma
20568 78 49 6e 74 20 29 7b 0a 20 20 20 20 2f 2a 20 6d  xInt ){.    /* m
20569 69 6e 49 6e 74 20 69 73 20 63 6f 72 72 65 63 74  inInt is correct
2056a 20 68 65 72 65 20 2d 20 6e 6f 74 20 6d 61 78 49   here - not maxI
2056b 6e 74 2e 20 20 49 74 20 74 75 72 6e 73 20 6f 75  nt.  It turns ou
2056c 74 20 74 68 61 74 20 61 73 73 69 67 6e 69 6e 67  t that assigning
2056d 0a 20 20 20 20 2a 2a 20 61 20 76 65 72 79 20 6c  .    ** a very l
2056e 61 72 67 65 20 70 6f 73 69 74 69 76 65 20 6e 75  arge positive nu
2056f 6d 62 65 72 20 74 6f 20 61 6e 20 69 6e 74 65 67  mber to an integ
20570 65 72 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20  er results in a 
20571 76 65 72 79 20 6c 61 72 67 65 0a 20 20 20 20 2a  very large.    *
20572 2a 20 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67  * negative integ
20573 65 72 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20  er.  This makes 
20574 6e 6f 20 73 65 6e 73 65 2c 20 62 75 74 20 69 74  no sense, but it
20575 20 69 73 20 77 68 61 74 20 78 38 36 20 68 61 72   is what x86 har
20576 64 77 61 72 65 0a 20 20 20 20 2a 2a 20 64 6f 65  dware.    ** doe
20577 73 20 73 6f 20 66 6f 72 20 63 6f 6d 70 61 74 69  s so for compati
20578 62 69 6c 69 74 79 20 77 65 20 77 69 6c 6c 20 64  bility we will d
20579 6f 20 74 68 65 20 73 61 6d 65 20 69 6e 20 73 6f  o the same in so
2057a 66 74 77 61 72 65 2e 20 2a 2f 0a 20 20 20 20 72  ftware. */.    r
2057b 65 74 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20  eturn minInt;.  
2057c 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
2057d 6e 20 28 69 36 34 29 72 3b 0a 20 20 7d 0a 23 65  n (i64)r;.  }.#e
2057e 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ndif.}../*.** Re
2057f 74 75 72 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f  turn some kind o
20580 66 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  f integer value 
20581 77 68 69 63 68 20 69 73 20 74 68 65 20 62 65 73  which is the bes
20582 74 20 77 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61  t we can do.** a
20583 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  t representing t
20584 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 2a 70  he value that *p
20585 4d 65 6d 20 64 65 73 63 72 69 62 65 73 20 61 73  Mem describes as
20586 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20   an integer..** 
20587 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e  If pMem is an in
20588 74 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20  teger, then the 
20589 76 61 6c 75 65 20 69 73 20 65 78 61 63 74 2e 20  value is exact. 
2058a 20 49 66 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61   If pMem is.** a
2058b 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20   floating-point 
2058c 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72  then the value r
2058d 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 69  eturned is the i
2058e 6e 74 65 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20  nteger part..** 
2058f 49 66 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72  If pMem is a str
20590 69 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65  ing or blob, the
20591 6e 20 77 65 20 6d 61 6b 65 20 61 6e 20 61 74 74  n we make an att
20592 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a  empt to convert.
20593 2a 2a 20 69 74 20 69 6e 74 6f 20 61 20 69 6e 74  ** it into a int
20594 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20  eger and return 
20595 74 68 61 74 2e 20 20 49 66 20 70 4d 65 6d 20 72  that.  If pMem r
20596 65 70 72 65 73 65 6e 74 73 20 61 6e 0a 2a 2a 20  epresents an.** 
20597 61 6e 20 53 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75  an SQL-NULL valu
20598 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a  e, return 0..**.
20599 2a 2a 20 49 66 20 70 4d 65 6d 20 72 65 70 72 65  ** If pMem repre
2059a 73 65 6e 74 73 20 61 20 73 74 72 69 6e 67 20 76  sents a string v
2059b 61 6c 75 65 2c 20 69 74 73 20 65 6e 63 6f 64 69  alue, its encodi
2059c 6e 67 20 6d 69 67 68 74 20 62 65 20 63 68 61 6e  ng might be chan
2059d 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ged..*/.SQLITE_P
2059e 52 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74  RIVATE i64 sqlit
2059f 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d  e3VdbeIntValue(M
205a0 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74  em *pMem){.  int
205a1 20 66 6c 61 67 73 3b 0a 20 20 61 73 73 65 72 74   flags;.  assert
205a2 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
205a3 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
205a4 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
205a5 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
205a6 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  ( EIGHT_BYTE_ALI
205a7 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a  GNMENT(pMem) );.
205a8 20 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e    flags = pMem->
205a9 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 66 6c 61  flags;.  if( fla
205aa 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a  gs & MEM_Int ){.
205ab 20 20 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d      return pMem-
205ac 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66  >u.i;.  }else if
205ad 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65  ( flags & MEM_Re
205ae 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  al ){.    return
205af 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70   doubleToInt64(p
205b0 4d 65 6d 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65  Mem->r);.  }else
205b1 20 69 66 28 20 66 6c 61 67 73 20 26 20 28 4d 45   if( flags & (ME
205b2 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20  M_Str|MEM_Blob) 
205b3 29 7b 0a 20 20 20 20 69 36 34 20 76 61 6c 75 65  ){.    i64 value
205b4 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74   = 0;.    assert
205b5 28 20 70 4d 65 6d 2d 3e 7a 20 7c 7c 20 70 4d 65  ( pMem->z || pMe
205b6 6d 2d 3e 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 74  m->n==0 );.    t
205b7 65 73 74 63 61 73 65 28 20 70 4d 65 6d 2d 3e 7a  estcase( pMem->z
205b8 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ==0 );.    sqlit
205b9 65 33 41 74 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a  e3Atoi64(pMem->z
205ba 2c 20 26 76 61 6c 75 65 2c 20 70 4d 65 6d 2d 3e  , &value, pMem->
205bb 6e 2c 20 70 4d 65 6d 2d 3e 65 6e 63 29 3b 0a 20  n, pMem->enc);. 
205bc 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 3b     return value;
205bd 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
205be 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  turn 0;.  }.}../
205bf 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
205c0 62 65 73 74 20 72 65 70 72 65 73 65 6e 74 61 74  best representat
205c1 69 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 74  ion of pMem that
205c2 20 77 65 20 63 61 6e 20 67 65 74 20 69 6e 74 6f   we can get into
205c3 20 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49   a.** double.  I
205c4 66 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64  f pMem is alread
205c5 79 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e  y a double or an
205c6 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e   integer, return
205c7 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20   its.** value.  
205c8 49 66 20 69 74 20 69 73 20 61 20 73 74 72 69 6e  If it is a strin
205c9 67 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74  g or blob, try t
205ca 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20  o convert it to 
205cb 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20  a double..** If 
205cc 69 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65  it is a NULL, re
205cd 74 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c  turn 0.0..*/.SQL
205ce 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62  ITE_PRIVATE doub
205cf 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  le sqlite3VdbeRe
205d0 61 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65  alValue(Mem *pMe
205d1 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  m){.  assert( pM
205d2 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
205d3 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
205d4 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
205d5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49   );.  assert( EI
205d6 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
205d7 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 69 66  NT(pMem) );.  if
205d8 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
205d9 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
205da 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a  return pMem->r;.
205db 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d    }else if( pMem
205dc 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
205dd 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
205de 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e  (double)pMem->u.
205df 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  i;.  }else if( p
205e0 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  Mem->flags & (ME
205e1 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20  M_Str|MEM_Blob) 
205e2 29 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c  ){.    /* (doubl
205e3 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53  e)0 In case of S
205e4 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
205e5 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a  ING_POINT... */.
205e6 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d      double val =
205e7 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20   (double)0;.    
205e8 73 71 6c 69 74 65 33 41 74 6f 46 28 70 4d 65 6d  sqlite3AtoF(pMem
205e9 2d 3e 7a 2c 20 26 76 61 6c 2c 20 70 4d 65 6d 2d  ->z, &val, pMem-
205ea 3e 6e 2c 20 70 4d 65 6d 2d 3e 65 6e 63 29 3b 0a  >n, pMem->enc);.
205eb 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a      return val;.
205ec 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
205ed 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73  (double)0 In cas
205ee 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  e of SQLITE_OMIT
205ef 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e  _FLOATING_POINT.
205f0 2e 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  .. */.    return
205f1 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a   (double)0;.  }.
205f2 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d  }../*.** The MEM
205f3 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c   structure is al
205f4 72 65 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c  ready a MEM_Real
205f5 2e 20 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d  .  Try to also m
205f6 61 6b 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f  ake it a.** MEM_
205f7 49 6e 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a  Int if we can..*
205f8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
205f9 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
205fa 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79  eIntegerAffinity
205fb 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61  (Mem *pMem){.  a
205fc 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61  ssert( pMem->fla
205fd 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b  gs & MEM_Real );
205fe 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
205ff 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
20600 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73  wSet)==0 );.  as
20601 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d  sert( pMem->db==
20602 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
20603 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62  ex_held(pMem->db
20604 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
20605 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45  sert( EIGHT_BYTE
20606 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29  _ALIGNMENT(pMem)
20607 20 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69   );..  pMem->u.i
20608 20 3d 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34   = doubleToInt64
20609 28 70 4d 65 6d 2d 3e 72 29 3b 0a 0a 20 20 2f 2a  (pMem->r);..  /*
2060a 20 4f 6e 6c 79 20 6d 61 72 6b 20 74 68 65 20 76   Only mark the v
2060b 61 6c 75 65 20 61 73 20 61 6e 20 69 6e 74 65 67  alue as an integ
2060c 65 72 20 69 66 0a 20 20 2a 2a 0a 20 20 2a 2a 20  er if.  **.  ** 
2060d 20 20 20 28 31 29 20 74 68 65 20 72 6f 75 6e 64     (1) the round
2060e 2d 74 72 69 70 20 63 6f 6e 76 65 72 73 69 6f 6e  -trip conversion
2060f 20 72 65 61 6c 2d 3e 69 6e 74 2d 3e 72 65 61 6c   real->int->real
20610 20 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 61 6e 64   is a no-op, and
20611 0a 20 20 2a 2a 20 20 20 20 28 32 29 20 54 68 65  .  **    (2) The
20612 20 69 6e 74 65 67 65 72 20 69 73 20 6e 65 69 74   integer is neit
20613 68 65 72 20 74 68 65 20 6c 61 72 67 65 73 74 20  her the largest 
20614 6e 6f 72 20 74 68 65 20 73 6d 61 6c 6c 65 73 74  nor the smallest
20615 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 70 6f 73  .  **        pos
20616 73 69 62 6c 65 20 69 6e 74 65 67 65 72 20 28 74  sible integer (t
20617 69 63 6b 65 74 20 23 33 39 32 32 29 0a 20 20 2a  icket #3922).  *
20618 2a 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e  *.  ** The secon
20619 64 20 61 6e 64 20 74 68 69 72 64 20 74 65 72 6d  d and third term
2061a 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  s in the followi
2061b 6e 67 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65  ng conditional e
2061c 6e 66 6f 72 63 65 73 0a 20 20 2a 2a 20 74 68 65  nforces.  ** the
2061d 20 73 65 63 6f 6e 64 20 63 6f 6e 64 69 74 69 6f   second conditio
2061e 6e 20 75 6e 64 65 72 20 74 68 65 20 61 73 73 75  n under the assu
2061f 6d 70 74 69 6f 6e 20 74 68 61 74 20 61 64 64 69  mption that addi
20620 74 69 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 63 61  tion overflow ca
20621 75 73 65 73 0a 20 20 2a 2a 20 76 61 6c 75 65 73  uses.  ** values
20622 20 74 6f 20 77 72 61 70 20 61 72 6f 75 6e 64 2e   to wrap around.
20623 20 20 4f 6e 20 78 38 36 20 68 61 72 64 77 61 72    On x86 hardwar
20624 65 2c 20 74 68 65 20 74 68 69 72 64 20 74 65 72  e, the third ter
20625 6d 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a  m is always.  **
20626 20 74 72 75 65 20 61 6e 64 20 63 6f 75 6c 64 20   true and could 
20627 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 74  be omitted.  But
20628 20 77 65 20 6c 65 61 76 65 20 69 74 20 69 6e 20   we leave it in 
20629 62 65 63 61 75 73 65 20 6f 74 68 65 72 0a 20 20  because other.  
2062a 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72 65 73  ** architectures
2062b 20 6d 69 67 68 74 20 62 65 68 61 76 65 20 64 69   might behave di
2062c 66 66 65 72 65 6e 74 6c 79 2e 0a 20 20 2a 2f 0a  fferently..  */.
2062d 20 20 69 66 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28    if( pMem->r==(
2062e 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69  double)pMem->u.i
2062f 0a 20 20 20 26 26 20 70 4d 65 6d 2d 3e 75 2e 69  .   && pMem->u.i
20630 3e 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 0a  >SMALLEST_INT64.
20631 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 69 34  #if defined(__i4
20632 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  86__) || defined
20633 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 0a 20 20 20  (__x86_64__).   
20634 26 26 20 41 4c 57 41 59 53 28 70 4d 65 6d 2d 3e  && ALWAYS(pMem->
20635 75 2e 69 3c 4c 41 52 47 45 53 54 5f 49 4e 54 36  u.i<LARGEST_INT6
20636 34 29 0a 23 65 6c 73 65 0a 20 20 20 26 26 20 70  4).#else.   && p
20637 4d 65 6d 2d 3e 75 2e 69 3c 4c 41 52 47 45 53 54  Mem->u.i<LARGEST
20638 5f 49 4e 54 36 34 0a 23 65 6e 64 69 66 0a 20 20  _INT64.#endif.  
20639 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ){.    pMem->fla
2063a 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  gs |= MEM_Int;. 
2063b 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76   }.}../*.** Conv
2063c 65 72 74 20 70 4d 65 6d 20 74 6f 20 74 79 70 65  ert pMem to type
2063d 20 69 6e 74 65 67 65 72 2e 20 20 49 6e 76 61 6c   integer.  Inval
2063e 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20  idate any prior 
2063f 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e  representations.
20640 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20641 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
20642 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
20643 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73  Mem *pMem){.  as
20644 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d  sert( pMem->db==
20645 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
20646 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62  ex_held(pMem->db
20647 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
20648 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
20649 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29  gs & MEM_RowSet)
2064a 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
2064b 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47   EIGHT_BYTE_ALIG
2064c 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a  NMENT(pMem) );..
2064d 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 71    pMem->u.i = sq
2064e 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
2064f 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65  e(pMem);.  MemSe
20650 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20  tTypeFlag(pMem, 
20651 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 72 65 74 75  MEM_Int);.  retu
20652 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
20653 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70  ./*.** Convert p
20654 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 69  Mem so that it i
20655 73 20 6f 66 20 74 79 70 65 20 4d 45 4d 5f 52 65  s of type MEM_Re
20656 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74  al..** Invalidat
20657 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72  e any prior repr
20658 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a  esentations..*/.
20659 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2065a 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
2065b 6d 52 65 61 6c 69 66 79 28 4d 65 6d 20 2a 70 4d  mRealify(Mem *pM
2065c 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  em){.  assert( p
2065d 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
2065e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
2065f 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
20660 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45  ) );.  assert( E
20661 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d  IGHT_BYTE_ALIGNM
20662 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20 20  ENT(pMem) );..  
20663 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c 69 74 65  pMem->r = sqlite
20664 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70  3VdbeRealValue(p
20665 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79  Mem);.  MemSetTy
20666 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d  peFlag(pMem, MEM
20667 5f 52 65 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e  _Real);.  return
20668 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
20669 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65  *.** Convert pMe
2066a 6d 20 73 6f 20 74 68 61 74 20 69 74 20 68 61 73  m so that it has
2066b 20 74 79 70 65 73 20 4d 45 4d 5f 52 65 61 6c 20   types MEM_Real 
2066c 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f 72 20 62 6f  or MEM_Int or bo
2066d 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74  th..** Invalidat
2066e 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72  e any prior repr
2066f 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a  esentations..**.
20670 2a 2a 20 45 76 65 72 79 20 65 66 66 6f 72 74 20  ** Every effort 
20671 69 73 20 6d 61 64 65 20 74 6f 20 66 6f 72 63 65  is made to force
20672 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2c   the conversion,
20673 20 65 76 65 6e 20 69 66 20 74 68 65 20 69 6e 70   even if the inp
20674 75 74 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e  ut.** is a strin
20675 67 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20  g that does not 
20676 6c 6f 6f 6b 20 63 6f 6d 70 6c 65 74 65 6c 79 20  look completely 
20677 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 2e 20 20  like a number.  
20678 43 6f 6e 76 65 72 74 0a 2a 2a 20 61 73 20 6d 75  Convert.** as mu
20679 63 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67  ch of the string
2067a 20 61 73 20 77 65 20 63 61 6e 20 61 6e 64 20 69   as we can and i
2067b 67 6e 6f 72 65 20 74 68 65 20 72 65 73 74 2e 0a  gnore the rest..
2067c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2067d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
2067e 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d  eMemNumerify(Mem
2067f 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 28   *pMem){.  if( (
20680 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d  pMem->flags & (M
20681 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c  EM_Int|MEM_Real|
20682 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 7b  MEM_Null))==0 ){
20683 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 4d  .    assert( (pM
20684 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  em->flags & (MEM
20685 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21  _Blob|MEM_Str))!
20686 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
20687 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
20688 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
20689 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
2068a 74 65 78 29 20 29 3b 0a 20 20 20 20 69 66 28 20  tex) );.    if( 
2068b 30 3d 3d 73 71 6c 69 74 65 33 41 74 6f 69 36 34  0==sqlite3Atoi64
2068c 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 6d 2d  (pMem->z, &pMem-
2068d 3e 75 2e 69 2c 20 70 4d 65 6d 2d 3e 6e 2c 20 70  >u.i, pMem->n, p
2068e 4d 65 6d 2d 3e 65 6e 63 29 20 29 7b 0a 20 20 20  Mem->enc) ){.   
2068f 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
20690 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29  g(pMem, MEM_Int)
20691 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
20692 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c     pMem->r = sql
20693 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75  ite3VdbeRealValu
20694 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 4d  e(pMem);.      M
20695 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d  emSetTypeFlag(pM
20696 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20  em, MEM_Real);. 
20697 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
20698 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28  IntegerAffinity(
20699 70 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  pMem);.    }.  }
2069a 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
2069b 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49  ->flags & (MEM_I
2069c 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f  nt|MEM_Real|MEM_
2069d 4e 75 6c 6c 29 29 21 3d 30 20 29 3b 0a 20 20 70  Null))!=0 );.  p
2069e 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  Mem->flags &= ~(
2069f 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62  MEM_Str|MEM_Blob
206a0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
206a1 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
206a2 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69  Delete any previ
206a3 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65  ous value and se
206a4 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72  t the value stor
206a5 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 4e  ed in *pMem to N
206a6 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ULL..*/.SQLITE_P
206a7 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
206a8 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c  te3VdbeMemSetNul
206a9 6c 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20  l(Mem *pMem){.  
206aa 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  if( pMem->flags 
206ab 26 20 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a 20  & MEM_Frame ){. 
206ac 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46     VdbeFrame *pF
206ad 72 61 6d 65 20 3d 20 70 4d 65 6d 2d 3e 75 2e 70  rame = pMem->u.p
206ae 46 72 61 6d 65 3b 0a 20 20 20 20 70 46 72 61 6d  Frame;.    pFram
206af 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 46 72  e->pParent = pFr
206b0 61 6d 65 2d 3e 76 2d 3e 70 44 65 6c 46 72 61 6d  ame->v->pDelFram
206b1 65 3b 0a 20 20 20 20 70 46 72 61 6d 65 2d 3e 76  e;.    pFrame->v
206b2 2d 3e 70 44 65 6c 46 72 61 6d 65 20 3d 20 70 46  ->pDelFrame = pF
206b3 72 61 6d 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rame;.  }.  if( 
206b4 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
206b5 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20  M_RowSet ){.    
206b6 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65  sqlite3RowSetCle
206b7 61 72 28 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53  ar(pMem->u.pRowS
206b8 65 74 29 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65  et);.  }.  MemSe
206b9 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20  tTypeFlag(pMem, 
206ba 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 70 4d 65  MEM_Null);.  pMe
206bb 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
206bc 5f 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  _NULL;.}../*.** 
206bd 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69  Delete any previ
206be 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65  ous value and se
206bf 74 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62  t the value to b
206c0 65 20 61 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67  e a BLOB of leng
206c1 74 68 0a 2a 2a 20 6e 20 63 6f 6e 74 61 69 6e 69  th.** n containi
206c2 6e 67 20 61 6c 6c 20 7a 65 72 6f 73 2e 0a 2a 2f  ng all zeros..*/
206c3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
206c4 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
206c5 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d  MemSetZeroBlob(M
206c6 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 29  em *pMem, int n)
206c7 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  {.  sqlite3VdbeM
206c8 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b  emRelease(pMem);
206c9 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d  .  pMem->flags =
206ca 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65   MEM_Blob|MEM_Ze
206cb 72 6f 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65  ro;.  pMem->type
206cc 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a   = SQLITE_BLOB;.
206cd 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 30 3b 0a 20    pMem->n = 0;. 
206ce 20 69 66 28 20 6e 3c 30 20 29 20 6e 20 3d 20 30   if( n<0 ) n = 0
206cf 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72  ;.  pMem->u.nZer
206d0 6f 20 3d 20 6e 3b 0a 20 20 70 4d 65 6d 2d 3e 65  o = n;.  pMem->e
206d1 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38  nc = SQLITE_UTF8
206d2 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
206d3 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20  _OMIT_INCRBLOB. 
206d4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
206d5 72 6f 77 28 70 4d 65 6d 2c 20 6e 2c 20 30 29 3b  row(pMem, n, 0);
206d6 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 29  .  if( pMem->z )
206d7 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20  {.    pMem->n = 
206d8 6e 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 4d  n;.    memset(pM
206d9 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 29 3b 0a 20 20  em->z, 0, n);.  
206da 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
206db 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65  * Delete any pre
206dc 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20  vious value and 
206dd 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74  set the value st
206de 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f  ored in *pMem to
206df 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73   val,.** manifes
206e0 74 20 74 79 70 65 20 49 4e 54 45 47 45 52 2e 0a  t type INTEGER..
206e1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
206e2 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
206e3 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d 65  beMemSetInt64(Me
206e4 6d 20 2a 70 4d 65 6d 2c 20 69 36 34 20 76 61 6c  m *pMem, i64 val
206e5 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ){.  sqlite3Vdbe
206e6 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29  MemRelease(pMem)
206e7 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20  ;.  pMem->u.i = 
206e8 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61  val;.  pMem->fla
206e9 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
206ea 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
206eb 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 7d 0a 0a  ITE_INTEGER;.}..
206ec 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
206ed 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
206ee 4e 54 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  NT./*.** Delete 
206ef 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c  any previous val
206f0 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76  ue and set the v
206f1 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a  alue stored in *
206f2 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20  pMem to val,.** 
206f3 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 52 45  manifest type RE
206f4 41 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  AL..*/.SQLITE_PR
206f5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
206f6 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62  e3VdbeMemSetDoub
206f7 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64 6f  le(Mem *pMem, do
206f8 75 62 6c 65 20 76 61 6c 29 7b 0a 20 20 69 66 28  uble val){.  if(
206f9 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61   sqlite3IsNaN(va
206fa 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  l) ){.    sqlite
206fb 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
206fc 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  pMem);.  }else{.
206fd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
206fe 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b  emRelease(pMem);
206ff 0a 20 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 76  .    pMem->r = v
20700 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  al;.    pMem->fl
20701 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a  ags = MEM_Real;.
20702 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
20703 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20   SQLITE_FLOAT;. 
20704 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   }.}.#endif../*.
20705 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72  ** Delete any pr
20706 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64  evious value and
20707 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f   set the value o
20708 66 20 70 4d 65 6d 20 74 6f 20 62 65 20 61 6e 0a  f pMem to be an.
20709 2a 2a 20 65 6d 70 74 79 20 62 6f 6f 6c 65 61 6e  ** empty boolean
2070a 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54   index..*/.SQLIT
2070b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2070c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
2070d 52 6f 77 53 65 74 28 4d 65 6d 20 2a 70 4d 65 6d  RowSet(Mem *pMem
2070e 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
2070f 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 61   = pMem->db;.  a
20710 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a  ssert( db!=0 );.
20711 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
20712 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
20713 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c  Set)==0 );.  sql
20714 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
20715 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d  se(pMem);.  pMem
20716 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69  ->zMalloc = sqli
20717 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
20718 62 2c 20 36 34 29 3b 0a 20 20 69 66 28 20 64 62  b, 64);.  if( db
20719 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2071a 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  {.    pMem->flag
2071b 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
2071c 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
2071d 74 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63  t( pMem->zMalloc
2071e 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e   );.    pMem->u.
2071f 70 52 6f 77 53 65 74 20 3d 20 73 71 6c 69 74 65  pRowSet = sqlite
20720 33 52 6f 77 53 65 74 49 6e 69 74 28 64 62 2c 20  3RowSetInit(db, 
20721 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a  pMem->zMalloc, .
20722 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20723 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20724 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
20725 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70  MallocSize(db, p
20726 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 3b 0a  Mem->zMalloc));.
20727 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
20728 2d 3e 75 2e 70 52 6f 77 53 65 74 21 3d 30 20 29  ->u.pRowSet!=0 )
20729 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ;.    pMem->flag
2072a 73 20 3d 20 4d 45 4d 5f 52 6f 77 53 65 74 3b 0a  s = MEM_RowSet;.
2072b 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
2072c 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
2072d 4d 65 6d 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61  Mem object conta
2072e 69 6e 73 20 61 20 54 45 58 54 20 6f 72 20 42 4c  ins a TEXT or BL
2072f 4f 42 20 74 68 61 74 20 69 73 0a 2a 2a 20 74 6f  OB that is.** to
20730 6f 20 6c 61 72 67 65 20 2d 20 77 68 6f 73 65 20  o large - whose 
20731 73 69 7a 65 20 65 78 63 65 65 64 73 20 53 51 4c  size exceeds SQL
20732 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a  ITE_MAX_LENGTH..
20733 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20734 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
20735 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 20 2a  eMemTooBig(Mem *
20736 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p){.  assert( p-
20737 3e 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  >db!=0 );.  if( 
20738 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  p->flags & (MEM_
20739 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b  Str|MEM_Blob) ){
2073a 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e  .    int n = p->
2073b 6e 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c  n;.    if( p->fl
2073c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29  ags & MEM_Zero )
2073d 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70 2d 3e  {.      n += p->
2073e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 0a 20  u.nZero;.    }. 
2073f 20 20 20 72 65 74 75 72 6e 20 6e 3e 70 2d 3e 64     return n>p->d
20740 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
20741 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a  _LIMIT_LENGTH];.
20742 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 20    }.  return 0; 
20743 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
20744 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68  E_DEBUG./*.** Th
20745 69 73 20 72 6f 75 74 69 6e 65 20 70 72 65 70 61  is routine prepa
20746 72 65 73 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c  res a memory cel
20747 6c 20 66 6f 72 20 6d 6f 64 69 63 61 74 69 6f 6e  l for modication
20748 20 62 79 20 62 72 65 61 6b 69 6e 67 0a 2a 2a 20   by breaking.** 
20749 69 74 73 20 6c 69 6e 6b 20 74 6f 20 61 20 73 68  its link to a sh
2074a 61 6c 6c 6f 77 20 63 6f 70 79 20 61 6e 64 20 62  allow copy and b
2074b 79 20 6d 61 72 6b 69 6e 67 20 61 6e 79 20 63 75  y marking any cu
2074c 72 72 65 6e 74 20 73 68 61 6c 6c 6f 77 0a 2a 2a  rrent shallow.**
2074d 20 63 6f 70 69 65 73 20 6f 66 20 74 68 69 73 20   copies of this 
2074e 63 65 6c 6c 20 61 73 20 69 6e 76 61 6c 69 64 2e  cell as invalid.
2074f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 75  .**.** This is u
20750 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20  sed for testing 
20751 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e  and debugging on
20752 6c 79 20 2d 20 74 6f 20 6d 61 6b 65 20 73 75 72  ly - to make sur
20753 65 20 73 68 61 6c 6c 6f 77 0a 2a 2a 20 63 6f 70  e shallow.** cop
20754 69 65 73 20 61 72 65 20 6e 6f 74 20 6d 69 73 75  ies are not misu
20755 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  sed..*/.SQLITE_P
20756 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
20757 74 65 33 56 64 62 65 4d 65 6d 41 62 6f 75 74 54  te3VdbeMemAboutT
20758 6f 43 68 61 6e 67 65 28 56 64 62 65 20 2a 70 56  oChange(Vdbe *pV
20759 64 62 65 2c 20 4d 65 6d 20 2a 70 4d 65 6d 29 7b  dbe, Mem *pMem){
2075a 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20  .  int i;.  Mem 
2075b 2a 70 58 3b 0a 20 20 66 6f 72 28 69 3d 31 2c 20  *pX;.  for(i=1, 
2075c 70 58 3d 26 70 56 64 62 65 2d 3e 61 4d 65 6d 5b  pX=&pVdbe->aMem[
2075d 31 5d 3b 20 69 3c 3d 70 56 64 62 65 2d 3e 6e 4d  1]; i<=pVdbe->nM
2075e 65 6d 3b 20 69 2b 2b 2c 20 70 58 2b 2b 29 7b 0a  em; i++, pX++){.
2075f 20 20 20 20 69 66 28 20 70 58 2d 3e 70 53 63 6f      if( pX->pSco
20760 70 79 46 72 6f 6d 3d 3d 70 4d 65 6d 20 29 7b 0a  pyFrom==pMem ){.
20761 20 20 20 20 20 20 70 58 2d 3e 66 6c 61 67 73 20        pX->flags 
20762 7c 3d 20 4d 45 4d 5f 49 6e 76 61 6c 69 64 3b 0a  |= MEM_Invalid;.
20763 20 20 20 20 20 20 70 58 2d 3e 70 53 63 6f 70 79        pX->pScopy
20764 46 72 6f 6d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  From = 0;.    }.
20765 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 70 53 63 6f    }.  pMem->pSco
20766 70 79 46 72 6f 6d 20 3d 20 30 3b 0a 7d 0a 23 65  pyFrom = 0;.}.#e
20767 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44  ndif /* SQLITE_D
20768 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53  EBUG */../*.** S
20769 69 7a 65 20 6f 66 20 73 74 72 75 63 74 20 4d 65  ize of struct Me
2076a 6d 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20  m not including 
2076b 74 68 65 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20  the Mem.zMalloc 
2076c 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66 69  member..*/.#defi
2076d 6e 65 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 20 28  ne MEMCELLSIZE (
2076e 73 69 7a 65 5f 74 29 28 26 28 28 28 4d 65 6d 20  size_t)(&(((Mem 
2076f 2a 29 30 29 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 0a  *)0)->zMalloc)).
20770 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 6e 20 73  ./*.** Make an s
20771 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20 70  hallow copy of p
20772 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20  From into pTo.  
20773 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f  Prior contents o
20774 66 0a 2a 2a 20 70 54 6f 20 61 72 65 20 66 72 65  f.** pTo are fre
20775 65 64 2e 20 20 54 68 65 20 70 46 72 6f 6d 2d 3e  ed.  The pFrom->
20776 7a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 64  z field is not d
20777 75 70 6c 69 63 61 74 65 64 2e 20 20 49 66 0a 2a  uplicated.  If.*
20778 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69 73 20 75 73  * pFrom->z is us
20779 65 64 2c 20 74 68 65 6e 20 70 54 6f 2d 3e 7a 20  ed, then pTo->z 
2077a 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61  points to the sa
2077b 6d 65 20 74 68 69 6e 67 20 61 73 20 70 46 72 6f  me thing as pFro
2077c 6d 2d 3e 7a 0a 2a 2a 20 61 6e 64 20 66 6c 61 67  m->z.** and flag
2077d 73 20 67 65 74 73 20 73 72 63 54 79 70 65 20 28  s gets srcType (
2077e 65 69 74 68 65 72 20 4d 45 4d 5f 45 70 68 65 6d  either MEM_Ephem
2077f 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 2e   or MEM_Static).
20780 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20781 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
20782 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70  dbeMemShallowCop
20783 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73  y(Mem *pTo, cons
20784 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 2c 20 69 6e  t Mem *pFrom, in
20785 74 20 73 72 63 54 79 70 65 29 7b 0a 20 20 61 73  t srcType){.  as
20786 73 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e 66 6c  sert( (pFrom->fl
20787 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74  ags & MEM_RowSet
20788 29 3d 3d 30 20 29 3b 0a 20 20 56 64 62 65 4d 65  )==0 );.  VdbeMe
20789 6d 52 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a 20  mRelease(pTo);. 
2078a 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 72   memcpy(pTo, pFr
2078b 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 29  om, MEMCELLSIZE)
2078c 3b 0a 20 20 70 54 6f 2d 3e 78 44 65 6c 20 3d 20  ;.  pTo->xDel = 
2078d 30 3b 0a 20 20 69 66 28 20 28 70 46 72 6f 6d 2d  0;.  if( (pFrom-
2078e 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69  >flags&MEM_Stati
2078f 63 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 54 6f  c)==0 ){.    pTo
20790 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d  ->flags &= ~(MEM
20791 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c  _Dyn|MEM_Static|
20792 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20  MEM_Ephem);.    
20793 61 73 73 65 72 74 28 20 73 72 63 54 79 70 65 3d  assert( srcType=
20794 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c 20 73 72  =MEM_Ephem || sr
20795 63 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 61 74 69  cType==MEM_Stati
20796 63 20 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c  c );.    pTo->fl
20797 61 67 73 20 7c 3d 20 73 72 63 54 79 70 65 3b 0a  ags |= srcType;.
20798 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b    }.}../*.** Mak
20799 65 20 61 20 66 75 6c 6c 20 63 6f 70 79 20 6f 66  e a full copy of
2079a 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e   pFrom into pTo.
2079b 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73    Prior contents
2079c 20 6f 66 20 70 54 6f 20 61 72 65 0a 2a 2a 20 66   of pTo are.** f
2079d 72 65 65 64 20 62 65 66 6f 72 65 20 74 68 65 20  reed before the 
2079e 63 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2f  copy is made..*/
2079f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
207a0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
207a1 65 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c  emCopy(Mem *pTo,
207a2 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f   const Mem *pFro
207a3 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  m){.  int rc = S
207a4 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73  QLITE_OK;..  ass
207a5 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61  ert( (pFrom->fla
207a6 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29  gs & MEM_RowSet)
207a7 3d 3d 30 20 29 3b 0a 20 20 56 64 62 65 4d 65 6d  ==0 );.  VdbeMem
207a8 52 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a 20 20  Release(pTo);.  
207a9 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 6f  memcpy(pTo, pFro
207aa 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 29 3b  m, MEMCELLSIZE);
207ab 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d  .  pTo->flags &=
207ac 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20 69 66   ~MEM_Dyn;..  if
207ad 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26 28 4d 45  ( pTo->flags&(ME
207ae 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20  M_Str|MEM_Blob) 
207af 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70  ){.    if( 0==(p
207b0 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  From->flags&MEM_
207b1 53 74 61 74 69 63 29 20 29 7b 0a 20 20 20 20 20  Static) ){.     
207b2 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d   pTo->flags |= M
207b3 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20  EM_Ephem;.      
207b4 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
207b5 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65  MemMakeWriteable
207b6 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  (pTo);.    }.  }
207b7 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
207b8 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72  ../*.** Transfer
207b9 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
207ba 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 2e 20 41   pFrom to pTo. A
207bb 6e 79 20 65 78 69 73 74 69 6e 67 20 76 61 6c 75  ny existing valu
207bc 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a 2a 20 66  e in pTo is.** f
207bd 72 65 65 64 2e 20 49 66 20 70 46 72 6f 6d 20 63  reed. If pFrom c
207be 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d 65 72 61  ontains ephemera
207bf 6c 20 64 61 74 61 2c 20 61 20 63 6f 70 79 20 69  l data, a copy i
207c0 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 70 46  s made..**.** pF
207c1 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20  rom contains an 
207c2 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e 20 74 68  SQL NULL when th
207c3 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
207c4 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ns..*/.SQLITE_PR
207c5 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
207c6 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65  e3VdbeMemMove(Me
207c7 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70 46 72  m *pTo, Mem *pFr
207c8 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  om){.  assert( p
207c9 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  From->db==0 || s
207ca 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
207cb 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d 75 74  d(pFrom->db->mut
207cc 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
207cd 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73   pTo->db==0 || s
207ce 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
207cf 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78  d(pTo->db->mutex
207d0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
207d1 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70  From->db==0 || p
207d2 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 46 72  To->db==0 || pFr
207d3 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64 62 20  om->db==pTo->db 
207d4 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62  );..  sqlite3Vdb
207d5 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 54 6f 29  eMemRelease(pTo)
207d6 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20  ;.  memcpy(pTo, 
207d7 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 4d 65  pFrom, sizeof(Me
207d8 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e 66 6c  m));.  pFrom->fl
207d9 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
207da 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c 20 3d 20    pFrom->xDel = 
207db 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d 61 6c  0;.  pFrom->zMal
207dc 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  loc = 0;.}../*.*
207dd 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c  * Change the val
207de 75 65 20 6f 66 20 61 20 4d 65 6d 20 74 6f 20 62  ue of a Mem to b
207df 65 20 61 20 73 74 72 69 6e 67 20 6f 72 20 61 20  e a string or a 
207e0 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  BLOB..**.** The 
207e1 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e  memory managemen
207e2 74 20 73 74 72 61 74 65 67 79 20 64 65 70 65 6e  t strategy depen
207e3 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20  ds on the value 
207e4 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a 2a 20 70  of the xDel.** p
207e5 61 72 61 6d 65 74 65 72 2e 20 49 66 20 74 68 65  arameter. If the
207e6 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 69 73   value passed is
207e7 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
207e8 54 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20  T, then the .** 
207e9 73 74 72 69 6e 67 20 69 73 20 63 6f 70 69 65 64  string is copied
207ea 20 69 6e 74 6f 20 61 20 28 70 6f 73 73 69 62 6c   into a (possibl
207eb 79 20 65 78 69 73 74 69 6e 67 29 20 62 75 66 66  y existing) buff
207ec 65 72 20 6d 61 6e 61 67 65 64 20 62 79 20 74 68  er managed by th
207ed 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72 75 63 74  e .** Mem struct
207ee 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ure. Otherwise, 
207ef 61 6e 79 20 65 78 69 73 74 69 6e 67 20 62 75 66  any existing buf
207f0 66 65 72 20 69 73 20 66 72 65 65 64 20 61 6e 64  fer is freed and
207f1 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20   the.** pointer 
207f2 63 6f 70 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  copied..**.** If
207f3 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 74   the string is t
207f4 6f 6f 20 6c 61 72 67 65 20 28 69 66 20 69 74 20  oo large (if it 
207f5 65 78 63 65 65 64 73 20 74 68 65 20 53 51 4c 49  exceeds the SQLI
207f6 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 0a  TE_LIMIT_LENGTH.
207f7 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74 29 20 74  ** size limit) t
207f8 68 65 6e 20 6e 6f 20 6d 65 6d 6f 72 79 20 61 6c  hen no memory al
207f9 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72 73 2e  location occurs.
207fa 20 20 49 66 20 74 68 65 20 73 74 72 69 6e 67 20    If the string 
207fb 63 61 6e 20 62 65 0a 2a 2a 20 73 74 6f 72 65 64  can be.** stored
207fc 20 77 69 74 68 6f 75 74 20 61 6c 6c 6f 63 61 74   without allocat
207fd 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 74 68 65 6e  ing memory, then
207fe 20 69 74 20 69 73 2e 20 20 49 66 20 61 20 6d 65   it is.  If a me
207ff 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a  mory allocation.
20800 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20 74  ** is required t
20801 6f 20 73 74 6f 72 65 20 74 68 65 20 73 74 72 69  o store the stri
20802 6e 67 2c 20 74 68 65 6e 20 76 61 6c 75 65 20 6f  ng, then value o
20803 66 20 70 4d 65 6d 20 69 73 20 75 6e 63 68 61 6e  f pMem is unchan
20804 67 65 64 2e 20 20 49 6e 0a 2a 2a 20 65 69 74 68  ged.  In.** eith
20805 65 72 20 63 61 73 65 2c 20 53 51 4c 49 54 45 5f  er case, SQLITE_
20806 54 4f 4f 42 49 47 20 69 73 20 72 65 74 75 72 6e  TOOBIG is return
20807 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
20808 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
20809 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 0a  3VdbeMemSetStr(.
2080a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 20    Mem *pMem,    
2080b 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20        /* Memory 
2080c 63 65 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20 73  cell to set to s
2080d 74 72 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a 20  tring value */. 
2080e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
2080f 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 70       /* String p
20810 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ointer */.  int 
20811 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
20812 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 74 72 69  /* Bytes in stri
20813 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20  ng, or negative 
20814 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20  */.  u8 enc,    
20815 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f           /* Enco
20816 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f  ding of z.  0 fo
20817 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69  r BLOBs */.  voi
20818 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  d (*xDel)(void*)
20819 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66   /* Destructor f
2081a 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20  unction */.){.  
2081b 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20 20  int nByte = n;  
2081c 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65      /* New value
2081d 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f 0a   for pMem->n */.
2081e 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20 20    int iLimit;   
2081f 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
20820 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20   allowed string 
20821 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f 0a  or blob size */.
20822 20 20 75 31 36 20 66 6c 61 67 73 20 3d 20 30 3b    u16 flags = 0;
20823 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c        /* New val
20824 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c 61  ue for pMem->fla
20825 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  gs */..  assert(
20826 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pMem->db==0 || 
20827 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
20828 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74  ld(pMem->db->mut
20829 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
2082a 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20   (pMem->flags & 
2082b 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
2082c 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73 20  ;..  /* If z is 
2082d 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  a NULL pointer, 
2082e 73 65 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e 74  set pMem to cont
2082f 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e  ain an SQL NULL.
20830 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a   */.  if( !z ){.
20831 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
20832 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b  emSetNull(pMem);
20833 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
20834 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66  TE_OK;.  }..  if
20835 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 20  ( pMem->db ){.  
20836 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d 2d    iLimit = pMem-
20837 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
20838 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
20839 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
2083a 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 4d  Limit = SQLITE_M
2083b 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a 20  AX_LENGTH;.  }. 
2083c 20 66 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d 30   flags = (enc==0
2083d 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74  ?MEM_Blob:MEM_St
2083e 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c  r);.  if( nByte<
2083f 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
20840 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20 69   enc!=0 );.    i
20841 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  f( enc==SQLITE_U
20842 54 46 38 20 29 7b 0a 20 20 20 20 20 20 66 6f 72  TF8 ){.      for
20843 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c  (nByte=0; nByte<
20844 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42 79  =iLimit && z[nBy
20845 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a  te]; nByte++){}.
20846 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
20847 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42   for(nByte=0; nB
20848 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 28  yte<=iLimit && (
20849 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 79  z[nByte] | z[nBy
2084a 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d 32  te+1]); nByte+=2
2084b 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6c  ){}.    }.    fl
2084c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b  ags |= MEM_Term;
2084d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66  .  }..  /* The f
2084e 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73  ollowing block s
2084f 65 74 73 20 74 68 65 20 6e 65 77 20 76 61 6c 75  ets the new valu
20850 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 20  es of Mem.z and 
20851 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20 2a  Mem.xDel. It.  *
20852 2a 20 61 6c 73 6f 20 73 65 74 73 20 61 20 66 6c  * also sets a fl
20853 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69  ag in local vari
20854 61 62 6c 65 20 22 66 6c 61 67 73 22 20 74 6f 20  able "flags" to 
20855 69 6e 64 69 63 61 74 65 20 74 68 65 20 6d 65 6d  indicate the mem
20856 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d  ory.  ** managem
20857 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f  ent (one of MEM_
20858 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69  Dyn or MEM_Stati
20859 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 78  c)..  */.  if( x
2085a 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e  Del==SQLITE_TRAN
2085b 53 49 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e 74  SIENT ){.    int
2085c 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b   nAlloc = nByte;
2085d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 26 4d  .    if( flags&M
2085e 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20 20  EM_Term ){.     
2085f 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d   nAlloc += (enc=
20860 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32  =SQLITE_UTF8?1:2
20861 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
20862 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b   nByte>iLimit ){
20863 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
20864 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 20  LITE_TOOBIG;.   
20865 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
20866 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d  e3VdbeMemGrow(pM
20867 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 29  em, nAlloc, 0) )
20868 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
20869 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
2086a 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d   }.    memcpy(pM
2086b 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63  em->z, z, nAlloc
2086c 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78  );.  }else if( x
2086d 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e 41  Del==SQLITE_DYNA
2086e 4d 49 43 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  MIC ){.    sqlit
2086f 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
20870 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d  (pMem);.    pMem
20871 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d  ->zMalloc = pMem
20872 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b  ->z = (char *)z;
20873 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20  .    pMem->xDel 
20874 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
20875 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
20876 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20  Release(pMem);. 
20877 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68     pMem->z = (ch
20878 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d  ar *)z;.    pMem
20879 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20  ->xDel = xDel;. 
2087a 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 28 78 44     flags |= ((xD
2087b 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54 49  el==SQLITE_STATI
2087c 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d 45  C)?MEM_Static:ME
2087d 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 0a 20 20 70  M_Dyn);.  }..  p
2087e 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a  Mem->n = nByte;.
2087f 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
20880 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e 65  flags;.  pMem->e
20881 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53  nc = (enc==0 ? S
20882 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65 6e 63  QLITE_UTF8 : enc
20883 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20  );.  pMem->type 
20884 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c 49  = (enc==0 ? SQLI
20885 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54 45  TE_BLOB : SQLITE
20886 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65 66  _TEXT);..#ifndef
20887 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
20888 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65  16.  if( pMem->e
20889 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc!=SQLITE_UTF8 
2088a 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  && sqlite3VdbeMe
2088b 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d 29  mHandleBom(pMem)
2088c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
2088d 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
2088e 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e  .#endif..  if( n
2088f 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20  Byte>iLimit ){. 
20890 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
20891 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 20  _TOOBIG;.  }..  
20892 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
20893 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61  ;.}../*.** Compa
20894 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f  re the values co
20895 6e 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 74  ntained by the t
20896 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c  wo memory cells,
20897 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65   returning.** ne
20898 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20  gative, zero or 
20899 70 6f 73 69 74 69 76 65 20 69 66 20 70 4d 65 6d  positive if pMem
2089a 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20  1 is less than, 
2089b 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65  equal to, or gre
2089c 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d 65  ater.** than pMe
2089d 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64 65  m2. Sorting orde
2089e 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 69 72 73  r is NULL's firs
2089f 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e  t, followed by n
208a0 75 6d 62 65 72 73 20 28 69 6e 74 65 67 65 72 73  umbers (integers
208a1 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 29 20 73  .** and reals) s
208a2 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 61 6c 6c  orted numericall
208a3 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74  y, followed by t
208a4 65 78 74 20 6f 72 64 65 72 65 64 20 62 79 20 74  ext ordered by t
208a5 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20  he collating.** 
208a6 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 61  sequence pColl a
208a7 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 27  nd finally blob'
208a8 73 20 6f 72 64 65 72 65 64 20 62 79 20 6d 65 6d  s ordered by mem
208a9 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f  cmp()..**.** Two
208aa 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65   NULL values are
208ab 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71 75 61   considered equa
208ac 6c 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  l by this functi
208ad 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
208ae 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
208af 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73  3MemCompare(cons
208b0 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f  t Mem *pMem1, co
208b1 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20  nst Mem *pMem2, 
208b2 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70  const CollSeq *p
208b3 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b  Coll){.  int rc;
208b4 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a 20  .  int f1, f2;. 
208b5 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66 6c   int combined_fl
208b6 61 67 73 3b 0a 0a 20 20 66 31 20 3d 20 70 4d 65  ags;..  f1 = pMe
208b7 6d 31 2d 3e 66 6c 61 67 73 3b 0a 20 20 66 32 20  m1->flags;.  f2 
208b8 3d 20 70 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b 0a  = pMem2->flags;.
208b9 20 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73    combined_flags
208ba 20 3d 20 66 31 7c 66 32 3b 0a 20 20 61 73 73 65   = f1|f2;.  asse
208bb 72 74 28 20 28 63 6f 6d 62 69 6e 65 64 5f 66 6c  rt( (combined_fl
208bc 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74  ags & MEM_RowSet
208bd 29 3d 3d 30 20 29 3b 0a 20 0a 20 20 2f 2a 20 49  )==0 );. .  /* I
208be 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 4e  f one value is N
208bf 55 4c 4c 2c 20 69 74 20 69 73 20 6c 65 73 73 20  ULL, it is less 
208c0 74 68 61 6e 20 74 68 65 20 6f 74 68 65 72 2e 20  than the other. 
208c1 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 0a 20  If both values. 
208c2 20 2a 2a 20 61 72 65 20 4e 55 4c 4c 2c 20 72 65   ** are NULL, re
208c3 74 75 72 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 69  turn 0..  */.  i
208c4 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67  f( combined_flag
208c5 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  s&MEM_Null ){.  
208c6 20 20 72 65 74 75 72 6e 20 28 66 32 26 4d 45 4d    return (f2&MEM
208c7 5f 4e 75 6c 6c 29 20 2d 20 28 66 31 26 4d 45 4d  _Null) - (f1&MEM
208c8 5f 4e 75 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f  _Null);.  }..  /
208c9 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69  * If one value i
208ca 73 20 61 20 6e 75 6d 62 65 72 20 61 6e 64 20 74  s a number and t
208cb 68 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 2c  he other is not,
208cc 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20 6c   the number is l
208cd 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74  ess..  ** If bot
208ce 68 20 61 72 65 20 6e 75 6d 62 65 72 73 2c 20 63  h are numbers, c
208cf 6f 6d 70 61 72 65 20 61 73 20 72 65 61 6c 73 20  ompare as reals 
208d0 69 66 20 6f 6e 65 20 69 73 20 61 20 72 65 61 6c  if one is a real
208d1 2c 20 6f 72 20 61 73 20 69 6e 74 65 67 65 72 73  , or as integers
208d2 0a 20 20 2a 2a 20 69 66 20 62 6f 74 68 20 76 61  .  ** if both va
208d3 6c 75 65 73 20 61 72 65 20 69 6e 74 65 67 65 72  lues are integer
208d4 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f  s..  */.  if( co
208d5 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 28 4d 45  mbined_flags&(ME
208d6 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20  M_Int|MEM_Real) 
208d7 29 7b 0a 20 20 20 20 69 66 28 20 21 28 66 31 26  ){.    if( !(f1&
208d8 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61  (MEM_Int|MEM_Rea
208d9 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  l)) ){.      ret
208da 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
208db 20 69 66 28 20 21 28 66 32 26 28 4d 45 4d 5f 49   if( !(f2&(MEM_I
208dc 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b  nt|MEM_Real)) ){
208dd 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31  .      return -1
208de 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
208df 28 66 31 20 26 20 66 32 20 26 20 4d 45 4d 5f 49  (f1 & f2 & MEM_I
208e0 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  nt)==0 ){.      
208e1 64 6f 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20  double r1, r2;. 
208e2 20 20 20 20 20 69 66 28 20 28 66 31 26 4d 45 4d       if( (f1&MEM
208e3 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20  _Real)==0 ){.   
208e4 20 20 20 20 20 72 31 20 3d 20 28 64 6f 75 62 6c       r1 = (doubl
208e5 65 29 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20  e)pMem1->u.i;.  
208e6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
208e7 20 20 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72     r1 = pMem1->r
208e8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
208e9 69 66 28 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c  if( (f2&MEM_Real
208ea 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
208eb 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65  r2 = (double)pMe
208ec 6d 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d  m2->u.i;.      }
208ed 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 32  else{.        r2
208ee 20 3d 20 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 20   = pMem2->r;.   
208ef 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72     }.      if( r
208f0 31 3c 72 32 20 29 20 72 65 74 75 72 6e 20 2d 31  1<r2 ) return -1
208f1 3b 0a 20 20 20 20 20 20 69 66 28 20 72 31 3e 72  ;.      if( r1>r
208f2 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  2 ) return 1;.  
208f3 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
208f4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
208f5 73 73 65 72 74 28 20 66 31 26 4d 45 4d 5f 49 6e  ssert( f1&MEM_In
208f6 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t );.      asser
208f7 74 28 20 66 32 26 4d 45 4d 5f 49 6e 74 20 29 3b  t( f2&MEM_Int );
208f8 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31  .      if( pMem1
208f9 2d 3e 75 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 75  ->u.i < pMem2->u
208fa 2e 69 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a  .i ) return -1;.
208fb 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d        if( pMem1-
208fc 3e 75 2e 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 2e  >u.i > pMem2->u.
208fd 69 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  i ) return 1;.  
208fe 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
208ff 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
20900 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61 20   one value is a 
20901 73 74 72 69 6e 67 20 61 6e 64 20 74 68 65 20 6f  string and the o
20902 74 68 65 72 20 69 73 20 61 20 62 6c 6f 62 2c 20  ther is a blob, 
20903 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 6c 65  the string is le
20904 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68  ss..  ** If both
20905 20 61 72 65 20 73 74 72 69 6e 67 73 2c 20 63 6f   are strings, co
20906 6d 70 61 72 65 20 75 73 69 6e 67 20 74 68 65 20  mpare using the 
20907 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69  collating functi
20908 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ons..  */.  if( 
20909 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d  combined_flags&M
2090a 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 66  EM_Str ){.    if
2090b 28 20 28 66 31 20 26 20 4d 45 4d 5f 53 74 72 29  ( (f1 & MEM_Str)
2090c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
2090d 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
2090e 20 69 66 28 20 28 66 32 20 26 20 4d 45 4d 5f 53   if( (f2 & MEM_S
2090f 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  tr)==0 ){.      
20910 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d  return -1;.    }
20911 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d  ..    assert( pM
20912 65 6d 31 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 2d  em1->enc==pMem2-
20913 3e 65 6e 63 20 29 3b 0a 20 20 20 20 61 73 73 65  >enc );.    asse
20914 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d  rt( pMem1->enc==
20915 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 0a  SQLITE_UTF8 || .
20916 20 20 20 20 20 20 20 20 20 20 20 20 70 4d 65 6d              pMem
20917 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  1->enc==SQLITE_U
20918 54 46 31 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 2d  TF16LE || pMem1-
20919 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  >enc==SQLITE_UTF
2091a 31 36 42 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  16BE );..    /* 
2091b 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  The collation se
2091c 71 75 65 6e 63 65 20 6d 75 73 74 20 62 65 20 64  quence must be d
2091d 65 66 69 6e 65 64 20 61 74 20 74 68 69 73 20 70  efined at this p
2091e 6f 69 6e 74 2c 20 65 76 65 6e 20 69 66 0a 20 20  oint, even if.  
2091f 20 20 2a 2a 20 74 68 65 20 75 73 65 72 20 64 65    ** the user de
20920 6c 65 74 65 73 20 74 68 65 20 63 6f 6c 6c 61 74  letes the collat
20921 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 61 66 74  ion sequence aft
20922 65 72 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  er the vdbe prog
20923 72 61 6d 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f  ram is.    ** co
20924 6d 70 69 6c 65 64 20 28 74 68 69 73 20 77 61 73  mpiled (this was
20925 20 6e 6f 74 20 61 6c 77 61 79 73 20 74 68 65 20   not always the 
20926 63 61 73 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  case)..    */.  
20927 20 20 61 73 73 65 72 74 28 20 21 70 43 6f 6c 6c    assert( !pColl
20928 20 7c 7c 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20   || pColl->xCmp 
20929 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 43 6f 6c  );..    if( pCol
2092a 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  l ){.      if( p
2092b 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c  Mem1->enc==pColl
2092c 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20 20 20 20  ->enc ){.       
2092d 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20   /* The strings 
2092e 61 72 65 20 61 6c 72 65 61 64 79 20 69 6e 20 74  are already in t
2092f 68 65 20 63 6f 72 72 65 63 74 20 65 6e 63 6f 64  he correct encod
20930 69 6e 67 2e 20 20 43 61 6c 6c 20 74 68 65 0a 20  ing.  Call the. 
20931 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72         ** compar
20932 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 64 69  ison function di
20933 72 65 63 74 6c 79 20 2a 2f 0a 20 20 20 20 20 20  rectly */.      
20934 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 2d 3e    return pColl->
20935 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65  xCmp(pColl->pUse
20936 72 2c 70 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d 31  r,pMem1->n,pMem1
20937 2d 3e 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 65  ->z,pMem2->n,pMe
20938 6d 32 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 65  m2->z);.      }e
20939 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e  lse{.        con
2093a 73 74 20 76 6f 69 64 20 2a 76 31 2c 20 2a 76 32  st void *v1, *v2
2093b 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 31  ;.        int n1
2093c 2c 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 4d 65  , n2;.        Me
2093d 6d 20 63 31 3b 0a 20 20 20 20 20 20 20 20 4d 65  m c1;.        Me
2093e 6d 20 63 32 3b 0a 20 20 20 20 20 20 20 20 6d 65  m c2;.        me
2093f 6d 73 65 74 28 26 63 31 2c 20 30 2c 20 73 69 7a  mset(&c1, 0, siz
20940 65 6f 66 28 63 31 29 29 3b 0a 20 20 20 20 20 20  eof(c1));.      
20941 20 20 6d 65 6d 73 65 74 28 26 63 32 2c 20 30 2c    memset(&c2, 0,
20942 20 73 69 7a 65 6f 66 28 63 32 29 29 3b 0a 20 20   sizeof(c2));.  
20943 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
20944 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28  eMemShallowCopy(
20945 26 63 31 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d 5f  &c1, pMem1, MEM_
20946 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20 20 20  Ephem);.        
20947 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68  sqlite3VdbeMemSh
20948 61 6c 6c 6f 77 43 6f 70 79 28 26 63 32 2c 20 70  allowCopy(&c2, p
20949 4d 65 6d 32 2c 20 4d 45 4d 5f 45 70 68 65 6d 29  Mem2, MEM_Ephem)
2094a 3b 0a 20 20 20 20 20 20 20 20 76 31 20 3d 20 73  ;.        v1 = s
2094b 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
2094c 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
2094d 26 63 31 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29  &c1, pColl->enc)
2094e 3b 0a 20 20 20 20 20 20 20 20 6e 31 20 3d 20 76  ;.        n1 = v
2094f 31 3d 3d 30 20 3f 20 30 20 3a 20 63 31 2e 6e 3b  1==0 ? 0 : c1.n;
20950 0a 20 20 20 20 20 20 20 20 76 32 20 3d 20 73 71  .        v2 = sq
20951 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 28  lite3ValueText((
20952 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26  sqlite3_value*)&
20953 63 32 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b  c2, pColl->enc);
20954 0a 20 20 20 20 20 20 20 20 6e 32 20 3d 20 76 32  .        n2 = v2
20955 3d 3d 30 20 3f 20 30 20 3a 20 63 32 2e 6e 3b 0a  ==0 ? 0 : c2.n;.
20956 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 43 6f          rc = pCo
20957 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e  ll->xCmp(pColl->
20958 70 55 73 65 72 2c 20 6e 31 2c 20 76 31 2c 20 6e  pUser, n1, v1, n
20959 32 2c 20 76 32 29 3b 0a 20 20 20 20 20 20 20 20  2, v2);.        
2095a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
2095b 6c 65 61 73 65 28 26 63 31 29 3b 0a 20 20 20 20  lease(&c1);.    
2095c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
2095d 65 6d 52 65 6c 65 61 73 65 28 26 63 32 29 3b 0a  emRelease(&c2);.
2095e 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
2095f 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  c;.      }.    }
20960 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 4e 55 4c  .    /* If a NUL
20961 4c 20 70 6f 69 6e 74 65 72 20 77 61 73 20 70 61  L pointer was pa
20962 73 73 65 64 20 61 73 20 74 68 65 20 63 6f 6c 6c  ssed as the coll
20963 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 61  ate function, fa
20964 6c 6c 20 74 68 72 6f 75 67 68 0a 20 20 20 20 2a  ll through.    *
20965 2a 20 74 6f 20 74 68 65 20 62 6c 6f 62 20 63 61  * to the blob ca
20966 73 65 20 61 6e 64 20 75 73 65 20 6d 65 6d 63 6d  se and use memcm
20967 70 28 29 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a 20  p().  */.  }. . 
20968 20 2f 2a 20 42 6f 74 68 20 76 61 6c 75 65 73 20   /* Both values 
20969 6d 75 73 74 20 62 65 20 62 6c 6f 62 73 2e 20 20  must be blobs.  
2096a 43 6f 6d 70 61 72 65 20 75 73 69 6e 67 20 6d 65  Compare using me
2096b 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 72 63  mcmp().  */.  rc
2096c 20 3d 20 6d 65 6d 63 6d 70 28 70 4d 65 6d 31 2d   = memcmp(pMem1-
2096d 3e 7a 2c 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28 70  >z, pMem2->z, (p
2096e 4d 65 6d 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e 6e  Mem1->n>pMem2->n
2096f 29 3f 70 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d 31  )?pMem2->n:pMem1
20970 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ->n);.  if( rc==
20971 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 4d  0 ){.    rc = pM
20972 65 6d 31 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d 3e  em1->n - pMem2->
20973 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  n;.  }.  return 
20974 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76  rc;.}../*.** Mov
20975 65 20 64 61 74 61 20 6f 75 74 20 6f 66 20 61 20  e data out of a 
20976 62 74 72 65 65 20 6b 65 79 20 6f 72 20 64 61 74  btree key or dat
20977 61 20 66 69 65 6c 64 20 61 6e 64 20 69 6e 74 6f  a field and into
20978 20 61 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65   a Mem structure
20979 2e 0a 2a 2a 20 54 68 65 20 64 61 74 61 20 6f 72  ..** The data or
2097a 20 6b 65 79 20 69 73 20 74 61 6b 65 6e 20 66 72   key is taken fr
2097b 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61  om the entry tha
2097c 74 20 70 43 75 72 20 69 73 20 63 75 72 72 65 6e  t pCur is curren
2097d 74 6c 79 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20  tly pointing.** 
2097e 74 6f 2e 20 20 6f 66 66 73 65 74 20 61 6e 64 20  to.  offset and 
2097f 61 6d 74 20 64 65 74 65 72 6d 69 6e 65 20 77 68  amt determine wh
20980 61 74 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68  at portion of th
20981 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 74 6f  e data or key to
20982 20 72 65 74 72 69 65 76 65 2e 0a 2a 2a 20 6b 65   retrieve..** ke
20983 79 20 69 73 20 74 72 75 65 20 74 6f 20 67 65 74  y is true to get
20984 20 74 68 65 20 6b 65 79 20 6f 72 20 66 61 6c 73   the key or fals
20985 65 20 74 6f 20 67 65 74 20 64 61 74 61 2e 20 20  e to get data.  
20986 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 77 72  The result is wr
20987 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68  itten.** into th
20988 65 20 70 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e 0a  e pMem element..
20989 2a 2a 0a 2a 2a 20 54 68 65 20 70 4d 65 6d 20 73  **.** The pMem s
2098a 74 72 75 63 74 75 72 65 20 69 73 20 61 73 73 75  tructure is assu
2098b 6d 65 64 20 74 6f 20 62 65 20 75 6e 69 6e 69 74  med to be uninit
2098c 69 61 6c 69 7a 65 64 2e 20 20 41 6e 79 20 70 72  ialized.  Any pr
2098d 69 6f 72 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69  ior content.** i
2098e 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69  s overwritten wi
2098f 74 68 6f 75 74 20 62 65 69 6e 67 20 66 72 65 65  thout being free
20990 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73  d..**.** If this
20991 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 66   routine fails f
20992 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 20 28 6d  or any reason (m
20993 61 6c 6c 6f 63 20 72 65 74 75 72 6e 73 20 4e 55  alloc returns NU
20994 4c 4c 20 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a 20  LL or unable.** 
20995 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65  to read from the
20996 20 64 69 73 6b 29 20 74 68 65 6e 20 74 68 65 20   disk) then the 
20997 70 4d 65 6d 20 69 73 20 6c 65 66 74 20 69 6e 20  pMem is left in 
20998 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20  an inconsistent 
20999 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  state..*/.SQLITE
2099a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2099b 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42  ite3VdbeMemFromB
2099c 74 72 65 65 28 0a 20 20 42 74 43 75 72 73 6f 72  tree(.  BtCursor
2099d 20 2a 70 43 75 72 2c 20 20 20 2f 2a 20 43 75 72   *pCur,   /* Cur
2099e 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  sor pointing at 
2099f 72 65 63 6f 72 64 20 74 6f 20 72 65 74 72 69 65  record to retrie
209a0 76 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66  ve. */.  int off
209a1 73 65 74 2c 20 20 20 20 20 20 20 2f 2a 20 4f 66  set,       /* Of
209a2 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 73 74  fset from the st
209a3 61 72 74 20 6f 66 20 64 61 74 61 20 74 6f 20 72  art of data to r
209a4 65 74 75 72 6e 20 62 79 74 65 73 20 66 72 6f 6d  eturn bytes from
209a5 2e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20  . */.  int amt, 
209a6 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
209a7 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72  er of bytes to r
209a8 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20  eturn. */.  int 
209a9 6b 65 79 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  key,          /*
209aa 20 49 66 20 74 72 75 65 2c 20 72 65 74 72 69 65   If true, retrie
209ab 76 65 20 66 72 6f 6d 20 74 68 65 20 62 74 72 65  ve from the btre
209ac 65 20 6b 65 79 2c 20 6e 6f 74 20 64 61 74 61 2e  e key, not data.
209ad 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20   */.  Mem *pMem 
209ae 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
209af 52 65 74 75 72 6e 20 64 61 74 61 20 69 6e 20 74  Return data in t
209b0 68 69 73 20 4d 65 6d 20 73 74 72 75 63 74 75 72  his Mem structur
209b1 65 2e 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20  e. */.){.  char 
209b2 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20 20 2f  *zData;        /
209b3 2a 20 44 61 74 61 20 66 72 6f 6d 20 74 68 65 20  * Data from the 
209b4 62 74 72 65 65 20 6c 61 79 65 72 20 2a 2f 0a 20  btree layer */. 
209b5 20 69 6e 74 20 61 76 61 69 6c 61 62 6c 65 20 3d   int available =
209b6 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f   0;  /* Number o
209b7 66 20 62 79 74 65 73 20 61 76 61 69 6c 61 62 6c  f bytes availabl
209b8 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 62  e on the local b
209b9 74 72 65 65 20 70 61 67 65 20 2a 2f 0a 20 20 69  tree page */.  i
209ba 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
209bb 4b 3b 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64  K; /* Return cod
209bc 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
209bd 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
209be 6f 72 49 73 56 61 6c 69 64 28 70 43 75 72 29 20  orIsValid(pCur) 
209bf 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20 74  );..  /* Note: t
209c0 68 65 20 63 61 6c 6c 73 20 74 6f 20 42 74 72 65  he calls to Btre
209c1 65 4b 65 79 46 65 74 63 68 28 29 20 61 6e 64 20  eKeyFetch() and 
209c2 44 61 74 61 46 65 74 63 68 28 29 20 62 65 6c 6f  DataFetch() belo
209c3 77 20 61 73 73 65 72 74 28 29 20 0a 20 20 2a 2a  w assert() .  **
209c4 20 74 68 61 74 20 62 6f 74 68 20 74 68 65 20 42   that both the B
209c5 74 53 68 61 72 65 64 20 61 6e 64 20 64 61 74 61  tShared and data
209c6 62 61 73 65 20 68 61 6e 64 6c 65 20 6d 75 74 65  base handle mute
209c7 78 65 73 20 61 72 65 20 68 65 6c 64 2e 20 2a 2f  xes are held. */
209c8 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
209c9 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
209ca 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 69 66  wSet)==0 );.  if
209cb 28 20 6b 65 79 20 29 7b 0a 20 20 20 20 7a 44 61  ( key ){.    zDa
209cc 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  ta = (char *)sql
209cd 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 63  ite3BtreeKeyFetc
209ce 68 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62  h(pCur, &availab
209cf 6c 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  le);.  }else{.  
209d0 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72 20    zData = (char 
209d1 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  *)sqlite3BtreeDa
209d2 74 61 46 65 74 63 68 28 70 43 75 72 2c 20 26 61  taFetch(pCur, &a
209d3 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a 20  vailable);.  }. 
209d4 20 61 73 73 65 72 74 28 20 7a 44 61 74 61 21 3d   assert( zData!=
209d5 30 20 29 3b 0a 0a 20 20 69 66 28 20 6f 66 66 73  0 );..  if( offs
209d6 65 74 2b 61 6d 74 3c 3d 61 76 61 69 6c 61 62 6c  et+amt<=availabl
209d7 65 20 26 26 20 28 70 4d 65 6d 2d 3e 66 6c 61 67  e && (pMem->flag
209d8 73 26 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 7b  s&MEM_Dyn)==0 ){
209d9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
209da 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29  MemRelease(pMem)
209db 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20  ;.    pMem->z = 
209dc 26 7a 44 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a  &zData[offset];.
209dd 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
209de 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 45  = MEM_Blob|MEM_E
209df 70 68 65 6d 3b 0a 20 20 7d 65 6c 73 65 20 69 66  phem;.  }else if
209e0 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  ( SQLITE_OK==(rc
209e1 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
209e2 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 61 6d 74 2b  mGrow(pMem, amt+
209e3 32 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 70 4d  2, 0)) ){.    pM
209e4 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
209e5 42 6c 6f 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d  Blob|MEM_Dyn|MEM
209e6 5f 54 65 72 6d 3b 0a 20 20 20 20 70 4d 65 6d 2d  _Term;.    pMem-
209e7 3e 65 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 4d  >enc = 0;.    pM
209e8 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
209e9 45 5f 42 4c 4f 42 3b 0a 20 20 20 20 69 66 28 20  E_BLOB;.    if( 
209ea 6b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20  key ){.      rc 
209eb 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  = sqlite3BtreeKe
209ec 79 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20  y(pCur, offset, 
209ed 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20  amt, pMem->z);. 
209ee 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
209ef 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
209f0 65 44 61 74 61 28 70 43 75 72 2c 20 6f 66 66 73  eData(pCur, offs
209f1 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a  et, amt, pMem->z
209f2 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65  );.    }.    pMe
209f3 6d 2d 3e 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a 20  m->z[amt] = 0;. 
209f4 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b 31     pMem->z[amt+1
209f5 5d 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72  ] = 0;.    if( r
209f6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
209f7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
209f8 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d  eMemRelease(pMem
209f9 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  );.    }.  }.  p
209fa 4d 65 6d 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a 20  Mem->n = amt;.. 
209fb 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
209fc 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
209fd 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  is only availabl
209fe 65 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74  e internally, it
209ff 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20   is not part of 
20a00 74 68 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20  the.** external 
20a01 41 50 49 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e  API. It works in
20a02 20 61 20 73 69 6d 69 6c 61 72 20 77 61 79 20 74   a similar way t
20a03 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  o sqlite3_value_
20a04 74 65 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70  text(),.** excep
20a05 74 20 74 68 65 20 64 61 74 61 20 72 65 74 75 72  t the data retur
20a06 6e 65 64 20 69 73 20 69 6e 20 74 68 65 20 65 6e  ned is in the en
20a07 63 6f 64 69 6e 67 20 73 70 65 63 69 66 69 65 64  coding specified
20a08 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a   by the second.*
20a09 2a 20 70 61 72 61 6d 65 74 65 72 2c 20 77 68 69  * parameter, whi
20a0a 63 68 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f  ch must be one o
20a0b 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  f SQLITE_UTF16BE
20a0c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  , SQLITE_UTF16LE
20a0d 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54   or.** SQLITE_UT
20a0e 46 38 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d  F8..**.** (2006-
20a0f 30 32 2d 31 36 3a 29 20 20 54 68 65 20 65 6e 63  02-16:)  The enc
20a10 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 6f 72   value can be or
20a11 2d 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f  -ed with SQLITE_
20a12 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a  UTF16_ALIGNED..*
20a13 2a 20 49 66 20 74 68 61 74 20 69 73 20 74 68 65  * If that is the
20a14 20 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20   case, then the 
20a15 72 65 73 75 6c 74 20 6d 75 73 74 20 62 65 20 61  result must be a
20a16 6c 69 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65  ligned on an eve
20a17 6e 20 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61  n byte.** bounda
20a18 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ry..*/.SQLITE_PR
20a19 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64  IVATE const void
20a1a 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65   *sqlite3ValueTe
20a1b 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  xt(sqlite3_value
20a1c 2a 20 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b  * pVal, u8 enc){
20a1d 0a 20 20 69 66 28 20 21 70 56 61 6c 20 29 20 72  .  if( !pVal ) r
20a1e 65 74 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65  eturn 0;..  asse
20a1f 72 74 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20  rt( pVal->db==0 
20a20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
20a21 5f 68 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e  _held(pVal->db->
20a22 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
20a23 72 74 28 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e  rt( (enc&3)==(en
20a24 63 26 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f  c&~SQLITE_UTF16_
20a25 41 4c 49 47 4e 45 44 29 20 29 3b 0a 20 20 61 73  ALIGNED) );.  as
20a26 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61  sert( (pVal->fla
20a27 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29  gs & MEM_RowSet)
20a28 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 56  ==0 );..  if( pV
20a29 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75  al->flags&MEM_Nu
20a2a 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ll ){.    return
20a2b 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
20a2c 28 20 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20  ( (MEM_Blob>>3) 
20a2d 3d 3d 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20  == MEM_Str );.  
20a2e 70 56 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28  pVal->flags |= (
20a2f 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pVal->flags & ME
20a30 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 45 78  M_Blob)>>3;.  Ex
20a31 70 61 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0a  pandBlob(pVal);.
20a32 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67    if( pVal->flag
20a33 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20  s&MEM_Str ){.   
20a34 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
20a35 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c  geEncoding(pVal,
20a36 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55   enc & ~SQLITE_U
20a37 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a 20  TF16_ALIGNED);. 
20a38 20 20 20 69 66 28 20 28 65 6e 63 20 26 20 53 51     if( (enc & SQ
20a39 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
20a3a 45 44 29 21 3d 30 20 26 26 20 31 3d 3d 28 31 26  ED)!=0 && 1==(1&
20a3b 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e  SQLITE_PTR_TO_IN
20a3c 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a 20  T(pVal->z)) ){. 
20a3d 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 56       assert( (pV
20a3e 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  al->flags & (MEM
20a3f 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69  _Ephem|MEM_Stati
20a40 63 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  c))!=0 );.      
20a41 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
20a42 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28  emMakeWriteable(
20a43 70 56 61 6c 29 21 3d 53 51 4c 49 54 45 5f 4f 4b  pVal)!=SQLITE_OK
20a44 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
20a45 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
20a46 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
20a47 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61  dbeMemNulTermina
20a48 74 65 28 70 56 61 6c 29 3b 20 2f 2a 20 49 4d 50  te(pVal); /* IMP
20a49 3a 20 52 2d 33 31 32 37 35 2d 34 34 30 36 30 20  : R-31275-44060 
20a4a 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  */.  }else{.    
20a4b 61 73 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66  assert( (pVal->f
20a4c 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3d 3d  lags&MEM_Blob)==
20a4d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 );.    sqlite3
20a4e 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79  VdbeMemStringify
20a4f 28 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20  (pVal, enc);.   
20a50 20 61 73 73 65 72 74 28 20 30 3d 3d 28 31 26 53   assert( 0==(1&S
20a51 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54  QLITE_PTR_TO_INT
20a52 28 70 56 61 6c 2d 3e 7a 29 29 20 29 3b 0a 20 20  (pVal->z)) );.  
20a53 7d 0a 20 20 61 73 73 65 72 74 28 70 56 61 6c 2d  }.  assert(pVal-
20a54 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51  >enc==(enc & ~SQ
20a55 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
20a56 45 44 29 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d  ED) || pVal->db=
20a57 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  =0.             
20a58 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61   || pVal->db->ma
20a59 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
20a5a 69 66 28 20 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28  if( pVal->enc==(
20a5b 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54  enc & ~SQLITE_UT
20a5c 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 29 7b 0a  F16_ALIGNED) ){.
20a5d 20 20 20 20 72 65 74 75 72 6e 20 70 56 61 6c 2d      return pVal-
20a5e 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  >z;.  }else{.   
20a5f 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d   return 0;.  }.}
20a60 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
20a61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c   new sqlite3_val
20a62 75 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51  ue object..*/.SQ
20a63 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
20a64 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69  ite3_value *sqli
20a65 74 65 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69  te3ValueNew(sqli
20a66 74 65 33 20 2a 64 62 29 7b 0a 20 20 4d 65 6d 20  te3 *db){.  Mem 
20a67 2a 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  *p = sqlite3DbMa
20a68 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
20a69 65 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20  eof(*p));.  if( 
20a6a 70 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67  p ){.    p->flag
20a6b 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
20a6c 20 20 70 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49    p->type = SQLI
20a6d 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20 70 2d 3e  TE_NULL;.    p->
20a6e 64 62 20 3d 20 64 62 3b 0a 20 20 7d 0a 20 20 72  db = db;.  }.  r
20a6f 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
20a70 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73  * Create a new s
20a71 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
20a72 65 63 74 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ect, containing 
20a73 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45 78  the value of pEx
20a74 70 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  pr..**.** This o
20a75 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 76 65  nly works for ve
20a76 72 79 20 73 69 6d 70 6c 65 20 65 78 70 72 65 73  ry simple expres
20a77 73 69 6f 6e 73 20 74 68 61 74 20 63 6f 6e 73 69  sions that consi
20a78 73 74 20 6f 66 20 6f 6e 65 20 63 6f 6e 73 74 61  st of one consta
20a79 6e 74 0a 2a 2a 20 74 6f 6b 65 6e 20 28 69 2e 65  nt.** token (i.e
20a7a 2e 20 22 35 22 2c 20 22 35 2e 31 22 2c 20 22 27  . "5", "5.1", "'
20a7b 61 20 73 74 72 69 6e 67 27 22 29 2e 20 49 66 20  a string'"). If 
20a7c 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63  the expression c
20a7d 61 6e 0a 2a 2a 20 62 65 20 63 6f 6e 76 65 72 74  an.** be convert
20a7e 65 64 20 64 69 72 65 63 74 6c 79 20 69 6e 74 6f  ed directly into
20a7f 20 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74   a value, then t
20a80 68 65 20 76 61 6c 75 65 20 69 73 20 61 6c 6c 6f  he value is allo
20a81 63 61 74 65 64 20 61 6e 64 0a 2a 2a 20 61 20 70  cated and.** a p
20a82 6f 69 6e 74 65 72 20 77 72 69 74 74 65 6e 20 74  ointer written t
20a83 6f 20 2a 70 70 56 61 6c 2e 20 54 68 65 20 63 61  o *ppVal. The ca
20a84 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69  ller is responsi
20a85 62 6c 65 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61  ble for dealloca
20a86 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 61 6c 75  ting.** the valu
20a87 65 20 62 79 20 70 61 73 73 69 6e 67 20 69 74 20  e by passing it 
20a88 74 6f 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46  to sqlite3ValueF
20a89 72 65 65 28 29 20 6c 61 74 65 72 20 6f 6e 2e 20  ree() later on. 
20a8a 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
20a8b 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63  n.** cannot be c
20a8c 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 20 76 61  onverted to a va
20a8d 6c 75 65 2c 20 74 68 65 6e 20 2a 70 70 56 61 6c  lue, then *ppVal
20a8e 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e   is set to NULL.
20a8f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20a90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61  TE int sqlite3Va
20a91 6c 75 65 46 72 6f 6d 45 78 70 72 28 0a 20 20 73  lueFromExpr(.  s
20a92 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
20a93 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
20a94 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
20a95 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ion */.  Expr *p
20a96 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20  Expr,           
20a97 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
20a98 73 69 6f 6e 20 74 6f 20 65 76 61 6c 75 61 74 65  sion to evaluate
20a99 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20   */.  u8 enc,   
20a9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20a9b 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75  /* Encoding to u
20a9c 73 65 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e  se */.  u8 affin
20a9d 69 74 79 2c 20 20 20 20 20 20 20 20 20 20 20 20  ity,            
20a9e 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 74 6f    /* Affinity to
20a9f 20 75 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65   use */.  sqlite
20aa0 33 5f 76 61 6c 75 65 20 2a 2a 70 70 56 61 6c 20  3_value **ppVal 
20aa1 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
20aa2 20 6e 65 77 20 76 61 6c 75 65 20 68 65 72 65 20   new value here 
20aa3 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a  */.){.  int op;.
20aa4 20 20 63 68 61 72 20 2a 7a 56 61 6c 20 3d 20 30    char *zVal = 0
20aa5 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ;.  sqlite3_valu
20aa6 65 20 2a 70 56 61 6c 20 3d 20 30 3b 0a 20 20 69  e *pVal = 0;.  i
20aa7 6e 74 20 6e 65 67 49 6e 74 20 3d 20 31 3b 0a 20  nt negInt = 1;. 
20aa8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65   const char *zNe
20aa9 67 20 3d 20 22 22 3b 0a 0a 20 20 69 66 28 20 21  g = "";..  if( !
20aaa 70 45 78 70 72 20 29 7b 0a 20 20 20 20 2a 70 70  pExpr ){.    *pp
20aab 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  Val = 0;.    ret
20aac 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
20aad 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d   }.  op = pExpr-
20aae 3e 6f 70 3b 0a 0a 20 20 2f 2a 20 6f 70 20 63 61  >op;..  /* op ca
20aaf 6e 20 6f 6e 6c 79 20 62 65 20 54 4b 5f 52 45 47  n only be TK_REG
20ab0 49 53 54 45 52 20 69 66 20 77 65 20 68 61 76 65  ISTER if we have
20ab1 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53   compiled with S
20ab2 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
20ab3 54 33 2e 0a 20 20 2a 2a 20 54 68 65 20 69 66 64  T3..  ** The ifd
20ab4 65 66 20 68 65 72 65 20 69 73 20 74 6f 20 65 6e  ef here is to en
20ab5 61 62 6c 65 20 75 73 20 74 6f 20 61 63 68 69 65  able us to achie
20ab6 76 65 20 31 30 30 25 20 62 72 61 6e 63 68 20 74  ve 100% branch t
20ab7 65 73 74 20 63 6f 76 65 72 61 67 65 20 65 76 65  est coverage eve
20ab8 6e 0a 20 20 2a 2a 20 77 68 65 6e 20 53 51 4c 49  n.  ** when SQLI
20ab9 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 20  TE_ENABLE_STAT3 
20aba 69 73 20 6f 6d 69 74 74 65 64 2e 0a 20 20 2a 2f  is omitted..  */
20abb 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
20abc 4e 41 42 4c 45 5f 53 54 41 54 33 0a 20 20 69 66  NABLE_STAT3.  if
20abd 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45  ( op==TK_REGISTE
20abe 52 20 29 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e  R ) op = pExpr->
20abf 6f 70 32 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28  op2;.#else.  if(
20ac0 20 4e 45 56 45 52 28 6f 70 3d 3d 54 4b 5f 52 45   NEVER(op==TK_RE
20ac1 47 49 53 54 45 52 29 20 29 20 6f 70 20 3d 20 70  GISTER) ) op = p
20ac2 45 78 70 72 2d 3e 6f 70 32 3b 0a 23 65 6e 64 69  Expr->op2;.#endi
20ac3 66 0a 0a 20 20 2f 2a 20 48 61 6e 64 6c 65 20 6e  f..  /* Handle n
20ac4 65 67 61 74 69 76 65 20 69 6e 74 65 67 65 72 73  egative integers
20ac5 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 73 74 65   in a single ste
20ac6 70 2e 20 20 54 68 69 73 20 69 73 20 6e 65 65 64  p.  This is need
20ac7 65 64 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 63  ed in the.  ** c
20ac8 61 73 65 20 77 68 65 6e 20 74 68 65 20 76 61 6c  ase when the val
20ac9 75 65 20 69 73 20 2d 39 32 32 33 33 37 32 30 33  ue is -922337203
20aca 36 38 35 34 37 37 35 38 30 38 2e 0a 20 20 2a 2f  6854775808..  */
20acb 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 55 4d  .  if( op==TK_UM
20acc 49 4e 55 53 0a 20 20 20 26 26 20 28 70 45 78 70  INUS.   && (pExp
20acd 72 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b  r->pLeft->op==TK
20ace 5f 49 4e 54 45 47 45 52 20 7c 7c 20 70 45 78 70  _INTEGER || pExp
20acf 72 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b  r->pLeft->op==TK
20ad0 5f 46 4c 4f 41 54 29 20 29 7b 0a 20 20 20 20 70  _FLOAT) ){.    p
20ad1 45 78 70 72 20 3d 20 70 45 78 70 72 2d 3e 70 4c  Expr = pExpr->pL
20ad2 65 66 74 3b 0a 20 20 20 20 6f 70 20 3d 20 70 45  eft;.    op = pE
20ad3 78 70 72 2d 3e 6f 70 3b 0a 20 20 20 20 6e 65 67  xpr->op;.    neg
20ad4 49 6e 74 20 3d 20 2d 31 3b 0a 20 20 20 20 7a 4e  Int = -1;.    zN
20ad5 65 67 20 3d 20 22 2d 22 3b 0a 20 20 7d 0a 0a 20  eg = "-";.  }.. 
20ad6 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 54 52 49   if( op==TK_STRI
20ad7 4e 47 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c 4f  NG || op==TK_FLO
20ad8 41 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 54  AT || op==TK_INT
20ad9 45 47 45 52 20 29 7b 0a 20 20 20 20 70 56 61 6c  EGER ){.    pVal
20ada 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e   = sqlite3ValueN
20adb 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20  ew(db);.    if( 
20adc 70 56 61 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e  pVal==0 ) goto n
20add 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69 66 28 20 45  o_mem;.    if( E
20ade 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
20adf 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75  Expr, EP_IntValu
20ae0 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  e) ){.      sqli
20ae1 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74  te3VdbeMemSetInt
20ae2 36 34 28 70 56 61 6c 2c 20 28 69 36 34 29 70 45  64(pVal, (i64)pE
20ae3 78 70 72 2d 3e 75 2e 69 56 61 6c 75 65 2a 6e 65  xpr->u.iValue*ne
20ae4 67 49 6e 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  gInt);.    }else
20ae5 7b 0a 20 20 20 20 20 20 7a 56 61 6c 20 3d 20 73  {.      zVal = s
20ae6 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
20ae7 2c 20 22 25 73 25 73 22 2c 20 7a 4e 65 67 2c 20  , "%s%s", zNeg, 
20ae8 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29  pExpr->u.zToken)
20ae9 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 56 61 6c  ;.      if( zVal
20aea 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ==0 ) goto no_me
20aeb 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  m;.      sqlite3
20aec 56 61 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c  ValueSetStr(pVal
20aed 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 4c 49  , -1, zVal, SQLI
20aee 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
20aef 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20  DYNAMIC);.      
20af0 69 66 28 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54  if( op==TK_FLOAT
20af1 20 29 20 70 56 61 6c 2d 3e 74 79 70 65 20 3d 20   ) pVal->type = 
20af2 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20  SQLITE_FLOAT;.  
20af3 20 20 7d 0a 20 20 20 20 69 66 28 20 28 6f 70 3d    }.    if( (op=
20af4 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 6f  =TK_INTEGER || o
20af5 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26 26  p==TK_FLOAT ) &&
20af6 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54   affinity==SQLIT
20af7 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20  E_AFF_NONE ){.  
20af8 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
20af9 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 56  ApplyAffinity(pV
20afa 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  al, SQLITE_AFF_N
20afb 55 4d 45 52 49 43 2c 20 53 51 4c 49 54 45 5f 55  UMERIC, SQLITE_U
20afc 54 46 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  TF8);.    }else{
20afd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61  .      sqlite3Va
20afe 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79  lueApplyAffinity
20aff 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c  (pVal, affinity,
20b00 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20   SQLITE_UTF8);. 
20b01 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 56 61     }.    if( pVa
20b02 6c 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  l->flags & (MEM_
20b03 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 20  Int|MEM_Real) ) 
20b04 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  pVal->flags &= ~
20b05 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28  MEM_Str;.    if(
20b06 20 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46   enc!=SQLITE_UTF
20b07 38 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  8 ){.      sqlit
20b08 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
20b09 64 69 6e 67 28 70 56 61 6c 2c 20 65 6e 63 29 3b  ding(pVal, enc);
20b0a 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
20b0b 66 28 20 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53  f( op==TK_UMINUS
20b0c 20 29 20 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73   ) {.    /* This
20b0d 20 62 72 61 6e 63 68 20 68 61 70 70 65 6e 73 20   branch happens 
20b0e 66 6f 72 20 6d 75 6c 74 69 70 6c 65 20 6e 65 67  for multiple neg
20b0f 61 74 69 76 65 20 73 69 67 6e 73 2e 20 20 45 78  ative signs.  Ex
20b10 3a 20 2d 28 2d 35 29 20 2a 2f 0a 20 20 20 20 69  : -(-5) */.    i
20b11 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71  f( SQLITE_OK==sq
20b12 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78  lite3ValueFromEx
20b13 70 72 28 64 62 2c 70 45 78 70 72 2d 3e 70 4c 65  pr(db,pExpr->pLe
20b14 66 74 2c 65 6e 63 2c 61 66 66 69 6e 69 74 79 2c  ft,enc,affinity,
20b15 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 20  &pVal) ){.      
20b16 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75  sqlite3VdbeMemNu
20b17 6d 65 72 69 66 79 28 70 56 61 6c 29 3b 0a 20 20  merify(pVal);.  
20b18 20 20 20 20 69 66 28 20 70 56 61 6c 2d 3e 75 2e      if( pVal->u.
20b19 69 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36  i==SMALLEST_INT6
20b1a 34 20 29 7b 0a 20 20 20 20 20 20 20 20 70 56 61  4 ){.        pVa
20b1b 6c 2d 3e 66 6c 61 67 73 20 26 3d 20 4d 45 4d 5f  l->flags &= MEM_
20b1c 49 6e 74 3b 0a 20 20 20 20 20 20 20 20 70 56 61  Int;.        pVa
20b1d 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  l->flags |= MEM_
20b1e 52 65 61 6c 3b 0a 20 20 20 20 20 20 20 20 70 56  Real;.        pV
20b1f 61 6c 2d 3e 72 20 3d 20 28 64 6f 75 62 6c 65 29  al->r = (double)
20b20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 3b 0a 20  LARGEST_INT64;. 
20b21 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
20b22 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69 20 3d 20      pVal->u.i = 
20b23 2d 70 56 61 6c 2d 3e 75 2e 69 3b 0a 20 20 20 20  -pVal->u.i;.    
20b24 20 20 7d 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e    }.      pVal->
20b25 72 20 3d 20 2d 70 56 61 6c 2d 3e 72 3b 0a 20 20  r = -pVal->r;.  
20b26 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
20b27 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 56  ApplyAffinity(pV
20b28 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 6e  al, affinity, en
20b29 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  c);.    }.  }els
20b2a 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 4e 55 4c  e if( op==TK_NUL
20b2b 4c 20 29 7b 0a 20 20 20 20 70 56 61 6c 20 3d 20  L ){.    pVal = 
20b2c 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28  sqlite3ValueNew(
20b2d 64 62 29 3b 0a 20 20 20 20 69 66 28 20 70 56 61  db);.    if( pVa
20b2e 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  l==0 ) goto no_m
20b2f 65 6d 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20  em;.  }.#ifndef 
20b30 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42  SQLITE_OMIT_BLOB
20b31 5f 4c 49 54 45 52 41 4c 0a 20 20 65 6c 73 65 20  _LITERAL.  else 
20b32 69 66 28 20 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20  if( op==TK_BLOB 
20b33 29 7b 0a 20 20 20 20 69 6e 74 20 6e 56 61 6c 3b  ){.    int nVal;
20b34 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
20b35 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d  pr->u.zToken[0]=
20b36 3d 27 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 75  ='x' || pExpr->u
20b37 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 58 27 20  .zToken[0]=='X' 
20b38 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
20b39 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31  Expr->u.zToken[1
20b3a 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 70  ]=='\'' );.    p
20b3b 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  Val = sqlite3Val
20b3c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69  ueNew(db);.    i
20b3d 66 28 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20  f( !pVal ) goto 
20b3e 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7a 56 61 6c  no_mem;.    zVal
20b3f 20 3d 20 26 70 45 78 70 72 2d 3e 75 2e 7a 54 6f   = &pExpr->u.zTo
20b40 6b 65 6e 5b 32 5d 3b 0a 20 20 20 20 6e 56 61 6c  ken[2];.    nVal
20b41 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
20b42 33 30 28 7a 56 61 6c 29 2d 31 3b 0a 20 20 20 20  30(zVal)-1;.    
20b43 61 73 73 65 72 74 28 20 7a 56 61 6c 5b 6e 56 61  assert( zVal[nVa
20b44 6c 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20  l]=='\'' );.    
20b45 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
20b46 74 53 74 72 28 70 56 61 6c 2c 20 73 71 6c 69 74  tStr(pVal, sqlit
20b47 65 33 48 65 78 54 6f 42 6c 6f 62 28 64 62 2c 20  e3HexToBlob(db, 
20b48 7a 56 61 6c 2c 20 6e 56 61 6c 29 2c 20 6e 56 61  zVal, nVal), nVa
20b49 6c 2f 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20  l/2,.           
20b4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
20b4b 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29   SQLITE_DYNAMIC)
20b4c 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
20b4d 69 66 28 20 70 56 61 6c 20 29 7b 0a 20 20 20 20  if( pVal ){.    
20b4e 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74  sqlite3VdbeMemSt
20b4f 6f 72 65 54 79 70 65 28 70 56 61 6c 29 3b 0a 20  oreType(pVal);. 
20b50 20 7d 0a 20 20 2a 70 70 56 61 6c 20 3d 20 70 56   }.  *ppVal = pV
20b51 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  al;.  return SQL
20b52 49 54 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a  ITE_OK;..no_mem:
20b53 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  .  db->mallocFai
20b54 6c 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74  led = 1;.  sqlit
20b55 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 56 61  e3DbFree(db, zVa
20b56 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c  l);.  sqlite3Val
20b57 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20  ueFree(pVal);.  
20b58 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20 20 72 65  *ppVal = 0;.  re
20b59 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
20b5a 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  M;.}../*.** Chan
20b5b 67 65 20 74 68 65 20 73 74 72 69 6e 67 20 76 61  ge the string va
20b5c 6c 75 65 20 6f 66 20 61 6e 20 73 71 6c 69 74 65  lue of an sqlite
20b5d 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a  3_value object.*
20b5e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20b5f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c   void sqlite3Val
20b60 75 65 53 65 74 53 74 72 28 0a 20 20 73 71 6c 69  ueSetStr(.  sqli
20b61 74 65 33 5f 76 61 6c 75 65 20 2a 76 2c 20 20 20  te3_value *v,   
20b62 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 62 65    /* Value to be
20b63 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c   set */.  int n,
20b64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20b65 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72  /* Length of str
20b66 69 6e 67 20 7a 20 2a 2f 0a 20 20 63 6f 6e 73 74  ing z */.  const
20b67 20 76 6f 69 64 20 2a 7a 2c 20 20 20 20 20 20 20   void *z,       
20b68 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20   /* Text of the 
20b69 6e 65 77 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  new string */.  
20b6a 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20  u8 enc,         
20b6b 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e        /* Encodin
20b6c 67 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 76 6f  g to use */.  vo
20b6d 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a  id (*xDel)(void*
20b6e 29 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f  )   /* Destructo
20b6f 72 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67  r for the string
20b70 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 76 20 29   */.){.  if( v )
20b71 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
20b72 65 74 53 74 72 28 28 4d 65 6d 20 2a 29 76 2c 20  etStr((Mem *)v, 
20b73 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 65 6c 29  z, n, enc, xDel)
20b74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
20b75 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  an sqlite3_value
20b76 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54   object.*/.SQLIT
20b77 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
20b78 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
20b79 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76  sqlite3_value *v
20b7a 29 7b 0a 20 20 69 66 28 20 21 76 20 29 20 72 65  ){.  if( !v ) re
20b7b 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56  turn;.  sqlite3V
20b7c 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 28 4d  dbeMemRelease((M
20b7d 65 6d 20 2a 29 76 29 3b 0a 20 20 73 71 6c 69 74  em *)v);.  sqlit
20b7e 65 33 44 62 46 72 65 65 28 28 28 4d 65 6d 2a 29  e3DbFree(((Mem*)
20b7f 76 29 2d 3e 64 62 2c 20 76 29 3b 0a 7d 0a 0a 2f  v)->db, v);.}../
20b80 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
20b81 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
20b82 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  in the sqlite3_v
20b83 61 6c 75 65 20 6f 62 6a 65 63 74 20 61 73 73 75  alue object assu
20b84 6d 69 6e 67 0a 2a 2a 20 74 68 61 74 20 69 74 20  ming.** that it 
20b85 75 73 65 73 20 74 68 65 20 65 6e 63 6f 64 69 6e  uses the encodin
20b86 67 20 22 65 6e 63 22 0a 2a 2f 0a 53 51 4c 49 54  g "enc".*/.SQLIT
20b87 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
20b88 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28  lite3ValueBytes(
20b89 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
20b8a 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20  Val, u8 enc){.  
20b8b 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70  Mem *p = (Mem*)p
20b8c 56 61 6c 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66  Val;.  if( (p->f
20b8d 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29  lags & MEM_Blob)
20b8e 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 56 61  !=0 || sqlite3Va
20b8f 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 65 6e  lueText(pVal, en
20b90 63 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  c) ){.    if( p-
20b91 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72  >flags & MEM_Zer
20b92 6f 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  o ){.      retur
20b93 6e 20 70 2d 3e 6e 20 2b 20 70 2d 3e 75 2e 6e 5a  n p->n + p->u.nZ
20b94 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ero;.    }else{.
20b95 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e        return p->
20b96 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  n;.    }.  }.  r
20b97 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a  eturn 0;.}../***
20b98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
20b99 6f 66 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a  of vdbemem.c ***
20b9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
20b9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
20b9e 6e 20 66 69 6c 65 20 76 64 62 65 61 75 78 2e 63  n file vdbeaux.c
20b9f 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
20ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20ba1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
20ba2 2a 20 32 30 30 33 20 53 65 70 74 65 6d 62 65 72  * 2003 September
20ba3 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   6.**.** The aut
20ba4 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
20ba5 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
20ba6 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
20ba7 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
20ba8 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
20ba9 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
20baa 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
20bab 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
20bac 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
20bad 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
20bae 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
20baf 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
20bb0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
20bb1 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
20bb2 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
20bb3 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
20bb4 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
20bb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20bb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20bb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20bb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20bb9 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
20bba 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65  ontains code use
20bbb 64 20 66 6f 72 20 63 72 65 61 74 69 6e 67 2c 20  d for creating, 
20bbc 64 65 73 74 72 6f 79 69 6e 67 2c 20 61 6e 64 20  destroying, and 
20bbd 70 6f 70 75 6c 61 74 69 6e 67 0a 2a 2a 20 61 20  populating.** a 
20bbe 56 44 42 45 20 28 6f 72 20 61 6e 20 22 73 71 6c  VDBE (or an "sql
20bbf 69 74 65 33 5f 73 74 6d 74 22 20 61 73 20 69 74  ite3_stmt" as it
20bc0 20 69 73 20 6b 6e 6f 77 6e 20 74 6f 20 74 68 65   is known to the
20bc1 20 6f 75 74 73 69 64 65 20 77 6f 72 6c 64 2e 29   outside world.)
20bc2 20 20 50 72 69 6f 72 0a 2a 2a 20 74 6f 20 76 65    Prior.** to ve
20bc3 72 73 69 6f 6e 20 32 2e 38 2e 37 2c 20 61 6c 6c  rsion 2.8.7, all
20bc4 20 74 68 69 73 20 63 6f 64 65 20 77 61 73 20 63   this code was c
20bc5 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 65  ombined into the
20bc6 20 76 64 62 65 2e 63 20 73 6f 75 72 63 65 20 66   vdbe.c source f
20bc7 69 6c 65 2e 0a 2a 2a 20 42 75 74 20 74 68 61 74  ile..** But that
20bc8 20 66 69 6c 65 20 77 61 73 20 67 65 74 74 69 6e   file was gettin
20bc9 67 20 74 6f 6f 20 62 69 67 20 73 6f 20 74 68 69  g too big so thi
20bca 73 20 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65  s subroutines we
20bcb 72 65 20 73 70 6c 69 74 20 6f 75 74 2e 0a 2a 2f  re split out..*/
20bcc 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 64  ..../*.** When d
20bcd 65 62 75 67 67 69 6e 67 20 74 68 65 20 63 6f 64  ebugging the cod
20bce 65 20 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 61  e generator in a
20bcf 20 73 79 6d 62 6f 6c 69 63 20 64 65 62 75 67 67   symbolic debugg
20bd0 65 72 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a 20 73  er, one can.** s
20bd1 65 74 20 74 68 65 20 73 71 6c 69 74 65 33 56 64  et the sqlite3Vd
20bd2 62 65 41 64 64 6f 70 54 72 61 63 65 20 74 6f 20  beAddopTrace to 
20bd3 31 20 61 6e 64 20 61 6c 6c 20 6f 70 63 6f 64 65  1 and all opcode
20bd4 73 20 77 69 6c 6c 20 62 65 20 70 72 69 6e 74 65  s will be printe
20bd5 64 0a 2a 2a 20 61 73 20 74 68 65 79 20 61 72 65  d.** as they are
20bd6 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e   added to the in
20bd7 73 74 72 75 63 74 69 6f 6e 20 73 74 72 65 61 6d  struction stream
20bd8 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
20bd9 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
20bda 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
20bdb 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63  te3VdbeAddopTrac
20bdc 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a  e = 0;.#endif...
20bdd 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
20bde 65 77 20 76 69 72 74 75 61 6c 20 64 61 74 61 62  ew virtual datab
20bdf 61 73 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f 0a 53  ase engine..*/.S
20be0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
20be1 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43  be *sqlite3VdbeC
20be2 72 65 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64  reate(sqlite3 *d
20be3 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20  b){.  Vdbe *p;. 
20be4 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61   p = sqlite3DbMa
20be5 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
20be6 65 6f 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69  eof(Vdbe) );.  i
20be7 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
20be8 20 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62   0;.  p->db = db
20be9 3b 0a 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62  ;.  if( db->pVdb
20bea 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64  e ){.    db->pVd
20beb 62 65 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20  be->pPrev = p;. 
20bec 20 7d 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20   }.  p->pNext = 
20bed 64 62 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e  db->pVdbe;.  p->
20bee 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d  pPrev = 0;.  db-
20bef 3e 70 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d  >pVdbe = p;.  p-
20bf0 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41  >magic = VDBE_MA
20bf1 47 49 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75  GIC_INIT;.  retu
20bf2 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn p;.}../*.** R
20bf3 65 6d 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20  emember the SQL 
20bf4 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65  string for a pre
20bf5 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e  pared statement.
20bf6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20bf7 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
20bf8 64 62 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a  dbeSetSql(Vdbe *
20bf9 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  p, const char *z
20bfa 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 73 50  , int n, int isP
20bfb 72 65 70 61 72 65 56 32 29 7b 0a 20 20 61 73 73  repareV2){.  ass
20bfc 65 72 74 28 20 69 73 50 72 65 70 61 72 65 56 32  ert( isPrepareV2
20bfd 3d 3d 31 20 7c 7c 20 69 73 50 72 65 70 61 72 65  ==1 || isPrepare
20bfe 56 32 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  V2==0 );.  if( p
20bff 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69  ==0 ) return;.#i
20c00 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
20c01 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26 20  _OMIT_TRACE) && 
20c02 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
20c03 45 4e 41 42 4c 45 5f 53 51 4c 4c 4f 47 29 0a 20  ENABLE_SQLLOG). 
20c04 20 69 66 28 20 21 69 73 50 72 65 70 61 72 65 56   if( !isPrepareV
20c05 32 20 29 20 72 65 74 75 72 6e 3b 0a 23 65 6e 64  2 ) return;.#end
20c06 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  if.  assert( p->
20c07 7a 53 71 6c 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e  zSql==0 );.  p->
20c08 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 44 62  zSql = sqlite3Db
20c09 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 7a  StrNDup(p->db, z
20c0a 2c 20 6e 29 3b 0a 20 20 70 2d 3e 69 73 50 72 65  , n);.  p->isPre
20c0b 70 61 72 65 56 32 20 3d 20 28 75 38 29 69 73 50  pareV2 = (u8)isP
20c0c 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 2f 2a 0a  repareV2;.}../*.
20c0d 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 53 51  ** Return the SQ
20c0e 4c 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  L associated wit
20c0f 68 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61  h a prepared sta
20c10 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45  tement.*/.SQLITE
20c11 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
20c12 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 73 71 6c  *sqlite3_sql(sql
20c13 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
20c14 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  ){.  Vdbe *p = (
20c15 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20  Vdbe *)pStmt;.  
20c16 72 65 74 75 72 6e 20 28 70 20 26 26 20 70 2d 3e  return (p && p->
20c17 69 73 50 72 65 70 61 72 65 56 32 29 20 3f 20 70  isPrepareV2) ? p
20c18 2d 3e 7a 53 71 6c 20 3a 20 30 3b 0a 7d 0a 0a 2f  ->zSql : 0;.}../
20c19 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63 6f  *.** Swap all co
20c1a 6e 74 65 6e 74 20 62 65 74 77 65 65 6e 20 74 77  ntent between tw
20c1b 6f 20 56 44 42 45 20 73 74 72 75 63 74 75 72 65  o VDBE structure
20c1c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
20c1d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
20c1e 33 56 64 62 65 53 77 61 70 28 56 64 62 65 20 2a  3VdbeSwap(Vdbe *
20c1f 70 41 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a 20  pA, Vdbe *pB){. 
20c20 20 56 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d 70   Vdbe tmp, *pTmp
20c21 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a  ;.  char *zTmp;.
20c22 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 2a    tmp = *pA;.  *
20c23 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 20  pA = *pB;.  *pB 
20c24 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20  = tmp;.  pTmp = 
20c25 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 2d  pA->pNext;.  pA-
20c26 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e 65  >pNext = pB->pNe
20c27 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 20  xt;.  pB->pNext 
20c28 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 3d  = pTmp;.  pTmp =
20c29 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 41   pA->pPrev;.  pA
20c2a 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 50  ->pPrev = pB->pP
20c2b 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 76  rev;.  pB->pPrev
20c2c 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 20   = pTmp;.  zTmp 
20c2d 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 41  = pA->zSql;.  pA
20c2e 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 71  ->zSql = pB->zSq
20c2f 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d 20  l;.  pB->zSql = 
20c30 7a 54 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50 72  zTmp;.  pB->isPr
20c31 65 70 61 72 65 56 32 20 3d 20 70 41 2d 3e 69 73  epareV2 = pA->is
20c32 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23 69  PrepareV2;.}..#i
20c33 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
20c34 47 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 61  G./*.** Turn tra
20c35 63 69 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a 2a  cing on or off.*
20c36 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20c37 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
20c38 65 54 72 61 63 65 28 56 64 62 65 20 2a 70 2c 20  eTrace(Vdbe *p, 
20c39 46 49 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 20  FILE *trace){.  
20c3a 70 2d 3e 74 72 61 63 65 20 3d 20 74 72 61 63 65  p->trace = trace
20c3b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
20c3c 2a 20 52 65 73 69 7a 65 20 74 68 65 20 56 64 62  * Resize the Vdb
20c3d 65 2e 61 4f 70 20 61 72 72 61 79 20 73 6f 20 74  e.aOp array so t
20c3e 68 61 74 20 69 74 20 69 73 20 61 74 20 6c 65 61  hat it is at lea
20c3f 73 74 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 72  st one op larger
20c40 20 74 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 73   than .** it was
20c41 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75  ..**.** If an ou
20c42 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f  t-of-memory erro
20c43 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72  r occurs while r
20c44 65 73 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61  esizing the arra
20c45 79 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  y, return.** SQL
20c46 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68  ITE_NOMEM. In th
20c47 69 73 20 63 61 73 65 20 56 64 62 65 2e 61 4f 70  is case Vdbe.aOp
20c48 20 61 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c   and Vdbe.nOpAll
20c49 6f 63 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 6e  oc remain .** un
20c4a 63 68 61 6e 67 65 64 20 28 74 68 69 73 20 69 73  changed (this is
20c4b 20 73 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 63   so that any opc
20c4c 6f 64 65 73 20 61 6c 72 65 61 64 79 20 61 6c 6c  odes already all
20c4d 6f 63 61 74 65 64 20 63 61 6e 20 62 65 20 0a 2a  ocated can be .*
20c4e 2a 20 63 6f 72 72 65 63 74 6c 79 20 64 65 61 6c  * correctly deal
20c4f 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69  located along wi
20c50 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 74  th the rest of t
20c51 68 65 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 61  he Vdbe)..*/.sta
20c52 74 69 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 72  tic int growOpAr
20c53 72 61 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  ray(Vdbe *p){.  
20c54 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 20  VdbeOp *pNew;.  
20c55 69 6e 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e 6e  int nNew = (p->n
20c56 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f 70  OpAlloc ? p->nOp
20c57 41 6c 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 28  Alloc*2 : (int)(
20c58 31 30 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29  1024/sizeof(Op))
20c59 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69  );.  pNew = sqli
20c5a 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e  te3DbRealloc(p->
20c5b 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 77  db, p->aOp, nNew
20c5c 2a 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20  *sizeof(Op));.  
20c5d 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20  if( pNew ){.    
20c5e 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73 71  p->nOpAlloc = sq
20c5f 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a  lite3DbMallocSiz
20c60 65 28 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f 73  e(p->db, pNew)/s
20c61 69 7a 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20 70  izeof(Op);.    p
20c62 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20 20  ->aOp = pNew;.  
20c63 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 4e 65 77  }.  return (pNew
20c64 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53   ? SQLITE_OK : S
20c65 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a  QLITE_NOMEM);.}.
20c66 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77  ./*.** Add a new
20c67 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20   instruction to 
20c68 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 73 74  the list of inst
20c69 72 75 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74  ructions current
20c6a 20 69 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e   in the.** VDBE.
20c6b 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 64 64    Return the add
20c6c 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65 77 20  ress of the new 
20c6d 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a  instruction..**.
20c6e 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 3a 0a 2a  ** Parameters:.*
20c6f 2a 0a 2a 2a 20 20 20 20 70 20 20 20 20 20 20 20  *.**    p       
20c70 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20          Pointer 
20c71 74 6f 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a 2a  to the VDBE.**.*
20c72 2a 20 20 20 20 6f 70 20 20 20 20 20 20 20 20 20  *    op         
20c73 20 20 20 20 20 54 68 65 20 6f 70 63 6f 64 65 20       The opcode 
20c74 66 6f 72 20 74 68 69 73 20 69 6e 73 74 72 75 63  for this instruc
20c75 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70 31  tion.**.**    p1
20c76 2c 20 70 32 2c 20 70 33 20 20 20 20 20 20 4f 70  , p2, p3      Op
20c77 65 72 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73 65  erands.**.** Use
20c78 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65   the sqlite3Vdbe
20c79 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20 66  ResolveLabel() f
20c7a 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 78 20 61  unction to fix a
20c7b 6e 20 61 64 64 72 65 73 73 20 61 6e 64 0a 2a 2a  n address and.**
20c7c 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65   the sqlite3Vdbe
20c7d 43 68 61 6e 67 65 50 34 28 29 20 66 75 6e 63 74  ChangeP4() funct
20c7e 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65 20 74 68  ion to change th
20c7f 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50  e value of the P
20c80 34 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a 2f  4.** operand..*/
20c81 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20c82 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
20c83 64 64 4f 70 33 28 56 64 62 65 20 2a 70 2c 20 69  ddOp3(Vdbe *p, i
20c84 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69  nt op, int p1, i
20c85 6e 74 20 70 32 2c 20 69 6e 74 20 70 33 29 7b 0a  nt p2, int p3){.
20c86 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 4f    int i;.  VdbeO
20c87 70 20 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20 70  p *pOp;..  i = p
20c88 2d 3e 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74 28  ->nOp;.  assert(
20c89 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
20c8a 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
20c8b 61 73 73 65 72 74 28 20 6f 70 3e 30 20 26 26 20  assert( op>0 && 
20c8c 6f 70 3c 30 78 66 66 20 29 3b 0a 20 20 69 66 28  op<0xff );.  if(
20c8d 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69 20   p->nOpAlloc<=i 
20c8e 29 7b 0a 20 20 20 20 69 66 28 20 67 72 6f 77 4f  ){.    if( growO
20c8f 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20  pArray(p) ){.   
20c90 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
20c91 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70 2b   }.  }.  p->nOp+
20c92 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61  +;.  pOp = &p->a
20c93 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f 70  Op[i];.  pOp->op
20c94 63 6f 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a 20  code = (u8)op;. 
20c95 20 70 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20 20   pOp->p5 = 0;.  
20c96 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 20  pOp->p1 = p1;.  
20c97 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 20  pOp->p2 = p2;.  
20c98 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20 20  pOp->p3 = p3;.  
20c99 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20  pOp->p4.p = 0;. 
20c9a 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
20c9b 34 5f 4e 4f 54 55 53 45 44 3b 0a 23 69 66 64 65  4_NOTUSED;.#ifde
20c9c 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
20c9d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d   pOp->zComment =
20c9e 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   0;.  if( sqlite
20c9f 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20  3VdbeAddopTrace 
20ca0 29 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69  ) sqlite3VdbePri
20ca1 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70 2d 3e 61  ntOp(0, i, &p->a
20ca2 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69 66 0a 23  Op[i]);.#endif.#
20ca3 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49  ifdef VDBE_PROFI
20ca4 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73  LE.  pOp->cycles
20ca5 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 63 6e 74   = 0;.  pOp->cnt
20ca6 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72   = 0;.#endif.  r
20ca7 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54  eturn i;.}.SQLIT
20ca8 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
20ca9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
20caa 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 29  Vdbe *p, int op)
20cab 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
20cac 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20  e3VdbeAddOp3(p, 
20cad 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 7d 0a  op, 0, 0, 0);.}.
20cae 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
20caf 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
20cb0 64 4f 70 31 28 56 64 62 65 20 2a 70 2c 20 69 6e  dOp1(Vdbe *p, in
20cb1 74 20 6f 70 2c 20 69 6e 74 20 70 31 29 7b 0a 20  t op, int p1){. 
20cb2 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
20cb3 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c  dbeAddOp3(p, op,
20cb4 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d 0a 53 51   p1, 0, 0);.}.SQ
20cb5 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
20cb6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20cb7 70 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  p2(Vdbe *p, int 
20cb8 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74 20  op, int p1, int 
20cb9 70 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  p2){.  return sq
20cba 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
20cbb 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 30  p, op, p1, p2, 0
20cbc 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  );.}.../*.** Add
20cbd 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 61 74 20   an opcode that 
20cbe 69 6e 63 6c 75 64 65 73 20 74 68 65 20 70 34 20  includes the p4 
20cbf 76 61 6c 75 65 20 61 73 20 61 20 70 6f 69 6e 74  value as a point
20cc0 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
20cc1 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
20cc2 33 56 64 62 65 41 64 64 4f 70 34 28 0a 20 20 56  3VdbeAddOp4(.  V
20cc3 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  dbe *p,         
20cc4 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6f 70     /* Add the op
20cc5 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 4d 20  code to this VM 
20cc6 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  */.  int op,    
20cc7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
20cc8 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20  new opcode */.  
20cc9 69 6e 74 20 70 31 2c 20 20 20 20 20 20 20 20 20  int p1,         
20cca 20 20 20 20 2f 2a 20 54 68 65 20 50 31 20 6f 70      /* The P1 op
20ccb 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70  erand */.  int p
20ccc 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  2,             /
20ccd 2a 20 54 68 65 20 50 32 20 6f 70 65 72 61 6e 64  * The P2 operand
20cce 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c 20 20 20   */.  int p3,   
20ccf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
20cd0 20 50 33 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20   P3 operand */. 
20cd1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34   const char *zP4
20cd2 2c 20 20 20 20 2f 2a 20 54 68 65 20 50 34 20 6f  ,    /* The P4 o
20cd3 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  perand */.  int 
20cd4 70 34 74 79 70 65 20 20 20 20 20 20 20 20 20 20  p4type          
20cd5 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64 20 74 79  /* P4 operand ty
20cd6 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61  pe */.){.  int a
20cd7 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
20cd8 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70  eAddOp3(p, op, p
20cd9 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20 20 73 71  1, p2, p3);.  sq
20cda 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
20cdb 34 28 70 2c 20 61 64 64 72 2c 20 7a 50 34 2c 20  4(p, addr, zP4, 
20cdc 70 34 74 79 70 65 29 3b 0a 20 20 72 65 74 75 72  p4type);.  retur
20cdd 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n addr;.}../*.**
20cde 20 41 64 64 20 61 6e 20 4f 50 5f 50 61 72 73 65   Add an OP_Parse
20cdf 53 63 68 65 6d 61 20 6f 70 63 6f 64 65 2e 20 20  Schema opcode.  
20ce0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
20ce1 62 72 6f 6b 65 6e 20 6f 75 74 20 66 72 6f 6d 0a  broken out from.
20ce2 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  ** sqlite3VdbeAd
20ce3 64 4f 70 34 28 29 20 73 69 6e 63 65 20 69 74 20  dOp4() since it 
20ce4 6e 65 65 64 73 20 74 6f 20 61 6c 73 6f 20 6e 65  needs to also ne
20ce5 65 64 73 20 74 6f 20 6d 61 72 6b 20 61 6c 6c 20  eds to mark all 
20ce6 62 74 72 65 65 73 0a 2a 2a 20 61 73 20 68 61 76  btrees.** as hav
20ce7 69 6e 67 20 62 65 65 6e 20 75 73 65 64 2e 0a 2a  ing been used..*
20ce8 2a 0a 2a 2a 20 54 68 65 20 7a 57 68 65 72 65 20  *.** The zWhere 
20ce9 73 74 72 69 6e 67 20 6d 75 73 74 20 68 61 76 65  string must have
20cea 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66   been obtained f
20ceb 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
20cec 6f 63 28 29 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  oc()..** This ro
20ced 75 74 69 6e 65 20 77 69 6c 6c 20 74 61 6b 65 20  utine will take 
20cee 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 74 68 65  ownership of the
20cef 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72   allocated memor
20cf0 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
20cf1 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
20cf2 33 56 64 62 65 41 64 64 50 61 72 73 65 53 63 68  3VdbeAddParseSch
20cf3 65 6d 61 4f 70 28 56 64 62 65 20 2a 70 2c 20 69  emaOp(Vdbe *p, i
20cf4 6e 74 20 69 44 62 2c 20 63 68 61 72 20 2a 7a 57  nt iDb, char *zW
20cf5 68 65 72 65 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a  here){.  int j;.
20cf6 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73 71 6c    int addr = sql
20cf7 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 70  ite3VdbeAddOp3(p
20cf8 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61  , OP_ParseSchema
20cf9 2c 20 69 44 62 2c 20 30 2c 20 30 29 3b 0a 20 20  , iDb, 0, 0);.  
20cfa 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
20cfb 65 50 34 28 70 2c 20 61 64 64 72 2c 20 7a 57 68  eP4(p, addr, zWh
20cfc 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29  ere, P4_DYNAMIC)
20cfd 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70  ;.  for(j=0; j<p
20cfe 2d 3e 64 62 2d 3e 6e 44 62 3b 20 6a 2b 2b 29 20  ->db->nDb; j++) 
20cff 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
20d00 74 72 65 65 28 70 2c 20 6a 29 3b 0a 7d 0a 0a 2f  tree(p, j);.}../
20d01 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63 6f  *.** Add an opco
20d02 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65 73  de that includes
20d03 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61 73   the p4 value as
20d04 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a   an integer..*/.
20d05 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
20d06 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
20d07 64 4f 70 34 49 6e 74 28 0a 20 20 56 64 62 65 20  dOp4Int(.  Vdbe 
20d08 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  *p,            /
20d09 2a 20 41 64 64 20 74 68 65 20 6f 70 63 6f 64 65  * Add the opcode
20d0a 20 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20   to this VM */. 
20d0b 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20   int op,        
20d0c 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
20d0d 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  opcode */.  int 
20d0e 70 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  p1,             
20d0f 2f 2a 20 54 68 65 20 50 31 20 6f 70 65 72 61 6e  /* The P1 operan
20d10 64 20 2a 2f 0a 20 20 69 6e 74 20 70 32 2c 20 20  d */.  int p2,  
20d11 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
20d12 65 20 50 32 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  e P2 operand */.
20d13 20 20 69 6e 74 20 70 33 2c 20 20 20 20 20 20 20    int p3,       
20d14 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 33 20        /* The P3 
20d15 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74  operand */.  int
20d16 20 70 34 20 20 20 20 20 20 20 20 20 20 20 20 20   p4             
20d17 20 2f 2a 20 54 68 65 20 50 34 20 6f 70 65 72 61   /* The P4 opera
20d18 6e 64 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72  nd as an integer
20d19 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 64 64   */.){.  int add
20d1a 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
20d1b 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c  ddOp3(p, op, p1,
20d1c 20 70 32 2c 20 70 33 29 3b 0a 20 20 73 71 6c 69   p2, p3);.  sqli
20d1d 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
20d1e 70 2c 20 61 64 64 72 2c 20 53 51 4c 49 54 45 5f  p, addr, SQLITE_
20d1f 49 4e 54 5f 54 4f 5f 50 54 52 28 70 34 29 2c 20  INT_TO_PTR(p4), 
20d20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 72 65 74  P4_INT32);.  ret
20d21 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a  urn addr;.}../*.
20d22 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
20d23 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 66  symbolic label f
20d24 6f 72 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f  or an instructio
20d25 6e 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74  n that has yet t
20d26 6f 20 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 20  o be.** coded.  
20d27 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62  The symbolic lab
20d28 65 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73  el is really jus
20d29 74 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d  t a negative num
20d2a 62 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 62  ber.  The.** lab
20d2b 65 6c 20 63 61 6e 20 62 65 20 75 73 65 64 20 61  el can be used a
20d2c 73 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 6f  s the P2 value o
20d2d 66 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20  f an operation. 
20d2e 20 4c 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a 20   Later, when.** 
20d2f 74 68 65 20 6c 61 62 65 6c 20 69 73 20 72 65 73  the label is res
20d30 6f 6c 76 65 64 20 74 6f 20 61 20 73 70 65 63 69  olved to a speci
20d31 66 69 63 20 61 64 64 72 65 73 73 2c 20 74 68 65  fic address, the
20d32 20 56 44 42 45 20 77 69 6c 6c 20 73 63 61 6e 0a   VDBE will scan.
20d33 2a 2a 20 74 68 72 6f 75 67 68 20 69 74 73 20 6f  ** through its o
20d34 70 65 72 61 74 69 6f 6e 20 6c 69 73 74 20 61 6e  peration list an
20d35 64 20 63 68 61 6e 67 65 20 61 6c 6c 20 76 61 6c  d change all val
20d36 75 65 73 20 6f 66 20 50 32 20 77 68 69 63 68 20  ues of P2 which 
20d37 6d 61 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61 62  match.** the lab
20d38 65 6c 20 69 6e 74 6f 20 74 68 65 20 72 65 73 6f  el into the reso
20d39 6c 76 65 64 20 61 64 64 72 65 73 73 2e 0a 2a 2a  lved address..**
20d3a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6b 6e 6f  .** The VDBE kno
20d3b 77 73 20 74 68 61 74 20 61 20 50 32 20 76 61 6c  ws that a P2 val
20d3c 75 65 20 69 73 20 61 20 6c 61 62 65 6c 20 62 65  ue is a label be
20d3d 63 61 75 73 65 20 6c 61 62 65 6c 73 20 61 72 65  cause labels are
20d3e 0a 2a 2a 20 61 6c 77 61 79 73 20 6e 65 67 61 74  .** always negat
20d3f 69 76 65 20 61 6e 64 20 50 32 20 76 61 6c 75 65  ive and P2 value
20d40 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f  s are suppose to
20d41 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65   be non-negative
20d42 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e 65  ..** Hence, a ne
20d43 67 61 74 69 76 65 20 50 32 20 76 61 6c 75 65 20  gative P2 value 
20d44 69 73 20 61 20 6c 61 62 65 6c 20 74 68 61 74 20  is a label that 
20d45 68 61 73 20 79 65 74 20 74 6f 20 62 65 20 72 65  has yet to be re
20d46 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65  solved..**.** Ze
20d47 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ro is returned i
20d48 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  f a malloc() fai
20d49 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ls..*/.SQLITE_PR
20d4a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
20d4b 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56  3VdbeMakeLabel(V
20d4c 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  dbe *p){.  int i
20d4d 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a   = p->nLabel++;.
20d4e 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67    assert( p->mag
20d4f 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
20d50 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 28 69 20  NIT );.  if( (i 
20d51 26 20 28 69 2d 31 29 29 3d 3d 30 20 29 7b 0a 20  & (i-1))==0 ){. 
20d52 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 73     p->aLabel = s
20d53 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f  qlite3DbReallocO
20d54 72 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e  rFree(p->db, p->
20d55 61 4c 61 62 65 6c 2c 20 0a 20 20 20 20 20 20 20  aLabel, .       
20d56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d58 28 69 2a 32 2b 31 29 2a 73 69 7a 65 6f 66 28 70  (i*2+1)*sizeof(p
20d59 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 29 3b 0a 20  ->aLabel[0]));. 
20d5a 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4c 61 62   }.  if( p->aLab
20d5b 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61  el ){.    p->aLa
20d5c 62 65 6c 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d  bel[i] = -1;.  }
20d5d 0a 20 20 72 65 74 75 72 6e 20 2d 31 2d 69 3b 0a  .  return -1-i;.
20d5e 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65  }../*.** Resolve
20d5f 20 6c 61 62 65 6c 20 22 78 22 20 74 6f 20 62 65   label "x" to be
20d60 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   the address of 
20d61 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
20d62 74 69 6f 6e 20 74 6f 0a 2a 2a 20 62 65 20 69 6e  tion to.** be in
20d63 73 65 72 74 65 64 2e 20 20 54 68 65 20 70 61 72  serted.  The par
20d64 61 6d 65 74 65 72 20 22 78 22 20 6d 75 73 74 20  ameter "x" must 
20d65 68 61 76 65 20 62 65 65 6e 20 6f 62 74 61 69 6e  have been obtain
20d66 65 64 20 66 72 6f 6d 0a 2a 2a 20 61 20 70 72 69  ed from.** a pri
20d67 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  or call to sqlit
20d68 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
20d69 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
20d6a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
20d6b 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
20d6c 6c 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 78  l(Vdbe *p, int x
20d6d 29 7b 0a 20 20 69 6e 74 20 6a 20 3d 20 2d 31 2d  ){.  int j = -1-
20d6e 78 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  x;.  assert( p->
20d6f 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
20d70 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73 73 65  C_INIT );.  asse
20d71 72 74 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 70 2d  rt( j>=0 && j<p-
20d72 3e 6e 4c 61 62 65 6c 20 29 3b 0a 20 20 69 66 28  >nLabel );.  if(
20d73 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20   p->aLabel ){.  
20d74 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 6a 5d 20 3d    p->aLabel[j] =
20d75 20 70 2d 3e 6e 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a   p->nOp;.  }.}..
20d76 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 74 68 65 20 56  /*.** Mark the V
20d77 44 42 45 20 61 73 20 6f 6e 65 20 74 68 61 74 20  DBE as one that 
20d78 63 61 6e 20 6f 6e 6c 79 20 62 65 20 72 75 6e 20  can only be run 
20d79 6f 6e 65 20 74 69 6d 65 2e 0a 2a 2f 0a 53 51 4c  one time..*/.SQL
20d7a 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
20d7b 20 73 71 6c 69 74 65 33 56 64 62 65 52 75 6e 4f   sqlite3VdbeRunO
20d7c 6e 6c 79 4f 6e 63 65 28 56 64 62 65 20 2a 70 29  nlyOnce(Vdbe *p)
20d7d 7b 0a 20 20 70 2d 3e 72 75 6e 4f 6e 6c 79 4f 6e  {.  p->runOnlyOn
20d7e 63 65 20 3d 20 31 3b 0a 7d 0a 0a 23 69 66 64 65  ce = 1;.}..#ifde
20d7f 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2f  f SQLITE_DEBUG /
20d80 2a 20 73 71 6c 69 74 65 33 41 73 73 65 72 74 4d  * sqlite3AssertM
20d81 61 79 41 62 6f 72 74 28 29 20 6c 6f 67 69 63 20  ayAbort() logic 
20d82 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  */../*.** The fo
20d83 6c 6c 6f 77 69 6e 67 20 74 79 70 65 20 61 6e 64  llowing type and
20d84 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 75 73   function are us
20d85 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  ed to iterate th
20d86 72 6f 75 67 68 20 61 6c 6c 20 6f 70 63 6f 64 65  rough all opcode
20d87 73 0a 2a 2a 20 69 6e 20 61 20 56 64 62 65 20 6d  s.** in a Vdbe m
20d88 61 69 6e 20 70 72 6f 67 72 61 6d 20 61 6e 64 20  ain program and 
20d89 65 61 63 68 20 6f 66 20 74 68 65 20 73 75 62 2d  each of the sub-
20d8a 70 72 6f 67 72 61 6d 73 20 28 74 72 69 67 67 65  programs (trigge
20d8b 72 73 29 20 69 74 20 6d 61 79 20 0a 2a 2a 20 69  rs) it may .** i
20d8c 6e 76 6f 6b 65 20 64 69 72 65 63 74 6c 79 20 6f  nvoke directly o
20d8d 72 20 69 6e 64 69 72 65 63 74 6c 79 2e 20 49 74  r indirectly. It
20d8e 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20   should be used 
20d8f 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
20d90 2a 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 2a 2a 20  *   Op *pOp;.** 
20d91 20 20 56 64 62 65 4f 70 49 74 65 72 20 73 49 74    VdbeOpIter sIt
20d92 65 72 3b 0a 2a 2a 0a 2a 2a 20 20 20 6d 65 6d 73  er;.**.**   mems
20d93 65 74 28 26 73 49 74 65 72 2c 20 30 2c 20 73 69  et(&sIter, 0, si
20d94 7a 65 6f 66 28 73 49 74 65 72 29 29 3b 0a 2a 2a  zeof(sIter));.**
20d95 20 20 20 73 49 74 65 72 2e 76 20 3d 20 76 3b 20     sIter.v = v; 
20d96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d97 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 76 20             // v 
20d98 69 73 20 6f 66 20 74 79 70 65 20 56 64 62 65 2a  is of type Vdbe*
20d99 20 0a 2a 2a 20 20 20 77 68 69 6c 65 28 20 28 70   .**   while( (p
20d9a 4f 70 20 3d 20 6f 70 49 74 65 72 4e 65 78 74 28  Op = opIterNext(
20d9b 26 73 49 74 65 72 29 29 20 29 7b 0a 2a 2a 20 20  &sIter)) ){.**  
20d9c 20 20 20 2f 2f 20 44 6f 20 73 6f 6d 65 74 68 69     // Do somethi
20d9d 6e 67 20 77 69 74 68 20 70 4f 70 0a 2a 2a 20 20  ng with pOp.**  
20d9e 20 7d 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 44   }.**   sqlite3D
20d9f 62 46 72 65 65 28 76 2d 3e 64 62 2c 20 73 49 74  bFree(v->db, sIt
20da0 65 72 2e 61 70 53 75 62 29 3b 0a 2a 2a 20 0a 2a  er.apSub);.** .*
20da1 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
20da2 20 56 64 62 65 4f 70 49 74 65 72 20 56 64 62 65   VdbeOpIter Vdbe
20da3 4f 70 49 74 65 72 3b 0a 73 74 72 75 63 74 20 56  OpIter;.struct V
20da4 64 62 65 4f 70 49 74 65 72 20 7b 0a 20 20 56 64  dbeOpIter {.  Vd
20da5 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
20da6 20 20 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65           /* Vdbe
20da7 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f   to iterate thro
20da8 75 67 68 20 74 68 65 20 6f 70 63 6f 64 65 73 20  ugh the opcodes 
20da9 6f 66 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72  of */.  SubProgr
20daa 61 6d 20 2a 2a 61 70 53 75 62 3b 20 20 20 20 20  am **apSub;     
20dab 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73     /* Array of s
20dac 75 62 70 72 6f 67 72 61 6d 73 20 2a 2f 0a 20 20  ubprograms */.  
20dad 69 6e 74 20 6e 53 75 62 3b 20 20 20 20 20 20 20  int nSub;       
20dae 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
20daf 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
20db0 69 6e 20 61 70 53 75 62 20 2a 2f 0a 20 20 69 6e  in apSub */.  in
20db1 74 20 69 41 64 64 72 3b 20 20 20 20 20 20 20 20  t iAddr;        
20db2 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
20db3 65 73 73 20 6f 66 20 6e 65 78 74 20 69 6e 73 74  ess of next inst
20db4 72 75 63 74 69 6f 6e 20 74 6f 20 72 65 74 75 72  ruction to retur
20db5 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 75 62 3b  n */.  int iSub;
20db6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20db7 20 20 2f 2a 20 30 20 3d 20 6d 61 69 6e 20 70 72    /* 0 = main pr
20db8 6f 67 72 61 6d 2c 20 31 20 3d 20 66 69 72 73 74  ogram, 1 = first
20db9 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 65 74 63   sub-program etc
20dba 2e 20 2a 2f 0a 7d 3b 0a 73 74 61 74 69 63 20 4f  . */.};.static O
20dbb 70 20 2a 6f 70 49 74 65 72 4e 65 78 74 28 56 64  p *opIterNext(Vd
20dbc 62 65 4f 70 49 74 65 72 20 2a 70 29 7b 0a 20 20  beOpIter *p){.  
20dbd 56 64 62 65 20 2a 76 20 3d 20 70 2d 3e 76 3b 0a  Vdbe *v = p->v;.
20dbe 20 20 4f 70 20 2a 70 52 65 74 20 3d 20 30 3b 0a    Op *pRet = 0;.
20dbf 20 20 4f 70 20 2a 61 4f 70 3b 0a 20 20 69 6e 74    Op *aOp;.  int
20dc0 20 6e 4f 70 3b 0a 0a 20 20 69 66 28 20 70 2d 3e   nOp;..  if( p->
20dc1 69 53 75 62 3c 3d 70 2d 3e 6e 53 75 62 20 29 7b  iSub<=p->nSub ){
20dc2 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 53 75  ..    if( p->iSu
20dc3 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 4f  b==0 ){.      aO
20dc4 70 20 3d 20 76 2d 3e 61 4f 70 3b 0a 20 20 20 20  p = v->aOp;.    
20dc5 20 20 6e 4f 70 20 3d 20 76 2d 3e 6e 4f 70 3b 0a    nOp = v->nOp;.
20dc6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
20dc7 20 61 4f 70 20 3d 20 70 2d 3e 61 70 53 75 62 5b   aOp = p->apSub[
20dc8 70 2d 3e 69 53 75 62 2d 31 5d 2d 3e 61 4f 70 3b  p->iSub-1]->aOp;
20dc9 0a 20 20 20 20 20 20 6e 4f 70 20 3d 20 70 2d 3e  .      nOp = p->
20dca 61 70 53 75 62 5b 70 2d 3e 69 53 75 62 2d 31 5d  apSub[p->iSub-1]
20dcb 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a 20 20 20  ->nOp;.    }.   
20dcc 20 61 73 73 65 72 74 28 20 70 2d 3e 69 41 64 64   assert( p->iAdd
20dcd 72 3c 6e 4f 70 20 29 3b 0a 0a 20 20 20 20 70 52  r<nOp );..    pR
20dce 65 74 20 3d 20 26 61 4f 70 5b 70 2d 3e 69 41 64  et = &aOp[p->iAd
20dcf 64 72 5d 3b 0a 20 20 20 20 70 2d 3e 69 41 64 64  dr];.    p->iAdd
20dd0 72 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  r++;.    if( p->
20dd1 69 41 64 64 72 3d 3d 6e 4f 70 20 29 7b 0a 20 20  iAddr==nOp ){.  
20dd2 20 20 20 20 70 2d 3e 69 53 75 62 2b 2b 3b 0a 20      p->iSub++;. 
20dd3 20 20 20 20 20 70 2d 3e 69 41 64 64 72 20 3d 20       p->iAddr = 
20dd4 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  0;.    }.  .    
20dd5 69 66 28 20 70 52 65 74 2d 3e 70 34 74 79 70 65  if( pRet->p4type
20dd6 3d 3d 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20  ==P4_SUBPROGRAM 
20dd7 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 79  ){.      int nBy
20dd8 74 65 20 3d 20 28 70 2d 3e 6e 53 75 62 2b 31 29  te = (p->nSub+1)
20dd9 2a 73 69 7a 65 6f 66 28 53 75 62 50 72 6f 67 72  *sizeof(SubProgr
20dda 61 6d 2a 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  am*);.      int 
20ddb 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  j;.      for(j=0
20ddc 3b 20 6a 3c 70 2d 3e 6e 53 75 62 3b 20 6a 2b 2b  ; j<p->nSub; j++
20ddd 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
20dde 2d 3e 61 70 53 75 62 5b 6a 5d 3d 3d 70 52 65 74  ->apSub[j]==pRet
20ddf 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d 20 29 20  ->p4.pProgram ) 
20de0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
20de1 20 20 20 20 20 69 66 28 20 6a 3d 3d 70 2d 3e 6e       if( j==p->n
20de2 53 75 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70  Sub ){.        p
20de3 2d 3e 61 70 53 75 62 20 3d 20 73 71 6c 69 74 65  ->apSub = sqlite
20de4 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  3DbReallocOrFree
20de5 28 76 2d 3e 64 62 2c 20 70 2d 3e 61 70 53 75 62  (v->db, p->apSub
20de6 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20  , nByte);.      
20de7 20 20 69 66 28 20 21 70 2d 3e 61 70 53 75 62 20    if( !p->apSub 
20de8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 52 65  ){.          pRe
20de9 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  t = 0;.        }
20dea 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
20deb 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 6e 53 75 62  p->apSub[p->nSub
20dec 2b 2b 5d 20 3d 20 70 52 65 74 2d 3e 70 34 2e 70  ++] = pRet->p4.p
20ded 50 72 6f 67 72 61 6d 3b 0a 20 20 20 20 20 20 20  Program;.       
20dee 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
20def 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
20df0 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  Ret;.}../*.** Ch
20df1 65 63 6b 20 69 66 20 74 68 65 20 70 72 6f 67 72  eck if the progr
20df2 61 6d 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  am stored in the
20df3 20 56 4d 20 61 73 73 6f 63 69 61 74 65 64 20 77   VM associated w
20df4 69 74 68 20 70 50 61 72 73 65 20 6d 61 79 0a 2a  ith pParse may.*
20df5 2a 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 54  * throw an ABORT
20df6 20 65 78 63 65 70 74 69 6f 6e 20 28 63 61 75 73   exception (caus
20df7 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ing the statemen
20df8 74 2c 20 62 75 74 20 6e 6f 74 20 65 6e 74 69 72  t, but not entir
20df9 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a  e transaction.**
20dfa 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61   to be rolled ba
20dfb 63 6b 29 2e 20 54 68 69 73 20 63 6f 6e 64 69 74  ck). This condit
20dfc 69 6f 6e 20 69 73 20 74 72 75 65 20 69 66 20 74  ion is true if t
20dfd 68 65 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20  he main program 
20dfe 6f 72 20 61 6e 79 0a 2a 2a 20 73 75 62 2d 70 72  or any.** sub-pr
20dff 6f 67 72 61 6d 73 20 63 6f 6e 74 61 69 6e 73 20  ograms contains 
20e00 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  any of the follo
20e01 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  wing:.**.**   * 
20e02 20 4f 50 5f 48 61 6c 74 20 77 69 74 68 20 50 31   OP_Halt with P1
20e03 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49  =SQLITE_CONSTRAI
20e04 4e 54 20 61 6e 64 20 50 32 3d 4f 45 5f 41 62 6f  NT and P2=OE_Abo
20e05 72 74 2e 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48  rt..**   *  OP_H
20e06 61 6c 74 49 66 4e 75 6c 6c 20 77 69 74 68 20 50  altIfNull with P
20e07 31 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  1=SQLITE_CONSTRA
20e08 49 4e 54 20 61 6e 64 20 50 32 3d 4f 45 5f 41 62  INT and P2=OE_Ab
20e09 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f  ort..**   *  OP_
20e0a 44 65 73 74 72 6f 79 0a 2a 2a 20 20 20 2a 20 20  Destroy.**   *  
20e0b 4f 50 5f 56 55 70 64 61 74 65 0a 2a 2a 20 20 20  OP_VUpdate.**   
20e0c 2a 20 20 4f 50 5f 56 52 65 6e 61 6d 65 0a 2a 2a  *  OP_VRename.**
20e0d 20 20 20 2a 20 20 4f 50 5f 46 6b 43 6f 75 6e 74     *  OP_FkCount
20e0e 65 72 20 77 69 74 68 20 50 32 3d 3d 30 20 28 69  er with P2==0 (i
20e0f 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67 6e  mmediate foreign
20e10 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 29   key constraint)
20e11 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 63 68 65 63  .**.** Then chec
20e12 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65  k that the value
20e13 20 6f 66 20 50 61 72 73 65 2e 6d 61 79 41 62 6f   of Parse.mayAbo
20e14 72 74 20 69 73 20 74 72 75 65 20 69 66 20 61 6e  rt is true if an
20e15 0a 2a 2a 20 41 42 4f 52 54 20 6d 61 79 20 62 65  .** ABORT may be
20e16 20 74 68 72 6f 77 6e 2c 20 6f 72 20 66 61 6c 73   thrown, or fals
20e17 65 20 6f 74 68 65 72 77 69 73 65 2e 20 52 65 74  e otherwise. Ret
20e18 75 72 6e 20 74 72 75 65 20 69 66 20 69 74 20 64  urn true if it d
20e19 6f 65 73 0a 2a 2a 20 6d 61 74 63 68 2c 20 6f 72  oes.** match, or
20e1a 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65   false otherwise
20e1b 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
20e1c 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62  is intended to b
20e1d 65 20 75 73 65 64 20 61 73 0a 2a 2a 20 70 61 72  e used as.** par
20e1e 74 20 6f 66 20 61 6e 20 61 73 73 65 72 74 20 73  t of an assert s
20e1f 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20  tatement in the 
20e20 63 6f 6d 70 69 6c 65 72 2e 20 53 69 6d 69 6c 61  compiler. Simila
20e21 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 73  r to:.**.**   as
20e22 73 65 72 74 28 20 73 71 6c 69 74 65 33 56 64 62  sert( sqlite3Vdb
20e23 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28  eAssertMayAbort(
20e24 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 70  pParse->pVdbe, p
20e25 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f 72 74 29  Parse->mayAbort)
20e26 20 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52   );.*/.SQLITE_PR
20e27 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
20e28 33 56 64 62 65 41 73 73 65 72 74 4d 61 79 41 62  3VdbeAssertMayAb
20e29 6f 72 74 28 56 64 62 65 20 2a 76 2c 20 69 6e 74  ort(Vdbe *v, int
20e2a 20 6d 61 79 41 62 6f 72 74 29 7b 0a 20 20 69 6e   mayAbort){.  in
20e2b 74 20 68 61 73 41 62 6f 72 74 20 3d 20 30 3b 0a  t hasAbort = 0;.
20e2c 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62    Op *pOp;.  Vdb
20e2d 65 4f 70 49 74 65 72 20 73 49 74 65 72 3b 0a 20  eOpIter sIter;. 
20e2e 20 6d 65 6d 73 65 74 28 26 73 49 74 65 72 2c 20   memset(&sIter, 
20e2f 30 2c 20 73 69 7a 65 6f 66 28 73 49 74 65 72 29  0, sizeof(sIter)
20e30 29 3b 0a 20 20 73 49 74 65 72 2e 76 20 3d 20 76  );.  sIter.v = v
20e31 3b 0a 0a 20 20 77 68 69 6c 65 28 20 28 70 4f 70  ;..  while( (pOp
20e32 20 3d 20 6f 70 49 74 65 72 4e 65 78 74 28 26 73   = opIterNext(&s
20e33 49 74 65 72 29 29 21 3d 30 20 29 7b 0a 20 20 20  Iter))!=0 ){.   
20e34 20 69 6e 74 20 6f 70 63 6f 64 65 20 3d 20 70 4f   int opcode = pO
20e35 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 69  p->opcode;.    i
20e36 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 44 65  f( opcode==OP_De
20e37 73 74 72 6f 79 20 7c 7c 20 6f 70 63 6f 64 65 3d  stroy || opcode=
20e38 3d 4f 50 5f 56 55 70 64 61 74 65 20 7c 7c 20 6f  =OP_VUpdate || o
20e39 70 63 6f 64 65 3d 3d 4f 50 5f 56 52 65 6e 61 6d  pcode==OP_VRenam
20e3a 65 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e .#ifndef SQLIT
20e3b 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
20e3c 45 59 0a 20 20 20 20 20 7c 7c 20 28 6f 70 63 6f  EY.     || (opco
20e3d 64 65 3d 3d 4f 50 5f 46 6b 43 6f 75 6e 74 65 72  de==OP_FkCounter
20e3e 20 26 26 20 70 4f 70 2d 3e 70 31 3d 3d 30 20 26   && pOp->p1==0 &
20e3f 26 20 70 4f 70 2d 3e 70 32 3d 3d 31 29 20 0a 23  & pOp->p2==1) .#
20e40 65 6e 64 69 66 0a 20 20 20 20 20 7c 7c 20 28 28  endif.     || ((
20e41 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20  opcode==OP_Halt 
20e42 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61  || opcode==OP_Ha
20e43 6c 74 49 66 4e 75 6c 6c 29 20 0a 20 20 20 20 20  ltIfNull) .     
20e44 20 26 26 20 28 70 4f 70 2d 3e 70 31 3d 3d 53 51   && (pOp->p1==SQ
20e45 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20  LITE_CONSTRAINT 
20e46 26 26 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 41  && pOp->p2==OE_A
20e47 62 6f 72 74 29 29 0a 20 20 20 20 29 7b 0a 20 20  bort)).    ){.  
20e48 20 20 20 20 68 61 73 41 62 6f 72 74 20 3d 20 31      hasAbort = 1
20e49 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
20e4a 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
20e4b 65 33 44 62 46 72 65 65 28 76 2d 3e 64 62 2c 20  e3DbFree(v->db, 
20e4c 73 49 74 65 72 2e 61 70 53 75 62 29 3b 0a 0a 20  sIter.apSub);.. 
20e4d 20 2f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20   /* Return true 
20e4e 69 66 20 68 61 73 41 62 6f 72 74 3d 3d 6d 61 79  if hasAbort==may
20e4f 41 62 6f 72 74 2e 20 4f 72 20 69 66 20 61 20 6d  Abort. Or if a m
20e50 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63  alloc failure oc
20e51 63 75 72 65 64 2e 0a 20 20 2a 2a 20 49 66 20 6d  cured..  ** If m
20e52 61 6c 6c 6f 63 20 66 61 69 6c 65 64 2c 20 74 68  alloc failed, th
20e53 65 6e 20 74 68 65 20 77 68 69 6c 65 28 29 20 6c  en the while() l
20e54 6f 6f 70 20 61 62 6f 76 65 20 6d 61 79 20 6e 6f  oop above may no
20e55 74 20 68 61 76 65 20 69 74 65 72 61 74 65 64 0a  t have iterated.
20e56 20 20 2a 2a 20 74 68 72 6f 75 67 68 20 61 6c 6c    ** through all
20e57 20 6f 70 63 6f 64 65 73 20 61 6e 64 20 68 61 73   opcodes and has
20e58 41 62 6f 72 74 20 6d 61 79 20 62 65 20 73 65 74  Abort may be set
20e59 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e 20 52 65   incorrectly. Re
20e5a 74 75 72 6e 0a 20 20 2a 2a 20 74 72 75 65 20 66  turn.  ** true f
20e5b 6f 72 20 74 68 69 73 20 63 61 73 65 20 74 6f 20  or this case to 
20e5c 70 72 65 76 65 6e 74 20 74 68 65 20 61 73 73 65  prevent the asse
20e5d 72 74 28 29 20 69 6e 20 74 68 65 20 63 61 6c 6c  rt() in the call
20e5e 65 72 73 20 66 72 61 6d 65 0a 20 20 2a 2a 20 66  ers frame.  ** f
20e5f 72 6f 6d 20 66 61 69 6c 69 6e 67 2e 20 20 2a 2f  rom failing.  */
20e60 0a 20 20 72 65 74 75 72 6e 20 28 20 76 2d 3e 64  .  return ( v->d
20e61 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
20e62 7c 7c 20 68 61 73 41 62 6f 72 74 3d 3d 6d 61 79  || hasAbort==may
20e63 41 62 6f 72 74 20 29 3b 0a 7d 0a 23 65 6e 64 69  Abort );.}.#endi
20e64 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55  f /* SQLITE_DEBU
20e65 47 20 2d 20 74 68 65 20 73 71 6c 69 74 65 33 41  G - the sqlite3A
20e66 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 29 20  ssertMayAbort() 
20e67 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a  function */../*.
20e68 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  ** Loop through 
20e69 74 68 65 20 70 72 6f 67 72 61 6d 20 6c 6f 6f 6b  the program look
20e6a 69 6e 67 20 66 6f 72 20 50 32 20 76 61 6c 75 65  ing for P2 value
20e6b 73 20 74 68 61 74 20 61 72 65 20 6e 65 67 61 74  s that are negat
20e6c 69 76 65 0a 2a 2a 20 6f 6e 20 6a 75 6d 70 20 69  ive.** on jump i
20e6d 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 45 61  nstructions.  Ea
20e6e 63 68 20 73 75 63 68 20 76 61 6c 75 65 20 69 73  ch such value is
20e6f 20 61 20 6c 61 62 65 6c 2e 20 20 52 65 73 6f 6c   a label.  Resol
20e70 76 65 20 74 68 65 0a 2a 2a 20 6c 61 62 65 6c 20  ve the.** label 
20e71 62 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 50  by setting the P
20e72 32 20 76 61 6c 75 65 20 74 6f 20 69 74 73 20 63  2 value to its c
20e73 6f 72 72 65 63 74 20 6e 6f 6e 2d 7a 65 72 6f 20  orrect non-zero 
20e74 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  value..**.** Thi
20e75 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
20e76 6c 65 64 20 6f 6e 63 65 20 61 66 74 65 72 20 61  led once after a
20e77 6c 6c 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20  ll opcodes have 
20e78 62 65 65 6e 20 69 6e 73 65 72 74 65 64 2e 0a 2a  been inserted..*
20e79 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 2a 70  *.** Variable *p
20e7a 4d 61 78 46 75 6e 63 41 72 67 73 20 69 73 20 73  MaxFuncArgs is s
20e7b 65 74 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75  et to the maximu
20e7c 6d 20 76 61 6c 75 65 20 6f 66 20 61 6e 79 20 50  m value of any P
20e7d 32 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74  2 argument .** t
20e7e 6f 20 61 6e 20 4f 50 5f 46 75 6e 63 74 69 6f 6e  o an OP_Function
20e7f 2c 20 4f 50 5f 41 67 67 53 74 65 70 20 6f 72 20  , OP_AggStep or 
20e80 4f 50 5f 56 46 69 6c 74 65 72 20 6f 70 63 6f 64  OP_VFilter opcod
20e81 65 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  e. This is used 
20e82 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  by .** sqlite3Vd
20e83 62 65 4d 61 6b 65 52 65 61 64 79 28 29 20 74 6f  beMakeReady() to
20e84 20 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61   size the Vdbe.a
20e85 70 41 72 67 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a  pArg[] array..**
20e86 0a 2a 2a 20 54 68 65 20 4f 70 2e 6f 70 66 6c 61  .** The Op.opfla
20e87 67 73 20 66 69 65 6c 64 20 69 73 20 73 65 74 20  gs field is set 
20e88 6f 6e 20 61 6c 6c 20 6f 70 63 6f 64 65 73 2e 0a  on all opcodes..
20e89 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
20e8a 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 56  esolveP2Values(V
20e8b 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70 4d 61  dbe *p, int *pMa
20e8c 78 46 75 6e 63 41 72 67 73 29 7b 0a 20 20 69 6e  xFuncArgs){.  in
20e8d 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4d 61 78 41  t i;.  int nMaxA
20e8e 72 67 73 20 3d 20 2a 70 4d 61 78 46 75 6e 63 41  rgs = *pMaxFuncA
20e8f 72 67 73 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a  rgs;.  Op *pOp;.
20e90 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 20 3d 20    int *aLabel = 
20e91 70 2d 3e 61 4c 61 62 65 6c 3b 0a 20 20 70 2d 3e  p->aLabel;.  p->
20e92 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20  readOnly = 1;.  
20e93 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 2c 20  for(pOp=p->aOp, 
20e94 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e 3d 30  i=p->nOp-1; i>=0
20e95 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b 0a 20  ; i--, pOp++){. 
20e96 20 20 20 75 38 20 6f 70 63 6f 64 65 20 3d 20 70     u8 opcode = p
20e97 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20 20 20  Op->opcode;..   
20e98 20 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 3d 20   pOp->opflags = 
20e99 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 50 72 6f  sqlite3OpcodePro
20e9a 70 65 72 74 79 5b 6f 70 63 6f 64 65 5d 3b 0a 20  perty[opcode];. 
20e9b 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f     if( opcode==O
20e9c 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70  P_Function || op
20e9d 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65 70  code==OP_AggStep
20e9e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f   ){.      if( pO
20e9f 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20 29  p->p5>nMaxArgs )
20ea0 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d   nMaxArgs = pOp-
20ea1 3e 70 35 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  >p5;.    }else i
20ea2 66 28 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54  f( (opcode==OP_T
20ea3 72 61 6e 73 61 63 74 69 6f 6e 20 26 26 20 70 4f  ransaction && pO
20ea4 70 2d 3e 70 32 21 3d 30 29 20 7c 7c 20 6f 70 63  p->p2!=0) || opc
20ea5 6f 64 65 3d 3d 4f 50 5f 56 61 63 75 75 6d 20 29  ode==OP_Vacuum )
20ea6 7b 0a 20 20 20 20 20 20 70 2d 3e 72 65 61 64 4f  {.      p->readO
20ea7 6e 6c 79 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66  nly = 0;.#ifndef
20ea8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
20ea9 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65  TUALTABLE.    }e
20eaa 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d  lse if( opcode==
20eab 4f 50 5f 56 55 70 64 61 74 65 20 29 7b 0a 20 20  OP_VUpdate ){.  
20eac 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e      if( pOp->p2>
20ead 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41  nMaxArgs ) nMaxA
20eae 72 67 73 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  rgs = pOp->p2;. 
20eaf 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63     }else if( opc
20eb0 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74 65 72 20  ode==OP_VFilter 
20eb1 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a  ){.      int n;.
20eb2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
20eb3 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 20 29 3b  >nOp - i >= 3 );
20eb4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
20eb5 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f  Op[-1].opcode==O
20eb6 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a 20 20 20  P_Integer );.   
20eb7 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70     n = pOp[-1].p
20eb8 31 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 6e  1;.      if( n>n
20eb9 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72  MaxArgs ) nMaxAr
20eba 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a 20  gs = n;.#endif. 
20ebb 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63     }else if( opc
20ebc 6f 64 65 3d 3d 4f 50 5f 4e 65 78 74 20 7c 7c 20  ode==OP_Next || 
20ebd 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 6f 72 74 65  opcode==OP_Sorte
20ebe 72 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70  rNext ){.      p
20ebf 4f 70 2d 3e 70 34 2e 78 41 64 76 61 6e 63 65 20  Op->p4.xAdvance 
20ec0 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  = sqlite3BtreeNe
20ec1 78 74 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70  xt;.      pOp->p
20ec2 34 74 79 70 65 20 3d 20 50 34 5f 41 44 56 41 4e  4type = P4_ADVAN
20ec3 43 45 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  CE;.    }else if
20ec4 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 50 72 65  ( opcode==OP_Pre
20ec5 76 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 2d 3e  v ){.      pOp->
20ec6 70 34 2e 78 41 64 76 61 6e 63 65 20 3d 20 73 71  p4.xAdvance = sq
20ec7 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f  lite3BtreePrevio
20ec8 75 73 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70  us;.      pOp->p
20ec9 34 74 79 70 65 20 3d 20 50 34 5f 41 44 56 41 4e  4type = P4_ADVAN
20eca 43 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  CE;.    }..    i
20ecb 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73  f( (pOp->opflags
20ecc 20 26 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 21 3d   & OPFLG_JUMP)!=
20ecd 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20 29  0 && pOp->p2<0 )
20ece 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
20ecf 2d 31 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c  -1-pOp->p2<p->nL
20ed0 61 62 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 4f  abel );.      pO
20ed1 70 2d 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d  p->p2 = aLabel[-
20ed2 31 2d 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20  1-pOp->p2];.    
20ed3 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44  }.  }.  sqlite3D
20ed4 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e  bFree(p->db, p->
20ed5 61 4c 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c  aLabel);.  p->aL
20ed6 61 62 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d  abel = 0;..  *pM
20ed7 61 78 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d 61  axFuncArgs = nMa
20ed8 78 41 72 67 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  xArgs;.}../*.** 
20ed9 52 65 74 75 72 6e 20 74 68 65 20 61 64 64 72 65  Return the addre
20eda 73 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69  ss of the next i
20edb 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65  nstruction to be
20edc 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51   inserted..*/.SQ
20edd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
20ede 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
20edf 65 6e 74 41 64 64 72 28 56 64 62 65 20 2a 70 29  entAddr(Vdbe *p)
20ee0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d  {.  assert( p->m
20ee1 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
20ee2 5f 49 4e 49 54 20 29 3b 0a 20 20 72 65 74 75 72  _INIT );.  retur
20ee3 6e 20 70 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a  n p->nOp;.}../*.
20ee4 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
20ee5 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
20ee6 65 72 20 74 6f 20 74 68 65 20 61 72 72 61 79 20  er to the array 
20ee7 6f 66 20 6f 70 63 6f 64 65 73 20 61 73 73 6f 63  of opcodes assoc
20ee8 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 74 68  iated with.** th
20ee9 65 20 56 64 62 65 20 70 61 73 73 65 64 20 61 73  e Vdbe passed as
20eea 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
20eeb 65 6e 74 2e 20 49 74 20 69 73 20 74 68 65 20 63  ent. It is the c
20eec 61 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62  allers responsib
20eed 69 6c 69 74 79 0a 2a 2a 20 74 6f 20 61 72 72 61  ility.** to arra
20eee 6e 67 65 20 66 6f 72 20 74 68 65 20 72 65 74 75  nge for the retu
20eef 72 6e 65 64 20 61 72 72 61 79 20 74 6f 20 62 65  rned array to be
20ef0 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65   eventually free
20ef1 64 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20  d using the .** 
20ef2 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 28  vdbeFreeOpArray(
20ef3 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  ) function..**.*
20ef4 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69  * Before returni
20ef5 6e 67 2c 20 2a 70 6e 4f 70 20 69 73 20 73 65 74  ng, *pnOp is set
20ef6 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
20ef7 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  f entries in the
20ef8 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 72 72   returned.** arr
20ef9 61 79 2e 20 41 6c 73 6f 2c 20 2a 70 6e 4d 61 78  ay. Also, *pnMax
20efa 41 72 67 20 69 73 20 73 65 74 20 74 6f 20 74 68  Arg is set to th
20efb 65 20 6c 61 72 67 65 72 20 6f 66 20 69 74 73 20  e larger of its 
20efc 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 61 6e  current value an
20efd 64 20 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72  d .** the number
20efe 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74   of entries in t
20eff 68 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20  he Vdbe.apArg[] 
20f00 61 72 72 61 79 20 72 65 71 75 69 72 65 64 20 74  array required t
20f01 6f 20 65 78 65 63 75 74 65 20 74 68 65 20 0a 2a  o execute the .*
20f02 2a 20 72 65 74 75 72 6e 65 64 20 70 72 6f 67 72  * returned progr
20f03 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  am..*/.SQLITE_PR
20f04 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71  IVATE VdbeOp *sq
20f05 6c 69 74 65 33 56 64 62 65 54 61 6b 65 4f 70 41  lite3VdbeTakeOpA
20f06 72 72 61 79 28 56 64 62 65 20 2a 70 2c 20 69 6e  rray(Vdbe *p, in
20f07 74 20 2a 70 6e 4f 70 2c 20 69 6e 74 20 2a 70 6e  t *pnOp, int *pn
20f08 4d 61 78 41 72 67 29 7b 0a 20 20 56 64 62 65 4f  MaxArg){.  VdbeO
20f09 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b  p *aOp = p->aOp;
20f0a 0a 20 20 61 73 73 65 72 74 28 20 61 4f 70 20 26  .  assert( aOp &
20f0b 26 20 21 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  & !p->db->malloc
20f0c 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 2f 2a 20  Failed );..  /* 
20f0d 43 68 65 63 6b 20 74 68 61 74 20 73 71 6c 69 74  Check that sqlit
20f0e 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28  e3VdbeUsesBtree(
20f0f 29 20 77 61 73 20 6e 6f 74 20 63 61 6c 6c 65 64  ) was not called
20f10 20 6f 6e 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20   on this VM */. 
20f11 20 61 73 73 65 72 74 28 20 70 2d 3e 62 74 72 65   assert( p->btre
20f12 65 4d 61 73 6b 3d 3d 30 20 29 3b 0a 0a 20 20 72  eMask==0 );..  r
20f13 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 70  esolveP2Values(p
20f14 2c 20 70 6e 4d 61 78 41 72 67 29 3b 0a 20 20 2a  , pnMaxArg);.  *
20f15 70 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20  pnOp = p->nOp;. 
20f16 20 70 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20 20 72   p->aOp = 0;.  r
20f17 65 74 75 72 6e 20 61 4f 70 3b 0a 7d 0a 0a 2f 2a  eturn aOp;.}../*
20f18 0a 2a 2a 20 41 64 64 20 61 20 77 68 6f 6c 65 20  .** Add a whole 
20f19 6c 69 73 74 20 6f 66 20 6f 70 65 72 61 74 69 6f  list of operatio
20f1a 6e 73 20 74 6f 20 74 68 65 20 6f 70 65 72 61 74  ns to the operat
20f1b 69 6f 6e 20 73 74 61 63 6b 2e 20 20 52 65 74 75  ion stack.  Retu
20f1c 72 6e 20 74 68 65 0a 2a 2a 20 61 64 64 72 65 73  rn the.** addres
20f1d 73 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 6f  s of the first o
20f1e 70 65 72 61 74 69 6f 6e 20 61 64 64 65 64 2e 0a  peration added..
20f1f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20f20 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
20f21 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 65 20  eAddOpList(Vdbe 
20f22 2a 70 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62  *p, int nOp, Vdb
20f23 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61  eOpList const *a
20f24 4f 70 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b  Op){.  int addr;
20f25 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61  .  assert( p->ma
20f26 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
20f27 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d  INIT );.  if( p-
20f28 3e 6e 4f 70 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e  >nOp + nOp > p->
20f29 6e 4f 70 41 6c 6c 6f 63 20 26 26 20 67 72 6f 77  nOpAlloc && grow
20f2a 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20  OpArray(p) ){.  
20f2b 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
20f2c 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b    addr = p->nOp;
20f2d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 6e 4f  .  if( ALWAYS(nO
20f2e 70 3e 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  p>0) ){.    int 
20f2f 69 3b 0a 20 20 20 20 56 64 62 65 4f 70 4c 69 73  i;.    VdbeOpLis
20f30 74 20 63 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61  t const *pIn = a
20f31 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  Op;.    for(i=0;
20f32 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e   i<nOp; i++, pIn
20f33 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70  ++){.      int p
20f34 32 20 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20  2 = pIn->p2;.   
20f35 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 75 74 20     VdbeOp *pOut 
20f36 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72  = &p->aOp[i+addr
20f37 5d 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f  ];.      pOut->o
20f38 70 63 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63  pcode = pIn->opc
20f39 6f 64 65 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d  ode;.      pOut-
20f3a 3e 70 31 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20  >p1 = pIn->p1;. 
20f3b 20 20 20 20 20 69 66 28 20 70 32 3c 30 20 26 26       if( p2<0 &&
20f3c 20 28 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 50   (sqlite3OpcodeP
20f3d 72 6f 70 65 72 74 79 5b 70 4f 75 74 2d 3e 6f 70  roperty[pOut->op
20f3e 63 6f 64 65 5d 20 26 20 4f 50 46 4c 47 5f 4a 55  code] & OPFLG_JU
20f3f 4d 50 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  MP)!=0 ){.      
20f40 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64    pOut->p2 = add
20f41 72 20 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20  r + ADDR(p2);.  
20f42 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
20f43 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32     pOut->p2 = p2
20f44 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
20f45 70 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e  pOut->p3 = pIn->
20f46 70 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e  p3;.      pOut->
20f47 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55  p4type = P4_NOTU
20f48 53 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d  SED;.      pOut-
20f49 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20  >p4.p = 0;.     
20f4a 20 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23   pOut->p5 = 0;.#
20f4b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
20f4c 55 47 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a  UG.      pOut->z
20f4d 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20  Comment = 0;.   
20f4e 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
20f4f 62 65 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a  beAddopTrace ){.
20f50 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
20f51 64 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b  dbePrintOp(0, i+
20f52 61 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b  addr, &p->aOp[i+
20f53 61 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  addr]);.      }.
20f54 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20  #endif.    }.   
20f55 20 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a   p->nOp += nOp;.
20f56 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64    }.  return add
20f57 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  r;.}../*.** Chan
20f58 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ge the value of 
20f59 74 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66  the P1 operand f
20f5a 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e  or a specific in
20f5b 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68  struction..** Th
20f5c 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
20f5d 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67  eful when a larg
20f5e 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61  e program is loa
20f5f 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74  ded from a.** st
20f60 61 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67  atic array using
20f61 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20f62 70 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e  pList but we wan
20f63 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66  t to make a.** f
20f64 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73  ew minor changes
20f65 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e   to the program.
20f66 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20f67 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
20f68 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65  dbeChangeP1(Vdbe
20f69 20 2a 70 2c 20 75 33 32 20 61 64 64 72 2c 20 69   *p, u32 addr, i
20f6a 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72  nt val){.  asser
20f6b 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28  t( p!=0 );.  if(
20f6c 20 28 28 75 33 32 29 70 2d 3e 6e 4f 70 29 3e 61   ((u32)p->nOp)>a
20f6d 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f  ddr ){.    p->aO
20f6e 70 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c  p[addr].p1 = val
20f6f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
20f70 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20  hange the value 
20f71 6f 66 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e  of the P2 operan
20f72 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63  d for a specific
20f73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a   instruction..**
20f74 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
20f75 20 75 73 65 66 75 6c 20 66 6f 72 20 73 65 74 74   useful for sett
20f76 69 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 74 69  ing a jump desti
20f77 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  nation..*/.SQLIT
20f78 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
20f79 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
20f7a 50 32 28 56 64 62 65 20 2a 70 2c 20 75 33 32 20  P2(Vdbe *p, u32 
20f7b 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a  addr, int val){.
20f7c 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29    assert( p!=0 )
20f7d 3b 0a 20 20 69 66 28 20 28 28 75 33 32 29 70 2d  ;.  if( ((u32)p-
20f7e 3e 6e 4f 70 29 3e 61 64 64 72 20 29 7b 0a 20 20  >nOp)>addr ){.  
20f7f 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70    p->aOp[addr].p
20f80 32 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a  2 = val;.  }.}..
20f81 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
20f82 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33   value of the P3
20f83 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73   operand for a s
20f84 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 74  pecific instruct
20f85 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
20f86 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
20f87 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
20f88 56 64 62 65 20 2a 70 2c 20 75 33 32 20 61 64 64  Vdbe *p, u32 add
20f89 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61  r, int val){.  a
20f8a 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
20f8b 20 69 66 28 20 28 28 75 33 32 29 70 2d 3e 6e 4f   if( ((u32)p->nO
20f8c 70 29 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70  p)>addr ){.    p
20f8d 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d  ->aOp[addr].p3 =
20f8e 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   val;.  }.}../*.
20f8f 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61  ** Change the va
20f90 6c 75 65 20 6f 66 20 74 68 65 20 50 35 20 6f 70  lue of the P5 op
20f91 65 72 61 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f  erand for the mo
20f92 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61  st recently.** a
20f93 64 64 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a  dded operation..
20f94 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20f95 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
20f96 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 20  beChangeP5(Vdbe 
20f97 2a 70 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20 61  *p, u8 val){.  a
20f98 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
20f99 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a 20   if( p->aOp ){. 
20f9a 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f     assert( p->nO
20f9b 70 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 61 4f  p>0 );.    p->aO
20f9c 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20 3d  p[p->nOp-1].p5 =
20f9d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   val;.  }.}../*.
20f9e 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 50 32  ** Change the P2
20f9f 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 73 74   operand of inst
20fa0 72 75 63 74 69 6f 6e 20 61 64 64 72 20 73 6f 20  ruction addr so 
20fa1 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74  that it points t
20fa2 6f 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73 73  o.** the address
20fa3 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73   of the next ins
20fa4 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 63  truction to be c
20fa5 6f 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oded..*/.SQLITE_
20fa6 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
20fa7 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
20fa8 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64  (Vdbe *p, int ad
20fa9 64 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 61  dr){.  assert( a
20faa 64 64 72 3e 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d  ddr>=0 || p->db-
20fab 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
20fac 0a 20 20 69 66 28 20 61 64 64 72 3e 3d 30 20 29  .  if( addr>=0 )
20fad 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
20fae 67 65 50 32 28 70 2c 20 61 64 64 72 2c 20 70 2d  geP2(p, addr, p-
20faf 3e 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  >nOp);.}.../*.**
20fb0 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 46 75   If the input Fu
20fb1 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20  ncDef structure 
20fb2 69 73 20 65 70 68 65 6d 65 72 61 6c 2c 20 74 68  is ephemeral, th
20fb3 65 6e 20 66 72 65 65 20 69 74 2e 20 20 49 66 0a  en free it.  If.
20fb4 2a 2a 20 74 68 65 20 46 75 6e 63 44 65 66 20 69  ** the FuncDef i
20fb5 73 20 6e 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20  s not ephermal, 
20fb6 74 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e  then do nothing.
20fb7 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
20fb8 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e  freeEphemeralFun
20fb9 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64  ction(sqlite3 *d
20fba 62 2c 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66  b, FuncDef *pDef
20fbb 29 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  ){.  if( ALWAYS(
20fbc 70 44 65 66 29 20 26 26 20 28 70 44 65 66 2d 3e  pDef) && (pDef->
20fbd 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46  flags & SQLITE_F
20fbe 55 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b  UNC_EPHEM)!=0 ){
20fbf 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
20fc0 65 65 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20  ee(db, pDef);.  
20fc1 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  }.}..static void
20fc2 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79   vdbeFreeOpArray
20fc3 28 73 71 6c 69 74 65 33 20 2a 2c 20 4f 70 20 2a  (sqlite3 *, Op *
20fc4 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 44  , int);../*.** D
20fc5 65 6c 65 74 65 20 61 20 50 34 20 76 61 6c 75 65  elete a P4 value
20fc6 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   if necessary..*
20fc7 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72  /.static void fr
20fc8 65 65 50 34 28 73 71 6c 69 74 65 33 20 2a 64 62  eeP4(sqlite3 *db
20fc9 2c 20 69 6e 74 20 70 34 74 79 70 65 2c 20 76 6f  , int p4type, vo
20fca 69 64 20 2a 70 34 29 7b 0a 20 20 69 66 28 20 70  id *p4){.  if( p
20fcb 34 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  4 ){.    assert(
20fcc 20 64 62 20 29 3b 0a 20 20 20 20 73 77 69 74 63   db );.    switc
20fcd 68 28 20 70 34 74 79 70 65 20 29 7b 0a 20 20 20  h( p4type ){.   
20fce 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a     case P4_REAL:
20fcf 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 49  .      case P4_I
20fd0 4e 54 36 34 3a 0a 20 20 20 20 20 20 63 61 73 65  NT64:.      case
20fd1 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20   P4_DYNAMIC:.   
20fd2 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e     case P4_KEYIN
20fd3 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50  FO:.      case P
20fd4 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20  4_INTARRAY:.    
20fd5 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46    case P4_KEYINF
20fd6 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20  O_HANDOFF: {.   
20fd7 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
20fd8 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20  ee(db, p4);.    
20fd9 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
20fda 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34   }.      case P4
20fdb 5f 4d 50 52 49 4e 54 46 3a 20 7b 0a 20 20 20 20  _MPRINTF: {.    
20fdc 20 20 20 20 69 66 28 20 64 62 2d 3e 70 6e 42 79      if( db->pnBy
20fdd 74 65 73 46 72 65 65 64 3d 3d 30 20 29 20 73 71  tesFreed==0 ) sq
20fde 6c 69 74 65 33 5f 66 72 65 65 28 70 34 29 3b 0a  lite3_free(p4);.
20fdf 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
20fe0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
20fe1 65 20 50 34 5f 56 44 42 45 46 55 4e 43 3a 20 7b  e P4_VDBEFUNC: {
20fe2 0a 20 20 20 20 20 20 20 20 56 64 62 65 46 75 6e  .        VdbeFun
20fe3 63 20 2a 70 56 64 62 65 46 75 6e 63 20 3d 20 28  c *pVdbeFunc = (
20fe4 56 64 62 65 46 75 6e 63 20 2a 29 70 34 3b 0a 20  VdbeFunc *)p4;. 
20fe5 20 20 20 20 20 20 20 66 72 65 65 45 70 68 65 6d         freeEphem
20fe6 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c  eralFunction(db,
20fe7 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e   pVdbeFunc->pFun
20fe8 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  c);.        if( 
20fe9 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 65 64  db->pnBytesFreed
20fea 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64 62  ==0 ) sqlite3Vdb
20feb 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 70  eDeleteAuxData(p
20fec 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a 20 20  VdbeFunc, 0);.  
20fed 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
20fee 72 65 65 28 64 62 2c 20 70 56 64 62 65 46 75 6e  ree(db, pVdbeFun
20fef 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  c);.        brea
20ff0 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
20ff1 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44 45 46   case P4_FUNCDEF
20ff2 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72 65 65  : {.        free
20ff3 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f  EphemeralFunctio
20ff4 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66 2a 29  n(db, (FuncDef*)
20ff5 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  p4);.        bre
20ff6 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
20ff7 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a 20 7b    case P4_MEM: {
20ff8 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d  .        if( db-
20ff9 3e 70 6e 42 79 74 65 73 46 72 65 65 64 3d 3d 30  >pnBytesFreed==0
20ffa 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
20ffb 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 28  lite3ValueFree((
20ffc 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 70  sqlite3_value*)p
20ffd 34 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  4);.        }els
20ffe 65 7b 0a 20 20 20 20 20 20 20 20 20 20 4d 65 6d  e{.          Mem
20fff 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 34 3b 0a   *p = (Mem*)p4;.
21000 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
21001 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
21002 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20  Malloc);.       
21003 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
21004 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20  (db, p);.       
21005 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
21006 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
21007 63 61 73 65 20 50 34 5f 56 54 41 42 20 3a 20 7b  case P4_VTAB : {
21008 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d  .        if( db-
21009 3e 70 6e 42 79 74 65 73 46 72 65 65 64 3d 3d 30  >pnBytesFreed==0
2100a 20 29 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e   ) sqlite3VtabUn
2100b 6c 6f 63 6b 28 28 56 54 61 62 6c 65 20 2a 29 70  lock((VTable *)p
2100c 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  4);.        brea
2100d 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
2100e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  .  }.}../*.** Fr
2100f 65 65 20 74 68 65 20 73 70 61 63 65 20 61 6c 6c  ee the space all
21010 6f 63 61 74 65 64 20 66 6f 72 20 61 4f 70 20 61  ocated for aOp a
21011 6e 64 20 61 6e 79 20 70 34 20 76 61 6c 75 65 73  nd any p4 values
21012 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74   allocated for t
21013 68 65 0a 2a 2a 20 6f 70 63 6f 64 65 73 20 63 6f  he.** opcodes co
21014 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 2e 20  ntained within. 
21015 49 66 20 61 4f 70 20 69 73 20 6e 6f 74 20 4e 55  If aOp is not NU
21016 4c 4c 20 69 74 20 69 73 20 61 73 73 75 6d 65 64  LL it is assumed
21017 20 74 6f 20 63 6f 6e 74 61 69 6e 20 0a 2a 2a 20   to contain .** 
21018 6e 4f 70 20 65 6e 74 72 69 65 73 2e 20 0a 2a 2f  nOp entries. .*/
21019 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 64 62  .static void vdb
2101a 65 46 72 65 65 4f 70 41 72 72 61 79 28 73 71 6c  eFreeOpArray(sql
2101b 69 74 65 33 20 2a 64 62 2c 20 4f 70 20 2a 61 4f  ite3 *db, Op *aO
2101c 70 2c 20 69 6e 74 20 6e 4f 70 29 7b 0a 20 20 69  p, int nOp){.  i
2101d 66 28 20 61 4f 70 20 29 7b 0a 20 20 20 20 4f 70  f( aOp ){.    Op
2101e 20 2a 70 4f 70 3b 0a 20 20 20 20 66 6f 72 28 70   *pOp;.    for(p
2101f 4f 70 3d 61 4f 70 3b 20 70 4f 70 3c 26 61 4f 70  Op=aOp; pOp<&aOp
21020 5b 6e 4f 70 5d 3b 20 70 4f 70 2b 2b 29 7b 0a 20  [nOp]; pOp++){. 
21021 20 20 20 20 20 66 72 65 65 50 34 28 64 62 2c 20       freeP4(db, 
21022 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70  pOp->p4type, pOp
21023 2d 3e 70 34 2e 70 29 3b 0a 23 69 66 64 65 66 20  ->p4.p);.#ifdef 
21024 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
21025 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
21026 28 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65  (db, pOp->zComme
21027 6e 74 29 3b 0a 23 65 6e 64 69 66 20 20 20 20 20  nt);.#endif     
21028 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
21029 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61  ite3DbFree(db, a
2102a 4f 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69  Op);.}../*.** Li
2102b 6e 6b 20 74 68 65 20 53 75 62 50 72 6f 67 72 61  nk the SubProgra
2102c 6d 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20  m object passed 
2102d 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
2102e 67 75 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20  gument into the 
2102f 6c 69 6e 6b 65 64 0a 2a 2a 20 6c 69 73 74 20 61  linked.** list a
21030 74 20 56 64 62 65 2e 70 53 75 62 50 72 6f 67 72  t Vdbe.pSubProgr
21031 61 6d 2e 20 54 68 69 73 20 6c 69 73 74 20 69 73  am. This list is
21032 20 75 73 65 64 20 74 6f 20 64 65 6c 65 74 65 20   used to delete 
21033 61 6c 6c 20 73 75 62 2d 70 72 6f 67 72 61 6d 0a  all sub-program.
21034 2a 2a 20 6f 62 6a 65 63 74 73 20 77 68 65 6e 20  ** objects when 
21035 74 68 65 20 56 4d 20 69 73 20 6e 6f 20 6c 6f 6e  the VM is no lon
21036 67 65 72 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f  ger required..*/
21037 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21038 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
21039 4c 69 6e 6b 53 75 62 50 72 6f 67 72 61 6d 28 56  LinkSubProgram(V
2103a 64 62 65 20 2a 70 56 64 62 65 2c 20 53 75 62 50  dbe *pVdbe, SubP
2103b 72 6f 67 72 61 6d 20 2a 70 29 7b 0a 20 20 70 2d  rogram *p){.  p-
2103c 3e 70 4e 65 78 74 20 3d 20 70 56 64 62 65 2d 3e  >pNext = pVdbe->
2103d 70 50 72 6f 67 72 61 6d 3b 0a 20 20 70 56 64 62  pProgram;.  pVdb
2103e 65 2d 3e 70 50 72 6f 67 72 61 6d 20 3d 20 70 3b  e->pProgram = p;
2103f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
21040 20 74 68 65 20 6f 70 63 6f 64 65 20 61 74 20 61   the opcode at a
21041 64 64 72 20 69 6e 74 6f 20 4f 50 5f 4e 6f 6f 70  ddr into OP_Noop
21042 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
21043 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
21044 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28  dbeChangeToNoop(
21045 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64  Vdbe *p, int add
21046 72 29 7b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70  r){.  if( p->aOp
21047 20 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20 2a   ){.    VdbeOp *
21048 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64  pOp = &p->aOp[ad
21049 64 72 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  dr];.    sqlite3
2104a 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20   *db = p->db;.  
2104b 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70    freeP4(db, pOp
2104c 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70  ->p4type, pOp->p
2104d 34 2e 70 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74  4.p);.    memset
2104e 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28  (pOp, 0, sizeof(
2104f 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20 70 4f  pOp[0]));.    pO
21050 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e  p->opcode = OP_N
21051 6f 6f 70 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  oop;.  }.}../*.*
21052 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c  * Change the val
21053 75 65 20 6f 66 20 74 68 65 20 50 34 20 6f 70 65  ue of the P4 ope
21054 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69  rand for a speci
21055 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  fic instruction.
21056 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
21057 20 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20   is useful when 
21058 61 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20  a large program 
21059 69 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61  is loaded from a
2105a 0a 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 79  .** static array
2105b 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64   using sqlite3Vd
2105c 62 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20  beAddOpList but 
2105d 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20  we want to make 
2105e 61 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63  a.** few minor c
2105f 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72  hanges to the pr
21060 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ogram..**.** If 
21061 6e 3e 3d 30 20 74 68 65 6e 20 74 68 65 20 50 34  n>=0 then the P4
21062 20 6f 70 65 72 61 6e 64 20 69 73 20 64 79 6e 61   operand is dyna
21063 6d 69 63 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61  mic, meaning tha
21064 74 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74  t a copy of.** t
21065 68 65 20 73 74 72 69 6e 67 20 69 73 20 6d 61 64  he string is mad
21066 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  e into memory ob
21067 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
21068 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a  te3_malloc()..**
21069 20 41 20 76 61 6c 75 65 20 6f 66 20 6e 3d 3d 30   A value of n==0
2106a 20 6d 65 61 6e 73 20 63 6f 70 79 20 62 79 74 65   means copy byte
2106b 73 20 6f 66 20 7a 50 34 20 75 70 20 74 6f 20 61  s of zP4 up to a
2106c 6e 64 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65  nd including the
2106d 0a 2a 2a 20 66 69 72 73 74 20 6e 75 6c 6c 20 62  .** first null b
2106e 79 74 65 2e 20 20 49 66 20 6e 3e 30 20 74 68 65  yte.  If n>0 the
2106f 6e 20 63 6f 70 79 20 6e 2b 31 20 62 79 74 65 73  n copy n+1 bytes
21070 20 6f 66 20 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 49   of zP4..**.** I
21071 66 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20  f n==P4_KEYINFO 
21072 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 50  it means that zP
21073 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
21074 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  o a KeyInfo stru
21075 63 74 75 72 65 2e 0a 2a 2a 20 41 20 63 6f 70 79  cture..** A copy
21076 20 69 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20   is made of the 
21077 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
21078 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  e into memory ob
21079 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73  tained from.** s
2107a 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 20 74  qlite3_malloc, t
2107b 6f 20 62 65 20 66 72 65 65 64 20 77 68 65 6e 20  o be freed when 
2107c 74 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61  the Vdbe is fina
2107d 6c 69 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f  lized..** n==P4_
2107e 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20  KEYINFO_HANDOFF 
2107f 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 7a  indicates that z
21080 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b  P4 points to a K
21081 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
21082 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d 65  .** stored in me
21083 6d 6f 72 79 20 74 68 61 74 20 74 68 65 20 63 61  mory that the ca
21084 6c 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65  ller has obtaine
21085 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
21086 61 6c 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20 63  alloc. The .** c
21087 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74  aller should not
21088 20 66 72 65 65 20 74 68 65 20 61 6c 6c 6f 63 61   free the alloca
21089 74 69 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65  tion, it will be
2108a 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20   freed when the 
2108b 56 64 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61 6c  Vdbe is.** final
2108c 69 7a 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68  ized..** .** Oth
2108d 65 72 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 28  er values of n (
2108e 50 34 5f 53 54 41 54 49 43 2c 20 50 34 5f 43 4f  P4_STATIC, P4_CO
2108f 4c 4c 53 45 51 20 65 74 63 2e 29 20 69 6e 64 69  LLSEQ etc.) indi
21090 63 61 74 65 20 74 68 61 74 20 7a 50 34 20 70 6f  cate that zP4 po
21091 69 6e 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74 72  ints.** to a str
21092 69 6e 67 20 6f 72 20 73 74 72 75 63 74 75 72 65  ing or structure
21093 20 74 68 61 74 20 69 73 20 67 75 61 72 61 6e 74   that is guarant
21094 65 65 64 20 74 6f 20 65 78 69 73 74 20 66 6f 72  eed to exist for
21095 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66   the lifetime of
21096 0a 2a 2a 20 74 68 65 20 56 64 62 65 2e 20 49 6e  .** the Vdbe. In
21097 20 74 68 65 73 65 20 63 61 73 65 73 20 77 65 20   these cases we 
21098 63 61 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 68  can just copy th
21099 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  e pointer..**.**
2109a 20 49 66 20 61 64 64 72 3c 30 20 74 68 65 6e 20   If addr<0 then 
2109b 63 68 61 6e 67 65 20 50 34 20 6f 6e 20 74 68 65  change P4 on the
2109c 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69   most recently i
2109d 6e 73 65 72 74 65 64 20 69 6e 73 74 72 75 63 74  nserted instruct
2109e 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
2109f 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
210a0 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
210a1 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64  Vdbe *p, int add
210a2 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  r, const char *z
210a3 50 34 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f 70  P4, int n){.  Op
210a4 20 2a 70 4f 70 3b 0a 20 20 73 71 6c 69 74 65 33   *pOp;.  sqlite3
210a5 20 2a 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20   *db;.  assert( 
210a6 70 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70  p!=0 );.  db = p
210a7 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20  ->db;.  assert( 
210a8 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
210a9 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69  AGIC_INIT );.  i
210aa 66 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20  f( p->aOp==0 || 
210ab 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
210ac 20 29 7b 0a 20 20 20 20 69 66 20 28 20 6e 21 3d   ){.    if ( n!=
210ad 50 34 5f 4b 45 59 49 4e 46 4f 20 26 26 20 6e 21  P4_KEYINFO && n!
210ae 3d 50 34 5f 56 54 41 42 20 29 20 7b 0a 20 20 20  =P4_VTAB ) {.   
210af 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 6e 2c     freeP4(db, n,
210b0 20 28 76 6f 69 64 2a 29 2a 28 63 68 61 72 2a 2a   (void*)*(char**
210b1 29 26 7a 50 34 29 3b 0a 20 20 20 20 7d 0a 20 20  )&zP4);.    }.  
210b2 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
210b3 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30  assert( p->nOp>0
210b4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 64   );.  assert( ad
210b5 64 72 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69  dr<p->nOp );.  i
210b6 66 28 20 61 64 64 72 3c 30 20 29 7b 0a 20 20 20  f( addr<0 ){.   
210b7 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 20 2d   addr = p->nOp -
210b8 20 31 3b 0a 20 20 7d 0a 20 20 70 4f 70 20 3d 20   1;.  }.  pOp = 
210b9 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20  &p->aOp[addr];. 
210ba 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
210bb 74 79 70 65 3d 3d 50 34 5f 4e 4f 54 55 53 45 44  type==P4_NOTUSED
210bc 20 7c 7c 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d   || pOp->p4type=
210bd 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 66  =P4_INT32 );.  f
210be 72 65 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70  reeP4(db, pOp->p
210bf 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70  4type, pOp->p4.p
210c0 29 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d  );.  pOp->p4.p =
210c1 20 30 3b 0a 20 20 69 66 28 20 6e 3d 3d 50 34 5f   0;.  if( n==P4_
210c2 49 4e 54 33 32 20 29 7b 0a 20 20 20 20 2f 2a 20  INT32 ){.    /* 
210c3 4e 6f 74 65 3a 20 74 68 69 73 20 63 61 73 74 20  Note: this cast 
210c4 69 73 20 73 61 66 65 2c 20 62 65 63 61 75 73 65  is safe, because
210c5 20 74 68 65 20 6f 72 69 67 69 6e 20 64 61 74 61   the origin data
210c6 20 70 6f 69 6e 74 20 77 61 73 20 61 6e 20 69 6e   point was an in
210c7 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 61  t.    ** that wa
210c8 73 20 63 61 73 74 20 74 6f 20 61 20 28 63 6f 6e  s cast to a (con
210c9 73 74 20 63 68 61 72 20 2a 29 2e 20 2a 2f 0a 20  st char *). */. 
210ca 20 20 20 70 4f 70 2d 3e 70 34 2e 69 20 3d 20 53     pOp->p4.i = S
210cb 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54  QLITE_PTR_TO_INT
210cc 28 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e  (zP4);.    pOp->
210cd 70 34 74 79 70 65 20 3d 20 50 34 5f 49 4e 54 33  p4type = P4_INT3
210ce 32 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a  2;.  }else if( z
210cf 50 34 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70  P4==0 ){.    pOp
210d0 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20  ->p4.p = 0;.    
210d1 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
210d2 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 7d 65 6c 73  _NOTUSED;.  }els
210d3 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49  e if( n==P4_KEYI
210d4 4e 46 4f 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e  NFO ){.    KeyIn
210d5 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  fo *pKeyInfo;.  
210d6 20 20 69 6e 74 20 6e 46 69 65 6c 64 2c 20 6e 42    int nField, nB
210d7 79 74 65 3b 0a 0a 20 20 20 20 6e 46 69 65 6c 64  yte;..    nField
210d8 20 3d 20 28 28 4b 65 79 49 6e 66 6f 2a 29 7a 50   = ((KeyInfo*)zP
210d9 34 29 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20  4)->nField;.    
210da 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a  nByte = sizeof(*
210db 70 4b 65 79 49 6e 66 6f 29 20 2b 20 28 6e 46 69  pKeyInfo) + (nFi
210dc 65 6c 64 2d 31 29 2a 73 69 7a 65 6f 66 28 70 4b  eld-1)*sizeof(pK
210dd 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d  eyInfo->aColl[0]
210de 29 20 2b 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20  ) + nField;.    
210df 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74  pKeyInfo = sqlit
210e0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 30 2c  e3DbMallocRaw(0,
210e1 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 70 4f 70   nByte);.    pOp
210e2 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 3d 20  ->p4.pKeyInfo = 
210e3 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 66  pKeyInfo;.    if
210e4 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20  ( pKeyInfo ){.  
210e5 20 20 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 64      u8 *aSortOrd
210e6 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  er;.      memcpy
210e7 28 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f  ((char*)pKeyInfo
210e8 2c 20 7a 50 34 2c 20 6e 42 79 74 65 20 2d 20 6e  , zP4, nByte - n
210e9 46 69 65 6c 64 29 3b 0a 20 20 20 20 20 20 61 53  Field);.      aS
210ea 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79 49  ortOrder = pKeyI
210eb 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 3b  nfo->aSortOrder;
210ec 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61  .      assert( a
210ed 53 6f 72 74 4f 72 64 65 72 21 3d 30 20 29 3b 0a  SortOrder!=0 );.
210ee 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e        pKeyInfo->
210ef 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 6e  aSortOrder = (un
210f0 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 70 4b  signed char*)&pK
210f1 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46  eyInfo->aColl[nF
210f2 69 65 6c 64 5d 3b 0a 20 20 20 20 20 20 6d 65 6d  ield];.      mem
210f3 63 70 79 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53  cpy(pKeyInfo->aS
210f4 6f 72 74 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f  ortOrder, aSortO
210f5 72 64 65 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20  rder, nField);. 
210f6 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65       pOp->p4type
210f7 20 3d 20 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20   = P4_KEYINFO;. 
210f8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
210f9 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  p->db->mallocFai
210fa 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 70  led = 1;.      p
210fb 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f  Op->p4type = P4_
210fc 4e 4f 54 55 53 45 44 3b 0a 20 20 20 20 7d 0a 20  NOTUSED;.    }. 
210fd 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34   }else if( n==P4
210fe 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
210ff 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e   ){.    pOp->p4.
21100 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a  p = (void*)zP4;.
21101 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20      pOp->p4type 
21102 3d 20 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20  = P4_KEYINFO;.  
21103 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f  }else if( n==P4_
21104 56 54 41 42 20 29 7b 0a 20 20 20 20 70 4f 70 2d  VTAB ){.    pOp-
21105 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a  >p4.p = (void*)z
21106 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74  P4;.    pOp->p4t
21107 79 70 65 20 3d 20 50 34 5f 56 54 41 42 3b 0a 20  ype = P4_VTAB;. 
21108 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f     sqlite3VtabLo
21109 63 6b 28 28 56 54 61 62 6c 65 20 2a 29 7a 50 34  ck((VTable *)zP4
2110a 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
2110b 28 56 54 61 62 6c 65 20 2a 29 7a 50 34 29 2d 3e  (VTable *)zP4)->
2110c 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 7d  db==p->db );.  }
2110d 65 6c 73 65 20 69 66 28 20 6e 3c 30 20 29 7b 0a  else if( n<0 ){.
2110e 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20      pOp->p4.p = 
2110f 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20  (void*)zP4;.    
21110 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 28 73  pOp->p4type = (s
21111 69 67 6e 65 64 20 63 68 61 72 29 6e 3b 0a 20 20  igned char)n;.  
21112 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 6e  }else{.    if( n
21113 3d 3d 30 20 29 20 6e 20 3d 20 73 71 6c 69 74 65  ==0 ) n = sqlite
21114 33 53 74 72 6c 65 6e 33 30 28 7a 50 34 29 3b 0a  3Strlen30(zP4);.
21115 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20      pOp->p4.z = 
21116 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
21117 28 70 2d 3e 64 62 2c 20 7a 50 34 2c 20 6e 29 3b  (p->db, zP4, n);
21118 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65  .    pOp->p4type
21119 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20   = P4_DYNAMIC;. 
2111a 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44   }.}..#ifndef ND
2111b 45 42 55 47 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  EBUG./*.** Chang
2111c 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e  e the comment on
2111d 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
2111e 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63  ly coded instruc
2111f 74 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73  tion.  Or.** ins
21120 65 72 74 20 61 20 4e 6f 2d 6f 70 20 61 6e 64 20  ert a No-op and 
21121 61 64 64 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20  add the comment 
21122 74 6f 20 74 68 61 74 20 6e 65 77 20 69 6e 73 74  to that new inst
21123 72 75 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a  ruction.  This.*
21124 2a 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65  * makes the code
21125 20 65 61 73 69 65 72 20 74 6f 20 72 65 61 64 20   easier to read 
21126 64 75 72 69 6e 67 20 64 65 62 75 67 67 69 6e 67  during debugging
21127 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 69 73 20  .  None of this 
21128 68 61 70 70 65 6e 73 0a 2a 2a 20 69 6e 20 61 20  happens.** in a 
21129 70 72 6f 64 75 63 74 69 6f 6e 20 62 75 69 6c 64  production build
2112a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2112b 20 76 64 62 65 56 43 6f 6d 6d 65 6e 74 28 56 64   vdbeVComment(Vd
2112c 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61  be *p, const cha
2112d 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c  r *zFormat, va_l
2112e 69 73 74 20 61 70 29 7b 0a 20 20 61 73 73 65 72  ist ap){.  asser
2112f 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70  t( p->nOp>0 || p
21130 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73  ->aOp==0 );.  as
21131 73 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20  sert( p->aOp==0 
21132 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70  || p->aOp[p->nOp
21133 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20  -1].zComment==0 
21134 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  || p->db->malloc
21135 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20  Failed );.  if( 
21136 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 61 73  p->nOp ){.    as
21137 73 65 72 74 28 20 70 2d 3e 61 4f 70 20 29 3b 0a  sert( p->aOp );.
21138 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
21139 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4f 70 5b  e(p->db, p->aOp[
2113a 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65  p->nOp-1].zComme
2113b 6e 74 29 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b  nt);.    p->aOp[
2113c 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65  p->nOp-1].zComme
2113d 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72  nt = sqlite3VMPr
2113e 69 6e 74 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72  intf(p->db, zFor
2113f 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7d 0a 7d 0a  mat, ap);.  }.}.
21140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
21141 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
21142 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20  omment(Vdbe *p, 
21143 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
21144 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  mat, ...){.  va_
21145 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 70  list ap;.  if( p
21146 20 29 7b 0a 20 20 20 20 76 61 5f 73 74 61 72 74   ){.    va_start
21147 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20  (ap, zFormat);. 
21148 20 20 20 76 64 62 65 56 43 6f 6d 6d 65 6e 74 28     vdbeVComment(
21149 70 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  p, zFormat, ap);
2114a 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b  .    va_end(ap);
2114b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  .  }.}.SQLITE_PR
2114c 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2114d 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e  e3VdbeNoopCommen
2114e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74  t(Vdbe *p, const
2114f 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
21150 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
21151 61 70 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  ap;.  if( p ){. 
21152 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21153 64 4f 70 30 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29  dOp0(p, OP_Noop)
21154 3b 0a 20 20 20 20 76 61 5f 73 74 61 72 74 28 61  ;.    va_start(a
21155 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20  p, zFormat);.   
21156 20 76 64 62 65 56 43 6f 6d 6d 65 6e 74 28 70 2c   vdbeVComment(p,
21157 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20   zFormat, ap);. 
21158 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20     va_end(ap);. 
21159 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20   }.}.#endif  /* 
2115a 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  NDEBUG */../*.**
2115b 20 52 65 74 75 72 6e 20 74 68 65 20 6f 70 63 6f   Return the opco
2115c 64 65 20 66 6f 72 20 61 20 67 69 76 65 6e 20 61  de for a given a
2115d 64 64 72 65 73 73 2e 20 20 49 66 20 74 68 65 20  ddress.  If the 
2115e 61 64 64 72 65 73 73 20 69 73 20 2d 31 2c 20 74  address is -1, t
2115f 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68  hen.** return th
21160 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
21161 69 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 65 2e  inserted opcode.
21162 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f  .**.** If a memo
21163 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ry allocation er
21164 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64  ror has occurred
21165 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 61   prior to the ca
21166 6c 6c 69 6e 67 20 6f 66 20 74 68 69 73 0a 2a 2a  lling of this.**
21167 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 6e 20 61   routine, then a
21168 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 64 75   pointer to a du
21169 6d 6d 79 20 56 64 62 65 4f 70 20 77 69 6c 6c 20  mmy VdbeOp will 
2116a 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68  be returned.  Th
2116b 61 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20  at opcode.** is 
2116c 72 65 61 64 61 62 6c 65 20 62 75 74 20 6e 6f 74  readable but not
2116d 20 77 72 69 74 61 62 6c 65 2c 20 74 68 6f 75 67   writable, thoug
2116e 68 20 69 74 20 69 73 20 63 61 73 74 20 74 6f 20  h it is cast to 
2116f 61 20 77 72 69 74 61 62 6c 65 20 76 61 6c 75 65  a writable value
21170 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  ..** The return 
21171 6f 66 20 61 20 64 75 6d 6d 79 20 6f 70 63 6f 64  of a dummy opcod
21172 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 63 61 6c  e allows the cal
21173 6c 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 66 75  l to continue fu
21174 6e 63 74 69 6f 6e 69 6e 67 0a 2a 2a 20 61 66 74  nctioning.** aft
21175 65 72 20 61 20 4f 4f 4d 20 66 61 75 6c 74 20 77  er a OOM fault w
21176 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f  ithout having to
21177 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   check to see if
21178 20 74 68 65 20 72 65 74 75 72 6e 20 66 72 6f 6d   the return from
21179 20 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e   .** this routin
2117a 65 20 69 73 20 61 20 76 61 6c 69 64 20 70 6f 69  e is a valid poi
2117b 6e 74 65 72 2e 20 20 42 75 74 20 62 65 63 61 75  nter.  But becau
2117c 73 65 20 74 68 65 20 64 75 6d 6d 79 2e 6f 70 63  se the dummy.opc
2117d 6f 64 65 20 69 73 20 30 2c 0a 2a 2a 20 64 75 6d  ode is 0,.** dum
2117e 6d 79 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  my will never be
2117f 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 20 54 68   written to.  Th
21180 69 73 20 69 73 20 76 65 72 69 66 69 65 64 20 62  is is verified b
21181 79 20 63 6f 64 65 20 69 6e 73 70 65 63 74 69 6f  y code inspectio
21182 6e 20 61 6e 64 0a 2a 2a 20 62 79 20 72 75 6e 6e  n and.** by runn
21183 69 6e 67 20 77 69 74 68 20 56 61 6c 67 72 69 6e  ing with Valgrin
21184 64 2e 0a 2a 2a 0a 2a 2a 20 41 62 6f 75 74 20 74  d..**.** About t
21185 68 65 20 23 69 66 64 65 66 20 53 51 4c 49 54 45  he #ifdef SQLITE
21186 5f 4f 4d 49 54 5f 54 52 41 43 45 3a 20 20 4e 6f  _OMIT_TRACE:  No
21187 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20 72 6f 75  rmally, this rou
21188 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61  tine is never ca
21189 6c 6c 65 64 0a 2a 2a 20 75 6e 6c 65 73 73 20 70  lled.** unless p
2118a 2d 3e 6e 4f 70 3e 30 2e 20 20 54 68 69 73 20 69  ->nOp>0.  This i
2118b 73 20 62 65 63 61 75 73 65 20 69 6e 20 74 68 65  s because in the
2118c 20 61 62 73 65 6e 73 65 20 6f 66 20 53 51 4c 49   absense of SQLI
2118d 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 2c 0a 2a  TE_OMIT_TRACE,.*
2118e 2a 20 61 6e 20 4f 50 5f 54 72 61 63 65 20 69 6e  * an OP_Trace in
2118f 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c 77  struction is alw
21190 61 79 73 20 69 6e 73 65 72 74 65 64 20 62 79 20  ays inserted by 
21191 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 28 29  sqlite3VdbeGet()
21192 20 61 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 61   as soon as.** a
21193 20 6e 65 77 20 56 44 42 45 20 69 73 20 63 72 65   new VDBE is cre
21194 61 74 65 64 2e 20 20 53 6f 20 77 65 20 61 72 65  ated.  So we are
21195 20 66 72 65 65 20 74 6f 20 73 65 74 20 61 64 64   free to set add
21196 72 20 74 6f 20 70 2d 3e 6e 4f 70 2d 31 20 77 69  r to p->nOp-1 wi
21197 74 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20  thout.** having 
21198 74 6f 20 64 6f 75 62 6c 65 2d 63 68 65 63 6b 20  to double-check 
21199 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
2119a 74 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  t the result is 
2119b 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 42 75  non-negative. Bu
2119c 74 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f  t.** if SQLITE_O
2119d 4d 49 54 5f 54 52 41 43 45 20 69 73 20 64 65 66  MIT_TRACE is def
2119e 69 6e 65 64 2c 20 74 68 65 20 4f 50 5f 54 72 61  ined, the OP_Tra
2119f 63 65 20 69 73 20 6f 6d 69 74 74 65 64 20 61 6e  ce is omitted an
211a0 64 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 0a  d we do need to.
211a1 2a 2a 20 63 68 65 63 6b 20 74 68 65 20 76 61 6c  ** check the val
211a2 75 65 20 6f 66 20 70 2d 3e 6e 4f 70 2d 31 20 62  ue of p->nOp-1 b
211a3 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67  efore continuing
211a4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
211a5 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69  ATE VdbeOp *sqli
211a6 74 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62  te3VdbeGetOp(Vdb
211a7 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b  e *p, int addr){
211a8 0a 20 20 2f 2a 20 43 38 39 20 73 70 65 63 69 66  .  /* C89 specif
211a9 69 65 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e  ies that the con
211aa 73 74 61 6e 74 20 22 64 75 6d 6d 79 22 20 77 69  stant "dummy" wi
211ab 6c 6c 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  ll be initialize
211ac 64 20 74 6f 20 61 6c 6c 0a 20 20 2a 2a 20 7a 65  d to all.  ** ze
211ad 72 6f 73 2c 20 77 68 69 63 68 20 69 73 20 63 6f  ros, which is co
211ae 72 72 65 63 74 2e 20 20 4d 53 56 43 20 67 65 6e  rrect.  MSVC gen
211af 65 72 61 74 65 73 20 61 20 77 61 72 6e 69 6e 67  erates a warning
211b0 2c 20 6e 65 76 65 72 74 68 65 6c 65 73 73 2e 20  , nevertheless. 
211b1 2a 2f 0a 20 20 73 74 61 74 69 63 20 56 64 62 65  */.  static Vdbe
211b2 4f 70 20 64 75 6d 6d 79 3b 20 20 2f 2a 20 49 67  Op dummy;  /* Ig
211b3 6e 6f 72 65 20 74 68 65 20 4d 53 56 43 20 77 61  nore the MSVC wa
211b4 72 6e 69 6e 67 20 61 62 6f 75 74 20 6e 6f 20 69  rning about no i
211b5 6e 69 74 69 61 6c 69 7a 65 72 20 2a 2f 0a 20 20  nitializer */.  
211b6 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
211b7 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
211b8 54 20 29 3b 0a 20 20 69 66 28 20 61 64 64 72 3c  T );.  if( addr<
211b9 30 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  0 ){.#ifdef SQLI
211ba 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20  TE_OMIT_TRACE.  
211bb 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3d 3d 30 20    if( p->nOp==0 
211bc 29 20 72 65 74 75 72 6e 20 28 56 64 62 65 4f 70  ) return (VdbeOp
211bd 2a 29 26 64 75 6d 6d 79 3b 0a 23 65 6e 64 69 66  *)&dummy;.#endif
211be 0a 20 20 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e  .    addr = p->n
211bf 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 61 73  Op - 1;.  }.  as
211c0 73 65 72 74 28 20 28 61 64 64 72 3e 3d 30 20 26  sert( (addr>=0 &
211c1 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c  & addr<p->nOp) |
211c2 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  | p->db->mallocF
211c3 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70  ailed );.  if( p
211c4 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
211c5 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
211c6 20 28 56 64 62 65 4f 70 2a 29 26 64 75 6d 6d 79   (VdbeOp*)&dummy
211c7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
211c8 65 74 75 72 6e 20 26 70 2d 3e 61 4f 70 5b 61 64  eturn &p->aOp[ad
211c9 64 72 5d 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20  dr];.  }.}..#if 
211ca 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
211cb 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c  OMIT_EXPLAIN) ||
211cc 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
211cd 29 20 5c 0a 20 20 20 20 20 7c 7c 20 64 65 66 69  ) \.     || defi
211ce 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45  ned(VDBE_PROFILE
211cf 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
211d0 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a  ITE_DEBUG)./*.**
211d1 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 69 6e   Compute a strin
211d2 67 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  g that describes
211d3 20 74 68 65 20 50 34 20 70 61 72 61 6d 65 74 65   the P4 paramete
211d4 72 20 66 6f 72 20 61 6e 20 6f 70 63 6f 64 65 2e  r for an opcode.
211d5 0a 2a 2a 20 55 73 65 20 7a 54 65 6d 70 20 66 6f  .** Use zTemp fo
211d6 72 20 61 6e 79 20 72 65 71 75 69 72 65 64 20 74  r any required t
211d7 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72 20  emporary buffer 
211d8 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  space..*/.static
211d9 20 63 68 61 72 20 2a 64 69 73 70 6c 61 79 50 34   char *displayP4
211da 28 4f 70 20 2a 70 4f 70 2c 20 63 68 61 72 20 2a  (Op *pOp, char *
211db 7a 54 65 6d 70 2c 20 69 6e 74 20 6e 54 65 6d 70  zTemp, int nTemp
211dc 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34 20 3d  ){.  char *zP4 =
211dd 20 7a 54 65 6d 70 3b 0a 20 20 61 73 73 65 72 74   zTemp;.  assert
211de 28 20 6e 54 65 6d 70 3e 3d 32 30 20 29 3b 0a 20  ( nTemp>=20 );. 
211df 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 70 34   switch( pOp->p4
211e0 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65  type ){.    case
211e1 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54   P4_KEYINFO_STAT
211e2 49 43 3a 0a 20 20 20 20 63 61 73 65 20 50 34 5f  IC:.    case P4_
211e3 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 20 20 20 20  KEYINFO: {.     
211e4 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20 20   int i, j;.     
211e5 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
211e6 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65  fo = pOp->p4.pKe
211e7 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 61 73 73  yInfo;.      ass
211e8 65 72 74 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  ert( pKeyInfo->a
211e9 53 6f 72 74 4f 72 64 65 72 21 3d 30 20 29 3b 0a  SortOrder!=0 );.
211ea 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
211eb 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54  printf(nTemp, zT
211ec 65 6d 70 2c 20 22 6b 65 79 69 6e 66 6f 28 25 64  emp, "keyinfo(%d
211ed 22 2c 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69  ", pKeyInfo->nFi
211ee 65 6c 64 29 3b 0a 20 20 20 20 20 20 69 20 3d 20  eld);.      i = 
211ef 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
211f0 7a 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 66 6f  zTemp);.      fo
211f1 72 28 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49 6e 66  r(j=0; j<pKeyInf
211f2 6f 2d 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b  o->nField; j++){
211f3 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  .        CollSeq
211f4 20 2a 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e   *pColl = pKeyIn
211f5 66 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20  fo->aColl[j];.  
211f6 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20        if( pColl 
211f7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
211f8 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
211f9 65 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  en30(pColl->zNam
211fa 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  e);.          if
211fb 28 20 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b  ( i+n>nTemp-6 ){
211fc 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d  .            mem
211fd 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c  cpy(&zTemp[i],",
211fe 2e 2e 2e 22 2c 34 29 3b 0a 20 20 20 20 20 20 20  ...",4);.       
211ff 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21200 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
21201 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27    zTemp[i++] = '
21202 2c 27 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ,';.          if
21203 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72  ( pKeyInfo->aSor
21204 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a 20 20 20  tOrder[j] ){.   
21205 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 69           zTemp[i
21206 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20  ++] = '-';.     
21207 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
21208 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b 69   memcpy(&zTemp[i
21209 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c  ], pColl->zName,
2120a 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  n+1);.          
2120b 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20  i += n;.        
2120c 7d 65 6c 73 65 20 69 66 28 20 69 2b 34 3c 6e 54  }else if( i+4<nT
2120d 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 20  emp-6 ){.       
2120e 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70     memcpy(&zTemp
2120f 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b 0a 20  [i],",nil",4);. 
21210 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 34 3b           i += 4;
21211 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
21212 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69   }.      zTemp[i
21213 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 20 20 20  ++] = ')';.     
21214 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 3b 0a 20   zTemp[i] = 0;. 
21215 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e       assert( i<n
21216 54 65 6d 70 20 29 3b 0a 20 20 20 20 20 20 62 72  Temp );.      br
21217 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
21218 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45 51 3a 20  ase P4_COLLSEQ: 
21219 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20  {.      CollSeq 
2121a 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d 3e 70 34  *pColl = pOp->p4
2121b 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 73 71  .pColl;.      sq
2121c 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
2121d 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 63 6f  Temp, zTemp, "co
2121e 6c 6c 73 65 71 28 25 2e 32 30 73 29 22 2c 20 70  llseq(%.20s)", p
2121f 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Coll->zName);.  
21220 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21221 0a 20 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e  .    case P4_FUN
21222 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 46 75  CDEF: {.      Fu
21223 6e 63 44 65 66 20 2a 70 44 65 66 20 3d 20 70 4f  ncDef *pDef = pO
21224 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 20  p->p4.pFunc;.   
21225 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
21226 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70  ntf(nTemp, zTemp
21227 2c 20 22 25 73 28 25 64 29 22 2c 20 70 44 65 66  , "%s(%d)", pDef
21228 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e 6e  ->zName, pDef->n
21229 41 72 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61  Arg);.      brea
2122a 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
2122b 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b 0a 20 20  e P4_INT64: {.  
2122c 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
2122d 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
2122e 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70 4f 70 2d  p, "%lld", *pOp-
2122f 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20 20 20 20  >p4.pI64);.     
21230 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
21231 20 20 63 61 73 65 20 50 34 5f 49 4e 54 33 32 3a    case P4_INT32:
21232 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
21233 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c  _snprintf(nTemp,
21234 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c 20 70 4f   zTemp, "%d", pO
21235 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20 20 20 20  p->p4.i);.      
21236 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
21237 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 20 7b   case P4_REAL: {
21238 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
21239 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a  nprintf(nTemp, z
2123a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 2a  Temp, "%.16g", *
2123b 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 3b 0a  pOp->p4.pReal);.
2123c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2123d 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 4d   }.    case P4_M
2123e 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20  EM: {.      Mem 
2123f 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e 70 34 2e  *pMem = pOp->p4.
21240 70 4d 65 6d 3b 0a 20 20 20 20 20 20 69 66 28 20  pMem;.      if( 
21241 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
21242 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20  M_Str ){.       
21243 20 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a   zP4 = pMem->z;.
21244 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
21245 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
21246 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 20  M_Int ){.       
21247 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
21248 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20  f(nTemp, zTemp, 
21249 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e  "%lld", pMem->u.
2124a 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  i);.      }else 
2124b 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  if( pMem->flags 
2124c 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20  & MEM_Real ){.  
2124d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
2124e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54  printf(nTemp, zT
2124f 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d  emp, "%.16g", pM
21250 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d 65  em->r);.      }e
21251 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c  lse if( pMem->fl
21252 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29  ags & MEM_Null )
21253 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
21254 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
21255 2c 20 7a 54 65 6d 70 2c 20 22 4e 55 4c 4c 22 29  , zTemp, "NULL")
21256 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
21257 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
21258 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
21259 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 20  _Blob );.       
2125a 20 7a 50 34 20 3d 20 22 28 62 6c 6f 62 29 22 3b   zP4 = "(blob)";
2125b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
2125c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  reak;.    }.#ifn
2125d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2125e 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
2125f 20 63 61 73 65 20 50 34 5f 56 54 41 42 3a 20 7b   case P4_VTAB: {
21260 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76  .      sqlite3_v
21261 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70  tab *pVtab = pOp
21262 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61  ->p4.pVtab->pVta
21263 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  b;.      sqlite3
21264 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c  _snprintf(nTemp,
21265 20 7a 54 65 6d 70 2c 20 22 76 74 61 62 3a 25 70   zTemp, "vtab:%p
21266 3a 25 70 22 2c 20 70 56 74 61 62 2c 20 70 56 74  :%p", pVtab, pVt
21267 61 62 2d 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20  ab->pModule);.  
21268 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21269 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65  .#endif.    case
2126a 20 50 34 5f 49 4e 54 41 52 52 41 59 3a 20 7b 0a   P4_INTARRAY: {.
2126b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
2126c 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54  printf(nTemp, zT
2126d 65 6d 70 2c 20 22 69 6e 74 61 72 72 61 79 22 29  emp, "intarray")
2126e 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2126f 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34     }.    case P4
21270 5f 53 55 42 50 52 4f 47 52 41 4d 3a 20 7b 0a 20  _SUBPROGRAM: {. 
21271 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
21272 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65  rintf(nTemp, zTe
21273 6d 70 2c 20 22 70 72 6f 67 72 61 6d 22 29 3b 0a  mp, "program");.
21274 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21275 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 41   }.    case P4_A
21276 44 56 41 4e 43 45 3a 20 7b 0a 20 20 20 20 20 20  DVANCE: {.      
21277 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 20  zTemp[0] = 0;.  
21278 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21279 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
2127a 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4f 70 2d        zP4 = pOp-
2127b 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 69 66 28  >p4.z;.      if(
2127c 20 7a 50 34 3d 3d 30 20 29 7b 0a 20 20 20 20 20   zP4==0 ){.     
2127d 20 20 20 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a     zP4 = zTemp;.
2127e 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 30 5d          zTemp[0]
2127f 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
21280 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74    }.  }.  assert
21281 28 20 7a 50 34 21 3d 30 20 29 3b 0a 20 20 72 65  ( zP4!=0 );.  re
21282 74 75 72 6e 20 7a 50 34 3b 0a 7d 0a 23 65 6e 64  turn zP4;.}.#end
21283 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61 72  if../*.** Declar
21284 65 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68  e to the Vdbe th
21285 61 74 20 74 68 65 20 42 54 72 65 65 20 6f 62 6a  at the BTree obj
21286 65 63 74 20 61 74 20 64 62 2d 3e 61 44 62 5b 69  ect at db->aDb[i
21287 5d 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a  ] is used..**.**
21288 20 54 68 65 20 70 72 65 70 61 72 65 64 20 73 74   The prepared st
21289 61 74 65 6d 65 6e 74 73 20 6e 65 65 64 20 74 6f  atements need to
2128a 20 6b 6e 6f 77 20 69 6e 20 61 64 76 61 6e 63 65   know in advance
2128b 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 73 65   the complete se
2128c 74 20 6f 66 0a 2a 2a 20 61 74 74 61 63 68 65 64  t of.** attached
2128d 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20   databases that 
2128e 77 69 6c 6c 20 62 65 20 75 73 65 2e 20 20 41 20  will be use.  A 
2128f 6d 61 73 6b 20 6f 66 20 74 68 65 73 65 20 64 61  mask of these da
21290 74 61 62 61 73 65 73 0a 2a 2a 20 69 73 20 6d 61  tabases.** is ma
21291 69 6e 74 61 69 6e 65 64 20 69 6e 20 70 2d 3e 62  intained in p->b
21292 74 72 65 65 4d 61 73 6b 2e 20 20 54 68 65 20 70  treeMask.  The p
21293 2d 3e 6c 6f 63 6b 4d 61 73 6b 20 76 61 6c 75 65  ->lockMask value
21294 20 69 73 20 74 68 65 20 73 75 62 73 65 74 20 6f   is the subset o
21295 66 0a 2a 2a 20 70 2d 3e 62 74 72 65 65 4d 61 73  f.** p->btreeMas
21296 6b 20 6f 66 20 64 61 74 61 62 61 73 65 73 20 74  k of databases t
21297 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69 72 65  hat will require
21298 20 61 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49   a lock..*/.SQLI
21299 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2129a 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
2129b 74 72 65 65 28 56 64 62 65 20 2a 70 2c 20 69 6e  tree(Vdbe *p, in
2129c 74 20 69 29 7b 0a 20 20 61 73 73 65 72 74 28 20  t i){.  assert( 
2129d 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 64 62 2d  i>=0 && i<p->db-
2129e 3e 6e 44 62 20 26 26 20 69 3c 28 69 6e 74 29 73  >nDb && i<(int)s
2129f 69 7a 65 6f 66 28 79 44 62 4d 61 73 6b 29 2a 38  izeof(yDbMask)*8
212a0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3c   );.  assert( i<
212a1 28 69 6e 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62  (int)sizeof(p->b
212a2 74 72 65 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20  treeMask)*8 );. 
212a3 20 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 7c 3d   p->btreeMask |=
212a4 20 28 28 79 44 62 4d 61 73 6b 29 31 29 3c 3c 69   ((yDbMask)1)<<i
212a5 3b 0a 20 20 69 66 28 20 69 21 3d 31 20 26 26 20  ;.  if( i!=1 && 
212a6 73 71 6c 69 74 65 33 42 74 72 65 65 53 68 61 72  sqlite3BtreeShar
212a7 61 62 6c 65 28 70 2d 3e 64 62 2d 3e 61 44 62 5b  able(p->db->aDb[
212a8 69 5d 2e 70 42 74 29 20 29 7b 0a 20 20 20 20 70  i].pBt) ){.    p
212a9 2d 3e 6c 6f 63 6b 4d 61 73 6b 20 7c 3d 20 28 28  ->lockMask |= ((
212aa 79 44 62 4d 61 73 6b 29 31 29 3c 3c 69 3b 0a 20  yDbMask)1)<<i;. 
212ab 20 7d 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e   }.}..#if !defin
212ac 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
212ad 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20  HARED_CACHE) && 
212ae 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
212af 45 3e 30 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c  E>0./*.** If SQL
212b0 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20  ite is compiled 
212b1 74 6f 20 73 75 70 70 6f 72 74 20 73 68 61 72 65  to support share
212b2 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 61 6e 64  d-cache mode and
212b3 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 61 66   to be threadsaf
212b4 65 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  e,.** this routi
212b5 6e 65 20 6f 62 74 61 69 6e 73 20 74 68 65 20 6d  ne obtains the m
212b6 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20  utex associated 
212b7 77 69 74 68 20 65 61 63 68 20 42 74 53 68 61 72  with each BtShar
212b8 65 64 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  ed structure.** 
212b9 74 68 61 74 20 6d 61 79 20 62 65 20 61 63 63 65  that may be acce
212ba 73 73 65 64 20 62 79 20 74 68 65 20 56 4d 20 70  ssed by the VM p
212bb 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75  assed as an argu
212bc 6d 65 6e 74 2e 20 49 6e 20 64 6f 69 6e 67 20 73  ment. In doing s
212bd 6f 20 69 74 20 61 6c 73 6f 0a 2a 2a 20 73 65 74  o it also.** set
212be 73 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 64  s the BtShared.d
212bf 62 20 6d 65 6d 62 65 72 20 6f 66 20 65 61 63 68  b member of each
212c0 20 6f 66 20 74 68 65 20 42 74 53 68 61 72 65 64   of the BtShared
212c1 20 73 74 72 75 63 74 75 72 65 73 2c 20 65 6e 73   structures, ens
212c2 75 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74 68  uring.** that th
212c3 65 20 63 6f 72 72 65 63 74 20 62 75 73 79 2d 68  e correct busy-h
212c4 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20  andler callback 
212c5 69 73 20 69 6e 76 6f 6b 65 64 20 69 66 20 72 65  is invoked if re
212c6 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  quired..**.** If
212c7 20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 74   SQLite is not t
212c8 68 72 65 61 64 73 61 66 65 20 62 75 74 20 64 6f  hreadsafe but do
212c9 65 73 20 73 75 70 70 6f 72 74 20 73 68 61 72 65  es support share
212ca 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 68  d-cache mode, th
212cb 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72  en.** sqlite3Btr
212cc 65 65 45 6e 74 65 72 28 29 20 69 73 20 69 6e 76  eeEnter() is inv
212cd 6f 6b 65 64 20 74 6f 20 73 65 74 20 74 68 65 20  oked to set the 
212ce 42 74 53 68 61 72 65 64 2e 64 62 20 76 61 72 69  BtShared.db vari
212cf 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 61 6c 6c 20  ables.** of all 
212d0 6f 66 20 42 74 53 68 61 72 65 64 20 73 74 72 75  of BtShared stru
212d1 63 74 75 72 65 73 20 61 63 63 65 73 73 69 62 6c  ctures accessibl
212d2 65 20 76 69 61 20 74 68 65 20 64 61 74 61 62 61  e via the databa
212d3 73 65 20 68 61 6e 64 6c 65 20 0a 2a 2a 20 61 73  se handle .** as
212d4 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
212d5 65 20 56 4d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53  e VM..**.** If S
212d6 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 74 68 72  QLite is not thr
212d7 65 61 64 73 61 66 65 20 61 6e 64 20 64 6f 65 73  eadsafe and does
212d8 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 68 61   not support sha
212d9 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20  red-cache mode, 
212da 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  this.** function
212db 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
212dc 2a 2a 20 54 68 65 20 70 2d 3e 62 74 72 65 65 4d  ** The p->btreeM
212dd 61 73 6b 20 66 69 65 6c 64 20 69 73 20 61 20 62  ask field is a b
212de 69 74 6d 61 73 6b 20 6f 66 20 61 6c 6c 20 62 74  itmask of all bt
212df 72 65 65 73 20 74 68 61 74 20 74 68 65 20 70 72  rees that the pr
212e0 65 70 61 72 65 64 20 0a 2a 2a 20 73 74 61 74 65  epared .** state
212e1 6d 65 6e 74 20 70 20 77 69 6c 6c 20 65 76 65 72  ment p will ever
212e2 20 75 73 65 2e 20 20 4c 65 74 20 4e 20 62 65 20   use.  Let N be 
212e3 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 69  the number of bi
212e4 74 73 20 69 6e 20 70 2d 3e 62 74 72 65 65 4d 61  ts in p->btreeMa
212e5 73 6b 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64  sk.** correspond
212e6 69 6e 67 20 74 6f 20 62 74 72 65 65 73 20 74 68  ing to btrees th
212e7 61 74 20 75 73 65 20 73 68 61 72 65 64 20 63 61  at use shared ca
212e8 63 68 65 2e 20 20 54 68 65 6e 20 74 68 65 20 72  che.  Then the r
212e9 75 6e 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 69  untime of.** thi
212ea 73 20 72 6f 75 74 69 6e 65 20 69 73 20 4e 2a 4e  s routine is N*N
212eb 2e 20 20 42 75 74 20 61 73 20 4e 20 69 73 20 72  .  But as N is r
212ec 61 72 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20  arely more than 
212ed 31 2c 20 74 68 69 73 20 73 68 6f 75 6c 64 20 6e  1, this should n
212ee 6f 74 0a 2a 2a 20 62 65 20 61 20 70 72 6f 62 6c  ot.** be a probl
212ef 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  em..*/.SQLITE_PR
212f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
212f1 65 33 56 64 62 65 45 6e 74 65 72 28 56 64 62 65  e3VdbeEnter(Vdbe
212f2 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20   *p){.  int i;. 
212f3 20 79 44 62 4d 61 73 6b 20 6d 61 73 6b 3b 0a 20   yDbMask mask;. 
212f4 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
212f5 44 62 20 2a 61 44 62 3b 0a 20 20 69 6e 74 20 6e  Db *aDb;.  int n
212f6 44 62 3b 0a 20 20 69 66 28 20 70 2d 3e 6c 6f 63  Db;.  if( p->loc
212f7 6b 4d 61 73 6b 3d 3d 30 20 29 20 72 65 74 75 72  kMask==0 ) retur
212f8 6e 3b 20 20 2f 2a 20 54 68 65 20 63 6f 6d 6d 6f  n;  /* The commo
212f9 6e 20 63 61 73 65 20 2a 2f 0a 20 20 64 62 20 3d  n case */.  db =
212fa 20 70 2d 3e 64 62 3b 0a 20 20 61 44 62 20 3d 20   p->db;.  aDb = 
212fb 64 62 2d 3e 61 44 62 3b 0a 20 20 6e 44 62 20 3d  db->aDb;.  nDb =
212fc 20 64 62 2d 3e 6e 44 62 3b 0a 20 20 66 6f 72 28   db->nDb;.  for(
212fd 69 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 3c 6e  i=0, mask=1; i<n
212fe 44 62 3b 20 69 2b 2b 2c 20 6d 61 73 6b 20 2b 3d  Db; i++, mask +=
212ff 20 6d 61 73 6b 29 7b 0a 20 20 20 20 69 66 28 20   mask){.    if( 
21300 69 21 3d 31 20 26 26 20 28 6d 61 73 6b 20 26 20  i!=1 && (mask & 
21301 70 2d 3e 6c 6f 63 6b 4d 61 73 6b 29 21 3d 30 20  p->lockMask)!=0 
21302 26 26 20 41 4c 57 41 59 53 28 61 44 62 5b 69 5d  && ALWAYS(aDb[i]
21303 2e 70 42 74 21 3d 30 29 20 29 7b 0a 20 20 20 20  .pBt!=0) ){.    
21304 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
21305 74 65 72 28 61 44 62 5b 69 5d 2e 70 42 74 29 3b  ter(aDb[i].pBt);
21306 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e  .    }.  }.}.#en
21307 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  dif..#if !define
21308 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  d(SQLITE_OMIT_SH
21309 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 53  ARED_CACHE) && S
2130a 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
2130b 3e 30 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20  >0./*.** Unlock 
2130c 61 6c 6c 20 6f 66 20 74 68 65 20 62 74 72 65 65  all of the btree
2130d 73 20 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63  s previously loc
2130e 6b 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  ked by a call to
2130f 20 73 71 6c 69 74 65 33 56 64 62 65 45 6e 74 65   sqlite3VdbeEnte
21310 72 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  r()..*/.SQLITE_P
21311 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
21312 74 65 33 56 64 62 65 4c 65 61 76 65 28 56 64 62  te3VdbeLeave(Vdb
21313 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  e *p){.  int i;.
21314 20 20 79 44 62 4d 61 73 6b 20 6d 61 73 6b 3b 0a    yDbMask mask;.
21315 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
21316 20 44 62 20 2a 61 44 62 3b 0a 20 20 69 6e 74 20   Db *aDb;.  int 
21317 6e 44 62 3b 0a 20 20 69 66 28 20 70 2d 3e 6c 6f  nDb;.  if( p->lo
21318 63 6b 4d 61 73 6b 3d 3d 30 20 29 20 72 65 74 75  ckMask==0 ) retu
21319 72 6e 3b 20 20 2f 2a 20 54 68 65 20 63 6f 6d 6d  rn;  /* The comm
2131a 6f 6e 20 63 61 73 65 20 2a 2f 0a 20 20 64 62 20  on case */.  db 
2131b 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 44 62 20 3d  = p->db;.  aDb =
2131c 20 64 62 2d 3e 61 44 62 3b 0a 20 20 6e 44 62 20   db->aDb;.  nDb 
2131d 3d 20 64 62 2d 3e 6e 44 62 3b 0a 20 20 66 6f 72  = db->nDb;.  for
2131e 28 69 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 3c  (i=0, mask=1; i<
2131f 6e 44 62 3b 20 69 2b 2b 2c 20 6d 61 73 6b 20 2b  nDb; i++, mask +
21320 3d 20 6d 61 73 6b 29 7b 0a 20 20 20 20 69 66 28  = mask){.    if(
21321 20 69 21 3d 31 20 26 26 20 28 6d 61 73 6b 20 26   i!=1 && (mask &
21322 20 70 2d 3e 6c 6f 63 6b 4d 61 73 6b 29 21 3d 30   p->lockMask)!=0
21323 20 26 26 20 41 4c 57 41 59 53 28 61 44 62 5b 69   && ALWAYS(aDb[i
21324 5d 2e 70 42 74 21 3d 30 29 20 29 7b 0a 20 20 20  ].pBt!=0) ){.   
21325 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
21326 65 61 76 65 28 61 44 62 5b 69 5d 2e 70 42 74 29  eave(aDb[i].pBt)
21327 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65  ;.    }.  }.}.#e
21328 6e 64 69 66 0a 0a 23 69 66 20 64 65 66 69 6e 65  ndif..#if define
21329 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20  d(VDBE_PROFILE) 
2132a 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
2132b 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 50  E_DEBUG)./*.** P
2132c 72 69 6e 74 20 61 20 73 69 6e 67 6c 65 20 6f 70  rint a single op
2132d 63 6f 64 65 2e 20 20 54 68 69 73 20 72 6f 75 74  code.  This rout
2132e 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20  ine is used for 
2132f 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a  debugging only..
21330 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21331 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
21332 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 20 2a  bePrintOp(FILE *
21333 70 4f 75 74 2c 20 69 6e 74 20 70 63 2c 20 4f 70  pOut, int pc, Op
21334 20 2a 70 4f 70 29 7b 0a 20 20 63 68 61 72 20 2a   *pOp){.  char *
21335 7a 50 34 3b 0a 20 20 63 68 61 72 20 7a 50 74 72  zP4;.  char zPtr
21336 5b 35 30 5d 3b 0a 20 20 73 74 61 74 69 63 20 63  [50];.  static c
21337 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
21338 61 74 31 20 3d 20 22 25 34 64 20 25 2d 31 33 73  at1 = "%4d %-13s
21339 20 25 34 64 20 25 34 64 20 25 34 64 20 25 2d 34   %4d %4d %4d %-4
2133a 73 20 25 2e 32 58 20 25 73 5c 6e 22 3b 0a 20 20  s %.2X %s\n";.  
2133b 69 66 28 20 70 4f 75 74 3d 3d 30 20 29 20 70 4f  if( pOut==0 ) pO
2133c 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7a  ut = stdout;.  z
2133d 50 34 20 3d 20 64 69 73 70 6c 61 79 50 34 28 70  P4 = displayP4(p
2133e 4f 70 2c 20 7a 50 74 72 2c 20 73 69 7a 65 6f 66  Op, zPtr, sizeof
2133f 28 7a 50 74 72 29 29 3b 0a 20 20 66 70 72 69 6e  (zPtr));.  fprin
21340 74 66 28 70 4f 75 74 2c 20 7a 46 6f 72 6d 61 74  tf(pOut, zFormat
21341 31 2c 20 70 63 2c 20 0a 20 20 20 20 20 20 73 71  1, pc, .      sq
21342 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28  lite3OpcodeName(
21343 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 2c 20 70 4f  pOp->opcode), pO
21344 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20  p->p1, pOp->p2, 
21345 70 4f 70 2d 3e 70 33 2c 20 7a 50 34 2c 20 70 4f  pOp->p3, zP4, pO
21346 70 2d 3e 70 35 2c 0a 23 69 66 64 65 66 20 53 51  p->p5,.#ifdef SQ
21347 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20  LITE_DEBUG.     
21348 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3f   pOp->zComment ?
21349 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3a   pOp->zComment :
2134a 20 22 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 20   "".#else.      
2134b 22 22 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a 20  "".#endif.  );. 
2134c 20 66 66 6c 75 73 68 28 70 4f 75 74 29 3b 0a 7d   fflush(pOut);.}
2134d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
2134e 65 6c 65 61 73 65 20 61 6e 20 61 72 72 61 79 20  elease an array 
2134f 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e 74  of N Mem element
21350 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  s.*/.static void
21351 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79   releaseMemArray
21352 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 20 4e 29 7b  (Mem *p, int N){
21353 0a 20 20 69 66 28 20 70 20 26 26 20 4e 20 29 7b  .  if( p && N ){
21354 0a 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 0a  .    Mem *pEnd;.
21355 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
21356 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 75 38 20  = p->db;.    u8 
21357 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 20 3d 20  malloc_failed = 
21358 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
21359 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 70 6e  ;.    if( db->pn
2135a 42 79 74 65 73 46 72 65 65 64 20 29 7b 0a 20 20  BytesFreed ){.  
2135b 20 20 20 20 66 6f 72 28 70 45 6e 64 3d 26 70 5b      for(pEnd=&p[
2135c 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 70 2b 2b 29  N]; p<pEnd; p++)
2135d 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2135e 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
2135f 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 7d  Malloc);.      }
21360 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
21361 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 45 6e     }.    for(pEn
21362 64 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64 3b  d=&p[N]; p<pEnd;
21363 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73   p++){.      ass
21364 65 72 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70 45  ert( (&p[1])==pE
21365 6e 64 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d 70  nd || p[0].db==p
21366 5b 31 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20 20  [1].db );..     
21367 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 69   /* This block i
21368 73 20 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c 69  s really an inli
21369 6e 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 73  ned version of s
2136a 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
2136b 65 61 73 65 28 29 0a 20 20 20 20 20 20 2a 2a 20  ease().      ** 
2136c 74 68 61 74 20 74 61 6b 65 73 20 61 64 76 61 6e  that takes advan
2136d 74 61 67 65 20 6f 66 20 74 68 65 20 66 61 63 74  tage of the fact
2136e 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79   that the memory
2136f 20 63 65 6c 6c 20 76 61 6c 75 65 20 69 73 20 0a   cell value is .
21370 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 73        ** being s
21371 65 74 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65 72  et to NULL after
21372 20 72 65 6c 65 61 73 69 6e 67 20 61 6e 79 20 64   releasing any d
21373 79 6e 61 6d 69 63 20 72 65 73 6f 75 72 63 65 73  ynamic resources
21374 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
21375 20 2a 2a 20 54 68 65 20 6a 75 73 74 69 66 69 63   ** The justific
21376 61 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 63  ation for duplic
21377 61 74 69 6e 67 20 63 6f 64 65 20 69 73 20 74 68  ating code is th
21378 61 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  at according to 
21379 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67 72  .      ** callgr
2137a 69 6e 64 2c 20 74 68 69 73 20 63 61 75 73 65 73  ind, this causes
2137b 20 61 20 63 65 72 74 61 69 6e 20 74 65 73 74 20   a certain test 
2137c 63 61 73 65 20 74 6f 20 68 69 74 20 74 68 65 20  case to hit the 
2137d 43 50 55 20 34 2e 37 20 0a 20 20 20 20 20 20 2a  CPU 4.7 .      *
2137e 2a 20 70 65 72 63 65 6e 74 20 6c 65 73 73 20 28  * percent less (
2137f 78 38 36 20 6c 69 6e 75 78 2c 20 67 63 63 20 76  x86 linux, gcc v
21380 65 72 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d 4f  ersion 4.1.2, -O
21381 36 29 20 74 68 61 6e 20 69 66 20 0a 20 20 20 20  6) than if .    
21382 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 52    ** sqlite3MemR
21383 65 6c 65 61 73 65 28 29 20 77 65 72 65 20 63 61  elease() were ca
21384 6c 6c 65 64 20 66 72 6f 6d 20 68 65 72 65 2e 20  lled from here. 
21385 57 69 74 68 20 2d 4f 32 2c 20 74 68 69 73 20 6a  With -O2, this j
21386 75 6d 70 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f  umps.      ** to
21387 20 36 2e 36 20 70 65 72 63 65 6e 74 2e 20 54 68   6.6 percent. Th
21388 65 20 74 65 73 74 20 63 61 73 65 20 69 73 20 69  e test case is i
21389 6e 73 65 72 74 69 6e 67 20 31 30 30 30 20 72 6f  nserting 1000 ro
2138a 77 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20  ws into a table 
2138b 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 6e  .      ** with n
2138c 6f 20 69 6e 64 65 78 65 73 20 75 73 69 6e 67 20  o indexes using 
2138d 61 20 73 69 6e 67 6c 65 20 70 72 65 70 61 72 65  a single prepare
2138e 64 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65  d INSERT stateme
2138f 6e 74 2c 20 62 69 6e 64 28 29 20 0a 20 20 20 20  nt, bind() .    
21390 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 28 29    ** and reset()
21391 2e 20 49 6e 73 65 72 74 73 20 61 72 65 20 67 72  . Inserts are gr
21392 6f 75 70 65 64 20 69 6e 74 6f 20 61 20 74 72 61  ouped into a tra
21393 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20  nsaction..      
21394 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  */.      if( p->
21395 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d  flags&(MEM_Agg|M
21396 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 46 72 61 6d 65  EM_Dyn|MEM_Frame
21397 7c 4d 45 4d 5f 52 6f 77 53 65 74 29 20 29 7b 0a  |MEM_RowSet) ){.
21398 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
21399 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 29  dbeMemRelease(p)
2139a 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
2139b 28 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a  ( p->zMalloc ){.
2139c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2139d 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 4d 61  bFree(db, p->zMa
2139e 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 70  lloc);.        p
2139f 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20  ->zMalloc = 0;. 
213a0 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 70 2d       }..      p-
213a1 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 76  >flags = MEM_Inv
213a2 61 6c 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20  alid;.    }.    
213a3 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
213a4 20 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64   = malloc_failed
213a5 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
213a6 65 6c 65 74 65 20 61 20 56 64 62 65 46 72 61 6d  elete a VdbeFram
213a7 65 20 6f 62 6a 65 63 74 20 61 6e 64 20 69 74 73  e object and its
213a8 20 63 6f 6e 74 65 6e 74 73 2e 20 56 64 62 65 46   contents. VdbeF
213a9 72 61 6d 65 20 6f 62 6a 65 63 74 73 20 61 72 65  rame objects are
213aa 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  .** allocated by
213ab 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20   the OP_Program 
213ac 6f 70 63 6f 64 65 20 69 6e 20 73 71 6c 69 74 65  opcode in sqlite
213ad 33 56 64 62 65 45 78 65 63 28 29 2e 0a 2a 2f 0a  3VdbeExec()..*/.
213ae 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
213af 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46  oid sqlite3VdbeF
213b0 72 61 6d 65 44 65 6c 65 74 65 28 56 64 62 65 46  rameDelete(VdbeF
213b1 72 61 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  rame *p){.  int 
213b2 69 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 20 3d  i;.  Mem *aMem =
213b3 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 29   VdbeFrameMem(p)
213b4 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ;.  VdbeCursor *
213b5 2a 61 70 43 73 72 20 3d 20 28 56 64 62 65 43 75  *apCsr = (VdbeCu
213b6 72 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d  rsor **)&aMem[p-
213b7 3e 6e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20 66  >nChildMem];.  f
213b8 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 68  or(i=0; i<p->nCh
213b9 69 6c 64 43 73 72 3b 20 69 2b 2b 29 7b 0a 20 20  ildCsr; i++){.  
213ba 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65    sqlite3VdbeFre
213bb 65 43 75 72 73 6f 72 28 70 2d 3e 76 2c 20 61 70  eCursor(p->v, ap
213bc 43 73 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72  Csr[i]);.  }.  r
213bd 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 61  eleaseMemArray(a
213be 4d 65 6d 2c 20 70 2d 3e 6e 43 68 69 6c 64 4d 65  Mem, p->nChildMe
213bf 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  m);.  sqlite3DbF
213c0 72 65 65 28 70 2d 3e 76 2d 3e 64 62 2c 20 70 29  ree(p->v->db, p)
213c1 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
213c2 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
213c3 0a 2f 2a 0a 2a 2a 20 47 69 76 65 20 61 20 6c 69  ./*.** Give a li
213c4 73 74 69 6e 67 20 6f 66 20 74 68 65 20 70 72 6f  sting of the pro
213c5 67 72 61 6d 20 69 6e 20 74 68 65 20 76 69 72 74  gram in the virt
213c6 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a  ual machine..**.
213c7 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65  ** The interface
213c8 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
213c9 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28  sqlite3VdbeExec(
213ca 29 2e 20 20 42 75 74 20 69 6e 73 74 65 61 64 20  ).  But instead 
213cb 6f 66 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 74 68  of.** running th
213cc 65 20 63 6f 64 65 2c 20 69 74 20 69 6e 76 6f 6b  e code, it invok
213cd 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  es the callback 
213ce 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69 6e  once for each in
213cf 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68  struction..** Th
213d0 69 73 20 66 65 61 74 75 72 65 20 69 73 20 75 73  is feature is us
213d1 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
213d2 22 45 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a  "EXPLAIN"..**.**
213d3 20 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69 6e   When p->explain
213d4 3d 3d 31 2c 20 65 61 63 68 20 69 6e 73 74 72 75  ==1, each instru
213d5 63 74 69 6f 6e 20 69 73 20 6c 69 73 74 65 64 2e  ction is listed.
213d6 20 20 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65 78 70    When.** p->exp
213d7 6c 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 4f 50  lain==2, only OP
213d8 5f 45 78 70 6c 61 69 6e 20 69 6e 73 74 72 75 63  _Explain instruc
213d9 74 69 6f 6e 73 20 61 72 65 20 6c 69 73 74 65 64  tions are listed
213da 20 61 6e 64 20 74 68 65 73 65 0a 2a 2a 20 61 72   and these.** ar
213db 65 20 73 68 6f 77 6e 20 69 6e 20 61 20 64 69 66  e shown in a dif
213dc 66 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e 20 20  ferent format.  
213dd 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 69 73  p->explain==2 is
213de 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
213df 6e 74 0a 2a 2a 20 45 58 50 4c 41 49 4e 20 51 55  nt.** EXPLAIN QU
213e0 45 52 59 20 50 4c 41 4e 2e 0a 2a 2a 0a 2a 2a 20  ERY PLAN..**.** 
213e1 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69 6e 3d  When p->explain=
213e2 3d 31 2c 20 66 69 72 73 74 20 74 68 65 20 6d 61  =1, first the ma
213e3 69 6e 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 69  in program is li
213e4 73 74 65 64 2c 20 74 68 65 6e 20 65 61 63 68 20  sted, then each 
213e5 6f 66 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65  of.** the trigge
213e6 72 20 73 75 62 70 72 6f 67 72 61 6d 73 20 61 72  r subprograms ar
213e7 65 20 6c 69 73 74 65 64 20 6f 6e 65 20 62 79 20  e listed one by 
213e8 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  one..*/.SQLITE_P
213e9 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
213ea 65 33 56 64 62 65 4c 69 73 74 28 0a 20 20 56 64  e3VdbeList(.  Vd
213eb 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 20  be *p           
213ec 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
213ed 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  DBE */.){.  int 
213ee 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20  nRow;           
213ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
213f0 20 2f 2a 20 53 74 6f 70 20 77 68 65 6e 20 72 6f   /* Stop when ro
213f1 77 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20  w count reaches 
213f2 74 68 69 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53  this */.  int nS
213f3 75 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ub = 0;         
213f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
213f5 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 75 62 2d  * Number of sub-
213f6 76 64 62 65 73 20 73 65 65 6e 20 73 6f 20 66 61  vdbes seen so fa
213f7 72 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61  r */.  SubProgra
213f8 6d 20 2a 2a 61 70 53 75 62 20 3d 20 30 3b 20 20  m **apSub = 0;  
213f9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
213fa 72 72 61 79 20 6f 66 20 73 75 62 2d 76 64 62 65  rray of sub-vdbe
213fb 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 53 75 62  s */.  Mem *pSub
213fc 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
213fd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
213fe 65 6d 6f 72 79 20 63 65 6c 6c 20 68 6f 6c 64 20  emory cell hold 
213ff 61 72 72 61 79 20 6f 66 20 73 75 62 70 72 6f 67  array of subprog
21400 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  s */.  sqlite3 *
21401 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20  db = p->db;     
21402 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
21403 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
21404 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ection */.  int 
21405 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
21406 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21407 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
21408 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   */.  int rc = S
21409 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
2140a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
2140b 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 4d  turn code */.  M
2140c 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61  em *pMem = &p->a
2140d 4d 65 6d 5b 31 5d 3b 20 20 20 20 20 20 20 20 20  Mem[1];         
2140e 20 20 20 20 2f 2a 20 46 69 72 73 74 20 4d 65 6d      /* First Mem
2140f 20 6f 66 20 72 65 73 75 6c 74 20 73 65 74 20 2a   of result set *
21410 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  /..  assert( p->
21411 65 78 70 6c 61 69 6e 20 29 3b 0a 20 20 61 73 73  explain );.  ass
21412 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  ert( p->magic==V
21413 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 3b  DBE_MAGIC_RUN );
21414 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63  .  assert( p->rc
21415 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
21416 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  ->rc==SQLITE_BUS
21417 59 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49  Y || p->rc==SQLI
21418 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f  TE_NOMEM );..  /
21419 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68  * Even though th
2141a 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e  is opcode does n
2141b 6f 74 20 75 73 65 20 64 79 6e 61 6d 69 63 20 73  ot use dynamic s
2141c 74 72 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20  trings for.  ** 
2141d 74 68 65 20 72 65 73 75 6c 74 2c 20 72 65 73 75  the result, resu
2141e 6c 74 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62  lt columns may b
2141f 65 63 6f 6d 65 20 64 79 6e 61 6d 69 63 20 69 66  ecome dynamic if
21420 20 74 68 65 20 75 73 65 72 20 63 61 6c 6c 73 0a   the user calls.
21421 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c    ** sqlite3_col
21422 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 20 63 61  umn_text16(), ca
21423 75 73 69 6e 67 20 61 20 74 72 61 6e 73 6c 61 74  using a translat
21424 69 6f 6e 20 74 6f 20 55 54 46 2d 31 36 20 65 6e  ion to UTF-16 en
21425 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72  coding..  */.  r
21426 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70  eleaseMemArray(p
21427 4d 65 6d 2c 20 38 29 3b 0a 20 20 70 2d 3e 70 52  Mem, 8);.  p->pR
21428 65 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a 0a 20  esultSet = 0;.. 
21429 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49   if( p->rc==SQLI
2142a 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  TE_NOMEM ){.    
2142b 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  /* This happens 
2142c 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e  if a malloc() in
2142d 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73  side a call to s
2142e 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
2142f 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73  xt() or.    ** s
21430 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
21431 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20  xt16() failed.  
21432 2a 2f 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  */.    db->mallo
21433 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
21434 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
21435 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  RROR;.  }..  /* 
21436 57 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  When the number 
21437 6f 66 20 6f 75 74 70 75 74 20 72 6f 77 73 20 72  of output rows r
21438 65 61 63 68 65 73 20 6e 52 6f 77 2c 20 74 68 61  eaches nRow, tha
21439 74 20 6d 65 61 6e 73 20 74 68 65 0a 20 20 2a 2a  t means the.  **
2143a 20 6c 69 73 74 69 6e 67 20 68 61 73 20 66 69 6e   listing has fin
2143b 69 73 68 65 64 20 61 6e 64 20 73 71 6c 69 74 65  ished and sqlite
2143c 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20  3_step() should 
2143d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
2143e 4e 45 2e 0a 20 20 2a 2a 20 6e 52 6f 77 20 69 73  NE..  ** nRow is
2143f 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20   the sum of the 
21440 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69  number of rows i
21441 6e 20 74 68 65 20 6d 61 69 6e 20 70 72 6f 67 72  n the main progr
21442 61 6d 2c 20 70 6c 75 73 0a 20 20 2a 2a 20 74 68  am, plus.  ** th
21443 65 20 73 75 6d 20 6f 66 20 74 68 65 20 6e 75 6d  e sum of the num
21444 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 61  ber of rows in a
21445 6c 6c 20 74 72 69 67 67 65 72 20 73 75 62 70 72  ll trigger subpr
21446 6f 67 72 61 6d 73 20 65 6e 63 6f 75 6e 74 65 72  ograms encounter
21447 65 64 0a 20 20 2a 2a 20 73 6f 20 66 61 72 2e 20  ed.  ** so far. 
21448 20 54 68 65 20 6e 52 6f 77 20 76 61 6c 75 65 20   The nRow value 
21449 77 69 6c 6c 20 69 6e 63 72 65 61 73 65 20 61 73  will increase as
2144a 20 6e 65 77 20 74 72 69 67 67 65 72 20 73 75 62   new trigger sub
2144b 70 72 6f 67 72 61 6d 73 20 61 72 65 0a 20 20 2a  programs are.  *
2144c 2a 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 62  * encountered, b
2144d 75 74 20 70 2d 3e 70 63 20 77 69 6c 6c 20 65 76  ut p->pc will ev
2144e 65 6e 74 75 61 6c 6c 79 20 63 61 74 63 68 20 75  entually catch u
2144f 70 20 74 6f 20 6e 52 6f 77 2e 0a 20 20 2a 2f 0a  p to nRow..  */.
21450 20 20 6e 52 6f 77 20 3d 20 70 2d 3e 6e 4f 70 3b    nRow = p->nOp;
21451 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69  .  if( p->explai
21452 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54  n==1 ){.    /* T
21453 68 65 20 66 69 72 73 74 20 38 20 6d 65 6d 6f 72  he first 8 memor
21454 79 20 63 65 6c 6c 73 20 61 72 65 20 75 73 65 64  y cells are used
21455 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20   for the result 
21456 73 65 74 2e 20 20 53 6f 20 77 65 20 77 69 6c 6c  set.  So we will
21457 0a 20 20 20 20 2a 2a 20 63 6f 6d 6d 61 6e 64 65  .    ** commande
21458 65 72 20 74 68 65 20 39 74 68 20 63 65 6c 6c 20  er the 9th cell 
21459 74 6f 20 75 73 65 20 61 73 20 73 74 6f 72 61 67  to use as storag
2145a 65 20 66 6f 72 20 61 6e 20 61 72 72 61 79 20 6f  e for an array o
2145b 66 20 70 6f 69 6e 74 65 72 73 0a 20 20 20 20 2a  f pointers.    *
2145c 2a 20 74 6f 20 74 72 69 67 67 65 72 20 73 75 62  * to trigger sub
2145d 70 72 6f 67 72 61 6d 73 2e 20 20 54 68 65 20 56  programs.  The V
2145e 44 42 45 20 69 73 20 67 75 61 72 61 6e 74 65 65  DBE is guarantee
2145f 64 20 74 6f 20 68 61 76 65 20 61 74 20 6c 65 61  d to have at lea
21460 73 74 20 39 0a 20 20 20 20 2a 2a 20 63 65 6c 6c  st 9.    ** cell
21461 73 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  s.  */.    asser
21462 74 28 20 70 2d 3e 6e 4d 65 6d 3e 39 20 29 3b 0a  t( p->nMem>9 );.
21463 20 20 20 20 70 53 75 62 20 3d 20 26 70 2d 3e 61      pSub = &p->a
21464 4d 65 6d 5b 39 5d 3b 0a 20 20 20 20 69 66 28 20  Mem[9];.    if( 
21465 70 53 75 62 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pSub->flags&MEM_
21466 42 6c 6f 62 20 29 7b 0a 20 20 20 20 20 20 2f 2a  Blob ){.      /*
21467 20 4f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61   On the first ca
21468 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 74  ll to sqlite3_st
21469 65 70 28 29 2c 20 70 53 75 62 20 77 69 6c 6c 20  ep(), pSub will 
2146a 68 6f 6c 64 20 61 20 4e 55 4c 4c 2e 20 20 49 74  hold a NULL.  It
2146b 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 6e 69   is.      ** ini
2146c 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 42 4c  tialized to a BL
2146d 4f 42 20 62 79 20 74 68 65 20 50 34 5f 53 55 42  OB by the P4_SUB
2146e 50 52 4f 47 52 41 4d 20 70 72 6f 63 65 73 73 69  PROGRAM processi
2146f 6e 67 20 6c 6f 67 69 63 20 62 65 6c 6f 77 20 2a  ng logic below *
21470 2f 0a 20 20 20 20 20 20 6e 53 75 62 20 3d 20 70  /.      nSub = p
21471 53 75 62 2d 3e 6e 2f 73 69 7a 65 6f 66 28 56 64  Sub->n/sizeof(Vd
21472 62 65 2a 29 3b 0a 20 20 20 20 20 20 61 70 53 75  be*);.      apSu
21473 62 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d 20  b = (SubProgram 
21474 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20  **)pSub->z;.    
21475 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  }.    for(i=0; i
21476 3c 6e 53 75 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nSub; i++){.   
21477 20 20 20 6e 52 6f 77 20 2b 3d 20 61 70 53 75 62     nRow += apSub
21478 5b 69 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a  [i]->nOp;.    }.
21479 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 69    }..  do{.    i
2147a 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77   = p->pc++;.  }w
2147b 68 69 6c 65 28 20 69 3c 6e 52 6f 77 20 26 26 20  hile( i<nRow && 
2147c 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26  p->explain==2 &&
2147d 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64   p->aOp[i].opcod
2147e 65 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b  e!=OP_Explain );
2147f 0a 20 20 69 66 28 20 69 3e 3d 6e 52 6f 77 20 29  .  if( i>=nRow )
21480 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51  {.    p->rc = SQ
21481 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20  LITE_OK;.    rc 
21482 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  = SQLITE_DONE;. 
21483 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 75   }else if( db->u
21484 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20  1.isInterrupted 
21485 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53  ){.    p->rc = S
21486 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b  QLITE_INTERRUPT;
21487 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
21488 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69  _ERROR;.    sqli
21489 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
2148a 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25  >zErrMsg, db, "%
2148b 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74  s", sqlite3ErrSt
2148c 72 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d 65 6c  r(p->rc));.  }el
2148d 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b  se{.    char *z;
2148e 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20  .    Op *pOp;.  
2148f 20 20 69 66 28 20 69 3c 70 2d 3e 6e 4f 70 20 29    if( i<p->nOp )
21490 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f  {.      /* The o
21491 75 74 70 75 74 20 6c 69 6e 65 20 6e 75 6d 62 65  utput line numbe
21492 72 20 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67  r is small enoug
21493 68 20 74 68 61 74 20 77 65 20 61 72 65 20 73 74  h that we are st
21494 69 6c 6c 20 69 6e 20 74 68 65 0a 20 20 20 20 20  ill in the.     
21495 20 2a 2a 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d   ** main program
21496 2e 20 2a 2f 0a 20 20 20 20 20 20 70 4f 70 20 3d  . */.      pOp =
21497 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20   &p->aOp[i];.   
21498 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
21499 20 57 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c   We are currentl
2149a 79 20 6c 69 73 74 69 6e 67 20 73 75 62 70 72 6f  y listing subpro
2149b 67 72 61 6d 73 2e 20 20 46 69 67 75 72 65 20 6f  grams.  Figure o
2149c 75 74 20 77 68 69 63 68 20 6f 6e 65 20 61 6e 64  ut which one and
2149d 0a 20 20 20 20 20 20 2a 2a 20 70 69 63 6b 20 75  .      ** pick u
2149e 70 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  p the appropriat
2149f 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a 20 20 20  e opcode. */.   
214a0 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
214a1 69 20 2d 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 20  i -= p->nOp;.   
214a2 20 20 20 66 6f 72 28 6a 3d 30 3b 20 69 3e 3d 61     for(j=0; i>=a
214a3 70 53 75 62 5b 6a 5d 2d 3e 6e 4f 70 3b 20 6a 2b  pSub[j]->nOp; j+
214a4 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 20 2d 3d  +){.        i -=
214a5 20 61 70 53 75 62 5b 6a 5d 2d 3e 6e 4f 70 3b 0a   apSub[j]->nOp;.
214a6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f        }.      pO
214a7 70 20 3d 20 26 61 70 53 75 62 5b 6a 5d 2d 3e 61  p = &apSub[j]->a
214a8 4f 70 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  Op[i];.    }.   
214a9 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d   if( p->explain=
214aa 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d  =1 ){.      pMem
214ab 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
214ac 74 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74  t;.      pMem->t
214ad 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ype = SQLITE_INT
214ae 45 47 45 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d  EGER;.      pMem
214af 2d 3e 75 2e 69 20 3d 20 69 3b 20 20 20 20 20 20  ->u.i = i;      
214b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
214b1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f            /* Pro
214b2 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  gram counter */.
214b3 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20        pMem++;.  
214b4 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
214b5 67 73 20 3d 20 4d 45 4d 5f 53 74 61 74 69 63 7c  gs = MEM_Static|
214b6 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d  MEM_Str|MEM_Term
214b7 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20  ;.      pMem->z 
214b8 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  = (char*)sqlite3
214b9 4f 70 63 6f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e  OpcodeName(pOp->
214ba 6f 70 63 6f 64 65 29 3b 20 20 2f 2a 20 4f 70 63  opcode);  /* Opc
214bb 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  ode */.      ass
214bc 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20  ert( pMem->z!=0 
214bd 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e  );.      pMem->n
214be 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
214bf 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20  30(pMem->z);.   
214c0 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
214c1 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20  SQLITE_TEXT;.   
214c2 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53     pMem->enc = S
214c3 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20  QLITE_UTF8;.    
214c4 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 20    pMem++;..     
214c5 20 2f 2a 20 57 68 65 6e 20 61 6e 20 4f 50 5f 50   /* When an OP_P
214c6 72 6f 67 72 61 6d 20 6f 70 63 6f 64 65 20 69 73  rogram opcode is
214c7 20 65 6e 63 6f 75 6e 74 65 72 20 28 74 68 65 20   encounter (the 
214c8 6f 6e 6c 79 20 6f 70 63 6f 64 65 20 74 68 61 74  only opcode that
214c9 20 68 61 73 0a 20 20 20 20 20 20 2a 2a 20 61 20   has.      ** a 
214ca 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 61 72  P4_SUBPROGRAM ar
214cb 67 75 6d 65 6e 74 29 2c 20 65 78 70 61 6e 64 20  gument), expand 
214cc 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
214cd 61 72 72 61 79 20 6f 66 20 73 75 62 70 72 6f 67  array of subprog
214ce 72 61 6d 73 0a 20 20 20 20 20 20 2a 2a 20 6b 65  rams.      ** ke
214cf 70 74 20 69 6e 20 70 2d 3e 61 4d 65 6d 5b 39 5d  pt in p->aMem[9]
214d0 2e 7a 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e  .z to hold the n
214d1 65 77 20 70 72 6f 67 72 61 6d 20 2d 20 61 73 73  ew program - ass
214d2 75 6d 69 6e 67 20 74 68 69 73 20 73 75 62 70 72  uming this subpr
214d3 6f 67 72 61 6d 0a 20 20 20 20 20 20 2a 2a 20 68  ogram.      ** h
214d4 61 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 62  as not already b
214d5 65 65 6e 20 73 65 65 6e 2e 0a 20 20 20 20 20 20  een seen..      
214d6 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  */.      if( pOp
214d7 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 55 42  ->p4type==P4_SUB
214d8 50 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 20 20  PROGRAM ){.     
214d9 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28     int nByte = (
214da 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53  nSub+1)*sizeof(S
214db 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20  ubProgram*);.   
214dc 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
214dd 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e      for(j=0; j<n
214de 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  Sub; j++){.     
214df 20 20 20 20 20 69 66 28 20 61 70 53 75 62 5b 6a       if( apSub[j
214e0 5d 3d 3d 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67  ]==pOp->p4.pProg
214e1 72 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ram ) break;.   
214e2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
214e3 66 28 20 6a 3d 3d 6e 53 75 62 20 26 26 20 53 51  f( j==nSub && SQ
214e4 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33  LITE_OK==sqlite3
214e5 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 53 75 62  VdbeMemGrow(pSub
214e6 2c 20 6e 42 79 74 65 2c 20 6e 53 75 62 21 3d 30  , nByte, nSub!=0
214e7 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  ) ){.          a
214e8 70 53 75 62 20 3d 20 28 53 75 62 50 72 6f 67 72  pSub = (SubProgr
214e9 61 6d 20 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20  am **)pSub->z;. 
214ea 20 20 20 20 20 20 20 20 20 61 70 53 75 62 5b 6e           apSub[n
214eb 53 75 62 2b 2b 5d 20 3d 20 70 4f 70 2d 3e 70 34  Sub++] = pOp->p4
214ec 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 20 20 20  .pProgram;.     
214ed 20 20 20 20 20 70 53 75 62 2d 3e 66 6c 61 67 73       pSub->flags
214ee 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20   |= MEM_Blob;.  
214ef 20 20 20 20 20 20 20 20 70 53 75 62 2d 3e 6e 20          pSub->n 
214f0 3d 20 6e 53 75 62 2a 73 69 7a 65 6f 66 28 53 75  = nSub*sizeof(Su
214f1 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20  bProgram*);.    
214f2 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
214f3 20 20 7d 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66    }..    pMem->f
214f4 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
214f5 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20      pMem->u.i = 
214f6 70 4f 70 2d 3e 70 31 3b 20 20 20 20 20 20 20 20  pOp->p1;        
214f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
214f8 20 20 2f 2a 20 50 31 20 2a 2f 0a 20 20 20 20 70    /* P1 */.    p
214f9 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
214fa 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  TE_INTEGER;.    
214fb 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65  pMem++;..    pMe
214fc 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  m->flags = MEM_I
214fd 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  nt;.    pMem->u.
214fe 69 20 3d 20 70 4f 70 2d 3e 70 32 3b 20 20 20 20  i = pOp->p2;    
214ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21500 20 20 20 20 20 20 2f 2a 20 50 32 20 2a 2f 0a 20        /* P2 */. 
21501 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
21502 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a  SQLITE_INTEGER;.
21503 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20      pMem++;..   
21504 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
21505 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d  EM_Int;.    pMem
21506 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 33 3b  ->u.i = pOp->p3;
21507 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21508 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 33 20            /* P3 
21509 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  */.    pMem->typ
2150a 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  e = SQLITE_INTEG
2150b 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a  ER;.    pMem++;.
2150c 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2150d 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d  VdbeMemGrow(pMem
2150e 2c 20 33 32 2c 20 30 29 20 29 7b 20 20 20 20 20  , 32, 0) ){     
2150f 20 20 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f 0a         /* P4 */.
21510 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
21511 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
21512 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  d );.      retur
21513 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
21514 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e      }.    pMem->
21515 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c  flags = MEM_Dyn|
21516 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d  MEM_Str|MEM_Term
21517 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73 70 6c 61  ;.    z = displa
21518 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e 7a  yP4(pOp, pMem->z
21519 2c 20 33 32 29 3b 0a 20 20 20 20 69 66 28 20 7a  , 32);.    if( z
2151a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20  !=pMem->z ){.   
2151b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
2151c 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a 2c  mSetStr(pMem, z,
2151d 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38   -1, SQLITE_UTF8
2151e 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , 0);.    }else{
2151f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
21520 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20  Mem->z!=0 );.   
21521 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c     pMem->n = sql
21522 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65  ite3Strlen30(pMe
21523 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65  m->z);.      pMe
21524 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  m->enc = SQLITE_
21525 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20  UTF8;.    }.    
21526 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
21527 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 4d  ITE_TEXT;.    pM
21528 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70  em++;..    if( p
21529 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a  ->explain==1 ){.
2152a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
2152b 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65  3VdbeMemGrow(pMe
2152c 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 20  m, 4, 0) ){.    
2152d 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64      assert( p->d
2152e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2152f 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
21530 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
21531 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4d        }.      pM
21532 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
21533 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  Dyn|MEM_Str|MEM_
21534 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d  Term;.      pMem
21535 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20 20 20 20 73  ->n = 2;.      s
21536 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
21537 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 2e 32  3, pMem->z, "%.2
21538 78 22 2c 20 70 4f 70 2d 3e 70 35 29 3b 20 20 20  x", pOp->p5);   
21539 2f 2a 20 50 35 20 2a 2f 0a 20 20 20 20 20 20 70  /* P5 */.      p
2153a 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
2153b 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70  TE_TEXT;.      p
2153c 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Mem->enc = SQLIT
2153d 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d  E_UTF8;.      pM
2153e 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66 64 65 66 20  em++;.  .#ifdef 
2153f 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
21540 20 20 20 69 66 28 20 70 4f 70 2d 3e 7a 43 6f 6d     if( pOp->zCom
21541 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20  ment ){.        
21542 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
21543 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Str|MEM_Term;.
21544 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20          pMem->z 
21545 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 3b  = pOp->zComment;
21546 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e  .        pMem->n
21547 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
21548 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20  30(pMem->z);.   
21549 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d       pMem->enc =
2154a 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20   SQLITE_UTF8;.  
2154b 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65        pMem->type
2154c 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a   = SQLITE_TEXT;.
2154d 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64        }else.#end
2154e 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20  if.      {.     
2154f 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
21550 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20 20   MEM_Null;      
21551 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21552 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a 20   /* Comment */. 
21553 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70         pMem->typ
21554 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b  e = SQLITE_NULL;
21555 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
21556 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d      p->nResColum
21557 6e 20 3d 20 38 20 2d 20 34 2a 28 70 2d 3e 65 78  n = 8 - 4*(p->ex
21558 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70 2d  plain-1);.    p-
21559 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26 70  >pResultSet = &p
2155a 2d 3e 61 4d 65 6d 5b 31 5d 3b 0a 20 20 20 20 70  ->aMem[1];.    p
2155b 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  ->rc = SQLITE_OK
2155c 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
2155d 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74  E_ROW;.  }.  ret
2155e 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
2155f 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
21560 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64  EXPLAIN */..#ifd
21561 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
21562 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20  /*.** Print the 
21563 53 51 4c 20 74 68 61 74 20 77 61 73 20 75 73 65  SQL that was use
21564 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20  d to generate a 
21565 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f  VDBE program..*/
21566 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21567 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
21568 50 72 69 6e 74 53 71 6c 28 56 64 62 65 20 2a 70  PrintSql(Vdbe *p
21569 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70  ){.  int nOp = p
2156a 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20  ->nOp;.  VdbeOp 
2156b 2a 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c  *pOp;.  if( nOp<
2156c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f  1 ) return;.  pO
2156d 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a  p = &p->aOp[0];.
2156e 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
2156f 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70  e==OP_Trace && p
21570 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20  Op->p4.z!=0 ){. 
21571 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
21572 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20   = pOp->p4.z;.  
21573 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
21574 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b  Isspace(*z) ) z+
21575 2b 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53  +;.    printf("S
21576 51 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b  QL: [%s]\n", z);
21577 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  .  }.}.#endif..#
21578 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
21579 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26  TE_OMIT_TRACE) &
2157a 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
2157b 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29  _ENABLE_IOTRACE)
2157c 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20  ./*.** Print an 
2157d 49 4f 54 52 41 43 45 20 6d 65 73 73 61 67 65 20  IOTRACE message 
2157e 73 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74  showing SQL cont
2157f 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
21580 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
21581 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71  te3VdbeIOTraceSq
21582 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e  l(Vdbe *p){.  in
21583 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a  t nOp = p->nOp;.
21584 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20    VdbeOp *pOp;. 
21585 20 69 66 28 20 73 71 6c 69 74 65 33 49 6f 54 72   if( sqlite3IoTr
21586 61 63 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ace==0 ) return;
21587 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72  .  if( nOp<1 ) r
21588 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26  eturn;.  pOp = &
21589 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28  p->aOp[0];.  if(
2158a 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
2158b 5f 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70  _Trace && pOp->p
2158c 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  4.z!=0 ){.    in
2158d 74 20 69 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72  t i, j;.    char
2158e 20 7a 5b 31 30 30 30 5d 3b 0a 20 20 20 20 73 71   z[1000];.    sq
2158f 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
21590 69 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73  izeof(z), z, "%s
21591 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20  ", pOp->p4.z);. 
21592 20 20 20 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69     for(i=0; sqli
21593 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29  te3Isspace(z[i])
21594 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72  ; i++){}.    for
21595 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29  (j=0; z[i]; i++)
21596 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
21597 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29  te3Isspace(z[i])
21598 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
21599 7a 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20  z[i-1]!=' ' ){. 
2159a 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20           z[j++] 
2159b 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 7d  = ' ';.        }
2159c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2159d 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a        z[j++] = z
2159e 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  [i];.      }.   
2159f 20 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b   }.    z[j] = 0;
215a0 0a 20 20 20 20 73 71 6c 69 74 65 33 49 6f 54 72  .    sqlite3IoTr
215a1 61 63 65 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20  ace("SQL %s\n", 
215a2 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  z);.  }.}.#endif
215a3 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54   /* !SQLITE_OMIT
215a4 5f 54 52 41 43 45 20 26 26 20 53 51 4c 49 54 45  _TRACE && SQLITE
215a5 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20  _ENABLE_IOTRACE 
215a6 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  */../*.** Alloca
215a7 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 61 20  te space from a 
215a8 66 69 78 65 64 20 73 69 7a 65 20 62 75 66 66 65  fixed size buffe
215a9 72 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70  r and return a p
215aa 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 61  ointer to.** tha
215ab 74 20 73 70 61 63 65 2e 20 20 49 66 20 69 6e 73  t space.  If ins
215ac 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20  ufficient space 
215ad 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 72 65  is available, re
215ae 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  turn NULL..**.**
215af 20 54 68 65 20 70 42 75 66 20 70 61 72 61 6d 65   The pBuf parame
215b0 74 65 72 20 69 73 20 74 68 65 20 69 6e 69 74 69  ter is the initi
215b1 61 6c 20 76 61 6c 75 65 20 6f 66 20 61 20 70 6f  al value of a po
215b2 69 6e 74 65 72 20 77 68 69 63 68 20 77 69 6c 6c  inter which will
215b3 0a 2a 2a 20 72 65 63 65 69 76 65 20 74 68 65 20  .** receive the 
215b4 6e 65 77 20 6d 65 6d 6f 72 79 2e 20 20 70 42 75  new memory.  pBu
215b5 66 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 4e 55  f is normally NU
215b6 4c 4c 2e 20 20 49 66 20 70 42 75 66 20 69 73 20  LL.  If pBuf is 
215b7 6e 6f 74 0a 2a 2a 20 4e 55 4c 4c 2c 20 69 74 20  not.** NULL, it 
215b8 6d 65 61 6e 73 20 74 68 61 74 20 6d 65 6d 6f 72  means that memor
215b9 79 20 73 70 61 63 65 20 68 61 73 20 61 6c 72 65  y space has alre
215ba 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  ady been allocat
215bb 65 64 20 61 6e 64 20 74 68 61 74 0a 2a 2a 20 74  ed and that.** t
215bc 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  his routine shou
215bd 6c 64 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 20  ld not allocate 
215be 61 6e 79 20 6e 65 77 20 6d 65 6d 6f 72 79 2e 20  any new memory. 
215bf 20 57 68 65 6e 20 70 42 75 66 20 69 73 20 6e 6f   When pBuf is no
215c0 74 0a 2a 2a 20 4e 55 4c 4c 20 73 69 6d 70 6c 79  t.** NULL simply
215c1 20 72 65 74 75 72 6e 20 70 42 75 66 2e 20 20 4f   return pBuf.  O
215c2 6e 6c 79 20 61 6c 6c 6f 63 61 74 65 20 6e 65 77  nly allocate new
215c3 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 77 68   memory space wh
215c4 65 6e 20 70 42 75 66 0a 2a 2a 20 69 73 20 4e 55  en pBuf.** is NU
215c5 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 65 20  LL..**.** nByte 
215c6 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
215c7 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20   bytes of space 
215c8 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70  needed..**.** *p
215c9 70 46 72 6f 6d 20 70 6f 69 6e 74 73 20 74 6f 20  pFrom points to 
215ca 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20  available space 
215cb 61 6e 64 20 70 45 6e 64 20 70 6f 69 6e 74 73 20  and pEnd points 
215cc 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
215cd 65 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 73  e.** available s
215ce 70 61 63 65 2e 20 20 57 68 65 6e 20 73 70 61 63  pace.  When spac
215cf 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20  e is allocated, 
215d0 2a 70 70 46 72 6f 6d 20 69 73 20 61 64 76 61 6e  *ppFrom is advan
215d1 63 65 64 20 70 61 73 74 0a 2a 2a 20 74 68 65 20  ced past.** the 
215d2 65 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63  end of the alloc
215d3 61 74 65 64 20 73 70 61 63 65 2e 0a 2a 2a 0a 2a  ated space..**.*
215d4 2a 20 2a 70 6e 42 79 74 65 20 69 73 20 61 20 63  * *pnByte is a c
215d5 6f 75 6e 74 65 72 20 6f 66 20 74 68 65 20 6e 75  ounter of the nu
215d6 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
215d7 20 73 70 61 63 65 20 74 68 61 74 20 68 61 76 65   space that have
215d8 20 66 61 69 6c 65 64 0a 2a 2a 20 74 6f 20 61 6c   failed.** to al
215d9 6c 6f 63 61 74 65 2e 20 20 49 66 20 74 68 65 72  locate.  If ther
215da 65 20 69 73 20 69 6e 73 75 66 66 69 63 69 65 6e  e is insufficien
215db 74 20 73 70 61 63 65 20 69 6e 20 2a 70 70 46 72  t space in *ppFr
215dc 6f 6d 20 74 6f 20 73 61 74 69 73 66 79 20 74 68  om to satisfy th
215dd 65 0a 2a 2a 20 72 65 71 75 65 73 74 2c 20 74 68  e.** request, th
215de 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e  en increment *pn
215df 42 79 74 65 20 62 79 20 74 68 65 20 61 6d 6f 75  Byte by the amou
215e0 6e 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 73  nt of the reques
215e1 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
215e2 64 20 2a 61 6c 6c 6f 63 53 70 61 63 65 28 0a 20  d *allocSpace(. 
215e3 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20   void *pBuf,    
215e4 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 72        /* Where r
215e5 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 20 77 69  eturn pointer wi
215e6 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 2a 2f 0a  ll be stored */.
215e7 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
215e8 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
215e9 20 6f 66 20 62 79 74 65 73 20 74 6f 20 61 6c 6c   of bytes to all
215ea 6f 63 61 74 65 20 2a 2f 0a 20 20 75 38 20 2a 2a  ocate */.  u8 **
215eb 70 70 46 72 6f 6d 2c 20 20 20 20 20 20 20 20 20  ppFrom,         
215ec 2f 2a 20 49 4e 2f 4f 55 54 3a 20 41 6c 6c 6f 63  /* IN/OUT: Alloc
215ed 61 74 65 20 66 72 6f 6d 20 2a 70 70 46 72 6f 6d  ate from *ppFrom
215ee 20 2a 2f 0a 20 20 75 38 20 2a 70 45 6e 64 2c 20   */.  u8 *pEnd, 
215ef 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
215f0 69 6e 74 65 72 20 74 6f 20 31 20 62 79 74 65 20  inter to 1 byte 
215f1 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
215f2 2a 70 70 46 72 6f 6d 20 62 75 66 66 65 72 20 2a  *ppFrom buffer *
215f3 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74 65 20  /.  int *pnByte 
215f4 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61           /* If a
215f5 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 6e 6f 74  llocation cannot
215f6 20 62 65 20 6d 61 64 65 2c 20 69 6e 63 72 65 6d   be made, increm
215f7 65 6e 74 20 2a 70 6e 42 79 74 65 20 2a 2f 0a 29  ent *pnByte */.)
215f8 7b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48  {.  assert( EIGH
215f9 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54  T_BYTE_ALIGNMENT
215fa 28 2a 70 70 46 72 6f 6d 29 20 29 3b 0a 20 20 69  (*ppFrom) );.  i
215fb 66 28 20 70 42 75 66 20 29 20 72 65 74 75 72 6e  f( pBuf ) return
215fc 20 70 42 75 66 3b 0a 20 20 6e 42 79 74 65 20 3d   pBuf;.  nByte =
215fd 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a   ROUND8(nByte);.
215fe 20 20 69 66 28 20 26 28 2a 70 70 46 72 6f 6d 29    if( &(*ppFrom)
215ff 5b 6e 42 79 74 65 5d 20 3c 3d 20 70 45 6e 64 20  [nByte] <= pEnd 
21600 29 7b 0a 20 20 20 20 70 42 75 66 20 3d 20 28 76  ){.    pBuf = (v
21601 6f 69 64 2a 29 2a 70 70 46 72 6f 6d 3b 0a 20 20  oid*)*ppFrom;.  
21602 20 20 2a 70 70 46 72 6f 6d 20 2b 3d 20 6e 42 79    *ppFrom += nBy
21603 74 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  te;.  }else{.   
21604 20 2a 70 6e 42 79 74 65 20 2b 3d 20 6e 42 79 74   *pnByte += nByt
21605 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  e;.  }.  return 
21606 70 42 75 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  pBuf;.}../*.** R
21607 65 77 69 6e 64 20 74 68 65 20 56 44 42 45 20 62  ewind the VDBE b
21608 61 63 6b 20 74 6f 20 74 68 65 20 62 65 67 69 6e  ack to the begin
21609 6e 69 6e 67 20 69 6e 20 70 72 65 70 61 72 61 74  ning in preparat
2160a 69 6f 6e 20 66 6f 72 0a 2a 2a 20 72 75 6e 6e 69  ion for.** runni
2160b 6e 67 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ng it..*/.SQLITE
2160c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2160d 6c 69 74 65 33 56 64 62 65 52 65 77 69 6e 64 28  lite3VdbeRewind(
2160e 56 64 62 65 20 2a 70 29 7b 0a 23 69 66 20 64 65  Vdbe *p){.#if de
2160f 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
21610 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56  UG) || defined(V
21611 44 42 45 5f 50 52 4f 46 49 4c 45 29 0a 20 20 69  DBE_PROFILE).  i
21612 6e 74 20 69 3b 0a 23 65 6e 64 69 66 0a 20 20 61  nt i;.#endif.  a
21613 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
21614 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69   assert( p->magi
21615 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e  c==VDBE_MAGIC_IN
21616 49 54 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 72  IT );..  /* Ther
21617 65 20 73 68 6f 75 6c 64 20 62 65 20 61 74 20 6c  e should be at l
21618 65 61 73 74 20 6f 6e 65 20 6f 70 63 6f 64 65 2e  east one opcode.
21619 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
2161a 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f  p->nOp>0 );..  /
2161b 2a 20 53 65 74 20 74 68 65 20 6d 61 67 69 63 20  * Set the magic 
2161c 74 6f 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  to VDBE_MAGIC_RU
2161d 4e 20 73 6f 6f 6e 65 72 20 72 61 74 68 65 72 20  N sooner rather 
2161e 74 68 61 6e 20 6c 61 74 65 72 2e 20 2a 2f 0a 20  than later. */. 
2161f 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45   p->magic = VDBE
21620 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a 0a 23 69 66  _MAGIC_RUN;..#if
21621 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
21622 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 70 2d  .  for(i=1; i<p-
21623 3e 6e 4d 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nMem; i++){.   
21624 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4d 65 6d   assert( p->aMem
21625 5b 69 5d 2e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b  [i].db==p->db );
21626 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 2d  .  }.#endif.  p-
21627 3e 70 63 20 3d 20 2d 31 3b 0a 20 20 70 2d 3e 72  >pc = -1;.  p->r
21628 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
21629 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20   p->errorAction 
2162a 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 70 2d  = OE_Abort;.  p-
2162b 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41  >magic = VDBE_MA
2162c 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43  GIC_RUN;.  p->nC
2162d 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e  hange = 0;.  p->
2162e 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 20  cacheCtr = 1;.  
2162f 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46  p->minWriteFileF
21630 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70  ormat = 255;.  p
21631 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 30  ->iStatement = 0
21632 3b 0a 20 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72  ;.  p->nFkConstr
21633 61 69 6e 74 20 3d 20 30 3b 0a 23 69 66 64 65 66  aint = 0;.#ifdef
21634 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20   VDBE_PROFILE.  
21635 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f  for(i=0; i<p->nO
21636 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 2d 3e  p; i++){.    p->
21637 61 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a  aOp[i].cnt = 0;.
21638 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79      p->aOp[i].cy
21639 63 6c 65 73 20 3d 20 30 3b 0a 20 20 7d 0a 23 65  cles = 0;.  }.#e
2163a 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  ndif.}../*.** Pr
2163b 65 70 61 72 65 20 61 20 76 69 72 74 75 61 6c 20  epare a virtual 
2163c 6d 61 63 68 69 6e 65 20 66 6f 72 20 65 78 65 63  machine for exec
2163d 75 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66 69  ution for the fi
2163e 72 73 74 20 74 69 6d 65 20 61 66 74 65 72 0a 2a  rst time after.*
2163f 2a 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 76  * creating the v
21640 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20  irtual machine. 
21641 20 54 68 69 73 20 69 6e 76 6f 6c 76 65 73 20 74   This involves t
21642 68 69 6e 67 73 20 73 75 63 68 0a 2a 2a 20 61 73  hings such.** as
21643 20 61 6c 6c 6f 63 61 74 69 6e 67 20 73 74 61 63   allocating stac
21644 6b 20 73 70 61 63 65 20 61 6e 64 20 69 6e 69 74  k space and init
21645 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 70 72 6f  ializing the pro
21646 67 72 61 6d 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a  gram counter..**
21647 20 41 66 74 65 72 20 74 68 65 20 56 44 42 45 20   After the VDBE 
21648 68 61 73 20 62 65 20 70 72 65 70 70 65 64 2c 20  has be prepped, 
21649 69 74 20 63 61 6e 20 62 65 20 65 78 65 63 75 74  it can be execut
2164a 65 64 20 62 79 20 6f 6e 65 20 6f 72 20 6d 6f 72  ed by one or mor
2164b 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71  e.** calls to sq
2164c 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e  lite3VdbeExec().
2164d 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75    .**.** This fu
2164e 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61  nction may be ca
2164f 6c 6c 65 64 20 65 78 61 63 74 20 6f 6e 63 65 20  lled exact once 
21650 6f 6e 20 61 20 65 61 63 68 20 76 69 72 74 75 61  on a each virtua
21651 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 20 41 66  l machine..** Af
21652 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
21653 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 56   is called the V
21654 4d 20 68 61 73 20 62 65 65 6e 20 22 70 61 63 6b  M has been "pack
21655 61 67 65 64 22 20 61 6e 64 20 69 73 20 72 65 61  aged" and is rea
21656 64 79 0a 2a 2a 20 74 6f 20 72 75 6e 2e 20 20 41  dy.** to run.  A
21657 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e  fter this routin
21658 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 66 75 74  e is called, fut
21659 68 65 72 20 63 61 6c 6c 73 20 74 6f 20 0a 2a 2a  her calls to .**
2165a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2165b 70 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  p() functions ar
2165c 65 20 70 72 6f 68 69 62 69 74 65 64 2e 20 20 54  e prohibited.  T
2165d 68 69 73 20 72 6f 75 74 69 6e 65 20 64 69 73 63  his routine disc
2165e 6f 6e 6e 65 63 74 73 0a 2a 2a 20 74 68 65 20 56  onnects.** the V
2165f 64 62 65 20 66 72 6f 6d 20 74 68 65 20 50 61 72  dbe from the Par
21660 73 65 20 6f 62 6a 65 63 74 20 74 68 61 74 20 68  se object that h
21661 65 6c 70 65 64 20 67 65 6e 65 72 61 74 65 20 69  elped generate i
21662 74 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a  t so that the.**
21663 20 74 68 65 20 56 64 62 65 20 62 65 63 6f 6d 65   the Vdbe become
21664 73 20 61 6e 20 69 6e 64 65 70 65 6e 64 65 6e 74  s an independent
21665 20 65 6e 74 69 74 79 20 61 6e 64 20 74 68 65 20   entity and the 
21666 50 61 72 73 65 20 6f 62 6a 65 63 74 20 63 61 6e  Parse object can
21667 20 62 65 0a 2a 2a 20 64 65 73 74 72 6f 79 65 64   be.** destroyed
21668 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20  ..**.** Use the 
21669 73 71 6c 69 74 65 33 56 64 62 65 52 65 77 69 6e  sqlite3VdbeRewin
2166a 64 28 29 20 70 72 6f 63 65 64 75 72 65 20 74 6f  d() procedure to
2166b 20 72 65 73 74 6f 72 65 20 61 20 76 69 72 74 75   restore a virtu
2166c 61 6c 20 6d 61 63 68 69 6e 65 20 62 61 63 6b 0a  al machine back.
2166d 2a 2a 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61  ** to its initia
2166e 6c 20 73 74 61 74 65 20 61 66 74 65 72 20 69 74  l state after it
2166f 20 68 61 73 20 62 65 65 6e 20 72 75 6e 2e 0a 2a   has been run..*
21670 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21671 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
21672 65 4d 61 6b 65 52 65 61 64 79 28 0a 20 20 56 64  eMakeReady(.  Vd
21673 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  be *p,          
21674 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21675 54 68 65 20 56 44 42 45 20 2a 2f 0a 20 20 50 61  The VDBE */.  Pa
21676 72 73 65 20 2a 70 50 61 72 73 65 20 20 20 20 20  rse *pParse     
21677 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21678 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
21679 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
2167a 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  *db;            
2167b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
2167c 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2167d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 3b  n */.  int nVar;
2167e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2167f 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
21680 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f  of parameters */
21681 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20  .  int nMem;    
21682 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21683 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 56    /* Number of V
21684 4d 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65  M memory registe
21685 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72  rs */.  int nCur
21686 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  sor;            
21687 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
21688 20 6f 66 20 63 75 72 73 6f 72 73 20 72 65 71 75   of cursors requ
21689 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  ired */.  int nA
2168a 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
2168b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2168c 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
2168d 69 6e 20 73 75 62 70 72 6f 67 72 61 6d 73 20 2a  in subprograms *
2168e 2f 0a 20 20 69 6e 74 20 6e 4f 6e 63 65 3b 20 20  /.  int nOnce;  
2168f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21690 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
21691 4f 50 5f 4f 6e 63 65 20 69 6e 73 74 72 75 63 74  OP_Once instruct
21692 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b  ions */.  int n;
21693 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21694 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
21695 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38   counter */.  u8
21696 20 2a 7a 43 73 72 3b 20 20 20 20 20 20 20 20 20   *zCsr;         
21697 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21698 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65  Memory available
21699 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   for allocation 
2169a 2a 2f 0a 20 20 75 38 20 2a 7a 45 6e 64 3b 20 20  */.  u8 *zEnd;  
2169b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2169c 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74      /* First byt
2169d 65 20 70 61 73 74 20 61 6c 6c 6f 63 61 74 65 64  e past allocated
2169e 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74   memory */.  int
2169f 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20   nByte;         
216a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
216a1 6f 77 20 6d 75 63 68 20 65 78 74 72 61 20 6d 65  ow much extra me
216a2 6d 6f 72 79 20 69 73 20 6e 65 65 64 65 64 20 2a  mory is needed *
216a3 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d  /..  assert( p!=
216a4 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
216a5 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20 61 73 73  ->nOp>0 );.  ass
216a6 65 72 74 28 20 70 50 61 72 73 65 21 3d 30 20 29  ert( pParse!=0 )
216a7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d  ;.  assert( p->m
216a8 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
216a9 5f 49 4e 49 54 20 29 3b 0a 20 20 64 62 20 3d 20  _INIT );.  db = 
216aa 70 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28  p->db;.  assert(
216ab 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
216ac 64 3d 3d 30 20 29 3b 0a 20 20 6e 56 61 72 20 3d  d==0 );.  nVar =
216ad 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3b 0a 20   pParse->nVar;. 
216ae 20 6e 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e   nMem = pParse->
216af 6e 4d 65 6d 3b 0a 20 20 6e 43 75 72 73 6f 72 20  nMem;.  nCursor 
216b0 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a  = pParse->nTab;.
216b1 20 20 6e 41 72 67 20 3d 20 70 50 61 72 73 65 2d    nArg = pParse-
216b2 3e 6e 4d 61 78 41 72 67 3b 0a 20 20 6e 4f 6e 63  >nMaxArg;.  nOnc
216b3 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4f 6e 63  e = pParse->nOnc
216b4 65 3b 0a 20 20 69 66 28 20 6e 4f 6e 63 65 3d 3d  e;.  if( nOnce==
216b5 30 20 29 20 6e 4f 6e 63 65 20 3d 20 31 3b 20 2f  0 ) nOnce = 1; /
216b6 2a 20 45 6e 73 75 72 65 20 61 74 20 6c 65 61 73  * Ensure at leas
216b7 74 20 6f 6e 65 20 62 79 74 65 20 69 6e 20 70 2d  t one byte in p-
216b8 3e 61 4f 6e 63 65 46 6c 61 67 5b 5d 20 2a 2f 0a  >aOnceFlag[] */.
216b9 20 20 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63 68    .  /* For each
216ba 20 63 75 72 73 6f 72 20 72 65 71 75 69 72 65 64   cursor required
216bb 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 20  , also allocate 
216bc 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 4d  a memory cell. M
216bd 65 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c 73  emory.  ** cells
216be 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f 72   (nMem+1-nCursor
216bf 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73 69  )..nMem, inclusi
216c0 76 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20 62  ve, will never b
216c1 65 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 74  e used by.  ** t
216c2 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e  he vdbe program.
216c3 20 49 6e 73 74 65 61 64 20 74 68 65 79 20 61 72   Instead they ar
216c4 65 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61  e used to alloca
216c5 74 65 20 73 70 61 63 65 20 66 6f 72 0a 20 20 2a  te space for.  *
216c6 2a 20 56 64 62 65 43 75 72 73 6f 72 2f 42 74 43  * VdbeCursor/BtC
216c7 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 73  ursor structures
216c8 2e 20 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65  . The blob of me
216c9 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20  mory associated 
216ca 77 69 74 68 20 0a 20 20 2a 2a 20 63 75 72 73 6f  with .  ** curso
216cb 72 20 30 20 69 73 20 73 74 6f 72 65 64 20 69 6e  r 0 is stored in
216cc 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d 65   memory cell nMe
216cd 6d 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28  m. Memory cell (
216ce 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74 6f  nMem-1).  ** sto
216cf 72 65 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 20  res the blob of 
216d0 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
216d1 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 31 2c  d with cursor 1,
216d2 20 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20   etc..  **.  ** 
216d3 53 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63 61  See also: alloca
216d4 74 65 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a 2f  teCursor()..  */
216d5 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72 73  .  nMem += nCurs
216d6 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61  or;..  /* Alloca
216d7 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65 6d  te space for mem
216d8 6f 72 79 20 72 65 67 69 73 74 65 72 73 2c 20 53  ory registers, S
216d9 51 4c 20 76 61 72 69 61 62 6c 65 73 2c 20 56 44  QL variables, VD
216da 42 45 20 63 75 72 73 6f 72 73 20 61 6e 64 20 0a  BE cursors and .
216db 20 20 2a 2a 20 61 6e 20 61 72 72 61 79 20 74 6f    ** an array to
216dc 20 6d 61 72 73 68 61 6c 20 53 51 4c 20 66 75 6e   marshal SQL fun
216dd 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20  ction arguments 
216de 69 6e 2e 0a 20 20 2a 2f 0a 20 20 7a 43 73 72 20  in..  */.  zCsr 
216df 3d 20 28 75 38 2a 29 26 70 2d 3e 61 4f 70 5b 70  = (u8*)&p->aOp[p
216e0 2d 3e 6e 4f 70 5d 3b 20 20 20 20 20 20 20 2f 2a  ->nOp];       /*
216e1 20 4d 65 6d 6f 72 79 20 61 76 61 6c 69 61 62 6c   Memory avaliabl
216e2 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  e for allocation
216e3 20 2a 2f 0a 20 20 7a 45 6e 64 20 3d 20 28 75 38   */.  zEnd = (u8
216e4 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70  *)&p->aOp[p->nOp
216e5 41 6c 6c 6f 63 5d 3b 20 20 2f 2a 20 46 69 72 73  Alloc];  /* Firs
216e6 74 20 62 79 74 65 20 70 61 73 74 20 65 6e 64 20  t byte past end 
216e7 6f 66 20 7a 43 73 72 5b 5d 20 2a 2f 0a 0a 20 20  of zCsr[] */..  
216e8 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 28  resolveP2Values(
216e9 70 2c 20 26 6e 41 72 67 29 3b 0a 20 20 70 2d 3e  p, &nArg);.  p->
216ea 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20  usesStmtJournal 
216eb 3d 20 28 75 38 29 28 70 50 61 72 73 65 2d 3e 69  = (u8)(pParse->i
216ec 73 4d 75 6c 74 69 57 72 69 74 65 20 26 26 20 70  sMultiWrite && p
216ed 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f 72 74 29  Parse->mayAbort)
216ee 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
216ef 65 78 70 6c 61 69 6e 20 26 26 20 6e 4d 65 6d 3c  explain && nMem<
216f0 31 30 20 29 7b 0a 20 20 20 20 6e 4d 65 6d 20 3d  10 ){.    nMem =
216f1 20 31 30 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65   10;.  }.  memse
216f2 74 28 7a 43 73 72 2c 20 30 2c 20 7a 45 6e 64 2d  t(zCsr, 0, zEnd-
216f3 7a 43 73 72 29 3b 0a 20 20 7a 43 73 72 20 2b 3d  zCsr);.  zCsr +=
216f4 20 28 7a 43 73 72 20 2d 20 28 75 38 2a 29 30 29   (zCsr - (u8*)0)
216f5 26 37 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49  &7;.  assert( EI
216f6 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
216f7 4e 54 28 7a 43 73 72 29 20 29 3b 0a 20 20 70 2d  NT(zCsr) );.  p-
216f8 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 0a 20  >expired = 0;.. 
216f9 20 2f 2a 20 4d 65 6d 6f 72 79 20 66 6f 72 20 72   /* Memory for r
216fa 65 67 69 73 74 65 72 73 2c 20 70 61 72 61 6d 65  egisters, parame
216fb 74 65 72 73 2c 20 63 75 72 73 6f 72 2c 20 65 74  ters, cursor, et
216fc 63 2c 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  c, is allocated 
216fd 69 6e 20 74 77 6f 0a 20 20 2a 2a 20 70 61 73 73  in two.  ** pass
216fe 65 73 2e 20 20 4f 6e 20 74 68 65 20 66 69 72 73  es.  On the firs
216ff 74 20 70 61 73 73 2c 20 77 65 20 74 72 79 20 74  t pass, we try t
21700 6f 20 72 65 75 73 65 20 75 6e 75 73 65 64 20 73  o reuse unused s
21701 70 61 63 65 20 61 74 20 74 68 65 20 0a 20 20 2a  pace at the .  *
21702 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 6f 70 63  * end of the opc
21703 6f 64 65 20 61 72 72 61 79 2e 20 20 49 66 20 77  ode array.  If w
21704 65 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 20  e are unable to 
21705 73 61 74 69 73 66 79 20 61 6c 6c 20 6d 65 6d 6f  satisfy all memo
21706 72 79 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 6d  ry.  ** requirem
21707 65 6e 74 73 20 62 79 20 72 65 75 73 69 6e 67 20  ents by reusing 
21708 74 68 65 20 6f 70 63 6f 64 65 20 61 72 72 61 79  the opcode array
21709 20 74 61 69 6c 2c 20 74 68 65 6e 20 74 68 65 20   tail, then the 
2170a 73 65 63 6f 6e 64 0a 20 20 2a 2a 20 70 61 73 73  second.  ** pass
2170b 20 77 69 6c 6c 20 66 69 6c 6c 20 69 6e 20 74 68   will fill in th
2170c 65 20 72 65 73 74 20 75 73 69 6e 67 20 61 20 66  e rest using a f
2170d 72 65 73 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  resh allocation.
2170e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69    .  **.  ** Thi
2170f 73 20 74 77 6f 2d 70 61 73 73 20 61 70 70 72 6f  s two-pass appro
21710 61 63 68 20 74 68 61 74 20 72 65 75 73 65 73 20  ach that reuses 
21711 61 73 20 6d 75 63 68 20 6d 65 6d 6f 72 79 20 61  as much memory a
21712 73 20 70 6f 73 73 69 62 6c 65 20 66 72 6f 6d 0a  s possible from.
21713 20 20 2a 2a 20 74 68 65 20 6c 65 66 74 6f 76 65    ** the leftove
21714 72 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65  r space at the e
21715 6e 64 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65  nd of the opcode
21716 20 61 72 72 61 79 20 63 61 6e 20 73 69 67 6e 69   array can signi
21717 66 69 63 61 6e 74 6c 79 0a 20 20 2a 2a 20 72 65  ficantly.  ** re
21718 64 75 63 65 20 74 68 65 20 61 6d 6f 75 6e 74 20  duce the amount 
21719 6f 66 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62  of memory held b
2171a 79 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61  y a prepared sta
2171b 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 64  tement..  */.  d
2171c 6f 20 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20  o {.    nByte = 
2171d 30 3b 0a 20 20 20 20 70 2d 3e 61 4d 65 6d 20 3d  0;.    p->aMem =
2171e 20 61 6c 6c 6f 63 53 70 61 63 65 28 70 2d 3e 61   allocSpace(p->a
2171f 4d 65 6d 2c 20 6e 4d 65 6d 2a 73 69 7a 65 6f 66  Mem, nMem*sizeof
21720 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45  (Mem), &zCsr, zE
21721 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20  nd, &nByte);.   
21722 20 70 2d 3e 61 56 61 72 20 3d 20 61 6c 6c 6f 63   p->aVar = alloc
21723 53 70 61 63 65 28 70 2d 3e 61 56 61 72 2c 20 6e  Space(p->aVar, n
21724 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c  Var*sizeof(Mem),
21725 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e   &zCsr, zEnd, &n
21726 42 79 74 65 29 3b 0a 20 20 20 20 70 2d 3e 61 70  Byte);.    p->ap
21727 41 72 67 20 3d 20 61 6c 6c 6f 63 53 70 61 63 65  Arg = allocSpace
21728 28 70 2d 3e 61 70 41 72 67 2c 20 6e 41 72 67 2a  (p->apArg, nArg*
21729 73 69 7a 65 6f 66 28 4d 65 6d 2a 29 2c 20 26 7a  sizeof(Mem*), &z
2172a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74  Csr, zEnd, &nByt
2172b 65 29 3b 0a 20 20 20 20 70 2d 3e 61 7a 56 61 72  e);.    p->azVar
2172c 20 3d 20 61 6c 6c 6f 63 53 70 61 63 65 28 70 2d   = allocSpace(p-
2172d 3e 61 7a 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a  >azVar, nVar*siz
2172e 65 6f 66 28 63 68 61 72 2a 29 2c 20 26 7a 43 73  eof(char*), &zCs
2172f 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29  r, zEnd, &nByte)
21730 3b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 20 3d  ;.    p->apCsr =
21731 20 61 6c 6c 6f 63 53 70 61 63 65 28 70 2d 3e 61   allocSpace(p->a
21732 70 43 73 72 2c 20 6e 43 75 72 73 6f 72 2a 73 69  pCsr, nCursor*si
21733 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 2a  zeof(VdbeCursor*
21734 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
21735 20 20 20 20 20 20 20 20 20 20 20 20 20 26 7a 43               &zC
21736 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65  sr, zEnd, &nByte
21737 29 3b 0a 20 20 20 20 70 2d 3e 61 4f 6e 63 65 46  );.    p->aOnceF
21738 6c 61 67 20 3d 20 61 6c 6c 6f 63 53 70 61 63 65  lag = allocSpace
21739 28 70 2d 3e 61 4f 6e 63 65 46 6c 61 67 2c 20 6e  (p->aOnceFlag, n
2173a 4f 6e 63 65 2c 20 26 7a 43 73 72 2c 20 7a 45 6e  Once, &zCsr, zEn
2173b 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20  d, &nByte);.    
2173c 69 66 28 20 6e 42 79 74 65 20 29 7b 0a 20 20 20  if( nByte ){.   
2173d 20 20 20 70 2d 3e 70 46 72 65 65 20 3d 20 73 71     p->pFree = sq
2173e 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
2173f 6f 28 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20  o(db, nByte);.  
21740 20 20 7d 0a 20 20 20 20 7a 43 73 72 20 3d 20 70    }.    zCsr = p
21741 2d 3e 70 46 72 65 65 3b 0a 20 20 20 20 7a 45 6e  ->pFree;.    zEn
21742 64 20 3d 20 26 7a 43 73 72 5b 6e 42 79 74 65 5d  d = &zCsr[nByte]
21743 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6e 42 79 74  ;.  }while( nByt
21744 65 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  e && !db->malloc
21745 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 70 2d 3e  Failed );..  p->
21746 6e 43 75 72 73 6f 72 20 3d 20 28 75 31 36 29 6e  nCursor = (u16)n
21747 43 75 72 73 6f 72 3b 0a 20 20 70 2d 3e 6e 4f 6e  Cursor;.  p->nOn
21748 63 65 46 6c 61 67 20 3d 20 6e 4f 6e 63 65 3b 0a  ceFlag = nOnce;.
21749 20 20 69 66 28 20 70 2d 3e 61 56 61 72 20 29 7b    if( p->aVar ){
2174a 0a 20 20 20 20 70 2d 3e 6e 56 61 72 20 3d 20 28  .    p->nVar = (
2174b 79 6e 56 61 72 29 6e 56 61 72 3b 0a 20 20 20 20  ynVar)nVar;.    
2174c 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b  for(n=0; n<nVar;
2174d 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e   n++){.      p->
2174e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20  aVar[n].flags = 
2174f 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20  MEM_Null;.      
21750 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 62 20 3d 20  p->aVar[n].db = 
21751 64 62 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  db;.    }.  }.  
21752 69 66 28 20 70 2d 3e 61 7a 56 61 72 20 29 7b 0a  if( p->azVar ){.
21753 20 20 20 20 70 2d 3e 6e 7a 56 61 72 20 3d 20 70      p->nzVar = p
21754 50 61 72 73 65 2d 3e 6e 7a 56 61 72 3b 0a 20 20  Parse->nzVar;.  
21755 20 20 6d 65 6d 63 70 79 28 70 2d 3e 61 7a 56 61    memcpy(p->azVa
21756 72 2c 20 70 50 61 72 73 65 2d 3e 61 7a 56 61 72  r, pParse->azVar
21757 2c 20 70 2d 3e 6e 7a 56 61 72 2a 73 69 7a 65 6f  , p->nzVar*sizeo
21758 66 28 70 2d 3e 61 7a 56 61 72 5b 30 5d 29 29 3b  f(p->azVar[0]));
21759 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 72  .    memset(pPar
2175a 73 65 2d 3e 61 7a 56 61 72 2c 20 30 2c 20 70 50  se->azVar, 0, pP
2175b 61 72 73 65 2d 3e 6e 7a 56 61 72 2a 73 69 7a 65  arse->nzVar*size
2175c 6f 66 28 70 50 61 72 73 65 2d 3e 61 7a 56 61 72  of(pParse->azVar
2175d 5b 30 5d 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28  [0]));.  }.  if(
2175e 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20   p->aMem ){.    
2175f 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20  p->aMem--;      
21760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21761 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 20 66  /* aMem[] goes f
21762 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a 20  rom 1..nMem */. 
21763 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e 4d 65     p->nMem = nMe
21764 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
21765 20 20 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74 20     /*       not 
21766 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a  from 0..nMem-1 *
21767 2f 0a 20 20 20 20 66 6f 72 28 6e 3d 31 3b 20 6e  /.    for(n=1; n
21768 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20  <=nMem; n++){.  
21769 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 66      p->aMem[n].f
2176a 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 76 61 6c  lags = MEM_Inval
2176b 69 64 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4d 65  id;.      p->aMe
2176c 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20  m[n].db = db;.  
2176d 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 65 78 70    }.  }.  p->exp
2176e 6c 61 69 6e 20 3d 20 70 50 61 72 73 65 2d 3e 65  lain = pParse->e
2176f 78 70 6c 61 69 6e 3b 0a 20 20 73 71 6c 69 74 65  xplain;.  sqlite
21770 33 56 64 62 65 52 65 77 69 6e 64 28 70 29 3b 0a  3VdbeRewind(p);.
21771 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
21772 20 56 44 42 45 20 63 75 72 73 6f 72 20 61 6e 64   VDBE cursor and
21773 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65   release all the
21774 20 72 65 73 6f 75 72 63 65 73 20 74 68 61 74 20   resources that 
21775 63 75 72 73 6f 72 20 0a 2a 2a 20 68 61 70 70 65  cursor .** happe
21776 6e 73 20 74 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53  ns to hold..*/.S
21777 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
21778 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72  id sqlite3VdbeFr
21779 65 65 43 75 72 73 6f 72 28 56 64 62 65 20 2a 70  eeCursor(Vdbe *p
2177a 2c 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43  , VdbeCursor *pC
2177b 78 29 7b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30  x){.  if( pCx==0
2177c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
2177d 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
2177e 65 53 6f 72 74 65 72 43 6c 6f 73 65 28 70 2d 3e  eSorterClose(p->
2177f 64 62 2c 20 70 43 78 29 3b 0a 20 20 69 66 28 20  db, pCx);.  if( 
21780 70 43 78 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20  pCx->pBt ){.    
21781 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
21782 65 28 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 20  e(pCx->pBt);.   
21783 20 2f 2a 20 54 68 65 20 70 43 78 2d 3e 70 43 75   /* The pCx->pCu
21784 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 63 6c 6f  rsor will be clo
21785 73 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  se automatically
21786 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 2c 20  , if it exists, 
21787 62 79 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61  by.    ** the ca
21788 6c 6c 20 61 62 6f 76 65 2e 20 2a 2f 0a 20 20 7d  ll above. */.  }
21789 65 6c 73 65 20 69 66 28 20 70 43 78 2d 3e 70 43  else if( pCx->pC
2178a 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c  ursor ){.    sql
2178b 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75  ite3BtreeCloseCu
2178c 72 73 6f 72 28 70 43 78 2d 3e 70 43 75 72 73 6f  rsor(pCx->pCurso
2178d 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20  r);.  }.#ifndef 
2178e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2178f 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70  UALTABLE.  if( p
21790 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20  Cx->pVtabCursor 
21791 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
21792 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61  tab_cursor *pVta
21793 62 43 75 72 73 6f 72 20 3d 20 70 43 78 2d 3e 70  bCursor = pCx->p
21794 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20  VtabCursor;.    
21795 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
21796 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d 20  dule *pModule = 
21797 70 43 78 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  pCx->pModule;.  
21798 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f    p->inVtabMetho
21799 64 20 3d 20 31 3b 0a 20 20 20 20 70 4d 6f 64 75  d = 1;.    pModu
2179a 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 56 74 61 62  le->xClose(pVtab
2179b 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 70 2d 3e  Cursor);.    p->
2179c 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30  inVtabMethod = 0
2179d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a  ;.  }.#endif.}..
2179e 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 76  /*.** Copy the v
2179f 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20  alues stored in 
217a0 74 68 65 20 56 64 62 65 46 72 61 6d 65 20 73 74  the VdbeFrame st
217a1 72 75 63 74 75 72 65 20 74 6f 20 69 74 73 20 56  ructure to its V
217a2 64 62 65 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20  dbe. This.** is 
217a3 75 73 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c  used, for exampl
217a4 65 2c 20 77 68 65 6e 20 61 20 74 72 69 67 67 65  e, when a trigge
217a5 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 69 73  r sub-program is
217a6 20 68 61 6c 74 65 64 20 74 6f 20 72 65 73 74 6f   halted to resto
217a7 72 65 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 20 74 6f  re.** control to
217a8 20 74 68 65 20 6d 61 69 6e 20 70 72 6f 67 72 61   the main progra
217a9 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  m..*/.SQLITE_PRI
217aa 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
217ab 56 64 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65  VdbeFrameRestore
217ac 28 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61  (VdbeFrame *pFra
217ad 6d 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  me){.  Vdbe *v =
217ae 20 70 46 72 61 6d 65 2d 3e 76 3b 0a 20 20 76 2d   pFrame->v;.  v-
217af 3e 61 4f 6e 63 65 46 6c 61 67 20 3d 20 70 46 72  >aOnceFlag = pFr
217b0 61 6d 65 2d 3e 61 4f 6e 63 65 46 6c 61 67 3b 0a  ame->aOnceFlag;.
217b1 20 20 76 2d 3e 6e 4f 6e 63 65 46 6c 61 67 20 3d    v->nOnceFlag =
217b2 20 70 46 72 61 6d 65 2d 3e 6e 4f 6e 63 65 46 6c   pFrame->nOnceFl
217b3 61 67 3b 0a 20 20 76 2d 3e 61 4f 70 20 3d 20 70  ag;.  v->aOp = p
217b4 46 72 61 6d 65 2d 3e 61 4f 70 3b 0a 20 20 76 2d  Frame->aOp;.  v-
217b5 3e 6e 4f 70 20 3d 20 70 46 72 61 6d 65 2d 3e 6e  >nOp = pFrame->n
217b6 4f 70 3b 0a 20 20 76 2d 3e 61 4d 65 6d 20 3d 20  Op;.  v->aMem = 
217b7 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 3b 0a 20 20  pFrame->aMem;.  
217b8 76 2d 3e 6e 4d 65 6d 20 3d 20 70 46 72 61 6d 65  v->nMem = pFrame
217b9 2d 3e 6e 4d 65 6d 3b 0a 20 20 76 2d 3e 61 70 43  ->nMem;.  v->apC
217ba 73 72 20 3d 20 70 46 72 61 6d 65 2d 3e 61 70 43  sr = pFrame->apC
217bb 73 72 3b 0a 20 20 76 2d 3e 6e 43 75 72 73 6f 72  sr;.  v->nCursor
217bc 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 43 75 72 73   = pFrame->nCurs
217bd 6f 72 3b 0a 20 20 76 2d 3e 64 62 2d 3e 6c 61 73  or;.  v->db->las
217be 74 52 6f 77 69 64 20 3d 20 70 46 72 61 6d 65 2d  tRowid = pFrame-
217bf 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 76 2d  >lastRowid;.  v-
217c0 3e 6e 43 68 61 6e 67 65 20 3d 20 70 46 72 61 6d  >nChange = pFram
217c1 65 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 72 65  e->nChange;.  re
217c2 74 75 72 6e 20 70 46 72 61 6d 65 2d 3e 70 63 3b  turn pFrame->pc;
217c3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
217c4 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2a 0a  all cursors..**.
217c5 2a 2a 20 41 6c 73 6f 20 72 65 6c 65 61 73 65 20  ** Also release 
217c6 61 6e 79 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f  any dynamic memo
217c7 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 56  ry held by the V
217c8 4d 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61 4d  M in the Vdbe.aM
217c9 65 6d 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65  em memory .** ce
217ca 6c 6c 20 61 72 72 61 79 2e 20 54 68 69 73 20 69  ll array. This i
217cb 73 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 74  s necessary as t
217cc 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61  he memory cell a
217cd 72 72 61 79 20 6d 61 79 20 63 6f 6e 74 61 69 6e  rray may contain
217ce 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  .** pointers to 
217cf 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 63 74  VdbeFrame object
217d0 73 2c 20 77 68 69 63 68 20 6d 61 79 20 69 6e 20  s, which may in 
217d1 74 75 72 6e 20 63 6f 6e 74 61 69 6e 20 70 6f 69  turn contain poi
217d2 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 6f 70 65 6e  nters to.** open
217d3 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 73 74 61   cursors..*/.sta
217d4 74 69 63 20 76 6f 69 64 20 63 6c 6f 73 65 41 6c  tic void closeAl
217d5 6c 43 75 72 73 6f 72 73 28 56 64 62 65 20 2a 70  lCursors(Vdbe *p
217d6 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 46 72 61  ){.  if( p->pFra
217d7 6d 65 20 29 7b 0a 20 20 20 20 56 64 62 65 46 72  me ){.    VdbeFr
217d8 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 20  ame *pFrame;.   
217d9 20 66 6f 72 28 70 46 72 61 6d 65 3d 70 2d 3e 70   for(pFrame=p->p
217da 46 72 61 6d 65 3b 20 70 46 72 61 6d 65 2d 3e 70  Frame; pFrame->p
217db 50 61 72 65 6e 74 3b 20 70 46 72 61 6d 65 3d 70  Parent; pFrame=p
217dc 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b  Frame->pParent);
217dd 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
217de 46 72 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72  FrameRestore(pFr
217df 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70  ame);.  }.  p->p
217e0 46 72 61 6d 65 20 3d 20 30 3b 0a 20 20 70 2d 3e  Frame = 0;.  p->
217e1 6e 46 72 61 6d 65 20 3d 20 30 3b 0a 0a 20 20 69  nFrame = 0;..  i
217e2 66 28 20 70 2d 3e 61 70 43 73 72 20 29 7b 0a 20  f( p->apCsr ){. 
217e3 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
217e4 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72  r(i=0; i<p->nCur
217e5 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  sor; i++){.     
217e6 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20   VdbeCursor *pC 
217e7 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20  = p->apCsr[i];. 
217e8 20 20 20 20 20 69 66 28 20 70 43 20 29 7b 0a 20       if( pC ){. 
217e9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
217ea 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20  beFreeCursor(p, 
217eb 70 43 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  pC);.        p->
217ec 61 70 43 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20  apCsr[i] = 0;.  
217ed 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
217ee 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b    if( p->aMem ){
217ef 0a 20 20 20 20 72 65 6c 65 61 73 65 4d 65 6d 41  .    releaseMemA
217f0 72 72 61 79 28 26 70 2d 3e 61 4d 65 6d 5b 31 5d  rray(&p->aMem[1]
217f1 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 7d 0a  , p->nMem);.  }.
217f2 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 44 65 6c    while( p->pDel
217f3 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 62  Frame ){.    Vdb
217f4 65 46 72 61 6d 65 20 2a 70 44 65 6c 20 3d 20 70  eFrame *pDel = p
217f5 2d 3e 70 44 65 6c 46 72 61 6d 65 3b 0a 20 20 20  ->pDelFrame;.   
217f6 20 70 2d 3e 70 44 65 6c 46 72 61 6d 65 20 3d 20   p->pDelFrame = 
217f7 70 44 65 6c 2d 3e 70 50 61 72 65 6e 74 3b 0a 20  pDel->pParent;. 
217f8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72     sqlite3VdbeFr
217f9 61 6d 65 44 65 6c 65 74 65 28 70 44 65 6c 29 3b  ameDelete(pDel);
217fa 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  .  }.}../*.** Cl
217fb 65 61 6e 20 75 70 20 74 68 65 20 56 4d 20 61 66  ean up the VM af
217fc 74 65 72 20 65 78 65 63 75 74 69 6f 6e 2e 0a 2a  ter execution..*
217fd 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
217fe 65 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63  e will automatic
217ff 61 6c 6c 79 20 63 6c 6f 73 65 20 61 6e 79 20 63  ally close any c
21800 75 72 73 6f 72 73 2c 20 6c 69 73 74 73 2c 20 61  ursors, lists, a
21801 6e 64 2f 6f 72 0a 2a 2a 20 73 6f 72 74 65 72 73  nd/or.** sorters
21802 20 74 68 61 74 20 77 65 72 65 20 6c 65 66 74 20   that were left 
21803 6f 70 65 6e 2e 20 20 49 74 20 61 6c 73 6f 20 64  open.  It also d
21804 65 6c 65 74 65 73 20 74 68 65 20 76 61 6c 75 65  eletes the value
21805 73 20 6f 66 0a 2a 2a 20 76 61 72 69 61 62 6c 65  s of.** variable
21806 73 20 69 6e 20 74 68 65 20 61 56 61 72 5b 5d 20  s in the aVar[] 
21807 61 72 72 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  array..*/.static
21808 20 76 6f 69 64 20 43 6c 65 61 6e 75 70 28 56 64   void Cleanup(Vd
21809 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  be *p){.  sqlite
2180a 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a  3 *db = p->db;..
2180b 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
2180c 42 55 47 0a 20 20 2f 2a 20 45 78 65 63 75 74 65  BUG.  /* Execute
2180d 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
2180e 65 6e 74 73 20 74 6f 20 65 6e 73 75 72 65 20 74  ents to ensure t
2180f 68 61 74 20 74 68 65 20 56 64 62 65 2e 61 70 43  hat the Vdbe.apC
21810 73 72 5b 5d 20 61 6e 64 20 0a 20 20 2a 2a 20 56  sr[] and .  ** V
21811 64 62 65 2e 61 4d 65 6d 5b 5d 20 61 72 72 61 79  dbe.aMem[] array
21812 73 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  s have already b
21813 65 65 6e 20 63 6c 65 61 6e 65 64 20 75 70 2e 20  een cleaned up. 
21814 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69   */.  int i;.  i
21815 66 28 20 70 2d 3e 61 70 43 73 72 20 29 20 66 6f  f( p->apCsr ) fo
21816 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72  r(i=0; i<p->nCur
21817 73 6f 72 3b 20 69 2b 2b 29 20 61 73 73 65 72 74  sor; i++) assert
21818 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3d 3d 30  ( p->apCsr[i]==0
21819 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65   );.  if( p->aMe
2181a 6d 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 31  m ){.    for(i=1
2181b 3b 20 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 2b  ; i<=p->nMem; i+
2181c 2b 29 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4d  +) assert( p->aM
2181d 65 6d 5b 69 5d 2e 66 6c 61 67 73 3d 3d 4d 45 4d  em[i].flags==MEM
2181e 5f 49 6e 76 61 6c 69 64 20 29 3b 0a 20 20 7d 0a  _Invalid );.  }.
2181f 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65  #endif..  sqlite
21820 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
21821 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45  ErrMsg);.  p->zE
21822 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e  rrMsg = 0;.  p->
21823 70 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a  pResultSet = 0;.
21824 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
21825 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c   number of resul
21826 74 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77  t columns that w
21827 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20  ill be returned 
21828 62 79 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73  by this SQL.** s
21829 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69  tatement. This i
2182a 73 20 6e 6f 77 20 73 65 74 20 61 74 20 63 6f 6d  s now set at com
2182b 70 69 6c 65 20 74 69 6d 65 2c 20 72 61 74 68 65  pile time, rathe
2182c 72 20 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a  r than during.**
2182d 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68   execution of th
2182e 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 73  e vdbe program s
2182f 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63  o that sqlite3_c
21830 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61  olumn_count() ca
21831 6e 0a 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f  n.** be called o
21832 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  n an SQL stateme
21833 6e 74 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65  nt before sqlite
21834 33 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c  3_step()..*/.SQL
21835 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
21836 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
21837 75 6d 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20  umCols(Vdbe *p, 
21838 69 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b  int nResColumn){
21839 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65  .  Mem *pColName
2183a 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c  ;.  int n;.  sql
2183b 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
2183c 3b 0a 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41  ;..  releaseMemA
2183d 72 72 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65  rray(p->aColName
2183e 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a  , p->nResColumn*
2183f 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71  COLNAME_N);.  sq
21840 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
21841 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20  p->aColName);.  
21842 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43  n = nResColumn*C
21843 4f 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e  OLNAME_N;.  p->n
21844 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 28 75 31 36  ResColumn = (u16
21845 29 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70  )nResColumn;.  p
21846 2d 3e 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f  ->aColName = pCo
21847 6c 4e 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71  lName = (Mem*)sq
21848 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
21849 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d  o(db, sizeof(Mem
2184a 29 2a 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  )*n );.  if( p->
2184b 61 43 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65  aColName==0 ) re
2184c 74 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e  turn;.  while( n
2184d 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 43  -- > 0 ){.    pC
2184e 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20  olName->flags = 
2184f 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43  MEM_Null;.    pC
21850 6f 6c 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e  olName->db = p->
21851 64 62 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65  db;.    pColName
21852 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ++;.  }.}../*.**
21853 20 53 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66   Set the name of
21854 20 74 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75   the idx'th colu
21855 6d 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65  mn to be returne
21856 64 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61  d by the SQL sta
21857 74 65 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65  tement..** zName
21858 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74   must be a point
21859 65 72 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d  er to a nul term
2185a 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a  inated string..*
2185b 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d  *.** This call m
2185c 75 73 74 20 62 65 20 6d 61 64 65 20 61 66 74 65  ust be made afte
2185d 72 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  r a call to sqli
2185e 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
2185f 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  s()..**.** The f
21860 69 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 2c 20  inal parameter, 
21861 78 44 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e  xDel, must be on
21862 65 20 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e 41  e of SQLITE_DYNA
21863 4d 49 43 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  MIC, SQLITE_STAT
21864 49 43 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f  IC.** or SQLITE_
21865 54 52 41 4e 53 49 45 4e 54 2e 20 49 66 20 69 74  TRANSIENT. If it
21866 20 69 73 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d   is SQLITE_DYNAM
21867 49 43 2c 20 74 68 65 6e 20 74 68 65 20 62 75 66  IC, then the buf
21868 66 65 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74  fer pointed.** t
21869 6f 20 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20  o by zName will 
2186a 62 65 20 66 72 65 65 64 20 62 79 20 73 71 6c 69  be freed by sqli
2186b 74 65 33 44 62 46 72 65 65 28 29 20 77 68 65 6e  te3DbFree() when
2186c 20 74 68 65 20 76 64 62 65 20 69 73 20 64 65 73   the vdbe is des
2186d 74 72 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  troyed..*/.SQLIT
2186e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2186f 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
21870 61 6d 65 28 0a 20 20 56 64 62 65 20 2a 70 2c 20  ame(.  Vdbe *p, 
21871 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21872 20 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20          /* Vdbe 
21873 62 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64  being configured
21874 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20 20   */.  int idx,  
21875 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21876 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
21877 6f 66 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20  of column zName 
21878 61 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20  applies to */.  
21879 69 6e 74 20 76 61 72 2c 20 20 20 20 20 20 20 20  int var,        
2187a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2187b 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43   /* One of the C
2187c 4f 4c 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e  OLNAME_* constan
2187d 74 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ts */.  const ch
2187e 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20  ar *zName,      
2187f 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
21880 74 65 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f  ter to buffer co
21881 6e 74 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f  ntaining name */
21882 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
21883 76 6f 69 64 2a 29 20 20 20 20 20 20 20 20 20 20  void*)          
21884 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61      /* Memory ma
21885 6e 61 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67  nagement strateg
21886 79 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29  y for zName */.)
21887 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65  {.  int rc;.  Me
21888 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61  m *pColName;.  a
21889 73 73 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52  ssert( idx<p->nR
2188a 65 73 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73  esColumn );.  as
2188b 73 65 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d  sert( var<COLNAM
2188c 45 5f 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  E_N );.  if( p->
2188d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2188e 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
2188f 21 7a 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d  !zName || xDel!=
21890 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29  SQLITE_DYNAMIC )
21891 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
21892 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
21893 20 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c   assert( p->aCol
21894 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f  Name!=0 );.  pCo
21895 6c 4e 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f  lName = &(p->aCo
21896 6c 4e 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d  lName[idx+var*p-
21897 3e 6e 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20  >nResColumn]);. 
21898 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
21899 65 4d 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e  eMemSetStr(pColN
2189a 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20  ame, zName, -1, 
2189b 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65  SQLITE_UTF8, xDe
2189c 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  l);.  assert( rc
2189d 21 3d 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c  !=0 || !zName ||
2189e 20 28 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67   (pColName->flag
2189f 73 26 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29  s&MEM_Term)!=0 )
218a0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
218a1 0a 0a 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f  ../*.** A read o
218a2 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  r write transact
218a3 69 6f 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e  ion may or may n
218a4 6f 74 20 62 65 20 61 63 74 69 76 65 20 6f 6e 20  ot be active on 
218a5 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a  database handle.
218a6 2a 2a 20 64 62 2e 20 49 66 20 61 20 74 72 61 6e  ** db. If a tran
218a7 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76  saction is activ
218a8 65 2c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66  e, commit it. If
218a9 20 74 68 65 72 65 20 69 73 20 61 0a 2a 2a 20 77   there is a.** w
218aa 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
218ab 20 73 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74   spanning more t
218ac 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65  han one database
218ad 20 66 69 6c 65 2c 20 74 68 69 73 20 72 6f 75 74   file, this rout
218ae 69 6e 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72  ine.** takes car
218af 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20  e of the master 
218b0 6a 6f 75 72 6e 61 6c 20 74 72 69 63 6b 65 72 79  journal trickery
218b1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
218b2 76 64 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74  vdbeCommit(sqlit
218b3 65 33 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 29  e3 *db, Vdbe *p)
218b4 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
218b5 20 6e 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a   nTrans = 0;  /*
218b6 20 4e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62   Number of datab
218b7 61 73 65 73 20 77 69 74 68 20 61 6e 20 61 63 74  ases with an act
218b8 69 76 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61  ive write-transa
218b9 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72  ction */.  int r
218ba 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
218bb 20 69 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74   int needXcommit
218bc 20 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51   = 0;..#ifdef SQ
218bd 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
218be 4c 54 41 42 4c 45 0a 20 20 2f 2a 20 57 69 74 68  LTABLE.  /* With
218bf 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 73 71   this option, sq
218c0 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 29 20  lite3VtabSync() 
218c1 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65  is defined to be
218c2 20 73 69 6d 70 6c 79 20 0a 20 20 2a 2a 20 53 51   simply .  ** SQ
218c3 4c 49 54 45 5f 4f 4b 20 73 6f 20 70 20 69 73 20  LITE_OK so p is 
218c4 6e 6f 74 20 75 73 65 64 2e 20 0a 20 20 2a 2f 0a  not used. .  */.
218c5 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
218c6 45 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  ER(p);.#endif.. 
218c7 20 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67   /* Before doing
218c8 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20   anything else, 
218c9 63 61 6c 6c 20 74 68 65 20 78 53 79 6e 63 28 29  call the xSync()
218ca 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e   callback for an
218cb 79 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d  y.  ** virtual m
218cc 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 77 72 69  odule tables wri
218cd 74 74 65 6e 20 69 6e 20 74 68 69 73 20 74 72 61  tten in this tra
218ce 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 68  nsaction. This h
218cf 61 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f  as to.  ** be do
218d0 6e 65 20 62 65 66 6f 72 65 20 64 65 74 65 72 6d  ne before determ
218d1 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 61 20  ining whether a 
218d2 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
218d3 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 72 65 71  ile is .  ** req
218d4 75 69 72 65 64 2c 20 61 73 20 61 6e 20 78 53 79  uired, as an xSy
218d5 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61  nc() callback ma
218d6 79 20 61 64 64 20 61 6e 20 61 74 74 61 63 68 65  y add an attache
218d7 64 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  d database.  ** 
218d8 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  to the transacti
218d9 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  on..  */.  rc = 
218da 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28  sqlite3VtabSync(
218db 64 62 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29  db, &p->zErrMsg)
218dc 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f  ;..  /* This loo
218dd 70 20 64 65 74 65 72 6d 69 6e 65 73 20 28 61 29  p determines (a)
218de 20 69 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 68   if the commit h
218df 6f 6f 6b 20 73 68 6f 75 6c 64 20 62 65 20 69 6e  ook should be in
218e0 76 6f 6b 65 64 20 61 6e 64 0a 20 20 2a 2a 20 28  voked and.  ** (
218e1 62 29 20 68 6f 77 20 6d 61 6e 79 20 64 61 74 61  b) how many data
218e2 62 61 73 65 20 66 69 6c 65 73 20 68 61 76 65 20  base files have 
218e3 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73  open write trans
218e4 61 63 74 69 6f 6e 73 2c 20 6e 6f 74 20 0a 20 20  actions, not .  
218e5 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65  ** including the
218e6 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e 20   temp database. 
218e7 28 62 29 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  (b) is important
218e8 20 62 65 63 61 75 73 65 20 69 66 20 6d 6f 72 65   because if more
218e9 20 74 68 61 6e 20 0a 20 20 2a 2a 20 6f 6e 65 20   than .  ** one 
218ea 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  database file ha
218eb 73 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 20  s an open write 
218ec 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 6d  transaction, a m
218ed 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 20 20  aster journal.  
218ee 2a 2a 20 66 69 6c 65 20 69 73 20 72 65 71 75 69  ** file is requi
218ef 72 65 64 20 66 6f 72 20 61 6e 20 61 74 6f 6d 69  red for an atomi
218f0 63 20 63 6f 6d 6d 69 74 2e 0a 20 20 2a 2f 20 0a  c commit..  */ .
218f1 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
218f2 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62  QLITE_OK && i<db
218f3 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20  ->nDb; i++){ .  
218f4 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64    Btree *pBt = d
218f5 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
218f6 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74     if( sqlite3Bt
218f7 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74  reeIsInTrans(pBt
218f8 29 20 29 7b 0a 20 20 20 20 20 20 6e 65 65 64 58  ) ){.      needX
218f9 63 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20  commit = 1;.    
218fa 20 20 69 66 28 20 69 21 3d 31 20 29 20 6e 54 72    if( i!=1 ) nTr
218fb 61 6e 73 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c  ans++;.      sql
218fc 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
218fd 42 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  Bt);.      rc = 
218fe 73 71 6c 69 74 65 33 50 61 67 65 72 45 78 63 6c  sqlite3PagerExcl
218ff 75 73 69 76 65 4c 6f 63 6b 28 73 71 6c 69 74 65  usiveLock(sqlite
21900 33 42 74 72 65 65 50 61 67 65 72 28 70 42 74 29  3BtreePager(pBt)
21901 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21902 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 29 3b  BtreeLeave(pBt);
21903 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
21904 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
21905 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
21906 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
21907 65 72 65 20 61 72 65 20 61 6e 79 20 77 72 69 74  ere are any writ
21908 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61  e-transactions a
21909 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 20 74 68  t all, invoke th
2190a 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 2a 2f  e commit hook */
2190b 0a 20 20 69 66 28 20 6e 65 65 64 58 63 6f 6d 6d  .  if( needXcomm
2190c 69 74 20 26 26 20 64 62 2d 3e 78 43 6f 6d 6d 69  it && db->xCommi
2190d 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20  tCallback ){.   
2190e 20 72 63 20 3d 20 64 62 2d 3e 78 43 6f 6d 6d 69   rc = db->xCommi
2190f 74 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 43  tCallback(db->pC
21910 6f 6d 6d 69 74 41 72 67 29 3b 0a 20 20 20 20 69  ommitArg);.    i
21911 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72  f( rc ){.      r
21912 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e  eturn SQLITE_CON
21913 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 7d 0a 20  STRAINT;.    }. 
21914 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69 6d   }..  /* The sim
21915 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f 20 6d 6f  ple case - no mo
21916 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61  re than one data
21917 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 63  base file (not c
21918 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20 20 2a 2a  ounting the.  **
21919 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 29 20   TEMP database) 
2191a 68 61 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f  has a transactio
2191b 6e 20 61 63 74 69 76 65 2e 20 20 20 54 68 65 72  n active.   Ther
2191c 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72  e is no need for
2191d 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73 74 65 72   the.  ** master
2191e 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20  -journal..  **. 
2191f 20 2a 2a 20 49 66 20 74 68 65 20 72 65 74 75 72   ** If the retur
21920 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74  n value of sqlit
21921 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61  e3BtreeGetFilena
21922 6d 65 28 29 20 69 73 20 61 20 7a 65 72 6f 20 6c  me() is a zero l
21923 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74 72 69 6e  ength.  ** strin
21924 67 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20  g, it means the 
21925 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 73  main database is
21926 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20 61 20 74   :memory: or a t
21927 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e 20 0a 20  emp file.  In . 
21928 20 2a 2a 20 74 68 61 74 20 63 61 73 65 20 77 65   ** that case we
21929 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20   do not support 
2192a 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 69 6c  atomic multi-fil
2192b 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 73  e commits, so us
2192c 65 20 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70  e the .  ** simp
2192d 6c 65 20 63 61 73 65 20 74 68 65 6e 20 74 6f 6f  le case then too
2192e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d  ..  */.  if( 0==
2192f 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
21930 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46  sqlite3BtreeGetF
21931 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b  ilename(db->aDb[
21932 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c 7c 20 6e  0].pBt)).   || n
21933 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b 0a 20 20  Trans<=1.  ){.  
21934 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
21935 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62  QLITE_OK && i<db
21936 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
21937 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
21938 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
21939 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b        if( pBt ){
2193a 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
2193b 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
2193c 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 30 29  PhaseOne(pBt, 0)
2193d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2193e 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63  .    /* Do the c
2193f 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61 6c  ommit only if al
21940 6c 20 64 61 74 61 62 61 73 65 73 20 73 75 63 63  l databases succ
21941 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74  essfully complet
21942 65 20 70 68 61 73 65 20 31 2e 20 0a 20 20 20 20  e phase 1. .    
21943 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 65  ** If one of the
21944 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73   BtreeCommitPhas
21945 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 66 61 69  eOne() calls fai
21946 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74  ls, this indicat
21947 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f 20  es an.    ** IO 
21948 65 72 72 6f 72 20 77 68 69 6c 65 20 64 65 6c 65  error while dele
21949 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69  ting or truncati
2194a 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ng a journal fil
2194b 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c  e. It is unlikel
2194c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 20 63 6f  y,.    ** but co
2194d 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 6e 20 74  uld happen. In t
2194e 68 69 73 20 63 61 73 65 20 61 62 61 6e 64 6f 6e  his case abandon
2194f 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 20   processing and 
21950 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72  return the error
21951 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
21952 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  (i=0; rc==SQLITE
21953 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62  _OK && i<db->nDb
21954 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74  ; i++){.      Bt
21955 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61  ree *pBt = db->a
21956 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20  Db[i].pBt;.     
21957 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20   if( pBt ){.    
21958 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
21959 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
2195a 54 77 6f 28 70 42 74 2c 20 30 29 3b 0a 20 20 20  Two(pBt, 0);.   
2195b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
2195c 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
2195d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2195e 33 56 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b  3VtabCommit(db);
2195f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
21960 20 54 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73   The complex cas
21961 65 20 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d  e - There is a m
21962 75 6c 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d  ulti-file write-
21963 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69  transaction acti
21964 76 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65  ve..  ** This re
21965 71 75 69 72 65 73 20 61 20 6d 61 73 74 65 72 20  quires a master 
21966 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20  journal file to 
21967 65 6e 73 75 72 65 20 74 68 65 20 74 72 61 6e 73  ensure the trans
21968 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63  action is.  ** c
21969 6f 6d 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c  ommitted atomicl
2196a 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  y..  */.#ifndef 
2196b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
2196c 49 4f 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 73  IO.  else{.    s
2196d 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
2196e 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20   = db->pVfs;.   
2196f 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20   int needSync = 
21970 30 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61  0;.    char *zMa
21971 73 74 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46  ster = 0;   /* F
21972 69 6c 65 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65  ile-name for the
21973 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
21974 2a 2f 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73  */.    char cons
21975 74 20 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73  t *zMainFile = s
21976 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69  qlite3BtreeGetFi
21977 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30  lename(db->aDb[0
21978 5d 2e 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69  ].pBt);.    sqli
21979 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65  te3_file *pMaste
2197a 72 20 3d 20 30 3b 0a 20 20 20 20 69 36 34 20 6f  r = 0;.    i64 o
2197b 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 69  ffset = 0;.    i
2197c 6e 74 20 72 65 73 3b 0a 20 20 20 20 69 6e 74 20  nt res;.    int 
2197d 72 65 74 72 79 43 6f 75 6e 74 20 3d 20 30 3b 0a  retryCount = 0;.
2197e 20 20 20 20 69 6e 74 20 6e 4d 61 69 6e 46 69 6c      int nMainFil
2197f 65 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63  e;..    /* Selec
21980 74 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  t a master journ
21981 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a  al file name */.
21982 20 20 20 20 6e 4d 61 69 6e 46 69 6c 65 20 3d 20      nMainFile = 
21983 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
21984 7a 4d 61 69 6e 46 69 6c 65 29 3b 0a 20 20 20 20  zMainFile);.    
21985 7a 4d 61 73 74 65 72 20 3d 20 73 71 6c 69 74 65  zMaster = sqlite
21986 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73  3MPrintf(db, "%s
21987 2d 6d 6a 58 58 58 58 58 58 39 58 58 7a 22 2c 20  -mjXXXXXX9XXz", 
21988 7a 4d 61 69 6e 46 69 6c 65 29 3b 0a 20 20 20 20  zMainFile);.    
21989 69 66 28 20 7a 4d 61 73 74 65 72 3d 3d 30 20 29  if( zMaster==0 )
2198a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
2198b 4f 4d 45 4d 3b 0a 20 20 20 20 64 6f 20 7b 0a 20  OMEM;.    do {. 
2198c 20 20 20 20 20 75 33 32 20 69 52 61 6e 64 6f 6d       u32 iRandom
2198d 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65 74 72  ;.      if( retr
2198e 79 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20  yCount ){.      
2198f 20 20 69 66 28 20 72 65 74 72 79 43 6f 75 6e 74    if( retryCount
21990 3e 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20  >100 ){.        
21991 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51    sqlite3_log(SQ
21992 4c 49 54 45 5f 46 55 4c 4c 2c 20 22 4d 4a 20 64  LITE_FULL, "MJ d
21993 65 6c 65 74 65 3a 20 25 73 22 2c 20 7a 4d 61 73  elete: %s", zMas
21994 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ter);.          
21995 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
21996 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30  pVfs, zMaster, 0
21997 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  );.          bre
21998 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ak;.        }els
21999 65 20 69 66 28 20 72 65 74 72 79 43 6f 75 6e 74  e if( retryCount
2199a 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==1 ){.         
2199b 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c   sqlite3_log(SQL
2199c 49 54 45 5f 46 55 4c 4c 2c 20 22 4d 4a 20 63 6f  ITE_FULL, "MJ co
2199d 6c 6c 69 64 65 3a 20 25 73 22 2c 20 7a 4d 61 73  llide: %s", zMas
2199e 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ter);.        }.
2199f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
219a0 74 72 79 43 6f 75 6e 74 2b 2b 3b 0a 20 20 20 20  tryCount++;.    
219a1 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d    sqlite3_random
219a2 6e 65 73 73 28 73 69 7a 65 6f 66 28 69 52 61 6e  ness(sizeof(iRan
219a3 64 6f 6d 29 2c 20 26 69 52 61 6e 64 6f 6d 29 3b  dom), &iRandom);
219a4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
219a5 6e 70 72 69 6e 74 66 28 31 33 2c 20 26 7a 4d 61  nprintf(13, &zMa
219a6 73 74 65 72 5b 6e 4d 61 69 6e 46 69 6c 65 5d 2c  ster[nMainFile],
219a7 20 22 2d 6d 6a 25 30 36 58 39 25 30 32 58 22 2c   "-mj%06X9%02X",
219a8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
219a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
219aa 28 69 52 61 6e 64 6f 6d 3e 3e 38 29 26 30 78 66  (iRandom>>8)&0xf
219ab 66 66 66 66 66 2c 20 69 52 61 6e 64 6f 6d 26 30  fffff, iRandom&0
219ac 78 66 66 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54  xff);.      /* T
219ad 68 65 20 61 6e 74 69 70 65 6e 75 6c 74 69 6d 61  he antipenultima
219ae 74 65 20 63 68 61 72 61 63 74 65 72 20 6f 66 20  te character of 
219af 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
219b0 61 6c 20 6e 61 6d 65 20 6d 75 73 74 0a 20 20 20  al name must.   
219b1 20 20 20 2a 2a 20 62 65 20 22 39 22 20 74 6f 20     ** be "9" to 
219b2 61 76 6f 69 64 20 6e 61 6d 65 20 63 6f 6c 6c 69  avoid name colli
219b3 73 69 6f 6e 73 20 77 68 65 6e 20 75 73 69 6e 67  sions when using
219b4 20 38 2b 33 20 66 69 6c 65 6e 61 6d 65 73 2e 20   8+3 filenames. 
219b5 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
219b6 20 7a 4d 61 73 74 65 72 5b 73 71 6c 69 74 65 33   zMaster[sqlite3
219b7 53 74 72 6c 65 6e 33 30 28 7a 4d 61 73 74 65 72  Strlen30(zMaster
219b8 29 2d 33 5d 3d 3d 27 39 27 20 29 3b 0a 20 20 20  )-3]=='9' );.   
219b9 20 20 20 73 71 6c 69 74 65 33 46 69 6c 65 53 75     sqlite3FileSu
219ba 66 66 69 78 33 28 7a 4d 61 69 6e 46 69 6c 65 2c  ffix3(zMainFile,
219bb 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20   zMaster);.     
219bc 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41   rc = sqlite3OsA
219bd 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73  ccess(pVfs, zMas
219be 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45  ter, SQLITE_ACCE
219bf 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29  SS_EXISTS, &res)
219c0 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63  ;.    }while( rc
219c1 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72  ==SQLITE_OK && r
219c2 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63  es );.    if( rc
219c3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
219c4 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65       /* Open the
219c5 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e   master journal.
219c6 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73   */.      rc = s
219c7 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c  qlite3OsOpenMall
219c8 6f 63 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72  oc(pVfs, zMaster
219c9 2c 20 26 70 4d 61 73 74 65 72 2c 20 0a 20 20 20  , &pMaster, .   
219ca 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
219cb 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c  EN_READWRITE|SQL
219cc 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c  ITE_OPEN_CREATE|
219cd 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  .          SQLIT
219ce 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
219cf 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53  |SQLITE_OPEN_MAS
219d0 54 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30 0a 20  TER_JOURNAL, 0. 
219d1 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20       );.    }.  
219d2 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
219d3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
219d4 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
219d5 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72  Master);.      r
219d6 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
219d7 20 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74   .    /* Write t
219d8 68 65 20 6e 61 6d 65 20 6f 66 20 65 61 63 68 20  he name of each 
219d9 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e  database file in
219da 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
219db 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 20   into the new.  
219dc 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72    ** master jour
219dd 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 6e 20  nal file. If an 
219de 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 74 20  error occurs at 
219df 74 68 69 73 20 70 6f 69 6e 74 20 63 6c 6f 73 65  this point close
219e0 0a 20 20 20 20 2a 2a 20 61 6e 64 20 64 65 6c 65  .    ** and dele
219e1 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  te the master jo
219e2 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 6c 6c 20  urnal file. All 
219e3 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 6a  the individual j
219e4 6f 75 72 6e 61 6c 20 66 69 6c 65 73 0a 20 20 20  ournal files.   
219e5 20 2a 2a 20 73 74 69 6c 6c 20 68 61 76 65 20 27   ** still have '
219e6 6e 75 6c 6c 27 20 61 73 20 74 68 65 20 6d 61 73  null' as the mas
219e7 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e  ter journal poin
219e8 74 65 72 2c 20 73 6f 20 74 68 65 79 20 77 69 6c  ter, so they wil
219e9 6c 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20 62 61  l roll.    ** ba
219ea 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79  ck independently
219eb 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63   if a failure oc
219ec 63 75 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  curs..    */.   
219ed 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
219ee 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
219ef 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
219f0 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
219f1 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42      if( sqlite3B
219f2 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42  treeIsInTrans(pB
219f3 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  t) ){.        ch
219f4 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c 65 20  ar const *zFile 
219f5 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
219f6 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 42 74  tJournalname(pBt
219f7 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  );.        if( z
219f8 46 69 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  File==0 ){.     
219f9 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 20 20       continue;  
219fa 2f 2a 20 49 67 6e 6f 72 65 20 54 45 4d 50 20 61  /* Ignore TEMP a
219fb 6e 64 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61  nd :memory: data
219fc 62 61 73 65 73 20 2a 2f 0a 20 20 20 20 20 20 20  bases */.       
219fd 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72   }.        asser
219fe 74 28 20 7a 46 69 6c 65 5b 30 5d 21 3d 30 20 29  t( zFile[0]!=0 )
219ff 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 6e  ;.        if( !n
21a00 65 65 64 53 79 6e 63 20 26 26 20 21 73 71 6c 69  eedSync && !sqli
21a01 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 61  te3BtreeSyncDisa
21a02 62 6c 65 64 28 70 42 74 29 20 29 7b 0a 20 20 20  bled(pBt) ){.   
21a03 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20         needSync 
21a04 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 1;.        }. 
21a05 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
21a06 74 65 33 4f 73 57 72 69 74 65 28 70 4d 61 73 74  te3OsWrite(pMast
21a07 65 72 2c 20 7a 46 69 6c 65 2c 20 73 71 6c 69 74  er, zFile, sqlit
21a08 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65  e3Strlen30(zFile
21a09 29 2b 31 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20  )+1, offset);.  
21a0a 20 20 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20        offset += 
21a0b 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
21a0c 7a 46 69 6c 65 29 2b 31 3b 0a 20 20 20 20 20 20  zFile)+1;.      
21a0d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
21a0e 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
21a0f 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46   sqlite3OsCloseF
21a10 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ree(pMaster);.  
21a11 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f          sqlite3O
21a12 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d  sDelete(pVfs, zM
21a13 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 20  aster, 0);.     
21a14 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
21a15 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b  ee(db, zMaster);
21a16 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
21a17 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
21a18 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
21a19 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6d     /* Sync the m
21a1a 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
21a1b 6c 65 2e 20 49 66 20 74 68 65 20 49 4f 43 41 50  le. If the IOCAP
21a1c 5f 53 45 51 55 45 4e 54 49 41 4c 20 64 65 76 69  _SEQUENTIAL devi
21a1d 63 65 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20 69  ce.    ** flag i
21a1e 73 20 73 65 74 20 74 68 69 73 20 69 73 20 6e 6f  s set this is no
21a1f 74 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20  t required..    
21a20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 65 64 53  */.    if( needS
21a21 79 6e 63 20 0a 20 20 20 20 20 26 26 20 30 3d 3d  ync .     && 0==
21a22 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65  (sqlite3OsDevice
21a23 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
21a24 70 4d 61 73 74 65 72 29 26 53 51 4c 49 54 45 5f  pMaster)&SQLITE_
21a25 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c  IOCAP_SEQUENTIAL
21a26 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54 45  ).     && SQLITE
21a27 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK!=(rc = sqlit
21a28 65 33 4f 73 53 79 6e 63 28 70 4d 61 73 74 65 72  e3OsSync(pMaster
21a29 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f  , SQLITE_SYNC_NO
21a2a 52 4d 41 4c 29 29 0a 20 20 20 20 29 7b 0a 20 20  RMAL)).    ){.  
21a2b 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
21a2c 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b  seFree(pMaster);
21a2d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
21a2e 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61  Delete(pVfs, zMa
21a2f 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  ster, 0);.      
21a30 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
21a31 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  , zMaster);.    
21a32 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
21a33 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20   }..    /* Sync 
21a34 61 6c 6c 20 74 68 65 20 64 62 20 66 69 6c 65 73  all the db files
21a35 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68 65   involved in the
21a36 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68   transaction. Th
21a37 65 20 73 61 6d 65 20 63 61 6c 6c 0a 20 20 20 20  e same call.    
21a38 2a 2a 20 73 65 74 73 20 74 68 65 20 6d 61 73 74  ** sets the mast
21a39 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74  er journal point
21a3a 65 72 20 69 6e 20 65 61 63 68 20 69 6e 64 69 76  er in each indiv
21a3b 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 2e 20 49  idual journal. I
21a3c 66 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f  f.    ** an erro
21a3d 72 20 6f 63 63 75 72 73 20 68 65 72 65 2c 20 64  r occurs here, d
21a3e 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65  o not delete the
21a3f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
21a40 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  file..    **.   
21a41 20 2a 2a 20 49 66 20 74 68 65 20 65 72 72 6f 72   ** If the error
21a42 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74   occurs during t
21a43 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f  he first call to
21a44 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42  .    ** sqlite3B
21a45 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f  treeCommitPhaseO
21a46 6e 65 28 29 2c 20 74 68 65 6e 20 74 68 65 72 65  ne(), then there
21a47 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 68 61   is a chance tha
21a48 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 6d 61 73  t the.    ** mas
21a49 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
21a4a 20 77 69 6c 6c 20 62 65 20 6f 72 70 68 61 6e 65   will be orphane
21a4b 64 2e 20 42 75 74 20 77 65 20 63 61 6e 6e 6f 74  d. But we cannot
21a4c 20 64 65 6c 65 74 65 20 69 74 2c 0a 20 20 20 20   delete it,.    
21a4d 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 65 20 6d  ** in case the m
21a4e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
21a4f 6c 65 20 6e 61 6d 65 20 77 61 73 20 77 72 69 74  le name was writ
21a50 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ten into the jou
21a51 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65  rnal.    ** file
21a52 20 62 65 66 6f 72 65 20 74 68 65 20 66 61 69 6c   before the fail
21a53 75 72 65 20 6f 63 63 75 72 72 65 64 2e 0a 20 20  ure occurred..  
21a54 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30    */.    for(i=0
21a55 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
21a56 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  && i<db->nDb; i+
21a57 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72 65 65  +){ .      Btree
21a58 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b   *pBt = db->aDb[
21a59 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66  i].pBt;.      if
21a5a 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20  ( pBt ){.       
21a5b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
21a5c 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
21a5d 28 70 42 74 2c 20 7a 4d 61 73 74 65 72 29 3b 0a  (pBt, zMaster);.
21a5e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
21a5f 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
21a60 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20  Free(pMaster);. 
21a61 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
21a62 51 4c 49 54 45 5f 42 55 53 59 20 29 3b 0a 20 20  QLITE_BUSY );.  
21a63 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
21a64 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
21a65 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
21a66 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72  Master);.      r
21a67 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
21a68 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74  .    /* Delete t
21a69 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
21a6a 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 63 6f 6d  l file. This com
21a6b 6d 69 74 73 20 74 68 65 20 74 72 61 6e 73 61 63  mits the transac
21a6c 74 69 6f 6e 2e 20 41 66 74 65 72 0a 20 20 20 20  tion. After.    
21a6d 2a 2a 20 64 6f 69 6e 67 20 74 68 69 73 20 74 68  ** doing this th
21a6e 65 20 64 69 72 65 63 74 6f 72 79 20 69 73 20 73  e directory is s
21a6f 79 6e 63 65 64 20 61 67 61 69 6e 20 62 65 66 6f  ynced again befo
21a70 72 65 20 61 6e 79 20 69 6e 64 69 76 69 64 75 61  re any individua
21a71 6c 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63  l.    ** transac
21a72 74 69 6f 6e 20 66 69 6c 65 73 20 61 72 65 20 64  tion files are d
21a73 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  eleted..    */. 
21a74 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
21a75 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d  sDelete(pVfs, zM
21a76 61 73 74 65 72 2c 20 31 29 3b 0a 20 20 20 20 73  aster, 1);.    s
21a77 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
21a78 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 7a   zMaster);.    z
21a79 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Master = 0;.    
21a7a 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
21a7b 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
21a7c 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 20 66 69 6c  ..    /* All fil
21a7d 65 73 20 61 6e 64 20 64 69 72 65 63 74 6f 72 69  es and directori
21a7e 65 73 20 68 61 76 65 20 61 6c 72 65 61 64 79 20  es have already 
21a7f 62 65 65 6e 20 73 79 6e 63 65 64 2c 20 73 6f 20  been synced, so 
21a80 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20  the following.  
21a81 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71    ** calls to sq
21a82 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
21a83 50 68 61 73 65 54 77 6f 28 29 20 61 72 65 20 6f  PhaseTwo() are o
21a84 6e 6c 79 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65  nly closing file
21a85 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20 64 65 6c  s and.    ** del
21a86 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 74  eting or truncat
21a87 69 6e 67 20 6a 6f 75 72 6e 61 6c 73 2e 20 49 66  ing journals. If
21a88 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20   something goes 
21a89 77 72 6f 6e 67 20 77 68 69 6c 65 0a 20 20 20 20  wrong while.    
21a8a 2a 2a 20 74 68 69 73 20 69 73 20 68 61 70 70 65  ** this is happe
21a8b 6e 69 6e 67 20 77 65 20 64 6f 6e 27 74 20 72 65  ning we don't re
21a8c 61 6c 6c 79 20 63 61 72 65 2e 20 54 68 65 20 69  ally care. The i
21a8d 6e 74 65 67 72 69 74 79 20 6f 66 20 74 68 65 0a  ntegrity of the.
21a8e 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69      ** transacti
21a8f 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20 67 75  on is already gu
21a90 61 72 61 6e 74 65 65 64 2c 20 62 75 74 20 73 6f  aranteed, but so
21a91 6d 65 20 73 74 72 61 79 20 27 63 6f 6c 64 27 20  me stray 'cold' 
21a92 6a 6f 75 72 6e 61 6c 73 0a 20 20 20 20 2a 2a 20  journals.    ** 
21a93 6d 61 79 20 62 65 20 6c 79 69 6e 67 20 61 72 6f  may be lying aro
21a94 75 6e 64 2e 20 52 65 74 75 72 6e 69 6e 67 20 61  und. Returning a
21a95 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 77 6f 6e  n error code won
21a96 27 74 20 68 65 6c 70 20 6d 61 74 74 65 72 73 2e  't help matters.
21a97 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 69 73 61  .    */.    disa
21a98 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
21a99 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 20 20 73  _errors();.    s
21a9a 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
21a9b 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 66  nMalloc();.    f
21a9c 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
21a9d 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20  b; i++){ .      
21a9e 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d  Btree *pBt = db-
21a9f 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20  >aDb[i].pBt;.   
21aa0 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20     if( pBt ){.  
21aa1 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
21aa2 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  eeCommitPhaseTwo
21aa3 28 70 42 74 2c 20 31 29 3b 0a 20 20 20 20 20 20  (pBt, 1);.      
21aa4 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
21aa5 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
21aa6 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62 6c 65  oc();.    enable
21aa7 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
21aa8 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73 71 6c  rors();..    sql
21aa9 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 64  ite3VtabCommit(d
21aaa 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  b);.  }.#endif..
21aab 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
21aac 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  /* .** This rout
21aad 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61 74 20  ine checks that 
21aae 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63 74 69  the sqlite3.acti
21aaf 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e 74 20  veVdbeCnt count 
21ab0 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 74 63  variable.** matc
21ab1 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  hes the number o
21ab2 66 20 76 64 62 65 27 73 20 69 6e 20 74 68 65 20  f vdbe's in the 
21ab3 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70 56 64  list sqlite3.pVd
21ab4 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63  be that are.** c
21ab5 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65 2e  urrently active.
21ab6 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   An assertion fa
21ab7 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f 20 63  ils if the two c
21ab8 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 74  ounts do not mat
21ab9 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  ch..** This is a
21aba 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c 66 2d  n internal self-
21abb 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69 74 20  check only - it 
21abc 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65 6e 74  is not an essent
21abd 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a  ial processing.*
21abe 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 68  * step..**.** Th
21abf 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  is is a no-op if
21ac0 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e   NDEBUG is defin
21ac1 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e  ed..*/.#ifndef N
21ac2 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 6f 69  DEBUG.static voi
21ac3 64 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62  d checkActiveVdb
21ac4 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62  eCnt(sqlite3 *db
21ac5 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20  ){.  Vdbe *p;.  
21ac6 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69  int cnt = 0;.  i
21ac7 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b 0a 20  nt nWrite = 0;. 
21ac8 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 0a   p = db->pVdbe;.
21ac9 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20    while( p ){.  
21aca 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d    if( p->magic==
21acb 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26  VDBE_MAGIC_RUN &
21acc 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20  & p->pc>=0 ){.  
21acd 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20      cnt++;.     
21ace 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79   if( p->readOnly
21acf 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b 3b 0a  ==0 ) nWrite++;.
21ad0 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d      }.    p = p-
21ad1 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 61 73  >pNext;.  }.  as
21ad2 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d 3e 61  sert( cnt==db->a
21ad3 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 3b 0a  ctiveVdbeCnt );.
21ad4 20 20 61 73 73 65 72 74 28 20 6e 57 72 69 74 65    assert( nWrite
21ad5 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43  ==db->writeVdbeC
21ad6 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64  nt );.}.#else.#d
21ad7 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74 69 76  efine checkActiv
21ad8 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65 6e 64  eVdbeCnt(x).#end
21ad9 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  if../*.** If the
21ada 20 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20   Vdbe passed as 
21adb 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
21adc 6e 74 20 6f 70 65 6e 65 64 20 61 20 73 74 61 74  nt opened a stat
21add 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f  ement-transactio
21ade 6e 2c 0a 2a 2a 20 63 6c 6f 73 65 20 69 74 20 6e  n,.** close it n
21adf 6f 77 2e 20 41 72 67 75 6d 65 6e 74 20 65 4f 70  ow. Argument eOp
21ae0 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
21ae1 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
21ae2 43 4b 20 6f 72 0a 2a 2a 20 53 41 56 45 50 4f 49  CK or.** SAVEPOI
21ae3 4e 54 5f 52 45 4c 45 41 53 45 2e 20 49 66 20 69  NT_RELEASE. If i
21ae4 74 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52  t is SAVEPOINT_R
21ae5 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 74 68  OLLBACK, then th
21ae6 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74  e statement.** t
21ae7 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f  ransaction is ro
21ae8 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 65 4f  lled back. If eO
21ae9 70 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52  p is SAVEPOINT_R
21aea 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 74 68 65  ELEASE, then the
21aeb 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74   .** statement t
21aec 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
21aed 6d 6d 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  mmtted..**.** If
21aee 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   an IO error occ
21aef 75 72 73 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49  urs, an SQLITE_I
21af0 4f 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63  OERR_XXX error c
21af1 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
21af2 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 53   .** Otherwise S
21af3 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c  QLITE_OK..*/.SQL
21af4 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
21af5 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65  sqlite3VdbeClose
21af6 53 74 61 74 65 6d 65 6e 74 28 56 64 62 65 20 2a  Statement(Vdbe *
21af7 70 2c 20 69 6e 74 20 65 4f 70 29 7b 0a 20 20 73  p, int eOp){.  s
21af8 71 6c 69 74 65 33 20 2a 63 6f 6e 73 74 20 64 62  qlite3 *const db
21af9 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20   = p->db;.  int 
21afa 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
21afb 0a 20 20 2f 2a 20 49 66 20 70 2d 3e 69 53 74 61  .  /* If p->iSta
21afc 74 65 6d 65 6e 74 20 69 73 20 67 72 65 61 74 65  tement is greate
21afd 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65  r than zero, the
21afe 6e 20 74 68 69 73 20 56 64 62 65 20 6f 70 65 6e  n this Vdbe open
21aff 65 64 20 61 20 0a 20 20 2a 2a 20 73 74 61 74 65  ed a .  ** state
21b00 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
21b01 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
21b02 63 6c 6f 73 65 64 20 68 65 72 65 2e 20 54 68 65  closed here. The
21b03 20 6f 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 0a   only exception.
21b04 20 20 2a 2a 20 69 73 20 74 68 61 74 20 61 6e 20    ** is that an 
21b05 49 4f 20 65 72 72 6f 72 20 6d 61 79 20 68 61 76  IO error may hav
21b06 65 20 6f 63 63 75 72 65 64 2c 20 63 61 75 73 69  e occured, causi
21b07 6e 67 20 61 6e 20 65 6d 65 72 67 65 6e 63 79 20  ng an emergency 
21b08 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 2a 2a 20 49  rollback..  ** I
21b09 6e 20 74 68 69 73 20 63 61 73 65 20 28 64 62 2d  n this case (db-
21b0a 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 29 2c  >nStatement==0),
21b0b 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f   and there is no
21b0c 74 68 69 6e 67 20 74 6f 20 64 6f 2e 0a 20 20 2a  thing to do..  *
21b0d 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6e 53 74 61  /.  if( db->nSta
21b0e 74 65 6d 65 6e 74 20 26 26 20 70 2d 3e 69 53 74  tement && p->iSt
21b0f 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 69  atement ){.    i
21b10 6e 74 20 69 3b 0a 20 20 20 20 63 6f 6e 73 74 20  nt i;.    const 
21b11 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 20 3d  int iSavepoint =
21b12 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 2d 31   p->iStatement-1
21b13 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 65  ;..    assert( e
21b14 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f  Op==SAVEPOINT_RO
21b15 4c 4c 42 41 43 4b 20 7c 7c 20 65 4f 70 3d 3d 53  LLBACK || eOp==S
21b16 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
21b17 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  );.    assert( d
21b18 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3e 30 20  b->nStatement>0 
21b19 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
21b1a 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 28 64  ->iStatement==(d
21b1b 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2b 64 62  b->nStatement+db
21b1c 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 20 29 3b  ->nSavepoint) );
21b1d 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ..    for(i=0; i
21b1e 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20  <db->nDb; i++){ 
21b1f 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 20 3d  .      int rc2 =
21b20 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
21b21 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64    Btree *pBt = d
21b22 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
21b23 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a       if( pBt ){.
21b24 20 20 20 20 20 20 20 20 69 66 28 20 65 4f 70 3d          if( eOp=
21b25 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42  =SAVEPOINT_ROLLB
21b26 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ACK ){.         
21b27 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 42 74   rc2 = sqlite3Bt
21b28 72 65 65 53 61 76 65 70 6f 69 6e 74 28 70 42 74  reeSavepoint(pBt
21b29 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  , SAVEPOINT_ROLL
21b2a 42 41 43 4b 2c 20 69 53 61 76 65 70 6f 69 6e 74  BACK, iSavepoint
21b2b 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
21b2c 20 20 20 20 20 69 66 28 20 72 63 32 3d 3d 53 51       if( rc2==SQ
21b2d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
21b2e 20 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74       rc2 = sqlit
21b2f 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74  e3BtreeSavepoint
21b30 28 70 42 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f  (pBt, SAVEPOINT_
21b31 52 45 4c 45 41 53 45 2c 20 69 53 61 76 65 70 6f  RELEASE, iSavepo
21b32 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  int);.        }.
21b33 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
21b34 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
21b35 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b         rc = rc2;
21b36 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
21b37 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d   }.    }.    db-
21b38 3e 6e 53 74 61 74 65 6d 65 6e 74 2d 2d 3b 0a 20  >nStatement--;. 
21b39 20 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74     p->iStatement
21b3a 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 72   = 0;..    if( r
21b3b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
21b3c 20 20 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 53        if( eOp==S
21b3d 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
21b3e 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
21b3f 3d 20 73 71 6c 69 74 65 33 56 74 61 62 53 61 76  = sqlite3VtabSav
21b40 65 70 6f 69 6e 74 28 64 62 2c 20 53 41 56 45 50  epoint(db, SAVEP
21b41 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 69  OINT_ROLLBACK, i
21b42 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20  Savepoint);.    
21b43 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63    }.      if( rc
21b44 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
21b45 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
21b46 74 65 33 56 74 61 62 53 61 76 65 70 6f 69 6e 74  te3VtabSavepoint
21b47 28 64 62 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52  (db, SAVEPOINT_R
21b48 45 4c 45 41 53 45 2c 20 69 53 61 76 65 70 6f 69  ELEASE, iSavepoi
21b49 6e 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nt);.      }.   
21b4a 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   }..    /* If th
21b4b 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  e statement tran
21b4c 73 61 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67  saction is being
21b4d 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 61 6c   rolled back, al
21b4e 73 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 0a  so restore the .
21b4f 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
21b50 68 61 6e 64 6c 65 73 20 64 65 66 65 72 72 65 64  handles deferred
21b51 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e   constraint coun
21b52 74 65 72 20 74 6f 20 74 68 65 20 76 61 6c 75 65  ter to the value
21b53 20 69 74 20 68 61 64 20 77 68 65 6e 20 0a 20 20   it had when .  
21b54 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65    ** the stateme
21b55 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  nt transaction w
21b56 61 73 20 6f 70 65 6e 65 64 2e 20 20 2a 2f 0a 20  as opened.  */. 
21b57 20 20 20 69 66 28 20 65 4f 70 3d 3d 53 41 56 45     if( eOp==SAVE
21b58 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29  POINT_ROLLBACK )
21b59 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6e 44 65 66  {.      db->nDef
21b5a 65 72 72 65 64 43 6f 6e 73 20 3d 20 70 2d 3e 6e  erredCons = p->n
21b5b 53 74 6d 74 44 65 66 43 6f 6e 73 3b 0a 20 20 20  StmtDefCons;.   
21b5c 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
21b5d 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
21b5e 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
21b5f 6c 6c 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e  lled when a tran
21b60 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 62  saction opened b
21b61 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a  y the database .
21b62 2a 2a 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69  ** handle associ
21b63 61 74 65 64 20 77 69 74 68 20 74 68 65 20 56 4d  ated with the VM
21b64 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72   passed as an ar
21b65 67 75 6d 65 6e 74 20 69 73 20 61 62 6f 75 74 20  gument is about 
21b66 74 6f 20 62 65 20 0a 2a 2a 20 63 6f 6d 6d 69 74  to be .** commit
21b67 74 65 64 2e 20 49 66 20 74 68 65 72 65 20 61 72  ted. If there ar
21b68 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 65  e outstanding de
21b69 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e 20 6b  ferred foreign k
21b6a 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a  ey constraint.**
21b6b 20 76 69 6f 6c 61 74 69 6f 6e 73 2c 20 72 65 74   violations, ret
21b6c 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
21b6d 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c  . Otherwise, SQL
21b6e 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  ITE_OK..**.** If
21b6f 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74   there are outst
21b70 61 6e 64 69 6e 67 20 46 4b 20 76 69 6f 6c 61 74  anding FK violat
21b71 69 6f 6e 73 20 61 6e 64 20 74 68 69 73 20 66 75  ions and this fu
21b72 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 0a  nction returns .
21b73 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c  ** SQLITE_ERROR,
21b74 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20   set the result 
21b75 6f 66 20 74 68 65 20 56 4d 20 74 6f 20 53 51 4c  of the VM to SQL
21b76 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61  ITE_CONSTRAINT a
21b77 6e 64 20 77 72 69 74 65 0a 2a 2a 20 61 6e 20 65  nd write.** an e
21b78 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20  rror message to 
21b79 69 74 2e 20 54 68 65 6e 20 72 65 74 75 72 6e 20  it. Then return 
21b7a 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2f  SQLITE_ERROR..*/
21b7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
21b7c 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
21b7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21b7e 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43  int sqlite3VdbeC
21b7f 68 65 63 6b 46 6b 28 56 64 62 65 20 2a 70 2c 20  heckFk(Vdbe *p, 
21b80 69 6e 74 20 64 65 66 65 72 72 65 64 29 7b 0a 20  int deferred){. 
21b81 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
21b82 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 64 65 66  ->db;.  if( (def
21b83 65 72 72 65 64 20 26 26 20 64 62 2d 3e 6e 44 65  erred && db->nDe
21b84 66 65 72 72 65 64 43 6f 6e 73 3e 30 29 20 7c 7c  ferredCons>0) ||
21b85 20 28 21 64 65 66 65 72 72 65 64 20 26 26 20 70   (!deferred && p
21b86 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 3e  ->nFkConstraint>
21b87 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20  0) ){.    p->rc 
21b88 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  = SQLITE_CONSTRA
21b89 49 4e 54 3b 0a 20 20 20 20 70 2d 3e 65 72 72 6f  INT;.    p->erro
21b8a 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f  rAction = OE_Abo
21b8b 72 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  rt;.    sqlite3S
21b8c 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
21b8d 72 4d 73 67 2c 20 64 62 2c 20 22 66 6f 72 65 69  rMsg, db, "forei
21b8e 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
21b8f 74 20 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 20  t failed");.    
21b90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
21b91 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ROR;.  }.  retur
21b92 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
21b93 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
21b94 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
21b95 6c 65 64 20 74 68 65 20 77 68 65 6e 20 61 20 56  led the when a V
21b96 44 42 45 20 74 72 69 65 73 20 74 6f 20 68 61 6c  DBE tries to hal
21b97 74 2e 20 20 49 66 20 74 68 65 20 56 44 42 45 0a  t.  If the VDBE.
21b98 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 68 61 6e  ** has made chan
21b99 67 65 73 20 61 6e 64 20 69 73 20 69 6e 20 61 75  ges and is in au
21b9a 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20 74  tocommit mode, t
21b9b 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 6f 73 65  hen commit those
21b9c 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20 49 66  .** changes.  If
21b9d 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6e   a rollback is n
21b9e 65 65 64 65 64 2c 20 74 68 65 6e 20 64 6f 20 74  eeded, then do t
21b9f 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a  he rollback..**.
21ba0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
21ba1 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20  is the only way 
21ba2 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 74 61 74  to move the stat
21ba3 65 20 6f 66 20 61 20 56 4d 20 66 72 6f 6d 0a 2a  e of a VM from.*
21ba4 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 52  * SQLITE_MAGIC_R
21ba5 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47  UN to SQLITE_MAG
21ba6 49 43 5f 48 41 4c 54 2e 20 20 49 74 20 69 73 20  IC_HALT.  It is 
21ba7 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 63  harmless to.** c
21ba8 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61 20 56 4d  all this on a VM
21ba9 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20   that is in the 
21baa 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 4c  SQLITE_MAGIC_HAL
21bab 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 52  T state..**.** R
21bac 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
21bad 6f 64 65 2e 20 20 49 66 20 74 68 65 20 63 6f 6d  ode.  If the com
21bae 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 20 63 6f  mit could not co
21baf 6d 70 6c 65 74 65 20 62 65 63 61 75 73 65 20 6f  mplete because o
21bb0 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e  f.** lock conten
21bb1 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 53 51 4c  tion, return SQL
21bb2 49 54 45 5f 42 55 53 59 2e 20 20 49 66 20 53 51  ITE_BUSY.  If SQ
21bb3 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74  LITE_BUSY is ret
21bb4 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d 65 61  urned, it.** mea
21bb5 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 64 69 64  ns the close did
21bb6 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 6e 64 20   not happen and 
21bb7 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 70 65  needs to be repe
21bb8 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ated..*/.SQLITE_
21bb9 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
21bba 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 62 65  te3VdbeHalt(Vdbe
21bbb 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20   *p){.  int rc; 
21bbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21bbd 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
21bbe 74 6f 20 73 74 6f 72 65 20 74 72 61 6e 73 69 65  to store transie
21bbf 6e 74 20 72 65 74 75 72 6e 20 63 6f 64 65 73 20  nt return codes 
21bc0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
21bc1 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20   = p->db;..  /* 
21bc2 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f  This function co
21bc3 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67 69 63  ntains the logic
21bc4 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73   that determines
21bc5 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20   if a statement 
21bc6 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  or.  ** transact
21bc7 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d  ion will be comm
21bc8 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20  itted or rolled 
21bc9 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74  back as a result
21bca 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65 78 65   of the.  ** exe
21bcb 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 76  cution of this v
21bcc 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20  irtual machine. 
21bcd 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6e  .  **.  ** If an
21bce 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  y of the followi
21bcf 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75 72 3a  ng errors occur:
21bd0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53  .  **.  **     S
21bd1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 2a 2a  QLITE_NOMEM.  **
21bd2 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52       SQLITE_IOER
21bd3 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54  R.  **     SQLIT
21bd4 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 20 20  E_FULL.  **     
21bd5 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
21bd6 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20  .  **.  ** Then 
21bd7 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 63  the internal cac
21bd8 68 65 20 6d 69 67 68 74 20 68 61 76 65 20 62 65  he might have be
21bd9 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e  en left in an in
21bda 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a 2a 20  consistent.  ** 
21bdb 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65 64 20  state.  We need 
21bdc 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  to rollback the 
21bdd 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
21bde 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72 65 20  ction, if there 
21bdf 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f 72 20  is.  ** one, or 
21be0 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 72 61  the complete tra
21be1 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65 72  nsaction if ther
21be2 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e  e is no statemen
21be3 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20  t transaction.. 
21be4 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 64 62   */..  if( p->db
21be5 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
21be6 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51  {.    p->rc = SQ
21be7 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
21be8 20 20 69 66 28 20 70 2d 3e 61 4f 6e 63 65 46 6c    if( p->aOnceFl
21be9 61 67 20 29 20 6d 65 6d 73 65 74 28 70 2d 3e 61  ag ) memset(p->a
21bea 4f 6e 63 65 46 6c 61 67 2c 20 30 2c 20 70 2d 3e  OnceFlag, 0, p->
21beb 6e 4f 6e 63 65 46 6c 61 67 29 3b 0a 20 20 63 6c  nOnceFlag);.  cl
21bec 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 29  oseAllCursors(p)
21bed 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63  ;.  if( p->magic
21bee 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  !=VDBE_MAGIC_RUN
21bef 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
21bf0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
21bf1 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43  checkActiveVdbeC
21bf2 6e 74 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4e 6f  nt(db);..  /* No
21bf3 20 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62   commit or rollb
21bf4 61 63 6b 20 6e 65 65 64 65 64 20 69 66 20 74 68  ack needed if th
21bf5 65 20 70 72 6f 67 72 61 6d 20 6e 65 76 65 72 20  e program never 
21bf6 73 74 61 72 74 65 64 20 2a 2f 0a 20 20 69 66 28  started */.  if(
21bf7 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20   p->pc>=0 ){.   
21bf8 20 69 6e 74 20 6d 72 63 3b 20 20 20 2f 2a 20 50   int mrc;   /* P
21bf9 72 69 6d 61 72 79 20 65 72 72 6f 72 20 63 6f 64  rimary error cod
21bfa 65 20 66 72 6f 6d 20 70 2d 3e 72 63 20 2a 2f 0a  e from p->rc */.
21bfb 20 20 20 20 69 6e 74 20 65 53 74 61 74 65 6d 65      int eStateme
21bfc 6e 74 4f 70 20 3d 20 30 3b 0a 20 20 20 20 69 6e  ntOp = 0;.    in
21bfd 74 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72  t isSpecialError
21bfe 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
21bff 53 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 61  Set to true if a
21c00 20 27 73 70 65 63 69 61 6c 27 20 65 72 72 6f 72   'special' error
21c01 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b   */..    /* Lock
21c02 20 61 6c 6c 20 62 74 72 65 65 73 20 75 73 65 64   all btrees used
21c03 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e   by the statemen
21c04 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  t */.    sqlite3
21c05 56 64 62 65 45 6e 74 65 72 28 70 29 3b 0a 0a 20  VdbeEnter(p);.. 
21c06 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20     /* Check for 
21c07 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69  one of the speci
21c08 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20  al errors */.   
21c09 20 6d 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 30   mrc = p->rc & 0
21c0a 78 66 66 3b 0a 20 20 20 20 61 73 73 65 72 74 28  xff;.    assert(
21c0b 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 49   p->rc!=SQLITE_I
21c0c 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b 20  OERR_BLOCKED ); 
21c0d 20 2f 2a 20 54 68 69 73 20 65 72 72 6f 72 20 6e   /* This error n
21c0e 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73 74 73 20  o longer exists 
21c0f 2a 2f 0a 20 20 20 20 69 73 53 70 65 63 69 61 6c  */.    isSpecial
21c10 45 72 72 6f 72 20 3d 20 6d 72 63 3d 3d 53 51 4c  Error = mrc==SQL
21c11 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 63  ITE_NOMEM || mrc
21c12 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20  ==SQLITE_IOERR. 
21c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c14 20 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49      || mrc==SQLI
21c15 54 45 5f 49 4e 54 45 52 52 55 50 54 20 7c 7c 20  TE_INTERRUPT || 
21c16 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c  mrc==SQLITE_FULL
21c17 3b 0a 20 20 20 20 69 66 28 20 69 73 53 70 65 63  ;.    if( isSpec
21c18 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20 20 20 20  ialError ){.    
21c19 20 20 2f 2a 20 49 66 20 74 68 65 20 71 75 65 72    /* If the quer
21c1a 79 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c 79 20  y was read-only 
21c1b 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f  and the error co
21c1c 64 65 20 69 73 20 53 51 4c 49 54 45 5f 49 4e 54  de is SQLITE_INT
21c1d 45 52 52 55 50 54 2c 20 0a 20 20 20 20 20 20 2a  ERRUPT, .      *
21c1e 2a 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 69 73  * no rollback is
21c1f 20 6e 65 63 65 73 73 61 72 79 2e 20 4f 74 68 65   necessary. Othe
21c20 72 77 69 73 65 2c 20 61 74 20 6c 65 61 73 74 20  rwise, at least 
21c21 61 20 73 61 76 65 70 6f 69 6e 74 20 0a 20 20 20  a savepoint .   
21c22 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f     ** transactio
21c23 6e 20 6d 75 73 74 20 62 65 20 72 6f 6c 6c 65 64  n must be rolled
21c24 20 62 61 63 6b 20 74 6f 20 72 65 73 74 6f 72 65   back to restore
21c25 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f   the database to
21c26 20 61 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e   a .      ** con
21c27 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 20  sistent state.. 
21c28 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
21c29 20 45 76 65 6e 20 69 66 20 74 68 65 20 73 74 61   Even if the sta
21c2a 74 65 6d 65 6e 74 20 69 73 20 72 65 61 64 2d 6f  tement is read-o
21c2b 6e 6c 79 2c 20 69 74 20 69 73 20 69 6d 70 6f 72  nly, it is impor
21c2c 74 61 6e 74 20 74 6f 20 70 65 72 66 6f 72 6d 0a  tant to perform.
21c2d 20 20 20 20 20 20 2a 2a 20 61 20 73 74 61 74 65        ** a state
21c2e 6d 65 6e 74 20 6f 72 20 74 72 61 6e 73 61 63 74  ment or transact
21c2f 69 6f 6e 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65  ion rollback ope
21c30 72 61 74 69 6f 6e 2e 20 49 66 20 74 68 65 20 65  ration. If the e
21c31 72 72 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 6f  rror .      ** o
21c32 63 63 75 72 65 64 20 77 68 69 6c 65 20 77 72 69  ccured while wri
21c33 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72  ting to the jour
21c34 6e 61 6c 2c 20 73 75 62 2d 6a 6f 75 72 6e 61 6c  nal, sub-journal
21c35 20 6f 72 20 64 61 74 61 62 61 73 65 0a 20 20 20   or database.   
21c36 20 20 20 2a 2a 20 66 69 6c 65 20 61 73 20 70 61     ** file as pa
21c37 72 74 20 6f 66 20 61 6e 20 65 66 66 6f 72 74 20  rt of an effort 
21c38 74 6f 20 66 72 65 65 20 75 70 20 63 61 63 68 65  to free up cache
21c39 20 73 70 61 63 65 20 28 73 65 65 20 66 75 6e 63   space (see func
21c3a 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 70 61  tion.      ** pa
21c3b 67 65 72 53 74 72 65 73 73 28 29 20 69 6e 20 70  gerStress() in p
21c3c 61 67 65 72 2e 63 29 2c 20 74 68 65 20 72 6f 6c  ager.c), the rol
21c3d 6c 62 61 63 6b 20 69 73 20 72 65 71 75 69 72 65  lback is require
21c3e 64 20 74 6f 20 72 65 73 74 6f 72 65 20 0a 20 20  d to restore .  
21c3f 20 20 20 20 2a 2a 20 74 68 65 20 70 61 67 65 72      ** the pager
21c40 20 74 6f 20 61 20 63 6f 6e 73 69 73 74 65 6e 74   to a consistent
21c41 20 73 74 61 74 65 2e 0a 20 20 20 20 20 20 2a 2f   state..      */
21c42 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 72  .      if( !p->r
21c43 65 61 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 3d  eadOnly || mrc!=
21c44 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
21c45 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
21c46 28 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  (mrc==SQLITE_NOM
21c47 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54  EM || mrc==SQLIT
21c48 45 5f 46 55 4c 4c 29 20 26 26 20 70 2d 3e 75 73  E_FULL) && p->us
21c49 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b  esStmtJournal ){
21c4a 0a 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74  .          eStat
21c4b 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f  ementOp = SAVEPO
21c4c 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20  INT_ROLLBACK;.  
21c4d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
21c4e 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65         /* We are
21c4f 20 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20   forced to roll 
21c50 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20  back the active 
21c51 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66  transaction. Bef
21c52 6f 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20  ore doing.      
21c53 20 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74      ** so, abort
21c54 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65   any other state
21c55 6d 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c  ments this handl
21c56 65 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20  e currently has 
21c57 61 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20  active..        
21c58 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73    */.          s
21c59 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c  qlite3RollbackAl
21c5a 6c 28 64 62 2c 20 53 51 4c 49 54 45 5f 41 42 4f  l(db, SQLITE_ABO
21c5b 52 54 5f 52 4f 4c 4c 42 41 43 4b 29 3b 0a 20 20  RT_ROLLBACK);.  
21c5c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
21c5d 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 64  loseSavepoints(d
21c5e 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62  b);.          db
21c5f 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31  ->autoCommit = 1
21c60 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
21c61 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
21c62 2a 20 43 68 65 63 6b 20 66 6f 72 20 69 6d 6d 65  * Check for imme
21c63 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65  diate foreign ke
21c64 79 20 76 69 6f 6c 61 74 69 6f 6e 73 2e 20 2a 2f  y violations. */
21c65 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d  .    if( p->rc==
21c66 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
21c67 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
21c68 65 63 6b 46 6b 28 70 2c 20 30 29 3b 0a 20 20 20  eckFk(p, 0);.   
21c69 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20   }.  .    /* If 
21c6a 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20  the auto-commit 
21c6b 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 20  flag is set and 
21c6c 74 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79  this is the only
21c6d 20 61 63 74 69 76 65 20 77 72 69 74 65 72 20 0a   active writer .
21c6e 20 20 20 20 2a 2a 20 56 4d 2c 20 74 68 65 6e 20      ** VM, then 
21c6f 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20 63  we do either a c
21c70 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 63  ommit or rollbac
21c71 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  k of the current
21c72 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 20   transaction. . 
21c73 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74     **.    ** Not
21c74 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61 6c  e: This block al
21c75 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20 6f  so runs if one o
21c76 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 72  f the special er
21c77 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20 20  rors handled .  
21c78 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20 6f    ** above has o
21c79 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a 2f  ccurred. .    */
21c7a 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65  .    if( !sqlite
21c7b 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20  3VtabInSync(db) 
21c7c 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75 74  .     && db->aut
21c7d 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26 26  oCommit .     &&
21c7e 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e   db->writeVdbeCn
21c7f 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d  t==(p->readOnly=
21c80 3d 30 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20  =0) .    ){.    
21c81 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c    if( p->rc==SQL
21c82 49 54 45 5f 4f 4b 20 7c 7c 20 28 70 2d 3e 65 72  ITE_OK || (p->er
21c83 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61  rorAction==OE_Fa
21c84 69 6c 20 26 26 20 21 69 73 53 70 65 63 69 61 6c  il && !isSpecial
21c85 45 72 72 6f 72 29 20 29 7b 0a 20 20 20 20 20 20  Error) ){.      
21c86 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
21c87 62 65 43 68 65 63 6b 46 6b 28 70 2c 20 31 29 3b  beCheckFk(p, 1);
21c88 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
21c89 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
21c8a 20 20 20 20 20 20 20 20 69 66 28 20 4e 45 56 45          if( NEVE
21c8b 52 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29  R(p->readOnly) )
21c8c 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  {.            sq
21c8d 6c 69 74 65 33 56 64 62 65 4c 65 61 76 65 28 70  lite3VdbeLeave(p
21c8e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  );.            r
21c8f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
21c90 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  OR;.          }.
21c91 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
21c92 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
21c93 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
21c94 20 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54   .          /* T
21c95 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66  he auto-commit f
21c96 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68 65  lag is true, the
21c97 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 61   vdbe program wa
21c98 73 20 73 75 63 63 65 73 73 66 75 6c 20 0a 20 20  s successful .  
21c99 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 68 69          ** or hi
21c9a 74 20 61 6e 20 27 4f 52 20 46 41 49 4c 27 20 63  t an 'OR FAIL' c
21c9b 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 74 68  onstraint and th
21c9c 65 72 65 20 61 72 65 20 6e 6f 20 64 65 66 65 72  ere are no defer
21c9d 72 65 64 20 66 6f 72 65 69 67 6e 0a 20 20 20 20  red foreign.    
21c9e 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6e        ** key con
21c9f 73 74 72 61 69 6e 74 73 20 74 6f 20 68 6f 6c 64  straints to hold
21ca0 20 75 70 20 74 68 65 20 74 72 61 6e 73 61 63 74   up the transact
21ca1 69 6f 6e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  ion. This means 
21ca2 61 20 63 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20  a commit .      
21ca3 20 20 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72      ** is requir
21ca4 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ed. */.         
21ca5 20 72 63 20 3d 20 76 64 62 65 43 6f 6d 6d 69 74   rc = vdbeCommit
21ca6 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20  (db, p);.       
21ca7 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72   }.        if( r
21ca8 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26  c==SQLITE_BUSY &
21ca9 26 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b  & p->readOnly ){
21caa 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
21cab 65 33 56 64 62 65 4c 65 61 76 65 28 70 29 3b 0a  e3VdbeLeave(p);.
21cac 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
21cad 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
21cae 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
21caf 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
21cb0 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63  .          p->rc
21cb1 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 20 20 20   = rc;.         
21cb2 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b   sqlite3Rollback
21cb3 41 6c 6c 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  All(db, SQLITE_O
21cb4 4b 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  K);.        }els
21cb5 65 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d  e{.          db-
21cb6 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 20 3d  >nDeferredCons =
21cb7 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71   0;.          sq
21cb8 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72  lite3CommitInter
21cb9 6e 61 6c 43 68 61 6e 67 65 73 28 64 62 29 3b 0a  nalChanges(db);.
21cba 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
21cbb 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
21cbc 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c  qlite3RollbackAl
21cbd 6c 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29  l(db, SQLITE_OK)
21cbe 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
21cbf 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d  db->nStatement =
21cc0 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   0;.    }else if
21cc1 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 3d 3d  ( eStatementOp==
21cc2 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  0 ){.      if( p
21cc3 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
21cc4 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f  || p->errorActio
21cc5 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20  n==OE_Fail ){.  
21cc6 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e 74        eStatement
21cc7 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52  Op = SAVEPOINT_R
21cc8 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 7d 65  ELEASE;.      }e
21cc9 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f 72  lse if( p->error
21cca 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 74  Action==OE_Abort
21ccb 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74 61   ){.        eSta
21ccc 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50  tementOp = SAVEP
21ccd 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20  OINT_ROLLBACK;. 
21cce 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
21ccf 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62      sqlite3Rollb
21cd0 61 63 6b 41 6c 6c 28 64 62 2c 20 53 51 4c 49 54  ackAll(db, SQLIT
21cd1 45 5f 41 42 4f 52 54 5f 52 4f 4c 4c 42 41 43 4b  E_ABORT_ROLLBACK
21cd2 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
21cd3 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74  e3CloseSavepoint
21cd4 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 64  s(db);.        d
21cd5 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20  b->autoCommit = 
21cd6 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
21cd7 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 65 53  .  .    /* If eS
21cd8 74 61 74 65 6d 65 6e 74 4f 70 20 69 73 20 6e 6f  tatementOp is no
21cd9 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 73  n-zero, then a s
21cda 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
21cdb 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 0a 20 20  tion needs to.  
21cdc 20 20 2a 2a 20 62 65 20 63 6f 6d 6d 69 74 74 65    ** be committe
21cdd 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b  d or rolled back
21cde 2e 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64  . Call sqlite3Vd
21cdf 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74  beCloseStatement
21ce0 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 64 6f 20  () to.    ** do 
21ce1 73 6f 2e 20 49 66 20 74 68 69 73 20 6f 70 65 72  so. If this oper
21ce2 61 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 6e  ation returns an
21ce3 20 65 72 72 6f 72 2c 20 61 6e 64 20 74 68 65 20   error, and the 
21ce4 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e  current statemen
21ce5 74 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20 63  t.    ** error c
21ce6 6f 64 65 20 69 73 20 53 51 4c 49 54 45 5f 4f 4b  ode is SQLITE_OK
21ce7 20 6f 72 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54   or SQLITE_CONST
21ce8 52 41 49 4e 54 2c 20 74 68 65 6e 20 70 72 6f 6d  RAINT, then prom
21ce9 6f 74 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 63  ote the.    ** c
21cea 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e 74  urrent statement
21ceb 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 20 20 20   error code..   
21cec 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 53 74 61   */.    if( eSta
21ced 74 65 6d 65 6e 74 4f 70 20 29 7b 0a 20 20 20 20  tementOp ){.    
21cee 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
21cef 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74  beCloseStatement
21cf0 28 70 2c 20 65 53 74 61 74 65 6d 65 6e 74 4f 70  (p, eStatementOp
21cf1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  );.      if( rc 
21cf2 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
21cf3 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
21cf4 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  || p->rc==SQLITE
21cf5 5f 43 4f 4e 53 54 52 41 49 4e 54 20 29 7b 0a 20  _CONSTRAINT ){. 
21cf6 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d           p->rc =
21cf7 20 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73   rc;.          s
21cf8 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
21cf9 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
21cfa 20 20 20 20 20 20 20 20 70 2d 3e 7a 45 72 72 4d          p->zErrM
21cfb 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  sg = 0;.        
21cfc 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
21cfd 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 2c  3RollbackAll(db,
21cfe 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 5f 52 4f   SQLITE_ABORT_RO
21cff 4c 4c 42 41 43 4b 29 3b 0a 20 20 20 20 20 20 20  LLBACK);.       
21d00 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76   sqlite3CloseSav
21d01 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20  epoints(db);.   
21d02 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d       db->autoCom
21d03 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  mit = 1;.      }
21d04 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
21d05 20 49 66 20 74 68 69 73 20 77 61 73 20 61 6e 20   If this was an 
21d06 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20 6f  INSERT, UPDATE o
21d07 72 20 44 45 4c 45 54 45 20 61 6e 64 20 6e 6f 20  r DELETE and no 
21d08 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
21d09 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 68 61 73  ction.    ** has
21d0a 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 63   been rolled bac
21d0b 6b 2c 20 75 70 64 61 74 65 20 74 68 65 20 64 61  k, update the da
21d0c 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
21d0d 6e 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  n change-counter
21d0e 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  . .    */.    if
21d0f 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e  ( p->changeCntOn
21d10 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 53   ){.      if( eS
21d11 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56 45  tatementOp!=SAVE
21d12 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29  POINT_ROLLBACK )
21d13 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
21d14 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28  3VdbeSetChanges(
21d15 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b  db, p->nChange);
21d16 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
21d17 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21d18 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20  eSetChanges(db, 
21d19 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
21d1a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30    p->nChange = 0
21d1b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
21d1c 52 65 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b  Release the lock
21d1d 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  s */.    sqlite3
21d1e 56 64 62 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  VdbeLeave(p);.  
21d1f 7d 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76 65 20  }..  /* We have 
21d20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 68 61 6c  successfully hal
21d21 74 65 64 20 61 6e 64 20 63 6c 6f 73 65 64 20 74  ted and closed t
21d22 68 65 20 56 4d 2e 20 20 52 65 63 6f 72 64 20 74  he VM.  Record t
21d23 68 69 73 20 66 61 63 74 2e 20 2a 2f 0a 20 20 69  his fact. */.  i
21d24 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20  f( p->pc>=0 ){. 
21d25 20 20 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62     db->activeVdb
21d26 65 43 6e 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20  eCnt--;.    if( 
21d27 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a  !p->readOnly ){.
21d28 20 20 20 20 20 20 64 62 2d 3e 77 72 69 74 65 56        db->writeV
21d29 64 62 65 43 6e 74 2d 2d 3b 0a 20 20 20 20 7d 0a  dbeCnt--;.    }.
21d2a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
21d2b 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 3d 64  activeVdbeCnt>=d
21d2c 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 20  b->writeVdbeCnt 
21d2d 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6d 61 67 69  );.  }.  p->magi
21d2e 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 48  c = VDBE_MAGIC_H
21d2f 41 4c 54 3b 0a 20 20 63 68 65 63 6b 41 63 74 69  ALT;.  checkActi
21d30 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 20  veVdbeCnt(db);. 
21d31 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c   if( p->db->mall
21d32 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
21d33 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  p->rc = SQLITE_N
21d34 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  OMEM;.  }..  /* 
21d35 49 66 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d  If the auto-comm
21d36 69 74 20 66 6c 61 67 20 69 73 20 73 65 74 20 74  it flag is set t
21d37 6f 20 74 72 75 65 2c 20 74 68 65 6e 20 61 6e 79  o true, then any
21d38 20 6c 6f 63 6b 73 20 74 68 61 74 20 77 65 72 65   locks that were
21d39 20 68 65 6c 64 0a 20 20 2a 2a 20 62 79 20 63 6f   held.  ** by co
21d3a 6e 6e 65 63 74 69 6f 6e 20 64 62 20 68 61 76 65  nnection db have
21d3b 20 6e 6f 77 20 62 65 65 6e 20 72 65 6c 65 61 73   now been releas
21d3c 65 64 2e 20 43 61 6c 6c 20 73 71 6c 69 74 65 33  ed. Call sqlite3
21d3d 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b  ConnectionUnlock
21d3e 65 64 28 29 20 0a 20 20 2a 2a 20 74 6f 20 69 6e  ed() .  ** to in
21d3f 76 6f 6b 65 20 61 6e 79 20 72 65 71 75 69 72 65  voke any require
21d40 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  d unlock-notify 
21d41 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a  callbacks..  */.
21d42 20 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f    if( db->autoCo
21d43 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  mmit ){.    sqli
21d44 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c  te3ConnectionUnl
21d45 6f 63 6b 65 64 28 64 62 29 3b 0a 20 20 7d 0a 0a  ocked(db);.  }..
21d46 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63    assert( db->ac
21d47 74 69 76 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c  tiveVdbeCnt>0 ||
21d48 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d   db->autoCommit=
21d49 3d 30 20 7c 7c 20 64 62 2d 3e 6e 53 74 61 74 65  =0 || db->nState
21d4a 6d 65 6e 74 3d 3d 30 20 29 3b 0a 20 20 72 65 74  ment==0 );.  ret
21d4b 75 72 6e 20 28 70 2d 3e 72 63 3d 3d 53 51 4c 49  urn (p->rc==SQLI
21d4c 54 45 5f 42 55 53 59 20 3f 20 53 51 4c 49 54 45  TE_BUSY ? SQLITE
21d4d 5f 42 55 53 59 20 3a 20 53 51 4c 49 54 45 5f 4f  _BUSY : SQLITE_O
21d4e 4b 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61  K);.}.../*.** Ea
21d4f 63 68 20 56 44 42 45 20 68 6f 6c 64 73 20 74 68  ch VDBE holds th
21d50 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20  e result of the 
21d51 6d 6f 73 74 20 72 65 63 65 6e 74 20 73 71 6c 69  most recent sqli
21d52 74 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 0a  te3_step() call.
21d53 2a 2a 20 69 6e 20 70 2d 3e 72 63 2e 20 20 54 68  ** in p->rc.  Th
21d54 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
21d55 74 68 61 74 20 72 65 73 75 6c 74 20 62 61 63 6b  that result back
21d56 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a   to SQLITE_OK..*
21d57 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21d58 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
21d59 65 52 65 73 65 74 53 74 65 70 52 65 73 75 6c 74  eResetStepResult
21d5a 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 70 2d 3e  (Vdbe *p){.  p->
21d5b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
21d5c 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74 68  }../*.** Copy th
21d5d 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64  e error code and
21d5e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62   error message b
21d5f 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 68 65 20  elonging to the 
21d60 56 44 42 45 20 70 61 73 73 65 64 0a 2a 2a 20 61  VDBE passed.** a
21d61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
21d62 6d 65 6e 74 20 74 6f 20 69 74 73 20 64 61 74 61  ment to its data
21d63 62 61 73 65 20 68 61 6e 64 6c 65 20 28 73 6f 20  base handle (so 
21d64 74 68 61 74 20 74 68 65 79 20 77 69 6c 6c 20 62  that they will b
21d65 65 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62  e .** returned b
21d66 79 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  y calls to sqlit
21d67 65 33 5f 65 72 72 63 6f 64 65 28 29 20 61 6e 64  e3_errcode() and
21d68 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
21d69 29 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ))..**.** This f
21d6a 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  unction does not
21d6b 20 63 6c 65 61 72 20 74 68 65 20 56 44 42 45 20   clear the VDBE 
21d6c 65 72 72 6f 72 20 63 6f 64 65 20 6f 72 20 6d 65  error code or me
21d6d 73 73 61 67 65 2c 20 6a 75 73 74 0a 2a 2a 20 63  ssage, just.** c
21d6e 6f 70 69 65 73 20 74 68 65 6d 20 74 6f 20 74 68  opies them to th
21d6f 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
21d70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
21d71 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
21d72 56 64 62 65 54 72 61 6e 73 66 65 72 45 72 72 6f  VdbeTransferErro
21d73 72 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71  r(Vdbe *p){.  sq
21d74 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64  lite3 *db = p->d
21d75 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 2d  b;.  int rc = p-
21d76 3e 72 63 3b 0a 20 20 69 66 28 20 70 2d 3e 7a 45  >rc;.  if( p->zE
21d77 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 75 38 20  rrMsg ){.    u8 
21d78 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 64  mallocFailed = d
21d79 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b  b->mallocFailed;
21d7a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69  .    sqlite3Begi
21d7b 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
21d7c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
21d7d 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72  eSetStr(db->pErr
21d7e 2c 20 2d 31 2c 20 70 2d 3e 7a 45 72 72 4d 73 67  , -1, p->zErrMsg
21d7f 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
21d80 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
21d81 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64  ;.    sqlite3End
21d82 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
21d83 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
21d84 69 6c 65 64 20 3d 20 6d 61 6c 6c 6f 63 46 61 69  iled = mallocFai
21d85 6c 65 64 3b 0a 20 20 20 20 64 62 2d 3e 65 72 72  led;.    db->err
21d86 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 7d 65 6c  Code = rc;.  }el
21d87 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  se{.    sqlite3E
21d88 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b  rror(db, rc, 0);
21d89 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
21d8a 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
21d8b 54 45 5f 45 4e 41 42 4c 45 5f 53 51 4c 4c 4f 47  TE_ENABLE_SQLLOG
21d8c 0a 2f 2a 0a 2a 2a 20 49 66 20 61 6e 20 53 51 4c  ./*.** If an SQL
21d8d 49 54 45 5f 43 4f 4e 46 49 47 5f 53 51 4c 4c 4f  ITE_CONFIG_SQLLO
21d8e 47 20 68 6f 6f 6b 20 69 73 20 72 65 67 69 73 74  G hook is regist
21d8f 65 72 65 64 20 61 6e 64 20 74 68 65 20 56 4d 20  ered and the VM 
21d90 68 61 73 20 62 65 65 6e 20 72 75 6e 2c 20 0a 2a  has been run, .*
21d91 2a 20 69 6e 76 6f 6b 65 20 69 74 2e 0a 2a 2f 0a  * invoke it..*/.
21d92 73 74 61 74 69 63 20 76 6f 69 64 20 76 64 62 65  static void vdbe
21d93 49 6e 76 6f 6b 65 53 71 6c 6c 6f 67 28 56 64 62  InvokeSqllog(Vdb
21d94 65 20 2a 76 29 7b 0a 20 20 69 66 28 20 73 71 6c  e *v){.  if( sql
21d95 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
21d96 2e 78 53 71 6c 6c 6f 67 20 26 26 20 76 2d 3e 72  .xSqllog && v->r
21d97 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
21d98 76 2d 3e 7a 53 71 6c 20 26 26 20 76 2d 3e 70 63  v->zSql && v->pc
21d99 3e 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20  >=0 ){.    char 
21d9a 2a 7a 45 78 70 61 6e 64 65 64 20 3d 20 73 71 6c  *zExpanded = sql
21d9b 69 74 65 33 56 64 62 65 45 78 70 61 6e 64 53 71  ite3VdbeExpandSq
21d9c 6c 28 76 2c 20 76 2d 3e 7a 53 71 6c 29 3b 0a 20  l(v, v->zSql);. 
21d9d 20 20 20 61 73 73 65 72 74 28 20 76 2d 3e 64 62     assert( v->db
21d9e 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29  ->init.busy==0 )
21d9f 3b 0a 20 20 20 20 69 66 28 20 7a 45 78 70 61 6e  ;.    if( zExpan
21da0 64 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ded ){.      sql
21da1 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
21da2 2e 78 53 71 6c 6c 6f 67 28 0a 20 20 20 20 20 20  .xSqllog(.      
21da3 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
21da4 6c 43 6f 6e 66 69 67 2e 70 53 71 6c 6c 6f 67 41  lConfig.pSqllogA
21da5 72 67 2c 20 76 2d 3e 64 62 2c 20 7a 45 78 70 61  rg, v->db, zExpa
21da6 6e 64 65 64 2c 20 31 0a 20 20 20 20 20 20 29 3b  nded, 1.      );
21da7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
21da8 46 72 65 65 28 76 2d 3e 64 62 2c 20 7a 45 78 70  Free(v->db, zExp
21da9 61 6e 64 65 64 29 3b 0a 20 20 20 20 7d 0a 20 20  anded);.    }.  
21daa 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  }.}.#else.# defi
21dab 6e 65 20 76 64 62 65 49 6e 76 6f 6b 65 53 71 6c  ne vdbeInvokeSql
21dac 6c 6f 67 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f  log(x).#endif../
21dad 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 61 20  *.** Clean up a 
21dae 56 44 42 45 20 61 66 74 65 72 20 65 78 65 63 75  VDBE after execu
21daf 74 69 6f 6e 20 62 75 74 20 64 6f 20 6e 6f 74 20  tion but do not 
21db0 64 65 6c 65 74 65 20 74 68 65 20 56 44 42 45 20  delete the VDBE 
21db1 6a 75 73 74 20 79 65 74 2e 0a 2a 2a 20 57 72 69  just yet..** Wri
21db2 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73  te any error mes
21db3 73 61 67 65 73 20 69 6e 74 6f 20 2a 70 7a 45 72  sages into *pzEr
21db4 72 4d 73 67 2e 20 20 52 65 74 75 72 6e 20 74 68  rMsg.  Return th
21db5 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a 2a  e result code..*
21db6 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20  *.** After this 
21db7 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e 2c 20  routine is run, 
21db8 74 68 65 20 56 44 42 45 20 73 68 6f 75 6c 64 20  the VDBE should 
21db9 62 65 20 72 65 61 64 79 20 74 6f 20 62 65 20 65  be ready to be e
21dba 78 65 63 75 74 65 64 0a 2a 2a 20 61 67 61 69 6e  xecuted.** again
21dbb 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 6c 6f 6f 6b 20  ..**.** To look 
21dbc 61 74 20 69 74 20 61 6e 6f 74 68 65 72 20 77 61  at it another wa
21dbd 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  y, this routine 
21dbe 72 65 73 65 74 73 20 74 68 65 20 73 74 61 74 65  resets the state
21dbf 20 6f 66 20 74 68 65 0a 2a 2a 20 76 69 72 74 75   of the.** virtu
21dc0 61 6c 20 6d 61 63 68 69 6e 65 20 66 72 6f 6d 20  al machine from 
21dc1 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 6f  VDBE_MAGIC_RUN o
21dc2 72 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c  r VDBE_MAGIC_HAL
21dc3 54 20 62 61 63 6b 20 74 6f 0a 2a 2a 20 56 44 42  T back to.** VDB
21dc4 45 5f 4d 41 47 49 43 5f 49 4e 49 54 2e 0a 2a 2f  E_MAGIC_INIT..*/
21dc5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21dc6 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52  int sqlite3VdbeR
21dc7 65 73 65 74 28 56 64 62 65 20 2a 70 29 7b 0a 20  eset(Vdbe *p){. 
21dc8 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
21dc9 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f  db = p->db;..  /
21dca 2a 20 49 66 20 74 68 65 20 56 4d 20 64 69 64 20  * If the VM did 
21dcb 6e 6f 74 20 72 75 6e 20 74 6f 20 63 6f 6d 70 6c  not run to compl
21dcc 65 74 69 6f 6e 20 6f 72 20 69 66 20 69 74 20 65  etion or if it e
21dcd 6e 63 6f 75 6e 74 65 72 65 64 20 61 6e 0a 20 20  ncountered an.  
21dce 2a 2a 20 65 72 72 6f 72 2c 20 74 68 65 6e 20 69  ** error, then i
21dcf 74 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 76 65  t might not have
21dd0 20 62 65 65 6e 20 68 61 6c 74 65 64 20 70 72 6f   been halted pro
21dd1 70 65 72 6c 79 2e 20 20 53 6f 20 68 61 6c 74 0a  perly.  So halt.
21dd2 20 20 2a 2a 20 69 74 20 6e 6f 77 2e 0a 20 20 2a    ** it now..  *
21dd3 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 48  /.  sqlite3VdbeH
21dd4 61 6c 74 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66  alt(p);..  /* If
21dd5 20 74 68 65 20 56 44 42 45 20 68 61 73 20 62 65   the VDBE has be
21dd6 20 72 75 6e 20 65 76 65 6e 20 70 61 72 74 69 61   run even partia
21dd7 6c 6c 79 2c 20 74 68 65 6e 20 74 72 61 6e 73 66  lly, then transf
21dd8 65 72 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  er the error cod
21dd9 65 0a 20 20 2a 2a 20 61 6e 64 20 65 72 72 6f 72  e.  ** and error
21dda 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68   message from th
21ddb 65 20 56 44 42 45 20 69 6e 74 6f 20 74 68 65 20  e VDBE into the 
21ddc 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74  main database st
21ddd 72 75 63 74 75 72 65 2e 20 20 42 75 74 0a 20 20  ructure.  But.  
21dde 2a 2a 20 69 66 20 74 68 65 20 56 44 42 45 20 68  ** if the VDBE h
21ddf 61 73 20 6a 75 73 74 20 62 65 65 6e 20 73 65 74  as just been set
21de0 20 74 6f 20 72 75 6e 20 62 75 74 20 68 61 73 20   to run but has 
21de1 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 65 78 65  not actually exe
21de2 63 75 74 65 64 20 61 6e 79 0a 20 20 2a 2a 20 69  cuted any.  ** i
21de3 6e 73 74 72 75 63 74 69 6f 6e 73 20 79 65 74 2c  nstructions yet,
21de4 20 6c 65 61 76 65 20 74 68 65 20 6d 61 69 6e 20   leave the main 
21de5 64 61 74 61 62 61 73 65 20 65 72 72 6f 72 20 69  database error i
21de6 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 6e 63 68 61  nformation uncha
21de7 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  nged..  */.  if(
21de8 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20   p->pc>=0 ){.   
21de9 20 76 64 62 65 49 6e 76 6f 6b 65 53 71 6c 6c 6f   vdbeInvokeSqllo
21dea 67 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  g(p);.    sqlite
21deb 33 56 64 62 65 54 72 61 6e 73 66 65 72 45 72 72  3VdbeTransferErr
21dec 6f 72 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 74  or(p);.    sqlit
21ded 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
21dee 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d  zErrMsg);.    p-
21def 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
21df0 20 20 69 66 28 20 70 2d 3e 72 75 6e 4f 6e 6c 79    if( p->runOnly
21df1 4f 6e 63 65 20 29 20 70 2d 3e 65 78 70 69 72 65  Once ) p->expire
21df2 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69  d = 1;.  }else i
21df3 66 28 20 70 2d 3e 72 63 20 26 26 20 70 2d 3e 65  f( p->rc && p->e
21df4 78 70 69 72 65 64 20 29 7b 0a 20 20 20 20 2f 2a  xpired ){.    /*
21df5 20 54 68 65 20 65 78 70 69 72 65 64 20 66 6c 61   The expired fla
21df6 67 20 77 61 73 20 73 65 74 20 6f 6e 20 74 68 65  g was set on the
21df7 20 56 44 42 45 20 62 65 66 6f 72 65 20 74 68 65   VDBE before the
21df8 20 66 69 72 73 74 20 63 61 6c 6c 0a 20 20 20 20   first call.    
21df9 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 74  ** to sqlite3_st
21dfa 65 70 28 29 2e 20 46 6f 72 20 63 6f 6e 73 69 73  ep(). For consis
21dfb 74 65 6e 63 79 20 28 73 69 6e 63 65 20 73 71 6c  tency (since sql
21dfc 69 74 65 33 5f 73 74 65 70 28 29 20 77 61 73 0a  ite3_step() was.
21dfd 20 20 20 20 2a 2a 20 63 61 6c 6c 65 64 29 2c 20      ** called), 
21dfe 73 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65  set the database
21dff 20 65 72 72 6f 72 20 69 6e 20 74 68 69 73 20 63   error in this c
21e00 61 73 65 20 61 73 20 77 65 6c 6c 2e 0a 20 20 20  ase as well..   
21e01 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45   */.    sqlite3E
21e02 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63 2c 20  rror(db, p->rc, 
21e03 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
21e04 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70  alueSetStr(db->p
21e05 45 72 72 2c 20 2d 31 2c 20 70 2d 3e 7a 45 72 72  Err, -1, p->zErr
21e06 4d 73 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Msg, SQLITE_UTF8
21e07 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
21e08 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  NT);.    sqlite3
21e09 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
21e0a 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a  rrMsg);.    p->z
21e0b 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a  ErrMsg = 0;.  }.
21e0c 0a 20 20 2f 2a 20 52 65 63 6c 61 69 6d 20 61 6c  .  /* Reclaim al
21e0d 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79  l memory used by
21e0e 20 74 68 65 20 56 44 42 45 0a 20 20 2a 2f 0a 20   the VDBE.  */. 
21e0f 20 43 6c 65 61 6e 75 70 28 70 29 3b 0a 0a 20 20   Cleanup(p);..  
21e10 2f 2a 20 53 61 76 65 20 70 72 6f 66 69 6c 69 6e  /* Save profilin
21e11 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  g information fr
21e12 6f 6d 20 74 68 69 73 20 56 44 42 45 20 72 75 6e  om this VDBE run
21e13 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 56 44  ..  */.#ifdef VD
21e14 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20  BE_PROFILE.  {. 
21e15 20 20 20 46 49 4c 45 20 2a 6f 75 74 20 3d 20 66     FILE *out = f
21e16 6f 70 65 6e 28 22 76 64 62 65 5f 70 72 6f 66 69  open("vdbe_profi
21e17 6c 65 2e 6f 75 74 22 2c 20 22 61 22 29 3b 0a 20  le.out", "a");. 
21e18 20 20 20 69 66 28 20 6f 75 74 20 29 7b 0a 20 20     if( out ){.  
21e19 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
21e1a 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2d   fprintf(out, "-
21e1b 2d 2d 2d 20 22 29 3b 0a 20 20 20 20 20 20 66 6f  --- ");.      fo
21e1c 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
21e1d 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66   i++){.        f
21e1e 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 30 32  printf(out, "%02
21e1f 78 22 2c 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70  x", p->aOp[i].op
21e20 63 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  code);.      }. 
21e21 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
21e22 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 20 20 66  , "\n");.      f
21e23 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70  or(i=0; i<p->nOp
21e24 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
21e25 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 36  fprintf(out, "%6
21e26 64 20 25 31 30 6c 6c 64 20 25 38 6c 6c 64 20 22  d %10lld %8lld "
21e27 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e  ,.           p->
21e28 61 4f 70 5b 69 5d 2e 63 6e 74 2c 0a 20 20 20 20  aOp[i].cnt,.    
21e29 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d         p->aOp[i]
21e2a 2e 63 79 63 6c 65 73 2c 0a 20 20 20 20 20 20 20  .cycles,.       
21e2b 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e      p->aOp[i].cn
21e2c 74 3e 30 20 3f 20 70 2d 3e 61 4f 70 5b 69 5d 2e  t>0 ? p->aOp[i].
21e2d 63 79 63 6c 65 73 2f 70 2d 3e 61 4f 70 5b 69 5d  cycles/p->aOp[i]
21e2e 2e 63 6e 74 20 3a 20 30 0a 20 20 20 20 20 20 20  .cnt : 0.       
21e2f 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
21e30 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 6f  te3VdbePrintOp(o
21e31 75 74 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69  ut, i, &p->aOp[i
21e32 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ]);.      }.    
21e33 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20    fclose(out);. 
21e34 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
21e35 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42    p->magic = VDB
21e36 45 5f 4d 41 47 49 43 5f 49 4e 49 54 3b 0a 20 20  E_MAGIC_INIT;.  
21e37 72 65 74 75 72 6e 20 70 2d 3e 72 63 20 26 20 64  return p->rc & d
21e38 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d 0a 20 0a  b->errMask;.}. .
21e39 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 61  /*.** Clean up a
21e3a 6e 64 20 64 65 6c 65 74 65 20 61 20 56 44 42 45  nd delete a VDBE
21e3b 20 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e   after execution
21e3c 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74  .  Return an int
21e3d 65 67 65 72 20 77 68 69 63 68 20 69 73 0a 2a 2a  eger which is.**
21e3e 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65   the result code
21e3f 2e 20 20 57 72 69 74 65 20 61 6e 79 20 65 72 72  .  Write any err
21e40 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20  or message text 
21e41 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 0a  into *pzErrMsg..
21e42 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21e43 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
21e44 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 20 2a  eFinalize(Vdbe *
21e45 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  p){.  int rc = S
21e46 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
21e47 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
21e48 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 6d  AGIC_RUN || p->m
21e49 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
21e4a 5f 48 41 4c 54 20 29 7b 0a 20 20 20 20 72 63 20  _HALT ){.    rc 
21e4b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73  = sqlite3VdbeRes
21e4c 65 74 28 70 29 3b 0a 20 20 20 20 61 73 73 65 72  et(p);.    asser
21e4d 74 28 20 28 72 63 20 26 20 70 2d 3e 64 62 2d 3e  t( (rc & p->db->
21e4e 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a  errMask)==rc );.
21e4f 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
21e50 65 44 65 6c 65 74 65 28 70 29 3b 0a 20 20 72 65  eDelete(p);.  re
21e51 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
21e52 2a 20 43 61 6c 6c 20 74 68 65 20 64 65 73 74 72  * Call the destr
21e53 75 63 74 6f 72 20 66 6f 72 20 65 61 63 68 20 61  uctor for each a
21e54 75 78 64 61 74 61 20 65 6e 74 72 79 20 69 6e 20  uxdata entry in 
21e55 70 56 64 62 65 46 75 6e 63 20 66 6f 72 20 77 68  pVdbeFunc for wh
21e56 69 63 68 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65  ich.** the corre
21e57 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 6e 20  sponding bit in 
21e58 6d 61 73 6b 20 69 73 20 63 6c 65 61 72 2e 20 20  mask is clear.  
21e59 41 75 78 64 61 74 61 20 65 6e 74 72 69 65 73 20  Auxdata entries 
21e5a 62 65 79 6f 6e 64 20 33 31 0a 2a 2a 20 61 72 65  beyond 31.** are
21e5b 20 61 6c 77 61 79 73 20 64 65 73 74 72 6f 79 65   always destroye
21e5c 64 2e 20 20 54 6f 20 64 65 73 74 72 6f 79 20 61  d.  To destroy a
21e5d 6c 6c 20 61 75 78 64 61 74 61 20 65 6e 74 72 69  ll auxdata entri
21e5e 65 73 2c 20 63 61 6c 6c 20 74 68 69 73 0a 2a 2a  es, call this.**
21e5f 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 6d 61   routine with ma
21e60 73 6b 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45  sk==0..*/.SQLITE
21e61 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
21e62 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41  lite3VdbeDeleteA
21e63 75 78 44 61 74 61 28 56 64 62 65 46 75 6e 63 20  uxData(VdbeFunc 
21e64 2a 70 56 64 62 65 46 75 6e 63 2c 20 69 6e 74 20  *pVdbeFunc, int 
21e65 6d 61 73 6b 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  mask){.  int i;.
21e66 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 56 64    for(i=0; i<pVd
21e67 62 65 46 75 6e 63 2d 3e 6e 41 75 78 3b 20 69 2b  beFunc->nAux; i+
21e68 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 41  +){.    struct A
21e69 75 78 44 61 74 61 20 2a 70 41 75 78 20 3d 20 26  uxData *pAux = &
21e6a 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78  pVdbeFunc->apAux
21e6b 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 28 69 3e  [i];.    if( (i>
21e6c 33 31 20 7c 7c 20 21 28 6d 61 73 6b 26 28 28 28  31 || !(mask&(((
21e6d 75 33 32 29 31 29 3c 3c 69 29 29 29 20 26 26 20  u32)1)<<i))) && 
21e6e 70 41 75 78 2d 3e 70 41 75 78 20 29 7b 0a 20 20  pAux->pAux ){.  
21e6f 20 20 20 20 69 66 28 20 70 41 75 78 2d 3e 78 44      if( pAux->xD
21e70 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20 20  elete ){.       
21e71 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 28 70   pAux->xDelete(p
21e72 41 75 78 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20  Aux->pAux);.    
21e73 20 20 7d 0a 20 20 20 20 20 20 70 41 75 78 2d 3e    }.      pAux->
21e74 70 41 75 78 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  pAux = 0;.    }.
21e75 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65    }.}../*.** Fre
21e76 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 73 73  e all memory ass
21e77 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
21e78 20 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20   Vdbe passed as 
21e79 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
21e7a 65 6e 74 2c 0a 2a 2a 20 65 78 63 65 70 74 20 66  ent,.** except f
21e7b 6f 72 20 6f 62 6a 65 63 74 20 69 74 73 65 6c 66  or object itself
21e7c 2c 20 77 68 69 63 68 20 69 73 20 70 72 65 73 65  , which is prese
21e7d 72 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rved..**.** The 
21e7e 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65  difference betwe
21e7f 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
21e80 20 61 6e 64 20 73 71 6c 69 74 65 33 56 64 62 65   and sqlite3Vdbe
21e81 44 65 6c 65 74 65 28 29 20 69 73 20 74 68 61 74  Delete() is that
21e82 0a 2a 2a 20 56 64 62 65 44 65 6c 65 74 65 28 29  .** VdbeDelete()
21e83 20 61 6c 73 6f 20 75 6e 6c 69 6e 6b 73 20 74 68   also unlinks th
21e84 65 20 56 64 62 65 20 66 72 6f 6d 20 74 68 65 20  e Vdbe from the 
21e85 6c 69 73 74 20 6f 66 20 56 4d 73 20 61 73 73 6f  list of VMs asso
21e86 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 74  ciated with.** t
21e87 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
21e88 65 63 74 69 6f 6e 20 61 6e 64 20 66 72 65 65 73  ection and frees
21e89 20 74 68 65 20 6f 62 6a 65 63 74 20 69 74 73 65   the object itse
21e8a 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  lf..*/.SQLITE_PR
21e8b 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
21e8c 65 33 56 64 62 65 43 6c 65 61 72 4f 62 6a 65 63  e3VdbeClearObjec
21e8d 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56  t(sqlite3 *db, V
21e8e 64 62 65 20 2a 70 29 7b 0a 20 20 53 75 62 50 72  dbe *p){.  SubPr
21e8f 6f 67 72 61 6d 20 2a 70 53 75 62 2c 20 2a 70 4e  ogram *pSub, *pN
21e90 65 78 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  ext;.  int i;.  
21e91 61 73 73 65 72 74 28 20 70 2d 3e 64 62 3d 3d 30  assert( p->db==0
21e92 20 7c 7c 20 70 2d 3e 64 62 3d 3d 64 62 20 29 3b   || p->db==db );
21e93 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72  .  releaseMemArr
21e94 61 79 28 70 2d 3e 61 56 61 72 2c 20 70 2d 3e 6e  ay(p->aVar, p->n
21e95 56 61 72 29 3b 0a 20 20 72 65 6c 65 61 73 65 4d  Var);.  releaseM
21e96 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c 4e  emArray(p->aColN
21e97 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c 75  ame, p->nResColu
21e98 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20  mn*COLNAME_N);. 
21e99 20 66 6f 72 28 70 53 75 62 3d 70 2d 3e 70 50 72   for(pSub=p->pPr
21e9a 6f 67 72 61 6d 3b 20 70 53 75 62 3b 20 70 53 75  ogram; pSub; pSu
21e9b 62 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e  b=pNext){.    pN
21e9c 65 78 74 20 3d 20 70 53 75 62 2d 3e 70 4e 65 78  ext = pSub->pNex
21e9d 74 3b 0a 20 20 20 20 76 64 62 65 46 72 65 65 4f  t;.    vdbeFreeO
21e9e 70 41 72 72 61 79 28 64 62 2c 20 70 53 75 62 2d  pArray(db, pSub-
21e9f 3e 61 4f 70 2c 20 70 53 75 62 2d 3e 6e 4f 70 29  >aOp, pSub->nOp)
21ea0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
21ea1 72 65 65 28 64 62 2c 20 70 53 75 62 29 3b 0a 20  ree(db, pSub);. 
21ea2 20 7d 0a 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 7a   }.  for(i=p->nz
21ea3 56 61 72 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d  Var-1; i>=0; i--
21ea4 29 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  ) sqlite3DbFree(
21ea5 64 62 2c 20 70 2d 3e 61 7a 56 61 72 5b 69 5d 29  db, p->azVar[i])
21ea6 3b 0a 20 20 76 64 62 65 46 72 65 65 4f 70 41 72  ;.  vdbeFreeOpAr
21ea7 72 61 79 28 64 62 2c 20 70 2d 3e 61 4f 70 2c 20  ray(db, p->aOp, 
21ea8 70 2d 3e 6e 4f 70 29 3b 0a 20 20 73 71 6c 69 74  p->nOp);.  sqlit
21ea9 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
21eaa 61 4c 61 62 65 6c 29 3b 0a 20 20 73 71 6c 69 74  aLabel);.  sqlit
21eab 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
21eac 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  aColName);.  sql
21ead 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
21eae 2d 3e 7a 53 71 6c 29 3b 0a 20 20 73 71 6c 69 74  ->zSql);.  sqlit
21eaf 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
21eb0 70 46 72 65 65 29 3b 0a 23 69 66 20 64 65 66 69  pFree);.#if defi
21eb1 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
21eb2 45 5f 54 52 45 45 5f 45 58 50 4c 41 49 4e 29 0a  E_TREE_EXPLAIN).
21eb3 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
21eb4 2d 3e 7a 45 78 70 6c 61 69 6e 29 3b 0a 20 20 73  ->zExplain);.  s
21eb5 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
21eb6 20 70 2d 3e 70 45 78 70 6c 61 69 6e 29 3b 0a 23   p->pExplain);.#
21eb7 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  endif.}../*.** D
21eb8 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20  elete an entire 
21eb9 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  VDBE..*/.SQLITE_
21eba 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
21ebb 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 56  ite3VdbeDelete(V
21ebc 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74  dbe *p){.  sqlit
21ebd 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 4e  e3 *db;..  if( N
21ebe 45 56 45 52 28 70 3d 3d 30 29 20 29 20 72 65 74  EVER(p==0) ) ret
21ebf 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64  urn;.  db = p->d
21ec0 62 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  b;.  assert( sql
21ec1 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
21ec2 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
21ec3 73 71 6c 69 74 65 33 56 64 62 65 43 6c 65 61 72  sqlite3VdbeClear
21ec4 4f 62 6a 65 63 74 28 64 62 2c 20 70 29 3b 0a 20  Object(db, p);. 
21ec5 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29 7b   if( p->pPrev ){
21ec6 0a 20 20 20 20 70 2d 3e 70 50 72 65 76 2d 3e 70  .    p->pPrev->p
21ec7 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b  Next = p->pNext;
21ec8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
21ec9 73 65 72 74 28 20 64 62 2d 3e 70 56 64 62 65 3d  sert( db->pVdbe=
21eca 3d 70 20 29 3b 0a 20 20 20 20 64 62 2d 3e 70 56  =p );.    db->pV
21ecb 64 62 65 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a  dbe = p->pNext;.
21ecc 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65    }.  if( p->pNe
21ecd 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e 70 4e 65  xt ){.    p->pNe
21ece 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 2d 3e 70  xt->pPrev = p->p
21ecf 50 72 65 76 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6d  Prev;.  }.  p->m
21ed0 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
21ed1 43 5f 44 45 41 44 3b 0a 20 20 70 2d 3e 64 62 20  C_DEAD;.  p->db 
21ed2 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  = 0;.  sqlite3Db
21ed3 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a  Free(db, p);.}..
21ed4 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
21ed5 74 68 65 20 63 75 72 73 6f 72 20 70 20 69 73 20  the cursor p is 
21ed6 72 65 61 64 79 20 74 6f 20 72 65 61 64 20 6f 72  ready to read or
21ed7 20 77 72 69 74 65 20 74 68 65 20 72 6f 77 20 74   write the row t
21ed8 6f 20 77 68 69 63 68 20 69 74 0a 2a 2a 20 77 61  o which it.** wa
21ed9 73 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 65  s last positione
21eda 64 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 65 72  d.  Return an er
21edb 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20 4f  ror code if an O
21edc 4f 4d 20 66 61 75 6c 74 20 6f 72 20 49 2f 4f 20  OM fault or I/O 
21edd 65 72 72 6f 72 0a 2a 2a 20 70 72 65 76 65 6e 74  error.** prevent
21ede 73 20 75 73 20 66 72 6f 6d 20 70 6f 73 69 74 69  s us from positi
21edf 6f 6e 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72  oning the cursor
21ee0 20 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20   to its correct 
21ee1 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  position..**.** 
21ee2 49 66 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72  If a MoveTo oper
21ee3 61 74 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67  ation is pending
21ee4 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75   on the given cu
21ee5 72 73 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68  rsor, then do th
21ee6 61 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77  at.** MoveTo now
21ee7 2e 20 20 49 66 20 6e 6f 20 6d 6f 76 65 20 69 73  .  If no move is
21ee8 20 70 65 6e 64 69 6e 67 2c 20 63 68 65 63 6b 20   pending, check 
21ee9 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 72 6f  to see if the ro
21eea 77 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 64 65  w has been.** de
21eeb 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75  leted out from u
21eec 6e 64 65 72 20 74 68 65 20 63 75 72 73 6f 72 20  nder the cursor 
21eed 61 6e 64 20 69 66 20 69 74 20 68 61 73 2c 20 6d  and if it has, m
21eee 61 72 6b 20 74 68 65 20 72 6f 77 20 61 73 0a 2a  ark the row as.*
21eef 2a 20 61 20 4e 55 4c 4c 20 72 6f 77 2e 0a 2a 2a  * a NULL row..**
21ef0 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f  .** If the curso
21ef1 72 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f 69  r is already poi
21ef2 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 72  nting to the cor
21ef3 72 65 63 74 20 72 6f 77 20 61 6e 64 20 74 68 61  rect row and tha
21ef4 74 20 72 6f 77 20 68 61 73 0a 2a 2a 20 6e 6f 74  t row has.** not
21ef5 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20 6f 75   been deleted ou
21ef6 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65  t from under the
21ef7 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 74 68   cursor, then th
21ef8 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
21ef9 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45  no-op..*/.SQLITE
21efa 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
21efb 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f  ite3VdbeCursorMo
21efc 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 20  veto(VdbeCursor 
21efd 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 65  *p){.  if( p->de
21efe 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a  ferredMoveto ){.
21eff 20 20 20 20 69 6e 74 20 72 65 73 2c 20 72 63 3b      int res, rc;
21f00 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
21f01 45 53 54 0a 20 20 20 20 65 78 74 65 72 6e 20 69  EST.    extern i
21f02 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63  nt sqlite3_searc
21f03 68 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 0a  h_count;.#endif.
21f04 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69      assert( p->i
21f05 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 63  sTable );.    rc
21f06 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
21f07 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d  ovetoUnpacked(p-
21f08 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e  >pCursor, 0, p->
21f09 6d 6f 76 65 74 6f 54 61 72 67 65 74 2c 20 30 2c  movetoTarget, 0,
21f0a 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20   &res);.    if( 
21f0b 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
21f0c 20 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64      p->lastRowid
21f0d 20 3d 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67   = p->movetoTarg
21f0e 65 74 3b 0a 20 20 20 20 69 66 28 20 72 65 73 21  et;.    if( res!
21f0f 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
21f10 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
21f11 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64 49 73 56  .    p->rowidIsV
21f12 61 6c 69 64 20 3d 20 31 3b 0a 23 69 66 64 65 66  alid = 1;.#ifdef
21f13 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20   SQLITE_TEST.   
21f14 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f   sqlite3_search_
21f15 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a  count++;.#endif.
21f16 20 20 20 20 70 2d 3e 64 65 66 65 72 72 65 64 4d      p->deferredM
21f17 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70  oveto = 0;.    p
21f18 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
21f19 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d  CACHE_STALE;.  }
21f1a 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53 28  else if( ALWAYS(
21f1b 70 2d 3e 70 43 75 72 73 6f 72 29 20 29 7b 0a 20  p->pCursor) ){. 
21f1c 20 20 20 69 6e 74 20 68 61 73 4d 6f 76 65 64 3b     int hasMoved;
21f1d 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71  .    int rc = sq
21f1e 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
21f1f 48 61 73 4d 6f 76 65 64 28 70 2d 3e 70 43 75 72  HasMoved(p->pCur
21f20 73 6f 72 2c 20 26 68 61 73 4d 6f 76 65 64 29 3b  sor, &hasMoved);
21f21 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
21f22 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28  turn rc;.    if(
21f23 20 68 61 73 4d 6f 76 65 64 20 29 7b 0a 20 20 20   hasMoved ){.   
21f24 20 20 20 70 2d 3e 63 61 63 68 65 53 74 61 74 75     p->cacheStatu
21f25 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
21f26 0a 20 20 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f  .      p->nullRo
21f27 77 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  w = 1;.    }.  }
21f28 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
21f29 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
21f2a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63  e following func
21f2b 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c  tions:.**.** sql
21f2c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79  ite3VdbeSerialTy
21f2d 70 65 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56  pe().** sqlite3V
21f2e 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e  dbeSerialTypeLen
21f2f 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62  ().** sqlite3Vdb
21f30 65 53 65 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20  eSerialLen().** 
21f31 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
21f32 6c 50 75 74 28 29 0a 2a 2a 20 73 71 6c 69 74 65  lPut().** sqlite
21f33 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 29  3VdbeSerialGet()
21f34 0a 2a 2a 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61  .**.** encapsula
21f35 74 65 20 74 68 65 20 63 6f 64 65 20 74 68 61 74  te the code that
21f36 20 73 65 72 69 61 6c 69 7a 65 73 20 76 61 6c 75   serializes valu
21f37 65 73 20 66 6f 72 20 73 74 6f 72 61 67 65 20 69  es for storage i
21f38 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61  n SQLite.** data
21f39 20 61 6e 64 20 69 6e 64 65 78 20 72 65 63 6f 72   and index recor
21f3a 64 73 2e 20 45 61 63 68 20 73 65 72 69 61 6c 69  ds. Each seriali
21f3b 7a 65 64 20 76 61 6c 75 65 20 63 6f 6e 73 69 73  zed value consis
21f3c 74 73 20 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69  ts of a.** 'seri
21f3d 61 6c 2d 74 79 70 65 27 20 61 6e 64 20 61 20 62  al-type' and a b
21f3e 6c 6f 62 20 6f 66 20 64 61 74 61 2e 20 54 68 65  lob of data. The
21f3f 20 73 65 72 69 61 6c 20 74 79 70 65 20 69 73 20   serial type is 
21f40 61 6e 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e  an 8-byte unsign
21f41 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73  ed.** integer, s
21f42 74 6f 72 65 64 20 61 73 20 61 20 76 61 72 69 6e  tored as a varin
21f43 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53  t..**.** In an S
21f44 51 4c 69 74 65 20 69 6e 64 65 78 20 72 65 63 6f  QLite index reco
21f45 72 64 2c 20 74 68 65 20 73 65 72 69 61 6c 20 74  rd, the serial t
21f46 79 70 65 20 69 73 20 73 74 6f 72 65 64 20 64 69  ype is stored di
21f47 72 65 63 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a  rectly before.**
21f48 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 64 61 74   the blob of dat
21f49 61 20 74 68 61 74 20 69 74 20 63 6f 72 72 65 73  a that it corres
21f4a 70 6f 6e 64 73 20 74 6f 2e 20 49 6e 20 61 20 74  ponds to. In a t
21f4b 61 62 6c 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c  able record, all
21f4c 20 73 65 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73   serial.** types
21f4d 20 61 72 65 20 73 74 6f 72 65 64 20 61 74 20 74   are stored at t
21f4e 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
21f4f 72 65 63 6f 72 64 2c 20 61 6e 64 20 74 68 65 20  record, and the 
21f50 62 6c 6f 62 73 20 6f 66 20 64 61 74 61 20 61 74  blobs of data at
21f51 0a 2a 2a 20 74 68 65 20 65 6e 64 2e 20 48 65 6e  .** the end. Hen
21f52 63 65 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f  ce these functio
21f53 6e 73 20 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c  ns allow the cal
21f54 6c 65 72 20 74 6f 20 68 61 6e 64 6c 65 20 74 68  ler to handle th
21f55 65 0a 2a 2a 20 73 65 72 69 61 6c 2d 74 79 70 65  e.** serial-type
21f56 20 61 6e 64 20 64 61 74 61 20 62 6c 6f 62 20 73   and data blob s
21f57 65 70 65 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a  eperately..**.**
21f58 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
21f59 61 62 6c 65 20 64 65 73 63 72 69 62 65 73 20 74  able describes t
21f5a 68 65 20 76 61 72 69 6f 75 73 20 73 74 6f 72 61  he various stora
21f5b 67 65 20 63 6c 61 73 73 65 73 20 66 6f 72 20 64  ge classes for d
21f5c 61 74 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72  ata:.**.**   ser
21f5d 69 61 6c 20 74 79 70 65 20 20 20 20 20 20 20 20  ial type        
21f5e 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 20 20  bytes of data   
21f5f 20 20 20 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d     type.**   ---
21f60 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20  -----------     
21f61 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20  --------------- 
21f62 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
21f63 2d 2d 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20  --.**      0    
21f64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f65 20 30 20 20 20 20 20 20 20 20 20 20 20 20 4e 55   0            NU
21f66 4c 4c 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 20  LL.**      1    
21f67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f68 20 31 20 20 20 20 20 20 20 20 20 20 20 20 73 69   1            si
21f69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20  gned integer.** 
21f6a 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20       2          
21f6b 20 20 20 20 20 20 20 20 20 20 20 32 20 20 20 20             2    
21f6c 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69          signed i
21f6d 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 33  nteger.**      3
21f6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f6f 20 20 20 20 20 33 20 20 20 20 20 20 20 20 20 20       3          
21f70 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72    signed integer
21f71 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 20  .**      4      
21f72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
21f73 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e              sign
21f74 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20  ed integer.**   
21f75 20 20 20 35 20 20 20 20 20 20 20 20 20 20 20 20     5            
21f76 20 20 20 20 20 20 20 20 20 36 20 20 20 20 20 20           6      
21f77 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74        signed int
21f78 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 36 20 20  eger.**      6  
21f79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f7a 20 20 20 38 20 20 20 20 20 20 20 20 20 20 20 20     8            
21f7b 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a  signed integer.*
21f7c 2a 20 20 20 20 20 20 37 20 20 20 20 20 20 20 20  *      7        
21f7d 20 20 20 20 20 20 20 20 20 20 20 20 20 38 20 20               8  
21f7e 20 20 20 20 20 20 20 20 20 20 49 45 45 45 20 66            IEEE f
21f7f 6c 6f 61 74 0a 2a 2a 20 20 20 20 20 20 38 20 20  loat.**      8  
21f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f81 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
21f82 49 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  Integer constant
21f83 20 30 0a 2a 2a 20 20 20 20 20 20 39 20 20 20 20   0.**      9    
21f84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f85 20 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e   0            In
21f86 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 31  teger constant 1
21f87 0a 2a 2a 20 20 20 20 20 31 30 2c 31 31 20 20 20  .**     10,11   
21f88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f89 20 20 20 20 20 20 20 20 20 20 20 20 72 65 73 65              rese
21f8a 72 76 65 64 20 66 6f 72 20 65 78 70 61 6e 73 69  rved for expansi
21f8b 6f 6e 0a 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61  on.**    N>=12 a
21f8c 6e 64 20 65 76 65 6e 20 20 20 20 20 20 20 28 4e  nd even       (N
21f8d 2d 31 32 29 2f 32 20 20 20 20 20 20 20 20 42 4c  -12)/2        BL
21f8e 4f 42 0a 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61  OB.**    N>=13 a
21f8f 6e 64 20 6f 64 64 20 20 20 20 20 20 20 20 28 4e  nd odd        (N
21f90 2d 31 33 29 2f 32 20 20 20 20 20 20 20 20 74 65  -13)/2        te
21f91 78 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61  xt.**.** The 8 a
21f92 6e 64 20 39 20 74 79 70 65 73 20 77 65 72 65 20  nd 9 types were 
21f93 61 64 64 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20  added in 3.3.0, 
21f94 66 69 6c 65 20 66 6f 72 6d 61 74 20 34 2e 20 20  file format 4.  
21f95 50 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a  Prior versions.*
21f96 2a 20 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c  * of SQLite will
21f97 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20   not understand 
21f98 74 68 6f 73 65 20 73 65 72 69 61 6c 20 74 79 70  those serial typ
21f99 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  es..*/../*.** Re
21f9a 74 75 72 6e 20 74 68 65 20 73 65 72 69 61 6c 2d  turn the serial-
21f9b 74 79 70 65 20 66 6f 72 20 74 68 65 20 76 61 6c  type for the val
21f9c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d 65  ue stored in pMe
21f9d 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  m..*/.SQLITE_PRI
21f9e 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
21f9f 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 4d  VdbeSerialType(M
21fa0 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69  em *pMem, int fi
21fa1 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e  le_format){.  in
21fa2 74 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e  t flags = pMem->
21fa3 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a  flags;.  int n;.
21fa4 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d  .  if( flags&MEM
21fa5 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74  _Null ){.    ret
21fa6 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 0;.  }.  if(
21fa7 20 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29   flags&MEM_Int )
21fa8 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20  {.    /* Figure 
21fa9 6f 75 74 20 77 68 65 74 68 65 72 20 74 6f 20 75  out whether to u
21faa 73 65 20 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72  se 1, 2, 4, 6 or
21fab 20 38 20 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20   8 bytes. */.#  
21fac 20 64 65 66 69 6e 65 20 4d 41 58 5f 36 42 59 54   define MAX_6BYT
21fad 45 20 28 28 28 28 69 36 34 29 30 78 30 30 30 30  E ((((i64)0x0000
21fae 38 30 30 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20  8000)<<32)-1).  
21faf 20 20 69 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e    i64 i = pMem->
21fb0 75 2e 69 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a  u.i;.    u64 u;.
21fb1 20 20 20 20 69 66 28 20 69 3c 30 20 29 7b 0a 20      if( i<0 ){. 
21fb2 20 20 20 20 20 69 66 28 20 69 3c 28 2d 4d 41 58       if( i<(-MAX
21fb3 5f 36 42 59 54 45 29 20 29 20 72 65 74 75 72 6e  _6BYTE) ) return
21fb4 20 36 3b 0a 20 20 20 20 20 20 2f 2a 20 50 72 65   6;.      /* Pre
21fb5 76 69 6f 75 73 20 74 65 73 74 20 70 72 65 76 65  vious test preve
21fb6 6e 74 73 3a 20 20 75 20 3d 20 2d 28 2d 39 32 32  nts:  u = -(-922
21fb7 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 38  3372036854775808
21fb8 29 20 2a 2f 0a 20 20 20 20 20 20 75 20 3d 20 2d  ) */.      u = -
21fb9 69 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  i;.    }else{.  
21fba 20 20 20 20 75 20 3d 20 69 3b 0a 20 20 20 20 7d      u = i;.    }
21fbb 0a 20 20 20 20 69 66 28 20 75 3c 3d 31 32 37 20  .    if( u<=127 
21fbc 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
21fbd 28 28 69 26 31 29 3d 3d 69 20 26 26 20 66 69 6c  ((i&1)==i && fil
21fbe 65 5f 66 6f 72 6d 61 74 3e 3d 34 29 20 3f 20 38  e_format>=4) ? 8
21fbf 2b 28 75 33 32 29 75 20 3a 20 31 3b 0a 20 20 20  +(u32)u : 1;.   
21fc0 20 7d 0a 20 20 20 20 69 66 28 20 75 3c 3d 33 32   }.    if( u<=32
21fc1 37 36 37 20 29 20 72 65 74 75 72 6e 20 32 3b 0a  767 ) return 2;.
21fc2 20 20 20 20 69 66 28 20 75 3c 3d 38 33 38 38 36      if( u<=83886
21fc3 30 37 20 29 20 72 65 74 75 72 6e 20 33 3b 0a 20  07 ) return 3;. 
21fc4 20 20 20 69 66 28 20 75 3c 3d 32 31 34 37 34 38     if( u<=214748
21fc5 33 36 34 37 20 29 20 72 65 74 75 72 6e 20 34 3b  3647 ) return 4;
21fc6 0a 20 20 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f  .    if( u<=MAX_
21fc7 36 42 59 54 45 20 29 20 72 65 74 75 72 6e 20 35  6BYTE ) return 5
21fc8 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a  ;.    return 6;.
21fc9 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26    }.  if( flags&
21fca 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
21fcb 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20  return 7;.  }.  
21fcc 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
21fcd 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c  ->mallocFailed |
21fce 7c 20 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72  | flags&(MEM_Str
21fcf 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20  |MEM_Blob) );.  
21fd0 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69  n = pMem->n;.  i
21fd1 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a  f( flags & MEM_Z
21fd2 65 72 6f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20  ero ){.    n += 
21fd3 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20  pMem->u.nZero;. 
21fd4 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d   }.  assert( n>=
21fd5 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28  0 );.  return ((
21fd6 6e 2a 32 29 20 2b 20 31 32 20 2b 20 28 28 66 6c  n*2) + 12 + ((fl
21fd7 61 67 73 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29  ags&MEM_Str)!=0)
21fd8 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
21fd9 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66  rn the length of
21fda 20 74 68 65 20 64 61 74 61 20 63 6f 72 72 65 73   the data corres
21fdb 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73  ponding to the s
21fdc 75 70 70 6c 69 65 64 20 73 65 72 69 61 6c 2d 74  upplied serial-t
21fdd 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ype..*/.SQLITE_P
21fde 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74  RIVATE u32 sqlit
21fdf 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
21fe0 4c 65 6e 28 75 33 32 20 73 65 72 69 61 6c 5f 74  Len(u32 serial_t
21fe1 79 70 65 29 7b 0a 20 20 69 66 28 20 73 65 72 69  ype){.  if( seri
21fe2 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20  al_type>=12 ){. 
21fe3 20 20 20 72 65 74 75 72 6e 20 28 73 65 72 69 61     return (seria
21fe4 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20  l_type-12)/2;.  
21fe5 7d 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69  }else{.    stati
21fe6 63 20 63 6f 6e 73 74 20 75 38 20 61 53 69 7a 65  c const u8 aSize
21fe7 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20  [] = { 0, 1, 2, 
21fe8 33 2c 20 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30  3, 4, 6, 8, 8, 0
21fe9 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20  , 0, 0, 0 };.   
21fea 20 72 65 74 75 72 6e 20 61 53 69 7a 65 5b 73 65   return aSize[se
21feb 72 69 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a  rial_type];.  }.
21fec 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61  }../*.** If we a
21fed 72 65 20 6f 6e 20 61 6e 20 61 72 63 68 69 74 65  re on an archite
21fee 63 74 75 72 65 20 77 69 74 68 20 6d 69 78 65 64  cture with mixed
21fef 2d 65 6e 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67  -endian floating
21ff0 20 0a 2a 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a   .** points (ex:
21ff1 20 41 52 4d 37 29 20 74 68 65 6e 20 73 77 61 70   ARM7) then swap
21ff2 20 74 68 65 20 6c 6f 77 65 72 20 34 20 62 79 74   the lower 4 byt
21ff3 65 73 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20  es with the .** 
21ff4 75 70 70 65 72 20 34 20 62 79 74 65 73 2e 20 20  upper 4 bytes.  
21ff5 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c  Return the resul
21ff6 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73  t..**.** For mos
21ff7 74 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2c  t architectures,
21ff8 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70   this is a no-op
21ff9 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a  ..**.** (later):
21ffa 20 20 49 74 20 69 73 20 72 65 70 6f 72 74 65 64    It is reported
21ffb 20 74 6f 20 6d 65 20 74 68 61 74 20 74 68 65 20   to me that the 
21ffc 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f  mixed-endian pro
21ffd 62 6c 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20  blem.** on ARM7 
21ffe 69 73 20 61 6e 20 69 73 73 75 65 20 77 69 74 68  is an issue with
21fff 20 47 43 43 2c 20 6e 6f 74 20 77 69 74 68 20 74   GCC, not with t
22000 68 65 20 41 52 4d 37 20 63 68 69 70 2e 20 20 49  he ARM7 chip.  I
22001 74 20 73 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20  t seems.** that 
22002 65 61 72 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f  early versions o
22003 66 20 47 43 43 20 73 74 6f 72 65 64 20 74 68 65  f GCC stored the
22004 20 74 77 6f 20 77 6f 72 64 73 20 6f 66 20 61 20   two words of a 
22005 36 34 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20  64-bit.** float 
22006 69 6e 20 74 68 65 20 77 72 6f 6e 67 20 6f 72 64  in the wrong ord
22007 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20 65 72  er.  And that er
22008 72 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72 6f  ror has been pro
22009 70 61 67 61 74 65 64 0a 2a 2a 20 65 76 65 72 20  pagated.** ever 
2200a 73 69 6e 63 65 2e 20 20 54 68 65 20 62 6c 61 6d  since.  The blam
2200b 65 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61  e is not necessa
2200c 72 69 6c 79 20 77 69 74 68 20 47 43 43 2c 20 74  rily with GCC, t
2200d 68 6f 75 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69  hough..** GCC mi
2200e 67 68 74 20 68 61 76 65 20 6a 75 73 74 20 63 6f  ght have just co
2200f 70 79 69 6e 67 20 74 68 65 20 70 72 6f 62 6c 65  pying the proble
22010 6d 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63  m from a prior c
22011 6f 6d 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d  ompiler..** I am
22012 20 61 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74 20   also told that 
22013 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f  newer versions o
22014 66 20 47 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f  f GCC that follo
22015 77 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a  w a different.**
22016 20 41 42 49 20 67 65 74 20 74 68 65 20 62 79 74   ABI get the byt
22017 65 20 6f 72 64 65 72 20 72 69 67 68 74 2e 0a 2a  e order right..*
22018 2a 0a 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73 20  *.** Developers 
22019 75 73 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20  using SQLite on 
2201a 61 6e 20 41 52 4d 37 20 73 68 6f 75 6c 64 20 63  an ARM7 should c
2201b 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 20 74  ompile and run t
2201c 68 65 69 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74  heir.** applicat
2201d 69 6f 6e 20 75 73 69 6e 67 20 2d 44 53 51 4c 49  ion using -DSQLI
2201e 54 45 5f 44 45 42 55 47 3d 31 20 61 74 20 6c 65  TE_DEBUG=1 at le
2201f 61 73 74 20 6f 6e 63 65 2e 20 20 57 69 74 68 20  ast once.  With 
22020 44 45 42 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64  DEBUG.** enabled
22021 2c 20 73 6f 6d 65 20 61 73 73 65 72 74 73 20 62  , some asserts b
22022 65 6c 6f 77 20 77 69 6c 6c 20 65 6e 73 75 72 65  elow will ensure
22023 20 74 68 61 74 20 74 68 65 20 62 79 74 65 20 6f   that the byte o
22024 72 64 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74  rder of.** float
22025 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73  ing point values
22026 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a   is correct..**.
22027 2a 2a 20 28 32 30 30 37 2d 30 38 2d 33 30 29 20  ** (2007-08-30) 
22028 20 46 72 61 6e 6b 20 76 61 6e 20 56 75 67 74 20   Frank van Vugt 
22029 68 61 73 20 73 74 75 64 69 65 64 20 74 68 69 73  has studied this
2202a 20 70 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79   problem closely
2202b 0a 2a 2a 20 61 6e 64 20 68 61 73 20 73 65 6e 64  .** and has send
2202c 20 68 69 73 20 66 69 6e 64 69 6e 67 73 20 74 6f   his findings to
2202d 20 74 68 65 20 53 51 4c 69 74 65 20 64 65 76 65   the SQLite deve
2202e 6c 6f 70 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a  lopers.  Frank.*
2202f 2a 20 77 72 69 74 65 73 20 74 68 61 74 20 73 6f  * writes that so
22030 6d 65 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73  me Linux kernels
22031 20 6f 66 66 65 72 20 66 6c 6f 61 74 69 6e 67 20   offer floating 
22032 70 6f 69 6e 74 20 68 61 72 64 77 61 72 65 0a 2a  point hardware.*
22033 2a 20 65 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74  * emulation that
22034 20 75 73 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69   uses only 32-bi
22035 74 20 6d 61 6e 74 69 73 73 61 73 20 69 6e 73 74  t mantissas inst
22036 65 61 64 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a  ead of a full .*
22037 2a 20 34 38 2d 62 69 74 73 20 61 73 20 72 65 71  * 48-bits as req
22038 75 69 72 65 64 20 62 79 20 74 68 65 20 49 45 45  uired by the IEE
22039 45 20 73 74 61 6e 64 61 72 64 2e 20 20 28 54 68  E standard.  (Th
2203a 69 73 20 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e  is is the.** CON
2203b 46 49 47 5f 46 50 45 5f 46 41 53 54 46 50 45 20  FIG_FPE_FASTFPE 
2203c 6f 70 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63  option.)  On suc
2203d 68 20 73 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74  h systems, float
2203e 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74  ing point.** byt
2203f 65 20 73 77 61 70 70 69 6e 67 20 62 65 63 6f 6d  e swapping becom
22040 65 73 20 76 65 72 79 20 63 6f 6d 70 6c 69 63 61  es very complica
22041 74 65 64 2e 20 20 54 6f 20 61 76 6f 69 64 20 70  ted.  To avoid p
22042 72 6f 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20  roblems,.** the 
22043 6e 65 63 65 73 73 61 72 79 20 62 79 74 65 20 73  necessary byte s
22044 77 61 70 70 69 6e 67 20 69 73 20 63 61 72 72 69  wapping is carri
22045 65 64 20 6f 75 74 20 75 73 69 6e 67 20 61 20 36  ed out using a 6
22046 34 2d 62 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a  4-bit integer.**
22047 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 20 36   rather than a 6
22048 34 2d 62 69 74 20 66 6c 6f 61 74 2e 20 20 46 72  4-bit float.  Fr
22049 61 6e 6b 20 61 73 73 75 72 65 73 20 75 73 20 74  ank assures us t
2204a 68 61 74 20 74 68 65 20 63 6f 64 65 20 68 65 72  hat the code her
2204b 65 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68  e.** works for h
2204c 69 6d 2e 20 20 57 65 2c 20 74 68 65 20 64 65 76  im.  We, the dev
2204d 65 6c 6f 70 65 72 73 2c 20 68 61 76 65 20 6e 6f  elopers, have no
2204e 20 77 61 79 20 74 6f 20 69 6e 64 65 70 65 6e 64   way to independ
2204f 65 6e 74 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20  ently.** verify 
22050 74 68 69 73 2c 20 62 75 74 20 46 72 61 6e 6b 20  this, but Frank 
22051 73 65 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68  seems to know wh
22052 61 74 20 68 65 20 69 73 20 74 61 6c 6b 69 6e 67  at he is talking
22053 20 61 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20   about.** so we 
22054 74 72 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69  trust him..*/.#i
22055 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45  fdef SQLITE_MIXE
22056 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46  D_ENDIAN_64BIT_F
22057 4c 4f 41 54 0a 73 74 61 74 69 63 20 75 36 34 20  LOAT.static u64 
22058 66 6c 6f 61 74 53 77 61 70 28 75 36 34 20 69 6e  floatSwap(u64 in
22059 29 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20  ){.  union {.   
2205a 20 75 36 34 20 72 3b 0a 20 20 20 20 75 33 32 20   u64 r;.    u32 
2205b 69 5b 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75  i[2];.  } u;.  u
2205c 33 32 20 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69  32 t;..  u.r = i
2205d 6e 3b 0a 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b  n;.  t = u.i[0];
2205e 0a 20 20 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b  .  u.i[0] = u.i[
2205f 31 5d 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74  1];.  u.i[1] = t
22060 3b 0a 20 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a  ;.  return u.r;.
22061 7d 0a 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d  }.# define swapM
22062 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28  ixedEndianFloat(
22063 58 29 20 20 58 20 3d 20 66 6c 6f 61 74 53 77 61  X)  X = floatSwa
22064 70 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  p(X).#else.# def
22065 69 6e 65 20 73 77 61 70 4d 69 78 65 64 45 6e 64  ine swapMixedEnd
22066 69 61 6e 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64  ianFloat(X).#end
22067 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  if../*.** Write 
22068 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64  the serialized d
22069 61 74 61 20 62 6c 6f 62 20 66 6f 72 20 74 68 65  ata blob for the
2206a 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
2206b 20 70 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62   pMem into .** b
2206c 75 66 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65  uf. It is assume
2206d 64 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65  d that the calle
2206e 72 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20  r has allocated 
2206f 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65  sufficient space
22070 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
22071 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
22072 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e  written..**.** n
22073 42 75 66 20 69 73 20 74 68 65 20 61 6d 6f 75 6e  Buf is the amoun
22074 74 20 6f 66 20 73 70 61 63 65 20 6c 65 66 74 20  t of space left 
22075 69 6e 20 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20  in buf[].  nBuf 
22076 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65 0a 2a  must always be.*
22077 2a 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74  * large enough t
22078 6f 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 69 72  o hold the entir
22079 65 20 66 69 65 6c 64 2e 20 20 45 78 63 65 70 74  e field.  Except
2207a 2c 20 69 66 20 74 68 65 20 66 69 65 6c 64 20 69  , if the field i
2207b 73 0a 2a 2a 20 61 20 62 6c 6f 62 20 77 69 74 68  s.** a blob with
2207c 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74   a zero-filled t
2207d 61 69 6c 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20  ail, then buf[] 
2207e 6d 69 67 68 74 20 62 65 20 6a 75 73 74 20 74 68  might be just th
2207f 65 20 72 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20  e right.** size 
22080 74 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69  to hold everythi
22081 6e 67 20 65 78 63 65 70 74 20 66 6f 72 20 74 68  ng except for th
22082 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61  e zero-filled ta
22083 69 6c 2e 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a  il.  If buf[].**
22084 20 69 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f   is only big eno
22085 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ugh to hold the 
22086 6e 6f 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c  non-zero prefix,
22087 20 74 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65   then only write
22088 20 74 68 61 74 0a 2a 2a 20 70 72 65 66 69 78 20   that.** prefix 
22089 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74  into buf[].  But
2208a 20 69 66 20 62 75 66 5b 5d 20 69 73 20 6c 61 72   if buf[] is lar
2208b 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c  ge enough to hol
2208c 64 20 62 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72  d both the.** pr
2208d 65 66 69 78 20 61 6e 64 20 74 68 65 20 74 61 69  efix and the tai
2208e 6c 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65  l then write the
2208f 20 70 72 65 66 69 78 20 61 6e 64 20 73 65 74 20   prefix and set 
22090 74 68 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a  the tail to all.
22091 2a 2a 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20  ** zeros..**.** 
22092 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
22093 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61  r of bytes actua
22094 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  lly written into
22095 20 62 75 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d   buf[].  The num
22096 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20  ber.** of bytes 
22097 69 6e 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c  in the zero-fill
22098 65 64 20 74 61 69 6c 20 69 73 20 69 6e 63 6c 75  ed tail is inclu
22099 64 65 64 20 69 6e 20 74 68 65 20 72 65 74 75 72  ded in the retur
2209a 6e 20 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20  n value only.** 
2209b 69 66 20 74 68 6f 73 65 20 62 79 74 65 73 20 77  if those bytes w
2209c 65 72 65 20 7a 65 72 6f 65 64 20 69 6e 20 62 75  ere zeroed in bu
2209d 66 5b 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f  f[]..*/ .SQLITE_
2209e 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69  PRIVATE u32 sqli
2209f 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74  te3VdbeSerialPut
220a0 28 75 38 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42  (u8 *buf, int nB
220a1 75 66 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69  uf, Mem *pMem, i
220a2 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b  nt file_format){
220a3 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79  .  u32 serial_ty
220a4 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  pe = sqlite3Vdbe
220a5 53 65 72 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c  SerialType(pMem,
220a6 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20   file_format);. 
220a7 20 75 33 32 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20   u32 len;..  /* 
220a8 49 6e 74 65 67 65 72 20 61 6e 64 20 52 65 61 6c  Integer and Real
220a9 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c   */.  if( serial
220aa 5f 74 79 70 65 3c 3d 37 20 26 26 20 73 65 72 69  _type<=7 && seri
220ab 61 6c 5f 74 79 70 65 3e 30 20 29 7b 0a 20 20 20  al_type>0 ){.   
220ac 20 75 36 34 20 76 3b 0a 20 20 20 20 75 33 32 20   u64 v;.    u32 
220ad 69 3b 0a 20 20 20 20 69 66 28 20 73 65 72 69 61  i;.    if( seria
220ae 6c 5f 74 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20  l_type==7 ){.   
220af 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f     assert( sizeo
220b0 66 28 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65  f(v)==sizeof(pMe
220b1 6d 2d 3e 72 29 20 29 3b 0a 20 20 20 20 20 20 6d  m->r) );.      m
220b2 65 6d 63 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d  emcpy(&v, &pMem-
220b3 3e 72 2c 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a  >r, sizeof(v));.
220b4 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45        swapMixedE
220b5 6e 64 69 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20  ndianFloat(v);. 
220b6 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
220b7 76 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20  v = pMem->u.i;. 
220b8 20 20 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69     }.    len = i
220b9 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
220ba 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69  rialTypeLen(seri
220bb 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 61 73  al_type);.    as
220bc 73 65 72 74 28 20 6c 65 6e 3c 3d 28 75 33 32 29  sert( len<=(u32)
220bd 6e 42 75 66 20 29 3b 0a 20 20 20 20 77 68 69 6c  nBuf );.    whil
220be 65 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20  e( i-- ){.      
220bf 62 75 66 5b 69 5d 20 3d 20 28 75 38 29 28 76 26  buf[i] = (u8)(v&
220c0 30 78 46 46 29 3b 0a 20 20 20 20 20 20 76 20 3e  0xFF);.      v >
220c1 3e 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20  >= 8;.    }.    
220c2 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a  return len;.  }.
220c3 0a 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20  .  /* String or 
220c4 62 6c 6f 62 20 2a 2f 0a 20 20 69 66 28 20 73 65  blob */.  if( se
220c5 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b  rial_type>=12 ){
220c6 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
220c7 6d 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66  m->n + ((pMem->f
220c8 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29  lags & MEM_Zero)
220c9 3f 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30  ?pMem->u.nZero:0
220ca 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 3d  ).             =
220cb 3d 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64  = (int)sqlite3Vd
220cc 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
220cd 73 65 72 69 61 6c 5f 74 79 70 65 29 20 29 3b 0a  serial_type) );.
220ce 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
220cf 2d 3e 6e 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20  ->n<=nBuf );.   
220d0 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a   len = pMem->n;.
220d1 20 20 20 20 6d 65 6d 63 70 79 28 62 75 66 2c 20      memcpy(buf, 
220d2 70 4d 65 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b 0a 20  pMem->z, len);. 
220d3 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61     if( pMem->fla
220d4 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b  gs & MEM_Zero ){
220d5 0a 20 20 20 20 20 20 6c 65 6e 20 2b 3d 20 70 4d  .      len += pM
220d6 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20  em->u.nZero;.   
220d7 20 20 20 61 73 73 65 72 74 28 20 6e 42 75 66 3e     assert( nBuf>
220d8 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =0 );.      if( 
220d9 6c 65 6e 20 3e 20 28 75 33 32 29 6e 42 75 66 20  len > (u32)nBuf 
220da 29 7b 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 3d  ){.        len =
220db 20 28 75 33 32 29 6e 42 75 66 3b 0a 20 20 20 20   (u32)nBuf;.    
220dc 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74    }.      memset
220dd 28 26 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20  (&buf[pMem->n], 
220de 30 2c 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b  0, len-pMem->n);
220df 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
220e0 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  n len;.  }..  /*
220e1 20 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e   NULL or constan
220e2 74 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72  ts 0 or 1 */.  r
220e3 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
220e4 2a 20 44 65 73 65 72 69 61 6c 69 7a 65 20 74 68  * Deserialize th
220e5 65 20 64 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e  e data blob poin
220e6 74 65 64 20 74 6f 20 62 79 20 62 75 66 20 61 73  ted to by buf as
220e7 20 73 65 72 69 61 6c 20 74 79 70 65 20 73 65 72   serial type ser
220e8 69 61 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20  ial_type.** and 
220e9 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
220ea 20 69 6e 20 70 4d 65 6d 2e 20 20 52 65 74 75 72   in pMem.  Retur
220eb 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
220ec 62 79 74 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a  bytes read..*/ .
220ed 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
220ee 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  32 sqlite3VdbeSe
220ef 72 69 61 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74  rialGet(.  const
220f0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
220f1 62 75 66 2c 20 20 20 20 20 2f 2a 20 42 75 66 66  buf,     /* Buff
220f2 65 72 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a  er to deserializ
220f3 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20  e from */.  u32 
220f4 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 20 20 20  serial_type,    
220f5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 72            /* Ser
220f6 69 61 6c 20 74 79 70 65 20 74 6f 20 64 65 73 65  ial type to dese
220f7 72 69 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d  rialize */.  Mem
220f8 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 20   *pMem          
220f9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65             /* Me
220fa 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 77 72 69  mory cell to wri
220fb 74 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f  te value into */
220fc 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 20 73 65  .){.  switch( se
220fd 72 69 61 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20  rial_type ){.   
220fe 20 63 61 73 65 20 31 30 3a 20 20 20 2f 2a 20 52   case 10:   /* R
220ff 65 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75  eserved for futu
22100 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61  re use */.    ca
22101 73 65 20 31 31 3a 20 20 20 2f 2a 20 52 65 73 65  se 11:   /* Rese
22102 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20  rved for future 
22103 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  use */.    case 
22104 30 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f  0: {  /* NULL */
22105 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
22106 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
22107 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
22108 7d 0a 20 20 20 20 63 61 73 65 20 31 3a 20 7b 20  }.    case 1: { 
22109 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 1-byte signed
2210a 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20   integer */.    
2210b 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73    pMem->u.i = (s
2210c 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30  igned char)buf[0
2210d 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  ];.      pMem->f
2210e 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
2210f 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
22110 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 32      }.    case 2
22111 3a 20 7b 20 2f 2a 20 32 2d 62 79 74 65 20 73 69  : { /* 2-byte si
22112 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
22113 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20        pMem->u.i 
22114 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72  = (((signed char
22115 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62  )buf[0])<<8) | b
22116 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65  uf[1];.      pMe
22117 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  m->flags = MEM_I
22118 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  nt;.      return
22119 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   2;.    }.    ca
2211a 73 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74  se 3: { /* 3-byt
2211b 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  e signed integer
2211c 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   */.      pMem->
2211d 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20  u.i = (((signed 
2211e 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36  char)buf[0])<<16
2211f 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20  ) | (buf[1]<<8) 
22120 7c 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20  | buf[2];.      
22121 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
22122 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74  M_Int;.      ret
22123 75 72 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 3;.    }.   
22124 20 63 61 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d   case 4: { /* 4-
22125 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65  byte signed inte
22126 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65  ger */.      pMe
22127 6d 2d 3e 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d  m->u.i = (buf[0]
22128 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c  <<24) | (buf[1]<
22129 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c  <16) | (buf[2]<<
2212a 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20  8) | buf[3];.   
2212b 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
2212c 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
2212d 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a  return 4;.    }.
2212e 20 20 20 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a      case 5: { /*
2212f 20 36 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69   6-byte signed i
22130 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20  nteger */.      
22131 75 36 34 20 78 20 3d 20 28 28 28 73 69 67 6e 65  u64 x = (((signe
22132 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c  d char)buf[0])<<
22133 38 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20  8) | buf[1];.   
22134 20 20 20 75 33 32 20 79 20 3d 20 28 62 75 66 5b     u32 y = (buf[
22135 32 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 33  2]<<24) | (buf[3
22136 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 34 5d  ]<<16) | (buf[4]
22137 3c 3c 38 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20  <<8) | buf[5];. 
22138 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29       x = (x<<32)
22139 20 7c 20 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d   | y;.      pMem
2213a 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26  ->u.i = *(i64*)&
2213b 78 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  x;.      pMem->f
2213c 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
2213d 20 20 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a        return 6;.
2213e 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 36      }.    case 6
2213f 3a 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69  :   /* 8-byte si
22140 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
22141 20 20 20 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a      case 7: { /*
22142 20 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70   IEEE floating p
22143 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75 36  oint */.      u6
22144 34 20 78 3b 0a 20 20 20 20 20 20 75 33 32 20 79  4 x;.      u32 y
22145 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e  ;.#if !defined(N
22146 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e  DEBUG) && !defin
22147 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ed(SQLITE_OMIT_F
22148 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20  LOATING_POINT). 
22149 20 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74       /* Verify t
2214a 68 61 74 20 69 6e 74 65 67 65 72 73 20 61 6e 64  hat integers and
2214b 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
2214c 76 61 6c 75 65 73 20 75 73 65 20 74 68 65 20 73  values use the s
2214d 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74  ame.      ** byt
2214e 65 20 6f 72 64 65 72 2e 20 20 4f 72 2c 20 74 68  e order.  Or, th
2214f 61 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 49 58  at if SQLITE_MIX
22150 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f  ED_ENDIAN_64BIT_
22151 46 4c 4f 41 54 20 69 73 0a 20 20 20 20 20 20 2a  FLOAT is.      *
22152 2a 20 64 65 66 69 6e 65 64 20 74 68 61 74 20 36  * defined that 6
22153 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70  4-bit floating p
22154 6f 69 6e 74 20 76 61 6c 75 65 73 20 72 65 61 6c  oint values real
22155 6c 79 20 61 72 65 20 6d 69 78 65 64 0a 20 20 20  ly are mixed.   
22156 20 20 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20     ** endian..  
22157 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61      */.      sta
22158 74 69 63 20 63 6f 6e 73 74 20 75 36 34 20 74 31  tic const u64 t1
22159 20 3d 20 28 28 75 36 34 29 30 78 33 66 66 30 30   = ((u64)0x3ff00
2215a 30 30 30 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20  000)<<32;.      
2215b 73 74 61 74 69 63 20 63 6f 6e 73 74 20 64 6f 75  static const dou
2215c 62 6c 65 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20  ble r1 = 1.0;.  
2215d 20 20 20 20 75 36 34 20 74 32 20 3d 20 74 31 3b      u64 t2 = t1;
2215e 0a 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64  .      swapMixed
2215f 45 6e 64 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b  EndianFloat(t2);
22160 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73  .      assert( s
22161 69 7a 65 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f  izeof(r1)==sizeo
22162 66 28 74 32 29 20 26 26 20 6d 65 6d 63 6d 70 28  f(t2) && memcmp(
22163 26 72 31 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66  &r1, &t2, sizeof
22164 28 72 31 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64  (r1))==0 );.#end
22165 69 66 0a 0a 20 20 20 20 20 20 78 20 3d 20 28 62  if..      x = (b
22166 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75  uf[0]<<24) | (bu
22167 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66  f[1]<<16) | (buf
22168 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d  [2]<<8) | buf[3]
22169 3b 0a 20 20 20 20 20 20 79 20 3d 20 28 62 75 66  ;.      y = (buf
2216a 5b 34 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b  [4]<<24) | (buf[
2216b 35 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 36  5]<<16) | (buf[6
2216c 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a  ]<<8) | buf[7];.
2216d 20 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32        x = (x<<32
2216e 29 20 7c 20 79 3b 0a 20 20 20 20 20 20 69 66 28  ) | y;.      if(
2216f 20 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20   serial_type==6 
22170 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d  ){.        pMem-
22171 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78  >u.i = *(i64*)&x
22172 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  ;.        pMem->
22173 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
22174 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
22175 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69        assert( si
22176 7a 65 6f 66 28 78 29 3d 3d 38 20 26 26 20 73 69  zeof(x)==8 && si
22177 7a 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38  zeof(pMem->r)==8
22178 20 29 3b 0a 20 20 20 20 20 20 20 20 73 77 61 70   );.        swap
22179 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74  MixedEndianFloat
2217a 28 78 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  (x);.        mem
2217b 63 70 79 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78  cpy(&pMem->r, &x
2217c 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20  , sizeof(x));.  
2217d 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
2217e 73 20 3d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e  s = sqlite3IsNaN
2217f 28 70 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f  (pMem->r) ? MEM_
22180 4e 75 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b  Null : MEM_Real;
22181 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
22182 65 74 75 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20  eturn 8;.    }. 
22183 20 20 20 63 61 73 65 20 38 3a 20 20 20 20 2f 2a     case 8:    /*
22184 20 49 6e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20   Integer 0 */.  
22185 20 20 63 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20    case 9: {  /* 
22186 49 6e 74 65 67 65 72 20 31 20 2a 2f 0a 20 20 20  Integer 1 */.   
22187 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73     pMem->u.i = s
22188 65 72 69 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20  erial_type-8;.  
22189 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
2218a 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20  = MEM_Int;.     
2218b 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
2218c 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
2218d 20 20 20 20 20 20 75 33 32 20 6c 65 6e 20 3d 20        u32 len = 
2218e 28 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29  (serial_type-12)
2218f 2f 32 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  /2;.      pMem->
22190 7a 20 3d 20 28 63 68 61 72 20 2a 29 62 75 66 3b  z = (char *)buf;
22191 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d  .      pMem->n =
22192 20 6c 65 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d   len;.      pMem
22193 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20  ->xDel = 0;.    
22194 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70    if( serial_typ
22195 65 26 30 78 30 31 20 29 7b 0a 20 20 20 20 20 20  e&0x01 ){.      
22196 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
22197 4d 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70  MEM_Str | MEM_Ep
22198 68 65 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  hem;.      }else
22199 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  {.        pMem->
2219a 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62  flags = MEM_Blob
2219b 20 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20   | MEM_Ephem;.  
2219c 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
2219d 72 6e 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20  rn len;.    }.  
2219e 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2219f 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
221a0 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 61  ine is used to a
221a1 6c 6c 6f 63 61 74 65 20 73 75 66 66 69 63 69 65  llocate sufficie
221a2 6e 74 20 73 70 61 63 65 20 66 6f 72 20 61 6e 20  nt space for an 
221a3 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 0a 2a  UnpackedRecord.*
221a4 2a 20 73 74 72 75 63 74 75 72 65 20 6c 61 72 67  * structure larg
221a5 65 20 65 6e 6f 75 67 68 20 74 6f 20 62 65 20 75  e enough to be u
221a6 73 65 64 20 77 69 74 68 20 73 71 6c 69 74 65 33  sed with sqlite3
221a7 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b  VdbeRecordUnpack
221a8 28 29 20 69 66 0a 2a 2a 20 74 68 65 20 66 69 72  () if.** the fir
221a9 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  st argument is a
221aa 20 70 6f 69 6e 74 65 72 20 74 6f 20 4b 65 79 49   pointer to KeyI
221ab 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 70 4b  nfo structure pK
221ac 65 79 49 6e 66 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68  eyInfo..**.** Th
221ad 65 20 73 70 61 63 65 20 69 73 20 65 69 74 68 65  e space is eithe
221ae 72 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e  r allocated usin
221af 67 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  g sqlite3DbMallo
221b0 63 52 61 77 28 29 20 6f 72 20 66 72 6f 6d 20 77  cRaw() or from w
221b1 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 75 6e 61  ithin.** the una
221b2 6c 69 67 6e 65 64 20 62 75 66 66 65 72 20 70 61  ligned buffer pa
221b3 73 73 65 64 20 76 69 61 20 74 68 65 20 73 65 63  ssed via the sec
221b4 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20 61 72  ond and third ar
221b5 67 75 6d 65 6e 74 73 20 28 70 72 65 73 75 6d 61  guments (presuma
221b6 62 6c 79 0a 2a 2a 20 73 74 61 63 6b 20 73 70 61  bly.** stack spa
221b7 63 65 29 2e 20 49 66 20 74 68 65 20 66 6f 72 6d  ce). If the form
221b8 65 72 2c 20 74 68 65 6e 20 2a 70 70 46 72 65 65  er, then *ppFree
221b9 20 69 73 20 73 65 74 20 74 6f 20 61 20 70 6f 69   is set to a poi
221ba 6e 74 65 72 20 74 68 61 74 20 73 68 6f 75 6c 64  nter that should
221bb 0a 2a 2a 20 62 65 20 65 76 65 6e 74 75 61 6c 6c  .** be eventuall
221bc 79 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63  y freed by the c
221bd 61 6c 6c 65 72 20 75 73 69 6e 67 20 73 71 6c 69  aller using sqli
221be 74 65 33 44 62 46 72 65 65 28 29 2e 20 4f 72 2c  te3DbFree(). Or,
221bf 20 69 66 20 74 68 65 20 0a 2a 2a 20 61 6c 6c 6f   if the .** allo
221c0 63 61 74 69 6f 6e 20 63 6f 6d 65 73 20 66 72 6f  cation comes fro
221c1 6d 20 74 68 65 20 70 53 70 61 63 65 2f 73 7a 53  m the pSpace/szS
221c2 70 61 63 65 20 62 75 66 66 65 72 2c 20 2a 70 70  pace buffer, *pp
221c3 46 72 65 65 20 69 73 20 73 65 74 20 74 6f 20 4e  Free is set to N
221c4 55 4c 4c 0a 2a 2a 20 62 65 66 6f 72 65 20 72 65  ULL.** before re
221c5 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49  turning..**.** I
221c6 66 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f  f an OOM error o
221c7 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72  ccurs, NULL is r
221c8 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
221c9 54 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63  TE_PRIVATE Unpac
221ca 6b 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74  kedRecord *sqlit
221cb 65 33 56 64 62 65 41 6c 6c 6f 63 55 6e 70 61 63  e3VdbeAllocUnpac
221cc 6b 65 64 52 65 63 6f 72 64 28 0a 20 20 4b 65 79  kedRecord(.  Key
221cd 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20  Info *pKeyInfo, 
221ce 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
221cf 44 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74  Description of t
221d0 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63  he record */.  c
221d1 68 61 72 20 2a 70 53 70 61 63 65 2c 20 20 20 20  har *pSpace,    
221d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
221d3 2a 20 55 6e 61 6c 69 67 6e 65 64 20 73 70 61 63  * Unaligned spac
221d4 65 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20  e available */. 
221d5 20 69 6e 74 20 73 7a 53 70 61 63 65 2c 20 20 20   int szSpace,   
221d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
221d7 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53 70 61   /* Size of pSpa
221d8 63 65 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f  ce[] in bytes */
221d9 0a 20 20 63 68 61 72 20 2a 2a 70 70 46 72 65 65  .  char **ppFree
221da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
221db 20 20 20 2f 2a 20 4f 55 54 3a 20 43 61 6c 6c 65     /* OUT: Calle
221dc 72 20 73 68 6f 75 6c 64 20 66 72 65 65 20 74 68  r should free th
221dd 69 73 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 29 7b  is pointer */.){
221de 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
221df 64 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  d *p;           
221e0 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 72     /* Unpacked r
221e1 65 63 6f 72 64 20 74 6f 20 72 65 74 75 72 6e 20  ecord to return 
221e2 2a 2f 0a 20 20 69 6e 74 20 6e 4f 66 66 3b 20 20  */.  int nOff;  
221e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
221e4 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e       /* Incremen
221e5 74 20 70 53 70 61 63 65 20 62 79 20 6e 4f 66 66  t pSpace by nOff
221e6 20 74 6f 20 61 6c 69 67 6e 20 69 74 20 2a 2f 0a   to align it */.
221e7 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20    int nByte;    
221e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
221e9 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
221ea 79 74 65 73 20 72 65 71 75 69 72 65 64 20 66 6f  ytes required fo
221eb 72 20 2a 70 20 2a 2f 0a 0a 20 20 2f 2a 20 57 65  r *p */..  /* We
221ec 20 77 61 6e 74 20 74 6f 20 73 68 69 66 74 20 74   want to shift t
221ed 68 65 20 70 6f 69 6e 74 65 72 20 70 53 70 61 63  he pointer pSpac
221ee 65 20 75 70 20 73 75 63 68 20 74 68 61 74 20 69  e up such that i
221ef 74 20 69 73 20 38 2d 62 79 74 65 20 61 6c 69 67  t is 8-byte alig
221f0 6e 65 64 2e 0a 20 20 2a 2a 20 54 68 75 73 2c 20  ned..  ** Thus, 
221f1 77 65 20 6e 65 65 64 20 74 6f 20 63 61 6c 63 75  we need to calcu
221f2 6c 61 74 65 20 61 20 76 61 6c 75 65 2c 20 6e 4f  late a value, nO
221f3 66 66 2c 20 62 65 74 77 65 65 6e 20 30 20 61 6e  ff, between 0 an
221f4 64 20 37 2c 20 74 6f 20 73 68 69 66 74 20 0a 20  d 7, to shift . 
221f5 20 2a 2a 20 69 74 20 62 79 2e 20 20 49 66 20 70   ** it by.  If p
221f6 53 70 61 63 65 20 69 73 20 61 6c 72 65 61 64 79  Space is already
221f7 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 2c   8-byte aligned,
221f8 20 6e 4f 66 66 20 73 68 6f 75 6c 64 20 62 65 20   nOff should be 
221f9 7a 65 72 6f 2e 0a 20 20 2a 2f 0a 20 20 6e 4f 66  zero..  */.  nOf
221fa 66 20 3d 20 28 38 20 2d 20 28 53 51 4c 49 54 45  f = (8 - (SQLITE
221fb 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 53 70 61  _PTR_TO_INT(pSpa
221fc 63 65 29 20 26 20 37 29 29 20 26 20 37 3b 0a 20  ce) & 7)) & 7;. 
221fd 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28   nByte = ROUND8(
221fe 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52  sizeof(UnpackedR
221ff 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66  ecord)) + sizeof
22200 28 4d 65 6d 29 2a 28 70 4b 65 79 49 6e 66 6f 2d  (Mem)*(pKeyInfo-
22201 3e 6e 46 69 65 6c 64 2b 31 29 3b 0a 20 20 69 66  >nField+1);.  if
22202 28 20 6e 42 79 74 65 3e 73 7a 53 70 61 63 65 2b  ( nByte>szSpace+
22203 6e 4f 66 66 20 29 7b 0a 20 20 20 20 70 20 3d 20  nOff ){.    p = 
22204 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20  (UnpackedRecord 
22205 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
22206 63 52 61 77 28 70 4b 65 79 49 6e 66 6f 2d 3e 64  cRaw(pKeyInfo->d
22207 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 2a  b, nByte);.    *
22208 70 70 46 72 65 65 20 3d 20 28 63 68 61 72 20 2a  ppFree = (char *
22209 29 70 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29  )p;.    if( !p )
2220a 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c   return 0;.  }el
2220b 73 65 7b 0a 20 20 20 20 70 20 3d 20 28 55 6e 70  se{.    p = (Unp
2220c 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 26 70 53  ackedRecord*)&pS
2220d 70 61 63 65 5b 6e 4f 66 66 5d 3b 0a 20 20 20 20  pace[nOff];.    
2220e 2a 70 70 46 72 65 65 20 3d 20 30 3b 0a 20 20 7d  *ppFree = 0;.  }
2220f 0a 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 28 4d  ..  p->aMem = (M
22210 65 6d 2a 29 26 28 28 63 68 61 72 2a 29 70 29 5b  em*)&((char*)p)[
22211 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e  ROUND8(sizeof(Un
22212 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 5d 3b  packedRecord))];
22213 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 49  .  assert( pKeyI
22214 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 21  nfo->aSortOrder!
22215 3d 30 20 29 3b 0a 20 20 70 2d 3e 70 4b 65 79 49  =0 );.  p->pKeyI
22216 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a  nfo = pKeyInfo;.
22217 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4b    p->nField = pK
22218 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b  eyInfo->nField +
22219 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a   1;.  return p;.
2221a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74  }../*.** Given t
2221b 68 65 20 6e 4b 65 79 2d 62 79 74 65 20 65 6e 63  he nKey-byte enc
2221c 6f 64 69 6e 67 20 6f 66 20 61 20 72 65 63 6f 72  oding of a recor
2221d 64 20 69 6e 20 70 4b 65 79 5b 5d 2c 20 70 6f 70  d in pKey[], pop
2221e 75 6c 61 74 65 20 74 68 65 20 0a 2a 2a 20 55 6e  ulate the .** Un
2221f 70 61 63 6b 65 64 52 65 63 6f 72 64 20 73 74 72  packedRecord str
22220 75 63 74 75 72 65 20 69 6e 64 69 63 61 74 65 64  ucture indicated
22221 20 62 79 20 74 68 65 20 66 6f 75 72 74 68 20 61   by the fourth a
22222 72 67 75 6d 65 6e 74 20 77 69 74 68 20 74 68 65  rgument with the
22223 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  .** contents of 
22224 74 68 65 20 64 65 63 6f 64 65 64 20 72 65 63 6f  the decoded reco
22225 72 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50  rd..*/ .SQLITE_P
22226 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
22227 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70  te3VdbeRecordUnp
22228 61 63 6b 28 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  ack(.  KeyInfo *
22229 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 2f 2a  pKeyInfo,     /*
2222a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   Information abo
2222b 75 74 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f  ut the record fo
2222c 72 6d 61 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b  rmat */.  int nK
2222d 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ey,             
2222e 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
2222f 62 69 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f  binary record */
22230 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  .  const void *p
22231 4b 65 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  Key,      /* The
22232 20 62 69 6e 61 72 79 20 72 65 63 6f 72 64 20 2a   binary record *
22233 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f  /.  UnpackedReco
22234 72 64 20 2a 70 20 20 20 20 20 20 2f 2a 20 50 6f  rd *p      /* Po
22235 70 75 6c 61 74 65 20 74 68 69 73 20 73 74 72 75  pulate this stru
22236 63 74 75 72 65 20 62 65 66 6f 72 65 20 72 65 74  cture before ret
22237 75 72 6e 69 6e 67 2e 20 2a 2f 0a 29 7b 0a 20 20  urning. */.){.  
22238 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
22239 68 61 72 20 2a 61 4b 65 79 20 3d 20 28 63 6f 6e  har *aKey = (con
2223a 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
2223b 20 2a 29 70 4b 65 79 3b 0a 20 20 69 6e 74 20 64   *)pKey;.  int d
2223c 3b 20 0a 20 20 75 33 32 20 69 64 78 3b 20 20 20  ; .  u32 idx;   
2223d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2223e 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
2223f 6e 20 61 4b 65 79 5b 5d 20 74 6f 20 72 65 61 64  n aKey[] to read
22240 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 31 36 20 75   from */.  u16 u
22241 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22242 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e             /* Un
22243 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e  signed loop coun
22244 74 65 72 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48  ter */.  u32 szH
22245 64 72 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20  dr;.  Mem *pMem 
22246 3d 20 70 2d 3e 61 4d 65 6d 3b 0a 0a 20 20 70 2d  = p->aMem;..  p-
22247 3e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 61 73  >flags = 0;.  as
22248 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45  sert( EIGHT_BYTE
22249 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29  _ALIGNMENT(pMem)
2224a 20 29 3b 0a 20 20 69 64 78 20 3d 20 67 65 74 56   );.  idx = getV
2224b 61 72 69 6e 74 33 32 28 61 4b 65 79 2c 20 73 7a  arint32(aKey, sz
2224c 48 64 72 29 3b 0a 20 20 64 20 3d 20 73 7a 48 64  Hdr);.  d = szHd
2224d 72 3b 0a 20 20 75 20 3d 20 30 3b 0a 20 20 77 68  r;.  u = 0;.  wh
2224e 69 6c 65 28 20 69 64 78 3c 73 7a 48 64 72 20 26  ile( idx<szHdr &
2224f 26 20 75 3c 70 2d 3e 6e 46 69 65 6c 64 20 26 26  & u<p->nField &&
22250 20 64 3c 3d 6e 4b 65 79 20 29 7b 0a 20 20 20 20   d<=nKey ){.    
22251 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b  u32 serial_type;
22252 0a 0a 20 20 20 20 69 64 78 20 2b 3d 20 67 65 74  ..    idx += get
22253 56 61 72 69 6e 74 33 32 28 26 61 4b 65 79 5b 69  Varint32(&aKey[i
22254 64 78 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65  dx], serial_type
22255 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63  );.    pMem->enc
22256 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63   = pKeyInfo->enc
22257 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 64 62 20 3d  ;.    pMem->db =
22258 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20   pKeyInfo->db;. 
22259 20 20 20 2f 2a 20 70 4d 65 6d 2d 3e 66 6c 61 67     /* pMem->flag
2225a 73 20 3d 20 30 3b 20 2f 2f 20 73 71 6c 69 74 65  s = 0; // sqlite
2225b 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 29  3VdbeSerialGet()
2225c 20 77 69 6c 6c 20 73 65 74 20 74 68 69 73 20 66   will set this f
2225d 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 70 4d 65  or us */.    pMe
2225e 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a  m->zMalloc = 0;.
2225f 20 20 20 20 64 20 2b 3d 20 73 71 6c 69 74 65 33      d += sqlite3
22260 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 61  VdbeSerialGet(&a
22261 4b 65 79 5b 64 5d 2c 20 73 65 72 69 61 6c 5f 74  Key[d], serial_t
22262 79 70 65 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20  ype, pMem);.    
22263 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 75 2b 2b 3b  pMem++;.    u++;
22264 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 75  .  }.  assert( u
22265 3c 3d 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  <=pKeyInfo->nFie
22266 6c 64 20 2b 20 31 20 29 3b 0a 20 20 70 2d 3e 6e  ld + 1 );.  p->n
22267 46 69 65 6c 64 20 3d 20 75 3b 0a 7d 0a 0a 2f 2a  Field = u;.}../*
22268 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
22269 6e 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20 74  n compares the t
2226a 77 6f 20 74 61 62 6c 65 20 72 6f 77 73 20 6f 72  wo table rows or
2226b 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 0a 2a   index records.*
2226c 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 7b  * specified by {
2226d 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20 61 6e  nKey1, pKey1} an
2226e 64 20 70 50 4b 65 79 32 2e 20 20 49 74 20 72 65  d pPKey2.  It re
2226f 74 75 72 6e 73 20 61 20 6e 65 67 61 74 69 76 65  turns a negative
22270 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 70 6f 73  , zero.** or pos
22271 69 74 69 76 65 20 69 6e 74 65 67 65 72 20 69 66  itive integer if
22272 20 6b 65 79 31 20 69 73 20 6c 65 73 73 20 74 68   key1 is less th
22273 61 6e 2c 20 65 71 75 61 6c 20 74 6f 20 6f 72 20  an, equal to or 
22274 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e  .** greater than
22275 20 6b 65 79 32 2e 20 20 54 68 65 20 7b 6e 4b 65   key2.  The {nKe
22276 79 31 2c 20 70 4b 65 79 31 7d 20 6b 65 79 20 6d  y1, pKey1} key m
22277 75 73 74 20 62 65 20 61 20 62 6c 6f 62 0a 2a 2a  ust be a blob.**
22278 20 63 72 65 61 74 65 64 20 62 79 20 74 68 20 4f   created by th O
22279 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63  P_MakeRecord opc
2227a 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 2e  ode of the VDBE.
2227b 20 20 54 68 65 20 70 50 4b 65 79 32 0a 2a 2a 20    The pPKey2.** 
2227c 6b 65 79 20 6d 75 73 74 20 62 65 20 61 20 70 61  key must be a pa
2227d 72 73 65 64 20 6b 65 79 20 73 75 63 68 20 61 73  rsed key such as
2227e 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
2227f 2a 20 73 71 6c 69 74 65 33 56 64 62 65 50 61 72  * sqlite3VdbePar
22280 73 65 52 65 63 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20  seRecord..**.** 
22281 4b 65 79 31 20 61 6e 64 20 4b 65 79 32 20 64 6f  Key1 and Key2 do
22282 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 63 6f 6e   not have to con
22283 74 61 69 6e 20 74 68 65 20 73 61 6d 65 20 6e 75  tain the same nu
22284 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 2e 0a  mber of fields..
22285 2a 2a 20 54 68 65 20 6b 65 79 20 77 69 74 68 20  ** The key with 
22286 66 65 77 65 72 20 66 69 65 6c 64 73 20 69 73 20  fewer fields is 
22287 75 73 75 61 6c 6c 79 20 63 6f 6d 70 61 72 65 73  usually compares
22288 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 0a   less than the .
22289 2a 2a 20 6c 6f 6e 67 65 72 20 6b 65 79 2e 20 20  ** longer key.  
2228a 48 6f 77 65 76 65 72 20 69 66 20 74 68 65 20 55  However if the U
2228b 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20  NPACKED_INCRKEY 
2228c 66 6c 61 67 73 20 69 6e 20 70 50 4b 65 79 32 20  flags in pPKey2 
2228d 69 73 20 73 65 74 0a 2a 2a 20 61 6e 64 20 74 68  is set.** and th
2228e 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 65  e common prefixe
2228f 73 20 61 72 65 20 65 71 75 61 6c 2c 20 74 68 65  s are equal, the
22290 6e 20 6b 65 79 31 20 69 73 20 6c 65 73 73 20 74  n key1 is less t
22291 68 61 6e 20 6b 65 79 32 2e 0a 2a 2a 20 4f 72 20  han key2..** Or 
22292 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f  if the UNPACKED_
22293 4d 41 54 43 48 5f 50 52 45 46 49 58 20 66 6c 61  MATCH_PREFIX fla
22294 67 20 69 73 20 73 65 74 20 61 6e 64 20 74 68 65  g is set and the
22295 20 70 72 65 66 69 78 65 73 20 61 72 65 0a 2a 2a   prefixes are.**
22296 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 74 68 65   equal, then the
22297 20 6b 65 79 73 20 61 72 65 20 63 6f 6e 73 69 64   keys are consid
22298 65 72 65 64 20 74 6f 20 62 65 20 65 71 75 61 6c  ered to be equal
22299 20 61 6e 64 0a 2a 2a 20 74 68 65 20 70 61 72 74   and.** the part
2229a 73 20 62 65 79 6f 6e 64 20 74 68 65 20 63 6f 6d  s beyond the com
2229b 6d 6f 6e 20 70 72 65 66 69 78 20 61 72 65 20 69  mon prefix are i
2229c 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  gnored..*/.SQLIT
2229d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2229e 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43  lite3VdbeRecordC
2229f 6f 6d 70 61 72 65 28 0a 20 20 69 6e 74 20 6e 4b  ompare(.  int nK
222a0 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  ey1, const void 
222a1 2a 70 4b 65 79 31 2c 20 2f 2a 20 4c 65 66 74 20  *pKey1, /* Left 
222a2 6b 65 79 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65  key */.  Unpacke
222a3 64 52 65 63 6f 72 64 20 2a 70 50 4b 65 79 32 20  dRecord *pPKey2 
222a4 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20         /* Right 
222a5 6b 65 79 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  key */.){.  int 
222a6 64 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  d1;            /
222a7 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 4b  * Offset into aK
222a8 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 64 61 74  ey[] of next dat
222a9 61 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75  a element */.  u
222aa 33 32 20 69 64 78 31 3b 20 20 20 20 20 20 20 20  32 idx1;        
222ab 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f    /* Offset into
222ac 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20   aKey[] of next 
222ad 68 65 61 64 65 72 20 65 6c 65 6d 65 6e 74 20 2a  header element *
222ae 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 31 3b 20  /.  u32 szHdr1; 
222af 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
222b0 20 6f 66 20 62 79 74 65 73 20 69 6e 20 68 65 61   of bytes in hea
222b1 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d  der */.  int i =
222b2 20 30 3b 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64   0;.  int nField
222b3 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a  ;.  int rc = 0;.
222b4 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
222b5 20 63 68 61 72 20 2a 61 4b 65 79 31 20 3d 20 28   char *aKey1 = (
222b6 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
222b7 68 61 72 20 2a 29 70 4b 65 79 31 3b 0a 20 20 4b  har *)pKey1;.  K
222b8 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
222b9 3b 0a 20 20 4d 65 6d 20 6d 65 6d 31 3b 0a 0a 20  ;.  Mem mem1;.. 
222ba 20 70 4b 65 79 49 6e 66 6f 20 3d 20 70 50 4b 65   pKeyInfo = pPKe
222bb 79 32 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  y2->pKeyInfo;.  
222bc 6d 65 6d 31 2e 65 6e 63 20 3d 20 70 4b 65 79 49  mem1.enc = pKeyI
222bd 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 6d 65 6d 31  nfo->enc;.  mem1
222be 2e 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e  .db = pKeyInfo->
222bf 64 62 3b 0a 20 20 2f 2a 20 6d 65 6d 31 2e 66 6c  db;.  /* mem1.fl
222c0 61 67 73 20 3d 20 30 3b 20 20 2f 2f 20 57 69 6c  ags = 0;  // Wil
222c1 6c 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  l be initialized
222c2 20 62 79 20 73 71 6c 69 74 65 33 56 64 62 65 53   by sqlite3VdbeS
222c3 65 72 69 61 6c 47 65 74 28 29 20 2a 2f 0a 20 20  erialGet() */.  
222c4 56 56 41 5f 4f 4e 4c 59 28 20 6d 65 6d 31 2e 7a  VVA_ONLY( mem1.z
222c5 4d 61 6c 6c 6f 63 20 3d 20 30 3b 20 29 20 2f 2a  Malloc = 0; ) /*
222c6 20 4f 6e 6c 79 20 6e 65 65 64 65 64 20 62 79 20   Only needed by 
222c7 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
222c8 6e 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20 43 6f 6d  nts */..  /* Com
222c9 70 69 6c 65 72 73 20 6d 61 79 20 63 6f 6d 70 6c  pilers may compl
222ca 61 69 6e 20 74 68 61 74 20 6d 65 6d 31 2e 75 2e  ain that mem1.u.
222cb 69 20 69 73 20 70 6f 74 65 6e 74 69 61 6c 6c 79  i is potentially
222cc 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a   uninitialized..
222cd 20 20 2a 2a 20 57 65 20 63 6f 75 6c 64 20 69 6e    ** We could in
222ce 69 74 69 61 6c 69 7a 65 20 69 74 2c 20 61 73 20  itialize it, as 
222cf 73 68 6f 77 6e 20 68 65 72 65 2c 20 74 6f 20 73  shown here, to s
222d0 69 6c 65 6e 63 65 20 74 68 6f 73 65 20 63 6f 6d  ilence those com
222d1 70 6c 61 69 6e 74 73 2e 0a 20 20 2a 2a 20 42 75  plaints..  ** Bu
222d2 74 20 69 6e 20 66 61 63 74 2c 20 6d 65 6d 31 2e  t in fact, mem1.
222d3 75 2e 69 20 77 69 6c 6c 20 6e 65 76 65 72 20 61  u.i will never a
222d4 63 74 75 61 6c 6c 79 20 62 65 20 75 73 65 64 20  ctually be used 
222d5 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 61  uninitialized, a
222d6 6e 64 20 64 6f 69 6e 67 20 0a 20 20 2a 2a 20 74  nd doing .  ** t
222d7 68 65 20 75 6e 6e 65 63 65 73 73 61 72 79 20 69  he unnecessary i
222d8 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 68 61  nitialization ha
222d9 73 20 61 20 6d 65 61 73 75 72 61 62 6c 65 20 6e  s a measurable n
222da 65 67 61 74 69 76 65 20 70 65 72 66 6f 72 6d 61  egative performa
222db 6e 63 65 0a 20 20 2a 2a 20 69 6d 70 61 63 74 2c  nce.  ** impact,
222dc 20 73 69 6e 63 65 20 74 68 69 73 20 72 6f 75 74   since this rout
222dd 69 6e 65 20 69 73 20 61 20 76 65 72 79 20 68 69  ine is a very hi
222de 67 68 20 72 75 6e 6e 65 72 2e 20 20 41 6e 64 20  gh runner.  And 
222df 73 6f 2c 20 77 65 20 63 68 6f 6f 73 65 0a 20 20  so, we choose.  
222e0 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20 74 68 65  ** to ignore the
222e1 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e   compiler warnin
222e2 67 73 20 61 6e 64 20 6c 65 61 76 65 20 74 68 69  gs and leave thi
222e3 73 20 76 61 72 69 61 62 6c 65 20 75 6e 69 6e 69  s variable unini
222e4 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20  tialized..  */. 
222e5 20 2f 2a 20 20 6d 65 6d 31 2e 75 2e 69 20 3d 20   /*  mem1.u.i = 
222e6 30 3b 20 20 2f 2f 20 6e 6f 74 20 6e 65 65 64 65  0;  // not neede
222e7 64 2c 20 68 65 72 65 20 74 6f 20 73 69 6c 65 6e  d, here to silen
222e8 63 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e  ce compiler warn
222e9 69 6e 67 20 2a 2f 0a 20 20 0a 20 20 69 64 78 31  ing */.  .  idx1
222ea 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 61   = getVarint32(a
222eb 4b 65 79 31 2c 20 73 7a 48 64 72 31 29 3b 0a 20  Key1, szHdr1);. 
222ec 20 64 31 20 3d 20 73 7a 48 64 72 31 3b 0a 20 20   d1 = szHdr1;.  
222ed 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e 66  nField = pKeyInf
222ee 6f 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 61 73 73  o->nField;.  ass
222ef 65 72 74 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  ert( pKeyInfo->a
222f0 53 6f 72 74 4f 72 64 65 72 21 3d 30 20 29 3b 0a  SortOrder!=0 );.
222f1 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c 73 7a    while( idx1<sz
222f2 48 64 72 31 20 26 26 20 69 3c 70 50 4b 65 79 32  Hdr1 && i<pPKey2
222f3 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20  ->nField ){.    
222f4 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 31  u32 serial_type1
222f5 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74  ;..    /* Read t
222f6 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 20  he serial types 
222f7 66 6f 72 20 74 68 65 20 6e 65 78 74 20 65 6c 65  for the next ele
222f8 6d 65 6e 74 20 69 6e 20 65 61 63 68 20 6b 65 79  ment in each key
222f9 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31 20 2b 3d  . */.    idx1 +=
222fa 20 67 65 74 56 61 72 69 6e 74 33 32 28 20 61 4b   getVarint32( aK
222fb 65 79 31 2b 69 64 78 31 2c 20 73 65 72 69 61 6c  ey1+idx1, serial
222fc 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 20 69 66  _type1 );.    if
222fd 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 26 20 73  ( d1>=nKey1 && s
222fe 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
222ff 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74  TypeLen(serial_t
22300 79 70 65 31 29 3e 30 20 29 20 62 72 65 61 6b 3b  ype1)>0 ) break;
22301 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74  ..    /* Extract
22302 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f 20 62   the values to b
22303 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20 20 20 20  e compared..    
22304 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20 73 71 6c  */.    d1 += sql
22305 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65  ite3VdbeSerialGe
22306 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c 20 73 65  t(&aKey1[d1], se
22307 72 69 61 6c 5f 74 79 70 65 31 2c 20 26 6d 65 6d  rial_type1, &mem
22308 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74  1);..    /* Do t
22309 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 20 20  he comparison.  
2230a 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71    */.    rc = sq
2230b 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28  lite3MemCompare(
2230c 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79 32 2d 3e  &mem1, &pPKey2->
2230d 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20 20 20 20  aMem[i],.       
2230e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2230f 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 3f 20 70      i<nField ? p
22310 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69  KeyInfo->aColl[i
22311 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ] : 0);.    if( 
22312 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61  rc!=0 ){.      a
22313 73 73 65 72 74 28 20 6d 65 6d 31 2e 7a 4d 61 6c  ssert( mem1.zMal
22314 6c 6f 63 3d 3d 30 20 29 3b 20 20 2f 2a 20 53 65  loc==0 );  /* Se
22315 65 20 63 6f 6d 6d 65 6e 74 20 62 65 6c 6f 77 20  e comment below 
22316 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76  */..      /* Inv
22317 65 72 74 20 74 68 65 20 72 65 73 75 6c 74 20 69  ert the result i
22318 66 20 77 65 20 61 72 65 20 75 73 69 6e 67 20 44  f we are using D
22319 45 53 43 20 73 6f 72 74 20 6f 72 64 65 72 2e 20  ESC sort order. 
2231a 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 3c 6e  */.      if( i<n
2231b 46 69 65 6c 64 20 26 26 20 70 4b 65 79 49 6e 66  Field && pKeyInf
2231c 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  o->aSortOrder[i]
2231d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
2231e 20 2d 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20   -rc;.      }.  
2231f 20 20 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74    .      /* If t
22320 68 65 20 50 52 45 46 49 58 5f 53 45 41 52 43 48  he PREFIX_SEARCH
22321 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64   flag is set and
22322 20 61 6c 6c 20 66 69 65 6c 64 73 20 65 78 63 65   all fields exce
22323 70 74 20 74 68 65 20 66 69 6e 61 6c 0a 20 20 20  pt the final.   
22324 20 20 20 2a 2a 20 72 6f 77 69 64 20 66 69 65 6c     ** rowid fiel
22325 64 20 77 65 72 65 20 65 71 75 61 6c 2c 20 74 68  d were equal, th
22326 65 6e 20 63 6c 65 61 72 20 74 68 65 20 50 52 45  en clear the PRE
22327 46 49 58 5f 53 45 41 52 43 48 20 66 6c 61 67 20  FIX_SEARCH flag 
22328 61 6e 64 20 73 65 74 20 0a 20 20 20 20 20 20 2a  and set .      *
22329 2a 20 70 50 4b 65 79 32 2d 3e 72 6f 77 69 64 20  * pPKey2->rowid 
2232a 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  to the value of 
2232b 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64 20  the rowid field 
2232c 69 6e 20 28 70 4b 65 79 31 2c 20 6e 4b 65 79 31  in (pKey1, nKey1
2232d 29 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73  )..      ** This
2232e 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
2232f 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f 70 63 6f  OP_IsUnique opco
22330 64 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  de..      */.   
22331 20 20 20 69 66 28 20 28 70 50 4b 65 79 32 2d 3e     if( (pPKey2->
22332 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44  flags & UNPACKED
22333 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 29 20  _PREFIX_SEARCH) 
22334 26 26 20 69 3d 3d 28 70 50 4b 65 79 32 2d 3e 6e  && i==(pPKey2->n
22335 46 69 65 6c 64 2d 31 29 20 29 7b 0a 20 20 20 20  Field-1) ){.    
22336 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 31      assert( idx1
22337 3d 3d 73 7a 48 64 72 31 20 26 26 20 72 63 20 29  ==szHdr1 && rc )
22338 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
22339 28 20 6d 65 6d 31 2e 66 6c 61 67 73 20 26 20 4d  ( mem1.flags & M
2233a 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20  EM_Int );.      
2233b 20 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20    pPKey2->flags 
2233c 26 3d 20 7e 55 4e 50 41 43 4b 45 44 5f 50 52 45  &= ~UNPACKED_PRE
2233d 46 49 58 5f 53 45 41 52 43 48 3b 0a 20 20 20 20  FIX_SEARCH;.    
2233e 20 20 20 20 70 50 4b 65 79 32 2d 3e 72 6f 77 69      pPKey2->rowi
2233f 64 20 3d 20 6d 65 6d 31 2e 75 2e 69 3b 0a 20 20  d = mem1.u.i;.  
22340 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 20      }.    .     
22341 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
22342 7d 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a  }.    i++;.  }..
22343 20 20 2f 2a 20 4e 6f 20 6d 65 6d 6f 72 79 20 61    /* No memory a
22344 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 65 76 65  llocation is eve
22345 72 20 75 73 65 64 20 6f 6e 20 6d 65 6d 31 2e 20  r used on mem1. 
22346 20 50 72 6f 76 65 20 74 68 69 73 20 75 73 69 6e   Prove this usin
22347 67 0a 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f  g.  ** the follo
22348 77 69 6e 67 20 61 73 73 65 72 74 28 29 2e 20 20  wing assert().  
22349 49 66 20 74 68 65 20 61 73 73 65 72 74 28 29 20  If the assert() 
2234a 66 61 69 6c 73 2c 20 69 74 20 69 6e 64 69 63 61  fails, it indica
2234b 74 65 73 20 61 0a 20 20 2a 2a 20 6d 65 6d 6f 72  tes a.  ** memor
2234c 79 20 6c 65 61 6b 20 61 6e 64 20 61 20 6e 65 65  y leak and a nee
2234d 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65  d to call sqlite
2234e 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
2234f 26 6d 65 6d 31 29 2e 0a 20 20 2a 2f 0a 20 20 61  &mem1)..  */.  a
22350 73 73 65 72 74 28 20 6d 65 6d 31 2e 7a 4d 61 6c  ssert( mem1.zMal
22351 6c 6f 63 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  loc==0 );..  /* 
22352 72 63 3d 3d 30 20 68 65 72 65 20 6d 65 61 6e 73  rc==0 here means
22353 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65   that one of the
22354 20 6b 65 79 73 20 72 61 6e 20 6f 75 74 20 6f 66   keys ran out of
22355 20 66 69 65 6c 64 73 20 61 6e 64 0a 20 20 2a 2a   fields and.  **
22356 20 61 6c 6c 20 74 68 65 20 66 69 65 6c 64 73 20   all the fields 
22357 75 70 20 74 6f 20 74 68 61 74 20 70 6f 69 6e 74  up to that point
22358 20 77 65 72 65 20 65 71 75 61 6c 2e 20 49 66 20   were equal. If 
22359 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43  the UNPACKED_INC
2235a 52 4b 45 59 0a 20 20 2a 2a 20 66 6c 61 67 20 69  RKEY.  ** flag i
2235b 73 20 73 65 74 2c 20 74 68 65 6e 20 62 72 65 61  s set, then brea
2235c 6b 20 74 68 65 20 74 69 65 20 62 79 20 74 72 65  k the tie by tre
2235d 61 74 69 6e 67 20 6b 65 79 32 20 61 73 20 6c 61  ating key2 as la
2235e 72 67 65 72 2e 0a 20 20 2a 2a 20 49 66 20 74 68  rger..  ** If th
2235f 65 20 55 50 41 43 4b 45 44 5f 50 52 45 46 49 58  e UPACKED_PREFIX
22360 5f 4d 41 54 43 48 20 66 6c 61 67 20 69 73 20 73  _MATCH flag is s
22361 65 74 2c 20 74 68 65 6e 20 6b 65 79 73 20 77 69  et, then keys wi
22362 74 68 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78  th common prefix
22363 65 73 0a 20 20 2a 2a 20 61 72 65 20 63 6f 6e 73  es.  ** are cons
22364 69 64 65 72 65 64 20 74 6f 20 62 65 20 65 71 75  idered to be equ
22365 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20  al.  Otherwise, 
22366 74 68 65 20 6c 6f 6e 67 65 72 20 6b 65 79 20 69  the longer key i
22367 73 20 74 68 65 20 0a 20 20 2a 2a 20 6c 61 72 67  s the .  ** larg
22368 65 72 2e 20 20 41 73 20 69 74 20 68 61 70 70 65  er.  As it happe
22369 6e 73 2c 20 74 68 65 20 70 50 4b 65 79 32 20 77  ns, the pPKey2 w
2236a 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 74 68  ill always be th
2236b 65 20 6c 6f 6e 67 65 72 0a 20 20 2a 2a 20 69 66  e longer.  ** if
2236c 20 74 68 65 72 65 20 69 73 20 61 20 64 69 66 66   there is a diff
2236d 65 72 65 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20 61  erence..  */.  a
2236e 73 73 65 72 74 28 20 72 63 3d 3d 30 20 29 3b 0a  ssert( rc==0 );.
2236f 20 20 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c    if( pPKey2->fl
22370 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 49  ags & UNPACKED_I
22371 4e 43 52 4b 45 59 20 29 7b 0a 20 20 20 20 72 63  NCRKEY ){.    rc
22372 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 20 69   = -1;.  }else i
22373 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73  f( pPKey2->flags
22374 20 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46   & UNPACKED_PREF
22375 49 58 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20 20  IX_MATCH ){.    
22376 2f 2a 20 4c 65 61 76 65 20 72 63 3d 3d 30 20 2a  /* Leave rc==0 *
22377 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 64  /.  }else if( id
22378 78 31 3c 73 7a 48 64 72 31 20 29 7b 0a 20 20 20  x1<szHdr1 ){.   
22379 20 72 63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72   rc = 1;.  }.  r
2237a 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 20 0a 0a 2f  eturn rc;.}. ../
2237b 2a 0a 2a 2a 20 70 43 75 72 20 70 6f 69 6e 74 73  *.** pCur points
2237c 20 61 74 20 61 6e 20 69 6e 64 65 78 20 65 6e 74   at an index ent
2237d 72 79 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  ry created using
2237e 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f   the OP_MakeReco
2237f 72 64 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52 65  rd opcode..** Re
22380 61 64 20 74 68 65 20 72 6f 77 69 64 20 28 74 68  ad the rowid (th
22381 65 20 6c 61 73 74 20 66 69 65 6c 64 20 69 6e 20  e last field in 
22382 74 68 65 20 72 65 63 6f 72 64 29 20 61 6e 64 20  the record) and 
22383 73 74 6f 72 65 20 69 74 20 69 6e 20 2a 72 6f 77  store it in *row
22384 69 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  id..** Return SQ
22385 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79  LITE_OK if every
22386 74 68 69 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20  thing works, or 
22387 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74  an error code ot
22388 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 70  herwise..**.** p
22389 43 75 72 20 6d 69 67 68 74 20 62 65 20 70 6f 69  Cur might be poi
2238a 6e 74 69 6e 67 20 74 6f 20 74 65 78 74 20 6f 62  nting to text ob
2238b 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 20 63 6f  tained from a co
2238c 72 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66  rrupt database f
2238d 69 6c 65 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 63  ile..** So the c
2238e 6f 6e 74 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65  ontent cannot be
2238f 20 74 72 75 73 74 65 64 2e 20 20 44 6f 20 61 70   trusted.  Do ap
22390 70 72 6f 70 72 69 61 74 65 20 63 68 65 63 6b 73  propriate checks
22391 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e   on the content.
22392 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
22393 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
22394 62 65 49 64 78 52 6f 77 69 64 28 73 71 6c 69 74  beIdxRowid(sqlit
22395 65 33 20 2a 64 62 2c 20 42 74 43 75 72 73 6f 72  e3 *db, BtCursor
22396 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 72 6f 77   *pCur, i64 *row
22397 69 64 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c  id){.  i64 nCell
22398 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  Key = 0;.  int r
22399 63 3b 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 20  c;.  u32 szHdr; 
2239a 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
2239b 66 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a  f the header */.
2239c 20 20 75 33 32 20 74 79 70 65 52 6f 77 69 64 3b    u32 typeRowid;
2239d 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 79      /* Serial ty
2239e 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20  pe of the rowid 
2239f 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77 69  */.  u32 lenRowi
223a0 64 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f  d;     /* Size o
223a1 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20  f the rowid */. 
223a2 20 4d 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 55 4e   Mem m, v;..  UN
223a3 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64  USED_PARAMETER(d
223a4 62 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68  b);..  /* Get th
223a5 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e  e size of the in
223a6 64 65 78 20 65 6e 74 72 79 2e 20 20 4f 6e 6c 79  dex entry.  Only
223a7 20 69 6e 64 69 63 65 73 20 65 6e 74 72 69 65 73   indices entries
223a8 20 6f 66 20 6c 65 73 73 0a 20 20 2a 2a 20 74 68   of less.  ** th
223a9 61 6e 20 32 47 69 42 20 61 72 65 20 73 75 70 70  an 2GiB are supp
223aa 6f 72 74 20 2d 20 61 6e 79 74 68 69 6e 67 20 6c  ort - anything l
223ab 61 72 67 65 20 6d 75 73 74 20 62 65 20 64 61 74  arge must be dat
223ac 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e  abase corruption
223ad 2e 0a 20 20 2a 2a 20 41 6e 79 20 63 6f 72 72 75  ..  ** Any corru
223ae 70 74 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65  ption is detecte
223af 64 20 69 6e 20 73 71 6c 69 74 65 33 42 74 72 65  d in sqlite3Btre
223b0 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c  eParseCellPtr(),
223b1 20 74 68 6f 75 67 68 2c 20 73 6f 0a 20 20 2a 2a   though, so.  **
223b2 20 74 68 69 73 20 63 6f 64 65 20 63 61 6e 20 73   this code can s
223b3 61 66 65 6c 79 20 61 73 73 75 6d 65 20 74 68 61  afely assume tha
223b4 74 20 6e 43 65 6c 6c 4b 65 79 20 69 73 20 33 32  t nCellKey is 32
223b5 2d 62 69 74 73 20 20 0a 20 20 2a 2f 0a 20 20 61  -bits  .  */.  a
223b6 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
223b7 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64  reeCursorIsValid
223b8 28 70 43 75 72 29 20 29 3b 0a 20 20 56 56 41 5f  (pCur) );.  VVA_
223b9 4f 4e 4c 59 28 72 63 20 3d 29 20 73 71 6c 69 74  ONLY(rc =) sqlit
223ba 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70  e3BtreeKeySize(p
223bb 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b  Cur, &nCellKey);
223bc 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
223bd 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 20  QLITE_OK );     
223be 2f 2a 20 70 43 75 72 20 69 73 20 61 6c 77 61 79  /* pCur is alway
223bf 73 20 76 61 6c 69 64 20 73 6f 20 4b 65 79 53 69  s valid so KeySi
223c0 7a 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a  ze cannot fail *
223c1 2f 0a 20 20 61 73 73 65 72 74 28 20 28 6e 43 65  /.  assert( (nCe
223c2 6c 6c 4b 65 79 20 26 20 53 51 4c 49 54 45 5f 4d  llKey & SQLITE_M
223c3 41 58 5f 55 33 32 29 3d 3d 28 75 36 34 29 6e 43  AX_U32)==(u64)nC
223c4 65 6c 6c 4b 65 79 20 29 3b 0a 0a 20 20 2f 2a 20  ellKey );..  /* 
223c5 52 65 61 64 20 69 6e 20 74 68 65 20 63 6f 6d 70  Read in the comp
223c6 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20  lete content of 
223c7 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20  the index entry 
223c8 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20  */.  memset(&m, 
223c9 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20  0, sizeof(m));. 
223ca 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
223cb 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43  eMemFromBtree(pC
223cc 75 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c  ur, 0, (int)nCel
223cd 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20  lKey, 1, &m);.  
223ce 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65  if( rc ){.    re
223cf 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
223d0 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 65 6e 74  /* The index ent
223d1 72 79 20 6d 75 73 74 20 62 65 67 69 6e 20 77 69  ry must begin wi
223d2 74 68 20 61 20 68 65 61 64 65 72 20 73 69 7a 65  th a header size
223d3 20 2a 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 56   */.  (void)getV
223d4 61 72 69 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a  arint32((u8*)m.z
223d5 2c 20 73 7a 48 64 72 29 3b 0a 20 20 74 65 73 74  , szHdr);.  test
223d6 63 61 73 65 28 20 73 7a 48 64 72 3d 3d 33 20 29  case( szHdr==3 )
223d7 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 73 7a  ;.  testcase( sz
223d8 48 64 72 3d 3d 6d 2e 6e 20 29 3b 0a 20 20 69 66  Hdr==m.n );.  if
223d9 28 20 75 6e 6c 69 6b 65 6c 79 28 73 7a 48 64 72  ( unlikely(szHdr
223da 3c 33 20 7c 7c 20 28 69 6e 74 29 73 7a 48 64 72  <3 || (int)szHdr
223db 3e 6d 2e 6e 29 20 29 7b 0a 20 20 20 20 67 6f 74  >m.n) ){.    got
223dc 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72  o idx_rowid_corr
223dd 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  uption;.  }..  /
223de 2a 20 54 68 65 20 6c 61 73 74 20 66 69 65 6c 64  * The last field
223df 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 73 68   of the index sh
223e0 6f 75 6c 64 20 62 65 20 61 6e 20 69 6e 74 65 67  ould be an integ
223e1 65 72 20 2d 20 74 68 65 20 52 4f 57 49 44 2e 0a  er - the ROWID..
223e2 20 20 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74    ** Verify that
223e3 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20   the last entry 
223e4 72 65 61 6c 6c 79 20 69 73 20 61 6e 20 69 6e 74  really is an int
223e5 65 67 65 72 2e 20 2a 2f 0a 20 20 28 76 6f 69 64  eger. */.  (void
223e6 29 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38  )getVarint32((u8
223e7 2a 29 26 6d 2e 7a 5b 73 7a 48 64 72 2d 31 5d 2c  *)&m.z[szHdr-1],
223e8 20 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 74   typeRowid);.  t
223e9 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77  estcase( typeRow
223ea 69 64 3d 3d 31 20 29 3b 0a 20 20 74 65 73 74 63  id==1 );.  testc
223eb 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d  ase( typeRowid==
223ec 32 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  2 );.  testcase(
223ed 20 74 79 70 65 52 6f 77 69 64 3d 3d 33 20 29 3b   typeRowid==3 );
223ee 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70  .  testcase( typ
223ef 65 52 6f 77 69 64 3d 3d 34 20 29 3b 0a 20 20 74  eRowid==4 );.  t
223f0 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77  estcase( typeRow
223f1 69 64 3d 3d 35 20 29 3b 0a 20 20 74 65 73 74 63  id==5 );.  testc
223f2 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d  ase( typeRowid==
223f3 36 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  6 );.  testcase(
223f4 20 74 79 70 65 52 6f 77 69 64 3d 3d 38 20 29 3b   typeRowid==8 );
223f5 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70  .  testcase( typ
223f6 65 52 6f 77 69 64 3d 3d 39 20 29 3b 0a 20 20 69  eRowid==9 );.  i
223f7 66 28 20 75 6e 6c 69 6b 65 6c 79 28 74 79 70 65  f( unlikely(type
223f8 52 6f 77 69 64 3c 31 20 7c 7c 20 74 79 70 65 52  Rowid<1 || typeR
223f9 6f 77 69 64 3e 39 20 7c 7c 20 74 79 70 65 52 6f  owid>9 || typeRo
223fa 77 69 64 3d 3d 37 29 20 29 7b 0a 20 20 20 20 67  wid==7) ){.    g
223fb 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f  oto idx_rowid_co
223fc 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20  rruption;.  }.  
223fd 6c 65 6e 52 6f 77 69 64 20 3d 20 73 71 6c 69 74  lenRowid = sqlit
223fe 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
223ff 4c 65 6e 28 74 79 70 65 52 6f 77 69 64 29 3b 0a  Len(typeRowid);.
22400 20 20 74 65 73 74 63 61 73 65 28 20 28 75 33 32    testcase( (u32
22401 29 6d 2e 6e 3d 3d 73 7a 48 64 72 2b 6c 65 6e 52  )m.n==szHdr+lenR
22402 6f 77 69 64 20 29 3b 0a 20 20 69 66 28 20 75 6e  owid );.  if( un
22403 6c 69 6b 65 6c 79 28 28 75 33 32 29 6d 2e 6e 3c  likely((u32)m.n<
22404 73 7a 48 64 72 2b 6c 65 6e 52 6f 77 69 64 29 20  szHdr+lenRowid) 
22405 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 64 78 5f  ){.    goto idx_
22406 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f 6e  rowid_corruption
22407 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 65 74 63  ;.  }..  /* Fetc
22408 68 20 74 68 65 20 69 6e 74 65 67 65 72 20 6f 66  h the integer of
22409 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  f the end of the
2240a 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f   index record */
2240b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  .  sqlite3VdbeSe
2240c 72 69 61 6c 47 65 74 28 28 75 38 2a 29 26 6d 2e  rialGet((u8*)&m.
2240d 7a 5b 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 64 5d 2c  z[m.n-lenRowid],
2240e 20 74 79 70 65 52 6f 77 69 64 2c 20 26 76 29 3b   typeRowid, &v);
2240f 0a 20 20 2a 72 6f 77 69 64 20 3d 20 76 2e 75 2e  .  *rowid = v.u.
22410 69 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  i;.  sqlite3Vdbe
22411 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a  MemRelease(&m);.
22412 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
22413 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68  OK;..  /* Jump h
22414 65 72 65 20 69 66 20 64 61 74 61 62 61 73 65 20  ere if database 
22415 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 64 65  corruption is de
22416 74 65 63 74 65 64 20 61 66 74 65 72 20 6d 20 68  tected after m h
22417 61 73 20 62 65 65 6e 0a 20 20 2a 2a 20 61 6c 6c  as been.  ** all
22418 6f 63 61 74 65 64 2e 20 20 46 72 65 65 20 74 68  ocated.  Free th
22419 65 20 6d 20 6f 62 6a 65 63 74 20 61 6e 64 20 72  e m object and r
2241a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
2241b 52 55 50 54 2e 20 2a 2f 0a 69 64 78 5f 72 6f 77  RUPT. */.idx_row
2241c 69 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3a 0a 20  id_corruption:. 
2241d 20 74 65 73 74 63 61 73 65 28 20 6d 2e 7a 4d 61   testcase( m.zMa
2241e 6c 6c 6f 63 21 3d 30 20 29 3b 0a 20 20 73 71 6c  lloc!=0 );.  sql
2241f 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
22420 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e  se(&m);.  return
22421 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
22422 42 4b 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  BKPT;.}../*.** C
22423 6f 6d 70 61 72 65 20 74 68 65 20 6b 65 79 20 6f  ompare the key o
22424 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72  f the index entr
22425 79 20 74 68 61 74 20 63 75 72 73 6f 72 20 70 43  y that cursor pC
22426 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20   is pointing to 
22427 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20 6b  against.** the k
22428 65 79 20 73 74 72 69 6e 67 20 69 6e 20 70 55 6e  ey string in pUn
22429 70 61 63 6b 65 64 2e 20 20 57 72 69 74 65 20 69  packed.  Write i
2242a 6e 74 6f 20 2a 70 52 65 73 20 61 20 6e 75 6d 62  nto *pRes a numb
2242b 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 6e 65  er.** that is ne
2242c 67 61 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72  gative, zero, or
2242d 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 43 20   positive if pC 
2242e 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71  is less than, eq
2242f 75 61 6c 20 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72  ual to,.** or gr
22430 65 61 74 65 72 20 74 68 61 6e 20 70 55 6e 70 61  eater than pUnpa
22431 63 6b 65 64 2e 20 20 52 65 74 75 72 6e 20 53 51  cked.  Return SQ
22432 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
22433 73 73 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e 70 61 63  ss..**.** pUnpac
22434 6b 65 64 20 69 73 20 65 69 74 68 65 72 20 63 72  ked is either cr
22435 65 61 74 65 64 20 77 69 74 68 6f 75 74 20 61 20  eated without a 
22436 72 6f 77 69 64 20 6f 72 20 69 73 20 74 72 75 6e  rowid or is trun
22437 63 61 74 65 64 20 73 6f 20 74 68 61 74 20 69 74  cated so that it
22438 0a 2a 2a 20 6f 6d 69 74 73 20 74 68 65 20 72 6f  .** omits the ro
22439 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 2e 20  wid at the end. 
2243a 20 54 68 65 20 72 6f 77 69 64 20 61 74 20 74 68   The rowid at th
2243b 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64  e end of the ind
2243c 65 78 20 65 6e 74 72 79 0a 2a 2a 20 69 73 20 69  ex entry.** is i
2243d 67 6e 6f 72 65 64 20 61 73 20 77 65 6c 6c 2e 20  gnored as well. 
2243e 20 48 65 6e 63 65 2c 20 74 68 69 73 20 72 6f 75   Hence, this rou
2243f 74 69 6e 65 20 6f 6e 6c 79 20 63 6f 6d 70 61 72  tine only compar
22440 65 73 20 74 68 65 20 70 72 65 66 69 78 65 73 20  es the prefixes 
22441 0a 2a 2a 20 6f 66 20 74 68 65 20 6b 65 79 73 20  .** of the keys 
22442 70 72 69 6f 72 20 74 6f 20 74 68 65 20 66 69 6e  prior to the fin
22443 61 6c 20 72 6f 77 69 64 2c 20 6e 6f 74 20 74 68  al rowid, not th
22444 65 20 65 6e 74 69 72 65 20 6b 65 79 2e 0a 2a 2f  e entire key..*/
22445 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22446 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49  int sqlite3VdbeI
22447 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 0a 20 20  dxKeyCompare(.  
22448 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 2c 20  VdbeCursor *pC, 
22449 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2244a 68 65 20 63 75 72 73 6f 72 20 74 6f 20 63 6f 6d  he cursor to com
2244b 70 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a  pare against */.
2244c 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
2244d 20 2a 70 55 6e 70 61 63 6b 65 64 2c 20 20 2f 2a   *pUnpacked,  /*
2244e 20 55 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f   Unpacked versio
2244f 6e 20 6f 66 20 6b 65 79 20 74 6f 20 63 6f 6d 70  n of key to comp
22450 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20  are against */. 
22451 20 69 6e 74 20 2a 72 65 73 20 20 20 20 20 20 20   int *res       
22452 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
22453 57 72 69 74 65 20 74 68 65 20 63 6f 6d 70 61 72  Write the compar
22454 69 73 6f 6e 20 72 65 73 75 6c 74 20 68 65 72 65  ison result here
22455 20 2a 2f 0a 29 7b 0a 20 20 69 36 34 20 6e 43 65   */.){.  i64 nCe
22456 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74  llKey = 0;.  int
22457 20 72 63 3b 0a 20 20 42 74 43 75 72 73 6f 72 20   rc;.  BtCursor 
22458 2a 70 43 75 72 20 3d 20 70 43 2d 3e 70 43 75 72  *pCur = pC->pCur
22459 73 6f 72 3b 0a 20 20 4d 65 6d 20 6d 3b 0a 0a 20  sor;.  Mem m;.. 
2245a 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
2245b 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c  BtreeCursorIsVal
2245c 69 64 28 70 43 75 72 29 20 29 3b 0a 20 20 56 56  id(pCur) );.  VV
2245d 41 5f 4f 4e 4c 59 28 72 63 20 3d 29 20 73 71 6c  A_ONLY(rc =) sql
2245e 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
2245f 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 79  (pCur, &nCellKey
22460 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d  );.  assert( rc=
22461 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20  =SQLITE_OK );   
22462 20 2f 2a 20 70 43 75 72 20 69 73 20 61 6c 77 61   /* pCur is alwa
22463 79 73 20 76 61 6c 69 64 20 73 6f 20 4b 65 79 53  ys valid so KeyS
22464 69 7a 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20  ize cannot fail 
22465 2a 2f 0a 20 20 2f 2a 20 6e 43 65 6c 6c 4b 65 79  */.  /* nCellKey
22466 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20   will always be 
22467 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 30 78  between 0 and 0x
22468 66 66 66 66 66 66 66 66 20 62 65 63 61 75 73 65  ffffffff because
22469 20 6f 66 20 74 68 65 20 73 61 79 0a 20 20 2a 2a   of the say.  **
2246a 20 74 68 61 74 20 62 74 72 65 65 50 61 72 73 65   that btreeParse
2246b 43 65 6c 6c 50 74 72 28 29 20 61 6e 64 20 73 71  CellPtr() and sq
2246c 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32  lite3GetVarint32
2246d 28 29 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74  () are implement
2246e 65 64 20 2a 2f 0a 20 20 69 66 28 20 6e 43 65 6c  ed */.  if( nCel
2246f 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43 65 6c 6c  lKey<=0 || nCell
22470 4b 65 79 3e 30 78 37 66 66 66 66 66 66 66 20 29  Key>0x7fffffff )
22471 7b 0a 20 20 20 20 2a 72 65 73 20 3d 20 30 3b 0a  {.    *res = 0;.
22472 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
22473 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
22474 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 2c    }.  memset(&m,
22475 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29 3b 0a   0, sizeof(m));.
22476 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
22477 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70  beMemFromBtree(p
22478 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28  C->pCursor, 0, (
22479 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c  int)nCellKey, 1,
2247a 20 26 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 29   &m);.  if( rc )
2247b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
2247c 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
2247d 55 6e 70 61 63 6b 65 64 2d 3e 66 6c 61 67 73 20  Unpacked->flags 
2247e 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49  & UNPACKED_PREFI
2247f 58 5f 4d 41 54 43 48 20 29 3b 0a 20 20 2a 72 65  X_MATCH );.  *re
22480 73 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52  s = sqlite3VdbeR
22481 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6d 2e 6e  ecordCompare(m.n
22482 2c 20 6d 2e 7a 2c 20 70 55 6e 70 61 63 6b 65 64  , m.z, pUnpacked
22483 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
22484 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a  MemRelease(&m);.
22485 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
22486 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  OK;.}../*.** Thi
22487 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
22488 68 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 72  he value to be r
22489 65 74 75 72 6e 65 64 20 62 79 20 73 75 62 73 65  eturned by subse
2248a 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a  quent calls to.*
2248b 2a 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65  * sqlite3_change
2248c 73 28 29 20 6f 6e 20 74 68 65 20 64 61 74 61 62  s() on the datab
2248d 61 73 65 20 68 61 6e 64 6c 65 20 27 64 62 27 2e  ase handle 'db'.
2248e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
2248f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
22490 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 73  VdbeSetChanges(s
22491 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
22492 6e 43 68 61 6e 67 65 29 7b 0a 20 20 61 73 73 65  nChange){.  asse
22493 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
22494 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
22495 29 20 29 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e  ) );.  db->nChan
22496 67 65 20 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20  ge = nChange;.  
22497 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65  db->nTotalChange
22498 20 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a   += nChange;.}..
22499 2f 2a 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67  /*.** Set a flag
2249a 20 69 6e 20 74 68 65 20 76 64 62 65 20 74 6f 20   in the vdbe to 
2249b 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67  update the chang
2249c 65 20 63 6f 75 6e 74 65 72 20 77 68 65 6e 20 69  e counter when i
2249d 74 20 69 73 20 66 69 6e 61 6c 69 73 65 64 0a 2a  t is finalised.*
2249e 2a 20 6f 72 20 72 65 73 65 74 2e 0a 2a 2f 0a 53  * or reset..*/.S
2249f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
224a0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f  id sqlite3VdbeCo
224a1 75 6e 74 43 68 61 6e 67 65 73 28 56 64 62 65 20  untChanges(Vdbe 
224a2 2a 76 29 7b 0a 20 20 76 2d 3e 63 68 61 6e 67 65  *v){.  v->change
224a3 43 6e 74 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a  CntOn = 1;.}../*
224a4 0a 2a 2a 20 4d 61 72 6b 20 65 76 65 72 79 20 70  .** Mark every p
224a5 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
224a6 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  t associated wit
224a7 68 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  h a database con
224a8 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 73 20 65 78  nection.** as ex
224a9 70 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20  pired..**.** An 
224aa 65 78 70 69 72 65 64 20 73 74 61 74 65 6d 65 6e  expired statemen
224ab 74 20 6d 65 61 6e 73 20 74 68 61 74 20 72 65 63  t means that rec
224ac 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 74 68  ompilation of th
224ad 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 0a 2a  e statement is.*
224ae 2a 20 72 65 63 6f 6d 6d 65 6e 64 2e 20 20 53 74  * recommend.  St
224af 61 74 65 6d 65 6e 74 73 20 65 78 70 69 72 65 20  atements expire 
224b0 77 68 65 6e 20 74 68 69 6e 67 73 20 68 61 70 70  when things happ
224b1 65 6e 20 74 68 61 74 20 6d 61 6b 65 20 74 68 65  en that make the
224b2 69 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 20 6f  ir.** programs o
224b3 62 73 6f 6c 65 74 65 2e 20 20 52 65 6d 6f 76 69  bsolete.  Removi
224b4 6e 67 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20  ng user-defined 
224b5 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 63 6f 6c  functions or col
224b6 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e  lating.** sequen
224b7 63 65 73 2c 20 6f 72 20 63 68 61 6e 67 69 6e 67  ces, or changing
224b8 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f   an authorizatio
224b9 6e 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74  n function are t
224ba 68 65 20 74 79 70 65 73 20 6f 66 0a 2a 2a 20 74  he types of.** t
224bb 68 69 6e 67 73 20 74 68 61 74 20 6d 61 6b 65 20  hings that make 
224bc 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
224bd 6e 74 73 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f  nts obsolete..*/
224be 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
224bf 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 69  void sqlite3Expi
224c0 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d  rePreparedStatem
224c1 65 6e 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62  ents(sqlite3 *db
224c2 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20  ){.  Vdbe *p;.  
224c3 66 6f 72 28 70 20 3d 20 64 62 2d 3e 70 56 64 62  for(p = db->pVdb
224c4 65 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  e; p; p=p->pNext
224c5 29 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65  ){.    p->expire
224c6 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  d = 1;.  }.}../*
224c7 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64  .** Return the d
224c8 61 74 61 62 61 73 65 20 61 73 73 6f 63 69 61 74  atabase associat
224c9 65 64 20 77 69 74 68 20 74 68 65 20 56 64 62 65  ed with the Vdbe
224ca 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
224cb 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c  ATE sqlite3 *sql
224cc 69 74 65 33 56 64 62 65 44 62 28 56 64 62 65 20  ite3VdbeDb(Vdbe 
224cd 2a 76 29 7b 0a 20 20 72 65 74 75 72 6e 20 76 2d  *v){.  return v-
224ce 3e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  >db;.}../*.** Re
224cf 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
224d0 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  o an sqlite3_val
224d1 75 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  ue structure con
224d2 74 61 69 6e 69 6e 67 20 74 68 65 20 76 61 6c 75  taining the valu
224d3 65 20 62 6f 75 6e 64 0a 2a 2a 20 70 61 72 61 6d  e bound.** param
224d4 65 74 65 72 20 69 56 61 72 20 6f 66 20 56 4d 20  eter iVar of VM 
224d5 76 2e 20 45 78 63 65 70 74 2c 20 69 66 20 74 68  v. Except, if th
224d6 65 20 76 61 6c 75 65 20 69 73 20 61 6e 20 53 51  e value is an SQ
224d7 4c 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 0a  L NULL, return .
224d8 2a 2a 20 30 20 69 6e 73 74 65 61 64 2e 20 55 6e  ** 0 instead. Un
224d9 6c 65 73 73 20 69 74 20 69 73 20 4e 55 4c 4c 2c  less it is NULL,
224da 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20   apply affinity 
224db 61 66 66 20 28 6f 6e 65 20 6f 66 20 74 68 65 20  aff (one of the 
224dc 53 51 4c 49 54 45 5f 41 46 46 5f 2a 0a 2a 2a 20  SQLITE_AFF_*.** 
224dd 63 6f 6e 73 74 61 6e 74 73 29 20 74 6f 20 74 68  constants) to th
224de 65 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 72  e value before r
224df 65 74 75 72 6e 69 6e 67 20 69 74 2e 0a 2a 2a 0a  eturning it..**.
224e0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
224e1 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 66 72  value must be fr
224e2 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65  eed by the calle
224e3 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56  r using sqlite3V
224e4 61 6c 75 65 46 72 65 65 28 29 2e 0a 2a 2f 0a 53  alueFree()..*/.S
224e5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
224e6 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c  lite3_value *sql
224e7 69 74 65 33 56 64 62 65 47 65 74 56 61 6c 75 65  ite3VdbeGetValue
224e8 28 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69 56  (Vdbe *v, int iV
224e9 61 72 2c 20 75 38 20 61 66 66 29 7b 0a 20 20 61  ar, u8 aff){.  a
224ea 73 73 65 72 74 28 20 69 56 61 72 3e 30 20 29 3b  ssert( iVar>0 );
224eb 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20  .  if( v ){.    
224ec 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 76 2d 3e  Mem *pMem = &v->
224ed 61 56 61 72 5b 69 56 61 72 2d 31 5d 3b 0a 20 20  aVar[iVar-1];.  
224ee 20 20 69 66 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e    if( 0==(pMem->
224ef 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
224f0 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
224f1 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 74 20 3d  e3_value *pRet =
224f2 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77   sqlite3ValueNew
224f3 28 76 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 69  (v->db);.      i
224f4 66 28 20 70 52 65 74 20 29 7b 0a 20 20 20 20 20  f( pRet ){.     
224f5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
224f6 6d 43 6f 70 79 28 28 4d 65 6d 20 2a 29 70 52 65  mCopy((Mem *)pRe
224f7 74 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20  t, pMem);.      
224f8 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70    sqlite3ValueAp
224f9 70 6c 79 41 66 66 69 6e 69 74 79 28 70 52 65 74  plyAffinity(pRet
224fa 2c 20 61 66 66 2c 20 53 51 4c 49 54 45 5f 55 54  , aff, SQLITE_UT
224fb 46 38 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  F8);.        sql
224fc 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65  ite3VdbeMemStore
224fd 54 79 70 65 28 28 4d 65 6d 20 2a 29 70 52 65 74  Type((Mem *)pRet
224fe 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
224ff 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 20 20   return pRet;.  
22500 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
22501 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e   0;.}../*.** Con
22502 66 69 67 75 72 65 20 53 51 4c 20 76 61 72 69 61  figure SQL varia
22503 62 6c 65 20 69 56 61 72 20 73 6f 20 74 68 61 74  ble iVar so that
22504 20 62 69 6e 64 69 6e 67 20 61 20 6e 65 77 20 76   binding a new v
22505 61 6c 75 65 20 74 6f 20 69 74 20 73 69 67 6e 61  alue to it signa
22506 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  ls.** to sqlite3
22507 5f 72 65 6f 70 74 69 6d 69 7a 65 28 29 20 74 68  _reoptimize() th
22508 61 74 20 72 65 2d 70 72 65 70 61 72 69 6e 67 20  at re-preparing 
22509 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6d 61  the statement ma
2250a 79 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61  y result.** in a
2250b 20 62 65 74 74 65 72 20 71 75 65 72 79 20 70 6c   better query pl
2250c 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  an..*/.SQLITE_PR
2250d 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2250e 65 33 56 64 62 65 53 65 74 56 61 72 6d 61 73 6b  e3VdbeSetVarmask
2250f 28 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69 56  (Vdbe *v, int iV
22510 61 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  ar){.  assert( i
22511 56 61 72 3e 30 20 29 3b 0a 20 20 69 66 28 20 69  Var>0 );.  if( i
22512 56 61 72 3e 33 32 20 29 7b 0a 20 20 20 20 76 2d  Var>32 ){.    v-
22513 3e 65 78 70 6d 61 73 6b 20 3d 20 30 78 66 66 66  >expmask = 0xfff
22514 66 66 66 66 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a  fffff;.  }else{.
22515 20 20 20 20 76 2d 3e 65 78 70 6d 61 73 6b 20 7c      v->expmask |
22516 3d 20 28 28 75 33 32 29 31 20 3c 3c 20 28 69 56  = ((u32)1 << (iV
22517 61 72 2d 31 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ar-1));.  }.}../
22518 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
22519 6e 64 20 6f 66 20 76 64 62 65 61 75 78 2e 63 20  nd of vdbeaux.c 
2251a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2251b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2251c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2251d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
2251e 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61 70  egin file vdbeap
2251f 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  i.c ************
22520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22521 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
22522 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36  *.** 2004 May 26
22523 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
22524 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
22525 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
22526 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
22527 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
22528 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
22529 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
2252a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
2252b 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
2252c 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
2252d 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
2252e 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
2252f 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
22530 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
22531 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
22532 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
22533 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
22534 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
22535 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22536 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22537 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22538 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
22539 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
2253a 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
2253b 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 41  e to implement A
2253c 50 49 73 20 74 68 61 74 20 61 72 65 20 70 61 72  PIs that are par
2253d 74 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42 45  t of the.** VDBE
2253e 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  ..*/..#ifndef SQ
2253f 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
22540 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ATED./*.** Retur
22541 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f  n TRUE (non-zero
22542 29 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65  ) of the stateme
22543 6e 74 20 73 75 70 70 6c 69 65 64 20 61 73 20 61  nt supplied as a
22544 6e 20 61 72 67 75 6d 65 6e 74 20 6e 65 65 64 73  n argument needs
22545 0a 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f 6d 70  .** to be recomp
22546 69 6c 65 64 2e 20 20 41 20 73 74 61 74 65 6d 65  iled.  A stateme
22547 6e 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  nt needs to be r
22548 65 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e 65 76  ecompiled whenev
22549 65 72 20 74 68 65 0a 2a 2a 20 65 78 65 63 75 74  er the.** execut
2254a 69 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20  ion environment 
2254b 63 68 61 6e 67 65 73 20 69 6e 20 61 20 77 61 79  changes in a way
2254c 20 74 68 61 74 20 77 6f 75 6c 64 20 61 6c 74 65   that would alte
2254d 72 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a  r the program.**
2254e 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 70 72   that sqlite3_pr
2254f 65 70 61 72 65 28 29 20 67 65 6e 65 72 61 74 65  epare() generate
22550 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  s.  For example,
22551 20 69 66 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e   if new function
22552 73 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e  s or.** collatin
22553 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 65 20  g sequences are 
22554 72 65 67 69 73 74 65 72 65 64 20 6f 72 20 69 66  registered or if
22555 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 66   an authorizer f
22556 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 64  unction is.** ad
22557 64 65 64 20 6f 72 20 63 68 61 6e 67 65 64 2e 0a  ded or changed..
22558 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
22559 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65  t sqlite3_expire
2255a 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  d(sqlite3_stmt *
2255b 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a  pStmt){.  Vdbe *
2255c 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74  p = (Vdbe*)pStmt
2255d 3b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20  ;.  return p==0 
2255e 7c 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0a 7d  || p->expired;.}
2255f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
22560 68 65 63 6b 20 6f 6e 20 61 20 56 64 62 65 20 74  heck on a Vdbe t
22561 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 68  o make sure it h
22562 61 73 20 6e 6f 74 20 62 65 65 6e 20 66 69 6e 61  as not been fina
22563 6c 69 7a 65 64 2e 20 20 4c 6f 67 0a 2a 2a 20 61  lized.  Log.** a
22564 6e 20 65 72 72 6f 72 20 61 6e 64 20 72 65 74 75  n error and retu
22565 72 6e 20 74 72 75 65 20 69 66 20 69 74 20 68 61  rn true if it ha
22566 73 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64  s been finalized
22567 20 28 6f 72 20 69 73 20 6f 74 68 65 72 77 69 73   (or is otherwis
22568 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 29 2e 20 20  e.** invalid).  
22569 52 65 74 75 72 6e 20 66 61 6c 73 65 20 69 66 20  Return false if 
2256a 69 74 20 69 73 20 6f 6b 2e 0a 2a 2f 0a 73 74 61  it is ok..*/.sta
2256b 74 69 63 20 69 6e 74 20 76 64 62 65 53 61 66 65  tic int vdbeSafe
2256c 74 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69  ty(Vdbe *p){.  i
2256d 66 28 20 70 2d 3e 64 62 3d 3d 30 20 29 7b 0a 20  f( p->db==0 ){. 
2256e 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53     sqlite3_log(S
2256f 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 22 41  QLITE_MISUSE, "A
22570 50 49 20 63 61 6c 6c 65 64 20 77 69 74 68 20 66  PI called with f
22571 69 6e 61 6c 69 7a 65 64 20 70 72 65 70 61 72 65  inalized prepare
22572 64 20 73 74 61 74 65 6d 65 6e 74 22 29 3b 0a 20  d statement");. 
22573 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
22574 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
22575 20 30 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63   0;.  }.}.static
22576 20 69 6e 74 20 76 64 62 65 53 61 66 65 74 79 4e   int vdbeSafetyN
22577 6f 74 4e 75 6c 6c 28 56 64 62 65 20 2a 70 29 7b  otNull(Vdbe *p){
22578 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20  .  if( p==0 ){. 
22579 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53     sqlite3_log(S
2257a 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 22 41  QLITE_MISUSE, "A
2257b 50 49 20 63 61 6c 6c 65 64 20 77 69 74 68 20 4e  PI called with N
2257c 55 4c 4c 20 70 72 65 70 61 72 65 64 20 73 74 61  ULL prepared sta
2257d 74 65 6d 65 6e 74 22 29 3b 0a 20 20 20 20 72 65  tement");.    re
2257e 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  turn 1;.  }else{
2257f 0a 20 20 20 20 72 65 74 75 72 6e 20 76 64 62 65  .    return vdbe
22580 53 61 66 65 74 79 28 70 29 3b 0a 20 20 7d 0a 7d  Safety(p);.  }.}
22581 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
22582 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 64 65  owing routine de
22583 73 74 72 6f 79 73 20 61 20 76 69 72 74 75 61 6c  stroys a virtual
22584 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 69 73   machine that is
22585 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74   created by.** t
22586 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69  he sqlite3_compi
22587 6c 65 28 29 20 72 6f 75 74 69 6e 65 2e 20 54 68  le() routine. Th
22588 65 20 69 6e 74 65 67 65 72 20 72 65 74 75 72 6e  e integer return
22589 65 64 20 69 73 20 61 6e 20 53 51 4c 49 54 45 5f  ed is an SQLITE_
2258a 0a 2a 2a 20 73 75 63 63 65 73 73 2f 66 61 69 6c  .** success/fail
2258b 75 72 65 20 63 6f 64 65 20 74 68 61 74 20 64 65  ure code that de
2258c 73 63 72 69 62 65 73 20 74 68 65 20 72 65 73 75  scribes the resu
2258d 6c 74 20 6f 66 20 65 78 65 63 75 74 69 6e 67 20  lt of executing 
2258e 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d  the virtual.** m
2258f 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  achine..**.** Th
22590 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
22591 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61  the error code a
22592 6e 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e  nd string return
22593 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33  ed by.** sqlite3
22594 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69  _errcode(), sqli
22595 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64  te3_errmsg() and
22596 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31   sqlite3_errmsg1
22597 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  6()..*/.SQLITE_A
22598 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
22599 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f  inalize(sqlite3_
2259a 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20  stmt *pStmt){.  
2259b 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 53  int rc;.  if( pS
2259c 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  tmt==0 ){.    /*
2259d 20 49 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d   IMPLEMENTATION-
2259e 4f 46 3a 20 52 2d 35 37 32 32 38 2d 31 32 39 30  OF: R-57228-1290
2259f 34 20 49 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74  4 Invoking sqlit
225a0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 6f 6e  e3_finalize() on
225a1 20 61 20 4e 55 4c 4c 0a 20 20 20 20 2a 2a 20 70   a NULL.    ** p
225a2 6f 69 6e 74 65 72 20 69 73 20 61 20 68 61 72 6d  ointer is a harm
225a3 6c 65 73 73 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20  less no-op. */. 
225a4 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
225a5 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
225a6 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a  Vdbe *v = (Vdbe*
225a7 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69  )pStmt;.    sqli
225a8 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62 3b  te3 *db = v->db;
225a9 0a 20 20 20 20 69 66 28 20 76 64 62 65 53 61 66  .    if( vdbeSaf
225aa 65 74 79 28 76 29 20 29 20 72 65 74 75 72 6e 20  ety(v) ) return 
225ab 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b  SQLITE_MISUSE_BK
225ac 50 54 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  PT;.    sqlite3_
225ad 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
225ae 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d  mutex);.    rc =
225af 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61   sqlite3VdbeFina
225b0 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 72 63 20  lize(v);.    rc 
225b1 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
225b2 28 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 73 71  (db, rc);.    sq
225b3 6c 69 74 65 33 4c 65 61 76 65 4d 75 74 65 78 41  lite3LeaveMutexA
225b4 6e 64 43 6c 6f 73 65 5a 6f 6d 62 69 65 28 64 62  ndCloseZombie(db
225b5 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
225b6 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 72  rc;.}../*.** Ter
225b7 6d 69 6e 61 74 65 20 74 68 65 20 63 75 72 72 65  minate the curre
225b8 6e 74 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  nt execution of 
225b9 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
225ba 20 61 6e 64 20 72 65 73 65 74 20 69 74 0a 2a 2a   and reset it.**
225bb 20 62 61 63 6b 20 74 6f 20 69 74 73 20 73 74 61   back to its sta
225bc 72 74 69 6e 67 20 73 74 61 74 65 20 73 6f 20 74  rting state so t
225bd 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65  hat it can be re
225be 75 73 65 64 2e 20 41 20 73 75 63 63 65 73 73 20  used. A success 
225bf 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  code from.** the
225c0 20 70 72 69 6f 72 20 65 78 65 63 75 74 69 6f 6e   prior execution
225c1 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
225c2 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
225c3 20 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20   sets the error 
225c4 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 20  code and string 
225c5 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73  returned by.** s
225c6 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
225c7 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  , sqlite3_errmsg
225c8 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65  () and sqlite3_e
225c9 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51  rrmsg16()..*/.SQ
225ca 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
225cb 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 74  ite3_reset(sqlit
225cc 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
225cd 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28  .  int rc;.  if(
225ce 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20   pStmt==0 ){.   
225cf 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
225d0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64  .  }else{.    Vd
225d1 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70  be *v = (Vdbe*)p
225d2 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  Stmt;.    sqlite
225d3 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 76 2d  3_mutex_enter(v-
225d4 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  >db->mutex);.   
225d5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
225d6 65 52 65 73 65 74 28 76 29 3b 0a 20 20 20 20 73  eReset(v);.    s
225d7 71 6c 69 74 65 33 56 64 62 65 52 65 77 69 6e 64  qlite3VdbeRewind
225d8 28 76 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  (v);.    assert(
225d9 20 28 72 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65   (rc & (v->db->e
225da 72 72 4d 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a  rrMask))==rc );.
225db 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
225dc 41 70 69 45 78 69 74 28 76 2d 3e 64 62 2c 20 72  ApiExit(v->db, r
225dd 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  c);.    sqlite3_
225de 6d 75 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64  mutex_leave(v->d
225df 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20  b->mutex);.  }. 
225e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
225e1 2a 0a 2a 2a 20 53 65 74 20 61 6c 6c 20 74 68 65  *.** Set all the
225e2 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 74   parameters in t
225e3 68 65 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20  he compiled SQL 
225e4 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 4e 55 4c  statement to NUL
225e5 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  L..*/.SQLITE_API
225e6 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65   int sqlite3_cle
225e7 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69  ar_bindings(sqli
225e8 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
225e9 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
225ea 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
225eb 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
225ec 62 65 2a 29 70 53 74 6d 74 3b 0a 23 69 66 20 53  be*)pStmt;.#if S
225ed 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
225ee 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
225ef 20 2a 6d 75 74 65 78 20 3d 20 28 28 56 64 62 65   *mutex = ((Vdbe
225f0 2a 29 70 53 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75  *)pStmt)->db->mu
225f1 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71  tex;.#endif.  sq
225f2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
225f3 72 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28  r(mutex);.  for(
225f4 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20  i=0; i<p->nVar; 
225f5 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
225f6 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
225f7 26 70 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20  &p->aVar[i]);.  
225f8 20 20 70 2d 3e 61 56 61 72 5b 69 5d 2e 66 6c 61    p->aVar[i].fla
225f9 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
225fa 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 73 50 72   }.  if( p->isPr
225fb 65 70 61 72 65 56 32 20 26 26 20 70 2d 3e 65 78  epareV2 && p->ex
225fc 70 6d 61 73 6b 20 29 7b 0a 20 20 20 20 70 2d 3e  pmask ){.    p->
225fd 65 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d  expired = 1;.  }
225fe 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
225ff 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20  _leave(mutex);. 
22600 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
22601 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
22602 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71  ************* sq
22603 6c 69 74 65 33 5f 76 61 6c 75 65 5f 20 20 2a 2a  lite3_value_  **
22604 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22605 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
22606 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
22607 6f 75 74 69 6e 65 73 20 65 78 74 72 61 63 74 20  outines extract 
22608 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
22609 20 61 20 4d 65 6d 20 6f 72 20 73 71 6c 69 74 65   a Mem or sqlite
2260a 33 5f 76 61 6c 75 65 0a 2a 2a 20 73 74 72 75 63  3_value.** struc
2260b 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ture..*/.SQLITE_
2260c 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
2260d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
2260e 6f 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ob(sqlite3_value
2260f 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a   *pVal){.  Mem *
22610 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a  p = (Mem*)pVal;.
22611 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26    if( p->flags &
22612 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53   (MEM_Blob|MEM_S
22613 74 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  tr) ){.    sqlit
22614 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42  e3VdbeMemExpandB
22615 6c 6f 62 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66  lob(p);.    p->f
22616 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 72  lags &= ~MEM_Str
22617 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c  ;.    p->flags |
22618 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20  = MEM_Blob;.    
22619 72 65 74 75 72 6e 20 70 2d 3e 6e 20 3f 20 70 2d  return p->n ? p-
2261a 3e 7a 20 3a 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  >z : 0;.  }else{
2261b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
2261c 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70  te3_value_text(p
2261d 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49  Val);.  }.}.SQLI
2261e 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2261f 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73  e3_value_bytes(s
22620 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
22621 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  al){.  return sq
22622 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28  lite3ValueBytes(
22623 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
22624 38 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  8);.}.SQLITE_API
22625 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
22626 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74  ue_bytes16(sqlit
22627 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b  e3_value *pVal){
22628 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
22629 33 56 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c  3ValueBytes(pVal
2262a 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  , SQLITE_UTF16NA
2262b 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  TIVE);.}.SQLITE_
2262c 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  API double sqlit
2262d 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
2262e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
2262f 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  Val){.  return s
22630 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61  qlite3VdbeRealVa
22631 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b  lue((Mem*)pVal);
22632 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
22633 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
22634 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  int(sqlite3_valu
22635 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75  e *pVal){.  retu
22636 72 6e 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56  rn (int)sqlite3V
22637 64 62 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d  dbeIntValue((Mem
22638 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54  *)pVal);.}.SQLIT
22639 45 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74  E_API sqlite_int
2263a 36 34 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  64 sqlite3_value
2263b 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76  _int64(sqlite3_v
2263c 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72  alue *pVal){.  r
2263d 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62  eturn sqlite3Vdb
2263e 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29  eIntValue((Mem*)
2263f 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  pVal);.}.SQLITE_
22640 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  API const unsign
22641 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  ed char *sqlite3
22642 5f 76 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69  _value_text(sqli
22643 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29  te3_value *pVal)
22644 7b 0a 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73  {.  return (cons
22645 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
22646 2a 29 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65  *)sqlite3ValueTe
22647 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f  xt(pVal, SQLITE_
22648 55 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66  UTF8);.}.#ifndef
22649 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
2264a 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  16.SQLITE_API co
2264b 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
2264c 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73  3_value_text16(s
2264d 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56  qlite3_value* pV
2264e 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  al){.  return sq
2264f 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70  lite3ValueText(p
22650 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Val, SQLITE_UTF1
22651 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49  6NATIVE);.}.SQLI
22652 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
22653 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  d *sqlite3_value
22654 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69 74 65  _text16be(sqlite
22655 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a  3_value *pVal){.
22656 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
22657 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20  ValueText(pVal, 
22658 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 29 3b  SQLITE_UTF16BE);
22659 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  .}.SQLITE_API co
2265a 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
2265b 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  3_value_text16le
2265c 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
2265d 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20  pVal){.  return 
2265e 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
2265f 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54  (pVal, SQLITE_UT
22660 46 31 36 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66  F16LE);.}.#endif
22661 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
22662 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f  UTF16 */.SQLITE_
22663 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
22664 76 61 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74  value_type(sqlit
22665 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b  e3_value* pVal){
22666 0a 20 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e  .  return pVal->
22667 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  type;.}../******
22668 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22669 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 72  ****** sqlite3_r
2266a 65 73 75 6c 74 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a  esult_  ********
2266b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2266c 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66  *******.** The f
2266d 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
2266e 73 20 61 72 65 20 75 73 65 64 20 62 79 20 75 73  s are used by us
2266f 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  er-defined funct
22670 69 6f 6e 73 20 74 6f 20 73 70 65 63 69 66 79 0a  ions to specify.
22671 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  ** the function 
22672 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  result..**.** Th
22673 65 20 73 65 74 53 74 72 4f 72 45 72 72 6f 72 28  e setStrOrError(
22674 29 20 66 75 6e 74 69 6f 6e 20 63 61 6c 6c 73 20  ) funtion calls 
22675 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
22676 74 53 74 72 28 29 20 74 6f 20 73 74 6f 72 65 20  tStr() to store 
22677 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 61 73  the.** result as
22678 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f   a string or blo
22679 62 20 62 75 74 20 69 66 20 74 68 65 20 73 74 72  b but if the str
2267a 69 6e 67 20 6f 72 20 62 6c 6f 62 20 69 73 20 74  ing or blob is t
2267b 6f 6f 20 6c 61 72 67 65 2c 20 69 74 0a 2a 2a 20  oo large, it.** 
2267c 74 68 65 6e 20 73 65 74 73 20 74 68 65 20 65 72  then sets the er
2267d 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49  ror code to SQLI
2267e 54 45 5f 54 4f 4f 42 49 47 0a 2a 2f 0a 73 74 61  TE_TOOBIG.*/.sta
2267f 74 69 63 20 76 6f 69 64 20 73 65 74 52 65 73 75  tic void setResu
22680 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 0a 20 20  ltStrOrError(.  
22681 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
22682 2a 70 43 74 78 2c 20 20 2f 2a 20 46 75 6e 63 74  *pCtx,  /* Funct
22683 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ion context */. 
22684 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
22685 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69           /* Stri
22686 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20  ng pointer */.  
22687 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20  int n,          
22688 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
22689 20 69 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e   in string, or n
2268a 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20  egative */.  u8 
2268b 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  enc,            
2268c 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67       /* Encoding
2268d 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20 42 4c   of z.  0 for BL
2268e 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  OBs */.  void (*
2268f 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 20  xDel)(void*)    
22690 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66   /* Destructor f
22691 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20  unction */.){.  
22692 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
22693 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e  emSetStr(&pCtx->
22694 73 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44  s, z, n, enc, xD
22695 65 6c 29 3d 3d 53 51 4c 49 54 45 5f 54 4f 4f 42  el)==SQLITE_TOOB
22696 49 47 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  IG ){.    sqlite
22697 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  3_result_error_t
22698 6f 6f 62 69 67 28 70 43 74 78 29 3b 0a 20 20 7d  oobig(pCtx);.  }
22699 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  .}.SQLITE_API vo
2269a 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
2269b 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65  t_blob(.  sqlite
2269c 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
2269d 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
2269e 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20  z, .  int n, .  
2269f 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
226a0 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74  d *).){.  assert
226a1 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65  ( n>=0 );.  asse
226a2 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
226a3 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
226a4 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
226a5 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72  etResultStrOrErr
226a6 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 30  or(pCtx, z, n, 0
226a7 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54  , xDel);.}.SQLIT
226a8 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
226a9 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  e3_result_double
226aa 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
226ab 20 2a 70 43 74 78 2c 20 64 6f 75 62 6c 65 20 72   *pCtx, double r
226ac 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Val){.  assert( 
226ad 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
226ae 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
226af 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
226b0 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62  e3VdbeMemSetDoub
226b1 6c 65 28 26 70 43 74 78 2d 3e 73 2c 20 72 56 61  le(&pCtx->s, rVa
226b2 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  l);.}.SQLITE_API
226b3 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
226b4 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69 74  sult_error(sqlit
226b5 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
226b6 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  , const char *z,
226b7 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72   int n){.  asser
226b8 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
226b9 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
226ba 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43  ->mutex) );.  pC
226bb 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51  tx->isError = SQ
226bc 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71  LITE_ERROR;.  sq
226bd 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
226be 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20  tr(&pCtx->s, z, 
226bf 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  n, SQLITE_UTF8, 
226c0 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
226c1 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  );.}.#ifndef SQL
226c2 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53  ITE_OMIT_UTF16.S
226c3 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
226c4 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
226c5 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f 63 6f  ror16(sqlite3_co
226c6 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e  ntext *pCtx, con
226c7 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20  st void *z, int 
226c8 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  n){.  assert( sq
226c9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
226ca 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
226cb 65 78 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69  ex) );.  pCtx->i
226cc 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f  sError = SQLITE_
226cd 45 52 52 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33  ERROR;.  sqlite3
226ce 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70  VdbeMemSetStr(&p
226cf 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51  Ctx->s, z, n, SQ
226d0 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
226d1 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
226d2 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 53 51  NT);.}.#endif.SQ
226d3 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
226d4 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
226d5 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
226d6 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 56 61 6c   *pCtx, int iVal
226d7 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
226d8 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
226d9 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
226da 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  x) );.  sqlite3V
226db 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26  dbeMemSetInt64(&
226dc 70 43 74 78 2d 3e 73 2c 20 28 69 36 34 29 69 56  pCtx->s, (i64)iV
226dd 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  al);.}.SQLITE_AP
226de 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
226df 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69  esult_int64(sqli
226e0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
226e1 78 2c 20 69 36 34 20 69 56 61 6c 29 7b 0a 20 20  x, i64 iVal){.  
226e2 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
226e3 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
226e4 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
226e5 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
226e6 6d 53 65 74 49 6e 74 36 34 28 26 70 43 74 78 2d  mSetInt64(&pCtx-
226e7 3e 73 2c 20 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c  >s, iVal);.}.SQL
226e8 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
226e9 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
226ea 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
226eb 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72   *pCtx){.  asser
226ec 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
226ed 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
226ee 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
226ef 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e  lite3VdbeMemSetN
226f0 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29 3b 0a 7d  ull(&pCtx->s);.}
226f1 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
226f2 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
226f3 74 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f  text(.  sqlite3_
226f4 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a  context *pCtx, .
226f5 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c    const char *z,
226f6 20 0a 20 20 69 6e 74 20 6e 2c 0a 20 20 76 6f 69   .  int n,.  voi
226f7 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a  d (*xDel)(void *
226f8 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  ).){.  assert( s
226f9 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
226fa 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
226fb 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73  tex) );.  setRes
226fc 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43  ultStrOrError(pC
226fd 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45  tx, z, n, SQLITE
226fe 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a  _UTF8, xDel);.}.
226ff 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
22700 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45  MIT_UTF16.SQLITE
22701 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
22702 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28  3_result_text16(
22703 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
22704 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e  xt *pCtx, .  con
22705 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69  st void *z, .  i
22706 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a  nt n, .  void (*
22707 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b  xDel)(void *).){
22708 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
22709 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
2270a 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
2270b 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53   );.  setResultS
2270c 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20  trOrError(pCtx, 
2270d 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46  z, n, SQLITE_UTF
2270e 31 36 4e 41 54 49 56 45 2c 20 78 44 65 6c 29 3b  16NATIVE, xDel);
2270f 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  .}.SQLITE_API vo
22710 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
22711 74 5f 74 65 78 74 31 36 62 65 28 0a 20 20 73 71  t_text16be(.  sq
22712 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
22713 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f  Ctx, .  const vo
22714 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c  id *z, .  int n,
22715 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29   .  void (*xDel)
22716 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73  (void *).){.  as
22717 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
22718 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
22719 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
2271a 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45   setResultStrOrE
2271b 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c  rror(pCtx, z, n,
2271c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c   SQLITE_UTF16BE,
2271d 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45   xDel);.}.SQLITE
2271e 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
2271f 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  3_result_text16l
22720 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  e(.  sqlite3_con
22721 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63  text *pCtx, .  c
22722 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20  onst void *z, . 
22723 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20   int n, .  void 
22724 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a  (*xDel)(void *).
22725 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
22726 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
22727 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
22728 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c  x) );.  setResul
22729 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78  tStrOrError(pCtx
2272a 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55  , z, n, SQLITE_U
2272b 54 46 31 36 4c 45 2c 20 78 44 65 6c 29 3b 0a 7d  TF16LE, xDel);.}
2272c 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2272d 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
2272e 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2272f 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
22730 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  alue(sqlite3_con
22731 74 65 78 74 20 2a 70 43 74 78 2c 20 73 71 6c 69  text *pCtx, sqli
22732 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75  te3_value *pValu
22733 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  e){.  assert( sq
22734 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
22735 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
22736 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
22737 56 64 62 65 4d 65 6d 43 6f 70 79 28 26 70 43 74  VdbeMemCopy(&pCt
22738 78 2d 3e 73 2c 20 70 56 61 6c 75 65 29 3b 0a 7d  x->s, pValue);.}
22739 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2273a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2273b 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33  zeroblob(sqlite3
2273c 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
2273d 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74  int n){.  assert
2273e 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
2273f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
22740 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
22741 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65  ite3VdbeMemSetZe
22742 72 6f 42 6c 6f 62 28 26 70 43 74 78 2d 3e 73 2c  roBlob(&pCtx->s,
22743 20 6e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50   n);.}.SQLITE_AP
22744 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
22745 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
22746 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
22747 20 2a 70 43 74 78 2c 20 69 6e 74 20 65 72 72 43   *pCtx, int errC
22748 6f 64 65 29 7b 0a 20 20 70 43 74 78 2d 3e 69 73  ode){.  pCtx->is
22749 45 72 72 6f 72 20 3d 20 65 72 72 43 6f 64 65 3b  Error = errCode;
2274a 0a 20 20 69 66 28 20 70 43 74 78 2d 3e 73 2e 66  .  if( pCtx->s.f
2274b 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
2274c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2274d 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74  beMemSetStr(&pCt
2274e 78 2d 3e 73 2c 20 73 71 6c 69 74 65 33 45 72 72  x->s, sqlite3Err
2274f 53 74 72 28 65 72 72 43 6f 64 65 29 2c 20 2d 31  Str(errCode), -1
22750 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
22751 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
22752 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
22753 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a  STATIC);.  }.}..
22754 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53 51 4c 49  /* Force an SQLI
22755 54 45 5f 54 4f 4f 42 49 47 20 65 72 72 6f 72 2e  TE_TOOBIG error.
22756 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76   */.SQLITE_API v
22757 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
22758 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28  lt_error_toobig(
22759 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
2275a 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74  *pCtx){.  assert
2275b 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
2275c 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
2275d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74  >mutex) );.  pCt
2275e 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c  x->isError = SQL
2275f 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 73 71  ITE_TOOBIG;.  sq
22760 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
22761 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 22 73 74  tr(&pCtx->s, "st
22762 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f  ring or blob too
22763 20 62 69 67 22 2c 20 2d 31 2c 20 0a 20 20 20 20   big", -1, .    
22764 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22765 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20     SQLITE_UTF8, 
22766 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
22767 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f  }../* An SQLITE_
22768 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a 2f 0a  NOMEM error. */.
22769 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2276a 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2276b 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74  rror_nomem(sqlit
2276c 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
2276d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
2276e 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
2276f 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
22770 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  x) );.  sqlite3V
22771 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70  dbeMemSetNull(&p
22772 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43 74 78 2d  Ctx->s);.  pCtx-
22773 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54  >isError = SQLIT
22774 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74 78 2d  E_NOMEM;.  pCtx-
22775 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  >s.db->mallocFai
22776 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  led = 1;.}../*.*
22777 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
22778 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
22779 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61  a transaction ha
2277a 73 20 62 65 65 6e 20 63 6f 6d 6d 69 74 74 65 64  s been committed
2277b 2e 20 49 74 20 0a 2a 2a 20 69 6e 76 6f 6b 65 73  . It .** invokes
2277c 20 63 61 6c 6c 62 61 63 6b 73 20 72 65 67 69 73   callbacks regis
2277d 74 65 72 65 64 20 77 69 74 68 20 73 71 6c 69 74  tered with sqlit
2277e 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28 29 20 61 73  e3_wal_hook() as
2277f 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74   required..*/.st
22780 61 74 69 63 20 69 6e 74 20 64 6f 57 61 6c 43 61  atic int doWalCa
22781 6c 6c 62 61 63 6b 73 28 73 71 6c 69 74 65 33 20  llbacks(sqlite3 
22782 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  *db){.  int rc =
22783 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 6e   SQLITE_OK;.#ifn
22784 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
22785 57 41 4c 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66  WAL.  int i;.  f
22786 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
22787 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72  b; i++){.    Btr
22788 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44  ee *pBt = db->aD
22789 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66  b[i].pBt;.    if
2278a 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 69  ( pBt ){.      i
2278b 6e 74 20 6e 45 6e 74 72 79 20 3d 20 73 71 6c 69  nt nEntry = sqli
2278c 74 65 33 50 61 67 65 72 57 61 6c 43 61 6c 6c 62  te3PagerWalCallb
2278d 61 63 6b 28 73 71 6c 69 74 65 33 42 74 72 65 65  ack(sqlite3Btree
2278e 50 61 67 65 72 28 70 42 74 29 29 3b 0a 20 20 20  Pager(pBt));.   
2278f 20 20 20 69 66 28 20 64 62 2d 3e 78 57 61 6c 43     if( db->xWalC
22790 61 6c 6c 62 61 63 6b 20 26 26 20 6e 45 6e 74 72  allback && nEntr
22791 79 3e 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  y>0 && rc==SQLIT
22792 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
22793 72 63 20 3d 20 64 62 2d 3e 78 57 61 6c 43 61 6c  rc = db->xWalCal
22794 6c 62 61 63 6b 28 64 62 2d 3e 70 57 61 6c 41 72  lback(db->pWalAr
22795 67 2c 20 64 62 2c 20 64 62 2d 3e 61 44 62 5b 69  g, db, db->aDb[i
22796 5d 2e 7a 4e 61 6d 65 2c 20 6e 45 6e 74 72 79 29  ].zName, nEntry)
22797 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
22798 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74    }.#endif.  ret
22799 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
2279a 20 45 78 65 63 75 74 65 20 74 68 65 20 73 74 61   Execute the sta
2279b 74 65 6d 65 6e 74 20 70 53 74 6d 74 2c 20 65 69  tement pStmt, ei
2279c 74 68 65 72 20 75 6e 74 69 6c 20 61 20 72 6f 77  ther until a row
2279d 20 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 64   of data is read
2279e 79 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d  y, the.** statem
2279f 65 6e 74 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c  ent is completel
227a0 79 20 65 78 65 63 75 74 65 64 20 6f 72 20 61 6e  y executed or an
227a1 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a   error occurs..*
227a2 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
227a3 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
227a4 20 62 75 6c 6b 20 6f 66 20 74 68 65 20 6c 6f 67   bulk of the log
227a5 69 63 20 62 65 68 69 6e 64 20 74 68 65 20 73 71  ic behind the sq
227a6 6c 69 74 65 5f 73 74 65 70 28 29 0a 2a 2a 20 41  lite_step().** A
227a7 50 49 2e 20 20 54 68 65 20 6f 6e 6c 79 20 74 68  PI.  The only th
227a8 69 6e 67 20 6f 6d 69 74 74 65 64 20 69 73 20 74  ing omitted is t
227a9 68 65 20 61 75 74 6f 6d 61 74 69 63 20 72 65 63  he automatic rec
227aa 6f 6d 70 69 6c 65 20 69 66 20 61 20 0a 2a 2a 20  ompile if a .** 
227ab 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 68 61  schema change ha
227ac 73 20 6f 63 63 75 72 72 65 64 2e 20 20 54 68 61  s occurred.  Tha
227ad 74 20 64 65 74 61 69 6c 20 69 73 20 68 61 6e 64  t detail is hand
227ae 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 6f 75  led by the.** ou
227af 74 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ter sqlite3_step
227b0 28 29 20 77 72 61 70 70 65 72 20 70 72 6f 63 65  () wrapper proce
227b1 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  dure..*/.static 
227b2 69 6e 74 20 73 71 6c 69 74 65 33 53 74 65 70 28  int sqlite3Step(
227b3 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69  Vdbe *p){.  sqli
227b4 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 72  te3 *db;.  int r
227b5 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 29 3b  c;..  assert(p);
227b6 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21  .  if( p->magic!
227b7 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20  =VDBE_MAGIC_RUN 
227b8 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 75 73 65  ){.    /* We use
227b9 64 20 74 6f 20 72 65 71 75 69 72 65 20 74 68 61  d to require tha
227ba 74 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  t sqlite3_reset(
227bb 29 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f  ) be called befo
227bc 72 65 20 72 65 74 72 79 69 6e 67 0a 20 20 20 20  re retrying.    
227bd 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  ** sqlite3_step(
227be 29 20 61 66 74 65 72 20 61 6e 79 20 65 72 72 6f  ) after any erro
227bf 72 20 6f 72 20 61 66 74 65 72 20 53 51 4c 49 54  r or after SQLIT
227c0 45 5f 44 4f 4e 45 2e 20 20 42 75 74 20 62 65 67  E_DONE.  But beg
227c1 69 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 77 69  inning.    ** wi
227c2 74 68 20 76 65 72 73 69 6f 6e 20 33 2e 37 2e 30  th version 3.7.0
227c3 2c 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 69  , we changed thi
227c4 73 20 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65  s so that sqlite
227c5 33 5f 72 65 73 65 74 28 29 20 77 6f 75 6c 64 0a  3_reset() would.
227c6 20 20 20 20 2a 2a 20 62 65 20 63 61 6c 6c 65 64      ** be called
227c7 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69   automatically i
227c8 6e 73 74 65 61 64 20 6f 66 20 74 68 72 6f 77 69  nstead of throwi
227c9 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 49  ng the SQLITE_MI
227ca 53 55 53 45 20 65 72 72 6f 72 2e 0a 20 20 20 20  SUSE error..    
227cb 2a 2a 20 54 68 69 73 20 22 61 75 74 6f 6d 61 74  ** This "automat
227cc 69 63 2d 72 65 73 65 74 22 20 63 68 61 6e 67 65  ic-reset" change
227cd 20 69 73 20 6e 6f 74 20 74 65 63 68 6e 69 63 61   is not technica
227ce 6c 6c 79 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69  lly an incompati
227cf 62 69 6c 69 74 79 2c 20 0a 20 20 20 20 2a 2a 20  bility, .    ** 
227d0 73 69 6e 63 65 20 61 6e 79 20 61 70 70 6c 69 63  since any applic
227d1 61 74 69 6f 6e 20 74 68 61 74 20 72 65 63 65 69  ation that recei
227d2 76 65 73 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49  ves an SQLITE_MI
227d3 53 55 53 45 20 69 73 20 62 72 6f 6b 65 6e 20 62  SUSE is broken b
227d4 79 0a 20 20 20 20 2a 2a 20 64 65 66 69 6e 69 74  y.    ** definit
227d5 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ion..    **.    
227d6 2a 2a 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c  ** Nevertheless,
227d7 20 73 6f 6d 65 20 70 75 62 6c 69 73 68 65 64 20   some published 
227d8 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  applications tha
227d9 74 20 77 65 72 65 20 6f 72 69 67 69 6e 61 6c 6c  t were originall
227da 79 20 77 72 69 74 74 65 6e 0a 20 20 20 20 2a 2a  y written.    **
227db 20 66 6f 72 20 76 65 72 73 69 6f 6e 20 33 2e 36   for version 3.6
227dc 2e 32 33 20 6f 72 20 65 61 72 6c 69 65 72 20 64  .23 or earlier d
227dd 6f 20 69 6e 20 66 61 63 74 20 64 65 70 65 6e 64  o in fact depend
227de 20 6f 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53   on SQLITE_MISUS
227df 45 20 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e  E .    ** return
227e0 73 2c 20 61 6e 64 20 74 68 6f 73 65 20 77 65 72  s, and those wer
227e1 65 20 62 72 6f 6b 65 6e 20 62 79 20 74 68 65 20  e broken by the 
227e2 61 75 74 6f 6d 61 74 69 63 2d 72 65 73 65 74 20  automatic-reset 
227e3 63 68 61 6e 67 65 2e 20 20 41 73 20 61 0a 20 20  change.  As a.  
227e4 20 20 2a 2a 20 61 20 77 6f 72 6b 2d 61 72 6f 75    ** a work-arou
227e5 6e 64 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 4f  nd, the SQLITE_O
227e6 4d 49 54 5f 41 55 54 4f 52 45 53 45 54 20 63 6f  MIT_AUTORESET co
227e7 6d 70 69 6c 65 2d 74 69 6d 65 20 72 65 73 74 6f  mpile-time resto
227e8 72 65 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 6c  res the.    ** l
227e9 65 67 61 63 79 20 62 65 68 61 76 69 6f 72 20 6f  egacy behavior o
227ea 66 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49  f returning SQLI
227eb 54 45 5f 4d 49 53 55 53 45 20 66 6f 72 20 63 61  TE_MISUSE for ca
227ec 73 65 73 20 77 68 65 72 65 20 74 68 65 20 0a 20  ses where the . 
227ed 20 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 20 73     ** previous s
227ee 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 72 65  qlite3_step() re
227ef 74 75 72 6e 65 64 20 73 6f 6d 65 74 68 69 6e 67  turned something
227f0 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20 53 51   other than a SQ
227f1 4c 49 54 45 5f 4c 4f 43 4b 45 44 0a 20 20 20 20  LITE_LOCKED.    
227f2 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 42 55 53  ** or SQLITE_BUS
227f3 59 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a  Y error..    */.
227f4 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
227f5 49 54 5f 41 55 54 4f 52 45 53 45 54 0a 20 20 20  IT_AUTORESET.   
227f6 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49   if( p->rc==SQLI
227f7 54 45 5f 42 55 53 59 20 7c 7c 20 70 2d 3e 72 63  TE_BUSY || p->rc
227f8 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  ==SQLITE_LOCKED 
227f9 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
227fa 5f 72 65 73 65 74 28 28 73 71 6c 69 74 65 33 5f  _reset((sqlite3_
227fb 73 74 6d 74 2a 29 70 29 3b 0a 20 20 20 20 7d 65  stmt*)p);.    }e
227fc 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72  lse{.      retur
227fd 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f  n SQLITE_MISUSE_
227fe 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6c 73  BKPT;.    }.#els
227ff 65 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  e.    sqlite3_re
22800 73 65 74 28 28 73 71 6c 69 74 65 33 5f 73 74 6d  set((sqlite3_stm
22801 74 2a 29 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20  t*)p);.#endif.  
22802 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  }..  /* Check th
22803 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20  at malloc() has 
22804 6e 6f 74 20 66 61 69 6c 65 64 2e 20 49 66 20 69  not failed. If i
22805 74 20 68 61 73 2c 20 72 65 74 75 72 6e 20 65 61  t has, return ea
22806 72 6c 79 2e 20 2a 2f 0a 20 20 64 62 20 3d 20 70  rly. */.  db = p
22807 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e  ->db;.  if( db->
22808 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
22809 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49      p->rc = SQLI
2280a 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 72 65  TE_NOMEM;.    re
2280b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
2280c 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d  M;.  }..  if( p-
2280d 3e 70 63 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70  >pc<=0 && p->exp
2280e 69 72 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72  ired ){.    p->r
2280f 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  c = SQLITE_SCHEM
22810 41 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  A;.    rc = SQLI
22811 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f  TE_ERROR;.    go
22812 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 65 70 3b 0a  to end_of_step;.
22813 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c    }.  if( p->pc<
22814 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74  0 ){.    /* If t
22815 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 74 68 65  here are no othe
22816 72 20 73 74 61 74 65 6d 65 6e 74 73 20 63 75 72  r statements cur
22817 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 2c 20  rently running, 
22818 74 68 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 65  then.    ** rese
22819 74 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 20  t the interrupt 
2281a 66 6c 61 67 2e 20 20 54 68 69 73 20 70 72 65 76  flag.  This prev
2281b 65 6e 74 73 20 61 20 63 61 6c 6c 20 74 6f 20 73  ents a call to s
2281c 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
2281d 0a 20 20 20 20 2a 2a 20 66 72 6f 6d 20 69 6e 74  .    ** from int
2281e 65 72 72 75 70 74 69 6e 67 20 61 20 73 74 61 74  errupting a stat
2281f 65 6d 65 6e 74 20 74 68 61 74 20 68 61 73 20 6e  ement that has n
22820 6f 74 20 79 65 74 20 73 74 61 72 74 65 64 2e 0a  ot yet started..
22821 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64      */.    if( d
22822 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
22823 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  ==0 ){.      db-
22824 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65  >u1.isInterrupte
22825 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20  d = 0;.    }..  
22826 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 77 72    assert( db->wr
22827 69 74 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20  iteVdbeCnt>0 || 
22828 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d  db->autoCommit==
22829 30 20 7c 7c 20 64 62 2d 3e 6e 44 65 66 65 72 72  0 || db->nDeferr
2282a 65 64 43 6f 6e 73 3d 3d 30 20 29 3b 0a 0a 23 69  edCons==0 );..#i
2282b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2282c 54 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 20  T_TRACE.    if( 
2282d 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20  db->xProfile && 
2282e 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29  !db->init.busy )
2282f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  {.      sqlite3O
22830 73 43 75 72 72 65 6e 74 54 69 6d 65 49 6e 74 36  sCurrentTimeInt6
22831 34 28 64 62 2d 3e 70 56 66 73 2c 20 26 70 2d 3e  4(db->pVfs, &p->
22832 73 74 61 72 74 54 69 6d 65 29 3b 0a 20 20 20 20  startTime);.    
22833 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 64 62  }.#endif..    db
22834 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 2b  ->activeVdbeCnt+
22835 2b 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 65  +;.    if( p->re
22836 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 64 62 2d 3e  adOnly==0 ) db->
22837 77 72 69 74 65 56 64 62 65 43 6e 74 2b 2b 3b 0a  writeVdbeCnt++;.
22838 20 20 20 20 70 2d 3e 70 63 20 3d 20 30 3b 0a 20      p->pc = 0;. 
22839 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
2283a 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20  E_OMIT_EXPLAIN. 
2283b 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20   if( p->explain 
2283c 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
2283d 74 65 33 56 64 62 65 4c 69 73 74 28 70 29 3b 0a  te3VdbeList(p);.
2283e 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
2283f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58  * SQLITE_OMIT_EX
22840 50 4c 41 49 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20  PLAIN */.  {.   
22841 20 64 62 2d 3e 76 64 62 65 45 78 65 63 43 6e 74   db->vdbeExecCnt
22842 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ++;.    rc = sql
22843 69 74 65 33 56 64 62 65 45 78 65 63 28 70 29 3b  ite3VdbeExec(p);
22844 0a 20 20 20 20 64 62 2d 3e 76 64 62 65 45 78 65  .    db->vdbeExe
22845 63 43 6e 74 2d 2d 3b 0a 20 20 7d 0a 0a 23 69 66  cCnt--;.  }..#if
22846 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
22847 5f 54 52 41 43 45 0a 20 20 2f 2a 20 49 6e 76 6f  _TRACE.  /* Invo
22848 6b 65 20 74 68 65 20 70 72 6f 66 69 6c 65 20 63  ke the profile c
22849 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 72 65  allback if there
2284a 20 69 73 20 6f 6e 65 0a 20 20 2a 2f 0a 20 20 69   is one.  */.  i
2284b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f  f( rc!=SQLITE_RO
2284c 57 20 26 26 20 64 62 2d 3e 78 50 72 6f 66 69 6c  W && db->xProfil
2284d 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74 2e 62  e && !db->init.b
2284e 75 73 79 20 26 26 20 70 2d 3e 7a 53 71 6c 20 29  usy && p->zSql )
2284f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e  {.    sqlite3_in
22850 74 36 34 20 69 4e 6f 77 3b 0a 20 20 20 20 73 71  t64 iNow;.    sq
22851 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69  lite3OsCurrentTi
22852 6d 65 49 6e 74 36 34 28 64 62 2d 3e 70 56 66 73  meInt64(db->pVfs
22853 2c 20 26 69 4e 6f 77 29 3b 0a 20 20 20 20 64 62  , &iNow);.    db
22854 2d 3e 78 50 72 6f 66 69 6c 65 28 64 62 2d 3e 70  ->xProfile(db->p
22855 50 72 6f 66 69 6c 65 41 72 67 2c 20 70 2d 3e 7a  ProfileArg, p->z
22856 53 71 6c 2c 20 28 69 4e 6f 77 20 2d 20 70 2d 3e  Sql, (iNow - p->
22857 73 74 61 72 74 54 69 6d 65 29 2a 31 30 30 30 30  startTime)*10000
22858 30 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  00);.  }.#endif.
22859 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2285a 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73  E_DONE ){.    as
2285b 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c  sert( p->rc==SQL
2285c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 70 2d  ITE_OK );.    p-
2285d 3e 72 63 20 3d 20 64 6f 57 61 6c 43 61 6c 6c 62  >rc = doWalCallb
2285e 61 63 6b 73 28 64 62 29 3b 0a 20 20 20 20 69 66  acks(db);.    if
2285f 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  ( p->rc!=SQLITE_
22860 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
22861 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
22862 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 62 2d 3e     }.  }..  db->
22863 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20  errCode = rc;.  
22864 69 66 28 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  if( SQLITE_NOMEM
22865 3d 3d 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  ==sqlite3ApiExit
22866 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 29 20 29  (p->db, p->rc) )
22867 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51  {.    p->rc = SQ
22868 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
22869 65 6e 64 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 2f  end_of_step:.  /
2286a 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20  * At this point 
2286b 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20 72  local variable r
2286c 63 20 68 6f 6c 64 73 20 74 68 65 20 76 61 6c 75  c holds the valu
2286d 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  e that should be
2286e 20 0a 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 20   .  ** returned 
2286f 69 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e  if this statemen
22870 74 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 75  t was compiled u
22871 73 69 6e 67 20 74 68 65 20 6c 65 67 61 63 79 20  sing the legacy 
22872 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72  .  ** sqlite3_pr
22873 65 70 61 72 65 28 29 20 69 6e 74 65 72 66 61 63  epare() interfac
22874 65 2e 20 41 63 63 6f 72 64 69 6e 67 20 74 6f 20  e. According to 
22875 74 68 65 20 64 6f 63 73 2c 20 74 68 69 73 20 63  the docs, this c
22876 61 6e 20 6f 6e 6c 79 0a 20 20 2a 2a 20 62 65 20  an only.  ** be 
22877 6f 6e 65 20 6f 66 20 74 68 65 20 76 61 6c 75 65  one of the value
22878 73 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61  s in the first a
22879 73 73 65 72 74 28 29 20 62 65 6c 6f 77 2e 20 56  ssert() below. V
2287a 61 72 69 61 62 6c 65 20 70 2d 3e 72 63 20 0a 20  ariable p->rc . 
2287b 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65   ** contains the
2287c 20 76 61 6c 75 65 20 74 68 61 74 20 77 6f 75 6c   value that woul
2287d 64 20 62 65 20 72 65 74 75 72 6e 65 64 20 69 66  d be returned if
2287e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
2287f 65 28 29 20 0a 20 20 2a 2a 20 77 65 72 65 20 63  e() .  ** were c
22880 61 6c 6c 65 64 20 6f 6e 20 73 74 61 74 65 6d 65  alled on stateme
22881 6e 74 20 70 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  nt p..  */.  ass
22882 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
22883 52 4f 57 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  ROW  || rc==SQLI
22884 54 45 5f 44 4f 4e 45 20 20 20 7c 7c 20 72 63 3d  TE_DONE   || rc=
22885 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 0a 20  =SQLITE_ERROR . 
22886 20 20 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c        || rc==SQL
22887 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d  ITE_BUSY || rc==
22888 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 0a 20 20  SQLITE_MISUSE.  
22889 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
2288a 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26  rc!=SQLITE_ROW &
2288b 26 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  & p->rc!=SQLITE_
2288c 44 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20 70 2d  DONE );.  if( p-
2288d 3e 69 73 50 72 65 70 61 72 65 56 32 20 26 26 20  >isPrepareV2 && 
2288e 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26  rc!=SQLITE_ROW &
2288f 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  & rc!=SQLITE_DON
22890 45 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74  E ){.    /* If t
22891 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 77 61  his statement wa
22892 73 20 70 72 65 70 61 72 65 64 20 75 73 69 6e 67  s prepared using
22893 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
22894 5f 76 32 28 29 2c 20 61 6e 64 20 61 6e 0a 20 20  _v2(), and an.  
22895 20 20 2a 2a 20 65 72 72 6f 72 20 68 61 73 20 6f    ** error has o
22896 63 63 75 72 65 64 2c 20 74 68 65 6e 20 72 65 74  ccured, then ret
22897 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f  urn the error co
22898 64 65 20 69 6e 20 70 2d 3e 72 63 20 74 6f 20 74  de in p->rc to t
22899 68 65 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72  he.    ** caller
2289a 2e 20 53 65 74 20 74 68 65 20 65 72 72 6f 72 20  . Set the error 
2289b 63 6f 64 65 20 69 6e 20 74 68 65 20 64 61 74 61  code in the data
2289c 62 61 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 74  base handle to t
2289d 68 65 20 73 61 6d 65 20 76 61 6c 75 65 2e 0a 20  he same value.. 
2289e 20 20 20 2a 2f 20 0a 20 20 20 20 72 63 20 3d 20     */ .    rc = 
2289f 73 71 6c 69 74 65 33 56 64 62 65 54 72 61 6e 73  sqlite3VdbeTrans
228a0 66 65 72 45 72 72 6f 72 28 70 29 3b 0a 20 20 7d  ferError(p);.  }
228a1 0a 20 20 72 65 74 75 72 6e 20 28 72 63 26 64 62  .  return (rc&db
228a2 2d 3e 65 72 72 4d 61 73 6b 29 3b 0a 7d 0a 0a 2f  ->errMask);.}../
228a3 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d  *.** The maximum
228a4 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
228a5 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e   that a statemen
228a6 74 20 77 69 6c 6c 20 74 72 79 20 74 6f 20 72 65  t will try to re
228a7 70 61 72 73 65 0a 2a 2a 20 69 74 73 65 6c 66 20  parse.** itself 
228a8 62 65 66 6f 72 65 20 67 69 76 69 6e 67 20 75 70  before giving up
228a9 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 53   and returning S
228aa 51 4c 49 54 45 5f 53 43 48 45 4d 41 2e 0a 2a 2f  QLITE_SCHEMA..*/
228ab 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
228ac 4d 41 58 5f 53 43 48 45 4d 41 5f 52 45 54 52 59  MAX_SCHEMA_RETRY
228ad 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
228ae 5f 4d 41 58 5f 53 43 48 45 4d 41 5f 52 45 54 52  _MAX_SCHEMA_RETR
228af 59 20 35 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  Y 5.#endif../*.*
228b0 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 74 6f  * This is the to
228b1 70 2d 6c 65 76 65 6c 20 69 6d 70 6c 65 6d 65 6e  p-level implemen
228b2 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  tation of sqlite
228b3 33 5f 73 74 65 70 28 29 2e 20 20 43 61 6c 6c 0a  3_step().  Call.
228b4 2a 2a 20 73 71 6c 69 74 65 33 53 74 65 70 28 29  ** sqlite3Step()
228b5 20 74 6f 20 64 6f 20 6d 6f 73 74 20 6f 66 20 74   to do most of t
228b6 68 65 20 77 6f 72 6b 2e 20 20 49 66 20 61 20 73  he work.  If a s
228b7 63 68 65 6d 61 20 65 72 72 6f 72 20 6f 63 63 75  chema error occu
228b8 72 73 2c 0a 2a 2a 20 63 61 6c 6c 20 73 71 6c 69  rs,.** call sqli
228b9 74 65 33 52 65 70 72 65 70 61 72 65 28 29 20 61  te3Reprepare() a
228ba 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 0a 2a 2f  nd try again..*/
228bb 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
228bc 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c  sqlite3_step(sql
228bd 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
228be 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
228bf 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a  LITE_OK;      /*
228c0 20 52 65 73 75 6c 74 20 66 72 6f 6d 20 73 71 6c   Result from sql
228c1 69 74 65 33 53 74 65 70 28 29 20 2a 2f 0a 20 20  ite3Step() */.  
228c2 69 6e 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45  int rc2 = SQLITE
228c3 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 52 65 73 75  _OK;     /* Resu
228c4 6c 74 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 52  lt from sqlite3R
228c5 65 70 72 65 70 61 72 65 28 29 20 2a 2f 0a 20 20  eprepare() */.  
228c6 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a  Vdbe *v = (Vdbe*
228c7 29 70 53 74 6d 74 3b 20 20 2f 2a 20 74 68 65 20  )pStmt;  /* the 
228c8 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
228c9 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 20  nt */.  int cnt 
228ca 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
228cb 20 2f 2a 20 43 6f 75 6e 74 65 72 20 74 6f 20 70   /* Counter to p
228cc 72 65 76 65 6e 74 20 69 6e 66 69 6e 69 74 65 20  revent infinite 
228cd 6c 6f 6f 70 20 6f 66 20 72 65 70 72 65 70 61 72  loop of reprepar
228ce 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  es */.  sqlite3 
228cf 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  *db;            
228d0 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
228d1 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a   connection */..
228d2 20 20 69 66 28 20 76 64 62 65 53 61 66 65 74 79    if( vdbeSafety
228d3 4e 6f 74 4e 75 6c 6c 28 76 29 20 29 7b 0a 20 20  NotNull(v) ){.  
228d4 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
228d5 4d 49 53 55 53 45 5f 42 4b 50 54 3b 0a 20 20 7d  MISUSE_BKPT;.  }
228d6 0a 20 20 64 62 20 3d 20 76 2d 3e 64 62 3b 0a 20  .  db = v->db;. 
228d7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
228d8 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
228d9 0a 20 20 76 2d 3e 64 6f 69 6e 67 52 65 72 75 6e  .  v->doingRerun
228da 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 28   = 0;.  while( (
228db 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74 65 70  rc = sqlite3Step
228dc 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 53 43 48  (v))==SQLITE_SCH
228dd 45 4d 41 0a 20 20 20 20 20 20 20 20 20 26 26 20  EMA.         && 
228de 63 6e 74 2b 2b 20 3c 20 53 51 4c 49 54 45 5f 4d  cnt++ < SQLITE_M
228df 41 58 5f 53 43 48 45 4d 41 5f 52 45 54 52 59 0a  AX_SCHEMA_RETRY.
228e0 20 20 20 20 20 20 20 20 20 26 26 20 28 72 63 32           && (rc2
228e1 20 3d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52   = rc = sqlite3R
228e2 65 70 72 65 70 61 72 65 28 76 29 29 3d 3d 53 51  eprepare(v))==SQ
228e3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
228e4 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74  qlite3_reset(pSt
228e5 6d 74 29 3b 0a 20 20 20 20 76 2d 3e 64 6f 69 6e  mt);.    v->doin
228e6 67 52 65 72 75 6e 20 3d 20 31 3b 0a 20 20 20 20  gRerun = 1;.    
228e7 61 73 73 65 72 74 28 20 76 2d 3e 65 78 70 69 72  assert( v->expir
228e8 65 64 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69  ed==0 );.  }.  i
228e9 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f  f( rc2!=SQLITE_O
228ea 4b 20 26 26 20 41 4c 57 41 59 53 28 76 2d 3e 69  K && ALWAYS(v->i
228eb 73 50 72 65 70 61 72 65 56 32 29 20 26 26 20 41  sPrepareV2) && A
228ec 4c 57 41 59 53 28 64 62 2d 3e 70 45 72 72 29 20  LWAYS(db->pErr) 
228ed 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63  ){.    /* This c
228ee 61 73 65 20 6f 63 63 75 72 73 20 61 66 74 65 72  ase occurs after
228ef 20 66 61 69 6c 69 6e 67 20 74 6f 20 72 65 63 6f   failing to reco
228f0 6d 70 69 6c 65 20 61 6e 20 73 71 6c 20 73 74 61  mpile an sql sta
228f1 74 65 6d 65 6e 74 2e 20 0a 20 20 20 20 2a 2a 20  tement. .    ** 
228f2 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  The error messag
228f3 65 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 20 63  e from the SQL c
228f4 6f 6d 70 69 6c 65 72 20 68 61 73 20 61 6c 72 65  ompiler has alre
228f5 61 64 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 20  ady been loaded 
228f6 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65  .    ** into the
228f7 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
228f8 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70  . This block cop
228f9 69 65 73 20 74 68 65 20 65 72 72 6f 72 20 6d 65  ies the error me
228fa 73 73 61 67 65 20 0a 20 20 20 20 2a 2a 20 66 72  ssage .    ** fr
228fb 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
228fc 68 61 6e 64 6c 65 20 69 6e 74 6f 20 74 68 65 20  handle into the 
228fd 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 73 65  statement and se
228fe 74 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  ts the statement
228ff 0a 20 20 20 20 2a 2a 20 70 72 6f 67 72 61 6d 20  .    ** program 
22900 63 6f 75 6e 74 65 72 20 74 6f 20 30 20 74 6f 20  counter to 0 to 
22901 65 6e 73 75 72 65 20 74 68 61 74 20 77 68 65 6e  ensure that when
22902 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
22903 73 20 0a 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69  s .    ** finali
22904 7a 65 64 20 6f 72 20 72 65 73 65 74 20 74 68 65  zed or reset the
22905 20 70 61 72 73 65 72 20 65 72 72 6f 72 20 6d 65   parser error me
22906 73 73 61 67 65 20 69 73 20 61 76 61 69 6c 61 62  ssage is availab
22907 6c 65 20 76 69 61 0a 20 20 20 20 2a 2a 20 73 71  le via.    ** sq
22908 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61  lite3_errmsg() a
22909 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  nd sqlite3_errco
2290a 64 65 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  de()..    */.   
2290b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72   const char *zEr
2290c 72 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  r = (const char 
2290d 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
2290e 74 65 78 74 28 64 62 2d 3e 70 45 72 72 29 3b 20  text(db->pErr); 
2290f 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
22910 65 65 28 64 62 2c 20 76 2d 3e 7a 45 72 72 4d 73  ee(db, v->zErrMs
22911 67 29 3b 0a 20 20 20 20 69 66 28 20 21 64 62 2d  g);.    if( !db-
22912 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
22913 0a 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73  .      v->zErrMs
22914 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  g = sqlite3DbStr
22915 44 75 70 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20  Dup(db, zErr);. 
22916 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 72 63 32       v->rc = rc2
22917 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20  ;.    } else {. 
22918 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20       v->zErrMsg 
22919 3d 20 30 3b 0a 20 20 20 20 20 20 76 2d 3e 72 63  = 0;.      v->rc
2291a 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e   = rc = SQLITE_N
2291b 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  OMEM;.    }.  }.
2291c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
2291d 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
2291e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2291f 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
22920 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
22921 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74  ./*.** Extract t
22922 68 65 20 75 73 65 72 20 64 61 74 61 20 66 72 6f  he user data fro
22923 6d 20 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  m a sqlite3_cont
22924 65 78 74 20 73 74 72 75 63 74 75 72 65 20 61 6e  ext structure an
22925 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f  d return a.** po
22926 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a  inter to it..*/.
22927 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
22928 2a 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61  *sqlite3_user_da
22929 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ta(sqlite3_conte
2292a 78 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  xt *p){.  assert
2292b 28 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20  ( p && p->pFunc 
2292c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70  );.  return p->p
2292d 46 75 6e 63 2d 3e 70 55 73 65 72 44 61 74 61 3b  Func->pUserData;
2292e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63  .}../*.** Extrac
2292f 74 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20  t the user data 
22930 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 33 5f 63  from a sqlite3_c
22931 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65  ontext structure
22932 20 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a   and return a.**
22933 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a   pointer to it..
22934 2a 2a 0a 2a 2a 20 49 4d 50 4c 45 4d 45 4e 54 41  **.** IMPLEMENTA
22935 54 49 4f 4e 2d 4f 46 3a 20 52 2d 34 36 37 39 38  TION-OF: R-46798
22936 2d 35 30 33 30 31 20 54 68 65 20 73 71 6c 69 74  -50301 The sqlit
22937 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
22938 6e 64 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65  ndle() interface
22939 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 63 6f  .** returns a co
2293a 70 79 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65  py of the pointe
2293b 72 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  r to the databas
2293c 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 74 68  e connection (th
2293d 65 20 31 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74  e 1st.** paramet
2293e 65 72 29 20 6f 66 20 74 68 65 20 73 71 6c 69 74  er) of the sqlit
2293f 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
22940 6f 6e 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69  on() and.** sqli
22941 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
22942 69 6f 6e 31 36 28 29 20 72 6f 75 74 69 6e 65 73  ion16() routines
22943 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c 6c 79   that originally
22944 20 72 65 67 69 73 74 65 72 65 64 20 74 68 65 0a   registered the.
22945 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64  ** application d
22946 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e  efined function.
22947 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
22948 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f  qlite3 *sqlite3_
22949 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
2294a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e(sqlite3_contex
2294b 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  t *p){.  assert(
2294c 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29   p && p->pFunc )
2294d 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 73 2e  ;.  return p->s.
2294e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  db;.}../*.** The
2294f 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 68   following is th
22950 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
22951 20 6f 66 20 61 6e 20 53 51 4c 20 66 75 6e 63 74   of an SQL funct
22952 69 6f 6e 20 74 68 61 74 20 61 6c 77 61 79 73 0a  ion that always.
22953 2a 2a 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e  ** fails with an
22954 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73   error message s
22955 74 61 74 69 6e 67 20 74 68 61 74 20 74 68 65 20  tating that the 
22956 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
22957 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 6f 6e 67   in the.** wrong
22958 20 63 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 73   context.  The s
22959 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f  qlite3_overload_
2295a 66 75 6e 63 74 69 6f 6e 28 29 20 41 50 49 20 6d  function() API m
2295b 69 67 68 74 20 63 6f 6e 73 74 72 75 63 74 0a 2a  ight construct.*
2295c 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74  * SQL function t
2295d 68 61 74 20 75 73 65 20 74 68 69 73 20 72 6f 75  hat use this rou
2295e 74 69 6e 65 20 73 6f 20 74 68 61 74 20 74 68 65  tine so that the
2295f 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 6c 6c 20   functions will 
22960 65 78 69 73 74 0a 2a 2a 20 66 6f 72 20 6e 61 6d  exist.** for nam
22961 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 62 75 74  e resolution but
22962 20 61 72 65 20 61 63 74 75 61 6c 6c 79 20 6f 76   are actually ov
22963 65 72 6c 6f 61 64 65 64 20 62 79 20 74 68 65 20  erloaded by the 
22964 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 0a 2a 2a  xFindFunction.**
22965 20 6d 65 74 68 6f 64 20 6f 66 20 76 69 72 74 75   method of virtu
22966 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51  al tables..*/.SQ
22967 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
22968 64 20 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64  d sqlite3Invalid
22969 46 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69  Function(.  sqli
2296a 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
2296b 74 65 78 74 2c 20 20 2f 2a 20 54 68 65 20 66 75  text,  /* The fu
2296c 6e 63 74 69 6f 6e 20 63 61 6c 6c 69 6e 67 20 63  nction calling c
2296d 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
2296e 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 20 20 20  NotUsed,        
2296f 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
22970 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f   of arguments to
22971 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
22972 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
22973 20 2a 2a 4e 6f 74 55 73 65 64 32 20 20 20 2f 2a   **NotUsed2   /*
22974 20 56 61 6c 75 65 20 6f 66 20 65 61 63 68 20 61   Value of each a
22975 72 67 75 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20  rgument */.){.  
22976 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
22977 65 20 3d 20 63 6f 6e 74 65 78 74 2d 3e 70 46 75  e = context->pFu
22978 6e 63 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 63 68 61  nc->zName;.  cha
22979 72 20 2a 7a 45 72 72 3b 0a 20 20 55 4e 55 53 45  r *zErr;.  UNUSE
2297a 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74  D_PARAMETER2(Not
2297b 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b  Used, NotUsed2);
2297c 0a 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65  .  zErr = sqlite
2297d 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20  3_mprintf(.     
2297e 20 22 75 6e 61 62 6c 65 20 74 6f 20 75 73 65 20   "unable to use 
2297f 66 75 6e 63 74 69 6f 6e 20 25 73 20 69 6e 20 74  function %s in t
22980 68 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 6e  he requested con
22981 74 65 78 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  text", zName);. 
22982 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
22983 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a  error(context, z
22984 45 72 72 2c 20 2d 31 29 3b 0a 20 20 73 71 6c 69  Err, -1);.  sqli
22985 74 65 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a  te3_free(zErr);.
22986 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
22987 65 20 6f 72 20 72 65 74 75 72 6e 20 74 68 65 20  e or return the 
22988 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74 65 78  aggregate contex
22989 74 20 66 6f 72 20 61 20 75 73 65 72 20 66 75 6e  t for a user fun
2298a 63 74 69 6f 6e 2e 20 20 41 20 6e 65 77 0a 2a 2a  ction.  A new.**
2298b 20 63 6f 6e 74 65 78 74 20 69 73 20 61 6c 6c 6f   context is allo
2298c 63 61 74 65 64 20 6f 6e 20 74 68 65 20 66 69 72  cated on the fir
2298d 73 74 20 63 61 6c 6c 2e 20 20 53 75 62 73 65 71  st call.  Subseq
2298e 75 65 6e 74 20 63 61 6c 6c 73 20 72 65 74 75 72  uent calls retur
2298f 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 6f  n the.** same co
22990 6e 74 65 78 74 20 74 68 61 74 20 77 61 73 20 72  ntext that was r
22991 65 74 75 72 6e 65 64 20 6f 6e 20 70 72 69 6f 72  eturned on prior
22992 20 63 61 6c 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54   calls..*/.SQLIT
22993 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
22994 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
22995 6e 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f  ntext(sqlite3_co
22996 6e 74 65 78 74 20 2a 70 2c 20 69 6e 74 20 6e 42  ntext *p, int nB
22997 79 74 65 29 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65  yte){.  Mem *pMe
22998 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26  m;.  assert( p &
22999 26 20 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d  & p->pFunc && p-
2299a 3e 70 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b  >pFunc->xStep );
2299b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
2299c 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
2299d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
2299e 0a 20 20 70 4d 65 6d 20 3d 20 70 2d 3e 70 4d 65  .  pMem = p->pMe
2299f 6d 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e  m;.  testcase( n
229a0 42 79 74 65 3c 30 20 29 3b 0a 20 20 69 66 28 20  Byte<0 );.  if( 
229a1 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d  (pMem->flags & M
229a2 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b 0a 20 20  EM_Agg)==0 ){.  
229a3 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 30 20 29    if( nByte<=0 )
229a4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
229a5 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74  dbeMemReleaseExt
229a6 65 72 6e 61 6c 28 70 4d 65 6d 29 3b 0a 20 20 20  ernal(pMem);.   
229a7 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
229a8 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20   MEM_Null;.     
229a9 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b 0a 20 20   pMem->z = 0;.  
229aa 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
229ab 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
229ac 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30  w(pMem, nByte, 0
229ad 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  );.      pMem->f
229ae 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 67 67 3b 0a  lags = MEM_Agg;.
229af 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 44        pMem->u.pD
229b0 65 66 20 3d 20 70 2d 3e 70 46 75 6e 63 3b 0a 20  ef = p->pFunc;. 
229b1 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a       if( pMem->z
229b2 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73   ){.        mems
229b3 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e  et(pMem->z, 0, n
229b4 42 79 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Byte);.      }. 
229b5 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
229b6 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d 2d 3e 7a  n (void*)pMem->z
229b7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
229b8 6e 20 74 68 65 20 61 75 78 69 6c 61 72 79 20 64  n the auxilary d
229b9 61 74 61 20 70 6f 69 6e 74 65 72 2c 20 69 66 20  ata pointer, if 
229ba 61 6e 79 2c 20 66 6f 72 20 74 68 65 20 69 41 72  any, for the iAr
229bb 67 27 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f  g'th argument to
229bc 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d 66 75 6e  .** the user-fun
229bd 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 62 79  ction defined by
229be 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45   pCtx..*/.SQLITE
229bf 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
229c0 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 73  e3_get_auxdata(s
229c1 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
229c2 70 43 74 78 2c 20 69 6e 74 20 69 41 72 67 29 7b  pCtx, int iArg){
229c3 0a 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64  .  VdbeFunc *pVd
229c4 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73 73 65 72  beFunc;..  asser
229c5 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
229c6 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
229c7 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 56  ->mutex) );.  pV
229c8 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e  dbeFunc = pCtx->
229c9 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66 28  pVdbeFunc;.  if(
229ca 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c 20 69   !pVdbeFunc || i
229cb 41 72 67 3e 3d 70 56 64 62 65 46 75 6e 63 2d 3e  Arg>=pVdbeFunc->
229cc 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c 30 20 29  nAux || iArg<0 )
229cd 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
229ce 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 56 64    }.  return pVd
229cf 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 41  beFunc->apAux[iA
229d0 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a 2f 2a 0a  rg].pAux;.}../*.
229d1 2a 2a 20 53 65 74 20 74 68 65 20 61 75 78 69 6c  ** Set the auxil
229d2 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72  ary data pointer
229d3 20 61 6e 64 20 64 65 6c 65 74 65 20 66 75 6e 63   and delete func
229d4 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65 20 69 41  tion, for the iA
229d5 72 67 27 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e  rg'th.** argumen
229d6 74 20 74 6f 20 74 68 65 20 75 73 65 72 2d 66 75  t to the user-fu
229d7 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 62  nction defined b
229d8 79 20 70 43 74 78 2e 20 41 6e 79 20 70 72 65 76  y pCtx. Any prev
229d9 69 6f 75 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a  ious value is.**
229da 20 64 65 6c 65 74 65 64 20 62 79 20 63 61 6c 6c   deleted by call
229db 69 6e 67 20 74 68 65 20 64 65 6c 65 74 65 20 66  ing the delete f
229dc 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65  unction specifie
229dd 64 20 77 68 65 6e 20 69 74 20 77 61 73 20 73 65  d when it was se
229de 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
229df 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 65   void sqlite3_se
229e0 74 5f 61 75 78 64 61 74 61 28 0a 20 20 73 71 6c  t_auxdata(.  sql
229e1 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
229e2 74 78 2c 20 0a 20 20 69 6e 74 20 69 41 72 67 2c  tx, .  int iArg,
229e3 20 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20   .  void *pAux, 
229e4 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 65 74  .  void (*xDelet
229e5 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 73  e)(void*).){.  s
229e6 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a 70  truct AuxData *p
229e7 41 75 78 44 61 74 61 3b 0a 20 20 56 64 62 65 46  AuxData;.  VdbeF
229e8 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 0a  unc *pVdbeFunc;.
229e9 20 20 69 66 28 20 69 41 72 67 3c 30 20 29 20 67    if( iArg<0 ) g
229ea 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a 20 20 61  oto failed;..  a
229eb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
229ec 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
229ed 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
229ee 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43    pVdbeFunc = pC
229ef 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20  tx->pVdbeFunc;. 
229f0 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 20   if( !pVdbeFunc 
229f1 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41  || pVdbeFunc->nA
229f2 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20 20 20 20  ux<=iArg ){.    
229f3 69 6e 74 20 6e 41 75 78 20 3d 20 28 70 56 64 62  int nAux = (pVdb
229f4 65 46 75 6e 63 20 3f 20 70 56 64 62 65 46 75 6e  eFunc ? pVdbeFun
229f5 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b 0a 20 20  c->nAux : 0);.  
229f6 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63 20 3d 20    int nMalloc = 
229f7 73 69 7a 65 6f 66 28 56 64 62 65 46 75 6e 63 29  sizeof(VdbeFunc)
229f8 20 2b 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74   + sizeof(struct
229f9 20 41 75 78 44 61 74 61 29 2a 69 41 72 67 3b 0a   AuxData)*iArg;.
229fa 20 20 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20      pVdbeFunc = 
229fb 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
229fc 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20 70 56 64  (pCtx->s.db, pVd
229fd 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c 6f 63 29  beFunc, nMalloc)
229fe 3b 0a 20 20 20 20 69 66 28 20 21 70 56 64 62 65  ;.    if( !pVdbe
229ff 46 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 67 6f  Func ){.      go
22a00 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d  to failed;.    }
22a01 0a 20 20 20 20 70 43 74 78 2d 3e 70 56 64 62 65  .    pCtx->pVdbe
22a02 46 75 6e 63 20 3d 20 70 56 64 62 65 46 75 6e 63  Func = pVdbeFunc
22a03 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 56  ;.    memset(&pV
22a04 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 6e  dbeFunc->apAux[n
22a05 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Aux], 0, sizeof(
22a06 73 74 72 75 63 74 20 41 75 78 44 61 74 61 29 2a  struct AuxData)*
22a07 28 69 41 72 67 2b 31 2d 6e 41 75 78 29 29 3b 0a  (iArg+1-nAux));.
22a08 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e      pVdbeFunc->n
22a09 41 75 78 20 3d 20 69 41 72 67 2b 31 3b 0a 20 20  Aux = iArg+1;.  
22a0a 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75    pVdbeFunc->pFu
22a0b 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46 75 6e 63  nc = pCtx->pFunc
22a0c 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78 44 61 74  ;.  }..  pAuxDat
22a0d 61 20 3d 20 26 70 56 64 62 65 46 75 6e 63 2d 3e  a = &pVdbeFunc->
22a0e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a 20 20 69  apAux[iArg];.  i
22a0f 66 28 20 70 41 75 78 44 61 74 61 2d 3e 70 41 75  f( pAuxData->pAu
22a10 78 20 26 26 20 70 41 75 78 44 61 74 61 2d 3e 78  x && pAuxData->x
22a11 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 20 70 41  Delete ){.    pA
22a12 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 28  uxData->xDelete(
22a13 70 41 75 78 44 61 74 61 2d 3e 70 41 75 78 29 3b  pAuxData->pAux);
22a14 0a 20 20 7d 0a 20 20 70 41 75 78 44 61 74 61 2d  .  }.  pAuxData-
22a15 3e 70 41 75 78 20 3d 20 70 41 75 78 3b 0a 20 20  >pAux = pAux;.  
22a16 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 74  pAuxData->xDelet
22a17 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a 20 20 72  e = xDelete;.  r
22a18 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65 64 3a 0a  eturn;..failed:.
22a19 20 20 69 66 28 20 78 44 65 6c 65 74 65 20 29 7b    if( xDelete ){
22a1a 0a 20 20 20 20 78 44 65 6c 65 74 65 28 70 41 75  .    xDelete(pAu
22a1b 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64  x);.  }.}..#ifnd
22a1c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
22a1d 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20  EPRECATED./*.** 
22a1e 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
22a1f 72 20 6f 66 20 74 69 6d 65 73 20 74 68 65 20 53  r of times the S
22a20 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20  tep function of 
22a21 61 20 61 67 67 72 65 67 61 74 65 20 68 61 73 20  a aggregate has 
22a22 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c 65 64 2e  been .** called.
22a23 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
22a24 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61 74  tion is deprecat
22a25 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75 73 65 20  ed.  Do not use 
22a26 69 74 20 66 6f 72 20 6e 65 77 20 63 6f 64 65 2e  it for new code.
22a27 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76 69    It is.** provi
22a28 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76 6f 69 64  de only to avoid
22a29 20 62 72 65 61 6b 69 6e 67 20 6c 65 67 61 63 79   breaking legacy
22a2a 20 63 6f 64 65 2e 20 20 4e 65 77 20 61 67 67 72   code.  New aggr
22a2b 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a 2a  egate function.*
22a2c 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
22a2d 73 20 73 68 6f 75 6c 64 20 6b 65 65 70 20 74 68  s should keep th
22a2e 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74 73 20 77  eir own counts w
22a2f 69 74 68 69 6e 20 74 68 65 69 72 20 61 67 67 72  ithin their aggr
22a30 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74 65 78 74  egate.** context
22a31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
22a32 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 67 72  int sqlite3_aggr
22a33 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c 69  egate_count(sqli
22a34 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b  te3_context *p){
22a35 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20  .  assert( p && 
22a36 70 2d 3e 70 4d 65 6d 20 26 26 20 70 2d 3e 70 46  p->pMem && p->pF
22a37 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 2d  unc && p->pFunc-
22a38 3e 78 53 74 65 70 20 29 3b 0a 20 20 72 65 74 75  >xStep );.  retu
22a39 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e 6e 3b 0a 7d  rn p->pMem->n;.}
22a3a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
22a3b 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
22a3c 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
22a3d 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f  he result set fo
22a3e 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  r the statement 
22a3f 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pStmt..*/.SQLITE
22a40 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
22a41 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71  _column_count(sq
22a42 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
22a43 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 20  t){.  Vdbe *pVm 
22a44 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
22a45 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 20 3f 20  .  return pVm ? 
22a46 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20  pVm->nResColumn 
22a47 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  : 0;.}../*.** Re
22a48 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
22a49 6f 66 20 76 61 6c 75 65 73 20 61 76 61 69 6c 61  of values availa
22a4a 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 63 75 72  ble from the cur
22a4b 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 0a  rent row of the.
22a4c 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65  ** currently exe
22a4d 63 75 74 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  cuting statement
22a4e 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54   pStmt..*/.SQLIT
22a4f 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
22a50 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c  3_data_count(sql
22a51 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
22a52 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 20 3d  ){.  Vdbe *pVm =
22a53 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a   (Vdbe *)pStmt;.
22a54 20 20 69 66 28 20 70 56 6d 3d 3d 30 20 7c 7c 20    if( pVm==0 || 
22a55 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 3d  pVm->pResultSet=
22a56 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
22a57 20 72 65 74 75 72 6e 20 70 56 6d 2d 3e 6e 52 65   return pVm->nRe
22a58 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a  sColumn;.}.../*.
22a59 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  ** Check to see 
22a5a 69 66 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20 6f  if column iCol o
22a5b 66 20 74 68 65 20 67 69 76 65 6e 20 73 74 61 74  f the given stat
22a5c 65 6d 65 6e 74 20 69 73 20 76 61 6c 69 64 2e 20  ement is valid. 
22a5d 20 49 66 0a 2a 2a 20 69 74 20 69 73 2c 20 72 65   If.** it is, re
22a5e 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
22a5f 6f 20 74 68 65 20 4d 65 6d 20 66 6f 72 20 74 68  o the Mem for th
22a60 65 20 76 61 6c 75 65 20 6f 66 20 74 68 61 74 20  e value of that 
22a61 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49 66 20 69 43  column..** If iC
22a62 6f 6c 20 69 73 20 6e 6f 74 20 76 61 6c 69 64 2c  ol is not valid,
22a63 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
22a64 72 20 74 6f 20 61 20 4d 65 6d 20 77 68 69 63 68  r to a Mem which
22a65 20 68 61 73 20 61 20 76 61 6c 75 65 0a 2a 2a 20   has a value.** 
22a66 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74  of NULL..*/.stat
22a67 69 63 20 4d 65 6d 20 2a 63 6f 6c 75 6d 6e 4d 65  ic Mem *columnMe
22a68 6d 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  m(sqlite3_stmt *
22a69 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
22a6a 20 56 64 62 65 20 2a 70 56 6d 3b 0a 20 20 4d 65   Vdbe *pVm;.  Me
22a6b 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20  m *pOut;..  pVm 
22a6c 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
22a6d 0a 20 20 69 66 28 20 70 56 6d 20 26 26 20 70 56  .  if( pVm && pV
22a6e 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 21 3d 30  m->pResultSet!=0
22a6f 20 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43   && i<pVm->nResC
22a70 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b  olumn && i>=0 ){
22a71 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
22a72 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62  ex_enter(pVm->db
22a73 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 70 4f  ->mutex);.    pO
22a74 75 74 20 3d 20 26 70 56 6d 2d 3e 70 52 65 73 75  ut = &pVm->pResu
22a75 6c 74 53 65 74 5b 69 5d 3b 0a 20 20 7d 65 6c 73  ltSet[i];.  }els
22a76 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  e{.    /* If the
22a77 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73   value passed as
22a78 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
22a79 6d 65 6e 74 20 69 73 20 6f 75 74 20 6f 66 20 72  ment is out of r
22a7a 61 6e 67 65 2c 20 72 65 74 75 72 6e 0a 20 20 20  ange, return.   
22a7b 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   ** a pointer to
22a7c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
22a7d 74 61 74 69 63 20 4d 65 6d 20 6f 62 6a 65 63 74  tatic Mem object
22a7e 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e 73 20   which contains 
22a7f 74 68 65 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65  the.    ** value
22a80 20 53 51 4c 20 4e 55 4c 4c 2e 20 45 76 65 6e 20   SQL NULL. Even 
22a81 74 68 6f 75 67 68 20 74 68 65 20 4d 65 6d 20 73  though the Mem s
22a82 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
22a83 73 20 61 6e 20 65 6c 65 6d 65 6e 74 0a 20 20 20  s an element.   
22a84 20 2a 2a 20 6f 66 20 74 79 70 65 20 69 36 34 2c   ** of type i64,
22a85 20 6f 6e 20 63 65 72 74 61 69 6e 20 61 72 63 68   on certain arch
22a86 69 74 65 63 74 75 72 65 73 20 28 78 38 36 29 20  itectures (x86) 
22a87 77 69 74 68 20 63 65 72 74 61 69 6e 20 63 6f 6d  with certain com
22a88 70 69 6c 65 72 0a 20 20 20 20 2a 2a 20 73 77 69  piler.    ** swi
22a89 74 63 68 65 73 20 28 2d 4f 73 29 2c 20 67 63 63  tches (-Os), gcc
22a8a 20 6d 61 79 20 61 6c 69 67 6e 20 74 68 69 73 20   may align this 
22a8b 4d 65 6d 20 6f 62 6a 65 63 74 20 6f 6e 20 61 20  Mem object on a 
22a8c 34 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 0a  4-byte boundary.
22a8d 20 20 20 20 2a 2a 20 69 6e 73 74 65 61 64 20 6f      ** instead o
22a8e 66 20 61 6e 20 38 2d 62 79 74 65 20 6f 6e 65 2e  f an 8-byte one.
22a8f 20 54 68 69 73 20 61 6c 6c 20 77 6f 72 6b 73 20   This all works 
22a90 66 69 6e 65 2c 20 65 78 63 65 70 74 20 74 68 61  fine, except tha
22a91 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 72 75  t when.    ** ru
22a92 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 4c 49 54  nning with SQLIT
22a93 45 5f 44 45 42 55 47 20 64 65 66 69 6e 65 64 20  E_DEBUG defined 
22a94 74 68 65 20 53 51 4c 69 74 65 20 63 6f 64 65 20  the SQLite code 
22a95 73 6f 6d 65 74 69 6d 65 73 20 61 73 73 65 72 74  sometimes assert
22a96 28 29 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  ()s.    ** that 
22a97 61 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20  a Mem structure 
22a98 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 20 61 6e  is located on an
22a99 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79   8-byte boundary
22a9a 2e 20 54 6f 20 70 72 65 76 65 6e 74 0a 20 20 20  . To prevent.   
22a9b 20 2a 2a 20 74 68 65 73 65 20 61 73 73 65 72 74   ** these assert
22a9c 28 29 73 20 66 72 6f 6d 20 66 61 69 6c 69 6e 67  ()s from failing
22a9d 2c 20 77 68 65 6e 20 62 75 69 6c 64 69 6e 67 20  , when building 
22a9e 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55  with SQLITE_DEBU
22a9f 47 20 64 65 66 69 6e 65 64 0a 20 20 20 20 2a 2a  G defined.    **
22aa0 20 75 73 69 6e 67 20 67 63 63 2c 20 77 65 20 66   using gcc, we f
22aa1 6f 72 63 65 20 6e 75 6c 6c 4d 65 6d 20 74 6f 20  orce nullMem to 
22aa2 62 65 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65  be 8-byte aligne
22aa3 64 20 75 73 69 6e 67 20 74 68 65 20 6d 61 67 69  d using the magi
22aa4 63 61 6c 0a 20 20 20 20 2a 2a 20 5f 5f 61 74 74  cal.    ** __att
22aa5 72 69 62 75 74 65 5f 5f 28 28 61 6c 69 67 6e 65  ribute__((aligne
22aa6 64 28 38 29 29 29 20 6d 61 63 72 6f 2e 20 20 2a  d(8))) macro.  *
22aa7 2f 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  /.    static con
22aa8 73 74 20 4d 65 6d 20 6e 75 6c 6c 4d 65 6d 20 0a  st Mem nullMem .
22aa9 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
22aaa 54 45 5f 44 45 42 55 47 29 20 26 26 20 64 65 66  TE_DEBUG) && def
22aab 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 20  ined(__GNUC__). 
22aac 20 20 20 20 20 5f 5f 61 74 74 72 69 62 75 74 65       __attribute
22aad 5f 5f 28 28 61 6c 69 67 6e 65 64 28 38 29 29 29  __((aligned(8)))
22aae 20 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 3d   .#endif.      =
22aaf 20 7b 30 2c 20 22 22 2c 20 28 64 6f 75 62 6c 65   {0, "", (double
22ab0 29 30 2c 20 7b 30 7d 2c 20 30 2c 20 4d 45 4d 5f  )0, {0}, 0, MEM_
22ab1 4e 75 6c 6c 2c 20 53 51 4c 49 54 45 5f 4e 55 4c  Null, SQLITE_NUL
22ab2 4c 2c 20 30 2c 0a 23 69 66 64 65 66 20 53 51 4c  L, 0,.#ifdef SQL
22ab3 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20  ITE_DEBUG.      
22ab4 20 20 20 30 2c 20 30 2c 20 20 2f 2a 20 70 53 63     0, 0,  /* pSc
22ab5 6f 70 79 46 72 6f 6d 2c 20 70 46 69 6c 6c 65 72  opyFrom, pFiller
22ab6 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 20 20 20   */.#endif.     
22ab7 20 20 20 20 30 2c 20 30 20 7d 3b 0a 0a 20 20 20      0, 0 };..   
22ab8 20 69 66 28 20 70 56 6d 20 26 26 20 41 4c 57 41   if( pVm && ALWA
22ab9 59 53 28 70 56 6d 2d 3e 64 62 29 20 29 7b 0a 20  YS(pVm->db) ){. 
22aba 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
22abb 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62  ex_enter(pVm->db
22abc 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ->mutex);.      
22abd 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 56 6d  sqlite3Error(pVm
22abe 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e  ->db, SQLITE_RAN
22abf 47 45 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  GE, 0);.    }.  
22ac0 20 20 70 4f 75 74 20 3d 20 28 4d 65 6d 2a 29 26    pOut = (Mem*)&
22ac1 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72  nullMem;.  }.  r
22ac2 65 74 75 72 6e 20 70 4f 75 74 3b 0a 7d 0a 0a 2f  eturn pOut;.}../
22ac3 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
22ac4 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74  on is called aft
22ac5 65 72 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 73  er invoking an s
22ac6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 58 58 58  qlite3_value_XXX
22ac7 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 61 20 0a   function on a .
22ac8 2a 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20  ** column value 
22ac9 28 69 2e 65 2e 20 61 20 76 61 6c 75 65 20 72 65  (i.e. a value re
22aca 74 75 72 6e 65 64 20 62 79 20 65 76 61 6c 75 61  turned by evalua
22acb 74 69 6e 67 20 61 6e 20 53 51 4c 20 65 78 70 72  ting an SQL expr
22acc 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 2a 2a  ession in the.**
22acd 20 73 65 6c 65 63 74 20 6c 69 73 74 20 6f 66 20   select list of 
22ace 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
22acf 6e 74 29 20 74 68 61 74 20 6d 61 79 20 63 61 75  nt) that may cau
22ad0 73 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  se a malloc() fa
22ad1 69 6c 75 72 65 2e 20 49 66 20 0a 2a 2a 20 6d 61  ilure. If .** ma
22ad2 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65  lloc() has faile
22ad3 64 2c 20 74 68 65 20 74 68 72 65 61 64 73 20 6d  d, the threads m
22ad4 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
22ad5 20 69 73 20 63 6c 65 61 72 65 64 20 61 6e 64 20   is cleared and 
22ad6 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f  the result.** co
22ad7 64 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20  de of statement 
22ad8 70 53 74 6d 74 20 73 65 74 20 74 6f 20 53 51 4c  pStmt set to SQL
22ad9 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a  ITE_NOMEM..**.**
22ada 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74   Specifically, t
22adb 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 66 72  his is called fr
22adc 6f 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a  om within:.**.**
22add 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c       sqlite3_col
22ade 75 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20 20 20 20  umn_int().**    
22adf 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
22ae0 69 6e 74 36 34 28 29 0a 2a 2a 20 20 20 20 20 73  int64().**     s
22ae1 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
22ae2 78 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  xt().**     sqli
22ae3 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
22ae4 36 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  6().**     sqlit
22ae5 65 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61 6c 28 29  e3_column_real()
22ae6 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .**     sqlite3_
22ae7 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a  column_bytes().*
22ae8 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  *     sqlite3_co
22ae9 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 0a 2a  lumn_bytes16().*
22aea 2a 20 20 20 20 20 73 71 69 69 74 65 33 5f 63 6f  *     sqiite3_co
22aeb 6c 75 6d 6e 5f 62 6c 6f 62 28 29 0a 2a 2f 0a 73  lumn_blob().*/.s
22aec 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6c 75 6d  tatic void colum
22aed 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 73  nMallocFailure(s
22aee 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
22aef 6d 74 29 0a 7b 0a 20 20 2f 2a 20 49 66 20 6d 61  mt).{.  /* If ma
22af0 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 64 75  lloc() failed du
22af1 72 69 6e 67 20 61 6e 20 65 6e 63 6f 64 69 6e 67  ring an encoding
22af2 20 63 6f 6e 76 65 72 73 69 6f 6e 20 77 69 74 68   conversion with
22af3 69 6e 20 61 6e 0a 20 20 2a 2a 20 73 71 6c 69 74  in an.  ** sqlit
22af4 65 33 5f 63 6f 6c 75 6d 6e 5f 58 58 58 20 41 50  e3_column_XXX AP
22af5 49 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65 20  I, then set the 
22af6 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 74  return code of t
22af7 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 0a  he statement to.
22af8 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45    ** SQLITE_NOME
22af9 4d 2e 20 54 68 65 20 6e 65 78 74 20 63 61 6c 6c  M. The next call
22afa 20 74 6f 20 5f 73 74 65 70 28 29 20 28 69 66 20   to _step() (if 
22afb 61 6e 79 29 20 77 69 6c 6c 20 72 65 74 75 72 6e  any) will return
22afc 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 0a 20 20   SQLITE_ERROR.  
22afd 2a 2a 20 61 6e 64 20 5f 66 69 6e 61 6c 69 7a 65  ** and _finalize
22afe 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e  () will return N
22aff 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20 56 64 62  OMEM..  */.  Vdb
22b00 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70  e *p = (Vdbe *)p
22b01 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 20 29 7b  Stmt;.  if( p ){
22b02 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c  .    p->rc = sql
22b03 69 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64  ite3ApiExit(p->d
22b04 62 2c 20 70 2d 3e 72 63 29 3b 0a 20 20 20 20 73  b, p->rc);.    s
22b05 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
22b06 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
22b07 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  ;.  }.}../******
22b08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22b09 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63  ****** sqlite3_c
22b0a 6f 6c 75 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a  olumn_  ********
22b0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22b0c 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66  *******.** The f
22b0d 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
22b0e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 63  s are used to ac
22b0f 63 65 73 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66  cess elements of
22b10 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77   the current row
22b11 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c  .** in the resul
22b12 74 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  t set..*/.SQLITE
22b13 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
22b14 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
22b15 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d  blob(sqlite3_stm
22b16 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
22b17 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  {.  const void *
22b18 76 61 6c 3b 0a 20 20 76 61 6c 20 3d 20 73 71 6c  val;.  val = sql
22b19 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
22b1a 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
22b1b 2c 69 29 20 29 3b 0a 20 20 2f 2a 20 45 76 65 6e  ,i) );.  /* Even
22b1c 20 74 68 6f 75 67 68 20 74 68 65 72 65 20 69 73   though there is
22b1d 20 6e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e   no encoding con
22b1e 76 65 72 73 69 6f 6e 2c 20 76 61 6c 75 65 5f 62  version, value_b
22b1f 6c 6f 62 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a  lob() might.  **
22b20 20 6e 65 65 64 20 74 6f 20 63 61 6c 6c 20 6d 61   need to call ma
22b21 6c 6c 6f 63 28 29 20 74 6f 20 65 78 70 61 6e 64  lloc() to expand
22b22 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61   the result of a
22b23 20 7a 65 72 6f 62 6c 6f 62 28 29 20 0a 20 20 2a   zeroblob() .  *
22b24 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 0a 20  * expression. . 
22b25 20 2a 2f 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c   */.  columnMall
22b26 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29  ocFailure(pStmt)
22b27 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a  ;.  return val;.
22b28 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
22b29 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
22b2a 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74  bytes(sqlite3_st
22b2b 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
22b2c 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73  ){.  int val = s
22b2d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
22b2e 65 73 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53  es( columnMem(pS
22b2f 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75  tmt,i) );.  colu
22b30 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28  mnMallocFailure(
22b31 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e  pStmt);.  return
22b32 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41   val;.}.SQLITE_A
22b33 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
22b34 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 71  olumn_bytes16(sq
22b35 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
22b36 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74  t, int i){.  int
22b37 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76   val = sqlite3_v
22b38 61 6c 75 65 5f 62 79 74 65 73 31 36 28 20 63 6f  alue_bytes16( co
22b39 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29  lumnMem(pStmt,i)
22b3a 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c   );.  columnMall
22b3b 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29  ocFailure(pStmt)
22b3c 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a  ;.  return val;.
22b3d 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75  }.SQLITE_API dou
22b3e 62 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ble sqlite3_colu
22b3f 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65  mn_double(sqlite
22b40 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
22b41 6e 74 20 69 29 7b 0a 20 20 64 6f 75 62 6c 65 20  nt i){.  double 
22b42 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  val = sqlite3_va
22b43 6c 75 65 5f 64 6f 75 62 6c 65 28 20 63 6f 6c 75  lue_double( colu
22b44 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
22b45 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63  ;.  columnMalloc
22b46 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a  Failure(pStmt);.
22b47 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a    return val;.}.
22b48 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
22b49 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
22b4a 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
22b4b 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
22b4c 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74   int val = sqlit
22b4d 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f  e3_value_int( co
22b4e 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29  lumnMem(pStmt,i)
22b4f 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c   );.  columnMall
22b50 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29  ocFailure(pStmt)
22b51 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a  ;.  return val;.
22b52 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  }.SQLITE_API sql
22b53 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  ite_int64 sqlite
22b54 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73  3_column_int64(s
22b55 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
22b56 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 73 71  mt, int i){.  sq
22b57 6c 69 74 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d  lite_int64 val =
22b58 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
22b59 6e 74 36 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28  nt64( columnMem(
22b5a 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f  pStmt,i) );.  co
22b5b 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
22b5c 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75  e(pStmt);.  retu
22b5d 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45  rn val;.}.SQLITE
22b5e 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67  _API const unsig
22b5f 6e 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ned char *sqlite
22b60 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71  3_column_text(sq
22b61 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
22b62 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e  t, int i){.  con
22b63 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
22b64 20 2a 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f   *val = sqlite3_
22b65 76 61 6c 75 65 5f 74 65 78 74 28 20 63 6f 6c 75  value_text( colu
22b66 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
22b67 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63  ;.  columnMalloc
22b68 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a  Failure(pStmt);.
22b69 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a    return val;.}.
22b6a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
22b6b 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65  e3_value *sqlite
22b6c 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73  3_column_value(s
22b6d 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
22b6e 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65  mt, int i){.  Me
22b6f 6d 20 2a 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e  m *pOut = column
22b70 4d 65 6d 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20  Mem(pStmt, i);. 
22b71 20 69 66 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73   if( pOut->flags
22b72 26 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20  &MEM_Static ){. 
22b73 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26     pOut->flags &
22b74 3d 20 7e 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20  = ~MEM_Static;. 
22b75 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c     pOut->flags |
22b76 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d  = MEM_Ephem;.  }
22b77 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
22b78 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
22b79 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33   return (sqlite3
22b7a 5f 76 61 6c 75 65 20 2a 29 70 4f 75 74 3b 0a 7d  _value *)pOut;.}
22b7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22b7c 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54  OMIT_UTF16.SQLIT
22b7d 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
22b7e 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
22b7f 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f  _text16(sqlite3_
22b80 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
22b81 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69   i){.  const voi
22b82 64 20 2a 76 61 6c 20 3d 20 73 71 6c 69 74 65 33  d *val = sqlite3
22b83 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 20 63  _value_text16( c
22b84 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
22b85 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c  ) );.  columnMal
22b86 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74  locFailure(pStmt
22b87 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b  );.  return val;
22b88 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
22b89 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
22b8a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
22b8b 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
22b8c 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  type(sqlite3_stm
22b8d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
22b8e 7b 0a 20 20 69 6e 74 20 69 54 79 70 65 20 3d 20  {.  int iType = 
22b8f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
22b90 70 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53  pe( columnMem(pS
22b91 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75  tmt,i) );.  colu
22b92 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28  mnMallocFailure(
22b93 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e  pStmt);.  return
22b94 20 69 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 54 68   iType;.}../* Th
22b95 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63  e following func
22b96 74 69 6f 6e 20 69 73 20 65 78 70 65 72 69 6d 65  tion is experime
22b97 6e 74 61 6c 20 61 6e 64 20 73 75 62 6a 65 63 74  ntal and subject
22b98 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a   to change or.**
22b99 20 72 65 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a 69 6e   removal */./*in
22b9a 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
22b9b 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71  _numeric_type(sq
22b9c 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
22b9d 74 2c 20 69 6e 74 20 69 29 7b 0a 2a 2a 20 20 72  t, int i){.**  r
22b9e 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 61  eturn sqlite3_va
22b9f 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65  lue_numeric_type
22ba0 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d  ( columnMem(pStm
22ba1 74 2c 69 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a  t,i) );.**}.*/..
22ba2 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68  /*.** Convert th
22ba3 65 20 4e 2d 74 68 20 65 6c 65 6d 65 6e 74 20 6f  e N-th element o
22ba4 66 20 70 53 74 6d 74 2d 3e 70 43 6f 6c 4e 61 6d  f pStmt->pColNam
22ba5 65 5b 5d 20 69 6e 74 6f 20 61 20 73 74 72 69 6e  e[] into a strin
22ba6 67 20 75 73 69 6e 67 0a 2a 2a 20 78 46 75 6e 63  g using.** xFunc
22ba7 28 29 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74  () then return t
22ba8 68 61 74 20 73 74 72 69 6e 67 2e 20 20 49 66 20  hat string.  If 
22ba9 4e 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  N is out of rang
22baa 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a  e, return 0..**.
22bab 2a 2a 20 54 68 65 72 65 20 61 72 65 20 75 70 20  ** There are up 
22bac 74 6f 20 35 20 6e 61 6d 65 73 20 66 6f 72 20 65  to 5 names for e
22bad 61 63 68 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65  ach column.  use
22bae 54 79 70 65 20 64 65 74 65 72 6d 69 6e 65 73 20  Type determines 
22baf 77 68 69 63 68 0a 2a 2a 20 6e 61 6d 65 20 69 73  which.** name is
22bb0 20 72 65 74 75 72 6e 65 64 2e 20 20 48 65 72 65   returned.  Here
22bb1 20 61 72 65 20 74 68 65 20 6e 61 6d 65 73 3a 0a   are the names:.
22bb2 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20  **.**    0      
22bb3 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20  The column name 
22bb4 61 73 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20  as it should be 
22bb5 64 69 73 70 6c 61 79 65 64 20 66 6f 72 20 6f 75  displayed for ou
22bb6 74 70 75 74 0a 2a 2a 20 20 20 20 31 20 20 20 20  tput.**    1    
22bb7 20 20 54 68 65 20 64 61 74 61 74 79 70 65 20 6e    The datatype n
22bb8 61 6d 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75  ame for the colu
22bb9 6d 6e 0a 2a 2a 20 20 20 20 32 20 20 20 20 20 20  mn.**    2      
22bba 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
22bbb 64 61 74 61 62 61 73 65 20 74 68 61 74 20 74 68  database that th
22bbc 65 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73  e column derives
22bbd 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 33 20 20 20   from.**    3   
22bbe 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74     The name of t
22bbf 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 74 68  he table that th
22bc0 65 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73  e column derives
22bc1 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 34 20 20 20   from.**    4   
22bc2 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74     The name of t
22bc3 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20  he table column 
22bc4 74 68 61 74 20 74 68 65 20 72 65 73 75 6c 74 20  that the result 
22bc5 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66  column derives f
22bc6 72 6f 6d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  rom.**.** If the
22bc7 20 72 65 73 75 6c 74 20 69 73 20 6e 6f 74 20 61   result is not a
22bc8 20 73 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72   simple column r
22bc9 65 66 65 72 65 6e 63 65 20 28 69 66 20 69 74 20  eference (if it 
22bca 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  is an expression
22bcb 0a 2a 2a 20 6f 72 20 61 20 63 6f 6e 73 74 61 6e  .** or a constan
22bcc 74 29 20 74 68 65 6e 20 75 73 65 54 79 70 65 73  t) then useTypes
22bcd 20 32 2c 20 33 2c 20 61 6e 64 20 34 20 72 65 74   2, 3, and 4 ret
22bce 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61  urn NULL..*/.sta
22bcf 74 69 63 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  tic const void *
22bd0 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 73 71  columnName(.  sq
22bd1 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
22bd2 74 2c 0a 20 20 69 6e 74 20 4e 2c 0a 20 20 63 6f  t,.  int N,.  co
22bd3 6e 73 74 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e  nst void *(*xFun
22bd4 63 29 28 4d 65 6d 2a 29 2c 0a 20 20 69 6e 74 20  c)(Mem*),.  int 
22bd5 75 73 65 54 79 70 65 0a 29 7b 0a 20 20 63 6f 6e  useType.){.  con
22bd6 73 74 20 76 6f 69 64 20 2a 72 65 74 20 3d 20 30  st void *ret = 0
22bd7 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  ;.  Vdbe *p = (V
22bd8 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69  dbe *)pStmt;.  i
22bd9 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20  nt n;.  sqlite3 
22bda 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 0a  *db = p->db;.  .
22bdb 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20    assert( db!=0 
22bdc 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33  );.  n = sqlite3
22bdd 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53  _column_count(pS
22bde 74 6d 74 29 3b 0a 20 20 69 66 28 20 4e 3c 6e 20  tmt);.  if( N<n 
22bdf 26 26 20 4e 3e 3d 30 20 29 7b 0a 20 20 20 20 4e  && N>=0 ){.    N
22be0 20 2b 3d 20 75 73 65 54 79 70 65 2a 6e 3b 0a 20   += useType*n;. 
22be1 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
22be2 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
22be3 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  );.    assert( d
22be4 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d  b->mallocFailed=
22be5 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 20 3d 20  =0 );.    ret = 
22be6 78 46 75 6e 63 28 26 70 2d 3e 61 43 6f 6c 4e 61  xFunc(&p->aColNa
22be7 6d 65 5b 4e 5d 29 3b 0a 20 20 20 20 20 2f 2a 20  me[N]);.     /* 
22be8 41 20 6d 61 6c 6c 6f 63 20 6d 61 79 20 68 61 76  A malloc may hav
22be9 65 20 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20  e failed inside 
22bea 6f 66 20 74 68 65 20 78 46 75 6e 63 28 29 20 63  of the xFunc() c
22beb 61 6c 6c 2e 20 49 66 20 74 68 69 73 0a 20 20 20  all. If this.   
22bec 20 2a 2a 20 69 73 20 74 68 65 20 63 61 73 65 2c   ** is the case,
22bed 20 63 6c 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f   clear the mallo
22bee 63 46 61 69 6c 65 64 20 66 6c 61 67 20 61 6e 64  cFailed flag and
22bef 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 20 20   return NULL..  
22bf0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d    */.    if( db-
22bf1 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
22bf2 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
22bf3 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20  cFailed = 0;.   
22bf4 20 20 20 72 65 74 20 3d 20 30 3b 0a 20 20 20 20     ret = 0;.    
22bf5 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  }.    sqlite3_mu
22bf6 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
22bf7 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tex);.  }.  retu
22bf8 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn ret;.}../*.**
22bf9 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65   Return the name
22bfa 20 6f 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75   of the Nth colu
22bfb 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  mn of the result
22bfc 20 73 65 74 20 72 65 74 75 72 6e 65 64 20 62 79   set returned by
22bfd 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e   SQL.** statemen
22bfe 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49  t pStmt..*/.SQLI
22bff 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
22c00 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r *sqlite3_colum
22c01 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73  n_name(sqlite3_s
22c02 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
22c03 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c  N){.  return col
22c04 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70  umnName(.      p
22c05 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20  Stmt, N, (const 
22c06 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73  void*(*)(Mem*))s
22c07 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
22c08 74 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29  t, COLNAME_NAME)
22c09 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
22c0a 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51  TE_OMIT_UTF16.SQ
22c0b 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
22c0c 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
22c0d 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74  umn_name16(sqlit
22c0e 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
22c0f 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e  int N){.  return
22c10 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20   columnName(.   
22c11 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f     pStmt, N, (co
22c12 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d  nst void*(*)(Mem
22c13 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *))sqlite3_value
22c14 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45  _text16, COLNAME
22c15 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69 66  _NAME);.}.#endif
22c16 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61 69  ../*.** Constrai
22c17 6e 74 3a 20 20 49 66 20 79 6f 75 20 68 61 76 65  nt:  If you have
22c18 20 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d   ENABLE_COLUMN_M
22c19 45 54 41 44 41 54 41 20 74 68 65 6e 20 79 6f 75  ETADATA then you
22c1a 20 6d 75 73 74 0a 2a 2a 20 6e 6f 74 20 64 65 66   must.** not def
22c1b 69 6e 65 20 4f 4d 49 54 5f 44 45 43 4c 54 59 50  ine OMIT_DECLTYP
22c1c 45 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  E..*/.#if define
22c1d 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  d(SQLITE_OMIT_DE
22c1e 43 4c 54 59 50 45 29 20 26 26 20 64 65 66 69 6e  CLTYPE) && defin
22c1f 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
22c20 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
22c21 29 0a 23 20 65 72 72 6f 72 20 22 4d 75 73 74 20  ).# error "Must 
22c22 6e 6f 74 20 64 65 66 69 6e 65 20 62 6f 74 68 20  not define both 
22c23 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c  SQLITE_OMIT_DECL
22c24 54 59 50 45 20 5c 0a 20 20 20 20 20 20 20 20 20  TYPE \.         
22c25 61 6e 64 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  and SQLITE_ENABL
22c26 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54  E_COLUMN_METADAT
22c27 41 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  A".#endif..#ifnd
22c28 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
22c29 45 43 4c 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52 65  ECLTYPE./*.** Re
22c2a 74 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20  turn the column 
22c2b 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
22c2c 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29   (if applicable)
22c2d 20 6f 66 20 74 68 65 20 27 69 27 74 68 20 63 6f   of the 'i'th co
22c2e 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 72  lumn.** of the r
22c2f 65 73 75 6c 74 20 73 65 74 20 6f 66 20 53 51 4c  esult set of SQL
22c30 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74   statement pStmt
22c31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
22c32 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
22c33 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  te3_column_declt
22c34 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ype(sqlite3_stmt
22c35 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
22c36 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
22c37 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
22c38 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
22c39 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
22c3a 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20  te3_value_text, 
22c3b 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45  COLNAME_DECLTYPE
22c3c 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  );.}.#ifndef SQL
22c3d 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53  ITE_OMIT_UTF16.S
22c3e 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
22c3f 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
22c40 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28  lumn_decltype16(
22c41 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
22c42 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72  tmt, int N){.  r
22c43 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65  eturn columnName
22c44 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e  (.      pStmt, N
22c45 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a  , (const void*(*
22c46 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f  )(Mem*))sqlite3_
22c47 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f  value_text16, CO
22c48 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b  LNAME_DECLTYPE);
22c49 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
22c4a 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
22c4b 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  /.#endif /* SQLI
22c4c 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45  TE_OMIT_DECLTYPE
22c4d 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
22c4e 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
22c4f 5f 4d 45 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20  _METADATA./*.** 
22c50 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20  Return the name 
22c51 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
22c52 66 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73  from which a res
22c53 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76  ult column deriv
22c54 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72  es..** NULL is r
22c55 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 72  eturned if the r
22c56 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20  esult column is 
22c57 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72  an expression or
22c58 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20   constant or.** 
22c59 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68  anything else wh
22c5a 69 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e  ich is not an un
22c5b 61 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e  abiguous referen
22c5c 63 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65  ce to a database
22c5d 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49   column..*/.SQLI
22c5e 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
22c5f 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r *sqlite3_colum
22c60 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 28  n_database_name(
22c61 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
22c62 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72  tmt, int N){.  r
22c63 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65  eturn columnName
22c64 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e  (.      pStmt, N
22c65 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a  , (const void*(*
22c66 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f  )(Mem*))sqlite3_
22c67 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e  value_text, COLN
22c68 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d  AME_DATABASE);.}
22c69 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22c6a 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54  OMIT_UTF16.SQLIT
22c6b 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
22c6c 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
22c6d 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36  _database_name16
22c6e 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
22c6f 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  Stmt, int N){.  
22c70 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d  return columnNam
22c71 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20  e(.      pStmt, 
22c72 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28  N, (const void*(
22c73 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33  *)(Mem*))sqlite3
22c74 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43  _value_text16, C
22c75 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29  OLNAME_DATABASE)
22c76 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
22c77 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
22c78 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  */../*.** Return
22c79 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
22c7a 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63   table from whic
22c7b 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  h a result colum
22c7c 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55  n derives..** NU
22c7d 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  LL is returned i
22c7e 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c  f the result col
22c7f 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73  umn is an expres
22c80 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74  sion or constant
22c81 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20   or.** anything 
22c82 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f  else which is no
22c83 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20  t an unabiguous 
22c84 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64  reference to a d
22c85 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a  atabase column..
22c86 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
22c87 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
22c88 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  3_column_table_n
22c89 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
22c8a 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
22c8b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
22c8c 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
22c8d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
22c8e 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
22c8f 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20  te3_value_text, 
22c90 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a  COLNAME_TABLE);.
22c91 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
22c92 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49  _OMIT_UTF16.SQLI
22c93 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
22c94 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
22c95 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73  n_table_name16(s
22c96 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
22c97 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
22c98 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
22c99 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
22c9a 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
22c9b 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
22c9c 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c  alue_text16, COL
22c9d 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23  NAME_TABLE);.}.#
22c9e 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
22c9f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f  OMIT_UTF16 */../
22ca0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
22ca1 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
22ca2 65 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68  e column from wh
22ca3 69 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c  ich a result col
22ca4 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20  umn derives..** 
22ca5 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
22ca6 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 63   if the result c
22ca7 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72  olumn is an expr
22ca8 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61  ession or consta
22ca9 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e  nt or.** anythin
22caa 67 20 65 6c 73 65 20 77 68 69 63 68 20 69 73 20  g else which is 
22cab 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75  not an unabiguou
22cac 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61  s reference to a
22cad 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e   database column
22cae 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
22caf 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
22cb0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  te3_column_origi
22cb1 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73  n_name(sqlite3_s
22cb2 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
22cb3 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c  N){.  return col
22cb4 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70  umnName(.      p
22cb5 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20  Stmt, N, (const 
22cb6 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73  void*(*)(Mem*))s
22cb7 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
22cb8 74 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d  t, COLNAME_COLUM
22cb9 4e 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51  N);.}.#ifndef SQ
22cba 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
22cbb 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
22cbc 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63   void *sqlite3_c
22cbd 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
22cbe 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  e16(sqlite3_stmt
22cbf 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
22cc0 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
22cc1 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
22cc2 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
22cc3 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
22cc4 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
22cc5 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e  , COLNAME_COLUMN
22cc6 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
22cc7 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
22cc8 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51   */.#endif /* SQ
22cc9 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55  LITE_ENABLE_COLU
22cca 4d 4e 5f 4d 45 54 41 44 41 54 41 20 2a 2f 0a 0a  MN_METADATA */..
22ccb 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
22ccc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ccd 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 20  * sqlite3_bind_ 
22cce 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
22ccf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
22cd0 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 75 73 65  .** Routines use
22cd1 64 20 74 6f 20 61 74 74 61 63 68 20 76 61 6c 75  d to attach valu
22cd2 65 73 20 74 6f 20 77 69 6c 64 63 61 72 64 73 20  es to wildcards 
22cd3 69 6e 20 61 20 63 6f 6d 70 69 6c 65 64 20 53 51  in a compiled SQ
22cd4 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a  L statement..*/.
22cd5 2f 2a 0a 2a 2a 20 55 6e 62 69 6e 64 20 74 68 65  /*.** Unbind the
22cd6 20 76 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20   value bound to 
22cd7 76 61 72 69 61 62 6c 65 20 69 20 69 6e 20 76 69  variable i in vi
22cd8 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 2e  rtual machine p.
22cd9 20 54 68 69 73 20 69 73 20 74 68 65 20 0a 2a 2a   This is the .**
22cda 20 74 68 65 20 73 61 6d 65 20 61 73 20 62 69 6e   the same as bin
22cdb 64 69 6e 67 20 61 20 4e 55 4c 4c 20 76 61 6c 75  ding a NULL valu
22cdc 65 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 2e  e to the column.
22cdd 20 49 66 20 74 68 65 20 22 69 22 20 70 61 72 61   If the "i" para
22cde 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6f 75 74 20  meter is.** out 
22cdf 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 53  of range, then S
22ce0 51 4c 49 54 45 5f 52 41 4e 47 45 20 69 73 20 72  QLITE_RANGE is r
22ce1 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 77 69 73  eturned. Othewis
22ce2 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a  e SQLITE_OK..**.
22ce3 2a 2a 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  ** A successful 
22ce4 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68  evaluation of th
22ce5 69 73 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69  is routine acqui
22ce6 72 65 73 20 74 68 65 20 6d 75 74 65 78 20 6f 6e  res the mutex on
22ce7 20 70 2e 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78   p..** the mutex
22ce8 20 69 73 20 72 65 6c 65 61 73 65 64 20 69 66 20   is released if 
22ce9 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f  any kind of erro
22cea 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20  r occurs..**.** 
22ceb 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73  The error code s
22cec 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61 73  tored in databas
22ced 65 20 70 2d 3e 64 62 20 69 73 20 6f 76 65 72 77  e p->db is overw
22cee 72 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 20  ritten with the 
22cef 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20  return.** value 
22cf0 69 6e 20 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a  in any case..*/.
22cf1 73 74 61 74 69 63 20 69 6e 74 20 76 64 62 65 55  static int vdbeU
22cf2 6e 62 69 6e 64 28 56 64 62 65 20 2a 70 2c 20 69  nbind(Vdbe *p, i
22cf3 6e 74 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56  nt i){.  Mem *pV
22cf4 61 72 3b 0a 20 20 69 66 28 20 76 64 62 65 53 61  ar;.  if( vdbeSa
22cf5 66 65 74 79 4e 6f 74 4e 75 6c 6c 28 70 29 20 29  fetyNotNull(p) )
22cf6 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
22cf7 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b 50 54 3b  ITE_MISUSE_BKPT;
22cf8 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
22cf9 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62  utex_enter(p->db
22cfa 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  ->mutex);.  if( 
22cfb 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d  p->magic!=VDBE_M
22cfc 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 70  AGIC_RUN || p->p
22cfd 63 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  c>=0 ){.    sqli
22cfe 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20  te3Error(p->db, 
22cff 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30  SQLITE_MISUSE, 0
22d00 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
22d01 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62  utex_leave(p->db
22d02 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71  ->mutex);.    sq
22d03 6c 69 74 65 33 5f 6c 6f 67 28 53 51 4c 49 54 45  lite3_log(SQLITE
22d04 5f 4d 49 53 55 53 45 2c 20 0a 20 20 20 20 20 20  _MISUSE, .      
22d05 20 20 22 62 69 6e 64 20 6f 6e 20 61 20 62 75 73    "bind on a bus
22d06 79 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  y prepared state
22d07 6d 65 6e 74 3a 20 5b 25 73 5d 22 2c 20 70 2d 3e  ment: [%s]", p->
22d08 7a 53 71 6c 29 3b 0a 20 20 20 20 72 65 74 75 72  zSql);.    retur
22d09 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f  n SQLITE_MISUSE_
22d0a 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 69 66 28 20  BKPT;.  }.  if( 
22d0b 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72  i<1 || i>p->nVar
22d0c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
22d0d 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49  rror(p->db, SQLI
22d0e 54 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20  TE_RANGE, 0);.  
22d0f 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
22d10 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
22d11 65 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ex);.    return 
22d12 53 51 4c 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20  SQLITE_RANGE;.  
22d13 7d 0a 20 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20  }.  i--;.  pVar 
22d14 3d 20 26 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20  = &p->aVar[i];. 
22d15 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
22d16 65 6c 65 61 73 65 28 70 56 61 72 29 3b 0a 20 20  elease(pVar);.  
22d17 70 56 61 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pVar->flags = ME
22d18 4d 5f 4e 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65  M_Null;.  sqlite
22d19 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51  3Error(p->db, SQ
22d1a 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 0a 20 20  LITE_OK, 0);..  
22d1b 2f 2a 20 49 66 20 74 68 65 20 62 69 74 20 63 6f  /* If the bit co
22d1c 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74  rresponding to t
22d1d 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 6e 20  his variable in 
22d1e 56 64 62 65 2e 65 78 70 6d 61 73 6b 20 69 73 20  Vdbe.expmask is 
22d1f 73 65 74 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20  set, then .  ** 
22d20 62 69 6e 64 69 6e 67 20 61 20 6e 65 77 20 76 61  binding a new va
22d21 6c 75 65 20 74 6f 20 74 68 69 73 20 76 61 72 69  lue to this vari
22d22 61 62 6c 65 20 69 6e 76 61 6c 69 64 61 74 65 73  able invalidates
22d23 20 74 68 65 20 63 75 72 72 65 6e 74 20 71 75 65   the current que
22d24 72 79 20 70 6c 61 6e 2e 0a 20 20 2a 2a 0a 20 20  ry plan..  **.  
22d25 2a 2a 20 49 4d 50 4c 45 4d 45 4e 54 41 54 49 4f  ** IMPLEMENTATIO
22d26 4e 2d 4f 46 3a 20 52 2d 34 38 34 34 30 2d 33 37  N-OF: R-48440-37
22d27 35 39 35 20 49 66 20 74 68 65 20 73 70 65 63 69  595 If the speci
22d28 66 69 63 20 76 61 6c 75 65 20 62 6f 75 6e 64 20  fic value bound 
22d29 74 6f 20 68 6f 73 74 0a 20 20 2a 2a 20 70 61 72  to host.  ** par
22d2a 61 6d 65 74 65 72 20 69 6e 20 74 68 65 20 57 48  ameter in the WH
22d2b 45 52 45 20 63 6c 61 75 73 65 20 6d 69 67 68 74  ERE clause might
22d2c 20 69 6e 66 6c 75 65 6e 63 65 20 74 68 65 20 63   influence the c
22d2d 68 6f 69 63 65 20 6f 66 20 71 75 65 72 79 20 70  hoice of query p
22d2e 6c 61 6e 0a 20 20 2a 2a 20 66 6f 72 20 61 20 73  lan.  ** for a s
22d2f 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74  tatement, then t
22d30 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c  he statement wil
22d31 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  l be automatical
22d32 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 2c 0a 20  ly recompiled,. 
22d33 20 2a 2a 20 61 73 20 69 66 20 74 68 65 72 65 20   ** as if there 
22d34 68 61 64 20 62 65 65 6e 20 61 20 73 63 68 65 6d  had been a schem
22d35 61 20 63 68 61 6e 67 65 2c 20 6f 6e 20 74 68 65  a change, on the
22d36 20 66 69 72 73 74 20 73 71 6c 69 74 65 33 5f 73   first sqlite3_s
22d37 74 65 70 28 29 20 63 61 6c 6c 0a 20 20 2a 2a 20  tep() call.  ** 
22d38 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 20 63 68  following any ch
22d39 61 6e 67 65 20 74 6f 20 74 68 65 20 62 69 6e 64  ange to the bind
22d3a 69 6e 67 73 20 6f 66 20 74 68 61 74 20 70 61 72  ings of that par
22d3b 61 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 69  ameter..  */.  i
22d3c 66 28 20 70 2d 3e 69 73 50 72 65 70 61 72 65 56  f( p->isPrepareV
22d3d 32 20 26 26 0a 20 20 20 20 20 28 28 69 3c 33 32  2 &&.     ((i<32
22d3e 20 26 26 20 70 2d 3e 65 78 70 6d 61 73 6b 20 26   && p->expmask &
22d3f 20 28 28 75 33 32 29 31 20 3c 3c 20 69 29 29 20   ((u32)1 << i)) 
22d40 7c 7c 20 70 2d 3e 65 78 70 6d 61 73 6b 3d 3d 30  || p->expmask==0
22d41 78 66 66 66 66 66 66 66 66 29 0a 20 20 29 7b 0a  xffffffff).  ){.
22d42 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d      p->expired =
22d43 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   1;.  }.  return
22d44 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
22d45 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 74 65 78 74  *.** Bind a text
22d46 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 65 2e 0a   or BLOB value..
22d47 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 69  */.static int bi
22d48 6e 64 54 65 78 74 28 0a 20 20 73 71 6c 69 74 65  ndText(.  sqlite
22d49 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 20  3_stmt *pStmt,  
22d4a 20 2f 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e   /* The statemen
22d4b 74 20 74 6f 20 62 69 6e 64 20 61 67 61 69 6e 73  t to bind agains
22d4c 74 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20  t */.  int i,   
22d4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22d4e 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61   Index of the pa
22d4f 72 61 6d 65 74 65 72 20 74 6f 20 62 69 6e 64 20  rameter to bind 
22d50 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
22d51 2a 7a 44 61 74 61 2c 20 20 20 20 20 2f 2a 20 50  *zData,     /* P
22d52 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 64 61  ointer to the da
22d53 74 61 20 74 6f 20 62 65 20 62 6f 75 6e 64 20 2a  ta to be bound *
22d54 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 20  /.  int nData,  
22d55 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
22d56 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
22d57 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e   data to be boun
22d58 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44  d */.  void (*xD
22d59 65 6c 29 28 76 6f 69 64 2a 29 2c 20 20 20 2f 2a  el)(void*),   /*
22d5a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20   Destructor for 
22d5b 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 38  the data */.  u8
22d5c 20 65 6e 63 6f 64 69 6e 67 20 20 20 20 20 20 20   encoding       
22d5d 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67       /* Encoding
22d5e 20 66 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f   for the data */
22d5f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  .){.  Vdbe *p = 
22d60 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20  (Vdbe *)pStmt;. 
22d61 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 6e   Mem *pVar;.  in
22d62 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 76 64  t rc;..  rc = vd
22d63 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a  beUnbind(p, i);.
22d64 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
22d65 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 7a  _OK ){.    if( z
22d66 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20  Data!=0 ){.     
22d67 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72   pVar = &p->aVar
22d68 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20 72 63 20  [i-1];.      rc 
22d69 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
22d6a 53 65 74 53 74 72 28 70 56 61 72 2c 20 7a 44 61  SetStr(pVar, zDa
22d6b 74 61 2c 20 6e 44 61 74 61 2c 20 65 6e 63 6f 64  ta, nData, encod
22d6c 69 6e 67 2c 20 78 44 65 6c 29 3b 0a 20 20 20 20  ing, xDel);.    
22d6d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
22d6e 5f 4f 4b 20 26 26 20 65 6e 63 6f 64 69 6e 67 21  _OK && encoding!
22d6f 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  =0 ){.        rc
22d70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68   = sqlite3VdbeCh
22d71 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61  angeEncoding(pVa
22d72 72 2c 20 45 4e 43 28 70 2d 3e 64 62 29 29 3b 0a  r, ENC(p->db));.
22d73 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
22d74 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62  lite3Error(p->db
22d75 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20  , rc, 0);.      
22d76 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
22d77 78 69 74 28 70 2d 3e 64 62 2c 20 72 63 29 3b 0a  xit(p->db, rc);.
22d78 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
22d79 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
22d7a 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  >db->mutex);.  }
22d7b 65 6c 73 65 20 69 66 28 20 78 44 65 6c 21 3d 53  else if( xDel!=S
22d7c 51 4c 49 54 45 5f 53 54 41 54 49 43 20 26 26 20  QLITE_STATIC && 
22d7d 78 44 65 6c 21 3d 53 51 4c 49 54 45 5f 54 52 41  xDel!=SQLITE_TRA
22d7e 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20 78 44  NSIENT ){.    xD
22d7f 65 6c 28 28 76 6f 69 64 2a 29 7a 44 61 74 61 29  el((void*)zData)
22d80 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
22d81 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e  c;.}.../*.** Bin
22d82 64 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 20 74  d a blob value t
22d83 6f 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  o an SQL stateme
22d84 6e 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a  nt variable..*/.
22d85 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
22d86 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
22d87 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  (.  sqlite3_stmt
22d88 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20   *pStmt, .  int 
22d89 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  i, .  const void
22d8a 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20   *zData, .  int 
22d8b 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28  nData, .  void (
22d8c 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b  *xDel)(void*).){
22d8d 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65  .  return bindTe
22d8e 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61  xt(pStmt, i, zDa
22d8f 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c  ta, nData, xDel,
22d90 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50   0);.}.SQLITE_AP
22d91 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
22d92 6e 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65  nd_double(sqlite
22d93 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
22d94 6e 74 20 69 2c 20 64 6f 75 62 6c 65 20 72 56 61  nt i, double rVa
22d95 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  lue){.  int rc;.
22d96 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62    Vdbe *p = (Vdb
22d97 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20  e *)pStmt;.  rc 
22d98 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20  = vdbeUnbind(p, 
22d99 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  i);.  if( rc==SQ
22d9a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
22d9b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
22d9c 44 6f 75 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b  Double(&p->aVar[
22d9d 69 2d 31 5d 2c 20 72 56 61 6c 75 65 29 3b 0a 20  i-1], rValue);. 
22d9e 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
22d9f 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75  _leave(p->db->mu
22da0 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tex);.  }.  retu
22da1 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
22da2 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
22da3 62 69 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33  bind_int(sqlite3
22da4 5f 73 74 6d 74 20 2a 70 2c 20 69 6e 74 20 69 2c  _stmt *p, int i,
22da5 20 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20   int iValue){.  
22da6 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62  return sqlite3_b
22da7 69 6e 64 5f 69 6e 74 36 34 28 70 2c 20 69 2c 20  ind_int64(p, i, 
22da8 28 69 36 34 29 69 56 61 6c 75 65 29 3b 0a 7d 0a  (i64)iValue);.}.
22da9 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
22daa 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
22dab 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  4(sqlite3_stmt *
22dac 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71  pStmt, int i, sq
22dad 6c 69 74 65 5f 69 6e 74 36 34 20 69 56 61 6c 75  lite_int64 iValu
22dae 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  e){.  int rc;.  
22daf 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20  Vdbe *p = (Vdbe 
22db0 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20  *)pStmt;.  rc = 
22db1 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29  vdbeUnbind(p, i)
22db2 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
22db3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
22db4 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e  ite3VdbeMemSetIn
22db5 74 36 34 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31  t64(&p->aVar[i-1
22db6 5d 2c 20 69 56 61 6c 75 65 29 3b 0a 20 20 20 20  ], iValue);.    
22db7 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
22db8 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ave(p->db->mutex
22db9 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
22dba 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  rc;.}.SQLITE_API
22dbb 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
22dbc 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73  d_null(sqlite3_s
22dbd 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
22dbe 69 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  i){.  int rc;.  
22dbf 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a  Vdbe *p = (Vdbe*
22dc0 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76  )pStmt;.  rc = v
22dc1 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b  dbeUnbind(p, i);
22dc2 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
22dc3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
22dc4 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
22dc5 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  p->db->mutex);. 
22dc6 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
22dc7 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
22dc8 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
22dc9 78 74 28 20 0a 20 20 73 71 6c 69 74 65 33 5f 73  xt( .  sqlite3_s
22dca 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69  tmt *pStmt, .  i
22dcb 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63  nt i, .  const c
22dcc 68 61 72 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69  har *zData, .  i
22dcd 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69  nt nData, .  voi
22dce 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  d (*xDel)(void*)
22dcf 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e  .){.  return bin
22dd0 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20  dText(pStmt, i, 
22dd1 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44  zData, nData, xD
22dd2 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29  el, SQLITE_UTF8)
22dd3 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
22dd4 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51  TE_OMIT_UTF16.SQ
22dd5 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
22dd6 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
22dd7 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  (.  sqlite3_stmt
22dd8 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20   *pStmt, .  int 
22dd9 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  i, .  const void
22dda 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20   *zData, .  int 
22ddb 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28  nData, .  void (
22ddc 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b  *xDel)(void*).){
22ddd 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65  .  return bindTe
22dde 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61  xt(pStmt, i, zDa
22ddf 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c  ta, nData, xDel,
22de0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
22de1 49 56 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  IVE);.}.#endif /
22de2 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
22de3 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  F16 */.SQLITE_AP
22de4 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
22de5 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33  nd_value(sqlite3
22de6 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
22de7 74 20 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74  t i, const sqlit
22de8 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65  e3_value *pValue
22de9 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
22dea 77 69 74 63 68 28 20 70 56 61 6c 75 65 2d 3e 74  witch( pValue->t
22deb 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ype ){.    case 
22dec 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20  SQLITE_INTEGER: 
22ded 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
22dee 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
22def 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65  pStmt, i, pValue
22df0 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 62 72  ->u.i);.      br
22df1 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
22df2 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  ase SQLITE_FLOAT
22df3 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  : {.      rc = s
22df4 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62  qlite3_bind_doub
22df5 6c 65 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61  le(pStmt, i, pVa
22df6 6c 75 65 2d 3e 72 29 3b 0a 20 20 20 20 20 20 62  lue->r);.      b
22df7 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
22df8 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42  case SQLITE_BLOB
22df9 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 56  : {.      if( pV
22dfa 61 6c 75 65 2d 3e 66 6c 61 67 73 20 26 20 4d 45  alue->flags & ME
22dfb 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
22dfc 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
22dfd 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 70 53 74  ind_zeroblob(pSt
22dfe 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e 75  mt, i, pValue->u
22dff 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7d  .nZero);.      }
22e00 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63  else{.        rc
22e01 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
22e02 62 6c 6f 62 28 70 53 74 6d 74 2c 20 69 2c 20 70  blob(pStmt, i, p
22e03 56 61 6c 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65  Value->z, pValue
22e04 2d 3e 6e 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53  ->n,SQLITE_TRANS
22e05 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 7d 0a 20  IENT);.      }. 
22e06 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
22e07 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
22e08 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20  E_TEXT: {.      
22e09 72 63 20 3d 20 62 69 6e 64 54 65 78 74 28 70 53  rc = bindText(pS
22e0a 74 6d 74 2c 69 2c 20 20 70 56 61 6c 75 65 2d 3e  tmt,i,  pValue->
22e0b 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c 20 53 51  z, pValue->n, SQ
22e0c 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c 0a  LITE_TRANSIENT,.
22e0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22e0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 56                pV
22e0f 61 6c 75 65 2d 3e 65 6e 63 29 3b 0a 20 20 20 20  alue->enc);.    
22e10 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
22e11 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
22e12 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
22e13 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 53 74 6d 74  _bind_null(pStmt
22e14 2c 20 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61  , i);.      brea
22e15 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  k;.    }.  }.  r
22e16 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49  eturn rc;.}.SQLI
22e17 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22e18 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62  e3_bind_zeroblob
22e19 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
22e1a 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74  Stmt, int i, int
22e1b 20 6e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20   n){.  int rc;. 
22e1c 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
22e1d 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d   *)pStmt;.  rc =
22e1e 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69   vdbeUnbind(p, i
22e1f 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
22e20 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
22e21 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a  lite3VdbeMemSetZ
22e22 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 56 61 72  eroBlob(&p->aVar
22e23 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20 20 20 73  [i-1], n);.    s
22e24 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
22e25 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
22e26 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
22e27 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
22e28 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
22e29 20 77 69 6c 64 63 61 72 64 73 20 74 68 61 74 20   wildcards that 
22e2a 63 61 6e 20 62 65 20 70 6f 74 65 6e 74 69 61 6c  can be potential
22e2b 6c 79 20 62 6f 75 6e 64 20 74 6f 2e 0a 2a 2a 20  ly bound to..** 
22e2c 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
22e2d 61 64 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74  added to support
22e2e 20 44 42 44 3a 3a 53 51 4c 69 74 65 2e 20 20 0a   DBD::SQLite.  .
22e2f 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
22e30 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70  t sqlite3_bind_p
22e31 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73  arameter_count(s
22e32 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
22e33 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d  mt){.  Vdbe *p =
22e34 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20   (Vdbe*)pStmt;. 
22e35 20 72 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e   return p ? p->n
22e36 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  Var : 0;.}../*.*
22e37 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d  * Return the nam
22e38 65 20 6f 66 20 61 20 77 69 6c 64 63 61 72 64 20  e of a wildcard 
22e39 70 61 72 61 6d 65 74 65 72 2e 20 20 52 65 74 75  parameter.  Retu
22e3a 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 69  rn NULL if the i
22e3b 6e 64 65 78 0a 2a 2a 20 69 73 20 6f 75 74 20 6f  ndex.** is out o
22e3c 66 20 72 61 6e 67 65 20 6f 72 20 69 66 20 74 68  f range or if th
22e3d 65 20 77 69 6c 64 63 61 72 64 20 69 73 20 75 6e  e wildcard is un
22e3e 6e 61 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  named..**.** The
22e3f 20 72 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79   result is alway
22e40 73 20 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49  s UTF-8..*/.SQLI
22e41 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
22e42 72 20 2a 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  r *sqlite3_bind_
22e43 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 73  parameter_name(s
22e44 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
22e45 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 56 64  mt, int i){.  Vd
22e46 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70  be *p = (Vdbe*)p
22e47 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30  Stmt;.  if( p==0
22e48 20 7c 7c 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e   || i<1 || i>p->
22e49 6e 7a 56 61 72 20 29 7b 0a 20 20 20 20 72 65 74  nzVar ){.    ret
22e4a 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 0;.  }.  ret
22e4b 75 72 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d 31  urn p->azVar[i-1
22e4c 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  ];.}../*.** Give
22e4d 6e 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72  n a wildcard par
22e4e 61 6d 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 74  ameter name, ret
22e4f 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66  urn the index of
22e50 20 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a   the variable.**
22e51 20 77 69 74 68 20 74 68 61 74 20 6e 61 6d 65 2e   with that name.
22e52 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f    If there is no
22e53 20 76 61 72 69 61 62 6c 65 20 77 69 74 68 20 74   variable with t
22e54 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a 2a  he given name,.*
22e55 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53  * return 0..*/.S
22e56 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
22e57 74 20 73 71 6c 69 74 65 33 56 64 62 65 50 61 72  t sqlite3VdbePar
22e58 61 6d 65 74 65 72 49 6e 64 65 78 28 56 64 62 65  ameterIndex(Vdbe
22e59 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *p, const char 
22e5a 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d  *zName, int nNam
22e5b 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  e){.  int i;.  i
22e5c 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72  f( p==0 ){.    r
22e5d 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69  eturn 0;.  }.  i
22e5e 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20  f( zName ){.    
22e5f 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 7a  for(i=0; i<p->nz
22e60 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Var; i++){.     
22e61 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d   const char *z =
22e62 20 70 2d 3e 61 7a 56 61 72 5b 69 5d 3b 0a 20 20   p->azVar[i];.  
22e63 20 20 20 20 69 66 28 20 7a 20 26 26 20 6d 65 6d      if( z && mem
22e64 63 6d 70 28 7a 2c 7a 4e 61 6d 65 2c 6e 4e 61 6d  cmp(z,zName,nNam
22e65 65 29 3d 3d 30 20 26 26 20 7a 5b 6e 4e 61 6d 65  e)==0 && z[nName
22e66 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ]==0 ){.        
22e67 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20  return i+1;.    
22e68 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
22e69 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49  return 0;.}.SQLI
22e6a 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22e6b 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
22e6c 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 33 5f  r_index(sqlite3_
22e6d 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e  stmt *pStmt, con
22e6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b  st char *zName){
22e6f 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
22e70 33 56 64 62 65 50 61 72 61 6d 65 74 65 72 49 6e  3VdbeParameterIn
22e71 64 65 78 28 28 56 64 62 65 2a 29 70 53 74 6d 74  dex((Vdbe*)pStmt
22e72 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33  , zName, sqlite3
22e73 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 29  Strlen30(zName))
22e74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73  ;.}../*.** Trans
22e75 66 65 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 73  fer all bindings
22e76 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20   from the first 
22e77 73 74 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 74  statement over t
22e78 6f 20 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a 2f  o the second..*/
22e79 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22e7a 69 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73  int sqlite3Trans
22e7b 66 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69  ferBindings(sqli
22e7c 74 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53  te3_stmt *pFromS
22e7d 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d  tmt, sqlite3_stm
22e7e 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56  t *pToStmt){.  V
22e7f 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64  dbe *pFrom = (Vd
22e80 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20  be*)pFromStmt;. 
22e81 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64   Vdbe *pTo = (Vd
22e82 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69  be*)pToStmt;.  i
22e83 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20  nt i;.  assert( 
22e84 70 54 6f 2d 3e 64 62 3d 3d 70 46 72 6f 6d 2d 3e  pTo->db==pFrom->
22e85 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  db );.  assert( 
22e86 70 54 6f 2d 3e 6e 56 61 72 3d 3d 70 46 72 6f 6d  pTo->nVar==pFrom
22e87 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 73 71 6c 69  ->nVar );.  sqli
22e88 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
22e89 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  pTo->db->mutex);
22e8a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46  .  for(i=0; i<pF
22e8b 72 6f 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b  rom->nVar; i++){
22e8c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
22e8d 4d 65 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56  MemMove(&pTo->aV
22e8e 61 72 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61  ar[i], &pFrom->a
22e8f 56 61 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73  Var[i]);.  }.  s
22e90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
22e91 76 65 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65  ve(pTo->db->mute
22e92 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  x);.  return SQL
22e93 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64  ITE_OK;.}..#ifnd
22e94 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
22e95 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20  EPRECATED./*.** 
22e96 44 65 70 72 65 63 61 74 65 64 20 65 78 74 65 72  Deprecated exter
22e97 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20  nal interface.  
22e98 49 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51  Internal/core SQ
22e99 4c 69 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f  Lite code.** sho
22e9a 75 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  uld call sqlite3
22e9b 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73  TransferBindings
22e9c 2e 0a 2a 2a 0a 2a 2a 20 49 73 20 69 73 20 6d 69  ..**.** Is is mi
22e9d 73 75 73 65 20 74 6f 20 63 61 6c 6c 20 74 68 69  suse to call thi
22e9e 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 73  s routine with s
22e9f 74 61 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 64  tatements from d
22ea0 69 66 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61  ifferent.** data
22ea1 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
22ea2 2e 20 20 42 75 74 20 61 73 20 74 68 69 73 20 69  .  But as this i
22ea3 73 20 61 20 64 65 70 72 65 63 61 74 65 64 20 69  s a deprecated i
22ea4 6e 74 65 72 66 61 63 65 2c 20 77 65 0a 2a 2a 20  nterface, we.** 
22ea5 77 69 6c 6c 20 6e 6f 74 20 62 6f 74 68 65 72 20  will not bother 
22ea6 74 6f 20 63 68 65 63 6b 20 66 6f 72 20 74 68 61  to check for tha
22ea7 74 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 0a  t condition..**.
22ea8 2a 2a 20 49 66 20 74 68 65 20 74 77 6f 20 73 74  ** If the two st
22ea9 61 74 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e  atements contain
22eaa 20 61 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d   a different num
22eab 62 65 72 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c  ber of bindings,
22eac 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49   then.** an SQLI
22ead 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75  TE_ERROR is retu
22eae 72 6e 65 64 2e 20 20 4e 6f 74 68 69 6e 67 20 65  rned.  Nothing e
22eaf 6c 73 65 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67  lse can go wrong
22eb0 2c 20 73 6f 20 6f 74 68 65 72 77 69 73 65 0a 2a  , so otherwise.*
22eb1 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
22eb2 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
22eb3 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22eb4 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64  e3_transfer_bind
22eb5 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ings(sqlite3_stm
22eb6 74 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71  t *pFromStmt, sq
22eb7 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53  lite3_stmt *pToS
22eb8 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46  tmt){.  Vdbe *pF
22eb9 72 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72  rom = (Vdbe*)pFr
22eba 6f 6d 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a  omStmt;.  Vdbe *
22ebb 70 54 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f  pTo = (Vdbe*)pTo
22ebc 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 46 72 6f  Stmt;.  if( pFro
22ebd 6d 2d 3e 6e 56 61 72 21 3d 70 54 6f 2d 3e 6e 56  m->nVar!=pTo->nV
22ebe 61 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ar ){.    return
22ebf 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
22ec0 20 7d 0a 20 20 69 66 28 20 70 54 6f 2d 3e 69 73   }.  if( pTo->is
22ec1 50 72 65 70 61 72 65 56 32 20 26 26 20 70 54 6f  PrepareV2 && pTo
22ec2 2d 3e 65 78 70 6d 61 73 6b 20 29 7b 0a 20 20 20  ->expmask ){.   
22ec3 20 70 54 6f 2d 3e 65 78 70 69 72 65 64 20 3d 20   pTo->expired = 
22ec4 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 72  1;.  }.  if( pFr
22ec5 6f 6d 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20  om->isPrepareV2 
22ec6 26 26 20 70 46 72 6f 6d 2d 3e 65 78 70 6d 61 73  && pFrom->expmas
22ec7 6b 20 29 7b 0a 20 20 20 20 70 46 72 6f 6d 2d 3e  k ){.    pFrom->
22ec8 65 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d  expired = 1;.  }
22ec9 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
22eca 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67  3TransferBinding
22ecb 73 28 70 46 72 6f 6d 53 74 6d 74 2c 20 70 54 6f  s(pFromStmt, pTo
22ecc 53 74 6d 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  Stmt);.}.#endif.
22ecd 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
22ece 65 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62  e sqlite3* datab
22ecf 61 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 77 68  ase handle to wh
22ed0 69 63 68 20 74 68 65 20 70 72 65 70 61 72 65 64  ich the prepared
22ed1 20 73 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e   statement given
22ed2 0a 2a 2a 20 69 6e 20 74 68 65 20 61 72 67 75 6d  .** in the argum
22ed3 65 6e 74 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68  ent belongs.  Th
22ed4 69 73 20 69 73 20 74 68 65 20 73 61 6d 65 20 64  is is the same d
22ed5 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74  atabase handle t
22ed6 68 61 74 20 77 61 73 0a 2a 2a 20 74 68 65 20 66  hat was.** the f
22ed7 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
22ed8 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65   the sqlite3_pre
22ed9 70 61 72 65 28 29 20 74 68 61 74 20 77 61 73 20  pare() that was 
22eda 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 0a 2a  used to create.*
22edb 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * the statement 
22edc 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61  in the first pla
22edd 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ce..*/.SQLITE_AP
22ede 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74  I sqlite3 *sqlit
22edf 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c  e3_db_handle(sql
22ee0 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
22ee1 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 53 74 6d  ){.  return pStm
22ee2 74 20 3f 20 28 28 56 64 62 65 2a 29 70 53 74 6d  t ? ((Vdbe*)pStm
22ee3 74 29 2d 3e 64 62 20 3a 20 30 3b 0a 7d 0a 0a 2f  t)->db : 0;.}../
22ee4 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
22ee5 20 69 66 20 74 68 65 20 70 72 65 70 61 72 65 64   if the prepared
22ee6 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 67 75   statement is gu
22ee7 61 72 61 6e 74 65 65 64 20 74 6f 20 6e 6f 74 20  aranteed to not 
22ee8 6d 6f 64 69 66 79 20 74 68 65 0a 2a 2a 20 64 61  modify the.** da
22ee9 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  tabase..*/.SQLIT
22eea 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
22eeb 33 5f 73 74 6d 74 5f 72 65 61 64 6f 6e 6c 79 28  3_stmt_readonly(
22eec 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
22eed 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  tmt){.  return p
22eee 53 74 6d 74 20 3f 20 28 28 56 64 62 65 2a 29 70  Stmt ? ((Vdbe*)p
22eef 53 74 6d 74 29 2d 3e 72 65 61 64 4f 6e 6c 79 20  Stmt)->readOnly 
22ef0 3a 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  : 1;.}../*.** Re
22ef1 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
22ef2 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
22ef3 65 6e 74 20 69 73 20 69 6e 20 6e 65 65 64 20 6f  ent is in need o
22ef4 66 20 62 65 69 6e 67 20 72 65 73 65 74 2e 0a 2a  f being reset..*
22ef5 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
22ef6 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 62 75   sqlite3_stmt_bu
22ef7 73 79 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  sy(sqlite3_stmt 
22ef8 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20  *pStmt){.  Vdbe 
22ef9 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  *v = (Vdbe*)pStm
22efa 74 3b 0a 20 20 72 65 74 75 72 6e 20 76 21 3d 30  t;.  return v!=0
22efb 20 26 26 20 76 2d 3e 70 63 3e 30 20 26 26 20 76   && v->pc>0 && v
22efc 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
22efd 47 49 43 5f 52 55 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a  GIC_RUN;.}../*.*
22efe 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
22eff 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 70  er to the next p
22f00 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
22f01 74 20 61 66 74 65 72 20 70 53 74 6d 74 20 61 73  t after pStmt as
22f02 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68  sociated.** with
22f03 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
22f04 74 69 6f 6e 20 70 44 62 2e 20 20 49 66 20 70 53  tion pDb.  If pS
22f05 74 6d 74 20 69 73 20 4e 55 4c 4c 2c 20 72 65 74  tmt is NULL, ret
22f06 75 72 6e 20 74 68 65 20 66 69 72 73 74 0a 2a 2a  urn the first.**
22f07 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
22f08 65 6e 74 20 66 6f 72 20 74 68 65 20 64 61 74 61  ent for the data
22f09 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
22f0a 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66    Return NULL if
22f0b 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f   there.** are no
22f0c 20 6d 6f 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   more..*/.SQLITE
22f0d 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d  _API sqlite3_stm
22f0e 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f  t *sqlite3_next_
22f0f 73 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44  stmt(sqlite3 *pD
22f10 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  b, sqlite3_stmt 
22f11 2a 70 53 74 6d 74 29 7b 0a 20 20 73 71 6c 69 74  *pStmt){.  sqlit
22f12 65 33 5f 73 74 6d 74 20 2a 70 4e 65 78 74 3b 0a  e3_stmt *pNext;.
22f13 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
22f14 65 6e 74 65 72 28 70 44 62 2d 3e 6d 75 74 65 78  enter(pDb->mutex
22f15 29 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d  );.  if( pStmt==
22f16 30 20 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d  0 ){.    pNext =
22f17 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29   (sqlite3_stmt*)
22f18 70 44 62 2d 3e 70 56 64 62 65 3b 0a 20 20 7d 65  pDb->pVdbe;.  }e
22f19 6c 73 65 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d  lse{.    pNext =
22f1a 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29   (sqlite3_stmt*)
22f1b 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e  ((Vdbe*)pStmt)->
22f1c 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 73 71 6c  pNext;.  }.  sql
22f1d 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
22f1e 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  (pDb->mutex);.  
22f1f 72 65 74 75 72 6e 20 70 4e 65 78 74 3b 0a 7d 0a  return pNext;.}.
22f20 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
22f21 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61  e value of a sta
22f22 74 75 73 20 63 6f 75 6e 74 65 72 20 66 6f 72 20  tus counter for 
22f23 61 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  a prepared state
22f24 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ment.*/.SQLITE_A
22f25 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
22f26 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74  tmt_status(sqlit
22f27 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
22f28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 72 65 73 65  int op, int rese
22f29 74 46 6c 61 67 29 7b 0a 20 20 56 64 62 65 20 2a  tFlag){.  Vdbe *
22f2a 70 56 64 62 65 20 3d 20 28 56 64 62 65 2a 29 70  pVdbe = (Vdbe*)p
22f2b 53 74 6d 74 3b 0a 20 20 69 6e 74 20 76 20 3d 20  Stmt;.  int v = 
22f2c 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b  pVdbe->aCounter[
22f2d 6f 70 2d 31 5d 3b 0a 20 20 69 66 28 20 72 65 73  op-1];.  if( res
22f2e 65 74 46 6c 61 67 20 29 20 70 56 64 62 65 2d 3e  etFlag ) pVdbe->
22f2f 61 43 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d 20 3d  aCounter[op-1] =
22f30 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a   0;.  return v;.
22f31 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
22f32 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 61 70  ** End of vdbeap
22f33 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  i.c ************
22f34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22f35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22f36 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
22f37 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
22f38 62 65 74 72 61 63 65 2e 63 20 2a 2a 2a 2a 2a 2a  betrace.c ******
22f39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22f3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22f3b 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4e 6f  */./*.** 2009 No
22f3c 76 65 6d 62 65 72 20 32 35 0a 2a 2a 0a 2a 2a 20  vember 25.**.** 
22f3d 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
22f3e 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
22f3f 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
22f40 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
22f41 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
22f42 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
22f43 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
22f44 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
22f45 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
22f46 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
22f47 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
22f48 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
22f49 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
22f4a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
22f4b 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
22f4c 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
22f4d 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
22f4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22f4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22f51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22f52 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
22f53 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
22f54 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69  s code used to i
22f55 6e 73 65 72 74 20 74 68 65 20 76 61 6c 75 65 73  nsert the values
22f56 20 6f 66 20 68 6f 73 74 20 70 61 72 61 6d 65 74   of host paramet
22f57 65 72 73 0a 2a 2a 20 28 61 6b 61 20 22 77 69 6c  ers.** (aka "wil
22f58 64 63 61 72 64 73 22 29 20 69 6e 74 6f 20 74 68  dcards") into th
22f59 65 20 53 51 4c 20 74 65 78 74 20 6f 75 74 70 75  e SQL text outpu
22f5a 74 20 62 79 20 73 71 6c 69 74 65 33 5f 74 72 61  t by sqlite3_tra
22f5b 63 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ce()..**.** The 
22f5c 56 64 62 65 20 70 61 72 73 65 2d 74 72 65 65 20  Vdbe parse-tree 
22f5d 65 78 70 6c 61 69 6e 65 72 20 69 73 20 61 6c 73  explainer is als
22f5e 6f 20 66 6f 75 6e 64 20 68 65 72 65 2e 0a 2a 2f  o found here..*/
22f5f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
22f60 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 0a 2f 2a 0a  _OMIT_TRACE../*.
22f61 2a 2a 20 7a 53 71 6c 20 69 73 20 61 20 7a 65 72  ** zSql is a zer
22f62 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72  o-terminated str
22f63 69 6e 67 20 6f 66 20 55 54 46 2d 38 20 53 51 4c  ing of UTF-8 SQL
22f64 20 74 65 78 74 2e 20 20 52 65 74 75 72 6e 20 74   text.  Return t
22f65 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
22f66 62 79 74 65 73 20 69 6e 20 74 68 69 73 20 74 65  bytes in this te
22f67 78 74 20 75 70 20 74 6f 20 62 75 74 20 65 78 63  xt up to but exc
22f68 6c 75 64 69 6e 67 20 74 68 65 20 66 69 72 73 74  luding the first
22f69 20 63 68 61 72 61 63 74 65 72 20 69 6e 0a 2a 2a   character in.**
22f6a 20 61 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65   a host paramete
22f6b 72 2e 20 20 49 66 20 74 68 65 20 74 65 78 74 20  r.  If the text 
22f6c 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 68 6f 73 74  contains no host
22f6d 20 70 61 72 61 6d 65 74 65 72 73 2c 20 72 65 74   parameters, ret
22f6e 75 72 6e 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c  urn.** the total
22f6f 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
22f70 20 69 6e 20 74 68 65 20 74 65 78 74 2e 0a 2a 2f   in the text..*/
22f71 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e 64  .static int find
22f72 4e 65 78 74 48 6f 73 74 50 61 72 61 6d 65 74 65  NextHostParamete
22f73 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  r(const char *zS
22f74 71 6c 2c 20 69 6e 74 20 2a 70 6e 54 6f 6b 65 6e  ql, int *pnToken
22f75 29 7b 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 54 79  ){.  int tokenTy
22f76 70 65 3b 0a 20 20 69 6e 74 20 6e 54 6f 74 61 6c  pe;.  int nTotal
22f77 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a   = 0;.  int n;..
22f78 20 20 2a 70 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0a    *pnToken = 0;.
22f79 20 20 77 68 69 6c 65 28 20 7a 53 71 6c 5b 30 5d    while( zSql[0]
22f7a 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69   ){.    n = sqli
22f7b 74 65 33 47 65 74 54 6f 6b 65 6e 28 28 75 38 2a  te3GetToken((u8*
22f7c 29 7a 53 71 6c 2c 20 26 74 6f 6b 65 6e 54 79 70  )zSql, &tokenTyp
22f7d 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  e);.    assert( 
22f7e 6e 3e 30 20 26 26 20 74 6f 6b 65 6e 54 79 70 65  n>0 && tokenType
22f7f 21 3d 54 4b 5f 49 4c 4c 45 47 41 4c 20 29 3b 0a  !=TK_ILLEGAL );.
22f80 20 20 20 20 69 66 28 20 74 6f 6b 65 6e 54 79 70      if( tokenTyp
22f81 65 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45 20 29  e==TK_VARIABLE )
22f82 7b 0a 20 20 20 20 20 20 2a 70 6e 54 6f 6b 65 6e  {.      *pnToken
22f83 20 3d 20 6e 3b 0a 20 20 20 20 20 20 62 72 65 61   = n;.      brea
22f84 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 54 6f  k;.    }.    nTo
22f85 74 61 6c 20 2b 3d 20 6e 3b 0a 20 20 20 20 7a 53  tal += n;.    zS
22f86 71 6c 20 2b 3d 20 6e 3b 0a 20 20 7d 0a 20 20 72  ql += n;.  }.  r
22f87 65 74 75 72 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a  eturn nTotal;.}.
22f88 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
22f89 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70  tion returns a p
22f8a 6f 69 6e 74 65 72 20 74 6f 20 61 20 6e 75 6c 2d  ointer to a nul-
22f8b 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e  terminated strin
22f8c 67 20 69 6e 20 6d 65 6d 6f 72 79 0a 2a 2a 20 6f  g in memory.** o
22f8d 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
22f8e 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28 29 2e 20  ite3DbMalloc(). 
22f8f 49 66 20 73 71 6c 69 74 65 33 2e 76 64 62 65 45  If sqlite3.vdbeE
22f90 78 65 63 43 6e 74 20 69 73 20 31 2c 20 74 68 65  xecCnt is 1, the
22f91 6e 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20  n the.** string 
22f92 63 6f 6e 74 61 69 6e 73 20 61 20 63 6f 70 79 20  contains a copy 
22f93 6f 66 20 7a 52 61 77 53 71 6c 20 62 75 74 20 77  of zRawSql but w
22f94 69 74 68 20 68 6f 73 74 20 70 61 72 61 6d 65 74  ith host paramet
22f95 65 72 73 20 65 78 70 61 6e 64 65 64 20 74 6f 20  ers expanded to 
22f96 0a 2a 2a 20 74 68 65 69 72 20 63 75 72 72 65 6e  .** their curren
22f97 74 20 62 69 6e 64 69 6e 67 73 2e 20 4f 72 2c 20  t bindings. Or, 
22f98 69 66 20 73 71 6c 69 74 65 33 2e 76 64 62 65 45  if sqlite3.vdbeE
22f99 78 65 63 43 6e 74 20 69 73 20 67 72 65 61 74 65  xecCnt is greate
22f9a 72 20 74 68 61 6e 20 31 2c 20 0a 2a 2a 20 74 68  r than 1, .** th
22f9b 65 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  en the returned 
22f9c 73 74 72 69 6e 67 20 68 6f 6c 64 73 20 61 20 63  string holds a c
22f9d 6f 70 79 20 6f 66 20 7a 52 61 77 53 71 6c 20 77  opy of zRawSql w
22f9e 69 74 68 20 22 2d 2d 20 22 20 70 72 65 70 65 6e  ith "-- " prepen
22f9f 64 65 64 0a 2a 2a 20 74 6f 20 65 61 63 68 20 6c  ded.** to each l
22fa0 69 6e 65 20 6f 66 20 74 65 78 74 2e 0a 2a 2a 0a  ine of text..**.
22fa1 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66  ** The calling f
22fa2 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f  unction is respo
22fa3 6e 73 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e  nsible for makin
22fa4 67 20 73 75 72 65 20 74 68 65 20 6d 65 6d 6f 72  g sure the memor
22fa5 79 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73  y returned.** is
22fa6 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65   eventually free
22fa7 64 2e 0a 2a 2a 0a 2a 2a 20 41 4c 47 4f 52 49 54  d..**.** ALGORIT
22fa8 48 4d 3a 20 20 53 63 61 6e 20 74 68 65 20 69 6e  HM:  Scan the in
22fa9 70 75 74 20 73 74 72 69 6e 67 20 6c 6f 6f 6b 69  put string looki
22faa 6e 67 20 66 6f 72 20 68 6f 73 74 20 70 61 72 61  ng for host para
22fab 6d 65 74 65 72 73 20 69 6e 20 61 6e 79 20 6f 66  meters in any of
22fac 0a 2a 2a 20 74 68 65 73 65 20 66 6f 72 6d 73 3a  .** these forms:
22fad 20 20 3f 2c 20 3f 4e 2c 20 24 41 2c 20 40 41 2c    ?, ?N, $A, @A,
22fae 20 3a 41 2e 20 20 54 61 6b 65 20 63 61 72 65 20   :A.  Take care 
22faf 74 6f 20 61 76 6f 69 64 20 74 65 78 74 20 77 69  to avoid text wi
22fb0 74 68 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c  thin.** string l
22fb1 69 74 65 72 61 6c 73 2c 20 71 75 6f 74 65 64 20  iterals, quoted 
22fb2 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 73  identifier names
22fb3 2c 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2e 20  , and comments. 
22fb4 20 46 6f 72 20 74 65 78 74 20 66 6f 72 6d 73 2c   For text forms,
22fb5 0a 2a 2a 20 74 68 65 20 68 6f 73 74 20 70 61 72  .** the host par
22fb6 61 6d 65 74 65 72 20 69 6e 64 65 78 20 69 73 20  ameter index is 
22fb7 66 6f 75 6e 64 20 62 79 20 73 63 61 6e 6e 69 6e  found by scannin
22fb8 67 20 74 68 65 20 70 65 72 70 61 72 65 64 0a 2a  g the perpared.*
22fb9 2a 20 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20  * statement for 
22fba 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
22fbb 67 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 6f 70  g OP_Variable op
22fbc 63 6f 64 65 2e 20 20 4f 6e 63 65 20 74 68 65 20  code.  Once the 
22fbd 68 6f 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65  host.** paramete
22fbe 72 20 69 6e 64 65 78 20 69 73 20 6b 6e 6f 77 6e  r index is known
22fbf 2c 20 6c 6f 63 61 74 65 20 74 68 65 20 76 61 6c  , locate the val
22fc0 75 65 20 69 6e 20 70 2d 3e 61 56 61 72 5b 5d 2e  ue in p->aVar[].
22fc1 20 20 54 68 65 6e 20 72 65 6e 64 65 72 0a 2a 2a    Then render.**
22fc2 20 74 68 65 20 76 61 6c 75 65 20 61 73 20 61 20   the value as a 
22fc3 6c 69 74 65 72 61 6c 20 69 6e 20 70 6c 61 63 65  literal in place
22fc4 20 6f 66 20 74 68 65 20 68 6f 73 74 20 70 61 72   of the host par
22fc5 61 6d 65 74 65 72 20 6e 61 6d 65 2e 0a 2a 2f 0a  ameter name..*/.
22fc6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
22fc7 68 61 72 20 2a 73 71 6c 69 74 65 33 56 64 62 65  har *sqlite3Vdbe
22fc8 45 78 70 61 6e 64 53 71 6c 28 0a 20 20 56 64 62  ExpandSql(.  Vdb
22fc9 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  e *p,           
22fca 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65        /* The pre
22fcb 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
22fcc 62 65 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20  being evaluated 
22fcd 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
22fce 2a 7a 52 61 77 53 71 6c 20 20 20 20 20 20 2f 2a  *zRawSql      /*
22fcf 20 52 61 77 20 74 65 78 74 20 6f 66 20 74 68 65   Raw text of the
22fd0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a   SQL statement *
22fd1 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
22fd2 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  db;             
22fd3 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
22fd4 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
22fd5 69 6e 74 20 69 64 78 20 3d 20 30 3b 20 20 20 20  int idx = 0;    
22fd6 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
22fd7 78 20 6f 66 20 61 20 68 6f 73 74 20 70 61 72 61  x of a host para
22fd8 6d 65 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  meter */.  int n
22fd9 65 78 74 49 6e 64 65 78 20 3d 20 31 3b 20 20 20  extIndex = 1;   
22fda 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
22fdb 6e 65 78 74 20 3f 20 68 6f 73 74 20 70 61 72 61  next ? host para
22fdc 6d 65 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  meter */.  int n
22fdd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22fde 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
22fdf 20 61 20 74 6f 6b 65 6e 20 70 72 65 66 69 78 20   a token prefix 
22fe0 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b  */.  int nToken;
22fe1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22fe2 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 70   Length of the p
22fe3 61 72 61 6d 65 74 65 72 20 74 6f 6b 65 6e 20 2a  arameter token *
22fe4 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
22fe5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
22fe6 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
22fe7 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 20    Mem *pVar;    
22fe8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
22fe9 6c 75 65 20 6f 66 20 61 20 68 6f 73 74 20 70 61  lue of a host pa
22fea 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 53 74 72  rameter */.  Str
22feb 41 63 63 75 6d 20 6f 75 74 3b 20 20 20 20 20 20  Accum out;      
22fec 20 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c        /* Accumul
22fed 61 74 65 20 74 68 65 20 6f 75 74 70 75 74 20 68  ate the output h
22fee 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 7a 42  ere */.  char zB
22fef 61 73 65 5b 31 30 30 5d 3b 20 20 20 20 20 20 20  ase[100];       
22ff0 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 77 6f 72    /* Initial wor
22ff1 6b 69 6e 67 20 73 70 61 63 65 20 2a 2f 0a 0a 20  king space */.. 
22ff2 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 73   db = p->db;.  s
22ff3 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e  qlite3StrAccumIn
22ff4 69 74 28 26 6f 75 74 2c 20 7a 42 61 73 65 2c 20  it(&out, zBase, 
22ff5 73 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20 0a  sizeof(zBase), .
22ff6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22ff7 20 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74        db->aLimit
22ff8 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
22ff9 4e 47 54 48 5d 29 3b 0a 20 20 6f 75 74 2e 64 62  NGTH]);.  out.db
22ffa 20 3d 20 64 62 3b 0a 20 20 69 66 28 20 64 62 2d   = db;.  if( db-
22ffb 3e 76 64 62 65 45 78 65 63 43 6e 74 3e 31 20 29  >vdbeExecCnt>1 )
22ffc 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 52  {.    while( *zR
22ffd 61 77 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 63  awSql ){.      c
22ffe 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61 72  onst char *zStar
22fff 74 20 3d 20 7a 52 61 77 53 71 6c 3b 0a 20 20 20  t = zRawSql;.   
23000 20 20 20 77 68 69 6c 65 28 20 2a 28 7a 52 61 77     while( *(zRaw
23001 53 71 6c 2b 2b 29 21 3d 27 5c 6e 27 20 26 26 20  Sql++)!='\n' && 
23002 2a 7a 52 61 77 53 71 6c 20 29 3b 0a 20 20 20 20  *zRawSql );.    
23003 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
23004 6d 41 70 70 65 6e 64 28 26 6f 75 74 2c 20 22 2d  mAppend(&out, "-
23005 2d 20 22 2c 20 33 29 3b 0a 20 20 20 20 20 20 73  - ", 3);.      s
23006 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
23007 70 65 6e 64 28 26 6f 75 74 2c 20 7a 53 74 61 72  pend(&out, zStar
23008 74 2c 20 28 69 6e 74 29 28 7a 52 61 77 53 71 6c  t, (int)(zRawSql
23009 2d 7a 53 74 61 72 74 29 29 3b 0a 20 20 20 20 7d  -zStart));.    }
2300a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68  .  }else{.    wh
2300b 69 6c 65 28 20 7a 52 61 77 53 71 6c 5b 30 5d 20  ile( zRawSql[0] 
2300c 29 7b 0a 20 20 20 20 20 20 6e 20 3d 20 66 69 6e  ){.      n = fin
2300d 64 4e 65 78 74 48 6f 73 74 50 61 72 61 6d 65 74  dNextHostParamet
2300e 65 72 28 7a 52 61 77 53 71 6c 2c 20 26 6e 54 6f  er(zRawSql, &nTo
2300f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ken);.      asse
23010 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 20  rt( n>0 );.     
23011 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
23012 41 70 70 65 6e 64 28 26 6f 75 74 2c 20 7a 52 61  Append(&out, zRa
23013 77 53 71 6c 2c 20 6e 29 3b 0a 20 20 20 20 20 20  wSql, n);.      
23014 7a 52 61 77 53 71 6c 20 2b 3d 20 6e 3b 0a 20 20  zRawSql += n;.  
23015 20 20 20 20 61 73 73 65 72 74 28 20 7a 52 61 77      assert( zRaw
23016 53 71 6c 5b 30 5d 20 7c 7c 20 6e 54 6f 6b 65 6e  Sql[0] || nToken
23017 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ==0 );.      if(
23018 20 6e 54 6f 6b 65 6e 3d 3d 30 20 29 20 62 72 65   nToken==0 ) bre
23019 61 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 52  ak;.      if( zR
2301a 61 77 53 71 6c 5b 30 5d 3d 3d 27 3f 27 20 29 7b  awSql[0]=='?' ){
2301b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 54 6f  .        if( nTo
2301c 6b 65 6e 3e 31 20 29 7b 0a 20 20 20 20 20 20 20  ken>1 ){.       
2301d 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
2301e 65 33 49 73 64 69 67 69 74 28 7a 52 61 77 53 71  e3Isdigit(zRawSq
2301f 6c 5b 31 5d 29 20 29 3b 0a 20 20 20 20 20 20 20  l[1]) );.       
23020 20 20 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74     sqlite3GetInt
23021 33 32 28 26 7a 52 61 77 53 71 6c 5b 31 5d 2c 20  32(&zRawSql[1], 
23022 26 69 64 78 29 3b 0a 20 20 20 20 20 20 20 20 7d  &idx);.        }
23023 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
23024 69 64 78 20 3d 20 6e 65 78 74 49 6e 64 65 78 3b  idx = nextIndex;
23025 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
23026 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
23027 61 73 73 65 72 74 28 20 7a 52 61 77 53 71 6c 5b  assert( zRawSql[
23028 30 5d 3d 3d 27 3a 27 20 7c 7c 20 7a 52 61 77 53  0]==':' || zRawS
23029 71 6c 5b 30 5d 3d 3d 27 24 27 20 7c 7c 20 7a 52  ql[0]=='$' || zR
2302a 61 77 53 71 6c 5b 30 5d 3d 3d 27 40 27 20 29 3b  awSql[0]=='@' );
2302b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
2302c 65 28 20 7a 52 61 77 53 71 6c 5b 30 5d 3d 3d 27  e( zRawSql[0]=='
2302d 3a 27 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65  :' );.        te
2302e 73 74 63 61 73 65 28 20 7a 52 61 77 53 71 6c 5b  stcase( zRawSql[
2302f 30 5d 3d 3d 27 24 27 20 29 3b 0a 20 20 20 20 20  0]=='$' );.     
23030 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 52 61     testcase( zRa
23031 77 53 71 6c 5b 30 5d 3d 3d 27 40 27 20 29 3b 0a  wSql[0]=='@' );.
23032 20 20 20 20 20 20 20 20 69 64 78 20 3d 20 73 71          idx = sq
23033 6c 69 74 65 33 56 64 62 65 50 61 72 61 6d 65 74  lite3VdbeParamet
23034 65 72 49 6e 64 65 78 28 70 2c 20 7a 52 61 77 53  erIndex(p, zRawS
23035 71 6c 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20  ql, nToken);.   
23036 20 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78       assert( idx
23037 3e 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  >0 );.      }.  
23038 20 20 20 20 7a 52 61 77 53 71 6c 20 2b 3d 20 6e      zRawSql += n
23039 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 6e 65 78  Token;.      nex
2303a 74 49 6e 64 65 78 20 3d 20 69 64 78 20 2b 20 31  tIndex = idx + 1
2303b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2303c 69 64 78 3e 30 20 26 26 20 69 64 78 3c 3d 70 2d  idx>0 && idx<=p-
2303d 3e 6e 56 61 72 20 29 3b 0a 20 20 20 20 20 20 70  >nVar );.      p
2303e 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69  Var = &p->aVar[i
2303f 64 78 2d 31 5d 3b 0a 20 20 20 20 20 20 69 66 28  dx-1];.      if(
23040 20 70 56 61 72 2d 3e 66 6c 61 67 73 20 26 20 4d   pVar->flags & M
23041 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20  EM_Null ){.     
23042 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
23043 75 6d 41 70 70 65 6e 64 28 26 6f 75 74 2c 20 22  umAppend(&out, "
23044 4e 55 4c 4c 22 2c 20 34 29 3b 0a 20 20 20 20 20  NULL", 4);.     
23045 20 7d 65 6c 73 65 20 69 66 28 20 70 56 61 72 2d   }else if( pVar-
23046 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
23047 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
23048 74 65 33 58 50 72 69 6e 74 66 28 26 6f 75 74 2c  te3XPrintf(&out,
23049 20 22 25 6c 6c 64 22 2c 20 70 56 61 72 2d 3e 75   "%lld", pVar->u
2304a 2e 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  .i);.      }else
2304b 20 69 66 28 20 70 56 61 72 2d 3e 66 6c 61 67 73   if( pVar->flags
2304c 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20   & MEM_Real ){. 
2304d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 58 50         sqlite3XP
2304e 72 69 6e 74 66 28 26 6f 75 74 2c 20 22 25 21 2e  rintf(&out, "%!.
2304f 31 35 67 22 2c 20 70 56 61 72 2d 3e 72 29 3b 0a  15g", pVar->r);.
23050 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
23051 70 56 61 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pVar->flags & ME
23052 4d 5f 53 74 72 20 29 7b 0a 23 69 66 6e 64 65 66  M_Str ){.#ifndef
23053 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
23054 31 36 0a 20 20 20 20 20 20 20 20 75 38 20 65 6e  16.        u8 en
23055 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20  c = ENC(db);.   
23056 20 20 20 20 20 69 66 28 20 65 6e 63 21 3d 53 51       if( enc!=SQ
23057 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20  LITE_UTF8 ){.   
23058 20 20 20 20 20 20 20 4d 65 6d 20 75 74 66 38 3b         Mem utf8;
23059 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 65  .          memse
2305a 74 28 26 75 74 66 38 2c 20 30 2c 20 73 69 7a 65  t(&utf8, 0, size
2305b 6f 66 28 75 74 66 38 29 29 3b 0a 20 20 20 20 20  of(utf8));.     
2305c 20 20 20 20 20 75 74 66 38 2e 64 62 20 3d 20 64       utf8.db = d
2305d 62 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  b;.          sql
2305e 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
2305f 72 28 26 75 74 66 38 2c 20 70 56 61 72 2d 3e 7a  r(&utf8, pVar->z
23060 2c 20 70 56 61 72 2d 3e 6e 2c 20 65 6e 63 2c 20  , pVar->n, enc, 
23061 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
23062 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
23063 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
23064 69 6e 67 28 26 75 74 66 38 2c 20 53 51 4c 49 54  ing(&utf8, SQLIT
23065 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 20 20 20  E_UTF8);.       
23066 20 20 20 73 71 6c 69 74 65 33 58 50 72 69 6e 74     sqlite3XPrint
23067 66 28 26 6f 75 74 2c 20 22 27 25 2e 2a 71 27 22  f(&out, "'%.*q'"
23068 2c 20 75 74 66 38 2e 6e 2c 20 75 74 66 38 2e 7a  , utf8.n, utf8.z
23069 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
2306a 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
2306b 73 65 28 26 75 74 66 38 29 3b 0a 20 20 20 20 20  se(&utf8);.     
2306c 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a     }else.#endif.
2306d 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20          {.      
2306e 20 20 20 20 73 71 6c 69 74 65 33 58 50 72 69 6e      sqlite3XPrin
2306f 74 66 28 26 6f 75 74 2c 20 22 27 25 2e 2a 71 27  tf(&out, "'%.*q'
23070 22 2c 20 70 56 61 72 2d 3e 6e 2c 20 70 56 61 72  ", pVar->n, pVar
23071 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ->z);.        }.
23072 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
23073 70 56 61 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pVar->flags & ME
23074 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
23075 20 20 73 71 6c 69 74 65 33 58 50 72 69 6e 74 66    sqlite3XPrintf
23076 28 26 6f 75 74 2c 20 22 7a 65 72 6f 62 6c 6f 62  (&out, "zeroblob
23077 28 25 64 29 22 2c 20 70 56 61 72 2d 3e 75 2e 6e  (%d)", pVar->u.n
23078 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7d 65 6c  Zero);.      }el
23079 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  se{.        asse
2307a 72 74 28 20 70 56 61 72 2d 3e 66 6c 61 67 73 20  rt( pVar->flags 
2307b 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20  & MEM_Blob );.  
2307c 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
2307d 41 63 63 75 6d 41 70 70 65 6e 64 28 26 6f 75 74  AccumAppend(&out
2307e 2c 20 22 78 27 22 2c 20 32 29 3b 0a 20 20 20 20  , "x'", 2);.    
2307f 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
23080 56 61 72 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Var->n; i++){.  
23081 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 58          sqlite3X
23082 50 72 69 6e 74 66 28 26 6f 75 74 2c 20 22 25 30  Printf(&out, "%0
23083 32 78 22 2c 20 70 56 61 72 2d 3e 7a 5b 69 5d 26  2x", pVar->z[i]&
23084 30 78 66 66 29 3b 0a 20 20 20 20 20 20 20 20 7d  0xff);.        }
23085 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
23086 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26  StrAccumAppend(&
23087 6f 75 74 2c 20 22 27 22 2c 20 31 29 3b 0a 20 20  out, "'", 1);.  
23088 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
23089 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
2308a 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26  StrAccumFinish(&
2308b 6f 75 74 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  out);.}..#endif 
2308c 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /* #ifndef SQLIT
2308d 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 2a 2f 0a  E_OMIT_TRACE */.
2308e 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2308f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23091 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23092 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
23093 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
23094 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73   code implements
23095 20 74 68 65 20 64 61 74 61 2d 73 74 72 75 63 74   the data-struct
23096 75 72 65 20 65 78 70 6c 61 69 6e 69 6e 67 20 6c  ure explaining l
23097 6f 67 69 63 0a 2a 2a 20 66 6f 72 20 74 68 65 20  ogic.** for the 
23098 56 64 62 65 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65  Vdbe..*/..#if de
23099 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
2309a 42 4c 45 5f 54 52 45 45 5f 45 58 50 4c 41 49 4e  BLE_TREE_EXPLAIN
2309b 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  )../*.** Allocat
2309c 65 20 61 20 6e 65 77 20 45 78 70 6c 61 69 6e 20  e a new Explain 
2309d 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45  object.*/.SQLITE
2309e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2309f 6c 69 74 65 33 45 78 70 6c 61 69 6e 42 65 67 69  lite3ExplainBegi
230a0 6e 28 56 64 62 65 20 2a 70 56 64 62 65 29 7b 0a  n(Vdbe *pVdbe){.
230a1 20 20 69 66 28 20 70 56 64 62 65 20 29 7b 0a 20    if( pVdbe ){. 
230a2 20 20 20 45 78 70 6c 61 69 6e 20 2a 70 3b 0a 20     Explain *p;. 
230a3 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42     sqlite3BeginB
230a4 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
230a5 20 20 20 70 20 3d 20 28 45 78 70 6c 61 69 6e 20     p = (Explain 
230a6 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  *)sqlite3MallocZ
230a7 65 72 6f 28 20 73 69 7a 65 6f 66 28 45 78 70 6c  ero( sizeof(Expl
230a8 61 69 6e 29 20 29 3b 0a 20 20 20 20 69 66 28 20  ain) );.    if( 
230a9 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 56  p ){.      p->pV
230aa 64 62 65 20 3d 20 70 56 64 62 65 3b 0a 20 20 20  dbe = pVdbe;.   
230ab 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
230ac 70 56 64 62 65 2d 3e 70 45 78 70 6c 61 69 6e 29  pVdbe->pExplain)
230ad 3b 0a 20 20 20 20 20 20 70 56 64 62 65 2d 3e 70  ;.      pVdbe->p
230ae 45 78 70 6c 61 69 6e 20 3d 20 70 3b 0a 20 20 20  Explain = p;.   
230af 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
230b0 75 6d 49 6e 69 74 28 26 70 2d 3e 73 74 72 2c 20  umInit(&p->str, 
230b1 70 2d 3e 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66  p->zBase, sizeof
230b2 28 70 2d 3e 7a 42 61 73 65 29 2c 0a 20 20 20 20  (p->zBase),.    
230b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
230b4 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 41 58        SQLITE_MAX
230b5 5f 4c 45 4e 47 54 48 29 3b 0a 20 20 20 20 20 20  _LENGTH);.      
230b6 70 2d 3e 73 74 72 2e 75 73 65 4d 61 6c 6c 6f 63  p->str.useMalloc
230b7 20 3d 20 32 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 2;.    }else{
230b8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e  .      sqlite3En
230b9 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
230ba 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
230bb 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
230bc 69 66 20 74 68 65 20 45 78 70 6c 61 69 6e 20 65  if the Explain e
230bd 6e 64 73 20 77 69 74 68 20 61 20 6e 65 77 2d 6c  nds with a new-l
230be 69 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ine..*/.static i
230bf 6e 74 20 65 6e 64 73 57 69 74 68 4e 4c 28 45 78  nt endsWithNL(Ex
230c0 70 6c 61 69 6e 20 2a 70 29 7b 0a 20 20 72 65 74  plain *p){.  ret
230c1 75 72 6e 20 70 20 26 26 20 70 2d 3e 73 74 72 2e  urn p && p->str.
230c2 7a 54 65 78 74 20 26 26 20 70 2d 3e 73 74 72 2e  zText && p->str.
230c3 6e 43 68 61 72 0a 20 20 20 20 20 20 20 20 20 20  nChar.          
230c4 20 26 26 20 70 2d 3e 73 74 72 2e 7a 54 65 78 74   && p->str.zText
230c5 5b 70 2d 3e 73 74 72 2e 6e 43 68 61 72 2d 31 5d  [p->str.nChar-1]
230c6 3d 3d 27 5c 6e 27 3b 0a 7d 0a 20 20 20 20 0a 2f  =='\n';.}.    ./
230c7 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 74 65 78 74  *.** Append text
230c8 20 74 6f 20 74 68 65 20 69 6e 64 65 6e 74 61 74   to the indentat
230c9 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ion.*/.SQLITE_PR
230ca 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
230cb 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28  e3ExplainPrintf(
230cc 56 64 62 65 20 2a 70 56 64 62 65 2c 20 63 6f 6e  Vdbe *pVdbe, con
230cd 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
230ce 2c 20 2e 2e 2e 29 7b 0a 20 20 45 78 70 6c 61 69  , ...){.  Explai
230cf 6e 20 2a 70 3b 0a 20 20 69 66 28 20 70 56 64 62  n *p;.  if( pVdb
230d0 65 20 26 26 20 28 70 20 3d 20 70 56 64 62 65 2d  e && (p = pVdbe-
230d1 3e 70 45 78 70 6c 61 69 6e 29 21 3d 30 20 29 7b  >pExplain)!=0 ){
230d2 0a 20 20 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  .    va_list ap;
230d3 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 49 6e 64  .    if( p->nInd
230d4 65 6e 74 20 26 26 20 65 6e 64 73 57 69 74 68 4e  ent && endsWithN
230d5 4c 28 70 29 20 29 7b 0a 20 20 20 20 20 20 69 6e  L(p) ){.      in
230d6 74 20 6e 20 3d 20 70 2d 3e 6e 49 6e 64 65 6e 74  t n = p->nIndent
230d7 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 41 72  ;.      if( n>Ar
230d8 72 61 79 53 69 7a 65 28 70 2d 3e 61 49 6e 64 65  raySize(p->aInde
230d9 6e 74 29 20 29 20 6e 20 3d 20 41 72 72 61 79 53  nt) ) n = ArrayS
230da 69 7a 65 28 70 2d 3e 61 49 6e 64 65 6e 74 29 3b  ize(p->aIndent);
230db 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 70  .      sqlite3Ap
230dc 70 65 6e 64 53 70 61 63 65 28 26 70 2d 3e 73 74  pendSpace(&p->st
230dd 72 2c 20 70 2d 3e 61 49 6e 64 65 6e 74 5b 6e 2d  r, p->aIndent[n-
230de 31 5d 29 3b 0a 20 20 20 20 7d 20 20 20 0a 20 20  1]);.    }   .  
230df 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
230e0 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c  Format);.    sql
230e1 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 70 2d  ite3VXPrintf(&p-
230e2 3e 73 74 72 2c 20 31 2c 20 7a 46 6f 72 6d 61 74  >str, 1, zFormat
230e3 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e  , ap);.    va_en
230e4 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  d(ap);.  }.}../*
230e5 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 27 5c 6e  .** Append a '\n
230e6 27 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f  ' if there is no
230e7 74 20 61 6c 72 65 61 64 79 20 6f 6e 65 2e 0a 2a  t already one..*
230e8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
230e9 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
230ea 6c 61 69 6e 4e 4c 28 56 64 62 65 20 2a 70 56 64  lainNL(Vdbe *pVd
230eb 62 65 29 7b 0a 20 20 45 78 70 6c 61 69 6e 20 2a  be){.  Explain *
230ec 70 3b 0a 20 20 69 66 28 20 70 56 64 62 65 20 26  p;.  if( pVdbe &
230ed 26 20 28 70 20 3d 20 70 56 64 62 65 2d 3e 70 45  & (p = pVdbe->pE
230ee 78 70 6c 61 69 6e 29 21 3d 30 20 26 26 20 21 65  xplain)!=0 && !e
230ef 6e 64 73 57 69 74 68 4e 4c 28 70 29 20 29 7b 0a  ndsWithNL(p) ){.
230f0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
230f1 63 75 6d 41 70 70 65 6e 64 28 26 70 2d 3e 73 74  cumAppend(&p->st
230f2 72 2c 20 22 5c 6e 22 2c 20 31 29 3b 0a 20 20 7d  r, "\n", 1);.  }
230f3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 75 73 68 20 61  .}../*.** Push a
230f4 20 6e 65 77 20 69 6e 64 65 6e 74 61 74 69 6f 6e   new indentation
230f5 20 6c 65 76 65 6c 2e 20 20 53 75 62 73 65 71 75   level.  Subsequ
230f6 65 6e 74 20 6c 69 6e 65 73 20 77 69 6c 6c 20 62  ent lines will b
230f7 65 20 69 6e 64 65 6e 74 65 64 0a 2a 2a 20 73 6f  e indented.** so
230f8 20 74 68 61 74 20 74 68 65 79 20 62 65 67 69 6e   that they begin
230f9 20 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20   at the current 
230fa 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e  cursor position.
230fb 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
230fc 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
230fd 78 70 6c 61 69 6e 50 75 73 68 28 56 64 62 65 20  xplainPush(Vdbe 
230fe 2a 70 56 64 62 65 29 7b 0a 20 20 45 78 70 6c 61  *pVdbe){.  Expla
230ff 69 6e 20 2a 70 3b 0a 20 20 69 66 28 20 70 56 64  in *p;.  if( pVd
23100 62 65 20 26 26 20 28 70 20 3d 20 70 56 64 62 65  be && (p = pVdbe
23101 2d 3e 70 45 78 70 6c 61 69 6e 29 21 3d 30 20 29  ->pExplain)!=0 )
23102 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 74 72  {.    if( p->str
23103 2e 7a 54 65 78 74 20 26 26 20 70 2d 3e 6e 49 6e  .zText && p->nIn
23104 64 65 6e 74 3c 41 72 72 61 79 53 69 7a 65 28 70  dent<ArraySize(p
23105 2d 3e 61 49 6e 64 65 6e 74 29 20 29 7b 0a 20 20  ->aIndent) ){.  
23106 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
23107 7a 20 3d 20 70 2d 3e 73 74 72 2e 7a 54 65 78 74  z = p->str.zText
23108 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20  ;.      int i = 
23109 70 2d 3e 73 74 72 2e 6e 43 68 61 72 2d 31 3b 0a  p->str.nChar-1;.
2310a 20 20 20 20 20 20 69 6e 74 20 78 3b 0a 20 20 20        int x;.   
2310b 20 20 20 77 68 69 6c 65 28 20 69 3e 3d 30 20 26     while( i>=0 &
2310c 26 20 7a 5b 69 5d 21 3d 27 5c 6e 27 20 29 7b 20  & z[i]!='\n' ){ 
2310d 69 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 78 20 3d  i--; }.      x =
2310e 20 28 70 2d 3e 73 74 72 2e 6e 43 68 61 72 20 2d   (p->str.nChar -
2310f 20 31 29 20 2d 20 69 3b 0a 20 20 20 20 20 20 69   1) - i;.      i
23110 66 28 20 70 2d 3e 6e 49 6e 64 65 6e 74 20 26 26  f( p->nIndent &&
23111 20 78 3c 70 2d 3e 61 49 6e 64 65 6e 74 5b 70 2d   x<p->aIndent[p-
23112 3e 6e 49 6e 64 65 6e 74 2d 31 5d 20 29 7b 0a 20  >nIndent-1] ){. 
23113 20 20 20 20 20 20 20 78 20 3d 20 70 2d 3e 61 49         x = p->aI
23114 6e 64 65 6e 74 5b 70 2d 3e 6e 49 6e 64 65 6e 74  ndent[p->nIndent
23115 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  -1];.      }.   
23116 20 20 20 70 2d 3e 61 49 6e 64 65 6e 74 5b 70 2d     p->aIndent[p-
23117 3e 6e 49 6e 64 65 6e 74 5d 20 3d 20 78 3b 0a 20  >nIndent] = x;. 
23118 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 49 6e 64     }.    p->nInd
23119 65 6e 74 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ent++;.  }.}../*
2311a 0a 2a 2a 20 50 6f 70 20 74 68 65 20 69 6e 64 65  .** Pop the inde
2311b 6e 74 61 74 69 6f 6e 20 73 74 61 63 6b 20 62 79  ntation stack by
2311c 20 6f 6e 65 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 53   one level..*/.S
2311d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2311e 69 64 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69  id sqlite3Explai
2311f 6e 50 6f 70 28 56 64 62 65 20 2a 70 29 7b 0a 20  nPop(Vdbe *p){. 
23120 20 69 66 28 20 70 20 26 26 20 70 2d 3e 70 45 78   if( p && p->pEx
23121 70 6c 61 69 6e 20 29 20 70 2d 3e 70 45 78 70 6c  plain ) p->pExpl
23122 61 69 6e 2d 3e 6e 49 6e 64 65 6e 74 2d 2d 3b 0a  ain->nIndent--;.
23123 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68  }../*.** Free th
23124 65 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 73 74  e indentation st
23125 72 75 63 74 75 72 65 0a 2a 2f 0a 53 51 4c 49 54  ructure.*/.SQLIT
23126 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
23127 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 46 69 6e  qlite3ExplainFin
23128 69 73 68 28 56 64 62 65 20 2a 70 56 64 62 65 29  ish(Vdbe *pVdbe)
23129 7b 0a 20 20 69 66 28 20 70 56 64 62 65 20 26 26  {.  if( pVdbe &&
2312a 20 70 56 64 62 65 2d 3e 70 45 78 70 6c 61 69 6e   pVdbe->pExplain
2312b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2312c 66 72 65 65 28 70 56 64 62 65 2d 3e 7a 45 78 70  free(pVdbe->zExp
2312d 6c 61 69 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74  lain);.    sqlit
2312e 65 33 45 78 70 6c 61 69 6e 4e 4c 28 70 56 64 62  e3ExplainNL(pVdb
2312f 65 29 3b 0a 20 20 20 20 70 56 64 62 65 2d 3e 7a  e);.    pVdbe->z
23130 45 78 70 6c 61 69 6e 20 3d 20 73 71 6c 69 74 65  Explain = sqlite
23131 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28  3StrAccumFinish(
23132 26 70 56 64 62 65 2d 3e 70 45 78 70 6c 61 69 6e  &pVdbe->pExplain
23133 2d 3e 73 74 72 29 3b 0a 20 20 20 20 73 71 6c 69  ->str);.    sqli
23134 74 65 33 5f 66 72 65 65 28 70 56 64 62 65 2d 3e  te3_free(pVdbe->
23135 70 45 78 70 6c 61 69 6e 29 3b 0a 20 20 20 20 70  pExplain);.    p
23136 56 64 62 65 2d 3e 70 45 78 70 6c 61 69 6e 20 3d  Vdbe->pExplain =
23137 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   0;.    sqlite3E
23138 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
23139 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
2313a 65 74 75 72 6e 20 74 68 65 20 65 78 70 6c 61 6e  eturn the explan
2313b 61 74 69 6f 6e 20 6f 66 20 61 20 76 69 72 74 75  ation of a virtu
2313c 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2f 0a 53  al machine..*/.S
2313d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
2313e 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
2313f 33 56 64 62 65 45 78 70 6c 61 6e 61 74 69 6f 6e  3VdbeExplanation
23140 28 56 64 62 65 20 2a 70 56 64 62 65 29 7b 0a 20  (Vdbe *pVdbe){. 
23141 20 72 65 74 75 72 6e 20 28 70 56 64 62 65 20 26   return (pVdbe &
23142 26 20 70 56 64 62 65 2d 3e 7a 45 78 70 6c 61 69  & pVdbe->zExplai
23143 6e 29 20 3f 20 70 56 64 62 65 2d 3e 7a 45 78 70  n) ? pVdbe->zExp
23144 6c 61 69 6e 20 3a 20 30 3b 0a 7d 0a 23 65 6e 64  lain : 0;.}.#end
23145 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51  if /* defined(SQ
23146 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a  LITE_DEBUG) */..
23147 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
23148 45 6e 64 20 6f 66 20 76 64 62 65 74 72 61 63 65  End of vdbetrace
23149 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2314a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2314b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2314c 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2314d 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 2e  Begin file vdbe.
2314e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2314f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
23151 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
23152 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
23153 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
23154 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
23155 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
23156 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
23157 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
23158 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
23159 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2315a 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2315b 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2315c 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2315d 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2315e 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
2315f 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
23160 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
23161 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
23162 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
23163 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
23164 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23165 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23166 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23167 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23168 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  ******.** The co
23169 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
2316a 69 6d 70 6c 65 6d 65 6e 74 73 20 65 78 65 63 75  implements execu
2316b 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74  tion method of t
2316c 68 65 20 0a 2a 2a 20 56 69 72 74 75 61 6c 20 44  he .** Virtual D
2316d 61 74 61 62 61 73 65 20 45 6e 67 69 6e 65 20 28  atabase Engine (
2316e 56 44 42 45 29 2e 20 20 41 20 73 65 70 61 72 61  VDBE).  A separa
2316f 74 65 20 66 69 6c 65 20 28 22 76 64 62 65 61 75  te file ("vdbeau
23170 78 2e 63 22 29 0a 2a 2a 20 68 61 6e 64 6c 65 73  x.c").** handles
23171 20 68 6f 75 73 65 6b 65 65 70 69 6e 67 20 64 65   housekeeping de
23172 74 61 69 6c 73 20 73 75 63 68 20 61 73 20 63 72  tails such as cr
23173 65 61 74 69 6e 67 20 61 6e 64 20 64 65 6c 65 74  eating and delet
23174 69 6e 67 0a 2a 2a 20 56 44 42 45 20 69 6e 73 74  ing.** VDBE inst
23175 61 6e 63 65 73 2e 20 20 54 68 69 73 20 66 69 6c  ances.  This fil
23176 65 20 69 73 20 73 6f 6c 65 6c 79 20 69 6e 74 65  e is solely inte
23177 72 65 73 74 65 64 20 69 6e 20 65 78 65 63 75 74  rested in execut
23178 69 6e 67 0a 2a 2a 20 74 68 65 20 56 44 42 45 20  ing.** the VDBE 
23179 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49  program..**.** I
2317a 6e 20 74 68 65 20 65 78 74 65 72 6e 61 6c 20 69  n the external i
2317b 6e 74 65 72 66 61 63 65 2c 20 61 6e 20 22 73 71  nterface, an "sq
2317c 6c 69 74 65 33 5f 73 74 6d 74 2a 22 20 69 73 20  lite3_stmt*" is 
2317d 61 6e 20 6f 70 61 71 75 65 20 70 6f 69 6e 74 65  an opaque pointe
2317e 72 0a 2a 2a 20 74 6f 20 61 20 56 44 42 45 2e 0a  r.** to a VDBE..
2317f 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 20 70 61  **.** The SQL pa
23180 72 73 65 72 20 67 65 6e 65 72 61 74 65 73 20 61  rser generates a
23181 20 70 72 6f 67 72 61 6d 20 77 68 69 63 68 20 69   program which i
23182 73 20 74 68 65 6e 20 65 78 65 63 75 74 65 64 20  s then executed 
23183 62 79 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 74  by.** the VDBE t
23184 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66  o do the work of
23185 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
23186 6e 74 2e 20 20 56 44 42 45 20 70 72 6f 67 72 61  nt.  VDBE progra
23187 6d 73 20 61 72 65 20 0a 2a 2a 20 73 69 6d 69 6c  ms are .** simil
23188 61 72 20 69 6e 20 66 6f 72 6d 20 74 6f 20 61 73  ar in form to as
23189 73 65 6d 62 6c 79 20 6c 61 6e 67 75 61 67 65 2e  sembly language.
2318a 20 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f    The program co
2318b 6e 73 69 73 74 73 20 6f 66 0a 2a 2a 20 61 20 6c  nsists of.** a l
2318c 69 6e 65 61 72 20 73 65 71 75 65 6e 63 65 20 6f  inear sequence o
2318d 66 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 45  f operations.  E
2318e 61 63 68 20 6f 70 65 72 61 74 69 6f 6e 20 68 61  ach operation ha
2318f 73 20 61 6e 20 6f 70 63 6f 64 65 20 0a 2a 2a 20  s an opcode .** 
23190 61 6e 64 20 35 20 6f 70 65 72 61 6e 64 73 2e 20  and 5 operands. 
23191 20 4f 70 65 72 61 6e 64 73 20 50 31 2c 20 50 32   Operands P1, P2
23192 2c 20 61 6e 64 20 50 33 20 61 72 65 20 69 6e 74  , and P3 are int
23193 65 67 65 72 73 2e 20 20 4f 70 65 72 61 6e 64 20  egers.  Operand 
23194 50 34 20 0a 2a 2a 20 69 73 20 61 20 6e 75 6c 6c  P4 .** is a null
23195 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69  -terminated stri
23196 6e 67 2e 20 20 4f 70 65 72 61 6e 64 20 50 35 20  ng.  Operand P5 
23197 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 63  is an unsigned c
23198 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 46 65 77  haracter..** Few
23199 20 6f 70 63 6f 64 65 73 20 75 73 65 20 61 6c 6c   opcodes use all
2319a 20 35 20 6f 70 65 72 61 6e 64 73 2e 0a 2a 2a 0a   5 operands..**.
2319b 2a 2a 20 43 6f 6d 70 75 74 61 74 69 6f 6e 20 72  ** Computation r
2319c 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65  esults are store
2319d 64 20 6f 6e 20 61 20 73 65 74 20 6f 66 20 72 65  d on a set of re
2319e 67 69 73 74 65 72 73 20 6e 75 6d 62 65 72 65 64  gisters numbered
2319f 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69   beginning.** wi
231a0 74 68 20 31 20 61 6e 64 20 67 6f 69 6e 67 20 75  th 1 and going u
231a1 70 20 74 6f 20 56 64 62 65 2e 6e 4d 65 6d 2e 20  p to Vdbe.nMem. 
231a2 20 45 61 63 68 20 72 65 67 69 73 74 65 72 20 63   Each register c
231a3 61 6e 20 73 74 6f 72 65 0a 2a 2a 20 65 69 74 68  an store.** eith
231a4 65 72 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 61  er an integer, a
231a5 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64   null-terminated
231a6 20 73 74 72 69 6e 67 2c 20 61 20 66 6c 6f 61 74   string, a float
231a7 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 6e 75 6d  ing point.** num
231a8 62 65 72 2c 20 6f 72 20 74 68 65 20 53 51 4c 20  ber, or the SQL 
231a9 22 4e 55 4c 4c 22 20 76 61 6c 75 65 2e 20 20 41  "NULL" value.  A
231aa 6e 20 69 6d 70 6c 69 63 69 74 20 63 6f 6e 76 65  n implicit conve
231ab 72 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e 65 0a 2a  rsion from one.*
231ac 2a 20 74 79 70 65 20 74 6f 20 74 68 65 20 6f 74  * type to the ot
231ad 68 65 72 20 6f 63 63 75 72 73 20 61 73 20 6e 65  her occurs as ne
231ae 63 65 73 73 61 72 79 2e 0a 2a 2a 20 0a 2a 2a 20  cessary..** .** 
231af 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f 64 65  Most of the code
231b0 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73   in this file is
231b1 20 74 61 6b 65 6e 20 75 70 20 62 79 20 74 68 65   taken up by the
231b2 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63   sqlite3VdbeExec
231b3 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77  ().** function w
231b4 68 69 63 68 20 64 6f 65 73 20 74 68 65 20 77 6f  hich does the wo
231b5 72 6b 20 6f 66 20 69 6e 74 65 72 70 72 65 74 69  rk of interpreti
231b6 6e 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 61  ng a VDBE progra
231b7 6d 2e 0a 2a 2a 20 42 75 74 20 6f 74 68 65 72 20  m..** But other 
231b8 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c 73  routines are als
231b9 6f 20 70 72 6f 76 69 64 65 64 20 74 6f 20 68 65  o provided to he
231ba 6c 70 20 69 6e 20 62 75 69 6c 64 69 6e 67 20 75  lp in building u
231bb 70 0a 2a 2a 20 61 20 70 72 6f 67 72 61 6d 20 69  p.** a program i
231bc 6e 73 74 72 75 63 74 69 6f 6e 20 62 79 20 69 6e  nstruction by in
231bd 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  struction..**.**
231be 20 56 61 72 69 6f 75 73 20 73 63 72 69 70 74 73   Various scripts
231bf 20 73 63 61 6e 20 74 68 69 73 20 73 6f 75 72 63   scan this sourc
231c0 65 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20  e file in order 
231c1 74 6f 20 67 65 6e 65 72 61 74 65 20 48 54 4d 4c  to generate HTML
231c2 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  .** documentatio
231c3 6e 2c 20 68 65 61 64 65 72 73 20 66 69 6c 65 73  n, headers files
231c4 2c 20 6f 72 20 6f 74 68 65 72 20 64 65 72 69 76  , or other deriv
231c5 65 64 20 66 69 6c 65 73 2e 20 20 54 68 65 20 66  ed files.  The f
231c6 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 66 20  ormatting.** of 
231c7 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  the code in this
231c8 20 66 69 6c 65 20 69 73 2c 20 74 68 65 72 65 66   file is, theref
231c9 6f 72 65 2c 20 69 6d 70 6f 72 74 61 6e 74 2e 20  ore, important. 
231ca 20 53 65 65 20 6f 74 68 65 72 20 63 6f 6d 6d 65   See other comme
231cb 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 69 73 20 66  nts.** in this f
231cc 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  ile for details.
231cd 20 20 49 66 20 69 6e 20 64 6f 75 62 74 2c 20 64    If in doubt, d
231ce 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 72  o not deviate fr
231cf 6f 6d 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 63  om existing.** c
231d0 6f 6d 6d 65 6e 74 69 6e 67 20 61 6e 64 20 69 6e  ommenting and in
231d1 64 65 6e 74 61 74 69 6f 6e 20 70 72 61 63 74 69  dentation practi
231d2 63 65 73 20 77 68 65 6e 20 63 68 61 6e 67 69 6e  ces when changin
231d3 67 20 6f 72 20 61 64 64 69 6e 67 20 63 6f 64 65  g or adding code
231d4 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f  ..*/../*.** Invo
231d5 6b 65 20 74 68 69 73 20 6d 61 63 72 6f 20 6f 6e  ke this macro on
231d6 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 6a 75   memory cells ju
231d7 73 74 20 70 72 69 6f 72 20 74 6f 20 63 68 61 6e  st prior to chan
231d8 67 69 6e 67 20 74 68 65 0a 2a 2a 20 76 61 6c 75  ging the.** valu
231d9 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 2e 20 20  e of the cell.  
231da 54 68 69 73 20 6d 61 63 72 6f 20 76 65 72 69 66  This macro verif
231db 69 65 73 20 74 68 61 74 20 73 68 61 6c 6c 6f 77  ies that shallow
231dc 20 63 6f 70 69 65 73 20 61 72 65 0a 2a 2a 20 6e   copies are.** n
231dd 6f 74 20 6d 69 73 75 73 65 64 2e 0a 2a 2f 0a 23  ot misused..*/.#
231de 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
231df 55 47 0a 23 20 64 65 66 69 6e 65 20 6d 65 6d 41  UG.# define memA
231e0 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 50 2c 4d  boutToChange(P,M
231e1 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ) sqlite3VdbeMem
231e2 41 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 50 2c  AboutToChange(P,
231e3 4d 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  M).#else.# defin
231e4 65 20 6d 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e  e memAboutToChan
231e5 67 65 28 50 2c 4d 29 0a 23 65 6e 64 69 66 0a 0a  ge(P,M).#endif..
231e6 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
231e7 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ing global varia
231e8 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  ble is increment
231e9 65 64 20 65 76 65 72 79 20 74 69 6d 65 20 61 20  ed every time a 
231ea 63 75 72 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c  cursor.** moves,
231eb 20 65 69 74 68 65 72 20 62 79 20 74 68 65 20 4f   either by the O
231ec 50 5f 53 65 65 6b 58 58 2c 20 4f 50 5f 4e 65 78  P_SeekXX, OP_Nex
231ed 74 2c 20 6f 72 20 4f 50 5f 50 72 65 76 20 6f 70  t, or OP_Prev op
231ee 63 6f 64 65 73 2e 20 20 54 68 65 20 74 65 73 74  codes.  The test
231ef 0a 2a 2a 20 70 72 6f 63 65 64 75 72 65 73 20 75  .** procedures u
231f0 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74  se this informat
231f1 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ion to make sure
231f2 20 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 72   that indices ar
231f3 65 0a 2a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f 72  e.** working cor
231f4 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 76 61  rectly.  This va
231f5 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75  riable has no fu
231f6 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61  nction other tha
231f7 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72  n to.** help ver
231f8 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20  ify the correct 
231f9 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  operation of the
231fa 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66   library..*/.#if
231fb 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
231fc 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
231fd 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f  qlite3_search_co
231fe 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  unt = 0;.#endif.
231ff 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73  ./*.** When this
23200 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
23201 20 69 73 20 70 6f 73 69 74 69 76 65 2c 20 69 74   is positive, it
23202 20 67 65 74 73 20 64 65 63 72 65 6d 65 6e 74 65   gets decremente
23203 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a  d once before.**
23204 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f   each instructio
23205 6e 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20  n in the VDBE.  
23206 57 68 65 6e 20 69 74 20 72 65 61 63 68 65 73 20  When it reaches 
23207 7a 65 72 6f 2c 20 74 68 65 20 75 31 2e 69 73 49  zero, the u1.isI
23208 6e 74 65 72 72 75 70 74 65 64 0a 2a 2a 20 66 69  nterrupted.** fi
23209 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c 69 74  eld of the sqlit
2320a 65 33 20 73 74 72 75 63 74 75 72 65 20 69 73 20  e3 structure is 
2320b 73 65 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  set in order to 
2320c 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6e 74 65  simulate an inte
2320d 72 72 75 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  rrupt..**.** Thi
2320e 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 75 73  s facility is us
2320f 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70  ed for testing p
23210 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 49  urposes only.  I
23211 74 20 64 6f 65 73 20 6e 6f 74 20 66 75 6e 63 74  t does not funct
23212 69 6f 6e 0a 2a 2a 20 69 6e 20 61 6e 20 6f 72 64  ion.** in an ord
23213 69 6e 61 72 79 20 62 75 69 6c 64 2e 0a 2a 2f 0a  inary build..*/.
23214 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
23215 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
23216 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  t sqlite3_interr
23217 75 70 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  upt_count = 0;.#
23218 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
23219 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72   next global var
2321a 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65  iable is increme
2321b 6e 74 65 64 20 65 61 63 68 20 74 79 70 65 20 74  nted each type t
2321c 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64  he OP_Sort opcod
2321d 65 0a 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64  e.** is executed
2321e 2e 20 20 54 68 65 20 74 65 73 74 20 70 72 6f 63  .  The test proc
2321f 65 64 75 72 65 73 20 75 73 65 20 74 68 69 73 20  edures use this 
23220 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d  information to m
23221 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a  ake sure that.**
23222 20 73 6f 72 74 69 6e 67 20 69 73 20 6f 63 63 75   sorting is occu
23223 72 72 69 6e 67 20 6f 72 20 6e 6f 74 20 6f 63 63  rring or not occ
23224 75 72 72 69 6e 67 20 61 74 20 61 70 70 72 6f 70  urring at approp
23225 72 69 61 74 65 20 74 69 6d 65 73 2e 20 20 20 54  riate times.   T
23226 68 69 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20  his variable.** 
23227 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20  has no function 
23228 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 20 68 65  other than to he
23229 6c 70 20 76 65 72 69 66 79 20 74 68 65 20 63 6f  lp verify the co
2322a 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrect operation 
2322b 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72  of the.** librar
2322c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  y..*/.#ifdef SQL
2322d 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
2322e 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2322f 73 6f 72 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a  sort_count = 0;.
23230 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
23231 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61  e next global va
23232 72 69 61 62 6c 65 20 72 65 63 6f 72 64 73 20 74  riable records t
23233 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c  he size of the l
23234 61 72 67 65 73 74 20 4d 45 4d 5f 42 6c 6f 62 0a  argest MEM_Blob.
23235 2a 2a 20 6f 72 20 4d 45 4d 5f 53 74 72 20 74 68  ** or MEM_Str th
23236 61 74 20 68 61 73 20 62 65 65 6e 20 75 73 65 64  at has been used
23237 20 62 79 20 61 20 56 44 42 45 20 6f 70 63 6f 64   by a VDBE opcod
23238 65 2e 20 20 54 68 65 20 74 65 73 74 20 70 72 6f  e.  The test pro
23239 63 65 64 75 72 65 73 0a 2a 2a 20 75 73 65 20 74  cedures.** use t
2323a 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
2323b 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
2323c 74 20 74 68 65 20 7a 65 72 6f 2d 62 6c 6f 62 20  t the zero-blob 
2323d 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 2a 2a  functionality.**
2323e 20 69 73 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72   is working corr
2323f 65 63 74 6c 79 2e 20 20 20 54 68 69 73 20 76 61  ectly.   This va
23240 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75  riable has no fu
23241 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61  nction other tha
23242 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72  n to.** help ver
23243 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20  ify the correct 
23244 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  operation of the
23245 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66   library..*/.#if
23246 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
23247 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
23248 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73  qlite3_max_blobs
23249 69 7a 65 20 3d 20 30 3b 0a 73 74 61 74 69 63 20  ize = 0;.static 
2324a 76 6f 69 64 20 75 70 64 61 74 65 4d 61 78 42 6c  void updateMaxBl
2324b 6f 62 73 69 7a 65 28 4d 65 6d 20 2a 70 29 7b 0a  obsize(Mem *p){.
2324c 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20    if( (p->flags 
2324d 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  & (MEM_Str|MEM_B
2324e 6c 6f 62 29 29 21 3d 30 20 26 26 20 70 2d 3e 6e  lob))!=0 && p->n
2324f 3e 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f  >sqlite3_max_blo
23250 62 73 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c  bsize ){.    sql
23251 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a  ite3_max_blobsiz
23252 65 20 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a  e = p->n;.  }.}.
23253 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
23254 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61  e next global va
23255 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d  riable is increm
23256 65 6e 74 65 64 20 65 61 63 68 20 74 79 70 65 20  ented each type 
23257 74 68 65 20 4f 50 5f 46 6f 75 6e 64 20 6f 70 63  the OP_Found opc
23258 6f 64 65 0a 2a 2a 20 69 73 20 65 78 65 63 75 74  ode.** is execut
23259 65 64 2e 20 54 68 69 73 20 69 73 20 75 73 65 64  ed. This is used
2325a 20 74 6f 20 74 65 73 74 20 77 68 65 74 68 65 72   to test whether
2325b 20 6f 72 20 6e 6f 74 20 74 68 65 20 66 6f 72 65   or not the fore
2325c 69 67 6e 20 6b 65 79 0a 2a 2a 20 6f 70 65 72 61  ign key.** opera
2325d 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64  tion implemented
2325e 20 75 73 69 6e 67 20 4f 50 5f 46 6b 49 73 5a 65   using OP_FkIsZe
2325f 72 6f 20 69 73 20 77 6f 72 6b 69 6e 67 2e 20 54  ro is working. T
23260 68 69 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20  his variable.** 
23261 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20  has no function 
23262 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 20 68 65  other than to he
23263 6c 70 20 76 65 72 69 66 79 20 74 68 65 20 63 6f  lp verify the co
23264 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrect operation 
23265 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72  of the.** librar
23266 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  y..*/.#ifdef SQL
23267 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
23268 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
23269 66 6f 75 6e 64 5f 63 6f 75 6e 74 20 3d 20 30 3b  found_count = 0;
2326a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
2326b 65 73 74 20 61 20 72 65 67 69 73 74 65 72 20 74  est a register t
2326c 6f 20 73 65 65 20 69 66 20 69 74 20 65 78 63 65  o see if it exce
2326d 65 64 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  eds the current 
2326e 6d 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a  maximum blob siz
2326f 65 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73  e..** If it does
23270 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e 65 77  , record the new
23271 20 6d 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69   maximum blob si
23272 7a 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  ze..*/.#if defin
23273 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
23274 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
23275 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f  TE_OMIT_BUILTIN_
23276 54 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20 55  TEST).# define U
23277 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
23278 5a 45 28 50 29 20 20 75 70 64 61 74 65 4d 61 78  ZE(P)  updateMax
23279 42 6c 6f 62 73 69 7a 65 28 50 29 0a 23 65 6c 73  Blobsize(P).#els
2327a 65 0a 23 20 64 65 66 69 6e 65 20 55 50 44 41 54  e.# define UPDAT
2327b 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50  E_MAX_BLOBSIZE(P
2327c 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
2327d 43 6f 6e 76 65 72 74 20 74 68 65 20 67 69 76 65  Convert the give
2327e 6e 20 72 65 67 69 73 74 65 72 20 69 6e 74 6f 20  n register into 
2327f 61 20 73 74 72 69 6e 67 20 69 66 20 69 74 20 69  a string if it i
23280 73 6e 27 74 20 6f 6e 65 0a 2a 2a 20 61 6c 72 65  sn't one.** alre
23281 61 64 79 2e 20 52 65 74 75 72 6e 20 6e 6f 6e 2d  ady. Return non-
23282 7a 65 72 6f 20 69 66 20 61 20 6d 61 6c 6c 6f 63  zero if a malloc
23283 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65  () fails..*/.#de
23284 66 69 6e 65 20 53 74 72 69 6e 67 69 66 79 28 50  fine Stringify(P
23285 2c 20 65 6e 63 29 20 5c 0a 20 20 20 69 66 28 28  , enc) \.   if((
23286 28 50 29 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f  (P)->flags&(MEM_
23287 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d  Str|MEM_Blob))==
23288 30 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65  0 && sqlite3Vdbe
23289 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 50 2c 65  MemStringify(P,e
2328a 6e 63 29 29 20 5c 0a 20 20 20 20 20 7b 20 67 6f  nc)) \.     { go
2328b 74 6f 20 6e 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f 2a  to no_mem; }../*
2328c 0a 2a 2a 20 41 6e 20 65 70 68 65 6d 65 72 61 6c  .** An ephemeral
2328d 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 28 73   string value (s
2328e 69 67 6e 69 66 69 65 64 20 62 79 20 74 68 65 20  ignified by the 
2328f 4d 45 4d 5f 45 70 68 65 6d 20 66 6c 61 67 29 20  MEM_Ephem flag) 
23290 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f  contains.** a po
23291 69 6e 74 65 72 20 74 6f 20 61 20 64 79 6e 61 6d  inter to a dynam
23292 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
23293 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 73 6f   string where so
23294 6d 65 20 6f 74 68 65 72 20 65 6e 74 69 74 79 0a  me other entity.
23295 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  ** is responsibl
23296 65 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69  e for deallocati
23297 6e 67 20 74 68 61 74 20 73 74 72 69 6e 67 2e 20  ng that string. 
23298 20 42 65 63 61 75 73 65 20 74 68 65 20 72 65 67   Because the reg
23299 69 73 74 65 72 0a 2a 2a 20 64 6f 65 73 20 6e 6f  ister.** does no
2329a 74 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 73 74  t control the st
2329b 72 69 6e 67 2c 20 69 74 20 6d 69 67 68 74 20 62  ring, it might b
2329c 65 20 64 65 6c 65 74 65 64 20 77 69 74 68 6f 75  e deleted withou
2329d 74 20 74 68 65 20 72 65 67 69 73 74 65 72 0a 2a  t the register.*
2329e 2a 20 6b 6e 6f 77 69 6e 67 20 69 74 2e 0a 2a 2a  * knowing it..**
2329f 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
232a0 20 63 6f 6e 76 65 72 74 73 20 61 6e 20 65 70 68   converts an eph
232a1 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 20 69 6e  emeral string in
232a2 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  to a dynamically
232a3 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 73 74   allocated.** st
232a4 72 69 6e 67 20 74 68 61 74 20 74 68 65 20 72 65  ring that the re
232a5 67 69 73 74 65 72 20 69 74 73 65 6c 66 20 63 6f  gister itself co
232a6 6e 74 72 6f 6c 73 2e 20 20 49 6e 20 6f 74 68 65  ntrols.  In othe
232a7 72 20 77 6f 72 64 73 2c 20 69 74 0a 2a 2a 20 63  r words, it.** c
232a8 6f 6e 76 65 72 74 73 20 61 6e 20 4d 45 4d 5f 45  onverts an MEM_E
232a9 70 68 65 6d 20 73 74 72 69 6e 67 20 69 6e 74 6f  phem string into
232aa 20 61 6e 20 4d 45 4d 5f 44 79 6e 20 73 74 72 69   an MEM_Dyn stri
232ab 6e 67 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44  ng..*/.#define D
232ac 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 50 29  eephemeralize(P)
232ad 20 5c 0a 20 20 20 69 66 28 20 28 28 50 29 2d 3e   \.   if( ((P)->
232ae 66 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29  flags&MEM_Ephem)
232af 21 3d 30 20 5c 0a 20 20 20 20 20 20 20 26 26 20  !=0 \.       && 
232b0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61  sqlite3VdbeMemMa
232b1 6b 65 57 72 69 74 65 61 62 6c 65 28 50 29 20 29  keWriteable(P) )
232b2 7b 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a  { goto no_mem;}.
232b3 0a 2f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  ./* Return true 
232b4 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 77 61  if the cursor wa
232b5 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 74  s opened using t
232b6 68 65 20 4f 50 5f 4f 70 65 6e 53 6f 72 74 65 72  he OP_OpenSorter
232b7 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a 23 69 66 64   opcode. */.#ifd
232b8 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d  ef SQLITE_OMIT_M
232b9 45 52 47 45 5f 53 4f 52 54 0a 23 20 64 65 66 69  ERGE_SORT.# defi
232ba 6e 65 20 69 73 53 6f 72 74 65 72 28 78 29 20 30  ne isSorter(x) 0
232bb 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
232bc 69 73 53 6f 72 74 65 72 28 78 29 20 28 28 78 29  isSorter(x) ((x)
232bd 2d 3e 70 53 6f 72 74 65 72 21 3d 30 29 0a 23 65  ->pSorter!=0).#e
232be 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75  ndif../*.** Argu
232bf 6d 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 73  ment pMem points
232c0 20 61 74 20 61 20 72 65 67 69 73 74 65 72 20 74   at a register t
232c1 68 61 74 20 77 69 6c 6c 20 62 65 20 70 61 73 73  hat will be pass
232c2 65 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 2d  ed to a.** user-
232c3 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
232c4 20 6f 72 20 72 65 74 75 72 6e 65 64 20 74 6f 20   or returned to 
232c5 74 68 65 20 75 73 65 72 20 61 73 20 74 68 65 20  the user as the 
232c6 72 65 73 75 6c 74 20 6f 66 20 61 20 71 75 65 72  result of a quer
232c7 79 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  y..** This routi
232c8 6e 65 20 73 65 74 73 20 74 68 65 20 70 4d 65 6d  ne sets the pMem
232c9 2d 3e 74 79 70 65 20 76 61 72 69 61 62 6c 65 20  ->type variable 
232ca 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c 69  used by the sqli
232cb 74 65 33 5f 76 61 6c 75 65 5f 2a 28 29 20 0a 2a  te3_value_*() .*
232cc 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 53  * routines..*/.S
232cd 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
232ce 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
232cf 6d 53 74 6f 72 65 54 79 70 65 28 4d 65 6d 20 2a  mStoreType(Mem *
232d0 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61  pMem){.  int fla
232d1 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  gs = pMem->flags
232d2 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  ;.  if( flags & 
232d3 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
232d4 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
232d5 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20  ITE_NULL;.  }.  
232d6 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26  else if( flags &
232d7 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
232d8 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
232d9 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d  ITE_INTEGER;.  }
232da 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67  .  else if( flag
232db 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a  s & MEM_Real ){.
232dc 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
232dd 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20   SQLITE_FLOAT;. 
232de 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c   }.  else if( fl
232df 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b  ags & MEM_Str ){
232e0 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20  .    pMem->type 
232e1 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20  = SQLITE_TEXT;. 
232e2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d   }else{.    pMem
232e3 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
232e4 42 4c 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  BLOB;.  }.}../*.
232e5 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64 62 65  ** Allocate Vdbe
232e6 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43  Cursor number iC
232e7 75 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f  ur.  Return a po
232e8 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 52 65  inter to it.  Re
232e9 74 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20  turn NULL.** if 
232ea 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65  we run out of me
232eb 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  mory..*/.static 
232ec 56 64 62 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f  VdbeCursor *allo
232ed 63 61 74 65 43 75 72 73 6f 72 28 0a 20 20 56 64  cateCursor(.  Vd
232ee 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  be *p,          
232ef 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75      /* The virtu
232f0 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20  al machine */.  
232f1 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20  int iCur,       
232f2 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
232f3 66 20 74 68 65 20 6e 65 77 20 56 64 62 65 43 75  f the new VdbeCu
232f4 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 46  rsor */.  int nF
232f5 69 65 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20  ield,           
232f6 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65  /* Number of fie
232f7 6c 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  lds in the table
232f8 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69   or index */.  i
232f9 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20  nt iDb,         
232fa 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
232fb 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 6c 6f   the cursor belo
232fc 6e 67 73 20 74 6f 2c 20 6f 72 20 2d 31 20 2a 2f  ngs to, or -1 */
232fd 0a 20 20 69 6e 74 20 69 73 42 74 72 65 65 43 75  .  int isBtreeCu
232fe 72 73 6f 72 20 20 20 20 20 2f 2a 20 54 72 75 65  rsor     /* True
232ff 20 66 6f 72 20 42 2d 54 72 65 65 2e 20 20 46 61   for B-Tree.  Fa
23300 6c 73 65 20 66 6f 72 20 70 73 65 75 64 6f 2d 74  lse for pseudo-t
23301 61 62 6c 65 20 6f 72 20 76 74 61 62 20 2a 2f 0a  able or vtab */.
23302 29 7b 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65  ){.  /* Find the
23303 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61   memory cell tha
23304 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74  t will be used t
23305 6f 20 73 74 6f 72 65 20 74 68 65 20 62 6c 6f 62  o store the blob
23306 20 6f 66 20 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20   of memory.  ** 
23307 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69  required for thi
23308 73 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 72  s VdbeCursor str
23309 75 63 74 75 72 65 2e 20 49 74 20 69 73 20 63 6f  ucture. It is co
2330a 6e 76 65 6e 69 65 6e 74 20 74 6f 20 75 73 65 20  nvenient to use 
2330b 61 20 0a 20 20 2a 2a 20 76 64 62 65 20 6d 65 6d  a .  ** vdbe mem
2330c 6f 72 79 20 63 65 6c 6c 20 74 6f 20 6d 61 6e 61  ory cell to mana
2330d 67 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ge the memory al
2330e 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72 65  location require
2330f 64 20 66 6f 72 20 61 0a 20 20 2a 2a 20 56 64 62  d for a.  ** Vdb
23310 65 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72  eCursor structur
23311 65 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77  e for the follow
23312 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 20 20 2a  ing reasons:.  *
23313 2a 0a 20 20 2a 2a 20 20 20 2a 20 53 6f 6d 65 74  *.  **   * Somet
23314 69 6d 65 73 20 63 75 72 73 6f 72 20 6e 75 6d 62  imes cursor numb
23315 65 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72  ers are used for
23316 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 69 66   a couple of dif
23317 66 65 72 65 6e 74 0a 20 20 2a 2a 20 20 20 20 20  ferent.  **     
23318 70 75 72 70 6f 73 65 73 20 69 6e 20 61 20 76 64  purposes in a vd
23319 62 65 20 70 72 6f 67 72 61 6d 2e 20 54 68 65 20  be program. The 
2331a 64 69 66 66 65 72 65 6e 74 20 75 73 65 73 20 6d  different uses m
2331b 69 67 68 74 20 72 65 71 75 69 72 65 0a 20 20 2a  ight require.  *
2331c 2a 20 20 20 20 20 64 69 66 66 65 72 65 6e 74 20  *     different 
2331d 73 69 7a 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e  sized allocation
2331e 73 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  s. Memory cells 
2331f 70 72 6f 76 69 64 65 20 67 72 6f 77 61 62 6c 65  provide growable
23320 0a 20 20 2a 2a 20 20 20 20 20 61 6c 6c 6f 63 61  .  **     alloca
23321 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
23322 20 20 20 2a 20 57 68 65 6e 20 75 73 69 6e 67 20     * When using 
23323 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
23324 4e 41 47 45 4d 45 4e 54 2c 20 6d 65 6d 6f 72 79  NAGEMENT, memory
23325 20 63 65 6c 6c 20 62 75 66 66 65 72 73 20 63 61   cell buffers ca
23326 6e 0a 20 20 2a 2a 20 20 20 20 20 62 65 20 66 72  n.  **     be fr
23327 65 65 64 20 6c 61 7a 69 6c 79 20 76 69 61 20 74  eed lazily via t
23328 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  he sqlite3_relea
23329 73 65 5f 6d 65 6d 6f 72 79 28 29 20 41 50 49 2e  se_memory() API.
2332a 20 54 68 69 73 0a 20 20 2a 2a 20 20 20 20 20 6d   This.  **     m
2332b 69 6e 69 6d 69 7a 65 73 20 74 68 65 20 6e 75 6d  inimizes the num
2332c 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 63 61  ber of malloc ca
2332d 6c 6c 73 20 6d 61 64 65 20 62 79 20 74 68 65 20  lls made by the 
2332e 73 79 73 74 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a  system..  **.  *
2332f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66  * Memory cells f
23330 6f 72 20 63 75 72 73 6f 72 73 20 61 72 65 20 61  or cursors are a
23331 6c 6c 6f 63 61 74 65 64 20 61 74 20 74 68 65 20  llocated at the 
23332 74 6f 70 20 6f 66 20 74 68 65 20 61 64 64 72 65  top of the addre
23333 73 73 0a 20 20 2a 2a 20 73 70 61 63 65 2e 20 4d  ss.  ** space. M
23334 65 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e  emory cell (p->n
23335 4d 65 6d 29 20 63 6f 72 72 65 73 70 6f 6e 64 73  Mem) corresponds
23336 20 74 6f 20 63 75 72 73 6f 72 20 30 2e 20 53 70   to cursor 0. Sp
23337 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20 63 75 72  ace for.  ** cur
23338 73 6f 72 20 31 20 69 73 20 6d 61 6e 61 67 65 64  sor 1 is managed
23339 20 62 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20   by memory cell 
2333a 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c 20 65 74 63  (p->nMem-1), etc
2333b 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d  ..  */.  Mem *pM
2333c 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 2d  em = &p->aMem[p-
2333d 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b 0a 0a 20 20  >nMem-iCur];..  
2333e 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 56 64 62  int nByte;.  Vdb
2333f 65 43 75 72 73 6f 72 20 2a 70 43 78 20 3d 20 30  eCursor *pCx = 0
23340 3b 0a 20 20 6e 42 79 74 65 20 3d 20 0a 20 20 20  ;.  nByte = .   
23341 20 20 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66     ROUND8(sizeof
23342 28 56 64 62 65 43 75 72 73 6f 72 29 29 20 2b 20  (VdbeCursor)) + 
23343 0a 20 20 20 20 20 20 28 69 73 42 74 72 65 65 43  .      (isBtreeC
23344 75 72 73 6f 72 3f 73 71 6c 69 74 65 33 42 74 72  ursor?sqlite3Btr
23345 65 65 43 75 72 73 6f 72 53 69 7a 65 28 29 3a 30  eeCursorSize():0
23346 29 20 2b 20 0a 20 20 20 20 20 20 32 2a 6e 46 69  ) + .      2*nFi
23347 65 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32 29 3b  eld*sizeof(u32);
23348 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 75 72  ..  assert( iCur
23349 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
2334a 20 69 66 28 20 70 2d 3e 61 70 43 73 72 5b 69 43   if( p->apCsr[iC
2334b 75 72 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ur] ){.    sqlit
2334c 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72  e3VdbeFreeCursor
2334d 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 43 75  (p, p->apCsr[iCu
2334e 72 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 70 43 73  r]);.    p->apCs
2334f 72 5b 69 43 75 72 5d 20 3d 20 30 3b 0a 20 20 7d  r[iCur] = 0;.  }
23350 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
23351 3d 3d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ==sqlite3VdbeMem
23352 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65  Grow(pMem, nByte
23353 2c 20 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 61  , 0) ){.    p->a
23354 70 43 73 72 5b 69 43 75 72 5d 20 3d 20 70 43 78  pCsr[iCur] = pCx
23355 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72 2a 29   = (VdbeCursor*)
23356 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 6d 65 6d  pMem->z;.    mem
23357 73 65 74 28 70 43 78 2c 20 30 2c 20 73 69 7a 65  set(pCx, 0, size
23358 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 29 3b  of(VdbeCursor));
23359 0a 20 20 20 20 70 43 78 2d 3e 69 44 62 20 3d 20  .    pCx->iDb = 
2335a 69 44 62 3b 0a 20 20 20 20 70 43 78 2d 3e 6e 46  iDb;.    pCx->nF
2335b 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 3b 0a 20  ield = nField;. 
2335c 20 20 20 69 66 28 20 6e 46 69 65 6c 64 20 29 7b     if( nField ){
2335d 0a 20 20 20 20 20 20 70 43 78 2d 3e 61 54 79 70  .      pCx->aTyp
2335e 65 20 3d 20 28 75 33 32 20 2a 29 26 70 4d 65 6d  e = (u32 *)&pMem
2335f 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f  ->z[ROUND8(sizeo
23360 66 28 56 64 62 65 43 75 72 73 6f 72 29 29 5d 3b  f(VdbeCursor))];
23361 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
23362 73 42 74 72 65 65 43 75 72 73 6f 72 20 29 7b 0a  sBtreeCursor ){.
23363 20 20 20 20 20 20 70 43 78 2d 3e 70 43 75 72 73        pCx->pCurs
23364 6f 72 20 3d 20 28 42 74 43 75 72 73 6f 72 2a 29  or = (BtCursor*)
23365 0a 20 20 20 20 20 20 20 20 20 20 26 70 4d 65 6d  .          &pMem
23366 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f  ->z[ROUND8(sizeo
23367 66 28 56 64 62 65 43 75 72 73 6f 72 29 29 2b 32  f(VdbeCursor))+2
23368 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75  *nField*sizeof(u
23369 33 32 29 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69  32)];.      sqli
2336a 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a 65  te3BtreeCursorZe
2336b 72 6f 28 70 43 78 2d 3e 70 43 75 72 73 6f 72 29  ro(pCx->pCursor)
2336c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
2336d 74 75 72 6e 20 70 43 78 3b 0a 7d 0a 0a 2f 2a 0a  turn pCx;.}../*.
2336e 2a 2a 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72  ** Try to conver
2336f 74 20 61 20 76 61 6c 75 65 20 69 6e 74 6f 20 61  t a value into a
23370 20 6e 75 6d 65 72 69 63 20 72 65 70 72 65 73 65   numeric represe
23371 6e 74 61 74 69 6f 6e 20 69 66 20 77 65 20 63 61  ntation if we ca
23372 6e 0a 2a 2a 20 64 6f 20 73 6f 20 77 69 74 68 6f  n.** do so witho
23373 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72  ut loss of infor
23374 6d 61 74 69 6f 6e 2e 20 20 49 6e 20 6f 74 68 65  mation.  In othe
23375 72 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 20  r words, if the 
23376 73 74 72 69 6e 67 0a 2a 2a 20 6c 6f 6f 6b 73 20  string.** looks 
23377 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20 63  like a number, c
23378 6f 6e 76 65 72 74 20 69 74 20 69 6e 74 6f 20 61  onvert it into a
23379 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74 20   number.  If it 
2337a 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 6f 6b  does not.** look
2337b 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20   like a number, 
2337c 6c 65 61 76 65 20 69 74 20 61 6c 6f 6e 65 2e 0a  leave it alone..
2337d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
2337e 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e  pplyNumericAffin
2337f 69 74 79 28 4d 65 6d 20 2a 70 52 65 63 29 7b 0a  ity(Mem *pRec){.
23380 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c 61    if( (pRec->fla
23381 67 73 20 26 20 28 4d 45 4d 5f 52 65 61 6c 7c 4d  gs & (MEM_Real|M
23382 45 4d 5f 49 6e 74 29 29 3d 3d 30 20 29 7b 0a 20  EM_Int))==0 ){. 
23383 20 20 20 64 6f 75 62 6c 65 20 72 56 61 6c 75 65     double rValue
23384 3b 0a 20 20 20 20 69 36 34 20 69 56 61 6c 75 65  ;.    i64 iValue
23385 3b 0a 20 20 20 20 75 38 20 65 6e 63 20 3d 20 70  ;.    u8 enc = p
23386 52 65 63 2d 3e 65 6e 63 3b 0a 20 20 20 20 69 66  Rec->enc;.    if
23387 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d  ( (pRec->flags&M
23388 45 4d 5f 53 74 72 29 3d 3d 30 20 29 20 72 65 74  EM_Str)==0 ) ret
23389 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  urn;.    if( sql
2338a 69 74 65 33 41 74 6f 46 28 70 52 65 63 2d 3e 7a  ite3AtoF(pRec->z
2338b 2c 20 26 72 56 61 6c 75 65 2c 20 70 52 65 63 2d  , &rValue, pRec-
2338c 3e 6e 2c 20 65 6e 63 29 3d 3d 30 20 29 20 72 65  >n, enc)==0 ) re
2338d 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 30 3d  turn;.    if( 0=
2338e 3d 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70  =sqlite3Atoi64(p
2338f 52 65 63 2d 3e 7a 2c 20 26 69 56 61 6c 75 65 2c  Rec->z, &iValue,
23390 20 70 52 65 63 2d 3e 6e 2c 20 65 6e 63 29 20 29   pRec->n, enc) )
23391 7b 0a 20 20 20 20 20 20 70 52 65 63 2d 3e 75 2e  {.      pRec->u.
23392 69 20 3d 20 69 56 61 6c 75 65 3b 0a 20 20 20 20  i = iValue;.    
23393 20 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 7c 3d    pRec->flags |=
23394 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 7d 65   MEM_Int;.    }e
23395 6c 73 65 7b 0a 20 20 20 20 20 20 70 52 65 63 2d  lse{.      pRec-
23396 3e 72 20 3d 20 72 56 61 6c 75 65 3b 0a 20 20 20  >r = rValue;.   
23397 20 20 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 7c     pRec->flags |
23398 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 20 20  = MEM_Real;.    
23399 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  }.  }.}../*.** P
2339a 72 6f 63 65 73 73 69 6e 67 20 69 73 20 64 65 74  rocessing is det
2339b 65 72 6d 69 6e 65 20 62 79 20 74 68 65 20 61 66  ermine by the af
2339c 66 69 6e 69 74 79 20 70 61 72 61 6d 65 74 65 72  finity parameter
2339d 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41  :.**.** SQLITE_A
2339e 46 46 5f 49 4e 54 45 47 45 52 3a 0a 2a 2a 20 53  FF_INTEGER:.** S
2339f 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3a 0a  QLITE_AFF_REAL:.
233a0 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  ** SQLITE_AFF_NU
233a1 4d 45 52 49 43 3a 0a 2a 2a 20 20 20 20 54 72 79  MERIC:.**    Try
233a2 20 74 6f 20 63 6f 6e 76 65 72 74 20 70 52 65 63   to convert pRec
233a3 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 72   to an integer r
233a4 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 72  epresentation or
233a5 20 61 20 0a 2a 2a 20 20 20 20 66 6c 6f 61 74 69   a .**    floati
233a6 6e 67 2d 70 6f 69 6e 74 20 72 65 70 72 65 73 65  ng-point represe
233a7 6e 74 61 74 69 6f 6e 20 69 66 20 61 6e 20 69 6e  ntation if an in
233a8 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61  teger representa
233a9 74 69 6f 6e 0a 2a 2a 20 20 20 20 69 73 20 6e 6f  tion.**    is no
233aa 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 4e 6f 74  t possible.  Not
233ab 65 20 74 68 61 74 20 74 68 65 20 69 6e 74 65 67  e that the integ
233ac 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  er representatio
233ad 6e 20 69 73 0a 2a 2a 20 20 20 20 61 6c 77 61 79  n is.**    alway
233ae 73 20 70 72 65 66 65 72 72 65 64 2c 20 65 76 65  s preferred, eve
233af 6e 20 69 66 20 74 68 65 20 61 66 66 69 6e 69 74  n if the affinit
233b0 79 20 69 73 20 52 45 41 4c 2c 20 62 65 63 61 75  y is REAL, becau
233b1 73 65 0a 2a 2a 20 20 20 20 61 6e 20 69 6e 74 65  se.**    an inte
233b2 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69  ger representati
233b3 6f 6e 20 69 73 20 6d 6f 72 65 20 73 70 61 63 65  on is more space
233b4 20 65 66 66 69 63 69 65 6e 74 20 6f 6e 20 64 69   efficient on di
233b5 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  sk..**.** SQLITE
233b6 5f 41 46 46 5f 54 45 58 54 3a 0a 2a 2a 20 20 20  _AFF_TEXT:.**   
233b7 20 43 6f 6e 76 65 72 74 20 70 52 65 63 20 74 6f   Convert pRec to
233b8 20 61 20 74 65 78 74 20 72 65 70 72 65 73 65 6e   a text represen
233b9 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51  tation..**.** SQ
233ba 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 2a  LITE_AFF_NONE:.*
233bb 2a 20 20 20 20 4e 6f 2d 6f 70 2e 20 20 70 52 65  *    No-op.  pRe
233bc 63 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a  c is unchanged..
233bd 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
233be 70 70 6c 79 41 66 66 69 6e 69 74 79 28 0a 20 20  pplyAffinity(.  
233bf 4d 65 6d 20 2a 70 52 65 63 2c 20 20 20 20 20 20  Mem *pRec,      
233c0 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65      /* The value
233c1 20 74 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69   to apply affini
233c2 74 79 20 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20  ty to */.  char 
233c3 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 2f  affinity,      /
233c4 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 74  * The affinity t
233c5 6f 20 62 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a  o be applied */.
233c6 20 20 75 38 20 65 6e 63 20 20 20 20 20 20 20 20    u8 enc        
233c7 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 69        /* Use thi
233c8 73 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  s text encoding 
233c9 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 61 66 66 69  */.){.  if( affi
233ca 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46  nity==SQLITE_AFF
233cb 5f 54 45 58 54 20 29 7b 0a 20 20 20 20 2f 2a 20  _TEXT ){.    /* 
233cc 4f 6e 6c 79 20 61 74 74 65 6d 70 74 20 74 68 65  Only attempt the
233cd 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f 20 54   conversion to T
233ce 45 58 54 20 69 66 20 74 68 65 72 65 20 69 73 20  EXT if there is 
233cf 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 72 65  an integer or re
233d0 61 6c 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73  al.    ** repres
233d1 65 6e 74 61 74 69 6f 6e 20 28 62 6c 6f 62 20 61  entation (blob a
233d2 6e 64 20 4e 55 4c 4c 20 64 6f 20 6e 6f 74 20 67  nd NULL do not g
233d3 65 74 20 63 6f 6e 76 65 72 74 65 64 29 20 62 75  et converted) bu
233d4 74 20 6e 6f 20 73 74 72 69 6e 67 0a 20 20 20 20  t no string.    
233d5 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ** representatio
233d6 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  n..    */.    if
233d7 28 20 30 3d 3d 28 70 52 65 63 2d 3e 66 6c 61 67  ( 0==(pRec->flag
233d8 73 26 4d 45 4d 5f 53 74 72 29 20 26 26 20 28 70  s&MEM_Str) && (p
233d9 52 65 63 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f  Rec->flags&(MEM_
233da 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 20 29  Real|MEM_Int)) )
233db 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
233dc 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28  dbeMemStringify(
233dd 70 52 65 63 2c 20 65 6e 63 29 3b 0a 20 20 20 20  pRec, enc);.    
233de 7d 0a 20 20 20 20 70 52 65 63 2d 3e 66 6c 61 67  }.    pRec->flag
233df 73 20 26 3d 20 7e 28 4d 45 4d 5f 52 65 61 6c 7c  s &= ~(MEM_Real|
233e0 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73  MEM_Int);.  }els
233e1 65 20 69 66 28 20 61 66 66 69 6e 69 74 79 21 3d  e if( affinity!=
233e2 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20  SQLITE_AFF_NONE 
233e3 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 61  ){.    assert( a
233e4 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
233e5 41 46 46 5f 49 4e 54 45 47 45 52 20 7c 7c 20 61  AFF_INTEGER || a
233e6 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
233e7 41 46 46 5f 52 45 41 4c 0a 20 20 20 20 20 20 20  AFF_REAL.       
233e8 20 20 20 20 20 20 7c 7c 20 61 66 66 69 6e 69 74        || affinit
233e9 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  y==SQLITE_AFF_NU
233ea 4d 45 52 49 43 20 29 3b 0a 20 20 20 20 61 70 70  MERIC );.    app
233eb 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74  lyNumericAffinit
233ec 79 28 70 52 65 63 29 3b 0a 20 20 20 20 69 66 28  y(pRec);.    if(
233ed 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d   pRec->flags & M
233ee 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 20  EM_Real ){.     
233ef 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65   sqlite3VdbeInte
233f0 67 65 72 41 66 66 69 6e 69 74 79 28 70 52 65 63  gerAffinity(pRec
233f1 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
233f2 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f 6e  /*.** Try to con
233f3 76 65 72 74 20 74 68 65 20 74 79 70 65 20 6f 66  vert the type of
233f4 20 61 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75   a function argu
233f5 6d 65 6e 74 20 6f 72 20 61 20 72 65 73 75 6c 74  ment or a result
233f6 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 74 6f 20   column.** into 
233f7 61 20 6e 75 6d 65 72 69 63 20 72 65 70 72 65 73  a numeric repres
233f8 65 6e 74 61 74 69 6f 6e 2e 20 20 55 73 65 20 65  entation.  Use e
233f9 69 74 68 65 72 20 49 4e 54 45 47 45 52 20 6f 72  ither INTEGER or
233fa 20 52 45 41 4c 20 77 68 69 63 68 65 76 65 72 0a   REAL whichever.
233fb 2a 2a 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  ** is appropriat
233fc 65 2e 20 20 42 75 74 20 6f 6e 6c 79 20 64 6f 20  e.  But only do 
233fd 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  the conversion i
233fe 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65  f it is possible
233ff 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6c 6f 73 73   without.** loss
23400 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20   of information 
23401 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 72  and return the r
23402 65 76 69 73 65 64 20 74 79 70 65 20 6f 66 20 74  evised type of t
23403 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  he argument..*/.
23404 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
23405 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d  qlite3_value_num
23406 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 65  eric_type(sqlite
23407 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a  3_value *pVal){.
23408 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 28 4d    Mem *pMem = (M
23409 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20  em*)pVal;.  if( 
2340a 70 4d 65 6d 2d 3e 74 79 70 65 3d 3d 53 51 4c 49  pMem->type==SQLI
2340b 54 45 5f 54 45 58 54 20 29 7b 0a 20 20 20 20 61  TE_TEXT ){.    a
2340c 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e  pplyNumericAffin
2340d 69 74 79 28 70 4d 65 6d 29 3b 0a 20 20 20 20 73  ity(pMem);.    s
2340e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f  qlite3VdbeMemSto
2340f 72 65 54 79 70 65 28 70 4d 65 6d 29 3b 0a 20 20  reType(pMem);.  
23410 7d 0a 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d  }.  return pMem-
23411 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  >type;.}../*.** 
23412 45 78 70 6f 72 74 65 64 20 76 65 72 73 69 6f 6e  Exported version
23413 20 6f 66 20 61 70 70 6c 79 41 66 66 69 6e 69 74   of applyAffinit
23414 79 28 29 2e 20 54 68 69 73 20 6f 6e 65 20 77 6f  y(). This one wo
23415 72 6b 73 20 6f 6e 20 73 71 6c 69 74 65 33 5f 76  rks on sqlite3_v
23416 61 6c 75 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74  alue*, .** not t
23417 68 65 20 69 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a  he internal Mem*
23418 20 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   type..*/.SQLITE
23419 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2341a 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41  lite3ValueApplyA
2341b 66 66 69 6e 69 74 79 28 0a 20 20 73 71 6c 69 74  ffinity(.  sqlit
2341c 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20  e3_value *pVal, 
2341d 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20  .  u8 affinity, 
2341e 0a 20 20 75 38 20 65 6e 63 0a 29 7b 0a 20 20 61  .  u8 enc.){.  a
2341f 70 70 6c 79 41 66 66 69 6e 69 74 79 28 28 4d 65  pplyAffinity((Me
23420 6d 20 2a 29 70 56 61 6c 2c 20 61 66 66 69 6e 69  m *)pVal, affini
23421 74 79 2c 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66  ty, enc);.}..#if
23422 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
23423 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e  ./*.** Write a n
23424 69 63 65 20 73 74 72 69 6e 67 20 72 65 70 72 65  ice string repre
23425 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  sentation of the
23426 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c   contents of cel
23427 6c 20 70 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62  l pMem.** into b
23428 75 66 66 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67  uffer zBuf, leng
23429 74 68 20 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49  th nBuf..*/.SQLI
2342a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2342b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72  sqlite3VdbeMemPr
2342c 65 74 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70  ettyPrint(Mem *p
2342d 4d 65 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 29  Mem, char *zBuf)
2342e 7b 0a 20 20 63 68 61 72 20 2a 7a 43 73 72 20 3d  {.  char *zCsr =
2342f 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d   zBuf;.  int f =
23430 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20   pMem->flags;.. 
23431 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
23432 61 72 20 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d  ar *const encnam
23433 65 73 5b 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22  es[] = {"(X)", "
23434 28 38 29 22 2c 20 22 28 31 36 4c 45 29 22 2c 20  (8)", "(16LE)", 
23435 22 28 31 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66  "(16BE)"};..  if
23436 28 20 66 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a  ( f&MEM_Blob ){.
23437 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63      int i;.    c
23438 68 61 72 20 63 3b 0a 20 20 20 20 69 66 28 20 66  har c;.    if( f
23439 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20   & MEM_Dyn ){.  
2343a 20 20 20 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20      c = 'z';.   
2343b 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20     assert( (f & 
2343c 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f  (MEM_Static|MEM_
2343d 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20  Ephem))==0 );.  
2343e 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20    }else if( f & 
2343f 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20  MEM_Static ){.  
23440 20 20 20 20 63 20 3d 20 27 74 27 3b 0a 20 20 20      c = 't';.   
23441 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20     assert( (f & 
23442 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68  (MEM_Dyn|MEM_Eph
23443 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d  em))==0 );.    }
23444 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d  else if( f & MEM
23445 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20  _Ephem ){.      
23446 63 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 61  c = 'e';.      a
23447 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d  ssert( (f & (MEM
23448 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29  _Static|MEM_Dyn)
23449 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73  )==0 );.    }els
2344a 65 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 73 27  e{.      c = 's'
2344b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c  ;.    }..    sql
2344c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30  ite3_snprintf(10
2344d 30 2c 20 7a 43 73 72 2c 20 22 25 63 22 2c 20 63  0, zCsr, "%c", c
2344e 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73  );.    zCsr += s
2344f 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
23450 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Csr);.    sqlite
23451 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20  3_snprintf(100, 
23452 7a 43 73 72 2c 20 22 25 64 5b 22 2c 20 70 4d 65  zCsr, "%d[", pMe
23453 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7a 43 73 72 20  m->n);.    zCsr 
23454 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
23455 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 66 6f  30(zCsr);.    fo
23456 72 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69  r(i=0; i<16 && i
23457 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a  <pMem->n; i++){.
23458 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
23459 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72  printf(100, zCsr
2345a 2c 20 22 25 30 32 58 22 2c 20 28 28 69 6e 74 29  , "%02X", ((int)
2345b 70 4d 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78 46  pMem->z[i] & 0xF
2345c 46 29 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 20  F));.      zCsr 
2345d 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
2345e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a  30(zCsr);.    }.
2345f 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31      for(i=0; i<1
23460 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20  6 && i<pMem->n; 
23461 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72  i++){.      char
23462 20 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b   z = pMem->z[i];
23463 0a 20 20 20 20 20 20 69 66 28 20 7a 3c 33 32 20  .      if( z<32 
23464 7c 7c 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73 72  || z>126 ) *zCsr
23465 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20  ++ = '.';.      
23466 65 6c 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a  else *zCsr++ = z
23467 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c  ;.    }..    sql
23468 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30  ite3_snprintf(10
23469 30 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20  0, zCsr, "]%s", 
2346a 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65  encnames[pMem->e
2346b 6e 63 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b  nc]);.    zCsr +
2346c 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
2346d 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28  0(zCsr);.    if(
2346e 20 66 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b   f & MEM_Zero ){
2346f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
23470 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73  nprintf(100, zCs
23471 72 2c 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d 3e 75  r,"+%dz",pMem->u
23472 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7a  .nZero);.      z
23473 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74  Csr += sqlite3St
23474 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20  rlen30(zCsr);.  
23475 20 20 7d 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20    }.    *zCsr = 
23476 27 5c 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66  '\0';.  }else if
23477 28 20 66 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b  ( f & MEM_Str ){
23478 0a 20 20 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20  .    int j, k;. 
23479 20 20 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27     zBuf[0] = ' '
2347a 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45  ;.    if( f & ME
2347b 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a  M_Dyn ){.      z
2347c 42 75 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20  Buf[1] = 'z';.  
2347d 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26      assert( (f &
2347e 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d   (MEM_Static|MEM
2347f 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20  _Ephem))==0 );. 
23480 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26     }else if( f &
23481 20 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20   MEM_Static ){. 
23482 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27       zBuf[1] = '
23483 74 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  t';.      assert
23484 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c  ( (f & (MEM_Dyn|
23485 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29  MEM_Ephem))==0 )
23486 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
23487 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b  f & MEM_Ephem ){
23488 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d  .      zBuf[1] =
23489 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65   'e';.      asse
2348a 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74  rt( (f & (MEM_St
2348b 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d  atic|MEM_Dyn))==
2348c 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  0 );.    }else{.
2348d 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20        zBuf[1] = 
2348e 27 73 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b  's';.    }.    k
2348f 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 2;.    sqlite
23490 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20  3_snprintf(100, 
23491 26 7a 42 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20  &zBuf[k], "%d", 
23492 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20  pMem->n);.    k 
23493 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
23494 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20  30(&zBuf[k]);.  
23495 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b    zBuf[k++] = '[
23496 27 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  ';.    for(j=0; 
23497 6a 3c 31 35 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e  j<15 && j<pMem->
23498 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 75  n; j++){.      u
23499 38 20 63 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d  8 c = pMem->z[j]
2349a 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3e 3d 30  ;.      if( c>=0
2349b 78 32 30 20 26 26 20 63 3c 30 78 37 66 20 29 7b  x20 && c<0x7f ){
2349c 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b  .        zBuf[k+
2349d 2b 5d 20 3d 20 63 3b 0a 20 20 20 20 20 20 7d 65  +] = c;.      }e
2349e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 42 75  lse{.        zBu
2349f 66 5b 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20  f[k++] = '.';.  
234a0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
234a1 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b  zBuf[k++] = ']';
234a2 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
234a3 72 69 6e 74 66 28 31 30 30 2c 26 7a 42 75 66 5b  rintf(100,&zBuf[
234a4 6b 5d 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65  k], encnames[pMe
234a5 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 6b 20  m->enc]);.    k 
234a6 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
234a7 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20  30(&zBuf[k]);.  
234a8 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b    zBuf[k++] = 0;
234a9 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  .  }.}.#endif..#
234aa 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
234ab 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74  UG./*.** Print t
234ac 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 72 65  he value of a re
234ad 67 69 73 74 65 72 20 66 6f 72 20 74 72 61 63 69  gister for traci
234ae 6e 67 20 70 75 72 70 6f 73 65 73 3a 0a 2a 2f 0a  ng purposes:.*/.
234af 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 54  static void memT
234b0 72 61 63 65 50 72 69 6e 74 28 46 49 4c 45 20 2a  racePrint(FILE *
234b1 6f 75 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20  out, Mem *p){.  
234b2 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d  if( p->flags & M
234b3 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 66  EM_Null ){.    f
234b4 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55  printf(out, " NU
234b5 4c 4c 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  LL");.  }else if
234b6 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d  ( (p->flags & (M
234b7 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29  EM_Int|MEM_Str))
234b8 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53  ==(MEM_Int|MEM_S
234b9 74 72 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  tr) ){.    fprin
234ba 74 66 28 6f 75 74 2c 20 22 20 73 69 3a 25 6c 6c  tf(out, " si:%ll
234bb 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d  d", p->u.i);.  }
234bc 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67  else if( p->flag
234bd 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  s & MEM_Int ){. 
234be 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
234bf 22 20 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e  " i:%lld", p->u.
234c0 69 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  i);.#ifndef SQLI
234c1 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
234c2 5f 50 4f 49 4e 54 0a 20 20 7d 65 6c 73 65 20 69  _POINT.  }else i
234c3 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45  f( p->flags & ME
234c4 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 66 70  M_Real ){.    fp
234c5 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 72 3a 25  rintf(out, " r:%
234c6 67 22 2c 20 70 2d 3e 72 29 3b 0a 23 65 6e 64 69  g", p->r);.#endi
234c7 66 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  f.  }else if( p-
234c8 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
234c9 53 65 74 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  Set ){.    fprin
234ca 74 66 28 6f 75 74 2c 20 22 20 28 72 6f 77 73 65  tf(out, " (rowse
234cb 74 29 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  t)");.  }else{. 
234cc 20 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 30     char zBuf[200
234cd 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ];.    sqlite3Vd
234ce 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74  beMemPrettyPrint
234cf 28 70 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66  (p, zBuf);.    f
234d0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 22 29  printf(out, " ")
234d1 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  ;.    fprintf(ou
234d2 74 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a  t, "%s", zBuf);.
234d3 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
234d4 64 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28  d registerTrace(
234d5 46 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74 20 69  FILE *out, int i
234d6 52 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20  Reg, Mem *p){.  
234d7 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 52 45  fprintf(out, "RE
234d8 47 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65 67 29  G[%d] = ", iReg)
234d9 3b 0a 20 20 6d 65 6d 54 72 61 63 65 50 72 69 6e  ;.  memTracePrin
234da 74 28 6f 75 74 2c 20 70 29 3b 0a 20 20 66 70 72  t(out, p);.  fpr
234db 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b  intf(out, "\n");
234dc 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65  .}.#endif..#ifde
234dd 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23  f SQLITE_DEBUG.#
234de 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 45    define REGISTE
234df 52 5f 54 52 41 43 45 28 52 2c 4d 29 20 69 66 28  R_TRACE(R,M) if(
234e0 70 2d 3e 74 72 61 63 65 29 72 65 67 69 73 74 65  p->trace)registe
234e1 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c  rTrace(p->trace,
234e2 52 2c 4d 29 0a 23 65 6c 73 65 0a 23 20 20 64 65  R,M).#else.#  de
234e3 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 52  fine REGISTER_TR
234e4 41 43 45 28 52 2c 4d 29 0a 23 65 6e 64 69 66 0a  ACE(R,M).#endif.
234e5 0a 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52  ..#ifdef VDBE_PR
234e6 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77  OFILE../* .** hw
234e7 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20  time.h contains 
234e8 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72  inline assembler
234e9 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d   code for implem
234ea 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d  enting .** high-
234eb 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69  performance timi
234ec 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a  ng routines..*/.
234ed 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
234ee 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68  Include hwtime.h
234ef 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
234f0 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f vdbe.c *******
234f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
234f2 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
234f3 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d  Begin file hwtim
234f4 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
234f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
234f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
234f7 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32  /*.** 2008 May 2
234f8 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
234f9 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
234fa 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
234fb 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
234fc 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
234fd 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
234fe 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
234ff 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
23500 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
23501 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
23502 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
23503 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
23504 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
23505 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
23506 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
23507 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
23508 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
23509 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
2350a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2350b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2350c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2350d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2350e 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
2350f 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69   file contains i
23510 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66  nline asm code f
23511 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68  or retrieving "h
23512 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22  igh-performance"
23513 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72  .** counters for
23514 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e   x86 class CPUs.
23515 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54  .*/.#ifndef _HWT
23516 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  IME_H_.#define _
23517 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a  HWTIME_H_../*.**
23518 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
23519 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b  outine only work
2351a 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61  s on pentium-cla
2351b 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72  ss (or newer) pr
2351c 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20  ocessors..** It 
2351d 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f  uses the RDTSC o
2351e 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68  pcode to read th
2351f 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61  e cycle count va
23520 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a  lue out of the.*
23521 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20  * processor and 
23522 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c  returns that val
23523 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65  ue.  This can be
23524 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72   used for high-r
23525 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e  es.** profiling.
23526 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64  .*/.#if (defined
23527 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65  (__GNUC__) || de
23528 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29  fined(_MSC_VER))
23529 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66   && \.      (def
2352a 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65  ined(i386) || de
2352b 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20  fined(__i386__) 
2352c 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58  || defined(_M_IX
2352d 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69  86))..  #if defi
2352e 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20  ned(__GNUC__).. 
2352f 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69   __inline__ sqli
23530 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
23531 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
23532 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
23533 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f   lo, hi;.     __
23534 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
23535 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d  __ ("rdtsc" : "=
23536 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68  a" (lo), "=d" (h
23537 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e  i));.     return
23538 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29   (sqlite_uint64)
23539 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20  hi << 32 | lo;. 
2353a 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69   }..  #elif defi
2353b 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20  ned(_MSC_VER).. 
2353c 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65   __declspec(nake
2353d 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69  d) __inline sqli
2353e 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63  te_uint64 __cdec
2353f 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  l sqlite3Hwtime(
23540 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73  void){.     __as
23541 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73  m {.        rdts
23542 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20  c.        ret   
23543 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c      ; return val
23544 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20  ue at EDX:EAX.  
23545 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64     }.  }..  #end
23546 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e  if..#elif (defin
23547 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20  ed(__GNUC__) && 
23548 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34  defined(__x86_64
23549 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65  __))..  __inline
2354a 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  __ sqlite_uint64
2354b 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
2354c 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  oid){.      unsi
2354d 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20  gned long val;. 
2354e 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76       __asm__ __v
2354f 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73  olatile__ ("rdts
23550 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29  c" : "=A" (val))
23551 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76  ;.      return v
23552 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20  al;.  }. .#elif 
23553 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f  (defined(__GNUC_
23554 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  _) && defined(__
23555 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c  ppc__))..  __inl
23556 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
23557 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
23558 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75  e(void){.      u
23559 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
2355a 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20  g retval;.      
2355b 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75  unsigned long ju
2355c 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f  nk;.      __asm_
2355d 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
2355e 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31  "\n\.          1
2355f 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25  :      mftbu   %
23560 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  1\n\.           
23561 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25         mftb    %
23562 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  L0\n\.          
23563 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20          mftbu   
23564 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  %0\n\.          
23565 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20          cmpw    
23566 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20  %0,%1\n\.       
23567 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20             bne  
23568 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20     1b".         
23569 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20           : "=r" 
2356a 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28  (retval), "=r" (
2356b 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65  junk));.      re
2356c 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d  turn retval;.  }
2356d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f  ..#else..  #erro
2356e 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74  r Need implement
2356f 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  ation of sqlite3
23570 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75  Hwtime() for you
23571 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f  r platform...  /
23572 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c  *.  ** To compil
23573 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d  e without implem
23574 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77  enting sqlite3Hw
23575 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
23576 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79  platform,.  ** y
23577 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68  ou can remove th
23578 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61  e above #error a
23579 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f  nd use the follo
2357a 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66  wing.  ** stub f
2357b 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69  unction.  You wi
2357c 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73  ll lose timing s
2357d 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a  upport for many.
2357e 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75    ** of the debu
2357f 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e  gging and testin
23580 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74  g utilities, but
23581 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20   it should at.  
23582 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65  ** least compile
23583 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53   and run..  */.S
23584 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
23585 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
23586 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
23587 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69  ){ return ((sqli
23588 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a  te_uint64)0); }.
23589 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20  .#endif..#endif 
2358a 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54  /* !defined(_HWT
2358b 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a  IME_H_) */../***
2358c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2358d 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a  of hwtime.h ****
2358e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2358f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
23591 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
23592 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
23593 6c 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65  left off in vdbe
23594 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
23595 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e  **********/..#en
23596 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43  dif../*.** The C
23597 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55  HECK_FOR_INTERRU
23598 50 54 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64  PT macro defined
23599 20 68 65 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 73   here looks to s
2359a 65 65 20 69 66 20 74 68 65 0a 2a 2a 20 73 71 6c  ee if the.** sql
2359b 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29  ite3_interrupt()
2359c 20 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65   routine has bee
2359d 6e 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 74  n called.  If it
2359e 20 68 61 73 20 62 65 65 6e 2c 20 74 68 65 6e 0a   has been, then.
2359f 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66  ** processing of
235a0 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61   the VDBE progra
235a1 6d 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64  m is interrupted
235a2 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63  ..**.** This mac
235a3 72 6f 20 61 64 64 65 64 20 74 6f 20 65 76 65 72  ro added to ever
235a4 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68  y instruction th
235a5 61 74 20 64 6f 65 73 20 61 20 6a 75 6d 70 20 69  at does a jump i
235a6 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 69 6d  n order to.** im
235a7 70 6c 65 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e 20  plement a loop. 
235a8 20 54 68 69 73 20 74 65 73 74 20 75 73 65 64 20   This test used 
235a9 74 6f 20 62 65 20 6f 6e 20 65 76 65 72 79 20 73  to be on every s
235aa 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f  ingle instructio
235ab 6e 2c 0a 2a 2a 20 62 75 74 20 74 68 61 74 20 6d  n,.** but that m
235ac 65 61 6e 74 20 77 65 20 6d 6f 72 65 20 74 65 73  eant we more tes
235ad 74 69 6e 67 20 74 68 61 6e 20 77 65 20 6e 65 65  ting than we nee
235ae 64 65 64 2e 20 20 42 79 20 6f 6e 6c 79 20 74 65  ded.  By only te
235af 73 74 69 6e 67 20 74 68 65 0a 2a 2a 20 66 6c 61  sting the.** fla
235b0 67 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75  g on jump instru
235b1 63 74 69 6f 6e 73 2c 20 77 65 20 67 65 74 20 61  ctions, we get a
235b2 20 28 73 6d 61 6c 6c 29 20 73 70 65 65 64 20 69   (small) speed i
235b3 6d 70 72 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23  mprovement..*/.#
235b4 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 46 4f 52  define CHECK_FOR
235b5 5f 49 4e 54 45 52 52 55 50 54 20 5c 0a 20 20 20  _INTERRUPT \.   
235b6 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74  if( db->u1.isInt
235b7 65 72 72 75 70 74 65 64 20 29 20 67 6f 74 6f 20  errupted ) goto 
235b8 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74  abort_due_to_int
235b9 65 72 72 75 70 74 3b 0a 0a 0a 23 69 66 6e 64 65  errupt;...#ifnde
235ba 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54  f NDEBUG./*.** T
235bb 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
235bc 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d  only called from
235bd 20 77 69 74 68 69 6e 20 61 6e 20 61 73 73 65 72   within an asser
235be 74 28 29 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  t() expression. 
235bf 49 74 0a 2a 2a 20 63 68 65 63 6b 73 20 74 68 61  It.** checks tha
235c0 74 20 74 68 65 20 73 71 6c 69 74 65 33 2e 6e 54  t the sqlite3.nT
235c1 72 61 6e 73 61 63 74 69 6f 6e 20 76 61 72 69 61  ransaction varia
235c2 62 6c 65 20 69 73 20 63 6f 72 72 65 63 74 6c 79  ble is correctly
235c3 20 73 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20 6e   set to.** the n
235c4 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 72 61  umber of non-tra
235c5 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69  nsaction savepoi
235c6 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  nts currently in
235c7 20 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20   the .** linked 
235c8 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 61 74  list starting at
235c9 20 73 71 6c 69 74 65 33 2e 70 53 61 76 65 70 6f   sqlite3.pSavepo
235ca 69 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 55 73 61 67  int..** .** Usag
235cb 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 73 73  e:.**.**     ass
235cc 65 72 74 28 20 63 68 65 63 6b 53 61 76 65 70 6f  ert( checkSavepo
235cd 69 6e 74 43 6f 75 6e 74 28 64 62 29 20 29 3b 0a  intCount(db) );.
235ce 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68  */.static int ch
235cf 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e  eckSavepointCoun
235d0 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  t(sqlite3 *db){.
235d1 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 53    int n = 0;.  S
235d2 61 76 65 70 6f 69 6e 74 20 2a 70 3b 0a 20 20 66  avepoint *p;.  f
235d3 6f 72 28 70 3d 64 62 2d 3e 70 53 61 76 65 70 6f  or(p=db->pSavepo
235d4 69 6e 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65  int; p; p=p->pNe
235d5 78 74 29 20 6e 2b 2b 3b 0a 20 20 61 73 73 65 72  xt) n++;.  asser
235d6 74 28 20 6e 3d 3d 28 64 62 2d 3e 6e 53 61 76 65  t( n==(db->nSave
235d7 70 6f 69 6e 74 20 2b 20 64 62 2d 3e 69 73 54 72  point + db->isTr
235d8 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69  ansactionSavepoi
235d9 6e 74 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  nt) );.  return 
235da 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  1;.}.#endif../*.
235db 2a 2a 20 54 72 61 6e 73 66 65 72 20 65 72 72 6f  ** Transfer erro
235dc 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 66  r message text f
235dd 72 6f 6d 20 61 6e 20 73 71 6c 69 74 65 33 5f 76  rom an sqlite3_v
235de 74 61 62 2e 7a 45 72 72 4d 73 67 20 28 74 65 78  tab.zErrMsg (tex
235df 74 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 6d  t stored.** in m
235e0 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
235e1 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
235e2 6f 63 29 20 69 6e 74 6f 20 61 20 56 64 62 65 2e  oc) into a Vdbe.
235e3 7a 45 72 72 4d 73 67 20 28 74 65 78 74 20 73 74  zErrMsg (text st
235e4 6f 72 65 64 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72  ored.** in memor
235e5 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
235e6 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 29  sqlite3DbMalloc)
235e7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
235e8 20 69 6d 70 6f 72 74 56 74 61 62 45 72 72 4d 73   importVtabErrMs
235e9 67 28 56 64 62 65 20 2a 70 2c 20 73 71 6c 69 74  g(Vdbe *p, sqlit
235ea 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b  e3_vtab *pVtab){
235eb 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
235ec 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74 65   p->db;.  sqlite
235ed 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
235ee 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45  ErrMsg);.  p->zE
235ef 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44  rrMsg = sqlite3D
235f0 62 53 74 72 44 75 70 28 64 62 2c 20 70 56 74 61  bStrDup(db, pVta
235f1 62 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73  b->zErrMsg);.  s
235f2 71 6c 69 74 65 33 5f 66 72 65 65 28 70 56 74 61  qlite3_free(pVta
235f3 62 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70  b->zErrMsg);.  p
235f4 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  Vtab->zErrMsg = 
235f5 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 65  0;.}.../*.** Exe
235f6 63 75 74 65 20 61 73 20 6d 75 63 68 20 6f 66 20  cute as much of 
235f7 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61  a VDBE program a
235f8 73 20 77 65 20 63 61 6e 20 74 68 65 6e 20 72 65  s we can then re
235f9 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69  turn..**.** sqli
235fa 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79  te3VdbeMakeReady
235fb 28 29 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  () must be calle
235fc 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f  d before this ro
235fd 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74  utine in order t
235fe 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 70  o.** close the p
235ff 72 6f 67 72 61 6d 20 77 69 74 68 20 61 20 66 69  rogram with a fi
23600 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 61 6e 64 20  nal OP_Halt and 
23601 74 6f 20 73 65 74 20 75 70 20 74 68 65 20 63 61  to set up the ca
23602 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61 6e 64 20 74  llbacks.** and t
23603 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
23604 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
23605 57 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 6f  Whenever a row o
23606 72 20 72 65 73 75 6c 74 20 64 61 74 61 20 69 73  r result data is
23607 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 73   available, this
23608 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 69   routine will ei
23609 74 68 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74  ther.** invoke t
2360a 68 65 20 72 65 73 75 6c 74 20 63 61 6c 6c 62 61  he result callba
2360b 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 20  ck (if there is 
2360c 6f 6e 65 29 20 6f 72 20 72 65 74 75 72 6e 20 77  one) or return w
2360d 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f  ith.** SQLITE_RO
2360e 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 61  W..**.** If an a
2360f 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74  ttempt is made t
23610 6f 20 6f 70 65 6e 20 61 20 6c 6f 63 6b 65 64 20  o open a locked 
23611 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74  database, then t
23612 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77  his routine.** w
23613 69 6c 6c 20 65 69 74 68 65 72 20 69 6e 76 6f 6b  ill either invok
23614 65 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62  e the busy callb
23615 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73  ack (if there is
23616 20 6f 6e 65 29 20 6f 72 20 69 74 20 77 69 6c 6c   one) or it will
23617 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54  .** return SQLIT
23618 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 49 66  E_BUSY..**.** If
23619 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
2361a 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  , an error messa
2361b 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  ge is written to
2361c 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
2361d 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  .** from sqlite3
2361e 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 2d  _malloc() and p-
2361f 3e 7a 45 72 72 4d 73 67 20 69 73 20 6d 61 64 65  >zErrMsg is made
23620 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61   to point to tha
23621 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65  t memory..** The
23622 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73   error code is s
23623 74 6f 72 65 64 20 69 6e 20 70 2d 3e 72 63 20 61  tored in p->rc a
23624 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nd this routine 
23625 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45  returns SQLITE_E
23626 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  RROR..**.** If t
23627 68 65 20 63 61 6c 6c 62 61 63 6b 20 65 76 65 72  he callback ever
23628 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
23629 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67  o, then the prog
2362a 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 69 6d 6d  ram exits.** imm
2362b 65 64 69 61 74 65 6c 79 2e 20 20 54 68 65 72 65  ediately.  There
2362c 20 77 69 6c 6c 20 62 65 20 6e 6f 20 65 72 72 6f   will be no erro
2362d 72 20 6d 65 73 73 61 67 65 20 62 75 74 20 74 68  r message but th
2362e 65 20 70 2d 3e 72 63 20 66 69 65 6c 64 20 69 73  e p->rc field is
2362f 0a 2a 2a 20 73 65 74 20 74 6f 20 53 51 4c 49 54  .** set to SQLIT
23630 45 5f 41 42 4f 52 54 20 61 6e 64 20 74 68 69 73  E_ABORT and this
23631 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65   routine will re
23632 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
23633 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72  R..**.** A memor
23634 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
23635 6f 72 20 63 61 75 73 65 73 20 70 2d 3e 72 63 20  or causes p->rc 
23636 74 6f 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c  to be set to SQL
23637 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20 74 68  ITE_NOMEM and th
23638 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f  is.** routine to
23639 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
2363a 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65  RROR..**.** Othe
2363b 72 20 66 61 74 61 6c 20 65 72 72 6f 72 73 20 72  r fatal errors r
2363c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
2363d 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20  OR..**.** After 
2363e 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73  this routine has
2363f 20 66 69 6e 69 73 68 65 64 2c 20 73 71 6c 69 74   finished, sqlit
23640 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 29  e3VdbeFinalize()
23641 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 73   should be.** us
23642 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 74  ed to clean up t
23643 68 65 20 6d 65 73 73 20 74 68 61 74 20 77 61 73  he mess that was
23644 20 6c 65 66 74 20 62 65 68 69 6e 64 2e 0a 2a 2f   left behind..*/
23645 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23646 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 45  int sqlite3VdbeE
23647 78 65 63 28 0a 20 20 56 64 62 65 20 2a 70 20 20  xec(.  Vdbe *p  
23648 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23649 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f    /* The VDBE */
2364a 0a 29 7b 0a 20 20 69 6e 74 20 70 63 3d 30 3b 20  .){.  int pc=0; 
2364b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2364c 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20   /* The program 
2364d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20  counter */.  Op 
2364e 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 20 20  *aOp = p->aOp;  
2364f 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
23650 6f 66 20 70 2d 3e 61 4f 70 20 2a 2f 0a 20 20 4f  of p->aOp */.  O
23651 70 20 2a 70 4f 70 3b 20 20 20 20 20 20 20 20 20  p *pOp;         
23652 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
23653 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 2a  rent operation *
23654 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  /.  int rc = SQL
23655 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 2f  ITE_OK;        /
23656 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
23657 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  n */.  sqlite3 *
23658 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20  db = p->db;     
23659 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
2365a 65 20 2a 2f 0a 20 20 75 38 20 72 65 73 65 74 53  e */.  u8 resetS
2365b 63 68 65 6d 61 4f 6e 46 61 75 6c 74 20 3d 20 30  chemaOnFault = 0
2365c 3b 20 2f 2a 20 52 65 73 65 74 20 73 63 68 65 6d  ; /* Reset schem
2365d 61 20 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72  a after an error
2365e 20 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a   if positive */.
2365f 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 3d 20    u8 encoding = 
23660 45 4e 43 28 64 62 29 3b 20 20 20 20 20 2f 2a 20  ENC(db);     /* 
23661 54 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63  The database enc
23662 6f 64 69 6e 67 20 2a 2f 0a 23 69 66 6e 64 65 66  oding */.#ifndef
23663 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f   SQLITE_OMIT_PRO
23664 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20  GRESS_CALLBACK. 
23665 20 69 6e 74 20 63 68 65 63 6b 50 72 6f 67 72 65   int checkProgre
23666 73 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ss;         /* T
23667 72 75 65 20 69 66 20 70 72 6f 67 72 65 73 73 20  rue if progress 
23668 63 61 6c 6c 62 61 63 6b 73 20 61 72 65 20 65 6e  callbacks are en
23669 61 62 6c 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  abled */.  int n
2366a 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b  ProgressOps = 0;
2366b 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 73        /* Opcodes
2366c 20 65 78 65 63 75 74 65 64 20 73 69 6e 63 65 20   executed since 
2366d 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
2366e 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 4d  k. */.#endif.  M
2366f 65 6d 20 2a 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d  em *aMem = p->aM
23670 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 70  em;       /* Cop
23671 79 20 6f 66 20 70 2d 3e 61 4d 65 6d 20 2a 2f 0a  y of p->aMem */.
23672 20 20 4d 65 6d 20 2a 70 49 6e 31 20 3d 20 30 3b    Mem *pIn1 = 0;
23673 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
23674 31 73 74 20 69 6e 70 75 74 20 6f 70 65 72 61 6e  1st input operan
23675 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 32  d */.  Mem *pIn2
23676 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
23677 20 20 2f 2a 20 32 6e 64 20 69 6e 70 75 74 20 6f    /* 2nd input o
23678 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20  perand */.  Mem 
23679 2a 70 49 6e 33 20 3d 20 30 3b 20 20 20 20 20 20  *pIn3 = 0;      
2367a 20 20 20 20 20 20 20 2f 2a 20 33 72 64 20 69 6e         /* 3rd in
2367b 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  put operand */. 
2367c 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20 30 3b 20   Mem *pOut = 0; 
2367d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
2367e 75 74 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f  utput operand */
2367f 0a 20 20 69 6e 74 20 69 43 6f 6d 70 61 72 65 20  .  int iCompare 
23680 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  = 0;          /*
23681 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 20   Result of last 
23682 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 72 61  OP_Compare opera
23683 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  tion */.  int *a
23684 50 65 72 6d 75 74 65 20 3d 20 30 3b 20 20 20 20  Permute = 0;    
23685 20 20 20 20 20 2f 2a 20 50 65 72 6d 75 74 61 74       /* Permutat
23686 69 6f 6e 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66  ion of columns f
23687 6f 72 20 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f  or OP_Compare */
23688 0a 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64  .  i64 lastRowid
23689 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64   = db->lastRowid
2368a 3b 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75  ;  /* Saved valu
2368b 65 20 6f 66 20 74 68 65 20 6c 61 73 74 20 69 6e  e of the last in
2368c 73 65 72 74 20 52 4f 57 49 44 20 2a 2f 0a 23 69  sert ROWID */.#i
2368d 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c  fdef VDBE_PROFIL
2368e 45 0a 20 20 75 36 34 20 73 74 61 72 74 3b 20 20  E.  u64 start;  
2368f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
23690 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63 6f 75 6e  * CPU clock coun
23691 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70  t at start of op
23692 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  code */.  int or
23693 69 67 50 63 3b 20 20 20 20 20 20 20 20 20 20 20  igPc;           
23694 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20       /* Program 
23695 63 6f 75 6e 74 65 72 20 61 74 20 73 74 61 72 74  counter at start
23696 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 65   of opcode */.#e
23697 6e 64 69 66 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a  ndif.  /********
23698 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23699 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2369a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2369b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a  ************.  *
2369c 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * Automatically 
2369d 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 0a 20  generated code. 
2369e 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c   **.  ** The fol
2369f 6c 6f 77 69 6e 67 20 75 6e 69 6f 6e 20 69 73 20  lowing union is 
236a0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65  automatically ge
236a1 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 0a 20  nerated by the. 
236a2 20 2a 2a 20 76 64 62 65 2d 63 6f 6d 70 72 65 73   ** vdbe-compres
236a3 73 2e 74 63 6c 20 73 63 72 69 70 74 2e 20 20 54  s.tcl script.  T
236a4 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68  he purpose of th
236a5 69 73 20 75 6e 69 6f 6e 20 69 73 20 74 6f 0a 20  is union is to. 
236a6 20 2a 2a 20 72 65 64 75 63 65 20 74 68 65 20 61   ** reduce the a
236a7 6d 6f 75 6e 74 20 6f 66 20 73 74 61 63 6b 20 73  mount of stack s
236a8 70 61 63 65 20 72 65 71 75 69 72 65 64 20 62 79  pace required by
236a9 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
236aa 20 20 2a 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74    ** See comment
236ab 73 20 69 6e 20 74 68 65 20 76 64 62 65 2d 63 6f  s in the vdbe-co
236ac 6d 70 72 65 73 73 2e 74 63 6c 20 73 63 72 69 70  mpress.tcl scrip
236ad 74 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 20  t for details.. 
236ae 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 76 64 62 65   */.  union vdbe
236af 45 78 65 63 55 6e 69 6f 6e 20 7b 0a 20 20 20 20  ExecUnion {.    
236b0 73 74 72 75 63 74 20 4f 50 5f 59 69 65 6c 64 5f  struct OP_Yield_
236b1 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
236b2 20 20 20 69 6e 74 20 70 63 44 65 73 74 3b 0a 20     int pcDest;. 
236b3 20 20 20 7d 20 61 61 3b 0a 20 20 20 20 73 74 72     } aa;.    str
236b4 75 63 74 20 4f 50 5f 4e 75 6c 6c 5f 73 74 61 63  uct OP_Null_stac
236b5 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
236b6 6e 74 20 63 6e 74 3b 0a 20 20 20 20 20 20 75 31  nt cnt;.      u1
236b7 36 20 6e 75 6c 6c 46 6c 61 67 3b 0a 20 20 20 20  6 nullFlag;.    
236b8 7d 20 61 62 3b 0a 20 20 20 20 73 74 72 75 63 74  } ab;.    struct
236b9 20 4f 50 5f 56 61 72 69 61 62 6c 65 5f 73 74 61   OP_Variable_sta
236ba 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
236bb 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 20 20 20  Mem *pVar;      
236bc 20 2f 2a 20 56 61 6c 75 65 20 62 65 69 6e 67 20   /* Value being 
236bd 74 72 61 6e 73 66 65 72 72 65 64 20 2a 2f 0a 20  transferred */. 
236be 20 20 20 7d 20 61 63 3b 0a 20 20 20 20 73 74 72     } ac;.    str
236bf 75 63 74 20 4f 50 5f 4d 6f 76 65 5f 73 74 61 63  uct OP_Move_stac
236c0 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 63  k_vars {.      c
236c1 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20  har *zMalloc;   
236c2 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 61 72 69 61  /* Holding varia
236c3 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 65  ble for allocate
236c4 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 20  d memory */.    
236c5 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20    int n;        
236c6 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
236c7 72 65 67 69 73 74 65 72 73 20 6c 65 66 74 20 74  registers left t
236c8 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20 20 20 20  o copy */.      
236c9 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20  int p1;         
236ca 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20   /* Register to 
236cb 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 20  copy from */.   
236cc 20 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20     int p2;      
236cd 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
236ce 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 20 20  to copy to */.  
236cf 20 20 7d 20 61 64 3b 0a 20 20 20 20 73 74 72 75    } ad;.    stru
236d0 63 74 20 4f 50 5f 43 6f 70 79 5f 73 74 61 63 6b  ct OP_Copy_stack
236d1 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e  _vars {.      in
236d2 74 20 6e 3b 0a 20 20 20 20 7d 20 61 65 3b 0a 20  t n;.    } ae;. 
236d3 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 65 73     struct OP_Res
236d4 75 6c 74 52 6f 77 5f 73 74 61 63 6b 5f 76 61 72  ultRow_stack_var
236d5 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70  s {.      Mem *p
236d6 4d 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  Mem;.      int i
236d7 3b 0a 20 20 20 20 7d 20 61 66 3b 0a 20 20 20 20  ;.    } af;.    
236d8 73 74 72 75 63 74 20 4f 50 5f 43 6f 6e 63 61 74  struct OP_Concat
236d9 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
236da 20 20 20 20 69 36 34 20 6e 42 79 74 65 3b 0a 20      i64 nByte;. 
236db 20 20 20 7d 20 61 67 3b 0a 20 20 20 20 73 74 72     } ag;.    str
236dc 75 63 74 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72  uct OP_Remainder
236dd 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
236de 20 20 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20      int flags;  
236df 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20      /* Combined 
236e0 4d 45 4d 5f 2a 20 66 6c 61 67 73 20 66 72 6f 6d  MEM_* flags from
236e1 20 62 6f 74 68 20 69 6e 70 75 74 73 20 2a 2f 0a   both inputs */.
236e2 20 20 20 20 20 20 69 36 34 20 69 41 3b 20 20 20        i64 iA;   
236e3 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72        /* Integer
236e4 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f   value of left o
236e5 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20  perand */.      
236e6 69 36 34 20 69 42 3b 20 20 20 20 20 20 20 20 20  i64 iB;         
236e7 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65  /* Integer value
236e8 20 6f 66 20 72 69 67 68 74 20 6f 70 65 72 61 6e   of right operan
236e9 64 20 2a 2f 0a 20 20 20 20 20 20 64 6f 75 62 6c  d */.      doubl
236ea 65 20 72 41 3b 20 20 20 20 20 20 2f 2a 20 52 65  e rA;      /* Re
236eb 61 6c 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74  al value of left
236ec 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20   operand */.    
236ed 20 20 64 6f 75 62 6c 65 20 72 42 3b 20 20 20 20    double rB;    
236ee 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20    /* Real value 
236ef 6f 66 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64  of right operand
236f0 20 2a 2f 0a 20 20 20 20 7d 20 61 68 3b 0a 20 20   */.    } ah;.  
236f1 20 20 73 74 72 75 63 74 20 4f 50 5f 46 75 6e 63    struct OP_Func
236f2 74 69 6f 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20  tion_stack_vars 
236f3 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20  {.      int i;. 
236f4 20 20 20 20 20 4d 65 6d 20 2a 70 41 72 67 3b 0a       Mem *pArg;.
236f5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f        sqlite3_co
236f6 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 20  ntext ctx;.     
236f7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
236f8 2a 61 70 56 61 6c 3b 0a 20 20 20 20 20 20 69 6e  *apVal;.      in
236f9 74 20 6e 3b 0a 20 20 20 20 7d 20 61 69 3b 0a 20  t n;.    } ai;. 
236fa 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 68 69     struct OP_Shi
236fb 66 74 52 69 67 68 74 5f 73 74 61 63 6b 5f 76 61  ftRight_stack_va
236fc 72 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20 69  rs {.      i64 i
236fd 41 3b 0a 20 20 20 20 20 20 75 36 34 20 75 41 3b  A;.      u64 uA;
236fe 0a 20 20 20 20 20 20 69 36 34 20 69 42 3b 0a 20  .      i64 iB;. 
236ff 20 20 20 20 20 75 38 20 6f 70 3b 0a 20 20 20 20       u8 op;.    
23700 7d 20 61 6a 3b 0a 20 20 20 20 73 74 72 75 63 74  } aj;.    struct
23701 20 4f 50 5f 47 65 5f 73 74 61 63 6b 5f 76 61 72   OP_Ge_stack_var
23702 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 65  s {.      int re
23703 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  s;            /*
23704 20 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20 63   Result of the c
23705 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 70 49 6e  omparison of pIn
23706 31 20 61 67 61 69 6e 73 74 20 70 49 6e 33 20 2a  1 against pIn3 *
23707 2f 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66  /.      char aff
23708 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41  inity;      /* A
23709 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 66  ffinity to use f
2370a 6f 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f  or comparison */
2370b 0a 20 20 20 20 20 20 75 31 36 20 66 6c 61 67 73  .      u16 flags
2370c 31 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  1;         /* Co
2370d 70 79 20 6f 66 20 69 6e 69 74 69 61 6c 20 76 61  py of initial va
2370e 6c 75 65 20 6f 66 20 70 49 6e 31 2d 3e 66 6c 61  lue of pIn1->fla
2370f 67 73 20 2a 2f 0a 20 20 20 20 20 20 75 31 36 20  gs */.      u16 
23710 66 6c 61 67 73 33 3b 20 20 20 20 20 20 20 20 20  flags3;         
23711 2f 2a 20 43 6f 70 79 20 6f 66 20 69 6e 69 74 69  /* Copy of initi
23712 61 6c 20 76 61 6c 75 65 20 6f 66 20 70 49 6e 33  al value of pIn3
23713 2d 3e 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 7d  ->flags */.    }
23714 20 61 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 20   ak;.    struct 
23715 4f 50 5f 43 6f 6d 70 61 72 65 5f 73 74 61 63 6b  OP_Compare_stack
23716 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e  _vars {.      in
23717 74 20 6e 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  t n;.      int i
23718 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b 0a  ;.      int p1;.
23719 20 20 20 20 20 20 69 6e 74 20 70 32 3b 0a 20 20        int p2;.  
2371a 20 20 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66      const KeyInf
2371b 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20  o *pKeyInfo;.   
2371c 20 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 20 20     int idx;.    
2371d 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
2371e 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e  ;    /* Collatin
2371f 67 20 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73  g sequence to us
23720 65 20 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a  e on this term *
23721 2f 0a 20 20 20 20 20 20 69 6e 74 20 62 52 65 76  /.      int bRev
23722 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ;          /* Tr
23723 75 65 20 66 6f 72 20 44 45 53 43 45 4e 44 49 4e  ue for DESCENDIN
23724 47 20 73 6f 72 74 20 6f 72 64 65 72 20 2a 2f 0a  G sort order */.
23725 20 20 20 20 7d 20 61 6c 3b 0a 20 20 20 20 73 74      } al;.    st
23726 72 75 63 74 20 4f 50 5f 4f 72 5f 73 74 61 63 6b  ruct OP_Or_stack
23727 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e  _vars {.      in
23728 74 20 76 31 3b 20 20 20 20 2f 2a 20 4c 65 66 74  t v1;    /* Left
23729 20 6f 70 65 72 61 6e 64 3a 20 20 30 3d 3d 46 41   operand:  0==FA
2372a 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d  LSE, 1==TRUE, 2=
2372b 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c  =UNKNOWN or NULL
2372c 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 76 32   */.      int v2
2372d 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70  ;    /* Right op
2372e 65 72 61 6e 64 3a 20 30 3d 3d 46 41 4c 53 45 2c  erand: 0==FALSE,
2372f 20 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b   1==TRUE, 2==UNK
23730 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  NOWN or NULL */.
23731 20 20 20 20 7d 20 61 6d 3b 0a 20 20 20 20 73 74      } am;.    st
23732 72 75 63 74 20 4f 50 5f 49 66 4e 6f 74 5f 73 74  ruct OP_IfNot_st
23733 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
23734 20 69 6e 74 20 63 3b 0a 20 20 20 20 7d 20 61 6e   int c;.    } an
23735 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
23736 43 6f 6c 75 6d 6e 5f 73 74 61 63 6b 5f 76 61 72  Column_stack_var
23737 73 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70 61  s {.      u32 pa
23738 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20  yloadSize;   /* 
23739 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
2373a 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  in the record */
2373b 0a 20 20 20 20 20 20 69 36 34 20 70 61 79 6c 6f  .      i64 paylo
2373c 61 64 53 69 7a 65 36 34 3b 20 2f 2a 20 4e 75 6d  adSize64; /* Num
2373d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
2373e 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
2373f 20 20 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20      int p1;     
23740 20 20 20 20 20 20 20 2f 2a 20 50 31 20 76 61 6c         /* P1 val
23741 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65  ue of the opcode
23742 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 32   */.      int p2
23743 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
23744 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 6f  column number to
23745 20 72 65 74 72 69 65 76 65 20 2a 2f 0a 20 20 20   retrieve */.   
23746 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70     VdbeCursor *p
23747 43 3b 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42  C;    /* The VDB
23748 45 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20  E cursor */.    
23749 20 20 63 68 61 72 20 2a 7a 52 65 63 3b 20 20 20    char *zRec;   
2374a 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
2374b 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f  to complete reco
2374c 72 64 2d 64 61 74 61 20 2a 2f 0a 20 20 20 20 20  rd-data */.     
2374d 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
2374e 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65  ;   /* The BTree
2374f 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20   cursor */.     
23750 20 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20   u32 *aType;    
23751 20 20 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d 20      /* aType[i] 
23752 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65 72 69  holds the numeri
23753 63 20 74 79 70 65 20 6f 66 20 74 68 65 20 69 2d  c type of the i-
23754 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20  th column */.   
23755 20 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b     u32 *aOffset;
23756 20 20 20 20 20 20 2f 2a 20 61 4f 66 66 73 65 74        /* aOffset
23757 5b 69 5d 20 69 73 20 6f 66 66 73 65 74 20 74 6f  [i] is offset to
23758 20 73 74 61 72 74 20 6f 66 20 64 61 74 61 20 66   start of data f
23759 6f 72 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a  or i-th column *
2375a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65  /.      int nFie
2375b 6c 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 75  ld;        /* nu
2375c 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69  mber of fields i
2375d 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a  n the record */.
2375e 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20        int len;  
2375f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
23760 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65  length of the se
23761 72 69 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f  rialized data fo
23762 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  r the column */.
23763 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20        int i;    
23764 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
23765 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
23766 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 20    char *zData;  
23767 20 20 20 20 20 2f 2a 20 50 61 72 74 20 6f 66 20       /* Part of 
23768 74 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67  the record being
23769 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20   decoded */.    
2376a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 20 20 20    Mem *pDest;   
2376b 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f       /* Where to
2376c 20 77 72 69 74 65 20 74 68 65 20 65 78 74 72 61   write the extra
2376d 63 74 65 64 20 76 61 6c 75 65 20 2a 2f 0a 20 20  cted value */.  
2376e 20 20 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20      Mem sMem;   
2376f 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74         /* For st
23770 6f 72 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  oring the record
23771 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a   being decoded *
23772 2f 0a 20 20 20 20 20 20 75 38 20 2a 7a 49 64 78  /.      u8 *zIdx
23773 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  ;          /* In
23774 64 65 78 20 69 6e 74 6f 20 68 65 61 64 65 72 20  dex into header 
23775 2a 2f 0a 20 20 20 20 20 20 75 38 20 2a 7a 45 6e  */.      u8 *zEn
23776 64 48 64 72 3b 20 20 20 20 20 20 20 2f 2a 20 50  dHdr;       /* P
23777 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20  ointer to first 
23778 62 79 74 65 20 61 66 74 65 72 20 74 68 65 20 68  byte after the h
23779 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 75  eader */.      u
2377a 33 32 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20  32 offset;      
2377b 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f    /* Offset into
2377c 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 20   the data */.   
2377d 20 20 20 75 33 32 20 73 7a 46 69 65 6c 64 3b 20     u32 szField; 
2377e 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2377f 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
23780 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 66 69 65  content of a fie
23781 6c 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ld */.      int 
23782 73 7a 48 64 72 3b 20 20 20 20 20 20 20 20 20 2f  szHdr;         /
23783 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65  * Size of the he
23784 61 64 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20  ader size field 
23785 61 74 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f  at start of reco
23786 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  rd */.      int 
23787 61 76 61 69 6c 3b 20 20 20 20 20 20 20 20 20 2f  avail;         /
23788 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
23789 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 64  s of available d
2378a 61 74 61 20 2a 2f 0a 20 20 20 20 20 20 75 33 32  ata */.      u32
2378b 20 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   t;             
2378c 2f 2a 20 41 20 74 79 70 65 20 63 6f 64 65 20 66  /* A type code f
2378d 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 20 68  rom the record h
2378e 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 4d  eader */.      M
2378f 65 6d 20 2a 70 52 65 67 3b 20 20 20 20 20 20 20  em *pReg;       
23790 20 20 2f 2a 20 50 73 65 75 64 6f 54 61 62 6c 65    /* PseudoTable
23791 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 20   input register 
23792 2a 2f 0a 20 20 20 20 7d 20 61 6f 3b 0a 20 20 20  */.    } ao;.   
23793 20 73 74 72 75 63 74 20 4f 50 5f 41 66 66 69 6e   struct OP_Affin
23794 69 74 79 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ity_stack_vars {
23795 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
23796 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20  r *zAffinity;   
23797 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20  /* The affinity 
23798 74 6f 20 62 65 20 61 70 70 6c 69 65 64 20 2a 2f  to be applied */
23799 0a 20 20 20 20 20 20 63 68 61 72 20 63 41 66 66  .      char cAff
2379a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2379b 2f 2a 20 41 20 73 69 6e 67 6c 65 20 63 68 61 72  /* A single char
2379c 61 63 74 65 72 20 6f 66 20 61 66 66 69 6e 69 74  acter of affinit
2379d 79 20 2a 2f 0a 20 20 20 20 7d 20 61 70 3b 0a 20  y */.    } ap;. 
2379e 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4d 61 6b     struct OP_Mak
2379f 65 52 65 63 6f 72 64 5f 73 74 61 63 6b 5f 76 61  eRecord_stack_va
237a0 72 73 20 7b 0a 20 20 20 20 20 20 75 38 20 2a 7a  rs {.      u8 *z
237a1 4e 65 77 52 65 63 6f 72 64 3b 20 20 20 20 20 20  NewRecord;      
237a2 20 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f    /* A buffer to
237a3 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 66   hold the data f
237a4 6f 72 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72  or the new recor
237a5 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a  d */.      Mem *
237a6 70 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20  pRec;           
237a7 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 65 63    /* The new rec
237a8 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 75 36 34  ord */.      u64
237a9 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20 20   nData;         
237aa 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
237ab 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 73   bytes of data s
237ac 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e  pace */.      in
237ad 74 20 6e 48 64 72 3b 20 20 20 20 20 20 20 20 20  t nHdr;         
237ae 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
237af 66 20 62 79 74 65 73 20 6f 66 20 68 65 61 64 65  f bytes of heade
237b0 72 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20  r space */.     
237b1 20 69 36 34 20 6e 42 79 74 65 3b 20 20 20 20 20   i64 nByte;     
237b2 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20          /* Data 
237b3 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66  space required f
237b4 6f 72 20 74 68 69 73 20 72 65 63 6f 72 64 20 2a  or this record *
237b5 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 5a 65 72  /.      int nZer
237b6 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  o;             /
237b7 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f  * Number of zero
237b8 20 62 79 74 65 73 20 61 74 20 74 68 65 20 65 6e   bytes at the en
237b9 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20  d of the record 
237ba 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 56 61  */.      int nVa
237bb 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  rint;           
237bc 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
237bd 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20 2a  es in a varint *
237be 2f 0a 20 20 20 20 20 20 75 33 32 20 73 65 72 69  /.      u32 seri
237bf 61 6c 5f 74 79 70 65 3b 20 20 20 20 20 20 20 2f  al_type;       /
237c0 2a 20 54 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a  * Type field */.
237c1 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 61        Mem *pData
237c2 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  0;           /* 
237c3 46 69 72 73 74 20 66 69 65 6c 64 20 74 6f 20 62  First field to b
237c4 65 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20  e combined into 
237c5 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
237c6 20 20 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20      Mem *pLast; 
237c7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
237c8 73 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  st field of the 
237c9 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20  record */.      
237ca 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20  int nField;     
237cb 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
237cc 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68   of fields in th
237cd 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20  e record */.    
237ce 20 20 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74    char *zAffinit
237cf 79 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  y;       /* The 
237d0 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20  affinity string 
237d1 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a  for the record *
237d2 2f 0a 20 20 20 20 20 20 69 6e 74 20 66 69 6c 65  /.      int file
237d3 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 20 2f  _format;       /
237d4 2a 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 74 6f  * File format to
237d5 20 75 73 65 20 66 6f 72 20 65 6e 63 6f 64 69 6e   use for encodin
237d6 67 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69  g */.      int i
237d7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
237d8 20 20 2f 2a 20 53 70 61 63 65 20 75 73 65 64 20    /* Space used 
237d9 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b 5d 20  in zNewRecord[] 
237da 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6c 65 6e  */.      int len
237db 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
237dc 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 20 66  /* Length of a f
237dd 69 65 6c 64 20 2a 2f 0a 20 20 20 20 7d 20 61 71  ield */.    } aq
237de 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
237df 43 6f 75 6e 74 5f 73 74 61 63 6b 5f 76 61 72 73  Count_stack_vars
237e0 20 7b 0a 20 20 20 20 20 20 69 36 34 20 6e 45 6e   {.      i64 nEn
237e1 74 72 79 3b 0a 20 20 20 20 20 20 42 74 43 75 72  try;.      BtCur
237e2 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20  sor *pCrsr;.    
237e3 7d 20 61 72 3b 0a 20 20 20 20 73 74 72 75 63 74  } ar;.    struct
237e4 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 5f 73 74   OP_Savepoint_st
237e5 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
237e6 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20   int p1;        
237e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
237e8 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 50 31 20   /* Value of P1 
237e9 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20  operand */.     
237ea 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
237eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
237ec 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76 65   /* Name of save
237ed 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 69  point */.      i
237ee 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20 20  nt nName;.      
237ef 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 3b  Savepoint *pNew;
237f0 0a 20 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74  .      Savepoint
237f1 20 2a 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20   *pSavepoint;.  
237f2 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70      Savepoint *p
237f3 54 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  Tmp;.      int i
237f4 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20  Savepoint;.     
237f5 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 7d 20 61   int ii;.    } a
237f6 73 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  s;.    struct OP
237f7 5f 41 75 74 6f 43 6f 6d 6d 69 74 5f 73 74 61 63  _AutoCommit_stac
237f8 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
237f9 6e 74 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f  nt desiredAutoCo
237fa 6d 6d 69 74 3b 0a 20 20 20 20 20 20 69 6e 74 20  mmit;.      int 
237fb 69 52 6f 6c 6c 62 61 63 6b 3b 0a 20 20 20 20 20  iRollback;.     
237fc 20 69 6e 74 20 74 75 72 6e 4f 6e 41 43 3b 0a 20   int turnOnAC;. 
237fd 20 20 20 7d 20 61 74 3b 0a 20 20 20 20 73 74 72     } at;.    str
237fe 75 63 74 20 4f 50 5f 54 72 61 6e 73 61 63 74 69  uct OP_Transacti
237ff 6f 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  on_stack_vars {.
23800 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74        Btree *pBt
23801 3b 0a 20 20 20 20 7d 20 61 75 3b 0a 20 20 20 20  ;.    } au;.    
23802 73 74 72 75 63 74 20 4f 50 5f 52 65 61 64 43 6f  struct OP_ReadCo
23803 6f 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20  okie_stack_vars 
23804 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 74  {.      int iMet
23805 61 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62  a;.      int iDb
23806 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6f  ;.      int iCoo
23807 6b 69 65 3b 0a 20 20 20 20 7d 20 61 76 3b 0a 20  kie;.    } av;. 
23808 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 74     struct OP_Set
23809 43 6f 6f 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72  Cookie_stack_var
2380a 73 20 7b 0a 20 20 20 20 20 20 44 62 20 2a 70 44  s {.      Db *pD
2380b 62 3b 0a 20 20 20 20 7d 20 61 77 3b 0a 20 20 20  b;.    } aw;.   
2380c 20 73 74 72 75 63 74 20 4f 50 5f 56 65 72 69 66   struct OP_Verif
2380d 79 43 6f 6f 6b 69 65 5f 73 74 61 63 6b 5f 76 61  yCookie_stack_va
2380e 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  rs {.      int i
2380f 4d 65 74 61 3b 0a 20 20 20 20 20 20 69 6e 74 20  Meta;.      int 
23810 69 47 65 6e 3b 0a 20 20 20 20 20 20 42 74 72 65  iGen;.      Btre
23811 65 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 61 78  e *pBt;.    } ax
23812 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
23813 4f 70 65 6e 57 72 69 74 65 5f 73 74 61 63 6b 5f  OpenWrite_stack_
23814 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
23815 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20 4b   nField;.      K
23816 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
23817 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b 0a  ;.      int p2;.
23818 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20        int iDb;. 
23819 20 20 20 20 20 69 6e 74 20 77 72 46 6c 61 67 3b       int wrFlag;
2381a 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 58  .      Btree *pX
2381b 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  ;.      VdbeCurs
2381c 6f 72 20 2a 70 43 75 72 3b 0a 20 20 20 20 20 20  or *pCur;.      
2381d 44 62 20 2a 70 44 62 3b 0a 20 20 20 20 7d 20 61  Db *pDb;.    } a
2381e 79 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  y;.    struct OP
2381f 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 5f 73  _OpenEphemeral_s
23820 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
23821 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
23822 78 3b 0a 20 20 20 20 7d 20 61 7a 3b 0a 20 20 20  x;.    } az;.   
23823 20 73 74 72 75 63 74 20 4f 50 5f 53 6f 72 74 65   struct OP_Sorte
23824 72 4f 70 65 6e 5f 73 74 61 63 6b 5f 76 61 72 73  rOpen_stack_vars
23825 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
23826 73 6f 72 20 2a 70 43 78 3b 0a 20 20 20 20 7d 20  sor *pCx;.    } 
23827 62 61 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  ba;.    struct O
23828 50 5f 4f 70 65 6e 50 73 65 75 64 6f 5f 73 74 61  P_OpenPseudo_sta
23829 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
2382a 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b  VdbeCursor *pCx;
2382b 0a 20 20 20 20 7d 20 62 62 3b 0a 20 20 20 20 73  .    } bb;.    s
2382c 74 72 75 63 74 20 4f 50 5f 53 65 65 6b 47 74 5f  truct OP_SeekGt_
2382d 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
2382e 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20     int res;.    
2382f 20 20 69 6e 74 20 6f 63 3b 0a 20 20 20 20 20 20    int oc;.      
23830 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
23831 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65        UnpackedRe
23832 63 6f 72 64 20 72 3b 0a 20 20 20 20 20 20 69 6e  cord r;.      in
23833 74 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20  t nField;.      
23834 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 2f  i64 iKey;      /
23835 2a 20 54 68 65 20 72 6f 77 69 64 20 77 65 20 61  * The rowid we a
23836 72 65 20 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f  re to seek to */
23837 0a 20 20 20 20 7d 20 62 63 3b 0a 20 20 20 20 73  .    } bc;.    s
23838 74 72 75 63 74 20 4f 50 5f 53 65 65 6b 5f 73 74  truct OP_Seek_st
23839 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
2383a 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
2383b 0a 20 20 20 20 7d 20 62 64 3b 0a 20 20 20 20 73  .    } bd;.    s
2383c 74 72 75 63 74 20 4f 50 5f 46 6f 75 6e 64 5f 73  truct OP_Found_s
2383d 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
2383e 20 20 69 6e 74 20 61 6c 72 65 61 64 79 45 78 69    int alreadyExi
2383f 73 74 73 3b 0a 20 20 20 20 20 20 56 64 62 65 43  sts;.      VdbeC
23840 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20  ursor *pC;.     
23841 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20   int res;.      
23842 63 68 61 72 20 2a 70 46 72 65 65 3b 0a 20 20 20  char *pFree;.   
23843 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72     UnpackedRecor
23844 64 20 2a 70 49 64 78 4b 65 79 3b 0a 20 20 20 20  d *pIdxKey;.    
23845 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
23846 20 72 3b 0a 20 20 20 20 20 20 63 68 61 72 20 61   r;.      char a
23847 54 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28 73  TempRec[ROUND8(s
23848 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65  izeof(UnpackedRe
23849 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28  cord)) + sizeof(
2384a 4d 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 20 20 20  Mem)*3 + 7];.   
2384b 20 7d 20 62 65 3b 0a 20 20 20 20 73 74 72 75 63   } be;.    struc
2384c 74 20 4f 50 5f 49 73 55 6e 69 71 75 65 5f 73 74  t OP_IsUnique_st
2384d 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
2384e 20 75 31 36 20 69 69 3b 0a 20 20 20 20 20 20 56   u16 ii;.      V
2384f 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a  dbeCursor *pCx;.
23850 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a        BtCursor *
23851 70 43 72 73 72 3b 0a 20 20 20 20 20 20 75 31 36  pCrsr;.      u16
23852 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20 4d   nField;.      M
23853 65 6d 20 2a 61 4d 78 3b 0a 20 20 20 20 20 20 55  em *aMx;.      U
23854 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b  npackedRecord r;
23855 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23856 20 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e 64 65    /* B-Tree inde
23857 78 20 73 65 61 72 63 68 20 6b 65 79 20 2a 2f 0a  x search key */.
23858 20 20 20 20 20 20 69 36 34 20 52 3b 20 20 20 20        i64 R;    
23859 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2385a 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69           /* Rowi
2385b 64 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69  d stored in regi
2385c 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 20 20 7d  ster P3 */.    }
2385d 20 62 66 3b 0a 20 20 20 20 73 74 72 75 63 74 20   bf;.    struct 
2385e 4f 50 5f 4e 6f 74 45 78 69 73 74 73 5f 73 74 61  OP_NotExists_sta
2385f 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
23860 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
23861 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a        BtCursor *
23862 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74  pCrsr;.      int
23863 20 72 65 73 3b 0a 20 20 20 20 20 20 75 36 34 20   res;.      u64 
23864 69 4b 65 79 3b 0a 20 20 20 20 7d 20 62 67 3b 0a  iKey;.    } bg;.
23865 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 65      struct OP_Ne
23866 77 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72  wRowid_stack_var
23867 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20 76 3b  s {.      i64 v;
23868 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23869 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 77 69   /* The new rowi
2386a 64 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 43  d */.      VdbeC
2386b 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 20 20  ursor *pC;      
2386c 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74    /* Cursor of t
2386d 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65 20  able to get the 
2386e 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20  new rowid */.   
2386f 20 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20 20     int res;     
23870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
23871 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65  ult of an sqlite
23872 33 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a  3BtreeLast() */.
23873 20 20 20 20 20 20 69 6e 74 20 63 6e 74 3b 20 20        int cnt;  
23874 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
23875 43 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74  Counter to limit
23876 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   the number of s
23877 65 61 72 63 68 65 73 20 2a 2f 0a 20 20 20 20 20  earches */.     
23878 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20   Mem *pMem;     
23879 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
2387a 74 65 72 20 68 6f 6c 64 69 6e 67 20 6c 61 72 67  ter holding larg
2387b 65 73 74 20 72 6f 77 69 64 20 66 6f 72 20 41 55  est rowid for AU
2387c 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20  TOINCREMENT */. 
2387d 20 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a       VdbeFrame *
2387e 70 46 72 61 6d 65 3b 20 20 20 20 20 2f 2a 20 52  pFrame;     /* R
2387f 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 56 44 42  oot frame of VDB
23880 45 20 2a 2f 0a 20 20 20 20 7d 20 62 68 3b 0a 20  E */.    } bh;. 
23881 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 73     struct OP_Ins
23882 65 72 74 49 6e 74 5f 73 74 61 63 6b 5f 76 61 72  ertInt_stack_var
23883 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70  s {.      Mem *p
23884 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 4d  Data;       /* M
23885 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20  EM cell holding 
23886 64 61 74 61 20 66 6f 72 20 74 68 65 20 72 65 63  data for the rec
23887 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74  ord to be insert
23888 65 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20  ed */.      Mem 
23889 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 2a  *pKey;        /*
2388a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e   MEM cell holdin
2388b 67 20 6b 65 79 20 20 66 6f 72 20 74 68 65 20 72  g key  for the r
2388c 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69  ecord */.      i
2388d 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20  64 iKey;        
2388e 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20   /* The integer 
2388f 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 6f 72  ROWID or key for
23890 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62   the record to b
23891 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  e inserted */.  
23892 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
23893 70 43 3b 20 20 20 2f 2a 20 43 75 72 73 6f 72 20  pC;   /* Cursor 
23894 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  to table into wh
23895 69 63 68 20 69 6e 73 65 72 74 20 69 73 20 77 72  ich insert is wr
23896 69 74 74 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69  itten */.      i
23897 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20  nt nZero;       
23898 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65   /* Number of ze
23899 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 70 70 65  ro-bytes to appe
2389a 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  nd */.      int 
2389b 73 65 65 6b 52 65 73 75 6c 74 3b 20 20 20 2f 2a  seekResult;   /*
2389c 20 52 65 73 75 6c 74 20 6f 66 20 70 72 69 6f 72   Result of prior
2389d 20 73 65 65 6b 20 6f 72 20 30 20 69 66 20 6e 6f   seek or 0 if no
2389e 20 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66   USESEEKRESULT f
2389f 6c 61 67 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e  lag */.      con
238a0 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 2f  st char *zDb;  /
238a1 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  * database name 
238a2 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 75 70  - used by the up
238a3 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 20  date hook */.   
238a4 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
238a5 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e 61  Tbl; /* Table na
238a6 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65  me - used by the
238a7 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a   opdate hook */.
238a8 20 20 20 20 20 20 69 6e 74 20 6f 70 3b 20 20 20        int op;   
238a9 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64          /* Opcod
238aa 65 20 66 6f 72 20 75 70 64 61 74 65 20 68 6f 6f  e for update hoo
238ab 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45  k: SQLITE_UPDATE
238ac 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53 45 52   or SQLITE_INSER
238ad 54 20 2a 2f 0a 20 20 20 20 7d 20 62 69 3b 0a 20  T */.    } bi;. 
238ae 20 20 20 73 74 72 75 63 74 20 4f 50 5f 44 65 6c     struct OP_Del
238af 65 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ete_stack_vars {
238b0 0a 20 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b  .      i64 iKey;
238b1 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f  .      VdbeCurso
238b2 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 6a 3b  r *pC;.    } bj;
238b3 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53  .    struct OP_S
238b4 6f 72 74 65 72 43 6f 6d 70 61 72 65 5f 73 74 61  orterCompare_sta
238b5 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
238b6 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
238b7 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
238b8 20 20 20 7d 20 62 6b 3b 0a 20 20 20 20 73 74 72     } bk;.    str
238b9 75 63 74 20 4f 50 5f 53 6f 72 74 65 72 44 61 74  uct OP_SorterDat
238ba 61 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  a_stack_vars {. 
238bb 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
238bc 2a 70 43 3b 0a 20 20 20 20 7d 20 62 6c 3b 0a 20  *pC;.    } bl;. 
238bd 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77     struct OP_Row
238be 44 61 74 61 5f 73 74 61 63 6b 5f 76 61 72 73 20  Data_stack_vars 
238bf 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  {.      VdbeCurs
238c0 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74  or *pC;.      Bt
238c1 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
238c2 20 20 20 20 20 75 33 32 20 6e 3b 0a 20 20 20 20       u32 n;.    
238c3 20 20 69 36 34 20 6e 36 34 3b 0a 20 20 20 20 7d    i64 n64;.    }
238c4 20 62 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 20   bm;.    struct 
238c5 4f 50 5f 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76  OP_Rowid_stack_v
238c6 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65  ars {.      Vdbe
238c7 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20  Cursor *pC;.    
238c8 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20 20 73    i64 v;.      s
238c9 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
238ca 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  ab;.      const 
238cb 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
238cc 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 20 62  pModule;.    } b
238cd 6e 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  n;.    struct OP
238ce 5f 4e 75 6c 6c 52 6f 77 5f 73 74 61 63 6b 5f 76  _NullRow_stack_v
238cf 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65  ars {.      Vdbe
238d0 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20  Cursor *pC;.    
238d1 7d 20 62 6f 3b 0a 20 20 20 20 73 74 72 75 63 74  } bo;.    struct
238d2 20 4f 50 5f 4c 61 73 74 5f 73 74 61 63 6b 5f 76   OP_Last_stack_v
238d3 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65  ars {.      Vdbe
238d4 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20  Cursor *pC;.    
238d5 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
238d6 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73  r;.      int res
238d7 3b 0a 20 20 20 20 7d 20 62 70 3b 0a 20 20 20 20  ;.    } bp;.    
238d8 73 74 72 75 63 74 20 4f 50 5f 52 65 77 69 6e 64  struct OP_Rewind
238d9 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
238da 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
238db 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73  pC;.      BtCurs
238dc 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20  or *pCrsr;.     
238dd 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 7d 20   int res;.    } 
238de 62 71 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  bq;.    struct O
238df 50 5f 4e 65 78 74 5f 73 74 61 63 6b 5f 76 61 72  P_Next_stack_var
238e0 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75  s {.      VdbeCu
238e1 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20  rsor *pC;.      
238e2 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 7d 20 62  int res;.    } b
238e3 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  r;.    struct OP
238e4 5f 49 64 78 49 6e 73 65 72 74 5f 73 74 61 63 6b  _IdxInsert_stack
238e5 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
238e6 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
238e7 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43      BtCursor *pC
238e8 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  rsr;.      int n
238e9 4b 65 79 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74  Key;.      const
238ea 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 20 20 20   char *zKey;.   
238eb 20 7d 20 62 73 3b 0a 20 20 20 20 73 74 72 75 63   } bs;.    struc
238ec 74 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 5f 73  t OP_IdxDelete_s
238ed 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
238ee 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
238ef 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72  ;.      BtCursor
238f0 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69   *pCrsr;.      i
238f1 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 55 6e  nt res;.      Un
238f2 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a  packedRecord r;.
238f3 20 20 20 20 7d 20 62 74 3b 0a 20 20 20 20 73 74      } bt;.    st
238f4 72 75 63 74 20 4f 50 5f 49 64 78 52 6f 77 69 64  ruct OP_IdxRowid
238f5 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
238f6 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43      BtCursor *pC
238f7 72 73 72 3b 0a 20 20 20 20 20 20 56 64 62 65 43  rsr;.      VdbeC
238f8 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20  ursor *pC;.     
238f9 20 69 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20   i64 rowid;.    
238fa 7d 20 62 75 3b 0a 20 20 20 20 73 74 72 75 63 74  } bu;.    struct
238fb 20 4f 50 5f 49 64 78 47 45 5f 73 74 61 63 6b 5f   OP_IdxGE_stack_
238fc 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62  vars {.      Vdb
238fd 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20  eCursor *pC;.   
238fe 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20     int res;.    
238ff 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
23900 20 72 3b 0a 20 20 20 20 7d 20 62 76 3b 0a 20 20   r;.    } bv;.  
23901 20 20 73 74 72 75 63 74 20 4f 50 5f 44 65 73 74    struct OP_Dest
23902 72 6f 79 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  roy_stack_vars {
23903 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 6f 76 65  .      int iMove
23904 64 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6e  d;.      int iCn
23905 74 3b 0a 20 20 20 20 20 20 56 64 62 65 20 2a 70  t;.      Vdbe *p
23906 56 64 62 65 3b 0a 20 20 20 20 20 20 69 6e 74 20  Vdbe;.      int 
23907 69 44 62 3b 0a 20 20 20 20 7d 20 62 77 3b 0a 20  iDb;.    } bw;. 
23908 20 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6c 65     struct OP_Cle
23909 61 72 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ar_stack_vars {.
2390a 20 20 20 20 20 20 69 6e 74 20 6e 43 68 61 6e 67        int nChang
2390b 65 3b 0a 20 20 20 20 7d 20 62 78 3b 0a 20 20 20  e;.    } bx;.   
2390c 20 73 74 72 75 63 74 20 4f 50 5f 43 72 65 61 74   struct OP_Creat
2390d 65 54 61 62 6c 65 5f 73 74 61 63 6b 5f 76 61 72  eTable_stack_var
2390e 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 67  s {.      int pg
2390f 6e 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 6c  no;.      int fl
23910 61 67 73 3b 0a 20 20 20 20 20 20 44 62 20 2a 70  ags;.      Db *p
23911 44 62 3b 0a 20 20 20 20 7d 20 62 79 3b 0a 20 20  Db;.    } by;.  
23912 20 20 73 74 72 75 63 74 20 4f 50 5f 50 61 72 73    struct OP_Pars
23913 65 53 63 68 65 6d 61 5f 73 74 61 63 6b 5f 76 61  eSchema_stack_va
23914 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  rs {.      int i
23915 44 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  Db;.      const 
23916 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 3b 0a 20  char *zMaster;. 
23917 20 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b       char *zSql;
23918 0a 20 20 20 20 20 20 49 6e 69 74 44 61 74 61 20  .      InitData 
23919 69 6e 69 74 44 61 74 61 3b 0a 20 20 20 20 7d 20  initData;.    } 
2391a 62 7a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  bz;.    struct O
2391b 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 5f 73 74  P_IntegrityCk_st
2391c 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
2391d 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20 20 20   int nRoot;     
2391e 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61   /* Number of ta
2391f 62 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e 20 20  bles to check.  
23920 28 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f 74 20  (Number of root 
23921 70 61 67 65 73 2e 29 20 2a 2f 0a 20 20 20 20 20  pages.) */.     
23922 20 69 6e 74 20 2a 61 52 6f 6f 74 3b 20 20 20 20   int *aRoot;    
23923 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 72 6f 6f   /* Array of roo
23924 74 70 61 67 65 20 6e 75 6d 62 65 72 73 20 66 6f  tpage numbers fo
23925 72 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20 63  r tables to be c
23926 68 65 63 6b 65 64 20 2a 2f 0a 20 20 20 20 20 20  hecked */.      
23927 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20  int j;          
23928 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
23929 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 45 72  */.      int nEr
2392a 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  r;       /* Numb
2392b 65 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 70  er of errors rep
2392c 6f 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 63  orted */.      c
2392d 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 2f  har *z;        /
2392e 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 65 72  * Text of the er
2392f 72 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a 20 20  ror report */.  
23930 20 20 20 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20      Mem *pnErr; 
23931 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
23932 6b 65 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66  keeping track of
23933 20 65 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e   errors remainin
23934 67 20 2a 2f 0a 20 20 20 20 7d 20 63 61 3b 0a 20  g */.    } ca;. 
23935 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77     struct OP_Row
23936 53 65 74 52 65 61 64 5f 73 74 61 63 6b 5f 76 61  SetRead_stack_va
23937 72 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20 76  rs {.      i64 v
23938 61 6c 3b 0a 20 20 20 20 7d 20 63 62 3b 0a 20 20  al;.    } cb;.  
23939 20 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 53    struct OP_RowS
2393a 65 74 54 65 73 74 5f 73 74 61 63 6b 5f 76 61 72  etTest_stack_var
2393b 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 53  s {.      int iS
2393c 65 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 65 78  et;.      int ex
2393d 69 73 74 73 3b 0a 20 20 20 20 7d 20 63 63 3b 0a  ists;.    } cc;.
2393e 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 50 72      struct OP_Pr
2393f 6f 67 72 61 6d 5f 73 74 61 63 6b 5f 76 61 72 73  ogram_stack_vars
23940 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4d 65   {.      int nMe
23941 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
23942 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65   /* Number of me
23943 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 20 66  mory registers f
23944 6f 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a  or sub-program *
23945 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74  /.      int nByt
23946 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
23947 2f 2a 20 42 79 74 65 73 20 6f 66 20 72 75 6e 74  /* Bytes of runt
23948 69 6d 65 20 73 70 61 63 65 20 72 65 71 75 69 72  ime space requir
23949 65 64 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72  ed for sub-progr
2394a 61 6d 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20  am */.      Mem 
2394b 2a 70 52 74 3b 20 20 20 20 20 20 20 20 20 20 20  *pRt;           
2394c 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
2394d 74 6f 20 61 6c 6c 6f 63 61 74 65 20 72 75 6e 74  to allocate runt
2394e 69 6d 65 20 73 70 61 63 65 20 2a 2f 0a 20 20 20  ime space */.   
2394f 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20     Mem *pMem;   
23950 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
23951 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  ed to iterate th
23952 72 6f 75 67 68 20 6d 65 6d 6f 72 79 20 63 65 6c  rough memory cel
23953 6c 73 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20  ls */.      Mem 
23954 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20  *pEnd;          
23955 20 20 20 20 2f 2a 20 4c 61 73 74 20 6d 65 6d 6f      /* Last memo
23956 72 79 20 63 65 6c 6c 20 69 6e 20 6e 65 77 20 61  ry cell in new a
23957 72 72 61 79 20 2a 2f 0a 20 20 20 20 20 20 56 64  rray */.      Vd
23958 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b  beFrame *pFrame;
23959 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 64 62        /* New vdb
2395a 65 20 66 72 61 6d 65 20 74 6f 20 65 78 65 63 75  e frame to execu
2395b 74 65 20 69 6e 20 2a 2f 0a 20 20 20 20 20 20 53  te in */.      S
2395c 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67  ubProgram *pProg
2395d 72 61 6d 3b 20 20 20 2f 2a 20 53 75 62 2d 70 72  ram;   /* Sub-pr
2395e 6f 67 72 61 6d 20 74 6f 20 65 78 65 63 75 74 65  ogram to execute
2395f 20 2a 2f 0a 20 20 20 20 20 20 76 6f 69 64 20 2a   */.      void *
23960 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
23961 20 20 2f 2a 20 54 6f 6b 65 6e 20 69 64 65 6e 74    /* Token ident
23962 69 66 79 69 6e 67 20 74 72 69 67 67 65 72 20 2a  ifying trigger *
23963 2f 0a 20 20 20 20 7d 20 63 64 3b 0a 20 20 20 20  /.    } cd;.    
23964 73 74 72 75 63 74 20 4f 50 5f 50 61 72 61 6d 5f  struct OP_Param_
23965 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
23966 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46     VdbeFrame *pF
23967 72 61 6d 65 3b 0a 20 20 20 20 20 20 4d 65 6d 20  rame;.      Mem 
23968 2a 70 49 6e 3b 0a 20 20 20 20 7d 20 63 65 3b 0a  *pIn;.    } ce;.
23969 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4d 65      struct OP_Me
2396a 6d 4d 61 78 5f 73 74 61 63 6b 5f 76 61 72 73 20  mMax_stack_vars 
2396b 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 6e  {.      Mem *pIn
2396c 31 3b 0a 20 20 20 20 20 20 56 64 62 65 46 72 61  1;.      VdbeFra
2396d 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 20 20  me *pFrame;.    
2396e 7d 20 63 66 3b 0a 20 20 20 20 73 74 72 75 63 74  } cf;.    struct
2396f 20 4f 50 5f 41 67 67 53 74 65 70 5f 73 74 61 63   OP_AggStep_stac
23970 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
23971 6e 74 20 6e 3b 0a 20 20 20 20 20 20 69 6e 74 20  nt n;.      int 
23972 69 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d  i;.      Mem *pM
23973 65 6d 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70  em;.      Mem *p
23974 52 65 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Rec;.      sqlit
23975 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a  e3_context ctx;.
23976 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
23977 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 20  lue **apVal;.   
23978 20 7d 20 63 67 3b 0a 20 20 20 20 73 74 72 75 63   } cg;.    struc
23979 74 20 4f 50 5f 41 67 67 46 69 6e 61 6c 5f 73 74  t OP_AggFinal_st
2397a 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
2397b 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20   Mem *pMem;.    
2397c 7d 20 63 68 3b 0a 20 20 20 20 73 74 72 75 63 74  } ch;.    struct
2397d 20 4f 50 5f 43 68 65 63 6b 70 6f 69 6e 74 5f 73   OP_Checkpoint_s
2397e 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
2397f 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
23980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23981 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
23982 72 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61  r */.      int a
23983 52 65 73 5b 33 5d 3b 20 20 20 20 20 20 20 20 20  Res[3];         
23984 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
23985 73 75 6c 74 73 20 2a 2f 0a 20 20 20 20 20 20 4d  sults */.      M
23986 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20  em *pMem;       
23987 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
23988 2a 20 57 72 69 74 65 20 72 65 73 75 6c 74 73 20  * Write results 
23989 68 65 72 65 20 2a 2f 0a 20 20 20 20 7d 20 63 69  here */.    } ci
2398a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
2398b 4a 6f 75 72 6e 61 6c 4d 6f 64 65 5f 73 74 61 63  JournalMode_stac
2398c 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42  k_vars {.      B
2398d 74 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20 20  tree *pBt;      
2398e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2398f 2a 20 42 74 72 65 65 20 74 6f 20 63 68 61 6e 67  * Btree to chang
23990 65 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 6f  e journal mode o
23991 66 20 2a 2f 0a 20 20 20 20 20 20 50 61 67 65 72  f */.      Pager
23992 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20   *pPager;       
23993 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
23994 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 77  ger associated w
23995 69 74 68 20 70 42 74 20 2a 2f 0a 20 20 20 20 20  ith pBt */.     
23996 20 69 6e 74 20 65 4e 65 77 3b 20 20 20 20 20 20   int eNew;      
23997 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23998 20 2f 2a 20 4e 65 77 20 6a 6f 75 72 6e 61 6c 20   /* New journal 
23999 6d 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e  mode */.      in
2399a 74 20 65 4f 6c 64 3b 20 20 20 20 20 20 20 20 20  t eOld;         
2399b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2399c 20 54 68 65 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c   The old journal
2399d 20 6d 6f 64 65 20 2a 2f 0a 23 69 66 6e 64 65 66   mode */.#ifndef
2399e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c   SQLITE_OMIT_WAL
2399f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
239a0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 20 20 20  r *zFilename;   
239a1 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
239a2 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  f database file 
239a3 66 6f 72 20 70 50 61 67 65 72 20 2a 2f 0a 23 65  for pPager */.#e
239a4 6e 64 69 66 0a 20 20 20 20 7d 20 63 6a 3b 0a 20  ndif.    } cj;. 
239a5 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 63     struct OP_Inc
239a6 72 56 61 63 75 75 6d 5f 73 74 61 63 6b 5f 76 61  rVacuum_stack_va
239a7 72 73 20 7b 0a 20 20 20 20 20 20 42 74 72 65 65  rs {.      Btree
239a8 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 63 6b 3b   *pBt;.    } ck;
239a9 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56  .    struct OP_V
239aa 42 65 67 69 6e 5f 73 74 61 63 6b 5f 76 61 72 73  Begin_stack_vars
239ab 20 7b 0a 20 20 20 20 20 20 56 54 61 62 6c 65 20   {.      VTable 
239ac 2a 70 56 54 61 62 3b 0a 20 20 20 20 7d 20 63 6c  *pVTab;.    } cl
239ad 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
239ae 56 4f 70 65 6e 5f 73 74 61 63 6b 5f 76 61 72 73  VOpen_stack_vars
239af 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
239b0 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 20 20 20  sor *pCur;.     
239b1 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
239b2 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f  rsor *pVtabCurso
239b3 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  r;.      sqlite3
239b4 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20  _vtab *pVtab;.  
239b5 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75      sqlite3_modu
239b6 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20  le *pModule;.   
239b7 20 7d 20 63 6d 3b 0a 20 20 20 20 73 74 72 75 63   } cm;.    struc
239b8 74 20 4f 50 5f 56 46 69 6c 74 65 72 5f 73 74 61  t OP_VFilter_sta
239b9 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
239ba 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 20 20  int nArg;.      
239bb 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 20 20  int iQuery;.    
239bc 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
239bd 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
239be 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 51 75 65  .      Mem *pQue
239bf 72 79 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70  ry;.      Mem *p
239c0 41 72 67 63 3b 0a 20 20 20 20 20 20 73 71 6c 69  Argc;.      sqli
239c1 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
239c2 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20  *pVtabCursor;.  
239c3 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
239c4 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 56   *pVtab;.      V
239c5 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b  dbeCursor *pCur;
239c6 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a  .      int res;.
239c7 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
239c8 20 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a     Mem **apArg;.
239c9 20 20 20 20 7d 20 63 6e 3b 0a 20 20 20 20 73 74      } cn;.    st
239ca 72 75 63 74 20 4f 50 5f 56 43 6f 6c 75 6d 6e 5f  ruct OP_VColumn_
239cb 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
239cc 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20     sqlite3_vtab 
239cd 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f  *pVtab;.      co
239ce 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
239cf 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20  le *pModule;.   
239d0 20 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20     Mem *pDest;. 
239d1 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e       sqlite3_con
239d2 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 0a 20  text sContext;. 
239d3 20 20 20 7d 20 63 6f 3b 0a 20 20 20 20 73 74 72     } co;.    str
239d4 75 63 74 20 4f 50 5f 56 4e 65 78 74 5f 73 74 61  uct OP_VNext_sta
239d5 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
239d6 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
239d7 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74  tab;.      const
239d8 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
239d9 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20  *pModule;.      
239da 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 56  int res;.      V
239db 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b  dbeCursor *pCur;
239dc 0a 20 20 20 20 7d 20 63 70 3b 0a 20 20 20 20 73  .    } cp;.    s
239dd 74 72 75 63 74 20 4f 50 5f 56 52 65 6e 61 6d 65  truct OP_VRename
239de 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
239df 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
239e0 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 4d   *pVtab;.      M
239e1 65 6d 20 2a 70 4e 61 6d 65 3b 0a 20 20 20 20 7d  em *pName;.    }
239e2 20 63 71 3b 0a 20 20 20 20 73 74 72 75 63 74 20   cq;.    struct 
239e3 4f 50 5f 56 55 70 64 61 74 65 5f 73 74 61 63 6b  OP_VUpdate_stack
239e4 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 73 71  _vars {.      sq
239e5 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
239e6 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  b;.      sqlite3
239e7 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
239e8 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 41 72 67  ;.      int nArg
239e9 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20  ;.      int i;. 
239ea 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
239eb 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 20 20 4d  4 rowid;.      M
239ec 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20 20 20  em **apArg;.    
239ed 20 20 4d 65 6d 20 2a 70 58 3b 0a 20 20 20 20 7d    Mem *pX;.    }
239ee 20 63 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20   cr;.    struct 
239ef 4f 50 5f 54 72 61 63 65 5f 73 74 61 63 6b 5f 76  OP_Trace_stack_v
239f0 61 72 73 20 7b 0a 20 20 20 20 20 20 63 68 61 72  ars {.      char
239f1 20 2a 7a 54 72 61 63 65 3b 0a 20 20 20 20 20 20   *zTrace;.      
239f2 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 7d 20 63  char *z;.    } c
239f3 73 3b 0a 20 20 7d 20 75 3b 0a 20 20 2f 2a 20 45  s;.  } u;.  /* E
239f4 6e 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  nd automatically
239f5 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 0a   generated code.
239f6 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a    **************
239f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
239f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
239f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
239fa 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 61 73 73 65 72  ******/..  asser
239fb 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  t( p->magic==VDB
239fc 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 3b 20 20  E_MAGIC_RUN );  
239fd 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  /* sqlite3_step(
239fe 29 20 76 65 72 69 66 69 65 73 20 74 68 69 73 20  ) verifies this 
239ff 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
23a00 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20  Enter(p);.  if( 
23a01 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  p->rc==SQLITE_NO
23a02 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  MEM ){.    /* Th
23a03 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61 20  is happens if a 
23a04 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 20  malloc() inside 
23a05 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
23a06 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20  3_column_text() 
23a07 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  or.    ** sqlite
23a08 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
23a09 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20  ) failed.  */.  
23a0a 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
23a0b 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e   }.  assert( p->
23a0c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
23a0d 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42   p->rc==SQLITE_B
23a0e 55 53 59 20 29 3b 0a 20 20 70 2d 3e 72 63 20 3d  USY );.  p->rc =
23a0f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73   SQLITE_OK;.  as
23a10 73 65 72 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e  sert( p->explain
23a11 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 70 52 65 73  ==0 );.  p->pRes
23a12 75 6c 74 53 65 74 20 3d 20 30 3b 0a 20 20 64 62  ultSet = 0;.  db
23a13 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 6e 42  ->busyHandler.nB
23a14 75 73 79 20 3d 20 30 3b 0a 20 20 43 48 45 43 4b  usy = 0;.  CHECK
23a15 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a  _FOR_INTERRUPT;.
23a16 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54    sqlite3VdbeIOT
23a17 72 61 63 65 53 71 6c 28 70 29 3b 0a 23 69 66 6e  raceSql(p);.#ifn
23a18 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23a19 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43  PROGRESS_CALLBAC
23a1a 4b 0a 20 20 63 68 65 63 6b 50 72 6f 67 72 65 73  K.  checkProgres
23a1b 73 20 3d 20 64 62 2d 3e 78 50 72 6f 67 72 65 73  s = db->xProgres
23a1c 73 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66  s!=0;.#endif.#if
23a1d 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
23a1e 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42  .  sqlite3BeginB
23a1f 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
23a20 20 69 66 28 20 70 2d 3e 70 63 3d 3d 30 20 20 26   if( p->pc==0  &
23a21 26 20 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20  & (p->db->flags 
23a22 26 20 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73  & SQLITE_VdbeLis
23a23 74 69 6e 67 29 21 3d 30 20 29 7b 0a 20 20 20 20  ting)!=0 ){.    
23a24 69 6e 74 20 69 3b 0a 20 20 20 20 70 72 69 6e 74  int i;.    print
23a25 66 28 22 56 44 42 45 20 50 72 6f 67 72 61 6d 20  f("VDBE Program 
23a26 4c 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20  Listing:\n");.  
23a27 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
23a28 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 66 6f  ntSql(p);.    fo
23a29 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
23a2a 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c   i++){.      sql
23a2b 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28  ite3VdbePrintOp(
23a2c 73 74 64 6f 75 74 2c 20 69 2c 20 26 61 4f 70 5b  stdout, i, &aOp[
23a2d 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  i]);.    }.  }. 
23a2e 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
23a2f 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64 69  nMalloc();.#endi
23a30 66 0a 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 63  f.  for(pc=p->pc
23a31 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b  ; rc==SQLITE_OK;
23a32 20 70 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65   pc++){.    asse
23a33 72 74 28 20 70 63 3e 3d 30 20 26 26 20 70 63 3c  rt( pc>=0 && pc<
23a34 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69 66  p->nOp );.    if
23a35 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
23a36 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  ed ) goto no_mem
23a37 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52  ;.#ifdef VDBE_PR
23a38 4f 46 49 4c 45 0a 20 20 20 20 6f 72 69 67 50 63  OFILE.    origPc
23a39 20 3d 20 70 63 3b 0a 20 20 20 20 73 74 61 72 74   = pc;.    start
23a3a 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65   = sqlite3Hwtime
23a3b 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70  ();.#endif.    p
23a3c 4f 70 20 3d 20 26 61 4f 70 5b 70 63 5d 3b 0a 0a  Op = &aOp[pc];..
23a3d 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c 6f      /* Only allo
23a3e 77 20 74 72 61 63 69 6e 67 20 69 66 20 53 51 4c  w tracing if SQL
23a3f 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 66  ITE_DEBUG is def
23a40 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66  ined..    */.#if
23a41 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
23a42 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63  .    if( p->trac
23a43 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  e ){.      if( p
23a44 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  c==0 ){.        
23a45 70 72 69 6e 74 66 28 22 56 44 42 45 20 45 78 65  printf("VDBE Exe
23a46 63 75 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e 22  cution Trace:\n"
23a47 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
23a48 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 70  e3VdbePrintSql(p
23a49 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
23a4a 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
23a4b 74 4f 70 28 70 2d 3e 74 72 61 63 65 2c 20 70 63  tOp(p->trace, pc
23a4c 2c 20 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 23 65  , pOp);.    }.#e
23a4d 6e 64 69 66 0a 20 20 20 20 20 20 0a 0a 20 20 20  ndif.      ..   
23a4e 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65   /* Check to see
23a4f 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73   if we need to s
23a50 69 6d 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72  imulate an inter
23a51 72 75 70 74 2e 20 20 54 68 69 73 20 6f 6e 6c 79  rupt.  This only
23a52 20 68 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20   happens.    ** 
23a53 69 66 20 77 65 20 68 61 76 65 20 61 20 73 70 65  if we have a spe
23a54 63 69 61 6c 20 74 65 73 74 20 62 75 69 6c 64 2e  cial test build.
23a55 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  .    */.#ifdef S
23a56 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 69  QLITE_TEST.    i
23a57 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  f( sqlite3_inter
23a58 72 75 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a  rupt_count>0 ){.
23a59 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
23a5a 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b  terrupt_count--;
23a5b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
23a5c 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75  e3_interrupt_cou
23a5d 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  nt==0 ){.       
23a5e 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
23a5f 70 74 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a  pt(db);.      }.
23a60 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69      }.#endif..#i
23a61 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
23a62 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42  T_PROGRESS_CALLB
23a63 41 43 4b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20  ACK.    /* Call 
23a64 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
23a65 6c 62 61 63 6b 20 69 66 20 69 74 20 69 73 20 63  lback if it is c
23a66 6f 6e 66 69 67 75 72 65 64 20 61 6e 64 20 74 68  onfigured and th
23a67 65 20 72 65 71 75 69 72 65 64 20 6e 75 6d 62 65  e required numbe
23a68 72 0a 20 20 20 20 2a 2a 20 6f 66 20 56 44 42 45  r.    ** of VDBE
23a69 20 6f 70 73 20 68 61 76 65 20 62 65 65 6e 20 65   ops have been e
23a6a 78 65 63 75 74 65 64 20 28 65 69 74 68 65 72 20  xecuted (either 
23a6b 73 69 6e 63 65 20 74 68 69 73 20 69 6e 76 6f 63  since this invoc
23a6c 61 74 69 6f 6e 20 6f 66 0a 20 20 20 20 2a 2a 20  ation of.    ** 
23a6d 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28  sqlite3VdbeExec(
23a6e 29 20 6f 72 20 73 69 6e 63 65 20 6c 61 73 74 20  ) or since last 
23a6f 74 69 6d 65 20 74 68 65 20 70 72 6f 67 72 65 73  time the progres
23a70 73 20 63 61 6c 6c 62 61 63 6b 20 77 61 73 20 63  s callback was c
23a71 61 6c 6c 65 64 29 2e 0a 20 20 20 20 2a 2a 20 49  alled)..    ** I
23a72 66 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63  f the progress c
23a73 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20  allback returns 
23a74 6e 6f 6e 2d 7a 65 72 6f 2c 20 65 78 69 74 20 74  non-zero, exit t
23a75 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  he virtual machi
23a76 6e 65 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61  ne with.    ** a
23a77 20 72 65 74 75 72 6e 20 63 6f 64 65 20 53 51 4c   return code SQL
23a78 49 54 45 5f 41 42 4f 52 54 2e 0a 20 20 20 20 2a  ITE_ABORT..    *
23a79 2f 0a 20 20 20 20 69 66 28 20 63 68 65 63 6b 50  /.    if( checkP
23a7a 72 6f 67 72 65 73 73 20 29 7b 0a 20 20 20 20 20  rogress ){.     
23a7b 20 69 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65   if( db->nProgre
23a7c 73 73 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73  ssOps==nProgress
23a7d 4f 70 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69  Ops ){.        i
23a7e 6e 74 20 70 72 63 3b 0a 20 20 20 20 20 20 20 20  nt prc;.        
23a7f 70 72 63 20 3d 20 64 62 2d 3e 78 50 72 6f 67 72  prc = db->xProgr
23a80 65 73 73 28 64 62 2d 3e 70 50 72 6f 67 72 65 73  ess(db->pProgres
23a81 73 41 72 67 29 3b 0a 20 20 20 20 20 20 20 20 69  sArg);.        i
23a82 66 28 20 70 72 63 21 3d 30 20 29 7b 0a 20 20 20  f( prc!=0 ){.   
23a83 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
23a84 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20  TE_INTERRUPT;.  
23a85 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62          goto vdb
23a86 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 20 20  e_error_halt;.  
23a87 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
23a88 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30  nProgressOps = 0
23a89 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
23a8a 6e 50 72 6f 67 72 65 73 73 4f 70 73 2b 2b 3b 0a  nProgressOps++;.
23a8b 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
23a8c 20 20 2f 2a 20 4f 6e 20 61 6e 79 20 6f 70 63 6f    /* On any opco
23a8d 64 65 20 77 69 74 68 20 74 68 65 20 22 6f 75 74  de with the "out
23a8e 32 2d 70 72 65 72 65 6c 65 61 73 65 22 20 74 61  2-prerelease" ta
23a8f 67 2c 20 66 72 65 65 20 61 6e 79 0a 20 20 20 20  g, free any.    
23a90 2a 2a 20 65 78 74 65 72 6e 61 6c 20 61 6c 6c 6f  ** external allo
23a91 63 61 74 69 6f 6e 73 20 6f 75 74 20 6f 66 20 6d  cations out of m
23a92 65 6d 5b 70 32 5d 20 61 6e 64 20 73 65 74 20 6d  em[p2] and set m
23a93 65 6d 5b 70 32 5d 20 74 6f 20 62 65 0a 20 20 20  em[p2] to be.   
23a94 20 2a 2a 20 61 6e 20 75 6e 64 65 66 69 6e 65 64   ** an undefined
23a95 20 69 6e 74 65 67 65 72 2e 20 20 4f 70 63 6f 64   integer.  Opcod
23a96 65 73 20 77 69 6c 6c 20 65 69 74 68 65 72 20 66  es will either f
23a97 69 6c 6c 20 69 6e 20 74 68 65 20 69 6e 74 65 67  ill in the integ
23a98 65 72 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 20  er.    ** value 
23a99 6f 72 20 63 6f 6e 76 65 72 74 20 6d 65 6d 5b 70  or convert mem[p
23a9a 32 5d 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e  2] to a differen
23a9b 74 20 74 79 70 65 2e 0a 20 20 20 20 2a 2f 0a 20  t type..    */. 
23a9c 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
23a9d 6f 70 66 6c 61 67 73 3d 3d 73 71 6c 69 74 65 33  opflags==sqlite3
23a9e 4f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 70  OpcodeProperty[p
23a9f 4f 70 2d 3e 6f 70 63 6f 64 65 5d 20 29 3b 0a 20  Op->opcode] );. 
23aa0 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 66 6c     if( pOp->opfl
23aa1 61 67 73 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32  ags & OPFLG_OUT2
23aa2 5f 50 52 45 52 45 4c 45 41 53 45 20 29 7b 0a 20  _PRERELEASE ){. 
23aa3 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
23aa4 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20  ->p2>0 );.      
23aa5 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c  assert( pOp->p2<
23aa6 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20  =p->nMem );.    
23aa7 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70    pOut = &aMem[p
23aa8 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 6d  Op->p2];.      m
23aa9 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65 28  emAboutToChange(
23aaa 70 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20  p, pOut);.      
23aab 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
23aac 4f 75 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 74  Out);.      pOut
23aad 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
23aae 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  t;.    }..    /*
23aaf 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   Sanity checking
23ab0 20 6f 6e 20 6f 74 68 65 72 20 6f 70 65 72 61 6e   on other operan
23ab1 64 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  ds */.#ifdef SQL
23ab2 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66  ITE_DEBUG.    if
23ab3 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20  ( (pOp->opflags 
23ab4 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21 3d 30 20  & OPFLG_IN1)!=0 
23ab5 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
23ab6 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20   pOp->p1>0 );.  
23ab7 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
23ab8 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p1<=p->nMem );.
23ab9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d 65        assert( me
23aba 6d 49 73 56 61 6c 69 64 28 26 61 4d 65 6d 5b 70  mIsValid(&aMem[p
23abb 4f 70 2d 3e 70 31 5d 29 20 29 3b 0a 20 20 20 20  Op->p1]) );.    
23abc 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
23abd 28 70 4f 70 2d 3e 70 31 2c 20 26 61 4d 65 6d 5b  (pOp->p1, &aMem[
23abe 70 4f 70 2d 3e 70 31 5d 29 3b 0a 20 20 20 20 7d  pOp->p1]);.    }
23abf 0a 20 20 20 20 69 66 28 20 28 70 4f 70 2d 3e 6f  .    if( (pOp->o
23ac0 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 5f 49  pflags & OPFLG_I
23ac1 4e 32 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  N2)!=0 ){.      
23ac2 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e  assert( pOp->p2>
23ac3 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
23ac4 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e  t( pOp->p2<=p->n
23ac5 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 61 73 73  Mem );.      ass
23ac6 65 72 74 28 20 6d 65 6d 49 73 56 61 6c 69 64 28  ert( memIsValid(
23ac7 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 29 20  &aMem[pOp->p2]) 
23ac8 29 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45  );.      REGISTE
23ac9 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c  R_TRACE(pOp->p2,
23aca 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 29   &aMem[pOp->p2])
23acb 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
23acc 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20  (pOp->opflags & 
23acd 4f 50 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b  OPFLG_IN3)!=0 ){
23ace 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
23acf 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20  Op->p3>0 );.    
23ad0 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
23ad1 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
23ad2 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 49      assert( memI
23ad3 73 56 61 6c 69 64 28 26 61 4d 65 6d 5b 70 4f 70  sValid(&aMem[pOp
23ad4 2d 3e 70 33 5d 29 20 29 3b 0a 20 20 20 20 20 20  ->p3]) );.      
23ad5 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
23ad6 4f 70 2d 3e 70 33 2c 20 26 61 4d 65 6d 5b 70 4f  Op->p3, &aMem[pO
23ad7 70 2d 3e 70 33 5d 29 3b 0a 20 20 20 20 7d 0a 20  p->p3]);.    }. 
23ad8 20 20 20 69 66 28 20 28 70 4f 70 2d 3e 6f 70 66     if( (pOp->opf
23ad9 6c 61 67 73 20 26 20 4f 50 46 4c 47 5f 4f 55 54  lags & OPFLG_OUT
23ada 32 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61  2)!=0 ){.      a
23adb 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30  ssert( pOp->p2>0
23adc 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
23add 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d  ( pOp->p2<=p->nM
23ade 65 6d 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 41  em );.      memA
23adf 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 70 2c 20  boutToChange(p, 
23ae0 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 29 3b  &aMem[pOp->p2]);
23ae1 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28  .    }.    if( (
23ae2 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20 4f  pOp->opflags & O
23ae3 50 46 4c 47 5f 4f 55 54 33 29 21 3d 30 20 29 7b  PFLG_OUT3)!=0 ){
23ae4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
23ae5 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20  Op->p3>0 );.    
23ae6 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
23ae7 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
23ae8 20 20 20 20 6d 65 6d 41 62 6f 75 74 54 6f 43 68      memAboutToCh
23ae9 61 6e 67 65 28 70 2c 20 26 61 4d 65 6d 5b 70 4f  ange(p, &aMem[pO
23aea 70 2d 3e 70 33 5d 29 3b 0a 20 20 20 20 7d 0a 23  p->p3]);.    }.#
23aeb 65 6e 64 69 66 0a 20 20 0a 20 20 20 20 73 77 69  endif.  .    swi
23aec 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  tch( pOp->opcode
23aed 20 29 7b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ){../**********
23aee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23aef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23af1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23af2 2a 2a 2a 0a 2a 2a 20 57 68 61 74 20 66 6f 6c 6c  ***.** What foll
23af3 6f 77 73 20 69 73 20 61 20 6d 61 73 73 69 76 65  ows is a massive
23af4 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e   switch statemen
23af5 74 20 77 68 65 72 65 20 65 61 63 68 20 63 61 73  t where each cas
23af6 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 0a 2a  e implements a.*
23af7 2a 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 72  * separate instr
23af8 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 76 69  uction in the vi
23af9 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20  rtual machine.  
23afa 49 66 20 77 65 20 66 6f 6c 6c 6f 77 20 74 68 65  If we follow the
23afb 20 75 73 75 61 6c 0a 2a 2a 20 69 6e 64 65 6e 74   usual.** indent
23afc 61 74 69 6f 6e 20 63 6f 6e 76 65 6e 74 69 6f 6e  ation convention
23afd 73 2c 20 65 61 63 68 20 63 61 73 65 20 73 68 6f  s, each case sho
23afe 75 6c 64 20 62 65 20 69 6e 64 65 6e 74 65 64 20  uld be indented 
23aff 62 79 20 36 20 73 70 61 63 65 73 2e 20 20 42 75  by 6 spaces.  Bu
23b00 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 20 6c  t.** that is a l
23b01 6f 74 20 6f 66 20 77 61 73 74 65 64 20 73 70 61  ot of wasted spa
23b02 63 65 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 6d  ce on the left m
23b03 61 72 67 69 6e 2e 20 20 53 6f 20 74 68 65 20 63  argin.  So the c
23b04 6f 64 65 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68  ode within.** th
23b05 65 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65  e switch stateme
23b06 6e 74 20 77 69 6c 6c 20 62 72 65 61 6b 20 77 69  nt will break wi
23b07 74 68 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 61 6e  th convention an
23b08 64 20 62 65 20 66 6c 75 73 68 2d 6c 65 66 74 2e  d be flush-left.
23b09 20 41 6e 6f 74 68 65 72 0a 2a 2a 20 62 69 67 20   Another.** big 
23b0a 63 6f 6d 6d 65 6e 74 20 28 73 69 6d 69 6c 61 72  comment (similar
23b0b 20 74 6f 20 74 68 69 73 20 6f 6e 65 29 20 77 69   to this one) wi
23b0c 6c 6c 20 6d 61 72 6b 20 74 68 65 20 70 6f 69 6e  ll mark the poin
23b0d 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 77 68  t in the code wh
23b0e 65 72 65 0a 2a 2a 20 77 65 20 74 72 61 6e 73 69  ere.** we transi
23b0f 74 69 6f 6e 20 62 61 63 6b 20 74 6f 20 6e 6f 72  tion back to nor
23b10 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f 6e 2e  mal indentation.
23b11 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61  .**.** The forma
23b12 74 74 69 6e 67 20 6f 66 20 65 61 63 68 20 63 61  tting of each ca
23b13 73 65 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e  se is important.
23b14 20 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 66    The makefile f
23b15 6f 72 20 53 51 4c 69 74 65 0a 2a 2a 20 67 65 6e  or SQLite.** gen
23b16 65 72 61 74 65 73 20 74 77 6f 20 43 20 66 69 6c  erates two C fil
23b17 65 73 20 22 6f 70 63 6f 64 65 73 2e 68 22 20 61  es "opcodes.h" a
23b18 6e 64 20 22 6f 70 63 6f 64 65 73 2e 63 22 20 62  nd "opcodes.c" b
23b19 79 20 73 63 61 6e 6e 69 6e 67 20 74 68 69 73 0a  y scanning this.
23b1a 2a 2a 20 66 69 6c 65 20 6c 6f 6f 6b 69 6e 67 20  ** file looking 
23b1b 66 6f 72 20 6c 69 6e 65 73 20 74 68 61 74 20 62  for lines that b
23b1c 65 67 69 6e 20 77 69 74 68 20 22 63 61 73 65 20  egin with "case 
23b1d 4f 50 5f 22 2e 20 20 54 68 65 20 6f 70 63 6f 64  OP_".  The opcod
23b1e 65 73 2e 68 20 66 69 6c 65 73 0a 2a 2a 20 77 69  es.h files.** wi
23b1f 6c 6c 20 62 65 20 66 69 6c 6c 65 64 20 77 69 74  ll be filled wit
23b20 68 20 23 64 65 66 69 6e 65 73 20 74 68 61 74 20  h #defines that 
23b21 67 69 76 65 20 75 6e 69 71 75 65 20 69 6e 74 65  give unique inte
23b22 67 65 72 20 76 61 6c 75 65 73 20 74 6f 20 65 61  ger values to ea
23b23 63 68 0a 2a 2a 20 6f 70 63 6f 64 65 20 61 6e 64  ch.** opcode and
23b24 20 74 68 65 20 6f 70 63 6f 64 65 73 2e 63 20 66   the opcodes.c f
23b25 69 6c 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69  ile is filled wi
23b26 74 68 20 61 6e 20 61 72 72 61 79 20 6f 66 20 73  th an array of s
23b27 74 72 69 6e 67 73 20 77 68 65 72 65 0a 2a 2a 20  trings where.** 
23b28 65 61 63 68 20 73 74 72 69 6e 67 20 69 73 20 74  each string is t
23b29 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65  he symbolic name
23b2a 20 66 6f 72 20 74 68 65 20 63 6f 72 72 65 73 70   for the corresp
23b2b 6f 6e 64 69 6e 67 20 6f 70 63 6f 64 65 2e 20 20  onding opcode.  
23b2c 49 66 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 73  If the.** case s
23b2d 74 61 74 65 6d 65 6e 74 20 69 73 20 66 6f 6c 6c  tatement is foll
23b2e 6f 77 65 64 20 62 79 20 61 20 63 6f 6d 6d 65 6e  owed by a commen
23b2f 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 2f  t of the form "/
23b30 23 20 73 61 6d 65 20 61 73 20 2e 2e 2e 20 23 2f  # same as ... #/
23b31 22 0a 2a 2a 20 74 68 61 74 20 63 6f 6d 6d 65 6e  ".** that commen
23b32 74 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74  t is used to det
23b33 65 72 6d 69 6e 65 20 74 68 65 20 70 61 72 74 69  ermine the parti
23b34 63 75 6c 61 72 20 76 61 6c 75 65 20 6f 66 20 74  cular value of t
23b35 68 65 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  he opcode..**.**
23b36 20 4f 74 68 65 72 20 6b 65 79 77 6f 72 64 73 20   Other keywords 
23b37 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74  in the comment t
23b38 68 61 74 20 66 6f 6c 6c 6f 77 73 20 65 61 63 68  hat follows each
23b39 20 63 61 73 65 20 61 72 65 20 75 73 65 64 20 74   case are used t
23b3a 6f 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 20 74  o.** construct t
23b3b 68 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c  he OPFLG_INITIAL
23b3c 49 5a 45 52 20 76 61 6c 75 65 20 74 68 61 74 20  IZER value that 
23b3d 69 6e 69 74 69 61 6c 69 7a 65 73 20 6f 70 63 6f  initializes opco
23b3e 64 65 50 72 6f 70 65 72 74 79 5b 5d 2e 0a 2a 2a  deProperty[]..**
23b3f 20 4b 65 79 77 6f 72 64 73 20 69 6e 63 6c 75 64   Keywords includ
23b40 65 3a 20 69 6e 31 2c 20 69 6e 32 2c 20 69 6e 33  e: in1, in2, in3
23b41 2c 20 6f 75 74 32 5f 70 72 65 72 65 6c 65 61 73  , out2_prereleas
23b42 65 2c 20 6f 75 74 32 2c 20 6f 75 74 33 2e 20 20  e, out2, out3.  
23b43 53 65 65 0a 2a 2a 20 74 68 65 20 6d 6b 6f 70 63  See.** the mkopc
23b44 6f 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20  odeh.awk script 
23b45 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
23b46 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nformation..**.*
23b47 2a 20 44 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20  * Documentation 
23b48 61 62 6f 75 74 20 56 44 42 45 20 6f 70 63 6f 64  about VDBE opcod
23b49 65 73 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  es is generated 
23b4a 62 79 20 73 63 61 6e 6e 69 6e 67 20 74 68 69 73  by scanning this
23b4b 20 66 69 6c 65 0a 2a 2a 20 66 6f 72 20 6c 69 6e   file.** for lin
23b4c 65 73 20 6f 66 20 74 68 61 74 20 63 6f 6e 74 61  es of that conta
23b4d 69 6e 20 22 4f 70 63 6f 64 65 3a 22 2e 20 20 54  in "Opcode:".  T
23b4e 68 61 74 20 6c 69 6e 65 20 61 6e 64 20 61 6c 6c  hat line and all
23b4f 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63   subsequent.** c
23b50 6f 6d 6d 65 6e 74 20 6c 69 6e 65 73 20 61 72 65  omment lines are
23b51 20 75 73 65 64 20 69 6e 20 74 68 65 20 67 65 6e   used in the gen
23b52 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f  eration of the o
23b53 70 63 6f 64 65 2e 68 74 6d 6c 20 64 6f 63 75 6d  pcode.html docum
23b54 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 66 69 6c 65  entation.** file
23b55 2e 0a 2a 2a 0a 2a 2a 20 53 55 4d 4d 41 52 59 3a  ..**.** SUMMARY:
23b56 0a 2a 2a 0a 2a 2a 20 20 20 20 20 46 6f 72 6d 61  .**.**     Forma
23b57 74 74 69 6e 67 20 69 73 20 69 6d 70 6f 72 74 61  tting is importa
23b58 6e 74 20 74 6f 20 73 63 72 69 70 74 73 20 74 68  nt to scripts th
23b59 61 74 20 73 63 61 6e 20 74 68 69 73 20 66 69 6c  at scan this fil
23b5a 65 2e 0a 2a 2a 20 20 20 20 20 44 6f 20 6e 6f 74  e..**     Do not
23b5b 20 64 65 76 69 61 74 65 20 66 72 6f 6d 20 74 68   deviate from th
23b5c 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 73 74 79  e formatting sty
23b5d 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  le currently in 
23b5e 75 73 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  use..**.********
23b5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23b61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23b62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23b63 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  *****/../* Opcod
23b64 65 3a 20 20 47 6f 74 6f 20 2a 20 50 32 20 2a 20  e:  Goto * P2 * 
23b65 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6e 20 75 6e 63  * *.**.** An unc
23b66 6f 6e 64 69 74 69 6f 6e 61 6c 20 6a 75 6d 70 20  onditional jump 
23b67 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e 0a 2a  to address P2..*
23b68 2a 20 54 68 65 20 6e 65 78 74 20 69 6e 73 74 72  * The next instr
23b69 75 63 74 69 6f 6e 20 65 78 65 63 75 74 65 64 20  uction executed 
23b6a 77 69 6c 6c 20 62 65 20 0a 2a 2a 20 74 68 65 20  will be .** the 
23b6b 6f 6e 65 20 61 74 20 69 6e 64 65 78 20 50 32 20  one at index P2 
23b6c 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69  from the beginni
23b6d 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f  ng of.** the pro
23b6e 67 72 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  gram..*/.case OP
23b6f 5f 47 6f 74 6f 3a 20 7b 20 20 20 20 20 20 20 20  _Goto: {        
23b70 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a       /* jump */.
23b71 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45    CHECK_FOR_INTE
23b72 52 52 55 50 54 3b 0a 20 20 70 63 20 3d 20 70 4f  RRUPT;.  pc = pO
23b73 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 62 72 65  p->p2 - 1;.  bre
23b74 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
23b75 3a 20 20 47 6f 73 75 62 20 50 31 20 50 32 20 2a  :  Gosub P1 P2 *
23b76 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65   * *.**.** Write
23b77 20 74 68 65 20 63 75 72 72 65 6e 74 20 61 64 64   the current add
23b78 72 65 73 73 20 6f 6e 74 6f 20 72 65 67 69 73 74  ress onto regist
23b79 65 72 20 50 31 0a 2a 2a 20 61 6e 64 20 74 68 65  er P1.** and the
23b7a 6e 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73  n jump to addres
23b7b 73 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  s P2..*/.case OP
23b7c 5f 47 6f 73 75 62 3a 20 7b 20 20 20 20 20 20 20  _Gosub: {       
23b7d 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a       /* jump */.
23b7e 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
23b7f 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d  1>0 && pOp->p1<=
23b80 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e  p->nMem );.  pIn
23b81 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  1 = &aMem[pOp->p
23b82 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70  1];.  assert( (p
23b83 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
23b84 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 6d 65  _Dyn)==0 );.  me
23b85 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 70  mAboutToChange(p
23b86 2c 20 70 49 6e 31 29 3b 0a 20 20 70 49 6e 31 2d  , pIn1);.  pIn1-
23b87 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
23b88 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20  ;.  pIn1->u.i = 
23b89 70 63 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54  pc;.  REGISTER_T
23b8a 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49  RACE(pOp->p1, pI
23b8b 6e 31 29 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d  n1);.  pc = pOp-
23b8c 3e 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b  >p2 - 1;.  break
23b8d 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
23b8e 20 52 65 74 75 72 6e 20 50 31 20 2a 20 2a 20 2a   Return P1 * * *
23b8f 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f   *.**.** Jump to
23b90 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75   the next instru
23b91 63 74 69 6f 6e 20 61 66 74 65 72 20 74 68 65 20  ction after the 
23b92 61 64 64 72 65 73 73 20 69 6e 20 72 65 67 69 73  address in regis
23b93 74 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20  ter P1..*/.case 
23b94 4f 50 5f 52 65 74 75 72 6e 3a 20 7b 20 20 20 20  OP_Return: {    
23b95 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f         /* in1 */
23b96 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b  .  pIn1 = &aMem[
23b97 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
23b98 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  rt( pIn1->flags 
23b99 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 70  & MEM_Int );.  p
23b9a 63 20 3d 20 28 69 6e 74 29 70 49 6e 31 2d 3e 75  c = (int)pIn1->u
23b9b 2e 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  .i;.  break;.}..
23b9c 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 59 69 65 6c  /* Opcode:  Yiel
23b9d 64 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a  d P1 * * * *.**.
23b9e 2a 2a 20 53 77 61 70 20 74 68 65 20 70 72 6f 67  ** Swap the prog
23b9f 72 61 6d 20 63 6f 75 6e 74 65 72 20 77 69 74 68  ram counter with
23ba0 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
23ba1 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a 63 61  gister P1..*/.ca
23ba2 73 65 20 4f 50 5f 59 69 65 6c 64 3a 20 7b 20 20  se OP_Yield: {  
23ba3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31            /* in1
23ba4 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
23ba5 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
23ba6 76 65 64 20 69 6e 74 6f 20 75 2e 61 61 20 2a 2f  ved into u.aa */
23ba7 0a 20 20 69 6e 74 20 70 63 44 65 73 74 3b 0a 23  .  int pcDest;.#
23ba8 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
23ba9 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
23baa 6e 74 6f 20 75 2e 61 61 20 2a 2f 0a 20 20 70 49  nto u.aa */.  pI
23bab 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n1 = &aMem[pOp->
23bac 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28  p1];.  assert( (
23bad 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
23bae 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70  M_Dyn)==0 );.  p
23baf 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  In1->flags = MEM
23bb0 5f 49 6e 74 3b 0a 20 20 75 2e 61 61 2e 70 63 44  _Int;.  u.aa.pcD
23bb1 65 73 74 20 3d 20 28 69 6e 74 29 70 49 6e 31 2d  est = (int)pIn1-
23bb2 3e 75 2e 69 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e  >u.i;.  pIn1->u.
23bb3 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 49 53 54  i = pc;.  REGIST
23bb4 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31  ER_TRACE(pOp->p1
23bb5 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d 20  , pIn1);.  pc = 
23bb6 75 2e 61 61 2e 70 63 44 65 73 74 3b 0a 20 20 62  u.aa.pcDest;.  b
23bb7 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
23bb8 64 65 3a 20 20 48 61 6c 74 49 66 4e 75 6c 6c 20  de:  HaltIfNull 
23bb9 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
23bba 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 76  *.** Check the v
23bbb 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
23bbc 20 50 33 2e 20 20 49 66 20 69 74 20 69 73 20 4e   P3.  If it is N
23bbd 55 4c 4c 20 74 68 65 6e 20 48 61 6c 74 20 75 73  ULL then Halt us
23bbe 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  ing.** parameter
23bbf 20 50 31 2c 20 50 32 2c 20 61 6e 64 20 50 34 20   P1, P2, and P4 
23bc0 61 73 20 69 66 20 74 68 69 73 20 77 65 72 65 20  as if this were 
23bc1 61 20 48 61 6c 74 20 69 6e 73 74 72 75 63 74 69  a Halt instructi
23bc2 6f 6e 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 76  on.  If the.** v
23bc3 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
23bc4 20 50 33 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c   P3 is not NULL,
23bc5 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
23bc6 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
23bc7 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74 49 66  /.case OP_HaltIf
23bc8 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 2f 2a 20  Null: {      /* 
23bc9 69 6e 33 20 2a 2f 0a 20 20 70 49 6e 33 20 3d 20  in3 */.  pIn3 = 
23bca 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  &aMem[pOp->p3];.
23bcb 20 20 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61    if( (pIn3->fla
23bcc 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d  gs & MEM_Null)==
23bcd 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 2f 2a 20  0 ) break;.  /* 
23bce 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74  Fall through int
23bcf 6f 20 4f 50 5f 48 61 6c 74 20 2a 2f 0a 7d 0a 0a  o OP_Halt */.}..
23bd0 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61 6c 74  /* Opcode:  Halt
23bd1 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a   P1 P2 * P4 *.**
23bd2 0a 2a 2a 20 45 78 69 74 20 69 6d 6d 65 64 69 61  .** Exit immedia
23bd3 74 65 6c 79 2e 20 20 41 6c 6c 20 6f 70 65 6e 20  tely.  All open 
23bd4 63 75 72 73 6f 72 73 2c 20 65 74 63 20 61 72 65  cursors, etc are
23bd5 20 63 6c 6f 73 65 64 0a 2a 2a 20 61 75 74 6f 6d   closed.** autom
23bd6 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20  atically..**.** 
23bd7 50 31 20 69 73 20 74 68 65 20 72 65 73 75 6c 74  P1 is the result
23bd8 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 62   code returned b
23bd9 79 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29  y sqlite3_exec()
23bda 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  , sqlite3_reset(
23bdb 29 2c 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33  ),.** or sqlite3
23bdc 5f 66 69 6e 61 6c 69 7a 65 28 29 2e 20 20 46 6f  _finalize().  Fo
23bdd 72 20 61 20 6e 6f 72 6d 61 6c 20 68 61 6c 74 2c  r a normal halt,
23bde 20 74 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20   this should be 
23bdf 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29 2e 0a 2a  SQLITE_OK (0)..*
23be0 2a 20 46 6f 72 20 65 72 72 6f 72 73 2c 20 69 74  * For errors, it
23be1 20 63 61 6e 20 62 65 20 73 6f 6d 65 20 6f 74 68   can be some oth
23be2 65 72 20 76 61 6c 75 65 2e 20 20 49 66 20 50 31  er value.  If P1
23be3 21 3d 30 20 74 68 65 6e 20 50 32 20 77 69 6c 6c  !=0 then P2 will
23be4 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68   determine.** wh
23be5 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20  ether or not to 
23be6 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 63 75 72  rollback the cur
23be7 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
23be8 2e 20 20 44 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61  .  Do not rollba
23be9 63 6b 0a 2a 2a 20 69 66 20 50 32 3d 3d 4f 45 5f  ck.** if P2==OE_
23bea 46 61 69 6c 2e 20 44 6f 20 74 68 65 20 72 6f 6c  Fail. Do the rol
23beb 6c 62 61 63 6b 20 69 66 20 50 32 3d 3d 4f 45 5f  lback if P2==OE_
23bec 52 6f 6c 6c 62 61 63 6b 2e 20 20 49 66 20 50 32  Rollback.  If P2
23bed 3d 3d 4f 45 5f 41 62 6f 72 74 2c 0a 2a 2a 20 74  ==OE_Abort,.** t
23bee 68 65 6e 20 62 61 63 6b 20 6f 75 74 20 61 6c 6c  hen back out all
23bef 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 68 61   changes that ha
23bf0 76 65 20 6f 63 63 75 72 72 65 64 20 64 75 72 69  ve occurred duri
23bf1 6e 67 20 74 68 69 73 20 65 78 65 63 75 74 69 6f  ng this executio
23bf2 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42 45  n of the.** VDBE
23bf3 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20 72 6f 6c  , but do not rol
23bf4 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61  lback the transa
23bf5 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66  ction. .**.** If
23bf6 20 50 34 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 20   P4 is not null 
23bf7 74 68 65 6e 20 69 74 20 69 73 20 61 6e 20 65 72  then it is an er
23bf8 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 69  ror message stri
23bf9 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  ng..**.** There 
23bfa 69 73 20 61 6e 20 69 6d 70 6c 69 65 64 20 22 48  is an implied "H
23bfb 61 6c 74 20 30 20 30 20 30 22 20 69 6e 73 74 72  alt 0 0 0" instr
23bfc 75 63 74 69 6f 6e 20 69 6e 73 65 72 74 65 64 20  uction inserted 
23bfd 61 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 20  at the very end 
23bfe 6f 66 0a 2a 2a 20 65 76 65 72 79 20 70 72 6f 67  of.** every prog
23bff 72 61 6d 2e 20 20 53 6f 20 61 20 6a 75 6d 70 20  ram.  So a jump 
23c00 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 69 6e  past the last in
23c01 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65  struction of the
23c02 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 73 20 74   program.** is t
23c03 68 65 20 73 61 6d 65 20 61 73 20 65 78 65 63 75  he same as execu
23c04 74 69 6e 67 20 48 61 6c 74 2e 0a 2a 2f 0a 63 61  ting Halt..*/.ca
23c05 73 65 20 4f 50 5f 48 61 6c 74 3a 20 7b 0a 20 20  se OP_Halt: {.  
23c06 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 53 51 4c  if( pOp->p1==SQL
23c07 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 70 46 72  ITE_OK && p->pFr
23c08 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20 48 61  ame ){.    /* Ha
23c09 6c 74 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72  lt the sub-progr
23c0a 61 6d 2e 20 52 65 74 75 72 6e 20 63 6f 6e 74 72  am. Return contr
23c0b 6f 6c 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74  ol to the parent
23c0c 20 66 72 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 56   frame. */.    V
23c0d 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65  dbeFrame *pFrame
23c0e 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20   = p->pFrame;.  
23c0f 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20 70 46    p->pFrame = pF
23c10 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20  rame->pParent;. 
23c11 20 20 20 70 2d 3e 6e 46 72 61 6d 65 2d 2d 3b 0a     p->nFrame--;.
23c12 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
23c13 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 70 2d  etChanges(db, p-
23c14 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 70  >nChange);.    p
23c15 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46  c = sqlite3VdbeF
23c16 72 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72 61  rameRestore(pFra
23c17 6d 65 29 3b 0a 20 20 20 20 6c 61 73 74 52 6f 77  me);.    lastRow
23c18 69 64 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77  id = db->lastRow
23c19 69 64 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d  id;.    if( pOp-
23c1a 3e 70 32 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29  >p2==OE_Ignore )
23c1b 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 73 74 72  {.      /* Instr
23c1c 75 63 74 69 6f 6e 20 70 63 20 69 73 20 74 68 65  uction pc is the
23c1d 20 4f 50 5f 50 72 6f 67 72 61 6d 20 74 68 61 74   OP_Program that
23c1e 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 73 75 62   invoked the sub
23c1f 2d 70 72 6f 67 72 61 6d 20 0a 20 20 20 20 20 20  -program .      
23c20 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69  ** currently bei
23c21 6e 67 20 68 61 6c 74 65 64 2e 20 49 66 20 74 68  ng halted. If th
23c22 65 20 70 32 20 69 6e 73 74 72 75 63 74 69 6f 6e  e p2 instruction
23c23 20 6f 66 20 74 68 69 73 20 4f 50 5f 48 61 6c 74   of this OP_Halt
23c24 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 72 75  .      ** instru
23c25 63 74 69 6f 6e 20 69 73 20 73 65 74 20 74 6f 20  ction is set to 
23c26 4f 45 5f 49 67 6e 6f 72 65 2c 20 74 68 65 6e 20  OE_Ignore, then 
23c27 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20  the sub-program 
23c28 69 73 20 74 68 72 6f 77 69 6e 67 0a 20 20 20 20  is throwing.    
23c29 20 20 2a 2a 20 61 6e 20 49 47 4e 4f 52 45 20 65    ** an IGNORE e
23c2a 78 63 65 70 74 69 6f 6e 2e 20 49 6e 20 74 68 69  xception. In thi
23c2b 73 20 63 61 73 65 20 6a 75 6d 70 20 74 6f 20 74  s case jump to t
23c2c 68 65 20 61 64 64 72 65 73 73 20 73 70 65 63 69  he address speci
23c2d 66 69 65 64 0a 20 20 20 20 20 20 2a 2a 20 61 73  fied.      ** as
23c2e 20 74 68 65 20 70 32 20 6f 66 20 74 68 65 20 63   the p2 of the c
23c2f 61 6c 6c 69 6e 67 20 4f 50 5f 50 72 6f 67 72 61  alling OP_Progra
23c30 6d 2e 20 20 2a 2f 0a 20 20 20 20 20 20 70 63 20  m.  */.      pc 
23c31 3d 20 70 2d 3e 61 4f 70 5b 70 63 5d 2e 70 32 2d  = p->aOp[pc].p2-
23c32 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 4f 70  1;.    }.    aOp
23c33 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 61   = p->aOp;.    a
23c34 4d 65 6d 20 3d 20 70 2d 3e 61 4d 65 6d 3b 0a 20  Mem = p->aMem;. 
23c35 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20     break;.  }.. 
23c36 20 70 2d 3e 72 63 20 3d 20 70 4f 70 2d 3e 70 31   p->rc = pOp->p1
23c37 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69  ;.  p->errorActi
23c38 6f 6e 20 3d 20 28 75 38 29 70 4f 70 2d 3e 70 32  on = (u8)pOp->p2
23c39 3b 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a  ;.  p->pc = pc;.
23c3a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 7a 20    if( pOp->p4.z 
23c3b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
23c3c 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc!=SQLITE_OK 
23c3d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  );.    sqlite3Se
23c3e 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
23c3f 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 70  Msg, db, "%s", p
23c40 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 74  Op->p4.z);.    t
23c41 65 73 74 63 61 73 65 28 20 73 71 6c 69 74 65 33  estcase( sqlite3
23c42 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f  GlobalConfig.xLo
23c43 67 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69  g!=0 );.    sqli
23c44 74 65 33 5f 6c 6f 67 28 70 4f 70 2d 3e 70 31 2c  te3_log(pOp->p1,
23c45 20 22 61 62 6f 72 74 20 61 74 20 25 64 20 69 6e   "abort at %d in
23c46 20 5b 25 73 5d 3a 20 25 73 22 2c 20 70 63 2c 20   [%s]: %s", pc, 
23c47 70 2d 3e 7a 53 71 6c 2c 20 70 4f 70 2d 3e 70 34  p->zSql, pOp->p4
23c48 2e 7a 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  .z);.  }else if(
23c49 20 70 2d 3e 72 63 20 29 7b 0a 20 20 20 20 74 65   p->rc ){.    te
23c4a 73 74 63 61 73 65 28 20 73 71 6c 69 74 65 33 47  stcase( sqlite3G
23c4b 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f 67  lobalConfig.xLog
23c4c 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  !=0 );.    sqlit
23c4d 65 33 5f 6c 6f 67 28 70 4f 70 2d 3e 70 31 2c 20  e3_log(pOp->p1, 
23c4e 22 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c  "constraint fail
23c4f 65 64 20 61 74 20 25 64 20 69 6e 20 5b 25 73 5d  ed at %d in [%s]
23c50 22 2c 20 70 63 2c 20 70 2d 3e 7a 53 71 6c 29 3b  ", pc, p->zSql);
23c51 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69  .  }.  rc = sqli
23c52 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a  te3VdbeHalt(p);.
23c53 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
23c54 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d  LITE_BUSY || rc=
23c55 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63  =SQLITE_OK || rc
23c56 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29  ==SQLITE_ERROR )
23c57 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
23c58 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 70  TE_BUSY ){.    p
23c59 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49  ->rc = rc = SQLI
23c5a 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65  TE_BUSY;.  }else
23c5b 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  {.    assert( rc
23c5c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
23c5d 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e  ->rc==SQLITE_CON
23c5e 53 54 52 41 49 4e 54 20 29 3b 0a 20 20 20 20 61  STRAINT );.    a
23c5f 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
23c60 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6e 44 65 66  E_OK || db->nDef
23c61 65 72 72 65 64 43 6f 6e 73 3e 30 20 29 3b 0a 20  erredCons>0 );. 
23c62 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f 20     rc = p->rc ? 
23c63 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 53  SQLITE_ERROR : S
23c64 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a  QLITE_DONE;.  }.
23c65 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75    goto vdbe_retu
23c66 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  rn;.}../* Opcode
23c67 3a 20 49 6e 74 65 67 65 72 20 50 31 20 50 32 20  : Integer P1 P2 
23c68 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  * * *.**.** The 
23c69 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76  32-bit integer v
23c6a 61 6c 75 65 20 50 31 20 69 73 20 77 72 69 74 74  alue P1 is writt
23c6b 65 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  en into register
23c6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P2..*/.case OP_
23c6d 49 6e 74 65 67 65 72 3a 20 7b 20 20 20 20 20 20  Integer: {      
23c6e 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
23c6f 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d  lease */.  pOut-
23c70 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  >u.i = pOp->p1;.
23c71 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
23c72 70 63 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20 50  pcode: Int64 * P
23c73 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50  2 * P4 *.**.** P
23c74 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
23c75 6f 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67  o a 64-bit integ
23c76 65 72 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69  er value..** Wri
23c77 74 65 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e  te that value in
23c78 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  to register P2..
23c79 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36 34  */.case OP_Int64
23c7a 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  : {           /*
23c7b 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
23c7c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f   */.  assert( pO
23c7d 70 2d 3e 70 34 2e 70 49 36 34 21 3d 30 20 29 3b  p->p4.pI64!=0 );
23c7e 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 2a  .  pOut->u.i = *
23c7f 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20 20  pOp->p4.pI64;.  
23c80 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65  break;.}..#ifnde
23c81 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
23c82 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 20  OATING_POINT./* 
23c83 4f 70 63 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50  Opcode: Real * P
23c84 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50  2 * P4 *.**.** P
23c85 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
23c86 6f 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74  o a 64-bit float
23c87 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e  ing point value.
23c88 0a 2a 2a 20 57 72 69 74 65 20 74 68 61 74 20 76  .** Write that v
23c89 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74  alue into regist
23c8a 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f  er P2..*/.case O
23c8b 50 5f 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20  P_Real: {       
23c8c 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
23c8d 54 4b 5f 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70  TK_FLOAT, out2-p
23c8e 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70  rerelease */.  p
23c8f 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Out->flags = MEM
23c90 5f 52 65 61 6c 3b 0a 20 20 61 73 73 65 72 74 28  _Real;.  assert(
23c91 20 21 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a   !sqlite3IsNaN(*
23c92 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29  pOp->p4.pReal) )
23c93 3b 0a 20 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70  ;.  pOut->r = *p
23c94 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20  Op->p4.pReal;.  
23c95 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  break;.}.#endif.
23c96 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 72 69  ./* Opcode: Stri
23c97 6e 67 38 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a  ng8 * P2 * P4 *.
23c98 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20  **.** P4 points 
23c99 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61  to a nul termina
23c9a 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67  ted UTF-8 string
23c9b 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73  . This opcode is
23c9c 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 0a 2a 2a   transformed .**
23c9d 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 53 74 72 69   into an OP_Stri
23c9e 6e 67 20 62 65 66 6f 72 65 20 69 74 20 69 73 20  ng before it is 
23c9f 65 78 65 63 75 74 65 64 20 66 6f 72 20 74 68 65  executed for the
23ca0 20 66 69 72 73 74 20 74 69 6d 65 2e 0a 2a 2f 0a   first time..*/.
23ca1 63 61 73 65 20 4f 50 5f 53 74 72 69 6e 67 38 3a  case OP_String8:
23ca2 20 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61   {         /* sa
23ca3 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 2c  me as TK_STRING,
23ca4 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
23ca5 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f   */.  assert( pO
23ca6 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20  p->p4.z!=0 );.  
23ca7 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50  pOp->opcode = OP
23ca8 5f 53 74 72 69 6e 67 3b 0a 20 20 70 4f 70 2d 3e  _String;.  pOp->
23ca9 70 31 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  p1 = sqlite3Strl
23caa 65 6e 33 30 28 70 4f 70 2d 3e 70 34 2e 7a 29 3b  en30(pOp->p4.z);
23cab 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
23cac 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 69 66  _OMIT_UTF16.  if
23cad 28 20 65 6e 63 6f 64 69 6e 67 21 3d 53 51 4c 49  ( encoding!=SQLI
23cae 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 72  TE_UTF8 ){.    r
23caf 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
23cb0 65 6d 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70  emSetStr(pOut, p
23cb1 4f 70 2d 3e 70 34 2e 7a 2c 20 2d 31 2c 20 53 51  Op->p4.z, -1, SQ
23cb2 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
23cb3 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 69  E_STATIC);.    i
23cb4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 54 4f  f( rc==SQLITE_TO
23cb5 4f 42 49 47 20 29 20 67 6f 74 6f 20 74 6f 6f 5f  OBIG ) goto too_
23cb6 62 69 67 3b 0a 20 20 20 20 69 66 28 20 53 51 4c  big;.    if( SQL
23cb7 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 56  ITE_OK!=sqlite3V
23cb8 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
23cb9 67 28 70 4f 75 74 2c 20 65 6e 63 6f 64 69 6e 67  g(pOut, encoding
23cba 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  ) ) goto no_mem;
23cbb 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 75  .    assert( pOu
23cbc 74 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4f 75 74  t->zMalloc==pOut
23cbd 2d 3e 7a 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ->z );.    asser
23cbe 74 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26  t( pOut->flags &
23cbf 20 4d 45 4d 5f 44 79 6e 20 29 3b 0a 20 20 20 20   MEM_Dyn );.    
23cc0 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  pOut->zMalloc = 
23cc1 30 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61  0;.    pOut->fla
23cc2 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 61 74 69 63  gs |= MEM_Static
23cc3 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67  ;.    pOut->flag
23cc4 73 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 20  s &= ~MEM_Dyn;. 
23cc5 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79     if( pOp->p4ty
23cc6 70 65 3d 3d 50 34 5f 44 59 4e 41 4d 49 43 20 29  pe==P4_DYNAMIC )
23cc7 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
23cc8 62 46 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 70  bFree(db, pOp->p
23cc9 34 2e 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  4.z);.    }.    
23cca 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
23ccb 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 20 20 70 4f  _DYNAMIC;.    pO
23ccc 70 2d 3e 70 34 2e 7a 20 3d 20 70 4f 75 74 2d 3e  p->p4.z = pOut->
23ccd 7a 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d  z;.    pOp->p1 =
23cce 20 70 4f 75 74 2d 3e 6e 3b 0a 20 20 7d 0a 23 65   pOut->n;.  }.#e
23ccf 6e 64 69 66 0a 20 20 69 66 28 20 70 4f 70 2d 3e  ndif.  if( pOp->
23cd0 70 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  p1>db->aLimit[SQ
23cd1 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
23cd2 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  H] ){.    goto t
23cd3 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 2f 2a  oo_big;.  }.  /*
23cd4 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f   Fall through to
23cd5 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 2c 20   the next case, 
23cd6 4f 50 5f 53 74 72 69 6e 67 20 2a 2f 0a 7d 0a 20  OP_String */.}. 
23cd7 20 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 72   ./* Opcode: Str
23cd8 69 6e 67 20 50 31 20 50 32 20 2a 20 50 34 20 2a  ing P1 P2 * P4 *
23cd9 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e  .**.** The strin
23cda 67 20 76 61 6c 75 65 20 50 34 20 6f 66 20 6c 65  g value P4 of le
23cdb 6e 67 74 68 20 50 31 20 28 62 79 74 65 73 29 20  ngth P1 (bytes) 
23cdc 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  is stored in reg
23cdd 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73  ister P2..*/.cas
23cde 65 20 4f 50 5f 53 74 72 69 6e 67 3a 20 7b 20 20  e OP_String: {  
23cdf 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
23ce0 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20  prerelease */.  
23ce1 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e  assert( pOp->p4.
23ce2 7a 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e  z!=0 );.  pOut->
23ce3 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c  flags = MEM_Str|
23ce4 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 54  MEM_Static|MEM_T
23ce5 65 72 6d 3b 0a 20 20 70 4f 75 74 2d 3e 7a 20 3d  erm;.  pOut->z =
23ce6 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 70 4f   pOp->p4.z;.  pO
23ce7 75 74 2d 3e 6e 20 3d 20 70 4f 70 2d 3e 70 31 3b  ut->n = pOp->p1;
23ce8 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65  .  pOut->enc = e
23ce9 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54  ncoding;.  UPDAT
23cea 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70  E_MAX_BLOBSIZE(p
23ceb 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Out);.  break;.}
23cec 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c  ../* Opcode: Nul
23ced 6c 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  l P1 P2 P3 * *.*
23cee 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 4e 55 4c  *.** Write a NUL
23cef 4c 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73  L into registers
23cf0 20 50 32 2e 20 20 49 66 20 50 33 20 67 72 65 61   P2.  If P3 grea
23cf1 74 65 72 20 74 68 61 6e 20 50 32 2c 20 74 68 65  ter than P2, the
23cf2 6e 20 61 6c 73 6f 20 77 72 69 74 65 0a 2a 2a 20  n also write.** 
23cf3 4e 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69 73 74  NULL into regist
23cf4 65 72 20 50 33 20 61 6e 64 20 65 76 65 72 79 20  er P3 and every 
23cf5 72 65 67 69 73 74 65 72 20 69 6e 20 62 65 74 77  register in betw
23cf6 65 65 6e 20 50 32 20 61 6e 64 20 50 33 2e 20 20  een P2 and P3.  
23cf7 49 66 20 50 33 0a 2a 2a 20 69 73 20 6c 65 73 73  If P3.** is less
23cf8 20 74 68 61 6e 20 50 32 20 28 74 79 70 69 63 61   than P2 (typica
23cf9 6c 6c 79 20 50 33 20 69 73 20 7a 65 72 6f 29 20  lly P3 is zero) 
23cfa 74 68 65 6e 20 6f 6e 6c 79 20 72 65 67 69 73 74  then only regist
23cfb 65 72 20 50 32 20 69 73 0a 2a 2a 20 73 65 74 20  er P2 is.** set 
23cfc 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49  to NULL..**.** I
23cfd 66 20 74 68 65 20 50 31 20 76 61 6c 75 65 20 69  f the P1 value i
23cfe 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
23cff 20 61 6c 73 6f 20 73 65 74 20 74 68 65 20 4d 45   also set the ME
23d00 4d 5f 43 6c 65 61 72 65 64 20 66 6c 61 67 20 73  M_Cleared flag s
23d01 6f 20 74 68 61 74 0a 2a 2a 20 4e 55 4c 4c 20 76  o that.** NULL v
23d02 61 6c 75 65 73 20 77 69 6c 6c 20 6e 6f 74 20 63  alues will not c
23d03 6f 6d 70 61 72 65 20 65 71 75 61 6c 20 65 76 65  ompare equal eve
23d04 6e 20 69 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  n if SQLITE_NULL
23d05 45 51 20 69 73 20 73 65 74 20 6f 6e 0a 2a 2a 20  EQ is set on.** 
23d06 4f 50 5f 4e 65 20 6f 72 20 4f 50 5f 45 71 2e 0a  OP_Ne or OP_Eq..
23d07 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 3a  */.case OP_Null:
23d08 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
23d09 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
23d0a 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
23d0b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
23d0c 65 64 20 69 6e 74 6f 20 75 2e 61 62 20 2a 2f 0a  ed into u.ab */.
23d0d 20 20 69 6e 74 20 63 6e 74 3b 0a 20 20 75 31 36    int cnt;.  u16
23d0e 20 6e 75 6c 6c 46 6c 61 67 3b 0a 23 65 6e 64 69   nullFlag;.#endi
23d0f 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
23d10 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
23d11 75 2e 61 62 20 2a 2f 0a 20 20 75 2e 61 62 2e 63  u.ab */.  u.ab.c
23d12 6e 74 20 3d 20 70 4f 70 2d 3e 70 33 2d 70 4f 70  nt = pOp->p3-pOp
23d13 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20  ->p2;.  assert( 
23d14 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
23d15 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67   );.  pOut->flag
23d16 73 20 3d 20 75 2e 61 62 2e 6e 75 6c 6c 46 6c 61  s = u.ab.nullFla
23d17 67 20 3d 20 70 4f 70 2d 3e 70 31 20 3f 20 28 4d  g = pOp->p1 ? (M
23d18 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 43 6c 65 61  EM_Null|MEM_Clea
23d19 72 65 64 29 20 3a 20 4d 45 4d 5f 4e 75 6c 6c 3b  red) : MEM_Null;
23d1a 0a 20 20 77 68 69 6c 65 28 20 75 2e 61 62 2e 63  .  while( u.ab.c
23d1b 6e 74 3e 30 20 29 7b 0a 20 20 20 20 70 4f 75 74  nt>0 ){.    pOut
23d1c 2b 2b 3b 0a 20 20 20 20 6d 65 6d 41 62 6f 75 74  ++;.    memAbout
23d1d 54 6f 43 68 61 6e 67 65 28 70 2c 20 70 4f 75 74  ToChange(p, pOut
23d1e 29 3b 0a 20 20 20 20 56 64 62 65 4d 65 6d 52 65  );.    VdbeMemRe
23d1f 6c 65 61 73 65 28 70 4f 75 74 29 3b 0a 20 20 20  lease(pOut);.   
23d20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 75   pOut->flags = u
23d21 2e 61 62 2e 6e 75 6c 6c 46 6c 61 67 3b 0a 20 20  .ab.nullFlag;.  
23d22 20 20 75 2e 61 62 2e 63 6e 74 2d 2d 3b 0a 20 20    u.ab.cnt--;.  
23d23 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f  }.  break;.}.../
23d24 2a 20 4f 70 63 6f 64 65 3a 20 42 6c 6f 62 20 50  * Opcode: Blob P
23d25 31 20 50 32 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20  1 P2 * P4.**.** 
23d26 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 62  P4 points to a b
23d27 6c 6f 62 20 6f 66 20 64 61 74 61 20 50 31 20 62  lob of data P1 b
23d28 79 74 65 73 20 6c 6f 6e 67 2e 20 20 53 74 6f 72  ytes long.  Stor
23d29 65 20 74 68 69 73 0a 2a 2a 20 62 6c 6f 62 20 69  e this.** blob i
23d2a 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a  n register P2..*
23d2b 2f 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62 3a 20  /.case OP_Blob: 
23d2c 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
23d2d 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
23d2e 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ase */.  assert(
23d2f 20 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 51 4c 49   pOp->p1 <= SQLI
23d30 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b  TE_MAX_LENGTH );
23d31 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
23d32 6d 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 4f  mSetStr(pOut, pO
23d33 70 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d 3e 70 31  p->p4.z, pOp->p1
23d34 2c 20 30 2c 20 30 29 3b 0a 20 20 70 4f 75 74 2d  , 0, 0);.  pOut-
23d35 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b  >enc = encoding;
23d36 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  .  UPDATE_MAX_BL
23d37 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20  OBSIZE(pOut);.  
23d38 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
23d39 6f 64 65 3a 20 56 61 72 69 61 62 6c 65 20 50 31  ode: Variable P1
23d3a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a   P2 * P4 *.**.**
23d3b 20 54 72 61 6e 73 66 65 72 20 74 68 65 20 76 61   Transfer the va
23d3c 6c 75 65 73 20 6f 66 20 62 6f 75 6e 64 20 70 61  lues of bound pa
23d3d 72 61 6d 65 74 65 72 20 50 31 20 69 6e 74 6f 20  rameter P1 into 
23d3e 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a 2a  register P2.**.*
23d3f 2a 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74  * If the paramet
23d40 65 72 20 69 73 20 6e 61 6d 65 64 2c 20 74 68 65  er is named, the
23d41 6e 20 69 74 73 20 6e 61 6d 65 20 61 70 70 65 61  n its name appea
23d42 72 73 20 69 6e 20 50 34 20 61 6e 64 20 50 33 3d  rs in P4 and P3=
23d43 3d 31 2e 0a 2a 2a 20 54 68 65 20 50 34 20 76 61  =1..** The P4 va
23d44 6c 75 65 20 69 73 20 75 73 65 64 20 62 79 20 73  lue is used by s
23d45 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
23d46 6d 65 74 65 72 5f 6e 61 6d 65 28 29 2e 0a 2a 2f  meter_name()..*/
23d47 0a 63 61 73 65 20 4f 50 5f 56 61 72 69 61 62 6c  .case OP_Variabl
23d48 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  e: {            
23d49 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
23d4a 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  se */.#if 0  /* 
23d4b 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
23d4c 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20  moved into u.ac 
23d4d 2a 2f 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20  */.  Mem *pVar; 
23d4e 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 62        /* Value b
23d4f 65 69 6e 67 20 74 72 61 6e 73 66 65 72 72 65 64  eing transferred
23d50 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f   */.#endif /* lo
23d51 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
23d52 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20 2a 2f  ved into u.ac */
23d53 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
23d54 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31  >p1>0 && pOp->p1
23d55 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 61  <=p->nVar );.  a
23d56 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a  ssert( pOp->p4.z
23d57 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 34 2e 7a  ==0 || pOp->p4.z
23d58 3d 3d 70 2d 3e 61 7a 56 61 72 5b 70 4f 70 2d 3e  ==p->azVar[pOp->
23d59 70 31 2d 31 5d 20 29 3b 0a 20 20 75 2e 61 63 2e  p1-1] );.  u.ac.
23d5a 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b  pVar = &p->aVar[
23d5b 70 4f 70 2d 3e 70 31 20 2d 20 31 5d 3b 0a 20 20  pOp->p1 - 1];.  
23d5c 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
23d5d 65 6d 54 6f 6f 42 69 67 28 75 2e 61 63 2e 70 56  emTooBig(u.ac.pV
23d5e 61 72 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ar) ){.    goto 
23d5f 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 73  too_big;.  }.  s
23d60 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
23d61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 75  llowCopy(pOut, u
23d62 2e 61 63 2e 70 56 61 72 2c 20 4d 45 4d 5f 53 74  .ac.pVar, MEM_St
23d63 61 74 69 63 29 3b 0a 20 20 55 50 44 41 54 45 5f  atic);.  UPDATE_
23d64 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75  MAX_BLOBSIZE(pOu
23d65 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
23d66 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 20  /* Opcode: Move 
23d67 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
23d68 2a 2a 20 4d 6f 76 65 20 74 68 65 20 76 61 6c 75  ** Move the valu
23d69 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  es in register P
23d6a 31 2e 2e 50 31 2b 50 33 20 6f 76 65 72 20 69 6e  1..P1+P3 over in
23d6b 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20  to.** registers 
23d6c 50 32 2e 2e 50 32 2b 50 33 2e 20 20 52 65 67 69  P2..P2+P3.  Regi
23d6d 73 74 65 72 73 20 50 31 2e 2e 50 31 2b 50 33 20  sters P1..P1+P3 
23d6e 61 72 65 0a 2a 2a 20 6c 65 66 74 20 68 6f 6c 64  are.** left hold
23d6f 69 6e 67 20 61 20 4e 55 4c 4c 2e 20 20 49 74 20  ing a NULL.  It 
23d70 69 73 20 61 6e 20 65 72 72 6f 72 20 66 6f 72 20  is an error for 
23d71 72 65 67 69 73 74 65 72 20 72 61 6e 67 65 73 0a  register ranges.
23d72 2a 2a 20 50 31 2e 2e 50 31 2b 50 33 20 61 6e 64  ** P1..P1+P3 and
23d73 20 50 32 2e 2e 50 32 2b 50 33 20 74 6f 20 6f 76   P2..P2+P3 to ov
23d74 65 72 6c 61 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f  erlap..*/.case O
23d75 50 5f 4d 6f 76 65 3a 20 7b 0a 23 69 66 20 30 20  P_Move: {.#if 0 
23d76 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
23d77 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
23d78 2e 61 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  .ad */.  char *z
23d79 4d 61 6c 6c 6f 63 3b 20 20 20 2f 2a 20 48 6f 6c  Malloc;   /* Hol
23d7a 64 69 6e 67 20 76 61 72 69 61 62 6c 65 20 66 6f  ding variable fo
23d7b 72 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f  r allocated memo
23d7c 72 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20  ry */.  int n;  
23d7d 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
23d7e 65 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  er of registers 
23d7f 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a  left to copy */.
23d80 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20    int p1;       
23d81 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74     /* Register t
23d82 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20  o copy from */. 
23d83 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20   int p2;        
23d84 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f    /* Register to
23d85 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 23 65 6e 64   copy to */.#end
23d86 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
23d87 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
23d88 20 75 2e 61 64 20 2a 2f 0a 0a 20 20 75 2e 61 64   u.ad */..  u.ad
23d89 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 20 2b 20 31  .n = pOp->p3 + 1
23d8a 3b 0a 20 20 75 2e 61 64 2e 70 31 20 3d 20 70 4f  ;.  u.ad.p1 = pO
23d8b 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 64 2e 70 32  p->p1;.  u.ad.p2
23d8c 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73   = pOp->p2;.  as
23d8d 73 65 72 74 28 20 75 2e 61 64 2e 6e 3e 30 20 26  sert( u.ad.n>0 &
23d8e 26 20 75 2e 61 64 2e 70 31 3e 30 20 26 26 20 75  & u.ad.p1>0 && u
23d8f 2e 61 64 2e 70 32 3e 30 20 29 3b 0a 20 20 61 73  .ad.p2>0 );.  as
23d90 73 65 72 74 28 20 75 2e 61 64 2e 70 31 2b 75 2e  sert( u.ad.p1+u.
23d91 61 64 2e 6e 3c 3d 75 2e 61 64 2e 70 32 20 7c 7c  ad.n<=u.ad.p2 ||
23d92 20 75 2e 61 64 2e 70 32 2b 75 2e 61 64 2e 6e 3c   u.ad.p2+u.ad.n<
23d93 3d 75 2e 61 64 2e 70 31 20 29 3b 0a 0a 20 20 70  =u.ad.p1 );..  p
23d94 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 75 2e 61 64  In1 = &aMem[u.ad
23d95 2e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26  .p1];.  pOut = &
23d96 61 4d 65 6d 5b 75 2e 61 64 2e 70 32 5d 3b 0a 20  aMem[u.ad.p2];. 
23d97 20 77 68 69 6c 65 28 20 75 2e 61 64 2e 6e 2d 2d   while( u.ad.n--
23d98 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
23d99 70 4f 75 74 3c 3d 26 61 4d 65 6d 5b 70 2d 3e 6e  pOut<=&aMem[p->n
23d9a 4d 65 6d 5d 20 29 3b 0a 20 20 20 20 61 73 73 65  Mem] );.    asse
23d9b 72 74 28 20 70 49 6e 31 3c 3d 26 61 4d 65 6d 5b  rt( pIn1<=&aMem[
23d9c 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 20 20  p->nMem] );.    
23d9d 61 73 73 65 72 74 28 20 6d 65 6d 49 73 56 61 6c  assert( memIsVal
23d9e 69 64 28 70 49 6e 31 29 20 29 3b 0a 20 20 20 20  id(pIn1) );.    
23d9f 6d 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65  memAboutToChange
23da0 28 70 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 75  (p, pOut);.    u
23da1 2e 61 64 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4f  .ad.zMalloc = pO
23da2 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20  ut->zMalloc;.   
23da3 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d   pOut->zMalloc =
23da4 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   0;.    sqlite3V
23da5 64 62 65 4d 65 6d 4d 6f 76 65 28 70 4f 75 74 2c  dbeMemMove(pOut,
23da6 20 70 49 6e 31 29 3b 0a 23 69 66 64 65 66 20 53   pIn1);.#ifdef S
23da7 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20  QLITE_DEBUG.    
23da8 69 66 28 20 70 4f 75 74 2d 3e 70 53 63 6f 70 79  if( pOut->pScopy
23da9 46 72 6f 6d 3e 3d 26 61 4d 65 6d 5b 75 2e 61 64  From>=&aMem[u.ad
23daa 2e 70 31 5d 20 26 26 20 70 4f 75 74 2d 3e 70 53  .p1] && pOut->pS
23dab 63 6f 70 79 46 72 6f 6d 3c 26 61 4d 65 6d 5b 75  copyFrom<&aMem[u
23dac 2e 61 64 2e 70 31 2b 70 4f 70 2d 3e 70 33 5d 20  .ad.p1+pOp->p3] 
23dad 29 7b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  ){.      pOut->p
23dae 53 63 6f 70 79 46 72 6f 6d 20 2b 3d 20 75 2e 61  ScopyFrom += u.a
23daf 64 2e 70 31 20 2d 20 70 4f 70 2d 3e 70 32 3b 0a  d.p1 - pOp->p2;.
23db0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
23db1 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d   pIn1->zMalloc =
23db2 20 75 2e 61 64 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20   u.ad.zMalloc;. 
23db3 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43     REGISTER_TRAC
23db4 45 28 75 2e 61 64 2e 70 32 2b 2b 2c 20 70 4f 75  E(u.ad.p2++, pOu
23db5 74 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a  t);.    pIn1++;.
23db6 20 20 20 20 70 4f 75 74 2b 2b 3b 0a 20 20 7d 0a      pOut++;.  }.
23db7 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
23db8 70 63 6f 64 65 3a 20 43 6f 70 79 20 50 31 20 50  pcode: Copy P1 P
23db9 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d  2 P3 * *.**.** M
23dba 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 72 65  ake a copy of re
23dbb 67 69 73 74 65 72 73 20 50 31 2e 2e 50 31 2b 50  gisters P1..P1+P
23dbc 33 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73  3 into registers
23dbd 20 50 32 2e 2e 50 32 2b 50 33 2e 0a 2a 2a 0a 2a   P2..P2+P3..**.*
23dbe 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69  * This instructi
23dbf 6f 6e 20 6d 61 6b 65 73 20 61 20 64 65 65 70 20  on makes a deep 
23dc0 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75  copy of the valu
23dc1 65 2e 20 20 41 20 64 75 70 6c 69 63 61 74 65 0a  e.  A duplicate.
23dc2 2a 2a 20 69 73 20 6d 61 64 65 20 6f 66 20 61 6e  ** is made of an
23dc3 79 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62  y string or blob
23dc4 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65 20   constant.  See 
23dc5 61 6c 73 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a  also OP_SCopy..*
23dc6 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 70 79 3a 20  /.case OP_Copy: 
23dc7 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
23dc8 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
23dc9 64 20 69 6e 74 6f 20 75 2e 61 65 20 2a 2f 0a 20  d into u.ae */. 
23dca 20 69 6e 74 20 6e 3b 0a 23 65 6e 64 69 66 20 2f   int n;.#endif /
23dcb 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
23dcc 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
23dcd 65 20 2a 2f 0a 0a 20 20 75 2e 61 65 2e 6e 20 3d  e */..  u.ae.n =
23dce 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 70 49 6e 31   pOp->p3;.  pIn1
23dcf 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31   = &aMem[pOp->p1
23dd0 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65  ];.  pOut = &aMe
23dd1 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73  m[pOp->p2];.  as
23dd2 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49 6e 31  sert( pOut!=pIn1
23dd3 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 31 20 29   );.  while( 1 )
23dd4 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
23dd5 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28  eMemShallowCopy(
23dd6 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f  pOut, pIn1, MEM_
23dd7 45 70 68 65 6d 29 3b 0a 20 20 20 20 44 65 65 70  Ephem);.    Deep
23dd8 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f 75 74 29  hemeralize(pOut)
23dd9 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54  ;.    REGISTER_T
23dda 52 41 43 45 28 70 4f 70 2d 3e 70 32 2b 70 4f 70  RACE(pOp->p2+pOp
23ddb 2d 3e 70 33 2d 75 2e 61 65 2e 6e 2c 20 70 4f 75  ->p3-u.ae.n, pOu
23ddc 74 29 3b 0a 20 20 20 20 69 66 28 20 28 75 2e 61  t);.    if( (u.a
23ddd 65 2e 6e 2d 2d 29 3d 3d 30 20 29 20 62 72 65 61  e.n--)==0 ) brea
23dde 6b 3b 0a 20 20 20 20 70 4f 75 74 2b 2b 3b 0a 20  k;.    pOut++;. 
23ddf 20 20 20 70 49 6e 31 2b 2b 3b 0a 20 20 7d 0a 20     pIn1++;.  }. 
23de0 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
23de1 63 6f 64 65 3a 20 53 43 6f 70 79 20 50 31 20 50  code: SCopy P1 P
23de2 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61  2 * * *.**.** Ma
23de3 6b 65 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70  ke a shallow cop
23de4 79 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 31  y of register P1
23de5 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
23de6 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  2..**.** This in
23de7 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20  struction makes 
23de8 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f  a shallow copy o
23de9 66 20 74 68 65 20 76 61 6c 75 65 2e 20 20 49 66  f the value.  If
23dea 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 73   the value.** is
23deb 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f   a string or blo
23dec 62 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 70 79  b, then the copy
23ded 20 69 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74   is only a point
23dee 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 6f 72 69  er to the.** ori
23def 67 69 6e 61 6c 20 61 6e 64 20 68 65 6e 63 65 20  ginal and hence 
23df0 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  if the original 
23df1 63 68 61 6e 67 65 73 20 73 6f 20 77 69 6c 6c 20  changes so will 
23df2 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 20 57 6f 72  the copy..** Wor
23df3 73 65 2c 20 69 66 20 74 68 65 20 6f 72 69 67 69  se, if the origi
23df4 6e 61 6c 20 69 73 20 64 65 61 6c 6c 6f 63 61 74  nal is deallocat
23df5 65 64 2c 20 74 68 65 20 63 6f 70 79 20 62 65 63  ed, the copy bec
23df6 6f 6d 65 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a  omes invalid..**
23df7 20 54 68 75 73 20 74 68 65 20 70 72 6f 67 72 61   Thus the progra
23df8 6d 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65  m must guarantee
23df9 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e   that the origin
23dfa 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 6e  al will not chan
23dfb 67 65 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65  ge.** during the
23dfc 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65   lifetime of the
23dfd 20 63 6f 70 79 2e 20 20 55 73 65 20 4f 50 5f 43   copy.  Use OP_C
23dfe 6f 70 79 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f  opy to make a co
23dff 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f 70 79 2e 0a  mplete.** copy..
23e00 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 43 6f 70 79  */.case OP_SCopy
23e01 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  : {            /
23e02 2a 20 69 6e 31 2c 20 6f 75 74 32 20 2a 2f 0a 20  * in1, out2 */. 
23e03 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn1 = &aMem[pO
23e04 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d  p->p1];.  pOut =
23e05 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b   &aMem[pOp->p2];
23e06 0a 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21  .  assert( pOut!
23e07 3d 70 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74  =pIn1 );.  sqlit
23e08 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
23e09 43 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c  Copy(pOut, pIn1,
23e0a 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 23 69 66   MEM_Ephem);.#if
23e0b 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
23e0c 0a 20 20 69 66 28 20 70 4f 75 74 2d 3e 70 53 63  .  if( pOut->pSc
23e0d 6f 70 79 46 72 6f 6d 3d 3d 30 20 29 20 70 4f 75  opyFrom==0 ) pOu
23e0e 74 2d 3e 70 53 63 6f 70 79 46 72 6f 6d 20 3d 20  t->pScopyFrom = 
23e0f 70 49 6e 31 3b 0a 23 65 6e 64 69 66 0a 20 20 52  pIn1;.#endif.  R
23e10 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
23e11 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20  p->p2, pOut);.  
23e12 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
23e13 6f 64 65 3a 20 52 65 73 75 6c 74 52 6f 77 20 50  ode: ResultRow P
23e14 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
23e15 20 54 68 65 20 72 65 67 69 73 74 65 72 73 20 50   The registers P
23e16 31 20 74 68 72 6f 75 67 68 20 50 31 2b 50 32 2d  1 through P1+P2-
23e17 31 20 63 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67  1 contain a sing
23e18 6c 65 20 72 6f 77 20 6f 66 0a 2a 2a 20 72 65 73  le row of.** res
23e19 75 6c 74 73 2e 20 54 68 69 73 20 6f 70 63 6f 64  ults. This opcod
23e1a 65 20 63 61 75 73 65 73 20 74 68 65 20 73 71 6c  e causes the sql
23e1b 69 74 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c  ite3_step() call
23e1c 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a   to terminate.**
23e1d 20 77 69 74 68 20 61 6e 20 53 51 4c 49 54 45 5f   with an SQLITE_
23e1e 52 4f 57 20 72 65 74 75 72 6e 20 63 6f 64 65 20  ROW return code 
23e1f 61 6e 64 20 69 74 20 73 65 74 73 20 75 70 20 74  and it sets up t
23e20 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 0a  he sqlite3_stmt.
23e21 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20  ** structure to 
23e22 70 72 6f 76 69 64 65 20 61 63 63 65 73 73 20 74  provide access t
23e23 6f 20 74 68 65 20 74 6f 70 20 50 31 20 76 61 6c  o the top P1 val
23e24 75 65 73 20 61 73 20 74 68 65 20 72 65 73 75 6c  ues as the resul
23e25 74 0a 2a 2a 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73  t.** row..*/.cas
23e26 65 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 3a 20  e OP_ResultRow: 
23e27 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
23e28 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
23e29 64 20 69 6e 74 6f 20 75 2e 61 66 20 2a 2f 0a 20  d into u.af */. 
23e2a 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e   Mem *pMem;.  in
23e2b 74 20 69 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  t i;.#endif /* l
23e2c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
23e2d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 66 20 2a  oved into u.af *
23e2e 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  /.  assert( p->n
23e2f 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e  ResColumn==pOp->
23e30 70 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  p2 );.  assert( 
23e31 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61  pOp->p1>0 );.  a
23e32 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 2b 70  ssert( pOp->p1+p
23e33 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 2b  Op->p2<=p->nMem+
23e34 31 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  1 );..  /* If th
23e35 69 73 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73  is statement has
23e36 20 76 69 6f 6c 61 74 65 64 20 69 6d 6d 65 64 69   violated immedi
23e37 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ate foreign key 
23e38 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 64 6f 0a  constraints, do.
23e39 20 20 2a 2a 20 6e 6f 74 20 72 65 74 75 72 6e 20    ** not return 
23e3a 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
23e3b 77 73 20 6d 6f 64 69 66 69 65 64 2e 20 41 6e 64  ws modified. And
23e3c 20 64 6f 20 6e 6f 74 20 52 45 4c 45 41 53 45 20   do not RELEASE 
23e3d 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 20 20  the statement.  
23e3e 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ** transaction. 
23e3f 49 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  It needs to be r
23e40 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 2a 2f 0a  olled back.  */.
23e41 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
23e42 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64  =(rc = sqlite3Vd
23e43 62 65 43 68 65 63 6b 46 6b 28 70 2c 20 30 29 29  beCheckFk(p, 0))
23e44 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
23e45 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
23e46 5f 43 6f 75 6e 74 52 6f 77 73 20 29 3b 0a 20 20  _CountRows );.  
23e47 20 20 61 73 73 65 72 74 28 20 70 2d 3e 75 73 65    assert( p->use
23e48 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 3b 0a  sStmtJournal );.
23e49 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a      break;.  }..
23e4a 20 20 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 49    /* If the SQLI
23e4b 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 66 6c 61  TE_CountRows fla
23e4c 67 20 69 73 20 73 65 74 20 69 6e 20 73 71 6c 69  g is set in sqli
23e4d 74 65 33 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20  te3.flags mask, 
23e4e 74 68 65 6e 0a 20 20 2a 2a 20 44 4d 4c 20 73 74  then.  ** DML st
23e4f 61 74 65 6d 65 6e 74 73 20 69 6e 76 6f 6b 65 20  atements invoke 
23e50 74 68 69 73 20 6f 70 63 6f 64 65 20 74 6f 20 72  this opcode to r
23e51 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
23e52 20 6f 66 20 72 6f 77 73 0a 20 20 2a 2a 20 6d 6f   of rows.  ** mo
23e53 64 69 66 69 65 64 20 74 6f 20 74 68 65 20 75 73  dified to the us
23e54 65 72 2e 20 54 68 69 73 20 69 73 20 74 68 65 20  er. This is the 
23e55 6f 6e 6c 79 20 77 61 79 20 74 68 61 74 20 61 20  only way that a 
23e56 56 4d 20 74 68 61 74 0a 20 20 2a 2a 20 6f 70 65  VM that.  ** ope
23e57 6e 73 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  ns a statement t
23e58 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 69  ransaction may i
23e59 6e 76 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f 64  nvoke this opcod
23e5a 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20  e..  **.  ** In 
23e5b 63 61 73 65 20 74 68 69 73 20 69 73 20 73 75 63  case this is suc
23e5c 68 20 61 20 73 74 61 74 65 6d 65 6e 74 2c 20 63  h a statement, c
23e5d 6c 6f 73 65 20 61 6e 79 20 73 74 61 74 65 6d 65  lose any stateme
23e5e 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20  nt transaction. 
23e5f 20 2a 2a 20 6f 70 65 6e 65 64 20 62 79 20 74 68   ** opened by th
23e60 69 73 20 56 4d 20 62 65 66 6f 72 65 20 72 65 74  is VM before ret
23e61 75 72 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74  urning control t
23e62 6f 20 74 68 65 20 75 73 65 72 2e 20 54 68 69 73  o the user. This
23e63 20 69 73 20 74 6f 0a 20 20 2a 2a 20 65 6e 73 75   is to.  ** ensu
23e64 72 65 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e  re that statemen
23e65 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61  t-transactions a
23e66 72 65 20 61 6c 77 61 79 73 20 6e 65 73 74 65 64  re always nested
23e67 2c 20 6e 6f 74 20 6f 76 65 72 6c 61 70 70 69 6e  , not overlappin
23e68 67 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 6f  g..  ** If the o
23e69 70 65 6e 20 73 74 61 74 65 6d 65 6e 74 2d 74 72  pen statement-tr
23e6a 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74  ansaction is not
23e6b 20 63 6c 6f 73 65 64 20 68 65 72 65 2c 20 74 68   closed here, th
23e6c 65 6e 20 74 68 65 20 75 73 65 72 0a 20 20 2a 2a  en the user.  **
23e6d 20 6d 61 79 20 73 74 65 70 20 61 6e 6f 74 68 65   may step anothe
23e6e 72 20 56 4d 20 74 68 61 74 20 6f 70 65 6e 73 20  r VM that opens 
23e6f 69 74 73 20 6f 77 6e 20 73 74 61 74 65 6d 65 6e  its own statemen
23e70 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54  t transaction. T
23e71 68 69 73 0a 20 20 2a 2a 20 6d 61 79 20 6c 65 61  his.  ** may lea
23e72 64 20 74 6f 20 6f 76 65 72 6c 61 70 70 69 6e 67  d to overlapping
23e73 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
23e74 61 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20  actions..  **.  
23e75 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74  ** The statement
23e76 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
23e77 6e 65 76 65 72 20 61 20 74 6f 70 2d 6c 65 76 65  never a top-leve
23e78 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  l transaction.  
23e79 48 65 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 52  Hence.  ** the R
23e7a 45 4c 45 41 53 45 20 63 61 6c 6c 20 62 65 6c 6f  ELEASE call belo
23e7b 77 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c  w can never fail
23e7c 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
23e7d 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d   p->iStatement==
23e7e 30 20 7c 7c 20 64 62 2d 3e 66 6c 61 67 73 26 53  0 || db->flags&S
23e7f 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
23e80 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
23e81 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d  3VdbeCloseStatem
23e82 65 6e 74 28 70 2c 20 53 41 56 45 50 4f 49 4e 54  ent(p, SAVEPOINT
23e83 5f 52 45 4c 45 41 53 45 29 3b 0a 20 20 69 66 28  _RELEASE);.  if(
23e84 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54   NEVER(rc!=SQLIT
23e85 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 62 72 65  E_OK) ){.    bre
23e86 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e  ak;.  }..  /* In
23e87 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 65 70 68  validate all eph
23e88 65 6d 65 72 61 6c 20 63 75 72 73 6f 72 20 72 6f  emeral cursor ro
23e89 77 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 70 2d  w caches */.  p-
23e8a 3e 63 61 63 68 65 43 74 72 20 3d 20 28 70 2d 3e  >cacheCtr = (p->
23e8b 63 61 63 68 65 43 74 72 20 2b 20 32 29 7c 31 3b  cacheCtr + 2)|1;
23e8c 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
23e8d 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20   the results of 
23e8e 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
23e8f 61 72 65 20 5c 30 30 30 20 74 65 72 6d 69 6e 61  are \000 termina
23e90 74 65 64 0a 20 20 2a 2a 20 61 6e 64 20 68 61 76  ted.  ** and hav
23e91 65 20 61 6e 20 61 73 73 69 67 6e 65 64 20 74 79  e an assigned ty
23e92 70 65 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73  pe.  The results
23e93 20 61 72 65 20 64 65 2d 65 70 68 65 6d 65 72 61   are de-ephemera
23e94 6c 69 7a 65 64 20 61 73 0a 20 20 2a 2a 20 61 20  lized as.  ** a 
23e95 73 69 64 65 20 65 66 66 65 63 74 2e 0a 20 20 2a  side effect..  *
23e96 2f 0a 20 20 75 2e 61 66 2e 70 4d 65 6d 20 3d 20  /.  u.af.pMem = 
23e97 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20  p->pResultSet = 
23e98 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  &aMem[pOp->p1];.
23e99 20 20 66 6f 72 28 75 2e 61 66 2e 69 3d 30 3b 20    for(u.af.i=0; 
23e9a 75 2e 61 66 2e 69 3c 70 4f 70 2d 3e 70 32 3b 20  u.af.i<pOp->p2; 
23e9b 75 2e 61 66 2e 69 2b 2b 29 7b 0a 20 20 20 20 61  u.af.i++){.    a
23e9c 73 73 65 72 74 28 20 6d 65 6d 49 73 56 61 6c 69  ssert( memIsVali
23e9d 64 28 26 75 2e 61 66 2e 70 4d 65 6d 5b 75 2e 61  d(&u.af.pMem[u.a
23e9e 66 2e 69 5d 29 20 29 3b 0a 20 20 20 20 44 65 65  f.i]) );.    Dee
23e9f 70 68 65 6d 65 72 61 6c 69 7a 65 28 26 75 2e 61  phemeralize(&u.a
23ea0 66 2e 70 4d 65 6d 5b 75 2e 61 66 2e 69 5d 29 3b  f.pMem[u.af.i]);
23ea1 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 75 2e  .    assert( (u.
23ea2 61 66 2e 70 4d 65 6d 5b 75 2e 61 66 2e 69 5d 2e  af.pMem[u.af.i].
23ea3 66 6c 61 67 73 20 26 20 4d 45 4d 5f 45 70 68 65  flags & MEM_Ephe
23ea4 6d 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20  m)==0.          
23ea5 20 20 7c 7c 20 28 75 2e 61 66 2e 70 4d 65 6d 5b    || (u.af.pMem[
23ea6 75 2e 61 66 2e 69 5d 2e 66 6c 61 67 73 20 26 20  u.af.i].flags & 
23ea7 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
23ea8 62 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71  b))==0 );.    sq
23ea9 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54  lite3VdbeMemNulT
23eaa 65 72 6d 69 6e 61 74 65 28 26 75 2e 61 66 2e 70  erminate(&u.af.p
23eab 4d 65 6d 5b 75 2e 61 66 2e 69 5d 29 3b 0a 20 20  Mem[u.af.i]);.  
23eac 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
23ead 53 74 6f 72 65 54 79 70 65 28 26 75 2e 61 66 2e  StoreType(&u.af.
23eae 70 4d 65 6d 5b 75 2e 61 66 2e 69 5d 29 3b 0a 20  pMem[u.af.i]);. 
23eaf 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43     REGISTER_TRAC
23eb0 45 28 70 4f 70 2d 3e 70 31 2b 75 2e 61 66 2e 69  E(pOp->p1+u.af.i
23eb1 2c 20 26 75 2e 61 66 2e 70 4d 65 6d 5b 75 2e 61  , &u.af.pMem[u.a
23eb2 66 2e 69 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 28  f.i]);.  }.  if(
23eb3 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
23eb4 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  d ) goto no_mem;
23eb5 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 53 51  ..  /* Return SQ
23eb6 4c 49 54 45 5f 52 4f 57 0a 20 20 2a 2f 0a 20 20  LITE_ROW.  */.  
23eb7 70 2d 3e 70 63 20 3d 20 70 63 20 2b 20 31 3b 0a  p->pc = pc + 1;.
23eb8 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f    rc = SQLITE_RO
23eb9 57 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 72  W;.  goto vdbe_r
23eba 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  eturn;.}../* Opc
23ebb 6f 64 65 3a 20 43 6f 6e 63 61 74 20 50 31 20 50  ode: Concat P1 P
23ebc 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41  2 P3 * *.**.** A
23ebd 64 64 20 74 68 65 20 74 65 78 74 20 69 6e 20 72  dd the text in r
23ebe 65 67 69 73 74 65 72 20 50 31 20 6f 6e 74 6f 20  egister P1 onto 
23ebf 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74  the end of the t
23ec0 65 78 74 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74  ext in.** regist
23ec1 65 72 20 50 32 20 61 6e 64 20 73 74 6f 72 65 20  er P2 and store 
23ec2 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65  the result in re
23ec3 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66  gister P3..** If
23ec4 20 65 69 74 68 65 72 20 74 68 65 20 50 31 20 6f   either the P1 o
23ec5 72 20 50 32 20 74 65 78 74 20 61 72 65 20 4e 55  r P2 text are NU
23ec6 4c 4c 20 74 68 65 6e 20 73 74 6f 72 65 20 4e 55  LL then store NU
23ec7 4c 4c 20 69 6e 20 50 33 2e 0a 2a 2a 0a 2a 2a 20  LL in P3..**.** 
23ec8 20 20 50 33 20 3d 20 50 32 20 7c 7c 20 50 31 0a    P3 = P2 || P1.
23ec9 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65  **.** It is ille
23eca 67 61 6c 20 66 6f 72 20 50 31 20 61 6e 64 20 50  gal for P1 and P
23ecb 33 20 74 6f 20 62 65 20 74 68 65 20 73 61 6d 65  3 to be the same
23ecc 20 72 65 67 69 73 74 65 72 2e 20 53 6f 6d 65 74   register. Somet
23ecd 69 6d 65 73 2c 0a 2a 2a 20 69 66 20 50 33 20 69  imes,.** if P3 i
23ece 73 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73  s the same regis
23ecf 74 65 72 20 61 73 20 50 32 2c 20 74 68 65 20 69  ter as P2, the i
23ed0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73  mplementation is
23ed1 20 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 76 6f 69   able.** to avoi
23ed2 64 20 61 20 6d 65 6d 63 70 79 28 29 2e 0a 2a 2f  d a memcpy()..*/
23ed3 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 63 61 74 3a  .case OP_Concat:
23ed4 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
23ed5 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 41  same as TK_CONCA
23ed6 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74  T, in1, in2, out
23ed7 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  3 */.#if 0  /* l
23ed8 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
23ed9 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 67 20 2a  oved into u.ag *
23eda 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 3b 0a 23  /.  i64 nByte;.#
23edb 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
23edc 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
23edd 6e 74 6f 20 75 2e 61 67 20 2a 2f 0a 0a 20 20 70  nto u.ag */..  p
23ede 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  In1 = &aMem[pOp-
23edf 3e 70 31 5d 3b 0a 20 20 70 49 6e 32 20 3d 20 26  >p1];.  pIn2 = &
23ee0 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
23ee1 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f   pOut = &aMem[pO
23ee2 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74  p->p3];.  assert
23ee3 28 20 70 49 6e 31 21 3d 70 4f 75 74 20 29 3b 0a  ( pIn1!=pOut );.
23ee4 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
23ee5 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73  gs | pIn2->flags
23ee6 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  ) & MEM_Null ){.
23ee7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
23ee8 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b  emSetNull(pOut);
23ee9 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  .    break;.  }.
23eea 20 20 69 66 28 20 45 78 70 61 6e 64 42 6c 6f 62    if( ExpandBlob
23eeb 28 70 49 6e 31 29 20 7c 7c 20 45 78 70 61 6e 64  (pIn1) || Expand
23eec 42 6c 6f 62 28 70 49 6e 32 29 20 29 20 67 6f 74  Blob(pIn2) ) got
23eed 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 53 74 72 69  o no_mem;.  Stri
23eee 6e 67 69 66 79 28 70 49 6e 31 2c 20 65 6e 63 6f  ngify(pIn1, enco
23eef 64 69 6e 67 29 3b 0a 20 20 53 74 72 69 6e 67 69  ding);.  Stringi
23ef0 66 79 28 70 49 6e 32 2c 20 65 6e 63 6f 64 69 6e  fy(pIn2, encodin
23ef1 67 29 3b 0a 20 20 75 2e 61 67 2e 6e 42 79 74 65  g);.  u.ag.nByte
23ef2 20 3d 20 70 49 6e 31 2d 3e 6e 20 2b 20 70 49 6e   = pIn1->n + pIn
23ef3 32 2d 3e 6e 3b 0a 20 20 69 66 28 20 75 2e 61 67  2->n;.  if( u.ag
23ef4 2e 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69  .nByte>db->aLimi
23ef5 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
23ef6 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f  ENGTH] ){.    go
23ef7 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a  to too_big;.  }.
23ef8 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
23ef9 28 70 4f 75 74 2c 20 4d 45 4d 5f 53 74 72 29 3b  (pOut, MEM_Str);
23efa 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
23efb 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20  beMemGrow(pOut, 
23efc 28 69 6e 74 29 75 2e 61 67 2e 6e 42 79 74 65 2b  (int)u.ag.nByte+
23efd 32 2c 20 70 4f 75 74 3d 3d 70 49 6e 32 29 20 29  2, pOut==pIn2) )
23efe 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  {.    goto no_me
23eff 6d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75  m;.  }.  if( pOu
23f00 74 21 3d 70 49 6e 32 20 29 7b 0a 20 20 20 20 6d  t!=pIn2 ){.    m
23f01 65 6d 63 70 79 28 70 4f 75 74 2d 3e 7a 2c 20 70  emcpy(pOut->z, p
23f02 49 6e 32 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 6e 29  In2->z, pIn2->n)
23f03 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26  ;.  }.  memcpy(&
23f04 70 4f 75 74 2d 3e 7a 5b 70 49 6e 32 2d 3e 6e 5d  pOut->z[pIn2->n]
23f05 2c 20 70 49 6e 31 2d 3e 7a 2c 20 70 49 6e 31 2d  , pIn1->z, pIn1-
23f06 3e 6e 29 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b 75  >n);.  pOut->z[u
23f07 2e 61 67 2e 6e 42 79 74 65 5d 20 3d 20 30 3b 0a  .ag.nByte] = 0;.
23f08 20 20 70 4f 75 74 2d 3e 7a 5b 75 2e 61 67 2e 6e    pOut->z[u.ag.n
23f09 42 79 74 65 2b 31 5d 20 3d 20 30 3b 0a 20 20 70  Byte+1] = 0;.  p
23f0a 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Out->flags |= ME
23f0b 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d 3e  M_Term;.  pOut->
23f0c 6e 20 3d 20 28 69 6e 74 29 75 2e 61 67 2e 6e 42  n = (int)u.ag.nB
23f0d 79 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63  yte;.  pOut->enc
23f0e 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55   = encoding;.  U
23f0f 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
23f10 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61  ZE(pOut);.  brea
23f11 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
23f12 20 41 64 64 20 50 31 20 50 32 20 50 33 20 2a 20   Add P1 P2 P3 * 
23f13 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20  *.**.** Add the 
23f14 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
23f15 72 20 50 31 20 74 6f 20 74 68 65 20 76 61 6c 75  r P1 to the valu
23f16 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  e in register P2
23f17 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68  .** and store th
23f18 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69  e result in regi
23f19 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65  ster P3..** If e
23f1a 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e  ither input is N
23f1b 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20  ULL, the result 
23f1c 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f  is NULL..*/./* O
23f1d 70 63 6f 64 65 3a 20 4d 75 6c 74 69 70 6c 79 20  pcode: Multiply 
23f1e 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
23f1f 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c 79 20 74  **.** Multiply t
23f20 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
23f21 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76  ster P1 by the v
23f22 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
23f23 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65   P2.** and store
23f24 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
23f25 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49  egister P3..** I
23f26 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
23f27 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
23f28 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f  lt is NULL..*/./
23f29 2a 20 4f 70 63 6f 64 65 3a 20 53 75 62 74 72 61  * Opcode: Subtra
23f2a 63 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  ct P1 P2 P3 * *.
23f2b 2a 2a 0a 2a 2a 20 53 75 62 74 72 61 63 74 20 74  **.** Subtract t
23f2c 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
23f2d 73 74 65 72 20 50 31 20 66 72 6f 6d 20 74 68 65  ster P1 from the
23f2e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
23f2f 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f  er P2.** and sto
23f30 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  re the result in
23f31 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a   register P3..**
23f32 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74   If either input
23f33 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65   is NULL, the re
23f34 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  sult is NULL..*/
23f35 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 69 76 69  ./* Opcode: Divi
23f36 64 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  de P1 P2 P3 * *.
23f37 2a 2a 0a 2a 2a 20 44 69 76 69 64 65 20 74 68 65  **.** Divide the
23f38 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
23f39 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61 6c  er P1 by the val
23f3a 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
23f3b 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74  2.** and store t
23f3c 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67  he result in reg
23f3d 69 73 74 65 72 20 50 33 20 28 50 33 3d 50 32 2f  ister P3 (P3=P2/
23f3e 50 31 29 2e 20 49 66 20 74 68 65 20 76 61 6c 75  P1). If the valu
23f3f 65 20 69 6e 20 0a 2a 2a 20 72 65 67 69 73 74 65  e in .** registe
23f40 72 20 50 31 20 69 73 20 7a 65 72 6f 2c 20 74 68  r P1 is zero, th
23f41 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  en the result is
23f42 20 4e 55 4c 4c 2e 20 49 66 20 65 69 74 68 65 72   NULL. If either
23f43 20 69 6e 70 75 74 20 69 73 20 0a 2a 2a 20 4e 55   input is .** NU
23f44 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69  LL, the result i
23f45 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70  s NULL..*/./* Op
23f46 63 6f 64 65 3a 20 52 65 6d 61 69 6e 64 65 72 20  code: Remainder 
23f47 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
23f48 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 72  ** Compute the r
23f49 65 6d 61 69 6e 64 65 72 20 61 66 74 65 72 20 69  emainder after i
23f4a 6e 74 65 67 65 72 20 64 69 76 69 73 69 6f 6e 20  nteger division 
23f4b 6f 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 0a  of the value in.
23f4c 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 62  ** register P1 b
23f4d 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  y the value in r
23f4e 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 73  egister P2 and s
23f4f 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
23f50 69 6e 20 50 33 2e 20 0a 2a 2a 20 49 66 20 74 68  in P3. .** If th
23f51 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
23f52 74 65 72 20 50 32 20 69 73 20 7a 65 72 6f 20 74  ter P2 is zero t
23f53 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c  he result is NUL
23f54 4c 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20  L..** If either 
23f55 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c  operand is NULL,
23f56 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e   the result is N
23f57 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ULL..*/.case OP_
23f58 41 64 64 3a 20 20 20 20 20 20 20 20 20 20 20 20  Add:            
23f59 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
23f5a 73 20 54 4b 5f 50 4c 55 53 2c 20 69 6e 31 2c 20  s TK_PLUS, in1, 
23f5b 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73  in2, out3 */.cas
23f5c 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 20  e OP_Subtract:  
23f5d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
23f5e 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 2c  ame as TK_MINUS,
23f5f 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20   in1, in2, out3 
23f60 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69  */.case OP_Multi
23f61 70 6c 79 3a 20 20 20 20 20 20 20 20 20 20 20 20  ply:            
23f62 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
23f63 53 54 41 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  STAR, in1, in2, 
23f64 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f  out3 */.case OP_
23f65 44 69 76 69 64 65 3a 20 20 20 20 20 20 20 20 20  Divide:         
23f66 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
23f67 73 20 54 4b 5f 53 4c 41 53 48 2c 20 69 6e 31 2c  s TK_SLASH, in1,
23f68 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61   in2, out3 */.ca
23f69 73 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 3a  se OP_Remainder:
23f6a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
23f6b 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 4d 2c 20  same as TK_REM, 
23f6c 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
23f6d 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
23f6e 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
23f6f 64 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 20  d into u.ah */. 
23f70 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20   int flags;     
23f71 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 4d 45 4d   /* Combined MEM
23f72 5f 2a 20 66 6c 61 67 73 20 66 72 6f 6d 20 62 6f  _* flags from bo
23f73 74 68 20 69 6e 70 75 74 73 20 2a 2f 0a 20 20 69  th inputs */.  i
23f74 36 34 20 69 41 3b 20 20 20 20 20 20 20 20 20 2f  64 iA;         /
23f75 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20  * Integer value 
23f76 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  of left operand 
23f77 2a 2f 0a 20 20 69 36 34 20 69 42 3b 20 20 20 20  */.  i64 iB;    
23f78 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20       /* Integer 
23f79 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f  value of right o
23f7a 70 65 72 61 6e 64 20 2a 2f 0a 20 20 64 6f 75 62  perand */.  doub
23f7b 6c 65 20 72 41 3b 20 20 20 20 20 20 2f 2a 20 52  le rA;      /* R
23f7c 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 6c 65 66  eal value of lef
23f7d 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 64  t operand */.  d
23f7e 6f 75 62 6c 65 20 72 42 3b 20 20 20 20 20 20 2f  ouble rB;      /
23f7f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 20  * Real value of 
23f80 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f  right operand */
23f81 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
23f82 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
23f83 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 0a 20   into u.ah */.. 
23f84 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn1 = &aMem[pO
23f85 70 2d 3e 70 31 5d 3b 0a 20 20 61 70 70 6c 79 4e  p->p1];.  applyN
23f86 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70  umericAffinity(p
23f87 49 6e 31 29 3b 0a 20 20 70 49 6e 32 20 3d 20 26  In1);.  pIn2 = &
23f88 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
23f89 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66   applyNumericAff
23f8a 69 6e 69 74 79 28 70 49 6e 32 29 3b 0a 20 20 70  inity(pIn2);.  p
23f8b 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  Out = &aMem[pOp-
23f8c 3e 70 33 5d 3b 0a 20 20 75 2e 61 68 2e 66 6c 61  >p3];.  u.ah.fla
23f8d 67 73 20 3d 20 70 49 6e 31 2d 3e 66 6c 61 67 73  gs = pIn1->flags
23f8e 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 3b 0a   | pIn2->flags;.
23f8f 20 20 69 66 28 20 28 75 2e 61 68 2e 66 6c 61 67    if( (u.ah.flag
23f90 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30  s & MEM_Null)!=0
23f91 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74   ) goto arithmet
23f92 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c  ic_result_is_nul
23f93 6c 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  l;.  if( (pIn1->
23f94 66 6c 61 67 73 20 26 20 70 49 6e 32 2d 3e 66 6c  flags & pIn2->fl
23f95 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d  ags & MEM_Int)==
23f96 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 75  MEM_Int ){.    u
23f97 2e 61 68 2e 69 41 20 3d 20 70 49 6e 31 2d 3e 75  .ah.iA = pIn1->u
23f98 2e 69 3b 0a 20 20 20 20 75 2e 61 68 2e 69 42 20  .i;.    u.ah.iB 
23f99 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20  = pIn2->u.i;.   
23f9a 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70   switch( pOp->op
23f9b 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 63 61  code ){.      ca
23f9c 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20  se OP_Add:      
23f9d 20 69 66 28 20 73 71 6c 69 74 65 33 41 64 64 49   if( sqlite3AddI
23f9e 6e 74 36 34 28 26 75 2e 61 68 2e 69 42 2c 75 2e  nt64(&u.ah.iB,u.
23f9f 61 68 2e 69 41 29 20 29 20 67 6f 74 6f 20 66 70  ah.iA) ) goto fp
23fa0 5f 6d 61 74 68 3b 20 20 62 72 65 61 6b 3b 0a 20  _math;  break;. 
23fa1 20 20 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62       case OP_Sub
23fa2 74 72 61 63 74 3a 20 20 69 66 28 20 73 71 6c 69  tract:  if( sqli
23fa3 74 65 33 53 75 62 49 6e 74 36 34 28 26 75 2e 61  te3SubInt64(&u.a
23fa4 68 2e 69 42 2c 75 2e 61 68 2e 69 41 29 20 29 20  h.iB,u.ah.iA) ) 
23fa5 67 6f 74 6f 20 66 70 5f 6d 61 74 68 3b 20 20 62  goto fp_math;  b
23fa6 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
23fa7 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 69   OP_Multiply:  i
23fa8 66 28 20 73 71 6c 69 74 65 33 4d 75 6c 49 6e 74  f( sqlite3MulInt
23fa9 36 34 28 26 75 2e 61 68 2e 69 42 2c 75 2e 61 68  64(&u.ah.iB,u.ah
23faa 2e 69 41 29 20 29 20 67 6f 74 6f 20 66 70 5f 6d  .iA) ) goto fp_m
23fab 61 74 68 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20  ath;  break;.   
23fac 20 20 20 63 61 73 65 20 4f 50 5f 44 69 76 69 64     case OP_Divid
23fad 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28  e: {.        if(
23fae 20 75 2e 61 68 2e 69 41 3d 3d 30 20 29 20 67 6f   u.ah.iA==0 ) go
23faf 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65  to arithmetic_re
23fb0 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20  sult_is_null;.  
23fb1 20 20 20 20 20 20 69 66 28 20 75 2e 61 68 2e 69        if( u.ah.i
23fb2 41 3d 3d 2d 31 20 26 26 20 75 2e 61 68 2e 69 42  A==-1 && u.ah.iB
23fb3 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34  ==SMALLEST_INT64
23fb4 20 29 20 67 6f 74 6f 20 66 70 5f 6d 61 74 68 3b   ) goto fp_math;
23fb5 0a 20 20 20 20 20 20 20 20 75 2e 61 68 2e 69 42  .        u.ah.iB
23fb6 20 2f 3d 20 75 2e 61 68 2e 69 41 3b 0a 20 20 20   /= u.ah.iA;.   
23fb7 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
23fb8 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c    }.      defaul
23fb9 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28  t: {.        if(
23fba 20 75 2e 61 68 2e 69 41 3d 3d 30 20 29 20 67 6f   u.ah.iA==0 ) go
23fbb 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65  to arithmetic_re
23fbc 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20  sult_is_null;.  
23fbd 20 20 20 20 20 20 69 66 28 20 75 2e 61 68 2e 69        if( u.ah.i
23fbe 41 3d 3d 2d 31 20 29 20 75 2e 61 68 2e 69 41 20  A==-1 ) u.ah.iA 
23fbf 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61  = 1;.        u.a
23fc0 68 2e 69 42 20 25 3d 20 75 2e 61 68 2e 69 41 3b  h.iB %= u.ah.iA;
23fc1 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
23fc2 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
23fc3 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e    pOut->u.i = u.
23fc4 61 68 2e 69 42 3b 0a 20 20 20 20 4d 65 6d 53 65  ah.iB;.    MemSe
23fc5 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
23fc6 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73  MEM_Int);.  }els
23fc7 65 7b 0a 66 70 5f 6d 61 74 68 3a 0a 20 20 20 20  e{.fp_math:.    
23fc8 75 2e 61 68 2e 72 41 20 3d 20 73 71 6c 69 74 65  u.ah.rA = sqlite
23fc9 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70  3VdbeRealValue(p
23fca 49 6e 31 29 3b 0a 20 20 20 20 75 2e 61 68 2e 72  In1);.    u.ah.r
23fcb 42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52  B = sqlite3VdbeR
23fcc 65 61 6c 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a  ealValue(pIn2);.
23fcd 20 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d      switch( pOp-
23fce 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20  >opcode ){.     
23fcf 20 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20   case OP_Add:   
23fd0 20 20 20 20 20 20 75 2e 61 68 2e 72 42 20 2b 3d        u.ah.rB +=
23fd1 20 75 2e 61 68 2e 72 41 3b 20 20 20 20 20 20 20   u.ah.rA;       
23fd2 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
23fd3 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 20  e OP_Subtract:  
23fd4 20 20 75 2e 61 68 2e 72 42 20 2d 3d 20 75 2e 61    u.ah.rB -= u.a
23fd5 68 2e 72 41 3b 20 20 20 20 20 20 20 62 72 65 61  h.rA;       brea
23fd6 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50  k;.      case OP
23fd7 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 75 2e  _Multiply:    u.
23fd8 61 68 2e 72 42 20 2a 3d 20 75 2e 61 68 2e 72 41  ah.rB *= u.ah.rA
23fd9 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;       break;. 
23fda 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44 69 76       case OP_Div
23fdb 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 2f  ide: {.        /
23fdc 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63  * (double)0 In c
23fdd 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d  ase of SQLITE_OM
23fde 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
23fdf 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  T... */.        
23fe0 69 66 28 20 75 2e 61 68 2e 72 41 3d 3d 28 64 6f  if( u.ah.rA==(do
23fe1 75 62 6c 65 29 30 20 29 20 67 6f 74 6f 20 61 72  uble)0 ) goto ar
23fe2 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f  ithmetic_result_
23fe3 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20  is_null;.       
23fe4 20 75 2e 61 68 2e 72 42 20 2f 3d 20 75 2e 61 68   u.ah.rB /= u.ah
23fe5 2e 72 41 3b 0a 20 20 20 20 20 20 20 20 62 72 65  .rA;.        bre
23fe6 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
23fe7 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
23fe8 20 20 20 20 20 75 2e 61 68 2e 69 41 20 3d 20 28       u.ah.iA = (
23fe9 69 36 34 29 75 2e 61 68 2e 72 41 3b 0a 20 20 20  i64)u.ah.rA;.   
23fea 20 20 20 20 20 75 2e 61 68 2e 69 42 20 3d 20 28       u.ah.iB = (
23feb 69 36 34 29 75 2e 61 68 2e 72 42 3b 0a 20 20 20  i64)u.ah.rB;.   
23fec 20 20 20 20 20 69 66 28 20 75 2e 61 68 2e 69 41       if( u.ah.iA
23fed 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68  ==0 ) goto arith
23fee 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f  metic_result_is_
23fef 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66  null;.        if
23ff0 28 20 75 2e 61 68 2e 69 41 3d 3d 2d 31 20 29 20  ( u.ah.iA==-1 ) 
23ff1 75 2e 61 68 2e 69 41 20 3d 20 31 3b 0a 20 20 20  u.ah.iA = 1;.   
23ff2 20 20 20 20 20 75 2e 61 68 2e 72 42 20 3d 20 28       u.ah.rB = (
23ff3 64 6f 75 62 6c 65 29 28 75 2e 61 68 2e 69 42 20  double)(u.ah.iB 
23ff4 25 20 75 2e 61 68 2e 69 41 29 3b 0a 20 20 20 20  % u.ah.iA);.    
23ff5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
23ff6 20 7d 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20   }.    }.#ifdef 
23ff7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
23ff8 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 70  TING_POINT.    p
23ff9 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 68 2e  Out->u.i = u.ah.
23ffa 72 42 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79  rB;.    MemSetTy
23ffb 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
23ffc 5f 49 6e 74 29 3b 0a 23 65 6c 73 65 0a 20 20 20  _Int);.#else.   
23ffd 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61   if( sqlite3IsNa
23ffe 4e 28 75 2e 61 68 2e 72 42 29 20 29 7b 0a 20 20  N(u.ah.rB) ){.  
23fff 20 20 20 20 67 6f 74 6f 20 61 72 69 74 68 6d 65      goto arithme
24000 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75  tic_result_is_nu
24001 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f  ll;.    }.    pO
24002 75 74 2d 3e 72 20 3d 20 75 2e 61 68 2e 72 42 3b  ut->r = u.ah.rB;
24003 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46  .    MemSetTypeF
24004 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 52 65  lag(pOut, MEM_Re
24005 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 75 2e  al);.    if( (u.
24006 61 68 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  ah.flags & MEM_R
24007 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  eal)==0 ){.     
24008 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65   sqlite3VdbeInte
24009 67 65 72 41 66 66 69 6e 69 74 79 28 70 4f 75 74  gerAffinity(pOut
2400a 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
2400b 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 0a 61 72    }.  break;..ar
2400c 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f  ithmetic_result_
2400d 69 73 5f 6e 75 6c 6c 3a 0a 20 20 73 71 6c 69 74  is_null:.  sqlit
2400e 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c  e3VdbeMemSetNull
2400f 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b  (pOut);.  break;
24010 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  .}../* Opcode: C
24011 6f 6c 6c 53 65 71 20 50 31 20 2a 20 2a 20 50 34  ollSeq P1 * * P4
24012 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70  .**.** P4 is a p
24013 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c 6c  ointer to a Coll
24014 53 65 71 20 73 74 72 75 63 74 2e 20 49 66 20 74  Seq struct. If t
24015 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20  he next call to 
24016 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 0a  a user function.
24017 2a 2a 20 6f 72 20 61 67 67 72 65 67 61 74 65 20  ** or aggregate 
24018 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 47 65 74  calls sqlite3Get
24019 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29 2c 20 74  FuncCollSeq(), t
2401a 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  his collation se
2401b 71 75 65 6e 63 65 20 77 69 6c 6c 0a 2a 2a 20 62  quence will.** b
2401c 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73  e returned. This
2401d 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
2401e 62 75 69 6c 74 2d 69 6e 20 6d 69 6e 28 29 2c 20  built-in min(), 
2401f 6d 61 78 28 29 20 61 6e 64 20 6e 75 6c 6c 69 66  max() and nullif
24020 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e  ().** functions.
24021 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20  .**.** If P1 is 
24022 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 69  not zero, then i
24023 74 20 69 73 20 61 20 72 65 67 69 73 74 65 72 20  t is a register 
24024 74 68 61 74 20 61 20 73 75 62 73 65 71 75 65 6e  that a subsequen
24025 74 20 6d 69 6e 28 29 20 6f 72 0a 2a 2a 20 6d 61  t min() or.** ma
24026 78 28 29 20 61 67 67 72 65 67 61 74 65 20 77 69  x() aggregate wi
24027 6c 6c 20 73 65 74 20 74 6f 20 31 20 69 66 20 74  ll set to 1 if t
24028 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 69  he current row i
24029 73 20 6e 6f 74 20 74 68 65 20 6d 69 6e 69 6d 75  s not the minimu
2402a 6d 20 6f 72 0a 2a 2a 20 6d 61 78 69 6d 75 6d 2e  m or.** maximum.
2402b 20 20 54 68 65 20 50 31 20 72 65 67 69 73 74 65    The P1 registe
2402c 72 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  r is initialized
2402d 20 74 6f 20 30 20 62 79 20 74 68 69 73 20 69 6e   to 0 by this in
2402e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  struction..**.**
2402f 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 75   The interface u
24030 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65  sed by the imple
24031 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
24032 20 61 66 6f 72 65 6d 65 6e 74 69 6f 6e 65 64 20   aforementioned 
24033 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20  functions.** to 
24034 72 65 74 72 69 65 76 65 20 74 68 65 20 63 6f 6c  retrieve the col
24035 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
24036 73 65 74 20 62 79 20 74 68 69 73 20 6f 70 63 6f  set by this opco
24037 64 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61  de is not availa
24038 62 6c 65 0a 2a 2a 20 70 75 62 6c 69 63 6c 79 2c  ble.** publicly,
24039 20 6f 6e 6c 79 20 74 6f 20 75 73 65 72 20 66 75   only to user fu
2403a 6e 63 74 69 6f 6e 73 20 64 65 66 69 6e 65 64 20  nctions defined 
2403b 69 6e 20 66 75 6e 63 2e 63 2e 0a 2a 2f 0a 63 61  in func.c..*/.ca
2403c 73 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 3a 20 7b  se OP_CollSeq: {
2403d 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
2403e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53  p4type==P4_COLLS
2403f 45 51 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d  EQ );.  if( pOp-
24040 3e 70 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >p1 ){.    sqlit
24041 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
24042 34 28 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  4(&aMem[pOp->p1]
24043 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61  , 0);.  }.  brea
24044 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
24045 20 46 75 6e 63 74 69 6f 6e 20 50 31 20 50 32 20   Function P1 P2 
24046 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49  P3 P4 P5.**.** I
24047 6e 76 6f 6b 65 20 61 20 75 73 65 72 20 66 75 6e  nvoke a user fun
24048 63 74 69 6f 6e 20 28 50 34 20 69 73 20 61 20 70  ction (P4 is a p
24049 6f 69 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63  ointer to a Func
2404a 74 69 6f 6e 20 73 74 72 75 63 74 75 72 65 20 74  tion structure t
2404b 68 61 74 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74  hat.** defines t
2404c 68 65 20 66 75 6e 63 74 69 6f 6e 29 20 77 69 74  he function) wit
2404d 68 20 50 35 20 61 72 67 75 6d 65 6e 74 73 20 74  h P5 arguments t
2404e 61 6b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74  aken from regist
2404f 65 72 20 50 32 20 61 6e 64 0a 2a 2a 20 73 75 63  er P2 and.** suc
24050 63 65 73 73 6f 72 73 2e 20 20 54 68 65 20 72 65  cessors.  The re
24051 73 75 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63  sult of the func
24052 74 69 6f 6e 20 69 73 20 73 74 6f 72 65 64 20 69  tion is stored i
24053 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
24054 2a 20 52 65 67 69 73 74 65 72 20 50 33 20 6d 75  * Register P3 mu
24055 73 74 20 6e 6f 74 20 62 65 20 6f 6e 65 20 6f 66  st not be one of
24056 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e   the function in
24057 70 75 74 73 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69  puts..**.** P1 i
24058 73 20 61 20 33 32 2d 62 69 74 20 62 69 74 6d 61  s a 32-bit bitma
24059 73 6b 20 69 6e 64 69 63 61 74 69 6e 67 20 77 68  sk indicating wh
2405a 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 65 61 63  ether or not eac
2405b 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  h argument to th
2405c 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77  e .** function w
2405d 61 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74 6f  as determined to
2405e 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 61 74 20   be constant at 
2405f 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e 20 49 66  compile time. If
24060 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72   the first.** ar
24061 67 75 6d 65 6e 74 20 77 61 73 20 63 6f 6e 73 74  gument was const
24062 61 6e 74 20 74 68 65 6e 20 62 69 74 20 30 20 6f  ant then bit 0 o
24063 66 20 50 31 20 69 73 20 73 65 74 2e 20 54 68 69  f P1 is set. Thi
24064 73 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74  s is used to det
24065 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65  ermine.** whethe
24066 72 20 6d 65 74 61 20 64 61 74 61 20 61 73 73 6f  r meta data asso
24067 63 69 61 74 65 64 20 77 69 74 68 20 61 20 75 73  ciated with a us
24068 65 72 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75  er function argu
24069 6d 65 6e 74 20 75 73 69 6e 67 20 74 68 65 0a 2a  ment using the.*
2406a 2a 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  * sqlite3_set_au
2406b 78 64 61 74 61 28 29 20 41 50 49 20 6d 61 79 20  xdata() API may 
2406c 62 65 20 73 61 66 65 6c 79 20 72 65 74 61 69 6e  be safely retain
2406d 65 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78  ed until the nex
2406e 74 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20  t.** invocation 
2406f 6f 66 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a  of this opcode..
24070 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
24071 41 67 67 53 74 65 70 20 61 6e 64 20 41 67 67 46  AggStep and AggF
24072 69 6e 61 6c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  inal.*/.case OP_
24073 46 75 6e 63 74 69 6f 6e 3a 20 7b 0a 23 69 66 20  Function: {.#if 
24074 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
24075 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
24076 20 75 2e 61 69 20 2a 2f 0a 20 20 69 6e 74 20 69   u.ai */.  int i
24077 3b 0a 20 20 4d 65 6d 20 2a 70 41 72 67 3b 0a 20  ;.  Mem *pArg;. 
24078 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
24079 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f   ctx;.  sqlite3_
2407a 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 20  value **apVal;. 
2407b 20 69 6e 74 20 6e 3b 0a 23 65 6e 64 69 66 20 2f   int n;.#endif /
2407c 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
2407d 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
2407e 69 20 2a 2f 0a 0a 20 20 75 2e 61 69 2e 6e 20 3d  i */..  u.ai.n =
2407f 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 75 2e 61 69   pOp->p5;.  u.ai
24080 2e 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72  .apVal = p->apAr
24081 67 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  g;.  assert( u.a
24082 69 2e 61 70 56 61 6c 20 7c 7c 20 75 2e 61 69 2e  i.apVal || u.ai.
24083 6e 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  n==0 );.  assert
24084 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70  ( pOp->p3>0 && p
24085 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p3<=p->nMem 
24086 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65  );.  pOut = &aMe
24087 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 6d 65  m[pOp->p3];.  me
24088 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 70  mAboutToChange(p
24089 2c 20 70 4f 75 74 29 3b 0a 0a 20 20 61 73 73 65  , pOut);..  asse
2408a 72 74 28 20 75 2e 61 69 2e 6e 3d 3d 30 20 7c 7c  rt( u.ai.n==0 ||
2408b 20 28 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70   (pOp->p2>0 && p
2408c 4f 70 2d 3e 70 32 2b 75 2e 61 69 2e 6e 3c 3d 70  Op->p2+u.ai.n<=p
2408d 2d 3e 6e 4d 65 6d 2b 31 29 20 29 3b 0a 20 20 61  ->nMem+1) );.  a
2408e 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70  ssert( pOp->p3<p
2408f 4f 70 2d 3e 70 32 20 7c 7c 20 70 4f 70 2d 3e 70  Op->p2 || pOp->p
24090 33 3e 3d 70 4f 70 2d 3e 70 32 2b 75 2e 61 69 2e  3>=pOp->p2+u.ai.
24091 6e 20 29 3b 0a 20 20 75 2e 61 69 2e 70 41 72 67  n );.  u.ai.pArg
24092 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32   = &aMem[pOp->p2
24093 5d 3b 0a 20 20 66 6f 72 28 75 2e 61 69 2e 69 3d  ];.  for(u.ai.i=
24094 30 3b 20 75 2e 61 69 2e 69 3c 75 2e 61 69 2e 6e  0; u.ai.i<u.ai.n
24095 3b 20 75 2e 61 69 2e 69 2b 2b 2c 20 75 2e 61 69  ; u.ai.i++, u.ai
24096 2e 70 41 72 67 2b 2b 29 7b 0a 20 20 20 20 61 73  .pArg++){.    as
24097 73 65 72 74 28 20 6d 65 6d 49 73 56 61 6c 69 64  sert( memIsValid
24098 28 75 2e 61 69 2e 70 41 72 67 29 20 29 3b 0a 20  (u.ai.pArg) );. 
24099 20 20 20 75 2e 61 69 2e 61 70 56 61 6c 5b 75 2e     u.ai.apVal[u.
2409a 61 69 2e 69 5d 20 3d 20 75 2e 61 69 2e 70 41 72  ai.i] = u.ai.pAr
2409b 67 3b 0a 20 20 20 20 44 65 65 70 68 65 6d 65 72  g;.    Deephemer
2409c 61 6c 69 7a 65 28 75 2e 61 69 2e 70 41 72 67 29  alize(u.ai.pArg)
2409d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2409e 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 75 2e  eMemStoreType(u.
2409f 61 69 2e 70 41 72 67 29 3b 0a 20 20 20 20 52 45  ai.pArg);.    RE
240a0 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
240a1 2d 3e 70 32 2b 75 2e 61 69 2e 69 2c 20 75 2e 61  ->p2+u.ai.i, u.a
240a2 69 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 0a 20 20  i.pArg);.  }..  
240a3 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
240a4 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20  ype==P4_FUNCDEF 
240a5 7c 7c 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  || pOp->p4type==
240a6 50 34 5f 56 44 42 45 46 55 4e 43 20 29 3b 0a 20  P4_VDBEFUNC );. 
240a7 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65   if( pOp->p4type
240a8 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20 29 7b 0a  ==P4_FUNCDEF ){.
240a9 20 20 20 20 75 2e 61 69 2e 63 74 78 2e 70 46 75      u.ai.ctx.pFu
240aa 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75  nc = pOp->p4.pFu
240ab 6e 63 3b 0a 20 20 20 20 75 2e 61 69 2e 63 74 78  nc;.    u.ai.ctx
240ac 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 30 3b 0a  .pVdbeFunc = 0;.
240ad 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61    }else{.    u.a
240ae 69 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 20  i.ctx.pVdbeFunc 
240af 3d 20 28 56 64 62 65 46 75 6e 63 2a 29 70 4f 70  = (VdbeFunc*)pOp
240b0 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e 63 3b 0a  ->p4.pVdbeFunc;.
240b1 20 20 20 20 75 2e 61 69 2e 63 74 78 2e 70 46 75      u.ai.ctx.pFu
240b2 6e 63 20 3d 20 75 2e 61 69 2e 63 74 78 2e 70 56  nc = u.ai.ctx.pV
240b3 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a  dbeFunc->pFunc;.
240b4 20 20 7d 0a 0a 20 20 75 2e 61 69 2e 63 74 78 2e    }..  u.ai.ctx.
240b5 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75  s.flags = MEM_Nu
240b6 6c 6c 3b 0a 20 20 75 2e 61 69 2e 63 74 78 2e 73  ll;.  u.ai.ctx.s
240b7 2e 64 62 20 3d 20 64 62 3b 0a 20 20 75 2e 61 69  .db = db;.  u.ai
240b8 2e 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 3b  .ctx.s.xDel = 0;
240b9 0a 20 20 75 2e 61 69 2e 63 74 78 2e 73 2e 7a 4d  .  u.ai.ctx.s.zM
240ba 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a  alloc = 0;..  /*
240bb 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c 6c   The output cell
240bc 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61 76   may already hav
240bd 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63  e a buffer alloc
240be 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20  ated. Move.  ** 
240bf 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 75  the pointer to u
240c0 2e 61 69 2e 63 74 78 2e 73 20 73 6f 20 69 6e 20  .ai.ctx.s so in 
240c1 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66 75  case the user-fu
240c2 6e 63 74 69 6f 6e 20 63 61 6e 20 75 73 65 0a 20  nction can use. 
240c3 20 2a 2a 20 74 68 65 20 61 6c 72 65 61 64 79 20   ** the already 
240c4 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72  allocated buffer
240c5 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c 6f   instead of allo
240c6 63 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e 65  cating a new one
240c7 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
240c8 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 61  VdbeMemMove(&u.a
240c9 69 2e 63 74 78 2e 73 2c 20 70 4f 75 74 29 3b 0a  i.ctx.s, pOut);.
240ca 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
240cb 28 26 75 2e 61 69 2e 63 74 78 2e 73 2c 20 4d 45  (&u.ai.ctx.s, ME
240cc 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 75 2e 61 69  M_Null);..  u.ai
240cd 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30  .ctx.isError = 0
240ce 3b 0a 20 20 69 66 28 20 75 2e 61 69 2e 63 74 78  ;.  if( u.ai.ctx
240cf 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20  .pFunc->flags & 
240d0 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44  SQLITE_FUNC_NEED
240d1 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 65  COLL ){.    asse
240d2 72 74 28 20 70 4f 70 3e 61 4f 70 20 29 3b 0a 20  rt( pOp>aOp );. 
240d3 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d     assert( pOp[-
240d4 31 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f  1].p4type==P4_CO
240d5 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 73 73  LLSEQ );.    ass
240d6 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63  ert( pOp[-1].opc
240d7 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20  ode==OP_CollSeq 
240d8 29 3b 0a 20 20 20 20 75 2e 61 69 2e 63 74 78 2e  );.    u.ai.ctx.
240d9 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e  pColl = pOp[-1].
240da 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20  p4.pColl;.  }.  
240db 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20  db->lastRowid = 
240dc 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 28 2a 75  lastRowid;.  (*u
240dd 2e 61 69 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 78  .ai.ctx.pFunc->x
240de 46 75 6e 63 29 28 26 75 2e 61 69 2e 63 74 78 2c  Func)(&u.ai.ctx,
240df 20 75 2e 61 69 2e 6e 2c 20 75 2e 61 69 2e 61 70   u.ai.n, u.ai.ap
240e0 56 61 6c 29 3b 20 2f 2a 20 49 4d 50 3a 20 52 2d  Val); /* IMP: R-
240e1 32 34 35 30 35 2d 32 33 32 33 30 20 2a 2f 0a 20  24505-23230 */. 
240e2 20 6c 61 73 74 52 6f 77 69 64 20 3d 20 64 62 2d   lastRowid = db-
240e3 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 0a 20 20 2f  >lastRowid;..  /
240e4 2a 20 49 66 20 61 6e 79 20 61 75 78 69 6c 69 61  * If any auxilia
240e5 72 79 20 64 61 74 61 20 66 75 6e 63 74 69 6f 6e  ry data function
240e6 73 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c  s have been call
240e7 65 64 20 62 79 20 74 68 69 73 20 75 73 65 72 20  ed by this user 
240e8 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 69  function,.  ** i
240e9 6d 6d 65 64 69 61 74 65 6c 79 20 63 61 6c 6c 20  mmediately call 
240ea 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66  the destructor f
240eb 6f 72 20 61 6e 79 20 6e 6f 6e 2d 73 74 61 74 69  or any non-stati
240ec 63 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f 0a 20  c values..  */. 
240ed 20 69 66 28 20 75 2e 61 69 2e 63 74 78 2e 70 56   if( u.ai.ctx.pV
240ee 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 73  dbeFunc ){.    s
240ef 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
240f0 41 75 78 44 61 74 61 28 75 2e 61 69 2e 63 74 78  AuxData(u.ai.ctx
240f1 2e 70 56 64 62 65 46 75 6e 63 2c 20 70 4f 70 2d  .pVdbeFunc, pOp-
240f2 3e 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  >p1);.    pOp->p
240f3 34 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 75 2e  4.pVdbeFunc = u.
240f4 61 69 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63  ai.ctx.pVdbeFunc
240f5 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70  ;.    pOp->p4typ
240f6 65 20 3d 20 50 34 5f 56 44 42 45 46 55 4e 43 3b  e = P4_VDBEFUNC;
240f7 0a 20 20 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e  .  }..  if( db->
240f8 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
240f9 20 20 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75      /* Even thou
240fa 67 68 20 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61  gh a malloc() ha
240fb 73 20 66 61 69 6c 65 64 2c 20 74 68 65 20 69 6d  s failed, the im
240fc 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
240fd 74 68 65 0a 20 20 20 20 2a 2a 20 75 73 65 72 20  the.    ** user 
240fe 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 68 61 76  function may hav
240ff 65 20 63 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69  e called an sqli
24100 74 65 33 5f 72 65 73 75 6c 74 5f 58 58 58 28 29  te3_result_XXX()
24101 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a   function.    **
24102 20 74 6f 20 72 65 74 75 72 6e 20 61 20 76 61 6c   to return a val
24103 75 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ue. The followin
24104 67 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 73 20  g call releases 
24105 61 6e 79 20 72 65 73 6f 75 72 63 65 73 0a 20 20  any resources.  
24106 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20    ** associated 
24107 77 69 74 68 20 73 75 63 68 20 61 20 76 61 6c 75  with such a valu
24108 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  e..    */.    sq
24109 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
2410a 61 73 65 28 26 75 2e 61 69 2e 63 74 78 2e 73 29  ase(&u.ai.ctx.s)
2410b 3b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ;.    goto no_me
2410c 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  m;.  }..  /* If 
2410d 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  the function ret
2410e 75 72 6e 65 64 20 61 6e 20 65 72 72 6f 72 2c 20  urned an error, 
2410f 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69  throw an excepti
24110 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 69  on */.  if( u.ai
24111 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a  .ctx.isError ){.
24112 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
24113 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
24114 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
24115 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26  te3_value_text(&
24116 75 2e 61 69 2e 63 74 78 2e 73 29 29 3b 0a 20 20  u.ai.ctx.s));.  
24117 20 20 72 63 20 3d 20 75 2e 61 69 2e 63 74 78 2e    rc = u.ai.ctx.
24118 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20  isError;.  }..  
24119 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73 75  /* Copy the resu
2411a 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  lt of the functi
2411b 6f 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  on into register
2411c 20 50 33 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33   P3 */.  sqlite3
2411d 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
2411e 6e 67 28 26 75 2e 61 69 2e 63 74 78 2e 73 2c 20  ng(&u.ai.ctx.s, 
2411f 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 71 6c  encoding);.  sql
24120 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
24121 70 4f 75 74 2c 20 26 75 2e 61 69 2e 63 74 78 2e  pOut, &u.ai.ctx.
24122 73 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  s);.  if( sqlite
24123 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70  3VdbeMemTooBig(p
24124 4f 75 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Out) ){.    goto
24125 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 23   too_big;.  }..#
24126 69 66 20 30 0a 20 20 2f 2a 20 54 68 65 20 61 70  if 0.  /* The ap
24127 70 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  p-defined functi
24128 6f 6e 20 68 61 73 20 64 6f 6e 65 20 73 6f 6d 65  on has done some
24129 74 68 69 6e 67 20 74 68 61 74 20 61 73 20 63 61  thing that as ca
2412a 75 73 65 64 20 74 68 69 73 0a 20 20 2a 2a 20 73  used this.  ** s
2412b 74 61 74 65 6d 65 6e 74 20 74 6f 20 65 78 70 69  tatement to expi
2412c 72 65 2e 20 20 28 50 65 72 68 61 70 73 20 74 68  re.  (Perhaps th
2412d 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65  e function calle
2412e 64 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29  d sqlite3_exec()
2412f 0a 20 20 2a 2a 20 77 69 74 68 20 61 20 43 52 45  .  ** with a CRE
24130 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
24131 65 6e 74 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28  ent.).  */.  if(
24132 20 70 2d 3e 65 78 70 69 72 65 64 20 29 20 72 63   p->expired ) rc
24133 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b   = SQLITE_ABORT;
24134 0a 23 65 6e 64 69 66 0a 0a 20 20 52 45 47 49 53  .#endif..  REGIS
24135 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
24136 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50 44 41  3, pOut);.  UPDA
24137 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
24138 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  pOut);.  break;.
24139 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69  }../* Opcode: Bi
2413a 74 41 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20  tAnd P1 P2 P3 * 
2413b 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65  *.**.** Take the
2413c 20 62 69 74 2d 77 69 73 65 20 41 4e 44 20 6f 66   bit-wise AND of
2413d 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72   the values in r
2413e 65 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 50  egister P1 and P
2413f 32 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74  2 and.** store t
24140 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67  he result in reg
24141 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20  ister P3..** If 
24142 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20  either input is 
24143 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74  NULL, the result
24144 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20   is NULL..*/./* 
24145 4f 70 63 6f 64 65 3a 20 42 69 74 4f 72 20 50 31  Opcode: BitOr P1
24146 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
24147 20 54 61 6b 65 20 74 68 65 20 62 69 74 2d 77 69   Take the bit-wi
24148 73 65 20 4f 52 20 6f 66 20 74 68 65 20 76 61 6c  se OR of the val
24149 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20  ues in register 
2414a 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a  P1 and P2 and.**
2414b 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
2414c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  t in register P3
2414d 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69  ..** If either i
2414e 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68  nput is NULL, th
2414f 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
24150 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
24151 53 68 69 66 74 4c 65 66 74 20 50 31 20 50 32 20  ShiftLeft P1 P2 
24152 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 68 69  P3 * *.**.** Shi
24153 66 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 76  ft the integer v
24154 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
24155 20 50 32 20 74 6f 20 74 68 65 20 6c 65 66 74 20   P2 to the left 
24156 62 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72  by the.** number
24157 20 6f 66 20 62 69 74 73 20 73 70 65 63 69 66 69   of bits specifi
24158 65 64 20 62 79 20 74 68 65 20 69 6e 74 65 67 65  ed by the intege
24159 72 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  r in register P1
2415a 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 72  ..** Store the r
2415b 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65  esult in registe
2415c 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68  r P3..** If eith
2415d 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
2415e 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  , the result is 
2415f 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  NULL..*/./* Opco
24160 64 65 3a 20 53 68 69 66 74 52 69 67 68 74 20 50  de: ShiftRight P
24161 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
24162 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65  * Shift the inte
24163 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67  ger value in reg
24164 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20  ister P2 to the 
24165 72 69 67 68 74 20 62 79 20 74 68 65 0a 2a 2a 20  right by the.** 
24166 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73  number of bits s
24167 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20  pecified by the 
24168 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 73  integer in regis
24169 74 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65  ter P1..** Store
2416a 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
2416b 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49  egister P3..** I
2416c 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
2416d 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
2416e 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63  lt is NULL..*/.c
2416f 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20  ase OP_BitAnd:  
24170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
24171 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54  * same as TK_BIT
24172 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  AND, in1, in2, o
24173 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 42  ut3 */.case OP_B
24174 69 74 4f 72 3a 20 20 20 20 20 20 20 20 20 20 20  itOr:           
24175 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
24176 73 20 54 4b 5f 42 49 54 4f 52 2c 20 69 6e 31 2c  s TK_BITOR, in1,
24177 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61   in2, out3 */.ca
24178 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 3a  se OP_ShiftLeft:
24179 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2417a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49   same as TK_LSHI
2417b 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75  FT, in1, in2, ou
2417c 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 68  t3 */.case OP_Sh
2417d 69 66 74 52 69 67 68 74 3a 20 7b 20 20 20 20 20  iftRight: {     
2417e 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
2417f 20 54 4b 5f 52 53 48 49 46 54 2c 20 69 6e 31 2c   TK_RSHIFT, in1,
24180 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69   in2, out3 */.#i
24181 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
24182 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
24183 74 6f 20 75 2e 61 6a 20 2a 2f 0a 20 20 69 36 34  to u.aj */.  i64
24184 20 69 41 3b 0a 20 20 75 36 34 20 75 41 3b 0a 20   iA;.  u64 uA;. 
24185 20 69 36 34 20 69 42 3b 0a 20 20 75 38 20 6f 70   i64 iB;.  u8 op
24186 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
24187 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
24188 64 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0a 0a  d into u.aj */..
24189 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70    pIn1 = &aMem[p
2418a 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 32 20  Op->p1];.  pIn2 
2418b 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  = &aMem[pOp->p2]
2418c 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d  ;.  pOut = &aMem
2418d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28  [pOp->p3];.  if(
2418e 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20   (pIn1->flags | 
2418f 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d  pIn2->flags) & M
24190 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73  EM_Null ){.    s
24191 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
24192 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20  Null(pOut);.    
24193 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 61  break;.  }.  u.a
24194 6a 2e 69 41 20 3d 20 73 71 6c 69 74 65 33 56 64  j.iA = sqlite3Vd
24195 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29  beIntValue(pIn2)
24196 3b 0a 20 20 75 2e 61 6a 2e 69 42 20 3d 20 73 71  ;.  u.aj.iB = sq
24197 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
24198 65 28 70 49 6e 31 29 3b 0a 20 20 75 2e 61 6a 2e  e(pIn1);.  u.aj.
24199 6f 70 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  op = pOp->opcode
2419a 3b 0a 20 20 69 66 28 20 75 2e 61 6a 2e 6f 70 3d  ;.  if( u.aj.op=
2419b 3d 4f 50 5f 42 69 74 41 6e 64 20 29 7b 0a 20 20  =OP_BitAnd ){.  
2419c 20 20 75 2e 61 6a 2e 69 41 20 26 3d 20 75 2e 61    u.aj.iA &= u.a
2419d 6a 2e 69 42 3b 0a 20 20 7d 65 6c 73 65 20 69 66  j.iB;.  }else if
2419e 28 20 75 2e 61 6a 2e 6f 70 3d 3d 4f 50 5f 42 69  ( u.aj.op==OP_Bi
2419f 74 4f 72 20 29 7b 0a 20 20 20 20 75 2e 61 6a 2e  tOr ){.    u.aj.
241a0 69 41 20 7c 3d 20 75 2e 61 6a 2e 69 42 3b 0a 20  iA |= u.aj.iB;. 
241a1 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 6a 2e   }else if( u.aj.
241a2 69 42 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73  iB!=0 ){.    ass
241a3 65 72 74 28 20 75 2e 61 6a 2e 6f 70 3d 3d 4f 50  ert( u.aj.op==OP
241a4 5f 53 68 69 66 74 52 69 67 68 74 20 7c 7c 20 75  _ShiftRight || u
241a5 2e 61 6a 2e 6f 70 3d 3d 4f 50 5f 53 68 69 66 74  .aj.op==OP_Shift
241a6 4c 65 66 74 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  Left );..    /* 
241a7 49 66 20 73 68 69 66 74 69 6e 67 20 62 79 20 61  If shifting by a
241a8 20 6e 65 67 61 74 69 76 65 20 61 6d 6f 75 6e 74   negative amount
241a9 2c 20 73 68 69 66 74 20 69 6e 20 74 68 65 20 6f  , shift in the o
241aa 74 68 65 72 20 64 69 72 65 63 74 69 6f 6e 20 2a  ther direction *
241ab 2f 0a 20 20 20 20 69 66 28 20 75 2e 61 6a 2e 69  /.    if( u.aj.i
241ac 42 3c 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  B<0 ){.      ass
241ad 65 72 74 28 20 4f 50 5f 53 68 69 66 74 52 69 67  ert( OP_ShiftRig
241ae 68 74 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 66 74  ht==OP_ShiftLeft
241af 2b 31 20 29 3b 0a 20 20 20 20 20 20 75 2e 61 6a  +1 );.      u.aj
241b0 2e 6f 70 20 3d 20 32 2a 4f 50 5f 53 68 69 66 74  .op = 2*OP_Shift
241b1 4c 65 66 74 20 2b 20 31 20 2d 20 75 2e 61 6a 2e  Left + 1 - u.aj.
241b2 6f 70 3b 0a 20 20 20 20 20 20 75 2e 61 6a 2e 69  op;.      u.aj.i
241b3 42 20 3d 20 75 2e 61 6a 2e 69 42 3e 28 2d 36 34  B = u.aj.iB>(-64
241b4 29 20 3f 20 2d 75 2e 61 6a 2e 69 42 20 3a 20 36  ) ? -u.aj.iB : 6
241b5 34 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  4;.    }..    if
241b6 28 20 75 2e 61 6a 2e 69 42 3e 3d 36 34 20 29 7b  ( u.aj.iB>=64 ){
241b7 0a 20 20 20 20 20 20 75 2e 61 6a 2e 69 41 20 3d  .      u.aj.iA =
241b8 20 28 75 2e 61 6a 2e 69 41 3e 3d 30 20 7c 7c 20   (u.aj.iA>=0 || 
241b9 75 2e 61 6a 2e 6f 70 3d 3d 4f 50 5f 53 68 69 66  u.aj.op==OP_Shif
241ba 74 4c 65 66 74 29 20 3f 20 30 20 3a 20 2d 31 3b  tLeft) ? 0 : -1;
241bb 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
241bc 20 20 6d 65 6d 63 70 79 28 26 75 2e 61 6a 2e 75    memcpy(&u.aj.u
241bd 41 2c 20 26 75 2e 61 6a 2e 69 41 2c 20 73 69 7a  A, &u.aj.iA, siz
241be 65 6f 66 28 75 2e 61 6a 2e 75 41 29 29 3b 0a 20  eof(u.aj.uA));. 
241bf 20 20 20 20 20 69 66 28 20 75 2e 61 6a 2e 6f 70       if( u.aj.op
241c0 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 29  ==OP_ShiftLeft )
241c1 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6a 2e 75  {.        u.aj.u
241c2 41 20 3c 3c 3d 20 75 2e 61 6a 2e 69 42 3b 0a 20  A <<= u.aj.iB;. 
241c3 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
241c4 20 20 20 20 75 2e 61 6a 2e 75 41 20 3e 3e 3d 20      u.aj.uA >>= 
241c5 75 2e 61 6a 2e 69 42 3b 0a 20 20 20 20 20 20 20  u.aj.iB;.       
241c6 20 2f 2a 20 53 69 67 6e 2d 65 78 74 65 6e 64 20   /* Sign-extend 
241c7 6f 6e 20 61 20 72 69 67 68 74 20 73 68 69 66 74  on a right shift
241c8 20 6f 66 20 61 20 6e 65 67 61 74 69 76 65 20 6e   of a negative n
241c9 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 20  umber */.       
241ca 20 69 66 28 20 75 2e 61 6a 2e 69 41 3c 30 20 29   if( u.aj.iA<0 )
241cb 20 75 2e 61 6a 2e 75 41 20 7c 3d 20 28 28 28 28   u.aj.uA |= ((((
241cc 75 36 34 29 30 78 66 66 66 66 66 66 66 66 29 3c  u64)0xffffffff)<
241cd 3c 33 32 29 7c 30 78 66 66 66 66 66 66 66 66 29  <32)|0xffffffff)
241ce 20 3c 3c 20 28 36 34 2d 75 2e 61 6a 2e 69 42 29   << (64-u.aj.iB)
241cf 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
241d0 6d 65 6d 63 70 79 28 26 75 2e 61 6a 2e 69 41 2c  memcpy(&u.aj.iA,
241d1 20 26 75 2e 61 6a 2e 75 41 2c 20 73 69 7a 65 6f   &u.aj.uA, sizeo
241d2 66 28 75 2e 61 6a 2e 69 41 29 29 3b 0a 20 20 20  f(u.aj.iA));.   
241d3 20 7d 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75   }.  }.  pOut->u
241d4 2e 69 20 3d 20 75 2e 61 6a 2e 69 41 3b 0a 20 20  .i = u.aj.iA;.  
241d5 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
241d6 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20  Out, MEM_Int);. 
241d7 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
241d8 63 6f 64 65 3a 20 41 64 64 49 6d 6d 20 20 50 31  code: AddImm  P1
241d9 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a   P2 * * *.** .**
241da 20 41 64 64 20 74 68 65 20 63 6f 6e 73 74 61 6e   Add the constan
241db 74 20 50 32 20 74 6f 20 74 68 65 20 76 61 6c 75  t P2 to the valu
241dc 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
241dd 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20  ..** The result 
241de 69 73 20 61 6c 77 61 79 73 20 61 6e 20 69 6e 74  is always an int
241df 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 66  eger..**.** To f
241e0 6f 72 63 65 20 61 6e 79 20 72 65 67 69 73 74 65  orce any registe
241e1 72 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67  r to be an integ
241e2 65 72 2c 20 6a 75 73 74 20 61 64 64 20 30 2e 0a  er, just add 0..
241e3 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64 49 6d  */.case OP_AddIm
241e4 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  m: {            
241e5 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31  /* in1 */.  pIn1
241e6 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31   = &aMem[pOp->p1
241e7 5d 3b 0a 20 20 6d 65 6d 41 62 6f 75 74 54 6f 43  ];.  memAboutToC
241e8 68 61 6e 67 65 28 70 2c 20 70 49 6e 31 29 3b 0a  hange(p, pIn1);.
241e9 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
241ea 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 29  Integerify(pIn1)
241eb 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d  ;.  pIn1->u.i +=
241ec 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 62 72 65 61   pOp->p2;.  brea
241ed 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
241ee 20 4d 75 73 74 42 65 49 6e 74 20 50 31 20 50 32   MustBeInt P1 P2
241ef 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f   * * *.** .** Fo
241f0 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  rce the value in
241f1 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20   register P1 to 
241f2 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20  be an integer.  
241f3 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  If the value.** 
241f4 69 6e 20 50 31 20 69 73 20 6e 6f 74 20 61 6e 20  in P1 is not an 
241f5 69 6e 74 65 67 65 72 20 61 6e 64 20 63 61 6e 6e  integer and cann
241f6 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20  ot be converted 
241f7 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 0a  into an integer.
241f8 2a 2a 20 77 69 74 68 6f 75 74 20 64 61 74 61 20  ** without data 
241f9 6c 6f 73 73 2c 20 74 68 65 6e 20 6a 75 6d 70 20  loss, then jump 
241fa 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50  immediately to P
241fb 32 2c 20 6f 72 20 69 66 20 50 32 3d 3d 30 0a 2a  2, or if P2==0.*
241fc 2a 20 72 61 69 73 65 20 61 6e 20 53 51 4c 49 54  * raise an SQLIT
241fd 45 5f 4d 49 53 4d 41 54 43 48 20 65 78 63 65 70  E_MISMATCH excep
241fe 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  tion..*/.case OP
241ff 5f 4d 75 73 74 42 65 49 6e 74 3a 20 7b 20 20 20  _MustBeInt: {   
24200 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
24201 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20  , in1 */.  pIn1 
24202 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  = &aMem[pOp->p1]
24203 3b 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74  ;.  applyAffinit
24204 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41  y(pIn1, SQLITE_A
24205 46 46 5f 4e 55 4d 45 52 49 43 2c 20 65 6e 63 6f  FF_NUMERIC, enco
24206 64 69 6e 67 29 3b 0a 20 20 69 66 28 20 28 70 49  ding);.  if( (pI
24207 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
24208 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  Int)==0 ){.    i
24209 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b  f( pOp->p2==0 ){
2420a 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
2420b 54 45 5f 4d 49 53 4d 41 54 43 48 3b 0a 20 20 20  TE_MISMATCH;.   
2420c 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
2420d 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
2420e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 63 20  }else{.      pc 
2420f 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
24210 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
24211 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
24212 28 70 49 6e 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pIn1, MEM_Int);
24213 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
24214 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
24215 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
24216 49 4e 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  INT./* Opcode: R
24217 65 61 6c 41 66 66 69 6e 69 74 79 20 50 31 20 2a  ealAffinity P1 *
24218 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20   * * *.**.** If 
24219 72 65 67 69 73 74 65 72 20 50 31 20 68 6f 6c 64  register P1 hold
2421a 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e  s an integer con
2421b 76 65 72 74 20 69 74 20 74 6f 20 61 20 72 65 61  vert it to a rea
2421c 6c 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54  l value..**.** T
2421d 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73  his opcode is us
2421e 65 64 20 77 68 65 6e 20 65 78 74 72 61 63 74 69  ed when extracti
2421f 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ng information f
24220 72 6f 6d 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61  rom a column tha
24221 74 0a 2a 2a 20 68 61 73 20 52 45 41 4c 20 61 66  t.** has REAL af
24222 66 69 6e 69 74 79 2e 20 20 53 75 63 68 20 63 6f  finity.  Such co
24223 6c 75 6d 6e 20 76 61 6c 75 65 73 20 6d 61 79 20  lumn values may 
24224 73 74 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20  still be stored 
24225 61 73 0a 2a 2a 20 69 6e 74 65 67 65 72 73 2c 20  as.** integers, 
24226 66 6f 72 20 73 70 61 63 65 20 65 66 66 69 63 69  for space effici
24227 65 6e 63 79 2c 20 62 75 74 20 61 66 74 65 72 20  ency, but after 
24228 65 78 74 72 61 63 74 69 6f 6e 20 77 65 20 77 61  extraction we wa
24229 6e 74 20 74 68 65 6d 0a 2a 2a 20 74 6f 20 68 61  nt them.** to ha
2422a 76 65 20 6f 6e 6c 79 20 61 20 72 65 61 6c 20 76  ve only a real v
2422b 61 6c 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  alue..*/.case OP
2422c 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 3a 20 7b  _RealAffinity: {
2422d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2422e 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 70 49    /* in1 */.  pI
2422f 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n1 = &aMem[pOp->
24230 70 31 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d  p1];.  if( pIn1-
24231 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
24232 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
24233 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49  dbeMemRealify(pI
24234 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  n1);.  }.  break
24235 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ;.}.#endif..#ifn
24236 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
24237 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  CAST./* Opcode: 
24238 54 6f 54 65 78 74 20 50 31 20 2a 20 2a 20 2a 20  ToText P1 * * * 
24239 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68  *.**.** Force th
2423a 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
2423b 74 65 72 20 50 31 20 74 6f 20 62 65 20 74 65 78  ter P1 to be tex
2423c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c  t..** If the val
2423d 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63  ue is numeric, c
2423e 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73  onvert it to a s
2423f 74 72 69 6e 67 20 75 73 69 6e 67 20 74 68 65 0a  tring using the.
24240 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66  ** equivalent of
24241 20 70 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f 62   printf().  Blob
24242 20 76 61 6c 75 65 73 20 61 72 65 20 75 6e 63 68   values are unch
24243 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72 65  anged and.** are
24244 20 61 66 74 65 72 77 61 72 64 73 20 73 69 6d 70   afterwards simp
24245 6c 79 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  ly interpreted a
24246 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41 20  s text..**.** A 
24247 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f  NULL value is no
24248 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69  t changed by thi
24249 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72  s routine.  It r
2424a 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  emains NULL..*/.
2424b 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a 20  case OP_ToText: 
2424c 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
2424d 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
2424e 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a 2f  _TO_TEXT, in1 */
2424f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b  .  pIn1 = &aMem[
24250 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 6d 65 6d 41  pOp->p1];.  memA
24251 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 70 2c 20  boutToChange(p, 
24252 70 49 6e 31 29 3b 0a 20 20 69 66 28 20 70 49 6e  pIn1);.  if( pIn
24253 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  1->flags & MEM_N
24254 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 61  ull ) break;.  a
24255 73 73 65 72 74 28 20 4d 45 4d 5f 53 74 72 3d 3d  ssert( MEM_Str==
24256 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 29 3b  (MEM_Blob>>3) );
24257 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c  .  pIn1->flags |
24258 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d  = (pIn1->flags&M
24259 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 61  EM_Blob)>>3;.  a
2425a 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e  pplyAffinity(pIn
2425b 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45  1, SQLITE_AFF_TE
2425c 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  XT, encoding);. 
2425d 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c 6f 62   rc = ExpandBlob
2425e 28 70 49 6e 31 29 3b 0a 20 20 61 73 73 65 72 74  (pIn1);.  assert
2425f 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20  ( pIn1->flags & 
24260 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d  MEM_Str || db->m
24261 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
24262 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20   pIn1->flags &= 
24263 7e 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65  ~(MEM_Int|MEM_Re
24264 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f  al|MEM_Blob|MEM_
24265 5a 65 72 6f 29 3b 0a 20 20 55 50 44 41 54 45 5f  Zero);.  UPDATE_
24266 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e  MAX_BLOBSIZE(pIn
24267 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  1);.  break;.}..
24268 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 42 6c 6f  /* Opcode: ToBlo
24269 62 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a  b P1 * * * *.**.
2426a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c  ** Force the val
2426b 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
2426c 31 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 2e 0a  1 to be a BLOB..
2426d 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  ** If the value 
2426e 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f 6e 76  is numeric, conv
2426f 65 72 74 20 69 74 20 74 6f 20 61 20 73 74 72 69  ert it to a stri
24270 6e 67 20 66 69 72 73 74 2e 0a 2a 2a 20 53 74 72  ng first..** Str
24271 69 6e 67 73 20 61 72 65 20 73 69 6d 70 6c 79 20  ings are simply 
24272 72 65 69 6e 74 65 72 70 72 65 74 65 64 20 61 73  reinterpreted as
24273 20 62 6c 6f 62 73 20 77 69 74 68 20 6e 6f 20 63   blobs with no c
24274 68 61 6e 67 65 0a 2a 2a 20 74 6f 20 74 68 65 20  hange.** to the 
24275 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 2e  underlying data.
24276 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61  .**.** A NULL va
24277 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  lue is not chang
24278 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
24279 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20  ne.  It remains 
2427a 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NULL..*/.case OP
2427b 5f 54 6f 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20  _ToBlob: {      
2427c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
2427d 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f  ame as TK_TO_BLO
2427e 42 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31  B, in1 */.  pIn1
2427f 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31   = &aMem[pOp->p1
24280 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66  ];.  if( pIn1->f
24281 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
24282 29 20 62 72 65 61 6b 3b 0a 20 20 69 66 28 20 28  ) break;.  if( (
24283 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
24284 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a 20 20  M_Blob)==0 ){.  
24285 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28    applyAffinity(
24286 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46  pIn1, SQLITE_AFF
24287 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29  _TEXT, encoding)
24288 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49  ;.    assert( pI
24289 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
2428a 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Str || db->mallo
2428b 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 4d  cFailed );.    M
2428c 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49  emSetTypeFlag(pI
2428d 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20  n1, MEM_Blob);. 
2428e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 31   }else{.    pIn1
2428f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d  ->flags &= ~(MEM
24290 5f 54 79 70 65 4d 61 73 6b 26 7e 4d 45 4d 5f 42  _TypeMask&~MEM_B
24291 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20 55 50 44 41  lob);.  }.  UPDA
24292 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
24293 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  pIn1);.  break;.
24294 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f  }../* Opcode: To
24295 4e 75 6d 65 72 69 63 20 50 31 20 2a 20 2a 20 2a  Numeric P1 * * *
24296 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74   *.**.** Force t
24297 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
24298 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 6e 75  ster P1 to be nu
24299 6d 65 72 69 63 20 28 65 69 74 68 65 72 20 61 6e  meric (either an
2429a 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72 20 61  .** integer or a
2429b 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20   floating-point 
2429c 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66 20 74  number.).** If t
2429d 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 74  he value is text
2429e 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f   or blob, try to
2429f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
242a0 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65  n using the.** e
242a1 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f  quivalent of ato
242a2 69 28 29 20 6f 72 20 61 74 6f 66 28 29 20 61 6e  i() or atof() an
242a3 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20  d store 0 if no 
242a4 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20  such conversion 
242a5 0a 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 2e  .** is possible.
242a6 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61  .**.** A NULL va
242a7 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  lue is not chang
242a8 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
242a9 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20  ne.  It remains 
242aa 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NULL..*/.case OP
242ab 5f 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20 20 20  _ToNumeric: {   
242ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
242ad 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
242ae 4e 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a 2f 0a  NUMERIC, in1 */.
242af 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70    pIn1 = &aMem[p
242b0 4f 70 2d 3e 70 31 5d 3b 0a 20 20 73 71 6c 69 74  Op->p1];.  sqlit
242b1 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66  e3VdbeMemNumerif
242b2 79 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b  y(pIn1);.  break
242b3 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
242b4 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a  LITE_OMIT_CAST *
242b5 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f  /../* Opcode: To
242b6 49 6e 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  Int P1 * * * *.*
242b7 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76  *.** Force the v
242b8 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
242b9 20 50 31 20 74 6f 20 62 65 20 61 6e 20 69 6e 74   P1 to be an int
242ba 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20 54 68 65  eger.  If.** The
242bb 20 76 61 6c 75 65 20 69 73 20 63 75 72 72 65 6e   value is curren
242bc 74 6c 79 20 61 20 72 65 61 6c 20 6e 75 6d 62 65  tly a real numbe
242bd 72 2c 20 64 72 6f 70 20 69 74 73 20 66 72 61 63  r, drop its frac
242be 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a 2a 2a 20  tional part..** 
242bf 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  If the value is 
242c0 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72  text or blob, tr
242c1 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20  y to convert it 
242c2 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 75 73  to an integer us
242c3 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76  ing the.** equiv
242c4 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 20  alent of atoi() 
242c5 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e  and store 0 if n
242c6 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f  o such conversio
242c7 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a  n is possible..*
242c8 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75  *.** A NULL valu
242c9 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64  e is not changed
242ca 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65   by this routine
242cb 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55  .  It remains NU
242cc 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54  LL..*/.case OP_T
242cd 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20  oInt: {         
242ce 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
242cf 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 2c 20 69   as TK_TO_INT, i
242d0 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26  n1 */.  pIn1 = &
242d1 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
242d2 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67   if( (pIn1->flag
242d3 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30  s & MEM_Null)==0
242d4 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
242d5 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
242d6 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72  (pIn1);.  }.  br
242d7 65 61 6b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66  eak;.}..#if !def
242d8 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
242d9 5f 43 41 53 54 29 20 26 26 20 21 64 65 66 69 6e  _CAST) && !defin
242da 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ed(SQLITE_OMIT_F
242db 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 2f  LOATING_POINT)./
242dc 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 52 65 61 6c  * Opcode: ToReal
242dd 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a   P1 * * * *.**.*
242de 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75  * Force the valu
242df 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
242e0 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e   to be a floatin
242e1 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 0a  g point number..
242e2 2a 2a 20 49 66 20 54 68 65 20 76 61 6c 75 65 20  ** If The value 
242e3 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6e 20  is currently an 
242e4 69 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65 72 74  integer, convert
242e5 20 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76   it..** If the v
242e6 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72 20  alue is text or 
242e7 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e  blob, try to con
242e8 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20 69 6e  vert it to an in
242e9 74 65 67 65 72 20 75 73 69 6e 67 20 74 68 65 0a  teger using the.
242ea 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66  ** equivalent of
242eb 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74 6f 72   atoi() and stor
242ec 65 20 30 2e 30 20 69 66 20 6e 6f 20 73 75 63 68  e 0.0 if no such
242ed 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70   conversion is p
242ee 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  ossible..**.** A
242ef 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e   NULL value is n
242f0 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68  ot changed by th
242f1 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20  is routine.  It 
242f2 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f  remains NULL..*/
242f3 0a 63 61 73 65 20 4f 50 5f 54 6f 52 65 61 6c 3a  .case OP_ToReal:
242f4 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
242f5 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
242f6 4b 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31 20 2a  K_TO_REAL, in1 *
242f7 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d  /.  pIn1 = &aMem
242f8 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 6d 65 6d  [pOp->p1];.  mem
242f9 41 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 70 2c  AboutToChange(p,
242fa 20 70 49 6e 31 29 3b 0a 20 20 69 66 28 20 28 70   pIn1);.  if( (p
242fb 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
242fc 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20  _Null)==0 ){.   
242fd 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
242fe 65 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20  ealify(pIn1);.  
242ff 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  }.  break;.}.#en
24300 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
24301 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54  SQLITE_OMIT_CAST
24302 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
24303 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
24304 4e 47 5f 50 4f 49 4e 54 29 20 2a 2f 0a 0a 2f 2a  NG_POINT) */../*
24305 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50 31 20 50   Opcode: Lt P1 P
24306 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a  2 P3 P4 P5.**.**
24307 20 43 6f 6d 70 61 72 65 20 74 68 65 20 76 61 6c   Compare the val
24308 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20  ues in register 
24309 50 31 20 61 6e 64 20 50 33 2e 20 20 49 66 20 72  P1 and P3.  If r
2430a 65 67 28 50 33 29 3c 72 65 67 28 50 31 29 20 74  eg(P3)<reg(P1) t
2430b 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 61  hen.** jump to a
2430c 64 64 72 65 73 73 20 50 32 2e 20 20 0a 2a 2a 0a  ddress P2.  .**.
2430d 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45  ** If the SQLITE
2430e 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20  _JUMPIFNULL bit 
2430f 6f 66 20 50 35 20 69 73 20 73 65 74 20 61 6e 64  of P5 is set and
24310 20 65 69 74 68 65 72 20 72 65 67 28 50 31 29 20   either reg(P1) 
24311 6f 72 0a 2a 2a 20 72 65 67 28 50 33 29 20 69 73  or.** reg(P3) is
24312 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20   NULL then take 
24313 74 68 65 20 6a 75 6d 70 2e 20 20 49 66 20 74 68  the jump.  If th
24314 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  e SQLITE_JUMPIFN
24315 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 69 73 20 63  ULL .** bit is c
24316 6c 65 61 72 20 74 68 65 6e 20 66 61 6c 6c 20 74  lear then fall t
24317 68 72 6f 75 67 68 20 69 66 20 65 69 74 68 65 72  hrough if either
24318 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
24319 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ..**.** The SQLI
2431a 54 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f 72 74  TE_AFF_MASK port
2431b 69 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 20 62  ion of P5 must b
2431c 65 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68  e an affinity ch
2431d 61 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c  aracter -.** SQL
2431e 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 53 51  ITE_AFF_TEXT, SQ
2431f 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
24320 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20  , and so forth. 
24321 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  An attempt is ma
24322 64 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65  de .** to coerce
24323 20 62 6f 74 68 20 69 6e 70 75 74 73 20 61 63 63   both inputs acc
24324 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 61  ording to this a
24325 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 74  ffinity before t
24326 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e  he.** comparison
24327 20 69 73 20 6d 61 64 65 2e 20 49 66 20 74 68 65   is made. If the
24328 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b   SQLITE_AFF_MASK
24329 20 69 73 20 30 78 30 30 2c 20 74 68 65 6e 20 6e   is 0x00, then n
2432a 75 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 6e 69  umeric.** affini
2432b 74 79 20 69 73 20 75 73 65 64 2e 20 4e 6f 74 65  ty is used. Note
2432c 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e 69   that the affini
2432d 74 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61  ty conversions a
2432e 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 61 63  re stored.** bac
2432f 6b 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74  k into the input
24330 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e   registers P1 an
24331 64 20 50 33 2e 20 20 53 6f 20 74 68 69 73 20 6f  d P3.  So this o
24332 70 63 6f 64 65 20 63 61 6e 20 63 61 75 73 65 0a  pcode can cause.
24333 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 20 63 68  ** persistent ch
24334 61 6e 67 65 73 20 74 6f 20 72 65 67 69 73 74 65  anges to registe
24335 72 73 20 50 31 20 61 6e 64 20 50 33 2e 0a 2a 2a  rs P1 and P3..**
24336 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e  .** Once any con
24337 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74 61  versions have ta
24338 6b 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e  ken place, and n
24339 65 69 74 68 65 72 20 76 61 6c 75 65 20 69 73 20  either value is 
2433a 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61  NULL, .** the va
2433b 6c 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72 65  lues are compare
2433c 64 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65  d. If both value
2433d 73 20 61 72 65 20 62 6c 6f 62 73 20 74 68 65 6e  s are blobs then
2433e 20 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a 2a 20   memcmp() is.** 
2433f 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
24340 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66  e the results of
24341 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e   the comparison.
24342 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73    If both values
24343 0a 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74 68  .** are text, th
24344 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  en the appropria
24345 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e  te collating fun
24346 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20  ction specified 
24347 69 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 73 65  in.** P4 is  use
24348 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70  d to do the comp
24349 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20 69  arison.  If P4 i
2434a 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20  s not specified 
2434b 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 29  then.** memcmp()
2434c 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70   is used to comp
2434d 61 72 65 20 74 65 78 74 20 73 74 72 69 6e 67 2e  are text string.
2434e 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73    If both values
2434f 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 2c   are.** numeric,
24350 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63 20   then a numeric 
24351 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75 73  comparison is us
24352 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f 20 76  ed. If the two v
24353 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20  alues.** are of 
24354 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 2c  different types,
24355 20 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 61 72   then numbers ar
24356 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73  e considered les
24357 73 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 6e 67  s than.** string
24358 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 72  s and strings ar
24359 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73  e considered les
2435a 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2a  s than blobs..**
2435b 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54  .** If the SQLIT
2435c 45 5f 53 54 4f 52 45 50 32 20 62 69 74 20 6f 66  E_STOREP2 bit of
2435d 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e   P5 is set, then
2435e 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49   do not jump.  I
2435f 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 65  nstead,.** store
24360 20 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c   a boolean resul
24361 74 20 28 65 69 74 68 65 72 20 30 2c 20 6f 72 20  t (either 0, or 
24362 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 72  1, or NULL) in r
24363 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a  egister P2..**.*
24364 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f  * If the SQLITE_
24365 4e 55 4c 4c 45 51 20 62 69 74 20 69 73 20 73 65  NULLEQ bit is se
24366 74 20 69 6e 20 50 35 2c 20 74 68 65 6e 20 4e 55  t in P5, then NU
24367 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f  LL values are co
24368 6e 73 69 64 65 72 65 64 0a 2a 2a 20 65 71 75 61  nsidered.** equa
24369 6c 20 74 6f 20 6f 6e 65 20 61 6e 6f 74 68 65 72  l to one another
2436a 2c 20 70 72 6f 76 69 64 65 64 20 74 68 61 74 20  , provided that 
2436b 74 68 65 79 20 64 6f 20 6e 6f 74 20 68 61 76 65  they do not have
2436c 20 74 68 65 69 72 20 4d 45 4d 5f 43 6c 65 61 72   their MEM_Clear
2436d 65 64 0a 2a 2a 20 62 69 74 20 73 65 74 2e 0a 2a  ed.** bit set..*
2436e 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 20  /./* Opcode: Ne 
2436f 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
24370 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20  *.** This works 
24371 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74  just like the Lt
24372 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74   opcode except t
24373 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20  hat the jump is 
24374 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20  taken if.** the 
24375 6f 70 65 72 61 6e 64 73 20 69 6e 20 72 65 67 69  operands in regi
24376 73 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 20  sters P1 and P3 
24377 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20  are not equal.  
24378 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64  See the Lt opcod
24379 65 20 66 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f  e for.** additio
2437a 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
2437b 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45  .**.** If SQLITE
2437c 5f 4e 55 4c 4c 45 51 20 69 73 20 73 65 74 20 69  _NULLEQ is set i
2437d 6e 20 50 35 20 74 68 65 6e 20 74 68 65 20 72 65  n P5 then the re
2437e 73 75 6c 74 20 6f 66 20 63 6f 6d 70 61 72 69 73  sult of comparis
2437f 6f 6e 20 69 73 20 61 6c 77 61 79 73 20 65 69 74  on is always eit
24380 68 65 72 0a 2a 2a 20 74 72 75 65 20 6f 72 20 66  her.** true or f
24381 61 6c 73 65 20 61 6e 64 20 69 73 20 6e 65 76 65  alse and is neve
24382 72 20 4e 55 4c 4c 2e 20 20 49 66 20 62 6f 74 68  r NULL.  If both
24383 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 4e 55   operands are NU
24384 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  LL then the resu
24385 6c 74 0a 2a 2a 20 6f 66 20 63 6f 6d 70 61 72 69  lt.** of compari
24386 73 6f 6e 20 69 73 20 66 61 6c 73 65 2e 20 20 49  son is false.  I
24387 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64  f either operand
24388 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68   is NULL then th
24389 65 20 72 65 73 75 6c 74 20 69 73 20 74 72 75 65  e result is true
2438a 2e 0a 2a 2a 20 49 66 20 6e 65 69 74 68 65 72 20  ..** If neither 
2438b 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20  operand is NULL 
2438c 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 68  the result is th
2438d 65 20 73 61 6d 65 20 61 73 20 69 74 20 77 6f 75  e same as it wou
2438e 6c 64 20 62 65 20 69 66 0a 2a 2a 20 74 68 65 20  ld be if.** the 
2438f 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 66 6c  SQLITE_NULLEQ fl
24390 61 67 20 77 65 72 65 20 6f 6d 69 74 74 65 64 20  ag were omitted 
24391 66 72 6f 6d 20 50 35 2e 0a 2a 2f 0a 2f 2a 20 4f  from P5..*/./* O
24392 70 63 6f 64 65 3a 20 45 71 20 50 31 20 50 32 20  pcode: Eq P1 P2 
24393 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54  P3 P4 P5.**.** T
24394 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c  his works just l
24395 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64  ike the Lt opcod
24396 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68  e except that th
24397 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20  e jump is taken 
24398 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 6e  if.** the operan
24399 64 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 20  ds in registers 
2439a 50 31 20 61 6e 64 20 50 33 20 61 72 65 20 65 71  P1 and P3 are eq
2439b 75 61 6c 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20  ual..** See the 
2439c 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64  Lt opcode for ad
2439d 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
2439e 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53  tion..**.** If S
2439f 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73 20  QLITE_NULLEQ is 
243a0 73 65 74 20 69 6e 20 50 35 20 74 68 65 6e 20 74  set in P5 then t
243a1 68 65 20 72 65 73 75 6c 74 20 6f 66 20 63 6f 6d  he result of com
243a2 70 61 72 69 73 6f 6e 20 69 73 20 61 6c 77 61 79  parison is alway
243a3 73 20 65 69 74 68 65 72 0a 2a 2a 20 74 72 75 65  s either.** true
243a4 20 6f 72 20 66 61 6c 73 65 20 61 6e 64 20 69 73   or false and is
243a5 20 6e 65 76 65 72 20 4e 55 4c 4c 2e 20 20 49 66   never NULL.  If
243a6 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61   both operands a
243a7 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65  re NULL then the
243a8 20 72 65 73 75 6c 74 0a 2a 2a 20 6f 66 20 63 6f   result.** of co
243a9 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 72 75 65  mparison is true
243aa 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 70 65  .  If either ope
243ab 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 65  rand is NULL the
243ac 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  n the result is 
243ad 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 6e 65 69  false..** If nei
243ae 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
243af 4e 55 4c 4c 20 74 68 65 20 72 65 73 75 6c 74 20  NULL the result 
243b0 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 69  is the same as i
243b1 74 20 77 6f 75 6c 64 20 62 65 20 69 66 0a 2a 2a  t would be if.**
243b2 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c   the SQLITE_NULL
243b3 45 51 20 66 6c 61 67 20 77 65 72 65 20 6f 6d 69  EQ flag were omi
243b4 74 74 65 64 20 66 72 6f 6d 20 50 35 2e 0a 2a 2f  tted from P5..*/
243b5 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 65 20 50  ./* Opcode: Le P
243b6 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
243b7 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a  .** This works j
243b8 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20  ust like the Lt 
243b9 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68  opcode except th
243ba 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74  at the jump is t
243bb 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63  aken if.** the c
243bc 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74  ontent of regist
243bd 65 72 20 50 33 20 69 73 20 6c 65 73 73 20 74 68  er P3 is less th
243be 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
243bf 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a  he content of.**
243c0 20 72 65 67 69 73 74 65 72 20 50 31 2e 20 20 53   register P1.  S
243c1 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65  ee the Lt opcode
243c2 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
243c3 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
243c4 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 74 20 50 31  /* Opcode: Gt P1
243c5 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a   P2 P3 P4 P5.**.
243c6 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75  ** This works ju
243c7 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f  st like the Lt o
243c8 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61  pcode except tha
243c9 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61  t the jump is ta
243ca 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f  ken if.** the co
243cb 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65  ntent of registe
243cc 72 20 50 33 20 69 73 20 67 72 65 61 74 65 72 20  r P3 is greater 
243cd 74 68 61 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74  than the content
243ce 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20   of.** register 
243cf 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20  P1.  See the Lt 
243d0 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74  opcode for addit
243d1 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
243d2 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  n..*/./* Opcode:
243d3 20 47 65 20 50 31 20 50 32 20 50 33 20 50 34 20   Ge P1 P2 P3 P4 
243d4 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f  P5.**.** This wo
243d5 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68  rks just like th
243d6 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65  e Lt opcode exce
243d7 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70  pt that the jump
243d8 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20   is taken if.** 
243d9 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
243da 65 67 69 73 74 65 72 20 50 33 20 69 73 20 67 72  egister P3 is gr
243db 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71  eater than or eq
243dc 75 61 6c 20 74 6f 20 74 68 65 20 63 6f 6e 74 65  ual to the conte
243dd 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65  nt of.** registe
243de 72 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c  r P1.  See the L
243df 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64  t opcode for add
243e0 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
243e1 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ion..*/.case OP_
243e2 45 71 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  Eq:             
243e3 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
243e4 45 51 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69  EQ, jump, in1, i
243e5 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65  n3 */.case OP_Ne
243e6 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :               
243e7 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45  /* same as TK_NE
243e8 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33  , jump, in1, in3
243e9 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 74 3a 20   */.case OP_Lt: 
243ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
243eb 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54 2c 20   same as TK_LT, 
243ec 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a  jump, in1, in3 *
243ed 2f 0a 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20  /.case OP_Le:   
243ee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
243ef 61 6d 65 20 61 73 20 54 4b 5f 4c 45 2c 20 6a 75  ame as TK_LE, ju
243f0 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a  mp, in1, in3 */.
243f1 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20 20  case OP_Gt:     
243f2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
243f3 65 20 61 73 20 54 4b 5f 47 54 2c 20 6a 75 6d 70  e as TK_GT, jump
243f4 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61  , in1, in3 */.ca
243f5 73 65 20 4f 50 5f 47 65 3a 20 7b 20 20 20 20 20  se OP_Ge: {     
243f6 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
243f7 61 73 20 54 4b 5f 47 45 2c 20 6a 75 6d 70 2c 20  as TK_GE, jump, 
243f8 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20  in1, in3 */.#if 
243f9 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
243fa 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
243fb 20 75 2e 61 6b 20 2a 2f 0a 20 20 69 6e 74 20 72   u.ak */.  int r
243fc 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  es;            /
243fd 2a 20 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20  * Result of the 
243fe 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 70 49  comparison of pI
243ff 6e 31 20 61 67 61 69 6e 73 74 20 70 49 6e 33 20  n1 against pIn3 
24400 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69  */.  char affini
24401 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 66 69  ty;      /* Affi
24402 6e 69 74 79 20 74 6f 20 75 73 65 20 66 6f 72 20  nity to use for 
24403 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a 20 20  comparison */.  
24404 75 31 36 20 66 6c 61 67 73 31 3b 20 20 20 20 20  u16 flags1;     
24405 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 69      /* Copy of i
24406 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 6f 66 20  nitial value of 
24407 70 49 6e 31 2d 3e 66 6c 61 67 73 20 2a 2f 0a 20  pIn1->flags */. 
24408 20 75 31 36 20 66 6c 61 67 73 33 3b 20 20 20 20   u16 flags3;    
24409 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20       /* Copy of 
2440a 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 6f 66  initial value of
2440b 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 2a 2f 0a   pIn3->flags */.
2440c 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
2440d 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
2440e 69 6e 74 6f 20 75 2e 61 6b 20 2a 2f 0a 0a 20 20  into u.ak */..  
2440f 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn1 = &aMem[pOp
24410 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 33 20 3d 20  ->p1];.  pIn3 = 
24411 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  &aMem[pOp->p3];.
24412 20 20 75 2e 61 6b 2e 66 6c 61 67 73 31 20 3d 20    u.ak.flags1 = 
24413 70 49 6e 31 2d 3e 66 6c 61 67 73 3b 0a 20 20 75  pIn1->flags;.  u
24414 2e 61 6b 2e 66 6c 61 67 73 33 20 3d 20 70 49 6e  .ak.flags3 = pIn
24415 33 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20  3->flags;.  if( 
24416 28 75 2e 61 6b 2e 66 6c 61 67 73 31 20 7c 20 75  (u.ak.flags1 | u
24417 2e 61 6b 2e 66 6c 61 67 73 33 29 26 4d 45 4d 5f  .ak.flags3)&MEM_
24418 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 4f  Null ){.    /* O
24419 6e 65 20 6f 72 20 62 6f 74 68 20 6f 70 65 72 61  ne or both opera
2441a 6e 64 73 20 61 72 65 20 4e 55 4c 4c 20 2a 2f 0a  nds are NULL */.
2441b 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20      if( pOp->p5 
2441c 26 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20  & SQLITE_NULLEQ 
2441d 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 53  ){.      /* If S
2441e 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73 20  QLITE_NULLEQ is 
2441f 73 65 74 20 28 77 68 69 63 68 20 77 69 6c 6c 20  set (which will 
24420 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20 74  only happen if t
24421 68 65 20 6f 70 65 72 61 74 6f 72 20 69 73 0a 20  he operator is. 
24422 20 20 20 20 20 2a 2a 20 4f 50 5f 45 71 20 6f 72       ** OP_Eq or
24423 20 4f 50 5f 4e 65 29 20 74 68 65 6e 20 74 61 6b   OP_Ne) then tak
24424 65 20 74 68 65 20 6a 75 6d 70 20 6f 72 20 6e 6f  e the jump or no
24425 74 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77  t depending on w
24426 68 65 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20  hether.      ** 
24427 6f 72 20 6e 6f 74 20 62 6f 74 68 20 6f 70 65 72  or not both oper
24428 61 6e 64 73 20 61 72 65 20 6e 75 6c 6c 2e 0a 20  ands are null.. 
24429 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73       */.      as
2442a 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64  sert( pOp->opcod
2442b 65 3d 3d 4f 50 5f 45 71 20 7c 7c 20 70 4f 70 2d  e==OP_Eq || pOp-
2442c 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65 20 29  >opcode==OP_Ne )
2442d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2442e 28 75 2e 61 6b 2e 66 6c 61 67 73 31 20 26 20 4d  (u.ak.flags1 & M
2442f 45 4d 5f 43 6c 65 61 72 65 64 29 3d 3d 30 20 29  EM_Cleared)==0 )
24430 3b 0a 20 20 20 20 20 20 69 66 28 20 28 75 2e 61  ;.      if( (u.a
24431 6b 2e 66 6c 61 67 73 31 26 4d 45 4d 5f 4e 75 6c  k.flags1&MEM_Nul
24432 6c 29 21 3d 30 0a 20 20 20 20 20 20 20 26 26 20  l)!=0.       && 
24433 28 75 2e 61 6b 2e 66 6c 61 67 73 33 26 4d 45 4d  (u.ak.flags3&MEM
24434 5f 4e 75 6c 6c 29 21 3d 30 0a 20 20 20 20 20 20  _Null)!=0.      
24435 20 26 26 20 28 75 2e 61 6b 2e 66 6c 61 67 73 33   && (u.ak.flags3
24436 26 4d 45 4d 5f 43 6c 65 61 72 65 64 29 3d 3d 30  &MEM_Cleared)==0
24437 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
24438 20 20 75 2e 61 6b 2e 72 65 73 20 3d 20 30 3b 20    u.ak.res = 0; 
24439 20 2f 2a 20 52 65 73 75 6c 74 73 20 61 72 65 20   /* Results are 
2443a 65 71 75 61 6c 20 2a 2f 0a 20 20 20 20 20 20 7d  equal */.      }
2443b 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e  else{.        u.
2443c 61 6b 2e 72 65 73 20 3d 20 31 3b 20 20 2f 2a 20  ak.res = 1;  /* 
2443d 52 65 73 75 6c 74 73 20 61 72 65 20 6e 6f 74 20  Results are not 
2443e 65 71 75 61 6c 20 2a 2f 0a 20 20 20 20 20 20 7d  equal */.      }
2443f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
24440 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 55 4c 4c    /* SQLITE_NULL
24441 45 51 20 69 73 20 63 6c 65 61 72 20 61 6e 64 20  EQ is clear and 
24442 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70 65  at least one ope
24443 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 0a 20 20  rand is NULL,.  
24444 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20      ** then the 
24445 72 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73  result is always
24446 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 2a 2a 20   NULL..      ** 
24447 54 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65  The jump is take
24448 6e 20 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f  n if the SQLITE_
24449 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20 69  JUMPIFNULL bit i
2444a 73 20 73 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a  s set..      */.
2444b 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70        if( pOp->p
2444c 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f 52 45  5 & SQLITE_STORE
2444d 50 32 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4f  P2 ){.        pO
2444e 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  ut = &aMem[pOp->
2444f 70 32 5d 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d  p2];.        Mem
24450 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74  SetTypeFlag(pOut
24451 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 20  , MEM_Null);.   
24452 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52       REGISTER_TR
24453 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75  ACE(pOp->p2, pOu
24454 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  t);.      }else 
24455 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51  if( pOp->p5 & SQ
24456 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20  LITE_JUMPIFNULL 
24457 29 7b 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20  ){.        pc = 
24458 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 20 20 20  pOp->p2-1;.     
24459 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
2445a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
2445b 20 20 20 2f 2a 20 4e 65 69 74 68 65 72 20 6f 70     /* Neither op
2445c 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2e 20 20  erand is NULL.  
2445d 44 6f 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 2e  Do a comparison.
2445e 20 2a 2f 0a 20 20 20 20 75 2e 61 6b 2e 61 66 66   */.    u.ak.aff
2445f 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 35 20  inity = pOp->p5 
24460 26 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53  & SQLITE_AFF_MAS
24461 4b 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6b 2e  K;.    if( u.ak.
24462 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20  affinity ){.    
24463 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28    applyAffinity(
24464 70 49 6e 31 2c 20 75 2e 61 6b 2e 61 66 66 69 6e  pIn1, u.ak.affin
24465 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  ity, encoding);.
24466 20 20 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e        applyAffin
24467 69 74 79 28 70 49 6e 33 2c 20 75 2e 61 6b 2e 61  ity(pIn3, u.ak.a
24468 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e  ffinity, encodin
24469 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62  g);.      if( db
2446a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2446b 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
2446c 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28    }..    assert(
2446d 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34   pOp->p4type==P4
2446e 5f 43 4f 4c 4c 53 45 51 20 7c 7c 20 70 4f 70 2d  _COLLSEQ || pOp-
2446f 3e 70 34 2e 70 43 6f 6c 6c 3d 3d 30 20 29 3b 0a  >p4.pColl==0 );.
24470 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70      ExpandBlob(p
24471 49 6e 31 29 3b 0a 20 20 20 20 45 78 70 61 6e 64  In1);.    Expand
24472 42 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 20 20  Blob(pIn3);.    
24473 75 2e 61 6b 2e 72 65 73 20 3d 20 73 71 6c 69 74  u.ak.res = sqlit
24474 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 49 6e  e3MemCompare(pIn
24475 33 2c 20 70 49 6e 31 2c 20 70 4f 70 2d 3e 70 34  3, pIn1, pOp->p4
24476 2e 70 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 73  .pColl);.  }.  s
24477 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f  witch( pOp->opco
24478 64 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f  de ){.    case O
24479 50 5f 45 71 3a 20 20 20 20 75 2e 61 6b 2e 72 65  P_Eq:    u.ak.re
2447a 73 20 3d 20 75 2e 61 6b 2e 72 65 73 3d 3d 30 3b  s = u.ak.res==0;
2447b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2447c 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20 20 20 75  case OP_Ne:    u
2447d 2e 61 6b 2e 72 65 73 20 3d 20 75 2e 61 6b 2e 72  .ak.res = u.ak.r
2447e 65 73 21 3d 30 3b 20 20 20 20 20 62 72 65 61 6b  es!=0;     break
2447f 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4c 74  ;.    case OP_Lt
24480 3a 20 20 20 20 75 2e 61 6b 2e 72 65 73 20 3d 20  :    u.ak.res = 
24481 75 2e 61 6b 2e 72 65 73 3c 30 3b 20 20 20 20 20  u.ak.res<0;     
24482 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
24483 20 4f 50 5f 4c 65 3a 20 20 20 20 75 2e 61 6b 2e   OP_Le:    u.ak.
24484 72 65 73 20 3d 20 75 2e 61 6b 2e 72 65 73 3c 3d  res = u.ak.res<=
24485 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  0;     break;.  
24486 20 20 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20    case OP_Gt:   
24487 20 75 2e 61 6b 2e 72 65 73 20 3d 20 75 2e 61 6b   u.ak.res = u.ak
24488 2e 72 65 73 3e 30 3b 20 20 20 20 20 20 62 72 65  .res>0;      bre
24489 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a  ak;.    default:
2448a 20 20 20 20 20 20 20 75 2e 61 6b 2e 72 65 73 20         u.ak.res 
2448b 3d 20 75 2e 61 6b 2e 72 65 73 3e 3d 30 3b 20 20  = u.ak.res>=0;  
2448c 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20     break;.  }.. 
2448d 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53   if( pOp->p5 & S
2448e 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29 7b  QLITE_STOREP2 ){
2448f 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 61 4d 65  .    pOut = &aMe
24490 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20  m[pOp->p2];.    
24491 6d 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65  memAboutToChange
24492 28 70 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 4d  (p, pOut);.    M
24493 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f  emSetTypeFlag(pO
24494 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20  ut, MEM_Int);.  
24495 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e    pOut->u.i = u.
24496 61 6b 2e 72 65 73 3b 0a 20 20 20 20 52 45 47 49  ak.res;.    REGI
24497 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
24498 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 7d 65 6c  p2, pOut);.  }el
24499 73 65 20 69 66 28 20 75 2e 61 6b 2e 72 65 73 20  se if( u.ak.res 
2449a 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
2449b 3e 70 32 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  >p2-1;.  }..  /*
2449c 20 55 6e 64 6f 20 61 6e 79 20 63 68 61 6e 67 65   Undo any change
2449d 73 20 6d 61 64 65 20 62 79 20 61 70 70 6c 79 41  s made by applyA
2449e 66 66 69 6e 69 74 79 28 29 20 74 6f 20 74 68 65  ffinity() to the
2449f 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 73   input registers
244a0 2e 20 2a 2f 0a 20 20 70 49 6e 31 2d 3e 66 6c 61  . */.  pIn1->fla
244a1 67 73 20 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67  gs = (pIn1->flag
244a2 73 26 7e 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 29  s&~MEM_TypeMask)
244a3 20 7c 20 28 75 2e 61 6b 2e 66 6c 61 67 73 31 26   | (u.ak.flags1&
244a4 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 29 3b 0a 20  MEM_TypeMask);. 
244a5 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 3d 20 28   pIn3->flags = (
244a6 70 49 6e 33 2d 3e 66 6c 61 67 73 26 7e 4d 45 4d  pIn3->flags&~MEM
244a7 5f 54 79 70 65 4d 61 73 6b 29 20 7c 20 28 75 2e  _TypeMask) | (u.
244a8 61 6b 2e 66 6c 61 67 73 33 26 4d 45 4d 5f 54 79  ak.flags3&MEM_Ty
244a9 70 65 4d 61 73 6b 29 3b 0a 20 20 62 72 65 61 6b  peMask);.  break
244aa 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
244ab 50 65 72 6d 75 74 61 74 69 6f 6e 20 2a 20 2a 20  Permutation * * 
244ac 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74  * P4 *.**.** Set
244ad 20 74 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e   the permutation
244ae 20 75 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f   used by the OP_
244af 43 6f 6d 70 61 72 65 20 6f 70 65 72 61 74 6f 72  Compare operator
244b0 20 74 6f 20 62 65 20 74 68 65 20 61 72 72 61 79   to be the array
244b1 0a 2a 2a 20 6f 66 20 69 6e 74 65 67 65 72 73 20  .** of integers 
244b2 69 6e 20 50 34 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  in P4..**.** The
244b3 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 69 73 20   permutation is 
244b4 6f 6e 6c 79 20 76 61 6c 69 64 20 75 6e 74 69 6c  only valid until
244b5 20 74 68 65 20 6e 65 78 74 20 4f 50 5f 50 65 72   the next OP_Per
244b6 6d 75 74 61 74 69 6f 6e 2c 20 4f 50 5f 43 6f 6d  mutation, OP_Com
244b7 70 61 72 65 2c 0a 2a 2a 20 4f 50 5f 48 61 6c 74  pare,.** OP_Halt
244b8 2c 20 6f 72 20 4f 50 5f 52 65 73 75 6c 74 52 6f  , or OP_ResultRo
244b9 77 2e 20 20 54 79 70 69 63 61 6c 6c 79 20 74 68  w.  Typically th
244ba 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e  e OP_Permutation
244bb 20 73 68 6f 75 6c 64 20 6f 63 63 75 72 0a 2a 2a   should occur.**
244bc 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 70 72 69   immediately pri
244bd 6f 72 20 74 6f 20 74 68 65 20 4f 50 5f 43 6f 6d  or to the OP_Com
244be 70 61 72 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  pare..*/.case OP
244bf 5f 50 65 72 6d 75 74 61 74 69 6f 6e 3a 20 7b 0a  _Permutation: {.
244c0 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
244c1 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 41 52 52  4type==P4_INTARR
244c2 41 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  AY );.  assert( 
244c3 70 4f 70 2d 3e 70 34 2e 61 69 20 29 3b 0a 20 20  pOp->p4.ai );.  
244c4 61 50 65 72 6d 75 74 65 20 3d 20 70 4f 70 2d 3e  aPermute = pOp->
244c5 70 34 2e 61 69 3b 0a 20 20 62 72 65 61 6b 3b 0a  p4.ai;.  break;.
244c6 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f  }../* Opcode: Co
244c7 6d 70 61 72 65 20 50 31 20 50 32 20 50 33 20 50  mpare P1 P2 P3 P
244c8 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72  4 *.**.** Compar
244c9 65 20 74 77 6f 20 76 65 63 74 6f 72 73 20 6f 66  e two vectors of
244ca 20 72 65 67 69 73 74 65 72 73 20 69 6e 20 72 65   registers in re
244cb 67 28 50 31 29 2e 2e 72 65 67 28 50 31 2b 50 33  g(P1)..reg(P1+P3
244cc 2d 31 29 20 28 63 61 6c 6c 20 74 68 69 73 0a 2a  -1) (call this.*
244cd 2a 20 76 65 63 74 6f 72 20 22 41 22 29 20 61 6e  * vector "A") an
244ce 64 20 69 6e 20 72 65 67 28 50 32 29 2e 2e 72 65  d in reg(P2)..re
244cf 67 28 50 32 2b 50 33 2d 31 29 20 28 22 42 22 29  g(P2+P3-1) ("B")
244d0 2e 20 20 53 61 76 65 20 74 68 65 20 72 65 73 75  .  Save the resu
244d1 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6d  lt of.** the com
244d2 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 73 65 20  parison for use 
244d3 62 79 20 74 68 65 20 6e 65 78 74 20 4f 50 5f 4a  by the next OP_J
244d4 75 6d 70 20 69 6e 73 74 72 75 63 74 2e 0a 2a 2a  ump instruct..**
244d5 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b 65 79 49  .** P4 is a KeyI
244d6 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68  nfo structure th
244d7 61 74 20 64 65 66 69 6e 65 73 20 63 6f 6c 6c 61  at defines colla
244d8 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61  ting sequences a
244d9 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72 64 65 72  nd sort.** order
244da 73 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61 72  s for the compar
244db 69 73 6f 6e 2e 20 20 54 68 65 20 70 65 72 6d 75  ison.  The permu
244dc 74 61 74 69 6f 6e 20 61 70 70 6c 69 65 73 20 74  tation applies t
244dd 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 6f  o registers.** o
244de 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79 49 6e 66  nly.  The KeyInf
244df 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 75  o elements are u
244e0 73 65 64 20 73 65 71 75 65 6e 74 69 61 6c 6c 79  sed sequentially
244e1 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6d 70  ..**.** The comp
244e2 61 72 69 73 6f 6e 20 69 73 20 61 20 73 6f 72 74  arison is a sort
244e3 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 73 6f 20   comparison, so 
244e4 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65 20 65 71  NULLs compare eq
244e5 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73 20 61 72  ual,.** NULLs ar
244e6 65 20 6c 65 73 73 20 74 68 61 6e 20 6e 75 6d 62  e less than numb
244e7 65 72 73 2c 20 6e 75 6d 62 65 72 73 20 61 72 65  ers, numbers are
244e8 20 6c 65 73 73 20 74 68 61 6e 20 73 74 72 69 6e   less than strin
244e9 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74 72 69 6e  gs,.** and strin
244ea 67 73 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e  gs are less than
244eb 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61 73 65 20   blobs..*/.case 
244ec 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b 0a 23 69  OP_Compare: {.#i
244ed 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
244ee 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
244ef 74 6f 20 75 2e 61 6c 20 2a 2f 0a 20 20 69 6e 74  to u.al */.  int
244f0 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69   n;.  int i;.  i
244f1 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 70 32 3b  nt p1;.  int p2;
244f2 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66 6f  .  const KeyInfo
244f3 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 69 6e   *pKeyInfo;.  in
244f4 74 20 69 64 78 3b 0a 20 20 43 6f 6c 6c 53 65 71  t idx;.  CollSeq
244f5 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43   *pColl;    /* C
244f6 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
244f7 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73  e to use on this
244f8 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 62   term */.  int b
244f9 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Rev;          /*
244fa 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 45 4e   True for DESCEN
244fb 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 72 20  DING sort order 
244fc 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  */.#endif /* loc
244fd 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
244fe 65 64 20 69 6e 74 6f 20 75 2e 61 6c 20 2a 2f 0a  ed into u.al */.
244ff 0a 20 20 75 2e 61 6c 2e 6e 20 3d 20 70 4f 70 2d  .  u.al.n = pOp-
24500 3e 70 33 3b 0a 20 20 75 2e 61 6c 2e 70 4b 65 79  >p3;.  u.al.pKey
24501 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Info = pOp->p4.p
24502 4b 65 79 49 6e 66 6f 3b 0a 20 20 61 73 73 65 72  KeyInfo;.  asser
24503 74 28 20 75 2e 61 6c 2e 6e 3e 30 20 29 3b 0a 20  t( u.al.n>0 );. 
24504 20 61 73 73 65 72 74 28 20 75 2e 61 6c 2e 70 4b   assert( u.al.pK
24505 65 79 49 6e 66 6f 21 3d 30 20 29 3b 0a 20 20 75  eyInfo!=0 );.  u
24506 2e 61 6c 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31  .al.p1 = pOp->p1
24507 3b 0a 20 20 75 2e 61 6c 2e 70 32 20 3d 20 70 4f  ;.  u.al.p2 = pO
24508 70 2d 3e 70 32 3b 0a 23 69 66 20 53 51 4c 49 54  p->p2;.#if SQLIT
24509 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 61 50  E_DEBUG.  if( aP
2450a 65 72 6d 75 74 65 20 29 7b 0a 20 20 20 20 69 6e  ermute ){.    in
2450b 74 20 6b 2c 20 6d 78 20 3d 20 30 3b 0a 20 20 20  t k, mx = 0;.   
2450c 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 75 2e 61 6c   for(k=0; k<u.al
2450d 2e 6e 3b 20 6b 2b 2b 29 20 69 66 28 20 61 50 65  .n; k++) if( aPe
2450e 72 6d 75 74 65 5b 6b 5d 3e 6d 78 20 29 20 6d 78  rmute[k]>mx ) mx
2450f 20 3d 20 61 50 65 72 6d 75 74 65 5b 6b 5d 3b 0a   = aPermute[k];.
24510 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6c      assert( u.al
24511 2e 70 31 3e 30 20 26 26 20 75 2e 61 6c 2e 70 31  .p1>0 && u.al.p1
24512 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29  +mx<=p->nMem+1 )
24513 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  ;.    assert( u.
24514 61 6c 2e 70 32 3e 30 20 26 26 20 75 2e 61 6c 2e  al.p2>0 && u.al.
24515 70 32 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31  p2+mx<=p->nMem+1
24516 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
24517 20 61 73 73 65 72 74 28 20 75 2e 61 6c 2e 70 31   assert( u.al.p1
24518 3e 30 20 26 26 20 75 2e 61 6c 2e 70 31 2b 75 2e  >0 && u.al.p1+u.
24519 61 6c 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20  al.n<=p->nMem+1 
2451a 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  );.    assert( u
2451b 2e 61 6c 2e 70 32 3e 30 20 26 26 20 75 2e 61 6c  .al.p2>0 && u.al
2451c 2e 70 32 2b 75 2e 61 6c 2e 6e 3c 3d 70 2d 3e 6e  .p2+u.al.n<=p->n
2451d 4d 65 6d 2b 31 20 29 3b 0a 20 20 7d 0a 23 65 6e  Mem+1 );.  }.#en
2451e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45  dif /* SQLITE_DE
2451f 42 55 47 20 2a 2f 0a 20 20 66 6f 72 28 75 2e 61  BUG */.  for(u.a
24520 6c 2e 69 3d 30 3b 20 75 2e 61 6c 2e 69 3c 75 2e  l.i=0; u.al.i<u.
24521 61 6c 2e 6e 3b 20 75 2e 61 6c 2e 69 2b 2b 29 7b  al.n; u.al.i++){
24522 0a 20 20 20 20 75 2e 61 6c 2e 69 64 78 20 3d 20  .    u.al.idx = 
24523 61 50 65 72 6d 75 74 65 20 3f 20 61 50 65 72 6d  aPermute ? aPerm
24524 75 74 65 5b 75 2e 61 6c 2e 69 5d 20 3a 20 75 2e  ute[u.al.i] : u.
24525 61 6c 2e 69 3b 0a 20 20 20 20 61 73 73 65 72 74  al.i;.    assert
24526 28 20 6d 65 6d 49 73 56 61 6c 69 64 28 26 61 4d  ( memIsValid(&aM
24527 65 6d 5b 75 2e 61 6c 2e 70 31 2b 75 2e 61 6c 2e  em[u.al.p1+u.al.
24528 69 64 78 5d 29 20 29 3b 0a 20 20 20 20 61 73 73  idx]) );.    ass
24529 65 72 74 28 20 6d 65 6d 49 73 56 61 6c 69 64 28  ert( memIsValid(
2452a 26 61 4d 65 6d 5b 75 2e 61 6c 2e 70 32 2b 75 2e  &aMem[u.al.p2+u.
2452b 61 6c 2e 69 64 78 5d 29 20 29 3b 0a 20 20 20 20  al.idx]) );.    
2452c 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 75  REGISTER_TRACE(u
2452d 2e 61 6c 2e 70 31 2b 75 2e 61 6c 2e 69 64 78 2c  .al.p1+u.al.idx,
2452e 20 26 61 4d 65 6d 5b 75 2e 61 6c 2e 70 31 2b 75   &aMem[u.al.p1+u
2452f 2e 61 6c 2e 69 64 78 5d 29 3b 0a 20 20 20 20 52  .al.idx]);.    R
24530 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e  EGISTER_TRACE(u.
24531 61 6c 2e 70 32 2b 75 2e 61 6c 2e 69 64 78 2c 20  al.p2+u.al.idx, 
24532 26 61 4d 65 6d 5b 75 2e 61 6c 2e 70 32 2b 75 2e  &aMem[u.al.p2+u.
24533 61 6c 2e 69 64 78 5d 29 3b 0a 20 20 20 20 61 73  al.idx]);.    as
24534 73 65 72 74 28 20 75 2e 61 6c 2e 69 3c 75 2e 61  sert( u.al.i<u.a
24535 6c 2e 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  l.pKeyInfo->nFie
24536 6c 64 20 29 3b 0a 20 20 20 20 75 2e 61 6c 2e 70  ld );.    u.al.p
24537 43 6f 6c 6c 20 3d 20 75 2e 61 6c 2e 70 4b 65 79  Coll = u.al.pKey
24538 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 75 2e 61 6c  Info->aColl[u.al
24539 2e 69 5d 3b 0a 20 20 20 20 75 2e 61 6c 2e 62 52  .i];.    u.al.bR
2453a 65 76 20 3d 20 75 2e 61 6c 2e 70 4b 65 79 49 6e  ev = u.al.pKeyIn
2453b 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 75  fo->aSortOrder[u
2453c 2e 61 6c 2e 69 5d 3b 0a 20 20 20 20 69 43 6f 6d  .al.i];.    iCom
2453d 70 61 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65  pare = sqlite3Me
2453e 6d 43 6f 6d 70 61 72 65 28 26 61 4d 65 6d 5b 75  mCompare(&aMem[u
2453f 2e 61 6c 2e 70 31 2b 75 2e 61 6c 2e 69 64 78 5d  .al.p1+u.al.idx]
24540 2c 20 26 61 4d 65 6d 5b 75 2e 61 6c 2e 70 32 2b  , &aMem[u.al.p2+
24541 75 2e 61 6c 2e 69 64 78 5d 2c 20 75 2e 61 6c 2e  u.al.idx], u.al.
24542 70 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20  pColl);.    if( 
24543 69 43 6f 6d 70 61 72 65 20 29 7b 0a 20 20 20 20  iCompare ){.    
24544 20 20 69 66 28 20 75 2e 61 6c 2e 62 52 65 76 20    if( u.al.bRev 
24545 29 20 69 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43  ) iCompare = -iC
24546 6f 6d 70 61 72 65 3b 0a 20 20 20 20 20 20 62 72  ompare;.      br
24547 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
24548 20 61 50 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20   aPermute = 0;. 
24549 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
2454a 63 6f 64 65 3a 20 4a 75 6d 70 20 50 31 20 50 32  code: Jump P1 P2
2454b 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75   P3 * *.**.** Ju
2454c 6d 70 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75  mp to the instru
2454d 63 74 69 6f 6e 20 61 74 20 61 64 64 72 65 73 73  ction at address
2454e 20 50 31 2c 20 50 32 2c 20 6f 72 20 50 33 20 64   P1, P2, or P3 d
2454f 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74  epending on whet
24550 68 65 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f  her.** in the mo
24551 73 74 20 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d  st recent OP_Com
24552 70 61 72 65 20 69 6e 73 74 72 75 63 74 69 6f 6e  pare instruction
24553 20 74 68 65 20 50 31 20 76 65 63 74 6f 72 20 77   the P1 vector w
24554 61 73 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20  as less than.** 
24555 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65  equal to, or gre
24556 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 50 32  ater than the P2
24557 20 76 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74   vector, respect
24558 69 76 65 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ively..*/.case O
24559 50 5f 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20  P_Jump: {       
2455a 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f        /* jump */
2455b 0a 20 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3c  .  if( iCompare<
2455c 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f  0 ){.    pc = pO
2455d 70 2d 3e 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c  p->p1 - 1;.  }el
2455e 73 65 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3d  se if( iCompare=
2455f 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70  =0 ){.    pc = p
24560 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65  Op->p2 - 1;.  }e
24561 6c 73 65 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f  lse{.    pc = pO
24562 70 2d 3e 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20  p->p3 - 1;.  }. 
24563 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
24564 63 6f 64 65 3a 20 41 6e 64 20 50 31 20 50 32 20  code: And P1 P2 
24565 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b  P3 * *.**.** Tak
24566 65 20 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e  e the logical AN
24567 44 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20  D of the values 
24568 69 6e 20 72 65 67 69 73 74 65 72 73 20 50 31 20  in registers P1 
24569 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72  and P2 and.** wr
2456a 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ite the result i
2456b 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e  nto register P3.
2456c 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  .**.** If either
2456d 20 50 31 20 6f 72 20 50 32 20 69 73 20 30 20 28   P1 or P2 is 0 (
2456e 66 61 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20  false) then the 
2456f 72 65 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e  result is 0 even
24570 20 69 66 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72   if.** the other
24571 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20   input is NULL. 
24572 20 41 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65   A NULL and true
24573 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69   or two NULLs gi
24574 76 65 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74  ve.** a NULL out
24575 70 75 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  put..*/./* Opcod
24576 65 3a 20 4f 72 20 50 31 20 50 32 20 50 33 20 2a  e: Or P1 P2 P3 *
24577 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68   *.**.** Take th
24578 65 20 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20  e logical OR of 
24579 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65  the values in re
2457a 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 50 32  gister P1 and P2
2457b 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68   and.** store th
2457c 65 20 61 6e 73 77 65 72 20 69 6e 20 72 65 67 69  e answer in regi
2457d 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49  ster P3..**.** I
2457e 66 20 65 69 74 68 65 72 20 50 31 20 6f 72 20 50  f either P1 or P
2457f 32 20 69 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72  2 is nonzero (tr
24580 75 65 29 20 74 68 65 6e 20 74 68 65 20 72 65 73  ue) then the res
24581 75 6c 74 20 69 73 20 31 20 28 74 72 75 65 29 0a  ult is 1 (true).
24582 2a 2a 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f  ** even if the o
24583 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55  ther input is NU
24584 4c 4c 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20  LL.  A NULL and 
24585 66 61 6c 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c  false or two NUL
24586 4c 73 0a 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c  Ls.** give a NUL
24587 4c 20 6f 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73  L output..*/.cas
24588 65 20 4f 50 5f 41 6e 64 3a 20 20 20 20 20 20 20  e OP_And:       
24589 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
2458a 73 20 54 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69  s TK_AND, in1, i
2458b 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65  n2, out3 */.case
2458c 20 4f 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20   OP_Or: {       
2458d 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
2458e 20 54 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32   TK_OR, in1, in2
2458f 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20  , out3 */.#if 0 
24590 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
24591 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
24592 2e 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 76 31 3b  .am */.  int v1;
24593 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72      /* Left oper
24594 61 6e 64 3a 20 20 30 3d 3d 46 41 4c 53 45 2c 20  and:  0==FALSE, 
24595 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e  1==TRUE, 2==UNKN
24596 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  OWN or NULL */. 
24597 20 69 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52   int v2;    /* R
24598 69 67 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d  ight operand: 0=
24599 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c  =FALSE, 1==TRUE,
2459a 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e   2==UNKNOWN or N
2459b 55 4c 4c 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  ULL */.#endif /*
2459c 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
2459d 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6d   moved into u.am
2459e 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61   */..  pIn1 = &a
2459f 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
245a0 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  if( pIn1->flags 
245a1 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  & MEM_Null ){.  
245a2 20 20 75 2e 61 6d 2e 76 31 20 3d 20 32 3b 0a 20    u.am.v1 = 2;. 
245a3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6d   }else{.    u.am
245a4 2e 76 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  .v1 = sqlite3Vdb
245a5 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21  eIntValue(pIn1)!
245a6 3d 30 3b 0a 20 20 7d 0a 20 20 70 49 6e 32 20 3d  =0;.  }.  pIn2 =
245a7 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b   &aMem[pOp->p2];
245a8 0a 20 20 69 66 28 20 70 49 6e 32 2d 3e 66 6c 61  .  if( pIn2->fla
245a9 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b  gs & MEM_Null ){
245aa 0a 20 20 20 20 75 2e 61 6d 2e 76 32 20 3d 20 32  .    u.am.v2 = 2
245ab 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75  ;.  }else{.    u
245ac 2e 61 6d 2e 76 32 20 3d 20 73 71 6c 69 74 65 33  .am.v2 = sqlite3
245ad 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e  VdbeIntValue(pIn
245ae 32 29 21 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28  2)!=0;.  }.  if(
245af 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
245b0 5f 41 6e 64 20 29 7b 0a 20 20 20 20 73 74 61 74  _And ){.    stat
245b1 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
245b2 64 20 63 68 61 72 20 61 6e 64 5f 6c 6f 67 69 63  d char and_logic
245b3 5b 5d 20 3d 20 7b 20 30 2c 20 30 2c 20 30 2c 20  [] = { 0, 0, 0, 
245b4 30 2c 20 31 2c 20 32 2c 20 30 2c 20 32 2c 20 32  0, 1, 2, 0, 2, 2
245b5 20 7d 3b 0a 20 20 20 20 75 2e 61 6d 2e 76 31 20   };.    u.am.v1 
245b6 3d 20 61 6e 64 5f 6c 6f 67 69 63 5b 75 2e 61 6d  = and_logic[u.am
245b7 2e 76 31 2a 33 2b 75 2e 61 6d 2e 76 32 5d 3b 0a  .v1*3+u.am.v2];.
245b8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 74 61    }else{.    sta
245b9 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
245ba 65 64 20 63 68 61 72 20 6f 72 5f 6c 6f 67 69 63  ed char or_logic
245bb 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20  [] = { 0, 1, 2, 
245bc 31 2c 20 31 2c 20 31 2c 20 32 2c 20 31 2c 20 32  1, 1, 1, 2, 1, 2
245bd 20 7d 3b 0a 20 20 20 20 75 2e 61 6d 2e 76 31 20   };.    u.am.v1 
245be 3d 20 6f 72 5f 6c 6f 67 69 63 5b 75 2e 61 6d 2e  = or_logic[u.am.
245bf 76 31 2a 33 2b 75 2e 61 6d 2e 76 32 5d 3b 0a 20  v1*3+u.am.v2];. 
245c0 20 7d 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65   }.  pOut = &aMe
245c1 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66  m[pOp->p3];.  if
245c2 28 20 75 2e 61 6d 2e 76 31 3d 3d 32 20 29 7b 0a  ( u.am.v1==2 ){.
245c3 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
245c4 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c  ag(pOut, MEM_Nul
245c5 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  l);.  }else{.   
245c6 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61   pOut->u.i = u.a
245c7 6d 2e 76 31 3b 0a 20 20 20 20 4d 65 6d 53 65 74  m.v1;.    MemSet
245c8 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d  TypeFlag(pOut, M
245c9 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 62  EM_Int);.  }.  b
245ca 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
245cb 64 65 3a 20 4e 6f 74 20 50 31 20 50 32 20 2a 20  de: Not P1 P2 * 
245cc 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70  * *.**.** Interp
245cd 72 65 74 20 74 68 65 20 76 61 6c 75 65 20 69 6e  ret the value in
245ce 20 72 65 67 69 73 74 65 72 20 50 31 20 61 73 20   register P1 as 
245cf 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 2e  a boolean value.
245d0 20 20 53 74 6f 72 65 20 74 68 65 0a 2a 2a 20 62    Store the.** b
245d1 6f 6f 6c 65 61 6e 20 63 6f 6d 70 6c 65 6d 65 6e  oolean complemen
245d2 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  t in register P2
245d3 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  .  If the value 
245d4 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69  in register P1 i
245d5 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e  s .** NULL, then
245d6 20 61 20 4e 55 4c 4c 20 69 73 20 73 74 6f 72 65   a NULL is store
245d7 64 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65  d in P2..*/.case
245d8 20 4f 50 5f 4e 6f 74 3a 20 7b 20 20 20 20 20 20   OP_Not: {      
245d9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
245da 65 20 61 73 20 54 4b 5f 4e 4f 54 2c 20 69 6e 31  e as TK_NOT, in1
245db 2c 20 6f 75 74 32 20 2a 2f 0a 20 20 70 49 6e 31  , out2 */.  pIn1
245dc 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31   = &aMem[pOp->p1
245dd 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65  ];.  pOut = &aMe
245de 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66  m[pOp->p2];.  if
245df 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20  ( pIn1->flags & 
245e0 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
245e1 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
245e2 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d  tNull(pOut);.  }
245e3 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
245e4 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34  3VdbeMemSetInt64
245e5 28 70 4f 75 74 2c 20 21 73 71 6c 69 74 65 33 56  (pOut, !sqlite3V
245e6 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31  dbeIntValue(pIn1
245e7 29 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  ));.  }.  break;
245e8 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42  .}../* Opcode: B
245e9 69 74 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20  itNot P1 P2 * * 
245ea 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65  *.**.** Interpre
245eb 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  t the content of
245ec 20 72 65 67 69 73 74 65 72 20 50 31 20 61 73 20   register P1 as 
245ed 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 53 74 6f  an integer.  Sto
245ee 72 65 20 74 68 65 0a 2a 2a 20 6f 6e 65 73 2d 63  re the.** ones-c
245ef 6f 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  omplement of the
245f0 20 50 31 20 76 61 6c 75 65 20 69 6e 74 6f 20 72   P1 value into r
245f1 65 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 20  egister P2.  If 
245f2 50 31 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 4e 55  P1 holds.** a NU
245f3 4c 4c 20 74 68 65 6e 20 73 74 6f 72 65 20 61 20  LL then store a 
245f4 4e 55 4c 4c 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63  NULL in P2..*/.c
245f5 61 73 65 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b  ase OP_BitNot: {
245f6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
245f7 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f  same as TK_BITNO
245f8 54 2c 20 69 6e 31 2c 20 6f 75 74 32 20 2a 2f 0a  T, in1, out2 */.
245f9 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70    pIn1 = &aMem[p
245fa 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20  Op->p1];.  pOut 
245fb 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  = &aMem[pOp->p2]
245fc 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c  ;.  if( pIn1->fl
245fd 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29  ags & MEM_Null )
245fe 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
245ff 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74  eMemSetNull(pOut
24600 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
24601 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
24602 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 7e 73 71  tInt64(pOut, ~sq
24603 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
24604 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d 0a 20 20  e(pIn1));.  }.  
24605 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
24606 6f 64 65 3a 20 4f 6e 63 65 20 50 31 20 50 32 20  ode: Once P1 P2 
24607 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63  * * *.**.** Chec
24608 6b 20 69 66 20 4f 50 5f 4f 6e 63 65 20 66 6c 61  k if OP_Once fla
24609 67 20 50 31 20 69 73 20 73 65 74 2e 20 49 66 20  g P1 is set. If 
2460a 73 6f 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74  so, jump to inst
2460b 72 75 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65  ruction P2. Othe
2460c 72 77 69 73 65 2c 0a 2a 2a 20 73 65 74 20 74 68  rwise,.** set th
2460d 65 20 66 6c 61 67 20 61 6e 64 20 66 61 6c 6c 20  e flag and fall 
2460e 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
2460f 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ext instruction.
24610 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
24611 20 4a 75 6d 70 4f 6e 63 65 0a 2a 2f 0a 63 61 73   JumpOnce.*/.cas
24612 65 20 4f 50 5f 4f 6e 63 65 3a 20 7b 20 20 20 20  e OP_Once: {    
24613 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
24614 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f   */.  assert( pO
24615 70 2d 3e 70 31 3c 70 2d 3e 6e 4f 6e 63 65 46 6c  p->p1<p->nOnceFl
24616 61 67 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61  ag );.  if( p->a
24617 4f 6e 63 65 46 6c 61 67 5b 70 4f 70 2d 3e 70 31  OnceFlag[pOp->p1
24618 5d 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f  ] ){.    pc = pO
24619 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 65 6c 73 65  p->p2-1;.  }else
2461a 7b 0a 20 20 20 20 70 2d 3e 61 4f 6e 63 65 46 6c  {.    p->aOnceFl
2461b 61 67 5b 70 4f 70 2d 3e 70 31 5d 20 3d 20 31 3b  ag[pOp->p1] = 1;
2461c 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
2461d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 20 50  ./* Opcode: If P
2461e 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
2461f 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20  * Jump to P2 if 
24620 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
24621 69 73 74 65 72 20 50 31 20 69 73 20 74 72 75 65  ister P1 is true
24622 2e 20 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20  .  The value.** 
24623 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 72  is considered tr
24624 75 65 20 69 66 20 69 74 20 69 73 20 6e 75 6d 65  ue if it is nume
24625 72 69 63 20 61 6e 64 20 6e 6f 6e 2d 7a 65 72 6f  ric and non-zero
24626 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a  .  If the value.
24627 2a 2a 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c  ** in P1 is NULL
24628 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a   then take the j
24629 75 6d 70 20 69 66 20 50 33 20 69 73 20 6e 6f 6e  ump if P3 is non
2462a 2d 7a 65 72 6f 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63  -zero..*/./* Opc
2462b 6f 64 65 3a 20 49 66 4e 6f 74 20 50 31 20 50 32  ode: IfNot P1 P2
2462c 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75   P3 * *.**.** Ju
2462d 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20  mp to P2 if the 
2462e 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
2462f 72 20 50 31 20 69 73 20 46 61 6c 73 65 2e 20 20  r P1 is False.  
24630 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20  The value.** is 
24631 63 6f 6e 73 69 64 65 72 65 64 20 66 61 6c 73 65  considered false
24632 20 69 66 20 69 74 20 68 61 73 20 61 20 6e 75 6d   if it has a num
24633 65 72 69 63 20 76 61 6c 75 65 20 6f 66 20 7a 65  eric value of ze
24634 72 6f 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75  ro.  If the valu
24635 65 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 4e 55  e.** in P1 is NU
24636 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65  LL then take the
24637 20 6a 75 6d 70 20 69 66 20 50 33 20 69 73 20 7a   jump if P3 is z
24638 65 72 6f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ero..*/.case OP_
24639 49 66 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  If:             
2463a 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31      /* jump, in1
2463b 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 6f   */.case OP_IfNo
2463c 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  t: {            
2463d 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a  /* jump, in1 */.
2463e 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
2463f 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
24640 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0a 20 20 69  into u.an */.  i
24641 6e 74 20 63 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  nt c;.#endif /* 
24642 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
24643 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6e 20  moved into u.an 
24644 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  */.  pIn1 = &aMe
24645 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66  m[pOp->p1];.  if
24646 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20  ( pIn1->flags & 
24647 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
24648 75 2e 61 6e 2e 63 20 3d 20 70 4f 70 2d 3e 70 33  u.an.c = pOp->p3
24649 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 69 66 64 65  ;.  }else{.#ifde
2464a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
2464b 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20  OATING_POINT.   
2464c 20 75 2e 61 6e 2e 63 20 3d 20 73 71 6c 69 74 65   u.an.c = sqlite
2464d 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49  3VdbeIntValue(pI
2464e 6e 31 29 21 3d 30 3b 0a 23 65 6c 73 65 0a 20 20  n1)!=0;.#else.  
2464f 20 20 75 2e 61 6e 2e 63 20 3d 20 73 71 6c 69 74    u.an.c = sqlit
24650 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28  e3VdbeRealValue(
24651 70 49 6e 31 29 21 3d 30 2e 30 3b 0a 23 65 6e 64  pIn1)!=0.0;.#end
24652 69 66 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  if.    if( pOp->
24653 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 66 4e 6f 74  opcode==OP_IfNot
24654 20 29 20 75 2e 61 6e 2e 63 20 3d 20 21 75 2e 61   ) u.an.c = !u.a
24655 6e 2e 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 75  n.c;.  }.  if( u
24656 2e 61 6e 2e 63 20 29 7b 0a 20 20 20 20 70 63 20  .an.c ){.    pc 
24657 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d  = pOp->p2-1;.  }
24658 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
24659 4f 70 63 6f 64 65 3a 20 49 73 4e 75 6c 6c 20 50  Opcode: IsNull P
2465a 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
2465b 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74   Jump to P2 if t
2465c 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
2465d 73 74 65 72 20 50 31 20 69 73 20 4e 55 4c 4c 2e  ster P1 is NULL.
2465e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 73 4e 75  .*/.case OP_IsNu
2465f 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ll: {           
24660 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49   /* same as TK_I
24661 53 4e 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31  SNULL, jump, in1
24662 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d   */.  pIn1 = &aM
24663 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69  em[pOp->p1];.  i
24664 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20  f( (pIn1->flags 
24665 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29  & MEM_Null)!=0 )
24666 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e  {.    pc = pOp->
24667 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72  p2 - 1;.  }.  br
24668 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
24669 65 3a 20 4e 6f 74 4e 75 6c 6c 20 50 31 20 50 32  e: NotNull P1 P2
2466a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d   * * *.**.** Jum
2466b 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76  p to P2 if the v
2466c 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
2466d 20 50 31 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e   P1 is not NULL.
2466e 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f    .*/.case OP_No
2466f 74 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20  tNull: {        
24670 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
24671 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 6a 75 6d 70 2c  K_NOTNULL, jump,
24672 20 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d   in1 */.  pIn1 =
24673 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b   &aMem[pOp->p1];
24674 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c  .  if( (pIn1->fl
24675 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d  ags & MEM_Null)=
24676 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70  =0 ){.    pc = p
24677 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a  Op->p2 - 1;.  }.
24678 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
24679 70 63 6f 64 65 3a 20 43 6f 6c 75 6d 6e 20 50 31  pcode: Column P1
2467a 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a   P2 P3 P4 P5.**.
2467b 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65  ** Interpret the
2467c 20 64 61 74 61 20 74 68 61 74 20 63 75 72 73 6f   data that curso
2467d 72 20 50 31 20 70 6f 69 6e 74 73 20 74 6f 20 61  r P1 points to a
2467e 73 20 61 20 73 74 72 75 63 74 75 72 65 20 62 75  s a structure bu
2467f 69 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65  ilt using.** the
24680 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74   MakeRecord inst
24681 72 75 63 74 69 6f 6e 2e 20 20 28 53 65 65 20 74  ruction.  (See t
24682 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70  he MakeRecord op
24683 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f  code for additio
24684 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  nal.** informati
24685 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 6f 72  on about the for
24686 6d 61 74 20 6f 66 20 74 68 65 20 64 61 74 61 2e  mat of the data.
24687 29 20 20 45 78 74 72 61 63 74 20 74 68 65 20 50  )  Extract the P
24688 32 2d 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 66  2-th column.** f
24689 72 6f 6d 20 74 68 69 73 20 72 65 63 6f 72 64 2e  rom this record.
2468a 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6c    If there are l
2468b 65 73 73 20 74 68 61 74 20 28 50 32 2b 31 29 20  ess that (P2+1) 
2468c 0a 2a 2a 20 76 61 6c 75 65 73 20 69 6e 20 74 68  .** values in th
2468d 65 20 72 65 63 6f 72 64 2c 20 65 78 74 72 61 63  e record, extrac
2468e 74 20 61 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20  t a NULL..**.** 
2468f 54 68 65 20 76 61 6c 75 65 20 65 78 74 72 61 63  The value extrac
24690 74 65 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ted is stored in
24691 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a   register P3..**
24692 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d  .** If the colum
24693 6e 20 63 6f 6e 74 61 69 6e 73 20 66 65 77 65 72  n contains fewer
24694 20 74 68 61 6e 20 50 32 20 66 69 65 6c 64 73 2c   than P2 fields,
24695 20 74 68 65 6e 20 65 78 74 72 61 63 74 20 61 20   then extract a 
24696 4e 55 4c 4c 2e 20 20 4f 72 2c 0a 2a 2a 20 69 66  NULL.  Or,.** if
24697 20 74 68 65 20 50 34 20 61 72 67 75 6d 65 6e 74   the P4 argument
24698 20 69 73 20 61 20 50 34 5f 4d 45 4d 20 75 73 65   is a P4_MEM use
24699 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
2469a 65 20 50 34 20 61 72 67 75 6d 65 6e 74 20 61 73  e P4 argument as
2469b 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2e 0a  .** the result..
2469c 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46  **.** If the OPF
2469d 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45 20 62  LAG_CLEARCACHE b
2469e 69 74 20 69 73 20 73 65 74 20 6f 6e 20 50 35 20  it is set on P5 
2469f 61 6e 64 20 50 31 20 69 73 20 61 20 70 73 65 75  and P1 is a pseu
246a0 64 6f 2d 74 61 62 6c 65 20 63 75 72 73 6f 72 2c  do-table cursor,
246a1 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 61 63  .** then the cac
246a2 68 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72  he of the cursor
246a3 20 69 73 20 72 65 73 65 74 20 70 72 69 6f 72 20   is reset prior 
246a4 74 6f 20 65 78 74 72 61 63 74 69 6e 67 20 74 68  to extracting th
246a5 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 65  e column..** The
246a6 20 66 69 72 73 74 20 4f 50 5f 43 6f 6c 75 6d 6e   first OP_Column
246a7 20 61 67 61 69 6e 73 74 20 61 20 70 73 65 75 64   against a pseud
246a8 6f 2d 74 61 62 6c 65 20 61 66 74 65 72 20 74 68  o-table after th
246a9 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63  e value of the c
246aa 6f 6e 74 65 6e 74 0a 2a 2a 20 72 65 67 69 73 74  ontent.** regist
246ab 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 20 73  er has changed s
246ac 68 6f 75 6c 64 20 68 61 76 65 20 74 68 69 73 20  hould have this 
246ad 62 69 74 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49  bit set..**.** I
246ae 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4c 45 4e  f the OPFLAG_LEN
246af 47 54 48 41 52 47 20 61 6e 64 20 4f 50 46 4c 41  GTHARG and OPFLA
246b0 47 5f 54 59 50 45 4f 46 41 52 47 20 62 69 74 73  G_TYPEOFARG bits
246b1 20 61 72 65 20 73 65 74 20 6f 6e 20 50 35 20 77   are set on P5 w
246b2 68 65 6e 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c  hen.** the resul
246b3 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  t is guaranteed 
246b4 74 6f 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20  to only be used 
246b5 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  as the argument 
246b6 6f 66 20 61 20 6c 65 6e 67 74 68 28 29 0a 2a 2a  of a length().**
246b7 20 6f 72 20 74 79 70 65 6f 66 28 29 20 66 75 6e   or typeof() fun
246b8 63 74 69 6f 6e 2c 20 72 65 73 70 65 63 74 69 76  ction, respectiv
246b9 65 6c 79 2e 20 20 54 68 65 20 6c 6f 61 64 69 6e  ely.  The loadin
246ba 67 20 6f 66 20 6c 61 72 67 65 20 62 6c 6f 62 73  g of large blobs
246bb 20 63 61 6e 20 62 65 0a 2a 2a 20 73 6b 69 70 70   can be.** skipp
246bc 65 64 20 66 6f 72 20 6c 65 6e 67 74 68 28 29 20  ed for length() 
246bd 61 6e 64 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 20  and all content 
246be 6c 6f 61 64 69 6e 67 20 63 61 6e 20 62 65 20 73  loading can be s
246bf 6b 69 70 70 65 64 20 66 6f 72 20 74 79 70 65 6f  kipped for typeo
246c0 66 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  f()..*/.case OP_
246c1 43 6f 6c 75 6d 6e 3a 20 7b 0a 23 69 66 20 30 20  Column: {.#if 0 
246c2 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
246c3 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
246c4 2e 61 6f 20 2a 2f 0a 20 20 75 33 32 20 70 61 79  .ao */.  u32 pay
246c5 6c 6f 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20 4e  loadSize;   /* N
246c6 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
246c7 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a  n the record */.
246c8 20 20 69 36 34 20 70 61 79 6c 6f 61 64 53 69 7a    i64 payloadSiz
246c9 65 36 34 3b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  e64; /* Number o
246ca 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72  f bytes in the r
246cb 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 70  ecord */.  int p
246cc 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  1;            /*
246cd 20 50 31 20 76 61 6c 75 65 20 6f 66 20 74 68 65   P1 value of the
246ce 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74   opcode */.  int
246cf 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20   p2;            
246d0 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* column number
246d1 20 74 6f 20 72 65 74 72 69 65 76 65 20 2a 2f 0a   to retrieve */.
246d2 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
246d3 3b 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45  ;    /* The VDBE
246d4 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 68 61   cursor */.  cha
246d5 72 20 2a 7a 52 65 63 3b 20 20 20 20 20 20 20 20  r *zRec;        
246d6 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 6f  /* Pointer to co
246d7 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 2d 64 61  mplete record-da
246d8 74 61 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72  ta */.  BtCursor
246d9 20 2a 70 43 72 73 72 3b 20 20 20 2f 2a 20 54 68   *pCrsr;   /* Th
246da 65 20 42 54 72 65 65 20 63 75 72 73 6f 72 20 2a  e BTree cursor *
246db 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 65 3b 20  /.  u32 *aType; 
246dc 20 20 20 20 20 20 20 2f 2a 20 61 54 79 70 65 5b         /* aType[
246dd 69 5d 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d  i] holds the num
246de 65 72 69 63 20 74 79 70 65 20 6f 66 20 74 68 65  eric type of the
246df 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   i-th column */.
246e0 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20    u32 *aOffset; 
246e1 20 20 20 20 20 2f 2a 20 61 4f 66 66 73 65 74 5b       /* aOffset[
246e2 69 5d 20 69 73 20 6f 66 66 73 65 74 20 74 6f 20  i] is offset to 
246e3 73 74 61 72 74 20 6f 66 20 64 61 74 61 20 66 6f  start of data fo
246e4 72 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f  r i-th column */
246e5 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20  .  int nField;  
246e6 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20        /* number 
246e7 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65  of fields in the
246e8 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74   record */.  int
246e9 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20   len;           
246ea 2f 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66  /* The length of
246eb 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20   the serialized 
246ec 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 6f 6c  data for the col
246ed 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  umn */.  int i; 
246ee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
246ef 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
246f0 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 20 20   char *zData;   
246f1 20 20 20 20 2f 2a 20 50 61 72 74 20 6f 66 20 74      /* Part of t
246f2 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20  he record being 
246f3 64 65 63 6f 64 65 64 20 2a 2f 0a 20 20 4d 65 6d  decoded */.  Mem
246f4 20 2a 70 44 65 73 74 3b 20 20 20 20 20 20 20 20   *pDest;        
246f5 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 69 74  /* Where to writ
246f6 65 20 74 68 65 20 65 78 74 72 61 63 74 65 64 20  e the extracted 
246f7 76 61 6c 75 65 20 2a 2f 0a 20 20 4d 65 6d 20 73  value */.  Mem s
246f8 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Mem;          /*
246f9 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65   For storing the
246fa 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65   record being de
246fb 63 6f 64 65 64 20 2a 2f 0a 20 20 75 38 20 2a 7a  coded */.  u8 *z
246fc 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Idx;          /*
246fd 20 49 6e 64 65 78 20 69 6e 74 6f 20 68 65 61 64   Index into head
246fe 65 72 20 2a 2f 0a 20 20 75 38 20 2a 7a 45 6e 64  er */.  u8 *zEnd
246ff 48 64 72 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f  Hdr;       /* Po
24700 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20 62  inter to first b
24701 79 74 65 20 61 66 74 65 72 20 74 68 65 20 68 65  yte after the he
24702 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6f 66  ader */.  u32 of
24703 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  fset;        /* 
24704 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20  Offset into the 
24705 64 61 74 61 20 2a 2f 0a 20 20 75 33 32 20 73 7a  data */.  u32 sz
24706 46 69 65 6c 64 3b 20 20 20 20 20 20 20 2f 2a 20  Field;       /* 
24707 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
24708 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  in the content o
24709 66 20 61 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69  f a field */.  i
2470a 6e 74 20 73 7a 48 64 72 3b 20 20 20 20 20 20 20  nt szHdr;       
2470b 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
2470c 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 69 65   header size fie
2470d 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66 20 72  ld at start of r
2470e 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 61  ecord */.  int a
2470f 76 61 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a  vail;         /*
24710 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
24711 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 64 61   of available da
24712 74 61 20 2a 2f 0a 20 20 75 33 32 20 74 3b 20 20  ta */.  u32 t;  
24713 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
24714 74 79 70 65 20 63 6f 64 65 20 66 72 6f 6d 20 74  type code from t
24715 68 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 72  he record header
24716 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 67 3b   */.  Mem *pReg;
24717 20 20 20 20 20 20 20 20 20 2f 2a 20 50 73 65 75           /* Pseu
24718 64 6f 54 61 62 6c 65 20 69 6e 70 75 74 20 72 65  doTable input re
24719 67 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64 69 66  gister */.#endif
2471a 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
2471b 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
2471c 2e 61 6f 20 2a 2f 0a 0a 0a 20 20 75 2e 61 6f 2e  .ao */...  u.ao.
2471d 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  p1 = pOp->p1;.  
2471e 75 2e 61 6f 2e 70 32 20 3d 20 70 4f 70 2d 3e 70  u.ao.p2 = pOp->p
2471f 32 3b 0a 20 20 75 2e 61 6f 2e 70 43 20 3d 20 30  2;.  u.ao.pC = 0
24720 3b 0a 20 20 6d 65 6d 73 65 74 28 26 75 2e 61 6f  ;.  memset(&u.ao
24721 2e 73 4d 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66  .sMem, 0, sizeof
24722 28 75 2e 61 6f 2e 73 4d 65 6d 29 29 3b 0a 20 20  (u.ao.sMem));.  
24723 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 70 31 3c  assert( u.ao.p1<
24724 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
24725 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e  assert( pOp->p3>
24726 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  0 && pOp->p3<=p-
24727 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 61 6f 2e  >nMem );.  u.ao.
24728 70 44 65 73 74 20 3d 20 26 61 4d 65 6d 5b 70 4f  pDest = &aMem[pO
24729 70 2d 3e 70 33 5d 3b 0a 20 20 6d 65 6d 41 62 6f  p->p3];.  memAbo
2472a 75 74 54 6f 43 68 61 6e 67 65 28 70 2c 20 75 2e  utToChange(p, u.
2472b 61 6f 2e 70 44 65 73 74 29 3b 0a 20 20 75 2e 61  ao.pDest);.  u.a
2472c 6f 2e 7a 52 65 63 20 3d 20 30 3b 0a 0a 20 20 2f  o.zRec = 0;..  /
2472d 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 74  * This block set
2472e 73 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 75  s the variable u
2472f 2e 61 6f 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20  .ao.payloadSize 
24730 74 6f 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20  to be the total 
24731 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 62  number of.  ** b
24732 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f  ytes in the reco
24733 72 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 75 2e  rd..  **.  ** u.
24734 61 6f 2e 7a 52 65 63 20 69 73 20 73 65 74 20 74  ao.zRec is set t
24735 6f 20 62 65 20 74 68 65 20 63 6f 6d 70 6c 65 74  o be the complet
24736 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 72 65  e text of the re
24737 63 6f 72 64 20 69 66 20 69 74 20 69 73 20 61 76  cord if it is av
24738 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2a 20 54 68  ailable..  ** Th
24739 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72  e complete recor
2473a 64 20 74 65 78 74 20 69 73 20 61 6c 77 61 79 73  d text is always
2473b 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 70   available for p
2473c 73 65 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 2a  seudo-tables.  *
2473d 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20  * If the record 
2473e 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 63  is stored in a c
2473f 75 72 73 6f 72 2c 20 74 68 65 20 63 6f 6d 70 6c  ursor, the compl
24740 65 74 65 20 72 65 63 6f 72 64 20 74 65 78 74 0a  ete record text.
24741 20 20 2a 2a 20 6d 69 67 68 74 20 62 65 20 61 76    ** might be av
24742 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 20  ailable in the  
24743 75 2e 61 6f 2e 70 43 2d 3e 61 52 6f 77 20 63 61  u.ao.pC->aRow ca
24744 63 68 65 2e 20 20 4f 72 20 69 74 20 6d 69 67 68  che.  Or it migh
24745 74 20 6e 6f 74 20 62 65 2e 0a 20 20 2a 2a 20 49  t not be..  ** I
24746 66 20 74 68 65 20 64 61 74 61 20 69 73 20 75 6e  f the data is un
24747 61 76 61 69 6c 61 62 6c 65 2c 20 20 75 2e 61 6f  available,  u.ao
24748 2e 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f 20  .zRec is set to 
24749 4e 55 4c 4c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  NULL..  **.  ** 
2474a 57 65 20 61 6c 73 6f 20 63 6f 6d 70 75 74 65 20  We also compute 
2474b 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
2474c 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 63  lumns in the rec
2474d 6f 72 64 2e 20 20 46 6f 72 20 63 75 72 73 6f 72  ord.  For cursor
2474e 73 2c 0a 20 20 2a 2a 20 74 68 65 20 6e 75 6d 62  s,.  ** the numb
2474f 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 73  er of columns is
24750 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56   stored in the V
24751 64 62 65 43 75 72 73 6f 72 2e 6e 46 69 65 6c 64  dbeCursor.nField
24752 20 65 6c 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20   element..  */. 
24753 20 75 2e 61 6f 2e 70 43 20 3d 20 70 2d 3e 61 70   u.ao.pC = p->ap
24754 43 73 72 5b 75 2e 61 6f 2e 70 31 5d 3b 0a 20 20  Csr[u.ao.p1];.  
24755 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 70 43 21  assert( u.ao.pC!
24756 3d 30 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  =0 );.#ifndef SQ
24757 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
24758 4c 54 41 42 4c 45 0a 20 20 61 73 73 65 72 74 28  LTABLE.  assert(
24759 20 75 2e 61 6f 2e 70 43 2d 3e 70 56 74 61 62 43   u.ao.pC->pVtabC
2475a 75 72 73 6f 72 3d 3d 30 20 29 3b 0a 23 65 6e 64  ursor==0 );.#end
2475b 69 66 0a 20 20 75 2e 61 6f 2e 70 43 72 73 72 20  if.  u.ao.pCrsr 
2475c 3d 20 75 2e 61 6f 2e 70 43 2d 3e 70 43 75 72 73  = u.ao.pC->pCurs
2475d 6f 72 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 70  or;.  if( u.ao.p
2475e 43 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f  Crsr!=0 ){.    /
2475f 2a 20 54 68 65 20 72 65 63 6f 72 64 20 69 73 20  * The record is 
24760 73 74 6f 72 65 64 20 69 6e 20 61 20 42 2d 54 72  stored in a B-Tr
24761 65 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  ee */.    rc = s
24762 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72  qlite3VdbeCursor
24763 4d 6f 76 65 74 6f 28 75 2e 61 6f 2e 70 43 29 3b  Moveto(u.ao.pC);
24764 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f  .    if( rc ) go
24765 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
24766 65 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 75  error;.    if( u
24767 2e 61 6f 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  .ao.pC->nullRow 
24768 29 7b 0a 20 20 20 20 20 20 75 2e 61 6f 2e 70 61  ){.      u.ao.pa
24769 79 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20  yloadSize = 0;. 
2476a 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61     }else if( u.a
2476b 6f 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75  o.pC->cacheStatu
2476c 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74 72 20 29  s==p->cacheCtr )
2476d 7b 0a 20 20 20 20 20 20 75 2e 61 6f 2e 70 61 79  {.      u.ao.pay
2476e 6c 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6f 2e  loadSize = u.ao.
2476f 70 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 3b  pC->payloadSize;
24770 0a 20 20 20 20 20 20 75 2e 61 6f 2e 7a 52 65 63  .      u.ao.zRec
24771 20 3d 20 28 63 68 61 72 2a 29 75 2e 61 6f 2e 70   = (char*)u.ao.p
24772 43 2d 3e 61 52 6f 77 3b 0a 20 20 20 20 7d 65 6c  C->aRow;.    }el
24773 73 65 20 69 66 28 20 75 2e 61 6f 2e 70 43 2d 3e  se if( u.ao.pC->
24774 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20  isIndex ){.     
24775 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
24776 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c  BtreeCursorIsVal
24777 69 64 28 75 2e 61 6f 2e 70 43 72 73 72 29 20 29  id(u.ao.pCrsr) )
24778 3b 0a 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c 59  ;.      VVA_ONLY
24779 28 72 63 20 3d 29 20 73 71 6c 69 74 65 33 42 74  (rc =) sqlite3Bt
2477a 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 61 6f 2e  reeKeySize(u.ao.
2477b 70 43 72 73 72 2c 20 26 75 2e 61 6f 2e 70 61 79  pCrsr, &u.ao.pay
2477c 6c 6f 61 64 53 69 7a 65 36 34 29 3b 0a 20 20 20  loadSize64);.   
2477d 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
2477e 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a  QLITE_OK );   /*
2477f 20 54 72 75 65 20 62 65 63 61 75 73 65 20 6f 66   True because of
24780 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20   CursorMoveto() 
24781 63 61 6c 6c 20 61 62 6f 76 65 20 2a 2f 0a 20 20  call above */.  
24782 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 42 74      /* sqlite3Bt
24783 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
24784 29 20 75 73 65 73 20 67 65 74 56 61 72 69 6e 74  ) uses getVarint
24785 33 32 28 29 20 74 6f 20 65 78 74 72 61 63 74 20  32() to extract 
24786 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 79  the.      ** pay
24787 6c 6f 61 64 20 73 69 7a 65 2c 20 73 6f 20 69 74  load size, so it
24788 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66   is impossible f
24789 6f 72 20 75 2e 61 6f 2e 70 61 79 6c 6f 61 64 53  or u.ao.payloadS
2478a 69 7a 65 36 34 20 74 6f 20 62 65 0a 20 20 20 20  ize64 to be.    
2478b 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e    ** larger than
2478c 20 33 32 20 62 69 74 73 2e 20 2a 2f 0a 20 20 20   32 bits. */.   
2478d 20 20 20 61 73 73 65 72 74 28 20 28 75 2e 61 6f     assert( (u.ao
2478e 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 20 26  .payloadSize64 &
2478f 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29   SQLITE_MAX_U32)
24790 3d 3d 28 75 36 34 29 75 2e 61 6f 2e 70 61 79 6c  ==(u64)u.ao.payl
24791 6f 61 64 53 69 7a 65 36 34 20 29 3b 0a 20 20 20  oadSize64 );.   
24792 20 20 20 75 2e 61 6f 2e 70 61 79 6c 6f 61 64 53     u.ao.payloadS
24793 69 7a 65 20 3d 20 28 75 33 32 29 75 2e 61 6f 2e  ize = (u32)u.ao.
24794 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20  payloadSize64;. 
24795 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
24796 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
24797 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69  treeCursorIsVali
24798 64 28 75 2e 61 6f 2e 70 43 72 73 72 29 20 29 3b  d(u.ao.pCrsr) );
24799 0a 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c 59 28  .      VVA_ONLY(
2479a 72 63 20 3d 29 20 73 71 6c 69 74 65 33 42 74 72  rc =) sqlite3Btr
2479b 65 65 44 61 74 61 53 69 7a 65 28 75 2e 61 6f 2e  eeDataSize(u.ao.
2479c 70 43 72 73 72 2c 20 26 75 2e 61 6f 2e 70 61 79  pCrsr, &u.ao.pay
2479d 6c 6f 61 64 53 69 7a 65 29 3b 0a 20 20 20 20 20  loadSize);.     
2479e 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
2479f 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a 20 44  ITE_OK );   /* D
247a0 61 74 61 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74  ataSize() cannot
247a1 20 66 61 69 6c 20 2a 2f 0a 20 20 20 20 7d 0a 20   fail */.    }. 
247a2 20 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59   }else if( ALWAY
247a3 53 28 75 2e 61 6f 2e 70 43 2d 3e 70 73 65 75 64  S(u.ao.pC->pseud
247a4 6f 54 61 62 6c 65 52 65 67 3e 30 29 20 29 7b 0a  oTableReg>0) ){.
247a5 20 20 20 20 75 2e 61 6f 2e 70 52 65 67 20 3d 20      u.ao.pReg = 
247a6 26 61 4d 65 6d 5b 75 2e 61 6f 2e 70 43 2d 3e 70  &aMem[u.ao.pC->p
247a7 73 65 75 64 6f 54 61 62 6c 65 52 65 67 5d 3b 0a  seudoTableReg];.
247a8 20 20 20 20 69 66 28 20 75 2e 61 6f 2e 70 43 2d      if( u.ao.pC-
247a9 3e 6d 75 6c 74 69 50 73 65 75 64 6f 20 29 7b 0a  >multiPseudo ){.
247aa 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
247ab 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28  eMemShallowCopy(
247ac 75 2e 61 6f 2e 70 44 65 73 74 2c 20 75 2e 61 6f  u.ao.pDest, u.ao
247ad 2e 70 52 65 67 2b 75 2e 61 6f 2e 70 32 2c 20 4d  .pReg+u.ao.p2, M
247ae 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20  EM_Ephem);.     
247af 20 44 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28   Deephemeralize(
247b0 75 2e 61 6f 2e 70 44 65 73 74 29 3b 0a 20 20 20  u.ao.pDest);.   
247b1 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d     goto op_colum
247b2 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20  n_out;.    }.   
247b3 20 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 70 52   assert( u.ao.pR
247b4 65 67 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  eg->flags & MEM_
247b5 42 6c 6f 62 20 29 3b 0a 20 20 20 20 61 73 73 65  Blob );.    asse
247b6 72 74 28 20 6d 65 6d 49 73 56 61 6c 69 64 28 75  rt( memIsValid(u
247b7 2e 61 6f 2e 70 52 65 67 29 20 29 3b 0a 20 20 20  .ao.pReg) );.   
247b8 20 75 2e 61 6f 2e 70 61 79 6c 6f 61 64 53 69 7a   u.ao.payloadSiz
247b9 65 20 3d 20 75 2e 61 6f 2e 70 52 65 67 2d 3e 6e  e = u.ao.pReg->n
247ba 3b 0a 20 20 20 20 75 2e 61 6f 2e 7a 52 65 63 20  ;.    u.ao.zRec 
247bb 3d 20 75 2e 61 6f 2e 70 52 65 67 2d 3e 7a 3b 0a  = u.ao.pReg->z;.
247bc 20 20 20 20 75 2e 61 6f 2e 70 43 2d 3e 63 61 63      u.ao.pC->cac
247bd 68 65 53 74 61 74 75 73 20 3d 20 28 70 4f 70 2d  heStatus = (pOp-
247be 3e 70 35 26 4f 50 46 4c 41 47 5f 43 4c 45 41 52  >p5&OPFLAG_CLEAR
247bf 43 41 43 48 45 29 20 3f 20 43 41 43 48 45 5f 53  CACHE) ? CACHE_S
247c0 54 41 4c 45 20 3a 20 70 2d 3e 63 61 63 68 65 43  TALE : p->cacheC
247c1 74 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  tr;.    assert( 
247c2 75 2e 61 6f 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.ao.payloadSize
247c3 3d 3d 30 20 7c 7c 20 75 2e 61 6f 2e 7a 52 65 63  ==0 || u.ao.zRec
247c4 21 3d 30 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  !=0 );.  }else{.
247c5 20 20 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 20      /* Consider 
247c6 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 4e 55  the row to be NU
247c7 4c 4c 20 2a 2f 0a 20 20 20 20 75 2e 61 6f 2e 70  LL */.    u.ao.p
247c8 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a  ayloadSize = 0;.
247c9 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 75 2e 61    }..  /* If u.a
247ca 6f 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 69 73  o.payloadSize is
247cb 20 30 2c 20 74 68 65 6e 20 6a 75 73 74 20 73 74   0, then just st
247cc 6f 72 65 20 61 20 4e 55 4c 4c 2e 20 20 54 68 69  ore a NULL.  Thi
247cd 73 20 63 61 6e 20 68 61 70 70 65 6e 20 62 65 63  s can happen bec
247ce 61 75 73 65 20 6f 66 0a 20 20 2a 2a 20 6e 75 6c  ause of.  ** nul
247cf 6c 52 6f 77 20 6f 72 20 62 65 63 61 75 73 65 20  lRow or because 
247d0 6f 66 20 61 20 63 6f 72 72 75 70 74 20 64 61 74  of a corrupt dat
247d1 61 62 61 73 65 2e 20 2a 2f 0a 20 20 69 66 28 20  abase. */.  if( 
247d2 75 2e 61 6f 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.ao.payloadSize
247d3 3d 3d 30 20 29 7b 0a 20 20 20 20 4d 65 6d 53 65  ==0 ){.    MemSe
247d4 74 54 79 70 65 46 6c 61 67 28 75 2e 61 6f 2e 70  tTypeFlag(u.ao.p
247d5 44 65 73 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b  Dest, MEM_Null);
247d6 0a 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c  .    goto op_col
247d7 75 6d 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 61  umn_out;.  }.  a
247d8 73 73 65 72 74 28 20 64 62 2d 3e 61 4c 69 6d 69  ssert( db->aLimi
247d9 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
247da 45 4e 47 54 48 5d 3e 3d 30 20 29 3b 0a 20 20 69  ENGTH]>=0 );.  i
247db 66 28 20 75 2e 61 6f 2e 70 61 79 6c 6f 61 64 53  f( u.ao.payloadS
247dc 69 7a 65 20 3e 20 28 75 33 32 29 64 62 2d 3e 61  ize > (u32)db->a
247dd 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
247de 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20  IT_LENGTH] ){.  
247df 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
247e0 20 20 7d 0a 0a 20 20 75 2e 61 6f 2e 6e 46 69 65    }..  u.ao.nFie
247e1 6c 64 20 3d 20 75 2e 61 6f 2e 70 43 2d 3e 6e 46  ld = u.ao.pC->nF
247e2 69 65 6c 64 3b 0a 20 20 61 73 73 65 72 74 28 20  ield;.  assert( 
247e3 75 2e 61 6f 2e 70 32 3c 75 2e 61 6f 2e 6e 46 69  u.ao.p2<u.ao.nFi
247e4 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61  eld );..  /* Rea
247e5 64 20 61 6e 64 20 70 61 72 73 65 20 74 68 65 20  d and parse the 
247e6 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20 20 53  table header.  S
247e7 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  tore the results
247e8 20 6f 66 20 74 68 65 20 70 61 72 73 65 0a 20 20   of the parse.  
247e9 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f  ** into the reco
247ea 72 64 20 68 65 61 64 65 72 20 63 61 63 68 65 20  rd header cache 
247eb 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75  fields of the cu
247ec 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61  rsor..  */.  u.a
247ed 6f 2e 61 54 79 70 65 20 3d 20 75 2e 61 6f 2e 70  o.aType = u.ao.p
247ee 43 2d 3e 61 54 79 70 65 3b 0a 20 20 69 66 28 20  C->aType;.  if( 
247ef 75 2e 61 6f 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.ao.pC->cacheSt
247f0 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74  atus==p->cacheCt
247f1 72 20 29 7b 0a 20 20 20 20 75 2e 61 6f 2e 61 4f  r ){.    u.ao.aO
247f2 66 66 73 65 74 20 3d 20 75 2e 61 6f 2e 70 43 2d  ffset = u.ao.pC-
247f3 3e 61 4f 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73  >aOffset;.  }els
247f4 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 75 2e  e{.    assert(u.
247f5 61 6f 2e 61 54 79 70 65 29 3b 0a 20 20 20 20 75  ao.aType);.    u
247f6 2e 61 6f 2e 61 76 61 69 6c 20 3d 20 30 3b 0a 20  .ao.avail = 0;. 
247f7 20 20 20 75 2e 61 6f 2e 70 43 2d 3e 61 4f 66 66     u.ao.pC->aOff
247f8 73 65 74 20 3d 20 75 2e 61 6f 2e 61 4f 66 66 73  set = u.ao.aOffs
247f9 65 74 20 3d 20 26 75 2e 61 6f 2e 61 54 79 70 65  et = &u.ao.aType
247fa 5b 75 2e 61 6f 2e 6e 46 69 65 6c 64 5d 3b 0a 20  [u.ao.nField];. 
247fb 20 20 20 75 2e 61 6f 2e 70 43 2d 3e 70 61 79 6c     u.ao.pC->payl
247fc 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6f 2e 70  oadSize = u.ao.p
247fd 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20  ayloadSize;.    
247fe 75 2e 61 6f 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.ao.pC->cacheSt
247ff 61 74 75 73 20 3d 20 70 2d 3e 63 61 63 68 65 43  atus = p->cacheC
24800 74 72 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75  tr;..    /* Figu
24801 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
24802 62 79 74 65 73 20 61 72 65 20 69 6e 20 74 68 65  bytes are in the
24803 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 69   header */.    i
24804 66 28 20 75 2e 61 6f 2e 7a 52 65 63 20 29 7b 0a  f( u.ao.zRec ){.
24805 20 20 20 20 20 20 75 2e 61 6f 2e 7a 44 61 74 61        u.ao.zData
24806 20 3d 20 75 2e 61 6f 2e 7a 52 65 63 3b 0a 20 20   = u.ao.zRec;.  
24807 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
24808 66 28 20 75 2e 61 6f 2e 70 43 2d 3e 69 73 49 6e  f( u.ao.pC->isIn
24809 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 75  dex ){.        u
2480a 2e 61 6f 2e 7a 44 61 74 61 20 3d 20 28 63 68 61  .ao.zData = (cha
2480b 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b  r*)sqlite3BtreeK
2480c 65 79 46 65 74 63 68 28 75 2e 61 6f 2e 70 43 72  eyFetch(u.ao.pCr
2480d 73 72 2c 20 26 75 2e 61 6f 2e 61 76 61 69 6c 29  sr, &u.ao.avail)
2480e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2480f 20 20 20 20 20 20 20 75 2e 61 6f 2e 7a 44 61 74         u.ao.zDat
24810 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  a = (char*)sqlit
24811 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68  e3BtreeDataFetch
24812 28 75 2e 61 6f 2e 70 43 72 73 72 2c 20 26 75 2e  (u.ao.pCrsr, &u.
24813 61 6f 2e 61 76 61 69 6c 29 3b 0a 20 20 20 20 20  ao.avail);.     
24814 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 4b   }.      /* If K
24815 65 79 46 65 74 63 68 28 29 2f 44 61 74 61 46 65  eyFetch()/DataFe
24816 74 63 68 28 29 20 6d 61 6e 61 67 65 64 20 74 6f  tch() managed to
24817 20 67 65 74 20 74 68 65 20 65 6e 74 69 72 65 20   get the entire 
24818 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20 20 20 2a  payload,.      *
24819 2a 20 73 61 76 65 20 74 68 65 20 70 61 79 6c 6f  * save the paylo
2481a 61 64 20 69 6e 20 74 68 65 20 75 2e 61 6f 2e 70  ad in the u.ao.p
2481b 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20  C->aRow cache.  
2481c 54 68 61 74 20 77 69 6c 6c 20 73 61 76 65 20 75  That will save u
2481d 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20  s from.      ** 
2481e 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61  having to make a
2481f 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20  dditional calls 
24820 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e  to fetch the con
24821 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a  tent portion of.
24822 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63        ** the rec
24823 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ord..      */.  
24824 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6f      assert( u.ao
24825 2e 61 76 61 69 6c 3e 3d 30 20 29 3b 0a 20 20 20  .avail>=0 );.   
24826 20 20 20 69 66 28 20 75 2e 61 6f 2e 70 61 79 6c     if( u.ao.payl
24827 6f 61 64 53 69 7a 65 20 3c 3d 20 28 75 33 32 29  oadSize <= (u32)
24828 75 2e 61 6f 2e 61 76 61 69 6c 20 29 7b 0a 20 20  u.ao.avail ){.  
24829 20 20 20 20 20 20 75 2e 61 6f 2e 7a 52 65 63 20        u.ao.zRec 
2482a 3d 20 75 2e 61 6f 2e 7a 44 61 74 61 3b 0a 20 20  = u.ao.zData;.  
2482b 20 20 20 20 20 20 75 2e 61 6f 2e 70 43 2d 3e 61        u.ao.pC->a
2482c 52 6f 77 20 3d 20 28 75 38 2a 29 75 2e 61 6f 2e  Row = (u8*)u.ao.
2482d 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c  zData;.      }el
2482e 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6f  se{.        u.ao
2482f 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 30 3b 0a 20  .pC->aRow = 0;. 
24830 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
24831 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
24832 67 20 61 73 73 65 72 74 20 69 73 20 74 72 75 65  g assert is true
24833 20 69 6e 20 61 6c 6c 20 63 61 73 65 73 20 65 78   in all cases ex
24834 63 65 70 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a  cept when.    **
24835 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
24836 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72  le has been corr
24837 75 70 74 65 64 20 65 78 74 65 72 6e 61 6c 6c 79  upted externally
24838 2e 0a 20 20 20 20 2a 2a 20 20 20 20 61 73 73 65  ..    **    asse
24839 72 74 28 20 75 2e 61 6f 2e 7a 52 65 63 21 3d 30  rt( u.ao.zRec!=0
2483a 20 7c 7c 20 75 2e 61 6f 2e 61 76 61 69 6c 3e 3d   || u.ao.avail>=
2483b 75 2e 61 6f 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.ao.payloadSize
2483c 20 7c 7c 20 75 2e 61 6f 2e 61 76 61 69 6c 3e 3d   || u.ao.avail>=
2483d 39 20 29 3b 20 2a 2f 0a 20 20 20 20 75 2e 61 6f  9 ); */.    u.ao
2483e 2e 73 7a 48 64 72 20 3d 20 67 65 74 56 61 72 69  .szHdr = getVari
2483f 6e 74 33 32 28 28 75 38 2a 29 75 2e 61 6f 2e 7a  nt32((u8*)u.ao.z
24840 44 61 74 61 2c 20 75 2e 61 6f 2e 6f 66 66 73 65  Data, u.ao.offse
24841 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65  t);..    /* Make
24842 20 73 75 72 65 20 61 20 63 6f 72 72 75 70 74 20   sure a corrupt 
24843 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74  database has not
24844 20 67 69 76 65 6e 20 75 73 20 61 6e 20 6f 76 65   given us an ove
24845 72 73 69 7a 65 20 68 65 61 64 65 72 2e 0a 20 20  rsize header..  
24846 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 6e 6f 77    ** Do this now
24847 20 74 6f 20 61 76 6f 69 64 20 61 6e 20 6f 76 65   to avoid an ove
24848 72 73 69 7a 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  rsize memory all
24849 6f 63 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a  ocation..    **.
2484a 20 20 20 20 2a 2a 20 54 79 70 65 20 65 6e 74 72      ** Type entr
2484b 69 65 73 20 63 61 6e 20 62 65 20 62 65 74 77 65  ies can be betwe
2484c 65 6e 20 31 20 61 6e 64 20 35 20 62 79 74 65 73  en 1 and 5 bytes
2484d 20 65 61 63 68 2e 20 20 42 75 74 20 34 20 61 6e   each.  But 4 an
2484e 64 20 35 20 62 79 74 65 0a 20 20 20 20 2a 2a 20  d 5 byte.    ** 
2484f 74 79 70 65 73 20 75 73 65 20 73 6f 20 6d 75 63  types use so muc
24850 68 20 64 61 74 61 20 73 70 61 63 65 20 74 68 61  h data space tha
24851 74 20 74 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79  t there can only
24852 20 62 65 20 34 30 39 36 20 61 6e 64 20 33 32 20   be 4096 and 32 
24853 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 6d 2c 20  of.    ** them, 
24854 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 53  respectively.  S
24855 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65  o the maximum he
24856 61 64 65 72 20 6c 65 6e 67 74 68 20 72 65 73 75  ader length resu
24857 6c 74 73 20 66 72 6f 6d 20 61 0a 20 20 20 20 2a  lts from a.    *
24858 2a 20 33 2d 62 79 74 65 20 74 79 70 65 20 66 6f  * 3-byte type fo
24859 72 20 65 61 63 68 20 6f 66 20 74 68 65 20 6d 61  r each of the ma
2485a 78 69 6d 75 6d 20 6f 66 20 33 32 37 36 38 20 63  ximum of 32768 c
2485b 6f 6c 75 6d 6e 73 20 70 6c 75 73 20 74 68 72 65  olumns plus thre
2485c 65 0a 20 20 20 20 2a 2a 20 65 78 74 72 61 20 62  e.    ** extra b
2485d 79 74 65 73 20 66 6f 72 20 74 68 65 20 68 65 61  ytes for the hea
2485e 64 65 72 20 6c 65 6e 67 74 68 20 69 74 73 65 6c  der length itsel
2485f 66 2e 20 20 33 32 37 36 38 2a 33 20 2b 20 33 20  f.  32768*3 + 3 
24860 3d 20 39 38 33 30 37 2e 0a 20 20 20 20 2a 2f 0a  = 98307..    */.
24861 20 20 20 20 69 66 28 20 75 2e 61 6f 2e 6f 66 66      if( u.ao.off
24862 73 65 74 20 3e 20 39 38 33 30 37 20 29 7b 0a 20  set > 98307 ){. 
24863 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
24864 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
24865 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c       goto op_col
24866 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a  umn_out;.    }..
24867 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 69      /* Compute i
24868 6e 20 75 2e 61 6f 2e 6c 65 6e 20 74 68 65 20 6e  n u.ao.len the n
24869 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
2486a 66 20 64 61 74 61 20 77 65 20 6e 65 65 64 20 74  f data we need t
2486b 6f 20 72 65 61 64 20 69 6e 20 6f 72 64 65 72 0a  o read in order.
2486c 20 20 20 20 2a 2a 20 74 6f 20 67 65 74 20 75 2e      ** to get u.
2486d 61 6f 2e 6e 46 69 65 6c 64 20 74 79 70 65 20 76  ao.nField type v
2486e 61 6c 75 65 73 2e 20 20 75 2e 61 6f 2e 6f 66 66  alues.  u.ao.off
2486f 73 65 74 20 69 73 20 61 6e 20 75 70 70 65 72 20  set is an upper 
24870 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 2e 20 20  bound on this.  
24871 42 75 74 0a 20 20 20 20 2a 2a 20 75 2e 61 6f 2e  But.    ** u.ao.
24872 6e 46 69 65 6c 64 20 6d 69 67 68 74 20 62 65 20  nField might be 
24873 73 69 67 6e 69 66 69 63 61 6e 74 6c 79 20 6c 65  significantly le
24874 73 73 20 74 68 61 6e 20 74 68 65 20 74 72 75 65  ss than the true
24875 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
24876 6e 73 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65  ns.    ** in the
24877 20 74 61 62 6c 65 2c 20 61 6e 64 20 69 6e 20 74   table, and in t
24878 68 61 74 20 63 61 73 65 2c 20 35 2a 75 2e 61 6f  hat case, 5*u.ao
24879 2e 6e 46 69 65 6c 64 2b 33 20 6d 69 67 68 74 20  .nField+3 might 
2487a 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  be smaller than 
2487b 75 2e 61 6f 2e 6f 66 66 73 65 74 2e 0a 20 20 20  u.ao.offset..   
2487c 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 6d   ** We want to m
2487d 69 6e 69 6d 69 7a 65 20 75 2e 61 6f 2e 6c 65 6e  inimize u.ao.len
2487e 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6c 69 6d   in order to lim
2487f 69 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  it the size of t
24880 68 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a  he memory.    **
24881 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 65 73 70   allocation, esp
24882 65 63 69 61 6c 6c 79 20 69 66 20 61 20 63 6f 72  ecially if a cor
24883 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69  rupt database fi
24884 6c 65 20 68 61 73 20 63 61 75 73 65 64 20 75 2e  le has caused u.
24885 61 6f 2e 6f 66 66 73 65 74 0a 20 20 20 20 2a 2a  ao.offset.    **
24886 20 74 6f 20 62 65 20 6f 76 65 72 73 69 7a 65 64   to be oversized
24887 2e 20 4f 66 66 73 65 74 20 69 73 20 6c 69 6d 69  . Offset is limi
24888 74 65 64 20 74 6f 20 39 38 33 30 37 20 61 62 6f  ted to 98307 abo
24889 76 65 2e 20 20 42 75 74 20 39 38 33 30 37 20 6d  ve.  But 98307 m
2488a 69 67 68 74 0a 20 20 20 20 2a 2a 20 73 74 69 6c  ight.    ** stil
2488b 6c 20 65 78 63 65 65 64 20 52 6f 62 73 6f 6e 20  l exceed Robson 
2488c 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
2488d 6e 20 6c 69 6d 69 74 73 20 6f 6e 20 73 6f 6d 65  n limits on some
2488e 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 73 2e   configurations.
2488f 0a 20 20 20 20 2a 2a 20 4f 6e 20 73 79 73 74 65  .    ** On syste
24890 6d 73 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 74  ms that cannot t
24891 6f 6c 65 72 61 74 65 20 6c 61 72 67 65 20 6d 65  olerate large me
24892 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  mory allocations
24893 2c 20 75 2e 61 6f 2e 6e 46 69 65 6c 64 2a 35 2b  , u.ao.nField*5+
24894 33 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 6c 69  3.    ** will li
24895 6b 65 6c 79 20 62 65 20 6d 75 63 68 20 73 6d 61  kely be much sma
24896 6c 6c 65 72 20 73 69 6e 63 65 20 75 2e 61 6f 2e  ller since u.ao.
24897 6e 46 69 65 6c 64 20 77 69 6c 6c 20 6c 69 6b 65  nField will like
24898 6c 79 20 62 65 20 6c 65 73 73 20 74 68 61 6e 0a  ly be less than.
24899 20 20 20 20 2a 2a 20 32 30 20 6f 72 20 73 6f 2e      ** 20 or so.
2489a 20 20 54 68 69 73 20 69 6e 73 75 72 65 73 20 74    This insures t
2489b 68 61 74 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72  hat Robson memor
2489c 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d  y allocation lim
2489d 69 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e  its are.    ** n
2489e 6f 74 20 65 78 63 65 65 64 65 64 20 65 76 65 6e  ot exceeded even
2489f 20 66 6f 72 20 63 6f 72 72 75 70 74 20 64 61 74   for corrupt dat
248a0 61 62 61 73 65 20 66 69 6c 65 73 2e 0a 20 20 20  abase files..   
248a1 20 2a 2f 0a 20 20 20 20 75 2e 61 6f 2e 6c 65 6e   */.    u.ao.len
248a2 20 3d 20 75 2e 61 6f 2e 6e 46 69 65 6c 64 2a 35   = u.ao.nField*5
248a3 20 2b 20 33 3b 0a 20 20 20 20 69 66 28 20 75 2e   + 3;.    if( u.
248a4 61 6f 2e 6c 65 6e 20 3e 20 28 69 6e 74 29 75 2e  ao.len > (int)u.
248a5 61 6f 2e 6f 66 66 73 65 74 20 29 20 75 2e 61 6f  ao.offset ) u.ao
248a6 2e 6c 65 6e 20 3d 20 28 69 6e 74 29 75 2e 61 6f  .len = (int)u.ao
248a7 2e 6f 66 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a  .offset;..    /*
248a8 20 54 68 65 20 4b 65 79 46 65 74 63 68 28 29 20   The KeyFetch() 
248a9 6f 72 20 44 61 74 61 46 65 74 63 68 28 29 20 61  or DataFetch() a
248aa 62 6f 76 65 20 61 72 65 20 66 61 73 74 20 61 6e  bove are fast an
248ab 64 20 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65  d will get the e
248ac 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63  ntire.    ** rec
248ad 6f 72 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f  ord header in mo
248ae 73 74 20 63 61 73 65 73 2e 20 20 42 75 74 20 74  st cases.  But t
248af 68 65 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f  hey will fail to
248b0 20 67 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74   get the complet
248b1 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20  e.    ** record 
248b2 68 65 61 64 65 72 20 69 66 20 74 68 65 20 72 65  header if the re
248b3 63 6f 72 64 20 68 65 61 64 65 72 20 64 6f 65 73  cord header does
248b4 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69   not fit on a si
248b5 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a  ngle page.    **
248b6 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20   in the B-Tree. 
248b7 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65   When that happe
248b8 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56  ns, use sqlite3V
248b9 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
248ba 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75  ) to.    ** acqu
248bb 69 72 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65  ire the complete
248bc 20 68 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20   header text..  
248bd 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 75 2e    */.    if( !u.
248be 61 6f 2e 7a 52 65 63 20 26 26 20 75 2e 61 6f 2e  ao.zRec && u.ao.
248bf 61 76 61 69 6c 3c 75 2e 61 6f 2e 6c 65 6e 20 29  avail<u.ao.len )
248c0 7b 0a 20 20 20 20 20 20 75 2e 61 6f 2e 73 4d 65  {.      u.ao.sMe
248c1 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20  m.flags = 0;.   
248c2 20 20 20 75 2e 61 6f 2e 73 4d 65 6d 2e 64 62 20     u.ao.sMem.db 
248c3 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
248c4 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72  sqlite3VdbeMemFr
248c5 6f 6d 42 74 72 65 65 28 75 2e 61 6f 2e 70 43 72  omBtree(u.ao.pCr
248c6 73 72 2c 20 30 2c 20 75 2e 61 6f 2e 6c 65 6e 2c  sr, 0, u.ao.len,
248c7 20 75 2e 61 6f 2e 70 43 2d 3e 69 73 49 6e 64 65   u.ao.pC->isInde
248c8 78 2c 20 26 75 2e 61 6f 2e 73 4d 65 6d 29 3b 0a  x, &u.ao.sMem);.
248c9 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
248ca 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
248cb 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d     goto op_colum
248cc 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20  n_out;.      }. 
248cd 20 20 20 20 20 75 2e 61 6f 2e 7a 44 61 74 61 20       u.ao.zData 
248ce 3d 20 75 2e 61 6f 2e 73 4d 65 6d 2e 7a 3b 0a 20  = u.ao.sMem.z;. 
248cf 20 20 20 7d 0a 20 20 20 20 75 2e 61 6f 2e 7a 45     }.    u.ao.zE
248d0 6e 64 48 64 72 20 3d 20 28 75 38 20 2a 29 26 75  ndHdr = (u8 *)&u
248d1 2e 61 6f 2e 7a 44 61 74 61 5b 75 2e 61 6f 2e 6c  .ao.zData[u.ao.l
248d2 65 6e 5d 3b 0a 20 20 20 20 75 2e 61 6f 2e 7a 49  en];.    u.ao.zI
248d3 64 78 20 3d 20 28 75 38 20 2a 29 26 75 2e 61 6f  dx = (u8 *)&u.ao
248d4 2e 7a 44 61 74 61 5b 75 2e 61 6f 2e 73 7a 48 64  .zData[u.ao.szHd
248d5 72 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e  r];..    /* Scan
248d6 20 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 20   the header and 
248d7 75 73 65 20 69 74 20 74 6f 20 66 69 6c 6c 20 69  use it to fill i
248d8 6e 20 74 68 65 20 75 2e 61 6f 2e 61 54 79 70 65  n the u.ao.aType
248d9 5b 5d 20 61 6e 64 20 75 2e 61 6f 2e 61 4f 66 66  [] and u.ao.aOff
248da 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72  set[].    ** arr
248db 61 79 73 2e 20 20 75 2e 61 6f 2e 61 54 79 70 65  ays.  u.ao.aType
248dc 5b 75 2e 61 6f 2e 69 5d 20 77 69 6c 6c 20 63 6f  [u.ao.i] will co
248dd 6e 74 61 69 6e 20 74 68 65 20 74 79 70 65 20 69  ntain the type i
248de 6e 74 65 67 65 72 20 66 6f 72 20 74 68 65 20 75  nteger for the u
248df 2e 61 6f 2e 69 2d 74 68 0a 20 20 20 20 2a 2a 20  .ao.i-th.    ** 
248e0 63 6f 6c 75 6d 6e 20 61 6e 64 20 75 2e 61 6f 2e  column and u.ao.
248e1 61 4f 66 66 73 65 74 5b 75 2e 61 6f 2e 69 5d 20  aOffset[u.ao.i] 
248e2 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65  will contain the
248e3 20 75 2e 61 6f 2e 6f 66 66 73 65 74 20 66 72 6f   u.ao.offset fro
248e4 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a  m the beginning.
248e5 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65      ** of the re
248e6 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74 61 72  cord to the star
248e7 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f  t of the data fo
248e8 72 20 74 68 65 20 75 2e 61 6f 2e 69 2d 74 68 20  r the u.ao.i-th 
248e9 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20  column.    */.  
248ea 20 20 66 6f 72 28 75 2e 61 6f 2e 69 3d 30 3b 20    for(u.ao.i=0; 
248eb 75 2e 61 6f 2e 69 3c 75 2e 61 6f 2e 6e 46 69 65  u.ao.i<u.ao.nFie
248ec 6c 64 3b 20 75 2e 61 6f 2e 69 2b 2b 29 7b 0a 20  ld; u.ao.i++){. 
248ed 20 20 20 20 20 69 66 28 20 75 2e 61 6f 2e 7a 49       if( u.ao.zI
248ee 64 78 3c 75 2e 61 6f 2e 7a 45 6e 64 48 64 72 20  dx<u.ao.zEndHdr 
248ef 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6f 2e  ){.        u.ao.
248f0 61 4f 66 66 73 65 74 5b 75 2e 61 6f 2e 69 5d 20  aOffset[u.ao.i] 
248f1 3d 20 75 2e 61 6f 2e 6f 66 66 73 65 74 3b 0a 20  = u.ao.offset;. 
248f2 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 6f 2e         if( u.ao.
248f3 7a 49 64 78 5b 30 5d 3c 30 78 38 30 20 29 7b 0a  zIdx[0]<0x80 ){.
248f4 20 20 20 20 20 20 20 20 20 20 75 2e 61 6f 2e 74            u.ao.t
248f5 20 3d 20 75 2e 61 6f 2e 7a 49 64 78 5b 30 5d 3b   = u.ao.zIdx[0];
248f6 0a 20 20 20 20 20 20 20 20 20 20 75 2e 61 6f 2e  .          u.ao.
248f7 7a 49 64 78 2b 2b 3b 0a 20 20 20 20 20 20 20 20  zIdx++;.        
248f8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
248f9 20 75 2e 61 6f 2e 7a 49 64 78 20 2b 3d 20 73 71   u.ao.zIdx += sq
248fa 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32  lite3GetVarint32
248fb 28 75 2e 61 6f 2e 7a 49 64 78 2c 20 26 75 2e 61  (u.ao.zIdx, &u.a
248fc 6f 2e 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  o.t);.        }.
248fd 20 20 20 20 20 20 20 20 75 2e 61 6f 2e 61 54 79          u.ao.aTy
248fe 70 65 5b 75 2e 61 6f 2e 69 5d 20 3d 20 75 2e 61  pe[u.ao.i] = u.a
248ff 6f 2e 74 3b 0a 20 20 20 20 20 20 20 20 75 2e 61  o.t;.        u.a
24900 6f 2e 73 7a 46 69 65 6c 64 20 3d 20 73 71 6c 69  o.szField = sqli
24901 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
24902 65 4c 65 6e 28 75 2e 61 6f 2e 74 29 3b 0a 20 20  eLen(u.ao.t);.  
24903 20 20 20 20 20 20 75 2e 61 6f 2e 6f 66 66 73 65        u.ao.offse
24904 74 20 2b 3d 20 75 2e 61 6f 2e 73 7a 46 69 65 6c  t += u.ao.szFiel
24905 64 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75  d;.        if( u
24906 2e 61 6f 2e 6f 66 66 73 65 74 3c 75 2e 61 6f 2e  .ao.offset<u.ao.
24907 73 7a 46 69 65 6c 64 20 29 7b 20 20 2f 2a 20 54  szField ){  /* T
24908 72 75 65 20 69 66 20 75 2e 61 6f 2e 6f 66 66 73  rue if u.ao.offs
24909 65 74 20 6f 76 65 72 66 6c 6f 77 73 20 2a 2f 0a  et overflows */.
2490a 20 20 20 20 20 20 20 20 20 20 75 2e 61 6f 2e 7a            u.ao.z
2490b 49 64 78 20 3d 20 26 75 2e 61 6f 2e 7a 45 6e 64  Idx = &u.ao.zEnd
2490c 48 64 72 5b 31 5d 3b 20 20 2f 2a 20 46 6f 72 63  Hdr[1];  /* Forc
2490d 65 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  es SQLITE_CORRUP
2490e 54 20 72 65 74 75 72 6e 20 62 65 6c 6f 77 20 2a  T return below *
2490f 2f 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  /.          brea
24910 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
24911 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
24912 20 20 2f 2a 20 49 66 20 75 2e 61 6f 2e 69 20 69    /* If u.ao.i i
24913 73 20 6c 65 73 73 20 74 68 61 74 20 75 2e 61 6f  s less that u.ao
24914 2e 6e 46 69 65 6c 64 2c 20 74 68 65 6e 20 74 68  .nField, then th
24915 65 72 65 20 61 72 65 20 66 65 77 65 72 20 66 69  ere are fewer fi
24916 65 6c 64 73 20 69 6e 20 74 68 69 73 0a 20 20 20  elds in this.   
24917 20 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 74       ** record t
24918 68 61 6e 20 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e  han SetNumColumn
24919 73 20 69 6e 64 69 63 61 74 65 64 20 74 68 65 72  s indicated ther
2491a 65 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69 6e  e are columns in
2491b 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
2491c 74 61 62 6c 65 2e 20 53 65 74 20 74 68 65 20 75  table. Set the u
2491d 2e 61 6f 2e 6f 66 66 73 65 74 20 66 6f 72 20 61  .ao.offset for a
2491e 6e 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 73  ny extra columns
2491f 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 0a   not present in.
24920 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72          ** the r
24921 65 63 6f 72 64 20 74 6f 20 30 2e 20 54 68 69 73  ecord to 0. This
24922 20 74 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c 6f   tells code belo
24923 77 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 64  w to store the d
24924 65 66 61 75 6c 74 20 76 61 6c 75 65 0a 20 20 20  efault value.   
24925 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20       ** for the 
24926 63 6f 6c 75 6d 6e 20 69 6e 73 74 65 61 64 20 6f  column instead o
24927 66 20 64 65 73 65 72 69 61 6c 69 7a 69 6e 67 20  f deserializing 
24928 61 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65  a value from the
24929 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 20   record..       
2492a 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e 61 6f   */.        u.ao
2492b 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6f 2e 69 5d  .aOffset[u.ao.i]
2492c 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
2492d 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
2492e 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75  dbeMemRelease(&u
2492f 2e 61 6f 2e 73 4d 65 6d 29 3b 0a 20 20 20 20 75  .ao.sMem);.    u
24930 2e 61 6f 2e 73 4d 65 6d 2e 66 6c 61 67 73 20 3d  .ao.sMem.flags =
24931 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 0a 20 20 20 20   MEM_Null;..    
24932 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 72 65  /* If we have re
24933 61 64 20 6d 6f 72 65 20 68 65 61 64 65 72 20 64  ad more header d
24934 61 74 61 20 74 68 61 6e 20 77 61 73 20 63 6f 6e  ata than was con
24935 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 68 65  tained in the he
24936 61 64 65 72 2c 0a 20 20 20 20 2a 2a 20 6f 72 20  ader,.    ** or 
24937 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  if the end of th
24938 65 20 6c 61 73 74 20 66 69 65 6c 64 20 61 70 70  e last field app
24939 65 61 72 73 20 74 6f 20 62 65 20 70 61 73 74 20  ears to be past 
2493a 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20  the end of the. 
2493b 20 20 20 2a 2a 20 72 65 63 6f 72 64 2c 20 6f 72     ** record, or
2493c 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74   if the end of t
2493d 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 61 70  he last field ap
2493e 70 65 61 72 73 20 74 6f 20 62 65 20 62 65 66 6f  pears to be befo
2493f 72 65 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a  re the end.    *
24940 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20  * of the record 
24941 28 77 68 65 6e 20 61 6c 6c 20 66 69 65 6c 64 73  (when all fields
24942 20 70 72 65 73 65 6e 74 29 2c 20 74 68 65 6e 20   present), then 
24943 77 65 20 6d 75 73 74 20 62 65 20 64 65 61 6c 69  we must be deali
24944 6e 67 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 61  ng.    ** with a
24945 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73   corrupt databas
24946 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
24947 28 20 28 75 2e 61 6f 2e 7a 49 64 78 20 3e 20 75  ( (u.ao.zIdx > u
24948 2e 61 6f 2e 7a 45 6e 64 48 64 72 29 20 7c 7c 20  .ao.zEndHdr) || 
24949 28 75 2e 61 6f 2e 6f 66 66 73 65 74 20 3e 20 75  (u.ao.offset > u
2494a 2e 61 6f 2e 70 61 79 6c 6f 61 64 53 69 7a 65 29  .ao.payloadSize)
2494b 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 28 75 2e  .         || (u.
2494c 61 6f 2e 7a 49 64 78 3d 3d 75 2e 61 6f 2e 7a 45  ao.zIdx==u.ao.zE
2494d 6e 64 48 64 72 20 26 26 20 75 2e 61 6f 2e 6f 66  ndHdr && u.ao.of
2494e 66 73 65 74 21 3d 75 2e 61 6f 2e 70 61 79 6c 6f  fset!=u.ao.paylo
2494f 61 64 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 20  adSize) ){.     
24950 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
24951 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20  RUPT_BKPT;.     
24952 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f   goto op_column_
24953 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  out;.    }.  }..
24954 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6c    /* Get the col
24955 75 6d 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  umn information.
24956 20 49 66 20 75 2e 61 6f 2e 61 4f 66 66 73 65 74   If u.ao.aOffset
24957 5b 75 2e 61 6f 2e 70 32 5d 20 69 73 20 6e 6f 6e  [u.ao.p2] is non
24958 2d 7a 65 72 6f 2c 20 74 68 65 6e 0a 20 20 2a 2a  -zero, then.  **
24959 20 64 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65   deserialize the
2495a 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20   value from the 
2495b 72 65 63 6f 72 64 2e 20 49 66 20 75 2e 61 6f 2e  record. If u.ao.
2495c 61 4f 66 66 73 65 74 5b 75 2e 61 6f 2e 70 32 5d  aOffset[u.ao.p2]
2495d 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a 2a 20 74   is zero,.  ** t
2495e 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f  hen there are no
2495f 74 20 65 6e 6f 75 67 68 20 66 69 65 6c 64 73 20  t enough fields 
24960 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  in the record to
24961 20 73 61 74 69 73 66 79 20 74 68 65 0a 20 20 2a   satisfy the.  *
24962 2a 20 72 65 71 75 65 73 74 2e 20 20 49 6e 20 74  * request.  In t
24963 68 69 73 20 63 61 73 65 2c 20 73 65 74 20 74 68  his case, set th
24964 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20 6f 72 20  e value NULL or 
24965 74 6f 20 50 34 20 69 66 20 50 34 20 69 73 0a 20  to P4 if P4 is. 
24966 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   ** a pointer to
24967 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74 2e 0a 20   a Mem object.. 
24968 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 6f 2e 61   */.  if( u.ao.a
24969 4f 66 66 73 65 74 5b 75 2e 61 6f 2e 70 32 5d 20  Offset[u.ao.p2] 
2496a 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72  ){.    assert( r
2496b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c==SQLITE_OK );.
2496c 20 20 20 20 69 66 28 20 75 2e 61 6f 2e 7a 52 65      if( u.ao.zRe
2496d 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  c ){.      /* Th
2496e 69 73 20 69 73 20 74 68 65 20 63 6f 6d 6d 6f 6e  is is the common
2496f 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20   case where the 
24970 77 68 6f 6c 65 20 72 6f 77 20 66 69 74 73 20 6f  whole row fits o
24971 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20  n a single page 
24972 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 4d 65 6d  */.      VdbeMem
24973 52 65 6c 65 61 73 65 28 75 2e 61 6f 2e 70 44 65  Release(u.ao.pDe
24974 73 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  st);.      sqlit
24975 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28  e3VdbeSerialGet(
24976 28 75 38 20 2a 29 26 75 2e 61 6f 2e 7a 52 65 63  (u8 *)&u.ao.zRec
24977 5b 75 2e 61 6f 2e 61 4f 66 66 73 65 74 5b 75 2e  [u.ao.aOffset[u.
24978 61 6f 2e 70 32 5d 5d 2c 20 75 2e 61 6f 2e 61 54  ao.p2]], u.ao.aT
24979 79 70 65 5b 75 2e 61 6f 2e 70 32 5d 2c 20 75 2e  ype[u.ao.p2], u.
2497a 61 6f 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 7d  ao.pDest);.    }
2497b 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54  else{.      /* T
2497c 68 69 73 20 62 72 61 6e 63 68 20 68 61 70 70 65  his branch happe
2497d 6e 73 20 6f 6e 6c 79 20 77 68 65 6e 20 74 68 65  ns only when the
2497e 20 72 6f 77 20 6f 76 65 72 66 6c 6f 77 73 20 6f   row overflows o
2497f 6e 74 6f 20 6d 75 6c 74 69 70 6c 65 20 70 61 67  nto multiple pag
24980 65 73 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 6f  es */.      u.ao
24981 2e 74 20 3d 20 75 2e 61 6f 2e 61 54 79 70 65 5b  .t = u.ao.aType[
24982 75 2e 61 6f 2e 70 32 5d 3b 0a 20 20 20 20 20 20  u.ao.p2];.      
24983 69 66 28 20 28 70 4f 70 2d 3e 70 35 20 26 20 28  if( (pOp->p5 & (
24984 4f 50 46 4c 41 47 5f 4c 45 4e 47 54 48 41 52 47  OPFLAG_LENGTHARG
24985 7c 4f 50 46 4c 41 47 5f 54 59 50 45 4f 46 41 52  |OPFLAG_TYPEOFAR
24986 47 29 29 21 3d 30 0a 20 20 20 20 20 20 20 26 26  G))!=0.       &&
24987 20 28 28 75 2e 61 6f 2e 74 3e 3d 31 32 20 26 26   ((u.ao.t>=12 &&
24988 20 28 75 2e 61 6f 2e 74 26 31 29 3d 3d 30 29 20   (u.ao.t&1)==0) 
24989 7c 7c 20 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50  || (pOp->p5 & OP
2498a 46 4c 41 47 5f 54 59 50 45 4f 46 41 52 47 29 21  FLAG_TYPEOFARG)!
2498b 3d 30 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  =0).      ){.   
2498c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20       /* Content 
2498d 69 73 20 69 72 72 65 6c 65 76 61 6e 74 20 66 6f  is irrelevant fo
2498e 72 20 74 68 65 20 74 79 70 65 6f 66 28 29 20 66  r the typeof() f
2498f 75 6e 63 74 69 6f 6e 20 61 6e 64 20 66 6f 72 0a  unction and for.
24990 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c          ** the l
24991 65 6e 67 74 68 28 58 29 20 66 75 6e 63 74 69 6f  ength(X) functio
24992 6e 20 69 66 20 58 20 69 73 20 61 20 62 6c 6f 62  n if X is a blob
24993 2e 20 20 53 6f 20 77 65 20 6d 69 67 68 74 20 61  .  So we might a
24994 73 20 77 65 6c 6c 20 75 73 65 0a 20 20 20 20 20  s well use.     
24995 20 20 20 2a 2a 20 62 6f 67 75 73 20 63 6f 6e 74     ** bogus cont
24996 65 6e 74 20 72 61 74 68 65 72 20 74 68 61 6e 20  ent rather than 
24997 72 65 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74 20  reading content 
24998 66 72 6f 6d 20 64 69 73 6b 2e 20 20 4e 55 4c 4c  from disk.  NULL
24999 20 77 6f 72 6b 73 0a 20 20 20 20 20 20 20 20 2a   works.        *
2499a 2a 20 66 6f 72 20 74 65 78 74 20 61 6e 64 20 62  * for text and b
2499b 6c 6f 62 20 61 6e 64 20 77 68 61 74 65 76 65 72  lob and whatever
2499c 20 69 73 20 69 6e 20 74 68 65 20 75 2e 61 6f 2e   is in the u.ao.
2499d 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 20 76 61  payloadSize64 va
2499e 72 69 61 62 6c 65 0a 20 20 20 20 20 20 20 20 2a  riable.        *
2499f 2a 20 77 69 6c 6c 20 77 6f 72 6b 20 66 6f 72 20  * will work for 
249a0 65 76 65 72 79 74 68 69 6e 67 20 65 6c 73 65 2e  everything else.
249a1 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e 61 6f   */.        u.ao
249a2 2e 7a 44 61 74 61 20 3d 20 75 2e 61 6f 2e 74 3c  .zData = u.ao.t<
249a3 31 32 20 3f 20 28 63 68 61 72 2a 29 26 75 2e 61  12 ? (char*)&u.a
249a4 6f 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 20  o.payloadSize64 
249a5 3a 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  : 0;.      }else
249a6 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6f 2e 6c  {.        u.ao.l
249a7 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  en = sqlite3Vdbe
249a8 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e  SerialTypeLen(u.
249a9 61 6f 2e 74 29 3b 0a 20 20 20 20 20 20 20 20 73  ao.t);.        s
249aa 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
249ab 65 28 26 75 2e 61 6f 2e 73 4d 65 6d 2c 20 75 2e  e(&u.ao.sMem, u.
249ac 61 6f 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 20  ao.pDest);.     
249ad 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
249ae 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
249af 75 2e 61 6f 2e 70 43 72 73 72 2c 20 75 2e 61 6f  u.ao.pCrsr, u.ao
249b0 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6f 2e 70 32  .aOffset[u.ao.p2
249b1 5d 2c 20 75 2e 61 6f 2e 6c 65 6e 2c 20 20 75 2e  ], u.ao.len,  u.
249b2 61 6f 2e 70 43 2d 3e 69 73 49 6e 64 65 78 2c 0a  ao.pC->isIndex,.
249b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
249b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
249b5 20 20 20 20 20 26 75 2e 61 6f 2e 73 4d 65 6d 29       &u.ao.sMem)
249b6 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
249b7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
249b8 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6f 70           goto op
249b9 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20  _column_out;.   
249ba 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 75       }.        u
249bb 2e 61 6f 2e 7a 44 61 74 61 20 3d 20 75 2e 61 6f  .ao.zData = u.ao
249bc 2e 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20 20 20 7d  .sMem.z;.      }
249bd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
249be 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 2a  beSerialGet((u8*
249bf 29 75 2e 61 6f 2e 7a 44 61 74 61 2c 20 75 2e 61  )u.ao.zData, u.a
249c0 6f 2e 74 2c 20 75 2e 61 6f 2e 70 44 65 73 74 29  o.t, u.ao.pDest)
249c1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 6f  ;.    }.    u.ao
249c2 2e 70 44 65 73 74 2d 3e 65 6e 63 20 3d 20 65 6e  .pDest->enc = en
249c3 63 6f 64 69 6e 67 3b 0a 20 20 7d 65 6c 73 65 7b  coding;.  }else{
249c4 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34  .    if( pOp->p4
249c5 74 79 70 65 3d 3d 50 34 5f 4d 45 4d 20 29 7b 0a  type==P4_MEM ){.
249c6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
249c7 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28  eMemShallowCopy(
249c8 75 2e 61 6f 2e 70 44 65 73 74 2c 20 70 4f 70 2d  u.ao.pDest, pOp-
249c9 3e 70 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74  >p4.pMem, MEM_St
249ca 61 74 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65  atic);.    }else
249cb 7b 0a 20 20 20 20 20 20 4d 65 6d 53 65 74 54 79  {.      MemSetTy
249cc 70 65 46 6c 61 67 28 75 2e 61 6f 2e 70 44 65 73  peFlag(u.ao.pDes
249cd 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20  t, MEM_Null);.  
249ce 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
249cf 20 77 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20   we dynamically 
249d0 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20  allocated space 
249d1 74 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61  to hold the data
249d2 20 28 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71   (in the.  ** sq
249d3 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d  lite3VdbeMemFrom
249d4 42 74 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f  Btree() call abo
249d5 76 65 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65  ve) then transfe
249d6 72 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61  r control of tha
249d7 74 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c  t.  ** dynamical
249d8 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61  ly allocated spa
249d9 63 65 20 6f 76 65 72 20 74 6f 20 74 68 65 20 75  ce over to the u
249da 2e 61 6f 2e 70 44 65 73 74 20 73 74 72 75 63 74  .ao.pDest struct
249db 75 72 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70  ure..  ** This p
249dc 72 65 76 65 6e 74 73 20 61 20 6d 65 6d 6f 72 79  revents a memory
249dd 20 63 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20 69 66   copy..  */.  if
249de 28 20 75 2e 61 6f 2e 73 4d 65 6d 2e 7a 4d 61 6c  ( u.ao.sMem.zMal
249df 6c 6f 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72  loc ){.    asser
249e0 74 28 20 75 2e 61 6f 2e 73 4d 65 6d 2e 7a 3d 3d  t( u.ao.sMem.z==
249e1 75 2e 61 6f 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f  u.ao.sMem.zMallo
249e2 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  c );.    assert(
249e3 20 21 28 75 2e 61 6f 2e 70 44 65 73 74 2d 3e 66   !(u.ao.pDest->f
249e4 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 20  lags & MEM_Dyn) 
249e5 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  );.    assert( !
249e6 28 75 2e 61 6f 2e 70 44 65 73 74 2d 3e 66 6c 61  (u.ao.pDest->fla
249e7 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d  gs & (MEM_Blob|M
249e8 45 4d 5f 53 74 72 29 29 20 7c 7c 20 75 2e 61 6f  EM_Str)) || u.ao
249e9 2e 70 44 65 73 74 2d 3e 7a 3d 3d 75 2e 61 6f 2e  .pDest->z==u.ao.
249ea 73 4d 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 75 2e  sMem.z );.    u.
249eb 61 6f 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20  ao.pDest->flags 
249ec 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d  &= ~(MEM_Ephem|M
249ed 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20  EM_Static);.    
249ee 75 2e 61 6f 2e 70 44 65 73 74 2d 3e 66 6c 61 67  u.ao.pDest->flag
249ef 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20  s |= MEM_Term;. 
249f0 20 20 20 75 2e 61 6f 2e 70 44 65 73 74 2d 3e 7a     u.ao.pDest->z
249f1 20 3d 20 75 2e 61 6f 2e 73 4d 65 6d 2e 7a 3b 0a   = u.ao.sMem.z;.
249f2 20 20 20 20 75 2e 61 6f 2e 70 44 65 73 74 2d 3e      u.ao.pDest->
249f3 7a 4d 61 6c 6c 6f 63 20 3d 20 75 2e 61 6f 2e 73  zMalloc = u.ao.s
249f4 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 7d  Mem.zMalloc;.  }
249f5 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
249f6 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65  VdbeMemMakeWrite
249f7 61 62 6c 65 28 75 2e 61 6f 2e 70 44 65 73 74 29  able(u.ao.pDest)
249f8 3b 0a 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74  ;..op_column_out
249f9 3a 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  :.  UPDATE_MAX_B
249fa 4c 4f 42 53 49 5a 45 28 75 2e 61 6f 2e 70 44 65  LOBSIZE(u.ao.pDe
249fb 73 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f  st);.  REGISTER_
249fc 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75  TRACE(pOp->p3, u
249fd 2e 61 6f 2e 70 44 65 73 74 29 3b 0a 20 20 62 72  .ao.pDest);.  br
249fe 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
249ff 65 3a 20 41 66 66 69 6e 69 74 79 20 50 31 20 50  e: Affinity P1 P
24a00 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 41  2 * P4 *.**.** A
24a01 70 70 6c 79 20 61 66 66 69 6e 69 74 69 65 73 20  pply affinities 
24a02 74 6f 20 61 20 72 61 6e 67 65 20 6f 66 20 50 32  to a range of P2
24a03 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74   registers start
24a04 69 6e 67 20 77 69 74 68 20 50 31 2e 0a 2a 2a 0a  ing with P1..**.
24a05 2a 2a 20 50 34 20 69 73 20 61 20 73 74 72 69 6e  ** P4 is a strin
24a06 67 20 74 68 61 74 20 69 73 20 50 32 20 63 68 61  g that is P2 cha
24a07 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 54 68  racters long. Th
24a08 65 20 6e 74 68 20 63 68 61 72 61 63 74 65 72 20  e nth character 
24a09 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67  of the.** string
24a0a 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20 63   indicates the c
24a0b 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74  olumn affinity t
24a0c 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73  hat should be us
24a0d 65 64 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a  ed for the nth.*
24a0e 2a 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e  * memory cell in
24a0f 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a 2f 0a 63   the range..*/.c
24a10 61 73 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 3a  ase OP_Affinity:
24a11 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
24a12 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
24a13 65 64 20 69 6e 74 6f 20 75 2e 61 70 20 2a 2f 0a  ed into u.ap */.
24a14 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41    const char *zA
24a15 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 68  ffinity;   /* Th
24a16 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65  e affinity to be
24a17 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 63 68   applied */.  ch
24a18 61 72 20 63 41 66 66 3b 20 20 20 20 20 20 20 20  ar cAff;        
24a19 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67         /* A sing
24a1a 6c 65 20 63 68 61 72 61 63 74 65 72 20 6f 66 20  le character of 
24a1b 61 66 66 69 6e 69 74 79 20 2a 2f 0a 23 65 6e 64  affinity */.#end
24a1c 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
24a1d 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
24a1e 20 75 2e 61 70 20 2a 2f 0a 0a 20 20 75 2e 61 70   u.ap */..  u.ap
24a1f 2e 7a 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70  .zAffinity = pOp
24a20 2d 3e 70 34 2e 7a 3b 0a 20 20 61 73 73 65 72 74  ->p4.z;.  assert
24a21 28 20 75 2e 61 70 2e 7a 41 66 66 69 6e 69 74 79  ( u.ap.zAffinity
24a22 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
24a23 20 75 2e 61 70 2e 7a 41 66 66 69 6e 69 74 79 5b   u.ap.zAffinity[
24a24 70 4f 70 2d 3e 70 32 5d 3d 3d 30 20 29 3b 0a 20  pOp->p2]==0 );. 
24a25 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn1 = &aMem[pO
24a26 70 2d 3e 70 31 5d 3b 0a 20 20 77 68 69 6c 65 28  p->p1];.  while(
24a27 20 28 75 2e 61 70 2e 63 41 66 66 20 3d 20 2a 28   (u.ap.cAff = *(
24a28 75 2e 61 70 2e 7a 41 66 66 69 6e 69 74 79 2b 2b  u.ap.zAffinity++
24a29 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73  ))!=0 ){.    ass
24a2a 65 72 74 28 20 70 49 6e 31 20 3c 3d 20 26 70 2d  ert( pIn1 <= &p-
24a2b 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29  >aMem[p->nMem] )
24a2c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65  ;.    assert( me
24a2d 6d 49 73 56 61 6c 69 64 28 70 49 6e 31 29 20 29  mIsValid(pIn1) )
24a2e 3b 0a 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62  ;.    ExpandBlob
24a2f 28 70 49 6e 31 29 3b 0a 20 20 20 20 61 70 70 6c  (pIn1);.    appl
24a30 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20  yAffinity(pIn1, 
24a31 75 2e 61 70 2e 63 41 66 66 2c 20 65 6e 63 6f 64  u.ap.cAff, encod
24a32 69 6e 67 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b  ing);.    pIn1++
24a33 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
24a34 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 61 6b  ../* Opcode: Mak
24a35 65 52 65 63 6f 72 64 20 50 31 20 50 32 20 50 33  eRecord P1 P2 P3
24a36 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76   P4 *.**.** Conv
24a37 65 72 74 20 50 32 20 72 65 67 69 73 74 65 72 73  ert P2 registers
24a38 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
24a39 50 31 20 69 6e 74 6f 20 74 68 65 20 5b 72 65 63  P1 into the [rec
24a3a 6f 72 64 20 66 6f 72 6d 61 74 5d 0a 2a 2a 20 75  ord format].** u
24a3b 73 65 20 61 73 20 61 20 64 61 74 61 20 72 65 63  se as a data rec
24a3c 6f 72 64 20 69 6e 20 61 20 64 61 74 61 62 61 73  ord in a databas
24a3d 65 20 74 61 62 6c 65 20 6f 72 20 61 73 20 61 20  e table or as a 
24a3e 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 69 6e 64  key.** in an ind
24a3f 65 78 2e 20 20 54 68 65 20 4f 50 5f 43 6f 6c 75  ex.  The OP_Colu
24a40 6d 6e 20 6f 70 63 6f 64 65 20 63 61 6e 20 64 65  mn opcode can de
24a41 63 6f 64 65 20 74 68 65 20 72 65 63 6f 72 64 20  code the record 
24a42 6c 61 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 50 34 20  later..**.** P4 
24a43 6d 61 79 20 62 65 20 61 20 73 74 72 69 6e 67 20  may be a string 
24a44 74 68 61 74 20 69 73 20 50 32 20 63 68 61 72 61  that is P2 chara
24a45 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65  cters long.  The
24a46 20 6e 74 68 20 63 68 61 72 61 63 74 65 72 20 6f   nth character o
24a47 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20  f the.** string 
24a48 69 6e 64 69 63 61 74 65 73 20 74 68 65 20 63 6f  indicates the co
24a49 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 68  lumn affinity th
24a4a 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65  at should be use
24a4b 64 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a 2a  d for the nth.**
24a4c 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 69 6e   field of the in
24a4d 64 65 78 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54  dex key..**.** T
24a4e 68 65 20 6d 61 70 70 69 6e 67 20 66 72 6f 6d 20  he mapping from 
24a4f 63 68 61 72 61 63 74 65 72 20 74 6f 20 61 66 66  character to aff
24a50 69 6e 69 74 79 20 69 73 20 67 69 76 65 6e 20 62  inity is given b
24a51 79 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46  y the SQLITE_AFF
24a52 5f 0a 2a 2a 20 6d 61 63 72 6f 73 20 64 65 66 69  _.** macros defi
24a53 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  ned in sqliteInt
24a54 2e 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20  .h..**.** If P4 
24a55 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61 6c 6c  is NULL then all
24a56 20 69 6e 64 65 78 20 66 69 65 6c 64 73 20 68 61   index fields ha
24a57 76 65 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  ve the affinity 
24a58 4e 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NONE..*/.case OP
24a59 5f 4d 61 6b 65 52 65 63 6f 72 64 3a 20 7b 0a 23  _MakeRecord: {.#
24a5a 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
24a5b 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
24a5c 6e 74 6f 20 75 2e 61 71 20 2a 2f 0a 20 20 75 38  nto u.aq */.  u8
24a5d 20 2a 7a 4e 65 77 52 65 63 6f 72 64 3b 20 20 20   *zNewRecord;   
24a5e 20 20 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72       /* A buffer
24a5f 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74   to hold the dat
24a60 61 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65  a for the new re
24a61 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  cord */.  Mem *p
24a62 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rec;            
24a63 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 65 63 6f   /* The new reco
24a64 72 64 20 2a 2f 0a 20 20 75 36 34 20 6e 44 61 74  rd */.  u64 nDat
24a65 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  a;             /
24a66 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
24a67 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 20  s of data space 
24a68 2a 2f 0a 20 20 69 6e 74 20 6e 48 64 72 3b 20 20  */.  int nHdr;  
24a69 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
24a6a 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
24a6b 66 20 68 65 61 64 65 72 20 73 70 61 63 65 20 2a  f header space *
24a6c 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 3b 20 20  /.  i64 nByte;  
24a6d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
24a6e 74 61 20 73 70 61 63 65 20 72 65 71 75 69 72 65  ta space require
24a6f 64 20 66 6f 72 20 74 68 69 73 20 72 65 63 6f 72  d for this recor
24a70 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f  d */.  int nZero
24a71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
24a72 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20   Number of zero 
24a73 62 79 74 65 73 20 61 74 20 74 68 65 20 65 6e 64  bytes at the end
24a74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a   of the record *
24a75 2f 0a 20 20 69 6e 74 20 6e 56 61 72 69 6e 74 3b  /.  int nVarint;
24a76 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
24a77 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
24a78 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20 75   a varint */.  u
24a79 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 20  32 serial_type; 
24a7a 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 66 69        /* Type fi
24a7b 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44  eld */.  Mem *pD
24a7c 61 74 61 30 3b 20 20 20 20 20 20 20 20 20 20 20  ata0;           
24a7d 2f 2a 20 46 69 72 73 74 20 66 69 65 6c 64 20 74  /* First field t
24a7e 6f 20 62 65 20 63 6f 6d 62 69 6e 65 64 20 69 6e  o be combined in
24a7f 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  to the record */
24a80 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20  .  Mem *pLast;  
24a81 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73            /* Las
24a82 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 72  t field of the r
24a83 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ecord */.  int n
24a84 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20  Field;          
24a85 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66    /* Number of f
24a86 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63  ields in the rec
24a87 6f 72 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ord */.  char *z
24a88 41 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20  Affinity;       
24a89 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20  /* The affinity 
24a8a 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 72  string for the r
24a8b 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 66  ecord */.  int f
24a8c 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20  ile_format;     
24a8d 20 20 2f 2a 20 46 69 6c 65 20 66 6f 72 6d 61 74    /* File format
24a8e 20 74 6f 20 75 73 65 20 66 6f 72 20 65 6e 63 6f   to use for enco
24a8f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  ding */.  int i;
24a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24a91 20 2f 2a 20 53 70 61 63 65 20 75 73 65 64 20 69   /* Space used i
24a92 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b 5d 20 2a  n zNewRecord[] *
24a93 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20  /.  int len;    
24a94 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
24a95 6e 67 74 68 20 6f 66 20 61 20 66 69 65 6c 64 20  ngth of a field 
24a96 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  */.#endif /* loc
24a97 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
24a98 65 64 20 69 6e 74 6f 20 75 2e 61 71 20 2a 2f 0a  ed into u.aq */.
24a99 0a 20 20 2f 2a 20 41 73 73 75 6d 69 6e 67 20 74  .  /* Assuming t
24a9a 68 65 20 72 65 63 6f 72 64 20 63 6f 6e 74 61 69  he record contai
24a9b 6e 73 20 4e 20 66 69 65 6c 64 73 2c 20 74 68 65  ns N fields, the
24a9c 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20 6c   record format l
24a9d 6f 6f 6b 73 0a 20 20 2a 2a 20 6c 69 6b 65 20 74  ooks.  ** like t
24a9e 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 2d  his:.  **.  ** -
24a9f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24aa0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24aa1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24aa2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24aa3 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 7c 20 68  -------.  ** | h
24aa4 64 72 2d 73 69 7a 65 20 7c 20 74 79 70 65 20 30  dr-size | type 0
24aa5 20 7c 20 74 79 70 65 20 31 20 7c 20 2e 2e 2e 20   | type 1 | ... 
24aa6 7c 20 74 79 70 65 20 4e 2d 31 20 7c 20 64 61 74  | type N-1 | dat
24aa7 61 30 20 7c 20 2e 2e 2e 20 7c 20 64 61 74 61 20  a0 | ... | data 
24aa8 4e 2d 31 20 7c 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d  N-1 |.  ** -----
24aa9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24aaa 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24aab 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24aac 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24aad 2d 2d 2d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44 61  ---.  **.  ** Da
24aae 74 61 28 30 29 20 69 73 20 74 61 6b 65 6e 20 66  ta(0) is taken f
24aaf 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31 2e  rom register P1.
24ab0 20 20 44 61 74 61 28 31 29 20 63 6f 6d 65 73 20    Data(1) comes 
24ab1 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31  from register P1
24ab2 2b 31 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 66  +1.  ** and so f
24ab3 72 6f 74 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  roth..  **.  ** 
24ab4 45 61 63 68 20 74 79 70 65 20 66 69 65 6c 64 20  Each type field 
24ab5 69 73 20 61 20 76 61 72 69 6e 74 20 72 65 70 72  is a varint repr
24ab6 65 73 65 6e 74 69 6e 67 20 74 68 65 20 73 65 72  esenting the ser
24ab7 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 0a  ial type of the.
24ab8 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69    ** correspondi
24ab9 6e 67 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 20  ng data element 
24aba 28 73 65 65 20 73 71 6c 69 74 65 33 56 64 62 65  (see sqlite3Vdbe
24abb 53 65 72 69 61 6c 54 79 70 65 28 29 29 2e 20 54  SerialType()). T
24abc 68 65 0a 20 20 2a 2a 20 68 64 72 2d 73 69 7a 65  he.  ** hdr-size
24abd 20 66 69 65 6c 64 20 69 73 20 61 6c 73 6f 20 61   field is also a
24abe 20 76 61 72 69 6e 74 20 77 68 69 63 68 20 69 73   varint which is
24abf 20 74 68 65 20 6f 66 66 73 65 74 20 66 72 6f 6d   the offset from
24ac0 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20   the beginning. 
24ac1 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72   ** of the recor
24ac2 64 20 74 6f 20 64 61 74 61 30 2e 0a 20 20 2a 2f  d to data0..  */
24ac3 0a 20 20 75 2e 61 71 2e 6e 44 61 74 61 20 3d 20  .  u.aq.nData = 
24ac4 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  0;         /* Nu
24ac5 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
24ac6 20 64 61 74 61 20 73 70 61 63 65 20 2a 2f 0a 20   data space */. 
24ac7 20 75 2e 61 71 2e 6e 48 64 72 20 3d 20 30 3b 20   u.aq.nHdr = 0; 
24ac8 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
24ac9 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 68  er of bytes of h
24aca 65 61 64 65 72 20 73 70 61 63 65 20 2a 2f 0a 20  eader space */. 
24acb 20 75 2e 61 71 2e 6e 5a 65 72 6f 20 3d 20 30 3b   u.aq.nZero = 0;
24acc 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
24acd 65 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73  er of zero bytes
24ace 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
24acf 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75  he record */.  u
24ad0 2e 61 71 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70  .aq.nField = pOp
24ad1 2d 3e 70 31 3b 0a 20 20 75 2e 61 71 2e 7a 41 66  ->p1;.  u.aq.zAf
24ad2 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 34  finity = pOp->p4
24ad3 2e 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  .z;.  assert( u.
24ad4 61 71 2e 6e 46 69 65 6c 64 3e 30 20 26 26 20 70  aq.nField>0 && p
24ad5 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d  Op->p2>0 && pOp-
24ad6 3e 70 32 2b 75 2e 61 71 2e 6e 46 69 65 6c 64 3c  >p2+u.aq.nField<
24ad7 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20  =p->nMem+1 );.  
24ad8 75 2e 61 71 2e 70 44 61 74 61 30 20 3d 20 26 61  u.aq.pData0 = &a
24ad9 4d 65 6d 5b 75 2e 61 71 2e 6e 46 69 65 6c 64 5d  Mem[u.aq.nField]
24ada 3b 0a 20 20 75 2e 61 71 2e 6e 46 69 65 6c 64 20  ;.  u.aq.nField 
24adb 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61  = pOp->p2;.  u.a
24adc 71 2e 70 4c 61 73 74 20 3d 20 26 75 2e 61 71 2e  q.pLast = &u.aq.
24add 70 44 61 74 61 30 5b 75 2e 61 71 2e 6e 46 69 65  pData0[u.aq.nFie
24ade 6c 64 2d 31 5d 3b 0a 20 20 75 2e 61 71 2e 66 69  ld-1];.  u.aq.fi
24adf 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 70 2d 3e 6d  le_format = p->m
24ae0 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61  inWriteFileForma
24ae1 74 3b 0a 0a 20 20 2f 2a 20 49 64 65 6e 74 69 66  t;..  /* Identif
24ae2 79 20 74 68 65 20 6f 75 74 70 75 74 20 72 65 67  y the output reg
24ae3 69 73 74 65 72 20 2a 2f 0a 20 20 61 73 73 65 72  ister */.  asser
24ae4 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e  t( pOp->p3<pOp->
24ae5 70 31 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70  p1 || pOp->p3>=p
24ae6 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32 20 29  Op->p1+pOp->p2 )
24ae7 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d  ;.  pOut = &aMem
24ae8 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 6d 65 6d  [pOp->p3];.  mem
24ae9 41 62 6f 75 74 54 6f 43 68 61 6e 67 65 28 70 2c  AboutToChange(p,
24aea 20 70 4f 75 74 29 3b 0a 0a 20 20 2f 2a 20 4c 6f   pOut);..  /* Lo
24aeb 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65  op through the e
24aec 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 77 69 6c  lements that wil
24aed 6c 20 6d 61 6b 65 20 75 70 20 74 68 65 20 72 65  l make up the re
24aee 63 6f 72 64 20 74 6f 20 66 69 67 75 72 65 0a 20  cord to figure. 
24aef 20 2a 2a 20 6f 75 74 20 68 6f 77 20 6d 75 63 68   ** out how much
24af0 20 73 70 61 63 65 20 69 73 20 72 65 71 75 69 72   space is requir
24af1 65 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72  ed for the new r
24af2 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f  ecord..  */.  fo
24af3 72 28 75 2e 61 71 2e 70 52 65 63 3d 75 2e 61 71  r(u.aq.pRec=u.aq
24af4 2e 70 44 61 74 61 30 3b 20 75 2e 61 71 2e 70 52  .pData0; u.aq.pR
24af5 65 63 3c 3d 75 2e 61 71 2e 70 4c 61 73 74 3b 20  ec<=u.aq.pLast; 
24af6 75 2e 61 71 2e 70 52 65 63 2b 2b 29 7b 0a 20 20  u.aq.pRec++){.  
24af7 20 20 61 73 73 65 72 74 28 20 6d 65 6d 49 73 56    assert( memIsV
24af8 61 6c 69 64 28 75 2e 61 71 2e 70 52 65 63 29 20  alid(u.aq.pRec) 
24af9 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 71 2e  );.    if( u.aq.
24afa 7a 41 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20  zAffinity ){.   
24afb 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79     applyAffinity
24afc 28 75 2e 61 71 2e 70 52 65 63 2c 20 75 2e 61 71  (u.aq.pRec, u.aq
24afd 2e 7a 41 66 66 69 6e 69 74 79 5b 75 2e 61 71 2e  .zAffinity[u.aq.
24afe 70 52 65 63 2d 75 2e 61 71 2e 70 44 61 74 61 30  pRec-u.aq.pData0
24aff 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20  ], encoding);.  
24b00 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 61 71    }.    if( u.aq
24b01 2e 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d  .pRec->flags&MEM
24b02 5f 5a 65 72 6f 20 26 26 20 75 2e 61 71 2e 70 52  _Zero && u.aq.pR
24b03 65 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20  ec->n>0 ){.     
24b04 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45   sqlite3VdbeMemE
24b05 78 70 61 6e 64 42 6c 6f 62 28 75 2e 61 71 2e 70  xpandBlob(u.aq.p
24b06 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Rec);.    }.    
24b07 75 2e 61 71 2e 73 65 72 69 61 6c 5f 74 79 70 65  u.aq.serial_type
24b08 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
24b09 72 69 61 6c 54 79 70 65 28 75 2e 61 71 2e 70 52  rialType(u.aq.pR
24b0a 65 63 2c 20 75 2e 61 71 2e 66 69 6c 65 5f 66 6f  ec, u.aq.file_fo
24b0b 72 6d 61 74 29 3b 0a 20 20 20 20 75 2e 61 71 2e  rmat);.    u.aq.
24b0c 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62  len = sqlite3Vdb
24b0d 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75  eSerialTypeLen(u
24b0e 2e 61 71 2e 73 65 72 69 61 6c 5f 74 79 70 65 29  .aq.serial_type)
24b0f 3b 0a 20 20 20 20 75 2e 61 71 2e 6e 44 61 74 61  ;.    u.aq.nData
24b10 20 2b 3d 20 75 2e 61 71 2e 6c 65 6e 3b 0a 20 20   += u.aq.len;.  
24b11 20 20 75 2e 61 71 2e 6e 48 64 72 20 2b 3d 20 73    u.aq.nHdr += s
24b12 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28  qlite3VarintLen(
24b13 75 2e 61 71 2e 73 65 72 69 61 6c 5f 74 79 70 65  u.aq.serial_type
24b14 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 71 2e  );.    if( u.aq.
24b15 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pRec->flags & ME
24b16 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
24b17 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 7a 65 72  /* Only pure zer
24b18 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 73 20 63  o-filled BLOBs c
24b19 61 6e 20 62 65 20 69 6e 70 75 74 20 74 6f 20 74  an be input to t
24b1a 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 20 20 20  his Opcode..    
24b1b 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 61    ** We do not a
24b1c 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 74 68 20  llow blobs with 
24b1d 61 20 70 72 65 66 69 78 20 61 6e 64 20 61 20 7a  a prefix and a z
24b1e 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e  ero-filled tail.
24b1f 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 71 2e 6e   */.      u.aq.n
24b20 5a 65 72 6f 20 2b 3d 20 75 2e 61 71 2e 70 52 65  Zero += u.aq.pRe
24b21 63 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20  c->u.nZero;.    
24b22 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 71 2e 6c  }else if( u.aq.l
24b23 65 6e 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 71  en ){.      u.aq
24b24 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20  .nZero = 0;.    
24b25 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20  }.  }..  /* Add 
24b26 74 68 65 20 69 6e 69 74 69 61 6c 20 68 65 61 64  the initial head
24b27 65 72 20 76 61 72 69 6e 74 20 61 6e 64 20 74 6f  er varint and to
24b28 74 61 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f 0a  tal the size */.
24b29 20 20 75 2e 61 71 2e 6e 48 64 72 20 2b 3d 20 75    u.aq.nHdr += u
24b2a 2e 61 71 2e 6e 56 61 72 69 6e 74 20 3d 20 73 71  .aq.nVarint = sq
24b2b 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75  lite3VarintLen(u
24b2c 2e 61 71 2e 6e 48 64 72 29 3b 0a 20 20 69 66 28  .aq.nHdr);.  if(
24b2d 20 75 2e 61 71 2e 6e 56 61 72 69 6e 74 3c 73 71   u.aq.nVarint<sq
24b2e 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75  lite3VarintLen(u
24b2f 2e 61 71 2e 6e 48 64 72 29 20 29 7b 0a 20 20 20  .aq.nHdr) ){.   
24b30 20 75 2e 61 71 2e 6e 48 64 72 2b 2b 3b 0a 20 20   u.aq.nHdr++;.  
24b31 7d 0a 20 20 75 2e 61 71 2e 6e 42 79 74 65 20 3d  }.  u.aq.nByte =
24b32 20 75 2e 61 71 2e 6e 48 64 72 2b 75 2e 61 71 2e   u.aq.nHdr+u.aq.
24b33 6e 44 61 74 61 2d 75 2e 61 71 2e 6e 5a 65 72 6f  nData-u.aq.nZero
24b34 3b 0a 20 20 69 66 28 20 75 2e 61 71 2e 6e 42 79  ;.  if( u.aq.nBy
24b35 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  te>db->aLimit[SQ
24b36 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
24b37 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  H] ){.    goto t
24b38 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 2f  oo_big;.  }..  /
24b39 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
24b3a 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 72 20  output register 
24b3b 68 61 73 20 61 20 62 75 66 66 65 72 20 6c 61 72  has a buffer lar
24b3c 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f  ge enough to sto
24b3d 72 65 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77 20  re.  ** the new 
24b3e 72 65 63 6f 72 64 2e 20 54 68 65 20 6f 75 74 70  record. The outp
24b3f 75 74 20 72 65 67 69 73 74 65 72 20 28 70 4f 70  ut register (pOp
24b40 2d 3e 70 33 29 20 69 73 20 6e 6f 74 20 61 6c 6c  ->p3) is not all
24b41 6f 77 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65 20  owed to.  ** be 
24b42 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74  one of the input
24b43 20 72 65 67 69 73 74 65 72 73 20 28 62 65 63 61   registers (beca
24b44 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  use the followin
24b45 67 20 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20 73  g call to.  ** s
24b46 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
24b47 77 28 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62 65  w() could clobbe
24b48 72 20 74 68 65 20 76 61 6c 75 65 20 62 65 66 6f  r the value befo
24b49 72 65 20 69 74 20 69 73 20 75 73 65 64 29 2e 0a  re it is used)..
24b4a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
24b4b 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f  e3VdbeMemGrow(pO
24b4c 75 74 2c 20 28 69 6e 74 29 75 2e 61 71 2e 6e 42  ut, (int)u.aq.nB
24b4d 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67  yte, 0) ){.    g
24b4e 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
24b4f 20 20 75 2e 61 71 2e 7a 4e 65 77 52 65 63 6f 72    u.aq.zNewRecor
24b50 64 20 3d 20 28 75 38 20 2a 29 70 4f 75 74 2d 3e  d = (u8 *)pOut->
24b51 7a 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 74  z;..  /* Write t
24b52 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75  he record */.  u
24b53 2e 61 71 2e 69 20 3d 20 70 75 74 56 61 72 69 6e  .aq.i = putVarin
24b54 74 33 32 28 75 2e 61 71 2e 7a 4e 65 77 52 65 63  t32(u.aq.zNewRec
24b55 6f 72 64 2c 20 75 2e 61 71 2e 6e 48 64 72 29 3b  ord, u.aq.nHdr);
24b56 0a 20 20 66 6f 72 28 75 2e 61 71 2e 70 52 65 63  .  for(u.aq.pRec
24b57 3d 75 2e 61 71 2e 70 44 61 74 61 30 3b 20 75 2e  =u.aq.pData0; u.
24b58 61 71 2e 70 52 65 63 3c 3d 75 2e 61 71 2e 70 4c  aq.pRec<=u.aq.pL
24b59 61 73 74 3b 20 75 2e 61 71 2e 70 52 65 63 2b 2b  ast; u.aq.pRec++
24b5a 29 7b 0a 20 20 20 20 75 2e 61 71 2e 73 65 72 69  ){.    u.aq.seri
24b5b 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65  al_type = sqlite
24b5c 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28  3VdbeSerialType(
24b5d 75 2e 61 71 2e 70 52 65 63 2c 20 75 2e 61 71 2e  u.aq.pRec, u.aq.
24b5e 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20  file_format);.  
24b5f 20 20 75 2e 61 71 2e 69 20 2b 3d 20 70 75 74 56    u.aq.i += putV
24b60 61 72 69 6e 74 33 32 28 26 75 2e 61 71 2e 7a 4e  arint32(&u.aq.zN
24b61 65 77 52 65 63 6f 72 64 5b 75 2e 61 71 2e 69 5d  ewRecord[u.aq.i]
24b62 2c 20 75 2e 61 71 2e 73 65 72 69 61 6c 5f 74 79  , u.aq.serial_ty
24b63 70 65 29 3b 20 20 20 20 20 20 2f 2a 20 73 65 72  pe);      /* ser
24b64 69 61 6c 20 74 79 70 65 20 2a 2f 0a 20 20 7d 0a  ial type */.  }.
24b65 20 20 66 6f 72 28 75 2e 61 71 2e 70 52 65 63 3d    for(u.aq.pRec=
24b66 75 2e 61 71 2e 70 44 61 74 61 30 3b 20 75 2e 61  u.aq.pData0; u.a
24b67 71 2e 70 52 65 63 3c 3d 75 2e 61 71 2e 70 4c 61  q.pRec<=u.aq.pLa
24b68 73 74 3b 20 75 2e 61 71 2e 70 52 65 63 2b 2b 29  st; u.aq.pRec++)
24b69 7b 20 20 2f 2a 20 73 65 72 69 61 6c 20 64 61 74  {  /* serial dat
24b6a 61 20 2a 2f 0a 20 20 20 20 75 2e 61 71 2e 69 20  a */.    u.aq.i 
24b6b 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  += sqlite3VdbeSe
24b6c 72 69 61 6c 50 75 74 28 26 75 2e 61 71 2e 7a 4e  rialPut(&u.aq.zN
24b6d 65 77 52 65 63 6f 72 64 5b 75 2e 61 71 2e 69 5d  ewRecord[u.aq.i]
24b6e 2c 20 28 69 6e 74 29 28 75 2e 61 71 2e 6e 42 79  , (int)(u.aq.nBy
24b6f 74 65 2d 75 2e 61 71 2e 69 29 2c 20 75 2e 61 71  te-u.aq.i), u.aq
24b70 2e 70 52 65 63 2c 75 2e 61 71 2e 66 69 6c 65 5f  .pRec,u.aq.file_
24b71 66 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a 20 20 61  format);.  }.  a
24b72 73 73 65 72 74 28 20 75 2e 61 71 2e 69 3d 3d 75  ssert( u.aq.i==u
24b73 2e 61 71 2e 6e 42 79 74 65 20 29 3b 0a 0a 20 20  .aq.nByte );..  
24b74 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e  assert( pOp->p3>
24b75 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  0 && pOp->p3<=p-
24b76 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 2d  >nMem );.  pOut-
24b77 3e 6e 20 3d 20 28 69 6e 74 29 75 2e 61 71 2e 6e  >n = (int)u.aq.n
24b78 42 79 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c  Byte;.  pOut->fl
24b79 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c  ags = MEM_Blob |
24b7a 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 70 4f 75 74   MEM_Dyn;.  pOut
24b7b 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66  ->xDel = 0;.  if
24b7c 28 20 75 2e 61 71 2e 6e 5a 65 72 6f 20 29 7b 0a  ( u.aq.nZero ){.
24b7d 20 20 20 20 70 4f 75 74 2d 3e 75 2e 6e 5a 65 72      pOut->u.nZer
24b7e 6f 20 3d 20 75 2e 61 71 2e 6e 5a 65 72 6f 3b 0a  o = u.aq.nZero;.
24b7f 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20      pOut->flags 
24b80 7c 3d 20 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 7d  |= MEM_Zero;.  }
24b81 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53  .  pOut->enc = S
24b82 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20  QLITE_UTF8;  /* 
24b83 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62  In case the blob
24b84 20 69 73 20 65 76 65 72 20 63 6f 6e 76 65 72 74   is ever convert
24b85 65 64 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20  ed to text */.  
24b86 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
24b87 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20  Op->p3, pOut);. 
24b88 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
24b89 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72  SIZE(pOut);.  br
24b8a 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
24b8b 65 3a 20 43 6f 75 6e 74 20 50 31 20 50 32 20 2a  e: Count P1 P2 *
24b8c 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65   * *.**.** Store
24b8d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
24b8e 6e 74 72 69 65 73 20 28 61 6e 20 69 6e 74 65 67  ntries (an integ
24b8f 65 72 20 76 61 6c 75 65 29 20 69 6e 20 74 68 65  er value) in the
24b90 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
24b91 0a 2a 2a 20 6f 70 65 6e 65 64 20 62 79 20 63 75  .** opened by cu
24b92 72 73 6f 72 20 50 31 20 69 6e 20 72 65 67 69 73  rsor P1 in regis
24b93 74 65 72 20 50 32 0a 2a 2f 0a 23 69 66 6e 64 65  ter P2.*/.#ifnde
24b94 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54  f SQLITE_OMIT_BT
24b95 52 45 45 43 4f 55 4e 54 0a 63 61 73 65 20 4f 50  REECOUNT.case OP
24b96 5f 43 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20 20  _Count: {       
24b97 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
24b98 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f  ease */.#if 0  /
24b99 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
24b9a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
24b9b 72 20 2a 2f 0a 20 20 69 36 34 20 6e 45 6e 74 72  r */.  i64 nEntr
24b9c 79 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  y;.  BtCursor *p
24b9d 43 72 73 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  Crsr;.#endif /* 
24b9e 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
24b9f 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 72 20  moved into u.ar 
24ba0 2a 2f 0a 0a 20 20 75 2e 61 72 2e 70 43 72 73 72  */..  u.ar.pCrsr
24ba1 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
24ba2 3e 70 31 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20  >p1]->pCursor;. 
24ba3 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 61 72   if( ALWAYS(u.ar
24ba4 2e 70 43 72 73 72 29 20 29 7b 0a 20 20 20 20 72  .pCrsr) ){.    r
24ba5 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
24ba6 43 6f 75 6e 74 28 75 2e 61 72 2e 70 43 72 73 72  Count(u.ar.pCrsr
24ba7 2c 20 26 75 2e 61 72 2e 6e 45 6e 74 72 79 29 3b  , &u.ar.nEntry);
24ba8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e  .  }else{.    u.
24ba9 61 72 2e 6e 45 6e 74 72 79 20 3d 20 30 3b 0a 20  ar.nEntry = 0;. 
24baa 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d   }.  pOut->u.i =
24bab 20 75 2e 61 72 2e 6e 45 6e 74 72 79 3b 0a 20 20   u.ar.nEntry;.  
24bac 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  break;.}.#endif.
24bad 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 61 76 65  ./* Opcode: Save
24bae 70 6f 69 6e 74 20 50 31 20 2a 20 2a 20 50 34 20  point P1 * * P4 
24baf 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 2c 20 72 65  *.**.** Open, re
24bb0 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63  lease or rollbac
24bb1 6b 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20  k the savepoint 
24bb2 6e 61 6d 65 64 20 62 79 20 70 61 72 61 6d 65 74  named by paramet
24bb3 65 72 20 50 34 2c 20 64 65 70 65 6e 64 69 6e 67  er P4, depending
24bb4 0a 2a 2a 20 6f 6e 20 74 68 65 20 76 61 6c 75 65  .** on the value
24bb5 20 6f 66 20 50 31 2e 20 54 6f 20 6f 70 65 6e 20   of P1. To open 
24bb6 61 20 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 2c  a new savepoint,
24bb7 20 50 31 3d 3d 30 2e 20 54 6f 20 72 65 6c 65 61   P1==0. To relea
24bb8 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 6e 0a 2a  se (commit) an.*
24bb9 2a 20 65 78 69 73 74 69 6e 67 20 73 61 76 65 70  * existing savep
24bba 6f 69 6e 74 2c 20 50 31 3d 3d 31 2c 20 6f 72 20  oint, P1==1, or 
24bbb 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 20 65  to rollback an e
24bbc 78 69 73 74 69 6e 67 20 73 61 76 65 70 6f 69 6e  xisting savepoin
24bbd 74 20 50 31 3d 3d 32 2e 0a 2a 2f 0a 63 61 73 65  t P1==2..*/.case
24bbe 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 3a 20 7b   OP_Savepoint: {
24bbf 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
24bc0 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
24bc1 20 69 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 20 20   into u.as */.  
24bc2 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20  int p1;         
24bc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24bc4 2f 2a 20 56 61 6c 75 65 20 6f 66 20 50 31 20 6f  /* Value of P1 o
24bc5 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 68 61 72  perand */.  char
24bc6 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
24bc7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
24bc8 61 6d 65 20 6f 66 20 73 61 76 65 70 6f 69 6e 74  ame of savepoint
24bc9 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b   */.  int nName;
24bca 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e  .  Savepoint *pN
24bcb 65 77 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74 20  ew;.  Savepoint 
24bcc 2a 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 53  *pSavepoint;.  S
24bcd 61 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b 0a  avepoint *pTmp;.
24bce 20 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74    int iSavepoint
24bcf 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 23 65 6e 64  ;.  int ii;.#end
24bd0 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
24bd1 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
24bd2 20 75 2e 61 73 20 2a 2f 0a 0a 20 20 75 2e 61 73   u.as */..  u.as
24bd3 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  .p1 = pOp->p1;. 
24bd4 20 75 2e 61 73 2e 7a 4e 61 6d 65 20 3d 20 70 4f   u.as.zName = pO
24bd5 70 2d 3e 70 34 2e 7a 3b 0a 0a 20 20 2f 2a 20 41  p->p4.z;..  /* A
24bd6 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 75  ssert that the u
24bd7 2e 61 73 2e 70 31 20 70 61 72 61 6d 65 74 65 72  .as.p1 parameter
24bd8 20 69 73 20 76 61 6c 69 64 2e 20 41 6c 73 6f 20   is valid. Also 
24bd9 74 68 61 74 20 69 66 20 74 68 65 72 65 20 69 73  that if there is
24bda 20 6e 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 74 72   no open.  ** tr
24bdb 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20  ansaction, then 
24bdc 74 68 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20  there cannot be 
24bdd 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 2e 0a  any savepoints..
24bde 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64    */.  assert( d
24bdf 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 30  b->pSavepoint==0
24be0 20 7c 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d   || db->autoComm
24be1 69 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  it==0 );.  asser
24be2 74 28 20 75 2e 61 73 2e 70 31 3d 3d 53 41 56 45  t( u.as.p1==SAVE
24be3 50 4f 49 4e 54 5f 42 45 47 49 4e 7c 7c 75 2e 61  POINT_BEGIN||u.a
24be4 73 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f  s.p1==SAVEPOINT_
24be5 52 45 4c 45 41 53 45 7c 7c 75 2e 61 73 2e 70 31  RELEASE||u.as.p1
24be6 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  ==SAVEPOINT_ROLL
24be7 42 41 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  BACK );.  assert
24be8 28 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74  ( db->pSavepoint
24be9 20 7c 7c 20 64 62 2d 3e 69 73 54 72 61 6e 73 61   || db->isTransa
24bea 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3d 3d  ctionSavepoint==
24beb 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63  0 );.  assert( c
24bec 68 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75  heckSavepointCou
24bed 6e 74 28 64 62 29 20 29 3b 0a 0a 20 20 69 66 28  nt(db) );..  if(
24bee 20 75 2e 61 73 2e 70 31 3d 3d 53 41 56 45 50 4f   u.as.p1==SAVEPO
24bef 49 4e 54 5f 42 45 47 49 4e 20 29 7b 0a 20 20 20  INT_BEGIN ){.   
24bf0 20 69 66 28 20 64 62 2d 3e 77 72 69 74 65 56 64   if( db->writeVd
24bf1 62 65 43 6e 74 3e 30 20 29 7b 0a 20 20 20 20 20  beCnt>0 ){.     
24bf2 20 2f 2a 20 41 20 6e 65 77 20 73 61 76 65 70 6f   /* A new savepo
24bf3 69 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 63 72  int cannot be cr
24bf4 65 61 74 65 64 20 69 66 20 74 68 65 72 65 20 61  eated if there a
24bf5 72 65 20 61 63 74 69 76 65 20 77 72 69 74 65 0a  re active write.
24bf6 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65        ** stateme
24bf7 6e 74 73 20 28 69 2e 65 2e 20 6f 70 65 6e 20 72  nts (i.e. open r
24bf8 65 61 64 2f 77 72 69 74 65 20 69 6e 63 72 65 6d  ead/write increm
24bf9 65 6e 74 61 6c 20 62 6c 6f 62 20 68 61 6e 64 6c  ental blob handl
24bfa 65 73 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  es)..      */.  
24bfb 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
24bfc 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
24bfd 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70  , db, "cannot op
24bfe 65 6e 20 73 61 76 65 70 6f 69 6e 74 20 2d 20 22  en savepoint - "
24bff 0a 20 20 20 20 20 20 20 20 22 53 51 4c 20 73 74  .        "SQL st
24c00 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67  atements in prog
24c01 72 65 73 73 22 29 3b 0a 20 20 20 20 20 20 72 63  ress");.      rc
24c02 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   = SQLITE_BUSY;.
24c03 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
24c04 20 75 2e 61 73 2e 6e 4e 61 6d 65 20 3d 20 73 71   u.as.nName = sq
24c05 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 75 2e  lite3Strlen30(u.
24c06 61 73 2e 7a 4e 61 6d 65 29 3b 0a 0a 23 69 66 6e  as.zName);..#ifn
24c07 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
24c08 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
24c09 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20     /* This call 
24c0a 69 73 20 4f 6b 20 65 76 65 6e 20 69 66 20 74 68  is Ok even if th
24c0b 69 73 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20  is savepoint is 
24c0c 61 63 74 75 61 6c 6c 79 20 61 20 74 72 61 6e 73  actually a trans
24c0d 61 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20  action.      ** 
24c0e 73 61 76 65 70 6f 69 6e 74 20 28 61 6e 64 20 74  savepoint (and t
24c0f 68 65 72 65 66 6f 72 65 20 73 68 6f 75 6c 64 20  herefore should 
24c10 6e 6f 74 20 70 72 6f 6d 70 74 20 78 53 61 76 65  not prompt xSave
24c11 70 6f 69 6e 74 28 29 29 20 63 61 6c 6c 62 61 63  point()) callbac
24c12 6b 73 2e 0a 20 20 20 20 20 20 2a 2a 20 49 66 20  ks..      ** If 
24c13 74 68 69 73 20 69 73 20 61 20 74 72 61 6e 73 61  this is a transa
24c14 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20  ction savepoint 
24c15 62 65 69 6e 67 20 6f 70 65 6e 65 64 2c 20 69 74  being opened, it
24c16 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a 20   is guaranteed. 
24c17 20 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65       ** that the
24c18 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 5d 20 61   db->aVTrans[] a
24c19 72 72 61 79 20 69 73 20 65 6d 70 74 79 2e 20 20  rray is empty.  
24c1a 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
24c1b 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d   db->autoCommit=
24c1c 3d 30 20 7c 7c 20 64 62 2d 3e 6e 56 54 72 61 6e  =0 || db->nVTran
24c1d 73 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 63  s==0 );.      rc
24c1e 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 53 61   = sqlite3VtabSa
24c1f 76 65 70 6f 69 6e 74 28 64 62 2c 20 53 41 56 45  vepoint(db, SAVE
24c20 50 4f 49 4e 54 5f 42 45 47 49 4e 2c 0a 20 20 20  POINT_BEGIN,.   
24c21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24c22 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d               db-
24c23 3e 6e 53 74 61 74 65 6d 65 6e 74 2b 64 62 2d 3e  >nStatement+db->
24c24 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20  nSavepoint);.   
24c25 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
24c26 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72  E_OK ) goto abor
24c27 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
24c28 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f 2a  #endif..      /*
24c29 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 61   Create a new sa
24c2a 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72  vepoint structur
24c2b 65 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 73  e. */.      u.as
24c2c 2e 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44  .pNew = sqlite3D
24c2d 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
24c2e 69 7a 65 6f 66 28 53 61 76 65 70 6f 69 6e 74 29  izeof(Savepoint)
24c2f 2b 75 2e 61 73 2e 6e 4e 61 6d 65 2b 31 29 3b 0a  +u.as.nName+1);.
24c30 20 20 20 20 20 20 69 66 28 20 75 2e 61 73 2e 70        if( u.as.p
24c31 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 75  New ){.        u
24c32 2e 61 73 2e 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20  .as.pNew->zName 
24c33 3d 20 28 63 68 61 72 20 2a 29 26 75 2e 61 73 2e  = (char *)&u.as.
24c34 70 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20  pNew[1];.       
24c35 20 6d 65 6d 63 70 79 28 75 2e 61 73 2e 70 4e 65   memcpy(u.as.pNe
24c36 77 2d 3e 7a 4e 61 6d 65 2c 20 75 2e 61 73 2e 7a  w->zName, u.as.z
24c37 4e 61 6d 65 2c 20 75 2e 61 73 2e 6e 4e 61 6d 65  Name, u.as.nName
24c38 2b 31 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  +1);..        /*
24c39 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
24c3a 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e  open transaction
24c3b 2c 20 74 68 65 6e 20 6d 61 72 6b 20 74 68 69 73  , then mark this
24c3c 20 61 73 20 61 20 73 70 65 63 69 61 6c 0a 20 20   as a special.  
24c3d 20 20 20 20 20 20 2a 2a 20 22 74 72 61 6e 73 61        ** "transa
24c3e 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 22  ction savepoint"
24c3f 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  . */.        if(
24c40 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
24c41 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d  ){.          db-
24c42 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b  >autoCommit = 0;
24c43 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 69  .          db->i
24c44 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65  sTransactionSave
24c45 70 6f 69 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20  point = 1;.     
24c46 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
24c47 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69      db->nSavepoi
24c48 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  nt++;.        }.
24c49 0a 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b  .        /* Link
24c4a 20 74 68 65 20 6e 65 77 20 73 61 76 65 70 6f 69   the new savepoi
24c4b 6e 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  nt into the data
24c4c 62 61 73 65 20 68 61 6e 64 6c 65 27 73 20 6c 69  base handle's li
24c4d 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 75  st. */.        u
24c4e 2e 61 73 2e 70 4e 65 77 2d 3e 70 4e 65 78 74 20  .as.pNew->pNext 
24c4f 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74  = db->pSavepoint
24c50 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53  ;.        db->pS
24c51 61 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 73 2e  avepoint = u.as.
24c52 70 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 75 2e  pNew;.        u.
24c53 61 73 2e 70 4e 65 77 2d 3e 6e 44 65 66 65 72 72  as.pNew->nDeferr
24c54 65 64 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44 65  edCons = db->nDe
24c55 66 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20  ferredCons;.    
24c56 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
24c57 65 7b 0a 20 20 20 20 75 2e 61 73 2e 69 53 61 76  e{.    u.as.iSav
24c58 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 0a 20 20 20  epoint = 0;..   
24c59 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 6e 61 6d   /* Find the nam
24c5a 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 66  ed savepoint. If
24c5b 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63   there is no suc
24c5c 68 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65  h savepoint, the
24c5d 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 61 6e 20 65  n an.    ** an e
24c5e 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
24c5f 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 20 2a   to the user.  *
24c60 2f 0a 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20  /.    for(.     
24c61 20 75 2e 61 73 2e 70 53 61 76 65 70 6f 69 6e 74   u.as.pSavepoint
24c62 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e   = db->pSavepoin
24c63 74 3b 0a 20 20 20 20 20 20 75 2e 61 73 2e 70 53  t;.      u.as.pS
24c64 61 76 65 70 6f 69 6e 74 20 26 26 20 73 71 6c 69  avepoint && sqli
24c65 74 65 33 53 74 72 49 43 6d 70 28 75 2e 61 73 2e  te3StrICmp(u.as.
24c66 70 53 61 76 65 70 6f 69 6e 74 2d 3e 7a 4e 61 6d  pSavepoint->zNam
24c67 65 2c 20 75 2e 61 73 2e 7a 4e 61 6d 65 29 3b 0a  e, u.as.zName);.
24c68 20 20 20 20 20 20 75 2e 61 73 2e 70 53 61 76 65        u.as.pSave
24c69 70 6f 69 6e 74 20 3d 20 75 2e 61 73 2e 70 53 61  point = u.as.pSa
24c6a 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 0a 20  vepoint->pNext. 
24c6b 20 20 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 73     ){.      u.as
24c6c 2e 69 53 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20  .iSavepoint++;. 
24c6d 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 75 2e     }.    if( !u.
24c6e 61 73 2e 70 53 61 76 65 70 6f 69 6e 74 20 29 7b  as.pSavepoint ){
24c6f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65  .      sqlite3Se
24c70 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
24c71 4d 73 67 2c 20 64 62 2c 20 22 6e 6f 20 73 75 63  Msg, db, "no suc
24c72 68 20 73 61 76 65 70 6f 69 6e 74 3a 20 25 73 22  h savepoint: %s"
24c73 2c 20 75 2e 61 73 2e 7a 4e 61 6d 65 29 3b 0a 20  , u.as.zName);. 
24c74 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
24c75 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73  _ERROR;.    }els
24c76 65 20 69 66 28 20 64 62 2d 3e 77 72 69 74 65 56  e if( db->writeV
24c77 64 62 65 43 6e 74 3e 30 20 26 26 20 75 2e 61 73  dbeCnt>0 && u.as
24c78 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52  .p1==SAVEPOINT_R
24c79 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20 20  ELEASE ){.      
24c7a 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73  /* It is not pos
24c7b 73 69 62 6c 65 20 74 6f 20 72 65 6c 65 61 73 65  sible to release
24c7c 20 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 76 65   (commit) a save
24c7d 70 6f 69 6e 74 20 69 66 20 74 68 65 72 65 20 61  point if there a
24c7e 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 69  re.      ** acti
24c7f 76 65 20 77 72 69 74 65 20 73 74 61 74 65 6d 65  ve write stateme
24c80 6e 74 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  nts..      */.  
24c81 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
24c82 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
24c83 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20 22 63  , db,.        "c
24c84 61 6e 6e 6f 74 20 72 65 6c 65 61 73 65 20 73 61  annot release sa
24c85 76 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20 73 74  vepoint - SQL st
24c86 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67  atements in prog
24c87 72 65 73 73 22 0a 20 20 20 20 20 20 29 3b 0a 20  ress".      );. 
24c88 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
24c89 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65  _BUSY;.    }else
24c8a 7b 0a 0a 20 20 20 20 20 20 2f 2a 20 44 65 74 65  {..      /* Dete
24c8b 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72  rmine whether or
24c8c 20 6e 6f 74 20 74 68 69 73 20 69 73 20 61 20 74   not this is a t
24c8d 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70  ransaction savep
24c8e 6f 69 6e 74 2e 20 49 66 20 73 6f 2c 0a 20 20 20  oint. If so,.   
24c8f 20 20 20 2a 2a 20 61 6e 64 20 74 68 69 73 20 69     ** and this i
24c90 73 20 61 20 52 45 4c 45 41 53 45 20 63 6f 6d 6d  s a RELEASE comm
24c91 61 6e 64 2c 20 74 68 65 6e 20 74 68 65 20 63 75  and, then the cu
24c92 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  rrent transactio
24c93 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 63 6f  n.      ** is co
24c94 6d 6d 69 74 74 65 64 2e 0a 20 20 20 20 20 20 2a  mmitted..      *
24c95 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 73 54 72  /.      int isTr
24c96 61 6e 73 61 63 74 69 6f 6e 20 3d 20 75 2e 61 73  ansaction = u.as
24c97 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65  .pSavepoint->pNe
24c98 78 74 3d 3d 30 20 26 26 20 64 62 2d 3e 69 73 54  xt==0 && db->isT
24c99 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f  ransactionSavepo
24c9a 69 6e 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69  int;.      if( i
24c9b 73 54 72 61 6e 73 61 63 74 69 6f 6e 20 26 26 20  sTransaction && 
24c9c 75 2e 61 73 2e 70 31 3d 3d 53 41 56 45 50 4f 49  u.as.p1==SAVEPOI
24c9d 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20  NT_RELEASE ){.  
24c9e 20 20 20 20 20 20 69 66 28 20 28 72 63 20 3d 20        if( (rc = 
24c9f 73 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b  sqlite3VdbeCheck
24ca0 46 6b 28 70 2c 20 31 29 29 21 3d 53 51 4c 49 54  Fk(p, 1))!=SQLIT
24ca1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
24ca2 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75    goto vdbe_retu
24ca3 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rn;.        }.  
24ca4 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f        db->autoCo
24ca5 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  mmit = 1;.      
24ca6 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
24ca7 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45  eHalt(p)==SQLITE
24ca8 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20  _BUSY ){.       
24ca9 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20     p->pc = pc;. 
24caa 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74           db->aut
24cab 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20  oCommit = 0;.   
24cac 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72         p->rc = r
24cad 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
24cae 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
24caf 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20  vdbe_return;.   
24cb0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64       }.        d
24cb1 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e  b->isTransaction
24cb2 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20  Savepoint = 0;. 
24cb3 20 20 20 20 20 20 20 72 63 20 3d 20 70 2d 3e 72         rc = p->r
24cb4 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  c;.      }else{.
24cb5 20 20 20 20 20 20 20 20 75 2e 61 73 2e 69 53 61          u.as.iSa
24cb6 76 65 70 6f 69 6e 74 20 3d 20 64 62 2d 3e 6e 53  vepoint = db->nS
24cb7 61 76 65 70 6f 69 6e 74 20 2d 20 75 2e 61 73 2e  avepoint - u.as.
24cb8 69 53 61 76 65 70 6f 69 6e 74 20 2d 20 31 3b 0a  iSavepoint - 1;.
24cb9 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 73          if( u.as
24cba 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52  .p1==SAVEPOINT_R
24cbb 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 20  OLLBACK ){.     
24cbc 20 20 20 20 20 66 6f 72 28 75 2e 61 73 2e 69 69       for(u.as.ii
24cbd 3d 30 3b 20 75 2e 61 73 2e 69 69 3c 64 62 2d 3e  =0; u.as.ii<db->
24cbe 6e 44 62 3b 20 75 2e 61 73 2e 69 69 2b 2b 29 7b  nDb; u.as.ii++){
24cbf 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
24cc0 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c  ite3BtreeTripAll
24cc1 43 75 72 73 6f 72 73 28 64 62 2d 3e 61 44 62 5b  Cursors(db->aDb[
24cc2 75 2e 61 73 2e 69 69 5d 2e 70 42 74 2c 20 53 51  u.as.ii].pBt, SQ
24cc3 4c 49 54 45 5f 41 42 4f 52 54 29 3b 0a 20 20 20  LITE_ABORT);.   
24cc4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
24cc5 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 75   }.        for(u
24cc6 2e 61 73 2e 69 69 3d 30 3b 20 75 2e 61 73 2e 69  .as.ii=0; u.as.i
24cc7 69 3c 64 62 2d 3e 6e 44 62 3b 20 75 2e 61 73 2e  i<db->nDb; u.as.
24cc8 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  ii++){.         
24cc9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
24cca 65 65 53 61 76 65 70 6f 69 6e 74 28 64 62 2d 3e  eeSavepoint(db->
24ccb 61 44 62 5b 75 2e 61 73 2e 69 69 5d 2e 70 42 74  aDb[u.as.ii].pBt
24ccc 2c 20 75 2e 61 73 2e 70 31 2c 20 75 2e 61 73 2e  , u.as.p1, u.as.
24ccd 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20  iSavepoint);.   
24cce 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
24ccf 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
24cd0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f          goto abo
24cd1 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
24cd2 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
24cd3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
24cd4 66 28 20 75 2e 61 73 2e 70 31 3d 3d 53 41 56 45  f( u.as.p1==SAVE
24cd5 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26  POINT_ROLLBACK &
24cd6 26 20 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  & (db->flags&SQL
24cd7 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
24cd8 73 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  s)!=0 ){.       
24cd9 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
24cda 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
24cdb 74 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  ts(db);.        
24cdc 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 41 6c    sqlite3ResetAl
24cdd 6c 53 63 68 65 6d 61 73 4f 66 43 6f 6e 6e 65 63  lSchemasOfConnec
24cde 74 69 6f 6e 28 64 62 29 3b 0a 20 20 20 20 20 20  tion(db);.      
24cdf 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 3d 20      db->flags = 
24ce0 28 64 62 2d 3e 66 6c 61 67 73 20 7c 20 53 51 4c  (db->flags | SQL
24ce1 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
24ce2 73 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  s);.        }.  
24ce3 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
24ce4 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68  Regardless of wh
24ce5 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 20  ether this is a 
24ce6 52 45 4c 45 41 53 45 20 6f 72 20 52 4f 4c 4c 42  RELEASE or ROLLB
24ce7 41 43 4b 2c 20 64 65 73 74 72 6f 79 20 61 6c 6c  ACK, destroy all
24ce8 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 70 6f  .      ** savepo
24ce9 69 6e 74 73 20 6e 65 73 74 65 64 20 69 6e 73 69  ints nested insi
24cea 64 65 20 6f 66 20 74 68 65 20 73 61 76 65 70 6f  de of the savepo
24ceb 69 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61 74  int being operat
24cec 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20  ed on. */.      
24ced 77 68 69 6c 65 28 20 64 62 2d 3e 70 53 61 76 65  while( db->pSave
24cee 70 6f 69 6e 74 21 3d 75 2e 61 73 2e 70 53 61 76  point!=u.as.pSav
24cef 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20  epoint ){.      
24cf0 20 20 75 2e 61 73 2e 70 54 6d 70 20 3d 20 64 62    u.as.pTmp = db
24cf1 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20  ->pSavepoint;.  
24cf2 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70        db->pSavep
24cf3 6f 69 6e 74 20 3d 20 75 2e 61 73 2e 70 54 6d 70  oint = u.as.pTmp
24cf4 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20  ->pNext;.       
24cf5 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
24cf6 62 2c 20 75 2e 61 73 2e 70 54 6d 70 29 3b 0a 20  b, u.as.pTmp);. 
24cf7 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65         db->nSave
24cf8 70 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 7d  point--;.      }
24cf9 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 69 74  ..      /* If it
24cfa 20 69 73 20 61 20 52 45 4c 45 41 53 45 2c 20 74   is a RELEASE, t
24cfb 68 65 6e 20 64 65 73 74 72 6f 79 20 74 68 65 20  hen destroy the 
24cfc 73 61 76 65 70 6f 69 6e 74 20 62 65 69 6e 67 20  savepoint being 
24cfd 6f 70 65 72 61 74 65 64 20 6f 6e 0a 20 20 20 20  operated on.    
24cfe 20 20 2a 2a 20 74 6f 6f 2e 20 49 66 20 69 74 20    ** too. If it 
24cff 69 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20 54 4f  is a ROLLBACK TO
24d00 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 6e  , then set the n
24d01 75 6d 62 65 72 20 6f 66 20 64 65 66 65 72 72 65  umber of deferre
24d02 64 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74  d.      ** const
24d03 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 73  raint violations
24d04 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20   present in the 
24d05 64 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 20  database to the 
24d06 76 61 6c 75 65 20 73 74 6f 72 65 64 0a 20 20 20  value stored.   
24d07 20 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 73     ** when the s
24d08 61 76 65 70 6f 69 6e 74 20 77 61 73 20 63 72 65  avepoint was cre
24d09 61 74 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20  ated.  */.      
24d0a 69 66 28 20 75 2e 61 73 2e 70 31 3d 3d 53 41 56  if( u.as.p1==SAV
24d0b 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29  EPOINT_RELEASE )
24d0c 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
24d0d 28 20 75 2e 61 73 2e 70 53 61 76 65 70 6f 69 6e  ( u.as.pSavepoin
24d0e 74 3d 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e  t==db->pSavepoin
24d0f 74 20 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d  t );.        db-
24d10 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 75 2e  >pSavepoint = u.
24d11 61 73 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e 70  as.pSavepoint->p
24d12 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71  Next;.        sq
24d13 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
24d14 75 2e 61 73 2e 70 53 61 76 65 70 6f 69 6e 74 29  u.as.pSavepoint)
24d15 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 69  ;.        if( !i
24d16 73 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a  sTransaction ){.
24d17 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 53            db->nS
24d18 61 76 65 70 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20  avepoint--;.    
24d19 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
24d1a 65 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6e  e{.        db->n
24d1b 44 65 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 75  DeferredCons = u
24d1c 2e 61 73 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e  .as.pSavepoint->
24d1d 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 0a 20  nDeferredCons;. 
24d1e 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66       }..      if
24d1f 28 20 21 69 73 54 72 61 6e 73 61 63 74 69 6f 6e  ( !isTransaction
24d20 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
24d21 20 73 71 6c 69 74 65 33 56 74 61 62 53 61 76 65   sqlite3VtabSave
24d22 70 6f 69 6e 74 28 64 62 2c 20 75 2e 61 73 2e 70  point(db, u.as.p
24d23 31 2c 20 75 2e 61 73 2e 69 53 61 76 65 70 6f 69  1, u.as.iSavepoi
24d24 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  nt);.        if(
24d25 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
24d26 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
24d27 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to_error;.      
24d28 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62  }.    }.  }..  b
24d29 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
24d2a 64 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50  de: AutoCommit P
24d2b 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
24d2c 20 53 65 74 20 74 68 65 20 64 61 74 61 62 61 73   Set the databas
24d2d 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c  e auto-commit fl
24d2e 61 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20 30  ag to P1 (1 or 0
24d2f 29 2e 20 49 66 20 50 32 20 69 73 20 74 72 75 65  ). If P2 is true
24d30 2c 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61  , roll.** back a
24d31 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74  ny currently act
24d32 69 76 65 20 62 74 72 65 65 20 74 72 61 6e 73 61  ive btree transa
24d33 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 65  ctions. If there
24d34 20 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 0a   are any active.
24d35 2a 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66 72  ** VMs (apart fr
24d36 6f 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 68  om this one), th
24d37 65 6e 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66 61  en a ROLLBACK fa
24d38 69 6c 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20 66  ils.  A COMMIT f
24d39 61 69 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72 65  ails if.** there
24d3a 20 61 72 65 20 61 63 74 69 76 65 20 77 72 69 74   are active writ
24d3b 69 6e 67 20 56 4d 73 20 6f 72 20 61 63 74 69 76  ing VMs or activ
24d3c 65 20 56 4d 73 20 74 68 61 74 20 75 73 65 20 73  e VMs that use s
24d3d 68 61 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a 0a  hared cache..**.
24d3e 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  ** This instruct
24d3f 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 56  ion causes the V
24d40 4d 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61  M to halt..*/.ca
24d41 73 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74  se OP_AutoCommit
24d42 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  : {.#if 0  /* lo
24d43 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
24d44 76 65 64 20 69 6e 74 6f 20 75 2e 61 74 20 2a 2f  ved into u.at */
24d45 0a 20 20 69 6e 74 20 64 65 73 69 72 65 64 41 75  .  int desiredAu
24d46 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 69 6e 74 20  toCommit;.  int 
24d47 69 52 6f 6c 6c 62 61 63 6b 3b 0a 20 20 69 6e 74  iRollback;.  int
24d48 20 74 75 72 6e 4f 6e 41 43 3b 0a 23 65 6e 64 69   turnOnAC;.#endi
24d49 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
24d4a 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
24d4b 75 2e 61 74 20 2a 2f 0a 0a 20 20 75 2e 61 74 2e  u.at */..  u.at.
24d4c 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69  desiredAutoCommi
24d4d 74 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75  t = pOp->p1;.  u
24d4e 2e 61 74 2e 69 52 6f 6c 6c 62 61 63 6b 20 3d 20  .at.iRollback = 
24d4f 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 74 2e  pOp->p2;.  u.at.
24d50 74 75 72 6e 4f 6e 41 43 20 3d 20 75 2e 61 74 2e  turnOnAC = u.at.
24d51 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69  desiredAutoCommi
24d52 74 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 6f  t && !db->autoCo
24d53 6d 6d 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20  mmit;.  assert( 
24d54 75 2e 61 74 2e 64 65 73 69 72 65 64 41 75 74 6f  u.at.desiredAuto
24d55 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 2e 61  Commit==1 || u.a
24d56 74 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  t.desiredAutoCom
24d57 6d 69 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  mit==0 );.  asse
24d58 72 74 28 20 75 2e 61 74 2e 64 65 73 69 72 65 64  rt( u.at.desired
24d59 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c  AutoCommit==1 ||
24d5a 20 75 2e 61 74 2e 69 52 6f 6c 6c 62 61 63 6b 3d   u.at.iRollback=
24d5b 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
24d5c 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
24d5d 74 3e 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c 65  t>0 );  /* At le
24d5e 61 73 74 20 74 68 69 73 20 6f 6e 65 20 56 4d 20  ast this one VM 
24d5f 69 73 20 61 63 74 69 76 65 20 2a 2f 0a 0a 23 69  is active */..#i
24d60 66 20 30 0a 20 20 69 66 28 20 75 2e 61 74 2e 74  f 0.  if( u.at.t
24d61 75 72 6e 4f 6e 41 43 20 26 26 20 75 2e 61 74 2e  urnOnAC && u.at.
24d62 69 52 6f 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d  iRollback && db-
24d63 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31  >activeVdbeCnt>1
24d64 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
24d65 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  is instruction i
24d66 6d 70 6c 65 6d 65 6e 74 73 20 61 20 52 4f 4c 4c  mplements a ROLL
24d67 42 41 43 4b 20 61 6e 64 20 6f 74 68 65 72 20 56  BACK and other V
24d68 4d 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 73 74  Ms are.    ** st
24d69 69 6c 6c 20 72 75 6e 6e 69 6e 67 2c 20 61 6e 64  ill running, and
24d6a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
24d6b 73 20 61 63 74 69 76 65 2c 20 72 65 74 75 72 6e  s active, return
24d6c 20 61 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61   an error indica
24d6d 74 69 6e 67 0a 20 20 20 20 2a 2a 20 74 68 61 74  ting.    ** that
24d6e 20 74 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d   the other VMs m
24d6f 75 73 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72  ust complete fir
24d70 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  st..    */.    s
24d71 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
24d72 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
24d73 20 22 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63   "cannot rollbac
24d74 6b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20  k transaction - 
24d75 22 0a 20 20 20 20 20 20 20 20 22 53 51 4c 20 73  ".        "SQL s
24d76 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f  tatements in pro
24d77 67 72 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20  gress");.    rc 
24d78 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
24d79 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
24d7a 69 66 28 20 75 2e 61 74 2e 74 75 72 6e 4f 6e 41  if( u.at.turnOnA
24d7b 43 20 26 26 20 21 75 2e 61 74 2e 69 52 6f 6c 6c  C && !u.at.iRoll
24d7c 62 61 63 6b 20 26 26 20 64 62 2d 3e 77 72 69 74  back && db->writ
24d7d 65 56 64 62 65 43 6e 74 3e 30 20 29 7b 0a 20 20  eVdbeCnt>0 ){.  
24d7e 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73    /* If this ins
24d7f 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65  truction impleme
24d80 6e 74 73 20 61 20 43 4f 4d 4d 49 54 20 61 6e 64  nts a COMMIT and
24d81 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 20 77   other VMs are w
24d82 72 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65  riting.    ** re
24d83 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e  turn an error in
24d84 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 74 68  dicating that th
24d85 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74  e other VMs must
24d86 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e   complete first.
24d87 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
24d88 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
24d89 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63  >zErrMsg, db, "c
24d8a 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 74 72 61  annot commit tra
24d8b 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20  nsaction - ".   
24d8c 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d       "SQL statem
24d8d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73  ents in progress
24d8e 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ");.    rc = SQL
24d8f 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73  ITE_BUSY;.  }els
24d90 65 20 69 66 28 20 75 2e 61 74 2e 64 65 73 69 72  e if( u.at.desir
24d91 65 64 41 75 74 6f 43 6f 6d 6d 69 74 21 3d 64 62  edAutoCommit!=db
24d92 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
24d93 20 20 20 20 69 66 28 20 75 2e 61 74 2e 69 52 6f      if( u.at.iRo
24d94 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20  llback ){.      
24d95 61 73 73 65 72 74 28 20 75 2e 61 74 2e 64 65 73  assert( u.at.des
24d96 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d  iredAutoCommit==
24d97 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  1 );.      sqlit
24d98 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62  e3RollbackAll(db
24d99 2c 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 5f 52  , SQLITE_ABORT_R
24d9a 4f 4c 4c 42 41 43 4b 29 3b 0a 20 20 20 20 20 20  OLLBACK);.      
24d9b 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d  db->autoCommit =
24d9c 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   1;.    }else if
24d9d 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56  ( (rc = sqlite3V
24d9e 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 20 31 29  dbeCheckFk(p, 1)
24d9f 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  )!=SQLITE_OK ){.
24da0 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f        goto vdbe_
24da1 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 65 6c 73  return;.    }els
24da2 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74  e{.      db->aut
24da3 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 75 2e  oCommit = (u8)u.
24da4 61 74 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f  at.desiredAutoCo
24da5 6d 6d 69 74 3b 0a 20 20 20 20 20 20 69 66 28 20  mmit;.      if( 
24da6 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28  sqlite3VdbeHalt(
24da7 70 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  p)==SQLITE_BUSY 
24da8 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 63  ){.        p->pc
24da9 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 20 20 64   = pc;.        d
24daa 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20  b->autoCommit = 
24dab 28 75 38 29 28 31 2d 75 2e 61 74 2e 64 65 73 69  (u8)(1-u.at.desi
24dac 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 29 3b 0a  redAutoCommit);.
24dad 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20          p->rc = 
24dae 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
24daf 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76  ;.        goto v
24db0 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20  dbe_return;.    
24db1 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73    }.    }.    as
24db2 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65  sert( db->nState
24db3 6d 65 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73  ment==0 );.    s
24db4 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70  qlite3CloseSavep
24db5 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 69  oints(db);.    i
24db6 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  f( p->rc==SQLITE
24db7 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
24db8 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  = SQLITE_DONE;. 
24db9 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
24dba 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
24dbb 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74  R;.    }.    got
24dbc 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20  o vdbe_return;. 
24dbd 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
24dbe 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
24dbf 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 0a 20 20  >zErrMsg, db,.  
24dc0 20 20 20 20 20 20 28 21 75 2e 61 74 2e 64 65 73        (!u.at.des
24dc1 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 29 3f  iredAutoCommit)?
24dc2 22 63 61 6e 6e 6f 74 20 73 74 61 72 74 20 61 20  "cannot start a 
24dc3 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 74 68  transaction with
24dc4 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
24dc5 22 3a 28 0a 20 20 20 20 20 20 20 20 28 75 2e 61  ":(.        (u.a
24dc6 74 2e 69 52 6f 6c 6c 62 61 63 6b 29 3f 22 63 61  t.iRollback)?"ca
24dc7 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 2d 20  nnot rollback - 
24dc8 6e 6f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  no transaction i
24dc9 73 20 61 63 74 69 76 65 22 3a 0a 20 20 20 20 20  s active":.     
24dca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63                "c
24dcb 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 2d 20 6e  annot commit - n
24dcc 6f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  o transaction is
24dcd 20 61 63 74 69 76 65 22 29 29 3b 0a 0a 20 20 20   active"));..   
24dce 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
24dcf 4f 52 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  OR;.  }.  break;
24dd0 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54  .}../* Opcode: T
24dd1 72 61 6e 73 61 63 74 69 6f 6e 20 50 31 20 50 32  ransaction P1 P2
24dd2 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67   * * *.**.** Beg
24dd3 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
24dd4 2e 20 20 54 68 65 20 74 72 61 6e 73 61 63 74 69  .  The transacti
24dd5 6f 6e 20 65 6e 64 73 20 77 68 65 6e 20 61 20 43  on ends when a C
24dd6 6f 6d 6d 69 74 20 6f 72 20 52 6f 6c 6c 62 61 63  ommit or Rollbac
24dd7 6b 0a 2a 2a 20 6f 70 63 6f 64 65 20 69 73 20 65  k.** opcode is e
24dd8 6e 63 6f 75 6e 74 65 72 65 64 2e 20 20 44 65 70  ncountered.  Dep
24dd9 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 4f 4e  ending on the ON
24dda 20 43 4f 4e 46 4c 49 43 54 20 73 65 74 74 69 6e   CONFLICT settin
24ddb 67 2c 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61  g, the.** transa
24ddc 63 74 69 6f 6e 20 6d 69 67 68 74 20 61 6c 73 6f  ction might also
24ddd 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20   be rolled back 
24dde 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 65  if an error is e
24ddf 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a  ncountered..**.*
24de0 2a 20 50 31 20 69 73 20 74 68 65 20 69 6e 64 65  * P1 is the inde
24de1 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  x of the databas
24de2 65 20 66 69 6c 65 20 6f 6e 20 77 68 69 63 68 20  e file on which 
24de3 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
24de4 69 73 0a 2a 2a 20 73 74 61 72 74 65 64 2e 20 20  is.** started.  
24de5 49 6e 64 65 78 20 30 20 69 73 20 74 68 65 20 6d  Index 0 is the m
24de6 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
24de7 65 20 61 6e 64 20 69 6e 64 65 78 20 31 20 69 73  e and index 1 is
24de8 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 75 73 65   the.** file use
24de9 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20  d for temporary 
24dea 74 61 62 6c 65 73 2e 20 20 49 6e 64 69 63 65 73  tables.  Indices
24deb 20 6f 66 20 32 20 6f 72 20 6d 6f 72 65 20 61 72   of 2 or more ar
24dec 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 61 74  e used for.** at
24ded 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
24dee 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73  ..**.** If P2 is
24def 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
24df0 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  a write-transact
24df1 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20  ion is started. 
24df2 20 41 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   A RESERVED lock
24df3 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20   is.** obtained 
24df4 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
24df5 66 69 6c 65 20 77 68 65 6e 20 61 20 77 72 69 74  file when a writ
24df6 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  e-transaction is
24df7 20 73 74 61 72 74 65 64 2e 20 20 4e 6f 0a 2a 2a   started.  No.**
24df8 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 63   other process c
24df9 61 6e 20 73 74 61 72 74 20 61 6e 6f 74 68 65 72  an start another
24dfa 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
24dfb 6f 6e 20 77 68 69 6c 65 20 74 68 69 73 20 74 72  on while this tr
24dfc 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20  ansaction is.** 
24dfd 75 6e 64 65 72 77 61 79 2e 20 20 53 74 61 72 74  underway.  Start
24dfe 69 6e 67 20 61 20 77 72 69 74 65 20 74 72 61 6e  ing a write tran
24dff 73 61 63 74 69 6f 6e 20 61 6c 73 6f 20 63 72 65  saction also cre
24e00 61 74 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20  ates a rollback 
24e01 6a 6f 75 72 6e 61 6c 2e 20 41 0a 2a 2a 20 77 72  journal. A.** wr
24e02 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite transaction 
24e03 6d 75 73 74 20 62 65 20 73 74 61 72 74 65 64 20  must be started 
24e04 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 6e 67  before any chang
24e05 65 73 20 63 61 6e 20 62 65 20 6d 61 64 65 20 74  es can be made t
24e06 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  o the.** databas
24e07 65 2e 20 20 49 66 20 50 32 20 69 73 20 32 20 6f  e.  If P2 is 2 o
24e08 72 20 67 72 65 61 74 65 72 20 74 68 65 6e 20 61  r greater then a
24e09 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
24e0a 20 69 73 20 61 6c 73 6f 20 6f 62 74 61 69 6e 65   is also obtaine
24e0b 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 66 69 6c 65  d.** on the file
24e0c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 77 72 69  ..**.** If a wri
24e0d 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  te-transaction i
24e0e 73 20 73 74 61 72 74 65 64 20 61 6e 64 20 74 68  s started and th
24e0f 65 20 56 64 62 65 2e 75 73 65 73 53 74 6d 74 4a  e Vdbe.usesStmtJ
24e10 6f 75 72 6e 61 6c 20 66 6c 61 67 20 69 73 0a 2a  ournal flag is.*
24e11 2a 20 74 72 75 65 20 28 74 68 69 73 20 66 6c 61  * true (this fla
24e12 67 20 69 73 20 73 65 74 20 69 66 20 74 68 65 20  g is set if the 
24e13 56 64 62 65 20 6d 61 79 20 6d 6f 64 69 66 79 20  Vdbe may modify 
24e14 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 72 6f  more than one ro
24e15 77 20 61 6e 64 20 6d 61 79 0a 2a 2a 20 74 68 72  w and may.** thr
24e16 6f 77 20 61 6e 20 41 42 4f 52 54 20 65 78 63 65  ow an ABORT exce
24e17 70 74 69 6f 6e 29 2c 20 61 20 73 74 61 74 65 6d  ption), a statem
24e18 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
24e19 6d 61 79 20 61 6c 73 6f 20 62 65 20 6f 70 65 6e  may also be open
24e1a 65 64 2e 0a 2a 2a 20 4d 6f 72 65 20 73 70 65 63  ed..** More spec
24e1b 69 66 69 63 61 6c 6c 79 2c 20 61 20 73 74 61 74  ifically, a stat
24e1c 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
24e1d 6e 20 69 73 20 6f 70 65 6e 65 64 20 69 66 66 20  n is opened iff 
24e1e 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
24e1f 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 75  connection is cu
24e20 72 72 65 6e 74 6c 79 20 6e 6f 74 20 69 6e 20 61  rrently not in a
24e21 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20  utocommit mode, 
24e22 6f 72 20 69 66 20 74 68 65 72 65 20 61 72 65 20  or if there are 
24e23 6f 74 68 65 72 0a 2a 2a 20 61 63 74 69 76 65 20  other.** active 
24e24 73 74 61 74 65 6d 65 6e 74 73 2e 20 41 20 73 74  statements. A st
24e25 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
24e26 69 6f 6e 20 61 6c 6c 6f 77 73 20 74 68 65 20 63  ion allows the c
24e27 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74  hanges made by t
24e28 68 69 73 0a 2a 2a 20 56 44 42 45 20 74 6f 20 62  his.** VDBE to b
24e29 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66  e rolled back af
24e2a 74 65 72 20 61 6e 20 65 72 72 6f 72 20 77 69 74  ter an error wit
24e2b 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 72  hout having to r
24e2c 6f 6c 6c 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20  oll back the.** 
24e2d 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69  entire transacti
24e2e 6f 6e 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 20  on. If no error 
24e2f 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20  is encountered, 
24e30 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72  the statement tr
24e31 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c  ansaction.** wil
24e32 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  l automatically 
24e33 63 6f 6d 6d 69 74 20 77 68 65 6e 20 74 68 65 20  commit when the 
24e34 56 44 42 45 20 68 61 6c 74 73 2e 0a 2a 2a 0a 2a  VDBE halts..**.*
24e35 2a 20 49 66 20 50 32 20 69 73 20 7a 65 72 6f 2c  * If P2 is zero,
24e36 20 74 68 65 6e 20 61 20 72 65 61 64 2d 6c 6f 63   then a read-loc
24e37 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e  k is obtained on
24e38 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
24e39 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54  le..*/.case OP_T
24e3a 72 61 6e 73 61 63 74 69 6f 6e 3a 20 7b 0a 23 69  ransaction: {.#i
24e3b 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
24e3c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
24e3d 74 6f 20 75 2e 61 75 20 2a 2f 0a 20 20 42 74 72  to u.au */.  Btr
24e3e 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 20  ee *pBt;.#endif 
24e3f 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
24e40 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
24e41 61 75 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  au */..  assert(
24e42 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
24e43 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29  Op->p1<db->nDb )
24e44 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  ;.  assert( (p->
24e45 62 74 72 65 65 4d 61 73 6b 20 26 20 28 28 28 79  btreeMask & (((y
24e46 44 62 4d 61 73 6b 29 31 29 3c 3c 70 4f 70 2d 3e  DbMask)1)<<pOp->
24e47 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e 61  p1))!=0 );.  u.a
24e48 75 2e 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b  u.pBt = db->aDb[
24e49 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 0a 20  pOp->p1].pBt;.. 
24e4a 20 69 66 28 20 75 2e 61 75 2e 70 42 74 20 29 7b   if( u.au.pBt ){
24e4b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
24e4c 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73  3BtreeBeginTrans
24e4d 28 75 2e 61 75 2e 70 42 74 2c 20 70 4f 70 2d 3e  (u.au.pBt, pOp->
24e4e 70 32 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  p2);.    if( rc=
24e4f 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a  =SQLITE_BUSY ){.
24e50 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63        p->pc = pc
24e51 3b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20  ;.      p->rc = 
24e52 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
24e53 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62  ;.      goto vdb
24e54 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a  e_return;.    }.
24e55 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
24e56 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67  TE_OK ){.      g
24e57 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
24e58 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 0a 20  _error;.    }.. 
24e59 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26     if( pOp->p2 &
24e5a 26 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75  & p->usesStmtJou
24e5b 72 6e 61 6c 0a 20 20 20 20 20 26 26 20 28 64 62  rnal.     && (db
24e5c 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20  ->autoCommit==0 
24e5d 7c 7c 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62  || db->activeVdb
24e5e 65 43 6e 74 3e 31 29 0a 20 20 20 20 29 7b 0a 20  eCnt>1).    ){. 
24e5f 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c       assert( sql
24e60 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61  ite3BtreeIsInTra
24e61 6e 73 28 75 2e 61 75 2e 70 42 74 29 20 29 3b 0a  ns(u.au.pBt) );.
24e62 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 53 74        if( p->iSt
24e63 61 74 65 6d 65 6e 74 3d 3d 30 20 29 7b 0a 20 20  atement==0 ){.  
24e64 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62        assert( db
24e65 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3e 3d 30 20  ->nStatement>=0 
24e66 26 26 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e  && db->nSavepoin
24e67 74 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  t>=0 );.        
24e68 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2b 2b  db->nStatement++
24e69 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 53 74  ;.        p->iSt
24e6a 61 74 65 6d 65 6e 74 20 3d 20 64 62 2d 3e 6e 53  atement = db->nS
24e6b 61 76 65 70 6f 69 6e 74 20 2b 20 64 62 2d 3e 6e  avepoint + db->n
24e6c 53 74 61 74 65 6d 65 6e 74 3b 0a 20 20 20 20 20  Statement;.     
24e6d 20 7d 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 73   }..      rc = s
24e6e 71 6c 69 74 65 33 56 74 61 62 53 61 76 65 70 6f  qlite3VtabSavepo
24e6f 69 6e 74 28 64 62 2c 20 53 41 56 45 50 4f 49 4e  int(db, SAVEPOIN
24e70 54 5f 42 45 47 49 4e 2c 20 70 2d 3e 69 53 74 61  T_BEGIN, p->iSta
24e71 74 65 6d 65 6e 74 2d 31 29 3b 0a 20 20 20 20 20  tement-1);.     
24e72 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
24e73 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
24e74 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42   = sqlite3BtreeB
24e75 65 67 69 6e 53 74 6d 74 28 75 2e 61 75 2e 70 42  eginStmt(u.au.pB
24e76 74 2c 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74  t, p->iStatement
24e77 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
24e78 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 63    /* Store the c
24e79 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20  urrent value of 
24e7a 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
24e7b 64 6c 65 73 20 64 65 66 65 72 72 65 64 20 63 6f  dles deferred co
24e7c 6e 73 74 72 61 69 6e 74 0a 20 20 20 20 20 20 2a  nstraint.      *
24e7d 2a 20 63 6f 75 6e 74 65 72 2e 20 49 66 20 74 68  * counter. If th
24e7e 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  e statement tran
24e7f 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f  saction needs to
24e80 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c   be rolled back,
24e81 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 76 61  .      ** the va
24e82 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 75 6e  lue of this coun
24e83 74 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20  ter needs to be 
24e84 72 65 73 74 6f 72 65 64 20 74 6f 6f 2e 20 20 2a  restored too.  *
24e85 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 53 74 6d 74  /.      p->nStmt
24e86 44 65 66 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44  DefCons = db->nD
24e87 65 66 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20  eferredCons;.   
24e88 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a   }.  }.  break;.
24e89 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65  }../* Opcode: Re
24e8a 61 64 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50  adCookie P1 P2 P
24e8b 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64  3 * *.**.** Read
24e8c 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50   cookie number P
24e8d 33 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20  3 from database 
24e8e 50 31 20 61 6e 64 20 77 72 69 74 65 20 69 74 20  P1 and write it 
24e8f 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32  into register P2
24e90 2e 0a 2a 2a 20 50 33 3d 3d 31 20 69 73 20 74 68  ..** P3==1 is th
24e91 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e  e schema version
24e92 2e 20 20 50 33 3d 3d 32 20 69 73 20 74 68 65 20  .  P3==2 is the 
24e93 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e  database format.
24e94 0a 2a 2a 20 50 33 3d 3d 33 20 69 73 20 74 68 65  .** P3==3 is the
24e95 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67   recommended pag
24e96 65 72 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61  er cache size, a
24e97 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31  nd so forth.  P1
24e98 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61  ==0 is.** the ma
24e99 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
24e9a 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68   and P1==1 is th
24e9b 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
24e9c 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a  used to store.**
24e9d 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
24e9e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d  s..**.** There m
24e9f 75 73 74 20 62 65 20 61 20 72 65 61 64 2d 6c 6f  ust be a read-lo
24ea0 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
24ea1 73 65 20 28 65 69 74 68 65 72 20 61 20 74 72 61  se (either a tra
24ea2 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74  nsaction.** must
24ea3 20 62 65 20 73 74 61 72 74 65 64 20 6f 72 20 74   be started or t
24ea4 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20  here must be an 
24ea5 6f 70 65 6e 20 63 75 72 73 6f 72 29 20 62 65 66  open cursor) bef
24ea6 6f 72 65 0a 2a 2a 20 65 78 65 63 75 74 69 6e 67  ore.** executing
24ea7 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
24ea8 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65  n..*/.case OP_Re
24ea9 61 64 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20  adCookie: {     
24eaa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74            /* out
24eab 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a  2-prerelease */.
24eac 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
24ead 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
24eae 69 6e 74 6f 20 75 2e 61 76 20 2a 2f 0a 20 20 69  into u.av */.  i
24eaf 6e 74 20 69 4d 65 74 61 3b 0a 20 20 69 6e 74 20  nt iMeta;.  int 
24eb0 69 44 62 3b 0a 20 20 69 6e 74 20 69 43 6f 6f 6b  iDb;.  int iCook
24eb1 69 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  ie;.#endif /* lo
24eb2 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
24eb3 76 65 64 20 69 6e 74 6f 20 75 2e 61 76 20 2a 2f  ved into u.av */
24eb4 0a 0a 20 20 75 2e 61 76 2e 69 44 62 20 3d 20 70  ..  u.av.iDb = p
24eb5 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 76 2e 69  Op->p1;.  u.av.i
24eb6 43 6f 6f 6b 69 65 20 3d 20 70 4f 70 2d 3e 70 33  Cookie = pOp->p3
24eb7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
24eb8 3e 70 33 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52  >p3<SQLITE_N_BTR
24eb9 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73  EE_META );.  ass
24eba 65 72 74 28 20 75 2e 61 76 2e 69 44 62 3e 3d 30  ert( u.av.iDb>=0
24ebb 20 26 26 20 75 2e 61 76 2e 69 44 62 3c 64 62 2d   && u.av.iDb<db-
24ebc 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
24ebd 28 20 64 62 2d 3e 61 44 62 5b 75 2e 61 76 2e 69  ( db->aDb[u.av.i
24ebe 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20  Db].pBt!=0 );.  
24ebf 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65  assert( (p->btre
24ec0 65 4d 61 73 6b 20 26 20 28 28 28 79 44 62 4d 61  eMask & (((yDbMa
24ec1 73 6b 29 31 29 3c 3c 75 2e 61 76 2e 69 44 62 29  sk)1)<<u.av.iDb)
24ec2 29 21 3d 30 20 29 3b 0a 0a 20 20 73 71 6c 69 74  )!=0 );..  sqlit
24ec3 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 64  e3BtreeGetMeta(d
24ec4 62 2d 3e 61 44 62 5b 75 2e 61 76 2e 69 44 62 5d  b->aDb[u.av.iDb]
24ec5 2e 70 42 74 2c 20 75 2e 61 76 2e 69 43 6f 6f 6b  .pBt, u.av.iCook
24ec6 69 65 2c 20 28 75 33 32 20 2a 29 26 75 2e 61 76  ie, (u32 *)&u.av
24ec7 2e 69 4d 65 74 61 29 3b 0a 20 20 70 4f 75 74 2d  .iMeta);.  pOut-
24ec8 3e 75 2e 69 20 3d 20 75 2e 61 76 2e 69 4d 65 74  >u.i = u.av.iMet
24ec9 61 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  a;.  break;.}../
24eca 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 43 6f 6f  * Opcode: SetCoo
24ecb 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a  kie P1 P2 P3 * *
24ecc 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  .**.** Write the
24ecd 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
24ece 73 74 65 72 20 50 33 20 28 69 6e 74 65 72 70 72  ster P3 (interpr
24ecf 65 74 65 64 20 61 73 20 61 6e 20 69 6e 74 65 67  eted as an integ
24ed0 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 6f 6f 6b  er).** into cook
24ed1 69 65 20 6e 75 6d 62 65 72 20 50 32 20 6f 66 20  ie number P2 of 
24ed2 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 50 32  database P1.  P2
24ed3 3d 3d 31 20 69 73 20 74 68 65 20 73 63 68 65 6d  ==1 is the schem
24ed4 61 20 76 65 72 73 69 6f 6e 2e 20 20 0a 2a 2a 20  a version.  .** 
24ed5 50 32 3d 3d 32 20 69 73 20 74 68 65 20 64 61 74  P2==2 is the dat
24ed6 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 20 50 32  abase format. P2
24ed7 3d 3d 33 20 69 73 20 74 68 65 20 72 65 63 6f 6d  ==3 is the recom
24ed8 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63 61 63  mended pager cac
24ed9 68 65 20 0a 2a 2a 20 73 69 7a 65 2c 20 61 6e 64  he .** size, and
24eda 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d   so forth.  P1==
24edb 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61  0 is the main da
24edc 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20  tabase file and 
24edd 50 31 3d 3d 31 20 69 73 20 74 68 65 20 0a 2a 2a  P1==1 is the .**
24ede 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75   database file u
24edf 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 65 6d  sed to store tem
24ee0 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a  porary tables..*
24ee1 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69  *.** A transacti
24ee2 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74  on must be start
24ee3 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 75 74  ed before execut
24ee4 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e  ing this opcode.
24ee5 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74 43  .*/.case OP_SetC
24ee6 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 2f  ookie: {       /
24ee7 2a 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20  * in3 */.#if 0  
24ee8 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
24ee9 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
24eea 61 77 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b  aw */.  Db *pDb;
24eeb 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
24eec 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
24eed 20 69 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a 20 20   into u.aw */.  
24eee 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c  assert( pOp->p2<
24eef 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d  SQLITE_N_BTREE_M
24ef0 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ETA );.  assert(
24ef1 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
24ef2 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29  Op->p1<db->nDb )
24ef3 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  ;.  assert( (p->
24ef4 62 74 72 65 65 4d 61 73 6b 20 26 20 28 28 28 79  btreeMask & (((y
24ef5 44 62 4d 61 73 6b 29 31 29 3c 3c 70 4f 70 2d 3e  DbMask)1)<<pOp->
24ef6 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e 61  p1))!=0 );.  u.a
24ef7 77 2e 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62  w.pDb = &db->aDb
24ef8 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
24ef9 65 72 74 28 20 75 2e 61 77 2e 70 44 62 2d 3e 70  ert( u.aw.pDb->p
24efa 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  Bt!=0 );.  asser
24efb 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61  t( sqlite3Schema
24efc 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 70 4f  MutexHeld(db, pO
24efd 70 2d 3e 70 31 2c 20 30 29 20 29 3b 0a 20 20 70  p->p1, 0) );.  p
24efe 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  In3 = &aMem[pOp-
24eff 3e 70 33 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56  >p3];.  sqlite3V
24f00 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
24f01 28 70 49 6e 33 29 3b 0a 20 20 2f 2a 20 53 65 65  (pIn3);.  /* See
24f02 20 6e 6f 74 65 20 61 62 6f 75 74 20 69 6e 64 65   note about inde
24f03 78 20 73 68 69 66 74 69 6e 67 20 6f 6e 20 4f 50  x shifting on OP
24f04 5f 52 65 61 64 43 6f 6f 6b 69 65 20 2a 2f 0a 20  _ReadCookie */. 
24f05 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
24f06 65 65 55 70 64 61 74 65 4d 65 74 61 28 75 2e 61  eeUpdateMeta(u.a
24f07 77 2e 70 44 62 2d 3e 70 42 74 2c 20 70 4f 70 2d  w.pDb->pBt, pOp-
24f08 3e 70 32 2c 20 28 69 6e 74 29 70 49 6e 33 2d 3e  >p2, (int)pIn3->
24f09 75 2e 69 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d  u.i);.  if( pOp-
24f0a 3e 70 32 3d 3d 42 54 52 45 45 5f 53 43 48 45 4d  >p2==BTREE_SCHEM
24f0b 41 5f 56 45 52 53 49 4f 4e 20 29 7b 0a 20 20 20  A_VERSION ){.   
24f0c 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 73 63 68   /* When the sch
24f0d 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67  ema cookie chang
24f0e 65 73 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e  es, record the n
24f0f 65 77 20 63 6f 6f 6b 69 65 20 69 6e 74 65 72 6e  ew cookie intern
24f10 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 75 2e 61 77  ally */.    u.aw
24f11 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73  .pDb->pSchema->s
24f12 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 28  chema_cookie = (
24f13 69 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20  int)pIn3->u.i;. 
24f14 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20     db->flags |= 
24f15 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
24f16 6e 67 65 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66  nges;.  }else if
24f17 28 20 70 4f 70 2d 3e 70 32 3d 3d 42 54 52 45 45  ( pOp->p2==BTREE
24f18 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 29 7b 0a  _FILE_FORMAT ){.
24f19 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 63 68      /* Record ch
24f1a 61 6e 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c  anges in the fil
24f1b 65 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20 20  e format */.    
24f1c 75 2e 61 77 2e 70 44 62 2d 3e 70 53 63 68 65 6d  u.aw.pDb->pSchem
24f1d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d  a->file_format =
24f1e 20 28 75 38 29 70 49 6e 33 2d 3e 75 2e 69 3b 0a   (u8)pIn3->u.i;.
24f1f 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70    }.  if( pOp->p
24f20 31 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49  1==1 ){.    /* I
24f21 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72  nvalidate all pr
24f22 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
24f23 73 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 54  s whenever the T
24f24 45 4d 50 20 64 61 74 61 62 61 73 65 0a 20 20 20  EMP database.   
24f25 20 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 63 68   ** schema is ch
24f26 61 6e 67 65 64 2e 20 20 54 69 63 6b 65 74 20 23  anged.  Ticket #
24f27 31 36 34 34 20 2a 2f 0a 20 20 20 20 73 71 6c 69  1644 */.    sqli
24f28 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65  te3ExpirePrepare
24f29 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b  dStatements(db);
24f2a 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20  .    p->expired 
24f2b 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  = 0;.  }.  break
24f2c 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
24f2d 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 50 31 20  VerifyCookie P1 
24f2e 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
24f2f 43 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20  Check the value 
24f30 6f 66 20 67 6c 6f 62 61 6c 20 64 61 74 61 62 61  of global databa
24f31 73 65 20 70 61 72 61 6d 65 74 65 72 20 6e 75 6d  se parameter num
24f32 62 65 72 20 30 20 28 74 68 65 0a 2a 2a 20 73 63  ber 0 (the.** sc
24f33 68 65 6d 61 20 76 65 72 73 69 6f 6e 29 20 61 6e  hema version) an
24f34 64 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69  d make sure it i
24f35 73 20 65 71 75 61 6c 20 74 6f 20 50 32 20 61 6e  s equal to P2 an
24f36 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 67 65  d that the.** ge
24f37 6e 65 72 61 74 69 6f 6e 20 63 6f 75 6e 74 65 72  neration counter
24f38 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 73 63   on the local sc
24f39 68 65 6d 61 20 70 61 72 73 65 20 65 71 75 61 6c  hema parse equal
24f3a 73 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69  s P3..**.** P1 i
24f3b 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  s the database n
24f3c 75 6d 62 65 72 20 77 68 69 63 68 20 69 73 20 30  umber which is 0
24f3d 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61   for the main da
24f3e 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61  tabase file.** a
24f3f 6e 64 20 31 20 66 6f 72 20 74 68 65 20 66 69 6c  nd 1 for the fil
24f40 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70 6f 72  e holding tempor
24f41 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 20 73  ary tables and s
24f42 6f 6d 65 20 68 69 67 68 65 72 20 6e 75 6d 62 65  ome higher numbe
24f43 72 0a 2a 2a 20 66 6f 72 20 61 75 78 69 6c 69 61  r.** for auxilia
24f44 72 79 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a  ry databases..**
24f45 0a 2a 2a 20 54 68 65 20 63 6f 6f 6b 69 65 20 63  .** The cookie c
24f46 68 61 6e 67 65 73 20 69 74 73 20 76 61 6c 75 65  hanges its value
24f47 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 64 61   whenever the da
24f48 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 63 68  tabase schema ch
24f49 61 6e 67 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6f  anges..** This o
24f4a 70 65 72 61 74 69 6f 6e 20 69 73 20 75 73 65 64  peration is used
24f4b 20 74 6f 20 64 65 74 65 63 74 20 77 68 65 6e 20   to detect when 
24f4c 74 68 61 74 20 74 68 65 20 63 6f 6f 6b 69 65 20  that the cookie 
24f4d 68 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61  has changed.** a
24f4e 6e 64 20 74 68 61 74 20 74 68 65 20 63 75 72 72  nd that the curr
24f4f 65 6e 74 20 70 72 6f 63 65 73 73 20 6e 65 65 64  ent process need
24f50 73 20 74 6f 20 72 65 72 65 61 64 20 74 68 65 20  s to reread the 
24f51 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 45 69  schema..**.** Ei
24f52 74 68 65 72 20 61 20 74 72 61 6e 73 61 63 74 69  ther a transacti
24f53 6f 6e 20 6e 65 65 64 73 20 74 6f 20 68 61 76 65  on needs to have
24f54 20 62 65 65 6e 20 73 74 61 72 74 65 64 20 6f 72   been started or
24f55 20 61 6e 20 4f 50 5f 4f 70 65 6e 20 6e 65 65 64   an OP_Open need
24f56 73 0a 2a 2a 20 74 6f 20 62 65 20 65 78 65 63 75  s.** to be execu
24f57 74 65 64 20 28 74 6f 20 65 73 74 61 62 6c 69 73  ted (to establis
24f58 68 20 61 20 72 65 61 64 20 6c 6f 63 6b 29 20 62  h a read lock) b
24f59 65 66 6f 72 65 20 74 68 69 73 20 6f 70 63 6f 64  efore this opcod
24f5a 65 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b 65 64 2e  e is.** invoked.
24f5b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 65 72 69  .*/.case OP_Veri
24f5c 66 79 43 6f 6f 6b 69 65 3a 20 7b 0a 23 69 66 20  fyCookie: {.#if 
24f5d 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
24f5e 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
24f5f 20 75 2e 61 78 20 2a 2f 0a 20 20 69 6e 74 20 69   u.ax */.  int i
24f60 4d 65 74 61 3b 0a 20 20 69 6e 74 20 69 47 65 6e  Meta;.  int iGen
24f61 3b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a  ;.  Btree *pBt;.
24f62 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
24f63 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
24f64 69 6e 74 6f 20 75 2e 61 78 20 2a 2f 0a 0a 20 20  into u.ax */..  
24f65 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
24f66 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62  =0 && pOp->p1<db
24f67 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
24f68 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
24f69 20 26 20 28 28 28 79 44 62 4d 61 73 6b 29 31 29   & (((yDbMask)1)
24f6a 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29  <<pOp->p1))!=0 )
24f6b 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
24f6c 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65  te3SchemaMutexHe
24f6d 6c 64 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20  ld(db, pOp->p1, 
24f6e 30 29 20 29 3b 0a 20 20 75 2e 61 78 2e 70 42 74  0) );.  u.ax.pBt
24f6f 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e   = db->aDb[pOp->
24f70 70 31 5d 2e 70 42 74 3b 0a 20 20 69 66 28 20 75  p1].pBt;.  if( u
24f71 2e 61 78 2e 70 42 74 20 29 7b 0a 20 20 20 20 73  .ax.pBt ){.    s
24f72 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65  qlite3BtreeGetMe
24f73 74 61 28 75 2e 61 78 2e 70 42 74 2c 20 42 54 52  ta(u.ax.pBt, BTR
24f74 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f  EE_SCHEMA_VERSIO
24f75 4e 2c 20 28 75 33 32 20 2a 29 26 75 2e 61 78 2e  N, (u32 *)&u.ax.
24f76 69 4d 65 74 61 29 3b 0a 20 20 20 20 75 2e 61 78  iMeta);.    u.ax
24f77 2e 69 47 65 6e 20 3d 20 64 62 2d 3e 61 44 62 5b  .iGen = db->aDb[
24f78 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65 6d 61  pOp->p1].pSchema
24f79 2d 3e 69 47 65 6e 65 72 61 74 69 6f 6e 3b 0a 20  ->iGeneration;. 
24f7a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 78   }else{.    u.ax
24f7b 2e 69 47 65 6e 20 3d 20 75 2e 61 78 2e 69 4d 65  .iGen = u.ax.iMe
24f7c 74 61 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  ta = 0;.  }.  if
24f7d 28 20 75 2e 61 78 2e 69 4d 65 74 61 21 3d 70 4f  ( u.ax.iMeta!=pO
24f7e 70 2d 3e 70 32 20 7c 7c 20 75 2e 61 78 2e 69 47  p->p2 || u.ax.iG
24f7f 65 6e 21 3d 70 4f 70 2d 3e 70 33 20 29 7b 0a 20  en!=pOp->p3 ){. 
24f80 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
24f81 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
24f82 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67  ;.    p->zErrMsg
24f83 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
24f84 75 70 28 64 62 2c 20 22 64 61 74 61 62 61 73 65  up(db, "database
24f85 20 73 63 68 65 6d 61 20 68 61 73 20 63 68 61 6e   schema has chan
24f86 67 65 64 22 29 3b 0a 20 20 20 20 2f 2a 20 49 66  ged");.    /* If
24f87 20 74 68 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b   the schema-cook
24f88 69 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ie from the data
24f89 62 61 73 65 20 66 69 6c 65 20 6d 61 74 63 68 65  base file matche
24f8a 73 20 74 68 65 20 63 6f 6f 6b 69 65 0a 20 20 20  s the cookie.   
24f8b 20 2a 2a 20 73 74 6f 72 65 64 20 77 69 74 68 20   ** stored with 
24f8c 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65  the in-memory re
24f8d 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
24f8e 74 68 65 20 73 63 68 65 6d 61 2c 20 64 6f 0a 20  the schema, do. 
24f8f 20 20 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61 64     ** not reload
24f90 20 74 68 65 20 73 63 68 65 6d 61 20 66 72 6f 6d   the schema from
24f91 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
24f92 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  le..    **.    *
24f93 2a 20 49 66 20 76 69 72 74 75 61 6c 2d 74 61 62  * If virtual-tab
24f94 6c 65 73 20 61 72 65 20 69 6e 20 75 73 65 2c 20  les are in use, 
24f95 74 68 69 73 20 69 73 20 6e 6f 74 20 6a 75 73 74  this is not just
24f96 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e   an optimization
24f97 2e 0a 20 20 20 20 2a 2a 20 4f 66 74 65 6e 2c 20  ..    ** Often, 
24f98 76 2d 74 61 62 6c 65 73 20 73 74 6f 72 65 20 74  v-tables store t
24f99 68 65 69 72 20 64 61 74 61 20 69 6e 20 6f 74 68  heir data in oth
24f9a 65 72 20 53 51 4c 69 74 65 20 74 61 62 6c 65 73  er SQLite tables
24f9b 2c 20 77 68 69 63 68 0a 20 20 20 20 2a 2a 20 61  , which.    ** a
24f9c 72 65 20 71 75 65 72 69 65 64 20 66 72 6f 6d 20  re queried from 
24f9d 77 69 74 68 69 6e 20 78 4e 65 78 74 28 29 20 61  within xNext() a
24f9e 6e 64 20 6f 74 68 65 72 20 76 2d 74 61 62 6c 65  nd other v-table
24f9f 20 6d 65 74 68 6f 64 73 20 75 73 69 6e 67 0a 20   methods using. 
24fa0 20 20 20 2a 2a 20 70 72 65 70 61 72 65 64 20 71     ** prepared q
24fa1 75 65 72 69 65 73 2e 20 49 66 20 73 75 63 68 20  ueries. If such 
24fa2 61 20 71 75 65 72 79 20 69 73 20 6f 75 74 2d 6f  a query is out-o
24fa3 66 2d 64 61 74 65 2c 20 77 65 20 64 6f 20 6e 6f  f-date, we do no
24fa4 74 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 2a 2a  t want to.    **
24fa5 20 64 69 73 63 61 72 64 20 74 68 65 20 64 61 74   discard the dat
24fa6 61 62 61 73 65 20 73 63 68 65 6d 61 2c 20 61 73  abase schema, as
24fa7 20 74 68 65 20 75 73 65 72 20 63 6f 64 65 20 69   the user code i
24fa8 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 0a  mplementing the.
24fa9 20 20 20 20 2a 2a 20 76 2d 74 61 62 6c 65 20 77      ** v-table w
24faa 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 62 65 20  ould have to be 
24fab 72 65 61 64 79 20 66 6f 72 20 74 68 65 20 73 71  ready for the sq
24fac 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63  lite3_vtab struc
24fad 74 75 72 65 20 69 74 73 65 6c 66 0a 20 20 20 20  ture itself.    
24fae 2a 2a 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64  ** to be invalid
24faf 61 74 65 64 20 77 68 65 6e 65 76 65 72 20 73 71  ated whenever sq
24fb0 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 73 20  lite3_step() is 
24fb1 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68  called from with
24fb2 69 6e 0a 20 20 20 20 2a 2a 20 61 20 76 2d 74 61  in.    ** a v-ta
24fb3 62 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20 20 20  ble method..    
24fb4 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61  */.    if( db->a
24fb5 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68  Db[pOp->p1].pSch
24fb6 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b  ema->schema_cook
24fb7 69 65 21 3d 75 2e 61 78 2e 69 4d 65 74 61 20 29  ie!=u.ax.iMeta )
24fb8 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  {.      sqlite3R
24fb9 65 73 65 74 4f 6e 65 53 63 68 65 6d 61 28 64 62  esetOneSchema(db
24fba 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20  , pOp->p1);.    
24fbb 7d 0a 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65  }..    p->expire
24fbc 64 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20  d = 1;.    rc = 
24fbd 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20  SQLITE_SCHEMA;. 
24fbe 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
24fbf 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 52 65  * Opcode: OpenRe
24fc0 61 64 20 50 31 20 50 32 20 50 33 20 50 34 20 50  ad P1 P2 P3 P4 P
24fc1 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72  5.**.** Open a r
24fc2 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 20  ead-only cursor 
24fc3 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65  for the database
24fc4 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f   table whose roo
24fc5 74 20 70 61 67 65 20 69 73 0a 2a 2a 20 50 32 20  t page is.** P2 
24fc6 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69  in a database fi
24fc7 6c 65 2e 20 20 54 68 65 20 64 61 74 61 62 61 73  le.  The databas
24fc8 65 20 66 69 6c 65 20 69 73 20 64 65 74 65 72 6d  e file is determ
24fc9 69 6e 65 64 20 62 79 20 50 33 2e 20 0a 2a 2a 20  ined by P3. .** 
24fca 50 33 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 20  P3==0 means the 
24fcb 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 50  main database, P
24fcc 33 3d 3d 31 20 6d 65 61 6e 73 20 74 68 65 20 64  3==1 means the d
24fcd 61 74 61 62 61 73 65 20 75 73 65 64 20 66 6f 72  atabase used for
24fce 20 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 74   .** temporary t
24fcf 61 62 6c 65 73 2c 20 61 6e 64 20 50 33 3e 31 20  ables, and P3>1 
24fd0 6d 65 61 6e 73 20 75 73 65 64 20 74 68 65 20 63  means used the c
24fd1 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 74 74  orresponding att
24fd2 61 63 68 65 64 0a 2a 2a 20 64 61 74 61 62 61 73  ached.** databas
24fd3 65 2e 20 20 47 69 76 65 20 74 68 65 20 6e 65 77  e.  Give the new
24fd4 20 63 75 72 73 6f 72 20 61 6e 20 69 64 65 6e 74   cursor an ident
24fd5 69 66 69 65 72 20 6f 66 20 50 31 2e 20 20 54 68  ifier of P1.  Th
24fd6 65 20 50 31 0a 2a 2a 20 76 61 6c 75 65 73 20 6e  e P1.** values n
24fd7 65 65 64 20 6e 6f 74 20 62 65 20 63 6f 6e 74 69  eed not be conti
24fd8 67 75 6f 75 73 20 62 75 74 20 61 6c 6c 20 50 31  guous but all P1
24fd9 20 76 61 6c 75 65 73 20 73 68 6f 75 6c 64 20 62   values should b
24fda 65 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73  e small integers
24fdb 2e 0a 2a 2a 20 49 74 20 69 73 20 61 6e 20 65 72  ..** It is an er
24fdc 72 6f 72 20 66 6f 72 20 50 31 20 74 6f 20 62 65  ror for P1 to be
24fdd 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a   negative..**.**
24fde 20 49 66 20 50 35 21 3d 30 20 74 68 65 6e 20 75   If P5!=0 then u
24fdf 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  se the content o
24fe0 66 20 72 65 67 69 73 74 65 72 20 50 32 20 61 73  f register P2 as
24fe1 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 2c 20   the root page, 
24fe2 6e 6f 74 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65  not.** the value
24fe3 20 6f 66 20 50 32 20 69 74 73 65 6c 66 2e 0a 2a   of P2 itself..*
24fe4 2a 0a 2a 2a 20 54 68 65 72 65 20 77 69 6c 6c 20  *.** There will 
24fe5 62 65 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f  be a read lock o
24fe6 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77  n the database w
24fe7 68 65 6e 65 76 65 72 20 74 68 65 72 65 20 69 73  henever there is
24fe8 20 61 6e 0a 2a 2a 20 6f 70 65 6e 20 63 75 72 73   an.** open curs
24fe9 6f 72 2e 20 20 49 66 20 74 68 65 20 64 61 74 61  or.  If the data
24fea 62 61 73 65 20 77 61 73 20 75 6e 6c 6f 63 6b 65  base was unlocke
24feb 64 20 70 72 69 6f 72 20 74 6f 20 74 68 69 73 20  d prior to this 
24fec 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 74  instruction.** t
24fed 68 65 6e 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  hen a read lock 
24fee 69 73 20 61 63 71 75 69 72 65 64 20 61 73 20 70  is acquired as p
24fef 61 72 74 20 6f 66 20 74 68 69 73 20 69 6e 73 74  art of this inst
24ff0 72 75 63 74 69 6f 6e 2e 20 20 41 20 72 65 61 64  ruction.  A read
24ff1 0a 2a 2a 20 6c 6f 63 6b 20 61 6c 6c 6f 77 73 20  .** lock allows 
24ff2 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20  other processes 
24ff3 74 6f 20 72 65 61 64 20 74 68 65 20 64 61 74 61  to read the data
24ff4 62 61 73 65 20 62 75 74 20 70 72 6f 68 69 62 69  base but prohibi
24ff5 74 73 0a 2a 2a 20 61 6e 79 20 6f 74 68 65 72 20  ts.** any other 
24ff6 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 6d 6f 64  process from mod
24ff7 69 66 79 69 6e 67 20 74 68 65 20 64 61 74 61 62  ifying the datab
24ff8 61 73 65 2e 20 20 54 68 65 20 72 65 61 64 20 6c  ase.  The read l
24ff9 6f 63 6b 20 69 73 0a 2a 2a 20 72 65 6c 65 61 73  ock is.** releas
24ffa 65 64 20 77 68 65 6e 20 61 6c 6c 20 63 75 72 73  ed when all curs
24ffb 6f 72 73 20 61 72 65 20 63 6c 6f 73 65 64 2e 20  ors are closed. 
24ffc 20 49 66 20 74 68 69 73 20 69 6e 73 74 72 75 63   If this instruc
24ffd 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 0a 2a 2a  tion attempts.**
24ffe 20 74 6f 20 67 65 74 20 61 20 72 65 61 64 20 6c   to get a read l
24fff 6f 63 6b 20 62 75 74 20 66 61 69 6c 73 2c 20 74  ock but fails, t
25000 68 65 20 73 63 72 69 70 74 20 74 65 72 6d 69 6e  he script termin
25001 61 74 65 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20  ates with an.** 
25002 53 51 4c 49 54 45 5f 42 55 53 59 20 65 72 72 6f  SQLITE_BUSY erro
25003 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  r code..**.** Th
25004 65 20 50 34 20 76 61 6c 75 65 20 6d 61 79 20 62  e P4 value may b
25005 65 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65  e either an inte
25006 67 65 72 20 28 50 34 5f 49 4e 54 33 32 29 20 6f  ger (P4_INT32) o
25007 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a  r a pointer to.*
25008 2a 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  * a KeyInfo stru
25009 63 74 75 72 65 20 28 50 34 5f 4b 45 59 49 4e 46  cture (P4_KEYINF
2500a 4f 29 2e 20 49 66 20 69 74 20 69 73 20 61 20 70  O). If it is a p
2500b 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49  ointer to a KeyI
2500c 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72  nfo .** structur
2500d 65 2c 20 74 68 65 6e 20 73 61 69 64 20 73 74 72  e, then said str
2500e 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74  ucture defines t
2500f 68 65 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63  he content and c
25010 6f 6c 6c 61 74 69 6e 67 20 0a 2a 2a 20 73 65 71  ollating .** seq
25011 75 65 6e 63 65 20 6f 66 20 74 68 65 20 69 6e 64  uence of the ind
25012 65 78 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e  ex being opened.
25013 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 50   Otherwise, if P
25014 34 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  4 is an integer 
25015 0a 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20 69 73  .** value, it is
25016 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62   set to the numb
25017 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
25018 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a   the table..**.*
25019 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e 57  * See also OpenW
2501a 72 69 74 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  rite..*/./* Opco
2501b 64 65 3a 20 4f 70 65 6e 57 72 69 74 65 20 50 31  de: OpenWrite P1
2501c 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a   P2 P3 P4 P5.**.
2501d 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2f 77  ** Open a read/w
2501e 72 69 74 65 20 63 75 72 73 6f 72 20 6e 61 6d 65  rite cursor name
2501f 64 20 50 31 20 6f 6e 20 74 68 65 20 74 61 62 6c  d P1 on the tabl
25020 65 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65  e or index whose
25021 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73   root.** page is
25022 20 50 32 2e 20 20 4f 72 20 69 66 20 50 35 21 3d   P2.  Or if P5!=
25023 30 20 75 73 65 20 74 68 65 20 63 6f 6e 74 65 6e  0 use the conten
25024 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 32  t of register P2
25025 20 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20   to find the.** 
25026 72 6f 6f 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a  root page..**.**
25027 20 54 68 65 20 50 34 20 76 61 6c 75 65 20 6d 61   The P4 value ma
25028 79 20 62 65 20 65 69 74 68 65 72 20 61 6e 20 69  y be either an i
25029 6e 74 65 67 65 72 20 28 50 34 5f 49 4e 54 33 32  nteger (P4_INT32
2502a 29 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74  ) or a pointer t
2502b 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e 66 6f 20 73  o.** a KeyInfo s
2502c 74 72 75 63 74 75 72 65 20 28 50 34 5f 4b 45 59  tructure (P4_KEY
2502d 49 4e 46 4f 29 2e 20 49 66 20 69 74 20 69 73 20  INFO). If it is 
2502e 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b  a pointer to a K
2502f 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63  eyInfo .** struc
25030 74 75 72 65 2c 20 74 68 65 6e 20 73 61 69 64 20  ture, then said 
25031 73 74 72 75 63 74 75 72 65 20 64 65 66 69 6e 65  structure define
25032 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61 6e  s the content an
25033 64 20 63 6f 6c 6c 61 74 69 6e 67 20 0a 2a 2a 20  d collating .** 
25034 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68 65 20  sequence of the 
25035 69 6e 64 65 78 20 62 65 69 6e 67 20 6f 70 65 6e  index being open
25036 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ed. Otherwise, i
25037 66 20 50 34 20 69 73 20 61 6e 20 69 6e 74 65 67  f P4 is an integ
25038 65 72 20 0a 2a 2a 20 76 61 6c 75 65 2c 20 69 74  er .** value, it
25039 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e   is set to the n
2503a 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
2503b 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 6f   in the table, o
2503c 72 20 74 6f 20 74 68 65 0a 2a 2a 20 6c 61 72 67  r to the.** larg
2503d 65 73 74 20 69 6e 64 65 78 20 6f 66 20 61 6e 79  est index of any
2503e 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74   column of the t
2503f 61 62 6c 65 20 74 68 61 74 20 69 73 20 61 63 74  able that is act
25040 75 61 6c 6c 79 20 75 73 65 64 2e 0a 2a 2a 0a 2a  ually used..**.*
25041 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69  * This instructi
25042 6f 6e 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69  on works just li
25043 6b 65 20 4f 70 65 6e 52 65 61 64 20 65 78 63 65  ke OpenRead exce
25044 70 74 20 74 68 61 74 20 69 74 20 6f 70 65 6e 73  pt that it opens
25045 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69   the cursor.** i
25046 6e 20 72 65 61 64 2f 77 72 69 74 65 20 6d 6f 64  n read/write mod
25047 65 2e 20 20 46 6f 72 20 61 20 67 69 76 65 6e 20  e.  For a given 
25048 74 61 62 6c 65 2c 20 74 68 65 72 65 20 63 61 6e  table, there can
25049 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   be one or more 
2504a 72 65 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72  read-only.** cur
2504b 73 6f 72 73 20 6f 72 20 61 20 73 69 6e 67 6c 65  sors or a single
2504c 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   read/write curs
2504d 6f 72 20 62 75 74 20 6e 6f 74 20 62 6f 74 68 2e  or but not both.
2504e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20  .**.** See also 
2504f 4f 70 65 6e 52 65 61 64 2e 0a 2a 2f 0a 63 61 73  OpenRead..*/.cas
25050 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 3a 0a 63  e OP_OpenRead:.c
25051 61 73 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  ase OP_OpenWrite
25052 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  : {.#if 0  /* lo
25053 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
25054 76 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a 2f  ved into u.ay */
25055 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20  .  int nField;. 
25056 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
25057 66 6f 3b 0a 20 20 69 6e 74 20 70 32 3b 0a 20 20  fo;.  int p2;.  
25058 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 77  int iDb;.  int w
25059 72 46 6c 61 67 3b 0a 20 20 42 74 72 65 65 20 2a  rFlag;.  Btree *
2505a 70 58 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72  pX;.  VdbeCursor
2505b 20 2a 70 43 75 72 3b 0a 20 20 44 62 20 2a 70 44   *pCur;.  Db *pD
2505c 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  b;.#endif /* loc
2505d 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
2505e 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a 2f 0a  ed into u.ay */.
2505f 0a 20 20 61 73 73 65 72 74 28 20 28 70 4f 70 2d  .  assert( (pOp-
25060 3e 70 35 26 28 4f 50 46 4c 41 47 5f 50 32 49 53  >p5&(OPFLAG_P2IS
25061 52 45 47 7c 4f 50 46 4c 41 47 5f 42 55 4c 4b 43  REG|OPFLAG_BULKC
25062 53 52 29 29 3d 3d 70 4f 70 2d 3e 70 35 20 29 3b  SR))==pOp->p5 );
25063 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
25064 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57  opcode==OP_OpenW
25065 72 69 74 65 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d  rite || pOp->p5=
25066 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e  =0 );..  if( p->
25067 65 78 70 69 72 65 64 20 29 7b 0a 20 20 20 20 72  expired ){.    r
25068 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54  c = SQLITE_ABORT
25069 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  ;.    break;.  }
2506a 0a 0a 20 20 75 2e 61 79 2e 6e 46 69 65 6c 64 20  ..  u.ay.nField 
2506b 3d 20 30 3b 0a 20 20 75 2e 61 79 2e 70 4b 65 79  = 0;.  u.ay.pKey
2506c 49 6e 66 6f 20 3d 20 30 3b 0a 20 20 75 2e 61 79  Info = 0;.  u.ay
2506d 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  .p2 = pOp->p2;. 
2506e 20 75 2e 61 79 2e 69 44 62 20 3d 20 70 4f 70 2d   u.ay.iDb = pOp-
2506f 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 28 20 75  >p3;.  assert( u
25070 2e 61 79 2e 69 44 62 3e 3d 30 20 26 26 20 75 2e  .ay.iDb>=0 && u.
25071 61 79 2e 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  ay.iDb<db->nDb )
25072 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  ;.  assert( (p->
25073 62 74 72 65 65 4d 61 73 6b 20 26 20 28 28 28 79  btreeMask & (((y
25074 44 62 4d 61 73 6b 29 31 29 3c 3c 75 2e 61 79 2e  DbMask)1)<<u.ay.
25075 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e  iDb))!=0 );.  u.
25076 61 79 2e 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  ay.pDb = &db->aD
25077 62 5b 75 2e 61 79 2e 69 44 62 5d 3b 0a 20 20 75  b[u.ay.iDb];.  u
25078 2e 61 79 2e 70 58 20 3d 20 75 2e 61 79 2e 70 44  .ay.pX = u.ay.pD
25079 62 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74  b->pBt;.  assert
2507a 28 20 75 2e 61 79 2e 70 58 21 3d 30 20 29 3b 0a  ( u.ay.pX!=0 );.
2507b 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
2507c 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20  e==OP_OpenWrite 
2507d 29 7b 0a 20 20 20 20 75 2e 61 79 2e 77 72 46 6c  ){.    u.ay.wrFl
2507e 61 67 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 65  ag = 1;.    asse
2507f 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d  rt( sqlite3Schem
25080 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 75  aMutexHeld(db, u
25081 2e 61 79 2e 69 44 62 2c 20 30 29 20 29 3b 0a 20  .ay.iDb, 0) );. 
25082 20 20 20 69 66 28 20 75 2e 61 79 2e 70 44 62 2d     if( u.ay.pDb-
25083 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66  >pSchema->file_f
25084 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d 69 6e 57 72  ormat < p->minWr
25085 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 29 7b  iteFileFormat ){
25086 0a 20 20 20 20 20 20 70 2d 3e 6d 69 6e 57 72 69  .      p->minWri
25087 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20 75  teFileFormat = u
25088 2e 61 79 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61  .ay.pDb->pSchema
25089 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a 20  ->file_format;. 
2508a 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
2508b 20 20 75 2e 61 79 2e 77 72 46 6c 61 67 20 3d 20    u.ay.wrFlag = 
2508c 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70  0;.  }.  if( pOp
2508d 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 50 32  ->p5 & OPFLAG_P2
2508e 49 53 52 45 47 20 29 7b 0a 20 20 20 20 61 73 73  ISREG ){.    ass
2508f 65 72 74 28 20 75 2e 61 79 2e 70 32 3e 30 20 29  ert( u.ay.p2>0 )
25090 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  ;.    assert( u.
25091 61 79 2e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  ay.p2<=p->nMem )
25092 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20 26 61 4d  ;.    pIn2 = &aM
25093 65 6d 5b 75 2e 61 79 2e 70 32 5d 3b 0a 20 20 20  em[u.ay.p2];.   
25094 20 61 73 73 65 72 74 28 20 6d 65 6d 49 73 56 61   assert( memIsVa
25095 6c 69 64 28 70 49 6e 32 29 20 29 3b 0a 20 20 20  lid(pIn2) );.   
25096 20 61 73 73 65 72 74 28 20 28 70 49 6e 32 2d 3e   assert( (pIn2->
25097 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29  flags & MEM_Int)
25098 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  !=0 );.    sqlit
25099 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
2509a 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 20 20 75  ify(pIn2);.    u
2509b 2e 61 79 2e 70 32 20 3d 20 28 69 6e 74 29 70 49  .ay.p2 = (int)pI
2509c 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 2f 2a 20  n2->u.i;.    /* 
2509d 54 68 65 20 75 2e 61 79 2e 70 32 20 76 61 6c 75  The u.ay.p2 valu
2509e 65 20 61 6c 77 61 79 73 20 63 6f 6d 65 73 20 66  e always comes f
2509f 72 6f 6d 20 61 20 70 72 69 6f 72 20 4f 50 5f 43  rom a prior OP_C
250a0 72 65 61 74 65 54 61 62 6c 65 20 6f 70 63 6f 64  reateTable opcod
250a1 65 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 68 61  e and.    ** tha
250a2 74 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20 61 6c  t opcode will al
250a3 77 61 79 73 20 73 65 74 20 74 68 65 20 75 2e 61  ways set the u.a
250a4 79 2e 70 32 20 76 61 6c 75 65 20 74 6f 20 32 20  y.p2 value to 2 
250a5 6f 72 20 6d 6f 72 65 20 6f 72 20 65 6c 73 65 20  or more or else 
250a6 66 61 69 6c 2e 0a 20 20 20 20 2a 2a 20 49 66 20  fail..    ** If 
250a7 74 68 65 72 65 20 77 65 72 65 20 61 20 66 61 69  there were a fai
250a8 6c 75 72 65 2c 20 74 68 65 20 70 72 65 70 61 72  lure, the prepar
250a9 65 64 20 73 74 61 74 65 6d 65 6e 74 20 77 6f 75  ed statement wou
250aa 6c 64 20 68 61 76 65 20 68 61 6c 74 65 64 0a 20  ld have halted. 
250ab 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 72 65 61     ** before rea
250ac 63 68 69 6e 67 20 74 68 69 73 20 69 6e 73 74 72  ching this instr
250ad 75 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 69  uction. */.    i
250ae 66 28 20 4e 45 56 45 52 28 75 2e 61 79 2e 70 32  f( NEVER(u.ay.p2
250af 3c 32 29 20 29 20 7b 0a 20 20 20 20 20 20 72 63  <2) ) {.      rc
250b0 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
250b1 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f  T_BKPT;.      go
250b2 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
250b3 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d  error;.    }.  }
250b4 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79  .  if( pOp->p4ty
250b5 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29  pe==P4_KEYINFO )
250b6 7b 0a 20 20 20 20 75 2e 61 79 2e 70 4b 65 79 49  {.    u.ay.pKeyI
250b7 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b  nfo = pOp->p4.pK
250b8 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75 2e 61 79  eyInfo;.    u.ay
250b9 2e 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d  .pKeyInfo->enc =
250ba 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20 20 20   ENC(p->db);.   
250bb 20 75 2e 61 79 2e 6e 46 69 65 6c 64 20 3d 20 75   u.ay.nField = u
250bc 2e 61 79 2e 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  .ay.pKeyInfo->nF
250bd 69 65 6c 64 2b 31 3b 0a 20 20 7d 65 6c 73 65 20  ield+1;.  }else 
250be 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  if( pOp->p4type=
250bf 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20 20 20  =P4_INT32 ){.   
250c0 20 75 2e 61 79 2e 6e 46 69 65 6c 64 20 3d 20 70   u.ay.nField = p
250c1 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 7d 0a 20 20  Op->p4.i;.  }.  
250c2 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
250c3 3d 30 20 29 3b 0a 20 20 75 2e 61 79 2e 70 43 75  =0 );.  u.ay.pCu
250c4 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73  r = allocateCurs
250c5 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 75  or(p, pOp->p1, u
250c6 2e 61 79 2e 6e 46 69 65 6c 64 2c 20 75 2e 61 79  .ay.nField, u.ay
250c7 2e 69 44 62 2c 20 31 29 3b 0a 20 20 69 66 28 20  .iDb, 1);.  if( 
250c8 75 2e 61 79 2e 70 43 75 72 3d 3d 30 20 29 20 67  u.ay.pCur==0 ) g
250c9 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e  oto no_mem;.  u.
250ca 61 79 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77  ay.pCur->nullRow
250cb 20 3d 20 31 3b 0a 20 20 75 2e 61 79 2e 70 43 75   = 1;.  u.ay.pCu
250cc 72 2d 3e 69 73 4f 72 64 65 72 65 64 20 3d 20 31  r->isOrdered = 1
250cd 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
250ce 42 74 72 65 65 43 75 72 73 6f 72 28 75 2e 61 79  BtreeCursor(u.ay
250cf 2e 70 58 2c 20 75 2e 61 79 2e 70 32 2c 20 75 2e  .pX, u.ay.p2, u.
250d0 61 79 2e 77 72 46 6c 61 67 2c 20 75 2e 61 79 2e  ay.wrFlag, u.ay.
250d1 70 4b 65 79 49 6e 66 6f 2c 20 75 2e 61 79 2e 70  pKeyInfo, u.ay.p
250d2 43 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20  Cur->pCursor);. 
250d3 20 75 2e 61 79 2e 70 43 75 72 2d 3e 70 4b 65 79   u.ay.pCur->pKey
250d4 49 6e 66 6f 20 3d 20 75 2e 61 79 2e 70 4b 65 79  Info = u.ay.pKey
250d5 49 6e 66 6f 3b 0a 20 20 61 73 73 65 72 74 28 20  Info;.  assert( 
250d6 4f 50 46 4c 41 47 5f 42 55 4c 4b 43 53 52 3d 3d  OPFLAG_BULKCSR==
250d7 42 54 52 45 45 5f 42 55 4c 4b 4c 4f 41 44 20 29  BTREE_BULKLOAD )
250d8 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
250d9 43 75 72 73 6f 72 48 69 6e 74 73 28 75 2e 61 79  CursorHints(u.ay
250da 2e 70 43 75 72 2d 3e 70 43 75 72 73 6f 72 2c 20  .pCur->pCursor, 
250db 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41  (pOp->p5 & OPFLA
250dc 47 5f 42 55 4c 4b 43 53 52 29 29 3b 0a 0a 20 20  G_BULKCSR));..  
250dd 2f 2a 20 53 69 6e 63 65 20 69 74 20 70 65 72 66  /* Since it perf
250de 6f 72 6d 73 20 6e 6f 20 6d 65 6d 6f 72 79 20 61  orms no memory a
250df 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 20 49 4f 2c  llocation or IO,
250e0 20 74 68 65 20 6f 6e 6c 79 20 76 61 6c 75 65 20   the only value 
250e1 74 68 61 74 0a 20 20 2a 2a 20 73 71 6c 69 74 65  that.  ** sqlite
250e2 33 42 74 72 65 65 43 75 72 73 6f 72 28 29 20 6d  3BtreeCursor() m
250e3 61 79 20 72 65 74 75 72 6e 20 69 73 20 53 51 4c  ay return is SQL
250e4 49 54 45 5f 4f 4b 2e 20 2a 2f 0a 20 20 61 73 73  ITE_OK. */.  ass
250e5 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
250e6 4f 4b 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20  OK );..  /* Set 
250e7 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e 69  the VdbeCursor.i
250e8 73 54 61 62 6c 65 20 61 6e 64 20 69 73 49 6e 64  sTable and isInd
250e9 65 78 20 76 61 72 69 61 62 6c 65 73 2e 20 50 72  ex variables. Pr
250ea 65 76 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20  evious versions 
250eb 6f 66 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 75  of.  ** SQLite u
250ec 73 65 64 20 74 6f 20 63 68 65 63 6b 20 69 66 20  sed to check if 
250ed 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 66 6c  the root-page fl
250ee 61 67 73 20 77 65 72 65 20 73 61 6e 65 20 61 74  ags were sane at
250ef 20 74 68 69 73 20 70 6f 69 6e 74 0a 20 20 2a 2a   this point.  **
250f0 20 61 6e 64 20 72 65 70 6f 72 74 20 64 61 74 61   and report data
250f1 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20  base corruption 
250f2 69 66 20 74 68 65 79 20 77 65 72 65 20 6e 6f 74  if they were not
250f3 2c 20 62 75 74 20 74 68 69 73 20 63 68 65 63 6b  , but this check
250f4 20 68 61 73 0a 20 20 2a 2a 20 73 69 6e 63 65 20   has.  ** since 
250f5 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 62  moved into the b
250f6 74 72 65 65 20 6c 61 79 65 72 2e 20 20 2a 2f 0a  tree layer.  */.
250f7 20 20 75 2e 61 79 2e 70 43 75 72 2d 3e 69 73 54    u.ay.pCur->isT
250f8 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79  able = pOp->p4ty
250f9 70 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a  pe!=P4_KEYINFO;.
250fa 20 20 75 2e 61 79 2e 70 43 75 72 2d 3e 69 73 49    u.ay.pCur->isI
250fb 6e 64 65 78 20 3d 20 21 75 2e 61 79 2e 70 43 75  ndex = !u.ay.pCu
250fc 72 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72  r->isTable;.  br
250fd 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
250fe 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  e: OpenEphemeral
250ff 20 50 31 20 50 32 20 2a 20 50 34 20 50 35 0a 2a   P1 P2 * P4 P5.*
25100 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20  *.** Open a new 
25101 63 75 72 73 6f 72 20 50 31 20 74 6f 20 61 20 74  cursor P1 to a t
25102 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 2e 0a  ransient table..
25103 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 69 73  ** The cursor is
25104 20 61 6c 77 61 79 73 20 6f 70 65 6e 65 64 20 72   always opened r
25105 65 61 64 2f 77 72 69 74 65 20 65 76 65 6e 20 69  ead/write even i
25106 66 20 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64  f .** the main d
25107 61 74 61 62 61 73 65 20 69 73 20 72 65 61 64 2d  atabase is read-
25108 6f 6e 6c 79 2e 20 20 54 68 65 20 65 70 68 65 6d  only.  The ephem
25109 65 72 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 73  eral.** table is
2510a 20 64 65 6c 65 74 65 64 20 61 75 74 6f 6d 61 74   deleted automat
2510b 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 20  ically when the 
2510c 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 65 64  cursor is closed
2510d 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68  ..**.** P2 is th
2510e 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
2510f 6d 6e 73 20 69 6e 20 74 68 65 20 65 70 68 65 6d  mns in the ephem
25110 65 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 20 54  eral table..** T
25111 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73  he cursor points
25112 20 74 6f 20 61 20 42 54 72 65 65 20 74 61 62 6c   to a BTree tabl
25113 65 20 69 66 20 50 34 3d 3d 30 20 61 6e 64 20 74  e if P4==0 and t
25114 6f 20 61 20 42 54 72 65 65 20 69 6e 64 65 78 0a  o a BTree index.
25115 2a 2a 20 69 66 20 50 34 20 69 73 20 6e 6f 74 20  ** if P4 is not 
25116 30 2e 20 20 49 66 20 50 34 20 69 73 20 6e 6f 74  0.  If P4 is not
25117 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 73   NULL, it points
25118 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74   to a KeyInfo st
25119 72 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 20  ructure.** that 
2511a 64 65 66 69 6e 65 73 20 74 68 65 20 66 6f 72 6d  defines the form
2511b 61 74 20 6f 66 20 6b 65 79 73 20 69 6e 20 74 68  at of keys in th
2511c 65 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54  e index..**.** T
2511d 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20 6f  his opcode was o
2511e 6e 63 65 20 63 61 6c 6c 65 64 20 4f 70 65 6e 54  nce called OpenT
2511f 65 6d 70 2e 20 20 42 75 74 20 74 68 61 74 20 63  emp.  But that c
25120 72 65 61 74 65 64 0a 2a 2a 20 63 6f 6e 66 75 73  reated.** confus
25121 69 6f 6e 20 62 65 63 61 75 73 65 20 74 68 65 20  ion because the 
25122 74 65 72 6d 20 22 74 65 6d 70 20 74 61 62 6c 65  term "temp table
25123 22 2c 20 6d 69 67 68 74 20 72 65 66 65 72 20 65  ", might refer e
25124 69 74 68 65 72 0a 2a 2a 20 74 6f 20 61 20 54 45  ither.** to a TE
25125 4d 50 20 74 61 62 6c 65 20 61 74 20 74 68 65 20  MP table at the 
25126 53 51 4c 20 6c 65 76 65 6c 2c 20 6f 72 20 74 6f  SQL level, or to
25127 20 61 20 74 61 62 6c 65 20 6f 70 65 6e 65 64 20   a table opened 
25128 62 79 0a 2a 2a 20 74 68 69 73 20 6f 70 63 6f 64  by.** this opcod
25129 65 2e 20 20 54 68 65 6e 20 74 68 69 73 20 6f 70  e.  Then this op
2512a 63 6f 64 65 20 77 61 73 20 63 61 6c 6c 20 4f 70  code was call Op
2512b 65 6e 56 69 72 74 75 61 6c 2e 20 20 42 75 74 0a  enVirtual.  But.
2512c 2a 2a 20 74 68 61 74 20 63 72 65 61 74 65 64 20  ** that created 
2512d 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20 74  confusion with t
2512e 68 65 20 77 68 6f 6c 65 20 76 69 72 74 75 61 6c  he whole virtual
2512f 2d 74 61 62 6c 65 20 69 64 65 61 2e 0a 2a 2a 0a  -table idea..**.
25130 2a 2a 20 54 68 65 20 50 35 20 70 61 72 61 6d 65  ** The P5 parame
25131 74 65 72 20 63 61 6e 20 62 65 20 61 20 6d 61 73  ter can be a mas
25132 6b 20 6f 66 20 74 68 65 20 42 54 52 45 45 5f 2a  k of the BTREE_*
25133 20 66 6c 61 67 73 20 64 65 66 69 6e 65 64 0a 2a   flags defined.*
25134 2a 20 69 6e 20 62 74 72 65 65 2e 68 2e 20 20 54  * in btree.h.  T
25135 68 65 73 65 20 66 6c 61 67 73 20 63 6f 6e 74 72  hese flags contr
25136 6f 6c 20 61 73 70 65 63 74 73 20 6f 66 20 74 68  ol aspects of th
25137 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 0a 2a  e operation of.*
25138 2a 20 74 68 65 20 62 74 72 65 65 2e 20 20 54 68  * the btree.  Th
25139 65 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55  e BTREE_OMIT_JOU
2513a 52 4e 41 4c 20 61 6e 64 20 42 54 52 45 45 5f 53  RNAL and BTREE_S
2513b 49 4e 47 4c 45 20 66 6c 61 67 73 20 61 72 65 0a  INGLE flags are.
2513c 2a 2a 20 61 64 64 65 64 20 61 75 74 6f 6d 61 74  ** added automat
2513d 69 63 61 6c 6c 79 2e 0a 2a 2f 0a 2f 2a 20 4f 70  ically..*/./* Op
2513e 63 6f 64 65 3a 20 4f 70 65 6e 41 75 74 6f 69 6e  code: OpenAutoin
2513f 64 65 78 20 50 31 20 50 32 20 2a 20 50 34 20 2a  dex P1 P2 * P4 *
25140 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
25141 64 65 20 77 6f 72 6b 73 20 74 68 65 20 73 61 6d  de works the sam
25142 65 20 61 73 20 4f 50 5f 4f 70 65 6e 45 70 68 65  e as OP_OpenEphe
25143 6d 65 72 61 6c 2e 20 20 49 74 20 68 61 73 20 61  meral.  It has a
25144 0a 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 6e 61  .** different na
25145 6d 65 20 74 6f 20 64 69 73 74 69 6e 67 75 69 73  me to distinguis
25146 68 20 69 74 73 20 75 73 65 2e 20 20 54 61 62 6c  h its use.  Tabl
25147 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  es created using
25148 0a 2a 2a 20 62 79 20 74 68 69 73 20 6f 70 63 6f  .** by this opco
25149 64 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  de will be used 
2514a 66 6f 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  for automaticall
2514b 79 20 63 72 65 61 74 65 64 20 74 72 61 6e 73 69  y created transi
2514c 65 6e 74 0a 2a 2a 20 69 6e 64 69 63 65 73 20 69  ent.** indices i
2514d 6e 20 6a 6f 69 6e 73 2e 0a 2a 2f 0a 63 61 73 65  n joins..*/.case
2514e 20 4f 50 5f 4f 70 65 6e 41 75 74 6f 69 6e 64 65   OP_OpenAutoinde
2514f 78 3a 20 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e  x: .case OP_Open
25150 45 70 68 65 6d 65 72 61 6c 3a 20 7b 0a 23 69 66  Ephemeral: {.#if
25151 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
25152 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
25153 6f 20 75 2e 61 7a 20 2a 2f 0a 20 20 56 64 62 65  o u.az */.  Vdbe
25154 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 23 65 6e  Cursor *pCx;.#en
25155 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
25156 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
25157 6f 20 75 2e 61 7a 20 2a 2f 0a 20 20 73 74 61 74  o u.az */.  stat
25158 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 76 66 73  ic const int vfs
25159 46 6c 61 67 73 20 3d 0a 20 20 20 20 20 20 53 51  Flags =.      SQ
2515a 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
2515b 49 54 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49  ITE |.      SQLI
2515c 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c  TE_OPEN_CREATE |
2515d 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50  .      SQLITE_OP
2515e 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 0a 20  EN_EXCLUSIVE |. 
2515f 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
25160 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c  _DELETEONCLOSE |
25161 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50  .      SQLITE_OP
25162 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 3b  EN_TRANSIENT_DB;
25163 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
25164 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 7a  >p1>=0 );.  u.az
25165 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43  .pCx = allocateC
25166 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31  ursor(p, pOp->p1
25167 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d 31 2c 20 31  , pOp->p2, -1, 1
25168 29 3b 0a 20 20 69 66 28 20 75 2e 61 7a 2e 70 43  );.  if( u.az.pC
25169 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  x==0 ) goto no_m
2516a 65 6d 3b 0a 20 20 75 2e 61 7a 2e 70 43 78 2d 3e  em;.  u.az.pCx->
2516b 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72  nullRow = 1;.  r
2516c 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
2516d 4f 70 65 6e 28 64 62 2d 3e 70 56 66 73 2c 20 30  Open(db->pVfs, 0
2516e 2c 20 64 62 2c 20 26 75 2e 61 7a 2e 70 43 78 2d  , db, &u.az.pCx-
2516f 3e 70 42 74 2c 0a 20 20 20 20 20 20 20 20 20 20  >pBt,.          
25170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 42 54                BT
25171 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c  REE_OMIT_JOURNAL
25172 20 7c 20 42 54 52 45 45 5f 53 49 4e 47 4c 45 20   | BTREE_SINGLE 
25173 7c 20 70 4f 70 2d 3e 70 35 2c 20 76 66 73 46 6c  | pOp->p5, vfsFl
25174 61 67 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ags);.  if( rc==
25175 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
25176 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
25177 65 65 42 65 67 69 6e 54 72 61 6e 73 28 75 2e 61  eeBeginTrans(u.a
25178 7a 2e 70 43 78 2d 3e 70 42 74 2c 20 31 29 3b 0a  z.pCx->pBt, 1);.
25179 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
2517a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f  LITE_OK ){.    /
2517b 2a 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74  * If a transient
2517c 20 69 6e 64 65 78 20 69 73 20 72 65 71 75 69 72   index is requir
2517d 65 64 2c 20 63 72 65 61 74 65 20 69 74 20 62 79  ed, create it by
2517e 20 63 61 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a 20   calling.    ** 
2517f 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61  sqlite3BtreeCrea
25180 74 65 54 61 62 6c 65 28 29 20 77 69 74 68 20 74  teTable() with t
25181 68 65 20 42 54 52 45 45 5f 42 4c 4f 42 4b 45 59  he BTREE_BLOBKEY
25182 20 66 6c 61 67 20 62 65 66 6f 72 65 0a 20 20 20   flag before.   
25183 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 2e 20   ** opening it. 
25184 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74  If a transient t
25185 61 62 6c 65 20 69 73 20 72 65 71 75 69 72 65 64  able is required
25186 2c 20 6a 75 73 74 20 75 73 65 20 74 68 65 0a 20  , just use the. 
25187 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61     ** automatica
25188 6c 6c 79 20 63 72 65 61 74 65 64 20 74 61 62 6c  lly created tabl
25189 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65  e with root-page
2518a 20 31 20 28 61 6e 20 42 4c 4f 42 5f 49 4e 54 4b   1 (an BLOB_INTK
2518b 45 59 20 74 61 62 6c 65 29 2e 0a 20 20 20 20 2a  EY table)..    *
2518c 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70  /.    if( pOp->p
2518d 34 2e 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20  4.pKeyInfo ){.  
2518e 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20      int pgno;.  
2518f 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
25190 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49  >p4type==P4_KEYI
25191 4e 46 4f 20 29 3b 0a 20 20 20 20 20 20 72 63 20  NFO );.      rc 
25192 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72  = sqlite3BtreeCr
25193 65 61 74 65 54 61 62 6c 65 28 75 2e 61 7a 2e 70  eateTable(u.az.p
25194 43 78 2d 3e 70 42 74 2c 20 26 70 67 6e 6f 2c 20  Cx->pBt, &pgno, 
25195 42 54 52 45 45 5f 42 4c 4f 42 4b 45 59 20 7c 20  BTREE_BLOBKEY | 
25196 70 4f 70 2d 3e 70 35 29 3b 0a 20 20 20 20 20 20  pOp->p5);.      
25197 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
25198 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  K ){.        ass
25199 65 72 74 28 20 70 67 6e 6f 3d 3d 4d 41 53 54 45  ert( pgno==MASTE
2519a 52 5f 52 4f 4f 54 2b 31 20 29 3b 0a 20 20 20 20  R_ROOT+1 );.    
2519b 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2519c 42 74 72 65 65 43 75 72 73 6f 72 28 75 2e 61 7a  BtreeCursor(u.az
2519d 2e 70 43 78 2d 3e 70 42 74 2c 20 70 67 6e 6f 2c  .pCx->pBt, pgno,
2519e 20 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   1,.            
2519f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
251a0 20 20 20 20 28 4b 65 79 49 6e 66 6f 2a 29 70 4f      (KeyInfo*)pO
251a1 70 2d 3e 70 34 2e 7a 2c 20 75 2e 61 7a 2e 70 43  p->p4.z, u.az.pC
251a2 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20  x->pCursor);.   
251a3 20 20 20 20 20 75 2e 61 7a 2e 70 43 78 2d 3e 70       u.az.pCx->p
251a4 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70  KeyInfo = pOp->p
251a5 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20  4.pKeyInfo;.    
251a6 20 20 20 20 75 2e 61 7a 2e 70 43 78 2d 3e 70 4b      u.az.pCx->pK
251a7 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e  eyInfo->enc = EN
251a8 43 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20  C(p->db);.      
251a9 7d 0a 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 78  }.      u.az.pCx
251aa 2d 3e 69 73 54 61 62 6c 65 20 3d 20 30 3b 0a 20  ->isTable = 0;. 
251ab 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
251ac 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
251ad 65 43 75 72 73 6f 72 28 75 2e 61 7a 2e 70 43 78  eCursor(u.az.pCx
251ae 2d 3e 70 42 74 2c 20 4d 41 53 54 45 52 5f 52 4f  ->pBt, MASTER_RO
251af 4f 54 2c 20 31 2c 20 30 2c 20 75 2e 61 7a 2e 70  OT, 1, 0, u.az.p
251b0 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20  Cx->pCursor);.  
251b1 20 20 20 20 75 2e 61 7a 2e 70 43 78 2d 3e 69 73      u.az.pCx->is
251b2 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d  Table = 1;.    }
251b3 0a 20 20 7d 0a 20 20 75 2e 61 7a 2e 70 43 78 2d  .  }.  u.az.pCx-
251b4 3e 69 73 4f 72 64 65 72 65 64 20 3d 20 28 70 4f  >isOrdered = (pO
251b5 70 2d 3e 70 35 21 3d 42 54 52 45 45 5f 55 4e 4f  p->p5!=BTREE_UNO
251b6 52 44 45 52 45 44 29 3b 0a 20 20 75 2e 61 7a 2e  RDERED);.  u.az.
251b7 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21  pCx->isIndex = !
251b8 75 2e 61 7a 2e 70 43 78 2d 3e 69 73 54 61 62 6c  u.az.pCx->isTabl
251b9 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  e;.  break;.}../
251ba 2a 20 4f 70 63 6f 64 65 3a 20 53 6f 72 74 65 72  * Opcode: Sorter
251bb 4f 70 65 6e 20 50 31 20 50 32 20 2a 20 50 34 20  Open P1 P2 * P4 
251bc 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  *.**.** This opc
251bd 6f 64 65 20 77 6f 72 6b 73 20 6c 69 6b 65 20 4f  ode works like O
251be 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20  P_OpenEphemeral 
251bf 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20 6f  except that it o
251c0 70 65 6e 73 0a 2a 2a 20 61 20 74 72 61 6e 73 69  pens.** a transi
251c1 65 6e 74 20 69 6e 64 65 78 20 74 68 61 74 20 69  ent index that i
251c2 73 20 73 70 65 63 69 66 69 63 61 6c 6c 79 20 64  s specifically d
251c3 65 73 69 67 6e 65 64 20 74 6f 20 73 6f 72 74 20  esigned to sort 
251c4 6c 61 72 67 65 0a 2a 2a 20 74 61 62 6c 65 73 20  large.** tables 
251c5 75 73 69 6e 67 20 61 6e 20 65 78 74 65 72 6e 61  using an externa
251c6 6c 20 6d 65 72 67 65 2d 73 6f 72 74 20 61 6c 67  l merge-sort alg
251c7 6f 72 69 74 68 6d 2e 0a 2a 2f 0a 63 61 73 65 20  orithm..*/.case 
251c8 4f 50 5f 53 6f 72 74 65 72 4f 70 65 6e 3a 20 7b  OP_SorterOpen: {
251c9 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
251ca 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
251cb 20 69 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a 20 20   into u.ba */.  
251cc 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b  VdbeCursor *pCx;
251cd 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
251ce 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
251cf 20 69 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a 0a 23   into u.ba */..#
251d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
251d1 49 54 5f 4d 45 52 47 45 5f 53 4f 52 54 0a 20 20  IT_MERGE_SORT.  
251d2 75 2e 62 61 2e 70 43 78 20 3d 20 61 6c 6c 6f 63  u.ba.pCx = alloc
251d3 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70  ateCursor(p, pOp
251d4 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d  ->p1, pOp->p2, -
251d5 31 2c 20 31 29 3b 0a 20 20 69 66 28 20 75 2e 62  1, 1);.  if( u.b
251d6 61 2e 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20  a.pCx==0 ) goto 
251d7 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e 62 61 2e 70  no_mem;.  u.ba.p
251d8 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70  Cx->pKeyInfo = p
251d9 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b  Op->p4.pKeyInfo;
251da 0a 20 20 75 2e 62 61 2e 70 43 78 2d 3e 70 4b 65  .  u.ba.pCx->pKe
251db 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43  yInfo->enc = ENC
251dc 28 70 2d 3e 64 62 29 3b 0a 20 20 75 2e 62 61 2e  (p->db);.  u.ba.
251dd 70 43 78 2d 3e 69 73 53 6f 72 74 65 72 20 3d 20  pCx->isSorter = 
251de 31 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  1;.  rc = sqlite
251df 33 56 64 62 65 53 6f 72 74 65 72 49 6e 69 74 28  3VdbeSorterInit(
251e0 64 62 2c 20 75 2e 62 61 2e 70 43 78 29 3b 0a 23  db, u.ba.pCx);.#
251e1 65 6c 73 65 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f  else.  pOp->opco
251e2 64 65 20 3d 20 4f 50 5f 4f 70 65 6e 45 70 68 65  de = OP_OpenEphe
251e3 6d 65 72 61 6c 3b 0a 20 20 70 63 2d 2d 3b 0a 23  meral;.  pc--;.#
251e4 65 6e 64 69 66 0a 20 20 62 72 65 61 6b 3b 0a 7d  endif.  break;.}
251e5 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65  ../* Opcode: Ope
251e6 6e 50 73 65 75 64 6f 20 50 31 20 50 32 20 50 33  nPseudo P1 P2 P3
251e7 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e   * P5.**.** Open
251e8 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 74 68   a new cursor th
251e9 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66  at points to a f
251ea 61 6b 65 20 74 61 62 6c 65 20 74 68 61 74 20 63  ake table that c
251eb 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65  ontains a single
251ec 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61 74 61 2e  .** row of data.
251ed 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66    The content of
251ee 20 74 68 61 74 20 6f 6e 65 20 72 6f 77 20 69 6e   that one row in
251ef 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
251f0 6d 65 6d 6f 72 79 0a 2a 2a 20 72 65 67 69 73 74  memory.** regist
251f1 65 72 20 50 32 20 77 68 65 6e 20 50 35 3d 3d 30  er P2 when P5==0
251f2 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
251f3 73 2c 20 63 75 72 73 6f 72 20 50 31 20 62 65 63  s, cursor P1 bec
251f4 6f 6d 65 73 20 61 6e 20 61 6c 69 61 73 20 66 6f  omes an alias fo
251f5 72 20 74 68 65 20 0a 2a 2a 20 4d 45 4d 5f 42 6c  r the .** MEM_Bl
251f6 6f 62 20 63 6f 6e 74 65 6e 74 20 63 6f 6e 74 61  ob content conta
251f7 69 6e 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ined in register
251f8 20 50 32 2e 20 20 57 68 65 6e 20 50 35 3d 3d 31   P2.  When P5==1
251f9 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 72 6f  , then the.** ro
251fa 77 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64  w is represented
251fb 20 62 79 20 50 33 20 63 6f 6e 73 65 63 75 74 69   by P3 consecuti
251fc 76 65 20 72 65 67 69 73 74 65 72 73 20 62 65 67  ve registers beg
251fd 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 32 2e 0a  inning with P2..
251fe 2a 2a 0a 2a 2a 20 41 20 70 73 65 75 64 6f 2d 74  **.** A pseudo-t
251ff 61 62 6c 65 20 63 72 65 61 74 65 64 20 62 79 20  able created by 
25200 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75  this opcode is u
25201 73 65 64 20 74 6f 20 68 6f 6c 64 20 61 20 73 69  sed to hold a si
25202 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 70  ngle.** row outp
25203 75 74 20 66 72 6f 6d 20 74 68 65 20 73 6f 72 74  ut from the sort
25204 65 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 72  er so that the r
25205 6f 77 20 63 61 6e 20 62 65 20 64 65 63 6f 6d 70  ow can be decomp
25206 6f 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64  osed into.** ind
25207 69 76 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 20  ividual columns 
25208 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f 6c  using the OP_Col
25209 75 6d 6e 20 6f 70 63 6f 64 65 2e 20 20 54 68 65  umn opcode.  The
2520a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64   OP_Column opcod
2520b 65 0a 2a 2a 20 69 73 20 74 68 65 20 6f 6e 6c 79  e.** is the only
2520c 20 63 75 72 73 6f 72 20 6f 70 63 6f 64 65 20 74   cursor opcode t
2520d 68 61 74 20 77 6f 72 6b 73 20 77 69 74 68 20 61  hat works with a
2520e 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a   pseudo-table..*
2520f 2a 0a 2a 2a 20 50 33 20 69 73 20 74 68 65 20 6e  *.** P3 is the n
25210 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20  umber of fields 
25211 69 6e 20 74 68 65 20 72 65 63 6f 72 64 73 20 74  in the records t
25212 68 61 74 20 77 69 6c 6c 20 62 65 20 73 74 6f 72  hat will be stor
25213 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 73 65  ed by.** the pse
25214 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61  udo-table..*/.ca
25215 73 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f  se OP_OpenPseudo
25216 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  : {.#if 0  /* lo
25217 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
25218 76 65 64 20 69 6e 74 6f 20 75 2e 62 62 20 2a 2f  ved into u.bb */
25219 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
2521a 43 78 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  Cx;.#endif /* lo
2521b 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
2521c 76 65 64 20 69 6e 74 6f 20 75 2e 62 62 20 2a 2f  ved into u.bb */
2521d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
2521e 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 62 62  >p1>=0 );.  u.bb
2521f 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43  .pCx = allocateC
25220 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31  ursor(p, pOp->p1
25221 2c 20 70 4f 70 2d 3e 70 33 2c 20 2d 31 2c 20 30  , pOp->p3, -1, 0
25222 29 3b 0a 20 20 69 66 28 20 75 2e 62 62 2e 70 43  );.  if( u.bb.pC
25223 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  x==0 ) goto no_m
25224 65 6d 3b 0a 20 20 75 2e 62 62 2e 70 43 78 2d 3e  em;.  u.bb.pCx->
25225 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 75  nullRow = 1;.  u
25226 2e 62 62 2e 70 43 78 2d 3e 70 73 65 75 64 6f 54  .bb.pCx->pseudoT
25227 61 62 6c 65 52 65 67 20 3d 20 70 4f 70 2d 3e 70  ableReg = pOp->p
25228 32 3b 0a 20 20 75 2e 62 62 2e 70 43 78 2d 3e 69  2;.  u.bb.pCx->i
25229 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 75 2e  sTable = 1;.  u.
2522a 62 62 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78 20  bb.pCx->isIndex 
2522b 3d 20 30 3b 0a 20 20 75 2e 62 62 2e 70 43 78 2d  = 0;.  u.bb.pCx-
2522c 3e 6d 75 6c 74 69 50 73 65 75 64 6f 20 3d 20 70  >multiPseudo = p
2522d 4f 70 2d 3e 70 35 3b 0a 20 20 62 72 65 61 6b 3b  Op->p5;.  break;
2522e 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  .}../* Opcode: C
2522f 6c 6f 73 65 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  lose P1 * * * *.
25230 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75  **.** Close a cu
25231 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 20  rsor previously 
25232 6f 70 65 6e 65 64 20 61 73 20 50 31 2e 20 20 49  opened as P1.  I
25233 66 20 50 31 20 69 73 20 6e 6f 74 0a 2a 2a 20 63  f P1 is not.** c
25234 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 2c 20 74  urrently open, t
25235 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
25236 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63  is a no-op..*/.c
25237 61 73 65 20 4f 50 5f 43 6c 6f 73 65 3a 20 7b 0a  ase OP_Close: {.
25238 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
25239 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
2523a 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
2523b 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43  sqlite3VdbeFreeC
2523c 75 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73  ursor(p, p->apCs
2523d 72 5b 70 4f 70 2d 3e 70 31 5d 29 3b 0a 20 20 70  r[pOp->p1]);.  p
2523e 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
2523f 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   = 0;.  break;.}
25240 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65  ../* Opcode: See
25241 6b 47 65 20 50 31 20 50 32 20 50 33 20 50 34 20  kGe P1 P2 P3 P4 
25242 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f  *.**.** If curso
25243 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61  r P1 refers to a
25244 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54  n SQL table (B-T
25245 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e  ree that uses in
25246 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a  teger keys), .**
25247 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69   use the value i
25248 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73  n register P3 as
25249 20 74 68 65 20 6b 65 79 2e 20 20 49 66 20 63 75   the key.  If cu
2524a 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a  rsor P1 refers .
2524b 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64  ** to an SQL ind
2524c 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74  ex, then P3 is t
2524d 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61  he first in an a
2524e 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73  rray of P4 regis
2524f 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72  ters .** that ar
25250 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70  e used as an unp
25251 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e  acked index key.
25252 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69   .**.** Repositi
25253 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20  on cursor P1 so 
25254 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20  that  it points 
25255 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20  to the smallest 
25256 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69  entry that .** i
25257 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  s greater than o
25258 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b  r equal to the k
25259 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65  ey value. If the
2525a 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64  re are no record
2525b 73 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68  s .** greater th
2525c 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
2525d 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73  he key and P2 is
2525e 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20   not zero, then 
2525f 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a  jump to P2..**.*
25260 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e  * See also: Foun
25261 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73  d, NotFound, Dis
25262 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53  tinct, SeekLt, S
25263 65 65 6b 47 74 2c 20 53 65 65 6b 4c 65 0a 2a 2f  eekGt, SeekLe.*/
25264 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b  ./* Opcode: Seek
25265 47 74 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  Gt P1 P2 P3 P4 *
25266 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72  .**.** If cursor
25267 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e   P1 refers to an
25268 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72   SQL table (B-Tr
25269 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74  ee that uses int
2526a 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20  eger keys), .** 
2526b 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  use the value in
2526c 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20   register P3 as 
2526d 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72  a key. If cursor
2526e 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74   P1 refers .** t
2526f 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20  o an SQL index, 
25270 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66  then P3 is the f
25271 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
25272 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73   of P4 registers
25273 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73   .** that are us
25274 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65  ed as an unpacke
25275 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a  d index key. .**
25276 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63  .** Reposition c
25277 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74  ursor P1 so that
25278 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74    it points to t
25279 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72  he smallest entr
2527a 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72  y that .** is gr
2527b 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6b  eater than the k
2527c 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65  ey value. If the
2527d 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64  re are no record
2527e 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 0a  s greater than .
2527f 2a 2a 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50  ** the key and P
25280 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74  2 is not zero, t
25281 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a  hen jump to P2..
25282 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
25283 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c  Found, NotFound,
25284 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 4c   Distinct, SeekL
25285 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c  t, SeekGe, SeekL
25286 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  e.*/./* Opcode: 
25287 53 65 65 6b 4c 74 20 50 31 20 50 32 20 50 33 20  SeekLt P1 P2 P3 
25288 50 34 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 66 20 63  P4 * .**.** If c
25289 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20  ursor P1 refers 
2528a 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20  to an SQL table 
2528b 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 73 65  (B-Tree that use
2528c 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c  s integer keys),
2528d 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c   .** use the val
2528e 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
2528f 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63  3 as a key. If c
25290 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20  ursor P1 refers 
25291 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e  .** to an SQL in
25292 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20  dex, then P3 is 
25293 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20  the first in an 
25294 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69  array of P4 regi
25295 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61  sters .** that a
25296 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e  re used as an un
25297 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79  packed index key
25298 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74  . .**.** Reposit
25299 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f  ion cursor P1 so
2529a 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73   that  it points
2529b 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20   to the largest 
2529c 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69  entry that .** i
2529d 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20  s less than the 
2529e 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68  key value. If th
2529f 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72  ere are no recor
252a0 64 73 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a 2a  ds less than .**
252a1 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20   the key and P2 
252a2 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
252a3 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a  n jump to P2..**
252a4 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f  .** See also: Fo
252a5 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44  und, NotFound, D
252a6 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c  istinct, SeekGt,
252a7 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a   SeekGe, SeekLe.
252a8 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65  */./* Opcode: Se
252a9 65 6b 4c 65 20 50 31 20 50 32 20 50 33 20 50 34  ekLe P1 P2 P3 P4
252aa 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73   *.**.** If curs
252ab 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20  or P1 refers to 
252ac 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d  an SQL table (B-
252ad 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69  Tree that uses i
252ae 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a  nteger keys), .*
252af 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20  * use the value 
252b0 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61  in register P3 a
252b1 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73  s a key. If curs
252b2 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a  or P1 refers .**
252b3 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78   to an SQL index
252b4 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65  , then P3 is the
252b5 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72   first in an arr
252b6 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65  ay of P4 registe
252b7 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  rs .** that are 
252b8 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63  used as an unpac
252b9 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a  ked index key. .
252ba 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e  **.** Reposition
252bb 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68   cursor P1 so th
252bc 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  at it points to 
252bd 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72  the largest entr
252be 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65  y that .** is le
252bf 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
252c0 20 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75   to the key valu
252c1 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20  e. If there are 
252c2 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 6c  no records .** l
252c3 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ess than or equa
252c4 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64  l to the key and
252c5 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c   P2 is not zero,
252c6 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32   then jump to P2
252c7 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
252c8 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e  : Found, NotFoun
252c9 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65  d, Distinct, See
252ca 6b 47 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65  kGt, SeekGe, See
252cb 6b 4c 74 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  kLt.*/.case OP_S
252cc 65 65 6b 4c 74 3a 20 20 20 20 20 20 20 20 20 2f  eekLt:         /
252cd 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63  * jump, in3 */.c
252ce 61 73 65 20 4f 50 5f 53 65 65 6b 4c 65 3a 20 20  ase OP_SeekLe:  
252cf 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20         /* jump, 
252d0 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  in3 */.case OP_S
252d1 65 65 6b 47 65 3a 20 20 20 20 20 20 20 20 20 2f  eekGe:         /
252d2 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63  * jump, in3 */.c
252d3 61 73 65 20 4f 50 5f 53 65 65 6b 47 74 3a 20 7b  ase OP_SeekGt: {
252d4 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20         /* jump, 
252d5 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  in3 */.#if 0  /*
252d6 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
252d7 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 63   moved into u.bc
252d8 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20   */.  int res;. 
252d9 20 69 6e 74 20 6f 63 3b 0a 20 20 56 64 62 65 43   int oc;.  VdbeC
252da 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 55 6e 70  ursor *pC;.  Unp
252db 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20  ackedRecord r;. 
252dc 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 69   int nField;.  i
252dd 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 2f 2a  64 iKey;      /*
252de 20 54 68 65 20 72 6f 77 69 64 20 77 65 20 61 72   The rowid we ar
252df 65 20 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f 0a  e to seek to */.
252e0 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
252e1 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
252e2 69 6e 74 6f 20 75 2e 62 63 20 2a 2f 0a 0a 20 20  into u.bc */..  
252e3 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
252e4 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d  =0 && pOp->p1<p-
252e5 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73  >nCursor );.  as
252e6 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 21 3d 30  sert( pOp->p2!=0
252e7 20 29 3b 0a 20 20 75 2e 62 63 2e 70 43 20 3d 20   );.  u.bc.pC = 
252e8 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
252e9 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  ];.  assert( u.b
252ea 63 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73  c.pC!=0 );.  ass
252eb 65 72 74 28 20 75 2e 62 63 2e 70 43 2d 3e 70 73  ert( u.bc.pC->ps
252ec 65 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20  eudoTableReg==0 
252ed 29 3b 0a 20 20 61 73 73 65 72 74 28 20 4f 50 5f  );.  assert( OP_
252ee 53 65 65 6b 4c 65 20 3d 3d 20 4f 50 5f 53 65 65  SeekLe == OP_See
252ef 6b 4c 74 2b 31 20 29 3b 0a 20 20 61 73 73 65 72  kLt+1 );.  asser
252f0 74 28 20 4f 50 5f 53 65 65 6b 47 65 20 3d 3d 20  t( OP_SeekGe == 
252f1 4f 50 5f 53 65 65 6b 4c 74 2b 32 20 29 3b 0a 20  OP_SeekLt+2 );. 
252f2 20 61 73 73 65 72 74 28 20 4f 50 5f 53 65 65 6b   assert( OP_Seek
252f3 47 74 20 3d 3d 20 4f 50 5f 53 65 65 6b 4c 74 2b  Gt == OP_SeekLt+
252f4 33 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  3 );.  assert( u
252f5 2e 62 63 2e 70 43 2d 3e 69 73 4f 72 64 65 72 65  .bc.pC->isOrdere
252f6 64 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59  d );.  if( ALWAY
252f7 53 28 75 2e 62 63 2e 70 43 2d 3e 70 43 75 72 73  S(u.bc.pC->pCurs
252f8 6f 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 75 2e  or!=0) ){.    u.
252f9 62 63 2e 6f 63 20 3d 20 70 4f 70 2d 3e 6f 70 63  bc.oc = pOp->opc
252fa 6f 64 65 3b 0a 20 20 20 20 75 2e 62 63 2e 70 43  ode;.    u.bc.pC
252fb 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20  ->nullRow = 0;. 
252fc 20 20 20 69 66 28 20 75 2e 62 63 2e 70 43 2d 3e     if( u.bc.pC->
252fd 69 73 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  isTable ){.     
252fe 20 2f 2a 20 54 68 65 20 69 6e 70 75 74 20 76 61   /* The input va
252ff 6c 75 65 20 69 6e 20 50 33 20 6d 69 67 68 74 20  lue in P3 might 
25300 62 65 20 6f 66 20 61 6e 79 20 74 79 70 65 3a 20  be of any type: 
25301 69 6e 74 65 67 65 72 2c 20 72 65 61 6c 2c 20 73  integer, real, s
25302 74 72 69 6e 67 2c 0a 20 20 20 20 20 20 2a 2a 20  tring,.      ** 
25303 62 6c 6f 62 2c 20 6f 72 20 4e 55 4c 4c 2e 20 20  blob, or NULL.  
25304 42 75 74 20 69 74 20 6e 65 65 64 73 20 74 6f 20  But it needs to 
25305 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65  be an integer be
25306 66 6f 72 65 20 77 65 20 63 61 6e 20 64 6f 0a 20  fore we can do. 
25307 20 20 20 20 20 2a 2a 20 74 68 65 20 73 65 65 6b       ** the seek
25308 2c 20 73 6f 20 63 6f 76 65 72 74 20 69 74 2e 20  , so covert it. 
25309 2a 2f 0a 20 20 20 20 20 20 70 49 6e 33 20 3d 20  */.      pIn3 = 
2530a 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  &aMem[pOp->p3];.
2530b 20 20 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72        applyNumer
2530c 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e 33 29  icAffinity(pIn3)
2530d 3b 0a 20 20 20 20 20 20 75 2e 62 63 2e 69 4b 65  ;.      u.bc.iKe
2530e 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  y = sqlite3VdbeI
2530f 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 3b 0a 20  ntValue(pIn3);. 
25310 20 20 20 20 20 75 2e 62 63 2e 70 43 2d 3e 72 6f       u.bc.pC->ro
25311 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
25312 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
25313 20 50 33 20 76 61 6c 75 65 20 63 6f 75 6c 64 20   P3 value could 
25314 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64  not be converted
25315 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72   into an integer
25316 20 77 69 74 68 6f 75 74 0a 20 20 20 20 20 20 2a   without.      *
25317 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d  * loss of inform
25318 61 74 69 6f 6e 2c 20 74 68 65 6e 20 73 70 65 63  ation, then spec
25319 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 20 69  ial processing i
2531a 73 20 72 65 71 75 69 72 65 64 2e 2e 2e 20 2a 2f  s required... */
2531b 0a 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33  .      if( (pIn3
2531c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
2531d 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  t)==0 ){.       
2531e 20 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67   if( (pIn3->flag
2531f 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30  s & MEM_Real)==0
25320 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
25321 20 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65   If the P3 value
25322 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65   cannot be conve
25323 72 74 65 64 20 69 6e 74 6f 20 61 6e 79 20 6b 69  rted into any ki
25324 6e 64 20 6f 66 20 61 20 6e 75 6d 62 65 72 2c 0a  nd of a number,.
25325 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65            ** the
25326 6e 20 74 68 65 20 73 65 65 6b 20 69 73 20 6e 6f  n the seek is no
25327 74 20 70 6f 73 73 69 62 6c 65 2c 20 73 6f 20 6a  t possible, so j
25328 75 6d 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20  ump to P2 */.   
25329 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d         pc = pOp-
2532a 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20  >p2 - 1;.       
2532b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2532c 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 49    }.        /* I
2532d 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20  f we reach this 
2532e 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20  point, then the 
2532f 50 33 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65  P3 value must be
25330 20 61 20 66 6c 6f 61 74 69 6e 67 0a 20 20 20 20   a floating.    
25331 20 20 20 20 2a 2a 20 70 6f 69 6e 74 20 6e 75 6d      ** point num
25332 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ber. */.        
25333 61 73 73 65 72 74 28 20 28 70 49 6e 33 2d 3e 66  assert( (pIn3->f
25334 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29  lags & MEM_Real)
25335 21 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20 20 20  !=0 );..        
25336 69 66 28 20 75 2e 62 63 2e 69 4b 65 79 3d 3d 53  if( u.bc.iKey==S
25337 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 26 26  MALLEST_INT64 &&
25338 20 28 70 49 6e 33 2d 3e 72 3c 28 64 6f 75 62 6c   (pIn3->r<(doubl
25339 65 29 75 2e 62 63 2e 69 4b 65 79 20 7c 7c 20 70  e)u.bc.iKey || p
2533a 49 6e 33 2d 3e 72 3e 30 29 20 29 7b 0a 20 20 20  In3->r>0) ){.   
2533b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 33         /* The P3
2533c 20 76 61 6c 75 65 20 69 73 20 74 6f 6f 20 6c 61   value is too la
2533d 72 67 65 20 69 6e 20 6d 61 67 6e 69 74 75 64 65  rge in magnitude
2533e 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 65 64   to be expressed
2533f 20 61 73 20 61 6e 0a 20 20 20 20 20 20 20 20 20   as an.         
25340 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a   ** integer. */.
25341 20 20 20 20 20 20 20 20 20 20 75 2e 62 63 2e 72            u.bc.r
25342 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  es = 1;.        
25343 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 3c 30 20    if( pIn3->r<0 
25344 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
25345 66 28 20 75 2e 62 63 2e 6f 63 3e 3d 4f 50 5f 53  f( u.bc.oc>=OP_S
25346 65 65 6b 47 65 20 29 7b 20 20 61 73 73 65 72 74  eekGe ){  assert
25347 28 20 75 2e 62 63 2e 6f 63 3d 3d 4f 50 5f 53 65  ( u.bc.oc==OP_Se
25348 65 6b 47 65 20 7c 7c 20 75 2e 62 63 2e 6f 63 3d  ekGe || u.bc.oc=
25349 3d 4f 50 5f 53 65 65 6b 47 74 20 29 3b 0a 20 20  =OP_SeekGt );.  
2534a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
2534b 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72   sqlite3BtreeFir
2534c 73 74 28 75 2e 62 63 2e 70 43 2d 3e 70 43 75 72  st(u.bc.pC->pCur
2534d 73 6f 72 2c 20 26 75 2e 62 63 2e 72 65 73 29 3b  sor, &u.bc.res);
2534e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
2534f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
25350 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
25351 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
25352 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
25353 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
25354 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 63 2e         if( u.bc.
25355 6f 63 3c 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b  oc<=OP_SeekLe ){
25356 20 20 61 73 73 65 72 74 28 20 75 2e 62 63 2e 6f    assert( u.bc.o
25357 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20  c==OP_SeekLt || 
25358 75 2e 62 63 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b  u.bc.oc==OP_Seek
25359 4c 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Le );.          
2535a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2535b 42 74 72 65 65 4c 61 73 74 28 75 2e 62 63 2e 70  BtreeLast(u.bc.p
2535c 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 62  C->pCursor, &u.b
2535d 63 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20  c.res);.        
2535e 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
2535f 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61  LITE_OK ) goto a
25360 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
25361 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  r;.            }
25362 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
25363 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 63 2e         if( u.bc.
25364 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  res ){.         
25365 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
25366 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  - 1;.          }
25367 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
25368 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
25369 69 66 28 20 75 2e 62 63 2e 6f 63 3d 3d 4f 50 5f  if( u.bc.oc==OP_
2536a 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 62 63 2e 6f  SeekLt || u.bc.o
2536b 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b 0a  c==OP_SeekGe ){.
2536c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
2536d 20 74 68 65 20 63 65 69 6c 69 6e 67 28 29 20 66   the ceiling() f
2536e 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65  unction to conve
2536f 72 74 20 72 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a  rt real->int */.
25370 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49            if( pI
25371 6e 33 2d 3e 72 20 3e 20 28 64 6f 75 62 6c 65 29  n3->r > (double)
25372 75 2e 62 63 2e 69 4b 65 79 20 29 20 75 2e 62 63  u.bc.iKey ) u.bc
25373 2e 69 4b 65 79 2b 2b 3b 0a 20 20 20 20 20 20 20  .iKey++;.       
25374 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
25375 20 20 2f 2a 20 55 73 65 20 74 68 65 20 66 6c 6f    /* Use the flo
25376 6f 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f  or() function to
25377 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69   convert real->i
25378 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  nt */.          
25379 61 73 73 65 72 74 28 20 75 2e 62 63 2e 6f 63 3d  assert( u.bc.oc=
2537a 3d 4f 50 5f 53 65 65 6b 4c 65 20 7c 7c 20 75 2e  =OP_SeekLe || u.
2537b 62 63 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74  bc.oc==OP_SeekGt
2537c 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66   );.          if
2537d 28 20 70 49 6e 33 2d 3e 72 20 3c 20 28 64 6f 75  ( pIn3->r < (dou
2537e 62 6c 65 29 75 2e 62 63 2e 69 4b 65 79 20 29 20  ble)u.bc.iKey ) 
2537f 75 2e 62 63 2e 69 4b 65 79 2d 2d 3b 0a 20 20 20  u.bc.iKey--;.   
25380 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
25381 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
25382 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61  3BtreeMovetoUnpa
25383 63 6b 65 64 28 75 2e 62 63 2e 70 43 2d 3e 70 43  cked(u.bc.pC->pC
25384 75 72 73 6f 72 2c 20 30 2c 20 28 75 36 34 29 75  ursor, 0, (u64)u
25385 2e 62 63 2e 69 4b 65 79 2c 20 30 2c 20 26 75 2e  .bc.iKey, 0, &u.
25386 62 63 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 69  bc.res);.      i
25387 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
25388 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
25389 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
2538a 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ror;.      }.   
2538b 20 20 20 69 66 28 20 75 2e 62 63 2e 72 65 73 3d     if( u.bc.res=
2538c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e  =0 ){.        u.
2538d 62 63 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  bc.pC->rowidIsVa
2538e 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  lid = 1;.       
2538f 20 75 2e 62 63 2e 70 43 2d 3e 6c 61 73 74 52 6f   u.bc.pC->lastRo
25390 77 69 64 20 3d 20 75 2e 62 63 2e 69 4b 65 79 3b  wid = u.bc.iKey;
25391 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
25392 73 65 7b 0a 20 20 20 20 20 20 75 2e 62 63 2e 6e  se{.      u.bc.n
25393 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e  Field = pOp->p4.
25394 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  i;.      assert(
25395 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34   pOp->p4type==P4
25396 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20 20  _INT32 );.      
25397 61 73 73 65 72 74 28 20 75 2e 62 63 2e 6e 46 69  assert( u.bc.nFi
25398 65 6c 64 3e 30 20 29 3b 0a 20 20 20 20 20 20 75  eld>0 );.      u
25399 2e 62 63 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d  .bc.r.pKeyInfo =
2539a 20 75 2e 62 63 2e 70 43 2d 3e 70 4b 65 79 49 6e   u.bc.pC->pKeyIn
2539b 66 6f 3b 0a 20 20 20 20 20 20 75 2e 62 63 2e 72  fo;.      u.bc.r
2539c 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 75  .nField = (u16)u
2539d 2e 62 63 2e 6e 46 69 65 6c 64 3b 0a 0a 20 20 20  .bc.nField;..   
2539e 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 6c     /* The next l
2539f 69 6e 65 20 6f 66 20 63 6f 64 65 20 63 6f 6d 70  ine of code comp
253a0 75 74 65 73 20 61 73 20 66 6f 6c 6c 6f 77 73 2c  utes as follows,
253a1 20 6f 6e 6c 79 20 66 61 73 74 65 72 3a 0a 20 20   only faster:.  
253a2 20 20 20 20 2a 2a 20 20 20 69 66 28 20 75 2e 62      **   if( u.b
253a3 63 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20  c.oc==OP_SeekGt 
253a4 7c 7c 20 75 2e 62 63 2e 6f 63 3d 3d 4f 50 5f 53  || u.bc.oc==OP_S
253a5 65 65 6b 4c 65 20 29 7b 0a 20 20 20 20 20 20 2a  eekLe ){.      *
253a6 2a 20 20 20 20 20 75 2e 62 63 2e 72 2e 66 6c 61  *     u.bc.r.fla
253a7 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 49 4e  gs = UNPACKED_IN
253a8 43 52 4b 45 59 3b 0a 20 20 20 20 20 20 2a 2a 20  CRKEY;.      ** 
253a9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a    }else{.      *
253aa 2a 20 20 20 20 20 75 2e 62 63 2e 72 2e 66 6c 61  *     u.bc.r.fla
253ab 67 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 2a 2a  gs = 0;.      **
253ac 20 20 20 7d 0a 20 20 20 20 20 20 2a 2f 0a 20 20     }.      */.  
253ad 20 20 20 20 75 2e 62 63 2e 72 2e 66 6c 61 67 73      u.bc.r.flags
253ae 20 3d 20 28 75 31 36 29 28 55 4e 50 41 43 4b 45   = (u16)(UNPACKE
253af 44 5f 49 4e 43 52 4b 45 59 20 2a 20 28 31 20 26  D_INCRKEY * (1 &
253b0 20 28 75 2e 62 63 2e 6f 63 20 2d 20 4f 50 5f 53   (u.bc.oc - OP_S
253b1 65 65 6b 4c 74 29 29 29 3b 0a 20 20 20 20 20 20  eekLt)));.      
253b2 61 73 73 65 72 74 28 20 75 2e 62 63 2e 6f 63 21  assert( u.bc.oc!
253b3 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 75 2e  =OP_SeekGt || u.
253b4 62 63 2e 72 2e 66 6c 61 67 73 3d 3d 55 4e 50 41  bc.r.flags==UNPA
253b5 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 29 3b 0a  CKED_INCRKEY );.
253b6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
253b7 62 63 2e 6f 63 21 3d 4f 50 5f 53 65 65 6b 4c 65  bc.oc!=OP_SeekLe
253b8 20 7c 7c 20 75 2e 62 63 2e 72 2e 66 6c 61 67 73   || u.bc.r.flags
253b9 3d 3d 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b  ==UNPACKED_INCRK
253ba 45 59 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  EY );.      asse
253bb 72 74 28 20 75 2e 62 63 2e 6f 63 21 3d 4f 50 5f  rt( u.bc.oc!=OP_
253bc 53 65 65 6b 47 65 20 7c 7c 20 75 2e 62 63 2e 72  SeekGe || u.bc.r
253bd 2e 66 6c 61 67 73 3d 3d 30 20 29 3b 0a 20 20 20  .flags==0 );.   
253be 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 63 2e     assert( u.bc.
253bf 6f 63 21 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c  oc!=OP_SeekLt ||
253c0 20 75 2e 62 63 2e 72 2e 66 6c 61 67 73 3d 3d 30   u.bc.r.flags==0
253c1 20 29 3b 0a 0a 20 20 20 20 20 20 75 2e 62 63 2e   );..      u.bc.
253c2 72 2e 61 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70  r.aMem = &aMem[p
253c3 4f 70 2d 3e 70 33 5d 3b 0a 23 69 66 64 65 66 20  Op->p3];.#ifdef 
253c4 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
253c5 20 20 20 7b 20 69 6e 74 20 69 3b 20 66 6f 72 28     { int i; for(
253c6 69 3d 30 3b 20 69 3c 75 2e 62 63 2e 72 2e 6e 46  i=0; i<u.bc.r.nF
253c7 69 65 6c 64 3b 20 69 2b 2b 29 20 61 73 73 65 72  ield; i++) asser
253c8 74 28 20 6d 65 6d 49 73 56 61 6c 69 64 28 26 75  t( memIsValid(&u
253c9 2e 62 63 2e 72 2e 61 4d 65 6d 5b 69 5d 29 20 29  .bc.r.aMem[i]) )
253ca 3b 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  ; }.#endif.     
253cb 20 45 78 70 61 6e 64 42 6c 6f 62 28 75 2e 62 63   ExpandBlob(u.bc
253cc 2e 72 2e 61 4d 65 6d 29 3b 0a 20 20 20 20 20 20  .r.aMem);.      
253cd 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
253ce 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
253cf 75 2e 62 63 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.bc.pC->pCursor
253d0 2c 20 26 75 2e 62 63 2e 72 2c 20 30 2c 20 30 2c  , &u.bc.r, 0, 0,
253d1 20 26 75 2e 62 63 2e 72 65 73 29 3b 0a 20 20 20   &u.bc.res);.   
253d2 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
253d3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
253d4 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
253d5 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d  o_error;.      }
253d6 0a 20 20 20 20 20 20 75 2e 62 63 2e 70 43 2d 3e  .      u.bc.pC->
253d7 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30  rowidIsValid = 0
253d8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 63  ;.    }.    u.bc
253d9 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  .pC->deferredMov
253da 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62  eto = 0;.    u.b
253db 63 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75  c.pC->cacheStatu
253dc 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
253dd 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
253de 45 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  EST.    sqlite3_
253df 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a  search_count++;.
253e0 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 75  #endif.    if( u
253e1 2e 62 63 2e 6f 63 3e 3d 4f 50 5f 53 65 65 6b 47  .bc.oc>=OP_SeekG
253e2 65 20 29 7b 20 20 61 73 73 65 72 74 28 20 75 2e  e ){  assert( u.
253e3 62 63 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65  bc.oc==OP_SeekGe
253e4 20 7c 7c 20 75 2e 62 63 2e 6f 63 3d 3d 4f 50 5f   || u.bc.oc==OP_
253e5 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 20  SeekGt );.      
253e6 69 66 28 20 75 2e 62 63 2e 72 65 73 3c 30 20 7c  if( u.bc.res<0 |
253e7 7c 20 28 75 2e 62 63 2e 72 65 73 3d 3d 30 20 26  | (u.bc.res==0 &
253e8 26 20 75 2e 62 63 2e 6f 63 3d 3d 4f 50 5f 53 65  & u.bc.oc==OP_Se
253e9 65 6b 47 74 29 20 29 7b 0a 20 20 20 20 20 20 20  ekGt) ){.       
253ea 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
253eb 65 65 4e 65 78 74 28 75 2e 62 63 2e 70 43 2d 3e  eeNext(u.bc.pC->
253ec 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 63 2e 72  pCursor, &u.bc.r
253ed 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  es);.        if(
253ee 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
253ef 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
253f0 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to_error;.      
253f1 20 20 75 2e 62 63 2e 70 43 2d 3e 72 6f 77 69 64    u.bc.pC->rowid
253f2 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  IsValid = 0;.   
253f3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
253f4 20 20 75 2e 62 63 2e 72 65 73 20 3d 20 30 3b 0a    u.bc.res = 0;.
253f5 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
253f6 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e{.      assert(
253f7 20 75 2e 62 63 2e 6f 63 3d 3d 4f 50 5f 53 65 65   u.bc.oc==OP_See
253f8 6b 4c 74 20 7c 7c 20 75 2e 62 63 2e 6f 63 3d 3d  kLt || u.bc.oc==
253f9 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 20 20  OP_SeekLe );.   
253fa 20 20 20 69 66 28 20 75 2e 62 63 2e 72 65 73 3e     if( u.bc.res>
253fb 30 20 7c 7c 20 28 75 2e 62 63 2e 72 65 73 3d 3d  0 || (u.bc.res==
253fc 30 20 26 26 20 75 2e 62 63 2e 6f 63 3d 3d 4f 50  0 && u.bc.oc==OP
253fd 5f 53 65 65 6b 4c 74 29 20 29 7b 0a 20 20 20 20  _SeekLt) ){.    
253fe 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
253ff 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 75 2e  BtreePrevious(u.
25400 62 63 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  bc.pC->pCursor, 
25401 26 75 2e 62 63 2e 72 65 73 29 3b 0a 20 20 20 20  &u.bc.res);.    
25402 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
25403 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f  TE_OK ) goto abo
25404 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
25405 0a 20 20 20 20 20 20 20 20 75 2e 62 63 2e 70 43  .        u.bc.pC
25406 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d  ->rowidIsValid =
25407 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   0;.      }else{
25408 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 2e 62 63  .        /* u.bc
25409 2e 72 65 73 20 6d 69 67 68 74 20 62 65 20 6e 65  .res might be ne
2540a 67 61 74 69 76 65 20 62 65 63 61 75 73 65 20 74  gative because t
2540b 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74  he table is empt
2540c 79 2e 20 20 43 68 65 63 6b 20 74 6f 0a 20 20 20  y.  Check to.   
2540d 20 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 74       ** see if t
2540e 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2e  his is the case.
2540f 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
25410 20 20 20 20 75 2e 62 63 2e 72 65 73 20 3d 20 73      u.bc.res = s
25411 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 75  qlite3BtreeEof(u
25412 2e 62 63 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29  .bc.pC->pCursor)
25413 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
25414 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
25415 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 69 66 28  >p2>0 );.    if(
25416 20 75 2e 62 63 2e 72 65 73 20 29 7b 0a 20 20 20   u.bc.res ){.   
25417 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
25418 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  - 1;.    }.  }el
25419 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  se{.    /* This 
2541a 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61 74 74  happens when att
2541b 65 6d 70 74 69 6e 67 20 74 6f 20 6f 70 65 6e 20  empting to open 
2541c 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 73 74  the sqlite3_mast
2541d 65 72 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20  er table.    ** 
2541e 66 6f 72 20 72 65 61 64 20 61 63 63 65 73 73 20  for read access 
2541f 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45  returns SQLITE_E
25420 4d 50 54 59 2e 20 49 6e 20 74 68 69 73 20 63 61  MPTY. In this ca
25421 73 65 20 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a  se always.    **
25422 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 28   take the jump (
25423 73 69 6e 63 65 20 74 68 65 72 65 20 61 72 65 20  since there are 
25424 6e 6f 20 72 65 63 6f 72 64 73 20 69 6e 20 74 68  no records in th
25425 65 20 74 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f  e table)..    */
25426 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
25427 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
25428 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
25429 3a 20 53 65 65 6b 20 50 31 20 50 32 20 2a 20 2a  : Seek P1 P2 * *
2542a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61   *.**.** P1 is a
2542b 6e 20 6f 70 65 6e 20 74 61 62 6c 65 20 63 75 72  n open table cur
2542c 73 6f 72 20 61 6e 64 20 50 32 20 69 73 20 61 20  sor and P2 is a 
2542d 72 6f 77 69 64 20 69 6e 74 65 67 65 72 2e 20 20  rowid integer.  
2542e 41 72 72 61 6e 67 65 0a 2a 2a 20 66 6f 72 20 50  Arrange.** for P
2542f 31 20 74 6f 20 6d 6f 76 65 20 73 6f 20 74 68 61  1 to move so tha
25430 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74  t it points to t
25431 68 65 20 72 6f 77 69 64 20 67 69 76 65 6e 20 62  he rowid given b
25432 79 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  y P2..**.** This
25433 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 64   is actually a d
25434 65 66 65 72 72 65 64 20 73 65 65 6b 2e 20 20 4e  eferred seek.  N
25435 6f 74 68 69 6e 67 20 61 63 74 75 61 6c 6c 79 20  othing actually 
25436 68 61 70 70 65 6e 73 20 75 6e 74 69 6c 0a 2a 2a  happens until.**
25437 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 75   the cursor is u
25438 73 65 64 20 74 6f 20 72 65 61 64 20 61 20 72 65  sed to read a re
25439 63 6f 72 64 2e 20 20 54 68 61 74 20 77 61 79 2c  cord.  That way,
2543a 20 69 66 20 6e 6f 20 72 65 61 64 73 0a 2a 2a 20   if no reads.** 
2543b 6f 63 63 75 72 2c 20 6e 6f 20 75 6e 6e 65 63 65  occur, no unnece
2543c 73 73 61 72 79 20 49 2f 4f 20 68 61 70 70 65 6e  ssary I/O happen
2543d 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65  s..*/.case OP_Se
2543e 65 6b 3a 20 7b 20 20 20 20 2f 2a 20 69 6e 32 20  ek: {    /* in2 
2543f 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
25440 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
25441 65 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a  ed into u.bd */.
25442 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
25443 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
25444 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
25445 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a 0a  d into u.bd */..
25446 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
25447 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
25448 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
25449 75 2e 62 64 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bd.pC = p->apC
2544a 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  sr[pOp->p1];.  a
2544b 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43 21 3d  ssert( u.bd.pC!=
2544c 30 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59  0 );.  if( ALWAY
2544d 53 28 75 2e 62 64 2e 70 43 2d 3e 70 43 75 72 73  S(u.bd.pC->pCurs
2544e 6f 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 61 73  or!=0) ){.    as
2544f 73 65 72 74 28 20 75 2e 62 64 2e 70 43 2d 3e 69  sert( u.bd.pC->i
25450 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 75 2e  sTable );.    u.
25451 62 64 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d  bd.pC->nullRow =
25452 20 30 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20 26   0;.    pIn2 = &
25453 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
25454 20 20 20 75 2e 62 64 2e 70 43 2d 3e 6d 6f 76 65     u.bd.pC->move
25455 74 6f 54 61 72 67 65 74 20 3d 20 73 71 6c 69 74  toTarget = sqlit
25456 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70  e3VdbeIntValue(p
25457 49 6e 32 29 3b 0a 20 20 20 20 75 2e 62 64 2e 70  In2);.    u.bd.p
25458 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
25459 3d 20 30 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43  = 0;.    u.bd.pC
2545a 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
2545b 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   = 1;.  }.  brea
2545c 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 4f 70 63 6f  k;.}.  ../* Opco
2545d 64 65 3a 20 46 6f 75 6e 64 20 50 31 20 50 32 20  de: Found P1 P2 
2545e 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66  P3 P4 *.**.** If
2545f 20 50 34 3d 3d 30 20 74 68 65 6e 20 72 65 67 69   P4==0 then regi
25460 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61 20  ster P3 holds a 
25461 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64  blob constructed
25462 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20   by MakeRecord. 
25463 20 49 66 0a 2a 2a 20 50 34 3e 30 20 74 68 65 6e   If.** P4>0 then
25464 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20   register P3 is 
25465 74 68 65 20 66 69 72 73 74 20 6f 66 20 50 34 20  the first of P4 
25466 72 65 67 69 73 74 65 72 73 20 74 68 61 74 20 66  registers that f
25467 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 0a  orm an unpacked.
25468 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a 2a 0a 2a 2a  ** record..**.**
25469 20 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f 6e   Cursor P1 is on
2546a 20 61 6e 20 69 6e 64 65 78 20 62 74 72 65 65 2e   an index btree.
2546b 20 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20    If the record 
2546c 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 50 33  identified by P3
2546d 20 61 6e 64 20 50 34 0a 2a 2a 20 69 73 20 61 20   and P4.** is a 
2546e 70 72 65 66 69 78 20 6f 66 20 61 6e 79 20 65 6e  prefix of any en
2546f 74 72 79 20 69 6e 20 50 31 20 74 68 65 6e 20 61  try in P1 then a
25470 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f   jump is made to
25471 20 50 32 20 61 6e 64 0a 2a 2a 20 50 31 20 69 73   P2 and.** P1 is
25472 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61   left pointing a
25473 74 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65  t the matching e
25474 6e 74 72 79 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  ntry..*/./* Opco
25475 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 20 50 31 20  de: NotFound P1 
25476 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  P2 P3 P4 *.**.**
25477 20 49 66 20 50 34 3d 3d 30 20 74 68 65 6e 20 72   If P4==0 then r
25478 65 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64 73  egister P3 holds
25479 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63   a blob construc
2547a 74 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72  ted by MakeRecor
2547b 64 2e 20 20 49 66 0a 2a 2a 20 50 34 3e 30 20 74  d.  If.** P4>0 t
2547c 68 65 6e 20 72 65 67 69 73 74 65 72 20 50 33 20  hen register P3 
2547d 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 66 20  is the first of 
2547e 50 34 20 72 65 67 69 73 74 65 72 73 20 74 68 61  P4 registers tha
2547f 74 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b  t form an unpack
25480 65 64 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a 2a  ed.** record..**
25481 20 0a 2a 2a 20 43 75 72 73 6f 72 20 50 31 20 69   .** Cursor P1 i
25482 73 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 74  s on an index bt
25483 72 65 65 2e 20 20 49 66 20 74 68 65 20 72 65 63  ree.  If the rec
25484 6f 72 64 20 69 64 65 6e 74 69 66 69 65 64 20 62  ord identified b
25485 79 20 50 33 20 61 6e 64 20 50 34 0a 2a 2a 20 69  y P3 and P4.** i
25486 73 20 6e 6f 74 20 74 68 65 20 70 72 65 66 69 78  s not the prefix
25487 20 6f 66 20 61 6e 79 20 65 6e 74 72 79 20 69 6e   of any entry in
25488 20 50 31 20 74 68 65 6e 20 61 20 6a 75 6d 70 20   P1 then a jump 
25489 69 73 20 6d 61 64 65 20 74 6f 20 50 32 2e 20 20  is made to P2.  
2548a 49 66 20 50 31 20 0a 2a 2a 20 64 6f 65 73 20 63  If P1 .** does c
2548b 6f 6e 74 61 69 6e 20 61 6e 20 65 6e 74 72 79 20  ontain an entry 
2548c 77 68 6f 73 65 20 70 72 65 66 69 78 20 6d 61 74  whose prefix mat
2548d 63 68 65 73 20 74 68 65 20 50 33 2f 50 34 20 72  ches the P3/P4 r
2548e 65 63 6f 72 64 20 74 68 65 6e 20 63 6f 6e 74 72  ecord then contr
2548f 6f 6c 0a 2a 2a 20 66 61 6c 6c 73 20 74 68 72 6f  ol.** falls thro
25490 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
25491 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e 64 20  instruction and 
25492 50 31 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74  P1 is left point
25493 69 6e 67 20 61 74 20 74 68 65 0a 2a 2a 20 6d 61  ing at the.** ma
25494 74 63 68 69 6e 67 20 65 6e 74 72 79 2e 0a 2a 2a  tching entry..**
25495 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f  .** See also: Fo
25496 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20  und, NotExists, 
25497 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65  IsUnique.*/.case
25498 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 20   OP_NotFound:   
25499 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33      /* jump, in3
2549a 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 75 6e   */.case OP_Foun
2549b 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  d: {        /* j
2549c 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20  ump, in3 */.#if 
2549d 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
2549e 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
2549f 20 75 2e 62 65 20 2a 2f 0a 20 20 69 6e 74 20 61   u.be */.  int a
254a0 6c 72 65 61 64 79 45 78 69 73 74 73 3b 0a 20 20  lreadyExists;.  
254a1 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
254a2 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 63 68 61    int res;.  cha
254a3 72 20 2a 70 46 72 65 65 3b 0a 20 20 55 6e 70 61  r *pFree;.  Unpa
254a4 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78  ckedRecord *pIdx
254a5 4b 65 79 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52  Key;.  UnpackedR
254a6 65 63 6f 72 64 20 72 3b 0a 20 20 63 68 61 72 20  ecord r;.  char 
254a7 61 54 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28  aTempRec[ROUND8(
254a8 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52  sizeof(UnpackedR
254a9 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66  ecord)) + sizeof
254aa 28 4d 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 23 65  (Mem)*3 + 7];.#e
254ab 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
254ac 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
254ad 74 6f 20 75 2e 62 65 20 2a 2f 0a 0a 23 69 66 64  to u.be */..#ifd
254ae 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
254af 20 73 71 6c 69 74 65 33 5f 66 6f 75 6e 64 5f 63   sqlite3_found_c
254b0 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 0a  ount++;.#endif..
254b1 20 20 75 2e 62 65 2e 61 6c 72 65 61 64 79 45 78    u.be.alreadyEx
254b2 69 73 74 73 20 3d 20 30 3b 0a 20 20 61 73 73 65  ists = 0;.  asse
254b3 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
254b4 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
254b5 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74  rsor );.  assert
254b6 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
254b7 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 75 2e 62  4_INT32 );.  u.b
254b8 65 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b  e.pC = p->apCsr[
254b9 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
254ba 72 74 28 20 75 2e 62 65 2e 70 43 21 3d 30 20 29  rt( u.be.pC!=0 )
254bb 3b 0a 20 20 70 49 6e 33 20 3d 20 26 61 4d 65 6d  ;.  pIn3 = &aMem
254bc 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28  [pOp->p3];.  if(
254bd 20 41 4c 57 41 59 53 28 75 2e 62 65 2e 70 43 2d   ALWAYS(u.be.pC-
254be 3e 70 43 75 72 73 6f 72 21 3d 30 29 20 29 7b 0a  >pCursor!=0) ){.
254bf 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
254c0 65 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30  e.pC->isTable==0
254c1 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   );.    if( pOp-
254c2 3e 70 34 2e 69 3e 30 20 29 7b 0a 20 20 20 20 20  >p4.i>0 ){.     
254c3 20 75 2e 62 65 2e 72 2e 70 4b 65 79 49 6e 66 6f   u.be.r.pKeyInfo
254c4 20 3d 20 75 2e 62 65 2e 70 43 2d 3e 70 4b 65 79   = u.be.pC->pKey
254c5 49 6e 66 6f 3b 0a 20 20 20 20 20 20 75 2e 62 65  Info;.      u.be
254c6 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36  .r.nField = (u16
254c7 29 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20  )pOp->p4.i;.    
254c8 20 20 75 2e 62 65 2e 72 2e 61 4d 65 6d 20 3d 20    u.be.r.aMem = 
254c9 70 49 6e 33 3b 0a 23 69 66 64 65 66 20 53 51 4c  pIn3;.#ifdef SQL
254ca 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20  ITE_DEBUG.      
254cb 7b 20 69 6e 74 20 69 3b 20 66 6f 72 28 69 3d 30  { int i; for(i=0
254cc 3b 20 69 3c 75 2e 62 65 2e 72 2e 6e 46 69 65 6c  ; i<u.be.r.nFiel
254cd 64 3b 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20  d; i++) assert( 
254ce 6d 65 6d 49 73 56 61 6c 69 64 28 26 75 2e 62 65  memIsValid(&u.be
254cf 2e 72 2e 61 4d 65 6d 5b 69 5d 29 20 29 3b 20 7d  .r.aMem[i]) ); }
254d0 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 75 2e  .#endif.      u.
254d1 62 65 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50  be.r.flags = UNP
254d2 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54  ACKED_PREFIX_MAT
254d3 43 48 3b 0a 20 20 20 20 20 20 75 2e 62 65 2e 70  CH;.      u.be.p
254d4 49 64 78 4b 65 79 20 3d 20 26 75 2e 62 65 2e 72  IdxKey = &u.be.r
254d5 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
254d6 20 20 20 75 2e 62 65 2e 70 49 64 78 4b 65 79 20     u.be.pIdxKey 
254d7 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 6c 6c  = sqlite3VdbeAll
254d8 6f 63 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  ocUnpackedRecord
254d9 28 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62 65  (.          u.be
254da 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 75  .pC->pKeyInfo, u
254db 2e 62 65 2e 61 54 65 6d 70 52 65 63 2c 20 73 69  .be.aTempRec, si
254dc 7a 65 6f 66 28 75 2e 62 65 2e 61 54 65 6d 70 52  zeof(u.be.aTempR
254dd 65 63 29 2c 20 26 75 2e 62 65 2e 70 46 72 65 65  ec), &u.be.pFree
254de 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20  .      );.      
254df 69 66 28 20 75 2e 62 65 2e 70 49 64 78 4b 65 79  if( u.be.pIdxKey
254e0 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ==0 ) goto no_me
254e1 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  m;.      assert(
254e2 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn3->flags & M
254e3 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20  EM_Blob );.     
254e4 20 61 73 73 65 72 74 28 20 28 70 49 6e 33 2d 3e   assert( (pIn3->
254e5 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f  flags & MEM_Zero
254e6 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 7a 65 72 6f  )==0 );  /* zero
254e7 62 6c 6f 62 73 20 61 6c 72 65 61 64 79 20 65 78  blobs already ex
254e8 70 61 6e 64 65 64 20 2a 2f 0a 20 20 20 20 20 20  panded */.      
254e9 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72  sqlite3VdbeRecor
254ea 64 55 6e 70 61 63 6b 28 75 2e 62 65 2e 70 43 2d  dUnpack(u.be.pC-
254eb 3e 70 4b 65 79 49 6e 66 6f 2c 20 70 49 6e 33 2d  >pKeyInfo, pIn3-
254ec 3e 6e 2c 20 70 49 6e 33 2d 3e 7a 2c 20 75 2e 62  >n, pIn3->z, u.b
254ed 65 2e 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20  e.pIdxKey);.    
254ee 20 20 75 2e 62 65 2e 70 49 64 78 4b 65 79 2d 3e    u.be.pIdxKey->
254ef 66 6c 61 67 73 20 7c 3d 20 55 4e 50 41 43 4b 45  flags |= UNPACKE
254f0 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 3b 0a  D_PREFIX_MATCH;.
254f1 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
254f2 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74  qlite3BtreeMovet
254f3 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 65 2e 70  oUnpacked(u.be.p
254f4 43 2d 3e 70 43 75 72 73 6f 72 2c 20 75 2e 62 65  C->pCursor, u.be
254f5 2e 70 49 64 78 4b 65 79 2c 20 30 2c 20 30 2c 20  .pIdxKey, 0, 0, 
254f6 26 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20 20 20  &u.be.res);.    
254f7 69 66 28 20 70 4f 70 2d 3e 70 34 2e 69 3d 3d 30  if( pOp->p4.i==0
254f8 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
254f9 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 62 65  3DbFree(db, u.be
254fa 2e 70 46 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20  .pFree);.    }. 
254fb 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
254fc 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 62 72  E_OK ){.      br
254fd 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75  eak;.    }.    u
254fe 2e 62 65 2e 61 6c 72 65 61 64 79 45 78 69 73 74  .be.alreadyExist
254ff 73 20 3d 20 28 75 2e 62 65 2e 72 65 73 3d 3d 30  s = (u.be.res==0
25500 29 3b 0a 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e  );.    u.be.pC->
25501 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d  deferredMoveto =
25502 20 30 3b 0a 20 20 20 20 75 2e 62 65 2e 70 43 2d   0;.    u.be.pC-
25503 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
25504 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a  ACHE_STALE;.  }.
25505 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
25506 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20  e==OP_Found ){. 
25507 20 20 20 69 66 28 20 75 2e 62 65 2e 61 6c 72 65     if( u.be.alre
25508 61 64 79 45 78 69 73 74 73 20 29 20 70 63 20 3d  adyExists ) pc =
25509 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
2550a 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 21  }else{.    if( !
2550b 75 2e 62 65 2e 61 6c 72 65 61 64 79 45 78 69 73  u.be.alreadyExis
2550c 74 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70  ts ) pc = pOp->p
2550d 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
2550e 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
2550f 3a 20 49 73 55 6e 69 71 75 65 20 50 31 20 50 32  : IsUnique P1 P2
25510 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43   P3 P4 *.**.** C
25511 75 72 73 6f 72 20 50 31 20 69 73 20 6f 70 65 6e  ursor P1 is open
25512 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d 74   on an index b-t
25513 72 65 65 20 2d 20 74 68 61 74 20 69 73 20 74 6f  ree - that is to
25514 20 73 61 79 2c 20 61 20 62 74 72 65 65 20 77 68   say, a btree wh
25515 69 63 68 0a 2a 2a 20 6e 6f 20 64 61 74 61 20 61  ich.** no data a
25516 6e 64 20 77 68 65 72 65 20 74 68 65 20 6b 65 79  nd where the key
25517 20 61 72 65 20 72 65 63 6f 72 64 73 20 67 65 6e   are records gen
25518 65 72 61 74 65 64 20 62 79 20 4f 50 5f 4d 61 6b  erated by OP_Mak
25519 65 52 65 63 6f 72 64 20 77 69 74 68 0a 2a 2a 20  eRecord with.** 
2551a 74 68 65 20 6c 69 73 74 20 66 69 65 6c 64 20 62  the list field b
2551b 65 69 6e 67 20 74 68 65 20 69 6e 74 65 67 65 72  eing the integer
2551c 20 52 4f 57 49 44 20 6f 66 20 74 68 65 20 65 6e   ROWID of the en
2551d 74 72 79 20 74 68 61 74 20 74 68 65 20 69 6e 64  try that the ind
2551e 65 78 0a 2a 2a 20 65 6e 74 72 79 20 72 65 66 65  ex.** entry refe
2551f 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rs to..**.** The
25520 20 50 33 20 72 65 67 69 73 74 65 72 20 63 6f 6e   P3 register con
25521 74 61 69 6e 73 20 61 6e 20 69 6e 74 65 67 65 72  tains an integer
25522 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20   record number. 
25523 43 61 6c 6c 20 74 68 69 73 20 72 65 63 6f 72 64  Call this record
25524 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 52 2e 20 52   .** number R. R
25525 65 67 69 73 74 65 72 20 50 34 20 69 73 20 74 68  egister P4 is th
25526 65 20 66 69 72 73 74 20 69 6e 20 61 20 73 65 74  e first in a set
25527 20 6f 66 20 4e 20 63 6f 6e 74 69 67 75 6f 75 73   of N contiguous
25528 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 74 68   registers.** th
25529 61 74 20 6d 61 6b 65 20 75 70 20 61 6e 20 75 6e  at make up an un
2552a 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79  packed index key
2552b 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
2552c 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 50 31  d with cursor P1
2552d 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f  ..** The value o
2552e 66 20 4e 20 63 61 6e 20 62 65 20 69 6e 66 65 72  f N can be infer
2552f 72 65 64 20 66 72 6f 6d 20 74 68 65 20 63 75 72  red from the cur
25530 73 6f 72 2e 20 4e 20 69 6e 63 6c 75 64 65 73 20  sor. N includes 
25531 74 68 65 20 72 6f 77 69 64 0a 2a 2a 20 76 61 6c  the rowid.** val
25532 75 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74  ue appended to t
25533 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e  he end of the in
25534 64 65 78 20 72 65 63 6f 72 64 2e 20 54 68 69 73  dex record. This
25535 20 72 6f 77 69 64 20 76 61 6c 75 65 20 6d 61 79   rowid value may
25536 0a 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62  .** or may not b
25537 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 52 2e  e the same as R.
25538 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66  .**.** If any of
25539 20 74 68 65 20 4e 20 72 65 67 69 73 74 65 72 73   the N registers
2553a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
2553b 72 65 67 69 73 74 65 72 20 50 34 20 63 6f 6e 74  register P4 cont
2553c 61 69 6e 73 20 61 20 4e 55 4c 4c 0a 2a 2a 20 76  ains a NULL.** v
2553d 61 6c 75 65 2c 20 6a 75 6d 70 20 69 6d 6d 65 64  alue, jump immed
2553e 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a  iately to P2..**
2553f 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74  .** Otherwise, t
25540 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
25541 63 68 65 63 6b 73 20 69 66 20 63 75 72 73 6f 72  checks if cursor
25542 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20   P1 contains an 
25543 65 6e 74 72 79 0a 2a 2a 20 77 68 65 72 65 20 74  entry.** where t
25544 68 65 20 66 69 72 73 74 20 28 4e 2d 31 29 20 66  he first (N-1) f
25545 69 65 6c 64 73 20 6d 61 74 63 68 20 62 75 74 20  ields match but 
25546 74 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65 20  the rowid value 
25547 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66  at the end.** of
25548 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79   the index entry
25549 20 69 73 20 6e 6f 74 20 52 2e 20 49 66 20 74 68   is not R. If th
2554a 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 65  ere is no such e
2554b 6e 74 72 79 2c 20 63 6f 6e 74 72 6f 6c 20 6a 75  ntry, control ju
2554c 6d 70 73 0a 2a 2a 20 74 6f 20 69 6e 73 74 72 75  mps.** to instru
2554d 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65 72 77  ction P2. Otherw
2554e 69 73 65 2c 20 74 68 65 20 72 6f 77 69 64 20 6f  ise, the rowid o
2554f 66 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e  f the conflictin
25550 67 20 69 6e 64 65 78 0a 2a 2a 20 65 6e 74 72 79  g index.** entry
25551 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20 72 65   is copied to re
25552 67 69 73 74 65 72 20 50 33 20 61 6e 64 20 63 6f  gister P3 and co
25553 6e 74 72 6f 6c 20 66 61 6c 6c 73 20 74 68 72 6f  ntrol falls thro
25554 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 0a  ugh to the next.
25555 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ** instruction..
25556 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
25557 4e 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69  NotFound, NotExi
25558 73 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61  sts, Found.*/.ca
25559 73 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 3a 20  se OP_IsUnique: 
2555a 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
2555b 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20  , in3 */.#if 0  
2555c 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
2555d 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
2555e 62 66 20 2a 2f 0a 20 20 75 31 36 20 69 69 3b 0a  bf */.  u16 ii;.
2555f 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
25560 78 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  x;.  BtCursor *p
25561 43 72 73 72 3b 0a 20 20 75 31 36 20 6e 46 69 65  Crsr;.  u16 nFie
25562 6c 64 3b 0a 20 20 4d 65 6d 20 2a 61 4d 78 3b 0a  ld;.  Mem *aMx;.
25563 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
25564 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   r;             
25565 20 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 69       /* B-Tree i
25566 6e 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 20  ndex search key 
25567 2a 2f 0a 20 20 69 36 34 20 52 3b 20 20 20 20 20  */.  i64 R;     
25568 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25569 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64          /* Rowid
2556a 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
2556b 74 65 72 20 50 33 20 2a 2f 0a 23 65 6e 64 69 66  ter P3 */.#endif
2556c 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
2556d 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
2556e 2e 62 66 20 2a 2f 0a 0a 20 20 70 49 6e 33 20 3d  .bf */..  pIn3 =
2556f 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b   &aMem[pOp->p3];
25570 0a 20 20 75 2e 62 66 2e 61 4d 78 20 3d 20 26 61  .  u.bf.aMx = &a
25571 4d 65 6d 5b 70 4f 70 2d 3e 70 34 2e 69 5d 3b 0a  Mem[pOp->p4.i];.
25572 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74    /* Assert that
25573 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 70   the values of p
25574 61 72 61 6d 65 74 65 72 73 20 50 31 20 61 6e 64  arameters P1 and
25575 20 50 34 20 61 72 65 20 69 6e 20 72 61 6e 67 65   P4 are in range
25576 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  . */.  assert( p
25577 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49  Op->p4type==P4_I
25578 4e 54 33 32 20 29 3b 0a 20 20 61 73 73 65 72 74  NT32 );.  assert
25579 28 20 70 4f 70 2d 3e 70 34 2e 69 3e 30 20 26 26  ( pOp->p4.i>0 &&
2557a 20 70 4f 70 2d 3e 70 34 2e 69 3c 3d 70 2d 3e 6e   pOp->p4.i<=p->n
2557b 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Mem );.  assert(
2557c 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
2557d 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f  Op->p1<p->nCurso
2557e 72 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64 20  r );..  /* Find 
2557f 74 68 65 20 69 6e 64 65 78 20 63 75 72 73 6f 72  the index cursor
25580 2e 20 2a 2f 0a 20 20 75 2e 62 66 2e 70 43 78 20  . */.  u.bf.pCx 
25581 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
25582 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
25583 2e 62 66 2e 70 43 78 2d 3e 64 65 66 65 72 72 65  .bf.pCx->deferre
25584 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20  dMoveto==0 );.  
25585 75 2e 62 66 2e 70 43 78 2d 3e 73 65 65 6b 52 65  u.bf.pCx->seekRe
25586 73 75 6c 74 20 3d 20 30 3b 0a 20 20 75 2e 62 66  sult = 0;.  u.bf
25587 2e 70 43 78 2d 3e 63 61 63 68 65 53 74 61 74 75  .pCx->cacheStatu
25588 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
25589 0a 20 20 75 2e 62 66 2e 70 43 72 73 72 20 3d 20  .  u.bf.pCrsr = 
2558a 75 2e 62 66 2e 70 43 78 2d 3e 70 43 75 72 73 6f  u.bf.pCx->pCurso
2558b 72 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20  r;..  /* If any 
2558c 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 61 72  of the values ar
2558d 65 20 4e 55 4c 4c 2c 20 74 61 6b 65 20 74 68 65  e NULL, take the
2558e 20 6a 75 6d 70 2e 20 2a 2f 0a 20 20 75 2e 62 66   jump. */.  u.bf
2558f 2e 6e 46 69 65 6c 64 20 3d 20 75 2e 62 66 2e 70  .nField = u.bf.p
25590 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  Cx->pKeyInfo->nF
25591 69 65 6c 64 3b 0a 20 20 66 6f 72 28 75 2e 62 66  ield;.  for(u.bf
25592 2e 69 69 3d 30 3b 20 75 2e 62 66 2e 69 69 3c 75  .ii=0; u.bf.ii<u
25593 2e 62 66 2e 6e 46 69 65 6c 64 3b 20 75 2e 62 66  .bf.nField; u.bf
25594 2e 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  .ii++){.    if( 
25595 75 2e 62 66 2e 61 4d 78 5b 75 2e 62 66 2e 69 69  u.bf.aMx[u.bf.ii
25596 5d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ].flags & MEM_Nu
25597 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d  ll ){.      pc =
25598 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
25599 20 20 20 20 75 2e 62 66 2e 70 43 72 73 72 20 3d      u.bf.pCrsr =
2559a 20 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   0;.      break;
2559b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73  .    }.  }.  ass
2559c 65 72 74 28 20 28 75 2e 62 66 2e 61 4d 78 5b 75  ert( (u.bf.aMx[u
2559d 2e 62 66 2e 6e 46 69 65 6c 64 5d 2e 66 6c 61 67  .bf.nField].flag
2559e 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30  s & MEM_Null)==0
2559f 20 29 3b 0a 0a 20 20 69 66 28 20 75 2e 62 66 2e   );..  if( u.bf.
255a0 70 43 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20  pCrsr!=0 ){.    
255a1 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20  /* Populate the 
255a2 69 6e 64 65 78 20 73 65 61 72 63 68 20 6b 65 79  index search key
255a3 2e 20 2a 2f 0a 20 20 20 20 75 2e 62 66 2e 72 2e  . */.    u.bf.r.
255a4 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 62 66 2e  pKeyInfo = u.bf.
255a5 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20  pCx->pKeyInfo;. 
255a6 20 20 20 75 2e 62 66 2e 72 2e 6e 46 69 65 6c 64     u.bf.r.nField
255a7 20 3d 20 75 2e 62 66 2e 6e 46 69 65 6c 64 20 2b   = u.bf.nField +
255a8 20 31 3b 0a 20 20 20 20 75 2e 62 66 2e 72 2e 66   1;.    u.bf.r.f
255a9 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f  lags = UNPACKED_
255aa 50 52 45 46 49 58 5f 53 45 41 52 43 48 3b 0a 20  PREFIX_SEARCH;. 
255ab 20 20 20 75 2e 62 66 2e 72 2e 61 4d 65 6d 20 3d     u.bf.r.aMem =
255ac 20 75 2e 62 66 2e 61 4d 78 3b 0a 23 69 66 64 65   u.bf.aMx;.#ifde
255ad 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
255ae 20 20 20 7b 20 69 6e 74 20 69 3b 20 66 6f 72 28     { int i; for(
255af 69 3d 30 3b 20 69 3c 75 2e 62 66 2e 72 2e 6e 46  i=0; i<u.bf.r.nF
255b0 69 65 6c 64 3b 20 69 2b 2b 29 20 61 73 73 65 72  ield; i++) asser
255b1 74 28 20 6d 65 6d 49 73 56 61 6c 69 64 28 26 75  t( memIsValid(&u
255b2 2e 62 66 2e 72 2e 61 4d 65 6d 5b 69 5d 29 20 29  .bf.r.aMem[i]) )
255b3 3b 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20  ; }.#endif..    
255b4 2f 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 76  /* Extract the v
255b5 61 6c 75 65 20 6f 66 20 75 2e 62 66 2e 52 20 66  alue of u.bf.R f
255b6 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 33 2e  rom register P3.
255b7 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
255b8 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
255b9 28 70 49 6e 33 29 3b 0a 20 20 20 20 75 2e 62 66  (pIn3);.    u.bf
255ba 2e 52 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a  .R = pIn3->u.i;.
255bb 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 74  .    /* Search t
255bc 68 65 20 42 2d 54 72 65 65 20 69 6e 64 65 78 2e  he B-Tree index.
255bd 20 49 66 20 6e 6f 20 63 6f 6e 66 6c 69 63 74 69   If no conflicti
255be 6e 67 20 72 65 63 6f 72 64 20 69 73 20 66 6f 75  ng record is fou
255bf 6e 64 2c 20 6a 75 6d 70 0a 20 20 20 20 2a 2a 20  nd, jump.    ** 
255c0 74 6f 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65  to P2. Otherwise
255c1 2c 20 63 6f 70 79 20 74 68 65 20 72 6f 77 69 64  , copy the rowid
255c2 20 6f 66 20 74 68 65 20 63 6f 6e 66 6c 69 63 74   of the conflict
255c3 69 6e 67 20 72 65 63 6f 72 64 20 74 6f 0a 20 20  ing record to.  
255c4 20 20 2a 2a 20 72 65 67 69 73 74 65 72 20 50 33    ** register P3
255c5 20 61 6e 64 20 66 61 6c 6c 20 74 68 72 6f 75 67   and fall throug
255c6 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e  h to the next in
255c7 73 74 72 75 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20  struction.  */. 
255c8 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
255c9 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b  treeMovetoUnpack
255ca 65 64 28 75 2e 62 66 2e 70 43 72 73 72 2c 20 26  ed(u.bf.pCrsr, &
255cb 75 2e 62 66 2e 72 2c 20 30 2c 20 30 2c 20 26 75  u.bf.r, 0, 0, &u
255cc 2e 62 66 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73  .bf.pCx->seekRes
255cd 75 6c 74 29 3b 0a 20 20 20 20 69 66 28 20 28 75  ult);.    if( (u
255ce 2e 62 66 2e 72 2e 66 6c 61 67 73 20 26 20 55 4e  .bf.r.flags & UN
255cf 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45  PACKED_PREFIX_SE
255d0 41 52 43 48 29 20 7c 7c 20 75 2e 62 66 2e 72 2e  ARCH) || u.bf.r.
255d1 72 6f 77 69 64 3d 3d 75 2e 62 66 2e 52 20 29 7b  rowid==u.bf.R ){
255d2 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
255d3 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 65 6c  >p2 - 1;.    }el
255d4 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 33 2d 3e  se{.      pIn3->
255d5 75 2e 69 20 3d 20 75 2e 62 66 2e 72 2e 72 6f 77  u.i = u.bf.r.row
255d6 69 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  id;.    }.  }.  
255d7 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
255d8 6f 64 65 3a 20 4e 6f 74 45 78 69 73 74 73 20 50  ode: NotExists P
255d9 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
255da 2a 20 55 73 65 20 74 68 65 20 63 6f 6e 74 65 6e  * Use the conten
255db 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33  t of register P3
255dc 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6b   as an integer k
255dd 65 79 2e 20 20 49 66 20 61 20 72 65 63 6f 72 64  ey.  If a record
255de 20 0a 2a 2a 20 77 69 74 68 20 74 68 61 74 20 6b   .** with that k
255df 65 79 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  ey does not exis
255e0 74 20 69 6e 20 74 61 62 6c 65 20 6f 66 20 50 31  t in table of P1
255e1 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50  , then jump to P
255e2 32 2e 20 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  2. .** If the re
255e3 63 6f 72 64 20 64 6f 65 73 20 65 78 69 73 74 2c  cord does exist,
255e4 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75   then fall throu
255e5 67 68 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20  gh.  The cursor 
255e6 69 73 20 6c 65 66 74 20 0a 2a 2a 20 70 6f 69 6e  is left .** poin
255e7 74 69 6e 67 20 74 6f 20 74 68 65 20 72 65 63 6f  ting to the reco
255e8 72 64 20 69 66 20 69 74 20 65 78 69 73 74 73 2e  rd if it exists.
255e9 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65  .**.** The diffe
255ea 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68  rence between th
255eb 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64  is operation and
255ec 20 4e 6f 74 46 6f 75 6e 64 20 69 73 20 74 68 61   NotFound is tha
255ed 74 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 61 74  t this.** operat
255ee 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20  ion assumes the 
255ef 6b 65 79 20 69 73 20 61 6e 20 69 6e 74 65 67 65  key is an intege
255f0 72 20 61 6e 64 20 74 68 61 74 20 50 31 20 69 73  r and that P1 is
255f1 20 61 20 74 61 62 6c 65 20 77 68 65 72 65 61 73   a table whereas
255f2 0a 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20 61 73 73  .** NotFound ass
255f3 75 6d 65 73 20 6b 65 79 20 69 73 20 61 20 62 6c  umes key is a bl
255f4 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 66  ob constructed f
255f5 72 6f 6d 20 4d 61 6b 65 52 65 63 6f 72 64 20 61  rom MakeRecord a
255f6 6e 64 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20 69  nd.** P1 is an i
255f7 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  ndex..**.** See 
255f8 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74  also: Found, Not
255f9 46 6f 75 6e 64 2c 20 49 73 55 6e 69 71 75 65 0a  Found, IsUnique.
255fa 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 45 78  */.case OP_NotEx
255fb 69 73 74 73 3a 20 7b 20 20 20 20 20 20 20 20 2f  ists: {        /
255fc 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23  * jump, in3 */.#
255fd 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
255fe 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
255ff 6e 74 6f 20 75 2e 62 67 20 2a 2f 0a 20 20 56 64  nto u.bg */.  Vd
25600 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
25601 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
25602 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 75 36  .  int res;.  u6
25603 34 20 69 4b 65 79 3b 0a 23 65 6e 64 69 66 20 2f  4 iKey;.#endif /
25604 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
25605 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
25606 67 20 2a 2f 0a 0a 20 20 70 49 6e 33 20 3d 20 26  g */..  pIn3 = &
25607 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20  aMem[pOp->p3];. 
25608 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66   assert( pIn3->f
25609 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29  lags & MEM_Int )
2560a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
2560b 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
2560c 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
2560d 20 20 75 2e 62 67 2e 70 43 20 3d 20 70 2d 3e 61    u.bg.pC = p->a
2560e 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  pCsr[pOp->p1];. 
2560f 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43   assert( u.bg.pC
25610 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
25611 20 75 2e 62 67 2e 70 43 2d 3e 69 73 54 61 62 6c   u.bg.pC->isTabl
25612 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  e );.  assert( u
25613 2e 62 67 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61  .bg.pC->pseudoTa
25614 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 75  bleReg==0 );.  u
25615 2e 62 67 2e 70 43 72 73 72 20 3d 20 75 2e 62 67  .bg.pCrsr = u.bg
25616 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20  .pC->pCursor;.  
25617 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 67 2e  if( ALWAYS(u.bg.
25618 70 43 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20  pCrsr!=0) ){.   
25619 20 75 2e 62 67 2e 72 65 73 20 3d 20 30 3b 0a 20   u.bg.res = 0;. 
2561a 20 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 70     u.bg.iKey = p
2561b 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 72 63  In3->u.i;.    rc
2561c 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
2561d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e  ovetoUnpacked(u.
2561e 62 67 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62  bg.pCrsr, 0, u.b
2561f 67 2e 69 4b 65 79 2c 20 30 2c 20 26 75 2e 62 67  g.iKey, 0, &u.bg
25620 2e 72 65 73 29 3b 0a 20 20 20 20 75 2e 62 67 2e  .res);.    u.bg.
25621 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20  pC->lastRowid = 
25622 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 75  pIn3->u.i;.    u
25623 2e 62 67 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56  .bg.pC->rowidIsV
25624 61 6c 69 64 20 3d 20 75 2e 62 67 2e 72 65 73 3d  alid = u.bg.res=
25625 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 75 2e 62  =0 ?1:0;.    u.b
25626 67 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20  g.pC->nullRow = 
25627 30 3b 0a 20 20 20 20 75 2e 62 67 2e 70 43 2d 3e  0;.    u.bg.pC->
25628 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
25629 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 75  CHE_STALE;.    u
2562a 2e 62 67 2e 70 43 2d 3e 64 65 66 65 72 72 65 64  .bg.pC->deferred
2562b 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20  Moveto = 0;.    
2562c 69 66 28 20 75 2e 62 67 2e 72 65 73 21 3d 30 20  if( u.bg.res!=0 
2562d 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  ){.      pc = pO
2562e 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20  p->p2 - 1;.     
2562f 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43   assert( u.bg.pC
25630 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d  ->rowidIsValid==
25631 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75  0 );.    }.    u
25632 2e 62 67 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75  .bg.pC->seekResu
25633 6c 74 20 3d 20 75 2e 62 67 2e 72 65 73 3b 0a 20  lt = u.bg.res;. 
25634 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
25635 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e  his happens when
25636 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 6f   an attempt to o
25637 70 65 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f  pen a read curso
25638 72 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  r on the.    ** 
25639 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
2563a 62 6c 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ble returns SQLI
2563b 54 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20 2a 2f  TE_EMPTY..    */
2563c 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
2563d 32 20 2d 20 31 3b 0a 20 20 20 20 61 73 73 65 72  2 - 1;.    asser
2563e 74 28 20 75 2e 62 67 2e 70 43 2d 3e 72 6f 77 69  t( u.bg.pC->rowi
2563f 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20  dIsValid==0 );. 
25640 20 20 20 75 2e 62 67 2e 70 43 2d 3e 73 65 65 6b     u.bg.pC->seek
25641 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a  Result = 0;.  }.
25642 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
25643 70 63 6f 64 65 3a 20 53 65 71 75 65 6e 63 65 20  pcode: Sequence 
25644 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
25645 2a 20 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20  * Find the next 
25646 61 76 61 69 6c 61 62 6c 65 20 73 65 71 75 65 6e  available sequen
25647 63 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 63 75  ce number for cu
25648 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 57 72 69 74  rsor P1..** Writ
25649 65 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 6e  e the sequence n
2564a 75 6d 62 65 72 20 69 6e 74 6f 20 72 65 67 69 73  umber into regis
2564b 74 65 72 20 50 32 2e 0a 2a 2a 20 54 68 65 20 73  ter P2..** The s
2564c 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 6f  equence number o
2564d 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  n the cursor is 
2564e 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 66 74 65  incremented afte
2564f 72 20 74 68 69 73 0a 2a 2a 20 69 6e 73 74 72 75  r this.** instru
25650 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a 63 61 73 65  ction.  .*/.case
25651 20 4f 50 5f 53 65 71 75 65 6e 63 65 3a 20 7b 20   OP_Sequence: { 
25652 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74            /* out
25653 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a  2-prerelease */.
25654 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
25655 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
25656 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
25657 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72  assert( p->apCsr
25658 5b 70 4f 70 2d 3e 70 31 5d 21 3d 30 20 29 3b 0a  [pOp->p1]!=0 );.
25659 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 2d    pOut->u.i = p-
2565a 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 2d  >apCsr[pOp->p1]-
2565b 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a 20 20 62  >seqCount++;.  b
2565c 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63  reak;.}.../* Opc
2565d 6f 64 65 3a 20 4e 65 77 52 6f 77 69 64 20 50 31  ode: NewRowid P1
2565e 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
2565f 20 47 65 74 20 61 20 6e 65 77 20 69 6e 74 65 67   Get a new integ
25660 65 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  er record number
25661 20 28 61 2e 6b 2e 61 20 22 72 6f 77 69 64 22 29   (a.k.a "rowid")
25662 20 75 73 65 64 20 61 73 20 74 68 65 20 6b 65 79   used as the key
25663 20 74 6f 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20   to a table..** 
25664 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  The record numbe
25665 72 20 69 73 20 6e 6f 74 20 70 72 65 76 69 6f 75  r is not previou
25666 73 6c 79 20 75 73 65 64 20 61 73 20 61 20 6b 65  sly used as a ke
25667 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  y in the databas
25668 65 0a 2a 2a 20 74 61 62 6c 65 20 74 68 61 74 20  e.** table that 
25669 63 75 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73  cursor P1 points
2566a 20 74 6f 2e 20 20 54 68 65 20 6e 65 77 20 72 65   to.  The new re
2566b 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 77  cord number is w
2566c 72 69 74 74 65 6e 0a 2a 2a 20 77 72 69 74 74 65  ritten.** writte
2566d 6e 20 74 6f 20 72 65 67 69 73 74 65 72 20 50 32  n to register P2
2566e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 33 3e 30 20  ..**.** If P3>0 
2566f 74 68 65 6e 20 50 33 20 69 73 20 61 20 72 65 67  then P3 is a reg
25670 69 73 74 65 72 20 69 6e 20 74 68 65 20 72 6f 6f  ister in the roo
25671 74 20 66 72 61 6d 65 20 6f 66 20 74 68 69 73 20  t frame of this 
25672 56 44 42 45 20 74 68 61 74 20 68 6f 6c 64 73 20  VDBE that holds 
25673 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 73 74 20  .** the largest 
25674 70 72 65 76 69 6f 75 73 6c 79 20 67 65 6e 65 72  previously gener
25675 61 74 65 64 20 72 65 63 6f 72 64 20 6e 75 6d 62  ated record numb
25676 65 72 2e 20 4e 6f 20 6e 65 77 20 72 65 63 6f 72  er. No new recor
25677 64 20 6e 75 6d 62 65 72 73 20 61 72 65 0a 2a 2a  d numbers are.**
25678 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 6c   allowed to be l
25679 65 73 73 20 74 68 61 6e 20 74 68 69 73 20 76 61  ess than this va
2567a 6c 75 65 2e 20 57 68 65 6e 20 74 68 69 73 20 76  lue. When this v
2567b 61 6c 75 65 20 72 65 61 63 68 65 73 20 69 74 73  alue reaches its
2567c 20 6d 61 78 69 6d 75 6d 2c 20 0a 2a 2a 20 61 6e   maximum, .** an
2567d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65 72 72   SQLITE_FULL err
2567e 6f 72 20 69 73 20 67 65 6e 65 72 61 74 65 64 2e  or is generated.
2567f 20 54 68 65 20 50 33 20 72 65 67 69 73 74 65 72   The P3 register
25680 20 69 73 20 75 70 64 61 74 65 64 20 77 69 74 68   is updated with
25681 20 74 68 65 20 27 0a 2a 2a 20 67 65 6e 65 72 61   the '.** genera
25682 74 65 64 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  ted record numbe
25683 72 2e 20 54 68 69 73 20 50 33 20 6d 65 63 68 61  r. This P3 mecha
25684 6e 69 73 6d 20 69 73 20 75 73 65 64 20 74 6f 20  nism is used to 
25685 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20 74  help implement t
25686 68 65 0a 2a 2a 20 41 55 54 4f 49 4e 43 52 45 4d  he.** AUTOINCREM
25687 45 4e 54 20 66 65 61 74 75 72 65 2e 0a 2a 2f 0a  ENT feature..*/.
25688 63 61 73 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64  case OP_NewRowid
25689 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  : {           /*
2568a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
2568b 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
2568c 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
2568d 76 65 64 20 69 6e 74 6f 20 75 2e 62 68 20 2a 2f  ved into u.bh */
2568e 0a 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20  .  i64 v;       
2568f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
25690 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20   new rowid */.  
25691 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20  VdbeCursor *pC; 
25692 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
25693 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 67 65 74   of table to get
25694 20 74 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a   the new rowid *
25695 2f 0a 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20  /.  int res;    
25696 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
25697 73 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c 69 74  sult of an sqlit
25698 65 33 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f  e3BtreeLast() */
25699 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20  .  int cnt;     
2569a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75            /* Cou
2569b 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74 20 74 68  nter to limit th
2569c 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 61 72  e number of sear
2569d 63 68 65 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  ches */.  Mem *p
2569e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
2569f 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
256a0 64 69 6e 67 20 6c 61 72 67 65 73 74 20 72 6f 77  ding largest row
256a1 69 64 20 66 6f 72 20 41 55 54 4f 49 4e 43 52 45  id for AUTOINCRE
256a2 4d 45 4e 54 20 2a 2f 0a 20 20 56 64 62 65 46 72  MENT */.  VdbeFr
256a3 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20  ame *pFrame;    
256a4 20 2f 2a 20 52 6f 6f 74 20 66 72 61 6d 65 20 6f   /* Root frame o
256a5 66 20 56 44 42 45 20 2a 2f 0a 23 65 6e 64 69 66  f VDBE */.#endif
256a6 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
256a7 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
256a8 2e 62 68 20 2a 2f 0a 0a 20 20 75 2e 62 68 2e 76  .bh */..  u.bh.v
256a9 20 3d 20 30 3b 0a 20 20 75 2e 62 68 2e 72 65 73   = 0;.  u.bh.res
256aa 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
256ab 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
256ac 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72  p->p1<p->nCursor
256ad 20 29 3b 0a 20 20 75 2e 62 68 2e 70 43 20 3d 20   );.  u.bh.pC = 
256ae 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
256af 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  ];.  assert( u.b
256b0 68 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28  h.pC!=0 );.  if(
256b1 20 4e 45 56 45 52 28 75 2e 62 68 2e 70 43 2d 3e   NEVER(u.bh.pC->
256b2 70 43 75 72 73 6f 72 3d 3d 30 29 20 29 7b 0a 20  pCursor==0) ){. 
256b3 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f 20 69     /* The zero i
256b4 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 62  nitialization ab
256b5 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61 74 20  ove is all that 
256b6 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 7d  is needed */.  }
256b7 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65  else{.    /* The
256b8 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72 20 72   next rowid or r
256b9 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 64 69  ecord number (di
256ba 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20 66 6f  fferent terms fo
256bb 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 2a  r the same.    *
256bc 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62 74 61  * thing) is obta
256bd 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d 73 74  ined in a two-st
256be 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20  ep algorithm..  
256bf 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 72 73    **.    ** Firs
256c0 74 20 77 65 20 61 74 74 65 6d 70 74 20 74 6f 20  t we attempt to 
256c1 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73 74  find the largest
256c2 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 20   existing rowid 
256c3 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20 20 20  and add one.    
256c4 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42 75 74  ** to that.  But
256c5 20 69 66 20 74 68 65 20 6c 61 72 67 65 73 74 20   if the largest 
256c6 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 20 69  existing rowid i
256c7 73 20 61 6c 72 65 61 64 79 20 74 68 65 20 6d 61  s already the ma
256c8 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70 6f 73  ximum.    ** pos
256c9 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c 20 77  itive integer, w
256ca 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c 20 74  e have to fall t
256cb 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73 65  hrough to the se
256cc 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72 6f 62  cond.    ** prob
256cd 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f 72 69  abilistic algori
256ce 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  thm.    **.    *
256cf 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 6c 67  * The second alg
256d0 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73 65 6c  orithm is to sel
256d1 65 63 74 20 61 20 72 6f 77 69 64 20 61 74 20 72  ect a rowid at r
256d2 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20 69 66  andom and see if
256d3 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72 65 61  .    ** it alrea
256d4 64 79 20 65 78 69 73 74 73 20 69 6e 20 74 68 65  dy exists in the
256d5 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74 20 64   table.  If it d
256d6 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 77  oes not exist, w
256d7 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 73 75  e have.    ** su
256d8 63 63 65 65 64 65 64 2e 20 20 49 66 20 74 68 65  cceeded.  If the
256d9 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 64 6f   random rowid do
256da 65 73 20 65 78 69 73 74 2c 20 77 65 20 73 65 6c  es exist, we sel
256db 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a 20 20  ect a new one.  
256dc 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61 67 61    ** and try aga
256dd 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 20 74 69  in, up to 100 ti
256de 6d 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  mes..    */.    
256df 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d  assert( u.bh.pC-
256e0 3e 69 73 54 61 62 6c 65 20 29 3b 0a 0a 23 69 66  >isTable );..#if
256e1 64 65 66 20 53 51 4c 49 54 45 5f 33 32 42 49 54  def SQLITE_32BIT
256e2 5f 52 4f 57 49 44 0a 23 20 20 20 64 65 66 69 6e  _ROWID.#   defin
256e3 65 20 4d 41 58 5f 52 4f 57 49 44 20 30 78 37 66  e MAX_ROWID 0x7f
256e4 66 66 66 66 66 66 0a 23 65 6c 73 65 0a 20 20 20  ffffff.#else.   
256e5 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 70 69 6c 65   /* Some compile
256e6 72 73 20 63 6f 6d 70 6c 61 69 6e 20 61 62 6f 75  rs complain abou
256e7 74 20 63 6f 6e 73 74 61 6e 74 73 20 6f 66 20 74  t constants of t
256e8 68 65 20 66 6f 72 6d 20 30 78 37 66 66 66 66 66  he form 0x7fffff
256e9 66 66 66 66 66 66 66 66 66 66 2e 0a 20 20 20 20  ffffffffff..    
256ea 2a 2a 20 4f 74 68 65 72 73 20 63 6f 6d 70 6c 61  ** Others compla
256eb 69 6e 20 61 62 6f 75 74 20 30 78 37 66 66 66 66  in about 0x7ffff
256ec 66 66 66 66 66 66 66 66 66 66 66 66 4c 4c 2e 20  ffffffffffffLL. 
256ed 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   The following m
256ee 61 63 72 6f 20 73 65 65 6d 73 0a 20 20 20 20 2a  acro seems.    *
256ef 2a 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65  * to provide the
256f0 20 63 6f 6e 73 74 61 6e 74 20 77 68 69 6c 65 20   constant while 
256f1 6d 61 6b 69 6e 67 20 61 6c 6c 20 63 6f 6d 70 69  making all compi
256f2 6c 65 72 73 20 68 61 70 70 79 2e 0a 20 20 20 20  lers happy..    
256f3 2a 2f 0a 23 20 20 20 64 65 66 69 6e 65 20 4d 41  */.#   define MA
256f4 58 5f 52 4f 57 49 44 20 20 28 69 36 34 29 28 20  X_ROWID  (i64)( 
256f5 28 28 28 75 36 34 29 30 78 37 66 66 66 66 66 66  (((u64)0x7ffffff
256f6 66 29 3c 3c 33 32 29 20 7c 20 28 75 36 34 29 30  f)<<32) | (u64)0
256f7 78 66 66 66 66 66 66 66 66 20 29 0a 23 65 6e 64  xffffffff ).#end
256f8 69 66 0a 0a 20 20 20 20 69 66 28 20 21 75 2e 62  if..    if( !u.b
256f9 68 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52  h.pC->useRandomR
256fa 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 75 2e  owid ){.      u.
256fb 62 68 2e 76 20 3d 20 73 71 6c 69 74 65 33 42 74  bh.v = sqlite3Bt
256fc 72 65 65 47 65 74 43 61 63 68 65 64 52 6f 77 69  reeGetCachedRowi
256fd 64 28 75 2e 62 68 2e 70 43 2d 3e 70 43 75 72 73  d(u.bh.pC->pCurs
256fe 6f 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75  or);.      if( u
256ff 2e 62 68 2e 76 3d 3d 30 20 29 7b 0a 20 20 20 20  .bh.v==0 ){.    
25700 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
25701 42 74 72 65 65 4c 61 73 74 28 75 2e 62 68 2e 70  BtreeLast(u.bh.p
25702 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 62  C->pCursor, &u.b
25703 68 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20  h.res);.        
25704 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
25705 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67  K ){.          g
25706 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
25707 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20  _error;.        
25708 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  }.        if( u.
25709 62 68 2e 72 65 73 20 29 7b 0a 20 20 20 20 20 20  bh.res ){.      
2570a 20 20 20 20 75 2e 62 68 2e 76 20 3d 20 31 3b 20      u.bh.v = 1; 
2570b 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 36 31 39 31    /* IMP: R-6191
2570c 34 2d 34 38 30 37 34 20 2a 2f 0a 20 20 20 20 20  4-48074 */.     
2570d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2570e 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
2570f 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 73  te3BtreeCursorIs
25710 56 61 6c 69 64 28 75 2e 62 68 2e 70 43 2d 3e 70  Valid(u.bh.pC->p
25711 43 75 72 73 6f 72 29 20 29 3b 0a 20 20 20 20 20  Cursor) );.     
25712 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
25713 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75 2e  3BtreeKeySize(u.
25714 62 68 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  bh.pC->pCursor, 
25715 26 75 2e 62 68 2e 76 29 3b 0a 20 20 20 20 20 20  &u.bh.v);.      
25716 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
25717 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f  SQLITE_OK );   /
25718 2a 20 43 61 6e 6e 6f 74 20 66 61 69 6c 20 66 6f  * Cannot fail fo
25719 6c 6c 6f 77 69 6e 67 20 42 74 72 65 65 4c 61 73  llowing BtreeLas
2571a 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  t() */.         
2571b 20 69 66 28 20 75 2e 62 68 2e 76 3e 3d 4d 41 58   if( u.bh.v>=MAX
2571c 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20  _ROWID ){.      
2571d 20 20 20 20 20 20 75 2e 62 68 2e 70 43 2d 3e 75        u.bh.pC->u
2571e 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20 3d 20  seRandomRowid = 
2571f 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  1;.          }el
25720 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
25721 75 2e 62 68 2e 76 2b 2b 3b 20 20 20 2f 2a 20 49  u.bh.v++;   /* I
25722 4d 50 3a 20 52 2d 32 39 35 33 38 2d 33 34 39 38  MP: R-29538-3498
25723 37 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 7d  7 */.          }
25724 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
25725 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
25726 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
25727 45 4d 45 4e 54 0a 20 20 20 20 20 20 69 66 28 20  EMENT.      if( 
25728 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20  pOp->p3 ){.     
25729 20 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61     /* Assert tha
2572a 74 20 50 33 20 69 73 20 61 20 76 61 6c 69 64 20  t P3 is a valid 
2572b 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0a  memory cell. */.
2572c 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2572d 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20  pOp->p3>0 );.   
2572e 20 20 20 20 20 69 66 28 20 70 2d 3e 70 46 72 61       if( p->pFra
2572f 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  me ){.          
25730 66 6f 72 28 75 2e 62 68 2e 70 46 72 61 6d 65 3d  for(u.bh.pFrame=
25731 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 62 68 2e  p->pFrame; u.bh.
25732 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b  pFrame->pParent;
25733 20 75 2e 62 68 2e 70 46 72 61 6d 65 3d 75 2e 62   u.bh.pFrame=u.b
25734 68 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e  h.pFrame->pParen
25735 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  t);.          /*
25736 20 41 73 73 65 72 74 20 74 68 61 74 20 50 33 20   Assert that P3 
25737 69 73 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72  is a valid memor
25738 79 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20  y cell. */.     
25739 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
2573a 2d 3e 70 33 3c 3d 75 2e 62 68 2e 70 46 72 61 6d  ->p3<=u.bh.pFram
2573b 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20  e->nMem );.     
2573c 20 20 20 20 20 75 2e 62 68 2e 70 4d 65 6d 20 3d       u.bh.pMem =
2573d 20 26 75 2e 62 68 2e 70 46 72 61 6d 65 2d 3e 61   &u.bh.pFrame->a
2573e 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
2573f 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
25740 20 20 20 20 20 20 20 2f 2a 20 41 73 73 65 72 74         /* Assert
25741 20 74 68 61 74 20 50 33 20 69 73 20 61 20 76 61   that P3 is a va
25742 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e  lid memory cell.
25743 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73   */.          as
25744 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70  sert( pOp->p3<=p
25745 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20  ->nMem );.      
25746 20 20 20 20 75 2e 62 68 2e 70 4d 65 6d 20 3d 20      u.bh.pMem = 
25747 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  &aMem[pOp->p3];.
25748 20 20 20 20 20 20 20 20 20 20 6d 65 6d 41 62 6f            memAbo
25749 75 74 54 6f 43 68 61 6e 67 65 28 70 2c 20 75 2e  utToChange(p, u.
2574a 62 68 2e 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20  bh.pMem);.      
2574b 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65    }.        asse
2574c 72 74 28 20 6d 65 6d 49 73 56 61 6c 69 64 28 75  rt( memIsValid(u
2574d 2e 62 68 2e 70 4d 65 6d 29 20 29 3b 0a 0a 20 20  .bh.pMem) );..  
2574e 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54        REGISTER_T
2574f 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e  RACE(pOp->p3, u.
25750 62 68 2e 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20  bh.pMem);.      
25751 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
25752 49 6e 74 65 67 65 72 69 66 79 28 75 2e 62 68 2e  Integerify(u.bh.
25753 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 61  pMem);.        a
25754 73 73 65 72 74 28 20 28 75 2e 62 68 2e 70 4d 65  ssert( (u.bh.pMe
25755 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  m->flags & MEM_I
25756 6e 74 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d 65  nt)!=0 );  /* me
25757 6d 28 50 33 29 20 68 6f 6c 64 73 20 61 6e 20 69  m(P3) holds an i
25758 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20  nteger */.      
25759 20 20 69 66 28 20 75 2e 62 68 2e 70 4d 65 6d 2d    if( u.bh.pMem-
2575a 3e 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20  >u.i==MAX_ROWID 
2575b 7c 7c 20 75 2e 62 68 2e 70 43 2d 3e 75 73 65 52  || u.bh.pC->useR
2575c 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20  andomRowid ){.  
2575d 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
2575e 49 54 45 5f 46 55 4c 4c 3b 20 20 20 2f 2a 20 49  ITE_FULL;   /* I
2575f 4d 50 3a 20 52 2d 31 32 32 37 35 2d 36 31 33 33  MP: R-12275-6133
25760 38 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67  8 */.          g
25761 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
25762 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20  _error;.        
25763 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  }.        if( u.
25764 62 68 2e 76 3c 75 2e 62 68 2e 70 4d 65 6d 2d 3e  bh.v<u.bh.pMem->
25765 75 2e 69 2b 31 20 29 7b 0a 20 20 20 20 20 20 20  u.i+1 ){.       
25766 20 20 20 75 2e 62 68 2e 76 20 3d 20 75 2e 62 68     u.bh.v = u.bh
25767 2e 70 4d 65 6d 2d 3e 75 2e 69 20 2b 20 31 3b 0a  .pMem->u.i + 1;.
25768 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
25769 20 20 75 2e 62 68 2e 70 4d 65 6d 2d 3e 75 2e 69    u.bh.pMem->u.i
2576a 20 3d 20 75 2e 62 68 2e 76 3b 0a 20 20 20 20 20   = u.bh.v;.     
2576b 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20   }.#endif..     
2576c 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
2576d 43 61 63 68 65 64 52 6f 77 69 64 28 75 2e 62 68  CachedRowid(u.bh
2576e 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 75 2e  .pC->pCursor, u.
2576f 62 68 2e 76 3c 4d 41 58 5f 52 4f 57 49 44 20 3f  bh.v<MAX_ROWID ?
25770 20 75 2e 62 68 2e 76 2b 31 20 3a 20 30 29 3b 0a   u.bh.v+1 : 0);.
25771 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e      }.    if( u.
25772 62 68 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d  bh.pC->useRandom
25773 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 2f  Rowid ){.      /
25774 2a 20 49 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e  * IMPLEMENTATION
25775 2d 4f 46 3a 20 52 2d 30 37 36 37 37 2d 34 31 38  -OF: R-07677-418
25776 38 31 20 49 66 20 74 68 65 20 6c 61 72 67 65 73  81 If the larges
25777 74 20 52 4f 57 49 44 20 69 73 20 65 71 75 61 6c  t ROWID is equal
25778 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a   to the.      **
25779 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c   largest possibl
2577a 65 20 69 6e 74 65 67 65 72 20 28 39 32 32 33 33  e integer (92233
2577b 37 32 30 33 36 38 35 34 37 37 35 38 30 37 29 20  72036854775807) 
2577c 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73  then the databas
2577d 65 0a 20 20 20 20 20 20 2a 2a 20 65 6e 67 69 6e  e.      ** engin
2577e 65 20 73 74 61 72 74 73 20 70 69 63 6b 69 6e 67  e starts picking
2577f 20 70 6f 73 69 74 69 76 65 20 63 61 6e 64 69 64   positive candid
25780 61 74 65 20 52 4f 57 49 44 73 20 61 74 20 72 61  ate ROWIDs at ra
25781 6e 64 6f 6d 20 75 6e 74 69 6c 0a 20 20 20 20 20  ndom until.     
25782 20 2a 2a 20 69 74 20 66 69 6e 64 73 20 6f 6e 65   ** it finds one
25783 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 72 65   that is not pre
25784 76 69 6f 75 73 6c 79 20 75 73 65 64 2e 20 2a 2f  viously used. */
25785 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
25786 4f 70 2d 3e 70 33 3d 3d 30 20 29 3b 20 20 2f 2a  Op->p3==0 );  /*
25787 20 57 65 20 63 61 6e 6e 6f 74 20 62 65 20 69 6e   We cannot be in
25788 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 6d 6f   random rowid mo
25789 64 65 20 69 66 20 74 68 69 73 20 69 73 0a 20 20  de if this is.  
2578a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2578b 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6e             ** an
2578c 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74   AUTOINCREMENT t
2578d 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 2f  able. */.      /
2578e 2a 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 61  * on the first a
2578f 74 74 65 6d 70 74 2c 20 73 69 6d 70 6c 79 20 64  ttempt, simply d
25790 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 68 61 6e 20  o one more than 
25791 70 72 65 76 69 6f 75 73 20 2a 2f 0a 20 20 20 20  previous */.    
25792 20 20 75 2e 62 68 2e 76 20 3d 20 6c 61 73 74 52    u.bh.v = lastR
25793 6f 77 69 64 3b 0a 20 20 20 20 20 20 75 2e 62 68  owid;.      u.bh
25794 2e 76 20 26 3d 20 28 4d 41 58 5f 52 4f 57 49 44  .v &= (MAX_ROWID
25795 3e 3e 31 29 3b 20 2f 2a 20 65 6e 73 75 72 65 20  >>1); /* ensure 
25796 64 6f 65 73 6e 27 74 20 67 6f 20 6e 65 67 61 74  doesn't go negat
25797 69 76 65 20 2a 2f 0a 20 20 20 20 20 20 75 2e 62  ive */.      u.b
25798 68 2e 76 2b 2b 3b 20 2f 2a 20 65 6e 73 75 72 65  h.v++; /* ensure
25799 20 6e 6f 6e 2d 7a 65 72 6f 20 2a 2f 0a 20 20 20   non-zero */.   
2579a 20 20 20 75 2e 62 68 2e 63 6e 74 20 3d 20 30 3b     u.bh.cnt = 0;
2579b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 20 20  .      while(   
2579c 28 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74  ((rc = sqlite3Bt
2579d 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  reeMovetoUnpacke
2579e 64 28 75 2e 62 68 2e 70 43 2d 3e 70 43 75 72 73  d(u.bh.pC->pCurs
2579f 6f 72 2c 20 30 2c 20 28 75 36 34 29 75 2e 62 68  or, 0, (u64)u.bh
257a0 2e 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  .v,.            
257a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
257a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
257a3 20 20 20 20 20 30 2c 20 26 75 2e 62 68 2e 72 65       0, &u.bh.re
257a4 73 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 0a  s))==SQLITE_OK).
257a5 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28              && (
257a6 75 2e 62 68 2e 72 65 73 3d 3d 30 29 0a 20 20 20  u.bh.res==0).   
257a7 20 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b 75           && (++u
257a8 2e 62 68 2e 63 6e 74 3c 31 30 30 29 29 7b 0a 20  .bh.cnt<100)){. 
257a9 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c 6c 69 73         /* collis
257aa 69 6f 6e 20 2d 20 74 72 79 20 61 6e 6f 74 68 65  ion - try anothe
257ab 72 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 2a  r random rowid *
257ac 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  /.        sqlite
257ad 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a  3_randomness(siz
257ae 65 6f 66 28 75 2e 62 68 2e 76 29 2c 20 26 75 2e  eof(u.bh.v), &u.
257af 62 68 2e 76 29 3b 0a 20 20 20 20 20 20 20 20 69  bh.v);.        i
257b0 66 28 20 75 2e 62 68 2e 63 6e 74 3c 35 20 29 7b  f( u.bh.cnt<5 ){
257b1 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 72  .          /* tr
257b2 79 20 22 73 6d 61 6c 6c 22 20 72 61 6e 64 6f 6d  y "small" random
257b3 20 72 6f 77 69 64 73 20 66 6f 72 20 74 68 65 20   rowids for the 
257b4 69 6e 69 74 69 61 6c 20 61 74 74 65 6d 70 74 73  initial attempts
257b5 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 75 2e   */.          u.
257b6 62 68 2e 76 20 26 3d 20 30 78 66 66 66 66 66 66  bh.v &= 0xffffff
257b7 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
257b8 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62 68 2e  .          u.bh.
257b9 76 20 26 3d 20 28 4d 41 58 5f 52 4f 57 49 44 3e  v &= (MAX_ROWID>
257ba 3e 31 29 3b 20 2f 2a 20 65 6e 73 75 72 65 20 64  >1); /* ensure d
257bb 6f 65 73 6e 27 74 20 67 6f 20 6e 65 67 61 74 69  oesn't go negati
257bc 76 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a  ve */.        }.
257bd 20 20 20 20 20 20 20 20 75 2e 62 68 2e 76 2b 2b          u.bh.v++
257be 3b 20 2f 2a 20 65 6e 73 75 72 65 20 6e 6f 6e 2d  ; /* ensure non-
257bf 7a 65 72 6f 20 2a 2f 0a 20 20 20 20 20 20 7d 0a  zero */.      }.
257c0 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
257c1 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e 62 68 2e  LITE_OK && u.bh.
257c2 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  res==0 ){.      
257c3 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55    rc = SQLITE_FU
257c4 4c 4c 3b 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d  LL;   /* IMP: R-
257c5 33 38 32 31 39 2d 35 33 30 30 32 20 2a 2f 0a 20  38219-53002 */. 
257c6 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72         goto abor
257c7 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
257c8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
257c9 73 65 72 74 28 20 75 2e 62 68 2e 76 3e 30 20 29  sert( u.bh.v>0 )
257ca 3b 20 20 2f 2a 20 45 56 3a 20 52 2d 34 30 38 31  ;  /* EV: R-4081
257cb 32 2d 30 33 35 37 30 20 2a 2f 0a 20 20 20 20 7d  2-03570 */.    }
257cc 0a 20 20 20 20 75 2e 62 68 2e 70 43 2d 3e 72 6f  .    u.bh.pC->ro
257cd 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
257ce 20 20 20 20 75 2e 62 68 2e 70 43 2d 3e 64 65 66      u.bh.pC->def
257cf 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b  erredMoveto = 0;
257d0 0a 20 20 20 20 75 2e 62 68 2e 70 43 2d 3e 63 61  .    u.bh.pC->ca
257d1 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
257d2 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 70  E_STALE;.  }.  p
257d3 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 68 2e  Out->u.i = u.bh.
257d4 76 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  v;.  break;.}../
257d5 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72 74  * Opcode: Insert
257d6 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a   P1 P2 P3 P4 P5.
257d7 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65  **.** Write an e
257d8 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 74 61  ntry into the ta
257d9 62 6c 65 20 6f 66 20 63 75 72 73 6f 72 20 50 31  ble of cursor P1
257da 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69  .  A new entry i
257db 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 69 66 20  s.** created if 
257dc 69 74 20 64 6f 65 73 6e 27 74 20 61 6c 72 65 61  it doesn't alrea
257dd 64 79 20 65 78 69 73 74 20 6f 72 20 74 68 65 20  dy exist or the 
257de 64 61 74 61 20 66 6f 72 20 61 6e 20 65 78 69 73  data for an exis
257df 74 69 6e 67 0a 2a 2a 20 65 6e 74 72 79 20 69 73  ting.** entry is
257e0 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 54   overwritten.  T
257e1 68 65 20 64 61 74 61 20 69 73 20 74 68 65 20 76  he data is the v
257e2 61 6c 75 65 20 4d 45 4d 5f 42 6c 6f 62 20 73 74  alue MEM_Blob st
257e3 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
257e4 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 32 2e 20 54  .** number P2. T
257e5 68 65 20 6b 65 79 20 69 73 20 73 74 6f 72 65 64  he key is stored
257e6 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e   in register P3.
257e7 20 54 68 65 20 6b 65 79 20 6d 75 73 74 0a 2a 2a   The key must.**
257e8 20 62 65 20 61 20 4d 45 4d 5f 49 6e 74 2e 0a 2a   be a MEM_Int..*
257e9 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c  *.** If the OPFL
257ea 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20  AG_NCHANGE flag 
257eb 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20 74 68  of P5 is set, th
257ec 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67  en the row chang
257ed 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e  e count is.** in
257ee 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72  cremented (other
257ef 77 69 73 65 20 6e 6f 74 29 2e 20 20 49 66 20 74  wise not).  If t
257f0 68 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f  he OPFLAG_LASTRO
257f1 57 49 44 20 66 6c 61 67 20 6f 66 20 50 35 20 69  WID flag of P5 i
257f2 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 72  s set,.** then r
257f3 6f 77 69 64 20 69 73 20 73 74 6f 72 65 64 20 66  owid is stored f
257f4 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 72 65  or subsequent re
257f5 74 75 72 6e 20 62 79 20 74 68 65 0a 2a 2a 20 73  turn by the.** s
257f6 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
257f7 72 74 5f 72 6f 77 69 64 28 29 20 66 75 6e 63 74  rt_rowid() funct
257f8 69 6f 6e 20 28 6f 74 68 65 72 77 69 73 65 20 69  ion (otherwise i
257f9 74 20 69 73 20 75 6e 6d 6f 64 69 66 69 65 64 29  t is unmodified)
257fa 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f  ..**.** If the O
257fb 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53  PFLAG_USESEEKRES
257fc 55 4c 54 20 66 6c 61 67 20 6f 66 20 50 35 20 69  ULT flag of P5 i
257fd 73 20 73 65 74 20 61 6e 64 20 69 66 20 74 68 65  s set and if the
257fe 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68   result of.** th
257ff 65 20 6c 61 73 74 20 73 65 65 6b 20 6f 70 65 72  e last seek oper
25800 61 74 69 6f 6e 20 28 4f 50 5f 4e 6f 74 45 78 69  ation (OP_NotExi
25801 73 74 73 29 20 77 61 73 20 61 20 73 75 63 63 65  sts) was a succe
25802 73 73 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a  ss, then this.**
25803 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20   operation will 
25804 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 66  not attempt to f
25805 69 6e 64 20 74 68 65 20 61 70 70 72 6f 70 72 69  ind the appropri
25806 61 74 65 20 72 6f 77 20 62 65 66 6f 72 65 20 64  ate row before d
25807 6f 69 6e 67 0a 2a 2a 20 74 68 65 20 69 6e 73 65  oing.** the inse
25808 72 74 20 62 75 74 20 77 69 6c 6c 20 69 6e 73 74  rt but will inst
25809 65 61 64 20 6f 76 65 72 77 72 69 74 65 20 74 68  ead overwrite th
2580a 65 20 72 6f 77 20 74 68 61 74 20 74 68 65 20 63  e row that the c
2580b 75 72 73 6f 72 20 69 73 0a 2a 2a 20 63 75 72 72  ursor is.** curr
2580c 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74  ently pointing t
2580d 6f 2e 20 20 50 72 65 73 75 6d 61 62 6c 79 2c 20  o.  Presumably, 
2580e 74 68 65 20 70 72 69 6f 72 20 4f 50 5f 4e 6f 74  the prior OP_Not
2580f 45 78 69 73 74 73 20 6f 70 63 6f 64 65 0a 2a 2a  Exists opcode.**
25810 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 6f 73   has already pos
25811 69 74 69 6f 6e 65 64 20 74 68 65 20 63 75 72 73  itioned the curs
25812 6f 72 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54  or correctly.  T
25813 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69  his is an optimi
25814 7a 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 62  zation.** that b
25815 6f 6f 73 74 73 20 70 65 72 66 6f 72 6d 61 6e 63  oosts performanc
25816 65 20 62 79 20 61 76 6f 69 64 69 6e 67 20 72 65  e by avoiding re
25817 64 75 6e 64 61 6e 74 20 73 65 65 6b 73 2e 0a 2a  dundant seeks..*
25818 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c  *.** If the OPFL
25819 41 47 5f 49 53 55 50 44 41 54 45 20 66 6c 61 67  AG_ISUPDATE flag
2581a 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68   is set, then th
2581b 69 73 20 6f 70 63 6f 64 65 20 69 73 20 70 61 72  is opcode is par
2581c 74 20 6f 66 20 61 6e 0a 2a 2a 20 55 50 44 41 54  t of an.** UPDAT
2581d 45 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4f 74  E operation.  Ot
2581e 68 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20  herwise (if the 
2581f 66 6c 61 67 20 69 73 20 63 6c 65 61 72 29 20 74  flag is clear) t
25820 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 0a  hen this opcode.
25821 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e  ** is part of an
25822 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f   INSERT operatio
25823 6e 2e 20 20 54 68 65 20 64 69 66 66 65 72 65 6e  n.  The differen
25824 63 65 20 69 73 20 6f 6e 6c 79 20 69 6d 70 6f 72  ce is only impor
25825 74 61 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 75  tant to.** the u
25826 70 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 0a 2a  pdate hook..**.*
25827 2a 20 50 61 72 61 6d 65 74 65 72 20 50 34 20 6d  * Parameter P4 m
25828 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74  ay point to a st
25829 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ring containing 
2582a 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 2c 20  the table-name, 
2582b 6f 72 0a 2a 2a 20 6d 61 79 20 62 65 20 4e 55 4c  or.** may be NUL
2582c 4c 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20  L. If it is not 
2582d 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 75  NULL, then the u
2582e 70 64 61 74 65 2d 68 6f 6f 6b 20 0a 2a 2a 20 28  pdate-hook .** (
2582f 73 71 6c 69 74 65 33 2e 78 55 70 64 61 74 65 43  sqlite3.xUpdateC
25830 61 6c 6c 62 61 63 6b 29 20 69 73 20 69 6e 76 6f  allback) is invo
25831 6b 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20  ked following a 
25832 73 75 63 63 65 73 73 66 75 6c 20 69 6e 73 65 72  successful inser
25833 74 2e 0a 2a 2a 0a 2a 2a 20 28 57 41 52 4e 49 4e  t..**.** (WARNIN
25834 47 2f 54 4f 44 4f 3a 20 49 66 20 50 31 20 69 73  G/TODO: If P1 is
25835 20 61 20 70 73 65 75 64 6f 2d 63 75 72 73 6f 72   a pseudo-cursor
25836 20 61 6e 64 20 50 32 20 69 73 20 64 79 6e 61 6d   and P2 is dynam
25837 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61  ically.** alloca
25838 74 65 64 2c 20 74 68 65 6e 20 6f 77 6e 65 72 73  ted, then owners
25839 68 69 70 20 6f 66 20 50 32 20 69 73 20 74 72 61  hip of P2 is tra
2583a 6e 73 66 65 72 72 65 64 20 74 6f 20 74 68 65 20  nsferred to the 
2583b 70 73 65 75 64 6f 2d 63 75 72 73 6f 72 0a 2a 2a  pseudo-cursor.**
2583c 20 61 6e 64 20 72 65 67 69 73 74 65 72 20 50 32   and register P2
2583d 20 62 65 63 6f 6d 65 73 20 65 70 68 65 6d 65 72   becomes ephemer
2583e 61 6c 2e 20 20 49 66 20 74 68 65 20 63 75 72 73  al.  If the curs
2583f 6f 72 20 69 73 20 63 68 61 6e 67 65 64 2c 20 74  or is changed, t
25840 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 72  he.** value of r
25841 65 67 69 73 74 65 72 20 50 32 20 77 69 6c 6c 20  egister P2 will 
25842 74 68 65 6e 20 63 68 61 6e 67 65 2e 20 20 4d 61  then change.  Ma
25843 6b 65 20 73 75 72 65 20 74 68 69 73 20 64 6f 65  ke sure this doe
25844 73 20 6e 6f 74 0a 2a 2a 20 63 61 75 73 65 20 61  s not.** cause a
25845 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 29 0a 2a 2a  ny problems.).**
25846 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63  .** This instruc
25847 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  tion only works 
25848 6f 6e 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20  on tables.  The 
25849 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74 72  equivalent instr
2584a 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 69 6e  uction.** for in
2584b 64 69 63 65 73 20 69 73 20 4f 50 5f 49 64 78 49  dices is OP_IdxI
2584c 6e 73 65 72 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63  nsert..*/./* Opc
2584d 6f 64 65 3a 20 49 6e 73 65 72 74 49 6e 74 20 50  ode: InsertInt P
2584e 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
2584f 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 65  .** This works e
25850 78 61 63 74 6c 79 20 6c 69 6b 65 20 4f 50 5f 49  xactly like OP_I
25851 6e 73 65 72 74 20 65 78 63 65 70 74 20 74 68 61  nsert except tha
25852 74 20 74 68 65 20 6b 65 79 20 69 73 20 74 68 65  t the key is the
25853 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75  .** integer valu
25854 65 20 50 33 2c 20 6e 6f 74 20 74 68 65 20 76 61  e P3, not the va
25855 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67  lue of the integ
25856 65 72 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  er stored in reg
25857 69 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63 61 73  ister P3..*/.cas
25858 65 20 4f 50 5f 49 6e 73 65 72 74 3a 20 0a 63 61  e OP_Insert: .ca
25859 73 65 20 4f 50 5f 49 6e 73 65 72 74 49 6e 74 3a  se OP_InsertInt:
2585a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
2585b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
2585c 65 64 20 69 6e 74 6f 20 75 2e 62 69 20 2a 2f 0a  ed into u.bi */.
2585d 20 20 4d 65 6d 20 2a 70 44 61 74 61 3b 20 20 20    Mem *pData;   
2585e 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20      /* MEM cell 
2585f 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 66 6f 72  holding data for
25860 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62   the record to b
25861 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  e inserted */.  
25862 4d 65 6d 20 2a 70 4b 65 79 3b 20 20 20 20 20 20  Mem *pKey;      
25863 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f    /* MEM cell ho
25864 6c 64 69 6e 67 20 6b 65 79 20 20 66 6f 72 20 74  lding key  for t
25865 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69  he record */.  i
25866 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20  64 iKey;        
25867 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20   /* The integer 
25868 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 6f 72  ROWID or key for
25869 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62   the record to b
2586a 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  e inserted */.  
2586b 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20  VdbeCursor *pC; 
2586c 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20 74    /* Cursor to t
2586d 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20  able into which 
2586e 69 6e 73 65 72 74 20 69 73 20 77 72 69 74 74 65  insert is writte
2586f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f  n */.  int nZero
25870 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
25871 65 72 20 6f 66 20 7a 65 72 6f 2d 62 79 74 65 73  er of zero-bytes
25872 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f 0a 20 20   to append */.  
25873 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 20  int seekResult; 
25874 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70    /* Result of p
25875 72 69 6f 72 20 73 65 65 6b 20 6f 72 20 30 20 69  rior seek or 0 i
25876 66 20 6e 6f 20 55 53 45 53 45 45 4b 52 45 53 55  f no USESEEKRESU
25877 4c 54 20 66 6c 61 67 20 2a 2f 0a 20 20 63 6f 6e  LT flag */.  con
25878 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 2f  st char *zDb;  /
25879 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  * database name 
2587a 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 75 70  - used by the up
2587b 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 63  date hook */.  c
2587c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 3b  onst char *zTbl;
2587d 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2d   /* Table name -
2587e 20 75 73 65 64 20 62 79 20 74 68 65 20 6f 70 64   used by the opd
2587f 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 69 6e  ate hook */.  in
25880 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  t op;           
25881 2f 2a 20 4f 70 63 6f 64 65 20 66 6f 72 20 75 70  /* Opcode for up
25882 64 61 74 65 20 68 6f 6f 6b 3a 20 53 51 4c 49 54  date hook: SQLIT
25883 45 5f 55 50 44 41 54 45 20 6f 72 20 53 51 4c 49  E_UPDATE or SQLI
25884 54 45 5f 49 4e 53 45 52 54 20 2a 2f 0a 23 65 6e  TE_INSERT */.#en
25885 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
25886 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
25887 6f 20 75 2e 62 69 20 2a 2f 0a 0a 20 20 75 2e 62  o u.bi */..  u.b
25888 69 2e 70 44 61 74 61 20 3d 20 26 61 4d 65 6d 5b  i.pData = &aMem[
25889 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65  pOp->p2];.  asse
2588a 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
2588b 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
2588c 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74  rsor );.  assert
2588d 28 20 6d 65 6d 49 73 56 61 6c 69 64 28 75 2e 62  ( memIsValid(u.b
2588e 69 2e 70 44 61 74 61 29 20 29 3b 0a 20 20 75 2e  i.pData) );.  u.
2588f 62 69 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72  bi.pC = p->apCsr
25890 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
25891 65 72 74 28 20 75 2e 62 69 2e 70 43 21 3d 30 20  ert( u.bi.pC!=0 
25892 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  );.  assert( u.b
25893 69 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30  i.pC->pCursor!=0
25894 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e   );.  assert( u.
25895 62 69 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62  bi.pC->pseudoTab
25896 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 61 73  leReg==0 );.  as
25897 73 65 72 74 28 20 75 2e 62 69 2e 70 43 2d 3e 69  sert( u.bi.pC->i
25898 73 54 61 62 6c 65 20 29 3b 0a 20 20 52 45 47 49  sTable );.  REGI
25899 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
2589a 70 32 2c 20 75 2e 62 69 2e 70 44 61 74 61 29 3b  p2, u.bi.pData);
2589b 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63  ..  if( pOp->opc
2589c 6f 64 65 3d 3d 4f 50 5f 49 6e 73 65 72 74 20 29  ode==OP_Insert )
2589d 7b 0a 20 20 20 20 75 2e 62 69 2e 70 4b 65 79 20  {.    u.bi.pKey 
2589e 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  = &aMem[pOp->p3]
2589f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  ;.    assert( u.
258a0 62 69 2e 70 4b 65 79 2d 3e 66 6c 61 67 73 20 26  bi.pKey->flags &
258a1 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20   MEM_Int );.    
258a2 61 73 73 65 72 74 28 20 6d 65 6d 49 73 56 61 6c  assert( memIsVal
258a3 69 64 28 75 2e 62 69 2e 70 4b 65 79 29 20 29 3b  id(u.bi.pKey) );
258a4 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52  .    REGISTER_TR
258a5 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 62  ACE(pOp->p3, u.b
258a6 69 2e 70 4b 65 79 29 3b 0a 20 20 20 20 75 2e 62  i.pKey);.    u.b
258a7 69 2e 69 4b 65 79 20 3d 20 75 2e 62 69 2e 70 4b  i.iKey = u.bi.pK
258a8 65 79 2d 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65  ey->u.i;.  }else
258a9 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  {.    assert( pO
258aa 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e  p->opcode==OP_In
258ab 73 65 72 74 49 6e 74 20 29 3b 0a 20 20 20 20 75  sertInt );.    u
258ac 2e 62 69 2e 69 4b 65 79 20 3d 20 70 4f 70 2d 3e  .bi.iKey = pOp->
258ad 70 33 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  p3;.  }..  if( p
258ae 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f  Op->p5 & OPFLAG_
258af 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e 43 68  NCHANGE ) p->nCh
258b0 61 6e 67 65 2b 2b 3b 0a 20 20 69 66 28 20 70 4f  ange++;.  if( pO
258b1 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 4c  p->p5 & OPFLAG_L
258b2 41 53 54 52 4f 57 49 44 20 29 20 64 62 2d 3e 6c  ASTROWID ) db->l
258b3 61 73 74 52 6f 77 69 64 20 3d 20 6c 61 73 74 52  astRowid = lastR
258b4 6f 77 69 64 20 3d 20 75 2e 62 69 2e 69 4b 65 79  owid = u.bi.iKey
258b5 3b 0a 20 20 69 66 28 20 75 2e 62 69 2e 70 44 61  ;.  if( u.bi.pDa
258b6 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  ta->flags & MEM_
258b7 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 62 69  Null ){.    u.bi
258b8 2e 70 44 61 74 61 2d 3e 7a 20 3d 20 30 3b 0a 20  .pData->z = 0;. 
258b9 20 20 20 75 2e 62 69 2e 70 44 61 74 61 2d 3e 6e     u.bi.pData->n
258ba 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
258bb 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e     assert( u.bi.
258bc 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 28  pData->flags & (
258bd 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72  MEM_Blob|MEM_Str
258be 29 20 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 69 2e  ) );.  }.  u.bi.
258bf 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 28 28 70  seekResult = ((p
258c0 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f  Op->p5 & OPFLAG_
258c1 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 20 3f  USESEEKRESULT) ?
258c2 20 75 2e 62 69 2e 70 43 2d 3e 73 65 65 6b 52 65   u.bi.pC->seekRe
258c3 73 75 6c 74 20 3a 20 30 29 3b 0a 20 20 69 66 28  sult : 0);.  if(
258c4 20 75 2e 62 69 2e 70 44 61 74 61 2d 3e 66 6c 61   u.bi.pData->fla
258c5 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b  gs & MEM_Zero ){
258c6 0a 20 20 20 20 75 2e 62 69 2e 6e 5a 65 72 6f 20  .    u.bi.nZero 
258c7 3d 20 75 2e 62 69 2e 70 44 61 74 61 2d 3e 75 2e  = u.bi.pData->u.
258c8 6e 5a 65 72 6f 3b 0a 20 20 7d 65 6c 73 65 7b 0a  nZero;.  }else{.
258c9 20 20 20 20 75 2e 62 69 2e 6e 5a 65 72 6f 20 3d      u.bi.nZero =
258ca 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
258cb 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64 52  3BtreeSetCachedR
258cc 6f 77 69 64 28 75 2e 62 69 2e 70 43 2d 3e 70 43  owid(u.bi.pC->pC
258cd 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 63 20  ursor, 0);.  rc 
258ce 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e  = sqlite3BtreeIn
258cf 73 65 72 74 28 75 2e 62 69 2e 70 43 2d 3e 70 43  sert(u.bi.pC->pC
258d0 75 72 73 6f 72 2c 20 30 2c 20 75 2e 62 69 2e 69  ursor, 0, u.bi.i
258d1 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Key,.           
258d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
258d3 2e 62 69 2e 70 44 61 74 61 2d 3e 7a 2c 20 75 2e  .bi.pData->z, u.
258d4 62 69 2e 70 44 61 74 61 2d 3e 6e 2c 20 75 2e 62  bi.pData->n, u.b
258d5 69 2e 6e 5a 65 72 6f 2c 0a 20 20 20 20 20 20 20  i.nZero,.       
258d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
258d7 20 20 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46     pOp->p5 & OPF
258d8 4c 41 47 5f 41 50 50 45 4e 44 2c 20 75 2e 62 69  LAG_APPEND, u.bi
258d9 2e 73 65 65 6b 52 65 73 75 6c 74 0a 20 20 29 3b  .seekResult.  );
258da 0a 20 20 75 2e 62 69 2e 70 43 2d 3e 72 6f 77 69  .  u.bi.pC->rowi
258db 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
258dc 75 2e 62 69 2e 70 43 2d 3e 64 65 66 65 72 72 65  u.bi.pC->deferre
258dd 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 75  dMoveto = 0;.  u
258de 2e 62 69 2e 70 43 2d 3e 63 61 63 68 65 53 74 61  .bi.pC->cacheSta
258df 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
258e0 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  E;..  /* Invoke 
258e1 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20  the update-hook 
258e2 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a  if required. */.
258e3 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
258e4 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61  _OK && db->xUpda
258e5 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f  teCallback && pO
258e6 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 75  p->p4.z ){.    u
258e7 2e 62 69 2e 7a 44 62 20 3d 20 64 62 2d 3e 61 44  .bi.zDb = db->aD
258e8 62 5b 75 2e 62 69 2e 70 43 2d 3e 69 44 62 5d 2e  b[u.bi.pC->iDb].
258e9 7a 4e 61 6d 65 3b 0a 20 20 20 20 75 2e 62 69 2e  zName;.    u.bi.
258ea 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a  zTbl = pOp->p4.z
258eb 3b 0a 20 20 20 20 75 2e 62 69 2e 6f 70 20 3d 20  ;.    u.bi.op = 
258ec 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c  ((pOp->p5 & OPFL
258ed 41 47 5f 49 53 55 50 44 41 54 45 29 20 3f 20 53  AG_ISUPDATE) ? S
258ee 51 4c 49 54 45 5f 55 50 44 41 54 45 20 3a 20 53  QLITE_UPDATE : S
258ef 51 4c 49 54 45 5f 49 4e 53 45 52 54 29 3b 0a 20  QLITE_INSERT);. 
258f0 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e     assert( u.bi.
258f1 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20  pC->isTable );. 
258f2 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61     db->xUpdateCa
258f3 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 61  llback(db->pUpda
258f4 74 65 41 72 67 2c 20 75 2e 62 69 2e 6f 70 2c 20  teArg, u.bi.op, 
258f5 75 2e 62 69 2e 7a 44 62 2c 20 75 2e 62 69 2e 7a  u.bi.zDb, u.bi.z
258f6 54 62 6c 2c 20 75 2e 62 69 2e 69 4b 65 79 29 3b  Tbl, u.bi.iKey);
258f7 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
258f8 69 2e 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a  i.pC->iDb>=0 );.
258f9 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
258fa 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 6c 65 74  /* Opcode: Delet
258fb 65 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a  e P1 P2 * P4 *.*
258fc 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20  *.** Delete the 
258fd 72 65 63 6f 72 64 20 61 74 20 77 68 69 63 68 20  record at which 
258fe 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73  the P1 cursor is
258ff 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
25900 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ing..**.** The c
25901 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 6c 65  ursor will be le
25902 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 65  ft pointing at e
25903 69 74 68 65 72 20 74 68 65 20 6e 65 78 74 20 6f  ither the next o
25904 72 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a  r the previous.*
25905 2a 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20  * record in the 
25906 74 61 62 6c 65 2e 20 49 66 20 69 74 20 69 73 20  table. If it is 
25907 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
25908 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72 64   the next record
25909 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65  , then.** the ne
2590a 78 74 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74  xt Next instruct
2590b 69 6f 6e 20 77 69 6c 6c 20 62 65 20 61 20 6e 6f  ion will be a no
2590c 2d 6f 70 2e 20 20 48 65 6e 63 65 20 69 74 20 69  -op.  Hence it i
2590d 73 20 4f 4b 20 74 6f 20 64 65 6c 65 74 65 0a 2a  s OK to delete.*
2590e 2a 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20  * a record from 
2590f 77 69 74 68 69 6e 20 61 6e 20 4e 65 78 74 20 6c  within an Next l
25910 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  oop..**.** If th
25911 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  e OPFLAG_NCHANGE
25912 20 66 6c 61 67 20 6f 66 20 50 32 20 69 73 20 73   flag of P2 is s
25913 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77  et, then the row
25914 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73   change count is
25915 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20  .** incremented 
25916 28 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e  (otherwise not).
25917 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75 73 74 20 6e  .**.** P1 must n
25918 6f 74 20 62 65 20 70 73 65 75 64 6f 2d 74 61 62  ot be pseudo-tab
25919 6c 65 2e 20 20 49 74 20 68 61 73 20 74 6f 20 62  le.  It has to b
2591a 65 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 77  e a real table w
2591b 69 74 68 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20  ith.** multiple 
2591c 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  rows..**.** If P
2591d 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  4 is not NULL, t
2591e 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 6e 61  hen it is the na
2591f 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
25920 74 68 61 74 20 50 31 20 69 73 0a 2a 2a 20 70 6f  that P1 is.** po
25921 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20  inting to.  The 
25922 75 70 64 61 74 65 20 68 6f 6f 6b 20 77 69 6c 6c  update hook will
25923 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 69 66 20   be invoked, if 
25924 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 20 49 66  it exists..** If
25925 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20   P4 is not NULL 
25926 74 68 65 6e 20 74 68 65 20 50 31 20 63 75 72 73  then the P1 curs
25927 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 65  or must have bee
25928 6e 20 70 6f 73 69 74 69 6f 6e 65 64 0a 2a 2a 20  n positioned.** 
25929 75 73 69 6e 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e  using OP_NotFoun
2592a 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b  d prior to invok
2592b 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e  ing this opcode.
2592c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 6c 65  .*/.case OP_Dele
2592d 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  te: {.#if 0  /* 
2592e 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
2592f 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6a 20  moved into u.bj 
25930 2a 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 0a 20  */.  i64 iKey;. 
25931 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
25932 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
25933 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
25934 20 69 6e 74 6f 20 75 2e 62 6a 20 2a 2f 0a 0a 20   into u.bj */.. 
25935 20 75 2e 62 6a 2e 69 4b 65 79 20 3d 20 30 3b 0a   u.bj.iKey = 0;.
25936 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
25937 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
25938 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
25939 75 2e 62 6a 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bj.pC = p->apC
2593a 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  sr[pOp->p1];.  a
2593b 73 73 65 72 74 28 20 75 2e 62 6a 2e 70 43 21 3d  ssert( u.bj.pC!=
2593c 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  0 );.  assert( u
2593d 2e 62 6a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21  .bj.pC->pCursor!
2593e 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 76  =0 );  /* Only v
2593f 61 6c 69 64 20 66 6f 72 20 72 65 61 6c 20 74 61  alid for real ta
25940 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 64 6f 74  bles, no pseudot
25941 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49  ables */..  /* I
25942 66 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f  f the update-hoo
25943 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65  k will be invoke
25944 64 2c 20 73 65 74 20 75 2e 62 6a 2e 69 4b 65 79  d, set u.bj.iKey
25945 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 6f 66   to the rowid of
25946 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65   the.  ** row be
25947 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a  ing deleted..  *
25948 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 55 70 64  /.  if( db->xUpd
25949 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70  ateCallback && p
2594a 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20  Op->p4.z ){.    
2594b 61 73 73 65 72 74 28 20 75 2e 62 6a 2e 70 43 2d  assert( u.bj.pC-
2594c 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  >isTable );.    
2594d 61 73 73 65 72 74 28 20 75 2e 62 6a 2e 70 43 2d  assert( u.bj.pC-
2594e 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 3b  >rowidIsValid );
2594f 20 20 2f 2a 20 6c 61 73 74 52 6f 77 69 64 20 73    /* lastRowid s
25950 65 74 20 62 79 20 70 72 65 76 69 6f 75 73 20 4f  et by previous O
25951 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a 2f 0a 20 20  P_NotFound */.  
25952 20 20 75 2e 62 6a 2e 69 4b 65 79 20 3d 20 75 2e    u.bj.iKey = u.
25953 62 6a 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64  bj.pC->lastRowid
25954 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  ;.  }..  /* The 
25955 4f 50 5f 44 65 6c 65 74 65 20 6f 70 63 6f 64 65  OP_Delete opcode
25956 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 73 20   always follows 
25957 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20  an OP_NotExists 
25958 6f 72 20 4f 50 5f 4c 61 73 74 20 6f 72 0a 20 20  or OP_Last or.  
25959 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 6e 20  ** OP_Column on 
2595a 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20 77  the same table w
2595b 69 74 68 6f 75 74 20 61 6e 79 20 69 6e 74 65 72  ithout any inter
2595c 76 65 6e 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  vening operation
2595d 73 20 74 68 61 74 0a 20 20 2a 2a 20 6d 69 67 68  s that.  ** migh
2595e 74 20 6d 6f 76 65 20 6f 72 20 69 6e 76 61 6c 69  t move or invali
2595f 64 61 74 65 20 74 68 65 20 63 75 72 73 6f 72 2e  date the cursor.
25960 20 20 48 65 6e 63 65 20 63 75 72 73 6f 72 20 75    Hence cursor u
25961 2e 62 6a 2e 70 43 20 69 73 20 61 6c 77 61 79 73  .bj.pC is always
25962 20 70 6f 69 6e 74 69 6e 67 0a 20 20 2a 2a 20 74   pointing.  ** t
25963 6f 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20  o the row to be 
25964 64 65 6c 65 74 65 64 20 61 6e 64 20 74 68 65 20  deleted and the 
25965 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f  sqlite3VdbeCurso
25966 72 4d 6f 76 65 74 6f 28 29 20 6f 70 65 72 61 74  rMoveto() operat
25967 69 6f 6e 0a 20 20 2a 2a 20 62 65 6c 6f 77 20 69  ion.  ** below i
25968 73 20 61 6c 77 61 79 73 20 61 20 6e 6f 2d 6f 70  s always a no-op
25969 20 61 6e 64 20 63 61 6e 6e 6f 74 20 66 61 69 6c   and cannot fail
2596a 2e 20 20 57 65 20 77 69 6c 6c 20 72 75 6e 20 69  .  We will run i
2596b 74 20 61 6e 79 68 6f 77 2c 20 74 68 6f 75 67 68  t anyhow, though
2596c 2c 0a 20 20 2a 2a 20 74 6f 20 67 75 61 72 64 20  ,.  ** to guard 
2596d 61 67 61 69 6e 73 74 20 66 75 74 75 72 65 20 63  against future c
2596e 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 63 6f  hanges to the co
2596f 64 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a 20 20  de generator..  
25970 2a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 75 2e  **/.  assert( u.
25971 62 6a 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d  bj.pC->deferredM
25972 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63  oveto==0 );.  rc
25973 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
25974 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 6a 2e  rsorMoveto(u.bj.
25975 70 43 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52  pC);.  if( NEVER
25976 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20  (rc!=SQLITE_OK) 
25977 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
25978 5f 74 6f 5f 65 72 72 6f 72 3b 0a 0a 20 20 73 71  _to_error;..  sq
25979 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63  lite3BtreeSetCac
2597a 68 65 64 52 6f 77 69 64 28 75 2e 62 6a 2e 70 43  hedRowid(u.bj.pC
2597b 2d 3e 70 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20  ->pCursor, 0);. 
2597c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
2597d 65 65 44 65 6c 65 74 65 28 75 2e 62 6a 2e 70 43  eeDelete(u.bj.pC
2597e 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e  ->pCursor);.  u.
2597f 62 6a 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74  bj.pC->cacheStat
25980 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
25981 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74  ;..  /* Invoke t
25982 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69  he update-hook i
25983 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20  f required. */. 
25984 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
25985 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74  OK && db->xUpdat
25986 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70  eCallback && pOp
25987 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63 6f  ->p4.z ){.    co
25988 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  nst char *zDb = 
25989 64 62 2d 3e 61 44 62 5b 75 2e 62 6a 2e 70 43 2d  db->aDb[u.bj.pC-
2598a 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  >iDb].zName;.   
2598b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62   const char *zTb
2598c 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20  l = pOp->p4.z;. 
2598d 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61     db->xUpdateCa
2598e 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 61  llback(db->pUpda
2598f 74 65 41 72 67 2c 20 53 51 4c 49 54 45 5f 44 45  teArg, SQLITE_DE
25990 4c 45 54 45 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c  LETE, zDb, zTbl,
25991 20 75 2e 62 6a 2e 69 4b 65 79 29 3b 0a 20 20 20   u.bj.iKey);.   
25992 20 61 73 73 65 72 74 28 20 75 2e 62 6a 2e 70 43   assert( u.bj.pC
25993 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a  ->iDb>=0 );.  }.
25994 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26 20    if( pOp->p2 & 
25995 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29  OPFLAG_NCHANGE )
25996 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20   p->nChange++;. 
25997 20 62 72 65 61 6b 3b 0a 7d 0a 2f 2a 20 4f 70 63   break;.}./* Opc
25998 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 6e 74 20  ode: ResetCount 
25999 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  * * * * *.**.** 
2599a 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  The value of the
2599b 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
2599c 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68 65  is copied to the
2599d 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
2599e 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  .** change count
2599f 65 72 20 28 72 65 74 75 72 6e 65 64 20 62 79 20  er (returned by 
259a0 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
259a1 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 68 61 6e   to sqlite3_chan
259a2 67 65 73 28 29 29 2e 0a 2a 2a 20 54 68 65 6e 20  ges())..** Then 
259a3 74 68 65 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c  the VMs internal
259a4 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
259a5 72 65 73 65 74 73 20 74 6f 20 30 2e 0a 2a 2a 20  resets to 0..** 
259a6 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
259a7 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73  trigger programs
259a8 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73  ..*/.case OP_Res
259a9 65 74 43 6f 75 6e 74 3a 20 7b 0a 20 20 73 71 6c  etCount: {.  sql
259aa 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67  ite3VdbeSetChang
259ab 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67  es(db, p->nChang
259ac 65 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65  e);.  p->nChange
259ad 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   = 0;.  break;.}
259ae 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 6f 72  ../* Opcode: Sor
259af 74 65 72 43 6f 6d 70 61 72 65 20 50 31 20 50 32  terCompare P1 P2
259b0 20 50 33 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20   P3.**.** P1 is 
259b1 61 20 73 6f 72 74 65 72 20 63 75 72 73 6f 72 2e  a sorter cursor.
259b2 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
259b3 6e 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20 72  n compares the r
259b4 65 63 6f 72 64 20 62 6c 6f 62 20 69 6e 20 0a 2a  ecord blob in .*
259b5 2a 20 72 65 67 69 73 74 65 72 20 50 33 20 77 69  * register P3 wi
259b6 74 68 20 74 68 65 20 65 6e 74 72 79 20 74 68 61  th the entry tha
259b7 74 20 74 68 65 20 73 6f 72 74 65 72 20 63 75 72  t the sorter cur
259b8 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f  sor currently po
259b9 69 6e 74 73 20 74 6f 2e 0a 2a 2a 20 49 66 2c 20  ints to..** If, 
259ba 65 78 63 6c 75 64 69 6e 67 20 74 68 65 20 72 6f  excluding the ro
259bb 77 69 64 20 66 69 65 6c 64 73 20 61 74 20 74 68  wid fields at th
259bc 65 20 65 6e 64 2c 20 74 68 65 20 74 77 6f 20 72  e end, the two r
259bd 65 63 6f 72 64 73 20 61 72 65 20 61 20 6d 61 74  ecords are a mat
259be 63 68 2c 0a 2a 2a 20 66 61 6c 6c 20 74 68 72 6f  ch,.** fall thro
259bf 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
259c0 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 4f 74 68  instruction. Oth
259c1 65 72 77 69 73 65 2c 20 6a 75 6d 70 20 74 6f 20  erwise, jump to 
259c2 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 0a  instruction P2..
259c3 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 6f 72 74 65  */.case OP_Sorte
259c4 72 43 6f 6d 70 61 72 65 3a 20 7b 0a 23 69 66 20  rCompare: {.#if 
259c5 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
259c6 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
259c7 20 75 2e 62 6b 20 2a 2f 0a 20 20 56 64 62 65 43   u.bk */.  VdbeC
259c8 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 6e 74  ursor *pC;.  int
259c9 20 72 65 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20   res;.#endif /* 
259ca 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
259cb 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6b 20  moved into u.bk 
259cc 2a 2f 0a 0a 20 20 75 2e 62 6b 2e 70 43 20 3d 20  */..  u.bk.pC = 
259cd 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
259ce 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 53  ];.  assert( isS
259cf 6f 72 74 65 72 28 75 2e 62 6b 2e 70 43 29 20 29  orter(u.bk.pC) )
259d0 3b 0a 20 20 70 49 6e 33 20 3d 20 26 61 4d 65 6d  ;.  pIn3 = &aMem
259d1 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 72 63 20  [pOp->p3];.  rc 
259d2 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f 72  = sqlite3VdbeSor
259d3 74 65 72 43 6f 6d 70 61 72 65 28 75 2e 62 6b 2e  terCompare(u.bk.
259d4 70 43 2c 20 70 49 6e 33 2c 20 26 75 2e 62 6b 2e  pC, pIn3, &u.bk.
259d5 72 65 73 29 3b 0a 20 20 69 66 28 20 75 2e 62 6b  res);.  if( u.bk
259d6 2e 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d  .res ){.    pc =
259d7 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a   pOp->p2-1;.  }.
259d8 20 20 62 72 65 61 6b 3b 0a 7d 3b 0a 0a 2f 2a 20    break;.};../* 
259d9 4f 70 63 6f 64 65 3a 20 53 6f 72 74 65 72 44 61  Opcode: SorterDa
259da 74 61 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ta P1 P2 * * *.*
259db 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20  *.** Write into 
259dc 72 65 67 69 73 74 65 72 20 50 32 20 74 68 65 20  register P2 the 
259dd 63 75 72 72 65 6e 74 20 73 6f 72 74 65 72 20 64  current sorter d
259de 61 74 61 20 66 6f 72 20 73 6f 72 74 65 72 20 63  ata for sorter c
259df 75 72 73 6f 72 20 50 31 2e 0a 2a 2f 0a 63 61 73  ursor P1..*/.cas
259e0 65 20 4f 50 5f 53 6f 72 74 65 72 44 61 74 61 3a  e OP_SorterData:
259e1 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
259e2 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
259e3 65 64 20 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f 0a  ed into u.bl */.
259e4 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
259e5 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
259e6 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
259e7 64 20 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f 0a 0a  d into u.bl */..
259e8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
259e9 4d 49 54 5f 4d 45 52 47 45 5f 53 4f 52 54 0a 20  MIT_MERGE_SORT. 
259ea 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f   pOut = &aMem[pO
259eb 70 2d 3e 70 32 5d 3b 0a 20 20 75 2e 62 6c 2e 70  p->p2];.  u.bl.p
259ec 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
259ed 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
259ee 20 75 2e 62 6c 2e 70 43 2d 3e 69 73 53 6f 72 74   u.bl.pC->isSort
259ef 65 72 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  er );.  rc = sql
259f0 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 52 6f  ite3VdbeSorterRo
259f1 77 6b 65 79 28 75 2e 62 6c 2e 70 43 2c 20 70 4f  wkey(u.bl.pC, pO
259f2 75 74 29 3b 0a 23 65 6c 73 65 0a 20 20 70 4f 70  ut);.#else.  pOp
259f3 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 52 6f  ->opcode = OP_Ro
259f4 77 4b 65 79 3b 0a 20 20 70 63 2d 2d 3b 0a 23 65  wKey;.  pc--;.#e
259f5 6e 64 69 66 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ndif.  break;.}.
259f6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 44  ./* Opcode: RowD
259f7 61 74 61 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ata P1 P2 * * *.
259f8 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f  **.** Write into
259f9 20 72 65 67 69 73 74 65 72 20 50 32 20 74 68 65   register P2 the
259fa 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 64 61   complete row da
259fb 74 61 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31  ta for cursor P1
259fc 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f  ..** There is no
259fd 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20   interpretation 
259fe 6f 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a 2a  of the data.  .*
259ff 2a 20 49 74 20 69 73 20 6a 75 73 74 20 63 6f 70  * It is just cop
25a00 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 32 20  ied onto the P2 
25a01 72 65 67 69 73 74 65 72 20 65 78 61 63 74 6c 79  register exactly
25a02 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f   as .** it is fo
25a03 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62  und in the datab
25a04 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
25a05 49 66 20 74 68 65 20 50 31 20 63 75 72 73 6f 72  If the P1 cursor
25a06 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e   must be pointin
25a07 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77  g to a valid row
25a08 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77   (not a NULL row
25a09 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74  ).** of a real t
25a0a 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75  able, not a pseu
25a0b 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20  do-table..*/./* 
25a0c 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65 79 20 50  Opcode: RowKey P
25a0d 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
25a0e 20 57 72 69 74 65 20 69 6e 74 6f 20 72 65 67 69   Write into regi
25a0f 73 74 65 72 20 50 32 20 74 68 65 20 63 6f 6d 70  ster P2 the comp
25a10 6c 65 74 65 20 72 6f 77 20 6b 65 79 20 66 6f 72  lete row key for
25a11 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54   cursor P1..** T
25a12 68 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 72  here is no inter
25a13 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  pretation of the
25a14 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54 68 65 20   data.  .** The 
25a15 6b 65 79 20 69 73 20 63 6f 70 69 65 64 20 6f 6e  key is copied on
25a16 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 73 74  to the P3 regist
25a17 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 0a 2a  er exactly as .*
25a18 2a 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69 6e  * it is found in
25a19 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
25a1a 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  le..**.** If the
25a1b 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20   P1 cursor must 
25a1c 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61  be pointing to a
25a1d 20 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20   valid row (not 
25a1e 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f  a NULL row).** o
25a1f 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20  f a real table, 
25a20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62  not a pseudo-tab
25a21 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  le..*/.case OP_R
25a22 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f 50 5f 52  owKey:.case OP_R
25a23 6f 77 44 61 74 61 3a 20 7b 0a 23 69 66 20 30 20  owData: {.#if 0 
25a24 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
25a25 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
25a26 2e 62 6d 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  .bm */.  VdbeCur
25a27 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72  sor *pC;.  BtCur
25a28 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 75 33  sor *pCrsr;.  u3
25a29 32 20 6e 3b 0a 20 20 69 36 34 20 6e 36 34 3b 0a  2 n;.  i64 n64;.
25a2a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
25a2b 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
25a2c 69 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0a 0a 20 20  into u.bm */..  
25a2d 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pOut = &aMem[pOp
25a2e 2d 3e 70 32 5d 3b 0a 20 20 6d 65 6d 41 62 6f 75  ->p2];.  memAbou
25a2f 74 54 6f 43 68 61 6e 67 65 28 70 2c 20 70 4f 75  tToChange(p, pOu
25a30 74 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65 20 74  t);..  /* Note t
25a31 68 61 74 20 52 6f 77 4b 65 79 20 61 6e 64 20 52  hat RowKey and R
25a32 6f 77 44 61 74 61 20 61 72 65 20 72 65 61 6c 6c  owData are reall
25a33 79 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61  y exactly the sa
25a34 6d 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a  me instruction *
25a35 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  /.  assert( pOp-
25a36 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
25a37 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
25a38 20 20 75 2e 62 6d 2e 70 43 20 3d 20 70 2d 3e 61    u.bm.pC = p->a
25a39 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  pCsr[pOp->p1];. 
25a3a 20 61 73 73 65 72 74 28 20 75 2e 62 6d 2e 70 43   assert( u.bm.pC
25a3b 2d 3e 69 73 53 6f 72 74 65 72 3d 3d 30 20 29 3b  ->isSorter==0 );
25a3c 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6d 2e  .  assert( u.bm.
25a3d 70 43 2d 3e 69 73 54 61 62 6c 65 20 7c 7c 20 70  pC->isTable || p
25a3e 4f 70 2d 3e 6f 70 63 6f 64 65 21 3d 4f 50 5f 52  Op->opcode!=OP_R
25a3f 6f 77 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65  owData );.  asse
25a40 72 74 28 20 75 2e 62 6d 2e 70 43 2d 3e 69 73 49  rt( u.bm.pC->isI
25a41 6e 64 65 78 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63  ndex || pOp->opc
25a42 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44 61 74 61 20  ode==OP_RowData 
25a43 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  );.  assert( u.b
25a44 6d 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73  m.pC!=0 );.  ass
25a45 65 72 74 28 20 75 2e 62 6d 2e 70 43 2d 3e 6e 75  ert( u.bm.pC->nu
25a46 6c 6c 52 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73  llRow==0 );.  as
25a47 73 65 72 74 28 20 75 2e 62 6d 2e 70 43 2d 3e 70  sert( u.bm.pC->p
25a48 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30  seudoTableReg==0
25a49 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e   );.  assert( u.
25a4a 62 6d 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d  bm.pC->pCursor!=
25a4b 30 20 29 3b 0a 20 20 75 2e 62 6d 2e 70 43 72 73  0 );.  u.bm.pCrs
25a4c 72 20 3d 20 75 2e 62 6d 2e 70 43 2d 3e 70 43 75  r = u.bm.pC->pCu
25a4d 72 73 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20  rsor;.  assert( 
25a4e 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
25a4f 6f 72 49 73 56 61 6c 69 64 28 75 2e 62 6d 2e 70  orIsValid(u.bm.p
25a50 43 72 73 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 54  Crsr) );..  /* T
25a51 68 65 20 4f 50 5f 52 6f 77 4b 65 79 20 61 6e 64  he OP_RowKey and
25a52 20 4f 50 5f 52 6f 77 44 61 74 61 20 6f 70 63 6f   OP_RowData opco
25a53 64 65 73 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f  des always follo
25a54 77 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f  w OP_NotExists o
25a55 72 0a 20 20 2a 2a 20 4f 50 5f 52 65 77 69 6e 64  r.  ** OP_Rewind
25a56 2f 4f 70 5f 4e 65 78 74 20 77 69 74 68 20 6e 6f  /Op_Next with no
25a57 20 69 6e 74 65 72 76 65 6e 69 6e 67 20 69 6e 73   intervening ins
25a58 74 72 75 63 74 69 6f 6e 73 20 74 68 61 74 20 6d  tructions that m
25a59 69 67 68 74 20 69 6e 76 61 6c 69 64 61 74 65 0a  ight invalidate.
25a5a 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 2e    ** the cursor.
25a5b 20 20 48 65 6e 63 65 20 74 68 65 20 66 6f 6c 6c    Hence the foll
25a5c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62  owing sqlite3Vdb
25a5d 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20  eCursorMoveto() 
25a5e 63 61 6c 6c 20 69 73 20 61 6c 77 61 79 73 0a 20  call is always. 
25a5f 20 2a 2a 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20   ** a no-op and 
25a60 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 20  can never fail. 
25a61 20 42 75 74 20 77 65 20 6c 65 61 76 65 20 69 74   But we leave it
25a62 20 69 6e 20 70 6c 61 63 65 20 61 73 20 61 20 73   in place as a s
25a63 61 66 65 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73  afety..  */.  as
25a64 73 65 72 74 28 20 75 2e 62 6d 2e 70 43 2d 3e 64  sert( u.bm.pC->d
25a65 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30  eferredMoveto==0
25a66 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
25a67 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
25a68 74 6f 28 75 2e 62 6d 2e 70 43 29 3b 0a 20 20 69  to(u.bm.pC);.  i
25a69 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c  f( NEVER(rc!=SQL
25a6a 49 54 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20 61  ITE_OK) ) goto a
25a6b 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
25a6c 72 3b 0a 0a 20 20 69 66 28 20 75 2e 62 6d 2e 70  r;..  if( u.bm.p
25a6d 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20  C->isIndex ){.  
25a6e 20 20 61 73 73 65 72 74 28 20 21 75 2e 62 6d 2e    assert( !u.bm.
25a6f 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20  pC->isTable );. 
25a70 20 20 20 56 56 41 5f 4f 4e 4c 59 28 72 63 20 3d     VVA_ONLY(rc =
25a71 29 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  ) sqlite3BtreeKe
25a72 79 53 69 7a 65 28 75 2e 62 6d 2e 70 43 72 73 72  ySize(u.bm.pCrsr
25a73 2c 20 26 75 2e 62 6d 2e 6e 36 34 29 3b 0a 20 20  , &u.bm.n64);.  
25a74 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
25a75 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 2f 2a  LITE_OK );    /*
25a76 20 54 72 75 65 20 62 65 63 61 75 73 65 20 6f 66   True because of
25a77 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20   CursorMoveto() 
25a78 63 61 6c 6c 20 61 62 6f 76 65 20 2a 2f 0a 20 20  call above */.  
25a79 20 20 69 66 28 20 75 2e 62 6d 2e 6e 36 34 3e 64    if( u.bm.n64>d
25a7a 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
25a7b 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29  _LIMIT_LENGTH] )
25a7c 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f  {.      goto too
25a7d 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _big;.    }.    
25a7e 75 2e 62 6d 2e 6e 20 3d 20 28 75 33 32 29 75 2e  u.bm.n = (u32)u.
25a7f 62 6d 2e 6e 36 34 3b 0a 20 20 7d 65 6c 73 65 7b  bm.n64;.  }else{
25a80 0a 20 20 20 20 56 56 41 5f 4f 4e 4c 59 28 72 63  .    VVA_ONLY(rc
25a81 20 3d 29 20 73 71 6c 69 74 65 33 42 74 72 65 65   =) sqlite3Btree
25a82 44 61 74 61 53 69 7a 65 28 75 2e 62 6d 2e 70 43  DataSize(u.bm.pC
25a83 72 73 72 2c 20 26 75 2e 62 6d 2e 6e 29 3b 0a 20  rsr, &u.bm.n);. 
25a84 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
25a85 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 2f  QLITE_OK );    /
25a86 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61 6e  * DataSize() can
25a87 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 20 20  not fail */.    
25a88 69 66 28 20 75 2e 62 6d 2e 6e 3e 28 75 33 32 29  if( u.bm.n>(u32)
25a89 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
25a8a 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
25a8b 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f  ){.      goto to
25a8c 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 7d  o_big;.    }.  }
25a8d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
25a8e 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20  beMemGrow(pOut, 
25a8f 75 2e 62 6d 2e 6e 2c 20 30 29 20 29 7b 0a 20 20  u.bm.n, 0) ){.  
25a90 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
25a91 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 75   }.  pOut->n = u
25a92 2e 62 6d 2e 6e 3b 0a 20 20 4d 65 6d 53 65 74 54  .bm.n;.  MemSetT
25a93 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
25a94 4d 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28 20 75  M_Blob);.  if( u
25a95 2e 62 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20  .bm.pC->isIndex 
25a96 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
25a97 74 65 33 42 74 72 65 65 4b 65 79 28 75 2e 62 6d  te3BtreeKey(u.bm
25a98 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62 6d 2e  .pCrsr, 0, u.bm.
25a99 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d  n, pOut->z);.  }
25a9a 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73  else{.    rc = s
25a9b 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28  qlite3BtreeData(
25a9c 75 2e 62 6d 2e 70 43 72 73 72 2c 20 30 2c 20 75  u.bm.pCrsr, 0, u
25a9d 2e 62 6d 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b  .bm.n, pOut->z);
25a9e 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63  .  }.  pOut->enc
25a9f 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 20   = SQLITE_UTF8; 
25aa0 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20   /* In case the 
25aa1 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 61 73  blob is ever cas
25aa2 74 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 55  t to text */.  U
25aa3 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
25aa4 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61  ZE(pOut);.  brea
25aa5 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
25aa6 20 52 6f 77 69 64 20 50 31 20 50 32 20 2a 20 2a   Rowid P1 P2 * *
25aa7 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 69   *.**.** Store i
25aa8 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e  n register P2 an
25aa9 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69   integer which i
25aaa 73 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65  s the key of the
25aab 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74 68 61   table entry tha
25aac 74 0a 2a 2a 20 50 31 20 69 73 20 63 75 72 72 65  t.** P1 is curre
25aad 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 2e 0a 2a  ntly point to..*
25aae 2a 0a 2a 2a 20 50 31 20 63 61 6e 20 62 65 20 65  *.** P1 can be e
25aaf 69 74 68 65 72 20 61 6e 20 6f 72 64 69 6e 61 72  ither an ordinar
25ab0 79 20 74 61 62 6c 65 20 6f 72 20 61 20 76 69 72  y table or a vir
25ab1 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65  tual table.  The
25ab2 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 62 65  re used to.** be
25ab3 20 61 20 73 65 70 61 72 61 74 65 20 4f 50 5f 56   a separate OP_V
25ab4 52 6f 77 69 64 20 6f 70 63 6f 64 65 20 66 6f 72  Rowid opcode for
25ab5 20 75 73 65 20 77 69 74 68 20 76 69 72 74 75 61   use with virtua
25ab6 6c 20 74 61 62 6c 65 73 2c 20 62 75 74 20 74 68  l tables, but th
25ab7 69 73 0a 2a 2a 20 6f 6e 65 20 6f 70 63 6f 64 65  is.** one opcode
25ab8 20 6e 6f 77 20 77 6f 72 6b 73 20 66 6f 72 20 62   now works for b
25ab9 6f 74 68 20 74 61 62 6c 65 20 74 79 70 65 73 2e  oth table types.
25aba 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 69  .*/.case OP_Rowi
25abb 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  d: {            
25abc 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
25abd 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30  release */.#if 0
25abe 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
25abf 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
25ac0 75 2e 62 6e 20 2a 2f 0a 20 20 56 64 62 65 43 75  u.bn */.  VdbeCu
25ac1 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 34 20  rsor *pC;.  i64 
25ac2 76 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  v;.  sqlite3_vta
25ac3 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73  b *pVtab;.  cons
25ac4 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
25ac5 20 2a 70 4d 6f 64 75 6c 65 3b 0a 23 65 6e 64 69   *pModule;.#endi
25ac6 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
25ac7 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
25ac8 75 2e 62 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72  u.bn */..  asser
25ac9 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
25aca 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
25acb 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6e 2e 70 43  sor );.  u.bn.pC
25acc 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
25acd 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
25ace 75 2e 62 6e 2e 70 43 21 3d 30 20 29 3b 0a 20 20  u.bn.pC!=0 );.  
25acf 61 73 73 65 72 74 28 20 75 2e 62 6e 2e 70 43 2d  assert( u.bn.pC-
25ad0 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d  >pseudoTableReg=
25ad1 3d 30 20 7c 7c 20 75 2e 62 6e 2e 70 43 2d 3e 6e  =0 || u.bn.pC->n
25ad2 75 6c 6c 52 6f 77 20 29 3b 0a 20 20 69 66 28 20  ullRow );.  if( 
25ad3 75 2e 62 6e 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77  u.bn.pC->nullRow
25ad4 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c   ){.    pOut->fl
25ad5 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
25ad6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 65 6c      break;.  }el
25ad7 73 65 20 69 66 28 20 75 2e 62 6e 2e 70 43 2d 3e  se if( u.bn.pC->
25ad8 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 29  deferredMoveto )
25ad9 7b 0a 20 20 20 20 75 2e 62 6e 2e 76 20 3d 20 75  {.    u.bn.v = u
25ada 2e 62 6e 2e 70 43 2d 3e 6d 6f 76 65 74 6f 54 61  .bn.pC->movetoTa
25adb 72 67 65 74 3b 0a 23 69 66 6e 64 65 66 20 53 51  rget;.#ifndef SQ
25adc 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
25add 4c 54 41 42 4c 45 0a 20 20 7d 65 6c 73 65 20 69  LTABLE.  }else i
25ade 66 28 20 75 2e 62 6e 2e 70 43 2d 3e 70 56 74 61  f( u.bn.pC->pVta
25adf 62 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 75  bCursor ){.    u
25ae0 2e 62 6e 2e 70 56 74 61 62 20 3d 20 75 2e 62 6e  .bn.pVtab = u.bn
25ae1 2e 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  .pC->pVtabCursor
25ae2 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 75 2e 62  ->pVtab;.    u.b
25ae3 6e 2e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e 62 6e  n.pModule = u.bn
25ae4 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b  .pVtab->pModule;
25ae5 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
25ae6 6e 2e 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69  n.pModule->xRowi
25ae7 64 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e  d );.    rc = u.
25ae8 62 6e 2e 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77  bn.pModule->xRow
25ae9 69 64 28 75 2e 62 6e 2e 70 43 2d 3e 70 56 74 61  id(u.bn.pC->pVta
25aea 62 43 75 72 73 6f 72 2c 20 26 75 2e 62 6e 2e 76  bCursor, &u.bn.v
25aeb 29 3b 0a 20 20 20 20 69 6d 70 6f 72 74 56 74 61  );.    importVta
25aec 62 45 72 72 4d 73 67 28 70 2c 20 75 2e 62 6e 2e  bErrMsg(p, u.bn.
25aed 70 56 74 61 62 29 3b 0a 23 65 6e 64 69 66 20 2f  pVtab);.#endif /
25aee 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
25aef 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 20 20  RTUALTABLE */.  
25af0 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
25af1 74 28 20 75 2e 62 6e 2e 70 43 2d 3e 70 43 75 72  t( u.bn.pC->pCur
25af2 73 6f 72 21 3d 30 20 29 3b 0a 20 20 20 20 72 63  sor!=0 );.    rc
25af3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
25af4 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 6e 2e  rsorMoveto(u.bn.
25af5 70 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  pC);.    if( rc 
25af6 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
25af7 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 69  _to_error;.    i
25af8 66 28 20 75 2e 62 6e 2e 70 43 2d 3e 72 6f 77 69  f( u.bn.pC->rowi
25af9 64 49 73 56 61 6c 69 64 20 29 7b 0a 20 20 20 20  dIsValid ){.    
25afa 20 20 75 2e 62 6e 2e 76 20 3d 20 75 2e 62 6e 2e    u.bn.v = u.bn.
25afb 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20  pC->lastRowid;. 
25afc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
25afd 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
25afe 65 4b 65 79 53 69 7a 65 28 75 2e 62 6e 2e 70 43  eKeySize(u.bn.pC
25aff 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 6e  ->pCursor, &u.bn
25b00 2e 76 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  .v);.      asser
25b01 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
25b02 20 29 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 73   );  /* Always s
25b03 6f 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 72  o because of Cur
25b04 73 6f 72 4d 6f 76 65 74 6f 28 29 20 61 62 6f 76  sorMoveto() abov
25b05 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  e */.    }.  }. 
25b06 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62   pOut->u.i = u.b
25b07 6e 2e 76 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  n.v;.  break;.}.
25b08 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c  ./* Opcode: Null
25b09 52 6f 77 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  Row P1 * * * *.*
25b0a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75  *.** Move the cu
25b0b 72 73 6f 72 20 50 31 20 74 6f 20 61 20 6e 75 6c  rsor P1 to a nul
25b0c 6c 20 72 6f 77 2e 20 20 41 6e 79 20 4f 50 5f 43  l row.  Any OP_C
25b0d 6f 6c 75 6d 6e 20 6f 70 65 72 61 74 69 6f 6e 73  olumn operations
25b0e 0a 2a 2a 20 74 68 61 74 20 6f 63 63 75 72 20 77  .** that occur w
25b0f 68 69 6c 65 20 74 68 65 20 63 75 72 73 6f 72 20  hile the cursor 
25b10 69 73 20 6f 6e 20 74 68 65 20 6e 75 6c 6c 20 72  is on the null r
25b11 6f 77 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a  ow will always.*
25b12 2a 20 77 72 69 74 65 20 61 20 4e 55 4c 4c 2e 0a  * write a NULL..
25b13 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 52  */.case OP_NullR
25b14 6f 77 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  ow: {.#if 0  /* 
25b15 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
25b16 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6f 20  moved into u.bo 
25b17 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  */.  VdbeCursor 
25b18 2a 70 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  *pC;.#endif /* l
25b19 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
25b1a 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6f 20 2a  oved into u.bo *
25b1b 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  /..  assert( pOp
25b1c 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
25b1d 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
25b1e 0a 20 20 75 2e 62 6f 2e 70 43 20 3d 20 70 2d 3e  .  u.bo.pC = p->
25b1f 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
25b20 20 20 61 73 73 65 72 74 28 20 75 2e 62 6f 2e 70    assert( u.bo.p
25b21 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62 6f 2e 70  C!=0 );.  u.bo.p
25b22 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a  C->nullRow = 1;.
25b23 20 20 75 2e 62 6f 2e 70 43 2d 3e 72 6f 77 69 64    u.bo.pC->rowid
25b24 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 61  IsValid = 0;.  a
25b25 73 73 65 72 74 28 20 75 2e 62 6f 2e 70 43 2d 3e  ssert( u.bo.pC->
25b26 70 43 75 72 73 6f 72 20 7c 7c 20 75 2e 62 6f 2e  pCursor || u.bo.
25b27 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20  pC->pVtabCursor 
25b28 29 3b 0a 20 20 69 66 28 20 75 2e 62 6f 2e 70 43  );.  if( u.bo.pC
25b29 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20  ->pCursor ){.   
25b2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
25b2b 61 72 43 75 72 73 6f 72 28 75 2e 62 6f 2e 70 43  arCursor(u.bo.pC
25b2c 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a  ->pCursor);.  }.
25b2d 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
25b2e 70 63 6f 64 65 3a 20 4c 61 73 74 20 50 31 20 50  pcode: Last P1 P
25b2f 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68  2 * * *.**.** Th
25b30 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74 68  e next use of th
25b31 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 6d  e Rowid or Colum
25b32 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 75  n or Next instru
25b33 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a 2a  ction for P1 .**
25b34 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 74   will refer to t
25b35 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e  he last entry in
25b36 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61   the database ta
25b37 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a  ble or index..**
25b38 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   If the table or
25b39 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79 20   index is empty 
25b3a 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20 6a  and P2>0, then j
25b3b 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ump immediately 
25b3c 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32 20  to P2..** If P2 
25b3d 69 73 20 30 20 6f 72 20 69 66 20 74 68 65 20 74  is 0 or if the t
25b3e 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73  able or index is
25b3f 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c 6c   not empty, fall
25b40 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74   through.** to t
25b41 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73  he following ins
25b42 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  truction..*/.cas
25b43 65 20 4f 50 5f 4c 61 73 74 3a 20 7b 20 20 20 20  e OP_Last: {    
25b44 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23      /* jump */.#
25b45 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
25b46 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
25b47 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a 20 20 56 64  nto u.bp */.  Vd
25b48 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
25b49 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
25b4a 0a 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 6e 64  .  int res;.#end
25b4b 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
25b4c 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
25b4d 20 75 2e 62 70 20 2a 2f 0a 0a 20 20 61 73 73 65   u.bp */..  asse
25b4e 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
25b4f 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
25b50 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 70 2e 70  rsor );.  u.bp.p
25b51 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
25b52 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
25b53 20 75 2e 62 70 2e 70 43 21 3d 30 20 29 3b 0a 20   u.bp.pC!=0 );. 
25b54 20 75 2e 62 70 2e 70 43 72 73 72 20 3d 20 75 2e   u.bp.pCrsr = u.
25b55 62 70 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a  bp.pC->pCursor;.
25b56 20 20 75 2e 62 70 2e 72 65 73 20 3d 20 30 3b 0a    u.bp.res = 0;.
25b57 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62    if( ALWAYS(u.b
25b58 70 2e 70 43 72 73 72 21 3d 30 29 20 29 7b 0a 20  p.pCrsr!=0) ){. 
25b59 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
25b5a 74 72 65 65 4c 61 73 74 28 75 2e 62 70 2e 70 43  treeLast(u.bp.pC
25b5b 72 73 72 2c 20 26 75 2e 62 70 2e 72 65 73 29 3b  rsr, &u.bp.res);
25b5c 0a 20 20 7d 0a 20 20 75 2e 62 70 2e 70 43 2d 3e  .  }.  u.bp.pC->
25b5d 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e  nullRow = (u8)u.
25b5e 62 70 2e 72 65 73 3b 0a 20 20 75 2e 62 70 2e 70  bp.res;.  u.bp.p
25b5f 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
25b60 6f 20 3d 20 30 3b 0a 20 20 75 2e 62 70 2e 70 43  o = 0;.  u.bp.pC
25b61 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d  ->rowidIsValid =
25b62 20 30 3b 0a 20 20 75 2e 62 70 2e 70 43 2d 3e 63   0;.  u.bp.pC->c
25b63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43  acheStatus = CAC
25b64 48 45 5f 53 54 41 4c 45 3b 0a 20 20 69 66 28 20  HE_STALE;.  if( 
25b65 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 75 2e 62  pOp->p2>0 && u.b
25b66 70 2e 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20  p.res ){.    pc 
25b67 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
25b68 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a   }.  break;.}...
25b69 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 6f 72 74 20  /* Opcode: Sort 
25b6a 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
25b6b 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 64 6f  * This opcode do
25b6c 65 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73  es exactly the s
25b6d 61 6d 65 20 74 68 69 6e 67 20 61 73 20 4f 50 5f  ame thing as OP_
25b6e 52 65 77 69 6e 64 20 65 78 63 65 70 74 20 74 68  Rewind except th
25b6f 61 74 0a 2a 2a 20 69 74 20 69 6e 63 72 65 6d 65  at.** it increme
25b70 6e 74 73 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e  nts an undocumen
25b71 74 65 64 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ted global varia
25b72 62 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65 73  ble used for tes
25b73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 6f 72 74  ting..**.** Sort
25b74 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c 69 73  ing is accomplis
25b75 68 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 72  hed by writing r
25b76 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 20 73 6f  ecords into a so
25b77 72 74 69 6e 67 20 69 6e 64 65 78 2c 0a 2a 2a 20  rting index,.** 
25b78 74 68 65 6e 20 72 65 77 69 6e 64 69 6e 67 20 74  then rewinding t
25b79 68 61 74 20 69 6e 64 65 78 20 61 6e 64 20 70 6c  hat index and pl
25b7a 61 79 69 6e 67 20 69 74 20 62 61 63 6b 20 66 72  aying it back fr
25b7b 6f 6d 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f 0a  om beginning to.
25b7c 2a 2a 20 65 6e 64 2e 20 20 57 65 20 75 73 65 20  ** end.  We use 
25b7d 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f  the OP_Sort opco
25b7e 64 65 20 69 6e 73 74 65 61 64 20 6f 66 20 4f 50  de instead of OP
25b7f 5f 52 65 77 69 6e 64 20 74 6f 20 64 6f 20 74 68  _Rewind to do th
25b80 65 0a 2a 2a 20 72 65 77 69 6e 64 69 6e 67 20 73  e.** rewinding s
25b81 6f 20 74 68 61 74 20 74 68 65 20 67 6c 6f 62 61  o that the globa
25b82 6c 20 76 61 72 69 61 62 6c 65 20 77 69 6c 6c 20  l variable will 
25b83 62 65 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61  be incremented a
25b84 6e 64 0a 2a 2a 20 72 65 67 72 65 73 73 69 6f 6e  nd.** regression
25b85 20 74 65 73 74 73 20 63 61 6e 20 64 65 74 65 72   tests can deter
25b86 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20  mine whether or 
25b87 6e 6f 74 20 74 68 65 20 6f 70 74 69 6d 69 7a 65  not the optimize
25b88 72 20 69 73 0a 2a 2a 20 63 6f 72 72 65 63 74 6c  r is.** correctl
25b89 79 20 6f 70 74 69 6d 69 7a 69 6e 67 20 6f 75 74  y optimizing out
25b8a 20 73 6f 72 74 73 2e 0a 2a 2f 0a 63 61 73 65 20   sorts..*/.case 
25b8b 4f 50 5f 53 6f 72 74 65 72 53 6f 72 74 3a 20 20  OP_SorterSort:  
25b8c 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66    /* jump */.#if
25b8d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
25b8e 4d 45 52 47 45 5f 53 4f 52 54 0a 20 20 70 4f 70  MERGE_SORT.  pOp
25b8f 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 53 6f  ->opcode = OP_So
25b90 72 74 3b 0a 23 65 6e 64 69 66 0a 63 61 73 65 20  rt;.#endif.case 
25b91 4f 50 5f 53 6f 72 74 3a 20 7b 20 20 20 20 20 20  OP_Sort: {      
25b92 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66    /* jump */.#if
25b93 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
25b94 20 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63    sqlite3_sort_c
25b95 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65  ount++;.  sqlite
25b96 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2d 2d  3_search_count--
25b97 3b 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 61 43  ;.#endif.  p->aC
25b98 6f 75 6e 74 65 72 5b 53 51 4c 49 54 45 5f 53 54  ounter[SQLITE_ST
25b99 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 2d 31 5d  MTSTATUS_SORT-1]
25b9a 2b 2b 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68  ++;.  /* Fall th
25b9b 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f 52 65  rough into OP_Re
25b9c 77 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20 4f 70 63  wind */.}./* Opc
25b9d 6f 64 65 3a 20 52 65 77 69 6e 64 20 50 31 20 50  ode: Rewind P1 P
25b9e 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68  2 * * *.**.** Th
25b9f 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74 68  e next use of th
25ba0 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 6d  e Rowid or Colum
25ba1 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 75  n or Next instru
25ba2 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a 2a  ction for P1 .**
25ba3 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 74   will refer to t
25ba4 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69  he first entry i
25ba5 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  n the database t
25ba6 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a  able or index..*
25ba7 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f  * If the table o
25ba8 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79  r index is empty
25ba9 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20   and P2>0, then 
25baa 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
25bab 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32   to P2..** If P2
25bac 20 69 73 20 30 20 6f 72 20 69 66 20 74 68 65 20   is 0 or if the 
25bad 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69  table or index i
25bae 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c  s not empty, fal
25baf 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20  l through.** to 
25bb0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e  the following in
25bb1 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  struction..*/.ca
25bb2 73 65 20 4f 50 5f 52 65 77 69 6e 64 3a 20 7b 20  se OP_Rewind: { 
25bb3 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a         /* jump *
25bb4 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
25bb5 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
25bb6 64 20 69 6e 74 6f 20 75 2e 62 71 20 2a 2f 0a 20  d into u.bq */. 
25bb7 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
25bb8 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72  .  BtCursor *pCr
25bb9 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 23  sr;.  int res;.#
25bba 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
25bbb 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
25bbc 6e 74 6f 20 75 2e 62 71 20 2a 2f 0a 0a 20 20 61  nto u.bq */..  a
25bbd 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
25bbe 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e  0 && pOp->p1<p->
25bbf 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62  nCursor );.  u.b
25bc0 71 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b  q.pC = p->apCsr[
25bc1 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
25bc2 72 74 28 20 75 2e 62 71 2e 70 43 21 3d 30 20 29  rt( u.bq.pC!=0 )
25bc3 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 71  ;.  assert( u.bq
25bc4 2e 70 43 2d 3e 69 73 53 6f 72 74 65 72 3d 3d 28  .pC->isSorter==(
25bc5 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
25bc6 53 6f 72 74 65 72 53 6f 72 74 29 20 29 3b 0a 20  SorterSort) );. 
25bc7 20 75 2e 62 71 2e 72 65 73 20 3d 20 31 3b 0a 20   u.bq.res = 1;. 
25bc8 20 69 66 28 20 69 73 53 6f 72 74 65 72 28 75 2e   if( isSorter(u.
25bc9 62 71 2e 70 43 29 20 29 7b 0a 20 20 20 20 72 63  bq.pC) ){.    rc
25bca 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f   = sqlite3VdbeSo
25bcb 72 74 65 72 52 65 77 69 6e 64 28 64 62 2c 20 75  rterRewind(db, u
25bcc 2e 62 71 2e 70 43 2c 20 26 75 2e 62 71 2e 72 65  .bq.pC, &u.bq.re
25bcd 73 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  s);.  }else{.   
25bce 20 75 2e 62 71 2e 70 43 72 73 72 20 3d 20 75 2e   u.bq.pCrsr = u.
25bcf 62 71 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a  bq.pC->pCursor;.
25bd0 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 71      assert( u.bq
25bd1 2e 70 43 72 73 72 20 29 3b 0a 20 20 20 20 72 63  .pCrsr );.    rc
25bd2 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46   = sqlite3BtreeF
25bd3 69 72 73 74 28 75 2e 62 71 2e 70 43 72 73 72 2c  irst(u.bq.pCrsr,
25bd4 20 26 75 2e 62 71 2e 72 65 73 29 3b 0a 20 20 20   &u.bq.res);.   
25bd5 20 75 2e 62 71 2e 70 43 2d 3e 61 74 46 69 72 73   u.bq.pC->atFirs
25bd6 74 20 3d 20 75 2e 62 71 2e 72 65 73 3d 3d 30 20  t = u.bq.res==0 
25bd7 3f 31 3a 30 3b 0a 20 20 20 20 75 2e 62 71 2e 70  ?1:0;.    u.bq.p
25bd8 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
25bd9 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 71 2e  o = 0;.    u.bq.
25bda 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
25bdb 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
25bdc 20 20 20 75 2e 62 71 2e 70 43 2d 3e 72 6f 77 69     u.bq.pC->rowi
25bdd 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
25bde 7d 0a 20 20 75 2e 62 71 2e 70 43 2d 3e 6e 75 6c  }.  u.bq.pC->nul
25bdf 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e 62 71 2e  lRow = (u8)u.bq.
25be0 72 65 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70  res;.  assert( p
25be1 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d  Op->p2>0 && pOp-
25be2 3e 70 32 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20  >p2<p->nOp );.  
25be3 69 66 28 20 75 2e 62 71 2e 72 65 73 20 29 7b 0a  if( u.bq.res ){.
25be4 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
25be5 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
25be6 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
25be7 20 4e 65 78 74 20 50 31 20 50 32 20 2a 20 50 34   Next P1 P2 * P4
25be8 20 50 35 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63   P5.**.** Advanc
25be9 65 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74  e cursor P1 so t
25bea 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  hat it points to
25beb 20 74 68 65 20 6e 65 78 74 20 6b 65 79 2f 64 61   the next key/da
25bec 74 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a  ta pair in its.*
25bed 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  * table or index
25bee 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20  .  If there are 
25bef 6e 6f 20 6d 6f 72 65 20 6b 65 79 2f 76 61 6c 75  no more key/valu
25bf0 65 20 70 61 69 72 73 20 74 68 65 6e 20 66 61 6c  e pairs then fal
25bf1 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20  l through.** to 
25bf2 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e  the following in
25bf3 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20  struction.  But 
25bf4 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 64  if the cursor ad
25bf5 76 61 6e 63 65 20 77 61 73 20 73 75 63 63 65 73  vance was succes
25bf6 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d  sful,.** jump im
25bf7 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e  mediately to P2.
25bf8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75  .**.** The P1 cu
25bf9 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72  rsor must be for
25bfa 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e   a real table, n
25bfb 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c  ot a pseudo-tabl
25bfc 65 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61  e..**.** P4 is a
25bfd 6c 77 61 79 73 20 6f 66 20 74 79 70 65 20 50 34  lways of type P4
25bfe 5f 41 44 56 41 4e 43 45 2e 20 54 68 65 20 66 75  _ADVANCE. The fu
25bff 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 70  nction pointer p
25c00 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 73 71 6c 69  oints to.** sqli
25c01 74 65 33 42 74 72 65 65 4e 65 78 74 28 29 2e 0a  te3BtreeNext()..
25c02 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 70  **.** If P5 is p
25c03 6f 73 69 74 69 76 65 20 61 6e 64 20 74 68 65 20  ositive and the 
25c04 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 2c 20 74  jump is taken, t
25c05 68 65 6e 20 65 76 65 6e 74 20 63 6f 75 6e 74 65  hen event counte
25c06 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 35 2d 31  r.** number P5-1
25c07 20 69 6e 20 74 68 65 20 70 72 65 70 61 72 65 64   in the prepared
25c08 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 69 6e   statement is in
25c09 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a  cremented..**.**
25c0a 20 53 65 65 20 61 6c 73 6f 3a 20 50 72 65 76 0a   See also: Prev.
25c0b 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72  */./* Opcode: Pr
25c0c 65 76 20 50 31 20 50 32 20 2a 20 2a 20 50 35 0a  ev P1 P2 * * P5.
25c0d 2a 2a 0a 2a 2a 20 42 61 63 6b 20 75 70 20 63 75  **.** Back up cu
25c0e 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20  rsor P1 so that 
25c0f 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  it points to the
25c10 20 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 64 61   previous key/da
25c11 74 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a  ta pair in its.*
25c12 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  * table or index
25c13 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  .  If there is n
25c14 6f 20 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 76  o previous key/v
25c15 61 6c 75 65 20 70 61 69 72 73 20 74 68 65 6e 20  alue pairs then 
25c16 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20  fall through.** 
25c17 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  to the following
25c18 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42   instruction.  B
25c19 75 74 20 69 66 20 74 68 65 20 63 75 72 73 6f 72  ut if the cursor
25c1a 20 62 61 63 6b 75 70 20 77 61 73 20 73 75 63 63   backup was succ
25c1b 65 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20  essful,.** jump 
25c1c 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50  immediately to P
25c1d 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20  2..**.** The P1 
25c1e 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66  cursor must be f
25c1f 6f 72 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c  or a real table,
25c20 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61   not a pseudo-ta
25c21 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73  ble..**.** P4 is
25c22 20 61 6c 77 61 79 73 20 6f 66 20 74 79 70 65 20   always of type 
25c23 50 34 5f 41 44 56 41 4e 43 45 2e 20 54 68 65 20  P4_ADVANCE. The 
25c24 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
25c25 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 73 71   points to.** sq
25c26 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f  lite3BtreePrevio
25c27 75 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  us()..**.** If P
25c28 35 20 69 73 20 70 6f 73 69 74 69 76 65 20 61 6e  5 is positive an
25c29 64 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61  d the jump is ta
25c2a 6b 65 6e 2c 20 74 68 65 6e 20 65 76 65 6e 74 20  ken, then event 
25c2b 63 6f 75 6e 74 65 72 0a 2a 2a 20 6e 75 6d 62 65  counter.** numbe
25c2c 72 20 50 35 2d 31 20 69 6e 20 74 68 65 20 70 72  r P5-1 in the pr
25c2d 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
25c2e 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e   is incremented.
25c2f 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 6f 72 74  .*/.case OP_Sort
25c30 65 72 4e 65 78 74 3a 20 20 20 20 2f 2a 20 6a 75  erNext:    /* ju
25c31 6d 70 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  mp */.#ifdef SQL
25c32 49 54 45 5f 4f 4d 49 54 5f 4d 45 52 47 45 5f 53  ITE_OMIT_MERGE_S
25c33 4f 52 54 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64  ORT.  pOp->opcod
25c34 65 20 3d 20 4f 50 5f 4e 65 78 74 3b 0a 23 65 6e  e = OP_Next;.#en
25c35 64 69 66 0a 63 61 73 65 20 4f 50 5f 50 72 65 76  dif.case OP_Prev
25c36 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  :          /* ju
25c37 6d 70 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65  mp */.case OP_Ne
25c38 78 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  xt: {        /* 
25c39 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f  jump */.#if 0  /
25c3a 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
25c3b 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
25c3c 72 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f  r */.  VdbeCurso
25c3d 72 20 2a 70 43 3b 0a 20 20 69 6e 74 20 72 65 73  r *pC;.  int res
25c3e 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
25c3f 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
25c40 64 20 69 6e 74 6f 20 75 2e 62 72 20 2a 2f 0a 0a  d into u.br */..
25c41 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45    CHECK_FOR_INTE
25c42 52 52 55 50 54 3b 0a 20 20 61 73 73 65 72 74 28  RRUPT;.  assert(
25c43 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
25c44 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f  Op->p1<p->nCurso
25c45 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
25c46 4f 70 2d 3e 70 35 3c 3d 41 72 72 61 79 53 69 7a  Op->p5<=ArraySiz
25c47 65 28 70 2d 3e 61 43 6f 75 6e 74 65 72 29 20 29  e(p->aCounter) )
25c48 3b 0a 20 20 75 2e 62 72 2e 70 43 20 3d 20 70 2d  ;.  u.br.pC = p-
25c49 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
25c4a 0a 20 20 69 66 28 20 75 2e 62 72 2e 70 43 3d 3d  .  if( u.br.pC==
25c4b 30 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 20  0 ){.    break; 
25c4c 20 2f 2a 20 53 65 65 20 74 69 63 6b 65 74 20 23   /* See ticket #
25c4d 32 32 37 33 20 2a 2f 0a 20 20 7d 0a 20 20 61 73  2273 */.  }.  as
25c4e 73 65 72 74 28 20 75 2e 62 72 2e 70 43 2d 3e 69  sert( u.br.pC->i
25c4f 73 53 6f 72 74 65 72 3d 3d 28 70 4f 70 2d 3e 6f  sSorter==(pOp->o
25c50 70 63 6f 64 65 3d 3d 4f 50 5f 53 6f 72 74 65 72  pcode==OP_Sorter
25c51 4e 65 78 74 29 20 29 3b 0a 20 20 69 66 28 20 69  Next) );.  if( i
25c52 73 53 6f 72 74 65 72 28 75 2e 62 72 2e 70 43 29  sSorter(u.br.pC)
25c53 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
25c54 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
25c55 53 6f 72 74 65 72 4e 65 78 74 20 29 3b 0a 20 20  SorterNext );.  
25c56 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
25c57 62 65 53 6f 72 74 65 72 4e 65 78 74 28 64 62 2c  beSorterNext(db,
25c58 20 75 2e 62 72 2e 70 43 2c 20 26 75 2e 62 72 2e   u.br.pC, &u.br.
25c59 72 65 73 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  res);.  }else{. 
25c5a 20 20 20 75 2e 62 72 2e 72 65 73 20 3d 20 31 3b     u.br.res = 1;
25c5b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
25c5c 72 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f  r.pC->deferredMo
25c5d 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61  veto==0 );.    a
25c5e 73 73 65 72 74 28 20 75 2e 62 72 2e 70 43 2d 3e  ssert( u.br.pC->
25c5f 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 61  pCursor );.    a
25c60 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f  ssert( pOp->opco
25c61 64 65 21 3d 4f 50 5f 4e 65 78 74 20 7c 7c 20 70  de!=OP_Next || p
25c62 4f 70 2d 3e 70 34 2e 78 41 64 76 61 6e 63 65 3d  Op->p4.xAdvance=
25c63 3d 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78  =sqlite3BtreeNex
25c64 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  t );.    assert(
25c65 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 21 3d 4f 50   pOp->opcode!=OP
25c66 5f 50 72 65 76 20 7c 7c 20 70 4f 70 2d 3e 70 34  _Prev || pOp->p4
25c67 2e 78 41 64 76 61 6e 63 65 3d 3d 73 71 6c 69 74  .xAdvance==sqlit
25c68 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 20  e3BtreePrevious 
25c69 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 4f 70 2d  );.    rc = pOp-
25c6a 3e 70 34 2e 78 41 64 76 61 6e 63 65 28 75 2e 62  >p4.xAdvance(u.b
25c6b 72 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26  r.pC->pCursor, &
25c6c 75 2e 62 72 2e 72 65 73 29 3b 0a 20 20 7d 0a 20  u.br.res);.  }. 
25c6d 20 75 2e 62 72 2e 70 43 2d 3e 6e 75 6c 6c 52 6f   u.br.pC->nullRo
25c6e 77 20 3d 20 28 75 38 29 75 2e 62 72 2e 72 65 73  w = (u8)u.br.res
25c6f 3b 0a 20 20 75 2e 62 72 2e 70 43 2d 3e 63 61 63  ;.  u.br.pC->cac
25c70 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
25c71 5f 53 54 41 4c 45 3b 0a 20 20 69 66 28 20 75 2e  _STALE;.  if( u.
25c72 62 72 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20  br.res==0 ){.   
25c73 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
25c74 31 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  1;.    if( pOp->
25c75 70 35 20 29 20 70 2d 3e 61 43 6f 75 6e 74 65 72  p5 ) p->aCounter
25c76 5b 70 4f 70 2d 3e 70 35 2d 31 5d 2b 2b 3b 0a 23  [pOp->p5-1]++;.#
25c77 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
25c78 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65  T.    sqlite3_se
25c79 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65  arch_count++;.#e
25c7a 6e 64 69 66 0a 20 20 7d 0a 20 20 75 2e 62 72 2e  ndif.  }.  u.br.
25c7b 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
25c7c 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   = 0;.  break;.}
25c7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78  ../* Opcode: Idx
25c7e 49 6e 73 65 72 74 20 50 31 20 50 32 20 50 33 20  Insert P1 P2 P3 
25c7f 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73  * P5.**.** Regis
25c80 74 65 72 20 50 32 20 68 6f 6c 64 73 20 61 6e 20  ter P2 holds an 
25c81 53 51 4c 20 69 6e 64 65 78 20 6b 65 79 20 6d 61  SQL index key ma
25c82 64 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  de using the.** 
25c83 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72  MakeRecord instr
25c84 75 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 6f  uctions.  This o
25c85 70 63 6f 64 65 20 77 72 69 74 65 73 20 74 68 61  pcode writes tha
25c86 74 20 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20 74 68  t key.** into th
25c87 65 20 69 6e 64 65 78 20 50 31 2e 20 20 44 61 74  e index P1.  Dat
25c88 61 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20  a for the entry 
25c89 69 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50 33  is nil..**.** P3
25c8a 20 69 73 20 61 20 66 6c 61 67 20 74 68 61 74 20   is a flag that 
25c8b 70 72 6f 76 69 64 65 73 20 61 20 68 69 6e 74 20  provides a hint 
25c8c 74 6f 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61  to the b-tree la
25c8d 79 65 72 20 74 68 61 74 20 74 68 69 73 0a 2a 2a  yer that this.**
25c8e 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c   insert is likel
25c8f 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e  y to be an appen
25c90 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  d..**.** This in
25c91 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77  struction only w
25c92 6f 72 6b 73 20 66 6f 72 20 69 6e 64 69 63 65 73  orks for indices
25c93 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e  .  The equivalen
25c94 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a  t instruction.**
25c95 20 66 6f 72 20 74 61 62 6c 65 73 20 69 73 20 4f   for tables is O
25c96 50 5f 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 73  P_Insert..*/.cas
25c97 65 20 4f 50 5f 53 6f 72 74 65 72 49 6e 73 65 72  e OP_SorterInser
25c98 74 3a 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20  t:       /* in2 
25c99 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
25c9a 5f 4f 4d 49 54 5f 4d 45 52 47 45 5f 53 4f 52 54  _OMIT_MERGE_SORT
25c9b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d  .  pOp->opcode =
25c9c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 3b 0a 23   OP_IdxInsert;.#
25c9d 65 6e 64 69 66 0a 63 61 73 65 20 4f 50 5f 49 64  endif.case OP_Id
25c9e 78 49 6e 73 65 72 74 3a 20 7b 20 20 20 20 20 20  xInsert: {      
25c9f 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 20    /* in2 */.#if 
25ca0 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
25ca1 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
25ca2 20 75 2e 62 73 20 2a 2f 0a 20 20 56 64 62 65 43   u.bs */.  VdbeC
25ca3 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43  ursor *pC;.  BtC
25ca4 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
25ca5 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 63 6f 6e 73  int nKey;.  cons
25ca6 74 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 23 65  t char *zKey;.#e
25ca7 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
25ca8 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
25ca9 74 6f 20 75 2e 62 73 20 2a 2f 0a 0a 20 20 61 73  to u.bs */..  as
25caa 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
25cab 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
25cac 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 73  Cursor );.  u.bs
25cad 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
25cae 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
25caf 74 28 20 75 2e 62 73 2e 70 43 21 3d 30 20 29 3b  t( u.bs.pC!=0 );
25cb0 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 73 2e  .  assert( u.bs.
25cb1 70 43 2d 3e 69 73 53 6f 72 74 65 72 3d 3d 28 70  pC->isSorter==(p
25cb2 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53  Op->opcode==OP_S
25cb3 6f 72 74 65 72 49 6e 73 65 72 74 29 20 29 3b 0a  orterInsert) );.
25cb4 20 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d 5b 70    pIn2 = &aMem[p
25cb5 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72  Op->p2];.  asser
25cb6 74 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26  t( pIn2->flags &
25cb7 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 75   MEM_Blob );.  u
25cb8 2e 62 73 2e 70 43 72 73 72 20 3d 20 75 2e 62 73  .bs.pCrsr = u.bs
25cb9 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20  .pC->pCursor;.  
25cba 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 73 2e  if( ALWAYS(u.bs.
25cbb 70 43 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20  pCrsr!=0) ){.   
25cbc 20 61 73 73 65 72 74 28 20 75 2e 62 73 2e 70 43   assert( u.bs.pC
25cbd 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  ->isTable==0 );.
25cbe 20 20 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42      rc = ExpandB
25cbf 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20 20 20 69  lob(pIn2);.    i
25cc0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
25cc1 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73   ){.      if( is
25cc2 53 6f 72 74 65 72 28 75 2e 62 73 2e 70 43 29 20  Sorter(u.bs.pC) 
25cc3 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
25cc4 73 71 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65  sqlite3VdbeSorte
25cc5 72 57 72 69 74 65 28 64 62 2c 20 75 2e 62 73 2e  rWrite(db, u.bs.
25cc6 70 43 2c 20 70 49 6e 32 29 3b 0a 20 20 20 20 20  pC, pIn2);.     
25cc7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
25cc8 75 2e 62 73 2e 6e 4b 65 79 20 3d 20 70 49 6e 32  u.bs.nKey = pIn2
25cc9 2d 3e 6e 3b 0a 20 20 20 20 20 20 20 20 75 2e 62  ->n;.        u.b
25cca 73 2e 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 7a  s.zKey = pIn2->z
25ccb 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
25ccc 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72  qlite3BtreeInser
25ccd 74 28 75 2e 62 73 2e 70 43 72 73 72 2c 20 75 2e  t(u.bs.pCrsr, u.
25cce 62 73 2e 7a 4b 65 79 2c 20 75 2e 62 73 2e 6e 4b  bs.zKey, u.bs.nK
25ccf 65 79 2c 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f  ey, "", 0, 0, pO
25cd0 70 2d 3e 70 33 2c 0a 20 20 20 20 20 20 20 20 20  p->p3,.         
25cd1 20 20 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f     ((pOp->p5 & O
25cd2 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53  PFLAG_USESEEKRES
25cd3 55 4c 54 29 20 3f 20 75 2e 62 73 2e 70 43 2d 3e  ULT) ? u.bs.pC->
25cd4 73 65 65 6b 52 65 73 75 6c 74 20 3a 20 30 29 0a  seekResult : 0).
25cd5 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20              );. 
25cd6 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75         assert( u
25cd7 2e 62 73 2e 70 43 2d 3e 64 65 66 65 72 72 65 64  .bs.pC->deferred
25cd8 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20  Moveto==0 );.   
25cd9 20 20 20 20 20 75 2e 62 73 2e 70 43 2d 3e 63 61       u.bs.pC->ca
25cda 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
25cdb 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 20 20 7d  E_STALE;.      }
25cdc 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65  .    }.  }.  bre
25cdd 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
25cde 3a 20 49 64 78 44 65 6c 65 74 65 20 50 31 20 50  : IdxDelete P1 P
25cdf 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54  2 P3 * *.**.** T
25ce0 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 50 33  he content of P3
25ce1 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74   registers start
25ce2 69 6e 67 20 61 74 20 72 65 67 69 73 74 65 72 20  ing at register 
25ce3 50 32 20 66 6f 72 6d 0a 2a 2a 20 61 6e 20 75 6e  P2 form.** an un
25ce4 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79  packed index key
25ce5 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72 65  . This opcode re
25ce6 6d 6f 76 65 73 20 74 68 61 74 20 65 6e 74 72 79  moves that entry
25ce7 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 69 6e   from the .** in
25ce8 64 65 78 20 6f 70 65 6e 65 64 20 62 79 20 63 75  dex opened by cu
25ce9 72 73 6f 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65  rsor P1..*/.case
25cea 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 3a 20 7b   OP_IdxDelete: {
25ceb 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
25cec 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
25ced 20 69 6e 74 6f 20 75 2e 62 74 20 2a 2f 0a 20 20   into u.bt */.  
25cee 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
25cef 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
25cf0 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20  r;.  int res;.  
25cf1 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72  UnpackedRecord r
25cf2 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
25cf3 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
25cf4 64 20 69 6e 74 6f 20 75 2e 62 74 20 2a 2f 0a 0a  d into u.bt */..
25cf5 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
25cf6 33 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  3>0 );.  assert(
25cf7 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f   pOp->p2>0 && pO
25cf8 70 2d 3e 70 32 2b 70 4f 70 2d 3e 70 33 3c 3d 70  p->p2+pOp->p3<=p
25cf9 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 61 73  ->nMem+1 );.  as
25cfa 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
25cfb 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
25cfc 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 74  Cursor );.  u.bt
25cfd 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
25cfe 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
25cff 74 28 20 75 2e 62 74 2e 70 43 21 3d 30 20 29 3b  t( u.bt.pC!=0 );
25d00 0a 20 20 75 2e 62 74 2e 70 43 72 73 72 20 3d 20  .  u.bt.pCrsr = 
25d01 75 2e 62 74 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.bt.pC->pCursor
25d02 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75  ;.  if( ALWAYS(u
25d03 2e 62 74 2e 70 43 72 73 72 21 3d 30 29 20 29 7b  .bt.pCrsr!=0) ){
25d04 0a 20 20 20 20 75 2e 62 74 2e 72 2e 70 4b 65 79  .    u.bt.r.pKey
25d05 49 6e 66 6f 20 3d 20 75 2e 62 74 2e 70 43 2d 3e  Info = u.bt.pC->
25d06 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75 2e  pKeyInfo;.    u.
25d07 62 74 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75  bt.r.nField = (u
25d08 31 36 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20  16)pOp->p3;.    
25d09 75 2e 62 74 2e 72 2e 66 6c 61 67 73 20 3d 20 30  u.bt.r.flags = 0
25d0a 3b 0a 20 20 20 20 75 2e 62 74 2e 72 2e 61 4d 65  ;.    u.bt.r.aMe
25d0b 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  m = &aMem[pOp->p
25d0c 32 5d 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  2];.#ifdef SQLIT
25d0d 45 5f 44 45 42 55 47 0a 20 20 20 20 7b 20 69 6e  E_DEBUG.    { in
25d0e 74 20 69 3b 20 66 6f 72 28 69 3d 30 3b 20 69 3c  t i; for(i=0; i<
25d0f 75 2e 62 74 2e 72 2e 6e 46 69 65 6c 64 3b 20 69  u.bt.r.nField; i
25d10 2b 2b 29 20 61 73 73 65 72 74 28 20 6d 65 6d 49  ++) assert( memI
25d11 73 56 61 6c 69 64 28 26 75 2e 62 74 2e 72 2e 61  sValid(&u.bt.r.a
25d12 4d 65 6d 5b 69 5d 29 20 29 3b 20 7d 0a 23 65 6e  Mem[i]) ); }.#en
25d13 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  dif.    rc = sql
25d14 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55  ite3BtreeMovetoU
25d15 6e 70 61 63 6b 65 64 28 75 2e 62 74 2e 70 43 72  npacked(u.bt.pCr
25d16 73 72 2c 20 26 75 2e 62 74 2e 72 2c 20 30 2c 20  sr, &u.bt.r, 0, 
25d17 30 2c 20 26 75 2e 62 74 2e 72 65 73 29 3b 0a 20  0, &u.bt.res);. 
25d18 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
25d19 45 5f 4f 4b 20 26 26 20 75 2e 62 74 2e 72 65 73  E_OK && u.bt.res
25d1a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ==0 ){.      rc 
25d1b 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65  = sqlite3BtreeDe
25d1c 6c 65 74 65 28 75 2e 62 74 2e 70 43 72 73 72 29  lete(u.bt.pCrsr)
25d1d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
25d1e 72 74 28 20 75 2e 62 74 2e 70 43 2d 3e 64 65 66  rt( u.bt.pC->def
25d1f 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29  erredMoveto==0 )
25d20 3b 0a 20 20 20 20 75 2e 62 74 2e 70 43 2d 3e 63  ;.    u.bt.pC->c
25d21 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43  acheStatus = CAC
25d22 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20  HE_STALE;.  }.  
25d23 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
25d24 6f 64 65 3a 20 49 64 78 52 6f 77 69 64 20 50 31  ode: IdxRowid P1
25d25 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
25d26 57 72 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73  Write into regis
25d27 74 65 72 20 50 32 20 61 6e 20 69 6e 74 65 67 65  ter P2 an intege
25d28 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6c  r which is the l
25d29 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ast entry in the
25d2a 20 72 65 63 6f 72 64 20 61 74 0a 2a 2a 20 74 68   record at.** th
25d2b 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64  e end of the ind
25d2c 65 78 20 6b 65 79 20 70 6f 69 6e 74 65 64 20 74  ex key pointed t
25d2d 6f 20 62 79 20 63 75 72 73 6f 72 20 50 31 2e 20  o by cursor P1. 
25d2e 20 54 68 69 73 20 69 6e 74 65 67 65 72 20 73 68   This integer sh
25d2f 6f 75 6c 64 20 62 65 0a 2a 2a 20 74 68 65 20 72  ould be.** the r
25d30 6f 77 69 64 20 6f 66 20 74 68 65 20 74 61 62 6c  owid of the tabl
25d31 65 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68  e entry to which
25d32 20 74 68 69 73 20 69 6e 64 65 78 20 65 6e 74 72   this index entr
25d33 79 20 70 6f 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  y points..**.** 
25d34 53 65 65 20 61 6c 73 6f 3a 20 52 6f 77 69 64 2c  See also: Rowid,
25d35 20 4d 61 6b 65 52 65 63 6f 72 64 2e 0a 2a 2f 0a   MakeRecord..*/.
25d36 63 61 73 65 20 4f 50 5f 49 64 78 52 6f 77 69 64  case OP_IdxRowid
25d37 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
25d38 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
25d39 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  ase */.#if 0  /*
25d3a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
25d3b 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 75   moved into u.bu
25d3c 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a   */.  BtCursor *
25d3d 70 43 72 73 72 3b 0a 20 20 56 64 62 65 43 75 72  pCrsr;.  VdbeCur
25d3e 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 34 20 72  sor *pC;.  i64 r
25d3f 6f 77 69 64 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  owid;.#endif /* 
25d40 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
25d41 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 75 20  moved into u.bu 
25d42 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f  */..  assert( pO
25d43 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
25d44 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  >p1<p->nCursor )
25d45 3b 0a 20 20 75 2e 62 75 2e 70 43 20 3d 20 70 2d  ;.  u.bu.pC = p-
25d46 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
25d47 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 75 2e  .  assert( u.bu.
25d48 70 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62 75 2e  pC!=0 );.  u.bu.
25d49 70 43 72 73 72 20 3d 20 75 2e 62 75 2e 70 43 2d  pCrsr = u.bu.pC-
25d4a 3e 70 43 75 72 73 6f 72 3b 0a 20 20 70 4f 75 74  >pCursor;.  pOut
25d4b 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75  ->flags = MEM_Nu
25d4c 6c 6c 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53  ll;.  if( ALWAYS
25d4d 28 75 2e 62 75 2e 70 43 72 73 72 21 3d 30 29 20  (u.bu.pCrsr!=0) 
25d4e 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
25d4f 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76  te3VdbeCursorMov
25d50 65 74 6f 28 75 2e 62 75 2e 70 43 29 3b 0a 20 20  eto(u.bu.pC);.  
25d51 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 29 20    if( NEVER(rc) 
25d52 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
25d53 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 61  _to_error;.    a
25d54 73 73 65 72 74 28 20 75 2e 62 75 2e 70 43 2d 3e  ssert( u.bu.pC->
25d55 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d  deferredMoveto==
25d56 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
25d57 20 75 2e 62 75 2e 70 43 2d 3e 69 73 54 61 62 6c   u.bu.pC->isTabl
25d58 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  e==0 );.    if( 
25d59 21 75 2e 62 75 2e 70 43 2d 3e 6e 75 6c 6c 52 6f  !u.bu.pC->nullRo
25d5a 77 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  w ){.      rc = 
25d5b 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f  sqlite3VdbeIdxRo
25d5c 77 69 64 28 64 62 2c 20 75 2e 62 75 2e 70 43 72  wid(db, u.bu.pCr
25d5d 73 72 2c 20 26 75 2e 62 75 2e 72 6f 77 69 64 29  sr, &u.bu.rowid)
25d5e 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
25d5f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
25d60 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
25d61 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
25d62 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 75 74      }.      pOut
25d63 2d 3e 75 2e 69 20 3d 20 75 2e 62 75 2e 72 6f 77  ->u.i = u.bu.row
25d64 69 64 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e  id;.      pOut->
25d65 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
25d66 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65  .    }.  }.  bre
25d67 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
25d68 3a 20 49 64 78 47 45 20 50 31 20 50 32 20 50 33  : IdxGE P1 P2 P3
25d69 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65   P4 P5.**.** The
25d6a 20 50 34 20 72 65 67 69 73 74 65 72 20 76 61 6c   P4 register val
25d6b 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  ues beginning wi
25d6c 74 68 20 50 33 20 66 6f 72 6d 20 61 6e 20 75 6e  th P3 form an un
25d6d 70 61 63 6b 65 64 20 69 6e 64 65 78 20 0a 2a 2a  packed index .**
25d6e 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 74 73 20   key that omits 
25d6f 74 68 65 20 52 4f 57 49 44 2e 20 20 43 6f 6d 70  the ROWID.  Comp
25d70 61 72 65 20 74 68 69 73 20 6b 65 79 20 76 61 6c  are this key val
25d71 75 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 69  ue against the i
25d72 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 20 50 31  ndex .** that P1
25d73 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
25d74 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e 6f 72  inting to, ignor
25d75 69 6e 67 20 74 68 65 20 52 4f 57 49 44 20 6f 6e  ing the ROWID on
25d76 20 74 68 65 20 50 31 20 69 6e 64 65 78 2e 0a 2a   the P1 index..*
25d77 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 20 69  *.** If the P1 i
25d78 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 67 72  ndex entry is gr
25d79 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71  eater than or eq
25d7a 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 76  ual to the key v
25d7b 61 6c 75 65 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d  alue.** then jum
25d7c 70 20 74 6f 20 50 32 2e 20 20 4f 74 68 65 72 77  p to P2.  Otherw
25d7d 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  ise fall through
25d7e 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73   to the next ins
25d7f 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  truction..**.** 
25d80 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a 65 72  If P5 is non-zer
25d81 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 79 20 76  o then the key v
25d82 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65  alue is increase
25d83 64 20 62 79 20 61 6e 20 65 70 73 69 6c 6f 6e 20  d by an epsilon 
25d84 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 74 68 65  .** prior to the
25d85 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68   comparison.  Th
25d86 69 73 20 6d 61 6b 65 20 74 68 65 20 6f 70 63 6f  is make the opco
25d87 64 65 20 77 6f 72 6b 20 6c 69 6b 65 20 49 64 78  de work like Idx
25d88 47 54 20 65 78 63 65 70 74 0a 2a 2a 20 74 68 61  GT except.** tha
25d89 74 20 69 66 20 74 68 65 20 6b 65 79 20 66 72 6f  t if the key fro
25d8a 6d 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73  m register P3 is
25d8b 20 61 20 70 72 65 66 69 78 20 6f 66 20 74 68 65   a prefix of the
25d8c 20 6b 65 79 20 69 6e 20 74 68 65 20 63 75 72 73   key in the curs
25d8d 6f 72 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c  or,.** the resul
25d8e 74 20 69 73 20 66 61 6c 73 65 20 77 68 65 72 65  t is false where
25d8f 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20 74  as it would be t
25d90 72 75 65 20 77 69 74 68 20 49 64 78 47 54 2e 0a  rue with IdxGT..
25d91 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64  */./* Opcode: Id
25d92 78 4c 54 20 50 31 20 50 32 20 50 33 20 50 34 20  xLT P1 P2 P3 P4 
25d93 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20  P5.**.** The P4 
25d94 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 73 20  register values 
25d95 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50  beginning with P
25d96 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b  3 form an unpack
25d97 65 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79  ed index .** key
25d98 20 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20   that omits the 
25d99 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20  ROWID.  Compare 
25d9a 74 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61  this key value a
25d9b 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78  gainst the index
25d9c 20 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20   .** that P1 is 
25d9d 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
25d9e 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20  ng to, ignoring 
25d9f 74 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65  the ROWID on the
25da0 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a   P1 index..**.**
25da1 20 49 66 20 74 68 65 20 50 31 20 69 6e 64 65 78   If the P1 index
25da2 20 65 6e 74 72 79 20 69 73 20 6c 65 73 73 20 74   entry is less t
25da3 68 61 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75  han the key valu
25da4 65 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50  e then jump to P
25da5 32 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20  2..** Otherwise 
25da6 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20  fall through to 
25da7 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
25da8 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  tion..**.** If P
25da9 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68  5 is non-zero th
25daa 65 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65  en the key value
25dab 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 62 79   is increased by
25dac 20 61 6e 20 65 70 73 69 6c 6f 6e 20 70 72 69 6f   an epsilon prio
25dad 72 20 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f 6d  r .** to the com
25dae 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d  parison.  This m
25daf 61 6b 65 73 20 74 68 65 20 6f 70 63 6f 64 65 20  akes the opcode 
25db0 77 6f 72 6b 20 6c 69 6b 65 20 49 64 78 4c 45 2e  work like IdxLE.
25db1 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 4c  .*/.case OP_IdxL
25db2 54 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a  T:          /* j
25db3 75 6d 70 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  ump */.case OP_I
25db4 64 78 47 45 3a 20 7b 20 20 20 20 20 20 20 20 2f  dxGE: {        /
25db5 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20  * jump */.#if 0 
25db6 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
25db7 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
25db8 2e 62 76 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  .bv */.  VdbeCur
25db9 73 6f 72 20 2a 70 43 3b 0a 20 20 69 6e 74 20 72  sor *pC;.  int r
25dba 65 73 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65  es;.  UnpackedRe
25dbb 63 6f 72 64 20 72 3b 0a 23 65 6e 64 69 66 20 2f  cord r;.#endif /
25dbc 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
25dbd 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
25dbe 76 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  v */..  assert( 
25dbf 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
25dc0 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72  p->p1<p->nCursor
25dc1 20 29 3b 0a 20 20 75 2e 62 76 2e 70 43 20 3d 20   );.  u.bv.pC = 
25dc2 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
25dc3 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  ];.  assert( u.b
25dc4 76 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73  v.pC!=0 );.  ass
25dc5 65 72 74 28 20 75 2e 62 76 2e 70 43 2d 3e 69 73  ert( u.bv.pC->is
25dc6 4f 72 64 65 72 65 64 20 29 3b 0a 20 20 69 66 28  Ordered );.  if(
25dc7 20 41 4c 57 41 59 53 28 75 2e 62 76 2e 70 43 2d   ALWAYS(u.bv.pC-
25dc8 3e 70 43 75 72 73 6f 72 21 3d 30 29 20 29 7b 0a  >pCursor!=0) ){.
25dc9 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 76      assert( u.bv
25dca 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  .pC->deferredMov
25dcb 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73  eto==0 );.    as
25dcc 73 65 72 74 28 20 70 4f 70 2d 3e 70 35 3d 3d 30  sert( pOp->p5==0
25dcd 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d 3d 31 20 29   || pOp->p5==1 )
25dce 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  ;.    assert( pO
25dcf 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e  p->p4type==P4_IN
25dd0 54 33 32 20 29 3b 0a 20 20 20 20 75 2e 62 76 2e  T32 );.    u.bv.
25dd1 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 62  r.pKeyInfo = u.b
25dd2 76 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a  v.pC->pKeyInfo;.
25dd3 20 20 20 20 75 2e 62 76 2e 72 2e 6e 46 69 65 6c      u.bv.r.nFiel
25dd4 64 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 34  d = (u16)pOp->p4
25dd5 2e 69 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d  .i;.    if( pOp-
25dd6 3e 70 35 20 29 7b 0a 20 20 20 20 20 20 75 2e 62  >p5 ){.      u.b
25dd7 76 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41  v.r.flags = UNPA
25dd8 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 7c 20 55  CKED_INCRKEY | U
25dd9 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d  NPACKED_PREFIX_M
25dda 41 54 43 48 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ATCH;.    }else{
25ddb 0a 20 20 20 20 20 20 75 2e 62 76 2e 72 2e 66 6c  .      u.bv.r.fl
25ddc 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 50  ags = UNPACKED_P
25ddd 52 45 46 49 58 5f 4d 41 54 43 48 3b 0a 20 20 20  REFIX_MATCH;.   
25dde 20 7d 0a 20 20 20 20 75 2e 62 76 2e 72 2e 61 4d   }.    u.bv.r.aM
25ddf 65 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  em = &aMem[pOp->
25de0 70 33 5d 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  p3];.#ifdef SQLI
25de1 54 45 5f 44 45 42 55 47 0a 20 20 20 20 7b 20 69  TE_DEBUG.    { i
25de2 6e 74 20 69 3b 20 66 6f 72 28 69 3d 30 3b 20 69  nt i; for(i=0; i
25de3 3c 75 2e 62 76 2e 72 2e 6e 46 69 65 6c 64 3b 20  <u.bv.r.nField; 
25de4 69 2b 2b 29 20 61 73 73 65 72 74 28 20 6d 65 6d  i++) assert( mem
25de5 49 73 56 61 6c 69 64 28 26 75 2e 62 76 2e 72 2e  IsValid(&u.bv.r.
25de6 61 4d 65 6d 5b 69 5d 29 20 29 3b 20 7d 0a 23 65  aMem[i]) ); }.#e
25de7 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71  ndif.    rc = sq
25de8 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 43  lite3VdbeIdxKeyC
25de9 6f 6d 70 61 72 65 28 75 2e 62 76 2e 70 43 2c 20  ompare(u.bv.pC, 
25dea 26 75 2e 62 76 2e 72 2c 20 26 75 2e 62 76 2e 72  &u.bv.r, &u.bv.r
25deb 65 73 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70  es);.    if( pOp
25dec 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78  ->opcode==OP_Idx
25ded 4c 54 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 76  LT ){.      u.bv
25dee 2e 72 65 73 20 3d 20 2d 75 2e 62 76 2e 72 65 73  .res = -u.bv.res
25def 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
25df0 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
25df1 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 47 45  opcode==OP_IdxGE
25df2 20 29 3b 0a 20 20 20 20 20 20 75 2e 62 76 2e 72   );.      u.bv.r
25df3 65 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  es++;.    }.    
25df4 69 66 28 20 75 2e 62 76 2e 72 65 73 3e 30 20 29  if( u.bv.res>0 )
25df5 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70  {.      pc = pOp
25df6 2d 3e 70 32 20 2d 20 31 20 3b 0a 20 20 20 20 7d  ->p2 - 1 ;.    }
25df7 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
25df8 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 73 74  ./* Opcode: Dest
25df9 72 6f 79 20 50 31 20 50 32 20 50 33 20 2a 20 2a  roy P1 P2 P3 * *
25dfa 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  .**.** Delete an
25dfb 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65   entire database
25dfc 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
25dfd 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20  whose root page 
25dfe 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  in the database.
25dff 2a 2a 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e  ** file is given
25e00 20 62 79 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68   by P1..**.** Th
25e01 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 65  e table being de
25e02 73 74 72 6f 79 65 64 20 69 73 20 69 6e 20 74 68  stroyed is in th
25e03 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
25e04 66 69 6c 65 20 69 66 20 50 33 3d 3d 30 2e 20 20  file if P3==0.  
25e05 49 66 0a 2a 2a 20 50 33 3d 3d 31 20 74 68 65 6e  If.** P3==1 then
25e06 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   the table to be
25e07 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65   clear is in the
25e08 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62   auxiliary datab
25e09 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 74  ase file.** that
25e0a 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72   is used to stor
25e0b 65 20 74 61 62 6c 65 73 20 63 72 65 61 74 65 20  e tables create 
25e0c 75 73 69 6e 67 20 43 52 45 41 54 45 20 54 45 4d  using CREATE TEM
25e0d 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a 2a  PORARY TABLE..**
25e0e 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55  .** If AUTOVACUU
25e0f 4d 20 69 73 20 65 6e 61 62 6c 65 64 20 74 68 65  M is enabled the
25e10 6e 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65  n it is possible
25e11 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 72 6f   that another ro
25e12 6f 74 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68 74  ot page.** might
25e13 20 62 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74   be moved into t
25e14 68 65 20 6e 65 77 6c 79 20 64 65 6c 65 74 65 64  he newly deleted
25e15 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 6f 72   root page in or
25e16 64 65 72 20 74 6f 20 6b 65 65 70 20 61 6c 6c 0a  der to keep all.
25e17 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 63 6f  ** root pages co
25e18 6e 74 69 67 75 6f 75 73 20 61 74 20 74 68 65 20  ntiguous at the 
25e19 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
25e1a 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20   database.  The 
25e1b 66 6f 72 6d 65 72 0a 2a 2a 20 76 61 6c 75 65 20  former.** value 
25e1c 6f 66 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  of the root page
25e1d 20 74 68 61 74 20 6d 6f 76 65 64 20 2d 20 69 74   that moved - it
25e1e 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 74  s value before t
25e1f 68 65 20 6d 6f 76 65 20 6f 63 63 75 72 72 65 64  he move occurred
25e20 20 2d 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20   -.** is stored 
25e21 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 20  in register P2. 
25e22 20 49 66 20 6e 6f 20 70 61 67 65 20 0a 2a 2a 20   If no page .** 
25e23 6d 6f 76 65 6d 65 6e 74 20 77 61 73 20 72 65 71  movement was req
25e24 75 69 72 65 64 20 28 62 65 63 61 75 73 65 20 74  uired (because t
25e25 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64  he table being d
25e26 72 6f 70 70 65 64 20 77 61 73 20 61 6c 72 65 61  ropped was alrea
25e27 64 79 20 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20  dy .** the last 
25e28 6f 6e 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  one in the datab
25e29 61 73 65 29 20 74 68 65 6e 20 61 20 7a 65 72 6f  ase) then a zero
25e2a 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65   is stored in re
25e2b 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 49 66  gister P2..** If
25e2c 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 64   AUTOVACUUM is d
25e2d 69 73 61 62 6c 65 64 20 74 68 65 6e 20 61 20 7a  isabled then a z
25e2e 65 72 6f 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ero is stored in
25e2f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a   register P2..**
25e30 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 6c  .** See also: Cl
25e31 65 61 72 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44  ear.*/.case OP_D
25e32 65 73 74 72 6f 79 3a 20 7b 20 20 20 20 20 2f 2a  estroy: {     /*
25e33 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
25e34 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
25e35 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
25e36 76 65 64 20 69 6e 74 6f 20 75 2e 62 77 20 2a 2f  ved into u.bw */
25e37 0a 20 20 69 6e 74 20 69 4d 6f 76 65 64 3b 0a 20  .  int iMoved;. 
25e38 20 69 6e 74 20 69 43 6e 74 3b 0a 20 20 56 64 62   int iCnt;.  Vdb
25e39 65 20 2a 70 56 64 62 65 3b 0a 20 20 69 6e 74 20  e *pVdbe;.  int 
25e3a 69 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  iDb;.#endif /* l
25e3b 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
25e3c 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 77 20 2a  oved into u.bw *
25e3d 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
25e3e 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
25e3f 42 4c 45 0a 20 20 75 2e 62 77 2e 69 43 6e 74 20  BLE.  u.bw.iCnt 
25e40 3d 20 30 3b 0a 20 20 66 6f 72 28 75 2e 62 77 2e  = 0;.  for(u.bw.
25e41 70 56 64 62 65 3d 64 62 2d 3e 70 56 64 62 65 3b  pVdbe=db->pVdbe;
25e42 20 75 2e 62 77 2e 70 56 64 62 65 3b 20 75 2e 62   u.bw.pVdbe; u.b
25e43 77 2e 70 56 64 62 65 20 3d 20 75 2e 62 77 2e 70  w.pVdbe = u.bw.p
25e44 56 64 62 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  Vdbe->pNext){.  
25e45 20 20 69 66 28 20 75 2e 62 77 2e 70 56 64 62 65    if( u.bw.pVdbe
25e46 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
25e47 47 49 43 5f 52 55 4e 20 26 26 20 75 2e 62 77 2e  GIC_RUN && u.bw.
25e48 70 56 64 62 65 2d 3e 69 6e 56 74 61 62 4d 65 74  pVdbe->inVtabMet
25e49 68 6f 64 3c 32 20 26 26 20 75 2e 62 77 2e 70 56  hod<2 && u.bw.pV
25e4a 64 62 65 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20  dbe->pc>=0 ){.  
25e4b 20 20 20 20 75 2e 62 77 2e 69 43 6e 74 2b 2b 3b      u.bw.iCnt++;
25e4c 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65  .    }.  }.#else
25e4d 0a 20 20 75 2e 62 77 2e 69 43 6e 74 20 3d 20 64  .  u.bw.iCnt = d
25e4e 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
25e4f 3b 0a 23 65 6e 64 69 66 0a 20 20 70 4f 75 74 2d  ;.#endif.  pOut-
25e50 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  >flags = MEM_Nul
25e51 6c 3b 0a 20 20 69 66 28 20 75 2e 62 77 2e 69 43  l;.  if( u.bw.iC
25e52 6e 74 3e 31 20 29 7b 0a 20 20 20 20 72 63 20 3d  nt>1 ){.    rc =
25e53 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a   SQLITE_LOCKED;.
25e54 20 20 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69      p->errorActi
25e55 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20  on = OE_Abort;. 
25e56 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 62 77   }else{.    u.bw
25e57 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a  .iDb = pOp->p3;.
25e58 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 77      assert( u.bw
25e59 2e 69 43 6e 74 3d 3d 31 20 29 3b 0a 20 20 20 20  .iCnt==1 );.    
25e5a 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65  assert( (p->btre
25e5b 65 4d 61 73 6b 20 26 20 28 28 28 79 44 62 4d 61  eMask & (((yDbMa
25e5c 73 6b 29 31 29 3c 3c 75 2e 62 77 2e 69 44 62 29  sk)1)<<u.bw.iDb)
25e5d 29 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d  )!=0 );.    rc =
25e5e 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f   sqlite3BtreeDro
25e5f 70 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b 75  pTable(db->aDb[u
25e60 2e 62 77 2e 69 44 62 5d 2e 70 42 74 2c 20 70 4f  .bw.iDb].pBt, pO
25e61 70 2d 3e 70 31 2c 20 26 75 2e 62 77 2e 69 4d 6f  p->p1, &u.bw.iMo
25e62 76 65 64 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e  ved);.    pOut->
25e63 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
25e64 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d  .    pOut->u.i =
25e65 20 75 2e 62 77 2e 69 4d 6f 76 65 64 3b 0a 23 69   u.bw.iMoved;.#i
25e66 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
25e67 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
25e68 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
25e69 4f 4b 20 26 26 20 75 2e 62 77 2e 69 4d 6f 76 65  OK && u.bw.iMove
25e6a 64 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  d!=0 ){.      sq
25e6b 6c 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76  lite3RootPageMov
25e6c 65 64 28 64 62 2c 20 75 2e 62 77 2e 69 44 62 2c  ed(db, u.bw.iDb,
25e6d 20 75 2e 62 77 2e 69 4d 6f 76 65 64 2c 20 70 4f   u.bw.iMoved, pO
25e6e 70 2d 3e 70 31 29 3b 0a 20 20 20 20 20 20 2f 2a  p->p1);.      /*
25e6f 20 41 6c 6c 20 4f 50 5f 44 65 73 74 72 6f 79 20   All OP_Destroy 
25e70 6f 70 65 72 61 74 69 6f 6e 73 20 6f 63 63 75 72  operations occur
25e71 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 62 74 72   on the same btr
25e72 65 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  ee */.      asse
25e73 72 74 28 20 72 65 73 65 74 53 63 68 65 6d 61 4f  rt( resetSchemaO
25e74 6e 46 61 75 6c 74 3d 3d 30 20 7c 7c 20 72 65 73  nFault==0 || res
25e75 65 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74 3d  etSchemaOnFault=
25e76 3d 75 2e 62 77 2e 69 44 62 2b 31 20 29 3b 0a 20  =u.bw.iDb+1 );. 
25e77 20 20 20 20 20 72 65 73 65 74 53 63 68 65 6d 61       resetSchema
25e78 4f 6e 46 61 75 6c 74 20 3d 20 75 2e 62 77 2e 69  OnFault = u.bw.i
25e79 44 62 2b 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  Db+1;.    }.#end
25e7a 69 66 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  if.  }.  break;.
25e7b 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c  }../* Opcode: Cl
25e7c 65 61 72 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a  ear P1 P2 P3.**.
25e7d 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 63 6f  ** Delete all co
25e7e 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61  ntents of the da
25e7f 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20  tabase table or 
25e80 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74  index whose root
25e81 20 70 61 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20   page.** in the 
25e82 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
25e83 20 67 69 76 65 6e 20 62 79 20 50 31 2e 20 20 42   given by P1.  B
25e84 75 74 2c 20 75 6e 6c 69 6b 65 20 44 65 73 74 72  ut, unlike Destr
25e85 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72 65  oy, do not.** re
25e86 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65 20 6f  move the table o
25e87 72 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 65  r index from the
25e88 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
25e89 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20  **.** The table 
25e8a 62 65 69 6e 67 20 63 6c 65 61 72 20 69 73 20 69  being clear is i
25e8b 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
25e8c 61 73 65 20 66 69 6c 65 20 69 66 20 50 32 3d 3d  ase file if P2==
25e8d 30 2e 20 20 49 66 0a 2a 2a 20 50 32 3d 3d 31 20  0.  If.** P2==1 
25e8e 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 74  then the table t
25e8f 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 69 6e  o be clear is in
25e90 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64   the auxiliary d
25e91 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20  atabase file.** 
25e92 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f 20  that is used to 
25e93 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 72 65  store tables cre
25e94 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 54 45  ate using CREATE
25e95 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 4c 45   TEMPORARY TABLE
25e96 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50  ..**.** If the P
25e97 33 20 76 61 6c 75 65 20 69 73 20 6e 6f 6e 2d 7a  3 value is non-z
25e98 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 74 61  ero, then the ta
25e99 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f 20  ble referred to 
25e9a 6d 75 73 74 20 62 65 20 61 6e 0a 2a 2a 20 69 6e  must be an.** in
25e9b 74 6b 65 79 20 74 61 62 6c 65 20 28 61 6e 20 53  tkey table (an S
25e9c 51 4c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 6e  QL table, not an
25e9d 20 69 6e 64 65 78 29 2e 20 49 6e 20 74 68 69 73   index). In this
25e9e 20 63 61 73 65 20 74 68 65 20 72 6f 77 20 63 68   case the row ch
25e9f 61 6e 67 65 20 0a 2a 2a 20 63 6f 75 6e 74 20 69  ange .** count i
25ea0 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79  s incremented by
25ea1 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
25ea2 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  ows in the table
25ea3 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 20   being cleared. 
25ea4 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 67 72 65  .** If P3 is gre
25ea5 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20  ater than zero, 
25ea6 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 73  then the value s
25ea7 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
25ea8 72 20 50 33 20 69 73 0a 2a 2a 20 61 6c 73 6f 20  r P3 is.** also 
25ea9 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74  incremented by t
25eaa 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
25eab 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62  s in the table b
25eac 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 0a 2a 2a  eing cleared..**
25ead 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44 65  .** See also: De
25eae 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65 20 4f 50  stroy.*/.case OP
25eaf 5f 43 6c 65 61 72 3a 20 7b 0a 23 69 66 20 30 20  _Clear: {.#if 0 
25eb0 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
25eb1 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
25eb2 2e 62 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68  .bx */.  int nCh
25eb3 61 6e 67 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  ange;.#endif /* 
25eb4 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
25eb5 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 78 20  moved into u.bx 
25eb6 2a 2f 0a 0a 20 20 75 2e 62 78 2e 6e 43 68 61 6e  */..  u.bx.nChan
25eb7 67 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  ge = 0;.  assert
25eb8 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
25eb9 26 20 28 28 28 79 44 62 4d 61 73 6b 29 31 29 3c  & (((yDbMask)1)<
25eba 3c 70 4f 70 2d 3e 70 32 29 29 21 3d 30 20 29 3b  <pOp->p2))!=0 );
25ebb 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
25ebc 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 0a  treeClearTable(.
25ebd 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 70 4f        db->aDb[pO
25ebe 70 2d 3e 70 32 5d 2e 70 42 74 2c 20 70 4f 70 2d  p->p2].pBt, pOp-
25ebf 3e 70 31 2c 20 28 70 4f 70 2d 3e 70 33 20 3f 20  >p1, (pOp->p3 ? 
25ec0 26 75 2e 62 78 2e 6e 43 68 61 6e 67 65 20 3a 20  &u.bx.nChange : 
25ec1 30 29 0a 20 20 29 3b 0a 20 20 69 66 28 20 70 4f  0).  );.  if( pO
25ec2 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 70 2d 3e  p->p3 ){.    p->
25ec3 6e 43 68 61 6e 67 65 20 2b 3d 20 75 2e 62 78 2e  nChange += u.bx.
25ec4 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20 69 66 28  nChange;.    if(
25ec5 20 70 4f 70 2d 3e 70 33 3e 30 20 29 7b 0a 20 20   pOp->p3>0 ){.  
25ec6 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 49      assert( memI
25ec7 73 56 61 6c 69 64 28 26 61 4d 65 6d 5b 70 4f 70  sValid(&aMem[pOp
25ec8 2d 3e 70 33 5d 29 20 29 3b 0a 20 20 20 20 20 20  ->p3]) );.      
25ec9 6d 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65  memAboutToChange
25eca 28 70 2c 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  (p, &aMem[pOp->p
25ecb 33 5d 29 3b 0a 20 20 20 20 20 20 61 4d 65 6d 5b  3]);.      aMem[
25ecc 70 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 2b 3d 20  pOp->p3].u.i += 
25ecd 75 2e 62 78 2e 6e 43 68 61 6e 67 65 3b 0a 20 20  u.bx.nChange;.  
25ece 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b    }.  }.  break;
25ecf 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  .}../* Opcode: C
25ed0 72 65 61 74 65 54 61 62 6c 65 20 50 31 20 50 32  reateTable P1 P2
25ed1 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c   * * *.**.** All
25ed2 6f 63 61 74 65 20 61 20 6e 65 77 20 74 61 62 6c  ocate a new tabl
25ed3 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  e in the main da
25ed4 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50  tabase file if P
25ed5 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a 2a  1==0 or in the.*
25ed6 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  * auxiliary data
25ed7 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d  base file if P1=
25ed8 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74 61  =1 or in an atta
25ed9 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69 66  ched database if
25eda 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74 65  .** P1>1.  Write
25edb 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e   the root page n
25edc 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 77  umber of the new
25edd 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20 72   table into.** r
25ede 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a 2a 2a  egister P2.**.**
25edf 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20   The difference 
25ee0 62 65 74 77 65 65 6e 20 61 20 74 61 62 6c 65 20  between a table 
25ee1 61 6e 64 20 61 6e 20 69 6e 64 65 78 20 69 73 20  and an index is 
25ee2 74 68 69 73 3a 20 20 41 20 74 61 62 6c 65 20 6d  this:  A table m
25ee3 75 73 74 0a 2a 2a 20 68 61 76 65 20 61 20 34 2d  ust.** have a 4-
25ee4 62 79 74 65 20 69 6e 74 65 67 65 72 20 6b 65 79  byte integer key
25ee5 20 61 6e 64 20 63 61 6e 20 68 61 76 65 20 61 72   and can have ar
25ee6 62 69 74 72 61 72 79 20 64 61 74 61 2e 20 20 41  bitrary data.  A
25ee7 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 61 73 20 61  n index.** has a
25ee8 6e 20 61 72 62 69 74 72 61 72 79 20 6b 65 79 20  n arbitrary key 
25ee9 62 75 74 20 6e 6f 20 64 61 74 61 2e 0a 2a 2a 0a  but no data..**.
25eea 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 72 65  ** See also: Cre
25eeb 61 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 4f  ateIndex.*/./* O
25eec 70 63 6f 64 65 3a 20 43 72 65 61 74 65 49 6e 64  pcode: CreateInd
25eed 65 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ex P1 P2 * * *.*
25eee 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20  *.** Allocate a 
25eef 6e 65 77 20 69 6e 64 65 78 20 69 6e 20 74 68 65  new index in the
25ef0 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
25ef1 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72 20  ile if P1==0 or 
25ef2 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c 69  in the.** auxili
25ef3 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c  ary database fil
25ef4 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69 6e  e if P1==1 or in
25ef5 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 74   an attached dat
25ef6 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e 31  abase if.** P1>1
25ef7 2e 20 20 57 72 69 74 65 20 74 68 65 20 72 6f 6f  .  Write the roo
25ef8 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  t page number of
25ef9 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69   the new table i
25efa 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  nto.** register 
25efb 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 64 6f  P2..**.** See do
25efc 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 4f  cumentation on O
25efd 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 66 6f  P_CreateTable fo
25efe 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
25eff 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  ormation..*/.cas
25f00 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78  e OP_CreateIndex
25f01 3a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  :            /* 
25f02 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
25f03 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72 65 61 74  */.case OP_Creat
25f04 65 54 61 62 6c 65 3a 20 7b 20 20 20 20 20 20 20  eTable: {       
25f05 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
25f06 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20  lease */.#if 0  
25f07 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
25f08 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
25f09 62 79 20 2a 2f 0a 20 20 69 6e 74 20 70 67 6e 6f  by */.  int pgno
25f0a 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20  ;.  int flags;. 
25f0b 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64 69 66   Db *pDb;.#endif
25f0c 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
25f0d 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
25f0e 2e 62 79 20 2a 2f 0a 0a 20 20 75 2e 62 79 2e 70  .by */..  u.by.p
25f0f 67 6e 6f 20 3d 20 30 3b 0a 20 20 61 73 73 65 72  gno = 0;.  asser
25f10 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
25f11 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62   pOp->p1<db->nDb
25f12 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
25f13 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 28  ->btreeMask & ((
25f14 28 79 44 62 4d 61 73 6b 29 31 29 3c 3c 70 4f 70  (yDbMask)1)<<pOp
25f15 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 75  ->p1))!=0 );.  u
25f16 2e 62 79 2e 70 44 62 20 3d 20 26 64 62 2d 3e 61  .by.pDb = &db->a
25f17 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  Db[pOp->p1];.  a
25f18 73 73 65 72 74 28 20 75 2e 62 79 2e 70 44 62 2d  ssert( u.by.pDb-
25f19 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 69 66 28  >pBt!=0 );.  if(
25f1a 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
25f1b 5f 43 72 65 61 74 65 54 61 62 6c 65 20 29 7b 0a  _CreateTable ){.
25f1c 20 20 20 20 2f 2a 20 75 2e 62 79 2e 66 6c 61 67      /* u.by.flag
25f1d 73 20 3d 20 42 54 52 45 45 5f 49 4e 54 4b 45 59  s = BTREE_INTKEY
25f1e 3b 20 2a 2f 0a 20 20 20 20 75 2e 62 79 2e 66 6c  ; */.    u.by.fl
25f1f 61 67 73 20 3d 20 42 54 52 45 45 5f 49 4e 54 4b  ags = BTREE_INTK
25f20 45 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  EY;.  }else{.   
25f21 20 75 2e 62 79 2e 66 6c 61 67 73 20 3d 20 42 54   u.by.flags = BT
25f22 52 45 45 5f 42 4c 4f 42 4b 45 59 3b 0a 20 20 7d  REE_BLOBKEY;.  }
25f23 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
25f24 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28  treeCreateTable(
25f25 75 2e 62 79 2e 70 44 62 2d 3e 70 42 74 2c 20 26  u.by.pDb->pBt, &
25f26 75 2e 62 79 2e 70 67 6e 6f 2c 20 75 2e 62 79 2e  u.by.pgno, u.by.
25f27 66 6c 61 67 73 29 3b 0a 20 20 70 4f 75 74 2d 3e  flags);.  pOut->
25f28 75 2e 69 20 3d 20 75 2e 62 79 2e 70 67 6e 6f 3b  u.i = u.by.pgno;
25f29 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
25f2a 4f 70 63 6f 64 65 3a 20 50 61 72 73 65 53 63 68  Opcode: ParseSch
25f2b 65 6d 61 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a  ema P1 * * P4 *.
25f2c 2a 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 64 20 70  **.** Read and p
25f2d 61 72 73 65 20 61 6c 6c 20 65 6e 74 72 69 65 73  arse all entries
25f2e 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 49 54 45   from the SQLITE
25f2f 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 6f 66  _MASTER table of
25f30 20 64 61 74 61 62 61 73 65 20 50 31 0a 2a 2a 20   database P1.** 
25f31 74 68 61 74 20 6d 61 74 63 68 20 74 68 65 20 57  that match the W
25f32 48 45 52 45 20 63 6c 61 75 73 65 20 50 34 2e 20  HERE clause P4. 
25f33 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
25f34 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 70  de invokes the p
25f35 61 72 73 65 72 20 74 6f 20 63 72 65 61 74 65 20  arser to create 
25f36 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 6d 61  a new virtual ma
25f37 63 68 69 6e 65 2c 0a 2a 2a 20 74 68 65 6e 20 72  chine,.** then r
25f38 75 6e 73 20 74 68 65 20 6e 65 77 20 76 69 72 74  uns the new virt
25f39 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 74  ual machine.  It
25f3a 20 69 73 20 74 68 75 73 20 61 20 72 65 2d 65 6e   is thus a re-en
25f3b 74 72 61 6e 74 20 6f 70 63 6f 64 65 2e 0a 2a 2f  trant opcode..*/
25f3c 0a 63 61 73 65 20 4f 50 5f 50 61 72 73 65 53 63  .case OP_ParseSc
25f3d 68 65 6d 61 3a 20 7b 0a 23 69 66 20 30 20 20 2f  hema: {.#if 0  /
25f3e 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
25f3f 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
25f40 7a 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 0a  z */.  int iDb;.
25f41 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d    const char *zM
25f42 61 73 74 65 72 3b 0a 20 20 63 68 61 72 20 2a 7a  aster;.  char *z
25f43 53 71 6c 3b 0a 20 20 49 6e 69 74 44 61 74 61 20  Sql;.  InitData 
25f44 69 6e 69 74 44 61 74 61 3b 0a 23 65 6e 64 69 66  initData;.#endif
25f45 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
25f46 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
25f47 2e 62 7a 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6e 79  .bz */..  /* Any
25f48 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
25f49 65 6e 74 20 74 68 61 74 20 69 6e 76 6f 6b 65 73  ent that invokes
25f4a 20 74 68 69 73 20 6f 70 63 6f 64 65 20 77 69 6c   this opcode wil
25f4b 6c 20 68 6f 6c 64 20 6d 75 74 65 78 65 73 0a 20  l hold mutexes. 
25f4c 20 2a 2a 20 6f 6e 20 65 76 65 72 79 20 62 74 72   ** on every btr
25f4d 65 65 2e 20 20 54 68 69 73 20 69 73 20 61 20 70  ee.  This is a p
25f4e 72 65 72 65 71 75 69 73 69 74 65 20 66 6f 72 20  rerequisite for 
25f4f 69 6e 76 6f 6b 69 6e 67 0a 20 20 2a 2a 20 73 71  invoking.  ** sq
25f50 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63  lite3InitCallbac
25f51 6b 28 29 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66  k()..  */.#ifdef
25f52 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
25f53 66 6f 72 28 75 2e 62 7a 2e 69 44 62 3d 30 3b 20  for(u.bz.iDb=0; 
25f54 75 2e 62 7a 2e 69 44 62 3c 64 62 2d 3e 6e 44 62  u.bz.iDb<db->nDb
25f55 3b 20 75 2e 62 7a 2e 69 44 62 2b 2b 29 7b 0a 20  ; u.bz.iDb++){. 
25f56 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 7a 2e     assert( u.bz.
25f57 69 44 62 3d 3d 31 20 7c 7c 20 73 71 6c 69 74 65  iDb==1 || sqlite
25f58 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
25f59 28 64 62 2d 3e 61 44 62 5b 75 2e 62 7a 2e 69 44  (db->aDb[u.bz.iD
25f5a 62 5d 2e 70 42 74 29 20 29 3b 0a 20 20 7d 0a 23  b].pBt) );.  }.#
25f5b 65 6e 64 69 66 0a 0a 20 20 75 2e 62 7a 2e 69 44  endif..  u.bz.iD
25f5c 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61  b = pOp->p1;.  a
25f5d 73 73 65 72 74 28 20 75 2e 62 7a 2e 69 44 62 3e  ssert( u.bz.iDb>
25f5e 3d 30 20 26 26 20 75 2e 62 7a 2e 69 44 62 3c 64  =0 && u.bz.iDb<d
25f5f 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65  b->nDb );.  asse
25f60 72 74 28 20 44 62 48 61 73 50 72 6f 70 65 72 74  rt( DbHasPropert
25f61 79 28 64 62 2c 20 75 2e 62 7a 2e 69 44 62 2c 20  y(db, u.bz.iDb, 
25f62 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29  DB_SchemaLoaded)
25f63 20 29 3b 0a 20 20 2f 2a 20 55 73 65 64 20 74 6f   );.  /* Used to
25f64 20 62 65 20 61 20 63 6f 6e 64 69 74 69 6f 6e 61   be a conditiona
25f65 6c 20 2a 2f 20 7b 0a 20 20 20 20 75 2e 62 7a 2e  l */ {.    u.bz.
25f66 7a 4d 61 73 74 65 72 20 3d 20 53 43 48 45 4d 41  zMaster = SCHEMA
25f67 5f 54 41 42 4c 45 28 75 2e 62 7a 2e 69 44 62 29  _TABLE(u.bz.iDb)
25f68 3b 0a 20 20 20 20 75 2e 62 7a 2e 69 6e 69 74 44  ;.    u.bz.initD
25f69 61 74 61 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20  ata.db = db;.   
25f6a 20 75 2e 62 7a 2e 69 6e 69 74 44 61 74 61 2e 69   u.bz.initData.i
25f6b 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  Db = pOp->p1;.  
25f6c 20 20 75 2e 62 7a 2e 69 6e 69 74 44 61 74 61 2e    u.bz.initData.
25f6d 70 7a 45 72 72 4d 73 67 20 3d 20 26 70 2d 3e 7a  pzErrMsg = &p->z
25f6e 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 62 7a  ErrMsg;.    u.bz
25f6f 2e 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d  .zSql = sqlite3M
25f70 50 72 69 6e 74 66 28 64 62 2c 0a 20 20 20 20 20  Printf(db,.     
25f71 20 20 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c 20    "SELECT name, 
25f72 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c 20 46 52  rootpage, sql FR
25f73 4f 4d 20 27 25 71 27 2e 25 73 20 57 48 45 52 45  OM '%q'.%s WHERE
25f74 20 25 73 20 4f 52 44 45 52 20 42 59 20 72 6f 77   %s ORDER BY row
25f75 69 64 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e  id",.       db->
25f76 61 44 62 5b 75 2e 62 7a 2e 69 44 62 5d 2e 7a 4e  aDb[u.bz.iDb].zN
25f77 61 6d 65 2c 20 75 2e 62 7a 2e 7a 4d 61 73 74 65  ame, u.bz.zMaste
25f78 72 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20  r, pOp->p4.z);. 
25f79 20 20 20 69 66 28 20 75 2e 62 7a 2e 7a 53 71 6c     if( u.bz.zSql
25f7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ==0 ){.      rc 
25f7b 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
25f7c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
25f7d 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69   assert( db->ini
25f7e 74 2e 62 75 73 79 3d 3d 30 20 29 3b 0a 20 20 20  t.busy==0 );.   
25f7f 20 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79     db->init.busy
25f80 20 3d 20 31 3b 0a 20 20 20 20 20 20 75 2e 62 7a   = 1;.      u.bz
25f81 2e 69 6e 69 74 44 61 74 61 2e 72 63 20 3d 20 53  .initData.rc = S
25f82 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
25f83 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c  assert( !db->mal
25f84 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
25f85 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
25f86 65 78 65 63 28 64 62 2c 20 75 2e 62 7a 2e 7a 53  exec(db, u.bz.zS
25f87 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69 74 43  ql, sqlite3InitC
25f88 61 6c 6c 62 61 63 6b 2c 20 26 75 2e 62 7a 2e 69  allback, &u.bz.i
25f89 6e 69 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 20  nitData, 0);.   
25f8a 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
25f8b 45 5f 4f 4b 20 29 20 72 63 20 3d 20 75 2e 62 7a  E_OK ) rc = u.bz
25f8c 2e 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20  .initData.rc;.  
25f8d 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
25f8e 65 28 64 62 2c 20 75 2e 62 7a 2e 7a 53 71 6c 29  e(db, u.bz.zSql)
25f8f 3b 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 74  ;.      db->init
25f90 2e 62 75 73 79 20 3d 20 30 3b 0a 20 20 20 20 7d  .busy = 0;.    }
25f91 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 20  .  }.  if( rc ) 
25f92 73 71 6c 69 74 65 33 52 65 73 65 74 41 6c 6c 53  sqlite3ResetAllS
25f93 63 68 65 6d 61 73 4f 66 43 6f 6e 6e 65 63 74 69  chemasOfConnecti
25f94 6f 6e 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63  on(db);.  if( rc
25f95 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
25f96 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  {.    goto no_me
25f97 6d 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  m;.  }.  break;.
25f98 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  }..#if !defined(
25f99 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c  SQLITE_OMIT_ANAL
25f9a 59 5a 45 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  YZE)./* Opcode: 
25f9b 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 50 31 20  LoadAnalysis P1 
25f9c 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65  * * * *.**.** Re
25f9d 61 64 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  ad the sqlite_st
25f9e 61 74 31 20 74 61 62 6c 65 20 66 6f 72 20 64 61  at1 table for da
25f9f 74 61 62 61 73 65 20 50 31 20 61 6e 64 20 6c 6f  tabase P1 and lo
25fa0 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a  ad the content.*
25fa1 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20  * of that table 
25fa2 69 6e 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61  into the interna
25fa3 6c 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62  l index hash tab
25fa4 6c 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63  le.  This will c
25fa5 61 75 73 65 0a 2a 2a 20 74 68 65 20 61 6e 61 6c  ause.** the anal
25fa6 79 73 69 73 20 74 6f 20 62 65 20 75 73 65 64 20  ysis to be used 
25fa7 77 68 65 6e 20 70 72 65 70 61 72 69 6e 67 20 61  when preparing a
25fa8 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 71 75  ll subsequent qu
25fa9 65 72 69 65 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f  eries..*/.case O
25faa 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 3a 20  P_LoadAnalysis: 
25fab 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  {.  assert( pOp-
25fac 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
25fad 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 72  1<db->nDb );.  r
25fae 63 20 3d 20 73 71 6c 69 74 65 33 41 6e 61 6c 79  c = sqlite3Analy
25faf 73 69 73 4c 6f 61 64 28 64 62 2c 20 70 4f 70 2d  sisLoad(db, pOp-
25fb0 3e 70 31 29 3b 0a 20 20 62 72 65 61 6b 3b 20 20  >p1);.  break;  
25fb1 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  .}.#endif /* !de
25fb2 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
25fb3 54 5f 41 4e 41 4c 59 5a 45 29 20 2a 2f 0a 0a 2f  T_ANALYZE) */../
25fb4 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 54 61  * Opcode: DropTa
25fb5 62 6c 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a  ble P1 * * P4 *.
25fb6 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65  **.** Remove the
25fb7 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65   internal (in-me
25fb8 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 63  mory) data struc
25fb9 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72  tures that descr
25fba 69 62 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65  ibe.** the table
25fbb 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74   named P4 in dat
25fbc 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20  abase P1.  This 
25fbd 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
25fbe 61 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 64 72  a table.** is dr
25fbf 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20 74  opped in order t
25fc0 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65 72  o keep the inter
25fc1 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69  nal representati
25fc2 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68  on of the.** sch
25fc3 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77  ema consistent w
25fc4 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64  ith what is on d
25fc5 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  isk..*/.case OP_
25fc6 44 72 6f 70 54 61 62 6c 65 3a 20 7b 0a 20 20 73  DropTable: {.  s
25fc7 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
25fc8 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70  eleteTable(db, p
25fc9 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e  Op->p1, pOp->p4.
25fca 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  z);.  break;.}..
25fcb 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 49  /* Opcode: DropI
25fcc 6e 64 65 78 20 50 31 20 2a 20 2a 20 50 34 20 2a  ndex P1 * * P4 *
25fcd 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68  .**.** Remove th
25fce 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d  e internal (in-m
25fcf 65 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75  emory) data stru
25fd0 63 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63  ctures that desc
25fd1 72 69 62 65 0a 2a 2a 20 74 68 65 20 69 6e 64 65  ribe.** the inde
25fd2 78 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61  x named P4 in da
25fd3 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73  tabase P1.  This
25fd4 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72   is called after
25fd5 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 69 73 20   an index.** is 
25fd6 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72  dropped in order
25fd7 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74   to keep the int
25fd8 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61  ernal representa
25fd9 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73  tion of the.** s
25fda 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74  chema consistent
25fdb 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e   with what is on
25fdc 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f   disk..*/.case O
25fdd 50 5f 44 72 6f 70 49 6e 64 65 78 3a 20 7b 0a 20  P_DropIndex: {. 
25fde 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e   sqlite3UnlinkAn
25fdf 64 44 65 6c 65 74 65 49 6e 64 65 78 28 64 62 2c  dDeleteIndex(db,
25fe0 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70   pOp->p1, pOp->p
25fe1 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  4.z);.  break;.}
25fe2 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f  ../* Opcode: Dro
25fe3 70 54 72 69 67 67 65 72 20 50 31 20 2a 20 2a 20  pTrigger P1 * * 
25fe4 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76  P4 *.**.** Remov
25fe5 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28  e the internal (
25fe6 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20  in-memory) data 
25fe7 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20  structures that 
25fe8 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20  describe.** the 
25fe9 74 72 69 67 67 65 72 20 6e 61 6d 65 64 20 50 34  trigger named P4
25fea 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e   in database P1.
25feb 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64    This is called
25fec 20 61 66 74 65 72 20 61 20 74 72 69 67 67 65 72   after a trigger
25fed 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69  .** is dropped i
25fee 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
25fef 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70  the internal rep
25ff0 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
25ff1 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e  he.** schema con
25ff2 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61  sistent with wha
25ff3 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f  t is on disk..*/
25ff4 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 72 69  .case OP_DropTri
25ff5 67 67 65 72 3a 20 7b 0a 20 20 73 71 6c 69 74 65  gger: {.  sqlite
25ff6 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
25ff7 54 72 69 67 67 65 72 28 64 62 2c 20 70 4f 70 2d  Trigger(db, pOp-
25ff8 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b  >p1, pOp->p4.z);
25ff9 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69  .  break;.}...#i
25ffa 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
25ffb 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
25ffc 4b 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74  K./* Opcode: Int
25ffd 65 67 72 69 74 79 43 6b 20 50 31 20 50 32 20 50  egrityCk P1 P2 P
25ffe 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 44 6f 20  3 * P5.**.** Do 
25fff 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 74  an analysis of t
26000 68 65 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65  he currently ope
26001 6e 20 64 61 74 61 62 61 73 65 2e 20 20 53 74 6f  n database.  Sto
26002 72 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65  re in.** registe
26003 72 20 50 31 20 74 68 65 20 74 65 78 74 20 6f 66  r P1 the text of
26004 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
26005 65 20 64 65 73 63 72 69 62 69 6e 67 20 61 6e 79  e describing any
26006 20 70 72 6f 62 6c 65 6d 73 2e 0a 2a 2a 20 49 66   problems..** If
26007 20 6e 6f 20 70 72 6f 62 6c 65 6d 73 20 61 72 65   no problems are
26008 20 66 6f 75 6e 64 2c 20 73 74 6f 72 65 20 61 20   found, store a 
26009 4e 55 4c 4c 20 69 6e 20 72 65 67 69 73 74 65 72  NULL in register
2600a 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72   P1..**.** The r
2600b 65 67 69 73 74 65 72 20 50 33 20 63 6f 6e 74 61  egister P3 conta
2600c 69 6e 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  ins the maximum 
2600d 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 77 65  number of allowe
2600e 64 20 65 72 72 6f 72 73 2e 0a 2a 2a 20 41 74 20  d errors..** At 
2600f 6d 6f 73 74 20 72 65 67 28 50 33 29 20 65 72 72  most reg(P3) err
26010 6f 72 73 20 77 69 6c 6c 20 62 65 20 72 65 70 6f  ors will be repo
26011 72 74 65 64 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65  rted..** In othe
26012 72 20 77 6f 72 64 73 2c 20 74 68 65 20 61 6e 61  r words, the ana
26013 6c 79 73 69 73 20 73 74 6f 70 73 20 61 73 20 73  lysis stops as s
26014 6f 6f 6e 20 61 73 20 72 65 67 28 50 31 29 20 65  oon as reg(P1) e
26015 72 72 6f 72 73 20 61 72 65 20 0a 2a 2a 20 73 65  rrors are .** se
26016 65 6e 2e 20 20 52 65 67 28 50 31 29 20 69 73 20  en.  Reg(P1) is 
26017 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65  updated with the
26018 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   number of error
26019 73 20 72 65 6d 61 69 6e 69 6e 67 2e 0a 2a 2a 0a  s remaining..**.
2601a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65  ** The root page
2601b 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 6c 6c 20   numbers of all 
2601c 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64 61  tables in the da
2601d 74 61 62 61 73 65 20 61 72 65 20 69 6e 74 65 67  tabase are integ
2601e 65 72 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20  er.** stored in 
2601f 72 65 67 28 50 31 29 2c 20 72 65 67 28 50 31 2b  reg(P1), reg(P1+
26020 31 29 2c 20 72 65 67 28 50 31 2b 32 29 2c 20 2e  1), reg(P1+2), .
26021 2e 2e 2e 20 20 54 68 65 72 65 20 61 72 65 20 50  ...  There are P
26022 32 20 74 61 62 6c 65 73 0a 2a 2a 20 74 6f 74 61  2 tables.** tota
26023 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69  l..**.** If P5 i
26024 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 20  s not zero, the 
26025 63 68 65 63 6b 20 69 73 20 64 6f 6e 65 20 6f 6e  check is done on
26026 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64   the auxiliary d
26027 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 2c  atabase.** file,
26028 20 6e 6f 74 20 74 68 65 20 6d 61 69 6e 20 64 61   not the main da
26029 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
2602a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69  ** This opcode i
2602b 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  s used to implem
2602c 65 6e 74 20 74 68 65 20 69 6e 74 65 67 72 69 74  ent the integrit
2602d 79 5f 63 68 65 63 6b 20 70 72 61 67 6d 61 2e 0a  y_check pragma..
2602e 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65 67  */.case OP_Integ
2602f 72 69 74 79 43 6b 3a 20 7b 0a 23 69 66 20 30 20  rityCk: {.#if 0 
26030 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
26031 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
26032 2e 63 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f  .ca */.  int nRo
26033 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ot;      /* Numb
26034 65 72 20 6f 66 20 74 61 62 6c 65 73 20 74 6f 20  er of tables to 
26035 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 65 72 20  check.  (Number 
26036 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 2e 29 20  of root pages.) 
26037 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 3b  */.  int *aRoot;
26038 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
26039 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65 72   rootpage number
2603a 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74 6f 20  s for tables to 
2603b 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20  be checked */.  
2603c 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20  int j;          
2603d 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
2603e 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20  */.  int nErr;  
2603f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
26040 66 20 65 72 72 6f 72 73 20 72 65 70 6f 72 74 65  f errors reporte
26041 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20  d */.  char *z; 
26042 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f         /* Text o
26043 66 20 74 68 65 20 65 72 72 6f 72 20 72 65 70 6f  f the error repo
26044 72 74 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 6e 45  rt */.  Mem *pnE
26045 72 72 3b 20 20 20 20 20 2f 2a 20 52 65 67 69 73  rr;     /* Regis
26046 74 65 72 20 6b 65 65 70 69 6e 67 20 74 72 61 63  ter keeping trac
26047 6b 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61  k of errors rema
26048 69 6e 69 6e 67 20 2a 2f 0a 23 65 6e 64 69 66 20  ining */.#endif 
26049 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
2604a 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
2604b 63 61 20 2a 2f 0a 0a 20 20 75 2e 63 61 2e 6e 52  ca */..  u.ca.nR
2604c 6f 6f 74 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  oot = pOp->p2;. 
2604d 20 61 73 73 65 72 74 28 20 75 2e 63 61 2e 6e 52   assert( u.ca.nR
2604e 6f 6f 74 3e 30 20 29 3b 0a 20 20 75 2e 63 61 2e  oot>0 );.  u.ca.
2604f 61 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65 33 44  aRoot = sqlite3D
26050 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
26051 69 7a 65 6f 66 28 69 6e 74 29 2a 28 75 2e 63 61  izeof(int)*(u.ca
26052 2e 6e 52 6f 6f 74 2b 31 29 20 29 3b 0a 20 20 69  .nRoot+1) );.  i
26053 66 28 20 75 2e 63 61 2e 61 52 6f 6f 74 3d 3d 30  f( u.ca.aRoot==0
26054 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
26055 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
26056 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d  3>0 && pOp->p3<=
26057 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 63  p->nMem );.  u.c
26058 61 2e 70 6e 45 72 72 20 3d 20 26 61 4d 65 6d 5b  a.pnErr = &aMem[
26059 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65  pOp->p3];.  asse
2605a 72 74 28 20 28 75 2e 63 61 2e 70 6e 45 72 72 2d  rt( (u.ca.pnErr-
2605b 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
2605c 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  )!=0 );.  assert
2605d 28 20 28 75 2e 63 61 2e 70 6e 45 72 72 2d 3e 66  ( (u.ca.pnErr->f
2605e 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c  lags & (MEM_Str|
2605f 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 29 3b  MEM_Blob))==0 );
26060 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b  .  pIn1 = &aMem[
26061 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28  pOp->p1];.  for(
26062 75 2e 63 61 2e 6a 3d 30 3b 20 75 2e 63 61 2e 6a  u.ca.j=0; u.ca.j
26063 3c 75 2e 63 61 2e 6e 52 6f 6f 74 3b 20 75 2e 63  <u.ca.nRoot; u.c
26064 61 2e 6a 2b 2b 29 7b 0a 20 20 20 20 75 2e 63 61  a.j++){.    u.ca
26065 2e 61 52 6f 6f 74 5b 75 2e 63 61 2e 6a 5d 20 3d  .aRoot[u.ca.j] =
26066 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62   (int)sqlite3Vdb
26067 65 49 6e 74 56 61 6c 75 65 28 26 70 49 6e 31 5b  eIntValue(&pIn1[
26068 75 2e 63 61 2e 6a 5d 29 3b 0a 20 20 7d 0a 20 20  u.ca.j]);.  }.  
26069 75 2e 63 61 2e 61 52 6f 6f 74 5b 75 2e 63 61 2e  u.ca.aRoot[u.ca.
2606a 6a 5d 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  j] = 0;.  assert
2606b 28 20 70 4f 70 2d 3e 70 35 3c 64 62 2d 3e 6e 44  ( pOp->p5<db->nD
2606c 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  b );.  assert( (
2606d 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
2606e 28 28 79 44 62 4d 61 73 6b 29 31 29 3c 3c 70 4f  ((yDbMask)1)<<pO
2606f 70 2d 3e 70 35 29 29 21 3d 30 20 29 3b 0a 20 20  p->p5))!=0 );.  
26070 75 2e 63 61 2e 7a 20 3d 20 73 71 6c 69 74 65 33  u.ca.z = sqlite3
26071 42 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 68  BtreeIntegrityCh
26072 65 63 6b 28 64 62 2d 3e 61 44 62 5b 70 4f 70 2d  eck(db->aDb[pOp-
26073 3e 70 35 5d 2e 70 42 74 2c 20 75 2e 63 61 2e 61  >p5].pBt, u.ca.a
26074 52 6f 6f 74 2c 20 75 2e 63 61 2e 6e 52 6f 6f 74  Root, u.ca.nRoot
26075 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
26076 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26077 20 20 20 28 69 6e 74 29 75 2e 63 61 2e 70 6e 45     (int)u.ca.pnE
26078 72 72 2d 3e 75 2e 69 2c 20 26 75 2e 63 61 2e 6e  rr->u.i, &u.ca.n
26079 45 72 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  Err);.  sqlite3D
2607a 62 46 72 65 65 28 64 62 2c 20 75 2e 63 61 2e 61  bFree(db, u.ca.a
2607b 52 6f 6f 74 29 3b 0a 20 20 75 2e 63 61 2e 70 6e  Root);.  u.ca.pn
2607c 45 72 72 2d 3e 75 2e 69 20 2d 3d 20 75 2e 63 61  Err->u.i -= u.ca
2607d 2e 6e 45 72 72 3b 0a 20 20 73 71 6c 69 74 65 33  .nErr;.  sqlite3
2607e 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70  VdbeMemSetNull(p
2607f 49 6e 31 29 3b 0a 20 20 69 66 28 20 75 2e 63 61  In1);.  if( u.ca
26080 2e 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20  .nErr==0 ){.    
26081 61 73 73 65 72 74 28 20 75 2e 63 61 2e 7a 3d 3d  assert( u.ca.z==
26082 30 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  0 );.  }else if(
26083 20 75 2e 63 61 2e 7a 3d 3d 30 20 29 7b 0a 20 20   u.ca.z==0 ){.  
26084 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
26085 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
26086 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
26087 28 70 49 6e 31 2c 20 75 2e 63 61 2e 7a 2c 20 2d  (pIn1, u.ca.z, -
26088 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  1, SQLITE_UTF8, 
26089 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20  sqlite3_free);. 
2608a 20 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f   }.  UPDATE_MAX_
2608b 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a  BLOBSIZE(pIn1);.
2608c 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2608d 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 49 6e 31  ngeEncoding(pIn1
2608e 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 62  , encoding);.  b
2608f 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
26090 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  * SQLITE_OMIT_IN
26091 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f  TEGRITY_CHECK */
26092 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77  ../* Opcode: Row
26093 53 65 74 41 64 64 20 50 31 20 50 32 20 2a 20 2a  SetAdd P1 P2 * *
26094 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 73 65 72 74 20   *.**.** Insert 
26095 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75  the integer valu
26096 65 20 68 65 6c 64 20 62 79 20 72 65 67 69 73 74  e held by regist
26097 65 72 20 50 32 20 69 6e 74 6f 20 61 20 62 6f 6f  er P2 into a boo
26098 6c 65 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 65  lean index.** he
26099 6c 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ld in register P
2609a 31 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 73 73 65  1..**.** An asse
2609b 72 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 50  rtion fails if P
2609c 32 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65  2 is not an inte
2609d 67 65 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ger..*/.case OP_
2609e 52 6f 77 53 65 74 41 64 64 3a 20 7b 20 20 20 20  RowSetAdd: {    
2609f 20 20 20 2f 2a 20 69 6e 31 2c 20 69 6e 32 20 2a     /* in1, in2 *
260a0 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d  /.  pIn1 = &aMem
260a1 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e  [pOp->p1];.  pIn
260a2 32 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  2 = &aMem[pOp->p
260a3 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70  2];.  assert( (p
260a4 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In2->flags & MEM
260a5 5f 49 6e 74 29 21 3d 30 20 29 3b 0a 20 20 69 66  _Int)!=0 );.  if
260a6 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  ( (pIn1->flags &
260a7 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20   MEM_RowSet)==0 
260a8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
260a9 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 70  beMemSetRowSet(p
260aa 49 6e 31 29 3b 0a 20 20 20 20 69 66 28 20 28 70  In1);.    if( (p
260ab 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
260ac 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 20 67 6f  _RowSet)==0 ) go
260ad 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20  to no_mem;.  }. 
260ae 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e   sqlite3RowSetIn
260af 73 65 72 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f  sert(pIn1->u.pRo
260b0 77 53 65 74 2c 20 70 49 6e 32 2d 3e 75 2e 69 29  wSet, pIn2->u.i)
260b1 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
260b2 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 52   Opcode: RowSetR
260b3 65 61 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a  ead P1 P2 P3 * *
260b4 0a 2a 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74  .**.** Extract t
260b5 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c 75  he smallest valu
260b6 65 20 66 72 6f 6d 20 62 6f 6f 6c 65 61 6e 20 69  e from boolean i
260b7 6e 64 65 78 20 50 31 20 61 6e 64 20 70 75 74 20  ndex P1 and put 
260b8 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 0a  that value into.
260b9 2a 2a 20 72 65 67 69 73 74 65 72 20 50 33 2e 20  ** register P3. 
260ba 20 4f 72 2c 20 69 66 20 62 6f 6f 6c 65 61 6e 20   Or, if boolean 
260bb 69 6e 64 65 78 20 50 31 20 69 73 20 69 6e 69 74  index P1 is init
260bc 69 61 6c 6c 79 20 65 6d 70 74 79 2c 20 6c 65 61  ially empty, lea
260bd 76 65 20 50 33 0a 2a 2a 20 75 6e 63 68 61 6e 67  ve P3.** unchang
260be 65 64 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 69  ed and jump to i
260bf 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 0a 2a  nstruction P2..*
260c0 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74  /.case OP_RowSet
260c1 52 65 61 64 3a 20 7b 20 20 20 20 20 20 20 2f 2a  Read: {       /*
260c2 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 6f 75 74 33   jump, in1, out3
260c3 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
260c4 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
260c5 76 65 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f  ved into u.cb */
260c6 0a 20 20 69 36 34 20 76 61 6c 3b 0a 23 65 6e 64  .  i64 val;.#end
260c7 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
260c8 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
260c9 20 75 2e 63 62 20 2a 2f 0a 20 20 43 48 45 43 4b   u.cb */.  CHECK
260ca 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a  _FOR_INTERRUPT;.
260cb 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70    pIn1 = &aMem[p
260cc 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 28  Op->p1];.  if( (
260cd 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
260ce 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 0a 20 20 20  M_RowSet)==0.   
260cf 7c 7c 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74  || sqlite3RowSet
260d0 4e 65 78 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f  Next(pIn1->u.pRo
260d1 77 53 65 74 2c 20 26 75 2e 63 62 2e 76 61 6c 29  wSet, &u.cb.val)
260d2 3d 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20  ==0.  ){.    /* 
260d3 54 68 65 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65  The boolean inde
260d4 78 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 20 20  x is empty */.  
260d5 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
260d6 53 65 74 4e 75 6c 6c 28 70 49 6e 31 29 3b 0a 20  SetNull(pIn1);. 
260d7 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
260d8 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  - 1;.  }else{.  
260d9 20 20 2f 2a 20 41 20 76 61 6c 75 65 20 77 61 73    /* A value was
260da 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   pulled from the
260db 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 73 71   index */.    sq
260dc 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49  lite3VdbeMemSetI
260dd 6e 74 36 34 28 26 61 4d 65 6d 5b 70 4f 70 2d 3e  nt64(&aMem[pOp->
260de 70 33 5d 2c 20 75 2e 63 62 2e 76 61 6c 29 3b 0a  p3], u.cb.val);.
260df 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
260e0 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65  /* Opcode: RowSe
260e1 74 54 65 73 74 20 50 31 20 50 32 20 50 33 20 50  tTest P1 P2 P3 P
260e2 34 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  4.**.** Register
260e3 20 50 33 20 69 73 20 61 73 73 75 6d 65 64 20 74   P3 is assumed t
260e4 6f 20 68 6f 6c 64 20 61 20 36 34 2d 62 69 74 20  o hold a 64-bit 
260e5 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 20 49  integer value. I
260e6 66 20 72 65 67 69 73 74 65 72 20 50 31 0a 2a 2a  f register P1.**
260e7 20 63 6f 6e 74 61 69 6e 73 20 61 20 52 6f 77 53   contains a RowS
260e8 65 74 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68  et object and th
260e9 61 74 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74  at RowSet object
260ea 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65   contains.** the
260eb 20 76 61 6c 75 65 20 68 65 6c 64 20 69 6e 20 50   value held in P
260ec 33 2c 20 6a 75 6d 70 20 74 6f 20 72 65 67 69 73  3, jump to regis
260ed 74 65 72 20 50 32 2e 20 4f 74 68 65 72 77 69 73  ter P2. Otherwis
260ee 65 2c 20 69 6e 73 65 72 74 20 74 68 65 0a 2a 2a  e, insert the.**
260ef 20 69 6e 74 65 67 65 72 20 69 6e 20 50 33 20 69   integer in P3 i
260f0 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74 20 61  nto the RowSet a
260f1 6e 64 20 63 6f 6e 74 69 6e 75 65 20 6f 6e 20 74  nd continue on t
260f2 6f 20 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f 70  o the.** next op
260f3 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  code..**.** The 
260f4 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 69 73  RowSet object is
260f5 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72 20 74   optimized for t
260f6 68 65 20 63 61 73 65 20 77 68 65 72 65 20 73 75  he case where su
260f7 63 63 65 73 73 69 76 65 20 73 65 74 73 0a 2a 2a  ccessive sets.**
260f8 20 6f 66 20 69 6e 74 65 67 65 72 73 2c 20 77 68   of integers, wh
260f9 65 72 65 20 65 61 63 68 20 73 65 74 20 63 6f 6e  ere each set con
260fa 74 61 69 6e 73 20 6e 6f 20 64 75 70 6c 69 63 61  tains no duplica
260fb 74 65 73 2e 20 45 61 63 68 20 73 65 74 0a 2a 2a  tes. Each set.**
260fc 20 6f 66 20 76 61 6c 75 65 73 20 69 73 20 69 64   of values is id
260fd 65 6e 74 69 66 69 65 64 20 62 79 20 61 20 75 6e  entified by a un
260fe 69 71 75 65 20 50 34 20 76 61 6c 75 65 2e 20 54  ique P4 value. T
260ff 68 65 20 66 69 72 73 74 20 73 65 74 0a 2a 2a 20  he first set.** 
26100 6d 75 73 74 20 68 61 76 65 20 50 34 3d 3d 30 2c  must have P4==0,
26101 20 74 68 65 20 66 69 6e 61 6c 20 73 65 74 20 50   the final set P
26102 34 3d 2d 31 2e 20 20 50 34 20 6d 75 73 74 20 62  4=-1.  P4 must b
26103 65 20 65 69 74 68 65 72 20 2d 31 20 6f 72 0a 2a  e either -1 or.*
26104 2a 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20  * non-negative. 
26105 20 46 6f 72 20 6e 6f 6e 2d 6e 65 67 61 74 69 76   For non-negativ
26106 65 20 76 61 6c 75 65 73 20 6f 66 20 50 34 20 6f  e values of P4 o
26107 6e 6c 79 20 74 68 65 20 6c 6f 77 65 72 20 34 0a  nly the lower 4.
26108 2a 2a 20 62 69 74 73 20 61 72 65 20 73 69 67 6e  ** bits are sign
26109 69 66 69 63 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  ificant..**.** T
2610a 68 69 73 20 61 6c 6c 6f 77 73 20 6f 70 74 69 6d  his allows optim
2610b 69 7a 61 74 69 6f 6e 73 3a 20 28 61 29 20 77 68  izations: (a) wh
2610c 65 6e 20 50 34 3d 3d 30 20 74 68 65 72 65 20 69  en P4==0 there i
2610d 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 73  s no need to tes
2610e 74 0a 2a 2a 20 74 68 65 20 72 6f 77 73 65 74 20  t.** the rowset 
2610f 6f 62 6a 65 63 74 20 66 6f 72 20 50 33 2c 20 61  object for P3, a
26110 73 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65  s it is guarante
26111 65 64 20 6e 6f 74 20 74 6f 20 63 6f 6e 74 61 69  ed not to contai
26112 6e 20 69 74 2c 0a 2a 2a 20 28 62 29 20 77 68 65  n it,.** (b) whe
26113 6e 20 50 34 3d 3d 2d 31 20 74 68 65 72 65 20 69  n P4==-1 there i
26114 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 73  s no need to ins
26115 65 72 74 20 74 68 65 20 76 61 6c 75 65 2c 20 61  ert the value, a
26116 73 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76  s it will.** nev
26117 65 72 20 62 65 20 74 65 73 74 65 64 20 66 6f 72  er be tested for
26118 2c 20 61 6e 64 20 28 63 29 20 77 68 65 6e 20 61  , and (c) when a
26119 20 76 61 6c 75 65 20 74 68 61 74 20 69 73 20 70   value that is p
2611a 61 72 74 20 6f 66 20 73 65 74 20 58 20 69 73 0a  art of set X is.
2611b 2a 2a 20 69 6e 73 65 72 74 65 64 2c 20 74 68 65  ** inserted, the
2611c 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f  re is no need to
2611d 20 73 65 61 72 63 68 20 74 6f 20 73 65 65 20 69   search to see i
2611e 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65  f the same value
2611f 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73   was.** previous
26120 6c 79 20 69 6e 73 65 72 74 65 64 20 61 73 20 70  ly inserted as p
26121 61 72 74 20 6f 66 20 73 65 74 20 58 20 28 6f 6e  art of set X (on
26122 6c 79 20 69 66 20 69 74 20 77 61 73 20 70 72 65  ly if it was pre
26123 76 69 6f 75 73 6c 79 0a 2a 2a 20 69 6e 73 65 72  viously.** inser
26124 74 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 73  ted as part of s
26125 6f 6d 65 20 6f 74 68 65 72 20 73 65 74 29 2e 0a  ome other set)..
26126 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65  */.case OP_RowSe
26127 74 54 65 73 74 3a 20 7b 20 20 20 20 20 20 20 20  tTest: {        
26128 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26129 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a  jump, in1, in3 *
2612a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
2612b 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
2612c 64 20 69 6e 74 6f 20 75 2e 63 63 20 2a 2f 0a 20  d into u.cc */. 
2612d 20 69 6e 74 20 69 53 65 74 3b 0a 20 20 69 6e 74   int iSet;.  int
2612e 20 65 78 69 73 74 73 3b 0a 23 65 6e 64 69 66 20   exists;.#endif 
2612f 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
26130 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
26131 63 63 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d 20  cc */..  pIn1 = 
26132 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  &aMem[pOp->p1];.
26133 20 20 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70    pIn3 = &aMem[p
26134 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63 63 2e  Op->p3];.  u.cc.
26135 69 53 65 74 20 3d 20 70 4f 70 2d 3e 70 34 2e 69  iSet = pOp->p4.i
26136 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 33  ;.  assert( pIn3
26137 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20  ->flags&MEM_Int 
26138 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72  );..  /* If ther
26139 65 20 69 73 20 61 6e 79 74 68 69 6e 67 20 6f 74  e is anything ot
2613a 68 65 72 20 74 68 61 6e 20 61 20 72 6f 77 73 65  her than a rowse
2613b 74 20 6f 62 6a 65 63 74 20 69 6e 20 6d 65 6d 6f  t object in memo
2613c 72 79 20 63 65 6c 6c 20 50 31 2c 0a 20 20 2a 2a  ry cell P1,.  **
2613d 20 64 65 6c 65 74 65 20 69 74 20 6e 6f 77 20 61   delete it now a
2613e 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 50 31  nd initialize P1
2613f 20 77 69 74 68 20 61 6e 20 65 6d 70 74 79 20 72   with an empty r
26140 6f 77 73 65 74 0a 20 20 2a 2f 0a 20 20 69 66 28  owset.  */.  if(
26141 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
26142 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
26143 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
26144 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 70 49  eMemSetRowSet(pI
26145 6e 31 29 3b 0a 20 20 20 20 69 66 28 20 28 70 49  n1);.    if( (pI
26146 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
26147 52 6f 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74  RowSet)==0 ) got
26148 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20  o no_mem;.  }.. 
26149 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
2614a 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29  type==P4_INT32 )
2614b 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 63  ;.  assert( u.cc
2614c 2e 69 53 65 74 3d 3d 2d 31 20 7c 7c 20 75 2e 63  .iSet==-1 || u.c
2614d 63 2e 69 53 65 74 3e 3d 30 20 29 3b 0a 20 20 69  c.iSet>=0 );.  i
2614e 66 28 20 75 2e 63 63 2e 69 53 65 74 20 29 7b 0a  f( u.cc.iSet ){.
2614f 20 20 20 20 75 2e 63 63 2e 65 78 69 73 74 73 20      u.cc.exists 
26150 3d 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54  = sqlite3RowSetT
26151 65 73 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77  est(pIn1->u.pRow
26152 53 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Set,.           
26153 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26154 20 20 20 20 28 75 38 29 28 75 2e 63 63 2e 69 53      (u8)(u.cc.iS
26155 65 74 3e 3d 30 20 3f 20 75 2e 63 63 2e 69 53 65  et>=0 ? u.cc.iSe
26156 74 20 26 20 30 78 66 20 3a 20 30 78 66 66 29 2c  t & 0xf : 0xff),
26157 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
26158 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26159 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20  pIn3->u.i);.    
2615a 69 66 28 20 75 2e 63 63 2e 65 78 69 73 74 73 20  if( u.cc.exists 
2615b 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  ){.      pc = pO
2615c 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20  p->p2 - 1;.     
2615d 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2615e 7d 0a 20 20 69 66 28 20 75 2e 63 63 2e 69 53 65  }.  if( u.cc.iSe
2615f 74 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  t>=0 ){.    sqli
26160 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28  te3RowSetInsert(
26161 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c  pIn1->u.pRowSet,
26162 20 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 7d   pIn3->u.i);.  }
26163 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69  .  break;.}...#i
26164 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
26165 54 5f 54 52 49 47 47 45 52 0a 0a 2f 2a 20 4f 70  T_TRIGGER../* Op
26166 63 6f 64 65 3a 20 50 72 6f 67 72 61 6d 20 50 31  code: Program P1
26167 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
26168 2a 20 45 78 65 63 75 74 65 20 74 68 65 20 74 72  * Execute the tr
26169 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 70 61  igger program pa
2616a 73 73 65 64 20 61 73 20 50 34 20 28 74 79 70 65  ssed as P4 (type
2616b 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 29 2e   P4_SUBPROGRAM).
2616c 20 0a 2a 2a 0a 2a 2a 20 50 31 20 63 6f 6e 74 61   .**.** P1 conta
2616d 69 6e 73 20 74 68 65 20 61 64 64 72 65 73 73 20  ins the address 
2616e 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65  of the memory ce
2616f 6c 6c 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ll that contains
26170 20 74 68 65 20 66 69 72 73 74 20 6d 65 6d 6f 72   the first memor
26171 79 20 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 61 6e  y .** cell in an
26172 20 61 72 72 61 79 20 6f 66 20 76 61 6c 75 65 73   array of values
26173 20 75 73 65 64 20 61 73 20 61 72 67 75 6d 65 6e   used as argumen
26174 74 73 20 74 6f 20 74 68 65 20 73 75 62 2d 70 72  ts to the sub-pr
26175 6f 67 72 61 6d 2e 20 50 32 20 0a 2a 2a 20 63 6f  ogram. P2 .** co
26176 6e 74 61 69 6e 73 20 74 68 65 20 61 64 64 72 65  ntains the addre
26177 73 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66  ss to jump to if
26178 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d   the sub-program
26179 20 74 68 72 6f 77 73 20 61 6e 20 49 47 4e 4f 52   throws an IGNOR
2617a 45 20 0a 2a 2a 20 65 78 63 65 70 74 69 6f 6e 20  E .** exception 
2617b 75 73 69 6e 67 20 74 68 65 20 52 41 49 53 45 28  using the RAISE(
2617c 29 20 66 75 6e 63 74 69 6f 6e 2e 20 52 65 67 69  ) function. Regi
2617d 73 74 65 72 20 50 33 20 63 6f 6e 74 61 69 6e 73  ster P3 contains
2617e 20 74 68 65 20 61 64 64 72 65 73 73 20 0a 2a 2a   the address .**
2617f 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c   of a memory cel
26180 6c 20 69 6e 20 74 68 69 73 20 28 74 68 65 20 70  l in this (the p
26181 61 72 65 6e 74 29 20 56 4d 20 74 68 61 74 20 69  arent) VM that i
26182 73 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61  s used to alloca
26183 74 65 20 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f 72  te the .** memor
26184 79 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68  y required by th
26185 65 20 73 75 62 2d 76 64 62 65 20 61 74 20 72 75  e sub-vdbe at ru
26186 6e 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 50 34 20  ntime..**.** P4 
26187 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
26188 74 68 65 20 56 4d 20 63 6f 6e 74 61 69 6e 69 6e  the VM containin
26189 67 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72  g the trigger pr
2618a 6f 67 72 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ogram..*/.case O
2618b 50 5f 50 72 6f 67 72 61 6d 3a 20 7b 20 20 20 20  P_Program: {    
2618c 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23      /* jump */.#
2618d 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
2618e 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
2618f 6e 74 6f 20 75 2e 63 64 20 2a 2f 0a 20 20 69 6e  nto u.cd */.  in
26190 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  t nMem;         
26191 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
26192 6f 66 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74  of memory regist
26193 65 72 73 20 66 6f 72 20 73 75 62 2d 70 72 6f 67  ers for sub-prog
26194 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  ram */.  int nBy
26195 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  te;             
26196 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 72 75 6e   /* Bytes of run
26197 74 69 6d 65 20 73 70 61 63 65 20 72 65 71 75 69  time space requi
26198 72 65 64 20 66 6f 72 20 73 75 62 2d 70 72 6f 67  red for sub-prog
26199 72 61 6d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52  ram */.  Mem *pR
2619a 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
2619b 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20   /* Register to 
2619c 61 6c 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d 65  allocate runtime
2619d 20 73 70 61 63 65 20 2a 2f 0a 20 20 4d 65 6d 20   space */.  Mem 
2619e 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *pMem;          
2619f 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69      /* Used to i
261a0 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 6d  terate through m
261a1 65 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20  emory cells */. 
261a2 20 4d 65 6d 20 2a 70 45 6e 64 3b 20 20 20 20 20   Mem *pEnd;     
261a3 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74           /* Last
261a4 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20   memory cell in 
261a5 6e 65 77 20 61 72 72 61 79 20 2a 2f 0a 20 20 56  new array */.  V
261a6 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65  dbeFrame *pFrame
261a7 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 64  ;      /* New vd
261a8 62 65 20 66 72 61 6d 65 20 74 6f 20 65 78 65 63  be frame to exec
261a9 75 74 65 20 69 6e 20 2a 2f 0a 20 20 53 75 62 50  ute in */.  SubP
261aa 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d  rogram *pProgram
261ab 3b 20 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72  ;   /* Sub-progr
261ac 61 6d 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f  am to execute */
261ad 0a 20 20 76 6f 69 64 20 2a 74 3b 20 20 20 20 20  .  void *t;     
261ae 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
261af 6b 65 6e 20 69 64 65 6e 74 69 66 79 69 6e 67 20  ken identifying 
261b0 74 72 69 67 67 65 72 20 2a 2f 0a 23 65 6e 64 69  trigger */.#endi
261b1 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
261b2 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
261b3 75 2e 63 64 20 2a 2f 0a 0a 20 20 75 2e 63 64 2e  u.cd */..  u.cd.
261b4 70 50 72 6f 67 72 61 6d 20 3d 20 70 4f 70 2d 3e  pProgram = pOp->
261b5 70 34 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 75  p4.pProgram;.  u
261b6 2e 63 64 2e 70 52 74 20 3d 20 26 61 4d 65 6d 5b  .cd.pRt = &aMem[
261b7 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65  pOp->p3];.  asse
261b8 72 74 28 20 75 2e 63 64 2e 70 50 72 6f 67 72 61  rt( u.cd.pProgra
261b9 6d 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f  m->nOp>0 );..  /
261ba 2a 20 49 66 20 74 68 65 20 70 35 20 66 6c 61 67  * If the p5 flag
261bb 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65 6e 20   is clear, then 
261bc 72 65 63 75 72 73 69 76 65 20 69 6e 76 6f 63 61  recursive invoca
261bd 74 69 6f 6e 20 6f 66 20 74 72 69 67 67 65 72 73  tion of triggers
261be 20 69 73 0a 20 20 2a 2a 20 64 69 73 61 62 6c 65   is.  ** disable
261bf 64 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 20  d for backwards 
261c0 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 28 70  compatibility (p
261c1 35 20 69 73 20 73 65 74 20 69 66 20 74 68 69 73  5 is set if this
261c2 20 73 75 62 2d 70 72 6f 67 72 61 6d 0a 20 20 2a   sub-program.  *
261c3 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20 74 72  * is really a tr
261c4 69 67 67 65 72 2c 20 6e 6f 74 20 61 20 66 6f 72  igger, not a for
261c5 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 2c  eign key action,
261c6 20 61 6e 64 20 74 68 65 20 66 6c 61 67 20 73 65   and the flag se
261c7 74 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72  t.  ** and clear
261c8 65 64 20 62 79 20 74 68 65 20 22 50 52 41 47 4d  ed by the "PRAGM
261c9 41 20 72 65 63 75 72 73 69 76 65 5f 74 72 69 67  A recursive_trig
261ca 67 65 72 73 22 20 63 6f 6d 6d 61 6e 64 20 69 73  gers" command is
261cb 20 63 6c 65 61 72 29 2e 0a 20 20 2a 2a 0a 20 20   clear)..  **.  
261cc 2a 2a 20 49 74 20 69 73 20 72 65 63 75 72 73 69  ** It is recursi
261cd 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66  ve invocation of
261ce 20 74 72 69 67 67 65 72 73 2c 20 61 74 20 74 68   triggers, at th
261cf 65 20 53 51 4c 20 6c 65 76 65 6c 2c 20 74 68 61  e SQL level, tha
261d0 74 20 69 73 0a 20 20 2a 2a 20 64 69 73 61 62 6c  t is.  ** disabl
261d1 65 64 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65  ed. In some case
261d2 73 20 61 20 73 69 6e 67 6c 65 20 74 72 69 67 67  s a single trigg
261d3 65 72 20 6d 61 79 20 67 65 6e 65 72 61 74 65 20  er may generate 
261d4 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 0a 20 20  more than one.  
261d5 2a 2a 20 53 75 62 50 72 6f 67 72 61 6d 20 28 69  ** SubProgram (i
261d6 66 20 74 68 65 20 74 72 69 67 67 65 72 20 6d 61  f the trigger ma
261d7 79 20 62 65 20 65 78 65 63 75 74 65 64 20 77 69  y be executed wi
261d8 74 68 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  th more than one
261d9 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20   different.  ** 
261da 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f  ON CONFLICT algo
261db 72 69 74 68 6d 29 2e 20 53 75 62 50 72 6f 67 72  rithm). SubProgr
261dc 61 6d 20 73 74 72 75 63 74 75 72 65 73 20 61 73  am structures as
261dd 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 0a  sociated with a.
261de 20 20 2a 2a 20 73 69 6e 67 6c 65 20 74 72 69 67    ** single trig
261df 67 65 72 20 61 6c 6c 20 68 61 76 65 20 74 68 65  ger all have the
261e0 20 73 61 6d 65 20 76 61 6c 75 65 20 66 6f 72 20   same value for 
261e1 74 68 65 20 53 75 62 50 72 6f 67 72 61 6d 2e 74  the SubProgram.t
261e2 6f 6b 65 6e 0a 20 20 2a 2a 20 76 61 72 69 61 62  oken.  ** variab
261e3 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f  le.  */.  if( pO
261e4 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 75 2e 63  p->p5 ){.    u.c
261e5 64 2e 74 20 3d 20 75 2e 63 64 2e 70 50 72 6f 67  d.t = u.cd.pProg
261e6 72 61 6d 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20  ram->token;.    
261e7 66 6f 72 28 75 2e 63 64 2e 70 46 72 61 6d 65 3d  for(u.cd.pFrame=
261e8 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 63 64 2e  p->pFrame; u.cd.
261e9 70 46 72 61 6d 65 20 26 26 20 75 2e 63 64 2e 70  pFrame && u.cd.p
261ea 46 72 61 6d 65 2d 3e 74 6f 6b 65 6e 21 3d 75 2e  Frame->token!=u.
261eb 63 64 2e 74 3b 20 75 2e 63 64 2e 70 46 72 61 6d  cd.t; u.cd.pFram
261ec 65 3d 75 2e 63 64 2e 70 46 72 61 6d 65 2d 3e 70  e=u.cd.pFrame->p
261ed 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 69 66 28  Parent);.    if(
261ee 20 75 2e 63 64 2e 70 46 72 61 6d 65 20 29 20 62   u.cd.pFrame ) b
261ef 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  reak;.  }..  if(
261f0 20 70 2d 3e 6e 46 72 61 6d 65 3e 3d 64 62 2d 3e   p->nFrame>=db->
261f1 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
261f2 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54  MIT_TRIGGER_DEPT
261f3 48 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  H] ){.    rc = S
261f4 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
261f5 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
261f6 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
261f7 62 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 6c 65 76  b, "too many lev
261f8 65 6c 73 20 6f 66 20 74 72 69 67 67 65 72 20 72  els of trigger r
261f9 65 63 75 72 73 69 6f 6e 22 29 3b 0a 20 20 20 20  ecursion");.    
261fa 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  break;.  }..  /*
261fb 20 52 65 67 69 73 74 65 72 20 75 2e 63 64 2e 70   Register u.cd.p
261fc 52 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74  Rt is used to st
261fd 6f 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 72  ore the memory r
261fe 65 71 75 69 72 65 64 20 74 6f 20 73 61 76 65 20  equired to save 
261ff 74 68 65 20 73 74 61 74 65 0a 20 20 2a 2a 20 6f  the state.  ** o
26200 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72  f the current pr
26201 6f 67 72 61 6d 2c 20 61 6e 64 20 74 68 65 20 6d  ogram, and the m
26202 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 20 61  emory required a
26203 74 20 72 75 6e 74 69 6d 65 20 74 6f 20 65 78 65  t runtime to exe
26204 63 75 74 65 0a 20 20 2a 2a 20 74 68 65 20 74 72  cute.  ** the tr
26205 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 20 49  igger program. I
26206 66 20 74 68 69 73 20 74 72 69 67 67 65 72 20 68  f this trigger h
26207 61 73 20 62 65 65 6e 20 66 69 72 65 64 20 62 65  as been fired be
26208 66 6f 72 65 2c 20 74 68 65 6e 20 75 2e 63 64 2e  fore, then u.cd.
26209 70 52 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65  pRt.  ** is alre
2620a 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4f  ady allocated. O
2620b 74 68 65 72 77 69 73 65 2c 20 69 74 20 6d 75 73  therwise, it mus
2620c 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  t be initialized
2620d 2e 20 20 2a 2f 0a 20 20 69 66 28 20 28 75 2e 63  .  */.  if( (u.c
2620e 64 2e 70 52 74 2d 3e 66 6c 61 67 73 26 4d 45 4d  d.pRt->flags&MEM
2620f 5f 46 72 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20  _Frame)==0 ){.  
26210 20 20 2f 2a 20 53 75 62 50 72 6f 67 72 61 6d 2e    /* SubProgram.
26211 6e 4d 65 6d 20 69 73 20 73 65 74 20 74 6f 20 74  nMem is set to t
26212 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65 6d  he number of mem
26213 6f 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 62  ory cells used b
26214 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 72 6f  y the.    ** pro
26215 67 72 61 6d 20 73 74 6f 72 65 64 20 69 6e 20 53  gram stored in S
26216 75 62 50 72 6f 67 72 61 6d 2e 61 4f 70 2e 20 41  ubProgram.aOp. A
26217 73 20 77 65 6c 6c 20 61 73 20 74 68 65 73 65 2c  s well as these,
26218 20 6f 6e 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20   one memory.    
26219 2a 2a 20 63 65 6c 6c 20 69 73 20 72 65 71 75 69  ** cell is requi
2621a 72 65 64 20 66 6f 72 20 65 61 63 68 20 63 75 72  red for each cur
2621b 73 6f 72 20 75 73 65 64 20 62 79 20 74 68 65 20  sor used by the 
2621c 70 72 6f 67 72 61 6d 2e 20 53 65 74 20 6c 6f 63  program. Set loc
2621d 61 6c 0a 20 20 20 20 2a 2a 20 76 61 72 69 61 62  al.    ** variab
2621e 6c 65 20 75 2e 63 64 2e 6e 4d 65 6d 20 28 61 6e  le u.cd.nMem (an
2621f 64 20 6c 61 74 65 72 2c 20 56 64 62 65 46 72 61  d later, VdbeFra
26220 6d 65 2e 6e 43 68 69 6c 64 4d 65 6d 29 20 74 6f  me.nChildMem) to
26221 20 74 68 69 73 20 76 61 6c 75 65 2e 0a 20 20 20   this value..   
26222 20 2a 2f 0a 20 20 20 20 75 2e 63 64 2e 6e 4d 65   */.    u.cd.nMe
26223 6d 20 3d 20 75 2e 63 64 2e 70 50 72 6f 67 72 61  m = u.cd.pProgra
26224 6d 2d 3e 6e 4d 65 6d 20 2b 20 75 2e 63 64 2e 70  m->nMem + u.cd.p
26225 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3b 0a 20  Program->nCsr;. 
26226 20 20 20 75 2e 63 64 2e 6e 42 79 74 65 20 3d 20     u.cd.nByte = 
26227 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 56 64  ROUND8(sizeof(Vd
26228 62 65 46 72 61 6d 65 29 29 0a 20 20 20 20 20 20  beFrame)).      
26229 20 20 20 20 20 20 20 20 2b 20 75 2e 63 64 2e 6e          + u.cd.n
2622a 4d 65 6d 20 2a 20 73 69 7a 65 6f 66 28 4d 65 6d  Mem * sizeof(Mem
2622b 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
2622c 2b 20 75 2e 63 64 2e 70 50 72 6f 67 72 61 6d 2d  + u.cd.pProgram-
2622d 3e 6e 43 73 72 20 2a 20 73 69 7a 65 6f 66 28 56  >nCsr * sizeof(V
2622e 64 62 65 43 75 72 73 6f 72 20 2a 29 0a 20 20 20  dbeCursor *).   
2622f 20 20 20 20 20 20 20 20 20 20 20 2b 20 75 2e 63             + u.c
26230 64 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 6e 63  d.pProgram->nOnc
26231 65 20 2a 20 73 69 7a 65 6f 66 28 75 38 29 3b 0a  e * sizeof(u8);.
26232 20 20 20 20 75 2e 63 64 2e 70 46 72 61 6d 65 20      u.cd.pFrame 
26233 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
26234 63 5a 65 72 6f 28 64 62 2c 20 75 2e 63 64 2e 6e  cZero(db, u.cd.n
26235 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 21  Byte);.    if( !
26236 75 2e 63 64 2e 70 46 72 61 6d 65 20 29 7b 0a 20  u.cd.pFrame ){. 
26237 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d       goto no_mem
26238 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
26239 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
2623a 65 28 75 2e 63 64 2e 70 52 74 29 3b 0a 20 20 20  e(u.cd.pRt);.   
2623b 20 75 2e 63 64 2e 70 52 74 2d 3e 66 6c 61 67 73   u.cd.pRt->flags
2623c 20 3d 20 4d 45 4d 5f 46 72 61 6d 65 3b 0a 20 20   = MEM_Frame;.  
2623d 20 20 75 2e 63 64 2e 70 52 74 2d 3e 75 2e 70 46    u.cd.pRt->u.pF
2623e 72 61 6d 65 20 3d 20 75 2e 63 64 2e 70 46 72 61  rame = u.cd.pFra
2623f 6d 65 3b 0a 0a 20 20 20 20 75 2e 63 64 2e 70 46  me;..    u.cd.pF
26240 72 61 6d 65 2d 3e 76 20 3d 20 70 3b 0a 20 20 20  rame->v = p;.   
26241 20 75 2e 63 64 2e 70 46 72 61 6d 65 2d 3e 6e 43   u.cd.pFrame->nC
26242 68 69 6c 64 4d 65 6d 20 3d 20 75 2e 63 64 2e 6e  hildMem = u.cd.n
26243 4d 65 6d 3b 0a 20 20 20 20 75 2e 63 64 2e 70 46  Mem;.    u.cd.pF
26244 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72 20  rame->nChildCsr 
26245 3d 20 75 2e 63 64 2e 70 50 72 6f 67 72 61 6d 2d  = u.cd.pProgram-
26246 3e 6e 43 73 72 3b 0a 20 20 20 20 75 2e 63 64 2e  >nCsr;.    u.cd.
26247 70 46 72 61 6d 65 2d 3e 70 63 20 3d 20 70 63 3b  pFrame->pc = pc;
26248 0a 20 20 20 20 75 2e 63 64 2e 70 46 72 61 6d 65  .    u.cd.pFrame
26249 2d 3e 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d 65 6d  ->aMem = p->aMem
2624a 3b 0a 20 20 20 20 75 2e 63 64 2e 70 46 72 61 6d  ;.    u.cd.pFram
2624b 65 2d 3e 6e 4d 65 6d 20 3d 20 70 2d 3e 6e 4d 65  e->nMem = p->nMe
2624c 6d 3b 0a 20 20 20 20 75 2e 63 64 2e 70 46 72 61  m;.    u.cd.pFra
2624d 6d 65 2d 3e 61 70 43 73 72 20 3d 20 70 2d 3e 61  me->apCsr = p->a
2624e 70 43 73 72 3b 0a 20 20 20 20 75 2e 63 64 2e 70  pCsr;.    u.cd.p
2624f 46 72 61 6d 65 2d 3e 6e 43 75 72 73 6f 72 20 3d  Frame->nCursor =
26250 20 70 2d 3e 6e 43 75 72 73 6f 72 3b 0a 20 20 20   p->nCursor;.   
26251 20 75 2e 63 64 2e 70 46 72 61 6d 65 2d 3e 61 4f   u.cd.pFrame->aO
26252 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20  p = p->aOp;.    
26253 75 2e 63 64 2e 70 46 72 61 6d 65 2d 3e 6e 4f 70  u.cd.pFrame->nOp
26254 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 20 20 75   = p->nOp;.    u
26255 2e 63 64 2e 70 46 72 61 6d 65 2d 3e 74 6f 6b 65  .cd.pFrame->toke
26256 6e 20 3d 20 75 2e 63 64 2e 70 50 72 6f 67 72 61  n = u.cd.pProgra
26257 6d 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 75 2e  m->token;.    u.
26258 63 64 2e 70 46 72 61 6d 65 2d 3e 61 4f 6e 63 65  cd.pFrame->aOnce
26259 46 6c 61 67 20 3d 20 70 2d 3e 61 4f 6e 63 65 46  Flag = p->aOnceF
2625a 6c 61 67 3b 0a 20 20 20 20 75 2e 63 64 2e 70 46  lag;.    u.cd.pF
2625b 72 61 6d 65 2d 3e 6e 4f 6e 63 65 46 6c 61 67 20  rame->nOnceFlag 
2625c 3d 20 70 2d 3e 6e 4f 6e 63 65 46 6c 61 67 3b 0a  = p->nOnceFlag;.
2625d 0a 20 20 20 20 75 2e 63 64 2e 70 45 6e 64 20 3d  .    u.cd.pEnd =
2625e 20 26 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 75   &VdbeFrameMem(u
2625f 2e 63 64 2e 70 46 72 61 6d 65 29 5b 75 2e 63 64  .cd.pFrame)[u.cd
26260 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d  .pFrame->nChildM
26261 65 6d 5d 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63  em];.    for(u.c
26262 64 2e 70 4d 65 6d 3d 56 64 62 65 46 72 61 6d 65  d.pMem=VdbeFrame
26263 4d 65 6d 28 75 2e 63 64 2e 70 46 72 61 6d 65 29  Mem(u.cd.pFrame)
26264 3b 20 75 2e 63 64 2e 70 4d 65 6d 21 3d 75 2e 63  ; u.cd.pMem!=u.c
26265 64 2e 70 45 6e 64 3b 20 75 2e 63 64 2e 70 4d 65  d.pEnd; u.cd.pMe
26266 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e 63 64  m++){.      u.cd
26267 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d  .pMem->flags = M
26268 45 4d 5f 49 6e 76 61 6c 69 64 3b 0a 20 20 20 20  EM_Invalid;.    
26269 20 20 75 2e 63 64 2e 70 4d 65 6d 2d 3e 64 62 20    u.cd.pMem->db 
2626a 3d 20 64 62 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  = db;.    }.  }e
2626b 6c 73 65 7b 0a 20 20 20 20 75 2e 63 64 2e 70 46  lse{.    u.cd.pF
2626c 72 61 6d 65 20 3d 20 75 2e 63 64 2e 70 52 74 2d  rame = u.cd.pRt-
2626d 3e 75 2e 70 46 72 61 6d 65 3b 0a 20 20 20 20 61  >u.pFrame;.    a
2626e 73 73 65 72 74 28 20 75 2e 63 64 2e 70 50 72 6f  ssert( u.cd.pPro
2626f 67 72 61 6d 2d 3e 6e 4d 65 6d 2b 75 2e 63 64 2e  gram->nMem+u.cd.
26270 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3d 3d  pProgram->nCsr==
26271 75 2e 63 64 2e 70 46 72 61 6d 65 2d 3e 6e 43 68  u.cd.pFrame->nCh
26272 69 6c 64 4d 65 6d 20 29 3b 0a 20 20 20 20 61 73  ildMem );.    as
26273 73 65 72 74 28 20 75 2e 63 64 2e 70 50 72 6f 67  sert( u.cd.pProg
26274 72 61 6d 2d 3e 6e 43 73 72 3d 3d 75 2e 63 64 2e  ram->nCsr==u.cd.
26275 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 73  pFrame->nChildCs
26276 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  r );.    assert(
26277 20 70 63 3d 3d 75 2e 63 64 2e 70 46 72 61 6d 65   pc==u.cd.pFrame
26278 2d 3e 70 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 70  ->pc );.  }..  p
26279 2d 3e 6e 46 72 61 6d 65 2b 2b 3b 0a 20 20 75 2e  ->nFrame++;.  u.
2627a 63 64 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65  cd.pFrame->pPare
2627b 6e 74 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a  nt = p->pFrame;.
2627c 20 20 75 2e 63 64 2e 70 46 72 61 6d 65 2d 3e 6c    u.cd.pFrame->l
2627d 61 73 74 52 6f 77 69 64 20 3d 20 6c 61 73 74 52  astRowid = lastR
2627e 6f 77 69 64 3b 0a 20 20 75 2e 63 64 2e 70 46 72  owid;.  u.cd.pFr
2627f 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70  ame->nChange = p
26280 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 70 2d 3e  ->nChange;.  p->
26281 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70  nChange = 0;.  p
26282 2d 3e 70 46 72 61 6d 65 20 3d 20 75 2e 63 64 2e  ->pFrame = u.cd.
26283 70 46 72 61 6d 65 3b 0a 20 20 70 2d 3e 61 4d 65  pFrame;.  p->aMe
26284 6d 20 3d 20 61 4d 65 6d 20 3d 20 26 56 64 62 65  m = aMem = &Vdbe
26285 46 72 61 6d 65 4d 65 6d 28 75 2e 63 64 2e 70 46  FrameMem(u.cd.pF
26286 72 61 6d 65 29 5b 2d 31 5d 3b 0a 20 20 70 2d 3e  rame)[-1];.  p->
26287 6e 4d 65 6d 20 3d 20 75 2e 63 64 2e 70 46 72 61  nMem = u.cd.pFra
26288 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 3b 0a 20  me->nChildMem;. 
26289 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 28 75   p->nCursor = (u
2628a 31 36 29 75 2e 63 64 2e 70 46 72 61 6d 65 2d 3e  16)u.cd.pFrame->
2628b 6e 43 68 69 6c 64 43 73 72 3b 0a 20 20 70 2d 3e  nChildCsr;.  p->
2628c 61 70 43 73 72 20 3d 20 28 56 64 62 65 43 75 72  apCsr = (VdbeCur
2628d 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d 3e  sor **)&aMem[p->
2628e 6e 4d 65 6d 2b 31 5d 3b 0a 20 20 70 2d 3e 61 4f  nMem+1];.  p->aO
2628f 70 20 3d 20 61 4f 70 20 3d 20 75 2e 63 64 2e 70  p = aOp = u.cd.p
26290 50 72 6f 67 72 61 6d 2d 3e 61 4f 70 3b 0a 20 20  Program->aOp;.  
26291 70 2d 3e 6e 4f 70 20 3d 20 75 2e 63 64 2e 70 50  p->nOp = u.cd.pP
26292 72 6f 67 72 61 6d 2d 3e 6e 4f 70 3b 0a 20 20 70  rogram->nOp;.  p
26293 2d 3e 61 4f 6e 63 65 46 6c 61 67 20 3d 20 28 75  ->aOnceFlag = (u
26294 38 20 2a 29 26 70 2d 3e 61 70 43 73 72 5b 70 2d  8 *)&p->apCsr[p-
26295 3e 6e 43 75 72 73 6f 72 5d 3b 0a 20 20 70 2d 3e  >nCursor];.  p->
26296 6e 4f 6e 63 65 46 6c 61 67 20 3d 20 75 2e 63 64  nOnceFlag = u.cd
26297 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 6e 63 65  .pProgram->nOnce
26298 3b 0a 20 20 70 63 20 3d 20 2d 31 3b 0a 20 20 6d  ;.  pc = -1;.  m
26299 65 6d 73 65 74 28 70 2d 3e 61 4f 6e 63 65 46 6c  emset(p->aOnceFl
2629a 61 67 2c 20 30 2c 20 70 2d 3e 6e 4f 6e 63 65 46  ag, 0, p->nOnceF
2629b 6c 61 67 29 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a  lag);..  break;.
2629c 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61  }../* Opcode: Pa
2629d 72 61 6d 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ram P1 P2 * * *.
2629e 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64  **.** This opcod
2629f 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 70  e is only ever p
262a0 72 65 73 65 6e 74 20 69 6e 20 73 75 62 2d 70 72  resent in sub-pr
262a1 6f 67 72 61 6d 73 20 63 61 6c 6c 65 64 20 76 69  ograms called vi
262a2 61 20 74 68 65 20 0a 2a 2a 20 4f 50 5f 50 72 6f  a the .** OP_Pro
262a3 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e  gram instruction
262a4 2e 20 43 6f 70 79 20 61 20 76 61 6c 75 65 20 63  . Copy a value c
262a5 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20  urrently stored 
262a6 69 6e 20 61 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20  in a memory .** 
262a7 63 65 6c 6c 20 6f 66 20 74 68 65 20 63 61 6c 6c  cell of the call
262a8 69 6e 67 20 28 70 61 72 65 6e 74 29 20 66 72 61  ing (parent) fra
262a9 6d 65 20 74 6f 20 63 65 6c 6c 20 50 32 20 69 6e  me to cell P2 in
262aa 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 61   the current fra
262ab 6d 65 73 20 0a 2a 2a 20 61 64 64 72 65 73 73 20  mes .** address 
262ac 73 70 61 63 65 2e 20 54 68 69 73 20 69 73 20 75  space. This is u
262ad 73 65 64 20 62 79 20 74 72 69 67 67 65 72 20 70  sed by trigger p
262ae 72 6f 67 72 61 6d 73 20 74 6f 20 61 63 63 65 73  rograms to acces
262af 73 20 74 68 65 20 6e 65 77 2e 2a 20 0a 2a 2a 20  s the new.* .** 
262b0 61 6e 64 20 6f 6c 64 2e 2a 20 76 61 6c 75 65 73  and old.* values
262b1 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 64 64 72  ..**.** The addr
262b2 65 73 73 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  ess of the cell 
262b3 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 66 72  in the parent fr
262b4 61 6d 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65  ame is determine
262b5 64 20 62 79 20 61 64 64 69 6e 67 0a 2a 2a 20 74  d by adding.** t
262b6 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
262b7 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  P1 argument to t
262b8 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
262b9 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  P1 argument to t
262ba 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 4f 50  he.** calling OP
262bb 5f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63  _Program instruc
262bc 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  tion..*/.case OP
262bd 5f 50 61 72 61 6d 3a 20 7b 20 20 20 20 20 20 20  _Param: {       
262be 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
262bf 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20  elease */.#if 0 
262c0 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
262c1 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
262c2 2e 63 65 20 2a 2f 0a 20 20 56 64 62 65 46 72 61  .ce */.  VdbeFra
262c3 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 4d 65  me *pFrame;.  Me
262c4 6d 20 2a 70 49 6e 3b 0a 23 65 6e 64 69 66 20 2f  m *pIn;.#endif /
262c5 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
262c6 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
262c7 65 20 2a 2f 0a 20 20 75 2e 63 65 2e 70 46 72 61  e */.  u.ce.pFra
262c8 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a  me = p->pFrame;.
262c9 20 20 75 2e 63 65 2e 70 49 6e 20 3d 20 26 75 2e    u.ce.pIn = &u.
262ca 63 65 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b  ce.pFrame->aMem[
262cb 70 4f 70 2d 3e 70 31 20 2b 20 75 2e 63 65 2e 70  pOp->p1 + u.ce.p
262cc 46 72 61 6d 65 2d 3e 61 4f 70 5b 75 2e 63 65 2e  Frame->aOp[u.ce.
262cd 70 46 72 61 6d 65 2d 3e 70 63 5d 2e 70 31 5d 3b  pFrame->pc].p1];
262ce 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
262cf 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75  mShallowCopy(pOu
262d0 74 2c 20 75 2e 63 65 2e 70 49 6e 2c 20 4d 45 4d  t, u.ce.pIn, MEM
262d1 5f 45 70 68 65 6d 29 3b 0a 20 20 62 72 65 61 6b  _Ephem);.  break
262d2 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 23  ;.}..#endif /* #
262d3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
262d4 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 0a 23  IT_TRIGGER */..#
262d5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
262d6 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 2f  IT_FOREIGN_KEY./
262d7 2a 20 4f 70 63 6f 64 65 3a 20 46 6b 43 6f 75 6e  * Opcode: FkCoun
262d8 74 65 72 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ter P1 P2 * * *.
262d9 2a 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20  **.** Increment 
262da 61 20 22 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f  a "constraint co
262db 75 6e 74 65 72 22 20 62 79 20 50 32 20 28 50 32  unter" by P2 (P2
262dc 20 6d 61 79 20 62 65 20 6e 65 67 61 74 69 76 65   may be negative
262dd 20 6f 72 20 70 6f 73 69 74 69 76 65 29 2e 0a 2a   or positive)..*
262de 2a 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d 7a  * If P1 is non-z
262df 65 72 6f 2c 20 74 68 65 20 64 61 74 61 62 61 73  ero, the databas
262e0 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75  e constraint cou
262e1 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65 6e  nter is incremen
262e2 74 65 64 20 0a 2a 2a 20 28 64 65 66 65 72 72 65  ted .** (deferre
262e3 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  d foreign key co
262e4 6e 73 74 72 61 69 6e 74 73 29 2e 20 4f 74 68 65  nstraints). Othe
262e5 72 77 69 73 65 2c 20 69 66 20 50 31 20 69 73 20  rwise, if P1 is 
262e6 7a 65 72 6f 2c 20 74 68 65 20 0a 2a 2a 20 73 74  zero, the .** st
262e7 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 65 72 20  atement counter 
262e8 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28  is incremented (
262e9 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67  immediate foreig
262ea 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
262eb 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46  s)..*/.case OP_F
262ec 6b 43 6f 75 6e 74 65 72 3a 20 7b 0a 20 20 69 66  kCounter: {.  if
262ed 28 20 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20  ( pOp->p1 ){.   
262ee 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f   db->nDeferredCo
262ef 6e 73 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  ns += pOp->p2;. 
262f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 6e   }else{.    p->n
262f1 46 6b 43 6f 6e 73 74 72 61 69 6e 74 20 2b 3d 20  FkConstraint += 
262f2 70 4f 70 2d 3e 70 32 3b 0a 20 20 7d 0a 20 20 62  pOp->p2;.  }.  b
262f3 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
262f4 64 65 3a 20 46 6b 49 66 5a 65 72 6f 20 50 31 20  de: FkIfZero P1 
262f5 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54  P2 * * *.**.** T
262f6 68 69 73 20 6f 70 63 6f 64 65 20 74 65 73 74 73  his opcode tests
262f7 20 69 66 20 61 20 66 6f 72 65 69 67 6e 20 6b 65   if a foreign ke
262f8 79 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75  y constraint-cou
262f9 6e 74 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c  nter is currentl
262fa 79 20 7a 65 72 6f 2e 0a 2a 2a 20 49 66 20 73 6f  y zero..** If so
262fb 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 75  , jump to instru
262fc 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65 72 77  ction P2. Otherw
262fd 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  ise, fall throug
262fe 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 0a 2a  h to the next .*
262ff 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  * instruction..*
26300 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e 6f  *.** If P1 is no
26301 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  n-zero, then the
26302 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69   jump is taken i
26303 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  f the database c
26304 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65  onstraint-counte
26305 72 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 28 74 68  r.** is zero (th
26306 65 20 6f 6e 65 20 74 68 61 74 20 63 6f 75 6e 74  e one that count
26307 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74  s deferred const
26308 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 73  raint violations
26309 29 2e 20 49 66 20 50 31 20 69 73 0a 2a 2a 20 7a  ). If P1 is.** z
2630a 65 72 6f 2c 20 74 68 65 20 6a 75 6d 70 20 69 73  ero, the jump is
2630b 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 73 74   taken if the st
2630c 61 74 65 6d 65 6e 74 20 63 6f 6e 73 74 72 61 69  atement constrai
2630d 6e 74 2d 63 6f 75 6e 74 65 72 20 69 73 20 7a 65  nt-counter is ze
2630e 72 6f 0a 2a 2a 20 28 69 6d 6d 65 64 69 61 74 65  ro.** (immediate
2630f 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
26310 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f  straint violatio
26311 6e 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ns)..*/.case OP_
26312 46 6b 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20  FkIfZero: {     
26313 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20      /* jump */. 
26314 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 29 7b 0a   if( pOp->p1 ){.
26315 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 44 65 66      if( db->nDef
26316 65 72 72 65 64 43 6f 6e 73 3d 3d 30 20 29 20 70  erredCons==0 ) p
26317 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20  c = pOp->p2-1;. 
26318 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20   }else{.    if( 
26319 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74  p->nFkConstraint
2631a 3d 3d 30 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e  ==0 ) pc = pOp->
2631b 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61  p2-1;.  }.  brea
2631c 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 23  k;.}.#endif /* #
2631d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2631e 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 20 2a  IT_FOREIGN_KEY *
2631f 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
26320 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
26321 4d 45 4e 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  MENT./* Opcode: 
26322 4d 65 6d 4d 61 78 20 50 31 20 50 32 20 2a 20 2a  MemMax P1 P2 * *
26323 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61   *.**.** P1 is a
26324 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65   register in the
26325 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 74   root frame of t
26326 68 69 73 20 56 4d 20 28 74 68 65 20 72 6f 6f 74  his VM (the root
26327 20 66 72 61 6d 65 20 69 73 0a 2a 2a 20 64 69 66   frame is.** dif
26328 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20  ferent from the 
26329 63 75 72 72 65 6e 74 20 66 72 61 6d 65 20 69 66  current frame if
2632a 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
2632b 6e 20 69 73 20 62 65 69 6e 67 20 65 78 65 63 75  n is being execu
2632c 74 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20  ted.** within a 
2632d 73 75 62 2d 70 72 6f 67 72 61 6d 29 2e 20 53 65  sub-program). Se
2632e 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72  t the value of r
2632f 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 74 68  egister P1 to th
26330 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 0a 2a 2a  e maximum of .**
26331 20 69 74 73 20 63 75 72 72 65 6e 74 20 76 61 6c   its current val
26332 75 65 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65  ue and the value
26333 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e   in register P2.
26334 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74  .**.** This inst
26335 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73 20 61  ruction throws a
26336 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 6d  n error if the m
26337 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 6e 6f  emory cell is no
26338 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a 20 61  t initially.** a
26339 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61  n integer..*/.ca
2633a 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20 7b 20  se OP_MemMax: { 
2633b 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f         /* in2 */
2633c 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
2633d 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
2633e 20 69 6e 74 6f 20 75 2e 63 66 20 2a 2f 0a 20 20   into u.cf */.  
2633f 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 56 64 62  Mem *pIn1;.  Vdb
26340 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a  eFrame *pFrame;.
26341 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
26342 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
26343 69 6e 74 6f 20 75 2e 63 66 20 2a 2f 0a 20 20 69  into u.cf */.  i
26344 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29 7b 0a  f( p->pFrame ){.
26345 20 20 20 20 66 6f 72 28 75 2e 63 66 2e 70 46 72      for(u.cf.pFr
26346 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b 20 75  ame=p->pFrame; u
26347 2e 63 66 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72  .cf.pFrame->pPar
26348 65 6e 74 3b 20 75 2e 63 66 2e 70 46 72 61 6d 65  ent; u.cf.pFrame
26349 3d 75 2e 63 66 2e 70 46 72 61 6d 65 2d 3e 70 50  =u.cf.pFrame->pP
2634a 61 72 65 6e 74 29 3b 0a 20 20 20 20 75 2e 63 66  arent);.    u.cf
2634b 2e 70 49 6e 31 20 3d 20 26 75 2e 63 66 2e 70 46  .pIn1 = &u.cf.pF
2634c 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  rame->aMem[pOp->
2634d 70 31 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  p1];.  }else{.  
2634e 20 20 75 2e 63 66 2e 70 49 6e 31 20 3d 20 26 61    u.cf.pIn1 = &a
2634f 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
26350 7d 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 49  }.  assert( memI
26351 73 56 61 6c 69 64 28 75 2e 63 66 2e 70 49 6e 31  sValid(u.cf.pIn1
26352 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ) );.  sqlite3Vd
26353 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
26354 75 2e 63 66 2e 70 49 6e 31 29 3b 0a 20 20 70 49  u.cf.pIn1);.  pI
26355 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n2 = &aMem[pOp->
26356 70 32 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  p2];.  sqlite3Vd
26357 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
26358 70 49 6e 32 29 3b 0a 20 20 69 66 28 20 75 2e 63  pIn2);.  if( u.c
26359 66 2e 70 49 6e 31 2d 3e 75 2e 69 3c 70 49 6e 32  f.pIn1->u.i<pIn2
2635a 2d 3e 75 2e 69 29 7b 0a 20 20 20 20 75 2e 63 66  ->u.i){.    u.cf
2635b 2e 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 49 6e  .pIn1->u.i = pIn
2635c 32 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 62 72  2->u.i;.  }.  br
2635d 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
2635e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
2635f 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 2f  OINCREMENT */../
26360 2a 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f 73 20  * Opcode: IfPos 
26361 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
26362 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f  * If the value o
26363 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73  f register P1 is
26364 20 31 20 6f 72 20 67 72 65 61 74 65 72 2c 20 6a   1 or greater, j
26365 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a  ump to P2..**.**
26366 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74   It is illegal t
26367 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72  o use this instr
26368 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69  uction on a regi
26369 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a  ster that does.*
2636a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e  * not contain an
2636b 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73   integer.  An as
2636c 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69  sertion fault wi
2636d 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75  ll result if you
2636e 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50   try..*/.case OP
2636f 5f 49 66 50 6f 73 3a 20 7b 20 20 20 20 20 20 20  _IfPos: {       
26370 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f   /* jump, in1 */
26371 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b  .  pIn1 = &aMem[
26372 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
26373 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26  rt( pIn1->flags&
26374 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28  MEM_Int );.  if(
26375 20 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 7b 0a   pIn1->u.i>0 ){.
26376 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
26377 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
26378 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
26379 3a 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a 20  : IfNeg P1 P2 * 
2637a 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  * *.**.** If the
2637b 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74   value of regist
2637c 65 72 20 50 31 20 69 73 20 6c 65 73 73 20 74 68  er P1 is less th
2637d 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 74 6f  an zero, jump to
2637e 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69   P2. .**.** It i
2637f 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65  s illegal to use
26380 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
26381 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20  n on a register 
26382 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  that does.** not
26383 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65   contain an inte
26384 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69  ger.  An asserti
26385 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65  on fault will re
26386 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e  sult if you try.
26387 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 65  .*/.case OP_IfNe
26388 67 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  g: {        /* j
26389 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49  ump, in1 */.  pI
2638a 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n1 = &aMem[pOp->
2638b 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  p1];.  assert( p
2638c 49 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49  In1->flags&MEM_I
2638d 6e 74 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31  nt );.  if( pIn1
2638e 2d 3e 75 2e 69 3c 30 20 29 7b 0a 20 20 20 20 20  ->u.i<0 ){.     
2638f 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
26390 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
26391 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 5a  ../* Opcode: IfZ
26392 65 72 6f 20 50 31 20 50 32 20 50 33 20 2a 20 2a  ero P1 P2 P3 * *
26393 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 73  .**.** The regis
26394 74 65 72 20 50 31 20 6d 75 73 74 20 63 6f 6e 74  ter P1 must cont
26395 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20  ain an integer. 
26396 20 41 64 64 20 6c 69 74 65 72 61 6c 20 50 33 20   Add literal P3 
26397 74 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20  to the.** value 
26398 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 20  in register P1. 
26399 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69   If the result i
2639a 73 20 65 78 61 63 74 6c 79 20 30 2c 20 6a 75 6d  s exactly 0, jum
2639b 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20  p to P2. .**.** 
2639c 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f  It is illegal to
2639d 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 75   use this instru
2639e 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73  ction on a regis
2639f 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a  ter that does.**
263a0 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20   not contain an 
263a1 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73  integer.  An ass
263a2 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c  ertion fault wil
263a3 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20  l result if you 
263a4 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  try..*/.case OP_
263a5 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20 20 20  IfZero: {       
263a6 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f   /* jump, in1 */
263a7 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b  .  pIn1 = &aMem[
263a8 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
263a9 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26  rt( pIn1->flags&
263aa 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 70 49 6e  MEM_Int );.  pIn
263ab 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d 3e 70  1->u.i += pOp->p
263ac 33 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75  3;.  if( pIn1->u
263ad 2e 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 70 63  .i==0 ){.     pc
263ae 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
263af 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
263b0 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 53 74  /* Opcode: AggSt
263b1 65 70 20 2a 20 50 32 20 50 33 20 50 34 20 50 35  ep * P2 P3 P4 P5
263b2 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74  .**.** Execute t
263b3 68 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e  he step function
263b4 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74   for an aggregat
263b5 65 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e 63 74  e.  The.** funct
263b6 69 6f 6e 20 68 61 73 20 50 35 20 61 72 67 75 6d  ion has P5 argum
263b7 65 6e 74 73 2e 20 20 20 50 34 20 69 73 20 61 20  ents.   P4 is a 
263b8 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46  pointer to the F
263b9 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74  uncDef.** struct
263ba 75 72 65 20 74 68 61 74 20 73 70 65 63 69 66 69  ure that specifi
263bb 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  es the function.
263bc 20 20 55 73 65 20 72 65 67 69 73 74 65 72 0a 2a    Use register.*
263bd 2a 20 50 33 20 61 73 20 74 68 65 20 61 63 63 75  * P3 as the accu
263be 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  mulator..**.** T
263bf 68 65 20 50 35 20 61 72 67 75 6d 65 6e 74 73 20  he P5 arguments 
263c0 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72  are taken from r
263c1 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 69  egister P2 and i
263c2 74 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73  ts.** successors
263c3 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67  ..*/.case OP_Agg
263c4 53 74 65 70 3a 20 7b 0a 23 69 66 20 30 20 20 2f  Step: {.#if 0  /
263c5 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
263c6 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
263c7 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 0a 20 20  g */.  int n;.  
263c8 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d  int i;.  Mem *pM
263c9 65 6d 3b 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b  em;.  Mem *pRec;
263ca 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
263cb 78 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 65  xt ctx;.  sqlite
263cc 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b  3_value **apVal;
263cd 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
263ce 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
263cf 20 69 6e 74 6f 20 75 2e 63 67 20 2a 2f 0a 0a 20   into u.cg */.. 
263d0 20 75 2e 63 67 2e 6e 20 3d 20 70 4f 70 2d 3e 70   u.cg.n = pOp->p
263d1 35 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63  5;.  assert( u.c
263d2 67 2e 6e 3e 3d 30 20 29 3b 0a 20 20 75 2e 63 67  g.n>=0 );.  u.cg
263d3 2e 70 52 65 63 20 3d 20 26 61 4d 65 6d 5b 70 4f  .pRec = &aMem[pO
263d4 70 2d 3e 70 32 5d 3b 0a 20 20 75 2e 63 67 2e 61  p->p2];.  u.cg.a
263d5 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b  pVal = p->apArg;
263d6 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 67 2e  .  assert( u.cg.
263d7 61 70 56 61 6c 20 7c 7c 20 75 2e 63 67 2e 6e 3d  apVal || u.cg.n=
263d8 3d 30 20 29 3b 0a 20 20 66 6f 72 28 75 2e 63 67  =0 );.  for(u.cg
263d9 2e 69 3d 30 3b 20 75 2e 63 67 2e 69 3c 75 2e 63  .i=0; u.cg.i<u.c
263da 67 2e 6e 3b 20 75 2e 63 67 2e 69 2b 2b 2c 20 75  g.n; u.cg.i++, u
263db 2e 63 67 2e 70 52 65 63 2b 2b 29 7b 0a 20 20 20  .cg.pRec++){.   
263dc 20 61 73 73 65 72 74 28 20 6d 65 6d 49 73 56 61   assert( memIsVa
263dd 6c 69 64 28 75 2e 63 67 2e 70 52 65 63 29 20 29  lid(u.cg.pRec) )
263de 3b 0a 20 20 20 20 75 2e 63 67 2e 61 70 56 61 6c  ;.    u.cg.apVal
263df 5b 75 2e 63 67 2e 69 5d 20 3d 20 75 2e 63 67 2e  [u.cg.i] = u.cg.
263e0 70 52 65 63 3b 0a 20 20 20 20 6d 65 6d 41 62 6f  pRec;.    memAbo
263e1 75 74 54 6f 43 68 61 6e 67 65 28 70 2c 20 75 2e  utToChange(p, u.
263e2 63 67 2e 70 52 65 63 29 3b 0a 20 20 20 20 73 71  cg.pRec);.    sq
263e3 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72  lite3VdbeMemStor
263e4 65 54 79 70 65 28 75 2e 63 67 2e 70 52 65 63 29  eType(u.cg.pRec)
263e5 3b 0a 20 20 7d 0a 20 20 75 2e 63 67 2e 63 74 78  ;.  }.  u.cg.ctx
263e6 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34  .pFunc = pOp->p4
263e7 2e 70 46 75 6e 63 3b 0a 20 20 61 73 73 65 72 74  .pFunc;.  assert
263e8 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70  ( pOp->p3>0 && p
263e9 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p3<=p->nMem 
263ea 29 3b 0a 20 20 75 2e 63 67 2e 63 74 78 2e 70 4d  );.  u.cg.ctx.pM
263eb 65 6d 20 3d 20 75 2e 63 67 2e 70 4d 65 6d 20 3d  em = u.cg.pMem =
263ec 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b   &aMem[pOp->p3];
263ed 0a 20 20 75 2e 63 67 2e 70 4d 65 6d 2d 3e 6e 2b  .  u.cg.pMem->n+
263ee 2b 3b 0a 20 20 75 2e 63 67 2e 63 74 78 2e 73 2e  +;.  u.cg.ctx.s.
263ef 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
263f0 3b 0a 20 20 75 2e 63 67 2e 63 74 78 2e 73 2e 7a  ;.  u.cg.ctx.s.z
263f1 20 3d 20 30 3b 0a 20 20 75 2e 63 67 2e 63 74 78   = 0;.  u.cg.ctx
263f2 2e 73 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a  .s.zMalloc = 0;.
263f3 20 20 75 2e 63 67 2e 63 74 78 2e 73 2e 78 44 65    u.cg.ctx.s.xDe
263f4 6c 20 3d 20 30 3b 0a 20 20 75 2e 63 67 2e 63 74  l = 0;.  u.cg.ct
263f5 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 75  x.s.db = db;.  u
263f6 2e 63 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 20  .cg.ctx.isError 
263f7 3d 20 30 3b 0a 20 20 75 2e 63 67 2e 63 74 78 2e  = 0;.  u.cg.ctx.
263f8 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 75 2e 63  pColl = 0;.  u.c
263f9 67 2e 63 74 78 2e 73 6b 69 70 46 6c 61 67 20 3d  g.ctx.skipFlag =
263fa 20 30 3b 0a 20 20 69 66 28 20 75 2e 63 67 2e 63   0;.  if( u.cg.c
263fb 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20  tx.pFunc->flags 
263fc 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45  & SQLITE_FUNC_NE
263fd 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73  EDCOLL ){.    as
263fe 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f 70  sert( pOp>p->aOp
263ff 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
26400 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d 3d  pOp[-1].p4type==
26401 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20  P4_COLLSEQ );.  
26402 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31    assert( pOp[-1
26403 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c  ].opcode==OP_Col
26404 6c 53 65 71 20 29 3b 0a 20 20 20 20 75 2e 63 67  lSeq );.    u.cg
26405 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70  .ctx.pColl = pOp
26406 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20  [-1].p4.pColl;. 
26407 20 7d 0a 20 20 28 75 2e 63 67 2e 63 74 78 2e 70   }.  (u.cg.ctx.p
26408 46 75 6e 63 2d 3e 78 53 74 65 70 29 28 26 75 2e  Func->xStep)(&u.
26409 63 67 2e 63 74 78 2c 20 75 2e 63 67 2e 6e 2c 20  cg.ctx, u.cg.n, 
2640a 75 2e 63 67 2e 61 70 56 61 6c 29 3b 20 2f 2a 20  u.cg.apVal); /* 
2640b 49 4d 50 3a 20 52 2d 32 34 35 30 35 2d 32 33 32  IMP: R-24505-232
2640c 33 30 20 2a 2f 0a 20 20 69 66 28 20 75 2e 63 67  30 */.  if( u.cg
2640d 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a  .ctx.isError ){.
2640e 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
2640f 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
26410 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
26411 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26  te3_value_text(&
26412 75 2e 63 67 2e 63 74 78 2e 73 29 29 3b 0a 20 20  u.cg.ctx.s));.  
26413 20 20 72 63 20 3d 20 75 2e 63 67 2e 63 74 78 2e    rc = u.cg.ctx.
26414 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69  isError;.  }.  i
26415 66 28 20 75 2e 63 67 2e 63 74 78 2e 73 6b 69 70  f( u.cg.ctx.skip
26416 46 6c 61 67 20 29 7b 0a 20 20 20 20 61 73 73 65  Flag ){.    asse
26417 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f  rt( pOp[-1].opco
26418 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29  de==OP_CollSeq )
26419 3b 0a 20 20 20 20 75 2e 63 67 2e 69 20 3d 20 70  ;.    u.cg.i = p
2641a 4f 70 5b 2d 31 5d 2e 70 31 3b 0a 20 20 20 20 69  Op[-1].p1;.    i
2641b 66 28 20 75 2e 63 67 2e 69 20 29 20 73 71 6c 69  f( u.cg.i ) sqli
2641c 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74  te3VdbeMemSetInt
2641d 36 34 28 26 61 4d 65 6d 5b 75 2e 63 67 2e 69 5d  64(&aMem[u.cg.i]
2641e 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  , 1);.  }..  sql
2641f 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
26420 73 65 28 26 75 2e 63 67 2e 63 74 78 2e 73 29 3b  se(&u.cg.ctx.s);
26421 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ..  break;.}../*
26422 20 4f 70 63 6f 64 65 3a 20 41 67 67 46 69 6e 61   Opcode: AggFina
26423 6c 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a  l P1 P2 * P4 *.*
26424 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65  *.** Execute the
26425 20 66 69 6e 61 6c 69 7a 65 72 20 66 75 6e 63 74   finalizer funct
26426 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67 67 72 65  ion for an aggre
26427 67 61 74 65 2e 20 20 50 31 20 69 73 0a 2a 2a 20  gate.  P1 is.** 
26428 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74  the memory locat
26429 69 6f 6e 20 74 68 61 74 20 69 73 20 74 68 65 20  ion that is the 
2642a 61 63 63 75 6d 75 6c 61 74 6f 72 20 66 6f 72 20  accumulator for 
2642b 74 68 65 20 61 67 67 72 65 67 61 74 65 2e 0a 2a  the aggregate..*
2642c 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 6e  *.** P2 is the n
2642d 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
2642e 74 73 20 74 68 61 74 20 74 68 65 20 73 74 65 70  ts that the step
2642f 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20   function takes 
26430 61 6e 64 0a 2a 2a 20 50 34 20 69 73 20 61 20 70  and.** P4 is a p
26431 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75  ointer to the Fu
26432 6e 63 44 65 66 20 66 6f 72 20 74 68 69 73 20 66  ncDef for this f
26433 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 50 32  unction.  The P2
26434 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20  .** argument is 
26435 6e 6f 74 20 75 73 65 64 20 62 79 20 74 68 69 73  not used by this
26436 20 6f 70 63 6f 64 65 2e 20 20 49 74 20 69 73 20   opcode.  It is 
26437 6f 6e 6c 79 20 74 68 65 72 65 20 74 6f 20 64 69  only there to di
26438 73 61 6d 62 69 67 75 61 74 65 0a 2a 2a 20 66 75  sambiguate.** fu
26439 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e  nctions that can
2643a 20 74 61 6b 65 20 76 61 72 79 69 6e 67 20 6e 75   take varying nu
2643b 6d 62 65 72 73 20 6f 66 20 61 72 67 75 6d 65 6e  mbers of argumen
2643c 74 73 2e 20 20 54 68 65 0a 2a 2a 20 50 34 20 61  ts.  The.** P4 a
2643d 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e 6c 79 20  rgument is only 
2643e 6e 65 65 64 65 64 20 66 6f 72 20 74 68 65 20 64  needed for the d
2643f 65 67 65 6e 65 72 61 74 65 20 63 61 73 65 20 77  egenerate case w
26440 68 65 72 65 0a 2a 2a 20 74 68 65 20 73 74 65 70  here.** the step
26441 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 6e 6f   function was no
26442 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 61 6c  t previously cal
26443 6c 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  led..*/.case OP_
26444 41 67 67 46 69 6e 61 6c 3a 20 7b 0a 23 69 66 20  AggFinal: {.#if 
26445 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
26446 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
26447 20 75 2e 63 68 20 2a 2f 0a 20 20 4d 65 6d 20 2a   u.ch */.  Mem *
26448 70 4d 65 6d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  pMem;.#endif /* 
26449 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
2644a 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 68 20  moved into u.ch 
2644b 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
2644c 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p1>0 && pOp->p
2644d 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  1<=p->nMem );.  
2644e 75 2e 63 68 2e 70 4d 65 6d 20 3d 20 26 61 4d 65  u.ch.pMem = &aMe
2644f 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  m[pOp->p1];.  as
26450 73 65 72 74 28 20 28 75 2e 63 68 2e 70 4d 65 6d  sert( (u.ch.pMem
26451 2d 3e 66 6c 61 67 73 20 26 20 7e 28 4d 45 4d 5f  ->flags & ~(MEM_
26452 4e 75 6c 6c 7c 4d 45 4d 5f 41 67 67 29 29 3d 3d  Null|MEM_Agg))==
26453 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  0 );.  rc = sqli
26454 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69  te3VdbeMemFinali
26455 7a 65 28 75 2e 63 68 2e 70 4d 65 6d 2c 20 70 4f  ze(u.ch.pMem, pO
26456 70 2d 3e 70 34 2e 70 46 75 6e 63 29 3b 0a 20 20  p->p4.pFunc);.  
26457 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 73 71  if( rc ){.    sq
26458 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
26459 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
2645a 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 76 61  "%s", sqlite3_va
2645b 6c 75 65 5f 74 65 78 74 28 75 2e 63 68 2e 70 4d  lue_text(u.ch.pM
2645c 65 6d 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  em));.  }.  sqli
2645d 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
2645e 6f 64 69 6e 67 28 75 2e 63 68 2e 70 4d 65 6d 2c  oding(u.ch.pMem,
2645f 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 55 50   encoding);.  UP
26460 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
26461 45 28 75 2e 63 68 2e 70 4d 65 6d 29 3b 0a 20 20  E(u.ch.pMem);.  
26462 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
26463 65 6d 54 6f 6f 42 69 67 28 75 2e 63 68 2e 70 4d  emTooBig(u.ch.pM
26464 65 6d 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  em) ){.    goto 
26465 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 62  too_big;.  }.  b
26466 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  reak;.}..#ifndef
26467 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c   SQLITE_OMIT_WAL
26468 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 68 65 63  ./* Opcode: Chec
26469 6b 70 6f 69 6e 74 20 50 31 20 50 32 20 50 33 20  kpoint P1 P2 P3 
2646a 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 70  * *.**.** Checkp
2646b 6f 69 6e 74 20 64 61 74 61 62 61 73 65 20 50 31  oint database P1
2646c 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  . This is a no-o
2646d 70 20 69 66 20 50 31 20 69 73 20 6e 6f 74 20 63  p if P1 is not c
2646e 75 72 72 65 6e 74 6c 79 20 69 6e 0a 2a 2a 20 57  urrently in.** W
2646f 41 4c 20 6d 6f 64 65 2e 20 50 61 72 61 6d 65 74  AL mode. Paramet
26470 65 72 20 50 32 20 69 73 20 6f 6e 65 20 6f 66 20  er P2 is one of 
26471 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e  SQLITE_CHECKPOIN
26472 54 5f 50 41 53 53 49 56 45 2c 20 46 55 4c 4c 0a  T_PASSIVE, FULL.
26473 2a 2a 20 6f 72 20 52 45 53 54 41 52 54 2e 20 20  ** or RESTART.  
26474 57 72 69 74 65 20 31 20 6f 72 20 30 20 69 6e 74  Write 1 or 0 int
26475 6f 20 6d 65 6d 5b 50 33 5d 20 69 66 20 74 68 65  o mem[P3] if the
26476 20 63 68 65 63 6b 70 6f 69 6e 74 20 72 65 74 75   checkpoint retu
26477 72 6e 73 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55  rns.** SQLITE_BU
26478 53 59 20 6f 72 20 6e 6f 74 2c 20 72 65 73 70 65  SY or not, respe
26479 63 74 69 76 65 6c 79 2e 20 20 57 72 69 74 65 20  ctively.  Write 
2647a 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
2647b 67 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 57 41  ges in the.** WA
2647c 4c 20 61 66 74 65 72 20 74 68 65 20 63 68 65 63  L after the chec
2647d 6b 70 6f 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 5b  kpoint into mem[
2647e 50 33 2b 31 5d 20 61 6e 64 20 74 68 65 20 6e 75  P3+1] and the nu
2647f 6d 62 65 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a  mber of pages.**
26480 20 69 6e 20 74 68 65 20 57 41 4c 20 74 68 61 74   in the WAL that
26481 20 68 61 76 65 20 62 65 65 6e 20 63 68 65 63 6b   have been check
26482 70 6f 69 6e 74 65 64 20 61 66 74 65 72 20 74 68  pointed after th
26483 65 20 63 68 65 63 6b 70 6f 69 6e 74 0a 2a 2a 20  e checkpoint.** 
26484 63 6f 6d 70 6c 65 74 65 73 20 69 6e 74 6f 20 6d  completes into m
26485 65 6d 5b 50 33 2b 32 5d 2e 20 20 48 6f 77 65 76  em[P3+2].  Howev
26486 65 72 20 6f 6e 20 61 6e 20 65 72 72 6f 72 2c 20  er on an error, 
26487 6d 65 6d 5b 50 33 2b 31 5d 20 61 6e 64 0a 2a 2a  mem[P3+1] and.**
26488 20 6d 65 6d 5b 50 33 2b 32 5d 20 61 72 65 20 69   mem[P3+2] are i
26489 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 2d 31  nitialized to -1
2648a 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 68 65  ..*/.case OP_Che
2648b 63 6b 70 6f 69 6e 74 3a 20 7b 0a 23 69 66 20 30  ckpoint: {.#if 0
2648c 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
2648d 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
2648e 75 2e 63 69 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  u.ci */.  int i;
2648f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26490 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
26491 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
26492 6e 74 20 61 52 65 73 5b 33 5d 3b 20 20 20 20 20  nt aRes[3];     
26493 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26494 2a 20 52 65 73 75 6c 74 73 20 2a 2f 0a 20 20 4d  * Results */.  M
26495 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20  em *pMem;       
26496 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26497 2a 20 57 72 69 74 65 20 72 65 73 75 6c 74 73 20  * Write results 
26498 68 65 72 65 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  here */.#endif /
26499 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
2649a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
2649b 69 20 2a 2f 0a 0a 20 20 75 2e 63 69 2e 61 52 65  i */..  u.ci.aRe
2649c 73 5b 30 5d 20 3d 20 30 3b 0a 20 20 75 2e 63 69  s[0] = 0;.  u.ci
2649d 2e 61 52 65 73 5b 31 5d 20 3d 20 75 2e 63 69 2e  .aRes[1] = u.ci.
2649e 61 52 65 73 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20  aRes[2] = -1;.  
2649f 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3d  assert( pOp->p2=
264a0 3d 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49  =SQLITE_CHECKPOI
264a1 4e 54 5f 50 41 53 53 49 56 45 0a 20 20 20 20 20  NT_PASSIVE.     
264a2 20 20 7c 7c 20 70 4f 70 2d 3e 70 32 3d 3d 53 51    || pOp->p2==SQ
264a3 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f  LITE_CHECKPOINT_
264a4 46 55 4c 4c 0a 20 20 20 20 20 20 20 7c 7c 20 70  FULL.       || p
264a5 4f 70 2d 3e 70 32 3d 3d 53 51 4c 49 54 45 5f 43  Op->p2==SQLITE_C
264a6 48 45 43 4b 50 4f 49 4e 54 5f 52 45 53 54 41 52  HECKPOINT_RESTAR
264a7 54 0a 20 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  T.  );.  rc = sq
264a8 6c 69 74 65 33 43 68 65 63 6b 70 6f 69 6e 74 28  lite3Checkpoint(
264a9 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70  db, pOp->p1, pOp
264aa 2d 3e 70 32 2c 20 26 75 2e 63 69 2e 61 52 65 73  ->p2, &u.ci.aRes
264ab 5b 31 5d 2c 20 26 75 2e 63 69 2e 61 52 65 73 5b  [1], &u.ci.aRes[
264ac 32 5d 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  2]);.  if( rc==S
264ad 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20  QLITE_BUSY ){.  
264ae 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
264af 3b 0a 20 20 20 20 75 2e 63 69 2e 61 52 65 73 5b  ;.    u.ci.aRes[
264b0 30 5d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 66 6f  0] = 1;.  }.  fo
264b1 72 28 75 2e 63 69 2e 69 3d 30 2c 20 75 2e 63 69  r(u.ci.i=0, u.ci
264b2 2e 70 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f  .pMem = &aMem[pO
264b3 70 2d 3e 70 33 5d 3b 20 75 2e 63 69 2e 69 3c 33  p->p3]; u.ci.i<3
264b4 3b 20 75 2e 63 69 2e 69 2b 2b 2c 20 75 2e 63 69  ; u.ci.i++, u.ci
264b5 2e 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71  .pMem++){.    sq
264b6 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49  lite3VdbeMemSetI
264b7 6e 74 36 34 28 75 2e 63 69 2e 70 4d 65 6d 2c 20  nt64(u.ci.pMem, 
264b8 28 69 36 34 29 75 2e 63 69 2e 61 52 65 73 5b 75  (i64)u.ci.aRes[u
264b9 2e 63 69 2e 69 5d 29 3b 0a 20 20 7d 0a 20 20 62  .ci.i]);.  }.  b
264ba 72 65 61 6b 3b 0a 7d 3b 20 20 0a 23 65 6e 64 69  reak;.};  .#endi
264bb 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
264bc 45 5f 4f 4d 49 54 5f 50 52 41 47 4d 41 0a 2f 2a  E_OMIT_PRAGMA./*
264bd 20 4f 70 63 6f 64 65 3a 20 4a 6f 75 72 6e 61 6c   Opcode: Journal
264be 4d 6f 64 65 20 50 31 20 50 32 20 50 33 20 2a 20  Mode P1 P2 P3 * 
264bf 50 35 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  P5.**.** Change 
264c0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65  the journal mode
264c1 20 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 20   of database P1 
264c2 74 6f 20 50 33 2e 20 50 33 20 6d 75 73 74 20 62  to P3. P3 must b
264c3 65 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20  e one of the.** 
264c4 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
264c5 45 5f 58 58 58 20 76 61 6c 75 65 73 2e 20 49 66  E_XXX values. If
264c6 20 63 68 61 6e 67 69 6e 67 20 62 65 74 77 65 65   changing betwee
264c7 6e 20 74 68 65 20 76 61 72 69 6f 75 73 20 72 6f  n the various ro
264c8 6c 6c 62 61 63 6b 0a 2a 2a 20 6d 6f 64 65 73 20  llback.** modes 
264c9 28 64 65 6c 65 74 65 2c 20 74 72 75 6e 63 61 74  (delete, truncat
264ca 65 2c 20 70 65 72 73 69 73 74 2c 20 6f 66 66 20  e, persist, off 
264cb 61 6e 64 20 6d 65 6d 6f 72 79 29 2c 20 74 68 69  and memory), thi
264cc 73 20 69 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a  s is a simple.**
264cd 20 6f 70 65 72 61 74 69 6f 6e 2e 20 4e 6f 20 49   operation. No I
264ce 4f 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a  O is required..*
264cf 2a 0a 2a 2a 20 49 66 20 63 68 61 6e 67 69 6e 67  *.** If changing
264d0 20 69 6e 74 6f 20 6f 72 20 6f 75 74 20 6f 66 20   into or out of 
264d1 57 41 4c 20 6d 6f 64 65 20 74 68 65 20 70 72 6f  WAL mode the pro
264d2 63 65 64 75 72 65 20 69 73 20 6d 6f 72 65 20 63  cedure is more c
264d3 6f 6d 70 6c 69 63 61 74 65 64 2e 0a 2a 2a 0a 2a  omplicated..**.*
264d4 2a 20 57 72 69 74 65 20 61 20 73 74 72 69 6e 67  * Write a string
264d5 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
264d6 66 69 6e 61 6c 20 6a 6f 75 72 6e 61 6c 2d 6d 6f  final journal-mo
264d7 64 65 20 74 6f 20 72 65 67 69 73 74 65 72 20 50  de to register P
264d8 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4a 6f  2..*/.case OP_Jo
264d9 75 72 6e 61 6c 4d 6f 64 65 3a 20 7b 20 20 20 20  urnalMode: {    
264da 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
264db 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  se */.#if 0  /* 
264dc 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
264dd 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20  moved into u.cj 
264de 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b  */.  Btree *pBt;
264df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
264e0 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 74 6f       /* Btree to
264e1 20 63 68 61 6e 67 65 20 6a 6f 75 72 6e 61 6c 20   change journal 
264e2 6d 6f 64 65 20 6f 66 20 2a 2f 0a 20 20 50 61 67  mode of */.  Pag
264e3 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20  er *pPager;     
264e4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
264e5 50 61 67 65 72 20 61 73 73 6f 63 69 61 74 65 64  Pager associated
264e6 20 77 69 74 68 20 70 42 74 20 2a 2f 0a 20 20 69   with pBt */.  i
264e7 6e 74 20 65 4e 65 77 3b 20 20 20 20 20 20 20 20  nt eNew;        
264e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
264e9 2a 20 4e 65 77 20 6a 6f 75 72 6e 61 6c 20 6d 6f  * New journal mo
264ea 64 65 20 2a 2f 0a 20 20 69 6e 74 20 65 4f 6c 64  de */.  int eOld
264eb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
264ec 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f          /* The o
264ed 6c 64 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20  ld journal mode 
264ee 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
264ef 45 5f 4f 4d 49 54 5f 57 41 4c 0a 20 20 63 6f 6e  E_OMIT_WAL.  con
264f0 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
264f1 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  me;          /* 
264f2 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65  Name of database
264f3 20 66 69 6c 65 20 66 6f 72 20 70 50 61 67 65 72   file for pPager
264f4 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69   */.#endif.#endi
264f5 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
264f6 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
264f7 75 2e 63 6a 20 2a 2f 0a 0a 20 20 75 2e 63 6a 2e  u.cj */..  u.cj.
264f8 65 4e 65 77 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a  eNew = pOp->p3;.
264f9 20 20 61 73 73 65 72 74 28 20 75 2e 63 6a 2e 65    assert( u.cj.e
264fa 4e 65 77 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  New==PAGER_JOURN
264fb 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 20 20  ALMODE_DELETE.  
264fc 20 20 20 20 20 7c 7c 20 75 2e 63 6a 2e 65 4e 65       || u.cj.eNe
264fd 77 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  w==PAGER_JOURNAL
264fe 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 20 20  MODE_TRUNCATE.  
264ff 20 20 20 20 20 7c 7c 20 75 2e 63 6a 2e 65 4e 65       || u.cj.eNe
26500 77 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  w==PAGER_JOURNAL
26501 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 20  MODE_PERSIST.   
26502 20 20 20 20 7c 7c 20 75 2e 63 6a 2e 65 4e 65 77      || u.cj.eNew
26503 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
26504 4f 44 45 5f 4f 46 46 0a 20 20 20 20 20 20 20 7c  ODE_OFF.       |
26505 7c 20 75 2e 63 6a 2e 65 4e 65 77 3d 3d 50 41 47  | u.cj.eNew==PAG
26506 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d  ER_JOURNALMODE_M
26507 45 4d 4f 52 59 0a 20 20 20 20 20 20 20 7c 7c 20  EMORY.       || 
26508 75 2e 63 6a 2e 65 4e 65 77 3d 3d 50 41 47 45 52  u.cj.eNew==PAGER
26509 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c  _JOURNALMODE_WAL
2650a 0a 20 20 20 20 20 20 20 7c 7c 20 75 2e 63 6a 2e  .       || u.cj.
2650b 65 4e 65 77 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  eNew==PAGER_JOUR
2650c 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 20 20  NALMODE_QUERY.  
2650d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
2650e 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
2650f 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20  p1<db->nDb );.. 
26510 20 75 2e 63 6a 2e 70 42 74 20 3d 20 64 62 2d 3e   u.cj.pBt = db->
26511 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74  aDb[pOp->p1].pBt
26512 3b 0a 20 20 75 2e 63 6a 2e 70 50 61 67 65 72 20  ;.  u.cj.pPager 
26513 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  = sqlite3BtreePa
26514 67 65 72 28 75 2e 63 6a 2e 70 42 74 29 3b 0a 20  ger(u.cj.pBt);. 
26515 20 75 2e 63 6a 2e 65 4f 6c 64 20 3d 20 73 71 6c   u.cj.eOld = sql
26516 69 74 65 33 50 61 67 65 72 47 65 74 4a 6f 75 72  ite3PagerGetJour
26517 6e 61 6c 4d 6f 64 65 28 75 2e 63 6a 2e 70 50 61  nalMode(u.cj.pPa
26518 67 65 72 29 3b 0a 20 20 69 66 28 20 75 2e 63 6a  ger);.  if( u.cj
26519 2e 65 4e 65 77 3d 3d 50 41 47 45 52 5f 4a 4f 55  .eNew==PAGER_JOU
2651a 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20 29  RNALMODE_QUERY )
2651b 20 75 2e 63 6a 2e 65 4e 65 77 20 3d 20 75 2e 63   u.cj.eNew = u.c
2651c 6a 2e 65 4f 6c 64 3b 0a 20 20 69 66 28 20 21 73  j.eOld;.  if( !s
2651d 71 6c 69 74 65 33 50 61 67 65 72 4f 6b 54 6f 43  qlite3PagerOkToC
2651e 68 61 6e 67 65 4a 6f 75 72 6e 61 6c 4d 6f 64 65  hangeJournalMode
2651f 28 75 2e 63 6a 2e 70 50 61 67 65 72 29 20 29 20  (u.cj.pPager) ) 
26520 75 2e 63 6a 2e 65 4e 65 77 20 3d 20 75 2e 63 6a  u.cj.eNew = u.cj
26521 2e 65 4f 6c 64 3b 0a 0a 23 69 66 6e 64 65 66 20  .eOld;..#ifndef 
26522 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a  SQLITE_OMIT_WAL.
26523 20 20 75 2e 63 6a 2e 7a 46 69 6c 65 6e 61 6d 65    u.cj.zFilename
26524 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46   = sqlite3PagerF
26525 69 6c 65 6e 61 6d 65 28 75 2e 63 6a 2e 70 50 61  ilename(u.cj.pPa
26526 67 65 72 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 44  ger, 1);..  /* D
26527 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 74 72  o not allow a tr
26528 61 6e 73 69 74 69 6f 6e 20 74 6f 20 6a 6f 75 72  ansition to jour
26529 6e 61 6c 5f 6d 6f 64 65 3d 57 41 4c 20 66 6f 72  nal_mode=WAL for
2652a 20 61 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a   a database.  **
2652b 20 69 6e 20 74 65 6d 70 6f 72 61 72 79 20 73 74   in temporary st
2652c 6f 72 61 67 65 20 6f 72 20 69 66 20 74 68 65 20  orage or if the 
2652d 56 46 53 20 64 6f 65 73 20 6e 6f 74 20 73 75 70  VFS does not sup
2652e 70 6f 72 74 20 73 68 61 72 65 64 20 6d 65 6d 6f  port shared memo
2652f 72 79 0a 20 20 2a 2f 0a 20 20 69 66 28 20 75 2e  ry.  */.  if( u.
26530 63 6a 2e 65 4e 65 77 3d 3d 50 41 47 45 52 5f 4a  cj.eNew==PAGER_J
26531 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 0a 20  OURNALMODE_WAL. 
26532 20 20 26 26 20 28 73 71 6c 69 74 65 33 53 74 72    && (sqlite3Str
26533 6c 65 6e 33 30 28 75 2e 63 6a 2e 7a 46 69 6c 65  len30(u.cj.zFile
26534 6e 61 6d 65 29 3d 3d 30 20 20 20 20 20 20 20 20  name)==0        
26535 20 20 20 2f 2a 20 54 65 6d 70 20 66 69 6c 65 20     /* Temp file 
26536 2a 2f 0a 20 20 20 20 20 20 20 7c 7c 20 21 73 71  */.       || !sq
26537 6c 69 74 65 33 50 61 67 65 72 57 61 6c 53 75 70  lite3PagerWalSup
26538 70 6f 72 74 65 64 28 75 2e 63 6a 2e 70 50 61 67  ported(u.cj.pPag
26539 65 72 29 29 20 20 20 2f 2a 20 4e 6f 20 73 68 61  er))   /* No sha
2653a 72 65 64 2d 6d 65 6d 6f 72 79 20 73 75 70 70 6f  red-memory suppo
2653b 72 74 20 2a 2f 0a 20 20 29 7b 0a 20 20 20 20 75  rt */.  ){.    u
2653c 2e 63 6a 2e 65 4e 65 77 20 3d 20 75 2e 63 6a 2e  .cj.eNew = u.cj.
2653d 65 4f 6c 64 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  eOld;.  }..  if(
2653e 20 28 75 2e 63 6a 2e 65 4e 65 77 21 3d 75 2e 63   (u.cj.eNew!=u.c
2653f 6a 2e 65 4f 6c 64 29 0a 20 20 20 26 26 20 28 75  j.eOld).   && (u
26540 2e 63 6a 2e 65 4f 6c 64 3d 3d 50 41 47 45 52 5f  .cj.eOld==PAGER_
26541 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20  JOURNALMODE_WAL 
26542 7c 7c 20 75 2e 63 6a 2e 65 4e 65 77 3d 3d 50 41  || u.cj.eNew==PA
26543 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
26544 57 41 4c 29 0a 20 20 29 7b 0a 20 20 20 20 69 66  WAL).  ){.    if
26545 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  ( !db->autoCommi
26546 74 20 7c 7c 20 64 62 2d 3e 61 63 74 69 76 65 56  t || db->activeV
26547 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20  dbeCnt>1 ){.    
26548 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
26549 52 4f 52 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ROR;.      sqlit
2654a 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
2654b 7a 45 72 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20  zErrMsg, db,.   
2654c 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 63         "cannot c
2654d 68 61 6e 67 65 20 25 73 20 77 61 6c 20 6d 6f 64  hange %s wal mod
2654e 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20  e from within a 
2654f 74 72 61 6e 73 61 63 74 69 6f 6e 22 2c 0a 20 20  transaction",.  
26550 20 20 20 20 20 20 20 20 28 75 2e 63 6a 2e 65 4e          (u.cj.eN
26551 65 77 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  ew==PAGER_JOURNA
26552 4c 4d 4f 44 45 5f 57 41 4c 20 3f 20 22 69 6e 74  LMODE_WAL ? "int
26553 6f 22 20 3a 20 22 6f 75 74 20 6f 66 22 29 0a 20  o" : "out of"). 
26554 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 62 72       );.      br
26555 65 61 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  eak;.    }else{.
26556 0a 20 20 20 20 20 20 69 66 28 20 75 2e 63 6a 2e  .      if( u.cj.
26557 65 4f 6c 64 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  eOld==PAGER_JOUR
26558 4e 41 4c 4d 4f 44 45 5f 57 41 4c 20 29 7b 0a 20  NALMODE_WAL ){. 
26559 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6c 65 61         /* If lea
2655a 76 69 6e 67 20 57 41 4c 20 6d 6f 64 65 2c 20 63  ving WAL mode, c
2655b 6c 6f 73 65 20 74 68 65 20 6c 6f 67 20 66 69 6c  lose the log fil
2655c 65 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  e. If successful
2655d 2c 20 74 68 65 20 63 61 6c 6c 0a 20 20 20 20 20  , the call.     
2655e 20 20 20 2a 2a 20 74 6f 20 50 61 67 65 72 43 6c     ** to PagerCl
2655f 6f 73 65 57 61 6c 28 29 20 63 68 65 63 6b 70 6f  oseWal() checkpo
26560 69 6e 74 73 20 61 6e 64 20 64 65 6c 65 74 65 73  ints and deletes
26561 20 74 68 65 20 77 72 69 74 65 2d 61 68 65 61 64   the write-ahead
26562 2d 6c 6f 67 0a 20 20 20 20 20 20 20 20 2a 2a 20  -log.        ** 
26563 66 69 6c 65 2e 20 41 6e 20 45 58 43 4c 55 53 49  file. An EXCLUSI
26564 56 45 20 6c 6f 63 6b 20 6d 61 79 20 73 74 69 6c  VE lock may stil
26565 6c 20 62 65 20 68 65 6c 64 20 6f 6e 20 74 68 65  l be held on the
26566 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20   database file. 
26567 20 20 20 20 20 20 20 2a 2a 20 61 66 74 65 72 20         ** after 
26568 61 20 73 75 63 63 65 73 73 66 75 6c 20 72 65 74  a successful ret
26569 75 72 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  urn..        */.
2656a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
2656b 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 57 61  ite3PagerCloseWa
2656c 6c 28 75 2e 63 6a 2e 70 50 61 67 65 72 29 3b 0a  l(u.cj.pPager);.
2656d 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
2656e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2656f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
26570 67 65 72 53 65 74 4a 6f 75 72 6e 61 6c 4d 6f 64  gerSetJournalMod
26571 65 28 75 2e 63 6a 2e 70 50 61 67 65 72 2c 20 75  e(u.cj.pPager, u
26572 2e 63 6a 2e 65 4e 65 77 29 3b 0a 20 20 20 20 20  .cj.eNew);.     
26573 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
26574 20 69 66 28 20 75 2e 63 6a 2e 65 4f 6c 64 3d 3d   if( u.cj.eOld==
26575 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
26576 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20  E_MEMORY ){.    
26577 20 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 74 72      /* Cannot tr
26578 61 6e 73 69 74 69 6f 6e 20 64 69 72 65 63 74 6c  ansition directl
26579 79 20 66 72 6f 6d 20 4d 45 4d 4f 52 59 20 74 6f  y from MEMORY to
2657a 20 57 41 4c 2e 20 20 55 73 65 20 6d 6f 64 65 20   WAL.  Use mode 
2657b 4f 46 46 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  OFF.        ** a
2657c 73 20 61 6e 20 69 6e 74 65 72 6d 65 64 69 61 74  s an intermediat
2657d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c  e */.        sql
2657e 69 74 65 33 50 61 67 65 72 53 65 74 4a 6f 75 72  ite3PagerSetJour
2657f 6e 61 6c 4d 6f 64 65 28 75 2e 63 6a 2e 70 50 61  nalMode(u.cj.pPa
26580 67 65 72 2c 20 50 41 47 45 52 5f 4a 4f 55 52 4e  ger, PAGER_JOURN
26581 41 4c 4d 4f 44 45 5f 4f 46 46 29 3b 0a 20 20 20  ALMODE_OFF);.   
26582 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 4f     }..      /* O
26583 70 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f  pen a transactio
26584 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  n on the databas
26585 65 20 66 69 6c 65 2e 20 52 65 67 61 72 64 6c 65  e file. Regardle
26586 73 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ss of the journa
26587 6c 0a 20 20 20 20 20 20 2a 2a 20 6d 6f 64 65 2c  l.      ** mode,
26588 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f   this transactio
26589 6e 20 61 6c 77 61 79 73 20 75 73 65 73 20 61 20  n always uses a 
2658a 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
2658b 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2658c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
2658d 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 75  BtreeIsInTrans(u
2658e 2e 63 6a 2e 70 42 74 29 3d 3d 30 20 29 3b 0a 20  .cj.pBt)==0 );. 
2658f 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
26590 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
26591 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
26592 72 65 65 53 65 74 56 65 72 73 69 6f 6e 28 75 2e  reeSetVersion(u.
26593 63 6a 2e 70 42 74 2c 20 28 75 2e 63 6a 2e 65 4e  cj.pBt, (u.cj.eN
26594 65 77 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  ew==PAGER_JOURNA
26595 4c 4d 4f 44 45 5f 57 41 4c 20 3f 20 32 20 3a 20  LMODE_WAL ? 2 : 
26596 31 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  1));.      }.   
26597 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a   }.  }.#endif /*
26598 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f   ifndef SQLITE_O
26599 4d 49 54 5f 57 41 4c 20 2a 2f 0a 0a 20 20 69 66  MIT_WAL */..  if
2659a 28 20 72 63 20 29 7b 0a 20 20 20 20 75 2e 63 6a  ( rc ){.    u.cj
2659b 2e 65 4e 65 77 20 3d 20 75 2e 63 6a 2e 65 4f 6c  .eNew = u.cj.eOl
2659c 64 3b 0a 20 20 7d 0a 20 20 75 2e 63 6a 2e 65 4e  d;.  }.  u.cj.eN
2659d 65 77 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  ew = sqlite3Page
2659e 72 53 65 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28  rSetJournalMode(
2659f 75 2e 63 6a 2e 70 50 61 67 65 72 2c 20 75 2e 63  u.cj.pPager, u.c
265a0 6a 2e 65 4e 65 77 29 3b 0a 0a 20 20 70 4f 75 74  j.eNew);..  pOut
265a1 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32   = &aMem[pOp->p2
265a2 5d 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  ];.  pOut->flags
265a3 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53   = MEM_Str|MEM_S
265a4 74 61 74 69 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  tatic|MEM_Term;.
265a5 20 20 70 4f 75 74 2d 3e 7a 20 3d 20 28 63 68 61    pOut->z = (cha
265a6 72 20 2a 29 73 71 6c 69 74 65 33 4a 6f 75 72 6e  r *)sqlite3Journ
265a7 61 6c 4d 6f 64 65 6e 61 6d 65 28 75 2e 63 6a 2e  alModename(u.cj.
265a8 65 4e 65 77 29 3b 0a 20 20 70 4f 75 74 2d 3e 6e  eNew);.  pOut->n
265a9 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
265aa 33 30 28 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 70  30(pOut->z);.  p
265ab 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Out->enc = SQLIT
265ac 45 5f 55 54 46 38 3b 0a 20 20 73 71 6c 69 74 65  E_UTF8;.  sqlite
265ad 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
265ae 69 6e 67 28 70 4f 75 74 2c 20 65 6e 63 6f 64 69  ing(pOut, encodi
265af 6e 67 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 3b  ng);.  break;.};
265b0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
265b1 45 5f 4f 4d 49 54 5f 50 52 41 47 4d 41 20 2a 2f  E_OMIT_PRAGMA */
265b2 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
265b3 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
265b4 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  M) && !defined(S
265b5 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43  QLITE_OMIT_ATTAC
265b6 48 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61  H)./* Opcode: Va
265b7 63 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a  cuum * * * * *.*
265b8 2a 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20  *.** Vacuum the 
265b9 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e  entire database.
265ba 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69    This opcode wi
265bb 6c 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76  ll cause other v
265bc 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e  irtual.** machin
265bd 65 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64  es to be created
265be 20 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61   and run.  It ma
265bf 79 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20  y not be called 
265c0 66 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61  from within.** a
265c1 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f   transaction..*/
265c2 0a 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a  .case OP_Vacuum:
265c3 20 7b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   {.  rc = sqlite
265c4 33 52 75 6e 56 61 63 75 75 6d 28 26 70 2d 3e 7a  3RunVacuum(&p->z
265c5 45 72 72 4d 73 67 2c 20 64 62 29 3b 0a 20 20 62  ErrMsg, db);.  b
265c6 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  reak;.}.#endif..
265c7 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
265c8 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
265c9 55 55 4d 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  UUM)./* Opcode: 
265ca 49 6e 63 72 56 61 63 75 75 6d 20 50 31 20 50 32  IncrVacuum P1 P2
265cb 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72   * * *.**.** Per
265cc 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74  form a single st
265cd 65 70 20 6f 66 20 74 68 65 20 69 6e 63 72 65 6d  ep of the increm
265ce 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 70 72 6f  ental vacuum pro
265cf 63 65 64 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65  cedure on.** the
265d0 20 50 31 20 64 61 74 61 62 61 73 65 2e 20 49 66   P1 database. If
265d1 20 74 68 65 20 76 61 63 75 75 6d 20 68 61 73 20   the vacuum has 
265d2 66 69 6e 69 73 68 65 64 2c 20 6a 75 6d 70 20 74  finished, jump t
265d3 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a  o instruction.**
265d4 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20   P2. Otherwise, 
265d5 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20  fall through to 
265d6 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
265d7 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  tion..*/.case OP
265d8 5f 49 6e 63 72 56 61 63 75 75 6d 3a 20 7b 20 20  _IncrVacuum: {  
265d9 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f        /* jump */
265da 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
265db 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
265dc 20 69 6e 74 6f 20 75 2e 63 6b 20 2a 2f 0a 20 20   into u.ck */.  
265dd 42 74 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64  Btree *pBt;.#end
265de 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
265df 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
265e0 20 75 2e 63 6b 20 2a 2f 0a 0a 20 20 61 73 73 65   u.ck */..  asse
265e1 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
265e2 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44  & pOp->p1<db->nD
265e3 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  b );.  assert( (
265e4 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
265e5 28 28 79 44 62 4d 61 73 6b 29 31 29 3c 3c 70 4f  ((yDbMask)1)<<pO
265e6 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20  p->p1))!=0 );.  
265e7 75 2e 63 6b 2e 70 42 74 20 3d 20 64 62 2d 3e 61  u.ck.pBt = db->a
265e8 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b  Db[pOp->p1].pBt;
265e9 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
265ea 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 75  treeIncrVacuum(u
265eb 2e 63 6b 2e 70 42 74 29 3b 0a 20 20 69 66 28 20  .ck.pBt);.  if( 
265ec 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
265ed 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
265ee 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 72 63 20  >p2 - 1;.    rc 
265ef 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
265f0 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
265f1 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45  if../* Opcode: E
265f2 78 70 69 72 65 20 50 31 20 2a 20 2a 20 2a 20 2a  xpire P1 * * * *
265f3 0a 2a 2a 0a 2a 2a 20 43 61 75 73 65 20 70 72 65  .**.** Cause pre
265f4 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65  compiled stateme
265f5 6e 74 73 20 74 6f 20 62 65 63 6f 6d 65 20 65 78  nts to become ex
265f6 70 69 72 65 64 2e 20 41 6e 20 65 78 70 69 72 65  pired. An expire
265f7 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 66  d statement.** f
265f8 61 69 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72  ails with an err
265f9 6f 72 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54  or code of SQLIT
265fa 45 5f 53 43 48 45 4d 41 20 69 66 20 69 74 20 69  E_SCHEMA if it i
265fb 73 20 65 76 65 72 20 65 78 65 63 75 74 65 64 20  s ever executed 
265fc 0a 2a 2a 20 28 76 69 61 20 73 71 6c 69 74 65 33  .** (via sqlite3
265fd 5f 73 74 65 70 28 29 29 2e 0a 2a 2a 20 0a 2a 2a  _step())..** .**
265fe 20 49 66 20 50 31 20 69 73 20 30 2c 20 74 68 65   If P1 is 0, the
265ff 6e 20 61 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d  n all SQL statem
26600 65 6e 74 73 20 62 65 63 6f 6d 65 20 65 78 70 69  ents become expi
26601 72 65 64 2e 20 49 66 20 50 31 20 69 73 20 6e 6f  red. If P1 is no
26602 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20  n-zero,.** then 
26603 6f 6e 6c 79 20 74 68 65 20 63 75 72 72 65 6e 74  only the current
26604 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73 74 61  ly executing sta
26605 74 65 6d 65 6e 74 20 69 73 20 61 66 66 65 63 74  tement is affect
26606 65 64 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ed. .*/.case OP_
26607 45 78 70 69 72 65 3a 20 7b 0a 20 20 69 66 28 20  Expire: {.  if( 
26608 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20  !pOp->p1 ){.    
26609 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
2660a 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
2660b 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  db);.  }else{.  
2660c 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31    p->expired = 1
2660d 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
2660e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2660f 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
26610 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 61  HE./* Opcode: Ta
26611 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32 20 50 33  bleLock P1 P2 P3
26612 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62 74 61   P4 *.**.** Obta
26613 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70  in a lock on a p
26614 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e  articular table.
26615 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
26616 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77  n is only used w
26617 68 65 6e 0a 2a 2a 20 74 68 65 20 73 68 61 72 65  hen.** the share
26618 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 65 20  d-cache feature 
26619 69 73 20 65 6e 61 62 6c 65 64 2e 20 0a 2a 2a 0a  is enabled. .**.
2661a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 69 6e 64  ** P1 is the ind
2661b 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
2661c 73 65 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44  se in sqlite3.aD
2661d 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62  b[] of the datab
2661e 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69 63 68 20  ase.** on which 
2661f 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 63 71 75  the lock is acqu
26620 69 72 65 64 2e 20 20 41 20 72 65 61 64 6c 6f 63  ired.  A readloc
26621 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 69 66  k is obtained if
26622 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61 20 77   P3==0 or.** a w
26623 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 50 33 3d  rite lock if P3=
26624 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63 6f 6e  =1..**.** P2 con
26625 74 61 69 6e 73 20 74 68 65 20 72 6f 6f 74 2d 70  tains the root-p
26626 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  age of the table
26627 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20   to lock..**.** 
26628 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f  P4 contains a po
26629 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d  inter to the nam
2662a 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62  e of the table b
2662b 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 54 68 69  eing locked. Thi
2662c 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75 73 65  s is only.** use
2662d 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 6e  d to generate an
2662e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
2662f 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f  f the lock canno
26630 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a  t be obtained..*
26631 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 6c 65 4c  /.case OP_TableL
26632 6f 63 6b 3a 20 7b 0a 20 20 75 38 20 69 73 57 72  ock: {.  u8 isWr
26633 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38 29 70 4f  iteLock = (u8)pO
26634 70 2d 3e 70 33 3b 0a 20 20 69 66 28 20 69 73 57  p->p3;.  if( isW
26635 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 30 3d 3d 28  riteLock || 0==(
26636 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
26637 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64  _ReadUncommitted
26638 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 31 20  ) ){.    int p1 
26639 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 20 20 20  = pOp->p1; .    
2663a 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20 26 26  assert( p1>=0 &&
2663b 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20   p1<db->nDb );. 
2663c 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62     assert( (p->b
2663d 74 72 65 65 4d 61 73 6b 20 26 20 28 28 28 79 44  treeMask & (((yD
2663e 62 4d 61 73 6b 29 31 29 3c 3c 70 31 29 29 21 3d  bMask)1)<<p1))!=
2663f 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
26640 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20   isWriteLock==0 
26641 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d  || isWriteLock==
26642 31 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  1 );.    rc = sq
26643 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61  lite3BtreeLockTa
26644 62 6c 65 28 64 62 2d 3e 61 44 62 5b 70 31 5d 2e  ble(db->aDb[p1].
26645 70 42 74 2c 20 70 4f 70 2d 3e 70 32 2c 20 69 73  pBt, pOp->p2, is
26646 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 20 20  WriteLock);.    
26647 69 66 28 20 28 72 63 26 30 78 46 46 29 3d 3d 53  if( (rc&0xFF)==S
26648 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a  QLITE_LOCKED ){.
26649 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
2664a 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b   *z = pOp->p4.z;
2664b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65  .      sqlite3Se
2664c 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
2664d 4d 73 67 2c 20 64 62 2c 20 22 64 61 74 61 62 61  Msg, db, "databa
2664e 73 65 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b  se table is lock
2664f 65 64 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20 20  ed: %s", z);.   
26650 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a   }.  }.  break;.
26651 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
26652 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
26653 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  ACHE */..#ifndef
26654 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
26655 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63  TUALTABLE./* Opc
26656 6f 64 65 3a 20 56 42 65 67 69 6e 20 2a 20 2a 20  ode: VBegin * * 
26657 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
26658 6d 61 79 20 62 65 20 61 20 70 6f 69 6e 74 65 72  may be a pointer
26659 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76   to an sqlite3_v
2665a 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 20 49  tab structure. I
2665b 66 20 73 6f 2c 20 63 61 6c 6c 20 74 68 65 20 0a  f so, call the .
2665c 2a 2a 20 78 42 65 67 69 6e 20 6d 65 74 68 6f 64  ** xBegin method
2665d 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e   for that table.
2665e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 2c 20 77 68 65  .**.** Also, whe
2665f 74 68 65 72 20 6f 72 20 6e 6f 74 20 50 34 20 69  ther or not P4 i
26660 73 20 73 65 74 2c 20 63 68 65 63 6b 20 74 68 61  s set, check tha
26661 74 20 74 68 69 73 20 69 73 20 6e 6f 74 20 62 65  t this is not be
26662 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 0a  ing called from.
26663 2a 2a 20 77 69 74 68 69 6e 20 61 20 63 61 6c 6c  ** within a call
26664 62 61 63 6b 20 74 6f 20 61 20 76 69 72 74 75 61  back to a virtua
26665 6c 20 74 61 62 6c 65 20 78 53 79 6e 63 28 29 20  l table xSync() 
26666 6d 65 74 68 6f 64 2e 20 49 66 20 69 74 20 69 73  method. If it is
26667 2c 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63  , the error.** c
26668 6f 64 65 20 77 69 6c 6c 20 62 65 20 73 65 74 20  ode will be set 
26669 74 6f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  to SQLITE_LOCKED
2666a 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 42 65  ..*/.case OP_VBe
2666b 67 69 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  gin: {.#if 0  /*
2666c 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
2666d 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6c   moved into u.cl
2666e 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70 56   */.  VTable *pV
2666f 54 61 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  Tab;.#endif /* l
26670 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
26671 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6c 20 2a  oved into u.cl *
26672 2f 0a 20 20 75 2e 63 6c 2e 70 56 54 61 62 20 3d  /.  u.cl.pVTab =
26673 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a   pOp->p4.pVtab;.
26674 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74    rc = sqlite3Vt
26675 61 62 42 65 67 69 6e 28 64 62 2c 20 75 2e 63 6c  abBegin(db, u.cl
26676 2e 70 56 54 61 62 29 3b 0a 20 20 69 66 28 20 75  .pVTab);.  if( u
26677 2e 63 6c 2e 70 56 54 61 62 20 29 20 69 6d 70 6f  .cl.pVTab ) impo
26678 72 74 56 74 61 62 45 72 72 4d 73 67 28 70 2c 20  rtVtabErrMsg(p, 
26679 75 2e 63 6c 2e 70 56 54 61 62 2d 3e 70 56 74 61  u.cl.pVTab->pVta
2667a 62 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23  b);.  break;.}.#
2667b 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2667c 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2667d 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  E */..#ifndef SQ
2667e 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2667f 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65  LTABLE./* Opcode
26680 3a 20 56 43 72 65 61 74 65 20 50 31 20 2a 20 2a  : VCreate P1 * *
26681 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69   P4 *.**.** P4 i
26682 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  s the name of a 
26683 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e  virtual table in
26684 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 43 61   database P1. Ca
26685 6c 6c 20 74 68 65 20 78 43 72 65 61 74 65 20 6d  ll the xCreate m
26686 65 74 68 6f 64 0a 2a 2a 20 66 6f 72 20 74 68 61  ethod.** for tha
26687 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65  t table..*/.case
26688 20 4f 50 5f 56 43 72 65 61 74 65 3a 20 7b 0a 20   OP_VCreate: {. 
26689 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61   rc = sqlite3Vta
2668a 62 43 61 6c 6c 43 72 65 61 74 65 28 64 62 2c 20  bCallCreate(db, 
2668b 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34  pOp->p1, pOp->p4
2668c 2e 7a 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29  .z, &p->zErrMsg)
2668d 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  ;.  break;.}.#en
2668e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2668f 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
26690 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
26691 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
26692 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ABLE./* Opcode: 
26693 56 44 65 73 74 72 6f 79 20 50 31 20 2a 20 2a 20  VDestroy P1 * * 
26694 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73  P4 *.**.** P4 is
26695 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 76   the name of a v
26696 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20  irtual table in 
26697 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 43 61  database P1.  Ca
26698 6c 6c 20 74 68 65 20 78 44 65 73 74 72 6f 79 20  ll the xDestroy 
26699 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 61  method.** of tha
2669a 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65  t table..*/.case
2669b 20 4f 50 5f 56 44 65 73 74 72 6f 79 3a 20 7b 0a   OP_VDestroy: {.
2669c 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f    p->inVtabMetho
2669d 64 20 3d 20 32 3b 0a 20 20 72 63 20 3d 20 73 71  d = 2;.  rc = sq
2669e 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65 73  lite3VtabCallDes
2669f 74 72 6f 79 28 64 62 2c 20 70 4f 70 2d 3e 70 31  troy(db, pOp->p1
266a0 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20  , pOp->p4.z);.  
266a1 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20  p->inVtabMethod 
266a2 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  = 0;.  break;.}.
266a3 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
266a4 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
266a5 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  LE */..#ifndef S
266a6 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
266a7 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64  ALTABLE./* Opcod
266a8 65 3a 20 56 4f 70 65 6e 20 50 31 20 2a 20 2a 20  e: VOpen P1 * * 
266a9 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73  P4 *.**.** P4 is
266aa 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
266ab 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62  virtual table ob
266ac 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33  ject, an sqlite3
266ad 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e  _vtab structure.
266ae 0a 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 73  .** P1 is a curs
266af 6f 72 20 6e 75 6d 62 65 72 2e 20 20 54 68 69 73  or number.  This
266b0 20 6f 70 63 6f 64 65 20 6f 70 65 6e 73 20 61 20   opcode opens a 
266b1 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 76 69  cursor to the vi
266b2 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 61  rtual.** table a
266b3 6e 64 20 73 74 6f 72 65 73 20 74 68 61 74 20 63  nd stores that c
266b4 75 72 73 6f 72 20 69 6e 20 50 31 2e 0a 2a 2f 0a  ursor in P1..*/.
266b5 63 61 73 65 20 4f 50 5f 56 4f 70 65 6e 3a 20 7b  case OP_VOpen: {
266b6 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
266b7 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
266b8 20 69 6e 74 6f 20 75 2e 63 6d 20 2a 2f 0a 20 20   into u.cm */.  
266b9 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72  VdbeCursor *pCur
266ba 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ;.  sqlite3_vtab
266bb 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75  _cursor *pVtabCu
266bc 72 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f  rsor;.  sqlite3_
266bd 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 73  vtab *pVtab;.  s
266be 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
266bf 4d 6f 64 75 6c 65 3b 0a 23 65 6e 64 69 66 20 2f  Module;.#endif /
266c0 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
266c1 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
266c2 6d 20 2a 2f 0a 0a 20 20 75 2e 63 6d 2e 70 43 75  m */..  u.cm.pCu
266c3 72 20 3d 20 30 3b 0a 20 20 75 2e 63 6d 2e 70 56  r = 0;.  u.cm.pV
266c4 74 61 62 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20  tabCursor = 0;. 
266c5 20 75 2e 63 6d 2e 70 56 74 61 62 20 3d 20 70 4f   u.cm.pVtab = pO
266c6 70 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74  p->p4.pVtab->pVt
266c7 61 62 3b 0a 20 20 75 2e 63 6d 2e 70 4d 6f 64 75  ab;.  u.cm.pModu
266c8 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f  le = (sqlite3_mo
266c9 64 75 6c 65 20 2a 29 75 2e 63 6d 2e 70 56 74 61  dule *)u.cm.pVta
266ca 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73  b->pModule;.  as
266cb 73 65 72 74 28 75 2e 63 6d 2e 70 56 74 61 62 20  sert(u.cm.pVtab 
266cc 26 26 20 75 2e 63 6d 2e 70 4d 6f 64 75 6c 65 29  && u.cm.pModule)
266cd 3b 0a 20 20 72 63 20 3d 20 75 2e 63 6d 2e 70 4d  ;.  rc = u.cm.pM
266ce 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 75 2e 63  odule->xOpen(u.c
266cf 6d 2e 70 56 74 61 62 2c 20 26 75 2e 63 6d 2e 70  m.pVtab, &u.cm.p
266d0 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 69  VtabCursor);.  i
266d1 6d 70 6f 72 74 56 74 61 62 45 72 72 4d 73 67 28  mportVtabErrMsg(
266d2 70 2c 20 75 2e 63 6d 2e 70 56 74 61 62 29 3b 0a  p, u.cm.pVtab);.
266d3 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
266d4 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e  =rc ){.    /* In
266d5 69 74 69 61 6c 69 7a 65 20 73 71 6c 69 74 65 33  itialize sqlite3
266d6 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73  _vtab_cursor bas
266d7 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20 20 20 75  e class */.    u
266d8 2e 63 6d 2e 70 56 74 61 62 43 75 72 73 6f 72 2d  .cm.pVtabCursor-
266d9 3e 70 56 74 61 62 20 3d 20 75 2e 63 6d 2e 70 56  >pVtab = u.cm.pV
266da 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69  tab;..    /* Ini
266db 74 69 61 6c 69 73 65 20 76 64 62 65 20 63 75 72  tialise vdbe cur
266dc 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  sor object */.  
266dd 20 20 75 2e 63 6d 2e 70 43 75 72 20 3d 20 61 6c    u.cm.pCur = al
266de 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20  locateCursor(p, 
266df 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 2d 31 2c 20  pOp->p1, 0, -1, 
266e0 30 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 63 6d  0);.    if( u.cm
266e1 2e 70 43 75 72 20 29 7b 0a 20 20 20 20 20 20 75  .pCur ){.      u
266e2 2e 63 6d 2e 70 43 75 72 2d 3e 70 56 74 61 62 43  .cm.pCur->pVtabC
266e3 75 72 73 6f 72 20 3d 20 75 2e 63 6d 2e 70 56 74  ursor = u.cm.pVt
266e4 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20  abCursor;.      
266e5 75 2e 63 6d 2e 70 43 75 72 2d 3e 70 4d 6f 64 75  u.cm.pCur->pModu
266e6 6c 65 20 3d 20 75 2e 63 6d 2e 70 56 74 61 62 43  le = u.cm.pVtabC
266e7 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d 3e 70 4d  ursor->pVtab->pM
266e8 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 65 6c 73 65  odule;.    }else
266e9 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  {.      db->mall
266ea 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
266eb 20 20 20 20 75 2e 63 6d 2e 70 4d 6f 64 75 6c 65      u.cm.pModule
266ec 2d 3e 78 43 6c 6f 73 65 28 75 2e 63 6d 2e 70 56  ->xClose(u.cm.pV
266ed 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  tabCursor);.    
266ee 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  }.  }.  break;.}
266ef 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
266f0 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
266f1 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  BLE */..#ifndef 
266f2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
266f3 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f  UALTABLE./* Opco
266f4 64 65 3a 20 56 46 69 6c 74 65 72 20 50 31 20 50  de: VFilter P1 P
266f5 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  2 P3 P4 *.**.** 
266f6 50 31 20 69 73 20 61 20 63 75 72 73 6f 72 20 6f  P1 is a cursor o
266f7 70 65 6e 65 64 20 75 73 69 6e 67 20 56 4f 70 65  pened using VOpe
266f8 6e 2e 20 20 50 32 20 69 73 20 61 6e 20 61 64 64  n.  P2 is an add
266f9 72 65 73 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20  ress to jump to 
266fa 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c 74 65 72  if.** the filter
266fb 65 64 20 72 65 73 75 6c 74 20 73 65 74 20 69 73  ed result set is
266fc 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 50 34   empty..**.** P4
266fd 20 69 73 20 65 69 74 68 65 72 20 4e 55 4c 4c 20   is either NULL 
266fe 6f 72 20 61 20 73 74 72 69 6e 67 20 74 68 61 74  or a string that
266ff 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 62   was generated b
26700 79 20 74 68 65 20 78 42 65 73 74 49 6e 64 65 78  y the xBestIndex
26701 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  .** method of th
26702 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 69  e module.  The i
26703 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66  nterpretation of
26704 20 74 68 65 20 50 34 20 73 74 72 69 6e 67 20 69   the P4 string i
26705 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20 74 68 65  s left.** to the
26706 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
26707 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  tation..**.** Th
26708 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65  is opcode invoke
26709 73 20 74 68 65 20 78 46 69 6c 74 65 72 20 6d 65  s the xFilter me
2670a 74 68 6f 64 20 6f 6e 20 74 68 65 20 76 69 72 74  thod on the virt
2670b 75 61 6c 20 74 61 62 6c 65 20 73 70 65 63 69 66  ual table specif
2670c 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e 20 20 54  ied.** by P1.  T
2670d 68 65 20 69 6e 74 65 67 65 72 20 71 75 65 72 79  he integer query
2670e 20 70 6c 61 6e 20 70 61 72 61 6d 65 74 65 72 20   plan parameter 
2670f 74 6f 20 78 46 69 6c 74 65 72 20 69 73 20 73 74  to xFilter is st
26710 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
26711 0a 2a 2a 20 50 33 2e 20 52 65 67 69 73 74 65 72  .** P3. Register
26712 20 50 33 2b 31 20 73 74 6f 72 65 73 20 74 68 65   P3+1 stores the
26713 20 61 72 67 63 20 70 61 72 61 6d 65 74 65 72 20   argc parameter 
26714 74 6f 20 62 65 20 70 61 73 73 65 64 20 74 6f 20  to be passed to 
26715 74 68 65 0a 2a 2a 20 78 46 69 6c 74 65 72 20 6d  the.** xFilter m
26716 65 74 68 6f 64 2e 20 52 65 67 69 73 74 65 72 73  ethod. Registers
26717 20 50 33 2b 32 2e 2e 50 33 2b 31 2b 61 72 67 63   P3+2..P3+1+argc
26718 20 61 72 65 20 74 68 65 20 61 72 67 63 0a 2a 2a   are the argc.**
26719 20 61 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61   additional para
2671a 6d 65 74 65 72 73 20 77 68 69 63 68 20 61 72 65  meters which are
2671b 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 78 46   passed to.** xF
2671c 69 6c 74 65 72 20 61 73 20 61 72 67 76 2e 20 52  ilter as argv. R
2671d 65 67 69 73 74 65 72 20 50 33 2b 32 20 62 65 63  egister P3+2 bec
2671e 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20 77 68 65  omes argv[0] whe
2671f 6e 20 70 61 73 73 65 64 20 74 6f 20 78 46 69 6c  n passed to xFil
26720 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75 6d  ter..**.** A jum
26721 70 20 69 73 20 6d 61 64 65 20 74 6f 20 50 32 20  p is made to P2 
26722 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  if the result se
26723 74 20 61 66 74 65 72 20 66 69 6c 74 65 72 69 6e  t after filterin
26724 67 20 77 6f 75 6c 64 20 62 65 20 65 6d 70 74 79  g would be empty
26725 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 46 69  ..*/.case OP_VFi
26726 6c 74 65 72 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d  lter: {   /* jum
26727 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  p */.#if 0  /* l
26728 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
26729 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6e 20 2a  oved into u.cn *
2672a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20  /.  int nArg;.  
2672b 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 63 6f  int iQuery;.  co
2672c 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
2672d 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d  le *pModule;.  M
2672e 65 6d 20 2a 70 51 75 65 72 79 3b 0a 20 20 4d 65  em *pQuery;.  Me
2672f 6d 20 2a 70 41 72 67 63 3b 0a 20 20 73 71 6c 69  m *pArgc;.  sqli
26730 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
26731 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20  *pVtabCursor;.  
26732 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
26733 74 61 62 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  tab;.  VdbeCurso
26734 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 72  r *pCur;.  int r
26735 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d  es;.  int i;.  M
26736 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 23 65 6e 64  em **apArg;.#end
26737 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
26738 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
26739 20 75 2e 63 6e 20 2a 2f 0a 0a 20 20 75 2e 63 6e   u.cn */..  u.cn
2673a 2e 70 51 75 65 72 79 20 3d 20 26 61 4d 65 6d 5b  .pQuery = &aMem[
2673b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63 6e  pOp->p3];.  u.cn
2673c 2e 70 41 72 67 63 20 3d 20 26 75 2e 63 6e 2e 70  .pArgc = &u.cn.p
2673d 51 75 65 72 79 5b 31 5d 3b 0a 20 20 75 2e 63 6e  Query[1];.  u.cn
2673e 2e 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72  .pCur = p->apCsr
2673f 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
26740 65 72 74 28 20 6d 65 6d 49 73 56 61 6c 69 64 28  ert( memIsValid(
26741 75 2e 63 6e 2e 70 51 75 65 72 79 29 20 29 3b 0a  u.cn.pQuery) );.
26742 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
26743 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 63 6e 2e 70  (pOp->p3, u.cn.p
26744 51 75 65 72 79 29 3b 0a 20 20 61 73 73 65 72 74  Query);.  assert
26745 28 20 75 2e 63 6e 2e 70 43 75 72 2d 3e 70 56 74  ( u.cn.pCur->pVt
26746 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e  abCursor );.  u.
26747 63 6e 2e 70 56 74 61 62 43 75 72 73 6f 72 20 3d  cn.pVtabCursor =
26748 20 75 2e 63 6e 2e 70 43 75 72 2d 3e 70 56 74 61   u.cn.pCur->pVta
26749 62 43 75 72 73 6f 72 3b 0a 20 20 75 2e 63 6e 2e  bCursor;.  u.cn.
2674a 70 56 74 61 62 20 3d 20 75 2e 63 6e 2e 70 56 74  pVtab = u.cn.pVt
2674b 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b  abCursor->pVtab;
2674c 0a 20 20 75 2e 63 6e 2e 70 4d 6f 64 75 6c 65 20  .  u.cn.pModule 
2674d 3d 20 75 2e 63 6e 2e 70 56 74 61 62 2d 3e 70 4d  = u.cn.pVtab->pM
2674e 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 72 61  odule;..  /* Gra
2674f 62 20 74 68 65 20 69 6e 64 65 78 20 6e 75 6d 62  b the index numb
26750 65 72 20 61 6e 64 20 61 72 67 63 20 70 61 72 61  er and argc para
26751 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65  meters */.  asse
26752 72 74 28 20 28 75 2e 63 6e 2e 70 51 75 65 72 79  rt( (u.cn.pQuery
26753 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 29  ->flags&MEM_Int)
26754 21 3d 30 20 26 26 20 75 2e 63 6e 2e 70 41 72 67  !=0 && u.cn.pArg
26755 63 2d 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e  c->flags==MEM_In
26756 74 20 29 3b 0a 20 20 75 2e 63 6e 2e 6e 41 72 67  t );.  u.cn.nArg
26757 20 3d 20 28 69 6e 74 29 75 2e 63 6e 2e 70 41 72   = (int)u.cn.pAr
26758 67 63 2d 3e 75 2e 69 3b 0a 20 20 75 2e 63 6e 2e  gc->u.i;.  u.cn.
26759 69 51 75 65 72 79 20 3d 20 28 69 6e 74 29 75 2e  iQuery = (int)u.
2675a 63 6e 2e 70 51 75 65 72 79 2d 3e 75 2e 69 3b 0a  cn.pQuery->u.i;.
2675b 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65  .  /* Invoke the
2675c 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20   xFilter method 
2675d 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 2e 63 6e 2e  */.  {.    u.cn.
2675e 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e 63  res = 0;.    u.c
2675f 6e 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41  n.apArg = p->apA
26760 72 67 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63 6e  rg;.    for(u.cn
26761 2e 69 20 3d 20 30 3b 20 75 2e 63 6e 2e 69 3c 75  .i = 0; u.cn.i<u
26762 2e 63 6e 2e 6e 41 72 67 3b 20 75 2e 63 6e 2e 69  .cn.nArg; u.cn.i
26763 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e 63 6e 2e  ++){.      u.cn.
26764 61 70 41 72 67 5b 75 2e 63 6e 2e 69 5d 20 3d 20  apArg[u.cn.i] = 
26765 26 75 2e 63 6e 2e 70 41 72 67 63 5b 75 2e 63 6e  &u.cn.pArgc[u.cn
26766 2e 69 2b 31 5d 3b 0a 20 20 20 20 20 20 73 71 6c  .i+1];.      sql
26767 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65  ite3VdbeMemStore
26768 54 79 70 65 28 75 2e 63 6e 2e 61 70 41 72 67 5b  Type(u.cn.apArg[
26769 75 2e 63 6e 2e 69 5d 29 3b 0a 20 20 20 20 7d 0a  u.cn.i]);.    }.
2676a 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65  .    p->inVtabMe
2676b 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 72 63  thod = 1;.    rc
2676c 20 3d 20 75 2e 63 6e 2e 70 4d 6f 64 75 6c 65 2d   = u.cn.pModule-
2676d 3e 78 46 69 6c 74 65 72 28 75 2e 63 6e 2e 70 56  >xFilter(u.cn.pV
2676e 74 61 62 43 75 72 73 6f 72 2c 20 75 2e 63 6e 2e  tabCursor, u.cn.
2676f 69 51 75 65 72 79 2c 20 70 4f 70 2d 3e 70 34 2e  iQuery, pOp->p4.
26770 7a 2c 20 75 2e 63 6e 2e 6e 41 72 67 2c 20 75 2e  z, u.cn.nArg, u.
26771 63 6e 2e 61 70 41 72 67 29 3b 0a 20 20 20 20 70  cn.apArg);.    p
26772 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d  ->inVtabMethod =
26773 20 30 3b 0a 20 20 20 20 69 6d 70 6f 72 74 56 74   0;.    importVt
26774 61 62 45 72 72 4d 73 67 28 70 2c 20 75 2e 63 6e  abErrMsg(p, u.cn
26775 2e 70 56 74 61 62 29 3b 0a 20 20 20 20 69 66 28  .pVtab);.    if(
26776 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
26777 7b 0a 20 20 20 20 20 20 75 2e 63 6e 2e 72 65 73  {.      u.cn.res
26778 20 3d 20 75 2e 63 6e 2e 70 4d 6f 64 75 6c 65 2d   = u.cn.pModule-
26779 3e 78 45 6f 66 28 75 2e 63 6e 2e 70 56 74 61 62  >xEof(u.cn.pVtab
2677a 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 0a  Cursor);.    }..
2677b 20 20 20 20 69 66 28 20 75 2e 63 6e 2e 72 65 73      if( u.cn.res
2677c 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70   ){.      pc = p
2677d 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
2677e 7d 0a 20 20 7d 0a 20 20 75 2e 63 6e 2e 70 43 75  }.  }.  u.cn.pCu
2677f 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a  r->nullRow = 0;.
26780 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
26781 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
26782 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
26783 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
26784 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
26785 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
26786 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 20  Column P1 P2 P3 
26787 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20  * *.**.** Store 
26788 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
26789 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66   P2-th column of
2678a 0a 2a 2a 20 74 68 65 20 72 6f 77 20 6f 66 20 74  .** the row of t
2678b 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  he virtual-table
2678c 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 50 31   that the .** P1
2678d 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74   cursor is point
2678e 69 6e 67 20 74 6f 20 69 6e 74 6f 20 72 65 67 69  ing to into regi
2678f 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63 61 73 65  ster P3..*/.case
26790 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 23   OP_VColumn: {.#
26791 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
26792 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
26793 6e 74 6f 20 75 2e 63 6f 20 2a 2f 0a 20 20 73 71  nto u.co */.  sq
26794 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
26795 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  b;.  const sqlit
26796 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
26797 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74  le;.  Mem *pDest
26798 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ;.  sqlite3_cont
26799 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 0a 23 65  ext sContext;.#e
2679a 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
2679b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
2679c 74 6f 20 75 2e 63 6f 20 2a 2f 0a 0a 20 20 56 64  to u.co */..  Vd
2679d 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d  beCursor *pCur =
2679e 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
2679f 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  1];.  assert( pC
267a0 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20  ur->pVtabCursor 
267a1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
267a2 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p3>0 && pOp->p
267a3 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
267a4 75 2e 63 6f 2e 70 44 65 73 74 20 3d 20 26 61 4d  u.co.pDest = &aM
267a5 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 6d  em[pOp->p3];.  m
267a6 65 6d 41 62 6f 75 74 54 6f 43 68 61 6e 67 65 28  emAboutToChange(
267a7 70 2c 20 75 2e 63 6f 2e 70 44 65 73 74 29 3b 0a  p, u.co.pDest);.
267a8 20 20 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c 6c    if( pCur->null
267a9 52 6f 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Row ){.    sqlit
267aa 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c  e3VdbeMemSetNull
267ab 28 75 2e 63 6f 2e 70 44 65 73 74 29 3b 0a 20 20  (u.co.pDest);.  
267ac 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75    break;.  }.  u
267ad 2e 63 6f 2e 70 56 74 61 62 20 3d 20 70 43 75 72  .co.pVtab = pCur
267ae 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70  ->pVtabCursor->p
267af 56 74 61 62 3b 0a 20 20 75 2e 63 6f 2e 70 4d 6f  Vtab;.  u.co.pMo
267b0 64 75 6c 65 20 3d 20 75 2e 63 6f 2e 70 56 74 61  dule = u.co.pVta
267b1 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73  b->pModule;.  as
267b2 73 65 72 74 28 20 75 2e 63 6f 2e 70 4d 6f 64 75  sert( u.co.pModu
267b3 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 20 29 3b 0a 20  le->xColumn );. 
267b4 20 6d 65 6d 73 65 74 28 26 75 2e 63 6f 2e 73 43   memset(&u.co.sC
267b5 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f  ontext, 0, sizeo
267b6 66 28 75 2e 63 6f 2e 73 43 6f 6e 74 65 78 74 29  f(u.co.sContext)
267b7 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6f 75 74  );..  /* The out
267b8 70 75 74 20 63 65 6c 6c 20 6d 61 79 20 61 6c 72  put cell may alr
267b9 65 61 64 79 20 68 61 76 65 20 61 20 62 75 66 66  eady have a buff
267ba 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4d 6f  er allocated. Mo
267bb 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 72  ve.  ** the curr
267bc 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 74 6f 20  ent contents to 
267bd 75 2e 63 6f 2e 73 43 6f 6e 74 65 78 74 2e 73 20  u.co.sContext.s 
267be 73 6f 20 69 6e 20 63 61 73 65 20 74 68 65 20 75  so in case the u
267bf 73 65 72 2d 66 75 6e 63 74 69 6f 6e 0a 20 20 2a  ser-function.  *
267c0 2a 20 63 61 6e 20 75 73 65 20 74 68 65 20 61 6c  * can use the al
267c1 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20  ready allocated 
267c2 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f  buffer instead o
267c3 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 0a 20  f allocating a. 
267c4 20 2a 2a 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a   ** new one..  *
267c5 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  /.  sqlite3VdbeM
267c6 65 6d 4d 6f 76 65 28 26 75 2e 63 6f 2e 73 43 6f  emMove(&u.co.sCo
267c7 6e 74 65 78 74 2e 73 2c 20 75 2e 63 6f 2e 70 44  ntext.s, u.co.pD
267c8 65 73 74 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79  est);.  MemSetTy
267c9 70 65 46 6c 61 67 28 26 75 2e 63 6f 2e 73 43 6f  peFlag(&u.co.sCo
267ca 6e 74 65 78 74 2e 73 2c 20 4d 45 4d 5f 4e 75 6c  ntext.s, MEM_Nul
267cb 6c 29 3b 0a 0a 20 20 72 63 20 3d 20 75 2e 63 6f  l);..  rc = u.co
267cc 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c 75 6d  .pModule->xColum
267cd 6e 28 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72  n(pCur->pVtabCur
267ce 73 6f 72 2c 20 26 75 2e 63 6f 2e 73 43 6f 6e 74  sor, &u.co.sCont
267cf 65 78 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 20  ext, pOp->p2);. 
267d0 20 69 6d 70 6f 72 74 56 74 61 62 45 72 72 4d 73   importVtabErrMs
267d1 67 28 70 2c 20 75 2e 63 6f 2e 70 56 74 61 62 29  g(p, u.co.pVtab)
267d2 3b 0a 20 20 69 66 28 20 75 2e 63 6f 2e 73 43 6f  ;.  if( u.co.sCo
267d3 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 20 29 7b  ntext.isError ){
267d4 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 6f 2e 73  .    rc = u.co.s
267d5 43 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 3b  Context.isError;
267d6 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20  .  }..  /* Copy 
267d7 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
267d8 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68  e function to th
267d9 65 20 50 33 20 72 65 67 69 73 74 65 72 2e 20 57  e P3 register. W
267da 65 0a 20 20 2a 2a 20 64 6f 20 74 68 69 73 20 72  e.  ** do this r
267db 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65  egardless of whe
267dc 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65  ther or not an e
267dd 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 74 6f  rror occurred to
267de 20 65 6e 73 75 72 65 20 61 6e 79 0a 20 20 2a 2a   ensure any.  **
267df 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74   dynamic allocat
267e0 69 6f 6e 20 69 6e 20 75 2e 63 6f 2e 73 43 6f 6e  ion in u.co.sCon
267e1 74 65 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 74  text.s (a Mem st
267e2 72 75 63 74 29 20 69 73 20 20 72 65 6c 65 61 73  ruct) is  releas
267e3 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ed..  */.  sqlit
267e4 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
267e5 64 69 6e 67 28 26 75 2e 63 6f 2e 73 43 6f 6e 74  ding(&u.co.sCont
267e6 65 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29  ext.s, encoding)
267e7 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
267e8 65 6d 4d 6f 76 65 28 75 2e 63 6f 2e 70 44 65 73  emMove(u.co.pDes
267e9 74 2c 20 26 75 2e 63 6f 2e 73 43 6f 6e 74 65 78  t, &u.co.sContex
267ea 74 2e 73 29 3b 0a 20 20 52 45 47 49 53 54 45 52  t.s);.  REGISTER
267eb 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20  _TRACE(pOp->p3, 
267ec 75 2e 63 6f 2e 70 44 65 73 74 29 3b 0a 20 20 55  u.co.pDest);.  U
267ed 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
267ee 5a 45 28 75 2e 63 6f 2e 70 44 65 73 74 29 3b 0a  ZE(u.co.pDest);.
267ef 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
267f0 62 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 63 6f  beMemTooBig(u.co
267f1 2e 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 67  .pDest) ){.    g
267f2 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d  oto too_big;.  }
267f3 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
267f4 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
267f5 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
267f6 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
267f7 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
267f8 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
267f9 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  Next P1 P2 * * *
267fa 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76  .**.** Advance v
267fb 69 72 74 75 61 6c 20 74 61 62 6c 65 20 50 31 20  irtual table P1 
267fc 74 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20  to the next row 
267fd 69 6e 20 69 74 73 20 72 65 73 75 6c 74 20 73 65  in its result se
267fe 74 20 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f  t and.** jump to
267ff 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e   instruction P2.
26800 20 20 4f 72 2c 20 69 66 20 74 68 65 20 76 69 72    Or, if the vir
26801 74 75 61 6c 20 74 61 62 6c 65 20 68 61 73 20 72  tual table has r
26802 65 61 63 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e  eached.** the en
26803 64 20 6f 66 20 69 74 73 20 72 65 73 75 6c 74 20  d of its result 
26804 73 65 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74  set, then fall t
26805 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
26806 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  xt instruction..
26807 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 74  */.case OP_VNext
26808 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f  : {   /* jump */
26809 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
2680a 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
2680b 20 69 6e 74 6f 20 75 2e 63 70 20 2a 2f 0a 20 20   into u.cp */.  
2680c 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
2680d 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  tab;.  const sql
2680e 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
2680f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 65 73 3b  dule;.  int res;
26810 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
26811 43 75 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  Cur;.#endif /* l
26812 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
26813 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 70 20 2a  oved into u.cp *
26814 2f 0a 0a 20 20 75 2e 63 70 2e 72 65 73 20 3d 20  /..  u.cp.res = 
26815 30 3b 0a 20 20 75 2e 63 70 2e 70 43 75 72 20 3d  0;.  u.cp.pCur =
26816 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
26817 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  1];.  assert( u.
26818 63 70 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75  cp.pCur->pVtabCu
26819 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 75 2e  rsor );.  if( u.
2681a 63 70 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77  cp.pCur->nullRow
2681b 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20   ){.    break;. 
2681c 20 7d 0a 20 20 75 2e 63 70 2e 70 56 74 61 62 20   }.  u.cp.pVtab 
2681d 3d 20 75 2e 63 70 2e 70 43 75 72 2d 3e 70 56 74  = u.cp.pCur->pVt
2681e 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b  abCursor->pVtab;
2681f 0a 20 20 75 2e 63 70 2e 70 4d 6f 64 75 6c 65 20  .  u.cp.pModule 
26820 3d 20 75 2e 63 70 2e 70 56 74 61 62 2d 3e 70 4d  = u.cp.pVtab->pM
26821 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28  odule;.  assert(
26822 20 75 2e 63 70 2e 70 4d 6f 64 75 6c 65 2d 3e 78   u.cp.pModule->x
26823 4e 65 78 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e  Next );..  /* In
26824 76 6f 6b 65 20 74 68 65 20 78 4e 65 78 74 28 29  voke the xNext()
26825 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 6d   method of the m
26826 6f 64 75 6c 65 2e 20 54 68 65 72 65 20 69 73 20  odule. There is 
26827 6e 6f 20 77 61 79 20 66 6f 72 20 74 68 65 0a 20  no way for the. 
26828 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 69   ** underlying i
26829 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f  mplementation to
2682a 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   return an error
2682b 20 69 66 20 6f 6e 65 20 6f 63 63 75 72 73 20 64   if one occurs d
2682c 75 72 69 6e 67 0a 20 20 2a 2a 20 78 4e 65 78 74  uring.  ** xNext
2682d 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20  (). Instead, if 
2682e 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
2682f 20 74 72 75 65 20 69 73 20 72 65 74 75 72 6e 65   true is returne
26830 64 20 28 69 6e 64 69 63 61 74 69 6e 67 20 74 68  d (indicating th
26831 61 74 0a 20 20 2a 2a 20 64 61 74 61 20 69 73 20  at.  ** data is 
26832 61 76 61 69 6c 61 62 6c 65 29 20 61 6e 64 20 74  available) and t
26833 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65  he error code re
26834 74 75 72 6e 65 64 20 77 68 65 6e 20 78 43 6f 6c  turned when xCol
26835 75 6d 6e 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d 65  umn or.  ** some
26836 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 20 69 73   other method is
26837 20 6e 65 78 74 20 69 6e 76 6f 6b 65 64 20 6f 6e   next invoked on
26838 20 74 68 65 20 73 61 76 65 20 76 69 72 74 75 61   the save virtua
26839 6c 20 74 61 62 6c 65 20 63 75 72 73 6f 72 2e 0a  l table cursor..
2683a 20 20 2a 2f 0a 20 20 70 2d 3e 69 6e 56 74 61 62    */.  p->inVtab
2683b 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 72 63  Method = 1;.  rc
2683c 20 3d 20 75 2e 63 70 2e 70 4d 6f 64 75 6c 65 2d   = u.cp.pModule-
2683d 3e 78 4e 65 78 74 28 75 2e 63 70 2e 70 43 75 72  >xNext(u.cp.pCur
2683e 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a  ->pVtabCursor);.
2683f 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f    p->inVtabMetho
26840 64 20 3d 20 30 3b 0a 20 20 69 6d 70 6f 72 74 56  d = 0;.  importV
26841 74 61 62 45 72 72 4d 73 67 28 70 2c 20 75 2e 63  tabErrMsg(p, u.c
26842 70 2e 70 56 74 61 62 29 3b 0a 20 20 69 66 28 20  p.pVtab);.  if( 
26843 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
26844 0a 20 20 20 20 75 2e 63 70 2e 72 65 73 20 3d 20  .    u.cp.res = 
26845 75 2e 63 70 2e 70 4d 6f 64 75 6c 65 2d 3e 78 45  u.cp.pModule->xE
26846 6f 66 28 75 2e 63 70 2e 70 43 75 72 2d 3e 70 56  of(u.cp.pCur->pV
26847 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a  tabCursor);.  }.
26848 0a 20 20 69 66 28 20 21 75 2e 63 70 2e 72 65 73  .  if( !u.cp.res
26849 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
2684a 65 72 65 20 69 73 20 64 61 74 61 2c 20 6a 75 6d  ere is data, jum
2684b 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 70  p to P2 */.    p
2684c 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
2684d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
2684e 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2684f 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
26850 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  LE */..#ifndef S
26851 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
26852 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64  ALTABLE./* Opcod
26853 65 3a 20 56 52 65 6e 61 6d 65 20 50 31 20 2a 20  e: VRename P1 * 
26854 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
26855 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
26856 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
26857 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74  object, an sqlit
26858 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72  e3_vtab structur
26859 65 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64  e..** This opcod
2685a 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f  e invokes the co
2685b 72 72 65 73 70 6f 6e 64 69 6e 67 20 78 52 65 6e  rresponding xRen
2685c 61 6d 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20  ame method. The 
2685d 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 72 65 67 69  value.** in regi
2685e 73 74 65 72 20 50 31 20 69 73 20 70 61 73 73 65  ster P1 is passe
2685f 64 20 61 73 20 74 68 65 20 7a 4e 61 6d 65 20 61  d as the zName a
26860 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 78  rgument to the x
26861 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 0a 2a  Rename method..*
26862 2f 0a 63 61 73 65 20 4f 50 5f 56 52 65 6e 61 6d  /.case OP_VRenam
26863 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  e: {.#if 0  /* l
26864 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
26865 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 71 20 2a  oved into u.cq *
26866 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  /.  sqlite3_vtab
26867 20 2a 70 56 74 61 62 3b 0a 20 20 4d 65 6d 20 2a   *pVtab;.  Mem *
26868 70 4e 61 6d 65 3b 0a 23 65 6e 64 69 66 20 2f 2a  pName;.#endif /*
26869 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
2686a 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 71   moved into u.cq
2686b 20 2a 2f 0a 0a 20 20 75 2e 63 71 2e 70 56 74 61   */..  u.cq.pVta
2686c 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61  b = pOp->p4.pVta
2686d 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 71  b->pVtab;.  u.cq
2686e 2e 70 4e 61 6d 65 20 3d 20 26 61 4d 65 6d 5b 70  .pName = &aMem[p
2686f 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
26870 74 28 20 75 2e 63 71 2e 70 56 74 61 62 2d 3e 70  t( u.cq.pVtab->p
26871 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 20  Module->xRename 
26872 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d  );.  assert( mem
26873 49 73 56 61 6c 69 64 28 75 2e 63 71 2e 70 4e 61  IsValid(u.cq.pNa
26874 6d 65 29 20 29 3b 0a 20 20 52 45 47 49 53 54 45  me) );.  REGISTE
26875 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c  R_TRACE(pOp->p1,
26876 20 75 2e 63 71 2e 70 4e 61 6d 65 29 3b 0a 20 20   u.cq.pName);.  
26877 61 73 73 65 72 74 28 20 75 2e 63 71 2e 70 4e 61  assert( u.cq.pNa
26878 6d 65 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  me->flags & MEM_
26879 53 74 72 20 29 3b 0a 20 20 74 65 73 74 63 61 73  Str );.  testcas
2687a 65 28 20 75 2e 63 71 2e 70 4e 61 6d 65 2d 3e 65  e( u.cq.pName->e
2687b 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
2687c 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 75  );.  testcase( u
2687d 2e 63 71 2e 70 4e 61 6d 65 2d 3e 65 6e 63 3d 3d  .cq.pName->enc==
2687e 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29  SQLITE_UTF16BE )
2687f 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 75 2e  ;.  testcase( u.
26880 63 71 2e 70 4e 61 6d 65 2d 3e 65 6e 63 3d 3d 53  cq.pName->enc==S
26881 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 3b  QLITE_UTF16LE );
26882 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56  .  rc = sqlite3V
26883 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
26884 67 28 75 2e 63 71 2e 70 4e 61 6d 65 2c 20 53 51  g(u.cq.pName, SQ
26885 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66  LITE_UTF8);.  if
26886 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
26887 29 7b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 71  ){.    rc = u.cq
26888 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d  .pVtab->pModule-
26889 3e 78 52 65 6e 61 6d 65 28 75 2e 63 71 2e 70 56  >xRename(u.cq.pV
2688a 74 61 62 2c 20 75 2e 63 71 2e 70 4e 61 6d 65 2d  tab, u.cq.pName-
2688b 3e 7a 29 3b 0a 20 20 20 20 69 6d 70 6f 72 74 56  >z);.    importV
2688c 74 61 62 45 72 72 4d 73 67 28 70 2c 20 75 2e 63  tabErrMsg(p, u.c
2688d 71 2e 70 56 74 61 62 29 3b 0a 20 20 20 20 70 2d  q.pVtab);.    p-
2688e 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 20 20  >expired = 0;.  
2688f 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e  }.  break;.}.#en
26890 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
26891 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
26892 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  TABLE./* Opcode:
26893 20 56 55 70 64 61 74 65 20 50 31 20 50 32 20 50   VUpdate P1 P2 P
26894 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  3 P4 *.**.** P4 
26895 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
26896 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
26897 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74  object, an sqlit
26898 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72  e3_vtab structur
26899 65 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64  e..** This opcod
2689a 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f  e invokes the co
2689b 72 72 65 73 70 6f 6e 64 69 6e 67 20 78 55 70 64  rresponding xUpd
2689c 61 74 65 20 6d 65 74 68 6f 64 2e 20 50 32 20 76  ate method. P2 v
2689d 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 63 6f 6e  alues.** are con
2689e 74 69 67 75 6f 75 73 20 6d 65 6d 6f 72 79 20 63  tiguous memory c
2689f 65 6c 6c 73 20 73 74 61 72 74 69 6e 67 20 61 74  ells starting at
268a0 20 50 33 20 74 6f 20 70 61 73 73 20 74 6f 20 74   P3 to pass to t
268a1 68 65 20 78 55 70 64 61 74 65 20 0a 2a 2a 20 69  he xUpdate .** i
268a2 6e 76 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20 76  nvocation. The v
268a3 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
268a4 20 28 50 33 2b 50 32 2d 31 29 20 63 6f 72 72 65   (P3+P2-1) corre
268a5 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 0a 2a  sponds to the .*
268a6 2a 20 70 32 74 68 20 65 6c 65 6d 65 6e 74 20 6f  * p2th element o
268a7 66 20 74 68 65 20 61 72 67 76 20 61 72 72 61 79  f the argv array
268a8 20 70 61 73 73 65 64 20 74 6f 20 78 55 70 64 61   passed to xUpda
268a9 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 55  te..**.** The xU
268aa 70 64 61 74 65 20 6d 65 74 68 6f 64 20 77 69 6c  pdate method wil
268ab 6c 20 64 6f 20 61 20 44 45 4c 45 54 45 20 6f 72  l do a DELETE or
268ac 20 61 6e 20 49 4e 53 45 52 54 20 6f 72 20 62 6f   an INSERT or bo
268ad 74 68 2e 0a 2a 2a 20 54 68 65 20 61 72 67 76 5b  th..** The argv[
268ae 30 5d 20 65 6c 65 6d 65 6e 74 20 28 77 68 69 63  0] element (whic
268af 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  h corresponds to
268b0 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 33 29   memory cell P3)
268b1 0a 2a 2a 20 69 73 20 74 68 65 20 72 6f 77 69 64  .** is the rowid
268b2 20 6f 66 20 61 20 72 6f 77 20 74 6f 20 64 65 6c   of a row to del
268b3 65 74 65 2e 20 20 49 66 20 61 72 67 76 5b 30 5d  ete.  If argv[0]
268b4 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f   is NULL then no
268b5 20 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 6f 63   .** deletion oc
268b6 63 75 72 73 2e 20 20 54 68 65 20 61 72 67 76 5b  curs.  The argv[
268b7 31 5d 20 65 6c 65 6d 65 6e 74 20 69 73 20 74 68  1] element is th
268b8 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6e  e rowid of the n
268b9 65 77 20 0a 2a 2a 20 72 6f 77 2e 20 20 54 68 69  ew .** row.  Thi
268ba 73 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 74 6f  s can be NULL to
268bb 20 68 61 76 65 20 74 68 65 20 76 69 72 74 75 61   have the virtua
268bc 6c 20 74 61 62 6c 65 20 73 65 6c 65 63 74 20 74  l table select t
268bd 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 69 64  he new .** rowid
268be 20 66 6f 72 20 69 74 73 65 6c 66 2e 20 20 54 68   for itself.  Th
268bf 65 20 73 75 62 73 65 71 75 65 6e 74 20 65 6c 65  e subsequent ele
268c0 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61 72 72  ments in the arr
268c1 61 79 20 61 72 65 20 0a 2a 2a 20 74 68 65 20 76  ay are .** the v
268c2 61 6c 75 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73  alues of columns
268c3 20 69 6e 20 74 68 65 20 6e 65 77 20 72 6f 77 2e   in the new row.
268c4 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 3d 3d 31 20  .**.** If P2==1 
268c5 74 68 65 6e 20 6e 6f 20 69 6e 73 65 72 74 20 69  then no insert i
268c6 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 61 72  s performed.  ar
268c7 67 76 5b 30 5d 20 69 73 20 74 68 65 20 72 6f 77  gv[0] is the row
268c8 69 64 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 74  id of.** a row t
268c9 6f 20 64 65 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20  o delete..**.** 
268ca 50 31 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20  P1 is a boolean 
268cb 66 6c 61 67 2e 20 49 66 20 69 74 20 69 73 20 73  flag. If it is s
268cc 65 74 20 74 6f 20 74 72 75 65 20 61 6e 64 20 74  et to true and t
268cd 68 65 20 78 55 70 64 61 74 65 20 63 61 6c 6c 0a  he xUpdate call.
268ce 2a 2a 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  ** is successful
268cf 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  , then the value
268d0 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
268d1 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
268d2 5f 72 6f 77 69 64 28 29 20 0a 2a 2a 20 69 73 20  _rowid() .** is 
268d3 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65  set to the value
268d4 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 66 6f   of the rowid fo
268d5 72 20 74 68 65 20 72 6f 77 20 6a 75 73 74 20 69  r the row just i
268d6 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 63 61 73 65  nserted..*/.case
268d7 20 4f 50 5f 56 55 70 64 61 74 65 3a 20 7b 0a 23   OP_VUpdate: {.#
268d8 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
268d9 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
268da 6e 74 6f 20 75 2e 63 72 20 2a 2f 0a 20 20 73 71  nto u.cr */.  sq
268db 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
268dc 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64  b;.  sqlite3_mod
268dd 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20  ule *pModule;.  
268de 69 6e 74 20 6e 41 72 67 3b 0a 20 20 69 6e 74 20  int nArg;.  int 
268df 69 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  i;.  sqlite_int6
268e0 34 20 72 6f 77 69 64 3b 0a 20 20 4d 65 6d 20 2a  4 rowid;.  Mem *
268e1 2a 61 70 41 72 67 3b 0a 20 20 4d 65 6d 20 2a 70  *apArg;.  Mem *p
268e2 58 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  X;.#endif /* loc
268e3 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
268e4 65 64 20 69 6e 74 6f 20 75 2e 63 72 20 2a 2f 0a  ed into u.cr */.
268e5 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
268e6 70 32 3d 3d 31 20 20 20 20 20 20 20 20 7c 7c 20  p2==1        || 
268e7 70 4f 70 2d 3e 70 35 3d 3d 4f 45 5f 46 61 69 6c  pOp->p5==OE_Fail
268e8 20 20 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d 3d 4f     || pOp->p5==O
268e9 45 5f 52 6f 6c 6c 62 61 63 6b 0a 20 20 20 20 20  E_Rollback.     
268ea 20 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d 3d 4f 45    || pOp->p5==OE
268eb 5f 41 62 6f 72 74 20 7c 7c 20 70 4f 70 2d 3e 70  _Abort || pOp->p
268ec 35 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20  5==OE_Ignore || 
268ed 70 4f 70 2d 3e 70 35 3d 3d 4f 45 5f 52 65 70 6c  pOp->p5==OE_Repl
268ee 61 63 65 0a 20 20 29 3b 0a 20 20 75 2e 63 72 2e  ace.  );.  u.cr.
268ef 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e  pVtab = pOp->p4.
268f0 70 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20  pVtab->pVtab;.  
268f1 75 2e 63 72 2e 70 4d 6f 64 75 6c 65 20 3d 20 28  u.cr.pModule = (
268f2 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
268f3 29 75 2e 63 72 2e 70 56 74 61 62 2d 3e 70 4d 6f  )u.cr.pVtab->pMo
268f4 64 75 6c 65 3b 0a 20 20 75 2e 63 72 2e 6e 41 72  dule;.  u.cr.nAr
268f5 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61  g = pOp->p2;.  a
268f6 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
268f7 70 65 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0a 20  pe==P4_VTAB );. 
268f8 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 63 72   if( ALWAYS(u.cr
268f9 2e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74  .pModule->xUpdat
268fa 65 29 20 29 7b 0a 20 20 20 20 75 38 20 76 74 61  e) ){.    u8 vta
268fb 62 4f 6e 43 6f 6e 66 6c 69 63 74 20 3d 20 64 62  bOnConflict = db
268fc 2d 3e 76 74 61 62 4f 6e 43 6f 6e 66 6c 69 63 74  ->vtabOnConflict
268fd 3b 0a 20 20 20 20 75 2e 63 72 2e 61 70 41 72 67  ;.    u.cr.apArg
268fe 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 20   = p->apArg;.   
268ff 20 75 2e 63 72 2e 70 58 20 3d 20 26 61 4d 65 6d   u.cr.pX = &aMem
26900 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 66  [pOp->p3];.    f
26901 6f 72 28 75 2e 63 72 2e 69 3d 30 3b 20 75 2e 63  or(u.cr.i=0; u.c
26902 72 2e 69 3c 75 2e 63 72 2e 6e 41 72 67 3b 20 75  r.i<u.cr.nArg; u
26903 2e 63 72 2e 69 2b 2b 29 7b 0a 20 20 20 20 20 20  .cr.i++){.      
26904 61 73 73 65 72 74 28 20 6d 65 6d 49 73 56 61 6c  assert( memIsVal
26905 69 64 28 75 2e 63 72 2e 70 58 29 20 29 3b 0a 20  id(u.cr.pX) );. 
26906 20 20 20 20 20 6d 65 6d 41 62 6f 75 74 54 6f 43       memAboutToC
26907 68 61 6e 67 65 28 70 2c 20 75 2e 63 72 2e 70 58  hange(p, u.cr.pX
26908 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26909 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65  VdbeMemStoreType
2690a 28 75 2e 63 72 2e 70 58 29 3b 0a 20 20 20 20 20  (u.cr.pX);.     
2690b 20 75 2e 63 72 2e 61 70 41 72 67 5b 75 2e 63 72   u.cr.apArg[u.cr
2690c 2e 69 5d 20 3d 20 75 2e 63 72 2e 70 58 3b 0a 20  .i] = u.cr.pX;. 
2690d 20 20 20 20 20 75 2e 63 72 2e 70 58 2b 2b 3b 0a       u.cr.pX++;.
2690e 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 76 74      }.    db->vt
2690f 61 62 4f 6e 43 6f 6e 66 6c 69 63 74 20 3d 20 70  abOnConflict = p
26910 4f 70 2d 3e 70 35 3b 0a 20 20 20 20 72 63 20 3d  Op->p5;.    rc =
26911 20 75 2e 63 72 2e 70 4d 6f 64 75 6c 65 2d 3e 78   u.cr.pModule->x
26912 55 70 64 61 74 65 28 75 2e 63 72 2e 70 56 74 61  Update(u.cr.pVta
26913 62 2c 20 75 2e 63 72 2e 6e 41 72 67 2c 20 75 2e  b, u.cr.nArg, u.
26914 63 72 2e 61 70 41 72 67 2c 20 26 75 2e 63 72 2e  cr.apArg, &u.cr.
26915 72 6f 77 69 64 29 3b 0a 20 20 20 20 64 62 2d 3e  rowid);.    db->
26916 76 74 61 62 4f 6e 43 6f 6e 66 6c 69 63 74 20 3d  vtabOnConflict =
26917 20 76 74 61 62 4f 6e 43 6f 6e 66 6c 69 63 74 3b   vtabOnConflict;
26918 0a 20 20 20 20 69 6d 70 6f 72 74 56 74 61 62 45  .    importVtabE
26919 72 72 4d 73 67 28 70 2c 20 75 2e 63 72 2e 70 56  rrMsg(p, u.cr.pV
2691a 74 61 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63  tab);.    if( rc
2691b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
2691c 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 20 20  Op->p1 ){.      
2691d 61 73 73 65 72 74 28 20 75 2e 63 72 2e 6e 41 72  assert( u.cr.nAr
2691e 67 3e 31 20 26 26 20 75 2e 63 72 2e 61 70 41 72  g>1 && u.cr.apAr
2691f 67 5b 30 5d 20 26 26 20 28 75 2e 63 72 2e 61 70  g[0] && (u.cr.ap
26920 41 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26 4d 45  Arg[0]->flags&ME
26921 4d 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 20 20 20  M_Null) );.     
26922 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d   db->lastRowid =
26923 20 6c 61 73 74 52 6f 77 69 64 20 3d 20 75 2e 63   lastRowid = u.c
26924 72 2e 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20  r.rowid;.    }. 
26925 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
26926 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 26 26 20  E_CONSTRAINT && 
26927 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 62  pOp->p4.pVtab->b
26928 43 6f 6e 73 74 72 61 69 6e 74 20 29 7b 0a 20 20  Constraint ){.  
26929 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 3d      if( pOp->p5=
2692a 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20  =OE_Ignore ){.  
2692b 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
2692c 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  E_OK;.      }els
2692d 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 72  e{.        p->er
2692e 72 6f 72 41 63 74 69 6f 6e 20 3d 20 28 28 70 4f  rorAction = ((pO
2692f 70 2d 3e 70 35 3d 3d 4f 45 5f 52 65 70 6c 61 63  p->p5==OE_Replac
26930 65 29 20 3f 20 4f 45 5f 41 62 6f 72 74 20 3a 20  e) ? OE_Abort : 
26931 70 4f 70 2d 3e 70 35 29 3b 0a 20 20 20 20 20 20  pOp->p5);.      
26932 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
26933 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b     p->nChange++;
26934 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65  .    }.  }.  bre
26935 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
26936 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
26937 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66  UALTABLE */..#if
26938 6e 64 65 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49  ndef  SQLITE_OMI
26939 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a  T_PAGER_PRAGMAS.
2693a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61 67 65 63  /* Opcode: Pagec
2693b 6f 75 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  ount P1 P2 * * *
2693c 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  .**.** Write the
2693d 20 63 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20   current number 
2693e 6f 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 61  of pages in data
2693f 62 61 73 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72  base P1 to memor
26940 79 20 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61  y cell P2..*/.ca
26941 73 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a  se OP_Pagecount:
26942 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a   {            /*
26943 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
26944 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20   */.  pOut->u.i 
26945 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61  = sqlite3BtreeLa
26946 73 74 50 61 67 65 28 64 62 2d 3e 61 44 62 5b 70  stPage(db->aDb[p
26947 4f 70 2d 3e 70 31 5d 2e 70 42 74 29 3b 0a 20 20  Op->p1].pBt);.  
26948 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  break;.}.#endif.
26949 0a 0a 23 69 66 6e 64 65 66 20 20 53 51 4c 49 54  ..#ifndef  SQLIT
2694a 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
2694b 47 4d 41 53 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  GMAS./* Opcode: 
2694c 4d 61 78 50 67 63 6e 74 20 50 31 20 50 32 20 50  MaxPgcnt P1 P2 P
2694d 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 72 79 20  3 * *.**.** Try 
2694e 74 6f 20 73 65 74 20 74 68 65 20 6d 61 78 69 6d  to set the maxim
2694f 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 66 6f  um page count fo
26950 72 20 64 61 74 61 62 61 73 65 20 50 31 20 74 6f  r database P1 to
26951 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 50 33   the value in P3
26952 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20 6c 65 74 20  ..** Do not let 
26953 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65  the maximum page
26954 20 63 6f 75 6e 74 20 66 61 6c 6c 20 62 65 6c 6f   count fall belo
26955 77 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61  w the current pa
26956 67 65 20 63 6f 75 6e 74 20 61 6e 64 0a 2a 2a 20  ge count and.** 
26957 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68  do not change th
26958 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63  e maximum page c
26959 6f 75 6e 74 20 76 61 6c 75 65 20 69 66 20 50 33  ount value if P3
2695a 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65  ==0..**.** Store
2695b 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67   the maximum pag
2695c 65 20 63 6f 75 6e 74 20 61 66 74 65 72 20 74 68  e count after th
2695d 65 20 63 68 61 6e 67 65 20 69 6e 20 72 65 67 69  e change in regi
2695e 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65  ster P2..*/.case
2695f 20 4f 50 5f 4d 61 78 50 67 63 6e 74 3a 20 7b 20   OP_MaxPgcnt: { 
26960 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75             /* ou
26961 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f  t2-prerelease */
26962 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
26963 6e 65 77 4d 61 78 3b 0a 20 20 42 74 72 65 65 20  newMax;.  Btree 
26964 2a 70 42 74 3b 0a 0a 20 20 70 42 74 20 3d 20 64  *pBt;..  pBt = d
26965 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e  b->aDb[pOp->p1].
26966 70 42 74 3b 0a 20 20 6e 65 77 4d 61 78 20 3d 20  pBt;.  newMax = 
26967 30 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 33  0;.  if( pOp->p3
26968 20 29 7b 0a 20 20 20 20 6e 65 77 4d 61 78 20 3d   ){.    newMax =
26969 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73   sqlite3BtreeLas
2696a 74 50 61 67 65 28 70 42 74 29 3b 0a 20 20 20 20  tPage(pBt);.    
2696b 69 66 28 20 6e 65 77 4d 61 78 20 3c 20 28 75 6e  if( newMax < (un
2696c 73 69 67 6e 65 64 29 70 4f 70 2d 3e 70 33 20 29  signed)pOp->p3 )
2696d 20 6e 65 77 4d 61 78 20 3d 20 28 75 6e 73 69 67   newMax = (unsig
2696e 6e 65 64 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 7d  ned)pOp->p3;.  }
2696f 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 73  .  pOut->u.i = s
26970 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61  qlite3BtreeMaxPa
26971 67 65 43 6f 75 6e 74 28 70 42 74 2c 20 6e 65 77  geCount(pBt, new
26972 4d 61 78 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Max);.  break;.}
26973 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65  .#endif...#ifnde
26974 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
26975 41 43 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54  ACE./* Opcode: T
26976 72 61 63 65 20 2a 20 2a 20 2a 20 50 34 20 2a 0a  race * * * P4 *.
26977 2a 2a 0a 2a 2a 20 49 66 20 74 72 61 63 69 6e 67  **.** If tracing
26978 20 69 73 20 65 6e 61 62 6c 65 64 20 28 62 79 20   is enabled (by 
26979 74 68 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63  the sqlite3_trac
2697a 65 28 29 29 20 69 6e 74 65 72 66 61 63 65 2c 20  e()) interface, 
2697b 74 68 65 6e 0a 2a 2a 20 74 68 65 20 55 54 46 2d  then.** the UTF-
2697c 38 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e  8 string contain
2697d 65 64 20 69 6e 20 50 34 20 69 73 20 65 6d 69 74  ed in P4 is emit
2697e 74 65 64 20 6f 6e 20 74 68 65 20 74 72 61 63 65  ted on the trace
2697f 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61   callback..*/.ca
26980 73 65 20 4f 50 5f 54 72 61 63 65 3a 20 7b 0a 23  se OP_Trace: {.#
26981 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
26982 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
26983 6e 74 6f 20 75 2e 63 73 20 2a 2f 0a 20 20 63 68  nto u.cs */.  ch
26984 61 72 20 2a 7a 54 72 61 63 65 3b 0a 20 20 63 68  ar *zTrace;.  ch
26985 61 72 20 2a 7a 3b 0a 23 65 6e 64 69 66 20 2f 2a  ar *z;.#endif /*
26986 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
26987 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 73   moved into u.cs
26988 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62 2d 3e 78   */..  if( db->x
26989 54 72 61 63 65 0a 20 20 20 26 26 20 21 70 2d 3e  Trace.   && !p->
2698a 64 6f 69 6e 67 52 65 72 75 6e 0a 20 20 20 26 26  doingRerun.   &&
2698b 20 28 75 2e 63 73 2e 7a 54 72 61 63 65 20 3d 20   (u.cs.zTrace = 
2698c 28 70 4f 70 2d 3e 70 34 2e 7a 20 3f 20 70 4f 70  (pOp->p4.z ? pOp
2698d 2d 3e 70 34 2e 7a 20 3a 20 70 2d 3e 7a 53 71 6c  ->p4.z : p->zSql
2698e 29 29 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 75  ))!=0.  ){.    u
2698f 2e 63 73 2e 7a 20 3d 20 73 71 6c 69 74 65 33 56  .cs.z = sqlite3V
26990 64 62 65 45 78 70 61 6e 64 53 71 6c 28 70 2c 20  dbeExpandSql(p, 
26991 75 2e 63 73 2e 7a 54 72 61 63 65 29 3b 0a 20 20  u.cs.zTrace);.  
26992 20 20 64 62 2d 3e 78 54 72 61 63 65 28 64 62 2d    db->xTrace(db-
26993 3e 70 54 72 61 63 65 41 72 67 2c 20 75 2e 63 73  >pTraceArg, u.cs
26994 2e 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  .z);.    sqlite3
26995 44 62 46 72 65 65 28 64 62 2c 20 75 2e 63 73 2e  DbFree(db, u.cs.
26996 7a 29 3b 0a 20 20 7d 0a 23 69 66 64 65 66 20 53  z);.  }.#ifdef S
26997 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66  QLITE_DEBUG.  if
26998 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  ( (db->flags & S
26999 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 29 21  QLITE_SqlTrace)!
2699a 3d 30 0a 20 20 20 26 26 20 28 75 2e 63 73 2e 7a  =0.   && (u.cs.z
2699b 54 72 61 63 65 20 3d 20 28 70 4f 70 2d 3e 70 34  Trace = (pOp->p4
2699c 2e 7a 20 3f 20 70 4f 70 2d 3e 70 34 2e 7a 20 3a  .z ? pOp->p4.z :
2699d 20 70 2d 3e 7a 53 71 6c 29 29 21 3d 30 0a 20 20   p->zSql))!=0.  
2699e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
2699f 62 75 67 50 72 69 6e 74 66 28 22 53 51 4c 2d 74  bugPrintf("SQL-t
269a0 72 61 63 65 3a 20 25 73 5c 6e 22 2c 20 75 2e 63  race: %s\n", u.c
269a1 73 2e 7a 54 72 61 63 65 29 3b 0a 20 20 7d 0a 23  s.zTrace);.  }.#
269a2 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
269a3 44 45 42 55 47 20 2a 2f 0a 20 20 62 72 65 61 6b  DEBUG */.  break
269a4 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20  ;.}.#endif.../* 
269a5 4f 70 63 6f 64 65 3a 20 4e 6f 6f 70 20 2a 20 2a  Opcode: Noop * *
269a6 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 6f 20   * * *.**.** Do 
269a7 6e 6f 74 68 69 6e 67 2e 20 20 54 68 69 73 20 69  nothing.  This i
269a8 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 66  nstruction is of
269a9 74 65 6e 20 75 73 65 66 75 6c 20 61 73 20 61 20  ten useful as a 
269aa 6a 75 6d 70 0a 2a 2a 20 64 65 73 74 69 6e 61 74  jump.** destinat
269ab 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68  ion..*/./*.** Th
269ac 65 20 6d 61 67 69 63 20 45 78 70 6c 61 69 6e 20  e magic Explain 
269ad 6f 70 63 6f 64 65 20 61 72 65 20 6f 6e 6c 79 20  opcode are only 
269ae 69 6e 73 65 72 74 65 64 20 77 68 65 6e 20 65 78  inserted when ex
269af 70 6c 61 69 6e 3d 3d 32 20 28 77 68 69 63 68 0a  plain==2 (which.
269b0 2a 2a 20 69 73 20 74 6f 20 73 61 79 20 77 68 65  ** is to say whe
269b1 6e 20 74 68 65 20 45 58 50 4c 41 49 4e 20 51 55  n the EXPLAIN QU
269b2 45 52 59 20 50 4c 41 4e 20 73 79 6e 74 61 78 20  ERY PLAN syntax 
269b3 69 73 20 75 73 65 64 2e 29 0a 2a 2a 20 54 68 69  is used.).** Thi
269b4 73 20 6f 70 63 6f 64 65 20 72 65 63 6f 72 64 73  s opcode records
269b5 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
269b6 6d 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e  m the optimizer.
269b7 20 20 49 74 20 69 73 20 74 68 65 0a 2a 2a 20 74    It is the.** t
269b8 68 65 20 73 61 6d 65 20 61 73 20 61 20 6e 6f 2d  he same as a no-
269b9 6f 70 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65  op.  This opcode
269ba 73 6e 65 76 65 72 20 61 70 70 65 61 72 73 20 69  snever appears i
269bb 6e 20 61 20 72 65 61 6c 20 56 4d 20 70 72 6f 67  n a real VM prog
269bc 72 61 6d 2e 0a 2a 2f 0a 64 65 66 61 75 6c 74 3a  ram..*/.default:
269bd 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54   {          /* T
269be 68 69 73 20 69 73 20 72 65 61 6c 6c 79 20 4f 50  his is really OP
269bf 5f 4e 6f 6f 70 20 61 6e 64 20 4f 50 5f 45 78 70  _Noop and OP_Exp
269c0 6c 61 69 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74  lain */.  assert
269c1 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
269c2 50 5f 4e 6f 6f 70 20 7c 7c 20 70 4f 70 2d 3e 6f  P_Noop || pOp->o
269c3 70 63 6f 64 65 3d 3d 4f 50 5f 45 78 70 6c 61 69  pcode==OP_Explai
269c4 6e 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  n );.  break;.}.
269c5 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
269c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
269c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
269c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
269c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
269ca 2a 2a 20 54 68 65 20 63 61 73 65 73 20 6f 66 20  ** The cases of 
269cb 74 68 65 20 73 77 69 74 63 68 20 73 74 61 74 65  the switch state
269cc 6d 65 6e 74 20 61 62 6f 76 65 20 74 68 69 73 20  ment above this 
269cd 6c 69 6e 65 20 73 68 6f 75 6c 64 20 61 6c 6c 20  line should all 
269ce 62 65 20 69 6e 64 65 6e 74 65 64 0a 2a 2a 20 62  be indented.** b
269cf 79 20 36 20 73 70 61 63 65 73 2e 20 20 42 75 74  y 6 spaces.  But
269d0 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 36   the left-most 6
269d1 20 73 70 61 63 65 73 20 68 61 76 65 20 62 65 65   spaces have bee
269d2 6e 20 72 65 6d 6f 76 65 64 20 74 6f 20 69 6d 70  n removed to imp
269d3 72 6f 76 65 20 74 68 65 0a 2a 2a 20 72 65 61 64  rove the.** read
269d4 61 62 69 6c 69 74 79 2e 20 20 46 72 6f 6d 20 74  ability.  From t
269d5 68 69 73 20 70 6f 69 6e 74 20 6f 6e 20 64 6f 77  his point on dow
269d6 6e 2c 20 74 68 65 20 6e 6f 72 6d 61 6c 20 69 6e  n, the normal in
269d7 64 65 6e 74 61 74 69 6f 6e 20 72 75 6c 65 73 20  dentation rules 
269d8 61 72 65 0a 2a 2a 20 72 65 73 74 6f 72 65 64 2e  are.** restored.
269d9 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
269da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
269db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
269dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
269dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
269de 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 56 44      }..#ifdef VD
269df 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 7b  BE_PROFILE.    {
269e0 0a 20 20 20 20 20 20 75 36 34 20 65 6c 61 70 73  .      u64 elaps
269e1 65 64 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69  ed = sqlite3Hwti
269e2 6d 65 28 29 20 2d 20 73 74 61 72 74 3b 0a 20 20  me() - start;.  
269e3 20 20 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 20      pOp->cycles 
269e4 2b 3d 20 65 6c 61 70 73 65 64 3b 0a 20 20 20 20  += elapsed;.    
269e5 20 20 70 4f 70 2d 3e 63 6e 74 2b 2b 3b 0a 23 69    pOp->cnt++;.#i
269e6 66 20 30 0a 20 20 20 20 20 20 20 20 66 70 72 69  f 0.        fpri
269e7 6e 74 66 28 73 74 64 6f 75 74 2c 20 22 25 31 30  ntf(stdout, "%10
269e8 6c 6c 75 20 22 2c 20 65 6c 61 70 73 65 64 29 3b  llu ", elapsed);
269e9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
269ea 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f  VdbePrintOp(stdo
269eb 75 74 2c 20 6f 72 69 67 50 63 2c 20 26 61 4f 70  ut, origPc, &aOp
269ec 5b 6f 72 69 67 50 63 5d 29 3b 0a 23 65 6e 64 69  [origPc]);.#endi
269ed 66 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  f.    }.#endif..
269ee 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f      /* The follo
269ef 77 69 6e 67 20 63 6f 64 65 20 61 64 64 73 20 6e  wing code adds n
269f0 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 20 61 63  othing to the ac
269f1 74 75 61 6c 20 66 75 6e 63 74 69 6f 6e 61 6c 69  tual functionali
269f2 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  ty.    ** of the
269f3 20 70 72 6f 67 72 61 6d 2e 20 20 49 74 20 69 73   program.  It is
269f4 20 6f 6e 6c 79 20 68 65 72 65 20 66 6f 72 20 74   only here for t
269f5 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67  esting and debug
269f6 67 69 6e 67 2e 0a 20 20 20 20 2a 2a 20 4f 6e 20  ging..    ** On 
269f7 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20  the other hand, 
269f8 69 74 20 64 6f 65 73 20 62 75 72 6e 20 43 50 55  it does burn CPU
269f9 20 63 79 63 6c 65 73 20 65 76 65 72 79 20 74 69   cycles every ti
269fa 6d 65 20 74 68 72 6f 75 67 68 0a 20 20 20 20 2a  me through.    *
269fb 2a 20 74 68 65 20 65 76 61 6c 75 61 74 6f 72 20  * the evaluator 
269fc 6c 6f 6f 70 2e 20 20 53 6f 20 77 65 20 63 61 6e  loop.  So we can
269fd 20 6c 65 61 76 65 20 69 74 20 6f 75 74 20 77 68   leave it out wh
269fe 65 6e 20 4e 44 45 42 55 47 20 69 73 20 64 65 66  en NDEBUG is def
269ff 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66  ined..    */.#if
26a00 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  ndef NDEBUG.    
26a01 61 73 73 65 72 74 28 20 70 63 3e 3d 2d 31 20 26  assert( pc>=-1 &
26a02 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 0a  & pc<p->nOp );..
26a03 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
26a04 42 55 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74  BUG.    if( p->t
26a05 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66  race ){.      if
26a06 28 20 72 63 21 3d 30 20 29 20 66 70 72 69 6e 74  ( rc!=0 ) fprint
26a07 66 28 70 2d 3e 74 72 61 63 65 2c 22 72 63 3d 25  f(p->trace,"rc=%
26a08 64 5c 6e 22 2c 72 63 29 3b 0a 20 20 20 20 20 20  d\n",rc);.      
26a09 69 66 28 20 70 4f 70 2d 3e 6f 70 66 6c 61 67 73  if( pOp->opflags
26a0a 20 26 20 28 4f 50 46 4c 47 5f 4f 55 54 32 5f 50   & (OPFLG_OUT2_P
26a0b 52 45 52 45 4c 45 41 53 45 7c 4f 50 46 4c 47 5f  RERELEASE|OPFLG_
26a0c 4f 55 54 32 29 20 29 7b 0a 20 20 20 20 20 20 20  OUT2) ){.       
26a0d 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28 70   registerTrace(p
26a0e 2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70 32  ->trace, pOp->p2
26a0f 2c 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  , &aMem[pOp->p2]
26a10 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
26a11 20 69 66 28 20 70 4f 70 2d 3e 6f 70 66 6c 61 67   if( pOp->opflag
26a12 73 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 20 29  s & OPFLG_OUT3 )
26a13 7b 0a 20 20 20 20 20 20 20 20 72 65 67 69 73 74  {.        regist
26a14 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65  erTrace(p->trace
26a15 2c 20 70 4f 70 2d 3e 70 33 2c 20 26 61 4d 65 6d  , pOp->p3, &aMem
26a16 5b 70 4f 70 2d 3e 70 33 5d 29 3b 0a 20 20 20 20  [pOp->p3]);.    
26a17 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66    }.    }.#endif
26a18 20 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55    /* SQLITE_DEBU
26a19 47 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20  G */.#endif  /* 
26a1a 4e 44 45 42 55 47 20 2a 2f 0a 20 20 7d 20 20 2f  NDEBUG */.  }  /
26a1b 2a 20 54 68 65 20 65 6e 64 20 6f 66 20 74 68 65  * The end of the
26a1c 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 20 74 68   for(;;) loop th
26a1d 65 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20  e loops through 
26a1e 6f 70 63 6f 64 65 73 20 2a 2f 0a 0a 20 20 2f 2a  opcodes */..  /*
26a1f 20 49 66 20 77 65 20 72 65 61 63 68 20 74 68 69   If we reach thi
26a20 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d 65 61 6e  s point, it mean
26a21 73 20 74 68 61 74 20 65 78 65 63 75 74 69 6f 6e  s that execution
26a22 20 69 73 20 66 69 6e 69 73 68 65 64 20 77 69 74   is finished wit
26a23 68 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20  h.  ** an error 
26a24 6f 66 20 73 6f 6d 65 20 6b 69 6e 64 2e 0a 20 20  of some kind..  
26a25 2a 2f 0a 76 64 62 65 5f 65 72 72 6f 72 5f 68 61  */.vdbe_error_ha
26a26 6c 74 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63  lt:.  assert( rc
26a27 20 29 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63   );.  p->rc = rc
26a28 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 73 71  ;.  testcase( sq
26a29 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
26a2a 67 2e 78 4c 6f 67 21 3d 30 20 29 3b 0a 20 20 73  g.xLog!=0 );.  s
26a2b 71 6c 69 74 65 33 5f 6c 6f 67 28 72 63 2c 20 22  qlite3_log(rc, "
26a2c 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 72 74 73  statement aborts
26a2d 20 61 74 20 25 64 3a 20 5b 25 73 5d 20 25 73 22   at %d: [%s] %s"
26a2e 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
26a2f 20 20 20 20 20 20 70 63 2c 20 70 2d 3e 7a 53 71        pc, p->zSq
26a30 6c 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  l, p->zErrMsg);.
26a31 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c    sqlite3VdbeHal
26a32 74 28 70 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  t(p);.  if( rc==
26a33 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
26a34 45 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  EM ) db->mallocF
26a35 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 72 63 20  ailed = 1;.  rc 
26a36 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
26a37 20 20 69 66 28 20 72 65 73 65 74 53 63 68 65 6d    if( resetSchem
26a38 61 4f 6e 46 61 75 6c 74 3e 30 20 29 7b 0a 20 20  aOnFault>0 ){.  
26a39 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 4f 6e    sqlite3ResetOn
26a3a 65 53 63 68 65 6d 61 28 64 62 2c 20 72 65 73 65  eSchema(db, rese
26a3b 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74 2d 31  tSchemaOnFault-1
26a3c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69  );.  }..  /* Thi
26a3d 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61  s is the only wa
26a3e 79 20 6f 75 74 20 6f 66 20 74 68 69 73 20 70 72  y out of this pr
26a3f 6f 63 65 64 75 72 65 2e 20 20 57 65 20 68 61 76  ocedure.  We hav
26a40 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65 61 73  e to.  ** releas
26a41 65 20 74 68 65 20 6d 75 74 65 78 65 73 20 6f 6e  e the mutexes on
26a42 20 62 74 72 65 65 73 20 74 68 61 74 20 77 65 72   btrees that wer
26a43 65 20 61 63 71 75 69 72 65 64 20 61 74 20 74 68  e acquired at th
26a44 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f 0a 76  e.  ** top. */.v
26a45 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20 64 62  dbe_return:.  db
26a46 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 6c 61  ->lastRowid = la
26a47 73 74 52 6f 77 69 64 3b 0a 20 20 73 71 6c 69 74  stRowid;.  sqlit
26a48 65 33 56 64 62 65 4c 65 61 76 65 28 70 29 3b 0a  e3VdbeLeave(p);.
26a49 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20    return rc;..  
26a4a 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20  /* Jump to here 
26a4b 69 66 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62  if a string or b
26a4c 6c 6f 62 20 6c 61 72 67 65 72 20 74 68 61 6e 20  lob larger than 
26a4d 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
26a4e 48 0a 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75 6e  H.  ** is encoun
26a4f 74 65 72 65 64 2e 0a 20 20 2a 2f 0a 74 6f 6f 5f  tered..  */.too_
26a50 62 69 67 3a 0a 20 20 73 71 6c 69 74 65 33 53 65  big:.  sqlite3Se
26a51 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
26a52 4d 73 67 2c 20 64 62 2c 20 22 73 74 72 69 6e 67  Msg, db, "string
26a53 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67   or blob too big
26a54 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54  ");.  rc = SQLIT
26a55 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 67 6f 74 6f  E_TOOBIG;.  goto
26a56 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74   vdbe_error_halt
26a57 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  ;..  /* Jump to 
26a58 68 65 72 65 20 69 66 20 61 20 6d 61 6c 6c 6f 63  here if a malloc
26a59 28 29 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 6e  () fails..  */.n
26a5a 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c  o_mem:.  db->mal
26a5b 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
26a5c 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
26a5d 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
26a5e 62 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  b, "out of memor
26a5f 79 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49  y");.  rc = SQLI
26a60 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 67 6f 74 6f  TE_NOMEM;.  goto
26a61 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74   vdbe_error_halt
26a62 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  ;..  /* Jump to 
26a63 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f 74 68  here for any oth
26a64 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74 61 6c  er kind of fatal
26a65 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22 72 63   error.  The "rc
26a66 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a 20  " variable.  ** 
26a67 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20  should hold the 
26a68 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a 20 20  error number..  
26a69 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  */.abort_due_to_
26a6a 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 74 28  error:.  assert(
26a6b 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 29   p->zErrMsg==0 )
26a6c 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
26a6d 6f 63 46 61 69 6c 65 64 20 29 20 72 63 20 3d 20  ocFailed ) rc = 
26a6e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
26a6f 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49  if( rc!=SQLITE_I
26a70 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  OERR_NOMEM ){.  
26a71 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
26a72 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
26a73 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
26a74 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20  3ErrStr(rc));.  
26a75 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72  }.  goto vdbe_er
26a76 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20  ror_halt;..  /* 
26a77 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20  Jump to here if 
26a78 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  the sqlite3_inte
26a79 72 72 75 70 74 28 29 20 41 50 49 20 73 65 74 73  rrupt() API sets
26a7a 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 0a 20   the interrupt. 
26a7b 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a 61   ** flag..  */.a
26a7c 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65  bort_due_to_inte
26a7d 72 72 75 70 74 3a 0a 20 20 61 73 73 65 72 74 28  rrupt:.  assert(
26a7e 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
26a7f 75 70 74 65 64 20 29 3b 0a 20 20 72 63 20 3d 20  upted );.  rc = 
26a80 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
26a81 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a  ;.  p->rc = rc;.
26a82 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
26a83 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
26a84 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
26a85 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20  3ErrStr(rc));.  
26a86 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f  goto vdbe_error_
26a87 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  halt;.}../******
26a88 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
26a89 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  vdbe.c *********
26a8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a8c 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
26a8d 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
26a8e 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a  ile vdbeblob.c *
26a8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a91 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
26a92 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a 2a 20  007 May 1.**.** 
26a93 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
26a94 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
26a95 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
26a96 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
26a97 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
26a98 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
26a99 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
26a9a 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
26a9b 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
26a9c 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
26a9d 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
26a9e 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
26a9f 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
26aa0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
26aa1 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
26aa2 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
26aa3 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
26aa4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26aa5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26aa6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26aa7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26aa8 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
26aa9 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
26aaa 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69  s code used to i
26aab 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65 6d 65  mplement increme
26aac 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e 0a 2a  ntal BLOB I/O..*
26aad 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  /...#ifndef SQLI
26aae 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
26aaf 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 73 71  ../*.** Valid sq
26ab0 6c 69 74 65 33 5f 62 6c 6f 62 2a 20 68 61 6e 64  lite3_blob* hand
26ab1 6c 65 73 20 70 6f 69 6e 74 20 74 6f 20 49 6e 63  les point to Inc
26ab2 72 62 6c 6f 62 20 73 74 72 75 63 74 75 72 65 73  rblob structures
26ab3 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
26ab4 75 63 74 20 49 6e 63 72 62 6c 6f 62 20 49 6e 63  uct Incrblob Inc
26ab5 72 62 6c 6f 62 3b 0a 73 74 72 75 63 74 20 49 6e  rblob;.struct In
26ab6 63 72 62 6c 6f 62 20 7b 0a 20 20 69 6e 74 20 66  crblob {.  int f
26ab7 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
26ab8 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 22 66     /* Copy of "f
26ab9 6c 61 67 73 22 20 70 61 73 73 65 64 20 74 6f 20  lags" passed to 
26aba 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
26abb 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  n() */.  int nBy
26abc 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  te;             
26abd 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 70 65 6e   /* Size of open
26abe 20 62 6c 6f 62 2c 20 69 6e 20 62 79 74 65 73 20   blob, in bytes 
26abf 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74  */.  int iOffset
26ac0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
26ac1 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 62  Byte offset of b
26ac2 6c 6f 62 20 69 6e 20 63 75 72 73 6f 72 20 64 61  lob in cursor da
26ac3 74 61 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c  ta */.  int iCol
26ac4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26ac5 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d 6e 20  /* Table column 
26ac6 74 68 69 73 20 68 61 6e 64 6c 65 20 69 73 20 6f  this handle is o
26ac7 70 65 6e 20 6f 6e 20 2a 2f 0a 20 20 42 74 43 75  pen on */.  BtCu
26ac8 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 20 20  rsor *pCsr;     
26ac9 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f      /* Cursor po
26aca 69 6e 74 69 6e 67 20 61 74 20 62 6c 6f 62 20 72  inting at blob r
26acb 6f 77 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ow */.  sqlite3_
26acc 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20  stmt *pStmt;    
26acd 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 68 6f 6c  /* Statement hol
26ace 64 69 6e 67 20 63 75 72 73 6f 72 20 6f 70 65 6e  ding cursor open
26acf 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
26ad0 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b;            /*
26ad1 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 20   The associated 
26ad2 64 61 74 61 62 61 73 65 20 2a 2f 0a 7d 3b 0a 0a  database */.};..
26ad3 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
26ad4 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20  tion is used by 
26ad5 62 6f 74 68 20 62 6c 6f 62 5f 6f 70 65 6e 28 29  both blob_open()
26ad6 20 61 6e 64 20 62 6c 6f 62 5f 72 65 6f 70 65 6e   and blob_reopen
26ad7 28 29 2e 20 49 74 20 73 65 65 6b 73 0a 2a 2a 20  (). It seeks.** 
26ad8 74 68 65 20 62 2d 74 72 65 65 20 63 75 72 73 6f  the b-tree curso
26ad9 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  r associated wit
26ada 68 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 70 20  h blob handle p 
26adb 74 6f 20 70 6f 69 6e 74 20 74 6f 20 72 6f 77 20  to point to row 
26adc 69 52 6f 77 2e 0a 2a 2a 20 49 66 20 73 75 63 63  iRow..** If succ
26add 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f  essful, SQLITE_O
26ade 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  K is returned an
26adf 64 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  d subsequent cal
26ae0 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  ls to.** sqlite3
26ae1 5f 62 6c 6f 62 5f 72 65 61 64 28 29 20 6f 72 20  _blob_read() or 
26ae2 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69  sqlite3_blob_wri
26ae3 74 65 28 29 20 61 63 63 65 73 73 20 74 68 65 20  te() access the 
26ae4 73 70 65 63 69 66 69 65 64 20 72 6f 77 2e 0a 2a  specified row..*
26ae5 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
26ae6 20 6f 63 63 75 72 73 2c 20 6f 72 20 69 66 20 74   occurs, or if t
26ae7 68 65 20 73 70 65 63 69 66 69 65 64 20 72 6f 77  he specified row
26ae8 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
26ae9 6f 72 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63  or does not.** c
26aea 6f 6e 74 61 69 6e 20 61 20 76 61 6c 75 65 20 6f  ontain a value o
26aeb 66 20 74 79 70 65 20 54 45 58 54 20 6f 72 20 42  f type TEXT or B
26aec 4c 4f 42 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d  LOB in the colum
26aed 6e 20 6e 6f 6d 69 6e 61 74 65 64 20 77 68 65 6e  n nominated when
26aee 20 74 68 65 0a 2a 2a 20 62 6c 6f 62 20 68 61 6e   the.** blob han
26aef 64 6c 65 20 77 61 73 20 6f 70 65 6e 65 64 2c 20  dle was opened, 
26af0 74 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f  then an error co
26af1 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  de is returned a
26af2 6e 64 20 2a 70 7a 45 72 72 20 6d 61 79 0a 2a 2a  nd *pzErr may.**
26af3 20 62 65 20 73 65 74 20 74 6f 20 70 6f 69 6e 74   be set to point
26af4 20 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f 6e   to a buffer con
26af5 74 61 69 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72  taining an error
26af6 20 6d 65 73 73 61 67 65 2e 20 49 74 20 69 73 20   message. It is 
26af7 74 68 65 0a 2a 2a 20 72 65 73 70 6f 6e 73 69 62  the.** responsib
26af8 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c  ility of the cal
26af9 6c 65 72 20 74 6f 20 66 72 65 65 20 74 68 65 20  ler to free the 
26afa 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 75  error message bu
26afb 66 66 65 72 20 75 73 69 6e 67 0a 2a 2a 20 73 71  ffer using.** sq
26afc 6c 69 74 65 33 44 62 46 72 65 65 28 29 2e 0a 2a  lite3DbFree()..*
26afd 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
26afe 20 64 6f 65 73 20 6f 63 63 75 72 2c 20 74 68 65   does occur, the
26aff 6e 20 74 68 65 20 62 2d 74 72 65 65 20 63 75 72  n the b-tree cur
26b00 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 20 41  sor is closed. A
26b01 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a  ll subsequent.**
26b02 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
26b03 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 2c 20 62  3_blob_read(), b
26b04 6c 6f 62 5f 77 72 69 74 65 28 29 20 6f 72 20 62  lob_write() or b
26b05 6c 6f 62 5f 72 65 6f 70 65 6e 28 29 20 77 69 6c  lob_reopen() wil
26b06 6c 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  l .** immediatel
26b07 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  y return SQLITE_
26b08 41 42 4f 52 54 2e 0a 2a 2f 0a 73 74 61 74 69 63  ABORT..*/.static
26b09 20 69 6e 74 20 62 6c 6f 62 53 65 65 6b 54 6f 52   int blobSeekToR
26b0a 6f 77 28 49 6e 63 72 62 6c 6f 62 20 2a 70 2c 20  ow(Incrblob *p, 
26b0b 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52  sqlite3_int64 iR
26b0c 6f 77 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  ow, char **pzErr
26b0d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
26b0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b0f 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f       /* Error co
26b10 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45  de */.  char *zE
26b11 72 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  rr = 0;         
26b12 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72          /* Error
26b13 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 56 64   message */.  Vd
26b14 62 65 20 2a 76 20 3d 20 28 56 64 62 65 20 2a 29  be *v = (Vdbe *)
26b15 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20 20 2f 2a 20  p->pStmt;..  /* 
26b16 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  Set the value of
26b17 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
26b18 6e 74 73 20 6f 6e 6c 79 20 76 61 72 69 61 62 6c  nts only variabl
26b19 65 20 74 6f 20 69 6e 74 65 67 65 72 20 69 52 6f  e to integer iRo
26b1a 77 2e 20 0a 20 20 2a 2a 20 54 68 69 73 20 69 73  w. .  ** This is
26b1b 20 64 6f 6e 65 20 64 69 72 65 63 74 6c 79 20 69   done directly i
26b1c 6e 73 74 65 61 64 20 6f 66 20 75 73 69 6e 67 20  nstead of using 
26b1d 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
26b1e 36 34 28 29 20 74 6f 20 61 76 6f 69 64 20 0a 20  64() to avoid . 
26b1f 20 2a 2a 20 74 72 69 67 67 65 72 69 6e 67 20 61   ** triggering a
26b20 73 73 65 72 74 73 20 72 65 6c 61 74 65 64 20 74  sserts related t
26b21 6f 20 6d 75 74 65 78 65 73 2e 0a 20 20 2a 2f 0a  o mutexes..  */.
26b22 20 20 61 73 73 65 72 74 28 20 76 2d 3e 61 56 61    assert( v->aVa
26b23 72 5b 30 5d 2e 66 6c 61 67 73 26 4d 45 4d 5f 49  r[0].flags&MEM_I
26b24 6e 74 20 29 3b 0a 20 20 76 2d 3e 61 56 61 72 5b  nt );.  v->aVar[
26b25 30 5d 2e 75 2e 69 20 3d 20 69 52 6f 77 3b 0a 0a  0].u.i = iRow;..
26b26 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
26b27 74 65 70 28 70 2d 3e 70 53 74 6d 74 29 3b 0a 20  tep(p->pStmt);. 
26b28 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
26b29 52 4f 57 20 29 7b 0a 20 20 20 20 75 33 32 20 74  ROW ){.    u32 t
26b2a 79 70 65 20 3d 20 76 2d 3e 61 70 43 73 72 5b 30  ype = v->apCsr[0
26b2b 5d 2d 3e 61 54 79 70 65 5b 70 2d 3e 69 43 6f 6c  ]->aType[p->iCol
26b2c 5d 3b 0a 20 20 20 20 69 66 28 20 74 79 70 65 3c  ];.    if( type<
26b2d 31 32 20 29 7b 0a 20 20 20 20 20 20 7a 45 72 72  12 ){.      zErr
26b2e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
26b2f 66 28 70 2d 3e 64 62 2c 20 22 63 61 6e 6e 6f 74  f(p->db, "cannot
26b30 20 6f 70 65 6e 20 76 61 6c 75 65 20 6f 66 20 74   open value of t
26b31 79 70 65 20 25 73 22 2c 0a 20 20 20 20 20 20 20  ype %s",.       
26b32 20 20 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c     type==0?"null
26b33 22 3a 20 74 79 70 65 3d 3d 37 3f 22 72 65 61 6c  ": type==7?"real
26b34 22 3a 20 22 69 6e 74 65 67 65 72 22 0a 20 20 20  ": "integer".   
26b35 20 20 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d     );.      rc =
26b36 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
26b37 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e       sqlite3_fin
26b38 61 6c 69 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b  alize(p->pStmt);
26b39 0a 20 20 20 20 20 20 70 2d 3e 70 53 74 6d 74 20  .      p->pStmt 
26b3a 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 0;.    }else{.
26b3b 20 20 20 20 20 20 70 2d 3e 69 4f 66 66 73 65 74        p->iOffset
26b3c 20 3d 20 76 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e   = v->apCsr[0]->
26b3d 61 4f 66 66 73 65 74 5b 70 2d 3e 69 43 6f 6c 5d  aOffset[p->iCol]
26b3e 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 42 79 74 65  ;.      p->nByte
26b3f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
26b40 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 79 70 65  rialTypeLen(type
26b41 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 43 73 72  );.      p->pCsr
26b42 20 3d 20 20 76 2d 3e 61 70 43 73 72 5b 30 5d 2d   =  v->apCsr[0]-
26b43 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20  >pCursor;.      
26b44 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
26b45 72 43 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 29  rCursor(p->pCsr)
26b46 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ;.      sqlite3B
26b47 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c 6f  treeCacheOverflo
26b48 77 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20  w(p->pCsr);.    
26b49 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
26b4a 61 76 65 43 75 72 73 6f 72 28 70 2d 3e 70 43 73  aveCursor(p->pCs
26b4b 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  r);.    }.  }.. 
26b4c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
26b4d 52 4f 57 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ROW ){.    rc = 
26b4e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
26b4f 73 65 20 69 66 28 20 70 2d 3e 70 53 74 6d 74 20  se if( p->pStmt 
26b50 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
26b51 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 2d 3e  te3_finalize(p->
26b52 70 53 74 6d 74 29 3b 0a 20 20 20 20 70 2d 3e 70  pStmt);.    p->p
26b53 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 69 66  Stmt = 0;.    if
26b54 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
26b55 29 7b 0a 20 20 20 20 20 20 7a 45 72 72 20 3d 20  ){.      zErr = 
26b56 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 70  sqlite3MPrintf(p
26b57 2d 3e 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 72  ->db, "no such r
26b58 6f 77 69 64 3a 20 25 6c 6c 64 22 2c 20 69 52 6f  owid: %lld", iRo
26b59 77 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  w);.      rc = S
26b5a 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
26b5b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 45   }else{.      zE
26b5c 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  rr = sqlite3MPri
26b5d 6e 74 66 28 70 2d 3e 64 62 2c 20 22 25 73 22 2c  ntf(p->db, "%s",
26b5e 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
26b5f 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 7d 0a 20  p->db));.    }. 
26b60 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 72 63   }..  assert( rc
26b61 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 7a  !=SQLITE_OK || z
26b62 45 72 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  Err==0 );.  asse
26b63 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52  rt( rc!=SQLITE_R
26b64 4f 57 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45  OW && rc!=SQLITE
26b65 5f 44 4f 4e 45 20 29 3b 0a 0a 20 20 2a 70 7a 45  _DONE );..  *pzE
26b66 72 72 20 3d 20 7a 45 72 72 3b 0a 20 20 72 65 74  rr = zErr;.  ret
26b67 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
26b68 20 4f 70 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e   Open a blob han
26b69 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  dle..*/.SQLITE_A
26b6a 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
26b6b 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69  lob_open(.  sqli
26b6c 74 65 33 2a 20 64 62 2c 20 20 20 20 20 20 20 20  te3* db,        
26b6d 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
26b6e 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
26b6f 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
26b70 7a 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  zDb,        /* T
26b71 68 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61  he attached data
26b72 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
26b73 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f  the blob */.  co
26b74 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
26b75 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62  ,     /* The tab
26b76 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
26b77 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73  e blob */.  cons
26b78 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c  t char *zColumn,
26b79 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d      /* The colum
26b7a 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  n containing the
26b7b 20 62 6c 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74   blob */.  sqlit
26b7c 65 5f 69 6e 74 36 34 20 69 52 6f 77 2c 20 20 20  e_int64 iRow,   
26b7d 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f     /* The row co
26b7e 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f  ntaining the glo
26b7f 62 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  b */.  int flags
26b80 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
26b81 2a 20 54 72 75 65 20 2d 3e 20 72 65 61 64 2f 77  * True -> read/w
26b82 72 69 74 65 20 61 63 63 65 73 73 2c 20 66 61 6c  rite access, fal
26b83 73 65 20 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20  se -> read-only 
26b84 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f  */.  sqlite3_blo
26b85 62 20 2a 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20  b **ppBlob   /* 
26b86 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73  Handle for acces
26b87 73 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 72 65  sing the blob re
26b88 74 75 72 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29  turned here */.)
26b89 7b 0a 20 20 69 6e 74 20 6e 41 74 74 65 6d 70 74  {.  int nAttempt
26b8a 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c   = 0;.  int iCol
26b8b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26b8c 2f 2a 20 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c  /* Index of zCol
26b8d 75 6d 6e 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72  umn in row-recor
26b8e 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20  d */..  /* This 
26b8f 56 44 42 45 20 70 72 6f 67 72 61 6d 20 73 65 65  VDBE program see
26b90 6b 73 20 61 20 62 74 72 65 65 20 63 75 72 73 6f  ks a btree curso
26b91 72 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66  r to the identif
26b92 69 65 64 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62  ied .  ** db/tab
26b93 6c 65 2f 72 6f 77 20 65 6e 74 72 79 2e 20 54 68  le/row entry. Th
26b94 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 69  e reason for usi
26b95 6e 67 20 61 20 76 64 62 65 20 70 72 6f 67 72 61  ng a vdbe progra
26b96 6d 20 69 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f  m instead.  ** o
26b97 66 20 77 72 69 74 69 6e 67 20 63 6f 64 65 20 74  f writing code t
26b98 6f 20 75 73 65 20 74 68 65 20 62 2d 74 72 65 65  o use the b-tree
26b99 20 6c 61 79 65 72 20 64 69 72 65 63 74 6c 79 20   layer directly 
26b9a 69 73 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a  is that the.  **
26b9b 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 69   vdbe program wi
26b9c 6c 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67  ll take advantag
26b9d 65 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73  e of the various
26b9e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20   transaction,.  
26b9f 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65  ** locking and e
26ba0 72 72 6f 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e  rror handling in
26ba1 66 72 61 73 74 72 75 63 74 75 72 65 20 62 75 69  frastructure bui
26ba2 6c 74 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65  lt into the vdbe
26ba3 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65  ..  **.  ** Afte
26ba4 72 20 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75  r seeking the cu
26ba5 72 73 6f 72 2c 20 74 68 65 20 76 64 62 65 20 65  rsor, the vdbe e
26ba6 78 65 63 75 74 65 73 20 61 6e 20 4f 50 5f 52 65  xecutes an OP_Re
26ba7 73 75 6c 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f  sultRow..  ** Co
26ba8 64 65 20 65 78 74 65 72 6e 61 6c 20 74 6f 20 74  de external to t
26ba9 68 65 20 56 64 62 65 20 74 68 65 6e 20 22 62 6f  he Vdbe then "bo
26baa 72 72 6f 77 73 22 20 74 68 65 20 62 2d 74 72 65  rrows" the b-tre
26bab 65 20 63 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a  e cursor and.  *
26bac 2a 20 75 73 65 73 20 69 74 20 74 6f 20 69 6d 70  * uses it to imp
26bad 6c 65 6d 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f  lement the blob_
26bae 72 65 61 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69  read(), blob_wri
26baf 74 65 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62  te() and .  ** b
26bb0 6c 6f 62 5f 62 79 74 65 73 28 29 20 66 75 6e 63  lob_bytes() func
26bb1 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
26bb2 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f   The sqlite3_blo
26bb3 62 5f 63 6c 6f 73 65 28 29 20 66 75 6e 63 74 69  b_close() functi
26bb4 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65  on finalizes the
26bb5 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20   vdbe program,. 
26bb6 20 2a 2a 20 77 68 69 63 68 20 63 6c 6f 73 65 73   ** which closes
26bb7 20 74 68 65 20 62 2d 74 72 65 65 20 63 75 72 73   the b-tree curs
26bb8 6f 72 20 61 6e 64 20 28 70 6f 73 73 69 62 6c 79  or and (possibly
26bb9 29 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20  ) commits the . 
26bba 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e   ** transaction.
26bbb 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63  .  */.  static c
26bbc 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
26bbd 6f 70 65 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20  openBlob[] = {. 
26bbe 20 20 20 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69     {OP_Transacti
26bbf 6f 6e 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20  on, 0, 0, 0},   
26bc0 20 20 2f 2a 20 30 3a 20 53 74 61 72 74 20 61 20    /* 0: Start a 
26bc1 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20  transaction */. 
26bc2 20 20 20 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f     {OP_VerifyCoo
26bc3 6b 69 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20  kie, 0, 0, 0},  
26bc4 20 20 2f 2a 20 31 3a 20 43 68 65 63 6b 20 74 68    /* 1: Check th
26bc5 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20  e schema cookie 
26bc6 2a 2f 0a 20 20 20 20 7b 4f 50 5f 54 61 62 6c 65  */.    {OP_Table
26bc7 4c 6f 63 6b 2c 20 30 2c 20 30 2c 20 30 7d 2c 20  Lock, 0, 0, 0}, 
26bc8 20 20 20 20 20 20 2f 2a 20 32 3a 20 41 63 71 75        /* 2: Acqu
26bc9 69 72 65 20 61 20 72 65 61 64 20 6f 72 20 77 72  ire a read or wr
26bca 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20  ite lock */..   
26bcb 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 66   /* One of the f
26bcc 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73  ollowing two ins
26bcd 74 72 75 63 74 69 6f 6e 73 20 69 73 20 72 65 70  tructions is rep
26bce 6c 61 63 65 64 20 62 79 20 61 6e 20 4f 50 5f 4e  laced by an OP_N
26bcf 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  oop. */.    {OP_
26bd0 4f 70 65 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20  OpenRead, 0, 0, 
26bd1 30 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 33 3a  0},        /* 3:
26bd2 20 4f 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66   Open cursor 0 f
26bd3 6f 72 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20  or reading */.  
26bd4 20 20 7b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c    {OP_OpenWrite,
26bd5 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20   0, 0, 0},      
26bd6 20 2f 2a 20 34 3a 20 4f 70 65 6e 20 63 75 72 73   /* 4: Open curs
26bd7 6f 72 20 30 20 66 6f 72 20 72 65 61 64 2f 77 72  or 0 for read/wr
26bd8 69 74 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f  ite */..    {OP_
26bd9 56 61 72 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20  Variable, 1, 1, 
26bda 31 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 35 3a  1},        /* 5:
26bdb 20 50 75 73 68 20 74 68 65 20 72 6f 77 69 64 20   Push the rowid 
26bdc 74 6f 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a  to the stack */.
26bdd 20 20 20 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74      {OP_NotExist
26bde 73 2c 20 30 2c 20 31 30 2c 20 31 7d 2c 20 20 20  s, 0, 10, 1},   
26bdf 20 20 20 2f 2a 20 36 3a 20 53 65 65 6b 20 74 68     /* 6: Seek th
26be0 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20  e cursor */.    
26be1 7b 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30  {OP_Column, 0, 0
26be2 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20 2f  , 1},          /
26be3 2a 20 37 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  * 7  */.    {OP_
26be4 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c  ResultRow, 1, 0,
26be5 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 38 20   0},       /* 8 
26be6 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 47 6f 74 6f   */.    {OP_Goto
26be7 2c 20 30 2c 20 35 2c 20 30 7d 2c 20 20 20 20 20  , 0, 5, 0},     
26be8 20 20 20 20 20 20 20 2f 2a 20 39 20 20 2a 2f 0a         /* 9  */.
26be9 20 20 20 20 7b 4f 50 5f 43 6c 6f 73 65 2c 20 30      {OP_Close, 0
26bea 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20  , 0, 0},        
26beb 20 20 20 2f 2a 20 31 30 20 2a 2f 0a 20 20 20 20     /* 10 */.    
26bec 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 2c 20  {OP_Halt, 0, 0, 
26bed 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  0},            /
26bee 2a 20 31 31 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 20  * 11 */.  };..  
26bef 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
26bf0 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  OK;.  char *zErr
26bf1 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70   = 0;.  Table *p
26bf2 54 61 62 3b 0a 20 20 50 61 72 73 65 20 2a 70 50  Tab;.  Parse *pP
26bf3 61 72 73 65 20 3d 20 30 3b 0a 20 20 49 6e 63 72  arse = 0;.  Incr
26bf4 62 6c 6f 62 20 2a 70 42 6c 6f 62 20 3d 20 30 3b  blob *pBlob = 0;
26bf5 0a 0a 20 20 66 6c 61 67 73 20 3d 20 21 21 66 6c  ..  flags = !!fl
26bf6 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
26bf7 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 3d 20 28      /* flags = (
26bf8 66 6c 61 67 73 20 3f 20 31 20 3a 20 30 29 3b 20  flags ? 1 : 0); 
26bf9 2a 2f 0a 20 20 2a 70 70 42 6c 6f 62 20 3d 20 30  */.  *ppBlob = 0
26bfa 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
26bfb 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
26bfc 65 78 29 3b 0a 0a 20 20 70 42 6c 6f 62 20 3d 20  ex);..  pBlob = 
26bfd 28 49 6e 63 72 62 6c 6f 62 20 2a 29 73 71 6c 69  (Incrblob *)sqli
26bfe 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
26bff 64 62 2c 20 73 69 7a 65 6f 66 28 49 6e 63 72 62  db, sizeof(Incrb
26c00 6c 6f 62 29 29 3b 0a 20 20 69 66 28 20 21 70 42  lob));.  if( !pB
26c01 6c 6f 62 20 29 20 67 6f 74 6f 20 62 6c 6f 62 5f  lob ) goto blob_
26c02 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 70 50 61 72  open_out;.  pPar
26c03 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63  se = sqlite3Stac
26c04 6b 41 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69  kAllocRaw(db, si
26c05 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b 0a  zeof(*pParse));.
26c06 20 20 69 66 28 20 21 70 50 61 72 73 65 20 29 20    if( !pParse ) 
26c07 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f  goto blob_open_o
26c08 75 74 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20  ut;..  do {.    
26c09 6d 65 6d 73 65 74 28 70 50 61 72 73 65 2c 20 30  memset(pParse, 0
26c0a 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65 29 29  , sizeof(Parse))
26c0b 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62  ;.    pParse->db
26c0c 20 3d 20 64 62 3b 0a 20 20 20 20 73 71 6c 69 74   = db;.    sqlit
26c0d 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
26c0e 72 29 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 30  r);.    zErr = 0
26c0f 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ;..    sqlite3Bt
26c10 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b  reeEnterAll(db);
26c11 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69  .    pTab = sqli
26c12 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70  te3LocateTable(p
26c13 50 61 72 73 65 2c 20 30 2c 20 7a 54 61 62 6c 65  Parse, 0, zTable
26c14 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20  , zDb);.    if( 
26c15 70 54 61 62 20 26 26 20 49 73 56 69 72 74 75 61  pTab && IsVirtua
26c16 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  l(pTab) ){.     
26c17 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20   pTab = 0;.     
26c18 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
26c19 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74  (pParse, "cannot
26c1a 20 6f 70 65 6e 20 76 69 72 74 75 61 6c 20 74 61   open virtual ta
26c1b 62 6c 65 3a 20 25 73 22 2c 20 7a 54 61 62 6c 65  ble: %s", zTable
26c1c 29 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  );.    }.#ifndef
26c1d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
26c1e 57 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 26  W.    if( pTab &
26c1f 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  & pTab->pSelect 
26c20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20  ){.      pTab = 
26c21 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  0;.      sqlite3
26c22 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
26c23 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69   "cannot open vi
26c24 65 77 3a 20 25 73 22 2c 20 7a 54 61 62 6c 65 29  ew: %s", zTable)
26c25 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
26c26 20 20 20 69 66 28 20 21 70 54 61 62 20 29 7b 0a     if( !pTab ){.
26c27 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65        if( pParse
26c28 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20  ->zErrMsg ){.   
26c29 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
26c2a 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20  ee(db, zErr);.  
26c2b 20 20 20 20 20 20 7a 45 72 72 20 3d 20 70 50 61        zErr = pPa
26c2c 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20  rse->zErrMsg;.  
26c2d 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 45        pParse->zE
26c2e 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20  rrMsg = 0;.     
26c2f 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51   }.      rc = SQ
26c30 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
26c31 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
26c32 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  aveAll(db);.    
26c33 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e    goto blob_open
26c34 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  _out;.    }..   
26c35 20 2f 2a 20 4e 6f 77 20 73 65 61 72 63 68 20 70   /* Now search p
26c36 54 61 62 20 66 6f 72 20 74 68 65 20 65 78 61 63  Tab for the exac
26c37 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 20  t column. */.   
26c38 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f   for(iCol=0; iCo
26c39 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43  l<pTab->nCol; iC
26c3a 6f 6c 2b 2b 29 20 7b 0a 20 20 20 20 20 20 69 66  ol++) {.      if
26c3b 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
26c3c 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c  (pTab->aCol[iCol
26c3d 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e  ].zName, zColumn
26c3e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
26c3f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
26c40 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43 6f     }.    if( iCo
26c41 6c 3d 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b  l==pTab->nCol ){
26c42 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
26c43 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a  Free(db, zErr);.
26c44 20 20 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c        zErr = sql
26c45 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
26c46 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e 3a  "no such column:
26c47 20 5c 22 25 73 5c 22 22 2c 20 7a 43 6f 6c 75 6d   \"%s\"", zColum
26c48 6e 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  n);.      rc = S
26c49 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
26c4a 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
26c4b 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20  eaveAll(db);.   
26c4c 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65     goto blob_ope
26c4d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20  n_out;.    }..  
26c4e 20 20 2f 2a 20 49 66 20 74 68 65 20 76 61 6c 75    /* If the valu
26c4f 65 20 69 73 20 62 65 69 6e 67 20 6f 70 65 6e 65  e is being opene
26c50 64 20 66 6f 72 20 77 72 69 74 69 6e 67 2c 20 63  d for writing, c
26c51 68 65 63 6b 20 74 68 61 74 20 74 68 65 0a 20 20  heck that the.  
26c52 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 6e    ** column is n
26c53 6f 74 20 69 6e 64 65 78 65 64 2c 20 61 6e 64 20  ot indexed, and 
26c54 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 70  that it is not p
26c55 61 72 74 20 6f 66 20 61 20 66 6f 72 65 69 67 6e  art of a foreign
26c56 20 6b 65 79 2e 20 0a 20 20 20 20 2a 2a 20 49 74   key. .    ** It
26c57 20 69 73 20 61 67 61 69 6e 73 74 20 74 68 65 20   is against the 
26c58 72 75 6c 65 73 20 74 6f 20 6f 70 65 6e 20 61 20  rules to open a 
26c59 63 6f 6c 75 6d 6e 20 74 6f 20 77 68 69 63 68 20  column to which 
26c5a 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 0a  either of these.
26c5b 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70 74 69      ** descripti
26c5c 6f 6e 73 20 61 70 70 6c 69 65 73 20 66 6f 72 20  ons applies for 
26c5d 77 72 69 74 69 6e 67 2e 20 20 2a 2f 0a 20 20 20  writing.  */.   
26c5e 20 69 66 28 20 66 6c 61 67 73 20 29 7b 0a 20 20   if( flags ){.  
26c5f 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
26c60 7a 46 61 75 6c 74 20 3d 20 30 3b 0a 20 20 20 20  zFault = 0;.    
26c61 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 23    Index *pIdx;.#
26c62 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
26c63 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20  IT_FOREIGN_KEY. 
26c64 20 20 20 20 20 69 66 28 20 64 62 2d 3e 66 6c 61       if( db->fla
26c65 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67  gs&SQLITE_Foreig
26c66 6e 4b 65 79 73 20 29 7b 0a 20 20 20 20 20 20 20  nKeys ){.       
26c67 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74   /* Check that t
26c68 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74  he column is not
26c69 20 70 61 72 74 20 6f 66 20 61 6e 20 46 4b 20 63   part of an FK c
26c6a 68 69 6c 64 20 6b 65 79 20 64 65 66 69 6e 69 74  hild key definit
26c6b 69 6f 6e 2e 20 49 74 0a 20 20 20 20 20 20 20 20  ion. It.        
26c6c 2a 2a 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73  ** is not necess
26c6d 61 72 79 20 74 6f 20 63 68 65 63 6b 20 69 66 20  ary to check if 
26c6e 69 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 20  it is part of a 
26c6f 70 61 72 65 6e 74 20 6b 65 79 2c 20 61 73 20 70  parent key, as p
26c70 61 72 65 6e 74 0a 20 20 20 20 20 20 20 20 2a 2a  arent.        **
26c71 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 6d 75 73   key columns mus
26c72 74 20 62 65 20 69 6e 64 65 78 65 64 2e 20 54 68  t be indexed. Th
26c73 65 20 63 68 65 63 6b 20 62 65 6c 6f 77 20 77 69  e check below wi
26c74 6c 6c 20 70 69 63 6b 20 75 70 20 74 68 69 73 20  ll pick up this 
26c75 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65  .        ** case
26c76 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 46 4b  .  */.        FK
26c77 65 79 20 2a 70 46 4b 65 79 3b 0a 20 20 20 20 20  ey *pFKey;.     
26c78 20 20 20 66 6f 72 28 70 46 4b 65 79 3d 70 54 61     for(pFKey=pTa
26c79 62 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b  b->pFKey; pFKey;
26c7a 20 70 46 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e   pFKey=pFKey->pN
26c7b 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20  extFrom){.      
26c7c 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20      int j;.     
26c7d 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
26c7e 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b  pFKey->nCol; j++
26c7f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
26c80 66 28 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 6a  f( pFKey->aCol[j
26c81 5d 2e 69 46 72 6f 6d 3d 3d 69 43 6f 6c 20 29 7b  ].iFrom==iCol ){
26c82 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a  .              z
26c83 46 61 75 6c 74 20 3d 20 22 66 6f 72 65 69 67 6e  Fault = "foreign
26c84 20 6b 65 79 22 3b 0a 20 20 20 20 20 20 20 20 20   key";.         
26c85 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
26c86 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
26c87 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   }.#endif.      
26c88 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
26c89 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
26c8a 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
26c8b 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20          int j;. 
26c8c 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20         for(j=0; 
26c8d 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  j<pIdx->nColumn;
26c8e 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   j++){.         
26c8f 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c   if( pIdx->aiCol
26c90 75 6d 6e 5b 6a 5d 3d 3d 69 43 6f 6c 20 29 7b 0a  umn[j]==iCol ){.
26c91 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 61 75              zFau
26c92 6c 74 20 3d 20 22 69 6e 64 65 78 65 64 22 3b 0a  lt = "indexed";.
26c93 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
26c94 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
26c95 20 20 20 20 69 66 28 20 7a 46 61 75 6c 74 20 29      if( zFault )
26c96 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
26c97 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
26c98 29 3b 0a 20 20 20 20 20 20 20 20 7a 45 72 72 20  );.        zErr 
26c99 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
26c9a 28 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65  (db, "cannot ope
26c9b 6e 20 25 73 20 63 6f 6c 75 6d 6e 20 66 6f 72 20  n %s column for 
26c9c 77 72 69 74 69 6e 67 22 2c 20 7a 46 61 75 6c 74  writing", zFault
26c9d 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
26c9e 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
26c9f 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
26ca0 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
26ca1 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f          goto blo
26ca2 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20  b_open_out;.    
26ca3 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70    }.    }..    p
26ca4 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20 28 73  Blob->pStmt = (s
26ca5 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 73 71  qlite3_stmt *)sq
26ca6 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28  lite3VdbeCreate(
26ca7 64 62 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  db);.    assert(
26ca8 20 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 7c 7c   pBlob->pStmt ||
26ca9 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
26caa 64 20 29 3b 0a 20 20 20 20 69 66 28 20 70 42 6c  d );.    if( pBl
26cab 6f 62 2d 3e 70 53 74 6d 74 20 29 7b 0a 20 20 20  ob->pStmt ){.   
26cac 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64     Vdbe *v = (Vd
26cad 62 65 20 2a 29 70 42 6c 6f 62 2d 3e 70 53 74 6d  be *)pBlob->pStm
26cae 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62  t;.      int iDb
26caf 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
26cb0 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62  ToIndex(db, pTab
26cb1 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20 20 20  ->pSchema);..   
26cb2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26cb3 64 4f 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f  dOpList(v, sizeo
26cb4 66 28 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65  f(openBlob)/size
26cb5 6f 66 28 56 64 62 65 4f 70 4c 69 73 74 29 2c 20  of(VdbeOpList), 
26cb6 6f 70 65 6e 42 6c 6f 62 29 3b 0a 0a 0a 20 20 20  openBlob);...   
26cb7 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20     /* Configure 
26cb8 74 68 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69  the OP_Transacti
26cb9 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  on */.      sqli
26cba 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
26cbb 76 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20  v, 0, iDb);.    
26cbc 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
26cbd 6e 67 65 50 32 28 76 2c 20 30 2c 20 66 6c 61 67  ngeP2(v, 0, flag
26cbe 73 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f  s);..      /* Co
26cbf 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 56  nfigure the OP_V
26cc0 65 72 69 66 79 43 6f 6f 6b 69 65 20 2a 2f 0a 20  erifyCookie */. 
26cc1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26cc2 43 68 61 6e 67 65 50 31 28 76 2c 20 31 2c 20 69  ChangeP1(v, 1, i
26cc3 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
26cc4 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76  e3VdbeChangeP2(v
26cc5 2c 20 31 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  , 1, pTab->pSche
26cc6 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69  ma->schema_cooki
26cc7 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
26cc8 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c  3VdbeChangeP3(v,
26cc9 20 31 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d   1, pTab->pSchem
26cca 61 2d 3e 69 47 65 6e 65 72 61 74 69 6f 6e 29 3b  a->iGeneration);
26ccb 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20  ..      /* Make 
26ccc 73 75 72 65 20 61 20 6d 75 74 65 78 20 69 73 20  sure a mutex is 
26ccd 68 65 6c 64 20 6f 6e 20 74 68 65 20 74 61 62 6c  held on the tabl
26cce 65 20 74 6f 20 62 65 20 61 63 63 65 73 73 65 64  e to be accessed
26ccf 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
26cd0 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76  3VdbeUsesBtree(v
26cd1 2c 20 69 44 62 29 3b 20 0a 0a 20 20 20 20 20 20  , iDb); ..      
26cd2 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65  /* Configure the
26cd3 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e   OP_TableLock in
26cd4 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 69 66  struction */.#if
26cd5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
26cd6 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 20  SHARED_CACHE.   
26cd7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
26cd8 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 32 29  angeToNoop(v, 2)
26cd9 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 73 71  ;.#else.      sq
26cda 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
26cdb 31 28 76 2c 20 32 2c 20 69 44 62 29 3b 0a 20 20  1(v, 2, iDb);.  
26cdc 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
26cdd 68 61 6e 67 65 50 32 28 76 2c 20 32 2c 20 70 54  hangeP2(v, 2, pT
26cde 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20  ab->tnum);.     
26cdf 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
26ce0 67 65 50 33 28 76 2c 20 32 2c 20 66 6c 61 67 73  geP3(v, 2, flags
26ce1 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26ce2 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
26ce3 32 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  2, pTab->zName, 
26ce4 50 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 23  P4_TRANSIENT);.#
26ce5 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f 2a 20  endif..      /* 
26ce6 52 65 6d 6f 76 65 20 65 69 74 68 65 72 20 74 68  Remove either th
26ce7 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 6f  e OP_OpenWrite o
26ce8 72 20 4f 70 65 6e 52 65 61 64 2e 20 53 65 74 20  r OpenRead. Set 
26ce9 74 68 65 20 50 32 20 0a 20 20 20 20 20 20 2a 2a  the P2 .      **
26cea 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68   parameter of th
26ceb 65 20 6f 74 68 65 72 20 74 6f 20 70 54 61 62 2d  e other to pTab-
26cec 3e 74 6e 75 6d 2e 20 20 2a 2f 0a 20 20 20 20 20  >tnum.  */.     
26ced 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
26cee 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 34 20 2d 20  geToNoop(v, 4 - 
26cef 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 73 71  flags);.      sq
26cf0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
26cf1 32 28 76 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20  2(v, 3 + flags, 
26cf2 70 54 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20  pTab->tnum);.   
26cf3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
26cf4 61 6e 67 65 50 33 28 76 2c 20 33 20 2b 20 66 6c  angeP3(v, 3 + fl
26cf5 61 67 73 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20  ags, iDb);..    
26cf6 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74    /* Configure t
26cf7 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
26cf8 75 6d 6e 73 2e 20 43 6f 6e 66 69 67 75 72 65 20  umns. Configure 
26cf9 74 68 65 20 63 75 72 73 6f 72 20 74 6f 0a 20 20  the cursor to.  
26cfa 20 20 20 20 2a 2a 20 74 68 69 6e 6b 20 74 68 61      ** think tha
26cfb 74 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20  t the table has 
26cfc 6f 6e 65 20 6d 6f 72 65 20 63 6f 6c 75 6d 6e 20  one more column 
26cfd 74 68 61 6e 20 69 74 20 72 65 61 6c 6c 79 0a 20  than it really. 
26cfe 20 20 20 20 20 2a 2a 20 64 6f 65 73 2e 20 41 6e       ** does. An
26cff 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72 65   OP_Column to re
26d00 74 72 69 65 76 65 20 74 68 69 73 20 69 6d 61 67  trieve this imag
26d01 69 6e 61 72 79 20 63 6f 6c 75 6d 6e 20 77 69 6c  inary column wil
26d02 6c 0a 20 20 20 20 20 20 2a 2a 20 61 6c 77 61 79  l.      ** alway
26d03 73 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 20  s return an SQL 
26d04 4e 55 4c 4c 2e 20 54 68 69 73 20 69 73 20 75 73  NULL. This is us
26d05 65 66 75 6c 20 62 65 63 61 75 73 65 20 69 74 20  eful because it 
26d06 6d 65 61 6e 73 0a 20 20 20 20 20 20 2a 2a 20 77  means.      ** w
26d07 65 20 63 61 6e 20 69 6e 76 6f 6b 65 20 4f 50 5f  e can invoke OP_
26d08 43 6f 6c 75 6d 6e 20 74 6f 20 66 69 6c 6c 20 69  Column to fill i
26d09 6e 20 74 68 65 20 76 64 62 65 20 63 75 72 73 6f  n the vdbe curso
26d0a 72 73 20 74 79 70 65 20 0a 20 20 20 20 20 20 2a  rs type .      *
26d0b 2a 20 61 6e 64 20 6f 66 66 73 65 74 20 63 61 63  * and offset cac
26d0c 68 65 20 77 69 74 68 6f 75 74 20 63 61 75 73 69  he without causi
26d0d 6e 67 20 61 6e 79 20 49 4f 2e 0a 20 20 20 20 20  ng any IO..     
26d0e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
26d0f 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
26d10 20 33 2b 66 6c 61 67 73 2c 20 53 51 4c 49 54 45   3+flags, SQLITE
26d11 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 70 54 61 62  _INT_TO_PTR(pTab
26d12 2d 3e 6e 43 6f 6c 2b 31 29 2c 50 34 5f 49 4e 54  ->nCol+1),P4_INT
26d13 33 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  32);.      sqlit
26d14 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76  e3VdbeChangeP2(v
26d15 2c 20 37 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  , 7, pTab->nCol)
26d16 3b 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 2d  ;.      if( !db-
26d17 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
26d18 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
26d19 3e 6e 56 61 72 20 3d 20 31 3b 0a 20 20 20 20 20  >nVar = 1;.     
26d1a 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
26d1b 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 50 61  = 1;.        pPa
26d1c 72 73 65 2d 3e 6e 54 61 62 20 3d 20 31 3b 0a 20  rse->nTab = 1;. 
26d1d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26d1e 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 70  beMakeReady(v, p
26d1f 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a  Parse);.      }.
26d20 20 20 20 20 7d 0a 20 20 20 0a 20 20 20 20 70 42      }.   .    pB
26d21 6c 6f 62 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61  lob->flags = fla
26d22 67 73 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69  gs;.    pBlob->i
26d23 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20  Col = iCol;.    
26d24 70 42 6c 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a  pBlob->db = db;.
26d25 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
26d26 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20  LeaveAll(db);.  
26d27 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
26d28 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
26d29 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f  goto blob_open_o
26d2a 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ut;.    }.    sq
26d2b 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
26d2c 28 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 2c 20 31  (pBlob->pStmt, 1
26d2d 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20  , iRow);.    rc 
26d2e 3d 20 62 6c 6f 62 53 65 65 6b 54 6f 52 6f 77 28  = blobSeekToRow(
26d2f 70 42 6c 6f 62 2c 20 69 52 6f 77 2c 20 26 7a 45  pBlob, iRow, &zE
26d30 72 72 29 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20  rr);.  } while( 
26d31 28 2b 2b 6e 41 74 74 65 6d 70 74 29 3c 35 20 26  (++nAttempt)<5 &
26d32 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48  & rc==SQLITE_SCH
26d33 45 4d 41 20 29 3b 0a 0a 62 6c 6f 62 5f 6f 70 65  EMA );..blob_ope
26d34 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 3d  n_out:.  if( rc=
26d35 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64 62  =SQLITE_OK && db
26d36 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d  ->mallocFailed==
26d37 30 20 29 7b 0a 20 20 20 20 2a 70 70 42 6c 6f 62  0 ){.    *ppBlob
26d38 20 3d 20 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62   = (sqlite3_blob
26d39 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 7d 65 6c 73   *)pBlob;.  }els
26d3a 65 7b 0a 20 20 20 20 69 66 28 20 70 42 6c 6f 62  e{.    if( pBlob
26d3b 20 26 26 20 70 42 6c 6f 62 2d 3e 70 53 74 6d 74   && pBlob->pStmt
26d3c 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 46 69   ) sqlite3VdbeFi
26d3d 6e 61 6c 69 7a 65 28 28 56 64 62 65 20 2a 29 70  nalize((Vdbe *)p
26d3e 42 6c 6f 62 2d 3e 70 53 74 6d 74 29 3b 0a 20 20  Blob->pStmt);.  
26d3f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
26d40 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 20 7d 0a  db, pBlob);.  }.
26d41 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
26d42 62 2c 20 72 63 2c 20 28 7a 45 72 72 20 3f 20 22  b, rc, (zErr ? "
26d43 25 73 22 20 3a 20 30 29 2c 20 7a 45 72 72 29 3b  %s" : 0), zErr);
26d44 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
26d45 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 73 71  (db, zErr);.  sq
26d46 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64  lite3StackFree(d
26d47 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 72 63  b, pParse);.  rc
26d48 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
26d49 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
26d4a 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
26d4b 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
26d4c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
26d4d 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c 6f 62 20  ** Close a blob 
26d4e 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61 73 20  handle that was 
26d4f 70 72 65 76 69 6f 75 73 6c 79 20 63 72 65 61 74  previously creat
26d50 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69  ed using.** sqli
26d51 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 2e  te3_blob_open().
26d52 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
26d53 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  nt sqlite3_blob_
26d54 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 62 6c  close(sqlite3_bl
26d55 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 6e  ob *pBlob){.  In
26d56 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63  crblob *p = (Inc
26d57 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20  rblob *)pBlob;. 
26d58 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
26d59 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70  e3 *db;..  if( p
26d5a 20 29 7b 0a 20 20 20 20 64 62 20 3d 20 70 2d 3e   ){.    db = p->
26d5b 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  db;.    sqlite3_
26d5c 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
26d5d 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d  mutex);.    rc =
26d5e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
26d5f 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20  e(p->pStmt);.   
26d60 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
26d61 62 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74  b, p);.    sqlit
26d62 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
26d63 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c  b->mutex);.  }el
26d64 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  se{.    rc = SQL
26d65 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65  ITE_OK;.  }.  re
26d66 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
26d67 2a 20 50 65 72 66 6f 72 6d 20 61 20 72 65 61 64  * Perform a read
26d68 20 6f 72 20 77 72 69 74 65 20 6f 70 65 72 61 74   or write operat
26d69 69 6f 6e 20 6f 6e 20 61 20 62 6c 6f 62 0a 2a 2f  ion on a blob.*/
26d6a 0a 73 74 61 74 69 63 20 69 6e 74 20 62 6c 6f 62  .static int blob
26d6b 52 65 61 64 57 72 69 74 65 28 0a 20 20 73 71 6c  ReadWrite(.  sql
26d6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62  ite3_blob *pBlob
26d6d 2c 20 0a 20 20 76 6f 69 64 20 2a 7a 2c 20 0a 20  , .  void *z, . 
26d6e 20 69 6e 74 20 6e 2c 20 0a 20 20 69 6e 74 20 69   int n, .  int i
26d6f 4f 66 66 73 65 74 2c 20 0a 20 20 69 6e 74 20 28  Offset, .  int (
26d70 2a 78 43 61 6c 6c 29 28 42 74 43 75 72 73 6f 72  *xCall)(BtCursor
26d71 2a 2c 20 75 33 32 2c 20 75 33 32 2c 20 76 6f 69  *, u32, u32, voi
26d72 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b  d*).){.  int rc;
26d73 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d  .  Incrblob *p =
26d74 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c   (Incrblob *)pBl
26d75 6f 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20  ob;.  Vdbe *v;. 
26d76 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20   sqlite3 *db;.. 
26d77 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
26d78 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
26d79 5f 42 4b 50 54 3b 0a 20 20 64 62 20 3d 20 70 2d  _BKPT;.  db = p-
26d7a 3e 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  >db;.  sqlite3_m
26d7b 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
26d7c 75 74 65 78 29 3b 0a 20 20 76 20 3d 20 28 56 64  utex);.  v = (Vd
26d7d 62 65 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20  be*)p->pStmt;.. 
26d7e 20 69 66 28 20 6e 3c 30 20 7c 7c 20 69 4f 66 66   if( n<0 || iOff
26d7f 73 65 74 3c 30 20 7c 7c 20 28 69 4f 66 66 73 65  set<0 || (iOffse
26d80 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29 7b  t+n)>p->nByte ){
26d81 0a 20 20 20 20 2f 2a 20 52 65 71 75 65 73 74 20  .    /* Request 
26d82 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e  is out of range.
26d83 20 52 65 74 75 72 6e 20 61 20 74 72 61 6e 73 69   Return a transi
26d84 65 6e 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20  ent error. */.  
26d85 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
26d86 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ROR;.    sqlite3
26d87 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
26d88 5f 45 52 52 4f 52 2c 20 30 29 3b 0a 20 20 7d 65  _ERROR, 0);.  }e
26d89 6c 73 65 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a  lse if( v==0 ){.
26d8a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
26d8b 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20  is no statement 
26d8c 68 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68 65  handle, then the
26d8d 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61 73   blob-handle has
26d8e 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20  .    ** already 
26d8f 62 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 64  been invalidated
26d90 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  . Return SQLITE_
26d91 41 42 4f 52 54 20 69 6e 20 74 68 69 73 20 63 61  ABORT in this ca
26d92 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  se..    */.    r
26d93 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54  c = SQLITE_ABORT
26d94 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
26d95 2a 20 43 61 6c 6c 20 65 69 74 68 65 72 20 42 74  * Call either Bt
26d96 72 65 65 44 61 74 61 28 29 20 6f 72 20 42 74 72  reeData() or Btr
26d97 65 65 50 75 74 44 61 74 61 28 29 2e 20 49 66 20  eePutData(). If 
26d98 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 73 0a  SQLITE_ABORT is.
26d99 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c      ** returned,
26d9a 20 63 6c 65 61 6e 2d 75 70 20 74 68 65 20 73 74   clean-up the st
26d9b 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a  atement handle..
26d9c 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
26d9d 74 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62 20 29  t( db == v->db )
26d9e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
26d9f 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 2d  eeEnterCursor(p-
26da0 3e 70 43 73 72 29 3b 0a 20 20 20 20 72 63 20 3d  >pCsr);.    rc =
26da1 20 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72 2c 20   xCall(p->pCsr, 
26da2 69 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66 66 73  iOffset+p->iOffs
26da3 65 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 73  et, n, z);.    s
26da4 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
26da5 43 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b  Cursor(p->pCsr);
26da6 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
26da7 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20  ITE_ABORT ){.   
26da8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69     sqlite3VdbeFi
26da9 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 20  nalize(v);.     
26daa 20 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20   p->pStmt = 0;. 
26dab 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
26dac 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63  db->errCode = rc
26dad 3b 0a 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20  ;.      v->rc = 
26dae 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rc;.    }.  }.  
26daf 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
26db0 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73  xit(db, rc);.  s
26db1 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
26db2 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
26db3 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
26db4 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66  *.** Read data f
26db5 72 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c  rom a blob handl
26db6 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
26db7 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f   int sqlite3_blo
26db8 62 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62  b_read(sqlite3_b
26db9 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 64  lob *pBlob, void
26dba 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20   *z, int n, int 
26dbb 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75  iOffset){.  retu
26dbc 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65  rn blobReadWrite
26dbd 28 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f  (pBlob, z, n, iO
26dbe 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74  ffset, sqlite3Bt
26dbf 72 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a  reeData);.}../*.
26dc0 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f  ** Write data to
26dc1 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a   a blob handle..
26dc2 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
26dc3 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77  t sqlite3_blob_w
26dc4 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f  rite(sqlite3_blo
26dc5 62 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20  b *pBlob, const 
26dc6 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20  void *z, int n, 
26dc7 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20  int iOffset){.  
26dc8 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57  return blobReadW
26dc9 72 69 74 65 28 70 42 6c 6f 62 2c 20 28 76 6f 69  rite(pBlob, (voi
26dca 64 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 73 65  d *)z, n, iOffse
26dcb 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  t, sqlite3BtreeP
26dcc 75 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  utData);.}../*.*
26dcd 2a 20 51 75 65 72 79 20 61 20 62 6c 6f 62 20 68  * Query a blob h
26dce 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 73 69  andle for the si
26dcf 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 2e 0a  ze of the data..
26dd0 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72 62 6c  **.** The Incrbl
26dd1 6f 62 2e 6e 42 79 74 65 20 66 69 65 6c 64 20 69  ob.nByte field i
26dd2 73 20 66 69 78 65 64 20 66 6f 72 20 74 68 65 20  s fixed for the 
26dd3 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20  lifetime of the 
26dd4 49 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20 6e  Incrblob.** so n
26dd5 6f 20 6d 75 74 65 78 20 69 73 20 72 65 71 75 69  o mutex is requi
26dd6 72 65 64 20 66 6f 72 20 61 63 63 65 73 73 2e 0a  red for access..
26dd7 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
26dd8 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62  t sqlite3_blob_b
26dd9 79 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f  ytes(sqlite3_blo
26dda 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63  b *pBlob){.  Inc
26ddb 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72  rblob *p = (Incr
26ddc 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20  blob *)pBlob;.  
26ddd 72 65 74 75 72 6e 20 28 70 20 26 26 20 70 2d 3e  return (p && p->
26dde 70 53 74 6d 74 29 20 3f 20 70 2d 3e 6e 42 79 74  pStmt) ? p->nByt
26ddf 65 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  e : 0;.}../*.** 
26de0 4d 6f 76 65 20 61 6e 20 65 78 69 73 74 69 6e 67  Move an existing
26de1 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 74 6f 20   blob handle to 
26de2 70 6f 69 6e 74 20 74 6f 20 61 20 64 69 66 66 65  point to a diffe
26de3 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20  rent row of the 
26de4 73 61 6d 65 0a 2a 2a 20 64 61 74 61 62 61 73 65  same.** database
26de5 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   table..**.** If
26de6 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
26de7 2c 20 6f 72 20 69 66 20 74 68 65 20 73 70 65 63  , or if the spec
26de8 69 66 69 65 64 20 72 6f 77 20 64 6f 65 73 20 6e  ified row does n
26de9 6f 74 20 65 78 69 73 74 20 6f 72 20 64 6f 65 73  ot exist or does
26dea 20 6e 6f 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20   not.** contain 
26deb 61 20 62 6c 6f 62 20 6f 72 20 74 65 78 74 20 76  a blob or text v
26dec 61 6c 75 65 2c 20 74 68 65 6e 20 61 6e 20 65 72  alue, then an er
26ded 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
26dee 72 6e 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20  rned and the.** 
26def 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
26df0 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 6d  error code and m
26df1 65 73 73 61 67 65 20 73 65 74 2e 20 49 66 20 74  essage set. If t
26df2 68 69 73 20 68 61 70 70 65 6e 73 2c 20 74 68 65  his happens, the
26df3 6e 20 61 6c 6c 20 0a 2a 2a 20 73 75 62 73 65 71  n all .** subseq
26df4 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71  uent calls to sq
26df5 6c 69 74 65 33 5f 62 6c 6f 62 5f 78 78 78 28 29  lite3_blob_xxx()
26df6 20 66 75 6e 63 74 69 6f 6e 73 20 28 65 78 63 65   functions (exce
26df7 70 74 20 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 29  pt blob_close())
26df8 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79   .** immediately
26df9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41   return SQLITE_A
26dfa 42 4f 52 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  BORT..*/.SQLITE_
26dfb 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
26dfc 62 6c 6f 62 5f 72 65 6f 70 65 6e 28 73 71 6c 69  blob_reopen(sqli
26dfd 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c  te3_blob *pBlob,
26dfe 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
26dff 52 6f 77 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Row){.  int rc;.
26e00 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20    Incrblob *p = 
26e01 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f  (Incrblob *)pBlo
26e02 62 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  b;.  sqlite3 *db
26e03 3b 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  ;..  if( p==0 ) 
26e04 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
26e05 53 55 53 45 5f 42 4b 50 54 3b 0a 20 20 64 62 20  SUSE_BKPT;.  db 
26e06 3d 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74  = p->db;.  sqlit
26e07 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
26e08 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 69 66  b->mutex);..  if
26e09 28 20 70 2d 3e 70 53 74 6d 74 3d 3d 30 20 29 7b  ( p->pStmt==0 ){
26e0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65  .    /* If there
26e0b 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74   is no statement
26e0c 20 68 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68   handle, then th
26e0d 65 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61  e blob-handle ha
26e0e 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79  s.    ** already
26e0f 20 62 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 65   been invalidate
26e10 64 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  d. Return SQLITE
26e11 5f 41 42 4f 52 54 20 69 6e 20 74 68 69 73 20 63  _ABORT in this c
26e12 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ase..    */.    
26e13 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52  rc = SQLITE_ABOR
26e14 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  T;.  }else{.    
26e15 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20  char *zErr;.    
26e16 72 63 20 3d 20 62 6c 6f 62 53 65 65 6b 54 6f 52  rc = blobSeekToR
26e17 6f 77 28 70 2c 20 69 52 6f 77 2c 20 26 7a 45 72  ow(p, iRow, &zEr
26e18 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  r);.    if( rc!=
26e19 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
26e1a 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
26e1b 64 62 2c 20 72 63 2c 20 28 7a 45 72 72 20 3f 20  db, rc, (zErr ? 
26e1c 22 25 73 22 20 3a 20 30 29 2c 20 7a 45 72 72 29  "%s" : 0), zErr)
26e1d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
26e1e 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b  bFree(db, zErr);
26e1f 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
26e20 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 53 43  t( rc!=SQLITE_SC
26e21 48 45 4d 41 20 29 3b 0a 20 20 7d 0a 0a 20 20 72  HEMA );.  }..  r
26e22 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
26e23 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 61 73  it(db, rc);.  as
26e24 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
26e25 5f 4f 4b 20 7c 7c 20 70 2d 3e 70 53 74 6d 74 3d  _OK || p->pStmt=
26e26 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  =0 );.  sqlite3_
26e27 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
26e28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
26e29 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f   rc;.}..#endif /
26e2a 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  * #ifndef SQLITE
26e2b 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a  _OMIT_INCRBLOB *
26e2c 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
26e2d 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 62 6c  ** End of vdbebl
26e2e 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ob.c ***********
26e2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26e31 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
26e32 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
26e33 62 65 73 6f 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  besort.c *******
26e34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26e35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26e36 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 31 31 20 4a 75  */./*.** 2011 Ju
26e37 6c 79 20 39 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  ly 9.**.** The a
26e38 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
26e39 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
26e3a 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
26e3b 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
26e3c 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
26e3d 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
26e3e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
26e3f 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
26e40 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
26e41 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
26e42 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
26e43 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
26e44 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
26e45 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
26e46 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
26e47 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
26e48 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
26e49 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26e4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26e4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26e4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26e4d 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
26e4e 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66   contains code f
26e4f 6f 72 20 74 68 65 20 56 64 62 65 53 6f 72 74 65  or the VdbeSorte
26e50 72 20 6f 62 6a 65 63 74 2c 20 75 73 65 64 20 69  r object, used i
26e51 6e 20 63 6f 6e 63 65 72 74 20 77 69 74 68 0a 2a  n concert with.*
26e52 2a 20 61 20 56 64 62 65 43 75 72 73 6f 72 20 74  * a VdbeCursor t
26e53 6f 20 73 6f 72 74 20 6c 61 72 67 65 20 6e 75 6d  o sort large num
26e54 62 65 72 73 20 6f 66 20 6b 65 79 73 20 28 61 73  bers of keys (as
26e55 20 6d 61 79 20 62 65 20 72 65 71 75 69 72 65 64   may be required
26e56 2c 20 66 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65  , for.** example
26e57 2c 20 62 79 20 43 52 45 41 54 45 20 49 4e 44 45  , by CREATE INDE
26e58 58 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 20  X statements on 
26e59 74 61 62 6c 65 73 20 74 6f 6f 20 6c 61 72 67 65  tables too large
26e5a 20 74 6f 20 66 69 74 20 69 6e 20 6d 61 69 6e 0a   to fit in main.
26e5b 2a 2a 20 6d 65 6d 6f 72 79 29 2e 0a 2a 2f 0a 0a  ** memory)..*/..
26e5c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
26e5d 4f 4d 49 54 5f 4d 45 52 47 45 5f 53 4f 52 54 0a  OMIT_MERGE_SORT.
26e5e 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
26e5f 56 64 62 65 53 6f 72 74 65 72 49 74 65 72 20 56  VdbeSorterIter V
26e60 64 62 65 53 6f 72 74 65 72 49 74 65 72 3b 0a 74  dbeSorterIter;.t
26e61 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 6f  ypedef struct So
26e62 72 74 65 72 52 65 63 6f 72 64 20 53 6f 72 74 65  rterRecord Sorte
26e63 72 52 65 63 6f 72 64 3b 0a 74 79 70 65 64 65 66  rRecord;.typedef
26e64 20 73 74 72 75 63 74 20 46 69 6c 65 57 72 69 74   struct FileWrit
26e65 65 72 20 46 69 6c 65 57 72 69 74 65 72 3b 0a 0a  er FileWriter;..
26e66 2f 2a 0a 2a 2a 20 4e 4f 54 45 53 20 4f 4e 20 44  /*.** NOTES ON D
26e67 41 54 41 20 53 54 52 55 43 54 55 52 45 20 55 53  ATA STRUCTURE US
26e68 45 44 20 46 4f 52 20 4e 2d 57 41 59 20 4d 45 52  ED FOR N-WAY MER
26e69 47 45 53 3a 0a 2a 2a 0a 2a 2a 20 41 73 20 6b 65  GES:.**.** As ke
26e6a 79 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20  ys are added to 
26e6b 74 68 65 20 73 6f 72 74 65 72 2c 20 74 68 65 79  the sorter, they
26e6c 20 61 72 65 20 77 72 69 74 74 65 6e 20 74 6f 20   are written to 
26e6d 64 69 73 6b 20 69 6e 20 61 20 73 65 72 69 65 73  disk in a series
26e6e 0a 2a 2a 20 6f 66 20 73 6f 72 74 65 64 20 70 61  .** of sorted pa
26e6f 63 6b 65 64 2d 6d 65 6d 6f 72 79 2d 61 72 72 61  cked-memory-arra
26e70 79 73 20 28 50 4d 41 73 29 2e 20 54 68 65 20 73  ys (PMAs). The s
26e71 69 7a 65 20 6f 66 20 65 61 63 68 20 50 4d 41 20  ize of each PMA 
26e72 69 73 20 72 6f 75 67 68 6c 79 0a 2a 2a 20 74 68  is roughly.** th
26e73 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 63 61  e same as the ca
26e74 63 68 65 2d 73 69 7a 65 20 61 6c 6c 6f 77 65 64  che-size allowed
26e75 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 64   for temporary d
26e76 61 74 61 62 61 73 65 73 2e 20 49 6e 20 6f 72 64  atabases. In ord
26e77 65 72 0a 2a 2a 20 74 6f 20 61 6c 6c 6f 77 20 74  er.** to allow t
26e78 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 65 78 74  he caller to ext
26e79 72 61 63 74 20 6b 65 79 73 20 66 72 6f 6d 20 74  ract keys from t
26e7a 68 65 20 73 6f 72 74 65 72 20 69 6e 20 73 6f 72  he sorter in sor
26e7b 74 65 64 20 6f 72 64 65 72 2c 0a 2a 2a 20 61 6c  ted order,.** al
26e7c 6c 20 50 4d 41 73 20 63 75 72 72 65 6e 74 6c 79  l PMAs currently
26e7d 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20   stored on disk 
26e7e 6d 75 73 74 20 62 65 20 6d 65 72 67 65 64 20 74  must be merged t
26e7f 6f 67 65 74 68 65 72 2e 20 54 68 69 73 20 63 6f  ogether. This co
26e80 6d 6d 65 6e 74 0a 2a 2a 20 64 65 73 63 72 69 62  mment.** describ
26e81 65 73 20 74 68 65 20 64 61 74 61 20 73 74 72 75  es the data stru
26e82 63 74 75 72 65 20 75 73 65 64 20 74 6f 20 64 6f  cture used to do
26e83 20 73 6f 2e 20 54 68 65 20 73 74 72 75 63 74 75   so. The structu
26e84 72 65 20 73 75 70 70 6f 72 74 73 20 0a 2a 2a 20  re supports .** 
26e85 6d 65 72 67 69 6e 67 20 61 6e 79 20 6e 75 6d 62  merging any numb
26e86 65 72 20 6f 66 20 61 72 72 61 79 73 20 69 6e 20  er of arrays in 
26e87 61 20 73 69 6e 67 6c 65 20 70 61 73 73 20 77 69  a single pass wi
26e88 74 68 20 6e 6f 20 72 65 64 75 6e 64 61 6e 74 20  th no redundant 
26e89 63 6f 6d 70 61 72 69 73 6f 6e 20 0a 2a 2a 20 6f  comparison .** o
26e8a 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  perations..**.**
26e8b 20 54 68 65 20 61 49 74 65 72 5b 5d 20 61 72 72   The aIter[] arr
26e8c 61 79 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69  ay contains an i
26e8d 74 65 72 61 74 6f 72 20 66 6f 72 20 65 61 63 68  terator for each
26e8e 20 6f 66 20 74 68 65 20 50 4d 41 73 20 62 65 69   of the PMAs bei
26e8f 6e 67 20 6d 65 72 67 65 64 2e 0a 2a 2a 20 41 6e  ng merged..** An
26e90 20 61 49 74 65 72 5b 5d 20 69 74 65 72 61 74 6f   aIter[] iterato
26e91 72 20 65 69 74 68 65 72 20 70 6f 69 6e 74 73 20  r either points 
26e92 74 6f 20 61 20 76 61 6c 69 64 20 6b 65 79 20 6f  to a valid key o
26e93 72 20 65 6c 73 65 20 69 73 20 61 74 20 45 4f 46  r else is at EOF
26e94 2e 20 46 6f 72 20 0a 2a 2a 20 74 68 65 20 70 75  . For .** the pu
26e95 72 70 6f 73 65 73 20 6f 66 20 74 68 65 20 70 61  rposes of the pa
26e96 72 61 67 72 61 70 68 73 20 62 65 6c 6f 77 2c 20  ragraphs below, 
26e97 77 65 20 61 73 73 75 6d 65 20 74 68 61 74 20 74  we assume that t
26e98 68 65 20 61 72 72 61 79 20 69 73 20 61 63 74 75  he array is actu
26e99 61 6c 6c 79 20 0a 2a 2a 20 4e 20 65 6c 65 6d 65  ally .** N eleme
26e9a 6e 74 73 20 69 6e 20 73 69 7a 65 2c 20 77 68 65  nts in size, whe
26e9b 72 65 20 4e 20 69 73 20 74 68 65 20 73 6d 61 6c  re N is the smal
26e9c 6c 65 73 74 20 70 6f 77 65 72 20 6f 66 20 32 20  lest power of 2 
26e9d 67 72 65 61 74 65 72 20 74 6f 20 6f 72 20 65 71  greater to or eq
26e9e 75 61 6c 20 0a 2a 2a 20 74 6f 20 74 68 65 20 6e  ual .** to the n
26e9f 75 6d 62 65 72 20 6f 66 20 69 74 65 72 61 74 6f  umber of iterato
26ea0 72 73 20 62 65 69 6e 67 20 6d 65 72 67 65 64 2e  rs being merged.
26ea1 20 54 68 65 20 65 78 74 72 61 20 61 49 74 65 72   The extra aIter
26ea2 5b 5d 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20  [] elements are 
26ea3 0a 2a 2a 20 74 72 65 61 74 65 64 20 61 73 20 69  .** treated as i
26ea4 66 20 74 68 65 79 20 61 72 65 20 65 6d 70 74 79  f they are empty
26ea5 20 28 61 6c 77 61 79 73 20 61 74 20 45 4f 46 29   (always at EOF)
26ea6 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 54 72 65  ..**.** The aTre
26ea7 65 5b 5d 20 61 72 72 61 79 20 69 73 20 61 6c 73  e[] array is als
26ea8 6f 20 4e 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20  o N elements in 
26ea9 73 69 7a 65 2e 20 54 68 65 20 76 61 6c 75 65 20  size. The value 
26eaa 6f 66 20 4e 20 69 73 20 73 74 6f 72 65 64 20 69  of N is stored i
26eab 6e 0a 2a 2a 20 74 68 65 20 56 64 62 65 53 6f 72  n.** the VdbeSor
26eac 74 65 72 2e 6e 54 72 65 65 20 76 61 72 69 61 62  ter.nTree variab
26ead 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  le..**.** The fi
26eae 6e 61 6c 20 28 4e 2f 32 29 20 65 6c 65 6d 65 6e  nal (N/2) elemen
26eaf 74 73 20 6f 66 20 61 54 72 65 65 5b 5d 20 63 6f  ts of aTree[] co
26eb0 6e 74 61 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ntain the result
26eb1 73 20 6f 66 20 63 6f 6d 70 61 72 69 6e 67 0a 2a  s of comparing.*
26eb2 2a 20 70 61 69 72 73 20 6f 66 20 69 74 65 72 61  * pairs of itera
26eb3 74 6f 72 20 6b 65 79 73 20 74 6f 67 65 74 68 65  tor keys togethe
26eb4 72 2e 20 45 6c 65 6d 65 6e 74 20 69 20 63 6f 6e  r. Element i con
26eb5 74 61 69 6e 73 20 74 68 65 20 72 65 73 75 6c 74  tains the result
26eb6 20 6f 66 20 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e   of .** comparin
26eb7 67 20 61 49 74 65 72 5b 32 2a 69 2d 4e 5d 20 61  g aIter[2*i-N] a
26eb8 6e 64 20 61 49 74 65 72 5b 32 2a 69 2d 4e 2b 31  nd aIter[2*i-N+1
26eb9 5d 2e 20 57 68 69 63 68 65 76 65 72 20 6b 65 79  ]. Whichever key
26eba 20 69 73 20 73 6d 61 6c 6c 65 72 2c 20 74 68 65   is smaller, the
26ebb 0a 2a 2a 20 61 54 72 65 65 20 65 6c 65 6d 65 6e  .** aTree elemen
26ebc 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  t is set to the 
26ebd 69 6e 64 65 78 20 6f 66 20 69 74 2e 20 0a 2a 2a  index of it. .**
26ebe 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70  .** For the purp
26ebf 6f 73 65 73 20 6f 66 20 74 68 69 73 20 63 6f 6d  oses of this com
26ec0 70 61 72 69 73 6f 6e 2c 20 45 4f 46 20 69 73 20  parison, EOF is 
26ec1 63 6f 6e 73 69 64 65 72 65 64 20 67 72 65 61 74  considered great
26ec2 65 72 20 74 68 61 6e 20 61 6e 79 0a 2a 2a 20 6f  er than any.** o
26ec3 74 68 65 72 20 6b 65 79 20 76 61 6c 75 65 2e 20  ther key value. 
26ec4 49 66 20 74 68 65 20 6b 65 79 73 20 61 72 65 20  If the keys are 
26ec5 65 71 75 61 6c 20 28 6f 6e 6c 79 20 70 6f 73 73  equal (only poss
26ec6 69 62 6c 65 20 77 69 74 68 20 74 77 6f 20 45 4f  ible with two EO
26ec7 46 0a 2a 2a 20 76 61 6c 75 65 73 29 2c 20 69 74  F.** values), it
26ec8 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20   doesn't matter 
26ec9 77 68 69 63 68 20 69 6e 64 65 78 20 69 73 20 73  which index is s
26eca 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tored..**.** The
26ecb 20 28 4e 2f 34 29 20 65 6c 65 6d 65 6e 74 73 20   (N/4) elements 
26ecc 6f 66 20 61 54 72 65 65 5b 5d 20 74 68 61 74 20  of aTree[] that 
26ecd 70 72 65 63 65 65 64 20 74 68 65 20 66 69 6e 61  preceed the fina
26ece 6c 20 28 4e 2f 32 29 20 64 65 73 63 72 69 62 65  l (N/2) describe
26ecf 64 20 0a 2a 2a 20 61 62 6f 76 65 20 63 6f 6e 74  d .** above cont
26ed0 61 69 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f  ains the index o
26ed1 66 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 6f  f the smallest o
26ed2 66 20 65 61 63 68 20 62 6c 6f 63 6b 20 6f 66 20  f each block of 
26ed3 34 20 69 74 65 72 61 74 6f 72 73 2e 0a 2a 2a 20  4 iterators..** 
26ed4 41 6e 64 20 73 6f 20 6f 6e 2e 20 53 6f 20 74 68  And so on. So th
26ed5 61 74 20 61 54 72 65 65 5b 31 5d 20 63 6f 6e 74  at aTree[1] cont
26ed6 61 69 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f  ains the index o
26ed7 66 20 74 68 65 20 69 74 65 72 61 74 6f 72 20 74  f the iterator t
26ed8 68 61 74 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c  hat .** currentl
26ed9 79 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  y points to the 
26eda 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 76 61 6c  smallest key val
26edb 75 65 2e 20 61 54 72 65 65 5b 30 5d 20 69 73 20  ue. aTree[0] is 
26edc 75 6e 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 45 78  unused..**.** Ex
26edd 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ample:.**.**    
26ede 20 61 49 74 65 72 5b 30 5d 20 2d 3e 20 42 61 6e   aIter[0] -> Ban
26edf 61 6e 61 0a 2a 2a 20 20 20 20 20 61 49 74 65 72  ana.**     aIter
26ee0 5b 31 5d 20 2d 3e 20 46 65 69 6a 6f 61 0a 2a 2a  [1] -> Feijoa.**
26ee1 20 20 20 20 20 61 49 74 65 72 5b 32 5d 20 2d 3e       aIter[2] ->
26ee2 20 45 6c 64 65 72 62 65 72 72 79 0a 2a 2a 20 20   Elderberry.**  
26ee3 20 20 20 61 49 74 65 72 5b 33 5d 20 2d 3e 20 43     aIter[3] -> C
26ee4 75 72 72 61 6e 74 0a 2a 2a 20 20 20 20 20 61 49  urrant.**     aI
26ee5 74 65 72 5b 34 5d 20 2d 3e 20 47 72 61 70 65 66  ter[4] -> Grapef
26ee6 72 75 69 74 0a 2a 2a 20 20 20 20 20 61 49 74 65  ruit.**     aIte
26ee7 72 5b 35 5d 20 2d 3e 20 41 70 70 6c 65 0a 2a 2a  r[5] -> Apple.**
26ee8 20 20 20 20 20 61 49 74 65 72 5b 36 5d 20 2d 3e       aIter[6] ->
26ee9 20 44 75 72 69 61 6e 0a 2a 2a 20 20 20 20 20 61   Durian.**     a
26eea 49 74 65 72 5b 37 5d 20 2d 3e 20 45 4f 46 0a 2a  Iter[7] -> EOF.*
26eeb 2a 0a 2a 2a 20 20 20 20 20 61 54 72 65 65 5b 5d  *.**     aTree[]
26eec 20 3d 20 7b 20 58 2c 20 35 20 20 20 30 2c 20 35   = { X, 5   0, 5
26eed 20 20 20 20 30 2c 20 33 2c 20 35 2c 20 36 20 7d      0, 3, 5, 6 }
26eee 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 72 65  .**.** The curre
26eef 6e 74 20 65 6c 65 6d 65 6e 74 20 69 73 20 22 41  nt element is "A
26ef0 70 70 6c 65 22 20 28 74 68 65 20 76 61 6c 75 65  pple" (the value
26ef1 20 6f 66 20 74 68 65 20 6b 65 79 20 69 6e 64 69   of the key indi
26ef2 63 61 74 65 64 20 62 79 20 0a 2a 2a 20 69 74 65  cated by .** ite
26ef3 72 61 74 6f 72 20 35 29 2e 20 57 68 65 6e 20 74  rator 5). When t
26ef4 68 65 20 4e 65 78 74 28 29 20 6f 70 65 72 61 74  he Next() operat
26ef5 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20  ion is invoked, 
26ef6 69 74 65 72 61 74 6f 72 20 35 20 77 69 6c 6c 0a  iterator 5 will.
26ef7 2a 2a 20 62 65 20 61 64 76 61 6e 63 65 64 20 74  ** be advanced t
26ef8 6f 20 74 68 65 20 6e 65 78 74 20 6b 65 79 20 69  o the next key i
26ef9 6e 20 69 74 73 20 73 65 67 6d 65 6e 74 2e 20 53  n its segment. S
26efa 61 79 20 74 68 65 20 6e 65 78 74 20 6b 65 79 20  ay the next key 
26efb 69 73 0a 2a 2a 20 22 45 67 67 70 6c 61 6e 74 22  is.** "Eggplant"
26efc 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 49 74 65  :.**.**     aIte
26efd 72 5b 35 5d 20 2d 3e 20 45 67 67 70 6c 61 6e 74  r[5] -> Eggplant
26efe 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65  .**.** The conte
26eff 6e 74 73 20 6f 66 20 61 54 72 65 65 5b 5d 20 61  nts of aTree[] a
26f00 72 65 20 75 70 64 61 74 65 64 20 66 69 72 73 74  re updated first
26f01 20 62 79 20 63 6f 6d 70 61 72 69 6e 67 20 74 68   by comparing th
26f02 65 20 6e 65 77 20 69 74 65 72 61 74 6f 72 0a 2a  e new iterator.*
26f03 2a 20 35 20 6b 65 79 20 74 6f 20 74 68 65 20 63  * 5 key to the c
26f04 75 72 72 65 6e 74 20 6b 65 79 20 6f 66 20 69 74  urrent key of it
26f05 65 72 61 74 6f 72 20 34 20 28 73 74 69 6c 6c 20  erator 4 (still 
26f06 22 47 72 61 70 65 66 72 75 69 74 22 29 2e 20 54  "Grapefruit"). T
26f07 68 65 20 69 74 65 72 61 74 6f 72 0a 2a 2a 20 35  he iterator.** 5
26f08 20 76 61 6c 75 65 20 69 73 20 73 74 69 6c 6c 20   value is still 
26f09 73 6d 61 6c 6c 65 72 2c 20 73 6f 20 61 54 72 65  smaller, so aTre
26f0a 65 5b 36 5d 20 69 73 20 73 65 74 20 74 6f 20 35  e[6] is set to 5
26f0b 2e 20 41 6e 64 20 73 6f 20 6f 6e 20 75 70 20 74  . And so on up t
26f0c 68 65 20 74 72 65 65 2e 0a 2a 2a 20 54 68 65 20  he tree..** The 
26f0d 76 61 6c 75 65 20 6f 66 20 69 74 65 72 61 74 6f  value of iterato
26f0e 72 20 36 20 2d 20 22 44 75 72 69 61 6e 22 20 2d  r 6 - "Durian" -
26f0f 20 69 73 20 6e 6f 77 20 73 6d 61 6c 6c 65 72 20   is now smaller 
26f10 74 68 61 6e 20 74 68 61 74 20 6f 66 20 69 74 65  than that of ite
26f11 72 61 74 6f 72 0a 2a 2a 20 35 2c 20 73 6f 20 61  rator.** 5, so a
26f12 54 72 65 65 5b 33 5d 20 69 73 20 73 65 74 20 74  Tree[3] is set t
26f13 6f 20 36 2e 20 4b 65 79 20 30 20 69 73 20 73 6d  o 6. Key 0 is sm
26f14 61 6c 6c 65 72 20 74 68 61 6e 20 6b 65 79 20 36  aller than key 6
26f15 20 28 42 61 6e 61 6e 61 3c 44 75 72 69 61 6e 29   (Banana<Durian)
26f16 2c 0a 2a 2a 20 73 6f 20 74 68 65 20 76 61 6c 75  ,.** so the valu
26f17 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 65  e written into e
26f18 6c 65 6d 65 6e 74 20 31 20 6f 66 20 74 68 65 20  lement 1 of the 
26f19 61 72 72 61 79 20 69 73 20 30 2e 20 41 73 20 66  array is 0. As f
26f1a 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
26f1b 20 20 61 54 72 65 65 5b 5d 20 3d 20 7b 20 58 2c    aTree[] = { X,
26f1c 20 30 20 20 20 30 2c 20 36 20 20 20 20 30 2c 20   0   0, 6    0, 
26f1d 33 2c 20 35 2c 20 36 20 7d 0a 2a 2a 0a 2a 2a 20  3, 5, 6 }.**.** 
26f1e 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
26f1f 65 61 63 68 20 74 69 6d 65 20 77 65 20 61 64 76  each time we adv
26f20 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 74  ance to the next
26f21 20 73 6f 72 74 65 72 20 65 6c 65 6d 65 6e 74 2c   sorter element,
26f22 20 6c 6f 67 32 28 4e 29 0a 2a 2a 20 6b 65 79 20   log2(N).** key 
26f23 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61  comparison opera
26f24 74 69 6f 6e 73 20 61 72 65 20 72 65 71 75 69 72  tions are requir
26f25 65 64 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74  ed, where N is t
26f26 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 67  he number of seg
26f27 6d 65 6e 74 73 0a 2a 2a 20 62 65 69 6e 67 20 6d  ments.** being m
26f28 65 72 67 65 64 20 28 72 6f 75 6e 64 65 64 20 75  erged (rounded u
26f29 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 70 6f  p to the next po
26f2a 77 65 72 20 6f 66 20 32 29 2e 0a 2a 2f 0a 73 74  wer of 2)..*/.st
26f2b 72 75 63 74 20 56 64 62 65 53 6f 72 74 65 72 20  ruct VdbeSorter 
26f2c 7b 0a 20 20 69 36 34 20 69 57 72 69 74 65 4f 66  {.  i64 iWriteOf
26f2d 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
26f2e 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 77      /* Current w
26f2f 72 69 74 65 20 6f 66 66 73 65 74 20 77 69 74 68  rite offset with
26f30 69 6e 20 66 69 6c 65 20 70 54 65 6d 70 31 20 2a  in file pTemp1 *
26f31 2f 0a 20 20 69 36 34 20 69 52 65 61 64 4f 66 66  /.  i64 iReadOff
26f32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26f33 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 72      /* Current r
26f34 65 61 64 20 6f 66 66 73 65 74 20 77 69 74 68 69  ead offset withi
26f35 6e 20 66 69 6c 65 20 70 54 65 6d 70 31 20 2a 2f  n file pTemp1 */
26f36 0a 20 20 69 6e 74 20 6e 49 6e 4d 65 6d 6f 72 79  .  int nInMemory
26f37 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26f38 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 69     /* Current si
26f39 7a 65 20 6f 66 20 70 52 65 63 6f 72 64 20 6c 69  ze of pRecord li
26f3a 73 74 20 61 73 20 50 4d 41 20 2a 2f 0a 20 20 69  st as PMA */.  i
26f3b 6e 74 20 6e 54 72 65 65 3b 20 20 20 20 20 20 20  nt nTree;       
26f3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26f3d 2a 20 55 73 65 64 20 73 69 7a 65 20 6f 66 20 61  * Used size of a
26f3e 54 72 65 65 2f 61 49 74 65 72 20 28 70 6f 77 65  Tree/aIter (powe
26f3f 72 20 6f 66 20 32 29 20 2a 2f 0a 20 20 69 6e 74  r of 2) */.  int
26f40 20 6e 50 4d 41 3b 20 20 20 20 20 20 20 20 20 20   nPMA;          
26f41 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26f42 4e 75 6d 62 65 72 20 6f 66 20 50 4d 41 73 20 73  Number of PMAs s
26f43 74 6f 72 65 64 20 69 6e 20 70 54 65 6d 70 31 20  tored in pTemp1 
26f44 2a 2f 0a 20 20 69 6e 74 20 6d 6e 50 6d 61 53 69  */.  int mnPmaSi
26f45 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
26f46 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20       /* Minimum 
26f47 50 4d 41 20 73 69 7a 65 2c 20 69 6e 20 62 79 74  PMA size, in byt
26f48 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 6d  es */.  int mxPm
26f49 61 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  aSize;          
26f4a 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
26f4b 75 6d 20 50 4d 41 20 73 69 7a 65 2c 20 69 6e 20  um PMA size, in 
26f4c 62 79 74 65 73 2e 20 20 30 3d 3d 6e 6f 20 6c 69  bytes.  0==no li
26f4d 6d 69 74 20 2a 2f 0a 20 20 56 64 62 65 53 6f 72  mit */.  VdbeSor
26f4e 74 65 72 49 74 65 72 20 2a 61 49 74 65 72 3b 20  terIter *aIter; 
26f4f 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61           /* Arra
26f50 79 20 6f 66 20 69 74 65 72 61 74 6f 72 73 20 74  y of iterators t
26f51 6f 20 6d 65 72 67 65 20 2a 2f 0a 20 20 69 6e 74  o merge */.  int
26f52 20 2a 61 54 72 65 65 3b 20 20 20 20 20 20 20 20   *aTree;        
26f53 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26f54 43 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66  Current state of
26f55 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 6d 65 72   incremental mer
26f56 67 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ge */.  sqlite3_
26f57 66 69 6c 65 20 2a 70 54 65 6d 70 31 3b 20 20 20  file *pTemp1;   
26f58 20 20 20 20 20 20 20 20 2f 2a 20 50 4d 41 20 66          /* PMA f
26f59 69 6c 65 20 31 20 2a 2f 0a 20 20 53 6f 72 74 65  ile 1 */.  Sorte
26f5a 72 52 65 63 6f 72 64 20 2a 70 52 65 63 6f 72 64  rRecord *pRecord
26f5b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65  ;          /* He
26f5c 61 64 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20  ad of in-memory 
26f5d 72 65 63 6f 72 64 20 6c 69 73 74 20 2a 2f 0a 20  record list */. 
26f5e 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
26f5f 2a 70 55 6e 70 61 63 6b 65 64 3b 20 20 20 20 20  *pUnpacked;     
26f60 20 2f 2a 20 55 73 65 64 20 74 6f 20 75 6e 70 61   /* Used to unpa
26f61 63 6b 20 6b 65 79 73 20 2a 2f 0a 7d 3b 0a 0a 2f  ck keys */.};../
26f62 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
26f63 6e 67 20 74 79 70 65 20 69 73 20 61 6e 20 69 74  ng type is an it
26f64 65 72 61 74 6f 72 20 66 6f 72 20 61 20 50 4d 41  erator for a PMA
26f65 2e 20 49 74 20 63 61 63 68 65 73 20 74 68 65 20  . It caches the 
26f66 63 75 72 72 65 6e 74 20 6b 65 79 20 69 6e 20 0a  current key in .
26f67 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 6e 4b 65  ** variables nKe
26f68 79 2f 61 4b 65 79 2e 20 49 66 20 74 68 65 20 69  y/aKey. If the i
26f69 74 65 72 61 74 6f 72 20 69 73 20 61 74 20 45 4f  terator is at EO
26f6a 46 2c 20 70 46 69 6c 65 3d 3d 30 2e 0a 2a 2f 0a  F, pFile==0..*/.
26f6b 73 74 72 75 63 74 20 56 64 62 65 53 6f 72 74 65  struct VdbeSorte
26f6c 72 49 74 65 72 20 7b 0a 20 20 69 36 34 20 69 52  rIter {.  i64 iR
26f6d 65 61 64 4f 66 66 3b 20 20 20 20 20 20 20 20 20  eadOff;         
26f6e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
26f6f 72 65 6e 74 20 72 65 61 64 20 6f 66 66 73 65 74  rent read offset
26f70 20 2a 2f 0a 20 20 69 36 34 20 69 45 6f 66 3b 20   */.  i64 iEof; 
26f71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26f72 20 20 20 20 20 20 2f 2a 20 31 20 62 79 74 65 20        /* 1 byte 
26f73 70 61 73 74 20 45 4f 46 20 66 6f 72 20 74 68 69  past EOF for thi
26f74 73 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20  s iterator */.  
26f75 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20  int nAlloc;     
26f76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26f77 2f 2a 20 42 79 74 65 73 20 6f 66 20 73 70 61 63  /* Bytes of spac
26f78 65 20 61 74 20 61 41 6c 6c 6f 63 20 2a 2f 0a 20  e at aAlloc */. 
26f79 20 69 6e 74 20 6e 4b 65 79 3b 20 20 20 20 20 20   int nKey;      
26f7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26f7b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
26f7c 74 65 73 20 69 6e 20 6b 65 79 20 2a 2f 0a 20 20  tes in key */.  
26f7d 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46  sqlite3_file *pF
26f7e 69 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ile;            
26f7f 2f 2a 20 46 69 6c 65 20 69 74 65 72 61 74 6f 72  /* File iterator
26f80 20 69 73 20 72 65 61 64 69 6e 67 20 66 72 6f 6d   is reading from
26f81 20 2a 2f 0a 20 20 75 38 20 2a 61 41 6c 6c 6f 63   */.  u8 *aAlloc
26f82 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26f83 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74        /* Allocat
26f84 65 64 20 73 70 61 63 65 20 2a 2f 0a 20 20 75 38  ed space */.  u8
26f85 20 2a 61 4b 65 79 3b 20 20 20 20 20 20 20 20 20   *aKey;         
26f86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26f87 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 75 72 72   Pointer to curr
26f88 65 6e 74 20 6b 65 79 20 2a 2f 0a 20 20 75 38 20  ent key */.  u8 
26f89 2a 61 42 75 66 66 65 72 3b 20 20 20 20 20 20 20  *aBuffer;       
26f8a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26f8b 43 75 72 72 65 6e 74 20 72 65 61 64 20 62 75 66  Current read buf
26f8c 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75  fer */.  int nBu
26f8d 66 66 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  ffer;           
26f8e 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
26f8f 20 6f 66 20 72 65 61 64 20 62 75 66 66 65 72 20   of read buffer 
26f90 69 6e 20 62 79 74 65 73 20 2a 2f 0a 7d 3b 0a 0a  in bytes */.};..
26f91 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
26f92 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
26f93 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 6f  ure is used to o
26f94 72 67 61 6e 69 7a 65 20 74 68 65 20 73 74 72 65  rganize the stre
26f95 61 6d 20 6f 66 20 72 65 63 6f 72 64 73 0a 2a 2a  am of records.**
26f96 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20 74   being written t
26f97 6f 20 66 69 6c 65 73 20 62 79 20 74 68 65 20 6d  o files by the m
26f98 65 72 67 65 2d 73 6f 72 74 20 63 6f 64 65 20 69  erge-sort code i
26f99 6e 74 6f 20 61 6c 69 67 6e 65 64 2c 20 70 61 67  nto aligned, pag
26f9a 65 2d 73 69 7a 65 64 0a 2a 2a 20 62 6c 6f 63 6b  e-sized.** block
26f9b 73 2e 20 20 44 6f 69 6e 67 20 61 6c 6c 20 49 2f  s.  Doing all I/
26f9c 4f 20 69 6e 20 61 6c 69 67 6e 65 64 20 70 61 67  O in aligned pag
26f9d 65 2d 73 69 7a 65 64 20 62 6c 6f 63 6b 73 20 68  e-sized blocks h
26f9e 65 6c 70 73 20 49 2f 4f 20 74 6f 20 67 6f 0a 2a  elps I/O to go.*
26f9f 2a 20 66 61 73 74 65 72 20 6f 6e 20 6d 61 6e 79  * faster on many
26fa0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
26fa1 6d 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69  ms..*/.struct Fi
26fa2 6c 65 57 72 69 74 65 72 20 7b 0a 20 20 69 6e 74  leWriter {.  int
26fa3 20 65 46 57 45 72 72 3b 20 20 20 20 20 20 20 20   eFWErr;        
26fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26fa5 4e 6f 6e 2d 7a 65 72 6f 20 69 66 20 69 6e 20 61  Non-zero if in a
26fa6 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 2a 2f  n error state */
26fa7 0a 20 20 75 38 20 2a 61 42 75 66 66 65 72 3b 20  .  u8 *aBuffer; 
26fa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26fa9 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
26faa 20 77 72 69 74 65 20 62 75 66 66 65 72 20 2a 2f   write buffer */
26fab 0a 20 20 69 6e 74 20 6e 42 75 66 66 65 72 3b 20  .  int nBuffer; 
26fac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26fad 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 77 72     /* Size of wr
26fae 69 74 65 20 62 75 66 66 65 72 20 69 6e 20 62 79  ite buffer in by
26faf 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 42 75  tes */.  int iBu
26fb0 66 53 74 61 72 74 3b 20 20 20 20 20 20 20 20 20  fStart;         
26fb1 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
26fb2 74 20 62 79 74 65 20 6f 66 20 62 75 66 66 65 72  t byte of buffer
26fb3 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 69   to write */.  i
26fb4 6e 74 20 69 42 75 66 45 6e 64 3b 20 20 20 20 20  nt iBufEnd;     
26fb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26fb6 2a 20 4c 61 73 74 20 62 79 74 65 20 6f 66 20 62  * Last byte of b
26fb7 75 66 66 65 72 20 74 6f 20 77 72 69 74 65 20 2a  uffer to write *
26fb8 2f 0a 20 20 69 36 34 20 69 57 72 69 74 65 4f 66  /.  i64 iWriteOf
26fb9 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
26fba 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66      /* Offset of
26fbb 20 73 74 61 72 74 20 6f 66 20 62 75 66 66 65 72   start of buffer
26fbc 20 69 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71   in file */.  sq
26fbd 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
26fbe 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
26fbf 20 46 69 6c 65 20 74 6f 20 77 72 69 74 65 20 74   File to write t
26fc0 6f 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  o */.};../*.** A
26fc1 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 73 74   structure to st
26fc2 6f 72 65 20 61 20 73 69 6e 67 6c 65 20 72 65 63  ore a single rec
26fc3 6f 72 64 2e 20 41 6c 6c 20 69 6e 2d 6d 65 6d 6f  ord. All in-memo
26fc4 72 79 20 72 65 63 6f 72 64 73 20 61 72 65 20 63  ry records are c
26fc5 6f 6e 6e 65 63 74 65 64 0a 2a 2a 20 74 6f 67 65  onnected.** toge
26fc6 74 68 65 72 20 69 6e 74 6f 20 61 20 6c 69 6e 6b  ther into a link
26fc7 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 20 61  ed list headed a
26fc8 74 20 56 64 62 65 53 6f 72 74 65 72 2e 70 52 65  t VdbeSorter.pRe
26fc9 63 6f 72 64 20 75 73 69 6e 67 20 74 68 65 20 0a  cord using the .
26fca 2a 2a 20 53 6f 72 74 65 72 52 65 63 6f 72 64 2e  ** SorterRecord.
26fcb 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 2e 0a 2a  pNext pointer..*
26fcc 2f 0a 73 74 72 75 63 74 20 53 6f 72 74 65 72 52  /.struct SorterR
26fcd 65 63 6f 72 64 20 7b 0a 20 20 76 6f 69 64 20 2a  ecord {.  void *
26fce 70 56 61 6c 3b 0a 20 20 69 6e 74 20 6e 56 61 6c  pVal;.  int nVal
26fcf 3b 0a 20 20 53 6f 72 74 65 72 52 65 63 6f 72 64  ;.  SorterRecord
26fd0 20 2a 70 4e 65 78 74 3b 0a 7d 3b 0a 0a 2f 2a 20   *pNext;.};../* 
26fd1 4d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 61 62 6c  Minimum allowabl
26fd2 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  e value for the 
26fd3 56 64 62 65 53 6f 72 74 65 72 2e 6e 57 6f 72 6b  VdbeSorter.nWork
26fd4 69 6e 67 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  ing variable */.
26fd5 23 64 65 66 69 6e 65 20 53 4f 52 54 45 52 5f 4d  #define SORTER_M
26fd6 49 4e 5f 57 4f 52 4b 49 4e 47 20 31 30 0a 0a 2f  IN_WORKING 10../
26fd7 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  * Maximum number
26fd8 20 6f 66 20 73 65 67 6d 65 6e 74 73 20 74 6f 20   of segments to 
26fd9 6d 65 72 67 65 20 69 6e 20 61 20 73 69 6e 67 6c  merge in a singl
26fda 65 20 70 61 73 73 2e 20 2a 2f 0a 23 64 65 66 69  e pass. */.#defi
26fdb 6e 65 20 53 4f 52 54 45 52 5f 4d 41 58 5f 4d 45  ne SORTER_MAX_ME
26fdc 52 47 45 5f 43 4f 55 4e 54 20 31 36 0a 0a 2f 2a  RGE_COUNT 16../*
26fdd 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 6d 65 6d  .** Free all mem
26fde 6f 72 79 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f  ory belonging to
26fdf 20 74 68 65 20 56 64 62 65 53 6f 72 74 65 72 49   the VdbeSorterI
26fe0 74 65 72 20 6f 62 6a 65 63 74 20 70 61 73 73 65  ter object passe
26fe1 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 0a  d as the second.
26fe2 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 41 6c 6c  ** argument. All
26fe3 20 73 74 72 75 63 74 75 72 65 20 66 69 65 6c 64   structure field
26fe4 73 20 61 72 65 20 73 65 74 20 74 6f 20 7a 65 72  s are set to zer
26fe5 6f 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  o before returni
26fe6 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ng..*/.static vo
26fe7 69 64 20 76 64 62 65 53 6f 72 74 65 72 49 74 65  id vdbeSorterIte
26fe8 72 5a 65 72 6f 28 73 71 6c 69 74 65 33 20 2a 64  rZero(sqlite3 *d
26fe9 62 2c 20 56 64 62 65 53 6f 72 74 65 72 49 74 65  b, VdbeSorterIte
26fea 72 20 2a 70 49 74 65 72 29 7b 0a 20 20 73 71 6c  r *pIter){.  sql
26feb 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
26fec 49 74 65 72 2d 3e 61 41 6c 6c 6f 63 29 3b 0a 20  Iter->aAlloc);. 
26fed 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
26fee 62 2c 20 70 49 74 65 72 2d 3e 61 42 75 66 66 65  b, pIter->aBuffe
26fef 72 29 3b 0a 20 20 6d 65 6d 73 65 74 28 70 49 74  r);.  memset(pIt
26ff0 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 56 64  er, 0, sizeof(Vd
26ff1 62 65 53 6f 72 74 65 72 49 74 65 72 29 29 3b 0a  beSorterIter));.
26ff2 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6e 42  }../*.** Read nB
26ff3 79 74 65 20 62 79 74 65 73 20 6f 66 20 64 61 74  yte bytes of dat
26ff4 61 20 66 72 6f 6d 20 74 68 65 20 73 74 72 65 61  a from the strea
26ff5 6d 20 6f 66 20 64 61 74 61 20 69 74 65 72 61 74  m of data iterat
26ff6 65 64 20 62 79 20 6f 62 6a 65 63 74 20 70 2e 0a  ed by object p..
26ff7 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
26ff8 2c 20 73 65 74 20 2a 70 70 4f 75 74 20 74 6f 20  , set *ppOut to 
26ff9 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65  point to a buffe
26ffa 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  r containing the
26ffb 20 64 61 74 61 0a 2a 2a 20 61 6e 64 20 72 65 74   data.** and ret
26ffc 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f  urn SQLITE_OK. O
26ffd 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e 20  therwise, if an 
26ffe 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65  error occurs, re
26fff 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65 0a 2a  turn an SQLite.*
27000 2a 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a  * error code..**
27001 0a 2a 2a 20 54 68 65 20 62 75 66 66 65 72 20 69  .** The buffer i
27002 6e 64 69 63 61 74 65 64 20 62 79 20 2a 70 70 4f  ndicated by *ppO
27003 75 74 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63  ut may only be c
27004 6f 6e 73 69 64 65 72 65 64 20 76 61 6c 69 64 20  onsidered valid 
27005 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 6e 65 78  until the.** nex
27006 74 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 66  t call to this f
27007 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
27008 69 63 20 69 6e 74 20 76 64 62 65 53 6f 72 74 65  ic int vdbeSorte
27009 72 49 74 65 72 52 65 61 64 28 0a 20 20 73 71 6c  rIterRead(.  sql
2700a 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2700b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2700c 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  Database handle 
2700d 28 66 6f 72 20 6d 61 6c 6c 6f 63 29 20 2a 2f 0a  (for malloc) */.
2700e 20 20 56 64 62 65 53 6f 72 74 65 72 49 74 65 72    VdbeSorterIter
2700f 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
27010 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 2a 2f    /* Iterator */
27011 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20  .  int nByte,   
27012 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27013 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 64     /* Bytes of d
27014 61 74 61 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20  ata to read */. 
27015 20 75 38 20 2a 2a 70 70 4f 75 74 20 20 20 20 20   u8 **ppOut     
27016 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27017 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72   /* OUT: Pointer
27018 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e 74 61   to buffer conta
27019 69 6e 69 6e 67 20 64 61 74 61 20 2a 2f 0a 29 7b  ining data */.){
2701a 0a 20 20 69 6e 74 20 69 42 75 66 3b 20 20 20 20  .  int iBuf;    
2701b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2701c 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 77 69 74     /* Offset wit
2701d 68 69 6e 20 62 75 66 66 65 72 20 74 6f 20 72 65  hin buffer to re
2701e 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74  ad from */.  int
2701f 20 6e 41 76 61 69 6c 3b 20 20 20 20 20 20 20 20   nAvail;        
27020 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27021 42 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 76  Bytes of data av
27022 61 69 6c 61 62 6c 65 20 69 6e 20 62 75 66 66 65  ailable in buffe
27023 72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  r */.  assert( p
27024 2d 3e 61 42 75 66 66 65 72 20 29 3b 0a 0a 20 20  ->aBuffer );..  
27025 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  /* If there is n
27026 6f 20 6d 6f 72 65 20 64 61 74 61 20 74 6f 20 62  o more data to b
27027 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  e read from the 
27028 62 75 66 66 65 72 2c 20 72 65 61 64 20 74 68 65  buffer, read the
27029 20 6e 65 78 74 20 0a 20 20 2a 2a 20 70 2d 3e 6e   next .  ** p->n
2702a 42 75 66 66 65 72 20 62 79 74 65 73 20 6f 66 20  Buffer bytes of 
2702b 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 66 69  data from the fi
2702c 6c 65 20 69 6e 74 6f 20 69 74 2e 20 4f 72 2c 20  le into it. Or, 
2702d 69 66 20 74 68 65 72 65 20 61 72 65 20 6c 65 73  if there are les
2702e 73 0a 20 20 2a 2a 20 74 68 61 6e 20 70 2d 3e 6e  s.  ** than p->n
2702f 42 75 66 66 65 72 20 62 79 74 65 73 20 72 65 6d  Buffer bytes rem
27030 61 69 6e 69 6e 67 20 69 6e 20 74 68 65 20 50 4d  aining in the PM
27031 41 2c 20 72 65 61 64 20 61 6c 6c 20 72 65 6d 61  A, read all rema
27032 69 6e 69 6e 67 20 64 61 74 61 2e 20 20 2a 2f 0a  ining data.  */.
27033 20 20 69 42 75 66 20 3d 20 70 2d 3e 69 52 65 61    iBuf = p->iRea
27034 64 4f 66 66 20 25 20 70 2d 3e 6e 42 75 66 66 65  dOff % p->nBuffe
27035 72 3b 0a 20 20 69 66 28 20 69 42 75 66 3d 3d 30  r;.  if( iBuf==0
27036 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 52 65 61   ){.    int nRea
27037 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
27038 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74        /* Bytes t
27039 6f 20 72 65 61 64 20 66 72 6f 6d 20 64 69 73 6b  o read from disk
2703a 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 3b 20   */.    int rc; 
2703b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2703c 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33        /* sqlite3
2703d 4f 73 52 65 61 64 28 29 20 72 65 74 75 72 6e 20  OsRead() return 
2703e 63 6f 64 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  code */..    /* 
2703f 44 65 74 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61  Determine how ma
27040 6e 79 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  ny bytes of data
27041 20 74 6f 20 72 65 61 64 2e 20 2a 2f 0a 20 20 20   to read. */.   
27042 20 69 66 28 20 28 70 2d 3e 69 45 6f 66 20 2d 20   if( (p->iEof - 
27043 70 2d 3e 69 52 65 61 64 4f 66 66 29 20 3e 20 28  p->iReadOff) > (
27044 69 36 34 29 70 2d 3e 6e 42 75 66 66 65 72 20 29  i64)p->nBuffer )
27045 7b 0a 20 20 20 20 20 20 6e 52 65 61 64 20 3d 20  {.      nRead = 
27046 70 2d 3e 6e 42 75 66 66 65 72 3b 0a 20 20 20 20  p->nBuffer;.    
27047 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 52 65  }else{.      nRe
27048 61 64 20 3d 20 28 69 6e 74 29 28 70 2d 3e 69 45  ad = (int)(p->iE
27049 6f 66 20 2d 20 70 2d 3e 69 52 65 61 64 4f 66 66  of - p->iReadOff
2704a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  );.    }.    ass
2704b 65 72 74 28 20 6e 52 65 61 64 3e 30 20 29 3b 0a  ert( nRead>0 );.
2704c 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 64 61 74  .    /* Read dat
2704d 61 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e  a from the file.
2704e 20 52 65 74 75 72 6e 20 65 61 72 6c 79 20 69 66   Return early if
2704f 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
27050 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71  . */.    rc = sq
27051 6c 69 74 65 33 4f 73 52 65 61 64 28 70 2d 3e 70  lite3OsRead(p->p
27052 46 69 6c 65 2c 20 70 2d 3e 61 42 75 66 66 65 72  File, p->aBuffer
27053 2c 20 6e 52 65 61 64 2c 20 70 2d 3e 69 52 65 61  , nRead, p->iRea
27054 64 4f 66 66 29 3b 0a 20 20 20 20 61 73 73 65 72  dOff);.    asser
27055 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f  t( rc!=SQLITE_IO
27056 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29  ERR_SHORT_READ )
27057 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
27058 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
27059 20 72 63 3b 0a 20 20 7d 0a 20 20 6e 41 76 61 69   rc;.  }.  nAvai
2705a 6c 20 3d 20 70 2d 3e 6e 42 75 66 66 65 72 20 2d  l = p->nBuffer -
2705b 20 69 42 75 66 3b 20 0a 0a 20 20 69 66 28 20 6e   iBuf; ..  if( n
2705c 42 79 74 65 3c 3d 6e 41 76 61 69 6c 20 29 7b 0a  Byte<=nAvail ){.
2705d 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65      /* The reque
2705e 73 74 65 64 20 64 61 74 61 20 69 73 20 61 76 61  sted data is ava
2705f 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 69 6e  ilable in the in
27060 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 2e 20  -memory buffer. 
27061 49 6e 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 63  In this.    ** c
27062 61 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20  ase there is no 
27063 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 63  need to make a c
27064 6f 70 79 20 6f 66 20 74 68 65 20 64 61 74 61 2c  opy of the data,
27065 20 6a 75 73 74 20 72 65 74 75 72 6e 20 61 20 0a   just return a .
27066 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 69      ** pointer i
27067 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 74  nto the buffer t
27068 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 20 2a  o the caller.  *
27069 2f 0a 20 20 20 20 2a 70 70 4f 75 74 20 3d 20 26  /.    *ppOut = &
2706a 70 2d 3e 61 42 75 66 66 65 72 5b 69 42 75 66 5d  p->aBuffer[iBuf]
2706b 3b 0a 20 20 20 20 70 2d 3e 69 52 65 61 64 4f 66  ;.    p->iReadOf
2706c 66 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 7d 65  f += nByte;.  }e
2706d 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  lse{.    /* The 
2706e 72 65 71 75 65 73 74 65 64 20 64 61 74 61 20 69  requested data i
2706f 73 20 6e 6f 74 20 61 6c 6c 20 61 76 61 69 6c 61  s not all availa
27070 62 6c 65 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65  ble in the in-me
27071 6d 6f 72 79 20 62 75 66 66 65 72 2e 0a 20 20 20  mory buffer..   
27072 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65   ** In this case
27073 2c 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65  , allocate space
27074 20 61 74 20 70 2d 3e 61 41 6c 6c 6f 63 5b 5d 20   at p->aAlloc[] 
27075 74 6f 20 63 6f 70 79 20 74 68 65 20 72 65 71 75  to copy the requ
27076 65 73 74 65 64 0a 20 20 20 20 2a 2a 20 72 61 6e  ested.    ** ran
27077 67 65 20 69 6e 74 6f 2e 20 54 68 65 6e 20 72 65  ge into. Then re
27078 74 75 72 6e 20 61 20 63 6f 70 79 20 6f 66 20 70  turn a copy of p
27079 6f 69 6e 74 65 72 20 70 2d 3e 61 41 6c 6c 6f 63  ointer p->aAlloc
2707a 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   to the caller. 
2707b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 52 65 6d   */.    int nRem
2707c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2707d 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 72        /* Bytes r
2707e 65 6d 61 69 6e 69 6e 67 20 74 6f 20 63 6f 70 79  emaining to copy
2707f 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 45 78 74 65   */..    /* Exte
27080 6e 64 20 74 68 65 20 70 2d 3e 61 41 6c 6c 6f 63  nd the p->aAlloc
27081 5b 5d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 66  [] allocation if
27082 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20   required. */.  
27083 20 20 69 66 28 20 70 2d 3e 6e 41 6c 6c 6f 63 3c    if( p->nAlloc<
27084 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 69  nByte ){.      i
27085 6e 74 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 41 6c  nt nNew = p->nAl
27086 6c 6f 63 2a 32 3b 0a 20 20 20 20 20 20 77 68 69  loc*2;.      whi
27087 6c 65 28 20 6e 42 79 74 65 3e 6e 4e 65 77 20 29  le( nByte>nNew )
27088 20 6e 4e 65 77 20 3d 20 6e 4e 65 77 2a 32 3b 0a   nNew = nNew*2;.
27089 20 20 20 20 20 20 70 2d 3e 61 41 6c 6c 6f 63 20        p->aAlloc 
2708a 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c  = sqlite3DbReall
2708b 6f 63 4f 72 46 72 65 65 28 64 62 2c 20 70 2d 3e  ocOrFree(db, p->
2708c 61 41 6c 6c 6f 63 2c 20 6e 4e 65 77 29 3b 0a 20  aAlloc, nNew);. 
2708d 20 20 20 20 20 69 66 28 20 21 70 2d 3e 61 41 6c       if( !p->aAl
2708e 6c 6f 63 20 29 20 72 65 74 75 72 6e 20 53 51 4c  loc ) return SQL
2708f 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
27090 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 4e 65   p->nAlloc = nNe
27091 77 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  w;.    }..    /*
27092 20 43 6f 70 79 20 61 73 20 6d 75 63 68 20 64 61   Copy as much da
27093 74 61 20 61 73 20 69 73 20 61 76 61 69 6c 61 62  ta as is availab
27094 6c 65 20 69 6e 20 74 68 65 20 62 75 66 66 65 72  le in the buffer
27095 20 69 6e 74 6f 20 74 68 65 20 73 74 61 72 74 20   into the start 
27096 6f 66 0a 20 20 20 20 2a 2a 20 70 2d 3e 61 41 6c  of.    ** p->aAl
27097 6c 6f 63 5b 5d 2e 20 20 2a 2f 0a 20 20 20 20 6d  loc[].  */.    m
27098 65 6d 63 70 79 28 70 2d 3e 61 41 6c 6c 6f 63 2c  emcpy(p->aAlloc,
27099 20 26 70 2d 3e 61 42 75 66 66 65 72 5b 69 42 75   &p->aBuffer[iBu
2709a 66 5d 2c 20 6e 41 76 61 69 6c 29 3b 0a 20 20 20  f], nAvail);.   
2709b 20 70 2d 3e 69 52 65 61 64 4f 66 66 20 2b 3d 20   p->iReadOff += 
2709c 6e 41 76 61 69 6c 3b 0a 20 20 20 20 6e 52 65 6d  nAvail;.    nRem
2709d 20 3d 20 6e 42 79 74 65 20 2d 20 6e 41 76 61 69   = nByte - nAvai
2709e 6c 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66  l;..    /* The f
2709f 6f 6c 6c 6f 77 69 6e 67 20 6c 6f 6f 70 20 63 6f  ollowing loop co
270a0 70 69 65 73 20 75 70 20 74 6f 20 70 2d 3e 6e 42  pies up to p->nB
270a1 75 66 66 65 72 20 62 79 74 65 73 20 70 65 72 20  uffer bytes per 
270a2 69 74 65 72 61 74 69 6f 6e 20 69 6e 74 6f 0a 20  iteration into. 
270a3 20 20 20 2a 2a 20 74 68 65 20 70 2d 3e 61 41 6c     ** the p->aAl
270a4 6c 6f 63 5b 5d 20 62 75 66 66 65 72 2e 20 20 2a  loc[] buffer.  *
270a5 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 52 65  /.    while( nRe
270a6 6d 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  m>0 ){.      int
270a7 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
270a8 20 20 20 20 20 20 20 20 20 2f 2a 20 76 64 62 65           /* vdbe
270a9 53 6f 72 74 65 72 49 74 65 72 52 65 61 64 28 29  SorterIterRead()
270aa 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   return code */.
270ab 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 3b        int nCopy;
270ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
270ad 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
270ae 79 74 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a  ytes to copy */.
270af 20 20 20 20 20 20 75 38 20 2a 61 4e 65 78 74 3b        u8 *aNext;
270b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
270b1 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
270b2 62 75 66 66 65 72 20 74 6f 20 63 6f 70 79 20 64  buffer to copy d
270b3 61 74 61 20 66 72 6f 6d 20 2a 2f 0a 0a 20 20 20  ata from */..   
270b4 20 20 20 6e 43 6f 70 79 20 3d 20 6e 52 65 6d 3b     nCopy = nRem;
270b5 0a 20 20 20 20 20 20 69 66 28 20 6e 52 65 6d 3e  .      if( nRem>
270b6 70 2d 3e 6e 42 75 66 66 65 72 20 29 20 6e 43 6f  p->nBuffer ) nCo
270b7 70 79 20 3d 20 70 2d 3e 6e 42 75 66 66 65 72 3b  py = p->nBuffer;
270b8 0a 20 20 20 20 20 20 72 63 20 3d 20 76 64 62 65  .      rc = vdbe
270b9 53 6f 72 74 65 72 49 74 65 72 52 65 61 64 28 64  SorterIterRead(d
270ba 62 2c 20 70 2c 20 6e 43 6f 70 79 2c 20 26 61 4e  b, p, nCopy, &aN
270bb 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ext);.      if( 
270bc 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
270bd 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
270be 20 61 73 73 65 72 74 28 20 61 4e 65 78 74 21 3d   assert( aNext!=
270bf 70 2d 3e 61 41 6c 6c 6f 63 20 29 3b 0a 20 20 20  p->aAlloc );.   
270c0 20 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 61 41     memcpy(&p->aA
270c1 6c 6c 6f 63 5b 6e 42 79 74 65 20 2d 20 6e 52 65  lloc[nByte - nRe
270c2 6d 5d 2c 20 61 4e 65 78 74 2c 20 6e 43 6f 70 79  m], aNext, nCopy
270c3 29 3b 0a 20 20 20 20 20 20 6e 52 65 6d 20 2d 3d  );.      nRem -=
270c4 20 6e 43 6f 70 79 3b 0a 20 20 20 20 7d 0a 0a 20   nCopy;.    }.. 
270c5 20 20 20 2a 70 70 4f 75 74 20 3d 20 70 2d 3e 61     *ppOut = p->a
270c6 41 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a 20 20 72 65  Alloc;.  }..  re
270c7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
270c8 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20  }../*.** Read a 
270c9 76 61 72 69 6e 74 20 66 72 6f 6d 20 74 68 65 20  varint from the 
270ca 73 74 72 65 61 6d 20 6f 66 20 64 61 74 61 20 61  stream of data a
270cb 63 63 65 73 73 65 64 20 62 79 20 70 2e 20 53 65  ccessed by p. Se
270cc 74 20 2a 70 6e 4f 75 74 20 74 6f 0a 2a 2a 20 74  t *pnOut to.** t
270cd 68 65 20 76 61 6c 75 65 20 72 65 61 64 2e 0a 2a  he value read..*
270ce 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64 62  /.static int vdb
270cf 65 53 6f 72 74 65 72 49 74 65 72 56 61 72 69 6e  eSorterIterVarin
270d0 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56  t(sqlite3 *db, V
270d1 64 62 65 53 6f 72 74 65 72 49 74 65 72 20 2a 70  dbeSorterIter *p
270d2 2c 20 75 36 34 20 2a 70 6e 4f 75 74 29 7b 0a 20  , u64 *pnOut){. 
270d3 20 69 6e 74 20 69 42 75 66 3b 0a 0a 20 20 69 42   int iBuf;..  iB
270d4 75 66 20 3d 20 70 2d 3e 69 52 65 61 64 4f 66 66  uf = p->iReadOff
270d5 20 25 20 70 2d 3e 6e 42 75 66 66 65 72 3b 0a 20   % p->nBuffer;. 
270d6 20 69 66 28 20 69 42 75 66 20 26 26 20 28 70 2d   if( iBuf && (p-
270d7 3e 6e 42 75 66 66 65 72 2d 69 42 75 66 29 3e 3d  >nBuffer-iBuf)>=
270d8 39 20 29 7b 0a 20 20 20 20 70 2d 3e 69 52 65 61  9 ){.    p->iRea
270d9 64 4f 66 66 20 2b 3d 20 73 71 6c 69 74 65 33 47  dOff += sqlite3G
270da 65 74 56 61 72 69 6e 74 28 26 70 2d 3e 61 42 75  etVarint(&p->aBu
270db 66 66 65 72 5b 69 42 75 66 5d 2c 20 70 6e 4f 75  ffer[iBuf], pnOu
270dc 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
270dd 20 75 38 20 61 56 61 72 69 6e 74 5b 31 36 5d 2c   u8 aVarint[16],
270de 20 2a 61 3b 0a 20 20 20 20 69 6e 74 20 69 20 3d   *a;.    int i =
270df 20 30 2c 20 72 63 3b 0a 20 20 20 20 64 6f 7b 0a   0, rc;.    do{.
270e0 20 20 20 20 20 20 72 63 20 3d 20 76 64 62 65 53        rc = vdbeS
270e1 6f 72 74 65 72 49 74 65 72 52 65 61 64 28 64 62  orterIterRead(db
270e2 2c 20 70 2c 20 31 2c 20 26 61 29 3b 0a 20 20 20  , p, 1, &a);.   
270e3 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
270e4 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 61 56 61  rn rc;.      aVa
270e5 72 69 6e 74 5b 28 69 2b 2b 29 26 30 78 66 5d 20  rint[(i++)&0xf] 
270e6 3d 20 61 5b 30 5d 3b 0a 20 20 20 20 7d 77 68 69  = a[0];.    }whi
270e7 6c 65 28 20 28 61 5b 30 5d 26 30 78 38 30 29 21  le( (a[0]&0x80)!
270e8 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  =0 );.    sqlite
270e9 33 47 65 74 56 61 72 69 6e 74 28 61 56 61 72 69  3GetVarint(aVari
270ea 6e 74 2c 20 70 6e 4f 75 74 29 3b 0a 20 20 7d 0a  nt, pnOut);.  }.
270eb 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
270ec 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41  _OK;.}.../*.** A
270ed 64 76 61 6e 63 65 20 69 74 65 72 61 74 6f 72 20  dvance iterator 
270ee 70 49 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78  pIter to the nex
270ef 74 20 6b 65 79 20 69 6e 20 69 74 73 20 50 4d 41  t key in its PMA
270f0 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  . Return SQLITE_
270f1 4f 4b 20 69 66 0a 2a 2a 20 6e 6f 20 65 72 72 6f  OK if.** no erro
270f2 72 20 6f 63 63 75 72 73 2c 20 6f 72 20 61 6e 20  r occurs, or an 
270f3 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
270f4 65 20 69 66 20 6f 6e 65 20 64 6f 65 73 2e 0a 2a  e if one does..*
270f5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64 62  /.static int vdb
270f6 65 53 6f 72 74 65 72 49 74 65 72 4e 65 78 74 28  eSorterIterNext(
270f7 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
270f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
270f9 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68     /* Database h
270fa 61 6e 64 6c 65 20 28 66 6f 72 20 73 71 6c 69 74  andle (for sqlit
270fb 65 33 44 62 4d 61 6c 6c 6f 63 28 29 20 29 20 2a  e3DbMalloc() ) *
270fc 2f 0a 20 20 56 64 62 65 53 6f 72 74 65 72 49 74  /.  VdbeSorterIt
270fd 65 72 20 2a 70 49 74 65 72 20 20 20 20 20 20 20  er *pIter       
270fe 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
270ff 74 6f 20 61 64 76 61 6e 63 65 20 2a 2f 0a 29 7b  to advance */.){
27100 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
27101 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27102 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64     /* Return Cod
27103 65 20 2a 2f 0a 20 20 75 36 34 20 6e 52 65 63 20  e */.  u64 nRec 
27104 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
27105 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
27106 66 20 72 65 63 6f 72 64 20 69 6e 20 62 79 74 65  f record in byte
27107 73 20 2a 2f 0a 0a 20 20 69 66 28 20 70 49 74 65  s */..  if( pIte
27108 72 2d 3e 69 52 65 61 64 4f 66 66 3e 3d 70 49 74  r->iReadOff>=pIt
27109 65 72 2d 3e 69 45 6f 66 20 29 7b 0a 20 20 20 20  er->iEof ){.    
2710a 2f 2a 20 54 68 69 73 20 69 73 20 61 6e 20 45 4f  /* This is an EO
2710b 46 20 63 6f 6e 64 69 74 69 6f 6e 20 2a 2f 0a 20  F condition */. 
2710c 20 20 20 76 64 62 65 53 6f 72 74 65 72 49 74 65     vdbeSorterIte
2710d 72 5a 65 72 6f 28 64 62 2c 20 70 49 74 65 72 29  rZero(db, pIter)
2710e 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
2710f 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 72  ITE_OK;.  }..  r
27110 63 20 3d 20 76 64 62 65 53 6f 72 74 65 72 49 74  c = vdbeSorterIt
27111 65 72 56 61 72 69 6e 74 28 64 62 2c 20 70 49 74  erVarint(db, pIt
27112 65 72 2c 20 26 6e 52 65 63 29 3b 0a 20 20 69 66  er, &nRec);.  if
27113 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
27114 29 7b 0a 20 20 20 20 70 49 74 65 72 2d 3e 6e 4b  ){.    pIter->nK
27115 65 79 20 3d 20 28 69 6e 74 29 6e 52 65 63 3b 0a  ey = (int)nRec;.
27116 20 20 20 20 72 63 20 3d 20 76 64 62 65 53 6f 72      rc = vdbeSor
27117 74 65 72 49 74 65 72 52 65 61 64 28 64 62 2c 20  terIterRead(db, 
27118 70 49 74 65 72 2c 20 28 69 6e 74 29 6e 52 65 63  pIter, (int)nRec
27119 2c 20 26 70 49 74 65 72 2d 3e 61 4b 65 79 29 3b  , &pIter->aKey);
2711a 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
2711b 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  c;.}../*.** Init
2711c 69 61 6c 69 7a 65 20 69 74 65 72 61 74 6f 72 20  ialize iterator 
2711d 70 49 74 65 72 20 74 6f 20 73 63 61 6e 20 74 68  pIter to scan th
2711e 72 6f 75 67 68 20 74 68 65 20 50 4d 41 20 73 74  rough the PMA st
2711f 6f 72 65 64 20 69 6e 20 66 69 6c 65 20 70 46 69  ored in file pFi
27120 6c 65 0a 2a 2a 20 73 74 61 72 74 69 6e 67 20 61  le.** starting a
27121 74 20 6f 66 66 73 65 74 20 69 53 74 61 72 74 20  t offset iStart 
27122 61 6e 64 20 65 6e 64 69 6e 67 20 61 74 20 6f 66  and ending at of
27123 66 73 65 74 20 69 45 6f 66 2d 31 2e 20 54 68 69  fset iEof-1. Thi
27124 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 6c  s function .** l
27125 65 61 76 65 73 20 74 68 65 20 69 74 65 72 61 74  eaves the iterat
27126 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  or pointing to t
27127 68 65 20 66 69 72 73 74 20 6b 65 79 20 69 6e 20  he first key in 
27128 74 68 65 20 50 4d 41 20 28 6f 72 20 45 4f 46 20  the PMA (or EOF 
27129 69 66 20 74 68 65 20 0a 2a 2a 20 50 4d 41 20 69  if the .** PMA i
2712a 73 20 65 6d 70 74 79 29 2e 0a 2a 2f 0a 73 74 61  s empty)..*/.sta
2712b 74 69 63 20 69 6e 74 20 76 64 62 65 53 6f 72 74  tic int vdbeSort
2712c 65 72 49 74 65 72 49 6e 69 74 28 0a 20 20 73 71  erIterInit(.  sq
2712d 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
2712e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2712f 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
27130 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 56 64 62 65   */.  const Vdbe
27131 53 6f 72 74 65 72 20 2a 70 53 6f 72 74 65 72 2c  Sorter *pSorter,
27132 20 20 20 20 20 20 2f 2a 20 53 6f 72 74 65 72 20        /* Sorter 
27133 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 36 34 20  object */.  i64 
27134 69 53 74 61 72 74 2c 20 20 20 20 20 20 20 20 20  iStart,         
27135 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
27136 74 61 72 74 20 6f 66 66 73 65 74 20 69 6e 20 70  tart offset in p
27137 46 69 6c 65 20 2a 2f 0a 20 20 56 64 62 65 53 6f  File */.  VdbeSo
27138 72 74 65 72 49 74 65 72 20 2a 70 49 74 65 72 2c  rterIter *pIter,
27139 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
2713a 72 61 74 6f 72 20 74 6f 20 70 6f 70 75 6c 61 74  rator to populat
2713b 65 20 2a 2f 0a 20 20 69 36 34 20 2a 70 6e 42 79  e */.  i64 *pnBy
2713c 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  te              
2713d 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54         /* IN/OUT
2713e 3a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 69 73  : Increment this
2713f 20 76 61 6c 75 65 20 62 79 20 50 4d 41 20 73 69   value by PMA si
27140 7a 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  ze */.){.  int r
27141 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
27142 20 69 6e 74 20 6e 42 75 66 3b 0a 0a 20 20 6e 42   int nBuf;..  nB
27143 75 66 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  uf = sqlite3Btre
27144 65 47 65 74 50 61 67 65 53 69 7a 65 28 64 62 2d  eGetPageSize(db-
27145 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b 0a 0a 20  >aDb[0].pBt);.. 
27146 20 61 73 73 65 72 74 28 20 70 53 6f 72 74 65 72   assert( pSorter
27147 2d 3e 69 57 72 69 74 65 4f 66 66 3e 69 53 74 61  ->iWriteOff>iSta
27148 72 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  rt );.  assert( 
27149 70 49 74 65 72 2d 3e 61 41 6c 6c 6f 63 3d 3d 30  pIter->aAlloc==0
2714a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49   );.  assert( pI
2714b 74 65 72 2d 3e 61 42 75 66 66 65 72 3d 3d 30 20  ter->aBuffer==0 
2714c 29 3b 0a 20 20 70 49 74 65 72 2d 3e 70 46 69 6c  );.  pIter->pFil
2714d 65 20 3d 20 70 53 6f 72 74 65 72 2d 3e 70 54 65  e = pSorter->pTe
2714e 6d 70 31 3b 0a 20 20 70 49 74 65 72 2d 3e 69 52  mp1;.  pIter->iR
2714f 65 61 64 4f 66 66 20 3d 20 69 53 74 61 72 74 3b  eadOff = iStart;
27150 0a 20 20 70 49 74 65 72 2d 3e 6e 41 6c 6c 6f 63  .  pIter->nAlloc
27151 20 3d 20 31 32 38 3b 0a 20 20 70 49 74 65 72 2d   = 128;.  pIter-
27152 3e 61 41 6c 6c 6f 63 20 3d 20 28 75 38 20 2a 29  >aAlloc = (u8 *)
27153 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
27154 61 77 28 64 62 2c 20 70 49 74 65 72 2d 3e 6e 41  aw(db, pIter->nA
27155 6c 6c 6f 63 29 3b 0a 20 20 70 49 74 65 72 2d 3e  lloc);.  pIter->
27156 6e 42 75 66 66 65 72 20 3d 20 6e 42 75 66 3b 0a  nBuffer = nBuf;.
27157 20 20 70 49 74 65 72 2d 3e 61 42 75 66 66 65 72    pIter->aBuffer
27158 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33   = (u8 *)sqlite3
27159 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
2715a 6e 42 75 66 29 3b 0a 0a 20 20 69 66 28 20 21 70  nBuf);..  if( !p
2715b 49 74 65 72 2d 3e 61 42 75 66 66 65 72 20 29 7b  Iter->aBuffer ){
2715c 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
2715d 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b  _NOMEM;.  }else{
2715e 0a 20 20 20 20 69 6e 74 20 69 42 75 66 3b 0a 0a  .    int iBuf;..
2715f 20 20 20 20 69 42 75 66 20 3d 20 69 53 74 61 72      iBuf = iStar
27160 74 20 25 20 6e 42 75 66 3b 0a 20 20 20 20 69 66  t % nBuf;.    if
27161 28 20 69 42 75 66 20 29 7b 0a 20 20 20 20 20 20  ( iBuf ){.      
27162 69 6e 74 20 6e 52 65 61 64 20 3d 20 6e 42 75 66  int nRead = nBuf
27163 20 2d 20 69 42 75 66 3b 0a 20 20 20 20 20 20 69   - iBuf;.      i
27164 66 28 20 28 69 53 74 61 72 74 20 2b 20 6e 52 65  f( (iStart + nRe
27165 61 64 29 20 3e 20 70 53 6f 72 74 65 72 2d 3e 69  ad) > pSorter->i
27166 57 72 69 74 65 4f 66 66 20 29 7b 0a 20 20 20 20  WriteOff ){.    
27167 20 20 20 20 6e 52 65 61 64 20 3d 20 28 69 6e 74      nRead = (int
27168 29 28 70 53 6f 72 74 65 72 2d 3e 69 57 72 69 74  )(pSorter->iWrit
27169 65 4f 66 66 20 2d 20 69 53 74 61 72 74 29 3b 0a  eOff - iStart);.
2716a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
2716b 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
2716c 28 0a 20 20 20 20 20 20 20 20 20 20 70 53 6f 72  (.          pSor
2716d 74 65 72 2d 3e 70 54 65 6d 70 31 2c 20 26 70 49  ter->pTemp1, &pI
2716e 74 65 72 2d 3e 61 42 75 66 66 65 72 5b 69 42 75  ter->aBuffer[iBu
2716f 66 5d 2c 20 6e 52 65 61 64 2c 20 69 53 74 61 72  f], nRead, iStar
27170 74 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20  t.      );.     
27171 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
27172 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f  ITE_IOERR_SHORT_
27173 52 45 41 44 20 29 3b 0a 20 20 20 20 7d 0a 0a 20  READ );.    }.. 
27174 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
27175 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 36  E_OK ){.      u6
27176 34 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20  4 nByte;        
27177 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27178 2a 20 53 69 7a 65 20 6f 66 20 50 4d 41 20 69 6e  * Size of PMA in
27179 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 20 20   bytes */.      
2717a 70 49 74 65 72 2d 3e 69 45 6f 66 20 3d 20 70 53  pIter->iEof = pS
2717b 6f 72 74 65 72 2d 3e 69 57 72 69 74 65 4f 66 66  orter->iWriteOff
2717c 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 76 64 62  ;.      rc = vdb
2717d 65 53 6f 72 74 65 72 49 74 65 72 56 61 72 69 6e  eSorterIterVarin
2717e 74 28 64 62 2c 20 70 49 74 65 72 2c 20 26 6e 42  t(db, pIter, &nB
2717f 79 74 65 29 3b 0a 20 20 20 20 20 20 70 49 74 65  yte);.      pIte
27180 72 2d 3e 69 45 6f 66 20 3d 20 70 49 74 65 72 2d  r->iEof = pIter-
27181 3e 69 52 65 61 64 4f 66 66 20 2b 20 6e 42 79 74  >iReadOff + nByt
27182 65 3b 0a 20 20 20 20 20 20 2a 70 6e 42 79 74 65  e;.      *pnByte
27183 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d   += nByte;.    }
27184 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
27185 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
27186 20 72 63 20 3d 20 76 64 62 65 53 6f 72 74 65 72   rc = vdbeSorter
27187 49 74 65 72 4e 65 78 74 28 64 62 2c 20 70 49 74  IterNext(db, pIt
27188 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  er);.  }.  retur
27189 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
2718a 43 6f 6d 70 61 72 65 20 6b 65 79 31 20 28 62 75  Compare key1 (bu
2718b 66 66 65 72 20 70 4b 65 79 31 2c 20 73 69 7a 65  ffer pKey1, size
2718c 20 6e 4b 65 79 31 20 62 79 74 65 73 29 20 77 69   nKey1 bytes) wi
2718d 74 68 20 6b 65 79 32 20 28 62 75 66 66 65 72 20  th key2 (buffer 
2718e 70 4b 65 79 32 2c 20 0a 2a 2a 20 73 69 7a 65 20  pKey2, .** size 
2718f 6e 4b 65 79 32 20 62 79 74 65 73 29 2e 20 20 41  nKey2 bytes).  A
27190 72 67 75 6d 65 6e 74 20 70 4b 65 79 49 6e 66 6f  rgument pKeyInfo
27191 20 73 75 70 70 6c 69 65 73 20 74 68 65 20 63 6f   supplies the co
27192 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  llation function
27193 73 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65  s.** used by the
27194 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 49 66 20   comparison. If 
27195 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
27196 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74   return an SQLit
27197 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a  e error code..**
27198 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75   Otherwise, retu
27199 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64  rn SQLITE_OK and
2719a 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 61 20   set *pRes to a 
2719b 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f  negative, zero o
2719c 72 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20 76 61  r positive.** va
2719d 6c 75 65 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f  lue, depending o
2719e 6e 20 77 68 65 74 68 65 72 20 6b 65 79 31 20 69  n whether key1 i
2719f 73 20 73 6d 61 6c 6c 65 72 2c 20 65 71 75 61 6c  s smaller, equal
271a0 20 74 6f 20 6f 72 20 6c 61 72 67 65 72 20 74 68   to or larger th
271a1 61 6e 20 6b 65 79 32 2e 0a 2a 2a 0a 2a 2a 20 49  an key2..**.** I
271a2 66 20 74 68 65 20 62 4f 6d 69 74 52 6f 77 69 64  f the bOmitRowid
271a3 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e   argument is non
271a4 2d 7a 65 72 6f 2c 20 61 73 73 75 6d 65 20 62 6f  -zero, assume bo
271a5 74 68 20 6b 65 79 73 20 65 6e 64 20 69 6e 20 61  th keys end in a
271a6 20 72 6f 77 69 64 0a 2a 2a 20 66 69 65 6c 64 2e   rowid.** field.
271a7 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65   For the purpose
271a8 73 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69  s of the compari
271a9 73 6f 6e 2c 20 69 67 6e 6f 72 65 20 69 74 2e 20  son, ignore it. 
271aa 41 6c 73 6f 2c 20 69 66 20 62 4f 6d 69 74 52 6f  Also, if bOmitRo
271ab 77 69 64 0a 2a 2a 20 69 73 20 74 72 75 65 20 61  wid.** is true a
271ac 6e 64 20 6b 65 79 31 20 63 6f 6e 74 61 69 6e 73  nd key1 contains
271ad 20 65 76 65 6e 20 61 20 73 69 6e 67 6c 65 20 4e   even a single N
271ae 55 4c 4c 20 76 61 6c 75 65 2c 20 69 74 20 69 73  ULL value, it is
271af 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 0a 2a   considered to.*
271b0 2a 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 6b  * be less than k
271b1 65 79 32 2e 20 45 76 65 6e 20 69 66 20 6b 65 79  ey2. Even if key
271b2 32 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20  2 also contains 
271b3 4e 55 4c 4c 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a  NULL values..**.
271b4 2a 2a 20 49 66 20 70 4b 65 79 32 20 69 73 20 70  ** If pKey2 is p
271b5 61 73 73 65 64 20 61 20 4e 55 4c 4c 20 70 6f 69  assed a NULL poi
271b6 6e 74 65 72 2c 20 74 68 65 6e 20 69 74 20 69 73  nter, then it is
271b7 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68   assumed that th
271b8 65 20 70 43 73 72 2d 3e 61 53 70 61 63 65 0a 2a  e pCsr->aSpace.*
271b9 2a 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63  * has been alloc
271ba 61 74 65 64 20 61 6e 64 20 63 6f 6e 74 61 69 6e  ated and contain
271bb 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 72 65  s an unpacked re
271bc 63 6f 72 64 20 74 68 61 74 20 69 73 20 75 73 65  cord that is use
271bd 64 20 61 73 20 6b 65 79 32 2e 0a 2a 2f 0a 73 74  d as key2..*/.st
271be 61 74 69 63 20 76 6f 69 64 20 76 64 62 65 53 6f  atic void vdbeSo
271bf 72 74 65 72 43 6f 6d 70 61 72 65 28 0a 20 20 63  rterCompare(.  c
271c0 6f 6e 73 74 20 56 64 62 65 43 75 72 73 6f 72 20  onst VdbeCursor 
271c1 2a 70 43 73 72 2c 20 20 20 20 20 20 20 20 20 2f  *pCsr,         /
271c2 2a 20 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 20  * Cursor object 
271c3 28 66 6f 72 20 70 4b 65 79 49 6e 66 6f 29 20 2a  (for pKeyInfo) *
271c4 2f 0a 20 20 69 6e 74 20 62 4f 6d 69 74 52 6f 77  /.  int bOmitRow
271c5 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  id,             
271c6 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 72 6f      /* Ignore ro
271c7 77 69 64 20 66 69 65 6c 64 20 61 74 20 65 6e 64  wid field at end
271c8 20 6f 66 20 6b 65 79 73 20 2a 2f 0a 20 20 63 6f   of keys */.  co
271c9 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c  nst void *pKey1,
271ca 20 69 6e 74 20 6e 4b 65 79 31 2c 20 20 20 2f 2a   int nKey1,   /*
271cb 20 4c 65 66 74 20 73 69 64 65 20 6f 66 20 63 6f   Left side of co
271cc 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a 20 20 63 6f  mparison */.  co
271cd 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c  nst void *pKey2,
271ce 20 69 6e 74 20 6e 4b 65 79 32 2c 20 20 20 2f 2a   int nKey2,   /*
271cf 20 52 69 67 68 74 20 73 69 64 65 20 6f 66 20 63   Right side of c
271d0 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a 20 20 69  omparison */.  i
271d1 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 20  nt *pRes        
271d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
271d3 2a 20 4f 55 54 3a 20 52 65 73 75 6c 74 20 6f 66  * OUT: Result of
271d4 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a 29   comparison */.)
271d5 7b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  {.  KeyInfo *pKe
271d6 79 49 6e 66 6f 20 3d 20 70 43 73 72 2d 3e 70 4b  yInfo = pCsr->pK
271d7 65 79 49 6e 66 6f 3b 0a 20 20 56 64 62 65 53 6f  eyInfo;.  VdbeSo
271d8 72 74 65 72 20 2a 70 53 6f 72 74 65 72 20 3d 20  rter *pSorter = 
271d9 70 43 73 72 2d 3e 70 53 6f 72 74 65 72 3b 0a 20  pCsr->pSorter;. 
271da 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
271db 2a 72 32 20 3d 20 70 53 6f 72 74 65 72 2d 3e 70  *r2 = pSorter->p
271dc 55 6e 70 61 63 6b 65 64 3b 0a 20 20 69 6e 74 20  Unpacked;.  int 
271dd 69 3b 0a 0a 20 20 69 66 28 20 70 4b 65 79 32 20  i;..  if( pKey2 
271de 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
271df 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 70  beRecordUnpack(p
271e0 4b 65 79 49 6e 66 6f 2c 20 6e 4b 65 79 32 2c 20  KeyInfo, nKey2, 
271e1 70 4b 65 79 32 2c 20 72 32 29 3b 0a 20 20 7d 0a  pKey2, r2);.  }.
271e2 0a 20 20 69 66 28 20 62 4f 6d 69 74 52 6f 77 69  .  if( bOmitRowi
271e3 64 20 29 7b 0a 20 20 20 20 72 32 2d 3e 6e 46 69  d ){.    r2->nFi
271e4 65 6c 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e  eld = pKeyInfo->
271e5 6e 46 69 65 6c 64 3b 0a 20 20 20 20 61 73 73 65  nField;.    asse
271e6 72 74 28 20 72 32 2d 3e 6e 46 69 65 6c 64 3e 30  rt( r2->nField>0
271e7 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   );.    for(i=0;
271e8 20 69 3c 72 32 2d 3e 6e 46 69 65 6c 64 3b 20 69   i<r2->nField; i
271e9 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72  ++){.      if( r
271ea 32 2d 3e 61 4d 65 6d 5b 69 5d 2e 66 6c 61 67 73  2->aMem[i].flags
271eb 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20   & MEM_Null ){. 
271ec 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 2d         *pRes = -
271ed 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  1;.        retur
271ee 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  n;.      }.    }
271ef 0a 20 20 20 20 72 32 2d 3e 66 6c 61 67 73 20 7c  .    r2->flags |
271f0 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49  = UNPACKED_PREFI
271f1 58 5f 4d 41 54 43 48 3b 0a 20 20 7d 0a 0a 20 20  X_MATCH;.  }..  
271f2 2a 70 52 65 73 20 3d 20 73 71 6c 69 74 65 33 56  *pRes = sqlite3V
271f3 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65  dbeRecordCompare
271f4 28 6e 4b 65 79 31 2c 20 70 4b 65 79 31 2c 20 72  (nKey1, pKey1, r
271f5 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  2);.}../*.** Thi
271f6 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
271f7 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20  lled to compare 
271f8 74 77 6f 20 69 74 65 72 61 74 6f 72 20 6b 65 79  two iterator key
271f9 73 20 77 68 65 6e 20 6d 65 72 67 69 6e 67 20 0a  s when merging .
271fa 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 62 2d 74 72  ** multiple b-tr
271fb 65 65 20 73 65 67 6d 65 6e 74 73 2e 20 50 61 72  ee segments. Par
271fc 61 6d 65 74 65 72 20 69 4f 75 74 20 69 73 20 74  ameter iOut is t
271fd 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
271fe 61 54 72 65 65 5b 5d 20 0a 2a 2a 20 76 61 6c 75  aTree[] .** valu
271ff 65 20 74 6f 20 72 65 63 61 6c 63 75 6c 61 74 65  e to recalculate
27200 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
27201 76 64 62 65 53 6f 72 74 65 72 44 6f 43 6f 6d 70  vdbeSorterDoComp
27202 61 72 65 28 63 6f 6e 73 74 20 56 64 62 65 43 75  are(const VdbeCu
27203 72 73 6f 72 20 2a 70 43 73 72 2c 20 69 6e 74 20  rsor *pCsr, int 
27204 69 4f 75 74 29 7b 0a 20 20 56 64 62 65 53 6f 72  iOut){.  VdbeSor
27205 74 65 72 20 2a 70 53 6f 72 74 65 72 20 3d 20 70  ter *pSorter = p
27206 43 73 72 2d 3e 70 53 6f 72 74 65 72 3b 0a 20 20  Csr->pSorter;.  
27207 69 6e 74 20 69 31 3b 0a 20 20 69 6e 74 20 69 32  int i1;.  int i2
27208 3b 0a 20 20 69 6e 74 20 69 52 65 73 3b 0a 20 20  ;.  int iRes;.  
27209 56 64 62 65 53 6f 72 74 65 72 49 74 65 72 20 2a  VdbeSorterIter *
2720a 70 31 3b 0a 20 20 56 64 62 65 53 6f 72 74 65 72  p1;.  VdbeSorter
2720b 49 74 65 72 20 2a 70 32 3b 0a 0a 20 20 61 73 73  Iter *p2;..  ass
2720c 65 72 74 28 20 69 4f 75 74 3c 70 53 6f 72 74 65  ert( iOut<pSorte
2720d 72 2d 3e 6e 54 72 65 65 20 26 26 20 69 4f 75 74  r->nTree && iOut
2720e 3e 30 20 29 3b 0a 0a 20 20 69 66 28 20 69 4f 75  >0 );..  if( iOu
2720f 74 3e 3d 28 70 53 6f 72 74 65 72 2d 3e 6e 54 72  t>=(pSorter->nTr
27210 65 65 2f 32 29 20 29 7b 0a 20 20 20 20 69 31 20  ee/2) ){.    i1 
27211 3d 20 28 69 4f 75 74 20 2d 20 70 53 6f 72 74 65  = (iOut - pSorte
27212 72 2d 3e 6e 54 72 65 65 2f 32 29 20 2a 20 32 3b  r->nTree/2) * 2;
27213 0a 20 20 20 20 69 32 20 3d 20 69 31 20 2b 20 31  .    i2 = i1 + 1
27214 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
27215 31 20 3d 20 70 53 6f 72 74 65 72 2d 3e 61 54 72  1 = pSorter->aTr
27216 65 65 5b 69 4f 75 74 2a 32 5d 3b 0a 20 20 20 20  ee[iOut*2];.    
27217 69 32 20 3d 20 70 53 6f 72 74 65 72 2d 3e 61 54  i2 = pSorter->aT
27218 72 65 65 5b 69 4f 75 74 2a 32 2b 31 5d 3b 0a 20  ree[iOut*2+1];. 
27219 20 7d 0a 0a 20 20 70 31 20 3d 20 26 70 53 6f 72   }..  p1 = &pSor
2721a 74 65 72 2d 3e 61 49 74 65 72 5b 69 31 5d 3b 0a  ter->aIter[i1];.
2721b 20 20 70 32 20 3d 20 26 70 53 6f 72 74 65 72 2d    p2 = &pSorter-
2721c 3e 61 49 74 65 72 5b 69 32 5d 3b 0a 0a 20 20 69  >aIter[i2];..  i
2721d 66 28 20 70 31 2d 3e 70 46 69 6c 65 3d 3d 30 20  f( p1->pFile==0 
2721e 29 7b 0a 20 20 20 20 69 52 65 73 20 3d 20 69 32  ){.    iRes = i2
2721f 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 32  ;.  }else if( p2
27220 2d 3e 70 46 69 6c 65 3d 3d 30 20 29 7b 0a 20 20  ->pFile==0 ){.  
27221 20 20 69 52 65 73 20 3d 20 69 31 3b 0a 20 20 7d    iRes = i1;.  }
27222 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 65  else{.    int re
27223 73 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  s;.    assert( p
27224 43 73 72 2d 3e 70 53 6f 72 74 65 72 2d 3e 70 55  Csr->pSorter->pU
27225 6e 70 61 63 6b 65 64 21 3d 30 20 29 3b 20 20 2f  npacked!=0 );  /
27226 2a 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 76  * allocated in v
27227 64 62 65 53 6f 72 74 65 72 4d 65 72 67 65 28 29  dbeSorterMerge()
27228 20 2a 2f 0a 20 20 20 20 76 64 62 65 53 6f 72 74   */.    vdbeSort
27229 65 72 43 6f 6d 70 61 72 65 28 0a 20 20 20 20 20  erCompare(.     
2722a 20 20 20 70 43 73 72 2c 20 30 2c 20 70 31 2d 3e     pCsr, 0, p1->
2722b 61 4b 65 79 2c 20 70 31 2d 3e 6e 4b 65 79 2c 20  aKey, p1->nKey, 
2722c 70 32 2d 3e 61 4b 65 79 2c 20 70 32 2d 3e 6e 4b  p2->aKey, p2->nK
2722d 65 79 2c 20 26 72 65 73 0a 20 20 20 20 29 3b 0a  ey, &res.    );.
2722e 20 20 20 20 69 66 28 20 72 65 73 3c 3d 30 20 29      if( res<=0 )
2722f 7b 0a 20 20 20 20 20 20 69 52 65 73 20 3d 20 69  {.      iRes = i
27230 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
27231 20 20 20 20 69 52 65 73 20 3d 20 69 32 3b 0a 20      iRes = i2;. 
27232 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 53 6f 72     }.  }..  pSor
27233 74 65 72 2d 3e 61 54 72 65 65 5b 69 4f 75 74 5d  ter->aTree[iOut]
27234 20 3d 20 69 52 65 73 3b 0a 20 20 72 65 74 75 72   = iRes;.  retur
27235 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
27236 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
27237 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 69   the temporary i
27238 6e 64 65 78 20 63 75 72 73 6f 72 20 6a 75 73 74  ndex cursor just
27239 20 6f 70 65 6e 65 64 20 61 73 20 61 20 73 6f 72   opened as a sor
2723a 74 65 72 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 53  ter cursor..*/.S
2723b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2723c 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f 72  t sqlite3VdbeSor
2723d 74 65 72 49 6e 69 74 28 73 71 6c 69 74 65 33 20  terInit(sqlite3 
2723e 2a 64 62 2c 20 56 64 62 65 43 75 72 73 6f 72 20  *db, VdbeCursor 
2723f 2a 70 43 73 72 29 7b 0a 20 20 69 6e 74 20 70 67  *pCsr){.  int pg
27240 73 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sz;             
27241 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
27242 65 20 73 69 7a 65 20 6f 66 20 6d 61 69 6e 20 64  e size of main d
27243 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74  atabase */.  int
27244 20 6d 78 43 61 63 68 65 3b 20 20 20 20 20 20 20   mxCache;       
27245 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27246 43 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20  Cache size */.  
27247 56 64 62 65 53 6f 72 74 65 72 20 2a 70 53 6f 72  VdbeSorter *pSor
27248 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  ter;            
27249 2f 2a 20 54 68 65 20 6e 65 77 20 73 6f 72 74 65  /* The new sorte
2724a 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 64 3b 20  r */.  char *d; 
2724b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2724c 20 20 20 20 20 20 20 2f 2a 20 44 75 6d 6d 79 20         /* Dummy 
2724d 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43  */..  assert( pC
2724e 73 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 26 26 20  sr->pKeyInfo && 
2724f 70 43 73 72 2d 3e 70 42 74 3d 3d 30 20 29 3b 0a  pCsr->pBt==0 );.
27250 20 20 70 43 73 72 2d 3e 70 53 6f 72 74 65 72 20    pCsr->pSorter 
27251 3d 20 70 53 6f 72 74 65 72 20 3d 20 73 71 6c 69  = pSorter = sqli
27252 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
27253 64 62 2c 20 73 69 7a 65 6f 66 28 56 64 62 65 53  db, sizeof(VdbeS
27254 6f 72 74 65 72 29 29 3b 0a 20 20 69 66 28 20 70  orter));.  if( p
27255 53 6f 72 74 65 72 3d 3d 30 20 29 7b 0a 20 20 20  Sorter==0 ){.   
27256 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
27257 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 0a 20 20 70  OMEM;.  }.  .  p
27258 53 6f 72 74 65 72 2d 3e 70 55 6e 70 61 63 6b 65  Sorter->pUnpacke
27259 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  d = sqlite3VdbeA
2725a 6c 6c 6f 63 55 6e 70 61 63 6b 65 64 52 65 63 6f  llocUnpackedReco
2725b 72 64 28 70 43 73 72 2d 3e 70 4b 65 79 49 6e 66  rd(pCsr->pKeyInf
2725c 6f 2c 20 30 2c 20 30 2c 20 26 64 29 3b 0a 20 20  o, 0, 0, &d);.  
2725d 69 66 28 20 70 53 6f 72 74 65 72 2d 3e 70 55 6e  if( pSorter->pUn
2725e 70 61 63 6b 65 64 3d 3d 30 20 29 20 72 65 74 75  packed==0 ) retu
2725f 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
27260 0a 20 20 61 73 73 65 72 74 28 20 70 53 6f 72 74  .  assert( pSort
27261 65 72 2d 3e 70 55 6e 70 61 63 6b 65 64 3d 3d 28  er->pUnpacked==(
27262 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
27263 29 64 20 29 3b 0a 0a 20 20 69 66 28 20 21 73 71  )d );..  if( !sq
27264 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72  lite3TempInMemor
27265 79 28 64 62 29 20 29 7b 0a 20 20 20 20 70 67 73  y(db) ){.    pgs
27266 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  z = sqlite3Btree
27267 47 65 74 50 61 67 65 53 69 7a 65 28 64 62 2d 3e  GetPageSize(db->
27268 61 44 62 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 20  aDb[0].pBt);.   
27269 20 70 53 6f 72 74 65 72 2d 3e 6d 6e 50 6d 61 53   pSorter->mnPmaS
2726a 69 7a 65 20 3d 20 53 4f 52 54 45 52 5f 4d 49 4e  ize = SORTER_MIN
2726b 5f 57 4f 52 4b 49 4e 47 20 2a 20 70 67 73 7a 3b  _WORKING * pgsz;
2726c 0a 20 20 20 20 6d 78 43 61 63 68 65 20 3d 20 64  .    mxCache = d
2726d 62 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 68 65 6d  b->aDb[0].pSchem
2726e 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 3b 0a 20  a->cache_size;. 
2726f 20 20 20 69 66 28 20 6d 78 43 61 63 68 65 3c 53     if( mxCache<S
27270 4f 52 54 45 52 5f 4d 49 4e 5f 57 4f 52 4b 49 4e  ORTER_MIN_WORKIN
27271 47 20 29 20 6d 78 43 61 63 68 65 20 3d 20 53 4f  G ) mxCache = SO
27272 52 54 45 52 5f 4d 49 4e 5f 57 4f 52 4b 49 4e 47  RTER_MIN_WORKING
27273 3b 0a 20 20 20 20 70 53 6f 72 74 65 72 2d 3e 6d  ;.    pSorter->m
27274 78 50 6d 61 53 69 7a 65 20 3d 20 6d 78 43 61 63  xPmaSize = mxCac
27275 68 65 20 2a 20 70 67 73 7a 3b 0a 20 20 7d 0a 0a  he * pgsz;.  }..
27276 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
27277 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  OK;.}../*.** Fre
27278 65 20 74 68 65 20 6c 69 73 74 20 6f 66 20 73 6f  e the list of so
27279 72 74 65 64 20 72 65 63 6f 72 64 73 20 73 74 61  rted records sta
2727a 72 74 69 6e 67 20 61 74 20 70 52 65 63 6f 72 64  rting at pRecord
2727b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2727c 20 76 64 62 65 53 6f 72 74 65 72 52 65 63 6f 72   vdbeSorterRecor
2727d 64 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64  dFree(sqlite3 *d
2727e 62 2c 20 53 6f 72 74 65 72 52 65 63 6f 72 64 20  b, SorterRecord 
2727f 2a 70 52 65 63 6f 72 64 29 7b 0a 20 20 53 6f 72  *pRecord){.  Sor
27280 74 65 72 52 65 63 6f 72 64 20 2a 70 3b 0a 20 20  terRecord *p;.  
27281 53 6f 72 74 65 72 52 65 63 6f 72 64 20 2a 70 4e  SorterRecord *pN
27282 65 78 74 3b 0a 20 20 66 6f 72 28 70 3d 70 52 65  ext;.  for(p=pRe
27283 63 6f 72 64 3b 20 70 3b 20 70 3d 70 4e 65 78 74  cord; p; p=pNext
27284 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70  ){.    pNext = p
27285 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c  ->pNext;.    sql
27286 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
27287 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
27288 46 72 65 65 20 61 6e 79 20 63 75 72 73 6f 72 20  Free any cursor 
27289 63 6f 6d 70 6f 6e 65 6e 74 73 20 61 6c 6c 6f 63  components alloc
2728a 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 56  ated by sqlite3V
2728b 64 62 65 53 6f 72 74 65 72 58 58 58 20 72 6f 75  dbeSorterXXX rou
2728c 74 69 6e 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tines..*/.SQLITE
2728d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2728e 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 43  lite3VdbeSorterC
2728f 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a 64 62  lose(sqlite3 *db
27290 2c 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43  , VdbeCursor *pC
27291 73 72 29 7b 0a 20 20 56 64 62 65 53 6f 72 74 65  sr){.  VdbeSorte
27292 72 20 2a 70 53 6f 72 74 65 72 20 3d 20 70 43 73  r *pSorter = pCs
27293 72 2d 3e 70 53 6f 72 74 65 72 3b 0a 20 20 69 66  r->pSorter;.  if
27294 28 20 70 53 6f 72 74 65 72 20 29 7b 0a 20 20 20  ( pSorter ){.   
27295 20 69 66 28 20 70 53 6f 72 74 65 72 2d 3e 61 49   if( pSorter->aI
27296 74 65 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ter ){.      int
27297 20 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d   i;.      for(i=
27298 30 3b 20 69 3c 70 53 6f 72 74 65 72 2d 3e 6e 54  0; i<pSorter->nT
27299 72 65 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ree; i++){.     
2729a 20 20 20 76 64 62 65 53 6f 72 74 65 72 49 74 65     vdbeSorterIte
2729b 72 5a 65 72 6f 28 64 62 2c 20 26 70 53 6f 72 74  rZero(db, &pSort
2729c 65 72 2d 3e 61 49 74 65 72 5b 69 5d 29 3b 0a 20  er->aIter[i]);. 
2729d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
2729e 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2729f 53 6f 72 74 65 72 2d 3e 61 49 74 65 72 29 3b 0a  Sorter->aIter);.
272a0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53      }.    if( pS
272a1 6f 72 74 65 72 2d 3e 70 54 65 6d 70 31 20 29 7b  orter->pTemp1 ){
272a2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
272a3 43 6c 6f 73 65 46 72 65 65 28 70 53 6f 72 74 65  CloseFree(pSorte
272a4 72 2d 3e 70 54 65 6d 70 31 29 3b 0a 20 20 20 20  r->pTemp1);.    
272a5 7d 0a 20 20 20 20 76 64 62 65 53 6f 72 74 65 72  }.    vdbeSorter
272a6 52 65 63 6f 72 64 46 72 65 65 28 64 62 2c 20 70  RecordFree(db, p
272a7 53 6f 72 74 65 72 2d 3e 70 52 65 63 6f 72 64 29  Sorter->pRecord)
272a8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
272a9 72 65 65 28 64 62 2c 20 70 53 6f 72 74 65 72 2d  ree(db, pSorter-
272aa 3e 70 55 6e 70 61 63 6b 65 64 29 3b 0a 20 20 20  >pUnpacked);.   
272ab 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
272ac 62 2c 20 70 53 6f 72 74 65 72 29 3b 0a 20 20 20  b, pSorter);.   
272ad 20 70 43 73 72 2d 3e 70 53 6f 72 74 65 72 20 3d   pCsr->pSorter =
272ae 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
272af 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20   Allocate space 
272b0 66 6f 72 20 61 20 66 69 6c 65 2d 68 61 6e 64 6c  for a file-handl
272b1 65 20 61 6e 64 20 6f 70 65 6e 20 61 20 74 65 6d  e and open a tem
272b2 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 49 66 20  porary file. If 
272b3 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 73  successful,.** s
272b4 65 74 20 2a 70 70 46 69 6c 65 20 74 6f 20 70 6f  et *ppFile to po
272b5 69 6e 74 20 74 6f 20 74 68 65 20 6d 61 6c 6c 6f  int to the mallo
272b6 63 27 64 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20  c'd file-handle 
272b7 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  and return SQLIT
272b8 45 5f 4f 4b 2e 0a 2a 2a 20 4f 74 68 65 72 77 69  E_OK..** Otherwi
272b9 73 65 2c 20 73 65 74 20 2a 70 70 46 69 6c 65 20  se, set *ppFile 
272ba 74 6f 20 30 20 61 6e 64 20 72 65 74 75 72 6e 20  to 0 and return 
272bb 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
272bc 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  code..*/.static 
272bd 69 6e 74 20 76 64 62 65 53 6f 72 74 65 72 4f 70  int vdbeSorterOp
272be 65 6e 54 65 6d 70 46 69 6c 65 28 73 71 6c 69 74  enTempFile(sqlit
272bf 65 33 20 2a 64 62 2c 20 73 71 6c 69 74 65 33 5f  e3 *db, sqlite3_
272c0 66 69 6c 65 20 2a 2a 70 70 46 69 6c 65 29 7b 0a  file **ppFile){.
272c1 20 20 69 6e 74 20 64 75 6d 6d 79 3b 0a 20 20 72    int dummy;.  r
272c2 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 4f  eturn sqlite3OsO
272c3 70 65 6e 4d 61 6c 6c 6f 63 28 64 62 2d 3e 70 56  penMalloc(db->pV
272c4 66 73 2c 20 30 2c 20 70 70 46 69 6c 65 2c 0a 20  fs, 0, ppFile,. 
272c5 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
272c6 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 7c 0a  _TEMP_JOURNAL |.
272c7 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
272c8 4e 5f 52 45 41 44 57 52 49 54 45 20 20 20 20 7c  N_READWRITE    |
272c9 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
272ca 41 54 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49  ATE |.      SQLI
272cb 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
272cc 45 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 4f 50  E    | SQLITE_OP
272cd 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
272ce 2c 20 26 64 75 6d 6d 79 0a 20 20 29 3b 0a 7d 0a  , &dummy.  );.}.
272cf 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 68 65  ./*.** Merge the
272d0 20 74 77 6f 20 73 6f 72 74 65 64 20 6c 69 73 74   two sorted list
272d1 73 20 70 31 20 61 6e 64 20 70 32 20 69 6e 74 6f  s p1 and p2 into
272d2 20 61 20 73 69 6e 67 6c 65 20 6c 69 73 74 2e 0a   a single list..
272d3 2a 2a 20 53 65 74 20 2a 70 70 4f 75 74 20 74 6f  ** Set *ppOut to
272d4 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 65   the head of the
272d5 20 6e 65 77 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74   new list..*/.st
272d6 61 74 69 63 20 76 6f 69 64 20 76 64 62 65 53 6f  atic void vdbeSo
272d7 72 74 65 72 4d 65 72 67 65 28 0a 20 20 63 6f 6e  rterMerge(.  con
272d8 73 74 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  st VdbeCursor *p
272d9 43 73 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Csr,         /* 
272da 46 6f 72 20 70 4b 65 79 49 6e 66 6f 20 2a 2f 0a  For pKeyInfo */.
272db 20 20 53 6f 72 74 65 72 52 65 63 6f 72 64 20 2a    SorterRecord *
272dc 70 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  p1,             
272dd 20 20 2f 2a 20 46 69 72 73 74 20 6c 69 73 74 20    /* First list 
272de 74 6f 20 6d 65 72 67 65 20 2a 2f 0a 20 20 53 6f  to merge */.  So
272df 72 74 65 72 52 65 63 6f 72 64 20 2a 70 32 2c 20  rterRecord *p2, 
272e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
272e1 20 53 65 63 6f 6e 64 20 6c 69 73 74 20 74 6f 20   Second list to 
272e2 6d 65 72 67 65 20 2a 2f 0a 20 20 53 6f 72 74 65  merge */.  Sorte
272e3 72 52 65 63 6f 72 64 20 2a 2a 70 70 4f 75 74 20  rRecord **ppOut 
272e4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
272e5 54 3a 20 48 65 61 64 20 6f 66 20 6d 65 72 67 65  T: Head of merge
272e6 64 20 6c 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 53  d list */.){.  S
272e7 6f 72 74 65 72 52 65 63 6f 72 64 20 2a 70 46 69  orterRecord *pFi
272e8 6e 61 6c 20 3d 20 30 3b 0a 20 20 53 6f 72 74 65  nal = 0;.  Sorte
272e9 72 52 65 63 6f 72 64 20 2a 2a 70 70 20 3d 20 26  rRecord **pp = &
272ea 70 46 69 6e 61 6c 3b 0a 20 20 76 6f 69 64 20 2a  pFinal;.  void *
272eb 70 56 61 6c 32 20 3d 20 70 32 20 3f 20 70 32 2d  pVal2 = p2 ? p2-
272ec 3e 70 56 61 6c 20 3a 20 30 3b 0a 0a 20 20 77 68  >pVal : 0;..  wh
272ed 69 6c 65 28 20 70 31 20 26 26 20 70 32 20 29 7b  ile( p1 && p2 ){
272ee 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20  .    int res;.  
272ef 20 20 76 64 62 65 53 6f 72 74 65 72 43 6f 6d 70    vdbeSorterComp
272f0 61 72 65 28 70 43 73 72 2c 20 30 2c 20 70 31 2d  are(pCsr, 0, p1-
272f1 3e 70 56 61 6c 2c 20 70 31 2d 3e 6e 56 61 6c 2c  >pVal, p1->nVal,
272f2 20 70 56 61 6c 32 2c 20 70 32 2d 3e 6e 56 61 6c   pVal2, p2->nVal
272f3 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28  , &res);.    if(
272f4 20 72 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 20   res<=0 ){.     
272f5 20 2a 70 70 20 3d 20 70 31 3b 0a 20 20 20 20 20   *pp = p1;.     
272f6 20 70 70 20 3d 20 26 70 31 2d 3e 70 4e 65 78 74   pp = &p1->pNext
272f7 3b 0a 20 20 20 20 20 20 70 31 20 3d 20 70 31 2d  ;.      p1 = p1-
272f8 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 70 56  >pNext;.      pV
272f9 61 6c 32 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  al2 = 0;.    }el
272fa 73 65 7b 0a 20 20 20 20 20 20 2a 70 70 20 3d 20  se{.      *pp = 
272fb 70 32 3b 0a 20 20 20 20 20 20 20 70 70 20 3d 20  p2;.       pp = 
272fc 26 70 32 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  &p2->pNext;.    
272fd 20 20 70 32 20 3d 20 70 32 2d 3e 70 4e 65 78 74    p2 = p2->pNext
272fe 3b 0a 20 20 20 20 20 20 69 66 28 20 70 32 3d 3d  ;.      if( p2==
272ff 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  0 ) break;.     
27300 20 70 56 61 6c 32 20 3d 20 70 32 2d 3e 70 56 61   pVal2 = p2->pVa
27301 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a  l;.    }.  }.  *
27302 70 70 20 3d 20 70 31 20 3f 20 70 31 20 3a 20 70  pp = p1 ? p1 : p
27303 32 3b 0a 20 20 2a 70 70 4f 75 74 20 3d 20 70 46  2;.  *ppOut = pF
27304 69 6e 61 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  inal;.}../*.** S
27305 6f 72 74 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c  ort the linked l
27306 69 73 74 20 6f 66 20 72 65 63 6f 72 64 73 20 68  ist of records h
27307 65 61 64 65 64 20 61 74 20 70 43 73 72 2d 3e 70  eaded at pCsr->p
27308 52 65 63 6f 72 64 2e 20 52 65 74 75 72 6e 20 53  Record. Return S
27309 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 66 20 73  QLITE_OK.** if s
2730a 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e  uccessful, or an
2730b 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f   SQLite error co
2730c 64 65 20 28 69 2e 65 2e 20 53 51 4c 49 54 45 5f  de (i.e. SQLITE_
2730d 4e 4f 4d 45 4d 29 20 69 66 20 61 6e 20 65 72 72  NOMEM) if an err
2730e 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2f  or.** occurs..*/
2730f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64 62 65  .static int vdbe
27310 53 6f 72 74 65 72 53 6f 72 74 28 63 6f 6e 73 74  SorterSort(const
27311 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 73   VdbeCursor *pCs
27312 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 53  r){.  int i;.  S
27313 6f 72 74 65 72 52 65 63 6f 72 64 20 2a 2a 61 53  orterRecord **aS
27314 6c 6f 74 3b 0a 20 20 53 6f 72 74 65 72 52 65 63  lot;.  SorterRec
27315 6f 72 64 20 2a 70 3b 0a 20 20 56 64 62 65 53 6f  ord *p;.  VdbeSo
27316 72 74 65 72 20 2a 70 53 6f 72 74 65 72 20 3d 20  rter *pSorter = 
27317 70 43 73 72 2d 3e 70 53 6f 72 74 65 72 3b 0a 0a  pCsr->pSorter;..
27318 20 20 61 53 6c 6f 74 20 3d 20 28 53 6f 72 74 65    aSlot = (Sorte
27319 72 52 65 63 6f 72 64 20 2a 2a 29 73 71 6c 69 74  rRecord **)sqlit
2731a 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 36 34 20  e3MallocZero(64 
2731b 2a 20 73 69 7a 65 6f 66 28 53 6f 72 74 65 72 52  * sizeof(SorterR
2731c 65 63 6f 72 64 20 2a 29 29 3b 0a 20 20 69 66 28  ecord *));.  if(
2731d 20 21 61 53 6c 6f 74 20 29 7b 0a 20 20 20 20 72   !aSlot ){.    r
2731e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
2731f 45 4d 3b 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 70  EM;.  }..  p = p
27320 53 6f 72 74 65 72 2d 3e 70 52 65 63 6f 72 64 3b  Sorter->pRecord;
27321 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20  .  while( p ){. 
27322 20 20 20 53 6f 72 74 65 72 52 65 63 6f 72 64 20     SorterRecord 
27323 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78  *pNext = p->pNex
27324 74 3b 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 20  t;.    p->pNext 
27325 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  = 0;.    for(i=0
27326 3b 20 61 53 6c 6f 74 5b 69 5d 3b 20 69 2b 2b 29  ; aSlot[i]; i++)
27327 7b 0a 20 20 20 20 20 20 76 64 62 65 53 6f 72 74  {.      vdbeSort
27328 65 72 4d 65 72 67 65 28 70 43 73 72 2c 20 70 2c  erMerge(pCsr, p,
27329 20 61 53 6c 6f 74 5b 69 5d 2c 20 26 70 29 3b 0a   aSlot[i], &p);.
2732a 20 20 20 20 20 20 61 53 6c 6f 74 5b 69 5d 20 3d        aSlot[i] =
2732b 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 53   0;.    }.    aS
2732c 6c 6f 74 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20  lot[i] = p;.    
2732d 70 20 3d 20 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a  p = pNext;.  }..
2732e 20 20 70 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69    p = 0;.  for(i
2732f 3d 30 3b 20 69 3c 36 34 3b 20 69 2b 2b 29 7b 0a  =0; i<64; i++){.
27330 20 20 20 20 76 64 62 65 53 6f 72 74 65 72 4d 65      vdbeSorterMe
27331 72 67 65 28 70 43 73 72 2c 20 70 2c 20 61 53 6c  rge(pCsr, p, aSl
27332 6f 74 5b 69 5d 2c 20 26 70 29 3b 0a 20 20 7d 0a  ot[i], &p);.  }.
27333 20 20 70 53 6f 72 74 65 72 2d 3e 70 52 65 63 6f    pSorter->pReco
27334 72 64 20 3d 20 70 3b 0a 0a 20 20 73 71 6c 69 74  rd = p;..  sqlit
27335 65 33 5f 66 72 65 65 28 61 53 6c 6f 74 29 3b 0a  e3_free(aSlot);.
27336 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
27337 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  OK;.}../*.** Ini
27338 74 69 61 6c 69 7a 65 20 61 20 66 69 6c 65 2d 77  tialize a file-w
27339 72 69 74 65 72 20 6f 62 6a 65 63 74 2e 0a 2a 2f  riter object..*/
2733a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c  .static void fil
2733b 65 57 72 69 74 65 72 49 6e 69 74 28 0a 20 20 73  eWriterInit(.  s
2733c 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2733d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2733e 2a 20 44 61 74 61 62 61 73 65 20 28 66 6f 72 20  * Database (for 
2733f 6d 61 6c 6c 6f 63 29 20 2a 2f 0a 20 20 73 71 6c  malloc) */.  sql
27340 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65  ite3_file *pFile
27341 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
27342 46 69 6c 65 20 74 6f 20 77 72 69 74 65 20 74 6f  File to write to
27343 20 2a 2f 0a 20 20 46 69 6c 65 57 72 69 74 65 72   */.  FileWriter
27344 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
27345 20 20 20 20 20 20 2f 2a 20 4f 62 6a 65 63 74 20        /* Object 
27346 74 6f 20 70 6f 70 75 6c 61 74 65 20 2a 2f 0a 20  to populate */. 
27347 20 69 36 34 20 69 53 74 61 72 74 20 20 20 20 20   i64 iStart     
27348 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27349 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 70 46   /* Offset of pF
2734a 69 6c 65 20 74 6f 20 62 65 67 69 6e 20 77 72 69  ile to begin wri
2734b 74 69 6e 67 20 61 74 20 2a 2f 0a 29 7b 0a 20 20  ting at */.){.  
2734c 69 6e 74 20 6e 42 75 66 20 3d 20 73 71 6c 69 74  int nBuf = sqlit
2734d 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69  e3BtreeGetPageSi
2734e 7a 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42  ze(db->aDb[0].pB
2734f 74 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 70 2c  t);..  memset(p,
27350 20 30 2c 20 73 69 7a 65 6f 66 28 46 69 6c 65 57   0, sizeof(FileW
27351 72 69 74 65 72 29 29 3b 0a 20 20 70 2d 3e 61 42  riter));.  p->aB
27352 75 66 66 65 72 20 3d 20 28 75 38 20 2a 29 73 71  uffer = (u8 *)sq
27353 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
27354 28 64 62 2c 20 6e 42 75 66 29 3b 0a 20 20 69 66  (db, nBuf);.  if
27355 28 20 21 70 2d 3e 61 42 75 66 66 65 72 20 29 7b  ( !p->aBuffer ){
27356 0a 20 20 20 20 70 2d 3e 65 46 57 45 72 72 20 3d  .    p->eFWErr =
27357 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
27358 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 69   }else{.    p->i
27359 42 75 66 45 6e 64 20 3d 20 70 2d 3e 69 42 75 66  BufEnd = p->iBuf
2735a 53 74 61 72 74 20 3d 20 28 69 53 74 61 72 74 20  Start = (iStart 
2735b 25 20 6e 42 75 66 29 3b 0a 20 20 20 20 70 2d 3e  % nBuf);.    p->
2735c 69 57 72 69 74 65 4f 66 66 20 3d 20 69 53 74 61  iWriteOff = iSta
2735d 72 74 20 2d 20 70 2d 3e 69 42 75 66 53 74 61 72  rt - p->iBufStar
2735e 74 3b 0a 20 20 20 20 70 2d 3e 6e 42 75 66 66 65  t;.    p->nBuffe
2735f 72 20 3d 20 6e 42 75 66 3b 0a 20 20 20 20 70 2d  r = nBuf;.    p-
27360 3e 70 46 69 6c 65 20 3d 20 70 46 69 6c 65 3b 0a  >pFile = pFile;.
27361 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69    }.}../*.** Wri
27362 74 65 20 6e 44 61 74 61 20 62 79 74 65 73 20 6f  te nData bytes o
27363 66 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69  f data to the fi
27364 6c 65 2d 77 72 69 74 65 20 6f 62 6a 65 63 74 2e  le-write object.
27365 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
27366 4b 0a 2a 2a 20 69 66 20 73 75 63 63 65 73 73 66  K.** if successf
27367 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65  ul, or an SQLite
27368 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61   error code if a
27369 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a  n error occurs..
2736a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
2736b 69 6c 65 57 72 69 74 65 72 57 72 69 74 65 28 46  ileWriterWrite(F
2736c 69 6c 65 57 72 69 74 65 72 20 2a 70 2c 20 75 38  ileWriter *p, u8
2736d 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61   *pData, int nDa
2736e 74 61 29 7b 0a 20 20 69 6e 74 20 6e 52 65 6d 20  ta){.  int nRem 
2736f 3d 20 6e 44 61 74 61 3b 0a 20 20 77 68 69 6c 65  = nData;.  while
27370 28 20 6e 52 65 6d 3e 30 20 26 26 20 70 2d 3e 65  ( nRem>0 && p->e
27371 46 57 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20  FWErr==0 ){.    
27372 69 6e 74 20 6e 43 6f 70 79 20 3d 20 6e 52 65 6d  int nCopy = nRem
27373 3b 0a 20 20 20 20 69 66 28 20 6e 43 6f 70 79 3e  ;.    if( nCopy>
27374 28 70 2d 3e 6e 42 75 66 66 65 72 20 2d 20 70 2d  (p->nBuffer - p-
27375 3e 69 42 75 66 45 6e 64 29 20 29 7b 0a 20 20 20  >iBufEnd) ){.   
27376 20 20 20 6e 43 6f 70 79 20 3d 20 70 2d 3e 6e 42     nCopy = p->nB
27377 75 66 66 65 72 20 2d 20 70 2d 3e 69 42 75 66 45  uffer - p->iBufE
27378 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d  nd;.    }..    m
27379 65 6d 63 70 79 28 26 70 2d 3e 61 42 75 66 66 65  emcpy(&p->aBuffe
2737a 72 5b 70 2d 3e 69 42 75 66 45 6e 64 5d 2c 20 26  r[p->iBufEnd], &
2737b 70 44 61 74 61 5b 6e 44 61 74 61 2d 6e 52 65 6d  pData[nData-nRem
2737c 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 70  ], nCopy);.    p
2737d 2d 3e 69 42 75 66 45 6e 64 20 2b 3d 20 6e 43 6f  ->iBufEnd += nCo
2737e 70 79 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69  py;.    if( p->i
2737f 42 75 66 45 6e 64 3d 3d 70 2d 3e 6e 42 75 66 66  BufEnd==p->nBuff
27380 65 72 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65  er ){.      p->e
27381 46 57 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4f  FWErr = sqlite3O
27382 73 57 72 69 74 65 28 70 2d 3e 70 46 69 6c 65 2c  sWrite(p->pFile,
27383 20 0a 20 20 20 20 20 20 20 20 20 20 26 70 2d 3e   .          &p->
27384 61 42 75 66 66 65 72 5b 70 2d 3e 69 42 75 66 53  aBuffer[p->iBufS
27385 74 61 72 74 5d 2c 20 70 2d 3e 69 42 75 66 45 6e  tart], p->iBufEn
27386 64 20 2d 20 70 2d 3e 69 42 75 66 53 74 61 72 74  d - p->iBufStart
27387 2c 20 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e  , .          p->
27388 69 57 72 69 74 65 4f 66 66 20 2b 20 70 2d 3e 69  iWriteOff + p->i
27389 42 75 66 53 74 61 72 74 0a 20 20 20 20 20 20 29  BufStart.      )
2738a 3b 0a 20 20 20 20 20 20 70 2d 3e 69 42 75 66 53  ;.      p->iBufS
2738b 74 61 72 74 20 3d 20 70 2d 3e 69 42 75 66 45 6e  tart = p->iBufEn
2738c 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e  d = 0;.      p->
2738d 69 57 72 69 74 65 4f 66 66 20 2b 3d 20 70 2d 3e  iWriteOff += p->
2738e 6e 42 75 66 66 65 72 3b 0a 20 20 20 20 7d 0a 20  nBuffer;.    }. 
2738f 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 42     assert( p->iB
27390 75 66 45 6e 64 3c 70 2d 3e 6e 42 75 66 66 65 72  ufEnd<p->nBuffer
27391 20 29 3b 0a 0a 20 20 20 20 6e 52 65 6d 20 2d 3d   );..    nRem -=
27392 20 6e 43 6f 70 79 3b 0a 20 20 7d 0a 7d 0a 0a 2f   nCopy;.  }.}../
27393 2a 0a 2a 2a 20 46 6c 75 73 68 20 61 6e 79 20 62  *.** Flush any b
27394 75 66 66 65 72 65 64 20 64 61 74 61 20 74 6f 20  uffered data to 
27395 64 69 73 6b 20 61 6e 64 20 63 6c 65 61 6e 20 75  disk and clean u
27396 70 20 74 68 65 20 66 69 6c 65 2d 77 72 69 74 65  p the file-write
27397 72 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54 68 65  r object..** The
27398 20 72 65 73 75 6c 74 73 20 6f 66 20 75 73 69 6e   results of usin
27399 67 20 74 68 65 20 66 69 6c 65 2d 77 72 69 74 65  g the file-write
2739a 72 20 61 66 74 65 72 20 74 68 69 73 20 63 61 6c  r after this cal
2739b 6c 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e  l are undefined.
2739c 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
2739d 45 5f 4f 4b 20 69 66 20 66 6c 75 73 68 69 6e 67  E_OK if flushing
2739e 20 74 68 65 20 62 75 66 66 65 72 65 64 20 64 61   the buffered da
2739f 74 61 20 73 75 63 63 65 65 64 73 20 6f 72 20 69  ta succeeds or i
273a0 73 20 6e 6f 74 20 0a 2a 2a 20 72 65 71 75 69 72  s not .** requir
273a1 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72  ed. Otherwise, r
273a2 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65 20  eturn an SQLite 
273a3 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a  error code..**.*
273a4 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69  * Before returni
273a5 6e 67 2c 20 73 65 74 20 2a 70 69 45 6f 66 20 74  ng, set *piEof t
273a6 6f 20 74 68 65 20 6f 66 66 73 65 74 20 69 6d 6d  o the offset imm
273a7 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69  ediately followi
273a8 6e 67 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 62  ng the.** last b
273a9 79 74 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74  yte written to t
273aa 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74  he file..*/.stat
273ab 69 63 20 69 6e 74 20 66 69 6c 65 57 72 69 74 65  ic int fileWrite
273ac 72 46 69 6e 69 73 68 28 73 71 6c 69 74 65 33 20  rFinish(sqlite3 
273ad 2a 64 62 2c 20 46 69 6c 65 57 72 69 74 65 72 20  *db, FileWriter 
273ae 2a 70 2c 20 69 36 34 20 2a 70 69 45 6f 66 29 7b  *p, i64 *piEof){
273af 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28  .  int rc;.  if(
273b0 20 70 2d 3e 65 46 57 45 72 72 3d 3d 30 20 26 26   p->eFWErr==0 &&
273b1 20 41 4c 57 41 59 53 28 70 2d 3e 61 42 75 66 66   ALWAYS(p->aBuff
273b2 65 72 29 20 26 26 20 70 2d 3e 69 42 75 66 45 6e  er) && p->iBufEn
273b3 64 3e 70 2d 3e 69 42 75 66 53 74 61 72 74 20 29  d>p->iBufStart )
273b4 7b 0a 20 20 20 20 70 2d 3e 65 46 57 45 72 72 20  {.    p->eFWErr 
273b5 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
273b6 28 70 2d 3e 70 46 69 6c 65 2c 20 0a 20 20 20 20  (p->pFile, .    
273b7 20 20 20 20 26 70 2d 3e 61 42 75 66 66 65 72 5b      &p->aBuffer[
273b8 70 2d 3e 69 42 75 66 53 74 61 72 74 5d 2c 20 70  p->iBufStart], p
273b9 2d 3e 69 42 75 66 45 6e 64 20 2d 20 70 2d 3e 69  ->iBufEnd - p->i
273ba 42 75 66 53 74 61 72 74 2c 20 0a 20 20 20 20 20  BufStart, .     
273bb 20 20 20 70 2d 3e 69 57 72 69 74 65 4f 66 66 20     p->iWriteOff 
273bc 2b 20 70 2d 3e 69 42 75 66 53 74 61 72 74 0a 20  + p->iBufStart. 
273bd 20 20 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 69 45     );.  }.  *piE
273be 6f 66 20 3d 20 28 70 2d 3e 69 57 72 69 74 65 4f  of = (p->iWriteO
273bf 66 66 20 2b 20 70 2d 3e 69 42 75 66 45 6e 64 29  ff + p->iBufEnd)
273c0 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
273c1 65 28 64 62 2c 20 70 2d 3e 61 42 75 66 66 65 72  e(db, p->aBuffer
273c2 29 3b 0a 20 20 72 63 20 3d 20 70 2d 3e 65 46 57  );.  rc = p->eFW
273c3 45 72 72 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c  Err;.  memset(p,
273c4 20 30 2c 20 73 69 7a 65 6f 66 28 46 69 6c 65 57   0, sizeof(FileW
273c5 72 69 74 65 72 29 29 3b 0a 20 20 72 65 74 75 72  riter));.  retur
273c6 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  n rc;.}../*.** W
273c7 72 69 74 65 20 76 61 6c 75 65 20 69 56 61 6c 20  rite value iVal 
273c8 65 6e 63 6f 64 65 64 20 61 73 20 61 20 76 61 72  encoded as a var
273c9 69 6e 74 20 74 6f 20 74 68 65 20 66 69 6c 65 2d  int to the file-
273ca 77 72 69 74 65 20 6f 62 6a 65 63 74 2e 20 52 65  write object. Re
273cb 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  turn .** SQLITE_
273cc 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75 6c  OK if successful
273cd 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65  , or an SQLite e
273ce 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20  rror code if an 
273cf 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f  error occurs..*/
273d0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c  .static void fil
273d1 65 57 72 69 74 65 72 57 72 69 74 65 56 61 72 69  eWriterWriteVari
273d2 6e 74 28 46 69 6c 65 57 72 69 74 65 72 20 2a 70  nt(FileWriter *p
273d3 2c 20 75 36 34 20 69 56 61 6c 29 7b 0a 20 20 69  , u64 iVal){.  i
273d4 6e 74 20 6e 42 79 74 65 3b 20 0a 20 20 75 38 20  nt nByte; .  u8 
273d5 61 42 79 74 65 5b 31 30 5d 3b 0a 20 20 6e 42 79  aByte[10];.  nBy
273d6 74 65 20 3d 20 73 71 6c 69 74 65 33 50 75 74 56  te = sqlite3PutV
273d7 61 72 69 6e 74 28 61 42 79 74 65 2c 20 69 56 61  arint(aByte, iVa
273d8 6c 29 3b 0a 20 20 66 69 6c 65 57 72 69 74 65 72  l);.  fileWriter
273d9 57 72 69 74 65 28 70 2c 20 61 42 79 74 65 2c 20  Write(p, aByte, 
273da 6e 42 79 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  nByte);.}../*.**
273db 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72 65   Write the curre
273dc 6e 74 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  nt contents of t
273dd 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6c 69 6e  he in-memory lin
273de 6b 65 64 2d 6c 69 73 74 20 74 6f 20 61 20 50 4d  ked-list to a PM
273df 41 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  A. Return.** SQL
273e0 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 73  ITE_OK if succes
273e1 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69  sful, or an SQLi
273e2 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74  te error code ot
273e3 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  herwise..**.** T
273e4 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 61 20 50  he format of a P
273e5 4d 41 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  MA is:.**.**    
273e6 20 2a 20 41 20 76 61 72 69 6e 74 2e 20 54 68 69   * A varint. Thi
273e7 73 20 76 61 72 69 6e 74 20 63 6f 6e 74 61 69 6e  s varint contain
273e8 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  s the total numb
273e9 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 63  er of bytes of c
273ea 6f 6e 74 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20  ontent.**       
273eb 69 6e 20 74 68 65 20 50 4d 41 20 28 6e 6f 74 20  in the PMA (not 
273ec 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 76 61  including the va
273ed 72 69 6e 74 20 69 74 73 65 6c 66 29 2e 0a 2a 2a  rint itself)..**
273ee 0a 2a 2a 20 20 20 20 20 2a 20 4f 6e 65 20 6f 72  .**     * One or
273ef 20 6d 6f 72 65 20 72 65 63 6f 72 64 73 20 70 61   more records pa
273f0 63 6b 65 64 20 65 6e 64 2d 74 6f 2d 65 6e 64 20  cked end-to-end 
273f1 69 6e 20 6f 72 64 65 72 20 6f 66 20 61 73 63 65  in order of asce
273f2 6e 64 69 6e 67 20 6b 65 79 73 2e 20 0a 2a 2a 20  nding keys. .** 
273f3 20 20 20 20 20 20 45 61 63 68 20 72 65 63 6f 72        Each recor
273f4 64 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20  d consists of a 
273f5 76 61 72 69 6e 74 20 66 6f 6c 6c 6f 77 65 64 20  varint followed 
273f6 62 79 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74  by a blob of dat
273f7 61 20 28 74 68 65 20 0a 2a 2a 20 20 20 20 20 20  a (the .**      
273f8 20 6b 65 79 29 2e 20 54 68 65 20 76 61 72 69 6e   key). The varin
273f9 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  t is the number 
273fa 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
273fb 62 6c 6f 62 20 6f 66 20 64 61 74 61 2e 0a 2a 2f  blob of data..*/
273fc 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64 62 65  .static int vdbe
273fd 53 6f 72 74 65 72 4c 69 73 74 54 6f 50 4d 41 28  SorterListToPMA(
273fe 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
273ff 73 74 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  st VdbeCursor *p
27400 43 73 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  Csr){.  int rc =
27401 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
27402 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
27403 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 56 64 62 65  n code */.  Vdbe
27404 53 6f 72 74 65 72 20 2a 70 53 6f 72 74 65 72 20  Sorter *pSorter 
27405 3d 20 70 43 73 72 2d 3e 70 53 6f 72 74 65 72 3b  = pCsr->pSorter;
27406 0a 20 20 46 69 6c 65 57 72 69 74 65 72 20 77 72  .  FileWriter wr
27407 69 74 65 72 3b 0a 0a 20 20 6d 65 6d 73 65 74 28  iter;..  memset(
27408 26 77 72 69 74 65 72 2c 20 30 2c 20 73 69 7a 65  &writer, 0, size
27409 6f 66 28 46 69 6c 65 57 72 69 74 65 72 29 29 3b  of(FileWriter));
2740a 0a 0a 20 20 69 66 28 20 70 53 6f 72 74 65 72 2d  ..  if( pSorter-
2740b 3e 6e 49 6e 4d 65 6d 6f 72 79 3d 3d 30 20 29 7b  >nInMemory==0 ){
2740c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 6f  .    assert( pSo
2740d 72 74 65 72 2d 3e 70 52 65 63 6f 72 64 3d 3d 30  rter->pRecord==0
2740e 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   );.    return r
2740f 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 76  c;.  }..  rc = v
27410 64 62 65 53 6f 72 74 65 72 53 6f 72 74 28 70 43  dbeSorterSort(pC
27411 73 72 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  sr);..  /* If th
27412 65 20 66 69 72 73 74 20 74 65 6d 70 6f 72 61 72  e first temporar
27413 79 20 50 4d 41 20 66 69 6c 65 20 68 61 73 20 6e  y PMA file has n
27414 6f 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20  ot been opened, 
27415 6f 70 65 6e 20 69 74 20 6e 6f 77 2e 20 2a 2f 0a  open it now. */.
27416 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
27417 5f 4f 4b 20 26 26 20 70 53 6f 72 74 65 72 2d 3e  _OK && pSorter->
27418 70 54 65 6d 70 31 3d 3d 30 20 29 7b 0a 20 20 20  pTemp1==0 ){.   
27419 20 72 63 20 3d 20 76 64 62 65 53 6f 72 74 65 72   rc = vdbeSorter
2741a 4f 70 65 6e 54 65 6d 70 46 69 6c 65 28 64 62 2c  OpenTempFile(db,
2741b 20 26 70 53 6f 72 74 65 72 2d 3e 70 54 65 6d 70   &pSorter->pTemp
2741c 31 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  1);.    assert( 
2741d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
2741e 20 70 53 6f 72 74 65 72 2d 3e 70 54 65 6d 70 31   pSorter->pTemp1
2741f 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
27420 70 53 6f 72 74 65 72 2d 3e 69 57 72 69 74 65 4f  pSorter->iWriteO
27421 66 66 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  ff==0 );.    ass
27422 65 72 74 28 20 70 53 6f 72 74 65 72 2d 3e 6e 50  ert( pSorter->nP
27423 4d 41 3d 3d 30 20 29 3b 0a 20 20 7d 0a 0a 20 20  MA==0 );.  }..  
27424 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
27425 4b 20 29 7b 0a 20 20 20 20 53 6f 72 74 65 72 52  K ){.    SorterR
27426 65 63 6f 72 64 20 2a 70 3b 0a 20 20 20 20 53 6f  ecord *p;.    So
27427 72 74 65 72 52 65 63 6f 72 64 20 2a 70 4e 65 78  rterRecord *pNex
27428 74 20 3d 20 30 3b 0a 0a 20 20 20 20 66 69 6c 65  t = 0;..    file
27429 57 72 69 74 65 72 49 6e 69 74 28 64 62 2c 20 70  WriterInit(db, p
2742a 53 6f 72 74 65 72 2d 3e 70 54 65 6d 70 31 2c 20  Sorter->pTemp1, 
2742b 26 77 72 69 74 65 72 2c 20 70 53 6f 72 74 65 72  &writer, pSorter
2742c 2d 3e 69 57 72 69 74 65 4f 66 66 29 3b 0a 20 20  ->iWriteOff);.  
2742d 20 20 70 53 6f 72 74 65 72 2d 3e 6e 50 4d 41 2b    pSorter->nPMA+
2742e 2b 3b 0a 20 20 20 20 66 69 6c 65 57 72 69 74 65  +;.    fileWrite
2742f 72 57 72 69 74 65 56 61 72 69 6e 74 28 26 77 72  rWriteVarint(&wr
27430 69 74 65 72 2c 20 70 53 6f 72 74 65 72 2d 3e 6e  iter, pSorter->n
27431 49 6e 4d 65 6d 6f 72 79 29 3b 0a 20 20 20 20 66  InMemory);.    f
27432 6f 72 28 70 3d 70 53 6f 72 74 65 72 2d 3e 70 52  or(p=pSorter->pR
27433 65 63 6f 72 64 3b 20 70 3b 20 70 3d 70 4e 65 78  ecord; p; p=pNex
27434 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 74 20  t){.      pNext 
27435 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  = p->pNext;.    
27436 20 20 66 69 6c 65 57 72 69 74 65 72 57 72 69 74    fileWriterWrit
27437 65 56 61 72 69 6e 74 28 26 77 72 69 74 65 72 2c  eVarint(&writer,
27438 20 70 2d 3e 6e 56 61 6c 29 3b 0a 20 20 20 20 20   p->nVal);.     
27439 20 66 69 6c 65 57 72 69 74 65 72 57 72 69 74 65   fileWriterWrite
2743a 28 26 77 72 69 74 65 72 2c 20 70 2d 3e 70 56 61  (&writer, p->pVa
2743b 6c 2c 20 70 2d 3e 6e 56 61 6c 29 3b 0a 20 20 20  l, p->nVal);.   
2743c 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2743d 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20  (db, p);.    }. 
2743e 20 20 20 70 53 6f 72 74 65 72 2d 3e 70 52 65 63     pSorter->pRec
2743f 6f 72 64 20 3d 20 70 3b 0a 20 20 20 20 72 63 20  ord = p;.    rc 
27440 3d 20 66 69 6c 65 57 72 69 74 65 72 46 69 6e 69  = fileWriterFini
27441 73 68 28 64 62 2c 20 26 77 72 69 74 65 72 2c 20  sh(db, &writer, 
27442 26 70 53 6f 72 74 65 72 2d 3e 69 57 72 69 74 65  &pSorter->iWrite
27443 4f 66 66 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  Off);.  }..  ret
27444 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
27445 20 41 64 64 20 61 20 72 65 63 6f 72 64 20 74 6f   Add a record to
27446 20 74 68 65 20 73 6f 72 74 65 72 2e 0a 2a 2f 0a   the sorter..*/.
27447 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
27448 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f  nt sqlite3VdbeSo
27449 72 74 65 72 57 72 69 74 65 28 0a 20 20 73 71 6c  rterWrite(.  sql
2744a 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2744b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2744c 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  Database handle 
2744d 2a 2f 0a 20 20 63 6f 6e 73 74 20 56 64 62 65 43  */.  const VdbeC
2744e 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20  ursor *pCsr,    
2744f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f             /* So
27450 72 74 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20  rter cursor */. 
27451 20 4d 65 6d 20 2a 70 56 61 6c 20 20 20 20 20 20   Mem *pVal      
27452 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27453 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20   /* Memory cell 
27454 63 6f 6e 74 61 69 6e 69 6e 67 20 72 65 63 6f 72  containing recor
27455 64 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 53 6f  d */.){.  VdbeSo
27456 72 74 65 72 20 2a 70 53 6f 72 74 65 72 20 3d 20  rter *pSorter = 
27457 70 43 73 72 2d 3e 70 53 6f 72 74 65 72 3b 0a 20  pCsr->pSorter;. 
27458 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
27459 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
2745a 20 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20   /* Return Code 
2745b 2a 2f 0a 20 20 53 6f 72 74 65 72 52 65 63 6f 72  */.  SorterRecor
2745c 64 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20  d *pNew;        
2745d 20 20 20 20 20 2f 2a 20 4e 65 77 20 6c 69 73 74       /* New list
2745e 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 0a 20 20 61   element */..  a
2745f 73 73 65 72 74 28 20 70 53 6f 72 74 65 72 20 29  ssert( pSorter )
27460 3b 0a 20 20 70 53 6f 72 74 65 72 2d 3e 6e 49 6e  ;.  pSorter->nIn
27461 4d 65 6d 6f 72 79 20 2b 3d 20 73 71 6c 69 74 65  Memory += sqlite
27462 33 56 61 72 69 6e 74 4c 65 6e 28 70 56 61 6c 2d  3VarintLen(pVal-
27463 3e 6e 29 20 2b 20 70 56 61 6c 2d 3e 6e 3b 0a 0a  >n) + pVal->n;..
27464 20 20 70 4e 65 77 20 3d 20 28 53 6f 72 74 65 72    pNew = (Sorter
27465 52 65 63 6f 72 64 20 2a 29 73 71 6c 69 74 65 33  Record *)sqlite3
27466 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
27467 70 56 61 6c 2d 3e 6e 20 2b 20 73 69 7a 65 6f 66  pVal->n + sizeof
27468 28 53 6f 72 74 65 72 52 65 63 6f 72 64 29 29 3b  (SorterRecord));
27469 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29  .  if( pNew==0 )
2746a 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
2746b 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65  E_NOMEM;.  }else
2746c 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 56 61 6c  {.    pNew->pVal
2746d 20 3d 20 28 76 6f 69 64 20 2a 29 26 70 4e 65 77   = (void *)&pNew
2746e 5b 31 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  [1];.    memcpy(
2746f 70 4e 65 77 2d 3e 70 56 61 6c 2c 20 70 56 61 6c  pNew->pVal, pVal
27470 2d 3e 7a 2c 20 70 56 61 6c 2d 3e 6e 29 3b 0a 20  ->z, pVal->n);. 
27471 20 20 20 70 4e 65 77 2d 3e 6e 56 61 6c 20 3d 20     pNew->nVal = 
27472 70 56 61 6c 2d 3e 6e 3b 0a 20 20 20 20 70 4e 65  pVal->n;.    pNe
27473 77 2d 3e 70 4e 65 78 74 20 3d 20 70 53 6f 72 74  w->pNext = pSort
27474 65 72 2d 3e 70 52 65 63 6f 72 64 3b 0a 20 20 20  er->pRecord;.   
27475 20 70 53 6f 72 74 65 72 2d 3e 70 52 65 63 6f 72   pSorter->pRecor
27476 64 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 0a 20  d = pNew;.  }.. 
27477 20 2f 2a 20 53 65 65 20 69 66 20 74 68 65 20 63   /* See if the c
27478 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 73  ontents of the s
27479 6f 72 74 65 72 20 73 68 6f 75 6c 64 20 6e 6f 77  orter should now
2747a 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 74 2e   be written out.
2747b 20 54 68 65 79 0a 20 20 2a 2a 20 61 72 65 20 77   They.  ** are w
2747c 72 69 74 74 65 6e 20 6f 75 74 20 77 68 65 6e 20  ritten out when 
2747d 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 66 6f  either of the fo
2747e 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65  llowing are true
2747f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20  :.  **.  **   * 
27480 54 68 65 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79  The total memory
27481 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74   allocated for t
27482 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6c 69 73  he in-memory lis
27483 74 20 69 73 20 67 72 65 61 74 65 72 20 0a 20 20  t is greater .  
27484 2a 2a 20 20 20 20 20 74 68 61 6e 20 28 70 61 67  **     than (pag
27485 65 2d 73 69 7a 65 20 2a 20 63 61 63 68 65 2d 73  e-size * cache-s
27486 69 7a 65 29 2c 20 6f 72 0a 20 20 2a 2a 0a 20 20  ize), or.  **.  
27487 2a 2a 20 20 20 2a 20 54 68 65 20 74 6f 74 61 6c  **   * The total
27488 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 65   memory allocate
27489 64 20 66 6f 72 20 74 68 65 20 69 6e 2d 6d 65 6d  d for the in-mem
2748a 6f 72 79 20 6c 69 73 74 20 69 73 20 67 72 65 61  ory list is grea
2748b 74 65 72 20 0a 20 20 2a 2a 20 20 20 20 20 74 68  ter .  **     th
2748c 61 6e 20 28 70 61 67 65 2d 73 69 7a 65 20 2a 20  an (page-size * 
2748d 31 30 29 20 61 6e 64 20 73 71 6c 69 74 65 33 48  10) and sqlite3H
2748e 65 61 70 4e 65 61 72 6c 79 46 75 6c 6c 28 29 20  eapNearlyFull() 
2748f 72 65 74 75 72 6e 73 20 74 72 75 65 2e 0a 20 20  returns true..  
27490 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
27491 49 54 45 5f 4f 4b 20 26 26 20 70 53 6f 72 74 65  ITE_OK && pSorte
27492 72 2d 3e 6d 78 50 6d 61 53 69 7a 65 3e 30 20 26  r->mxPmaSize>0 &
27493 26 20 28 0a 20 20 20 20 20 20 20 20 28 70 53 6f  & (.        (pSo
27494 72 74 65 72 2d 3e 6e 49 6e 4d 65 6d 6f 72 79 3e  rter->nInMemory>
27495 70 53 6f 72 74 65 72 2d 3e 6d 78 50 6d 61 53 69  pSorter->mxPmaSi
27496 7a 65 29 0a 20 20 20 20 20 7c 7c 20 28 70 53 6f  ze).     || (pSo
27497 72 74 65 72 2d 3e 6e 49 6e 4d 65 6d 6f 72 79 3e  rter->nInMemory>
27498 70 53 6f 72 74 65 72 2d 3e 6d 6e 50 6d 61 53 69  pSorter->mnPmaSi
27499 7a 65 20 26 26 20 73 71 6c 69 74 65 33 48 65 61  ze && sqlite3Hea
2749a 70 4e 65 61 72 6c 79 46 75 6c 6c 28 29 29 0a 20  pNearlyFull()). 
2749b 20 29 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49   )){.#ifdef SQLI
2749c 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 36 34  TE_DEBUG.    i64
2749d 20 6e 45 78 70 65 63 74 20 3d 20 70 53 6f 72 74   nExpect = pSort
2749e 65 72 2d 3e 69 57 72 69 74 65 4f 66 66 0a 20 20  er->iWriteOff.  
2749f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b 20                + 
274a0 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e  sqlite3VarintLen
274a1 28 70 53 6f 72 74 65 72 2d 3e 6e 49 6e 4d 65 6d  (pSorter->nInMem
274a2 6f 72 79 29 0a 20 20 20 20 20 20 20 20 20 20 20  ory).           
274a3 20 20 20 20 20 2b 20 70 53 6f 72 74 65 72 2d 3e       + pSorter->
274a4 6e 49 6e 4d 65 6d 6f 72 79 3b 0a 23 65 6e 64 69  nInMemory;.#endi
274a5 66 0a 20 20 20 20 72 63 20 3d 20 76 64 62 65 53  f.    rc = vdbeS
274a6 6f 72 74 65 72 4c 69 73 74 54 6f 50 4d 41 28 64  orterListToPMA(d
274a7 62 2c 20 70 43 73 72 29 3b 0a 20 20 20 20 70 53  b, pCsr);.    pS
274a8 6f 72 74 65 72 2d 3e 6e 49 6e 4d 65 6d 6f 72 79  orter->nInMemory
274a9 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74   = 0;.    assert
274aa 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
274ab 7c 7c 20 28 6e 45 78 70 65 63 74 3d 3d 70 53 6f  || (nExpect==pSo
274ac 72 74 65 72 2d 3e 69 57 72 69 74 65 4f 66 66 29  rter->iWriteOff)
274ad 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72   );.  }..  retur
274ae 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48  n rc;.}../*.** H
274af 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66  elper function f
274b0 6f 72 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f  or sqlite3VdbeSo
274b1 72 74 65 72 52 65 77 69 6e 64 28 29 2e 20 0a 2a  rterRewind(). .*
274b2 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64 62  /.static int vdb
274b3 65 53 6f 72 74 65 72 49 6e 69 74 4d 65 72 67 65  eSorterInitMerge
274b4 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
274b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
274b6 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
274b7 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  handle */.  cons
274b8 74 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43  t VdbeCursor *pC
274b9 73 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 43  sr,         /* C
274ba 75 72 73 6f 72 20 68 61 6e 64 6c 65 20 66 6f 72  ursor handle for
274bb 20 74 68 69 73 20 73 6f 72 74 65 72 20 2a 2f 0a   this sorter */.
274bc 20 20 69 36 34 20 2a 70 6e 42 79 74 65 20 20 20    i64 *pnByte   
274bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
274be 20 20 2f 2a 20 53 75 6d 20 6f 66 20 62 79 74 65    /* Sum of byte
274bf 73 20 69 6e 20 61 6c 6c 20 6f 70 65 6e 65 64 20  s in all opened 
274c0 50 4d 41 73 20 2a 2f 0a 29 7b 0a 20 20 56 64 62  PMAs */.){.  Vdb
274c1 65 53 6f 72 74 65 72 20 2a 70 53 6f 72 74 65 72  eSorter *pSorter
274c2 20 3d 20 70 43 73 72 2d 3e 70 53 6f 72 74 65 72   = pCsr->pSorter
274c3 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
274c4 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20  ITE_OK;         
274c5 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
274c6 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20  de */.  int i;  
274c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
274c8 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
274c9 74 6f 20 69 74 65 72 61 74 6f 72 20 74 68 72 6f  to iterator thro
274ca 75 67 68 20 61 49 74 65 72 5b 5d 20 2a 2f 0a 20  ugh aIter[] */. 
274cb 20 69 36 34 20 6e 42 79 74 65 20 3d 20 30 3b 20   i64 nByte = 0; 
274cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
274cd 20 2f 2a 20 54 6f 74 61 6c 20 62 79 74 65 73 20   /* Total bytes 
274ce 69 6e 20 61 6c 6c 20 6f 70 65 6e 65 64 20 50 4d  in all opened PM
274cf 41 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49 6e 69 74  As */..  /* Init
274d0 69 61 6c 69 7a 65 20 74 68 65 20 69 74 65 72 61  ialize the itera
274d1 74 6f 72 73 2e 20 2a 2f 0a 20 20 66 6f 72 28 69  tors. */.  for(i
274d2 3d 30 3b 20 69 3c 53 4f 52 54 45 52 5f 4d 41 58  =0; i<SORTER_MAX
274d3 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 3b 20 69 2b  _MERGE_COUNT; i+
274d4 2b 29 7b 0a 20 20 20 20 56 64 62 65 53 6f 72 74  +){.    VdbeSort
274d5 65 72 49 74 65 72 20 2a 70 49 74 65 72 20 3d 20  erIter *pIter = 
274d6 26 70 53 6f 72 74 65 72 2d 3e 61 49 74 65 72 5b  &pSorter->aIter[
274d7 69 5d 3b 0a 20 20 20 20 72 63 20 3d 20 76 64 62  i];.    rc = vdb
274d8 65 53 6f 72 74 65 72 49 74 65 72 49 6e 69 74 28  eSorterIterInit(
274d9 64 62 2c 20 70 53 6f 72 74 65 72 2c 20 70 53 6f  db, pSorter, pSo
274da 72 74 65 72 2d 3e 69 52 65 61 64 4f 66 66 2c 20  rter->iReadOff, 
274db 70 49 74 65 72 2c 20 26 6e 42 79 74 65 29 3b 0a  pIter, &nByte);.
274dc 20 20 20 20 70 53 6f 72 74 65 72 2d 3e 69 52 65      pSorter->iRe
274dd 61 64 4f 66 66 20 3d 20 70 49 74 65 72 2d 3e 69  adOff = pIter->i
274de 45 6f 66 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Eof;.    assert(
274df 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
274e0 7c 20 70 53 6f 72 74 65 72 2d 3e 69 52 65 61 64  | pSorter->iRead
274e1 4f 66 66 3c 3d 70 53 6f 72 74 65 72 2d 3e 69 57  Off<=pSorter->iW
274e2 72 69 74 65 4f 66 66 20 29 3b 0a 20 20 20 20 69  riteOff );.    i
274e3 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
274e4 20 7c 7c 20 70 53 6f 72 74 65 72 2d 3e 69 52 65   || pSorter->iRe
274e5 61 64 4f 66 66 3e 3d 70 53 6f 72 74 65 72 2d 3e  adOff>=pSorter->
274e6 69 57 72 69 74 65 4f 66 66 20 29 20 62 72 65 61  iWriteOff ) brea
274e7 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69  k;.  }..  /* Ini
274e8 74 69 61 6c 69 7a 65 20 74 68 65 20 61 54 72 65  tialize the aTre
274e9 65 5b 5d 20 61 72 72 61 79 2e 20 2a 2f 0a 20 20  e[] array. */.  
274ea 66 6f 72 28 69 3d 70 53 6f 72 74 65 72 2d 3e 6e  for(i=pSorter->n
274eb 54 72 65 65 2d 31 3b 20 72 63 3d 3d 53 51 4c 49  Tree-1; rc==SQLI
274ec 54 45 5f 4f 4b 20 26 26 20 69 3e 30 3b 20 69 2d  TE_OK && i>0; i-
274ed 2d 29 7b 0a 20 20 20 20 72 63 20 3d 20 76 64 62  -){.    rc = vdb
274ee 65 53 6f 72 74 65 72 44 6f 43 6f 6d 70 61 72 65  eSorterDoCompare
274ef 28 70 43 73 72 2c 20 69 29 3b 0a 20 20 7d 0a 0a  (pCsr, i);.  }..
274f0 20 20 2a 70 6e 42 79 74 65 20 3d 20 6e 42 79 74    *pnByte = nByt
274f1 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  e;.  return rc;.
274f2 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68  }../*.** Once th
274f3 65 20 73 6f 72 74 65 72 20 68 61 73 20 62 65 65  e sorter has bee
274f4 6e 20 70 6f 70 75 6c 61 74 65 64 2c 20 74 68 69  n populated, thi
274f5 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
274f6 6c 6c 65 64 20 74 6f 20 70 72 65 70 61 72 65 0a  lled to prepare.
274f7 2a 2a 20 66 6f 72 20 69 74 65 72 61 74 69 6e 67  ** for iterating
274f8 20 74 68 72 6f 75 67 68 20 69 74 73 20 63 6f 6e   through its con
274f9 74 65 6e 74 73 20 69 6e 20 73 6f 72 74 65 64 20  tents in sorted 
274fa 6f 72 64 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  order..*/.SQLITE
274fb 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
274fc 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 52 65  ite3VdbeSorterRe
274fd 77 69 6e 64 28 73 71 6c 69 74 65 33 20 2a 64 62  wind(sqlite3 *db
274fe 2c 20 63 6f 6e 73 74 20 56 64 62 65 43 75 72 73  , const VdbeCurs
274ff 6f 72 20 2a 70 43 73 72 2c 20 69 6e 74 20 2a 70  or *pCsr, int *p
27500 62 45 6f 66 29 7b 0a 20 20 56 64 62 65 53 6f 72  bEof){.  VdbeSor
27501 74 65 72 20 2a 70 53 6f 72 74 65 72 20 3d 20 70  ter *pSorter = p
27502 43 73 72 2d 3e 70 53 6f 72 74 65 72 3b 0a 20 20  Csr->pSorter;.  
27503 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
27504 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27505 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
27506 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  /.  sqlite3_file
27507 20 2a 70 54 65 6d 70 32 20 3d 20 30 3b 20 20 20   *pTemp2 = 0;   
27508 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 74 65      /* Second te
27509 6d 70 20 66 69 6c 65 20 74 6f 20 75 73 65 20 2a  mp file to use *
2750a 2f 0a 20 20 69 36 34 20 69 57 72 69 74 65 32 20  /.  i64 iWrite2 
2750b 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
2750c 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6f 66 66      /* Write off
2750d 73 65 74 20 66 6f 72 20 70 54 65 6d 70 32 20 2a  set for pTemp2 *
2750e 2f 0a 20 20 69 6e 74 20 6e 49 74 65 72 3b 20 20  /.  int nIter;  
2750f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27510 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
27511 20 69 74 65 72 61 74 6f 72 73 20 75 73 65 64 20   iterators used 
27512 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20  */.  int nByte; 
27513 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27514 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66       /* Bytes of
27515 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20   space required 
27516 66 6f 72 20 61 49 74 65 72 2f 61 54 72 65 65 20  for aIter/aTree 
27517 2a 2f 0a 20 20 69 6e 74 20 4e 20 3d 20 32 3b 20  */.  int N = 2; 
27518 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27519 20 20 20 20 20 2f 2a 20 50 6f 77 65 72 20 6f 66       /* Power of
2751a 20 32 20 3e 3d 20 6e 49 74 65 72 20 2a 2f 0a 0a   2 >= nIter */..
2751b 20 20 61 73 73 65 72 74 28 20 70 53 6f 72 74 65    assert( pSorte
2751c 72 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f  r );..  /* If no
2751d 20 64 61 74 61 20 68 61 73 20 62 65 65 6e 20 77   data has been w
2751e 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2c 20  ritten to disk, 
2751f 74 68 65 6e 20 64 6f 20 6e 6f 74 20 64 6f 20 73  then do not do s
27520 6f 20 6e 6f 77 2e 20 49 6e 73 74 65 61 64 2c 0a  o now. Instead,.
27521 20 20 2a 2a 20 73 6f 72 74 20 74 68 65 20 56 64    ** sort the Vd
27522 62 65 53 6f 72 74 65 72 2e 70 52 65 63 6f 72 64  beSorter.pRecord
27523 20 6c 69 73 74 2e 20 54 68 65 20 76 64 62 65 20   list. The vdbe 
27524 6c 61 79 65 72 20 77 69 6c 6c 20 72 65 61 64 20  layer will read 
27525 64 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20  data directly.  
27526 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d  ** from the in-m
27527 65 6d 6f 72 79 20 6c 69 73 74 2e 20 20 2a 2f 0a  emory list.  */.
27528 20 20 69 66 28 20 70 53 6f 72 74 65 72 2d 3e 6e    if( pSorter->n
27529 50 4d 41 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70  PMA==0 ){.    *p
2752a 62 45 6f 66 20 3d 20 21 70 53 6f 72 74 65 72 2d  bEof = !pSorter-
2752b 3e 70 52 65 63 6f 72 64 3b 0a 20 20 20 20 61 73  >pRecord;.    as
2752c 73 65 72 74 28 20 70 53 6f 72 74 65 72 2d 3e 61  sert( pSorter->a
2752d 54 72 65 65 3d 3d 30 20 29 3b 0a 20 20 20 20 72  Tree==0 );.    r
2752e 65 74 75 72 6e 20 76 64 62 65 53 6f 72 74 65 72  eturn vdbeSorter
2752f 53 6f 72 74 28 70 43 73 72 29 3b 0a 20 20 7d 0a  Sort(pCsr);.  }.
27530 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20  .  /* Write the 
27531 63 75 72 72 65 6e 74 20 69 6e 2d 6d 65 6d 6f 72  current in-memor
27532 79 20 6c 69 73 74 20 74 6f 20 61 20 50 4d 41 2e  y list to a PMA.
27533 20 2a 2f 0a 20 20 72 63 20 3d 20 76 64 62 65 53   */.  rc = vdbeS
27534 6f 72 74 65 72 4c 69 73 74 54 6f 50 4d 41 28 64  orterListToPMA(d
27535 62 2c 20 70 43 73 72 29 3b 0a 20 20 69 66 28 20  b, pCsr);.  if( 
27536 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
27537 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
27538 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20   Allocate space 
27539 66 6f 72 20 61 49 74 65 72 5b 5d 20 61 6e 64 20  for aIter[] and 
2753a 61 54 72 65 65 5b 5d 2e 20 2a 2f 0a 20 20 6e 49  aTree[]. */.  nI
2753b 74 65 72 20 3d 20 70 53 6f 72 74 65 72 2d 3e 6e  ter = pSorter->n
2753c 50 4d 41 3b 0a 20 20 69 66 28 20 6e 49 74 65 72  PMA;.  if( nIter
2753d 3e 53 4f 52 54 45 52 5f 4d 41 58 5f 4d 45 52 47  >SORTER_MAX_MERG
2753e 45 5f 43 4f 55 4e 54 20 29 20 6e 49 74 65 72 20  E_COUNT ) nIter 
2753f 3d 20 53 4f 52 54 45 52 5f 4d 41 58 5f 4d 45 52  = SORTER_MAX_MER
27540 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 61 73 73 65  GE_COUNT;.  asse
27541 72 74 28 20 6e 49 74 65 72 3e 30 20 29 3b 0a 20  rt( nIter>0 );. 
27542 20 77 68 69 6c 65 28 20 4e 3c 6e 49 74 65 72 20   while( N<nIter 
27543 29 20 4e 20 2b 3d 20 4e 3b 0a 20 20 6e 42 79 74  ) N += N;.  nByt
27544 65 20 3d 20 4e 20 2a 20 28 73 69 7a 65 6f 66 28  e = N * (sizeof(
27545 69 6e 74 29 20 2b 20 73 69 7a 65 6f 66 28 56 64  int) + sizeof(Vd
27546 62 65 53 6f 72 74 65 72 49 74 65 72 29 29 3b 0a  beSorterIter));.
27547 20 20 70 53 6f 72 74 65 72 2d 3e 61 49 74 65 72    pSorter->aIter
27548 20 3d 20 28 56 64 62 65 53 6f 72 74 65 72 49 74   = (VdbeSorterIt
27549 65 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  er *)sqlite3DbMa
2754a 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79  llocZero(db, nBy
2754b 74 65 29 3b 0a 20 20 69 66 28 20 21 70 53 6f 72  te);.  if( !pSor
2754c 74 65 72 2d 3e 61 49 74 65 72 20 29 20 72 65 74  ter->aIter ) ret
2754d 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
2754e 3b 0a 20 20 70 53 6f 72 74 65 72 2d 3e 61 54 72  ;.  pSorter->aTr
2754f 65 65 20 3d 20 28 69 6e 74 20 2a 29 26 70 53 6f  ee = (int *)&pSo
27550 72 74 65 72 2d 3e 61 49 74 65 72 5b 4e 5d 3b 0a  rter->aIter[N];.
27551 20 20 70 53 6f 72 74 65 72 2d 3e 6e 54 72 65 65    pSorter->nTree
27552 20 3d 20 4e 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20   = N;..  do {.  
27553 20 20 69 6e 74 20 69 4e 65 77 3b 20 20 20 20 20    int iNew;     
27554 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27555 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6e 65 77 2c  /* Index of new,
27556 20 6d 65 72 67 65 64 2c 20 50 4d 41 20 2a 2f 0a   merged, PMA */.
27557 0a 20 20 20 20 66 6f 72 28 69 4e 65 77 3d 30 3b  .    for(iNew=0;
27558 20 0a 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51   .        rc==SQ
27559 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4e 65 77 2a  LITE_OK && iNew*
2755a 53 4f 52 54 45 52 5f 4d 41 58 5f 4d 45 52 47 45  SORTER_MAX_MERGE
2755b 5f 43 4f 55 4e 54 3c 70 53 6f 72 74 65 72 2d 3e  _COUNT<pSorter->
2755c 6e 50 4d 41 3b 20 0a 20 20 20 20 20 20 20 20 69  nPMA; .        i
2755d 4e 65 77 2b 2b 0a 20 20 20 20 29 7b 0a 20 20 20  New++.    ){.   
2755e 20 20 20 69 6e 74 20 72 63 32 3b 20 20 20 20 20     int rc2;     
2755f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27560 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72  * Return code fr
27561 6f 6d 20 66 69 6c 65 57 72 69 74 65 72 46 69 6e  om fileWriterFin
27562 69 73 68 28 29 20 2a 2f 0a 20 20 20 20 20 20 46  ish() */.      F
27563 69 6c 65 57 72 69 74 65 72 20 77 72 69 74 65 72  ileWriter writer
27564 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 62  ;          /* Ob
27565 6a 65 63 74 20 75 73 65 64 20 74 6f 20 77 72 69  ject used to wri
27566 74 65 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20  te to disk */.  
27567 20 20 20 20 69 36 34 20 6e 57 72 69 74 65 3b 20      i64 nWrite; 
27568 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27569 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
2756a 65 73 20 69 6e 20 6e 65 77 20 50 4d 41 20 2a 2f  es in new PMA */
2756b 0a 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26  ..      memset(&
2756c 77 72 69 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f  writer, 0, sizeo
2756d 66 28 46 69 6c 65 57 72 69 74 65 72 29 29 3b 0a  f(FileWriter));.
2756e 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
2756f 72 65 20 61 72 65 20 53 4f 52 54 45 52 5f 4d 41  re are SORTER_MA
27570 58 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6f 72  X_MERGE_COUNT or
27571 20 6c 65 73 73 20 50 4d 41 73 20 69 6e 20 66 69   less PMAs in fi
27572 6c 65 20 70 54 65 6d 70 31 2c 0a 20 20 20 20 20  le pTemp1,.     
27573 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 61   ** initialize a
27574 6e 20 69 74 65 72 61 74 6f 72 20 66 6f 72 20 65  n iterator for e
27575 61 63 68 20 6f 66 20 74 68 65 6d 20 61 6e 64 20  ach of them and 
27576 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65  break out of the
27577 20 6c 6f 6f 70 2e 0a 20 20 20 20 20 20 2a 2a 20   loop..      ** 
27578 54 68 65 73 65 20 69 74 65 72 61 74 6f 72 73 20  These iterators 
27579 77 69 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e  will be incremen
2757a 74 61 6c 6c 79 20 6d 65 72 67 65 64 20 61 73 20  tally merged as 
2757b 74 68 65 20 56 44 42 45 20 6c 61 79 65 72 20 63  the VDBE layer c
2757c 61 6c 6c 73 0a 20 20 20 20 20 20 2a 2a 20 73 71  alls.      ** sq
2757d 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 4e  lite3VdbeSorterN
2757e 65 78 74 28 29 2e 0a 20 20 20 20 20 20 2a 2a 0a  ext()..      **.
2757f 20 20 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69        ** Otherwi
27580 73 65 2c 20 69 66 20 70 54 65 6d 70 31 20 63 6f  se, if pTemp1 co
27581 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e  ntains more than
27582 20 53 4f 52 54 45 52 5f 4d 41 58 5f 4d 45 52 47   SORTER_MAX_MERG
27583 45 5f 43 4f 55 4e 54 20 50 4d 41 73 2c 0a 20 20  E_COUNT PMAs,.  
27584 20 20 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a      ** initializ
27585 65 20 69 6e 74 65 72 61 74 6f 72 73 20 66 6f 72  e interators for
27586 20 53 4f 52 54 45 52 5f 4d 41 58 5f 4d 45 52 47   SORTER_MAX_MERG
27587 45 5f 43 4f 55 4e 54 20 6f 66 20 74 68 65 6d 2e  E_COUNT of them.
27588 20 54 68 65 73 65 20 50 4d 41 73 0a 20 20 20 20   These PMAs.    
27589 20 20 2a 2a 20 61 72 65 20 6d 65 72 67 65 64 20    ** are merged 
2758a 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 50 4d  into a single PM
2758b 41 20 74 68 61 74 20 69 73 20 77 72 69 74 74 65  A that is writte
2758c 6e 20 74 6f 20 66 69 6c 65 20 70 54 65 6d 70 32  n to file pTemp2
2758d 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2758e 20 72 63 20 3d 20 76 64 62 65 53 6f 72 74 65 72   rc = vdbeSorter
2758f 49 6e 69 74 4d 65 72 67 65 28 64 62 2c 20 70 43  InitMerge(db, pC
27590 73 72 2c 20 26 6e 57 72 69 74 65 29 3b 0a 20 20  sr, &nWrite);.  
27591 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d      assert( rc!=
27592 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 53 6f  SQLITE_OK || pSo
27593 72 74 65 72 2d 3e 61 49 74 65 72 5b 20 70 53 6f  rter->aIter[ pSo
27594 72 74 65 72 2d 3e 61 54 72 65 65 5b 31 5d 20 5d  rter->aTree[1] ]
27595 2e 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20  .pFile );.      
27596 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
27597 4b 20 7c 7c 20 70 53 6f 72 74 65 72 2d 3e 6e 50  K || pSorter->nP
27598 4d 41 3c 3d 53 4f 52 54 45 52 5f 4d 41 58 5f 4d  MA<=SORTER_MAX_M
27599 45 52 47 45 5f 43 4f 55 4e 54 20 29 7b 0a 20 20  ERGE_COUNT ){.  
2759a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2759b 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 4f     }..      /* O
2759c 70 65 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 74  pen the second t
2759d 65 6d 70 20 66 69 6c 65 2c 20 69 66 20 69 74 20  emp file, if it 
2759e 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f  is not already o
2759f 70 65 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  pen. */.      if
275a0 28 20 70 54 65 6d 70 32 3d 3d 30 20 29 7b 0a 20  ( pTemp2==0 ){. 
275a1 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69         assert( i
275a2 57 72 69 74 65 32 3d 3d 30 20 29 3b 0a 20 20 20  Write2==0 );.   
275a3 20 20 20 20 20 72 63 20 3d 20 76 64 62 65 53 6f       rc = vdbeSo
275a4 72 74 65 72 4f 70 65 6e 54 65 6d 70 46 69 6c 65  rterOpenTempFile
275a5 28 64 62 2c 20 26 70 54 65 6d 70 32 29 3b 0a 20  (db, &pTemp2);. 
275a6 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66       }..      if
275a7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
275a8 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 62  ){.        int b
275a9 45 6f 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  Eof = 0;.       
275aa 20 66 69 6c 65 57 72 69 74 65 72 49 6e 69 74 28   fileWriterInit(
275ab 64 62 2c 20 70 54 65 6d 70 32 2c 20 26 77 72 69  db, pTemp2, &wri
275ac 74 65 72 2c 20 69 57 72 69 74 65 32 29 3b 0a 20  ter, iWrite2);. 
275ad 20 20 20 20 20 20 20 66 69 6c 65 57 72 69 74 65         fileWrite
275ae 72 57 72 69 74 65 56 61 72 69 6e 74 28 26 77 72  rWriteVarint(&wr
275af 69 74 65 72 2c 20 6e 57 72 69 74 65 29 3b 0a 20  iter, nWrite);. 
275b0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 63         while( rc
275b1 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 62  ==SQLITE_OK && b
275b2 45 6f 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Eof==0 ){.      
275b3 20 20 20 20 56 64 62 65 53 6f 72 74 65 72 49 74      VdbeSorterIt
275b4 65 72 20 2a 70 49 74 65 72 20 3d 20 26 70 53 6f  er *pIter = &pSo
275b5 72 74 65 72 2d 3e 61 49 74 65 72 5b 20 70 53 6f  rter->aIter[ pSo
275b6 72 74 65 72 2d 3e 61 54 72 65 65 5b 31 5d 20 5d  rter->aTree[1] ]
275b7 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ;.          asse
275b8 72 74 28 20 70 49 74 65 72 2d 3e 70 46 69 6c 65  rt( pIter->pFile
275b9 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 66   );..          f
275ba 69 6c 65 57 72 69 74 65 72 57 72 69 74 65 56 61  ileWriterWriteVa
275bb 72 69 6e 74 28 26 77 72 69 74 65 72 2c 20 70 49  rint(&writer, pI
275bc 74 65 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20  ter->nKey);.    
275bd 20 20 20 20 20 20 66 69 6c 65 57 72 69 74 65 72        fileWriter
275be 57 72 69 74 65 28 26 77 72 69 74 65 72 2c 20 70  Write(&writer, p
275bf 49 74 65 72 2d 3e 61 4b 65 79 2c 20 70 49 74 65  Iter->aKey, pIte
275c0 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20  r->nKey);.      
275c1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
275c2 56 64 62 65 53 6f 72 74 65 72 4e 65 78 74 28 64  VdbeSorterNext(d
275c3 62 2c 20 70 43 73 72 2c 20 26 62 45 6f 66 29 3b  b, pCsr, &bEof);
275c4 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
275c5 20 20 20 72 63 32 20 3d 20 66 69 6c 65 57 72 69     rc2 = fileWri
275c6 74 65 72 46 69 6e 69 73 68 28 64 62 2c 20 26 77  terFinish(db, &w
275c7 72 69 74 65 72 2c 20 26 69 57 72 69 74 65 32 29  riter, &iWrite2)
275c8 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
275c9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63  ==SQLITE_OK ) rc
275ca 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a   = rc2;.      }.
275cb 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70      }..    if( p
275cc 53 6f 72 74 65 72 2d 3e 6e 50 4d 41 3c 3d 53 4f  Sorter->nPMA<=SO
275cd 52 54 45 52 5f 4d 41 58 5f 4d 45 52 47 45 5f 43  RTER_MAX_MERGE_C
275ce 4f 55 4e 54 20 29 7b 0a 20 20 20 20 20 20 62 72  OUNT ){.      br
275cf 65 61 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  eak;.    }else{.
275d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69        sqlite3_fi
275d1 6c 65 20 2a 70 54 6d 70 20 3d 20 70 53 6f 72 74  le *pTmp = pSort
275d2 65 72 2d 3e 70 54 65 6d 70 31 3b 0a 20 20 20 20  er->pTemp1;.    
275d3 20 20 70 53 6f 72 74 65 72 2d 3e 6e 50 4d 41 20    pSorter->nPMA 
275d4 3d 20 69 4e 65 77 3b 0a 20 20 20 20 20 20 70 53  = iNew;.      pS
275d5 6f 72 74 65 72 2d 3e 70 54 65 6d 70 31 20 3d 20  orter->pTemp1 = 
275d6 70 54 65 6d 70 32 3b 0a 20 20 20 20 20 20 70 54  pTemp2;.      pT
275d7 65 6d 70 32 20 3d 20 70 54 6d 70 3b 0a 20 20 20  emp2 = pTmp;.   
275d8 20 20 20 70 53 6f 72 74 65 72 2d 3e 69 57 72 69     pSorter->iWri
275d9 74 65 4f 66 66 20 3d 20 69 57 72 69 74 65 32 3b  teOff = iWrite2;
275da 0a 20 20 20 20 20 20 70 53 6f 72 74 65 72 2d 3e  .      pSorter->
275db 69 52 65 61 64 4f 66 66 20 3d 20 30 3b 0a 20 20  iReadOff = 0;.  
275dc 20 20 20 20 69 57 72 69 74 65 32 20 3d 20 30 3b      iWrite2 = 0;
275dd 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28  .    }.  }while(
275de 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
275df 3b 0a 0a 20 20 69 66 28 20 70 54 65 6d 70 32 20  ;..  if( pTemp2 
275e0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  ){.    sqlite3Os
275e1 43 6c 6f 73 65 46 72 65 65 28 70 54 65 6d 70 32  CloseFree(pTemp2
275e2 29 3b 0a 20 20 7d 0a 20 20 2a 70 62 45 6f 66 20  );.  }.  *pbEof 
275e3 3d 20 28 70 53 6f 72 74 65 72 2d 3e 61 49 74 65  = (pSorter->aIte
275e4 72 5b 70 53 6f 72 74 65 72 2d 3e 61 54 72 65 65  r[pSorter->aTree
275e5 5b 31 5d 5d 2e 70 46 69 6c 65 3d 3d 30 29 3b 0a  [1]].pFile==0);.
275e6 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
275e7 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 6f  /*.** Advance to
275e8 20 74 68 65 20 6e 65 78 74 20 65 6c 65 6d 65 6e   the next elemen
275e9 74 20 69 6e 20 74 68 65 20 73 6f 72 74 65 72 2e  t in the sorter.
275ea 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
275eb 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
275ec 62 65 53 6f 72 74 65 72 4e 65 78 74 28 73 71 6c  beSorterNext(sql
275ed 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
275ee 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 73 72  VdbeCursor *pCsr
275ef 2c 20 69 6e 74 20 2a 70 62 45 6f 66 29 7b 0a 20  , int *pbEof){. 
275f0 20 56 64 62 65 53 6f 72 74 65 72 20 2a 70 53 6f   VdbeSorter *pSo
275f1 72 74 65 72 20 3d 20 70 43 73 72 2d 3e 70 53 6f  rter = pCsr->pSo
275f2 72 74 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 20  rter;.  int rc; 
275f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
275f4 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
275f5 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69 66 28  n code */..  if(
275f6 20 70 53 6f 72 74 65 72 2d 3e 61 54 72 65 65 20   pSorter->aTree 
275f7 29 7b 0a 20 20 20 20 69 6e 74 20 69 50 72 65 76  ){.    int iPrev
275f8 20 3d 20 70 53 6f 72 74 65 72 2d 3e 61 54 72 65   = pSorter->aTre
275f9 65 5b 31 5d 3b 2f 2a 20 49 6e 64 65 78 20 6f 66  e[1];/* Index of
275fa 20 69 74 65 72 61 74 6f 72 20 74 6f 20 61 64 76   iterator to adv
275fb 61 6e 63 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ance */.    int 
275fc 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
275fd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
275fe 65 78 20 6f 66 20 61 54 72 65 65 5b 5d 20 74 6f  ex of aTree[] to
275ff 20 72 65 63 61 6c 63 75 6c 61 74 65 20 2a 2f 0a   recalculate */.
27600 0a 20 20 20 20 72 63 20 3d 20 76 64 62 65 53 6f  .    rc = vdbeSo
27601 72 74 65 72 49 74 65 72 4e 65 78 74 28 64 62 2c  rterIterNext(db,
27602 20 26 70 53 6f 72 74 65 72 2d 3e 61 49 74 65 72   &pSorter->aIter
27603 5b 69 50 72 65 76 5d 29 3b 0a 20 20 20 20 66 6f  [iPrev]);.    fo
27604 72 28 69 3d 28 70 53 6f 72 74 65 72 2d 3e 6e 54  r(i=(pSorter->nT
27605 72 65 65 2b 69 50 72 65 76 29 2f 32 3b 20 72 63  ree+iPrev)/2; rc
27606 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
27607 3e 30 3b 20 69 3d 69 2f 32 29 7b 0a 20 20 20 20  >0; i=i/2){.    
27608 20 20 72 63 20 3d 20 76 64 62 65 53 6f 72 74 65    rc = vdbeSorte
27609 72 44 6f 43 6f 6d 70 61 72 65 28 70 43 73 72 2c  rDoCompare(pCsr,
2760a 20 69 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20   i);.    }..    
2760b 2a 70 62 45 6f 66 20 3d 20 28 70 53 6f 72 74 65  *pbEof = (pSorte
2760c 72 2d 3e 61 49 74 65 72 5b 70 53 6f 72 74 65 72  r->aIter[pSorter
2760d 2d 3e 61 54 72 65 65 5b 31 5d 5d 2e 70 46 69 6c  ->aTree[1]].pFil
2760e 65 3d 3d 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  e==0);.  }else{.
2760f 20 20 20 20 53 6f 72 74 65 72 52 65 63 6f 72 64      SorterRecord
27610 20 2a 70 46 72 65 65 20 3d 20 70 53 6f 72 74 65   *pFree = pSorte
27611 72 2d 3e 70 52 65 63 6f 72 64 3b 0a 20 20 20 20  r->pRecord;.    
27612 70 53 6f 72 74 65 72 2d 3e 70 52 65 63 6f 72 64  pSorter->pRecord
27613 20 3d 20 70 46 72 65 65 2d 3e 70 4e 65 78 74 3b   = pFree->pNext;
27614 0a 20 20 20 20 70 46 72 65 65 2d 3e 70 4e 65 78  .    pFree->pNex
27615 74 20 3d 20 30 3b 0a 20 20 20 20 76 64 62 65 53  t = 0;.    vdbeS
27616 6f 72 74 65 72 52 65 63 6f 72 64 46 72 65 65 28  orterRecordFree(
27617 64 62 2c 20 70 46 72 65 65 29 3b 0a 20 20 20 20  db, pFree);.    
27618 2a 70 62 45 6f 66 20 3d 20 21 70 53 6f 72 74 65  *pbEof = !pSorte
27619 72 2d 3e 70 52 65 63 6f 72 64 3b 0a 20 20 20 20  r->pRecord;.    
2761a 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
2761b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
2761c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
2761d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
2761e 62 75 66 66 65 72 20 6f 77 6e 65 64 20 62 79 20  buffer owned by 
2761f 74 68 65 20 73 6f 72 74 65 72 20 74 68 61 74 20  the sorter that 
27620 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 0a 2a 2a  contains the .**
27621 20 63 75 72 72 65 6e 74 20 6b 65 79 2e 0a 2a 2f   current key..*/
27622 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 76 64  .static void *vd
27623 62 65 53 6f 72 74 65 72 52 6f 77 6b 65 79 28 0a  beSorterRowkey(.
27624 20 20 63 6f 6e 73 74 20 56 64 62 65 53 6f 72 74    const VdbeSort
27625 65 72 20 2a 70 53 6f 72 74 65 72 2c 20 20 20 20  er *pSorter,    
27626 20 20 2f 2a 20 53 6f 72 74 65 72 20 6f 62 6a 65    /* Sorter obje
27627 63 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4b  ct */.  int *pnK
27628 65 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ey              
27629 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
2762a 53 69 7a 65 20 6f 66 20 63 75 72 72 65 6e 74 20  Size of current 
2762b 6b 65 79 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  key in bytes */.
2762c 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b  ){.  void *pKey;
2762d 0a 20 20 69 66 28 20 70 53 6f 72 74 65 72 2d 3e  .  if( pSorter->
2762e 61 54 72 65 65 20 29 7b 0a 20 20 20 20 56 64 62  aTree ){.    Vdb
2762f 65 53 6f 72 74 65 72 49 74 65 72 20 2a 70 49 74  eSorterIter *pIt
27630 65 72 3b 0a 20 20 20 20 70 49 74 65 72 20 3d 20  er;.    pIter = 
27631 26 70 53 6f 72 74 65 72 2d 3e 61 49 74 65 72 5b  &pSorter->aIter[
27632 20 70 53 6f 72 74 65 72 2d 3e 61 54 72 65 65 5b   pSorter->aTree[
27633 31 5d 20 5d 3b 0a 20 20 20 20 2a 70 6e 4b 65 79  1] ];.    *pnKey
27634 20 3d 20 70 49 74 65 72 2d 3e 6e 4b 65 79 3b 0a   = pIter->nKey;.
27635 20 20 20 20 70 4b 65 79 20 3d 20 70 49 74 65 72      pKey = pIter
27636 2d 3e 61 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 7b  ->aKey;.  }else{
27637 0a 20 20 20 20 2a 70 6e 4b 65 79 20 3d 20 70 53  .    *pnKey = pS
27638 6f 72 74 65 72 2d 3e 70 52 65 63 6f 72 64 2d 3e  orter->pRecord->
27639 6e 56 61 6c 3b 0a 20 20 20 20 70 4b 65 79 20 3d  nVal;.    pKey =
2763a 20 70 53 6f 72 74 65 72 2d 3e 70 52 65 63 6f 72   pSorter->pRecor
2763b 64 2d 3e 70 56 61 6c 3b 0a 20 20 7d 0a 20 20 72  d->pVal;.  }.  r
2763c 65 74 75 72 6e 20 70 4b 65 79 3b 0a 7d 0a 0a 2f  eturn pKey;.}../
2763d 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 63 75  *.** Copy the cu
2763e 72 72 65 6e 74 20 73 6f 72 74 65 72 20 6b 65 79  rrent sorter key
2763f 20 69 6e 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79   into the memory
27640 20 63 65 6c 6c 20 70 4f 75 74 2e 0a 2a 2f 0a 53   cell pOut..*/.S
27641 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
27642 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 6f 72  t sqlite3VdbeSor
27643 74 65 72 52 6f 77 6b 65 79 28 63 6f 6e 73 74 20  terRowkey(const 
27644 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 73 72  VdbeCursor *pCsr
27645 2c 20 4d 65 6d 20 2a 70 4f 75 74 29 7b 0a 20 20  , Mem *pOut){.  
27646 56 64 62 65 53 6f 72 74 65 72 20 2a 70 53 6f 72  VdbeSorter *pSor
27647 74 65 72 20 3d 20 70 43 73 72 2d 3e 70 53 6f 72  ter = pCsr->pSor
27648 74 65 72 3b 0a 20 20 76 6f 69 64 20 2a 70 4b 65  ter;.  void *pKe
27649 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 20 20 20  y; int nKey;    
2764a 20 20 20 20 20 20 20 2f 2a 20 53 6f 72 74 65 72         /* Sorter
2764b 20 6b 65 79 20 74 6f 20 63 6f 70 79 20 69 6e 74   key to copy int
2764c 6f 20 70 4f 75 74 20 2a 2f 0a 0a 20 20 70 4b 65  o pOut */..  pKe
2764d 79 20 3d 20 76 64 62 65 53 6f 72 74 65 72 52 6f  y = vdbeSorterRo
2764e 77 6b 65 79 28 70 53 6f 72 74 65 72 2c 20 26 6e  wkey(pSorter, &n
2764f 4b 65 79 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  Key);.  if( sqli
27650 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
27651 4f 75 74 2c 20 6e 4b 65 79 2c 20 30 29 20 29 7b  Out, nKey, 0) ){
27652 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
27653 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
27654 70 4f 75 74 2d 3e 6e 20 3d 20 6e 4b 65 79 3b 0a  pOut->n = nKey;.
27655 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
27656 28 70 4f 75 74 2c 20 4d 45 4d 5f 42 6c 6f 62 29  (pOut, MEM_Blob)
27657 3b 0a 20 20 6d 65 6d 63 70 79 28 70 4f 75 74 2d  ;.  memcpy(pOut-
27658 3e 7a 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29 3b  >z, pKey, nKey);
27659 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
2765a 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
2765b 6f 6d 70 61 72 65 20 74 68 65 20 6b 65 79 20 69  ompare the key i
2765c 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 70 56  n memory cell pV
2765d 61 6c 20 77 69 74 68 20 74 68 65 20 6b 65 79 20  al with the key 
2765e 74 68 61 74 20 74 68 65 20 73 6f 72 74 65 72 20  that the sorter 
2765f 63 75 72 73 6f 72 0a 2a 2a 20 70 61 73 73 65 64  cursor.** passed
27660 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
27661 67 75 6d 65 6e 74 20 63 75 72 72 65 6e 74 6c 79  gument currently
27662 20 70 6f 69 6e 74 73 20 74 6f 2e 20 46 6f 72 20   points to. For 
27663 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 0a  the purposes of.
27664 2a 2a 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  ** the compariso
27665 6e 2c 20 69 67 6e 6f 72 65 20 74 68 65 20 72 6f  n, ignore the ro
27666 77 69 64 20 66 69 65 6c 64 20 61 74 20 74 68 65  wid field at the
27667 20 65 6e 64 20 6f 66 20 65 61 63 68 20 72 65 63   end of each rec
27668 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ord..**.** If an
27669 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72   error occurs, r
2766a 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65 20  eturn an SQLite 
2766b 65 72 72 6f 72 20 63 6f 64 65 20 28 69 2e 65 2e  error code (i.e.
2766c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 2e 0a   SQLITE_NOMEM)..
2766d 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 65  ** Otherwise, se
2766e 74 20 2a 70 52 65 73 20 74 6f 20 61 20 6e 65 67  t *pRes to a neg
2766f 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70  ative, zero or p
27670 6f 73 69 74 69 76 65 20 76 61 6c 75 65 20 69 66  ositive value if
27671 20 74 68 65 0a 2a 2a 20 6b 65 79 20 69 6e 20 70   the.** key in p
27672 56 61 6c 20 69 73 20 73 6d 61 6c 6c 65 72 20 74  Val is smaller t
27673 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 20 6f 72  han, equal to or
27674 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
27675 20 63 75 72 72 65 6e 74 20 73 6f 72 74 65 72 0a   current sorter.
27676 2a 2a 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c 49 54  ** key..*/.SQLIT
27677 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
27678 6c 69 74 65 33 56 64 62 65 53 6f 72 74 65 72 43  lite3VdbeSorterC
27679 6f 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73 74 20  ompare(.  const 
2767a 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 73 72  VdbeCursor *pCsr
2767b 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 72  ,         /* Sor
2767c 74 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  ter cursor */.  
2767d 4d 65 6d 20 2a 70 56 61 6c 2c 20 20 20 20 20 20  Mem *pVal,      
2767e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2767f 2f 2a 20 56 61 6c 75 65 20 74 6f 20 63 6f 6d 70  /* Value to comp
27680 61 72 65 20 74 6f 20 63 75 72 72 65 6e 74 20 73  are to current s
27681 6f 72 74 65 72 20 6b 65 79 20 2a 2f 0a 20 20 69  orter key */.  i
27682 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 20  nt *pRes        
27683 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27684 2a 20 4f 55 54 3a 20 52 65 73 75 6c 74 20 6f 66  * OUT: Result of
27685 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a 29   comparison */.)
27686 7b 0a 20 20 56 64 62 65 53 6f 72 74 65 72 20 2a  {.  VdbeSorter *
27687 70 53 6f 72 74 65 72 20 3d 20 70 43 73 72 2d 3e  pSorter = pCsr->
27688 70 53 6f 72 74 65 72 3b 0a 20 20 76 6f 69 64 20  pSorter;.  void 
27689 2a 70 4b 65 79 3b 20 69 6e 74 20 6e 4b 65 79 3b  *pKey; int nKey;
2768a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f             /* So
2768b 72 74 65 72 20 6b 65 79 20 74 6f 20 63 6f 6d 70  rter key to comp
2768c 61 72 65 20 70 56 61 6c 20 77 69 74 68 20 2a 2f  are pVal with */
2768d 0a 0a 20 20 70 4b 65 79 20 3d 20 76 64 62 65 53  ..  pKey = vdbeS
2768e 6f 72 74 65 72 52 6f 77 6b 65 79 28 70 53 6f 72  orterRowkey(pSor
2768f 74 65 72 2c 20 26 6e 4b 65 79 29 3b 0a 20 20 76  ter, &nKey);.  v
27690 64 62 65 53 6f 72 74 65 72 43 6f 6d 70 61 72 65  dbeSorterCompare
27691 28 70 43 73 72 2c 20 31 2c 20 70 56 61 6c 2d 3e  (pCsr, 1, pVal->
27692 7a 2c 20 70 56 61 6c 2d 3e 6e 2c 20 70 4b 65 79  z, pVal->n, pKey
27693 2c 20 6e 4b 65 79 2c 20 70 52 65 73 29 3b 0a 20  , nKey, pRes);. 
27694 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
27695 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  K;.}..#endif /* 
27696 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
27697 4d 49 54 5f 4d 45 52 47 45 5f 53 4f 52 54 20 2a  MIT_MERGE_SORT *
27698 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
27699 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 73 6f  ** End of vdbeso
2769a 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  rt.c ***********
2769b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2769c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2769d 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2769e 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6a 6f  ** Begin file jo
2769f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  urnal.c ********
276a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
276a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
276a2 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75  */./*.** 2007 Au
276a3 67 75 73 74 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68  gust 22.**.** Th
276a4 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
276a5 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
276a6 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
276a7 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
276a8 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
276a9 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
276aa 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
276ab 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
276ac 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
276ad 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
276ae 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
276af 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
276b0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
276b1 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
276b2 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
276b3 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
276b4 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
276b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
276b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
276b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
276b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
276b9 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
276ba 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
276bb 73 20 61 20 73 70 65 63 69 61 6c 20 6b 69 6e 64  s a special kind
276bc 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   of sqlite3_file
276bd 20 6f 62 6a 65 63 74 20 75 73 65 64 0a 2a 2a 20   object used.** 
276be 62 79 20 53 51 4c 69 74 65 20 74 6f 20 63 72 65  by SQLite to cre
276bf 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ate journal file
276c0 73 20 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d  s if the atomic-
276c1 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  write optimizati
276c2 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64  on.** is enabled
276c3 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 73 74  ..**.** The dist
276c4 69 6e 63 74 69 76 65 20 63 68 61 72 61 63 74 65  inctive characte
276c5 72 69 73 74 69 63 20 6f 66 20 74 68 69 73 20 73  ristic of this s
276c6 71 6c 69 74 65 33 5f 66 69 6c 65 20 69 73 20 74  qlite3_file is t
276c7 68 61 74 20 74 68 65 0a 2a 2a 20 61 63 74 75 61  hat the.** actua
276c8 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c 65 20 69  l on disk file i
276c9 73 20 63 72 65 61 74 65 64 20 6c 61 7a 69 6c 79  s created lazily
276ca 2e 20 57 68 65 6e 20 74 68 65 20 66 69 6c 65 20  . When the file 
276cb 69 73 20 63 72 65 61 74 65 64 2c 0a 2a 2a 20 74  is created,.** t
276cc 68 65 20 63 61 6c 6c 65 72 20 73 70 65 63 69 66  he caller specif
276cd 69 65 73 20 61 20 62 75 66 66 65 72 20 73 69 7a  ies a buffer siz
276ce 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f  e for an in-memo
276cf 72 79 20 62 75 66 66 65 72 20 74 6f 0a 2a 2a 20  ry buffer to.** 
276d0 62 65 20 75 73 65 64 20 74 6f 20 73 65 72 76 69  be used to servi
276d1 63 65 20 72 65 61 64 28 29 20 61 6e 64 20 77 72  ce read() and wr
276d2 69 74 65 28 29 20 72 65 71 75 65 73 74 73 2e 20  ite() requests. 
276d3 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 0a  The actual file.
276d4 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f  ** on disk is no
276d5 74 20 63 72 65 61 74 65 64 20 6f 72 20 70 6f 70  t created or pop
276d6 75 6c 61 74 65 64 20 75 6e 74 69 6c 20 65 69 74  ulated until eit
276d7 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20  her:.**.**   1) 
276d8 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65  The in-memory re
276d9 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 67 72 6f  presentation gro
276da 77 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72  ws too large for
276db 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 0a   the allocated .
276dc 2a 2a 20 20 20 20 20 20 62 75 66 66 65 72 2c 20  **      buffer, 
276dd 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 73  or.**   2) The s
276de 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65  qlite3JournalCre
276df 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ate() function i
276e0 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66  s called..*/.#if
276e1 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
276e2 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 0a  E_ATOMIC_WRITE..
276e3 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 6e 61 6c  ./*.** A Journal
276e4 46 69 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 61  File object is a
276e5 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c   subclass of sql
276e6 69 74 65 33 5f 66 69 6c 65 20 75 73 65 64 20 62  ite3_file used b
276e7 79 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 65 6e 20  y.** as an open 
276e8 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20  file handle for 
276e9 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a  journal files..*
276ea 2f 0a 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c  /.struct Journal
276eb 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33  File {.  sqlite3
276ec 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65  _io_methods *pMe
276ed 74 68 6f 64 3b 20 20 20 20 2f 2a 20 49 2f 4f 20  thod;    /* I/O 
276ee 6d 65 74 68 6f 64 73 20 6f 6e 20 6a 6f 75 72 6e  methods on journ
276ef 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 69 6e  al files */.  in
276f0 74 20 6e 42 75 66 3b 20 20 20 20 20 20 20 20 20  t nBuf;         
276f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
276f2 20 53 69 7a 65 20 6f 66 20 7a 42 75 66 5b 5d 20   Size of zBuf[] 
276f3 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68  in bytes */.  ch
276f4 61 72 20 2a 7a 42 75 66 3b 20 20 20 20 20 20 20  ar *zBuf;       
276f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
276f6 20 53 70 61 63 65 20 74 6f 20 62 75 66 66 65 72   Space to buffer
276f7 20 6a 6f 75 72 6e 61 6c 20 77 72 69 74 65 73 20   journal writes 
276f8 2a 2f 0a 20 20 69 6e 74 20 69 53 69 7a 65 3b 20  */.  int iSize; 
276f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
276fa 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f       /* Amount o
276fb 66 20 7a 42 75 66 5b 5d 20 63 75 72 72 65 6e 74  f zBuf[] current
276fc 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74  ly used */.  int
276fd 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
276fe 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
276ff 78 4f 70 65 6e 20 66 6c 61 67 73 20 2a 2f 0a 20  xOpen flags */. 
27700 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
27701 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  fs;             
27702 20 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 20 75   /* The "real" u
27703 6e 64 65 72 6c 79 69 6e 67 20 56 46 53 20 2a 2f  nderlying VFS */
27704 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
27705 2a 70 52 65 61 6c 3b 20 20 20 20 20 20 20 20 20  *pReal;         
27706 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c 22     /* The "real"
27707 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65   underlying file
27708 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20   descriptor */. 
27709 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4a 6f   const char *zJo
2770a 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20  urnal;          
2770b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
2770c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
2770d 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  };.typedef struc
2770e 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 4a 6f  t JournalFile Jo
2770f 75 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f 2a 0a 2a  urnalFile;../*.*
27710 2a 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74  * If it does not
27711 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2c   already exists,
27712 20 63 72 65 61 74 65 20 61 6e 64 20 70 6f 70 75   create and popu
27713 6c 61 74 65 20 74 68 65 20 6f 6e 2d 64 69 73 6b  late the on-disk
27714 20 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 20 4a 6f   file .** for Jo
27715 75 72 6e 61 6c 46 69 6c 65 20 70 2e 0a 2a 2f 0a  urnalFile p..*/.
27716 73 74 61 74 69 63 20 69 6e 74 20 63 72 65 61 74  static int creat
27717 65 46 69 6c 65 28 4a 6f 75 72 6e 61 6c 46 69 6c  eFile(JournalFil
27718 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20  e *p){.  int rc 
27719 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
2771a 66 28 20 21 70 2d 3e 70 52 65 61 6c 20 29 7b 0a  f( !p->pReal ){.
2771b 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65      sqlite3_file
2771c 20 2a 70 52 65 61 6c 20 3d 20 28 73 71 6c 69 74   *pReal = (sqlit
2771d 65 33 5f 66 69 6c 65 20 2a 29 26 70 5b 31 5d 3b  e3_file *)&p[1];
2771e 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
2771f 33 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 66 73 2c  3OsOpen(p->pVfs,
27720 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 52   p->zJournal, pR
27721 65 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 2c 20 30  eal, p->flags, 0
27722 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
27723 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
27724 20 20 70 2d 3e 70 52 65 61 6c 20 3d 20 70 52 65    p->pReal = pRe
27725 61 6c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d  al;.      if( p-
27726 3e 69 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20  >iSize>0 ){.    
27727 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e 69 53      assert(p->iS
27728 69 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 3b 0a 20  ize<=p->nBuf);. 
27729 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
2772a 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e 70 52  te3OsWrite(p->pR
2772b 65 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c 20 70 2d  eal, p->zBuf, p-
2772c 3e 69 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20  >iSize, 0);.    
2772d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
2772e 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2772f 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69  .** Close the fi
27730 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
27731 74 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69  t jrnlClose(sqli
27732 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b  te3_file *pJfd){
27733 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  .  JournalFile *
27734 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65  p = (JournalFile
27735 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70   *)pJfd;.  if( p
27736 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 73  ->pReal ){.    s
27737 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 2d  qlite3OsClose(p-
27738 3e 70 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 73  >pReal);.  }.  s
27739 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a  qlite3_free(p->z
2773a 42 75 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  Buf);.  return S
2773b 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
2773c 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f  ** Read data fro
2773d 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73  m the file..*/.s
2773e 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 52 65  tatic int jrnlRe
2773f 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  ad(.  sqlite3_fi
27740 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20  le *pJfd,    /* 
27741 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  The journal file
27742 20 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 72   from which to r
27743 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a  ead */.  void *z
27744 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  Buf,            
27745 2f 2a 20 50 75 74 20 74 68 65 20 72 65 73 75 6c  /* Put the resul
27746 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  ts here */.  int
27747 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20   iAmt,          
27748 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
27749 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a   bytes to read *
2774a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  /.  sqlite_int64
2774b 20 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65   iOfst     /* Be
2774c 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 74  gin reading at t
2774d 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b  his offset */.){
2774e 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
2774f 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c  TE_OK;.  Journal
27750 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e  File *p = (Journ
27751 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20  alFile *)pJfd;. 
27752 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b   if( p->pReal ){
27753 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
27754 33 4f 73 52 65 61 64 28 70 2d 3e 70 52 65 61 6c  3OsRead(p->pReal
27755 2c 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f  , zBuf, iAmt, iO
27756 66 73 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  fst);.  }else if
27757 28 20 28 69 41 6d 74 2b 69 4f 66 73 74 29 3e 70  ( (iAmt+iOfst)>p
27758 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 72  ->iSize ){.    r
27759 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
2775a 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d  _SHORT_READ;.  }
2775b 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 70 79  else{.    memcpy
2775c 28 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 75 66 5b  (zBuf, &p->zBuf[
2775d 69 4f 66 73 74 5d 2c 20 69 41 6d 74 29 3b 0a 20  iOfst], iAmt);. 
2775e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
2775f 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64  }../*.** Write d
27760 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 2e  ata to the file.
27761 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a  .*/.static int j
27762 72 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 6c 69  rnlWrite(.  sqli
27763 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20  te3_file *pJfd, 
27764 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61     /* The journa
27765 6c 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 69 63  l file into whic
27766 68 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20  h to write */.  
27767 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66  const void *zBuf
27768 2c 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 20 64  ,      /* Take d
27769 61 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65  ata to be writte
2776a 6e 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 20  n from here */. 
2776b 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20   int iAmt,      
2776c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2776d 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72  r of bytes to wr
2776e 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f  ite */.  sqlite_
2776f 69 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20  int64 iOfst     
27770 2f 2a 20 42 65 67 69 6e 20 77 72 69 74 69 6e 67  /* Begin writing
27771 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20   at this offset 
27772 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f  into the file */
27773 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
27774 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72  QLITE_OK;.  Jour
27775 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f  nalFile *p = (Jo
27776 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64  urnalFile *)pJfd
27777 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61  ;.  if( !p->pRea
27778 6c 20 26 26 20 28 69 4f 66 73 74 2b 69 41 6d 74  l && (iOfst+iAmt
27779 29 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a 20 20 20  )>p->nBuf ){.   
2777a 20 72 63 20 3d 20 63 72 65 61 74 65 46 69 6c 65   rc = createFile
2777b 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  (p);.  }.  if( r
2777c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
2777d 20 20 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c      if( p->pReal
2777e 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
2777f 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d  qlite3OsWrite(p-
27780 3e 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41  >pReal, zBuf, iA
27781 6d 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 20 20  mt, iOfst);.    
27782 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d  }else{.      mem
27783 63 70 79 28 26 70 2d 3e 7a 42 75 66 5b 69 4f 66  cpy(&p->zBuf[iOf
27784 73 74 5d 2c 20 7a 42 75 66 2c 20 69 41 6d 74 29  st], zBuf, iAmt)
27785 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69  ;.      if( p->i
27786 53 69 7a 65 3c 28 69 4f 66 73 74 2b 69 41 6d 74  Size<(iOfst+iAmt
27787 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  ) ){.        p->
27788 69 53 69 7a 65 20 3d 20 28 69 4f 66 73 74 2b 69  iSize = (iOfst+i
27789 41 6d 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Amt);.      }.  
2778a 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2778b 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72   rc;.}../*.** Tr
2778c 75 6e 63 61 74 65 20 74 68 65 20 66 69 6c 65 2e  uncate the file.
2778d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a  .*/.static int j
2778e 72 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69  rnlTruncate(sqli
2778f 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20  te3_file *pJfd, 
27790 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a  sqlite_int64 siz
27791 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
27792 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72  QLITE_OK;.  Jour
27793 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f  nalFile *p = (Jo
27794 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64  urnalFile *)pJfd
27795 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c  ;.  if( p->pReal
27796 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
27797 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70  ite3OsTruncate(p
27798 2d 3e 70 52 65 61 6c 2c 20 73 69 7a 65 29 3b 0a  ->pReal, size);.
27799 20 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a 65    }else if( size
2779a 3c 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20  <p->iSize ){.   
2779b 20 70 2d 3e 69 53 69 7a 65 20 3d 20 73 69 7a 65   p->iSize = size
2779c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
2779d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63  c;.}../*.** Sync
2779e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74   the file..*/.st
2779f 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 53 79 6e  atic int jrnlSyn
277a0 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  c(sqlite3_file *
277a1 70 4a 66 64 2c 20 69 6e 74 20 66 6c 61 67 73 29  pJfd, int flags)
277a2 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4a 6f  {.  int rc;.  Jo
277a3 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28  urnalFile *p = (
277a4 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a  JournalFile *)pJ
277a5 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65  fd;.  if( p->pRe
277a6 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  al ){.    rc = s
277a7 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 2d 3e  qlite3OsSync(p->
277a8 70 52 65 61 6c 2c 20 66 6c 61 67 73 29 3b 0a 20  pReal, flags);. 
277a9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
277aa 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
277ab 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
277ac 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20  /*.** Query the 
277ad 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
277ae 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74   in bytes..*/.st
277af 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 46 69 6c  atic int jrnlFil
277b0 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  eSize(sqlite3_fi
277b1 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65  le *pJfd, sqlite
277b2 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a  _int64 *pSize){.
277b3 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
277b4 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46  E_OK;.  JournalF
277b5 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61  ile *p = (Journa
277b6 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20  lFile *)pJfd;.  
277b7 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a  if( p->pReal ){.
277b8 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
277b9 4f 73 46 69 6c 65 53 69 7a 65 28 70 2d 3e 70 52  OsFileSize(p->pR
277ba 65 61 6c 2c 20 70 53 69 7a 65 29 3b 0a 20 20 7d  eal, pSize);.  }
277bb 65 6c 73 65 7b 0a 20 20 20 20 2a 70 53 69 7a 65  else{.    *pSize
277bc 20 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34   = (sqlite_int64
277bd 29 20 70 2d 3e 69 53 69 7a 65 3b 0a 20 20 7d 0a  ) p->iSize;.  }.
277be 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
277bf 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f 66 20 6d  /*.** Table of m
277c0 65 74 68 6f 64 73 20 66 6f 72 20 4a 6f 75 72 6e  ethods for Journ
277c1 61 6c 46 69 6c 65 20 73 71 6c 69 74 65 33 5f 66  alFile sqlite3_f
277c2 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73  ile object..*/.s
277c3 74 61 74 69 63 20 73 74 72 75 63 74 20 73 71 6c  tatic struct sql
277c4 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
277c5 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f  JournalFileMetho
277c6 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20  ds = {.  1,     
277c7 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
277c8 69 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c 43 6c 6f  ion */.  jrnlClo
277c9 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73  se,     /* xClos
277ca 65 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65 61 64 2c  e */.  jrnlRead,
277cb 20 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a        /* xRead *
277cc 2f 0a 20 20 6a 72 6e 6c 57 72 69 74 65 2c 20 20  /.  jrnlWrite,  
277cd 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a     /* xWrite */.
277ce 20 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20    jrnlTruncate, 
277cf 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f   /* xTruncate */
277d0 0a 20 20 6a 72 6e 6c 53 79 6e 63 2c 20 20 20 20  .  jrnlSync,    
277d1 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a 20 20    /* xSync */.  
277d2 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f  jrnlFileSize,  /
277d3 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20  * xFileSize */. 
277d4 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
277d5 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c  /* xLock */.  0,
277d6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
277d7 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20  xUnlock */.  0, 
277d8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
277d9 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
277da 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  k */.  0,       
277db 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f        /* xFileCo
277dc 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 20 20  ntrol */.  0,   
277dd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65            /* xSe
277de 63 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 2c  ctorSize */.  0,
277df 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
277e0 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  xDeviceCharacter
277e1 69 73 74 69 63 73 20 2a 2f 0a 20 20 30 2c 20 20  istics */.  0,  
277e2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
277e3 68 6d 4d 61 70 20 2a 2f 0a 20 20 30 2c 20 20 20  hmMap */.  0,   
277e4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68            /* xSh
277e5 6d 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20  mLock */.  0,   
277e6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68            /* xSh
277e7 6d 42 61 72 72 69 65 72 20 2a 2f 0a 20 20 30 20  mBarrier */.  0 
277e8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
277e9 78 53 68 6d 55 6e 6d 61 70 20 2a 2f 0a 7d 3b 0a  xShmUnmap */.};.
277ea 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a  ./* .** Open a j
277eb 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a  ournal file..*/.
277ec 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
277ed 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  nt sqlite3Journa
277ee 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  lOpen(.  sqlite3
277ef 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
277f0 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74      /* The VFS t
277f1 6f 20 75 73 65 20 66 6f 72 20 61 63 74 75 61 6c  o use for actual
277f2 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 63   file I/O */.  c
277f3 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
277f4 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ,         /* Nam
277f5 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
277f6 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74   file */.  sqlit
277f7 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20  e3_file *pJfd,  
277f8 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f        /* Preallo
277f9 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66 69 6c  cated, blank fil
277fa 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e  e handle */.  in
277fb 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
277fc 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e           /* Open
277fd 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69  ing flags */.  i
277fe 6e 74 20 6e 42 75 66 20 20 20 20 20 20 20 20 20  nt nBuf         
277ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
27800 65 73 20 62 75 66 66 65 72 65 64 20 62 65 66 6f  es buffered befo
27801 72 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66  re opening the f
27802 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72  ile */.){.  Jour
27803 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f  nalFile *p = (Jo
27804 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64  urnalFile *)pJfd
27805 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  ;.  memset(p, 0,
27806 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53   sqlite3JournalS
27807 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 69 66  ize(pVfs));.  if
27808 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20 20 20  ( nBuf>0 ){.    
27809 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69 74 65  p->zBuf = sqlite
2780a 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 75 66  3MallocZero(nBuf
2780b 29 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 7a  );.    if( !p->z
2780c 42 75 66 20 29 7b 0a 20 20 20 20 20 20 72 65 74  Buf ){.      ret
2780d 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
2780e 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
2780f 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
27810 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20  te3OsOpen(pVfs, 
27811 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66 6c 61  zName, pJfd, fla
27812 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 2d  gs, 0);.  }.  p-
27813 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f 75 72  >pMethod = &Jour
27814 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 3b 0a  nalFileMethods;.
27815 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42 75 66    p->nBuf = nBuf
27816 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 66  ;.  p->flags = f
27817 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72  lags;.  p->zJour
27818 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70  nal = zName;.  p
27819 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20  ->pVfs = pVfs;. 
2781a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
2781b 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  K;.}../*.** If t
2781c 68 65 20 61 72 67 75 6d 65 6e 74 20 70 20 70 6f  he argument p po
2781d 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72 6e 61  ints to a Journa
2781e 6c 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 2c  lFile structure,
2781f 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79   and the underly
27820 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61 73 20  ing.** file has 
27821 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 72 65  not yet been cre
27822 61 74 65 64 2c 20 63 72 65 61 74 65 20 69 74 20  ated, create it 
27823 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  now..*/.SQLITE_P
27824 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
27825 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28  e3JournalCreate(
27826 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 29  sqlite3_file *p)
27827 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65 74 68  {.  if( p->pMeth
27828 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c  ods!=&JournalFil
27829 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  eMethods ){.    
2782a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2782b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63  ;.  }.  return c
2782c 72 65 61 74 65 46 69 6c 65 28 28 4a 6f 75 72 6e  reateFile((Journ
2782d 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a  alFile *)p);.}..
2782e 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 2d 68  /*.** The file-h
2782f 61 6e 64 6c 65 20 70 61 73 73 65 64 20 61 73 20  andle passed as 
27830 74 68 65 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e  the only argumen
27831 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  t is guaranteed 
27832 74 6f 20 62 65 20 61 6e 20 6f 70 65 6e 0a 2a 2a  to be an open.**
27833 20 66 69 6c 65 2e 20 49 74 20 6d 61 79 20 6f 72   file. It may or
27834 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f 66 20 63   may not be of c
27835 6c 61 73 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  lass JournalFile
27836 2e 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73  . If the file is
27837 20 61 0a 2a 2a 20 4a 6f 75 72 6e 61 6c 46 69 6c   a.** JournalFil
27838 65 2c 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72  e, and the under
27839 6c 79 69 6e 67 20 66 69 6c 65 20 6f 6e 20 64 69  lying file on di
2783a 73 6b 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62  sk has not yet b
2783b 65 65 6e 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 72  een opened,.** r
2783c 65 74 75 72 6e 20 30 2e 20 4f 74 68 65 72 77 69  eturn 0. Otherwi
2783d 73 65 2c 20 72 65 74 75 72 6e 20 31 2e 0a 2a 2f  se, return 1..*/
2783e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2783f 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e  int sqlite3Journ
27840 61 6c 45 78 69 73 74 73 28 73 71 6c 69 74 65 33  alExists(sqlite3
27841 5f 66 69 6c 65 20 2a 70 29 7b 0a 20 20 72 65 74  _file *p){.  ret
27842 75 72 6e 20 28 70 2d 3e 70 4d 65 74 68 6f 64 73  urn (p->pMethods
27843 21 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65  !=&JournalFileMe
27844 74 68 6f 64 73 20 7c 7c 20 28 28 4a 6f 75 72 6e  thods || ((Journ
27845 61 6c 46 69 6c 65 20 2a 29 70 29 2d 3e 70 52 65  alFile *)p)->pRe
27846 61 6c 21 3d 30 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  al!=0);.}../* .*
27847 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
27848 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71  ber of bytes req
27849 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 61  uired to store a
2784a 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 74 68 61   JournalFile tha
2784b 74 20 75 73 65 73 20 76 66 73 0a 2a 2a 20 70 56  t uses vfs.** pV
2784c 66 73 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  fs to create the
2784d 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64   underlying on-d
2784e 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51  isk files..*/.SQ
2784f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
27850 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53   sqlite3JournalS
27851 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ize(sqlite3_vfs 
27852 2a 70 56 66 73 29 7b 0a 20 20 72 65 74 75 72 6e  *pVfs){.  return
27853 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65   (pVfs->szOsFile
27854 2b 73 69 7a 65 6f 66 28 4a 6f 75 72 6e 61 6c 46  +sizeof(JournalF
27855 69 6c 65 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  ile));.}.#endif.
27856 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
27857 20 45 6e 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 2e   End of journal.
27858 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
27859 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2785a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2785b 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2785c 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 6a   Begin file memj
2785d 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a  ournal.c *******
2785e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2785f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
27860 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f  ./*.** 2008 Octo
27861 62 65 72 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ber 7.**.** The 
27862 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
27863 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
27864 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
27865 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
27866 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
27867 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
27868 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
27869 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2786a 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2786b 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2786c 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2786d 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2786e 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2786f 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
27870 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
27871 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
27872 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
27873 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27874 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27875 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27876 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27877 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
27878 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
27879 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65 6d  de use to implem
2787a 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  ent an in-memory
2787b 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
2787c 6c 2e 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d  l..** The in-mem
2787d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  ory rollback jou
2787e 72 6e 61 6c 20 69 73 20 75 73 65 64 20 74 6f 20  rnal is used to 
2787f 6a 6f 75 72 6e 61 6c 20 74 72 61 6e 73 61 63 74  journal transact
27880 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 22 3a 6d 65  ions for.** ":me
27881 6d 6f 72 79 3a 22 20 64 61 74 61 62 61 73 65 73  mory:" databases
27882 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20 6a 6f   and when the jo
27883 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d 45 4d 4f 52  urnal_mode=MEMOR
27884 59 20 70 72 61 67 6d 61 20 69 73 20 75 73 65 64  Y pragma is used
27885 2e 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  ..*/../* Forward
27886 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 69   references to i
27887 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75 72  nternal structur
27888 65 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  es */.typedef st
27889 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  ruct MemJournal 
2788a 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a 74 79 70 65  MemJournal;.type
2788b 64 65 66 20 73 74 72 75 63 74 20 46 69 6c 65 50  def struct FileP
2788c 6f 69 6e 74 20 46 69 6c 65 50 6f 69 6e 74 3b 0a  oint FilePoint;.
2788d 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46  typedef struct F
2788e 69 6c 65 43 68 75 6e 6b 20 46 69 6c 65 43 68 75  ileChunk FileChu
2788f 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63 65 20 74 6f  nk;../* Space to
27890 20 68 6f 6c 64 20 74 68 65 20 72 6f 6c 6c 62 61   hold the rollba
27891 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c  ck journal is al
27892 6c 6f 63 61 74 65 64 20 69 6e 20 69 6e 63 72 65  located in incre
27893 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 69 73  ments of.** this
27894 20 6d 61 6e 79 20 62 79 74 65 73 2e 0a 2a 2a 0a   many bytes..**.
27895 2a 2a 20 54 68 65 20 73 69 7a 65 20 63 68 6f 73  ** The size chos
27896 65 6e 20 69 73 20 61 20 6c 69 74 74 6c 65 20 6c  en is a little l
27897 65 73 73 20 74 68 61 6e 20 61 20 70 6f 77 65 72  ess than a power
27898 20 6f 66 20 74 77 6f 2e 20 20 54 68 61 74 20 77   of two.  That w
27899 61 79 2c 0a 2a 2a 20 74 68 65 20 46 69 6c 65 43  ay,.** the FileC
2789a 68 75 6e 6b 20 6f 62 6a 65 63 74 20 77 69 6c 6c  hunk object will
2789b 20 68 61 76 65 20 61 20 73 69 7a 65 20 74 68 61   have a size tha
2789c 74 20 61 6c 6d 6f 73 74 20 65 78 61 63 74 6c 79  t almost exactly
2789d 20 66 69 6c 6c 73 0a 2a 2a 20 61 20 70 6f 77 65   fills.** a powe
2789e 72 2d 6f 66 2d 74 77 6f 20 61 6c 6c 6f 63 61 74  r-of-two allocat
2789f 69 6f 6e 2e 20 20 54 68 69 73 20 6d 69 6d 69 6d  ion.  This mimim
278a0 69 7a 65 73 20 77 61 73 74 65 64 20 73 70 61 63  izes wasted spac
278a1 65 20 69 6e 20 70 6f 77 65 72 2d 6f 66 2d 74 77  e in power-of-tw
278a2 6f 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  o.** memory allo
278a3 63 61 74 6f 72 73 2e 0a 2a 2f 0a 23 64 65 66 69  cators..*/.#defi
278a4 6e 65 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b  ne JOURNAL_CHUNK
278a5 53 49 5a 45 20 28 28 69 6e 74 29 28 31 30 32 34  SIZE ((int)(1024
278a6 2d 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75 6e  -sizeof(FileChun
278a7 6b 2a 29 29 29 0a 0a 2f 2a 20 4d 61 63 72 6f 20  k*)))../* Macro 
278a8 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 6e 69  to find the mini
278a9 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d 65 72  mum of two numer
278aa 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 69  ic values..*/.#i
278ab 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65 66 69  fndef MIN.# defi
278ac 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 78 29  ne MIN(x,y) ((x)
278ad 3c 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 23 65  <(y)?(x):(y)).#e
278ae 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
278af 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
278b0 20 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20   is composed of 
278b1 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  a linked list of
278b2 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65   these structure
278b3 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c  s..*/.struct Fil
278b4 65 43 68 75 6e 6b 20 7b 0a 20 20 46 69 6c 65 43  eChunk {.  FileC
278b5 68 75 6e 6b 20 2a 70 4e 65 78 74 3b 20 20 20 20  hunk *pNext;    
278b6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
278b7 78 74 20 63 68 75 6e 6b 20 69 6e 20 74 68 65 20  xt chunk in the 
278b8 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20  journal */.  u8 
278b9 7a 43 68 75 6e 6b 5b 4a 4f 55 52 4e 41 4c 5f 43  zChunk[JOURNAL_C
278ba 48 55 4e 4b 53 49 5a 45 5d 3b 20 20 20 2f 2a 20  HUNKSIZE];   /* 
278bb 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20  Content of this 
278bc 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  chunk */.};../*.
278bd 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
278be 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 65  f this object se
278bf 72 76 65 73 20 61 73 20 61 20 63 75 72 73 6f 72  rves as a cursor
278c0 20 69 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61   into the rollba
278c1 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54  ck journal..** T
278c2 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 65  he cursor can be
278c3 20 65 69 74 68 65 72 20 66 6f 72 20 72 65 61 64   either for read
278c4 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 2e 0a  ing or writing..
278c5 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c 65 50 6f  */.struct FilePo
278c6 69 6e 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  int {.  sqlite3_
278c7 69 6e 74 36 34 20 69 4f 66 66 73 65 74 3b 20 20  int64 iOffset;  
278c8 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
278c9 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e  t from the begin
278ca 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c 65  ning of the file
278cb 20 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20   */.  FileChunk 
278cc 2a 70 43 68 75 6e 6b 3b 20 20 20 20 20 20 20 20  *pChunk;        
278cd 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 69        /* Specifi
278ce 63 20 63 68 75 6e 6b 20 69 6e 74 6f 20 77 68 69  c chunk into whi
278cf 63 68 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73  ch cursor points
278d0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
278d1 69 73 20 73 75 62 63 6c 61 73 73 20 69 73 20 61  is subclass is a
278d2 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c   subclass of sql
278d3 69 74 65 33 5f 66 69 6c 65 2e 20 20 45 61 63 68  ite3_file.  Each
278d4 20 6f 70 65 6e 20 6d 65 6d 6f 72 79 2d 6a 6f 75   open memory-jou
278d5 72 6e 61 6c 0a 2a 2a 20 69 73 20 61 6e 20 69 6e  rnal.** is an in
278d6 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 63  stance of this c
278d7 6c 61 73 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  lass..*/.struct 
278d8 4d 65 6d 4a 6f 75 72 6e 61 6c 20 7b 0a 20 20 73  MemJournal {.  s
278d9 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
278da 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f  s *pMethod;    /
278db 2a 20 50 61 72 65 6e 74 20 63 6c 61 73 73 2e 20  * Parent class. 
278dc 4d 55 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f  MUST BE FIRST */
278dd 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 46  .  FileChunk *pF
278de 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  irst;           
278df 20 20 20 2f 2a 20 48 65 61 64 20 6f 66 20 69 6e     /* Head of in
278e0 2d 6d 65 6d 6f 72 79 20 63 68 75 6e 6b 2d 6c 69  -memory chunk-li
278e1 73 74 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 6e  st */.  FilePoin
278e2 74 20 65 6e 64 70 6f 69 6e 74 3b 20 20 20 20 20  t endpoint;     
278e3 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
278e4 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  er to the end of
278e5 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 46   the file */.  F
278e6 69 6c 65 50 6f 69 6e 74 20 72 65 61 64 70 6f 69  ilePoint readpoi
278e7 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  nt;            /
278e8 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
278e9 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74   end of the last
278ea 20 78 52 65 61 64 28 29 20 2a 2f 0a 7d 3b 0a 0a   xRead() */.};..
278eb 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20  /*.** Read data 
278ec 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  from the in-memo
278ed 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  ry journal file.
278ee 20 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d    This is the im
278ef 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20  plementation.** 
278f0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  of the sqlite3_v
278f1 66 73 2e 78 52 65 61 64 20 6d 65 74 68 6f 64 2e  fs.xRead method.
278f2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
278f3 65 6d 6a 72 6e 6c 52 65 61 64 28 0a 20 20 73 71  emjrnlRead(.  sq
278f4 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
278f5 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72  ,    /* The jour
278f6 6e 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20 77 68  nal file from wh
278f7 69 63 68 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20  ich to read */. 
278f8 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20   void *zBuf,    
278f9 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20 74          /* Put t
278fa 68 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  he results here 
278fb 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20  */.  int iAmt,  
278fc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
278fd 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
278fe 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69  o read */.  sqli
278ff 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 20  te_int64 iOfst  
27900 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64     /* Begin read
27901 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73  ing at this offs
27902 65 74 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f  et */.){.  MemJo
27903 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a  urnal *p = (MemJ
27904 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20  ournal *)pJfd;. 
27905 20 75 38 20 2a 7a 4f 75 74 20 3d 20 7a 42 75 66   u8 *zOut = zBuf
27906 3b 0a 20 20 69 6e 74 20 6e 52 65 61 64 20 3d 20  ;.  int nRead = 
27907 69 41 6d 74 3b 0a 20 20 69 6e 74 20 69 43 68 75  iAmt;.  int iChu
27908 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 46 69 6c 65  nkOffset;.  File
27909 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a 0a  Chunk *pChunk;..
2790a 20 20 2f 2a 20 53 51 4c 69 74 65 20 6e 65 76 65    /* SQLite neve
2790b 72 20 74 72 69 65 73 20 74 6f 20 72 65 61 64 20  r tries to read 
2790c 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
2790d 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  a rollback journ
2790e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73 73  al file */.  ass
2790f 65 72 74 28 20 69 4f 66 73 74 2b 69 41 6d 74 3c  ert( iOfst+iAmt<
27910 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66  =p->endpoint.iOf
27911 66 73 65 74 20 29 3b 0a 0a 20 20 69 66 28 20 70  fset );..  if( p
27912 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66  ->readpoint.iOff
27913 73 65 74 21 3d 69 4f 66 73 74 20 7c 7c 20 69 4f  set!=iOfst || iO
27914 66 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  fst==0 ){.    sq
27915 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66  lite3_int64 iOff
27916 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 43   = 0;.    for(pC
27917 68 75 6e 6b 3d 70 2d 3e 70 46 69 72 73 74 3b 20  hunk=p->pFirst; 
27918 0a 20 20 20 20 20 20 20 20 41 4c 57 41 59 53 28  .        ALWAYS(
27919 70 43 68 75 6e 6b 29 20 26 26 20 28 69 4f 66 66  pChunk) && (iOff
2791a 2b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49  +JOURNAL_CHUNKSI
2791b 5a 45 29 3c 3d 69 4f 66 73 74 3b 0a 20 20 20 20  ZE)<=iOfst;.    
2791c 20 20 20 20 70 43 68 75 6e 6b 3d 70 43 68 75 6e      pChunk=pChun
2791d 6b 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29 7b 0a  k->pNext.    ){.
2791e 20 20 20 20 20 20 69 4f 66 66 20 2b 3d 20 4a 4f        iOff += JO
2791f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 3b  URNAL_CHUNKSIZE;
27920 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
27921 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 2d 3e      pChunk = p->
27922 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b  readpoint.pChunk
27923 3b 0a 20 20 7d 0a 0a 20 20 69 43 68 75 6e 6b 4f  ;.  }..  iChunkO
27924 66 66 73 65 74 20 3d 20 28 69 6e 74 29 28 69 4f  ffset = (int)(iO
27925 66 73 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e  fst%JOURNAL_CHUN
27926 4b 53 49 5a 45 29 3b 0a 20 20 64 6f 20 7b 0a 20  KSIZE);.  do {. 
27927 20 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d 20     int iSpace = 
27928 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a  JOURNAL_CHUNKSIZ
27929 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 74  E - iChunkOffset
2792a 3b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 20  ;.    int nCopy 
2792b 3d 20 4d 49 4e 28 6e 52 65 61 64 2c 20 28 4a 4f  = MIN(nRead, (JO
2792c 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20  URNAL_CHUNKSIZE 
2792d 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 29  - iChunkOffset))
2792e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75  ;.    memcpy(zOu
2792f 74 2c 20 26 70 43 68 75 6e 6b 2d 3e 7a 43 68 75  t, &pChunk->zChu
27930 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 5d  nk[iChunkOffset]
27931 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 7a 4f  , nCopy);.    zO
27932 75 74 20 2b 3d 20 6e 43 6f 70 79 3b 0a 20 20 20  ut += nCopy;.   
27933 20 6e 52 65 61 64 20 2d 3d 20 69 53 70 61 63 65   nRead -= iSpace
27934 3b 0a 20 20 20 20 69 43 68 75 6e 6b 4f 66 66 73  ;.    iChunkOffs
27935 65 74 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69 6c  et = 0;.  } whil
27936 65 28 20 6e 52 65 61 64 3e 3d 30 20 26 26 20 28  e( nRead>=0 && (
27937 70 43 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70  pChunk=pChunk->p
27938 4e 65 78 74 29 21 3d 30 20 26 26 20 6e 52 65 61  Next)!=0 && nRea
27939 64 3e 30 20 29 3b 0a 20 20 70 2d 3e 72 65 61 64  d>0 );.  p->read
2793a 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 3d 20  point.iOffset = 
2793b 69 4f 66 73 74 2b 69 41 6d 74 3b 0a 20 20 70 2d  iOfst+iAmt;.  p-
2793c 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 6e  >readpoint.pChun
2793d 6b 20 3d 20 70 43 68 75 6e 6b 3b 0a 0a 20 20 72  k = pChunk;..  r
2793e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2793f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
27940 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65  data to the file
27941 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
27942 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 28 0a 20 20  memjrnlWrite(.  
27943 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
27944 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f  fd,    /* The jo
27945 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20  urnal file into 
27946 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a  which to write *
27947 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
27948 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61  zBuf,      /* Ta
27949 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 72  ke data to be wr
2794a 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20  itten from here 
2794b 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20  */.  int iAmt,  
2794c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2794d 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
2794e 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c  o write */.  sql
2794f 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20  ite_int64 iOfst 
27950 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 69      /* Begin wri
27951 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66  ting at this off
27952 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c  set into the fil
27953 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75  e */.){.  MemJou
27954 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f  rnal *p = (MemJo
27955 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20  urnal *)pJfd;.  
27956 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 69 41 6d  int nWrite = iAm
27957 74 3b 0a 20 20 75 38 20 2a 7a 57 72 69 74 65 20  t;.  u8 *zWrite 
27958 3d 20 28 75 38 20 2a 29 7a 42 75 66 3b 0a 0a 20  = (u8 *)zBuf;.. 
27959 20 2f 2a 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79   /* An in-memory
2795a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 68   journal file sh
2795b 6f 75 6c 64 20 6f 6e 6c 79 20 65 76 65 72 20 62  ould only ever b
2795c 65 20 61 70 70 65 6e 64 65 64 20 74 6f 2e 20 52  e appended to. R
2795d 61 6e 64 6f 6d 0a 20 20 2a 2a 20 61 63 63 65 73  andom.  ** acces
2795e 73 20 77 72 69 74 65 73 20 61 72 65 20 6e 6f 74  s writes are not
2795f 20 72 65 71 75 69 72 65 64 20 62 79 20 73 71 6c   required by sql
27960 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ite..  */.  asse
27961 72 74 28 20 69 4f 66 73 74 3d 3d 70 2d 3e 65 6e  rt( iOfst==p->en
27962 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 29  dpoint.iOffset )
27963 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
27964 45 54 45 52 28 69 4f 66 73 74 29 3b 0a 0a 20 20  ETER(iOfst);..  
27965 77 68 69 6c 65 28 20 6e 57 72 69 74 65 3e 30 20  while( nWrite>0 
27966 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b  ){.    FileChunk
27967 20 2a 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 65 6e   *pChunk = p->en
27968 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20  dpoint.pChunk;. 
27969 20 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66     int iChunkOff
2796a 73 65 74 20 3d 20 28 69 6e 74 29 28 70 2d 3e 65  set = (int)(p->e
2796b 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 25  ndpoint.iOffset%
2796c 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a  JOURNAL_CHUNKSIZ
2796d 45 29 3b 0a 20 20 20 20 69 6e 74 20 69 53 70 61  E);.    int iSpa
2796e 63 65 20 3d 20 4d 49 4e 28 6e 57 72 69 74 65 2c  ce = MIN(nWrite,
2796f 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49   JOURNAL_CHUNKSI
27970 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65  ZE - iChunkOffse
27971 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 43 68  t);..    if( iCh
27972 75 6e 6b 4f 66 66 73 65 74 3d 3d 30 20 29 7b 0a  unkOffset==0 ){.
27973 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 68 75        /* New chu
27974 6e 6b 20 69 73 20 72 65 71 75 69 72 65 64 20 74  nk is required t
27975 6f 20 65 78 74 65 6e 64 20 74 68 65 20 66 69 6c  o extend the fil
27976 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46 69 6c 65  e. */.      File
27977 43 68 75 6e 6b 20 2a 70 4e 65 77 20 3d 20 73 71  Chunk *pNew = sq
27978 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
27979 65 6f 66 28 46 69 6c 65 43 68 75 6e 6b 29 29 3b  eof(FileChunk));
2797a 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77  .      if( !pNew
2797b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
2797c 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
2797d 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
2797e 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74       pNew->pNext
2797f 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20   = 0;.      if( 
27980 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20  pChunk ){.      
27981 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 46 69    assert( p->pFi
27982 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 70  rst );.        p
27983 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20 70  Chunk->pNext = p
27984 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  New;.      }else
27985 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
27986 28 20 21 70 2d 3e 70 46 69 72 73 74 20 29 3b 0a  ( !p->pFirst );.
27987 20 20 20 20 20 20 20 20 70 2d 3e 70 46 69 72 73          p->pFirs
27988 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20  t = pNew;.      
27989 7d 0a 20 20 20 20 20 20 70 2d 3e 65 6e 64 70 6f  }.      p->endpo
2798a 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 4e 65  int.pChunk = pNe
2798b 77 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65  w;.    }..    me
2798c 6d 63 70 79 28 26 70 2d 3e 65 6e 64 70 6f 69 6e  mcpy(&p->endpoin
2798d 74 2e 70 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e 6b  t.pChunk->zChunk
2798e 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 5d 2c 20  [iChunkOffset], 
2798f 7a 57 72 69 74 65 2c 20 69 53 70 61 63 65 29 3b  zWrite, iSpace);
27990 0a 20 20 20 20 7a 57 72 69 74 65 20 2b 3d 20 69  .    zWrite += i
27991 53 70 61 63 65 3b 0a 20 20 20 20 6e 57 72 69 74  Space;.    nWrit
27992 65 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20 20  e -= iSpace;.   
27993 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66   p->endpoint.iOf
27994 66 73 65 74 20 2b 3d 20 69 53 70 61 63 65 3b 0a  fset += iSpace;.
27995 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51    }..  return SQ
27996 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
27997 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 66  * Truncate the f
27998 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
27999 6e 74 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61  nt memjrnlTrunca
2799a 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  te(sqlite3_file 
2799b 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e  *pJfd, sqlite_in
2799c 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 4d 65 6d  t64 size){.  Mem
2799d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65  Journal *p = (Me
2799e 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b  mJournal *)pJfd;
2799f 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43  .  FileChunk *pC
279a0 68 75 6e 6b 3b 0a 20 20 61 73 73 65 72 74 28 73  hunk;.  assert(s
279a1 69 7a 65 3d 3d 30 29 3b 0a 20 20 55 4e 55 53 45  ize==0);.  UNUSE
279a2 44 5f 50 41 52 41 4d 45 54 45 52 28 73 69 7a 65  D_PARAMETER(size
279a3 29 3b 0a 20 20 70 43 68 75 6e 6b 20 3d 20 70 2d  );.  pChunk = p-
279a4 3e 70 46 69 72 73 74 3b 0a 20 20 77 68 69 6c 65  >pFirst;.  while
279a5 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20 20  ( pChunk ){.    
279a6 46 69 6c 65 43 68 75 6e 6b 20 2a 70 54 6d 70 20  FileChunk *pTmp 
279a7 3d 20 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70 43  = pChunk;.    pC
279a8 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 2d 3e 70  hunk = pChunk->p
279a9 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  Next;.    sqlite
279aa 33 5f 66 72 65 65 28 70 54 6d 70 29 3b 0a 20 20  3_free(pTmp);.  
279ab 7d 0a 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f  }.  sqlite3MemJo
279ac 75 72 6e 61 6c 4f 70 65 6e 28 70 4a 66 64 29 3b  urnalOpen(pJfd);
279ad 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
279ae 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
279af 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f  ose the file..*/
279b0 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a  .static int memj
279b1 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  rnlClose(sqlite3
279b2 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20  _file *pJfd){.  
279b3 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28  memjrnlTruncate(
279b4 70 4a 66 64 2c 20 30 29 3b 0a 20 20 72 65 74 75  pJfd, 0);.  retu
279b5 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
279b6 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65  ../*.** Sync the
279b7 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 79 6e   file..**.** Syn
279b8 63 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  cing an in-memor
279b9 79 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 20 6e  y journal is a n
279ba 6f 2d 6f 70 2e 20 20 41 6e 64 2c 20 69 6e 20 66  o-op.  And, in f
279bb 61 63 74 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  act, this routin
279bc 65 0a 2a 2a 20 69 73 20 6e 65 76 65 72 20 63 61  e.** is never ca
279bd 6c 6c 65 64 20 69 6e 20 61 20 77 6f 72 6b 69 6e  lled in a workin
279be 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  g implementation
279bf 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e  .  This implemen
279c0 74 61 74 69 6f 6e 0a 2a 2a 20 65 78 69 73 74 73  tation.** exists
279c1 20 70 75 72 65 6c 79 20 61 73 20 61 20 63 6f 6e   purely as a con
279c2 74 69 6e 67 65 6e 63 79 2c 20 69 6e 20 63 61 73  tingency, in cas
279c3 65 20 73 6f 6d 65 20 6d 61 6c 66 75 6e 63 74 69  e some malfuncti
279c4 6f 6e 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72  on in some other
279c5 0a 2a 2a 20 70 61 72 74 20 6f 66 20 53 51 4c 69  .** part of SQLi
279c6 74 65 20 63 61 75 73 65 73 20 53 79 6e 63 20 74  te causes Sync t
279c7 6f 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 6d  o be called by m
279c8 69 73 74 61 6b 65 2e 0a 2a 2f 0a 73 74 61 74 69  istake..*/.stati
279c9 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 53 79 6e  c int memjrnlSyn
279ca 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  c(sqlite3_file *
279cb 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74  NotUsed, int Not
279cc 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 45 44  Used2){.  UNUSED
279cd 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
279ce 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
279cf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
279d0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65  OK;.}../*.** Que
279d1 72 79 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  ry the size of t
279d2 68 65 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73  he file in bytes
279d3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
279d4 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 28  memjrnlFileSize(
279d5 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
279d6 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  fd, sqlite_int64
279d7 20 2a 70 53 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a   *pSize){.  MemJ
279d8 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d  ournal *p = (Mem
279d9 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a  Journal *)pJfd;.
279da 20 20 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c 69    *pSize = (sqli
279db 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e 65 6e 64  te_int64) p->end
279dc 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 3b 0a 20  point.iOffset;. 
279dd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
279de 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c  K;.}../*.** Tabl
279df 65 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72  e of methods for
279e0 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c 69   MemJournal sqli
279e1 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e  te3_file object.
279e2 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
279e3 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
279e4 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a 6f  io_methods MemJo
279e5 75 72 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20 7b  urnalMethods = {
279e6 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20  .  1,           
279e7 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
279e8 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 43 6c 6f   */.  memjrnlClo
279e9 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73  se,     /* xClos
279ea 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 52 65  e */.  memjrnlRe
279eb 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 61  ad,      /* xRea
279ec 64 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 57 72  d */.  memjrnlWr
279ed 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69  ite,     /* xWri
279ee 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 54  te */.  memjrnlT
279ef 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54 72  runcate,  /* xTr
279f0 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a  uncate */.  memj
279f1 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f 2a  rnlSync,      /*
279f2 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6d 65 6d 6a   xSync */.  memj
279f3 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f 2a  rnlFileSize,  /*
279f4 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20   xFileSize */.  
279f5 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
279f6 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20    /* xLock */.  
279f7 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
279f8 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a    /* xUnlock */.
279f9 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
279fa 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73      /* xCheckRes
279fb 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30  ervedLock */.  0
279fc 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
279fd 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c   /* xFileControl
279fe 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
279ff 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 74          /* xSect
27a00 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20  orSize */.  0,  
27a01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
27a02 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65   xDeviceCharacte
27a03 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 30 2c 20  ristics */.  0, 
27a04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27a05 2a 20 78 53 68 6d 4d 61 70 20 2a 2f 0a 20 20 30  * xShmMap */.  0
27a06 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
27a07 20 2f 2a 20 78 53 68 6d 4c 6f 63 6b 20 2a 2f 0a   /* xShmLock */.
27a08 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
27a09 20 20 20 20 2f 2a 20 78 53 68 6d 42 61 72 72 69      /* xShmBarri
27a0a 65 72 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20  er */.  0       
27a0b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68            /* xSh
27a0c 6d 55 6e 6c 6f 63 6b 20 2a 2f 0a 7d 3b 0a 0a 2f  mUnlock */.};../
27a0d 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75  * .** Open a jou
27a0e 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51  rnal file..*/.SQ
27a0f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
27a10 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72  d sqlite3MemJour
27a11 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f  nalOpen(sqlite3_
27a12 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 4d  file *pJfd){.  M
27a13 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28  emJournal *p = (
27a14 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66  MemJournal *)pJf
27a15 64 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47  d;.  assert( EIG
27a16 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e  HT_BYTE_ALIGNMEN
27a17 54 28 70 29 20 29 3b 0a 20 20 6d 65 6d 73 65 74  T(p) );.  memset
27a18 28 70 2c 20 30 2c 20 73 71 6c 69 74 65 33 4d 65  (p, 0, sqlite3Me
27a19 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 29 3b  mJournalSize());
27a1a 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d 20  .  p->pMethod = 
27a1b 28 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68  (sqlite3_io_meth
27a1c 6f 64 73 2a 29 26 4d 65 6d 4a 6f 75 72 6e 61 6c  ods*)&MemJournal
27a1d 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 0a 2a  Methods;.}../*.*
27a1e 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
27a1f 20 74 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65   the file-handle
27a20 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72   passed as an ar
27a21 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 61 6e  gument is .** an
27a22 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e   in-memory journ
27a23 61 6c 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  al .*/.SQLITE_PR
27a24 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
27a25 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 73 71  3IsMemJournal(sq
27a26 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
27a27 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 4a 66 64  ){.  return pJfd
27a28 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 26 4d 65 6d  ->pMethods==&Mem
27a29 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a  JournalMethods;.
27a2a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e  }../* .** Return
27a2b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
27a2c 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f  ytes required to
27a2d 20 73 74 6f 72 65 20 61 20 4d 65 6d 4a 6f 75 72   store a MemJour
27a2e 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70  nal file descrip
27a2f 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  tor..*/.SQLITE_P
27a30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
27a31 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65  e3MemJournalSize
27a32 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e  (void){.  return
27a33 20 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f 75 72 6e   sizeof(MemJourn
27a34 61 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  al);.}../*******
27a35 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d  ******* End of m
27a36 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a  emjournal.c ****
27a37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a39 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
27a3a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
27a3b 6c 65 20 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a  le walker.c ****
27a3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a3e 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
27a3f 30 38 20 41 75 67 75 73 74 20 31 36 0a 2a 2a 0a  08 August 16.**.
27a40 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
27a41 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
27a42 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
27a43 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
27a44 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
27a45 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
27a46 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
27a47 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
27a48 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
27a49 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
27a4a 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
27a4b 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
27a4c 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
27a4d 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
27a4e 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
27a4f 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
27a50 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
27a51 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
27a52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
27a56 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
27a57 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20  s routines used 
27a58 66 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20  for walking the 
27a59 70 61 72 73 65 72 20 74 72 65 65 20 66 6f 72 0a  parser tree for.
27a5a 2a 2a 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  ** an SQL statem
27a5b 65 6e 74 2e 0a 2a 2f 0a 2f 2a 20 23 69 6e 63 6c  ent..*/./* #incl
27a5c 75 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e 20 2a  ude <stdlib.h> *
27a5d 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73  /./* #include <s
27a5e 74 72 69 6e 67 2e 68 3e 20 2a 2f 0a 0a 0a 2f 2a  tring.h> */.../*
27a5f 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72  .** Walk an expr
27a60 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 49 6e  ession tree.  In
27a61 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63  voke the callbac
27a62 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20  k once for each 
27a63 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 65  node.** of the e
27a64 78 70 72 65 73 73 69 6f 6e 2c 20 77 68 69 6c 65  xpression, while
27a65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20 28 49 6e   decending.  (In
27a66 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68   other words, th
27a67 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73  e callback.** is
27a68 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f 72 65 20   invoked before 
27a69 76 69 73 69 74 69 6e 67 20 63 68 69 6c 64 72 65  visiting childre
27a6a 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  n.).**.** The re
27a6b 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20  turn value from 
27a6c 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f  the callback sho
27a6d 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66 20 74 68  uld be one of th
27a6e 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f 6e 73 74  e WRC_*.** const
27a6f 61 6e 74 73 20 74 6f 20 73 70 65 63 69 66 79 20  ants to specify 
27a70 68 6f 77 20 74 6f 20 70 72 6f 63 65 65 64 20 77  how to proceed w
27a71 69 74 68 20 74 68 65 20 77 61 6c 6b 2e 0a 2a 2a  ith the walk..**
27a72 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f 6e 74 69  .**    WRC_Conti
27a73 6e 75 65 20 20 20 20 20 20 43 6f 6e 74 69 6e 75  nue      Continu
27a74 65 20 64 65 73 63 65 6e 64 69 6e 67 20 64 6f 77  e descending dow
27a75 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a  n the tree..**.*
27a76 2a 20 20 20 20 57 52 43 5f 50 72 75 6e 65 20 20  *    WRC_Prune  
27a77 20 20 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65         Do not de
27a78 73 63 65 6e 64 20 69 6e 74 6f 20 63 68 69 6c 64  scend into child
27a79 20 6e 6f 64 65 73 2e 20 20 42 75 74 20 61 6c 6c   nodes.  But all
27a7a 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ow.**           
27a7b 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 77             the w
27a7c 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20  alk to continue 
27a7d 77 69 74 68 20 73 69 62 6c 69 6e 67 20 6e 6f 64  with sibling nod
27a7e 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43  es..**.**    WRC
27a7f 5f 41 62 6f 72 74 20 20 20 20 20 20 20 20 20 44  _Abort         D
27a80 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61  o no more callba
27a81 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20 74 68 65  cks.  Unwind the
27a82 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20   stack and.**   
27a83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27a84 20 20 20 72 65 74 75 72 6e 20 74 68 65 20 74 6f     return the to
27a85 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c  p-level walk cal
27a86 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  l..**.** The ret
27a87 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74  urn value from t
27a88 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 57  his routine is W
27a89 52 43 5f 41 62 6f 72 74 20 74 6f 20 61 62 61 6e  RC_Abort to aban
27a8a 64 6f 6e 20 74 68 65 20 74 72 65 65 20 77 61 6c  don the tree wal
27a8b 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f 43 6f 6e  k.** and WRC_Con
27a8c 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74 69 6e 75  tinue to continu
27a8d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
27a8e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
27a8f 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 20  WalkExpr(Walker 
27a90 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a  *pWalker, Expr *
27a91 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 72 63  pExpr){.  int rc
27a92 3b 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30  ;.  if( pExpr==0
27a93 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f   ) return WRC_Co
27a94 6e 74 69 6e 75 65 3b 0a 20 20 74 65 73 74 63 61  ntinue;.  testca
27a95 73 65 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  se( ExprHasPrope
27a96 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f  rty(pExpr, EP_To
27a97 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20 74 65  kenOnly) );.  te
27a98 73 74 63 61 73 65 28 20 45 78 70 72 48 61 73 50  stcase( ExprHasP
27a99 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
27a9a 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20  P_Reduced) );.  
27a9b 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 45  rc = pWalker->xE
27a9c 78 70 72 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c  xprCallback(pWal
27a9d 6b 65 72 2c 20 70 45 78 70 72 29 3b 0a 20 20 69  ker, pExpr);.  i
27a9e 66 28 20 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 69  f( rc==WRC_Conti
27a9f 6e 75 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  nue.            
27aa0 20 20 26 26 20 21 45 78 70 72 48 61 73 41 6e 79    && !ExprHasAny
27aa1 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45  Property(pExpr,E
27aa2 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a  P_TokenOnly) ){.
27aa3 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57      if( sqlite3W
27aa4 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c  alkExpr(pWalker,
27aa5 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 20 29   pExpr->pLeft) )
27aa6 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
27aa7 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  t;.    if( sqlit
27aa8 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b  e3WalkExpr(pWalk
27aa9 65 72 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  er, pExpr->pRigh
27aaa 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  t) ) return WRC_
27aab 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 45  Abort;.    if( E
27aac 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
27aad 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
27aae 63 74 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ct) ){.      if(
27aaf 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
27ab0 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70  ct(pWalker, pExp
27ab1 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 20 29 20  r->x.pSelect) ) 
27ab2 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
27ab3 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
27ab4 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61     if( sqlite3Wa
27ab5 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b  lkExprList(pWalk
27ab6 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  er, pExpr->x.pLi
27ab7 73 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  st) ) return WRC
27ab8 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
27ab9 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20  }.  return rc & 
27aba 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a  WRC_Abort;.}../*
27abb 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33  .** Call sqlite3
27abc 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65  WalkExpr() for e
27abd 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20  very expression 
27abe 69 6e 20 6c 69 73 74 20 70 20 6f 72 20 75 6e 74  in list p or unt
27abf 69 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72 74 20 72  il.** an abort r
27ac0 65 71 75 65 73 74 20 69 73 20 73 65 65 6e 2e 0a  equest is seen..
27ac1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
27ac2 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c  E int sqlite3Wal
27ac3 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b 65 72  kExprList(Walker
27ac4 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 4c   *pWalker, ExprL
27ac5 69 73 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  ist *p){.  int i
27ac6 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  ;.  struct ExprL
27ac7 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
27ac8 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
27ac9 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 72 2c 20  for(i=p->nExpr, 
27aca 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 3e 30 3b  pItem=p->a; i>0;
27acb 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i--, pItem++){.
27acc 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
27acd 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65  3WalkExpr(pWalke
27ace 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29  r, pItem->pExpr)
27acf 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
27ad0 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ort;.    }.  }. 
27ad1 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
27ad2 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  inue;.}../*.** W
27ad3 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65 73 73 69  alk all expressi
27ad4 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ons associated w
27ad5 69 74 68 20 53 45 4c 45 43 54 20 73 74 61 74 65  ith SELECT state
27ad6 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e  ment p.  Do.** n
27ad7 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 53 45  ot invoke the SE
27ad8 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e  LECT callback on
27ad9 20 70 2c 20 62 75 74 20 64 6f 20 28 6f 66 20 63   p, but do (of c
27ada 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a  ourse) invoke.**
27adb 20 61 6e 79 20 65 78 70 72 20 63 61 6c 6c 62 61   any expr callba
27adc 63 6b 73 20 61 6e 64 20 53 45 4c 45 43 54 20 63  cks and SELECT c
27add 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 63 6f  allbacks that co
27ade 6d 65 20 66 72 6f 6d 20 73 75 62 71 75 65 72 69  me from subqueri
27adf 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52  es..** Return WR
27ae0 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43  C_Abort or WRC_C
27ae1 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49  ontinue..*/.SQLI
27ae2 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
27ae3 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
27ae4 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61  Expr(Walker *pWa
27ae5 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29  lker, Select *p)
27ae6 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57  {.  if( sqlite3W
27ae7 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c  alkExprList(pWal
27ae8 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20  ker, p->pEList) 
27ae9 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
27aea 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rt;.  if( sqlite
27aeb 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65  3WalkExpr(pWalke
27aec 72 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 29 20  r, p->pWhere) ) 
27aed 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
27aee 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57  ;.  if( sqlite3W
27aef 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c  alkExprList(pWal
27af0 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79  ker, p->pGroupBy
27af1 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
27af2 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69  bort;.  if( sqli
27af3 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
27af4 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29  ker, p->pHaving)
27af5 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
27af6 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ort;.  if( sqlit
27af7 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70  e3WalkExprList(p
27af8 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65  Walker, p->pOrde
27af9 72 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52  rBy) ) return WR
27afa 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73  C_Abort;.  if( s
27afb 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70  qlite3WalkExpr(p
27afc 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69  Walker, p->pLimi
27afd 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  t) ) return WRC_
27afe 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c  Abort;.  if( sql
27aff 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61  ite3WalkExpr(pWa
27b00 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74  lker, p->pOffset
27b01 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
27b02 62 6f 72 74 3b 0a 20 20 72 65 74 75 72 6e 20 57  bort;.  return W
27b03 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a  RC_Continue;.}..
27b04 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70  /*.** Walk the p
27b05 61 72 73 65 20 74 72 65 65 73 20 61 73 73 6f 63  arse trees assoc
27b06 69 61 74 65 64 20 77 69 74 68 20 61 6c 6c 20 73  iated with all s
27b07 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 65  ubqueries in the
27b08 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  .** FROM clause 
27b09 6f 66 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  of SELECT statem
27b0a 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69  ent p.  Do not i
27b0b 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c 65 63 74  nvoke the select
27b0c 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20  .** callback on 
27b0d 70 2c 20 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65  p, but do invoke
27b0e 20 69 74 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d   it on each FROM
27b0f 20 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79   clause subquery
27b10 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73  .** and on any s
27b11 75 62 71 75 65 72 69 65 73 20 66 75 72 74 68 65  ubqueries furthe
27b12 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 74 72  r down in the tr
27b13 65 65 2e 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20  ee.  Return .** 
27b14 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43  WRC_Abort or WRC
27b15 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51  _Continue;.*/.SQ
27b16 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
27b17 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
27b18 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70  ctFrom(Walker *p
27b19 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a  Walker, Select *
27b1a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  p){.  SrcList *p
27b1b 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  Src;.  int i;.  
27b1c 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
27b1d 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70  tem *pItem;..  p
27b1e 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  Src = p->pSrc;. 
27b1f 20 69 66 28 20 41 4c 57 41 59 53 28 70 53 72 63   if( ALWAYS(pSrc
27b20 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70  ) ){.    for(i=p
27b21 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d  Src->nSrc, pItem
27b22 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69  =pSrc->a; i>0; i
27b23 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  --, pItem++){.  
27b24 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57      if( sqlite3W
27b25 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65  alkSelect(pWalke
27b26 72 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  r, pItem->pSelec
27b27 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  t) ){.        re
27b28 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
27b29 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
27b2a 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43  }.  return WRC_C
27b2b 6f 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a 0a  ontinue;.} ../*.
27b2c 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57  ** Call sqlite3W
27b2d 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76  alkExpr() for ev
27b2e 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ery expression i
27b2f 6e 20 53 65 6c 65 63 74 20 73 74 61 74 65 6d 65  n Select stateme
27b30 6e 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20  nt p..** Invoke 
27b31 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
27b32 74 28 29 20 66 6f 72 20 73 75 62 71 75 65 72 69  t() for subqueri
27b33 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  es in the FROM c
27b34 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e 20  lause and.** on 
27b35 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  the compound sel
27b36 65 63 74 20 63 68 61 69 6e 2c 20 70 2d 3e 70 50  ect chain, p->pP
27b37 72 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  rior..**.** Retu
27b38 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20  rn WRC_Continue 
27b39 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 6f 6e  under normal con
27b3a 64 69 74 69 6f 6e 73 2e 20 20 52 65 74 75 72 6e  ditions.  Return
27b3b 20 57 52 43 5f 41 62 6f 72 74 20 69 66 0a 2a 2a   WRC_Abort if.**
27b3c 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 62 6f   there is an abo
27b3d 72 74 20 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a  rt request..**.*
27b3e 2a 20 49 66 20 74 68 65 20 57 61 6c 6b 65 72 20  * If the Walker 
27b3f 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 6e  does not have an
27b40 20 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b   xSelectCallback
27b41 28 29 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  () then this rou
27b42 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d  tine.** is a no-
27b43 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 57 52 43  op returning WRC
27b44 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51  _Continue..*/.SQ
27b45 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
27b46 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
27b47 63 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b  ct(Walker *pWalk
27b48 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a  er, Select *p){.
27b49 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20    int rc;.  if( 
27b4a 70 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b 65 72 2d  p==0 || pWalker-
27b4b 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  >xSelectCallback
27b4c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43  ==0 ) return WRC
27b4d 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20  _Continue;.  rc 
27b4e 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a  = WRC_Continue;.
27b4f 20 20 70 57 61 6c 6b 65 72 2d 3e 77 61 6c 6b 65    pWalker->walke
27b50 72 44 65 70 74 68 2b 2b 3b 0a 20 20 77 68 69 6c  rDepth++;.  whil
27b51 65 28 20 70 20 29 7b 0a 20 20 20 20 72 63 20 3d  e( p ){.    rc =
27b52 20 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c 65 63   pWalker->xSelec
27b53 74 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65  tCallback(pWalke
27b54 72 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 72  r, p);.    if( r
27b55 63 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69  c ) break;.    i
27b56 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65  f( sqlite3WalkSe
27b57 6c 65 63 74 45 78 70 72 28 70 57 61 6c 6b 65 72  lectExpr(pWalker
27b58 2c 20 70 29 0a 20 20 20 20 20 7c 7c 20 73 71 6c  , p).     || sql
27b59 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72  ite3WalkSelectFr
27b5a 6f 6d 28 70 57 61 6c 6b 65 72 2c 20 70 29 0a 20  om(pWalker, p). 
27b5b 20 20 20 29 7b 0a 20 20 20 20 20 20 70 57 61 6c     ){.      pWal
27b5c 6b 65 72 2d 3e 77 61 6c 6b 65 72 44 65 70 74 68  ker->walkerDepth
27b5d 2d 2d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  --;.      return
27b5e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20   WRC_Abort;.    
27b5f 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 72  }.    p = p->pPr
27b60 69 6f 72 3b 0a 20 20 7d 0a 20 20 70 57 61 6c 6b  ior;.  }.  pWalk
27b61 65 72 2d 3e 77 61 6c 6b 65 72 44 65 70 74 68 2d  er->walkerDepth-
27b62 2d 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26  -;.  return rc &
27b63 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f   WRC_Abort;.}../
27b64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
27b65 6e 64 20 6f 66 20 77 61 6c 6b 65 72 2e 63 20 2a  nd of walker.c *
27b66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
27b69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
27b6a 65 67 69 6e 20 66 69 6c 65 20 72 65 73 6f 6c 76  egin file resolv
27b6b 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
27b6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
27b6e 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74  *.** 2008 August
27b6f 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   18.**.** The au
27b70 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
27b71 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
27b72 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
27b73 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
27b74 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
27b75 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
27b76 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
27b77 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
27b78 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
27b79 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
27b7a 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
27b7b 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
27b7c 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
27b7d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
27b7e 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
27b7f 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
27b80 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
27b81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27b85 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
27b86 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74  le contains rout
27b87 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61  ines used for wa
27b88 6c 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72  lking the parser
27b89 20 74 72 65 65 20 61 6e 64 0a 2a 2a 20 72 65 73   tree and.** res
27b8a 6f 6c 76 65 20 61 6c 6c 20 69 64 65 6e 74 69 66  olve all identif
27b8b 69 65 72 73 20 62 79 20 61 73 73 6f 63 69 61 74  iers by associat
27b8c 69 6e 67 20 74 68 65 6d 20 77 69 74 68 20 61 20  ing them with a 
27b8d 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 74 61  particular.** ta
27b8e 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a  ble and column..
27b8f 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c  */./* #include <
27b90 73 74 64 6c 69 62 2e 68 3e 20 2a 2f 0a 2f 2a 20  stdlib.h> */./* 
27b91 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67  #include <string
27b92 2e 68 3e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 61  .h> */../*.** Wa
27b93 6c 6b 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  lk the expressio
27b94 6e 20 74 72 65 65 20 70 45 78 70 72 20 61 6e 64  n tree pExpr and
27b95 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 61 67   increase the ag
27b96 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
27b97 0a 2a 2a 20 64 65 70 74 68 20 28 74 68 65 20 45  .** depth (the E
27b98 78 70 72 2e 6f 70 32 20 66 69 65 6c 64 29 20 62  xpr.op2 field) b
27b99 79 20 4e 20 6f 6e 20 65 76 65 72 79 20 54 4b 5f  y N on every TK_
27b9a 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 6e 6f 64  AGG_FUNCTION nod
27b9b 65 2e 0a 2a 2a 20 54 68 69 73 20 6e 65 65 64 73  e..** This needs
27b9c 20 74 6f 20 6f 63 63 75 72 20 77 68 65 6e 20 63   to occur when c
27b9d 6f 70 79 69 6e 67 20 61 20 54 4b 5f 41 47 47 5f  opying a TK_AGG_
27b9e 46 55 4e 43 54 49 4f 4e 20 6e 6f 64 65 20 66 72  FUNCTION node fr
27b9f 6f 6d 20 61 6e 0a 2a 2a 20 6f 75 74 65 72 20 71  om an.** outer q
27ba0 75 65 72 79 20 69 6e 74 6f 20 61 6e 20 69 6e 6e  uery into an inn
27ba1 65 72 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a  er subquery..**.
27ba2 2a 2a 20 69 6e 63 72 41 67 67 46 75 6e 63 74 69  ** incrAggFuncti
27ba3 6f 6e 44 65 70 74 68 28 70 45 78 70 72 2c 6e 29  onDepth(pExpr,n)
27ba4 20 69 73 20 74 68 65 20 6d 61 69 6e 20 72 6f 75   is the main rou
27ba5 74 69 6e 65 2e 20 20 69 6e 63 72 41 67 67 44 65  tine.  incrAggDe
27ba6 70 74 68 28 2e 2e 29 0a 2a 2a 20 69 73 20 61 20  pth(..).** is a 
27ba7 68 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20  helper function 
27ba8 2d 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72  - a callback for
27ba9 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b 65 72   the tree walker
27baa 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
27bab 69 6e 63 72 41 67 67 44 65 70 74 68 28 57 61 6c  incrAggDepth(Wal
27bac 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78  ker *pWalker, Ex
27bad 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 66  pr *pExpr){.  if
27bae 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
27baf 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 29 20 70  AGG_FUNCTION ) p
27bb0 45 78 70 72 2d 3e 6f 70 32 20 2b 3d 20 70 57 61  Expr->op2 += pWa
27bb1 6c 6b 65 72 2d 3e 75 2e 69 3b 0a 20 20 72 65 74  lker->u.i;.  ret
27bb2 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  urn WRC_Continue
27bb3 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
27bb4 69 6e 63 72 41 67 67 46 75 6e 63 74 69 6f 6e 44  incrAggFunctionD
27bb5 65 70 74 68 28 45 78 70 72 20 2a 70 45 78 70 72  epth(Expr *pExpr
27bb6 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28 20  , int N){.  if( 
27bb7 4e 3e 30 20 29 7b 0a 20 20 20 20 57 61 6c 6b 65  N>0 ){.    Walke
27bb8 72 20 77 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  r w;.    memset(
27bb9 26 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77 29  &w, 0, sizeof(w)
27bba 29 3b 0a 20 20 20 20 77 2e 78 45 78 70 72 43 61  );.    w.xExprCa
27bbb 6c 6c 62 61 63 6b 20 3d 20 69 6e 63 72 41 67 67  llback = incrAgg
27bbc 44 65 70 74 68 3b 0a 20 20 20 20 77 2e 75 2e 69  Depth;.    w.u.i
27bbd 20 3d 20 4e 3b 0a 20 20 20 20 73 71 6c 69 74 65   = N;.    sqlite
27bbe 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 45  3WalkExpr(&w, pE
27bbf 78 70 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  xpr);.  }.}../*.
27bc0 2a 2a 20 54 75 72 6e 20 74 68 65 20 70 45 78 70  ** Turn the pExp
27bc1 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74  r expression int
27bc2 6f 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 74  o an alias for t
27bc3 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d  he iCol-th colum
27bc4 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 73 75  n of the.** resu
27bc5 6c 74 20 73 65 74 20 69 6e 20 70 45 4c 69 73 74  lt set in pEList
27bc6 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72  ..**.** If the r
27bc7 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e  esult set column
27bc8 20 69 73 20 61 20 73 69 6d 70 6c 65 20 63 6f 6c   is a simple col
27bc9 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 2c 20 74  umn reference, t
27bca 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
27bcb 0a 2a 2a 20 6d 61 6b 65 73 20 61 6e 20 65 78 61  .** makes an exa
27bcc 63 74 20 63 6f 70 79 2e 20 20 42 75 74 20 66 6f  ct copy.  But fo
27bcd 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64  r any other kind
27bce 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 2c 20   of expression, 
27bcf 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
27bd0 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74  make a copy of t
27bd1 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f  he result set co
27bd2 6c 75 6d 6e 20 61 73 20 74 68 65 20 61 72 67 75  lumn as the argu
27bd3 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 54  ment to the.** T
27bd4 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 2e 20 20  K_AS operator.  
27bd5 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72 61 74  The TK_AS operat
27bd6 6f 72 20 63 61 75 73 65 73 20 74 68 65 20 65 78  or causes the ex
27bd7 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 0a 2a  pression to be.*
27bd8 2a 20 65 76 61 6c 75 61 74 65 64 20 6a 75 73 74  * evaluated just
27bd9 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 6e 20 72   once and then r
27bda 65 75 73 65 64 20 66 6f 72 20 65 61 63 68 20 61  eused for each a
27bdb 6c 69 61 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  lias..**.** The 
27bdc 72 65 61 73 6f 6e 20 66 6f 72 20 73 75 70 70 72  reason for suppr
27bdd 65 73 73 69 6e 67 20 74 68 65 20 54 4b 5f 41 53  essing the TK_AS
27bde 20 74 65 72 6d 20 77 68 65 6e 20 74 68 65 20 65   term when the e
27bdf 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73  xpression is a s
27be0 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  imple.** column 
27be1 72 65 66 65 72 65 6e 63 65 20 69 73 20 73 6f 20  reference is so 
27be2 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20  that the column 
27be3 72 65 66 65 72 65 6e 63 65 20 77 69 6c 6c 20 62  reference will b
27be4 65 20 72 65 63 6f 67 6e 69 7a 65 64 20 61 73 0a  e recognized as.
27be5 2a 2a 20 75 73 61 62 6c 65 20 62 79 20 69 6e 64  ** usable by ind
27be6 69 63 65 73 20 77 69 74 68 69 6e 20 74 68 65 20  ices within the 
27be7 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f  WHERE clause pro
27be8 63 65 73 73 69 6e 67 20 6c 6f 67 69 63 2e 20 0a  cessing logic. .
27be9 2a 2a 0a 2a 2a 20 48 61 63 6b 3a 20 20 54 68 65  **.** Hack:  The
27bea 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 20   TK_AS operator 
27beb 69 73 20 69 6e 68 69 62 69 74 65 64 20 69 66 20  is inhibited if 
27bec 7a 54 79 70 65 5b 30 5d 3d 3d 27 47 27 2e 20 20  zType[0]=='G'.  
27bed 54 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20 74 68  This means.** th
27bee 61 74 20 69 6e 20 61 20 47 52 4f 55 50 20 42 59  at in a GROUP BY
27bef 20 63 6c 61 75 73 65 2c 20 74 68 65 20 65 78 70   clause, the exp
27bf0 72 65 73 73 69 6f 6e 20 69 73 20 65 76 61 6c 75  ression is evalu
27bf1 61 74 65 64 20 74 77 69 63 65 2e 20 20 48 65 6e  ated twice.  Hen
27bf2 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  ce:.**.**     SE
27bf3 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 25 35 20  LECT random()%5 
27bf4 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46  AS x, count(*) F
27bf5 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 20 42 59  ROM tab GROUP BY
27bf6 20 78 0a 2a 2a 0a 2a 2a 20 49 73 20 65 71 75 69   x.**.** Is equi
27bf7 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a  valent to:.**.**
27bf8 20 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64       SELECT rand
27bf9 6f 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75  om()%5 AS x, cou
27bfa 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47  nt(*) FROM tab G
27bfb 52 4f 55 50 20 42 59 20 72 61 6e 64 6f 6d 28 29  ROUP BY random()
27bfc 25 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73  %5.**.** The res
27bfd 75 6c 74 20 6f 66 20 72 61 6e 64 6f 6d 28 29 25  ult of random()%
27bfe 35 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42  5 in the GROUP B
27bff 59 20 63 6c 61 75 73 65 20 69 73 20 70 72 6f 62  Y clause is prob
27c00 61 62 6c 79 20 64 69 66 66 65 72 65 6e 74 0a 2a  ably different.*
27c01 2a 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75 6c  * from the resul
27c02 74 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d  t in the result-
27c03 73 65 74 2e 20 20 57 65 20 6d 69 67 68 74 20 66  set.  We might f
27c04 69 78 20 74 68 69 73 20 73 6f 6d 65 64 61 79 2e  ix this someday.
27c05 20 20 4f 72 0a 2a 2a 20 74 68 65 6e 20 61 67 61    Or.** then aga
27c06 69 6e 2c 20 77 65 20 6d 69 67 68 74 20 6e 6f 74  in, we might not
27c07 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 53  ....**.** The nS
27c08 75 62 71 75 65 72 79 20 70 61 72 61 6d 65 74 65  ubquery paramete
27c09 72 20 73 70 65 63 69 66 69 65 73 20 68 6f 77 20  r specifies how 
27c0a 6d 61 6e 79 20 6c 65 76 65 6c 73 20 6f 66 20 73  many levels of s
27c0b 75 62 71 75 65 72 79 20 74 68 65 0a 2a 2a 20 61  ubquery the.** a
27c0c 6c 69 61 73 20 69 73 20 72 65 6d 6f 76 65 64 20  lias is removed 
27c0d 66 72 6f 6d 20 74 68 65 20 6f 72 69 67 69 6e 61  from the origina
27c0e 6c 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54  l expression.  T
27c0f 68 65 20 75 73 75 61 6c 6c 79 20 76 61 6c 75 65  he usually value
27c10 20 69 73 0a 2a 2a 20 7a 65 72 6f 20 62 75 74 20   is.** zero but 
27c11 69 74 20 6d 69 67 68 74 20 62 65 20 6d 6f 72 65  it might be more
27c12 20 69 66 20 74 68 65 20 61 6c 69 61 73 20 69 73   if the alias is
27c13 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69   contained withi
27c14 6e 20 61 20 73 75 62 71 75 65 72 79 0a 2a 2a 20  n a subquery.** 
27c15 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  of the original 
27c16 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 65  expression.  The
27c17 20 45 78 70 72 2e 6f 70 32 20 66 69 65 6c 64 20   Expr.op2 field 
27c18 6f 66 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  of TK_AGG_FUNCTI
27c19 4f 4e 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73  ON.** structures
27c1a 20 6d 75 73 74 20 62 65 20 69 6e 63 72 65 61 73   must be increas
27c1b 65 64 20 62 79 20 74 68 65 20 6e 53 75 62 71 75  ed by the nSubqu
27c1c 65 72 79 20 61 6d 6f 75 6e 74 2e 0a 2a 2f 0a 73  ery amount..*/.s
27c1d 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c  tatic void resol
27c1e 76 65 41 6c 69 61 73 28 0a 20 20 50 61 72 73 65  veAlias(.  Parse
27c1f 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
27c20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
27c21 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69  text */.  ExprLi
27c22 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20  st *pEList,     
27c23 20 2f 2a 20 41 20 72 65 73 75 6c 74 20 73 65 74   /* A result set
27c24 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20   */.  int iCol, 
27c25 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27c26 41 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  A column in the 
27c27 72 65 73 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e  result set.  0..
27c28 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20  pEList->nExpr-1 
27c29 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
27c2a 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
27c2b 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 20 69 6e  ransform this in
27c2c 74 6f 20 61 6e 20 61 6c 69 61 73 20 74 6f 20 74  to an alias to t
27c2d 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
27c2e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
27c2f 54 79 70 65 2c 20 20 20 20 20 2f 2a 20 22 47 52  Type,     /* "GR
27c30 4f 55 50 22 20 6f 72 20 22 4f 52 44 45 52 22 20  OUP" or "ORDER" 
27c31 6f 72 20 22 22 20 2a 2f 0a 20 20 69 6e 74 20 6e  or "" */.  int n
27c32 53 75 62 71 75 65 72 79 20 20 20 20 20 20 20 20  Subquery        
27c33 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
27c34 75 62 71 75 65 72 69 65 73 20 74 68 61 74 20 74  ubqueries that t
27c35 68 65 20 6c 61 62 65 6c 20 69 73 20 6d 6f 76 69  he label is movi
27c36 6e 67 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20  ng */.){.  Expr 
27c37 2a 70 4f 72 69 67 3b 20 20 20 20 20 20 20 20 20  *pOrig;         
27c38 20 20 2f 2a 20 54 68 65 20 69 43 6f 6c 2d 74 68    /* The iCol-th
27c39 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72   column of the r
27c3a 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 45  esult set */.  E
27c3b 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20 20 20  xpr *pDup;      
27c3c 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66        /* Copy of
27c3d 20 70 4f 72 69 67 20 2a 2f 0a 20 20 73 71 6c 69   pOrig */.  sqli
27c3e 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
27c3f 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
27c40 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
27c41 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c  ..  assert( iCol
27c42 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 45 4c 69  >=0 && iCol<pELi
27c43 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 70  st->nExpr );.  p
27c44 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61  Orig = pEList->a
27c45 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 20  [iCol].pExpr;.  
27c46 61 73 73 65 72 74 28 20 70 4f 72 69 67 21 3d 30  assert( pOrig!=0
27c47 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
27c48 72 69 67 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  rig->flags & EP_
27c49 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 64 62  Resolved );.  db
27c4a 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
27c4b 20 69 66 28 20 70 4f 72 69 67 2d 3e 6f 70 21 3d   if( pOrig->op!=
27c4c 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a 54 79  TK_COLUMN && zTy
27c4d 70 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0a 20 20  pe[0]!='G' ){.  
27c4e 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33    pDup = sqlite3
27c4f 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69  ExprDup(db, pOri
27c50 67 2c 20 30 29 3b 0a 20 20 20 20 69 6e 63 72 41  g, 0);.    incrA
27c51 67 67 46 75 6e 63 74 69 6f 6e 44 65 70 74 68 28  ggFunctionDepth(
27c52 70 44 75 70 2c 20 6e 53 75 62 71 75 65 72 79 29  pDup, nSubquery)
27c53 3b 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c  ;.    pDup = sql
27c54 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
27c55 2c 20 54 4b 5f 41 53 2c 20 70 44 75 70 2c 20 30  , TK_AS, pDup, 0
27c56 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 44  , 0);.    if( pD
27c57 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  up==0 ) return;.
27c58 20 20 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e      if( pEList->
27c59 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3d 3d  a[iCol].iAlias==
27c5a 30 20 29 7b 0a 20 20 20 20 20 20 70 45 4c 69 73  0 ){.      pELis
27c5b 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61  t->a[iCol].iAlia
27c5c 73 20 3d 20 28 75 31 36 29 28 2b 2b 70 50 61 72  s = (u16)(++pPar
27c5d 73 65 2d 3e 6e 41 6c 69 61 73 29 3b 0a 20 20 20  se->nAlias);.   
27c5e 20 7d 0a 20 20 20 20 70 44 75 70 2d 3e 69 54 61   }.    pDup->iTa
27c5f 62 6c 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b  ble = pEList->a[
27c60 69 43 6f 6c 5d 2e 69 41 6c 69 61 73 3b 0a 20 20  iCol].iAlias;.  
27c61 7d 65 6c 73 65 20 69 66 28 20 45 78 70 72 48 61  }else if( ExprHa
27c62 73 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c  sProperty(pOrig,
27c63 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c 7c   EP_IntValue) ||
27c64 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e   pOrig->u.zToken
27c65 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 75 70 20  ==0 ){.    pDup 
27c66 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
27c67 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29 3b 0a  (db, pOrig, 0);.
27c68 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20      if( pDup==0 
27c69 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73  ) return;.  }els
27c6a 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6f  e{.    char *zTo
27c6b 6b 65 6e 20 3d 20 70 4f 72 69 67 2d 3e 75 2e 7a  ken = pOrig->u.z
27c6c 54 6f 6b 65 6e 3b 0a 20 20 20 20 61 73 73 65 72  Token;.    asser
27c6d 74 28 20 7a 54 6f 6b 65 6e 21 3d 30 20 29 3b 0a  t( zToken!=0 );.
27c6e 20 20 20 20 70 4f 72 69 67 2d 3e 75 2e 7a 54 6f      pOrig->u.zTo
27c6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20 70 44 75  ken = 0;.    pDu
27c70 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  p = sqlite3ExprD
27c71 75 70 28 64 62 2c 20 70 4f 72 69 67 2c 20 30 29  up(db, pOrig, 0)
27c72 3b 0a 20 20 20 20 70 4f 72 69 67 2d 3e 75 2e 7a  ;.    pOrig->u.z
27c73 54 6f 6b 65 6e 20 3d 20 7a 54 6f 6b 65 6e 3b 0a  Token = zToken;.
27c74 20 20 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20      if( pDup==0 
27c75 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 61 73  ) return;.    as
27c76 73 65 72 74 28 20 28 70 44 75 70 2d 3e 66 6c 61  sert( (pDup->fla
27c77 67 73 20 26 20 28 45 50 5f 52 65 64 75 63 65 64  gs & (EP_Reduced
27c78 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 29 3d  |EP_TokenOnly))=
27c79 3d 30 20 29 3b 0a 20 20 20 20 70 44 75 70 2d 3e  =0 );.    pDup->
27c7a 66 6c 61 67 73 32 20 7c 3d 20 45 50 32 5f 4d 61  flags2 |= EP2_Ma
27c7b 6c 6c 6f 63 65 64 54 6f 6b 65 6e 3b 0a 20 20 20  llocedToken;.   
27c7c 20 70 44 75 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20   pDup->u.zToken 
27c7d 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
27c7e 70 28 64 62 2c 20 7a 54 6f 6b 65 6e 29 3b 0a 20  p(db, zToken);. 
27c7f 20 7d 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e   }.  if( pExpr->
27c80 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
27c81 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 70 44 75  llate ){.    pDu
27c82 70 2d 3e 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72  p->pColl = pExpr
27c83 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 70 44 75  ->pColl;.    pDu
27c84 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45  p->flags |= EP_E
27c85 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 7d 0a 0a  xpCollate;.  }..
27c86 20 20 2f 2a 20 42 65 66 6f 72 65 20 63 61 6c 6c    /* Before call
27c87 69 6e 67 20 73 71 6c 69 74 65 33 45 78 70 72 44  ing sqlite3ExprD
27c88 65 6c 65 74 65 28 29 2c 20 73 65 74 20 74 68 65  elete(), set the
27c89 20 45 50 5f 53 74 61 74 69 63 20 66 6c 61 67 2e   EP_Static flag.
27c8a 20 54 68 69 73 20 0a 20 20 2a 2a 20 70 72 65 76   This .  ** prev
27c8b 65 6e 74 73 20 45 78 70 72 44 65 6c 65 74 65 28  ents ExprDelete(
27c8c 29 20 66 72 6f 6d 20 64 65 6c 65 74 69 6e 67 20  ) from deleting 
27c8d 74 68 65 20 45 78 70 72 20 73 74 72 75 63 74 75  the Expr structu
27c8e 72 65 20 69 74 73 65 6c 66 2c 0a 20 20 2a 2a 20  re itself,.  ** 
27c8f 61 6c 6c 6f 77 69 6e 67 20 69 74 20 74 6f 20 62  allowing it to b
27c90 65 20 72 65 70 6f 70 75 6c 61 74 65 64 20 62 79  e repopulated by
27c91 20 74 68 65 20 6d 65 6d 63 70 79 28 29 20 6f 6e   the memcpy() on
27c92 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c   the following l
27c93 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 45 78 70 72  ine..  */.  Expr
27c94 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70  SetProperty(pExp
27c95 72 2c 20 45 50 5f 53 74 61 74 69 63 29 3b 0a 20  r, EP_Static);. 
27c96 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
27c97 74 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20  te(db, pExpr);. 
27c98 20 6d 65 6d 63 70 79 28 70 45 78 70 72 2c 20 70   memcpy(pExpr, p
27c99 44 75 70 2c 20 73 69 7a 65 6f 66 28 2a 70 45 78  Dup, sizeof(*pEx
27c9a 70 72 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  pr));.  sqlite3D
27c9b 62 46 72 65 65 28 64 62 2c 20 70 44 75 70 29 3b  bFree(db, pDup);
27c9c 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  .}.../*.** Retur
27c9d 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 6e 61  n TRUE if the na
27c9e 6d 65 20 7a 43 6f 6c 20 6f 63 63 75 72 73 20 61  me zCol occurs a
27c9f 6e 79 77 68 65 72 65 20 69 6e 20 74 68 65 20 55  nywhere in the U
27ca0 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a  SING clause..**.
27ca1 2a 2a 20 52 65 74 75 72 6e 20 46 41 4c 53 45 20  ** Return FALSE 
27ca2 69 66 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61  if the USING cla
27ca3 75 73 65 20 69 73 20 4e 55 4c 4c 20 6f 72 20 69  use is NULL or i
27ca4 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 63 6f  f it does not co
27ca5 6e 74 61 69 6e 0a 2a 2a 20 7a 43 6f 6c 2e 0a 2a  ntain.** zCol..*
27ca6 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 61 6d  /.static int nam
27ca7 65 49 6e 55 73 69 6e 67 43 6c 61 75 73 65 28 49  eInUsingClause(I
27ca8 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 2c 20 63  dList *pUsing, c
27ca9 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 29  onst char *zCol)
27caa 7b 0a 20 20 69 66 28 20 70 55 73 69 6e 67 20 29  {.  if( pUsing )
27cab 7b 0a 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20  {.    int k;.   
27cac 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69   for(k=0; k<pUsi
27cad 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20  ng->nId; k++){. 
27cae 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
27caf 53 74 72 49 43 6d 70 28 70 55 73 69 6e 67 2d 3e  StrICmp(pUsing->
27cb0 61 5b 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c  a[k].zName, zCol
27cb1 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b  )==0 ) return 1;
27cb2 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
27cb3 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn 0;.}.../*.**
27cb4 20 47 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20   Given the name 
27cb5 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  of a column of t
27cb6 68 65 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72  he form X.Y.Z or
27cb7 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20 5a 2c 20   Y.Z or just Z, 
27cb8 6c 6f 6f 6b 20 75 70 0a 2a 2a 20 74 68 61 74 20  look up.** that 
27cb9 6e 61 6d 65 20 69 6e 20 74 68 65 20 73 65 74 20  name in the set 
27cba 6f 66 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73  of source tables
27cbb 20 69 6e 20 70 53 72 63 4c 69 73 74 20 61 6e 64   in pSrcList and
27cbc 20 6d 61 6b 65 20 74 68 65 20 70 45 78 70 72 20   make the pExpr 
27cbd 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6e  .** expression n
27cbe 6f 64 65 20 72 65 66 65 72 20 62 61 63 6b 20 74  ode refer back t
27cbf 6f 20 74 68 61 74 20 73 6f 75 72 63 65 20 63 6f  o that source co
27cc0 6c 75 6d 6e 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  lumn.  The follo
27cc1 77 69 6e 67 20 63 68 61 6e 67 65 73 0a 2a 2a 20  wing changes.** 
27cc2 61 72 65 20 6d 61 64 65 20 74 6f 20 70 45 78 70  are made to pExp
27cc3 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 45 78 70  r:.**.**    pExp
27cc4 72 2d 3e 69 44 62 20 20 20 20 20 20 20 20 20 20  r->iDb          
27cc5 20 53 65 74 20 74 68 65 20 69 6e 64 65 78 20 69   Set the index i
27cc6 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 6f 66 20 74  n db->aDb[] of t
27cc7 68 65 20 64 61 74 61 62 61 73 65 20 58 0a 2a 2a  he database X.**
27cc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cc9 20 20 20 20 20 20 20 20 20 28 65 76 65 6e 20 69           (even i
27cca 66 20 58 20 69 73 20 69 6d 70 6c 69 65 64 29 2e  f X is implied).
27ccb 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 54  .**    pExpr->iT
27ccc 61 62 6c 65 20 20 20 20 20 20 20 20 53 65 74 20  able        Set 
27ccd 74 6f 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75  to the cursor nu
27cce 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62  mber for the tab
27ccf 6c 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 20  le obtained.**  
27cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cd1 20 20 20 20 20 20 20 66 72 6f 6d 20 70 53 72 63         from pSrc
27cd2 4c 69 73 74 2e 0a 2a 2a 20 20 20 20 70 45 78 70  List..**    pExp
27cd3 72 2d 3e 70 54 61 62 20 20 20 20 20 20 20 20 20  r->pTab         
27cd4 20 50 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 54   Points to the T
27cd5 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 6f  able structure o
27cd6 66 20 58 2e 59 20 28 65 76 65 6e 20 69 66 0a 2a  f X.Y (even if.*
27cd7 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
27cd8 20 20 20 20 20 20 20 20 20 20 58 20 61 6e 64 2f            X and/
27cd9 6f 72 20 59 20 61 72 65 20 69 6d 70 6c 69 65 64  or Y are implied
27cda 2e 29 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e  .).**    pExpr->
27cdb 69 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 53 65  iColumn       Se
27cdc 74 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20  t to the column 
27cdd 6e 75 6d 62 65 72 20 77 69 74 68 69 6e 20 74 68  number within th
27cde 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 70  e table..**    p
27cdf 45 78 70 72 2d 3e 6f 70 20 20 20 20 20 20 20 20  Expr->op        
27ce0 20 20 20 20 53 65 74 20 74 6f 20 54 4b 5f 43 4f      Set to TK_CO
27ce1 4c 55 4d 4e 2e 0a 2a 2a 20 20 20 20 70 45 78 70  LUMN..**    pExp
27ce2 72 2d 3e 70 4c 65 66 74 20 20 20 20 20 20 20 20  r->pLeft        
27ce3 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20   Any expression 
27ce4 74 68 69 73 20 70 6f 69 6e 74 73 20 74 6f 20 69  this points to i
27ce5 73 20 64 65 6c 65 74 65 64 0a 2a 2a 20 20 20 20  s deleted.**    
27ce6 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 20 20  pExpr->pRight   
27ce7 20 20 20 20 20 41 6e 79 20 65 78 70 72 65 73 73       Any express
27ce8 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20  ion this points 
27ce9 74 6f 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a  to is deleted..*
27cea 2a 0a 2a 2a 20 54 68 65 20 7a 44 62 20 76 61 72  *.** The zDb var
27ceb 69 61 62 6c 65 20 69 73 20 74 68 65 20 6e 61 6d  iable is the nam
27cec 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
27ced 65 20 28 74 68 65 20 22 58 22 29 2e 20 20 54 68  e (the "X").  Th
27cee 69 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65 0a  is value may be.
27cef 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20  ** NULL meaning 
27cf0 74 68 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 20  that name is of 
27cf1 74 68 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20  the form Y.Z or 
27cf2 5a 2e 20 20 41 6e 79 20 61 76 61 69 6c 61 62 6c  Z.  Any availabl
27cf3 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 61  e database.** ca
27cf4 6e 20 62 65 20 75 73 65 64 2e 20 20 54 68 65 20  n be used.  The 
27cf5 7a 54 61 62 6c 65 20 76 61 72 69 61 62 6c 65 20  zTable variable 
27cf6 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
27cf7 68 65 20 74 61 62 6c 65 20 28 74 68 65 20 22 59  he table (the "Y
27cf8 22 29 2e 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c  ").  This.** val
27cf9 75 65 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69  ue can be NULL i
27cfa 66 20 7a 44 62 20 69 73 20 61 6c 73 6f 20 4e 55  f zDb is also NU
27cfb 4c 4c 2e 20 20 49 66 20 7a 54 61 62 6c 65 20 69  LL.  If zTable i
27cfc 73 20 4e 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65 61  s NULL it.** mea
27cfd 6e 73 20 74 68 61 74 20 74 68 65 20 66 6f 72 6d  ns that the form
27cfe 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20   of the name is 
27cff 5a 20 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d  Z and that colum
27d00 6e 73 20 66 72 6f 6d 20 61 6e 79 20 74 61 62 6c  ns from any tabl
27d01 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64  e.** can be used
27d02 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e  ..**.** If the n
27d03 61 6d 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65  ame cannot be re
27d04 73 6f 6c 76 65 64 20 75 6e 61 6d 62 69 67 75 6f  solved unambiguo
27d05 75 73 6c 79 2c 20 6c 65 61 76 65 20 61 6e 20 65  usly, leave an e
27d06 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20  rror message.** 
27d07 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65  in pParse and re
27d08 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 2e 20  turn WRC_Abort. 
27d09 20 52 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   Return WRC_Prun
27d0a 65 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f  e on success..*/
27d0b 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f 6b  .static int look
27d0c 75 70 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20  upName(.  Parse 
27d0d 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f  *pParse,       /
27d0e 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
27d0f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ntext */.  const
27d10 20 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20   char *zDb,     
27d11 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64  /* Name of the d
27d12 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
27d13 6e 67 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 4c  ng table, or NUL
27d14 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  L */.  const cha
27d15 72 20 2a 7a 54 61 62 2c 20 20 20 20 2f 2a 20 4e  r *zTab,    /* N
27d16 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 63 6f 6e  ame of table con
27d17 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20  taining column, 
27d18 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e  or NULL */.  con
27d19 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20  st char *zCol,  
27d1a 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
27d1b 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e 61   column. */.  Na
27d1c 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20  meContext *pNC, 
27d1d 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63     /* The name c
27d1e 6f 6e 74 65 78 74 20 75 73 65 64 20 74 6f 20 72  ontext used to r
27d1f 65 73 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 20  esolve the name 
27d20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
27d21 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b            /* Mak
27d22 65 20 74 68 69 73 20 45 58 50 52 20 6e 6f 64 65  e this EXPR node
27d23 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 65   point to the se
27d24 6c 65 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f  lected column */
27d25 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20  .){.  int i, j; 
27d26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27d27 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
27d28 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e  counters */.  in
27d29 74 20 63 6e 74 20 3d 20 30 3b 20 20 20 20 20 20  t cnt = 0;      
27d2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27d2b 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74  /* Number of mat
27d2c 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ching column nam
27d2d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 54  es */.  int cntT
27d2e 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ab = 0;         
27d2f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
27d30 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 20  ber of matching 
27d31 74 61 62 6c 65 20 6e 61 6d 65 73 20 2a 2f 0a 20  table names */. 
27d32 20 69 6e 74 20 6e 53 75 62 71 75 65 72 79 20 3d   int nSubquery =
27d33 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
27d34 20 20 20 2f 2a 20 48 6f 77 20 6d 61 6e 79 20 6c     /* How many l
27d35 65 76 65 6c 73 20 6f 66 20 73 75 62 71 75 65 72  evels of subquer
27d36 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  y */.  sqlite3 *
27d37 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
27d38 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
27d39 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
27d3a 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ion */.  struct 
27d3b 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
27d3c 74 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73  tem;       /* Us
27d3d 65 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  e for looping ov
27d3e 65 72 20 70 53 72 63 4c 69 73 74 20 69 74 65 6d  er pSrcList item
27d3f 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  s */.  struct Sr
27d40 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4d 61 74  cList_item *pMat
27d41 63 68 20 3d 20 30 3b 20 20 2f 2a 20 54 68 65 20  ch = 0;  /* The 
27d42 6d 61 74 63 68 69 6e 67 20 70 53 72 63 4c 69 73  matching pSrcLis
27d43 74 20 69 74 65 6d 20 2a 2f 0a 20 20 4e 61 6d 65  t item */.  Name
27d44 43 6f 6e 74 65 78 74 20 2a 70 54 6f 70 4e 43 20  Context *pTopNC 
27d45 3d 20 70 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a  = pNC;        /*
27d46 20 46 69 72 73 74 20 6e 61 6d 65 63 6f 6e 74 65   First nameconte
27d47 78 74 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a  xt in the list *
27d48 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68  /.  Schema *pSch
27d49 65 6d 61 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ema = 0;        
27d4a 20 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20        /* Schema 
27d4b 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
27d4c 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 72 69  n */.  int isTri
27d4d 67 67 65 72 20 3d 20 30 3b 0a 0a 20 20 61 73 73  gger = 0;..  ass
27d4e 65 72 74 28 20 70 4e 43 20 29 3b 20 20 20 20 20  ert( pNC );     
27d4f 2f 2a 20 74 68 65 20 6e 61 6d 65 20 63 6f 6e 74  /* the name cont
27d50 65 78 74 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55  ext cannot be NU
27d51 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  LL. */.  assert(
27d52 20 7a 43 6f 6c 20 29 3b 20 20 20 20 2f 2a 20 54   zCol );    /* T
27d53 68 65 20 5a 20 69 6e 20 58 2e 59 2e 5a 20 63 61  he Z in X.Y.Z ca
27d54 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a  nnot be NULL */.
27d55 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
27d56 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45  asAnyProperty(pE
27d57 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  xpr, EP_TokenOnl
27d58 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b  y|EP_Reduced) );
27d59 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
27d5a 65 20 74 68 65 20 6e 6f 64 65 20 74 6f 20 6e 6f  e the node to no
27d5b 2d 6d 61 74 63 68 20 2a 2f 0a 20 20 70 45 78 70  -match */.  pExp
27d5c 72 2d 3e 69 54 61 62 6c 65 20 3d 20 2d 31 3b 0a  r->iTable = -1;.
27d5d 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20    pExpr->pTab = 
27d5e 30 3b 0a 20 20 45 78 70 72 53 65 74 49 72 72 65  0;.  ExprSetIrre
27d5f 64 75 63 69 62 6c 65 28 70 45 78 70 72 29 3b 0a  ducible(pExpr);.
27d60 0a 20 20 2f 2a 20 53 74 61 72 74 20 61 74 20 74  .  /* Start at t
27d61 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63 6f  he inner-most co
27d62 6e 74 65 78 74 20 61 6e 64 20 6d 6f 76 65 20 6f  ntext and move o
27d63 75 74 77 61 72 64 20 75 6e 74 69 6c 20 61 20 6d  utward until a m
27d64 61 74 63 68 20 69 73 20 66 6f 75 6e 64 20 2a 2f  atch is found */
27d65 0a 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26 26  .  while( pNC &&
27d66 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 45   cnt==0 ){.    E
27d67 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
27d68 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53  .    SrcList *pS
27d69 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53  rcList = pNC->pS
27d6a 72 63 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66 28  rcList;..    if(
27d6b 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 20   pSrcList ){.   
27d6c 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65     for(i=0, pIte
27d6d 6d 3d 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 69  m=pSrcList->a; i
27d6e 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b  <pSrcList->nSrc;
27d6f 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
27d70 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70          Table *p
27d71 54 61 62 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  Tab;.        int
27d72 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 43 6f   iDb;.        Co
27d73 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a 20  lumn *pCol;.  . 
27d74 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 49         pTab = pI
27d75 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20  tem->pTab;.     
27d76 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21     assert( pTab!
27d77 3d 30 20 26 26 20 70 54 61 62 2d 3e 7a 4e 61 6d  =0 && pTab->zNam
27d78 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  e!=0 );.        
27d79 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
27d7a 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
27d7b 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
27d7c 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
27d7d 54 61 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20  Tab->nCol>0 );. 
27d7e 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 62 20         if( zTab 
27d7f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
27d80 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29   pItem->zAlias )
27d81 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  {.            ch
27d82 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70  ar *zTabName = p
27d83 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20  Item->zAlias;.  
27d84 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71            if( sq
27d85 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61  lite3StrICmp(zTa
27d86 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20  bName, zTab)!=0 
27d87 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
27d88 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
27d89 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
27d8a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e  TabName = pTab->
27d8b 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20  zName;.         
27d8c 20 20 20 69 66 28 20 4e 45 56 45 52 28 7a 54 61     if( NEVER(zTa
27d8d 62 4e 61 6d 65 3d 3d 30 29 20 7c 7c 20 73 71 6c  bName==0) || sql
27d8e 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61 62  ite3StrICmp(zTab
27d8f 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20 29  Name, zTab)!=0 )
27d90 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
27d91 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
27d92 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
27d93 20 20 20 20 69 66 28 20 7a 44 62 21 3d 30 20 26      if( zDb!=0 &
27d94 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
27d95 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e  (db->aDb[iDb].zN
27d96 61 6d 65 2c 20 7a 44 62 29 21 3d 30 20 29 7b 0a  ame, zDb)!=0 ){.
27d97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
27d98 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
27d99 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
27d9a 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
27d9b 20 20 20 20 69 66 28 20 30 3d 3d 28 63 6e 74 54      if( 0==(cntT
27d9c 61 62 2b 2b 29 20 29 7b 0a 20 20 20 20 20 20 20  ab++) ){.       
27d9d 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
27d9e 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f   = pItem->iCurso
27d9f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70 45 78  r;.          pEx
27da0 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b  pr->pTab = pTab;
27da1 0a 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65  .          pSche
27da2 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65  ma = pTab->pSche
27da3 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d  ma;.          pM
27da4 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20  atch = pItem;.  
27da5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
27da6 66 6f 72 28 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54  for(j=0, pCol=pT
27da7 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62  ab->aCol; j<pTab
27da8 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f  ->nCol; j++, pCo
27da9 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  l++){.          
27daa 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
27dab 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20  mp(pCol->zName, 
27dac 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20  zCol)==0 ){.    
27dad 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
27dae 65 72 65 20 68 61 73 20 62 65 65 6e 20 65 78 61  ere has been exa
27daf 63 74 6c 79 20 6f 6e 65 20 70 72 69 6f 72 20 6d  ctly one prior m
27db0 61 74 63 68 20 61 6e 64 20 74 68 69 73 20 6d 61  atch and this ma
27db1 74 63 68 0a 20 20 20 20 20 20 20 20 20 20 20 20  tch.            
27db2 2a 2a 20 69 73 20 66 6f 72 20 74 68 65 20 72 69  ** is for the ri
27db3 67 68 74 2d 68 61 6e 64 20 74 61 62 6c 65 20 6f  ght-hand table o
27db4 66 20 61 20 4e 41 54 55 52 41 4c 20 4a 4f 49 4e  f a NATURAL JOIN
27db5 20 6f 72 20 69 73 20 69 6e 20 61 20 0a 20 20 20   or is in a .   
27db6 20 20 20 20 20 20 20 20 20 2a 2a 20 55 53 49 4e           ** USIN
27db7 47 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 73  G clause, then s
27db8 6b 69 70 20 74 68 69 73 20 6d 61 74 63 68 2e 0a  kip this match..
27db9 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
27dba 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63             if( c
27dbb 6e 74 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  nt==1 ){.       
27dbc 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d         if( pItem
27dbd 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f  ->jointype & JT_
27dbe 4e 41 54 55 52 41 4c 20 29 20 63 6f 6e 74 69 6e  NATURAL ) contin
27dbf 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ue;.            
27dc0 20 20 69 66 28 20 6e 61 6d 65 49 6e 55 73 69 6e    if( nameInUsin
27dc1 67 43 6c 61 75 73 65 28 70 49 74 65 6d 2d 3e 70  gClause(pItem->p
27dc2 55 73 69 6e 67 2c 20 7a 43 6f 6c 29 20 29 20 63  Using, zCol) ) c
27dc3 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
27dc4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
27dc5 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20     cnt++;.      
27dc6 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61        pExpr->iTa
27dc7 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75  ble = pItem->iCu
27dc8 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20  rsor;.          
27dc9 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20    pExpr->pTab = 
27dca 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20  pTab;.          
27dcb 20 20 70 4d 61 74 63 68 20 3d 20 70 49 74 65 6d    pMatch = pItem
27dcc 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 53  ;.            pS
27dcd 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53  chema = pTab->pS
27dce 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 20  chema;.         
27dcf 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65     /* Substitute
27dd0 20 74 68 65 20 72 6f 77 69 64 20 28 63 6f 6c 75   the rowid (colu
27dd1 6d 6e 20 2d 31 29 20 66 6f 72 20 74 68 65 20 49  mn -1) for the I
27dd2 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
27dd3 45 59 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  EY */.          
27dd4 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e    pExpr->iColumn
27dd5 20 3d 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65   = j==pTab->iPKe
27dd6 79 20 3f 20 2d 31 20 3a 20 28 69 31 36 29 6a 3b  y ? -1 : (i16)j;
27dd7 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
27dd8 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
27dd9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
27dda 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66  }.    }..#ifndef
27ddb 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
27ddc 47 47 45 52 0a 20 20 20 20 2f 2a 20 49 66 20 77  GGER.    /* If w
27ddd 65 20 68 61 76 65 20 6e 6f 74 20 61 6c 72 65 61  e have not alrea
27dde 64 79 20 72 65 73 6f 6c 76 65 64 20 74 68 65 20  dy resolved the 
27ddf 6e 61 6d 65 2c 20 74 68 65 6e 20 6d 61 79 62 65  name, then maybe
27de0 20 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 61   .    ** it is a
27de1 20 6e 65 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20   new.* or old.* 
27de2 74 72 69 67 67 65 72 20 61 72 67 75 6d 65 6e 74  trigger argument
27de3 20 72 65 66 65 72 65 6e 63 65 0a 20 20 20 20 2a   reference.    *
27de4 2f 0a 20 20 20 20 69 66 28 20 7a 44 62 3d 3d 30  /.    if( zDb==0
27de5 20 26 26 20 7a 54 61 62 21 3d 30 20 26 26 20 63   && zTab!=0 && c
27de6 6e 74 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d  nt==0 && pParse-
27de7 3e 70 54 72 69 67 67 65 72 54 61 62 21 3d 30 20  >pTriggerTab!=0 
27de8 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 20  ){.      int op 
27de9 3d 20 70 50 61 72 73 65 2d 3e 65 54 72 69 67 67  = pParse->eTrigg
27dea 65 72 4f 70 3b 0a 20 20 20 20 20 20 54 61 62 6c  erOp;.      Tabl
27deb 65 20 2a 70 54 61 62 20 3d 20 30 3b 0a 20 20 20  e *pTab = 0;.   
27dec 20 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 54     assert( op==T
27ded 4b 5f 44 45 4c 45 54 45 20 7c 7c 20 6f 70 3d 3d  K_DELETE || op==
27dee 54 4b 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70 3d  TK_UPDATE || op=
27def 3d 54 4b 5f 49 4e 53 45 52 54 20 29 3b 0a 20 20  =TK_INSERT );.  
27df0 20 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 44      if( op!=TK_D
27df1 45 4c 45 54 45 20 26 26 20 73 71 6c 69 74 65 33  ELETE && sqlite3
27df2 53 74 72 49 43 6d 70 28 22 6e 65 77 22 2c 7a 54  StrICmp("new",zT
27df3 61 62 29 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20  ab) == 0 ){.    
27df4 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
27df5 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70  e = 1;.        p
27df6 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 54  Tab = pParse->pT
27df7 72 69 67 67 65 72 54 61 62 3b 0a 20 20 20 20 20  riggerTab;.     
27df8 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 21 3d 54   }else if( op!=T
27df9 4b 5f 49 4e 53 45 52 54 20 26 26 20 73 71 6c 69  K_INSERT && sqli
27dfa 74 65 33 53 74 72 49 43 6d 70 28 22 6f 6c 64 22  te3StrICmp("old"
27dfb 2c 7a 54 61 62 29 3d 3d 30 20 29 7b 0a 20 20 20  ,zTab)==0 ){.   
27dfc 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
27dfd 6c 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  le = 0;.        
27dfe 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70  pTab = pParse->p
27dff 54 72 69 67 67 65 72 54 61 62 3b 0a 20 20 20 20  TriggerTab;.    
27e00 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 70    }..      if( p
27e01 54 61 62 20 29 7b 20 0a 20 20 20 20 20 20 20 20  Tab ){ .        
27e02 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20  int iCol;.      
27e03 20 20 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62    pSchema = pTab
27e04 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20  ->pSchema;.     
27e05 20 20 20 63 6e 74 54 61 62 2b 2b 3b 0a 20 20 20     cntTab++;.   
27e06 20 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b       for(iCol=0;
27e07 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c   iCol<pTab->nCol
27e08 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  ; iCol++){.     
27e09 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f       Column *pCo
27e0a 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b  l = &pTab->aCol[
27e0b 69 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 20 20 20  iCol];.         
27e0c 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
27e0d 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c  Cmp(pCol->zName,
27e0e 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20   zCol)==0 ){.   
27e0f 20 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f           if( iCo
27e10 6c 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29  l==pTab->iPKey )
27e11 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
27e12 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20  iCol = -1;.     
27e13 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
27e14 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27e15 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
27e16 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43  }.        if( iC
27e17 6f 6c 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 26  ol>=pTab->nCol &
27e18 26 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64  & sqlite3IsRowid
27e19 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20  (zCol) ){.      
27e1a 20 20 20 20 69 43 6f 6c 20 3d 20 2d 31 3b 20 20      iCol = -1;  
27e1b 20 20 20 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d        /* IMP: R-
27e1c 34 34 39 31 31 2d 35 35 31 32 34 20 2a 2f 0a 20  44911-55124 */. 
27e1d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
27e1e 20 69 66 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e   if( iCol<pTab->
27e1f 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  nCol ){.        
27e20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20    cnt++;.       
27e21 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b     if( iCol<0 ){
27e22 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78  .            pEx
27e23 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53  pr->affinity = S
27e24 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
27e25 52 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  R;.          }el
27e26 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54  se if( pExpr->iT
27e27 61 62 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  able==0 ){.     
27e28 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
27e29 20 69 43 6f 6c 3d 3d 33 31 20 29 3b 0a 20 20 20   iCol==31 );.   
27e2a 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73           testcas
27e2b 65 28 20 69 43 6f 6c 3d 3d 33 32 20 29 3b 0a 20  e( iCol==32 );. 
27e2c 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73             pPars
27e2d 65 2d 3e 6f 6c 64 6d 61 73 6b 20 7c 3d 20 28 69  e->oldmask |= (i
27e2e 43 6f 6c 3e 3d 33 32 20 3f 20 30 78 66 66 66 66  Col>=32 ? 0xffff
27e2f 66 66 66 66 20 3a 20 28 28 28 75 33 32 29 31 29  ffff : (((u32)1)
27e30 3c 3c 69 43 6f 6c 29 29 3b 0a 20 20 20 20 20 20  <<iCol));.      
27e31 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27e32 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
27e33 20 69 43 6f 6c 3d 3d 33 31 20 29 3b 0a 20 20 20   iCol==31 );.   
27e34 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73           testcas
27e35 65 28 20 69 43 6f 6c 3d 3d 33 32 20 29 3b 0a 20  e( iCol==32 );. 
27e36 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73             pPars
27e37 65 2d 3e 6e 65 77 6d 61 73 6b 20 7c 3d 20 28 69  e->newmask |= (i
27e38 43 6f 6c 3e 3d 33 32 20 3f 20 30 78 66 66 66 66  Col>=32 ? 0xffff
27e39 66 66 66 66 20 3a 20 28 28 28 75 33 32 29 31 29  ffff : (((u32)1)
27e3a 3c 3c 69 43 6f 6c 29 29 3b 0a 20 20 20 20 20 20  <<iCol));.      
27e3b 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
27e3c 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  pExpr->iColumn =
27e3d 20 28 69 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20   (i16)iCol;.    
27e3e 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61        pExpr->pTa
27e3f 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20  b = pTab;.      
27e40 20 20 20 20 69 73 54 72 69 67 67 65 72 20 3d 20      isTrigger = 
27e41 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
27e42 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
27e43 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
27e44 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
27e45 52 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 0a 20 20  R) */..    /*.  
27e46 20 20 2a 2a 20 50 65 72 68 61 70 73 20 74 68 65    ** Perhaps the
27e47 20 6e 61 6d 65 20 69 73 20 61 20 72 65 66 65 72   name is a refer
27e48 65 6e 63 65 20 74 6f 20 74 68 65 20 52 4f 57 49  ence to the ROWI
27e49 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  D.    */.    if(
27e4a 20 63 6e 74 3d 3d 30 20 26 26 20 63 6e 74 54 61   cnt==0 && cntTa
27e4b 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65 33 49  b==1 && sqlite3I
27e4c 73 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0a  sRowid(zCol) ){.
27e4d 20 20 20 20 20 20 63 6e 74 20 3d 20 31 3b 0a 20        cnt = 1;. 
27e4e 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c       pExpr->iCol
27e4f 75 6d 6e 20 3d 20 2d 31 3b 20 20 20 20 20 2f 2a  umn = -1;     /*
27e50 20 49 4d 50 3a 20 52 2d 34 34 39 31 31 2d 35 35   IMP: R-44911-55
27e51 31 32 34 20 2a 2f 0a 20 20 20 20 20 20 70 45 78  124 */.      pEx
27e52 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53  pr->affinity = S
27e53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
27e54 52 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  R;.    }..    /*
27e55 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 69  .    ** If the i
27e56 6e 70 75 74 20 69 73 20 6f 66 20 74 68 65 20 66  nput is of the f
27e57 6f 72 6d 20 5a 20 28 6e 6f 74 20 59 2e 5a 20 6f  orm Z (not Y.Z o
27e58 72 20 58 2e 59 2e 5a 29 20 74 68 65 6e 20 74 68  r X.Y.Z) then th
27e59 65 20 6e 61 6d 65 20 5a 0a 20 20 20 20 2a 2a 20  e name Z.    ** 
27e5a 6d 69 67 68 74 20 72 65 66 65 72 20 74 6f 20 61  might refer to a
27e5b 6e 20 72 65 73 75 6c 74 2d 73 65 74 20 61 6c 69  n result-set ali
27e5c 61 73 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e  as.  This happen
27e5d 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20  s, for example, 
27e5e 77 68 65 6e 0a 20 20 20 20 2a 2a 20 77 65 20 61  when.    ** we a
27e5f 72 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d  re resolving nam
27e60 65 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  es in the WHERE 
27e61 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 66 6f  clause of the fo
27e62 6c 6c 6f 77 69 6e 67 20 63 6f 6d 6d 61 6e 64 3a  llowing command:
27e63 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
27e64 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53     SELECT a+b AS
27e65 20 78 20 46 52 4f 4d 20 74 61 62 6c 65 20 57 48   x FROM table WH
27e66 45 52 45 20 78 3c 31 30 3b 0a 20 20 20 20 2a 2a  ERE x<10;.    **
27e67 0a 20 20 20 20 2a 2a 20 49 6e 20 63 61 73 65 73  .    ** In cases
27e68 20 6c 69 6b 65 20 74 68 69 73 2c 20 72 65 70 6c   like this, repl
27e69 61 63 65 20 70 45 78 70 72 20 77 69 74 68 20 61  ace pExpr with a
27e6a 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70   copy of the exp
27e6b 72 65 73 73 69 6f 6e 20 74 68 61 74 0a 20 20 20  ression that.   
27e6c 20 2a 2a 20 66 6f 72 6d 73 20 74 68 65 20 72 65   ** forms the re
27e6d 73 75 6c 74 20 73 65 74 20 65 6e 74 72 79 20 28  sult set entry (
27e6e 22 61 2b 62 22 20 69 6e 20 74 68 65 20 65 78 61  "a+b" in the exa
27e6f 6d 70 6c 65 29 20 61 6e 64 20 72 65 74 75 72 6e  mple) and return
27e70 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20   immediately..  
27e71 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74    ** Note that t
27e72 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  he expression in
27e73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
27e74 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65  should have alre
27e75 61 64 79 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20  ady been.    ** 
27e76 72 65 73 6f 6c 76 65 64 20 62 79 20 74 68 65 20  resolved by the 
27e77 74 69 6d 65 20 74 68 65 20 57 48 45 52 45 20 63  time the WHERE c
27e78 6c 61 75 73 65 20 69 73 20 72 65 73 6f 6c 76 65  lause is resolve
27e79 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  d..    */.    if
27e7a 28 20 63 6e 74 3d 3d 30 20 26 26 20 28 70 45 4c  ( cnt==0 && (pEL
27e7b 69 73 74 20 3d 20 70 4e 43 2d 3e 70 45 4c 69 73  ist = pNC->pELis
27e7c 74 29 21 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30  t)!=0 && zTab==0
27e7d 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d   ){.      for(j=
27e7e 30 3b 20 6a 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; j<pEList->nEx
27e7f 70 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; j++){.      
27e80 20 20 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 45    char *zAs = pE
27e81 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65  List->a[j].zName
27e82 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 41  ;.        if( zA
27e83 73 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53  s!=0 && sqlite3S
27e84 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c  trICmp(zAs, zCol
27e85 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
27e86 20 20 45 78 70 72 20 2a 70 4f 72 69 67 3b 0a 20    Expr *pOrig;. 
27e87 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
27e88 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30   pExpr->pLeft==0
27e89 20 26 26 20 70 45 78 70 72 2d 3e 70 52 69 67 68   && pExpr->pRigh
27e8a 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  t==0 );.        
27e8b 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
27e8c 3e 78 2e 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20  >x.pList==0 );. 
27e8d 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
27e8e 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63   pExpr->x.pSelec
27e8f 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  t==0 );.        
27e90 20 20 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 74    pOrig = pEList
27e91 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[j].pExpr;.  
27e92 20 20 20 20 20 20 20 20 69 66 28 20 28 70 4e 43          if( (pNC
27e93 2d 3e 6e 63 46 6c 61 67 73 26 4e 43 5f 41 6c 6c  ->ncFlags&NC_All
27e94 6f 77 41 67 67 29 3d 3d 30 20 26 26 20 45 78 70  owAgg)==0 && Exp
27e95 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f 72  rHasProperty(pOr
27e96 69 67 2c 20 45 50 5f 41 67 67 29 20 29 7b 0a 20  ig, EP_Agg) ){. 
27e97 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
27e98 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
27e99 65 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61 6c  e, "misuse of al
27e9a 69 61 73 65 64 20 61 67 67 72 65 67 61 74 65 20  iased aggregate 
27e9b 25 73 22 2c 20 7a 41 73 29 3b 0a 20 20 20 20 20  %s", zAs);.     
27e9c 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52         return WR
27e9d 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20  C_Abort;.       
27e9e 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72     }.          r
27e9f 65 73 6f 6c 76 65 41 6c 69 61 73 28 70 50 61 72  esolveAlias(pPar
27ea0 73 65 2c 20 70 45 4c 69 73 74 2c 20 6a 2c 20 70  se, pEList, j, p
27ea1 45 78 70 72 2c 20 22 22 2c 20 6e 53 75 62 71 75  Expr, "", nSubqu
27ea2 65 72 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ery);.          
27ea3 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  cnt = 1;.       
27ea4 20 20 20 70 4d 61 74 63 68 20 3d 20 30 3b 0a 20     pMatch = 0;. 
27ea5 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
27ea6 20 7a 54 61 62 3d 3d 30 20 26 26 20 7a 44 62 3d   zTab==0 && zDb=
27ea7 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =0 );.          
27ea8 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f  goto lookupname_
27ea9 65 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  end;.        }. 
27eaa 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20       } .    }.. 
27eab 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f     /* Advance to
27eac 20 74 68 65 20 6e 65 78 74 20 6e 61 6d 65 20 63   the next name c
27ead 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f  ontext.  The loo
27eae 70 20 77 69 6c 6c 20 65 78 69 74 20 77 68 65 6e  p will exit when
27eaf 20 65 69 74 68 65 72 0a 20 20 20 20 2a 2a 20 77   either.    ** w
27eb0 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 20 28  e have a match (
27eb1 63 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e 20 77  cnt>0) or when w
27eb2 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d  e run out of nam
27eb3 65 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20  e contexts..    
27eb4 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d  */.    if( cnt==
27eb5 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d  0 ){.      pNC =
27eb6 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20   pNC->pNext;.   
27eb7 20 20 20 6e 53 75 62 71 75 65 72 79 2b 2b 3b 0a     nSubquery++;.
27eb8 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a      }.  }..  /*.
27eb9 20 20 2a 2a 20 49 66 20 58 20 61 6e 64 20 59 20    ** If X and Y 
27eba 61 72 65 20 4e 55 4c 4c 20 28 69 6e 20 6f 74 68  are NULL (in oth
27ebb 65 72 20 77 6f 72 64 73 20 69 66 20 6f 6e 6c 79  er words if only
27ebc 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
27ebd 20 5a 20 69 73 0a 20 20 2a 2a 20 73 75 70 70 6c   Z is.  ** suppl
27ebe 69 65 64 29 20 61 6e 64 20 74 68 65 20 76 61 6c  ied) and the val
27ebf 75 65 20 6f 66 20 5a 20 69 73 20 65 6e 63 6c 6f  ue of Z is enclo
27ec0 73 65 64 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75  sed in double-qu
27ec1 6f 74 65 73 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  otes, then.  ** 
27ec2 5a 20 69 73 20 61 20 73 74 72 69 6e 67 20 6c 69  Z is a string li
27ec3 74 65 72 61 6c 20 69 66 20 69 74 20 64 6f 65 73  teral if it does
27ec4 6e 27 74 20 6d 61 74 63 68 20 61 6e 79 20 63 6f  n't match any co
27ec5 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 6e 20  lumn names.  In 
27ec6 74 68 61 74 0a 20 20 2a 2a 20 63 61 73 65 2c 20  that.  ** case, 
27ec7 77 65 20 6e 65 65 64 20 74 6f 20 72 65 74 75 72  we need to retur
27ec8 6e 20 72 69 67 68 74 20 61 77 61 79 20 61 6e 64  n right away and
27ec9 20 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 20 63 68   not make any ch
27eca 61 6e 67 65 73 20 74 6f 0a 20 20 2a 2a 20 70 45  anges to.  ** pE
27ecb 78 70 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42  xpr..  **.  ** B
27ecc 65 63 61 75 73 65 20 6e 6f 20 72 65 66 65 72 65  ecause no refere
27ecd 6e 63 65 20 77 61 73 20 6d 61 64 65 20 74 6f 20  nce was made to 
27ece 6f 75 74 65 72 20 63 6f 6e 74 65 78 74 73 2c 20  outer contexts, 
27ecf 74 68 65 20 70 4e 43 2d 3e 6e 52 65 66 0a 20 20  the pNC->nRef.  
27ed0 2a 2a 20 66 69 65 6c 64 73 20 61 72 65 20 6e 6f  ** fields are no
27ed1 74 20 63 68 61 6e 67 65 64 20 69 6e 20 61 6e 79  t changed in any
27ed2 20 63 6f 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a 20   context..  */. 
27ed3 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 7a   if( cnt==0 && z
27ed4 54 61 62 3d 3d 30 20 26 26 20 45 78 70 72 48 61  Tab==0 && ExprHa
27ed5 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
27ed6 45 50 5f 44 62 6c 51 75 6f 74 65 64 29 20 29 7b  EP_DblQuoted) ){
27ed7 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d  .    pExpr->op =
27ed8 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20   TK_STRING;.    
27ed9 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b  pExpr->pTab = 0;
27eda 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f  .    return WRC_
27edb 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Prune;.  }..  /*
27edc 0a 20 20 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 61  .  ** cnt==0 mea
27edd 6e 73 20 74 68 65 72 65 20 77 61 73 20 6e 6f 74  ns there was not
27ede 20 6d 61 74 63 68 2e 20 20 63 6e 74 3e 31 20 6d   match.  cnt>1 m
27edf 65 61 6e 73 20 74 68 65 72 65 20 77 65 72 65 20  eans there were 
27ee0 74 77 6f 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 65  two or.  ** more
27ee1 20 6d 61 74 63 68 65 73 2e 20 20 45 69 74 68 65   matches.  Eithe
27ee2 72 20 77 61 79 2c 20 77 65 20 68 61 76 65 20 61  r way, we have a
27ee3 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20  n error..  */.  
27ee4 69 66 28 20 63 6e 74 21 3d 31 20 29 7b 0a 20 20  if( cnt!=1 ){.  
27ee5 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45    const char *zE
27ee6 72 72 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 63  rr;.    zErr = c
27ee7 6e 74 3d 3d 30 20 3f 20 22 6e 6f 20 73 75 63 68  nt==0 ? "no such
27ee8 20 63 6f 6c 75 6d 6e 22 20 3a 20 22 61 6d 62 69   column" : "ambi
27ee9 67 75 6f 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d  guous column nam
27eea 65 22 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20  e";.    if( zDb 
27eeb 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
27eec 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
27eed 20 22 25 73 3a 20 25 73 2e 25 73 2e 25 73 22 2c   "%s: %s.%s.%s",
27eee 20 7a 45 72 72 2c 20 7a 44 62 2c 20 7a 54 61 62   zErr, zDb, zTab
27eef 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c  , zCol);.    }el
27ef0 73 65 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20  se if( zTab ){. 
27ef1 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
27ef2 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73  rMsg(pParse, "%s
27ef3 3a 20 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20  : %s.%s", zErr, 
27ef4 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20  zTab, zCol);.   
27ef5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
27ef6 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
27ef7 61 72 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20  arse, "%s: %s", 
27ef8 7a 45 72 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20  zErr, zCol);.   
27ef9 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63   }.    pParse->c
27efa 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b 0a  heckSchema = 1;.
27efb 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 45 72 72      pTopNC->nErr
27efc 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  ++;.  }..  /* If
27efd 20 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61   a column from a
27efe 20 74 61 62 6c 65 20 69 6e 20 70 53 72 63 4c 69   table in pSrcLi
27eff 73 74 20 69 73 20 72 65 66 65 72 65 6e 63 65 64  st is referenced
27f00 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 0a 20 20  , then record.  
27f01 2a 2a 20 74 68 69 73 20 66 61 63 74 20 69 6e 20  ** this fact in 
27f02 74 68 65 20 70 53 72 63 4c 69 73 74 2e 61 5b 5d  the pSrcList.a[]
27f03 2e 63 6f 6c 55 73 65 64 20 62 69 74 6d 61 73 6b  .colUsed bitmask
27f04 2e 20 20 43 6f 6c 75 6d 6e 20 30 20 63 61 75 73  .  Column 0 caus
27f05 65 73 0a 20 20 2a 2a 20 62 69 74 20 30 20 74 6f  es.  ** bit 0 to
27f06 20 62 65 20 73 65 74 2e 20 20 43 6f 6c 75 6d 6e   be set.  Column
27f07 20 31 20 73 65 74 73 20 62 69 74 20 31 2e 20 20   1 sets bit 1.  
27f08 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49  And so forth.  I
27f09 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d  f the.  ** colum
27f0a 6e 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65 61  n number is grea
27f0b 74 65 72 20 74 68 61 6e 20 74 68 65 20 6e 75 6d  ter than the num
27f0c 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20 74  ber of bits in t
27f0d 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a 20  he bitmask.  ** 
27f0e 74 68 65 6e 20 73 65 74 20 74 68 65 20 68 69 67  then set the hig
27f0f 68 2d 6f 72 64 65 72 20 62 69 74 20 6f 66 20 74  h-order bit of t
27f10 68 65 20 62 69 74 6d 61 73 6b 2e 0a 20 20 2a 2f  he bitmask..  */
27f11 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43  .  if( pExpr->iC
27f12 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 4d 61 74  olumn>=0 && pMat
27f13 63 68 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ch!=0 ){.    int
27f14 20 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c   n = pExpr->iCol
27f15 75 6d 6e 3b 0a 20 20 20 20 74 65 73 74 63 61 73  umn;.    testcas
27f16 65 28 20 6e 3d 3d 42 4d 53 2d 31 20 29 3b 0a 20  e( n==BMS-1 );. 
27f17 20 20 20 69 66 28 20 6e 3e 3d 42 4d 53 20 29 7b     if( n>=BMS ){
27f18 0a 20 20 20 20 20 20 6e 20 3d 20 42 4d 53 2d 31  .      n = BMS-1
27f19 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
27f1a 72 74 28 20 70 4d 61 74 63 68 2d 3e 69 43 75 72  rt( pMatch->iCur
27f1b 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62  sor==pExpr->iTab
27f1c 6c 65 20 29 3b 0a 20 20 20 20 70 4d 61 74 63 68  le );.    pMatch
27f1d 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 28 42  ->colUsed |= ((B
27f1e 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0a 20 20  itmask)1)<<n;.  
27f1f 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70  }..  /* Clean up
27f20 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a 2f   and return.  */
27f21 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
27f22 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 2d 3e  lete(db, pExpr->
27f23 70 4c 65 66 74 29 3b 0a 20 20 70 45 78 70 72 2d  pLeft);.  pExpr-
27f24 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 73 71  >pLeft = 0;.  sq
27f25 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
27f26 64 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  db, pExpr->pRigh
27f27 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69  t);.  pExpr->pRi
27f28 67 68 74 20 3d 20 30 3b 0a 20 20 70 45 78 70 72  ght = 0;.  pExpr
27f29 2d 3e 6f 70 20 3d 20 28 69 73 54 72 69 67 67 65  ->op = (isTrigge
27f2a 72 20 3f 20 54 4b 5f 54 52 49 47 47 45 52 20 3a  r ? TK_TRIGGER :
27f2b 20 54 4b 5f 43 4f 4c 55 4d 4e 29 3b 0a 6c 6f 6f   TK_COLUMN);.loo
27f2c 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20 20 69  kupname_end:.  i
27f2d 66 28 20 63 6e 74 3d 3d 31 20 29 7b 0a 20 20 20  f( cnt==1 ){.   
27f2e 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 20   assert( pNC!=0 
27f2f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75  );.    sqlite3Au
27f30 74 68 52 65 61 64 28 70 50 61 72 73 65 2c 20 70  thRead(pParse, p
27f31 45 78 70 72 2c 20 70 53 63 68 65 6d 61 2c 20 70  Expr, pSchema, p
27f32 4e 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b 0a 20  NC->pSrcList);. 
27f33 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20     /* Increment 
27f34 74 68 65 20 6e 52 65 66 20 76 61 6c 75 65 20 6f  the nRef value o
27f35 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74 65  n all name conte
27f36 78 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43 20 75  xts from TopNC u
27f37 70 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20  p to.    ** the 
27f38 70 6f 69 6e 74 20 77 68 65 72 65 20 74 68 65 20  point where the 
27f39 6e 61 6d 65 20 6d 61 74 63 68 65 64 2e 20 2a 2f  name matched. */
27f3a 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20  .    for(;;){.  
27f3b 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 70      assert( pTop
27f3c 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  NC!=0 );.      p
27f3d 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a 20  TopNC->nRef++;. 
27f3e 20 20 20 20 20 69 66 28 20 70 54 6f 70 4e 43 3d       if( pTopNC=
27f3f 3d 70 4e 43 20 29 20 62 72 65 61 6b 3b 0a 20 20  =pNC ) break;.  
27f40 20 20 20 20 70 54 6f 70 4e 43 20 3d 20 70 54 6f      pTopNC = pTo
27f41 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  pNC->pNext;.    
27f42 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43  }.    return WRC
27f43 5f 50 72 75 6e 65 3b 0a 20 20 7d 20 65 6c 73 65  _Prune;.  } else
27f44 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52   {.    return WR
27f45 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 7d 0a 0a  C_Abort;.  }.}..
27f46 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
27f47 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  nd return a poin
27f48 74 65 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73  ter to an expres
27f49 73 69 6f 6e 20 74 6f 20 6c 6f 61 64 20 74 68 65  sion to load the
27f4a 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 0a 2a 2a 20   column iCol.** 
27f4b 66 72 6f 6d 20 64 61 74 61 73 6f 75 72 63 65 20  from datasource 
27f4c 69 53 72 63 20 69 6e 20 53 72 63 4c 69 73 74 20  iSrc in SrcList 
27f4d 70 53 72 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  pSrc..*/.SQLITE_
27f4e 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
27f4f 6c 69 74 65 33 43 72 65 61 74 65 43 6f 6c 75 6d  lite3CreateColum
27f50 6e 45 78 70 72 28 73 71 6c 69 74 65 33 20 2a 64  nExpr(sqlite3 *d
27f51 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  b, SrcList *pSrc
27f52 2c 20 69 6e 74 20 69 53 72 63 2c 20 69 6e 74 20  , int iSrc, int 
27f53 69 43 6f 6c 29 7b 0a 20 20 45 78 70 72 20 2a 70  iCol){.  Expr *p
27f54 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c   = sqlite3ExprAl
27f55 6c 6f 63 28 64 62 2c 20 54 4b 5f 43 4f 4c 55 4d  loc(db, TK_COLUM
27f56 4e 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20  N, 0, 0);.  if( 
27f57 70 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  p ){.    struct 
27f58 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
27f59 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 69  tem = &pSrc->a[i
27f5a 53 72 63 5d 3b 0a 20 20 20 20 70 2d 3e 70 54 61  Src];.    p->pTa
27f5b 62 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b  b = pItem->pTab;
27f5c 0a 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d  .    p->iTable =
27f5d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b   pItem->iCursor;
27f5e 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 54 61 62  .    if( p->pTab
27f5f 2d 3e 69 50 4b 65 79 3d 3d 69 43 6f 6c 20 29 7b  ->iPKey==iCol ){
27f60 0a 20 20 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d  .      p->iColum
27f61 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c 73  n = -1;.    }els
27f62 65 7b 0a 20 20 20 20 20 20 70 2d 3e 69 43 6f 6c  e{.      p->iCol
27f63 75 6d 6e 20 3d 20 28 79 6e 56 61 72 29 69 43 6f  umn = (ynVar)iCo
27f64 6c 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  l;.      testcas
27f65 65 28 20 69 43 6f 6c 3d 3d 42 4d 53 20 29 3b 0a  e( iCol==BMS );.
27f66 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
27f67 69 43 6f 6c 3d 3d 42 4d 53 2d 31 20 29 3b 0a 20  iCol==BMS-1 );. 
27f68 20 20 20 20 20 70 49 74 65 6d 2d 3e 63 6f 6c 55       pItem->colU
27f69 73 65 64 20 7c 3d 20 28 28 42 69 74 6d 61 73 6b  sed |= ((Bitmask
27f6a 29 31 29 3c 3c 28 69 43 6f 6c 3e 3d 42 4d 53 20  )1)<<(iCol>=BMS 
27f6b 3f 20 42 4d 53 2d 31 20 3a 20 69 43 6f 6c 29 3b  ? BMS-1 : iCol);
27f6c 0a 20 20 20 20 7d 0a 20 20 20 20 45 78 70 72 53  .    }.    ExprS
27f6d 65 74 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  etProperty(p, EP
27f6e 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 20 20 7d 0a  _Resolved);.  }.
27f6f 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
27f70 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
27f71 65 20 69 73 20 63 61 6c 6c 62 61 63 6b 20 66 6f  e is callback fo
27f72 72 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70  r sqlite3WalkExp
27f73 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 6f 6c  r()..**.** Resol
27f74 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65  ve symbolic name
27f75 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e  s into TK_COLUMN
27f76 20 6f 70 65 72 61 74 6f 72 73 20 66 6f 72 20 74   operators for t
27f77 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 6e 6f  he current.** no
27f78 64 65 20 69 6e 20 74 68 65 20 65 78 70 72 65 73  de in the expres
27f79 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75  sion tree.  Retu
27f7a 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69 6e 75 65  rn 0 to continue
27f7b 20 74 68 65 20 73 65 61 72 63 68 20 64 6f 77 6e   the search down
27f7c 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 6f 72 20  .** the tree or 
27f7d 32 20 74 6f 20 61 62 6f 72 74 20 74 68 65 20 74  2 to abort the t
27f7e 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20  ree walk..**.** 
27f7f 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73  This routine als
27f80 6f 20 64 6f 65 73 20 65 72 72 6f 72 20 63 68 65  o does error che
27f81 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d 65 20 72  cking and name r
27f82 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72 0a 2a 2a  esolution for.**
27f83 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 2e   function names.
27f84 20 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20 66    The operator f
27f85 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  or aggregate fun
27f86 63 74 69 6f 6e 73 20 69 73 20 63 68 61 6e 67 65  ctions is change
27f87 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47 47 5f 46  d.** to TK_AGG_F
27f88 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74  UNCTION..*/.stat
27f89 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 45 78  ic int resolveEx
27f8a 70 72 53 74 65 70 28 57 61 6c 6b 65 72 20 2a 70  prStep(Walker *p
27f8b 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45  Walker, Expr *pE
27f8c 78 70 72 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74  xpr){.  NameCont
27f8d 65 78 74 20 2a 70 4e 43 3b 0a 20 20 50 61 72 73  ext *pNC;.  Pars
27f8e 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20 70 4e  e *pParse;..  pN
27f8f 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70  C = pWalker->u.p
27f90 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e  NC;.  assert( pN
27f91 43 21 3d 30 20 29 3b 0a 20 20 70 50 61 72 73 65  C!=0 );.  pParse
27f92 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a   = pNC->pParse;.
27f93 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
27f94 3d 3d 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73  ==pWalker->pPars
27f95 65 20 29 3b 0a 0a 20 20 69 66 28 20 45 78 70 72  e );..  if( Expr
27f96 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
27f97 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76 65  Expr, EP_Resolve
27f98 64 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  d) ) return WRC_
27f99 50 72 75 6e 65 3b 0a 20 20 45 78 70 72 53 65 74  Prune;.  ExprSet
27f9a 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
27f9b 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 23 69  EP_Resolved);.#i
27f9c 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69  fndef NDEBUG.  i
27f9d 66 28 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74  f( pNC->pSrcList
27f9e 20 26 26 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73   && pNC->pSrcLis
27f9f 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b 0a 20  t->nAlloc>0 ){. 
27fa0 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63     SrcList *pSrc
27fa1 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63  List = pNC->pSrc
27fa2 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b  List;.    int i;
27fa3 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
27fa4 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e  pNC->pSrcList->n
27fa5 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Src; i++){.     
27fa6 20 61 73 73 65 72 74 28 20 70 53 72 63 4c 69 73   assert( pSrcLis
27fa7 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3e  t->a[i].iCursor>
27fa8 3d 30 20 26 26 20 70 53 72 63 4c 69 73 74 2d 3e  =0 && pSrcList->
27fa9 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c 70 50 61  a[i].iCursor<pPa
27faa 72 73 65 2d 3e 6e 54 61 62 29 3b 0a 20 20 20 20  rse->nTab);.    
27fab 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73  }.  }.#endif.  s
27fac 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70  witch( pExpr->op
27fad 20 29 7b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64   ){..#if defined
27fae 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55  (SQLITE_ENABLE_U
27faf 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d  PDATE_DELETE_LIM
27fb0 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  IT) && !defined(
27fb1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
27fb2 55 45 52 59 29 0a 20 20 20 20 2f 2a 20 54 68 65  UERY).    /* The
27fb3 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61 74 6f   special operato
27fb4 72 20 54 4b 5f 52 4f 57 20 6d 65 61 6e 73 20 75  r TK_ROW means u
27fb5 73 65 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72  se the rowid for
27fb6 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20 2a   the first.    *
27fb7 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  * column in the 
27fb8 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68  FROM clause.  Th
27fb9 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 68  is is used by th
27fba 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 52 44 45  e LIMIT and ORDE
27fbb 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61 75  R BY.    ** clau
27fbc 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 6e  se processing on
27fbd 20 55 50 44 41 54 45 20 61 6e 64 20 44 45 4c 45   UPDATE and DELE
27fbe 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20  TE statements.. 
27fbf 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54     */.    case T
27fc0 4b 5f 52 4f 57 3a 20 7b 0a 20 20 20 20 20 20 53  K_ROW: {.      S
27fc1 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74  rcList *pSrcList
27fc2 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74   = pNC->pSrcList
27fc3 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 53  ;.      struct S
27fc4 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
27fc5 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  em;.      assert
27fc6 28 20 70 53 72 63 4c 69 73 74 20 26 26 20 70 53  ( pSrcList && pS
27fc7 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20  rcList->nSrc==1 
27fc8 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 20 3d  );.      pItem =
27fc9 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 0a 20   pSrcList->a; . 
27fca 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d       pExpr->op =
27fcb 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20   TK_COLUMN;.    
27fcc 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20    pExpr->pTab = 
27fcd 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20  pItem->pTab;.   
27fce 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
27fcf 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f   = pItem->iCurso
27fd0 72 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  r;.      pExpr->
27fd1 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20  iColumn = -1;.  
27fd2 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69 6e      pExpr->affin
27fd3 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  ity = SQLITE_AFF
27fd4 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20  _INTEGER;.      
27fd5 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
27fd6 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53  dif /* defined(S
27fd7 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44  QLITE_ENABLE_UPD
27fd8 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54  ATE_DELETE_LIMIT
27fd9 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
27fda 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
27fdb 52 59 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41  RY) */..    /* A
27fdc 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66 69 65 72   lone identifier
27fdd 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
27fde 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f  a column..    */
27fdf 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 44 3a  .    case TK_ID:
27fe0 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   {.      return 
27fe1 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72 73  lookupName(pPars
27fe2 65 2c 20 30 2c 20 30 2c 20 70 45 78 70 72 2d 3e  e, 0, 0, pExpr->
27fe3 75 2e 7a 54 6f 6b 65 6e 2c 20 70 4e 43 2c 20 70  u.zToken, pNC, p
27fe4 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 0a  Expr);.    }.  .
27fe5 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 6e      /* A table n
27fe6 61 6d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e  ame and column n
27fe7 61 6d 65 3a 20 20 20 20 20 49 44 2e 49 44 0a 20  ame:     ID.ID. 
27fe8 20 20 20 2a 2a 20 4f 72 20 61 20 64 61 74 61 62     ** Or a datab
27fe9 61 73 65 2c 20 74 61 62 6c 65 20 61 6e 64 20 63  ase, table and c
27fea 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 2e 49 44  olumn:  ID.ID.ID
27feb 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
27fec 20 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20 20 20 20   TK_DOT: {.     
27fed 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
27fee 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73  lumn;.      cons
27fef 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 3b 0a  t char *zTable;.
27ff0 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
27ff1 20 2a 7a 44 62 3b 0a 20 20 20 20 20 20 45 78 70   *zDb;.      Exp
27ff2 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20 20 20 20  r *pRight;..    
27ff3 20 20 2f 2a 20 69 66 28 20 70 53 72 63 4c 69 73    /* if( pSrcLis
27ff4 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 20 2a 2f  t==0 ) break; */
27ff5 0a 20 20 20 20 20 20 70 52 69 67 68 74 20 3d 20  .      pRight = 
27ff6 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20  pExpr->pRight;. 
27ff7 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d       if( pRight-
27ff8 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20  >op==TK_ID ){.  
27ff9 20 20 20 20 20 20 7a 44 62 20 3d 20 30 3b 0a 20        zDb = 0;. 
27ffa 20 20 20 20 20 20 20 7a 54 61 62 6c 65 20 3d 20         zTable = 
27ffb 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e  pExpr->pLeft->u.
27ffc 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20  zToken;.        
27ffd 7a 43 6f 6c 75 6d 6e 20 3d 20 70 52 69 67 68 74  zColumn = pRight
27ffe 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20  ->u.zToken;.    
27fff 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
28000 20 61 73 73 65 72 74 28 20 70 52 69 67 68 74 2d   assert( pRight-
28001 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 3b 0a 20  >op==TK_DOT );. 
28002 20 20 20 20 20 20 20 7a 44 62 20 3d 20 70 45 78         zDb = pEx
28003 70 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f  pr->pLeft->u.zTo
28004 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 7a 54 61  ken;.        zTa
28005 62 6c 65 20 3d 20 70 52 69 67 68 74 2d 3e 70 4c  ble = pRight->pL
28006 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20  eft->u.zToken;. 
28007 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 20 3d         zColumn =
28008 20 70 52 69 67 68 74 2d 3e 70 52 69 67 68 74 2d   pRight->pRight-
28009 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  >u.zToken;.     
2800a 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   }.      return 
2800b 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72 73  lookupName(pPars
2800c 65 2c 20 7a 44 62 2c 20 7a 54 61 62 6c 65 2c 20  e, zDb, zTable, 
2800d 7a 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c 20 70 45  zColumn, pNC, pE
2800e 78 70 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  xpr);.    }..   
2800f 20 2f 2a 20 52 65 73 6f 6c 76 65 20 66 75 6e 63   /* Resolve func
28010 74 69 6f 6e 20 6e 61 6d 65 73 0a 20 20 20 20 2a  tion names.    *
28011 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f  /.    case TK_CO
28012 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63 61  NST_FUNC:.    ca
28013 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20  se TK_FUNCTION: 
28014 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74  {.      ExprList
28015 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d   *pList = pExpr-
28016 3e 78 2e 70 4c 69 73 74 3b 20 20 20 20 2f 2a 20  >x.pList;    /* 
28017 54 68 65 20 61 72 67 75 6d 65 6e 74 20 6c 69 73  The argument lis
28018 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  t */.      int n
28019 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c 69 73 74   = pList ? pList
2801a 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20 20 20 20  ->nExpr : 0;    
2801b 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67  /* Number of arg
2801c 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20  uments */.      
2801d 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63  int no_such_func
2801e 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 54   = 0;       /* T
2801f 72 75 65 20 69 66 20 6e 6f 20 73 75 63 68 20 66  rue if no such f
28020 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 20 2a  unction exists *
28021 2f 0a 20 20 20 20 20 20 69 6e 74 20 77 72 6f 6e  /.      int wron
28022 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 30 3b 20  g_num_args = 0; 
28023 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77      /* True if w
28024 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 61  rong number of a
28025 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  rguments */.    
28026 20 20 69 6e 74 20 69 73 5f 61 67 67 20 3d 20 30    int is_agg = 0
28027 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
28028 20 54 72 75 65 20 69 66 20 69 73 20 61 6e 20 61   True if is an a
28029 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
2802a 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61  n */.      int a
2802b 75 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  uth;            
2802c 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68 6f 72         /* Author
2802d 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73 65 20 74  ization to use t
2802e 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  he function */. 
2802f 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 20 20       int nId;   
28030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28031 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68   /* Number of ch
28032 61 72 61 63 74 65 72 73 20 69 6e 20 66 75 6e 63  aracters in func
28033 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  tion name */.   
28034 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
28035 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  Id;            /
28036 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 6e  * The function n
28037 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46 75  ame. */.      Fu
28038 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20  ncDef *pDef;    
28039 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66            /* Inf
2803a 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
2803b 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  he function */. 
2803c 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e       u8 enc = EN
2803d 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 20 20  C(pParse->db);  
2803e 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2803f 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 0a 20 20   encoding */..  
28040 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
28041 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4e 53  xpr->op==TK_CONS
28042 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20 20 20 20  T_FUNC );.      
28043 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
28044 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
28045 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
28046 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 70 45 78  .      zId = pEx
28047 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20  pr->u.zToken;.  
28048 20 20 20 20 6e 49 64 20 3d 20 73 71 6c 69 74 65      nId = sqlite
28049 33 53 74 72 6c 65 6e 33 30 28 7a 49 64 29 3b 0a  3Strlen30(zId);.
2804a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c        pDef = sql
2804b 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
2804c 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64  (pParse->db, zId
2804d 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c 20 30  , nId, n, enc, 0
2804e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 65  );.      if( pDe
2804f 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  f==0 ){.        
28050 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69  pDef = sqlite3Fi
28051 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73  ndFunction(pPars
28052 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c  e->db, zId, nId,
28053 20 2d 32 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20   -2, enc, 0);.  
28054 20 20 20 20 20 20 69 66 28 20 70 44 65 66 3d 3d        if( pDef==
28055 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  0 ){.          n
28056 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 31 3b  o_such_func = 1;
28057 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
28058 20 20 20 20 20 20 20 20 20 20 77 72 6f 6e 67 5f            wrong_
28059 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b 0a 20 20  num_args = 1;.  
2805a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
2805b 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 73 5f  lse{.        is_
2805c 61 67 67 20 3d 20 70 44 65 66 2d 3e 78 46 75 6e  agg = pDef->xFun
2805d 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69  c==0;.      }.#i
2805e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2805f 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
28060 20 20 20 20 20 20 69 66 28 20 70 44 65 66 20 29        if( pDef )
28061 7b 0a 20 20 20 20 20 20 20 20 61 75 74 68 20 3d  {.        auth =
28062 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
28063 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
28064 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c 20 70 44  _FUNCTION, 0, pD
28065 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  ef->zName, 0);. 
28066 20 20 20 20 20 20 20 69 66 28 20 61 75 74 68 21         if( auth!
28067 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
28068 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74 68          if( auth
28069 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b  ==SQLITE_DENY ){
2806a 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
2806b 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2806c 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f 72  rse, "not author
2806d 69 7a 65 64 20 74 6f 20 75 73 65 20 66 75 6e 63  ized to use func
2806e 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20 20 20 20  tion: %s",.     
2806f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
28071 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Def->zName);.   
28072 20 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45           pNC->nE
28073 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  rr++;.          
28074 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70  }.          pExp
28075 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b  r->op = TK_NULL;
28076 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
28077 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20  n WRC_Prune;.   
28078 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23       }.      }.#
28079 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20  endif.      if( 
2807a 69 73 5f 61 67 67 20 26 26 20 28 70 4e 43 2d 3e  is_agg && (pNC->
2807b 6e 63 46 6c 61 67 73 20 26 20 4e 43 5f 41 6c 6c  ncFlags & NC_All
2807c 6f 77 41 67 67 29 3d 3d 30 20 29 7b 0a 20 20 20  owAgg)==0 ){.   
2807d 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2807e 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69  rMsg(pParse, "mi
2807f 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74  suse of aggregat
28080 65 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28  e function %.*s(
28081 29 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a 20 20  )", nId,zId);.  
28082 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b        pNC->nErr+
28083 2b 3b 0a 20 20 20 20 20 20 20 20 69 73 5f 61 67  +;.        is_ag
28084 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c  g = 0;.      }el
28085 73 65 20 69 66 28 20 6e 6f 5f 73 75 63 68 5f 66  se if( no_such_f
28086 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73  unc ){.        s
28087 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
28088 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
28089 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 22 2c  function: %.*s",
2808a 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20   nId, zId);.    
2808b 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b      pNC->nErr++;
2808c 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
2808d 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20   wrong_num_args 
2808e 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2808f 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
28090 65 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20  e,"wrong number 
28091 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
28092 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22  function %.*s()"
28093 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 6e  ,.             n
28094 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20  Id, zId);.      
28095 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20    pNC->nErr++;. 
28096 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
28097 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e 6e   is_agg ) pNC->n
28098 63 46 6c 61 67 73 20 26 3d 20 7e 4e 43 5f 41 6c  cFlags &= ~NC_Al
28099 6c 6f 77 41 67 67 3b 0a 20 20 20 20 20 20 73 71  lowAgg;.      sq
2809a 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
2809b 74 28 70 57 61 6c 6b 65 72 2c 20 70 4c 69 73 74  t(pWalker, pList
2809c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f  );.      if( is_
2809d 61 67 67 20 29 7b 0a 20 20 20 20 20 20 20 20 4e  agg ){.        N
2809e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 32  ameContext *pNC2
2809f 20 3d 20 70 4e 43 3b 0a 20 20 20 20 20 20 20 20   = pNC;.        
280a0 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41  pExpr->op = TK_A
280a1 47 47 5f 46 55 4e 43 54 49 4f 4e 3b 0a 20 20 20  GG_FUNCTION;.   
280a2 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 32 20       pExpr->op2 
280a3 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 77 68 69  = 0;.        whi
280a4 6c 65 28 20 70 4e 43 32 20 26 26 20 21 73 71 6c  le( pNC2 && !sql
280a5 69 74 65 33 46 75 6e 63 74 69 6f 6e 55 73 65 73  ite3FunctionUses
280a6 54 68 69 73 53 72 63 28 70 45 78 70 72 2c 20 70  ThisSrc(pExpr, p
280a7 4e 43 32 2d 3e 70 53 72 63 4c 69 73 74 29 20 29  NC2->pSrcList) )
280a8 7b 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70  {.          pExp
280a9 72 2d 3e 6f 70 32 2b 2b 3b 0a 20 20 20 20 20 20  r->op2++;.      
280aa 20 20 20 20 70 4e 43 32 20 3d 20 70 4e 43 32 2d      pNC2 = pNC2-
280ab 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20  >pNext;.        
280ac 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e  }.        if( pN
280ad 43 32 20 29 20 70 4e 43 32 2d 3e 6e 63 46 6c 61  C2 ) pNC2->ncFla
280ae 67 73 20 7c 3d 20 4e 43 5f 48 61 73 41 67 67 3b  gs |= NC_HasAgg;
280af 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 63  .        pNC->nc
280b0 46 6c 61 67 73 20 7c 3d 20 4e 43 5f 41 6c 6c 6f  Flags |= NC_Allo
280b1 77 41 67 67 3b 0a 20 20 20 20 20 20 7d 0a 20 20  wAgg;.      }.  
280b2 20 20 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20 20      /* FIX ME:  
280b3 43 6f 6d 70 75 74 65 20 70 45 78 70 72 2d 3e 61  Compute pExpr->a
280b4 66 66 69 6e 69 74 79 20 62 61 73 65 64 20 6f 6e  ffinity based on
280b5 20 74 68 65 20 65 78 70 65 63 74 65 64 20 72 65   the expected re
280b6 74 75 72 6e 0a 20 20 20 20 20 20 2a 2a 20 74 79  turn.      ** ty
280b7 70 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  pe of the functi
280b8 6f 6e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  on .      */.   
280b9 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
280ba 75 6e 65 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  une;.    }.#ifnd
280bb 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
280bc 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 65  UBQUERY.    case
280bd 20 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 20 20   TK_SELECT:.    
280be 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 20  case TK_EXISTS: 
280bf 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72   testcase( pExpr
280c0 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20  ->op==TK_EXISTS 
280c1 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61  );.#endif.    ca
280c2 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20  se TK_IN: {.    
280c3 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
280c4 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a  r->op==TK_IN );.
280c5 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61        if( ExprHa
280c6 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
280c7 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
280c8 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 52  {.        int nR
280c9 65 66 20 3d 20 70 4e 43 2d 3e 6e 52 65 66 3b 0a  ef = pNC->nRef;.
280ca 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
280cb 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 20 20  MIT_CHECK.      
280cc 20 20 69 66 28 20 28 70 4e 43 2d 3e 6e 63 46 6c    if( (pNC->ncFl
280cd 61 67 73 20 26 20 4e 43 5f 49 73 43 68 65 63 6b  ags & NC_IsCheck
280ce 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
280cf 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
280d0 67 28 70 50 61 72 73 65 2c 22 73 75 62 71 75 65  g(pParse,"subque
280d1 72 69 65 73 20 70 72 6f 68 69 62 69 74 65 64 20  ries prohibited 
280d2 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61  in CHECK constra
280d3 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 20 20  ints");.        
280d4 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  }.#endif.       
280d5 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
280d6 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70  ct(pWalker, pExp
280d7 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20  r->x.pSelect);. 
280d8 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
280d9 4e 43 2d 3e 6e 52 65 66 3e 3d 6e 52 65 66 20 29  NC->nRef>=nRef )
280da 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 52  ;.        if( nR
280db 65 66 21 3d 70 4e 43 2d 3e 6e 52 65 66 20 29 7b  ef!=pNC->nRef ){
280dc 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 53  .          ExprS
280dd 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  etProperty(pExpr
280de 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29 3b  , EP_VarSelect);
280df 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
280e0 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
280e1 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
280e2 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a  LITE_OMIT_CHECK.
280e3 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49      case TK_VARI
280e4 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 69 66  ABLE: {.      if
280e5 28 20 28 70 4e 43 2d 3e 6e 63 46 6c 61 67 73 20  ( (pNC->ncFlags 
280e6 26 20 4e 43 5f 49 73 43 68 65 63 6b 29 21 3d 30  & NC_IsCheck)!=0
280e7 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
280e8 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
280e9 73 65 2c 22 70 61 72 61 6d 65 74 65 72 73 20 70  se,"parameters p
280ea 72 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48 45  rohibited in CHE
280eb 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29  CK constraints")
280ec 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
280ed 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
280ee 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  dif.  }.  return
280ef 20 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c   (pParse->nErr |
280f0 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  | pParse->db->ma
280f1 6c 6c 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52  llocFailed) ? WR
280f2 43 5f 41 62 6f 72 74 20 3a 20 57 52 43 5f 43 6f  C_Abort : WRC_Co
280f3 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ntinue;.}../*.**
280f4 20 70 45 4c 69 73 74 20 69 73 20 61 20 6c 69 73   pEList is a lis
280f5 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73  t of expressions
280f6 20 77 68 69 63 68 20 61 72 65 20 72 65 61 6c 6c   which are reall
280f7 79 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  y the result set
280f8 20 6f 66 20 74 68 65 0a 2a 2a 20 61 20 53 45 4c   of the.** a SEL
280f9 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
280fa 70 45 20 69 73 20 61 20 74 65 72 6d 20 69 6e 20  pE is a term in 
280fb 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47  an ORDER BY or G
280fc 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a  ROUP BY clause..
280fd 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
280fe 63 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66  checks to see if
280ff 20 70 45 20 69 73 20 61 20 73 69 6d 70 6c 65 20   pE is a simple 
28100 69 64 65 6e 74 69 66 69 65 72 20 77 68 69 63 68  identifier which
28101 20 63 6f 72 72 65 73 70 6f 6e 64 73 0a 2a 2a 20   corresponds.** 
28102 74 6f 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f  to the AS-name o
28103 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 65 72  f one of the ter
28104 6d 73 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  ms of the expres
28105 73 69 6f 6e 20 6c 69 73 74 2e 20 20 49 66 20 69  sion list.  If i
28106 74 20 69 73 2c 0a 2a 2a 20 74 68 69 73 20 72 6f  t is,.** this ro
28107 75 74 69 6e 65 20 72 65 74 75 72 6e 20 61 6e 20  utine return an 
28108 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e 20  integer between 
28109 31 20 61 6e 64 20 4e 20 77 68 65 72 65 20 4e 20  1 and N where N 
2810a 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
2810b 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20  .** elements in 
2810c 70 45 4c 69 73 74 2c 20 63 6f 72 72 65 73 70 6f  pEList, correspo
2810d 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 6d 61 74  nding to the mat
2810e 63 68 69 6e 67 20 65 6e 74 72 79 2e 20 20 49 66  ching entry.  If
2810f 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20   there is.** no 
28110 6d 61 74 63 68 2c 20 6f 72 20 69 66 20 70 45 20  match, or if pE 
28111 69 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20  is not a simple 
28112 69 64 65 6e 74 69 66 69 65 72 2c 20 74 68 65 6e  identifier, then
28113 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
28114 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a   return 0..**.**
28115 20 70 45 4c 69 73 74 20 68 61 73 20 62 65 65 6e   pEList has been
28116 20 72 65 73 6f 6c 76 65 64 2e 20 20 70 45 20 68   resolved.  pE h
28117 61 73 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69  as not..*/.stati
28118 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 41 73 4e  c int resolveAsN
28119 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ame(.  Parse *pP
2811a 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72  arse,     /* Par
2811b 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72  sing context for
2811c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
2811d 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2811e 45 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20  EList,  /* List 
2811f 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74  of expressions t
28120 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72  o scan */.  Expr
28121 20 2a 70 45 20 20 20 20 20 20 20 20 20 20 20 2f   *pE           /
28122 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 77 65 20  * Expression we 
28123 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 6d 61  are trying to ma
28124 74 63 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  tch */.){.  int 
28125 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  i;             /
28126 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
28127 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  /..  UNUSED_PARA
28128 4d 45 54 45 52 28 70 50 61 72 73 65 29 3b 0a 0a  METER(pParse);..
28129 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b    if( pE->op==TK
2812a 5f 49 44 20 29 7b 0a 20 20 20 20 63 68 61 72 20  _ID ){.    char 
2812b 2a 7a 43 6f 6c 20 3d 20 70 45 2d 3e 75 2e 7a 54  *zCol = pE->u.zT
2812c 6f 6b 65 6e 3b 0a 20 20 20 20 66 6f 72 28 69 3d  oken;.    for(i=
2812d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; i<pEList->nEx
2812e 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
2812f 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69  char *zAs = pELi
28130 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a  st->a[i].zName;.
28131 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30        if( zAs!=0
28132 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
28133 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30  mp(zAs, zCol)==0
28134 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
28135 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d 0a  rn i+1;.      }.
28136 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
28137 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70  rn 0;.}../*.** p
28138 45 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  E is a pointer t
28139 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  o an expression 
2813a 77 68 69 63 68 20 69 73 20 61 20 73 69 6e 67 6c  which is a singl
2813b 65 20 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a  e term in the.**
2813c 20 4f 52 44 45 52 20 42 59 20 6f 66 20 61 20 63   ORDER BY of a c
2813d 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 20  ompound SELECT. 
2813e 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
2813f 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20  has not been.** 
28140 6e 61 6d 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a  name resolved..*
28141 2a 0a 2a 2a 20 41 74 20 74 68 65 20 70 6f 69 6e  *.** At the poin
28142 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  t this routine i
28143 73 20 63 61 6c 6c 65 64 2c 20 77 65 20 61 6c 72  s called, we alr
28144 65 61 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 74  eady know that t
28145 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74  he.** ORDER BY t
28146 65 72 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e  erm is not an in
28147 74 65 67 65 72 20 69 6e 64 65 78 20 69 6e 74 6f  teger index into
28148 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
28149 20 20 54 68 61 74 0a 2a 2a 20 63 61 73 65 20 69    That.** case i
2814a 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65  s handled by the
2814b 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65   calling routine
2814c 2e 0a 2a 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20  ..**.** Attempt 
2814d 74 6f 20 6d 61 74 63 68 20 70 45 20 61 67 61 69  to match pE agai
2814e 6e 73 74 20 72 65 73 75 6c 74 20 73 65 74 20 63  nst result set c
2814f 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6c 65  olumns in the le
28150 66 74 2d 6d 6f 73 74 0a 2a 2a 20 53 45 4c 45 43  ft-most.** SELEC
28151 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 65  T statement.  Re
28152 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69  turn the index i
28153 20 6f 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67   of the matching
28154 20 63 6f 6c 75 6d 6e 2c 0a 2a 2a 20 61 73 20 61   column,.** as a
28155 6e 20 69 6e 64 69 63 61 74 69 6f 6e 20 74 6f 20  n indication to 
28156 74 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74 20  the caller that 
28157 69 74 20 73 68 6f 75 6c 64 20 73 6f 72 74 20 62  it should sort b
28158 79 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d  y the i-th colum
28159 6e 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d  n..** The left-m
2815a 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 31 2e  ost column is 1.
2815b 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
2815c 2c 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  , the value retu
2815d 72 6e 65 64 20 69 73 20 74 68 65 0a 2a 2a 20 73  rned is the.** s
2815e 61 6d 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75  ame integer valu
2815f 65 20 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20  e that would be 
28160 75 73 65 64 20 69 6e 20 74 68 65 20 53 51 4c 20  used in the SQL 
28161 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e 64  statement to ind
28162 69 63 61 74 65 0a 2a 2a 20 74 68 65 20 63 6f 6c  icate.** the col
28163 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  umn..**.** If th
28164 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c  ere is no match,
28165 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75   return 0.  Retu
28166 72 6e 20 2d 31 20 69 66 20 61 6e 20 65 72 72 6f  rn -1 if an erro
28167 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61  r occurs..*/.sta
28168 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f  tic int resolveO
28169 72 64 65 72 42 79 54 65 72 6d 54 6f 45 78 70 72  rderByTermToExpr
2816a 4c 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70  List(.  Parse *p
2816b 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61  Parse,     /* Pa
2816c 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f  rsing context fo
2816d 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  r error messages
2816e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
2816f 65 6c 65 63 74 2c 20 20 20 2f 2a 20 54 68 65 20  elect,   /* The 
28170 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
28171 20 77 69 74 68 20 74 68 65 20 4f 52 44 45 52 20   with the ORDER 
28172 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45  BY clause */.  E
28173 78 70 72 20 2a 70 45 20 20 20 20 20 20 20 20 20  xpr *pE         
28174 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69    /* The specifi
28175 63 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20  c ORDER BY term 
28176 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20  */.){.  int i;  
28177 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
28178 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
28179 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
2817a 3b 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e  ;  /* The column
2817b 73 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  s of the result 
2817c 73 65 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e  set */.  NameCon
2817d 74 65 78 74 20 6e 63 3b 20 20 20 20 2f 2a 20 4e  text nc;    /* N
2817e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ame context for 
2817f 72 65 73 6f 6c 76 69 6e 67 20 70 45 20 2a 2f 0a  resolving pE */.
28180 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
28181 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
28182 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
28183 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
28184 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
28185 64 65 20 66 72 6f 6d 20 73 75 62 70 72 6f 63 65  de from subproce
28186 64 75 72 65 73 20 2a 2f 0a 20 20 75 38 20 73 61  dures */.  u8 sa
28187 76 65 64 53 75 70 70 45 72 72 3b 20 20 20 2f 2a  vedSuppErr;   /*
28188 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20   Saved value of 
28189 64 62 2d 3e 73 75 70 70 72 65 73 73 45 72 72 20  db->suppressErr 
2818a 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  */..  assert( sq
2818b 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67  lite3ExprIsInteg
2818c 65 72 28 70 45 2c 20 26 69 29 3d 3d 30 20 29 3b  er(pE, &i)==0 );
2818d 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c  .  pEList = pSel
2818e 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20 20  ect->pEList;..  
2818f 2f 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c 20 6e  /* Resolve all n
28190 61 6d 65 73 20 69 6e 20 74 68 65 20 4f 52 44 45  ames in the ORDE
28191 52 20 42 59 20 74 65 72 6d 20 65 78 70 72 65 73  R BY term expres
28192 73 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73  sion.  */.  mems
28193 65 74 28 26 6e 63 2c 20 30 2c 20 73 69 7a 65 6f  et(&nc, 0, sizeo
28194 66 28 6e 63 29 29 3b 0a 20 20 6e 63 2e 70 50 61  f(nc));.  nc.pPa
28195 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
28196 6e 63 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53  nc.pSrcList = pS
28197 65 6c 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20 6e  elect->pSrc;.  n
28198 63 2e 70 45 4c 69 73 74 20 3d 20 70 45 4c 69 73  c.pEList = pELis
28199 74 3b 0a 20 20 6e 63 2e 6e 63 46 6c 61 67 73 20  t;.  nc.ncFlags 
2819a 3d 20 4e 43 5f 41 6c 6c 6f 77 41 67 67 3b 0a 20  = NC_AllowAgg;. 
2819b 20 6e 63 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20   nc.nErr = 0;.  
2819c 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2819d 0a 20 20 73 61 76 65 64 53 75 70 70 45 72 72 20  .  savedSuppErr 
2819e 3d 20 64 62 2d 3e 73 75 70 70 72 65 73 73 45 72  = db->suppressEr
2819f 72 3b 0a 20 20 64 62 2d 3e 73 75 70 70 72 65 73  r;.  db->suppres
281a0 73 45 72 72 20 3d 20 31 3b 0a 20 20 72 63 20 3d  sErr = 1;.  rc =
281a1 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
281a2 78 70 72 4e 61 6d 65 73 28 26 6e 63 2c 20 70 45  xprNames(&nc, pE
281a3 29 3b 0a 20 20 64 62 2d 3e 73 75 70 70 72 65 73  );.  db->suppres
281a4 73 45 72 72 20 3d 20 73 61 76 65 64 53 75 70 70  sErr = savedSupp
281a5 45 72 72 3b 0a 20 20 69 66 28 20 72 63 20 29 20  Err;.  if( rc ) 
281a6 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20  return 0;..  /* 
281a7 54 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65  Try to match the
281a8 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 73   ORDER BY expres
281a9 73 69 6f 6e 20 61 67 61 69 6e 73 74 20 61 6e 20  sion against an 
281aa 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20  expression.  ** 
281ab 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
281ac 74 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 31 2d  t.  Return an 1-
281ad 62 61 73 65 64 20 69 6e 64 65 78 20 6f 66 20 74  based index of t
281ae 68 65 20 6d 61 74 63 68 69 6e 67 0a 20 20 2a 2a  he matching.  **
281af 20 72 65 73 75 6c 74 2d 73 65 74 20 65 6e 74 72   result-set entr
281b0 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  y..  */.  for(i=
281b1 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; i<pEList->nEx
281b2 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  pr; i++){.    if
281b3 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d  ( sqlite3ExprCom
281b4 70 61 72 65 28 70 45 4c 69 73 74 2d 3e 61 5b 69  pare(pEList->a[i
281b5 5d 2e 70 45 78 70 72 2c 20 70 45 29 3c 32 20 29  ].pExpr, pE)<2 )
281b6 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  {.      return i
281b7 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  +1;.    }.  }.. 
281b8 20 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 2c   /* If no match,
281b9 20 72 65 74 75 72 6e 20 30 2e 20 2a 2f 0a 20 20   return 0. */.  
281ba 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
281bb 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 4f  ** Generate an O
281bc 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50  RDER BY or GROUP
281bd 20 42 59 20 74 65 72 6d 20 6f 75 74 2d 6f 66 2d   BY term out-of-
281be 72 61 6e 67 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a  range error..*/.
281bf 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f  static void reso
281c0 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72  lveOutOfRangeErr
281c1 6f 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  or(.  Parse *pPa
281c2 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rse,         /* 
281c3 54 68 65 20 65 72 72 6f 72 20 63 6f 6e 74 65 78  The error contex
281c4 74 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20  t into which to 
281c5 77 72 69 74 65 20 74 68 65 20 65 72 72 6f 72 20  write the error 
281c6 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
281c7 2a 7a 54 79 70 65 2c 20 20 20 20 20 2f 2a 20 22  *zType,     /* "
281c8 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50  ORDER" or "GROUP
281c9 22 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20  " */.  int i,   
281ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
281cb 20 54 68 65 20 69 6e 64 65 78 20 28 31 2d 62 61   The index (1-ba
281cc 73 65 64 29 20 6f 66 20 74 68 65 20 74 65 72 6d  sed) of the term
281cd 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f   out of range */
281ce 0a 20 20 69 6e 74 20 6d 78 20 20 20 20 20 20 20  .  int mx       
281cf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72            /* Lar
281d0 67 65 73 74 20 70 65 72 6d 69 73 73 69 62 6c 65  gest permissible
281d1 20 76 61 6c 75 65 20 6f 66 20 69 20 2a 2f 0a 29   value of i */.)
281d2 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  {.  sqlite3Error
281d3 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20  Msg(pParse, .   
281d4 20 22 25 72 20 25 73 20 42 59 20 74 65 72 6d 20   "%r %s BY term 
281d5 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73  out of range - s
281d6 68 6f 75 6c 64 20 62 65 20 22 0a 20 20 20 20 22  hould be ".    "
281d7 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 25 64  between 1 and %d
281d8 22 2c 20 69 2c 20 7a 54 79 70 65 2c 20 6d 78 29  ", i, zType, mx)
281d9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79  ;.}../*.** Analy
281da 7a 65 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  ze the ORDER BY 
281db 63 6c 61 75 73 65 20 69 6e 20 61 20 63 6f 6d 70  clause in a comp
281dc 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74  ound SELECT stat
281dd 65 6d 65 6e 74 2e 20 20 20 4d 6f 64 69 66 79 0a  ement.   Modify.
281de 2a 2a 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20  ** each term of 
281df 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
281e0 75 73 65 20 69 73 20 61 20 63 6f 6e 73 74 61 6e  use is a constan
281e1 74 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65  t integer betwee
281e2 6e 20 31 0a 2a 2a 20 61 6e 64 20 4e 20 77 68 65  n 1.** and N whe
281e3 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62  re N is the numb
281e4 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
281e5 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45   the compound SE
281e6 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 4f 52 44 45  LECT..**.** ORDE
281e7 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20  R BY terms that 
281e8 61 72 65 20 61 6c 72 65 61 64 79 20 61 6e 20 69  are already an i
281e9 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31  nteger between 1
281ea 20 61 6e 64 20 4e 20 61 72 65 0a 2a 2a 20 75 6e   and N are.** un
281eb 6d 6f 64 69 66 69 65 64 2e 20 20 4f 52 44 45 52  modified.  ORDER
281ec 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61   BY terms that a
281ed 72 65 20 69 6e 74 65 67 65 72 73 20 6f 75 74 73  re integers outs
281ee 69 64 65 20 74 68 65 20 72 61 6e 67 65 20 6f 66  ide the range of
281ef 0a 2a 2a 20 31 20 74 68 72 6f 75 67 68 20 4e 20  .** 1 through N 
281f0 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f  generate an erro
281f1 72 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72  r.  ORDER BY ter
281f2 6d 73 20 74 68 61 74 20 61 72 65 20 65 78 70 72  ms that are expr
281f3 65 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d  essions.** are m
281f4 61 74 63 68 65 64 20 61 67 61 69 6e 73 74 20 72  atched against r
281f5 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73  esult set expres
281f6 73 69 6f 6e 73 20 6f 66 20 63 6f 6d 70 6f 75 6e  sions of compoun
281f7 64 20 53 45 4c 45 43 54 0a 2a 2a 20 62 65 67 69  d SELECT.** begi
281f8 6e 6e 69 6e 67 20 77 69 74 68 20 74 68 65 20 6c  nning with the l
281f9 65 66 74 2d 6d 6f 73 74 20 53 45 4c 45 43 54 20  eft-most SELECT 
281fa 61 6e 64 20 77 6f 72 6b 69 6e 67 20 74 6f 77 61  and working towa
281fb 72 64 20 74 68 65 20 72 69 67 68 74 2e 0a 2a 2a  rd the right..**
281fc 20 41 74 20 74 68 65 20 66 69 72 73 74 20 6d 61   At the first ma
281fd 74 63 68 2c 20 74 68 65 20 4f 52 44 45 52 20 42  tch, the ORDER B
281fe 59 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  Y expression is 
281ff 74 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f  transformed into
28200 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20  .** the integer 
28201 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a  column number..*
28202 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
28203 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
28204 20 73 65 65 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63   seen..*/.static
28205 20 69 6e 74 20 72 65 73 6f 6c 76 65 43 6f 6d 70   int resolveComp
28206 6f 75 6e 64 4f 72 64 65 72 42 79 28 0a 20 20 50  oundOrderBy(.  P
28207 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
28208 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
28209 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20  context.  Leave 
2820a 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68  error messages h
2820b 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ere */.  Select 
2820c 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f  *pSelect       /
2820d 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
2820e 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e  tement containin
2820f 67 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 2a  g the ORDER BY *
28210 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
28211 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
28212 42 79 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  By;.  ExprList *
28213 70 45 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65  pEList;.  sqlite
28214 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 6d 6f 72  3 *db;.  int mor
28215 65 54 6f 44 6f 20 3d 20 31 3b 0a 0a 20 20 70 4f  eToDo = 1;..  pO
28216 72 64 65 72 42 79 20 3d 20 70 53 65 6c 65 63 74  rderBy = pSelect
28217 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 66  ->pOrderBy;.  if
28218 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20  ( pOrderBy==0 ) 
28219 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d  return 0;.  db =
2821a 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66   pParse->db;.#if
2821b 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55   SQLITE_MAX_COLU
2821c 4d 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42  MN.  if( pOrderB
2821d 79 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69  y->nExpr>db->aLi
2821e 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
2821f 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20  _COLUMN] ){.    
28220 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
28221 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e  pParse, "too man
28222 79 20 74 65 72 6d 73 20 69 6e 20 4f 52 44 45 52  y terms in ORDER
28223 20 42 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20   BY clause");.  
28224 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
28225 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 69 3d 30  #endif.  for(i=0
28226 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45  ; i<pOrderBy->nE
28227 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  xpr; i++){.    p
28228 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f  OrderBy->a[i].do
28229 6e 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 53  ne = 0;.  }.  pS
2822a 65 6c 65 63 74 2d 3e 70 4e 65 78 74 20 3d 20 30  elect->pNext = 0
2822b 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65  ;.  while( pSele
2822c 63 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20  ct->pPrior ){.  
2822d 20 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f    pSelect->pPrio
2822e 72 2d 3e 70 4e 65 78 74 20 3d 20 70 53 65 6c 65  r->pNext = pSele
2822f 63 74 3b 0a 20 20 20 20 70 53 65 6c 65 63 74 20  ct;.    pSelect 
28230 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f  = pSelect->pPrio
28231 72 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20  r;.  }.  while( 
28232 70 53 65 6c 65 63 74 20 26 26 20 6d 6f 72 65 54  pSelect && moreT
28233 6f 44 6f 20 29 7b 0a 20 20 20 20 73 74 72 75 63  oDo ){.    struc
28234 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
28235 2a 70 49 74 65 6d 3b 0a 20 20 20 20 6d 6f 72 65  *pItem;.    more
28236 54 6f 44 6f 20 3d 20 30 3b 0a 20 20 20 20 70 45  ToDo = 0;.    pE
28237 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  List = pSelect->
28238 70 45 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65  pEList;.    asse
28239 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b  rt( pEList!=0 );
2823a 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49  .    for(i=0, pI
2823b 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b  tem=pOrderBy->a;
2823c 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78   i<pOrderBy->nEx
2823d 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  pr; i++, pItem++
2823e 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f  ){.      int iCo
2823f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 45 78  l = -1;.      Ex
28240 70 72 20 2a 70 45 2c 20 2a 70 44 75 70 3b 0a 20  pr *pE, *pDup;. 
28241 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e       if( pItem->
28242 64 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b  done ) continue;
28243 0a 20 20 20 20 20 20 70 45 20 3d 20 70 49 74 65  .      pE = pIte
28244 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  m->pExpr;.      
28245 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49  if( sqlite3ExprI
28246 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43  sInteger(pE, &iC
28247 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ol) ){.        i
28248 66 28 20 69 43 6f 6c 3c 3d 30 20 7c 7c 20 69 43  f( iCol<=0 || iC
28249 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  ol>pEList->nExpr
2824a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
2824b 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45  solveOutOfRangeE
2824c 72 72 6f 72 28 70 50 61 72 73 65 2c 20 22 4f 52  rror(pParse, "OR
2824d 44 45 52 22 2c 20 69 2b 31 2c 20 70 45 4c 69 73  DER", i+1, pELis
2824e 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20  t->nExpr);.     
2824f 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
28250 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
28251 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 43  else{.        iC
28252 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61  ol = resolveAsNa
28253 6d 65 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73  me(pParse, pELis
28254 74 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20  t, pE);.        
28255 69 66 28 20 69 43 6f 6c 3d 3d 30 20 29 7b 0a 20  if( iCol==0 ){. 
28256 20 20 20 20 20 20 20 20 20 70 44 75 70 20 3d 20           pDup = 
28257 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
28258 62 2c 20 70 45 2c 20 30 29 3b 0a 20 20 20 20 20  b, pE, 0);.     
28259 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61       if( !db->ma
2825a 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2825b 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
2825c 28 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20  (pDup);.        
2825d 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c      iCol = resol
2825e 76 65 4f 72 64 65 72 42 79 54 65 72 6d 54 6f 45  veOrderByTermToE
2825f 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20  xprList(pParse, 
28260 70 53 65 6c 65 63 74 2c 20 70 44 75 70 29 3b 0a  pSelect, pDup);.
28261 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
28262 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
28263 72 44 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70  rDelete(db, pDup
28264 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
28265 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69     }.      if( i
28266 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  Col>0 ){.       
28267 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
28268 3d 20 70 45 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20  = pE->pColl;.   
28269 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d       int flags =
2826a 20 70 45 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f   pE->flags & EP_
2826b 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20  ExpCollate;.    
2826c 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2826d 65 6c 65 74 65 28 64 62 2c 20 70 45 29 3b 0a 20  elete(db, pE);. 
2826e 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 45         pItem->pE
2826f 78 70 72 20 3d 20 70 45 20 3d 20 73 71 6c 69 74  xpr = pE = sqlit
28270 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e  e3Expr(db, TK_IN
28271 54 45 47 45 52 2c 20 30 29 3b 0a 20 20 20 20 20  TEGER, 0);.     
28272 20 20 20 69 66 28 20 70 45 3d 3d 30 20 29 20 72     if( pE==0 ) r
28273 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20  eturn 1;.       
28274 20 70 45 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f   pE->pColl = pCo
28275 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e  ll;.        pE->
28276 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56  flags |= EP_IntV
28277 61 6c 75 65 20 7c 20 66 6c 61 67 73 3b 0a 20 20  alue | flags;.  
28278 20 20 20 20 20 20 70 45 2d 3e 75 2e 69 56 61 6c        pE->u.iVal
28279 75 65 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20  ue = iCol;.     
2827a 20 20 20 70 49 74 65 6d 2d 3e 69 4f 72 64 65 72     pItem->iOrder
2827b 42 79 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f  ByCol = (u16)iCo
2827c 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d  l;.        pItem
2827d 2d 3e 64 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20  ->done = 1;.    
2827e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2827f 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a 20   moreToDo = 1;. 
28280 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
28281 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65   pSelect = pSele
28282 63 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20  ct->pNext;.  }. 
28283 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64   for(i=0; i<pOrd
28284 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  erBy->nExpr; i++
28285 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65  ){.    if( pOrde
28286 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 3d 3d  rBy->a[i].done==
28287 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
28288 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
28289 65 2c 20 22 25 72 20 4f 52 44 45 52 20 42 59 20  e, "%r ORDER BY 
2828a 74 65 72 6d 20 64 6f 65 73 20 6e 6f 74 20 6d 61  term does not ma
2828b 74 63 68 20 61 6e 79 20 22 0a 20 20 20 20 20 20  tch any ".      
2828c 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 20 69 6e        "column in
2828d 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 22   the result set"
2828e 2c 20 69 2b 31 29 3b 0a 20 20 20 20 20 20 72 65  , i+1);.      re
2828f 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
28290 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
28291 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 65 76 65  ./*.** Check eve
28292 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65 20 4f  ry term in the O
28293 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50  RDER BY or GROUP
28294 20 42 59 20 63 6c 61 75 73 65 20 70 4f 72 64 65   BY clause pOrde
28295 72 42 79 20 6f 66 0a 2a 2a 20 74 68 65 20 53 45  rBy of.** the SE
28296 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70  LECT statement p
28297 53 65 6c 65 63 74 2e 20 20 49 66 20 61 6e 79 20  Select.  If any 
28298 74 65 72 6d 20 69 73 20 72 65 66 65 72 65 6e 63  term is referenc
28299 65 20 74 6f 20 61 0a 2a 2a 20 72 65 73 75 6c 74  e to a.** result
2829a 20 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20   set expression 
2829b 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62  (as determined b
2829c 79 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e 61  y the ExprList.a
2829d 2e 69 43 6f 6c 20 66 69 65 6c 64 29 0a 2a 2a 20  .iCol field).** 
2829e 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 68 61  then convert tha
2829f 74 20 74 65 72 6d 20 69 6e 74 6f 20 61 20 63 6f  t term into a co
282a0 70 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73  py of the corres
282a1 70 6f 6e 64 69 6e 67 20 72 65 73 75 6c 74 20 73  ponding result s
282a2 65 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a  et.** column..**
282a3 0a 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f 72  .** If any error
282a4 73 20 61 72 65 20 64 65 74 65 63 74 65 64 2c 20  s are detected, 
282a5 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  add an error mes
282a6 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 20 61  sage to pParse a
282a7 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e  nd.** return non
282a8 2d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a  -zero.  Return z
282a9 65 72 6f 20 69 66 20 6e 6f 20 65 72 72 6f 72 73  ero if no errors
282aa 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2f 0a 53 51   are seen..*/.SQ
282ab 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
282ac 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f   sqlite3ResolveO
282ad 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20 50  rderGroupBy(.  P
282ae 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
282af 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
282b0 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20  context.  Leave 
282b1 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68  error messages h
282b2 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ere */.  Select 
282b3 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f  *pSelect,      /
282b4 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
282b5 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e  tement containin
282b6 67 20 74 68 65 20 63 6c 61 75 73 65 20 2a 2f 0a  g the clause */.
282b7 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
282b8 65 72 42 79 2c 20 20 20 2f 2a 20 54 68 65 20 4f  erBy,   /* The O
282b9 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50  RDER BY or GROUP
282ba 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 62 65   BY clause to be
282bb 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20 20   processed */.  
282bc 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70  const char *zTyp
282bd 65 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22  e     /* "ORDER"
282be 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 29   or "GROUP" */.)
282bf 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c  {.  int i;.  sql
282c0 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
282c1 65 2d 3e 64 62 3b 0a 20 20 45 78 70 72 4c 69 73  e->db;.  ExprLis
282c2 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74 72  t *pEList;.  str
282c3 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
282c4 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28  m *pItem;..  if(
282c5 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 7c 7c 20   pOrderBy==0 || 
282c6 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
282c7 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72  ocFailed ) retur
282c8 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  n 0;.#if SQLITE_
282c9 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28  MAX_COLUMN.  if(
282ca 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72   pOrderBy->nExpr
282cb 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
282cc 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d  TE_LIMIT_COLUMN]
282cd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
282ce 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
282cf 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20  "too many terms 
282d0 69 6e 20 25 73 20 42 59 20 63 6c 61 75 73 65 22  in %s BY clause"
282d1 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 72 65  , zType);.    re
282d2 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64  turn 1;.  }.#end
282d3 69 66 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53  if.  pEList = pS
282d4 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20  elect->pEList;. 
282d5 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21   assert( pEList!
282d6 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65  =0 );  /* sqlite
282d7 33 53 65 6c 65 63 74 4e 65 77 28 29 20 67 75 61  3SelectNew() gua
282d8 72 61 6e 74 65 65 73 20 74 68 69 73 20 2a 2f 0a  rantees this */.
282d9 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d    for(i=0, pItem
282da 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c  =pOrderBy->a; i<
282db 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b  pOrderBy->nExpr;
282dc 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
282dd 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69      if( pItem->i
282de 4f 72 64 65 72 42 79 43 6f 6c 20 29 7b 0a 20 20  OrderByCol ){.  
282df 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69      if( pItem->i
282e0 4f 72 64 65 72 42 79 43 6f 6c 3e 70 45 4c 69 73  OrderByCol>pELis
282e1 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20  t->nExpr ){.    
282e2 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66      resolveOutOf
282e3 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73  RangeError(pPars
282e4 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 70  e, zType, i+1, p
282e5 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20  EList->nExpr);. 
282e6 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b         return 1;
282e7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
282e8 65 73 6f 6c 76 65 41 6c 69 61 73 28 70 50 61 72  esolveAlias(pPar
282e9 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 49 74 65  se, pEList, pIte
282ea 6d 2d 3e 69 4f 72 64 65 72 42 79 43 6f 6c 2d 31  m->iOrderByCol-1
282eb 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20  , pItem->pExpr, 
282ec 7a 54 79 70 65 2c 30 29 3b 0a 20 20 20 20 7d 0a  zType,0);.    }.
282ed 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
282ee 7d 0a 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 65 72 42  }../*.** pOrderB
282ef 79 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59  y is an ORDER BY
282f0 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61   or GROUP BY cla
282f1 75 73 65 20 69 6e 20 53 45 4c 45 43 54 20 73 74  use in SELECT st
282f2 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e  atement pSelect.
282f3 0a 2a 2a 20 54 68 65 20 4e 61 6d 65 20 63 6f 6e  .** The Name con
282f4 74 65 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45  text of the SELE
282f5 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  CT statement is 
282f6 70 4e 43 2e 20 20 7a 54 79 70 65 20 69 73 20 65  pNC.  zType is e
282f7 69 74 68 65 72 0a 2a 2a 20 22 4f 52 44 45 52 22  ither.** "ORDER"
282f8 20 6f 72 20 22 47 52 4f 55 50 22 20 64 65 70 65   or "GROUP" depe
282f9 6e 64 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 74  nding on which t
282fa 79 70 65 20 6f 66 20 63 6c 61 75 73 65 20 70 4f  ype of clause pO
282fb 72 64 65 72 42 79 20 69 73 2e 0a 2a 2a 0a 2a 2a  rderBy is..**.**
282fc 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
282fd 73 6f 6c 76 65 73 20 65 61 63 68 20 74 65 72 6d  solves each term
282fe 20 6f 66 20 74 68 65 20 63 6c 61 75 73 65 20 69   of the clause i
282ff 6e 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f  nto an expressio
28300 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6f 72 64  n..** If the ord
28301 65 72 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e  er-by term is an
28302 20 69 6e 74 65 67 65 72 20 49 20 62 65 74 77 65   integer I betwe
28303 65 6e 20 31 20 61 6e 64 20 4e 20 28 77 68 65 72  en 1 and N (wher
28304 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75  e N is the.** nu
28305 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
28306 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
28307 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 29  t of the SELECT)
28308 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 73   then the expres
28309 73 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72  sion.** in the r
2830a 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 61 20 63  esolution is a c
2830b 6f 70 79 20 6f 66 20 74 68 65 20 49 2d 74 68 20  opy of the I-th 
2830c 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65  result-set expre
2830d 73 73 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 74 68  ssion.  If.** th
2830e 65 20 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20  e order-by term 
2830f 69 73 20 61 6e 20 69 64 65 6e 74 69 66 79 20 74  is an identify t
28310 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  hat corresponds 
28311 74 6f 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f  to the AS-name o
28312 66 0a 2a 2a 20 61 20 72 65 73 75 6c 74 2d 73 65  f.** a result-se
28313 74 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68  t expression, th
28314 65 6e 20 74 68 65 20 74 65 72 6d 20 72 65 73 6f  en the term reso
28315 6c 76 65 73 20 74 6f 20 61 20 63 6f 70 79 20 6f  lves to a copy o
28316 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d  f the.** result-
28317 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  set expression. 
28318 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
28319 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65  expression is re
2831a 73 6f 6c 76 65 64 20 69 6e 0a 2a 2a 20 74 68 65  solved in.** the
2831b 20 75 73 75 61 6c 20 77 61 79 20 2d 20 75 73 69   usual way - usi
2831c 6e 67 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  ng sqlite3Resolv
2831d 65 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2a  eExprNames()..**
2831e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2831f 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
28320 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20  ber of errors.  
28321 49 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c  If errors occur,
28322 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 61 70 70 72   then.** an appr
28323 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65  opriate error me
28324 73 73 61 67 65 20 6d 69 67 68 74 20 62 65 20 6c  ssage might be l
28325 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e 20 20  eft in pParse.  
28326 28 4f 4f 4d 20 65 72 72 6f 72 73 0a 2a 2a 20 65  (OOM errors.** e
28327 78 63 65 70 74 65 64 2e 29 0a 2a 2f 0a 73 74 61  xcepted.).*/.sta
28328 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f  tic int resolveO
28329 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20 4e  rderGroupBy(.  N
2832a 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c  ameContext *pNC,
2832b 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65       /* The name
2832c 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   context of the 
2832d 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2832e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
2832f 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54  elect,      /* T
28330 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
28331 65 6e 74 20 68 6f 6c 64 69 6e 67 20 70 4f 72 64  ent holding pOrd
28332 65 72 42 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69  erBy */.  ExprLi
28333 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20  st *pOrderBy,   
28334 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42 59 20 6f  /* An ORDER BY o
28335 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  r GROUP BY claus
28336 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 2a 2f 0a  e to resolve */.
28337 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
28338 79 70 65 20 20 20 20 20 2f 2a 20 45 69 74 68 65  ype     /* Eithe
28339 72 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52  r "ORDER" or "GR
2833a 4f 55 50 22 2c 20 61 73 20 61 70 70 72 6f 70 72  OUP", as appropr
2833b 69 61 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  iate */.){.  int
2833c 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20   i, j;          
2833d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2833e 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
2833f 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20    int iCol;     
28340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28341 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65   /* Column numbe
28342 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78  r */.  struct Ex
28343 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
28344 65 6d 3b 20 20 20 2f 2a 20 41 20 74 65 72 6d 20  em;   /* A term 
28345 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
28346 63 6c 61 75 73 65 20 2a 2f 0a 20 20 50 61 72 73  clause */.  Pars
28347 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20  e *pParse;      
28348 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
28349 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2834a 0a 20 20 69 6e 74 20 6e 52 65 73 75 6c 74 3b 20  .  int nResult; 
2834b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2834c 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
2834d 65 72 6d 73 20 69 6e 20 74 68 65 20 72 65 73 75  erms in the resu
2834e 6c 74 20 73 65 74 20 2a 2f 0a 0a 20 20 69 66 28  lt set */..  if(
2834f 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 72   pOrderBy==0 ) r
28350 65 74 75 72 6e 20 30 3b 0a 20 20 6e 52 65 73 75  eturn 0;.  nResu
28351 6c 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45  lt = pSelect->pE
28352 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70  List->nExpr;.  p
28353 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61  Parse = pNC->pPa
28354 72 73 65 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  rse;.  for(i=0, 
28355 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e  pItem=pOrderBy->
28356 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e  a; i<pOrderBy->n
28357 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
28358 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  ++){.    Expr *p
28359 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  E = pItem->pExpr
2835a 3b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73  ;.    iCol = res
2835b 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73  olveAsName(pPars
2835c 65 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69  e, pSelect->pELi
2835d 73 74 2c 20 70 45 29 3b 0a 20 20 20 20 69 66 28  st, pE);.    if(
2835e 20 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20   iCol>0 ){.     
2835f 20 2f 2a 20 49 66 20 61 6e 20 41 53 2d 6e 61 6d   /* If an AS-nam
28360 65 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64  e match is found
28361 2c 20 6d 61 72 6b 20 74 68 69 73 20 4f 52 44 45  , mark this ORDE
28362 52 20 42 59 20 63 6f 6c 75 6d 6e 20 61 73 20 62  R BY column as b
28363 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 20  eing.      ** a 
28364 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c  copy of the iCol
28365 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 63  -th result-set c
28366 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 73 75 62 73  olumn.  The subs
28367 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 20  equent call to. 
28368 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 52       ** sqlite3R
28369 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
2836a 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72  By() will conver
2836b 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  t the expression
2836c 20 74 6f 20 61 0a 20 20 20 20 20 20 2a 2a 20 63   to a.      ** c
2836d 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d  opy of the iCol-
2836e 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78  th result-set ex
2836f 70 72 65 73 73 69 6f 6e 2e 20 2a 2f 0a 20 20 20  pression. */.   
28370 20 20 20 70 49 74 65 6d 2d 3e 69 4f 72 64 65 72     pItem->iOrder
28371 42 79 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f  ByCol = (u16)iCo
28372 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  l;.      continu
28373 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  e;.    }.    if(
28374 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e   sqlite3ExprIsIn
28375 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f 6c 29  teger(pE, &iCol)
28376 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65   ){.      /* The
28377 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 69   ORDER BY term i
28378 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e  s an integer con
28379 73 74 61 6e 74 2e 20 20 41 67 61 69 6e 2c 20 73  stant.  Again, s
2837a 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20  et the column.  
2837b 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 73 6f      ** number so
2837c 20 74 68 61 74 20 73 71 6c 69 74 65 33 52 65 73   that sqlite3Res
2837d 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79  olveOrderGroupBy
2837e 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 74 20  () will convert 
2837f 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72 64  the.      ** ord
28380 65 72 2d 62 79 20 74 65 72 6d 20 74 6f 20 61 20  er-by term to a 
28381 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75  copy of the resu
28382 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f  lt-set expressio
28383 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69  n */.      if( i
28384 43 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 20 20 20  Col<1 ){.       
28385 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e   resolveOutOfRan
28386 67 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20  geError(pParse, 
28387 7a 54 79 70 65 2c 20 69 2b 31 2c 20 6e 52 65 73  zType, i+1, nRes
28388 75 6c 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65  ult);.        re
28389 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
2838a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 4f 72        pItem->iOr
2838b 64 65 72 42 79 43 6f 6c 20 3d 20 28 75 31 36 29  derByCol = (u16)
2838c 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74  iCol;.      cont
2838d 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  inue;.    }..   
2838e 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74   /* Otherwise, t
2838f 72 65 61 74 20 74 68 65 20 4f 52 44 45 52 20 42  reat the ORDER B
28390 59 20 74 65 72 6d 20 61 73 20 61 6e 20 6f 72 64  Y term as an ord
28391 69 6e 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e  inary expression
28392 20 2a 2f 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69   */.    pItem->i
28393 4f 72 64 65 72 42 79 43 6f 6c 20 3d 20 30 3b 0a  OrderByCol = 0;.
28394 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
28395 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
28396 70 4e 43 2c 20 70 45 29 20 29 7b 0a 20 20 20 20  pNC, pE) ){.    
28397 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
28398 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  }.    for(j=0; j
28399 3c 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74  <pSelect->pEList
2839a 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20  ->nExpr; j++){. 
2839b 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
2839c 45 78 70 72 43 6f 6d 70 61 72 65 28 70 45 2c 20  ExprCompare(pE, 
2839d 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d  pSelect->pEList-
2839e 3e 61 5b 6a 5d 2e 70 45 78 70 72 29 3d 3d 30 20  >a[j].pExpr)==0 
2839f 29 7b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d  ){.        pItem
283a0 2d 3e 69 4f 72 64 65 72 42 79 43 6f 6c 20 3d 20  ->iOrderByCol = 
283a1 6a 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  j+1;.      }.   
283a2 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
283a3 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72  sqlite3ResolveOr
283a4 64 65 72 47 72 6f 75 70 42 79 28 70 50 61 72 73  derGroupBy(pPars
283a5 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 4f 72 64  e, pSelect, pOrd
283a6 65 72 42 79 2c 20 7a 54 79 70 65 29 3b 0a 7d 0a  erBy, zType);.}.
283a7 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6e  ./*.** Resolve n
283a8 61 6d 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45  ames in the SELE
283a9 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 20 61  CT statement p a
283aa 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 65  nd all of its de
283ab 73 63 65 6e 64 65 6e 74 73 2e 0a 2a 2f 0a 73 74  scendents..*/.st
283ac 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65  atic int resolve
283ad 53 65 6c 65 63 74 53 74 65 70 28 57 61 6c 6b 65  SelectStep(Walke
283ae 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
283af 63 74 20 2a 70 29 7b 0a 20 20 4e 61 6d 65 43 6f  ct *p){.  NameCo
283b0 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 3b  ntext *pOuterNC;
283b1 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 74 68 61    /* Context tha
283b2 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20  t contains this 
283b3 53 45 4c 45 43 54 20 2a 2f 0a 20 20 4e 61 6d 65  SELECT */.  Name
283b4 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20  Context sNC;    
283b5 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74      /* Name cont
283b6 65 78 74 20 6f 66 20 74 68 69 73 20 53 45 4c 45  ext of this SELE
283b7 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f  CT */.  int isCo
283b8 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20  mpound;         
283b9 2f 2a 20 54 72 75 65 20 69 66 20 70 20 69 73 20  /* True if p is 
283ba 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  a compound selec
283bb 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6d 70  t */.  int nComp
283bc 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f  ound;          /
283bd 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6d 70  * Number of comp
283be 6f 75 6e 64 20 74 65 72 6d 73 20 70 72 6f 63 65  ound terms proce
283bf 73 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20  ssed so far */. 
283c0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20   Parse *pParse; 
283c1 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
283c2 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
283c3 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
283c4 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75  t;       /* Resu
283c5 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f  lt set expressio
283c6 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  n list */.  int 
283c7 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
283c8 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
283c9 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ter */.  ExprLis
283ca 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20  t *pGroupBy;    
283cb 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59   /* The GROUP BY
283cc 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c   clause */.  Sel
283cd 65 63 74 20 2a 70 4c 65 66 74 6d 6f 73 74 3b 20  ect *pLeftmost; 
283ce 20 20 20 20 20 2f 2a 20 4c 65 66 74 2d 6d 6f 73       /* Left-mos
283cf 74 20 6f 66 20 53 45 4c 45 43 54 20 6f 66 20 61  t of SELECT of a
283d0 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 73   compound */.  s
283d1 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
283d2 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
283d3 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
283d4 0a 20 20 0a 0a 20 20 61 73 73 65 72 74 28 20 70  .  ..  assert( p
283d5 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  !=0 );.  if( p->
283d6 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65  selFlags & SF_Re
283d7 73 6f 6c 76 65 64 20 29 7b 0a 20 20 20 20 72 65  solved ){.    re
283d8 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a  turn WRC_Prune;.
283d9 20 20 7d 0a 20 20 70 4f 75 74 65 72 4e 43 20 3d    }.  pOuterNC =
283da 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b   pWalker->u.pNC;
283db 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57 61 6c  .  pParse = pWal
283dc 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 64  ker->pParse;.  d
283dd 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
283de 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c 79 20 73  .  /* Normally s
283df 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61  qlite3SelectExpa
283e0 6e 64 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c  nd() will be cal
283e1 6c 65 64 20 66 69 72 73 74 20 61 6e 64 20 77 69  led first and wi
283e2 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72  ll have.  ** alr
283e3 65 61 64 79 20 65 78 70 61 6e 64 65 64 20 74 68  eady expanded th
283e4 69 73 20 53 45 4c 45 43 54 2e 20 20 48 6f 77 65  is SELECT.  Howe
283e5 76 65 72 2c 20 69 66 20 74 68 69 73 20 69 73 20  ver, if this is 
283e6 61 20 73 75 62 71 75 65 72 79 20 77 69 74 68 69  a subquery withi
283e7 6e 0a 20 20 2a 2a 20 61 6e 20 65 78 70 72 65 73  n.  ** an expres
283e8 73 69 6f 6e 2c 20 73 71 6c 69 74 65 33 52 65 73  sion, sqlite3Res
283e9 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20  olveExprNames() 
283ea 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 77  will be called w
283eb 69 74 68 6f 75 74 20 61 0a 20 20 2a 2a 20 70 72  ithout a.  ** pr
283ec 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ior call to sqli
283ed 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28  te3SelectExpand(
283ee 29 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68 61  ).  When that ha
283ef 70 70 65 6e 73 2c 20 6c 65 74 0a 20 20 2a 2a 20  ppens, let.  ** 
283f0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65  sqlite3SelectPre
283f1 70 28 29 20 64 6f 20 61 6c 6c 20 6f 66 20 74 68  p() do all of th
283f2 65 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72  e processing for
283f3 20 74 68 69 73 20 53 45 4c 45 43 54 2e 0a 20 20   this SELECT..  
283f4 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  ** sqlite3Select
283f5 50 72 65 70 28 29 20 77 69 6c 6c 20 69 6e 76 6f  Prep() will invo
283f6 6b 65 20 62 6f 74 68 20 73 71 6c 69 74 65 33 53  ke both sqlite3S
283f7 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 61 6e  electExpand() an
283f8 64 0a 20 20 2a 2a 20 74 68 69 73 20 72 6f 75 74  d.  ** this rout
283f9 69 6e 65 20 69 6e 20 74 68 65 20 63 6f 72 72 65  ine in the corre
283fa 63 74 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20  ct order..  */. 
283fb 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67   if( (p->selFlag
283fc 73 20 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29  s & SF_Expanded)
283fd 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
283fe 65 33 53 65 6c 65 63 74 50 72 65 70 28 70 50 61  e3SelectPrep(pPa
283ff 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 72 4e 43  rse, p, pOuterNC
28400 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70  );.    return (p
28401 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
28402 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29  b->mallocFailed)
28403 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a 20 57   ? WRC_Abort : W
28404 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20  RC_Prune;.  }.. 
28405 20 69 73 43 6f 6d 70 6f 75 6e 64 20 3d 20 70 2d   isCompound = p-
28406 3e 70 50 72 69 6f 72 21 3d 30 3b 0a 20 20 6e 43  >pPrior!=0;.  nC
28407 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 70  ompound = 0;.  p
28408 4c 65 66 74 6d 6f 73 74 20 3d 20 70 3b 0a 20 20  Leftmost = p;.  
28409 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20  while( p ){.    
2840a 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46  assert( (p->selF
2840b 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64  lags & SF_Expand
2840c 65 64 29 21 3d 30 20 29 3b 0a 20 20 20 20 61 73  ed)!=0 );.    as
2840d 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61  sert( (p->selFla
2840e 67 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64  gs & SF_Resolved
2840f 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d 3e 73  )==0 );.    p->s
28410 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 52 65  elFlags |= SF_Re
28411 73 6f 6c 76 65 64 3b 0a 0a 20 20 20 20 2f 2a 20  solved;..    /* 
28412 52 65 73 6f 6c 76 65 20 74 68 65 20 65 78 70 72  Resolve the expr
28413 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 4c  essions in the L
28414 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 20  IMIT and OFFSET 
28415 63 6c 61 75 73 65 73 2e 20 54 68 65 73 65 0a 20  clauses. These. 
28416 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 61 6c     ** are not al
28417 6c 6f 77 65 64 20 74 6f 20 72 65 66 65 72 20 74  lowed to refer t
28418 6f 20 61 6e 79 20 6e 61 6d 65 73 2c 20 73 6f 20  o any names, so 
28419 70 61 73 73 20 61 6e 20 65 6d 70 74 79 20 4e 61  pass an empty Na
2841a 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a  meContext..    *
2841b 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e  /.    memset(&sN
2841c 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
2841d 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72  ));.    sNC.pPar
2841e 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20  se = pParse;.   
2841f 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f   if( sqlite3Reso
28420 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e  lveExprNames(&sN
28421 43 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 20 7c 7c  C, p->pLimit) ||
28422 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28423 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
28424 28 26 73 4e 43 2c 20 70 2d 3e 70 4f 66 66 73 65  (&sNC, p->pOffse
28425 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  t) ){.      retu
28426 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
28427 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 53 65    }.  .    /* Se
28428 74 20 75 70 20 74 68 65 20 6c 6f 63 61 6c 20 6e  t up the local n
28429 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 6f 20 70  ame-context to p
2842a 61 73 73 20 74 6f 20 73 71 6c 69 74 65 33 52 65  ass to sqlite3Re
2842b 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29  solveExprNames()
2842c 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c   to.    ** resol
2842d 76 65 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65  ve the result-se
2842e 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  t expression lis
2842f 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e  t..    */.    sN
28430 43 2e 6e 63 46 6c 61 67 73 20 3d 20 4e 43 5f 41  C.ncFlags = NC_A
28431 6c 6c 6f 77 41 67 67 3b 0a 20 20 20 20 73 4e 43  llowAgg;.    sNC
28432 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 2d 3e 70  .pSrcList = p->p
28433 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 70 4e 65  Src;.    sNC.pNe
28434 78 74 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a 20  xt = pOuterNC;. 
28435 20 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65   .    /* Resolve
28436 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 72 65   names in the re
28437 73 75 6c 74 20 73 65 74 2e 20 2a 2f 0a 20 20 20  sult set. */.   
28438 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c   pEList = p->pEL
28439 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ist;.    assert(
2843a 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20   pEList!=0 );.  
2843b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c    for(i=0; i<pEL
2843c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
2843d 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 58  {.      Expr *pX
2843e 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e   = pEList->a[i].
2843f 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28  pExpr;.      if(
28440 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
28441 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70  xprNames(&sNC, p
28442 58 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  X) ){.        re
28443 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
28444 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
28445 0a 20 20 20 20 2f 2a 20 52 65 63 75 72 73 69 76  .    /* Recursiv
28446 65 6c 79 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65  ely resolve name
28447 73 20 69 6e 20 61 6c 6c 20 73 75 62 71 75 65 72  s in all subquer
28448 69 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  ies.    */.    f
28449 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 72  or(i=0; i<p->pSr
2844a 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  c->nSrc; i++){. 
2844b 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c       struct SrcL
2844c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
2844d 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d  = &p->pSrc->a[i]
2844e 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65  ;.      if( pIte
2844f 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  m->pSelect ){.  
28450 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78        NameContex
28451 74 20 2a 70 4e 43 3b 20 20 20 20 20 20 20 20 20  t *pNC;         
28452 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72 61  /* Used to itera
28453 74 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 73  te name contexts
28454 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20   */.        int 
28455 6e 52 65 66 20 3d 20 30 3b 20 20 20 20 20 20 20  nRef = 0;       
28456 20 20 20 20 20 20 2f 2a 20 52 65 66 63 6f 75 6e        /* Refcoun
28457 74 20 66 6f 72 20 70 4f 75 74 65 72 4e 43 20 61  t for pOuterNC a
28458 6e 64 20 6f 75 74 65 72 20 63 6f 6e 74 65 78 74  nd outer context
28459 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e  s */.        con
2845a 73 74 20 63 68 61 72 20 2a 7a 53 61 76 65 64 43  st char *zSavedC
2845b 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65 2d  ontext = pParse-
2845c 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 0a  >zAuthContext;..
2845d 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74          /* Count
2845e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   the total numbe
2845f 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20  r of references 
28460 74 6f 20 70 4f 75 74 65 72 4e 43 20 61 6e 64 20  to pOuterNC and 
28461 61 6c 6c 20 6f 66 20 69 74 73 0a 20 20 20 20 20  all of its.     
28462 20 20 20 2a 2a 20 70 61 72 65 6e 74 20 63 6f 6e     ** parent con
28463 74 65 78 74 73 2e 20 41 66 74 65 72 20 72 65 73  texts. After res
28464 6f 6c 76 69 6e 67 20 72 65 66 65 72 65 6e 63 65  olving reference
28465 73 20 74 6f 20 65 78 70 72 65 73 73 69 6f 6e 73  s to expressions
28466 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 70   in.        ** p
28467 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 63  Item->pSelect, c
28468 68 65 63 6b 20 69 66 20 74 68 69 73 20 76 61 6c  heck if this val
28469 75 65 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20  ue has changed. 
2846a 49 66 20 73 6f 2c 20 74 68 65 6e 0a 20 20 20 20  If so, then.    
2846b 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20 73 74      ** SELECT st
2846c 61 74 65 6d 65 6e 74 20 70 49 74 65 6d 2d 3e 70  atement pItem->p
2846d 53 65 6c 65 63 74 20 6d 75 73 74 20 62 65 20 63  Select must be c
2846e 6f 72 72 65 6c 61 74 65 64 2e 20 53 65 74 20 74  orrelated. Set t
2846f 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 49  he.        ** pI
28470 74 65 6d 2d 3e 69 73 43 6f 72 72 65 6c 61 74 65  tem->isCorrelate
28471 64 20 66 6c 61 67 20 69 66 20 74 68 69 73 20 69  d flag if this i
28472 73 20 74 68 65 20 63 61 73 65 2e 20 2a 2f 0a 20  s the case. */. 
28473 20 20 20 20 20 20 20 66 6f 72 28 70 4e 43 3d 70         for(pNC=p
28474 4f 75 74 65 72 4e 43 3b 20 70 4e 43 3b 20 70 4e  OuterNC; pNC; pN
28475 43 3d 70 4e 43 2d 3e 70 4e 65 78 74 29 20 6e 52  C=pNC->pNext) nR
28476 65 66 20 2b 3d 20 70 4e 43 2d 3e 6e 52 65 66 3b  ef += pNC->nRef;
28477 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49  ..        if( pI
28478 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 70 50 61  tem->zName ) pPa
28479 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
2847a 74 20 3d 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  t = pItem->zName
2847b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2847c 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61  3ResolveSelectNa
2847d 6d 65 73 28 70 50 61 72 73 65 2c 20 70 49 74 65  mes(pParse, pIte
2847e 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 70 4f 75 74  m->pSelect, pOut
2847f 65 72 4e 43 29 3b 0a 20 20 20 20 20 20 20 20 70  erNC);.        p
28480 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
28481 65 78 74 20 3d 20 7a 53 61 76 65 64 43 6f 6e 74  ext = zSavedCont
28482 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ext;.        if(
28483 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
28484 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
28485 64 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  d ) return WRC_A
28486 62 6f 72 74 3b 0a 0a 20 20 20 20 20 20 20 20 66  bort;..        f
28487 6f 72 28 70 4e 43 3d 70 4f 75 74 65 72 4e 43 3b  or(pNC=pOuterNC;
28488 20 70 4e 43 3b 20 70 4e 43 3d 70 4e 43 2d 3e 70   pNC; pNC=pNC->p
28489 4e 65 78 74 29 20 6e 52 65 66 20 2d 3d 20 70 4e  Next) nRef -= pN
2848a 43 2d 3e 6e 52 65 66 3b 0a 20 20 20 20 20 20 20  C->nRef;.       
2848b 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e   assert( pItem->
2848c 69 73 43 6f 72 72 65 6c 61 74 65 64 3d 3d 30 20  isCorrelated==0 
2848d 26 26 20 6e 52 65 66 3c 3d 30 20 29 3b 0a 20 20  && nRef<=0 );.  
2848e 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 73 43        pItem->isC
2848f 6f 72 72 65 6c 61 74 65 64 20 3d 20 28 6e 52 65  orrelated = (nRe
28490 66 21 3d 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  f!=0);.      }. 
28491 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49     }.  .    /* I
28492 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61  f there are no a
28493 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
28494 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ns in the result
28495 2d 73 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52 4f  -set, and no GRO
28496 55 50 20 42 59 20 0a 20 20 20 20 2a 2a 20 65 78  UP BY .    ** ex
28497 70 72 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74  pression, do not
28498 20 61 6c 6c 6f 77 20 61 67 67 72 65 67 61 74 65   allow aggregate
28499 73 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20  s in any of the 
2849a 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e  other expression
2849b 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  s..    */.    as
2849c 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61  sert( (p->selFla
2849d 67 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74  gs & SF_Aggregat
2849e 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 47 72  e)==0 );.    pGr
2849f 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75  oupBy = p->pGrou
284a0 70 42 79 3b 0a 20 20 20 20 69 66 28 20 70 47 72  pBy;.    if( pGr
284a1 6f 75 70 42 79 20 7c 7c 20 28 73 4e 43 2e 6e 63  oupBy || (sNC.nc
284a2 46 6c 61 67 73 20 26 20 4e 43 5f 48 61 73 41 67  Flags & NC_HasAg
284a3 67 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  g)!=0 ){.      p
284a4 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46  ->selFlags |= SF
284a5 5f 41 67 67 72 65 67 61 74 65 3b 0a 20 20 20 20  _Aggregate;.    
284a6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 4e 43  }else{.      sNC
284a7 2e 6e 63 46 6c 61 67 73 20 26 3d 20 7e 4e 43 5f  .ncFlags &= ~NC_
284a8 41 6c 6c 6f 77 41 67 67 3b 0a 20 20 20 20 7d 0a  AllowAgg;.    }.
284a9 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 48    .    /* If a H
284aa 41 56 49 4e 47 20 63 6c 61 75 73 65 20 69 73 20  AVING clause is 
284ab 70 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 74 68  present, then th
284ac 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 47 52  ere must be a GR
284ad 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 20  OUP BY clause.. 
284ae 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d     */.    if( p-
284af 3e 70 48 61 76 69 6e 67 20 26 26 20 21 70 47 72  >pHaving && !pGr
284b0 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 73  oupBy ){.      s
284b1 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
284b2 50 61 72 73 65 2c 20 22 61 20 47 52 4f 55 50 20  Parse, "a GROUP 
284b3 42 59 20 63 6c 61 75 73 65 20 69 73 20 72 65 71  BY clause is req
284b4 75 69 72 65 64 20 62 65 66 6f 72 65 20 48 41 56  uired before HAV
284b5 49 4e 47 22 29 3b 0a 20 20 20 20 20 20 72 65 74  ING");.      ret
284b6 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
284b7 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 41     }.  .    /* A
284b8 64 64 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  dd the expressio
284b9 6e 20 6c 69 73 74 20 74 6f 20 74 68 65 20 6e 61  n list to the na
284ba 6d 65 2d 63 6f 6e 74 65 78 74 20 62 65 66 6f 72  me-context befor
284bb 65 20 70 61 72 73 69 6e 67 20 74 68 65 0a 20 20  e parsing the.  
284bc 20 20 2a 2a 20 6f 74 68 65 72 20 65 78 70 72 65    ** other expre
284bd 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 53 45  ssions in the SE
284be 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  LECT statement. 
284bf 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 0a  This is so that.
284c0 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f      ** expressio
284c1 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  ns in the WHERE 
284c2 63 6c 61 75 73 65 20 28 65 74 63 2e 29 20 63 61  clause (etc.) ca
284c3 6e 20 72 65 66 65 72 20 74 6f 20 65 78 70 72 65  n refer to expre
284c4 73 73 69 6f 6e 73 20 62 79 0a 20 20 20 20 2a 2a  ssions by.    **
284c5 20 61 6c 69 61 73 65 73 20 69 6e 20 74 68 65 20   aliases in the 
284c6 72 65 73 75 6c 74 20 73 65 74 2e 0a 20 20 20 20  result set..    
284c7 2a 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e 6f 72 20  **.    ** Minor 
284c8 70 6f 69 6e 74 3a 20 49 66 20 74 68 69 73 20 69  point: If this i
284c9 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e  s the case, then
284ca 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
284cb 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a 20 72  will be.    ** r
284cc 65 2d 65 76 61 6c 75 61 74 65 64 20 66 6f 72 20  e-evaluated for 
284cd 65 61 63 68 20 72 65 66 65 72 65 6e 63 65 20 74  each reference t
284ce 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  o it..    */.   
284cf 20 73 4e 43 2e 70 45 4c 69 73 74 20 3d 20 70 2d   sNC.pEList = p-
284d0 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 69 66 28  >pEList;.    if(
284d1 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
284d2 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70  xprNames(&sNC, p
284d3 2d 3e 70 57 68 65 72 65 29 20 7c 7c 0a 20 20 20  ->pWhere) ||.   
284d4 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c      sqlite3Resol
284d5 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43  veExprNames(&sNC
284d6 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 0a 20 20  , p->pHaving).  
284d7 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72    ){.      retur
284d8 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20  n WRC_Abort;.   
284d9 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 4f   }..    /* The O
284da 52 44 45 52 20 42 59 20 61 6e 64 20 47 52 4f 55  RDER BY and GROU
284db 50 20 42 59 20 63 6c 61 75 73 65 73 20 6d 61 79  P BY clauses may
284dc 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 74 65   not refer to te
284dd 72 6d 73 20 69 6e 0a 20 20 20 20 2a 2a 20 6f 75  rms in.    ** ou
284de 74 65 72 20 71 75 65 72 69 65 73 20 0a 20 20 20  ter queries .   
284df 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 4e 65 78   */.    sNC.pNex
284e0 74 20 3d 20 30 3b 0a 20 20 20 20 73 4e 43 2e 6e  t = 0;.    sNC.n
284e1 63 46 6c 61 67 73 20 7c 3d 20 4e 43 5f 41 6c 6c  cFlags |= NC_All
284e2 6f 77 41 67 67 3b 0a 0a 20 20 20 20 2f 2a 20 50  owAgg;..    /* P
284e3 72 6f 63 65 73 73 20 74 68 65 20 4f 52 44 45 52  rocess the ORDER
284e4 20 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20 73   BY clause for s
284e5 69 6e 67 6c 65 74 6f 6e 20 53 45 4c 45 43 54 20  ingleton SELECT 
284e6 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20  statements..    
284e7 2a 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  ** The ORDER BY 
284e8 63 6c 61 75 73 65 20 66 6f 72 20 63 6f 6d 70 6f  clause for compo
284e9 75 6e 64 73 20 53 45 4c 45 43 54 20 73 74 61 74  unds SELECT stat
284ea 65 6d 65 6e 74 73 20 69 73 20 68 61 6e 64 6c 65  ements is handle
284eb 64 0a 20 20 20 20 2a 2a 20 62 65 6c 6f 77 2c 20  d.    ** below, 
284ec 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65  after all of the
284ed 20 72 65 73 75 6c 74 2d 73 65 74 73 20 66 6f 72   result-sets for
284ee 20 61 6c 6c 20 6f 66 20 74 68 65 20 65 6c 65 6d   all of the elem
284ef 65 6e 74 73 20 6f 66 0a 20 20 20 20 2a 2a 20 74  ents of.    ** t
284f0 68 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65  he compound have
284f1 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a   been resolved..
284f2 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
284f3 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65  isCompound && re
284f4 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42  solveOrderGroupB
284f5 79 28 26 73 4e 43 2c 20 70 2c 20 70 2d 3e 70 4f  y(&sNC, p, p->pO
284f6 72 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29  rderBy, "ORDER")
284f7 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
284f8 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20   WRC_Abort;.    
284f9 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  }.    if( db->ma
284fa 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
284fb 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
284fc 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  bort;.    }.  . 
284fd 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68     /* Resolve th
284fe 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  e GROUP BY claus
284ff 65 2e 20 20 41 74 20 74 68 65 20 73 61 6d 65 20  e.  At the same 
28500 74 69 6d 65 2c 20 6d 61 6b 65 20 73 75 72 65 20  time, make sure 
28501 0a 20 20 20 20 2a 2a 20 74 68 65 20 47 52 4f 55  .    ** the GROU
28502 50 20 42 59 20 63 6c 61 75 73 65 20 64 6f 65 73  P BY clause does
28503 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 67 67   not contain agg
28504 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
28505 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
28506 20 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20   pGroupBy ){.   
28507 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
28508 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
28509 20 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 72      .      if( r
2850a 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
2850b 42 79 28 26 73 4e 43 2c 20 70 2c 20 70 47 72 6f  By(&sNC, p, pGro
2850c 75 70 42 79 2c 20 22 47 52 4f 55 50 22 29 20 7c  upBy, "GROUP") |
2850d 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
2850e 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  ed ){.        re
2850f 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
28510 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f        }.      fo
28511 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 47 72  r(i=0, pItem=pGr
28512 6f 75 70 42 79 2d 3e 61 3b 20 69 3c 70 47 72 6f  oupBy->a; i<pGro
28513 75 70 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  upBy->nExpr; i++
28514 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
28515 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50      if( ExprHasP
28516 72 6f 70 65 72 74 79 28 70 49 74 65 6d 2d 3e 70  roperty(pItem->p
28517 45 78 70 72 2c 20 45 50 5f 41 67 67 29 20 29 7b  Expr, EP_Agg) ){
28518 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
28519 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2851a 65 2c 20 22 61 67 67 72 65 67 61 74 65 20 66 75  e, "aggregate fu
2851b 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20  nctions are not 
2851c 61 6c 6c 6f 77 65 64 20 69 6e 20 22 0a 20 20 20  allowed in ".   
2851d 20 20 20 20 20 20 20 20 20 20 20 22 74 68 65 20             "the 
2851e 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 22  GROUP BY clause"
2851f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
28520 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
28521 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
28522 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41  .    }..    /* A
28523 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65  dvance to the ne
28524 78 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 63  xt term of the c
28525 6f 6d 70 6f 75 6e 64 0a 20 20 20 20 2a 2f 0a 20  ompound.    */. 
28526 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72     p = p->pPrior
28527 3b 0a 20 20 20 20 6e 43 6f 6d 70 6f 75 6e 64 2b  ;.    nCompound+
28528 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73  +;.  }..  /* Res
28529 6f 6c 76 65 20 74 68 65 20 4f 52 44 45 52 20 42  olve the ORDER B
2852a 59 20 6f 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20  Y on a compound 
2852b 53 45 4c 45 43 54 20 61 66 74 65 72 20 61 6c 6c  SELECT after all
2852c 20 74 65 72 6d 73 20 6f 66 0a 20 20 2a 2a 20 74   terms of.  ** t
2852d 68 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65  he compound have
2852e 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a   been resolved..
2852f 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f 6d    */.  if( isCom
28530 70 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65  pound && resolve
28531 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 28  CompoundOrderBy(
28532 70 50 61 72 73 65 2c 20 70 4c 65 66 74 6d 6f 73  pParse, pLeftmos
28533 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  t) ){.    return
28534 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a   WRC_Abort;.  }.
28535 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72  .  return WRC_Pr
28536 75 6e 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  une;.}../*.** Th
28537 69 73 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73  is routine walks
28538 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
28539 72 65 65 20 61 6e 64 20 72 65 73 6f 6c 76 65 73  ree and resolves
2853a 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 0a 2a   references to.*
2853b 2a 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20  * table columns 
2853c 61 6e 64 20 72 65 73 75 6c 74 2d 73 65 74 20 63  and result-set c
2853d 6f 6c 75 6d 6e 73 2e 20 20 41 74 20 74 68 65 20  olumns.  At the 
2853e 73 61 6d 65 20 74 69 6d 65 2c 20 64 6f 20 65 72  same time, do er
2853f 72 6f 72 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 20  ror.** checking 
28540 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 61 67  on function usag
28541 65 20 61 6e 64 20 73 65 74 20 61 20 66 6c 61 67  e and set a flag
28542 20 69 66 20 61 6e 79 20 61 67 67 72 65 67 61 74   if any aggregat
28543 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 61  e functions.** a
28544 72 65 20 73 65 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54  re seen..**.** T
28545 6f 20 72 65 73 6f 6c 76 65 20 74 61 62 6c 65 20  o resolve table 
28546 63 6f 6c 75 6d 6e 73 20 72 65 66 65 72 65 6e 63  columns referenc
28547 65 73 20 77 65 20 6c 6f 6f 6b 20 66 6f 72 20 6e  es we look for n
28548 6f 64 65 73 20 28 6f 72 20 73 75 62 74 72 65 65  odes (or subtree
28549 73 29 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f  s) of the .** fo
2854a 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20  rm X.Y.Z or Y.Z 
2854b 6f 72 20 6a 75 73 74 20 5a 20 77 68 65 72 65 0a  or just Z where.
2854c 2a 2a 0a 2a 2a 20 20 20 20 20 20 58 3a 20 20 20  **.**      X:   
2854d 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61  The name of a da
2854e 74 61 62 61 73 65 2e 20 20 45 78 3a 20 20 22 6d  tabase.  Ex:  "m
2854f 61 69 6e 22 20 6f 72 20 22 74 65 6d 70 22 20 6f  ain" or "temp" o
28550 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74  r.**           t
28551 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65  he symbolic name
28552 20 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6e 20   assigned to an 
28553 41 54 54 41 43 48 2d 65 64 20 64 61 74 61 62 61  ATTACH-ed databa
28554 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59  se..**.**      Y
28555 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  :   The name of 
28556 61 20 74 61 62 6c 65 20 69 6e 20 61 20 46 52 4f  a table in a FRO
28557 4d 20 63 6c 61 75 73 65 2e 20 20 4f 72 20 69 6e  M clause.  Or in
28558 20 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 20 20   a trigger.**   
28559 20 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 74          one of t
2855a 68 65 20 73 70 65 63 69 61 6c 20 6e 61 6d 65 73  he special names
2855b 20 22 6f 6c 64 22 20 6f 72 20 22 6e 65 77 22 2e   "old" or "new".
2855c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 5a 3a 20 20  .**.**      Z:  
2855d 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63   The name of a c
2855e 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 59  olumn in table Y
2855f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 6f 64 65  ..**.** The node
28560 20 61 74 20 74 68 65 20 72 6f 6f 74 20 6f 66 20   at the root of 
28561 74 68 65 20 73 75 62 74 72 65 65 20 69 73 20 6d  the subtree is m
28562 6f 64 69 66 69 65 64 20 61 73 20 66 6f 6c 6c 6f  odified as follo
28563 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 45 78 70  ws:.**.**    Exp
28564 72 2e 6f 70 20 20 20 20 20 20 20 20 43 68 61 6e  r.op        Chan
28565 67 65 64 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e  ged to TK_COLUMN
28566 0a 2a 2a 20 20 20 20 45 78 70 72 2e 70 54 61 62  .**    Expr.pTab
28567 20 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20        Points to 
28568 74 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74  the Table object
28569 20 66 6f 72 20 58 2e 59 0a 2a 2a 20 20 20 20 45   for X.Y.**    E
2856a 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 20 20 54 68  xpr.iColumn   Th
2856b 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69  e column index i
2856c 6e 20 58 2e 59 2e 20 20 2d 31 20 66 6f 72 20 74  n X.Y.  -1 for t
2856d 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20  he rowid..**    
2856e 45 78 70 72 2e 69 54 61 62 6c 65 20 20 20 20 54  Expr.iTable    T
2856f 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e  he VDBE cursor n
28570 75 6d 62 65 72 20 66 6f 72 20 58 2e 59 0a 2a 2a  umber for X.Y.**
28571 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76  .**.** To resolv
28572 65 20 72 65 73 75 6c 74 2d 73 65 74 20 72 65 66  e result-set ref
28573 65 72 65 6e 63 65 73 2c 20 6c 6f 6f 6b 20 66 6f  erences, look fo
28574 72 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64  r expression nod
28575 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72  es of the.** for
28576 6d 20 5a 20 28 77 69 74 68 20 6e 6f 20 58 20 61  m Z (with no X a
28577 6e 64 20 59 20 70 72 65 66 69 78 29 20 77 68 65  nd Y prefix) whe
28578 72 65 20 74 68 65 20 5a 20 6d 61 74 63 68 65 73  re the Z matches
28579 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a   the right-hand.
2857a 2a 2a 20 73 69 7a 65 20 6f 66 20 61 6e 20 41 53  ** size of an AS
2857b 20 63 6c 61 75 73 65 20 69 6e 20 74 68 65 20 72   clause in the r
2857c 65 73 75 6c 74 2d 73 65 74 20 6f 66 20 61 20 53  esult-set of a S
2857d 45 4c 45 43 54 2e 20 20 54 68 65 20 5a 20 65 78  ELECT.  The Z ex
2857e 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 72  pression.** is r
2857f 65 70 6c 61 63 65 64 20 62 79 20 61 20 63 6f 70  eplaced by a cop
28580 79 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 68 61  y of the left-ha
28581 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 72  nd side of the r
28582 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73  esult-set expres
28583 73 69 6f 6e 2e 0a 2a 2a 20 54 61 62 6c 65 2d 6e  sion..** Table-n
28584 61 6d 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e  ame and function
28585 20 72 65 73 6f 6c 75 74 69 6f 6e 20 6f 63 63 75   resolution occu
28586 72 73 20 6f 6e 20 74 68 65 20 73 75 62 73 74 69  rs on the substi
28587 74 75 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e  tuted expression
28588 0a 2a 2a 20 74 72 65 65 2e 20 20 46 6f 72 20 65  .** tree.  For e
28589 78 61 6d 70 6c 65 2c 20 69 6e 3a 0a 2a 2a 0a 2a  xample, in:.**.*
2858a 2a 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b  *      SELECT a+
2858b 62 20 41 53 20 78 2c 20 63 2b 64 20 41 53 20 79  b AS x, c+d AS y
2858c 20 46 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 42   FROM t1 ORDER B
2858d 59 20 78 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22  Y x;.**.** The "
2858e 78 22 20 74 65 72 6d 20 6f 66 20 74 68 65 20 6f  x" term of the o
2858f 72 64 65 72 20 62 79 20 69 73 20 72 65 70 6c 61  rder by is repla
28590 63 65 64 20 62 79 20 22 61 2b 62 22 20 74 6f 20  ced by "a+b" to 
28591 72 65 6e 64 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20  render:.**.**   
28592 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53     SELECT a+b AS
28593 20 78 2c 20 63 2b 64 20 41 53 20 79 20 46 52 4f   x, c+d AS y FRO
28594 4d 20 74 31 20 4f 52 44 45 52 20 42 59 20 61 2b  M t1 ORDER BY a+
28595 62 3b 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f  b;.**.** Functio
28596 6e 20 63 61 6c 6c 73 20 61 72 65 20 63 68 65 63  n calls are chec
28597 6b 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ked to make sure
28598 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69   that the functi
28599 6f 6e 20 69 73 20 0a 2a 2a 20 64 65 66 69 6e 65  on is .** define
2859a 64 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63  d and that the c
2859b 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66  orrect number of
2859c 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 73   arguments are s
2859d 70 65 63 69 66 69 65 64 2e 0a 2a 2a 20 49 66 20  pecified..** If 
2859e 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
2859f 61 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  an aggregate fun
285a0 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20  ction, then the 
285a1 4e 43 5f 48 61 73 41 67 67 20 66 6c 61 67 20 69  NC_HasAgg flag i
285a2 73 0a 2a 2a 20 73 65 74 20 61 6e 64 20 74 68 65  s.** set and the
285a3 20 6f 70 63 6f 64 65 20 69 73 20 63 68 61 6e 67   opcode is chang
285a4 65 64 20 66 72 6f 6d 20 54 4b 5f 46 55 4e 43 54  ed from TK_FUNCT
285a5 49 4f 4e 20 74 6f 20 54 4b 5f 41 47 47 5f 46 55  ION to TK_AGG_FU
285a6 4e 43 54 49 4f 4e 2e 0a 2a 2a 20 49 66 20 61 6e  NCTION..** If an
285a7 20 65 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74   expression cont
285a8 61 69 6e 73 20 61 67 67 72 65 67 61 74 65 20 66  ains aggregate f
285a9 75 6e 63 74 69 6f 6e 73 20 74 68 65 6e 20 74 68  unctions then th
285aa 65 20 45 50 5f 41 67 67 0a 2a 2a 20 70 72 6f 70  e EP_Agg.** prop
285ab 65 72 74 79 20 6f 6e 20 74 68 65 20 65 78 70 72  erty on the expr
285ac 65 73 73 69 6f 6e 20 69 73 20 73 65 74 2e 0a 2a  ession is set..*
285ad 2a 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65  *.** An error me
285ae 73 73 61 67 65 20 69 73 20 6c 65 66 74 20 69 6e  ssage is left in
285af 20 70 50 61 72 73 65 20 69 66 20 61 6e 79 74 68   pParse if anyth
285b0 69 6e 67 20 69 73 20 61 6d 69 73 73 2e 20 20 54  ing is amiss.  T
285b1 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 69 66 20  he number.** if 
285b2 65 72 72 6f 72 73 20 69 73 20 72 65 74 75 72 6e  errors is return
285b3 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
285b4 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
285b5 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
285b6 73 28 20 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78  s( .  NameContex
285b7 74 20 2a 70 4e 43 2c 20 20 20 20 20 20 20 2f 2a  t *pNC,       /*
285b8 20 4e 61 6d 65 73 70 61 63 65 20 74 6f 20 72 65   Namespace to re
285b9 73 6f 6c 76 65 20 65 78 70 72 65 73 73 69 6f 6e  solve expression
285ba 73 20 69 6e 2e 20 2a 2f 0a 20 20 45 78 70 72 20  s in. */.  Expr 
285bb 2a 70 45 78 70 72 20 20 20 20 20 20 20 20 20 20  *pExpr          
285bc 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
285bd 73 69 6f 6e 20 74 6f 20 62 65 20 61 6e 61 6c 79  sion to be analy
285be 7a 65 64 2e 20 2a 2f 0a 29 7b 0a 20 20 75 38 20  zed. */.){.  u8 
285bf 73 61 76 65 64 48 61 73 41 67 67 3b 0a 20 20 57  savedHasAgg;.  W
285c0 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 69 66 28 20  alker w;..  if( 
285c1 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72  pExpr==0 ) retur
285c2 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  n 0;.#if SQLITE_
285c3 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30  MAX_EXPR_DEPTH>0
285c4 0a 20 20 7b 0a 20 20 20 20 50 61 72 73 65 20 2a  .  {.    Parse *
285c5 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50  pParse = pNC->pP
285c6 61 72 73 65 3b 0a 20 20 20 20 69 66 28 20 73 71  arse;.    if( sq
285c7 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65  lite3ExprCheckHe
285c8 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 45 78  ight(pParse, pEx
285c9 70 72 2d 3e 6e 48 65 69 67 68 74 2b 70 4e 43 2d  pr->nHeight+pNC-
285ca 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74  >pParse->nHeight
285cb 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
285cc 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  n 1;.    }.    p
285cd 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b  Parse->nHeight +
285ce 3d 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74  = pExpr->nHeight
285cf 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73  ;.  }.#endif.  s
285d0 61 76 65 64 48 61 73 41 67 67 20 3d 20 70 4e 43  avedHasAgg = pNC
285d1 2d 3e 6e 63 46 6c 61 67 73 20 26 20 4e 43 5f 48  ->ncFlags & NC_H
285d2 61 73 41 67 67 3b 0a 20 20 70 4e 43 2d 3e 6e 63  asAgg;.  pNC->nc
285d3 46 6c 61 67 73 20 26 3d 20 7e 4e 43 5f 48 61 73  Flags &= ~NC_Has
285d4 41 67 67 3b 0a 20 20 77 2e 78 45 78 70 72 43 61  Agg;.  w.xExprCa
285d5 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65  llback = resolve
285d6 45 78 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53  ExprStep;.  w.xS
285d7 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20  electCallback = 
285d8 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65  resolveSelectSte
285d9 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20  p;.  w.pParse = 
285da 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 77  pNC->pParse;.  w
285db 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20  .u.pNC = pNC;.  
285dc 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
285dd 26 77 2c 20 70 45 78 70 72 29 3b 0a 23 69 66 20  &w, pExpr);.#if 
285de 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
285df 44 45 50 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70  DEPTH>0.  pNC->p
285e0 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d  Parse->nHeight -
285e1 3d 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74  = pExpr->nHeight
285e2 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70  ;.#endif.  if( p
285e3 4e 43 2d 3e 6e 45 72 72 3e 30 20 7c 7c 20 77 2e  NC->nErr>0 || w.
285e4 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29  pParse->nErr>0 )
285e5 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f  {.    ExprSetPro
285e6 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
285e7 45 72 72 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66  Error);.  }.  if
285e8 28 20 70 4e 43 2d 3e 6e 63 46 6c 61 67 73 20 26  ( pNC->ncFlags &
285e9 20 4e 43 5f 48 61 73 41 67 67 20 29 7b 0a 20 20   NC_HasAgg ){.  
285ea 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74    ExprSetPropert
285eb 79 28 70 45 78 70 72 2c 20 45 50 5f 41 67 67 29  y(pExpr, EP_Agg)
285ec 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 61  ;.  }else if( sa
285ed 76 65 64 48 61 73 41 67 67 20 29 7b 0a 20 20 20  vedHasAgg ){.   
285ee 20 70 4e 43 2d 3e 6e 63 46 6c 61 67 73 20 7c 3d   pNC->ncFlags |=
285ef 20 4e 43 5f 48 61 73 41 67 67 3b 0a 20 20 7d 0a   NC_HasAgg;.  }.
285f0 20 20 72 65 74 75 72 6e 20 45 78 70 72 48 61 73    return ExprHas
285f1 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
285f2 45 50 5f 45 72 72 6f 72 29 3b 0a 7d 0a 0a 0a 2f  EP_Error);.}.../
285f3 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c  *.** Resolve all
285f4 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 65 78   names in all ex
285f5 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 20 53  pressions of a S
285f6 45 4c 45 43 54 20 61 6e 64 20 69 6e 20 61 6c 6c  ELECT and in all
285f7 0a 2a 2a 20 64 65 63 65 6e 64 65 6e 74 73 20 6f  .** decendents o
285f8 66 20 74 68 65 20 53 45 4c 45 43 54 2c 20 69 6e  f the SELECT, in
285f9 63 6c 75 64 69 6e 67 20 63 6f 6d 70 6f 75 6e 64  cluding compound
285fa 73 20 6f 66 66 20 6f 66 20 70 2d 3e 70 50 72 69  s off of p->pPri
285fb 6f 72 2c 0a 2a 2a 20 73 75 62 71 75 65 72 69 65  or,.** subquerie
285fc 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 73  s in expressions
285fd 2c 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73  , and subqueries
285fe 20 75 73 65 64 20 61 73 20 46 52 4f 4d 20 63 6c   used as FROM cl
285ff 61 75 73 65 0a 2a 2a 20 74 65 72 6d 73 2e 0a 2a  ause.** terms..*
28600 2a 0a 2a 2a 20 53 65 65 20 73 71 6c 69 74 65 33  *.** See sqlite3
28601 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
28602 28 29 20 66 6f 72 20 61 20 64 65 73 63 72 69 70  () for a descrip
28603 74 69 6f 6e 20 6f 66 20 74 68 65 20 6b 69 6e 64  tion of the kind
28604 73 20 6f 66 0a 2a 2a 20 74 72 61 6e 73 66 6f 72  s of.** transfor
28605 6d 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 63 63  mations that occ
28606 75 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 53 45  ur..**.** All SE
28607 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
28608 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e  should have been
28609 20 65 78 70 61 6e 64 65 64 20 75 73 69 6e 67 0a   expanded using.
2860a 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  ** sqlite3Select
2860b 45 78 70 61 6e 64 28 29 20 70 72 69 6f 72 20 74  Expand() prior t
2860c 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20  o invoking this 
2860d 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49  routine..*/.SQLI
2860e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2860f 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65  sqlite3ResolveSe
28610 6c 65 63 74 4e 61 6d 65 73 28 0a 20 20 50 61 72  lectNames(.  Par
28611 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
28612 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
28613 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  r context */.  S
28614 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
28615 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
28616 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65  ECT statement be
28617 69 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20  ing coded. */.  
28618 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75  NameContext *pOu
28619 74 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 20 63  terNC  /* Name c
2861a 6f 6e 74 65 78 74 20 66 6f 72 20 70 61 72 65 6e  ontext for paren
2861b 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  t SELECT stateme
2861c 6e 74 20 2a 2f 0a 29 7b 0a 20 20 57 61 6c 6b 65  nt */.){.  Walke
2861d 72 20 77 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  r w;..  assert( 
2861e 70 21 3d 30 20 29 3b 0a 20 20 77 2e 78 45 78 70  p!=0 );.  w.xExp
2861f 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f  rCallback = reso
28620 6c 76 65 45 78 70 72 53 74 65 70 3b 0a 20 20 77  lveExprStep;.  w
28621 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  .xSelectCallback
28622 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74   = resolveSelect
28623 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 73 65  Step;.  w.pParse
28624 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 77 2e 75   = pParse;.  w.u
28625 2e 70 4e 43 20 3d 20 70 4f 75 74 65 72 4e 43 3b  .pNC = pOuterNC;
28626 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65  .  sqlite3WalkSe
28627 6c 65 63 74 28 26 77 2c 20 70 29 3b 0a 7d 0a 0a  lect(&w, p);.}..
28628 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
28629 45 6e 64 20 6f 66 20 72 65 73 6f 6c 76 65 2e 63  End of resolve.c
2862a 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2862b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2862c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2862d 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2862e 42 65 67 69 6e 20 66 69 6c 65 20 65 78 70 72 2e  Begin file expr.
2862f 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
28630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28631 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
28632 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
28633 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
28634 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
28635 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
28636 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
28637 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
28638 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
28639 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2863a 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2863b 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2863c 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2863d 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2863e 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2863f 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
28640 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
28641 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
28642 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
28643 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
28644 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
28645 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28646 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28647 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28648 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28649 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
2864a 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75  ile contains rou
2864b 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 61  tines used for a
2864c 6e 61 6c 79 7a 69 6e 67 20 65 78 70 72 65 73 73  nalyzing express
2864d 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20  ions and.** for 
2864e 67 65 6e 65 72 61 74 69 6e 67 20 56 44 42 45 20  generating VDBE 
2864f 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 61  code that evalua
28650 74 65 73 20 65 78 70 72 65 73 73 69 6f 6e 73 20  tes expressions 
28651 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f  in SQLite..*/../
28652 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
28653 27 61 66 66 69 6e 69 74 79 27 20 6f 66 20 74 68  'affinity' of th
28654 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78  e expression pEx
28655 70 72 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a  pr if any..**.**
28656 20 49 66 20 70 45 78 70 72 20 69 73 20 61 20 63   If pExpr is a c
28657 6f 6c 75 6d 6e 2c 20 61 20 72 65 66 65 72 65 6e  olumn, a referen
28658 63 65 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 76  ce to a column v
28659 69 61 20 61 6e 20 27 41 53 27 20 61 6c 69 61 73  ia an 'AS' alias
2865a 2c 0a 2a 2a 20 6f 72 20 61 20 73 75 62 2d 73 65  ,.** or a sub-se
2865b 6c 65 63 74 20 77 69 74 68 20 61 20 63 6f 6c 75  lect with a colu
2865c 6d 6e 20 61 73 20 74 68 65 20 72 65 74 75 72 6e  mn as the return
2865d 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65   value, then the
2865e 20 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 6f 66   .** affinity of
2865f 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73 20   that column is 
28660 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
28661 69 73 65 2c 20 30 78 30 30 20 69 73 20 72 65 74  ise, 0x00 is ret
28662 75 72 6e 65 64 2c 0a 2a 2a 20 69 6e 64 69 63 61  urned,.** indica
28663 74 69 6e 67 20 6e 6f 20 61 66 66 69 6e 69 74 79  ting no affinity
28664 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73   for the express
28665 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20  ion..**.** i.e. 
28666 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
28667 20 65 78 70 72 65 73 73 73 69 6f 6e 73 20 69 6e   expresssions in
28668 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
28669 74 61 74 65 6d 65 6e 74 73 20 61 6c 6c 0a 2a 2a  tatements all.**
2866a 20 68 61 76 65 20 61 6e 20 61 66 66 69 6e 69 74   have an affinit
2866b 79 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20  y:.**.** CREATE 
2866c 54 41 42 4c 45 20 74 31 28 61 29 3b 0a 2a 2a 20  TABLE t1(a);.** 
2866d 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31  SELECT * FROM t1
2866e 20 57 48 45 52 45 20 61 3b 0a 2a 2a 20 53 45 4c   WHERE a;.** SEL
2866f 45 43 54 20 61 20 41 53 20 62 20 46 52 4f 4d 20  ECT a AS b FROM 
28670 74 31 20 57 48 45 52 45 20 62 3b 0a 2a 2a 20 53  t1 WHERE b;.** S
28671 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20  ELECT * FROM t1 
28672 57 48 45 52 45 20 28 73 65 6c 65 63 74 20 61 20  WHERE (select a 
28673 66 72 6f 6d 20 74 31 29 3b 0a 2a 2f 0a 53 51 4c  from t1);.*/.SQL
28674 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
28675 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69   sqlite3ExprAffi
28676 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72  nity(Expr *pExpr
28677 29 7b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 70 45  ){.  int op = pE
28678 78 70 72 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f  xpr->op;.  if( o
28679 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a  p==TK_SELECT ){.
2867a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
2867b 72 2d 3e 66 6c 61 67 73 26 45 50 5f 78 49 73 53  r->flags&EP_xIsS
2867c 65 6c 65 63 74 20 29 3b 0a 20 20 20 20 72 65 74  elect );.    ret
2867d 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 41  urn sqlite3ExprA
2867e 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 78  ffinity(pExpr->x
2867f 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74  .pSelect->pEList
28680 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20  ->a[0].pExpr);. 
28681 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
28682 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 69 66  E_OMIT_CAST.  if
28683 28 20 6f 70 3d 3d 54 4b 5f 43 41 53 54 20 29 7b  ( op==TK_CAST ){
28684 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78  .    assert( !Ex
28685 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
28686 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  xpr, EP_IntValue
28687 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) );.    return 
28688 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54  sqlite3AffinityT
28689 79 70 65 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f  ype(pExpr->u.zTo
2868a 6b 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  ken);.  }.#endif
2868b 0a 20 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41  .  if( (op==TK_A
2868c 47 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d  GG_COLUMN || op=
2868d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70  =TK_COLUMN || op
2868e 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 29 20 0a  ==TK_REGISTER) .
2868f 20 20 20 26 26 20 70 45 78 70 72 2d 3e 70 54 61     && pExpr->pTa
28690 62 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a  b!=0.  ){.    /*
28691 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52   op==TK_REGISTER
28692 20 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 21   && pExpr->pTab!
28693 3d 30 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20  =0 happens when 
28694 70 45 78 70 72 20 77 61 73 20 6f 72 69 67 69 6e  pExpr was origin
28695 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 61 20 54 4b  ally.    ** a TK
28696 5f 43 4f 4c 55 4d 4e 20 62 75 74 20 77 61 73 20  _COLUMN but was 
28697 70 72 65 76 69 6f 75 73 6c 79 20 65 76 61 6c 75  previously evalu
28698 61 74 65 64 20 61 6e 64 20 63 61 63 68 65 64 20  ated and cached 
28699 69 6e 20 61 20 72 65 67 69 73 74 65 72 20 2a 2f  in a register */
2869a 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 45 78  .    int j = pEx
2869b 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  pr->iColumn;.   
2869c 20 69 66 28 20 6a 3c 30 20 29 20 72 65 74 75 72   if( j<0 ) retur
2869d 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  n SQLITE_AFF_INT
2869e 45 47 45 52 3b 0a 20 20 20 20 61 73 73 65 72 74  EGER;.    assert
2869f 28 20 70 45 78 70 72 2d 3e 70 54 61 62 20 26 26  ( pExpr->pTab &&
286a0 20 6a 3c 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e   j<pExpr->pTab->
286a1 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 72 65 74 75  nCol );.    retu
286a2 72 6e 20 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e  rn pExpr->pTab->
286a3 61 43 6f 6c 5b 6a 5d 2e 61 66 66 69 6e 69 74 79  aCol[j].affinity
286a4 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
286a5 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3b 0a  Expr->affinity;.
286a6 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
286a7 20 65 78 70 6c 69 63 69 74 20 63 6f 6c 6c 61 74   explicit collat
286a8 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ing sequence for
286a9 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
286aa 6f 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69  o the.** collati
286ab 6e 67 20 73 65 71 75 65 6e 63 65 20 73 75 70 70  ng sequence supp
286ac 6c 69 65 64 20 69 6e 20 74 68 65 20 73 65 63 6f  lied in the seco
286ad 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  nd argument..*/.
286ae 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
286af 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72  xpr *sqlite3Expr
286b0 53 65 74 43 6f 6c 6c 28 45 78 70 72 20 2a 70 45  SetColl(Expr *pE
286b1 78 70 72 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43  xpr, CollSeq *pC
286b2 6f 6c 6c 29 7b 0a 20 20 69 66 28 20 70 45 78 70  oll){.  if( pExp
286b3 72 20 26 26 20 70 43 6f 6c 6c 20 29 7b 0a 20 20  r && pColl ){.  
286b4 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d    pExpr->pColl =
286b5 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 70 45 78 70   pColl;.    pExp
286b6 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45  r->flags |= EP_E
286b7 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 7d 0a 20  xpCollate;.  }. 
286b8 20 72 65 74 75 72 6e 20 70 45 78 70 72 3b 0a 7d   return pExpr;.}
286b9 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
286ba 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
286bb 63 65 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f  ce for expressio
286bc 6e 20 70 45 78 70 72 20 74 6f 20 62 65 20 74 68  n pExpr to be th
286bd 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73  e collating.** s
286be 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 62 79  equence named by
286bf 20 70 54 6f 6b 65 6e 2e 20 20 20 52 65 74 75 72   pToken.   Retur
286c0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
286c1 68 65 20 72 65 76 69 73 65 64 20 65 78 70 72 65  he revised expre
286c2 73 73 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 63 6f  ssion..** The co
286c3 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
286c4 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65   is marked as "e
286c5 78 70 6c 69 63 69 74 22 20 75 73 69 6e 67 20 74  xplicit" using t
286c6 68 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65  he EP_ExpCollate
286c7 0a 2a 2a 20 66 6c 61 67 2e 20 20 41 6e 20 65 78  .** flag.  An ex
286c8 70 6c 69 63 69 74 20 63 6f 6c 6c 61 74 69 6e 67  plicit collating
286c9 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 20 6f   sequence will o
286ca 76 65 72 72 69 64 65 20 69 6d 70 6c 69 63 69 74  verride implicit
286cb 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  .** collating se
286cc 71 75 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49  quences..*/.SQLI
286cd 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
286ce 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43  *sqlite3ExprSetC
286cf 6f 6c 6c 42 79 54 6f 6b 65 6e 28 50 61 72 73 65  ollByToken(Parse
286d0 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
286d1 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70 43  pExpr, Token *pC
286d2 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72  ollName){.  char
286d3 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 20 20   *zColl = 0;    
286d4 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75 6f          /* Dequo
286d5 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c  ted name of coll
286d6 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 2a  ation sequence *
286d7 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  /.  CollSeq *pCo
286d8 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ll;.  sqlite3 *d
286d9 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
286da 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    zColl = sqlite
286db 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
286dc 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b 0a 20  b, pCollName);. 
286dd 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
286de 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50  LocateCollSeq(pP
286df 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20  arse, zColl);.  
286e0 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f  sqlite3ExprSetCo
286e1 6c 6c 28 70 45 78 70 72 2c 20 70 43 6f 6c 6c 29  ll(pExpr, pColl)
286e2 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
286e3 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20  e(db, zColl);.  
286e4 72 65 74 75 72 6e 20 70 45 78 70 72 3b 0a 7d 0a  return pExpr;.}.
286e5 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
286e6 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74  e default collat
286e7 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ion sequence for
286e8 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
286e9 70 45 78 70 72 2e 20 49 66 0a 2a 2a 20 74 68 65  pExpr. If.** the
286ea 72 65 20 69 73 20 6e 6f 20 64 65 66 61 75 6c 74  re is no default
286eb 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 2c   collation type,
286ec 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51   return 0..*/.SQ
286ed 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c  LITE_PRIVATE Col
286ee 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78 70  lSeq *sqlite3Exp
286ef 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a  rCollSeq(Parse *
286f0 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45  pParse, Expr *pE
286f1 78 70 72 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  xpr){.  CollSeq 
286f2 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 45 78  *pColl = 0;.  Ex
286f3 70 72 20 2a 70 20 3d 20 70 45 78 70 72 3b 0a 20  pr *p = pExpr;. 
286f4 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
286f5 20 69 6e 74 20 6f 70 3b 0a 20 20 20 20 70 43 6f   int op;.    pCo
286f6 6c 6c 20 3d 20 70 2d 3e 70 43 6f 6c 6c 3b 0a 20  ll = p->pColl;. 
286f7 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 20 62     if( pColl ) b
286f8 72 65 61 6b 3b 0a 20 20 20 20 6f 70 20 3d 20 70  reak;.    op = p
286f9 2d 3e 6f 70 3b 0a 20 20 20 20 69 66 28 20 70 2d  ->op;.    if( p-
286fa 3e 70 54 61 62 21 3d 30 20 26 26 20 28 0a 20 20  >pTab!=0 && (.  
286fb 20 20 20 20 20 20 6f 70 3d 3d 54 4b 5f 41 47 47        op==TK_AGG
286fc 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54  _COLUMN || op==T
286fd 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d  K_COLUMN || op==
286fe 54 4b 5f 52 45 47 49 53 54 45 52 20 7c 7c 20 6f  TK_REGISTER || o
286ff 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52 0a 20 20  p==TK_TRIGGER.  
28700 20 20 29 29 7b 0a 20 20 20 20 20 20 2f 2a 20 6f    )){.      /* o
28701 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 26  p==TK_REGISTER &
28702 26 20 70 2d 3e 70 54 61 62 21 3d 30 20 68 61 70  & p->pTab!=0 hap
28703 70 65 6e 73 20 77 68 65 6e 20 70 45 78 70 72 20  pens when pExpr 
28704 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20  was originally. 
28705 20 20 20 20 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c       ** a TK_COL
28706 55 4d 4e 20 62 75 74 20 77 61 73 20 70 72 65 76  UMN but was prev
28707 69 6f 75 73 6c 79 20 65 76 61 6c 75 61 74 65 64  iously evaluated
28708 20 61 6e 64 20 63 61 63 68 65 64 20 69 6e 20 61   and cached in a
28709 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20   register */.   
2870a 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
2870b 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20  Coll;.      int 
2870c 6a 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  j = p->iColumn;.
2870d 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 30 20 29        if( j>=0 )
2870e 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2870f 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
28710 64 62 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c  db;.        zCol
28711 6c 20 3d 20 70 2d 3e 70 54 61 62 2d 3e 61 43 6f  l = p->pTab->aCo
28712 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20  l[j].zColl;.    
28713 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69      pColl = sqli
28714 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64  te3FindCollSeq(d
28715 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f 6c  b, ENC(db), zCol
28716 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70  l, 0);.        p
28717 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43  Expr->pColl = pC
28718 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  oll;.      }.   
28719 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2871a 20 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 43      if( op!=TK_C
2871b 41 53 54 20 26 26 20 6f 70 21 3d 54 4b 5f 55 50  AST && op!=TK_UP
2871c 4c 55 53 20 29 7b 0a 20 20 20 20 20 20 62 72 65  LUS ){.      bre
2871d 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20  ak;.    }.    p 
2871e 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a  = p->pLeft;.  }.
2871f 20 20 69 66 28 20 73 71 6c 69 74 65 33 43 68 65    if( sqlite3Che
28720 63 6b 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  ckCollSeq(pParse
28721 2c 20 70 43 6f 6c 6c 29 20 29 7b 20 0a 20 20 20  , pColl) ){ .   
28722 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a   pColl = 0;.  }.
28723 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a    return pColl;.
28724 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69  }../*.** pExpr i
28725 73 20 61 6e 20 6f 70 65 72 61 6e 64 20 6f 66 20  s an operand of 
28726 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65  a comparison ope
28727 72 61 74 6f 72 2e 20 20 61 66 66 32 20 69 73 20  rator.  aff2 is 
28728 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 69  the.** type affi
28729 6e 69 74 79 20 6f 66 20 74 68 65 20 6f 74 68 65  nity of the othe
2872a 72 20 6f 70 65 72 61 6e 64 2e 20 20 54 68 69 73  r operand.  This
2872b 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
2872c 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66   the.** type aff
2872d 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c  inity that shoul
2872e 64 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68  d be used for th
2872f 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65  e comparison ope
28730 72 61 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rator..*/.SQLITE
28731 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71  _PRIVATE char sq
28732 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69  lite3CompareAffi
28733 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72  nity(Expr *pExpr
28734 2c 20 63 68 61 72 20 61 66 66 32 29 7b 0a 20 20  , char aff2){.  
28735 63 68 61 72 20 61 66 66 31 20 3d 20 73 71 6c 69  char aff1 = sqli
28736 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28  te3ExprAffinity(
28737 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20 61 66  pExpr);.  if( af
28738 66 31 20 26 26 20 61 66 66 32 20 29 7b 0a 20 20  f1 && aff2 ){.  
28739 20 20 2f 2a 20 42 6f 74 68 20 73 69 64 65 73 20    /* Both sides 
2873a 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  of the compariso
2873b 6e 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2e 20 49  n are columns. I
2873c 66 20 6f 6e 65 20 68 61 73 20 6e 75 6d 65 72 69  f one has numeri
2873d 63 0a 20 20 20 20 2a 2a 20 61 66 66 69 6e 69 74  c.    ** affinit
2873e 79 2c 20 75 73 65 20 74 68 61 74 2e 20 4f 74 68  y, use that. Oth
2873f 65 72 77 69 73 65 20 75 73 65 20 6e 6f 20 61 66  erwise use no af
28740 66 69 6e 69 74 79 2e 0a 20 20 20 20 2a 2f 0a 20  finity..    */. 
28741 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73     if( sqlite3Is
28742 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28  NumericAffinity(
28743 61 66 66 31 29 20 7c 7c 20 73 71 6c 69 74 65 33  aff1) || sqlite3
28744 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74  IsNumericAffinit
28745 79 28 61 66 66 32 29 20 29 7b 0a 20 20 20 20 20  y(aff2) ){.     
28746 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41   return SQLITE_A
28747 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 20 20  FF_NUMERIC;.    
28748 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74  }else{.      ret
28749 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  urn SQLITE_AFF_N
2874a 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  ONE;.    }.  }el
2874b 73 65 20 69 66 28 20 21 61 66 66 31 20 26 26 20  se if( !aff1 && 
2874c 21 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20  !aff2 ){.    /* 
2874d 4e 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 20  Neither side of 
2874e 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69  the comparison i
2874f 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d  s a column.  Com
28750 70 61 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20  pare the.    ** 
28751 72 65 73 75 6c 74 73 20 64 69 72 65 63 74 6c 79  results directly
28752 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 74  ..    */.    ret
28753 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  urn SQLITE_AFF_N
28754 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ONE;.  }else{.  
28755 20 20 2f 2a 20 4f 6e 65 20 73 69 64 65 20 69 73    /* One side is
28756 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f   a column, the o
28757 74 68 65 72 20 69 73 20 6e 6f 74 2e 20 55 73 65  ther is not. Use
28758 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 61 66 66   the columns aff
28759 69 6e 69 74 79 2e 20 2a 2f 0a 20 20 20 20 61 73  inity. */.    as
2875a 73 65 72 74 28 20 61 66 66 31 3d 3d 30 20 7c 7c  sert( aff1==0 ||
2875b 20 61 66 66 32 3d 3d 30 20 29 3b 0a 20 20 20 20   aff2==0 );.    
2875c 72 65 74 75 72 6e 20 28 61 66 66 31 20 2b 20 61  return (aff1 + a
2875d 66 66 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ff2);.  }.}../*.
2875e 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20 63 6f  ** pExpr is a co
2875f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f  mparison operato
28760 72 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 74  r.  Return the t
28761 79 70 65 20 61 66 66 69 6e 69 74 79 20 74 68 61  ype affinity tha
28762 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 61  t should.** be a
28763 70 70 6c 69 65 64 20 74 6f 20 62 6f 74 68 20 6f  pplied to both o
28764 70 65 72 61 6e 64 73 20 70 72 69 6f 72 20 74 6f  perands prior to
28765 20 64 6f 69 6e 67 20 74 68 65 20 63 6f 6d 70 61   doing the compa
28766 72 69 73 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  rison..*/.static
28767 20 63 68 61 72 20 63 6f 6d 70 61 72 69 73 6f 6e   char comparison
28768 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70  Affinity(Expr *p
28769 45 78 70 72 29 7b 0a 20 20 63 68 61 72 20 61 66  Expr){.  char af
2876a 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  f;.  assert( pEx
2876b 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c  pr->op==TK_EQ ||
2876c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49   pExpr->op==TK_I
2876d 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  N || pExpr->op==
2876e 54 4b 5f 4c 54 20 7c 7c 0a 20 20 20 20 20 20 20  TK_LT ||.       
2876f 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b     pExpr->op==TK
28770 5f 47 54 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70  _GT || pExpr->op
28771 3d 3d 54 4b 5f 47 45 20 7c 7c 20 70 45 78 70 72  ==TK_GE || pExpr
28772 2d 3e 6f 70 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20  ->op==TK_LE ||. 
28773 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
28774 6f 70 3d 3d 54 4b 5f 4e 45 20 7c 7c 20 70 45 78  op==TK_NE || pEx
28775 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 20 7c 7c  pr->op==TK_IS ||
28776 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49   pExpr->op==TK_I
28777 53 4e 4f 54 20 29 3b 0a 20 20 61 73 73 65 72 74  SNOT );.  assert
28778 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 29  ( pExpr->pLeft )
28779 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65  ;.  aff = sqlite
2877a 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45  3ExprAffinity(pE
2877b 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69  xpr->pLeft);.  i
2877c 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  f( pExpr->pRight
2877d 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 73 71   ){.    aff = sq
2877e 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69  lite3CompareAffi
2877f 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 52 69 67  nity(pExpr->pRig
28780 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c 73  ht, aff);.  }els
28781 65 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f  e if( ExprHasPro
28782 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
28783 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
28784 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43    aff = sqlite3C
28785 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70  ompareAffinity(p
28786 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d  Expr->x.pSelect-
28787 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
28788 78 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c  xpr, aff);.  }el
28789 73 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a 20  se if( !aff ){. 
2878a 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f     aff = SQLITE_
2878b 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20  AFF_NONE;.  }.  
2878c 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f  return aff;.}../
2878d 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20  *.** pExpr is a 
2878e 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72 65  comparison expre
2878f 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c 20  ssion, eg. '=', 
28790 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74 63  '<', IN(...) etc
28791 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69 74  ..** idx_affinit
28792 79 20 69 73 20 74 68 65 20 61 66 66 69 6e 69 74  y is the affinit
28793 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 20  y of an indexed 
28794 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20 74  column. Return t
28795 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69 6e  rue.** if the in
28796 64 65 78 20 77 69 74 68 20 61 66 66 69 6e 69 74  dex with affinit
28797 79 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 6d  y idx_affinity m
28798 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d  ay be used to im
28799 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20 63  plement.** the c
2879a 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45 78  omparison in pEx
2879b 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  pr..*/.SQLITE_PR
2879c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2879d 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b  3IndexAffinityOk
2879e 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68  (Expr *pExpr, ch
2879f 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 29  ar idx_affinity)
287a0 7b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 63  {.  char aff = c
287a1 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74  omparisonAffinit
287a2 79 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69 74  y(pExpr);.  swit
287a3 63 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20 63  ch( aff ){.    c
287a4 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ase SQLITE_AFF_N
287a5 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75 72  ONE:.      retur
287a6 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53 51  n 1;.    case SQ
287a7 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 20  LITE_AFF_TEXT:. 
287a8 20 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 5f       return idx_
287a9 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45  affinity==SQLITE
287aa 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 64  _AFF_TEXT;.    d
287ab 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 65  efault:.      re
287ac 74 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e 75  turn sqlite3IsNu
287ad 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 69 64  mericAffinity(id
287ae 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 7d  x_affinity);.  }
287af 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
287b0 20 74 68 65 20 50 35 20 76 61 6c 75 65 20 74 68   the P5 value th
287b1 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65  at should be use
287b2 64 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 63  d for a binary c
287b3 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70 63  omparison.** opc
287b4 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f 47  ode (OP_Eq, OP_G
287b5 65 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f 20  e etc.) used to 
287b6 63 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20 61  compare pExpr1 a
287b7 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73 74  nd pExpr2..*/.st
287b8 61 74 69 63 20 75 38 20 62 69 6e 61 72 79 43 6f  atic u8 binaryCo
287b9 6d 70 61 72 65 50 35 28 45 78 70 72 20 2a 70 45  mpareP5(Expr *pE
287ba 78 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78 70  xpr1, Expr *pExp
287bb 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75  r2, int jumpIfNu
287bc 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d 20  ll){.  u8 aff = 
287bd 28 63 68 61 72 29 73 71 6c 69 74 65 33 45 78 70  (char)sqlite3Exp
287be 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 32  rAffinity(pExpr2
287bf 29 3b 0a 20 20 61 66 66 20 3d 20 28 75 38 29 73  );.  aff = (u8)s
287c0 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
287c1 69 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61 66  inity(pExpr1, af
287c2 66 29 20 7c 20 28 75 38 29 6a 75 6d 70 49 66 4e  f) | (u8)jumpIfN
287c3 75 6c 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 66  ull;.  return af
287c4 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  f;.}../*.** Retu
287c5 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
287c6 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
287c7 71 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f 75  quence that shou
287c8 6c 64 20 62 65 20 75 73 65 64 20 62 79 0a 2a 2a  ld be used by.**
287c9 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72   a binary compar
287ca 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63 6f  ison operator co
287cb 6d 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61 6e  mparing pLeft an
287cc 64 20 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20  d pRight..**.** 
287cd 49 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64  If the left hand
287ce 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20   expression has 
287cf 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  a collating sequ
287d0 65 6e 63 65 20 74 79 70 65 2c 20 74 68 65 6e 20  ence type, then 
287d1 69 74 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20 4f  it is.** used. O
287d2 74 68 65 72 77 69 73 65 20 74 68 65 20 63 6f 6c  therwise the col
287d3 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
287d4 66 6f 72 20 74 68 65 20 72 69 67 68 74 20 68 61  for the right ha
287d5 6e 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  nd expression.**
287d6 20 69 73 20 75 73 65 64 2c 20 6f 72 20 74 68 65   is used, or the
287d7 20 64 65 66 61 75 6c 74 20 28 42 49 4e 41 52 59   default (BINARY
287d8 29 20 69 66 20 6e 65 69 74 68 65 72 20 65 78 70  ) if neither exp
287d9 72 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f  ression has a co
287da 6c 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65 2e  llating.** type.
287db 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20  .**.** Argument 
287dc 70 52 69 67 68 74 20 28 62 75 74 20 6e 6f 74 20  pRight (but not 
287dd 70 4c 65 66 74 29 20 6d 61 79 20 62 65 20 61 20  pLeft) may be a 
287de 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49 6e  null pointer. In
287df 20 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69   this case,.** i
287e0 74 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65  t is not conside
287e1 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  red..*/.SQLITE_P
287e2 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a  RIVATE CollSeq *
287e3 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d  sqlite3BinaryCom
287e4 70 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20 50  pareCollSeq(.  P
287e5 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20  arse *pParse, . 
287e6 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a 20   Expr *pLeft, . 
287e7 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 7b   Expr *pRight.){
287e8 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
287e9 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65  l;.  assert( pLe
287ea 66 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65 66  ft );.  if( pLef
287eb 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  t->flags & EP_Ex
287ec 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20  pCollate ){.    
287ed 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 70  assert( pLeft->p
287ee 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c  Coll );.    pCol
287ef 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c  l = pLeft->pColl
287f0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52  ;.  }else if( pR
287f1 69 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e  ight && pRight->
287f2 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
287f3 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73  llate ){.    ass
287f4 65 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43 6f  ert( pRight->pCo
287f5 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20  ll );.    pColl 
287f6 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b  = pRight->pColl;
287f7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43  .  }else{.    pC
287f8 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
287f9 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
287fa 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28   pLeft);.    if(
287fb 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20   !pColl ){.     
287fc 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
287fd 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
287fe 73 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20  se, pRight);.   
287ff 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
28800 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pColl;.}../*.** 
28801 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
28802 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f  r a comparison o
28803 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74  perator..*/.stat
28804 69 63 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61  ic int codeCompa
28805 72 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  re(.  Parse *pPa
28806 72 73 65 2c 20 20 20 20 2f 2a 20 54 68 65 20 70  rse,    /* The p
28807 61 72 73 69 6e 67 20 28 61 6e 64 20 63 6f 64 65  arsing (and code
28808 20 67 65 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e   generating) con
28809 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a  text */.  Expr *
2880a 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54  pLeft,      /* T
2880b 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  he left operand 
2880c 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68  */.  Expr *pRigh
2880d 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69  t,     /* The ri
2880e 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  ght operand */. 
2880f 20 69 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20 20   int opcode,    
28810 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72     /* The compar
28811 69 73 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20  ison opcode */. 
28812 20 69 6e 74 20 69 6e 31 2c 20 69 6e 74 20 69 6e   int in1, int in
28813 32 2c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68  2, /* Register h
28814 6f 6c 64 69 6e 67 20 6f 70 65 72 61 6e 64 73 20  olding operands 
28815 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 2c 20 20  */.  int dest,  
28816 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68         /* Jump h
28817 65 72 65 20 69 66 20 74 72 75 65 2e 20 20 2a 2f  ere if true.  */
28818 0a 20 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c  .  int jumpIfNul
28819 6c 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c  l    /* If true,
2881a 20 6a 75 6d 70 20 69 66 20 65 69 74 68 65 72 20   jump if either 
2881b 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20  operand is NULL 
2881c 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 35 3b 0a  */.){.  int p5;.
2881d 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 43 6f    int addr;.  Co
2881e 6c 6c 53 65 71 20 2a 70 34 3b 0a 0a 20 20 70 34  llSeq *p4;..  p4
2881f 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79   = sqlite3Binary
28820 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70  CompareCollSeq(p
28821 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52  Parse, pLeft, pR
28822 69 67 68 74 29 3b 0a 20 20 70 35 20 3d 20 62 69  ight);.  p5 = bi
28823 6e 61 72 79 43 6f 6d 70 61 72 65 50 35 28 70 4c  naryCompareP5(pL
28824 65 66 74 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d  eft, pRight, jum
28825 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 72  pIfNull);.  addr
28826 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
28827 64 4f 70 34 28 70 50 61 72 73 65 2d 3e 70 56 64  dOp4(pParse->pVd
28828 62 65 2c 20 6f 70 63 6f 64 65 2c 20 69 6e 32 2c  be, opcode, in2,
28829 20 64 65 73 74 2c 20 69 6e 31 2c 0a 20 20 20 20   dest, in1,.    
2882a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2882b 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 34         (void*)p4
2882c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20  , P4_COLLSEQ);. 
2882d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
2882e 67 65 50 35 28 70 50 61 72 73 65 2d 3e 70 56 64  geP5(pParse->pVd
2882f 62 65 2c 20 28 75 38 29 70 35 29 3b 0a 20 20 72  be, (u8)p5);.  r
28830 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 23  eturn addr;.}..#
28831 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58  if SQLITE_MAX_EX
28832 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a 0a 2a 2a  PR_DEPTH>0./*.**
28833 20 43 68 65 63 6b 20 74 68 61 74 20 61 72 67 75   Check that argu
28834 6d 65 6e 74 20 6e 48 65 69 67 68 74 20 69 73 20  ment nHeight is 
28835 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75  less than or equ
28836 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75  al to the maximu
28837 6d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  m.** expression 
28838 64 65 70 74 68 20 61 6c 6c 6f 77 65 64 2e 20 49  depth allowed. I
28839 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 6c 65 61  f it is not, lea
2883a 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ve an error mess
2883b 61 67 65 20 69 6e 0a 2a 2a 20 70 50 61 72 73 65  age in.** pParse
2883c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2883d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
2883e 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 50  xprCheckHeight(P
2883f 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
28840 74 20 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 6e  t nHeight){.  in
28841 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
28842 3b 0a 20 20 69 6e 74 20 6d 78 48 65 69 67 68 74  ;.  int mxHeight
28843 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61   = pParse->db->a
28844 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
28845 49 54 5f 45 58 50 52 5f 44 45 50 54 48 5d 3b 0a  IT_EXPR_DEPTH];.
28846 20 20 69 66 28 20 6e 48 65 69 67 68 74 3e 6d 78    if( nHeight>mx
28847 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20 73 71  Height ){.    sq
28848 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
28849 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 22 45  arse, .       "E
2884a 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 69  xpression tree i
2884b 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 6d 61 78  s too large (max
2884c 69 6d 75 6d 20 64 65 70 74 68 20 25 64 29 22 2c  imum depth %d)",
2884d 20 6d 78 48 65 69 67 68 74 0a 20 20 20 20 29 3b   mxHeight.    );
2884e 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
2884f 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65  _ERROR;.  }.  re
28850 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54  turn rc;.}../* T
28851 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72  he following thr
28852 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 68 65  ee functions, he
28853 69 67 68 74 4f 66 45 78 70 72 28 29 2c 20 68 65  ightOfExpr(), he
28854 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 29  ightOfExprList()
28855 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68 74 4f 66  .** and heightOf
28856 53 65 6c 65 63 74 28 29 2c 20 61 72 65 20 75 73  Select(), are us
28857 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
28858 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67  the maximum heig
28859 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 65 78 70  ht.** of any exp
2885a 72 65 73 73 69 6f 6e 20 74 72 65 65 20 72 65 66  ression tree ref
2885b 65 72 65 6e 63 65 64 20 62 79 20 74 68 65 20 73  erenced by the s
2885c 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20  tructure passed 
2885d 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  as the.** first 
2885e 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
2885f 49 66 20 74 68 69 73 20 6d 61 78 69 6d 75 6d 20  If this maximum 
28860 68 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65  height is greate
28861 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65  r than the curre
28862 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64  nt value pointed
28863 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 65 69 67  .** to by pnHeig
28864 68 74 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 70  ht, the second p
28865 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 20 73  arameter, then s
28866 65 74 20 2a 70 6e 48 65 69 67 68 74 20 74 6f 20  et *pnHeight to 
28867 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a  that.** value..*
28868 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65  /.static void he
28869 69 67 68 74 4f 66 45 78 70 72 28 45 78 70 72 20  ightOfExpr(Expr 
2886a 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68  *p, int *pnHeigh
2886b 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  t){.  if( p ){. 
2886c 20 20 20 69 66 28 20 70 2d 3e 6e 48 65 69 67 68     if( p->nHeigh
2886d 74 3e 2a 70 6e 48 65 69 67 68 74 20 29 7b 0a 20  t>*pnHeight ){. 
2886e 20 20 20 20 20 2a 70 6e 48 65 69 67 68 74 20 3d       *pnHeight =
2886f 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20 20   p->nHeight;.   
28870 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20   }.  }.}.static 
28871 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45 78 70  void heightOfExp
28872 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20 2a  rList(ExprList *
28873 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74  p, int *pnHeight
28874 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  ){.  if( p ){.  
28875 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
28876 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72  (i=0; i<p->nExpr
28877 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 68 65  ; i++){.      he
28878 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 61 5b  ightOfExpr(p->a[
28879 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48 65 69 67  i].pExpr, pnHeig
2887a 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ht);.    }.  }.}
2887b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 69  .static void hei
2887c 67 68 74 4f 66 53 65 6c 65 63 74 28 53 65 6c 65  ghtOfSelect(Sele
2887d 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65  ct *p, int *pnHe
2887e 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29  ight){.  if( p )
2887f 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78  {.    heightOfEx
28880 70 72 28 70 2d 3e 70 57 68 65 72 65 2c 20 70 6e  pr(p->pWhere, pn
28881 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69  Height);.    hei
28882 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 48 61  ghtOfExpr(p->pHa
28883 76 69 6e 67 2c 20 70 6e 48 65 69 67 68 74 29 3b  ving, pnHeight);
28884 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70  .    heightOfExp
28885 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 70 6e 48  r(p->pLimit, pnH
28886 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67  eight);.    heig
28887 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4f 66 66  htOfExpr(p->pOff
28888 73 65 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a  set, pnHeight);.
28889 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72      heightOfExpr
2888a 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74 2c 20  List(p->pEList, 
2888b 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68  pnHeight);.    h
2888c 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28  eightOfExprList(
2888d 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70 6e 48  p->pGroupBy, pnH
2888e 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67  eight);.    heig
2888f 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e  htOfExprList(p->
28890 70 4f 72 64 65 72 42 79 2c 20 70 6e 48 65 69 67  pOrderBy, pnHeig
28891 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f  ht);.    heightO
28892 66 53 65 6c 65 63 74 28 70 2d 3e 70 50 72 69 6f  fSelect(p->pPrio
28893 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20  r, pnHeight);.  
28894 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  }.}../*.** Set t
28895 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 74 20  he Expr.nHeight 
28896 76 61 72 69 61 62 6c 65 20 69 6e 20 74 68 65 20  variable in the 
28897 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64  structure passed
28898 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72 67 75 6d   as an .** argum
28899 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 73 73 69  ent. An expressi
2889a 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 69 6c 64  on with no child
2889b 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 73 74 20  ren, Expr.pList 
2889c 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70 53 65 6c  or .** Expr.pSel
2889d 65 63 74 20 6d 65 6d 62 65 72 20 68 61 73 20 61  ect member has a
2889e 20 68 65 69 67 68 74 20 6f 66 20 31 2e 20 41 6e   height of 1. An
2889f 79 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69  y other expressi
288a0 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68 65 69 67  on.** has a heig
288a1 68 74 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  ht equal to the 
288a2 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f  maximum height o
288a3 66 20 61 6e 79 20 6f 74 68 65 72 20 0a 2a 2a 20  f any other .** 
288a4 72 65 66 65 72 65 6e 63 65 64 20 45 78 70 72 20  referenced Expr 
288a5 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 73 74 61  plus one..*/.sta
288a6 74 69 63 20 76 6f 69 64 20 65 78 70 72 53 65 74  tic void exprSet
288a7 48 65 69 67 68 74 28 45 78 70 72 20 2a 70 29 7b  Height(Expr *p){
288a8 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d  .  int nHeight =
288a9 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78   0;.  heightOfEx
288aa 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 6e 48  pr(p->pLeft, &nH
288ab 65 69 67 68 74 29 3b 0a 20 20 68 65 69 67 68 74  eight);.  height
288ac 4f 66 45 78 70 72 28 70 2d 3e 70 52 69 67 68 74  OfExpr(p->pRight
288ad 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 69  , &nHeight);.  i
288ae 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
288af 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c 65  ty(p, EP_xIsSele
288b0 63 74 29 20 29 7b 0a 20 20 20 20 68 65 69 67 68  ct) ){.    heigh
288b1 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 78 2e 70  tOfSelect(p->x.p
288b2 53 65 6c 65 63 74 2c 20 26 6e 48 65 69 67 68 74  Select, &nHeight
288b3 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
288b4 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74  heightOfExprList
288b5 28 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 26 6e 48  (p->x.pList, &nH
288b6 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 70 2d  eight);.  }.  p-
288b7 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 65 69 67  >nHeight = nHeig
288b8 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ht + 1;.}../*.**
288b9 20 53 65 74 20 74 68 65 20 45 78 70 72 2e 6e 48   Set the Expr.nH
288ba 65 69 67 68 74 20 76 61 72 69 61 62 6c 65 20 75  eight variable u
288bb 73 69 6e 67 20 74 68 65 20 65 78 70 72 53 65 74  sing the exprSet
288bc 48 65 69 67 68 74 28 29 20 66 75 6e 63 74 69 6f  Height() functio
288bd 6e 2e 20 49 66 0a 2a 2a 20 74 68 65 20 68 65 69  n. If.** the hei
288be 67 68 74 20 69 73 20 67 72 65 61 74 65 72 20 74  ght is greater t
288bf 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  han the maximum 
288c0 61 6c 6c 6f 77 65 64 20 65 78 70 72 65 73 73 69  allowed expressi
288c1 6f 6e 20 64 65 70 74 68 2c 0a 2a 2a 20 6c 65 61  on depth,.** lea
288c2 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 70  ve an error in p
288c3 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Parse..*/.SQLITE
288c4 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
288c5 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
288c6 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ht(Parse *pParse
288c7 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 65 78  , Expr *p){.  ex
288c8 70 72 53 65 74 48 65 69 67 68 74 28 70 29 3b 0a  prSetHeight(p);.
288c9 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65    sqlite3ExprChe
288ca 63 6b 48 65 69 67 68 74 28 70 50 61 72 73 65 2c  ckHeight(pParse,
288cb 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b 0a 7d 0a   p->nHeight);.}.
288cc 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
288cd 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74  e maximum height
288ce 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73 73 69   of any expressi
288cf 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65 6e 63  on tree referenc
288d0 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 65 6c  ed.** by the sel
288d1 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 70 61  ect statement pa
288d2 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d  ssed as an argum
288d3 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
288d4 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
288d5 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67  e3SelectExprHeig
288d6 68 74 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20  ht(Select *p){. 
288d7 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 30   int nHeight = 0
288d8 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 65 6c 65  ;.  heightOfSele
288d9 63 74 28 70 2c 20 26 6e 48 65 69 67 68 74 29 3b  ct(p, &nHeight);
288da 0a 20 20 72 65 74 75 72 6e 20 6e 48 65 69 67 68  .  return nHeigh
288db 74 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65  t;.}.#else.  #de
288dc 66 69 6e 65 20 65 78 70 72 53 65 74 48 65 69 67  fine exprSetHeig
288dd 68 74 28 79 29 0a 23 65 6e 64 69 66 20 2f 2a 20  ht(y).#endif /* 
288de 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
288df 44 45 50 54 48 3e 30 20 2a 2f 0a 0a 2f 2a 0a 2a  DEPTH>0 */../*.*
288e0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
288e1 73 20 74 68 65 20 63 6f 72 65 20 61 6c 6c 6f 63  s the core alloc
288e2 61 74 6f 72 20 66 6f 72 20 45 78 70 72 20 6e 6f  ator for Expr no
288e3 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 74  des..**.** Const
288e4 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 65  ruct a new expre
288e5 73 73 69 6f 6e 20 6e 6f 64 65 20 61 6e 64 20 72  ssion node and r
288e6 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
288e7 74 6f 20 69 74 2e 20 20 4d 65 6d 6f 72 79 0a 2a  to it.  Memory.*
288e8 2a 20 66 6f 72 20 74 68 69 73 20 6e 6f 64 65 20  * for this node 
288e9 61 6e 64 20 66 6f 72 20 74 68 65 20 70 54 6f 6b  and for the pTok
288ea 65 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  en argument is a
288eb 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63 61 74 69   single allocati
288ec 6f 6e 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66  on.** obtained f
288ed 72 6f 6d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  rom sqlite3DbMal
288ee 6c 6f 63 28 29 2e 20 20 54 68 65 20 63 61 6c 6c  loc().  The call
288ef 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  ing function.** 
288f0 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
288f1 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74  or making sure t
288f2 68 65 20 6e 6f 64 65 20 65 76 65 6e 74 75 61 6c  he node eventual
288f3 6c 79 20 67 65 74 73 20 66 72 65 65 64 2e 0a 2a  ly gets freed..*
288f4 2a 0a 2a 2a 20 49 66 20 64 65 71 75 6f 74 65 20  *.** If dequote 
288f5 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
288f6 65 20 74 6f 6b 65 6e 20 28 69 66 20 69 74 20 65  e token (if it e
288f7 78 69 73 74 73 29 20 69 73 20 64 65 71 75 6f 74  xists) is dequot
288f8 65 64 2e 0a 2a 2a 20 49 66 20 64 65 71 75 6f 74  ed..** If dequot
288f9 65 20 69 73 20 66 61 6c 73 65 2c 20 6e 6f 20 64  e is false, no d
288fa 65 71 75 6f 74 69 6e 67 20 69 73 20 70 65 72 66  equoting is perf
288fb 6f 72 6d 61 6e 63 65 2e 20 20 54 68 65 20 64 65  ormance.  The de
288fc 51 75 6f 74 65 0a 2a 2a 20 70 61 72 61 6d 65 74  Quote.** paramet
288fd 65 72 20 69 73 20 69 67 6e 6f 72 65 64 20 69 66  er is ignored if
288fe 20 70 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20   pToken is NULL 
288ff 6f 72 20 69 66 20 74 68 65 20 74 6f 6b 65 6e 20  or if the token 
28900 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 61 70 70 65  does not.** appe
28901 61 72 20 74 6f 20 62 65 20 71 75 6f 74 65 64 2e  ar to be quoted.
28902 20 20 49 66 20 74 68 65 20 71 75 6f 74 65 73 20    If the quotes 
28903 77 65 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d  were of the form
28904 20 22 2e 2e 2e 22 20 28 64 6f 75 62 6c 65 2d 71   "..." (double-q
28905 75 6f 74 65 73 29 0a 2a 2a 20 74 68 65 6e 20 74  uotes).** then t
28906 68 65 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 20  he EP_DblQuoted 
28907 66 6c 61 67 20 69 73 20 73 65 74 20 6f 6e 20 74  flag is set on t
28908 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f  he expression no
28909 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 61  de..**.** Specia
2890a 6c 20 63 61 73 65 3a 20 20 49 66 20 6f 70 3d 3d  l case:  If op==
2890b 54 4b 5f 49 4e 54 45 47 45 52 20 61 6e 64 20 70  TK_INTEGER and p
2890c 54 6f 6b 65 6e 20 70 6f 69 6e 74 73 20 74 6f 20  Token points to 
2890d 61 20 73 74 72 69 6e 67 20 74 68 61 74 0a 2a 2a  a string that.**
2890e 20 63 61 6e 20 62 65 20 74 72 61 6e 73 6c 61 74   can be translat
2890f 65 64 20 69 6e 74 6f 20 61 20 33 32 2d 62 69 74  ed into a 32-bit
28910 20 69 6e 74 65 67 65 72 2c 20 74 68 65 6e 20 74   integer, then t
28911 68 65 20 74 6f 6b 65 6e 20 69 73 20 6e 6f 74 0a  he token is not.
28912 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 75 2e 7a  ** stored in u.z
28913 54 6f 6b 65 6e 2e 20 20 49 6e 73 74 65 61 64 2c  Token.  Instead,
28914 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c   the integer val
28915 75 65 73 20 69 73 20 77 72 69 74 74 65 6e 0a 2a  ues is written.*
28916 2a 20 69 6e 74 6f 20 75 2e 69 56 61 6c 75 65 20  * into u.iValue 
28917 61 6e 64 20 74 68 65 20 45 50 5f 49 6e 74 56 61  and the EP_IntVa
28918 6c 75 65 20 66 6c 61 67 20 69 73 20 73 65 74 2e  lue flag is set.
28919 20 20 4e 6f 20 65 78 74 72 61 20 73 74 6f 72 61    No extra stora
2891a 67 65 0a 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74  ge.** is allocat
2891b 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 69  ed to hold the i
2891c 6e 74 65 67 65 72 20 74 65 78 74 20 61 6e 64 20  nteger text and 
2891d 74 68 65 20 64 65 71 75 6f 74 65 20 66 6c 61 67  the dequote flag
2891e 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a   is ignored..*/.
2891f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
28920 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72  xpr *sqlite3Expr
28921 41 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65 33  Alloc(.  sqlite3
28922 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
28923 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 73   /* Handle for s
28924 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
28925 72 6f 28 29 20 28 6d 61 79 20 62 65 20 6e 75 6c  ro() (may be nul
28926 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20  l) */.  int op, 
28927 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28928 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70  /* Expression op
28929 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  code */.  const 
2892a 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 2c 20 20  Token *pToken,  
2892b 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 72 67 75 6d    /* Token argum
2892c 65 6e 74 2e 20 20 4d 69 67 68 74 20 62 65 20 4e  ent.  Might be N
2892d 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 64 65 71  ULL */.  int deq
2892e 75 6f 74 65 20 20 20 20 20 20 20 20 20 20 20 20  uote            
2892f 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 65 71 75   /* True to dequ
28930 6f 74 65 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72  ote */.){.  Expr
28931 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 6e 45   *pNew;.  int nE
28932 78 74 72 61 20 3d 20 30 3b 0a 20 20 69 6e 74 20  xtra = 0;.  int 
28933 69 56 61 6c 75 65 20 3d 20 30 3b 0a 0a 20 20 69  iValue = 0;..  i
28934 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20  f( pToken ){.   
28935 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 4e 54 45   if( op!=TK_INTE
28936 47 45 52 20 7c 7c 20 70 54 6f 6b 65 6e 2d 3e 7a  GER || pToken->z
28937 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 7c 7c  ==0.          ||
28938 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32   sqlite3GetInt32
28939 28 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 26 69 56 61  (pToken->z, &iVa
2893a 6c 75 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  lue)==0 ){.     
2893b 20 6e 45 78 74 72 61 20 3d 20 70 54 6f 6b 65 6e   nExtra = pToken
2893c 2d 3e 6e 2b 31 3b 0a 20 20 20 20 20 20 61 73 73  ->n+1;.      ass
2893d 65 72 74 28 20 69 56 61 6c 75 65 3e 3d 30 20 29  ert( iValue>=0 )
2893e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4e  ;.    }.  }.  pN
2893f 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
28940 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
28941 65 6f 66 28 45 78 70 72 29 2b 6e 45 78 74 72 61  eof(Expr)+nExtra
28942 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b  );.  if( pNew ){
28943 0a 20 20 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20  .    pNew->op = 
28944 28 75 38 29 6f 70 3b 0a 20 20 20 20 70 4e 65 77  (u8)op;.    pNew
28945 2d 3e 69 41 67 67 20 3d 20 2d 31 3b 0a 20 20 20  ->iAgg = -1;.   
28946 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20   if( pToken ){. 
28947 20 20 20 20 20 69 66 28 20 6e 45 78 74 72 61 3d       if( nExtra=
28948 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4e  =0 ){.        pN
28949 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  ew->flags |= EP_
2894a 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 20 20  IntValue;.      
2894b 20 20 70 4e 65 77 2d 3e 75 2e 69 56 61 6c 75 65    pNew->u.iValue
2894c 20 3d 20 69 56 61 6c 75 65 3b 0a 20 20 20 20 20   = iValue;.     
2894d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2894e 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 20 20 70  int c;.        p
2894f 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20  New->u.zToken = 
28950 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d 3b  (char*)&pNew[1];
28951 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
28952 20 70 54 6f 6b 65 6e 2d 3e 7a 21 3d 30 20 7c 7c   pToken->z!=0 ||
28953 20 70 54 6f 6b 65 6e 2d 3e 6e 3d 3d 30 20 29 3b   pToken->n==0 );
28954 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 6f  .        if( pTo
28955 6b 65 6e 2d 3e 6e 20 29 20 6d 65 6d 63 70 79 28  ken->n ) memcpy(
28956 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20  pNew->u.zToken, 
28957 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b 65  pToken->z, pToke
28958 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 70  n->n);.        p
28959 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 70 54  New->u.zToken[pT
2895a 6f 6b 65 6e 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20  oken->n] = 0;.  
2895b 20 20 20 20 20 20 69 66 28 20 64 65 71 75 6f 74        if( dequot
2895c 65 20 26 26 20 6e 45 78 74 72 61 3e 3d 33 20 0a  e && nExtra>=3 .
2895d 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
2895e 28 28 63 20 3d 20 70 54 6f 6b 65 6e 2d 3e 7a 5b  ((c = pToken->z[
2895f 30 5d 29 3d 3d 27 5c 27 27 20 7c 7c 20 63 3d 3d  0])=='\'' || c==
28960 27 22 27 20 7c 7c 20 63 3d 3d 27 5b 27 20 7c 7c  '"' || c=='[' ||
28961 20 63 3d 3d 27 60 27 29 20 29 7b 0a 20 20 20 20   c=='`') ){.    
28962 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 71        sqlite3Deq
28963 75 6f 74 65 28 70 4e 65 77 2d 3e 75 2e 7a 54 6f  uote(pNew->u.zTo
28964 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ken);.          
28965 69 66 28 20 63 3d 3d 27 22 27 20 29 20 70 4e 65  if( c=='"' ) pNe
28966 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 44  w->flags |= EP_D
28967 62 6c 51 75 6f 74 65 64 3b 0a 20 20 20 20 20 20  blQuoted;.      
28968 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
28969 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  }.#if SQLITE_MAX
2896a 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20  _EXPR_DEPTH>0.  
2896b 20 20 70 4e 65 77 2d 3e 6e 48 65 69 67 68 74 20    pNew->nHeight 
2896c 3d 20 31 3b 0a 23 65 6e 64 69 66 20 20 0a 20 20  = 1;.#endif  .  
2896d 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  }.  return pNew;
2896e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
2896f 74 65 20 61 20 6e 65 77 20 65 78 70 72 65 73 73  te a new express
28970 69 6f 6e 20 6e 6f 64 65 20 66 72 6f 6d 20 61 20  ion node from a 
28971 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20  zero-terminated 
28972 74 6f 6b 65 6e 20 74 68 61 74 20 68 61 73 0a 2a  token that has.*
28973 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 64  * already been d
28974 65 71 75 6f 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  equoted..*/.SQLI
28975 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
28976 2a 73 71 6c 69 74 65 33 45 78 70 72 28 0a 20 20  *sqlite3Expr(.  
28977 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
28978 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c          /* Handl
28979 65 20 66 6f 72 20 73 71 6c 69 74 65 33 44 62 4d  e for sqlite3DbM
2897a 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28 6d 61 79  allocZero() (may
2897b 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a 20 20 69   be null) */.  i
2897c 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20  nt op,          
2897d 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73         /* Expres
2897e 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20  sion opcode */. 
2897f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f   const char *zTo
28980 6b 65 6e 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65  ken      /* Toke
28981 6e 20 61 72 67 75 6d 65 6e 74 2e 20 20 4d 69 67  n argument.  Mig
28982 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29 7b  ht be NULL */.){
28983 0a 20 20 54 6f 6b 65 6e 20 78 3b 0a 20 20 78 2e  .  Token x;.  x.
28984 7a 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 20 78 2e  z = zToken;.  x.
28985 6e 20 3d 20 7a 54 6f 6b 65 6e 20 3f 20 73 71 6c  n = zToken ? sql
28986 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 6f  ite3Strlen30(zTo
28987 6b 65 6e 29 20 3a 20 30 3b 0a 20 20 72 65 74 75  ken) : 0;.  retu
28988 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c  rn sqlite3ExprAl
28989 6c 6f 63 28 64 62 2c 20 6f 70 2c 20 26 78 2c 20  loc(db, op, &x, 
2898a 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74  0);.}../*.** Att
2898b 61 63 68 20 73 75 62 74 72 65 65 73 20 70 4c 65  ach subtrees pLe
2898c 66 74 20 61 6e 64 20 70 52 69 67 68 74 20 74 6f  ft and pRight to
2898d 20 74 68 65 20 45 78 70 72 20 6e 6f 64 65 20 70   the Expr node p
2898e 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  Root..**.** If p
2898f 52 6f 6f 74 3d 3d 4e 55 4c 4c 20 74 68 61 74 20  Root==NULL that 
28990 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 65 6d  means that a mem
28991 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
28992 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65  rror has occurre
28993 64 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 20 63 61  d..** In that ca
28994 73 65 2c 20 64 65 6c 65 74 65 20 74 68 65 20 73  se, delete the s
28995 75 62 74 72 65 65 73 20 70 4c 65 66 74 20 61 6e  ubtrees pLeft an
28996 64 20 70 52 69 67 68 74 2e 0a 2a 2f 0a 53 51 4c  d pRight..*/.SQL
28997 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
28998 20 73 71 6c 69 74 65 33 45 78 70 72 41 74 74 61   sqlite3ExprAtta
28999 63 68 53 75 62 74 72 65 65 73 28 0a 20 20 73 71  chSubtrees(.  sq
2899a 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 45 78 70  lite3 *db,.  Exp
2899b 72 20 2a 70 52 6f 6f 74 2c 0a 20 20 45 78 70 72  r *pRoot,.  Expr
2899c 20 2a 70 4c 65 66 74 2c 0a 20 20 45 78 70 72 20   *pLeft,.  Expr 
2899d 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 69 66 28  *pRight.){.  if(
2899e 20 70 52 6f 6f 74 3d 3d 30 20 29 7b 0a 20 20 20   pRoot==0 ){.   
2899f 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c   assert( db->mal
289a0 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
289a1 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
289a2 74 65 28 64 62 2c 20 70 4c 65 66 74 29 3b 0a 20  te(db, pLeft);. 
289a3 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
289a4 6c 65 74 65 28 64 62 2c 20 70 52 69 67 68 74 29  lete(db, pRight)
289a5 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
289a6 66 28 20 70 52 69 67 68 74 20 29 7b 0a 20 20 20  f( pRight ){.   
289a7 20 20 20 70 52 6f 6f 74 2d 3e 70 52 69 67 68 74     pRoot->pRight
289a8 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20 20   = pRight;.     
289a9 20 69 66 28 20 70 52 69 67 68 74 2d 3e 66 6c 61   if( pRight->fla
289aa 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
289ab 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52  te ){.        pR
289ac 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  oot->flags |= EP
289ad 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20  _ExpCollate;.   
289ae 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 43 6f 6c       pRoot->pCol
289af 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c  l = pRight->pCol
289b0 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  l;.      }.    }
289b1 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 20 29  .    if( pLeft )
289b2 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70  {.      pRoot->p
289b3 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20  Left = pLeft;.  
289b4 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 66      if( pLeft->f
289b5 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c  lags & EP_ExpCol
289b6 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  late ){.        
289b7 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d 20  pRoot->flags |= 
289b8 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20  EP_ExpCollate;. 
289b9 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 43         pRoot->pC
289ba 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f  oll = pLeft->pCo
289bb 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ll;.      }.    
289bc 7d 0a 20 20 20 20 65 78 70 72 53 65 74 48 65 69  }.    exprSetHei
289bd 67 68 74 28 70 52 6f 6f 74 29 3b 0a 20 20 7d 0a  ght(pRoot);.  }.
289be 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
289bf 65 20 61 20 45 78 70 72 20 6e 6f 64 65 20 77 68  e a Expr node wh
289c0 69 63 68 20 6a 6f 69 6e 73 20 61 73 20 6d 61 6e  ich joins as man
289c1 79 20 61 73 20 74 77 6f 20 73 75 62 74 72 65 65  y as two subtree
289c2 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 6f 72 20  s..**.** One or 
289c3 62 6f 74 68 20 6f 66 20 74 68 65 20 73 75 62 74  both of the subt
289c4 72 65 65 73 20 63 61 6e 20 62 65 20 4e 55 4c 4c  rees can be NULL
289c5 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  .  Return a poin
289c6 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a 2a  ter to the new.*
289c7 2a 20 45 78 70 72 20 6e 6f 64 65 2e 20 20 4f 72  * Expr node.  Or
289c8 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 65 72 72 6f  , if an OOM erro
289c9 72 20 6f 63 63 75 72 73 2c 20 73 65 74 20 70 50  r occurs, set pP
289ca 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
289cb 46 61 69 6c 65 64 2c 0a 2a 2a 20 66 72 65 65 20  Failed,.** free 
289cc 74 68 65 20 73 75 62 74 72 65 65 73 20 61 6e 64  the subtrees and
289cd 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f   return NULL..*/
289ce 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
289cf 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45 78  Expr *sqlite3PEx
289d0 70 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  pr(.  Parse *pPa
289d1 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  rse,          /*
289d2 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
289d3 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20   */.  int op,   
289d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
289d5 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f   Expression opco
289d6 64 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c  de */.  Expr *pL
289d7 65 66 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  eft,            
289d8 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 20  /* Left operand 
289d9 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68  */.  Expr *pRigh
289da 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t,           /* 
289db 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f  Right operand */
289dc 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a  .  const Token *
289dd 70 54 6f 6b 65 6e 20 20 20 20 20 2f 2a 20 41 72  pToken     /* Ar
289de 67 75 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a  gument token */.
289df 29 7b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 20 20  ){.  Expr *p;.  
289e0 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4e 44 20 26  if( op==TK_AND &
289e1 26 20 70 4c 65 66 74 20 26 26 20 70 52 69 67 68  & pLeft && pRigh
289e2 74 20 29 7b 0a 20 20 20 20 2f 2a 20 54 61 6b 65  t ){.    /* Take
289e3 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20 73 68   advantage of sh
289e4 6f 72 74 2d 63 69 72 63 75 69 74 20 66 61 6c 73  ort-circuit fals
289e5 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 66  e optimization f
289e6 6f 72 20 41 4e 44 20 2a 2f 0a 20 20 20 20 70 20  or AND */.    p 
289e7 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64  = sqlite3ExprAnd
289e8 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 65  (pParse->db, pLe
289e9 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 7d  ft, pRight);.  }
289ea 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 73 71  else{.    p = sq
289eb 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 70  lite3ExprAlloc(p
289ec 50 61 72 73 65 2d 3e 64 62 2c 20 6f 70 2c 20 70  Parse->db, op, p
289ed 54 6f 6b 65 6e 2c 20 31 29 3b 0a 20 20 20 20 73  Token, 1);.    s
289ee 71 6c 69 74 65 33 45 78 70 72 41 74 74 61 63 68  qlite3ExprAttach
289ef 53 75 62 74 72 65 65 73 28 70 50 61 72 73 65 2d  Subtrees(pParse-
289f0 3e 64 62 2c 20 70 2c 20 70 4c 65 66 74 2c 20 70  >db, p, pLeft, p
289f1 52 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 69 66  Right);.  }.  if
289f2 28 20 70 20 29 20 7b 0a 20 20 20 20 73 71 6c 69  ( p ) {.    sqli
289f3 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67  te3ExprCheckHeig
289f4 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 6e 48  ht(pParse, p->nH
289f5 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 72 65  eight);.  }.  re
289f6 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
289f7 20 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e 20   Return 1 if an 
289f8 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20  expression must 
289f9 62 65 20 46 41 4c 53 45 20 69 6e 20 61 6c 6c 20  be FALSE in all 
289fa 63 61 73 65 73 20 61 6e 64 20 30 20 69 66 20 74  cases and 0 if t
289fb 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  he.** expression
289fc 20 6d 69 67 68 74 20 62 65 20 74 72 75 65 2e 20   might be true. 
289fd 20 54 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69   This is an opti
289fe 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 66 20 69 73  mization.  If is
289ff 20 4f 4b 20 74 6f 0a 2a 2a 20 72 65 74 75 72 6e   OK to.** return
28a00 20 30 20 68 65 72 65 20 65 76 65 6e 20 69 66 20   0 here even if 
28a01 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72  the expression r
28a02 65 61 6c 6c 79 20 69 73 20 61 6c 77 61 79 73 20  eally is always 
28a03 66 61 6c 73 65 20 28 61 20 0a 2a 2a 20 66 61 6c  false (a .** fal
28a04 73 65 20 6e 65 67 61 74 69 76 65 29 2e 20 20 42  se negative).  B
28a05 75 74 20 69 74 20 69 73 20 61 20 62 75 67 20 74  ut it is a bug t
28a06 6f 20 72 65 74 75 72 6e 20 31 20 69 66 20 74 68  o return 1 if th
28a07 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  e expression.** 
28a08 6d 69 67 68 74 20 62 65 20 74 72 75 65 20 69 6e  might be true in
28a09 20 73 6f 6d 65 20 72 61 72 65 20 63 69 72 63 75   some rare circu
28a0a 6d 73 74 61 6e 63 65 73 20 28 61 20 66 61 6c 73  mstances (a fals
28a0b 65 20 70 6f 73 69 74 69 76 65 2e 29 0a 2a 2a 0a  e positive.).**.
28a0c 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20  ** Note that if 
28a0d 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
28a0e 73 20 70 61 72 74 20 6f 66 20 63 6f 6e 64 69 74  s part of condit
28a0f 69 6f 6e 61 6c 20 66 6f 72 20 61 0a 2a 2a 20 4c  ional for a.** L
28a10 45 46 54 20 4a 4f 49 4e 2c 20 74 68 65 6e 20 77  EFT JOIN, then w
28a11 65 20 63 61 6e 6e 6f 74 20 64 65 74 65 72 6d 69  e cannot determi
28a12 6e 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  ne at compile-ti
28a13 6d 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  me whether or no
28a14 74 0a 2a 2a 20 69 73 20 69 74 20 74 72 75 65 20  t.** is it true 
28a15 6f 72 20 66 61 6c 73 65 2c 20 73 6f 20 61 6c 77  or false, so alw
28a16 61 79 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f  ays return 0..*/
28a17 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 70 72  .static int expr
28a18 41 6c 77 61 79 73 46 61 6c 73 65 28 45 78 70 72  AlwaysFalse(Expr
28a19 20 2a 70 29 7b 0a 20 20 69 6e 74 20 76 20 3d 20   *p){.  int v = 
28a1a 30 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73  0;.  if( ExprHas
28a1b 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 46  Property(p, EP_F
28a1c 72 6f 6d 4a 6f 69 6e 29 20 29 20 72 65 74 75 72  romJoin) ) retur
28a1d 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  n 0;.  if( !sqli
28a1e 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
28a1f 28 70 2c 20 26 76 29 20 29 20 72 65 74 75 72 6e  (p, &v) ) return
28a20 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 76 3d 3d   0;.  return v==
28a21 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 69 6e  0;.}../*.** Join
28a22 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 73   two expressions
28a23 20 75 73 69 6e 67 20 61 6e 20 41 4e 44 20 6f 70   using an AND op
28a24 65 72 61 74 6f 72 2e 20 20 49 66 20 65 69 74 68  erator.  If eith
28a25 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  er expression is
28a26 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 6a  .** NULL, then j
28a27 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 6f  ust return the o
28a28 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 2e  ther expression.
28a29 0a 2a 2a 0a 2a 2a 20 49 66 20 6f 6e 65 20 73 69  .**.** If one si
28a2a 64 65 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20  de or the other 
28a2b 6f 66 20 74 68 65 20 41 4e 44 20 69 73 20 6b 6e  of the AND is kn
28a2c 6f 77 6e 20 74 6f 20 62 65 20 66 61 6c 73 65 2c  own to be false,
28a2d 20 74 68 65 6e 20 69 6e 73 74 65 61 64 0a 2a 2a   then instead.**
28a2e 20 6f 66 20 72 65 74 75 72 6e 69 6e 67 20 61 6e   of returning an
28a2f 20 41 4e 44 20 65 78 70 72 65 73 73 69 6f 6e 2c   AND expression,
28a30 20 6a 75 73 74 20 72 65 74 75 72 6e 20 61 20 63   just return a c
28a31 6f 6e 73 74 61 6e 74 20 65 78 70 72 65 73 73 69  onstant expressi
28a32 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 76 61 6c  on with.** a val
28a33 75 65 20 6f 66 20 66 61 6c 73 65 2e 0a 2a 2f 0a  ue of false..*/.
28a34 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
28a35 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72  xpr *sqlite3Expr
28a36 41 6e 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  And(sqlite3 *db,
28a37 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 45 78   Expr *pLeft, Ex
28a38 70 72 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 69  pr *pRight){.  i
28a39 66 28 20 70 4c 65 66 74 3d 3d 30 20 29 7b 0a 20  f( pLeft==0 ){. 
28a3a 20 20 20 72 65 74 75 72 6e 20 70 52 69 67 68 74     return pRight
28a3b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52  ;.  }else if( pR
28a3c 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72  ight==0 ){.    r
28a3d 65 74 75 72 6e 20 70 4c 65 66 74 3b 0a 20 20 7d  eturn pLeft;.  }
28a3e 65 6c 73 65 20 69 66 28 20 65 78 70 72 41 6c 77  else if( exprAlw
28a3f 61 79 73 46 61 6c 73 65 28 70 4c 65 66 74 29 20  aysFalse(pLeft) 
28a40 7c 7c 20 65 78 70 72 41 6c 77 61 79 73 46 61 6c  || exprAlwaysFal
28a41 73 65 28 70 52 69 67 68 74 29 20 29 7b 0a 20 20  se(pRight) ){.  
28a42 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
28a43 65 74 65 28 64 62 2c 20 70 4c 65 66 74 29 3b 0a  ete(db, pLeft);.
28a44 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
28a45 65 6c 65 74 65 28 64 62 2c 20 70 52 69 67 68 74  elete(db, pRight
28a46 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  );.    return sq
28a47 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64  lite3ExprAlloc(d
28a48 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 26  b, TK_INTEGER, &
28a49 73 71 6c 69 74 65 33 49 6e 74 54 6f 6b 65 6e 73  sqlite3IntTokens
28a4a 5b 30 5d 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  [0], 0);.  }else
28a4b 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77  {.    Expr *pNew
28a4c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c   = sqlite3ExprAl
28a4d 6c 6f 63 28 64 62 2c 20 54 4b 5f 41 4e 44 2c 20  loc(db, TK_AND, 
28a4e 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
28a4f 65 33 45 78 70 72 41 74 74 61 63 68 53 75 62 74  e3ExprAttachSubt
28a50 72 65 65 73 28 64 62 2c 20 70 4e 65 77 2c 20 70  rees(db, pNew, p
28a51 4c 65 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20  Left, pRight);. 
28a52 20 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a     return pNew;.
28a53 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e    }.}../*.** Con
28a54 73 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70  struct a new exp
28a55 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72  ression node for
28a56 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   a function with
28a57 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 67   multiple.** arg
28a58 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54  uments..*/.SQLIT
28a59 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
28a5a 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74  sqlite3ExprFunct
28a5b 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73  ion(Parse *pPars
28a5c 65 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  e, ExprList *pLi
28a5d 73 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65  st, Token *pToke
28a5e 6e 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77  n){.  Expr *pNew
28a5f 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
28a60 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
28a61 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 20 29  assert( pToken )
28a62 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  pNew = sqlit
28a63 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20  e3ExprAlloc(db, 
28a64 54 4b 5f 46 55 4e 43 54 49 4f 4e 2c 20 70 54 6f  TK_FUNCTION, pTo
28a65 6b 65 6e 2c 20 31 29 3b 0a 20 20 69 66 28 20 70  ken, 1);.  if( p
28a66 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  New==0 ){.    sq
28a67 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
28a68 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 20  ete(db, pList); 
28a69 2f 2a 20 41 76 6f 69 64 20 6d 65 6d 6f 72 79 20  /* Avoid memory 
28a6a 6c 65 61 6b 20 77 68 65 6e 20 6d 61 6c 6c 6f 63  leak when malloc
28a6b 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 72 65   fails */.    re
28a6c 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e  turn 0;.  }.  pN
28a6d 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70 4c  ew->x.pList = pL
28a6e 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ist;.  assert( !
28a6f 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
28a70 70 4e 65 77 2c 20 45 50 5f 78 49 73 53 65 6c 65  pNew, EP_xIsSele
28a71 63 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ct) );.  sqlite3
28a72 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50  ExprSetHeight(pP
28a73 61 72 73 65 2c 20 70 4e 65 77 29 3b 0a 20 20 72  arse, pNew);.  r
28a74 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  eturn pNew;.}../
28a75 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 61 20 76 61  *.** Assign a va
28a76 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 74 6f  riable number to
28a77 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
28a78 68 61 74 20 65 6e 63 6f 64 65 73 20 61 20 77 69  hat encodes a wi
28a79 6c 64 63 61 72 64 0a 2a 2a 20 69 6e 20 74 68 65  ldcard.** in the
28a7a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74   original SQL st
28a7b 61 74 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a  atement.  .**.**
28a7c 20 57 69 6c 64 63 61 72 64 73 20 63 6f 6e 73 69   Wildcards consi
28a7d 73 74 69 6e 67 20 6f 66 20 61 20 73 69 6e 67 6c  sting of a singl
28a7e 65 20 22 3f 22 20 61 72 65 20 61 73 73 69 67 6e  e "?" are assign
28a7f 65 64 20 74 68 65 20 6e 65 78 74 20 73 65 71 75  ed the next sequ
28a80 65 6e 74 69 61 6c 0a 2a 2a 20 76 61 72 69 61 62  ential.** variab
28a81 6c 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a  le number..**.**
28a82 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68   Wildcards of th
28a83 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20 61 72  e form "?nnn" ar
28a84 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 6e  e assigned the n
28a85 75 6d 62 65 72 20 22 6e 6e 6e 22 2e 20 20 57 65  umber "nnn".  We
28a86 20 6d 61 6b 65 0a 2a 2a 20 73 75 72 65 20 22 6e   make.** sure "n
28a87 6e 6e 22 20 69 73 20 6e 6f 74 20 74 6f 6f 20 62  nn" is not too b
28a88 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65 6e  e to avoid a den
28a89 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65 20 61  ial of service a
28a8a 74 74 61 63 6b 20 77 68 65 6e 0a 2a 2a 20 74 68  ttack when.** th
28a8b 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
28a8c 63 6f 6d 65 73 20 66 72 6f 6d 20 61 6e 20 65 78  comes from an ex
28a8d 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 2e 0a 2a  ternal source..*
28a8e 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f  *.** Wildcards o
28a8f 66 20 74 68 65 20 66 6f 72 6d 20 22 3a 61 61 61  f the form ":aaa
28a90 22 2c 20 22 40 61 61 61 22 2c 20 6f 72 20 22 24  ", "@aaa", or "$
28a91 61 61 61 22 20 61 72 65 20 61 73 73 69 67 6e 65  aaa" are assigne
28a92 64 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65  d the same numbe
28a93 72 0a 2a 2a 20 61 73 20 74 68 65 20 70 72 65 76  r.** as the prev
28a94 69 6f 75 73 20 69 6e 73 74 61 6e 63 65 20 6f 66  ious instance of
28a95 20 74 68 65 20 73 61 6d 65 20 77 69 6c 64 63 61   the same wildca
28a96 72 64 2e 20 20 4f 72 20 69 66 20 74 68 69 73 20  rd.  Or if this 
28a97 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  is the first.** 
28a98 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
28a99 77 69 6c 64 63 61 72 64 2c 20 74 68 65 20 6e 65  wildcard, the ne
28a9a 78 74 20 73 65 71 75 65 6e 69 61 6c 20 76 61 72  xt sequenial var
28a9b 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 69 73 0a  iable number is.
28a9c 2a 2a 20 61 73 73 69 67 6e 65 64 2e 0a 2a 2f 0a  ** assigned..*/.
28a9d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
28a9e 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41  oid sqlite3ExprA
28a9f 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50  ssignVarNumber(P
28aa0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
28aa1 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 73 71  pr *pExpr){.  sq
28aa2 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
28aa3 73 65 2d 3e 64 62 3b 0a 20 20 63 6f 6e 73 74 20  se->db;.  const 
28aa4 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 69 66 28 20  char *z;..  if( 
28aa5 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72  pExpr==0 ) retur
28aa6 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 21 45 78  n;.  assert( !Ex
28aa7 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
28aa8 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61  (pExpr, EP_IntVa
28aa9 6c 75 65 7c 45 50 5f 52 65 64 75 63 65 64 7c 45  lue|EP_Reduced|E
28aaa 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a  P_TokenOnly) );.
28aab 20 20 7a 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a    z = pExpr->u.z
28aac 54 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72 74 28  Token;.  assert(
28aad 20 7a 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72   z!=0 );.  asser
28aae 74 28 20 7a 5b 30 5d 21 3d 30 20 29 3b 0a 20 20  t( z[0]!=0 );.  
28aaf 69 66 28 20 7a 5b 31 5d 3d 3d 30 20 29 7b 0a 20  if( z[1]==0 ){. 
28ab0 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f     /* Wildcard o
28ab1 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 2e 20  f the form "?". 
28ab2 20 41 73 73 69 67 6e 20 74 68 65 20 6e 65 78 74   Assign the next
28ab3 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72   variable number
28ab4 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
28ab5 7a 5b 30 5d 3d 3d 27 3f 27 20 29 3b 0a 20 20 20  z[0]=='?' );.   
28ab6 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20   pExpr->iColumn 
28ab7 3d 20 28 79 6e 56 61 72 29 28 2b 2b 70 50 61 72  = (ynVar)(++pPar
28ab8 73 65 2d 3e 6e 56 61 72 29 3b 0a 20 20 7d 65 6c  se->nVar);.  }el
28ab9 73 65 7b 0a 20 20 20 20 79 6e 56 61 72 20 78 20  se{.    ynVar x 
28aba 3d 20 30 3b 0a 20 20 20 20 75 33 32 20 6e 20 3d  = 0;.    u32 n =
28abb 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
28abc 28 7a 29 3b 0a 20 20 20 20 69 66 28 20 7a 5b 30  (z);.    if( z[0
28abd 5d 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 20 20  ]=='?' ){.      
28abe 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 20 74  /* Wildcard of t
28abf 68 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e 20  he form "?nnn". 
28ac0 20 43 6f 6e 76 65 72 74 20 22 6e 6e 6e 22 20 74   Convert "nnn" t
28ac1 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64  o an integer and
28ac2 0a 20 20 20 20 20 20 2a 2a 20 75 73 65 20 69 74  .      ** use it
28ac3 20 61 73 20 74 68 65 20 76 61 72 69 61 62 6c 65   as the variable
28ac4 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20   number */.     
28ac5 20 69 36 34 20 69 3b 0a 20 20 20 20 20 20 69 6e   i64 i;.      in
28ac6 74 20 62 4f 6b 20 3d 20 30 3d 3d 73 71 6c 69 74  t bOk = 0==sqlit
28ac7 65 33 41 74 6f 69 36 34 28 26 7a 5b 31 5d 2c 20  e3Atoi64(&z[1], 
28ac8 26 69 2c 20 6e 2d 31 2c 20 53 51 4c 49 54 45 5f  &i, n-1, SQLITE_
28ac9 55 54 46 38 29 3b 0a 20 20 20 20 20 20 70 45 78  UTF8);.      pEx
28aca 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 78 20  pr->iColumn = x 
28acb 3d 20 28 79 6e 56 61 72 29 69 3b 0a 20 20 20 20  = (ynVar)i;.    
28acc 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 30    testcase( i==0
28acd 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
28ace 73 65 28 20 69 3d 3d 31 20 29 3b 0a 20 20 20 20  se( i==1 );.    
28acf 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 64    testcase( i==d
28ad0 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
28ad1 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f  _LIMIT_VARIABLE_
28ad2 4e 55 4d 42 45 52 5d 2d 31 20 29 3b 0a 20 20 20  NUMBER]-1 );.   
28ad3 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
28ad4 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
28ad5 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45  E_LIMIT_VARIABLE
28ad6 5f 4e 55 4d 42 45 52 5d 20 29 3b 0a 20 20 20 20  _NUMBER] );.    
28ad7 20 20 69 66 28 20 62 4f 6b 3d 3d 30 20 7c 7c 20    if( bOk==0 || 
28ad8 69 3c 31 20 7c 7c 20 69 3e 64 62 2d 3e 61 4c 69  i<1 || i>db->aLi
28ad9 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
28ada 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
28adb 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ] ){.        sql
28adc 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
28add 72 73 65 2c 20 22 76 61 72 69 61 62 6c 65 20 6e  rse, "variable n
28ade 75 6d 62 65 72 20 6d 75 73 74 20 62 65 20 62 65  umber must be be
28adf 74 77 65 65 6e 20 3f 31 20 61 6e 64 20 3f 25 64  tween ?1 and ?%d
28ae0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 64  ",.            d
28ae1 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
28ae2 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f  _LIMIT_VARIABLE_
28ae3 4e 55 4d 42 45 52 5d 29 3b 0a 20 20 20 20 20 20  NUMBER]);.      
28ae4 20 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d    x = 0;.      }
28ae5 0a 20 20 20 20 20 20 69 66 28 20 69 3e 70 50 61  .      if( i>pPa
28ae6 72 73 65 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20  rse->nVar ){.   
28ae7 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61       pParse->nVa
28ae8 72 20 3d 20 28 69 6e 74 29 69 3b 0a 20 20 20 20  r = (int)i;.    
28ae9 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
28aea 20 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64       /* Wildcard
28aeb 73 20 6c 69 6b 65 20 22 3a 61 61 61 22 2c 20 22  s like ":aaa", "
28aec 24 61 61 61 22 20 6f 72 20 22 40 61 61 61 22 2e  $aaa" or "@aaa".
28aed 20 20 52 65 75 73 65 20 74 68 65 20 73 61 6d 65    Reuse the same
28aee 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 20 20   variable.      
28aef 2a 2a 20 6e 75 6d 62 65 72 20 61 73 20 74 68 65  ** number as the
28af0 20 70 72 69 6f 72 20 61 70 70 65 61 72 61 6e 63   prior appearanc
28af1 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61  e of the same na
28af2 6d 65 2c 20 6f 72 20 69 66 20 74 68 65 20 6e 61  me, or if the na
28af3 6d 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20  me.      ** has 
28af4 6e 65 76 65 72 20 61 70 70 65 61 72 65 64 20 62  never appeared b
28af5 65 66 6f 72 65 2c 20 72 65 75 73 65 20 74 68 65  efore, reuse the
28af6 20 73 61 6d 65 20 76 61 72 69 61 62 6c 65 20 6e   same variable n
28af7 75 6d 62 65 72 0a 20 20 20 20 20 20 2a 2f 0a 20  umber.      */. 
28af8 20 20 20 20 20 79 6e 56 61 72 20 69 3b 0a 20 20       ynVar i;.  
28af9 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
28afa 50 61 72 73 65 2d 3e 6e 7a 56 61 72 3b 20 69 2b  Parse->nzVar; i+
28afb 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
28afc 70 50 61 72 73 65 2d 3e 61 7a 56 61 72 5b 69 5d  pParse->azVar[i]
28afd 20 26 26 20 6d 65 6d 63 6d 70 28 70 50 61 72 73   && memcmp(pPars
28afe 65 2d 3e 61 7a 56 61 72 5b 69 5d 2c 7a 2c 6e 2b  e->azVar[i],z,n+
28aff 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  1)==0 ){.       
28b00 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d     pExpr->iColum
28b01 6e 20 3d 20 78 20 3d 20 28 79 6e 56 61 72 29 69  n = x = (ynVar)i
28b02 2b 31 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  +1;.          br
28b03 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
28b04 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
28b05 20 78 3d 3d 30 20 29 20 78 20 3d 20 70 45 78 70   x==0 ) x = pExp
28b06 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28 79 6e  r->iColumn = (yn
28b07 56 61 72 29 28 2b 2b 70 50 61 72 73 65 2d 3e 6e  Var)(++pParse->n
28b08 56 61 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Var);.    }.    
28b09 69 66 28 20 78 3e 30 20 29 7b 0a 20 20 20 20 20  if( x>0 ){.     
28b0a 20 69 66 28 20 78 3e 70 50 61 72 73 65 2d 3e 6e   if( x>pParse->n
28b0b 7a 56 61 72 20 29 7b 0a 20 20 20 20 20 20 20 20  zVar ){.        
28b0c 63 68 61 72 20 2a 2a 61 3b 0a 20 20 20 20 20 20  char **a;.      
28b0d 20 20 61 20 3d 20 73 71 6c 69 74 65 33 44 62 52    a = sqlite3DbR
28b0e 65 61 6c 6c 6f 63 28 64 62 2c 20 70 50 61 72 73  ealloc(db, pPars
28b0f 65 2d 3e 61 7a 56 61 72 2c 20 78 2a 73 69 7a 65  e->azVar, x*size
28b10 6f 66 28 61 5b 30 5d 29 29 3b 0a 20 20 20 20 20  of(a[0]));.     
28b11 20 20 20 69 66 28 20 61 3d 3d 30 20 29 20 72 65     if( a==0 ) re
28b12 74 75 72 6e 3b 20 20 2f 2a 20 45 72 72 6f 72 20  turn;  /* Error 
28b13 72 65 70 6f 72 74 65 64 20 74 68 72 6f 75 67 68  reported through
28b14 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
28b15 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 50 61  d */.        pPa
28b16 72 73 65 2d 3e 61 7a 56 61 72 20 3d 20 61 3b 0a  rse->azVar = a;.
28b17 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26          memset(&
28b18 61 5b 70 50 61 72 73 65 2d 3e 6e 7a 56 61 72 5d  a[pParse->nzVar]
28b19 2c 20 30 2c 20 28 78 2d 70 50 61 72 73 65 2d 3e  , 0, (x-pParse->
28b1a 6e 7a 56 61 72 29 2a 73 69 7a 65 6f 66 28 61 5b  nzVar)*sizeof(a[
28b1b 30 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 70 50  0]));.        pP
28b1c 61 72 73 65 2d 3e 6e 7a 56 61 72 20 3d 20 78 3b  arse->nzVar = x;
28b1d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
28b1e 66 28 20 7a 5b 30 5d 21 3d 27 3f 27 20 7c 7c 20  f( z[0]!='?' || 
28b1f 70 50 61 72 73 65 2d 3e 61 7a 56 61 72 5b 78 2d  pParse->azVar[x-
28b20 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  1]==0 ){.       
28b21 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
28b22 62 2c 20 70 50 61 72 73 65 2d 3e 61 7a 56 61 72  b, pParse->azVar
28b23 5b 78 2d 31 5d 29 3b 0a 20 20 20 20 20 20 20 20  [x-1]);.        
28b24 70 50 61 72 73 65 2d 3e 61 7a 56 61 72 5b 78 2d  pParse->azVar[x-
28b25 31 5d 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  1] = sqlite3DbSt
28b26 72 4e 44 75 70 28 64 62 2c 20 7a 2c 20 6e 29 3b  rNDup(db, z, n);
28b27 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
28b28 20 7d 20 0a 20 20 69 66 28 20 21 70 50 61 72 73   } .  if( !pPars
28b29 65 2d 3e 6e 45 72 72 20 26 26 20 70 50 61 72 73  e->nErr && pPars
28b2a 65 2d 3e 6e 56 61 72 3e 64 62 2d 3e 61 4c 69 6d  e->nVar>db->aLim
28b2b 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
28b2c 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d  VARIABLE_NUMBER]
28b2d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
28b2e 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
28b2f 22 74 6f 6f 20 6d 61 6e 79 20 53 51 4c 20 76 61  "too many SQL va
28b30 72 69 61 62 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d  riables");.  }.}
28b31 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72 73 69 76  ../*.** Recursiv
28b32 65 6c 79 20 64 65 6c 65 74 65 20 61 6e 20 65 78  ely delete an ex
28b33 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 2a  pression tree..*
28b34 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
28b35 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
28b36 72 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20  rDelete(sqlite3 
28b37 2a 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20  *db, Expr *p){. 
28b38 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
28b39 72 6e 3b 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20  rn;.  /* Sanity 
28b3a 63 68 65 63 6b 3a 20 41 73 73 65 72 74 20 74 68  check: Assert th
28b3b 61 74 20 74 68 65 20 49 6e 74 56 61 6c 75 65 20  at the IntValue 
28b3c 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20  is non-negative 
28b3d 69 66 20 69 74 20 65 78 69 73 74 73 20 2a 2f 0a  if it exists */.
28b3e 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
28b3f 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  asProperty(p, EP
28b40 5f 49 6e 74 56 61 6c 75 65 29 20 7c 7c 20 70 2d  _IntValue) || p-
28b41 3e 75 2e 69 56 61 6c 75 65 3e 3d 30 20 29 3b 0a  >u.iValue>=0 );.
28b42 20 20 69 66 28 20 21 45 78 70 72 48 61 73 41 6e    if( !ExprHasAn
28b43 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f  yProperty(p, EP_
28b44 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20  TokenOnly) ){.  
28b45 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
28b46 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74  ete(db, p->pLeft
28b47 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
28b48 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  prDelete(db, p->
28b49 70 52 69 67 68 74 29 3b 0a 20 20 20 20 69 66 28  pRight);.    if(
28b4a 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
28b4b 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 29  y(p, EP_Reduced)
28b4c 20 26 26 20 28 70 2d 3e 66 6c 61 67 73 32 20 26   && (p->flags2 &
28b4d 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b   EP2_MallocedTok
28b4e 65 6e 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  en)!=0 ){.      
28b4f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
28b50 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a  , p->u.zToken);.
28b51 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 45 78      }.    if( Ex
28b52 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
28b53 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
28b54 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  {.      sqlite3S
28b55 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
28b56 70 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20  p->x.pSelect);. 
28b57 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
28b58 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
28b59 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70  elete(db, p->x.p
28b5a 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  List);.    }.  }
28b5b 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73 50  .  if( !ExprHasP
28b5c 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 53 74  roperty(p, EP_St
28b5d 61 74 69 63 29 20 29 7b 0a 20 20 20 20 73 71 6c  atic) ){.    sql
28b5e 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
28b5f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
28b60 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
28b61 72 20 6f 66 20 62 79 74 65 73 20 61 6c 6c 6f 63  r of bytes alloc
28b62 61 74 65 64 20 66 6f 72 20 74 68 65 20 65 78 70  ated for the exp
28b63 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 72  ression structur
28b64 65 20 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20  e .** passed as 
28b65 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
28b66 6e 74 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61  nt. This is alwa
28b67 79 73 20 6f 6e 65 20 6f 66 20 45 58 50 52 5f 46  ys one of EXPR_F
28b68 55 4c 4c 53 49 5a 45 2c 0a 2a 2a 20 45 58 50 52  ULLSIZE,.** EXPR
28b69 5f 52 45 44 55 43 45 44 53 49 5a 45 20 6f 72 20  _REDUCEDSIZE or 
28b6a 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49  EXPR_TOKENONLYSI
28b6b 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ZE..*/.static in
28b6c 74 20 65 78 70 72 53 74 72 75 63 74 53 69 7a 65  t exprStructSize
28b6d 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28  (Expr *p){.  if(
28b6e 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
28b6f 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  (p, EP_TokenOnly
28b70 29 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f  ) ) return EXPR_
28b71 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20  TOKENONLYSIZE;. 
28b72 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
28b73 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75 63  erty(p, EP_Reduc
28b74 65 64 29 20 29 20 72 65 74 75 72 6e 20 45 58 50  ed) ) return EXP
28b75 52 5f 52 45 44 55 43 45 44 53 49 5a 45 3b 0a 20  R_REDUCEDSIZE;. 
28b76 20 72 65 74 75 72 6e 20 45 58 50 52 5f 46 55 4c   return EXPR_FUL
28b77 4c 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  LSIZE;.}../*.** 
28b78 54 68 65 20 64 75 70 65 64 45 78 70 72 2a 53 69  The dupedExpr*Si
28b79 7a 65 28 29 20 72 6f 75 74 69 6e 65 73 20 65 61  ze() routines ea
28b7a 63 68 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  ch return the nu
28b7b 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65  mber of bytes re
28b7c 71 75 69 72 65 64 0a 2a 2a 20 74 6f 20 73 74 6f  quired.** to sto
28b7d 72 65 20 61 20 63 6f 70 79 20 6f 66 20 61 6e 20  re a copy of an 
28b7e 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 65 78  expression or ex
28b7f 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20  pression tree.  
28b80 54 68 65 79 20 64 69 66 66 65 72 20 69 6e 0a 2a  They differ in.*
28b81 2a 20 68 6f 77 20 6d 75 63 68 20 6f 66 20 74 68  * how much of th
28b82 65 20 74 72 65 65 20 69 73 20 6d 65 61 73 75 72  e tree is measur
28b83 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 64 75  ed..**.**     du
28b84 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69 7a  pedExprStructSiz
28b85 65 28 29 20 20 20 20 20 53 69 7a 65 20 6f 66 20  e()     Size of 
28b86 6f 6e 6c 79 20 74 68 65 20 45 78 70 72 20 73 74  only the Expr st
28b87 72 75 63 74 75 72 65 20 0a 2a 2a 20 20 20 20 20  ructure .**     
28b88 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a  dupedExprNodeSiz
28b89 65 28 29 20 20 20 20 20 20 20 53 69 7a 65 20 6f  e()       Size o
28b8a 66 20 45 78 70 72 20 2b 20 73 70 61 63 65 20 66  f Expr + space f
28b8b 6f 72 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20  or token.**     
28b8c 64 75 70 65 64 45 78 70 72 53 69 7a 65 28 29 20  dupedExprSize() 
28b8d 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2b            Expr +
28b8e 20 74 6f 6b 65 6e 20 2b 20 73 75 62 74 72 65 65   token + subtree
28b8f 20 63 6f 6d 70 6f 6e 65 6e 74 73 0a 2a 2a 0a 2a   components.**.*
28b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
28b95 20 54 68 65 20 64 75 70 65 64 45 78 70 72 53 74   The dupedExprSt
28b96 72 75 63 74 53 69 7a 65 28 29 20 66 75 6e 63 74  ructSize() funct
28b97 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 77 6f 20  ion returns two 
28b98 76 61 6c 75 65 73 20 4f 52 2d 65 64 20 74 6f 67  values OR-ed tog
28b99 65 74 68 65 72 3a 20 20 0a 2a 2a 20 28 31 29 20  ether:  .** (1) 
28b9a 74 68 65 20 73 70 61 63 65 20 72 65 71 75 69 72  the space requir
28b9b 65 64 20 66 6f 72 20 61 20 63 6f 70 79 20 6f 66  ed for a copy of
28b9c 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63 74   the Expr struct
28b9d 75 72 65 20 6f 6e 6c 79 20 61 6e 64 20 0a 2a 2a  ure only and .**
28b9e 20 28 32 29 20 74 68 65 20 45 50 5f 78 78 78 20   (2) the EP_xxx 
28b9f 66 6c 61 67 73 20 74 68 61 74 20 69 6e 64 69 63  flags that indic
28ba0 61 74 65 20 77 68 61 74 20 74 68 65 20 73 74 72  ate what the str
28ba1 75 63 74 75 72 65 20 73 69 7a 65 20 73 68 6f 75  ucture size shou
28ba2 6c 64 20 62 65 2e 0a 2a 2a 20 54 68 65 20 72 65  ld be..** The re
28ba3 74 75 72 6e 20 76 61 6c 75 65 73 20 69 73 20 61  turn values is a
28ba4 6c 77 61 79 73 20 6f 6e 65 20 6f 66 3a 0a 2a 2a  lways one of:.**
28ba5 0a 2a 2a 20 20 20 20 20 20 45 58 50 52 5f 46 55  .**      EXPR_FU
28ba6 4c 4c 53 49 5a 45 0a 2a 2a 20 20 20 20 20 20 45  LLSIZE.**      E
28ba7 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20  XPR_REDUCEDSIZE 
28ba8 20 20 7c 20 45 50 5f 52 65 64 75 63 65 64 0a 2a    | EP_Reduced.*
28ba9 2a 20 20 20 20 20 20 45 58 50 52 5f 54 4f 4b 45  *      EXPR_TOKE
28baa 4e 4f 4e 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54  NONLYSIZE | EP_T
28bab 6f 6b 65 6e 4f 6e 6c 79 0a 2a 2a 0a 2a 2a 20 54  okenOnly.**.** T
28bac 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73  he size of the s
28bad 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20  tructure can be 
28bae 66 6f 75 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67  found by masking
28baf 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
28bb0 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 72 6f 75  e.** of this rou
28bb1 74 69 6e 65 20 77 69 74 68 20 30 78 66 66 66 2e  tine with 0xfff.
28bb2 20 20 54 68 65 20 66 6c 61 67 73 20 63 61 6e 20    The flags can 
28bb3 62 65 20 66 6f 75 6e 64 20 62 79 20 6d 61 73 6b  be found by mask
28bb4 69 6e 67 20 74 68 65 0a 2a 2a 20 72 65 74 75 72  ing the.** retur
28bb5 6e 20 76 61 6c 75 65 20 77 69 74 68 20 45 50 5f  n value with EP_
28bb6 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e  Reduced|EP_Token
28bb7 4f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  Only..**.** Note
28bb8 20 74 68 61 74 20 77 69 74 68 20 66 6c 61 67 73   that with flags
28bb9 3d 3d 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  ==EXPRDUP_REDUCE
28bba 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 73 20  , this routines 
28bbb 77 6f 72 6b 73 20 6f 6e 20 66 75 6c 6c 2d 73 69  works on full-si
28bbc 7a 65 0a 2a 2a 20 28 75 6e 72 65 64 75 63 65 64  ze.** (unreduced
28bbd 29 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 61  ) Expr objects a
28bbe 73 20 74 68 65 79 20 6f 72 20 6f 72 69 67 69 6e  s they or origin
28bbf 61 6c 6c 79 20 63 6f 6e 73 74 72 75 63 74 65 64  ally constructed
28bc0 20 62 79 20 74 68 65 20 70 61 72 73 65 72 2e 0a   by the parser..
28bc1 2a 2a 20 44 75 72 69 6e 67 20 65 78 70 72 65 73  ** During expres
28bc2 73 69 6f 6e 20 61 6e 61 6c 79 73 69 73 2c 20 65  sion analysis, e
28bc3 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  xtra information
28bc4 20 69 73 20 63 6f 6d 70 75 74 65 64 20 61 6e 64   is computed and
28bc5 20 6d 6f 76 65 64 20 69 6e 74 6f 0a 2a 2a 20 6c   moved into.** l
28bc6 61 74 65 72 20 70 61 72 74 73 20 6f 66 20 74 65  ater parts of te
28bc7 68 20 45 78 70 72 20 6f 62 6a 65 63 74 20 61 6e  h Expr object an
28bc8 64 20 74 68 61 74 20 65 78 74 72 61 20 69 6e 66  d that extra inf
28bc9 6f 72 6d 61 74 69 6f 6e 20 6d 69 67 68 74 20 67  ormation might g
28bca 65 74 20 63 68 6f 70 70 65 64 0a 2a 2a 20 6f 66  et chopped.** of
28bcb 66 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73  f if the express
28bcc 69 6f 6e 20 69 73 20 72 65 64 75 63 65 64 2e 20  ion is reduced. 
28bcd 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20   Note also that 
28bce 69 74 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b  it does not work
28bcf 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 61 20 45 58   to.** make a EX
28bd0 50 52 44 55 50 5f 52 45 44 55 43 45 20 63 6f 70  PRDUP_REDUCE cop
28bd1 79 20 6f 66 20 61 20 72 65 64 75 63 65 64 20 65  y of a reduced e
28bd2 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 74 20 69  xpression.  It i
28bd3 73 20 6f 6e 6c 79 20 6c 65 67 61 6c 0a 2a 2a 20  s only legal.** 
28bd4 74 6f 20 72 65 64 75 63 65 20 61 20 70 72 69 73  to reduce a pris
28bd5 74 69 6e 65 20 65 78 70 72 65 73 73 69 6f 6e 20  tine expression 
28bd6 74 72 65 65 20 66 72 6f 6d 20 74 68 65 20 70 61  tree from the pa
28bd7 72 73 65 72 2e 20 20 54 68 65 20 69 6d 70 6c 65  rser.  The imple
28bd8 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20  mentation.** of 
28bd9 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74 53  dupedExprStructS
28bda 69 7a 65 28 29 20 63 6f 6e 74 61 69 6e 20 6d 75  ize() contain mu
28bdb 6c 74 69 70 6c 65 20 61 73 73 65 72 74 28 29 20  ltiple assert() 
28bdc 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20  statements that 
28bdd 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e  attempt.** to en
28bde 66 6f 72 63 65 20 74 68 69 73 20 63 6f 6e 73 74  force this const
28bdf 72 61 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  raint..*/.static
28be0 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 53 74   int dupedExprSt
28be1 72 75 63 74 53 69 7a 65 28 45 78 70 72 20 2a 70  ructSize(Expr *p
28be2 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
28be3 69 6e 74 20 6e 53 69 7a 65 3b 0a 20 20 61 73 73  int nSize;.  ass
28be4 65 72 74 28 20 66 6c 61 67 73 3d 3d 45 58 50 52  ert( flags==EXPR
28be5 44 55 50 5f 52 45 44 55 43 45 20 7c 7c 20 66 6c  DUP_REDUCE || fl
28be6 61 67 73 3d 3d 30 20 29 3b 20 2f 2a 20 4f 6e 6c  ags==0 ); /* Onl
28be7 79 20 6f 6e 65 20 66 6c 61 67 20 76 61 6c 75 65  y one flag value
28be8 20 61 6c 6c 6f 77 65 64 20 2a 2f 0a 20 20 69 66   allowed */.  if
28be9 28 20 30 3d 3d 28 66 6c 61 67 73 26 45 58 50 52  ( 0==(flags&EXPR
28bea 44 55 50 5f 52 45 44 55 43 45 29 20 29 7b 0a 20  DUP_REDUCE) ){. 
28beb 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f     nSize = EXPR_
28bec 46 55 4c 4c 53 49 5a 45 3b 0a 20 20 7d 65 6c 73  FULLSIZE;.  }els
28bed 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  e{.    assert( !
28bee 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
28bef 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e  ty(p, EP_TokenOn
28bf0 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29  ly|EP_Reduced) )
28bf1 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45  ;.    assert( !E
28bf2 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
28bf3 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29  , EP_FromJoin) )
28bf4 3b 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  ; .    assert( (
28bf5 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f  p->flags2 & EP2_
28bf6 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d  MallocedToken)==
28bf7 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
28bf8 20 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50   (p->flags2 & EP
28bf9 32 5f 49 72 72 65 64 75 63 69 62 6c 65 29 3d 3d  2_Irreducible)==
28bfa 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  0 );.    if( p->
28bfb 70 4c 65 66 74 20 7c 7c 20 70 2d 3e 70 52 69 67  pLeft || p->pRig
28bfc 68 74 20 7c 7c 20 70 2d 3e 70 43 6f 6c 6c 20 7c  ht || p->pColl |
28bfd 7c 20 70 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a  | p->x.pList ){.
28bfe 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58        nSize = EX
28bff 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 7c  PR_REDUCEDSIZE |
28c00 20 45 50 5f 52 65 64 75 63 65 64 3b 0a 20 20 20   EP_Reduced;.   
28c01 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 53   }else{.      nS
28c02 69 7a 65 20 3d 20 45 58 50 52 5f 54 4f 4b 45 4e  ize = EXPR_TOKEN
28c03 4f 4e 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54 6f  ONLYSIZE | EP_To
28c04 6b 65 6e 4f 6e 6c 79 3b 0a 20 20 20 20 7d 0a 20  kenOnly;.    }. 
28c05 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 53 69 7a   }.  return nSiz
28c06 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  e;.}../*.** This
28c07 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
28c08 73 20 74 68 65 20 73 70 61 63 65 20 69 6e 20 62  s the space in b
28c09 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f  ytes required to
28c0a 20 73 74 6f 72 65 20 74 68 65 20 63 6f 70 79 20   store the copy 
28c0b 0a 2a 2a 20 6f 66 20 74 68 65 20 45 78 70 72 20  .** of the Expr 
28c0c 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 61 20  structure and a 
28c0d 63 6f 70 79 20 6f 66 20 74 68 65 20 45 78 70 72  copy of the Expr
28c0e 2e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67  .u.zToken string
28c0f 20 28 69 66 20 74 68 61 74 0a 2a 2a 20 73 74 72   (if that.** str
28c10 69 6e 67 20 69 73 20 64 65 66 69 6e 65 64 2e 29  ing is defined.)
28c11 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
28c12 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65  upedExprNodeSize
28c13 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c  (Expr *p, int fl
28c14 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74  ags){.  int nByt
28c15 65 20 3d 20 64 75 70 65 64 45 78 70 72 53 74 72  e = dupedExprStr
28c16 75 63 74 53 69 7a 65 28 70 2c 20 66 6c 61 67 73  uctSize(p, flags
28c17 29 20 26 20 30 78 66 66 66 3b 0a 20 20 69 66 28  ) & 0xfff;.  if(
28c18 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
28c19 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  y(p, EP_IntValue
28c1a 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e  ) && p->u.zToken
28c1b 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 2b 3d   ){.    nByte +=
28c1c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
28c1d 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2b 31 3b  (p->u.zToken)+1;
28c1e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 52 4f  .  }.  return RO
28c1f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 7d 0a 0a  UND8(nByte);.}..
28c20 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
28c21 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
28c22 20 72 65 71 75 69 72 65 64 20 74 6f 20 63 72 65   required to cre
28c23 61 74 65 20 61 20 64 75 70 6c 69 63 61 74 65 20  ate a duplicate 
28c24 6f 66 20 74 68 65 20 0a 2a 2a 20 65 78 70 72 65  of the .** expre
28c25 73 73 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20  ssion passed as 
28c26 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
28c27 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61  nt. The second a
28c28 72 67 75 6d 65 6e 74 20 69 73 20 61 0a 2a 2a 20  rgument is a.** 
28c29 6d 61 73 6b 20 63 6f 6e 74 61 69 6e 69 6e 67 20  mask containing 
28c2a 45 58 50 52 44 55 50 5f 58 58 58 20 66 6c 61 67  EXPRDUP_XXX flag
28c2b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c  s..**.** The val
28c2c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c  ue returned incl
28c2d 75 64 65 73 20 73 70 61 63 65 20 74 6f 20 63 72  udes space to cr
28c2e 65 61 74 65 20 61 20 63 6f 70 79 20 6f 66 20 74  eate a copy of t
28c2f 68 65 20 45 78 70 72 20 73 74 72 75 63 74 0a 2a  he Expr struct.*
28c30 2a 20 69 74 73 65 6c 66 20 61 6e 64 20 74 68 65  * itself and the
28c31 20 62 75 66 66 65 72 20 72 65 66 65 72 72 65 64   buffer referred
28c32 20 74 6f 20 62 79 20 45 78 70 72 2e 75 2e 7a 54   to by Expr.u.zT
28c33 6f 6b 65 6e 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a  oken, if any..**
28c34 0a 2a 2a 20 49 66 20 74 68 65 20 45 58 50 52 44  .** If the EXPRD
28c35 55 50 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69  UP_REDUCE flag i
28c36 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  s set, then the 
28c37 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 63  return value inc
28c38 6c 75 64 65 73 20 0a 2a 2a 20 73 70 61 63 65 20  ludes .** space 
28c39 74 6f 20 64 75 70 6c 69 63 61 74 65 20 61 6c 6c  to duplicate all
28c3a 20 45 78 70 72 20 6e 6f 64 65 73 20 69 6e 20 74   Expr nodes in t
28c3b 68 65 20 74 72 65 65 20 66 6f 72 6d 65 64 20 62  he tree formed b
28c3c 79 20 45 78 70 72 2e 70 4c 65 66 74 20 0a 2a 2a  y Expr.pLeft .**
28c3d 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74   and Expr.pRight
28c3e 20 76 61 72 69 61 62 6c 65 73 20 28 62 75 74 20   variables (but 
28c3f 6e 6f 74 20 66 6f 72 20 61 6e 79 20 73 74 72 75  not for any stru
28c40 63 74 75 72 65 73 20 70 6f 69 6e 74 65 64 20 74  ctures pointed t
28c41 6f 20 6f 72 20 0a 2a 2a 20 64 65 73 63 65 6e 64  o or .** descend
28c42 65 64 20 66 72 6f 6d 20 74 68 65 20 45 78 70 72  ed from the Expr
28c43 2e 78 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72  .x.pList or Expr
28c44 2e 78 2e 70 53 65 6c 65 63 74 20 76 61 72 69 61  .x.pSelect varia
28c45 62 6c 65 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  bles)..*/.static
28c46 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 53 69   int dupedExprSi
28c47 7a 65 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20  ze(Expr *p, int 
28c48 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42  flags){.  int nB
28c49 79 74 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  yte = 0;.  if( p
28c4a 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20   ){.    nByte = 
28c4b 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a  dupedExprNodeSiz
28c4c 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20  e(p, flags);.   
28c4d 20 69 66 28 20 66 6c 61 67 73 26 45 58 50 52 44   if( flags&EXPRD
28c4e 55 50 5f 52 45 44 55 43 45 20 29 7b 0a 20 20 20  UP_REDUCE ){.   
28c4f 20 20 20 6e 42 79 74 65 20 2b 3d 20 64 75 70 65     nByte += dupe
28c50 64 45 78 70 72 53 69 7a 65 28 70 2d 3e 70 4c 65  dExprSize(p->pLe
28c51 66 74 2c 20 66 6c 61 67 73 29 20 2b 20 64 75 70  ft, flags) + dup
28c52 65 64 45 78 70 72 53 69 7a 65 28 70 2d 3e 70 52  edExprSize(p->pR
28c53 69 67 68 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20  ight, flags);.  
28c54 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
28c55 20 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   nByte;.}../*.**
28c56 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
28c57 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c  s similar to sql
28c58 69 74 65 33 45 78 70 72 44 75 70 28 29 2c 20 65  ite3ExprDup(), e
28c59 78 63 65 70 74 20 74 68 61 74 20 69 66 20 70 7a  xcept that if pz
28c5a 42 75 66 66 65 72 20 0a 2a 2a 20 69 73 20 6e 6f  Buffer .** is no
28c5b 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 42  t NULL then *pzB
28c5c 75 66 66 65 72 20 69 73 20 61 73 73 75 6d 65 64  uffer is assumed
28c5d 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 62   to point to a b
28c5e 75 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75  uffer large enou
28c5f 67 68 20 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 20  gh .** to store 
28c60 74 68 65 20 63 6f 70 79 20 6f 66 20 65 78 70 72  the copy of expr
28c61 65 73 73 69 6f 6e 20 70 2c 20 74 68 65 20 63 6f  ession p, the co
28c62 70 69 65 73 20 6f 66 20 70 2d 3e 75 2e 7a 54 6f  pies of p->u.zTo
28c63 6b 65 6e 0a 2a 2a 20 28 69 66 20 61 70 70 6c 69  ken.** (if appli
28c64 63 61 62 6c 65 29 2c 20 61 6e 64 20 74 68 65 20  cable), and the 
28c65 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 70 2d  copies of the p-
28c66 3e 70 4c 65 66 74 20 61 6e 64 20 70 2d 3e 70 52  >pLeft and p->pR
28c67 69 67 68 74 20 65 78 70 72 65 73 73 69 6f 6e 73  ight expressions
28c68 2c 0a 2a 2a 20 69 66 20 61 6e 79 2e 20 42 65 66  ,.** if any. Bef
28c69 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a  ore returning, *
28c6a 70 7a 42 75 66 66 65 72 20 69 73 20 73 65 74 20  pzBuffer is set 
28c6b 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 79 74  to the first byt
28c6c 65 20 70 61 73 73 65 64 20 74 68 65 0a 2a 2a 20  e passed the.** 
28c6d 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 62  portion of the b
28c6e 75 66 66 65 72 20 63 6f 70 69 65 64 20 69 6e 74  uffer copied int
28c6f 6f 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  o by this functi
28c70 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 45 78  on..*/.static Ex
28c71 70 72 20 2a 65 78 70 72 44 75 70 28 73 71 6c 69  pr *exprDup(sqli
28c72 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70  te3 *db, Expr *p
28c73 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 75 38 20  , int flags, u8 
28c74 2a 2a 70 7a 42 75 66 66 65 72 29 7b 0a 20 20 45  **pzBuffer){.  E
28c75 78 70 72 20 2a 70 4e 65 77 20 3d 20 30 3b 20 20  xpr *pNew = 0;  
28c76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28c77 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
28c78 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 66 28 20  return */.  if( 
28c79 70 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69  p ){.    const i
28c7a 6e 74 20 69 73 52 65 64 75 63 65 64 20 3d 20 28  nt isReduced = (
28c7b 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52 45  flags&EXPRDUP_RE
28c7c 44 55 43 45 29 3b 0a 20 20 20 20 75 38 20 2a 7a  DUCE);.    u8 *z
28c7d 41 6c 6c 6f 63 3b 0a 20 20 20 20 75 33 32 20 73  Alloc;.    u32 s
28c7e 74 61 74 69 63 46 6c 61 67 20 3d 20 30 3b 0a 0a  taticFlag = 0;..
28c7f 20 20 20 20 61 73 73 65 72 74 28 20 70 7a 42 75      assert( pzBu
28c80 66 66 65 72 3d 3d 30 20 7c 7c 20 69 73 52 65 64  ffer==0 || isRed
28c81 75 63 65 64 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  uced );..    /* 
28c82 46 69 67 75 72 65 20 6f 75 74 20 77 68 65 72 65  Figure out where
28c83 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 6e 65   to write the ne
28c84 77 20 45 78 70 72 20 73 74 72 75 63 74 75 72 65  w Expr structure
28c85 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 7a 42  . */.    if( pzB
28c86 75 66 66 65 72 20 29 7b 0a 20 20 20 20 20 20 7a  uffer ){.      z
28c87 41 6c 6c 6f 63 20 3d 20 2a 70 7a 42 75 66 66 65  Alloc = *pzBuffe
28c88 72 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63 46  r;.      staticF
28c89 6c 61 67 20 3d 20 45 50 5f 53 74 61 74 69 63 3b  lag = EP_Static;
28c8a 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
28c8b 20 20 7a 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74    zAlloc = sqlit
28c8c 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
28c8d 2c 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 28  , dupedExprSize(
28c8e 70 2c 20 66 6c 61 67 73 29 29 3b 0a 20 20 20 20  p, flags));.    
28c8f 7d 0a 20 20 20 20 70 4e 65 77 20 3d 20 28 45 78  }.    pNew = (Ex
28c90 70 72 20 2a 29 7a 41 6c 6c 6f 63 3b 0a 0a 20 20  pr *)zAlloc;..  
28c91 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20    if( pNew ){.  
28c92 20 20 20 20 2f 2a 20 53 65 74 20 6e 4e 65 77 53      /* Set nNewS
28c93 69 7a 65 20 74 6f 20 74 68 65 20 73 69 7a 65 20  ize to the size 
28c94 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68  allocated for th
28c95 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e  e structure poin
28c96 74 65 64 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  ted to.      ** 
28c97 62 79 20 70 4e 65 77 2e 20 54 68 69 73 20 69 73  by pNew. This is
28c98 20 65 69 74 68 65 72 20 45 58 50 52 5f 46 55 4c   either EXPR_FUL
28c99 4c 53 49 5a 45 2c 20 45 58 50 52 5f 52 45 44 55  LSIZE, EXPR_REDU
28c9a 43 45 44 53 49 5a 45 20 6f 72 0a 20 20 20 20 20  CEDSIZE or.     
28c9b 20 2a 2a 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e   ** EXPR_TOKENON
28c9c 4c 59 53 49 5a 45 2e 20 6e 54 6f 6b 65 6e 20 69  LYSIZE. nToken i
28c9d 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d  s set to the num
28c9e 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 6f 6e  ber of bytes con
28c9f 73 75 6d 65 64 0a 20 20 20 20 20 20 2a 2a 20 62  sumed.      ** b
28ca0 79 20 74 68 65 20 63 6f 70 79 20 6f 66 20 74 68  y the copy of th
28ca1 65 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74  e p->u.zToken st
28ca2 72 69 6e 67 20 28 69 66 20 61 6e 79 29 2e 0a 20  ring (if any).. 
28ca3 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f       */.      co
28ca4 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 6e 53 74  nst unsigned nSt
28ca5 72 75 63 74 53 69 7a 65 20 3d 20 64 75 70 65 64  ructSize = duped
28ca6 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 70  ExprStructSize(p
28ca7 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20  , flags);.      
28ca8 63 6f 6e 73 74 20 69 6e 74 20 6e 4e 65 77 53 69  const int nNewSi
28ca9 7a 65 20 3d 20 6e 53 74 72 75 63 74 53 69 7a 65  ze = nStructSize
28caa 20 26 20 30 78 66 66 66 3b 0a 20 20 20 20 20 20   & 0xfff;.      
28cab 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20  int nToken;.    
28cac 20 20 69 66 28 20 21 45 78 70 72 48 61 73 50 72    if( !ExprHasPr
28cad 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 49 6e 74  operty(p, EP_Int
28cae 56 61 6c 75 65 29 20 26 26 20 70 2d 3e 75 2e 7a  Value) && p->u.z
28caf 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 20  Token ){.       
28cb0 20 6e 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65   nToken = sqlite
28cb1 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e 75 2e 7a  3Strlen30(p->u.z
28cb2 54 6f 6b 65 6e 29 20 2b 20 31 3b 0a 20 20 20 20  Token) + 1;.    
28cb3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
28cb4 20 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 20   nToken = 0;.   
28cb5 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69     }.      if( i
28cb6 73 52 65 64 75 63 65 64 20 29 7b 0a 20 20 20 20  sReduced ){.    
28cb7 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70 72      assert( Expr
28cb8 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45  HasProperty(p, E
28cb9 50 5f 52 65 64 75 63 65 64 29 3d 3d 30 20 29 3b  P_Reduced)==0 );
28cba 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
28cbb 7a 41 6c 6c 6f 63 2c 20 70 2c 20 6e 4e 65 77 53  zAlloc, p, nNewS
28cbc 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ize);.      }els
28cbd 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  e{.        int n
28cbe 53 69 7a 65 20 3d 20 65 78 70 72 53 74 72 75 63  Size = exprStruc
28cbf 74 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20  tSize(p);.      
28cc0 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c    memcpy(zAlloc,
28cc1 20 70 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 20 20   p, nSize);.    
28cc2 20 20 20 20 6d 65 6d 73 65 74 28 26 7a 41 6c 6c      memset(&zAll
28cc3 6f 63 5b 6e 53 69 7a 65 5d 2c 20 30 2c 20 45 58  oc[nSize], 0, EX
28cc4 50 52 5f 46 55 4c 4c 53 49 5a 45 2d 6e 53 69 7a  PR_FULLSIZE-nSiz
28cc5 65 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  e);.      }..   
28cc6 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 45 50     /* Set the EP
28cc7 5f 52 65 64 75 63 65 64 2c 20 45 50 5f 54 6f 6b  _Reduced, EP_Tok
28cc8 65 6e 4f 6e 6c 79 2c 20 61 6e 64 20 45 50 5f 53  enOnly, and EP_S
28cc9 74 61 74 69 63 20 66 6c 61 67 73 20 61 70 70 72  tatic flags appr
28cca 6f 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 20  opriately. */.  
28ccb 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20      pNew->flags 
28ccc 26 3d 20 7e 28 45 50 5f 52 65 64 75 63 65 64 7c  &= ~(EP_Reduced|
28ccd 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f  EP_TokenOnly|EP_
28cce 53 74 61 74 69 63 29 3b 0a 20 20 20 20 20 20 70  Static);.      p
28ccf 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 6e 53  New->flags |= nS
28cd0 74 72 75 63 74 53 69 7a 65 20 26 20 28 45 50 5f  tructSize & (EP_
28cd1 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e  Reduced|EP_Token
28cd2 4f 6e 6c 79 29 3b 0a 20 20 20 20 20 20 70 4e 65  Only);.      pNe
28cd3 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 73 74 61 74  w->flags |= stat
28cd4 69 63 46 6c 61 67 3b 0a 0a 20 20 20 20 20 20 2f  icFlag;..      /
28cd5 2a 20 43 6f 70 79 20 74 68 65 20 70 2d 3e 75 2e  * Copy the p->u.
28cd6 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 2c 20 69  zToken string, i
28cd7 66 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 20 20  f any. */.      
28cd8 69 66 28 20 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20  if( nToken ){.  
28cd9 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b        char *zTok
28cda 65 6e 20 3d 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f  en = pNew->u.zTo
28cdb 6b 65 6e 20 3d 20 28 63 68 61 72 2a 29 26 7a 41  ken = (char*)&zA
28cdc 6c 6c 6f 63 5b 6e 4e 65 77 53 69 7a 65 5d 3b 0a  lloc[nNewSize];.
28cdd 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a          memcpy(z
28cde 54 6f 6b 65 6e 2c 20 70 2d 3e 75 2e 7a 54 6f 6b  Token, p->u.zTok
28cdf 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20  en, nToken);.   
28ce0 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20     }..      if( 
28ce1 30 3d 3d 28 28 70 2d 3e 66 6c 61 67 73 7c 70 4e  0==((p->flags|pN
28ce2 65 77 2d 3e 66 6c 61 67 73 29 20 26 20 45 50 5f  ew->flags) & EP_
28ce3 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20  TokenOnly) ){.  
28ce4 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e        /* Fill in
28ce5 20 74 68 65 20 70 4e 65 77 2d 3e 78 2e 70 53 65   the pNew->x.pSe
28ce6 6c 65 63 74 20 6f 72 20 70 4e 65 77 2d 3e 78 2e  lect or pNew->x.
28ce7 70 4c 69 73 74 20 6d 65 6d 62 65 72 2e 20 2a 2f  pList member. */
28ce8 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78 70  .        if( Exp
28ce9 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20  rHasProperty(p, 
28cea 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
28ceb 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d  .          pNew-
28cec 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c  >x.pSelect = sql
28ced 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62  ite3SelectDup(db
28cee 2c 20 70 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20  , p->x.pSelect, 
28cef 69 73 52 65 64 75 63 65 64 29 3b 0a 20 20 20 20  isReduced);.    
28cf0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
28cf1 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 4c 69       pNew->x.pLi
28cf2 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
28cf3 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 78  ListDup(db, p->x
28cf4 2e 70 4c 69 73 74 2c 20 69 73 52 65 64 75 63 65  .pList, isReduce
28cf5 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  d);.        }.  
28cf6 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
28cf7 46 69 6c 6c 20 69 6e 20 70 4e 65 77 2d 3e 70 4c  Fill in pNew->pL
28cf8 65 66 74 20 61 6e 64 20 70 4e 65 77 2d 3e 70 52  eft and pNew->pR
28cf9 69 67 68 74 2e 20 2a 2f 0a 20 20 20 20 20 20 69  ight. */.      i
28cfa 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f  f( ExprHasAnyPro
28cfb 70 65 72 74 79 28 70 4e 65 77 2c 20 45 50 5f 52  perty(pNew, EP_R
28cfc 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f  educed|EP_TokenO
28cfd 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20  nly) ){.        
28cfe 7a 41 6c 6c 6f 63 20 2b 3d 20 64 75 70 65 64 45  zAlloc += dupedE
28cff 78 70 72 4e 6f 64 65 53 69 7a 65 28 70 2c 20 66  xprNodeSize(p, f
28d00 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 69  lags);.        i
28d01 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
28d02 74 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 75  ty(pNew, EP_Redu
28d03 63 65 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ced) ){.        
28d04 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20    pNew->pLeft = 
28d05 65 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70  exprDup(db, p->p
28d06 4c 65 66 74 2c 20 45 58 50 52 44 55 50 5f 52 45  Left, EXPRDUP_RE
28d07 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b 0a  DUCE, &zAlloc);.
28d08 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e            pNew->
28d09 70 52 69 67 68 74 20 3d 20 65 78 70 72 44 75 70  pRight = exprDup
28d0a 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20  (db, p->pRight, 
28d0b 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20  EXPRDUP_REDUCE, 
28d0c 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20  &zAlloc);.      
28d0d 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
28d0e 70 7a 42 75 66 66 65 72 20 29 7b 0a 20 20 20 20  pzBuffer ){.    
28d0f 20 20 20 20 20 20 2a 70 7a 42 75 66 66 65 72 20        *pzBuffer 
28d10 3d 20 7a 41 6c 6c 6f 63 3b 0a 20 20 20 20 20 20  = zAlloc;.      
28d11 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
28d12 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66  .        pNew->f
28d13 6c 61 67 73 32 20 3d 20 30 3b 0a 20 20 20 20 20  lags2 = 0;.     
28d14 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73 41     if( !ExprHasA
28d15 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  nyProperty(p, EP
28d16 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20  _TokenOnly) ){. 
28d17 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70           pNew->p
28d18 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78  Left = sqlite3Ex
28d19 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65  prDup(db, p->pLe
28d1a 66 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ft, 0);.        
28d1b 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d    pNew->pRight =
28d1c 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
28d1d 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20 30  db, p->pRight, 0
28d1e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
28d1f 20 20 20 7d 0a 0a 20 20 20 20 7d 0a 20 20 7d 0a     }..    }.  }.
28d20 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
28d21 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
28d22 6f 77 69 6e 67 20 67 72 6f 75 70 20 6f 66 20 72  owing group of r
28d23 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 64 65 65  outines make dee
28d24 70 20 63 6f 70 69 65 73 20 6f 66 20 65 78 70 72  p copies of expr
28d25 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72  essions,.** expr
28d26 65 73 73 69 6f 6e 20 6c 69 73 74 73 2c 20 49 44  ession lists, ID
28d27 20 6c 69 73 74 73 2c 20 61 6e 64 20 73 65 6c 65   lists, and sele
28d28 63 74 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  ct statements.  
28d29 54 68 65 20 63 6f 70 69 65 73 20 63 61 6e 0a 2a  The copies can.*
28d2a 2a 20 62 65 20 64 65 6c 65 74 65 64 20 28 62 79  * be deleted (by
28d2b 20 62 65 69 6e 67 20 70 61 73 73 65 64 20 74 6f   being passed to
28d2c 20 74 68 65 69 72 20 72 65 73 70 65 63 74 69 76   their respectiv
28d2d 65 20 2e 2e 2e 44 65 6c 65 74 65 28 29 20 72 6f  e ...Delete() ro
28d2e 75 74 69 6e 65 73 29 0a 2a 2a 20 77 69 74 68 6f  utines).** witho
28d2f 75 74 20 65 66 66 65 63 74 69 6e 67 20 74 68 65  ut effecting the
28d30 20 6f 72 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a   originals..**.*
28d31 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
28d32 20 6c 69 73 74 2c 20 49 44 2c 20 61 6e 64 20 73   list, ID, and s
28d33 6f 75 72 63 65 20 6c 69 73 74 73 20 72 65 74 75  ource lists retu
28d34 72 6e 20 62 79 20 73 71 6c 69 74 65 33 45 78 70  rn by sqlite3Exp
28d35 72 4c 69 73 74 44 75 70 28 29 2c 0a 2a 2a 20 73  rListDup(),.** s
28d36 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28  qlite3IdListDup(
28d37 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 53 72  ), and sqlite3Sr
28d38 63 4c 69 73 74 44 75 70 28 29 20 63 61 6e 20 6e  cListDup() can n
28d39 6f 74 20 62 65 20 66 75 72 74 68 65 72 20 65 78  ot be further ex
28d3a 70 61 6e 64 65 64 20 0a 2a 2a 20 62 79 20 73 75  panded .** by su
28d3b 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
28d3c 6f 20 73 71 6c 69 74 65 2a 4c 69 73 74 41 70 70  o sqlite*ListApp
28d3d 65 6e 64 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a  end() routines..
28d3e 2a 2a 0a 2a 2a 20 41 6e 79 20 74 61 62 6c 65 73  **.** Any tables
28d3f 20 74 68 61 74 20 74 68 65 20 53 72 63 4c 69 73   that the SrcLis
28d40 74 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f  t might point to
28d41 20 61 72 65 20 6e 6f 74 20 64 75 70 6c 69 63 61   are not duplica
28d42 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ted..**.** The f
28d43 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63  lags parameter c
28d44 6f 6e 74 61 69 6e 73 20 61 20 63 6f 6d 62 69 6e  ontains a combin
28d45 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 45 58 50  ation of the EXP
28d46 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a  RDUP_XXX flags..
28d47 2a 2a 20 49 66 20 74 68 65 20 45 58 50 52 44 55  ** If the EXPRDU
28d48 50 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69 73  P_REDUCE flag is
28d49 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73   set, then the s
28d4a 74 72 75 63 74 75 72 65 20 72 65 74 75 72 6e 65  tructure returne
28d4b 64 20 69 73 20 61 0a 2a 2a 20 74 72 75 6e 63 61  d is a.** trunca
28d4c 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  ted version of t
28d4d 68 65 20 75 73 75 61 6c 20 45 78 70 72 20 73 74  he usual Expr st
28d4e 72 75 63 74 75 72 65 20 74 68 61 74 20 77 69 6c  ructure that wil
28d4f 6c 20 62 65 20 73 74 6f 72 65 64 20 61 73 0a 2a  l be stored as.*
28d50 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e  * part of the in
28d51 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e  -memory represen
28d52 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61  tation of the da
28d53 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a  tabase schema..*
28d54 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
28d55 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
28d56 70 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  prDup(sqlite3 *d
28d57 62 2c 20 45 78 70 72 20 2a 70 2c 20 69 6e 74 20  b, Expr *p, int 
28d58 66 6c 61 67 73 29 7b 0a 20 20 72 65 74 75 72 6e  flags){.  return
28d59 20 65 78 70 72 44 75 70 28 64 62 2c 20 70 2c 20   exprDup(db, p, 
28d5a 66 6c 61 67 73 2c 20 30 29 3b 0a 7d 0a 53 51 4c  flags, 0);.}.SQL
28d5b 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
28d5c 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70  List *sqlite3Exp
28d5d 72 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33  rListDup(sqlite3
28d5e 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20 2a   *db, ExprList *
28d5f 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  p, int flags){. 
28d60 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65 77 3b   ExprList *pNew;
28d61 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
28d62 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 2c 20  st_item *pItem, 
28d63 2a 70 4f 6c 64 49 74 65 6d 3b 0a 20 20 69 6e 74  *pOldItem;.  int
28d64 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29   i;.  if( p==0 )
28d65 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65   return 0;.  pNe
28d66 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
28d67 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
28d68 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69 66  f(*pNew) );.  if
28d69 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75  ( pNew==0 ) retu
28d6a 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 45  rn 0;.  pNew->iE
28d6b 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 70 4e  Cursor = 0;.  pN
28d6c 65 77 2d 3e 6e 45 78 70 72 20 3d 20 69 20 3d 20  ew->nExpr = i = 
28d6d 70 2d 3e 6e 45 78 70 72 3b 0a 20 20 69 66 28 20  p->nExpr;.  if( 
28d6e 28 66 6c 61 67 73 20 26 20 45 58 50 52 44 55 50  (flags & EXPRDUP
28d6f 5f 52 45 44 55 43 45 29 3d 3d 30 20 29 20 66 6f  _REDUCE)==0 ) fo
28d70 72 28 69 3d 31 3b 20 69 3c 70 2d 3e 6e 45 78 70  r(i=1; i<p->nExp
28d71 72 3b 20 69 2b 3d 69 29 7b 7d 0a 20 20 70 4e 65  r; i+=i){}.  pNe
28d72 77 2d 3e 61 20 3d 20 70 49 74 65 6d 20 3d 20 73  w->a = pItem = s
28d73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
28d74 77 28 64 62 2c 20 20 69 2a 73 69 7a 65 6f 66 28  w(db,  i*sizeof(
28d75 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66  p->a[0]) );.  if
28d76 28 20 70 49 74 65 6d 3d 3d 30 20 29 7b 0a 20 20  ( pItem==0 ){.  
28d77 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
28d78 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 72  db, pNew);.    r
28d79 65 74 75 72 6e 20 30 3b 0a 20 20 7d 20 0a 20 20  eturn 0;.  } .  
28d7a 70 4f 6c 64 49 74 65 6d 20 3d 20 70 2d 3e 61 3b  pOldItem = p->a;
28d7b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d  .  for(i=0; i<p-
28d7c 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74  >nExpr; i++, pIt
28d7d 65 6d 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d 2b 2b  em++, pOldItem++
28d7e 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6c  ){.    Expr *pOl
28d7f 64 45 78 70 72 20 3d 20 70 4f 6c 64 49 74 65 6d  dExpr = pOldItem
28d80 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 70 49 74  ->pExpr;.    pIt
28d81 65 6d 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69  em->pExpr = sqli
28d82 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
28d83 4f 6c 64 45 78 70 72 2c 20 66 6c 61 67 73 29 3b  OldExpr, flags);
28d84 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  .    pItem->zNam
28d85 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  e = sqlite3DbStr
28d86 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d  Dup(db, pOldItem
28d87 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 49  ->zName);.    pI
28d88 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20 73 71 6c  tem->zSpan = sql
28d89 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
28d8a 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 53 70 61 6e   pOldItem->zSpan
28d8b 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 73 6f  );.    pItem->so
28d8c 72 74 4f 72 64 65 72 20 3d 20 70 4f 6c 64 49 74  rtOrder = pOldIt
28d8d 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20  em->sortOrder;. 
28d8e 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d     pItem->done =
28d8f 20 30 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69   0;.    pItem->i
28d90 4f 72 64 65 72 42 79 43 6f 6c 20 3d 20 70 4f 6c  OrderByCol = pOl
28d91 64 49 74 65 6d 2d 3e 69 4f 72 64 65 72 42 79 43  dItem->iOrderByC
28d92 6f 6c 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69  ol;.    pItem->i
28d93 41 6c 69 61 73 20 3d 20 70 4f 6c 64 49 74 65 6d  Alias = pOldItem
28d94 2d 3e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20  ->iAlias;.  }.  
28d95 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a  return pNew;.}..
28d96 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 73  /*.** If cursors
28d97 2c 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 77  , triggers, view
28d98 73 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73  s and subqueries
28d99 20 61 72 65 20 61 6c 6c 20 6f 6d 69 74 74 65 64   are all omitted
28d9a 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62 75 69   from.** the bui
28d9b 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66  ld, then none of
28d9c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   the following r
28d9d 6f 75 74 69 6e 65 73 2c 20 65 78 63 65 70 74 20  outines, except 
28d9e 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 53  for .** sqlite3S
28d9f 65 6c 65 63 74 44 75 70 28 29 2c 20 63 61 6e 20  electDup(), can 
28da0 62 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c 69 74  be called. sqlit
28da1 65 33 53 65 6c 65 63 74 44 75 70 28 29 20 69 73  e3SelectDup() is
28da2 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 63 61   sometimes.** ca
28da3 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c  lled with a NULL
28da4 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 69   argument..*/.#i
28da5 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
28da6 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20  E_OMIT_VIEW) || 
28da7 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
28da8 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 5c 0a  OMIT_TRIGGER) \.
28da9 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
28daa 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
28dab 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  Y).SQLITE_PRIVAT
28dac 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74  E SrcList *sqlit
28dad 65 33 53 72 63 4c 69 73 74 44 75 70 28 73 71 6c  e3SrcListDup(sql
28dae 69 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69 73  ite3 *db, SrcLis
28daf 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29  t *p, int flags)
28db0 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4e 65  {.  SrcList *pNe
28db1 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  w;.  int i;.  in
28db2 74 20 6e 42 79 74 65 3b 0a 20 20 69 66 28 20 70  t nByte;.  if( p
28db3 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
28db4 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66    nByte = sizeof
28db5 28 2a 70 29 20 2b 20 28 70 2d 3e 6e 53 72 63 3e  (*p) + (p->nSrc>
28db6 30 20 3f 20 73 69 7a 65 6f 66 28 70 2d 3e 61 5b  0 ? sizeof(p->a[
28db7 30 5d 29 20 2a 20 28 70 2d 3e 6e 53 72 63 2d 31  0]) * (p->nSrc-1
28db8 29 20 3a 20 30 29 3b 0a 20 20 70 4e 65 77 20 3d  ) : 0);.  pNew =
28db9 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
28dba 52 61 77 28 64 62 2c 20 6e 42 79 74 65 20 29 3b  Raw(db, nByte );
28dbb 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29  .  if( pNew==0 )
28dbc 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65   return 0;.  pNe
28dbd 77 2d 3e 6e 53 72 63 20 3d 20 70 4e 65 77 2d 3e  w->nSrc = pNew->
28dbe 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 53 72 63  nAlloc = p->nSrc
28dbf 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
28dc0 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20  ->nSrc; i++){.  
28dc1 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
28dc2 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20  _item *pNewItem 
28dc3 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20  = &pNew->a[i];. 
28dc4 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73     struct SrcLis
28dc5 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d  t_item *pOldItem
28dc6 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20   = &p->a[i];.   
28dc7 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
28dc8 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 53 63 68    pNewItem->pSch
28dc9 65 6d 61 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  ema = pOldItem->
28dca 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 70 4e 65  pSchema;.    pNe
28dcb 77 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  wItem->zDatabase
28dcc 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
28dcd 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d  up(db, pOldItem-
28dce 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20  >zDatabase);.   
28dcf 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65   pNewItem->zName
28dd0 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
28dd1 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d  up(db, pOldItem-
28dd2 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65  >zName);.    pNe
28dd3 77 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20  wItem->zAlias = 
28dd4 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
28dd5 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 41  db, pOldItem->zA
28dd6 6c 69 61 73 29 3b 0a 20 20 20 20 70 4e 65 77 49  lias);.    pNewI
28dd7 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 3d 20  tem->jointype = 
28dd8 70 4f 6c 64 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79  pOldItem->jointy
28dd9 70 65 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  pe;.    pNewItem
28dda 2d 3e 69 43 75 72 73 6f 72 20 3d 20 70 4f 6c 64  ->iCursor = pOld
28ddb 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20  Item->iCursor;. 
28ddc 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 61 64 64     pNewItem->add
28ddd 72 46 69 6c 6c 53 75 62 20 3d 20 70 4f 6c 64 49  rFillSub = pOldI
28dde 74 65 6d 2d 3e 61 64 64 72 46 69 6c 6c 53 75 62  tem->addrFillSub
28ddf 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ;.    pNewItem->
28de0 72 65 67 52 65 74 75 72 6e 20 3d 20 70 4f 6c 64  regReturn = pOld
28de1 49 74 65 6d 2d 3e 72 65 67 52 65 74 75 72 6e 3b  Item->regReturn;
28de2 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69  .    pNewItem->i
28de3 73 43 6f 72 72 65 6c 61 74 65 64 20 3d 20 70 4f  sCorrelated = pO
28de4 6c 64 49 74 65 6d 2d 3e 69 73 43 6f 72 72 65 6c  ldItem->isCorrel
28de5 61 74 65 64 3b 0a 20 20 20 20 70 4e 65 77 49 74  ated;.    pNewIt
28de6 65 6d 2d 3e 76 69 61 43 6f 72 6f 75 74 69 6e 65  em->viaCoroutine
28de7 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 76 69 61   = pOldItem->via
28de8 43 6f 72 6f 75 74 69 6e 65 3b 0a 20 20 20 20 70  Coroutine;.    p
28de9 4e 65 77 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20  NewItem->zIndex 
28dea 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
28deb 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
28dec 7a 49 6e 64 65 78 29 3b 0a 20 20 20 20 70 4e 65  zIndex);.    pNe
28ded 77 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65  wItem->notIndexe
28dee 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f  d = pOldItem->no
28def 74 49 6e 64 65 78 65 64 3b 0a 20 20 20 20 70 4e  tIndexed;.    pN
28df0 65 77 49 74 65 6d 2d 3e 70 49 6e 64 65 78 20 3d  ewItem->pIndex =
28df1 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 49 6e 64 65   pOldItem->pInde
28df2 78 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 4e  x;.    pTab = pN
28df3 65 77 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70  ewItem->pTab = p
28df4 4f 6c 64 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20  OldItem->pTab;. 
28df5 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20     if( pTab ){. 
28df6 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b       pTab->nRef+
28df7 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65  +;.    }.    pNe
28df8 77 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d  wItem->pSelect =
28df9 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
28dfa 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
28dfb 70 53 65 6c 65 63 74 2c 20 66 6c 61 67 73 29 3b  pSelect, flags);
28dfc 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70  .    pNewItem->p
28dfd 4f 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  On = sqlite3Expr
28dfe 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d  Dup(db, pOldItem
28dff 2d 3e 70 4f 6e 2c 20 66 6c 61 67 73 29 3b 0a 20  ->pOn, flags);. 
28e00 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 55 73     pNewItem->pUs
28e01 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 49 64 4c  ing = sqlite3IdL
28e02 69 73 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 49  istDup(db, pOldI
28e03 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20  tem->pUsing);.  
28e04 20 20 70 4e 65 77 49 74 65 6d 2d 3e 63 6f 6c 55    pNewItem->colU
28e05 73 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  sed = pOldItem->
28e06 63 6f 6c 55 73 65 64 3b 0a 20 20 7d 0a 20 20 72  colUsed;.  }.  r
28e07 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51  eturn pNew;.}.SQ
28e08 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c  LITE_PRIVATE IdL
28e09 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69  ist *sqlite3IdLi
28e0a 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  stDup(sqlite3 *d
28e0b 62 2c 20 49 64 4c 69 73 74 20 2a 70 29 7b 0a 20  b, IdList *p){. 
28e0c 20 49 64 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20   IdList *pNew;. 
28e0d 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d   int i;.  if( p=
28e0e 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
28e0f 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
28e10 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
28e11 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a  izeof(*pNew) );.
28e12 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20    if( pNew==0 ) 
28e13 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77  return 0;.  pNew
28e14 2d 3e 6e 49 64 20 3d 20 70 2d 3e 6e 49 64 3b 0a  ->nId = p->nId;.
28e15 20 20 70 4e 65 77 2d 3e 61 20 3d 20 73 71 6c 69    pNew->a = sqli
28e16 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
28e17 62 2c 20 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66  b, p->nId*sizeof
28e18 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69  (p->a[0]) );.  i
28e19 66 28 20 70 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b  f( pNew->a==0 ){
28e1a 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
28e1b 65 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20  ee(db, pNew);.  
28e1c 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
28e1d 20 20 2f 2a 20 4e 6f 74 65 20 74 68 61 74 20 62    /* Note that b
28e1e 65 63 61 75 73 65 20 74 68 65 20 73 69 7a 65 20  ecause the size 
28e1f 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  of the allocatio
28e20 6e 20 66 6f 72 20 70 2d 3e 61 5b 5d 20 69 73 20  n for p->a[] is 
28e21 6e 6f 74 0a 20 20 2a 2a 20 6e 65 63 65 73 73 61  not.  ** necessa
28e22 72 69 6c 79 20 61 20 70 6f 77 65 72 20 6f 66 20  rily a power of 
28e23 74 77 6f 2c 20 73 71 6c 69 74 65 33 49 64 4c 69  two, sqlite3IdLi
28e24 73 74 41 70 70 65 6e 64 28 29 20 6d 61 79 20 6e  stAppend() may n
28e25 6f 74 20 62 65 20 63 61 6c 6c 65 64 0a 20 20 2a  ot be called.  *
28e26 2a 20 6f 6e 20 74 68 65 20 64 75 70 6c 69 63 61  * on the duplica
28e27 74 65 20 63 72 65 61 74 65 64 20 62 79 20 74 68  te created by th
28e28 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a  is function. */.
28e29 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
28e2a 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  nId; i++){.    s
28e2b 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65  truct IdList_ite
28e2c 6d 20 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 70  m *pNewItem = &p
28e2d 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73  New->a[i];.    s
28e2e 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65  truct IdList_ite
28e2f 6d 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70  m *pOldItem = &p
28e30 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 4e 65 77  ->a[i];.    pNew
28e31 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71  Item->zName = sq
28e32 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
28e33 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d  , pOldItem->zNam
28e34 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  e);.    pNewItem
28e35 2d 3e 69 64 78 20 3d 20 70 4f 6c 64 49 74 65 6d  ->idx = pOldItem
28e36 2d 3e 69 64 78 3b 0a 20 20 7d 0a 20 20 72 65 74  ->idx;.  }.  ret
28e37 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49  urn pNew;.}.SQLI
28e38 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63  TE_PRIVATE Selec
28e39 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74  t *sqlite3Select
28e3a 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Dup(sqlite3 *db,
28e3b 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20   Select *p, int 
28e3c 66 6c 61 67 73 29 7b 0a 20 20 53 65 6c 65 63 74  flags){.  Select
28e3d 20 2a 70 4e 65 77 2c 20 2a 70 50 72 69 6f 72 3b   *pNew, *pPrior;
28e3e 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
28e3f 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d  turn 0;.  pNew =
28e40 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
28e41 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  Raw(db, sizeof(*
28e42 70 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  p) );.  if( pNew
28e43 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
28e44 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d    pNew->pEList =
28e45 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
28e46 44 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73  Dup(db, p->pELis
28e47 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65  t, flags);.  pNe
28e48 77 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 74 65  w->pSrc = sqlite
28e49 33 53 72 63 4c 69 73 74 44 75 70 28 64 62 2c 20  3SrcListDup(db, 
28e4a 70 2d 3e 70 53 72 63 2c 20 66 6c 61 67 73 29 3b  p->pSrc, flags);
28e4b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65 20  .  pNew->pWhere 
28e4c 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
28e4d 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20  (db, p->pWhere, 
28e4e 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e  flags);.  pNew->
28e4f 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69 74  pGroupBy = sqlit
28e50 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
28e51 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 66  , p->pGroupBy, f
28e52 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70  lags);.  pNew->p
28e53 48 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 65 33  Having = sqlite3
28e54 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70  ExprDup(db, p->p
28e55 48 61 76 69 6e 67 2c 20 66 6c 61 67 73 29 3b 0a  Having, flags);.
28e56 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42 79    pNew->pOrderBy
28e57 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
28e58 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f 72  stDup(db, p->pOr
28e59 64 65 72 42 79 2c 20 66 6c 61 67 73 29 3b 0a 20  derBy, flags);. 
28e5a 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70 2d 3e 6f   pNew->op = p->o
28e5b 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50 72 69 6f  p;.  pNew->pPrio
28e5c 72 20 3d 20 70 50 72 69 6f 72 20 3d 20 73 71 6c  r = pPrior = sql
28e5d 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62  ite3SelectDup(db
28e5e 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20 66 6c 61  , p->pPrior, fla
28e5f 67 73 29 3b 0a 20 20 69 66 28 20 70 50 72 69 6f  gs);.  if( pPrio
28e60 72 20 29 20 70 50 72 69 6f 72 2d 3e 70 4e 65 78  r ) pPrior->pNex
28e61 74 20 3d 20 70 4e 65 77 3b 0a 20 20 70 4e 65 77  t = pNew;.  pNew
28e62 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70  ->pNext = 0;.  p
28e63 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71  New->pLimit = sq
28e64 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
28e65 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 66 6c 61 67   p->pLimit, flag
28e66 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 66 66  s);.  pNew->pOff
28e67 73 65 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  set = sqlite3Exp
28e68 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f 66 66  rDup(db, p->pOff
28e69 73 65 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70  set, flags);.  p
28e6a 4e 65 77 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b  New->iLimit = 0;
28e6b 0a 20 20 70 4e 65 77 2d 3e 69 4f 66 66 73 65 74  .  pNew->iOffset
28e6c 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 73 65   = 0;.  pNew->se
28e6d 6c 46 6c 61 67 73 20 3d 20 70 2d 3e 73 65 6c 46  lFlags = p->selF
28e6e 6c 61 67 73 20 26 20 7e 53 46 5f 55 73 65 73 45  lags & ~SF_UsesE
28e6f 70 68 65 6d 65 72 61 6c 3b 0a 20 20 70 4e 65 77  phemeral;.  pNew
28e70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30  ->pRightmost = 0
28e71 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70  ;.  pNew->addrOp
28e72 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a  enEphm[0] = -1;.
28e73 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e    pNew->addrOpen
28e74 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20  Ephm[1] = -1;.  
28e75 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70  pNew->addrOpenEp
28e76 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 72 65  hm[2] = -1;.  re
28e77 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 23 65 6c  turn pNew;.}.#el
28e78 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  se.SQLITE_PRIVAT
28e79 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65  E Select *sqlite
28e7a 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74  3SelectDup(sqlit
28e7b 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a  e3 *db, Select *
28e7c 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  p, int flags){. 
28e7d 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 29 3b   assert( p==0 );
28e7e 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23  .  return 0;.}.#
28e7f 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64  endif.../*.** Ad
28e80 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20  d a new element 
28e81 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 61 6e  to the end of an
28e82 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
28e83 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 0a 2a  .  If pList is.*
28e84 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c 4c  * initially NULL
28e85 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20  , then create a 
28e86 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  new expression l
28e87 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  ist..**.** If a 
28e88 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
28e89 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
28e8a 74 68 65 20 65 6e 74 69 72 65 20 6c 69 73 74 20  the entire list 
28e8b 69 73 20 66 72 65 65 64 20 61 6e 64 0a 2a 2a 20  is freed and.** 
28e8c 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
28e8d 2e 20 20 49 66 20 6e 6f 6e 2d 4e 55 4c 4c 20 69  .  If non-NULL i
28e8e 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e  s returned, then
28e8f 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65   it is guarantee
28e90 64 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6e 65  d.** that the ne
28e91 77 20 65 6e 74 72 79 20 77 61 73 20 73 75 63 63  w entry was succ
28e92 65 73 73 66 75 6c 6c 79 20 61 70 70 65 6e 64 65  essfully appende
28e93 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
28e94 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73  VATE ExprList *s
28e95 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
28e96 70 65 6e 64 28 0a 20 20 50 61 72 73 65 20 2a 70  pend(.  Parse *p
28e97 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
28e98 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
28e99 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
28e9a 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20   *pList,        
28e9b 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69 63 68  /* List to which
28e9c 20 74 6f 20 61 70 70 65 6e 64 2e 20 4d 69 67 68   to append. Migh
28e9d 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45  t be NULL */.  E
28e9e 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 20  xpr *pExpr      
28e9f 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73         /* Expres
28ea0 73 69 6f 6e 20 74 6f 20 62 65 20 61 70 70 65 6e  sion to be appen
28ea1 64 65 64 2e 20 4d 69 67 68 74 20 62 65 20 4e 55  ded. Might be NU
28ea2 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  LL */.){.  sqlit
28ea3 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
28ea4 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  >db;.  if( pList
28ea5 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74  ==0 ){.    pList
28ea6 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
28ea7 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
28ea8 66 28 45 78 70 72 4c 69 73 74 29 20 29 3b 0a 20  f(ExprList) );. 
28ea9 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20     if( pList==0 
28eaa 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f  ){.      goto no
28eab 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _mem;.    }.    
28eac 70 4c 69 73 74 2d 3e 61 20 3d 20 73 71 6c 69 74  pList->a = sqlit
28ead 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
28eae 2c 20 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e  , sizeof(pList->
28eaf 61 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20  a[0]));.    if( 
28eb0 70 4c 69 73 74 2d 3e 61 3d 3d 30 20 29 20 67 6f  pList->a==0 ) go
28eb1 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 65 6c  to no_mem;.  }el
28eb2 73 65 20 69 66 28 20 28 70 4c 69 73 74 2d 3e 6e  se if( (pList->n
28eb3 45 78 70 72 20 26 20 28 70 4c 69 73 74 2d 3e 6e  Expr & (pList->n
28eb4 45 78 70 72 2d 31 29 29 3d 3d 30 20 29 7b 0a 20  Expr-1))==0 ){. 
28eb5 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
28eb6 73 74 5f 69 74 65 6d 20 2a 61 3b 0a 20 20 20 20  st_item *a;.    
28eb7 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e  assert( pList->n
28eb8 45 78 70 72 3e 30 20 29 3b 0a 20 20 20 20 61 20  Expr>0 );.    a 
28eb9 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c  = sqlite3DbReall
28eba 6f 63 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 2c  oc(db, pList->a,
28ebb 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2a 32 2a   pList->nExpr*2*
28ebc 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b  sizeof(pList->a[
28ebd 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 61 3d  0]));.    if( a=
28ebe 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  =0 ){.      goto
28ebf 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20   no_mem;.    }. 
28ec0 20 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b     pList->a = a;
28ec1 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
28ec2 4c 69 73 74 2d 3e 61 21 3d 30 20 29 3b 0a 20 20  List->a!=0 );.  
28ec3 69 66 28 20 31 20 29 7b 0a 20 20 20 20 73 74 72  if( 1 ){.    str
28ec4 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
28ec5 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 4c 69 73  m *pItem = &pLis
28ec6 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70  t->a[pList->nExp
28ec7 72 2b 2b 5d 3b 0a 20 20 20 20 6d 65 6d 73 65 74  r++];.    memset
28ec8 28 70 49 74 65 6d 2c 20 30 2c 20 73 69 7a 65 6f  (pItem, 0, sizeo
28ec9 66 28 2a 70 49 74 65 6d 29 29 3b 0a 20 20 20 20  f(*pItem));.    
28eca 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70  pItem->pExpr = p
28ecb 45 78 70 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Expr;.  }.  retu
28ecc 72 6e 20 70 4c 69 73 74 3b 0a 0a 6e 6f 5f 6d 65  rn pList;..no_me
28ecd 6d 3a 20 20 20 20 20 0a 20 20 2f 2a 20 41 76 6f  m:     .  /* Avo
28ece 69 64 20 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72  id leaking memor
28ecf 79 20 69 66 20 6d 61 6c 6c 6f 63 20 68 61 73 20  y if malloc has 
28ed0 66 61 69 6c 65 64 2e 20 2a 2f 0a 20 20 73 71 6c  failed. */.  sql
28ed1 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
28ed2 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 73 71 6c  b, pExpr);.  sql
28ed3 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
28ed4 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20  te(db, pList);. 
28ed5 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
28ed6 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72  .** Set the Expr
28ed7 4c 69 73 74 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 65  List.a[].zName e
28ed8 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 6d 6f  lement of the mo
28ed9 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65  st recently adde
28eda 64 20 69 74 65 6d 0a 2a 2a 20 6f 6e 20 74 68 65  d item.** on the
28edb 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
28edc 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 6d 69  ..**.** pList mi
28edd 67 68 74 20 62 65 20 4e 55 4c 4c 20 66 6f 6c 6c  ght be NULL foll
28ede 6f 77 69 6e 67 20 61 6e 20 4f 4f 4d 20 65 72 72  owing an OOM err
28edf 6f 72 2e 20 20 42 75 74 20 70 4e 61 6d 65 20 73  or.  But pName s
28ee0 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 0a 2a  hould never be.*
28ee1 2a 20 4e 55 4c 4c 2e 20 20 49 66 20 61 20 6d 65  * NULL.  If a me
28ee2 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
28ee3 66 61 69 6c 73 2c 20 74 68 65 20 70 50 61 72 73  fails, the pPars
28ee4 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
28ee5 6c 65 64 20 66 6c 61 67 0a 2a 2a 20 69 73 20 73  led flag.** is s
28ee6 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  et..*/.SQLITE_PR
28ee7 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
28ee8 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d  e3ExprListSetNam
28ee9 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
28eea 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
28eeb 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
28eec 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
28eed 4c 69 73 74 2c 20 20 20 20 20 20 20 20 2f 2a 20  List,        /* 
28eee 4c 69 73 74 20 74 6f 20 77 68 69 63 68 20 74 6f  List to which to
28eef 20 61 64 64 20 74 68 65 20 73 70 61 6e 2e 20 2a   add the span. *
28ef0 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
28ef1 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
28ef2 61 6d 65 20 74 6f 20 62 65 20 61 64 64 65 64 20  ame to be added 
28ef3 2a 2f 0a 20 20 69 6e 74 20 64 65 71 75 6f 74 65  */.  int dequote
28ef4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28ef5 54 72 75 65 20 74 6f 20 63 61 75 73 65 20 74 68  True to cause th
28ef6 65 20 6e 61 6d 65 20 74 6f 20 62 65 20 64 65 71  e name to be deq
28ef7 75 6f 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 61 73  uoted */.){.  as
28ef8 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 7c  sert( pList!=0 |
28ef9 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  | pParse->db->ma
28efa 6c 6c 6f 63 46 61 69 6c 65 64 21 3d 30 20 29 3b  llocFailed!=0 );
28efb 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a  .  if( pList ){.
28efc 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
28efd 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
28efe 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69  .    assert( pLi
28eff 73 74 2d 3e 6e 45 78 70 72 3e 30 20 29 3b 0a 20  st->nExpr>0 );. 
28f00 20 20 20 70 49 74 65 6d 20 3d 20 26 70 4c 69 73     pItem = &pLis
28f01 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70  t->a[pList->nExp
28f02 72 2d 31 5d 3b 0a 20 20 20 20 61 73 73 65 72 74  r-1];.    assert
28f03 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3d 3d  ( pItem->zName==
28f04 30 20 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e  0 );.    pItem->
28f05 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44  zName = sqlite3D
28f06 62 53 74 72 4e 44 75 70 28 70 50 61 72 73 65 2d  bStrNDup(pParse-
28f07 3e 64 62 2c 20 70 4e 61 6d 65 2d 3e 7a 2c 20 70  >db, pName->z, p
28f08 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 69 66  Name->n);.    if
28f09 28 20 64 65 71 75 6f 74 65 20 26 26 20 70 49 74  ( dequote && pIt
28f0a 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 73 71 6c 69  em->zName ) sqli
28f0b 74 65 33 44 65 71 75 6f 74 65 28 70 49 74 65 6d  te3Dequote(pItem
28f0c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 7d 0a  ->zName);.  }.}.
28f0d 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45  ./*.** Set the E
28f0e 78 70 72 4c 69 73 74 2e 61 5b 5d 2e 7a 53 70 61  xprList.a[].zSpa
28f0f 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  n element of the
28f10 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61   most recently a
28f11 64 64 65 64 20 69 74 65 6d 0a 2a 2a 20 6f 6e 20  dded item.** on 
28f12 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  the expression l
28f13 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74  ist..**.** pList
28f14 20 6d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 66   might be NULL f
28f15 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20 4f 4f 4d 20  ollowing an OOM 
28f16 65 72 72 6f 72 2e 20 20 42 75 74 20 70 53 70 61  error.  But pSpa
28f17 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  n should never b
28f18 65 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 49 66 20 61  e.** NULL.  If a
28f19 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
28f1a 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 20 70 50  on fails, the pP
28f1b 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
28f1c 46 61 69 6c 65 64 20 66 6c 61 67 0a 2a 2a 20 69  Failed flag.** i
28f1d 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  s set..*/.SQLITE
28f1e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
28f1f 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74  lite3ExprListSet
28f20 53 70 61 6e 28 0a 20 20 50 61 72 73 65 20 2a 70  Span(.  Parse *p
28f21 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
28f22 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
28f23 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
28f24 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20   *pList,        
28f25 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69 63 68  /* List to which
28f26 20 74 6f 20 61 64 64 20 74 68 65 20 73 70 61 6e   to add the span
28f27 2e 20 2a 2f 0a 20 20 45 78 70 72 53 70 61 6e 20  . */.  ExprSpan 
28f28 2a 70 53 70 61 6e 20 20 20 20 20 20 20 20 20 2f  *pSpan         /
28f29 2a 20 54 68 65 20 73 70 61 6e 20 74 6f 20 62 65  * The span to be
28f2a 20 61 64 64 65 64 20 2a 2f 0a 29 7b 0a 20 20 73   added */.){.  s
28f2b 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
28f2c 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72  rse->db;.  asser
28f2d 74 28 20 70 4c 69 73 74 21 3d 30 20 7c 7c 20 64  t( pList!=0 || d
28f2e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 21  b->mallocFailed!
28f2f 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4c 69 73  =0 );.  if( pLis
28f30 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  t ){.    struct 
28f31 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
28f32 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61  Item = &pList->a
28f33 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 5d  [pList->nExpr-1]
28f34 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c  ;.    assert( pL
28f35 69 73 74 2d 3e 6e 45 78 70 72 3e 30 20 29 3b 0a  ist->nExpr>0 );.
28f36 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
28f37 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20  mallocFailed || 
28f38 70 49 74 65 6d 2d 3e 70 45 78 70 72 3d 3d 70 53  pItem->pExpr==pS
28f39 70 61 6e 2d 3e 70 45 78 70 72 20 29 3b 0a 20 20  pan->pExpr );.  
28f3a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
28f3b 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e  db, pItem->zSpan
28f3c 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 53  );.    pItem->zS
28f3d 70 61 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53  pan = sqlite3DbS
28f3e 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72  trNDup(db, (char
28f3f 2a 29 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74 2c  *)pSpan->zStart,
28f40 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
28f41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28f42 20 20 20 20 20 28 69 6e 74 29 28 70 53 70 61 6e       (int)(pSpan
28f43 2d 3e 7a 45 6e 64 20 2d 20 70 53 70 61 6e 2d 3e  ->zEnd - pSpan->
28f44 7a 53 74 61 72 74 29 29 3b 0a 20 20 7d 0a 7d 0a  zStart));.  }.}.
28f45 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78  ./*.** If the ex
28f46 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 70 45  pression list pE
28f47 4c 69 73 74 20 63 6f 6e 74 61 69 6e 73 20 6d 6f  List contains mo
28f48 72 65 20 74 68 61 6e 20 69 4c 69 6d 69 74 20 65  re than iLimit e
28f49 6c 65 6d 65 6e 74 73 2c 0a 2a 2a 20 6c 65 61 76  lements,.** leav
28f4a 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
28f4b 67 65 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f  ge in pParse..*/
28f4c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28f4d 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
28f4e 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28  ListCheckLength(
28f4f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
28f50 2c 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  ,.  ExprList *pE
28f51 4c 69 73 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68  List,.  const ch
28f52 61 72 20 2a 7a 4f 62 6a 65 63 74 0a 29 7b 0a 20  ar *zObject.){. 
28f53 20 69 6e 74 20 6d 78 20 3d 20 70 50 61 72 73 65   int mx = pParse
28f54 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ->db->aLimit[SQL
28f55 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e  ITE_LIMIT_COLUMN
28f56 5d 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70  ];.  testcase( p
28f57 45 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d  EList && pEList-
28f58 3e 6e 45 78 70 72 3d 3d 6d 78 20 29 3b 0a 20 20  >nExpr==mx );.  
28f59 74 65 73 74 63 61 73 65 28 20 70 45 4c 69 73 74  testcase( pEList
28f5a 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70   && pEList->nExp
28f5b 72 3d 3d 6d 78 2b 31 20 29 3b 0a 20 20 69 66 28  r==mx+1 );.  if(
28f5c 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c 69 73   pEList && pELis
28f5d 74 2d 3e 6e 45 78 70 72 3e 6d 78 20 29 7b 0a 20  t->nExpr>mx ){. 
28f5e 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
28f5f 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20  sg(pParse, "too 
28f60 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  many columns in 
28f61 25 73 22 2c 20 7a 4f 62 6a 65 63 74 29 3b 0a 20  %s", zObject);. 
28f62 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65   }.}../*.** Dele
28f63 74 65 20 61 6e 20 65 6e 74 69 72 65 20 65 78 70  te an entire exp
28f64 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f  ression list..*/
28f65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28f66 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
28f67 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74  ListDelete(sqlit
28f68 65 33 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 74  e3 *db, ExprList
28f69 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20   *pList){.  int 
28f6a 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  i;.  struct Expr
28f6b 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
28f6c 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
28f6d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
28f6e 65 72 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30  ert( pList->a!=0
28f6f 20 7c 7c 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72   || pList->nExpr
28f70 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 70 49 74  ==0 );.  for(pIt
28f71 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30  em=pList->a, i=0
28f72 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72  ; i<pList->nExpr
28f73 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
28f74 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
28f75 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d  Delete(db, pItem
28f76 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 73 71  ->pExpr);.    sq
28f77 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
28f78 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pItem->zName);. 
28f79 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
28f7a 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 53 70 61  (db, pItem->zSpa
28f7b 6e 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  n);.  }.  sqlite
28f7c 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73  3DbFree(db, pLis
28f7d 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 33  t->a);.  sqlite3
28f7e 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74  DbFree(db, pList
28f7f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73  );.}../*.** Thes
28f80 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 57  e routines are W
28f81 61 6c 6b 65 72 20 63 61 6c 6c 62 61 63 6b 73 2e  alker callbacks.
28f82 20 20 57 61 6c 6b 65 72 2e 75 2e 70 69 20 69 73    Walker.u.pi is
28f83 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f   a pointer.** to
28f84 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 54 68   an integer.  Th
28f85 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
28f86 20 63 68 65 63 6b 69 6e 67 20 61 6e 20 65 78 70   checking an exp
28f87 72 65 73 73 69 6f 6e 20 74 6f 20 73 65 65 0a 2a  ression to see.*
28f88 2a 20 69 66 20 69 74 20 69 73 20 61 20 63 6f 6e  * if it is a con
28f89 73 74 61 6e 74 2e 20 20 53 65 74 20 2a 57 61 6c  stant.  Set *Wal
28f8a 6b 65 72 2e 75 2e 70 69 20 74 6f 20 30 20 69 66  ker.u.pi to 0 if
28f8b 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
28f8c 69 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 73 74 61  is.** not consta
28f8d 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  nt..**.** These 
28f8e 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65  callback routine
28f8f 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d  s are used to im
28f90 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f 6c 6c  plement the foll
28f91 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  owing:.**.**    
28f92 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f   sqlite3ExprIsCo
28f93 6e 73 74 61 6e 74 28 29 0a 2a 2a 20 20 20 20 20  nstant().**     
28f94 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
28f95 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 29 0a 2a  stantNotJoin().*
28f96 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  *     sqlite3Exp
28f97 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e  rIsConstantOrFun
28f98 63 74 69 6f 6e 28 29 0a 2a 2a 0a 2a 2f 0a 73 74  ction().**.*/.st
28f99 61 74 69 63 20 69 6e 74 20 65 78 70 72 4e 6f 64  atic int exprNod
28f9a 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b  eIsConstant(Walk
28f9b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70  er *pWalker, Exp
28f9c 72 20 2a 70 45 78 70 72 29 7b 0a 0a 20 20 2f 2a  r *pExpr){..  /*
28f9d 20 49 66 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69   If pWalker->u.i
28f9e 20 69 73 20 33 20 74 68 65 6e 20 61 6e 79 20 74   is 3 then any t
28f9f 65 72 6d 20 6f 66 20 74 68 65 20 65 78 70 72 65  erm of the expre
28fa0 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6d 65 73  ssion that comes
28fa1 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 4f   from.  ** the O
28fa2 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73  N or USING claus
28fa3 65 73 20 6f 66 20 61 20 6a 6f 69 6e 20 64 69 73  es of a join dis
28fa4 71 75 61 6c 69 66 69 65 73 20 74 68 65 20 65 78  qualifies the ex
28fa5 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 66 72  pression.  ** fr
28fa6 6f 6d 20 62 65 69 6e 67 20 63 6f 6e 73 69 64 65  om being conside
28fa7 72 65 64 20 63 6f 6e 73 74 61 6e 74 2e 20 2a 2f  red constant. */
28fa8 0a 20 20 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e  .  if( pWalker->
28fa9 75 2e 69 3d 3d 33 20 26 26 20 45 78 70 72 48 61  u.i==3 && ExprHa
28faa 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78  sAnyProperty(pEx
28fab 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29  pr, EP_FromJoin)
28fac 20 29 7b 0a 20 20 20 20 70 57 61 6c 6b 65 72 2d   ){.    pWalker-
28fad 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 72 65  >u.i = 0;.    re
28fae 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
28faf 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70    }..  switch( p
28fb0 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  Expr->op ){.    
28fb1 2f 2a 20 43 6f 6e 73 69 64 65 72 20 66 75 6e 63  /* Consider func
28fb2 74 69 6f 6e 73 20 74 6f 20 62 65 20 63 6f 6e 73  tions to be cons
28fb3 74 61 6e 74 20 69 66 20 61 6c 6c 20 74 68 65 69  tant if all thei
28fb4 72 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20  r arguments are 
28fb5 63 6f 6e 73 74 61 6e 74 0a 20 20 20 20 2a 2a 20  constant.    ** 
28fb6 61 6e 64 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69  and pWalker->u.i
28fb7 3d 3d 32 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ==2 */.    case 
28fb8 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20  TK_FUNCTION:.   
28fb9 20 20 20 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e     if( pWalker->
28fba 75 2e 69 3d 3d 32 20 29 20 72 65 74 75 72 6e 20  u.i==2 ) return 
28fbb 30 3b 0a 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c  0;.      /* Fall
28fbc 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 20 20   through */.    
28fbd 63 61 73 65 20 54 4b 5f 49 44 3a 0a 20 20 20 20  case TK_ID:.    
28fbe 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 0a  case TK_COLUMN:.
28fbf 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f      case TK_AGG_
28fc0 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61  FUNCTION:.    ca
28fc1 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  se TK_AGG_COLUMN
28fc2 3a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  :.      testcase
28fc3 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
28fc4 49 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ID );.      test
28fc5 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
28fc6 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20  =TK_COLUMN );.  
28fc7 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
28fc8 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f  xpr->op==TK_AGG_
28fc9 46 55 4e 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20  FUNCTION );.    
28fca 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
28fcb 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f  r->op==TK_AGG_CO
28fcc 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 70 57  LUMN );.      pW
28fcd 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a  alker->u.i = 0;.
28fce 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
28fcf 5f 41 62 6f 72 74 3b 0a 20 20 20 20 64 65 66 61  _Abort;.    defa
28fd0 75 6c 74 3a 0a 20 20 20 20 20 20 74 65 73 74 63  ult:.      testc
28fd1 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  ase( pExpr->op==
28fd2 54 4b 5f 53 45 4c 45 43 54 20 29 3b 20 2f 2a 20  TK_SELECT ); /* 
28fd3 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73  selectNodeIsCons
28fd4 74 61 6e 74 20 77 69 6c 6c 20 64 69 73 61 6c 6c  tant will disall
28fd5 6f 77 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ow */.      test
28fd6 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
28fd7 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 20 2f 2a  =TK_EXISTS ); /*
28fd8 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e   selectNodeIsCon
28fd9 73 74 61 6e 74 20 77 69 6c 6c 20 64 69 73 61 6c  stant will disal
28fda 6c 6f 77 20 2a 2f 0a 20 20 20 20 20 20 72 65 74  low */.      ret
28fdb 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  urn WRC_Continue
28fdc 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 69  ;.  }.}.static i
28fdd 6e 74 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43  nt selectNodeIsC
28fde 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65 72 20 2a  onstant(Walker *
28fdf 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20  pWalker, Select 
28fe0 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55  *NotUsed){.  UNU
28fe1 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
28fe2 74 55 73 65 64 29 3b 0a 20 20 70 57 61 6c 6b 65  tUsed);.  pWalke
28fe3 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 72 65  r->u.i = 0;.  re
28fe4 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
28fe5 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 70  }.static int exp
28fe6 72 49 73 43 6f 6e 73 74 28 45 78 70 72 20 2a 70  rIsConst(Expr *p
28fe7 2c 20 69 6e 74 20 69 6e 69 74 46 6c 61 67 29 7b  , int initFlag){
28fe8 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77  .  Walker w;.  w
28fe9 2e 75 2e 69 20 3d 20 69 6e 69 74 46 6c 61 67 3b  .u.i = initFlag;
28fea 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61  .  w.xExprCallba
28feb 63 6b 20 3d 20 65 78 70 72 4e 6f 64 65 49 73 43  ck = exprNodeIsC
28fec 6f 6e 73 74 61 6e 74 3b 0a 20 20 77 2e 78 53 65  onstant;.  w.xSe
28fed 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 73  lectCallback = s
28fee 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74  electNodeIsConst
28fef 61 6e 74 3b 0a 20 20 73 71 6c 69 74 65 33 57 61  ant;.  sqlite3Wa
28ff0 6c 6b 45 78 70 72 28 26 77 2c 20 70 29 3b 0a 20  lkExpr(&w, p);. 
28ff1 20 72 65 74 75 72 6e 20 77 2e 75 2e 69 3b 0a 7d   return w.u.i;.}
28ff2 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20  ../*.** Walk an 
28ff3 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e  expression tree.
28ff4 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68    Return 1 if th
28ff5 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
28ff6 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 20  constant.** and 
28ff7 30 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65 73  0 if it involves
28ff8 20 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66 75   variables or fu
28ff9 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 2e 0a 2a 2a  nction calls..**
28ffa 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70  .** For the purp
28ffb 6f 73 65 73 20 6f 66 20 74 68 69 73 20 66 75 6e  oses of this fun
28ffc 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 6c 65 2d  ction, a double-
28ffd 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65  quoted string (e
28ffe 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 69 73 20  x: "abc").** is 
28fff 63 6f 6e 73 69 64 65 72 65 64 20 61 20 76 61 72  considered a var
29000 69 61 62 6c 65 20 62 75 74 20 61 20 73 69 6e 67  iable but a sing
29001 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67  le-quoted string
29002 20 28 65 78 3a 20 27 61 62 63 27 29 20 69 73 0a   (ex: 'abc') is.
29003 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a  ** a constant..*
29004 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
29005 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
29006 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72 20  IsConstant(Expr 
29007 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78  *p){.  return ex
29008 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 31 29 3b  prIsConst(p, 1);
29009 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61  .}../*.** Walk a
2900a 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  n expression tre
2900b 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20  e.  Return 1 if 
2900c 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
2900d 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 68  s constant.** th
2900e 61 74 20 64 6f 65 73 20 6e 6f 20 6f 72 69 67 69  at does no origi
2900f 6e 61 74 65 20 66 72 6f 6d 20 74 68 65 20 4f 4e  nate from the ON
29010 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65   or USING clause
29011 73 20 6f 66 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 20  s of a join..** 
29012 52 65 74 75 72 6e 20 30 20 69 66 20 69 74 20 69  Return 0 if it i
29013 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62 6c 65  nvolves variable
29014 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20 63 61  s or function ca
29015 6c 6c 73 20 6f 72 20 74 65 72 6d 73 20 66 72 6f  lls or terms fro
29016 6d 0a 2a 2a 20 61 6e 20 4f 4e 20 6f 72 20 55 53  m.** an ON or US
29017 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 53  ING clause..*/.S
29018 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
29019 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43  t sqlite3ExprIsC
2901a 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 45  onstantNotJoin(E
2901b 78 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  xpr *p){.  retur
2901c 6e 20 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c  n exprIsConst(p,
2901d 20 33 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61   3);.}../*.** Wa
2901e 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  lk an expression
2901f 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31   tree.  Return 1
29020 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69   if the expressi
29021 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a  on is constant.*
29022 2a 20 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20  * or a function 
29023 63 61 6c 6c 20 77 69 74 68 20 63 6f 6e 73 74 61  call with consta
29024 6e 74 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 52  nt arguments.  R
29025 65 74 75 72 6e 20 61 6e 64 20 30 20 69 66 20 74  eturn and 0 if t
29026 68 65 72 65 0a 2a 2a 20 61 72 65 20 61 6e 79 20  here.** are any 
29027 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a  variables..**.**
29028 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65   For the purpose
29029 73 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69  s of this functi
2902a 6f 6e 2c 20 61 20 64 6f 75 62 6c 65 2d 71 75 6f  on, a double-quo
2902b 74 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20  ted string (ex: 
2902c 22 61 62 63 22 29 0a 2a 2a 20 69 73 20 63 6f 6e  "abc").** is con
2902d 73 69 64 65 72 65 64 20 61 20 76 61 72 69 61 62  sidered a variab
2902e 6c 65 20 62 75 74 20 61 20 73 69 6e 67 6c 65 2d  le but a single-
2902f 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65  quoted string (e
29030 78 3a 20 27 61 62 63 27 29 20 69 73 0a 2a 2a 20  x: 'abc') is.** 
29031 61 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53  a constant..*/.S
29032 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
29033 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43  t sqlite3ExprIsC
29034 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f  onstantOrFunctio
29035 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72 65  n(Expr *p){.  re
29036 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73 74  turn exprIsConst
29037 28 70 2c 20 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  (p, 2);.}../*.**
29038 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
29039 6f 6e 20 70 20 63 6f 64 65 73 20 61 20 63 6f 6e  on p codes a con
2903a 73 74 61 6e 74 20 69 6e 74 65 67 65 72 20 74 68  stant integer th
2903b 61 74 20 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75  at is small enou
2903c 67 68 0a 2a 2a 20 74 6f 20 66 69 74 20 69 6e 20  gh.** to fit in 
2903d 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  a 32-bit integer
2903e 2c 20 72 65 74 75 72 6e 20 31 20 61 6e 64 20 70  , return 1 and p
2903f 75 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ut the value of 
29040 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 69  the integer.** i
29041 6e 20 2a 70 56 61 6c 75 65 2e 20 20 49 66 20 74  n *pValue.  If t
29042 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
29043 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 20   not an integer 
29044 6f 72 20 69 66 20 69 74 20 69 73 20 74 6f 6f 20  or if it is too 
29045 62 69 67 0a 2a 2a 20 74 6f 20 66 69 74 20 69 6e  big.** to fit in
29046 20 61 20 73 69 67 6e 65 64 20 33 32 2d 62 69 74   a signed 32-bit
29047 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e   integer, return
29048 20 30 20 61 6e 64 20 6c 65 61 76 65 20 2a 70 56   0 and leave *pV
29049 61 6c 75 65 20 75 6e 63 68 61 6e 67 65 64 2e 0a  alue unchanged..
2904a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2904b 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
2904c 72 49 73 49 6e 74 65 67 65 72 28 45 78 70 72 20  rIsInteger(Expr 
2904d 2a 70 2c 20 69 6e 74 20 2a 70 56 61 6c 75 65 29  *p, int *pValue)
2904e 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a  {.  int rc = 0;.
2904f 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 78 70 72  .  /* If an expr
29050 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 69 6e 74  ession is an int
29051 65 67 65 72 20 6c 69 74 65 72 61 6c 20 74 68 61  eger literal tha
29052 74 20 66 69 74 73 20 69 6e 20 61 20 73 69 67 6e  t fits in a sign
29053 65 64 20 33 32 2d 62 69 74 0a 20 20 2a 2a 20 69  ed 32-bit.  ** i
29054 6e 74 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65  nteger, then the
29055 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 66 6c 61   EP_IntValue fla
29056 67 20 77 69 6c 6c 20 68 61 76 65 20 61 6c 72 65  g will have alre
29057 61 64 79 20 62 65 65 6e 20 73 65 74 20 2a 2f 0a  ady been set */.
29058 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 70 21    assert( p->op!
29059 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 28  =TK_INTEGER || (
2905a 70 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e  p->flags & EP_In
2905b 74 56 61 6c 75 65 29 21 3d 30 0a 20 20 20 20 20  tValue)!=0.     
2905c 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
2905d 47 65 74 49 6e 74 33 32 28 70 2d 3e 75 2e 7a 54  GetInt32(p->u.zT
2905e 6f 6b 65 6e 2c 20 26 72 63 29 3d 3d 30 20 29 3b  oken, &rc)==0 );
2905f 0a 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73  ..  if( p->flags
29060 20 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 29   & EP_IntValue )
29061 7b 0a 20 20 20 20 2a 70 56 61 6c 75 65 20 3d 20  {.    *pValue = 
29062 70 2d 3e 75 2e 69 56 61 6c 75 65 3b 0a 20 20 20  p->u.iValue;.   
29063 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
29064 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29   switch( p->op )
29065 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50  {.    case TK_UP
29066 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 72 63 20  LUS: {.      rc 
29067 3d 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49  = sqlite3ExprIsI
29068 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c  nteger(p->pLeft,
29069 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 20 20   pValue);.      
2906a 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
2906b 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a   case TK_UMINUS:
2906c 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 76 3b 0a   {.      int v;.
2906d 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
2906e 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70  3ExprIsInteger(p
2906f 2d 3e 70 4c 65 66 74 2c 20 26 76 29 20 29 7b 0a  ->pLeft, &v) ){.
29070 20 20 20 20 20 20 20 20 2a 70 56 61 6c 75 65 20          *pValue 
29071 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 20 72 63  = -v;.        rc
29072 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
29073 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
29074 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 62 72  .    default: br
29075 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  eak;.  }.  retur
29076 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
29077 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20 74  eturn FALSE if t
29078 68 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63  here is no chanc
29079 65 20 74 68 61 74 20 74 68 65 20 65 78 70 72 65  e that the expre
2907a 73 73 69 6f 6e 20 63 61 6e 20 62 65 20 4e 55 4c  ssion can be NUL
2907b 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  L..**.** If the 
2907c 65 78 70 72 65 73 73 69 6f 6e 20 6d 69 67 68 74  expression might
2907d 20 62 65 20 4e 55 4c 4c 20 6f 72 20 69 66 20 74   be NULL or if t
2907e 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
2907f 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 0a 2a 2a 20   too complex.** 
29080 74 6f 20 74 65 6c 6c 20 72 65 74 75 72 6e 20 54  to tell return T
29081 52 55 45 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69  RUE.  .**.** Thi
29082 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
29083 64 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61  d as an optimiza
29084 74 69 6f 6e 2c 20 74 6f 20 73 6b 69 70 20 4f 50  tion, to skip OP
29085 5f 49 73 4e 75 6c 6c 20 6f 70 63 6f 64 65 73 0a  _IsNull opcodes.
29086 2a 2a 20 77 68 65 6e 20 77 65 20 6b 6e 6f 77 20  ** when we know 
29087 74 68 61 74 20 61 20 76 61 6c 75 65 20 63 61 6e  that a value can
29088 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20 20 48 65  not be NULL.  He
29089 6e 63 65 2c 20 61 20 66 61 6c 73 65 20 70 6f 73  nce, a false pos
2908a 69 74 69 76 65 0a 2a 2a 20 28 72 65 74 75 72 6e  itive.** (return
2908b 69 6e 67 20 54 52 55 45 20 77 68 65 6e 20 69 6e  ing TRUE when in
2908c 20 66 61 63 74 20 74 68 65 20 65 78 70 72 65 73   fact the expres
2908d 73 69 6f 6e 20 63 61 6e 20 6e 65 76 65 72 20 62  sion can never b
2908e 65 20 4e 55 4c 4c 29 20 6d 69 67 68 74 0a 2a 2a  e NULL) might.**
2908f 20 62 65 20 61 20 73 6d 61 6c 6c 20 70 65 72 66   be a small perf
29090 6f 72 6d 61 6e 63 65 20 68 69 74 20 62 75 74 20  ormance hit but 
29091 69 73 20 6f 74 68 65 72 77 69 73 65 20 68 61 72  is otherwise har
29092 6d 6c 65 73 73 2e 20 20 4f 6e 20 74 68 65 20 6f  mless.  On the o
29093 74 68 65 72 0a 2a 2a 20 68 61 6e 64 2c 20 61 20  ther.** hand, a 
29094 66 61 6c 73 65 20 6e 65 67 61 74 69 76 65 20 28  false negative (
29095 72 65 74 75 72 6e 69 6e 67 20 46 41 4c 53 45 20  returning FALSE 
29096 77 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20  when the result 
29097 63 6f 75 6c 64 20 62 65 20 4e 55 4c 4c 29 0a 2a  could be NULL).*
29098 2a 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 72 65  * will likely re
29099 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 72  sult in an incor
2909a 72 65 63 74 20 61 6e 73 77 65 72 2e 20 20 53 6f  rect answer.  So
2909b 20 77 68 65 6e 20 69 6e 20 64 6f 75 62 74 2c 20   when in doubt, 
2909c 72 65 74 75 72 6e 0a 2a 2a 20 54 52 55 45 2e 0a  return.** TRUE..
2909d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2909e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
2909f 72 43 61 6e 42 65 4e 75 6c 6c 28 63 6f 6e 73 74  rCanBeNull(const
290a0 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 75 38 20   Expr *p){.  u8 
290a1 6f 70 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e  op;.  while( p->
290a2 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 7c 7c 20  op==TK_UPLUS || 
290a3 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53  p->op==TK_UMINUS
290a4 20 29 7b 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74   ){ p = p->pLeft
290a5 3b 20 7d 0a 20 20 6f 70 20 3d 20 70 2d 3e 6f 70  ; }.  op = p->op
290a6 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52  ;.  if( op==TK_R
290a7 45 47 49 53 54 45 52 20 29 20 6f 70 20 3d 20 70  EGISTER ) op = p
290a8 2d 3e 6f 70 32 3b 0a 20 20 73 77 69 74 63 68 28  ->op2;.  switch(
290a9 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20   op ){.    case 
290aa 54 4b 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20  TK_INTEGER:.    
290ab 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 0a  case TK_STRING:.
290ac 20 20 20 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41      case TK_FLOA
290ad 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42  T:.    case TK_B
290ae 4c 4f 42 3a 0a 20 20 20 20 20 20 72 65 74 75 72  LOB:.      retur
290af 6e 20 30 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  n 0;.    default
290b0 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  :.      return 1
290b1 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ;.  }.}../*.** G
290b2 65 6e 65 72 61 74 65 20 61 6e 20 4f 50 5f 49 73  enerate an OP_Is
290b3 4e 75 6c 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e  Null instruction
290b4 20 74 68 61 74 20 74 65 73 74 73 20 72 65 67 69   that tests regi
290b5 73 74 65 72 20 69 52 65 67 20 61 6e 64 20 6a 75  ster iReg and ju
290b6 6d 70 73 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 69  mps.** to locati
290b7 6f 6e 20 69 44 65 73 74 20 69 66 20 74 68 65 20  on iDest if the 
290b8 76 61 6c 75 65 20 69 6e 20 69 52 65 67 20 69 73  value in iReg is
290b9 20 4e 55 4c 4c 2e 20 20 54 68 65 20 76 61 6c 75   NULL.  The valu
290ba 65 20 69 6e 20 69 52 65 67 20 0a 2a 2a 20 77 61  e in iReg .** wa
290bb 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20 70 45  s computed by pE
290bc 78 70 72 2e 20 20 49 66 20 77 65 20 63 61 6e 20  xpr.  If we can 
290bd 6c 6f 6f 6b 20 61 74 20 70 45 78 70 72 20 61 74  look at pExpr at
290be 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 61 6e   compile-time an
290bf 64 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 74  d.** determine t
290c0 68 61 74 20 69 74 20 63 61 6e 20 6e 65 76 65 72  hat it can never
290c1 20 67 65 6e 65 72 61 74 65 20 61 20 4e 55 4c 4c   generate a NULL
290c2 2c 20 74 68 65 6e 20 74 68 65 20 4f 50 5f 49 73  , then the OP_Is
290c3 4e 75 6c 6c 20 6f 70 65 72 61 74 69 6f 6e 0a 2a  Null operation.*
290c4 2a 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64  * can be omitted
290c5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
290c6 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
290c7 45 78 70 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75  ExprCodeIsNullJu
290c8 6d 70 28 0a 20 20 56 64 62 65 20 2a 76 2c 20 20  mp(.  Vdbe *v,  
290c9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
290ca 20 56 44 42 45 20 75 6e 64 65 72 20 63 6f 6e 73   VDBE under cons
290cb 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f  truction */.  co
290cc 6e 73 74 20 45 78 70 72 20 2a 70 45 78 70 72 2c  nst Expr *pExpr,
290cd 20 20 2f 2a 20 4f 6e 6c 79 20 67 65 6e 65 72 61    /* Only genera
290ce 74 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 69 66 20  te OP_IsNull if 
290cf 74 68 69 73 20 65 78 70 72 20 63 61 6e 20 62 65  this expr can be
290d0 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69   NULL */.  int i
290d1 52 65 67 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Reg,           /
290d2 2a 20 54 65 73 74 20 74 68 65 20 76 61 6c 75 65  * Test the value
290d3 20 69 6e 20 74 68 69 73 20 72 65 67 69 73 74 65   in this registe
290d4 72 20 66 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20  r for NULL */.  
290d5 69 6e 74 20 69 44 65 73 74 20 20 20 20 20 20 20  int iDest       
290d6 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
290d7 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 73   if the value is
290d8 20 6e 75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 69 66   null */.){.  if
290d9 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 6e  ( sqlite3ExprCan
290da 42 65 4e 75 6c 6c 28 70 45 78 70 72 29 20 29 7b  BeNull(pExpr) ){
290db 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
290dc 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e  AddOp2(v, OP_IsN
290dd 75 6c 6c 2c 20 69 52 65 67 2c 20 69 44 65 73 74  ull, iReg, iDest
290de 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
290df 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74  Return TRUE if t
290e0 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73  he given express
290e1 69 6f 6e 20 69 73 20 61 20 63 6f 6e 73 74 61 6e  ion is a constan
290e2 74 20 77 68 69 63 68 20 77 6f 75 6c 64 20 62 65  t which would be
290e3 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 62 79  .** unchanged by
290e4 20 4f 50 5f 41 66 66 69 6e 69 74 79 20 77 69 74   OP_Affinity wit
290e5 68 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 67  h the affinity g
290e6 69 76 65 6e 20 69 6e 20 74 68 65 20 73 65 63 6f  iven in the seco
290e7 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a  nd.** argument..
290e8 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
290e9 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 64 65  ne is used to de
290ea 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 4f  termine if the O
290eb 50 5f 41 66 66 69 6e 69 74 79 20 6f 70 65 72 61  P_Affinity opera
290ec 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 6f  tion.** can be o
290ed 6d 69 74 74 65 64 2e 20 20 57 68 65 6e 20 69 6e  mitted.  When in
290ee 20 64 6f 75 62 74 20 72 65 74 75 72 6e 20 46 41   doubt return FA
290ef 4c 53 45 2e 20 20 41 20 66 61 6c 73 65 20 6e 65  LSE.  A false ne
290f0 67 61 74 69 76 65 0a 2a 2a 20 69 73 20 68 61 72  gative.** is har
290f1 6d 6c 65 73 73 2e 20 20 41 20 66 61 6c 73 65 20  mless.  A false 
290f2 70 6f 73 69 74 69 76 65 2c 20 68 6f 77 65 76 65  positive, howeve
290f3 72 2c 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e  r, can result in
290f4 20 74 68 65 20 77 72 6f 6e 67 0a 2a 2a 20 61 6e   the wrong.** an
290f5 73 77 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  swer..*/.SQLITE_
290f6 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
290f7 74 65 33 45 78 70 72 4e 65 65 64 73 4e 6f 41 66  te3ExprNeedsNoAf
290f8 66 69 6e 69 74 79 43 68 61 6e 67 65 28 63 6f 6e  finityChange(con
290f9 73 74 20 45 78 70 72 20 2a 70 2c 20 63 68 61 72  st Expr *p, char
290fa 20 61 66 66 29 7b 0a 20 20 75 38 20 6f 70 3b 0a   aff){.  u8 op;.
290fb 20 20 69 66 28 20 61 66 66 3d 3d 53 51 4c 49 54    if( aff==SQLIT
290fc 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 20 72 65 74  E_AFF_NONE ) ret
290fd 75 72 6e 20 31 3b 0a 20 20 77 68 69 6c 65 28 20  urn 1;.  while( 
290fe 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20  p->op==TK_UPLUS 
290ff 7c 7c 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4d 49  || p->op==TK_UMI
29100 4e 55 53 20 29 7b 20 70 20 3d 20 70 2d 3e 70 4c  NUS ){ p = p->pL
29101 65 66 74 3b 20 7d 0a 20 20 6f 70 20 3d 20 70 2d  eft; }.  op = p-
29102 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54  >op;.  if( op==T
29103 4b 5f 52 45 47 49 53 54 45 52 20 29 20 6f 70 20  K_REGISTER ) op 
29104 3d 20 70 2d 3e 6f 70 32 3b 0a 20 20 73 77 69 74  = p->op2;.  swit
29105 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61  ch( op ){.    ca
29106 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b  se TK_INTEGER: {
29107 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 66  .      return af
29108 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e  f==SQLITE_AFF_IN
29109 54 45 47 45 52 20 7c 7c 20 61 66 66 3d 3d 53 51  TEGER || aff==SQ
2910a 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
2910b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2910c 20 54 4b 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20   TK_FLOAT: {.   
2910d 20 20 20 72 65 74 75 72 6e 20 61 66 66 3d 3d 53     return aff==S
2910e 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 7c  QLITE_AFF_REAL |
2910f 7c 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46  | aff==SQLITE_AF
29110 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d  F_NUMERIC;.    }
29111 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52  .    case TK_STR
29112 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 72 65 74  ING: {.      ret
29113 75 72 6e 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f  urn aff==SQLITE_
29114 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 7d 0a  AFF_TEXT;.    }.
29115 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42      case TK_BLOB
29116 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  : {.      return
29117 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
29118 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a  se TK_COLUMN: {.
29119 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
2911a 3e 69 54 61 62 6c 65 3e 3d 30 20 29 3b 20 20 2f  >iTable>=0 );  /
2911b 2a 20 70 20 63 61 6e 6e 6f 74 20 62 65 20 70 61  * p cannot be pa
2911c 72 74 20 6f 66 20 61 20 43 48 45 43 4b 20 63 6f  rt of a CHECK co
2911d 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20  nstraint */.    
2911e 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 43 6f 6c    return p->iCol
2911f 75 6d 6e 3c 30 0a 20 20 20 20 20 20 20 20 20 20  umn<0.          
29120 26 26 20 28 61 66 66 3d 3d 53 51 4c 49 54 45 5f  && (aff==SQLITE_
29121 41 46 46 5f 49 4e 54 45 47 45 52 20 7c 7c 20 61  AFF_INTEGER || a
29122 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ff==SQLITE_AFF_N
29123 55 4d 45 52 49 43 29 3b 0a 20 20 20 20 7d 0a 20  UMERIC);.    }. 
29124 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
29125 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
29126 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
29127 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
29128 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67  the given string
29129 20 69 73 20 61 20 72 6f 77 2d 69 64 20 63 6f 6c   is a row-id col
2912a 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c  umn name..*/.SQL
2912b 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2912c 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 63  sqlite3IsRowid(c
2912d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
2912e 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
2912f 43 6d 70 28 7a 2c 20 22 5f 52 4f 57 49 44 5f 22  Cmp(z, "_ROWID_"
29130 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b  )==0 ) return 1;
29131 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
29132 72 49 43 6d 70 28 7a 2c 20 22 52 4f 57 49 44 22  rICmp(z, "ROWID"
29133 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b  )==0 ) return 1;
29134 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
29135 72 49 43 6d 70 28 7a 2c 20 22 4f 49 44 22 29 3d  rICmp(z, "OID")=
29136 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  =0 ) return 1;. 
29137 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
29138 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
29139 69 66 20 77 65 20 61 72 65 20 61 62 6c 65 20 74  if we are able t
2913a 6f 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f  o the IN operato
2913b 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f  r optimization o
2913c 6e 20 61 0a 2a 2a 20 71 75 65 72 79 20 6f 66 20  n a.** query of 
2913d 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20  the form.**.**  
2913e 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43       x IN (SELEC
2913f 54 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 57 68 65  T ...).**.** Whe
29140 72 65 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e  re the SELECT...
29141 20 63 6c 61 75 73 65 20 69 73 20 61 73 20 73 70   clause is as sp
29142 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 70  ecified by the p
29143 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73  arameter to this
29144 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a  .** routine..**.
29145 2a 2a 20 54 68 65 20 53 65 6c 65 63 74 20 6f 62  ** The Select ob
29146 6a 65 63 74 20 70 61 73 73 65 64 20 69 6e 20 68  ject passed in h
29147 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
29148 70 72 65 70 72 6f 63 65 73 73 65 64 20 61 6e 64  preprocessed and
29149 20 6e 6f 0a 2a 2a 20 65 72 72 6f 72 73 20 68 61   no.** errors ha
2914a 76 65 20 62 65 65 6e 20 66 6f 75 6e 64 2e 0a 2a  ve been found..*
2914b 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
2914c 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 73  _OMIT_SUBQUERY.s
2914d 74 61 74 69 63 20 69 6e 74 20 69 73 43 61 6e 64  tatic int isCand
2914e 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 53 65  idateForInOpt(Se
2914f 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c  lect *p){.  SrcL
29150 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 45 78 70  ist *pSrc;.  Exp
29151 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20  rList *pEList;. 
29152 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
29153 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
29154 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
29155 20 20 20 20 20 20 20 2f 2a 20 72 69 67 68 74 2d         /* right-
29156 68 61 6e 64 20 73 69 64 65 20 6f 66 20 49 4e 20  hand side of IN 
29157 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69  is SELECT */.  i
29158 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 20 72  f( p->pPrior ) r
29159 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
2915a 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 61 20 63        /* Not a c
2915b 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 2a  ompound SELECT *
2915c 2f 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c  /.  if( p->selFl
2915d 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e  ags & (SF_Distin
2915e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29  ct|SF_Aggregate)
2915f 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65   ){.    testcase
29160 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  ( (p->selFlags &
29161 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46   (SF_Distinct|SF
29162 5f 41 67 67 72 65 67 61 74 65 29 29 3d 3d 53 46  _Aggregate))==SF
29163 5f 44 69 73 74 69 6e 63 74 20 29 3b 0a 20 20 20  _Distinct );.   
29164 20 74 65 73 74 63 61 73 65 28 20 28 70 2d 3e 73   testcase( (p->s
29165 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69  elFlags & (SF_Di
29166 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67  stinct|SF_Aggreg
29167 61 74 65 29 29 3d 3d 53 46 5f 41 67 67 72 65 67  ate))==SF_Aggreg
29168 61 74 65 20 29 3b 0a 20 20 20 20 72 65 74 75 72  ate );.    retur
29169 6e 20 30 3b 20 2f 2a 20 4e 6f 20 44 49 53 54 49  n 0; /* No DISTI
2916a 4e 43 54 20 6b 65 79 77 6f 72 64 20 61 6e 64 20  NCT keyword and 
2916b 6e 6f 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  no aggregate fun
2916c 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20  ctions */.  }.  
2916d 61 73 73 65 72 74 28 20 70 2d 3e 70 47 72 6f 75  assert( p->pGrou
2916e 70 42 79 3d 3d 30 20 29 3b 20 20 20 20 20 20 20  pBy==0 );       
2916f 20 20 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f         /* Has no
29170 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
29171 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69   */.  if( p->pLi
29172 6d 69 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20  mit ) return 0; 
29173 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29174 48 61 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61  Has no LIMIT cla
29175 75 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  use */.  assert(
29176 20 70 2d 3e 70 4f 66 66 73 65 74 3d 3d 30 20 29   p->pOffset==0 )
29177 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
29178 2f 2a 20 4e 6f 20 4c 49 4d 49 54 20 6d 65 61 6e  /* No LIMIT mean
29179 73 20 6e 6f 20 4f 46 46 53 45 54 20 2a 2f 0a 20  s no OFFSET */. 
2917a 20 69 66 28 20 70 2d 3e 70 57 68 65 72 65 20 29   if( p->pWhere )
2917b 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
2917c 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 20 6e          /* Has n
2917d 6f 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  o WHERE clause *
2917e 2f 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53  /.  pSrc = p->pS
2917f 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53  rc;.  assert( pS
29180 72 63 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  rc!=0 );.  if( p
29181 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20 29 20 72  Src->nSrc!=1 ) r
29182 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
29183 20 20 2f 2a 20 53 69 6e 67 6c 65 20 74 65 72 6d    /* Single term
29184 20 69 6e 20 46 52 4f 4d 20 63 6c 61 75 73 65 20   in FROM clause 
29185 2a 2f 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 61  */.  if( pSrc->a
29186 5b 30 5d 2e 70 53 65 6c 65 63 74 20 29 20 72 65  [0].pSelect ) re
29187 74 75 72 6e 20 30 3b 20 20 20 20 20 2f 2a 20 46  turn 0;     /* F
29188 52 4f 4d 20 69 73 20 6e 6f 74 20 61 20 73 75 62  ROM is not a sub
29189 71 75 65 72 79 20 6f 72 20 76 69 65 77 20 2a 2f  query or view */
2918a 0a 20 20 70 54 61 62 20 3d 20 70 53 72 63 2d 3e  .  pTab = pSrc->
2918b 61 5b 30 5d 2e 70 54 61 62 3b 0a 20 20 69 66 28  a[0].pTab;.  if(
2918c 20 4e 45 56 45 52 28 70 54 61 62 3d 3d 30 29 20   NEVER(pTab==0) 
2918d 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73  ) return 0;.  as
2918e 73 65 72 74 28 20 70 54 61 62 2d 3e 70 53 65 6c  sert( pTab->pSel
2918f 65 63 74 3d 3d 30 20 29 3b 20 20 20 20 20 20 20  ect==0 );       
29190 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61       /* FROM cla
29191 75 73 65 20 69 73 20 6e 6f 74 20 61 20 76 69 65  use is not a vie
29192 77 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72  w */.  if( IsVir
29193 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65 74  tual(pTab) ) ret
29194 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  urn 0;        /*
29195 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6e 6f 74   FROM clause not
29196 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
29197 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20 70   */.  pEList = p
29198 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 20  ->pEList;.  if( 
29199 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31  pEList->nExpr!=1
2919a 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
2919b 20 20 20 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e     /* One column
2919c 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
2919d 65 74 20 2a 2f 0a 20 20 69 66 28 20 70 45 4c 69  et */.  if( pELi
2919e 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e  st->a[0].pExpr->
2919f 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20  op!=TK_COLUMN ) 
291a0 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 52 65 73  return 0; /* Res
291a1 75 6c 74 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20  ult is a column 
291a2 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  */.  return 1;.}
291a3 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
291a4 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20  E_OMIT_SUBQUERY 
291a5 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61  */../*.** Code a
291a6 6e 20 4f 50 5f 4f 6e 63 65 20 69 6e 73 74 72 75  n OP_Once instru
291a7 63 74 69 6f 6e 20 61 6e 64 20 61 6c 6c 6f 63 61  ction and alloca
291a8 74 65 20 73 70 61 63 65 20 66 6f 72 20 69 74 73  te space for its
291a9 20 66 6c 61 67 2e 20 52 65 74 75 72 6e 20 74 68   flag. Return th
291aa 65 20 0a 2a 2a 20 61 64 64 72 65 73 73 20 6f 66  e .** address of
291ab 20 74 68 65 20 6e 65 77 20 69 6e 73 74 72 75 63   the new instruc
291ac 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
291ad 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
291ae 74 65 33 43 6f 64 65 4f 6e 63 65 28 50 61 72 73  te3CodeOnce(Pars
291af 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 56 64  e *pParse){.  Vd
291b0 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
291b1 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 20  etVdbe(pParse); 
291b2 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20       /* Virtual 
291b3 6d 61 63 68 69 6e 65 20 62 65 69 6e 67 20 63 6f  machine being co
291b4 64 65 64 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ded */.  return 
291b5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
291b6 31 28 76 2c 20 4f 50 5f 4f 6e 63 65 2c 20 70 50  1(v, OP_Once, pP
291b7 61 72 73 65 2d 3e 6e 4f 6e 63 65 2b 2b 29 3b 0a  arse->nOnce++);.
291b8 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
291b9 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62  nction is used b
291ba 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  y the implementa
291bb 74 69 6f 6e 20 6f 66 20 74 68 65 20 49 4e 20 28  tion of the IN (
291bc 2e 2e 2e 29 20 6f 70 65 72 61 74 6f 72 2e 0a 2a  ...) operator..*
291bd 2a 20 54 68 65 20 70 58 20 70 61 72 61 6d 65 74  * The pX paramet
291be 65 72 20 69 73 20 74 68 65 20 65 78 70 72 65 73  er is the expres
291bf 73 69 6f 6e 20 6f 6e 20 74 68 65 20 52 48 53 20  sion on the RHS 
291c0 6f 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74  of the IN operat
291c1 6f 72 2c 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67  or, which.** mig
291c2 68 74 20 62 65 20 65 69 74 68 65 72 20 61 20 6c  ht be either a l
291c3 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f  ist of expressio
291c4 6e 73 20 6f 72 20 61 20 73 75 62 71 75 65 72 79  ns or a subquery
291c5 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 62 20  ..**.** The job 
291c6 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  of this routine 
291c7 69 73 20 74 6f 20 66 69 6e 64 20 6f 72 20 63 72  is to find or cr
291c8 65 61 74 65 20 61 20 62 2d 74 72 65 65 20 6f 62  eate a b-tree ob
291c9 6a 65 63 74 20 74 68 61 74 20 63 61 6e 0a 2a 2a  ject that can.**
291ca 20 62 65 20 75 73 65 64 20 65 69 74 68 65 72 20   be used either 
291cb 74 6f 20 74 65 73 74 20 66 6f 72 20 6d 65 6d 62  to test for memb
291cc 65 72 73 68 69 70 20 69 6e 20 74 68 65 20 52 48  ership in the RH
291cd 53 20 73 65 74 20 6f 72 20 74 6f 20 69 74 65 72  S set or to iter
291ce 61 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20 61  ate through.** a
291cf 6c 6c 20 6d 65 6d 62 65 72 73 20 6f 66 20 74 68  ll members of th
291d0 65 20 52 48 53 20 73 65 74 2c 20 73 6b 69 70 70  e RHS set, skipp
291d1 69 6e 67 20 64 75 70 6c 69 63 61 74 65 73 2e 0a  ing duplicates..
291d2 2a 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69  **.** A cursor i
291d3 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 20  s opened on the 
291d4 62 2d 74 72 65 65 20 6f 62 6a 65 63 74 20 74 68  b-tree object th
291d5 61 74 20 74 68 65 20 52 48 53 20 6f 66 20 74 68  at the RHS of th
291d6 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 0a 2a 2a  e IN operator.**
291d7 20 61 6e 64 20 70 58 2d 3e 69 54 61 62 6c 65 20   and pX->iTable 
291d8 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 69 6e  is set to the in
291d9 64 65 78 20 6f 66 20 74 68 61 74 20 63 75 72 73  dex of that curs
291da 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  or..**.** The re
291db 74 75 72 6e 65 64 20 76 61 6c 75 65 20 6f 66 20  turned value of 
291dc 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e  this function in
291dd 64 69 63 61 74 65 73 20 74 68 65 20 62 2d 74 72  dicates the b-tr
291de 65 65 20 74 79 70 65 2c 20 61 73 20 66 6f 6c 6c  ee type, as foll
291df 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 5f  ows:.**.**   IN_
291e0 49 4e 44 45 58 5f 52 4f 57 49 44 20 2d 20 54 68  INDEX_ROWID - Th
291e1 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65  e cursor was ope
291e2 6e 65 64 20 6f 6e 20 61 20 64 61 74 61 62 61 73  ned on a databas
291e3 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 49 4e  e table..**   IN
291e4 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20 2d 20 54  _INDEX_INDEX - T
291e5 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70  he cursor was op
291e6 65 6e 65 64 20 6f 6e 20 61 20 64 61 74 61 62 61  ened on a databa
291e7 73 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 20 20 49  se index..**   I
291e8 4e 5f 49 4e 44 45 58 5f 45 50 48 20 2d 20 20 20  N_INDEX_EPH -   
291e9 54 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f  The cursor was o
291ea 70 65 6e 65 64 20 6f 6e 20 61 20 73 70 65 63 69  pened on a speci
291eb 61 6c 6c 79 20 63 72 65 61 74 65 64 20 61 6e 64  ally created and
291ec 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
291ed 20 20 20 20 20 20 20 70 6f 70 75 6c 61 74 65 64         populated
291ee 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65   epheremal table
291ef 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 69 73 74  ..**.** An exist
291f0 69 6e 67 20 62 2d 74 72 65 65 20 6d 69 67 68 74  ing b-tree might
291f1 20 62 65 20 75 73 65 64 20 69 66 20 74 68 65 20   be used if the 
291f2 52 48 53 20 65 78 70 72 65 73 73 69 6f 6e 20 70  RHS expression p
291f3 58 20 69 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a  X is a simple.**
291f4 20 73 75 62 71 75 65 72 79 20 73 75 63 68 20 61   subquery such a
291f5 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c  s:.**.**     SEL
291f6 45 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f  ECT <column> FRO
291f7 4d 20 3c 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20  M <table>.**.** 
291f8 49 66 20 74 68 65 20 52 48 53 20 6f 66 20 74 68  If the RHS of th
291f9 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 69 73  e IN operator is
291fa 20 61 20 6c 69 73 74 20 6f 72 20 61 20 6d 6f 72   a list or a mor
291fb 65 20 63 6f 6d 70 6c 65 78 20 73 75 62 71 75 65  e complex subque
291fc 72 79 2c 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 65  ry, then.** an e
291fd 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 6d  phemeral table m
291fe 69 67 68 74 20 6e 65 65 64 20 74 6f 20 62 65 20  ight need to be 
291ff 67 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 74  generated from t
29200 68 65 20 52 48 53 20 61 6e 64 20 74 68 65 6e 0a  he RHS and then.
29201 2a 2a 20 70 58 2d 3e 69 54 61 62 6c 65 20 6d 61  ** pX->iTable ma
29202 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  de to point to t
29203 68 65 20 65 70 68 65 72 6d 65 72 61 6c 20 74 61  he ephermeral ta
29204 62 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 61  ble instead of a
29205 6e 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 74 61  n.** existing ta
29206 62 6c 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20  ble.  .**.** If 
29207 74 68 65 20 70 72 4e 6f 74 46 6f 75 6e 64 20 70  the prNotFound p
29208 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20 74  arameter is 0, t
29209 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20 77  hen the b-tree w
2920a 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20 69  ill be used to i
2920b 74 65 72 61 74 65 0a 2a 2a 20 74 68 72 6f 75 67  terate.** throug
2920c 68 20 74 68 65 20 73 65 74 20 6d 65 6d 62 65 72  h the set member
2920d 73 2c 20 73 6b 69 70 70 69 6e 67 20 61 6e 79 20  s, skipping any 
2920e 64 75 70 6c 69 63 61 74 65 73 2e 20 49 6e 20 74  duplicates. In t
2920f 68 69 73 20 63 61 73 65 20 61 6e 0a 2a 2a 20 65  his case an.** e
29210 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d  pheremal table m
29211 75 73 74 20 62 65 20 75 73 65 64 20 75 6e 6c 65  ust be used unle
29212 73 73 20 74 68 65 20 73 65 6c 65 63 74 65 64 20  ss the selected 
29213 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20 67 75 61 72  <column> is guar
29214 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20  anteed.** to be 
29215 75 6e 69 71 75 65 20 2d 20 65 69 74 68 65 72 20  unique - either 
29216 62 65 63 61 75 73 65 20 69 74 20 69 73 20 61 6e  because it is an
29217 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
29218 20 4b 45 59 20 6f 72 20 69 74 0a 2a 2a 20 68 61   KEY or it.** ha
29219 73 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74  s a UNIQUE const
2921a 72 61 69 6e 74 20 6f 72 20 55 4e 49 51 55 45 20  raint or UNIQUE 
2921b 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  index..**.** If 
2921c 74 68 65 20 70 72 4e 6f 74 46 6f 75 6e 64 20 70  the prNotFound p
2921d 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20  arameter is not 
2921e 30 2c 20 74 68 65 6e 20 74 68 65 20 62 2d 74 72  0, then the b-tr
2921f 65 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  ee will be used 
29220 0a 2a 2a 20 66 6f 72 20 66 61 73 74 20 73 65 74  .** for fast set
29221 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 74   membership test
29222 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  s. In this case 
29223 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62  an epheremal tab
29224 6c 65 20 6d 75 73 74 20 0a 2a 2a 20 62 65 20 75  le must .** be u
29225 73 65 64 20 75 6e 6c 65 73 73 20 3c 63 6f 6c 75  sed unless <colu
29226 6d 6e 3e 20 69 73 20 61 6e 20 49 4e 54 45 47 45  mn> is an INTEGE
29227 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72  R PRIMARY KEY or
29228 20 61 6e 20 69 6e 64 65 78 20 63 61 6e 20 0a 2a   an index can .*
29229 2a 20 62 65 20 66 6f 75 6e 64 20 77 69 74 68 20  * be found with 
2922a 3c 63 6f 6c 75 6d 6e 3e 20 61 73 20 69 74 73 20  <column> as its 
2922b 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e  left-most column
2922c 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65  ..**.** When the
2922d 20 62 2d 74 72 65 65 20 69 73 20 62 65 69 6e 67   b-tree is being
2922e 20 75 73 65 64 20 66 6f 72 20 6d 65 6d 62 65 72   used for member
2922f 73 68 69 70 20 74 65 73 74 73 2c 20 74 68 65 20  ship tests, the 
29230 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
29231 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 6b 6e 6f  .** needs to kno
29232 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  w whether or not
29233 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 63   the structure c
29234 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51 4c 20 4e  ontains an SQL N
29235 55 4c 4c 20 0a 2a 2a 20 76 61 6c 75 65 20 69 6e  ULL .** value in
29236 20 6f 72 64 65 72 20 74 6f 20 63 6f 72 72 65 63   order to correc
29237 74 6c 79 20 65 76 61 6c 75 61 74 65 20 65 78 70  tly evaluate exp
29238 72 65 73 73 69 6f 6e 73 20 6c 69 6b 65 20 22 58  ressions like "X
29239 20 49 4e 20 28 59 2c 20 5a 29 22 2e 0a 2a 2a 20   IN (Y, Z)"..** 
2923a 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20  If there is any 
2923b 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20  chance that the 
2923c 28 2e 2e 2e 29 20 6d 69 67 68 74 20 63 6f 6e 74  (...) might cont
2923d 61 69 6e 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65  ain a NULL value
2923e 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2c 20   at.** runtime, 
2923f 74 68 65 6e 20 61 20 72 65 67 69 73 74 65 72 20  then a register 
29240 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  is allocated and
29241 20 74 68 65 20 72 65 67 69 73 74 65 72 20 6e 75   the register nu
29242 6d 62 65 72 20 77 72 69 74 74 65 6e 0a 2a 2a 20  mber written.** 
29243 74 6f 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 2e 20  to *prNotFound. 
29244 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63  If there is no c
29245 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 28  hance that the (
29246 2e 2e 2e 29 20 63 6f 6e 74 61 69 6e 73 20 61 0a  ...) contains a.
29247 2a 2a 20 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 74  ** NULL value, t
29248 68 65 6e 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20  hen *prNotFound 
29249 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65  is left unchange
2924a 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 72 65  d..**.** If a re
2924b 67 69 73 74 65 72 20 69 73 20 61 6c 6c 6f 63 61  gister is alloca
2924c 74 65 64 20 61 6e 64 20 69 74 73 20 6c 6f 63 61  ted and its loca
2924d 74 69 6f 6e 20 73 74 6f 72 65 64 20 69 6e 20 2a  tion stored in *
2924e 70 72 4e 6f 74 46 6f 75 6e 64 2c 20 74 68 65 6e  prNotFound, then
2924f 0a 2a 2a 20 69 74 73 20 69 6e 69 74 69 61 6c 20  .** its initial 
29250 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2e 20 20  value is NULL.  
29251 49 66 20 74 68 65 20 28 2e 2e 2e 29 20 64 6f 65  If the (...) doe
29252 73 20 6e 6f 74 20 72 65 6d 61 69 6e 20 63 6f 6e  s not remain con
29253 73 74 61 6e 74 0a 2a 2a 20 66 6f 72 20 74 68 65  stant.** for the
29254 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68 65   duration of the
29255 20 71 75 65 72 79 20 28 69 2e 65 2e 20 74 68 65   query (i.e. the
29256 20 53 45 4c 45 43 54 20 77 69 74 68 69 6e 20 74   SELECT within t
29257 68 65 20 28 2e 2e 2e 29 0a 2a 2a 20 69 73 20 61  he (...).** is a
29258 20 63 6f 72 72 65 6c 61 74 65 64 20 73 75 62 71   correlated subq
29259 75 65 72 79 29 20 74 68 65 6e 20 74 68 65 20 76  uery) then the v
2925a 61 6c 75 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f  alue of the allo
2925b 63 61 74 65 64 20 72 65 67 69 73 74 65 72 20 69  cated register i
2925c 73 0a 2a 2a 20 72 65 73 65 74 20 74 6f 20 4e 55  s.** reset to NU
2925d 4c 4c 20 65 61 63 68 20 74 69 6d 65 20 74 68 65  LL each time the
2925e 20 73 75 62 71 75 65 72 79 20 69 73 20 72 65 72   subquery is rer
2925f 75 6e 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20  un. This allows 
29260 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 74 6f  the.** caller to
29261 20 75 73 65 20 76 64 62 65 20 63 6f 64 65 20 65   use vdbe code e
29262 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 65  quivalent to the
29263 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
29264 2a 20 20 20 69 66 28 20 72 65 67 69 73 74 65 72  *   if( register
29265 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a 2a 20 20 20 20  ==NULL ){.**    
29266 20 68 61 73 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73   has_null = <tes
29267 74 20 69 66 20 64 61 74 61 20 73 74 72 75 63 74  t if data struct
29268 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c  ure contains nul
29269 6c 3e 0a 2a 2a 20 20 20 20 20 72 65 67 69 73 74  l>.**     regist
2926a 65 72 20 3d 20 31 0a 2a 2a 20 20 20 7d 0a 2a 2a  er = 1.**   }.**
2926b 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  .** in order to 
2926c 61 76 6f 69 64 20 72 75 6e 6e 69 6e 67 20 74 68  avoid running th
2926d 65 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20  e <test if data 
2926e 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
2926f 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 74 65 73 74  ns null>.** test
29270 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e   more often than
29271 20 69 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   is necessary..*
29272 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
29273 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 53  _OMIT_SUBQUERY.S
29274 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
29275 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49  t sqlite3FindInI
29276 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72  ndex(Parse *pPar
29277 73 65 2c 20 45 78 70 72 20 2a 70 58 2c 20 69 6e  se, Expr *pX, in
29278 74 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 29 7b 0a  t *prNotFound){.
29279 20 20 53 65 6c 65 63 74 20 2a 70 3b 20 20 20 20    Select *p;    
2927a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2927b 20 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43          /* SELEC
2927c 54 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 6f  T to the right o
2927d 66 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f  f IN operator */
2927e 0a 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 30  .  int eType = 0
2927f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
29280 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65           /* Type
29281 20 6f 66 20 52 48 53 20 74 61 62 6c 65 2e 20 49   of RHS table. I
29282 4e 5f 49 4e 44 45 58 5f 2a 20 2a 2f 0a 20 20 69  N_INDEX_* */.  i
29283 6e 74 20 69 54 61 62 20 3d 20 70 50 61 72 73 65  nt iTab = pParse
29284 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 20 20 20  ->nTab++;       
29285 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f       /* Cursor o
29286 66 20 74 68 65 20 52 48 53 20 74 61 62 6c 65 20  f the RHS table 
29287 2a 2f 0a 20 20 69 6e 74 20 6d 75 73 74 42 65 55  */.  int mustBeU
29288 6e 69 71 75 65 20 3d 20 28 70 72 4e 6f 74 46 6f  nique = (prNotFo
29289 75 6e 64 3d 3d 30 29 3b 20 20 20 2f 2a 20 54 72  und==0);   /* Tr
2928a 75 65 20 69 66 20 52 48 53 20 6d 75 73 74 20 62  ue if RHS must b
2928b 65 20 75 6e 69 71 75 65 20 2a 2f 0a 20 20 56 64  e unique */.  Vd
2928c 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
2928d 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 20  etVdbe(pParse); 
2928e 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 6d      /* Virtual m
2928f 61 63 68 69 6e 65 20 62 65 69 6e 67 20 63 6f 64  achine being cod
29290 65 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ed */..  assert(
29291 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 29   pX->op==TK_IN )
29292 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  ;..  /* Check to
29293 20 73 65 65 20 69 66 20 61 6e 20 65 78 69 73 74   see if an exist
29294 69 6e 67 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  ing table or ind
29295 65 78 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  ex can be used t
29296 6f 0a 20 20 2a 2a 20 73 61 74 69 73 66 79 20 74  o.  ** satisfy t
29297 68 65 20 71 75 65 72 79 2e 20 20 54 68 69 73 20  he query.  This 
29298 69 73 20 70 72 65 66 65 72 61 62 6c 65 20 74 6f  is preferable to
29299 20 67 65 6e 65 72 61 74 69 6e 67 20 61 20 6e 65   generating a ne
2929a 77 20 0a 20 20 2a 2a 20 65 70 68 65 6d 65 72 61  w .  ** ephemera
2929b 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  l table..  */.  
2929c 70 20 3d 20 28 45 78 70 72 48 61 73 50 72 6f 70  p = (ExprHasProp
2929d 65 72 74 79 28 70 58 2c 20 45 50 5f 78 49 73 53  erty(pX, EP_xIsS
2929e 65 6c 65 63 74 29 20 3f 20 70 58 2d 3e 78 2e 70  elect) ? pX->x.p
2929f 53 65 6c 65 63 74 20 3a 20 30 29 3b 0a 20 20 69  Select : 0);.  i
292a0 66 28 20 41 4c 57 41 59 53 28 70 50 61 72 73 65  f( ALWAYS(pParse
292a1 2d 3e 6e 45 72 72 3d 3d 30 29 20 26 26 20 69 73  ->nErr==0) && is
292a2 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f 70  CandidateForInOp
292a3 74 28 70 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  t(p) ){.    sqli
292a4 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
292a5 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 20 20  ->db;           
292a6 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
292a7 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20  onnection */.   
292a8 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
292a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292aa 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
292ab 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20   <table>. */.   
292ac 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20   Expr *pExpr;   
292ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292ae 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65          /* Expre
292af 73 73 69 6f 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a  ssion <column> *
292b0 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b 20  /.    int iCol; 
292b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292b2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
292b3 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20  Index of column 
292b4 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 20 20 20  <column> */.    
292b5 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
292b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292b7 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
292b8 73 65 20 69 64 78 20 66 6f 72 20 70 54 61 62 20  se idx for pTab 
292b9 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  */..    assert( 
292ba 70 20 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  p );            
292bb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
292bc 65 63 61 75 73 65 20 6f 66 20 69 73 43 61 6e 64  ecause of isCand
292bd 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 70 29  idateForInOpt(p)
292be 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
292bf 70 2d 3e 70 45 4c 69 73 74 21 3d 30 20 29 3b 20  p->pEList!=0 ); 
292c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
292c1 65 63 61 75 73 65 20 6f 66 20 69 73 43 61 6e 64  ecause of isCand
292c2 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 70 29  idateForInOpt(p)
292c3 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
292c4 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e  p->pEList->a[0].
292c5 70 45 78 70 72 21 3d 30 20 29 3b 20 2f 2a 20 42  pExpr!=0 ); /* B
292c6 65 63 61 75 73 65 20 6f 66 20 69 73 43 61 6e 64  ecause of isCand
292c7 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 70 29  idateForInOpt(p)
292c8 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
292c9 70 2d 3e 70 53 72 63 21 3d 30 20 29 3b 20 20 20  p->pSrc!=0 );   
292ca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
292cb 65 63 61 75 73 65 20 6f 66 20 69 73 43 61 6e 64  ecause of isCand
292cc 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 70 29  idateForInOpt(p)
292cd 20 2a 2f 0a 20 20 20 20 70 54 61 62 20 3d 20 70   */.    pTab = p
292ce 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61  ->pSrc->a[0].pTa
292cf 62 3b 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70  b;.    pExpr = p
292d0 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  ->pEList->a[0].p
292d1 45 78 70 72 3b 0a 20 20 20 20 69 43 6f 6c 20 3d  Expr;.    iCol =
292d2 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b   pExpr->iColumn;
292d3 0a 20 20 20 0a 20 20 20 20 2f 2a 20 43 6f 64 65  .   .    /* Code
292d4 20 61 6e 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f   an OP_VerifyCoo
292d5 6b 69 65 20 61 6e 64 20 4f 50 5f 54 61 62 6c 65  kie and OP_Table
292d6 4c 6f 63 6b 20 66 6f 72 20 3c 74 61 62 6c 65 3e  Lock for <table>
292d7 2e 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73  . */.    iDb = s
292d8 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
292d9 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  dex(db, pTab->pS
292da 63 68 65 6d 61 29 3b 0a 20 20 20 20 73 71 6c 69  chema);.    sqli
292db 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
292dc 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29  ema(pParse, iDb)
292dd 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 61 62  ;.    sqlite3Tab
292de 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69  leLock(pParse, i
292df 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  Db, pTab->tnum, 
292e0 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  0, pTab->zName);
292e1 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 66 75  ..    /* This fu
292e2 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63  nction is only c
292e3 61 6c 6c 65 64 20 66 72 6f 6d 20 74 77 6f 20 70  alled from two p
292e4 6c 61 63 65 73 2e 20 49 6e 20 62 6f 74 68 20 63  laces. In both c
292e5 61 73 65 73 20 74 68 65 20 76 64 62 65 0a 20 20  ases the vdbe.  
292e6 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79    ** has already
292e7 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2e   been allocated.
292e8 20 53 6f 20 61 73 73 75 6d 65 20 73 71 6c 69 74   So assume sqlit
292e9 65 33 47 65 74 56 64 62 65 28 29 20 69 73 20 61  e3GetVdbe() is a
292ea 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 73 75 63  lways.    ** suc
292eb 63 65 73 73 66 75 6c 20 68 65 72 65 2e 0a 20 20  cessful here..  
292ec 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
292ed 76 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c  v);.    if( iCol
292ee 3c 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  <0 ){.      int 
292ef 69 41 64 64 72 3b 0a 0a 20 20 20 20 20 20 69 41  iAddr;..      iA
292f0 64 64 72 20 3d 20 73 71 6c 69 74 65 33 43 6f 64  ddr = sqlite3Cod
292f1 65 4f 6e 63 65 28 70 50 61 72 73 65 29 3b 0a 0a  eOnce(pParse);..
292f2 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65        sqlite3Ope
292f3 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69  nTable(pParse, i
292f4 54 61 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20  Tab, iDb, pTab, 
292f5 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20  OP_OpenRead);.  
292f6 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49      eType = IN_I
292f7 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 0a 20 20 20  NDEX_ROWID;..   
292f8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
292f9 6d 70 48 65 72 65 28 76 2c 20 69 41 64 64 72 29  mpHere(v, iAddr)
292fa 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
292fb 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20     Index *pIdx; 
292fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292fd 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61          /* Itera
292fe 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  tor variable */.
292ff 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f  .      /* The co
29300 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
29301 20 75 73 65 64 20 62 79 20 74 68 65 20 63 6f 6d   used by the com
29302 70 61 72 69 73 6f 6e 2e 20 49 66 20 61 6e 20 69  parison. If an i
29303 6e 64 65 78 20 69 73 20 74 6f 0a 20 20 20 20 20  ndex is to.     
29304 20 2a 2a 20 62 65 20 75 73 65 64 20 69 6e 20 70   ** be used in p
29305 6c 61 63 65 20 6f 66 20 61 20 74 65 6d 70 2d 74  lace of a temp-t
29306 61 62 6c 65 2c 20 69 74 20 6d 75 73 74 20 62 65  able, it must be
29307 20 6f 72 64 65 72 65 64 20 61 63 63 6f 72 64 69   ordered accordi
29308 6e 67 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74  ng.      ** to t
29309 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  his collation se
2930a 71 75 65 6e 63 65 2e 20 20 2a 2f 0a 20 20 20 20  quence.  */.    
2930b 20 20 43 6f 6c 6c 53 65 71 20 2a 70 52 65 71 20    CollSeq *pReq 
2930c 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43  = sqlite3BinaryC
2930d 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50  ompareCollSeq(pP
2930e 61 72 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c  arse, pX->pLeft,
2930f 20 70 45 78 70 72 29 3b 0a 0a 20 20 20 20 20 20   pExpr);..      
29310 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
29311 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20  e affinity that 
29312 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20  will be used to 
29313 70 65 72 66 6f 72 6d 20 74 68 65 20 0a 20 20 20  perform the .   
29314 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e     ** comparison
29315 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
29316 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  the affinity of 
29317 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20  the column. If. 
29318 20 20 20 20 20 2a 2a 20 69 74 20 69 73 20 6e 6f       ** it is no
29319 74 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73  t, it is not pos
2931a 73 69 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 79  sible to use any
2931b 20 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 2a 2f   index..      */
2931c 0a 20 20 20 20 20 20 69 6e 74 20 61 66 66 69 6e  .      int affin
2931d 69 74 79 5f 6f 6b 20 3d 20 73 71 6c 69 74 65 33  ity_ok = sqlite3
2931e 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b 28  IndexAffinityOk(
2931f 70 58 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  pX, pTab->aCol[i
29320 43 6f 6c 5d 2e 61 66 66 69 6e 69 74 79 29 3b 0a  Col].affinity);.
29321 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d  .      for(pIdx=
29322 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
29323 64 78 20 26 26 20 65 54 79 70 65 3d 3d 30 20 26  dx && eType==0 &
29324 26 20 61 66 66 69 6e 69 74 79 5f 6f 6b 3b 20 70  & affinity_ok; p
29325 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
29326 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 70  {.        if( (p
29327 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d  Idx->aiColumn[0]
29328 3d 3d 69 43 6f 6c 29 0a 20 20 20 20 20 20 20 20  ==iCol).        
29329 20 26 26 20 73 71 6c 69 74 65 33 46 69 6e 64 43   && sqlite3FindC
2932a 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64  ollSeq(db, ENC(d
2932b 62 29 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c  b), pIdx->azColl
2932c 5b 30 5d 2c 20 30 29 3d 3d 70 52 65 71 0a 20 20  [0], 0)==pReq.  
2932d 20 20 20 20 20 20 20 26 26 20 28 21 6d 75 73 74         && (!must
2932e 42 65 55 6e 69 71 75 65 20 7c 7c 20 28 70 49 64  BeUnique || (pId
2932f 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 26 26  x->nColumn==1 &&
29330 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d   pIdx->onError!=
29331 4f 45 5f 4e 6f 6e 65 29 29 0a 20 20 20 20 20 20  OE_None)).      
29332 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69    ){.          i
29333 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20 20  nt iAddr;.      
29334 20 20 20 20 63 68 61 72 20 2a 70 4b 65 79 3b 0a      char *pKey;.
29335 20 20 0a 20 20 20 20 20 20 20 20 20 20 70 4b 65    .          pKe
29336 79 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  y = (char *)sqli
29337 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
29338 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20  pParse, pIdx);. 
29339 20 20 20 20 20 20 20 20 20 69 41 64 64 72 20 3d           iAddr =
2933a 20 73 71 6c 69 74 65 33 43 6f 64 65 4f 6e 63 65   sqlite3CodeOnce
2933b 28 70 50 61 72 73 65 29 3b 0a 20 20 0a 20 20 20  (pParse);.  .   
2933c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2933d 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
2933e 70 65 6e 52 65 61 64 2c 20 69 54 61 62 2c 20 70  penRead, iTab, p
2933f 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a  Idx->tnum, iDb,.
29340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29341 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
29342 4b 65 79 2c 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  Key,P4_KEYINFO_H
29343 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 20  ANDOFF);.       
29344 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
29345 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d 3e 7a  v, "%s", pIdx->z
29346 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 20 20  Name));.        
29347 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44    eType = IN_IND
29348 45 58 5f 49 4e 44 45 58 3b 0a 0a 20 20 20 20 20  EX_INDEX;..     
29349 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2934a 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41 64 64  JumpHere(v, iAdd
2934b 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  r);.          if
2934c 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 26 26 20  ( prNotFound && 
2934d 21 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c  !pTab->aCol[iCol
2934e 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20 20  ].notNull ){.   
2934f 20 20 20 20 20 20 20 20 20 2a 70 72 4e 6f 74 46           *prNotF
29350 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d  ound = ++pParse-
29351 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20  >nMem;.         
29352 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29353 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
29354 20 30 2c 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 29   0, *prNotFound)
29355 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
29356 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
29357 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
29358 20 65 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20   eType==0 ){.   
29359 20 2f 2a 20 43 6f 75 6c 64 20 6e 6f 74 20 66 6f   /* Could not fo
2935a 75 6e 64 20 61 6e 20 65 78 69 73 74 69 6e 67 20  und an existing 
2935b 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74  table or index t
2935c 6f 20 75 73 65 20 61 73 20 74 68 65 20 52 48 53  o use as the RHS
2935d 20 62 2d 74 72 65 65 2e 0a 20 20 20 20 2a 2a 20   b-tree..    ** 
2935e 57 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 20  We will have to 
2935f 67 65 6e 65 72 61 74 65 20 61 6e 20 65 70 68 65  generate an ephe
29360 6d 65 72 61 6c 20 74 61 62 6c 65 20 74 6f 20 64  meral table to d
29361 6f 20 74 68 65 20 6a 6f 62 2e 0a 20 20 20 20 2a  o the job..    *
29362 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 73 61 76  /.    double sav
29363 65 64 4e 51 75 65 72 79 4c 6f 6f 70 20 3d 20 70  edNQueryLoop = p
29364 50 61 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f 6f  Parse->nQueryLoo
29365 70 3b 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 48  p;.    int rMayH
29366 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20  aveNull = 0;.   
29367 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45   eType = IN_INDE
29368 58 5f 45 50 48 3b 0a 20 20 20 20 69 66 28 20 70  X_EPH;.    if( p
29369 72 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20  rNotFound ){.   
2936a 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d     *prNotFound =
2936b 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20   rMayHaveNull = 
2936c 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2936d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2936e 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
2936f 6c 6c 2c 20 30 2c 20 2a 70 72 4e 6f 74 46 6f 75  ll, 0, *prNotFou
29370 6e 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  nd);.    }else{.
29371 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
29372 70 50 61 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f  pParse->nQueryLo
29373 6f 70 3e 28 64 6f 75 62 6c 65 29 31 20 29 3b 0a  op>(double)1 );.
29374 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 51        pParse->nQ
29375 75 65 72 79 4c 6f 6f 70 20 3d 20 28 64 6f 75 62  ueryLoop = (doub
29376 6c 65 29 31 3b 0a 20 20 20 20 20 20 69 66 28 20  le)1;.      if( 
29377 70 58 2d 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c 75  pX->pLeft->iColu
29378 6d 6e 3c 30 20 26 26 20 21 45 78 70 72 48 61 73  mn<0 && !ExprHas
29379 41 6e 79 50 72 6f 70 65 72 74 79 28 70 58 2c 20  AnyProperty(pX, 
2937a 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
2937b 0a 20 20 20 20 20 20 20 20 65 54 79 70 65 20 3d  .        eType =
2937c 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 3b   IN_INDEX_ROWID;
2937d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2937e 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75     sqlite3CodeSu
2937f 62 73 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  bselect(pParse, 
29380 70 58 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c  pX, rMayHaveNull
29381 2c 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45  , eType==IN_INDE
29382 58 5f 52 4f 57 49 44 29 3b 0a 20 20 20 20 70 50  X_ROWID);.    pP
29383 61 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f 6f 70  arse->nQueryLoop
29384 20 3d 20 73 61 76 65 64 4e 51 75 65 72 79 4c 6f   = savedNQueryLo
29385 6f 70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  op;.  }else{.   
29386 20 70 58 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54   pX->iTable = iT
29387 61 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ab;.  }.  return
29388 20 65 54 79 70 65 3b 0a 7d 0a 23 65 6e 64 69 66   eType;.}.#endif
29389 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2938a 20 63 6f 64 65 20 66 6f 72 20 73 63 61 6c 61 72   code for scalar
2938b 20 73 75 62 71 75 65 72 69 65 73 20 75 73 65 64   subqueries used
2938c 20 61 73 20 61 20 73 75 62 71 75 65 72 79 20 65   as a subquery e
2938d 78 70 72 65 73 73 69 6f 6e 2c 20 45 58 49 53 54  xpression, EXIST
2938e 53 2c 0a 2a 2a 20 6f 72 20 49 4e 20 6f 70 65 72  S,.** or IN oper
2938f 61 74 6f 72 73 2e 20 20 45 78 61 6d 70 6c 65 73  ators.  Examples
29390 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 53 45 4c  :.**.**     (SEL
29391 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20  ECT a FROM b)   
29392 20 20 20 20 20 20 20 2d 2d 20 73 75 62 71 75 65         -- subque
29393 72 79 0a 2a 2a 20 20 20 20 20 45 58 49 53 54 53  ry.**     EXISTS
29394 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20   (SELECT a FROM 
29395 62 29 20 20 20 2d 2d 20 45 58 49 53 54 53 20 73  b)   -- EXISTS s
29396 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 20 20 78  ubquery.**     x
29397 20 49 4e 20 28 34 2c 35 2c 31 31 29 20 20 20 20   IN (4,5,11)    
29398 20 20 20 20 20 20 20 20 20 20 2d 2d 20 49 4e 20            -- IN 
29399 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 6c 69  operator with li
2939a 73 74 20 6f 6e 20 72 69 67 68 74 2d 68 61 6e 64  st on right-hand
2939b 20 73 69 64 65 0a 2a 2a 20 20 20 20 20 78 20 49   side.**     x I
2939c 4e 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f 4d  N (SELECT a FROM
2939d 20 62 29 20 20 20 20 20 2d 2d 20 49 4e 20 6f 70   b)     -- IN op
2939e 65 72 61 74 6f 72 20 77 69 74 68 20 73 75 62 71  erator with subq
2939f 75 65 72 79 20 6f 6e 20 74 68 65 20 72 69 67 68  uery on the righ
293a0 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 45 78 70  t.**.** The pExp
293a1 72 20 70 61 72 61 6d 65 74 65 72 20 64 65 73 63  r parameter desc
293a2 72 69 62 65 73 20 74 68 65 20 65 78 70 72 65 73  ribes the expres
293a3 73 69 6f 6e 20 74 68 61 74 20 63 6f 6e 74 61 69  sion that contai
293a4 6e 73 20 74 68 65 20 49 4e 0a 2a 2a 20 6f 70 65  ns the IN.** ope
293a5 72 61 74 6f 72 20 6f 72 20 73 75 62 71 75 65 72  rator or subquer
293a6 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61  y..**.** If para
293a7 6d 65 74 65 72 20 69 73 52 6f 77 69 64 20 69 73  meter isRowid is
293a8 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
293a9 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72  expression pExpr
293aa 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a 2a   is guaranteed.*
293ab 2a 20 74 6f 20 62 65 20 6f 66 20 74 68 65 20 66  * to be of the f
293ac 6f 72 6d 20 22 3c 72 6f 77 69 64 3e 20 49 4e 20  orm "<rowid> IN 
293ad 28 3f 2c 20 3f 2c 20 3f 29 22 2c 20 77 68 65 72  (?, ?, ?)", wher
293ae 65 20 3c 72 6f 77 69 64 3e 20 69 73 20 61 20 72  e <rowid> is a r
293af 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 6f 20 73  eference.** to s
293b0 6f 6d 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20  ome integer key 
293b1 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 74 61 62 6c  column of a tabl
293b2 65 20 42 2d 54 72 65 65 2e 20 49 6e 20 74 68 69  e B-Tree. In thi
293b3 73 20 63 61 73 65 2c 20 75 73 65 20 61 6e 0a 2a  s case, use an.*
293b4 2a 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 20  * intkey B-Tree 
293b5 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65 74  to store the set
293b6 20 6f 66 20 49 4e 28 2e 2e 2e 29 20 76 61 6c 75   of IN(...) valu
293b7 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68  es instead of th
293b8 65 20 75 73 75 61 6c 0a 2a 2a 20 28 73 6c 6f 77  e usual.** (slow
293b9 65 72 29 20 76 61 72 69 61 62 6c 65 20 6c 65 6e  er) variable len
293ba 67 74 68 20 6b 65 79 73 20 42 2d 54 72 65 65 2e  gth keys B-Tree.
293bb 0a 2a 2a 0a 2a 2a 20 49 66 20 72 4d 61 79 48 61  .**.** If rMayHa
293bc 76 65 4e 75 6c 6c 20 69 73 20 6e 6f 6e 2d 7a 65  veNull is non-ze
293bd 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74  ro, that means t
293be 68 61 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f  hat the operatio
293bf 6e 20 69 73 20 61 6e 20 49 4e 0a 2a 2a 20 28 6e  n is an IN.** (n
293c0 6f 74 20 61 20 53 45 4c 45 43 54 20 6f 72 20 45  ot a SELECT or E
293c1 58 49 53 54 53 29 20 61 6e 64 20 74 68 61 74 20  XISTS) and that 
293c2 74 68 65 20 52 48 53 20 6d 69 67 68 74 20 63 6f  the RHS might co
293c3 6e 74 61 69 6e 73 20 4e 55 4c 4c 73 2e 0a 2a 2a  ntains NULLs..**
293c4 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 74 68   Furthermore, th
293c5 65 20 49 4e 20 69 73 20 69 6e 20 61 20 57 48 45  e IN is in a WHE
293c6 52 45 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68  RE clause and th
293c7 61 74 20 77 65 20 72 65 61 6c 6c 79 20 77 61 6e  at we really wan
293c8 74 0a 2a 2a 20 74 6f 20 69 74 65 72 61 74 65 20  t.** to iterate 
293c9 6f 76 65 72 20 74 68 65 20 52 48 53 20 6f 66 20  over the RHS of 
293ca 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20  the IN operator 
293cb 69 6e 20 6f 72 64 65 72 20 74 6f 20 71 75 69 63  in order to quic
293cc 6b 6c 79 20 6c 6f 63 61 74 65 0a 2a 2a 20 61 6c  kly locate.** al
293cd 6c 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  l corresponding 
293ce 4c 48 53 20 65 6c 65 6d 65 6e 74 73 2e 20 20 41  LHS elements.  A
293cf 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ll this routine 
293d0 64 6f 65 73 20 69 73 20 69 6e 69 74 69 61 6c 69  does is initiali
293d1 7a 65 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74  ze.** the regist
293d2 65 72 20 67 69 76 65 6e 20 62 79 20 72 4d 61 79  er given by rMay
293d3 48 61 76 65 4e 75 6c 6c 20 74 6f 20 4e 55 4c 4c  HaveNull to NULL
293d4 2e 20 20 43 61 6c 6c 69 6e 67 20 72 6f 75 74 69  .  Calling routi
293d5 6e 65 73 20 77 69 6c 6c 20 74 61 6b 65 0a 2a 2a  nes will take.**
293d6 20 63 61 72 65 20 6f 66 20 63 68 61 6e 67 69 6e   care of changin
293d7 67 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20  g this register 
293d8 76 61 6c 75 65 20 74 6f 20 6e 6f 6e 2d 4e 55 4c  value to non-NUL
293d9 4c 20 69 66 20 74 68 65 20 52 48 53 20 69 73 20  L if the RHS is 
293da 4e 55 4c 4c 2d 66 72 65 65 2e 0a 2a 2a 0a 2a 2a  NULL-free..**.**
293db 20 49 66 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c   If rMayHaveNull
293dc 20 69 73 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d   is zero, that m
293dd 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 73 75  eans that the su
293de 62 71 75 65 72 79 20 69 73 20 62 65 69 6e 67 20  bquery is being 
293df 75 73 65 64 0a 2a 2a 20 66 6f 72 20 6d 65 6d 62  used.** for memb
293e0 65 72 73 68 69 70 20 74 65 73 74 69 6e 67 20 6f  ership testing o
293e1 6e 6c 79 2e 20 20 54 68 65 72 65 20 69 73 20 6e  nly.  There is n
293e2 6f 20 6e 65 65 64 20 74 6f 20 69 6e 69 74 69 61  o need to initia
293e3 6c 69 7a 65 20 61 6e 79 0a 2a 2a 20 72 65 67 69  lize any.** regi
293e4 73 74 65 72 73 20 74 6f 20 69 6e 64 69 63 61 74  sters to indicat
293e5 65 20 74 68 65 20 70 72 65 73 65 6e 73 65 20 6f  e the presense o
293e6 72 20 61 62 73 65 6e 63 65 20 6f 66 20 4e 55 4c  r absence of NUL
293e7 4c 73 20 6f 6e 20 74 68 65 20 52 48 53 2e 0a 2a  Ls on the RHS..*
293e8 2a 0a 2a 2a 20 46 6f 72 20 61 20 53 45 4c 45 43  *.** For a SELEC
293e9 54 20 6f 72 20 45 58 49 53 54 53 20 6f 70 65 72  T or EXISTS oper
293ea 61 74 6f 72 2c 20 72 65 74 75 72 6e 20 74 68 65  ator, return the
293eb 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 68   register that h
293ec 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 72 65 73 75  olds the.** resu
293ed 6c 74 2e 20 20 46 6f 72 20 49 4e 20 6f 70 65 72  lt.  For IN oper
293ee 61 74 6f 72 73 20 6f 72 20 69 66 20 61 6e 20 65  ators or if an e
293ef 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65  rror occurs, the
293f0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
293f1 20 30 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53   0..*/.#ifndef S
293f2 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
293f3 45 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ERY.SQLITE_PRIVA
293f4 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f  TE int sqlite3Co
293f5 64 65 53 75 62 73 65 6c 65 63 74 28 0a 20 20 50  deSubselect(.  P
293f6 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
293f7 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
293f8 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
293f9 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20  xpr *pExpr,     
293fa 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 49 4e         /* The IN
293fb 2c 20 53 45 4c 45 43 54 2c 20 6f 72 20 45 58 49  , SELECT, or EXI
293fc 53 54 53 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a  STS operator */.
293fd 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75    int rMayHaveNu
293fe 6c 6c 2c 20 20 20 20 20 20 20 2f 2a 20 52 65 67  ll,       /* Reg
293ff 69 73 74 65 72 20 74 68 61 74 20 72 65 63 6f 72  ister that recor
29400 64 73 20 77 68 65 74 68 65 72 20 4e 55 4c 4c 73  ds whether NULLs
29401 20 65 78 69 73 74 20 69 6e 20 52 48 53 20 2a 2f   exist in RHS */
29402 0a 20 20 69 6e 74 20 69 73 52 6f 77 69 64 20 20  .  int isRowid  
29403 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
29404 20 74 72 75 65 2c 20 4c 48 53 20 6f 66 20 49 4e   true, LHS of IN
29405 20 6f 70 65 72 61 74 6f 72 20 69 73 20 61 20 72   operator is a r
29406 6f 77 69 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  owid */.){.  int
29407 20 74 65 73 74 41 64 64 72 20 3d 20 2d 31 3b 20   testAddr = -1; 
29408 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29409 20 20 20 20 20 2f 2a 20 4f 6e 65 2d 74 69 6d 65       /* One-time
2940a 20 74 65 73 74 20 61 64 64 72 65 73 73 20 2a 2f   test address */
2940b 0a 20 20 69 6e 74 20 72 52 65 67 20 3d 20 30 3b  .  int rReg = 0;
2940c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2940d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
2940e 67 69 73 74 65 72 20 73 74 6f 72 69 6e 67 20 72  gister storing r
2940f 65 73 75 6c 74 69 6e 67 20 2a 2f 0a 20 20 56 64  esulting */.  Vd
29410 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
29411 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
29412 20 20 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30    if( NEVER(v==0
29413 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
29414 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
29415 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 0a 20  Push(pParse);.. 
29416 20 2f 2a 20 54 68 69 73 20 63 6f 64 65 20 6d 75   /* This code mu
29417 73 74 20 62 65 20 72 75 6e 20 69 6e 20 69 74 73  st be run in its
29418 20 65 6e 74 69 72 65 74 79 20 65 76 65 72 79 20   entirety every 
29419 74 69 6d 65 20 69 74 20 69 73 20 65 6e 63 6f 75  time it is encou
2941a 6e 74 65 72 65 64 0a 20 20 2a 2a 20 69 66 20 61  ntered.  ** if a
2941b 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ny of the follow
2941c 69 6e 67 20 69 73 20 74 72 75 65 3a 0a 20 20 2a  ing is true:.  *
2941d 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65  *.  **    *  The
2941e 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65   right-hand side
2941f 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64   is a correlated
29420 20 73 75 62 71 75 65 72 79 0a 20 20 2a 2a 20 20   subquery.  **  
29421 20 20 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68    *  The right-h
29422 61 6e 64 20 73 69 64 65 20 69 73 20 61 6e 20 65  and side is an e
29423 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 63  xpression list c
29424 6f 6e 74 61 69 6e 69 6e 67 20 76 61 72 69 61 62  ontaining variab
29425 6c 65 73 0a 20 20 2a 2a 20 20 20 20 2a 20 20 57  les.  **    *  W
29426 65 20 61 72 65 20 69 6e 73 69 64 65 20 61 20 74  e are inside a t
29427 72 69 67 67 65 72 0a 20 20 2a 2a 0a 20 20 2a 2a  rigger.  **.  **
29428 20 49 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 61   If all of the a
29429 62 6f 76 65 20 61 72 65 20 66 61 6c 73 65 2c 20  bove are false, 
2942a 74 68 65 6e 20 77 65 20 63 61 6e 20 72 75 6e 20  then we can run 
2942b 74 68 69 73 20 63 6f 64 65 20 6a 75 73 74 20 6f  this code just o
2942c 6e 63 65 0a 20 20 2a 2a 20 73 61 76 65 20 74 68  nce.  ** save th
2942d 65 20 72 65 73 75 6c 74 73 2c 20 61 6e 64 20 72  e results, and r
2942e 65 75 73 65 20 74 68 65 20 73 61 6d 65 20 72 65  euse the same re
2942f 73 75 6c 74 20 6f 6e 20 73 75 62 73 65 71 75 65  sult on subseque
29430 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a  nt invocations..
29431 20 20 2a 2f 0a 20 20 69 66 28 20 21 45 78 70 72    */.  if( !Expr
29432 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
29433 45 78 70 72 2c 20 45 50 5f 56 61 72 53 65 6c 65  Expr, EP_VarSele
29434 63 74 29 20 29 7b 0a 20 20 20 20 74 65 73 74 41  ct) ){.    testA
29435 64 64 72 20 3d 20 73 71 6c 69 74 65 33 43 6f 64  ddr = sqlite3Cod
29436 65 4f 6e 63 65 28 70 50 61 72 73 65 29 3b 0a 20  eOnce(pParse);. 
29437 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
29438 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a  TE_OMIT_EXPLAIN.
29439 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78    if( pParse->ex
2943a 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a 20 20 20 20  plain==2 ){.    
2943b 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 73 71 6c  char *zMsg = sql
2943c 69 74 65 33 4d 50 72 69 6e 74 66 28 0a 20 20 20  ite3MPrintf(.   
2943d 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2c       pParse->db,
2943e 20 22 45 58 45 43 55 54 45 20 25 73 25 73 20 53   "EXECUTE %s%s S
2943f 55 42 51 55 45 52 59 20 25 64 22 2c 20 74 65 73  UBQUERY %d", tes
29440 74 41 64 64 72 3e 3d 30 3f 22 22 3a 22 43 4f 52  tAddr>=0?"":"COR
29441 52 45 4c 41 54 45 44 20 22 2c 0a 20 20 20 20 20  RELATED ",.     
29442 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b     pExpr->op==TK
29443 5f 49 4e 3f 22 4c 49 53 54 22 3a 22 53 43 41 4c  _IN?"LIST":"SCAL
29444 41 52 22 2c 20 70 50 61 72 73 65 2d 3e 69 4e 65  AR", pParse->iNe
29445 78 74 53 65 6c 65 63 74 49 64 0a 20 20 20 20 29  xtSelectId.    )
29446 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
29447 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 45 78  eAddOp4(v, OP_Ex
29448 70 6c 61 69 6e 2c 20 70 50 61 72 73 65 2d 3e 69  plain, pParse->i
29449 53 65 6c 65 63 74 49 64 2c 20 30 2c 20 30 2c 20  SelectId, 0, 0, 
2944a 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41 4d 49 43  zMsg, P4_DYNAMIC
2944b 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
2944c 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e   switch( pExpr->
2944d 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
2944e 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 63 68  K_IN: {.      ch
2944f 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20  ar affinity;    
29450 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 66 66            /* Aff
29451 69 6e 69 74 79 20 6f 66 20 74 68 65 20 4c 48 53  inity of the LHS
29452 20 6f 66 20 74 68 65 20 49 4e 20 2a 2f 0a 20 20   of the IN */.  
29453 20 20 20 20 4b 65 79 49 6e 66 6f 20 6b 65 79 49      KeyInfo keyI
29454 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  nfo;            
29455 2f 2a 20 4b 65 79 69 6e 66 6f 20 66 6f 72 20 74  /* Keyinfo for t
29456 68 65 20 67 65 6e 65 72 61 74 65 64 20 74 61 62  he generated tab
29457 6c 65 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74  le */.      stat
29458 69 63 20 75 38 20 73 6f 72 74 4f 72 64 65 72 20  ic u8 sortOrder 
29459 3d 20 30 3b 20 20 20 20 2f 2a 20 46 61 6b 65 20  = 0;    /* Fake 
2945a 61 53 6f 72 74 4f 72 64 65 72 20 66 6f 72 20 6b  aSortOrder for k
2945b 65 79 49 6e 66 6f 20 2a 2f 0a 20 20 20 20 20 20  eyInfo */.      
2945c 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20 20  int addr;       
2945d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2945e 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65  ddress of OP_Ope
2945f 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72  nEphemeral instr
29460 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  uction */.      
29461 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45  Expr *pLeft = pE
29462 78 70 72 2d 3e 70 4c 65 66 74 3b 20 2f 2a 20 74  xpr->pLeft; /* t
29463 68 65 20 4c 48 53 20 6f 66 20 74 68 65 20 49 4e  he LHS of the IN
29464 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 0a 20 20   operator */..  
29465 20 20 20 20 69 66 28 20 72 4d 61 79 48 61 76 65      if( rMayHave
29466 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  Null ){.        
29467 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29468 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
29469 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a   rMayHaveNull);.
2946a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61        }..      a
2946b 66 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65  ffinity = sqlite
2946c 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 4c  3ExprAffinity(pL
2946d 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  eft);..      /* 
2946e 57 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20  Whether this is 
2946f 61 6e 20 27 78 20 49 4e 28 53 45 4c 45 43 54 2e  an 'x IN(SELECT.
29470 2e 2e 29 27 20 6f 72 20 61 6e 20 27 78 20 49 4e  ..)' or an 'x IN
29471 28 3c 65 78 70 72 6c 69 73 74 3e 29 27 0a 20 20  (<exprlist>)'.  
29472 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f      ** expressio
29473 6e 20 69 74 20 69 73 20 68 61 6e 64 6c 65 64 20  n it is handled 
29474 74 68 65 20 73 61 6d 65 20 77 61 79 2e 20 20 41  the same way.  A
29475 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  n ephemeral tabl
29476 65 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66  e is .      ** f
29477 69 6c 6c 65 64 20 77 69 74 68 20 73 69 6e 67 6c  illed with singl
29478 65 2d 66 69 65 6c 64 20 69 6e 64 65 78 20 6b 65  e-field index ke
29479 79 73 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20  ys representing 
2947a 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20 20 20  the results.    
2947b 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 45    ** from the SE
2947c 4c 45 43 54 20 6f 72 20 74 68 65 20 3c 65 78 70  LECT or the <exp
2947d 72 6c 69 73 74 3e 2e 0a 20 20 20 20 20 20 2a 2a  rlist>..      **
2947e 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65  .      ** If the
2947f 20 27 78 27 20 65 78 70 72 65 73 73 69 6f 6e 20   'x' expression 
29480 69 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75  is a column valu
29481 65 2c 20 6f 72 20 74 68 65 20 53 45 4c 45 43 54  e, or the SELECT
29482 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 73 74 61  ....      ** sta
29483 74 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61  tement returns a
29484 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74   column value, t
29485 68 65 6e 20 74 68 65 20 61 66 66 69 6e 69 74 79  hen the affinity
29486 20 6f 66 20 74 68 61 74 0a 20 20 20 20 20 20 2a   of that.      *
29487 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64  * column is used
29488 20 74 6f 20 62 75 69 6c 64 20 74 68 65 20 69 6e   to build the in
29489 64 65 78 20 6b 65 79 73 2e 20 49 66 20 62 6f 74  dex keys. If bot
2948a 68 20 27 78 27 20 61 6e 64 20 74 68 65 0a 20 20  h 'x' and the.  
2948b 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 2e 2e 2e      ** SELECT...
2948c 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63   statement are c
2948d 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d  olumns, then num
2948e 65 72 69 63 20 61 66 66 69 6e 69 74 79 20 69 73  eric affinity is
2948f 20 75 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 69   used.      ** i
29490 66 20 65 69 74 68 65 72 20 63 6f 6c 75 6d 6e 20  f either column 
29491 68 61 73 20 4e 55 4d 45 52 49 43 20 6f 72 20 49  has NUMERIC or I
29492 4e 54 45 47 45 52 20 61 66 66 69 6e 69 74 79 2e  NTEGER affinity.
29493 20 49 66 20 6e 65 69 74 68 65 72 0a 20 20 20 20   If neither.    
29494 20 20 2a 2a 20 27 78 27 20 6e 6f 72 20 74 68 65    ** 'x' nor the
29495 20 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65   SELECT... state
29496 6d 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73  ment are columns
29497 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61  , then numeric a
29498 66 66 69 6e 69 74 79 0a 20 20 20 20 20 20 2a 2a  ffinity.      **
29499 20 69 73 20 75 73 65 64 2e 0a 20 20 20 20 20 20   is used..      
2949a 2a 2f 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  */.      pExpr->
2949b 69 54 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d  iTable = pParse-
2949c 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 61  >nTab++;.      a
2949d 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
2949e 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70  eAddOp2(v, OP_Op
2949f 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 45 78  enEphemeral, pEx
294a0 70 72 2d 3e 69 54 61 62 6c 65 2c 20 21 69 73 52  pr->iTable, !isR
294a1 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28  owid);.      if(
294a2 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 3d 3d 30   rMayHaveNull==0
294a3 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 43 68   ) sqlite3VdbeCh
294a4 61 6e 67 65 50 35 28 76 2c 20 42 54 52 45 45 5f  angeP5(v, BTREE_
294a5 55 4e 4f 52 44 45 52 45 44 29 3b 0a 20 20 20 20  UNORDERED);.    
294a6 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 49 6e 66    memset(&keyInf
294a7 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65 79  o, 0, sizeof(key
294a8 49 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20 6b 65  Info));.      ke
294a9 79 49 6e 66 6f 2e 6e 46 69 65 6c 64 20 3d 20 31  yInfo.nField = 1
294aa 3b 0a 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e  ;.      keyInfo.
294ab 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 26 73 6f  aSortOrder = &so
294ac 72 74 4f 72 64 65 72 3b 0a 0a 20 20 20 20 20 20  rtOrder;..      
294ad 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
294ae 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
294af 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20  sSelect) ){.    
294b0 20 20 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20      /* Case 1:  
294b1 20 20 20 65 78 70 72 20 49 4e 20 28 53 45 4c 45     expr IN (SELE
294b2 43 54 20 2e 2e 2e 29 0a 20 20 20 20 20 20 20 20  CT ...).        
294b3 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 47 65  **.        ** Ge
294b4 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 77  nerate code to w
294b5 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 73  rite the results
294b6 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 20 69   of the select i
294b7 6e 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72  nto the temporar
294b8 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62  y.        ** tab
294b9 6c 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  le allocated and
294ba 20 6f 70 65 6e 65 64 20 61 62 6f 76 65 2e 0a 20   opened above.. 
294bb 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
294bc 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73    SelectDest des
294bd 74 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c  t;.        ExprL
294be 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 0a 20 20  ist *pEList;..  
294bf 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 69        assert( !i
294c0 73 52 6f 77 69 64 20 29 3b 0a 20 20 20 20 20 20  sRowid );.      
294c1 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
294c2 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53  estInit(&dest, S
294c3 52 54 5f 53 65 74 2c 20 70 45 78 70 72 2d 3e 69  RT_Set, pExpr->i
294c4 54 61 62 6c 65 29 3b 0a 20 20 20 20 20 20 20 20  Table);.        
294c5 64 65 73 74 2e 61 66 66 53 64 73 74 20 3d 20 28  dest.affSdst = (
294c6 75 38 29 61 66 66 69 6e 69 74 79 3b 0a 20 20 20  u8)affinity;.   
294c7 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 45       assert( (pE
294c8 78 70 72 2d 3e 69 54 61 62 6c 65 26 30 78 30 30  xpr->iTable&0x00
294c9 30 30 46 46 46 46 29 3d 3d 70 45 78 70 72 2d 3e  00FFFF)==pExpr->
294ca 69 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20  iTable );.      
294cb 20 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65    pExpr->x.pSele
294cc 63 74 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a  ct->iLimit = 0;.
294cd 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
294ce 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
294cf 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65  , pExpr->x.pSele
294d0 63 74 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20  ct, &dest) ){.  
294d1 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
294d2 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
294d3 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78      pEList = pEx
294d4 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70  pr->x.pSelect->p
294d5 45 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 69  EList;.        i
294d6 66 28 20 41 4c 57 41 59 53 28 70 45 4c 69 73 74  f( ALWAYS(pEList
294d7 21 3d 30 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e  !=0 && pEList->n
294d8 45 78 70 72 3e 30 29 20 29 7b 20 0a 20 20 20 20  Expr>0) ){ .    
294d9 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43        keyInfo.aC
294da 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33  oll[0] = sqlite3
294db 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c  BinaryCompareCol
294dc 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78  lSeq(pParse, pEx
294dd 70 72 2d 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20  pr->pLeft,.     
294de 20 20 20 20 20 20 20 20 20 70 45 4c 69 73 74 2d           pEList-
294df 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20  >a[0].pExpr);.  
294e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
294e1 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53 28 70  lse if( ALWAYS(p
294e2 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 21 3d 30  Expr->x.pList!=0
294e3 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
294e4 43 61 73 65 20 32 3a 20 20 20 20 20 65 78 70 72  Case 2:     expr
294e5 20 49 4e 20 28 65 78 70 72 6c 69 73 74 29 0a 20   IN (exprlist). 
294e6 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
294e7 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20 65 78    ** For each ex
294e8 70 72 65 73 73 69 6f 6e 2c 20 62 75 69 6c 64 20  pression, build 
294e9 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 66 72 6f  an index key fro
294ea 6d 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e  m the evaluation
294eb 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20   and.        ** 
294ec 73 74 6f 72 65 20 69 74 20 69 6e 20 74 68 65 20  store it in the 
294ed 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e  temporary table.
294ee 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 61 20   If <expr> is a 
294ef 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 75 73 65  column, then use
294f0 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74  .        ** that
294f1 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74   columns affinit
294f2 79 20 77 68 65 6e 20 62 75 69 6c 64 69 6e 67 20  y when building 
294f3 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 66 20 3c  index keys. If <
294f4 65 78 70 72 3e 20 69 73 20 6e 6f 74 0a 20 20 20  expr> is not.   
294f5 20 20 20 20 20 2a 2a 20 61 20 63 6f 6c 75 6d 6e       ** a column
294f6 2c 20 75 73 65 20 6e 75 6d 65 72 69 63 20 61 66  , use numeric af
294f7 66 69 6e 69 74 79 2e 0a 20 20 20 20 20 20 20 20  finity..        
294f8 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  */.        int i
294f9 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c 69  ;.        ExprLi
294fa 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70  st *pList = pExp
294fb 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20  r->x.pList;.    
294fc 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
294fd 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
294fe 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 2c  .        int r1,
294ff 20 72 32 2c 20 72 33 3b 0a 0a 20 20 20 20 20 20   r2, r3;..      
29500 20 20 69 66 28 20 21 61 66 66 69 6e 69 74 79 20    if( !affinity 
29501 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 66 66  ){.          aff
29502 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41  inity = SQLITE_A
29503 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20  FF_NONE;.       
29504 20 7d 0a 20 20 20 20 20 20 20 20 6b 65 79 49 6e   }.        keyIn
29505 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71  fo.aColl[0] = sq
29506 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
29507 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
29508 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 20 20  pLeft);.        
29509 6b 65 79 49 6e 66 6f 2e 61 53 6f 72 74 4f 72 64  keyInfo.aSortOrd
2950a 65 72 20 3d 20 26 73 6f 72 74 4f 72 64 65 72 3b  er = &sortOrder;
2950b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f  ..        /* Loo
2950c 70 20 74 68 72 6f 75 67 68 20 65 61 63 68 20 65  p through each e
2950d 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 3c 65 78  xpression in <ex
2950e 70 72 6c 69 73 74 3e 2e 20 2a 2f 0a 20 20 20 20  prlist>. */.    
2950f 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
29510 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
29511 65 29 3b 0a 20 20 20 20 20 20 20 20 72 32 20 3d  e);.        r2 =
29512 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
29513 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
29514 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29515 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
29516 2c 20 30 2c 20 72 32 29 3b 0a 20 20 20 20 20 20  , 0, r2);.      
29517 20 20 66 6f 72 28 69 3d 70 4c 69 73 74 2d 3e 6e    for(i=pList->n
29518 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 4c 69 73  Expr, pItem=pLis
29519 74 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20  t->a; i>0; i--, 
2951a 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
2951b 20 20 20 20 45 78 70 72 20 2a 70 45 32 20 3d 20      Expr *pE2 = 
2951c 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pItem->pExpr;.  
2951d 20 20 20 20 20 20 20 20 69 6e 74 20 69 56 61 6c          int iVal
2951e 54 6f 49 6e 73 3b 0a 0a 20 20 20 20 20 20 20 20  ToIns;..        
2951f 20 20 2f 2a 20 49 66 20 74 68 65 20 65 78 70 72    /* If the expr
29520 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f  ession is not co
29521 6e 73 74 61 6e 74 20 74 68 65 6e 20 77 65 20 77  nstant then we w
29522 69 6c 6c 20 6e 65 65 64 20 74 6f 0a 20 20 20 20  ill need to.    
29523 20 20 20 20 20 20 2a 2a 20 64 69 73 61 62 6c 65        ** disable
29524 20 74 68 65 20 74 65 73 74 20 74 68 61 74 20 77   the test that w
29525 61 73 20 67 65 6e 65 72 61 74 65 64 20 61 62 6f  as generated abo
29526 76 65 20 74 68 61 74 20 6d 61 6b 65 73 20 73 75  ve that makes su
29527 72 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  re.          ** 
29528 74 68 69 73 20 63 6f 64 65 20 6f 6e 6c 79 20 65  this code only e
29529 78 65 63 75 74 65 73 20 6f 6e 63 65 2e 20 20 42  xecutes once.  B
2952a 65 63 61 75 73 65 20 66 6f 72 20 61 20 6e 6f 6e  ecause for a non
2952b 2d 63 6f 6e 73 74 61 6e 74 0a 20 20 20 20 20 20  -constant.      
2952c 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f      ** expressio
2952d 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 72  n we need to rer
2952e 75 6e 20 74 68 69 73 20 63 6f 64 65 20 65 61 63  un this code eac
2952f 68 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20  h time..        
29530 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69    */.          i
29531 66 28 20 74 65 73 74 41 64 64 72 3e 3d 30 20 26  f( testAddr>=0 &
29532 26 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73  & !sqlite3ExprIs
29533 43 6f 6e 73 74 61 6e 74 28 70 45 32 29 20 29 7b  Constant(pE2) ){
29534 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
29535 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f  ite3VdbeChangeTo
29536 4e 6f 6f 70 28 76 2c 20 74 65 73 74 41 64 64 72  Noop(v, testAddr
29537 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74  );.            t
29538 65 73 74 41 64 64 72 20 3d 20 2d 31 3b 0a 20 20  estAddr = -1;.  
29539 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
2953a 20 20 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65       /* Evaluate
2953b 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2953c 61 6e 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e  and insert it in
2953d 74 6f 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c  to the temp tabl
2953e 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69  e */.          i
2953f 66 28 20 69 73 52 6f 77 69 64 20 26 26 20 73 71  f( isRowid && sq
29540 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67  lite3ExprIsInteg
29541 65 72 28 70 45 32 2c 20 26 69 56 61 6c 54 6f 49  er(pE2, &iValToI
29542 6e 73 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ns) ){.         
29543 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29544 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
29545 74 49 6e 74 2c 20 70 45 78 70 72 2d 3e 69 54 61  tInt, pExpr->iTa
29546 62 6c 65 2c 20 72 32 2c 20 69 56 61 6c 54 6f 49  ble, r2, iValToI
29547 6e 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ns);.          }
29548 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
29549 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33 45 78    r3 = sqlite3Ex
2954a 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61  prCodeTarget(pPa
2954b 72 73 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a 20  rse, pE2, r1);. 
2954c 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69             if( i
2954d 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  sRowid ){.      
2954e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2954f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29550 4d 75 73 74 42 65 49 6e 74 2c 20 72 33 2c 0a 20  MustBeInt, r3,. 
29551 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29552 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
29553 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
29554 74 41 64 64 72 28 76 29 2b 32 29 3b 0a 20 20 20  tAddr(v)+2);.   
29555 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
29556 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
29557 4f 50 5f 49 6e 73 65 72 74 2c 20 70 45 78 70 72  OP_Insert, pExpr
29558 2d 3e 69 54 61 62 6c 65 2c 20 72 32 2c 20 72 33  ->iTable, r2, r3
29559 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  );.            }
2955a 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
2955b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2955c 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp4(v, OP_Make
2955d 52 65 63 6f 72 64 2c 20 72 33 2c 20 31 2c 20 72  Record, r3, 1, r
2955e 32 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31 29  2, &affinity, 1)
2955f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
29560 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
29561 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
29562 50 61 72 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20  Parse, r3, 1);. 
29563 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
29564 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29565 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
29566 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72  pExpr->iTable, r
29567 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  2);.            
29568 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
29569 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2956a 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2956b 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
2956c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2956d 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2956e 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20  (pParse, r2);.  
2956f 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
29570 21 69 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  !isRowid ){.    
29571 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
29572 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c  hangeP4(v, addr,
29573 20 28 76 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66   (void *)&keyInf
29574 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a  o, P4_KEYINFO);.
29575 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
29576 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
29577 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a  case TK_EXISTS:.
29578 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45      case TK_SELE
29579 43 54 3a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a  CT:.    default:
2957a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74   {.      /* If t
2957b 68 69 73 20 68 61 73 20 74 6f 20 62 65 20 61 20  his has to be a 
2957c 73 63 61 6c 61 72 20 53 45 4c 45 43 54 2e 20 20  scalar SELECT.  
2957d 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
2957e 20 70 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a   put the.      *
2957f 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20  * value of this 
29580 73 65 6c 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f  select in a memo
29581 72 79 20 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f  ry cell and reco
29582 72 64 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20  rd the number.  
29583 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65      ** of the me
29584 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f  mory cell in iCo
29585 6c 75 6d 6e 2e 20 20 49 66 20 74 68 69 73 20 69  lumn.  If this i
29586 73 20 61 6e 20 45 58 49 53 54 53 2c 20 77 72 69  s an EXISTS, wri
29587 74 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 69  te.      ** an i
29588 6e 74 65 67 65 72 20 30 20 28 6e 6f 74 20 65 78  nteger 0 (not ex
29589 69 73 74 73 29 20 6f 72 20 31 20 28 65 78 69 73  ists) or 1 (exis
2958a 74 73 29 20 69 6e 74 6f 20 61 20 6d 65 6d 6f 72  ts) into a memor
2958b 79 20 63 65 6c 6c 0a 20 20 20 20 20 20 2a 2a 20  y cell.      ** 
2958c 61 6e 64 20 72 65 63 6f 72 64 20 74 68 61 74 20  and record that 
2958d 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69  memory cell in i
2958e 43 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f  Column..      */
2958f 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70  .      Select *p
29590 53 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Sel;            
29591 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29592 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
29593 20 74 6f 20 65 6e 63 6f 64 65 20 2a 2f 0a 20 20   to encode */.  
29594 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 64      SelectDest d
29595 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  est;            
29596 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77            /* How
29597 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 53 45   to deal with SE
29598 4c 45 43 74 20 72 65 73 75 6c 74 20 2a 2f 0a 0a  LECt result */..
29599 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2959a 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58  pExpr->op==TK_EX
2959b 49 53 54 53 20 29 3b 0a 20 20 20 20 20 20 74 65  ISTS );.      te
2959c 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
2959d 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a  p==TK_SELECT );.
2959e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
2959f 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53  xpr->op==TK_EXIS
295a0 54 53 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d  TS || pExpr->op=
295a1 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 0a 20  =TK_SELECT );.. 
295a2 20 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70       assert( Exp
295a3 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
295a4 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
295a5 29 20 29 3b 0a 20 20 20 20 20 20 70 53 65 6c 20  ) );.      pSel 
295a6 3d 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65  = pExpr->x.pSele
295a7 63 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ct;.      sqlite
295a8 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
295a9 26 64 65 73 74 2c 20 30 2c 20 2b 2b 70 50 61 72  &dest, 0, ++pPar
295aa 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20  se->nMem);.     
295ab 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d   if( pExpr->op==
295ac 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20  TK_SELECT ){.   
295ad 20 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 20       dest.eDest 
295ae 3d 20 53 52 54 5f 4d 65 6d 3b 0a 20 20 20 20 20  = SRT_Mem;.     
295af 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
295b0 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
295b1 20 30 2c 20 64 65 73 74 2e 69 53 44 50 61 72 6d   0, dest.iSDParm
295b2 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43  );.        VdbeC
295b3 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69 74  omment((v, "Init
295b4 20 73 75 62 71 75 65 72 79 20 72 65 73 75 6c 74   subquery result
295b5 22 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  "));.      }else
295b6 7b 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 65  {.        dest.e
295b7 44 65 73 74 20 3d 20 53 52 54 5f 45 78 69 73 74  Dest = SRT_Exist
295b8 73 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  s;.        sqlit
295b9 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
295ba 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 64  OP_Integer, 0, d
295bb 65 73 74 2e 69 53 44 50 61 72 6d 29 3b 0a 20 20  est.iSDParm);.  
295bc 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
295bd 74 28 28 76 2c 20 22 49 6e 69 74 20 45 58 49 53  t((v, "Init EXIS
295be 54 53 20 72 65 73 75 6c 74 22 29 29 3b 0a 20 20  TS result"));.  
295bf 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
295c0 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50  te3ExprDelete(pP
295c1 61 72 73 65 2d 3e 64 62 2c 20 70 53 65 6c 2d 3e  arse->db, pSel->
295c2 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 70  pLimit);.      p
295c3 53 65 6c 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71  Sel->pLimit = sq
295c4 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
295c5 65 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30  e, TK_INTEGER, 0
295c6 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
295c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
295c8 20 20 20 20 20 20 20 26 73 71 6c 69 74 65 33 49         &sqlite3I
295c9 6e 74 54 6f 6b 65 6e 73 5b 31 5d 29 3b 0a 20 20  ntTokens[1]);.  
295ca 20 20 20 20 70 53 65 6c 2d 3e 69 4c 69 6d 69 74      pSel->iLimit
295cb 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20   = 0;.      if( 
295cc 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
295cd 61 72 73 65 2c 20 70 53 65 6c 2c 20 26 64 65 73  arse, pSel, &des
295ce 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  t) ){.        re
295cf 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
295d0 20 20 20 20 20 20 72 52 65 67 20 3d 20 64 65 73        rReg = des
295d1 74 2e 69 53 44 50 61 72 6d 3b 0a 20 20 20 20 20  t.iSDParm;.     
295d2 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69   ExprSetIrreduci
295d3 62 6c 65 28 70 45 78 70 72 29 3b 0a 20 20 20 20  ble(pExpr);.    
295d4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
295d5 20 7d 0a 0a 20 20 69 66 28 20 74 65 73 74 41 64   }..  if( testAd
295d6 64 72 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  dr>=0 ){.    sql
295d7 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
295d8 28 76 2c 20 74 65 73 74 41 64 64 72 29 3b 0a 20  (v, testAddr);. 
295d9 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72   }.  sqlite3Expr
295da 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c  CachePop(pParse,
295db 20 31 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72   1);..  return r
295dc 52 65 67 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  Reg;.}.#endif /*
295dd 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
295de 51 55 45 52 59 20 2a 2f 0a 0a 23 69 66 6e 64 65  QUERY */..#ifnde
295df 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
295e0 42 51 55 45 52 59 0a 2f 2a 0a 2a 2a 20 47 65 6e  BQUERY./*.** Gen
295e1 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61  erate code for a
295e2 6e 20 49 4e 20 65 78 70 72 65 73 73 69 6f 6e 2e  n IN expression.
295e3 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 78 20 49 4e  .**.**      x IN
295e4 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 0a 2a 2a   (SELECT ...).**
295e5 20 20 20 20 20 20 78 20 49 4e 20 28 76 61 6c 75        x IN (valu
295e6 65 2c 20 76 61 6c 75 65 2c 20 2e 2e 2e 29 0a 2a  e, value, ...).*
295e7 2a 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d 68 61  *.** The left-ha
295e8 6e 64 20 73 69 64 65 20 28 4c 48 53 29 20 69 73  nd side (LHS) is
295e9 20 61 20 73 63 61 6c 61 72 20 65 78 70 72 65 73   a scalar expres
295ea 73 69 6f 6e 2e 20 20 54 68 65 20 72 69 67 68 74  sion.  The right
295eb 2d 68 61 6e 64 20 73 69 64 65 20 28 52 48 53 29  -hand side (RHS)
295ec 0a 2a 2a 20 69 73 20 61 6e 20 61 72 72 61 79 20  .** is an array 
295ed 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  of zero or more 
295ee 76 61 6c 75 65 73 2e 20 20 54 68 65 20 65 78 70  values.  The exp
295ef 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 20  ression is true 
295f0 69 66 20 74 68 65 20 4c 48 53 20 69 73 0a 2a 2a  if the LHS is.**
295f1 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69   contained withi
295f2 6e 20 74 68 65 20 52 48 53 2e 20 20 54 68 65 20  n the RHS.  The 
295f3 76 61 6c 75 65 20 6f 66 20 74 68 65 20 65 78 70  value of the exp
295f4 72 65 73 73 69 6f 6e 20 69 73 20 75 6e 6b 6e 6f  ression is unkno
295f5 77 6e 20 28 4e 55 4c 4c 29 0a 2a 2a 20 69 66 20  wn (NULL).** if 
295f6 74 68 65 20 4c 48 53 20 69 73 20 4e 55 4c 4c 20  the LHS is NULL 
295f7 6f 72 20 69 66 20 74 68 65 20 4c 48 53 20 69 73  or if the LHS is
295f8 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 65 64 20 77   not contained w
295f9 69 74 68 69 6e 20 74 68 65 20 52 48 53 20 61 6e  ithin the RHS an
295fa 64 20 74 68 65 0a 2a 2a 20 52 48 53 20 63 6f 6e  d the.** RHS con
295fb 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72  tains one or mor
295fc 65 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2e 0a 2a  e NULL values..*
295fd 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
295fe 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65  e generates code
295ff 20 77 69 6c 6c 20 6a 75 6d 70 20 74 6f 20 64 65   will jump to de
29600 73 74 49 66 46 61 6c 73 65 20 69 66 20 74 68 65  stIfFalse if the
29601 20 4c 48 53 20 69 73 20 6e 6f 74 20 0a 2a 2a 20   LHS is not .** 
29602 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e  contained within
29603 20 74 68 65 20 52 48 53 2e 20 20 49 66 20 64 75   the RHS.  If du
29604 65 20 74 6f 20 4e 55 4c 4c 73 20 77 65 20 63 61  e to NULLs we ca
29605 6e 6e 6f 74 20 64 65 74 65 72 6d 69 6e 65 20 69  nnot determine i
29606 66 20 74 68 65 20 4c 48 53 0a 2a 2a 20 69 73 20  f the LHS.** is 
29607 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65  contained in the
29608 20 52 48 53 20 74 68 65 6e 20 6a 75 6d 70 20 74   RHS then jump t
29609 6f 20 64 65 73 74 49 66 4e 75 6c 6c 2e 20 20 49  o destIfNull.  I
2960a 66 20 74 68 65 20 4c 48 53 20 69 73 20 63 6f 6e  f the LHS is con
2960b 74 61 69 6e 65 64 0a 2a 2a 20 77 69 74 68 69 6e  tained.** within
2960c 20 74 68 65 20 52 48 53 20 74 68 65 6e 20 66 61   the RHS then fa
2960d 6c 6c 20 74 68 72 6f 75 67 68 2e 0a 2a 2f 0a 73  ll through..*/.s
2960e 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
2960f 65 33 45 78 70 72 43 6f 64 65 49 4e 28 0a 20 20  e3ExprCodeIN(.  
29610 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
29611 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
29612 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61   and code genera
29613 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  ting context */.
29614 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20    Expr *pExpr,  
29615 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 49          /* The I
29616 4e 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  N expression */.
29617 20 20 69 6e 74 20 64 65 73 74 49 66 46 61 6c 73    int destIfFals
29618 65 2c 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20  e,      /* Jump 
29619 68 65 72 65 20 69 66 20 4c 48 53 20 69 73 20 6e  here if LHS is n
2961a 6f 74 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  ot contained in 
2961b 74 68 65 20 52 48 53 20 2a 2f 0a 20 20 69 6e 74  the RHS */.  int
2961c 20 64 65 73 74 49 66 4e 75 6c 6c 20 20 20 20 20   destIfNull     
2961d 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20     /* Jump here 
2961e 69 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 61  if the results a
2961f 72 65 20 75 6e 6b 6e 6f 77 6e 20 64 75 65 20 74  re unknown due t
29620 6f 20 4e 55 4c 4c 73 20 2a 2f 0a 29 7b 0a 20 20  o NULLs */.){.  
29621 69 6e 74 20 72 52 68 73 48 61 73 4e 75 6c 6c 20  int rRhsHasNull 
29622 3d 20 30 3b 20 20 2f 2a 20 52 65 67 69 73 74 65  = 0;  /* Registe
29623 72 20 74 68 61 74 20 69 73 20 74 72 75 65 20 69  r that is true i
29624 66 20 52 48 53 20 63 6f 6e 74 61 69 6e 73 20 4e  f RHS contains N
29625 55 4c 4c 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20  ULL values */.  
29626 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20  char affinity;  
29627 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 61 72 69        /* Compari
29628 73 6f 6e 20 61 66 66 69 6e 69 74 79 20 74 6f 20  son affinity to 
29629 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79  use */.  int eTy
2962a 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  pe;            /
2962b 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 52 48  * Type of the RH
2962c 53 20 2a 2f 0a 20 20 69 6e 74 20 72 31 3b 20 20  S */.  int r1;  
2962d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2962e 54 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65  Temporary use re
2962f 67 69 73 74 65 72 20 2a 2f 0a 20 20 56 64 62 65  gister */.  Vdbe
29630 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
29631 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 75    /* Statement u
29632 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
29633 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75  n */..  /* Compu
29634 74 65 20 74 68 65 20 52 48 53 2e 20 20 20 41 66  te the RHS.   Af
29635 74 65 72 20 74 68 69 73 20 73 74 65 70 2c 20 74  ter this step, t
29636 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 63 75  he table with cu
29637 72 73 6f 72 0a 20 20 2a 2a 20 70 45 78 70 72 2d  rsor.  ** pExpr-
29638 3e 69 54 61 62 6c 65 20 77 69 6c 6c 20 63 6f 6e  >iTable will con
29639 74 61 69 6e 73 20 74 68 65 20 76 61 6c 75 65 73  tains the values
2963a 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68   that make up th
2963b 65 20 52 48 53 2e 0a 20 20 2a 2f 0a 20 20 76 20  e RHS..  */.  v 
2963c 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
2963d 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20  .  assert( v!=0 
2963e 29 3b 20 20 20 20 20 20 20 2f 2a 20 4f 4f 4d 20  );       /* OOM 
2963f 64 65 74 65 63 74 65 64 20 70 72 69 6f 72 20 74  detected prior t
29640 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 2a  o this routine *
29641 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  /.  VdbeNoopComm
29642 65 6e 74 28 28 76 2c 20 22 62 65 67 69 6e 20 49  ent((v, "begin I
29643 4e 20 65 78 70 72 22 29 29 3b 0a 20 20 65 54 79  N expr"));.  eTy
29644 70 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  pe = sqlite3Find
29645 49 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  InIndex(pParse, 
29646 70 45 78 70 72 2c 20 26 72 52 68 73 48 61 73 4e  pExpr, &rRhsHasN
29647 75 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75  ull);..  /* Figu
29648 72 65 20 6f 75 74 20 74 68 65 20 61 66 66 69 6e  re out the affin
29649 69 74 79 20 74 6f 20 75 73 65 20 74 6f 20 63 72  ity to use to cr
2964a 65 61 74 65 20 61 20 6b 65 79 20 66 72 6f 6d 20  eate a key from 
2964b 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20 2a 2a  the results.  **
2964c 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
2964d 6f 6e 2e 20 61 66 66 69 6e 69 74 79 53 74 72 20  on. affinityStr 
2964e 73 74 6f 72 65 73 20 61 20 73 74 61 74 69 63 20  stores a static 
2964f 73 74 72 69 6e 67 20 73 75 69 74 61 62 6c 65 20  string suitable 
29650 66 6f 72 0a 20 20 2a 2a 20 50 34 20 6f 66 20 4f  for.  ** P4 of O
29651 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2e 0a 20 20  P_MakeRecord..  
29652 2a 2f 0a 20 20 61 66 66 69 6e 69 74 79 20 3d 20  */.  affinity = 
29653 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69  comparisonAffini
29654 74 79 28 70 45 78 70 72 29 3b 0a 0a 20 20 2f 2a  ty(pExpr);..  /*
29655 20 43 6f 64 65 20 74 68 65 20 4c 48 53 2c 20 74   Code the LHS, t
29656 68 65 20 3c 65 78 70 72 3e 20 66 72 6f 6d 20 22  he <expr> from "
29657 3c 65 78 70 72 3e 20 49 4e 20 28 2e 2e 2e 29 22  <expr> IN (...)"
29658 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
29659 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50  ExprCachePush(pP
2965a 61 72 73 65 29 3b 0a 20 20 72 31 20 3d 20 73 71  arse);.  r1 = sq
2965b 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
2965c 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74  pParse);.  sqlit
2965d 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
2965e 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
2965f 20 72 31 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74   r1);..  /* If t
29660 68 65 20 4c 48 53 20 69 73 20 4e 55 4c 4c 2c 20  he LHS is NULL, 
29661 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20  then the result 
29662 69 73 20 65 69 74 68 65 72 20 66 61 6c 73 65 20  is either false 
29663 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64 69 6e  or NULL dependin
29664 67 0a 20 20 2a 2a 20 6f 6e 20 77 68 65 74 68 65  g.  ** on whethe
29665 72 20 74 68 65 20 52 48 53 20 69 73 20 65 6d 70  r the RHS is emp
29666 74 79 20 6f 72 20 6e 6f 74 2c 20 72 65 73 70 65  ty or not, respe
29667 63 74 69 76 65 6c 79 2e 0a 20 20 2a 2f 0a 20 20  ctively..  */.  
29668 69 66 28 20 64 65 73 74 49 66 4e 75 6c 6c 3d 3d  if( destIfNull==
29669 64 65 73 74 49 66 46 61 6c 73 65 20 29 7b 0a 20  destIfFalse ){. 
2966a 20 20 20 2f 2a 20 53 68 6f 72 74 63 75 74 20 66     /* Shortcut f
2966b 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61  or the common ca
2966c 73 65 20 77 68 65 72 65 20 74 68 65 20 66 61 6c  se where the fal
2966d 73 65 20 61 6e 64 20 4e 55 4c 4c 20 6f 75 74 63  se and NULL outc
2966e 6f 6d 65 73 20 61 72 65 0a 20 20 20 20 2a 2a 20  omes are.    ** 
2966f 74 68 65 20 73 61 6d 65 2e 20 2a 2f 0a 20 20 20  the same. */.   
29670 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29671 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c  p2(v, OP_IsNull,
29672 20 72 31 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29   r1, destIfNull)
29673 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
29674 6e 74 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74  nt addr1 = sqlit
29675 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
29676 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 31 29 3b  OP_NotNull, r1);
29677 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29678 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77  AddOp2(v, OP_Rew
29679 69 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62  ind, pExpr->iTab
2967a 6c 65 2c 20 64 65 73 74 49 66 46 61 6c 73 65 29  le, destIfFalse)
2967b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2967c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2967d 74 6f 2c 20 30 2c 20 64 65 73 74 49 66 4e 75 6c  to, 0, destIfNul
2967e 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  l);.    sqlite3V
2967f 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
29680 64 64 72 31 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  ddr1);.  }..  if
29681 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45  ( eType==IN_INDE
29682 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 2f  X_ROWID ){.    /
29683 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20  * In this case, 
29684 74 68 65 20 52 48 53 20 69 73 20 74 68 65 20 52  the RHS is the R
29685 4f 57 49 44 20 6f 66 20 74 61 62 6c 65 20 62 2d  OWID of table b-
29686 74 72 65 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20  tree.    */.    
29687 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29688 32 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e  2(v, OP_MustBeIn
29689 74 2c 20 72 31 2c 20 64 65 73 74 49 66 46 61 6c  t, r1, destIfFal
2968a 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  se);.    sqlite3
2968b 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2968c 5f 4e 6f 74 45 78 69 73 74 73 2c 20 70 45 78 70  _NotExists, pExp
2968d 72 2d 3e 69 54 61 62 6c 65 2c 20 64 65 73 74 49  r->iTable, destI
2968e 66 46 61 6c 73 65 2c 20 72 31 29 3b 0a 20 20 7d  fFalse, r1);.  }
2968f 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 6e 20  else{.    /* In 
29690 74 68 69 73 20 63 61 73 65 2c 20 74 68 65 20 52  this case, the R
29691 48 53 20 69 73 20 61 6e 20 69 6e 64 65 78 20 62  HS is an index b
29692 2d 74 72 65 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  -tree..    */.  
29693 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29694 4f 70 34 28 76 2c 20 4f 50 5f 41 66 66 69 6e 69  Op4(v, OP_Affini
29695 74 79 2c 20 72 31 2c 20 31 2c 20 30 2c 20 26 61  ty, r1, 1, 0, &a
29696 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 0a 20 20  ffinity, 1);..  
29697 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 74 20    /* If the set 
29698 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 20  membership test 
29699 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68 65 20  fails, then the 
2969a 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 0a 20  result of the . 
2969b 20 20 20 2a 2a 20 22 78 20 49 4e 20 28 2e 2e 2e     ** "x IN (...
2969c 29 22 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75  )" expression mu
2969d 73 74 20 62 65 20 65 69 74 68 65 72 20 30 20 6f  st be either 0 o
2969e 72 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 73  r NULL. If the s
2969f 65 74 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69  et.    ** contai
296a0 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c 75 65  ns no NULL value
296a1 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  s, then the resu
296a2 6c 74 20 69 73 20 30 2e 20 49 66 20 74 68 65 20  lt is 0. If the 
296a3 73 65 74 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 74  set .    ** cont
296a4 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  ains one or more
296a5 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68   NULL values, th
296a6 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  en the result of
296a7 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 78 70 72   the.    ** expr
296a8 65 73 73 69 6f 6e 20 69 73 20 61 6c 73 6f 20 4e  ession is also N
296a9 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ULL..    */.    
296aa 69 66 28 20 72 52 68 73 48 61 73 4e 75 6c 6c 3d  if( rRhsHasNull=
296ab 3d 30 20 7c 7c 20 64 65 73 74 49 66 46 61 6c 73  =0 || destIfFals
296ac 65 3d 3d 64 65 73 74 49 66 4e 75 6c 6c 20 29 7b  e==destIfNull ){
296ad 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62  .      /* This b
296ae 72 61 6e 63 68 20 72 75 6e 73 20 69 66 20 69 74  ranch runs if it
296af 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20 63 6f 6d   is known at com
296b0 70 69 6c 65 20 74 69 6d 65 20 74 68 61 74 20 74  pile time that t
296b1 68 65 20 52 48 53 0a 20 20 20 20 20 20 2a 2a 20  he RHS.      ** 
296b2 63 61 6e 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 4e  cannot contain N
296b3 55 4c 4c 20 76 61 6c 75 65 73 2e 20 54 68 69 73  ULL values. This
296b4 20 68 61 70 70 65 6e 73 20 61 73 20 74 68 65 20   happens as the 
296b5 72 65 73 75 6c 74 0a 20 20 20 20 20 20 2a 2a 20  result.      ** 
296b6 6f 66 20 61 20 22 4e 4f 54 20 4e 55 4c 4c 22 20  of a "NOT NULL" 
296b7 63 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 74 68  constraint in th
296b8 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
296b9 61 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  a..      **.    
296ba 20 20 2a 2a 20 41 6c 73 6f 20 72 75 6e 20 74 68    ** Also run th
296bb 69 73 20 62 72 61 6e 63 68 20 69 66 20 4e 55 4c  is branch if NUL
296bc 4c 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20  L is equivalent 
296bd 74 6f 20 46 41 4c 53 45 0a 20 20 20 20 20 20 2a  to FALSE.      *
296be 2a 20 66 6f 72 20 74 68 69 73 20 70 61 72 74 69  * for this parti
296bf 63 75 6c 61 72 20 49 4e 20 6f 70 65 72 61 74 6f  cular IN operato
296c0 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  r..      */.    
296c1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
296c2 4f 70 34 49 6e 74 28 76 2c 20 4f 50 5f 4e 6f 74  Op4Int(v, OP_Not
296c3 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54  Found, pExpr->iT
296c4 61 62 6c 65 2c 20 64 65 73 74 49 66 46 61 6c 73  able, destIfFals
296c5 65 2c 20 72 31 2c 20 31 29 3b 0a 0a 20 20 20 20  e, r1, 1);..    
296c6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
296c7 49 6e 20 74 68 69 73 20 62 72 61 6e 63 68 2c 20  In this branch, 
296c8 74 68 65 20 52 48 53 20 6f 66 20 74 68 65 20 49  the RHS of the I
296c9 4e 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20  N might contain 
296ca 61 20 4e 55 4c 4c 20 61 6e 64 0a 20 20 20 20 20  a NULL and.     
296cb 20 2a 2a 20 74 68 65 20 70 72 65 73 65 6e 63 65   ** the presence
296cc 20 6f 66 20 61 20 4e 55 4c 4c 20 6f 6e 20 74 68   of a NULL on th
296cd 65 20 52 48 53 20 6d 61 6b 65 73 20 61 20 64 69  e RHS makes a di
296ce 66 66 65 72 65 6e 63 65 20 69 6e 20 74 68 65 0a  fference in the.
296cf 20 20 20 20 20 20 2a 2a 20 6f 75 74 63 6f 6d 65        ** outcome
296d0 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
296d1 20 69 6e 74 20 6a 31 2c 20 6a 32 2c 20 6a 33 3b   int j1, j2, j3;
296d2 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74  ..      /* First
296d3 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   check to see if
296d4 20 74 68 65 20 4c 48 53 20 69 73 20 63 6f 6e 74   the LHS is cont
296d5 61 69 6e 65 64 20 69 6e 20 74 68 65 20 52 48 53  ained in the RHS
296d6 2e 20 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20  .  If so,.      
296d7 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 72 65 73  ** then the pres
296d8 65 6e 63 65 20 6f 66 20 4e 55 4c 4c 73 20 69 6e  ence of NULLs in
296d9 20 74 68 65 20 52 48 53 20 64 6f 65 73 20 6e 6f   the RHS does no
296da 74 20 6d 61 74 74 65 72 2c 20 73 6f 20 6a 75 6d  t matter, so jum
296db 70 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 20  p.      ** over 
296dc 61 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64 65 20  all of the code 
296dd 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a 20 20  that follows..  
296de 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6a 31 20      */.      j1 
296df 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
296e0 4f 70 34 49 6e 74 28 76 2c 20 4f 50 5f 46 6f 75  Op4Int(v, OP_Fou
296e1 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  nd, pExpr->iTabl
296e2 65 2c 20 30 2c 20 72 31 2c 20 31 29 3b 0a 0a 20  e, 0, r1, 1);.. 
296e3 20 20 20 20 20 2f 2a 20 48 65 72 65 20 77 65 20       /* Here we 
296e4 62 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67  begin generating
296e5 20 63 6f 64 65 20 74 68 61 74 20 72 75 6e 73 20   code that runs 
296e6 69 66 20 74 68 65 20 4c 48 53 20 69 73 20 6e 6f  if the LHS is no
296e7 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61  t.      ** conta
296e8 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20  ined within the 
296e9 52 48 53 2e 20 20 47 65 6e 65 72 61 74 65 20 61  RHS.  Generate a
296ea 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65 20 74  dditional code t
296eb 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 74 65 73  hat.      ** tes
296ec 74 73 20 74 68 65 20 52 48 53 20 66 6f 72 20 4e  ts the RHS for N
296ed 55 4c 4c 73 2e 20 20 49 66 20 74 68 65 20 52 48  ULLs.  If the RH
296ee 53 20 63 6f 6e 74 61 69 6e 73 20 61 20 4e 55 4c  S contains a NUL
296ef 4c 20 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20  L then.      ** 
296f0 6a 75 6d 70 20 74 6f 20 64 65 73 74 49 66 4e 75  jump to destIfNu
296f1 6c 6c 2e 20 20 49 66 20 74 68 65 72 65 20 61 72  ll.  If there ar
296f2 65 20 6e 6f 20 4e 55 4c 4c 73 20 69 6e 20 74 68  e no NULLs in th
296f3 65 20 52 48 53 20 74 68 65 6e 0a 20 20 20 20 20  e RHS then.     
296f4 20 2a 2a 20 6a 75 6d 70 20 74 6f 20 64 65 73 74   ** jump to dest
296f5 49 66 46 61 6c 73 65 2e 0a 20 20 20 20 20 20 2a  IfFalse..      *
296f6 2f 0a 20 20 20 20 20 20 6a 32 20 3d 20 73 71 6c  /.      j2 = sql
296f7 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
296f8 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 52  , OP_NotNull, rR
296f9 68 73 48 61 73 4e 75 6c 6c 29 3b 0a 20 20 20 20  hsHasNull);.    
296fa 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64    j3 = sqlite3Vd
296fb 62 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f  beAddOp4Int(v, O
296fc 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d 3e  P_Found, pExpr->
296fd 69 54 61 62 6c 65 2c 20 30 2c 20 72 52 68 73 48  iTable, 0, rRhsH
296fe 61 73 4e 75 6c 6c 2c 20 31 29 3b 0a 20 20 20 20  asNull, 1);.    
296ff 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29700 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
29701 72 2c 20 2d 31 2c 20 72 52 68 73 48 61 73 4e 75  r, -1, rRhsHasNu
29702 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ll);.      sqlit
29703 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
29704 2c 20 6a 33 29 3b 0a 20 20 20 20 20 20 73 71 6c  , j3);.      sql
29705 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29706 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 52 68  , OP_AddImm, rRh
29707 73 48 61 73 4e 75 6c 6c 2c 20 31 29 3b 0a 20 20  sHasNull, 1);.  
29708 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
29709 75 6d 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a  umpHere(v, j2);.
2970a 0a 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 74  .      /* Jump t
2970b 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  o the appropriat
2970c 65 20 74 61 72 67 65 74 20 64 65 70 65 6e 64 69  e target dependi
2970d 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72  ng on whether or
2970e 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 74 68   not.      ** th
2970f 65 20 52 48 53 20 63 6f 6e 74 61 69 6e 73 20 61  e RHS contains a
29710 20 4e 55 4c 4c 0a 20 20 20 20 20 20 2a 2f 0a 20   NULL.      */. 
29711 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29712 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c  AddOp2(v, OP_If,
29713 20 72 52 68 73 48 61 73 4e 75 6c 6c 2c 20 64 65   rRhsHasNull, de
29714 73 74 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  stIfNull);.     
29715 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29716 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
29717 2c 20 64 65 73 74 49 66 46 61 6c 73 65 29 3b 0a  , destIfFalse);.
29718 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 50  .      /* The OP
29719 5f 46 6f 75 6e 64 20 61 74 20 74 68 65 20 74 6f  _Found at the to
2971a 70 20 6f 66 20 74 68 69 73 20 62 72 61 6e 63 68  p of this branch
2971b 20 6a 75 6d 70 73 20 68 65 72 65 20 77 68 65 6e   jumps here when
2971c 20 74 72 75 65 2c 20 0a 20 20 20 20 20 20 2a 2a   true, .      **
2971d 20 63 61 75 73 69 6e 67 20 74 68 65 20 6f 76 65   causing the ove
2971e 72 61 6c 6c 20 49 4e 20 65 78 70 72 65 73 73 69  rall IN expressi
2971f 6f 6e 20 65 76 61 6c 75 61 74 69 6f 6e 20 74 6f  on evaluation to
29720 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 2e 0a 20   fall through.. 
29721 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
29722 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
29723 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 7d 0a  e(v, j1);.    }.
29724 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c    }.  sqlite3Rel
29725 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
29726 73 65 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74  se, r1);.  sqlit
29727 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 70  e3ExprCachePop(p
29728 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 56 64 62  Parse, 1);.  Vdb
29729 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e  eComment((v, "en
2972a 64 20 49 4e 20 65 78 70 72 22 29 29 3b 0a 7d 0a  d IN expr"));.}.
2972b 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2972c 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a  _OMIT_SUBQUERY *
2972d 2f 0a 0a 2f 2a 0a 2a 2a 20 44 75 70 6c 69 63 61  /../*.** Duplica
2972e 74 65 20 61 6e 20 38 2d 62 79 74 65 20 76 61 6c  te an 8-byte val
2972f 75 65 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  ue.*/.static cha
29730 72 20 2a 64 75 70 38 62 79 74 65 73 28 56 64 62  r *dup8bytes(Vdb
29731 65 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72  e *v, const char
29732 20 2a 69 6e 29 7b 0a 20 20 63 68 61 72 20 2a 6f   *in){.  char *o
29733 75 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ut = sqlite3DbMa
29734 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 56  llocRaw(sqlite3V
29735 64 62 65 44 62 28 76 29 2c 20 38 29 3b 0a 20 20  dbeDb(v), 8);.  
29736 69 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 20 6d  if( out ){.    m
29737 65 6d 63 70 79 28 6f 75 74 2c 20 69 6e 2c 20 38  emcpy(out, in, 8
29738 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
29739 6f 75 74 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  out;.}..#ifndef 
2973a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
2973b 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 0a 2a 2a  TING_POINT./*.**
2973c 20 47 65 6e 65 72 61 74 65 20 61 6e 20 69 6e 73   Generate an ins
2973d 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 77 69  truction that wi
2973e 6c 6c 20 70 75 74 20 74 68 65 20 66 6c 6f 61 74  ll put the float
2973f 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c  ing point.** val
29740 75 65 20 64 65 73 63 72 69 62 65 64 20 62 79 20  ue described by 
29741 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72  z[0..n-1] into r
29742 65 67 69 73 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a  egister iMem..**
29743 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73 74 72 69  .** The z[] stri
29744 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61 62 6c 79  ng will probably
29745 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d 74 65 72   not be zero-ter
29746 6d 69 6e 61 74 65 64 2e 20 20 42 75 74 20 74 68  minated.  But th
29747 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68 61 72 61  e .** z[n] chara
29748 63 74 65 72 20 69 73 20 67 75 61 72 61 6e 74 65  cter is guarante
29749 65 64 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69  ed to be somethi
2974a 6e 67 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74  ng that does not
2974b 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68   look.** like th
2974c 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 6f  e continuation o
2974d 66 20 74 68 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f  f the number..*/
2974e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
2974f 65 52 65 61 6c 28 56 64 62 65 20 2a 76 2c 20 63  eReal(Vdbe *v, c
29750 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e  onst char *z, in
29751 74 20 6e 65 67 61 74 65 46 6c 61 67 2c 20 69 6e  t negateFlag, in
29752 74 20 69 4d 65 6d 29 7b 0a 20 20 69 66 28 20 41  t iMem){.  if( A
29753 4c 57 41 59 53 28 7a 21 3d 30 29 20 29 7b 0a 20  LWAYS(z!=0) ){. 
29754 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 75 65 3b     double value;
29755 0a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a 20  .    char *zV;. 
29756 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 7a     sqlite3AtoF(z
29757 2c 20 26 76 61 6c 75 65 2c 20 73 71 6c 69 74 65  , &value, sqlite
29758 33 53 74 72 6c 65 6e 33 30 28 7a 29 2c 20 53 51  3Strlen30(z), SQ
29759 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20  LITE_UTF8);.    
2975a 61 73 73 65 72 74 28 20 21 73 71 6c 69 74 65 33  assert( !sqlite3
2975b 49 73 4e 61 4e 28 76 61 6c 75 65 29 20 29 3b 20  IsNaN(value) ); 
2975c 2f 2a 20 54 68 65 20 6e 65 77 20 41 74 6f 46 20  /* The new AtoF 
2975d 6e 65 76 65 72 20 72 65 74 75 72 6e 73 20 4e 61  never returns Na
2975e 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 67  N */.    if( neg
2975f 61 74 65 46 6c 61 67 20 29 20 76 61 6c 75 65 20  ateFlag ) value 
29760 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20 7a 56  = -value;.    zV
29761 20 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c 20   = dup8bytes(v, 
29762 28 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a  (char*)&value);.
29763 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29764 64 64 4f 70 34 28 76 2c 20 4f 50 5f 52 65 61 6c  ddOp4(v, OP_Real
29765 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56  , 0, iMem, 0, zV
29766 2c 20 50 34 5f 52 45 41 4c 29 3b 0a 20 20 7d 0a  , P4_REAL);.  }.
29767 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  }.#endif.../*.**
29768 20 47 65 6e 65 72 61 74 65 20 61 6e 20 69 6e 73   Generate an ins
29769 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 77 69  truction that wi
2976a 6c 6c 20 70 75 74 20 74 68 65 20 69 6e 74 65 67  ll put the integ
2976b 65 72 20 64 65 73 63 72 69 62 65 20 62 79 0a 2a  er describe by.*
2976c 2a 20 74 65 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d  * text z[0..n-1]
2976d 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69   into register i
2976e 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e  Mem..**.** Expr.
2976f 75 2e 7a 54 6f 6b 65 6e 20 69 73 20 61 6c 77 61  u.zToken is alwa
29770 79 73 20 55 54 46 38 20 61 6e 64 20 7a 65 72 6f  ys UTF8 and zero
29771 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a  -terminated..*/.
29772 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65  static void code
29773 49 6e 74 65 67 65 72 28 50 61 72 73 65 20 2a 70  Integer(Parse *p
29774 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
29775 70 72 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 2c  pr, int negFlag,
29776 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 56 64   int iMem){.  Vd
29777 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
29778 70 56 64 62 65 3b 0a 20 20 69 66 28 20 70 45 78  pVdbe;.  if( pEx
29779 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49  pr->flags & EP_I
2977a 6e 74 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 69  ntValue ){.    i
2977b 6e 74 20 69 20 3d 20 70 45 78 70 72 2d 3e 75 2e  nt i = pExpr->u.
2977c 69 56 61 6c 75 65 3b 0a 20 20 20 20 61 73 73 65  iValue;.    asse
2977d 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 20 20  rt( i>=0 );.    
2977e 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 69 20  if( negFlag ) i 
2977f 3d 20 2d 69 3b 0a 20 20 20 20 73 71 6c 69 74 65  = -i;.    sqlite
29780 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29781 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 69 4d  P_Integer, i, iM
29782 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  em);.  }else{.  
29783 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 69 36 34    int c;.    i64
29784 20 76 61 6c 75 65 3b 0a 20 20 20 20 63 6f 6e 73   value;.    cons
29785 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 45 78 70  t char *z = pExp
29786 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20  r->u.zToken;.   
29787 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20 29 3b   assert( z!=0 );
29788 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33  .    c = sqlite3
29789 41 74 6f 69 36 34 28 7a 2c 20 26 76 61 6c 75 65  Atoi64(z, &value
2978a 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  , sqlite3Strlen3
2978b 30 28 7a 29 2c 20 53 51 4c 49 54 45 5f 55 54 46  0(z), SQLITE_UTF
2978c 38 29 3b 0a 20 20 20 20 69 66 28 20 63 3d 3d 30  8);.    if( c==0
2978d 20 7c 7c 20 28 63 3d 3d 32 20 26 26 20 6e 65 67   || (c==2 && neg
2978e 46 6c 61 67 29 20 29 7b 0a 20 20 20 20 20 20 63  Flag) ){.      c
2978f 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20 20 20 69  har *zV;.      i
29790 66 28 20 6e 65 67 46 6c 61 67 20 29 7b 20 76 61  f( negFlag ){ va
29791 6c 75 65 20 3d 20 63 3d 3d 32 20 3f 20 53 4d 41  lue = c==2 ? SMA
29792 4c 4c 45 53 54 5f 49 4e 54 36 34 20 3a 20 2d 76  LLEST_INT64 : -v
29793 61 6c 75 65 3b 20 7d 0a 20 20 20 20 20 20 7a 56  alue; }.      zV
29794 20 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c 20   = dup8bytes(v, 
29795 28 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a  (char*)&value);.
29796 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29797 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 6e  eAddOp4(v, OP_In
29798 74 36 34 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c  t64, 0, iMem, 0,
29799 20 7a 56 2c 20 50 34 5f 49 4e 54 36 34 29 3b 0a   zV, P4_INT64);.
2979a 20 20 20 20 7d 65 6c 73 65 7b 0a 23 69 66 64 65      }else{.#ifde
2979b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
2979c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20  OATING_POINT.   
2979d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2979e 73 67 28 70 50 61 72 73 65 2c 20 22 6f 76 65 72  sg(pParse, "over
2979f 73 69 7a 65 64 20 69 6e 74 65 67 65 72 3a 20 25  sized integer: %
297a0 73 25 73 22 2c 20 6e 65 67 46 6c 61 67 20 3f 20  s%s", negFlag ? 
297a1 22 2d 22 20 3a 20 22 22 2c 20 7a 29 3b 0a 23 65  "-" : "", z);.#e
297a2 6c 73 65 0a 20 20 20 20 20 20 63 6f 64 65 52 65  lse.      codeRe
297a3 61 6c 28 76 2c 20 7a 2c 20 6e 65 67 46 6c 61 67  al(v, z, negFlag
297a4 2c 20 69 4d 65 6d 29 3b 0a 23 65 6e 64 69 66 0a  , iMem);.#endif.
297a5 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
297a6 2a 2a 20 43 6c 65 61 72 20 61 20 63 61 63 68 65  ** Clear a cache
297a7 20 65 6e 74 72 79 2e 0a 2a 2f 0a 73 74 61 74 69   entry..*/.stati
297a8 63 20 76 6f 69 64 20 63 61 63 68 65 45 6e 74 72  c void cacheEntr
297a9 79 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 50  yClear(Parse *pP
297aa 61 72 73 65 2c 20 73 74 72 75 63 74 20 79 43 6f  arse, struct yCo
297ab 6c 43 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 66  lCache *p){.  if
297ac 28 20 70 2d 3e 74 65 6d 70 52 65 67 20 29 7b 0a  ( p->tempReg ){.
297ad 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
297ae 6e 54 65 6d 70 52 65 67 3c 41 72 72 61 79 53 69  nTempReg<ArraySi
297af 7a 65 28 70 50 61 72 73 65 2d 3e 61 54 65 6d 70  ze(pParse->aTemp
297b0 52 65 67 29 20 29 7b 0a 20 20 20 20 20 20 70 50  Reg) ){.      pP
297b1 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 70  arse->aTempReg[p
297b2 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 2b  Parse->nTempReg+
297b3 2b 5d 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20  +] = p->iReg;.  
297b4 20 20 7d 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52    }.    p->tempR
297b5 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a  eg = 0;.  }.}...
297b6 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 69 6e 20  /*.** Record in 
297b7 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  the column cache
297b8 20 74 68 61 74 20 61 20 70 61 72 74 69 63 75 6c   that a particul
297b9 61 72 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61  ar column from a
297ba 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 74  .** particular t
297bb 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69  able is stored i
297bc 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72  n a particular r
297bd 65 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  egister..*/.SQLI
297be 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
297bf 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
297c0 53 74 6f 72 65 28 50 61 72 73 65 20 2a 70 50 61  Store(Parse *pPa
297c1 72 73 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 69  rse, int iTab, i
297c2 6e 74 20 69 43 6f 6c 2c 20 69 6e 74 20 69 52 65  nt iCol, int iRe
297c3 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  g){.  int i;.  i
297c4 6e 74 20 6d 69 6e 4c 72 75 3b 0a 20 20 69 6e 74  nt minLru;.  int
297c5 20 69 64 78 4c 72 75 3b 0a 20 20 73 74 72 75 63   idxLru;.  struc
297c6 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a  t yColCache *p;.
297c7 0a 20 20 61 73 73 65 72 74 28 20 69 52 65 67 3e  .  assert( iReg>
297c8 30 20 29 3b 20 20 2f 2a 20 52 65 67 69 73 74 65  0 );  /* Registe
297c9 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61 6c  r numbers are al
297ca 77 61 79 73 20 70 6f 73 69 74 69 76 65 20 2a 2f  ways positive */
297cb 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e  .  assert( iCol>
297cc 3d 2d 31 20 26 26 20 69 43 6f 6c 3c 33 32 37 36  =-1 && iCol<3276
297cd 38 20 29 3b 20 20 2f 2a 20 46 69 6e 69 74 65 20  8 );  /* Finite 
297ce 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 73 20 2a  column numbers *
297cf 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 53 51 4c 49  /..  /* The SQLI
297d0 54 45 5f 43 6f 6c 75 6d 6e 43 61 63 68 65 20 66  TE_ColumnCache f
297d1 6c 61 67 20 64 69 73 61 62 6c 65 73 20 74 68 65  lag disables the
297d2 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 20 20   column cache.  
297d3 54 68 69 73 20 69 73 20 75 73 65 64 0a 20 20 2a  This is used.  *
297d4 2a 20 66 6f 72 20 74 65 73 74 69 6e 67 20 6f 6e  * for testing on
297d5 6c 79 20 2d 20 74 6f 20 76 65 72 69 66 79 20 74  ly - to verify t
297d6 68 61 74 20 53 51 4c 69 74 65 20 61 6c 77 61 79  hat SQLite alway
297d7 73 20 67 65 74 73 20 74 68 65 20 73 61 6d 65 20  s gets the same 
297d8 61 6e 73 77 65 72 0a 20 20 2a 2a 20 77 69 74 68  answer.  ** with
297d9 20 61 6e 64 20 77 69 74 68 6f 75 74 20 74 68 65   and without the
297da 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 0a 20   column cache.. 
297db 20 2a 2f 0a 20 20 69 66 28 20 4f 70 74 69 6d 69   */.  if( Optimi
297dc 7a 61 74 69 6f 6e 44 69 73 61 62 6c 65 64 28 70  zationDisabled(p
297dd 50 61 72 73 65 2d 3e 64 62 2c 20 53 51 4c 49 54  Parse->db, SQLIT
297de 45 5f 43 6f 6c 75 6d 6e 43 61 63 68 65 29 20 29  E_ColumnCache) )
297df 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 46   return;..  /* F
297e0 69 72 73 74 20 72 65 70 6c 61 63 65 20 61 6e 79  irst replace any
297e1 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 2e   existing entry.
297e2 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 63 74 75 61  .  **.  ** Actua
297e3 6c 6c 79 2c 20 74 68 65 20 77 61 79 20 74 68 65  lly, the way the
297e4 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 69 73   column cache is
297e5 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 2c   currently used,
297e6 20 77 65 20 61 72 65 20 67 75 61 72 61 6e 74 65   we are guarante
297e7 65 64 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65  ed.  ** that the
297e8 20 6f 62 6a 65 63 74 20 77 69 6c 6c 20 6e 65 76   object will nev
297e9 65 72 20 61 6c 72 65 61 64 79 20 62 65 20 69 6e  er already be in
297ea 20 63 61 63 68 65 2e 20 20 56 65 72 69 66 79 20   cache.  Verify 
297eb 74 68 69 73 20 67 75 61 72 61 6e 74 65 65 2e 0a  this guarantee..
297ec 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45    */.#ifndef NDE
297ed 42 55 47 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  BUG.  for(i=0, p
297ee 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63  =pParse->aColCac
297ef 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43  he; i<SQLITE_N_C
297f0 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b  OLCACHE; i++, p+
297f1 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  +){.    assert( 
297f2 70 2d 3e 69 52 65 67 3d 3d 30 20 7c 7c 20 70 2d  p->iReg==0 || p-
297f3 3e 69 54 61 62 6c 65 21 3d 69 54 61 62 20 7c 7c  >iTable!=iTab ||
297f4 20 70 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 69 43 6f   p->iColumn!=iCo
297f5 6c 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  l );.  }.#endif.
297f6 0a 20 20 2f 2a 20 46 69 6e 64 20 61 6e 20 65 6d  .  /* Find an em
297f7 70 74 79 20 73 6c 6f 74 20 61 6e 64 20 72 65 70  pty slot and rep
297f8 6c 61 63 65 20 69 74 20 2a 2f 0a 20 20 66 6f 72  lace it */.  for
297f9 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
297fa 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
297fb 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
297fc 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
297fd 66 28 20 70 2d 3e 69 52 65 67 3d 3d 30 20 29 7b  f( p->iReg==0 ){
297fe 0a 20 20 20 20 20 20 70 2d 3e 69 4c 65 76 65 6c  .      p->iLevel
297ff 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68   = pParse->iCach
29800 65 4c 65 76 65 6c 3b 0a 20 20 20 20 20 20 70 2d  eLevel;.      p-
29801 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a  >iTable = iTab;.
29802 20 20 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e        p->iColumn
29803 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 70   = iCol;.      p
29804 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a 20  ->iReg = iReg;. 
29805 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20       p->tempReg 
29806 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 72  = 0;.      p->lr
29807 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63  u = pParse->iCac
29808 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 72  heCnt++;.      r
29809 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
2980a 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 74  ..  /* Replace t
2980b 68 65 20 6c 61 73 74 20 72 65 63 65 6e 74 6c 79  he last recently
2980c 20 75 73 65 64 20 2a 2f 0a 20 20 6d 69 6e 4c 72   used */.  minLr
2980d 75 20 3d 20 30 78 37 66 66 66 66 66 66 66 3b 0a  u = 0x7fffffff;.
2980e 20 20 69 64 78 4c 72 75 20 3d 20 2d 31 3b 0a 20    idxLru = -1;. 
2980f 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72   for(i=0, p=pPar
29810 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69  se->aColCache; i
29811 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43  <SQLITE_N_COLCAC
29812 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  HE; i++, p++){. 
29813 20 20 20 69 66 28 20 70 2d 3e 6c 72 75 3c 6d 69     if( p->lru<mi
29814 6e 4c 72 75 20 29 7b 0a 20 20 20 20 20 20 69 64  nLru ){.      id
29815 78 4c 72 75 20 3d 20 69 3b 0a 20 20 20 20 20 20  xLru = i;.      
29816 6d 69 6e 4c 72 75 20 3d 20 70 2d 3e 6c 72 75 3b  minLru = p->lru;
29817 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
29818 20 41 4c 57 41 59 53 28 69 64 78 4c 72 75 3e 3d   ALWAYS(idxLru>=
29819 30 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 26 70  0) ){.    p = &p
2981a 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65  Parse->aColCache
2981b 5b 69 64 78 4c 72 75 5d 3b 0a 20 20 20 20 70 2d  [idxLru];.    p-
2981c 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65  >iLevel = pParse
2981d 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20  ->iCacheLevel;. 
2981e 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69     p->iTable = i
2981f 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 43 6f 6c  Tab;.    p->iCol
29820 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20  umn = iCol;.    
29821 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a  p->iReg = iReg;.
29822 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d      p->tempReg =
29823 20 30 3b 0a 20 20 20 20 70 2d 3e 6c 72 75 20 3d   0;.    p->lru =
29824 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43   pParse->iCacheC
29825 6e 74 2b 2b 3b 0a 20 20 20 20 72 65 74 75 72 6e  nt++;.    return
29826 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
29827 6e 64 69 63 61 74 65 20 74 68 61 74 20 72 65 67  ndicate that reg
29828 69 73 74 65 72 73 20 62 65 74 77 65 65 6e 20 69  isters between i
29829 52 65 67 2e 2e 69 52 65 67 2b 6e 52 65 67 2d 31  Reg..iReg+nReg-1
2982a 20 61 72 65 20 62 65 69 6e 67 20 6f 76 65 72 77   are being overw
2982b 72 69 74 74 65 6e 2e 0a 2a 2a 20 50 75 72 67 65  ritten..** Purge
2982c 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 72 65   the range of re
2982d 67 69 73 74 65 72 73 20 66 72 6f 6d 20 74 68 65  gisters from the
2982e 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 0a 2a   column cache..*
2982f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
29830 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
29831 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61 72  rCacheRemove(Par
29832 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
29833 69 52 65 67 2c 20 69 6e 74 20 6e 52 65 67 29 7b  iReg, int nReg){
29834 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
29835 69 4c 61 73 74 20 3d 20 69 52 65 67 20 2b 20 6e  iLast = iReg + n
29836 52 65 67 20 2d 20 31 3b 0a 20 20 73 74 72 75 63  Reg - 1;.  struc
29837 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a  t yColCache *p;.
29838 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61    for(i=0, p=pPa
29839 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20  rse->aColCache; 
2983a 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41  i<SQLITE_N_COLCA
2983b 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a  CHE; i++, p++){.
2983c 20 20 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69      int r = p->i
2983d 52 65 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d  Reg;.    if( r>=
2983e 69 52 65 67 20 26 26 20 72 3c 3d 69 4c 61 73 74  iReg && r<=iLast
2983f 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45   ){.      cacheE
29840 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 65  ntryClear(pParse
29841 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69  , p);.      p->i
29842 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  Reg = 0;.    }. 
29843 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65   }.}../*.** Reme
29844 6d 62 65 72 20 74 68 65 20 63 75 72 72 65 6e 74  mber the current
29845 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 63 6f   column cache co
29846 6e 74 65 78 74 2e 20 20 41 6e 79 20 6e 65 77 20  ntext.  Any new 
29847 65 6e 74 72 69 65 73 20 61 64 64 65 64 0a 2a 2a  entries added.**
29848 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 63 6f   added to the co
29849 6c 75 6d 6e 20 63 61 63 68 65 20 61 66 74 65 72  lumn cache after
2984a 20 74 68 69 73 20 63 61 6c 6c 20 61 72 65 20 72   this call are r
2984b 65 6d 6f 76 65 64 20 77 68 65 6e 20 74 68 65 0a  emoved when the.
2984c 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
2984d 20 70 6f 70 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a   pop occurs..*/.
2984e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2984f 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
29850 61 63 68 65 50 75 73 68 28 50 61 72 73 65 20 2a  achePush(Parse *
29851 70 50 61 72 73 65 29 7b 0a 20 20 70 50 61 72 73  pParse){.  pPars
29852 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 2b 2b  e->iCacheLevel++
29853 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76  ;.}../*.** Remov
29854 65 20 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75 6d  e from the colum
29855 6e 20 63 61 63 68 65 20 61 6e 79 20 65 6e 74 72  n cache any entr
29856 69 65 73 20 74 68 61 74 20 77 65 72 65 20 61 64  ies that were ad
29857 64 65 64 20 73 69 6e 63 65 20 74 68 65 0a 2a 2a  ded since the.**
29858 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 4e 20   the previous N 
29859 50 75 73 68 20 6f 70 65 72 61 74 69 6f 6e 73 2e  Push operations.
2985a 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
2985b 2c 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 61  , restore the ca
2985c 63 68 65 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74  che.** to the st
2985d 61 74 65 20 69 74 20 77 61 73 20 69 6e 20 4e 20  ate it was in N 
2985e 50 75 73 68 65 73 20 61 67 6f 2e 0a 2a 2f 0a 53  Pushes ago..*/.S
2985f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
29860 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  id sqlite3ExprCa
29861 63 68 65 50 6f 70 28 50 61 72 73 65 20 2a 70 50  chePop(Parse *pP
29862 61 72 73 65 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  arse, int N){.  
29863 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
29864 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20  yColCache *p;.  
29865 61 73 73 65 72 74 28 20 4e 3e 30 20 29 3b 0a 20  assert( N>0 );. 
29866 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
29867 3e 69 43 61 63 68 65 4c 65 76 65 6c 3e 3d 4e 20  >iCacheLevel>=N 
29868 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 69 43 61  );.  pParse->iCa
29869 63 68 65 4c 65 76 65 6c 20 2d 3d 20 4e 3b 0a 20  cheLevel -= N;. 
2986a 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72   for(i=0, p=pPar
2986b 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69  se->aColCache; i
2986c 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43  <SQLITE_N_COLCAC
2986d 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  HE; i++, p++){. 
2986e 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 20 26     if( p->iReg &
2986f 26 20 70 2d 3e 69 4c 65 76 65 6c 3e 70 50 61 72  & p->iLevel>pPar
29870 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 20  se->iCacheLevel 
29871 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e  ){.      cacheEn
29872 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c  tryClear(pParse,
29873 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52   p);.      p->iR
29874 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  eg = 0;.    }.  
29875 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  }.}../*.** When 
29876 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20  a cached column 
29877 69 73 20 72 65 75 73 65 64 2c 20 6d 61 6b 65 20  is reused, make 
29878 73 75 72 65 20 74 68 61 74 20 69 74 73 20 72 65  sure that its re
29879 67 69 73 74 65 72 20 69 73 0a 2a 2a 20 6e 6f 20  gister is.** no 
2987a 6c 6f 6e 67 65 72 20 61 76 61 69 6c 61 62 6c 65  longer available
2987b 20 61 73 20 61 20 74 65 6d 70 20 72 65 67 69 73   as a temp regis
2987c 74 65 72 2e 20 20 74 69 63 6b 65 74 20 23 33 38  ter.  ticket #38
2987d 37 39 3a 20 20 74 68 61 74 20 73 61 6d 65 0a 2a  79:  that same.*
2987e 2a 20 72 65 67 69 73 74 65 72 20 6d 69 67 68 74  * register might
2987f 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 65   be in the cache
29880 20 69 6e 20 6d 75 6c 74 69 70 6c 65 20 70 6c 61   in multiple pla
29881 63 65 73 2c 20 73 6f 20 62 65 20 73 75 72 65 20  ces, so be sure 
29882 74 6f 0a 2a 2a 20 67 65 74 20 74 68 65 6d 20 61  to.** get them a
29883 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ll..*/.static vo
29884 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  id sqlite3ExprCa
29885 63 68 65 50 69 6e 52 65 67 69 73 74 65 72 28 50  chePinRegister(P
29886 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
29887 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 69  t iReg){.  int i
29888 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43  ;.  struct yColC
29889 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69  ache *p;.  for(i
2988a 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43  =0, p=pParse->aC
2988b 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54  olCache; i<SQLIT
2988c 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b  E_N_COLCACHE; i+
2988d 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28  +, p++){.    if(
2988e 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29   p->iReg==iReg )
2988f 7b 0a 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52  {.      p->tempR
29890 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  eg = 0;.    }.  
29891 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  }.}../*.** Gener
29892 61 74 65 20 63 6f 64 65 20 74 6f 20 65 78 74 72  ate code to extr
29893 61 63 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  act the value of
29894 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c   the iCol-th col
29895 75 6d 6e 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a  umn of a table..
29896 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
29897 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
29898 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 4f  prCodeGetColumnO
29899 66 54 61 62 6c 65 28 0a 20 20 56 64 62 65 20 2a  fTable(.  Vdbe *
2989a 76 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  v,        /* The
2989b 20 56 44 42 45 20 75 6e 64 65 72 20 63 6f 6e 73   VDBE under cons
2989c 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 61  truction */.  Ta
2989d 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 2f 2a  ble *pTab,    /*
2989e 20 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61   The table conta
2989f 69 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20  ining the value 
298a0 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 43 75 72  */.  int iTabCur
298a1 2c 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73  ,    /* The curs
298a2 6f 72 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c  or for this tabl
298a3 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c  e */.  int iCol,
298a4 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
298a5 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 6f  of the column to
298a6 20 65 78 74 72 61 63 74 20 2a 2f 0a 20 20 69 6e   extract */.  in
298a7 74 20 72 65 67 4f 75 74 20 20 20 20 20 20 2f 2a  t regOut      /*
298a8 20 45 78 74 72 61 63 74 20 74 68 65 20 76 61 6c   Extract the val
298a9 75 64 20 69 6e 74 6f 20 74 68 69 73 20 72 65 67  ud into this reg
298aa 69 73 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 66  ister */.){.  if
298ab 28 20 69 43 6f 6c 3c 30 20 7c 7c 20 69 43 6f 6c  ( iCol<0 || iCol
298ac 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b  ==pTab->iPKey ){
298ad 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
298ae 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
298af 69 64 2c 20 69 54 61 62 43 75 72 2c 20 72 65 67  id, iTabCur, reg
298b0 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Out);.  }else{. 
298b1 20 20 20 69 6e 74 20 6f 70 20 3d 20 49 73 56 69     int op = IsVi
298b2 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50  rtual(pTab) ? OP
298b3 5f 56 43 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f  _VColumn : OP_Co
298b4 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65  lumn;.    sqlite
298b5 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 6f  3VdbeAddOp3(v, o
298b6 70 2c 20 69 54 61 62 43 75 72 2c 20 69 43 6f 6c  p, iTabCur, iCol
298b7 2c 20 72 65 67 4f 75 74 29 3b 0a 20 20 7d 0a 20  , regOut);.  }. 
298b8 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a   if( iCol>=0 ){.
298b9 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d      sqlite3Colum
298ba 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62  nDefault(v, pTab
298bb 2c 20 69 43 6f 6c 2c 20 72 65 67 4f 75 74 29 3b  , iCol, regOut);
298bc 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  .  }.}../*.** Ge
298bd 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
298be 20 77 69 6c 6c 20 65 78 74 72 61 63 74 20 74 68   will extract th
298bf 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c  e iColumn-th col
298c0 75 6d 6e 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c  umn from.** tabl
298c1 65 20 70 54 61 62 20 61 6e 64 20 73 74 6f 72 65  e pTab and store
298c2 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75   the column valu
298c3 65 20 69 6e 20 61 20 72 65 67 69 73 74 65 72 2e  e in a register.
298c4 20 20 41 6e 20 65 66 66 6f 72 74 0a 2a 2a 20 69    An effort.** i
298c5 73 20 6d 61 64 65 20 74 6f 20 73 74 6f 72 65 20  s made to store 
298c6 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  the column value
298c7 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 52 65   in register iRe
298c8 67 2c 20 62 75 74 20 74 68 69 73 20 69 73 0a 2a  g, but this is.*
298c9 2a 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64  * not guaranteed
298ca 2e 20 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20  .  The location 
298cb 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61  of the column va
298cc 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  lue is returned.
298cd 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73  .**.** There mus
298ce 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72  t be an open cur
298cf 73 6f 72 20 74 6f 20 70 54 61 62 20 69 6e 20 69  sor to pTab in i
298d0 54 61 62 6c 65 20 77 68 65 6e 20 74 68 69 73 20  Table when this 
298d1 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61  routine.** is ca
298d2 6c 6c 65 64 2e 20 20 49 66 20 69 43 6f 6c 75 6d  lled.  If iColum
298d3 6e 3c 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73  n<0 then code is
298d4 20 67 65 6e 65 72 61 74 65 64 20 74 68 61 74 20   generated that 
298d5 65 78 74 72 61 63 74 73 20 74 68 65 20 72 6f 77  extracts the row
298d6 69 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  id..*/.SQLITE_PR
298d7 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
298d8 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75  3ExprCodeGetColu
298d9 6d 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  mn(.  Parse *pPa
298da 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 69 6e  rse,   /* Parsin
298db 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72  g and code gener
298dc 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  ating context */
298dd 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
298de 20 20 20 20 2f 2a 20 44 65 73 63 72 69 70 74 69      /* Descripti
298df 6f 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  on of the table 
298e0 77 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 66  we are reading f
298e1 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  rom */.  int iCo
298e2 6c 75 6d 6e 2c 20 20 20 20 20 2f 2a 20 49 6e 64  lumn,     /* Ind
298e3 65 78 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ex of the table 
298e4 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20  column */.  int 
298e5 69 54 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a 20  iTable,      /* 
298e6 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74  The cursor point
298e7 69 6e 67 20 74 6f 20 74 68 65 20 74 61 62 6c 65  ing to the table
298e8 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 2c 20   */.  int iReg, 
298e9 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20         /* Store 
298ea 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a  results here */.
298eb 20 20 75 38 20 70 35 20 20 20 20 20 20 20 20 20    u8 p5         
298ec 20 20 20 2f 2a 20 50 35 20 76 61 6c 75 65 20 66     /* P5 value f
298ed 6f 72 20 4f 50 5f 43 6f 6c 75 6d 6e 20 2a 2f 0a  or OP_Column */.
298ee 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
298ef 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
298f0 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
298f1 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20  yColCache *p;.. 
298f2 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72   for(i=0, p=pPar
298f3 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69  se->aColCache; i
298f4 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43  <SQLITE_N_COLCAC
298f5 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  HE; i++, p++){. 
298f6 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3e 30     if( p->iReg>0
298f7 20 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 69   && p->iTable==i
298f8 54 61 62 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c  Table && p->iCol
298f9 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a  umn==iColumn ){.
298fa 20 20 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70        p->lru = p
298fb 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74  Parse->iCacheCnt
298fc 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ++;.      sqlite
298fd 33 45 78 70 72 43 61 63 68 65 50 69 6e 52 65 67  3ExprCachePinReg
298fe 69 73 74 65 72 28 70 50 61 72 73 65 2c 20 70 2d  ister(pParse, p-
298ff 3e 69 52 65 67 29 3b 0a 20 20 20 20 20 20 72 65  >iReg);.      re
29900 74 75 72 6e 20 70 2d 3e 69 52 65 67 3b 0a 20 20  turn p->iReg;.  
29901 20 20 7d 0a 20 20 7d 20 20 0a 20 20 61 73 73 65    }.  }  .  asse
29902 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 73 71  rt( v!=0 );.  sq
29903 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74  lite3ExprCodeGet
29904 43 6f 6c 75 6d 6e 4f 66 54 61 62 6c 65 28 76 2c  ColumnOfTable(v,
29905 20 70 54 61 62 2c 20 69 54 61 62 6c 65 2c 20 69   pTab, iTable, i
29906 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20  Column, iReg);. 
29907 20 69 66 28 20 70 35 20 29 7b 0a 20 20 20 20 73   if( p5 ){.    s
29908 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
29909 50 35 28 76 2c 20 70 35 29 3b 0a 20 20 7d 65 6c  P5(v, p5);.  }el
2990a 73 65 7b 20 20 20 0a 20 20 20 20 73 71 6c 69 74  se{   .    sqlit
2990b 65 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 65  e3ExprCacheStore
2990c 28 70 50 61 72 73 65 2c 20 69 54 61 62 6c 65 2c  (pParse, iTable,
2990d 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b   iColumn, iReg);
2990e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 52  .  }.  return iR
2990f 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65  eg;.}../*.** Cle
29910 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61  ar all column ca
29911 63 68 65 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a  che entries..*/.
29912 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
29913 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
29914 61 63 68 65 43 6c 65 61 72 28 50 61 72 73 65 20  acheClear(Parse 
29915 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 20  *pParse){.  int 
29916 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  i;.  struct yCol
29917 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72  Cache *p;..  for
29918 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
29919 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
2991a 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
2991b 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
2991c 66 28 20 70 2d 3e 69 52 65 67 20 29 7b 0a 20 20  f( p->iReg ){.  
2991d 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c      cacheEntryCl
2991e 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a  ear(pParse, p);.
2991f 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20        p->iReg = 
29920 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
29921 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65  /*.** Record the
29922 20 66 61 63 74 20 74 68 61 74 20 61 6e 20 61 66   fact that an af
29923 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61  finity change ha
29924 73 20 6f 63 63 75 72 72 65 64 20 6f 6e 20 69 43  s occurred on iC
29925 6f 75 6e 74 0a 2a 2a 20 72 65 67 69 73 74 65 72  ount.** register
29926 73 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20  s starting with 
29927 69 53 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54  iStart..*/.SQLIT
29928 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
29929 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
2992a 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61  ffinityChange(Pa
2992b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
2992c 20 69 53 74 61 72 74 2c 20 69 6e 74 20 69 43 6f   iStart, int iCo
2992d 75 6e 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 45  unt){.  sqlite3E
2992e 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 70  xprCacheRemove(p
2992f 50 61 72 73 65 2c 20 69 53 74 61 72 74 2c 20 69  Parse, iStart, i
29930 43 6f 75 6e 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Count);.}../*.**
29931 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
29932 6f 20 6d 6f 76 65 20 63 6f 6e 74 65 6e 74 20 66  o move content f
29933 72 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 46  rom registers iF
29934 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67  rom...iFrom+nReg
29935 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54  -1.** over to iT
29936 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 20 4b  o..iTo+nReg-1. K
29937 65 65 70 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63  eep the column c
29938 61 63 68 65 20 75 70 2d 74 6f 2d 64 61 74 65 2e  ache up-to-date.
29939 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2993a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
2993b 78 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73  xprCodeMove(Pars
2993c 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
2993d 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69  From, int iTo, i
2993e 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20  nt nReg){.  int 
2993f 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  i;.  struct yCol
29940 43 61 63 68 65 20 2a 70 3b 0a 20 20 61 73 73 65  Cache *p;.  asse
29941 72 74 28 20 69 46 72 6f 6d 3e 3d 69 54 6f 2b 6e  rt( iFrom>=iTo+n
29942 52 65 67 20 7c 7c 20 69 46 72 6f 6d 2b 6e 52 65  Reg || iFrom+nRe
29943 67 3c 3d 69 54 6f 20 29 3b 0a 20 20 73 71 6c 69  g<=iTo );.  sqli
29944 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 50  te3VdbeAddOp3(pP
29945 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f  arse->pVdbe, OP_
29946 4d 6f 76 65 2c 20 69 46 72 6f 6d 2c 20 69 54 6f  Move, iFrom, iTo
29947 2c 20 6e 52 65 67 2d 31 29 3b 0a 20 20 66 6f 72  , nReg-1);.  for
29948 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
29949 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
2994a 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
2994b 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
2994c 6e 74 20 78 20 3d 20 70 2d 3e 69 52 65 67 3b 0a  nt x = p->iReg;.
2994d 20 20 20 20 69 66 28 20 78 3e 3d 69 46 72 6f 6d      if( x>=iFrom
2994e 20 26 26 20 78 3c 69 46 72 6f 6d 2b 6e 52 65 67   && x<iFrom+nReg
2994f 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65   ){.      p->iRe
29950 67 20 2b 3d 20 69 54 6f 2d 69 46 72 6f 6d 3b 0a  g += iTo-iFrom;.
29951 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66      }.  }.}..#if
29952 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
29953 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65  DEBUG) || define
29954 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47  d(SQLITE_COVERAG
29955 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 52 65  E_TEST)./*.** Re
29956 74 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e 79  turn true if any
29957 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65   register in the
29958 20 72 61 6e 67 65 20 69 46 72 6f 6d 2e 2e 69 54   range iFrom..iT
29959 6f 20 28 69 6e 63 6c 75 73 69 76 65 29 0a 2a 2a  o (inclusive).**
2995a 20 69 73 20 75 73 65 64 20 61 73 20 70 61 72 74   is used as part
2995b 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63   of the column c
2995c 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ache..**.** This
2995d 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
2995e 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29   within assert()
2995f 20 61 6e 64 20 74 65 73 74 63 61 73 65 28 29 20   and testcase() 
29960 6d 61 63 72 6f 73 20 6f 6e 6c 79 0a 2a 2a 20 61  macros only.** a
29961 6e 64 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 65  nd does not appe
29962 61 72 20 69 6e 20 61 20 6e 6f 72 6d 61 6c 20 62  ar in a normal b
29963 75 69 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  uild..*/.static 
29964 69 6e 74 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e  int usedAsColumn
29965 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61  Cache(Parse *pPa
29966 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20  rse, int iFrom, 
29967 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e 74 20  int iTo){.  int 
29968 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  i;.  struct yCol
29969 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28  Cache *p;.  for(
2996a 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
2996b 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
2996c 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
2996d 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e  ++, p++){.    in
2996e 74 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20  t r = p->iReg;. 
2996f 20 20 20 69 66 28 20 72 3e 3d 69 46 72 6f 6d 20     if( r>=iFrom 
29970 26 26 20 72 3c 3d 69 54 6f 20 29 20 72 65 74 75  && r<=iTo ) retu
29971 72 6e 20 31 3b 20 20 20 20 2f 2a 4e 4f 5f 54 45  rn 1;    /*NO_TE
29972 53 54 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 72  ST*/.  }.  retur
29973 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  n 0;.}.#endif /*
29974 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 7c 7c   SQLITE_DEBUG ||
29975 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45   SQLITE_COVERAGE
29976 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  _TEST */../*.** 
29977 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e  Generate code in
29978 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 56  to the current V
29979 64 62 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20  dbe to evaluate 
2997a 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70  the given.** exp
2997b 72 65 73 73 69 6f 6e 2e 20 20 41 74 74 65 6d 70  ression.  Attemp
2997c 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72  t to store the r
2997d 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74  esults in regist
2997e 65 72 20 22 74 61 72 67 65 74 22 2e 0a 2a 2a 20  er "target"..** 
2997f 52 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73  Return the regis
29980 74 65 72 20 77 68 65 72 65 20 72 65 73 75 6c 74  ter where result
29981 73 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a  s are stored..**
29982 0a 2a 2a 20 57 69 74 68 20 74 68 69 73 20 72 6f  .** With this ro
29983 75 74 69 6e 65 2c 20 74 68 65 72 65 20 69 73 20  utine, there is 
29984 6e 6f 20 67 75 61 72 61 6e 74 65 65 20 74 68 61  no guarantee tha
29985 74 20 72 65 73 75 6c 74 73 20 77 69 6c 6c 0a 2a  t results will.*
29986 2a 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74  * be stored in t
29987 61 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 75  arget.  The resu
29988 6c 74 20 6d 69 67 68 74 20 62 65 20 73 74 6f 72  lt might be stor
29989 65 64 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72  ed in some other
2998a 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 69 66 20  .** register if 
2998b 69 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74  it is convenient
2998c 20 74 6f 20 64 6f 20 73 6f 2e 20 20 54 68 65 20   to do so.  The 
2998d 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
2998e 0a 2a 2a 20 6d 75 73 74 20 63 68 65 63 6b 20 74  .** must check t
2998f 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61  he return code a
29990 6e 64 20 6d 6f 76 65 20 74 68 65 20 72 65 73 75  nd move the resu
29991 6c 74 73 20 74 6f 20 74 68 65 20 64 65 73 69 72  lts to the desir
29992 65 64 0a 2a 2a 20 72 65 67 69 73 74 65 72 2e 0a  ed.** register..
29993 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
29994 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
29995 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 73  rCodeTarget(Pars
29996 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
29997 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67  *pExpr, int targ
29998 65 74 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  et){.  Vdbe *v =
29999 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20   pParse->pVdbe; 
2999a 20 2f 2a 20 54 68 65 20 56 4d 20 75 6e 64 65 72   /* The VM under
2999b 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f   construction */
2999c 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20  .  int op;      
2999d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2999e 54 68 65 20 6f 70 63 6f 64 65 20 62 65 69 6e 67  The opcode being
2999f 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20   coded */.  int 
299a0 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 20  inReg = target; 
299a1 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73        /* Results
299a2 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
299a3 74 65 72 20 69 6e 52 65 67 20 2a 2f 0a 20 20 69  ter inReg */.  i
299a4 6e 74 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b  nt regFree1 = 0;
299a5 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e           /* If n
299a6 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 69  on-zero free thi
299a7 73 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69  s temporary regi
299a8 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ster */.  int re
299a9 67 46 72 65 65 32 20 3d 20 30 3b 20 20 20 20 20  gFree2 = 0;     
299aa 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65      /* If non-ze
299ab 72 6f 20 66 72 65 65 20 74 68 69 73 20 74 65 6d  ro free this tem
299ac 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20  porary register 
299ad 2a 2f 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 2c  */.  int r1, r2,
299ae 20 72 33 2c 20 72 34 3b 20 20 20 20 20 20 20 2f   r3, r4;       /
299af 2a 20 56 61 72 69 6f 75 73 20 72 65 67 69 73 74  * Various regist
299b0 65 72 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20  er numbers */.  
299b1 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
299b2 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 54 68 65  arse->db; /* The
299b3 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
299b4 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72  tion */..  asser
299b5 74 28 20 74 61 72 67 65 74 3e 30 20 26 26 20 74  t( target>0 && t
299b6 61 72 67 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e  arget<=pParse->n
299b7 4d 65 6d 20 29 3b 0a 20 20 69 66 28 20 76 3d 3d  Mem );.  if( v==
299b8 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
299b9 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
299ba 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
299bb 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
299bc 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29    if( pExpr==0 )
299bd 7b 0a 20 20 20 20 6f 70 20 3d 20 54 4b 5f 4e 55  {.    op = TK_NU
299be 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  LL;.  }else{.   
299bf 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b   op = pExpr->op;
299c0 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 6f  .  }.  switch( o
299c1 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b  p ){.    case TK
299c2 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20  _AGG_COLUMN: {. 
299c3 20 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 41       AggInfo *pA
299c4 67 67 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e  ggInfo = pExpr->
299c5 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pAggInfo;.      
299c6 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63  struct AggInfo_c
299c7 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26 70 41 67 67  ol *pCol = &pAgg
299c8 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70 72  Info->aCol[pExpr
299c9 2d 3e 69 41 67 67 5d 3b 0a 20 20 20 20 20 20 69  ->iAgg];.      i
299ca 66 28 20 21 70 41 67 67 49 6e 66 6f 2d 3e 64 69  f( !pAggInfo->di
299cb 72 65 63 74 4d 6f 64 65 20 29 7b 0a 20 20 20 20  rectMode ){.    
299cc 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c      assert( pCol
299cd 2d 3e 69 4d 65 6d 3e 30 20 29 3b 0a 20 20 20 20  ->iMem>0 );.    
299ce 20 20 20 20 69 6e 52 65 67 20 3d 20 70 43 6f 6c      inReg = pCol
299cf 2d 3e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20  ->iMem;.        
299d0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c  break;.      }el
299d1 73 65 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d  se if( pAggInfo-
299d2 3e 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20 29  >useSortingIdx )
299d3 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
299d4 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
299d5 50 5f 43 6f 6c 75 6d 6e 2c 20 70 41 67 67 49 6e  P_Column, pAggIn
299d6 66 6f 2d 3e 73 6f 72 74 69 6e 67 49 64 78 50 54  fo->sortingIdxPT
299d7 61 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ab,.            
299d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
299d9 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43    pCol->iSorterC
299da 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74 29 3b 0a  olumn, target);.
299db 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
299dc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20       }.      /* 
299dd 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20  Otherwise, fall 
299de 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 54 4b  thru into the TK
299df 5f 43 4f 4c 55 4d 4e 20 63 61 73 65 20 2a 2f 0a  _COLUMN case */.
299e0 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
299e1 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20  K_COLUMN: {.    
299e2 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61    if( pExpr->iTa
299e3 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  ble<0 ){.       
299e4 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 68 61   /* This only ha
299e5 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 64 69 6e  ppens when codin
299e6 67 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69  g check constrai
299e7 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 61  nts */.        a
299e8 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 63  ssert( pParse->c
299e9 6b 42 61 73 65 3e 30 20 29 3b 0a 20 20 20 20 20  kBase>0 );.     
299ea 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78 70 72     inReg = pExpr
299eb 2d 3e 69 43 6f 6c 75 6d 6e 20 2b 20 70 50 61 72  ->iColumn + pPar
299ec 73 65 2d 3e 63 6b 42 61 73 65 3b 0a 20 20 20 20  se->ckBase;.    
299ed 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
299ee 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33   inReg = sqlite3
299ef 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d  ExprCodeGetColum
299f0 6e 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  n(pParse, pExpr-
299f1 3e 70 54 61 62 2c 0a 20 20 20 20 20 20 20 20 20  >pTab,.         
299f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
299f3 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
299f4 43 6f 6c 75 6d 6e 2c 20 70 45 78 70 72 2d 3e 69  Column, pExpr->i
299f5 54 61 62 6c 65 2c 20 74 61 72 67 65 74 2c 0a 20  Table, target,. 
299f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
299f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
299f8 70 45 78 70 72 2d 3e 6f 70 32 29 3b 0a 20 20 20  pExpr->op2);.   
299f9 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
299fa 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
299fb 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20   TK_INTEGER: {. 
299fc 20 20 20 20 20 63 6f 64 65 49 6e 74 65 67 65 72       codeInteger
299fd 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
299fe 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  0, target);.    
299ff 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
29a00 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
29a01 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
29a02 54 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 4c  T.    case TK_FL
29a03 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  OAT: {.      ass
29a04 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
29a05 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
29a06 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20  IntValue) );.   
29a07 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 70     codeReal(v, p
29a08 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20  Expr->u.zToken, 
29a09 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  0, target);.    
29a0a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
29a0b 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54  endif.    case T
29a0c 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20  K_STRING: {.    
29a0d 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
29a0e 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
29a0f 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
29a10 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29a11 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
29a12 53 74 72 69 6e 67 38 2c 20 30 2c 20 74 61 72 67  String8, 0, targ
29a13 65 74 2c 20 30 2c 20 70 45 78 70 72 2d 3e 75 2e  et, 0, pExpr->u.
29a14 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20  zToken, 0);.    
29a15 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
29a16 20 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a     case TK_NULL:
29a17 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
29a18 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29a19 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74  _Null, 0, target
29a1a 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
29a1b 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
29a1c 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c  LITE_OMIT_BLOB_L
29a1d 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20  ITERAL.    case 
29a1e 54 4b 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20  TK_BLOB: {.     
29a1f 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 63 6f   int n;.      co
29a20 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20  nst char *z;.   
29a21 20 20 20 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a     char *zBlob;.
29a22 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45        assert( !E
29a23 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
29a24 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75  Expr, EP_IntValu
29a25 65 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  e) );.      asse
29a26 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f  rt( pExpr->u.zTo
29a27 6b 65 6e 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70  ken[0]=='x' || p
29a28 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30  Expr->u.zToken[0
29a29 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 20 20  ]=='X' );.      
29a2a 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75  assert( pExpr->u
29a2b 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27  .zToken[1]=='\''
29a2c 20 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 26 70   );.      z = &p
29a2d 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32  Expr->u.zToken[2
29a2e 5d 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c  ];.      n = sql
29a2f 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 20  ite3Strlen30(z) 
29a30 2d 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72  - 1;.      asser
29a31 74 28 20 7a 5b 6e 5d 3d 3d 27 5c 27 27 20 29 3b  t( z[n]=='\'' );
29a32 0a 20 20 20 20 20 20 7a 42 6c 6f 62 20 3d 20 73  .      zBlob = s
29a33 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28  qlite3HexToBlob(
29a34 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
29a35 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73  , z, n);.      s
29a36 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
29a37 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 6e 2f 32  (v, OP_Blob, n/2
29a38 2c 20 74 61 72 67 65 74 2c 20 30 2c 20 7a 42 6c  , target, 0, zBl
29a39 6f 62 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b  ob, P4_DYNAMIC);
29a3a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
29a3b 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63    }.#endif.    c
29a3c 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a  ase TK_VARIABLE:
29a3d 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
29a3e 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
29a3f 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56  y(pExpr, EP_IntV
29a40 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 61  alue) );.      a
29a41 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e  ssert( pExpr->u.
29a42 7a 54 6f 6b 65 6e 21 3d 30 20 29 3b 0a 20 20 20  zToken!=0 );.   
29a43 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
29a44 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30  ->u.zToken[0]!=0
29a45 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
29a46 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29a47 50 5f 56 61 72 69 61 62 6c 65 2c 20 70 45 78 70  P_Variable, pExp
29a48 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 74 61 72 67  r->iColumn, targ
29a49 65 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  et);.      if( p
29a4a 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31  Expr->u.zToken[1
29a4b 5d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ]!=0 ){.        
29a4c 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75  assert( pExpr->u
29a4d 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 3f 27 20  .zToken[0]=='?' 
29a4e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  .             ||
29a4f 20 73 74 72 63 6d 70 28 70 45 78 70 72 2d 3e 75   strcmp(pExpr->u
29a50 2e 7a 54 6f 6b 65 6e 2c 20 70 50 61 72 73 65 2d  .zToken, pParse-
29a51 3e 61 7a 56 61 72 5b 70 45 78 70 72 2d 3e 69 43  >azVar[pExpr->iC
29a52 6f 6c 75 6d 6e 2d 31 5d 29 3d 3d 30 20 29 3b 0a  olumn-1])==0 );.
29a53 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29a54 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
29a55 31 2c 20 70 50 61 72 73 65 2d 3e 61 7a 56 61 72  1, pParse->azVar
29a56 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2d  [pExpr->iColumn-
29a57 31 5d 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  1], P4_STATIC);.
29a58 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
29a59 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
29a5a 61 73 65 20 54 4b 5f 52 45 47 49 53 54 45 52 3a  ase TK_REGISTER:
29a5b 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d   {.      inReg =
29a5c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b 0a   pExpr->iTable;.
29a5d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
29a5e 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41   }.    case TK_A
29a5f 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 67  S: {.      inReg
29a60 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
29a61 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c  deTarget(pParse,
29a62 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74   pExpr->pLeft, t
29a63 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72  arget);.      br
29a64 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  eak;.    }.#ifnd
29a65 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
29a66 41 53 54 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  AST.    case TK_
29a67 43 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  CAST: {.      /*
29a68 20 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20   Expressions of 
29a69 74 68 65 20 66 6f 72 6d 3a 20 20 20 43 41 53 54  the form:   CAST
29a6a 28 70 4c 65 66 74 20 41 53 20 74 6f 6b 65 6e 29  (pLeft AS token)
29a6b 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 66   */.      int af
29a6c 66 2c 20 74 6f 5f 6f 70 3b 0a 20 20 20 20 20 20  f, to_op;.      
29a6d 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45  inReg = sqlite3E
29a6e 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50  xprCodeTarget(pP
29a6f 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
29a70 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  ft, target);.   
29a71 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
29a72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
29a73 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20  r, EP_IntValue) 
29a74 29 3b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 73  );.      aff = s
29a75 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79  qlite3AffinityTy
29a76 70 65 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  pe(pExpr->u.zTok
29a77 65 6e 29 3b 0a 20 20 20 20 20 20 74 6f 5f 6f 70  en);.      to_op
29a78 20 3d 20 61 66 66 20 2d 20 53 51 4c 49 54 45 5f   = aff - SQLITE_
29a79 41 46 46 5f 54 45 58 54 20 2b 20 4f 50 5f 54 6f  AFF_TEXT + OP_To
29a7a 54 65 78 74 3b 0a 20 20 20 20 20 20 61 73 73 65  Text;.      asse
29a7b 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f  rt( to_op==OP_To
29a7c 54 65 78 74 20 20 20 20 7c 7c 20 61 66 66 21 3d  Text    || aff!=
29a7d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20  SQLITE_AFF_TEXT 
29a7e 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65     );.      asse
29a7f 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f  rt( to_op==OP_To
29a80 42 6c 6f 62 20 20 20 20 7c 7c 20 61 66 66 21 3d  Blob    || aff!=
29a81 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20  SQLITE_AFF_NONE 
29a82 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65     );.      asse
29a83 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f  rt( to_op==OP_To
29a84 4e 75 6d 65 72 69 63 20 7c 7c 20 61 66 66 21 3d  Numeric || aff!=
29a85 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
29a86 49 43 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  IC );.      asse
29a87 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f  rt( to_op==OP_To
29a88 49 6e 74 20 20 20 20 20 7c 7c 20 61 66 66 21 3d  Int     || aff!=
29a89 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
29a8a 45 52 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ER );.      asse
29a8b 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f  rt( to_op==OP_To
29a8c 52 65 61 6c 20 20 20 20 7c 7c 20 61 66 66 21 3d  Real    || aff!=
29a8d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20  SQLITE_AFF_REAL 
29a8e 20 20 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74     );.      test
29a8f 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f  case( to_op==OP_
29a90 54 6f 54 65 78 74 20 29 3b 0a 20 20 20 20 20 20  ToText );.      
29a91 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d  testcase( to_op=
29a92 3d 4f 50 5f 54 6f 42 6c 6f 62 20 29 3b 0a 20 20  =OP_ToBlob );.  
29a93 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f      testcase( to
29a94 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69  _op==OP_ToNumeri
29a95 63 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  c );.      testc
29a96 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ase( to_op==OP_T
29a97 6f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 74 65  oInt );.      te
29a98 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f  stcase( to_op==O
29a99 50 5f 54 6f 52 65 61 6c 20 29 3b 0a 20 20 20 20  P_ToReal );.    
29a9a 20 20 69 66 28 20 69 6e 52 65 67 21 3d 74 61 72    if( inReg!=tar
29a9b 67 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  get ){.        s
29a9c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
29a9d 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 6e  (v, OP_SCopy, in
29a9e 52 65 67 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  Reg, target);.  
29a9f 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61        inReg = ta
29aa0 72 67 65 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rget;.      }.  
29aa1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29aa2 64 64 4f 70 31 28 76 2c 20 74 6f 5f 6f 70 2c 20  ddOp1(v, to_op, 
29aa3 69 6e 52 65 67 29 3b 0a 20 20 20 20 20 20 74 65  inReg);.      te
29aa4 73 74 63 61 73 65 28 20 75 73 65 64 41 73 43 6f  stcase( usedAsCo
29aa5 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73 65  lumnCache(pParse
29aa6 2c 20 69 6e 52 65 67 2c 20 69 6e 52 65 67 29 20  , inReg, inReg) 
29aa7 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29aa8 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
29aa9 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
29aaa 69 6e 52 65 67 2c 20 31 29 3b 0a 20 20 20 20 20  inReg, 1);.     
29aab 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
29aac 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
29aad 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 20 20 20 20  MIT_CAST */.    
29aae 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20  case TK_LT:.    
29aaf 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20  case TK_LE:.    
29ab0 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20  case TK_GT:.    
29ab1 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20  case TK_GE:.    
29ab2 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20  case TK_NE:.    
29ab3 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20  case TK_EQ: {.  
29ab4 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c      assert( TK_L
29ab5 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 20  T==OP_Lt );.    
29ab6 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d    assert( TK_LE=
29ab7 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 20  =OP_Le );.      
29ab8 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d 4f  assert( TK_GT==O
29ab9 50 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 61 73  P_Gt );.      as
29aba 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f  sert( TK_GE==OP_
29abb 47 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Ge );.      asse
29abc 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71  rt( TK_EQ==OP_Eq
29abd 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
29abe 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29  ( TK_NE==OP_Ne )
29abf 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
29ac0 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20  ( op==TK_LT );. 
29ac1 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
29ac2 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20  p==TK_LE );.    
29ac3 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
29ac4 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74  TK_GT );.      t
29ac5 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
29ac6 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  GE );.      test
29ac7 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20  case( op==TK_EQ 
29ac8 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
29ac9 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a  e( op==TK_NE );.
29aca 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
29acb 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
29acc 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
29acd 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b  eft, &regFree1);
29ace 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69  .      r2 = sqli
29acf 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
29ad0 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
29ad1 52 69 67 68 74 2c 20 26 72 65 67 46 72 65 65 32  Right, &regFree2
29ad2 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  );.      codeCom
29ad3 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78  pare(pParse, pEx
29ad4 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72  pr->pLeft, pExpr
29ad5 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20  ->pRight, op,.  
29ad6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29ad7 72 31 2c 20 72 32 2c 20 69 6e 52 65 67 2c 20 53  r1, r2, inReg, S
29ad8 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a  QLITE_STOREP2);.
29ad9 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
29ada 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
29adb 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
29adc 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20  egFree2==0 );.  
29add 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
29ade 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53 3a  .    case TK_IS:
29adf 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e  .    case TK_ISN
29ae0 4f 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  OT: {.      test
29ae1 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 20  case( op==TK_IS 
29ae2 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
29ae3 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 4f 54 20  e( op==TK_ISNOT 
29ae4 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71  );.      r1 = sq
29ae5 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
29ae6 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  p(pParse, pExpr-
29ae7 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65  >pLeft, &regFree
29ae8 31 29 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73  1);.      r2 = s
29ae9 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
29aea 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  mp(pParse, pExpr
29aeb 2d 3e 70 52 69 67 68 74 2c 20 26 72 65 67 46 72  ->pRight, &regFr
29aec 65 65 32 29 3b 0a 20 20 20 20 20 20 6f 70 20 3d  ee2);.      op =
29aed 20 28 6f 70 3d 3d 54 4b 5f 49 53 29 20 3f 20 54   (op==TK_IS) ? T
29aee 4b 5f 45 51 20 3a 20 54 4b 5f 4e 45 3b 0a 20 20  K_EQ : TK_NE;.  
29aef 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28      codeCompare(
29af0 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
29af1 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69  Left, pExpr->pRi
29af2 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20  ght, op,.       
29af3 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72             r1, r
29af4 32 2c 20 69 6e 52 65 67 2c 20 53 51 4c 49 54 45  2, inReg, SQLITE
29af5 5f 53 54 4f 52 45 50 32 20 7c 20 53 51 4c 49 54  _STOREP2 | SQLIT
29af6 45 5f 4e 55 4c 4c 45 51 29 3b 0a 20 20 20 20 20  E_NULLEQ);.     
29af7 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
29af8 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee1==0 );.      
29af9 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
29afa 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  e2==0 );.      b
29afb 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
29afc 63 61 73 65 20 54 4b 5f 41 4e 44 3a 0a 20 20 20  case TK_AND:.   
29afd 20 63 61 73 65 20 54 4b 5f 4f 52 3a 0a 20 20 20   case TK_OR:.   
29afe 20 63 61 73 65 20 54 4b 5f 50 4c 55 53 3a 0a 20   case TK_PLUS:. 
29aff 20 20 20 63 61 73 65 20 54 4b 5f 53 54 41 52 3a     case TK_STAR:
29b00 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4d 49 4e  .    case TK_MIN
29b01 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  US:.    case TK_
29b02 52 45 4d 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  REM:.    case TK
29b03 5f 42 49 54 41 4e 44 3a 0a 20 20 20 20 63 61 73  _BITAND:.    cas
29b04 65 20 54 4b 5f 42 49 54 4f 52 3a 0a 20 20 20 20  e TK_BITOR:.    
29b05 63 61 73 65 20 54 4b 5f 53 4c 41 53 48 3a 0a 20  case TK_SLASH:. 
29b06 20 20 20 63 61 73 65 20 54 4b 5f 4c 53 48 49 46     case TK_LSHIF
29b07 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52  T:.    case TK_R
29b08 53 48 49 46 54 3a 20 0a 20 20 20 20 63 61 73 65  SHIFT: .    case
29b09 20 54 4b 5f 43 4f 4e 43 41 54 3a 20 7b 0a 20 20   TK_CONCAT: {.  
29b0a 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 41      assert( TK_A
29b0b 4e 44 3d 3d 4f 50 5f 41 6e 64 20 29 3b 0a 20 20  ND==OP_And );.  
29b0c 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4f      assert( TK_O
29b0d 52 3d 3d 4f 50 5f 4f 72 20 29 3b 0a 20 20 20 20  R==OP_Or );.    
29b0e 20 20 61 73 73 65 72 74 28 20 54 4b 5f 50 4c 55    assert( TK_PLU
29b0f 53 3d 3d 4f 50 5f 41 64 64 20 29 3b 0a 20 20 20  S==OP_Add );.   
29b10 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4d 49     assert( TK_MI
29b11 4e 55 53 3d 3d 4f 50 5f 53 75 62 74 72 61 63 74  NUS==OP_Subtract
29b12 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
29b13 28 20 54 4b 5f 52 45 4d 3d 3d 4f 50 5f 52 65 6d  ( TK_REM==OP_Rem
29b14 61 69 6e 64 65 72 20 29 3b 0a 20 20 20 20 20 20  ainder );.      
29b15 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 41 4e  assert( TK_BITAN
29b16 44 3d 3d 4f 50 5f 42 69 74 41 6e 64 20 29 3b 0a  D==OP_BitAnd );.
29b17 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
29b18 5f 42 49 54 4f 52 3d 3d 4f 50 5f 42 69 74 4f 72  _BITOR==OP_BitOr
29b19 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
29b1a 28 20 54 4b 5f 53 4c 41 53 48 3d 3d 4f 50 5f 44  ( TK_SLASH==OP_D
29b1b 69 76 69 64 65 20 29 3b 0a 20 20 20 20 20 20 61  ivide );.      a
29b1c 73 73 65 72 74 28 20 54 4b 5f 4c 53 48 49 46 54  ssert( TK_LSHIFT
29b1d 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 29  ==OP_ShiftLeft )
29b1e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
29b1f 54 4b 5f 52 53 48 49 46 54 3d 3d 4f 50 5f 53 68  TK_RSHIFT==OP_Sh
29b20 69 66 74 52 69 67 68 74 20 29 3b 0a 20 20 20 20  iftRight );.    
29b21 20 20 61 73 73 65 72 74 28 20 54 4b 5f 43 4f 4e    assert( TK_CON
29b22 43 41 54 3d 3d 4f 50 5f 43 6f 6e 63 61 74 20 29  CAT==OP_Concat )
29b23 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
29b24 28 20 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a  ( op==TK_AND );.
29b25 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
29b26 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 20  op==TK_OR );.   
29b27 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
29b28 3d 54 4b 5f 50 4c 55 53 20 29 3b 0a 20 20 20 20  =TK_PLUS );.    
29b29 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
29b2a 54 4b 5f 4d 49 4e 55 53 20 29 3b 0a 20 20 20 20  TK_MINUS );.    
29b2b 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
29b2c 54 4b 5f 52 45 4d 20 29 3b 0a 20 20 20 20 20 20  TK_REM );.      
29b2d 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
29b2e 5f 42 49 54 41 4e 44 20 29 3b 0a 20 20 20 20 20  _BITAND );.     
29b2f 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
29b30 4b 5f 42 49 54 4f 52 20 29 3b 0a 20 20 20 20 20  K_BITOR );.     
29b31 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
29b32 4b 5f 53 4c 41 53 48 20 29 3b 0a 20 20 20 20 20  K_SLASH );.     
29b33 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
29b34 4b 5f 4c 53 48 49 46 54 20 29 3b 0a 20 20 20 20  K_LSHIFT );.    
29b35 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
29b36 54 4b 5f 52 53 48 49 46 54 20 29 3b 0a 20 20 20  TK_RSHIFT );.   
29b37 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
29b38 3d 54 4b 5f 43 4f 4e 43 41 54 20 29 3b 0a 20 20  =TK_CONCAT );.  
29b39 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
29b3a 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
29b3b 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
29b3c 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  t, &regFree1);. 
29b3d 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65       r2 = sqlite
29b3e 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
29b3f 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69  arse, pExpr->pRi
29b40 67 68 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b  ght, &regFree2);
29b41 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29b42 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20  beAddOp3(v, op, 
29b43 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 3b  r2, r1, target);
29b44 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
29b45 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
29b46 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
29b47 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20  regFree2==0 );. 
29b48 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29b49 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4d  }.    case TK_UM
29b4a 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 45 78  INUS: {.      Ex
29b4b 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70  pr *pLeft = pExp
29b4c 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  r->pLeft;.      
29b4d 61 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b  assert( pLeft );
29b4e 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74  .      if( pLeft
29b4f 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52  ->op==TK_INTEGER
29b50 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65   ){.        code
29b51 49 6e 74 65 67 65 72 28 70 50 61 72 73 65 2c 20  Integer(pParse, 
29b52 70 4c 65 66 74 2c 20 31 2c 20 74 61 72 67 65 74  pLeft, 1, target
29b53 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
29b54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
29b55 50 4f 49 4e 54 0a 20 20 20 20 20 20 7d 65 6c 73  POINT.      }els
29b56 65 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d  e if( pLeft->op=
29b57 3d 54 4b 5f 46 4c 4f 41 54 20 29 7b 0a 20 20 20  =TK_FLOAT ){.   
29b58 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
29b59 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
29b5a 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  xpr, EP_IntValue
29b5b 29 20 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 64  ) );.        cod
29b5c 65 52 65 61 6c 28 76 2c 20 70 4c 65 66 74 2d 3e  eReal(v, pLeft->
29b5d 75 2e 7a 54 6f 6b 65 6e 2c 20 31 2c 20 74 61 72  u.zToken, 1, tar
29b5e 67 65 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  get);.#endif.   
29b5f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
29b60 20 20 72 65 67 46 72 65 65 31 20 3d 20 72 31 20    regFree1 = r1 
29b61 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
29b62 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
29b63 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29b64 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
29b65 65 67 65 72 2c 20 30 2c 20 72 31 29 3b 0a 20 20  eger, 0, r1);.  
29b66 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74        r2 = sqlit
29b67 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
29b68 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
29b69 65 66 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b  eft, &regFree2);
29b6a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29b6b 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
29b6c 5f 53 75 62 74 72 61 63 74 2c 20 72 32 2c 20 72  _Subtract, r2, r
29b6d 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  1, target);.    
29b6e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
29b6f 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20  gFree2==0 );.   
29b70 20 20 20 7d 0a 20 20 20 20 20 20 69 6e 52 65 67     }.      inReg
29b71 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20   = target;.     
29b72 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
29b73 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4e 4f 54    case TK_BITNOT
29b74 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f  :.    case TK_NO
29b75 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  T: {.      asser
29b76 74 28 20 54 4b 5f 42 49 54 4e 4f 54 3d 3d 4f 50  t( TK_BITNOT==OP
29b77 5f 42 69 74 4e 6f 74 20 29 3b 0a 20 20 20 20 20  _BitNot );.     
29b78 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f 54 3d   assert( TK_NOT=
29b79 3d 4f 50 5f 4e 6f 74 20 29 3b 0a 20 20 20 20 20  =OP_Not );.     
29b7a 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
29b7b 4b 5f 42 49 54 4e 4f 54 20 29 3b 0a 20 20 20 20  K_BITNOT );.    
29b7c 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
29b7d 54 4b 5f 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20  TK_NOT );.      
29b7e 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r1 = sqlite3Expr
29b7f 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
29b80 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26   pExpr->pLeft, &
29b81 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20  regFree1);.     
29b82 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
29b83 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee1==0 );.      
29b84 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a  inReg = target;.
29b85 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29b86 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72  eAddOp2(v, op, r
29b87 31 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 20  1, inReg);.     
29b88 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
29b89 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c    case TK_ISNULL
29b8a 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f  :.    case TK_NO
29b8b 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 69  TNULL: {.      i
29b8c 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 61  nt addr;.      a
29b8d 73 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c  ssert( TK_ISNULL
29b8e 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20  ==OP_IsNull );. 
29b8f 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
29b90 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e  NOTNULL==OP_NotN
29b91 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ull );.      tes
29b92 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53  tcase( op==TK_IS
29b93 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65  NULL );.      te
29b94 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e  stcase( op==TK_N
29b95 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20  OTNULL );.      
29b96 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29b97 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
29b98 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20   1, target);.   
29b99 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45     r1 = sqlite3E
29b9a 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
29b9b 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
29b9c 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20  , &regFree1);.  
29b9d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
29b9e 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20  gFree1==0 );.   
29b9f 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
29ba0 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 6f  3VdbeAddOp1(v, o
29ba1 70 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  p, r1);.      sq
29ba2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29ba3 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 74 61  v, OP_AddImm, ta
29ba4 72 67 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20 20  rget, -1);.     
29ba5 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
29ba6 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20  Here(v, addr);. 
29ba7 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29ba8 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  }.    case TK_AG
29ba9 47 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20  G_FUNCTION: {.  
29baa 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e      AggInfo *pIn
29bab 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67  fo = pExpr->pAgg
29bac 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20  Info;.      if( 
29bad 70 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20  pInfo==0 ){.    
29bae 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
29baf 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
29bb0 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  pr, EP_IntValue)
29bb1 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
29bb2 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
29bb3 73 65 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61  se, "misuse of a
29bb4 67 67 72 65 67 61 74 65 3a 20 25 73 28 29 22 2c  ggregate: %s()",
29bb5 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
29bb6 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
29bb7 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20          inReg = 
29bb8 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 70 45 78  pInfo->aFunc[pEx
29bb9 70 72 2d 3e 69 41 67 67 5d 2e 69 4d 65 6d 3b 0a  pr->iAgg].iMem;.
29bba 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
29bbb 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
29bbc 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e  ase TK_CONST_FUN
29bbd 43 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46  C:.    case TK_F
29bbe 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20  UNCTION: {.     
29bbf 20 45 78 70 72 4c 69 73 74 20 2a 70 46 61 72 67   ExprList *pFarg
29bc0 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20  ;       /* List 
29bc1 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75  of function argu
29bc2 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 69  ments */.      i
29bc3 6e 74 20 6e 46 61 72 67 3b 20 20 20 20 20 20 20  nt nFarg;       
29bc4 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
29bc5 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75  of function argu
29bc6 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 46  ments */.      F
29bc7 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20  uncDef *pDef;   
29bc8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e        /* The fun
29bc9 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e  ction definition
29bca 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 20   object */.     
29bcb 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 20   int nId;       
29bcc 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
29bcd 68 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  h of the functio
29bce 6e 20 6e 61 6d 65 20 69 6e 20 62 79 74 65 73 20  n name in bytes 
29bcf 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  */.      const c
29bd0 68 61 72 20 2a 7a 49 64 3b 20 20 20 20 20 20 20  har *zId;       
29bd1 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20  /* The function 
29bd2 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e  name */.      in
29bd3 74 20 63 6f 6e 73 74 4d 61 73 6b 20 3d 20 30 3b  t constMask = 0;
29bd4 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
29bd5 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
29bd6 74 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 73  ts that are cons
29bd7 74 61 6e 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e  tant */.      in
29bd8 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
29bd9 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
29bda 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 38  nter */.      u8
29bdb 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 20   enc = ENC(db); 
29bdc 20 20 20 20 20 2f 2a 20 54 68 65 20 74 65 78 74       /* The text
29bdd 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62   encoding used b
29bde 79 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  y this database 
29bdf 2a 2f 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  */.      CollSeq
29be0 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 20 20 20 20   *pColl = 0;    
29be1 2f 2a 20 41 20 63 6f 6c 6c 61 74 69 6e 67 20 73  /* A collating s
29be2 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 20 20 20 20  equence */..    
29be3 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
29be4 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
29be5 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
29be6 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
29be7 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 53 54 5f  e( op==TK_CONST_
29be8 46 55 4e 43 20 29 3b 0a 20 20 20 20 20 20 74 65  FUNC );.      te
29be9 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 46  stcase( op==TK_F
29bea 55 4e 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20 20  UNCTION );.     
29beb 20 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50   if( ExprHasAnyP
29bec 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
29bed 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a  P_TokenOnly) ){.
29bee 20 20 20 20 20 20 20 20 70 46 61 72 67 20 3d 20          pFarg = 
29bef 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  0;.      }else{.
29bf0 20 20 20 20 20 20 20 20 70 46 61 72 67 20 3d 20          pFarg = 
29bf1 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a  pExpr->x.pList;.
29bf2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 46        }.      nF
29bf3 61 72 67 20 3d 20 70 46 61 72 67 20 3f 20 70 46  arg = pFarg ? pF
29bf4 61 72 67 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 0a  arg->nExpr : 0;.
29bf5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45        assert( !E
29bf6 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
29bf7 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75  Expr, EP_IntValu
29bf8 65 29 20 29 3b 0a 20 20 20 20 20 20 7a 49 64 20  e) );.      zId 
29bf9 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  = pExpr->u.zToke
29bfa 6e 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d 20 73  n;.      nId = s
29bfb 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
29bfc 49 64 29 3b 0a 20 20 20 20 20 20 70 44 65 66 20  Id);.      pDef 
29bfd 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e  = sqlite3FindFun
29bfe 63 74 69 6f 6e 28 64 62 2c 20 7a 49 64 2c 20 6e  ction(db, zId, n
29bff 49 64 2c 20 6e 46 61 72 67 2c 20 65 6e 63 2c 20  Id, nFarg, enc, 
29c00 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44  0);.      if( pD
29c01 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ef==0 ){.       
29c02 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
29c03 28 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77  (pParse, "unknow
29c04 6e 20 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73  n function: %.*s
29c05 28 29 22 2c 20 6e 49 64 2c 20 7a 49 64 29 3b 0a  ()", nId, zId);.
29c06 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
29c07 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
29c08 20 41 74 74 65 6d 70 74 20 61 20 64 69 72 65 63   Attempt a direc
29c09 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  t implementation
29c0a 20 6f 66 20 74 68 65 20 62 75 69 6c 74 2d 69 6e   of the built-in
29c0b 20 43 4f 41 4c 45 53 43 45 28 29 20 61 6e 64 0a   COALESCE() and.
29c0c 20 20 20 20 20 20 2a 2a 20 49 46 4e 55 4c 4c 28        ** IFNULL(
29c0d 29 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68  ) functions.  Th
29c0e 69 73 20 61 76 6f 69 64 73 20 75 6e 6e 65 63 65  is avoids unnece
29c0f 73 73 61 72 79 20 65 76 61 6c 61 74 69 6f 6e 20  ssary evalation 
29c10 6f 66 0a 20 20 20 20 20 20 2a 2a 20 61 72 67 75  of.      ** argu
29c11 6d 65 6e 74 73 20 70 61 73 74 20 74 68 65 20 66  ments past the f
29c12 69 72 73 74 20 6e 6f 6e 2d 4e 55 4c 4c 20 61 72  irst non-NULL ar
29c13 67 75 6d 65 6e 74 2e 0a 20 20 20 20 20 20 2a 2f  gument..      */
29c14 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 66 2d  .      if( pDef-
29c15 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
29c16 46 55 4e 43 5f 43 4f 41 4c 45 53 43 45 20 29 7b  FUNC_COALESCE ){
29c17 0a 20 20 20 20 20 20 20 20 69 6e 74 20 65 6e 64  .        int end
29c18 43 6f 61 6c 65 73 63 65 20 3d 20 73 71 6c 69 74  Coalesce = sqlit
29c19 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
29c1a 76 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  v);.        asse
29c1b 72 74 28 20 6e 46 61 72 67 3e 3d 32 20 29 3b 0a  rt( nFarg>=2 );.
29c1c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
29c1d 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
29c1e 70 46 61 72 67 2d 3e 61 5b 30 5d 2e 70 45 78 70  pFarg->a[0].pExp
29c1f 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  r, target);.    
29c20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6e      for(i=1; i<n
29c21 46 61 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Farg; i++){.    
29c22 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29c23 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 6f  eAddOp2(v, OP_No
29c24 74 4e 75 6c 6c 2c 20 74 61 72 67 65 74 2c 20 65  tNull, target, e
29c25 6e 64 43 6f 61 6c 65 73 63 65 29 3b 0a 20 20 20  ndCoalesce);.   
29c26 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
29c27 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 70 50  prCacheRemove(pP
29c28 61 72 73 65 2c 20 74 61 72 67 65 74 2c 20 31 29  arse, target, 1)
29c29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
29c2a 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68  te3ExprCachePush
29c2b 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
29c2c 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
29c2d 6f 64 65 28 70 50 61 72 73 65 2c 20 70 46 61 72  ode(pParse, pFar
29c2e 67 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 74  g->a[i].pExpr, t
29c2f 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20  arget);.        
29c30 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
29c31 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29  hePop(pParse, 1)
29c32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
29c33 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
29c34 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65  esolveLabel(v, e
29c35 6e 64 43 6f 61 6c 65 73 63 65 29 3b 0a 20 20 20  ndCoalesce);.   
29c36 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29c37 20 20 7d 0a 0a 0a 20 20 20 20 20 20 69 66 28 20    }...      if( 
29c38 70 46 61 72 67 20 29 7b 0a 20 20 20 20 20 20 20  pFarg ){.       
29c39 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
29c3a 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
29c3b 2c 20 6e 46 61 72 67 29 3b 0a 0a 20 20 20 20 20  , nFarg);..     
29c3c 20 20 20 2f 2a 20 46 6f 72 20 6c 65 6e 67 74 68     /* For length
29c3d 28 29 20 61 6e 64 20 74 79 70 65 6f 66 28 29 20  () and typeof() 
29c3e 66 75 6e 63 74 69 6f 6e 73 20 77 69 74 68 20 61  functions with a
29c3f 20 63 6f 6c 75 6d 6e 20 61 72 67 75 6d 65 6e 74   column argument
29c40 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74  ,.        ** set
29c41 20 74 68 65 20 50 35 20 70 61 72 61 6d 65 74 65   the P5 paramete
29c42 72 20 74 6f 20 74 68 65 20 4f 50 5f 43 6f 6c 75  r to the OP_Colu
29c43 6d 6e 20 6f 70 63 6f 64 65 20 74 6f 20 4f 50 46  mn opcode to OPF
29c44 4c 41 47 5f 4c 45 4e 47 54 48 41 52 47 0a 20 20  LAG_LENGTHARG.  
29c45 20 20 20 20 20 20 2a 2a 20 6f 72 20 4f 50 46 4c        ** or OPFL
29c46 41 47 5f 54 59 50 45 4f 46 41 52 47 20 72 65 73  AG_TYPEOFARG res
29c47 70 65 63 74 69 76 65 6c 79 2c 20 74 6f 20 61 76  pectively, to av
29c48 6f 69 64 20 75 6e 6e 65 63 65 73 73 61 72 79 20  oid unnecessary 
29c49 64 61 74 61 0a 20 20 20 20 20 20 20 20 2a 2a 20  data.        ** 
29c4a 6c 6f 61 64 69 6e 67 2e 0a 20 20 20 20 20 20 20  loading..       
29c4b 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
29c4c 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 28  (pDef->flags & (
29c4d 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 45 4e 47  SQLITE_FUNC_LENG
29c4e 54 48 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f 54  TH|SQLITE_FUNC_T
29c4f 59 50 45 4f 46 29 29 21 3d 30 20 29 7b 0a 20 20  YPEOF))!=0 ){.  
29c50 20 20 20 20 20 20 20 20 75 38 20 65 78 70 72 4f          u8 exprO
29c51 70 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  p;.          ass
29c52 65 72 74 28 20 6e 46 61 72 67 3d 3d 31 20 29 3b  ert( nFarg==1 );
29c53 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
29c54 74 28 20 70 46 61 72 67 2d 3e 61 5b 30 5d 2e 70  t( pFarg->a[0].p
29c55 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 20  Expr!=0 );.     
29c56 20 20 20 20 20 65 78 70 72 4f 70 20 3d 20 70 46       exprOp = pF
29c57 61 72 67 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d  arg->a[0].pExpr-
29c58 3e 6f 70 3b 0a 20 20 20 20 20 20 20 20 20 20 69  >op;.          i
29c59 66 28 20 65 78 70 72 4f 70 3d 3d 54 4b 5f 43 4f  f( exprOp==TK_CO
29c5a 4c 55 4d 4e 20 7c 7c 20 65 78 70 72 4f 70 3d 3d  LUMN || exprOp==
29c5b 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 7b  TK_AGG_COLUMN ){
29c5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
29c5d 65 72 74 28 20 53 51 4c 49 54 45 5f 46 55 4e 43  ert( SQLITE_FUNC
29c5e 5f 4c 45 4e 47 54 48 3d 3d 4f 50 46 4c 41 47 5f  _LENGTH==OPFLAG_
29c5f 4c 45 4e 47 54 48 41 52 47 20 29 3b 0a 20 20 20  LENGTHARG );.   
29c60 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
29c61 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 54 59 50   SQLITE_FUNC_TYP
29c62 45 4f 46 3d 3d 4f 50 46 4c 41 47 5f 54 59 50 45  EOF==OPFLAG_TYPE
29c63 4f 46 41 52 47 20 29 3b 0a 20 20 20 20 20 20 20  OFARG );.       
29c64 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
29c65 44 65 66 2d 3e 66 6c 61 67 73 3d 3d 53 51 4c 49  Def->flags==SQLI
29c66 54 45 5f 46 55 4e 43 5f 4c 45 4e 47 54 48 20 29  TE_FUNC_LENGTH )
29c67 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 46  ;.            pF
29c68 61 72 67 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d  arg->a[0].pExpr-
29c69 3e 6f 70 32 20 3d 20 70 44 65 66 2d 3e 66 6c 61  >op2 = pDef->fla
29c6a 67 73 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  gs;.          }.
29c6b 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
29c6c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
29c6d 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b  chePush(pParse);
29c6e 20 20 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 32       /* Ticket 2
29c6f 65 61 32 34 32 35 64 33 34 62 65 20 2a 2f 0a 20  ea2425d34be */. 
29c70 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
29c71 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70  prCodeExprList(p
29c72 50 61 72 73 65 2c 20 70 46 61 72 67 2c 20 72 31  Parse, pFarg, r1
29c73 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 1);.        sq
29c74 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f  lite3ExprCachePo
29c75 70 28 70 50 61 72 73 65 2c 20 31 29 3b 20 20 20  p(pParse, 1);   
29c76 2f 2a 20 54 69 63 6b 65 74 20 32 65 61 32 34 32  /* Ticket 2ea242
29c77 35 64 33 34 62 65 20 2a 2f 0a 20 20 20 20 20 20  5d34be */.      
29c78 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
29c79 31 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23  1 = 0;.      }.#
29c7a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
29c7b 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
29c7c 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c        /* Possibl
29c7d 79 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 66  y overload the f
29c7e 75 6e 63 74 69 6f 6e 20 69 66 20 74 68 65 20 66  unction if the f
29c7f 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73  irst argument is
29c80 0a 20 20 20 20 20 20 2a 2a 20 61 20 76 69 72 74  .      ** a virt
29c81 75 61 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  ual table column
29c82 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
29c83 20 2a 2a 20 46 6f 72 20 69 6e 66 69 78 20 66 75   ** For infix fu
29c84 6e 63 74 69 6f 6e 73 20 28 4c 49 4b 45 2c 20 47  nctions (LIKE, G
29c85 4c 4f 42 2c 20 52 45 47 45 58 50 2c 20 61 6e 64  LOB, REGEXP, and
29c86 20 4d 41 54 43 48 29 20 75 73 65 20 74 68 65 0a   MATCH) use the.
29c87 20 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 20        ** second 
29c88 61 72 67 75 6d 65 6e 74 2c 20 6e 6f 74 20 74 68  argument, not th
29c89 65 20 66 69 72 73 74 2c 20 61 73 20 74 68 65 20  e first, as the 
29c8a 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 65 73 74  argument to test
29c8b 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 65 65   to.      ** see
29c8c 20 69 66 20 69 74 20 69 73 20 61 20 63 6f 6c 75   if it is a colu
29c8d 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20  mn in a virtual 
29c8e 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 20  table.  This is 
29c8f 64 6f 6e 65 20 62 65 63 61 75 73 65 0a 20 20 20  done because.   
29c90 20 20 20 2a 2a 20 74 68 65 20 6c 65 66 74 20 6f     ** the left o
29c91 70 65 72 61 6e 64 20 6f 66 20 69 6e 66 69 78 20  perand of infix 
29c92 66 75 6e 63 74 69 6f 6e 73 20 28 74 68 65 20 6f  functions (the o
29c93 70 65 72 61 6e 64 20 77 65 20 77 61 6e 74 20 74  perand we want t
29c94 6f 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 72  o.      ** contr
29c95 6f 6c 20 6f 76 65 72 6c 6f 61 64 69 6e 67 29 20  ol overloading) 
29c96 65 6e 64 73 20 75 70 20 61 73 20 74 68 65 20 73  ends up as the s
29c97 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
29c98 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 66  o the.      ** f
29c99 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 65 78  unction.  The ex
29c9a 70 72 65 73 73 69 6f 6e 20 22 41 20 67 6c 6f 62  pression "A glob
29c9b 20 42 22 20 69 73 20 65 71 75 69 76 61 6c 65 6e   B" is equivalen
29c9c 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 22  t to .      ** "
29c9d 67 6c 6f 62 28 42 2c 41 29 2e 20 20 57 65 20 77  glob(B,A).  We w
29c9e 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 41  ant to use the A
29c9f 20 69 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 74   in "A glob B" t
29ca0 6f 20 74 65 73 74 0a 20 20 20 20 20 20 2a 2a 20  o test.      ** 
29ca1 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 6f 76 65  for function ove
29ca2 72 6c 6f 61 64 69 6e 67 2e 20 20 42 75 74 20 77  rloading.  But w
29ca3 65 20 75 73 65 20 74 68 65 20 42 20 74 65 72 6d  e use the B term
29ca4 20 69 6e 20 22 67 6c 6f 62 28 42 2c 41 29 22 2e   in "glob(B,A)".
29ca5 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
29ca6 69 66 28 20 6e 46 61 72 67 3e 3d 32 20 26 26 20  if( nFarg>=2 && 
29ca7 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20  (pExpr->flags & 
29ca8 45 50 5f 49 6e 66 69 78 46 75 6e 63 29 20 29 7b  EP_InfixFunc) ){
29ca9 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20  .        pDef = 
29caa 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c  sqlite3VtabOverl
29cab 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20  oadFunction(db, 
29cac 70 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 61  pDef, nFarg, pFa
29cad 72 67 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 29 3b  rg->a[1].pExpr);
29cae 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
29caf 20 6e 46 61 72 67 3e 30 20 29 7b 0a 20 20 20 20   nFarg>0 ){.    
29cb0 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74      pDef = sqlit
29cb1 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75  e3VtabOverloadFu
29cb2 6e 63 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 2c  nction(db, pDef,
29cb3 20 6e 46 61 72 67 2c 20 70 46 61 72 67 2d 3e 61   nFarg, pFarg->a
29cb4 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20  [0].pExpr);.    
29cb5 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
29cb6 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 61 72   for(i=0; i<nFar
29cb7 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  g; i++){.       
29cb8 20 69 66 28 20 69 3c 33 32 20 26 26 20 73 71 6c   if( i<32 && sql
29cb9 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
29cba 6e 74 28 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70  nt(pFarg->a[i].p
29cbb 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20  Expr) ){.       
29cbc 20 20 20 63 6f 6e 73 74 4d 61 73 6b 20 7c 3d 20     constMask |= 
29cbd 28 31 3c 3c 69 29 3b 0a 20 20 20 20 20 20 20 20  (1<<i);.        
29cbe 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 70  }.        if( (p
29cbf 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  Def->flags & SQL
29cc0 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c  ITE_FUNC_NEEDCOL
29cc1 4c 29 21 3d 30 20 26 26 20 21 70 43 6f 6c 6c 20  L)!=0 && !pColl 
29cc2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f  ){.          pCo
29cc3 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ll = sqlite3Expr
29cc4 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
29cc5 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78 70  pFarg->a[i].pExp
29cc6 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  r);.        }.  
29cc7 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
29cc8 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51  pDef->flags & SQ
29cc9 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f  LITE_FUNC_NEEDCO
29cca 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  LL ){.        if
29ccb 28 20 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c  ( !pColl ) pColl
29ccc 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c   = db->pDfltColl
29ccd 3b 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ; .        sqlit
29cce 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
29ccf 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30  OP_CollSeq, 0, 0
29cd0 2c 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43 6f  , 0, (char *)pCo
29cd1 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b  ll, P4_COLLSEQ);
29cd2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
29cd3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
29cd4 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c  (v, OP_Function,
29cd5 20 63 6f 6e 73 74 4d 61 73 6b 2c 20 72 31 2c 20   constMask, r1, 
29cd6 74 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20 20  target,.        
29cd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29cd8 28 63 68 61 72 2a 29 70 44 65 66 2c 20 50 34 5f  (char*)pDef, P4_
29cd9 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 20 20  FUNCDEF);.      
29cda 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
29cdb 65 50 35 28 76 2c 20 28 75 38 29 6e 46 61 72 67  eP5(v, (u8)nFarg
29cdc 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 46 61  );.      if( nFa
29cdd 72 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  rg ){.        sq
29cde 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
29cdf 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31  Range(pParse, r1
29ce0 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20  , nFarg);.      
29ce1 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
29ce2 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
29ce3 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
29ce4 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58  Y.    case TK_EX
29ce5 49 53 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54  ISTS:.    case T
29ce6 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20  K_SELECT: {.    
29ce7 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
29ce8 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 20 20 20  TK_EXISTS );.   
29ce9 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
29cea 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20  =TK_SELECT );.  
29ceb 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69      inReg = sqli
29cec 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74  te3CodeSubselect
29ced 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
29cee 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65  0, 0);.      bre
29cef 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
29cf0 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20  se TK_IN: {.    
29cf1 20 20 69 6e 74 20 64 65 73 74 49 66 46 61 6c 73    int destIfFals
29cf2 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  e = sqlite3VdbeM
29cf3 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
29cf4 20 20 20 69 6e 74 20 64 65 73 74 49 66 4e 75 6c     int destIfNul
29cf5 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  l = sqlite3VdbeM
29cf6 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
29cf7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29cf8 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
29cf9 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20   0, target);.   
29cfa 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
29cfb 64 65 49 4e 28 70 50 61 72 73 65 2c 20 70 45 78  deIN(pParse, pEx
29cfc 70 72 2c 20 64 65 73 74 49 66 46 61 6c 73 65 2c  pr, destIfFalse,
29cfd 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a 20 20   destIfNull);.  
29cfe 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29cff 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
29d00 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29 3b  ger, 1, target);
29d01 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29d02 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
29d03 2c 20 64 65 73 74 49 66 46 61 6c 73 65 29 3b 0a  , destIfFalse);.
29d04 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29d05 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64  eAddOp2(v, OP_Ad
29d06 64 49 6d 6d 2c 20 74 61 72 67 65 74 2c 20 30 29  dImm, target, 0)
29d07 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29d08 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
29d09 76 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a  v, destIfNull);.
29d0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
29d0b 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
29d0c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
29d0d 59 20 2a 2f 0a 0a 0a 20 20 20 20 2f 2a 0a 20 20  Y */...    /*.  
29d0e 20 20 2a 2a 20 20 20 20 78 20 42 45 54 57 45 45    **    x BETWEE
29d0f 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 2a 2a  N y AND z.    **
29d10 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20  .    ** This is 
29d11 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a 20 20  equivalent to.  
29d12 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78    **.    **    x
29d13 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20  >=y AND x<=z.   
29d14 20 2a 2a 0a 20 20 20 20 2a 2a 20 58 20 69 73 20   **.    ** X is 
29d15 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d  stored in pExpr-
29d16 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59  >pLeft..    ** Y
29d17 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45   is stored in pE
29d18 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d  xpr->pList->a[0]
29d19 2e 70 45 78 70 72 2e 0a 20 20 20 20 2a 2a 20 5a  .pExpr..    ** Z
29d1a 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45   is stored in pE
29d1b 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d  xpr->pList->a[1]
29d1c 2e 70 45 78 70 72 2e 0a 20 20 20 20 2a 2f 0a 20  .pExpr..    */. 
29d1d 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45     case TK_BETWE
29d1e 45 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72  EN: {.      Expr
29d1f 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d   *pLeft = pExpr-
29d20 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 73 74  >pLeft;.      st
29d21 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
29d22 65 6d 20 2a 70 4c 49 74 65 6d 20 3d 20 70 45 78  em *pLItem = pEx
29d23 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 3b 0a  pr->x.pList->a;.
29d24 20 20 20 20 20 20 45 78 70 72 20 2a 70 52 69 67        Expr *pRig
29d25 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78  ht = pLItem->pEx
29d26 70 72 3b 0a 0a 20 20 20 20 20 20 72 31 20 3d 20  pr;..      r1 = 
29d27 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
29d28 65 6d 70 28 70 50 61 72 73 65 2c 20 70 4c 65 66  emp(pParse, pLef
29d29 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  t, &regFree1);. 
29d2a 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65       r2 = sqlite
29d2b 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
29d2c 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 26 72  arse, pRight, &r
29d2d 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20  egFree2);.      
29d2e 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
29d2f 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74  e1==0 );.      t
29d30 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
29d31 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 33  2==0 );.      r3
29d32 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
29d33 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
29d34 20 20 20 20 72 34 20 3d 20 73 71 6c 69 74 65 33      r4 = sqlite3
29d35 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
29d36 65 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f  e);.      codeCo
29d37 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c  mpare(pParse, pL
29d38 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f  eft, pRight, OP_
29d39 47 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  Ge,.            
29d3a 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 72 33        r1, r2, r3
29d3b 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32  , SQLITE_STOREP2
29d3c 29 3b 0a 20 20 20 20 20 20 70 4c 49 74 65 6d 2b  );.      pLItem+
29d3d 2b 3b 0a 20 20 20 20 20 20 70 52 69 67 68 74 20  +;.      pRight 
29d3e 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b  = pLItem->pExpr;
29d3f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
29d40 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
29d41 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b 0a  rse, regFree2);.
29d42 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74        r2 = sqlit
29d43 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
29d44 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 26  Parse, pRight, &
29d45 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20  regFree2);.     
29d46 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
29d47 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee2==0 );.      
29d48 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72  codeCompare(pPar
29d49 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  se, pLeft, pRigh
29d4a 74 2c 20 4f 50 5f 4c 65 2c 20 72 31 2c 20 72 32  t, OP_Le, r1, r2
29d4b 2c 20 72 34 2c 20 53 51 4c 49 54 45 5f 53 54 4f  , r4, SQLITE_STO
29d4c 52 45 50 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  REP2);.      sql
29d4d 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
29d4e 2c 20 4f 50 5f 41 6e 64 2c 20 72 33 2c 20 72 34  , OP_And, r3, r4
29d4f 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
29d50 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
29d51 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
29d52 33 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  3);.      sqlite
29d53 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
29d54 70 50 61 72 73 65 2c 20 72 34 29 3b 0a 20 20 20  pParse, r4);.   
29d55 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
29d56 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50 4c 55      case TK_UPLU
29d57 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 67  S: {.      inReg
29d58 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
29d59 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c  deTarget(pParse,
29d5a 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74   pExpr->pLeft, t
29d5b 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72  arget);.      br
29d5c 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
29d5d 63 61 73 65 20 54 4b 5f 54 52 49 47 47 45 52 3a  case TK_TRIGGER:
29d5e 20 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74   {.      /* If t
29d5f 68 65 20 6f 70 63 6f 64 65 20 69 73 20 54 4b 5f  he opcode is TK_
29d60 54 52 49 47 47 45 52 2c 20 74 68 65 6e 20 74 68  TRIGGER, then th
29d61 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
29d62 61 20 72 65 66 65 72 65 6e 63 65 0a 20 20 20 20  a reference.    
29d63 20 20 2a 2a 20 74 6f 20 61 20 63 6f 6c 75 6d 6e    ** to a column
29d64 20 69 6e 20 74 68 65 20 6e 65 77 2e 2a 20 6f 72   in the new.* or
29d65 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61   old.* pseudo-ta
29d66 62 6c 65 73 20 61 76 61 69 6c 61 62 6c 65 20 74  bles available t
29d67 6f 0a 20 20 20 20 20 20 2a 2a 20 74 72 69 67 67  o.      ** trigg
29d68 65 72 20 70 72 6f 67 72 61 6d 73 2e 20 49 6e 20  er programs. In 
29d69 74 68 69 73 20 63 61 73 65 20 45 78 70 72 2e 69  this case Expr.i
29d6a 54 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20  Table is set to 
29d6b 31 20 66 6f 72 20 74 68 65 0a 20 20 20 20 20 20  1 for the.      
29d6c 2a 2a 20 6e 65 77 2e 2a 20 70 73 65 75 64 6f 2d  ** new.* pseudo-
29d6d 74 61 62 6c 65 2c 20 6f 72 20 30 20 66 6f 72 20  table, or 0 for 
29d6e 74 68 65 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f  the old.* pseudo
29d6f 2d 74 61 62 6c 65 2e 20 45 78 70 72 2e 69 43 6f  -table. Expr.iCo
29d70 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73  lumn.      ** is
29d71 20 73 65 74 20 74 6f 20 74 68 65 20 63 6f 6c 75   set to the colu
29d72 6d 6e 20 6f 66 20 74 68 65 20 70 73 65 75 64 6f  mn of the pseudo
29d73 2d 74 61 62 6c 65 20 74 6f 20 72 65 61 64 2c 20  -table to read, 
29d74 6f 72 20 74 6f 20 2d 31 20 74 6f 0a 20 20 20 20  or to -1 to.    
29d75 20 20 2a 2a 20 72 65 61 64 20 74 68 65 20 72 6f    ** read the ro
29d76 77 69 64 20 66 69 65 6c 64 2e 0a 20 20 20 20 20  wid field..     
29d77 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65   **.      ** The
29d78 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 69   expression is i
29d79 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67  mplemented using
29d7a 20 61 6e 20 4f 50 5f 50 61 72 61 6d 20 6f 70 63   an OP_Param opc
29d7b 6f 64 65 2e 20 54 68 65 20 70 31 0a 20 20 20 20  ode. The p1.    
29d7c 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69    ** parameter i
29d7d 73 20 73 65 74 20 74 6f 20 30 20 66 6f 72 20 61  s set to 0 for a
29d7e 6e 20 6f 6c 64 2e 72 6f 77 69 64 20 72 65 66 65  n old.rowid refe
29d7f 72 65 6e 63 65 2c 20 6f 72 20 74 6f 20 28 69 2b  rence, or to (i+
29d80 31 29 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 72  1).      ** to r
29d81 65 66 65 72 65 6e 63 65 20 61 6e 6f 74 68 65 72  eference another
29d82 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 6f   column of the o
29d83 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c  ld.* pseudo-tabl
29d84 65 2c 20 77 68 65 72 65 20 0a 20 20 20 20 20 20  e, where .      
29d85 2a 2a 20 69 20 69 73 20 74 68 65 20 69 6e 64 65  ** i is the inde
29d86 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e  x of the column.
29d87 20 46 6f 72 20 61 20 6e 65 77 2e 72 6f 77 69 64   For a new.rowid
29d88 20 72 65 66 65 72 65 6e 63 65 2c 20 70 31 20 69   reference, p1 i
29d89 73 0a 20 20 20 20 20 20 2a 2a 20 73 65 74 20 74  s.      ** set t
29d8a 6f 20 28 6e 2b 31 29 2c 20 77 68 65 72 65 20 6e  o (n+1), where n
29d8b 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
29d8c 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 65 61 63  f columns in eac
29d8d 68 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a  h pseudo-table..
29d8e 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 61 20 72        ** For a r
29d8f 65 66 65 72 65 6e 63 65 20 74 6f 20 61 6e 79 20  eference to any 
29d90 6f 74 68 65 72 20 63 6f 6c 75 6d 6e 20 69 6e 20  other column in 
29d91 74 68 65 20 6e 65 77 2e 2a 20 70 73 65 75 64 6f  the new.* pseudo
29d92 2d 74 61 62 6c 65 2c 20 70 31 0a 20 20 20 20 20  -table, p1.     
29d93 20 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 28 6e   ** is set to (n
29d94 2b 32 2b 69 29 2c 20 77 68 65 72 65 20 6e 20 61  +2+i), where n a
29d95 6e 64 20 69 20 61 72 65 20 61 73 20 64 65 66 69  nd i are as defi
29d96 6e 65 64 20 70 72 65 76 69 6f 75 73 6c 79 2e 20  ned previously. 
29d97 46 6f 72 0a 20 20 20 20 20 20 2a 2a 20 65 78 61  For.      ** exa
29d98 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 74 61 62  mple, if the tab
29d99 6c 65 20 6f 6e 20 77 68 69 63 68 20 74 72 69 67  le on which trig
29d9a 67 65 72 73 20 61 72 65 20 62 65 69 6e 67 20 66  gers are being f
29d9b 69 72 65 64 20 69 73 0a 20 20 20 20 20 20 2a 2a  ired is.      **
29d9c 20 64 65 63 6c 61 72 65 64 20 61 73 3a 0a 20 20   declared as:.  
29d9d 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
29d9e 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
29d9f 31 28 61 2c 20 62 29 3b 0a 20 20 20 20 20 20 2a  1(a, b);.      *
29da0 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 6e 20  *.      ** Then 
29da1 70 31 20 69 73 20 69 6e 74 65 72 70 72 65 74 65  p1 is interprete
29da2 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20  d as follows:.  
29da3 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
29da4 20 20 70 31 3d 3d 30 20 20 20 2d 3e 20 20 20 20    p1==0   ->    
29da5 6f 6c 64 2e 72 6f 77 69 64 20 20 20 20 20 70 31  old.rowid     p1
29da6 3d 3d 33 20 20 20 2d 3e 20 20 20 20 6e 65 77 2e  ==3   ->    new.
29da7 72 6f 77 69 64 0a 20 20 20 20 20 20 2a 2a 20 20  rowid.      **  
29da8 20 70 31 3d 3d 31 20 20 20 2d 3e 20 20 20 20 6f   p1==1   ->    o
29da9 6c 64 2e 61 20 20 20 20 20 20 20 20 20 70 31 3d  ld.a         p1=
29daa 3d 34 20 20 20 2d 3e 20 20 20 20 6e 65 77 2e 61  =4   ->    new.a
29dab 0a 20 20 20 20 20 20 2a 2a 20 20 20 70 31 3d 3d  .      **   p1==
29dac 32 20 20 20 2d 3e 20 20 20 20 6f 6c 64 2e 62 20  2   ->    old.b 
29dad 20 20 20 20 20 20 20 20 70 31 3d 3d 35 20 20 20          p1==5   
29dae 2d 3e 20 20 20 20 6e 65 77 2e 62 20 20 20 20 20  ->    new.b     
29daf 20 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20    .      */.    
29db0 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
29db1 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20  pExpr->pTab;.   
29db2 20 20 20 69 6e 74 20 70 31 20 3d 20 70 45 78 70     int p1 = pExp
29db3 72 2d 3e 69 54 61 62 6c 65 20 2a 20 28 70 54 61  r->iTable * (pTa
29db4 62 2d 3e 6e 43 6f 6c 2b 31 29 20 2b 20 31 20 2b  b->nCol+1) + 1 +
29db5 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b   pExpr->iColumn;
29db6 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
29db7 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 30  pExpr->iTable==0
29db8 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c   || pExpr->iTabl
29db9 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 61 73  e==1 );.      as
29dba 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f  sert( pExpr->iCo
29dbb 6c 75 6d 6e 3e 3d 2d 31 20 26 26 20 70 45 78 70  lumn>=-1 && pExp
29dbc 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d  r->iColumn<pTab-
29dbd 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 20 20 61  >nCol );.      a
29dbe 73 73 65 72 74 28 20 70 54 61 62 2d 3e 69 50 4b  ssert( pTab->iPK
29dbf 65 79 3c 30 20 7c 7c 20 70 45 78 70 72 2d 3e 69  ey<0 || pExpr->i
29dc0 43 6f 6c 75 6d 6e 21 3d 70 54 61 62 2d 3e 69 50  Column!=pTab->iP
29dc1 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 61 73 73  Key );.      ass
29dc2 65 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70 31  ert( p1>=0 && p1
29dc3 3c 28 70 54 61 62 2d 3e 6e 43 6f 6c 2a 32 2b 32  <(pTab->nCol*2+2
29dc4 29 20 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69  ) );..      sqli
29dc5 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29dc6 20 4f 50 5f 50 61 72 61 6d 2c 20 70 31 2c 20 74   OP_Param, p1, t
29dc7 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 56 64  arget);.      Vd
29dc8 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25  beComment((v, "%
29dc9 73 2e 25 73 20 2d 3e 20 24 25 64 22 2c 0a 20 20  s.%s -> $%d",.  
29dca 20 20 20 20 20 20 28 70 45 78 70 72 2d 3e 69 54        (pExpr->iT
29dcb 61 62 6c 65 20 3f 20 22 6e 65 77 22 20 3a 20 22  able ? "new" : "
29dcc 6f 6c 64 22 29 2c 0a 20 20 20 20 20 20 20 20 28  old"),.        (
29dcd 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 30  pExpr->iColumn<0
29dce 20 3f 20 22 72 6f 77 69 64 22 20 3a 20 70 45 78   ? "rowid" : pEx
29dcf 70 72 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 70  pr->pTab->aCol[p
29dd0 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 7a  Expr->iColumn].z
29dd1 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 74  Name),.        t
29dd2 61 72 67 65 74 0a 20 20 20 20 20 20 29 29 3b 0a  arget.      ));.
29dd3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
29dd4 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
29dd5 49 4e 54 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  INT.      /* If 
29dd6 74 68 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20 52  the column has R
29dd7 45 41 4c 20 61 66 66 69 6e 69 74 79 2c 20 69 74  EAL affinity, it
29dd8 20 6d 61 79 20 63 75 72 72 65 6e 74 6c 79 20 62   may currently b
29dd9 65 20 73 74 6f 72 65 64 20 61 73 20 61 6e 0a 20  e stored as an. 
29dda 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e       ** integer.
29ddb 20 55 73 65 20 4f 50 5f 52 65 61 6c 41 66 66 69   Use OP_RealAffi
29ddc 6e 69 74 79 20 74 6f 20 6d 61 6b 65 20 73 75 72  nity to make sur
29ddd 65 20 69 74 20 69 73 20 72 65 61 6c 6c 79 20 72  e it is really r
29dde 65 61 6c 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69  eal.  */.      i
29ddf 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  f( pExpr->iColum
29de0 6e 3e 3d 30 20 0a 20 20 20 20 20 20 20 26 26 20  n>=0 .       && 
29de1 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 72  pTab->aCol[pExpr
29de2 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e  ->iColumn].affin
29de3 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity==SQLITE_AFF_
29de4 52 45 41 4c 0a 20 20 20 20 20 20 29 7b 0a 20 20  REAL.      ){.  
29de5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29de6 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65  eAddOp1(v, OP_Re
29de7 61 6c 41 66 66 69 6e 69 74 79 2c 20 74 61 72 67  alAffinity, targ
29de8 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  et);.      }.#en
29de9 64 69 66 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  dif.      break;
29dea 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a 0a  .    }...    /*.
29deb 20 20 20 20 2a 2a 20 46 6f 72 6d 20 41 3a 0a 20      ** Form A:. 
29dec 20 20 20 2a 2a 20 20 20 43 41 53 45 20 78 20 57     **   CASE x W
29ded 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31 20 57  HEN e1 THEN r1 W
29dee 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32 20 2e  HEN e2 THEN r2 .
29def 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 4e 20  .. WHEN eN THEN 
29df0 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20  rN ELSE y END.  
29df1 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d    **.    ** Form
29df2 20 42 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53   B:.    **   CAS
29df3 45 20 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72  E WHEN e1 THEN r
29df4 31 20 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72  1 WHEN e2 THEN r
29df5 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48  2 ... WHEN eN TH
29df6 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44  EN rN ELSE y END
29df7 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46  .    **.    ** F
29df8 6f 72 6d 20 41 20 69 73 20 63 61 6e 20 62 65 20  orm A is can be 
29df9 74 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f  transformed into
29dfa 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20   the equivalent 
29dfb 66 6f 72 6d 20 42 20 61 73 20 66 6f 6c 6c 6f 77  form B as follow
29dfc 73 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45  s:.    **   CASE
29dfd 20 57 48 45 4e 20 78 3d 65 31 20 54 48 45 4e 20   WHEN x=e1 THEN 
29dfe 72 31 20 57 48 45 4e 20 78 3d 65 32 20 54 48 45  r1 WHEN x=e2 THE
29dff 4e 20 72 32 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20  N r2 ....    ** 
29e00 20 20 20 20 20 20 20 57 48 45 4e 20 78 3d 65 4e         WHEN x=eN
29e01 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20   THEN rN ELSE y 
29e02 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  END.    **.    *
29e03 2a 20 58 20 28 69 66 20 69 74 20 65 78 69 73 74  * X (if it exist
29e04 73 29 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e  s) is in pExpr->
29e05 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20  pLeft..    ** Y 
29e06 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 52 69  is in pExpr->pRi
29e07 67 68 74 2e 20 20 54 68 65 20 59 20 69 73 20 61  ght.  The Y is a
29e08 6c 73 6f 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49  lso optional.  I
29e09 66 20 74 68 65 72 65 20 69 73 20 6e 6f 0a 20 20  f there is no.  
29e0a 20 20 2a 2a 20 45 4c 53 45 20 63 6c 61 75 73 65    ** ELSE clause
29e0b 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 74 65   and no other te
29e0c 72 6d 20 6d 61 74 63 68 65 73 2c 20 74 68 65 6e  rm matches, then
29e0d 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
29e0e 68 65 0a 20 20 20 20 2a 2a 20 65 78 70 72 73 73  he.    ** exprss
29e0f 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 20 20 20  ion is NULL..   
29e10 20 2a 2a 20 45 69 20 69 73 20 69 6e 20 70 45 78   ** Ei is in pEx
29e11 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32  pr->pList->a[i*2
29e12 5d 20 61 6e 64 20 52 69 20 69 73 20 70 45 78 70  ] and Ri is pExp
29e13 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 2b  r->pList->a[i*2+
29e14 31 5d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  1]..    **.    *
29e15 2a 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20  * The result of 
29e16 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
29e17 73 20 74 68 65 20 52 69 20 66 6f 72 20 74 68 65  s the Ri for the
29e18 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20   first matching 
29e19 45 69 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69 66  Ei,.    ** or if
29e1a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74   there is no mat
29e1b 63 68 69 6e 67 20 45 69 2c 20 74 68 65 20 45 4c  ching Ei, the EL
29e1c 53 45 20 74 65 72 6d 20 59 2c 20 6f 72 20 69 66  SE term Y, or if
29e1d 20 74 68 65 72 65 20 69 73 0a 20 20 20 20 2a 2a   there is.    **
29e1e 20 6e 6f 20 45 4c 53 45 20 74 65 72 6d 2c 20 4e   no ELSE term, N
29e1f 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ULL..    */.    
29e20 64 65 66 61 75 6c 74 3a 20 61 73 73 65 72 74 28  default: assert(
29e21 20 6f 70 3d 3d 54 4b 5f 43 41 53 45 20 29 3b 20   op==TK_CASE ); 
29e22 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 6e 64 4c  {.      int endL
29e23 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20  abel;           
29e24 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 4f 54            /* GOT
29e25 4f 20 6c 61 62 65 6c 20 66 6f 72 20 65 6e 64 20  O label for end 
29e26 6f 66 20 43 41 53 45 20 73 74 6d 74 20 2a 2f 0a  of CASE stmt */.
29e27 20 20 20 20 20 20 69 6e 74 20 6e 65 78 74 43 61        int nextCa
29e28 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  se;             
29e29 20 20 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20          /* GOTO 
29e2a 6c 61 62 65 6c 20 66 6f 72 20 6e 65 78 74 20 57  label for next W
29e2b 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  HEN clause */.  
29e2c 20 20 20 20 69 6e 74 20 6e 45 78 70 72 3b 20 20      int nExpr;  
29e2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29e2e 20 20 20 20 20 20 2f 2a 20 32 78 20 6e 75 6d 62        /* 2x numb
29e2f 65 72 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73  er of WHEN terms
29e30 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   */.      int i;
29e31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29e32 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
29e33 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
29e34 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70       ExprList *p
29e35 45 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  EList;          
29e36 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
29e37 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a  f WHEN terms */.
29e38 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70        struct Exp
29e39 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 4c 69 73  rList_item *aLis
29e3a 74 65 6c 65 6d 3b 20 20 2f 2a 20 41 72 72 61 79  telem;  /* Array
29e3b 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a   of WHEN terms *
29e3c 2f 0a 20 20 20 20 20 20 45 78 70 72 20 6f 70 43  /.      Expr opC
29e3d 6f 6d 70 61 72 65 3b 20 20 20 20 20 20 20 20 20  ompare;         
29e3e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
29e3f 20 58 3d 3d 45 69 20 65 78 70 72 65 73 73 69 6f   X==Ei expressio
29e40 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20  n */.      Expr 
29e41 63 61 63 68 65 58 3b 20 20 20 20 20 20 20 20 20  cacheX;         
29e42 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29e43 43 61 63 68 65 64 20 65 78 70 72 65 73 73 69 6f  Cached expressio
29e44 6e 20 58 20 2a 2f 0a 20 20 20 20 20 20 45 78 70  n X */.      Exp
29e45 72 20 2a 70 58 3b 20 20 20 20 20 20 20 20 20 20  r *pX;          
29e46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
29e47 2a 20 54 68 65 20 58 20 65 78 70 72 65 73 73 69  * The X expressi
29e48 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  on */.      Expr
29e49 20 2a 70 54 65 73 74 20 3d 20 30 3b 20 20 20 20   *pTest = 0;    
29e4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
29e4b 20 58 3d 3d 45 69 20 28 66 6f 72 6d 20 41 29 20   X==Ei (form A) 
29e4c 6f 72 20 6a 75 73 74 20 45 69 20 28 66 6f 72 6d  or just Ei (form
29e4d 20 42 29 20 2a 2f 0a 20 20 20 20 20 20 56 56 41   B) */.      VVA
29e4e 5f 4f 4e 4c 59 28 20 69 6e 74 20 69 43 61 63 68  _ONLY( int iCach
29e4f 65 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d  eLevel = pParse-
29e50 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 20 29 0a  >iCacheLevel; ).
29e51 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
29e52 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
29e53 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
29e54 65 63 74 29 20 26 26 20 70 45 78 70 72 2d 3e 78  ect) && pExpr->x
29e55 2e 70 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20  .pList );.      
29e56 61 73 73 65 72 74 28 28 70 45 78 70 72 2d 3e 78  assert((pExpr->x
29e57 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 25 20  .pList->nExpr % 
29e58 32 29 20 3d 3d 20 30 29 3b 0a 20 20 20 20 20 20  2) == 0);.      
29e59 61 73 73 65 72 74 28 70 45 78 70 72 2d 3e 78 2e  assert(pExpr->x.
29e5a 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3e 20 30  pList->nExpr > 0
29e5b 29 3b 0a 20 20 20 20 20 20 70 45 4c 69 73 74 20  );.      pEList 
29e5c 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
29e5d 3b 0a 20 20 20 20 20 20 61 4c 69 73 74 65 6c 65  ;.      aListele
29e5e 6d 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a 20  m = pEList->a;. 
29e5f 20 20 20 20 20 6e 45 78 70 72 20 3d 20 70 45 4c       nExpr = pEL
29e60 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  ist->nExpr;.    
29e61 20 20 65 6e 64 4c 61 62 65 6c 20 3d 20 73 71 6c    endLabel = sql
29e62 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
29e63 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 66 28 20  l(v);.      if( 
29e64 28 70 58 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65  (pX = pExpr->pLe
29e65 66 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ft)!=0 ){.      
29e66 20 20 63 61 63 68 65 58 20 3d 20 2a 70 58 3b 0a    cacheX = *pX;.
29e67 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
29e68 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  ( pX->op==TK_COL
29e69 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 74  UMN );.        t
29e6a 65 73 74 63 61 73 65 28 20 70 58 2d 3e 6f 70 3d  estcase( pX->op=
29e6b 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b 0a  =TK_REGISTER );.
29e6c 20 20 20 20 20 20 20 20 63 61 63 68 65 58 2e 69          cacheX.i
29e6d 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45  Table = sqlite3E
29e6e 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
29e6f 73 65 2c 20 70 58 2c 20 26 72 65 67 46 72 65 65  se, pX, &regFree
29e70 31 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74  1);.        test
29e71 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
29e72 30 20 29 3b 0a 20 20 20 20 20 20 20 20 63 61 63  0 );.        cac
29e73 68 65 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49  heX.op = TK_REGI
29e74 53 54 45 52 3b 0a 20 20 20 20 20 20 20 20 6f 70  STER;.        op
29e75 43 6f 6d 70 61 72 65 2e 6f 70 20 3d 20 54 4b 5f  Compare.op = TK_
29e76 45 51 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f  EQ;.        opCo
29e77 6d 70 61 72 65 2e 70 4c 65 66 74 20 3d 20 26 63  mpare.pLeft = &c
29e78 61 63 68 65 58 3b 0a 20 20 20 20 20 20 20 20 70  acheX;.        p
29e79 54 65 73 74 20 3d 20 26 6f 70 43 6f 6d 70 61 72  Test = &opCompar
29e7a 65 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 69  e;.        /* Ti
29e7b 63 6b 65 74 20 62 33 35 31 64 39 35 66 39 63 64  cket b351d95f9cd
29e7c 35 65 66 31 37 65 39 64 39 64 62 61 65 31 38 66  5ef17e9d9dbae18f
29e7d 35 63 61 38 36 31 31 31 39 30 30 30 31 3a 0a 20  5ca8611190001:. 
29e7e 20 20 20 20 20 20 20 2a 2a 20 54 68 65 20 76 61         ** The va
29e7f 6c 75 65 20 69 6e 20 72 65 67 46 72 65 65 31 20  lue in regFree1 
29e80 6d 69 67 68 74 20 67 65 74 20 53 43 6f 70 79 2d  might get SCopy-
29e81 65 64 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65  ed into the file
29e82 20 72 65 73 75 6c 74 2e 0a 20 20 20 20 20 20 20   result..       
29e83 20 2a 2a 20 53 6f 20 6d 61 6b 65 20 73 75 72 65   ** So make sure
29e84 20 74 68 61 74 20 74 68 65 20 72 65 67 46 72 65   that the regFre
29e85 65 31 20 72 65 67 69 73 74 65 72 20 69 73 20 6e  e1 register is n
29e86 6f 74 20 72 65 75 73 65 64 20 66 6f 72 20 6f 74  ot reused for ot
29e87 68 65 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 70  her.        ** p
29e88 75 72 70 6f 73 65 73 20 61 6e 64 20 70 6f 73 73  urposes and poss
29e89 69 62 6c 79 20 6f 76 65 72 77 72 69 74 74 65 6e  ibly overwritten
29e8a 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65  .  */.        re
29e8b 67 46 72 65 65 31 20 3d 20 30 3b 0a 20 20 20 20  gFree1 = 0;.    
29e8c 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d    }.      for(i=
29e8d 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69 3d 69 2b  0; i<nExpr; i=i+
29e8e 32 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  2){.        sqli
29e8f 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68  te3ExprCachePush
29e90 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
29e91 20 20 69 66 28 20 70 58 20 29 7b 0a 20 20 20 20    if( pX ){.    
29e92 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
29e93 65 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  est!=0 );.      
29e94 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 52      opCompare.pR
29e95 69 67 68 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d  ight = aListelem
29e96 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
29e97 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
29e98 20 20 20 20 70 54 65 73 74 20 3d 20 61 4c 69 73      pTest = aLis
29e99 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a  telem[i].pExpr;.
29e9a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
29e9b 20 20 6e 65 78 74 43 61 73 65 20 3d 20 73 71 6c    nextCase = sql
29e9c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
29e9d 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 74 65  l(v);.        te
29e9e 73 74 63 61 73 65 28 20 70 54 65 73 74 2d 3e 6f  stcase( pTest->o
29e9f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a  p==TK_COLUMN );.
29ea0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
29ea1 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
29ea2 65 2c 20 70 54 65 73 74 2c 20 6e 65 78 74 43 61  e, pTest, nextCa
29ea3 73 65 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  se, SQLITE_JUMPI
29ea4 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20  FNULL);.        
29ea5 74 65 73 74 63 61 73 65 28 20 61 4c 69 73 74 65  testcase( aListe
29ea6 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e  lem[i+1].pExpr->
29ea7 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b  op==TK_COLUMN );
29ea8 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
29ea9 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31  e( aListelem[i+1
29eaa 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ].pExpr->op==TK_
29eab 52 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20  REGISTER );.    
29eac 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
29ead 6f 64 65 28 70 50 61 72 73 65 2c 20 61 4c 69 73  ode(pParse, aLis
29eae 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72  telem[i+1].pExpr
29eaf 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
29eb0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29eb1 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
29eb2 20 30 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a 20   0, endLabel);. 
29eb3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
29eb4 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73  prCachePop(pPars
29eb5 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73  e, 1);.        s
29eb6 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
29eb7 65 4c 61 62 65 6c 28 76 2c 20 6e 65 78 74 43 61  eLabel(v, nextCa
29eb8 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  se);.      }.   
29eb9 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 52     if( pExpr->pR
29eba 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 20 20  ight ){.        
29ebb 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
29ebc 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20  Push(pParse);.  
29ebd 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
29ebe 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45  rCode(pParse, pE
29ebf 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 74 61 72  xpr->pRight, tar
29ec0 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  get);.        sq
29ec1 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f  lite3ExprCachePo
29ec2 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20  p(pParse, 1);.  
29ec3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
29ec4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29ec5 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
29ec6 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20   0, target);.   
29ec7 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72     }.      asser
29ec8 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  t( db->mallocFai
29ec9 6c 65 64 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e  led || pParse->n
29eca 45 72 72 3e 30 20 0a 20 20 20 20 20 20 20 20 20  Err>0 .         
29ecb 20 20 7c 7c 20 70 50 61 72 73 65 2d 3e 69 43 61    || pParse->iCa
29ecc 63 68 65 4c 65 76 65 6c 3d 3d 69 43 61 63 68 65  cheLevel==iCache
29ecd 4c 65 76 65 6c 20 29 3b 0a 20 20 20 20 20 20 73  Level );.      s
29ece 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
29ecf 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4c 61 62  eLabel(v, endLab
29ed0 65 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  el);.      break
29ed1 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
29ed2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
29ed3 47 45 52 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  GER.    case TK_
29ed4 52 41 49 53 45 3a 20 7b 0a 20 20 20 20 20 20 61  RAISE: {.      a
29ed5 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 61 66  ssert( pExpr->af
29ed6 66 69 6e 69 74 79 3d 3d 4f 45 5f 52 6f 6c 6c 62  finity==OE_Rollb
29ed7 61 63 6b 20 0a 20 20 20 20 20 20 20 20 20 20 20  ack .           
29ed8 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69  || pExpr->affini
29ed9 74 79 3d 3d 4f 45 5f 41 62 6f 72 74 0a 20 20 20  ty==OE_Abort.   
29eda 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70 72          || pExpr
29edb 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 46  ->affinity==OE_F
29edc 61 69 6c 0a 20 20 20 20 20 20 20 20 20 20 20 7c  ail.           |
29edd 7c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74  | pExpr->affinit
29ede 79 3d 3d 4f 45 5f 49 67 6e 6f 72 65 0a 20 20 20  y==OE_Ignore.   
29edf 20 20 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20     );.      if( 
29ee0 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65  !pParse->pTrigge
29ee1 72 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20  rTab ){.        
29ee2 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
29ee3 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20  pParse,.        
29ee4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
29ee5 52 41 49 53 45 28 29 20 6d 61 79 20 6f 6e 6c 79  RAISE() may only
29ee6 20 62 65 20 75 73 65 64 20 77 69 74 68 69 6e 20   be used within 
29ee7 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61  a trigger-progra
29ee8 6d 22 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  m");.        ret
29ee9 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 0;.      }. 
29eea 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
29eeb 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 41 62 6f  affinity==OE_Abo
29eec 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  rt ){.        sq
29eed 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50  lite3MayAbort(pP
29eee 61 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  arse);.      }. 
29eef 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
29ef0 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
29ef1 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  xpr, EP_IntValue
29ef2 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ) );.      if( p
29ef3 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d  Expr->affinity==
29ef4 4f 45 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20  OE_Ignore ){.   
29ef5 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29ef6 41 64 64 4f 70 34 28 0a 20 20 20 20 20 20 20 20  AddOp4(.        
29ef7 20 20 20 20 76 2c 20 4f 50 5f 48 61 6c 74 2c 20      v, OP_Halt, 
29ef8 53 51 4c 49 54 45 5f 4f 4b 2c 20 4f 45 5f 49 67  SQLITE_OK, OE_Ig
29ef9 6e 6f 72 65 2c 20 30 2c 20 70 45 78 70 72 2d 3e  nore, 0, pExpr->
29efa 75 2e 7a 54 6f 6b 65 6e 2c 30 29 3b 0a 20 20 20  u.zToken,0);.   
29efb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
29efc 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e    sqlite3HaltCon
29efd 73 74 72 61 69 6e 74 28 70 50 61 72 73 65 2c 20  straint(pParse, 
29efe 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 2c  pExpr->affinity,
29eff 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
29f00 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  , 0);.      }.. 
29f01 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29f02 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73  }.#endif.  }.  s
29f03 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
29f04 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
29f05 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65  Free1);.  sqlite
29f06 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
29f07 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32  pParse, regFree2
29f08 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 6e 52 65  );.  return inRe
29f09 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  g;.}../*.** Gene
29f0a 72 61 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61  rate code to eva
29f0b 6c 75 61 74 65 20 61 6e 20 65 78 70 72 65 73 73  luate an express
29f0c 69 6f 6e 20 61 6e 64 20 73 74 6f 72 65 20 74 68  ion and store th
29f0d 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 74  e results.** int
29f0e 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 52  o a register.  R
29f0f 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74  eturn the regist
29f10 65 72 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20  er number where 
29f11 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 61  the results.** a
29f12 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a  re stored..**.**
29f13 20 49 66 20 74 68 65 20 72 65 67 69 73 74 65 72   If the register
29f14 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20   is a temporary 
29f15 72 65 67 69 73 74 65 72 20 74 68 61 74 20 63 61  register that ca
29f16 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64  n be deallocated
29f17 2c 0a 2a 2a 20 74 68 65 6e 20 77 72 69 74 65 20  ,.** then write 
29f18 69 74 73 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20  its number into 
29f19 2a 70 52 65 67 2e 20 20 49 66 20 74 68 65 20 72  *pReg.  If the r
29f1a 65 73 75 6c 74 20 72 65 67 69 73 74 65 72 20 69  esult register i
29f1b 73 20 6e 6f 74 0a 2a 2a 20 61 20 74 65 6d 70 6f  s not.** a tempo
29f1c 72 61 72 79 2c 20 74 68 65 6e 20 73 65 74 20 2a  rary, then set *
29f1d 70 52 65 67 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f  pReg to zero..*/
29f1e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
29f1f 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
29f20 6f 64 65 54 65 6d 70 28 50 61 72 73 65 20 2a 70  odeTemp(Parse *p
29f21 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
29f22 70 72 2c 20 69 6e 74 20 2a 70 52 65 67 29 7b 0a  pr, int *pReg){.
29f23 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
29f24 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
29f25 72 73 65 29 3b 0a 20 20 69 6e 74 20 72 32 20 3d  rse);.  int r2 =
29f26 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
29f27 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70  Target(pParse, p
29f28 45 78 70 72 2c 20 72 31 29 3b 0a 20 20 69 66 28  Expr, r1);.  if(
29f29 20 72 32 3d 3d 72 31 20 29 7b 0a 20 20 20 20 2a   r2==r1 ){.    *
29f2a 70 52 65 67 20 3d 20 72 31 3b 0a 20 20 7d 65 6c  pReg = r1;.  }el
29f2b 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  se{.    sqlite3R
29f2c 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
29f2d 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 2a  arse, r1);.    *
29f2e 70 52 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  pReg = 0;.  }.  
29f2f 72 65 74 75 72 6e 20 72 32 3b 0a 7d 0a 0a 2f 2a  return r2;.}../*
29f30 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
29f31 65 20 74 68 61 74 20 77 69 6c 6c 20 65 76 61 6c  e that will eval
29f32 75 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 20  uate expression 
29f33 70 45 78 70 72 20 61 6e 64 20 73 74 6f 72 65 20  pExpr and store 
29f34 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69  the.** results i
29f35 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 65  n register targe
29f36 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73 20  t.  The results 
29f37 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74  are guaranteed t
29f38 6f 20 61 70 70 65 61 72 0a 2a 2a 20 69 6e 20 72  o appear.** in r
29f39 65 67 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a  egister target..
29f3a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
29f3b 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
29f3c 72 43 6f 64 65 28 50 61 72 73 65 20 2a 70 50 61  rCode(Parse *pPa
29f3d 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
29f3e 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20  , int target){. 
29f3f 20 69 6e 74 20 69 6e 52 65 67 3b 0a 0a 20 20 61   int inReg;..  a
29f40 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20  ssert( target>0 
29f41 26 26 20 74 61 72 67 65 74 3c 3d 70 50 61 72 73  && target<=pPars
29f42 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 66 28  e->nMem );.  if(
29f43 20 70 45 78 70 72 20 26 26 20 70 45 78 70 72 2d   pExpr && pExpr-
29f44 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52  >op==TK_REGISTER
29f45 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
29f46 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 73 65  dbeAddOp2(pParse
29f47 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 43 6f 70 79  ->pVdbe, OP_Copy
29f48 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c  , pExpr->iTable,
29f49 20 74 61 72 67 65 74 29 3b 0a 20 20 7d 65 6c 73   target);.  }els
29f4a 65 7b 0a 20 20 20 20 69 6e 52 65 67 20 3d 20 73  e{.    inReg = s
29f4b 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
29f4c 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78  rget(pParse, pEx
29f4d 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  pr, target);.   
29f4e 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
29f4f 3e 70 56 64 62 65 20 7c 7c 20 70 50 61 72 73 65  >pVdbe || pParse
29f50 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
29f51 65 64 20 29 3b 0a 20 20 20 20 69 66 28 20 69 6e  ed );.    if( in
29f52 52 65 67 21 3d 74 61 72 67 65 74 20 26 26 20 70  Reg!=target && p
29f53 50 61 72 73 65 2d 3e 70 56 64 62 65 20 29 7b 0a  Parse->pVdbe ){.
29f54 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29f55 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e  eAddOp2(pParse->
29f56 70 56 64 62 65 2c 20 4f 50 5f 53 43 6f 70 79 2c  pVdbe, OP_SCopy,
29f57 20 69 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b   inReg, target);
29f58 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
29f59 75 72 6e 20 74 61 72 67 65 74 3b 0a 7d 0a 0a 2f  urn target;.}../
29f5a 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
29f5b 64 65 20 74 68 61 74 20 65 76 61 6c 75 74 65 73  de that evalutes
29f5c 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72 65   the given expre
29f5d 73 73 69 6f 6e 20 61 6e 64 20 70 75 74 73 20 74  ssion and puts t
29f5e 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20  he result.** in 
29f5f 72 65 67 69 73 74 65 72 20 74 61 72 67 65 74 2e  register target.
29f60 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 6d 61 6b 65  .**.** Also make
29f61 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65   a copy of the e
29f62 78 70 72 65 73 73 69 6f 6e 20 72 65 73 75 6c 74  xpression result
29f63 73 20 69 6e 74 6f 20 61 6e 6f 74 68 65 72 20 22  s into another "
29f64 63 61 63 68 65 22 20 72 65 67 69 73 74 65 72 0a  cache" register.
29f65 2a 2a 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68  ** and modify th
29f66 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 6f 20  e expression so 
29f67 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 74 69  that the next ti
29f68 6d 65 20 69 74 20 69 73 20 65 76 61 6c 75 61 74  me it is evaluat
29f69 65 64 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c  ed,.** the resul
29f6a 74 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74  t is a copy of t
29f6b 68 65 20 63 61 63 68 65 20 72 65 67 69 73 74 65  he cache registe
29f6c 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  r..**.** This ro
29f6d 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f  utine is used fo
29f6e 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68  r expressions th
29f6f 61 74 20 61 72 65 20 75 73 65 64 20 6d 75 6c 74  at are used mult
29f70 69 70 6c 65 20 0a 2a 2a 20 74 69 6d 65 73 2e 20  iple .** times. 
29f71 20 54 68 65 79 20 61 72 65 20 65 76 61 6c 75 61   They are evalua
29f72 74 65 64 20 6f 6e 63 65 20 61 6e 64 20 74 68 65  ted once and the
29f73 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
29f74 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 72  expression.** ar
29f75 65 20 72 65 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c  e reused..*/.SQL
29f76 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
29f77 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41  sqlite3ExprCodeA
29f78 6e 64 43 61 63 68 65 28 50 61 72 73 65 20 2a 70  ndCache(Parse *p
29f79 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
29f7a 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b  pr, int target){
29f7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
29f7c 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e  rse->pVdbe;.  in
29f7d 74 20 69 6e 52 65 67 3b 0a 20 20 69 6e 52 65 67  t inReg;.  inReg
29f7e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
29f7f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  de(pParse, pExpr
29f80 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 73 73  , target);.  ass
29f81 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 29 3b  ert( target>0 );
29f82 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69  .  /* This routi
29f83 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72  ne is called for
29f84 20 74 65 72 6d 73 20 74 6f 20 49 4e 53 45 52 54   terms to INSERT
29f85 20 6f 72 20 55 50 44 41 54 45 2e 20 20 41 6e 64   or UPDATE.  And
29f86 20 74 68 65 20 6f 6e 6c 79 0a 20 20 2a 2a 20 6f   the only.  ** o
29f87 74 68 65 72 20 70 6c 61 63 65 20 77 68 65 72 65  ther place where
29f88 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 6e   expressions can
29f89 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e   be converted in
29f8a 74 6f 20 54 4b 5f 52 45 47 49 53 54 45 52 20 69  to TK_REGISTER i
29f8b 73 0a 20 20 2a 2a 20 69 6e 20 57 48 45 52 45 20  s.  ** in WHERE 
29f8c 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e  clause processin
29f8d 67 2e 20 20 53 6f 20 61 73 20 63 75 72 72 65 6e  g.  So as curren
29f8e 74 6c 79 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2c  tly implemented,
29f8f 20 74 68 65 72 65 20 69 73 0a 20 20 2a 2a 20 6e   there is.  ** n
29f90 6f 20 77 61 79 20 66 6f 72 20 61 20 54 4b 5f 52  o way for a TK_R
29f91 45 47 49 53 54 45 52 20 74 6f 20 65 78 69 73 74  EGISTER to exist
29f92 20 68 65 72 65 2e 20 20 42 75 74 20 69 74 20 73   here.  But it s
29f93 65 65 6d 73 20 70 72 75 64 65 6e 74 20 74 6f 0a  eems prudent to.
29f94 20 20 2a 2a 20 6b 65 65 70 20 74 68 65 20 41 4c    ** keep the AL
29f95 57 41 59 53 28 29 20 69 6e 20 63 61 73 65 20 74  WAYS() in case t
29f96 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62  he conditions ab
29f97 6f 76 65 20 63 68 61 6e 67 65 20 77 69 74 68 20  ove change with 
29f98 66 75 74 75 72 65 0a 20 20 2a 2a 20 6d 6f 64 69  future.  ** modi
29f99 66 69 63 61 74 69 6f 6e 73 20 6f 72 20 65 6e 68  fications or enh
29f9a 61 6e 63 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20  ancements. */.  
29f9b 69 66 28 20 41 4c 57 41 59 53 28 70 45 78 70 72  if( ALWAYS(pExpr
29f9c 2d 3e 6f 70 21 3d 54 4b 5f 52 45 47 49 53 54 45  ->op!=TK_REGISTE
29f9d 52 29 20 29 7b 20 20 0a 20 20 20 20 69 6e 74 20  R) ){  .    int 
29f9e 69 4d 65 6d 3b 0a 20 20 20 20 69 4d 65 6d 20 3d  iMem;.    iMem =
29f9f 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
29fa0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29fa1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70  AddOp2(v, OP_Cop
29fa2 79 2c 20 69 6e 52 65 67 2c 20 69 4d 65 6d 29 3b  y, inReg, iMem);
29fa3 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62  .    pExpr->iTab
29fa4 6c 65 20 3d 20 69 4d 65 6d 3b 0a 20 20 20 20 70  le = iMem;.    p
29fa5 45 78 70 72 2d 3e 6f 70 32 20 3d 20 70 45 78 70  Expr->op2 = pExp
29fa6 72 2d 3e 6f 70 3b 0a 20 20 20 20 70 45 78 70 72  r->op;.    pExpr
29fa7 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54  ->op = TK_REGIST
29fa8 45 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ER;.  }.  return
29fa9 20 69 6e 52 65 67 3b 0a 7d 0a 0a 23 69 66 20 64   inReg;.}..#if d
29faa 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
29fab 41 42 4c 45 5f 54 52 45 45 5f 45 58 50 4c 41 49  ABLE_TREE_EXPLAI
29fac 4e 29 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  N)./*.** Generat
29fad 65 20 61 20 68 75 6d 61 6e 2d 72 65 61 64 61 62  e a human-readab
29fae 6c 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 6f  le explanation o
29faf 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  f an expression 
29fb0 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tree..*/.SQLITE_
29fb1 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
29fb2 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72 28  ite3ExplainExpr(
29fb3 56 64 62 65 20 2a 70 4f 75 74 2c 20 45 78 70 72  Vdbe *pOut, Expr
29fb4 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20   *pExpr){.  int 
29fb5 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
29fb6 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 63        /* The opc
29fb7 6f 64 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20  ode being coded 
29fb8 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
29fb9 2a 7a 42 69 6e 4f 70 20 3d 20 30 3b 20 20 20 2f  *zBinOp = 0;   /
29fba 2a 20 42 69 6e 61 72 79 20 6f 70 65 72 61 74 6f  * Binary operato
29fbb 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  r */.  const cha
29fbc 72 20 2a 7a 55 6e 69 4f 70 20 3d 20 30 3b 20 20  r *zUniOp = 0;  
29fbd 20 2f 2a 20 55 6e 61 72 79 20 6f 70 65 72 61 74   /* Unary operat
29fbe 6f 72 20 2a 2f 0a 20 20 69 66 28 20 70 45 78 70  or */.  if( pExp
29fbf 72 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 70 20 3d  r==0 ){.    op =
29fc0 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73   TK_NULL;.  }els
29fc1 65 7b 0a 20 20 20 20 6f 70 20 3d 20 70 45 78 70  e{.    op = pExp
29fc2 72 2d 3e 6f 70 3b 0a 20 20 7d 0a 20 20 73 77 69  r->op;.  }.  swi
29fc3 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63  tch( op ){.    c
29fc4 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  ase TK_AGG_COLUM
29fc5 4e 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  N: {.      sqlit
29fc6 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28  e3ExplainPrintf(
29fc7 70 4f 75 74 2c 20 22 41 47 47 7b 25 64 3a 25 64  pOut, "AGG{%d:%d
29fc8 7d 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  }",.            
29fc9 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 70  pExpr->iTable, p
29fca 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0a  Expr->iColumn);.
29fcb 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
29fcc 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43   }.    case TK_C
29fcd 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 69  OLUMN: {.      i
29fce 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  f( pExpr->iTable
29fcf 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  <0 ){.        /*
29fd0 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65   This only happe
29fd1 6e 73 20 77 68 65 6e 20 63 6f 64 69 6e 67 20 63  ns when coding c
29fd2 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73  heck constraints
29fd3 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69   */.        sqli
29fd4 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66  te3ExplainPrintf
29fd5 28 70 4f 75 74 2c 20 22 43 4f 4c 55 4d 4e 28 25  (pOut, "COLUMN(%
29fd6 64 29 22 2c 20 70 45 78 70 72 2d 3e 69 43 6f 6c  d)", pExpr->iCol
29fd7 75 6d 6e 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  umn);.      }els
29fd8 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
29fd9 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28  e3ExplainPrintf(
29fda 70 4f 75 74 2c 20 22 7b 25 64 3a 25 64 7d 22 2c  pOut, "{%d:%d}",
29fdb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
29fdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
29fdd 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 70 45 78  xpr->iTable, pEx
29fde 70 72 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20  pr->iColumn);.  
29fdf 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
29fe0 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
29fe1 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a  e TK_INTEGER: {.
29fe2 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
29fe3 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56  >flags & EP_IntV
29fe4 61 6c 75 65 20 29 7b 0a 20 20 20 20 20 20 20 20  alue ){.        
29fe5 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72  sqlite3ExplainPr
29fe6 69 6e 74 66 28 70 4f 75 74 2c 20 22 25 64 22 2c  intf(pOut, "%d",
29fe7 20 70 45 78 70 72 2d 3e 75 2e 69 56 61 6c 75 65   pExpr->u.iValue
29fe8 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
29fe9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
29fea 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f 75  xplainPrintf(pOu
29feb 74 2c 20 22 25 73 22 2c 20 70 45 78 70 72 2d 3e  t, "%s", pExpr->
29fec 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20  u.zToken);.     
29fed 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
29fee 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
29fef 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
29ff0 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 63 61 73  NG_POINT.    cas
29ff1 65 20 54 4b 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20  e TK_FLOAT: {.  
29ff2 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61      sqlite3Expla
29ff3 69 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 22 25  inPrintf(pOut,"%
29ff4 73 22 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f  s", pExpr->u.zTo
29ff5 6b 65 6e 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ken);.      brea
29ff6 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  k;.    }.#endif.
29ff7 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49      case TK_STRI
29ff8 4e 47 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  NG: {.      sqli
29ff9 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66  te3ExplainPrintf
29ffa 28 70 4f 75 74 2c 22 25 51 22 2c 20 70 45 78 70  (pOut,"%Q", pExp
29ffb 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20  r->u.zToken);.  
29ffc 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
29ffd 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c  .    case TK_NUL
29ffe 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L: {.      sqlit
29fff 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28  e3ExplainPrintf(
2a000 70 4f 75 74 2c 22 4e 55 4c 4c 22 29 3b 0a 20 20  pOut,"NULL");.  
2a001 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2a002 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2a003 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
2a004 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c  L.    case TK_BL
2a005 4f 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  OB: {.      sqli
2a006 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66  te3ExplainPrintf
2a007 28 70 4f 75 74 2c 22 25 73 22 2c 20 70 45 78 70  (pOut,"%s", pExp
2a008 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20  r->u.zToken);.  
2a009 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2a00a 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65  .#endif.    case
2a00b 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a   TK_VARIABLE: {.
2a00c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2a00d 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c  lainPrintf(pOut,
2a00e 22 56 41 52 49 41 42 4c 45 28 25 73 2c 25 64 29  "VARIABLE(%s,%d)
2a00f 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
2a010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
2a011 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70  xpr->u.zToken, p
2a012 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0a  Expr->iColumn);.
2a013 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2a014 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52   }.    case TK_R
2a015 45 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20  EGISTER: {.     
2a016 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50   sqlite3ExplainP
2a017 72 69 6e 74 66 28 70 4f 75 74 2c 22 52 45 47 49  rintf(pOut,"REGI
2a018 53 54 45 52 28 25 64 29 22 2c 20 70 45 78 70 72  STER(%d)", pExpr
2a019 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 20  ->iTable);.     
2a01a 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2a01b 20 20 63 61 73 65 20 54 4b 5f 41 53 3a 20 7b 0a    case TK_AS: {.
2a01c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2a01d 6c 61 69 6e 45 78 70 72 28 70 4f 75 74 2c 20 70  lainExpr(pOut, p
2a01e 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  Expr->pLeft);.  
2a01f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2a020 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2a021 4f 4d 49 54 5f 43 41 53 54 0a 20 20 20 20 63 61  OMIT_CAST.    ca
2a022 73 65 20 54 4b 5f 43 41 53 54 3a 20 7b 0a 20 20  se TK_CAST: {.  
2a023 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
2a024 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20  ns of the form: 
2a025 20 20 43 41 53 54 28 70 4c 65 66 74 20 41 53 20    CAST(pLeft AS 
2a026 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20 20 20 20 20  token) */.      
2a027 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 66 66  const char *zAff
2a028 20 3d 20 22 75 6e 6b 22 3b 0a 20 20 20 20 20 20   = "unk";.      
2a029 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 41  switch( sqlite3A
2a02a 66 66 69 6e 69 74 79 54 79 70 65 28 70 45 78 70  ffinityType(pExp
2a02b 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 20 29 7b 0a  r->u.zToken) ){.
2a02c 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51 4c          case SQL
2a02d 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 20 20 20  ITE_AFF_TEXT:   
2a02e 20 7a 41 66 66 20 3d 20 22 54 45 58 54 22 3b 20   zAff = "TEXT"; 
2a02f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2a030 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41     case SQLITE_A
2a031 46 46 5f 4e 4f 4e 45 3a 20 20 20 20 7a 41 66 66  FF_NONE:    zAff
2a032 20 3d 20 22 4e 4f 4e 45 22 3b 20 20 20 20 20 62   = "NONE";     b
2a033 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
2a034 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  se SQLITE_AFF_NU
2a035 4d 45 52 49 43 3a 20 7a 41 66 66 20 3d 20 22 4e  MERIC: zAff = "N
2a036 55 4d 45 52 49 43 22 3b 20 20 62 72 65 61 6b 3b  UMERIC";  break;
2a037 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51  .        case SQ
2a038 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
2a039 3a 20 7a 41 66 66 20 3d 20 22 49 4e 54 45 47 45  : zAff = "INTEGE
2a03a 52 22 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  R";  break;.    
2a03b 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2a03c 41 46 46 5f 52 45 41 4c 3a 20 20 20 20 7a 41 66  AFF_REAL:    zAf
2a03d 66 20 3d 20 22 52 45 41 4c 22 3b 20 20 20 20 20  f = "REAL";     
2a03e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2a03f 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c       sqlite3Expl
2a040 61 69 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 20  ainPrintf(pOut, 
2a041 22 43 41 53 54 2d 25 73 28 22 2c 20 7a 41 66 66  "CAST-%s(", zAff
2a042 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2a043 45 78 70 6c 61 69 6e 45 78 70 72 28 70 4f 75 74  ExplainExpr(pOut
2a044 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b  , pExpr->pLeft);
2a045 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2a046 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f 75 74  plainPrintf(pOut
2a047 2c 20 22 29 22 29 3b 0a 20 20 20 20 20 20 62 72  , ")");.      br
2a048 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
2a049 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2a04a 5f 43 41 53 54 20 2a 2f 0a 20 20 20 20 63 61 73  _CAST */.    cas
2a04b 65 20 54 4b 5f 4c 54 3a 20 20 20 20 20 20 7a 42  e TK_LT:      zB
2a04c 69 6e 4f 70 20 3d 20 22 4c 54 22 3b 20 20 20 20  inOp = "LT";    
2a04d 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
2a04e 20 54 4b 5f 4c 45 3a 20 20 20 20 20 20 7a 42 69   TK_LE:      zBi
2a04f 6e 4f 70 20 3d 20 22 4c 45 22 3b 20 20 20 20 20  nOp = "LE";     
2a050 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
2a051 54 4b 5f 47 54 3a 20 20 20 20 20 20 7a 42 69 6e  TK_GT:      zBin
2a052 4f 70 20 3d 20 22 47 54 22 3b 20 20 20 20 20 62  Op = "GT";     b
2a053 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 54  reak;.    case T
2a054 4b 5f 47 45 3a 20 20 20 20 20 20 7a 42 69 6e 4f  K_GE:      zBinO
2a055 70 20 3d 20 22 47 45 22 3b 20 20 20 20 20 62 72  p = "GE";     br
2a056 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 54 4b  eak;.    case TK
2a057 5f 4e 45 3a 20 20 20 20 20 20 7a 42 69 6e 4f 70  _NE:      zBinOp
2a058 20 3d 20 22 4e 45 22 3b 20 20 20 20 20 62 72 65   = "NE";     bre
2a059 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ak;.    case TK_
2a05a 45 51 3a 20 20 20 20 20 20 7a 42 69 6e 4f 70 20  EQ:      zBinOp 
2a05b 3d 20 22 45 51 22 3b 20 20 20 20 20 62 72 65 61  = "EQ";     brea
2a05c 6b 3b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49  k;.    case TK_I
2a05d 53 3a 20 20 20 20 20 20 7a 42 69 6e 4f 70 20 3d  S:      zBinOp =
2a05e 20 22 49 53 22 3b 20 20 20 20 20 62 72 65 61 6b   "IS";     break
2a05f 3b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53  ;.    case TK_IS
2a060 4e 4f 54 3a 20 20 20 7a 42 69 6e 4f 70 20 3d 20  NOT:   zBinOp = 
2a061 22 49 53 4e 4f 54 22 3b 20 20 62 72 65 61 6b 3b  "ISNOT";  break;
2a062 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44  .    case TK_AND
2a063 3a 20 20 20 20 20 7a 42 69 6e 4f 70 20 3d 20 22  :     zBinOp = "
2a064 41 4e 44 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a  AND";    break;.
2a065 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a 20      case TK_OR: 
2a066 20 20 20 20 20 7a 42 69 6e 4f 70 20 3d 20 22 4f       zBinOp = "O
2a067 52 22 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  R";     break;. 
2a068 20 20 20 63 61 73 65 20 54 4b 5f 50 4c 55 53 3a     case TK_PLUS:
2a069 20 20 20 20 7a 42 69 6e 4f 70 20 3d 20 22 41 44      zBinOp = "AD
2a06a 44 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  D";    break;.  
2a06b 20 20 63 61 73 65 20 54 4b 5f 53 54 41 52 3a 20    case TK_STAR: 
2a06c 20 20 20 7a 42 69 6e 4f 70 20 3d 20 22 4d 55 4c     zBinOp = "MUL
2a06d 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ";    break;.   
2a06e 20 63 61 73 65 20 54 4b 5f 4d 49 4e 55 53 3a 20   case TK_MINUS: 
2a06f 20 20 7a 42 69 6e 4f 70 20 3d 20 22 53 55 42 22    zBinOp = "SUB"
2a070 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  ;    break;.    
2a071 63 61 73 65 20 54 4b 5f 52 45 4d 3a 20 20 20 20  case TK_REM:    
2a072 20 7a 42 69 6e 4f 70 20 3d 20 22 52 45 4d 22 3b   zBinOp = "REM";
2a073 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
2a074 61 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a 20 20  ase TK_BITAND:  
2a075 7a 42 69 6e 4f 70 20 3d 20 22 42 49 54 41 4e 44  zBinOp = "BITAND
2a076 22 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61  "; break;.    ca
2a077 73 65 20 54 4b 5f 42 49 54 4f 52 3a 20 20 20 7a  se TK_BITOR:   z
2a078 42 69 6e 4f 70 20 3d 20 22 42 49 54 4f 52 22 3b  BinOp = "BITOR";
2a079 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
2a07a 65 20 54 4b 5f 53 4c 41 53 48 3a 20 20 20 7a 42  e TK_SLASH:   zB
2a07b 69 6e 4f 70 20 3d 20 22 44 49 56 22 3b 20 20 20  inOp = "DIV";   
2a07c 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
2a07d 20 54 4b 5f 4c 53 48 49 46 54 3a 20 20 7a 42 69   TK_LSHIFT:  zBi
2a07e 6e 4f 70 20 3d 20 22 4c 53 48 49 46 54 22 3b 20  nOp = "LSHIFT"; 
2a07f 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
2a080 54 4b 5f 52 53 48 49 46 54 3a 20 20 7a 42 69 6e  TK_RSHIFT:  zBin
2a081 4f 70 20 3d 20 22 52 53 48 49 46 54 22 3b 20 62  Op = "RSHIFT"; b
2a082 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 54  reak;.    case T
2a083 4b 5f 43 4f 4e 43 41 54 3a 20 20 7a 42 69 6e 4f  K_CONCAT:  zBinO
2a084 70 20 3d 20 22 43 4f 4e 43 41 54 22 3b 20 62 72  p = "CONCAT"; br
2a085 65 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 20 54  eak;..    case T
2a086 4b 5f 55 4d 49 4e 55 53 3a 20 20 7a 55 6e 69 4f  K_UMINUS:  zUniO
2a087 70 20 3d 20 22 55 4d 49 4e 55 53 22 3b 20 62 72  p = "UMINUS"; br
2a088 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 54 4b  eak;.    case TK
2a089 5f 55 50 4c 55 53 3a 20 20 20 7a 55 6e 69 4f 70  _UPLUS:   zUniOp
2a08a 20 3d 20 22 55 50 4c 55 53 22 3b 20 20 62 72 65   = "UPLUS";  bre
2a08b 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ak;.    case TK_
2a08c 42 49 54 4e 4f 54 3a 20 20 7a 55 6e 69 4f 70 20  BITNOT:  zUniOp 
2a08d 3d 20 22 42 49 54 4e 4f 54 22 3b 20 62 72 65 61  = "BITNOT"; brea
2a08e 6b 3b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e  k;.    case TK_N
2a08f 4f 54 3a 20 20 20 20 20 7a 55 6e 69 4f 70 20 3d  OT:     zUniOp =
2a090 20 22 4e 4f 54 22 3b 20 20 20 20 62 72 65 61 6b   "NOT";    break
2a091 3b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53  ;.    case TK_IS
2a092 4e 55 4c 4c 3a 20 20 7a 55 6e 69 4f 70 20 3d 20  NULL:  zUniOp = 
2a093 22 49 53 4e 55 4c 4c 22 3b 20 62 72 65 61 6b 3b  "ISNULL"; break;
2a094 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54  .    case TK_NOT
2a095 4e 55 4c 4c 3a 20 7a 55 6e 69 4f 70 20 3d 20 22  NULL: zUniOp = "
2a096 4e 4f 54 4e 55 4c 4c 22 3b 20 62 72 65 61 6b 3b  NOTNULL"; break;
2a097 0a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  ..    case TK_AG
2a098 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20  G_FUNCTION:.    
2a099 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55  case TK_CONST_FU
2a09a 4e 43 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  NC:.    case TK_
2a09b 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20  FUNCTION: {.    
2a09c 20 20 45 78 70 72 4c 69 73 74 20 2a 70 46 61 72    ExprList *pFar
2a09d 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74  g;       /* List
2a09e 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72 67   of function arg
2a09f 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20  uments */.      
2a0a0 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72  if( ExprHasAnyPr
2a0a1 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
2a0a2 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20  _TokenOnly) ){. 
2a0a3 20 20 20 20 20 20 20 70 46 61 72 67 20 3d 20 30         pFarg = 0
2a0a4 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2a0a5 20 20 20 20 20 20 20 70 46 61 72 67 20 3d 20 70         pFarg = p
2a0a6 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20  Expr->x.pList;. 
2a0a7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2a0a8 20 6f 70 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e 43   op==TK_AGG_FUNC
2a0a9 54 49 4f 4e 20 29 7b 0a 20 20 20 20 20 20 20 20  TION ){.        
2a0aa 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72  sqlite3ExplainPr
2a0ab 69 6e 74 66 28 70 4f 75 74 2c 20 22 41 47 47 5f  intf(pOut, "AGG_
2a0ac 46 55 4e 43 54 49 4f 4e 25 64 3a 25 73 28 22 2c  FUNCTION%d:%s(",
2a0ad 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2a0ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
2a0af 78 70 72 2d 3e 6f 70 32 2c 20 70 45 78 70 72 2d  xpr->op2, pExpr-
2a0b0 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  >u.zToken);.    
2a0b1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2a0b2 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50   sqlite3ExplainP
2a0b3 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 46 55 4e  rintf(pOut, "FUN
2a0b4 43 54 49 4f 4e 3a 25 73 28 22 2c 20 70 45 78 70  CTION:%s(", pExp
2a0b5 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20  r->u.zToken);.  
2a0b6 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2a0b7 70 46 61 72 67 20 29 7b 0a 20 20 20 20 20 20 20  pFarg ){.       
2a0b8 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45   sqlite3ExplainE
2a0b9 78 70 72 4c 69 73 74 28 70 4f 75 74 2c 20 70 46  xprList(pOut, pF
2a0ba 61 72 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  arg);.      }.  
2a0bb 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61      sqlite3Expla
2a0bc 69 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 20 22  inPrintf(pOut, "
2a0bd 29 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  )");.      break
2a0be 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
2a0bf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
2a0c0 55 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b  UERY.    case TK
2a0c1 5f 45 58 49 53 54 53 3a 20 7b 0a 20 20 20 20 20  _EXISTS: {.     
2a0c2 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50   sqlite3ExplainP
2a0c3 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 45 58 49  rintf(pOut, "EXI
2a0c4 53 54 53 28 22 29 3b 0a 20 20 20 20 20 20 73 71  STS(");.      sq
2a0c5 6c 69 74 65 33 45 78 70 6c 61 69 6e 53 65 6c 65  lite3ExplainSele
2a0c6 63 74 28 70 4f 75 74 2c 20 70 45 78 70 72 2d 3e  ct(pOut, pExpr->
2a0c7 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  x.pSelect);.    
2a0c8 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
2a0c9 50 72 69 6e 74 66 28 70 4f 75 74 2c 22 29 22 29  Printf(pOut,")")
2a0ca 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2a0cb 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
2a0cc 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20  _SELECT: {.     
2a0cd 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50   sqlite3ExplainP
2a0ce 72 69 6e 74 66 28 70 4f 75 74 2c 20 22 28 22 29  rintf(pOut, "(")
2a0cf 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2a0d0 78 70 6c 61 69 6e 53 65 6c 65 63 74 28 70 4f 75  xplainSelect(pOu
2a0d1 74 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c  t, pExpr->x.pSel
2a0d2 65 63 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ect);.      sqli
2a0d3 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66  te3ExplainPrintf
2a0d4 28 70 4f 75 74 2c 20 22 29 22 29 3b 0a 20 20 20  (pOut, ")");.   
2a0d5 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2a0d6 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20      case TK_IN: 
2a0d7 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
2a0d8 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f 75  xplainPrintf(pOu
2a0d9 74 2c 20 22 49 4e 28 22 29 3b 0a 20 20 20 20 20  t, "IN(");.     
2a0da 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45   sqlite3ExplainE
2a0db 78 70 72 28 70 4f 75 74 2c 20 70 45 78 70 72 2d  xpr(pOut, pExpr-
2a0dc 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 73  >pLeft);.      s
2a0dd 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69  qlite3ExplainPri
2a0de 6e 74 66 28 70 4f 75 74 2c 20 22 2c 22 29 3b 0a  ntf(pOut, ",");.
2a0df 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61        if( ExprHa
2a0e0 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
2a0e1 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
2a0e2 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2a0e3 33 45 78 70 6c 61 69 6e 53 65 6c 65 63 74 28 70  3ExplainSelect(p
2a0e4 4f 75 74 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53  Out, pExpr->x.pS
2a0e5 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 7d 65  elect);.      }e
2a0e6 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
2a0e7 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72 4c  ite3ExplainExprL
2a0e8 69 73 74 28 70 4f 75 74 2c 20 70 45 78 70 72 2d  ist(pOut, pExpr-
2a0e9 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20 20 20 20  >x.pList);.     
2a0ea 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2a0eb 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f  ExplainPrintf(pO
2a0ec 75 74 2c 20 22 29 22 29 3b 0a 20 20 20 20 20 20  ut, ")");.      
2a0ed 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
2a0ee 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2a0ef 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a  IT_SUBQUERY */..
2a0f0 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 20      /*.    **   
2a0f1 20 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44   x BETWEEN y AND
2a0f2 20 7a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a   z.    **.    **
2a0f3 20 54 68 69 73 20 69 73 20 65 71 75 69 76 61 6c   This is equival
2a0f4 65 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 0a 20 20  ent to.    **.  
2a0f5 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44    **    x>=y AND
2a0f6 20 78 3c 3d 7a 0a 20 20 20 20 2a 2a 0a 20 20 20   x<=z.    **.   
2a0f7 20 2a 2a 20 58 20 69 73 20 73 74 6f 72 65 64 20   ** X is stored 
2a0f8 69 6e 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e  in pExpr->pLeft.
2a0f9 0a 20 20 20 20 2a 2a 20 59 20 69 73 20 73 74 6f  .    ** Y is sto
2a0fa 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c  red in pExpr->pL
2a0fb 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2e  ist->a[0].pExpr.
2a0fc 0a 20 20 20 20 2a 2a 20 5a 20 69 73 20 73 74 6f  .    ** Z is sto
2a0fd 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c  red in pExpr->pL
2a0fe 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 2e  ist->a[1].pExpr.
2a0ff 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
2a100 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20   TK_BETWEEN: {. 
2a101 20 20 20 20 20 45 78 70 72 20 2a 70 58 20 3d 20       Expr *pX = 
2a102 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20  pExpr->pLeft;.  
2a103 20 20 20 20 45 78 70 72 20 2a 70 59 20 3d 20 70      Expr *pY = p
2a104 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61  Expr->x.pList->a
2a105 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [0].pExpr;.     
2a106 20 45 78 70 72 20 2a 70 5a 20 3d 20 70 45 78 70   Expr *pZ = pExp
2a107 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d  r->x.pList->a[1]
2a108 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 73 71  .pExpr;.      sq
2a109 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e  lite3ExplainPrin
2a10a 74 66 28 70 4f 75 74 2c 20 22 42 45 54 57 45 45  tf(pOut, "BETWEE
2a10b 4e 28 22 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  N(");.      sqli
2a10c 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72 28 70  te3ExplainExpr(p
2a10d 4f 75 74 2c 20 70 58 29 3b 0a 20 20 20 20 20 20  Out, pX);.      
2a10e 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72  sqlite3ExplainPr
2a10f 69 6e 74 66 28 70 4f 75 74 2c 20 22 2c 22 29 3b  intf(pOut, ",");
2a110 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2a111 70 6c 61 69 6e 45 78 70 72 28 70 4f 75 74 2c 20  plainExpr(pOut, 
2a112 70 59 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  pY);.      sqlit
2a113 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28  e3ExplainPrintf(
2a114 70 4f 75 74 2c 20 22 2c 22 29 3b 0a 20 20 20 20  pOut, ",");.    
2a115 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
2a116 45 78 70 72 28 70 4f 75 74 2c 20 70 5a 29 3b 0a  Expr(pOut, pZ);.
2a117 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2a118 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c  lainPrintf(pOut,
2a119 20 22 29 22 29 3b 0a 20 20 20 20 20 20 62 72 65   ")");.      bre
2a11a 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
2a11b 73 65 20 54 4b 5f 54 52 49 47 47 45 52 3a 20 7b  se TK_TRIGGER: {
2a11c 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
2a11d 20 6f 70 63 6f 64 65 20 69 73 20 54 4b 5f 54 52   opcode is TK_TR
2a11e 49 47 47 45 52 2c 20 74 68 65 6e 20 74 68 65 20  IGGER, then the 
2a11f 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
2a120 72 65 66 65 72 65 6e 63 65 0a 20 20 20 20 20 20  reference.      
2a121 2a 2a 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69  ** to a column i
2a122 6e 20 74 68 65 20 6e 65 77 2e 2a 20 6f 72 20 6f  n the new.* or o
2a123 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c  ld.* pseudo-tabl
2a124 65 73 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 0a  es available to.
2a125 20 20 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72        ** trigger
2a126 20 70 72 6f 67 72 61 6d 73 2e 20 49 6e 20 74 68   programs. In th
2a127 69 73 20 63 61 73 65 20 45 78 70 72 2e 69 54 61  is case Expr.iTa
2a128 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 31 20  ble is set to 1 
2a129 66 6f 72 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  for the.      **
2a12a 20 6e 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61   new.* pseudo-ta
2a12b 62 6c 65 2c 20 6f 72 20 30 20 66 6f 72 20 74 68  ble, or 0 for th
2a12c 65 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74  e old.* pseudo-t
2a12d 61 62 6c 65 2e 20 45 78 70 72 2e 69 43 6f 6c 75  able. Expr.iColu
2a12e 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73  mn.      ** is s
2a12f 65 74 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e  et to the column
2a130 20 6f 66 20 74 68 65 20 70 73 65 75 64 6f 2d 74   of the pseudo-t
2a131 61 62 6c 65 20 74 6f 20 72 65 61 64 2c 20 6f 72  able to read, or
2a132 20 74 6f 20 2d 31 20 74 6f 0a 20 20 20 20 20 20   to -1 to.      
2a133 2a 2a 20 72 65 61 64 20 74 68 65 20 72 6f 77 69  ** read the rowi
2a134 64 20 66 69 65 6c 64 2e 0a 20 20 20 20 20 20 2a  d field..      *
2a135 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  /.      sqlite3E
2a136 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f 75  xplainPrintf(pOu
2a137 74 2c 20 22 25 73 28 25 64 29 22 2c 20 0a 20 20  t, "%s(%d)", .  
2a138 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
2a139 54 61 62 6c 65 20 3f 20 22 4e 45 57 22 20 3a 20  Table ? "NEW" : 
2a13a 22 4f 4c 44 22 2c 20 70 45 78 70 72 2d 3e 69 43  "OLD", pExpr->iC
2a13b 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 62 72  olumn);.      br
2a13c 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
2a13d 61 73 65 20 54 4b 5f 43 41 53 45 3a 20 7b 0a 20  ase TK_CASE: {. 
2a13e 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c       sqlite3Expl
2a13f 61 69 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 20  ainPrintf(pOut, 
2a140 22 43 41 53 45 28 22 29 3b 0a 20 20 20 20 20 20  "CASE(");.      
2a141 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78  sqlite3ExplainEx
2a142 70 72 28 70 4f 75 74 2c 20 70 45 78 70 72 2d 3e  pr(pOut, pExpr->
2a143 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 73 71  pLeft);.      sq
2a144 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e  lite3ExplainPrin
2a145 74 66 28 70 4f 75 74 2c 20 22 2c 22 29 3b 0a 20  tf(pOut, ",");. 
2a146 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c       sqlite3Expl
2a147 61 69 6e 45 78 70 72 4c 69 73 74 28 70 4f 75 74  ainExprList(pOut
2a148 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  , pExpr->x.pList
2a149 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
2a14a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
2a14b 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
2a14c 52 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 41  R.    case TK_RA
2a14d 49 53 45 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e  ISE: {.      con
2a14e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 3d  st char *zType =
2a14f 20 22 75 6e 6b 22 3b 0a 20 20 20 20 20 20 73 77   "unk";.      sw
2a150 69 74 63 68 28 20 70 45 78 70 72 2d 3e 61 66 66  itch( pExpr->aff
2a151 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 20  inity ){.       
2a152 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63   case OE_Rollbac
2a153 6b 3a 20 20 20 7a 54 79 70 65 20 3d 20 22 72 6f  k:   zType = "ro
2a154 6c 6c 62 61 63 6b 22 3b 20 20 62 72 65 61 6b 3b  llback";  break;
2a155 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 4f 45  .        case OE
2a156 5f 41 62 6f 72 74 3a 20 20 20 20 20 20 7a 54 79  _Abort:      zTy
2a157 70 65 20 3d 20 22 61 62 6f 72 74 22 3b 20 20 20  pe = "abort";   
2a158 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
2a159 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20 20   case OE_Fail:  
2a15a 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 66 61       zType = "fa
2a15b 69 6c 22 3b 20 20 20 20 20 20 62 72 65 61 6b 3b  il";      break;
2a15c 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 4f 45  .        case OE
2a15d 5f 49 67 6e 6f 72 65 3a 20 20 20 20 20 7a 54 79  _Ignore:     zTy
2a15e 70 65 20 3d 20 22 69 67 6e 6f 72 65 22 3b 20 20  pe = "ignore";  
2a15f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2a160 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2a161 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f 75 74  plainPrintf(pOut
2a162 2c 20 22 52 41 49 53 45 2d 25 73 28 25 73 29 22  , "RAISE-%s(%s)"
2a163 2c 20 7a 54 79 70 65 2c 20 70 45 78 70 72 2d 3e  , zType, pExpr->
2a164 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20  u.zToken);.     
2a165 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
2a166 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 7a  ndif.  }.  if( z
2a167 42 69 6e 4f 70 20 29 7b 0a 20 20 20 20 73 71 6c  BinOp ){.    sql
2a168 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74  ite3ExplainPrint
2a169 66 28 70 4f 75 74 2c 22 25 73 28 22 2c 20 7a 42  f(pOut,"%s(", zB
2a16a 69 6e 4f 70 29 3b 0a 20 20 20 20 73 71 6c 69 74  inOp);.    sqlit
2a16b 65 33 45 78 70 6c 61 69 6e 45 78 70 72 28 70 4f  e3ExplainExpr(pO
2a16c 75 74 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  ut, pExpr->pLeft
2a16d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
2a16e 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f 75 74  plainPrintf(pOut
2a16f 2c 22 2c 22 29 3b 0a 20 20 20 20 73 71 6c 69 74  ,",");.    sqlit
2a170 65 33 45 78 70 6c 61 69 6e 45 78 70 72 28 70 4f  e3ExplainExpr(pO
2a171 75 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  ut, pExpr->pRigh
2a172 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  t);.    sqlite3E
2a173 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 4f 75  xplainPrintf(pOu
2a174 74 2c 22 29 22 29 3b 0a 20 20 7d 65 6c 73 65 20  t,")");.  }else 
2a175 69 66 28 20 7a 55 6e 69 4f 70 20 29 7b 0a 20 20  if( zUniOp ){.  
2a176 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
2a177 50 72 69 6e 74 66 28 70 4f 75 74 2c 22 25 73 28  Printf(pOut,"%s(
2a178 22 2c 20 7a 55 6e 69 4f 70 29 3b 0a 20 20 20 20  ", zUniOp);.    
2a179 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78  sqlite3ExplainEx
2a17a 70 72 28 70 4f 75 74 2c 20 70 45 78 70 72 2d 3e  pr(pOut, pExpr->
2a17b 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71 6c 69  pLeft);.    sqli
2a17c 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66  te3ExplainPrintf
2a17d 28 70 4f 75 74 2c 22 29 22 29 3b 0a 20 20 7d 0a  (pOut,")");.  }.
2a17e 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69  }.#endif /* defi
2a17f 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
2a180 45 5f 54 52 45 45 5f 45 58 50 4c 41 49 4e 29 20  E_TREE_EXPLAIN) 
2a181 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  */..#if defined(
2a182 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 54 52  SQLITE_ENABLE_TR
2a183 45 45 5f 45 58 50 4c 41 49 4e 29 0a 2f 2a 0a 2a  EE_EXPLAIN)./*.*
2a184 2a 20 47 65 6e 65 72 61 74 65 20 61 20 68 75 6d  * Generate a hum
2a185 61 6e 2d 72 65 61 64 61 62 6c 65 20 65 78 70 6c  an-readable expl
2a186 61 6e 61 74 69 6f 6e 20 6f 66 20 61 6e 20 65 78  anation of an ex
2a187 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a  pression list..*
2a188 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2a189 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
2a18a 6c 61 69 6e 45 78 70 72 4c 69 73 74 28 56 64 62  lainExprList(Vdb
2a18b 65 20 2a 70 4f 75 74 2c 20 45 78 70 72 4c 69 73  e *pOut, ExprLis
2a18c 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74  t *pList){.  int
2a18d 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d   i;.  if( pList=
2a18e 3d 30 20 7c 7c 20 70 4c 69 73 74 2d 3e 6e 45 78  =0 || pList->nEx
2a18f 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  pr==0 ){.    sql
2a190 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74  ite3ExplainPrint
2a191 66 28 70 4f 75 74 2c 20 22 28 65 6d 70 74 79 2d  f(pOut, "(empty-
2a192 6c 69 73 74 29 22 29 3b 0a 20 20 20 20 72 65 74  list)");.    ret
2a193 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  urn;.  }else if(
2a194 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 31   pList->nExpr==1
2a195 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2a196 78 70 6c 61 69 6e 45 78 70 72 28 70 4f 75 74 2c  xplainExpr(pOut,
2a197 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78   pList->a[0].pEx
2a198 70 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  pr);.  }else{.  
2a199 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
2a19a 50 75 73 68 28 70 4f 75 74 29 3b 0a 20 20 20 20  Push(pOut);.    
2a19b 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74  for(i=0; i<pList
2a19c 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
2a19d 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c       sqlite3Expl
2a19e 61 69 6e 50 72 69 6e 74 66 28 70 4f 75 74 2c 20  ainPrintf(pOut, 
2a19f 22 69 74 65 6d 5b 25 64 5d 20 3d 20 22 2c 20 69  "item[%d] = ", i
2a1a0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2a1a1 45 78 70 6c 61 69 6e 50 75 73 68 28 70 4f 75 74  ExplainPush(pOut
2a1a2 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2a1a3 45 78 70 6c 61 69 6e 45 78 70 72 28 70 4f 75 74  ExplainExpr(pOut
2a1a4 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  , pList->a[i].pE
2a1a5 78 70 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  xpr);.      sqli
2a1a6 74 65 33 45 78 70 6c 61 69 6e 50 6f 70 28 70 4f  te3ExplainPop(pO
2a1a7 75 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69  ut);.      if( i
2a1a8 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20  <pList->nExpr-1 
2a1a9 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2a1aa 65 33 45 78 70 6c 61 69 6e 4e 4c 28 70 4f 75 74  e3ExplainNL(pOut
2a1ab 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2a1ac 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c  .    sqlite3Expl
2a1ad 61 69 6e 50 6f 70 28 70 4f 75 74 29 3b 0a 20 20  ainPop(pOut);.  
2a1ae 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  }.}.#endif /* SQ
2a1af 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 2f  LITE_DEBUG */../
2a1b0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
2a1b1 20 69 66 20 70 45 78 70 72 20 69 73 20 61 6e 20   if pExpr is an 
2a1b2 63 6f 6e 73 74 61 6e 74 20 65 78 70 72 65 73 73  constant express
2a1b3 69 6f 6e 20 74 68 61 74 20 69 73 20 61 70 70 72  ion that is appr
2a1b4 6f 70 72 69 61 74 65 0a 2a 2a 20 66 6f 72 20 66  opriate.** for f
2a1b5 61 63 74 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20  actoring out of 
2a1b6 61 20 6c 6f 6f 70 2e 20 20 41 70 70 72 6f 70 72  a loop.  Appropr
2a1b7 69 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73  iate expressions
2a1b8 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a   are:.**.**    *
2a1b9 20 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e    Any expression
2a1ba 20 74 68 61 74 20 65 76 61 6c 75 61 74 65 73 20   that evaluates 
2a1bb 74 6f 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f  to two or more o
2a1bc 70 63 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20  pcodes..**.**   
2a1bd 20 2a 20 20 41 6e 79 20 4f 50 5f 49 6e 74 65 67   *  Any OP_Integ
2a1be 65 72 2c 20 4f 50 5f 52 65 61 6c 2c 20 4f 50 5f  er, OP_Real, OP_
2a1bf 53 74 72 69 6e 67 2c 20 4f 50 5f 42 6c 6f 62 2c  String, OP_Blob,
2a1c0 20 4f 50 5f 4e 75 6c 6c 2c 20 0a 2a 2a 20 20 20   OP_Null, .**   
2a1c1 20 20 20 20 6f 72 20 4f 50 5f 56 61 72 69 61 62      or OP_Variab
2a1c2 6c 65 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74  le that does not
2a1c3 20 6e 65 65 64 20 74 6f 20 62 65 20 70 6c 61 63   need to be plac
2a1c4 65 64 20 69 6e 20 61 20 0a 2a 2a 20 20 20 20 20  ed in a .**     
2a1c5 20 20 73 70 65 63 69 66 69 63 20 72 65 67 69 73    specific regis
2a1c6 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  ter..**.** There
2a1c7 20 69 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20   is no point in 
2a1c8 66 61 63 74 6f 72 69 6e 67 20 6f 75 74 20 73 69  factoring out si
2a1c9 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e  ngle-instruction
2a1ca 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 65 78 70   constant.** exp
2a1cb 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 6e 65  ressions that ne
2a1cc 65 64 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20  ed to be placed 
2a1cd 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  in a particular 
2a1ce 72 65 67 69 73 74 65 72 2e 20 20 0a 2a 2a 20 57  register.  .** W
2a1cf 65 20 63 6f 75 6c 64 20 66 61 63 74 6f 72 20 74  e could factor t
2a1d0 68 65 6d 20 6f 75 74 2c 20 62 75 74 20 74 68 65  hem out, but the
2a1d1 6e 20 77 65 20 77 6f 75 6c 64 20 65 6e 64 20 75  n we would end u
2a1d2 70 20 61 64 64 69 6e 67 20 61 6e 0a 2a 2a 20 4f  p adding an.** O
2a1d3 50 5f 53 43 6f 70 79 20 69 6e 73 74 72 75 63 74  P_SCopy instruct
2a1d4 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 74 68 65 20  ion to move the 
2a1d5 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 65 20 63  value into the c
2a1d6 6f 72 72 65 63 74 20 72 65 67 69 73 74 65 72 0a  orrect register.
2a1d7 2a 2a 20 6c 61 74 65 72 2e 20 20 57 65 20 6d 69  ** later.  We mi
2a1d8 67 68 74 20 61 73 20 77 65 6c 6c 20 6a 75 73 74  ght as well just
2a1d9 20 75 73 65 20 74 68 65 20 6f 72 69 67 69 6e 61   use the origina
2a1da 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e  l instruction an
2a1db 64 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 4f  d.** avoid the O
2a1dc 50 5f 53 43 6f 70 79 2e 0a 2a 2f 0a 73 74 61 74  P_SCopy..*/.stat
2a1dd 69 63 20 69 6e 74 20 69 73 41 70 70 72 6f 70 72  ic int isAppropr
2a1de 69 61 74 65 46 6f 72 46 61 63 74 6f 72 69 6e 67  iateForFactoring
2a1df 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28  (Expr *p){.  if(
2a1e0 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 43   !sqlite3ExprIsC
2a1e1 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70  onstantNotJoin(p
2a1e2 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
2a1e3 30 3b 20 20 2f 2a 20 4f 6e 6c 79 20 63 6f 6e 73  0;  /* Only cons
2a1e4 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73  tant expressions
2a1e5 20 61 72 65 20 61 70 70 72 6f 70 72 69 61 74 65   are appropriate
2a1e6 20 66 6f 72 20 66 61 63 74 6f 72 69 6e 67 20 2a   for factoring *
2a1e7 2f 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e  /.  }.  if( (p->
2a1e8 66 6c 61 67 73 20 26 20 45 50 5f 46 69 78 65 64  flags & EP_Fixed
2a1e9 44 65 73 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Dest)==0 ){.    
2a1ea 72 65 74 75 72 6e 20 31 3b 20 20 2f 2a 20 41 6e  return 1;  /* An
2a1eb 79 20 63 6f 6e 73 74 61 6e 74 20 77 69 74 68 6f  y constant witho
2a1ec 75 74 20 61 20 66 69 78 65 64 20 64 65 73 74 69  ut a fixed desti
2a1ed 6e 61 74 69 6f 6e 20 69 73 20 61 70 70 72 6f 70  nation is approp
2a1ee 72 69 61 74 65 20 2a 2f 0a 20 20 7d 0a 20 20 77  riate */.  }.  w
2a1ef 68 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  hile( p->op==TK_
2a1f0 55 50 4c 55 53 20 29 20 70 20 3d 20 70 2d 3e 70  UPLUS ) p = p->p
2a1f1 4c 65 66 74 3b 0a 20 20 73 77 69 74 63 68 28 20  Left;.  switch( 
2a1f2 70 2d 3e 6f 70 20 29 7b 0a 23 69 66 6e 64 65 66  p->op ){.#ifndef
2a1f3 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f   SQLITE_OMIT_BLO
2a1f4 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61  B_LITERAL.    ca
2a1f5 73 65 20 54 4b 5f 42 4c 4f 42 3a 0a 23 65 6e 64  se TK_BLOB:.#end
2a1f6 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 56  if.    case TK_V
2a1f7 41 52 49 41 42 4c 45 3a 0a 20 20 20 20 63 61 73  ARIABLE:.    cas
2a1f8 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 0a 20 20  e TK_INTEGER:.  
2a1f9 20 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a    case TK_FLOAT:
2a1fa 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c  .    case TK_NUL
2a1fb 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  L:.    case TK_S
2a1fc 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 74  TRING: {.      t
2a1fd 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d  estcase( p->op==
2a1fe 54 4b 5f 42 4c 4f 42 20 29 3b 0a 20 20 20 20 20  TK_BLOB );.     
2a1ff 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70   testcase( p->op
2a200 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45 20 29 3b  ==TK_VARIABLE );
2a201 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2a202 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47   p->op==TK_INTEG
2a203 45 52 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ER );.      test
2a204 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  case( p->op==TK_
2a205 46 4c 4f 41 54 20 29 3b 0a 20 20 20 20 20 20 74  FLOAT );.      t
2a206 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d  estcase( p->op==
2a207 54 4b 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20  TK_NULL );.     
2a208 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70   testcase( p->op
2a209 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 29 3b 0a 20  ==TK_STRING );. 
2a20a 20 20 20 20 20 2f 2a 20 53 69 6e 67 6c 65 2d 69       /* Single-i
2a20b 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74  nstruction const
2a20c 61 6e 74 73 20 77 69 74 68 20 61 20 66 69 78 65  ants with a fixe
2a20d 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 61 72  d destination ar
2a20e 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 74 74 65  e.      ** bette
2a20f 72 20 64 6f 6e 65 20 69 6e 2d 6c 69 6e 65 2e 20  r done in-line. 
2a210 20 49 66 20 77 65 20 66 61 63 74 6f 72 20 74 68   If we factor th
2a211 65 6d 2c 20 74 68 65 79 20 77 69 6c 6c 20 6a 75  em, they will ju
2a212 73 74 20 65 6e 64 0a 20 20 20 20 20 20 2a 2a 20  st end.      ** 
2a213 75 70 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e  up generating an
2a214 20 4f 50 5f 53 43 6f 70 79 20 74 6f 20 6d 6f 76   OP_SCopy to mov
2a215 65 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 74  e the value to t
2a216 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20  he destination. 
2a217 20 20 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72       ** register
2a218 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72  . */.      retur
2a219 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 0;.    }.    c
2a21a 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b  ase TK_UMINUS: {
2a21b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4c  .      if( p->pL
2a21c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41  eft->op==TK_FLOA
2a21d 54 20 7c 7c 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f  T || p->pLeft->o
2a21e 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b  p==TK_INTEGER ){
2a21f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2a220 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
2a221 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2a222 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
2a223 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2a224 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a    }.  return 1;.
2a225 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 45 78 70  }../*.** If pExp
2a226 72 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20  r is a constant 
2a227 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
2a228 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  is appropriate f
2a229 6f 72 0a 2a 2a 20 66 61 63 74 6f 72 69 6e 67 20  or.** factoring 
2a22a 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2c 20 74  out of a loop, t
2a22b 68 65 6e 20 65 76 61 6c 75 61 74 65 20 74 68 65  hen evaluate the
2a22c 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69   expression.** i
2a22d 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 20 61  nto a register a
2a22e 6e 64 20 63 6f 6e 76 65 72 74 20 74 68 65 20 65  nd convert the e
2a22f 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61  xpression into a
2a230 20 54 4b 5f 52 45 47 49 53 54 45 52 0a 2a 2a 20   TK_REGISTER.** 
2a231 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73  expression..*/.s
2a232 74 61 74 69 63 20 69 6e 74 20 65 76 61 6c 43 6f  tatic int evalCo
2a233 6e 73 74 45 78 70 72 28 57 61 6c 6b 65 72 20 2a  nstExpr(Walker *
2a234 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70  pWalker, Expr *p
2a235 45 78 70 72 29 7b 0a 20 20 50 61 72 73 65 20 2a  Expr){.  Parse *
2a236 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72  pParse = pWalker
2a237 2d 3e 70 50 61 72 73 65 3b 0a 20 20 73 77 69 74  ->pParse;.  swit
2a238 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b  ch( pExpr->op ){
2a239 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a  .    case TK_IN:
2a23a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47  .    case TK_REG
2a23b 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 72  ISTER: {.      r
2a23c 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b  eturn WRC_Prune;
2a23d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2a23e 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20  TK_FUNCTION:.   
2a23f 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e   case TK_AGG_FUN
2a240 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20  CTION:.    case 
2a241 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 20 7b  TK_CONST_FUNC: {
2a242 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 72  .      /* The ar
2a243 67 75 6d 65 6e 74 73 20 74 6f 20 61 20 66 75 6e  guments to a fun
2a244 63 74 69 6f 6e 20 68 61 76 65 20 61 20 66 69 78  ction have a fix
2a245 65 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a  ed destination..
2a246 20 20 20 20 20 20 2a 2a 20 4d 61 72 6b 20 74 68        ** Mark th
2a247 65 6d 20 74 68 69 73 20 77 61 79 20 74 6f 20 61  em this way to a
2a248 76 6f 69 64 20 67 65 6e 65 72 61 74 65 64 20 75  void generated u
2a249 6e 6e 65 65 64 65 64 20 4f 50 5f 53 43 6f 70 79  nneeded OP_SCopy
2a24a 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 72 75  .      ** instru
2a24b 63 74 69 6f 6e 73 2e 20 0a 20 20 20 20 20 20 2a  ctions. .      *
2a24c 2f 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74  /.      ExprList
2a24d 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d   *pList = pExpr-
2a24e 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20  >x.pList;.      
2a24f 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2a250 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
2a251 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
2a252 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74  .      if( pList
2a253 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
2a254 69 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  i = pList->nExpr
2a255 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74  ;.        struct
2a256 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
2a257 70 49 74 65 6d 20 3d 20 70 4c 69 73 74 2d 3e 61  pItem = pList->a
2a258 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20  ;.        for(; 
2a259 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b  i>0; i--, pItem+
2a25a 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
2a25b 28 20 41 4c 57 41 59 53 28 70 49 74 65 6d 2d 3e  ( ALWAYS(pItem->
2a25c 70 45 78 70 72 29 20 29 20 70 49 74 65 6d 2d 3e  pExpr) ) pItem->
2a25d 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20  pExpr->flags |= 
2a25e 45 50 5f 46 69 78 65 64 44 65 73 74 3b 0a 20 20  EP_FixedDest;.  
2a25f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2a260 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2a261 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 41   }.  }.  if( isA
2a262 70 70 72 6f 70 72 69 61 74 65 46 6f 72 46 61 63  ppropriateForFac
2a263 74 6f 72 69 6e 67 28 70 45 78 70 72 29 20 29 7b  toring(pExpr) ){
2a264 0a 20 20 20 20 69 6e 74 20 72 31 20 3d 20 2b 2b  .    int r1 = ++
2a265 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2a266 20 20 69 6e 74 20 72 32 3b 0a 20 20 20 20 72 32    int r2;.    r2
2a267 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2a268 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c  deTarget(pParse,
2a269 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20 20 20   pExpr, r1);.   
2a26a 20 69 66 28 20 4e 45 56 45 52 28 72 31 21 3d 72   if( NEVER(r1!=r
2a26b 32 29 20 29 20 73 71 6c 69 74 65 33 52 65 6c 65  2) ) sqlite3Rele
2a26c 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2a26d 65 2c 20 72 31 29 3b 0a 20 20 20 20 70 45 78 70  e, r1);.    pExp
2a26e 72 2d 3e 6f 70 32 20 3d 20 70 45 78 70 72 2d 3e  r->op2 = pExpr->
2a26f 6f 70 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f  op;.    pExpr->o
2a270 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b  p = TK_REGISTER;
2a271 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62  .    pExpr->iTab
2a272 6c 65 20 3d 20 72 32 3b 0a 20 20 20 20 72 65 74  le = r2;.    ret
2a273 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
2a274 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f   }.  return WRC_
2a275 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a  Continue;.}../*.
2a276 2a 2a 20 50 72 65 65 76 61 6c 75 61 74 65 20 63  ** Preevaluate c
2a277 6f 6e 73 74 61 6e 74 20 73 75 62 65 78 70 72 65  onstant subexpre
2a278 73 73 69 6f 6e 73 20 77 69 74 68 69 6e 20 70 45  ssions within pE
2a279 78 70 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68  xpr and store th
2a27a 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 20  e.** results in 
2a27b 72 65 67 69 73 74 65 72 73 2e 20 20 4d 6f 64 69  registers.  Modi
2a27c 66 79 20 70 45 78 70 72 20 73 6f 20 74 68 61 74  fy pExpr so that
2a27d 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 73 75   the constant su
2a27e 62 65 78 70 72 65 73 69 6f 6e 73 0a 2a 2a 20 61  bexpresions.** a
2a27f 72 65 20 54 4b 5f 52 45 47 49 53 54 45 52 20 6f  re TK_REGISTER o
2a280 70 63 6f 64 65 73 20 74 68 61 74 20 72 65 66 65  pcodes that refe
2a281 72 20 74 6f 20 74 68 65 20 70 72 65 63 6f 6d 70  r to the precomp
2a282 75 74 65 64 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a  uted values..**.
2a283 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2a284 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68  is a no-op if th
2a285 65 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 63 6f  e jump to the co
2a286 6f 6b 69 65 2d 63 68 65 63 6b 20 63 6f 64 65 20  okie-check code 
2a287 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 6f  has.** already o
2a288 63 63 75 72 2e 20 20 53 69 6e 63 65 20 74 68 65  ccur.  Since the
2a289 20 63 6f 6f 6b 69 65 2d 63 68 65 63 6b 20 6a 75   cookie-check ju
2a28a 6d 70 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  mp is generated 
2a28b 70 72 69 6f 72 20 74 6f 0a 2a 2a 20 61 6e 79 20  prior to.** any 
2a28c 6f 74 68 65 72 20 73 65 72 69 6f 75 73 20 70 72  other serious pr
2a28d 6f 63 65 73 73 69 6e 67 2c 20 74 68 69 73 20 63  ocessing, this c
2a28e 68 65 63 6b 20 65 6e 73 75 72 65 73 20 74 68 61  heck ensures tha
2a28f 74 20 74 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a  t there is no.**
2a290 20 77 61 79 20 74 6f 20 61 63 63 69 64 65 6e 74   way to accident
2a291 6c 79 20 62 79 70 61 73 73 20 74 68 65 20 63 6f  ly bypass the co
2a292 6e 73 74 61 6e 74 20 69 6e 69 74 69 61 6c 69 7a  nstant initializ
2a293 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ations..**.** Th
2a294 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 6c  is routine is al
2a295 73 6f 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68  so a no-op if th
2a296 65 20 53 51 4c 49 54 45 5f 46 61 63 74 6f 72 4f  e SQLITE_FactorO
2a297 75 74 43 6f 6e 73 74 20 6f 70 74 69 6d 69 7a 61  utConst optimiza
2a298 74 69 6f 6e 0a 2a 2a 20 69 73 20 64 69 73 61 62  tion.** is disab
2a299 6c 65 64 20 76 69 61 20 74 68 65 20 73 71 6c 69  led via the sqli
2a29a 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
2a29b 28 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c  (SQLITE_TESTCTRL
2a29c 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 53 29 0a  _OPTIMIZATIONS).
2a29d 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 20 54  ** interface.  T
2a29e 68 69 73 20 61 6c 6c 6f 77 73 20 74 65 73 74 20  his allows test 
2a29f 6c 6f 67 69 63 20 74 6f 20 76 65 72 69 66 79 20  logic to verify 
2a2a0 74 68 61 74 20 74 68 65 20 73 61 6d 65 20 61 6e  that the same an
2a2a1 73 77 65 72 20 69 73 0a 2a 2a 20 6f 62 74 61 69  swer is.** obtai
2a2a2 6e 65 64 20 66 6f 72 20 71 75 65 72 69 65 73 20  ned for queries 
2a2a3 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68  regardless of wh
2a2a4 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 63 6f 6e  ether or not con
2a2a5 73 74 61 6e 74 73 20 61 72 65 0a 2a 2a 20 70 72  stants are.** pr
2a2a6 65 63 6f 6d 70 75 74 65 64 20 69 6e 74 6f 20 72  ecomputed into r
2a2a7 65 67 69 73 74 65 72 73 20 6f 72 20 69 66 20 74  egisters or if t
2a2a8 68 65 79 20 61 72 65 20 69 6e 73 65 72 74 65 64  hey are inserted
2a2a9 20 69 6e 2d 6c 69 6e 65 2e 0a 2a 2f 0a 53 51 4c   in-line..*/.SQL
2a2aa 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2a2ab 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2a2ac 43 6f 6e 73 74 61 6e 74 73 28 50 61 72 73 65 20  Constants(Parse 
2a2ad 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
2a2ae 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b 65 72 20  Expr){.  Walker 
2a2af 77 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  w;.  if( pParse-
2a2b0 3e 63 6f 6f 6b 69 65 47 6f 74 6f 20 29 20 72 65  >cookieGoto ) re
2a2b1 74 75 72 6e 3b 0a 20 20 69 66 28 20 4f 70 74 69  turn;.  if( Opti
2a2b2 6d 69 7a 61 74 69 6f 6e 44 69 73 61 62 6c 65 64  mizationDisabled
2a2b3 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 53 51 4c  (pParse->db, SQL
2a2b4 49 54 45 5f 46 61 63 74 6f 72 4f 75 74 43 6f 6e  ITE_FactorOutCon
2a2b5 73 74 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  st) ) return;.  
2a2b6 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20  w.xExprCallback 
2a2b7 3d 20 65 76 61 6c 43 6f 6e 73 74 45 78 70 72 3b  = evalConstExpr;
2a2b8 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c  .  w.xSelectCall
2a2b9 62 61 63 6b 20 3d 20 30 3b 0a 20 20 77 2e 70 50  back = 0;.  w.pP
2a2ba 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
2a2bb 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
2a2bc 28 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a  (&w, pExpr);.}..
2a2bd 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
2a2be 63 6f 64 65 20 74 68 61 74 20 70 75 73 68 65 73  code that pushes
2a2bf 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 65 76   the value of ev
2a2c0 65 72 79 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  ery element of t
2a2c1 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72  he given.** expr
2a2c2 65 73 73 69 6f 6e 20 6c 69 73 74 20 69 6e 74 6f  ession list into
2a2c3 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 72   a sequence of r
2a2c4 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69  egisters beginni
2a2c5 6e 67 20 61 74 20 74 61 72 67 65 74 2e 0a 2a 2a  ng at target..**
2a2c6 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
2a2c7 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74  umber of element
2a2c8 73 20 65 76 61 6c 75 61 74 65 64 2e 0a 2a 2f 0a  s evaluated..*/.
2a2c9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2a2ca 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
2a2cb 64 65 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61  deExprList(.  Pa
2a2cc 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2a2cd 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
2a2ce 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ext */.  ExprLis
2a2cf 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 54  t *pList,   /* T
2a2d0 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  he expression li
2a2d1 73 74 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a  st to be coded *
2a2d2 2f 0a 20 20 69 6e 74 20 74 61 72 67 65 74 2c 20  /.  int target, 
2a2d3 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20         /* Where 
2a2d4 74 6f 20 77 72 69 74 65 20 72 65 73 75 6c 74 73  to write results
2a2d5 20 2a 2f 0a 20 20 69 6e 74 20 64 6f 48 61 72 64   */.  int doHard
2a2d6 43 6f 70 79 20 20 20 20 20 2f 2a 20 4d 61 6b 65  Copy     /* Make
2a2d7 20 61 20 68 61 72 64 20 63 6f 70 79 20 6f 66 20   a hard copy of 
2a2d8 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20 2a 2f  every element */
2a2d9 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 45 78 70  .){.  struct Exp
2a2da 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
2a2db 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20  m;.  int i, n;. 
2a2dc 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d   assert( pList!=
2a2dd 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74  0 );.  assert( t
2a2de 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 61 73 73  arget>0 );.  ass
2a2df 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 56 64  ert( pParse->pVd
2a2e0 62 65 21 3d 30 20 29 3b 20 20 2f 2a 20 4e 65 76  be!=0 );  /* Nev
2a2e1 65 72 20 67 65 74 73 20 74 68 69 73 20 66 61 72  er gets this far
2a2e2 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20   otherwise */.  
2a2e3 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  n = pList->nExpr
2a2e4 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c  ;.  for(pItem=pL
2a2e5 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 6e  ist->a, i=0; i<n
2a2e6 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
2a2e7 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72  .    Expr *pExpr
2a2e8 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b   = pItem->pExpr;
2a2e9 0a 20 20 20 20 69 6e 74 20 69 6e 52 65 67 20 3d  .    int inReg =
2a2ea 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2a2eb 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70  Target(pParse, p
2a2ec 45 78 70 72 2c 20 74 61 72 67 65 74 2b 69 29 3b  Expr, target+i);
2a2ed 0a 20 20 20 20 69 66 28 20 69 6e 52 65 67 21 3d  .    if( inReg!=
2a2ee 74 61 72 67 65 74 2b 69 20 29 7b 0a 20 20 20 20  target+i ){.    
2a2ef 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a2f0 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62  Op2(pParse->pVdb
2a2f1 65 2c 20 64 6f 48 61 72 64 43 6f 70 79 20 3f 20  e, doHardCopy ? 
2a2f2 4f 50 5f 43 6f 70 79 20 3a 20 4f 50 5f 53 43 6f  OP_Copy : OP_SCo
2a2f3 70 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  py,.            
2a2f4 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 52 65              inRe
2a2f5 67 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20 20  g, target+i);.  
2a2f6 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2a2f7 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e   n;.}../*.** Gen
2a2f8 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61  erate code for a
2a2f9 20 42 45 54 57 45 45 4e 20 6f 70 65 72 61 74 6f   BETWEEN operato
2a2fa 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 78 20 42 45  r..**.**    x BE
2a2fb 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 2a 2a  TWEEN y AND z.**
2a2fc 0a 2a 2a 20 54 68 65 20 61 62 6f 76 65 20 69 73  .** The above is
2a2fd 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a   equivalent to .
2a2fe 2a 2a 0a 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e  **.**    x>=y AN
2a2ff 44 20 78 3c 3d 7a 0a 2a 2a 0a 2a 2a 20 43 6f 64  D x<=z.**.** Cod
2a300 65 20 69 74 20 61 73 20 73 75 63 68 2c 20 74 61  e it as such, ta
2a301 6b 69 6e 67 20 63 61 72 65 20 74 6f 20 64 6f 20  king care to do 
2a302 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 75 62 65 78  the common subex
2a303 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 65 6c 65 6d  pression.** elem
2a304 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 2e 0a 2a  entation of x..*
2a305 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78  /.static void ex
2a306 70 72 43 6f 64 65 42 65 74 77 65 65 6e 28 0a 20  prCodeBetween(. 
2a307 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2a308 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e     /* Parsing an
2a309 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  d code generatin
2a30a 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
2a30b 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20  xpr *pExpr,     
2a30c 20 2f 2a 20 54 68 65 20 42 45 54 57 45 45 4e 20   /* The BETWEEN 
2a30d 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
2a30e 69 6e 74 20 64 65 73 74 2c 20 20 20 20 20 20 20  int dest,       
2a30f 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69    /* Jump here i
2a310 66 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61  f the jump is ta
2a311 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d  ken */.  int jum
2a312 70 49 66 54 72 75 65 2c 20 20 20 2f 2a 20 54 61  pIfTrue,   /* Ta
2a313 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 74  ke the jump if t
2a314 68 65 20 42 45 54 57 45 45 4e 20 69 73 20 74 72  he BETWEEN is tr
2a315 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d 70  ue */.  int jump
2a316 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 54 61 6b  IfNull    /* Tak
2a317 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 74 68  e the jump if th
2a318 65 20 42 45 54 57 45 45 4e 20 69 73 20 4e 55 4c  e BETWEEN is NUL
2a319 4c 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 65  L */.){.  Expr e
2a31a 78 70 72 41 6e 64 3b 20 20 20 20 20 2f 2a 20 54  xprAnd;     /* T
2a31b 68 65 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 20  he AND operator 
2a31c 69 6e 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d  in  x>=y AND x<=
2a31d 7a 20 20 2a 2f 0a 20 20 45 78 70 72 20 63 6f 6d  z  */.  Expr com
2a31e 70 4c 65 66 74 3b 20 20 20 20 2f 2a 20 54 68 65  pLeft;    /* The
2a31f 20 20 78 3e 3d 79 20 20 74 65 72 6d 20 2a 2f 0a    x>=y  term */.
2a320 20 20 45 78 70 72 20 63 6f 6d 70 52 69 67 68 74    Expr compRight
2a321 3b 20 20 20 2f 2a 20 54 68 65 20 20 78 3c 3d 7a  ;   /* The  x<=z
2a322 20 20 74 65 72 6d 20 2a 2f 0a 20 20 45 78 70 72    term */.  Expr
2a323 20 65 78 70 72 58 3b 20 20 20 20 20 20 20 2f 2a   exprX;       /*
2a324 20 54 68 65 20 20 78 20 20 73 75 62 65 78 70 72   The  x  subexpr
2a325 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ession */.  int 
2a326 72 65 67 46 72 65 65 31 20 3d 20 30 3b 20 2f 2a  regFree1 = 0; /*
2a327 20 54 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72   Temporary use r
2a328 65 67 69 73 74 65 72 20 2a 2f 0a 0a 20 20 61 73  egister */..  as
2a329 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
2a32a 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
2a32b 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20  _xIsSelect) );. 
2a32c 20 65 78 70 72 58 20 3d 20 2a 70 45 78 70 72 2d   exprX = *pExpr-
2a32d 3e 70 4c 65 66 74 3b 0a 20 20 65 78 70 72 41 6e  >pLeft;.  exprAn
2a32e 64 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20  d.op = TK_AND;. 
2a32f 20 65 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d   exprAnd.pLeft =
2a330 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 65 78   &compLeft;.  ex
2a331 70 72 41 6e 64 2e 70 52 69 67 68 74 20 3d 20 26  prAnd.pRight = &
2a332 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 63 6f 6d  compRight;.  com
2a333 70 4c 65 66 74 2e 6f 70 20 3d 20 54 4b 5f 47 45  pLeft.op = TK_GE
2a334 3b 0a 20 20 63 6f 6d 70 4c 65 66 74 2e 70 4c 65  ;.  compLeft.pLe
2a335 66 74 20 3d 20 26 65 78 70 72 58 3b 0a 20 20 63  ft = &exprX;.  c
2a336 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68 74 20 3d  ompLeft.pRight =
2a337 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d   pExpr->x.pList-
2a338 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 63  >a[0].pExpr;.  c
2a339 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20 54 4b  ompRight.op = TK
2a33a 5f 4c 45 3b 0a 20 20 63 6f 6d 70 52 69 67 68 74  _LE;.  compRight
2a33b 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b  .pLeft = &exprX;
2a33c 0a 20 20 63 6f 6d 70 52 69 67 68 74 2e 70 52 69  .  compRight.pRi
2a33d 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70  ght = pExpr->x.p
2a33e 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72  List->a[1].pExpr
2a33f 3b 0a 20 20 65 78 70 72 58 2e 69 54 61 62 6c 65  ;.  exprX.iTable
2a340 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2a341 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 26  deTemp(pParse, &
2a342 65 78 70 72 58 2c 20 26 72 65 67 46 72 65 65 31  exprX, &regFree1
2a343 29 3b 0a 20 20 65 78 70 72 58 2e 6f 70 20 3d 20  );.  exprX.op = 
2a344 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 69  TK_REGISTER;.  i
2a345 66 28 20 6a 75 6d 70 49 66 54 72 75 65 20 29 7b  f( jumpIfTrue ){
2a346 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2a347 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 26  IfTrue(pParse, &
2a348 65 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 6a  exprAnd, dest, j
2a349 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 7d 65  umpIfNull);.  }e
2a34a 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
2a34b 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72  ExprIfFalse(pPar
2a34c 73 65 2c 20 26 65 78 70 72 41 6e 64 2c 20 64 65  se, &exprAnd, de
2a34d 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b  st, jumpIfNull);
2a34e 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65  .  }.  sqlite3Re
2a34f 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2a350 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0a  rse, regFree1);.
2a351 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 61 64 65  .  /* Ensure ade
2a352 71 75 61 74 65 20 74 65 73 74 20 63 6f 76 65 72  quate test cover
2a353 61 67 65 20 2a 2f 0a 20 20 74 65 73 74 63 61 73  age */.  testcas
2a354 65 28 20 6a 75 6d 70 49 66 54 72 75 65 3d 3d 30  e( jumpIfTrue==0
2a355 20 26 26 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d   && jumpIfNull==
2a356 30 20 26 26 20 72 65 67 46 72 65 65 31 3d 3d 30  0 && regFree1==0
2a357 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
2a358 6a 75 6d 70 49 66 54 72 75 65 3d 3d 30 20 26 26  jumpIfTrue==0 &&
2a359 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 26   jumpIfNull==0 &
2a35a 26 20 72 65 67 46 72 65 65 31 21 3d 30 20 29 3b  & regFree1!=0 );
2a35b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d  .  testcase( jum
2a35c 70 49 66 54 72 75 65 3d 3d 30 20 26 26 20 6a 75  pIfTrue==0 && ju
2a35d 6d 70 49 66 4e 75 6c 6c 21 3d 30 20 26 26 20 72  mpIfNull!=0 && r
2a35e 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
2a35f 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66  testcase( jumpIf
2a360 54 72 75 65 3d 3d 30 20 26 26 20 6a 75 6d 70 49  True==0 && jumpI
2a361 66 4e 75 6c 6c 21 3d 30 20 26 26 20 72 65 67 46  fNull!=0 && regF
2a362 72 65 65 31 21 3d 30 20 29 3b 0a 20 20 74 65 73  ree1!=0 );.  tes
2a363 74 63 61 73 65 28 20 6a 75 6d 70 49 66 54 72 75  tcase( jumpIfTru
2a364 65 21 3d 30 20 26 26 20 6a 75 6d 70 49 66 4e 75  e!=0 && jumpIfNu
2a365 6c 6c 3d 3d 30 20 26 26 20 72 65 67 46 72 65 65  ll==0 && regFree
2a366 31 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74 63 61  1==0 );.  testca
2a367 73 65 28 20 6a 75 6d 70 49 66 54 72 75 65 21 3d  se( jumpIfTrue!=
2a368 30 20 26 26 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  0 && jumpIfNull=
2a369 3d 30 20 26 26 20 72 65 67 46 72 65 65 31 21 3d  =0 && regFree1!=
2a36a 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  0 );.  testcase(
2a36b 20 6a 75 6d 70 49 66 54 72 75 65 21 3d 30 20 26   jumpIfTrue!=0 &
2a36c 26 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 20  & jumpIfNull!=0 
2a36d 26 26 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  && regFree1==0 )
2a36e 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6a 75  ;.  testcase( ju
2a36f 6d 70 49 66 54 72 75 65 21 3d 30 20 26 26 20 6a  mpIfTrue!=0 && j
2a370 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 20 26 26 20  umpIfNull!=0 && 
2a371 72 65 67 46 72 65 65 31 21 3d 30 20 29 3b 0a 7d  regFree1!=0 );.}
2a372 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2a373 20 63 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c   code for a bool
2a374 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73  ean expression s
2a375 75 63 68 20 74 68 61 74 20 61 20 6a 75 6d 70 20  uch that a jump 
2a376 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68  is made.** to th
2a377 65 20 6c 61 62 65 6c 20 22 64 65 73 74 22 20 69  e label "dest" i
2a378 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
2a379 20 69 73 20 74 72 75 65 20 62 75 74 20 65 78 65   is true but exe
2a37a 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 6e  cution.** contin
2a37b 75 65 73 20 73 74 72 61 69 67 68 74 20 74 68 72  ues straight thr
2a37c 75 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73  u if the express
2a37d 69 6f 6e 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a  ion is false..**
2a37e 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
2a37f 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20  ssion evaluates 
2a380 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72  to NULL (neither
2a381 20 74 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 29   true nor false)
2a382 2c 20 74 68 65 6e 0a 2a 2a 20 74 61 6b 65 20 74  , then.** take t
2a383 68 65 20 6a 75 6d 70 20 69 66 20 74 68 65 20 6a  he jump if the j
2a384 75 6d 70 49 66 4e 75 6c 6c 20 66 6c 61 67 20 69  umpIfNull flag i
2a385 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  s SQLITE_JUMPIFN
2a386 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ULL..**.** This 
2a387 63 6f 64 65 20 64 65 70 65 6e 64 73 20 6f 6e 20  code depends on 
2a388 74 68 65 20 66 61 63 74 20 74 68 61 74 20 63 65  the fact that ce
2a389 72 74 61 69 6e 20 74 6f 6b 65 6e 20 76 61 6c 75  rtain token valu
2a38a 65 73 20 28 65 78 3a 20 54 4b 5f 45 51 29 0a 2a  es (ex: TK_EQ).*
2a38b 2a 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 61  * are the same a
2a38c 73 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 73 20  s opcode values 
2a38d 28 65 78 3a 20 4f 50 5f 45 71 29 20 74 68 61 74  (ex: OP_Eq) that
2a38e 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63   implement the c
2a38f 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20  orresponding.** 
2a390 6f 70 65 72 61 74 69 6f 6e 2e 20 20 53 70 65 63  operation.  Spec
2a391 69 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20  ial comments in 
2a392 76 64 62 65 2e 63 20 61 6e 64 20 74 68 65 20 6d  vdbe.c and the m
2a393 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 72  kopcodeh.awk scr
2a394 69 70 74 20 69 6e 0a 2a 2a 20 74 68 65 20 6d 61  ipt in.** the ma
2a395 6b 65 20 70 72 6f 63 65 73 73 20 63 61 75 73 65  ke process cause
2a396 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 74 6f   these values to
2a397 20 61 6c 69 67 6e 2e 20 20 41 73 73 65 72 74 28   align.  Assert(
2a398 29 73 20 69 6e 20 74 68 65 20 63 6f 64 65 0a 2a  )s in the code.*
2a399 2a 20 62 65 6c 6f 77 20 76 65 72 69 66 79 20 74  * below verify t
2a39a 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 73 20  hat the numbers 
2a39b 61 72 65 20 61 6c 69 67 6e 65 64 20 63 6f 72 72  are aligned corr
2a39c 65 63 74 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ectly..*/.SQLITE
2a39d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2a39e 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
2a39f 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
2a3a0 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20  xpr *pExpr, int 
2a3a1 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49 66  dest, int jumpIf
2a3a2 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a 76  Null){.  Vdbe *v
2a3a3 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2a3a4 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b 0a  ;.  int op = 0;.
2a3a5 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 3d    int regFree1 =
2a3a6 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65   0;.  int regFre
2a3a7 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 31  e2 = 0;.  int r1
2a3a8 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72 74 28  , r2;..  assert(
2a3a9 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51 4c   jumpIfNull==SQL
2a3aa 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 7c  ITE_JUMPIFNULL |
2a3ab 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  | jumpIfNull==0 
2a3ac 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 76  );.  if( NEVER(v
2a3ad 3d 3d 30 29 20 29 20 20 20 20 20 72 65 74 75 72  ==0) )     retur
2a3ae 6e 3b 20 20 2f 2a 20 45 78 69 73 74 61 6e 63 65  n;  /* Existance
2a3af 20 6f 66 20 56 44 42 45 20 63 68 65 63 6b 65 64   of VDBE checked
2a3b0 20 62 79 20 63 61 6c 6c 65 72 20 2a 2f 0a 20 20   by caller */.  
2a3b1 69 66 28 20 4e 45 56 45 52 28 70 45 78 70 72 3d  if( NEVER(pExpr=
2a3b2 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 20 20 2f  =0) ) return;  /
2a3b3 2a 20 4e 6f 20 77 61 79 20 74 68 69 73 20 63 61  * No way this ca
2a3b4 6e 20 68 61 70 70 65 6e 20 2a 2f 0a 20 20 6f 70  n happen */.  op
2a3b5 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20   = pExpr->op;.  
2a3b6 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20  switch( op ){.  
2a3b7 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a 20 7b    case TK_AND: {
2a3b8 0a 20 20 20 20 20 20 69 6e 74 20 64 32 20 3d 20  .      int d2 = 
2a3b9 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2a3ba 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 74  abel(v);.      t
2a3bb 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e  estcase( jumpIfN
2a3bc 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ull==0 );.      
2a3bd 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2a3be 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20  Push(pParse);.  
2a3bf 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
2a3c0 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70  fFalse(pParse, p
2a3c1 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 32 2c  Expr->pLeft, d2,
2a3c2 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53 51 4c 49 54  jumpIfNull^SQLIT
2a3c3 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
2a3c4 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2a3c5 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70  IfTrue(pParse, p
2a3c6 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65  Expr->pRight, de
2a3c7 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b  st, jumpIfNull);
2a3c8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2a3c9 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2a3ca 2c 20 64 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  , d2);.      sql
2a3cb 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70  ite3ExprCachePop
2a3cc 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 20  (pParse, 1);.   
2a3cd 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2a3ce 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a 20      case TK_OR: 
2a3cf 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
2a3d0 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  ( jumpIfNull==0 
2a3d1 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2a3d2 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73  ExprIfTrue(pPars
2a3d3 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
2a3d4 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
2a3d5 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  l);.      sqlite
2a3d6 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72  3ExprIfTrue(pPar
2a3d7 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  se, pExpr->pRigh
2a3d8 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  t, dest, jumpIfN
2a3d9 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ull);.      brea
2a3da 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
2a3db 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20  e TK_NOT: {.    
2a3dc 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
2a3dd 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  IfNull==0 );.   
2a3de 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
2a3df 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45  False(pParse, pE
2a3e0 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74  xpr->pLeft, dest
2a3e1 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
2a3e2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a3e3 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 54  }.    case TK_LT
2a3e4 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 45  :.    case TK_LE
2a3e5 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 54  :.    case TK_GT
2a3e6 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 45  :.    case TK_GE
2a3e7 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 45  :.    case TK_NE
2a3e8 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 51  :.    case TK_EQ
2a3e9 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
2a3ea 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 29  ( TK_LT==OP_Lt )
2a3eb 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2a3ec 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b 0a  TK_LE==OP_Le );.
2a3ed 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
2a3ee 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20  _GT==OP_Gt );.  
2a3ef 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47      assert( TK_G
2a3f0 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20 20  E==OP_Ge );.    
2a3f1 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45 51 3d    assert( TK_EQ=
2a3f2 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 20 20  =OP_Eq );.      
2a3f3 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d 4f  assert( TK_NE==O
2a3f4 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 74 65  P_Ne );.      te
2a3f5 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c  stcase( op==TK_L
2a3f6 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
2a3f7 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29  ase( op==TK_LE )
2a3f8 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2a3f9 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20  ( op==TK_GT );. 
2a3fa 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
2a3fb 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20  p==TK_GE );.    
2a3fc 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
2a3fd 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 74  TK_EQ );.      t
2a3fe 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
2a3ff 4e 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  NE );.      test
2a400 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  case( jumpIfNull
2a401 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 31 20  ==0 );.      r1 
2a402 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
2a403 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45  eTemp(pParse, pE
2a404 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67  xpr->pLeft, &reg
2a405 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 72 32  Free1);.      r2
2a406 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2a407 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
2a408 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72  Expr->pRight, &r
2a409 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20  egFree2);.      
2a40a 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72  codeCompare(pPar
2a40b 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
2a40c 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
2a40d 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20   op,.           
2a40e 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 64         r1, r2, d
2a40f 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  est, jumpIfNull)
2a410 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2a411 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
2a412 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2a413 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a   regFree2==0 );.
2a414 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2a415 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49   }.    case TK_I
2a416 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49  S:.    case TK_I
2a417 53 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 74 65  SNOT: {.      te
2a418 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49  stcase( op==TK_I
2a419 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  S );.      testc
2a41a 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 4f  ase( op==TK_ISNO
2a41b 54 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20  T );.      r1 = 
2a41c 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
2a41d 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
2a41e 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72  r->pLeft, &regFr
2a41f 65 65 31 29 3b 0a 20 20 20 20 20 20 72 32 20 3d  ee1);.      r2 =
2a420 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2a421 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
2a422 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 65 67  pr->pRight, &reg
2a423 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 6f 70  Free2);.      op
2a424 20 3d 20 28 6f 70 3d 3d 54 4b 5f 49 53 29 20 3f   = (op==TK_IS) ?
2a425 20 54 4b 5f 45 51 20 3a 20 54 4b 5f 4e 45 3b 0a   TK_EQ : TK_NE;.
2a426 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72        codeCompar
2a427 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
2a428 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70  >pLeft, pExpr->p
2a429 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20  Right, op,.     
2a42a 20 20 20 20 20 20 20 20 20 20 20 20 20 72 31 2c               r1,
2a42b 20 72 32 2c 20 64 65 73 74 2c 20 53 51 4c 49 54   r2, dest, SQLIT
2a42c 45 5f 4e 55 4c 4c 45 51 29 3b 0a 20 20 20 20 20  E_NULLEQ);.     
2a42d 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
2a42e 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee1==0 );.      
2a42f 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
2a430 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  e2==0 );.      b
2a431 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2a432 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a  case TK_ISNULL:.
2a433 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e      case TK_NOTN
2a434 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ULL: {.      ass
2a435 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d  ert( TK_ISNULL==
2a436 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20  OP_IsNull );.   
2a437 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f     assert( TK_NO
2a438 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c  TNULL==OP_NotNul
2a439 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  l );.      testc
2a43a 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55  ase( op==TK_ISNU
2a43b 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LL );.      test
2a43c 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54  case( op==TK_NOT
2a43d 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72 31  NULL );.      r1
2a43e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2a43f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
2a440 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65  Expr->pLeft, &re
2a441 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73  gFree1);.      s
2a442 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2a443 28 76 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73 74  (v, op, r1, dest
2a444 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2a445 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
2a446 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2a447 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
2a448 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20  _BETWEEN: {.    
2a449 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
2a44a 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  IfNull==0 );.   
2a44b 20 20 20 65 78 70 72 43 6f 64 65 42 65 74 77 65     exprCodeBetwe
2a44c 65 6e 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  en(pParse, pExpr
2a44d 2c 20 64 65 73 74 2c 20 31 2c 20 6a 75 6d 70 49  , dest, 1, jumpI
2a44e 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72  fNull);.      br
2a44f 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  eak;.    }.#ifnd
2a450 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
2a451 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 65  UBQUERY.    case
2a452 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20   TK_IN: {.      
2a453 69 6e 74 20 64 65 73 74 49 66 46 61 6c 73 65 20  int destIfFalse 
2a454 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2a455 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
2a456 20 69 6e 74 20 64 65 73 74 49 66 4e 75 6c 6c 20   int destIfNull 
2a457 3d 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 3f 20 64  = jumpIfNull ? d
2a458 65 73 74 20 3a 20 64 65 73 74 49 66 46 61 6c 73  est : destIfFals
2a459 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e;.      sqlite3
2a45a 45 78 70 72 43 6f 64 65 49 4e 28 70 50 61 72 73  ExprCodeIN(pPars
2a45b 65 2c 20 70 45 78 70 72 2c 20 64 65 73 74 49 66  e, pExpr, destIf
2a45c 46 61 6c 73 65 2c 20 64 65 73 74 49 66 4e 75 6c  False, destIfNul
2a45d 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  l);.      sqlite
2a45e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a45f 50 5f 47 6f 74 6f 2c 20 30 2c 20 64 65 73 74 29  P_Goto, 0, dest)
2a460 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2a461 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2a462 76 2c 20 64 65 73 74 49 66 46 61 6c 73 65 29 3b  v, destIfFalse);
2a463 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2a464 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 64    }.#endif.    d
2a465 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
2a466 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r1 = sqlite3Expr
2a467 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
2a468 20 70 45 78 70 72 2c 20 26 72 65 67 46 72 65 65   pExpr, &regFree
2a469 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
2a46a 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2a46b 50 5f 49 66 2c 20 72 31 2c 20 64 65 73 74 2c 20  P_If, r1, dest, 
2a46c 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b 0a  jumpIfNull!=0);.
2a46d 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2a46e 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
2a46f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
2a470 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
2a471 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2a472 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
2a473 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2a474 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29  Parse, regFree1)
2a475 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  ;.  sqlite3Relea
2a476 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
2a477 2c 20 72 65 67 46 72 65 65 32 29 3b 20 20 0a 7d  , regFree2);  .}
2a478 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2a479 20 63 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c   code for a bool
2a47a 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73  ean expression s
2a47b 75 63 68 20 74 68 61 74 20 61 20 6a 75 6d 70 20  uch that a jump 
2a47c 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68  is made.** to th
2a47d 65 20 6c 61 62 65 6c 20 22 64 65 73 74 22 20 69  e label "dest" i
2a47e 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
2a47f 20 69 73 20 66 61 6c 73 65 20 62 75 74 20 65 78   is false but ex
2a480 65 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69  ecution.** conti
2a481 6e 75 65 73 20 73 74 72 61 69 67 68 74 20 74 68  nues straight th
2a482 72 75 20 69 66 20 74 68 65 20 65 78 70 72 65 73  ru if the expres
2a483 73 69 6f 6e 20 69 73 20 74 72 75 65 2e 0a 2a 2a  sion is true..**
2a484 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
2a485 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20  ssion evaluates 
2a486 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72  to NULL (neither
2a487 20 74 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 29   true nor false)
2a488 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 69 66   then.** jump if
2a489 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 69 73 20 53   jumpIfNull is S
2a48a 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
2a48b 20 6f 72 20 66 61 6c 6c 20 74 68 72 6f 75 67 68   or fall through
2a48c 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 0a 2a   if jumpIfNull.*
2a48d 2a 20 69 73 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54  * is 0..*/.SQLIT
2a48e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2a48f 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
2a490 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
2a491 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e   Expr *pExpr, in
2a492 74 20 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70  t dest, int jump
2a493 49 66 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20  IfNull){.  Vdbe 
2a494 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
2a495 62 65 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30  be;.  int op = 0
2a496 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 31  ;.  int regFree1
2a497 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46   = 0;.  int regF
2a498 72 65 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20  ree2 = 0;.  int 
2a499 72 31 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72  r1, r2;..  asser
2a49a 74 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53  t( jumpIfNull==S
2a49b 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
2a49c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d   || jumpIfNull==
2a49d 30 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52  0 );.  if( NEVER
2a49e 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b  (v==0) ) return;
2a49f 20 2f 2a 20 45 78 69 73 74 61 6e 63 65 20 6f 66   /* Existance of
2a4a0 20 56 44 42 45 20 63 68 65 63 6b 65 64 20 62 79   VDBE checked by
2a4a1 20 63 61 6c 6c 65 72 20 2a 2f 0a 20 20 69 66 28   caller */.  if(
2a4a2 20 70 45 78 70 72 3d 3d 30 20 29 20 20 20 20 72   pExpr==0 )    r
2a4a3 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 54 68 65  eturn;..  /* The
2a4a4 20 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2d   value of pExpr-
2a4a5 3e 6f 70 20 61 6e 64 20 6f 70 20 61 72 65 20 72  >op and op are r
2a4a6 65 6c 61 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77  elated as follow
2a4a7 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  s:.  **.  **    
2a4a8 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 20     pExpr->op    
2a4a9 20 20 20 20 20 20 20 20 6f 70 0a 20 20 2a 2a 20          op.  ** 
2a4aa 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 20        --------- 
2a4ab 20 20 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d           -------
2a4ac 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20 20 20 20 54  ---.  **       T
2a4ad 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20 20 20  K_ISNULL        
2a4ae 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 0a 20 20 2a    OP_NotNull.  *
2a4af 2a 20 20 20 20 20 20 20 54 4b 5f 4e 4f 54 4e 55  *       TK_NOTNU
2a4b0 4c 4c 20 20 20 20 20 20 20 20 20 4f 50 5f 49 73  LL         OP_Is
2a4b1 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 20 20  Null.  **       
2a4b2 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20 20  TK_NE           
2a4b3 20 20 20 4f 50 5f 45 71 0a 20 20 2a 2a 20 20 20     OP_Eq.  **   
2a4b4 20 20 20 20 54 4b 5f 45 51 20 20 20 20 20 20 20      TK_EQ       
2a4b5 20 20 20 20 20 20 20 4f 50 5f 4e 65 0a 20 20 2a         OP_Ne.  *
2a4b6 2a 20 20 20 20 20 20 20 54 4b 5f 47 54 20 20 20  *       TK_GT   
2a4b7 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4c 65             OP_Le
2a4b8 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c  .  **       TK_L
2a4b9 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f  E              O
2a4ba 50 5f 47 74 0a 20 20 2a 2a 20 20 20 20 20 20 20  P_Gt.  **       
2a4bb 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20 20 20  TK_GE           
2a4bc 20 20 20 4f 50 5f 4c 74 0a 20 20 2a 2a 20 20 20     OP_Lt.  **   
2a4bd 20 20 20 20 54 4b 5f 4c 54 20 20 20 20 20 20 20      TK_LT       
2a4be 20 20 20 20 20 20 20 4f 50 5f 47 65 0a 20 20 2a         OP_Ge.  *
2a4bf 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f 74 68 65 72  *.  ** For other
2a4c0 20 76 61 6c 75 65 73 20 6f 66 20 70 45 78 70 72   values of pExpr
2a4c1 2d 3e 6f 70 2c 20 6f 70 20 69 73 20 75 6e 64 65  ->op, op is unde
2a4c2 66 69 6e 65 64 20 61 6e 64 20 75 6e 75 73 65 64  fined and unused
2a4c3 2e 0a 20 20 2a 2a 20 54 68 65 20 76 61 6c 75 65  ..  ** The value
2a4c4 20 6f 66 20 54 4b 5f 20 61 6e 64 20 4f 50 5f 20   of TK_ and OP_ 
2a4c5 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 61 72  constants are ar
2a4c6 72 61 6e 67 65 64 20 73 75 63 68 20 74 68 61 74  ranged such that
2a4c7 20 77 65 0a 20 20 2a 2a 20 63 61 6e 20 63 6f 6d   we.  ** can com
2a4c8 70 75 74 65 20 74 68 65 20 6d 61 70 70 69 6e 67  pute the mapping
2a4c9 20 61 62 6f 76 65 20 75 73 69 6e 67 20 74 68 65   above using the
2a4ca 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65   following expre
2a4cb 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20 41 73 73 65  ssion..  ** Asse
2a4cc 72 74 28 29 73 20 76 65 72 69 66 79 20 74 68 61  rt()s verify tha
2a4cd 74 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69 6f  t the computatio
2a4ce 6e 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 20 20  n is correct..  
2a4cf 2a 2f 0a 20 20 6f 70 20 3d 20 28 28 70 45 78 70  */.  op = ((pExp
2a4d0 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53 4e 55 4c 4c  r->op+(TK_ISNULL
2a4d1 26 31 29 29 5e 31 29 2d 28 54 4b 5f 49 53 4e 55  &1))^1)-(TK_ISNU
2a4d2 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a 20 56 65 72  LL&1);..  /* Ver
2a4d3 69 66 79 20 63 6f 72 72 65 63 74 20 61 6c 69 67  ify correct alig
2a4d4 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f 20 61 6e 64  nment of TK_ and
2a4d5 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73 0a 20   OP_ constants. 
2a4d6 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 45   */.  assert( pE
2a4d7 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 53 4e 55  xpr->op!=TK_ISNU
2a4d8 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 6f 74  LL || op==OP_Not
2a4d9 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74  Null );.  assert
2a4da 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
2a4db 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f  NOTNULL || op==O
2a4dc 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 61 73  P_IsNull );.  as
2a4dd 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21  sert( pExpr->op!
2a4de 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70 3d 3d 4f 50  =TK_NE || op==OP
2a4df 5f 45 71 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _Eq );.  assert(
2a4e0 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 45   pExpr->op!=TK_E
2a4e1 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 65 20 29  Q || op==OP_Ne )
2a4e2 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70  ;.  assert( pExp
2a4e3 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54 20 7c 7c 20  r->op!=TK_LT || 
2a4e4 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 61  op==OP_Ge );.  a
2a4e5 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70  ssert( pExpr->op
2a4e6 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f 70 3d 3d 4f  !=TK_LE || op==O
2a4e7 50 5f 47 74 20 29 3b 0a 20 20 61 73 73 65 72 74  P_Gt );.  assert
2a4e8 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
2a4e9 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 65 20  GT || op==OP_Le 
2a4ea 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  );.  assert( pEx
2a4eb 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47 45 20 7c 7c  pr->op!=TK_GE ||
2a4ec 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 0a 20   op==OP_Lt );.. 
2a4ed 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e   switch( pExpr->
2a4ee 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
2a4ef 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 74  K_AND: {.      t
2a4f0 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e  estcase( jumpIfN
2a4f1 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ull==0 );.      
2a4f2 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c  sqlite3ExprIfFal
2a4f3 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  se(pParse, pExpr
2a4f4 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a  ->pLeft, dest, j
2a4f5 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
2a4f6 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
2a4f7 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78  alse(pParse, pEx
2a4f8 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74  pr->pRight, dest
2a4f9 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
2a4fa 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a4fb 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52  }.    case TK_OR
2a4fc 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32  : {.      int d2
2a4fd 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
2a4fe 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
2a4ff 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
2a500 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  IfNull==0 );.   
2a501 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
2a502 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b  chePush(pParse);
2a503 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2a504 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c  prIfTrue(pParse,
2a505 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64   pExpr->pLeft, d
2a506 32 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53 51  2, jumpIfNull^SQ
2a507 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
2a508 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2a509 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
2a50a 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  e, pExpr->pRight
2a50b 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
2a50c 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ll);.      sqlit
2a50d 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
2a50e 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20 20  el(v, d2);.     
2a50f 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2a510 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b  ePop(pParse, 1);
2a511 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2a512 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
2a513 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73  NOT: {.      tes
2a514 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c  tcase( jumpIfNul
2a515 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  l==0 );.      sq
2a516 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
2a517 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
2a518 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  Left, dest, jump
2a519 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62  IfNull);.      b
2a51a 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2a51b 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20  case TK_LT:.    
2a51c 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20  case TK_LE:.    
2a51d 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20  case TK_GT:.    
2a51e 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20  case TK_GE:.    
2a51f 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20  case TK_NE:.    
2a520 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20  case TK_EQ: {.  
2a521 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
2a522 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20  ==TK_LT );.     
2a523 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
2a524 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65  K_LE );.      te
2a525 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47  stcase( op==TK_G
2a526 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
2a527 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29  ase( op==TK_GE )
2a528 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2a529 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20  ( op==TK_EQ );. 
2a52a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
2a52b 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20  p==TK_NE );.    
2a52c 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
2a52d 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  IfNull==0 );.   
2a52e 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45     r1 = sqlite3E
2a52f 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
2a530 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
2a531 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20  , &regFree1);.  
2a532 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33      r2 = sqlite3
2a533 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
2a534 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  rse, pExpr->pRig
2a535 68 74 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a  ht, &regFree2);.
2a536 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72        codeCompar
2a537 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
2a538 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70  >pLeft, pExpr->p
2a539 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20  Right, op,.     
2a53a 20 20 20 20 20 20 20 20 20 20 20 20 20 72 31 2c               r1,
2a53b 20 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49   r2, dest, jumpI
2a53c 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65  fNull);.      te
2a53d 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
2a53e 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ==0 );.      tes
2a53f 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
2a540 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  =0 );.      brea
2a541 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
2a542 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61 73  e TK_IS:.    cas
2a543 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 20  e TK_ISNOT: {.  
2a544 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
2a545 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 20 29  xpr->op==TK_IS )
2a546 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2a547 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2a548 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 72  ISNOT );.      r
2a549 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  1 = sqlite3ExprC
2a54a 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
2a54b 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72  pExpr->pLeft, &r
2a54c 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20  egFree1);.      
2a54d 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r2 = sqlite3Expr
2a54e 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
2a54f 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
2a550 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20  &regFree2);.    
2a551 20 20 6f 70 20 3d 20 28 70 45 78 70 72 2d 3e 6f    op = (pExpr->o
2a552 70 3d 3d 54 4b 5f 49 53 29 20 3f 20 54 4b 5f 4e  p==TK_IS) ? TK_N
2a553 45 20 3a 20 54 4b 5f 45 51 3b 0a 20 20 20 20 20  E : TK_EQ;.     
2a554 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61   codeCompare(pPa
2a555 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
2a556 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  t, pExpr->pRight
2a557 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20  , op,.          
2a558 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20          r1, r2, 
2a559 64 65 73 74 2c 20 53 51 4c 49 54 45 5f 4e 55 4c  dest, SQLITE_NUL
2a55a 4c 45 51 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LEQ);.      test
2a55b 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
2a55c 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
2a55d 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
2a55e 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
2a55f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2a560 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63  TK_ISNULL:.    c
2a561 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20  ase TK_NOTNULL: 
2a562 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
2a563 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ( op==TK_ISNULL 
2a564 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2a565 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c  e( op==TK_NOTNUL
2a566 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20  L );.      r1 = 
2a567 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
2a568 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
2a569 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72  r->pLeft, &regFr
2a56a 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ee1);.      sqli
2a56b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a56c 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a   op, r1, dest);.
2a56d 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2a56e 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
2a56f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a570 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45  }.    case TK_BE
2a571 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 74  TWEEN: {.      t
2a572 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e  estcase( jumpIfN
2a573 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ull==0 );.      
2a574 65 78 70 72 43 6f 64 65 42 65 74 77 65 65 6e 28  exprCodeBetween(
2a575 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 64  pParse, pExpr, d
2a576 65 73 74 2c 20 30 2c 20 6a 75 6d 70 49 66 4e 75  est, 0, jumpIfNu
2a577 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ll);.      break
2a578 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
2a579 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
2a57a 55 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b  UERY.    case TK
2a57b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  _IN: {.      if(
2a57c 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 29 7b 0a 20   jumpIfNull ){. 
2a57d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2a57e 70 72 43 6f 64 65 49 4e 28 70 50 61 72 73 65 2c  prCodeIN(pParse,
2a57f 20 70 45 78 70 72 2c 20 64 65 73 74 2c 20 64 65   pExpr, dest, de
2a580 73 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  st);.      }else
2a581 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 64 65  {.        int de
2a582 73 74 49 66 4e 75 6c 6c 20 3d 20 73 71 6c 69 74  stIfNull = sqlit
2a583 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2a584 76 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  v);.        sqli
2a585 74 65 33 45 78 70 72 43 6f 64 65 49 4e 28 70 50  te3ExprCodeIN(pP
2a586 61 72 73 65 2c 20 70 45 78 70 72 2c 20 64 65 73  arse, pExpr, des
2a587 74 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a  t, destIfNull);.
2a588 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2a589 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2a58a 76 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a  v, destIfNull);.
2a58b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
2a58c 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
2a58d 66 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  f.    default: {
2a58e 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69  .      r1 = sqli
2a58f 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
2a590 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26  pParse, pExpr, &
2a591 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20  regFree1);.     
2a592 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a593 70 33 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20  p3(v, OP_IfNot, 
2a594 72 31 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  r1, dest, jumpIf
2a595 4e 75 6c 6c 21 3d 30 29 3b 0a 20 20 20 20 20 20  Null!=0);.      
2a596 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
2a597 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74  e1==0 );.      t
2a598 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e  estcase( jumpIfN
2a599 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ull==0 );.      
2a59a 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
2a59b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2a59c 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2a59d 20 72 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71   regFree1);.  sq
2a59e 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2a59f 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46  Reg(pParse, regF
2a5a0 72 65 65 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ree2);.}../*.** 
2a5a1 44 6f 20 61 20 64 65 65 70 20 63 6f 6d 70 61 72  Do a deep compar
2a5a2 69 73 6f 6e 20 6f 66 20 74 77 6f 20 65 78 70 72  ison of two expr
2a5a3 65 73 73 69 6f 6e 20 74 72 65 65 73 2e 20 20 52  ession trees.  R
2a5a4 65 74 75 72 6e 20 30 20 69 66 20 74 68 65 20 74  eturn 0 if the t
2a5a5 77 6f 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  wo.** expression
2a5a6 73 20 61 72 65 20 63 6f 6d 70 6c 65 74 65 6c 79  s are completely
2a5a7 20 69 64 65 6e 74 69 63 61 6c 2e 20 20 52 65 74   identical.  Ret
2a5a8 75 72 6e 20 31 20 69 66 20 74 68 65 79 20 64 69  urn 1 if they di
2a5a9 66 66 65 72 20 6f 6e 6c 79 0a 2a 2a 20 62 79 20  ffer only.** by 
2a5aa 61 20 43 4f 4c 4c 41 54 45 20 6f 70 65 72 61 74  a COLLATE operat
2a5ab 6f 72 20 61 74 20 74 68 65 20 74 6f 70 20 6c 65  or at the top le
2a5ac 76 65 6c 2e 20 20 52 65 74 75 72 6e 20 32 20 69  vel.  Return 2 i
2a5ad 66 20 74 68 65 72 65 20 61 72 65 20 64 69 66 66  f there are diff
2a5ae 65 72 65 6e 63 65 73 0a 2a 2a 20 6f 74 68 65 72  erences.** other
2a5af 20 74 68 61 6e 20 74 68 65 20 74 6f 70 2d 6c 65   than the top-le
2a5b0 76 65 6c 20 43 4f 4c 4c 41 54 45 20 6f 70 65 72  vel COLLATE oper
2a5b1 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  ator..**.** Some
2a5b2 74 69 6d 65 73 20 74 68 69 73 20 72 6f 75 74 69  times this routi
2a5b3 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 32  ne will return 2
2a5b4 20 65 76 65 6e 20 69 66 20 74 68 65 20 74 77 6f   even if the two
2a5b5 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20   expressions.** 
2a5b6 72 65 61 6c 6c 79 20 61 72 65 20 65 71 75 69 76  really are equiv
2a5b7 61 6c 65 6e 74 2e 20 20 49 66 20 77 65 20 63 61  alent.  If we ca
2a5b8 6e 6e 6f 74 20 70 72 6f 76 65 20 74 68 61 74 20  nnot prove that 
2a5b9 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  the expressions 
2a5ba 61 72 65 0a 2a 2a 20 69 64 65 6e 74 69 63 61 6c  are.** identical
2a5bb 2c 20 77 65 20 72 65 74 75 72 6e 20 32 20 6a 75  , we return 2 ju
2a5bc 73 74 20 74 6f 20 62 65 20 73 61 66 65 2e 20 20  st to be safe.  
2a5bd 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74 69  So if this routi
2a5be 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 32 2c  ne.** returns 2,
2a5bf 20 74 68 65 6e 20 79 6f 75 20 64 6f 20 6e 6f 74   then you do not
2a5c0 20 72 65 61 6c 6c 79 20 6b 6e 6f 77 20 66 6f 72   really know for
2a5c1 20 63 65 72 74 61 69 6e 20 69 66 20 74 68 65 20   certain if the 
2a5c2 74 77 6f 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  two.** expressio
2a5c3 6e 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 2e  ns are the same.
2a5c4 20 20 42 75 74 20 69 66 20 79 6f 75 20 67 65 74    But if you get
2a5c5 20 61 20 30 20 6f 72 20 31 20 72 65 74 75 72 6e   a 0 or 1 return
2a5c6 2c 20 74 68 65 6e 20 79 6f 75 0a 2a 2a 20 63 61  , then you.** ca
2a5c7 6e 20 62 65 20 73 75 72 65 20 74 68 65 20 65 78  n be sure the ex
2a5c8 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 74 68  pressions are th
2a5c9 65 20 73 61 6d 65 2e 20 20 49 6e 20 74 68 65 20  e same.  In the 
2a5ca 70 6c 61 63 65 73 20 77 68 65 72 65 0a 2a 2a 20  places where.** 
2a5cb 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
2a5cc 75 73 65 64 2c 20 69 74 20 64 6f 65 73 20 6e 6f  used, it does no
2a5cd 74 20 68 75 72 74 20 74 6f 20 67 65 74 20 61 6e  t hurt to get an
2a5ce 20 65 78 74 72 61 20 32 20 2d 20 74 68 61 74 0a   extra 2 - that.
2a5cf 2a 2a 20 6a 75 73 74 20 6d 69 67 68 74 20 72 65  ** just might re
2a5d0 73 75 6c 74 20 69 6e 20 73 6f 6d 65 20 73 6c 69  sult in some sli
2a5d1 67 68 74 6c 79 20 73 6c 6f 77 65 72 20 63 6f 64  ghtly slower cod
2a5d2 65 2e 20 20 42 75 74 20 72 65 74 75 72 6e 69 6e  e.  But returnin
2a5d3 67 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63  g.** an incorrec
2a5d4 74 20 30 20 6f 72 20 31 20 63 6f 75 6c 64 20 6c  t 0 or 1 could l
2a5d5 65 61 64 20 74 6f 20 61 20 6d 61 6c 66 75 6e 63  ead to a malfunc
2a5d6 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
2a5d7 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2a5d8 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45  te3ExprCompare(E
2a5d9 78 70 72 20 2a 70 41 2c 20 45 78 70 72 20 2a 70  xpr *pA, Expr *p
2a5da 42 29 7b 0a 20 20 69 66 28 20 70 41 3d 3d 30 7c  B){.  if( pA==0|
2a5db 7c 70 42 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  |pB==0 ){.    re
2a5dc 74 75 72 6e 20 70 42 3d 3d 70 41 20 3f 20 30 20  turn pB==pA ? 0 
2a5dd 3a 20 32 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  : 2;.  }.  asser
2a5de 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72  t( !ExprHasAnyPr
2a5df 6f 70 65 72 74 79 28 70 41 2c 20 45 50 5f 54 6f  operty(pA, EP_To
2a5e0 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63  kenOnly|EP_Reduc
2a5e1 65 64 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ed) );.  assert(
2a5e2 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70   !ExprHasAnyProp
2a5e3 65 72 74 79 28 70 42 2c 20 45 50 5f 54 6f 6b 65  erty(pB, EP_Toke
2a5e4 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64  nOnly|EP_Reduced
2a5e5 29 20 29 3b 0a 20 20 69 66 28 20 45 78 70 72 48  ) );.  if( ExprH
2a5e6 61 73 50 72 6f 70 65 72 74 79 28 70 41 2c 20 45  asProperty(pA, E
2a5e7 50 5f 78 49 73 53 65 6c 65 63 74 29 20 7c 7c 20  P_xIsSelect) || 
2a5e8 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2a5e9 70 42 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pB, EP_xIsSelect
2a5ea 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
2a5eb 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 41  2;.  }.  if( (pA
2a5ec 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73  ->flags & EP_Dis
2a5ed 74 69 6e 63 74 29 21 3d 28 70 42 2d 3e 66 6c 61  tinct)!=(pB->fla
2a5ee 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74  gs & EP_Distinct
2a5ef 29 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20 20  ) ) return 2;.  
2a5f0 69 66 28 20 70 41 2d 3e 6f 70 21 3d 70 42 2d 3e  if( pA->op!=pB->
2a5f1 6f 70 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20  op ) return 2;. 
2a5f2 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72   if( sqlite3Expr
2a5f3 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 65 66  Compare(pA->pLef
2a5f4 74 2c 20 70 42 2d 3e 70 4c 65 66 74 29 20 29 20  t, pB->pLeft) ) 
2a5f5 72 65 74 75 72 6e 20 32 3b 0a 20 20 69 66 28 20  return 2;.  if( 
2a5f6 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61  sqlite3ExprCompa
2a5f7 72 65 28 70 41 2d 3e 70 52 69 67 68 74 2c 20 70  re(pA->pRight, p
2a5f8 42 2d 3e 70 52 69 67 68 74 29 20 29 20 72 65 74  B->pRight) ) ret
2a5f9 75 72 6e 20 32 3b 0a 20 20 69 66 28 20 73 71 6c  urn 2;.  if( sql
2a5fa 69 74 65 33 45 78 70 72 4c 69 73 74 43 6f 6d 70  ite3ExprListComp
2a5fb 61 72 65 28 70 41 2d 3e 78 2e 70 4c 69 73 74 2c  are(pA->x.pList,
2a5fc 20 70 42 2d 3e 78 2e 70 4c 69 73 74 29 20 29 20   pB->x.pList) ) 
2a5fd 72 65 74 75 72 6e 20 32 3b 0a 20 20 69 66 28 20  return 2;.  if( 
2a5fe 70 41 2d 3e 69 54 61 62 6c 65 21 3d 70 42 2d 3e  pA->iTable!=pB->
2a5ff 69 54 61 62 6c 65 20 7c 7c 20 70 41 2d 3e 69 43  iTable || pA->iC
2a600 6f 6c 75 6d 6e 21 3d 70 42 2d 3e 69 43 6f 6c 75  olumn!=pB->iColu
2a601 6d 6e 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20  mn ) return 2;. 
2a602 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
2a603 65 72 74 79 28 70 41 2c 20 45 50 5f 49 6e 74 56  erty(pA, EP_IntV
2a604 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 69 66 28  alue) ){.    if(
2a605 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
2a606 79 28 70 42 2c 20 45 50 5f 49 6e 74 56 61 6c 75  y(pB, EP_IntValu
2a607 65 29 20 7c 7c 20 70 41 2d 3e 75 2e 69 56 61 6c  e) || pA->u.iVal
2a608 75 65 21 3d 70 42 2d 3e 75 2e 69 56 61 6c 75 65  ue!=pB->u.iValue
2a609 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2a60a 20 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73   2;.    }.  }els
2a60b 65 20 69 66 28 20 70 41 2d 3e 6f 70 21 3d 54 4b  e if( pA->op!=TK
2a60c 5f 43 4f 4c 55 4d 4e 20 26 26 20 41 4c 57 41 59  _COLUMN && ALWAY
2a60d 53 28 70 41 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47  S(pA->op!=TK_AGG
2a60e 5f 43 4f 4c 55 4d 4e 29 20 26 26 20 70 41 2d 3e  _COLUMN) && pA->
2a60f 75 2e 7a 54 6f 6b 65 6e 29 7b 0a 20 20 20 20 69  u.zToken){.    i
2a610 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
2a611 74 79 28 70 42 2c 20 45 50 5f 49 6e 74 56 61 6c  ty(pB, EP_IntVal
2a612 75 65 29 20 7c 7c 20 4e 45 56 45 52 28 70 42 2d  ue) || NEVER(pB-
2a613 3e 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 29 20 29 20  >u.zToken==0) ) 
2a614 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 69 66  return 2;.    if
2a615 28 20 73 74 72 63 6d 70 28 70 41 2d 3e 75 2e 7a  ( strcmp(pA->u.z
2a616 54 6f 6b 65 6e 2c 70 42 2d 3e 75 2e 7a 54 6f 6b  Token,pB->u.zTok
2a617 65 6e 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  en)!=0 ){.      
2a618 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 7d 0a  return 2;.    }.
2a619 20 20 7d 0a 20 20 69 66 28 20 28 70 41 2d 3e 66    }.  if( (pA->f
2a61a 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c  lags & EP_ExpCol
2a61b 6c 61 74 65 29 21 3d 28 70 42 2d 3e 66 6c 61 67  late)!=(pB->flag
2a61c 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  s & EP_ExpCollat
2a61d 65 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  e) ) return 1;. 
2a61e 20 69 66 28 20 28 70 41 2d 3e 66 6c 61 67 73 20   if( (pA->flags 
2a61f 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29  & EP_ExpCollate)
2a620 21 3d 30 20 26 26 20 70 41 2d 3e 70 43 6f 6c 6c  !=0 && pA->pColl
2a621 21 3d 70 42 2d 3e 70 43 6f 6c 6c 20 29 20 72 65  !=pB->pColl ) re
2a622 74 75 72 6e 20 32 3b 0a 20 20 72 65 74 75 72 6e  turn 2;.  return
2a623 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d   0;.}../*.** Com
2a624 70 61 72 65 20 74 77 6f 20 45 78 70 72 4c 69 73  pare two ExprLis
2a625 74 20 6f 62 6a 65 63 74 73 2e 20 20 52 65 74 75  t objects.  Retu
2a626 72 6e 20 30 20 69 66 20 74 68 65 79 20 61 72 65  rn 0 if they are
2a627 20 69 64 65 6e 74 69 63 61 6c 20 61 6e 64 20 0a   identical and .
2a628 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74  ** non-zero if t
2a629 68 65 79 20 64 69 66 66 65 72 20 69 6e 20 61 6e  hey differ in an
2a62a 79 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  y way..**.** Thi
2a62b 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20  s routine might 
2a62c 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  return non-zero 
2a62d 66 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 45  for equivalent E
2a62e 78 70 72 4c 69 73 74 73 2e 20 20 54 68 65 0a 2a  xprLists.  The.*
2a62f 2a 20 6f 6e 6c 79 20 63 6f 6e 73 65 71 75 65 6e  * only consequen
2a630 63 65 20 77 69 6c 6c 20 62 65 20 64 69 73 61 62  ce will be disab
2a631 6c 65 64 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  led optimization
2a632 73 2e 20 20 42 75 74 20 74 68 69 73 20 72 6f 75  s.  But this rou
2a633 74 69 6e 65 0a 2a 2a 20 6d 75 73 74 20 6e 65 76  tine.** must nev
2a634 65 72 20 72 65 74 75 72 6e 20 30 20 69 66 20 74  er return 0 if t
2a635 68 65 20 74 77 6f 20 45 78 70 72 4c 69 73 74 20  he two ExprList 
2a636 6f 62 6a 65 63 74 73 20 61 72 65 20 64 69 66 66  objects are diff
2a637 65 72 65 6e 74 2c 20 6f 72 0a 2a 2a 20 61 20 6d  erent, or.** a m
2a638 61 6c 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20  alfunction will 
2a639 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 77  result..**.** Tw
2a63a 6f 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20  o NULL pointers 
2a63b 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74  are considered t
2a63c 6f 20 62 65 20 74 68 65 20 73 61 6d 65 2e 20 20  o be the same.  
2a63d 42 75 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  But a NULL point
2a63e 65 72 0a 2a 2a 20 61 6c 77 61 79 73 20 64 69 66  er.** always dif
2a63f 66 65 72 73 20 66 72 6f 6d 20 61 20 6e 6f 6e 2d  fers from a non-
2a640 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  NULL pointer..*/
2a641 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2a642 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 4c  int sqlite3ExprL
2a643 69 73 74 43 6f 6d 70 61 72 65 28 45 78 70 72 4c  istCompare(ExprL
2a644 69 73 74 20 2a 70 41 2c 20 45 78 70 72 4c 69 73  ist *pA, ExprLis
2a645 74 20 2a 70 42 29 7b 0a 20 20 69 6e 74 20 69 3b  t *pB){.  int i;
2a646 0a 20 20 69 66 28 20 70 41 3d 3d 30 20 26 26 20  .  if( pA==0 && 
2a647 70 42 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  pB==0 ) return 0
2a648 3b 0a 20 20 69 66 28 20 70 41 3d 3d 30 20 7c 7c  ;.  if( pA==0 ||
2a649 20 70 42 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   pB==0 ) return 
2a64a 31 3b 0a 20 20 69 66 28 20 70 41 2d 3e 6e 45 78  1;.  if( pA->nEx
2a64b 70 72 21 3d 70 42 2d 3e 6e 45 78 70 72 20 29 20  pr!=pB->nExpr ) 
2a64c 72 65 74 75 72 6e 20 31 3b 0a 20 20 66 6f 72 28  return 1;.  for(
2a64d 69 3d 30 3b 20 69 3c 70 41 2d 3e 6e 45 78 70 72  i=0; i<pA->nExpr
2a64e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72  ; i++){.    Expr
2a64f 20 2a 70 45 78 70 72 41 20 3d 20 70 41 2d 3e 61   *pExprA = pA->a
2a650 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 45  [i].pExpr;.    E
2a651 78 70 72 20 2a 70 45 78 70 72 42 20 3d 20 70 42  xpr *pExprB = pB
2a652 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
2a653 20 20 69 66 28 20 70 41 2d 3e 61 5b 69 5d 2e 73    if( pA->a[i].s
2a654 6f 72 74 4f 72 64 65 72 21 3d 70 42 2d 3e 61 5b  ortOrder!=pB->a[
2a655 69 5d 2e 73 6f 72 74 4f 72 64 65 72 20 29 20 72  i].sortOrder ) r
2a656 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 69 66 28  eturn 1;.    if(
2a657 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70   sqlite3ExprComp
2a658 61 72 65 28 70 45 78 70 72 41 2c 20 70 45 78 70  are(pExprA, pExp
2a659 72 42 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  rB) ) return 1;.
2a65a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
2a65b 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  }../*.** An inst
2a65c 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
2a65d 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
2a65e 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 74  is used by the t
2a65f 72 65 65 20 77 61 6c 6b 65 72 0a 2a 2a 20 74 6f  ree walker.** to
2a660 20 63 6f 75 6e 74 20 72 65 66 65 72 65 6e 63 65   count reference
2a661 73 20 74 6f 20 74 61 62 6c 65 20 63 6f 6c 75 6d  s to table colum
2a662 6e 73 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65  ns in the argume
2a663 6e 74 73 20 6f 66 20 61 6e 20 0a 2a 2a 20 61 67  nts of an .** ag
2a664 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
2a665 2c 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6d  , in order to im
2a666 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 73  plement the.** s
2a667 71 6c 69 74 65 33 46 75 6e 63 74 69 6f 6e 54 68  qlite3FunctionTh
2a668 69 73 53 72 63 28 29 20 72 6f 75 74 69 6e 65 2e  isSrc() routine.
2a669 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72 63 43 6f  .*/.struct SrcCo
2a66a 75 6e 74 20 7b 0a 20 20 53 72 63 4c 69 73 74 20  unt {.  SrcList 
2a66b 2a 70 53 72 63 3b 20 20 20 2f 2a 20 4f 6e 65 20  *pSrc;   /* One 
2a66c 70 61 72 74 69 63 75 6c 61 72 20 46 52 4f 4d 20  particular FROM 
2a66d 63 6c 61 75 73 65 20 69 6e 20 61 20 6e 65 73 74  clause in a nest
2a66e 65 64 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e  ed query */.  in
2a66f 74 20 6e 54 68 69 73 3b 20 20 20 20 20 20 20 2f  t nThis;       /
2a670 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65  * Number of refe
2a671 72 65 6e 63 65 73 20 74 6f 20 63 6f 6c 75 6d 6e  rences to column
2a672 73 20 69 6e 20 70 53 72 63 4c 69 73 74 20 2a 2f  s in pSrcList */
2a673 0a 20 20 69 6e 74 20 6e 4f 74 68 65 72 3b 20 20  .  int nOther;  
2a674 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2a675 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 63   references to c
2a676 6f 6c 75 6d 6e 73 20 69 6e 20 6f 74 68 65 72 20  olumns in other 
2a677 46 52 4f 4d 20 63 6c 61 75 73 65 73 20 2a 2f 0a  FROM clauses */.
2a678 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20  };../*.** Count 
2a679 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  the number of re
2a67a 66 65 72 65 6e 63 65 73 20 74 6f 20 63 6f 6c 75  ferences to colu
2a67b 6d 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  mns..*/.static i
2a67c 6e 74 20 65 78 70 72 53 72 63 43 6f 75 6e 74 28  nt exprSrcCount(
2a67d 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c  Walker *pWalker,
2a67e 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20   Expr *pExpr){. 
2a67f 20 2f 2a 20 54 68 65 20 4e 45 56 45 52 28 29 20   /* The NEVER() 
2a680 6f 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 74 65  on the second te
2a681 72 6d 20 69 73 20 62 65 63 61 75 73 65 20 73 71  rm is because sq
2a682 6c 69 74 65 33 46 75 6e 63 74 69 6f 6e 55 73 65  lite3FunctionUse
2a683 73 54 68 69 73 53 72 63 28 29 0a 20 20 2a 2a 20  sThisSrc().  ** 
2a684 69 73 20 61 6c 77 61 79 73 20 63 61 6c 6c 65 64  is always called
2a685 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 45   before sqlite3E
2a686 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67  xprAnalyzeAggreg
2a687 61 74 65 73 28 29 20 61 6e 64 20 73 6f 20 74 68  ates() and so th
2a688 65 0a 20 20 2a 2a 20 54 4b 5f 43 4f 4c 55 4d 4e  e.  ** TK_COLUMN
2a689 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 62  s have not yet b
2a68a 65 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 69 6e  een converted in
2a68b 74 6f 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  to TK_AGG_COLUMN
2a68c 2e 20 20 49 66 0a 20 20 2a 2a 20 73 71 6c 69 74  .  If.  ** sqlit
2a68d 65 33 46 75 6e 63 74 69 6f 6e 55 73 65 73 54 68  e3FunctionUsesTh
2a68e 69 73 53 72 63 28 29 20 69 73 20 75 73 65 64 20  isSrc() is used 
2a68f 64 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 74  differently in t
2a690 68 65 20 66 75 74 75 72 65 2c 20 74 68 65 0a 20  he future, the. 
2a691 20 2a 2a 20 4e 45 56 45 52 28 29 20 77 69 6c 6c   ** NEVER() will
2a692 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65 6d 6f   need to be remo
2a693 76 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 70 45  ved. */.  if( pE
2a694 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  xpr->op==TK_COLU
2a695 4d 4e 20 7c 7c 20 4e 45 56 45 52 28 70 45 78 70  MN || NEVER(pExp
2a696 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f  r->op==TK_AGG_CO
2a697 4c 55 4d 4e 29 20 29 7b 0a 20 20 20 20 69 6e 74  LUMN) ){.    int
2a698 20 69 3b 0a 20 20 20 20 73 74 72 75 63 74 20 53   i;.    struct S
2a699 72 63 43 6f 75 6e 74 20 2a 70 20 3d 20 70 57 61  rcCount *p = pWa
2a69a 6c 6b 65 72 2d 3e 75 2e 70 53 72 63 43 6f 75 6e  lker->u.pSrcCoun
2a69b 74 3b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a  t;.    SrcList *
2a69c 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a  pSrc = p->pSrc;.
2a69d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
2a69e 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b  Src->nSrc; i++){
2a69f 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72  .      if( pExpr
2a6a0 2d 3e 69 54 61 62 6c 65 3d 3d 70 53 72 63 2d 3e  ->iTable==pSrc->
2a6a1 61 5b 69 5d 2e 69 43 75 72 73 6f 72 20 29 20 62  a[i].iCursor ) b
2a6a2 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2a6a3 69 66 28 20 69 3c 70 53 72 63 2d 3e 6e 53 72 63  if( i<pSrc->nSrc
2a6a4 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 54 68   ){.      p->nTh
2a6a5 69 73 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  is++;.    }else{
2a6a6 0a 20 20 20 20 20 20 70 2d 3e 6e 4f 74 68 65 72  .      p->nOther
2a6a7 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ++;.    }.  }.  
2a6a8 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
2a6a9 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  nue;.}../*.** De
2a6aa 74 65 72 6d 69 6e 65 20 69 66 20 61 6e 79 20 6f  termine if any o
2a6ab 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73 20  f the arguments 
2a6ac 74 6f 20 74 68 65 20 70 45 78 70 72 20 46 75 6e  to the pExpr Fun
2a6ad 63 74 69 6f 6e 20 72 65 66 65 72 65 6e 63 65 0a  ction reference.
2a6ae 2a 2a 20 70 53 72 63 4c 69 73 74 2e 20 20 52 65  ** pSrcList.  Re
2a6af 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
2a6b0 79 20 64 6f 2e 20 20 41 6c 73 6f 20 72 65 74 75  y do.  Also retu
2a6b1 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 66  rn true if the f
2a6b2 75 6e 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 6e  unction.** has n
2a6b3 6f 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 68  o arguments or h
2a6b4 61 73 20 6f 6e 6c 79 20 63 6f 6e 73 74 61 6e 74  as only constant
2a6b5 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 52 65 74   arguments.  Ret
2a6b6 75 72 6e 20 66 61 6c 73 65 20 69 66 20 70 45 78  urn false if pEx
2a6b7 70 72 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 73  pr.** references
2a6b8 20 63 6f 6c 75 6d 6e 73 20 62 75 74 20 6e 6f 74   columns but not
2a6b9 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 61 62 6c   columns of tabl
2a6ba 65 73 20 66 6f 75 6e 64 20 69 6e 20 70 53 72 63  es found in pSrc
2a6bb 4c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  List..*/.SQLITE_
2a6bc 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2a6bd 74 65 33 46 75 6e 63 74 69 6f 6e 55 73 65 73 54  te3FunctionUsesT
2a6be 68 69 73 53 72 63 28 45 78 70 72 20 2a 70 45 78  hisSrc(Expr *pEx
2a6bf 70 72 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72  pr, SrcList *pSr
2a6c0 63 4c 69 73 74 29 7b 0a 20 20 57 61 6c 6b 65 72  cList){.  Walker
2a6c1 20 77 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63   w;.  struct Src
2a6c2 43 6f 75 6e 74 20 63 6e 74 3b 0a 20 20 61 73 73  Count cnt;.  ass
2a6c3 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  ert( pExpr->op==
2a6c4 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20  TK_AGG_FUNCTION 
2a6c5 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 77 2c 20  );.  memset(&w, 
2a6c6 30 2c 20 73 69 7a 65 6f 66 28 77 29 29 3b 0a 20  0, sizeof(w));. 
2a6c7 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b   w.xExprCallback
2a6c8 20 3d 20 65 78 70 72 53 72 63 43 6f 75 6e 74 3b   = exprSrcCount;
2a6c9 0a 20 20 77 2e 75 2e 70 53 72 63 43 6f 75 6e 74  .  w.u.pSrcCount
2a6ca 20 3d 20 26 63 6e 74 3b 0a 20 20 63 6e 74 2e 70   = &cnt;.  cnt.p
2a6cb 53 72 63 20 3d 20 70 53 72 63 4c 69 73 74 3b 0a  Src = pSrcList;.
2a6cc 20 20 63 6e 74 2e 6e 54 68 69 73 20 3d 20 30 3b    cnt.nThis = 0;
2a6cd 0a 20 20 63 6e 74 2e 6e 4f 74 68 65 72 20 3d 20  .  cnt.nOther = 
2a6ce 30 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b  0;.  sqlite3Walk
2a6cf 45 78 70 72 4c 69 73 74 28 26 77 2c 20 70 45 78  ExprList(&w, pEx
2a6d0 70 72 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20  pr->x.pList);.  
2a6d1 72 65 74 75 72 6e 20 63 6e 74 2e 6e 54 68 69 73  return cnt.nThis
2a6d2 3e 30 20 7c 7c 20 63 6e 74 2e 6e 4f 74 68 65 72  >0 || cnt.nOther
2a6d3 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  ==0;.}../*.** Ad
2a6d4 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20  d a new element 
2a6d5 74 6f 20 74 68 65 20 70 41 67 67 49 6e 66 6f 2d  to the pAggInfo-
2a6d6 3e 61 43 6f 6c 5b 5d 20 61 72 72 61 79 2e 20 20  >aCol[] array.  
2a6d7 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78  Return the index
2a6d8 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 65   of.** the new e
2a6d9 6c 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20  lement.  Return 
2a6da 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65  a negative numbe
2a6db 72 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  r if malloc fail
2a6dc 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
2a6dd 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d   addAggInfoColum
2a6de 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 41  n(sqlite3 *db, A
2a6df 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a  ggInfo *pInfo){.
2a6e0 20 20 69 6e 74 20 69 3b 0a 20 20 70 49 6e 66 6f    int i;.  pInfo
2a6e1 2d 3e 61 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33  ->aCol = sqlite3
2a6e2 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20  ArrayAllocate(. 
2a6e3 20 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20        db,.      
2a6e4 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 2c 0a 20 20   pInfo->aCol,.  
2a6e5 20 20 20 20 20 73 69 7a 65 6f 66 28 70 49 6e 66       sizeof(pInf
2a6e6 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c 0a 20 20 20  o->aCol[0]),.   
2a6e7 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c      &pInfo->nCol
2a6e8 75 6d 6e 2c 0a 20 20 20 20 20 20 20 26 69 0a 20  umn,.       &i. 
2a6e9 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a   );.  return i;.
2a6ea 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  }    ../*.** Add
2a6eb 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74   a new element t
2a6ec 6f 20 74 68 65 20 70 41 67 67 49 6e 66 6f 2d 3e  o the pAggInfo->
2a6ed 61 46 75 6e 63 5b 5d 20 61 72 72 61 79 2e 20 20  aFunc[] array.  
2a6ee 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78  Return the index
2a6ef 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 65   of.** the new e
2a6f0 6c 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20  lement.  Return 
2a6f1 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65  a negative numbe
2a6f2 72 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  r if malloc fail
2a6f3 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
2a6f4 20 61 64 64 41 67 67 49 6e 66 6f 46 75 6e 63 28   addAggInfoFunc(
2a6f5 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 41 67 67  sqlite3 *db, Agg
2a6f6 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 20  Info *pInfo){.  
2a6f7 69 6e 74 20 69 3b 0a 20 20 70 49 6e 66 6f 2d 3e  int i;.  pInfo->
2a6f8 61 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 41  aFunc = sqlite3A
2a6f9 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20  rrayAllocate(.  
2a6fa 20 20 20 20 20 64 62 2c 20 0a 20 20 20 20 20 20       db, .      
2a6fb 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 2c 0a 20   pInfo->aFunc,. 
2a6fc 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 49 6e        sizeof(pIn
2a6fd 66 6f 2d 3e 61 46 75 6e 63 5b 30 5d 29 2c 0a 20  fo->aFunc[0]),. 
2a6fe 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 46        &pInfo->nF
2a6ff 75 6e 63 2c 0a 20 20 20 20 20 20 20 26 69 0a 20  unc,.       &i. 
2a700 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a   );.  return i;.
2a701 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  }    ../*.** Thi
2a702 73 20 69 73 20 74 68 65 20 78 45 78 70 72 43 61  s is the xExprCa
2a703 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 74 72 65  llback for a tre
2a704 65 20 77 61 6c 6b 65 72 2e 20 20 49 74 20 69 73  e walker.  It is
2a705 20 75 73 65 64 20 74 6f 0a 2a 2a 20 69 6d 70 6c   used to.** impl
2a706 65 6d 65 6e 74 20 73 71 6c 69 74 65 33 45 78 70  ement sqlite3Exp
2a707 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
2a708 65 73 28 29 2e 20 20 53 65 65 20 73 71 6c 69 74  es().  See sqlit
2a709 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
2a70a 72 65 67 61 74 65 73 0a 2a 2a 20 66 6f 72 20 61  regates.** for a
2a70b 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
2a70c 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ation..*/.static
2a70d 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72   int analyzeAggr
2a70e 65 67 61 74 65 28 57 61 6c 6b 65 72 20 2a 70 57  egate(Walker *pW
2a70f 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78  alker, Expr *pEx
2a710 70 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  pr){.  int i;.  
2a711 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43  NameContext *pNC
2a712 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e   = pWalker->u.pN
2a713 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  C;.  Parse *pPar
2a714 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
2a715 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  ;.  SrcList *pSr
2a716 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72  cList = pNC->pSr
2a717 63 4c 69 73 74 3b 0a 20 20 41 67 67 49 6e 66 6f  cList;.  AggInfo
2a718 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 4e 43   *pAggInfo = pNC
2a719 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 0a 20 20 73  ->pAggInfo;..  s
2a71a 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70  witch( pExpr->op
2a71b 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   ){.    case TK_
2a71c 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20  AGG_COLUMN:.    
2a71d 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20  case TK_COLUMN: 
2a71e 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
2a71f 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2a720 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20  AGG_COLUMN );.  
2a721 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
2a722 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  xpr->op==TK_COLU
2a723 4d 4e 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 43  MN );.      /* C
2a724 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
2a725 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69 6e 20  he column is in 
2a726 6f 6e 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  one of the table
2a727 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20 20  s in the FROM.  
2a728 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 6f 66      ** clause of
2a729 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 71   the aggregate q
2a72a 75 65 72 79 20 2a 2f 0a 20 20 20 20 20 20 69 66  uery */.      if
2a72b 28 20 41 4c 57 41 59 53 28 70 53 72 63 4c 69 73  ( ALWAYS(pSrcLis
2a72c 74 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20  t!=0) ){.       
2a72d 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2a72e 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53  item *pItem = pS
2a72f 72 63 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20  rcList->a;.     
2a730 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53     for(i=0; i<pS
2a731 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  rcList->nSrc; i+
2a732 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
2a733 20 20 20 20 20 20 20 73 74 72 75 63 74 20 41 67         struct Ag
2a734 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 3b  gInfo_col *pCol;
2a735 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
2a736 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72  t( !ExprHasAnyPr
2a737 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
2a738 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65  _TokenOnly|EP_Re
2a739 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20 20 20  duced) );.      
2a73a 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69      if( pExpr->i
2a73b 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43  Table==pItem->iC
2a73c 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20  ursor ){.       
2a73d 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 72 65       /* If we re
2a73e 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20  ach this point, 
2a73f 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 70 45  it means that pE
2a740 78 70 72 20 72 65 66 65 72 73 20 74 6f 20 61 20  xpr refers to a 
2a741 74 61 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20  table.          
2a742 20 20 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 20    ** that is in 
2a743 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2a744 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65  of the aggregate
2a745 20 71 75 65 72 79 2e 20 20 0a 20 20 20 20 20 20   query.  .      
2a746 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
2a747 20 20 20 20 20 2a 2a 20 4d 61 6b 65 20 61 6e 20       ** Make an 
2a748 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 63 6f  entry for the co
2a749 6c 75 6d 6e 20 69 6e 20 70 41 67 67 49 6e 66 6f  lumn in pAggInfo
2a74a 2d 3e 61 43 6f 6c 5b 5d 20 69 66 20 74 68 65 72  ->aCol[] if ther
2a74b 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  e.            **
2a74c 20 69 73 20 6e 6f 74 20 61 6e 20 65 6e 74 72 79   is not an entry
2a74d 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a   there already..
2a74e 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
2a74f 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b             int k
2a750 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 43  ;.            pC
2a751 6f 6c 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 61  ol = pAggInfo->a
2a752 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20  Col;.           
2a753 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 41 67 67   for(k=0; k<pAgg
2a754 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b  Info->nColumn; k
2a755 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20  ++, pCol++){.   
2a756 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
2a757 43 6f 6c 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78  Col->iTable==pEx
2a758 70 72 2d 3e 69 54 61 62 6c 65 20 26 26 0a 20 20  pr->iTable &&.  
2a759 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a75a 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70  pCol->iColumn==p
2a75b 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b  Expr->iColumn ){
2a75c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2a75d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2a75e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2a75f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2a760 20 20 69 66 28 20 28 6b 3e 3d 70 41 67 67 49 6e    if( (k>=pAggIn
2a761 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 29 0a 20 20 20  fo->nColumn).   
2a762 20 20 20 20 20 20 20 20 20 20 26 26 20 28 6b 20            && (k 
2a763 3d 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75  = addAggInfoColu
2a764 6d 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  mn(pParse->db, p
2a765 41 67 67 49 6e 66 6f 29 29 3e 3d 30 20 0a 20 20  AggInfo))>=0 .  
2a766 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20            ){.   
2a767 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20             pCol 
2a768 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f  = &pAggInfo->aCo
2a769 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  l[k];.          
2a76a 20 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62 20 3d      pCol->pTab =
2a76b 20 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20   pExpr->pTab;.  
2a76c 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
2a76d 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 78 70 72  ->iTable = pExpr
2a76e 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20  ->iTable;.      
2a76f 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 43          pCol->iC
2a770 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 69  olumn = pExpr->i
2a771 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20  Column;.        
2a772 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 4d 65 6d        pCol->iMem
2a773 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2a774 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  m;.             
2a775 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f   pCol->iSorterCo
2a776 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20  lumn = -1;.     
2a777 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70           pCol->p
2a778 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20  Expr = pExpr;.  
2a779 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
2a77a 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70  pAggInfo->pGroup
2a77b 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  By ){.          
2a77c 20 20 20 20 20 20 69 6e 74 20 6a 2c 20 6e 3b 0a        int j, n;.
2a77d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a77e 45 78 70 72 4c 69 73 74 20 2a 70 47 42 20 3d 20  ExprList *pGB = 
2a77f 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70  pAggInfo->pGroup
2a780 42 79 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  By;.            
2a781 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
2a782 69 73 74 5f 69 74 65 6d 20 2a 70 54 65 72 6d 20  ist_item *pTerm 
2a783 3d 20 70 47 42 2d 3e 61 3b 0a 20 20 20 20 20 20  = pGB->a;.      
2a784 20 20 20 20 20 20 20 20 20 20 6e 20 3d 20 70 47            n = pG
2a785 42 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  B->nExpr;.      
2a786 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d            for(j=
2a787 30 3b 20 6a 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 65  0; j<n; j++, pTe
2a788 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  rm++){.         
2a789 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70           Expr *p
2a78a 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  E = pTerm->pExpr
2a78b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2a78c 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d      if( pE->op==
2a78d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 45 2d  TK_COLUMN && pE-
2a78e 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e  >iTable==pExpr->
2a78f 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20  iTable &&.      
2a790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a791 70 45 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78  pE->iColumn==pEx
2a792 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20  pr->iColumn ){. 
2a793 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a794 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72     pCol->iSorter
2a795 43 6f 6c 75 6d 6e 20 3d 20 6a 3b 0a 20 20 20 20  Column = j;.    
2a796 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a797 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
2a798 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2a799 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2a79a 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2a79b 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
2a79c 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75  Col->iSorterColu
2a79d 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  mn<0 ){.        
2a79e 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53          pCol->iS
2a79f 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 70 41  orterColumn = pA
2a7a0 67 67 49 6e 66 6f 2d 3e 6e 53 6f 72 74 69 6e 67  ggInfo->nSorting
2a7a1 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 20 20  Column++;.      
2a7a2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2a7a3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2a7a4 20 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20      /* There is 
2a7a5 6e 6f 77 20 61 6e 20 65 6e 74 72 79 20 66 6f 72  now an entry for
2a7a6 20 70 45 78 70 72 20 69 6e 20 70 41 67 67 49 6e   pExpr in pAggIn
2a7a7 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 28 65 69 74 68  fo->aCol[] (eith
2a7a8 65 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  er.            *
2a7a9 2a 20 62 65 63 61 75 73 65 20 69 74 20 77 61 73  * because it was
2a7aa 20 74 68 65 72 65 20 62 65 66 6f 72 65 20 6f 72   there before or
2a7ab 20 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 74   because we just
2a7ac 20 63 72 65 61 74 65 64 20 69 74 29 2e 0a 20 20   created it)..  
2a7ad 20 20 20 20 20 20 20 20 20 20 2a 2a 20 43 6f 6e            ** Con
2a7ae 76 65 72 74 20 74 68 65 20 70 45 78 70 72 20 74  vert the pExpr t
2a7af 6f 20 62 65 20 61 20 54 4b 5f 41 47 47 5f 43 4f  o be a TK_AGG_CO
2a7b0 4c 55 4d 4e 20 72 65 66 65 72 72 69 6e 67 20 74  LUMN referring t
2a7b1 6f 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 20  o that.         
2a7b2 20 20 20 2a 2a 20 70 41 67 67 49 6e 66 6f 2d 3e     ** pAggInfo->
2a7b3 61 43 6f 6c 5b 5d 20 65 6e 74 72 79 2e 0a 20 20  aCol[] entry..  
2a7b4 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
2a7b5 20 20 20 20 20 20 20 20 20 45 78 70 72 53 65 74           ExprSet
2a7b6 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78 70  Irreducible(pExp
2a7b7 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  r);.            
2a7b8 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 20  pExpr->pAggInfo 
2a7b9 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20  = pAggInfo;.    
2a7ba 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f          pExpr->o
2a7bb 70 20 3d 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  p = TK_AGG_COLUM
2a7bc 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  N;.            p
2a7bd 45 78 70 72 2d 3e 69 41 67 67 20 3d 20 28 69 31  Expr->iAgg = (i1
2a7be 36 29 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  6)k;.           
2a7bf 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2a7c0 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70 45 78    } /* endif pEx
2a7c1 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65  pr->iTable==pIte
2a7c2 6d 2d 3e 69 43 75 72 73 6f 72 20 2a 2f 0a 20 20  m->iCursor */.  
2a7c3 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e 64 20 6c        } /* end l
2a7c4 6f 6f 70 20 6f 76 65 72 20 70 53 72 63 4c 69 73  oop over pSrcLis
2a7c5 74 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  t */.      }.   
2a7c6 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
2a7c7 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  une;.    }.    c
2a7c8 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54  ase TK_AGG_FUNCT
2a7c9 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  ION: {.      if(
2a7ca 20 28 70 4e 43 2d 3e 6e 63 46 6c 61 67 73 20 26   (pNC->ncFlags &
2a7cb 20 4e 43 5f 49 6e 41 67 67 46 75 6e 63 29 3d 3d   NC_InAggFunc)==
2a7cc 30 0a 20 20 20 20 20 20 20 26 26 20 70 57 61 6c  0.       && pWal
2a7cd 6b 65 72 2d 3e 77 61 6c 6b 65 72 44 65 70 74 68  ker->walkerDepth
2a7ce 3d 3d 70 45 78 70 72 2d 3e 6f 70 32 0a 20 20 20  ==pExpr->op2.   
2a7cf 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a     ){.        /*
2a7d0 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
2a7d1 20 70 45 78 70 72 20 69 73 20 61 20 64 75 70 6c   pExpr is a dupl
2a7d2 69 63 61 74 65 20 6f 66 20 61 6e 6f 74 68 65 72  icate of another
2a7d3 20 61 67 67 72 65 67 61 74 65 20 0a 20 20 20 20   aggregate .    
2a7d4 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20      ** function 
2a7d5 74 68 61 74 20 69 73 20 61 6c 72 65 61 64 79 20  that is already 
2a7d6 69 6e 20 74 68 65 20 70 41 67 67 49 6e 66 6f 20  in the pAggInfo 
2a7d7 73 74 72 75 63 74 75 72 65 0a 20 20 20 20 20 20  structure.      
2a7d8 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 74 72    */.        str
2a7d9 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63  uct AggInfo_func
2a7da 20 2a 70 49 74 65 6d 20 3d 20 70 41 67 67 49 6e   *pItem = pAggIn
2a7db 66 6f 2d 3e 61 46 75 6e 63 3b 0a 20 20 20 20 20  fo->aFunc;.     
2a7dc 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41     for(i=0; i<pA
2a7dd 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69  ggInfo->nFunc; i
2a7de 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
2a7df 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
2a7e0 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70  te3ExprCompare(p
2a7e1 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 70 45 78  Item->pExpr, pEx
2a7e2 70 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  pr)==0 ){.      
2a7e3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2a7e4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2a7e5 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69   }.        if( i
2a7e6 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e  >=pAggInfo->nFun
2a7e7 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  c ){.          /
2a7e8 2a 20 70 45 78 70 72 20 69 73 20 6f 72 69 67 69  * pExpr is origi
2a7e9 6e 61 6c 2e 20 20 4d 61 6b 65 20 61 20 6e 65 77  nal.  Make a new
2a7ea 20 65 6e 74 72 79 20 69 6e 20 70 41 67 67 49 6e   entry in pAggIn
2a7eb 66 6f 2d 3e 61 46 75 6e 63 5b 5d 0a 20 20 20 20  fo->aFunc[].    
2a7ec 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
2a7ed 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28     u8 enc = ENC(
2a7ee 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 20  pParse->db);.   
2a7ef 20 20 20 20 20 20 20 69 20 3d 20 61 64 64 41 67         i = addAg
2a7f0 67 49 6e 66 6f 46 75 6e 63 28 70 50 61 72 73 65  gInfoFunc(pParse
2a7f1 2d 3e 64 62 2c 20 70 41 67 67 49 6e 66 6f 29 3b  ->db, pAggInfo);
2a7f2 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69  .          if( i
2a7f3 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=0 ){.         
2a7f4 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
2a7f5 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
2a7f6 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
2a7f7 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
2a7f8 70 49 74 65 6d 20 3d 20 26 70 41 67 67 49 6e 66  pItem = &pAggInf
2a7f9 6f 2d 3e 61 46 75 6e 63 5b 69 5d 3b 0a 20 20 20  o->aFunc[i];.   
2a7fa 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e           pItem->
2a7fb 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20  pExpr = pExpr;. 
2a7fc 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d             pItem
2a7fd 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73  ->iMem = ++pPars
2a7fe 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20  e->nMem;.       
2a7ff 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
2a800 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
2a801 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  xpr, EP_IntValue
2a802 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ) );.           
2a803 20 70 49 74 65 6d 2d 3e 70 46 75 6e 63 20 3d 20   pItem->pFunc = 
2a804 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
2a805 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 0a  ion(pParse->db,.
2a806 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a807 20 20 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b     pExpr->u.zTok
2a808 65 6e 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65  en, sqlite3Strle
2a809 6e 33 30 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f  n30(pExpr->u.zTo
2a80a 6b 65 6e 29 2c 0a 20 20 20 20 20 20 20 20 20 20  ken),.          
2a80b 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
2a80c 78 2e 70 4c 69 73 74 20 3f 20 70 45 78 70 72 2d  x.pList ? pExpr-
2a80d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20  >x.pList->nExpr 
2a80e 3a 20 30 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20  : 0, enc, 0);.  
2a80f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45            if( pE
2a810 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  xpr->flags & EP_
2a811 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20  Distinct ){.    
2a812 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d            pItem-
2a813 3e 69 44 69 73 74 69 6e 63 74 20 3d 20 70 50 61  >iDistinct = pPa
2a814 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
2a815 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
2a816 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49                pI
2a817 74 65 6d 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d  tem->iDistinct =
2a818 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20   -1;.           
2a819 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
2a81a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2a81b 20 2f 2a 20 4d 61 6b 65 20 70 45 78 70 72 20 70   /* Make pExpr p
2a81c 6f 69 6e 74 20 74 6f 20 74 68 65 20 61 70 70 72  oint to the appr
2a81d 6f 70 72 69 61 74 65 20 70 41 67 67 49 6e 66 6f  opriate pAggInfo
2a81e 2d 3e 61 46 75 6e 63 5b 5d 20 65 6e 74 72 79 0a  ->aFunc[] entry.
2a81f 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2a820 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
2a821 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
2a822 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e  Expr, EP_TokenOn
2a823 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29  ly|EP_Reduced) )
2a824 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72 53 65  ;.        ExprSe
2a825 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78  tIrreducible(pEx
2a826 70 72 29 3b 0a 20 20 20 20 20 20 20 20 70 45 78  pr);.        pEx
2a827 70 72 2d 3e 69 41 67 67 20 3d 20 28 69 31 36 29  pr->iAgg = (i16)
2a828 69 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72  i;.        pExpr
2a829 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20 70 41 67  ->pAggInfo = pAg
2a82a 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 72  gInfo;.        r
2a82b 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b  eturn WRC_Prune;
2a82c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2a82d 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
2a82e 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20  _Continue;.     
2a82f 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
2a830 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e  eturn WRC_Contin
2a831 75 65 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  ue;.}.static int
2a832 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74   analyzeAggregat
2a833 65 73 49 6e 53 65 6c 65 63 74 28 57 61 6c 6b 65  esInSelect(Walke
2a834 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
2a835 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20  ct *pSelect){.  
2a836 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
2a837 28 70 57 61 6c 6b 65 72 29 3b 0a 20 20 55 4e 55  (pWalker);.  UNU
2a838 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 53  SED_PARAMETER(pS
2a839 65 6c 65 63 74 29 3b 0a 20 20 72 65 74 75 72 6e  elect);.  return
2a83a 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
2a83b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20  ../*.** Analyze 
2a83c 74 68 65 20 70 45 78 70 72 20 65 78 70 72 65 73  the pExpr expres
2a83d 73 69 6f 6e 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  sion looking for
2a83e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
2a83f 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20  ions and.** for 
2a840 76 61 72 69 61 62 6c 65 73 20 74 68 61 74 20 6e  variables that n
2a841 65 65 64 20 74 6f 20 62 65 20 61 64 64 65 64 20  eed to be added 
2a842 74 6f 20 41 67 67 49 6e 66 6f 20 6f 62 6a 65 63  to AggInfo objec
2a843 74 20 74 68 61 74 20 70 4e 43 2d 3e 70 41 67 67  t that pNC->pAgg
2a844 49 6e 66 6f 0a 2a 2a 20 70 6f 69 6e 74 73 20 74  Info.** points t
2a845 6f 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 65  o.  Additional e
2a846 6e 74 72 69 65 73 20 61 72 65 20 6d 61 64 65 20  ntries are made 
2a847 6f 6e 20 74 68 65 20 41 67 67 49 6e 66 6f 20 6f  on the AggInfo o
2a848 62 6a 65 63 74 20 61 73 0a 2a 2a 20 6e 65 63 65  bject as.** nece
2a849 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ssary..**.** Thi
2a84a 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64  s routine should
2a84b 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20   only be called 
2a84c 61 66 74 65 72 20 74 68 65 20 65 78 70 72 65 73  after the expres
2a84d 73 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a  sion has been.**
2a84e 20 61 6e 61 6c 79 7a 65 64 20 62 79 20 73 71 6c   analyzed by sql
2a84f 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
2a850 61 6d 65 73 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  ames()..*/.SQLIT
2a851 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2a852 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a  qlite3ExprAnalyz
2a853 65 41 67 67 72 65 67 61 74 65 73 28 4e 61 6d 65  eAggregates(Name
2a854 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 45 78  Context *pNC, Ex
2a855 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 57 61  pr *pExpr){.  Wa
2a856 6c 6b 65 72 20 77 3b 0a 20 20 6d 65 6d 73 65 74  lker w;.  memset
2a857 28 26 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77  (&w, 0, sizeof(w
2a858 29 29 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c  ));.  w.xExprCal
2a859 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79 7a 65 41  lback = analyzeA
2a85a 67 67 72 65 67 61 74 65 3b 0a 20 20 77 2e 78 53  ggregate;.  w.xS
2a85b 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20  electCallback = 
2a85c 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  analyzeAggregate
2a85d 73 49 6e 53 65 6c 65 63 74 3b 0a 20 20 77 2e 75  sInSelect;.  w.u
2a85e 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 61 73  .pNC = pNC;.  as
2a85f 73 65 72 74 28 20 70 4e 43 2d 3e 70 53 72 63 4c  sert( pNC->pSrcL
2a860 69 73 74 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69  ist!=0 );.  sqli
2a861 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20  te3WalkExpr(&w, 
2a862 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pExpr);.}../*.**
2a863 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 45 78 70   Call sqlite3Exp
2a864 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
2a865 65 73 28 29 20 66 6f 72 20 65 76 65 72 79 20 65  es() for every e
2a866 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 61 6e 0a  xpression in an.
2a867 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  ** expression li
2a868 73 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  st.  Return the 
2a869 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
2a86a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72  ..**.** If an er
2a86b 72 6f 72 20 69 73 20 66 6f 75 6e 64 2c 20 74 68  ror is found, th
2a86c 65 20 61 6e 61 6c 79 73 69 73 20 69 73 20 63 75  e analysis is cu
2a86d 74 20 73 68 6f 72 74 2e 0a 2a 2f 0a 53 51 4c 49  t short..*/.SQLI
2a86e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2a86f 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79  sqlite3ExprAnaly
2a870 7a 65 41 67 67 4c 69 73 74 28 4e 61 6d 65 43 6f  zeAggList(NameCo
2a871 6e 74 65 78 74 20 2a 70 4e 43 2c 20 45 78 70 72  ntext *pNC, Expr
2a872 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20  List *pList){.  
2a873 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
2a874 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69  item *pItem;.  i
2a875 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73  nt i;.  if( pLis
2a876 74 20 29 7b 0a 20 20 20 20 66 6f 72 28 70 49 74  t ){.    for(pIt
2a877 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30  em=pList->a, i=0
2a878 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72  ; i<pList->nExpr
2a879 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
2a87a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2a87b 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61  prAnalyzeAggrega
2a87c 74 65 73 28 70 4e 43 2c 20 70 49 74 65 6d 2d 3e  tes(pNC, pItem->
2a87d 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20  pExpr);.    }.  
2a87e 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  }.}../*.** Alloc
2a87f 61 74 65 20 61 20 73 69 6e 67 6c 65 20 6e 65 77  ate a single new
2a880 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 75 73   register for us
2a881 65 20 74 6f 20 68 6f 6c 64 20 73 6f 6d 65 20 69  e to hold some i
2a882 6e 74 65 72 6d 65 64 69 61 74 65 20 72 65 73 75  ntermediate resu
2a883 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  lt..*/.SQLITE_PR
2a884 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2a885 33 47 65 74 54 65 6d 70 52 65 67 28 50 61 72 73  3GetTempReg(Pars
2a886 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 66  e *pParse){.  if
2a887 28 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52  ( pParse->nTempR
2a888 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  eg==0 ){.    ret
2a889 75 72 6e 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  urn ++pParse->nM
2a88a 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  em;.  }.  return
2a88b 20 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65   pParse->aTempRe
2a88c 67 5b 2d 2d 70 50 61 72 73 65 2d 3e 6e 54 65 6d  g[--pParse->nTem
2a88d 70 52 65 67 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pReg];.}../*.** 
2a88e 44 65 61 6c 6c 6f 63 61 74 65 20 61 20 72 65 67  Deallocate a reg
2a88f 69 73 74 65 72 2c 20 6d 61 6b 69 6e 67 20 61 76  ister, making av
2a890 61 69 6c 61 62 6c 65 20 66 6f 72 20 72 65 75 73  ailable for reus
2a891 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72  e for some other
2a892 0a 2a 2a 20 70 75 72 70 6f 73 65 2e 0a 2a 2a 0a  .** purpose..**.
2a893 2a 2a 20 49 66 20 61 20 72 65 67 69 73 74 65 72  ** If a register
2a894 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 62 65   is currently be
2a895 69 6e 67 20 75 73 65 64 20 62 79 20 74 68 65 20  ing used by the 
2a896 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2c 20 74 68  column cache, th
2a897 65 6e 0a 2a 2a 20 74 68 65 20 64 61 6c 6c 6f 63  en.** the dalloc
2a898 61 74 69 6f 6e 20 69 73 20 64 65 66 65 72 72 65  ation is deferre
2a899 64 20 75 6e 74 69 6c 20 74 68 65 20 63 6f 6c 75  d until the colu
2a89a 6d 6e 20 63 61 63 68 65 20 6c 69 6e 65 20 74 68  mn cache line th
2a89b 61 74 20 75 73 65 73 0a 2a 2a 20 74 68 65 20 72  at uses.** the r
2a89c 65 67 69 73 74 65 72 20 62 65 63 6f 6d 65 73 20  egister becomes 
2a89d 73 74 61 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  stale..*/.SQLITE
2a89e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2a89f 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2a8a0 52 65 67 28 50 61 72 73 65 20 2a 70 50 61 72 73  Reg(Parse *pPars
2a8a1 65 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20 20  e, int iReg){.  
2a8a2 69 66 28 20 69 52 65 67 20 26 26 20 70 50 61 72  if( iReg && pPar
2a8a3 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c 41 72 72  se->nTempReg<Arr
2a8a4 61 79 53 69 7a 65 28 70 50 61 72 73 65 2d 3e 61  aySize(pParse->a
2a8a5 54 65 6d 70 52 65 67 29 20 29 7b 0a 20 20 20 20  TempReg) ){.    
2a8a6 69 6e 74 20 69 3b 0a 20 20 20 20 73 74 72 75 63  int i;.    struc
2a8a7 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a  t yColCache *p;.
2a8a8 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70      for(i=0, p=p
2a8a9 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65  Parse->aColCache
2a8aa 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c  ; i<SQLITE_N_COL
2a8ab 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29  CACHE; i++, p++)
2a8ac 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69  {.      if( p->i
2a8ad 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a 20 20 20  Reg==iReg ){.   
2a8ae 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20       p->tempReg 
2a8af 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74  = 1;.        ret
2a8b0 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
2a8b1 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61   }.    pParse->a
2a8b2 54 65 6d 70 52 65 67 5b 70 50 61 72 73 65 2d 3e  TempReg[pParse->
2a8b3 6e 54 65 6d 70 52 65 67 2b 2b 5d 20 3d 20 69 52  nTempReg++] = iR
2a8b4 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  eg;.  }.}../*.**
2a8b5 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 64 65 61   Allocate or dea
2a8b6 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20  llocate a block 
2a8b7 6f 66 20 6e 52 65 67 20 63 6f 6e 73 65 63 75 74  of nReg consecut
2a8b8 69 76 65 20 72 65 67 69 73 74 65 72 73 0a 2a 2f  ive registers.*/
2a8b9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2a8ba 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65  int sqlite3GetTe
2a8bb 6d 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a 70  mpRange(Parse *p
2a8bc 50 61 72 73 65 2c 20 69 6e 74 20 6e 52 65 67 29  Parse, int nReg)
2a8bd 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  {.  int i, n;.  
2a8be 69 20 3d 20 70 50 61 72 73 65 2d 3e 69 52 61 6e  i = pParse->iRan
2a8bf 67 65 52 65 67 3b 0a 20 20 6e 20 3d 20 70 50 61  geReg;.  n = pPa
2a8c0 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 3b 0a  rse->nRangeReg;.
2a8c1 20 20 69 66 28 20 6e 52 65 67 3c 3d 6e 20 29 7b    if( nReg<=n ){
2a8c2 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 75 73  .    assert( !us
2a8c3 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28  edAsColumnCache(
2a8c4 70 50 61 72 73 65 2c 20 69 2c 20 69 2b 6e 2d 31  pParse, i, i+n-1
2a8c5 29 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  ) );.    pParse-
2a8c6 3e 69 52 61 6e 67 65 52 65 67 20 2b 3d 20 6e 52  >iRangeReg += nR
2a8c7 65 67 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  eg;.    pParse->
2a8c8 6e 52 61 6e 67 65 52 65 67 20 2d 3d 20 6e 52 65  nRangeReg -= nRe
2a8c9 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  g;.  }else{.    
2a8ca 69 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  i = pParse->nMem
2a8cb 2b 31 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  +1;.    pParse->
2a8cc 6e 4d 65 6d 20 2b 3d 20 6e 52 65 67 3b 0a 20 20  nMem += nReg;.  
2a8cd 7d 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a  }.  return i;.}.
2a8ce 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2a8cf 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61  oid sqlite3Relea
2a8d0 73 65 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73  seTempRange(Pars
2a8d1 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
2a8d2 52 65 67 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a  Reg, int nReg){.
2a8d3 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2a8d4 68 65 52 65 6d 6f 76 65 28 70 50 61 72 73 65 2c  heRemove(pParse,
2a8d5 20 69 52 65 67 2c 20 6e 52 65 67 29 3b 0a 20 20   iReg, nReg);.  
2a8d6 69 66 28 20 6e 52 65 67 3e 70 50 61 72 73 65 2d  if( nReg>pParse-
2a8d7 3e 6e 52 61 6e 67 65 52 65 67 20 29 7b 0a 20 20  >nRangeReg ){.  
2a8d8 20 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65    pParse->nRange
2a8d9 52 65 67 20 3d 20 6e 52 65 67 3b 0a 20 20 20 20  Reg = nReg;.    
2a8da 70 50 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65  pParse->iRangeRe
2a8db 67 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a  g = iReg;.  }.}.
2a8dc 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 6c 6c 20  ./*.** Mark all 
2a8dd 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
2a8de 65 72 73 20 61 73 20 62 65 69 6e 67 20 75 6e 61  ers as being una
2a8df 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 72 65 75  vailable for reu
2a8e0 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
2a8e1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2a8e2 65 33 43 6c 65 61 72 54 65 6d 70 52 65 67 43 61  e3ClearTempRegCa
2a8e3 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  che(Parse *pPars
2a8e4 65 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54  e){.  pParse->nT
2a8e5 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 70 50  empReg = 0;.  pP
2a8e6 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20  arse->nRangeReg 
2a8e7 3d 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  = 0;.}../*******
2a8e8 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 65  ******* End of e
2a8e9 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  xpr.c **********
2a8ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a8eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a8ec 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2a8ed 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2a8ee 6c 65 20 61 6c 74 65 72 2e 63 20 2a 2a 2a 2a 2a  le alter.c *****
2a8ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a8f1 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
2a8f2 30 35 20 46 65 62 72 75 61 72 79 20 31 35 0a 2a  05 February 15.*
2a8f3 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
2a8f4 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
2a8f5 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
2a8f6 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
2a8f7 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
2a8f8 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
2a8f9 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
2a8fa 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
2a8fb 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
2a8fc 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
2a8fd 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
2a8fe 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
2a8ff 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
2a900 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
2a901 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
2a902 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
2a903 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
2a904 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
2a905 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a906 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a907 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a908 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
2a909 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
2a90a 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69  ins C code routi
2a90b 6e 65 73 20 74 68 61 74 20 75 73 65 64 20 74 6f  nes that used to
2a90c 20 67 65 6e 65 72 61 74 65 20 56 44 42 45 20 63   generate VDBE c
2a90d 6f 64 65 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c  ode.** that impl
2a90e 65 6d 65 6e 74 73 20 74 68 65 20 41 4c 54 45 52  ements the ALTER
2a90f 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 0a   TABLE command..
2a910 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f  */../*.** The co
2a911 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
2a912 6f 6e 6c 79 20 65 78 69 73 74 73 20 69 66 20 77  only exists if w
2a913 65 20 61 72 65 20 6e 6f 74 20 6f 6d 69 74 74 69  e are not omitti
2a914 6e 67 20 74 68 65 0a 2a 2a 20 41 4c 54 45 52 20  ng the.** ALTER 
2a915 54 41 42 4c 45 20 6c 6f 67 69 63 20 66 72 6f 6d  TABLE logic from
2a916 20 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23   the build..*/.#
2a917 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2a918 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 0a 0a  IT_ALTERTABLE...
2a919 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
2a91a 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 53  ion is used by S
2a91b 51 4c 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20  QL generated to 
2a91c 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 0a 2a  implement the .*
2a91d 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f  * ALTER TABLE co
2a91e 6d 6d 61 6e 64 2e 20 54 68 65 20 66 69 72 73 74  mmand. The first
2a91f 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
2a920 20 74 65 78 74 20 6f 66 20 61 20 43 52 45 41 54   text of a CREAT
2a921 45 20 54 41 42 4c 45 20 6f 72 0a 2a 2a 20 43 52  E TABLE or.** CR
2a922 45 41 54 45 20 49 4e 44 45 58 20 63 6f 6d 6d 61  EATE INDEX comma
2a923 6e 64 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69  nd. The second i
2a924 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  s a table name. 
2a925 54 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  The table name i
2a926 6e 20 0a 2a 2a 20 74 68 65 20 43 52 45 41 54 45  n .** the CREATE
2a927 20 54 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45   TABLE or CREATE
2a928 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74   INDEX statement
2a929 20 69 73 20 72 65 70 6c 61 63 65 64 20 77 69 74   is replaced wit
2a92a 68 20 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 61  h the third.** a
2a92b 72 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20  rgument and the 
2a92c 72 65 73 75 6c 74 20 72 65 74 75 72 6e 65 64 2e  result returned.
2a92d 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a   Examples:.**.**
2a92e 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74   sqlite_rename_t
2a92f 61 62 6c 65 28 27 43 52 45 41 54 45 20 54 41 42  able('CREATE TAB
2a930 4c 45 20 61 62 63 28 61 2c 20 62 2c 20 63 29 27  LE abc(a, b, c)'
2a931 2c 20 27 64 65 66 27 29 0a 2a 2a 20 20 20 20 20  , 'def').**     
2a932 2d 3e 20 27 43 52 45 41 54 45 20 54 41 42 4c 45  -> 'CREATE TABLE
2a933 20 64 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a   def(a, b, c)'.*
2a934 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e 61  *.** sqlite_rena
2a935 6d 65 5f 74 61 62 6c 65 28 27 43 52 45 41 54 45  me_table('CREATE
2a936 20 49 4e 44 45 58 20 69 20 4f 4e 20 61 62 63 28   INDEX i ON abc(
2a937 61 29 27 2c 20 27 64 65 66 27 29 0a 2a 2a 20 20  a)', 'def').**  
2a938 20 20 20 2d 3e 20 27 43 52 45 41 54 45 20 49 4e     -> 'CREATE IN
2a939 44 45 58 20 69 20 4f 4e 20 64 65 66 28 61 2c 20  DEX i ON def(a, 
2a93a 62 2c 20 63 29 27 0a 2a 2f 0a 73 74 61 74 69 63  b, c)'.*/.static
2a93b 20 76 6f 69 64 20 72 65 6e 61 6d 65 54 61 62 6c   void renameTabl
2a93c 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
2a93d 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
2a93e 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
2a93f 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
2a940 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e  e **argv.){.  un
2a941 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
2a942 74 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  t *zSql = sqlite
2a943 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
2a944 76 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65  v[0]);.  unsigne
2a945 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54  d char const *zT
2a946 61 62 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74  ableName = sqlit
2a947 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
2a948 67 76 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74  gv[1]);..  int t
2a949 6f 6b 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e  oken;.  Token tn
2a94a 61 6d 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  ame;.  unsigned 
2a94b 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72  char const *zCsr
2a94c 20 3d 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c   = zSql;.  int l
2a94d 65 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a  en = 0;.  char *
2a94e 7a 52 65 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33  zRet;..  sqlite3
2a94f 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63   *db = sqlite3_c
2a950 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
2a951 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 55 4e  (context);..  UN
2a952 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
2a953 6f 74 55 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54  otUsed);..  /* T
2a954 68 65 20 70 72 69 6e 63 69 70 6c 65 20 75 73 65  he principle use
2a955 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20  d to locate the 
2a956 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68  table name in th
2a957 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 0a  e CREATE TABLE .
2a958 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69    ** statement i
2a959 73 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65  s that the table
2a95a 20 6e 61 6d 65 20 69 73 20 74 68 65 20 66 69 72   name is the fir
2a95b 73 74 20 6e 6f 6e 2d 73 70 61 63 65 20 74 6f 6b  st non-space tok
2a95c 65 6e 20 74 68 61 74 0a 20 20 2a 2a 20 69 73 20  en that.  ** is 
2a95d 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c  immediately foll
2a95e 6f 77 65 64 20 62 79 20 61 20 54 4b 5f 4c 50 20  owed by a TK_LP 
2a95f 6f 72 20 54 4b 5f 55 53 49 4e 47 20 74 6f 6b 65  or TK_USING toke
2a960 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 53  n..  */.  if( zS
2a961 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 20  ql ){.    do {. 
2a962 20 20 20 20 20 69 66 28 20 21 2a 7a 43 73 72 20       if( !*zCsr 
2a963 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 61  ){.        /* Ra
2a964 6e 20 6f 75 74 20 6f 66 20 69 6e 70 75 74 20 62  n out of input b
2a965 65 66 6f 72 65 20 66 69 6e 64 69 6e 67 20 61 6e  efore finding an
2a966 20 6f 70 65 6e 69 6e 67 20 62 72 61 63 6b 65 74   opening bracket
2a967 2e 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a  . Return NULL. *
2a968 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  /.        return
2a969 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
2a96a 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f   /* Store the to
2a96b 6b 65 6e 20 74 68 61 74 20 7a 43 73 72 20 70 6f  ken that zCsr po
2a96c 69 6e 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65  ints to in tname
2a96d 2e 20 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65  . */.      tname
2a96e 2e 7a 20 3d 20 28 63 68 61 72 2a 29 7a 43 73 72  .z = (char*)zCsr
2a96f 3b 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20  ;.      tname.n 
2a970 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a  = len;..      /*
2a971 20 41 64 76 61 6e 63 65 20 7a 43 73 72 20 74 6f   Advance zCsr to
2a972 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e   the next token.
2a973 20 53 74 6f 72 65 20 74 68 61 74 20 74 6f 6b 65   Store that toke
2a974 6e 20 74 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e  n type in 'token
2a975 27 2c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  ',.      ** and 
2a976 69 74 73 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c  its length in 'l
2a977 65 6e 27 20 28 74 6f 20 62 65 20 75 73 65 64 20  en' (to be used 
2a978 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f  next iteration o
2a979 66 20 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20  f this loop)..  
2a97a 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20      */.      do 
2a97b 7b 0a 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b  {.        zCsr +
2a97c 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c  = len;.        l
2a97d 65 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  en = sqlite3GetT
2a97e 6f 6b 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65  oken(zCsr, &toke
2a97f 6e 29 3b 0a 20 20 20 20 20 20 7d 20 77 68 69 6c  n);.      } whil
2a980 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41  e( token==TK_SPA
2a981 43 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  CE );.      asse
2a982 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0a 20 20 20  rt( len>0 );.   
2a983 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65 6e 21   } while( token!
2a984 3d 54 4b 5f 4c 50 20 26 26 20 74 6f 6b 65 6e 21  =TK_LP && token!
2a985 3d 54 4b 5f 55 53 49 4e 47 20 29 3b 0a 0a 20 20  =TK_USING );..  
2a986 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 74 65 33    zRet = sqlite3
2a987 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 2e 2a  MPrintf(db, "%.*
2a988 73 5c 22 25 77 5c 22 25 73 22 2c 20 28 28 75 38  s\"%w\"%s", ((u8
2a989 2a 29 74 6e 61 6d 65 2e 7a 29 20 2d 20 7a 53 71  *)tname.z) - zSq
2a98a 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20 20 20  l, zSql, .      
2a98b 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74 6e 61   zTableName, tna
2a98c 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20  me.z+tname.n);. 
2a98d 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
2a98e 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
2a98f 7a 52 65 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45  zRet, -1, SQLITE
2a990 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d  _DYNAMIC);.  }.}
2a991 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 43 20 66  ../*.** This C f
2a992 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  unction implemen
2a993 74 73 20 61 6e 20 53 51 4c 20 75 73 65 72 20 66  ts an SQL user f
2a994 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 69 73 20  unction that is 
2a995 75 73 65 64 20 62 79 20 53 51 4c 20 63 6f 64 65  used by SQL code
2a996 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 62 79  .** generated by
2a997 20 74 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45   the ALTER TABLE
2a998 20 2e 2e 2e 20 52 45 4e 41 4d 45 20 63 6f 6d 6d   ... RENAME comm
2a999 61 6e 64 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  and to modify th
2a99a 65 20 64 65 66 69 6e 69 74 69 6f 6e 0a 2a 2a 20  e definition.** 
2a99b 6f 66 20 61 6e 79 20 66 6f 72 65 69 67 6e 20 6b  of any foreign k
2a99c 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74  ey constraints t
2a99d 68 61 74 20 75 73 65 20 74 68 65 20 74 61 62 6c  hat use the tabl
2a99e 65 20 62 65 69 6e 67 20 72 65 6e 61 6d 65 64 20  e being renamed 
2a99f 61 73 20 74 68 65 20 0a 2a 2a 20 70 61 72 65 6e  as the .** paren
2a9a0 74 20 74 61 62 6c 65 2e 20 49 74 20 69 73 20 70  t table. It is p
2a9a1 61 73 73 65 64 20 74 68 72 65 65 20 61 72 67 75  assed three argu
2a9a2 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  ments:.**.**   1
2a9a3 29 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 74  ) The complete t
2a9a4 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54  ext of the CREAT
2a9a5 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
2a9a6 74 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64  t being modified
2a9a7 2c 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 6f 6c  ,.**   2) The ol
2a9a8 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  d name of the ta
2a9a9 62 6c 65 20 62 65 69 6e 67 20 72 65 6e 61 6d 65  ble being rename
2a9aa 64 2c 20 61 6e 64 0a 2a 2a 20 20 20 33 29 20 54  d, and.**   3) T
2a9ab 68 65 20 6e 65 77 20 6e 61 6d 65 20 6f 66 20 74  he new name of t
2a9ac 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72  he table being r
2a9ad 65 6e 61 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74  enamed..**.** It
2a9ae 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 65 77   returns the new
2a9af 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
2a9b0 61 74 65 6d 65 6e 74 2e 20 46 6f 72 20 65 78 61  atement. For exa
2a9b1 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 71  mple:.**.**   sq
2a9b2 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 70 61 72 65  lite_rename_pare
2a9b3 6e 74 28 27 43 52 45 41 54 45 20 54 41 42 4c 45  nt('CREATE TABLE
2a9b4 20 74 31 28 61 20 52 45 46 45 52 45 4e 43 45 53   t1(a REFERENCES
2a9b5 20 74 32 29 27 2c 20 27 74 32 27 2c 20 27 74 33   t2)', 't2', 't3
2a9b6 27 29 0a 2a 2a 20 20 20 20 20 20 20 2d 3e 20 27  ').**       -> '
2a9b7 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
2a9b8 61 20 52 45 46 45 52 45 4e 43 45 53 20 74 33 29  a REFERENCES t3)
2a9b9 27 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  '.*/.#ifndef SQL
2a9ba 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
2a9bb 5f 4b 45 59 0a 73 74 61 74 69 63 20 76 6f 69 64  _KEY.static void
2a9bc 20 72 65 6e 61 6d 65 50 61 72 65 6e 74 46 75 6e   renameParentFun
2a9bd 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
2a9be 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
2a9bf 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20   int NotUsed,.  
2a9c0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2a9c1 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74 65  argv.){.  sqlite
2a9c2 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
2a9c3 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
2a9c4 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63 68  e(context);.  ch
2a9c5 61 72 20 2a 7a 4f 75 74 70 75 74 20 3d 20 30 3b  ar *zOutput = 0;
2a9c6 0a 20 20 63 68 61 72 20 2a 7a 52 65 73 75 6c 74  .  char *zResult
2a9c7 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
2a9c8 72 20 63 6f 6e 73 74 20 2a 7a 49 6e 70 75 74 20  r const *zInput 
2a9c9 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
2a9ca 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
2a9cb 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
2a9cc 6f 6e 73 74 20 2a 7a 4f 6c 64 20 3d 20 73 71 6c  onst *zOld = sql
2a9cd 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
2a9ce 61 72 67 76 5b 31 5d 29 3b 0a 20 20 75 6e 73 69  argv[1]);.  unsi
2a9cf 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
2a9d0 2a 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f  *zNew = sqlite3_
2a9d1 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
2a9d2 32 5d 29 3b 0a 0a 20 20 75 6e 73 69 67 6e 65 64  2]);..  unsigned
2a9d3 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 20   const char *z; 
2a9d4 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
2a9d5 65 72 20 74 6f 20 74 6f 6b 65 6e 20 2a 2f 0a 20  er to token */. 
2a9d6 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20   int n;         
2a9d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a9d8 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 6f   /* Length of to
2a9d9 6b 65 6e 20 7a 20 2a 2f 0a 20 20 69 6e 74 20 74  ken z */.  int t
2a9da 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  oken;           
2a9db 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79             /* Ty
2a9dc 70 65 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 0a  pe of token */..
2a9dd 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
2a9de 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 66  ER(NotUsed);.  f
2a9df 6f 72 28 7a 3d 7a 49 6e 70 75 74 3b 20 2a 7a 3b  or(z=zInput; *z;
2a9e0 20 7a 3d 7a 2b 6e 29 7b 0a 20 20 20 20 6e 20 3d   z=z+n){.    n =
2a9e1 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e   sqlite3GetToken
2a9e2 28 7a 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20  (z, &token);.   
2a9e3 20 69 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 52   if( token==TK_R
2a9e4 45 46 45 52 45 4e 43 45 53 20 29 7b 0a 20 20 20  EFERENCES ){.   
2a9e5 20 20 20 63 68 61 72 20 2a 7a 50 61 72 65 6e 74     char *zParent
2a9e6 3b 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20  ;.      do {.   
2a9e7 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20       z += n;.   
2a9e8 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33       n = sqlite3
2a9e9 47 65 74 54 6f 6b 65 6e 28 7a 2c 20 26 74 6f 6b  GetToken(z, &tok
2a9ea 65 6e 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c  en);.      }whil
2a9eb 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41  e( token==TK_SPA
2a9ec 43 45 20 29 3b 0a 0a 20 20 20 20 20 20 7a 50 61  CE );..      zPa
2a9ed 72 65 6e 74 20 3d 20 73 71 6c 69 74 65 33 44 62  rent = sqlite3Db
2a9ee 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 6f 6e  StrNDup(db, (con
2a9ef 73 74 20 63 68 61 72 20 2a 29 7a 2c 20 6e 29 3b  st char *)z, n);
2a9f0 0a 20 20 20 20 20 20 69 66 28 20 7a 50 61 72 65  .      if( zPare
2a9f1 6e 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  nt==0 ) break;. 
2a9f2 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 71 75       sqlite3Dequ
2a9f3 6f 74 65 28 7a 50 61 72 65 6e 74 29 3b 0a 20 20  ote(zParent);.  
2a9f4 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74      if( 0==sqlit
2a9f5 65 33 53 74 72 49 43 6d 70 28 28 63 6f 6e 73 74  e3StrICmp((const
2a9f6 20 63 68 61 72 20 2a 29 7a 4f 6c 64 2c 20 7a 50   char *)zOld, zP
2a9f7 61 72 65 6e 74 29 20 29 7b 0a 20 20 20 20 20 20  arent) ){.      
2a9f8 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 3d 20 73    char *zOut = s
2a9f9 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2a9fa 2c 20 22 25 73 25 2e 2a 73 5c 22 25 77 5c 22 22  , "%s%.*s\"%w\""
2a9fb 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 28  , .            (
2a9fc 7a 4f 75 74 70 75 74 3f 7a 4f 75 74 70 75 74 3a  zOutput?zOutput:
2a9fd 22 22 29 2c 20 7a 2d 7a 49 6e 70 75 74 2c 20 7a  ""), z-zInput, z
2a9fe 49 6e 70 75 74 2c 20 28 63 6f 6e 73 74 20 63 68  Input, (const ch
2a9ff 61 72 20 2a 29 7a 4e 65 77 0a 20 20 20 20 20 20  ar *)zNew.      
2aa00 20 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c    );.        sql
2aa01 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2aa02 4f 75 74 70 75 74 29 3b 0a 20 20 20 20 20 20 20  Output);.       
2aa03 20 7a 4f 75 74 70 75 74 20 3d 20 7a 4f 75 74 3b   zOutput = zOut;
2aa04 0a 20 20 20 20 20 20 20 20 7a 49 6e 70 75 74 20  .        zInput 
2aa05 3d 20 26 7a 5b 6e 5d 3b 0a 20 20 20 20 20 20 7d  = &z[n];.      }
2aa06 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2aa07 46 72 65 65 28 64 62 2c 20 7a 50 61 72 65 6e 74  Free(db, zParent
2aa08 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
2aa09 7a 52 65 73 75 6c 74 20 3d 20 73 71 6c 69 74 65  zResult = sqlite
2aa0a 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73  3MPrintf(db, "%s
2aa0b 25 73 22 2c 20 28 7a 4f 75 74 70 75 74 3f 7a 4f  %s", (zOutput?zO
2aa0c 75 74 70 75 74 3a 22 22 29 2c 20 7a 49 6e 70 75  utput:""), zInpu
2aa0d 74 29 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 72  t), .  sqlite3_r
2aa0e 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
2aa0f 78 74 2c 20 7a 52 65 73 75 6c 74 2c 20 2d 31 2c  xt, zResult, -1,
2aa10 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29   SQLITE_DYNAMIC)
2aa11 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
2aa12 65 28 64 62 2c 20 7a 4f 75 74 70 75 74 29 3b 0a  e(db, zOutput);.
2aa13 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
2aa14 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
2aa15 49 47 47 45 52 0a 2f 2a 20 54 68 69 73 20 66 75  IGGER./* This fu
2aa16 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62  nction is used b
2aa17 79 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64 20  y SQL generated 
2aa18 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
2aa19 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20  .** ALTER TABLE 
2aa1a 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 66 69 72  command. The fir
2aa1b 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  st argument is t
2aa1c 68 65 20 74 65 78 74 20 6f 66 20 61 20 43 52 45  he text of a CRE
2aa1d 41 54 45 20 54 52 49 47 47 45 52 20 0a 2a 2a 20  ATE TRIGGER .** 
2aa1e 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 73  statement. The s
2aa1f 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62 6c 65  econd is a table
2aa20 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62 6c 65   name. The table
2aa21 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52 45   name in the CRE
2aa22 41 54 45 20 0a 2a 2a 20 54 52 49 47 47 45 52 20  ATE .** TRIGGER 
2aa23 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65 70  statement is rep
2aa24 6c 61 63 65 64 20 77 69 74 68 20 74 68 65 20 74  laced with the t
2aa25 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 61 6e  hird argument an
2aa26 64 20 74 68 65 20 72 65 73 75 6c 74 20 0a 2a 2a  d the result .**
2aa27 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 20   returned. This 
2aa28 69 73 20 61 6e 61 6c 61 67 6f 75 73 20 74 6f 20  is analagous to 
2aa29 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 28  renameTableFunc(
2aa2a 29 20 61 62 6f 76 65 2c 20 65 78 63 65 70 74 20  ) above, except 
2aa2b 66 6f 72 20 43 52 45 41 54 45 0a 2a 2a 20 54 52  for CREATE.** TR
2aa2c 49 47 47 45 52 2c 20 6e 6f 74 20 43 52 45 41 54  IGGER, not CREAT
2aa2d 45 20 49 4e 44 45 58 20 61 6e 64 20 43 52 45 41  E INDEX and CREA
2aa2e 54 45 20 54 41 42 4c 45 2e 0a 2a 2f 0a 73 74 61  TE TABLE..*/.sta
2aa2f 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 54  tic void renameT
2aa30 72 69 67 67 65 72 46 75 6e 63 28 0a 20 20 73 71  riggerFunc(.  sq
2aa31 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
2aa32 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f  ontext,.  int No
2aa33 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33  tUsed,.  sqlite3
2aa34 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
2aa35 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
2aa36 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d 20 73   const *zSql = s
2aa37 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2aa38 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 75 6e  t(argv[0]);.  un
2aa39 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
2aa3a 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 3d 20  t *zTableName = 
2aa3b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2aa3c 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20  xt(argv[1]);..  
2aa3d 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54 6f 6b  int token;.  Tok
2aa3e 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 69 6e 74 20  en tname;.  int 
2aa3f 64 69 73 74 20 3d 20 33 3b 0a 20 20 75 6e 73 69  dist = 3;.  unsi
2aa40 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
2aa41 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a 20 20  *zCsr = zSql;.  
2aa42 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 63  int len = 0;.  c
2aa43 68 61 72 20 2a 7a 52 65 74 3b 0a 20 20 73 71 6c  har *zRet;.  sql
2aa44 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
2aa45 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
2aa46 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a  ndle(context);..
2aa47 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
2aa48 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20  ER(NotUsed);..  
2aa49 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 70 6c 65  /* The principle
2aa4a 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20   used to locate 
2aa4b 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  the table name i
2aa4c 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 52 49  n the CREATE TRI
2aa4d 47 47 45 52 20 0a 20 20 2a 2a 20 73 74 61 74 65  GGER .  ** state
2aa4e 6d 65 6e 74 20 69 73 20 74 68 61 74 20 74 68 65  ment is that the
2aa4f 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 74   table name is t
2aa50 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 20 74  he first token t
2aa51 68 61 74 20 69 73 20 69 6d 6d 65 64 69 61 74 65  hat is immediate
2aa52 64 6c 79 0a 20 20 2a 2a 20 70 72 65 63 65 64 65  dly.  ** precede
2aa53 64 20 62 79 20 65 69 74 68 65 72 20 54 4b 5f 4f  d by either TK_O
2aa54 4e 20 6f 72 20 54 4b 5f 44 4f 54 20 61 6e 64 20  N or TK_DOT and 
2aa55 69 6d 6d 65 64 69 61 74 65 64 6c 79 20 66 6f 6c  immediatedly fol
2aa56 6c 6f 77 65 64 20 62 79 20 6f 6e 65 0a 20 20 2a  lowed by one.  *
2aa57 2a 20 6f 66 20 54 4b 5f 57 48 45 4e 2c 20 54 4b  * of TK_WHEN, TK
2aa58 5f 42 45 47 49 4e 20 6f 72 20 54 4b 5f 46 4f 52  _BEGIN or TK_FOR
2aa59 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 53 71  ..  */.  if( zSq
2aa5a 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 0a 20  l ){.    do {.. 
2aa5b 20 20 20 20 20 69 66 28 20 21 2a 7a 43 73 72 20       if( !*zCsr 
2aa5c 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 61  ){.        /* Ra
2aa5d 6e 20 6f 75 74 20 6f 66 20 69 6e 70 75 74 20 62  n out of input b
2aa5e 65 66 6f 72 65 20 66 69 6e 64 69 6e 67 20 74 68  efore finding th
2aa5f 65 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 52 65  e table name. Re
2aa60 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20  turn NULL. */.  
2aa61 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
2aa62 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
2aa63 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e 20  Store the token 
2aa64 74 68 61 74 20 7a 43 73 72 20 70 6f 69 6e 74 73  that zCsr points
2aa65 20 74 6f 20 69 6e 20 74 6e 61 6d 65 2e 20 2a 2f   to in tname. */
2aa66 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 7a 20 3d  .      tname.z =
2aa67 20 28 63 68 61 72 2a 29 7a 43 73 72 3b 0a 20 20   (char*)zCsr;.  
2aa68 20 20 20 20 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65      tname.n = le
2aa69 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41 64 76  n;..      /* Adv
2aa6a 61 6e 63 65 20 7a 43 73 72 20 74 6f 20 74 68 65  ance zCsr to the
2aa6b 20 6e 65 78 74 20 74 6f 6b 65 6e 2e 20 53 74 6f   next token. Sto
2aa6c 72 65 20 74 68 61 74 20 74 6f 6b 65 6e 20 74 79  re that token ty
2aa6d 70 65 20 69 6e 20 27 74 6f 6b 65 6e 27 2c 0a 20  pe in 'token',. 
2aa6e 20 20 20 20 20 2a 2a 20 61 6e 64 20 69 74 73 20       ** and its 
2aa6f 6c 65 6e 67 74 68 20 69 6e 20 27 6c 65 6e 27 20  length in 'len' 
2aa70 28 74 6f 20 62 65 20 75 73 65 64 20 6e 65 78 74  (to be used next
2aa71 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   iteration of th
2aa72 69 73 20 6c 6f 6f 70 29 2e 0a 20 20 20 20 20 20  is loop)..      
2aa73 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20  */.      do {.  
2aa74 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 6c 65        zCsr += le
2aa75 6e 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 3d  n;.        len =
2aa76 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e   sqlite3GetToken
2aa77 28 7a 43 73 72 2c 20 26 74 6f 6b 65 6e 29 3b 0a  (zCsr, &token);.
2aa78 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 74 6f        }while( to
2aa79 6b 65 6e 3d 3d 54 4b 5f 53 50 41 43 45 20 29 3b  ken==TK_SPACE );
2aa7a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c  .      assert( l
2aa7b 65 6e 3e 30 20 29 3b 0a 0a 20 20 20 20 20 20 2f  en>0 );..      /
2aa7c 2a 20 56 61 72 69 61 62 6c 65 20 27 64 69 73 74  * Variable 'dist
2aa7d 27 20 73 74 6f 72 65 73 20 74 68 65 20 6e 75 6d  ' stores the num
2aa7e 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 72 65  ber of tokens re
2aa7f 61 64 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73  ad since the mos
2aa80 74 0a 20 20 20 20 20 20 2a 2a 20 72 65 63 65 6e  t.      ** recen
2aa81 74 20 54 4b 5f 44 4f 54 20 6f 72 20 54 4b 5f 4f  t TK_DOT or TK_O
2aa82 4e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68  N. This means th
2aa83 61 74 20 77 68 65 6e 20 61 20 57 48 45 4e 2c 20  at when a WHEN, 
2aa84 46 4f 52 20 6f 72 20 42 45 47 49 4e 20 0a 20 20  FOR or BEGIN .  
2aa85 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69 73 20      ** token is 
2aa86 72 65 61 64 20 61 6e 64 20 27 64 69 73 74 27 20  read and 'dist' 
2aa87 65 71 75 61 6c 73 20 32 2c 20 74 68 65 20 63 6f  equals 2, the co
2aa88 6e 64 69 74 69 6f 6e 20 73 74 61 74 65 64 20 61  ndition stated a
2aa89 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f  bove.      ** to
2aa8a 20 62 65 20 6d 65 74 2e 0a 20 20 20 20 20 20 2a   be met..      *
2aa8b 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20  *.      ** Note 
2aa8c 74 68 61 74 20 4f 4e 20 63 61 6e 6e 6f 74 20 62  that ON cannot b
2aa8d 65 20 61 20 64 61 74 61 62 61 73 65 2c 20 74 61  e a database, ta
2aa8e 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20 6e 61  ble or column na
2aa8f 6d 65 2c 20 73 6f 0a 20 20 20 20 20 20 2a 2a 20  me, so.      ** 
2aa90 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  there is no need
2aa91 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20   to worry about 
2aa92 73 79 6e 74 61 78 20 6c 69 6b 65 20 0a 20 20 20  syntax like .   
2aa93 20 20 20 2a 2a 20 22 43 52 45 41 54 45 20 54 52     ** "CREATE TR
2aa94 49 47 47 45 52 20 2e 2e 2e 20 4f 4e 20 4f 4e 2e  IGGER ... ON ON.
2aa95 4f 4e 20 42 45 47 49 4e 20 2e 2e 2e 22 20 65 74  ON BEGIN ..." et
2aa96 63 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  c..      */.    
2aa97 20 20 64 69 73 74 2b 2b 3b 0a 20 20 20 20 20 20    dist++;.      
2aa98 69 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 44 4f  if( token==TK_DO
2aa99 54 20 7c 7c 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 4f  T || token==TK_O
2aa9a 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 64 69 73  N ){.        dis
2aa9b 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  t = 0;.      }. 
2aa9c 20 20 20 7d 20 77 68 69 6c 65 28 20 64 69 73 74     } while( dist
2aa9d 21 3d 32 20 7c 7c 20 28 74 6f 6b 65 6e 21 3d 54  !=2 || (token!=T
2aa9e 4b 5f 57 48 45 4e 20 26 26 20 74 6f 6b 65 6e 21  K_WHEN && token!
2aa9f 3d 54 4b 5f 46 4f 52 20 26 26 20 74 6f 6b 65 6e  =TK_FOR && token
2aaa0 21 3d 54 4b 5f 42 45 47 49 4e 29 20 29 3b 0a 0a  !=TK_BEGIN) );..
2aaa1 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 20      /* Variable 
2aaa2 74 6e 61 6d 65 20 6e 6f 77 20 63 6f 6e 74 61 69  tname now contai
2aaa3 6e 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61  ns the token tha
2aaa4 74 20 69 73 20 74 68 65 20 6f 6c 64 20 74 61 62  t is the old tab
2aaa5 6c 65 2d 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 69  le-name.    ** i
2aaa6 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 52 49  n the CREATE TRI
2aaa7 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 0a  GGER statement..
2aaa8 20 20 20 20 2a 2f 0a 20 20 20 20 7a 52 65 74 20      */.    zRet 
2aaa9 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2aaaa 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c 22  (db, "%.*s\"%w\"
2aaab 25 73 22 2c 20 28 28 75 38 2a 29 74 6e 61 6d 65  %s", ((u8*)tname
2aaac 2e 7a 29 20 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c  .z) - zSql, zSql
2aaad 2c 20 0a 20 20 20 20 20 20 20 7a 54 61 62 6c 65  , .       zTable
2aaae 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e  Name, tname.z+tn
2aaaf 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73 71 6c 69  ame.n);.    sqli
2aab0 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
2aab1 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74 2c 20 2d  context, zRet, -
2aab2 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  1, SQLITE_DYNAMI
2aab3 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  C);.  }.}.#endif
2aab4 20 20 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d     /* !SQLITE_OM
2aab5 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 0a 2f  IT_TRIGGER */../
2aab6 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 62 75  *.** Register bu
2aab7 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73  ilt-in functions
2aab8 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69 6d   used to help im
2aab9 70 6c 65 6d 65 6e 74 20 41 4c 54 45 52 20 54 41  plement ALTER TA
2aaba 42 4c 45 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  BLE.*/.SQLITE_PR
2aabb 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2aabc 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73  e3AlterFunctions
2aabd 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
2aabe 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63   SQLITE_WSD Func
2aabf 44 65 66 20 61 41 6c 74 65 72 54 61 62 6c 65 46  Def aAlterTableF
2aac0 75 6e 63 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 46  uncs[] = {.    F
2aac1 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f 72  UNCTION(sqlite_r
2aac2 65 6e 61 6d 65 5f 74 61 62 6c 65 2c 20 20 20 32  ename_table,   2
2aac3 2c 20 30 2c 20 30 2c 20 72 65 6e 61 6d 65 54 61  , 0, 0, renameTa
2aac4 62 6c 65 46 75 6e 63 29 2c 0a 23 69 66 6e 64 65  bleFunc),.#ifnde
2aac5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
2aac6 49 47 47 45 52 0a 20 20 20 20 46 55 4e 43 54 49  IGGER.    FUNCTI
2aac7 4f 4e 28 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65  ON(sqlite_rename
2aac8 5f 74 72 69 67 67 65 72 2c 20 32 2c 20 30 2c 20  _trigger, 2, 0, 
2aac9 30 2c 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72  0, renameTrigger
2aaca 46 75 6e 63 29 2c 0a 23 65 6e 64 69 66 0a 23 69  Func),.#endif.#i
2aacb 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2aacc 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20  T_FOREIGN_KEY.  
2aacd 20 20 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74    FUNCTION(sqlit
2aace 65 5f 72 65 6e 61 6d 65 5f 70 61 72 65 6e 74 2c  e_rename_parent,
2aacf 20 20 33 2c 20 30 2c 20 30 2c 20 72 65 6e 61 6d    3, 0, 0, renam
2aad0 65 50 61 72 65 6e 74 46 75 6e 63 29 2c 0a 23 65  eParentFunc),.#e
2aad1 6e 64 69 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20  ndif.  };.  int 
2aad2 69 3b 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68  i;.  FuncDefHash
2aad3 20 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41   *pHash = &GLOBA
2aad4 4c 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73  L(FuncDefHash, s
2aad5 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63  qlite3GlobalFunc
2aad6 74 69 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65  tions);.  FuncDe
2aad7 66 20 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e 63  f *aFunc = (Func
2aad8 44 65 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e  Def*)&GLOBAL(Fun
2aad9 63 44 65 66 2c 20 61 41 6c 74 65 72 54 61 62 6c  cDef, aAlterTabl
2aada 65 46 75 6e 63 73 29 3b 0a 0a 20 20 66 6f 72 28  eFuncs);..  for(
2aadb 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65  i=0; i<ArraySize
2aadc 28 61 41 6c 74 65 72 54 61 62 6c 65 46 75 6e 63  (aAlterTableFunc
2aadd 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  s); i++){.    sq
2aade 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65  lite3FuncDefInse
2aadf 72 74 28 70 48 61 73 68 2c 20 26 61 46 75 6e 63  rt(pHash, &aFunc
2aae0 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  [i]);.  }.}../*.
2aae1 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
2aae2 20 69 73 20 75 73 65 64 20 74 6f 20 63 72 65 61   is used to crea
2aae3 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 20 65  te the text of e
2aae4 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68  xpressions of th
2aae5 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20  e form:.**.**   
2aae6 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61 6e 74 31 3e  name=<constant1>
2aae7 20 4f 52 20 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61   OR name=<consta
2aae8 6e 74 32 3e 20 4f 52 20 2e 2e 2e 0a 2a 2a 0a 2a  nt2> OR ....**.*
2aae9 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a 57  * If argument zW
2aaea 68 65 72 65 20 69 73 20 4e 55 4c 4c 2c 20 74 68  here is NULL, th
2aaeb 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 73 74 72  en a pointer str
2aaec 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ing containing t
2aaed 68 65 20 74 65 78 74 20 0a 2a 2a 20 22 6e 61 6d  he text .** "nam
2aaee 65 3d 3c 63 6f 6e 73 74 61 6e 74 3e 22 20 69 73  e=<constant>" is
2aaef 20 72 65 74 75 72 6e 65 64 2c 20 77 68 65 72 65   returned, where
2aaf0 20 3c 63 6f 6e 73 74 61 6e 74 3e 20 69 73 20 74   <constant> is t
2aaf1 68 65 20 71 75 6f 74 65 64 20 76 65 72 73 69 6f  he quoted versio
2aaf2 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 69  n.** of the stri
2aaf3 6e 67 20 70 61 73 73 65 64 20 61 73 20 61 72 67  ng passed as arg
2aaf4 75 6d 65 6e 74 20 7a 43 6f 6e 73 74 61 6e 74 2e  ument zConstant.
2aaf5 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 62 75   The returned bu
2aaf6 66 66 65 72 20 69 73 0a 2a 2a 20 61 6c 6c 6f 63  ffer is.** alloc
2aaf7 61 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74  ated using sqlit
2aaf8 65 33 44 62 4d 61 6c 6c 6f 63 28 29 2e 20 49 74  e3DbMalloc(). It
2aaf9 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69   is the responsi
2aafa 62 69 6c 69 74 79 20 6f 66 20 74 68 65 0a 2a 2a  bility of the.**
2aafb 20 63 61 6c 6c 65 72 20 74 6f 20 65 6e 73 75 72   caller to ensur
2aafc 65 20 74 68 61 74 20 69 74 20 69 73 20 65 76 65  e that it is eve
2aafd 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a  ntually freed..*
2aafe 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74  *.** If argument
2aaff 20 7a 57 68 65 72 65 20 69 73 20 6e 6f 74 20 4e   zWhere is not N
2ab00 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 73 74  ULL, then the st
2ab01 72 69 6e 67 20 72 65 74 75 72 6e 65 64 20 69 73  ring returned is
2ab02 20 0a 2a 2a 20 22 3c 77 68 65 72 65 3e 20 4f 52   .** "<where> OR
2ab03 20 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61 6e 74 3e   name=<constant>
2ab04 22 2c 20 77 68 65 72 65 20 3c 77 68 65 72 65 3e  ", where <where>
2ab05 20 69 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73   is the contents
2ab06 20 6f 66 20 7a 57 68 65 72 65 2e 0a 2a 2a 20 49   of zWhere..** I
2ab07 6e 20 74 68 69 73 20 63 61 73 65 20 7a 57 68 65  n this case zWhe
2ab08 72 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20  re is passed to 
2ab09 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 20  sqlite3DbFree() 
2ab0a 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
2ab0b 2e 0a 2a 2a 20 0a 2a 2f 0a 73 74 61 74 69 63 20  ..** .*/.static 
2ab0c 63 68 61 72 20 2a 77 68 65 72 65 4f 72 4e 61 6d  char *whereOrNam
2ab0d 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  e(sqlite3 *db, c
2ab0e 68 61 72 20 2a 7a 57 68 65 72 65 2c 20 63 68 61  har *zWhere, cha
2ab0f 72 20 2a 7a 43 6f 6e 73 74 61 6e 74 29 7b 0a 20  r *zConstant){. 
2ab10 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 69   char *zNew;.  i
2ab11 66 28 20 21 7a 57 68 65 72 65 20 29 7b 0a 20 20  f( !zWhere ){.  
2ab12 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    zNew = sqlite3
2ab13 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d  MPrintf(db, "nam
2ab14 65 3d 25 51 22 2c 20 7a 43 6f 6e 73 74 61 6e 74  e=%Q", zConstant
2ab15 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2ab16 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 4d 50  zNew = sqlite3MP
2ab17 72 69 6e 74 66 28 64 62 2c 20 22 25 73 20 4f 52  rintf(db, "%s OR
2ab18 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 57 68 65 72   name=%Q", zWher
2ab19 65 2c 20 7a 43 6f 6e 73 74 61 6e 74 29 3b 0a 20  e, zConstant);. 
2ab1a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2ab1b 28 64 62 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20  (db, zWhere);.  
2ab1c 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b  }.  return zNew;
2ab1d 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .}..#if !defined
2ab1e 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52  (SQLITE_OMIT_FOR
2ab1f 45 49 47 4e 5f 4b 45 59 29 20 26 26 20 21 64 65  EIGN_KEY) && !de
2ab20 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2ab21 54 5f 54 52 49 47 47 45 52 29 0a 2f 2a 0a 2a 2a  T_TRIGGER)./*.**
2ab22 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65   Generate the te
2ab23 78 74 20 6f 66 20 61 20 57 48 45 52 45 20 65 78  xt of a WHERE ex
2ab24 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 63  pression which c
2ab25 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65  an be used to se
2ab26 6c 65 63 74 20 61 6c 6c 0a 2a 2a 20 74 61 62 6c  lect all.** tabl
2ab27 65 73 20 74 68 61 74 20 68 61 76 65 20 66 6f 72  es that have for
2ab28 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
2ab29 69 6e 74 73 20 74 68 61 74 20 72 65 66 65 72 20  ints that refer 
2ab2a 74 6f 20 74 61 62 6c 65 20 70 54 61 62 20 28 69  to table pTab (i
2ab2b 2e 65 2e 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e  .e..** constrain
2ab2c 74 73 20 66 6f 72 20 77 68 69 63 68 20 70 54 61  ts for which pTa
2ab2d 62 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 20  b is the parent 
2ab2e 74 61 62 6c 65 29 20 66 72 6f 6d 20 74 68 65 20  table) from the 
2ab2f 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 0a 2a 2a  sqlite_master.**
2ab30 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69   table..*/.stati
2ab31 63 20 63 68 61 72 20 2a 77 68 65 72 65 46 6f 72  c char *whereFor
2ab32 65 69 67 6e 4b 65 79 73 28 50 61 72 73 65 20 2a  eignKeys(Parse *
2ab33 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70  pParse, Table *p
2ab34 54 61 62 29 7b 0a 20 20 46 4b 65 79 20 2a 70 3b  Tab){.  FKey *p;
2ab35 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20  .  char *zWhere 
2ab36 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d 73 71 6c  = 0;.  for(p=sql
2ab37 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73  ite3FkReferences
2ab38 28 70 54 61 62 29 3b 20 70 3b 20 70 3d 70 2d 3e  (pTab); p; p=p->
2ab39 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20 20 7a 57  pNextTo){.    zW
2ab3a 68 65 72 65 20 3d 20 77 68 65 72 65 4f 72 4e 61  here = whereOrNa
2ab3b 6d 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a  me(pParse->db, z
2ab3c 57 68 65 72 65 2c 20 70 2d 3e 70 46 72 6f 6d 2d  Where, p->pFrom-
2ab3d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 72  >zName);.  }.  r
2ab3e 65 74 75 72 6e 20 7a 57 68 65 72 65 3b 0a 7d 0a  eturn zWhere;.}.
2ab3f 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65  #endif../*.** Ge
2ab40 6e 65 72 61 74 65 20 74 68 65 20 74 65 78 74 20  nerate the text 
2ab41 6f 66 20 61 20 57 48 45 52 45 20 65 78 70 72 65  of a WHERE expre
2ab42 73 73 69 6f 6e 20 77 68 69 63 68 20 63 61 6e 20  ssion which can 
2ab43 62 65 20 75 73 65 64 20 74 6f 20 73 65 6c 65 63  be used to selec
2ab44 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d 70 6f 72 61  t all.** tempora
2ab45 72 79 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74  ry triggers on t
2ab46 61 62 6c 65 20 70 54 61 62 20 66 72 6f 6d 20 74  able pTab from t
2ab47 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d  he sqlite_temp_m
2ab48 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49 66 0a  aster table. If.
2ab49 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20 68 61  ** table pTab ha
2ab4a 73 20 6e 6f 20 74 65 6d 70 6f 72 61 72 79 20 74  s no temporary t
2ab4b 72 69 67 67 65 72 73 2c 20 6f 72 20 69 73 20 69  riggers, or is i
2ab4c 74 73 65 6c 66 20 73 74 6f 72 65 64 20 69 6e 20  tself stored in 
2ab4d 74 68 65 20 0a 2a 2a 20 74 65 6d 70 6f 72 61 72  the .** temporar
2ab4e 79 20 64 61 74 61 62 61 73 65 2c 20 4e 55 4c 4c  y database, NULL
2ab4f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
2ab50 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 77 68  .static char *wh
2ab51 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73 28  ereTempTriggers(
2ab52 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
2ab53 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 54  able *pTab){.  T
2ab54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b 0a 20  rigger *pTrig;. 
2ab55 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20 3d 20   char *zWhere = 
2ab56 30 3b 0a 20 20 63 6f 6e 73 74 20 53 63 68 65 6d  0;.  const Schem
2ab57 61 20 2a 70 54 65 6d 70 53 63 68 65 6d 61 20 3d  a *pTempSchema =
2ab58 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62   pParse->db->aDb
2ab59 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 20 2f 2a 20  [1].pSchema; /* 
2ab5a 54 65 6d 70 20 64 62 20 73 63 68 65 6d 61 20 2a  Temp db schema *
2ab5b 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74  /..  /* If the t
2ab5c 61 62 6c 65 20 69 73 20 6e 6f 74 20 6c 6f 63 61  able is not loca
2ab5d 74 65 64 20 69 6e 20 74 68 65 20 74 65 6d 70 2d  ted in the temp-
2ab5e 64 62 20 28 69 6e 20 77 68 69 63 68 20 63 61 73  db (in which cas
2ab5f 65 20 4e 55 4c 4c 20 69 73 20 0a 20 20 2a 2a 20  e NULL is .  ** 
2ab60 72 65 74 75 72 6e 65 64 2c 20 6c 6f 6f 70 20 74  returned, loop t
2ab61 68 72 6f 75 67 68 20 74 68 65 20 74 61 62 6c 65  hrough the table
2ab62 73 20 6c 69 73 74 20 6f 66 20 74 72 69 67 67 65  s list of trigge
2ab63 72 73 2e 20 46 6f 72 20 65 61 63 68 20 74 72 69  rs. For each tri
2ab64 67 67 65 72 0a 20 20 2a 2a 20 74 68 61 74 20 69  gger.  ** that i
2ab65 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68  s not part of th
2ab66 65 20 74 65 6d 70 2d 64 62 20 73 63 68 65 6d 61  e temp-db schema
2ab67 2c 20 61 64 64 20 61 20 63 6c 61 75 73 65 20 74  , add a clause t
2ab68 6f 20 74 68 65 20 57 48 45 52 45 20 0a 20 20 2a  o the WHERE .  *
2ab69 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 62 65 69  * expression bei
2ab6a 6e 67 20 62 75 69 6c 74 20 75 70 20 69 6e 20 7a  ng built up in z
2ab6b 57 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  Where..  */.  if
2ab6c 28 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 21  ( pTab->pSchema!
2ab6d 3d 70 54 65 6d 70 53 63 68 65 6d 61 20 29 7b 0a  =pTempSchema ){.
2ab6e 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
2ab6f 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2ab70 20 20 66 6f 72 28 70 54 72 69 67 3d 73 71 6c 69    for(pTrig=sqli
2ab71 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 70  te3TriggerList(p
2ab72 50 61 72 73 65 2c 20 70 54 61 62 29 3b 20 70 54  Parse, pTab); pT
2ab73 72 69 67 3b 20 70 54 72 69 67 3d 70 54 72 69 67  rig; pTrig=pTrig
2ab74 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
2ab75 69 66 28 20 70 54 72 69 67 2d 3e 70 53 63 68 65  if( pTrig->pSche
2ab76 6d 61 3d 3d 70 54 65 6d 70 53 63 68 65 6d 61 20  ma==pTempSchema 
2ab77 29 7b 0a 20 20 20 20 20 20 20 20 7a 57 68 65 72  ){.        zWher
2ab78 65 20 3d 20 77 68 65 72 65 4f 72 4e 61 6d 65 28  e = whereOrName(
2ab79 64 62 2c 20 7a 57 68 65 72 65 2c 20 70 54 72 69  db, zWhere, pTri
2ab7a 67 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  g->zName);.     
2ab7b 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69   }.    }.  }.  i
2ab7c 66 28 20 7a 57 68 65 72 65 20 29 7b 0a 20 20 20  f( zWhere ){.   
2ab7d 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73 71   char *zNew = sq
2ab7e 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 70 50 61  lite3MPrintf(pPa
2ab7f 72 73 65 2d 3e 64 62 2c 20 22 74 79 70 65 3d 27  rse->db, "type='
2ab80 74 72 69 67 67 65 72 27 20 41 4e 44 20 28 25 73  trigger' AND (%s
2ab81 29 22 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 20  )", zWhere);.   
2ab82 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
2ab83 50 61 72 73 65 2d 3e 64 62 2c 20 7a 57 68 65 72  Parse->db, zWher
2ab84 65 29 3b 0a 20 20 20 20 7a 57 68 65 72 65 20 3d  e);.    zWhere =
2ab85 20 7a 4e 65 77 3b 0a 20 20 7d 0a 20 20 72 65 74   zNew;.  }.  ret
2ab86 75 72 6e 20 7a 57 68 65 72 65 3b 0a 7d 0a 0a 2f  urn zWhere;.}../
2ab87 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
2ab88 64 65 20 74 6f 20 64 72 6f 70 20 61 6e 64 20 72  de to drop and r
2ab89 65 6c 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e  eload the intern
2ab8a 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  al representatio
2ab8b 6e 20 6f 66 20 74 61 62 6c 65 0a 2a 2a 20 70 54  n of table.** pT
2ab8c 61 62 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ab from the data
2ab8d 62 61 73 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20  base, including 
2ab8e 74 72 69 67 67 65 72 73 20 61 6e 64 20 74 65 6d  triggers and tem
2ab8f 70 6f 72 61 72 79 20 74 72 69 67 67 65 72 73 2e  porary triggers.
2ab90 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4e 61  .** Argument zNa
2ab91 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  me is the name o
2ab92 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74  f the table in t
2ab93 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
2ab94 6d 61 20 61 74 0a 2a 2a 20 74 68 65 20 74 69 6d  ma at.** the tim
2ab95 65 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20  e the generated 
2ab96 63 6f 64 65 20 69 73 20 65 78 65 63 75 74 65 64  code is executed
2ab97 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 64 69  . This can be di
2ab98 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20  fferent from.** 
2ab99 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 69 66 20 74  pTab->zName if t
2ab9a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2ab9b 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 74 6f 20  being called to 
2ab9c 63 6f 64 65 20 70 61 72 74 20 6f 66 20 61 6e 20  code part of an 
2ab9d 0a 2a 2a 20 22 41 4c 54 45 52 20 54 41 42 4c 45  .** "ALTER TABLE
2ab9e 20 52 45 4e 41 4d 45 20 54 4f 22 20 73 74 61 74   RENAME TO" stat
2ab9f 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ement..*/.static
2aba0 20 76 6f 69 64 20 72 65 6c 6f 61 64 54 61 62 6c   void reloadTabl
2aba1 65 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70  eSchema(Parse *p
2aba2 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54  Parse, Table *pT
2aba3 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ab, const char *
2aba4 7a 4e 61 6d 65 29 7b 0a 20 20 56 64 62 65 20 2a  zName){.  Vdbe *
2aba5 76 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72  v;.  char *zWher
2aba6 65 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  e;.  int iDb;   
2aba7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aba8 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61  /* Index of data
2aba9 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
2abaa 70 54 61 62 20 2a 2f 0a 23 69 66 6e 64 65 66 20  pTab */.#ifndef 
2abab 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
2abac 47 45 52 0a 20 20 54 72 69 67 67 65 72 20 2a 70  GER.  Trigger *p
2abad 54 72 69 67 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  Trig;.#endif..  
2abae 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
2abaf 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
2abb0 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20  ( NEVER(v==0) ) 
2abb1 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
2abb2 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
2abb3 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50  ldsAllMutexes(pP
2abb4 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20 69  arse->db) );.  i
2abb5 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
2abb6 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
2abb7 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
2abb8 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ema);.  assert( 
2abb9 69 44 62 3e 3d 30 20 29 3b 0a 0a 23 69 66 6e 64  iDb>=0 );..#ifnd
2abba 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
2abbb 52 49 47 47 45 52 0a 20 20 2f 2a 20 44 72 6f 70  RIGGER.  /* Drop
2abbc 20 61 6e 79 20 74 61 62 6c 65 20 74 72 69 67 67   any table trigg
2abbd 65 72 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74  ers from the int
2abbe 65 72 6e 61 6c 20 73 63 68 65 6d 61 2e 20 2a 2f  ernal schema. */
2abbf 0a 20 20 66 6f 72 28 70 54 72 69 67 3d 73 71 6c  .  for(pTrig=sql
2abc0 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28  ite3TriggerList(
2abc1 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 20 70  pParse, pTab); p
2abc2 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54 72 69  Trig; pTrig=pTri
2abc3 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  g->pNext){.    i
2abc4 6e 74 20 69 54 72 69 67 44 62 20 3d 20 73 71 6c  nt iTrigDb = sql
2abc5 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
2abc6 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
2abc7 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  rig->pSchema);. 
2abc8 20 20 20 61 73 73 65 72 74 28 20 69 54 72 69 67     assert( iTrig
2abc9 44 62 3d 3d 69 44 62 20 7c 7c 20 69 54 72 69 67  Db==iDb || iTrig
2abca 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 73 71 6c  Db==1 );.    sql
2abcb 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2abcc 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72  , OP_DropTrigger
2abcd 2c 20 69 54 72 69 67 44 62 2c 20 30 2c 20 30 2c  , iTrigDb, 0, 0,
2abce 20 70 54 72 69 67 2d 3e 7a 4e 61 6d 65 2c 20 30   pTrig->zName, 0
2abcf 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
2abd0 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 61 62   /* Drop the tab
2abd1 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 66 72 6f  le and index fro
2abd2 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73  m the internal s
2abd3 63 68 65 6d 61 2e 20 20 2a 2f 0a 20 20 73 71 6c  chema.  */.  sql
2abd4 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2abd5 2c 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20  , OP_DropTable, 
2abd6 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d  iDb, 0, 0, pTab-
2abd7 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 0a 20 20 2f  >zName, 0);..  /
2abd8 2a 20 52 65 6c 6f 61 64 20 74 68 65 20 74 61 62  * Reload the tab
2abd9 6c 65 2c 20 69 6e 64 65 78 20 61 6e 64 20 70 65  le, index and pe
2abda 72 6d 61 6e 65 6e 74 20 74 72 69 67 67 65 72 20  rmanent trigger 
2abdb 73 63 68 65 6d 61 73 2e 20 2a 2f 0a 20 20 7a 57  schemas. */.  zW
2abdc 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  here = sqlite3MP
2abdd 72 69 6e 74 66 28 70 50 61 72 73 65 2d 3e 64 62  rintf(pParse->db
2abde 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d 25 51 22 2c  , "tbl_name=%Q",
2abdf 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21   zName);.  if( !
2abe0 7a 57 68 65 72 65 20 29 20 72 65 74 75 72 6e 3b  zWhere ) return;
2abe1 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2abe2 64 50 61 72 73 65 53 63 68 65 6d 61 4f 70 28 76  dParseSchemaOp(v
2abe3 2c 20 69 44 62 2c 20 7a 57 68 65 72 65 29 3b 0a  , iDb, zWhere);.
2abe4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2abe5 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f  OMIT_TRIGGER.  /
2abe6 2a 20 4e 6f 77 2c 20 69 66 20 74 68 65 20 74 61  * Now, if the ta
2abe7 62 6c 65 20 69 73 20 6e 6f 74 20 73 74 6f 72 65  ble is not store
2abe8 64 20 69 6e 20 74 68 65 20 74 65 6d 70 20 64 61  d in the temp da
2abe9 74 61 62 61 73 65 2c 20 72 65 6c 6f 61 64 20 61  tabase, reload a
2abea 6e 79 20 74 65 6d 70 20 0a 20 20 2a 2a 20 74 72  ny temp .  ** tr
2abeb 69 67 67 65 72 73 2e 20 44 6f 6e 27 74 20 75 73  iggers. Don't us
2abec 65 20 49 4e 28 2e 2e 2e 29 20 69 6e 20 63 61 73  e IN(...) in cas
2abed 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  e SQLITE_OMIT_SU
2abee 42 51 55 45 52 59 20 69 73 20 64 65 66 69 6e 65  BQUERY is define
2abef 64 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  d. .  */.  if( (
2abf0 7a 57 68 65 72 65 3d 77 68 65 72 65 54 65 6d 70  zWhere=whereTemp
2abf1 54 72 69 67 67 65 72 73 28 70 50 61 72 73 65 2c  Triggers(pParse,
2abf2 20 70 54 61 62 29 29 21 3d 30 20 29 7b 0a 20 20   pTab))!=0 ){.  
2abf3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2abf4 50 61 72 73 65 53 63 68 65 6d 61 4f 70 28 76 2c  ParseSchemaOp(v,
2abf5 20 31 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 7d   1, zWhere);.  }
2abf6 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
2abf7 20 50 61 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65   Parameter zName
2abf8 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
2abf9 61 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 20  a table that is 
2abfa 61 62 6f 75 74 20 74 6f 20 62 65 20 61 6c 74 65  about to be alte
2abfb 72 65 64 0a 2a 2a 20 28 65 69 74 68 65 72 20 77  red.** (either w
2abfc 69 74 68 20 41 4c 54 45 52 20 54 41 42 4c 45 20  ith ALTER TABLE 
2abfd 2e 2e 2e 20 52 45 4e 41 4d 45 20 54 4f 20 6f 72  ... RENAME TO or
2abfe 20 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e   ALTER TABLE ...
2abff 20 41 44 44 20 43 4f 4c 55 4d 4e 29 2e 0a 2a 2a   ADD COLUMN)..**
2ac00 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 69 73   If the table is
2ac01 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 2c   a system table,
2ac02 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6c   this function l
2ac03 65 61 76 65 73 20 61 6e 20 65 72 72 6f 72 20 6d  eaves an error m
2ac04 65 73 73 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61  essage.** in pPa
2ac05 72 73 65 2d 3e 7a 45 72 72 20 28 73 79 73 74 65  rse->zErr (syste
2ac06 6d 20 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74  m tables may not
2ac07 20 62 65 20 61 6c 74 65 72 65 64 29 20 61 6e 64   be altered) and
2ac08 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
2ac09 6f 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 66 20  o..**.** Or, if 
2ac0a 7a 4e 61 6d 65 20 69 73 20 6e 6f 74 20 61 20 73  zName is not a s
2ac0b 79 73 74 65 6d 20 74 61 62 6c 65 2c 20 7a 65 72  ystem table, zer
2ac0c 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  o is returned..*
2ac0d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 53  /.static int isS
2ac0e 79 73 74 65 6d 54 61 62 6c 65 28 50 61 72 73 65  ystemTable(Parse
2ac0f 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20   *pParse, const 
2ac10 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
2ac11 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 6c 65  if( sqlite3Strle
2ac12 6e 33 30 28 7a 4e 61 6d 65 29 3e 36 20 26 26 20  n30(zName)>6 && 
2ac13 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43  0==sqlite3StrNIC
2ac14 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74  mp(zName, "sqlit
2ac15 65 5f 22 2c 20 37 29 20 29 7b 0a 20 20 20 20 73  e_", 7) ){.    s
2ac16 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2ac17 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73  Parse, "table %s
2ac18 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 74 65   may not be alte
2ac19 72 65 64 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  red", zName);.  
2ac1a 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
2ac1b 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
2ac1c 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
2ac1d 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  de to implement 
2ac1e 74 68 65 20 22 41 4c 54 45 52 20 54 41 42 4c 45  the "ALTER TABLE
2ac1f 20 78 78 78 20 52 45 4e 41 4d 45 20 54 4f 20 79   xxx RENAME TO y
2ac20 79 79 22 20 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2e  yy" .** command.
2ac21 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
2ac22 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2ac23 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65  AlterRenameTable
2ac24 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2ac25 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
2ac26 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e   Parser context.
2ac27 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
2ac28 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  Src,            
2ac29 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20  /* The table to 
2ac2a 72 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20 54 6f 6b  rename. */.  Tok
2ac2b 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20  en *pName       
2ac2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
2ac2d 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f  w table name. */
2ac2e 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  .){.  int iDb;  
2ac2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ac30 2f 2a 20 44 61 74 61 62 61 73 65 20 74 68 61 74  /* Database that
2ac31 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 61   contains the ta
2ac32 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ble */.  char *z
2ac33 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
2ac34 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61     /* Name of da
2ac35 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 20  tabase iDb */.  
2ac36 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
2ac37 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62            /* Tab
2ac38 6c 65 20 62 65 69 6e 67 20 72 65 6e 61 6d 65 64  le being renamed
2ac39 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d   */.  char *zNam
2ac3a 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
2ac3b 2f 2a 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74  /* NULL-terminat
2ac3c 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 4e  ed version of pN
2ac3d 61 6d 65 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65  ame */ .  sqlite
2ac3e 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2ac3f 64 62 3b 20 2f 2a 20 44 61 74 61 62 61 73 65 20  db; /* Database 
2ac40 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
2ac41 69 6e 74 20 6e 54 61 62 4e 61 6d 65 3b 20 20 20  int nTabName;   
2ac42 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2ac43 62 65 72 20 6f 66 20 55 54 46 2d 38 20 63 68 61  ber of UTF-8 cha
2ac44 72 61 63 74 65 72 73 20 69 6e 20 7a 54 61 62 4e  racters in zTabN
2ac45 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ame */.  const c
2ac46 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 3b 20 20  har *zTabName;  
2ac47 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e     /* Original n
2ac48 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
2ac49 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 0a 23   */.  Vdbe *v;.#
2ac4a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2ac4b 49 54 5f 54 52 49 47 47 45 52 0a 20 20 63 68 61  IT_TRIGGER.  cha
2ac4c 72 20 2a 7a 57 68 65 72 65 20 3d 20 30 3b 20 20  r *zWhere = 0;  
2ac4d 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20         /* Where 
2ac4e 63 6c 61 75 73 65 20 74 6f 20 6c 6f 63 61 74 65  clause to locate
2ac4f 20 74 65 6d 70 20 74 72 69 67 67 65 72 73 20 2a   temp triggers *
2ac50 2f 0a 23 65 6e 64 69 66 0a 20 20 56 54 61 62 6c  /.#endif.  VTabl
2ac51 65 20 2a 70 56 54 61 62 20 3d 20 30 3b 20 20 20  e *pVTab = 0;   
2ac52 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 7a 65 72 6f       /* Non-zero
2ac53 20 69 66 20 74 68 69 73 20 69 73 20 61 20 76 2d   if this is a v-
2ac54 74 61 62 20 77 69 74 68 20 61 6e 20 78 52 65 6e  tab with an xRen
2ac55 61 6d 65 28 29 20 2a 2f 0a 20 20 69 6e 74 20 73  ame() */.  int s
2ac56 61 76 65 64 44 62 46 6c 61 67 73 3b 20 20 20 20  avedDbFlags;    
2ac57 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61       /* Saved va
2ac58 6c 75 65 20 6f 66 20 64 62 2d 3e 66 6c 61 67 73  lue of db->flags
2ac59 20 2a 2f 0a 0a 20 20 73 61 76 65 64 44 62 46 6c   */..  savedDbFl
2ac5a 61 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67 73 3b  ags = db->flags;
2ac5b 20 20 0a 20 20 69 66 28 20 4e 45 56 45 52 28 64    .  if( NEVER(d
2ac5c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29  b->mallocFailed)
2ac5d 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e   ) goto exit_ren
2ac5e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 61 73 73  ame_table;.  ass
2ac5f 65 72 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3d  ert( pSrc->nSrc=
2ac60 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =1 );.  assert( 
2ac61 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
2ac62 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72  sAllMutexes(pPar
2ac63 73 65 2d 3e 64 62 29 20 29 3b 0a 0a 20 20 70 54  se->db) );..  pT
2ac64 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  ab = sqlite3Loca
2ac65 74 65 54 61 62 6c 65 49 74 65 6d 28 70 50 61 72  teTableItem(pPar
2ac66 73 65 2c 20 30 2c 20 26 70 53 72 63 2d 3e 61 5b  se, 0, &pSrc->a[
2ac67 30 5d 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62  0]);.  if( !pTab
2ac68 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e   ) goto exit_ren
2ac69 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 69 44 62  ame_table;.  iDb
2ac6a 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
2ac6b 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
2ac6c 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
2ac6d 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e  a);.  zDb = db->
2ac6e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
2ac6f 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
2ac70 51 4c 49 54 45 5f 50 72 65 66 65 72 42 75 69 6c  QLITE_PreferBuil
2ac71 74 69 6e 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 61  tin;..  /* Get a
2ac72 20 4e 55 4c 4c 20 74 65 72 6d 69 6e 61 74 65 64   NULL terminated
2ac73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
2ac74 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  new table name. 
2ac75 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c  */.  zName = sql
2ac76 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
2ac77 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20  n(db, pName);.  
2ac78 69 66 28 20 21 7a 4e 61 6d 65 20 29 20 67 6f 74  if( !zName ) got
2ac79 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
2ac7a 62 6c 65 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  ble;..  /* Check
2ac7b 20 74 68 61 74 20 61 20 74 61 62 6c 65 20 6f 72   that a table or
2ac7c 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 27 7a 4e   index named 'zN
2ac7d 61 6d 65 27 20 64 6f 65 73 20 6e 6f 74 20 61 6c  ame' does not al
2ac7e 72 65 61 64 79 20 65 78 69 73 74 0a 20 20 2a 2a  ready exist.  **
2ac7f 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62   in database iDb
2ac80 2e 20 49 66 20 73 6f 2c 20 74 68 69 73 20 69 73  . If so, this is
2ac81 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a   an error..  */.
2ac82 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e    if( sqlite3Fin
2ac83 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65  dTable(db, zName
2ac84 2c 20 7a 44 62 29 20 7c 7c 20 73 71 6c 69 74 65  , zDb) || sqlite
2ac85 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a  3FindIndex(db, z
2ac86 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20  Name, zDb) ){.  
2ac87 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2ac88 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  g(pParse, .     
2ac89 20 20 20 22 74 68 65 72 65 20 69 73 20 61 6c 72     "there is alr
2ac8a 65 61 64 79 20 61 6e 6f 74 68 65 72 20 74 61 62  eady another tab
2ac8b 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 69 74 68  le or index with
2ac8c 20 74 68 69 73 20 6e 61 6d 65 3a 20 25 73 22 2c   this name: %s",
2ac8d 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74   zName);.    got
2ac8e 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
2ac8f 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  ble;.  }..  /* M
2ac90 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 6e  ake sure it is n
2ac91 6f 74 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c  ot a system tabl
2ac92 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2c  e being altered,
2ac93 20 6f 72 20 61 20 72 65 73 65 72 76 65 64 20 6e   or a reserved n
2ac94 61 6d 65 0a 20 20 2a 2a 20 74 68 61 74 20 74 68  ame.  ** that th
2ac95 65 20 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67  e table is being
2ac96 20 72 65 6e 61 6d 65 64 20 74 6f 2e 0a 20 20 2a   renamed to..  *
2ac97 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  /.  if( SQLITE_O
2ac98 4b 21 3d 69 73 53 79 73 74 65 6d 54 61 62 6c 65  K!=isSystemTable
2ac99 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 7a  (pParse, pTab->z
2ac9a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74  Name) ){.    got
2ac9b 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
2ac9c 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53  ble;.  }.  if( S
2ac9d 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
2ac9e 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65  3CheckObjectName
2ac9f 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20  (pParse, zName) 
2aca0 29 7b 20 67 6f 74 6f 0a 20 20 20 20 65 78 69 74  ){ goto.    exit
2aca1 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20  _rename_table;. 
2aca2 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
2aca3 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69  TE_OMIT_VIEW.  i
2aca4 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  f( pTab->pSelect
2aca5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2aca6 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2aca7 22 76 69 65 77 20 25 73 20 6d 61 79 20 6e 6f 74  "view %s may not
2aca8 20 62 65 20 61 6c 74 65 72 65 64 22 2c 20 70 54   be altered", pT
2aca9 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
2acaa 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65  goto exit_rename
2acab 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64  _table;.  }.#end
2acac 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
2acad 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
2acae 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b  ATION.  /* Invok
2acaf 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74  e the authorizat
2acb0 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f  ion callback. */
2acb1 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75  .  if( sqlite3Au
2acb2 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
2acb3 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42  SQLITE_ALTER_TAB
2acb4 4c 45 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a  LE, zDb, pTab->z
2acb5 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20  Name, 0) ){.    
2acb6 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65  goto exit_rename
2acb7 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64  _table;.  }.#end
2acb8 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
2acb9 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2acba 41 42 4c 45 0a 20 20 69 66 28 20 73 71 6c 69 74  ABLE.  if( sqlit
2acbb 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
2acbc 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ames(pParse, pTa
2acbd 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  b) ){.    goto e
2acbe 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
2acbf 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 56 69  ;.  }.  if( IsVi
2acc0 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
2acc1 20 20 20 70 56 54 61 62 20 3d 20 73 71 6c 69 74     pVTab = sqlit
2acc2 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20  e3GetVTable(db, 
2acc3 70 54 61 62 29 3b 0a 20 20 20 20 69 66 28 20 70  pTab);.    if( p
2acc4 56 54 61 62 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f  VTab->pVtab->pMo
2acc5 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 3d 3d 30  dule->xRename==0
2acc6 20 29 7b 0a 20 20 20 20 20 20 70 56 54 61 62 20   ){.      pVTab 
2acc7 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  = 0;.    }.  }.#
2acc8 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69  endif..  /* Begi
2acc9 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  n a transaction 
2acca 61 6e 64 20 63 6f 64 65 20 74 68 65 20 56 65 72  and code the Ver
2accb 69 66 79 43 6f 6f 6b 69 65 20 66 6f 72 20 64 61  ifyCookie for da
2accc 74 61 62 61 73 65 20 69 44 62 2e 20 0a 20 20 2a  tabase iDb. .  *
2accd 2a 20 54 68 65 6e 20 6d 6f 64 69 66 79 20 74 68  * Then modify th
2acce 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20  e schema cookie 
2accf 28 73 69 6e 63 65 20 74 68 65 20 41 4c 54 45 52  (since the ALTER
2acd0 20 54 41 42 4c 45 20 6d 6f 64 69 66 69 65 73 20   TABLE modifies 
2acd1 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 29  the.  ** schema)
2acd2 2e 20 4f 70 65 6e 20 61 20 73 74 61 74 65 6d 65  . Open a stateme
2acd3 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  nt transaction i
2acd4 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 61  f the table is a
2acd5 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20 74 61   virtual.  ** ta
2acd6 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20  ble..  */.  v = 
2acd7 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2acd8 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d  Parse);.  if( v=
2acd9 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  =0 ){.    goto e
2acda 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
2acdb 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42  ;.  }.  sqlite3B
2acdc 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
2acdd 6f 6e 28 70 50 61 72 73 65 2c 20 70 56 54 61 62  on(pParse, pVTab
2acde 21 3d 30 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c  !=0, iDb);.  sql
2acdf 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
2ace0 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a  (pParse, iDb);..
2ace1 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
2ace2 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2c  a virtual table,
2ace3 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 52 65 6e   invoke the xRen
2ace4 61 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ame() function i
2ace5 66 0a 20 20 2a 2a 20 6f 6e 65 20 69 73 20 64 65  f.  ** one is de
2ace6 66 69 6e 65 64 2e 20 54 68 65 20 78 52 65 6e 61  fined. The xRena
2ace7 6d 65 28 29 20 63 61 6c 6c 62 61 63 6b 20 77 69  me() callback wi
2ace8 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 20 6e 61  ll modify the na
2ace9 6d 65 73 0a 20 20 2a 2a 20 6f 66 20 61 6e 79 20  mes.  ** of any 
2acea 72 65 73 6f 75 72 63 65 73 20 75 73 65 64 20 62  resources used b
2aceb 79 20 74 68 65 20 76 2d 74 61 62 6c 65 20 69 6d  y the v-table im
2acec 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 69 6e  plementation (in
2aced 63 6c 75 64 69 6e 67 20 6f 74 68 65 72 0a 20 20  cluding other.  
2acee 2a 2a 20 53 51 4c 69 74 65 20 74 61 62 6c 65 73  ** SQLite tables
2acef 29 20 74 68 61 74 20 61 72 65 20 69 64 65 6e 74  ) that are ident
2acf0 69 66 69 65 64 20 62 79 20 74 68 65 20 6e 61 6d  ified by the nam
2acf1 65 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  e of the virtual
2acf2 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66   table..  */.#if
2acf3 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2acf4 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
2acf5 69 66 28 20 70 56 54 61 62 20 29 7b 0a 20 20 20  if( pVTab ){.   
2acf6 20 69 6e 74 20 69 20 3d 20 2b 2b 70 50 61 72 73   int i = ++pPars
2acf7 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
2acf8 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2acf9 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
2acfa 20 69 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20 30 29   i, 0, zName, 0)
2acfb 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2acfc 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 52  eAddOp4(v, OP_VR
2acfd 65 6e 61 6d 65 2c 20 69 2c 20 30 2c 20 30 2c 28  ename, i, 0, 0,(
2acfe 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 56 54 61  const char*)pVTa
2acff 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20  b, P4_VTAB);.   
2ad00 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74   sqlite3MayAbort
2ad01 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 23 65  (pParse);.  }.#e
2ad02 6e 64 69 66 0a 0a 20 20 2f 2a 20 66 69 67 75 72  ndif..  /* figur
2ad03 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 55  e out how many U
2ad04 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 73 20  TF-8 characters 
2ad05 61 72 65 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a  are in zName */.
2ad06 20 20 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61    zTabName = pTa
2ad07 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 6e 54 61 62  b->zName;.  nTab
2ad08 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 55 74  Name = sqlite3Ut
2ad09 66 38 43 68 61 72 4c 65 6e 28 7a 54 61 62 4e 61  f8CharLen(zTabNa
2ad0a 6d 65 2c 20 2d 31 29 3b 0a 0a 23 69 66 20 21 64  me, -1);..#if !d
2ad0b 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2ad0c 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20  IT_FOREIGN_KEY) 
2ad0d 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
2ad0e 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
2ad0f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73  .  if( db->flags
2ad10 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b  &SQLITE_ForeignK
2ad11 65 79 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  eys ){.    /* If
2ad12 20 66 6f 72 65 69 67 6e 2d 6b 65 79 20 73 75 70   foreign-key sup
2ad13 70 6f 72 74 20 69 73 20 65 6e 61 62 6c 65 64 2c  port is enabled,
2ad14 20 72 65 77 72 69 74 65 20 74 68 65 20 43 52 45   rewrite the CRE
2ad15 41 54 45 20 54 41 42 4c 45 20 0a 20 20 20 20 2a  ATE TABLE .    *
2ad16 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 63 6f 72  * statements cor
2ad17 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 61 6c  responding to al
2ad18 6c 20 63 68 69 6c 64 20 74 61 62 6c 65 73 20 6f  l child tables o
2ad19 66 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  f foreign key co
2ad1a 6e 73 74 72 61 69 6e 74 73 0a 20 20 20 20 2a 2a  nstraints.    **
2ad1b 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 72   for which the r
2ad1c 65 6e 61 6d 65 64 20 74 61 62 6c 65 20 69 73 20  enamed table is 
2ad1d 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65  the parent table
2ad1e 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 7a  .  */.    if( (z
2ad1f 57 68 65 72 65 3d 77 68 65 72 65 46 6f 72 65 69  Where=whereForei
2ad20 67 6e 4b 65 79 73 28 70 50 61 72 73 65 2c 20 70  gnKeys(pParse, p
2ad21 54 61 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  Tab))!=0 ){.    
2ad22 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
2ad23 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20  arse(pParse, .  
2ad24 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20          "UPDATE 
2ad25 5c 22 25 77 5c 22 2e 25 73 20 53 45 54 20 22 0a  \"%w\".%s SET ".
2ad26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 73                "s
2ad27 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61  ql = sqlite_rena
2ad28 6d 65 5f 70 61 72 65 6e 74 28 73 71 6c 2c 20 25  me_parent(sql, %
2ad29 51 2c 20 25 51 29 20 22 0a 20 20 20 20 20 20 20  Q, %Q) ".       
2ad2a 20 20 20 20 20 20 20 22 57 48 45 52 45 20 25 73         "WHERE %s
2ad2b 3b 22 2c 20 7a 44 62 2c 20 53 43 48 45 4d 41 5f  ;", zDb, SCHEMA_
2ad2c 54 41 42 4c 45 28 69 44 62 29 2c 20 7a 54 61 62  TABLE(iDb), zTab
2ad2d 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57 68  Name, zName, zWh
2ad2e 65 72 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ere);.      sqli
2ad2f 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 57  te3DbFree(db, zW
2ad30 68 65 72 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  here);.    }.  }
2ad31 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 6f  .#endif..  /* Mo
2ad32 64 69 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f  dify the sqlite_
2ad33 6d 61 73 74 65 72 20 74 61 62 6c 65 20 74 6f 20  master table to 
2ad34 75 73 65 20 74 68 65 20 6e 65 77 20 74 61 62 6c  use the new tabl
2ad35 65 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 73 71 6c  e name. */.  sql
2ad36 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
2ad37 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 22 55  pParse,.      "U
2ad38 50 44 41 54 45 20 25 51 2e 25 73 20 53 45 54 20  PDATE %Q.%s SET 
2ad39 22 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ".#ifdef SQLITE_
2ad3a 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20  OMIT_TRIGGER.   
2ad3b 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73 71         "sql = sq
2ad3c 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  lite_rename_tabl
2ad3d 65 28 73 71 6c 2c 20 25 51 29 2c 20 22 0a 23 65  e(sql, %Q), ".#e
2ad3e 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 22 73  lse.          "s
2ad3f 71 6c 20 3d 20 43 41 53 45 20 22 0a 20 20 20 20  ql = CASE ".    
2ad40 20 20 20 20 20 20 20 20 22 57 48 45 4e 20 74 79          "WHEN ty
2ad41 70 65 20 3d 20 27 74 72 69 67 67 65 72 27 20 54  pe = 'trigger' T
2ad42 48 45 4e 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d  HEN sqlite_renam
2ad43 65 5f 74 72 69 67 67 65 72 28 73 71 6c 2c 20 25  e_trigger(sql, %
2ad44 51 29 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  Q)".            
2ad45 22 45 4c 53 45 20 73 71 6c 69 74 65 5f 72 65 6e  "ELSE sqlite_ren
2ad46 61 6d 65 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25  ame_table(sql, %
2ad47 51 29 20 45 4e 44 2c 20 22 0a 23 65 6e 64 69 66  Q) END, ".#endif
2ad48 0a 20 20 20 20 20 20 20 20 20 20 22 74 62 6c 5f  .          "tbl_
2ad49 6e 61 6d 65 20 3d 20 25 51 2c 20 22 0a 20 20 20  name = %Q, ".   
2ad4a 20 20 20 20 20 20 20 22 6e 61 6d 65 20 3d 20 43         "name = C
2ad4b 41 53 45 20 22 0a 20 20 20 20 20 20 20 20 20 20  ASE ".          
2ad4c 20 20 22 57 48 45 4e 20 74 79 70 65 3d 27 74 61    "WHEN type='ta
2ad4d 62 6c 65 27 20 54 48 45 4e 20 25 51 20 22 0a 20  ble' THEN %Q ". 
2ad4e 20 20 20 20 20 20 20 20 20 20 20 22 57 48 45 4e             "WHEN
2ad4f 20 6e 61 6d 65 20 4c 49 4b 45 20 27 73 71 6c 69   name LIKE 'sqli
2ad50 74 65 5f 61 75 74 6f 69 6e 64 65 78 25 25 27 20  te_autoindex%%' 
2ad51 41 4e 44 20 74 79 70 65 3d 27 69 6e 64 65 78 27  AND type='index'
2ad52 20 54 48 45 4e 20 22 0a 20 20 20 20 20 20 20 20   THEN ".        
2ad53 20 20 20 20 20 22 27 73 71 6c 69 74 65 5f 61 75       "'sqlite_au
2ad54 74 6f 69 6e 64 65 78 5f 27 20 7c 7c 20 25 51 20  toindex_' || %Q 
2ad55 7c 7c 20 73 75 62 73 74 72 28 6e 61 6d 65 2c 25  || substr(name,%
2ad56 64 2b 31 38 29 20 22 0a 20 20 20 20 20 20 20 20  d+18) ".        
2ad57 20 20 20 20 22 45 4c 53 45 20 6e 61 6d 65 20 45      "ELSE name E
2ad58 4e 44 20 22 0a 20 20 20 20 20 20 22 57 48 45 52  ND ".      "WHER
2ad59 45 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 43 4f  E tbl_name=%Q CO
2ad5a 4c 4c 41 54 45 20 6e 6f 63 61 73 65 20 41 4e 44  LLATE nocase AND
2ad5b 20 22 0a 20 20 20 20 20 20 20 20 20 20 22 28 74   ".          "(t
2ad5c 79 70 65 3d 27 74 61 62 6c 65 27 20 4f 52 20 74  ype='table' OR t
2ad5d 79 70 65 3d 27 69 6e 64 65 78 27 20 4f 52 20 74  ype='index' OR t
2ad5e 79 70 65 3d 27 74 72 69 67 67 65 72 27 29 3b 22  ype='trigger');"
2ad5f 2c 20 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43  , .      zDb, SC
2ad60 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c  HEMA_TABLE(iDb),
2ad61 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a   zName, zName, z
2ad62 4e 61 6d 65 2c 20 0a 23 69 66 6e 64 65 66 20 53  Name, .#ifndef S
2ad63 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
2ad64 45 52 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 0a  ER.      zName,.
2ad65 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7a 4e 61  #endif.      zNa
2ad66 6d 65 2c 20 6e 54 61 62 4e 61 6d 65 2c 20 7a 54  me, nTabName, zT
2ad67 61 62 4e 61 6d 65 0a 20 20 29 3b 0a 0a 23 69 66  abName.  );..#if
2ad68 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2ad69 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20  _AUTOINCREMENT. 
2ad6a 20 2f 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74   /* If the sqlit
2ad6b 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65  e_sequence table
2ad6c 20 65 78 69 73 74 73 20 69 6e 20 74 68 69 73 20   exists in this 
2ad6d 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 75  database, then u
2ad6e 70 64 61 74 65 20 0a 20 20 2a 2a 20 69 74 20 77  pdate .  ** it w
2ad6f 69 74 68 20 74 68 65 20 6e 65 77 20 74 61 62 6c  ith the new tabl
2ad70 65 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69  e name..  */.  i
2ad71 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61  f( sqlite3FindTa
2ad72 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f  ble(db, "sqlite_
2ad73 73 65 71 75 65 6e 63 65 22 2c 20 7a 44 62 29 20  sequence", zDb) 
2ad74 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  ){.    sqlite3Ne
2ad75 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
2ad76 2c 0a 20 20 20 20 20 20 20 20 22 55 50 44 41 54  ,.        "UPDAT
2ad77 45 20 5c 22 25 77 5c 22 2e 73 71 6c 69 74 65 5f  E \"%w\".sqlite_
2ad78 73 65 71 75 65 6e 63 65 20 73 65 74 20 6e 61 6d  sequence set nam
2ad79 65 20 3d 20 25 51 20 57 48 45 52 45 20 6e 61 6d  e = %Q WHERE nam
2ad7a 65 20 3d 20 25 51 22 2c 0a 20 20 20 20 20 20 20  e = %Q",.       
2ad7b 20 7a 44 62 2c 20 7a 4e 61 6d 65 2c 20 70 54 61   zDb, zName, pTa
2ad7c 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23  b->zName);.  }.#
2ad7d 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
2ad7e 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
2ad7f 45 52 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ER.  /* If there
2ad80 20 61 72 65 20 54 45 4d 50 20 74 72 69 67 67 65   are TEMP trigge
2ad81 72 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65  rs on this table
2ad82 2c 20 6d 6f 64 69 66 79 20 74 68 65 20 73 71 6c  , modify the sql
2ad83 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 0a  ite_temp_master.
2ad84 20 20 2a 2a 20 74 61 62 6c 65 2e 20 44 6f 6e 27    ** table. Don'
2ad85 74 20 64 6f 20 74 68 69 73 20 69 66 20 74 68 65  t do this if the
2ad86 20 74 61 62 6c 65 20 62 65 69 6e 67 20 41 4c 54   table being ALT
2ad87 45 52 65 64 20 69 73 20 69 74 73 65 6c 66 20 6c  ERed is itself l
2ad88 6f 63 61 74 65 64 20 69 6e 0a 20 20 2a 2a 20 74  ocated in.  ** t
2ad89 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65  he temp database
2ad8a 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57  ..  */.  if( (zW
2ad8b 68 65 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72  here=whereTempTr
2ad8c 69 67 67 65 72 73 28 70 50 61 72 73 65 2c 20 70  iggers(pParse, p
2ad8d 54 61 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  Tab))!=0 ){.    
2ad8e 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
2ad8f 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  se(pParse, .    
2ad90 20 20 20 20 22 55 50 44 41 54 45 20 73 71 6c 69      "UPDATE sqli
2ad91 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 53  te_temp_master S
2ad92 45 54 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  ET ".           
2ad93 20 22 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72   "sql = sqlite_r
2ad94 65 6e 61 6d 65 5f 74 72 69 67 67 65 72 28 73 71  ename_trigger(sq
2ad95 6c 2c 20 25 51 29 2c 20 22 0a 20 20 20 20 20 20  l, %Q), ".      
2ad96 20 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20        "tbl_name 
2ad97 3d 20 25 51 20 22 0a 20 20 20 20 20 20 20 20 20  = %Q ".         
2ad98 20 20 20 22 57 48 45 52 45 20 25 73 3b 22 2c 20     "WHERE %s;", 
2ad99 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57  zName, zName, zW
2ad9a 68 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  here);.    sqlit
2ad9b 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 57 68  e3DbFree(db, zWh
2ad9c 65 72 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  ere);.  }.#endif
2ad9d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
2ad9e 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
2ad9f 47 4e 5f 4b 45 59 29 20 26 26 20 21 64 65 66 69  GN_KEY) && !defi
2ada0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2ada1 54 52 49 47 47 45 52 29 0a 20 20 69 66 28 20 64  TRIGGER).  if( d
2ada2 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f  b->flags&SQLITE_
2ada3 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b 0a 20  ForeignKeys ){. 
2ada4 20 20 20 46 4b 65 79 20 2a 70 3b 0a 20 20 20 20     FKey *p;.    
2ada5 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 46 6b 52  for(p=sqlite3FkR
2ada6 65 66 65 72 65 6e 63 65 73 28 70 54 61 62 29 3b  eferences(pTab);
2ada7 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 54 6f   p; p=p->pNextTo
2ada8 29 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a  ){.      Table *
2ada9 70 46 72 6f 6d 20 3d 20 70 2d 3e 70 46 72 6f 6d  pFrom = p->pFrom
2adaa 3b 0a 20 20 20 20 20 20 69 66 28 20 70 46 72 6f  ;.      if( pFro
2adab 6d 21 3d 70 54 61 62 20 29 7b 0a 20 20 20 20 20  m!=pTab ){.     
2adac 20 20 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63     reloadTableSc
2adad 68 65 6d 61 28 70 50 61 72 73 65 2c 20 70 2d 3e  hema(pParse, p->
2adae 70 46 72 6f 6d 2c 20 70 46 72 6f 6d 2d 3e 7a 4e  pFrom, pFrom->zN
2adaf 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ame);.      }.  
2adb0 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a    }.  }.#endif..
2adb1 20 20 2f 2a 20 44 72 6f 70 20 61 6e 64 20 72 65    /* Drop and re
2adb2 6c 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e 61  load the interna
2adb3 6c 20 74 61 62 6c 65 20 73 63 68 65 6d 61 2e 20  l table schema. 
2adb4 2a 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c 65  */.  reloadTable
2adb5 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 70  Schema(pParse, p
2adb6 54 61 62 2c 20 7a 4e 61 6d 65 29 3b 0a 0a 65 78  Tab, zName);..ex
2adb7 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3a  it_rename_table:
2adb8 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
2adb9 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63  tDelete(db, pSrc
2adba 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
2adbb 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20  ee(db, zName);. 
2adbc 20 64 62 2d 3e 66 6c 61 67 73 20 3d 20 73 61 76   db->flags = sav
2adbd 65 64 44 62 46 6c 61 67 73 3b 0a 7d 0a 0a 0a 2f  edDbFlags;.}.../
2adbe 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
2adbf 64 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  de to make sure 
2adc0 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  the file format 
2adc1 6e 75 6d 62 65 72 20 69 73 20 61 74 20 6c 65 61  number is at lea
2adc2 73 74 20 6d 69 6e 46 6f 72 6d 61 74 2e 0a 2a 2a  st minFormat..**
2adc3 20 54 68 65 20 67 65 6e 65 72 61 74 65 64 20 63   The generated c
2adc4 6f 64 65 20 77 69 6c 6c 20 69 6e 63 72 65 61 73  ode will increas
2adc5 65 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61  e the file forma
2adc6 74 20 6e 75 6d 62 65 72 20 69 66 20 6e 65 63 65  t number if nece
2adc7 73 73 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ssary..*/.SQLITE
2adc8 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2adc9 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65  lite3MinimumFile
2adca 46 6f 72 6d 61 74 28 50 61 72 73 65 20 2a 70 50  Format(Parse *pP
2adcb 61 72 73 65 2c 20 69 6e 74 20 69 44 62 2c 20 69  arse, int iDb, i
2adcc 6e 74 20 6d 69 6e 46 6f 72 6d 61 74 29 7b 0a 20  nt minFormat){. 
2adcd 20 56 64 62 65 20 2a 76 3b 0a 20 20 76 20 3d 20   Vdbe *v;.  v = 
2adce 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2adcf 50 61 72 73 65 29 3b 0a 20 20 2f 2a 20 54 68 65  Parse);.  /* The
2add0 20 56 44 42 45 20 73 68 6f 75 6c 64 20 68 61 76   VDBE should hav
2add1 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  e been allocated
2add2 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75   before this rou
2add3 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tine is called..
2add4 20 20 2a 2a 20 49 66 20 74 68 61 74 20 61 6c 6c    ** If that all
2add5 6f 63 61 74 69 6f 6e 20 66 61 69 6c 65 64 2c 20  ocation failed, 
2add6 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 71 75  we would have qu
2add7 69 74 20 62 65 66 6f 72 65 20 72 65 61 63 68 69  it before reachi
2add8 6e 67 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f 69  ng this.  ** poi
2add9 6e 74 20 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41  nt */.  if( ALWA
2adda 59 53 28 76 29 20 29 7b 0a 20 20 20 20 69 6e 74  YS(v) ){.    int
2addb 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
2addc 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
2addd 0a 20 20 20 20 69 6e 74 20 72 32 20 3d 20 73 71  .    int r2 = sq
2adde 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
2addf 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 6e 74  pParse);.    int
2ade0 20 6a 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   j1;.    sqlite3
2ade1 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2ade2 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62  _ReadCookie, iDb
2ade3 2c 20 72 31 2c 20 42 54 52 45 45 5f 46 49 4c 45  , r1, BTREE_FILE
2ade4 5f 46 4f 52 4d 41 54 29 3b 0a 20 20 20 20 73 71  _FORMAT);.    sq
2ade5 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72  lite3VdbeUsesBtr
2ade6 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20  ee(v, iDb);.    
2ade7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2ade8 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2ade9 20 6d 69 6e 46 6f 72 6d 61 74 2c 20 72 32 29 3b   minFormat, r2);
2adea 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65  .    j1 = sqlite
2adeb 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2adec 50 5f 47 65 2c 20 72 32 2c 20 30 2c 20 72 31 29  P_Ge, r2, 0, r1)
2aded 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2adee 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65  eAddOp3(v, OP_Se
2adef 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 42 54  tCookie, iDb, BT
2adf0 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2c  REE_FILE_FORMAT,
2adf1 20 72 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   r2);.    sqlite
2adf2 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2adf3 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   j1);.    sqlite
2adf4 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
2adf5 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20  pParse, r1);.   
2adf6 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2adf7 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
2adf8 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  2);.  }.}../*.**
2adf9 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
2adfa 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61  s called after a
2adfb 6e 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 2e  n "ALTER TABLE .
2adfc 2e 2e 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e  .. ADD" statemen
2adfd 74 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 70 61  t.** has been pa
2adfe 72 73 65 64 2e 20 41 72 67 75 6d 65 6e 74 20 70  rsed. Argument p
2adff 43 6f 6c 44 65 66 20 63 6f 6e 74 61 69 6e 73 20  ColDef contains 
2ae00 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20  the text of the 
2ae01 6e 65 77 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64 65  new.** column de
2ae02 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  finition..**.** 
2ae03 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  The Table struct
2ae04 75 72 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  ure pParse->pNew
2ae05 54 61 62 6c 65 20 77 61 73 20 65 78 74 65 6e 64  Table was extend
2ae06 65 64 20 74 6f 20 69 6e 63 6c 75 64 65 0a 2a 2a  ed to include.**
2ae07 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20   the new column 
2ae08 64 75 72 69 6e 67 20 70 61 72 73 69 6e 67 2e 0a  during parsing..
2ae09 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2ae0a 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c  E void sqlite3Al
2ae0b 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75  terFinishAddColu
2ae0c 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  mn(Parse *pParse
2ae0d 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 44 65 66  , Token *pColDef
2ae0e 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77  ){.  Table *pNew
2ae0f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2ae10 2a 20 43 6f 70 79 20 6f 66 20 70 50 61 72 73 65  * Copy of pParse
2ae11 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 20  ->pNewTable */. 
2ae12 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
2ae13 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
2ae14 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65  ble being altere
2ae15 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  d */.  int iDb; 
2ae16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae17 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d   /* Database num
2ae18 62 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ber */.  const c
2ae19 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
2ae1a 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e     /* Database n
2ae1b 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ame */.  const c
2ae1c 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 20  har *zTab;      
2ae1d 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65     /* Table name
2ae1e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c   */.  char *zCol
2ae1f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2ae20 2f 2a 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74  /* Null-terminat
2ae21 65 64 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69  ed column defini
2ae22 74 69 6f 6e 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e  tion */.  Column
2ae23 20 2a 70 43 6f 6c 3b 20 20 20 20 20 20 20 20 20   *pCol;         
2ae24 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 63      /* The new c
2ae25 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72 20  olumn */.  Expr 
2ae26 2a 70 44 66 6c 74 3b 20 20 20 20 20 20 20 20 20  *pDflt;         
2ae27 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20       /* Default 
2ae28 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65  value for the ne
2ae29 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 73 71  w column */.  sq
2ae2a 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
2ae2b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
2ae2c 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2ae2d 6f 6e 3b 20 2a 2f 0a 0a 20 20 64 62 20 3d 20 70  on; */..  db = p
2ae2e 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
2ae2f 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
2ae30 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2ae31 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4e  d ) return;.  pN
2ae32 65 77 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  ew = pParse->pNe
2ae33 77 54 61 62 6c 65 3b 0a 20 20 61 73 73 65 72 74  wTable;.  assert
2ae34 28 20 70 4e 65 77 20 29 3b 0a 0a 20 20 61 73 73  ( pNew );..  ass
2ae35 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
2ae36 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73  eHoldsAllMutexes
2ae37 28 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20  (db) );.  iDb = 
2ae38 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
2ae39 6e 64 65 78 28 64 62 2c 20 70 4e 65 77 2d 3e 70  ndex(db, pNew->p
2ae3a 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d  Schema);.  zDb =
2ae3b 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
2ae3c 61 6d 65 3b 0a 20 20 7a 54 61 62 20 3d 20 26 70  ame;.  zTab = &p
2ae3d 4e 65 77 2d 3e 7a 4e 61 6d 65 5b 31 36 5d 3b 20  New->zName[16]; 
2ae3e 20 2f 2a 20 53 6b 69 70 20 74 68 65 20 22 73 71   /* Skip the "sq
2ae3f 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 22 20  lite_altertab_" 
2ae40 70 72 65 66 69 78 20 6f 6e 20 74 68 65 20 6e 61  prefix on the na
2ae41 6d 65 20 2a 2f 0a 20 20 70 43 6f 6c 20 3d 20 26  me */.  pCol = &
2ae42 70 4e 65 77 2d 3e 61 43 6f 6c 5b 70 4e 65 77 2d  pNew->aCol[pNew-
2ae43 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20 70 44 66 6c  >nCol-1];.  pDfl
2ae44 74 20 3d 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 3b  t = pCol->pDflt;
2ae45 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
2ae46 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
2ae47 54 61 62 2c 20 7a 44 62 29 3b 0a 20 20 61 73 73  Tab, zDb);.  ass
2ae48 65 72 74 28 20 70 54 61 62 20 29 3b 0a 0a 23 69  ert( pTab );..#i
2ae49 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2ae4a 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
2ae4b 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
2ae4c 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61  authorization ca
2ae4d 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20 69 66 28  llback. */.  if(
2ae4e 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
2ae4f 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
2ae50 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c 20 7a 44  _ALTER_TABLE, zD
2ae51 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  b, pTab->zName, 
2ae52 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  0) ){.    return
2ae53 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
2ae54 2f 2a 20 49 66 20 74 68 65 20 64 65 66 61 75 6c  /* If the defaul
2ae55 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
2ae56 6e 65 77 20 63 6f 6c 75 6d 6e 20 77 61 73 20 73  new column was s
2ae57 70 65 63 69 66 69 65 64 20 77 69 74 68 20 61 20  pecified with a 
2ae58 0a 20 20 2a 2a 20 6c 69 74 65 72 61 6c 20 4e 55  .  ** literal NU
2ae59 4c 4c 2c 20 74 68 65 6e 20 73 65 74 20 70 44 66  LL, then set pDf
2ae5a 6c 74 20 74 6f 20 30 2e 20 54 68 69 73 20 73 69  lt to 0. This si
2ae5b 6d 70 6c 69 66 69 65 73 20 63 68 65 63 6b 69 6e  mplifies checkin
2ae5c 67 0a 20 20 2a 2a 20 66 6f 72 20 61 6e 20 53 51  g.  ** for an SQ
2ae5d 4c 20 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20 62  L NULL default b
2ae5e 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  elow..  */.  if(
2ae5f 20 70 44 66 6c 74 20 26 26 20 70 44 66 6c 74 2d   pDflt && pDflt-
2ae60 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b 0a  >op==TK_NULL ){.
2ae61 20 20 20 20 70 44 66 6c 74 20 3d 20 30 3b 0a 20      pDflt = 0;. 
2ae62 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74   }..  /* Check t
2ae63 68 61 74 20 74 68 65 20 6e 65 77 20 63 6f 6c 75  hat the new colu
2ae64 6d 6e 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66  mn is not specif
2ae65 69 65 64 20 61 73 20 50 52 49 4d 41 52 59 20 4b  ied as PRIMARY K
2ae66 45 59 20 6f 72 20 55 4e 49 51 55 45 2e 0a 20 20  EY or UNIQUE..  
2ae67 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  ** If there is a
2ae68 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72   NOT NULL constr
2ae69 61 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 64  aint, then the d
2ae6a 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
2ae6b 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e   the.  ** column
2ae6c 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 4e 55 4c   must not be NUL
2ae6d 4c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43  L..  */.  if( pC
2ae6e 6f 6c 2d 3e 63 6f 6c 46 6c 61 67 73 20 26 20 43  ol->colFlags & C
2ae6f 4f 4c 46 4c 41 47 5f 50 52 49 4d 4b 45 59 20 29  OLFLAG_PRIMKEY )
2ae70 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
2ae71 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43  orMsg(pParse, "C
2ae72 61 6e 6e 6f 74 20 61 64 64 20 61 20 50 52 49 4d  annot add a PRIM
2ae73 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 22 29  ARY KEY column")
2ae74 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
2ae75 7d 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49  }.  if( pNew->pI
2ae76 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71 6c 69  ndex ){.    sqli
2ae77 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2ae78 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20  se, "Cannot add 
2ae79 61 20 55 4e 49 51 55 45 20 63 6f 6c 75 6d 6e 22  a UNIQUE column"
2ae7a 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
2ae7b 20 7d 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c   }.  if( (db->fl
2ae7c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69  ags&SQLITE_Forei
2ae7d 67 6e 4b 65 79 73 29 20 26 26 20 70 4e 65 77 2d  gnKeys) && pNew-
2ae7e 3e 70 46 4b 65 79 20 26 26 20 70 44 66 6c 74 20  >pFKey && pDflt 
2ae7f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2ae80 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
2ae81 20 20 20 20 20 20 20 20 22 43 61 6e 6e 6f 74 20          "Cannot 
2ae82 61 64 64 20 61 20 52 45 46 45 52 45 4e 43 45 53  add a REFERENCES
2ae83 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e   column with non
2ae84 2d 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20 76 61  -NULL default va
2ae85 6c 75 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72  lue");.    retur
2ae86 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f  n;.  }.  if( pCo
2ae87 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70  l->notNull && !p
2ae88 44 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  Dflt ){.    sqli
2ae89 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2ae8a 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 43 61  se, .        "Ca
2ae8b 6e 6e 6f 74 20 61 64 64 20 61 20 4e 4f 54 20 4e  nnot add a NOT N
2ae8c 55 4c 4c 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20  ULL column with 
2ae8d 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 4e 55  default value NU
2ae8e 4c 4c 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  LL");.    return
2ae8f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75  ;.  }..  /* Ensu
2ae90 72 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 65  re the default e
2ae91 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 6f 6d  xpression is som
2ae92 65 74 68 69 6e 67 20 74 68 61 74 20 73 71 6c 69  ething that sqli
2ae93 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72  te3ValueFromExpr
2ae94 28 29 0a 20 20 2a 2a 20 63 61 6e 20 68 61 6e 64  ().  ** can hand
2ae95 6c 65 20 28 69 2e 65 2e 20 6e 6f 74 20 43 55 52  le (i.e. not CUR
2ae96 52 45 4e 54 5f 54 49 4d 45 20 65 74 63 2e 29 0a  RENT_TIME etc.).
2ae97 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c 74    */.  if( pDflt
2ae98 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2ae99 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 20  value *pVal;.   
2ae9a 20 69 66 28 20 73 71 6c 69 74 65 33 56 61 6c 75   if( sqlite3Valu
2ae9b 65 46 72 6f 6d 45 78 70 72 28 64 62 2c 20 70 44  eFromExpr(db, pD
2ae9c 66 6c 74 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  flt, SQLITE_UTF8
2ae9d 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  , SQLITE_AFF_NON
2ae9e 45 2c 20 26 70 56 61 6c 29 20 29 7b 0a 20 20 20  E, &pVal) ){.   
2ae9f 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
2aea0 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  led = 1;.      r
2aea1 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
2aea2 20 69 66 28 20 21 70 56 61 6c 20 29 7b 0a 20 20   if( !pVal ){.  
2aea3 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2aea4 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e  Msg(pParse, "Can
2aea5 6e 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e  not add a column
2aea6 20 77 69 74 68 20 6e 6f 6e 2d 63 6f 6e 73 74 61   with non-consta
2aea7 6e 74 20 64 65 66 61 75 6c 74 22 29 3b 0a 20 20  nt default");.  
2aea8 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
2aea9 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c  }.    sqlite3Val
2aeaa 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20  ueFree(pVal);.  
2aeab 7d 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74  }..  /* Modify t
2aeac 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  he CREATE TABLE 
2aead 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20  statement. */.  
2aeae 7a 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44 62  zCol = sqlite3Db
2aeaf 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61  StrNDup(db, (cha
2aeb0 72 2a 29 70 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70  r*)pColDef->z, p
2aeb1 43 6f 6c 44 65 66 2d 3e 6e 29 3b 0a 20 20 69 66  ColDef->n);.  if
2aeb2 28 20 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 63 68  ( zCol ){.    ch
2aeb3 61 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c  ar *zEnd = &zCol
2aeb4 5b 70 43 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a  [pColDef->n-1];.
2aeb5 20 20 20 20 69 6e 74 20 73 61 76 65 64 44 62 46      int savedDbF
2aeb6 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67 73  lags = db->flags
2aeb7 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 45 6e  ;.    while( zEn
2aeb8 64 3e 7a 43 6f 6c 20 26 26 20 28 2a 7a 45 6e 64  d>zCol && (*zEnd
2aeb9 3d 3d 27 3b 27 20 7c 7c 20 73 71 6c 69 74 65 33  ==';' || sqlite3
2aeba 49 73 73 70 61 63 65 28 2a 7a 45 6e 64 29 29 20  Isspace(*zEnd)) 
2aebb 29 7b 0a 20 20 20 20 20 20 2a 7a 45 6e 64 2d 2d  ){.      *zEnd--
2aebc 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 7d 0a 20   = '\0';.    }. 
2aebd 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20     db->flags |= 
2aebe 53 51 4c 49 54 45 5f 50 72 65 66 65 72 42 75 69  SQLITE_PreferBui
2aebf 6c 74 69 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65  ltin;.    sqlite
2aec0 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
2aec1 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 55  rse, .        "U
2aec2 50 44 41 54 45 20 5c 22 25 77 5c 22 2e 25 73 20  PDATE \"%w\".%s 
2aec3 53 45 54 20 22 0a 20 20 20 20 20 20 20 20 20 20  SET ".          
2aec4 22 73 71 6c 20 3d 20 73 75 62 73 74 72 28 73 71  "sql = substr(sq
2aec5 6c 2c 31 2c 25 64 29 20 7c 7c 20 27 2c 20 27 20  l,1,%d) || ', ' 
2aec6 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 74 72 28  || %Q || substr(
2aec7 73 71 6c 2c 25 64 29 20 22 0a 20 20 20 20 20 20  sql,%d) ".      
2aec8 20 20 22 57 48 45 52 45 20 74 79 70 65 20 3d 20    "WHERE type = 
2aec9 27 74 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d 65  'table' AND name
2aeca 20 3d 20 25 51 22 2c 20 0a 20 20 20 20 20 20 7a   = %Q", .      z
2aecb 44 62 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  Db, SCHEMA_TABLE
2aecc 28 69 44 62 29 2c 20 70 4e 65 77 2d 3e 61 64 64  (iDb), pNew->add
2aecd 43 6f 6c 4f 66 66 73 65 74 2c 20 7a 43 6f 6c 2c  ColOffset, zCol,
2aece 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66   pNew->addColOff
2aecf 73 65 74 2b 31 2c 0a 20 20 20 20 20 20 7a 54 61  set+1,.      zTa
2aed0 62 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c  b.    );.    sql
2aed1 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2aed2 43 6f 6c 29 3b 0a 20 20 20 20 64 62 2d 3e 66 6c  Col);.    db->fl
2aed3 61 67 73 20 3d 20 73 61 76 65 64 44 62 46 6c 61  ags = savedDbFla
2aed4 67 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  gs;.  }..  /* If
2aed5 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c   the default val
2aed6 75 65 20 6f 66 20 74 68 65 20 6e 65 77 20 63 6f  ue of the new co
2aed7 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68  lumn is NULL, th
2aed8 65 6e 20 73 65 74 20 74 68 65 20 66 69 6c 65 0a  en set the file.
2aed9 20 20 2a 2a 20 66 6f 72 6d 61 74 20 74 6f 20 32    ** format to 2
2aeda 2e 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74  . If the default
2aedb 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 65   value of the ne
2aedc 77 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20  w column is not 
2aedd 4e 55 4c 4c 2c 0a 20 20 2a 2a 20 74 68 65 20 66  NULL,.  ** the f
2aede 69 6c 65 20 66 6f 72 6d 61 74 20 62 65 63 6f 6d  ile format becom
2aedf 65 73 20 33 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  es 3..  */.  sql
2aee0 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46  ite3MinimumFileF
2aee1 6f 72 6d 61 74 28 70 50 61 72 73 65 2c 20 69 44  ormat(pParse, iD
2aee2 62 2c 20 70 44 66 6c 74 20 3f 20 33 20 3a 20 32  b, pDflt ? 3 : 2
2aee3 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20  );..  /* Reload 
2aee4 74 68 65 20 73 63 68 65 6d 61 20 6f 66 20 74 68  the schema of th
2aee5 65 20 6d 6f 64 69 66 69 65 64 20 74 61 62 6c 65  e modified table
2aee6 2e 20 2a 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62  . */.  reloadTab
2aee7 6c 65 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c  leSchema(pParse,
2aee8 20 70 54 61 62 2c 20 70 54 61 62 2d 3e 7a 4e 61   pTab, pTab->zNa
2aee9 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  me);.}../*.** Th
2aeea 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
2aeeb 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
2aeec 73 65 72 20 61 66 74 65 72 20 74 68 65 20 74 61  ser after the ta
2aeed 62 6c 65 2d 6e 61 6d 65 20 69 6e 0a 2a 2a 20 61  ble-name in.** a
2aeee 6e 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 3c  n "ALTER TABLE <
2aeef 74 61 62 6c 65 2d 6e 61 6d 65 3e 20 41 44 44 22  table-name> ADD"
2aef0 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61   statement is pa
2aef1 72 73 65 64 2e 20 41 72 67 75 6d 65 6e 74 20 0a  rsed. Argument .
2aef2 2a 2a 20 70 53 72 63 20 69 73 20 74 68 65 20 66  ** pSrc is the f
2aef3 75 6c 6c 2d 6e 61 6d 65 20 6f 66 20 74 68 65 20  ull-name of the 
2aef4 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65  table being alte
2aef5 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  red..**.** This 
2aef6 72 6f 75 74 69 6e 65 20 6d 61 6b 65 73 20 61 20  routine makes a 
2aef7 28 70 61 72 74 69 61 6c 29 20 63 6f 70 79 20 6f  (partial) copy o
2aef8 66 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  f the Table stru
2aef9 63 74 75 72 65 0a 2a 2a 20 66 6f 72 20 74 68 65  cture.** for the
2aefa 20 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74   table being alt
2aefb 65 72 65 64 20 61 6e 64 20 73 65 74 73 20 50 61  ered and sets Pa
2aefc 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 20 74 6f  rse.pNewTable to
2aefd 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 69 74 2e   point.** to it.
2aefe 20 52 6f 75 74 69 6e 65 73 20 63 61 6c 6c 65 64   Routines called
2aeff 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 61   by the parser a
2af00 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 66  s the column def
2af01 69 6e 69 74 69 6f 6e 0a 2a 2a 20 69 73 20 70 61  inition.** is pa
2af02 72 73 65 64 20 28 69 2e 65 2e 20 73 71 6c 69 74  rsed (i.e. sqlit
2af03 65 33 41 64 64 43 6f 6c 75 6d 6e 28 29 29 20 61  e3AddColumn()) a
2af04 64 64 20 74 68 65 20 6e 65 77 20 43 6f 6c 75 6d  dd the new Colum
2af05 6e 20 64 61 74 61 20 74 6f 20 0a 2a 2a 20 74 68  n data to .** th
2af06 65 20 63 6f 70 79 2e 20 54 68 65 20 63 6f 70 79  e copy. The copy
2af07 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73 74   of the Table st
2af08 72 75 63 74 75 72 65 20 69 73 20 64 65 6c 65 74  ructure is delet
2af09 65 64 20 62 79 20 74 6f 6b 65 6e 69 7a 65 2e 63  ed by tokenize.c
2af0a 20 0a 2a 2a 20 61 66 74 65 72 20 70 61 72 73 69   .** after parsi
2af0b 6e 67 20 69 73 20 66 69 6e 69 73 68 65 64 2e 0a  ng is finished..
2af0c 2a 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 73 71  **.** Routine sq
2af0d 6c 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68  lite3AlterFinish
2af0e 41 64 64 43 6f 6c 75 6d 6e 28 29 20 77 69 6c 6c  AddColumn() will
2af0f 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f   be called to co
2af10 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f 64 69 6e 67  mplete.** coding
2af11 20 74 68 65 20 22 41 4c 54 45 52 20 54 41 42 4c   the "ALTER TABL
2af12 45 20 2e 2e 2e 20 41 44 44 22 20 73 74 61 74 65  E ... ADD" state
2af13 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
2af14 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2af15 69 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64  ite3AlterBeginAd
2af16 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70  dColumn(Parse *p
2af17 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a  Parse, SrcList *
2af18 70 53 72 63 29 7b 0a 20 20 54 61 62 6c 65 20 2a  pSrc){.  Table *
2af19 70 4e 65 77 3b 0a 20 20 54 61 62 6c 65 20 2a 70  pNew;.  Table *p
2af1a 54 61 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  Tab;.  Vdbe *v;.
2af1b 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74    int iDb;.  int
2af1c 20 69 3b 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63   i;.  int nAlloc
2af1d 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
2af1e 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
2af1f 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 74 68 65 20   /* Look up the 
2af20 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65  table being alte
2af21 72 65 64 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  red. */.  assert
2af22 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  ( pParse->pNewTa
2af23 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  ble==0 );.  asse
2af24 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
2af25 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
2af26 64 62 29 20 29 3b 0a 20 20 69 66 28 20 64 62 2d  db) );.  if( db-
2af27 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
2af28 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f  goto exit_begin_
2af29 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 70 54  add_column;.  pT
2af2a 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  ab = sqlite3Loca
2af2b 74 65 54 61 62 6c 65 49 74 65 6d 28 70 50 61 72  teTableItem(pPar
2af2c 73 65 2c 20 30 2c 20 26 70 53 72 63 2d 3e 61 5b  se, 0, &pSrc->a[
2af2d 30 5d 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62  0]);.  if( !pTab
2af2e 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67   ) goto exit_beg
2af2f 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 0a  in_add_column;..
2af30 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2af31 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2af32 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
2af33 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71  (pTab) ){.    sq
2af34 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2af35 61 72 73 65 2c 20 22 76 69 72 74 75 61 6c 20 74  arse, "virtual t
2af36 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62 65  ables may not be
2af37 20 61 6c 74 65 72 65 64 22 29 3b 0a 20 20 20 20   altered");.    
2af38 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f  goto exit_begin_
2af39 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a  add_column;.  }.
2af3a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b  #endif..  /* Mak
2af3b 65 20 73 75 72 65 20 74 68 69 73 20 69 73 20 6e  e sure this is n
2af3c 6f 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f  ot an attempt to
2af3d 20 41 4c 54 45 52 20 61 20 76 69 65 77 2e 20 2a   ALTER a view. *
2af3e 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53  /.  if( pTab->pS
2af3f 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c  elect ){.    sql
2af40 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2af41 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64  rse, "Cannot add
2af42 20 61 20 63 6f 6c 75 6d 6e 20 74 6f 20 61 20 76   a column to a v
2af43 69 65 77 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20  iew");.    goto 
2af44 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63  exit_begin_add_c
2af45 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 20 20 69 66 28  olumn;.  }.  if(
2af46 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 69 73 53 79   SQLITE_OK!=isSy
2af47 73 74 65 6d 54 61 62 6c 65 28 70 50 61 72 73 65  stemTable(pParse
2af48 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 20 29  , pTab->zName) )
2af49 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
2af4a 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e  begin_add_column
2af4b 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
2af4c 20 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66 66   pTab->addColOff
2af4d 73 65 74 3e 30 20 29 3b 0a 20 20 69 44 62 20 3d  set>0 );.  iDb =
2af4e 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2af4f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e  Index(db, pTab->
2af50 70 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a 20  pSchema);..  /* 
2af51 50 75 74 20 61 20 63 6f 70 79 20 6f 66 20 74 68  Put a copy of th
2af52 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 20 69  e Table struct i
2af53 6e 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c  n Parse.pNewTabl
2af54 65 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 73  e for the.  ** s
2af55 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 28  qlite3AddColumn(
2af56 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 66  ) function and f
2af57 72 69 65 6e 64 73 20 74 6f 20 6d 6f 64 69 66 79  riends to modify
2af58 2e 20 20 42 75 74 20 6d 6f 64 69 66 79 0a 20 20  .  But modify.  
2af59 2a 2a 20 74 68 65 20 6e 61 6d 65 20 62 79 20 61  ** the name by a
2af5a 64 64 69 6e 67 20 61 6e 20 22 73 71 6c 69 74 65  dding an "sqlite
2af5b 5f 61 6c 74 65 72 74 61 62 5f 22 20 70 72 65 66  _altertab_" pref
2af5c 69 78 2e 20 20 42 79 20 61 64 64 69 6e 67 20 74  ix.  By adding t
2af5d 68 69 73 0a 20 20 2a 2a 20 70 72 65 66 69 78 2c  his.  ** prefix,
2af5e 20 77 65 20 69 6e 73 75 72 65 20 74 68 61 74 20   we insure that 
2af5f 74 68 65 20 6e 61 6d 65 20 77 69 6c 6c 20 6e 6f  the name will no
2af60 74 20 63 6f 6c 6c 69 64 65 20 77 69 74 68 20 61  t collide with a
2af61 6e 20 65 78 69 73 74 69 6e 67 0a 20 20 2a 2a 20  n existing.  ** 
2af62 74 61 62 6c 65 20 62 65 63 61 75 73 65 20 75 73  table because us
2af63 65 72 20 74 61 62 6c 65 20 61 72 65 20 6e 6f 74  er table are not
2af64 20 61 6c 6c 6f 77 65 64 20 74 6f 20 68 61 76 65   allowed to have
2af65 20 74 68 65 20 22 73 71 6c 69 74 65 5f 22 0a 20   the "sqlite_". 
2af66 20 2a 2a 20 70 72 65 66 69 78 20 6f 6e 20 74 68   ** prefix on th
2af67 65 69 72 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20  eir name..  */. 
2af68 20 70 4e 65 77 20 3d 20 28 54 61 62 6c 65 2a 29   pNew = (Table*)
2af69 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2af6a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54  ero(db, sizeof(T
2af6b 61 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 21 70  able));.  if( !p
2af6c 4e 65 77 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  New ) goto exit_
2af6d 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e  begin_add_column
2af6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  ;.  pParse->pNew
2af6f 54 61 62 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20  Table = pNew;.  
2af70 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a  pNew->nRef = 1;.
2af71 20 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20 70    pNew->nCol = p
2af72 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73 73  Tab->nCol;.  ass
2af73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c 3e  ert( pNew->nCol>
2af74 30 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d 20  0 );.  nAlloc = 
2af75 28 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 29  (((pNew->nCol-1)
2af76 2f 38 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73 65  /8)*8)+8;.  asse
2af77 72 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65 77  rt( nAlloc>=pNew
2af78 2d 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f 63  ->nCol && nAlloc
2af79 25 38 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63 2d  %8==0 && nAlloc-
2af7a 70 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b 0a  pNew->nCol<8 );.
2af7b 20 20 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20 28    pNew->aCol = (
2af7c 43 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33 44  Column*)sqlite3D
2af7d 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
2af7e 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 6e  sizeof(Column)*n
2af7f 41 6c 6c 6f 63 29 3b 0a 20 20 70 4e 65 77 2d 3e  Alloc);.  pNew->
2af80 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d  zName = sqlite3M
2af81 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69  Printf(db, "sqli
2af82 74 65 5f 61 6c 74 65 72 74 61 62 5f 25 73 22 2c  te_altertab_%s",
2af83 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
2af84 20 69 66 28 20 21 70 4e 65 77 2d 3e 61 43 6f 6c   if( !pNew->aCol
2af85 20 7c 7c 20 21 70 4e 65 77 2d 3e 7a 4e 61 6d 65   || !pNew->zName
2af86 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c   ){.    db->mall
2af87 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
2af88 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69    goto exit_begi
2af89 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20  n_add_column;.  
2af8a 7d 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d  }.  memcpy(pNew-
2af8b 3e 61 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 6f  >aCol, pTab->aCo
2af8c 6c 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e  l, sizeof(Column
2af8d 29 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a 20  )*pNew->nCol);. 
2af8e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65 77   for(i=0; i<pNew
2af8f 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
2af90 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d    Column *pCol =
2af91 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d 3b   &pNew->aCol[i];
2af92 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65  .    pCol->zName
2af93 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
2af94 75 70 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61  up(db, pCol->zNa
2af95 6d 65 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a  me);.    pCol->z
2af96 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70 43  Coll = 0;.    pC
2af97 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 30 3b 0a 20  ol->zType = 0;. 
2af98 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d     pCol->pDflt =
2af99 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 44   0;.    pCol->zD
2af9a 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70  flt = 0;.  }.  p
2af9b 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64  New->pSchema = d
2af9c 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
2af9d 65 6d 61 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64  ema;.  pNew->add
2af9e 43 6f 6c 4f 66 66 73 65 74 20 3d 20 70 54 61 62  ColOffset = pTab
2af9f 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b 0a  ->addColOffset;.
2afa0 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31    pNew->nRef = 1
2afa1 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20  ;..  /* Begin a 
2afa2 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20  transaction and 
2afa3 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 73 63  increment the sc
2afa4 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 2a 2f  hema cookie.  */
2afa5 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57  .  sqlite3BeginW
2afa6 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
2afa7 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20  arse, 0, iDb);. 
2afa8 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
2afa9 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
2afaa 66 28 20 21 76 20 29 20 67 6f 74 6f 20 65 78 69  f( !v ) goto exi
2afab 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75  t_begin_add_colu
2afac 6d 6e 3b 0a 20 20 73 71 6c 69 74 65 33 43 68 61  mn;.  sqlite3Cha
2afad 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65  ngeCookie(pParse
2afae 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74 5f 62 65  , iDb);..exit_be
2afaf 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3a 0a  gin_add_column:.
2afb0 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
2afb1 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29  Delete(db, pSrc)
2afb2 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65  ;.  return;.}.#e
2afb3 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ndif  /* SQLITE_
2afb4 41 4c 54 45 52 5f 54 41 42 4c 45 20 2a 2f 0a 0a  ALTER_TABLE */..
2afb5 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2afb6 45 6e 64 20 6f 66 20 61 6c 74 65 72 2e 63 20 2a  End of alter.c *
2afb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2afba 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2afbb 42 65 67 69 6e 20 66 69 6c 65 20 61 6e 61 6c 79  Begin file analy
2afbc 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ze.c ***********
2afbd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afbe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2afbf 2f 2a 0a 2a 2a 20 32 30 30 35 20 4a 75 6c 79 20  /*.** 2005 July 
2afc0 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  8.**.** The auth
2afc1 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
2afc2 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
2afc3 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
2afc4 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2afc5 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2afc6 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2afc7 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2afc8 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2afc9 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2afca 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
2afcb 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
2afcc 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
2afcd 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
2afce 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
2afcf 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
2afd0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
2afd1 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
2afd2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afd3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2afd6 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
2afd7 6e 74 61 69 6e 73 20 63 6f 64 65 20 61 73 73 6f  ntains code asso
2afd8 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
2afd9 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e  ANALYZE command.
2afda 0a 2a 2a 0a 2a 2a 20 54 68 65 20 41 4e 41 4c 59  .**.** The ANALY
2afdb 5a 45 20 63 6f 6d 6d 61 6e 64 20 67 61 74 68 65  ZE command gathe
2afdc 72 20 73 74 61 74 69 73 74 69 63 73 20 61 62 6f  r statistics abo
2afdd 75 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ut the content o
2afde 66 20 74 61 62 6c 65 73 0a 2a 2a 20 61 6e 64 20  f tables.** and 
2afdf 69 6e 64 69 63 65 73 2e 20 20 54 68 65 73 65 20  indices.  These 
2afe0 73 74 61 74 69 73 74 69 63 73 20 61 72 65 20 6d  statistics are m
2afe1 61 64 65 20 61 76 61 69 6c 61 62 6c 65 20 74 6f  ade available to
2afe2 20 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 6e   the query plann
2afe3 65 72 0a 2a 2a 20 74 6f 20 68 65 6c 70 20 69 74  er.** to help it
2afe4 20 6d 61 6b 65 20 62 65 74 74 65 72 20 64 65 63   make better dec
2afe5 69 73 69 6f 6e 73 20 61 62 6f 75 74 20 68 6f 77  isions about how
2afe6 20 74 6f 20 70 65 72 66 6f 72 6d 20 71 75 65 72   to perform quer
2afe7 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ies..**.** The f
2afe8 6f 6c 6c 6f 77 69 6e 67 20 73 79 73 74 65 6d 20  ollowing system 
2afe9 74 61 62 6c 65 73 20 61 72 65 20 6f 72 20 68 61  tables are or ha
2afea 76 65 20 62 65 65 6e 20 73 75 70 70 6f 72 74 65  ve been supporte
2afeb 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 43 52 45 41  d:.**.**    CREA
2afec 54 45 20 54 41 42 4c 45 20 73 71 6c 69 74 65 5f  TE TABLE sqlite_
2afed 73 74 61 74 31 28 74 62 6c 2c 20 69 64 78 2c 20  stat1(tbl, idx, 
2afee 73 74 61 74 29 3b 0a 2a 2a 20 20 20 20 43 52 45  stat);.**    CRE
2afef 41 54 45 20 54 41 42 4c 45 20 73 71 6c 69 74 65  ATE TABLE sqlite
2aff0 5f 73 74 61 74 32 28 74 62 6c 2c 20 69 64 78 2c  _stat2(tbl, idx,
2aff1 20 73 61 6d 70 6c 65 6e 6f 2c 20 73 61 6d 70 6c   sampleno, sampl
2aff2 65 29 3b 0a 2a 2a 20 20 20 20 43 52 45 41 54 45  e);.**    CREATE
2aff3 20 54 41 42 4c 45 20 73 71 6c 69 74 65 5f 73 74   TABLE sqlite_st
2aff4 61 74 33 28 74 62 6c 2c 20 69 64 78 2c 20 6e 45  at3(tbl, idx, nE
2aff5 71 2c 20 6e 4c 74 2c 20 6e 44 4c 74 2c 20 73 61  q, nLt, nDLt, sa
2aff6 6d 70 6c 65 29 3b 0a 2a 2a 0a 2a 2a 20 41 64 64  mple);.**.** Add
2aff7 69 74 69 6f 6e 61 6c 20 74 61 62 6c 65 73 20 6d  itional tables m
2aff8 69 67 68 74 20 62 65 20 61 64 64 65 64 20 69 6e  ight be added in
2aff9 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
2affa 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 54   of SQLite..** T
2affb 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20  he sqlite_stat2 
2affc 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 72 65  table is not cre
2affd 61 74 65 64 20 6f 72 20 75 73 65 64 20 75 6e 6c  ated or used unl
2affe 65 73 73 20 74 68 65 20 53 51 4c 69 74 65 20 76  ess the SQLite v
2afff 65 72 73 69 6f 6e 0a 2a 2a 20 69 73 20 62 65 74  ersion.** is bet
2b000 77 65 65 6e 20 33 2e 36 2e 31 38 20 61 6e 64 20  ween 3.6.18 and 
2b001 33 2e 37 2e 38 2c 20 69 6e 63 6c 75 73 69 76 65  3.7.8, inclusive
2b002 2c 20 61 6e 64 20 75 6e 6c 65 73 73 20 53 51 4c  , and unless SQL
2b003 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 0a  ite is compiled.
2b004 2a 2a 20 77 69 74 68 20 53 51 4c 49 54 45 5f 45  ** with SQLITE_E
2b005 4e 41 42 4c 45 5f 53 54 41 54 32 2e 20 20 54 68  NABLE_STAT2.  Th
2b006 65 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74  e sqlite_stat2 t
2b007 61 62 6c 65 20 69 73 20 64 65 70 72 65 63 61 74  able is deprecat
2b008 65 64 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ed..** The sqlit
2b009 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20 69 73  e_stat2 table is
2b00a 20 73 75 70 65 72 63 65 64 65 64 20 62 79 20 73   superceded by s
2b00b 71 6c 69 74 65 5f 73 74 61 74 33 2c 20 77 68 69  qlite_stat3, whi
2b00c 63 68 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 63 72  ch is only.** cr
2b00d 65 61 74 65 64 20 61 6e 64 20 75 73 65 64 20 62  eated and used b
2b00e 79 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e  y SQLite version
2b00f 73 20 33 2e 37 2e 39 20 61 6e 64 20 6c 61 74 65  s 3.7.9 and late
2b010 72 20 61 6e 64 20 77 69 74 68 0a 2a 2a 20 53 51  r and with.** SQ
2b011 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54  LITE_ENABLE_STAT
2b012 33 20 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20  3 defined.  The 
2b013 66 75 63 6e 74 69 6f 6e 61 6c 69 74 79 20 6f 66  fucntionality of
2b014 20 73 71 6c 69 74 65 5f 73 74 61 74 33 0a 2a 2a   sqlite_stat3.**
2b015 20 69 73 20 61 20 73 75 70 65 72 73 65 74 20 6f   is a superset o
2b016 66 20 73 71 6c 69 74 65 5f 73 74 61 74 32 2e 20  f sqlite_stat2. 
2b017 20 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 61 74 20 6f   .**.** Format o
2b018 66 20 73 71 6c 69 74 65 5f 73 74 61 74 31 3a 0a  f sqlite_stat1:.
2b019 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e  **.** There is n
2b01a 6f 72 6d 61 6c 6c 79 20 6f 6e 65 20 72 6f 77 20  ormally one row 
2b01b 70 65 72 20 69 6e 64 65 78 2c 20 77 69 74 68 20  per index, with 
2b01c 74 68 65 20 69 6e 64 65 78 20 69 64 65 6e 74 69  the index identi
2b01d 66 69 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 6e  fied by the.** n
2b01e 61 6d 65 20 69 6e 20 74 68 65 20 69 64 78 20 63  ame in the idx c
2b01f 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 74 62 6c 20  olumn.  The tbl 
2b020 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 6e 61  column is the na
2b021 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
2b022 74 6f 0a 2a 2a 20 77 68 69 63 68 20 74 68 65 20  to.** which the 
2b023 69 6e 64 65 78 20 62 65 6c 6f 6e 67 73 2e 20 20  index belongs.  
2b024 49 6e 20 65 61 63 68 20 73 75 63 68 20 72 6f 77  In each such row
2b025 2c 20 74 68 65 20 73 74 61 74 20 63 6f 6c 75 6d  , the stat colum
2b026 6e 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 61 20 73  n will be.** a s
2b027 74 72 69 6e 67 20 63 6f 6e 73 69 73 74 69 6e 67  tring consisting
2b028 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 20 69 6e   of a list of in
2b029 74 65 67 65 72 73 2e 20 20 54 68 65 20 66 69 72  tegers.  The fir
2b02a 73 74 20 69 6e 74 65 67 65 72 20 69 6e 20 74 68  st integer in th
2b02b 69 73 0a 2a 2a 20 6c 69 73 74 20 69 73 20 74 68  is.** list is th
2b02c 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
2b02d 20 69 6e 20 74 68 65 20 69 6e 64 65 78 20 61 6e   in the index an
2b02e 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20  d in the table. 
2b02f 20 54 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 69   The second.** i
2b030 6e 74 65 67 65 72 20 69 73 20 74 68 65 20 61 76  nteger is the av
2b031 65 72 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20  erage number of 
2b032 72 6f 77 73 20 69 6e 20 74 68 65 20 69 6e 64 65  rows in the inde
2b033 78 20 74 68 61 74 20 68 61 76 65 20 74 68 65 20  x that have the 
2b034 73 61 6d 65 0a 2a 2a 20 76 61 6c 75 65 20 69 6e  same.** value in
2b035 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d   the first colum
2b036 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20  n of the index. 
2b037 20 54 68 65 20 74 68 69 72 64 20 69 6e 74 65 67   The third integ
2b038 65 72 20 69 73 20 74 68 65 20 61 76 65 72 61 67  er is the averag
2b039 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 72  e.** number of r
2b03a 6f 77 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78  ows in the index
2b03b 20 74 68 61 74 20 68 61 76 65 20 74 68 65 20 73   that have the s
2b03c 61 6d 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ame value for th
2b03d 65 20 66 69 72 73 74 20 74 77 6f 0a 2a 2a 20 63  e first two.** c
2b03e 6f 6c 75 6d 6e 73 2e 20 20 54 68 65 20 4e 2d 74  olumns.  The N-t
2b03f 68 20 69 6e 74 65 67 65 72 20 28 66 6f 72 20 4e  h integer (for N
2b040 3e 31 29 20 69 73 20 74 68 65 20 61 76 65 72 61  >1) is the avera
2b041 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  ge number of row
2b042 73 20 69 6e 20 0a 2a 2a 20 74 68 65 20 69 6e 64  s in .** the ind
2b043 65 78 20 77 68 69 63 68 20 68 61 76 65 20 74 68  ex which have th
2b044 65 20 73 61 6d 65 20 76 61 6c 75 65 20 66 6f 72  e same value for
2b045 20 74 68 65 20 66 69 72 73 74 20 4e 2d 31 20 63   the first N-1 c
2b046 6f 6c 75 6d 6e 73 2e 20 20 46 6f 72 0a 2a 2a 20  olumns.  For.** 
2b047 61 20 4b 2d 63 6f 6c 75 6d 6e 20 69 6e 64 65 78  a K-column index
2b048 2c 20 74 68 65 72 65 20 77 69 6c 6c 20 62 65 20  , there will be 
2b049 4b 2b 31 20 69 6e 74 65 67 65 72 73 20 69 6e 20  K+1 integers in 
2b04a 74 68 65 20 73 74 61 74 20 63 6f 6c 75 6d 6e 2e  the stat column.
2b04b 20 20 49 66 0a 2a 2a 20 74 68 65 20 69 6e 64 65    If.** the inde
2b04c 78 20 69 73 20 75 6e 69 71 75 65 2c 20 74 68 65  x is unique, the
2b04d 6e 20 74 68 65 20 6c 61 73 74 20 69 6e 74 65 67  n the last integ
2b04e 65 72 20 77 69 6c 6c 20 62 65 20 31 2e 0a 2a 2a  er will be 1..**
2b04f 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 20  .** The list of 
2b050 69 6e 74 65 67 65 72 73 20 69 6e 20 74 68 65 20  integers in the 
2b051 73 74 61 74 20 63 6f 6c 75 6d 6e 20 63 61 6e 20  stat column can 
2b052 6f 70 74 69 6f 6e 61 6c 6c 79 20 62 65 20 66 6f  optionally be fo
2b053 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 74 68 65  llowed.** by the
2b054 20 6b 65 79 77 6f 72 64 20 22 75 6e 6f 72 64 65   keyword "unorde
2b055 72 65 64 22 2e 20 20 54 68 65 20 22 75 6e 6f 72  red".  The "unor
2b056 64 65 72 65 64 22 20 6b 65 79 77 6f 72 64 2c 20  dered" keyword, 
2b057 69 66 20 69 74 20 69 73 20 70 72 65 73 65 6e 74  if it is present
2b058 2c 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73 65 70  ,.** must be sep
2b059 61 72 61 74 65 64 20 66 72 6f 6d 20 74 68 65 20  arated from the 
2b05a 6c 61 73 74 20 69 6e 74 65 67 65 72 20 62 79 20  last integer by 
2b05b 61 20 73 69 6e 67 6c 65 20 73 70 61 63 65 2e 20  a single space. 
2b05c 20 49 66 20 74 68 65 0a 2a 2a 20 22 75 6e 6f 72   If the.** "unor
2b05d 64 65 72 65 64 22 20 6b 65 79 77 6f 72 64 20 69  dered" keyword i
2b05e 73 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e 20  s present, then 
2b05f 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 6e 65  the query planne
2b060 72 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a  r assumes that.*
2b061 2a 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 75  * the index is u
2b062 6e 6f 72 64 65 72 65 64 20 61 6e 64 20 77 69 6c  nordered and wil
2b063 6c 20 6e 6f 74 20 75 73 65 20 74 68 65 20 69 6e  l not use the in
2b064 64 65 78 20 66 6f 72 20 61 20 72 61 6e 67 65 20  dex for a range 
2b065 71 75 65 72 79 2e 0a 2a 2a 20 0a 2a 2a 20 49 66  query..** .** If
2b066 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
2b067 31 2e 69 64 78 20 63 6f 6c 75 6d 6e 20 69 73 20  1.idx column is 
2b068 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 73  NULL, then the s
2b069 71 6c 69 74 65 5f 73 74 61 74 31 2e 73 74 61 74  qlite_stat1.stat
2b06a 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61  .** column conta
2b06b 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 69 6e 74  ins a single int
2b06c 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68  eger which is th
2b06d 65 20 28 65 73 74 69 6d 61 74 65 64 29 20 6e 75  e (estimated) nu
2b06e 6d 62 65 72 20 6f 66 0a 2a 2a 20 72 6f 77 73 20  mber of.** rows 
2b06f 69 6e 20 74 68 65 20 74 61 62 6c 65 20 69 64 65  in the table ide
2b070 6e 74 69 66 69 65 64 20 62 79 20 73 71 6c 69 74  ntified by sqlit
2b071 65 5f 73 74 61 74 31 2e 74 62 6c 2e 0a 2a 2a 0a  e_stat1.tbl..**.
2b072 2a 2a 20 46 6f 72 6d 61 74 20 6f 66 20 73 71 6c  ** Format of sql
2b073 69 74 65 5f 73 74 61 74 32 3a 0a 2a 2a 0a 2a 2a  ite_stat2:.**.**
2b074 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   The sqlite_stat
2b075 32 20 69 73 20 6f 6e 6c 79 20 63 72 65 61 74 65  2 is only create
2b076 64 20 61 6e 64 20 69 73 20 6f 6e 6c 79 20 75 73  d and is only us
2b077 65 64 20 69 66 20 53 51 4c 69 74 65 20 69 73 20  ed if SQLite is 
2b078 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74 68  compiled.** with
2b079 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
2b07a 54 41 54 32 20 61 6e 64 20 69 66 20 74 68 65 20  TAT2 and if the 
2b07b 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 6e  SQLite version n
2b07c 75 6d 62 65 72 20 69 73 20 62 65 74 77 65 65 6e  umber is between
2b07d 0a 2a 2a 20 33 2e 36 2e 31 38 20 61 6e 64 20 33  .** 3.6.18 and 3
2b07e 2e 37 2e 38 2e 20 20 54 68 65 20 22 73 74 61 74  .7.8.  The "stat
2b07f 32 22 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  2" table contain
2b080 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  s additional inf
2b081 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75  ormation.** abou
2b082 74 20 74 68 65 20 64 69 73 74 72 69 62 75 74 69  t the distributi
2b083 6f 6e 20 6f 66 20 6b 65 79 73 20 77 69 74 68 69  on of keys withi
2b084 6e 20 61 6e 20 69 6e 64 65 78 2e 20 20 54 68 65  n an index.  The
2b085 20 69 6e 64 65 78 20 69 73 20 69 64 65 6e 74 69   index is identi
2b086 66 69 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 22  fied by.** the "
2b087 69 64 78 22 20 63 6f 6c 75 6d 6e 20 61 6e 64 20  idx" column and 
2b088 74 68 65 20 22 74 62 6c 22 20 63 6f 6c 75 6d 6e  the "tbl" column
2b089 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
2b08a 74 68 65 20 74 61 62 6c 65 20 74 6f 20 77 68 69  the table to whi
2b08b 63 68 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20  ch.** the index 
2b08c 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 72 65 20  belongs.  There 
2b08d 61 72 65 20 75 73 75 61 6c 6c 79 20 31 30 20 72  are usually 10 r
2b08e 6f 77 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74  ows in the sqlit
2b08f 65 5f 73 74 61 74 32 0a 2a 2a 20 74 61 62 6c 65  e_stat2.** table
2b090 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78 2e   for each index.
2b091 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
2b092 65 5f 73 74 61 74 32 20 65 6e 74 72 69 65 73 20  e_stat2 entries 
2b093 66 6f 72 20 61 6e 20 69 6e 64 65 78 20 74 68 61  for an index tha
2b094 74 20 68 61 76 65 20 73 61 6d 70 6c 65 6e 6f 20  t have sampleno 
2b095 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 39 0a  between 0 and 9.
2b096 2a 2a 20 69 6e 63 6c 75 73 69 76 65 20 61 72 65  ** inclusive are
2b097 20 73 61 6d 70 6c 65 73 20 6f 66 20 74 68 65 20   samples of the 
2b098 6c 65 66 74 2d 6d 6f 73 74 20 6b 65 79 20 76 61  left-most key va
2b099 6c 75 65 20 69 6e 20 74 68 65 20 69 6e 64 65 78  lue in the index
2b09a 20 74 61 6b 65 6e 20 61 74 0a 2a 2a 20 65 76 65   taken at.** eve
2b09b 6e 6c 79 20 73 70 61 63 65 64 20 70 6f 69 6e 74  nly spaced point
2b09c 73 20 61 6c 6f 6e 67 20 74 68 65 20 69 6e 64 65  s along the inde
2b09d 78 2e 20 20 4c 65 74 20 74 68 65 20 6e 75 6d 62  x.  Let the numb
2b09e 65 72 20 6f 66 20 73 61 6d 70 6c 65 73 20 62 65  er of samples be
2b09f 20 53 0a 2a 2a 20 28 31 30 20 69 6e 20 74 68 65   S.** (10 in the
2b0a0 20 73 74 61 6e 64 61 72 64 20 62 75 69 6c 64 29   standard build)
2b0a1 20 61 6e 64 20 6c 65 74 20 43 20 62 65 20 74 68   and let C be th
2b0a2 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
2b0a3 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a   in the index..*
2b0a4 2a 20 54 68 65 6e 20 74 68 65 20 73 61 6d 70 6c  * Then the sampl
2b0a5 65 64 20 72 6f 77 73 20 61 72 65 20 67 69 76 65  ed rows are give
2b0a6 6e 20 62 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  n by:.**.**     
2b0a7 72 6f 77 6e 75 6d 62 65 72 20 3d 20 28 69 2a 43  rownumber = (i*C
2b0a8 2a 32 20 2b 20 43 29 2f 28 53 2a 32 29 0a 2a 2a  *2 + C)/(S*2).**
2b0a9 0a 2a 2a 20 46 6f 72 20 69 20 62 65 74 77 65 65  .** For i betwee
2b0aa 6e 20 30 20 61 6e 64 20 53 2d 31 2e 20 20 43 6f  n 0 and S-1.  Co
2b0ab 6e 63 65 70 74 75 61 6c 6c 79 2c 20 74 68 65 20  nceptually, the 
2b0ac 69 6e 64 65 78 20 73 70 61 63 65 20 69 73 20 64  index space is d
2b0ad 69 76 69 64 65 64 20 69 6e 74 6f 0a 2a 2a 20 53  ivided into.** S
2b0ae 20 75 6e 69 66 6f 72 6d 20 62 75 63 6b 65 74 73   uniform buckets
2b0af 20 61 6e 64 20 74 68 65 20 73 61 6d 70 6c 65 73   and the samples
2b0b0 20 61 72 65 20 74 68 65 20 6d 69 64 64 6c 65 20   are the middle 
2b0b1 72 6f 77 20 66 72 6f 6d 20 65 61 63 68 20 62 75  row from each bu
2b0b2 63 6b 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  cket..**.** The 
2b0b3 66 6f 72 6d 61 74 20 66 6f 72 20 73 71 6c 69 74  format for sqlit
2b0b4 65 5f 73 74 61 74 32 20 69 73 20 72 65 63 6f 72  e_stat2 is recor
2b0b5 64 65 64 20 68 65 72 65 20 66 6f 72 20 6c 65 67  ded here for leg
2b0b6 61 63 79 20 72 65 66 65 72 65 6e 63 65 2e 20 20  acy reference.  
2b0b7 54 68 69 73 0a 2a 2a 20 76 65 72 73 69 6f 6e 20  This.** version 
2b0b8 6f 66 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e  of SQLite does n
2b0b9 6f 74 20 73 75 70 70 6f 72 74 20 73 71 6c 69 74  ot support sqlit
2b0ba 65 5f 73 74 61 74 32 2e 20 20 49 74 20 6e 65 69  e_stat2.  It nei
2b0bb 74 68 65 72 20 72 65 61 64 73 20 6e 6f 72 0a 2a  ther reads nor.*
2b0bc 2a 20 77 72 69 74 65 73 20 74 68 65 20 73 71 6c  * writes the sql
2b0bd 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 2e  ite_stat2 table.
2b0be 20 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f    This version o
2b0bf 66 20 53 51 4c 69 74 65 20 6f 6e 6c 79 20 73 75  f SQLite only su
2b0c0 70 70 6f 72 74 73 0a 2a 2a 20 73 71 6c 69 74 65  pports.** sqlite
2b0c1 5f 73 74 61 74 33 2e 0a 2a 2a 0a 2a 2a 20 46 6f  _stat3..**.** Fo
2b0c2 72 6d 61 74 20 66 6f 72 20 73 71 6c 69 74 65 5f  rmat for sqlite_
2b0c3 73 74 61 74 33 3a 0a 2a 2a 0a 2a 2a 20 54 68 65  stat3:.**.** The
2b0c4 20 73 71 6c 69 74 65 5f 73 74 61 74 33 20 69 73   sqlite_stat3 is
2b0c5 20 61 6e 20 65 6e 68 61 6e 63 65 6d 65 6e 74 20   an enhancement 
2b0c6 74 6f 20 73 71 6c 69 74 65 5f 73 74 61 74 32 2e  to sqlite_stat2.
2b0c7 20 20 41 20 6e 65 77 20 6e 61 6d 65 20 69 73 0a    A new name is.
2b0c8 2a 2a 20 75 73 65 64 20 74 6f 20 61 76 6f 69 64  ** used to avoid
2b0c9 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 70   compatibility p
2b0ca 72 6f 62 6c 65 6d 73 2e 20 20 0a 2a 2a 0a 2a 2a  roblems.  .**.**
2b0cb 20 54 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74   The format of t
2b0cc 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 33 20  he sqlite_stat3 
2b0cd 74 61 62 6c 65 20 69 73 20 73 69 6d 69 6c 61 72  table is similar
2b0ce 20 74 6f 20 74 68 65 20 66 6f 72 6d 61 74 20 6f   to the format o
2b0cf 66 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f  f.** the sqlite_
2b0d0 73 74 61 74 32 20 74 61 62 6c 65 2e 20 20 54 68  stat2 table.  Th
2b0d1 65 72 65 20 61 72 65 20 6d 75 6c 74 69 70 6c 65  ere are multiple
2b0d2 20 65 6e 74 72 69 65 73 20 66 6f 72 20 65 61 63   entries for eac
2b0d3 68 20 69 6e 64 65 78 2e 0a 2a 2a 20 54 68 65 20  h index..** The 
2b0d4 69 64 78 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  idx column names
2b0d5 20 74 68 65 20 69 6e 64 65 78 20 61 6e 64 20 74   the index and t
2b0d6 68 65 20 74 62 6c 20 63 6f 6c 75 6d 6e 20 69 73  he tbl column is
2b0d7 20 74 68 65 20 74 61 62 6c 65 20 6f 66 20 74 68   the table of th
2b0d8 65 0a 2a 2a 20 69 6e 64 65 78 2e 20 20 49 66 20  e.** index.  If 
2b0d9 74 68 65 20 69 64 78 20 61 6e 64 20 74 62 6c 20  the idx and tbl 
2b0da 63 6f 6c 75 6d 6e 73 20 61 72 65 20 74 68 65 20  columns are the 
2b0db 73 61 6d 65 2c 20 74 68 65 6e 20 74 68 65 20 73  same, then the s
2b0dc 61 6d 70 6c 65 20 69 73 0a 2a 2a 20 6f 66 20 74  ample is.** of t
2b0dd 68 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  he INTEGER PRIMA
2b0de 52 59 20 4b 45 59 2e 20 20 54 68 65 20 73 61 6d  RY KEY.  The sam
2b0df 70 6c 65 20 63 6f 6c 75 6d 6e 20 69 73 20 61 20  ple column is a 
2b0e0 76 61 6c 75 65 20 74 61 6b 65 6e 20 66 72 6f 6d  value taken from
2b0e1 0a 2a 2a 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73  .** the left-mos
2b0e2 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  t column of the 
2b0e3 69 6e 64 65 78 2e 20 20 54 68 65 20 6e 45 71 20  index.  The nEq 
2b0e4 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 61 70  column is the ap
2b0e5 70 72 6f 78 69 6d 61 74 65 0a 2a 2a 20 6e 75 6d  proximate.** num
2b0e6 62 65 72 20 6f 66 20 65 6e 74 69 72 65 73 20 69  ber of entires i
2b0e7 6e 20 74 68 65 20 69 6e 64 65 78 20 77 68 6f 73  n the index whos
2b0e8 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75  e left-most colu
2b0e9 6d 6e 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68  mn exactly match
2b0ea 65 73 0a 2a 2a 20 74 68 65 20 73 61 6d 70 6c 65  es.** the sample
2b0eb 2e 20 20 6e 4c 74 20 69 73 20 74 68 65 20 61 70  .  nLt is the ap
2b0ec 70 72 6f 78 69 6d 61 74 65 20 6e 75 6d 62 65 72  proximate number
2b0ed 20 6f 66 20 65 6e 74 69 72 65 73 20 77 68 6f 73   of entires whos
2b0ee 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 2a 2a 20 63  e left-most.** c
2b0ef 6f 6c 75 6d 6e 20 69 73 20 6c 65 73 73 20 74 68  olumn is less th
2b0f0 61 6e 20 74 68 65 20 73 61 6d 70 6c 65 2e 20 20  an the sample.  
2b0f1 54 68 65 20 6e 44 4c 74 20 63 6f 6c 75 6d 6e 20  The nDLt column 
2b0f2 69 73 20 74 68 65 20 61 70 70 72 6f 78 69 6d 61  is the approxima
2b0f3 74 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  te.** number of 
2b0f4 64 69 73 74 69 6e 63 74 20 6c 65 66 74 2d 6d 6f  distinct left-mo
2b0f5 73 74 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  st entries in th
2b0f6 65 20 69 6e 64 65 78 20 74 68 61 74 20 61 72 65  e index that are
2b0f7 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 74 68   less than.** th
2b0f8 65 20 73 61 6d 70 6c 65 2e 0a 2a 2a 0a 2a 2a 20  e sample..**.** 
2b0f9 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  Future versions 
2b0fa 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 20  of SQLite might 
2b0fb 63 68 61 6e 67 65 20 74 6f 20 73 74 6f 72 65 20  change to store 
2b0fc 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e  a string contain
2b0fd 69 6e 67 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20  ing.** multiple 
2b0fe 69 6e 74 65 67 65 72 73 20 76 61 6c 75 65 73 20  integers values 
2b0ff 69 6e 20 74 68 65 20 6e 44 4c 74 20 63 6f 6c 75  in the nDLt colu
2b100 6d 6e 20 6f 66 20 73 71 6c 69 74 65 5f 73 74 61  mn of sqlite_sta
2b101 74 33 2e 20 20 54 68 65 20 66 69 72 73 74 0a 2a  t3.  The first.*
2b102 2a 20 69 6e 74 65 67 65 72 20 77 69 6c 6c 20 62  * integer will b
2b103 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
2b104 70 72 69 6f 72 20 69 6e 64 65 78 20 65 6e 74 69  prior index enti
2b105 72 65 73 20 74 68 61 74 20 61 72 65 20 64 69 73  res that are dis
2b106 74 69 6e 63 74 20 69 6e 0a 2a 2a 20 74 68 65 20  tinct in.** the 
2b107 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e  left-most column
2b108 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 69 6e  .  The second in
2b109 74 65 67 65 72 20 77 69 6c 6c 20 62 65 20 74 68  teger will be th
2b10a 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f  e number of prio
2b10b 72 20 69 6e 64 65 78 0a 2a 2a 20 65 6e 74 72 69  r index.** entri
2b10c 65 73 20 74 68 61 74 20 61 72 65 20 64 69 73 74  es that are dist
2b10d 69 6e 63 74 20 69 6e 20 74 68 65 20 66 69 72 73  inct in the firs
2b10e 74 20 74 77 6f 20 63 6f 6c 75 6d 6e 73 2e 20 20  t two columns.  
2b10f 54 68 65 20 74 68 69 72 64 20 69 6e 74 65 67 65  The third intege
2b110 72 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 74 68 65  r.** will be the
2b111 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f 72   number of prior
2b112 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 74   index entries t
2b113 68 61 74 20 61 72 65 20 64 69 73 74 69 6e 63 74  hat are distinct
2b114 20 69 6e 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   in the first.**
2b115 20 74 68 72 65 65 20 63 6f 6c 75 6d 6e 73 2e 20   three columns. 
2b116 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20   And so forth.  
2b117 57 69 74 68 20 74 68 61 74 20 65 78 74 65 6e 73  With that extens
2b118 69 6f 6e 2c 20 74 68 65 20 6e 44 4c 74 20 66 69  ion, the nDLt fi
2b119 65 6c 64 20 69 73 0a 2a 2a 20 73 69 6d 69 6c 61  eld is.** simila
2b11a 72 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20 74 6f  r in function to
2b11b 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
2b11c 31 2e 73 74 61 74 20 66 69 65 6c 64 2e 0a 2a 2a  1.stat field..**
2b11d 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 62 65  .** There can be
2b11e 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 6e 75   an arbitrary nu
2b11f 6d 62 65 72 20 6f 66 20 73 71 6c 69 74 65 5f 73  mber of sqlite_s
2b120 74 61 74 33 20 65 6e 74 72 69 65 73 20 70 65 72  tat3 entries per
2b121 20 69 6e 64 65 78 2e 0a 2a 2a 20 54 68 65 20 41   index..** The A
2b122 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 20 77  NALYZE command w
2b123 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 67 65  ill typically ge
2b124 6e 65 72 61 74 65 20 73 71 6c 69 74 65 5f 73 74  nerate sqlite_st
2b125 61 74 33 20 74 61 62 6c 65 73 0a 2a 2a 20 74 68  at3 tables.** th
2b126 61 74 20 63 6f 6e 74 61 69 6e 20 62 65 74 77 65  at contain betwe
2b127 65 6e 20 31 30 20 61 6e 64 20 34 30 20 73 61 6d  en 10 and 40 sam
2b128 70 6c 65 73 20 77 68 69 63 68 20 61 72 65 20 64  ples which are d
2b129 69 73 74 72 69 62 75 74 65 64 20 61 63 72 6f 73  istributed acros
2b12a 73 0a 2a 2a 20 74 68 65 20 6b 65 79 20 73 70 61  s.** the key spa
2b12b 63 65 2c 20 74 68 6f 75 67 68 20 6e 6f 74 20 75  ce, though not u
2b12c 6e 69 66 6f 72 6d 6c 79 2c 20 61 6e 64 20 77 68  niformly, and wh
2b12d 69 63 68 20 69 6e 63 6c 75 64 65 20 73 61 6d 70  ich include samp
2b12e 6c 65 73 20 77 69 74 68 0a 2a 2a 20 6c 61 72 67  les with.** larg
2b12f 65 73 74 20 70 6f 73 73 69 62 6c 65 20 6e 45 71  est possible nEq
2b130 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e   values..*/.#ifn
2b131 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2b132 41 4e 41 4c 59 5a 45 0a 0a 2f 2a 0a 2a 2a 20 54  ANALYZE../*.** T
2b133 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65  his routine gene
2b134 72 61 74 65 73 20 63 6f 64 65 20 74 68 61 74 20  rates code that 
2b135 6f 70 65 6e 73 20 74 68 65 20 73 71 6c 69 74 65  opens the sqlite
2b136 5f 73 74 61 74 31 20 74 61 62 6c 65 20 66 6f 72  _stat1 table for
2b137 0a 2a 2a 20 77 72 69 74 69 6e 67 20 77 69 74 68  .** writing with
2b138 20 63 75 72 73 6f 72 20 69 53 74 61 74 43 75 72   cursor iStatCur
2b139 2e 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 79  . If the library
2b13a 20 77 61 73 20 62 75 69 6c 74 20 77 69 74 68 20   was built with 
2b13b 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e  the.** SQLITE_EN
2b13c 41 42 4c 45 5f 53 54 41 54 33 20 6d 61 63 72 6f  ABLE_STAT3 macro
2b13d 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74   defined, then t
2b13e 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 33 20  he sqlite_stat3 
2b13f 74 61 62 6c 65 20 69 73 0a 2a 2a 20 6f 70 65 6e  table is.** open
2b140 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 20 75  ed for writing u
2b141 73 69 6e 67 20 63 75 72 73 6f 72 20 28 69 53 74  sing cursor (iSt
2b142 61 74 43 75 72 2b 31 29 0a 2a 2a 0a 2a 2a 20 49  atCur+1).**.** I
2b143 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  f the sqlite_sta
2b144 74 31 20 74 61 62 6c 65 73 20 64 6f 65 73 20 6e  t1 tables does n
2b145 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78  ot previously ex
2b146 69 73 74 2c 20 69 74 20 69 73 20 63 72 65 61 74  ist, it is creat
2b147 65 64 2e 0a 2a 2a 20 53 69 6d 69 6c 61 72 6c 79  ed..** Similarly
2b148 2c 20 69 66 20 74 68 65 20 73 71 6c 69 74 65 5f  , if the sqlite_
2b149 73 74 61 74 33 20 74 61 62 6c 65 20 64 6f 65 73  stat3 table does
2b14a 20 6e 6f 74 20 65 78 69 73 74 20 61 6e 64 20 74   not exist and t
2b14b 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 69 73  he library.** is
2b14c 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53   compiled with S
2b14d 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
2b14e 54 33 20 64 65 66 69 6e 65 64 2c 20 69 74 20 69  T3 defined, it i
2b14f 73 20 63 72 65 61 74 65 64 2e 20 0a 2a 2a 0a 2a  s created. .**.*
2b150 2a 20 41 72 67 75 6d 65 6e 74 20 7a 57 68 65 72  * Argument zWher
2b151 65 20 6d 61 79 20 62 65 20 61 20 70 6f 69 6e 74  e may be a point
2b152 65 72 20 74 6f 20 61 20 62 75 66 66 65 72 20 63  er to a buffer c
2b153 6f 6e 74 61 69 6e 69 6e 67 20 61 20 74 61 62 6c  ontaining a tabl
2b154 65 20 6e 61 6d 65 2c 0a 2a 2a 20 6f 72 20 69 74  e name,.** or it
2b155 20 6d 61 79 20 62 65 20 61 20 4e 55 4c 4c 20 70   may be a NULL p
2b156 6f 69 6e 74 65 72 2e 20 49 66 20 69 74 20 69 73  ointer. If it is
2b157 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
2b158 61 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 0a 2a  all entries in.*
2b159 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  * the sqlite_sta
2b15a 74 31 20 61 6e 64 20 28 69 66 20 61 70 70 6c 69  t1 and (if appli
2b15b 63 61 62 6c 65 29 20 73 71 6c 69 74 65 5f 73 74  cable) sqlite_st
2b15c 61 74 33 20 74 61 62 6c 65 73 20 61 73 73 6f 63  at3 tables assoc
2b15d 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68  iated.** with th
2b15e 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 20 61 72  e named table ar
2b15f 65 20 64 65 6c 65 74 65 64 2e 20 49 66 20 7a 57  e deleted. If zW
2b160 68 65 72 65 3d 3d 30 2c 20 74 68 65 6e 20 63 6f  here==0, then co
2b161 64 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 0a  de is generated.
2b162 2a 2a 20 74 6f 20 64 65 6c 65 74 65 20 61 6c 6c  ** to delete all
2b163 20 73 74 61 74 20 74 61 62 6c 65 20 65 6e 74 72   stat table entr
2b164 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ies..*/.static v
2b165 6f 69 64 20 6f 70 65 6e 53 74 61 74 54 61 62 6c  oid openStatTabl
2b166 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
2b167 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
2b168 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
2b169 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20  */.  int iDb,   
2b16a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2b16b 54 68 65 20 64 61 74 61 62 61 73 65 20 77 65 20  The database we 
2b16c 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 69 6e 20 2a  are looking in *
2b16d 2f 0a 20 20 69 6e 74 20 69 53 74 61 74 43 75 72  /.  int iStatCur
2b16e 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  ,           /* O
2b16f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 73  pen the sqlite_s
2b170 74 61 74 31 20 74 61 62 6c 65 20 6f 6e 20 74 68  tat1 table on th
2b171 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63  is cursor */.  c
2b172 6f 6e 73 74 20 63 68 61 72 20 2a 7a 57 68 65 72  onst char *zWher
2b173 65 2c 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65  e,     /* Delete
2b174 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68 69   entries for thi
2b175 73 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  s table or index
2b176 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2b177 20 2a 7a 57 68 65 72 65 54 79 70 65 20 20 2f 2a   *zWhereType  /*
2b178 20 45 69 74 68 65 72 20 22 74 62 6c 22 20 6f 72   Either "tbl" or
2b179 20 22 69 64 78 22 20 2a 2f 0a 29 7b 0a 20 20 73   "idx" */.){.  s
2b17a 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75  tatic const stru
2b17b 63 74 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ct {.    const c
2b17c 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20  har *zName;.    
2b17d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
2b17e 73 3b 0a 20 20 7d 20 61 54 61 62 6c 65 5b 5d 20  s;.  } aTable[] 
2b17f 3d 20 7b 0a 20 20 20 20 7b 20 22 73 71 6c 69 74  = {.    { "sqlit
2b180 65 5f 73 74 61 74 31 22 2c 20 22 74 62 6c 2c 69  e_stat1", "tbl,i
2b181 64 78 2c 73 74 61 74 22 20 7d 2c 0a 23 69 66 64  dx,stat" },.#ifd
2b182 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
2b183 5f 53 54 41 54 33 0a 20 20 20 20 7b 20 22 73 71  _STAT3.    { "sq
2b184 6c 69 74 65 5f 73 74 61 74 33 22 2c 20 22 74 62  lite_stat3", "tb
2b185 6c 2c 69 64 78 2c 6e 65 71 2c 6e 6c 74 2c 6e 64  l,idx,neq,nlt,nd
2b186 6c 74 2c 73 61 6d 70 6c 65 22 20 7d 2c 0a 23 65  lt,sample" },.#e
2b187 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74  ndif.  };..  int
2b188 20 61 52 6f 6f 74 5b 5d 20 3d 20 7b 30 2c 20 30   aRoot[] = {0, 0
2b189 7d 3b 0a 20 20 75 38 20 61 43 72 65 61 74 65 54  };.  u8 aCreateT
2b18a 62 6c 5b 5d 20 3d 20 7b 30 2c 20 30 7d 3b 0a 0a  bl[] = {0, 0};..
2b18b 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74    int i;.  sqlit
2b18c 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2b18d 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a  >db;.  Db *pDb;.
2b18e 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69    Vdbe *v = sqli
2b18f 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2b190 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  e);.  if( v==0 )
2b191 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
2b192 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
2b193 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64  oldsAllMutexes(d
2b194 62 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  b) );.  assert( 
2b195 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
2b196 3d 3d 64 62 20 29 3b 0a 20 20 70 44 62 20 3d 20  ==db );.  pDb = 
2b197 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a  &db->aDb[iDb];..
2b198 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20    /* Create new 
2b199 73 74 61 74 69 73 74 69 63 20 74 61 62 6c 65 73  statistic tables
2b19a 20 69 66 20 74 68 65 79 20 64 6f 20 6e 6f 74 20   if they do not 
2b19b 65 78 69 73 74 2c 20 6f 72 20 63 6c 65 61 72 20  exist, or clear 
2b19c 74 68 65 6d 0a 20 20 2a 2a 20 69 66 20 74 68 65  them.  ** if the
2b19d 79 20 64 6f 20 61 6c 72 65 61 64 79 20 65 78 69  y do already exi
2b19e 73 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  st..  */.  for(i
2b19f 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  =0; i<ArraySize(
2b1a0 61 54 61 62 6c 65 29 3b 20 69 2b 2b 29 7b 0a 20  aTable); i++){. 
2b1a1 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
2b1a2 54 61 62 20 3d 20 61 54 61 62 6c 65 5b 69 5d 2e  Tab = aTable[i].
2b1a3 7a 4e 61 6d 65 3b 0a 20 20 20 20 54 61 62 6c 65  zName;.    Table
2b1a4 20 2a 70 53 74 61 74 3b 0a 20 20 20 20 69 66 28   *pStat;.    if(
2b1a5 20 28 70 53 74 61 74 20 3d 20 73 71 6c 69 74 65   (pStat = sqlite
2b1a6 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
2b1a7 54 61 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29  Tab, pDb->zName)
2b1a8 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  )==0 ){.      /*
2b1a9 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   The sqlite_stat
2b1aa 5b 31 32 5d 20 74 61 62 6c 65 20 64 6f 65 73 20  [12] table does 
2b1ab 6e 6f 74 20 65 78 69 73 74 2e 20 43 72 65 61 74  not exist. Creat
2b1ac 65 20 69 74 2e 20 4e 6f 74 65 20 74 68 61 74 20  e it. Note that 
2b1ad 61 20 0a 20 20 20 20 20 20 2a 2a 20 73 69 64 65  a .      ** side
2b1ae 2d 65 66 66 65 63 74 20 6f 66 20 74 68 65 20 43  -effect of the C
2b1af 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
2b1b0 65 6d 65 6e 74 20 69 73 20 74 6f 20 6c 65 61 76  ement is to leav
2b1b1 65 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20 0a  e the rootpage .
2b1b2 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20        ** of the 
2b1b3 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 72 65 67  new table in reg
2b1b4 69 73 74 65 72 20 70 50 61 72 73 65 2d 3e 72 65  ister pParse->re
2b1b5 67 52 6f 6f 74 2e 20 54 68 69 73 20 69 73 20 69  gRoot. This is i
2b1b6 6d 70 6f 72 74 61 6e 74 20 0a 20 20 20 20 20 20  mportant .      
2b1b7 2a 2a 20 62 65 63 61 75 73 65 20 74 68 65 20 4f  ** because the O
2b1b8 70 65 6e 57 72 69 74 65 20 6f 70 63 6f 64 65 20  penWrite opcode 
2b1b9 62 65 6c 6f 77 20 77 69 6c 6c 20 62 65 20 6e 65  below will be ne
2b1ba 65 64 69 6e 67 20 69 74 2e 20 2a 2f 0a 20 20 20  eding it. */.   
2b1bb 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
2b1bc 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
2b1bd 20 20 20 20 20 20 20 20 22 43 52 45 41 54 45 20          "CREATE 
2b1be 54 41 42 4c 45 20 25 51 2e 25 73 28 25 73 29 22  TABLE %Q.%s(%s)"
2b1bf 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 54  , pDb->zName, zT
2b1c0 61 62 2c 20 61 54 61 62 6c 65 5b 69 5d 2e 7a 43  ab, aTable[i].zC
2b1c1 6f 6c 73 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  ols.      );.   
2b1c2 20 20 20 61 52 6f 6f 74 5b 69 5d 20 3d 20 70 50     aRoot[i] = pP
2b1c3 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 3b 0a 20  arse->regRoot;. 
2b1c4 20 20 20 20 20 61 43 72 65 61 74 65 54 62 6c 5b       aCreateTbl[
2b1c5 69 5d 20 3d 20 4f 50 46 4c 41 47 5f 50 32 49 53  i] = OPFLAG_P2IS
2b1c6 52 45 47 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  REG;.    }else{.
2b1c7 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
2b1c8 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  le already exist
2b1c9 73 2e 20 49 66 20 7a 57 68 65 72 65 20 69 73 20  s. If zWhere is 
2b1ca 6e 6f 74 20 4e 55 4c 4c 2c 20 64 65 6c 65 74 65  not NULL, delete
2b1cb 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 0a 20 20   all entries .  
2b1cc 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65      ** associate
2b1cd 64 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65  d with the table
2b1ce 20 7a 57 68 65 72 65 2e 20 49 66 20 7a 57 68 65   zWhere. If zWhe
2b1cf 72 65 20 69 73 20 4e 55 4c 4c 2c 20 64 65 6c 65  re is NULL, dele
2b1d0 74 65 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  te the.      ** 
2b1d1 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20  entire contents 
2b1d2 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 2a 2f  of the table. */
2b1d3 0a 20 20 20 20 20 20 61 52 6f 6f 74 5b 69 5d 20  .      aRoot[i] 
2b1d4 3d 20 70 53 74 61 74 2d 3e 74 6e 75 6d 3b 0a 20  = pStat->tnum;. 
2b1d5 20 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c       sqlite3Tabl
2b1d6 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44  eLock(pParse, iD
2b1d7 62 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 31 2c 20  b, aRoot[i], 1, 
2b1d8 7a 54 61 62 29 3b 0a 20 20 20 20 20 20 69 66 28  zTab);.      if(
2b1d9 20 7a 57 68 65 72 65 20 29 7b 0a 20 20 20 20 20   zWhere ){.     
2b1da 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
2b1db 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
2b1dc 20 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 45           "DELETE
2b1dd 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 52   FROM %Q.%s WHER
2b1de 45 20 25 73 3d 25 51 22 2c 20 70 44 62 2d 3e 7a  E %s=%Q", pDb->z
2b1df 4e 61 6d 65 2c 20 7a 54 61 62 2c 20 7a 57 68 65  Name, zTab, zWhe
2b1e0 72 65 54 79 70 65 2c 20 7a 57 68 65 72 65 0a 20  reType, zWhere. 
2b1e1 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
2b1e2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
2b1e3 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61  * The sqlite_sta
2b1e4 74 5b 31 32 5d 20 74 61 62 6c 65 20 61 6c 72 65  t[12] table alre
2b1e5 61 64 79 20 65 78 69 73 74 73 2e 20 20 44 65 6c  ady exists.  Del
2b1e6 65 74 65 20 61 6c 6c 20 72 6f 77 73 2e 20 2a 2f  ete all rows. */
2b1e7 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2b1e8 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b1e9 5f 43 6c 65 61 72 2c 20 61 52 6f 6f 74 5b 69 5d  _Clear, aRoot[i]
2b1ea 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 7d 0a  , iDb);.      }.
2b1eb 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
2b1ec 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 5f  Open the sqlite_
2b1ed 73 74 61 74 5b 31 33 5d 20 74 61 62 6c 65 73 20  stat[13] tables 
2b1ee 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a  for writing. */.
2b1ef 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72    for(i=0; i<Arr
2b1f0 61 79 53 69 7a 65 28 61 54 61 62 6c 65 29 3b 20  aySize(aTable); 
2b1f1 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
2b1f2 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2b1f3 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 53 74  P_OpenWrite, iSt
2b1f4 61 74 43 75 72 2b 69 2c 20 61 52 6f 6f 74 5b 69  atCur+i, aRoot[i
2b1f5 5d 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  ], iDb);.    sql
2b1f6 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
2b1f7 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29  (v, -1, (char *)
2b1f8 33 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20  3, P4_INT32);.  
2b1f9 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2b1fa 6e 67 65 50 35 28 76 2c 20 61 43 72 65 61 74 65  ngeP5(v, aCreate
2b1fb 54 62 6c 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a  Tbl[i]);.  }.}..
2b1fc 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 6d 65 6e 64 65  /*.** Recommende
2b1fd 64 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 6d 70  d number of samp
2b1fe 6c 65 73 20 66 6f 72 20 73 71 6c 69 74 65 5f 73  les for sqlite_s
2b1ff 74 61 74 33 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  tat3.*/.#ifndef 
2b200 53 51 4c 49 54 45 5f 53 54 41 54 33 5f 53 41 4d  SQLITE_STAT3_SAM
2b201 50 4c 45 53 0a 23 20 64 65 66 69 6e 65 20 53 51  PLES.# define SQ
2b202 4c 49 54 45 5f 53 54 41 54 33 5f 53 41 4d 50 4c  LITE_STAT3_SAMPL
2b203 45 53 20 32 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ES 24.#endif../*
2b204 0a 2a 2a 20 54 68 72 65 65 20 53 51 4c 20 66 75  .** Three SQL fu
2b205 6e 63 74 69 6f 6e 73 20 2d 20 73 74 61 74 33 5f  nctions - stat3_
2b206 69 6e 69 74 28 29 2c 20 73 74 61 74 33 5f 70 75  init(), stat3_pu
2b207 73 68 28 29 2c 20 61 6e 64 20 73 74 61 74 33 5f  sh(), and stat3_
2b208 70 6f 70 28 29 20 2d 0a 2a 2a 20 73 68 61 72 65  pop() -.** share
2b209 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
2b20a 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
2b20b 72 75 63 74 75 72 65 20 74 6f 20 68 6f 6c 64 20  ructure to hold 
2b20c 74 68 65 69 72 20 73 74 61 74 65 0a 2a 2a 20 69  their state.** i
2b20d 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74  nformation..*/.t
2b20e 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 74  ypedef struct St
2b20f 61 74 33 41 63 63 75 6d 20 53 74 61 74 33 41 63  at3Accum Stat3Ac
2b210 63 75 6d 3b 0a 73 74 72 75 63 74 20 53 74 61 74  cum;.struct Stat
2b211 33 41 63 63 75 6d 20 7b 0a 20 20 74 52 6f 77 63  3Accum {.  tRowc
2b212 6e 74 20 6e 52 6f 77 3b 20 20 20 20 20 20 20 20  nt nRow;        
2b213 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2b214 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 65 6e  f rows in the en
2b215 74 69 72 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tire table */.  
2b216 74 52 6f 77 63 6e 74 20 6e 50 53 61 6d 70 6c 65  tRowcnt nPSample
2b217 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77  ;         /* How
2b218 20 6f 66 74 65 6e 20 74 6f 20 64 6f 20 61 20 70   often to do a p
2b219 65 72 69 6f 64 69 63 20 73 61 6d 70 6c 65 20 2a  eriodic sample *
2b21a 2f 0a 20 20 69 6e 74 20 69 4d 69 6e 3b 20 20 20  /.  int iMin;   
2b21b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b21c 20 49 6e 64 65 78 20 6f 66 20 65 6e 74 72 79 20   Index of entry 
2b21d 77 69 74 68 20 6d 69 6e 69 6d 75 6d 20 6e 45 71  with minimum nEq
2b21e 20 61 6e 64 20 68 61 73 68 20 2a 2f 0a 20 20 69   and hash */.  i
2b21f 6e 74 20 6d 78 53 61 6d 70 6c 65 3b 20 20 20 20  nt mxSample;    
2b220 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
2b221 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 73 61  mum number of sa
2b222 6d 70 6c 65 73 20 74 6f 20 61 63 63 75 6d 75 6c  mples to accumul
2b223 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 61  ate */.  int nSa
2b224 6d 70 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20  mple;           
2b225 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75     /* Current nu
2b226 6d 62 65 72 20 6f 66 20 73 61 6d 70 6c 65 73 20  mber of samples 
2b227 2a 2f 0a 20 20 75 33 32 20 69 50 72 6e 3b 20 20  */.  u32 iPrn;  
2b228 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2b229 2a 20 50 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20  * Pseudo-random 
2b22a 6e 75 6d 62 65 72 20 75 73 65 64 20 66 6f 72 20  number used for 
2b22b 73 61 6d 70 6c 69 6e 67 20 2a 2f 0a 20 20 73 74  sampling */.  st
2b22c 72 75 63 74 20 53 74 61 74 33 53 61 6d 70 6c 65  ruct Stat3Sample
2b22d 20 7b 0a 20 20 20 20 69 36 34 20 69 52 6f 77 69   {.    i64 iRowi
2b22e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
2b22f 20 20 2f 2a 20 52 6f 77 69 64 20 69 6e 20 6d 61    /* Rowid in ma
2b230 69 6e 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20  in table of the 
2b231 6b 65 79 20 2a 2f 0a 20 20 20 20 74 52 6f 77 63  key */.    tRowc
2b232 6e 74 20 6e 45 71 3b 20 20 20 20 20 20 20 20 20  nt nEq;         
2b233 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 5f        /* sqlite_
2b234 73 74 61 74 33 2e 6e 45 71 20 2a 2f 0a 20 20 20  stat3.nEq */.   
2b235 20 74 52 6f 77 63 6e 74 20 6e 4c 74 3b 20 20 20   tRowcnt nLt;   
2b236 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
2b237 71 6c 69 74 65 5f 73 74 61 74 33 2e 6e 4c 74 20  qlite_stat3.nLt 
2b238 2a 2f 0a 20 20 20 20 74 52 6f 77 63 6e 74 20 6e  */.    tRowcnt n
2b239 44 4c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  DLt;            
2b23a 20 20 2f 2a 20 73 71 6c 69 74 65 5f 73 74 61 74    /* sqlite_stat
2b23b 33 2e 6e 44 4c 74 20 2a 2f 0a 20 20 20 20 75 38  3.nDLt */.    u8
2b23c 20 69 73 50 53 61 6d 70 6c 65 3b 20 20 20 20 20   isPSample;     
2b23d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2b23e 20 69 66 20 61 20 70 65 72 69 6f 64 69 63 20 73   if a periodic s
2b23f 61 6d 70 6c 65 20 2a 2f 0a 20 20 20 20 75 33 32  ample */.    u32
2b240 20 69 48 61 73 68 3b 20 20 20 20 20 20 20 20 20   iHash;         
2b241 20 20 20 20 20 20 20 20 2f 2a 20 54 69 65 62 72          /* Tiebr
2b242 65 61 6b 65 72 20 68 61 73 68 20 2a 2f 0a 20 20  eaker hash */.  
2b243 7d 20 2a 61 3b 20 20 20 20 20 20 20 20 20 20 20  } *a;           
2b244 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20            /* An 
2b245 61 72 72 61 79 20 6f 66 20 73 61 6d 70 6c 65 73  array of samples
2b246 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53   */.};..#ifdef S
2b247 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
2b248 54 33 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  T3./*.** Impleme
2b249 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
2b24a 74 61 74 33 5f 69 6e 69 74 28 43 2c 53 29 20 53  tat3_init(C,S) S
2b24b 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  QL function.  Th
2b24c 65 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72 73  e two parameters
2b24d 0a 2a 2a 20 61 72 65 20 74 68 65 20 6e 75 6d 62  .** are the numb
2b24e 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68  er of rows in th
2b24f 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  e table or index
2b250 20 28 43 29 20 61 6e 64 20 74 68 65 20 6e 75 6d   (C) and the num
2b251 62 65 72 20 6f 66 20 73 61 6d 70 6c 65 73 0a 2a  ber of samples.*
2b252 2a 20 74 6f 20 61 63 63 75 6d 75 6c 61 74 65 20  * to accumulate 
2b253 28 53 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  (S)..**.** This 
2b254 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65  routine allocate
2b255 73 20 74 68 65 20 53 74 61 74 33 41 63 63 75 6d  s the Stat3Accum
2b256 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54   object..**.** T
2b257 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
2b258 69 73 20 74 68 65 20 53 74 61 74 33 41 63 63 75  is the Stat3Accu
2b259 6d 20 6f 62 6a 65 63 74 20 28 50 29 2e 0a 2a 2f  m object (P)..*/
2b25a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 61  .static void sta
2b25b 74 33 49 6e 69 74 28 0a 20 20 73 71 6c 69 74 65  t3Init(.  sqlite
2b25c 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
2b25d 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
2b25e 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
2b25f 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 53 74 61 74  **argv.){.  Stat
2b260 33 41 63 63 75 6d 20 2a 70 3b 0a 20 20 74 52 6f  3Accum *p;.  tRo
2b261 77 63 6e 74 20 6e 52 6f 77 3b 0a 20 20 69 6e 74  wcnt nRow;.  int
2b262 20 6d 78 53 61 6d 70 6c 65 3b 0a 20 20 69 6e 74   mxSample;.  int
2b263 20 6e 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41   n;..  UNUSED_PA
2b264 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20  RAMETER(argc);. 
2b265 20 6e 52 6f 77 20 3d 20 28 74 52 6f 77 63 6e 74   nRow = (tRowcnt
2b266 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69  )sqlite3_value_i
2b267 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20  nt64(argv[0]);. 
2b268 20 6d 78 53 61 6d 70 6c 65 20 3d 20 73 71 6c 69   mxSample = sqli
2b269 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72  te3_value_int(ar
2b26a 67 76 5b 31 5d 29 3b 0a 20 20 6e 20 3d 20 73 69  gv[1]);.  n = si
2b26b 7a 65 6f 66 28 2a 70 29 20 2b 20 73 69 7a 65 6f  zeof(*p) + sizeo
2b26c 66 28 70 2d 3e 61 5b 30 5d 29 2a 6d 78 53 61 6d  f(p->a[0])*mxSam
2b26d 70 6c 65 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74  ple;.  p = sqlit
2b26e 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 6e 20  e3MallocZero( n 
2b26f 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b  );.  if( p==0 ){
2b270 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
2b271 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
2b272 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65  context);.    re
2b273 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 2d 3e 61  turn;.  }.  p->a
2b274 20 3d 20 28 73 74 72 75 63 74 20 53 74 61 74 33   = (struct Stat3
2b275 53 61 6d 70 6c 65 2a 29 26 70 5b 31 5d 3b 0a 20  Sample*)&p[1];. 
2b276 20 70 2d 3e 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b   p->nRow = nRow;
2b277 0a 20 20 70 2d 3e 6d 78 53 61 6d 70 6c 65 20 3d  .  p->mxSample =
2b278 20 6d 78 53 61 6d 70 6c 65 3b 0a 20 20 70 2d 3e   mxSample;.  p->
2b279 6e 50 53 61 6d 70 6c 65 20 3d 20 70 2d 3e 6e 52  nPSample = p->nR
2b27a 6f 77 2f 28 6d 78 53 61 6d 70 6c 65 2f 33 2b 31  ow/(mxSample/3+1
2b27b 29 20 2b 20 31 3b 0a 20 20 73 71 6c 69 74 65 33  ) + 1;.  sqlite3
2b27c 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65  _randomness(size
2b27d 6f 66 28 70 2d 3e 69 50 72 6e 29 2c 20 26 70 2d  of(p->iPrn), &p-
2b27e 3e 69 50 72 6e 29 3b 0a 20 20 73 71 6c 69 74 65  >iPrn);.  sqlite
2b27f 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 63 6f  3_result_blob(co
2b280 6e 74 65 78 74 2c 20 70 2c 20 73 69 7a 65 6f 66  ntext, p, sizeof
2b281 28 70 29 2c 20 73 71 6c 69 74 65 33 5f 66 72 65  (p), sqlite3_fre
2b282 65 29 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e  e);.}.static con
2b283 73 74 20 46 75 6e 63 44 65 66 20 73 74 61 74 33  st FuncDef stat3
2b284 49 6e 69 74 46 75 6e 63 64 65 66 20 3d 20 7b 0a  InitFuncdef = {.
2b285 20 20 32 2c 20 20 20 20 20 20 20 20 20 20 20 20    2,            
2b286 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f 0a 20      /* nArg */. 
2b287 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20   SQLITE_UTF8,   
2b288 20 20 20 2f 2a 20 69 50 72 65 66 45 6e 63 20 2a     /* iPrefEnc *
2b289 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
2b28a 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 2a        /* flags *
2b28b 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
2b28c 20 20 20 20 20 20 2f 2a 20 70 55 73 65 72 44 61        /* pUserDa
2b28d 74 61 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  ta */.  0,      
2b28e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65            /* pNe
2b28f 78 74 20 2a 2f 0a 20 20 73 74 61 74 33 49 6e 69  xt */.  stat3Ini
2b290 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 46 75  t,        /* xFu
2b291 6e 63 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  nc */.  0,      
2b292 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 74            /* xSt
2b293 65 70 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  ep */.  0,      
2b294 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
2b295 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 22 73 74 61  nalize */.  "sta
2b296 74 33 5f 69 6e 69 74 22 2c 20 20 20 20 20 2f 2a  t3_init",     /*
2b297 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 30 2c 20 20   zName */.  0,  
2b298 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b299 20 70 48 61 73 68 20 2a 2f 0a 20 20 30 20 20 20   pHash */.  0   
2b29a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b29b 20 70 44 65 73 74 72 75 63 74 6f 72 20 2a 2f 0a   pDestructor */.
2b29c 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  };.../*.** Imple
2b29d 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
2b29e 20 73 74 61 74 33 5f 70 75 73 68 28 6e 45 71 2c   stat3_push(nEq,
2b29f 6e 4c 74 2c 6e 44 4c 74 2c 72 6f 77 69 64 2c 50  nLt,nDLt,rowid,P
2b2a0 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20  ) SQL function. 
2b2a1 20 54 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74   The.** argument
2b2a2 73 20 64 65 73 63 72 69 62 65 20 61 20 73 69 6e  s describe a sin
2b2a3 67 6c 65 20 6b 65 79 20 69 6e 73 74 61 6e 63 65  gle key instance
2b2a4 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
2b2a5 6d 61 6b 65 73 20 74 68 65 20 0a 2a 2a 20 64 65  makes the .** de
2b2a6 63 69 73 69 6f 6e 20 61 62 6f 75 74 20 77 68 65  cision about whe
2b2a7 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 72  ther or not to r
2b2a8 65 74 61 69 6e 20 74 68 69 73 20 6b 65 79 20 66  etain this key f
2b2a9 6f 72 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  or the sqlite_st
2b2aa 61 74 33 0a 2a 2a 20 74 61 62 6c 65 2e 0a 2a 2a  at3.** table..**
2b2ab 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76  .** The return v
2b2ac 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  alue is NULL..*/
2b2ad 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 61  .static void sta
2b2ae 74 33 50 75 73 68 28 0a 20 20 73 71 6c 69 74 65  t3Push(.  sqlite
2b2af 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
2b2b0 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
2b2b1 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
2b2b2 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 53 74 61 74  **argv.){.  Stat
2b2b3 33 41 63 63 75 6d 20 2a 70 20 3d 20 28 53 74 61  3Accum *p = (Sta
2b2b4 74 33 41 63 63 75 6d 2a 29 73 71 6c 69 74 65 33  t3Accum*)sqlite3
2b2b5 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76  _value_blob(argv
2b2b6 5b 34 5d 29 3b 0a 20 20 74 52 6f 77 63 6e 74 20  [4]);.  tRowcnt 
2b2b7 6e 45 71 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  nEq = sqlite3_va
2b2b8 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30  lue_int64(argv[0
2b2b9 5d 29 3b 0a 20 20 74 52 6f 77 63 6e 74 20 6e 4c  ]);.  tRowcnt nL
2b2ba 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  t = sqlite3_valu
2b2bb 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 31 5d 29  e_int64(argv[1])
2b2bc 3b 0a 20 20 74 52 6f 77 63 6e 74 20 6e 44 4c 74  ;.  tRowcnt nDLt
2b2bd 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
2b2be 5f 69 6e 74 36 34 28 61 72 67 76 5b 32 5d 29 3b  _int64(argv[2]);
2b2bf 0a 20 20 69 36 34 20 72 6f 77 69 64 20 3d 20 73  .  i64 rowid = s
2b2c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
2b2c1 36 34 28 61 72 67 76 5b 33 5d 29 3b 0a 20 20 75  64(argv[3]);.  u
2b2c2 38 20 69 73 50 53 61 6d 70 6c 65 20 3d 20 30 3b  8 isPSample = 0;
2b2c3 0a 20 20 75 38 20 64 6f 49 6e 73 65 72 74 20 3d  .  u8 doInsert =
2b2c4 20 30 3b 0a 20 20 69 6e 74 20 69 4d 69 6e 20 3d   0;.  int iMin =
2b2c5 20 70 2d 3e 69 4d 69 6e 3b 0a 20 20 73 74 72 75   p->iMin;.  stru
2b2c6 63 74 20 53 74 61 74 33 53 61 6d 70 6c 65 20 2a  ct Stat3Sample *
2b2c7 70 53 61 6d 70 6c 65 3b 0a 20 20 69 6e 74 20 69  pSample;.  int i
2b2c8 3b 0a 20 20 75 33 32 20 68 3b 0a 0a 20 20 55 4e  ;.  u32 h;..  UN
2b2c9 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 63  USED_PARAMETER(c
2b2ca 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45  ontext);.  UNUSE
2b2cb 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
2b2cc 29 3b 0a 20 20 69 66 28 20 6e 45 71 3d 3d 30 20  );.  if( nEq==0 
2b2cd 29 20 72 65 74 75 72 6e 3b 0a 20 20 68 20 3d 20  ) return;.  h = 
2b2ce 70 2d 3e 69 50 72 6e 20 3d 20 70 2d 3e 69 50 72  p->iPrn = p->iPr
2b2cf 6e 2a 31 31 30 33 35 31 35 32 34 35 20 2b 20 31  n*1103515245 + 1
2b2d0 32 33 34 35 3b 0a 20 20 69 66 28 20 28 6e 4c 74  2345;.  if( (nLt
2b2d1 2f 70 2d 3e 6e 50 53 61 6d 70 6c 65 29 21 3d 28  /p->nPSample)!=(
2b2d2 28 6e 45 71 2b 6e 4c 74 29 2f 70 2d 3e 6e 50 53  (nEq+nLt)/p->nPS
2b2d3 61 6d 70 6c 65 29 20 29 7b 0a 20 20 20 20 64 6f  ample) ){.    do
2b2d4 49 6e 73 65 72 74 20 3d 20 69 73 50 53 61 6d 70  Insert = isPSamp
2b2d5 6c 65 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20  le = 1;.  }else 
2b2d6 69 66 28 20 70 2d 3e 6e 53 61 6d 70 6c 65 3c 70  if( p->nSample<p
2b2d7 2d 3e 6d 78 53 61 6d 70 6c 65 20 29 7b 0a 20 20  ->mxSample ){.  
2b2d8 20 20 64 6f 49 6e 73 65 72 74 20 3d 20 31 3b 0a    doInsert = 1;.
2b2d9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28    }else{.    if(
2b2da 20 6e 45 71 3e 70 2d 3e 61 5b 69 4d 69 6e 5d 2e   nEq>p->a[iMin].
2b2db 6e 45 71 20 7c 7c 20 28 6e 45 71 3d 3d 70 2d 3e  nEq || (nEq==p->
2b2dc 61 5b 69 4d 69 6e 5d 2e 6e 45 71 20 26 26 20 68  a[iMin].nEq && h
2b2dd 3e 70 2d 3e 61 5b 69 4d 69 6e 5d 2e 69 48 61 73  >p->a[iMin].iHas
2b2de 68 29 20 29 7b 0a 20 20 20 20 20 20 64 6f 49 6e  h) ){.      doIn
2b2df 73 65 72 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  sert = 1;.    }.
2b2e0 20 20 7d 0a 20 20 69 66 28 20 21 64 6f 49 6e 73    }.  if( !doIns
2b2e1 65 72 74 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ert ) return;.  
2b2e2 69 66 28 20 70 2d 3e 6e 53 61 6d 70 6c 65 3d 3d  if( p->nSample==
2b2e3 70 2d 3e 6d 78 53 61 6d 70 6c 65 20 29 7b 0a 20  p->mxSample ){. 
2b2e4 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 53     assert( p->nS
2b2e5 61 6d 70 6c 65 20 2d 20 69 4d 69 6e 20 2d 20 31  ample - iMin - 1
2b2e6 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 6d 65 6d   >= 0 );.    mem
2b2e7 6d 6f 76 65 28 26 70 2d 3e 61 5b 69 4d 69 6e 5d  move(&p->a[iMin]
2b2e8 2c 20 26 70 2d 3e 61 5b 69 4d 69 6e 2b 31 5d 2c  , &p->a[iMin+1],
2b2e9 20 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29   sizeof(p->a[0])
2b2ea 2a 28 70 2d 3e 6e 53 61 6d 70 6c 65 2d 69 4d 69  *(p->nSample-iMi
2b2eb 6e 2d 31 29 29 3b 0a 20 20 20 20 70 53 61 6d 70  n-1));.    pSamp
2b2ec 6c 65 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53  le = &p->a[p->nS
2b2ed 61 6d 70 6c 65 2d 31 5d 3b 0a 20 20 7d 65 6c 73  ample-1];.  }els
2b2ee 65 7b 0a 20 20 20 20 70 53 61 6d 70 6c 65 20 3d  e{.    pSample =
2b2ef 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53 61 6d 70 6c   &p->a[p->nSampl
2b2f0 65 2b 2b 5d 3b 0a 20 20 7d 0a 20 20 70 53 61 6d  e++];.  }.  pSam
2b2f1 70 6c 65 2d 3e 69 52 6f 77 69 64 20 3d 20 72 6f  ple->iRowid = ro
2b2f2 77 69 64 3b 0a 20 20 70 53 61 6d 70 6c 65 2d 3e  wid;.  pSample->
2b2f3 6e 45 71 20 3d 20 6e 45 71 3b 0a 20 20 70 53 61  nEq = nEq;.  pSa
2b2f4 6d 70 6c 65 2d 3e 6e 4c 74 20 3d 20 6e 4c 74 3b  mple->nLt = nLt;
2b2f5 0a 20 20 70 53 61 6d 70 6c 65 2d 3e 6e 44 4c 74  .  pSample->nDLt
2b2f6 20 3d 20 6e 44 4c 74 3b 0a 20 20 70 53 61 6d 70   = nDLt;.  pSamp
2b2f7 6c 65 2d 3e 69 48 61 73 68 20 3d 20 68 3b 0a 20  le->iHash = h;. 
2b2f8 20 70 53 61 6d 70 6c 65 2d 3e 69 73 50 53 61 6d   pSample->isPSam
2b2f9 70 6c 65 20 3d 20 69 73 50 53 61 6d 70 6c 65 3b  ple = isPSample;
2b2fa 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20  ..  /* Find the 
2b2fb 6e 65 77 20 6d 69 6e 69 6d 75 6d 20 2a 2f 0a 20  new minimum */. 
2b2fc 20 69 66 28 20 70 2d 3e 6e 53 61 6d 70 6c 65 3d   if( p->nSample=
2b2fd 3d 70 2d 3e 6d 78 53 61 6d 70 6c 65 20 29 7b 0a  =p->mxSample ){.
2b2fe 20 20 20 20 70 53 61 6d 70 6c 65 20 3d 20 70 2d      pSample = p-
2b2ff 3e 61 3b 0a 20 20 20 20 69 20 3d 20 30 3b 0a 20  >a;.    i = 0;. 
2b300 20 20 20 77 68 69 6c 65 28 20 70 53 61 6d 70 6c     while( pSampl
2b301 65 2d 3e 69 73 50 53 61 6d 70 6c 65 20 29 7b 0a  e->isPSample ){.
2b302 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
2b303 20 70 53 61 6d 70 6c 65 2b 2b 3b 0a 20 20 20 20   pSample++;.    
2b304 20 20 61 73 73 65 72 74 28 20 69 3c 70 2d 3e 6e    assert( i<p->n
2b305 53 61 6d 70 6c 65 20 29 3b 0a 20 20 20 20 7d 0a  Sample );.    }.
2b306 20 20 20 20 6e 45 71 20 3d 20 70 53 61 6d 70 6c      nEq = pSampl
2b307 65 2d 3e 6e 45 71 3b 0a 20 20 20 20 68 20 3d 20  e->nEq;.    h = 
2b308 70 53 61 6d 70 6c 65 2d 3e 69 48 61 73 68 3b 0a  pSample->iHash;.
2b309 20 20 20 20 69 4d 69 6e 20 3d 20 69 3b 0a 20 20      iMin = i;.  
2b30a 20 20 66 6f 72 28 69 2b 2b 2c 20 70 53 61 6d 70    for(i++, pSamp
2b30b 6c 65 2b 2b 3b 20 69 3c 70 2d 3e 6e 53 61 6d 70  le++; i<p->nSamp
2b30c 6c 65 3b 20 69 2b 2b 2c 20 70 53 61 6d 70 6c 65  le; i++, pSample
2b30d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
2b30e 53 61 6d 70 6c 65 2d 3e 69 73 50 53 61 6d 70 6c  Sample->isPSampl
2b30f 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  e ) continue;.  
2b310 20 20 20 20 69 66 28 20 70 53 61 6d 70 6c 65 2d      if( pSample-
2b311 3e 6e 45 71 3c 6e 45 71 0a 20 20 20 20 20 20 20  >nEq<nEq.       
2b312 7c 7c 20 28 70 53 61 6d 70 6c 65 2d 3e 6e 45 71  || (pSample->nEq
2b313 3d 3d 6e 45 71 20 26 26 20 70 53 61 6d 70 6c 65  ==nEq && pSample
2b314 2d 3e 69 48 61 73 68 3c 68 29 0a 20 20 20 20 20  ->iHash<h).     
2b315 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4d 69 6e   ){.        iMin
2b316 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 6e 45   = i;.        nE
2b317 71 20 3d 20 70 53 61 6d 70 6c 65 2d 3e 6e 45 71  q = pSample->nEq
2b318 3b 0a 20 20 20 20 20 20 20 20 68 20 3d 20 70 53  ;.        h = pS
2b319 61 6d 70 6c 65 2d 3e 69 48 61 73 68 3b 0a 20 20  ample->iHash;.  
2b31a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2b31b 70 2d 3e 69 4d 69 6e 20 3d 20 69 4d 69 6e 3b 0a  p->iMin = iMin;.
2b31c 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e    }.}.static con
2b31d 73 74 20 46 75 6e 63 44 65 66 20 73 74 61 74 33  st FuncDef stat3
2b31e 50 75 73 68 46 75 6e 63 64 65 66 20 3d 20 7b 0a  PushFuncdef = {.
2b31f 20 20 35 2c 20 20 20 20 20 20 20 20 20 20 20 20    5,            
2b320 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f 0a 20      /* nArg */. 
2b321 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20   SQLITE_UTF8,   
2b322 20 20 20 2f 2a 20 69 50 72 65 66 45 6e 63 20 2a     /* iPrefEnc *
2b323 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
2b324 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 2a        /* flags *
2b325 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
2b326 20 20 20 20 20 20 2f 2a 20 70 55 73 65 72 44 61        /* pUserDa
2b327 74 61 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  ta */.  0,      
2b328 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65            /* pNe
2b329 78 74 20 2a 2f 0a 20 20 73 74 61 74 33 50 75 73  xt */.  stat3Pus
2b32a 68 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 46 75  h,        /* xFu
2b32b 6e 63 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  nc */.  0,      
2b32c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 74            /* xSt
2b32d 65 70 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  ep */.  0,      
2b32e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
2b32f 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 22 73 74 61  nalize */.  "sta
2b330 74 33 5f 70 75 73 68 22 2c 20 20 20 20 20 2f 2a  t3_push",     /*
2b331 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 30 2c 20 20   zName */.  0,  
2b332 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b333 20 70 48 61 73 68 20 2a 2f 0a 20 20 30 20 20 20   pHash */.  0   
2b334 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b335 20 70 44 65 73 74 72 75 63 74 6f 72 20 2a 2f 0a   pDestructor */.
2b336 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  };../*.** Implem
2b337 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
2b338 73 74 61 74 33 5f 67 65 74 28 50 2c 4e 2c 2e 2e  stat3_get(P,N,..
2b339 2e 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e  .) SQL function.
2b33a 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
2b33b 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 71 75 65  s.** used to que
2b33c 72 79 20 74 68 65 20 72 65 73 75 6c 74 73 2e 20  ry the results. 
2b33d 20 43 6f 6e 74 65 6e 74 20 69 73 20 72 65 74 75   Content is retu
2b33e 72 6e 65 64 20 66 6f 72 20 74 68 65 20 4e 74 68  rned for the Nth
2b33f 20 73 71 6c 69 74 65 5f 73 74 61 74 33 0a 2a 2a   sqlite_stat3.**
2b340 20 72 6f 77 20 77 68 65 72 65 20 4e 20 69 73 20   row where N is 
2b341 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 53 2d  between 0 and S-
2b342 31 20 61 6e 64 20 53 20 69 73 20 74 68 65 20 6e  1 and S is the n
2b343 75 6d 62 65 72 20 6f 66 20 73 61 6d 70 6c 65 73  umber of samples
2b344 2e 20 20 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20  .  The.** value 
2b345 72 65 74 75 72 6e 65 64 20 64 65 70 65 6e 64 73  returned depends
2b346 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   on the number o
2b347 66 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a  f arguments..**.
2b348 2a 2a 20 20 20 61 72 67 63 3d 3d 32 20 20 20 20  **   argc==2    
2b349 72 65 73 75 6c 74 3a 20 20 72 6f 77 69 64 0a 2a  result:  rowid.*
2b34a 2a 20 20 20 61 72 67 63 3d 3d 33 20 20 20 20 72  *   argc==3    r
2b34b 65 73 75 6c 74 3a 20 20 6e 45 71 0a 2a 2a 20 20  esult:  nEq.**  
2b34c 20 61 72 67 63 3d 3d 34 20 20 20 20 72 65 73 75   argc==4    resu
2b34d 6c 74 3a 20 20 6e 4c 74 0a 2a 2a 20 20 20 61 72  lt:  nLt.**   ar
2b34e 67 63 3d 3d 35 20 20 20 20 72 65 73 75 6c 74 3a  gc==5    result:
2b34f 20 20 6e 44 4c 74 0a 2a 2f 0a 73 74 61 74 69 63    nDLt.*/.static
2b350 20 76 6f 69 64 20 73 74 61 74 33 47 65 74 28 0a   void stat3Get(.
2b351 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
2b352 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
2b353 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
2b354 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
2b355 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69  {.  int n = sqli
2b356 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72  te3_value_int(ar
2b357 67 76 5b 31 5d 29 3b 0a 20 20 53 74 61 74 33 41  gv[1]);.  Stat3A
2b358 63 63 75 6d 20 2a 70 20 3d 20 28 53 74 61 74 33  ccum *p = (Stat3
2b359 41 63 63 75 6d 2a 29 73 71 6c 69 74 65 33 5f 76  Accum*)sqlite3_v
2b35a 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30  alue_blob(argv[0
2b35b 5d 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  ]);..  assert( p
2b35c 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  !=0 );.  if( p->
2b35d 6e 53 61 6d 70 6c 65 3c 3d 6e 20 29 20 72 65 74  nSample<=n ) ret
2b35e 75 72 6e 3b 0a 20 20 73 77 69 74 63 68 28 20 61  urn;.  switch( a
2b35f 72 67 63 20 29 7b 0a 20 20 20 20 63 61 73 65 20  rgc ){.    case 
2b360 32 3a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  2:  sqlite3_resu
2b361 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74  lt_int64(context
2b362 2c 20 70 2d 3e 61 5b 6e 5d 2e 69 52 6f 77 69 64  , p->a[n].iRowid
2b363 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61  ); break;.    ca
2b364 73 65 20 33 3a 20 20 73 71 6c 69 74 65 33 5f 72  se 3:  sqlite3_r
2b365 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74  esult_int64(cont
2b366 65 78 74 2c 20 70 2d 3e 61 5b 6e 5d 2e 6e 45 71  ext, p->a[n].nEq
2b367 29 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  );    break;.   
2b368 20 63 61 73 65 20 34 3a 20 20 73 71 6c 69 74 65   case 4:  sqlite
2b369 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63  3_result_int64(c
2b36a 6f 6e 74 65 78 74 2c 20 70 2d 3e 61 5b 6e 5d 2e  ontext, p->a[n].
2b36b 6e 4c 74 29 3b 20 20 20 20 62 72 65 61 6b 3b 0a  nLt);    break;.
2b36c 20 20 20 20 64 65 66 61 75 6c 74 3a 20 73 71 6c      default: sql
2b36d 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
2b36e 34 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 61 5b  4(context, p->a[
2b36f 6e 5d 2e 6e 44 4c 74 29 3b 20 20 20 62 72 65 61  n].nDLt);   brea
2b370 6b 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20  k;.  }.}.static 
2b371 63 6f 6e 73 74 20 46 75 6e 63 44 65 66 20 73 74  const FuncDef st
2b372 61 74 33 47 65 74 46 75 6e 63 64 65 66 20 3d 20  at3GetFuncdef = 
2b373 7b 0a 20 20 2d 31 2c 20 20 20 20 20 20 20 20 20  {.  -1,         
2b374 20 20 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f        /* nArg */
2b375 0a 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  .  SQLITE_UTF8, 
2b376 20 20 20 20 20 2f 2a 20 69 50 72 65 66 45 6e 63       /* iPrefEnc
2b377 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
2b378 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73          /* flags
2b379 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
2b37a 20 20 20 20 20 20 20 20 2f 2a 20 70 55 73 65 72          /* pUser
2b37b 44 61 74 61 20 2a 2f 0a 20 20 30 2c 20 20 20 20  Data */.  0,    
2b37c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
2b37d 4e 65 78 74 20 2a 2f 0a 20 20 73 74 61 74 33 47  Next */.  stat3G
2b37e 65 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78  et,         /* x
2b37f 46 75 6e 63 20 2a 2f 0a 20 20 30 2c 20 20 20 20  Func */.  0,    
2b380 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
2b381 53 74 65 70 20 2a 2f 0a 20 20 30 2c 20 20 20 20  Step */.  0,    
2b382 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
2b383 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 22 73  Finalize */.  "s
2b384 74 61 74 33 5f 67 65 74 22 2c 20 20 20 20 20 2f  tat3_get",     /
2b385 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 30 2c 20  * zName */.  0, 
2b386 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2b387 2a 20 70 48 61 73 68 20 2a 2f 0a 20 20 30 20 20  * pHash */.  0  
2b388 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2b389 2a 20 70 44 65 73 74 72 75 63 74 6f 72 20 2a 2f  * pDestructor */
2b38a 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .};.#endif /* SQ
2b38b 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54  LITE_ENABLE_STAT
2b38c 33 20 2a 2f 0a 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 47  3 */...../*.** G
2b38d 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
2b38e 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f  do an analysis o
2b38f 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 61 73  f all indices as
2b390 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a  sociated with.**
2b391 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e   a single table.
2b392 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2b393 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28  analyzeOneTable(
2b394 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2b395 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f  ,   /* Parser co
2b396 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
2b397 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 54   *pTab,     /* T
2b398 61 62 6c 65 20 77 68 6f 73 65 20 69 6e 64 69 63  able whose indic
2b399 65 73 20 61 72 65 20 74 6f 20 62 65 20 61 6e 61  es are to be ana
2b39a 6c 79 7a 65 64 20 2a 2f 0a 20 20 49 6e 64 65 78  lyzed */.  Index
2b39b 20 2a 70 4f 6e 6c 79 49 64 78 2c 20 2f 2a 20 49   *pOnlyIdx, /* I
2b39c 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 6f 6e 6c 79  f not NULL, only
2b39d 20 61 6e 61 6c 79 7a 65 20 74 68 69 73 20 6f 6e   analyze this on
2b39e 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  e index */.  int
2b39f 20 69 53 74 61 74 43 75 72 2c 20 20 20 20 2f 2a   iStatCur,    /*
2b3a0 20 49 6e 64 65 78 20 6f 66 20 56 64 62 65 43 75   Index of VdbeCu
2b3a1 72 73 6f 72 20 74 68 61 74 20 77 72 69 74 65 73  rsor that writes
2b3a2 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
2b3a3 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  1 table */.  int
2b3a4 20 69 4d 65 6d 20 20 20 20 20 20 20 20 20 2f 2a   iMem         /*
2b3a5 20 41 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72   Available memor
2b3a6 79 20 6c 6f 63 61 74 69 6f 6e 73 20 62 65 67 69  y locations begi
2b3a7 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 73  n here */.){.  s
2b3a8 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2b3a9 72 73 65 2d 3e 64 62 3b 20 20 20 20 2f 2a 20 44  rse->db;    /* D
2b3aa 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
2b3ab 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  /.  Index *pIdx;
2b3ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b3ad 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 74 6f 20   /* An index to 
2b3ae 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a  being analyzed *
2b3af 2f 0a 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b  /.  int iIdxCur;
2b3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b3b1 20 2f 2a 20 43 75 72 73 6f 72 20 6f 70 65 6e 20   /* Cursor open 
2b3b2 6f 6e 20 69 6e 64 65 78 20 62 65 69 6e 67 20 61  on index being a
2b3b3 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 56 64 62  nalyzed */.  Vdb
2b3b4 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
2b3b5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2b3b6 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
2b3b7 20 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20   being built up 
2b3b8 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
2b3b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b3ba 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2b3bb 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 4f 66  r */.  int topOf
2b3bc 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  Loop;           
2b3bd 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f      /* The top o
2b3be 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20  f the loop */.  
2b3bf 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20  int endOfLoop;  
2b3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2b3c1 54 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c  The end of the l
2b3c2 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6a 5a 65  oop */.  int jZe
2b3c3 72 6f 52 6f 77 73 20 3d 20 2d 31 3b 20 20 20 20  roRows = -1;    
2b3c4 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 66 72        /* Jump fr
2b3c5 6f 6d 20 68 65 72 65 20 69 66 20 6e 75 6d 62 65  om here if numbe
2b3c6 72 20 6f 66 20 72 6f 77 73 20 69 73 20 7a 65 72  r of rows is zer
2b3c7 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  o */.  int iDb; 
2b3c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b3c9 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
2b3ca 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
2b3cb 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e  ing pTab */.  in
2b3cc 74 20 72 65 67 54 61 62 6e 61 6d 65 20 3d 20 69  t regTabname = i
2b3cd 4d 65 6d 2b 2b 3b 20 20 20 20 20 2f 2a 20 52 65  Mem++;     /* Re
2b3ce 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e  gister containin
2b3cf 67 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  g table name */.
2b3d0 20 20 69 6e 74 20 72 65 67 49 64 78 6e 61 6d 65    int regIdxname
2b3d1 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 2f   = iMem++;     /
2b3d2 2a 20 52 65 67 69 73 74 65 72 20 63 6f 6e 74 61  * Register conta
2b3d3 69 6e 69 6e 67 20 69 6e 64 65 78 20 6e 61 6d 65  ining index name
2b3d4 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 53 74 61   */.  int regSta
2b3d5 74 31 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20  t1 = iMem++;    
2b3d6 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74 20 63     /* The stat c
2b3d7 6f 6c 75 6d 6e 20 6f 66 20 73 71 6c 69 74 65 5f  olumn of sqlite_
2b3d8 73 74 61 74 31 20 2a 2f 0a 23 69 66 64 65 66 20  stat1 */.#ifdef 
2b3d9 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
2b3da 41 54 33 0a 20 20 69 6e 74 20 72 65 67 4e 75 6d  AT3.  int regNum
2b3db 45 71 20 3d 20 72 65 67 53 74 61 74 31 3b 20 20  Eq = regStat1;  
2b3dc 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2b3dd 69 6e 73 74 61 6e 63 65 73 2e 20 20 53 61 6d 65  instances.  Same
2b3de 20 61 73 20 72 65 67 53 74 61 74 31 20 2a 2f 0a   as regStat1 */.
2b3df 20 20 69 6e 74 20 72 65 67 4e 75 6d 4c 74 20 3d    int regNumLt =
2b3e0 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f   iMem++;       /
2b3e1 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6b 65 79 73  * Number of keys
2b3e2 20 6c 65 73 73 20 74 68 61 6e 20 72 65 67 53 61   less than regSa
2b3e3 6d 70 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65  mple */.  int re
2b3e4 67 4e 75 6d 44 4c 74 20 3d 20 69 4d 65 6d 2b 2b  gNumDLt = iMem++
2b3e5 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ;      /* Number
2b3e6 20 6f 66 20 64 69 73 74 69 6e 63 74 20 6b 65 79   of distinct key
2b3e7 73 20 6c 65 73 73 20 74 68 61 6e 20 72 65 67 53  s less than regS
2b3e8 61 6d 70 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72  ample */.  int r
2b3e9 65 67 53 61 6d 70 6c 65 20 3d 20 69 4d 65 6d 2b  egSample = iMem+
2b3ea 2b 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e  +;      /* The n
2b3eb 65 78 74 20 73 61 6d 70 6c 65 20 76 61 6c 75 65  ext sample value
2b3ec 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77   */.  int regRow
2b3ed 69 64 20 3d 20 72 65 67 53 61 6d 70 6c 65 3b 20  id = regSample; 
2b3ee 20 20 20 2f 2a 20 52 6f 77 69 64 20 6f 66 20 61     /* Rowid of a
2b3ef 20 73 61 6d 70 6c 65 20 2a 2f 0a 20 20 69 6e 74   sample */.  int
2b3f0 20 72 65 67 41 63 63 75 6d 20 3d 20 69 4d 65 6d   regAccum = iMem
2b3f1 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 67  ++;       /* Reg
2b3f2 69 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 53 74  ister to hold St
2b3f3 61 74 33 41 63 63 75 6d 20 6f 62 6a 65 63 74 20  at3Accum object 
2b3f4 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c 6f 6f 70  */.  int regLoop
2b3f5 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20   = iMem++;      
2b3f6 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2b3f7 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 43 6f  r */.  int regCo
2b3f8 75 6e 74 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20  unt = iMem++;   
2b3f9 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2b3fa 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62   rows in the tab
2b3fb 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20  le or index */. 
2b3fc 20 69 6e 74 20 72 65 67 54 65 6d 70 31 20 3d 20   int regTemp1 = 
2b3fd 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a  iMem++;       /*
2b3fe 20 49 6e 74 65 72 6d 65 64 69 61 74 65 20 72 65   Intermediate re
2b3ff 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  gister */.  int 
2b400 72 65 67 54 65 6d 70 32 20 3d 20 69 4d 65 6d 2b  regTemp2 = iMem+
2b401 2b 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65  +;       /* Inte
2b402 72 6d 65 64 69 61 74 65 20 72 65 67 69 73 74 65  rmediate registe
2b403 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 63 65 20  r */.  int once 
2b404 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 1;            
2b405 20 20 20 20 2f 2a 20 4f 6e 65 2d 74 69 6d 65 20      /* One-time 
2b406 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a  initialization *
2b407 2f 0a 20 20 69 6e 74 20 73 68 6f 72 74 4a 75 6d  /.  int shortJum
2b408 70 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  p = 0;          
2b409 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20   /* Instruction 
2b40a 61 64 64 72 65 73 73 20 2a 2f 0a 20 20 69 6e 74  address */.  int
2b40b 20 69 54 61 62 43 75 72 20 3d 20 70 50 61 72 73   iTabCur = pPars
2b40c 65 2d 3e 6e 54 61 62 2b 2b 3b 20 2f 2a 20 54 61  e->nTab++; /* Ta
2b40d 62 6c 65 20 63 75 72 73 6f 72 20 2a 2f 0a 23 65  ble cursor */.#e
2b40e 6e 64 69 66 0a 20 20 69 6e 74 20 72 65 67 43 6f  ndif.  int regCo
2b40f 6c 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20  l = iMem++;     
2b410 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f      /* Content o
2b411 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 6e  f a column in an
2b412 61 6c 79 7a 65 64 20 74 61 62 6c 65 20 2a 2f 0a  alyzed table */.
2b413 20 20 69 6e 74 20 72 65 67 52 65 63 20 3d 20 69    int regRec = i
2b414 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 2f  Mem++;         /
2b415 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
2b416 6e 67 20 63 6f 6d 70 6c 65 74 65 64 20 72 65 63  ng completed rec
2b417 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ord */.  int reg
2b418 54 65 6d 70 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20  Temp = iMem++;  
2b419 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61        /* Tempora
2b41a 72 79 20 75 73 65 20 72 65 67 69 73 74 65 72 20  ry use register 
2b41b 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4e 65 77 52  */.  int regNewR
2b41c 6f 77 69 64 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20  owid = iMem++;  
2b41d 20 20 2f 2a 20 52 6f 77 69 64 20 66 6f 72 20 74    /* Rowid for t
2b41e 68 65 20 69 6e 73 65 72 74 65 64 20 72 65 63 6f  he inserted reco
2b41f 72 64 20 2a 2f 0a 0a 0a 20 20 76 20 3d 20 73 71  rd */...  v = sq
2b420 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
2b421 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30  rse);.  if( v==0
2b422 20 7c 7c 20 4e 45 56 45 52 28 70 54 61 62 3d 3d   || NEVER(pTab==
2b423 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  0) ){.    return
2b424 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 61 62  ;.  }.  if( pTab
2b425 2d 3e 74 6e 75 6d 3d 3d 30 20 29 7b 0a 20 20 20  ->tnum==0 ){.   
2b426 20 2f 2a 20 44 6f 20 6e 6f 74 20 67 61 74 68 65   /* Do not gathe
2b427 72 20 73 74 61 74 69 73 74 69 63 73 20 6f 6e 20  r statistics on 
2b428 76 69 65 77 73 20 6f 72 20 76 69 72 74 75 61 6c  views or virtual
2b429 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 72   tables */.    r
2b42a 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28  eturn;.  }.  if(
2b42b 20 6d 65 6d 63 6d 70 28 70 54 61 62 2d 3e 7a 4e   memcmp(pTab->zN
2b42c 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20  ame, "sqlite_", 
2b42d 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  7)==0 ){.    /* 
2b42e 44 6f 20 6e 6f 74 20 67 61 74 68 65 72 20 73 74  Do not gather st
2b42f 61 74 69 73 74 69 63 73 20 6f 6e 20 73 79 73 74  atistics on syst
2b430 65 6d 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20  em tables */.   
2b431 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61   return;.  }.  a
2b432 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
2b433 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
2b434 65 73 28 64 62 29 20 29 3b 0a 20 20 69 44 62 20  es(db) );.  iDb 
2b435 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
2b436 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
2b437 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73  >pSchema);.  ass
2b438 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a 20  ert( iDb>=0 );. 
2b439 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
2b43a 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28  SchemaMutexHeld(
2b43b 64 62 2c 20 69 44 62 2c 20 30 29 20 29 3b 0a 23  db, iDb, 0) );.#
2b43c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2b43d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
2b43e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75  .  if( sqlite3Au
2b43f 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
2b440 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a 45 2c 20  SQLITE_ANALYZE, 
2b441 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 0a  pTab->zName, 0,.
2b442 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
2b443 62 5d 2e 7a 4e 61 6d 65 20 29 20 29 7b 0a 20 20  b].zName ) ){.  
2b444 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65    return;.  }.#e
2b445 6e 64 69 66 0a 0a 20 20 2f 2a 20 45 73 74 61 62  ndif..  /* Estab
2b446 6c 69 73 68 20 61 20 72 65 61 64 2d 6c 6f 63 6b  lish a read-lock
2b447 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 61 74   on the table at
2b448 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68   the shared-cach
2b449 65 20 6c 65 76 65 6c 2e 20 2a 2f 0a 20 20 73 71  e level. */.  sq
2b44a 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70  lite3TableLock(p
2b44b 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62  Parse, iDb, pTab
2b44c 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d  ->tnum, 0, pTab-
2b44d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 69 49 64 78  >zName);..  iIdx
2b44e 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Cur = pParse->nT
2b44f 61 62 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 56  ab++;.  sqlite3V
2b450 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2b451 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 54  String8, 0, regT
2b452 61 62 6e 61 6d 65 2c 20 30 2c 20 70 54 61 62 2d  abname, 0, pTab-
2b453 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 66 6f  >zName, 0);.  fo
2b454 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
2b455 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
2b456 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
2b457 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 20 20    int nCol;.    
2b458 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 0a 20  KeyInfo *pKey;. 
2b459 20 20 20 69 6e 74 20 61 64 64 72 49 66 4e 6f 74     int addrIfNot
2b45a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2b45b 2f 2a 20 61 64 64 72 65 73 73 20 6f 66 20 4f 50  /* address of OP
2b45c 5f 49 66 4e 6f 74 20 2a 2f 0a 20 20 20 20 69 6e  _IfNot */.    in
2b45d 74 20 2a 61 43 68 6e 67 41 64 64 72 3b 20 20 20  t *aChngAddr;   
2b45e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
2b45f 72 61 79 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74  ray of jump inst
2b460 72 75 63 74 69 6f 6e 20 61 64 64 72 65 73 73 65  ruction addresse
2b461 73 20 2a 2f 0a 0a 20 20 20 20 69 66 28 20 70 4f  s */..    if( pO
2b462 6e 6c 79 49 64 78 20 26 26 20 70 4f 6e 6c 79 49  nlyIdx && pOnlyI
2b463 64 78 21 3d 70 49 64 78 20 29 20 63 6f 6e 74 69  dx!=pIdx ) conti
2b464 6e 75 65 3b 0a 20 20 20 20 56 64 62 65 4e 6f 6f  nue;.    VdbeNoo
2b465 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 42 65  pComment((v, "Be
2b466 67 69 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20  gin analysis of 
2b467 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65  %s", pIdx->zName
2b468 29 29 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 70  ));.    nCol = p
2b469 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20  Idx->nColumn;.  
2b46a 20 20 61 43 68 6e 67 41 64 64 72 20 3d 20 73 71    aChngAddr = sq
2b46b 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
2b46c 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29  (db, sizeof(int)
2b46d 2a 6e 43 6f 6c 29 3b 0a 20 20 20 20 69 66 28 20  *nCol);.    if( 
2b46e 61 43 68 6e 67 41 64 64 72 3d 3d 30 20 29 20 63  aChngAddr==0 ) c
2b46f 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 4b 65  ontinue;.    pKe
2b470 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78  y = sqlite3Index
2b471 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20  Keyinfo(pParse, 
2b472 70 49 64 78 29 3b 0a 20 20 20 20 69 66 28 20 69  pIdx);.    if( i
2b473 4d 65 6d 2b 31 2b 28 6e 43 6f 6c 2a 32 29 3e 70  Mem+1+(nCol*2)>p
2b474 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 7b 0a 20  Parse->nMem ){. 
2b475 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65       pParse->nMe
2b476 6d 20 3d 20 69 4d 65 6d 2b 31 2b 28 6e 43 6f 6c  m = iMem+1+(nCol
2b477 2a 32 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  *2);.    }..    
2b478 2f 2a 20 4f 70 65 6e 20 61 20 63 75 72 73 6f 72  /* Open a cursor
2b479 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 74 6f   to the index to
2b47a 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f   be analyzed. */
2b47b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62  .    assert( iDb
2b47c 3d 3d 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  ==sqlite3SchemaT
2b47d 6f 49 6e 64 65 78 28 64 62 2c 20 70 49 64 78 2d  oIndex(db, pIdx-
2b47e 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 20 20  >pSchema) );.   
2b47f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b480 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  p4(v, OP_OpenRea
2b481 64 2c 20 69 49 64 78 43 75 72 2c 20 70 49 64 78  d, iIdxCur, pIdx
2b482 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20  ->tnum, iDb,.   
2b483 20 20 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65       (char *)pKe
2b484 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  y, P4_KEYINFO_HA
2b485 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62 65  NDOFF);.    Vdbe
2b486 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22  Comment((v, "%s"
2b487 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b  , pIdx->zName));
2b488 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74  ..    /* Populat
2b489 65 20 74 68 65 20 72 65 67 69 73 74 65 72 20 63  e the register c
2b48a 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 69 6e  ontaining the in
2b48b 64 65 78 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20  dex name. */.   
2b48c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b48d 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
2b48e 2c 20 30 2c 20 72 65 67 49 64 78 6e 61 6d 65 2c  , 0, regIdxname,
2b48f 20 30 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c   0, pIdx->zName,
2b490 20 30 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c   0);..#ifdef SQL
2b491 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33  ITE_ENABLE_STAT3
2b492 0a 20 20 20 20 69 66 28 20 6f 6e 63 65 20 29 7b  .    if( once ){
2b493 0a 20 20 20 20 20 20 6f 6e 63 65 20 3d 20 30 3b  .      once = 0;
2b494 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70  .      sqlite3Op
2b495 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  enTable(pParse, 
2b496 69 54 61 62 43 75 72 2c 20 69 44 62 2c 20 70 54  iTabCur, iDb, pT
2b497 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29  ab, OP_OpenRead)
2b498 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2b499 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b49a 20 4f 50 5f 43 6f 75 6e 74 2c 20 69 49 64 78 43   OP_Count, iIdxC
2b49b 75 72 2c 20 72 65 67 43 6f 75 6e 74 29 3b 0a 20  ur, regCount);. 
2b49c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b49d 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2b49e 65 72 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 33  er, SQLITE_STAT3
2b49f 5f 53 41 4d 50 4c 45 53 2c 20 72 65 67 54 65 6d  _SAMPLES, regTem
2b4a0 70 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  p1);.    sqlite3
2b4a1 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b4a2 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67  _Integer, 0, reg
2b4a3 4e 75 6d 45 71 29 3b 0a 20 20 20 20 73 71 6c 69  NumEq);.    sqli
2b4a4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b4a5 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
2b4a6 72 65 67 4e 75 6d 4c 74 29 3b 0a 20 20 20 20 73  regNumLt);.    s
2b4a7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b4a8 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2b4a9 2d 31 2c 20 72 65 67 4e 75 6d 44 4c 74 29 3b 0a  -1, regNumDLt);.
2b4aa 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b4ab 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp3(v, OP_Null
2b4ac 2c 20 30 2c 20 72 65 67 53 61 6d 70 6c 65 2c 20  , 0, regSample, 
2b4ad 72 65 67 41 63 63 75 6d 29 3b 0a 20 20 20 20 73  regAccum);.    s
2b4ae 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2b4af 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c  (v, OP_Function,
2b4b0 20 31 2c 20 72 65 67 43 6f 75 6e 74 2c 20 72 65   1, regCount, re
2b4b1 67 41 63 63 75 6d 2c 0a 20 20 20 20 20 20 20 20  gAccum,.        
2b4b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
2b4b3 68 61 72 2a 29 26 73 74 61 74 33 49 6e 69 74 46  har*)&stat3InitF
2b4b4 75 6e 63 64 65 66 2c 20 50 34 5f 46 55 4e 43 44  uncdef, P4_FUNCD
2b4b5 45 46 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  EF);.    sqlite3
2b4b6 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
2b4b7 32 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  2);.#endif /* SQ
2b4b8 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54  LITE_ENABLE_STAT
2b4b9 33 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 54 68 65  3 */..    /* The
2b4ba 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79   block of memory
2b4bb 20 63 65 6c 6c 73 20 69 6e 69 74 69 61 6c 69 7a   cells initializ
2b4bc 65 64 20 68 65 72 65 20 69 73 20 75 73 65 64 20  ed here is used 
2b4bd 61 73 20 66 6f 6c 6c 6f 77 73 2e 0a 20 20 20 20  as follows..    
2b4be 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 69 4d 65  **.    **    iMe
2b4bf 6d 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m:              
2b4c0 20 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20    .    **       
2b4c1 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   The total numbe
2b4c2 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65  r of rows in the
2b4c3 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20   table..    **. 
2b4c4 20 20 20 2a 2a 20 20 20 20 69 4d 65 6d 2b 31 20     **    iMem+1 
2b4c5 2e 2e 20 69 4d 65 6d 2b 6e 43 6f 6c 3a 20 0a 20  .. iMem+nCol: . 
2b4c6 20 20 20 2a 2a 20 20 20 20 20 20 20 20 4e 75 6d     **        Num
2b4c7 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 74 20  ber of distinct 
2b4c8 65 6e 74 72 69 65 73 20 69 6e 20 69 6e 64 65 78  entries in index
2b4c9 20 63 6f 6e 73 69 64 65 72 69 6e 67 20 74 68 65   considering the
2b4ca 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20   .    **        
2b4cb 6c 65 66 74 2d 6d 6f 73 74 20 4e 20 63 6f 6c 75  left-most N colu
2b4cc 6d 6e 73 20 6f 6e 6c 79 2c 20 77 68 65 72 65 20  mns only, where 
2b4cd 4e 20 69 73 20 62 65 74 77 65 65 6e 20 31 20 61  N is between 1 a
2b4ce 6e 64 20 6e 43 6f 6c 2c 20 0a 20 20 20 20 2a 2a  nd nCol, .    **
2b4cf 20 20 20 20 20 20 20 20 69 6e 63 6c 75 73 69 76          inclusiv
2b4d0 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e..    **.    **
2b4d1 20 20 20 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 31 20      iMem+nCol+1 
2b4d2 2e 2e 20 4d 65 6d 2b 32 2a 6e 43 6f 6c 3a 20 20  .. Mem+2*nCol:  
2b4d3 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 50  .    **        P
2b4d4 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 6f 66  revious value of
2b4d5 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 73   indexed columns
2b4d6 2c 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72  , from left to r
2b4d7 69 67 68 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  ight..    **.   
2b4d8 20 2a 2a 20 43 65 6c 6c 73 20 69 4d 65 6d 20 74   ** Cells iMem t
2b4d9 68 72 6f 75 67 68 20 69 4d 65 6d 2b 6e 43 6f 6c  hrough iMem+nCol
2b4da 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64   are initialized
2b4db 20 74 6f 20 30 2e 20 54 68 65 20 6f 74 68 65 72   to 0. The other
2b4dc 73 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 69 6e  s are .    ** in
2b4dd 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 63 6f 6e  itialized to con
2b4de 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c  tain an SQL NULL
2b4df 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
2b4e0 28 69 3d 30 3b 20 69 3c 3d 6e 43 6f 6c 3b 20 69  (i=0; i<=nCol; i
2b4e1 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
2b4e2 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b4e3 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69  OP_Integer, 0, i
2b4e4 4d 65 6d 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20  Mem+i);.    }.  
2b4e5 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
2b4e6 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  l; i++){.      s
2b4e7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b4e8 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
2b4e9 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a  iMem+nCol+i+1);.
2b4ea 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 74      }..    /* St
2b4eb 61 72 74 20 74 68 65 20 61 6e 61 6c 79 73 69 73  art the analysis
2b4ec 20 6c 6f 6f 70 2e 20 54 68 69 73 20 6c 6f 6f 70   loop. This loop
2b4ed 20 72 75 6e 73 20 74 68 72 6f 75 67 68 20 61 6c   runs through al
2b4ee 6c 20 74 68 65 20 65 6e 74 72 69 65 73 20 69 6e  l the entries in
2b4ef 0a 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 64 65  .    ** the inde
2b4f0 78 20 62 2d 74 72 65 65 2e 20 20 2a 2f 0a 20 20  x b-tree.  */.  
2b4f1 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 71    endOfLoop = sq
2b4f2 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
2b4f3 65 6c 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74  el(v);.    sqlit
2b4f4 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b4f5 4f 50 5f 52 65 77 69 6e 64 2c 20 69 49 64 78 43  OP_Rewind, iIdxC
2b4f6 75 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a  ur, endOfLoop);.
2b4f7 20 20 20 20 74 6f 70 4f 66 4c 6f 6f 70 20 3d 20      topOfLoop = 
2b4f8 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2b4f9 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73  ntAddr(v);.    s
2b4fa 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b4fb 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69  (v, OP_AddImm, i
2b4fc 4d 65 6d 2c 20 31 29 3b 20 20 2f 2a 20 49 6e 63  Mem, 1);  /* Inc
2b4fd 72 65 6d 65 6e 74 20 72 6f 77 20 63 6f 75 6e 74  rement row count
2b4fe 65 72 20 2a 2f 0a 0a 20 20 20 20 66 6f 72 28 69  er */..    for(i
2b4ff 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
2b500 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20  {.      CollSeq 
2b501 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 73 71  *pColl;.      sq
2b502 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2b503 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49  v, OP_Column, iI
2b504 64 78 43 75 72 2c 20 69 2c 20 72 65 67 43 6f 6c  dxCur, i, regCol
2b505 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d  );.      if( i==
2b506 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
2b507 41 6c 77 61 79 73 20 72 65 63 6f 72 64 20 74 68  Always record th
2b508 65 20 76 65 72 79 20 66 69 72 73 74 20 72 6f 77  e very first row
2b509 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 64 64 72   */.        addr
2b50a 49 66 4e 6f 74 20 3d 20 73 71 6c 69 74 65 33 56  IfNot = sqlite3V
2b50b 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2b50c 49 66 4e 6f 74 2c 20 69 4d 65 6d 2b 31 29 3b 0a  IfNot, iMem+1);.
2b50d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
2b50e 73 65 72 74 28 20 70 49 64 78 2d 3e 61 7a 43 6f  sert( pIdx->azCo
2b50f 6c 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  ll!=0 );.      a
2b510 73 73 65 72 74 28 20 70 49 64 78 2d 3e 61 7a 43  ssert( pIdx->azC
2b511 6f 6c 6c 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 20  oll[i]!=0 );.   
2b512 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
2b513 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28  e3LocateCollSeq(
2b514 70 50 61 72 73 65 2c 20 70 49 64 78 2d 3e 61 7a  pParse, pIdx->az
2b515 43 6f 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20  Coll[i]);.      
2b516 61 43 68 6e 67 41 64 64 72 5b 69 5d 20 3d 20 73  aChngAddr[i] = s
2b517 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2b518 28 76 2c 20 4f 50 5f 4e 65 2c 20 72 65 67 43 6f  (v, OP_Ne, regCo
2b519 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b  l, 0, iMem+nCol+
2b51a 69 2b 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20  i+1,.           
2b51b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b51c 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72             (char
2b51d 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c  *)pColl, P4_COLL
2b51e 53 45 51 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  SEQ);.      sqli
2b51f 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
2b520 76 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51  v, SQLITE_NULLEQ
2b521 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2b522 6d 65 6e 74 28 28 76 2c 20 22 6a 75 6d 70 20 69  ment((v, "jump i
2b523 66 20 63 6f 6c 75 6d 6e 20 25 64 20 63 68 61 6e  f column %d chan
2b524 67 65 64 22 2c 20 69 29 29 3b 0a 23 69 66 64 65  ged", i));.#ifde
2b525 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
2b526 53 54 41 54 33 0a 20 20 20 20 20 20 69 66 28 20  STAT3.      if( 
2b527 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  i==0 ){.        
2b528 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b529 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
2b52a 72 65 67 4e 75 6d 45 71 2c 20 31 29 3b 0a 20 20  regNumEq, 1);.  
2b52b 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
2b52c 74 28 28 76 2c 20 22 69 6e 63 72 20 72 65 70 65  t((v, "incr repe
2b52d 61 74 20 63 6f 75 6e 74 22 29 29 3b 0a 20 20 20  at count"));.   
2b52e 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
2b52f 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
2b530 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2b531 74 6f 2c 20 30 2c 20 65 6e 64 4f 66 4c 6f 6f 70  to, 0, endOfLoop
2b532 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
2b533 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
2b534 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
2b535 75 6d 70 48 65 72 65 28 76 2c 20 61 43 68 6e 67  umpHere(v, aChng
2b536 41 64 64 72 5b 69 5d 29 3b 20 20 2f 2a 20 53 65  Addr[i]);  /* Se
2b537 74 20 6a 75 6d 70 20 64 65 73 74 20 66 6f 72 20  t jump dest for 
2b538 74 68 65 20 4f 50 5f 4e 65 20 2a 2f 0a 20 20 20  the OP_Ne */.   
2b539 20 20 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a 20     if( i==0 ){. 
2b53a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2b53b 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
2b53c 64 72 49 66 4e 6f 74 29 3b 20 20 20 2f 2a 20 4a  drIfNot);   /* J
2b53d 75 6d 70 20 64 65 73 74 20 66 6f 72 20 4f 50 5f  ump dest for OP_
2b53e 49 66 4e 6f 74 20 2a 2f 0a 23 69 66 64 65 66 20  IfNot */.#ifdef 
2b53f 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
2b540 41 54 33 0a 20 20 20 20 20 20 20 20 73 71 6c 69  AT3.        sqli
2b541 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2b542 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 31 2c   OP_Function, 1,
2b543 20 72 65 67 4e 75 6d 45 71 2c 20 72 65 67 54 65   regNumEq, regTe
2b544 6d 70 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20  mp2,.           
2b545 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
2b546 63 68 61 72 2a 29 26 73 74 61 74 33 50 75 73 68  char*)&stat3Push
2b547 46 75 6e 63 64 65 66 2c 20 50 34 5f 46 55 4e 43  Funcdef, P4_FUNC
2b548 44 45 46 29 3b 0a 20 20 20 20 20 20 20 20 73 71  DEF);.        sq
2b549 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2b54a 35 28 76 2c 20 35 29 3b 0a 20 20 20 20 20 20 20  5(v, 5);.       
2b54b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b54c 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
2b54d 20 69 49 64 78 43 75 72 2c 20 70 49 64 78 2d 3e   iIdxCur, pIdx->
2b54e 6e 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 6f 77 69  nColumn, regRowi
2b54f 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  d);.        sqli
2b550 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2b551 20 4f 50 5f 41 64 64 2c 20 72 65 67 4e 75 6d 45   OP_Add, regNumE
2b552 71 2c 20 72 65 67 4e 75 6d 4c 74 2c 20 72 65 67  q, regNumLt, reg
2b553 4e 75 6d 4c 74 29 3b 0a 20 20 20 20 20 20 20 20  NumLt);.        
2b554 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b555 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
2b556 72 65 67 4e 75 6d 44 4c 74 2c 20 31 29 3b 0a 20  regNumDLt, 1);. 
2b557 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2b558 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2b559 6e 74 65 67 65 72 2c 20 31 2c 20 72 65 67 4e 75  nteger, 1, regNu
2b55a 6d 45 71 29 3b 0a 23 65 6e 64 69 66 20 20 20 20  mEq);.#endif    
2b55b 20 20 20 20 0a 20 20 20 20 20 20 7d 0a 20 20 20      .      }.   
2b55c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b55d 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
2b55e 6d 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20 31 29 3b  m, iMem+i+1, 1);
2b55f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2b560 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
2b561 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20  olumn, iIdxCur, 
2b562 69 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31  i, iMem+nCol+i+1
2b563 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
2b564 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61  ite3DbFree(db, a
2b565 43 68 6e 67 41 64 64 72 29 3b 0a 0a 20 20 20 20  ChngAddr);..    
2b566 2f 2a 20 41 6c 77 61 79 73 20 6a 75 6d 70 20 68  /* Always jump h
2b567 65 72 65 20 61 66 74 65 72 20 75 70 64 61 74 69  ere after updati
2b568 6e 67 20 74 68 65 20 69 4d 65 6d 2b 31 2e 2e 2e  ng the iMem+1...
2b569 69 4d 65 6d 2b 31 2b 6e 43 6f 6c 20 63 6f 75 6e  iMem+1+nCol coun
2b56a 74 65 72 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69  ters */.    sqli
2b56b 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
2b56c 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c 6f 6f 70  bel(v, endOfLoop
2b56d 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56  );..    sqlite3V
2b56e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b56f 4e 65 78 74 2c 20 69 49 64 78 43 75 72 2c 20 74  Next, iIdxCur, t
2b570 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 73  opOfLoop);.    s
2b571 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2b572 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49  (v, OP_Close, iI
2b573 64 78 43 75 72 29 3b 0a 23 69 66 64 65 66 20 53  dxCur);.#ifdef S
2b574 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
2b575 54 33 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  T3.    sqlite3Vd
2b576 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46  beAddOp4(v, OP_F
2b577 75 6e 63 74 69 6f 6e 2c 20 31 2c 20 72 65 67 4e  unction, 1, regN
2b578 75 6d 45 71 2c 20 72 65 67 54 65 6d 70 32 2c 0a  umEq, regTemp2,.
2b579 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b57a 20 20 20 20 20 20 28 63 68 61 72 2a 29 26 73 74        (char*)&st
2b57b 61 74 33 50 75 73 68 46 75 6e 63 64 65 66 2c 20  at3PushFuncdef, 
2b57c 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20  P4_FUNCDEF);.   
2b57d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
2b57e 67 65 50 35 28 76 2c 20 35 29 3b 0a 20 20 20 20  geP5(v, 5);.    
2b57f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b580 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2b581 20 2d 31 2c 20 72 65 67 4c 6f 6f 70 29 3b 0a 20   -1, regLoop);. 
2b582 20 20 20 73 68 6f 72 74 4a 75 6d 70 20 3d 20 0a     shortJump = .
2b583 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b584 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
2b585 6d 6d 2c 20 72 65 67 4c 6f 6f 70 2c 20 31 29 3b  mm, regLoop, 1);
2b586 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2b587 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 6e  AddOp4(v, OP_Fun
2b588 63 74 69 6f 6e 2c 20 31 2c 20 72 65 67 41 63 63  ction, 1, regAcc
2b589 75 6d 2c 20 72 65 67 54 65 6d 70 31 2c 0a 20 20  um, regTemp1,.  
2b58a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b58b 20 20 20 20 28 63 68 61 72 2a 29 26 73 74 61 74      (char*)&stat
2b58c 33 47 65 74 46 75 6e 63 64 65 66 2c 20 50 34 5f  3GetFuncdef, P4_
2b58d 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 73 71  FUNCDEF);.    sq
2b58e 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2b58f 35 28 76 2c 20 32 29 3b 0a 20 20 20 20 73 71 6c  5(v, 2);.    sql
2b590 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2b591 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67  , OP_IsNull, reg
2b592 54 65 6d 70 31 29 3b 0a 20 20 20 20 73 71 6c 69  Temp1);.    sqli
2b593 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2b594 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69   OP_NotExists, i
2b595 54 61 62 43 75 72 2c 20 73 68 6f 72 74 4a 75 6d  TabCur, shortJum
2b596 70 2c 20 72 65 67 54 65 6d 70 31 29 3b 0a 20 20  p, regTemp1);.  
2b597 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b598 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
2b599 2c 20 69 54 61 62 43 75 72 2c 20 70 49 64 78 2d  , iTabCur, pIdx-
2b59a 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 2c 20 72 65  >aiColumn[0], re
2b59b 67 53 61 6d 70 6c 65 29 3b 0a 20 20 20 20 73 71  gSample);.    sq
2b59c 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75  lite3ColumnDefau
2b59d 6c 74 28 76 2c 20 70 54 61 62 2c 20 70 49 64 78  lt(v, pTab, pIdx
2b59e 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 2c 20 72  ->aiColumn[0], r
2b59f 65 67 53 61 6d 70 6c 65 29 3b 0a 20 20 20 20 73  egSample);.    s
2b5a0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2b5a1 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c  (v, OP_Function,
2b5a2 20 31 2c 20 72 65 67 41 63 63 75 6d 2c 20 72 65   1, regAccum, re
2b5a3 67 4e 75 6d 45 71 2c 0a 20 20 20 20 20 20 20 20  gNumEq,.        
2b5a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
2b5a5 68 61 72 2a 29 26 73 74 61 74 33 47 65 74 46 75  har*)&stat3GetFu
2b5a6 6e 63 64 65 66 2c 20 50 34 5f 46 55 4e 43 44 45  ncdef, P4_FUNCDE
2b5a7 46 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  F);.    sqlite3V
2b5a8 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 33  dbeChangeP5(v, 3
2b5a9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2b5aa 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46  beAddOp4(v, OP_F
2b5ab 75 6e 63 74 69 6f 6e 2c 20 31 2c 20 72 65 67 41  unction, 1, regA
2b5ac 63 63 75 6d 2c 20 72 65 67 4e 75 6d 4c 74 2c 0a  ccum, regNumLt,.
2b5ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b5ae 20 20 20 20 20 20 28 63 68 61 72 2a 29 26 73 74        (char*)&st
2b5af 61 74 33 47 65 74 46 75 6e 63 64 65 66 2c 20 50  at3GetFuncdef, P
2b5b0 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20  4_FUNCDEF);.    
2b5b1 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2b5b2 65 50 35 28 76 2c 20 34 29 3b 0a 20 20 20 20 73  eP5(v, 4);.    s
2b5b3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2b5b4 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c  (v, OP_Function,
2b5b5 20 31 2c 20 72 65 67 41 63 63 75 6d 2c 20 72 65   1, regAccum, re
2b5b6 67 4e 75 6d 44 4c 74 2c 0a 20 20 20 20 20 20 20  gNumDLt,.       
2b5b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
2b5b8 63 68 61 72 2a 29 26 73 74 61 74 33 47 65 74 46  char*)&stat3GetF
2b5b9 75 6e 63 64 65 66 2c 20 50 34 5f 46 55 4e 43 44  uncdef, P4_FUNCD
2b5ba 45 46 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  EF);.    sqlite3
2b5bb 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
2b5bc 35 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  5);.    sqlite3V
2b5bd 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2b5be 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 54  MakeRecord, regT
2b5bf 61 62 6e 61 6d 65 2c 20 36 2c 20 72 65 67 52 65  abname, 6, regRe
2b5c0 63 2c 20 22 62 62 62 62 62 62 22 2c 20 30 29 3b  c, "bbbbbb", 0);
2b5c1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2b5c2 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77  AddOp2(v, OP_New
2b5c3 52 6f 77 69 64 2c 20 69 53 74 61 74 43 75 72 2b  Rowid, iStatCur+
2b5c4 31 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b  1, regNewRowid);
2b5c5 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2b5c6 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73  AddOp3(v, OP_Ins
2b5c7 65 72 74 2c 20 69 53 74 61 74 43 75 72 2b 31 2c  ert, iStatCur+1,
2b5c8 20 72 65 67 52 65 63 2c 20 72 65 67 4e 65 77 52   regRec, regNewR
2b5c9 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
2b5ca 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b5cb 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 73 68 6f 72  OP_Goto, 0, shor
2b5cc 74 4a 75 6d 70 29 3b 0a 20 20 20 20 73 71 6c 69  tJump);.    sqli
2b5cd 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2b5ce 76 2c 20 73 68 6f 72 74 4a 75 6d 70 2b 32 29 3b  v, shortJump+2);
2b5cf 0a 23 65 6e 64 69 66 20 20 20 20 20 20 20 20 0a  .#endif        .
2b5d0 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68  .    /* Store th
2b5d1 65 20 72 65 73 75 6c 74 73 20 69 6e 20 73 71 6c  e results in sql
2b5d2 69 74 65 5f 73 74 61 74 31 2e 0a 20 20 20 20 2a  ite_stat1..    *
2b5d3 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73  *.    ** The res
2b5d4 75 6c 74 20 69 73 20 61 20 73 69 6e 67 6c 65 20  ult is a single 
2b5d5 72 6f 77 20 6f 66 20 74 68 65 20 73 71 6c 69 74  row of the sqlit
2b5d6 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e 20 20  e_stat1 table.  
2b5d7 54 68 65 20 66 69 72 73 74 0a 20 20 20 20 2a 2a  The first.    **
2b5d8 20 74 77 6f 20 63 6f 6c 75 6d 6e 73 20 61 72 65   two columns are
2b5d9 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68   the names of th
2b5da 65 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65  e table and inde
2b5db 78 2e 20 20 54 68 65 20 74 68 69 72 64 20 63 6f  x.  The third co
2b5dc 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 69 73 20 61  lumn.    ** is a
2b5dd 20 73 74 72 69 6e 67 20 63 6f 6d 70 6f 73 65 64   string composed
2b5de 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 20 69 6e   of a list of in
2b5df 74 65 67 65 72 20 73 74 61 74 69 73 74 69 63 73  teger statistics
2b5e0 20 61 62 6f 75 74 20 74 68 65 0a 20 20 20 20 2a   about the.    *
2b5e1 2a 20 69 6e 64 65 78 2e 20 20 54 68 65 20 66 69  * index.  The fi
2b5e2 72 73 74 20 69 6e 74 65 67 65 72 20 69 6e 20 74  rst integer in t
2b5e3 68 65 20 6c 69 73 74 20 69 73 20 74 68 65 20 74  he list is the t
2b5e4 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 65  otal number of e
2b5e5 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a 20 69 6e  ntries.    ** in
2b5e6 20 74 68 65 20 69 6e 64 65 78 2e 20 20 54 68 65   the index.  The
2b5e7 72 65 20 69 73 20 6f 6e 65 20 61 64 64 69 74 69  re is one additi
2b5e8 6f 6e 61 6c 20 69 6e 74 65 67 65 72 20 69 6e 20  onal integer in 
2b5e9 74 68 65 20 6c 69 73 74 20 66 6f 72 20 65 61 63  the list for eac
2b5ea 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20  h.    ** column 
2b5eb 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54  of the table.  T
2b5ec 68 69 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  his additional i
2b5ed 6e 74 65 67 65 72 20 69 73 20 61 20 67 75 65 73  nteger is a gues
2b5ee 73 20 6f 66 20 68 6f 77 20 6d 61 6e 79 0a 20 20  s of how many.  
2b5ef 20 20 2a 2a 20 72 6f 77 73 20 6f 66 20 74 68 65    ** rows of the
2b5f0 20 74 61 62 6c 65 20 74 68 65 20 69 6e 64 65 78   table the index
2b5f1 20 77 69 6c 6c 20 73 65 6c 65 63 74 2e 20 20 49   will select.  I
2b5f2 66 20 44 20 69 73 20 74 68 65 20 63 6f 75 6e 74  f D is the count
2b5f3 20 6f 66 20 64 69 73 74 69 6e 63 74 0a 20 20 20   of distinct.   
2b5f4 20 2a 2a 20 76 61 6c 75 65 73 20 61 6e 64 20 4b   ** values and K
2b5f5 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75   is the total nu
2b5f6 6d 62 65 72 20 6f 66 20 72 6f 77 73 2c 20 74 68  mber of rows, th
2b5f7 65 6e 20 74 68 65 20 69 6e 74 65 67 65 72 20 69  en the integer i
2b5f8 73 20 63 6f 6d 70 75 74 65 64 0a 20 20 20 20 2a  s computed.    *
2b5f9 2a 20 61 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  * as:.    **.   
2b5fa 20 2a 2a 20 20 20 20 20 20 20 20 49 20 3d 20 28   **        I = (
2b5fb 4b 2b 44 2d 31 29 2f 44 0a 20 20 20 20 2a 2a 0a  K+D-1)/D.    **.
2b5fc 20 20 20 20 2a 2a 20 49 66 20 4b 3d 3d 30 20 74      ** If K==0 t
2b5fd 68 65 6e 20 6e 6f 20 65 6e 74 72 79 20 69 73 20  hen no entry is 
2b5fe 6d 61 64 65 20 69 6e 74 6f 20 74 68 65 20 73 71  made into the sq
2b5ff 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
2b600 2e 20 20 0a 20 20 20 20 2a 2a 20 49 66 20 4b 3e  .  .    ** If K>
2b601 30 20 74 68 65 6e 20 69 74 20 69 73 20 61 6c 77  0 then it is alw
2b602 61 79 73 20 74 68 65 20 63 61 73 65 20 74 68 65  ays the case the
2b603 20 44 3e 30 20 73 6f 20 64 69 76 69 73 69 6f 6e   D>0 so division
2b604 20 62 79 20 7a 65 72 6f 0a 20 20 20 20 2a 2a 20   by zero.    ** 
2b605 69 73 20 6e 65 76 65 72 20 70 6f 73 73 69 62 6c  is never possibl
2b606 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  e..    */.    sq
2b607 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b608 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 4d 65  v, OP_SCopy, iMe
2b609 6d 2c 20 72 65 67 53 74 61 74 31 29 3b 0a 20 20  m, regStat1);.  
2b60a 20 20 69 66 28 20 6a 5a 65 72 6f 52 6f 77 73 3c    if( jZeroRows<
2b60b 30 20 29 7b 0a 20 20 20 20 20 20 6a 5a 65 72 6f  0 ){.      jZero
2b60c 52 6f 77 73 20 3d 20 73 71 6c 69 74 65 33 56 64  Rows = sqlite3Vd
2b60d 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
2b60e 66 4e 6f 74 2c 20 69 4d 65 6d 29 3b 0a 20 20 20  fNot, iMem);.   
2b60f 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20   }.    for(i=0; 
2b610 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
2b611 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b612 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
2b613 6e 67 38 2c 20 30 2c 20 72 65 67 54 65 6d 70 2c  ng8, 0, regTemp,
2b614 20 30 2c 20 22 20 22 2c 20 30 29 3b 0a 20 20 20   0, " ", 0);.   
2b615 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b616 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6e 63 61  dOp3(v, OP_Conca
2b617 74 2c 20 72 65 67 54 65 6d 70 2c 20 72 65 67 53  t, regTemp, regS
2b618 74 61 74 31 2c 20 72 65 67 53 74 61 74 31 29 3b  tat1, regStat1);
2b619 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2b61a 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41  beAddOp3(v, OP_A
2b61b 64 64 2c 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69  dd, iMem, iMem+i
2b61c 2b 31 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20  +1, regTemp);.  
2b61d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b61e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
2b61f 6d 6d 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31 29  mm, regTemp, -1)
2b620 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2b621 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2b622 44 69 76 69 64 65 2c 20 69 4d 65 6d 2b 69 2b 31  Divide, iMem+i+1
2b623 2c 20 72 65 67 54 65 6d 70 2c 20 72 65 67 54 65  , regTemp, regTe
2b624 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  mp);.      sqlit
2b625 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2b626 4f 50 5f 54 6f 49 6e 74 2c 20 72 65 67 54 65 6d  OP_ToInt, regTem
2b627 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  p);.      sqlite
2b628 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2b629 50 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d  P_Concat, regTem
2b62a 70 2c 20 72 65 67 53 74 61 74 31 2c 20 72 65 67  p, regStat1, reg
2b62b 53 74 61 74 31 29 3b 0a 20 20 20 20 7d 0a 20 20  Stat1);.    }.  
2b62c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b62d 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op4(v, OP_MakeRe
2b62e 63 6f 72 64 2c 20 72 65 67 54 61 62 6e 61 6d 65  cord, regTabname
2b62f 2c 20 33 2c 20 72 65 67 52 65 63 2c 20 22 61 61  , 3, regRec, "aa
2b630 61 22 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  a", 0);.    sqli
2b631 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b632 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 53   OP_NewRowid, iS
2b633 74 61 74 43 75 72 2c 20 72 65 67 4e 65 77 52 6f  tatCur, regNewRo
2b634 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  wid);.    sqlite
2b635 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2b636 50 5f 49 6e 73 65 72 74 2c 20 69 53 74 61 74 43  P_Insert, iStatC
2b637 75 72 2c 20 72 65 67 52 65 63 2c 20 72 65 67 4e  ur, regRec, regN
2b638 65 77 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71  ewRowid);.    sq
2b639 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2b63a 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45  5(v, OPFLAG_APPE
2b63b 4e 44 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  ND);.  }..  /* I
2b63c 66 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20  f the table has 
2b63d 6e 6f 20 69 6e 64 69 63 65 73 2c 20 63 72 65 61  no indices, crea
2b63e 74 65 20 61 20 73 69 6e 67 6c 65 20 73 71 6c 69  te a single sqli
2b63f 74 65 5f 73 74 61 74 31 20 65 6e 74 72 79 0a 20  te_stat1 entry. 
2b640 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 4e   ** containing N
2b641 55 4c 4c 20 61 73 20 74 68 65 20 69 6e 64 65 78  ULL as the index
2b642 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 72 6f   name and the ro
2b643 77 20 63 6f 75 6e 74 20 61 73 20 74 68 65 20 63  w count as the c
2b644 6f 6e 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69  ontent..  */.  i
2b645 66 28 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d  f( pTab->pIndex=
2b646 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
2b647 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2b648 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 49 64 78  P_OpenRead, iIdx
2b649 43 75 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  Cur, pTab->tnum,
2b64a 20 69 44 62 29 3b 0a 20 20 20 20 56 64 62 65 43   iDb);.    VdbeC
2b64b 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c  omment((v, "%s",
2b64c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a   pTab->zName));.
2b64d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b64e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 75 6e  ddOp2(v, OP_Coun
2b64f 74 2c 20 69 49 64 78 43 75 72 2c 20 72 65 67 53  t, iIdxCur, regS
2b650 74 61 74 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  tat1);.    sqlit
2b651 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2b652 4f 50 5f 43 6c 6f 73 65 2c 20 69 49 64 78 43 75  OP_Close, iIdxCu
2b653 72 29 3b 0a 20 20 20 20 6a 5a 65 72 6f 52 6f 77  r);.    jZeroRow
2b654 73 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  s = sqlite3VdbeA
2b655 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 4e 6f  ddOp1(v, OP_IfNo
2b656 74 2c 20 72 65 67 53 74 61 74 31 29 3b 0a 20 20  t, regStat1);.  
2b657 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
2b658 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2b659 2c 20 6a 5a 65 72 6f 52 6f 77 73 29 3b 0a 20 20  , jZeroRows);.  
2b65a 20 20 6a 5a 65 72 6f 52 6f 77 73 20 3d 20 73 71    jZeroRows = sq
2b65b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
2b65c 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 7d  v, OP_Goto);.  }
2b65d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2b65e 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
2b65f 20 30 2c 20 72 65 67 49 64 78 6e 61 6d 65 29 3b   0, regIdxname);
2b660 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2b661 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  dOp4(v, OP_MakeR
2b662 65 63 6f 72 64 2c 20 72 65 67 54 61 62 6e 61 6d  ecord, regTabnam
2b663 65 2c 20 33 2c 20 72 65 67 52 65 63 2c 20 22 61  e, 3, regRec, "a
2b664 61 61 22 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  aa", 0);.  sqlit
2b665 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b666 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 53 74  OP_NewRowid, iSt
2b667 61 74 43 75 72 2c 20 72 65 67 4e 65 77 52 6f 77  atCur, regNewRow
2b668 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  id);.  sqlite3Vd
2b669 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
2b66a 6e 73 65 72 74 2c 20 69 53 74 61 74 43 75 72 2c  nsert, iStatCur,
2b66b 20 72 65 67 52 65 63 2c 20 72 65 67 4e 65 77 52   regRec, regNewR
2b66c 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33  owid);.  sqlite3
2b66d 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
2b66e 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a  OPFLAG_APPEND);.
2b66f 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 4d    if( pParse->nM
2b670 65 6d 3c 72 65 67 52 65 63 20 29 20 70 50 61 72  em<regRec ) pPar
2b671 73 65 2d 3e 6e 4d 65 6d 20 3d 20 72 65 67 52 65  se->nMem = regRe
2b672 63 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  c;.  sqlite3Vdbe
2b673 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 5a 65 72  JumpHere(v, jZer
2b674 6f 52 6f 77 73 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  oRows);.}.../*.*
2b675 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2b676 74 68 61 74 20 77 69 6c 6c 20 63 61 75 73 65 20  that will cause 
2b677 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
2b678 69 6e 64 65 78 20 61 6e 61 6c 79 73 69 73 20 74  index analysis t
2b679 6f 0a 2a 2a 20 62 65 20 6c 6f 61 64 65 64 20 69  o.** be loaded i
2b67a 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73  nto internal has
2b67b 68 20 74 61 62 6c 65 73 20 77 68 65 72 65 20 69  h tables where i
2b67c 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a  s can be used..*
2b67d 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f  /.static void lo
2b67e 61 64 41 6e 61 6c 79 73 69 73 28 50 61 72 73 65  adAnalysis(Parse
2b67f 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44   *pParse, int iD
2b680 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  b){.  Vdbe *v = 
2b681 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2b682 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20  Parse);.  if( v 
2b683 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2b684 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4c  beAddOp1(v, OP_L
2b685 6f 61 64 41 6e 61 6c 79 73 69 73 2c 20 69 44 62  oadAnalysis, iDb
2b686 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2b687 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
2b688 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e 20 61 6e  at will do an an
2b689 61 6c 79 73 69 73 20 6f 66 20 61 6e 20 65 6e 74  alysis of an ent
2b68a 69 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2f 0a  ire database.*/.
2b68b 73 74 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c  static void anal
2b68c 79 7a 65 44 61 74 61 62 61 73 65 28 50 61 72 73  yzeDatabase(Pars
2b68d 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
2b68e 44 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Db){.  sqlite3 *
2b68f 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2b690 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65  .  Schema *pSche
2b691 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  ma = db->aDb[iDb
2b692 5d 2e 70 53 63 68 65 6d 61 3b 20 20 20 20 2f 2a  ].pSchema;    /*
2b693 20 53 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62   Schema of datab
2b694 61 73 65 20 69 44 62 20 2a 2f 0a 20 20 48 61 73  ase iDb */.  Has
2b695 68 45 6c 65 6d 20 2a 6b 3b 0a 20 20 69 6e 74 20  hElem *k;.  int 
2b696 69 53 74 61 74 43 75 72 3b 0a 20 20 69 6e 74 20  iStatCur;.  int 
2b697 69 4d 65 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33  iMem;..  sqlite3
2b698 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
2b699 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69  ion(pParse, 0, i
2b69a 44 62 29 3b 0a 20 20 69 53 74 61 74 43 75 72 20  Db);.  iStatCur 
2b69b 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a  = pParse->nTab;.
2b69c 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 2b    pParse->nTab +
2b69d 3d 20 33 3b 0a 20 20 6f 70 65 6e 53 74 61 74 54  = 3;.  openStatT
2b69e 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62  able(pParse, iDb
2b69f 2c 20 69 53 74 61 74 43 75 72 2c 20 30 2c 20 30  , iStatCur, 0, 0
2b6a0 29 3b 0a 20 20 69 4d 65 6d 20 3d 20 70 50 61 72  );.  iMem = pPar
2b6a1 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 61 73  se->nMem+1;.  as
2b6a2 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68  sert( sqlite3Sch
2b6a3 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c  emaMutexHeld(db,
2b6a4 20 69 44 62 2c 20 30 29 20 29 3b 0a 20 20 66 6f   iDb, 0) );.  fo
2b6a5 72 28 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(k=sqliteHashFi
2b6a6 72 73 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62  rst(&pSchema->tb
2b6a7 6c 48 61 73 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c  lHash); k; k=sql
2b6a8 69 74 65 48 61 73 68 4e 65 78 74 28 6b 29 29 7b  iteHashNext(k)){
2b6a9 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
2b6aa 20 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74   = (Table*)sqlit
2b6ab 65 48 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20  eHashData(k);.  
2b6ac 20 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c    analyzeOneTabl
2b6ad 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
2b6ae 30 2c 20 69 53 74 61 74 43 75 72 2c 20 69 4d 65  0, iStatCur, iMe
2b6af 6d 29 3b 0a 20 20 7d 0a 20 20 6c 6f 61 64 41 6e  m);.  }.  loadAn
2b6b0 61 6c 79 73 69 73 28 70 50 61 72 73 65 2c 20 69  alysis(pParse, i
2b6b1 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  Db);.}../*.** Ge
2b6b2 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
2b6b3 20 77 69 6c 6c 20 64 6f 20 61 6e 20 61 6e 61 6c   will do an anal
2b6b4 79 73 69 73 20 6f 66 20 61 20 73 69 6e 67 6c 65  ysis of a single
2b6b5 20 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 61 20 64   table in.** a d
2b6b6 61 74 61 62 61 73 65 2e 20 20 49 66 20 70 4f 6e  atabase.  If pOn
2b6b7 6c 79 49 64 78 20 69 73 20 6e 6f 74 20 4e 55 4c  lyIdx is not NUL
2b6b8 4c 20 74 68 65 6e 20 69 74 20 69 73 20 61 20 73  L then it is a s
2b6b9 69 6e 67 6c 65 20 69 6e 64 65 78 0a 2a 2a 20 69  ingle index.** i
2b6ba 6e 20 70 54 61 62 20 74 68 61 74 20 73 68 6f 75  n pTab that shou
2b6bb 6c 64 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 0a  ld be analyzed..
2b6bc 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
2b6bd 6e 61 6c 79 7a 65 54 61 62 6c 65 28 50 61 72 73  nalyzeTable(Pars
2b6be 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65  e *pParse, Table
2b6bf 20 2a 70 54 61 62 2c 20 49 6e 64 65 78 20 2a 70   *pTab, Index *p
2b6c0 4f 6e 6c 79 49 64 78 29 7b 0a 20 20 69 6e 74 20  OnlyIdx){.  int 
2b6c1 69 44 62 3b 0a 20 20 69 6e 74 20 69 53 74 61 74  iDb;.  int iStat
2b6c2 43 75 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  Cur;..  assert( 
2b6c3 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 61 73 73  pTab!=0 );.  ass
2b6c4 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
2b6c5 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73  eHoldsAllMutexes
2b6c6 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a  (pParse->db) );.
2b6c7 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
2b6c8 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
2b6c9 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70  rse->db, pTab->p
2b6ca 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74  Schema);.  sqlit
2b6cb 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
2b6cc 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c  ation(pParse, 0,
2b6cd 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74 43 75   iDb);.  iStatCu
2b6ce 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
2b6cf 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  ;.  pParse->nTab
2b6d0 20 2b 3d 20 33 3b 0a 20 20 69 66 28 20 70 4f 6e   += 3;.  if( pOn
2b6d1 6c 79 49 64 78 20 29 7b 0a 20 20 20 20 6f 70 65  lyIdx ){.    ope
2b6d2 6e 53 74 61 74 54 61 62 6c 65 28 70 50 61 72 73  nStatTable(pPars
2b6d3 65 2c 20 69 44 62 2c 20 69 53 74 61 74 43 75 72  e, iDb, iStatCur
2b6d4 2c 20 70 4f 6e 6c 79 49 64 78 2d 3e 7a 4e 61 6d  , pOnlyIdx->zNam
2b6d5 65 2c 20 22 69 64 78 22 29 3b 0a 20 20 7d 65 6c  e, "idx");.  }el
2b6d6 73 65 7b 0a 20 20 20 20 6f 70 65 6e 53 74 61 74  se{.    openStat
2b6d7 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
2b6d8 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 54 61  b, iStatCur, pTa
2b6d9 62 2d 3e 7a 4e 61 6d 65 2c 20 22 74 62 6c 22 29  b->zName, "tbl")
2b6da 3b 0a 20 20 7d 0a 20 20 61 6e 61 6c 79 7a 65 4f  ;.  }.  analyzeO
2b6db 6e 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  neTable(pParse, 
2b6dc 70 54 61 62 2c 20 70 4f 6e 6c 79 49 64 78 2c 20  pTab, pOnlyIdx, 
2b6dd 69 53 74 61 74 43 75 72 2c 20 70 50 61 72 73 65  iStatCur, pParse
2b6de 2d 3e 6e 4d 65 6d 2b 31 29 3b 0a 20 20 6c 6f 61  ->nMem+1);.  loa
2b6df 64 41 6e 61 6c 79 73 69 73 28 70 50 61 72 73 65  dAnalysis(pParse
2b6e0 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  , iDb);.}../*.**
2b6e1 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
2b6e2 6f 72 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63  or the ANALYZE c
2b6e3 6f 6d 6d 61 6e 64 2e 20 20 54 68 65 20 70 61 72  ommand.  The par
2b6e4 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
2b6e5 6f 75 74 69 6e 65 0a 2a 2a 20 77 68 65 6e 20 69  outine.** when i
2b6e6 74 20 72 65 63 6f 67 6e 69 7a 65 73 20 61 6e 20  t recognizes an 
2b6e7 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e  ANALYZE command.
2b6e8 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e  .**.**        AN
2b6e9 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20 20 20  ALYZE           
2b6ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b6eb 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20   -- 1.**        
2b6ec 41 4e 41 4c 59 5a 45 20 20 3c 64 61 74 61 62 61  ANALYZE  <databa
2b6ed 73 65 3e 20 20 20 20 20 20 20 20 20 20 20 20 20  se>             
2b6ee 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20 20 20 20     -- 2.**      
2b6ef 20 20 41 4e 41 4c 59 5a 45 20 20 3f 3c 64 61 74    ANALYZE  ?<dat
2b6f0 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61  abase>.?<tablena
2b6f1 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20  me>  -- 3.**.** 
2b6f2 46 6f 72 6d 20 31 20 63 61 75 73 65 73 20 61 6c  Form 1 causes al
2b6f3 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c  l indices in all
2b6f4 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
2b6f5 73 65 73 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a  ses to be analyz
2b6f6 65 64 2e 0a 2a 2a 20 46 6f 72 6d 20 32 20 61 6e  ed..** Form 2 an
2b6f7 61 6c 79 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63  alyzes all indic
2b6f8 65 73 20 74 68 65 20 73 69 6e 67 6c 65 20 64 61  es the single da
2b6f9 74 61 62 61 73 65 20 6e 61 6d 65 64 2e 0a 2a 2a  tabase named..**
2b6fa 20 46 6f 72 6d 20 33 20 61 6e 61 6c 79 7a 65 73   Form 3 analyzes
2b6fb 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 61 73 73   all indices ass
2b6fc 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
2b6fd 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a 2f   named table..*/
2b6fe 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2b6ff 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6e 61 6c  void sqlite3Anal
2b700 79 7a 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  yze(Parse *pPars
2b701 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31  e, Token *pName1
2b702 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29  , Token *pName2)
2b703 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
2b704 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2b705 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69  int iDb;.  int i
2b706 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 44  ;.  char *z, *zD
2b707 62 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  b;.  Table *pTab
2b708 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  ;.  Index *pIdx;
2b709 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65  .  Token *pTable
2b70a 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 52 65 61 64  Name;..  /* Read
2b70b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
2b70c 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72 6f  hema. If an erro
2b70d 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65 20  r occurs, leave 
2b70e 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
2b70f 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69  .  ** and code i
2b710 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74  n pParse and ret
2b711 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61  urn NULL. */.  a
2b712 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
2b713 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
2b714 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29  es(pParse->db) )
2b715 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ;.  if( SQLITE_O
2b716 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63  K!=sqlite3ReadSc
2b717 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a  hema(pParse) ){.
2b718 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2b719 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65  .  assert( pName
2b71a 32 21 3d 30 20 7c 7c 20 70 4e 61 6d 65 31 3d 3d  2!=0 || pName1==
2b71b 30 20 29 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65  0 );.  if( pName
2b71c 31 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46  1==0 ){.    /* F
2b71d 6f 72 6d 20 31 3a 20 20 41 6e 61 6c 79 7a 65 20  orm 1:  Analyze 
2b71e 65 76 65 72 79 74 68 69 6e 67 20 2a 2f 0a 20 20  everything */.  
2b71f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
2b720 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
2b721 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63 6f 6e    if( i==1 ) con
2b722 74 69 6e 75 65 3b 20 20 2f 2a 20 44 6f 20 6e 6f  tinue;  /* Do no
2b723 74 20 61 6e 61 6c 79 7a 65 20 74 68 65 20 54 45  t analyze the TE
2b724 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  MP database */. 
2b725 20 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61       analyzeData
2b726 62 61 73 65 28 70 50 61 72 73 65 2c 20 69 29 3b  base(pParse, i);
2b727 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
2b728 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20  f( pName2->n==0 
2b729 29 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 32  ){.    /* Form 2
2b72a 3a 20 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 64  :  Analyze the d
2b72b 61 74 61 62 61 73 65 20 6f 72 20 74 61 62 6c 65  atabase or table
2b72c 20 6e 61 6d 65 64 20 2a 2f 0a 20 20 20 20 69 44   named */.    iD
2b72d 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44  b = sqlite3FindD
2b72e 62 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20  b(db, pName1);. 
2b72f 20 20 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b     if( iDb>=0 ){
2b730 0a 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 44 61  .      analyzeDa
2b731 74 61 62 61 73 65 28 70 50 61 72 73 65 2c 20 69  tabase(pParse, i
2b732 44 62 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  Db);.    }else{.
2b733 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65        z = sqlite
2b734 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
2b735 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20  b, pName1);.    
2b736 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20    if( z ){.     
2b737 20 20 20 69 66 28 20 28 70 49 64 78 20 3d 20 73     if( (pIdx = s
2b738 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
2b739 64 62 2c 20 7a 2c 20 30 29 29 21 3d 30 20 29 7b  db, z, 0))!=0 ){
2b73a 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61 6c 79  .          analy
2b73b 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  zeTable(pParse, 
2b73c 70 49 64 78 2d 3e 70 54 61 62 6c 65 2c 20 70 49  pIdx->pTable, pI
2b73d 64 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  dx);.        }el
2b73e 73 65 20 69 66 28 20 28 70 54 61 62 20 3d 20 73  se if( (pTab = s
2b73f 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
2b740 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c 20  e(pParse, 0, z, 
2b741 30 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  0))!=0 ){.      
2b742 20 20 20 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65      analyzeTable
2b743 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30  (pParse, pTab, 0
2b744 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
2b745 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2b746 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20  ee(db, z);.     
2b747 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
2b748 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 33 3a  {.    /* Form 3:
2b749 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 66 75 6c   Analyze the ful
2b74a 6c 79 20 71 75 61 6c 69 66 69 65 64 20 74 61 62  ly qualified tab
2b74b 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 69  le name */.    i
2b74c 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50  Db = sqlite3TwoP
2b74d 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  artName(pParse, 
2b74e 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20  pName1, pName2, 
2b74f 26 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20  &pTableName);.  
2b750 20 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a    if( iDb>=0 ){.
2b751 20 20 20 20 20 20 7a 44 62 20 3d 20 64 62 2d 3e        zDb = db->
2b752 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
2b753 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65        z = sqlite
2b754 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
2b755 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a  b, pTableName);.
2b756 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a 20        if( z ){. 
2b757 20 20 20 20 20 20 20 69 66 28 20 28 70 49 64 78         if( (pIdx
2b758 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e   = sqlite3FindIn
2b759 64 65 78 28 64 62 2c 20 7a 2c 20 7a 44 62 29 29  dex(db, z, zDb))
2b75a 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=0 ){.         
2b75b 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70 50   analyzeTable(pP
2b75c 61 72 73 65 2c 20 70 49 64 78 2d 3e 70 54 61 62  arse, pIdx->pTab
2b75d 6c 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20  le, pIdx);.     
2b75e 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 54     }else if( (pT
2b75f 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  ab = sqlite3Loca
2b760 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  teTable(pParse, 
2b761 30 2c 20 7a 2c 20 7a 44 62 29 29 21 3d 30 20 29  0, z, zDb))!=0 )
2b762 7b 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61 6c  {.          anal
2b763 79 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  yzeTable(pParse,
2b764 20 70 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 20   pTab, 0);.     
2b765 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
2b766 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2b767 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2b768 20 20 20 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a     .  }.}../*.**
2b769 20 55 73 65 64 20 74 6f 20 70 61 73 73 20 69 6e   Used to pass in
2b76a 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74  formation from t
2b76b 68 65 20 61 6e 61 6c 79 7a 65 72 20 72 65 61 64  he analyzer read
2b76c 65 72 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  er through to th
2b76d 65 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 72 6f  e.** callback ro
2b76e 75 74 69 6e 65 2e 0a 2a 2f 0a 74 79 70 65 64 65  utine..*/.typede
2b76f 66 20 73 74 72 75 63 74 20 61 6e 61 6c 79 73 69  f struct analysi
2b770 73 49 6e 66 6f 20 61 6e 61 6c 79 73 69 73 49 6e  sInfo analysisIn
2b771 66 6f 3b 0a 73 74 72 75 63 74 20 61 6e 61 6c 79  fo;.struct analy
2b772 73 69 73 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69  sisInfo {.  sqli
2b773 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74  te3 *db;.  const
2b774 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65   char *zDatabase
2b775 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ;.};../*.** This
2b776 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76   callback is inv
2b777 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61  oked once for ea
2b778 63 68 20 69 6e 64 65 78 20 77 68 65 6e 20 72 65  ch index when re
2b779 61 64 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c  ading the.** sql
2b77a 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e  ite_stat1 table.
2b77b 20 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72 67    .**.**     arg
2b77c 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74  v[0] = name of t
2b77d 68 65 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20  he table.**     
2b77e 61 72 67 76 5b 31 5d 20 3d 20 6e 61 6d 65 20 6f  argv[1] = name o
2b77f 66 20 74 68 65 20 69 6e 64 65 78 20 28 6d 69 67  f the index (mig
2b780 68 74 20 62 65 20 4e 55 4c 4c 29 0a 2a 2a 20 20  ht be NULL).**  
2b781 20 20 20 61 72 67 76 5b 32 5d 20 3d 20 72 65 73     argv[2] = res
2b782 75 6c 74 73 20 6f 66 20 61 6e 61 6c 79 73 69 73  ults of analysis
2b783 20 2d 20 6f 6e 20 69 6e 74 65 67 65 72 20 66 6f   - on integer fo
2b784 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2a  r each column.**
2b785 0a 2a 2a 20 45 6e 74 72 69 65 73 20 66 6f 72 20  .** Entries for 
2b786 77 68 69 63 68 20 61 72 67 76 5b 31 5d 3d 3d 4e  which argv[1]==N
2b787 55 4c 4c 20 73 69 6d 70 6c 79 20 72 65 63 6f 72  ULL simply recor
2b788 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  d the number of 
2b789 72 6f 77 73 20 69 6e 0a 2a 2a 20 74 68 65 20 74  rows in.** the t
2b78a 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
2b78b 69 6e 74 20 61 6e 61 6c 79 73 69 73 4c 6f 61 64  int analysisLoad
2b78c 65 72 28 76 6f 69 64 20 2a 70 44 61 74 61 2c 20  er(void *pData, 
2b78d 69 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a  int argc, char *
2b78e 2a 61 72 67 76 2c 20 63 68 61 72 20 2a 2a 4e 6f  *argv, char **No
2b78f 74 55 73 65 64 29 7b 0a 20 20 61 6e 61 6c 79 73  tUsed){.  analys
2b790 69 73 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20  isInfo *pInfo = 
2b791 28 61 6e 61 6c 79 73 69 73 49 6e 66 6f 2a 29 70  (analysisInfo*)p
2b792 44 61 74 61 3b 0a 20 20 49 6e 64 65 78 20 2a 70  Data;.  Index *p
2b793 49 6e 64 65 78 3b 0a 20 20 54 61 62 6c 65 20 2a  Index;.  Table *
2b794 70 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 69 2c  pTable;.  int i,
2b795 20 63 2c 20 6e 3b 0a 20 20 74 52 6f 77 63 6e 74   c, n;.  tRowcnt
2b796 20 76 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   v;.  const char
2b797 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   *z;..  assert( 
2b798 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20 55 4e 55  argc==3 );.  UNU
2b799 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
2b79a 6f 74 55 73 65 64 2c 20 61 72 67 63 29 3b 0a 0a  otUsed, argc);..
2b79b 20 20 69 66 28 20 61 72 67 76 3d 3d 30 20 7c 7c    if( argv==0 ||
2b79c 20 61 72 67 76 5b 30 5d 3d 3d 30 20 7c 7c 20 61   argv[0]==0 || a
2b79d 72 67 76 5b 32 5d 3d 3d 30 20 29 7b 0a 20 20 20  rgv[2]==0 ){.   
2b79e 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
2b79f 20 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65   pTable = sqlite
2b7a0 33 46 69 6e 64 54 61 62 6c 65 28 70 49 6e 66 6f  3FindTable(pInfo
2b7a1 2d 3e 64 62 2c 20 61 72 67 76 5b 30 5d 2c 20 70  ->db, argv[0], p
2b7a2 49 6e 66 6f 2d 3e 7a 44 61 74 61 62 61 73 65 29  Info->zDatabase)
2b7a3 3b 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d  ;.  if( pTable==
2b7a4 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
2b7a5 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 61 72 67  0;.  }.  if( arg
2b7a6 76 5b 31 5d 20 29 7b 0a 20 20 20 20 70 49 6e 64  v[1] ){.    pInd
2b7a7 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ex = sqlite3Find
2b7a8 49 6e 64 65 78 28 70 49 6e 66 6f 2d 3e 64 62 2c  Index(pInfo->db,
2b7a9 20 61 72 67 76 5b 31 5d 2c 20 70 49 6e 66 6f 2d   argv[1], pInfo-
2b7aa 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 7d  >zDatabase);.  }
2b7ab 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 64 65 78  else{.    pIndex
2b7ac 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20   = 0;.  }.  n = 
2b7ad 70 49 6e 64 65 78 20 3f 20 70 49 6e 64 65 78 2d  pIndex ? pIndex-
2b7ae 3e 6e 43 6f 6c 75 6d 6e 20 3a 20 30 3b 0a 20 20  >nColumn : 0;.  
2b7af 7a 20 3d 20 61 72 67 76 5b 32 5d 3b 0a 20 20 66  z = argv[2];.  f
2b7b0 6f 72 28 69 3d 30 3b 20 2a 7a 20 26 26 20 69 3c  or(i=0; *z && i<
2b7b1 3d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20  =n; i++){.    v 
2b7b2 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  = 0;.    while( 
2b7b3 28 63 3d 7a 5b 30 5d 29 3e 3d 27 30 27 20 26 26  (c=z[0])>='0' &&
2b7b4 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20   c<='9' ){.     
2b7b5 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20   v = v*10 + c - 
2b7b6 27 30 27 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a  '0';.      z++;.
2b7b7 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3d      }.    if( i=
2b7b8 3d 30 20 29 20 70 54 61 62 6c 65 2d 3e 6e 52 6f  =0 ) pTable->nRo
2b7b9 77 45 73 74 20 3d 20 76 3b 0a 20 20 20 20 69 66  wEst = v;.    if
2b7ba 28 20 70 49 6e 64 65 78 3d 3d 30 20 29 20 62 72  ( pIndex==0 ) br
2b7bb 65 61 6b 3b 0a 20 20 20 20 70 49 6e 64 65 78 2d  eak;.    pIndex-
2b7bc 3e 61 69 52 6f 77 45 73 74 5b 69 5d 20 3d 20 76  >aiRowEst[i] = v
2b7bd 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 20  ;.    if( *z==' 
2b7be 27 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 69 66 28  ' ) z++;.    if(
2b7bf 20 6d 65 6d 63 6d 70 28 7a 2c 20 22 75 6e 6f 72   memcmp(z, "unor
2b7c0 64 65 72 65 64 22 2c 20 31 30 29 3d 3d 30 20 29  dered", 10)==0 )
2b7c1 7b 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e  {.      pIndex->
2b7c2 62 55 6e 6f 72 64 65 72 65 64 20 3d 20 31 3b 0a  bUnordered = 1;.
2b7c3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b7c4 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2b7c5 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  0;.}../*.** If t
2b7c6 68 65 20 49 6e 64 65 78 2e 61 53 61 6d 70 6c 65  he Index.aSample
2b7c7 20 76 61 72 69 61 62 6c 65 20 69 73 20 6e 6f 74   variable is not
2b7c8 20 4e 55 4c 4c 2c 20 64 65 6c 65 74 65 20 74 68   NULL, delete th
2b7c9 65 20 61 53 61 6d 70 6c 65 5b 5d 20 61 72 72 61  e aSample[] arra
2b7ca 79 0a 2a 2a 20 61 6e 64 20 69 74 73 20 63 6f 6e  y.** and its con
2b7cb 74 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tents..*/.SQLITE
2b7cc 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2b7cd 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65 78  lite3DeleteIndex
2b7ce 53 61 6d 70 6c 65 73 28 73 71 6c 69 74 65 33 20  Samples(sqlite3 
2b7cf 2a 64 62 2c 20 49 6e 64 65 78 20 2a 70 49 64 78  *db, Index *pIdx
2b7d0 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
2b7d1 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 0a 20 20  _ENABLE_STAT3.  
2b7d2 69 66 28 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c  if( pIdx->aSampl
2b7d3 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a  e ){.    int j;.
2b7d4 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
2b7d5 49 64 78 2d 3e 6e 53 61 6d 70 6c 65 3b 20 6a 2b  Idx->nSample; j+
2b7d6 2b 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 53  +){.      IndexS
2b7d7 61 6d 70 6c 65 20 2a 70 20 3d 20 26 70 49 64 78  ample *p = &pIdx
2b7d8 2d 3e 61 53 61 6d 70 6c 65 5b 6a 5d 3b 0a 20 20  ->aSample[j];.  
2b7d9 20 20 20 20 69 66 28 20 70 2d 3e 65 54 79 70 65      if( p->eType
2b7da 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 20 7c 7c  ==SQLITE_TEXT ||
2b7db 20 70 2d 3e 65 54 79 70 65 3d 3d 53 51 4c 49 54   p->eType==SQLIT
2b7dc 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20  E_BLOB ){.      
2b7dd 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2b7de 64 62 2c 20 70 2d 3e 75 2e 7a 29 3b 0a 20 20 20  db, p->u.z);.   
2b7df 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
2b7e0 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2b7e1 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 29 3b   pIdx->aSample);
2b7e2 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 20 26 26  .  }.  if( db &&
2b7e3 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 65   db->pnBytesFree
2b7e4 64 3d 3d 30 20 29 7b 0a 20 20 20 20 70 49 64 78  d==0 ){.    pIdx
2b7e5 2d 3e 6e 53 61 6d 70 6c 65 20 3d 20 30 3b 0a 20  ->nSample = 0;. 
2b7e6 20 20 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65     pIdx->aSample
2b7e7 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a   = 0;.  }.#else.
2b7e8 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
2b7e9 45 52 28 64 62 29 3b 0a 20 20 55 4e 55 53 45 44  ER(db);.  UNUSED
2b7ea 5f 50 41 52 41 4d 45 54 45 52 28 70 49 64 78 29  _PARAMETER(pIdx)
2b7eb 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 23 69 66 64  ;.#endif.}..#ifd
2b7ec 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
2b7ed 5f 53 54 41 54 33 0a 2f 2a 0a 2a 2a 20 4c 6f 61  _STAT3./*.** Loa
2b7ee 64 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 74  d content from t
2b7ef 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 33 20  he sqlite_stat3 
2b7f0 74 61 62 6c 65 20 69 6e 74 6f 20 74 68 65 20 49  table into the I
2b7f1 6e 64 65 78 2e 61 53 61 6d 70 6c 65 5b 5d 0a 2a  ndex.aSample[].*
2b7f2 2a 20 61 72 72 61 79 73 20 6f 66 20 61 6c 6c 20  * arrays of all 
2b7f3 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 73 74 61 74  indices..*/.stat
2b7f4 69 63 20 69 6e 74 20 6c 6f 61 64 53 74 61 74 33  ic int loadStat3
2b7f5 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
2b7f6 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a  nst char *zDb){.
2b7f7 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
2b7f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b7f9 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 73 20  /* Result codes 
2b7fa 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 73  from subroutines
2b7fb 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
2b7fc 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 20 20  mt *pStmt = 0;  
2b7fd 20 20 20 20 2f 2a 20 41 6e 20 53 51 4c 20 73 74      /* An SQL st
2b7fe 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 72 75  atement being ru
2b7ff 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71  n */.  char *zSq
2b800 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
2b801 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20       /* Text of 
2b802 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
2b803 74 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 50  t */.  Index *pP
2b804 72 65 76 49 64 78 20 3d 20 30 3b 20 20 20 20 20  revIdx = 0;     
2b805 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73       /* Previous
2b806 20 69 6e 64 65 78 20 69 6e 20 74 68 65 20 6c 6f   index in the lo
2b807 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 20  op */.  int idx 
2b808 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
2b809 20 20 20 20 20 20 2f 2a 20 73 6c 6f 74 20 69 6e        /* slot in
2b80a 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 5b 5d   pIdx->aSample[]
2b80b 20 66 6f 72 20 6e 65 78 74 20 73 61 6d 70 6c 65   for next sample
2b80c 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65 3b   */.  int eType;
2b80d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b80e 20 20 20 20 2f 2a 20 44 61 74 61 74 79 70 65 20      /* Datatype 
2b80f 6f 66 20 61 20 73 61 6d 70 6c 65 20 2a 2f 0a 20  of a sample */. 
2b810 20 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a 70 53   IndexSample *pS
2b811 61 6d 70 6c 65 3b 20 20 20 20 20 20 20 20 20 2f  ample;         /
2b812 2a 20 41 20 73 6c 6f 74 20 69 6e 20 70 49 64 78  * A slot in pIdx
2b813 2d 3e 61 53 61 6d 70 6c 65 5b 5d 20 2a 2f 0a 0a  ->aSample[] */..
2b814 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6c 6f    assert( db->lo
2b815 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
2b816 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 73 71  ==0 );.  if( !sq
2b817 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
2b818 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 33  b, "sqlite_stat3
2b819 22 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 72  ", zDb) ){.    r
2b81a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2b81b 0a 20 20 7d 0a 0a 20 20 7a 53 71 6c 20 3d 20 73  .  }..  zSql = s
2b81c 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2b81d 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54  , .      "SELECT
2b81e 20 69 64 78 2c 63 6f 75 6e 74 28 2a 29 20 46 52   idx,count(*) FR
2b81f 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61  OM %Q.sqlite_sta
2b820 74 33 22 0a 20 20 20 20 20 20 22 20 47 52 4f 55  t3".      " GROU
2b821 50 20 42 59 20 69 64 78 22 2c 20 7a 44 62 29 3b  P BY idx", zDb);
2b822 0a 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a  .  if( !zSql ){.
2b823 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2b824 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72  E_NOMEM;.  }.  r
2b825 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70  c = sqlite3_prep
2b826 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31  are(db, zSql, -1
2b827 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20  , &pStmt, 0);.  
2b828 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2b829 2c 20 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 72  , zSql);.  if( r
2b82a 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  c ) return rc;..
2b82b 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
2b82c 5f 73 74 65 70 28 70 53 74 6d 74 29 3d 3d 53 51  _step(pStmt)==SQ
2b82d 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
2b82e 63 68 61 72 20 2a 7a 49 6e 64 65 78 3b 20 20 20  char *zIndex;   
2b82f 2f 2a 20 49 6e 64 65 78 20 6e 61 6d 65 20 2a 2f  /* Index name */
2b830 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78  .    Index *pIdx
2b831 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20  ;    /* Pointer 
2b832 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 62 6a  to the index obj
2b833 65 63 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ect */.    int n
2b834 53 61 6d 70 6c 65 3b 20 20 20 20 2f 2a 20 4e 75  Sample;    /* Nu
2b835 6d 62 65 72 20 6f 66 20 73 61 6d 70 6c 65 73 20  mber of samples 
2b836 2a 2f 0a 0a 20 20 20 20 7a 49 6e 64 65 78 20 3d  */..    zIndex =
2b837 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
2b838 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53 74  _column_text(pSt
2b839 6d 74 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  mt, 0);.    if( 
2b83a 7a 49 6e 64 65 78 3d 3d 30 20 29 20 63 6f 6e 74  zIndex==0 ) cont
2b83b 69 6e 75 65 3b 0a 20 20 20 20 6e 53 61 6d 70 6c  inue;.    nSampl
2b83c 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  e = sqlite3_colu
2b83d 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2c 20 31 29  mn_int(pStmt, 1)
2b83e 3b 0a 20 20 20 20 70 49 64 78 20 3d 20 73 71 6c  ;.    pIdx = sql
2b83f 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62  ite3FindIndex(db
2b840 2c 20 7a 49 6e 64 65 78 2c 20 7a 44 62 29 3b 0a  , zIndex, zDb);.
2b841 20 20 20 20 69 66 28 20 70 49 64 78 3d 3d 30 20      if( pIdx==0 
2b842 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2b843 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 53  assert( pIdx->nS
2b844 61 6d 70 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20  ample==0 );.    
2b845 70 49 64 78 2d 3e 6e 53 61 6d 70 6c 65 20 3d 20  pIdx->nSample = 
2b846 6e 53 61 6d 70 6c 65 3b 0a 20 20 20 20 70 49 64  nSample;.    pId
2b847 78 2d 3e 61 53 61 6d 70 6c 65 20 3d 20 73 71 6c  x->aSample = sql
2b848 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
2b849 28 64 62 2c 20 6e 53 61 6d 70 6c 65 2a 73 69 7a  (db, nSample*siz
2b84a 65 6f 66 28 49 6e 64 65 78 53 61 6d 70 6c 65 29  eof(IndexSample)
2b84b 29 3b 0a 20 20 20 20 70 49 64 78 2d 3e 61 76 67  );.    pIdx->avg
2b84c 45 71 20 3d 20 70 49 64 78 2d 3e 61 69 52 6f 77  Eq = pIdx->aiRow
2b84d 45 73 74 5b 31 5d 3b 0a 20 20 20 20 69 66 28 20  Est[1];.    if( 
2b84e 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 3d 3d 30  pIdx->aSample==0
2b84f 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61   ){.      db->ma
2b850 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
2b851 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69        sqlite3_fi
2b852 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20  nalize(pStmt);. 
2b853 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
2b854 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
2b855 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
2b856 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d  e3_finalize(pStm
2b857 74 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  t);.  if( rc ) r
2b858 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 7a 53 71  eturn rc;..  zSq
2b859 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  l = sqlite3MPrin
2b85a 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53  tf(db, .      "S
2b85b 45 4c 45 43 54 20 69 64 78 2c 6e 65 71 2c 6e 6c  ELECT idx,neq,nl
2b85c 74 2c 6e 64 6c 74 2c 73 61 6d 70 6c 65 20 46 52  t,ndlt,sample FR
2b85d 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61  OM %Q.sqlite_sta
2b85e 74 33 22 2c 20 7a 44 62 29 3b 0a 20 20 69 66 28  t3", zDb);.  if(
2b85f 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72 65   !zSql ){.    re
2b860 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
2b861 4d 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71  M;.  }.  rc = sq
2b862 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 64 62  lite3_prepare(db
2b863 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
2b864 6d 74 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  mt, 0);.  sqlite
2b865 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c  3DbFree(db, zSql
2b866 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
2b867 74 75 72 6e 20 72 63 3b 0a 0a 20 20 77 68 69 6c  turn rc;..  whil
2b868 65 28 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  e( sqlite3_step(
2b869 70 53 74 6d 74 29 3d 3d 53 51 4c 49 54 45 5f 52  pStmt)==SQLITE_R
2b86a 4f 57 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  OW ){.    char *
2b86b 7a 49 6e 64 65 78 3b 20 20 20 2f 2a 20 49 6e 64  zIndex;   /* Ind
2b86c 65 78 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 49  ex name */.    I
2b86d 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 2f  ndex *pIdx;    /
2b86e 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
2b86f 20 69 6e 64 65 78 20 6f 62 6a 65 63 74 20 2a 2f   index object */
2b870 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20  .    int i;     
2b871 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
2b872 6e 74 65 72 20 2a 2f 0a 20 20 20 20 74 52 6f 77  nter */.    tRow
2b873 63 6e 74 20 73 75 6d 45 71 3b 20 20 2f 2a 20 53  cnt sumEq;  /* S
2b874 75 6d 20 6f 66 20 74 68 65 20 6e 45 71 20 76 61  um of the nEq va
2b875 6c 75 65 73 20 2a 2f 0a 0a 20 20 20 20 7a 49 6e  lues */..    zIn
2b876 64 65 78 20 3d 20 28 63 68 61 72 20 2a 29 73 71  dex = (char *)sq
2b877 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
2b878 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20  t(pStmt, 0);.   
2b879 20 69 66 28 20 7a 49 6e 64 65 78 3d 3d 30 20 29   if( zIndex==0 )
2b87a 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70   continue;.    p
2b87b 49 64 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Idx = sqlite3Fin
2b87c 64 49 6e 64 65 78 28 64 62 2c 20 7a 49 6e 64 65  dIndex(db, zInde
2b87d 78 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28  x, zDb);.    if(
2b87e 20 70 49 64 78 3d 3d 30 20 29 20 63 6f 6e 74 69   pIdx==0 ) conti
2b87f 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 49 64  nue;.    if( pId
2b880 78 3d 3d 70 50 72 65 76 49 64 78 20 29 7b 0a 20  x==pPrevIdx ){. 
2b881 20 20 20 20 20 69 64 78 2b 2b 3b 0a 20 20 20 20       idx++;.    
2b882 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 72  }else{.      pPr
2b883 65 76 49 64 78 20 3d 20 70 49 64 78 3b 0a 20 20  evIdx = pIdx;.  
2b884 20 20 20 20 69 64 78 20 3d 20 30 3b 0a 20 20 20      idx = 0;.   
2b885 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69   }.    assert( i
2b886 64 78 3c 70 49 64 78 2d 3e 6e 53 61 6d 70 6c 65  dx<pIdx->nSample
2b887 20 29 3b 0a 20 20 20 20 70 53 61 6d 70 6c 65 20   );.    pSample 
2b888 3d 20 26 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65  = &pIdx->aSample
2b889 5b 69 64 78 5d 3b 0a 20 20 20 20 70 53 61 6d 70  [idx];.    pSamp
2b88a 6c 65 2d 3e 6e 45 71 20 3d 20 28 74 52 6f 77 63  le->nEq = (tRowc
2b88b 6e 74 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  nt)sqlite3_colum
2b88c 6e 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c 20 31  n_int64(pStmt, 1
2b88d 29 3b 0a 20 20 20 20 70 53 61 6d 70 6c 65 2d 3e  );.    pSample->
2b88e 6e 4c 74 20 3d 20 28 74 52 6f 77 63 6e 74 29 73  nLt = (tRowcnt)s
2b88f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
2b890 74 36 34 28 70 53 74 6d 74 2c 20 32 29 3b 0a 20  t64(pStmt, 2);. 
2b891 20 20 20 70 53 61 6d 70 6c 65 2d 3e 6e 44 4c 74     pSample->nDLt
2b892 20 3d 20 28 74 52 6f 77 63 6e 74 29 73 71 6c 69   = (tRowcnt)sqli
2b893 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
2b894 28 70 53 74 6d 74 2c 20 33 29 3b 0a 20 20 20 20  (pStmt, 3);.    
2b895 69 66 28 20 69 64 78 3d 3d 70 49 64 78 2d 3e 6e  if( idx==pIdx->n
2b896 53 61 6d 70 6c 65 2d 31 20 29 7b 0a 20 20 20 20  Sample-1 ){.    
2b897 20 20 69 66 28 20 70 53 61 6d 70 6c 65 2d 3e 6e    if( pSample->n
2b898 44 4c 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  DLt>0 ){.       
2b899 20 66 6f 72 28 69 3d 30 2c 20 73 75 6d 45 71 3d   for(i=0, sumEq=
2b89a 30 3b 20 69 3c 3d 69 64 78 2d 31 3b 20 69 2b 2b  0; i<=idx-1; i++
2b89b 29 20 73 75 6d 45 71 20 2b 3d 20 70 49 64 78 2d  ) sumEq += pIdx-
2b89c 3e 61 53 61 6d 70 6c 65 5b 69 5d 2e 6e 45 71 3b  >aSample[i].nEq;
2b89d 0a 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 61  .        pIdx->a
2b89e 76 67 45 71 20 3d 20 28 70 53 61 6d 70 6c 65 2d  vgEq = (pSample-
2b89f 3e 6e 4c 74 20 2d 20 73 75 6d 45 71 29 2f 70 53  >nLt - sumEq)/pS
2b8a0 61 6d 70 6c 65 2d 3e 6e 44 4c 74 3b 0a 20 20 20  ample->nDLt;.   
2b8a1 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
2b8a2 49 64 78 2d 3e 61 76 67 45 71 3c 3d 30 20 29 20  Idx->avgEq<=0 ) 
2b8a3 70 49 64 78 2d 3e 61 76 67 45 71 20 3d 20 31 3b  pIdx->avgEq = 1;
2b8a4 0a 20 20 20 20 7d 0a 20 20 20 20 65 54 79 70 65  .    }.    eType
2b8a5 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
2b8a6 6e 5f 74 79 70 65 28 70 53 74 6d 74 2c 20 34 29  n_type(pStmt, 4)
2b8a7 3b 0a 20 20 20 20 70 53 61 6d 70 6c 65 2d 3e 65  ;.    pSample->e
2b8a8 54 79 70 65 20 3d 20 28 75 38 29 65 54 79 70 65  Type = (u8)eType
2b8a9 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 65 54  ;.    switch( eT
2b8aa 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73  ype ){.      cas
2b8ab 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  e SQLITE_INTEGER
2b8ac 3a 20 7b 0a 20 20 20 20 20 20 20 20 70 53 61 6d  : {.        pSam
2b8ad 70 6c 65 2d 3e 75 2e 69 20 3d 20 73 71 6c 69 74  ple->u.i = sqlit
2b8ae 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
2b8af 70 53 74 6d 74 2c 20 34 29 3b 0a 20 20 20 20 20  pStmt, 4);.     
2b8b0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2b8b1 7d 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c  }.      case SQL
2b8b2 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20  ITE_FLOAT: {.   
2b8b3 20 20 20 20 20 70 53 61 6d 70 6c 65 2d 3e 75 2e       pSample->u.
2b8b4 72 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  r = sqlite3_colu
2b8b5 6d 6e 5f 64 6f 75 62 6c 65 28 70 53 74 6d 74 2c  mn_double(pStmt,
2b8b6 20 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65   4);.        bre
2b8b7 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2b8b8 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55    case SQLITE_NU
2b8b9 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 20 20 62 72  LL: {.        br
2b8ba 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2b8bb 20 20 20 64 65 66 61 75 6c 74 3a 20 61 73 73 65     default: asse
2b8bc 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54  rt( eType==SQLIT
2b8bd 45 5f 54 45 58 54 20 7c 7c 20 65 54 79 70 65 3d  E_TEXT || eType=
2b8be 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 3b 20  =SQLITE_BLOB ); 
2b8bf 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  {.        const 
2b8c0 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74  char *z = (const
2b8c1 20 63 68 61 72 20 2a 29 28 0a 20 20 20 20 20 20   char *)(.      
2b8c2 20 20 20 20 20 20 20 20 28 65 54 79 70 65 3d 3d          (eType==
2b8c3 53 51 4c 49 54 45 5f 42 4c 4f 42 29 20 3f 0a 20  SQLITE_BLOB) ?. 
2b8c4 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2b8c5 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
2b8c6 28 70 53 74 6d 74 2c 20 34 29 3a 0a 20 20 20 20  (pStmt, 4):.    
2b8c7 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2b8c8 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53  3_column_text(pS
2b8c9 74 6d 74 2c 20 34 29 0a 20 20 20 20 20 20 20 20  tmt, 4).        
2b8ca 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e     );.        in
2b8cb 74 20 6e 20 3d 20 7a 20 3f 20 73 71 6c 69 74 65  t n = z ? sqlite
2b8cc 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70  3_column_bytes(p
2b8cd 53 74 6d 74 2c 20 34 29 20 3a 20 30 3b 0a 20 20  Stmt, 4) : 0;.  
2b8ce 20 20 20 20 20 20 70 53 61 6d 70 6c 65 2d 3e 6e        pSample->n
2b8cf 42 79 74 65 20 3d 20 6e 3b 0a 20 20 20 20 20 20  Byte = n;.      
2b8d0 20 20 69 66 28 20 6e 20 3c 20 31 29 7b 0a 20 20    if( n < 1){.  
2b8d1 20 20 20 20 20 20 20 20 70 53 61 6d 70 6c 65 2d          pSample-
2b8d2 3e 75 2e 7a 20 3d 20 30 3b 0a 20 20 20 20 20 20  >u.z = 0;.      
2b8d3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2b8d4 20 20 20 70 53 61 6d 70 6c 65 2d 3e 75 2e 7a 20     pSample->u.z 
2b8d5 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2b8d6 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20  cRaw(db, n);.   
2b8d7 20 20 20 20 20 20 20 69 66 28 20 70 53 61 6d 70         if( pSamp
2b8d8 6c 65 2d 3e 75 2e 7a 3d 3d 30 20 29 7b 0a 20 20  le->u.z==0 ){.  
2b8d9 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61            db->ma
2b8da 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
2b8db 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2b8dc 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74  te3_finalize(pSt
2b8dd 6d 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  mt);.           
2b8de 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
2b8df 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20  OMEM;.          
2b8e0 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63  }.          memc
2b8e1 70 79 28 70 53 61 6d 70 6c 65 2d 3e 75 2e 7a 2c  py(pSample->u.z,
2b8e2 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20   z, n);.        
2b8e3 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
2b8e4 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c    }.  return sql
2b8e5 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
2b8e6 74 6d 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  tmt);.}.#endif /
2b8e7 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
2b8e8 53 54 41 54 33 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  STAT3 */../*.** 
2b8e9 4c 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74  Load the content
2b8ea 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73   of the sqlite_s
2b8eb 74 61 74 31 20 61 6e 64 20 73 71 6c 69 74 65 5f  tat1 and sqlite_
2b8ec 73 74 61 74 33 20 74 61 62 6c 65 73 2e 20 54 68  stat3 tables. Th
2b8ed 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66  e.** contents of
2b8ee 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 61 72   sqlite_stat1 ar
2b8ef 65 20 75 73 65 64 20 74 6f 20 70 6f 70 75 6c 61  e used to popula
2b8f0 74 65 20 74 68 65 20 49 6e 64 65 78 2e 61 69 52  te the Index.aiR
2b8f1 6f 77 45 73 74 5b 5d 0a 2a 2a 20 61 72 72 61 79  owEst[].** array
2b8f2 73 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20  s. The contents 
2b8f3 6f 66 20 73 71 6c 69 74 65 5f 73 74 61 74 33 20  of sqlite_stat3 
2b8f4 61 72 65 20 75 73 65 64 20 74 6f 20 70 6f 70 75  are used to popu
2b8f5 6c 61 74 65 20 74 68 65 0a 2a 2a 20 49 6e 64 65  late the.** Inde
2b8f6 78 2e 61 53 61 6d 70 6c 65 5b 5d 20 61 72 72 61  x.aSample[] arra
2b8f7 79 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ys..**.** If the
2b8f8 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
2b8f9 62 6c 65 20 69 73 20 6e 6f 74 20 70 72 65 73 65  ble is not prese
2b8fa 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nt in the databa
2b8fb 73 65 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  se, SQLITE_ERROR
2b8fc 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e  .** is returned.
2b8fd 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 65   In this case, e
2b8fe 76 65 6e 20 69 66 20 53 51 4c 49 54 45 5f 45 4e  ven if SQLITE_EN
2b8ff 41 42 4c 45 5f 53 54 41 54 33 20 77 61 73 20 64  ABLE_STAT3 was d
2b900 65 66 69 6e 65 64 20 0a 2a 2a 20 64 75 72 69 6e  efined .** durin
2b901 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 61 6e  g compilation an
2b902 64 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  d the sqlite_sta
2b903 74 33 20 74 61 62 6c 65 20 69 73 20 70 72 65 73  t3 table is pres
2b904 65 6e 74 2c 20 6e 6f 20 64 61 74 61 20 69 73 20  ent, no data is 
2b905 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 69 74  .** read from it
2b906 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ..**.** If SQLIT
2b907 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 20 77  E_ENABLE_STAT3 w
2b908 61 73 20 64 65 66 69 6e 65 64 20 64 75 72 69 6e  as defined durin
2b909 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 61 6e  g compilation an
2b90a 64 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65  d the .** sqlite
2b90b 5f 73 74 61 74 33 20 74 61 62 6c 65 20 69 73 20  _stat3 table is 
2b90c 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 20 74  not present in t
2b90d 68 65 20 64 61 74 61 62 61 73 65 2c 20 53 51 4c  he database, SQL
2b90e 49 54 45 5f 45 52 52 4f 52 20 69 73 0a 2a 2a 20  ITE_ERROR is.** 
2b90f 72 65 74 75 72 6e 65 64 2e 20 48 6f 77 65 76 65  returned. Howeve
2b910 72 2c 20 69 6e 20 74 68 69 73 20 63 61 73 65 2c  r, in this case,
2b911 20 64 61 74 61 20 69 73 20 72 65 61 64 20 66 72   data is read fr
2b912 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  om the sqlite_st
2b913 61 74 31 0a 2a 2a 20 74 61 62 6c 65 20 28 69 66  at1.** table (if
2b914 20 69 74 20 69 73 20 70 72 65 73 65 6e 74 29 20   it is present) 
2b915 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
2b916 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 4f 4f  ..**.** If an OO
2b917 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  M error occurs, 
2b918 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c  this function al
2b919 77 61 79 73 20 73 65 74 73 20 64 62 2d 3e 6d 61  ways sets db->ma
2b91a 6c 6c 6f 63 46 61 69 6c 65 64 2e 0a 2a 2a 20 54  llocFailed..** T
2b91b 68 69 73 20 6d 65 61 6e 73 20 69 66 20 74 68 65  his means if the
2b91c 20 63 61 6c 6c 65 72 20 64 6f 65 73 20 6e 6f 74   caller does not
2b91d 20 63 61 72 65 20 61 62 6f 75 74 20 6f 74 68 65   care about othe
2b91e 72 20 65 72 72 6f 72 73 2c 20 74 68 65 20 72 65  r errors, the re
2b91f 74 75 72 6e 0a 2a 2a 20 63 6f 64 65 20 6d 61 79  turn.** code may
2b920 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a   be ignored..*/.
2b921 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2b922 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73  nt sqlite3Analys
2b923 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 20 2a  isLoad(sqlite3 *
2b924 64 62 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20  db, int iDb){.  
2b925 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 73 49 6e  analysisInfo sIn
2b926 66 6f 3b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a  fo;.  HashElem *
2b927 69 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b  i;.  char *zSql;
2b928 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
2b929 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20  sert( iDb>=0 && 
2b92a 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  iDb<db->nDb );. 
2b92b 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62   assert( db->aDb
2b92c 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0a  [iDb].pBt!=0 );.
2b92d 0a 20 20 2f 2a 20 43 6c 65 61 72 20 61 6e 79 20  .  /* Clear any 
2b92e 70 72 69 6f 72 20 73 74 61 74 69 73 74 69 63 73  prior statistics
2b92f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71   */.  assert( sq
2b930 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78  lite3SchemaMutex
2b931 48 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29  Held(db, iDb, 0)
2b932 20 29 3b 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69   );.  for(i=sqli
2b933 74 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d  teHashFirst(&db-
2b934 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
2b935 61 2d 3e 69 64 78 48 61 73 68 29 3b 69 3b 69 3d  a->idxHash);i;i=
2b936 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69  sqliteHashNext(i
2b937 29 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70  )){.    Index *p
2b938 49 64 78 20 3d 20 73 71 6c 69 74 65 48 61 73 68  Idx = sqliteHash
2b939 44 61 74 61 28 69 29 3b 0a 20 20 20 20 73 71 6c  Data(i);.    sql
2b93a 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73  ite3DefaultRowEs
2b93b 74 28 70 49 64 78 29 3b 0a 23 69 66 64 65 66 20  t(pIdx);.#ifdef 
2b93c 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
2b93d 41 54 33 0a 20 20 20 20 73 71 6c 69 74 65 33 44  AT3.    sqlite3D
2b93e 65 6c 65 74 65 49 6e 64 65 78 53 61 6d 70 6c 65  eleteIndexSample
2b93f 73 28 64 62 2c 20 70 49 64 78 29 3b 0a 20 20 20  s(db, pIdx);.   
2b940 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 20 3d   pIdx->aSample =
2b941 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a   0;.#endif.  }..
2b942 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61    /* Check to ma
2b943 6b 65 20 73 75 72 65 20 74 68 65 20 73 71 6c 69  ke sure the sqli
2b944 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 65  te_stat1 table e
2b945 78 69 73 74 73 20 2a 2f 0a 20 20 73 49 6e 66 6f  xists */.  sInfo
2b946 2e 64 62 20 3d 20 64 62 3b 0a 20 20 73 49 6e 66  .db = db;.  sInf
2b947 6f 2e 7a 44 61 74 61 62 61 73 65 20 3d 20 64 62  o.zDatabase = db
2b948 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
2b949 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46  ;.  if( sqlite3F
2b94a 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71  indTable(db, "sq
2b94b 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 73 49 6e  lite_stat1", sIn
2b94c 66 6f 2e 7a 44 61 74 61 62 61 73 65 29 3d 3d 30  fo.zDatabase)==0
2b94d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
2b94e 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
2b94f 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 6e 65 77 20  ..  /* Load new 
2b950 73 74 61 74 69 73 74 69 63 73 20 6f 75 74 20 6f  statistics out o
2b951 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  f the sqlite_sta
2b952 74 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7a 53  t1 table */.  zS
2b953 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  ql = sqlite3MPri
2b954 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 22  ntf(db, .      "
2b955 53 45 4c 45 43 54 20 74 62 6c 2c 69 64 78 2c 73  SELECT tbl,idx,s
2b956 74 61 74 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69  tat FROM %Q.sqli
2b957 74 65 5f 73 74 61 74 31 22 2c 20 73 49 6e 66 6f  te_stat1", sInfo
2b958 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69  .zDatabase);.  i
2b959 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20  f( zSql==0 ){.  
2b95a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
2b95b 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  MEM;.  }else{.  
2b95c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65    rc = sqlite3_e
2b95d 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 61 6e  xec(db, zSql, an
2b95e 61 6c 79 73 69 73 4c 6f 61 64 65 72 2c 20 26 73  alysisLoader, &s
2b95f 49 6e 66 6f 2c 20 30 29 3b 0a 20 20 20 20 73 71  Info, 0);.    sq
2b960 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2b961 7a 53 71 6c 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f  zSql);.  }...  /
2b962 2a 20 4c 6f 61 64 20 74 68 65 20 73 74 61 74 69  * Load the stati
2b963 73 74 69 63 73 20 66 72 6f 6d 20 74 68 65 20 73  stics from the s
2b964 71 6c 69 74 65 5f 73 74 61 74 33 20 74 61 62 6c  qlite_stat3 tabl
2b965 65 2e 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e. */.#ifdef SQL
2b966 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33  ITE_ENABLE_STAT3
2b967 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2b968 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20  E_OK ){.    int 
2b969 6c 6f 6f 6b 61 73 69 64 65 45 6e 61 62 6c 65 64  lookasideEnabled
2b96a 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65   = db->lookaside
2b96b 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 20 20 64  .bEnabled;.    d
2b96c 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e  b->lookaside.bEn
2b96d 61 62 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 72  abled = 0;.    r
2b96e 63 20 3d 20 6c 6f 61 64 53 74 61 74 33 28 64 62  c = loadStat3(db
2b96f 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73  , sInfo.zDatabas
2b970 65 29 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  e);.    db->look
2b971 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d  aside.bEnabled =
2b972 20 6c 6f 6f 6b 61 73 69 64 65 45 6e 61 62 6c 65   lookasideEnable
2b973 64 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  d;.  }.#endif.. 
2b974 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2b975 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 64 62 2d  NOMEM ){.    db-
2b976 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
2b977 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
2b978 72 63 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20 2f  rc;.}...#endif /
2b979 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e  * SQLITE_OMIT_AN
2b97a 41 4c 59 5a 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  ALYZE */../*****
2b97b 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2b97c 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a   analyze.c *****
2b97d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b97e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b97f 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2b980 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2b981 66 69 6c 65 20 61 74 74 61 63 68 2e 63 20 2a 2a  file attach.c **
2b982 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b983 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b984 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
2b985 32 30 30 33 20 41 70 72 69 6c 20 36 0a 2a 2a 0a  2003 April 6.**.
2b986 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2b987 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2b988 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2b989 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2b98a 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
2b98b 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
2b98c 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
2b98d 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
2b98e 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
2b98f 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2b990 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2b991 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2b992 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
2b993 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2b994 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
2b995 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
2b996 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
2b997 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
2b998 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b999 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b99a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b99b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
2b99c 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
2b99d 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69  s code used to i
2b99e 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 41 54 54  mplement the ATT
2b99f 41 43 48 20 61 6e 64 20 44 45 54 41 43 48 20 63  ACH and DETACH c
2b9a0 6f 6d 6d 61 6e 64 73 2e 0a 2a 2f 0a 0a 23 69 66  ommands..*/..#if
2b9a1 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2b9a2 5f 41 54 54 41 43 48 0a 2f 2a 0a 2a 2a 20 52 65  _ATTACH./*.** Re
2b9a3 73 6f 6c 76 65 20 61 6e 20 65 78 70 72 65 73 73  solve an express
2b9a4 69 6f 6e 20 74 68 61 74 20 77 61 73 20 70 61 72  ion that was par
2b9a5 74 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20 6f  t of an ATTACH o
2b9a6 72 20 44 45 54 41 43 48 20 73 74 61 74 65 6d 65  r DETACH stateme
2b9a7 6e 74 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 73  nt. This.** is s
2b9a8 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 65 6e  lightly differen
2b9a9 74 20 66 72 6f 6d 20 72 65 73 6f 6c 76 69 6e 67  t from resolving
2b9aa 20 61 20 6e 6f 72 6d 61 6c 20 53 51 4c 20 65 78   a normal SQL ex
2b9ab 70 72 65 73 73 69 6f 6e 2c 20 62 65 63 61 75 73  pression, becaus
2b9ac 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 69 64 65 6e  e simple.** iden
2b9ad 74 69 66 69 65 72 73 20 61 72 65 20 74 72 65 61  tifiers are trea
2b9ae 74 65 64 20 61 73 20 73 74 72 69 6e 67 73 2c 20  ted as strings, 
2b9af 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 63 6f 6c  not possible col
2b9b0 75 6d 6e 20 6e 61 6d 65 73 20 6f 72 20 61 6c 69  umn names or ali
2b9b1 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e  ases..**.** i.e.
2b9b2 20 69 66 20 74 68 65 20 70 61 72 73 65 72 20 73   if the parser s
2b9b3 65 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41  ees:.**.**     A
2b9b4 54 54 41 43 48 20 44 41 54 41 42 41 53 45 20 61  TTACH DATABASE a
2b9b5 62 63 20 41 53 20 64 65 66 0a 2a 2a 0a 2a 2a 20  bc AS def.**.** 
2b9b6 69 74 20 74 72 65 61 74 73 20 74 68 65 20 74 77  it treats the tw
2b9b7 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 73  o expressions as
2b9b8 20 6c 69 74 65 72 61 6c 20 73 74 72 69 6e 67 73   literal strings
2b9b9 20 27 61 62 63 27 20 61 6e 64 20 27 64 65 66 27   'abc' and 'def'
2b9ba 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 6c   instead of.** l
2b9bb 6f 6f 6b 69 6e 67 20 66 6f 72 20 63 6f 6c 75 6d  ooking for colum
2b9bc 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e  ns of the same n
2b9bd 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ame..**.** This 
2b9be 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20 74 6f 20  only applies to 
2b9bf 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66  the root node of
2b9c0 20 70 45 78 70 72 2c 20 73 6f 20 74 68 65 20 73   pExpr, so the s
2b9c1 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20  tatement:.**.** 
2b9c2 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42      ATTACH DATAB
2b9c3 41 53 45 20 61 62 63 7c 7c 64 65 66 20 41 53 20  ASE abc||def AS 
2b9c4 27 64 62 32 27 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c  'db2'.**.** will
2b9c5 20 66 61 69 6c 20 62 65 63 61 75 73 65 20 6e 65   fail because ne
2b9c6 69 74 68 65 72 20 61 62 63 20 6f 72 20 64 65 66  ither abc or def
2b9c7 20 63 61 6e 20 62 65 20 72 65 73 6f 6c 76 65 64   can be resolved
2b9c8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2b9c9 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70  resolveAttachExp
2b9ca 72 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70  r(NameContext *p
2b9cb 4e 61 6d 65 2c 20 45 78 70 72 20 2a 70 45 78 70  Name, Expr *pExp
2b9cc 72 29 0a 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  r).{.  int rc = 
2b9cd 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
2b9ce 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66   pExpr ){.    if
2b9cf 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
2b9d0 49 44 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ID ){.      rc =
2b9d1 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
2b9d2 78 70 72 4e 61 6d 65 73 28 70 4e 61 6d 65 2c 20  xprNames(pName, 
2b9d3 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66  pExpr);.      if
2b9d4 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
2b9d5 26 26 20 21 73 71 6c 69 74 65 33 45 78 70 72 49  && !sqlite3ExprI
2b9d6 73 43 6f 6e 73 74 61 6e 74 28 70 45 78 70 72 29  sConstant(pExpr)
2b9d7 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2b9d8 74 65 33 45 72 72 6f 72 4d 73 67 28 70 4e 61 6d  te3ErrorMsg(pNam
2b9d9 65 2d 3e 70 50 61 72 73 65 2c 20 22 69 6e 76 61  e->pParse, "inva
2b9da 6c 69 64 20 6e 61 6d 65 3a 20 5c 22 25 73 5c 22  lid name: \"%s\"
2b9db 22 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  ", pExpr->u.zTok
2b9dc 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  en);.        ret
2b9dd 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
2b9de 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
2b9df 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78 70 72  lse{.      pExpr
2b9e0 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 4e 47  ->op = TK_STRING
2b9e1 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
2b9e2 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2b9e3 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d 66 75  * An SQL user-fu
2b9e4 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65  nction registere
2b9e5 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b  d to do the work
2b9e6 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20 73 74   of an ATTACH st
2b9e7 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20  atement. The.** 
2b9e8 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20  three arguments 
2b9e9 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
2b9ea 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 66 72  come directly fr
2b9eb 6f 6d 20 61 6e 20 61 74 74 61 63 68 20 73 74 61  om an attach sta
2b9ec 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  tement:.**.**   
2b9ed 20 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53    ATTACH DATABAS
2b9ee 45 20 78 20 41 53 20 79 20 4b 45 59 20 7a 0a 2a  E x AS y KEY z.*
2b9ef 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
2b9f0 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28 78 2c  sqlite_attach(x,
2b9f1 20 79 2c 20 7a 29 0a 2a 2a 0a 2a 2a 20 49 66 20   y, z).**.** If 
2b9f2 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 22 4b 45  the optional "KE
2b9f3 59 20 7a 22 20 73 79 6e 74 61 78 20 69 73 20 6f  Y z" syntax is o
2b9f4 6d 69 74 74 65 64 2c 20 61 6e 20 53 51 4c 20 4e  mitted, an SQL N
2b9f5 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20 61 73  ULL is passed as
2b9f6 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72   the.** third ar
2b9f7 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69  gument..*/.stati
2b9f8 63 20 76 6f 69 64 20 61 74 74 61 63 68 46 75 6e  c void attachFun
2b9f9 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
2b9fa 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
2b9fb 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20   int NotUsed,.  
2b9fc 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2b9fd 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  argv.){.  int i;
2b9fe 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20  .  int rc = 0;. 
2b9ff 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
2ba00 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
2ba01 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
2ba02 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
2ba03 2a 7a 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 74 20  *zName;.  const 
2ba04 63 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 63  char *zFile;.  c
2ba05 68 61 72 20 2a 7a 50 61 74 68 20 3d 20 30 3b 0a  har *zPath = 0;.
2ba06 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30    char *zErr = 0
2ba07 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ;.  unsigned int
2ba08 20 66 6c 61 67 73 3b 0a 20 20 44 62 20 2a 61 4e   flags;.  Db *aN
2ba09 65 77 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  ew;.  char *zErr
2ba0a 44 79 6e 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  Dyn = 0;.  sqlit
2ba0b 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 0a 0a 20  e3_vfs *pVfs;.. 
2ba0c 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
2ba0d 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 7a  R(NotUsed);..  z
2ba0e 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68  File = (const ch
2ba0f 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
2ba10 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
2ba11 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e  ;.  zName = (con
2ba12 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
2ba13 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
2ba14 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 7a 46 69  v[1]);.  if( zFi
2ba15 6c 65 3d 3d 30 20 29 20 7a 46 69 6c 65 20 3d 20  le==0 ) zFile = 
2ba16 22 22 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d  "";.  if( zName=
2ba17 3d 30 20 29 20 7a 4e 61 6d 65 20 3d 20 22 22 3b  =0 ) zName = "";
2ba18 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72  ..  /* Check for
2ba19 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   the following e
2ba1a 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  rrors:.  **.  **
2ba1b 20 20 20 20 20 2a 20 54 6f 6f 20 6d 61 6e 79 20       * Too many 
2ba1c 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
2ba1d 65 73 2c 0a 20 20 2a 2a 20 20 20 20 20 2a 20 54  es,.  **     * T
2ba1e 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 65  ransaction curre
2ba1f 6e 74 6c 79 20 6f 70 65 6e 0a 20 20 2a 2a 20 20  ntly open.  **  
2ba20 20 20 20 2a 20 53 70 65 63 69 66 69 65 64 20 64     * Specified d
2ba21 61 74 61 62 61 73 65 20 6e 61 6d 65 20 61 6c 72  atabase name alr
2ba22 65 61 64 79 20 62 65 69 6e 67 20 75 73 65 64 2e  eady being used.
2ba23 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
2ba24 6e 44 62 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b  nDb>=db->aLimit[
2ba25 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54  SQLITE_LIMIT_ATT
2ba26 41 43 48 45 44 5d 2b 32 20 29 7b 0a 20 20 20 20  ACHED]+2 ){.    
2ba27 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65  zErrDyn = sqlite
2ba28 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 74 6f  3MPrintf(db, "to
2ba29 6f 20 6d 61 6e 79 20 61 74 74 61 63 68 65 64 20  o many attached 
2ba2a 64 61 74 61 62 61 73 65 73 20 2d 20 6d 61 78 20  databases - max 
2ba2b 25 64 22 2c 20 0a 20 20 20 20 20 20 64 62 2d 3e  %d", .      db->
2ba2c 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
2ba2d 4d 49 54 5f 41 54 54 41 43 48 45 44 5d 0a 20 20  MIT_ATTACHED].  
2ba2e 20 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74    );.    goto at
2ba2f 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  tach_error;.  }.
2ba30 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43    if( !db->autoC
2ba31 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 7a 45 72  ommit ){.    zEr
2ba32 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50  rDyn = sqlite3MP
2ba33 72 69 6e 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f  rintf(db, "canno
2ba34 74 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73  t ATTACH databas
2ba35 65 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63  e within transac
2ba36 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f  tion");.    goto
2ba37 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
2ba38 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
2ba39 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
2ba3a 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d     char *z = db-
2ba3b 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20  >aDb[i].zName;. 
2ba3c 20 20 20 61 73 73 65 72 74 28 20 7a 20 26 26 20     assert( z && 
2ba3d 7a 4e 61 6d 65 20 29 3b 0a 20 20 20 20 69 66 28  zName );.    if(
2ba3e 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2ba3f 7a 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  z, zName)==0 ){.
2ba40 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20        zErrDyn = 
2ba41 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
2ba42 62 2c 20 22 64 61 74 61 62 61 73 65 20 25 73 20  b, "database %s 
2ba43 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 75 73  is already in us
2ba44 65 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  e", zName);.    
2ba45 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72    goto attach_er
2ba46 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ror;.    }.  }..
2ba47 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68    /* Allocate th
2ba48 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 74  e new entry in t
2ba49 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72  he db->aDb[] arr
2ba4a 61 79 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73  ay and initialis
2ba4b 65 20 74 68 65 20 73 63 68 65 6d 61 0a 20 20 2a  e the schema.  *
2ba4c 2a 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 20  * hash tables.. 
2ba4d 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 61 44   */.  if( db->aD
2ba4e 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63  b==db->aDbStatic
2ba4f 20 29 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73   ){.    aNew = s
2ba50 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
2ba51 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d  w(db, sizeof(db-
2ba52 3e 61 44 62 5b 30 5d 29 2a 33 20 29 3b 0a 20 20  >aDb[0])*3 );.  
2ba53 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 20    if( aNew==0 ) 
2ba54 72 65 74 75 72 6e 3b 0a 20 20 20 20 6d 65 6d 63  return;.    memc
2ba55 70 79 28 61 4e 65 77 2c 20 64 62 2d 3e 61 44 62  py(aNew, db->aDb
2ba56 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62  , sizeof(db->aDb
2ba57 5b 30 5d 29 2a 32 29 3b 0a 20 20 7d 65 6c 73 65  [0])*2);.  }else
2ba58 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c  {.    aNew = sql
2ba59 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62  ite3DbRealloc(db
2ba5a 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69 7a 65 6f  , db->aDb, sizeo
2ba5b 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 28 64  f(db->aDb[0])*(d
2ba5c 62 2d 3e 6e 44 62 2b 31 29 20 29 3b 0a 20 20 20  b->nDb+1) );.   
2ba5d 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 20 72   if( aNew==0 ) r
2ba5e 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 64 62 2d  eturn;.  }.  db-
2ba5f 3e 61 44 62 20 3d 20 61 4e 65 77 3b 0a 20 20 61  >aDb = aNew;.  a
2ba60 4e 65 77 20 3d 20 26 64 62 2d 3e 61 44 62 5b 64  New = &db->aDb[d
2ba61 62 2d 3e 6e 44 62 5d 3b 0a 20 20 6d 65 6d 73 65  b->nDb];.  memse
2ba62 74 28 61 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f  t(aNew, 0, sizeo
2ba63 66 28 2a 61 4e 65 77 29 29 3b 0a 0a 20 20 2f 2a  f(*aNew));..  /*
2ba64 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61   Open the databa
2ba65 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20  se file. If the 
2ba66 62 74 72 65 65 20 69 73 20 73 75 63 63 65 73 73  btree is success
2ba67 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 75 73  fully opened, us
2ba68 65 0a 20 20 2a 2a 20 69 74 20 74 6f 20 6f 62 74  e.  ** it to obt
2ba69 61 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  ain the database
2ba6a 20 73 63 68 65 6d 61 2e 20 41 74 20 74 68 69 73   schema. At this
2ba6b 20 70 6f 69 6e 74 20 74 68 65 20 73 63 68 65 6d   point the schem
2ba6c 61 20 6d 61 79 0a 20 20 2a 2a 20 6f 72 20 6d 61  a may.  ** or ma
2ba6d 79 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c  y not be initial
2ba6e 69 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 66 6c 61  ised..  */.  fla
2ba6f 67 73 20 3d 20 64 62 2d 3e 6f 70 65 6e 46 6c 61  gs = db->openFla
2ba70 67 73 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  gs;.  rc = sqlit
2ba71 65 33 50 61 72 73 65 55 72 69 28 64 62 2d 3e 70  e3ParseUri(db->p
2ba72 56 66 73 2d 3e 7a 4e 61 6d 65 2c 20 7a 46 69 6c  Vfs->zName, zFil
2ba73 65 2c 20 26 66 6c 61 67 73 2c 20 26 70 56 66 73  e, &flags, &pVfs
2ba74 2c 20 26 7a 50 61 74 68 2c 20 26 7a 45 72 72 29  , &zPath, &zErr)
2ba75 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2ba76 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
2ba77 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
2ba78 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  M ) db->mallocFa
2ba79 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 73 71  iled = 1;.    sq
2ba7a 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2ba7b 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72  or(context, zErr
2ba7c 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , -1);.    sqlit
2ba7d 65 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a 20  e3_free(zErr);. 
2ba7e 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
2ba7f 20 61 73 73 65 72 74 28 20 70 56 66 73 20 29 3b   assert( pVfs );
2ba80 0a 20 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  .  flags |= SQLI
2ba81 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b  TE_OPEN_MAIN_DB;
2ba82 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
2ba83 74 72 65 65 4f 70 65 6e 28 70 56 66 73 2c 20 7a  treeOpen(pVfs, z
2ba84 50 61 74 68 2c 20 64 62 2c 20 26 61 4e 65 77 2d  Path, db, &aNew-
2ba85 3e 70 42 74 2c 20 30 2c 20 66 6c 61 67 73 29 3b  >pBt, 0, flags);
2ba86 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
2ba87 20 7a 50 61 74 68 20 29 3b 0a 20 20 64 62 2d 3e   zPath );.  db->
2ba88 6e 44 62 2b 2b 3b 0a 20 20 69 66 28 20 72 63 3d  nDb++;.  if( rc=
2ba89 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49  =SQLITE_CONSTRAI
2ba8a 4e 54 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  NT ){.    rc = S
2ba8b 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
2ba8c 20 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74   zErrDyn = sqlit
2ba8d 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 64  e3MPrintf(db, "d
2ba8e 61 74 61 62 61 73 65 20 69 73 20 61 6c 72 65 61  atabase is alrea
2ba8f 64 79 20 61 74 74 61 63 68 65 64 22 29 3b 0a 20  dy attached");. 
2ba90 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53   }else if( rc==S
2ba91 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2ba92 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20  Pager *pPager;. 
2ba93 20 20 20 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61     aNew->pSchema
2ba94 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
2ba95 47 65 74 28 64 62 2c 20 61 4e 65 77 2d 3e 70 42  Get(db, aNew->pB
2ba96 74 29 3b 0a 20 20 20 20 69 66 28 20 21 61 4e 65  t);.    if( !aNe
2ba97 77 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20  w->pSchema ){.  
2ba98 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
2ba99 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65  NOMEM;.    }else
2ba9a 20 69 66 28 20 61 4e 65 77 2d 3e 70 53 63 68 65   if( aNew->pSche
2ba9b 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20  ma->file_format 
2ba9c 26 26 20 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61  && aNew->pSchema
2ba9d 2d 3e 65 6e 63 21 3d 45 4e 43 28 64 62 29 20 29  ->enc!=ENC(db) )
2ba9e 7b 0a 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20  {.      zErrDyn 
2ba9f 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2baa0 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20 22 61  (db, .        "a
2baa1 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
2baa2 73 20 6d 75 73 74 20 75 73 65 20 74 68 65 20 73  s must use the s
2baa3 61 6d 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  ame text encodin
2baa4 67 20 61 73 20 6d 61 69 6e 20 64 61 74 61 62 61  g as main databa
2baa5 73 65 22 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  se");.      rc =
2baa6 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
2baa7 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 20     }.    pPager 
2baa8 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  = sqlite3BtreePa
2baa9 67 65 72 28 61 4e 65 77 2d 3e 70 42 74 29 3b 0a  ger(aNew->pBt);.
2baaa 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
2baab 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67  LockingMode(pPag
2baac 65 72 2c 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b  er, db->dfltLock
2baad 4d 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  Mode);.    sqlit
2baae 65 33 42 74 72 65 65 53 65 63 75 72 65 44 65 6c  e3BtreeSecureDel
2baaf 65 74 65 28 61 4e 65 77 2d 3e 70 42 74 2c 0a 20  ete(aNew->pBt,. 
2bab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bab1 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2bab2 74 65 33 42 74 72 65 65 53 65 63 75 72 65 44 65  te3BtreeSecureDe
2bab3 6c 65 74 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e  lete(db->aDb[0].
2bab4 70 42 74 2c 2d 31 29 20 29 3b 0a 20 20 7d 0a 20  pBt,-1) );.  }. 
2bab5 20 61 4e 65 77 2d 3e 73 61 66 65 74 79 5f 6c 65   aNew->safety_le
2bab6 76 65 6c 20 3d 20 33 3b 0a 20 20 61 4e 65 77 2d  vel = 3;.  aNew-
2bab7 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  >zName = sqlite3
2bab8 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 4e 61  DbStrDup(db, zNa
2bab9 6d 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  me);.  if( rc==S
2baba 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 4e 65 77  QLITE_OK && aNew
2babb 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  ->zName==0 ){.  
2babc 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
2babd 4d 45 4d 3b 0a 20 20 7d 0a 0a 0a 23 69 66 64 65  MEM;.  }...#ifde
2babe 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  f SQLITE_HAS_COD
2babf 45 43 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  EC.  if( rc==SQL
2bac0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 65 78  ITE_OK ){.    ex
2bac1 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33  tern int sqlite3
2bac2 43 6f 64 65 63 41 74 74 61 63 68 28 73 71 6c 69  CodecAttach(sqli
2bac3 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  te3*, int, const
2bac4 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 20 20   void*, int);.  
2bac5 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71    extern void sq
2bac6 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79  lite3CodecGetKey
2bac7 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20  (sqlite3*, int, 
2bac8 76 6f 69 64 2a 2a 2c 20 69 6e 74 2a 29 3b 0a 20  void**, int*);. 
2bac9 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 20     int nKey;.   
2baca 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 20 20 20   char *zKey;.   
2bacb 20 69 6e 74 20 74 20 3d 20 73 71 6c 69 74 65 33   int t = sqlite3
2bacc 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
2bacd 5b 32 5d 29 3b 0a 20 20 20 20 73 77 69 74 63 68  [2]);.    switch
2bace 28 20 74 20 29 7b 0a 20 20 20 20 20 20 63 61 73  ( t ){.      cas
2bacf 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  e SQLITE_INTEGER
2bad0 3a 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c  :.      case SQL
2bad1 49 54 45 5f 46 4c 4f 41 54 3a 0a 20 20 20 20 20  ITE_FLOAT:.     
2bad2 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c     zErrDyn = sql
2bad3 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
2bad4 20 22 49 6e 76 61 6c 69 64 20 6b 65 79 20 76 61   "Invalid key va
2bad5 6c 75 65 22 29 3b 0a 20 20 20 20 20 20 20 20 72  lue");.        r
2bad6 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
2bad7 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
2bad8 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20  .        .      
2bad9 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54  case SQLITE_TEXT
2bada 3a 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c  :.      case SQL
2badb 49 54 45 5f 42 4c 4f 42 3a 0a 20 20 20 20 20 20  ITE_BLOB:.      
2badc 20 20 6e 4b 65 79 20 3d 20 73 71 6c 69 74 65 33    nKey = sqlite3
2badd 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
2bade 76 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 7a  v[2]);.        z
2badf 4b 65 79 20 3d 20 28 63 68 61 72 20 2a 29 73 71  Key = (char *)sq
2bae0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
2bae1 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 20  (argv[2]);.     
2bae2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43     rc = sqlite3C
2bae3 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c 20 64  odecAttach(db, d
2bae4 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c 20  b->nDb-1, zKey, 
2bae5 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 62  nKey);.        b
2bae6 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 63 61 73  reak;..      cas
2bae7 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 0a 20  e SQLITE_NULL:. 
2bae8 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 6b 65 79         /* No key
2bae9 20 73 70 65 63 69 66 69 65 64 2e 20 20 55 73 65   specified.  Use
2baea 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20 74 68   the key from th
2baeb 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
2baec 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
2baed 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28 64 62  e3CodecGetKey(db
2baee 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29 26 7a 4b  , 0, (void**)&zK
2baef 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20 20 20 20  ey, &nKey);.    
2baf0 20 20 20 20 69 66 28 20 6e 4b 65 79 3e 30 20 7c      if( nKey>0 |
2baf1 7c 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  | sqlite3BtreeGe
2baf2 74 52 65 73 65 72 76 65 28 64 62 2d 3e 61 44 62  tReserve(db->aDb
2baf3 5b 30 5d 2e 70 42 74 29 3e 30 20 29 7b 0a 20 20  [0].pBt)>0 ){.  
2baf4 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
2baf5 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68 28  ite3CodecAttach(
2baf6 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a  db, db->nDb-1, z
2baf7 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20  Key, nKey);.    
2baf8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72      }.        br
2baf9 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  eak;.    }.  }.#
2bafa 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74  endif..  /* If t
2bafb 68 65 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e  he file was open
2bafc 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c  ed successfully,
2bafd 20 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61   read the schema
2bafe 20 66 6f 72 20 74 68 65 20 6e 65 77 20 64 61 74   for the new dat
2baff 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49 66 20 74  abase..  ** If t
2bb00 68 69 73 20 66 61 69 6c 73 2c 20 6f 72 20 69 66  his fails, or if
2bb01 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c   opening the fil
2bb02 65 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 20 63  e failed, then c
2bb03 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 61 6e  lose the file an
2bb04 64 20 0a 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74  d .  ** remove t
2bb05 68 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68  he entry from th
2bb06 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61  e db->aDb[] arra
2bb07 79 2e 20 69 2e 65 2e 20 70 75 74 20 65 76 65 72  y. i.e. put ever
2bb08 79 74 68 69 6e 67 20 62 61 63 6b 20 74 68 65 20  ything back the 
2bb09 77 61 79 0a 20 20 2a 2a 20 77 65 20 66 6f 75 6e  way.  ** we foun
2bb0a 64 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  d it..  */.  if(
2bb0b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
2bb0c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  {.    sqlite3Btr
2bb0d 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a  eeEnterAll(db);.
2bb0e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2bb0f 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 44 79  Init(db, &zErrDy
2bb10 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  n);.    sqlite3B
2bb11 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29  treeLeaveAll(db)
2bb12 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29  ;.  }.  if( rc )
2bb13 7b 0a 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20  {.    int iDb = 
2bb14 64 62 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20 20 20  db->nDb - 1;.   
2bb15 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 32 20   assert( iDb>=2 
2bb16 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61  );.    if( db->a
2bb17 44 62 5b 69 44 62 5d 2e 70 42 74 20 29 7b 0a 20  Db[iDb].pBt ){. 
2bb18 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
2bb19 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b 69  eClose(db->aDb[i
2bb1a 44 62 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20  Db].pBt);.      
2bb1b 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
2bb1c 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 62 2d 3e   = 0;.      db->
2bb1d 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
2bb1e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
2bb1f 73 71 6c 69 74 65 33 52 65 73 65 74 41 6c 6c 53  sqlite3ResetAllS
2bb20 63 68 65 6d 61 73 4f 66 43 6f 6e 6e 65 63 74 69  chemasOfConnecti
2bb21 6f 6e 28 64 62 29 3b 0a 20 20 20 20 64 62 2d 3e  on(db);.    db->
2bb22 6e 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 69  nDb = iDb;.    i
2bb23 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
2bb24 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  MEM || rc==SQLIT
2bb25 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b  E_IOERR_NOMEM ){
2bb26 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
2bb27 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
2bb28 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2bb29 28 64 62 2c 20 7a 45 72 72 44 79 6e 29 3b 0a 20  (db, zErrDyn);. 
2bb2a 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73       zErrDyn = s
2bb2b 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2bb2c 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79  , "out of memory
2bb2d 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ");.    }else if
2bb2e 28 20 7a 45 72 72 44 79 6e 3d 3d 30 20 29 7b 0a  ( zErrDyn==0 ){.
2bb2f 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20        zErrDyn = 
2bb30 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
2bb31 62 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70  b, "unable to op
2bb32 65 6e 20 64 61 74 61 62 61 73 65 3a 20 25 73 22  en database: %s"
2bb33 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a  , zFile);.    }.
2bb34 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f      goto attach_
2bb35 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 0a 20 20  error;.  }.  .  
2bb36 72 65 74 75 72 6e 3b 0a 0a 61 74 74 61 63 68 5f  return;..attach_
2bb37 65 72 72 6f 72 3a 0a 20 20 2f 2a 20 52 65 74 75  error:.  /* Retu
2bb38 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 66 20 77  rn an error if w
2bb39 65 20 67 65 74 20 68 65 72 65 20 2a 2f 0a 20 20  e get here */.  
2bb3a 69 66 28 20 7a 45 72 72 44 79 6e 20 29 7b 0a 20  if( zErrDyn ){. 
2bb3b 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
2bb3c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
2bb3d 20 7a 45 72 72 44 79 6e 2c 20 2d 31 29 3b 0a 20   zErrDyn, -1);. 
2bb3e 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2bb3f 28 64 62 2c 20 7a 45 72 72 44 79 6e 29 3b 0a 20  (db, zErrDyn);. 
2bb40 20 7d 0a 20 20 69 66 28 20 72 63 20 29 20 73 71   }.  if( rc ) sq
2bb41 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2bb42 6f 72 5f 63 6f 64 65 28 63 6f 6e 74 65 78 74 2c  or_code(context,
2bb43 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41   rc);.}../*.** A
2bb44 6e 20 53 51 4c 20 75 73 65 72 2d 66 75 6e 63 74  n SQL user-funct
2bb45 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 74  ion registered t
2bb46 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66  o do the work of
2bb47 20 61 6e 20 44 45 54 41 43 48 20 73 74 61 74 65   an DETACH state
2bb48 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 74 68 72  ment. The.** thr
2bb49 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  ee arguments to 
2bb4a 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d  the function com
2bb4b 65 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20  e directly from 
2bb4c 61 20 64 65 74 61 63 68 20 73 74 61 74 65 6d 65  a detach stateme
2bb4d 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45  nt:.**.**     DE
2bb4e 54 41 43 48 20 44 41 54 41 42 41 53 45 20 78 0a  TACH DATABASE x.
2bb4f 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  **.**     SELECT
2bb50 20 73 71 6c 69 74 65 5f 64 65 74 61 63 68 28 78   sqlite_detach(x
2bb51 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ).*/.static void
2bb52 20 64 65 74 61 63 68 46 75 6e 63 28 0a 20 20 73   detachFunc(.  s
2bb53 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
2bb54 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
2bb55 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
2bb56 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
2bb57 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
2bb58 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63  zName = (const c
2bb59 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
2bb5a 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
2bb5b 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  );.  sqlite3 *db
2bb5c 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
2bb5d 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
2bb5e 74 65 78 74 29 3b 0a 20 20 69 6e 74 20 69 3b 0a  text);.  int i;.
2bb5f 20 20 44 62 20 2a 70 44 62 20 3d 20 30 3b 0a 20    Db *pDb = 0;. 
2bb60 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d 3b   char zErr[128];
2bb61 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
2bb62 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a  ETER(NotUsed);..
2bb63 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29    if( zName==0 )
2bb64 20 7a 4e 61 6d 65 20 3d 20 22 22 3b 0a 20 20 66   zName = "";.  f
2bb65 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
2bb66 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 44 62  b; i++){.    pDb
2bb67 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a   = &db->aDb[i];.
2bb68 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74      if( pDb->pBt
2bb69 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
2bb6a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
2bb6b 74 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d  trICmp(pDb->zNam
2bb6c 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62  e, zName)==0 ) b
2bb6d 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  reak;.  }..  if(
2bb6e 20 69 3e 3d 64 62 2d 3e 6e 44 62 20 29 7b 0a 20   i>=db->nDb ){. 
2bb6f 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
2bb70 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
2bb71 2c 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68 20  ,zErr, "no such 
2bb72 64 61 74 61 62 61 73 65 3a 20 25 73 22 2c 20 7a  database: %s", z
2bb73 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
2bb74 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20  detach_error;.  
2bb75 7d 0a 20 20 69 66 28 20 69 3c 32 20 29 7b 0a 20  }.  if( i<2 ){. 
2bb76 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
2bb77 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
2bb78 2c 7a 45 72 72 2c 20 22 63 61 6e 6e 6f 74 20 64  ,zErr, "cannot d
2bb79 65 74 61 63 68 20 64 61 74 61 62 61 73 65 20 25  etach database %
2bb7a 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
2bb7b 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f  goto detach_erro
2bb7c 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62  r;.  }.  if( !db
2bb7d 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
2bb7e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
2bb7f 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
2bb80 29 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20 20 20  ), zErr,.       
2bb81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63                "c
2bb82 61 6e 6e 6f 74 20 44 45 54 41 43 48 20 64 61 74  annot DETACH dat
2bb83 61 62 61 73 65 20 77 69 74 68 69 6e 20 74 72 61  abase within tra
2bb84 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20  nsaction");.    
2bb85 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f  goto detach_erro
2bb86 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  r;.  }.  if( sql
2bb87 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61  ite3BtreeIsInRea
2bb88 64 54 72 61 6e 73 28 70 44 62 2d 3e 70 42 74 29  dTrans(pDb->pBt)
2bb89 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72 65 65   || sqlite3Btree
2bb8a 49 73 49 6e 42 61 63 6b 75 70 28 70 44 62 2d 3e  IsInBackup(pDb->
2bb8b 70 42 74 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  pBt) ){.    sqli
2bb8c 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
2bb8d 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20  eof(zErr),zErr, 
2bb8e 22 64 61 74 61 62 61 73 65 20 25 73 20 69 73 20  "database %s is 
2bb8f 6c 6f 63 6b 65 64 22 2c 20 7a 4e 61 6d 65 29 3b  locked", zName);
2bb90 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68  .    goto detach
2bb91 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 73  _error;.  }..  s
2bb92 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
2bb93 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 70 44  (pDb->pBt);.  pD
2bb94 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20 70 44  b->pBt = 0;.  pD
2bb95 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a  b->pSchema = 0;.
2bb96 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 41 6c    sqlite3ResetAl
2bb97 6c 53 63 68 65 6d 61 73 4f 66 43 6f 6e 6e 65 63  lSchemasOfConnec
2bb98 74 69 6f 6e 28 64 62 29 3b 0a 20 20 72 65 74 75  tion(db);.  retu
2bb99 72 6e 3b 0a 0a 64 65 74 61 63 68 5f 65 72 72 6f  rn;..detach_erro
2bb9a 72 3a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  r:.  sqlite3_res
2bb9b 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
2bb9c 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 7d 0a  t, zErr, -1);.}.
2bb9d 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63  ./*.** This proc
2bb9e 65 64 75 72 65 20 67 65 6e 65 72 61 74 65 73 20  edure generates 
2bb9f 56 44 42 45 20 63 6f 64 65 20 66 6f 72 20 61 20  VDBE code for a 
2bba0 73 69 6e 67 6c 65 20 69 6e 76 6f 63 61 74 69 6f  single invocatio
2bba1 6e 20 6f 66 20 65 69 74 68 65 72 20 74 68 65 0a  n of either the.
2bba2 2a 2a 20 73 71 6c 69 74 65 5f 64 65 74 61 63 68  ** sqlite_detach
2bba3 28 29 20 6f 72 20 73 71 6c 69 74 65 5f 61 74 74  () or sqlite_att
2bba4 61 63 68 28 29 20 53 51 4c 20 75 73 65 72 20 66  ach() SQL user f
2bba5 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61  unctions..*/.sta
2bba6 74 69 63 20 76 6f 69 64 20 63 6f 64 65 41 74 74  tic void codeAtt
2bba7 61 63 68 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ach(.  Parse *pP
2bba8 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54  arse,       /* T
2bba9 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78  he parser contex
2bbaa 74 20 2a 2f 0a 20 20 69 6e 74 20 74 79 70 65 2c  t */.  int type,
2bbab 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
2bbac 69 74 68 65 72 20 53 51 4c 49 54 45 5f 41 54 54  ither SQLITE_ATT
2bbad 41 43 48 20 6f 72 20 53 51 4c 49 54 45 5f 44 45  ACH or SQLITE_DE
2bbae 54 41 43 48 20 2a 2f 0a 20 20 46 75 6e 63 44 65  TACH */.  FuncDe
2bbaf 66 20 63 6f 6e 73 74 20 2a 70 46 75 6e 63 2c 2f  f const *pFunc,/
2bbb0 2a 20 46 75 6e 63 44 65 66 20 77 72 61 70 70 65  * FuncDef wrappe
2bbb1 72 20 66 6f 72 20 64 65 74 61 63 68 46 75 6e 63  r for detachFunc
2bbb2 28 29 20 6f 72 20 61 74 74 61 63 68 46 75 6e 63  () or attachFunc
2bbb3 28 29 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 41  () */.  Expr *pA
2bbb4 75 74 68 41 72 67 2c 20 20 20 20 20 20 2f 2a 20  uthArg,      /* 
2bbb5 45 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 70 61  Expression to pa
2bbb6 73 73 20 74 6f 20 61 75 74 68 6f 72 69 7a 61 74  ss to authorizat
2bbb7 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a  ion callback */.
2bbb8 20 20 45 78 70 72 20 2a 70 46 69 6c 65 6e 61 6d    Expr *pFilenam
2bbb9 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  e,     /* Name o
2bbba 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  f database file 
2bbbb 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 62 6e 61  */.  Expr *pDbna
2bbbc 6d 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  me,       /* Nam
2bbbd 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
2bbbe 65 20 74 6f 20 75 73 65 20 69 6e 74 65 72 6e 61  e to use interna
2bbbf 6c 6c 79 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  lly */.  Expr *p
2bbc0 4b 65 79 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Key           /*
2bbc1 20 44 61 74 61 62 61 73 65 20 6b 65 79 20 66 6f   Database key fo
2bbc2 72 20 65 6e 63 72 79 70 74 69 6f 6e 20 65 78 74  r encryption ext
2bbc3 65 6e 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69  ension */.){.  i
2bbc4 6e 74 20 72 63 3b 0a 20 20 4e 61 6d 65 43 6f 6e  nt rc;.  NameCon
2bbc5 74 65 78 74 20 73 4e 61 6d 65 3b 0a 20 20 56 64  text sName;.  Vd
2bbc6 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33  be *v;.  sqlite3
2bbc7 2a 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64  * db = pParse->d
2bbc8 62 3b 0a 20 20 69 6e 74 20 72 65 67 41 72 67 73  b;.  int regArgs
2bbc9 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 61  ;..  memset(&sNa
2bbca 6d 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4e 61  me, 0, sizeof(Na
2bbcb 6d 65 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 73  meContext));.  s
2bbcc 4e 61 6d 65 2e 70 50 61 72 73 65 20 3d 20 70 50  Name.pParse = pP
2bbcd 61 72 73 65 3b 0a 0a 20 20 69 66 28 20 0a 20 20  arse;..  if( .  
2bbce 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28      SQLITE_OK!=(
2bbcf 72 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61  rc = resolveAtta
2bbd0 63 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70  chExpr(&sName, p
2bbd1 46 69 6c 65 6e 61 6d 65 29 29 20 7c 7c 0a 20 20  Filename)) ||.  
2bbd2 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28      SQLITE_OK!=(
2bbd3 72 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61  rc = resolveAtta
2bbd4 63 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70  chExpr(&sName, p
2bbd5 44 62 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20  Dbname)) ||.    
2bbd6 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63    SQLITE_OK!=(rc
2bbd7 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68   = resolveAttach
2bbd8 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 4b 65  Expr(&sName, pKe
2bbd9 79 29 29 0a 20 20 29 7b 0a 20 20 20 20 70 50 61  y)).  ){.    pPa
2bbda 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20  rse->nErr++;.   
2bbdb 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64   goto attach_end
2bbdc 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
2bbdd 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
2bbde 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 70  RIZATION.  if( p
2bbdf 41 75 74 68 41 72 67 20 29 7b 0a 20 20 20 20 63  AuthArg ){.    c
2bbe0 68 61 72 20 2a 7a 41 75 74 68 41 72 67 3b 0a 20  har *zAuthArg;. 
2bbe1 20 20 20 69 66 28 20 70 41 75 74 68 41 72 67 2d     if( pAuthArg-
2bbe2 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 29  >op==TK_STRING )
2bbe3 7b 0a 20 20 20 20 20 20 7a 41 75 74 68 41 72 67  {.      zAuthArg
2bbe4 20 3d 20 70 41 75 74 68 41 72 67 2d 3e 75 2e 7a   = pAuthArg->u.z
2bbe5 54 6f 6b 65 6e 3b 0a 20 20 20 20 7d 65 6c 73 65  Token;.    }else
2bbe6 7b 0a 20 20 20 20 20 20 7a 41 75 74 68 41 72 67  {.      zAuthArg
2bbe7 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
2bbe8 72 63 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68  rc = sqlite3Auth
2bbe9 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 74 79  Check(pParse, ty
2bbea 70 65 2c 20 7a 41 75 74 68 41 72 67 2c 20 30 2c  pe, zAuthArg, 0,
2bbeb 20 30 29 3b 0a 20 20 20 20 69 66 28 72 63 21 3d   0);.    if(rc!=
2bbec 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2bbed 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65     goto attach_e
2bbee 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  nd;.    }.  }.#e
2bbef 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2bbf0 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
2bbf1 4e 20 2a 2f 0a 0a 0a 20 20 76 20 3d 20 73 71 6c  N */...  v = sql
2bbf2 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2bbf3 73 65 29 3b 0a 20 20 72 65 67 41 72 67 73 20 3d  se);.  regArgs =
2bbf4 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2bbf5 61 6e 67 65 28 70 50 61 72 73 65 2c 20 34 29 3b  ange(pParse, 4);
2bbf6 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  .  sqlite3ExprCo
2bbf7 64 65 28 70 50 61 72 73 65 2c 20 70 46 69 6c 65  de(pParse, pFile
2bbf8 6e 61 6d 65 2c 20 72 65 67 41 72 67 73 29 3b 0a  name, regArgs);.
2bbf9 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2bbfa 65 28 70 50 61 72 73 65 2c 20 70 44 62 6e 61 6d  e(pParse, pDbnam
2bbfb 65 2c 20 72 65 67 41 72 67 73 2b 31 29 3b 0a 20  e, regArgs+1);. 
2bbfc 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2bbfd 28 70 50 61 72 73 65 2c 20 70 4b 65 79 2c 20 72  (pParse, pKey, r
2bbfe 65 67 41 72 67 73 2b 32 29 3b 0a 0a 20 20 61 73  egArgs+2);..  as
2bbff 73 65 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e 6d  sert( v || db->m
2bc00 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
2bc01 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71   if( v ){.    sq
2bc02 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2bc03 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20  v, OP_Function, 
2bc04 30 2c 20 72 65 67 41 72 67 73 2b 33 2d 70 46 75  0, regArgs+3-pFu
2bc05 6e 63 2d 3e 6e 41 72 67 2c 20 72 65 67 41 72 67  nc->nArg, regArg
2bc06 73 2b 33 29 3b 0a 20 20 20 20 61 73 73 65 72 74  s+3);.    assert
2bc07 28 20 70 46 75 6e 63 2d 3e 6e 41 72 67 3d 3d 2d  ( pFunc->nArg==-
2bc08 31 20 7c 7c 20 28 70 46 75 6e 63 2d 3e 6e 41 72  1 || (pFunc->nAr
2bc09 67 26 30 78 66 66 29 3d 3d 70 46 75 6e 63 2d 3e  g&0xff)==pFunc->
2bc0a 6e 41 72 67 20 29 3b 0a 20 20 20 20 73 71 6c 69  nArg );.    sqli
2bc0b 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
2bc0c 76 2c 20 28 75 38 29 28 70 46 75 6e 63 2d 3e 6e  v, (u8)(pFunc->n
2bc0d 41 72 67 29 29 3b 0a 20 20 20 20 73 71 6c 69 74  Arg));.    sqlit
2bc0e 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
2bc0f 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 70 46  , -1, (char *)pF
2bc10 75 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 29  unc, P4_FUNCDEF)
2bc11 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 61  ;..    /* Code a
2bc12 6e 20 4f 50 5f 45 78 70 69 72 65 2e 20 46 6f 72  n OP_Expire. For
2bc13 20 61 6e 20 41 54 54 41 43 48 20 73 74 61 74 65   an ATTACH state
2bc14 6d 65 6e 74 2c 20 73 65 74 20 50 31 20 74 6f 20  ment, set P1 to 
2bc15 74 72 75 65 20 28 65 78 70 69 72 65 20 74 68 69  true (expire thi
2bc16 73 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65  s.    ** stateme
2bc17 6e 74 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 44 45  nt only). For DE
2bc18 54 41 43 48 2c 20 73 65 74 20 69 74 20 74 6f 20  TACH, set it to 
2bc19 66 61 6c 73 65 20 28 65 78 70 69 72 65 20 61 6c  false (expire al
2bc1a 6c 20 65 78 69 73 74 69 6e 67 0a 20 20 20 20 2a  l existing.    *
2bc1b 2a 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a 20  * statements).. 
2bc1c 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
2bc1d 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2bc1e 50 5f 45 78 70 69 72 65 2c 20 28 74 79 70 65 3d  P_Expire, (type=
2bc1f 3d 53 51 4c 49 54 45 5f 41 54 54 41 43 48 29 29  =SQLITE_ATTACH))
2bc20 3b 0a 20 20 7d 0a 20 20 0a 61 74 74 61 63 68 5f  ;.  }.  .attach_
2bc21 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 45 78  end:.  sqlite3Ex
2bc22 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 46 69  prDelete(db, pFi
2bc23 6c 65 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  lename);.  sqlit
2bc24 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2bc25 20 70 44 62 6e 61 6d 65 29 3b 0a 20 20 73 71 6c   pDbname);.  sql
2bc26 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2bc27 62 2c 20 70 4b 65 79 29 3b 0a 7d 0a 0a 2f 2a 0a  b, pKey);.}../*.
2bc28 2a 2a 20 43 61 6c 6c 65 64 20 62 79 20 74 68 65  ** Called by the
2bc29 20 70 61 72 73 65 72 20 74 6f 20 63 6f 6d 70 69   parser to compi
2bc2a 6c 65 20 61 20 44 45 54 41 43 48 20 73 74 61 74  le a DETACH stat
2bc2b 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  ement..**.**    
2bc2c 20 44 45 54 41 43 48 20 70 44 62 6e 61 6d 65 0a   DETACH pDbname.
2bc2d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2bc2e 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
2bc2f 74 61 63 68 28 50 61 72 73 65 20 2a 70 50 61 72  tach(Parse *pPar
2bc30 73 65 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d  se, Expr *pDbnam
2bc31 65 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  e){.  static con
2bc32 73 74 20 46 75 6e 63 44 65 66 20 64 65 74 61 63  st FuncDef detac
2bc33 68 5f 66 75 6e 63 20 3d 20 7b 0a 20 20 20 20 31  h_func = {.    1
2bc34 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2bc35 20 2f 2a 20 6e 41 72 67 20 2a 2f 0a 20 20 20 20   /* nArg */.    
2bc36 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
2bc37 20 20 2f 2a 20 69 50 72 65 66 45 6e 63 20 2a 2f    /* iPrefEnc */
2bc38 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
2bc39 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20         /* flags 
2bc3a 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
2bc3b 20 20 20 20 20 20 20 20 20 2f 2a 20 70 55 73 65           /* pUse
2bc3c 72 44 61 74 61 20 2a 2f 0a 20 20 20 20 30 2c 20  rData */.    0, 
2bc3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2bc3e 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 64  * pNext */.    d
2bc3f 65 74 61 63 68 46 75 6e 63 2c 20 20 20 20 20 20  etachFunc,      
2bc40 20 2f 2a 20 78 46 75 6e 63 20 2a 2f 0a 20 20 20   /* xFunc */.   
2bc41 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
2bc42 20 20 20 2f 2a 20 78 53 74 65 70 20 2a 2f 0a 20     /* xStep */. 
2bc43 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
2bc44 20 20 20 20 20 2f 2a 20 78 46 69 6e 61 6c 69 7a       /* xFinaliz
2bc45 65 20 2a 2f 0a 20 20 20 20 22 73 71 6c 69 74 65  e */.    "sqlite
2bc46 5f 64 65 74 61 63 68 22 2c 20 20 2f 2a 20 7a 4e  _detach",  /* zN
2bc47 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  ame */.    0,   
2bc48 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2bc49 70 48 61 73 68 20 2a 2f 0a 20 20 20 20 30 20 20  pHash */.    0  
2bc4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2bc4b 2a 20 70 44 65 73 74 72 75 63 74 6f 72 20 2a 2f  * pDestructor */
2bc4c 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61  .  };.  codeAtta
2bc4d 63 68 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ch(pParse, SQLIT
2bc4e 45 5f 44 45 54 41 43 48 2c 20 26 64 65 74 61 63  E_DETACH, &detac
2bc4f 68 5f 66 75 6e 63 2c 20 70 44 62 6e 61 6d 65 2c  h_func, pDbname,
2bc50 20 30 2c 20 30 2c 20 70 44 62 6e 61 6d 65 29 3b   0, 0, pDbname);
2bc51 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64  .}../*.** Called
2bc52 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74   by the parser t
2bc53 6f 20 63 6f 6d 70 69 6c 65 20 61 6e 20 41 54 54  o compile an ATT
2bc54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ACH statement..*
2bc55 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20  *.**     ATTACH 
2bc56 70 20 41 53 20 70 44 62 6e 61 6d 65 20 4b 45 59  p AS pDbname KEY
2bc57 20 70 4b 65 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f   pKey.*/.SQLITE_
2bc58 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2bc59 69 74 65 33 41 74 74 61 63 68 28 50 61 72 73 65  ite3Attach(Parse
2bc5a 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
2bc5b 70 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65  p, Expr *pDbname
2bc5c 2c 20 45 78 70 72 20 2a 70 4b 65 79 29 7b 0a 20  , Expr *pKey){. 
2bc5d 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 46 75   static const Fu
2bc5e 6e 63 44 65 66 20 61 74 74 61 63 68 5f 66 75 6e  ncDef attach_fun
2bc5f 63 20 3d 20 7b 0a 20 20 20 20 33 2c 20 20 20 20  c = {.    3,    
2bc60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e              /* n
2bc61 41 72 67 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54  Arg */.    SQLIT
2bc62 45 5f 55 54 46 38 2c 20 20 20 20 20 20 2f 2a 20  E_UTF8,      /* 
2bc63 69 50 72 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20  iPrefEnc */.    
2bc64 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
2bc65 20 20 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20    /* flags */.  
2bc66 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
2bc67 20 20 20 20 2f 2a 20 70 55 73 65 72 44 61 74 61      /* pUserData
2bc68 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20   */.    0,      
2bc69 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65            /* pNe
2bc6a 78 74 20 2a 2f 0a 20 20 20 20 61 74 74 61 63 68  xt */.    attach
2bc6b 46 75 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20 78  Func,       /* x
2bc6c 46 75 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20  Func */.    0,  
2bc6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2bc6e 20 78 53 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c   xStep */.    0,
2bc6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bc70 2f 2a 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a  /* xFinalize */.
2bc71 20 20 20 20 22 73 71 6c 69 74 65 5f 61 74 74 61      "sqlite_atta
2bc72 63 68 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a  ch",  /* zName *
2bc73 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
2bc74 20 20 20 20 20 20 20 20 2f 2a 20 70 48 61 73 68          /* pHash
2bc75 20 2a 2f 0a 20 20 20 20 30 20 20 20 20 20 20 20   */.    0       
2bc76 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 44 65            /* pDe
2bc77 73 74 72 75 63 74 6f 72 20 2a 2f 0a 20 20 7d 3b  structor */.  };
2bc78 0a 20 20 63 6f 64 65 41 74 74 61 63 68 28 70 50  .  codeAttach(pP
2bc79 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 54 54  arse, SQLITE_ATT
2bc7a 41 43 48 2c 20 26 61 74 74 61 63 68 5f 66 75 6e  ACH, &attach_fun
2bc7b 63 2c 20 70 2c 20 70 2c 20 70 44 62 6e 61 6d 65  c, p, p, pDbname
2bc7c 2c 20 70 4b 65 79 29 3b 0a 7d 0a 23 65 6e 64 69  , pKey);.}.#endi
2bc7d 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2bc7e 5f 41 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a 0a 2a  _ATTACH */../*.*
2bc7f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 44  * Initialize a D
2bc80 62 46 69 78 65 72 20 73 74 72 75 63 74 75 72 65  bFixer structure
2bc81 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
2bc82 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 70  must be called p
2bc83 72 69 6f 72 0a 2a 2a 20 74 6f 20 70 61 73 73 69  rior.** to passi
2bc84 6e 67 20 74 68 65 20 73 74 72 75 63 74 75 72 65  ng the structure
2bc85 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 73   to one of the s
2bc86 71 6c 69 74 65 46 69 78 41 41 41 41 28 29 20 72  qliteFixAAAA() r
2bc87 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a  outines below..*
2bc88 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  *.** The return 
2bc89 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20  value indicates 
2bc8a 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 66  whether or not f
2bc8b 69 78 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69  ixation is requi
2bc8c 72 65 64 2e 20 20 54 52 55 45 0a 2a 2a 20 6d 65  red.  TRUE.** me
2bc8d 61 6e 73 20 77 65 20 64 6f 20 6e 65 65 64 20 74  ans we do need t
2bc8e 6f 20 66 69 78 20 74 68 65 20 64 61 74 61 62 61  o fix the databa
2bc8f 73 65 20 72 65 66 65 72 65 6e 63 65 73 2c 20 46  se references, F
2bc90 41 4c 53 45 20 6d 65 61 6e 73 20 77 65 20 64 6f  ALSE means we do
2bc91 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   not..*/.SQLITE_
2bc92 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2bc93 74 65 33 46 69 78 49 6e 69 74 28 0a 20 20 44 62  te3FixInit(.  Db
2bc94 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20  Fixer *pFix,    
2bc95 20 20 2f 2a 20 54 68 65 20 66 69 78 65 72 20 74    /* The fixer t
2bc96 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  o be initialized
2bc97 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61   */.  Parse *pPa
2bc98 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 45 72 72  rse,      /* Err
2bc99 6f 72 20 6d 65 73 73 61 67 65 73 20 77 69 6c 6c  or messages will
2bc9a 20 62 65 20 77 72 69 74 74 65 6e 20 68 65 72 65   be written here
2bc9b 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20   */.  int iDb,  
2bc9c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69            /* Thi
2bc9d 73 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73  s is the databas
2bc9e 65 20 74 68 61 74 20 6d 75 73 74 20 62 65 20 75  e that must be u
2bc9f 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  sed */.  const c
2bca0 68 61 72 20 2a 7a 54 79 70 65 2c 20 20 2f 2a 20  har *zType,  /* 
2bca1 22 76 69 65 77 22 2c 20 22 74 72 69 67 67 65 72  "view", "trigger
2bca2 22 2c 20 6f 72 20 22 69 6e 64 65 78 22 20 2a 2f  ", or "index" */
2bca3 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a  .  const Token *
2bca4 70 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d 65 20 6f  pName  /* Name o
2bca5 66 20 74 68 65 20 76 69 65 77 2c 20 74 72 69 67  f the view, trig
2bca6 67 65 72 2c 20 6f 72 20 69 6e 64 65 78 20 2a 2f  ger, or index */
2bca7 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
2bca8 62 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28  b;..  if( NEVER(
2bca9 69 44 62 3c 30 29 20 7c 7c 20 69 44 62 3d 3d 31  iDb<0) || iDb==1
2bcaa 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 64   ) return 0;.  d
2bcab 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2bcac 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 44    assert( db->nD
2bcad 62 3e 69 44 62 20 29 3b 0a 20 20 70 46 69 78 2d  b>iDb );.  pFix-
2bcae 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  >pParse = pParse
2bcaf 3b 0a 20 20 70 46 69 78 2d 3e 7a 44 62 20 3d 20  ;.  pFix->zDb = 
2bcb0 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
2bcb1 6d 65 3b 0a 20 20 70 46 69 78 2d 3e 70 53 63 68  me;.  pFix->pSch
2bcb2 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  ema = db->aDb[iD
2bcb3 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 46  b].pSchema;.  pF
2bcb4 69 78 2d 3e 7a 54 79 70 65 20 3d 20 7a 54 79 70  ix->zType = zTyp
2bcb5 65 3b 0a 20 20 70 46 69 78 2d 3e 70 4e 61 6d 65  e;.  pFix->pName
2bcb6 20 3d 20 70 4e 61 6d 65 3b 0a 20 20 72 65 74 75   = pName;.  retu
2bcb7 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn 1;.}../*.** T
2bcb8 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 74  he following set
2bcb9 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 77 61 6c   of routines wal
2bcba 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 70 61  k through the pa
2bcbb 72 73 65 20 74 72 65 65 20 61 6e 64 20 61 73 73  rse tree and ass
2bcbc 69 67 6e 0a 2a 2a 20 61 20 73 70 65 63 69 66 69  ign.** a specifi
2bcbd 63 20 64 61 74 61 62 61 73 65 20 74 6f 20 61 6c  c database to al
2bcbe 6c 20 74 61 62 6c 65 20 72 65 66 65 72 65 6e 63  l table referenc
2bcbf 65 73 20 77 68 65 72 65 20 74 68 65 20 64 61 74  es where the dat
2bcc0 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 77 61  abase name.** wa
2bcc1 73 20 6c 65 66 74 20 75 6e 73 70 65 63 69 66 69  s left unspecifi
2bcc2 65 64 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e  ed in the origin
2bcc3 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  al SQL statement
2bcc4 2e 20 20 54 68 65 20 70 46 69 78 20 73 74 72 75  .  The pFix stru
2bcc5 63 74 75 72 65 0a 2a 2a 20 6d 75 73 74 20 68 61  cture.** must ha
2bcc6 76 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  ve been initiali
2bcc7 7a 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 63  zed by a prior c
2bcc8 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 46 69  all to sqlite3Fi
2bcc9 78 49 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  xInit()..**.** T
2bcca 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
2bccb 65 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73  e used to make s
2bccc 75 72 65 20 74 68 61 74 20 61 6e 20 69 6e 64 65  ure that an inde
2bccd 78 2c 20 74 72 69 67 67 65 72 2c 20 6f 72 0a 2a  x, trigger, or.*
2bcce 2a 20 76 69 65 77 20 69 6e 20 6f 6e 65 20 64 61  * view in one da
2bccf 74 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 20  tabase does not 
2bcd0 72 65 66 65 72 20 74 6f 20 6f 62 6a 65 63 74 73  refer to objects
2bcd1 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20   in a different 
2bcd2 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 28 45 78  database..** (Ex
2bcd3 63 65 70 74 69 6f 6e 3a 20 69 6e 64 69 63 65 73  ception: indices
2bcd4 2c 20 74 72 69 67 67 65 72 73 2c 20 61 6e 64 20  , triggers, and 
2bcd5 76 69 65 77 73 20 69 6e 20 74 68 65 20 54 45 4d  views in the TEM
2bcd6 50 20 64 61 74 61 62 61 73 65 20 61 72 65 0a 2a  P database are.*
2bcd7 2a 20 61 6c 6c 6f 77 65 64 20 74 6f 20 72 65 66  * allowed to ref
2bcd8 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 2e 29  er to anything.)
2bcd9 20 20 49 66 20 61 20 72 65 66 65 72 65 6e 63 65    If a reference
2bcda 20 69 73 20 65 78 70 6c 69 63 69 74 6c 79 20 6d   is explicitly m
2bcdb 61 64 65 0a 2a 2a 20 74 6f 20 61 6e 20 6f 62 6a  ade.** to an obj
2bcdc 65 63 74 20 69 6e 20 61 20 64 69 66 66 65 72 65  ect in a differe
2bcdd 6e 74 20 64 61 74 61 62 61 73 65 2c 20 61 6e 20  nt database, an 
2bcde 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73  error message is
2bcdf 20 61 64 64 65 64 20 74 6f 0a 2a 2a 20 70 50 61   added to.** pPa
2bce0 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61 6e 64  rse->zErrMsg and
2bce1 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
2bce2 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e  return non-zero.
2bce3 20 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 0a    If everything.
2bce4 2a 2a 20 63 68 65 63 6b 73 20 6f 75 74 2c 20 74  ** checks out, t
2bce5 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65  hese routines re
2bce6 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54  turn 0..*/.SQLIT
2bce7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2bce8 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28  lite3FixSrcList(
2bce9 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78  .  DbFixer *pFix
2bcea 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ,       /* Conte
2bceb 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69  xt of the fixati
2bcec 6f 6e 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  on */.  SrcList 
2bced 2a 70 4c 69 73 74 20 20 20 20 20 20 20 2f 2a 20  *pList       /* 
2bcee 54 68 65 20 53 6f 75 72 63 65 20 6c 69 73 74 20  The Source list 
2bcef 74 6f 20 63 68 65 63 6b 20 61 6e 64 20 6d 6f 64  to check and mod
2bcf0 69 66 79 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ify */.){.  int 
2bcf1 69 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  i;.  const char 
2bcf2 2a 7a 44 62 3b 0a 20 20 73 74 72 75 63 74 20 53  *zDb;.  struct S
2bcf3 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
2bcf4 65 6d 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52  em;..  if( NEVER
2bcf5 28 70 4c 69 73 74 3d 3d 30 29 20 29 20 72 65 74  (pList==0) ) ret
2bcf6 75 72 6e 20 30 3b 0a 20 20 7a 44 62 20 3d 20 70  urn 0;.  zDb = p
2bcf7 46 69 78 2d 3e 7a 44 62 3b 0a 20 20 66 6f 72 28  Fix->zDb;.  for(
2bcf8 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74  i=0, pItem=pList
2bcf9 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53  ->a; i<pList->nS
2bcfa 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  rc; i++, pItem++
2bcfb 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d  ){.    if( pItem
2bcfc 2d 3e 7a 44 61 74 61 62 61 73 65 20 26 26 20 73  ->zDatabase && s
2bcfd 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 49  qlite3StrICmp(pI
2bcfe 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c 20  tem->zDatabase, 
2bcff 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71  zDb) ){.      sq
2bd00 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 46  lite3ErrorMsg(pF
2bd01 69 78 2d 3e 70 50 61 72 73 65 2c 0a 20 20 20 20  ix->pParse,.    
2bd02 20 20 20 20 20 22 25 73 20 25 54 20 63 61 6e 6e       "%s %T cann
2bd03 6f 74 20 72 65 66 65 72 65 6e 63 65 20 6f 62 6a  ot reference obj
2bd04 65 63 74 73 20 69 6e 20 64 61 74 61 62 61 73 65  ects in database
2bd05 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 70   %s",.         p
2bd06 46 69 78 2d 3e 7a 54 79 70 65 2c 20 70 46 69 78  Fix->zType, pFix
2bd07 2d 3e 70 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e  ->pName, pItem->
2bd08 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20  zDatabase);.    
2bd09 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2bd0a 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
2bd0b 72 65 65 28 70 46 69 78 2d 3e 70 50 61 72 73 65  ree(pFix->pParse
2bd0c 2d 3e 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 44 61  ->db, pItem->zDa
2bd0d 74 61 62 61 73 65 29 3b 0a 20 20 20 20 70 49 74  tabase);.    pIt
2bd0e 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20  em->zDatabase = 
2bd0f 30 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70 53  0;.    pItem->pS
2bd10 63 68 65 6d 61 20 3d 20 70 46 69 78 2d 3e 70 53  chema = pFix->pS
2bd11 63 68 65 6d 61 3b 0a 23 69 66 20 21 64 65 66 69  chema;.#if !defi
2bd12 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2bd13 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65  VIEW) || !define
2bd14 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
2bd15 49 47 47 45 52 29 0a 20 20 20 20 69 66 28 20 73  IGGER).    if( s
2bd16 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28  qlite3FixSelect(
2bd17 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e 70 53 65  pFix, pItem->pSe
2bd18 6c 65 63 74 29 20 29 20 72 65 74 75 72 6e 20 31  lect) ) return 1
2bd19 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
2bd1a 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70  3FixExpr(pFix, p
2bd1b 49 74 65 6d 2d 3e 70 4f 6e 29 20 29 20 72 65 74  Item->pOn) ) ret
2bd1c 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 20 20  urn 1;.#endif.  
2bd1d 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2bd1e 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2bd1f 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c  ITE_OMIT_VIEW) |
2bd20 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  | !defined(SQLIT
2bd21 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a  E_OMIT_TRIGGER).
2bd22 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2bd23 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c  nt sqlite3FixSel
2bd24 65 63 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a  ect(.  DbFixer *
2bd25 70 46 69 78 2c 20 20 20 20 20 20 20 2f 2a 20 43  pFix,       /* C
2bd26 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69  ontext of the fi
2bd27 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65  xation */.  Sele
2bd28 63 74 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20  ct *pSelect     
2bd29 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
2bd2a 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 66  tatement to be f
2bd2b 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61  ixed to one data
2bd2c 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69  base */.){.  whi
2bd2d 6c 65 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20  le( pSelect ){. 
2bd2e 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
2bd2f 78 45 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20  xExprList(pFix, 
2bd30 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 29  pSelect->pEList)
2bd31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2bd32 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
2bd33 28 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c  ( sqlite3FixSrcL
2bd34 69 73 74 28 70 46 69 78 2c 20 70 53 65 6c 65 63  ist(pFix, pSelec
2bd35 74 2d 3e 70 53 72 63 29 20 29 7b 0a 20 20 20 20  t->pSrc) ){.    
2bd36 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2bd37 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
2bd38 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70  3FixExpr(pFix, p
2bd39 53 65 6c 65 63 74 2d 3e 70 57 68 65 72 65 29 20  Select->pWhere) 
2bd3a 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
2bd3b 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
2bd3c 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28   sqlite3FixExpr(
2bd3d 70 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70  pFix, pSelect->p
2bd3e 48 61 76 69 6e 67 29 20 29 7b 0a 20 20 20 20 20  Having) ){.     
2bd3f 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
2bd40 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20 70  .    pSelect = p
2bd41 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a  Select->pPrior;.
2bd42 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
2bd43 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
2bd44 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45   int sqlite3FixE
2bd45 78 70 72 28 0a 20 20 44 62 46 69 78 65 72 20 2a  xpr(.  DbFixer *
2bd46 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e  pFix,     /* Con
2bd47 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78 61  text of the fixa
2bd48 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  tion */.  Expr *
2bd49 70 45 78 70 72 20 20 20 20 20 20 20 20 2f 2a 20  pExpr        /* 
2bd4a 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  The expression t
2bd4b 6f 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e  o be fixed to on
2bd4c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b  e database */.){
2bd4d 0a 20 20 77 68 69 6c 65 28 20 70 45 78 70 72 20  .  while( pExpr 
2bd4e 29 7b 0a 20 20 20 20 69 66 28 20 45 78 70 72 48  ){.    if( ExprH
2bd4f 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45  asAnyProperty(pE
2bd50 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  xpr, EP_TokenOnl
2bd51 79 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  y) ) break;.    
2bd52 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
2bd53 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
2bd54 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20  sSelect) ){.    
2bd55 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
2bd56 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70 45 78  Select(pFix, pEx
2bd57 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 20 29  pr->x.pSelect) )
2bd58 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
2bd59 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
2bd5a 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69  sqlite3FixExprLi
2bd5b 73 74 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e  st(pFix, pExpr->
2bd5c 78 2e 70 4c 69 73 74 29 20 29 20 72 65 74 75 72  x.pList) ) retur
2bd5d 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n 1;.    }.    i
2bd5e 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  f( sqlite3FixExp
2bd5f 72 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e 70  r(pFix, pExpr->p
2bd60 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20  Right) ){.      
2bd61 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
2bd62 20 20 20 20 70 45 78 70 72 20 3d 20 70 45 78 70      pExpr = pExp
2bd63 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20  r->pLeft;.  }.  
2bd64 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49  return 0;.}.SQLI
2bd65 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2bd66 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 73  qlite3FixExprLis
2bd67 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46  t(.  DbFixer *pF
2bd68 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ix,     /* Conte
2bd69 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69  xt of the fixati
2bd6a 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  on */.  ExprList
2bd6b 20 2a 70 4c 69 73 74 20 20 20 20 2f 2a 20 54 68   *pList    /* Th
2bd6c 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  e expression to 
2bd6d 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20  be fixed to one 
2bd6e 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20  database */.){. 
2bd6f 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
2bd70 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
2bd71 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69  pItem;.  if( pLi
2bd72 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  st==0 ) return 0
2bd73 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74  ;.  for(i=0, pIt
2bd74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  em=pList->a; i<p
2bd75 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
2bd76 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
2bd77 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
2bd78 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e  pr(pFix, pItem->
2bd79 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  pExpr) ){.      
2bd7a 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
2bd7b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
2bd7c 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
2bd7d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
2bd7e 49 47 47 45 52 0a 53 51 4c 49 54 45 5f 50 52 49  IGGER.SQLITE_PRI
2bd7f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2bd80 46 69 78 54 72 69 67 67 65 72 53 74 65 70 28 0a  FixTriggerStep(.
2bd81 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c    DbFixer *pFix,
2bd82 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
2bd83 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20  of the fixation 
2bd84 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  */.  TriggerStep
2bd85 20 2a 70 53 74 65 70 20 2f 2a 20 54 68 65 20 74   *pStep /* The t
2bd86 72 69 67 67 65 72 20 73 74 65 70 20 62 65 20 66  rigger step be f
2bd87 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61  ixed to one data
2bd88 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69  base */.){.  whi
2bd89 6c 65 28 20 70 53 74 65 70 20 29 7b 0a 20 20 20  le( pStep ){.   
2bd8a 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 53   if( sqlite3FixS
2bd8b 65 6c 65 63 74 28 70 46 69 78 2c 20 70 53 74 65  elect(pFix, pSte
2bd8c 70 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a 20  p->pSelect) ){. 
2bd8d 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2bd8e 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
2bd8f 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78  ite3FixExpr(pFix
2bd90 2c 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65 29  , pStep->pWhere)
2bd91 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2bd92 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
2bd93 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  ( sqlite3FixExpr
2bd94 4c 69 73 74 28 70 46 69 78 2c 20 70 53 74 65 70  List(pFix, pStep
2bd95 2d 3e 70 45 78 70 72 4c 69 73 74 29 20 29 7b 0a  ->pExprList) ){.
2bd96 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
2bd97 20 20 20 20 7d 0a 20 20 20 20 70 53 74 65 70 20      }.    pStep 
2bd98 3d 20 70 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a  = pStep->pNext;.
2bd99 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
2bd9a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  }.#endif../*****
2bd9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2bd9c 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a 2a 2a 2a   attach.c ******
2bd9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bd9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bd9f 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2bda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2bda1 66 69 6c 65 20 61 75 74 68 2e 63 20 2a 2a 2a 2a  file auth.c ****
2bda2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bda3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bda4 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
2bda5 32 30 30 33 20 4a 61 6e 75 61 72 79 20 31 31 0a  2003 January 11.
2bda6 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
2bda7 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2bda8 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2bda9 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2bdaa 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2bdab 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2bdac 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
2bdad 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2bdae 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2bdaf 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2bdb0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2bdb1 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2bdb2 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2bdb3 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2bdb4 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
2bdb5 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
2bdb6 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
2bdb7 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
2bdb8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bdb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bdba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bdbb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2bdbc 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
2bdbd 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74  ains code used t
2bdbe 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
2bdbf 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
2bdc0 6f 72 69 7a 65 72 28 29 0a 2a 2a 20 41 50 49 2e  orizer().** API.
2bdc1 20 20 54 68 69 73 20 66 61 63 69 6c 69 74 79 20    This facility 
2bdc2 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 66  is an optional f
2bdc3 65 61 74 75 72 65 20 6f 66 20 74 68 65 20 6c 69  eature of the li
2bdc4 62 72 61 72 79 2e 20 20 45 6d 62 65 64 64 65 64  brary.  Embedded
2bdc5 0a 2a 2a 20 73 79 73 74 65 6d 73 20 74 68 61 74  .** systems that
2bdc6 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 68 69   do not need thi
2bdc7 73 20 66 61 63 69 6c 69 74 79 20 6d 61 79 20 6f  s facility may o
2bdc8 6d 69 74 20 69 74 20 62 79 20 72 65 63 6f 6d 70  mit it by recomp
2bdc9 69 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6c 69 62  iling.** the lib
2bdca 72 61 72 79 20 77 69 74 68 20 2d 44 53 51 4c 49  rary with -DSQLI
2bdcb 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
2bdcc 41 54 49 4f 4e 3d 31 0a 2a 2f 0a 0a 2f 2a 0a 2a  ATION=1.*/../*.*
2bdcd 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64  * All of the cod
2bdce 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6d  e in this file m
2bdcf 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20 62 79  ay be omitted by
2bdd0 20 64 65 66 69 6e 69 6e 67 20 61 20 73 69 6e 67   defining a sing
2bdd1 6c 65 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a  le.** macro..*/.
2bdd2 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2bdd3 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
2bdd4 4e 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20  N../*.** Set or 
2bdd5 63 6c 65 61 72 20 74 68 65 20 61 63 63 65 73 73  clear the access
2bdd6 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66   authorization f
2bdd7 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  unction..**.** T
2bdd8 68 65 20 61 63 63 65 73 73 20 61 75 74 68 6f 72  he access author
2bdd9 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  ization function
2bdda 20 69 73 20 62 65 20 63 61 6c 6c 65 64 20 64 75   is be called du
2bddb 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61  ring the compila
2bddc 74 69 6f 6e 0a 2a 2a 20 70 68 61 73 65 20 74 6f  tion.** phase to
2bddd 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65   verify that the
2bdde 20 75 73 65 72 20 68 61 73 20 72 65 61 64 20 61   user has read a
2bddf 6e 64 2f 6f 72 20 77 72 69 74 65 20 61 63 63 65  nd/or write acce
2bde0 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20 6f 6e  ss permission on
2bde1 0a 2a 2a 20 76 61 72 69 6f 75 73 20 66 69 65 6c  .** various fiel
2bde2 64 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ds of the databa
2bde3 73 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 61  se.  The first a
2bde4 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61  rgument to the a
2bde5 75 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  uth function.** 
2bde6 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  is a copy of the
2bde7 20 33 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f   3rd argument to
2bde8 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20   this routine.  
2bde9 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
2bdea 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 61 75  ent.** to the au
2bdeb 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f  th function is o
2bdec 6e 65 20 6f 66 20 74 68 65 73 65 20 63 6f 6e 73  ne of these cons
2bded 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  tants:.**.**    
2bdee 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45     SQLITE_CREATE
2bdef 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20  _INDEX.**       
2bdf0 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41  SQLITE_CREATE_TA
2bdf1 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  BLE.**       SQL
2bdf2 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
2bdf3 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53  INDEX.**       S
2bdf4 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
2bdf5 50 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20  P_TABLE.**      
2bdf6 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
2bdf7 45 4d 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20  EMP_TRIGGER.**  
2bdf8 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41       SQLITE_CREA
2bdf9 54 45 5f 54 45 4d 50 5f 56 49 45 57 0a 2a 2a 20  TE_TEMP_VIEW.** 
2bdfa 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45        SQLITE_CRE
2bdfb 41 54 45 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20  ATE_TRIGGER.**  
2bdfc 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41       SQLITE_CREA
2bdfd 54 45 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20  TE_VIEW.**      
2bdfe 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 0a 2a   SQLITE_DELETE.*
2bdff 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
2be00 52 4f 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20  ROP_INDEX.**    
2be01 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54     SQLITE_DROP_T
2be02 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51  ABLE.**       SQ
2be03 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49  LITE_DROP_TEMP_I
2be04 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51  NDEX.**       SQ
2be05 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54  LITE_DROP_TEMP_T
2be06 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51  ABLE.**       SQ
2be07 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54  LITE_DROP_TEMP_T
2be08 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20  RIGGER.**       
2be09 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
2be0a 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53  _VIEW.**       S
2be0b 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47  QLITE_DROP_TRIGG
2be0c 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  ER.**       SQLI
2be0d 54 45 5f 44 52 4f 50 5f 56 49 45 57 0a 2a 2a 20  TE_DROP_VIEW.** 
2be0e 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 53        SQLITE_INS
2be0f 45 52 54 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  ERT.**       SQL
2be10 49 54 45 5f 50 52 41 47 4d 41 0a 2a 2a 20 20 20  ITE_PRAGMA.**   
2be11 20 20 20 20 53 51 4c 49 54 45 5f 52 45 41 44 0a      SQLITE_READ.
2be12 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
2be13 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  SELECT.**       
2be14 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49  SQLITE_TRANSACTI
2be15 4f 4e 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  ON.**       SQLI
2be16 54 45 5f 55 50 44 41 54 45 0a 2a 2a 0a 2a 2a 20  TE_UPDATE.**.** 
2be17 54 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f  The third and fo
2be18 75 72 74 68 20 61 72 67 75 6d 65 6e 74 73 20 74  urth arguments t
2be19 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74  o the auth funct
2be1a 69 6f 6e 20 61 72 65 20 74 68 65 20 6e 61 6d 65  ion are the name
2be1b 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65   of.** the table
2be1c 20 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20   and the column 
2be1d 74 68 61 74 20 61 72 65 20 62 65 69 6e 67 20 61  that are being a
2be1e 63 63 65 73 73 65 64 2e 20 20 54 68 65 20 61 75  ccessed.  The au
2be1f 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73  th function.** s
2be20 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 65 69 74  hould return eit
2be21 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 53  her SQLITE_OK, S
2be22 51 4c 49 54 45 5f 44 45 4e 59 2c 20 6f 72 20 53  QLITE_DENY, or S
2be23 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2e 20 20 49  QLITE_IGNORE.  I
2be24 66 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69  f.** SQLITE_OK i
2be25 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74 20 6d  s returned, it m
2be26 65 61 6e 73 20 74 68 61 74 20 61 63 63 65 73 73  eans that access
2be27 20 69 73 20 61 6c 6c 6f 77 65 64 2e 20 20 53 51   is allowed.  SQ
2be28 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 6d 65 61  LITE_DENY.** mea
2be29 6e 73 20 74 68 61 74 20 74 68 65 20 53 51 4c 20  ns that the SQL 
2be2a 73 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 6e  statement will n
2be2b 65 76 65 72 2d 72 75 6e 20 2d 20 74 68 65 20 73  ever-run - the s
2be2c 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61  qlite3_exec() ca
2be2d 6c 6c 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72  ll.** will retur
2be2e 6e 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 2e  n with an error.
2be2f 20 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20    SQLITE_IGNORE 
2be30 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 53  means that the S
2be31 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  QL statement.** 
2be32 73 68 6f 75 6c 64 20 72 75 6e 20 62 75 74 20 61  should run but a
2be33 74 74 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20  ttempts to read 
2be34 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f  the specified co
2be35 6c 75 6d 6e 20 77 69 6c 6c 20 72 65 74 75 72 6e  lumn will return
2be36 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 61 74 74   NULL.** and att
2be37 65 6d 70 74 73 20 74 6f 20 77 72 69 74 65 20 74  empts to write t
2be38 68 65 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 62  he column will b
2be39 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a  e ignored..**.**
2be3a 20 53 65 74 74 69 6e 67 20 74 68 65 20 61 75 74   Setting the aut
2be3b 68 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 4e 55  h function to NU
2be3c 4c 4c 20 64 69 73 61 62 6c 65 73 20 74 68 69 73  LL disables this
2be3d 20 68 6f 6f 6b 2e 20 20 54 68 65 20 64 65 66 61   hook.  The defa
2be3e 75 6c 74 0a 2a 2a 20 73 65 74 74 69 6e 67 20 6f  ult.** setting o
2be3f 66 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74  f the auth funct
2be40 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ion is NULL..*/.
2be41 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2be42 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
2be43 72 69 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 33  rizer(.  sqlite3
2be44 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28 2a 78 41   *db,.  int (*xA
2be45 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  uth)(void*,int,c
2be46 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
2be47 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
2be48 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c  r*,const char*),
2be49 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b  .  void *pArg.){
2be4a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2be4b 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
2be4c 29 3b 0a 20 20 64 62 2d 3e 78 41 75 74 68 20 3d  );.  db->xAuth =
2be4d 20 78 41 75 74 68 3b 0a 20 20 64 62 2d 3e 70 41   xAuth;.  db->pA
2be4e 75 74 68 41 72 67 20 3d 20 70 41 72 67 3b 0a 20  uthArg = pArg;. 
2be4f 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72   sqlite3ExpirePr
2be50 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73  eparedStatements
2be51 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  (db);.  sqlite3_
2be52 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2be53 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
2be54 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
2be55 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 72  *.** Write an er
2be56 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f  ror message into
2be57 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
2be58 20 74 68 61 74 20 65 78 70 6c 61 69 6e 73 20 74   that explains t
2be59 68 61 74 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d  hat the.** user-
2be5a 73 75 70 70 6c 69 65 64 20 61 75 74 68 6f 72 69  supplied authori
2be5b 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  zation function 
2be5c 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6c 6c 65  returned an ille
2be5d 67 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74  gal value..*/.st
2be5e 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
2be5f 41 75 74 68 42 61 64 52 65 74 75 72 6e 43 6f 64  AuthBadReturnCod
2be60 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  e(Parse *pParse)
2be61 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  {.  sqlite3Error
2be62 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 75 74  Msg(pParse, "aut
2be63 68 6f 72 69 7a 65 72 20 6d 61 6c 66 75 6e 63 74  horizer malfunct
2be64 69 6f 6e 22 29 3b 0a 20 20 70 50 61 72 73 65 2d  ion");.  pParse-
2be65 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52  >rc = SQLITE_ERR
2be66 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76  OR;.}../*.** Inv
2be67 6f 6b 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a  oke the authoriz
2be68 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 66  ation callback f
2be69 6f 72 20 70 65 72 6d 69 73 73 69 6f 6e 20 74 6f  or permission to
2be6a 20 72 65 61 64 20 63 6f 6c 75 6d 6e 20 7a 43 6f   read column zCo
2be6b 6c 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c 65 20  l from.** table 
2be6c 7a 54 61 62 20 69 6e 20 64 61 74 61 62 61 73 65  zTab in database
2be6d 20 7a 44 62 2e 20 54 68 69 73 20 66 75 6e 63 74   zDb. This funct
2be6e 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74  ion assumes that
2be6f 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f   an authorizatio
2be70 6e 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 68 61  n.** callback ha
2be71 73 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65  s been registere
2be72 64 20 28 69 2e 65 2e 20 74 68 61 74 20 73 71 6c  d (i.e. that sql
2be73 69 74 65 33 2e 78 41 75 74 68 20 69 73 20 6e 6f  ite3.xAuth is no
2be74 74 20 4e 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a 20 49  t NULL)..**.** I
2be75 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20  f SQLITE_IGNORE 
2be76 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
2be77 70 45 78 70 72 20 69 73 20 6e 6f 74 20 4e 55 4c  pExpr is not NUL
2be78 4c 2c 20 74 68 65 6e 20 70 45 78 70 72 20 69 73  L, then pExpr is
2be79 20 63 68 61 6e 67 65 64 0a 2a 2a 20 74 6f 20 61   changed.** to a
2be7a 6e 20 53 51 4c 20 4e 55 4c 4c 20 65 78 70 72 65  n SQL NULL expre
2be7b 73 73 69 6f 6e 2e 20 4f 74 68 65 72 77 69 73 65  ssion. Otherwise
2be7c 2c 20 69 66 20 70 45 78 70 72 20 69 73 20 4e 55  , if pExpr is NU
2be7d 4c 4c 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f  LL, then SQLITE_
2be7e 49 47 4e 4f 52 45 0a 2a 2a 20 69 73 20 74 72 65  IGNORE.** is tre
2be7f 61 74 65 64 20 61 73 20 53 51 4c 49 54 45 5f 44  ated as SQLITE_D
2be80 45 4e 59 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ENY. In this cas
2be81 65 20 61 6e 20 65 72 72 6f 72 20 69 73 20 6c 65  e an error is le
2be82 66 74 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f  ft in pParse..*/
2be83 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2be84 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 68 52  int sqlite3AuthR
2be85 65 61 64 43 6f 6c 28 0a 20 20 50 61 72 73 65 20  eadCol(.  Parse 
2be86 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2be87 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2be88 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   parser context 
2be89 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2be8a 2a 7a 54 61 62 2c 20 20 20 20 20 20 20 20 20 20  *zTab,          
2be8b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61       /* Table na
2be8c 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  me */.  const ch
2be8d 61 72 20 2a 7a 43 6f 6c 2c 20 20 20 20 20 20 20  ar *zCol,       
2be8e 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
2be8f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20  n name */.  int 
2be90 69 44 62 20 20 20 20 20 20 20 20 20 20 20 20 20  iDb             
2be91 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2be92 6e 64 65 78 20 6f 66 20 63 6f 6e 74 61 69 6e 69  ndex of containi
2be93 6e 67 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a  ng database. */.
2be94 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
2be95 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20   = pParse->db;  
2be96 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2be97 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 68 61   handle */.  cha
2be98 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62  r *zDb = db->aDb
2be99 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 20 2f 2a 20  [iDb].zName; /* 
2be9a 4e 61 6d 65 20 6f 66 20 61 74 74 61 63 68 65 64  Name of attached
2be9b 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69   database */.  i
2be9c 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
2be9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2be9e 2a 20 41 75 74 68 20 63 61 6c 6c 62 61 63 6b 20  * Auth callback 
2be9f 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a  return code */..
2bea0 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 75 74 68    rc = db->xAuth
2bea1 28 64 62 2d 3e 70 41 75 74 68 41 72 67 2c 20 53  (db->pAuthArg, S
2bea2 51 4c 49 54 45 5f 52 45 41 44 2c 20 7a 54 61 62  QLITE_READ, zTab
2bea3 2c 7a 43 6f 6c 2c 7a 44 62 2c 70 50 61 72 73 65  ,zCol,zDb,pParse
2bea4 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 29 3b  ->zAuthContext);
2bea5 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2bea6 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 69 66  E_DENY ){.    if
2bea7 28 20 64 62 2d 3e 6e 44 62 3e 32 20 7c 7c 20 69  ( db->nDb>2 || i
2bea8 44 62 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  Db!=0 ){.      s
2bea9 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2beaa 50 61 72 73 65 2c 20 22 61 63 63 65 73 73 20 74  Parse, "access t
2beab 6f 20 25 73 2e 25 73 2e 25 73 20 69 73 20 70 72  o %s.%s.%s is pr
2beac 6f 68 69 62 69 74 65 64 22 2c 7a 44 62 2c 7a 54  ohibited",zDb,zT
2bead 61 62 2c 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65  ab,zCol);.    }e
2beae 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
2beaf 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2beb0 65 2c 20 22 61 63 63 65 73 73 20 74 6f 20 25 73  e, "access to %s
2beb1 2e 25 73 20 69 73 20 70 72 6f 68 69 62 69 74 65  .%s is prohibite
2beb2 64 22 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b  d", zTab, zCol);
2beb3 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73  .    }.    pPars
2beb4 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 41  e->rc = SQLITE_A
2beb5 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  UTH;.  }else if(
2beb6 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 47 4e 4f   rc!=SQLITE_IGNO
2beb7 52 45 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45  RE && rc!=SQLITE
2beb8 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
2beb9 65 41 75 74 68 42 61 64 52 65 74 75 72 6e 43 6f  eAuthBadReturnCo
2beba 64 65 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a  de(pParse);.  }.
2bebb 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2bebc 2f 2a 0a 2a 2a 20 54 68 65 20 70 45 78 70 72 20  /*.** The pExpr 
2bebd 73 68 6f 75 6c 64 20 62 65 20 61 20 54 4b 5f 43  should be a TK_C
2bebe 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73 69 6f 6e  OLUMN expression
2bebf 2e 20 20 54 68 65 20 74 61 62 6c 65 20 72 65 66  .  The table ref
2bec0 65 72 72 65 64 20 74 6f 0a 2a 2a 20 69 73 20 69  erred to.** is i
2bec1 6e 20 70 54 61 62 4c 69 73 74 20 6f 72 20 65 6c  n pTabList or el
2bec2 73 65 20 69 74 20 69 73 20 74 68 65 20 4e 45 57  se it is the NEW
2bec3 20 6f 72 20 4f 4c 44 20 74 61 62 6c 65 20 6f 66   or OLD table of
2bec4 20 61 20 74 72 69 67 67 65 72 2e 20 20 0a 2a 2a   a trigger.  .**
2bec5 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
2bec6 20 69 74 20 69 73 20 4f 4b 20 74 6f 20 72 65 61   it is OK to rea
2bec7 64 20 74 68 69 73 20 70 61 72 74 69 63 75 6c 61  d this particula
2bec8 72 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20  r column..**.** 
2bec9 49 66 20 74 68 65 20 61 75 74 68 20 66 75 6e 63  If the auth func
2beca 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c  tion returns SQL
2becb 49 54 45 5f 49 47 4e 4f 52 45 2c 20 63 68 61 6e  ITE_IGNORE, chan
2becc 67 65 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e  ge the TK_COLUMN
2becd 20 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e   .** instruction
2bece 20 69 6e 74 6f 20 61 20 54 4b 5f 4e 55 4c 4c 2e   into a TK_NULL.
2becf 20 20 49 66 20 74 68 65 20 61 75 74 68 20 66 75    If the auth fu
2bed0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53  nction returns S
2bed1 51 4c 49 54 45 5f 44 45 4e 59 2c 0a 2a 2a 20 74  QLITE_DENY,.** t
2bed2 68 65 6e 20 67 65 6e 65 72 61 74 65 20 61 6e 20  hen generate an 
2bed3 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  error..*/.SQLITE
2bed4 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2bed5 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 0a 20  lite3AuthRead(. 
2bed6 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2bed7 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
2bed8 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
2bed9 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20    Expr *pExpr,  
2beda 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
2bedb 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 63 68 65  xpression to che
2bedc 63 6b 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ck authorization
2bedd 20 6f 6e 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20   on */.  Schema 
2bede 2a 70 53 63 68 65 6d 61 2c 20 20 20 20 20 20 2f  *pSchema,      /
2bedf 2a 20 54 68 65 20 73 63 68 65 6d 61 20 6f 66 20  * The schema of 
2bee0 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  the expression *
2bee1 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  /.  SrcList *pTa
2bee2 62 4c 69 73 74 20 20 20 20 20 2f 2a 20 41 6c 6c  bList     /* All
2bee3 20 74 61 62 6c 65 20 74 68 61 74 20 70 45 78 70   table that pExp
2bee4 72 20 6d 69 67 68 74 20 72 65 66 65 72 20 74 6f  r might refer to
2bee5 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
2bee6 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2bee7 62 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  b;.  Table *pTab
2bee8 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 68   = 0;      /* Th
2bee9 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65  e table being re
2beea 61 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ad */.  const ch
2beeb 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 2f 2a  ar *zCol;     /*
2beec 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c   Name of the col
2beed 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65  umn of the table
2beee 20 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63 3b 20   */.  int iSrc; 
2beef 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2bef0 6e 64 65 78 20 69 6e 20 70 54 61 62 4c 69 73 74  ndex in pTabList
2bef1 2d 3e 61 5b 5d 20 6f 66 20 74 61 62 6c 65 20 62  ->a[] of table b
2bef2 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a 20 20 69  eing read */.  i
2bef3 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
2bef4 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65       /* The inde
2bef5 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  x of the databas
2bef6 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  e the expression
2bef7 20 72 65 66 65 72 73 20 74 6f 20 2a 2f 0a 20 20   refers to */.  
2bef8 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20  int iCol;       
2bef9 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
2befa 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c  f column in tabl
2befb 65 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62 2d 3e  e */..  if( db->
2befc 78 41 75 74 68 3d 3d 30 20 29 20 72 65 74 75 72  xAuth==0 ) retur
2befd 6e 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  n;.  iDb = sqlit
2befe 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
2beff 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 63 68  pParse->db, pSch
2bf00 65 6d 61 29 3b 0a 20 20 69 66 28 20 69 44 62 3c  ema);.  if( iDb<
2bf01 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 61  0 ){.    /* An a
2bf02 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 61  ttempt to read a
2bf03 20 63 6f 6c 75 6d 6e 20 6f 75 74 20 6f 66 20 61   column out of a
2bf04 20 73 75 62 71 75 65 72 79 20 6f 72 20 6f 74 68   subquery or oth
2bf05 65 72 0a 20 20 20 20 2a 2a 20 74 65 6d 70 6f 72  er.    ** tempor
2bf06 61 72 79 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20  ary table. */.  
2bf07 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
2bf08 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
2bf09 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c  op==TK_COLUMN ||
2bf0a 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 54   pExpr->op==TK_T
2bf0b 52 49 47 47 45 52 20 29 3b 0a 20 20 69 66 28 20  RIGGER );.  if( 
2bf0c 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 54 52  pExpr->op==TK_TR
2bf0d 49 47 47 45 52 20 29 7b 0a 20 20 20 20 70 54 61  IGGER ){.    pTa
2bf0e 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69  b = pParse->pTri
2bf0f 67 67 65 72 54 61 62 3b 0a 20 20 7d 65 6c 73 65  ggerTab;.  }else
2bf10 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  {.    assert( pT
2bf11 61 62 4c 69 73 74 20 29 3b 0a 20 20 20 20 66 6f  abList );.    fo
2bf12 72 28 69 53 72 63 3d 30 3b 20 41 4c 57 41 59 53  r(iSrc=0; ALWAYS
2bf13 28 69 53 72 63 3c 70 54 61 62 4c 69 73 74 2d 3e  (iSrc<pTabList->
2bf14 6e 53 72 63 29 3b 20 69 53 72 63 2b 2b 29 7b 0a  nSrc); iSrc++){.
2bf15 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
2bf16 3e 69 54 61 62 6c 65 3d 3d 70 54 61 62 4c 69 73  >iTable==pTabLis
2bf17 74 2d 3e 61 5b 69 53 72 63 5d 2e 69 43 75 72 73  t->a[iSrc].iCurs
2bf18 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54  or ){.        pT
2bf19 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61  ab = pTabList->a
2bf1a 5b 69 53 72 63 5d 2e 70 54 61 62 3b 0a 20 20 20  [iSrc].pTab;.   
2bf1b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2bf1c 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
2bf1d 69 43 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43  iCol = pExpr->iC
2bf1e 6f 6c 75 6d 6e 3b 0a 20 20 69 66 28 20 4e 45 56  olumn;.  if( NEV
2bf1f 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20 72 65  ER(pTab==0) ) re
2bf20 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 69 43 6f  turn;..  if( iCo
2bf21 6c 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65  l>=0 ){.    asse
2bf22 72 74 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e  rt( iCol<pTab->n
2bf23 43 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20  Col );.    zCol 
2bf24 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f  = pTab->aCol[iCo
2bf25 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73  l].zName;.  }els
2bf26 65 20 69 66 28 20 70 54 61 62 2d 3e 69 50 4b 65  e if( pTab->iPKe
2bf27 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65  y>=0 ){.    asse
2bf28 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c  rt( pTab->iPKey<
2bf29 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20  pTab->nCol );.  
2bf2a 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61    zCol = pTab->a
2bf2b 43 6f 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79 5d  Col[pTab->iPKey]
2bf2c 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b  .zName;.  }else{
2bf2d 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 22 52 4f 57  .    zCol = "ROW
2bf2e 49 44 22 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  ID";.  }.  asser
2bf2f 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62  t( iDb>=0 && iDb
2bf30 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 69 66  <db->nDb );.  if
2bf31 28 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 3d  ( SQLITE_IGNORE=
2bf32 3d 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64  =sqlite3AuthRead
2bf33 43 6f 6c 28 70 50 61 72 73 65 2c 20 70 54 61 62  Col(pParse, pTab
2bf34 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 2c 20 69  ->zName, zCol, i
2bf35 44 62 29 20 29 7b 0a 20 20 20 20 70 45 78 70 72  Db) ){.    pExpr
2bf36 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a  ->op = TK_NULL;.
2bf37 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20    }.}../*.** Do 
2bf38 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  an authorization
2bf39 20 63 68 65 63 6b 20 75 73 69 6e 67 20 74 68 65   check using the
2bf3a 20 63 6f 64 65 20 61 6e 64 20 61 72 67 75 6d 65   code and argume
2bf3b 6e 74 73 20 67 69 76 65 6e 2e 20 20 52 65 74 75  nts given.  Retu
2bf3c 72 6e 0a 2a 2a 20 65 69 74 68 65 72 20 53 51 4c  rn.** either SQL
2bf3d 49 54 45 5f 4f 4b 20 28 7a 65 72 6f 29 20 6f 72  ITE_OK (zero) or
2bf3e 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 6f   SQLITE_IGNORE o
2bf3f 72 20 53 51 4c 49 54 45 5f 44 45 4e 59 2e 20 20  r SQLITE_DENY.  
2bf40 49 66 20 53 51 4c 49 54 45 5f 44 45 4e 59 0a 2a  If SQLITE_DENY.*
2bf41 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74  * is returned, t
2bf42 68 65 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f  hen the error co
2bf43 75 6e 74 20 61 6e 64 20 65 72 72 6f 72 20 6d 65  unt and error me
2bf44 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 20  ssage in pParse 
2bf45 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20  are.** modified 
2bf46 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a  appropriately..*
2bf47 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2bf48 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 68   int sqlite3Auth
2bf49 43 68 65 63 6b 28 0a 20 20 50 61 72 73 65 20 2a  Check(.  Parse *
2bf4a 70 50 61 72 73 65 2c 0a 20 20 69 6e 74 20 63 6f  pParse,.  int co
2bf4b 64 65 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  de,.  const char
2bf4c 20 2a 7a 41 72 67 31 2c 0a 20 20 63 6f 6e 73 74   *zArg1,.  const
2bf4d 20 63 68 61 72 20 2a 7a 41 72 67 32 2c 0a 20 20   char *zArg2,.  
2bf4e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67  const char *zArg
2bf4f 33 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  3.){.  sqlite3 *
2bf50 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2bf51 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a  .  int rc;..  /*
2bf52 20 44 6f 6e 27 74 20 64 6f 20 61 6e 79 20 61 75   Don't do any au
2bf53 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 68 65 63  thorization chec
2bf54 6b 73 20 69 66 20 74 68 65 20 64 61 74 61 62 61  ks if the databa
2bf55 73 65 20 69 73 20 69 6e 69 74 69 61 6c 69 73 69  se is initialisi
2bf56 6e 67 0a 20 20 2a 2a 20 6f 72 20 69 66 20 74 68  ng.  ** or if th
2bf57 65 20 70 61 72 73 65 72 20 69 73 20 62 65 69 6e  e parser is bein
2bf58 67 20 69 6e 76 6f 6b 65 64 20 66 72 6f 6d 20 77  g invoked from w
2bf59 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 64 65  ithin sqlite3_de
2bf5a 63 6c 61 72 65 5f 76 74 61 62 2e 0a 20 20 2a 2f  clare_vtab..  */
2bf5b 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e  .  if( db->init.
2bf5c 62 75 73 79 20 7c 7c 20 49 4e 5f 44 45 43 4c 41  busy || IN_DECLA
2bf5d 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 72  RE_VTAB ){.    r
2bf5e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2bf5f 0a 20 20 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e  .  }..  if( db->
2bf60 78 41 75 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20  xAuth==0 ){.    
2bf61 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2bf62 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 64 62 2d  ;.  }.  rc = db-
2bf63 3e 78 41 75 74 68 28 64 62 2d 3e 70 41 75 74 68  >xAuth(db->pAuth
2bf64 41 72 67 2c 20 63 6f 64 65 2c 20 7a 41 72 67 31  Arg, code, zArg1
2bf65 2c 20 7a 41 72 67 32 2c 20 7a 41 72 67 33 2c 20  , zArg2, zArg3, 
2bf66 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
2bf67 74 65 78 74 29 3b 0a 20 20 69 66 28 20 72 63 3d  text);.  if( rc=
2bf68 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a  =SQLITE_DENY ){.
2bf69 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2bf6a 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 74  Msg(pParse, "not
2bf6b 20 61 75 74 68 6f 72 69 7a 65 64 22 29 3b 0a 20   authorized");. 
2bf6c 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
2bf6d 53 51 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20 7d  SQLITE_AUTH;.  }
2bf6e 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c  else if( rc!=SQL
2bf6f 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51  ITE_OK && rc!=SQ
2bf70 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20  LITE_IGNORE ){. 
2bf71 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44     rc = SQLITE_D
2bf72 45 4e 59 3b 0a 20 20 20 20 73 71 6c 69 74 65 41  ENY;.    sqliteA
2bf73 75 74 68 42 61 64 52 65 74 75 72 6e 43 6f 64 65  uthBadReturnCode
2bf74 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20  (pParse);.  }.  
2bf75 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2bf76 0a 2a 2a 20 50 75 73 68 20 61 6e 20 61 75 74 68  .** Push an auth
2bf77 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78  orization contex
2bf78 74 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72  t.  After this r
2bf79 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
2bf7a 2c 20 74 68 65 0a 2a 2a 20 7a 41 72 67 33 20 61  , the.** zArg3 a
2bf7b 72 67 75 6d 65 6e 74 20 74 6f 20 61 75 74 68 6f  rgument to autho
2bf7c 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63  rization callbac
2bf7d 6b 73 20 77 69 6c 6c 20 62 65 20 7a 43 6f 6e 74  ks will be zCont
2bf7e 65 78 74 20 75 6e 74 69 6c 0a 2a 2a 20 70 6f 70  ext until.** pop
2bf7f 70 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61 72  ped.  Or if pPar
2bf80 73 65 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74  se==0, this rout
2bf81 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
2bf82 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2bf83 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75  E void sqlite3Au
2bf84 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 0a 20  thContextPush(. 
2bf85 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a   Parse *pParse,.
2bf86 20 20 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70    AuthContext *p
2bf87 43 6f 6e 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73  Context, .  cons
2bf88 74 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74  t char *zContext
2bf89 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50  .){.  assert( pP
2bf8a 61 72 73 65 20 29 3b 0a 20 20 70 43 6f 6e 74 65  arse );.  pConte
2bf8b 78 74 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61  xt->pParse = pPa
2bf8c 72 73 65 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d  rse;.  pContext-
2bf8d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20  >zAuthContext = 
2bf8e 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
2bf8f 74 65 78 74 3b 0a 20 20 70 50 61 72 73 65 2d 3e  text;.  pParse->
2bf90 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a  zAuthContext = z
2bf91 43 6f 6e 74 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  Context;.}../*.*
2bf92 2a 20 50 6f 70 20 61 6e 20 61 75 74 68 6f 72 69  * Pop an authori
2bf93 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 74  zation context t
2bf94 68 61 74 20 77 61 73 20 70 72 65 76 69 6f 75 73  hat was previous
2bf95 6c 79 20 70 75 73 68 65 64 0a 2a 2a 20 62 79 20  ly pushed.** by 
2bf96 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
2bf97 78 74 50 75 73 68 0a 2a 2f 0a 53 51 4c 49 54 45  xtPush.*/.SQLITE
2bf98 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2bf99 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
2bf9a 50 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 20  Pop(AuthContext 
2bf9b 2a 70 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66  *pContext){.  if
2bf9c 28 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72  ( pContext->pPar
2bf9d 73 65 20 29 7b 0a 20 20 20 20 70 43 6f 6e 74 65  se ){.    pConte
2bf9e 78 74 2d 3e 70 50 61 72 73 65 2d 3e 7a 41 75 74  xt->pParse->zAut
2bf9f 68 43 6f 6e 74 65 78 74 20 3d 20 70 43 6f 6e 74  hContext = pCont
2bfa0 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  ext->zAuthContex
2bfa1 74 3b 0a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d  t;.    pContext-
2bfa2 3e 70 50 61 72 73 65 20 3d 20 30 3b 0a 20 20 7d  >pParse = 0;.  }
2bfa3 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
2bfa4 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
2bfa5 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a 2a 2a  IZATION */../***
2bfa6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2bfa7 6f 66 20 61 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a  of auth.c ******
2bfa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bfa9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bfaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2bfab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2bfac 6e 20 66 69 6c 65 20 62 75 69 6c 64 2e 63 20 2a  n file build.c *
2bfad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bfae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bfaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
2bfb0 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
2bfb1 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
2bfb2 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
2bfb3 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
2bfb4 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
2bfb5 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
2bfb6 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
2bfb7 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
2bfb8 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
2bfb9 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
2bfba 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
2bfbb 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
2bfbc 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
2bfbd 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
2bfbe 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
2bfbf 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
2bfc0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
2bfc1 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
2bfc2 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
2bfc3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bfc4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bfc5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bfc6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bfc7 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
2bfc8 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20  contains C code 
2bfc9 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72  routines that ar
2bfca 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  e called by the 
2bfcb 53 51 4c 69 74 65 20 70 61 72 73 65 72 0a 2a 2a  SQLite parser.**
2bfcc 20 77 68 65 6e 20 73 79 6e 74 61 78 20 72 75 6c   when syntax rul
2bfcd 65 73 20 61 72 65 20 72 65 64 75 63 65 64 2e 20  es are reduced. 
2bfce 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e   The routines in
2bfcf 20 74 68 69 73 20 66 69 6c 65 20 68 61 6e 64 6c   this file handl
2bfd0 65 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69  e the.** followi
2bfd1 6e 67 20 6b 69 6e 64 73 20 6f 66 20 53 51 4c 20  ng kinds of SQL 
2bfd2 73 79 6e 74 61 78 3a 0a 2a 2a 0a 2a 2a 20 20 20  syntax:.**.**   
2bfd3 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 0a 2a    CREATE TABLE.*
2bfd4 2a 20 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45  *     DROP TABLE
2bfd5 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 49  .**     CREATE I
2bfd6 4e 44 45 58 0a 2a 2a 20 20 20 20 20 44 52 4f 50  NDEX.**     DROP
2bfd7 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 63 72   INDEX.**     cr
2bfd8 65 61 74 69 6e 67 20 49 44 20 6c 69 73 74 73 0a  eating ID lists.
2bfd9 2a 2a 20 20 20 20 20 42 45 47 49 4e 20 54 52 41  **     BEGIN TRA
2bfda 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  NSACTION.**     
2bfdb 43 4f 4d 4d 49 54 0a 2a 2a 20 20 20 20 20 52 4f  COMMIT.**     RO
2bfdc 4c 4c 42 41 43 4b 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  LLBACK.*/../*.**
2bfdd 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2bfde 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 6e   called when a n
2bfdf 65 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ew SQL statement
2bfe0 20 69 73 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f   is beginning to
2bfe1 0a 2a 2a 20 62 65 20 70 61 72 73 65 64 2e 20 20  .** be parsed.  
2bfe2 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 70  Initialize the p
2bfe3 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20  Parse structure 
2bfe4 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53 51  as needed..*/.SQ
2bfe5 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2bfe6 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61  d sqlite3BeginPa
2bfe7 72 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  rse(Parse *pPars
2bfe8 65 2c 20 69 6e 74 20 65 78 70 6c 61 69 6e 46 6c  e, int explainFl
2bfe9 61 67 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e 65  ag){.  pParse->e
2bfea 78 70 6c 61 69 6e 20 3d 20 28 75 38 29 65 78 70  xplain = (u8)exp
2bfeb 6c 61 69 6e 46 6c 61 67 3b 0a 20 20 70 50 61 72  lainFlag;.  pPar
2bfec 73 65 2d 3e 6e 56 61 72 20 3d 20 30 3b 0a 7d 0a  se->nVar = 0;.}.
2bfed 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2bfee 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
2bfef 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c  E./*.** The Tabl
2bff0 65 4c 6f 63 6b 20 73 74 72 75 63 74 75 72 65 20  eLock structure 
2bff1 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20  is only used by 
2bff2 74 68 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65  the sqlite3Table
2bff3 4c 6f 63 6b 28 29 20 61 6e 64 0a 2a 2a 20 63 6f  Lock() and.** co
2bff4 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 66  deTableLocks() f
2bff5 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72  unctions..*/.str
2bff6 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 7b 0a  uct TableLock {.
2bff7 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
2bff8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
2bff9 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
2bffa 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  g the table to b
2bffb 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e  e locked */.  in
2bffc 74 20 69 54 61 62 3b 20 20 20 20 20 20 20 20 20  t iTab;         
2bffd 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70     /* The root p
2bffe 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  age of the table
2bfff 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f   to be locked */
2c000 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63  .  u8 isWriteLoc
2c001 6b 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  k;      /* True 
2c002 66 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 2e 20  for write lock. 
2c003 20 46 61 6c 73 65 20 66 6f 72 20 61 20 72 65 61   False for a rea
2c004 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73  d lock */.  cons
2c005 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20  t char *zName;  
2c006 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
2c007 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  table */.};../*.
2c008 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 66 61  ** Record the fa
2c009 63 74 20 74 68 61 74 20 77 65 20 77 61 6e 74 20  ct that we want 
2c00a 74 6f 20 6c 6f 63 6b 20 61 20 74 61 62 6c 65 20  to lock a table 
2c00b 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 0a 2a  at run-time.  .*
2c00c 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 74  *.** The table t
2c00d 6f 20 62 65 20 6c 6f 63 6b 65 64 20 68 61 73 20  o be locked has 
2c00e 72 6f 6f 74 20 70 61 67 65 20 69 54 61 62 20 61  root page iTab a
2c00f 6e 64 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 64  nd is found in d
2c010 61 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20  atabase iDb..** 
2c011 41 20 72 65 61 64 20 6f 72 20 61 20 77 72 69 74  A read or a writ
2c012 65 20 6c 6f 63 6b 20 63 61 6e 20 62 65 20 74 61  e lock can be ta
2c013 6b 65 6e 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  ken depending on
2c014 20 69 73 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a 2a   isWritelock..**
2c015 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2c016 20 6a 75 73 74 20 72 65 63 6f 72 64 73 20 74 68   just records th
2c017 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 20  e fact that the 
2c018 6c 6f 63 6b 20 69 73 20 64 65 73 69 72 65 64 2e  lock is desired.
2c019 20 20 54 68 65 0a 2a 2a 20 63 6f 64 65 20 74 6f    The.** code to
2c01a 20 6d 61 6b 65 20 74 68 65 20 6c 6f 63 6b 20 6f   make the lock o
2c01b 63 63 75 72 20 69 73 20 67 65 6e 65 72 61 74 65  ccur is generate
2c01c 64 20 62 79 20 61 20 6c 61 74 65 72 20 63 61 6c  d by a later cal
2c01d 6c 20 74 6f 0a 2a 2a 20 63 6f 64 65 54 61 62 6c  l to.** codeTabl
2c01e 65 4c 6f 63 6b 73 28 29 20 77 68 69 63 68 20 6f  eLocks() which o
2c01f 63 63 75 72 73 20 64 75 72 69 6e 67 20 73 71 6c  ccurs during sql
2c020 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67  ite3FinishCoding
2c021 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
2c022 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2c023 65 33 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 50  e3TableLock(.  P
2c024 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2c025 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
2c026 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44  text */.  int iD
2c027 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b,           /* 
2c028 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  Index of the dat
2c029 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
2c02a 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f   the table to lo
2c02b 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  ck */.  int iTab
2c02c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f  ,          /* Ro
2c02d 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ot page number o
2c02e 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  f the table to b
2c02f 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38  e locked */.  u8
2c030 20 69 73 57 72 69 74 65 4c 6f 63 6b 2c 20 20 20   isWriteLock,   
2c031 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 77   /* True for a w
2c032 72 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63  rite lock */.  c
2c033 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2c034 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
2c035 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63   table to be loc
2c036 6b 65 64 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73  ked */.){.  Pars
2c037 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73  e *pToplevel = s
2c038 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65  qlite3ParseTople
2c039 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 69  vel(pParse);.  i
2c03a 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74  nt i;.  int nByt
2c03b 65 73 3b 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20  es;.  TableLock 
2c03c 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44  *p;.  assert( iD
2c03d 62 3e 3d 30 20 29 3b 0a 0a 20 20 66 6f 72 28 69  b>=0 );..  for(i
2c03e 3d 30 3b 20 69 3c 70 54 6f 70 6c 65 76 65 6c 2d  =0; i<pToplevel-
2c03f 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b  >nTableLock; i++
2c040 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 54 6f 70  ){.    p = &pTop
2c041 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63  level->aTableLoc
2c042 6b 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 2d  k[i];.    if( p-
2c043 3e 69 44 62 3d 3d 69 44 62 20 26 26 20 70 2d 3e  >iDb==iDb && p->
2c044 69 54 61 62 3d 3d 69 54 61 62 20 29 7b 0a 20 20  iTab==iTab ){.  
2c045 20 20 20 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f      p->isWriteLo
2c046 63 6b 20 3d 20 28 70 2d 3e 69 73 57 72 69 74 65  ck = (p->isWrite
2c047 4c 6f 63 6b 20 7c 7c 20 69 73 57 72 69 74 65 4c  Lock || isWriteL
2c048 6f 63 6b 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ock);.      retu
2c049 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  rn;.    }.  }.. 
2c04a 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f 66   nBytes = sizeof
2c04b 28 54 61 62 6c 65 4c 6f 63 6b 29 20 2a 20 28 70  (TableLock) * (p
2c04c 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c 65  Toplevel->nTable
2c04d 4c 6f 63 6b 2b 31 29 3b 0a 20 20 70 54 6f 70 6c  Lock+1);.  pTopl
2c04e 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b  evel->aTableLock
2c04f 20 3d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   =.      sqlite3
2c050 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
2c051 70 54 6f 70 6c 65 76 65 6c 2d 3e 64 62 2c 20 70  pToplevel->db, p
2c052 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65  Toplevel->aTable
2c053 4c 6f 63 6b 2c 20 6e 42 79 74 65 73 29 3b 0a 20  Lock, nBytes);. 
2c054 20 69 66 28 20 70 54 6f 70 6c 65 76 65 6c 2d 3e   if( pToplevel->
2c055 61 54 61 62 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20  aTableLock ){.  
2c056 20 20 70 20 3d 20 26 70 54 6f 70 6c 65 76 65 6c    p = &pToplevel
2c057 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 70 54 6f  ->aTableLock[pTo
2c058 70 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f  plevel->nTableLo
2c059 63 6b 2b 2b 5d 3b 0a 20 20 20 20 70 2d 3e 69 44  ck++];.    p->iD
2c05a 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 2d 3e  b = iDb;.    p->
2c05b 69 54 61 62 20 3d 20 69 54 61 62 3b 0a 20 20 20  iTab = iTab;.   
2c05c 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20   p->isWriteLock 
2c05d 3d 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20  = isWriteLock;. 
2c05e 20 20 20 70 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e     p->zName = zN
2c05f 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ame;.  }else{.  
2c060 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61    pToplevel->nTa
2c061 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20  bleLock = 0;.   
2c062 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 64 62 2d 3e   pToplevel->db->
2c063 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
2c064 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
2c065 6f 64 65 20 61 6e 20 4f 50 5f 54 61 62 6c 65 4c  ode an OP_TableL
2c066 6f 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ock instruction 
2c067 66 6f 72 20 65 61 63 68 20 74 61 62 6c 65 20 6c  for each table l
2c068 6f 63 6b 65 64 20 62 79 20 74 68 65 0a 2a 2a 20  ocked by the.** 
2c069 73 74 61 74 65 6d 65 6e 74 20 28 63 6f 6e 66 69  statement (confi
2c06a 67 75 72 65 64 20 62 79 20 63 61 6c 6c 73 20 74  gured by calls t
2c06b 6f 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f  o sqlite3TableLo
2c06c 63 6b 28 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ck())..*/.static
2c06d 20 76 6f 69 64 20 63 6f 64 65 54 61 62 6c 65 4c   void codeTableL
2c06e 6f 63 6b 73 28 50 61 72 73 65 20 2a 70 50 61 72  ocks(Parse *pPar
2c06f 73 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  se){.  int i;.  
2c070 56 64 62 65 20 2a 70 56 64 62 65 3b 20 0a 0a 20  Vdbe *pVdbe; .. 
2c071 20 70 56 64 62 65 20 3d 20 73 71 6c 69 74 65 33   pVdbe = sqlite3
2c072 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
2c073 0a 20 20 61 73 73 65 72 74 28 20 70 56 64 62 65  .  assert( pVdbe
2c074 21 3d 30 20 29 3b 20 2f 2a 20 73 71 6c 69 74 65  !=0 ); /* sqlite
2c075 33 47 65 74 56 64 62 65 20 63 61 6e 6e 6f 74 20  3GetVdbe cannot 
2c076 66 61 69 6c 3a 20 56 44 42 45 20 61 6c 72 65 61  fail: VDBE alrea
2c077 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  dy allocated */.
2c078 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50  .  for(i=0; i<pP
2c079 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b  arse->nTableLock
2c07a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c  ; i++){.    Tabl
2c07b 65 4c 6f 63 6b 20 2a 70 20 3d 20 26 70 50 61 72  eLock *p = &pPar
2c07c 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 69  se->aTableLock[i
2c07d 5d 3b 0a 20 20 20 20 69 6e 74 20 70 31 20 3d 20  ];.    int p1 = 
2c07e 70 2d 3e 69 44 62 3b 0a 20 20 20 20 73 71 6c 69  p->iDb;.    sqli
2c07f 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 70 56  te3VdbeAddOp4(pV
2c080 64 62 65 2c 20 4f 50 5f 54 61 62 6c 65 4c 6f 63  dbe, OP_TableLoc
2c081 6b 2c 20 70 31 2c 20 70 2d 3e 69 54 61 62 2c 20  k, p1, p->iTab, 
2c082 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 2c 0a  p->isWriteLock,.
2c083 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c084 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 20        p->zName, 
2c085 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a  P4_STATIC);.  }.
2c086 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e  }.#else.  #defin
2c087 65 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73  e codeTableLocks
2c088 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  (x).#endif../*.*
2c089 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
2c08a 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61  s called after a
2c08b 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 74   single SQL stat
2c08c 65 6d 65 6e 74 20 68 61 73 20 62 65 65 6e 0a 2a  ement has been.*
2c08d 2a 20 70 61 72 73 65 64 20 61 6e 64 20 61 20 56  * parsed and a V
2c08e 44 42 45 20 70 72 6f 67 72 61 6d 20 74 6f 20 65  DBE program to e
2c08f 78 65 63 75 74 65 20 74 68 61 74 20 73 74 61 74  xecute that stat
2c090 65 6d 65 6e 74 20 68 61 73 20 62 65 65 6e 0a 2a  ement has been.*
2c091 2a 20 70 72 65 70 61 72 65 64 2e 20 20 54 68 69  * prepared.  Thi
2c092 73 20 72 6f 75 74 69 6e 65 20 70 75 74 73 20 74  s routine puts t
2c093 68 65 20 66 69 6e 69 73 68 69 6e 67 20 74 6f 75  he finishing tou
2c094 63 68 65 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 56  ches on the.** V
2c095 44 42 45 20 70 72 6f 67 72 61 6d 20 61 6e 64 20  DBE program and 
2c096 72 65 73 65 74 73 20 74 68 65 20 70 50 61 72 73  resets the pPars
2c097 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  e structure for 
2c098 74 68 65 20 6e 65 78 74 0a 2a 2a 20 70 61 72 73  the next.** pars
2c099 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  e..**.** Note th
2c09a 61 74 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f  at if an error o
2c09b 63 63 75 72 72 65 64 2c 20 69 74 20 6d 69 67 68  ccurred, it migh
2c09c 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68  t be the case th
2c09d 61 74 0a 2a 2a 20 6e 6f 20 56 44 42 45 20 63 6f  at.** no VDBE co
2c09e 64 65 20 77 61 73 20 67 65 6e 65 72 61 74 65 64  de was generated
2c09f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2c0a0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2c0a1 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 50 61 72  FinishCoding(Par
2c0a2 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73  se *pParse){.  s
2c0a3 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64  qlite3 *db;.  Vd
2c0a4 62 65 20 2a 76 3b 0a 0a 20 20 64 62 20 3d 20 70  be *v;..  db = p
2c0a5 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
2c0a6 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2c0a7 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  d ) return;.  if
2c0a8 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  ( pParse->nested
2c0a9 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
2c0aa 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20   pParse->nErr ) 
2c0ab 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 42 65  return;..  /* Be
2c0ac 67 69 6e 20 62 79 20 67 65 6e 65 72 61 74 69 6e  gin by generatin
2c0ad 67 20 73 6f 6d 65 20 74 65 72 6d 69 6e 61 74 69  g some terminati
2c0ae 6f 6e 20 63 6f 64 65 20 61 74 20 74 68 65 20 65  on code at the e
2c0af 6e 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 76  nd of the.  ** v
2c0b0 64 62 65 20 70 72 6f 67 72 61 6d 0a 20 20 2a 2f  dbe program.  */
2c0b1 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
2c0b2 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
2c0b3 20 61 73 73 65 72 74 28 20 21 70 50 61 72 73 65   assert( !pParse
2c0b4 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20 0a  ->isMultiWrite .
2c0b5 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
2c0b6 33 56 64 62 65 41 73 73 65 72 74 4d 61 79 41 62  3VdbeAssertMayAb
2c0b7 6f 72 74 28 76 2c 20 70 50 61 72 73 65 2d 3e 6d  ort(v, pParse->m
2c0b8 61 79 41 62 6f 72 74 29 29 3b 0a 20 20 69 66 28  ayAbort));.  if(
2c0b9 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   v ){.    sqlite
2c0ba 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f  3VdbeAddOp0(v, O
2c0bb 50 5f 48 61 6c 74 29 3b 0a 0a 20 20 20 20 2f 2a  P_Halt);..    /*
2c0bc 20 54 68 65 20 63 6f 6f 6b 69 65 20 6d 61 73 6b   The cookie mask
2c0bd 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 62 69   contains one bi
2c0be 74 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62  t for each datab
2c0bf 61 73 65 20 66 69 6c 65 20 6f 70 65 6e 2e 0a 20  ase file open.. 
2c0c0 20 20 20 2a 2a 20 28 42 69 74 20 30 20 69 73 20     ** (Bit 0 is 
2c0c1 66 6f 72 20 6d 61 69 6e 2c 20 62 69 74 20 31 20  for main, bit 1 
2c0c2 69 73 20 66 6f 72 20 74 65 6d 70 2c 20 61 6e 64  is for temp, and
2c0c3 20 73 6f 20 66 6f 72 74 68 2e 29 20 20 42 69 74   so forth.)  Bit
2c0c4 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 73 65 74  s are.    ** set
2c0c5 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61   for each databa
2c0c6 73 65 20 74 68 61 74 20 69 73 20 75 73 65 64 2e  se that is used.
2c0c7 20 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20    Generate code 
2c0c8 74 6f 20 73 74 61 72 74 20 61 0a 20 20 20 20 2a  to start a.    *
2c0c9 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  * transaction on
2c0ca 20 65 61 63 68 20 75 73 65 64 20 64 61 74 61 62   each used datab
2c0cb 61 73 65 20 61 6e 64 20 74 6f 20 76 65 72 69 66  ase and to verif
2c0cc 79 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  y the schema coo
2c0cd 6b 69 65 0a 20 20 20 20 2a 2a 20 6f 6e 20 65 61  kie.    ** on ea
2c0ce 63 68 20 75 73 65 64 20 64 61 74 61 62 61 73 65  ch used database
2c0cf 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
2c0d0 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47   pParse->cookieG
2c0d1 6f 74 6f 3e 30 20 29 7b 0a 20 20 20 20 20 20 79  oto>0 ){.      y
2c0d2 44 62 4d 61 73 6b 20 6d 61 73 6b 3b 0a 20 20 20  DbMask mask;.   
2c0d3 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20     int iDb;.    
2c0d4 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2c0d5 70 48 65 72 65 28 76 2c 20 70 50 61 72 73 65 2d  pHere(v, pParse-
2c0d6 3e 63 6f 6f 6b 69 65 47 6f 74 6f 2d 31 29 3b 0a  >cookieGoto-1);.
2c0d7 20 20 20 20 20 20 66 6f 72 28 69 44 62 3d 30 2c        for(iDb=0,
2c0d8 20 6d 61 73 6b 3d 31 3b 20 69 44 62 3c 64 62 2d   mask=1; iDb<db-
2c0d9 3e 6e 44 62 3b 20 6d 61 73 6b 3c 3c 3d 31 2c 20  >nDb; mask<<=1, 
2c0da 69 44 62 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  iDb++){.        
2c0db 69 66 28 20 28 6d 61 73 6b 20 26 20 70 50 61 72  if( (mask & pPar
2c0dc 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 29 3d  se->cookieMask)=
2c0dd 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
2c0de 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2c0df 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
2c0e0 44 62 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  Db);.        sql
2c0e1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2c0e2 2c 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c  ,OP_Transaction,
2c0e3 20 69 44 62 2c 20 28 6d 61 73 6b 20 26 20 70 50   iDb, (mask & pP
2c0e4 61 72 73 65 2d 3e 77 72 69 74 65 4d 61 73 6b 29  arse->writeMask)
2c0e5 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66  !=0);.        if
2c0e6 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d  ( db->init.busy=
2c0e7 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
2c0e8 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53  assert( sqlite3S
2c0e9 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64  chemaMutexHeld(d
2c0ea 62 2c 20 69 44 62 2c 20 30 29 20 29 3b 0a 20 20  b, iDb, 0) );.  
2c0eb 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2c0ec 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2c0ed 56 65 72 69 66 79 43 6f 6f 6b 69 65 2c 0a 20 20  VerifyCookie,.  
2c0ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c0ef 20 20 20 20 20 20 20 20 20 20 69 44 62 2c 20 70            iDb, p
2c0f0 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 56 61 6c  Parse->cookieVal
2c0f1 75 65 5b 69 44 62 5d 2c 0a 20 20 20 20 20 20 20  ue[iDb],.       
2c0f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c0f3 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
2c0f4 5d 2e 70 53 63 68 65 6d 61 2d 3e 69 47 65 6e 65  ].pSchema->iGene
2c0f5 72 61 74 69 6f 6e 29 3b 0a 20 20 20 20 20 20 20  ration);.       
2c0f6 20 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64   }.      }.#ifnd
2c0f7 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2c0f8 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
2c0f9 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20    {.        int 
2c0fa 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  i;.        for(i
2c0fb 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 56  =0; i<pParse->nV
2c0fc 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20  tabLock; i++){. 
2c0fd 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 76           char *v
2c0fe 74 61 62 20 3d 20 28 63 68 61 72 20 2a 29 73 71  tab = (char *)sq
2c0ff 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64  lite3GetVTable(d
2c100 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56 74 61  b, pParse->apVta
2c101 62 4c 6f 63 6b 5b 69 5d 29 3b 0a 20 20 20 20 20  bLock[i]);.     
2c102 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c103 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 42 65  AddOp4(v, OP_VBe
2c104 67 69 6e 2c 20 30 2c 20 30 2c 20 30 2c 20 76 74  gin, 0, 0, 0, vt
2c105 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20  ab, P4_VTAB);.  
2c106 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2c107 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63  pParse->nVtabLoc
2c108 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23  k = 0;.      }.#
2c109 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f 2a 20  endif..      /* 
2c10a 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 63 6f 6f  Once all the coo
2c10b 6b 69 65 73 20 68 61 76 65 20 62 65 65 6e 20 76  kies have been v
2c10c 65 72 69 66 69 65 64 20 61 6e 64 20 74 72 61 6e  erified and tran
2c10d 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e 65 64 2c  sactions opened,
2c10e 20 0a 20 20 20 20 20 20 2a 2a 20 6f 62 74 61 69   .      ** obtai
2c10f 6e 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74  n the required t
2c110 61 62 6c 65 2d 6c 6f 63 6b 73 2e 20 54 68 69 73  able-locks. This
2c111 20 69 73 20 61 20 6e 6f 2d 6f 70 20 75 6e 6c 65   is a no-op unle
2c112 73 73 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a  ss the .      **
2c113 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 65   shared-cache fe
2c114 61 74 75 72 65 20 69 73 20 65 6e 61 62 6c 65 64  ature is enabled
2c115 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2c116 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28   codeTableLocks(
2c117 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 20 20  pParse);..      
2c118 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e  /* Initialize an
2c119 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20  y AUTOINCREMENT 
2c11a 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
2c11b 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 20 20  required..      
2c11c 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
2c11d 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67  AutoincrementBeg
2c11e 69 6e 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20  in(pParse);..   
2c11f 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 6a     /* Finally, j
2c120 75 6d 70 20 62 61 63 6b 20 74 6f 20 74 68 65 20  ump back to the 
2c121 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
2c122 20 65 78 65 63 75 74 61 62 6c 65 20 63 6f 64 65   executable code
2c123 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  . */.      sqlit
2c124 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c125 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 50 61 72  OP_Goto, 0, pPar
2c126 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 29 3b  se->cookieGoto);
2c127 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20 2f  .    }.  }...  /
2c128 2a 20 47 65 74 20 74 68 65 20 56 44 42 45 20 70  * Get the VDBE p
2c129 72 6f 67 72 61 6d 20 72 65 61 64 79 20 66 6f 72  rogram ready for
2c12a 20 65 78 65 63 75 74 69 6f 6e 0a 20 20 2a 2f 0a   execution.  */.
2c12b 20 20 69 66 28 20 76 20 26 26 20 41 4c 57 41 59    if( v && ALWAY
2c12c 53 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d  S(pParse->nErr==
2c12d 30 29 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f  0) && !db->mallo
2c12e 63 46 61 69 6c 65 64 20 29 7b 0a 23 69 66 64 65  cFailed ){.#ifde
2c12f 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
2c130 20 20 20 46 49 4c 45 20 2a 74 72 61 63 65 20 3d     FILE *trace =
2c131 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
2c132 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65 29 21  LITE_VdbeTrace)!
2c133 3d 30 20 3f 20 73 74 64 6f 75 74 20 3a 20 30 3b  =0 ? stdout : 0;
2c134 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2c135 54 72 61 63 65 28 76 2c 20 74 72 61 63 65 29 3b  Trace(v, trace);
2c136 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65  .#endif.    asse
2c137 72 74 28 20 70 50 61 72 73 65 2d 3e 69 43 61 63  rt( pParse->iCac
2c138 68 65 4c 65 76 65 6c 3d 3d 30 20 29 3b 20 20 2f  heLevel==0 );  /
2c139 2a 20 44 69 73 61 62 6c 65 73 20 61 6e 64 20 72  * Disables and r
2c13a 65 2d 65 6e 61 62 6c 65 73 20 6d 61 74 63 68 20  e-enables match 
2c13b 2a 2f 0a 20 20 20 20 2f 2a 20 41 20 6d 69 6e 69  */.    /* A mini
2c13c 6d 75 6d 20 6f 66 20 6f 6e 65 20 63 75 72 73 6f  mum of one curso
2c13d 72 20 69 73 20 72 65 71 75 69 72 65 64 20 69 66  r is required if
2c13e 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 69   autoincrement i
2c13f 73 20 75 73 65 64 0a 20 20 20 20 2a 20 20 53 65  s used.    *  Se
2c140 65 20 74 69 63 6b 65 74 20 5b 61 36 39 36 33 37  e ticket [a69637
2c141 39 63 31 66 30 38 38 36 36 5d 20 2a 2f 0a 20 20  9c1f08866] */.  
2c142 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70 41    if( pParse->pA
2c143 69 6e 63 21 3d 30 20 26 26 20 70 50 61 72 73 65  inc!=0 && pParse
2c144 2d 3e 6e 54 61 62 3d 3d 30 20 29 20 70 50 61 72  ->nTab==0 ) pPar
2c145 73 65 2d 3e 6e 54 61 62 20 3d 20 31 3b 0a 20 20  se->nTab = 1;.  
2c146 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b    sqlite3VdbeMak
2c147 65 52 65 61 64 79 28 76 2c 20 70 50 61 72 73 65  eReady(v, pParse
2c148 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  );.    pParse->r
2c149 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  c = SQLITE_DONE;
2c14a 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c  .    pParse->col
2c14b 4e 61 6d 65 73 53 65 74 20 3d 20 30 3b 0a 20 20  NamesSet = 0;.  
2c14c 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 72 73  }else{.    pPars
2c14d 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45  e->rc = SQLITE_E
2c14e 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 70 50 61 72  RROR;.  }.  pPar
2c14f 73 65 2d 3e 6e 54 61 62 20 3d 20 30 3b 0a 20 20  se->nTab = 0;.  
2c150 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30  pParse->nMem = 0
2c151 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 53 65 74  ;.  pParse->nSet
2c152 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e   = 0;.  pParse->
2c153 6e 56 61 72 20 3d 20 30 3b 0a 20 20 70 50 61 72  nVar = 0;.  pPar
2c154 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d  se->cookieMask =
2c155 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f   0;.  pParse->co
2c156 6f 6b 69 65 47 6f 74 6f 20 3d 20 30 3b 0a 7d 0a  okieGoto = 0;.}.
2c157 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70  ./*.** Run the p
2c158 61 72 73 65 72 20 61 6e 64 20 63 6f 64 65 20 67  arser and code g
2c159 65 6e 65 72 61 74 6f 72 20 72 65 63 75 72 73 69  enerator recursi
2c15a 76 65 6c 79 20 69 6e 20 6f 72 64 65 72 20 74 6f  vely in order to
2c15b 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64   generate.** cod
2c15c 65 20 66 6f 72 20 74 68 65 20 53 51 4c 20 73 74  e for the SQL st
2c15d 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 20 6f 6e  atement given on
2c15e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
2c15f 65 20 70 50 61 72 73 65 20 63 6f 6e 74 65 78 74  e pParse context
2c160 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 75 6e  .** currently un
2c161 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
2c162 2e 20 20 57 68 65 6e 20 74 68 65 20 70 61 72 73  .  When the pars
2c163 65 72 20 69 73 20 72 75 6e 20 72 65 63 75 72 73  er is run recurs
2c164 69 76 65 6c 79 0a 2a 2a 20 74 68 69 73 20 77 61  ively.** this wa
2c165 79 2c 20 74 68 65 20 66 69 6e 61 6c 20 4f 50 5f  y, the final OP_
2c166 48 61 6c 74 20 69 73 20 6e 6f 74 20 61 70 70 65  Halt is not appe
2c167 6e 64 65 64 20 61 6e 64 20 6f 74 68 65 72 20 69  nded and other i
2c168 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a  nitialization.**
2c169 20 61 6e 64 20 66 69 6e 61 6c 69 7a 61 74 69 6f   and finalizatio
2c16a 6e 20 73 74 65 70 73 20 61 72 65 20 6f 6d 69 74  n steps are omit
2c16b 74 65 64 20 62 65 63 61 75 73 65 20 74 68 6f 73  ted because thos
2c16c 65 20 61 72 65 20 68 61 6e 64 6c 69 6e 67 20 62  e are handling b
2c16d 79 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 6d 6f  y the.** outermo
2c16e 73 74 20 70 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a  st parser..**.**
2c16f 20 4e 6f 74 20 65 76 65 72 79 74 68 69 6e 67 20   Not everything 
2c170 69 73 20 6e 65 73 74 61 62 6c 65 2e 20 20 54 68  is nestable.  Th
2c171 69 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 64  is facility is d
2c172 65 73 69 67 6e 65 64 20 74 6f 20 70 65 72 6d 69  esigned to permi
2c173 74 0a 2a 2a 20 49 4e 53 45 52 54 2c 20 55 50 44  t.** INSERT, UPD
2c174 41 54 45 2c 20 61 6e 64 20 44 45 4c 45 54 45 20  ATE, and DELETE 
2c175 6f 70 65 72 61 74 69 6f 6e 73 20 61 67 61 69 6e  operations again
2c176 73 74 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  st SQLITE_MASTER
2c177 2e 20 20 55 73 65 0a 2a 2a 20 63 61 72 65 20 69  .  Use.** care i
2c178 66 20 79 6f 75 20 64 65 63 69 64 65 20 74 6f 20  f you decide to 
2c179 74 72 79 20 74 6f 20 75 73 65 20 74 68 69 73 20  try to use this 
2c17a 72 6f 75 74 69 6e 65 20 66 6f 72 20 73 6f 6d 65  routine for some
2c17b 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e   other purposes.
2c17c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2c17d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4e  TE void sqlite3N
2c17e 65 73 74 65 64 50 61 72 73 65 28 50 61 72 73 65  estedParse(Parse
2c17f 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20   *pParse, const 
2c180 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
2c181 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
2c182 70 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b  p;.  char *zSql;
2c183 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
2c184 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20   = 0;.  sqlite3 
2c185 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2c186 3b 0a 23 20 64 65 66 69 6e 65 20 53 41 56 45 5f  ;.# define SAVE_
2c187 53 5a 20 20 28 73 69 7a 65 6f 66 28 50 61 72 73  SZ  (sizeof(Pars
2c188 65 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 50 61  e) - offsetof(Pa
2c189 72 73 65 2c 6e 56 61 72 29 29 0a 20 20 63 68 61  rse,nVar)).  cha
2c18a 72 20 73 61 76 65 42 75 66 5b 53 41 56 45 5f 53  r saveBuf[SAVE_S
2c18b 5a 5d 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73  Z];..  if( pPars
2c18c 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e  e->nErr ) return
2c18d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
2c18e 73 65 2d 3e 6e 65 73 74 65 64 3c 31 30 20 29 3b  se->nested<10 );
2c18f 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 73 68 6f    /* Nesting sho
2c190 75 6c 64 20 6f 6e 6c 79 20 62 65 20 6f 66 20 6c  uld only be of l
2c191 69 6d 69 74 65 64 20 64 65 70 74 68 20 2a 2f 0a  imited depth */.
2c192 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
2c193 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 53 71 6c 20  Format);.  zSql 
2c194 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74  = sqlite3VMPrint
2c195 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  f(db, zFormat, a
2c196 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29  p);.  va_end(ap)
2c197 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20  ;.  if( zSql==0 
2c198 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 20  ){.    return;  
2c199 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 75 73   /* A malloc mus
2c19a 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a 2f  t have failed */
2c19b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e 6e  .  }.  pParse->n
2c19c 65 73 74 65 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70  ested++;.  memcp
2c19d 79 28 73 61 76 65 42 75 66 2c 20 26 70 50 61 72  y(saveBuf, &pPar
2c19e 73 65 2d 3e 6e 56 61 72 2c 20 53 41 56 45 5f 53  se->nVar, SAVE_S
2c19f 5a 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 50  Z);.  memset(&pP
2c1a0 61 72 73 65 2d 3e 6e 56 61 72 2c 20 30 2c 20 53  arse->nVar, 0, S
2c1a1 41 56 45 5f 53 5a 29 3b 0a 20 20 73 71 6c 69 74  AVE_SZ);.  sqlit
2c1a2 65 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 72  e3RunParser(pPar
2c1a3 73 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72 4d  se, zSql, &zErrM
2c1a4 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  sg);.  sqlite3Db
2c1a5 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67  Free(db, zErrMsg
2c1a6 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
2c1a7 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20  ee(db, zSql);.  
2c1a8 6d 65 6d 63 70 79 28 26 70 50 61 72 73 65 2d 3e  memcpy(&pParse->
2c1a9 6e 56 61 72 2c 20 73 61 76 65 42 75 66 2c 20 53  nVar, saveBuf, S
2c1aa 41 56 45 5f 53 5a 29 3b 0a 20 20 70 50 61 72 73  AVE_SZ);.  pPars
2c1ab 65 2d 3e 6e 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a  e->nested--;.}..
2c1ac 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65  /*.** Locate the
2c1ad 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63   in-memory struc
2c1ae 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69  ture that descri
2c1af 62 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72  bes a particular
2c1b0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62   database.** tab
2c1b1 6c 65 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d  le given the nam
2c1b2 65 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20  e of that table 
2c1b3 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29  and (optionally)
2c1b4 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
2c1b5 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
2c1b6 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c  taining the tabl
2c1b7 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20  e.  Return NULL 
2c1b8 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a  if not found..**
2c1b9 0a 2a 2a 20 49 66 20 7a 44 61 74 61 62 61 73 65  .** If zDatabase
2c1ba 20 69 73 20 30 2c 20 61 6c 6c 20 64 61 74 61 62   is 0, all datab
2c1bb 61 73 65 73 20 61 72 65 20 73 65 61 72 63 68 65  ases are searche
2c1bc 64 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  d for the table 
2c1bd 61 6e 64 20 74 68 65 0a 2a 2a 20 66 69 72 73 74  and the.** first
2c1be 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c 65 20   matching table 
2c1bf 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 28 4e  is returned.  (N
2c1c0 6f 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 64  o checking for d
2c1c1 75 70 6c 69 63 61 74 65 20 74 61 62 6c 65 0a 2a  uplicate table.*
2c1c2 2a 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e  * names is done.
2c1c3 29 20 20 54 68 65 20 73 65 61 72 63 68 20 6f 72  )  The search or
2c1c4 64 65 72 20 69 73 20 54 45 4d 50 20 66 69 72 73  der is TEMP firs
2c1c5 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68  t, then MAIN, th
2c1c6 65 6e 20 61 6e 79 0a 2a 2a 20 61 75 78 69 6c 69  en any.** auxili
2c1c7 61 72 79 20 64 61 74 61 62 61 73 65 73 20 61 64  ary databases ad
2c1c8 64 65 64 20 75 73 69 6e 67 20 74 68 65 20 41 54  ded using the AT
2c1c9 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  TACH command..**
2c1ca 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c  .** See also sql
2c1cb 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
2c1cc 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
2c1cd 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69  VATE Table *sqli
2c1ce 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73 71 6c  te3FindTable(sql
2c1cf 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
2c1d0 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e  char *zName, con
2c1d1 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61  st char *zDataba
2c1d2 73 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 20  se){.  Table *p 
2c1d3 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  = 0;.  int i;.  
2c1d4 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 61 73 73  int nName;.  ass
2c1d5 65 72 74 28 20 7a 4e 61 6d 65 21 3d 30 20 29 3b  ert( zName!=0 );
2c1d6 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  .  nName = sqlit
2c1d7 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
2c1d8 29 3b 0a 20 20 2f 2a 20 41 6c 6c 20 6d 75 74 65  );.  /* All mute
2c1d9 78 65 73 20 61 72 65 20 72 65 71 75 69 72 65 64  xes are required
2c1da 20 66 6f 72 20 73 63 68 65 6d 61 20 61 63 63 65   for schema acce
2c1db 73 73 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 77  ss.  Make sure w
2c1dc 65 20 68 6f 6c 64 20 74 68 65 6d 2e 20 2a 2f 0a  e hold them. */.
2c1dd 20 20 61 73 73 65 72 74 28 20 7a 44 61 74 61 62    assert( zDatab
2c1de 61 73 65 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65  ase!=0 || sqlite
2c1df 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
2c1e0 74 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 66  texes(db) );.  f
2c1e1 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42  or(i=OMIT_TEMPDB
2c1e2 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
2c1e3 29 7b 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28  ){.    int j = (
2c1e4 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20  i<2) ? i^1 : i; 
2c1e5 20 20 2f 2a 20 53 65 61 72 63 68 20 54 45 4d 50    /* Search TEMP
2c1e6 20 62 65 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a   before MAIN */.
2c1e7 20 20 20 20 69 66 28 20 7a 44 61 74 61 62 61 73      if( zDatabas
2c1e8 65 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53  e!=0 && sqlite3S
2c1e9 74 72 49 43 6d 70 28 7a 44 61 74 61 62 61 73 65  trICmp(zDatabase
2c1ea 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61  , db->aDb[j].zNa
2c1eb 6d 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  me) ) continue;.
2c1ec 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
2c1ed 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65  te3SchemaMutexHe
2c1ee 6c 64 28 64 62 2c 20 6a 2c 20 30 29 20 29 3b 0a  ld(db, j, 0) );.
2c1ef 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48      p = sqlite3H
2c1f0 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 44 62  ashFind(&db->aDb
2c1f1 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c  [j].pSchema->tbl
2c1f2 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  Hash, zName, nNa
2c1f3 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29  me);.    if( p )
2c1f4 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65   break;.  }.  re
2c1f5 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
2c1f6 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d 6d   Locate the in-m
2c1f7 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 20  emory structure 
2c1f8 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 61  that describes a
2c1f9 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61   particular data
2c1fa 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 67 69  base.** table gi
2c1fb 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  ven the name of 
2c1fc 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64 20 28  that table and (
2c1fd 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68 65 20  optionally) the 
2c1fe 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64  name of the.** d
2c1ff 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
2c200 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52  ng the table.  R
2c201 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f  eturn NULL if no
2c202 74 20 66 6f 75 6e 64 2e 20 20 41 6c 73 6f 20 6c  t found.  Also l
2c203 65 61 76 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72  eave an.** error
2c204 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72   message in pPar
2c205 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a  se->zErrMsg..**.
2c206 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63  ** The differenc
2c207 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 72  e between this r
2c208 6f 75 74 69 6e 65 20 61 6e 64 20 73 71 6c 69 74  outine and sqlit
2c209 65 33 46 69 6e 64 54 61 62 6c 65 28 29 20 69 73  e3FindTable() is
2c20a 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 72 6f   that this.** ro
2c20b 75 74 69 6e 65 20 6c 65 61 76 65 73 20 61 6e 20  utine leaves an 
2c20c 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
2c20d 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
2c20e 20 77 68 65 72 65 0a 2a 2a 20 73 71 6c 69 74 65   where.** sqlite
2c20f 33 46 69 6e 64 54 61 62 6c 65 28 29 20 64 6f 65  3FindTable() doe
2c210 73 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  s not..*/.SQLITE
2c211 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a  _PRIVATE Table *
2c212 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62  sqlite3LocateTab
2c213 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  le(.  Parse *pPa
2c214 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rse,         /* 
2c215 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68  context in which
2c216 20 74 6f 20 72 65 70 6f 72 74 20 65 72 72 6f 72   to report error
2c217 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69 65  s */.  int isVie
2c218 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  w,            /*
2c219 20 54 72 75 65 20 69 66 20 6c 6f 6f 6b 69 6e 67   True if looking
2c21a 20 66 6f 72 20 61 20 56 49 45 57 20 72 61 74 68   for a VIEW rath
2c21b 65 72 20 74 68 61 6e 20 61 20 54 41 42 4c 45 20  er than a TABLE 
2c21c 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2c21d 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e  *zName,     /* N
2c21e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
2c21f 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20   we are looking 
2c220 66 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  for */.  const c
2c221 68 61 72 20 2a 7a 44 62 61 73 65 20 20 20 20 20  har *zDbase     
2c222 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64  /* Name of the d
2c223 61 74 61 62 61 73 65 2e 20 20 4d 69 67 68 74 20  atabase.  Might 
2c224 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20  be NULL */.){.  
2c225 54 61 62 6c 65 20 2a 70 3b 0a 0a 20 20 2f 2a 20  Table *p;..  /* 
2c226 52 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73  Read the databas
2c227 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20  e schema. If an 
2c228 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65  error occurs, le
2c229 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
2c22a 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f  sage.  ** and co
2c22b 64 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64  de in pParse and
2c22c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f   return NULL. */
2c22d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
2c22e 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  !=sqlite3ReadSch
2c22f 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20  ema(pParse) ){. 
2c230 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2c231 0a 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46  ..  p = sqlite3F
2c232 69 6e 64 54 61 62 6c 65 28 70 50 61 72 73 65 2d  indTable(pParse-
2c233 3e 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 61  >db, zName, zDba
2c234 73 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  se);.  if( p==0 
2c235 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
2c236 72 20 2a 7a 4d 73 67 20 3d 20 69 73 56 69 65 77  r *zMsg = isView
2c237 20 3f 20 22 6e 6f 20 73 75 63 68 20 76 69 65 77   ? "no such view
2c238 22 20 3a 20 22 6e 6f 20 73 75 63 68 20 74 61 62  " : "no such tab
2c239 6c 65 22 3b 0a 20 20 20 20 69 66 28 20 7a 44 62  le";.    if( zDb
2c23a 61 73 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ase ){.      sql
2c23b 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2c23c 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 22  rse, "%s: %s.%s"
2c23d 2c 20 7a 4d 73 67 2c 20 7a 44 62 61 73 65 2c 20  , zMsg, zDbase, 
2c23e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73  zName);.    }els
2c23f 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
2c240 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2c241 20 22 25 73 3a 20 25 73 22 2c 20 7a 4d 73 67 2c   "%s: %s", zMsg,
2c242 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20   zName);.    }. 
2c243 20 20 20 70 50 61 72 73 65 2d 3e 63 68 65 63 6b     pParse->check
2c244 53 63 68 65 6d 61 20 3d 20 31 3b 0a 20 20 7d 0a  Schema = 1;.  }.
2c245 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
2c246 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20  *.** Locate the 
2c247 74 61 62 6c 65 20 69 64 65 6e 74 69 66 69 65 64  table identified
2c248 20 62 79 20 2a 70 2e 0a 2a 2a 0a 2a 2a 20 54 68   by *p..**.** Th
2c249 69 73 20 69 73 20 61 20 77 72 61 70 70 65 72 20  is is a wrapper 
2c24a 61 72 6f 75 6e 64 20 73 71 6c 69 74 65 33 4c 6f  around sqlite3Lo
2c24b 63 61 74 65 54 61 62 6c 65 28 29 2e 20 54 68 65  cateTable(). The
2c24c 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77   difference betw
2c24d 65 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 4c 6f  een.** sqlite3Lo
2c24e 63 61 74 65 54 61 62 6c 65 28 29 20 61 6e 64 20  cateTable() and 
2c24f 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
2c250 20 74 68 61 74 20 74 68 69 73 20 66 75 6e 63 74   that this funct
2c251 69 6f 6e 20 72 65 73 74 72 69 63 74 73 0a 2a 2a  ion restricts.**
2c252 20 74 68 65 20 73 65 61 72 63 68 20 74 6f 20 73   the search to s
2c253 63 68 65 6d 61 20 28 70 2d 3e 70 53 63 68 65 6d  chema (p->pSchem
2c254 61 29 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20  a) if it is not 
2c255 4e 55 4c 4c 2e 20 70 2d 3e 70 53 63 68 65 6d 61  NULL. p->pSchema
2c256 20 6d 61 79 20 62 65 0a 2a 2a 20 6e 6f 6e 2d 4e   may be.** non-N
2c257 55 4c 4c 20 69 66 20 69 74 20 69 73 20 70 61 72  ULL if it is par
2c258 74 20 6f 66 20 61 20 76 69 65 77 20 6f 72 20 74  t of a view or t
2c259 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 64  rigger program d
2c25a 65 66 69 6e 69 74 69 6f 6e 2e 20 53 65 65 0a 2a  efinition. See.*
2c25b 2a 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c  * sqlite3FixSrcL
2c25c 69 73 74 28 29 20 66 6f 72 20 64 65 74 61 69 6c  ist() for detail
2c25d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
2c25e 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69  VATE Table *sqli
2c25f 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 49 74  te3LocateTableIt
2c260 65 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  em(.  Parse *pPa
2c261 72 73 65 2c 20 0a 20 20 69 6e 74 20 69 73 56 69  rse, .  int isVi
2c262 65 77 2c 20 0a 20 20 73 74 72 75 63 74 20 53 72  ew, .  struct Sr
2c263 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 0a 29 7b  cList_item *p.){
2c264 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2c265 44 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  Db;.  assert( p-
2c266 3e 70 53 63 68 65 6d 61 3d 3d 30 20 7c 7c 20 70  >pSchema==0 || p
2c267 2d 3e 7a 44 61 74 61 62 61 73 65 3d 3d 30 20 29  ->zDatabase==0 )
2c268 3b 0a 20 20 69 66 28 20 70 2d 3e 70 53 63 68 65  ;.  if( p->pSche
2c269 6d 61 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 44  ma ){.    int iD
2c26a 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2c26b 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
2c26c 3e 64 62 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29  >db, p->pSchema)
2c26d 3b 0a 20 20 20 20 7a 44 62 20 3d 20 70 50 61 72  ;.    zDb = pPar
2c26e 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d  se->db->aDb[iDb]
2c26f 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b  .zName;.  }else{
2c270 0a 20 20 20 20 7a 44 62 20 3d 20 70 2d 3e 7a 44  .    zDb = p->zD
2c271 61 74 61 62 61 73 65 3b 0a 20 20 7d 0a 20 20 72  atabase;.  }.  r
2c272 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4c 6f 63  eturn sqlite3Loc
2c273 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ateTable(pParse,
2c274 20 69 73 56 69 65 77 2c 20 70 2d 3e 7a 4e 61 6d   isView, p->zNam
2c275 65 2c 20 7a 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  e, zDb);.}../*.*
2c276 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d  * Locate the in-
2c277 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65  memory structure
2c278 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
2c279 0a 2a 2a 20 61 20 70 61 72 74 69 63 75 6c 61 72  .** a particular
2c27a 20 69 6e 64 65 78 20 67 69 76 65 6e 20 74 68 65   index given the
2c27b 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 69 6e   name of that in
2c27c 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e  dex.** and the n
2c27d 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
2c27e 61 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ase that contain
2c27f 73 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20  s the index..** 
2c280 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e  Return NULL if n
2c281 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20  ot found..**.** 
2c282 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20  If zDatabase is 
2c283 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  0, all databases
2c284 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f   are searched fo
2c285 72 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 61  r the.** table a
2c286 6e 64 20 74 68 65 20 66 69 72 73 74 20 6d 61 74  nd the first mat
2c287 63 68 69 6e 67 20 69 6e 64 65 78 20 69 73 20 72  ching index is r
2c288 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68  eturned.  (No ch
2c289 65 63 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 64 75  ecking.** for du
2c28a 70 6c 69 63 61 74 65 20 69 6e 64 65 78 20 6e 61  plicate index na
2c28b 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54  mes is done.)  T
2c28c 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 20  he search order 
2c28d 69 73 0a 2a 2a 20 54 45 4d 50 20 66 69 72 73 74  is.** TEMP first
2c28e 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65  , then MAIN, the
2c28f 6e 20 61 6e 79 20 61 75 78 69 6c 69 61 72 79 20  n any auxiliary 
2c290 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64 0a  databases added.
2c291 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 41 54 54  ** using the ATT
2c292 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a  ACH command..*/.
2c293 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49  SQLITE_PRIVATE I
2c294 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 46 69 6e  ndex *sqlite3Fin
2c295 64 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a  dIndex(sqlite3 *
2c296 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
2c297 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61  zName, const cha
2c298 72 20 2a 7a 44 62 29 7b 0a 20 20 49 6e 64 65 78  r *zDb){.  Index
2c299 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69   *p = 0;.  int i
2c29a 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20  ;.  int nName = 
2c29b 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2c29c 7a 4e 61 6d 65 29 3b 0a 20 20 2f 2a 20 41 6c 6c  zName);.  /* All
2c29d 20 6d 75 74 65 78 65 73 20 61 72 65 20 72 65 71   mutexes are req
2c29e 75 69 72 65 64 20 66 6f 72 20 73 63 68 65 6d 61  uired for schema
2c29f 20 61 63 63 65 73 73 2e 20 20 4d 61 6b 65 20 73   access.  Make s
2c2a0 75 72 65 20 77 65 20 68 6f 6c 64 20 74 68 65 6d  ure we hold them
2c2a1 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a  . */.  assert( z
2c2a2 44 62 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  Db!=0 || sqlite3
2c2a3 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
2c2a4 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 66 6f  exes(db) );.  fo
2c2a5 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b  r(i=OMIT_TEMPDB;
2c2a6 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
2c2a7 7b 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69  {.    int j = (i
2c2a8 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20  <2) ? i^1 : i;  
2c2a9 2f 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62  /* Search TEMP b
2c2aa 65 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20  efore MAIN */.  
2c2ab 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
2c2ac 61 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70  a = db->aDb[j].p
2c2ad 53 63 68 65 6d 61 3b 0a 20 20 20 20 61 73 73 65  Schema;.    asse
2c2ae 72 74 28 20 70 53 63 68 65 6d 61 20 29 3b 0a 20  rt( pSchema );. 
2c2af 20 20 20 69 66 28 20 7a 44 62 20 26 26 20 73 71     if( zDb && sq
2c2b0 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 62  lite3StrICmp(zDb
2c2b1 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61  , db->aDb[j].zNa
2c2b2 6d 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  me) ) continue;.
2c2b3 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
2c2b4 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65  te3SchemaMutexHe
2c2b5 6c 64 28 64 62 2c 20 6a 2c 20 30 29 20 29 3b 0a  ld(db, j, 0) );.
2c2b6 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48      p = sqlite3H
2c2b7 61 73 68 46 69 6e 64 28 26 70 53 63 68 65 6d 61  ashFind(&pSchema
2c2b8 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65  ->idxHash, zName
2c2b9 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66  , nName);.    if
2c2ba 28 20 70 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d  ( p ) break;.  }
2c2bb 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
2c2bc 2f 2a 0a 2a 2a 20 52 65 63 6c 61 69 6d 20 74 68  /*.** Reclaim th
2c2bd 65 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79  e memory used by
2c2be 20 61 6e 20 69 6e 64 65 78 0a 2a 2f 0a 73 74 61   an index.*/.sta
2c2bf 74 69 63 20 76 6f 69 64 20 66 72 65 65 49 6e 64  tic void freeInd
2c2c0 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ex(sqlite3 *db, 
2c2c1 49 6e 64 65 78 20 2a 70 29 7b 0a 23 69 66 6e 64  Index *p){.#ifnd
2c2c2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2c2c3 4e 41 4c 59 5a 45 0a 20 20 73 71 6c 69 74 65 33  NALYZE.  sqlite3
2c2c4 44 65 6c 65 74 65 49 6e 64 65 78 53 61 6d 70 6c  DeleteIndexSampl
2c2c5 65 73 28 64 62 2c 20 70 29 3b 0a 23 65 6e 64 69  es(db, p);.#endi
2c2c6 66 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  f.  sqlite3DbFre
2c2c7 65 28 64 62 2c 20 70 2d 3e 7a 43 6f 6c 41 66 66  e(db, p->zColAff
2c2c8 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
2c2c9 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a  ee(db, p);.}../*
2c2ca 0a 2a 2a 20 46 6f 72 20 74 68 65 20 69 6e 64 65  .** For the inde
2c2cb 78 20 63 61 6c 6c 65 64 20 7a 49 64 78 4e 61 6d  x called zIdxNam
2c2cc 65 20 77 68 69 63 68 20 69 73 20 66 6f 75 6e 64  e which is found
2c2cd 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
2c2ce 20 69 44 62 2c 0a 2a 2a 20 75 6e 6c 69 6b 65 20   iDb,.** unlike 
2c2cf 74 68 61 74 20 69 6e 64 65 78 20 66 72 6f 6d 20  that index from 
2c2d0 69 74 73 20 54 61 62 6c 65 20 74 68 65 6e 20 72  its Table then r
2c2d1 65 6d 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20  emove the index 
2c2d2 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 65  from.** the inde
2c2d3 78 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64  x hash table and
2c2d4 20 66 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79   free all memory
2c2d5 20 73 74 72 75 63 74 75 72 65 73 20 61 73 73 6f   structures asso
2c2d6 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74  ciated.** with t
2c2d7 68 65 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c  he index..*/.SQL
2c2d8 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2c2d9 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e   sqlite3UnlinkAn
2c2da 64 44 65 6c 65 74 65 49 6e 64 65 78 28 73 71 6c  dDeleteIndex(sql
2c2db 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44  ite3 *db, int iD
2c2dc 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
2c2dd 49 64 78 4e 61 6d 65 29 7b 0a 20 20 49 6e 64 65  IdxName){.  Inde
2c2de 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 69 6e 74  x *pIndex;.  int
2c2df 20 6c 65 6e 3b 0a 20 20 48 61 73 68 20 2a 70 48   len;.  Hash *pH
2c2e0 61 73 68 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ash;..  assert( 
2c2e1 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74  sqlite3SchemaMut
2c2e2 65 78 48 65 6c 64 28 64 62 2c 20 69 44 62 2c 20  exHeld(db, iDb, 
2c2e3 30 29 20 29 3b 0a 20 20 70 48 61 73 68 20 3d 20  0) );.  pHash = 
2c2e4 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53  &db->aDb[iDb].pS
2c2e5 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a  chema->idxHash;.
2c2e6 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 53    len = sqlite3S
2c2e7 74 72 6c 65 6e 33 30 28 7a 49 64 78 4e 61 6d 65  trlen30(zIdxName
2c2e8 29 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71  );.  pIndex = sq
2c2e9 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
2c2ea 70 48 61 73 68 2c 20 7a 49 64 78 4e 61 6d 65 2c  pHash, zIdxName,
2c2eb 20 6c 65 6e 2c 20 30 29 3b 0a 20 20 69 66 28 20   len, 0);.  if( 
2c2ec 41 4c 57 41 59 53 28 70 49 6e 64 65 78 29 20 29  ALWAYS(pIndex) )
2c2ed 7b 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65 78  {.    if( pIndex
2c2ee 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78  ->pTable->pIndex
2c2ef 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  ==pIndex ){.    
2c2f0 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65    pIndex->pTable
2c2f1 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e 64 65  ->pIndex = pInde
2c2f2 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65  x->pNext;.    }e
2c2f3 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 65 78  lse{.      Index
2c2f4 20 2a 70 3b 0a 20 20 20 20 20 20 2f 2a 20 4a 75   *p;.      /* Ju
2c2f5 73 74 69 66 69 63 61 74 69 6f 6e 20 6f 66 20 41  stification of A
2c2f6 4c 57 41 59 53 28 29 3b 20 20 54 68 65 20 69 6e  LWAYS();  The in
2c2f7 64 65 78 20 6d 75 73 74 20 62 65 20 6f 6e 20 74  dex must be on t
2c2f8 68 65 20 6c 69 73 74 20 6f 66 0a 20 20 20 20 20  he list of.     
2c2f9 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20 2a 2f 0a   ** indices. */.
2c2fa 20 20 20 20 20 20 70 20 3d 20 70 49 6e 64 65 78        p = pIndex
2c2fb 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78  ->pTable->pIndex
2c2fc 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 41  ;.      while( A
2c2fd 4c 57 41 59 53 28 70 29 20 26 26 20 70 2d 3e 70  LWAYS(p) && p->p
2c2fe 4e 65 78 74 21 3d 70 49 6e 64 65 78 20 29 7b 20  Next!=pIndex ){ 
2c2ff 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20 7d 0a  p = p->pNext; }.
2c300 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53        if( ALWAYS
2c301 28 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 3d 3d  (p && p->pNext==
2c302 70 49 6e 64 65 78 29 20 29 7b 0a 20 20 20 20 20  pIndex) ){.     
2c303 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 49     p->pNext = pI
2c304 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  ndex->pNext;.   
2c305 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66     }.    }.    f
2c306 72 65 65 49 6e 64 65 78 28 64 62 2c 20 70 49 6e  reeIndex(db, pIn
2c307 64 65 78 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e  dex);.  }.  db->
2c308 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
2c309 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d  InternChanges;.}
2c30a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 20 74 68 72  ../*.** Look thr
2c30b 6f 75 67 68 20 74 68 65 20 6c 69 73 74 20 6f 66  ough the list of
2c30c 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 66   open database f
2c30d 69 6c 65 73 20 69 6e 20 64 62 2d 3e 61 44 62 5b  iles in db->aDb[
2c30e 5d 20 61 6e 64 20 69 66 0a 2a 2a 20 61 6e 79 20  ] and if.** any 
2c30f 68 61 76 65 20 62 65 65 6e 20 63 6c 6f 73 65 64  have been closed
2c310 2c 20 72 65 6d 6f 76 65 20 74 68 65 6d 20 66 72  , remove them fr
2c311 6f 6d 20 74 68 65 20 6c 69 73 74 2e 20 20 52 65  om the list.  Re
2c312 61 6c 6c 6f 63 61 74 65 20 74 68 65 0a 2a 2a 20  allocate the.** 
2c313 64 62 2d 3e 61 44 62 5b 5d 20 73 74 72 75 63 74  db->aDb[] struct
2c314 75 72 65 20 74 6f 20 61 20 73 6d 61 6c 6c 65 72  ure to a smaller
2c315 20 73 69 7a 65 2c 20 69 66 20 70 6f 73 73 69 62   size, if possib
2c316 6c 65 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 72 79 20  le..**.** Entry 
2c317 30 20 28 74 68 65 20 22 6d 61 69 6e 22 20 64 61  0 (the "main" da
2c318 74 61 62 61 73 65 29 20 61 6e 64 20 65 6e 74 72  tabase) and entr
2c319 79 20 31 20 28 74 68 65 20 22 74 65 6d 70 22 20  y 1 (the "temp" 
2c31a 64 61 74 61 62 61 73 65 29 0a 2a 2a 20 61 72 65  database).** are
2c31b 20 6e 65 76 65 72 20 63 61 6e 64 69 64 61 74 65   never candidate
2c31c 73 20 66 6f 72 20 62 65 69 6e 67 20 63 6f 6c 6c  s for being coll
2c31d 61 70 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  apsed..*/.SQLITE
2c31e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2c31f 6c 69 74 65 33 43 6f 6c 6c 61 70 73 65 44 61 74  lite3CollapseDat
2c320 61 62 61 73 65 41 72 72 61 79 28 73 71 6c 69 74  abaseArray(sqlit
2c321 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69  e3 *db){.  int i
2c322 2c 20 6a 3b 0a 20 20 66 6f 72 28 69 3d 6a 3d 32  , j;.  for(i=j=2
2c323 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
2c324 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44 62  ){.    struct Db
2c325 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62   *pDb = &db->aDb
2c326 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62  [i];.    if( pDb
2c327 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ->pBt==0 ){.    
2c328 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2c329 64 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 3b  db, pDb->zName);
2c32a 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d  .      pDb->zNam
2c32b 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 6f 6e  e = 0;.      con
2c32c 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20  tinue;.    }.   
2c32d 20 69 66 28 20 6a 3c 69 20 29 7b 0a 20 20 20 20   if( j<i ){.    
2c32e 20 20 64 62 2d 3e 61 44 62 5b 6a 5d 20 3d 20 64    db->aDb[j] = d
2c32f 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 7d  b->aDb[i];.    }
2c330 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20  .    j++;.  }.  
2c331 6d 65 6d 73 65 74 28 26 64 62 2d 3e 61 44 62 5b  memset(&db->aDb[
2c332 6a 5d 2c 20 30 2c 20 28 64 62 2d 3e 6e 44 62 2d  j], 0, (db->nDb-
2c333 6a 29 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44  j)*sizeof(db->aD
2c334 62 5b 6a 5d 29 29 3b 0a 20 20 64 62 2d 3e 6e 44  b[j]));.  db->nD
2c335 62 20 3d 20 6a 3b 0a 20 20 69 66 28 20 64 62 2d  b = j;.  if( db-
2c336 3e 6e 44 62 3c 3d 32 20 26 26 20 64 62 2d 3e 61  >nDb<=2 && db->a
2c337 44 62 21 3d 64 62 2d 3e 61 44 62 53 74 61 74 69  Db!=db->aDbStati
2c338 63 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  c ){.    memcpy(
2c339 64 62 2d 3e 61 44 62 53 74 61 74 69 63 2c 20 64  db->aDbStatic, d
2c33a 62 2d 3e 61 44 62 2c 20 32 2a 73 69 7a 65 6f 66  b->aDb, 2*sizeof
2c33b 28 64 62 2d 3e 61 44 62 5b 30 5d 29 29 3b 0a 20  (db->aDb[0]));. 
2c33c 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2c33d 28 64 62 2c 20 64 62 2d 3e 61 44 62 29 3b 0a 20  (db, db->aDb);. 
2c33e 20 20 20 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d     db->aDb = db-
2c33f 3e 61 44 62 53 74 61 74 69 63 3b 0a 20 20 7d 0a  >aDbStatic;.  }.
2c340 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 74  }../*.** Reset t
2c341 68 65 20 73 63 68 65 6d 61 20 66 6f 72 20 74 68  he schema for th
2c342 65 20 64 61 74 61 62 61 73 65 20 61 74 20 69 6e  e database at in
2c343 64 65 78 20 69 44 62 2e 20 20 41 6c 73 6f 20 72  dex iDb.  Also r
2c344 65 73 65 74 20 74 68 65 0a 2a 2a 20 54 45 4d 50  eset the.** TEMP
2c345 20 73 63 68 65 6d 61 2e 0a 2a 2f 0a 53 51 4c 49   schema..*/.SQLI
2c346 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2c347 73 71 6c 69 74 65 33 52 65 73 65 74 4f 6e 65 53  sqlite3ResetOneS
2c348 63 68 65 6d 61 28 73 71 6c 69 74 65 33 20 2a 64  chema(sqlite3 *d
2c349 62 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 44  b, int iDb){.  D
2c34a 62 20 2a 70 44 62 3b 0a 20 20 61 73 73 65 72 74  b *pDb;.  assert
2c34b 28 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b  ( iDb<db->nDb );
2c34c 0a 0a 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20  ..  /* Case 1:  
2c34d 52 65 73 65 74 20 74 68 65 20 73 69 6e 67 6c 65  Reset the single
2c34e 20 73 63 68 65 6d 61 20 69 64 65 6e 74 69 66 69   schema identifi
2c34f 65 64 20 62 79 20 69 44 62 20 2a 2f 0a 20 20 70  ed by iDb */.  p
2c350 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44  Db = &db->aDb[iD
2c351 62 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  b];.  assert( sq
2c352 6c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78  lite3SchemaMutex
2c353 48 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29  Held(db, iDb, 0)
2c354 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44   );.  assert( pD
2c355 62 2d 3e 70 53 63 68 65 6d 61 21 3d 30 20 29 3b  b->pSchema!=0 );
2c356 0a 20 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61  .  sqlite3Schema
2c357 43 6c 65 61 72 28 70 44 62 2d 3e 70 53 63 68 65  Clear(pDb->pSche
2c358 6d 61 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e  ma);..  /* If an
2c359 79 20 64 61 74 61 62 61 73 65 20 6f 74 68 65 72  y database other
2c35a 20 74 68 61 6e 20 54 45 4d 50 20 69 73 20 72 65   than TEMP is re
2c35b 73 65 74 2c 20 74 68 65 6e 20 61 6c 73 6f 20 72  set, then also r
2c35c 65 73 65 74 20 54 45 4d 50 0a 20 20 2a 2a 20 73  eset TEMP.  ** s
2c35d 69 6e 63 65 20 54 45 4d 50 20 6d 69 67 68 74 20  ince TEMP might 
2c35e 62 65 20 68 6f 6c 64 69 6e 67 20 74 72 69 67 67  be holding trigg
2c35f 65 72 73 20 74 68 61 74 20 72 65 66 65 72 65 6e  ers that referen
2c360 63 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  ce tables in the
2c361 0a 20 20 2a 2a 20 6f 74 68 65 72 20 64 61 74 61  .  ** other data
2c362 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  base..  */.  if(
2c363 20 69 44 62 21 3d 31 20 29 7b 0a 20 20 20 20 70   iDb!=1 ){.    p
2c364 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 31 5d  Db = &db->aDb[1]
2c365 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 44  ;.    assert( pD
2c366 62 2d 3e 70 53 63 68 65 6d 61 21 3d 30 20 29 3b  b->pSchema!=0 );
2c367 0a 20 20 20 20 73 71 6c 69 74 65 33 53 63 68 65  .    sqlite3Sche
2c368 6d 61 43 6c 65 61 72 28 70 44 62 2d 3e 70 53 63  maClear(pDb->pSc
2c369 68 65 6d 61 29 3b 0a 20 20 7d 0a 20 20 72 65 74  hema);.  }.  ret
2c36a 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72  urn;.}../*.** Er
2c36b 61 73 65 20 61 6c 6c 20 73 63 68 65 6d 61 20 69  ase all schema i
2c36c 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
2c36d 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74  all attached dat
2c36e 61 62 61 73 65 73 20 28 69 6e 63 6c 75 64 69 6e  abases (includin
2c36f 67 0a 2a 2a 20 22 6d 61 69 6e 22 20 61 6e 64 20  g.** "main" and 
2c370 22 74 65 6d 70 22 29 20 66 6f 72 20 61 20 73 69  "temp") for a si
2c371 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 63 6f  ngle database co
2c372 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  nnection..*/.SQL
2c373 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2c374 20 73 71 6c 69 74 65 33 52 65 73 65 74 41 6c 6c   sqlite3ResetAll
2c375 53 63 68 65 6d 61 73 4f 66 43 6f 6e 6e 65 63 74  SchemasOfConnect
2c376 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ion(sqlite3 *db)
2c377 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c  {.  int i;.  sql
2c378 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c  ite3BtreeEnterAl
2c379 6c 28 64 62 29 3b 0a 20 20 66 6f 72 28 69 3d 30  l(db);.  for(i=0
2c37a 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
2c37b 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d  ){.    Db *pDb =
2c37c 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20   &db->aDb[i];.  
2c37d 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65    if( pDb->pSche
2c37e 6d 61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ma ){.      sqli
2c37f 74 65 33 53 63 68 65 6d 61 43 6c 65 61 72 28 70  te3SchemaClear(p
2c380 44 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  Db->pSchema);.  
2c381 20 20 7d 0a 20 20 7d 0a 20 20 64 62 2d 3e 66 6c    }.  }.  db->fl
2c382 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 49  ags &= ~SQLITE_I
2c383 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20  nternChanges;.  
2c384 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
2c385 6b 4c 69 73 74 28 64 62 29 3b 0a 20 20 73 71 6c  kList(db);.  sql
2c386 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c  ite3BtreeLeaveAl
2c387 6c 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33  l(db);.  sqlite3
2c388 43 6f 6c 6c 61 70 73 65 44 61 74 61 62 61 73 65  CollapseDatabase
2c389 41 72 72 61 79 28 64 62 29 3b 0a 7d 0a 0a 2f 2a  Array(db);.}../*
2c38a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2c38b 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
2c38c 61 20 63 6f 6d 6d 69 74 20 6f 63 63 75 72 73 2e  a commit occurs.
2c38d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2c38e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
2c38f 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61  ommitInternalCha
2c390 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62  nges(sqlite3 *db
2c391 29 7b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26  ){.  db->flags &
2c392 3d 20 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  = ~SQLITE_Intern
2c393 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a  Changes;.}../*.*
2c394 2a 20 44 65 6c 65 74 65 20 6d 65 6d 6f 72 79 20  * Delete memory 
2c395 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68  allocated for th
2c396 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f  e column names o
2c397 66 20 61 20 74 61 62 6c 65 20 6f 72 20 76 69 65  f a table or vie
2c398 77 20 28 74 68 65 0a 2a 2a 20 54 61 62 6c 65 2e  w (the.** Table.
2c399 61 43 6f 6c 5b 5d 20 61 72 72 61 79 29 2e 0a 2a  aCol[] array)..*
2c39a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
2c39b 6c 69 74 65 44 65 6c 65 74 65 43 6f 6c 75 6d 6e  liteDeleteColumn
2c39c 4e 61 6d 65 73 28 73 71 6c 69 74 65 33 20 2a 64  Names(sqlite3 *d
2c39d 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65  b, Table *pTable
2c39e 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 43 6f  ){.  int i;.  Co
2c39f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 61 73  lumn *pCol;.  as
2c3a0 73 65 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20  sert( pTable!=0 
2c3a1 29 3b 0a 20 20 69 66 28 20 28 70 43 6f 6c 20 3d  );.  if( (pCol =
2c3a2 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 21 3d   pTable->aCol)!=
2c3a3 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  0 ){.    for(i=0
2c3a4 3b 20 69 3c 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c  ; i<pTable->nCol
2c3a5 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a  ; i++, pCol++){.
2c3a6 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2c3a7 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e  ree(db, pCol->zN
2c3a8 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ame);.      sqli
2c3a9 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2c3aa 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a  , pCol->pDflt);.
2c3ab 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2c3ac 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 44  ree(db, pCol->zD
2c3ad 66 6c 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  flt);.      sqli
2c3ae 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
2c3af 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 20 20 20 20  ol->zType);.    
2c3b0 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2c3b1 64 62 2c 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29  db, pCol->zColl)
2c3b2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2c3b3 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54  te3DbFree(db, pT
2c3b4 61 62 6c 65 2d 3e 61 43 6f 6c 29 3b 0a 20 20 7d  able->aCol);.  }
2c3b5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
2c3b6 20 74 68 65 20 6d 65 6d 6f 72 79 20 64 61 74 61   the memory data
2c3b7 20 73 74 72 75 63 74 75 72 65 73 20 61 73 73 6f   structures asso
2c3b8 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
2c3b9 67 69 76 65 6e 0a 2a 2a 20 54 61 62 6c 65 2e 20  given.** Table. 
2c3ba 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20   No changes are 
2c3bb 6d 61 64 65 20 74 6f 20 64 69 73 6b 20 62 79 20  made to disk by 
2c3bc 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a  this routine..**
2c3bd 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2c3be 20 6a 75 73 74 20 64 65 6c 65 74 65 73 20 74 68   just deletes th
2c3bf 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  e data structure
2c3c0 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 75  .  It does not u
2c3c1 6e 6c 69 6e 6b 0a 2a 2a 20 74 68 65 20 74 61 62  nlink.** the tab
2c3c2 6c 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72  le data structur
2c3c3 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20  e from the hash 
2c3c4 74 61 62 6c 65 2e 20 20 42 75 74 20 69 74 20 64  table.  But it d
2c3c5 6f 65 73 20 64 65 73 74 72 6f 79 0a 2a 2a 20 6d  oes destroy.** m
2c3c6 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 73  emory structures
2c3c7 20 6f 66 20 74 68 65 20 69 6e 64 69 63 65 73 20   of the indices 
2c3c8 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 73  and foreign keys
2c3c9 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2c3ca 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 0a   .** the table..
2c3cb 2a 2a 0a 2a 2a 20 54 68 65 20 64 62 20 70 61 72  **.** The db par
2c3cc 61 6d 65 74 65 72 20 69 73 20 6f 70 74 69 6f 6e  ameter is option
2c3cd 61 6c 2e 20 20 49 74 20 69 73 20 6e 65 65 64 65  al.  It is neede
2c3ce 64 20 69 66 20 74 68 65 20 54 61 62 6c 65 20 6f  d if the Table o
2c3cf 62 6a 65 63 74 20 0a 2a 2a 20 63 6f 6e 74 61 69  bject .** contai
2c3d0 6e 73 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d  ns lookaside mem
2c3d1 6f 72 79 2e 20 20 28 54 61 62 6c 65 20 6f 62 6a  ory.  (Table obj
2c3d2 65 63 74 73 20 69 6e 20 74 68 65 20 73 63 68 65  ects in the sche
2c3d3 6d 61 20 64 6f 20 6e 6f 74 20 75 73 65 0a 2a 2a  ma do not use.**
2c3d4 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72   lookaside memor
2c3d5 79 2c 20 62 75 74 20 73 6f 6d 65 20 65 70 68 65  y, but some ephe
2c3d6 6d 65 72 61 6c 20 54 61 62 6c 65 20 6f 62 6a 65  meral Table obje
2c3d7 63 74 73 20 64 6f 2e 29 20 20 4f 72 20 74 68 65  cts do.)  Or the
2c3d8 0a 2a 2a 20 64 62 20 70 61 72 61 6d 65 74 65 72  .** db parameter
2c3d9 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 69 74   can be used wit
2c3da 68 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65  h db->pnBytesFre
2c3db 65 64 20 74 6f 20 6d 65 61 73 75 72 65 20 74 68  ed to measure th
2c3dc 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 75 73 65 64  e memory.** used
2c3dd 20 62 79 20 74 68 65 20 54 61 62 6c 65 20 6f 62   by the Table ob
2c3de 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ject..*/.SQLITE_
2c3df 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2c3e0 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
2c3e1 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62  sqlite3 *db, Tab
2c3e2 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 49  le *pTable){.  I
2c3e3 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 2a 70  ndex *pIndex, *p
2c3e4 4e 65 78 74 3b 0a 20 20 54 45 53 54 4f 4e 4c 59  Next;.  TESTONLY
2c3e5 28 20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65  ( int nLookaside
2c3e6 3b 20 29 20 2f 2a 20 55 73 65 64 20 74 6f 20 76  ; ) /* Used to v
2c3e7 65 72 69 66 79 20 6c 6f 6f 6b 61 73 69 64 65 20  erify lookaside 
2c3e8 6e 6f 74 20 75 73 65 64 20 66 6f 72 20 73 63 68  not used for sch
2c3e9 65 6d 61 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ema */..  assert
2c3ea 28 20 21 70 54 61 62 6c 65 20 7c 7c 20 70 54 61  ( !pTable || pTa
2c3eb 62 6c 65 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 0a  ble->nRef>0 );..
2c3ec 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 65 6c 65    /* Do not dele
2c3ed 74 65 20 74 68 65 20 74 61 62 6c 65 20 75 6e 74  te the table unt
2c3ee 69 6c 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  il the reference
2c3ef 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a   count reaches z
2c3f0 65 72 6f 2e 20 2a 2f 0a 20 20 69 66 28 20 21 70  ero. */.  if( !p
2c3f1 54 61 62 6c 65 20 29 20 72 65 74 75 72 6e 3b 0a  Table ) return;.
2c3f2 20 20 69 66 28 20 28 28 21 64 62 20 7c 7c 20 64    if( ((!db || d
2c3f3 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 65 64 3d  b->pnBytesFreed=
2c3f4 3d 30 29 20 26 26 20 28 2d 2d 70 54 61 62 6c 65  =0) && (--pTable
2c3f5 2d 3e 6e 52 65 66 29 3e 30 29 20 29 20 72 65 74  ->nRef)>0) ) ret
2c3f6 75 72 6e 3b 0a 0a 20 20 2f 2a 20 52 65 63 6f 72  urn;..  /* Recor
2c3f7 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  d the number of 
2c3f8 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 6f 6b  outstanding look
2c3f9 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  aside allocation
2c3fa 73 20 69 6e 20 73 63 68 65 6d 61 20 54 61 62 6c  s in schema Tabl
2c3fb 65 73 0a 20 20 2a 2a 20 70 72 69 6f 72 20 74 6f  es.  ** prior to
2c3fc 20 64 6f 69 6e 67 20 61 6e 79 20 66 72 65 65 28   doing any free(
2c3fd 29 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 53  ) operations.  S
2c3fe 69 6e 63 65 20 73 63 68 65 6d 61 20 54 61 62 6c  ince schema Tabl
2c3ff 65 73 20 64 6f 20 6e 6f 74 20 75 73 65 0a 20 20  es do not use.  
2c400 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 2c 20 74 68  ** lookaside, th
2c401 69 73 20 6e 75 6d 62 65 72 20 73 68 6f 75 6c 64  is number should
2c402 20 6e 6f 74 20 63 68 61 6e 67 65 2e 20 2a 2f 0a   not change. */.
2c403 20 20 54 45 53 54 4f 4e 4c 59 28 20 6e 4c 6f 6f    TESTONLY( nLoo
2c404 6b 61 73 69 64 65 20 3d 20 28 64 62 20 26 26 20  kaside = (db && 
2c405 28 70 54 61 62 6c 65 2d 3e 74 61 62 46 6c 61 67  (pTable->tabFlag
2c406 73 20 26 20 54 46 5f 45 70 68 65 6d 65 72 61 6c  s & TF_Ephemeral
2c407 29 3d 3d 30 29 20 3f 0a 20 20 20 20 20 20 20 20  )==0) ?.        
2c408 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c409 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e   db->lookaside.n
2c40a 4f 75 74 20 3a 20 30 20 29 3b 0a 0a 20 20 2f 2a  Out : 0 );..  /*
2c40b 20 44 65 6c 65 74 65 20 61 6c 6c 20 69 6e 64 69   Delete all indi
2c40c 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ces associated w
2c40d 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 2e 20  ith this table. 
2c40e 2a 2f 0a 20 20 66 6f 72 28 70 49 6e 64 65 78 20  */.  for(pIndex 
2c40f 3d 20 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78  = pTable->pIndex
2c410 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78  ; pIndex; pIndex
2c411 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65  =pNext){.    pNe
2c412 78 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65  xt = pIndex->pNe
2c413 78 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  xt;.    assert( 
2c414 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 3d  pIndex->pSchema=
2c415 3d 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61  =pTable->pSchema
2c416 20 29 3b 0a 20 20 20 20 69 66 28 20 21 64 62 20   );.    if( !db 
2c417 7c 7c 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72  || db->pnBytesFr
2c418 65 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  eed==0 ){.      
2c419 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 49  char *zName = pI
2c41a 6e 64 65 78 2d 3e 7a 4e 61 6d 65 3b 20 0a 20 20  ndex->zName; .  
2c41b 20 20 20 20 54 45 53 54 4f 4e 4c 59 20 28 20 49      TESTONLY ( I
2c41c 6e 64 65 78 20 2a 70 4f 6c 64 20 3d 20 29 20 73  ndex *pOld = ) s
2c41d 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
2c41e 28 0a 20 20 20 20 20 20 20 20 20 26 70 49 6e 64  (.         &pInd
2c41f 65 78 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78  ex->pSchema->idx
2c420 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c  Hash, zName, sql
2c421 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61  ite3Strlen30(zNa
2c422 6d 65 29 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a  me), 0.      );.
2c423 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62        assert( db
2c424 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 53 63  ==0 || sqlite3Sc
2c425 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62  hemaMutexHeld(db
2c426 2c 20 30 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63  , 0, pIndex->pSc
2c427 68 65 6d 61 29 20 29 3b 0a 20 20 20 20 20 20 61  hema) );.      a
2c428 73 73 65 72 74 28 20 70 4f 6c 64 3d 3d 70 49 6e  ssert( pOld==pIn
2c429 64 65 78 20 7c 7c 20 70 4f 6c 64 3d 3d 30 20 29  dex || pOld==0 )
2c42a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 72 65 65  ;.    }.    free
2c42b 49 6e 64 65 78 28 64 62 2c 20 70 49 6e 64 65 78  Index(db, pIndex
2c42c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c  );.  }..  /* Del
2c42d 65 74 65 20 61 6e 79 20 66 6f 72 65 69 67 6e 20  ete any foreign 
2c42e 6b 65 79 73 20 61 74 74 61 63 68 65 64 20 74 6f  keys attached to
2c42f 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a   this table. */.
2c430 20 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 74    sqlite3FkDelet
2c431 65 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 0a  e(db, pTable);..
2c432 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20    /* Delete the 
2c433 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
2c434 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 73  itself..  */.  s
2c435 71 6c 69 74 65 44 65 6c 65 74 65 43 6f 6c 75 6d  qliteDeleteColum
2c436 6e 4e 61 6d 65 73 28 64 62 2c 20 70 54 61 62 6c  nNames(db, pTabl
2c437 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  e);.  sqlite3DbF
2c438 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  ree(db, pTable->
2c439 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  zName);.  sqlite
2c43a 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62  3DbFree(db, pTab
2c43b 6c 65 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20 20  le->zColAff);.  
2c43c 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
2c43d 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  ete(db, pTable->
2c43e 70 53 65 6c 65 63 74 29 3b 0a 23 69 66 6e 64 65  pSelect);.#ifnde
2c43f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48  f SQLITE_OMIT_CH
2c440 45 43 4b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  ECK.  sqlite3Exp
2c441 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
2c442 70 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b 29 3b  pTable->pCheck);
2c443 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
2c444 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2c445 55 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74  UALTABLE.  sqlit
2c446 65 33 56 74 61 62 43 6c 65 61 72 28 64 62 2c 20  e3VtabClear(db, 
2c447 70 54 61 62 6c 65 29 3b 0a 23 65 6e 64 69 66 0a  pTable);.#endif.
2c448 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2c449 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 0a 20 20  db, pTable);..  
2c44a 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 6e  /* Verify that n
2c44b 6f 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f  o lookaside memo
2c44c 72 79 20 77 61 73 20 75 73 65 64 20 62 79 20 73  ry was used by s
2c44d 63 68 65 6d 61 20 74 61 62 6c 65 73 20 2a 2f 0a  chema tables */.
2c44e 20 20 61 73 73 65 72 74 28 20 6e 4c 6f 6f 6b 61    assert( nLooka
2c44f 73 69 64 65 3d 3d 30 20 7c 7c 20 6e 4c 6f 6f 6b  side==0 || nLook
2c450 61 73 69 64 65 3d 3d 64 62 2d 3e 6c 6f 6f 6b 61  aside==db->looka
2c451 73 69 64 65 2e 6e 4f 75 74 20 29 3b 0a 7d 0a 0a  side.nOut );.}..
2c452 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65  /*.** Unlink the
2c453 20 67 69 76 65 6e 20 74 61 62 6c 65 20 66 72 6f   given table fro
2c454 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  m the hash table
2c455 73 20 61 6e 64 20 74 68 65 20 64 65 6c 65 74 65  s and the delete
2c456 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 73 74   the.** table st
2c457 72 75 63 74 75 72 65 20 77 69 74 68 20 61 6c 6c  ructure with all
2c458 20 69 74 73 20 69 6e 64 69 63 65 73 20 61 6e 64   its indices and
2c459 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 2e 0a 2a   foreign keys..*
2c45a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2c45b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c   void sqlite3Unl
2c45c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62 6c  inkAndDeleteTabl
2c45d 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  e(sqlite3 *db, i
2c45e 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68  nt iDb, const ch
2c45f 61 72 20 2a 7a 54 61 62 4e 61 6d 65 29 7b 0a 20  ar *zTabName){. 
2c460 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 44 62 20   Table *p;.  Db 
2c461 2a 70 44 62 3b 0a 0a 20 20 61 73 73 65 72 74 28  *pDb;..  assert(
2c462 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65   db!=0 );.  asse
2c463 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44  rt( iDb>=0 && iD
2c464 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61  b<db->nDb );.  a
2c465 73 73 65 72 74 28 20 7a 54 61 62 4e 61 6d 65 20  ssert( zTabName 
2c466 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
2c467 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48  ite3SchemaMutexH
2c468 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20  eld(db, iDb, 0) 
2c469 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 7a  );.  testcase( z
2c46a 54 61 62 4e 61 6d 65 5b 30 5d 3d 3d 30 20 29 3b  TabName[0]==0 );
2c46b 20 20 2f 2a 20 5a 65 72 6f 2d 6c 65 6e 67 74 68    /* Zero-length
2c46c 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 61 72 65   table names are
2c46d 20 61 6c 6c 6f 77 65 64 20 2a 2f 0a 20 20 70 44   allowed */.  pD
2c46e 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
2c46f 5d 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  ];.  p = sqlite3
2c470 48 61 73 68 49 6e 73 65 72 74 28 26 70 44 62 2d  HashInsert(&pDb-
2c471 3e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  >pSchema->tblHas
2c472 68 2c 20 7a 54 61 62 4e 61 6d 65 2c 0a 20 20 20  h, zTabName,.   
2c473 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c474 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 6c       sqlite3Strl
2c475 65 6e 33 30 28 7a 54 61 62 4e 61 6d 65 29 2c 30  en30(zTabName),0
2c476 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65  );.  sqlite3Dele
2c477 74 65 54 61 62 6c 65 28 64 62 2c 20 70 29 3b 0a  teTable(db, p);.
2c478 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
2c479 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
2c47a 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69  ges;.}../*.** Gi
2c47b 76 65 6e 20 61 20 74 6f 6b 65 6e 2c 20 72 65 74  ven a token, ret
2c47c 75 72 6e 20 61 20 73 74 72 69 6e 67 20 74 68 61  urn a string tha
2c47d 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68  t consists of th
2c47e 65 20 74 65 78 74 20 6f 66 20 74 68 61 74 0a 2a  e text of that.*
2c47f 2a 20 74 6f 6b 65 6e 2e 20 20 53 70 61 63 65 20  * token.  Space 
2c480 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75  to hold the retu
2c481 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 69  rned string.** i
2c482 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
2c483 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61  sqliteMalloc() a
2c484 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64  nd must be freed
2c485 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a   by the calling.
2c486 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  ** function..**.
2c487 2a 2a 20 41 6e 79 20 71 75 6f 74 61 74 69 6f 6e  ** Any quotation
2c488 20 6d 61 72 6b 73 20 28 65 78 3a 20 20 22 6e 61   marks (ex:  "na
2c489 6d 65 22 2c 20 27 6e 61 6d 65 27 2c 20 5b 6e 61  me", 'name', [na
2c48a 6d 65 5d 2c 20 6f 72 20 60 6e 61 6d 65 60 29 20  me], or `name`) 
2c48b 74 68 61 74 0a 2a 2a 20 73 75 72 72 6f 75 6e 64  that.** surround
2c48c 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65   the body of the
2c48d 20 74 6f 6b 65 6e 20 61 72 65 20 72 65 6d 6f 76   token are remov
2c48e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 6f 6b 65 6e 73  ed..**.** Tokens
2c48f 20 61 72 65 20 6f 66 74 65 6e 20 6a 75 73 74 20   are often just 
2c490 70 6f 69 6e 74 65 72 73 20 69 6e 74 6f 20 74 68  pointers into th
2c491 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74  e original SQL t
2c492 65 78 74 20 61 6e 64 20 73 6f 0a 2a 2a 20 61 72  ext and so.** ar
2c493 65 20 6e 6f 74 20 5c 30 30 30 20 74 65 72 6d 69  e not \000 termi
2c494 6e 61 74 65 64 20 61 6e 64 20 61 72 65 20 6e 6f  nated and are no
2c495 74 20 70 65 72 73 69 73 74 65 6e 74 2e 20 20 54  t persistent.  T
2c496 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
2c497 6e 67 0a 2a 2a 20 69 73 20 5c 30 30 30 20 74 65  ng.** is \000 te
2c498 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 69 73 20  rminated and is 
2c499 70 65 72 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 53  persistent..*/.S
2c49a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
2c49b 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46  ar *sqlite3NameF
2c49c 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33  romToken(sqlite3
2c49d 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61   *db, Token *pNa
2c49e 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61  me){.  char *zNa
2c49f 6d 65 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 20  me;.  if( pName 
2c4a0 29 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73  ){.    zName = s
2c4a1 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
2c4a2 64 62 2c 20 28 63 68 61 72 2a 29 70 4e 61 6d 65  db, (char*)pName
2c4a3 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b 0a  ->z, pName->n);.
2c4a4 20 20 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f      sqlite3Dequo
2c4a5 74 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 7d 65 6c  te(zName);.  }el
2c4a6 73 65 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20  se{.    zName = 
2c4a7 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
2c4a8 7a 4e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  zName;.}../*.** 
2c4a9 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 5f  Open the sqlite_
2c4aa 6d 61 73 74 65 72 20 74 61 62 6c 65 20 73 74 6f  master table sto
2c4ab 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 20  red in database 
2c4ac 6e 75 6d 62 65 72 20 69 44 62 20 66 6f 72 0a 2a  number iDb for.*
2c4ad 2a 20 77 72 69 74 69 6e 67 2e 20 54 68 65 20 74  * writing. The t
2c4ae 61 62 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 75  able is opened u
2c4af 73 69 6e 67 20 63 75 72 73 6f 72 20 30 2e 0a 2a  sing cursor 0..*
2c4b0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2c4b1 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65   void sqlite3Ope
2c4b2 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 50 61 72  nMasterTable(Par
2c4b3 73 65 20 2a 70 2c 20 69 6e 74 20 69 44 62 29 7b  se *p, int iDb){
2c4b4 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c  .  Vdbe *v = sql
2c4b5 69 74 65 33 47 65 74 56 64 62 65 28 70 29 3b 0a  ite3GetVdbe(p);.
2c4b6 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f    sqlite3TableLo
2c4b7 63 6b 28 70 2c 20 69 44 62 2c 20 4d 41 53 54 45  ck(p, iDb, MASTE
2c4b8 52 5f 52 4f 4f 54 2c 20 31 2c 20 53 43 48 45 4d  R_ROOT, 1, SCHEM
2c4b9 41 5f 54 41 42 4c 45 28 69 44 62 29 29 3b 0a 20  A_TABLE(iDb));. 
2c4ba 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c4bb 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  p3(v, OP_OpenWri
2c4bc 74 65 2c 20 30 2c 20 4d 41 53 54 45 52 5f 52 4f  te, 0, MASTER_RO
2c4bd 4f 54 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69  OT, iDb);.  sqli
2c4be 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
2c4bf 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 35  v, -1, (char *)5
2c4c0 2c 20 50 34 5f 49 4e 54 33 32 29 3b 20 20 2f 2a  , P4_INT32);  /*
2c4c1 20 35 20 63 6f 6c 75 6d 6e 20 74 61 62 6c 65 20   5 column table 
2c4c2 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 6e 54 61 62  */.  if( p->nTab
2c4c3 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 54  ==0 ){.    p->nT
2c4c4 61 62 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ab = 1;.  }.}../
2c4c5 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a  *.** Parameter z
2c4c6 4e 61 6d 65 20 70 6f 69 6e 74 73 20 74 6f 20 61  Name points to a
2c4c7 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20   nul-terminated 
2c4c8 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e  buffer containin
2c4c9 67 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66  g the name.** of
2c4ca 20 61 20 64 61 74 61 62 61 73 65 20 28 22 6d 61   a database ("ma
2c4cb 69 6e 22 2c 20 22 74 65 6d 70 22 20 6f 72 20 74  in", "temp" or t
2c4cc 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 61 74  he name of an at
2c4cd 74 61 63 68 65 64 20 64 62 29 2e 20 54 68 69 73  tached db). This
2c4ce 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  .** function ret
2c4cf 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f  urns the index o
2c4d0 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74 61  f the named data
2c4d1 62 61 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b  base in db->aDb[
2c4d2 5d 2c 20 6f 72 0a 2a 2a 20 2d 31 20 69 66 20 74  ], or.** -1 if t
2c4d3 68 65 20 6e 61 6d 65 64 20 64 62 20 63 61 6e 6e  he named db cann
2c4d4 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a  ot be found..*/.
2c4d5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2c4d6 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62  nt sqlite3FindDb
2c4d7 4e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a 64 62  Name(sqlite3 *db
2c4d8 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
2c4d9 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20  ame){.  int i = 
2c4da 2d 31 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44  -1;         /* D
2c4db 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a  atabase number *
2c4dc 2f 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b  /.  if( zName ){
2c4dd 0a 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 20  .    Db *pDb;.  
2c4de 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65    int n = sqlite
2c4df 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
2c4e0 3b 0a 20 20 20 20 66 6f 72 28 69 3d 28 64 62 2d  ;.    for(i=(db-
2c4e1 3e 6e 44 62 2d 31 29 2c 20 70 44 62 3d 26 64 62  >nDb-1), pDb=&db
2c4e2 2d 3e 61 44 62 5b 69 5d 3b 20 69 3e 3d 30 3b 20  ->aDb[i]; i>=0; 
2c4e3 69 2d 2d 2c 20 70 44 62 2d 2d 29 7b 0a 20 20 20  i--, pDb--){.   
2c4e4 20 20 20 69 66 28 20 28 21 4f 4d 49 54 5f 54 45     if( (!OMIT_TE
2c4e5 4d 50 44 42 20 7c 7c 20 69 21 3d 31 20 29 20 26  MPDB || i!=1 ) &
2c4e6 26 20 6e 3d 3d 73 71 6c 69 74 65 33 53 74 72 6c  & n==sqlite3Strl
2c4e7 65 6e 33 30 28 70 44 62 2d 3e 7a 4e 61 6d 65 29  en30(pDb->zName)
2c4e8 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 20 30   && .          0
2c4e9 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
2c4ea 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61  (pDb->zName, zNa
2c4eb 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62  me) ){.        b
2c4ec 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2c4ed 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2c4ee 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   i;.}../*.** The
2c4ef 20 74 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 63 6f   token *pName co
2c4f0 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 6d 65 20  ntains the name 
2c4f1 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 28 65  of a database (e
2c4f2 69 74 68 65 72 20 22 6d 61 69 6e 22 20 6f 72 0a  ither "main" or.
2c4f3 2a 2a 20 22 74 65 6d 70 22 20 6f 72 20 74 68 65  ** "temp" or the
2c4f4 20 6e 61 6d 65 20 6f 66 20 61 6e 20 61 74 74 61   name of an atta
2c4f5 63 68 65 64 20 64 62 29 2e 20 54 68 69 73 20 72  ched db). This r
2c4f6 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74  outine returns t
2c4f7 68 65 0a 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74  he.** index of t
2c4f8 68 65 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73  he named databas
2c4f9 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20  e in db->aDb[], 
2c4fa 6f 72 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d  or -1 if the nam
2c4fb 65 64 20 64 62 20 0a 2a 2a 20 64 6f 65 73 20 6e  ed db .** does n
2c4fc 6f 74 20 65 78 69 73 74 2e 0a 2a 2f 0a 53 51 4c  ot exist..*/.SQL
2c4fd 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2c4fe 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71  sqlite3FindDb(sq
2c4ff 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e  lite3 *db, Token
2c500 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20   *pName){.  int 
2c501 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
2c502 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c503 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d   /* Database num
2c504 62 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ber */.  char *z
2c505 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
2c506 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c507 20 4e 61 6d 65 20 77 65 20 61 72 65 20 73 65 61   Name we are sea
2c508 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20  rching for */.  
2c509 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e  zName = sqlite3N
2c50a 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
2c50b 20 70 4e 61 6d 65 29 3b 0a 20 20 69 20 3d 20 73   pName);.  i = s
2c50c 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65  qlite3FindDbName
2c50d 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73  (db, zName);.  s
2c50e 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2c50f 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72   zName);.  retur
2c510 6e 20 69 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 74  n i;.}../* The t
2c511 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6f 72 20  able or view or 
2c512 74 72 69 67 67 65 72 20 6e 61 6d 65 20 69 73 20  trigger name is 
2c513 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 72  passed to this r
2c514 6f 75 74 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e  outine via token
2c515 73 0a 2a 2a 20 70 4e 61 6d 65 31 20 61 6e 64 20  s.** pName1 and 
2c516 70 4e 61 6d 65 32 2e 20 49 66 20 74 68 65 20 74  pName2. If the t
2c517 61 62 6c 65 20 6e 61 6d 65 20 77 61 73 20 66 75  able name was fu
2c518 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 66  lly qualified, f
2c519 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a  or example:.**.*
2c51a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 78  * CREATE TABLE x
2c51b 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 2a 2a  xx.yyy (...);.**
2c51c 20 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31   .** Then pName1
2c51d 20 69 73 20 73 65 74 20 74 6f 20 22 78 78 78 22   is set to "xxx"
2c51e 20 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 79 79   and pName2 "yyy
2c51f 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20  ". On the other 
2c520 68 61 6e 64 20 69 66 0a 2a 2a 20 74 68 65 20 74  hand if.** the t
2c521 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74  able name is not
2c522 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64   fully qualified
2c523 2c 20 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 43 52  , i.e.:.**.** CR
2c524 45 41 54 45 20 54 41 42 4c 45 20 79 79 79 28 2e  EATE TABLE yyy(.
2c525 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20  ..);.**.** Then 
2c526 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f  pName1 is set to
2c527 20 22 79 79 79 22 20 61 6e 64 20 70 4e 61 6d 65   "yyy" and pName
2c528 32 20 69 73 20 22 22 2e 0a 2a 2a 0a 2a 2a 20 54  2 is ""..**.** T
2c529 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
2c52a 20 74 68 65 20 2a 70 70 55 6e 71 75 61 6c 20 70   the *ppUnqual p
2c52b 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 20  ointer to point 
2c52c 61 74 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e  at the token (pN
2c52d 61 6d 65 31 20 6f 72 0a 2a 2a 20 70 4e 61 6d 65  ame1 or.** pName
2c52e 32 29 20 74 68 61 74 20 73 74 6f 72 65 73 20 74  2) that stores t
2c52f 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74  he unqualified t
2c530 61 62 6c 65 20 6e 61 6d 65 2e 20 20 54 68 65 20  able name.  The 
2c531 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20  index of the.** 
2c532 64 61 74 61 62 61 73 65 20 22 78 78 78 22 20 69  database "xxx" i
2c533 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53  s returned..*/.S
2c534 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2c535 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  t sqlite3TwoPart
2c536 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70  Name(.  Parse *p
2c537 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 50  Parse,      /* P
2c538 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20  arsing and code 
2c539 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65  generating conte
2c53a 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  xt */.  Token *p
2c53b 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 54  Name1,      /* T
2c53c 68 65 20 22 78 78 78 22 20 69 6e 20 74 68 65 20  he "xxx" in the 
2c53d 6e 61 6d 65 20 22 78 78 78 2e 79 79 79 22 20 6f  name "xxx.yyy" o
2c53e 72 20 22 78 78 78 22 20 2a 2f 0a 20 20 54 6f 6b  r "xxx" */.  Tok
2c53f 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20  en *pName2,     
2c540 20 2f 2a 20 54 68 65 20 22 79 79 79 22 20 69 6e   /* The "yyy" in
2c541 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78 2e 79   the name "xxx.y
2c542 79 79 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  yy" */.  Token *
2c543 2a 70 55 6e 71 75 61 6c 20 20 20 20 20 2f 2a 20  *pUnqual     /* 
2c544 57 72 69 74 65 20 74 68 65 20 75 6e 71 75 61 6c  Write the unqual
2c545 69 66 69 65 64 20 6f 62 6a 65 63 74 20 6e 61 6d  ified object nam
2c546 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  e here */.){.  i
2c547 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
2c548 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2c549 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74  tabase holding t
2c54a 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73  he object */.  s
2c54b 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2c54c 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20  rse->db;..  if( 
2c54d 41 4c 57 41 59 53 28 70 4e 61 6d 65 32 21 3d 30  ALWAYS(pName2!=0
2c54e 29 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30  ) && pName2->n>0
2c54f 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   ){.    if( db->
2c550 69 6e 69 74 2e 62 75 73 79 20 29 20 7b 0a 20 20  init.busy ) {.  
2c551 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2c552 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 6f 72  Msg(pParse, "cor
2c553 72 75 70 74 20 64 61 74 61 62 61 73 65 22 29 3b  rupt database");
2c554 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
2c555 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74  Err++;.      ret
2c556 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20  urn -1;.    }.  
2c557 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70 4e 61    *pUnqual = pNa
2c558 6d 65 32 3b 0a 20 20 20 20 69 44 62 20 3d 20 73  me2;.    iDb = s
2c559 71 6c 69 74 65 33 46 69 6e 64 44 62 28 64 62 2c  qlite3FindDb(db,
2c55a 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69 66   pName1);.    if
2c55b 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 20  ( iDb<0 ){.     
2c55c 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2c55d 28 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77  (pParse, "unknow
2c55e 6e 20 64 61 74 61 62 61 73 65 20 25 54 22 2c 20  n database %T", 
2c55f 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20 20 70  pName1);.      p
2c560 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  Parse->nErr++;. 
2c561 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a       return -1;.
2c562 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
2c563 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69     assert( db->i
2c564 6e 69 74 2e 69 44 62 3d 3d 30 20 7c 7c 20 64 62  nit.iDb==0 || db
2c565 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b 0a 20  ->init.busy );. 
2c566 20 20 20 69 44 62 20 3d 20 64 62 2d 3e 69 6e 69     iDb = db->ini
2c567 74 2e 69 44 62 3b 0a 20 20 20 20 2a 70 55 6e 71  t.iDb;.    *pUnq
2c568 75 61 6c 20 3d 20 70 4e 61 6d 65 31 3b 0a 20 20  ual = pName1;.  
2c569 7d 0a 20 20 72 65 74 75 72 6e 20 69 44 62 3b 0a  }.  return iDb;.
2c56a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
2c56b 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f  utine is used to
2c56c 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 55 54   check if the UT
2c56d 46 2d 38 20 73 74 72 69 6e 67 20 7a 4e 61 6d 65  F-8 string zName
2c56e 20 69 73 20 61 20 6c 65 67 61 6c 0a 2a 2a 20 75   is a legal.** u
2c56f 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 20  nqualified name 
2c570 66 6f 72 20 61 20 6e 65 77 20 73 63 68 65 6d 61  for a new schema
2c571 20 6f 62 6a 65 63 74 20 28 74 61 62 6c 65 2c 20   object (table, 
2c572 69 6e 64 65 78 2c 20 76 69 65 77 20 6f 72 0a 2a  index, view or.*
2c573 2a 20 74 72 69 67 67 65 72 29 2e 20 41 6c 6c 20  * trigger). All 
2c574 6e 61 6d 65 73 20 61 72 65 20 6c 65 67 61 6c 20  names are legal 
2c575 65 78 63 65 70 74 20 74 68 6f 73 65 20 74 68 61  except those tha
2c576 74 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 65  t begin with the
2c577 20 73 74 72 69 6e 67 0a 2a 2a 20 22 73 71 6c 69   string.** "sqli
2c578 74 65 5f 22 20 28 69 6e 20 75 70 70 65 72 2c 20  te_" (in upper, 
2c579 6c 6f 77 65 72 20 6f 72 20 6d 69 78 65 64 20 63  lower or mixed c
2c57a 61 73 65 29 2e 20 54 68 69 73 20 70 6f 72 74 69  ase). This porti
2c57b 6f 6e 20 6f 66 20 74 68 65 20 6e 61 6d 65 73 70  on of the namesp
2c57c 61 63 65 0a 2a 2a 20 69 73 20 72 65 73 65 72 76  ace.** is reserv
2c57d 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20  ed for internal 
2c57e 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  use..*/.SQLITE_P
2c57f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2c580 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d  e3CheckObjectNam
2c581 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
2c582 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2c583 6d 65 29 7b 0a 20 20 69 66 28 20 21 70 50 61 72  me){.  if( !pPar
2c584 73 65 2d 3e 64 62 2d 3e 69 6e 69 74 2e 62 75 73  se->db->init.bus
2c585 79 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73  y && pParse->nes
2c586 74 65 64 3d 3d 30 20 0a 20 20 20 20 20 20 20 20  ted==0 .        
2c587 20 20 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62    && (pParse->db
2c588 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
2c589 5f 57 72 69 74 65 53 63 68 65 6d 61 29 3d 3d 30  _WriteSchema)==0
2c58a 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 30 3d  .          && 0=
2c58b 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70  =sqlite3StrNICmp
2c58c 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f  (zName, "sqlite_
2c58d 22 2c 20 37 29 20 29 7b 0a 20 20 20 20 73 71 6c  ", 7) ){.    sql
2c58e 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2c58f 72 73 65 2c 20 22 6f 62 6a 65 63 74 20 6e 61 6d  rse, "object nam
2c590 65 20 72 65 73 65 72 76 65 64 20 66 6f 72 20 69  e reserved for i
2c591 6e 74 65 72 6e 61 6c 20 75 73 65 3a 20 25 73 22  nternal use: %s"
2c592 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65  , zName);.    re
2c593 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
2c594 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  R;.  }.  return 
2c595 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
2c596 0a 2a 2a 20 42 65 67 69 6e 20 63 6f 6e 73 74 72  .** Begin constr
2c597 75 63 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62  ucting a new tab
2c598 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  le representatio
2c599 6e 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 68  n in memory.  Th
2c59a 69 73 20 69 73 0a 2a 2a 20 74 68 65 20 66 69 72  is is.** the fir
2c59b 73 74 20 6f 66 20 73 65 76 65 72 61 6c 20 61 63  st of several ac
2c59c 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 74 68  tion routines th
2c59d 61 74 20 67 65 74 20 63 61 6c 6c 65 64 20 69 6e  at get called in
2c59e 20 72 65 73 70 6f 6e 73 65 0a 2a 2a 20 74 6f 20   response.** to 
2c59f 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
2c5a0 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 20 70 61  tatement.  In pa
2c5a1 72 74 69 63 75 6c 61 72 2c 20 74 68 69 73 20 72  rticular, this r
2c5a2 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
2c5a3 0a 2a 2a 20 61 66 74 65 72 20 73 65 65 69 6e 67  .** after seeing
2c5a4 20 74 6f 6b 65 6e 73 20 22 43 52 45 41 54 45 22   tokens "CREATE"
2c5a5 20 61 6e 64 20 22 54 41 42 4c 45 22 20 61 6e 64   and "TABLE" and
2c5a6 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2e   the table name.
2c5a7 20 54 68 65 20 69 73 54 65 6d 70 0a 2a 2a 20 66   The isTemp.** f
2c5a8 6c 61 67 20 69 73 20 74 72 75 65 20 69 66 20 74  lag is true if t
2c5a9 68 65 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20  he table should 
2c5aa 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  be stored in the
2c5ab 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62   auxiliary datab
2c5ac 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 6e 73 74  ase.** file inst
2c5ad 65 61 64 20 6f 66 20 69 6e 20 74 68 65 20 6d 61  ead of in the ma
2c5ae 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
2c5af 2e 20 20 54 68 69 73 20 69 73 20 6e 6f 72 6d 61  .  This is norma
2c5b0 6c 6c 79 20 74 68 65 20 63 61 73 65 0a 2a 2a 20  lly the case.** 
2c5b1 77 68 65 6e 20 74 68 65 20 22 54 45 4d 50 22 20  when the "TEMP" 
2c5b2 6f 72 20 22 54 45 4d 50 4f 52 41 52 59 22 20 6b  or "TEMPORARY" k
2c5b3 65 79 77 6f 72 64 20 6f 63 63 75 72 73 20 69 6e  eyword occurs in
2c5b4 20 62 65 74 77 65 65 6e 0a 2a 2a 20 43 52 45 41   between.** CREA
2c5b5 54 45 20 61 6e 64 20 54 41 42 4c 45 2e 0a 2a 2a  TE and TABLE..**
2c5b6 0a 2a 2a 20 54 68 65 20 6e 65 77 20 74 61 62 6c  .** The new tabl
2c5b7 65 20 72 65 63 6f 72 64 20 69 73 20 69 6e 69 74  e record is init
2c5b8 69 61 6c 69 7a 65 64 20 61 6e 64 20 70 75 74 20  ialized and put 
2c5b9 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  in pParse->pNewT
2c5ba 61 62 6c 65 2e 0a 2a 2a 20 41 73 20 6d 6f 72 65  able..** As more
2c5bb 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54   of the CREATE T
2c5bc 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69  ABLE statement i
2c5bd 73 20 70 61 72 73 65 64 2c 20 61 64 64 69 74 69  s parsed, additi
2c5be 6f 6e 61 6c 20 61 63 74 69 6f 6e 0a 2a 2a 20 72  onal action.** r
2c5bf 6f 75 74 69 6e 65 73 20 77 69 6c 6c 20 62 65 20  outines will be 
2c5c0 63 61 6c 6c 65 64 20 74 6f 20 61 64 64 20 6d 6f  called to add mo
2c5c1 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74  re information t
2c5c2 6f 20 74 68 69 73 20 72 65 63 6f 72 64 2e 0a 2a  o this record..*
2c5c3 2a 20 41 74 20 74 68 65 20 65 6e 64 20 6f 66 20  * At the end of 
2c5c4 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
2c5c5 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 20   statement, the 
2c5c6 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28  sqlite3EndTable(
2c5c7 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20  ) routine.** is 
2c5c8 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65  called to comple
2c5c9 74 65 20 74 68 65 20 63 6f 6e 73 74 72 75 63 74  te the construct
2c5ca 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 20 74  ion of the new t
2c5cb 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a  able record..*/.
2c5cc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2c5cd 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 72 74  oid sqlite3Start
2c5ce 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a  Table(.  Parse *
2c5cf 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72  pParse,   /* Par
2c5d0 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
2c5d1 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20   Token *pName1, 
2c5d2 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20    /* First part 
2c5d3 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  of the name of t
2c5d4 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77  he table or view
2c5d5 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
2c5d6 6d 65 32 2c 20 20 20 2f 2a 20 53 65 63 6f 6e 64  me2,   /* Second
2c5d7 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d   part of the nam
2c5d8 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f  e of the table o
2c5d9 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20  r view */.  int 
2c5da 69 73 54 65 6d 70 2c 20 20 20 20 20 20 2f 2a 20  isTemp,      /* 
2c5db 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20  True if this is 
2c5dc 61 20 54 45 4d 50 20 74 61 62 6c 65 20 2a 2f 0a  a TEMP table */.
2c5dd 20 20 69 6e 74 20 69 73 56 69 65 77 2c 20 20 20    int isView,   
2c5de 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
2c5df 69 73 20 69 73 20 61 20 56 49 45 57 20 2a 2f 0a  is is a VIEW */.
2c5e0 20 20 69 6e 74 20 69 73 56 69 72 74 75 61 6c 2c    int isVirtual,
2c5e1 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
2c5e2 69 73 20 69 73 20 61 20 56 49 52 54 55 41 4c 20  is is a VIRTUAL 
2c5e3 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  table */.  int n
2c5e4 6f 45 72 72 20 20 20 20 20 20 20 20 2f 2a 20 44  oErr        /* D
2c5e5 6f 20 6e 6f 74 68 69 6e 67 20 69 66 20 74 61 62  o nothing if tab
2c5e6 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  le already exist
2c5e7 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20  s */.){.  Table 
2c5e8 2a 70 54 61 62 6c 65 3b 0a 20 20 63 68 61 72 20  *pTable;.  char 
2c5e9 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 2f 2a 20 54  *zName = 0; /* T
2c5ea 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e  he name of the n
2c5eb 65 77 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71  ew table */.  sq
2c5ec 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2c5ed 73 65 2d 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a  se->db;.  Vdbe *
2c5ee 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  v;.  int iDb;   
2c5ef 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2c5f0 65 20 6e 75 6d 62 65 72 20 74 6f 20 63 72 65 61  e number to crea
2c5f1 74 65 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20  te the table in 
2c5f2 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  */.  Token *pNam
2c5f3 65 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61 6c 69  e;    /* Unquali
2c5f4 66 69 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65  fied name of the
2c5f5 20 74 61 62 6c 65 20 74 6f 20 63 72 65 61 74 65   table to create
2c5f6 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 74 61   */..  /* The ta
2c5f7 62 6c 65 20 6f 72 20 76 69 65 77 20 6e 61 6d 65  ble or view name
2c5f8 20 74 6f 20 63 72 65 61 74 65 20 69 73 20 70 61   to create is pa
2c5f9 73 73 65 64 20 74 6f 20 74 68 69 73 20 72 6f 75  ssed to this rou
2c5fa 74 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e 73 0a  tine via tokens.
2c5fb 20 20 2a 2a 20 70 4e 61 6d 65 31 20 61 6e 64 20    ** pName1 and 
2c5fc 70 4e 61 6d 65 32 2e 20 49 66 20 74 68 65 20 74  pName2. If the t
2c5fd 61 62 6c 65 20 6e 61 6d 65 20 77 61 73 20 66 75  able name was fu
2c5fe 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 66  lly qualified, f
2c5ff 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a  or example:.  **
2c600 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42  .  ** CREATE TAB
2c601 4c 45 20 78 78 78 2e 79 79 79 20 28 2e 2e 2e 29  LE xxx.yyy (...)
2c602 3b 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 54 68 65  ;.  ** .  ** The
2c603 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20  n pName1 is set 
2c604 74 6f 20 22 78 78 78 22 20 61 6e 64 20 70 4e 61  to "xxx" and pNa
2c605 6d 65 32 20 22 79 79 79 22 2e 20 4f 6e 20 74 68  me2 "yyy". On th
2c606 65 20 6f 74 68 65 72 20 68 61 6e 64 20 69 66 0a  e other hand if.
2c607 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 6e    ** the table n
2c608 61 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79  ame is not fully
2c609 20 71 75 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e   qualified, i.e.
2c60a 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 52 45 41  :.  **.  ** CREA
2c60b 54 45 20 54 41 42 4c 45 20 79 79 79 28 2e 2e 2e  TE TABLE yyy(...
2c60c 29 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  );.  **.  ** The
2c60d 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20  n pName1 is set 
2c60e 74 6f 20 22 79 79 79 22 20 61 6e 64 20 70 4e 61  to "yyy" and pNa
2c60f 6d 65 32 20 69 73 20 22 22 2e 0a 20 20 2a 2a 0a  me2 is ""..  **.
2c610 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20 62 65    ** The call be
2c611 6c 6f 77 20 73 65 74 73 20 74 68 65 20 70 4e 61  low sets the pNa
2c612 6d 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 6f  me pointer to po
2c613 69 6e 74 20 61 74 20 74 68 65 20 74 6f 6b 65 6e  int at the token
2c614 20 28 70 4e 61 6d 65 31 20 6f 72 0a 20 20 2a 2a   (pName1 or.  **
2c615 20 70 4e 61 6d 65 32 29 20 74 68 61 74 20 73 74   pName2) that st
2c616 6f 72 65 73 20 74 68 65 20 75 6e 71 75 61 6c 69  ores the unquali
2c617 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 2e  fied table name.
2c618 20 54 68 65 20 76 61 72 69 61 62 6c 65 20 69 44   The variable iD
2c619 62 20 69 73 0a 20 20 2a 2a 20 73 65 74 20 74 6f  b is.  ** set to
2c61a 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
2c61b 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20  e database that 
2c61c 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65  the table or vie
2c61d 77 20 69 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20  w is to be.  ** 
2c61e 63 72 65 61 74 65 64 20 69 6e 2e 0a 20 20 2a 2f  created in..  */
2c61f 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
2c620 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72  TwoPartName(pPar
2c621 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d  se, pName1, pNam
2c622 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69  e2, &pName);.  i
2c623 66 28 20 69 44 62 3c 30 20 29 20 72 65 74 75 72  f( iDb<0 ) retur
2c624 6e 3b 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54  n;.  if( !OMIT_T
2c625 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20  EMPDB && isTemp 
2c626 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 26  && pName2->n>0 &
2c627 26 20 69 44 62 21 3d 31 20 29 7b 0a 20 20 20 20  & iDb!=1 ){.    
2c628 2f 2a 20 49 66 20 63 72 65 61 74 69 6e 67 20 61  /* If creating a
2c629 20 74 65 6d 70 20 74 61 62 6c 65 2c 20 74 68 65   temp table, the
2c62a 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 20 62 65   name may not be
2c62b 20 71 75 61 6c 69 66 69 65 64 2e 20 55 6e 6c 65   qualified. Unle
2c62c 73 73 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 64  ss .    ** the d
2c62d 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20  atabase name is 
2c62e 22 74 65 6d 70 22 20 61 6e 79 77 61 79 2e 20 20  "temp" anyway.  
2c62f 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  */.    sqlite3Er
2c630 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2c631 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
2c632 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 75 6e 71  name must be unq
2c633 75 61 6c 69 66 69 65 64 22 29 3b 0a 20 20 20 20  ualified");.    
2c634 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66  return;.  }.  if
2c635 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
2c636 26 20 69 73 54 65 6d 70 20 29 20 69 44 62 20 3d  & isTemp ) iDb =
2c637 20 31 3b 0a 0a 20 20 70 50 61 72 73 65 2d 3e 73   1;..  pParse->s
2c638 4e 61 6d 65 54 6f 6b 65 6e 20 3d 20 2a 70 4e 61  NameToken = *pNa
2c639 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71  me;.  zName = sq
2c63a 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
2c63b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20  en(db, pName);. 
2c63c 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20   if( zName==0 ) 
2c63d 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 53 51  return;.  if( SQ
2c63e 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
2c63f 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28  CheckObjectName(
2c640 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29  pParse, zName) )
2c641 7b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e  {.    goto begin
2c642 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20  _table_error;.  
2c643 7d 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74  }.  if( db->init
2c644 2e 69 44 62 3d 3d 31 20 29 20 69 73 54 65 6d 70  .iDb==1 ) isTemp
2c645 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51   = 1;.#ifndef SQ
2c646 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
2c647 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 65 72 74  IZATION.  assert
2c648 28 20 28 69 73 54 65 6d 70 20 26 20 31 29 3d 3d  ( (isTemp & 1)==
2c649 69 73 54 65 6d 70 20 29 3b 0a 20 20 7b 0a 20 20  isTemp );.  {.  
2c64a 20 20 69 6e 74 20 63 6f 64 65 3b 0a 20 20 20 20    int code;.    
2c64b 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e  char *zDb = db->
2c64c 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
2c64d 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
2c64e 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2c64f 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20   SQLITE_INSERT, 
2c650 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 73 54  SCHEMA_TABLE(isT
2c651 65 6d 70 29 2c 20 30 2c 20 7a 44 62 29 20 29 7b  emp), 0, zDb) ){
2c652 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69  .      goto begi
2c653 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20  n_table_error;. 
2c654 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 56     }.    if( isV
2c655 69 65 77 20 29 7b 0a 20 20 20 20 20 20 69 66 28  iew ){.      if(
2c656 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26   !OMIT_TEMPDB &&
2c657 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20 20   isTemp ){.     
2c658 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45     code = SQLITE
2c659 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45  _CREATE_TEMP_VIE
2c65a 57 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  W;.      }else{.
2c65b 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53          code = S
2c65c 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45  QLITE_CREATE_VIE
2c65d 57 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  W;.      }.    }
2c65e 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
2c65f 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
2c660 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20  isTemp ){.      
2c661 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f    code = SQLITE_
2c662 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c  CREATE_TEMP_TABL
2c663 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  E;.      }else{.
2c664 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53          code = S
2c665 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42  QLITE_CREATE_TAB
2c666 4c 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  LE;.      }.    
2c667 7d 0a 20 20 20 20 69 66 28 20 21 69 73 56 69 72  }.    if( !isVir
2c668 74 75 61 6c 20 26 26 20 73 71 6c 69 74 65 33 41  tual && sqlite3A
2c669 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2c66a 20 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c 20 30 2c   code, zName, 0,
2c66b 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67   zDb) ){.      g
2c66c 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f  oto begin_table_
2c66d 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d  error;.    }.  }
2c66e 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61  .#endif..  /* Ma
2c66f 6b 65 20 73 75 72 65 20 74 68 65 20 6e 65 77 20  ke sure the new 
2c670 74 61 62 6c 65 20 6e 61 6d 65 20 64 6f 65 73 20  table name does 
2c671 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69 74 68  not collide with
2c672 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 20 20 2a   an existing.  *
2c673 2a 20 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65  * index or table
2c674 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 73 61 6d   name in the sam
2c675 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 73 73  e database.  Iss
2c676 75 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ue an error mess
2c677 61 67 65 20 69 66 0a 20 20 2a 2a 20 69 74 20 64  age if.  ** it d
2c678 6f 65 73 2e 20 54 68 65 20 65 78 63 65 70 74 69  oes. The excepti
2c679 6f 6e 20 69 73 20 69 66 20 74 68 65 20 73 74 61  on is if the sta
2c67a 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 70 61 72  tement being par
2c67b 73 65 64 20 77 61 73 20 70 61 73 73 65 64 0a 20  sed was passed. 
2c67c 20 2a 2a 20 74 6f 20 61 6e 20 73 71 6c 69 74 65   ** to an sqlite
2c67d 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29  3_declare_vtab()
2c67e 20 63 61 6c 6c 2e 20 49 6e 20 74 68 61 74 20 63   call. In that c
2c67f 61 73 65 20 6f 6e 6c 79 20 74 68 65 20 63 6f 6c  ase only the col
2c680 75 6d 6e 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 61  umn names.  ** a
2c681 6e 64 20 74 79 70 65 73 20 77 69 6c 6c 20 62 65  nd types will be
2c682 20 75 73 65 64 2c 20 73 6f 20 74 68 65 72 65 20   used, so there 
2c683 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65  is no need to te
2c684 73 74 20 66 6f 72 20 6e 61 6d 65 73 70 61 63 65  st for namespace
2c685 0a 20 20 2a 2a 20 63 6f 6c 6c 69 73 69 6f 6e 73  .  ** collisions
2c686 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 49 4e  ..  */.  if( !IN
2c687 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b  _DECLARE_VTAB ){
2c688 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 62 20 3d  .    char *zDb =
2c689 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
2c68a 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 53 51 4c  ame;.    if( SQL
2c68b 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52  ITE_OK!=sqlite3R
2c68c 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
2c68d 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
2c68e 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f  begin_table_erro
2c68f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61  r;.    }.    pTa
2c690 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  ble = sqlite3Fin
2c691 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65  dTable(db, zName
2c692 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20  , zDb);.    if( 
2c693 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  pTable ){.      
2c694 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20  if( !noErr ){.  
2c695 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2c696 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
2c697 61 62 6c 65 20 25 54 20 61 6c 72 65 61 64 79 20  able %T already 
2c698 65 78 69 73 74 73 22 2c 20 70 4e 61 6d 65 29 3b  exists", pName);
2c699 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2c69a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 64        assert( !d
2c69b 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b 0a  b->init.busy );.
2c69c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
2c69d 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28  odeVerifySchema(
2c69e 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20  pParse, iDb);.  
2c69f 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f      }.      goto
2c6a0 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72   begin_table_err
2c6a1 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  or;.    }.    if
2c6a2 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64  ( sqlite3FindInd
2c6a3 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44  ex(db, zName, zD
2c6a4 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  b)!=0 ){.      s
2c6a5 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2c6a6 50 61 72 73 65 2c 20 22 74 68 65 72 65 20 69 73  Parse, "there is
2c6a7 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 64 65   already an inde
2c6a8 78 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e 61  x named %s", zNa
2c6a9 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  me);.      goto 
2c6aa 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f  begin_table_erro
2c6ab 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  r;.    }.  }..  
2c6ac 70 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33  pTable = sqlite3
2c6ad 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
2c6ae 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b   sizeof(Table));
2c6af 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30  .  if( pTable==0
2c6b0 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c   ){.    db->mall
2c6b1 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
2c6b2 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
2c6b3 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
2c6b4 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b   pParse->nErr++;
2c6b5 0a 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f  .    goto begin_
2c6b6 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 7d  table_error;.  }
2c6b7 0a 20 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65  .  pTable->zName
2c6b8 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62   = zName;.  pTab
2c6b9 6c 65 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a  le->iPKey = -1;.
2c6ba 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d    pTable->pSchem
2c6bb 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  a = db->aDb[iDb]
2c6bc 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 61 62  .pSchema;.  pTab
2c6bd 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20  le->nRef = 1;.  
2c6be 70 54 61 62 6c 65 2d 3e 6e 52 6f 77 45 73 74 20  pTable->nRowEst 
2c6bf 3d 20 31 30 30 30 30 30 30 3b 0a 20 20 61 73 73  = 1000000;.  ass
2c6c0 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65  ert( pParse->pNe
2c6c1 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 70  wTable==0 );.  p
2c6c2 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2c6c3 20 3d 20 70 54 61 62 6c 65 3b 0a 0a 20 20 2f 2a   = pTable;..  /*
2c6c4 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
2c6c5 6d 61 67 69 63 20 73 71 6c 69 74 65 5f 73 65 71  magic sqlite_seq
2c6c6 75 65 6e 63 65 20 74 61 62 6c 65 20 75 73 65 64  uence table used
2c6c7 20 62 79 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e   by autoincremen
2c6c8 74 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65 63  t,.  ** then rec
2c6c9 6f 72 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ord a pointer to
2c6ca 20 74 68 69 73 20 74 61 62 6c 65 20 69 6e 20 74   this table in t
2c6cb 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
2c6cc 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a 20   structure.  ** 
2c6cd 73 6f 20 74 68 61 74 20 49 4e 53 45 52 54 20 63  so that INSERT c
2c6ce 61 6e 20 66 69 6e 64 20 74 68 65 20 74 61 62 6c  an find the tabl
2c6cf 65 20 65 61 73 69 6c 79 2e 0a 20 20 2a 2f 0a 23  e easily..  */.#
2c6d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2c6d1 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  IT_AUTOINCREMENT
2c6d2 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e  .  if( !pParse->
2c6d3 6e 65 73 74 65 64 20 26 26 20 73 74 72 63 6d 70  nested && strcmp
2c6d4 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f  (zName, "sqlite_
2c6d5 73 65 71 75 65 6e 63 65 22 29 3d 3d 30 20 29 7b  sequence")==0 ){
2c6d6 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
2c6d7 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48  ite3SchemaMutexH
2c6d8 65 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20  eld(db, iDb, 0) 
2c6d9 29 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e 70  );.    pTable->p
2c6da 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 20  Schema->pSeqTab 
2c6db 3d 20 70 54 61 62 6c 65 3b 0a 20 20 7d 0a 23 65  = pTable;.  }.#e
2c6dc 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e  ndif..  /* Begin
2c6dd 20 67 65 6e 65 72 61 74 69 6e 67 20 74 68 65 20   generating the 
2c6de 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 69  code that will i
2c6df 6e 73 65 72 74 20 74 68 65 20 74 61 62 6c 65 20  nsert the table 
2c6e0 72 65 63 6f 72 64 20 69 6e 74 6f 0a 20 20 2a 2a  record into.  **
2c6e1 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54   the SQLITE_MAST
2c6e2 45 52 20 74 61 62 6c 65 2e 20 20 4e 6f 74 65 20  ER table.  Note 
2c6e3 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20 74 68  in particular th
2c6e4 61 74 20 77 65 20 6d 75 73 74 20 67 6f 20 61 68  at we must go ah
2c6e5 65 61 64 0a 20 20 2a 2a 20 61 6e 64 20 61 6c 6c  ead.  ** and all
2c6e6 6f 63 61 74 65 20 74 68 65 20 72 65 63 6f 72 64  ocate the record
2c6e7 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
2c6e8 74 61 62 6c 65 20 65 6e 74 72 79 20 6e 6f 77 2e  table entry now.
2c6e9 20 20 42 65 66 6f 72 65 20 61 6e 79 0a 20 20 2a    Before any.  *
2c6ea 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72  * PRIMARY KEY or
2c6eb 20 55 4e 49 51 55 45 20 6b 65 79 77 6f 72 64 73   UNIQUE keywords
2c6ec 20 61 72 65 20 70 61 72 73 65 64 2e 20 20 54 68   are parsed.  Th
2c6ed 6f 73 65 20 6b 65 79 77 6f 72 64 73 20 77 69 6c  ose keywords wil
2c6ee 6c 20 63 61 75 73 65 0a 20 20 2a 2a 20 69 6e 64  l cause.  ** ind
2c6ef 69 63 65 73 20 74 6f 20 62 65 20 63 72 65 61 74  ices to be creat
2c6f0 65 64 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65  ed and the table
2c6f1 20 72 65 63 6f 72 64 20 6d 75 73 74 20 63 6f 6d   record must com
2c6f2 65 20 62 65 66 6f 72 65 20 74 68 65 20 0a 20 20  e before the .  
2c6f3 2a 2a 20 69 6e 64 69 63 65 73 2e 20 20 48 65 6e  ** indices.  Hen
2c6f4 63 65 2c 20 74 68 65 20 72 65 63 6f 72 64 20 6e  ce, the record n
2c6f5 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61  umber for the ta
2c6f6 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6c 6c 6f  ble must be allo
2c6f7 63 61 74 65 64 0a 20 20 2a 2a 20 6e 6f 77 2e 0a  cated.  ** now..
2c6f8 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e    */.  if( !db->
2c6f9 69 6e 69 74 2e 62 75 73 79 20 26 26 20 28 76 20  init.busy && (v 
2c6fa 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2c6fb 28 70 50 61 72 73 65 29 29 21 3d 30 20 29 7b 0a  (pParse))!=0 ){.
2c6fc 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20      int j1;.    
2c6fd 69 6e 74 20 66 69 6c 65 46 6f 72 6d 61 74 3b 0a  int fileFormat;.
2c6fe 20 20 20 20 69 6e 74 20 72 65 67 31 2c 20 72 65      int reg1, re
2c6ff 67 32 2c 20 72 65 67 33 3b 0a 20 20 20 20 73 71  g2, reg3;.    sq
2c700 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
2c701 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
2c702 20 30 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64   0, iDb);..#ifnd
2c703 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2c704 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
2c705 69 66 28 20 69 73 56 69 72 74 75 61 6c 20 29 7b  if( isVirtual ){
2c706 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2c707 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56  beAddOp0(v, OP_V
2c708 42 65 67 69 6e 29 3b 0a 20 20 20 20 7d 0a 23 65  Begin);.    }.#e
2c709 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49 66 20  ndif..    /* If 
2c70a 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  the file format 
2c70b 61 6e 64 20 65 6e 63 6f 64 69 6e 67 20 69 6e 20  and encoding in 
2c70c 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 76  the database hav
2c70d 65 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 2c 20  e not been set, 
2c70e 0a 20 20 20 20 2a 2a 20 73 65 74 20 74 68 65 6d  .    ** set them
2c70f 20 6e 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20   now..    */.   
2c710 20 72 65 67 31 20 3d 20 70 50 61 72 73 65 2d 3e   reg1 = pParse->
2c711 72 65 67 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61  regRowid = ++pPa
2c712 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72  rse->nMem;.    r
2c713 65 67 32 20 3d 20 70 50 61 72 73 65 2d 3e 72 65  eg2 = pParse->re
2c714 67 52 6f 6f 74 20 3d 20 2b 2b 70 50 61 72 73 65  gRoot = ++pParse
2c715 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72 65 67 33  ->nMem;.    reg3
2c716 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2c717 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  m;.    sqlite3Vd
2c718 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52  beAddOp3(v, OP_R
2c719 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  eadCookie, iDb, 
2c71a 72 65 67 33 2c 20 42 54 52 45 45 5f 46 49 4c 45  reg3, BTREE_FILE
2c71b 5f 46 4f 52 4d 41 54 29 3b 0a 20 20 20 20 73 71  _FORMAT);.    sq
2c71c 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72  lite3VdbeUsesBtr
2c71d 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20  ee(v, iDb);.    
2c71e 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
2c71f 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c  AddOp1(v, OP_If,
2c720 20 72 65 67 33 29 3b 0a 20 20 20 20 66 69 6c 65   reg3);.    file
2c721 46 6f 72 6d 61 74 20 3d 20 28 64 62 2d 3e 66 6c  Format = (db->fl
2c722 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4c 65 67  ags & SQLITE_Leg
2c723 61 63 79 46 69 6c 65 46 6d 74 29 21 3d 30 20 3f  acyFileFmt)!=0 ?
2c724 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c725 20 20 20 31 20 3a 20 53 51 4c 49 54 45 5f 4d 41     1 : SQLITE_MA
2c726 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 3b 0a 20  X_FILE_FORMAT;. 
2c727 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c728 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2c729 65 72 2c 20 66 69 6c 65 46 6f 72 6d 61 74 2c 20  er, fileFormat, 
2c72a 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74  reg3);.    sqlit
2c72b 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2c72c 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44  OP_SetCookie, iD
2c72d 62 2c 20 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f  b, BTREE_FILE_FO
2c72e 52 4d 41 54 2c 20 72 65 67 33 29 3b 0a 20 20 20  RMAT, reg3);.   
2c72f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c730 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2c731 2c 20 45 4e 43 28 64 62 29 2c 20 72 65 67 33 29  , ENC(db), reg3)
2c732 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2c733 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65  eAddOp3(v, OP_Se
2c734 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 42 54  tCookie, iDb, BT
2c735 52 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e  REE_TEXT_ENCODIN
2c736 47 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71  G, reg3);.    sq
2c737 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2c738 65 28 76 2c 20 6a 31 29 3b 0a 0a 20 20 20 20 2f  e(v, j1);..    /
2c739 2a 20 54 68 69 73 20 6a 75 73 74 20 63 72 65 61  * This just crea
2c73a 74 65 73 20 61 20 70 6c 61 63 65 2d 68 6f 6c 64  tes a place-hold
2c73b 65 72 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65  er record in the
2c73c 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
2c73d 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 20 54 68 65  able..    ** The
2c73e 20 72 65 63 6f 72 64 20 63 72 65 61 74 65 64 20   record created 
2c73f 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e  does not contain
2c740 20 61 6e 79 74 68 69 6e 67 20 79 65 74 2e 20 20   anything yet.  
2c741 49 74 20 77 69 6c 6c 20 62 65 20 72 65 70 6c 61  It will be repla
2c742 63 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 68  ced.    ** by th
2c743 65 20 72 65 61 6c 20 65 6e 74 72 79 20 69 6e 20  e real entry in 
2c744 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 61  code generated a
2c745 74 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c  t sqlite3EndTabl
2c746 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  e()..    **.    
2c747 2a 2a 20 54 68 65 20 72 6f 77 69 64 20 66 6f 72  ** The rowid for
2c748 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69   the new entry i
2c749 73 20 6c 65 66 74 20 69 6e 20 72 65 67 69 73 74  s left in regist
2c74a 65 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f  er pParse->regRo
2c74b 77 69 64 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20  wid..    ** The 
2c74c 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
2c74d 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c   of the new tabl
2c74e 65 20 69 73 20 6c 65 66 74 20 69 6e 20 72 65 67  e is left in reg
2c74f 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74   pParse->regRoot
2c750 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 77  ..    ** The row
2c751 69 64 20 61 6e 64 20 72 6f 6f 74 20 70 61 67 65  id and root page
2c752 20 6e 75 6d 62 65 72 20 76 61 6c 75 65 73 20 61   number values a
2c753 72 65 20 6e 65 65 64 65 64 20 62 79 20 74 68 65  re needed by the
2c754 20 63 6f 64 65 20 74 68 61 74 0a 20 20 20 20 2a   code that.    *
2c755 2a 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c  * sqlite3EndTabl
2c756 65 20 77 69 6c 6c 20 67 65 6e 65 72 61 74 65 2e  e will generate.
2c757 0a 20 20 20 20 2a 2f 0a 23 69 66 20 21 64 65 66  .    */.#if !def
2c758 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2c759 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e  _VIEW) || !defin
2c75a 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
2c75b 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 20 20 20  IRTUALTABLE).   
2c75c 20 69 66 28 20 69 73 56 69 65 77 20 7c 7c 20 69   if( isView || i
2c75d 73 56 69 72 74 75 61 6c 20 29 7b 0a 20 20 20 20  sVirtual ){.    
2c75e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c75f 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2c760 72 2c 20 30 2c 20 72 65 67 32 29 3b 0a 20 20 20  r, 0, reg2);.   
2c761 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
2c762 20 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65    {.      sqlite
2c763 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c764 50 5f 43 72 65 61 74 65 54 61 62 6c 65 2c 20 69  P_CreateTable, i
2c765 44 62 2c 20 72 65 67 32 29 3b 0a 20 20 20 20 7d  Db, reg2);.    }
2c766 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e  .    sqlite3Open
2c767 4d 61 73 74 65 72 54 61 62 6c 65 28 70 50 61 72  MasterTable(pPar
2c768 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71  se, iDb);.    sq
2c769 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2c76a 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
2c76b 30 2c 20 72 65 67 31 29 3b 0a 20 20 20 20 73 71  0, reg1);.    sq
2c76c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2c76d 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72  v, OP_Null, 0, r
2c76e 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  eg3);.    sqlite
2c76f 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2c770 50 5f 49 6e 73 65 72 74 2c 20 30 2c 20 72 65 67  P_Insert, 0, reg
2c771 33 2c 20 72 65 67 31 29 3b 0a 20 20 20 20 73 71  3, reg1);.    sq
2c772 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2c773 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45  5(v, OPFLAG_APPE
2c774 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ND);.    sqlite3
2c775 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50  VdbeAddOp0(v, OP
2c776 5f 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 0a 20 20  _Close);.  }..  
2c777 2f 2a 20 4e 6f 72 6d 61 6c 20 28 6e 6f 6e 2d 65  /* Normal (non-e
2c778 72 72 6f 72 29 20 72 65 74 75 72 6e 2e 20 2a 2f  rror) return. */
2c779 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a  .  return;..  /*
2c77a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
2c77b 75 72 73 2c 20 77 65 20 6a 75 6d 70 20 68 65 72  urs, we jump her
2c77c 65 20 2a 2f 0a 62 65 67 69 6e 5f 74 61 62 6c 65  e */.begin_table
2c77d 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74 65  _error:.  sqlite
2c77e 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d  3DbFree(db, zNam
2c77f 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a  e);.  return;.}.
2c780 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72  ./*.** This macr
2c781 6f 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d  o is used to com
2c782 70 61 72 65 20 74 77 6f 20 73 74 72 69 6e 67 73  pare two strings
2c783 20 69 6e 20 61 20 63 61 73 65 2d 69 6e 73 65 6e   in a case-insen
2c784 73 69 74 69 76 65 20 6d 61 6e 6e 65 72 2e 0a 2a  sitive manner..*
2c785 2a 20 49 74 20 69 73 20 73 6c 69 67 68 74 6c 79  * It is slightly
2c786 20 66 61 73 74 65 72 20 74 68 61 6e 20 63 61 6c   faster than cal
2c787 6c 69 6e 67 20 73 71 6c 69 74 65 33 53 74 72 49  ling sqlite3StrI
2c788 43 6d 70 28 29 20 64 69 72 65 63 74 6c 79 2c 20  Cmp() directly, 
2c789 62 75 74 0a 2a 2a 20 70 72 6f 64 75 63 65 73 20  but.** produces 
2c78a 6c 61 72 67 65 72 20 63 6f 64 65 2e 0a 2a 2a 0a  larger code..**.
2c78b 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 54 68 69 73  ** WARNING: This
2c78c 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74 20 63 6f   macro is not co
2c78d 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 74 68  mpatible with th
2c78e 65 20 73 74 72 63 6d 70 28 29 20 66 61 6d 69 6c  e strcmp() famil
2c78f 79 2e 20 49 74 0a 2a 2a 20 72 65 74 75 72 6e 73  y. It.** returns
2c790 20 74 72 75 65 20 69 66 20 74 68 65 20 74 77 6f   true if the two
2c791 20 73 74 72 69 6e 67 73 20 61 72 65 20 65 71 75   strings are equ
2c792 61 6c 2c 20 6f 74 68 65 72 77 69 73 65 20 66 61  al, otherwise fa
2c793 6c 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  lse..*/.#define 
2c794 53 54 52 49 43 4d 50 28 78 2c 20 79 29 20 28 5c  STRICMP(x, y) (\
2c795 0a 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c  .sqlite3UpperToL
2c796 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e 65 64 20  ower[*(unsigned 
2c797 63 68 61 72 20 2a 29 28 78 29 5d 3d 3d 20 20 20  char *)(x)]==   
2c798 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  \.sqlite3UpperTo
2c799 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e 65 64  Lower[*(unsigned
2c79a 20 63 68 61 72 20 2a 29 28 79 29 5d 20 20 20 20   char *)(y)]    
2c79b 20 5c 0a 26 26 20 73 71 6c 69 74 65 33 53 74 72   \.&& sqlite3Str
2c79c 49 43 6d 70 28 28 78 29 2b 31 2c 28 79 29 2b 31  ICmp((x)+1,(y)+1
2c79d 29 3d 3d 30 20 29 0a 0a 2f 2a 0a 2a 2a 20 41 64  )==0 )../*.** Ad
2c79e 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 74  d a new column t
2c79f 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72  o the table curr
2c7a0 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73  ently being cons
2c7a1 74 72 75 63 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  tructed..**.** T
2c7a2 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20  he parser calls 
2c7a3 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 63  this routine onc
2c7a4 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  e for each colum
2c7a5 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a  n declaration.**
2c7a6 20 69 6e 20 61 20 43 52 45 41 54 45 20 54 41 42   in a CREATE TAB
2c7a7 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 73  LE statement.  s
2c7a8 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65  qlite3StartTable
2c7a9 28 29 20 67 65 74 73 20 63 61 6c 6c 65 64 0a 2a  () gets called.*
2c7aa 2a 20 66 69 72 73 74 20 74 6f 20 67 65 74 20 74  * first to get t
2c7ab 68 69 6e 67 73 20 67 6f 69 6e 67 2e 20 20 54 68  hings going.  Th
2c7ac 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
2c7ad 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 65 61  is called for ea
2c7ae 63 68 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f  ch.** column..*/
2c7af 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2c7b0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43  void sqlite3AddC
2c7b1 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 61  olumn(Parse *pPa
2c7b2 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  rse, Token *pNam
2c7b3 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a  e){.  Table *p;.
2c7b4 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 20    int i;.  char 
2c7b5 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43  *z;.  Column *pC
2c7b6 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ol;.  sqlite3 *d
2c7b7 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2c7b8 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72 73    if( (p = pPars
2c7b9 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30  e->pNewTable)==0
2c7ba 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 20 53   ) return;.#if S
2c7bb 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e  QLITE_MAX_COLUMN
2c7bc 0a 20 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 2b 31  .  if( p->nCol+1
2c7bd 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
2c7be 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d  TE_LIMIT_COLUMN]
2c7bf 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2c7c0 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2c7c1 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e  "too many column
2c7c2 73 20 6f 6e 20 25 73 22 2c 20 70 2d 3e 7a 4e 61  s on %s", p->zNa
2c7c3 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  me);.    return;
2c7c4 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7a 20  .  }.#endif.  z 
2c7c5 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
2c7c6 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65  mToken(db, pName
2c7c7 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20  );.  if( z==0 ) 
2c7c8 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d  return;.  for(i=
2c7c9 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b  0; i<p->nCol; i+
2c7ca 2b 29 7b 0a 20 20 20 20 69 66 28 20 53 54 52 49  +){.    if( STRI
2c7cb 43 4d 50 28 7a 2c 20 70 2d 3e 61 43 6f 6c 5b 69  CMP(z, p->aCol[i
2c7cc 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  ].zName) ){.    
2c7cd 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2c7ce 67 28 70 50 61 72 73 65 2c 20 22 64 75 70 6c 69  g(pParse, "dupli
2c7cf 63 61 74 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  cate column name
2c7d0 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20 20  : %s", z);.     
2c7d1 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2c7d2 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 72 65 74  b, z);.      ret
2c7d3 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  urn;.    }.  }. 
2c7d4 20 69 66 28 20 28 70 2d 3e 6e 43 6f 6c 20 26 20   if( (p->nCol & 
2c7d5 30 78 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 43  0x7)==0 ){.    C
2c7d6 6f 6c 75 6d 6e 20 2a 61 4e 65 77 3b 0a 20 20 20  olumn *aNew;.   
2c7d7 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   aNew = sqlite3D
2c7d8 62 52 65 61 6c 6c 6f 63 28 64 62 2c 70 2d 3e 61  bRealloc(db,p->a
2c7d9 43 6f 6c 2c 28 70 2d 3e 6e 43 6f 6c 2b 38 29 2a  Col,(p->nCol+8)*
2c7da 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30  sizeof(p->aCol[0
2c7db 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 61 4e 65  ]));.    if( aNe
2c7dc 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  w==0 ){.      sq
2c7dd 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2c7de 7a 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  z);.      return
2c7df 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61  ;.    }.    p->a
2c7e0 43 6f 6c 20 3d 20 61 4e 65 77 3b 0a 20 20 7d 0a  Col = aNew;.  }.
2c7e1 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f    pCol = &p->aCo
2c7e2 6c 5b 70 2d 3e 6e 43 6f 6c 5d 3b 0a 20 20 6d 65  l[p->nCol];.  me
2c7e3 6d 73 65 74 28 70 43 6f 6c 2c 20 30 2c 20 73 69  mset(pCol, 0, si
2c7e4 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29  zeof(p->aCol[0])
2c7e5 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65  );.  pCol->zName
2c7e6 20 3d 20 7a 3b 0a 20 0a 20 20 2f 2a 20 49 66 20   = z;. .  /* If 
2c7e7 74 68 65 72 65 20 69 73 20 6e 6f 20 74 79 70 65  there is no type
2c7e8 20 73 70 65 63 69 66 69 65 64 2c 20 63 6f 6c 75   specified, colu
2c7e9 6d 6e 73 20 68 61 76 65 20 74 68 65 20 64 65 66  mns have the def
2c7ea 61 75 6c 74 20 61 66 66 69 6e 69 74 79 0a 20 20  ault affinity.  
2c7eb 2a 2a 20 27 4e 4f 4e 45 27 2e 20 49 66 20 74 68  ** 'NONE'. If th
2c7ec 65 72 65 20 69 73 20 61 20 74 79 70 65 20 73 70  ere is a type sp
2c7ed 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 73 71  ecified, then sq
2c7ee 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79  lite3AddColumnTy
2c7ef 70 65 28 29 20 77 69 6c 6c 0a 20 20 2a 2a 20 62  pe() will.  ** b
2c7f0 65 20 63 61 6c 6c 65 64 20 6e 65 78 74 20 74 6f  e called next to
2c7f1 20 73 65 74 20 70 43 6f 6c 2d 3e 61 66 66 69 6e   set pCol->affin
2c7f2 69 74 79 20 63 6f 72 72 65 63 74 6c 79 2e 0a 20  ity correctly.. 
2c7f3 20 2a 2f 0a 20 20 70 43 6f 6c 2d 3e 61 66 66 69   */.  pCol->affi
2c7f4 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46  nity = SQLITE_AF
2c7f5 46 5f 4e 4f 4e 45 3b 0a 20 20 70 2d 3e 6e 43 6f  F_NONE;.  p->nCo
2c7f6 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  l++;.}../*.** Th
2c7f7 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
2c7f8 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
2c7f9 65 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20  er while in the 
2c7fa 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72  middle of.** par
2c7fb 73 69 6e 67 20 61 20 43 52 45 41 54 45 20 54 41  sing a CREATE TA
2c7fc 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  BLE statement.  
2c7fd 41 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f 6e  A "NOT NULL" con
2c7fe 73 74 72 61 69 6e 74 20 68 61 73 0a 2a 2a 20 62  straint has.** b
2c7ff 65 65 6e 20 73 65 65 6e 20 6f 6e 20 61 20 63 6f  een seen on a co
2c800 6c 75 6d 6e 2e 20 20 54 68 69 73 20 72 6f 75 74  lumn.  This rout
2c801 69 6e 65 20 73 65 74 73 20 74 68 65 20 6e 6f 74  ine sets the not
2c802 4e 75 6c 6c 20 66 6c 61 67 20 6f 6e 0a 2a 2a 20  Null flag on.** 
2c803 74 68 65 20 63 6f 6c 75 6d 6e 20 63 75 72 72 65  the column curre
2c804 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74  ntly under const
2c805 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  ruction..*/.SQLI
2c806 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2c807 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c  sqlite3AddNotNul
2c808 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  l(Parse *pParse,
2c809 20 69 6e 74 20 6f 6e 45 72 72 6f 72 29 7b 0a 20   int onError){. 
2c80a 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 70 20 3d   Table *p;.  p =
2c80b 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
2c80c 6c 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c  le;.  if( p==0 |
2c80d 7c 20 4e 45 56 45 52 28 70 2d 3e 6e 43 6f 6c 3c  | NEVER(p->nCol<
2c80e 31 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  1) ) return;.  p
2c80f 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31  ->aCol[p->nCol-1
2c810 5d 2e 6e 6f 74 4e 75 6c 6c 20 3d 20 28 75 38 29  ].notNull = (u8)
2c811 6f 6e 45 72 72 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a  onError;.}../*.*
2c812 2a 20 53 63 61 6e 20 74 68 65 20 63 6f 6c 75 6d  * Scan the colum
2c813 6e 20 74 79 70 65 20 6e 61 6d 65 20 7a 54 79 70  n type name zTyp
2c814 65 20 28 6c 65 6e 67 74 68 20 6e 54 79 70 65 29  e (length nType)
2c815 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 0a   and return the.
2c816 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 61 66  ** associated af
2c817 66 69 6e 69 74 79 20 74 79 70 65 2e 0a 2a 2a 0a  finity type..**.
2c818 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2c819 64 6f 65 73 20 61 20 63 61 73 65 2d 69 6e 64 65  does a case-inde
2c81a 70 65 6e 64 65 6e 74 20 73 65 61 72 63 68 20 6f  pendent search o
2c81b 66 20 7a 54 79 70 65 20 66 6f 72 20 74 68 65 20  f zType for the 
2c81c 0a 2a 2a 20 73 75 62 73 74 72 69 6e 67 73 20 69  .** substrings i
2c81d 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
2c81e 74 61 62 6c 65 2e 20 49 66 20 6f 6e 65 20 6f 66  table. If one of
2c81f 20 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 20   the substrings 
2c820 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 74 68 65  is.** found, the
2c821 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61   corresponding a
2c822 66 66 69 6e 69 74 79 20 69 73 20 72 65 74 75 72  ffinity is retur
2c823 6e 65 64 2e 20 49 66 20 7a 54 79 70 65 20 63 6f  ned. If zType co
2c824 6e 74 61 69 6e 73 0a 2a 2a 20 6d 6f 72 65 20 74  ntains.** more t
2c825 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 73  han one of the s
2c826 75 62 73 74 72 69 6e 67 73 2c 20 65 6e 74 72 69  ubstrings, entri
2c827 65 73 20 74 6f 77 61 72 64 20 74 68 65 20 74 6f  es toward the to
2c828 70 20 6f 66 20 0a 2a 2a 20 74 68 65 20 74 61 62  p of .** the tab
2c829 6c 65 20 74 61 6b 65 20 70 72 69 6f 72 69 74 79  le take priority
2c82a 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  . For example, i
2c82b 66 20 7a 54 79 70 65 20 69 73 20 27 42 4c 4f 42  f zType is 'BLOB
2c82c 49 4e 54 27 2c 20 0a 2a 2a 20 53 51 4c 49 54 45  INT', .** SQLITE
2c82d 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 69 73 20  _AFF_INTEGER is 
2c82e 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
2c82f 53 75 62 73 74 72 69 6e 67 20 20 20 20 20 7c 20  Substring     | 
2c830 41 66 66 69 6e 69 74 79 0a 2a 2a 20 2d 2d 2d 2d  Affinity.** ----
2c831 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c832 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
2c833 27 49 4e 54 27 20 20 20 20 20 20 20 20 20 7c 20  'INT'         | 
2c834 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
2c835 45 52 0a 2a 2a 20 27 43 48 41 52 27 20 20 20 20  ER.** 'CHAR'    
2c836 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46      | SQLITE_AFF
2c837 5f 54 45 58 54 0a 2a 2a 20 27 43 4c 4f 42 27 20  _TEXT.** 'CLOB' 
2c838 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
2c839 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 54 45 58  AFF_TEXT.** 'TEX
2c83a 54 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49  T'        | SQLI
2c83b 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27  TE_AFF_TEXT.** '
2c83c 42 4c 4f 42 27 20 20 20 20 20 20 20 20 7c 20 53  BLOB'        | S
2c83d 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a 2a  QLITE_AFF_NONE.*
2c83e 2a 20 27 52 45 41 4c 27 20 20 20 20 20 20 20 20  * 'REAL'        
2c83f 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  | SQLITE_AFF_REA
2c840 4c 0a 2a 2a 20 27 46 4c 4f 41 27 20 20 20 20 20  L.** 'FLOA'     
2c841 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f     | SQLITE_AFF_
2c842 52 45 41 4c 0a 2a 2a 20 27 44 4f 55 42 27 20 20  REAL.** 'DOUB'  
2c843 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41        | SQLITE_A
2c844 46 46 5f 52 45 41 4c 0a 2a 2a 0a 2a 2a 20 49 66  FF_REAL.**.** If
2c845 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62   none of the sub
2c846 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20 61  strings in the a
2c847 62 6f 76 65 20 74 61 62 6c 65 20 61 72 65 20 66  bove table are f
2c848 6f 75 6e 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f  ound,.** SQLITE_
2c849 41 46 46 5f 4e 55 4d 45 52 49 43 20 69 73 20 72  AFF_NUMERIC is r
2c84a 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
2c84b 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
2c84c 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54  sqlite3AffinityT
2c84d 79 70 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ype(const char *
2c84e 7a 49 6e 29 7b 0a 20 20 75 33 32 20 68 20 3d 20  zIn){.  u32 h = 
2c84f 30 3b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20  0;.  char aff = 
2c850 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
2c851 49 43 3b 0a 0a 20 20 69 66 28 20 7a 49 6e 20 29  IC;..  if( zIn )
2c852 20 77 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20 29   while( zIn[0] )
2c853 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 38 29  {.    h = (h<<8)
2c854 20 2b 20 73 71 6c 69 74 65 33 55 70 70 65 72 54   + sqlite3UpperT
2c855 6f 4c 6f 77 65 72 5b 28 2a 7a 49 6e 29 26 30 78  oLower[(*zIn)&0x
2c856 66 66 5d 3b 0a 20 20 20 20 7a 49 6e 2b 2b 3b 0a  ff];.    zIn++;.
2c857 20 20 20 20 69 66 28 20 68 3d 3d 28 28 27 63 27      if( h==(('c'
2c858 3c 3c 32 34 29 2b 28 27 68 27 3c 3c 31 36 29 2b  <<24)+('h'<<16)+
2c859 28 27 61 27 3c 3c 38 29 2b 27 72 27 29 20 29 7b  ('a'<<8)+'r') ){
2c85a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c85b 43 48 41 52 20 2a 2f 0a 20 20 20 20 20 20 61 66  CHAR */.      af
2c85c 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54  f = SQLITE_AFF_T
2c85d 45 58 54 3b 20 0a 20 20 20 20 7d 65 6c 73 65 20  EXT; .    }else 
2c85e 69 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34  if( h==(('c'<<24
2c85f 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27  )+('l'<<16)+('o'
2c860 3c 3c 38 29 2b 27 62 27 29 20 29 7b 20 20 20 20  <<8)+'b') ){    
2c861 20 20 20 2f 2a 20 43 4c 4f 42 20 2a 2f 0a 20 20     /* CLOB */.  
2c862 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
2c863 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 7d  _AFF_TEXT;.    }
2c864 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 74  else if( h==(('t
2c865 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36 29  '<<24)+('e'<<16)
2c866 2b 28 27 78 27 3c 3c 38 29 2b 27 74 27 29 20 29  +('x'<<8)+'t') )
2c867 7b 20 20 20 20 20 20 20 2f 2a 20 54 45 58 54 20  {       /* TEXT 
2c868 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53  */.      aff = S
2c869 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a  QLITE_AFF_TEXT;.
2c86a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d      }else if( h=
2c86b 3d 28 28 27 62 27 3c 3c 32 34 29 2b 28 27 6c 27  =(('b'<<24)+('l'
2c86c 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27  <<16)+('o'<<8)+'
2c86d 62 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b')          /* 
2c86e 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 20 20  BLOB */.        
2c86f 26 26 20 28 61 66 66 3d 3d 53 51 4c 49 54 45 5f  && (aff==SQLITE_
2c870 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c 7c 20 61  AFF_NUMERIC || a
2c871 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52  ff==SQLITE_AFF_R
2c872 45 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 61 66  EAL) ){.      af
2c873 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  f = SQLITE_AFF_N
2c874 4f 4e 45 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ONE;.#ifndef SQL
2c875 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
2c876 47 5f 50 4f 49 4e 54 0a 20 20 20 20 7d 65 6c 73  G_POINT.    }els
2c877 65 20 69 66 28 20 68 3d 3d 28 28 27 72 27 3c 3c  e if( h==(('r'<<
2c878 32 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28 27  24)+('e'<<16)+('
2c879 61 27 3c 3c 38 29 2b 27 6c 27 29 20 20 20 20 20  a'<<8)+'l')     
2c87a 20 20 20 20 20 2f 2a 20 52 45 41 4c 20 2a 2f 0a       /* REAL */.
2c87b 20 20 20 20 20 20 20 20 26 26 20 61 66 66 3d 3d          && aff==
2c87c 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
2c87d 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20  IC ){.      aff 
2c87e 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  = SQLITE_AFF_REA
2c87f 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  L;.    }else if(
2c880 20 68 3d 3d 28 28 27 66 27 3c 3c 32 34 29 2b 28   h==(('f'<<24)+(
2c881 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38  'l'<<16)+('o'<<8
2c882 29 2b 27 61 27 29 20 20 20 20 20 20 20 20 20 20  )+'a')          
2c883 2f 2a 20 46 4c 4f 41 20 2a 2f 0a 20 20 20 20 20  /* FLOA */.     
2c884 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54     && aff==SQLIT
2c885 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b  E_AFF_NUMERIC ){
2c886 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c  .      aff = SQL
2c887 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 20 20  ITE_AFF_REAL;.  
2c888 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28    }else if( h==(
2c889 28 27 64 27 3c 3c 32 34 29 2b 28 27 6f 27 3c 3c  ('d'<<24)+('o'<<
2c88a 31 36 29 2b 28 27 75 27 3c 3c 38 29 2b 27 62 27  16)+('u'<<8)+'b'
2c88b 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 4f  )          /* DO
2c88c 55 42 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26  UB */.        &&
2c88d 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46   aff==SQLITE_AFF
2c88e 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20  _NUMERIC ){.    
2c88f 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
2c890 46 46 5f 52 45 41 4c 3b 0a 23 65 6e 64 69 66 0a  FF_REAL;.#endif.
2c891 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 68      }else if( (h
2c892 26 30 78 30 30 46 46 46 46 46 46 29 3d 3d 28 28  &0x00FFFFFF)==((
2c893 27 69 27 3c 3c 31 36 29 2b 28 27 6e 27 3c 3c 38  'i'<<16)+('n'<<8
2c894 29 2b 27 74 27 29 20 29 7b 20 20 20 20 2f 2a 20  )+'t') ){    /* 
2c895 49 4e 54 20 2a 2f 0a 20 20 20 20 20 20 61 66 66  INT */.      aff
2c896 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e   = SQLITE_AFF_IN
2c897 54 45 47 45 52 3b 0a 20 20 20 20 20 20 62 72 65  TEGER;.      bre
2c898 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ak;.    }.  }.. 
2c899 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a   return aff;.}..
2c89a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2c89b 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  ne is called by 
2c89c 74 68 65 20 70 61 72 73 65 72 20 77 68 69 6c 65  the parser while
2c89d 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
2c89e 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43  f.** parsing a C
2c89f 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
2c8a0 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46 69 72  ement.  The pFir
2c8a1 73 74 20 74 6f 6b 65 6e 20 69 73 20 74 68 65 20  st token is the 
2c8a2 66 69 72 73 74 0a 2a 2a 20 74 6f 6b 65 6e 20 69  first.** token i
2c8a3 6e 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 6f  n the sequence o
2c8a4 66 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 64 65  f tokens that de
2c8a5 73 63 72 69 62 65 20 74 68 65 20 74 79 70 65 20  scribe the type 
2c8a6 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e  of the.** column
2c8a7 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72   currently under
2c8a8 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20   construction.  
2c8a9 20 70 4c 61 73 74 20 69 73 20 74 68 65 20 6c 61   pLast is the la
2c8aa 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e 20 74  st token.** in t
2c8ab 68 65 20 73 65 71 75 65 6e 63 65 2e 20 20 55 73  he sequence.  Us
2c8ac 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69  e this informati
2c8ad 6f 6e 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20  on to construct 
2c8ae 61 20 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74  a string.** that
2c8af 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 79   contains the ty
2c8b0 70 65 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f  pename of the co
2c8b1 6c 75 6d 6e 20 61 6e 64 20 73 74 6f 72 65 20 74  lumn and store t
2c8b2 68 61 74 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e  hat string.** in
2c8b3 20 7a 54 79 70 65 2e 0a 2a 2f 20 0a 53 51 4c 49   zType..*/ .SQLI
2c8b4 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2c8b5 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
2c8b6 54 79 70 65 28 50 61 72 73 65 20 2a 70 50 61 72  Type(Parse *pPar
2c8b7 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 79 70 65  se, Token *pType
2c8b8 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20  ){.  Table *p;. 
2c8b9 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 0a   Column *pCol;..
2c8ba 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e    p = pParse->pN
2c8bb 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70  ewTable;.  if( p
2c8bc 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 2d 3e  ==0 || NEVER(p->
2c8bd 6e 43 6f 6c 3c 31 29 20 29 20 72 65 74 75 72 6e  nCol<1) ) return
2c8be 3b 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e 61  ;.  pCol = &p->a
2c8bf 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a  Col[p->nCol-1];.
2c8c0 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e    assert( pCol->
2c8c1 7a 54 79 70 65 3d 3d 30 20 29 3b 0a 20 20 70 43  zType==0 );.  pC
2c8c2 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 73 71 6c 69  ol->zType = sqli
2c8c3 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
2c8c4 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 79  (pParse->db, pTy
2c8c5 70 65 29 3b 0a 20 20 70 43 6f 6c 2d 3e 61 66 66  pe);.  pCol->aff
2c8c6 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 41  inity = sqlite3A
2c8c7 66 66 69 6e 69 74 79 54 79 70 65 28 70 43 6f 6c  ffinityType(pCol
2c8c8 2d 3e 7a 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a  ->zType);.}../*.
2c8c9 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  ** The expressio
2c8ca 6e 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74  n is the default
2c8cb 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6d   value for the m
2c8cc 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64  ost recently add
2c8cd 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20  ed column.** of 
2c8ce 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e  the table curren
2c8cf 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tly under constr
2c8d0 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 65  uction..**.** De
2c8d1 66 61 75 6c 74 20 76 61 6c 75 65 20 65 78 70 72  fault value expr
2c8d2 65 73 73 69 6f 6e 73 20 6d 75 73 74 20 62 65 20  essions must be 
2c8d3 63 6f 6e 73 74 61 6e 74 2e 20 20 52 61 69 73 65  constant.  Raise
2c8d4 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 66   an exception if
2c8d5 20 74 68 69 73 0a 2a 2a 20 69 73 20 6e 6f 74 20   this.** is not 
2c8d6 74 68 65 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  the case..**.** 
2c8d7 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
2c8d8 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61  called by the pa
2c8d9 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74 68  rser while in th
2c8da 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70  e middle of.** p
2c8db 61 72 73 69 6e 67 20 61 20 43 52 45 41 54 45 20  arsing a CREATE 
2c8dc 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e  TABLE statement.
2c8dd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2c8de 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
2c8df 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 50  ddDefaultValue(P
2c8e0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
2c8e1 70 72 53 70 61 6e 20 2a 70 53 70 61 6e 29 7b 0a  prSpan *pSpan){.
2c8e2 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 43 6f    Table *p;.  Co
2c8e3 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71  lumn *pCol;.  sq
2c8e4 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2c8e5 73 65 2d 3e 64 62 3b 0a 20 20 70 20 3d 20 70 50  se->db;.  p = pP
2c8e6 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b  arse->pNewTable;
2c8e7 0a 20 20 69 66 28 20 70 21 3d 30 20 29 7b 0a 20  .  if( p!=0 ){. 
2c8e8 20 20 20 70 43 6f 6c 20 3d 20 26 28 70 2d 3e 61     pCol = &(p->a
2c8e9 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 29 3b  Col[p->nCol-1]);
2c8ea 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65  .    if( !sqlite
2c8eb 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f  3ExprIsConstantO
2c8ec 72 46 75 6e 63 74 69 6f 6e 28 70 53 70 61 6e 2d  rFunction(pSpan-
2c8ed 3e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20  >pExpr) ){.     
2c8ee 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2c8ef 28 70 50 61 72 73 65 2c 20 22 64 65 66 61 75 6c  (pParse, "defaul
2c8f0 74 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c 75 6d  t value of colum
2c8f1 6e 20 5b 25 73 5d 20 69 73 20 6e 6f 74 20 63 6f  n [%s] is not co
2c8f2 6e 73 74 61 6e 74 22 2c 0a 20 20 20 20 20 20 20  nstant",.       
2c8f3 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b     pCol->zName);
2c8f4 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2c8f5 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f 66 20 70    /* A copy of p
2c8f6 45 78 70 72 20 69 73 20 75 73 65 64 20 69 6e 73  Expr is used ins
2c8f7 74 65 61 64 20 6f 66 20 74 68 65 20 6f 72 69 67  tead of the orig
2c8f8 69 6e 61 6c 2c 20 61 73 20 70 45 78 70 72 20 63  inal, as pExpr c
2c8f9 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 2a 2a  ontains.      **
2c8fa 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 70 6f 69   tokens that poi
2c8fb 6e 74 20 74 6f 20 76 6f 6c 61 74 69 6c 65 20 6d  nt to volatile m
2c8fc 65 6d 6f 72 79 2e 20 54 68 65 20 27 73 70 61 6e  emory. The 'span
2c8fd 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  ' of the express
2c8fe 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73 20  ion.      ** is 
2c8ff 72 65 71 75 69 72 65 64 20 62 79 20 70 72 61 67  required by prag
2c900 6d 61 20 74 61 62 6c 65 5f 69 6e 66 6f 2e 0a 20  ma table_info.. 
2c901 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
2c902 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2c903 64 62 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 29  db, pCol->pDflt)
2c904 3b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 44  ;.      pCol->pD
2c905 66 6c 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  flt = sqlite3Exp
2c906 72 44 75 70 28 64 62 2c 20 70 53 70 61 6e 2d 3e  rDup(db, pSpan->
2c907 70 45 78 70 72 2c 20 45 58 50 52 44 55 50 5f 52  pExpr, EXPRDUP_R
2c908 45 44 55 43 45 29 3b 0a 20 20 20 20 20 20 73 71  EDUCE);.      sq
2c909 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2c90a 70 43 6f 6c 2d 3e 7a 44 66 6c 74 29 3b 0a 20 20  pCol->zDflt);.  
2c90b 20 20 20 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74 20      pCol->zDflt 
2c90c 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44  = sqlite3DbStrND
2c90d 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 53  up(db, (char*)pS
2c90e 70 61 6e 2d 3e 7a 53 74 61 72 74 2c 0a 20 20 20  pan->zStart,.   
2c90f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c911 20 20 28 69 6e 74 29 28 70 53 70 61 6e 2d 3e 7a    (int)(pSpan->z
2c912 45 6e 64 20 2d 20 70 53 70 61 6e 2d 3e 7a 53 74  End - pSpan->zSt
2c913 61 72 74 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  art));.    }.  }
2c914 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
2c915 6c 65 74 65 28 64 62 2c 20 70 53 70 61 6e 2d 3e  lete(db, pSpan->
2c916 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pExpr);.}../*.**
2c917 20 44 65 73 69 67 6e 61 74 65 20 74 68 65 20 50   Designate the P
2c918 52 49 4d 41 52 59 20 4b 45 59 20 66 6f 72 20 74  RIMARY KEY for t
2c919 68 65 20 74 61 62 6c 65 2e 20 20 70 4c 69 73 74  he table.  pList
2c91a 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 6e 61   is a list of na
2c91b 6d 65 73 20 0a 2a 2a 20 6f 66 20 63 6f 6c 75 6d  mes .** of colum
2c91c 6e 73 20 74 68 61 74 20 66 6f 72 6d 20 74 68 65  ns that form the
2c91d 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 49   primary key.  I
2c91e 66 20 70 4c 69 73 74 20 69 73 20 4e 55 4c 4c 2c  f pList is NULL,
2c91f 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73   then the.** mos
2c920 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64  t recently added
2c921 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74   column of the t
2c922 61 62 6c 65 20 69 73 20 74 68 65 20 70 72 69 6d  able is the prim
2c923 61 72 79 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41  ary key..**.** A
2c924 20 74 61 62 6c 65 20 63 61 6e 20 68 61 76 65 20   table can have 
2c925 61 74 20 6d 6f 73 74 20 6f 6e 65 20 70 72 69 6d  at most one prim
2c926 61 72 79 20 6b 65 79 2e 20 20 49 66 20 74 68 65  ary key.  If the
2c927 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 68   table already h
2c928 61 73 0a 2a 2a 20 61 20 70 72 69 6d 61 72 79 20  as.** a primary 
2c929 6b 65 79 20 28 61 6e 64 20 74 68 69 73 20 69 73  key (and this is
2c92a 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 69 6d   the second prim
2c92b 61 72 79 20 6b 65 79 29 20 74 68 65 6e 20 63 72  ary key) then cr
2c92c 65 61 74 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72  eate an.** error
2c92d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50  ..**.** If the P
2c92e 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20 6f 6e  RIMARY KEY is on
2c92f 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e   a single column
2c930 20 77 68 6f 73 65 20 64 61 74 61 74 79 70 65 20   whose datatype 
2c931 69 73 20 49 4e 54 45 47 45 52 2c 0a 2a 2a 20 74  is INTEGER,.** t
2c932 68 65 6e 20 77 65 20 77 69 6c 6c 20 74 72 79 20  hen we will try 
2c933 74 6f 20 75 73 65 20 74 68 61 74 20 63 6f 6c 75  to use that colu
2c934 6d 6e 20 61 73 20 74 68 65 20 72 6f 77 69 64 2e  mn as the rowid.
2c935 20 20 53 65 74 20 74 68 65 20 54 61 62 6c 65 2e    Set the Table.
2c936 69 50 4b 65 79 0a 2a 2a 20 66 69 65 6c 64 20 6f  iPKey.** field o
2c937 66 20 74 68 65 20 74 61 62 6c 65 20 75 6e 64 65  f the table unde
2c938 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 74  r construction t
2c939 6f 20 62 65 20 74 68 65 20 69 6e 64 65 78 20 6f  o be the index o
2c93a 66 20 74 68 65 0a 2a 2a 20 49 4e 54 45 47 45 52  f the.** INTEGER
2c93b 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c   PRIMARY KEY col
2c93c 75 6d 6e 2e 20 20 54 61 62 6c 65 2e 69 50 4b 65  umn.  Table.iPKe
2c93d 79 20 69 73 20 73 65 74 20 74 6f 20 2d 31 20 69  y is set to -1 i
2c93e 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f  f there is.** no
2c93f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
2c940 20 4b 45 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74   KEY..**.** If t
2c941 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 61 6e  he key is not an
2c942 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
2c943 20 4b 45 59 2c 20 74 68 65 6e 20 63 72 65 61 74   KEY, then creat
2c944 65 20 61 20 75 6e 69 71 75 65 0a 2a 2a 20 69 6e  e a unique.** in
2c945 64 65 78 20 66 6f 72 20 74 68 65 20 6b 65 79 2e  dex for the key.
2c946 20 20 4e 6f 20 69 6e 64 65 78 20 69 73 20 63 72    No index is cr
2c947 65 61 74 65 64 20 66 6f 72 20 49 4e 54 45 47 45  eated for INTEGE
2c948 52 20 50 52 49 4d 41 52 59 20 4b 45 59 73 2e 0a  R PRIMARY KEYs..
2c949 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2c94a 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
2c94b 64 50 72 69 6d 61 72 79 4b 65 79 28 0a 20 20 50  dPrimaryKey(.  P
2c94c 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2c94d 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
2c94e 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ext */.  ExprLis
2c94f 74 20 2a 70 4c 69 73 74 2c 20 20 2f 2a 20 4c 69  t *pList,  /* Li
2c950 73 74 20 6f 66 20 66 69 65 6c 64 20 6e 61 6d 65  st of field name
2c951 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20  s to be indexed 
2c952 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72  */.  int onError
2c953 2c 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74  ,      /* What t
2c954 6f 20 64 6f 20 77 69 74 68 20 61 20 75 6e 69 71  o do with a uniq
2c955 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20  ueness conflict 
2c956 2a 2f 0a 20 20 69 6e 74 20 61 75 74 6f 49 6e 63  */.  int autoInc
2c957 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ,      /* True i
2c958 66 20 74 68 65 20 41 55 54 4f 49 4e 43 52 45 4d  f the AUTOINCREM
2c959 45 4e 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70  ENT keyword is p
2c95a 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  resent */.  int 
2c95b 73 6f 72 74 4f 72 64 65 72 20 20 20 20 20 2f 2a  sortOrder     /*
2c95c 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 6f   SQLITE_SO_ASC o
2c95d 72 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43  r SQLITE_SO_DESC
2c95e 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a   */.){.  Table *
2c95f 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70  pTab = pParse->p
2c960 4e 65 77 54 61 62 6c 65 3b 0a 20 20 63 68 61 72  NewTable;.  char
2c961 20 2a 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20 69   *zType = 0;.  i
2c962 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 2c 20 69 3b  nt iCol = -1, i;
2c963 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 7c  .  if( pTab==0 |
2c964 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41  | IN_DECLARE_VTA
2c965 42 20 29 20 67 6f 74 6f 20 70 72 69 6d 61 72 79  B ) goto primary
2c966 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 20 69 66 28  _key_exit;.  if(
2c967 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20   pTab->tabFlags 
2c968 26 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b  & TF_HasPrimaryK
2c969 65 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ey ){.    sqlite
2c96a 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2c96b 2c 20 0a 20 20 20 20 20 20 22 74 61 62 6c 65 20  , .      "table 
2c96c 5c 22 25 73 5c 22 20 68 61 73 20 6d 6f 72 65 20  \"%s\" has more 
2c96d 74 68 61 6e 20 6f 6e 65 20 70 72 69 6d 61 72 79  than one primary
2c96e 20 6b 65 79 22 2c 20 70 54 61 62 2d 3e 7a 4e 61   key", pTab->zNa
2c96f 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 70 72  me);.    goto pr
2c970 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b 0a  imary_key_exit;.
2c971 20 20 7d 0a 20 20 70 54 61 62 2d 3e 74 61 62 46    }.  pTab->tabF
2c972 6c 61 67 73 20 7c 3d 20 54 46 5f 48 61 73 50 72  lags |= TF_HasPr
2c973 69 6d 61 72 79 4b 65 79 3b 0a 20 20 69 66 28 20  imaryKey;.  if( 
2c974 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  pList==0 ){.    
2c975 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 6f  iCol = pTab->nCo
2c976 6c 20 2d 20 31 3b 0a 20 20 20 20 70 54 61 62 2d  l - 1;.    pTab-
2c977 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 63 6f 6c 46  >aCol[iCol].colF
2c978 6c 61 67 73 20 7c 3d 20 43 4f 4c 46 4c 41 47 5f  lags |= COLFLAG_
2c979 50 52 49 4d 4b 45 59 3b 0a 20 20 7d 65 6c 73 65  PRIMKEY;.  }else
2c97a 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
2c97b 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  <pList->nExpr; i
2c97c 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69  ++){.      for(i
2c97d 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62  Col=0; iCol<pTab
2c97e 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b  ->nCol; iCol++){
2c97f 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
2c980 69 74 65 33 53 74 72 49 43 6d 70 28 70 4c 69 73  ite3StrICmp(pLis
2c981 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 70  t->a[i].zName, p
2c982 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
2c983 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
2c984 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2c985 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2c986 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 70        if( iCol<p
2c987 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20  Tab->nCol ){.   
2c988 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b       pTab->aCol[
2c989 69 43 6f 6c 5d 2e 63 6f 6c 46 6c 61 67 73 20 7c  iCol].colFlags |
2c98a 3d 20 43 4f 4c 46 4c 41 47 5f 50 52 49 4d 4b 45  = COLFLAG_PRIMKE
2c98b 59 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  Y;.      }.    }
2c98c 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e  .    if( pList->
2c98d 6e 45 78 70 72 3e 31 20 29 20 69 43 6f 6c 20 3d  nExpr>1 ) iCol =
2c98e 20 2d 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69   -1;.  }.  if( i
2c98f 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70  Col>=0 && iCol<p
2c990 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20  Tab->nCol ){.   
2c991 20 7a 54 79 70 65 20 3d 20 70 54 61 62 2d 3e 61   zType = pTab->a
2c992 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b  Col[iCol].zType;
2c993 0a 20 20 7d 0a 20 20 69 66 28 20 7a 54 79 70 65  .  }.  if( zType
2c994 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
2c995 6d 70 28 7a 54 79 70 65 2c 20 22 49 4e 54 45 47  mp(zType, "INTEG
2c996 45 52 22 29 3d 3d 30 0a 20 20 20 20 20 20 20 20  ER")==0.        
2c997 26 26 20 73 6f 72 74 4f 72 64 65 72 3d 3d 53 51  && sortOrder==SQ
2c998 4c 49 54 45 5f 53 4f 5f 41 53 43 20 29 7b 0a 20  LITE_SO_ASC ){. 
2c999 20 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d     pTab->iPKey =
2c99a 20 69 43 6f 6c 3b 0a 20 20 20 20 70 54 61 62 2d   iCol;.    pTab-
2c99b 3e 6b 65 79 43 6f 6e 66 20 3d 20 28 75 38 29 6f  >keyConf = (u8)o
2c99c 6e 45 72 72 6f 72 3b 0a 20 20 20 20 61 73 73 65  nError;.    asse
2c99d 72 74 28 20 61 75 74 6f 49 6e 63 3d 3d 30 20 7c  rt( autoInc==0 |
2c99e 7c 20 61 75 74 6f 49 6e 63 3d 3d 31 20 29 3b 0a  | autoInc==1 );.
2c99f 20 20 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61      pTab->tabFla
2c9a0 67 73 20 7c 3d 20 61 75 74 6f 49 6e 63 2a 54 46  gs |= autoInc*TF
2c9a1 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 3b 0a  _Autoincrement;.
2c9a2 20 20 7d 65 6c 73 65 20 69 66 28 20 61 75 74 6f    }else if( auto
2c9a3 49 6e 63 20 29 7b 0a 23 69 66 6e 64 65 66 20 53  Inc ){.#ifndef S
2c9a4 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
2c9a5 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 73 71 6c  NCREMENT.    sql
2c9a6 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2c9a7 72 73 65 2c 20 22 41 55 54 4f 49 4e 43 52 45 4d  rse, "AUTOINCREM
2c9a8 45 4e 54 20 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f  ENT is only allo
2c9a9 77 65 64 20 6f 6e 20 61 6e 20 22 0a 20 20 20 20  wed on an ".    
2c9aa 20 20 20 22 49 4e 54 45 47 45 52 20 50 52 49 4d     "INTEGER PRIM
2c9ab 41 52 59 20 4b 45 59 22 29 3b 0a 23 65 6e 64 69  ARY KEY");.#endi
2c9ac 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 49  f.  }else{.    I
2c9ad 6e 64 65 78 20 2a 70 3b 0a 20 20 20 20 70 20 3d  ndex *p;.    p =
2c9ae 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e   sqlite3CreateIn
2c9af 64 65 78 28 70 50 61 72 73 65 2c 20 30 2c 20 30  dex(pParse, 0, 0
2c9b0 2c 20 30 2c 20 70 4c 69 73 74 2c 20 6f 6e 45 72  , 0, pList, onEr
2c9b1 72 6f 72 2c 20 30 2c 20 30 2c 20 73 6f 72 74 4f  ror, 0, 0, sortO
2c9b2 72 64 65 72 2c 20 30 29 3b 0a 20 20 20 20 69 66  rder, 0);.    if
2c9b3 28 20 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  ( p ){.      p->
2c9b4 61 75 74 6f 49 6e 64 65 78 20 3d 20 32 3b 0a 20  autoIndex = 2;. 
2c9b5 20 20 20 7d 0a 20 20 20 20 70 4c 69 73 74 20 3d     }.    pList =
2c9b6 20 30 3b 0a 20 20 7d 0a 0a 70 72 69 6d 61 72 79   0;.  }..primary
2c9b7 5f 6b 65 79 5f 65 78 69 74 3a 0a 20 20 73 71 6c  _key_exit:.  sql
2c9b8 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2c9b9 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
2c9ba 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 3b  List);.  return;
2c9bb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20  .}../*.** Add a 
2c9bc 6e 65 77 20 43 48 45 43 4b 20 63 6f 6e 73 74 72  new CHECK constr
2c9bd 61 69 6e 74 20 74 6f 20 74 68 65 20 74 61 62 6c  aint to the tabl
2c9be 65 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65  e currently unde
2c9bf 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  r construction..
2c9c0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2c9c1 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
2c9c2 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74  dCheckConstraint
2c9c3 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2c9c4 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67  e,    /* Parsing
2c9c5 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78   context */.  Ex
2c9c6 70 72 20 2a 70 43 68 65 63 6b 45 78 70 72 20 20  pr *pCheckExpr  
2c9c7 2f 2a 20 54 68 65 20 63 68 65 63 6b 20 65 78 70  /* The check exp
2c9c8 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 23 69  ression */.){.#i
2c9c9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2c9ca 54 5f 43 48 45 43 4b 0a 20 20 54 61 62 6c 65 20  T_CHECK.  Table 
2c9cb 2a 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e  *pTab = pParse->
2c9cc 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28  pNewTable;.  if(
2c9cd 20 70 54 61 62 20 26 26 20 21 49 4e 5f 44 45 43   pTab && !IN_DEC
2c9ce 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20  LARE_VTAB ){.   
2c9cf 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 20 3d 20   pTab->pCheck = 
2c9d0 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2c9d1 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 54  ppend(pParse, pT
2c9d2 61 62 2d 3e 70 43 68 65 63 6b 2c 20 70 43 68 65  ab->pCheck, pChe
2c9d3 63 6b 45 78 70 72 29 3b 0a 20 20 20 20 69 66 28  ckExpr);.    if(
2c9d4 20 70 50 61 72 73 65 2d 3e 63 6f 6e 73 74 72 61   pParse->constra
2c9d5 69 6e 74 4e 61 6d 65 2e 6e 20 29 7b 0a 20 20 20  intName.n ){.   
2c9d6 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
2c9d7 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72 73 65  stSetName(pParse
2c9d8 2c 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20  , pTab->pCheck, 
2c9d9 26 70 50 61 72 73 65 2d 3e 63 6f 6e 73 74 72 61  &pParse->constra
2c9da 69 6e 74 4e 61 6d 65 2c 20 31 29 3b 0a 20 20 20  intName, 1);.   
2c9db 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69   }.  }else.#endi
2c9dc 66 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 74 65  f.  {.    sqlite
2c9dd 33 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 72  3ExprDelete(pPar
2c9de 73 65 2d 3e 64 62 2c 20 70 43 68 65 63 6b 45 78  se->db, pCheckEx
2c9df 70 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  pr);.  }.}../*.*
2c9e0 2a 20 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74  * Set the collat
2c9e1 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20  ion function of 
2c9e2 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
2c9e3 79 20 70 61 72 73 65 64 20 74 61 62 6c 65 20 63  y parsed table c
2c9e4 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f 20 74 68 65 20  olumn.** to the 
2c9e5 43 6f 6c 6c 53 65 71 20 67 69 76 65 6e 2e 0a 2a  CollSeq given..*
2c9e6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2c9e7 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
2c9e8 43 6f 6c 6c 61 74 65 54 79 70 65 28 50 61 72 73  CollateType(Pars
2c9e9 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e  e *pParse, Token
2c9ea 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 54 61 62   *pToken){.  Tab
2c9eb 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a  le *p;.  int i;.
2c9ec 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20    char *zColl;  
2c9ed 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
2c9ee 65 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20  equoted name of 
2c9ef 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2c9f0 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  ce */.  sqlite3 
2c9f1 2a 64 62 3b 0a 0a 20 20 69 66 28 20 28 70 20 3d  *db;..  if( (p =
2c9f2 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
2c9f3 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  le)==0 ) return;
2c9f4 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31  .  i = p->nCol-1
2c9f5 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ;.  db = pParse-
2c9f6 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73  >db;.  zColl = s
2c9f7 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
2c9f8 6b 65 6e 28 64 62 2c 20 70 54 6f 6b 65 6e 29 3b  ken(db, pToken);
2c9f9 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 20  .  if( !zColl ) 
2c9fa 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 73  return;..  if( s
2c9fb 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c  qlite3LocateColl
2c9fc 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c  Seq(pParse, zCol
2c9fd 6c 29 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20  l) ){.    Index 
2c9fe 2a 70 49 64 78 3b 0a 20 20 20 20 70 2d 3e 61 43  *pIdx;.    p->aC
2c9ff 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 20 3d 20 7a 43  ol[i].zColl = zC
2ca00 6f 6c 6c 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49  oll;.  .    /* I
2ca01 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20  f the column is 
2ca02 64 65 63 6c 61 72 65 64 20 61 73 20 22 3c 6e 61  declared as "<na
2ca03 6d 65 3e 20 50 52 49 4d 41 52 59 20 4b 45 59 20  me> PRIMARY KEY 
2ca04 43 4f 4c 4c 41 54 45 20 3c 74 79 70 65 3e 22 2c  COLLATE <type>",
2ca05 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e 20  .    ** then an 
2ca06 69 6e 64 65 78 20 6d 61 79 20 68 61 76 65 20 62  index may have b
2ca07 65 65 6e 20 63 72 65 61 74 65 64 20 6f 6e 20 74  een created on t
2ca08 68 69 73 20 63 6f 6c 75 6d 6e 20 62 65 66 6f 72  his column befor
2ca09 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c  e the.    ** col
2ca0a 6c 61 74 69 6f 6e 20 74 79 70 65 20 77 61 73 20  lation type was 
2ca0b 61 64 64 65 64 2e 20 43 6f 72 72 65 63 74 20 74  added. Correct t
2ca0c 68 69 73 20 69 66 20 69 74 20 69 73 20 74 68 65  his if it is the
2ca0d 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   case..    */.  
2ca0e 20 20 66 6f 72 28 70 49 64 78 3d 70 2d 3e 70 49    for(pIdx=p->pI
2ca0f 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
2ca10 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pIdx->pNext){. 
2ca11 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64       assert( pId
2ca12 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b  x->nColumn==1 );
2ca13 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d  .      if( pIdx-
2ca14 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 20  >aiColumn[0]==i 
2ca15 29 7b 0a 20 20 20 20 20 20 20 20 70 49 64 78 2d  ){.        pIdx-
2ca16 3e 61 7a 43 6f 6c 6c 5b 30 5d 20 3d 20 70 2d 3e  >azColl[0] = p->
2ca17 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 3b 0a 20  aCol[i].zColl;. 
2ca18 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2ca19 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
2ca1a 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c  3DbFree(db, zCol
2ca1b 6c 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  l);.  }.}../*.**
2ca1c 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
2ca1d 65 74 75 72 6e 73 20 74 68 65 20 63 6f 6c 6c 61  eturns the colla
2ca1e 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f  tion sequence fo
2ca1f 72 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 76  r database nativ
2ca20 65 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69  e text.** encodi
2ca21 6e 67 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  ng identified by
2ca22 20 74 68 65 20 73 74 72 69 6e 67 20 7a 4e 61 6d   the string zNam
2ca23 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e  e, length nName.
2ca24 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  .**.** If the re
2ca25 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f  quested collatio
2ca26 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f  n sequence is no
2ca27 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 20  t available, or 
2ca28 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a  not available.**
2ca29 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
2ca2a 20 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67   native encoding
2ca2b 2c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  , the collation 
2ca2c 66 61 63 74 6f 72 79 20 69 73 20 69 6e 76 6f 6b  factory is invok
2ca2d 65 64 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74  ed to.** request
2ca2e 20 69 74 2e 20 49 66 20 74 68 65 20 63 6f 6c 6c   it. If the coll
2ca2f 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 64 6f  ation factory do
2ca30 65 73 20 6e 6f 74 20 73 75 70 70 6c 79 20 73 75  es not supply su
2ca31 63 68 20 61 20 73 65 71 75 65 6e 63 65 2c 0a 2a  ch a sequence,.*
2ca32 2a 20 61 6e 64 20 74 68 65 20 73 65 71 75 65 6e  * and the sequen
2ca33 63 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20  ce is available 
2ca34 69 6e 20 61 6e 6f 74 68 65 72 20 74 65 78 74 20  in another text 
2ca35 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74  encoding, then t
2ca36 68 61 74 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e  hat is.** return
2ca37 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a  ed instead..**.*
2ca38 2a 20 49 66 20 6e 6f 20 76 65 72 73 69 6f 6e 73  * If no versions
2ca39 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65   of the requeste
2ca3a 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 73 65 71  d collations seq
2ca3b 75 65 6e 63 65 20 61 72 65 20 61 76 61 69 6c 61  uence are availa
2ca3c 62 6c 65 2c 20 6f 72 0a 2a 2a 20 61 6e 6f 74 68  ble, or.** anoth
2ca3d 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  er error occurs,
2ca3e 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
2ca3f 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d  d and an error m
2ca40 65 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 69  essage written i
2ca41 6e 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a  nto.** pParse..*
2ca42 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2ca43 65 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61  e is a wrapper a
2ca44 72 6f 75 6e 64 20 73 71 6c 69 74 65 33 46 69 6e  round sqlite3Fin
2ca45 64 43 6f 6c 6c 53 65 71 28 29 2e 20 20 54 68 69  dCollSeq().  Thi
2ca46 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6e 76  s routine.** inv
2ca47 6f 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69  okes the collati
2ca48 6f 6e 20 66 61 63 74 6f 72 79 20 69 66 20 74 68  on factory if th
2ca49 65 20 6e 61 6d 65 64 20 63 6f 6c 6c 61 74 69 6f  e named collatio
2ca4a 6e 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e  n cannot be foun
2ca4b 64 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74  d.** and generat
2ca4c 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  es an error mess
2ca4d 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  age..**.** See a
2ca4e 6c 73 6f 3a 20 73 71 6c 69 74 65 33 46 69 6e 64  lso: sqlite3Find
2ca4f 43 6f 6c 6c 53 65 71 28 29 2c 20 73 71 6c 69 74  CollSeq(), sqlit
2ca50 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 29 0a 2a  e3GetCollSeq().*
2ca51 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2ca52 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65   CollSeq *sqlite
2ca53 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 50  3LocateCollSeq(P
2ca54 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f  arse *pParse, co
2ca55 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29  nst char *zName)
2ca56 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
2ca57 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2ca58 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29  u8 enc = ENC(db)
2ca59 3b 0a 20 20 75 38 20 69 6e 69 74 62 75 73 79 20  ;.  u8 initbusy 
2ca5a 3d 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3b  = db->init.busy;
2ca5b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
2ca5c 6c 3b 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71  l;..  pColl = sq
2ca5d 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
2ca5e 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c  (db, enc, zName,
2ca5f 20 69 6e 69 74 62 75 73 79 29 3b 0a 20 20 69 66   initbusy);.  if
2ca60 28 20 21 69 6e 69 74 62 75 73 79 20 26 26 20 28  ( !initbusy && (
2ca61 21 70 43 6f 6c 6c 20 7c 7c 20 21 70 43 6f 6c 6c  !pColl || !pColl
2ca62 2d 3e 78 43 6d 70 29 20 29 7b 0a 20 20 20 20 70  ->xCmp) ){.    p
2ca63 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65  Coll = sqlite3Ge
2ca64 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  tCollSeq(pParse,
2ca65 20 65 6e 63 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61   enc, pColl, zNa
2ca66 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  me);.  }..  retu
2ca67 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a  rn pColl;.}.../*
2ca68 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
2ca69 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e 63 72  e that will incr
2ca6a 65 6d 65 6e 74 20 74 68 65 20 73 63 68 65 6d 61  ement the schema
2ca6b 20 63 6f 6f 6b 69 65 2e 0a 2a 2a 0a 2a 2a 20 54   cookie..**.** T
2ca6c 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  he schema cookie
2ca6d 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65   is used to dete
2ca6e 72 6d 69 6e 65 20 77 68 65 6e 20 74 68 65 20 73  rmine when the s
2ca6f 63 68 65 6d 61 20 66 6f 72 20 74 68 65 0a 2a 2a  chema for the.**
2ca70 20 64 61 74 61 62 61 73 65 20 63 68 61 6e 67 65   database change
2ca71 73 2e 20 20 41 66 74 65 72 20 65 61 63 68 20 73  s.  After each s
2ca72 63 68 65 6d 61 20 63 68 61 6e 67 65 2c 20 74 68  chema change, th
2ca73 65 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 0a 2a  e cookie value.*
2ca74 2a 20 63 68 61 6e 67 65 73 2e 20 20 57 68 65 6e  * changes.  When
2ca75 20 61 20 70 72 6f 63 65 73 73 20 66 69 72 73 74   a process first
2ca76 20 72 65 61 64 73 20 74 68 65 20 73 63 68 65 6d   reads the schem
2ca77 61 20 69 74 20 72 65 63 6f 72 64 73 20 74 68 65  a it records the
2ca78 0a 2a 2a 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65  .** cookie.  The
2ca79 72 65 61 66 74 65 72 2c 20 77 68 65 6e 65 76 65  reafter, wheneve
2ca7a 72 20 69 74 20 67 6f 65 73 20 74 6f 20 61 63 63  r it goes to acc
2ca7b 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ess the database
2ca7c 2c 0a 2a 2a 20 69 74 20 63 68 65 63 6b 73 20 74  ,.** it checks t
2ca7d 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20 6d 61 6b  he cookie to mak
2ca7e 65 20 73 75 72 65 20 74 68 65 20 73 63 68 65 6d  e sure the schem
2ca7f 61 20 68 61 73 20 6e 6f 74 20 63 68 61 6e 67 65  a has not change
2ca80 64 0a 2a 2a 20 73 69 6e 63 65 20 69 74 20 77 61  d.** since it wa
2ca81 73 20 6c 61 73 74 20 72 65 61 64 2e 0a 2a 2a 0a  s last read..**.
2ca82 2a 2a 20 54 68 69 73 20 70 6c 61 6e 20 69 73 20  ** This plan is 
2ca83 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 62  not completely b
2ca84 75 6c 6c 65 74 2d 70 72 6f 6f 66 2e 20 20 49 74  ullet-proof.  It
2ca85 20 69 73 20 70 6f 73 73 69 62 6c 65 20 66 6f 72   is possible for
2ca86 0a 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20 74  .** the schema t
2ca87 6f 20 63 68 61 6e 67 65 20 6d 75 6c 74 69 70 6c  o change multipl
2ca88 65 20 74 69 6d 65 73 20 61 6e 64 20 66 6f 72 20  e times and for 
2ca89 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20 62 65  the cookie to be
2ca8a 0a 2a 2a 20 73 65 74 20 62 61 63 6b 20 74 6f 20  .** set back to 
2ca8b 70 72 69 6f 72 20 76 61 6c 75 65 2e 20 20 42 75  prior value.  Bu
2ca8c 74 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73  t schema changes
2ca8d 20 61 72 65 20 69 6e 66 72 65 71 75 65 6e 74 0a   are infrequent.
2ca8e 2a 2a 20 61 6e 64 20 74 68 65 20 70 72 6f 62 61  ** and the proba
2ca8f 62 69 6c 69 74 79 20 6f 66 20 68 69 74 74 69 6e  bility of hittin
2ca90 67 20 74 68 65 20 73 61 6d 65 20 63 6f 6f 6b 69  g the same cooki
2ca91 65 20 76 61 6c 75 65 20 69 73 20 6f 6e 6c 79 0a  e value is only.
2ca92 2a 2a 20 31 20 63 68 61 6e 63 65 20 69 6e 20 32  ** 1 chance in 2
2ca93 5e 33 32 2e 20 20 53 6f 20 77 65 27 72 65 20 73  ^32.  So we're s
2ca94 61 66 65 20 65 6e 6f 75 67 68 2e 0a 2a 2f 0a 53  afe enough..*/.S
2ca95 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2ca96 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65  id sqlite3Change
2ca97 43 6f 6f 6b 69 65 28 50 61 72 73 65 20 2a 70 50  Cookie(Parse *pP
2ca98 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a  arse, int iDb){.
2ca99 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
2ca9a 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2ca9b 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 20  rse);.  sqlite3 
2ca9c 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2ca9d 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  ;.  Vdbe *v = pP
2ca9e 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61  arse->pVdbe;.  a
2ca9f 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 63  ssert( sqlite3Sc
2caa0 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62  hemaMutexHeld(db
2caa1 2c 20 69 44 62 2c 20 30 29 20 29 3b 0a 20 20 73  , iDb, 0) );.  s
2caa2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2caa3 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2caa4 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
2caa5 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
2caa6 6b 69 65 2b 31 2c 20 72 31 29 3b 0a 20 20 73 71  kie+1, r1);.  sq
2caa7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2caa8 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c  v, OP_SetCookie,
2caa9 20 69 44 62 2c 20 42 54 52 45 45 5f 53 43 48 45   iDb, BTREE_SCHE
2caaa 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 72 31 29 3b  MA_VERSION, r1);
2caab 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2caac 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2caad 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d   r1);.}../*.** M
2caae 65 61 73 75 72 65 20 74 68 65 20 6e 75 6d 62 65  easure the numbe
2caaf 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
2cab0 6e 65 65 64 65 64 20 74 6f 20 6f 75 74 70 75 74  needed to output
2cab1 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 69 64   the given.** id
2cab2 65 6e 74 69 66 69 65 72 2e 20 20 54 68 65 20 6e  entifier.  The n
2cab3 75 6d 62 65 72 20 72 65 74 75 72 6e 65 64 20 69  umber returned i
2cab4 6e 63 6c 75 64 65 73 20 61 6e 79 20 71 75 6f 74  ncludes any quot
2cab5 65 73 20 75 73 65 64 0a 2a 2a 20 62 75 74 20 64  es used.** but d
2cab6 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20  oes not include 
2cab7 74 68 65 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61  the null termina
2cab8 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65  tor..**.** The e
2cab9 73 74 69 6d 61 74 65 20 69 73 20 63 6f 6e 73 65  stimate is conse
2caba 72 76 61 74 69 76 65 2e 20 20 49 74 20 6d 69 67  rvative.  It mig
2cabb 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 61  ht be larger tha
2cabc 74 20 77 68 61 74 20 69 73 0a 2a 2a 20 72 65 61  t what is.** rea
2cabd 6c 6c 79 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73  lly needed..*/.s
2cabe 74 61 74 69 63 20 69 6e 74 20 69 64 65 6e 74 4c  tatic int identL
2cabf 65 6e 67 74 68 28 63 6f 6e 73 74 20 63 68 61 72  ength(const char
2cac0 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20   *z){.  int n;. 
2cac1 20 66 6f 72 28 6e 3d 30 3b 20 2a 7a 3b 20 6e 2b   for(n=0; *z; n+
2cac2 2b 2c 20 7a 2b 2b 29 7b 0a 20 20 20 20 69 66 28  +, z++){.    if(
2cac3 20 2a 7a 3d 3d 27 22 27 20 29 7b 20 6e 2b 2b 3b   *z=='"' ){ n++;
2cac4 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2cac5 6e 20 2b 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n + 2;.}../*.** 
2cac6 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
2cac7 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ter is a pointer
2cac8 20 74 6f 20 61 6e 20 6f 75 74 70 75 74 20 62 75   to an output bu
2cac9 66 66 65 72 2e 20 54 68 65 20 73 65 63 6f 6e 64  ffer. The second
2caca 20 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69   .** parameter i
2cacb 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
2cacc 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 63  n integer that c
2cacd 6f 6e 74 61 69 6e 73 20 74 68 65 20 6f 66 66 73  ontains the offs
2cace 65 74 20 61 74 0a 2a 2a 20 77 68 69 63 68 20 74  et at.** which t
2cacf 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 74 68 65  o write into the
2cad0 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e 20   output buffer. 
2cad1 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f  This function co
2cad2 70 69 65 73 20 74 68 65 0a 2a 2a 20 6e 75 6c 2d  pies the.** nul-
2cad3 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e  terminated strin
2cad4 67 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  g pointed to by 
2cad5 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65  the third parame
2cad6 74 65 72 2c 20 7a 53 69 67 6e 65 64 49 64 65 6e  ter, zSignedIden
2cad7 74 2c 0a 2a 2a 20 74 6f 20 74 68 65 20 73 70 65  t,.** to the spe
2cad8 63 69 66 69 65 64 20 6f 66 66 73 65 74 20 69 6e  cified offset in
2cad9 20 74 68 65 20 62 75 66 66 65 72 20 61 6e 64 20   the buffer and 
2cada 75 70 64 61 74 65 73 20 2a 70 49 64 78 20 74 6f  updates *pIdx to
2cadb 20 72 65 66 65 72 0a 2a 2a 20 74 6f 20 74 68 65   refer.** to the
2cadc 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65   first byte afte
2cadd 72 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 20  r the last byte 
2cade 77 72 69 74 74 65 6e 20 62 65 66 6f 72 65 20 72  written before r
2cadf 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 20 0a 2a 2a  eturning..** .**
2cae0 20 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 7a   If the string z
2cae1 53 69 67 6e 65 64 49 64 65 6e 74 20 63 6f 6e 73  SignedIdent cons
2cae2 69 73 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 66  ists entirely of
2cae3 20 61 6c 70 68 61 2d 6e 75 6d 65 72 69 63 0a 2a   alpha-numeric.*
2cae4 2a 20 63 68 61 72 61 63 74 65 72 73 2c 20 64 6f  * characters, do
2cae5 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74  es not begin wit
2cae6 68 20 61 20 64 69 67 69 74 20 61 6e 64 20 69 73  h a digit and is
2cae7 20 6e 6f 74 20 61 6e 20 53 51 4c 20 6b 65 79 77   not an SQL keyw
2cae8 6f 72 64 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20  ord,.** then it 
2cae9 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68 65  is copied to the
2caea 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 65   output buffer e
2caeb 78 61 63 74 6c 79 20 61 73 20 69 74 20 69 73 2e  xactly as it is.
2caec 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a 2a 20 69   Otherwise,.** i
2caed 74 20 69 73 20 71 75 6f 74 65 64 20 75 73 69 6e  t is quoted usin
2caee 67 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2e  g double-quotes.
2caef 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2caf0 69 64 65 6e 74 50 75 74 28 63 68 61 72 20 2a 7a  identPut(char *z
2caf1 2c 20 69 6e 74 20 2a 70 49 64 78 2c 20 63 68 61  , int *pIdx, cha
2caf2 72 20 2a 7a 53 69 67 6e 65 64 49 64 65 6e 74 29  r *zSignedIdent)
2caf3 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  {.  unsigned cha
2caf4 72 20 2a 7a 49 64 65 6e 74 20 3d 20 28 75 6e 73  r *zIdent = (uns
2caf5 69 67 6e 65 64 20 63 68 61 72 2a 29 7a 53 69 67  igned char*)zSig
2caf6 6e 65 64 49 64 65 6e 74 3b 0a 20 20 69 6e 74 20  nedIdent;.  int 
2caf7 69 2c 20 6a 2c 20 6e 65 65 64 51 75 6f 74 65 3b  i, j, needQuote;
2caf8 0a 20 20 69 20 3d 20 2a 70 49 64 78 3b 0a 0a 20  .  i = *pIdx;.. 
2caf9 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64 65 6e 74   for(j=0; zIdent
2cafa 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69  [j]; j++){.    i
2cafb 66 28 20 21 73 71 6c 69 74 65 33 49 73 61 6c 6e  f( !sqlite3Isaln
2cafc 75 6d 28 7a 49 64 65 6e 74 5b 6a 5d 29 20 26 26  um(zIdent[j]) &&
2cafd 20 7a 49 64 65 6e 74 5b 6a 5d 21 3d 27 5f 27 20   zIdent[j]!='_' 
2cafe 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 6e  ) break;.  }.  n
2caff 65 65 64 51 75 6f 74 65 20 3d 20 73 71 6c 69 74  eedQuote = sqlit
2cb00 65 33 49 73 64 69 67 69 74 28 7a 49 64 65 6e 74  e3Isdigit(zIdent
2cb01 5b 30 5d 29 20 7c 7c 20 73 71 6c 69 74 65 33 4b  [0]) || sqlite3K
2cb02 65 79 77 6f 72 64 43 6f 64 65 28 7a 49 64 65 6e  eywordCode(zIden
2cb03 74 2c 20 6a 29 21 3d 54 4b 5f 49 44 3b 0a 20 20  t, j)!=TK_ID;.  
2cb04 69 66 28 20 21 6e 65 65 64 51 75 6f 74 65 20 29  if( !needQuote )
2cb05 7b 0a 20 20 20 20 6e 65 65 64 51 75 6f 74 65 20  {.    needQuote 
2cb06 3d 20 7a 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 7d  = zIdent[j];.  }
2cb07 0a 0a 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74  ..  if( needQuot
2cb08 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27  e ) z[i++] = '"'
2cb09 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64  ;.  for(j=0; zId
2cb0a 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20  ent[j]; j++){.  
2cb0b 20 20 7a 5b 69 2b 2b 5d 20 3d 20 7a 49 64 65 6e    z[i++] = zIden
2cb0c 74 5b 6a 5d 3b 0a 20 20 20 20 69 66 28 20 7a 49  t[j];.    if( zI
2cb0d 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27 20 29 20 7a  dent[j]=='"' ) z
2cb0e 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 7d  [i++] = '"';.  }
2cb0f 0a 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65  .  if( needQuote
2cb10 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b   ) z[i++] = '"';
2cb11 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a  .  z[i] = 0;.  *
2cb12 70 49 64 78 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a  pIdx = i;.}../*.
2cb13 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 20 43 52  ** Generate a CR
2cb14 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
2cb15 6d 65 6e 74 20 61 70 70 72 6f 70 72 69 61 74 65  ment appropriate
2cb16 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 0a 2a   for the given.*
2cb17 2a 20 74 61 62 6c 65 2e 20 20 4d 65 6d 6f 72 79  * table.  Memory
2cb18 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 74 65 78   to hold the tex
2cb19 74 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65  t of the stateme
2cb1a 6e 74 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a  nt is obtained.*
2cb1b 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c  * from sqliteMal
2cb1c 6c 6f 63 28 29 20 61 6e 64 20 6d 75 73 74 20 62  loc() and must b
2cb1d 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63  e freed by the c
2cb1e 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e  alling function.
2cb1f 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
2cb20 2a 63 72 65 61 74 65 54 61 62 6c 65 53 74 6d 74  *createTableStmt
2cb21 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61  (sqlite3 *db, Ta
2cb22 62 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  ble *p){.  int i
2cb23 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68 61 72 20 2a  , k, n;.  char *
2cb24 7a 53 74 6d 74 3b 0a 20 20 63 68 61 72 20 2a 7a  zStmt;.  char *z
2cb25 53 65 70 2c 20 2a 7a 53 65 70 32 2c 20 2a 7a 45  Sep, *zSep2, *zE
2cb26 6e 64 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43  nd;.  Column *pC
2cb27 6f 6c 3b 0a 20 20 6e 20 3d 20 30 3b 0a 20 20 66  ol;.  n = 0;.  f
2cb28 6f 72 28 70 43 6f 6c 20 3d 20 70 2d 3e 61 43 6f  or(pCol = p->aCo
2cb29 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f  l, i=0; i<p->nCo
2cb2a 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b  l; i++, pCol++){
2cb2b 0a 20 20 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c  .    n += identL
2cb2c 65 6e 67 74 68 28 70 43 6f 6c 2d 3e 7a 4e 61 6d  ength(pCol->zNam
2cb2d 65 29 20 2b 20 35 3b 0a 20 20 7d 0a 20 20 6e 20  e) + 5;.  }.  n 
2cb2e 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 74 68 28 70  += identLength(p
2cb2f 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  ->zName);.  if( 
2cb30 6e 3c 35 30 20 29 7b 20 0a 20 20 20 20 7a 53 65  n<50 ){ .    zSe
2cb31 70 20 3d 20 22 22 3b 0a 20 20 20 20 7a 53 65 70  p = "";.    zSep
2cb32 32 20 3d 20 22 2c 22 3b 0a 20 20 20 20 7a 45 6e  2 = ",";.    zEn
2cb33 64 20 3d 20 22 29 22 3b 0a 20 20 7d 65 6c 73 65  d = ")";.  }else
2cb34 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 5c 6e  {.    zSep = "\n
2cb35 20 20 22 3b 0a 20 20 20 20 7a 53 65 70 32 20 3d    ";.    zSep2 =
2cb36 20 22 2c 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 45   ",\n  ";.    zE
2cb37 6e 64 20 3d 20 22 5c 6e 29 22 3b 0a 20 20 7d 0a  nd = "\n)";.  }.
2cb38 20 20 6e 20 2b 3d 20 33 35 20 2b 20 36 2a 70 2d    n += 35 + 6*p-
2cb39 3e 6e 43 6f 6c 3b 0a 20 20 7a 53 74 6d 74 20 3d  >nCol;.  zStmt =
2cb3a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2cb3b 52 61 77 28 30 2c 20 6e 29 3b 0a 20 20 69 66 28  Raw(0, n);.  if(
2cb3c 20 7a 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20   zStmt==0 ){.   
2cb3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2cb3e 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72  d = 1;.    retur
2cb3f 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  n 0;.  }.  sqlit
2cb40 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2c 20 7a  e3_snprintf(n, z
2cb41 53 74 6d 74 2c 20 22 43 52 45 41 54 45 20 54 41  Stmt, "CREATE TA
2cb42 42 4c 45 20 22 29 3b 0a 20 20 6b 20 3d 20 73 71  BLE ");.  k = sq
2cb43 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 53  lite3Strlen30(zS
2cb44 74 6d 74 29 3b 0a 20 20 69 64 65 6e 74 50 75 74  tmt);.  identPut
2cb45 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 2d 3e 7a  (zStmt, &k, p->z
2cb46 4e 61 6d 65 29 3b 0a 20 20 7a 53 74 6d 74 5b 6b  Name);.  zStmt[k
2cb47 2b 2b 5d 20 3d 20 27 28 27 3b 0a 20 20 66 6f 72  ++] = '(';.  for
2cb48 28 70 43 6f 6c 3d 70 2d 3e 61 43 6f 6c 2c 20 69  (pCol=p->aCol, i
2cb49 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69  =0; i<p->nCol; i
2cb4a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20  ++, pCol++){.   
2cb4b 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
2cb4c 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a 54 79 70  ar * const azTyp
2cb4d 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20  e[] = {.        
2cb4e 2f 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45  /* SQLITE_AFF_TE
2cb4f 58 54 20 20 20 20 2a 2f 20 22 20 54 45 58 54 22  XT    */ " TEXT"
2cb50 2c 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  ,.        /* SQL
2cb51 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20  ITE_AFF_NONE    
2cb52 2a 2f 20 22 22 2c 0a 20 20 20 20 20 20 20 20 2f  */ "",.        /
2cb53 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  * SQLITE_AFF_NUM
2cb54 45 52 49 43 20 2a 2f 20 22 20 4e 55 4d 22 2c 0a  ERIC */ " NUM",.
2cb55 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54          /* SQLIT
2cb56 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 2a 2f  E_AFF_INTEGER */
2cb57 20 22 20 49 4e 54 22 2c 0a 20 20 20 20 20 20 20   " INT",.       
2cb58 20 2f 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 52   /* SQLITE_AFF_R
2cb59 45 41 4c 20 20 20 20 2a 2f 20 22 20 52 45 41 4c  EAL    */ " REAL
2cb5a 22 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74  ".    };.    int
2cb5b 20 6c 65 6e 3b 0a 20 20 20 20 63 6f 6e 73 74 20   len;.    const 
2cb5c 63 68 61 72 20 2a 7a 54 79 70 65 3b 0a 0a 20 20  char *zType;..  
2cb5d 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
2cb5e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b  tf(n-k, &zStmt[k
2cb5f 5d 2c 20 7a 53 65 70 29 3b 0a 20 20 20 20 6b 20  ], zSep);.    k 
2cb60 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  += sqlite3Strlen
2cb61 33 30 28 26 7a 53 74 6d 74 5b 6b 5d 29 3b 0a 20  30(&zStmt[k]);. 
2cb62 20 20 20 7a 53 65 70 20 3d 20 7a 53 65 70 32 3b     zSep = zSep2;
2cb63 0a 20 20 20 20 69 64 65 6e 74 50 75 74 28 7a 53  .    identPut(zS
2cb64 74 6d 74 2c 20 26 6b 2c 20 70 43 6f 6c 2d 3e 7a  tmt, &k, pCol->z
2cb65 4e 61 6d 65 29 3b 0a 20 20 20 20 61 73 73 65 72  Name);.    asser
2cb66 74 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74  t( pCol->affinit
2cb67 79 2d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  y-SQLITE_AFF_TEX
2cb68 54 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 61 73  T >= 0 );.    as
2cb69 73 65 72 74 28 20 70 43 6f 6c 2d 3e 61 66 66 69  sert( pCol->affi
2cb6a 6e 69 74 79 2d 53 51 4c 49 54 45 5f 41 46 46 5f  nity-SQLITE_AFF_
2cb6b 54 45 58 54 20 3c 20 41 72 72 61 79 53 69 7a 65  TEXT < ArraySize
2cb6c 28 61 7a 54 79 70 65 29 20 29 3b 0a 20 20 20 20  (azType) );.    
2cb6d 74 65 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e  testcase( pCol->
2cb6e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45  affinity==SQLITE
2cb6f 5f 41 46 46 5f 54 45 58 54 20 29 3b 0a 20 20 20  _AFF_TEXT );.   
2cb70 20 74 65 73 74 63 61 73 65 28 20 70 43 6f 6c 2d   testcase( pCol-
2cb71 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54  >affinity==SQLIT
2cb72 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 3b 0a 20 20  E_AFF_NONE );.  
2cb73 20 20 74 65 73 74 63 61 73 65 28 20 70 43 6f 6c    testcase( pCol
2cb74 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49  ->affinity==SQLI
2cb75 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29  TE_AFF_NUMERIC )
2cb76 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2cb77 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d  pCol->affinity==
2cb78 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
2cb79 45 52 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  ER );.    testca
2cb7a 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  se( pCol->affini
2cb7b 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52  ty==SQLITE_AFF_R
2cb7c 45 41 4c 20 29 3b 0a 20 20 20 20 0a 20 20 20 20  EAL );.    .    
2cb7d 7a 54 79 70 65 20 3d 20 61 7a 54 79 70 65 5b 70  zType = azType[p
2cb7e 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 2d 20  Col->affinity - 
2cb7f 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 5d  SQLITE_AFF_TEXT]
2cb80 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69  ;.    len = sqli
2cb81 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 79 70  te3Strlen30(zTyp
2cb82 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  e);.    assert( 
2cb83 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d  pCol->affinity==
2cb84 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20  SQLITE_AFF_NONE 
2cb85 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
2cb86 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d  pCol->affinity==
2cb87 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54  sqlite3AffinityT
2cb88 79 70 65 28 7a 54 79 70 65 29 20 29 3b 0a 20 20  ype(zType) );.  
2cb89 20 20 6d 65 6d 63 70 79 28 26 7a 53 74 6d 74 5b    memcpy(&zStmt[
2cb8a 6b 5d 2c 20 7a 54 79 70 65 2c 20 6c 65 6e 29 3b  k], zType, len);
2cb8b 0a 20 20 20 20 6b 20 2b 3d 20 6c 65 6e 3b 0a 20  .    k += len;. 
2cb8c 20 20 20 61 73 73 65 72 74 28 20 6b 3c 3d 6e 20     assert( k<=n 
2cb8d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
2cb8e 5f 73 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26  _snprintf(n-k, &
2cb8f 7a 53 74 6d 74 5b 6b 5d 2c 20 22 25 73 22 2c 20  zStmt[k], "%s", 
2cb90 7a 45 6e 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  zEnd);.  return 
2cb91 7a 53 74 6d 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  zStmt;.}../*.** 
2cb92 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
2cb93 63 61 6c 6c 65 64 20 74 6f 20 72 65 70 6f 72 74  called to report
2cb94 20 74 68 65 20 66 69 6e 61 6c 20 22 29 22 20 74   the final ")" t
2cb95 68 61 74 20 74 65 72 6d 69 6e 61 74 65 73 0a 2a  hat terminates.*
2cb96 2a 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  * a CREATE TABLE
2cb97 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a   statement..**.*
2cb98 2a 20 54 68 65 20 74 61 62 6c 65 20 73 74 72 75  * The table stru
2cb99 63 74 75 72 65 20 74 68 61 74 20 6f 74 68 65 72  cture that other
2cb9a 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73   action routines
2cb9b 20 68 61 76 65 20 62 65 65 6e 20 62 75 69 6c 64   have been build
2cb9c 69 6e 67 0a 2a 2a 20 69 73 20 61 64 64 65 64 20  ing.** is added 
2cb9d 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  to the internal 
2cb9e 68 61 73 68 20 74 61 62 6c 65 73 2c 20 61 73 73  hash tables, ass
2cb9f 75 6d 69 6e 67 20 6e 6f 20 65 72 72 6f 72 73 20  uming no errors 
2cba0 68 61 76 65 0a 2a 2a 20 6f 63 63 75 72 72 65 64  have.** occurred
2cba1 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 6e 74 72 79  ..**.** An entry
2cba2 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 69   for the table i
2cba3 73 20 6d 61 64 65 20 69 6e 20 74 68 65 20 6d 61  s made in the ma
2cba4 73 74 65 72 20 74 61 62 6c 65 20 6f 6e 20 64 69  ster table on di
2cba5 73 6b 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20 74 68  sk, unless.** th
2cba6 69 73 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72  is is a temporar
2cba7 79 20 74 61 62 6c 65 20 6f 72 20 64 62 2d 3e 69  y table or db->i
2cba8 6e 69 74 2e 62 75 73 79 3d 3d 31 2e 20 20 57 68  nit.busy==1.  Wh
2cba9 65 6e 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  en db->init.busy
2cbaa 3d 3d 31 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 20  ==1.** it means 
2cbab 77 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 74  we are reading t
2cbac 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
2cbad 20 74 61 62 6c 65 20 62 65 63 61 75 73 65 20 77   table because w
2cbae 65 20 6a 75 73 74 0a 2a 2a 20 63 6f 6e 6e 65 63  e just.** connec
2cbaf 74 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62  ted to the datab
2cbb0 61 73 65 20 6f 72 20 62 65 63 61 75 73 65 20 74  ase or because t
2cbb1 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
2cbb2 20 74 61 62 6c 65 20 68 61 73 0a 2a 2a 20 72 65   table has.** re
2cbb3 63 65 6e 74 6c 79 20 63 68 61 6e 67 65 64 2c 20  cently changed, 
2cbb4 73 6f 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72  so the entry for
2cbb5 20 74 68 69 73 20 74 61 62 6c 65 20 61 6c 72 65   this table alre
2cbb6 61 64 79 20 65 78 69 73 74 73 20 69 6e 0a 2a 2a  ady exists in.**
2cbb7 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
2cbb8 65 72 20 74 61 62 6c 65 2e 20 20 57 65 20 64 6f  er table.  We do
2cbb9 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20 63 72 65   not want to cre
2cbba 61 74 65 20 69 74 20 61 67 61 69 6e 2e 0a 2a 2a  ate it again..**
2cbbb 0a 2a 2a 20 49 66 20 74 68 65 20 70 53 65 6c 65  .** If the pSele
2cbbc 63 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e  ct argument is n
2cbbd 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e  ot NULL, it mean
2cbbe 73 20 74 68 61 74 20 74 68 69 73 20 72 6f 75 74  s that this rout
2cbbf 69 6e 65 0a 2a 2a 20 77 61 73 20 63 61 6c 6c 65  ine.** was calle
2cbc0 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 74 61  d to create a ta
2cbc1 62 6c 65 20 67 65 6e 65 72 61 74 65 64 20 66 72  ble generated fr
2cbc2 6f 6d 20 61 20 0a 2a 2a 20 22 43 52 45 41 54 45  om a .** "CREATE
2cbc3 20 54 41 42 4c 45 20 2e 2e 2e 20 41 53 20 53 45   TABLE ... AS SE
2cbc4 4c 45 43 54 20 2e 2e 2e 22 20 73 74 61 74 65 6d  LECT ..." statem
2cbc5 65 6e 74 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e  ent.  The column
2cbc6 20 6e 61 6d 65 73 20 6f 66 0a 2a 2a 20 74 68 65   names of.** the
2cbc7 20 6e 65 77 20 74 61 62 6c 65 20 77 69 6c 6c 20   new table will 
2cbc8 6d 61 74 63 68 20 74 68 65 20 72 65 73 75 6c 74  match the result
2cbc9 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45   set of the SELE
2cbca 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  CT..*/.SQLITE_PR
2cbcb 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2cbcc 65 33 45 6e 64 54 61 62 6c 65 28 0a 20 20 50 61  e3EndTable(.  Pa
2cbcd 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2cbce 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63        /* Parse c
2cbcf 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65  ontext */.  Toke
2cbd0 6e 20 2a 70 43 6f 6e 73 2c 20 20 20 20 20 20 20  n *pCons,       
2cbd1 20 20 20 20 2f 2a 20 54 68 65 20 27 2c 27 20 74      /* The ',' t
2cbd2 6f 6b 65 6e 20 61 66 74 65 72 20 74 68 65 20 6c  oken after the l
2cbd3 61 73 74 20 63 6f 6c 75 6d 6e 20 64 65 66 6e 2e  ast column defn.
2cbd4 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 45 6e   */.  Token *pEn
2cbd5 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  d,            /*
2cbd6 20 54 68 65 20 66 69 6e 61 6c 20 27 29 27 20 74   The final ')' t
2cbd7 6f 6b 65 6e 20 69 6e 20 74 68 65 20 43 52 45 41  oken in the CREA
2cbd8 54 45 20 54 41 42 4c 45 20 2a 2f 0a 20 20 53 65  TE TABLE */.  Se
2cbd9 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20 20  lect *pSelect   
2cbda 20 20 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20        /* Select 
2cbdb 66 72 6f 6d 20 61 20 22 43 52 45 41 54 45 20 2e  from a "CREATE .
2cbdc 2e 2e 20 41 53 20 53 45 4c 45 43 54 22 20 2a 2f  .. AS SELECT" */
2cbdd 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a  .){.  Table *p;.
2cbde 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2cbdf 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e  pParse->db;.  in
2cbe0 74 20 69 44 62 3b 0a 0a 20 20 69 66 28 20 28 70  t iDb;..  if( (p
2cbe1 45 6e 64 3d 3d 30 20 26 26 20 70 53 65 6c 65 63  End==0 && pSelec
2cbe2 74 3d 3d 30 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c  t==0) || db->mal
2cbe3 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
2cbe4 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70   return;.  }.  p
2cbe5 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
2cbe6 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30  able;.  if( p==0
2cbe7 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73   ) return;..  as
2cbe8 73 65 72 74 28 20 21 64 62 2d 3e 69 6e 69 74 2e  sert( !db->init.
2cbe9 62 75 73 79 20 7c 7c 20 21 70 53 65 6c 65 63 74  busy || !pSelect
2cbea 20 29 3b 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c   );..  iDb = sql
2cbeb 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
2cbec 78 28 64 62 2c 20 70 2d 3e 70 53 63 68 65 6d 61  x(db, p->pSchema
2cbed 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
2cbee 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20  TE_OMIT_CHECK.  
2cbef 2f 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73  /* Resolve names
2cbf0 20 69 6e 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f   in all CHECK co
2cbf1 6e 73 74 72 61 69 6e 74 20 65 78 70 72 65 73 73  nstraint express
2cbf2 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ions..  */.  if(
2cbf3 20 70 2d 3e 70 43 68 65 63 6b 20 29 7b 0a 20 20   p->pCheck ){.  
2cbf4 20 20 53 72 63 4c 69 73 74 20 73 53 72 63 3b 20    SrcList sSrc; 
2cbf5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cbf6 20 20 2f 2a 20 46 61 6b 65 20 53 72 63 4c 69 73    /* Fake SrcLis
2cbf7 74 20 66 6f 72 20 70 50 61 72 73 65 2d 3e 70 4e  t for pParse->pN
2cbf8 65 77 54 61 62 6c 65 20 2a 2f 0a 20 20 20 20 4e  ewTable */.    N
2cbf9 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20  ameContext sNC; 
2cbfa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2cbfb 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66  * Name context f
2cbfc 6f 72 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  or pParse->pNewT
2cbfd 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70 72  able */.    Expr
2cbfe 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20  List *pList;    
2cbff 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2cc00 69 73 74 20 6f 66 20 61 6c 6c 20 43 48 45 43 4b  ist of all CHECK
2cc01 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
2cc02 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20      int i;      
2cc03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cc04 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2cc05 74 65 72 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73  ter */..    mems
2cc06 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65  et(&sNC, 0, size
2cc07 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 6d 65  of(sNC));.    me
2cc08 6d 73 65 74 28 26 73 53 72 63 2c 20 30 2c 20 73  mset(&sSrc, 0, s
2cc09 69 7a 65 6f 66 28 73 53 72 63 29 29 3b 0a 20 20  izeof(sSrc));.  
2cc0a 20 20 73 53 72 63 2e 6e 53 72 63 20 3d 20 31 3b    sSrc.nSrc = 1;
2cc0b 0a 20 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 7a  .    sSrc.a[0].z
2cc0c 4e 61 6d 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b  Name = p->zName;
2cc0d 0a 20 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 70  .    sSrc.a[0].p
2cc0e 54 61 62 20 3d 20 70 3b 0a 20 20 20 20 73 53 72  Tab = p;.    sSr
2cc0f 63 2e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d  c.a[0].iCursor =
2cc10 20 2d 31 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61   -1;.    sNC.pPa
2cc11 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
2cc12 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d    sNC.pSrcList =
2cc13 20 26 73 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e   &sSrc;.    sNC.
2cc14 6e 63 46 6c 61 67 73 20 3d 20 4e 43 5f 49 73 43  ncFlags = NC_IsC
2cc15 68 65 63 6b 3b 0a 20 20 20 20 70 4c 69 73 74 20  heck;.    pList 
2cc16 3d 20 70 2d 3e 70 43 68 65 63 6b 3b 0a 20 20 20  = p->pCheck;.   
2cc17 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73   for(i=0; i<pLis
2cc18 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
2cc19 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
2cc1a 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
2cc1b 73 28 26 73 4e 43 2c 20 70 4c 69 73 74 2d 3e 61  s(&sNC, pList->a
2cc1c 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20  [i].pExpr) ){.  
2cc1d 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
2cc1e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
2cc1f 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
2cc20 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ed(SQLITE_OMIT_C
2cc21 48 45 43 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49  HECK) */..  /* I
2cc22 66 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62  f the db->init.b
2cc23 75 73 79 20 69 73 20 31 20 69 74 20 6d 65 61 6e  usy is 1 it mean
2cc24 73 20 77 65 20 61 72 65 20 72 65 61 64 69 6e 67  s we are reading
2cc25 20 74 68 65 20 53 51 4c 20 6f 66 66 20 74 68 65   the SQL off the
2cc26 0a 20 20 2a 2a 20 22 73 71 6c 69 74 65 5f 6d 61  .  ** "sqlite_ma
2cc27 73 74 65 72 22 20 6f 72 20 22 73 71 6c 69 74 65  ster" or "sqlite
2cc28 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 20 74 61  _temp_master" ta
2cc29 62 6c 65 20 6f 6e 20 74 68 65 20 64 69 73 6b 2e  ble on the disk.
2cc2a 0a 20 20 2a 2a 20 53 6f 20 64 6f 20 6e 6f 74 20  .  ** So do not 
2cc2b 77 72 69 74 65 20 74 6f 20 74 68 65 20 64 69 73  write to the dis
2cc2c 6b 20 61 67 61 69 6e 2e 20 20 45 78 74 72 61 63  k again.  Extrac
2cc2d 74 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  t the root page 
2cc2e 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20  number.  ** for 
2cc2f 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 74  the table from t
2cc30 68 65 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54  he db->init.newT
2cc31 6e 75 6d 20 66 69 65 6c 64 2e 20 20 28 54 68 65  num field.  (The
2cc32 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a   page number.  *
2cc33 2a 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65  * should have be
2cc34 65 6e 20 70 75 74 20 74 68 65 72 65 20 62 79 20  en put there by 
2cc35 74 68 65 20 73 71 6c 69 74 65 4f 70 65 6e 43 62  the sqliteOpenCb
2cc36 20 72 6f 75 74 69 6e 65 2e 29 0a 20 20 2a 2f 0a   routine.).  */.
2cc37 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62    if( db->init.b
2cc38 75 73 79 20 29 7b 0a 20 20 20 20 70 2d 3e 74 6e  usy ){.    p->tn
2cc39 75 6d 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65  um = db->init.ne
2cc3a 77 54 6e 75 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  wTnum;.  }..  /*
2cc3b 20 49 66 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69   If not initiali
2cc3c 7a 69 6e 67 2c 20 74 68 65 6e 20 63 72 65 61 74  zing, then creat
2cc3d 65 20 61 20 72 65 63 6f 72 64 20 66 6f 72 20 74  e a record for t
2cc3e 68 65 20 6e 65 77 20 74 61 62 6c 65 0a 20 20 2a  he new table.  *
2cc3f 2a 20 69 6e 20 74 68 65 20 53 51 4c 49 54 45 5f  * in the SQLITE_
2cc40 4d 41 53 54 45 52 20 74 61 62 6c 65 20 6f 66 20  MASTER table of 
2cc41 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20  the database..  
2cc42 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20  **.  ** If this 
2cc43 69 73 20 61 20 54 45 4d 50 4f 52 41 52 59 20 74  is a TEMPORARY t
2cc44 61 62 6c 65 2c 20 77 72 69 74 65 20 74 68 65 20  able, write the 
2cc45 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 61  entry into the a
2cc46 75 78 69 6c 69 61 72 79 0a 20 20 2a 2a 20 66 69  uxiliary.  ** fi
2cc47 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e  le instead of in
2cc48 74 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  to the main data
2cc49 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a  base file..  */.
2cc4a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
2cc4b 62 75 73 79 20 29 7b 0a 20 20 20 20 69 6e 74 20  busy ){.    int 
2cc4c 6e 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a  n;.    Vdbe *v;.
2cc4d 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b      char *zType;
2cc4e 20 20 20 20 2f 2a 20 22 76 69 65 77 22 20 6f 72      /* "view" or
2cc4f 20 22 74 61 62 6c 65 22 20 2a 2f 0a 20 20 20 20   "table" */.    
2cc50 63 68 61 72 20 2a 7a 54 79 70 65 32 3b 20 20 20  char *zType2;   
2cc51 2f 2a 20 22 56 49 45 57 22 20 6f 72 20 22 54 41  /* "VIEW" or "TA
2cc52 42 4c 45 22 20 2a 2f 0a 20 20 20 20 63 68 61 72  BLE" */.    char
2cc53 20 2a 7a 53 74 6d 74 3b 20 20 20 20 2f 2a 20 54   *zStmt;    /* T
2cc54 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54  ext of the CREAT
2cc55 45 20 54 41 42 4c 45 20 6f 72 20 43 52 45 41 54  E TABLE or CREAT
2cc56 45 20 56 49 45 57 20 73 74 61 74 65 6d 65 6e 74  E VIEW statement
2cc57 20 2a 2f 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c   */..    v = sql
2cc58 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2cc59 73 65 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56  se);.    if( NEV
2cc5a 45 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72  ER(v==0) ) retur
2cc5b 6e 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56  n;..    sqlite3V
2cc5c 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2cc5d 43 6c 6f 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20  Close, 0);..    
2cc5e 2f 2a 20 0a 20 20 20 20 2a 2a 20 49 6e 69 74 69  /* .    ** Initi
2cc5f 61 6c 69 7a 65 20 7a 54 79 70 65 20 66 6f 72 20  alize zType for 
2cc60 74 68 65 20 6e 65 77 20 76 69 65 77 20 6f 72 20  the new view or 
2cc61 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  table..    */.  
2cc62 20 20 69 66 28 20 70 2d 3e 70 53 65 6c 65 63 74    if( p->pSelect
2cc63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
2cc64 41 20 72 65 67 75 6c 61 72 20 74 61 62 6c 65 20  A regular table 
2cc65 2a 2f 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d  */.      zType =
2cc66 20 22 74 61 62 6c 65 22 3b 0a 20 20 20 20 20 20   "table";.      
2cc67 7a 54 79 70 65 32 20 3d 20 22 54 41 42 4c 45 22  zType2 = "TABLE"
2cc68 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
2cc69 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 7d  _OMIT_VIEW.    }
2cc6a 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41  else{.      /* A
2cc6b 20 76 69 65 77 20 2a 2f 0a 20 20 20 20 20 20 7a   view */.      z
2cc6c 54 79 70 65 20 3d 20 22 76 69 65 77 22 3b 0a 20  Type = "view";. 
2cc6d 20 20 20 20 20 7a 54 79 70 65 32 20 3d 20 22 56       zType2 = "V
2cc6e 49 45 57 22 3b 0a 23 65 6e 64 69 66 0a 20 20 20  IEW";.#endif.   
2cc6f 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   }..    /* If th
2cc70 69 73 20 69 73 20 61 20 43 52 45 41 54 45 20 54  is is a CREATE T
2cc71 41 42 4c 45 20 78 78 20 41 53 20 53 45 4c 45 43  ABLE xx AS SELEC
2cc72 54 20 2e 2e 2e 2c 20 65 78 65 63 75 74 65 20 74  T ..., execute t
2cc73 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a  he SELECT.    **
2cc74 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 70 6f   statement to po
2cc75 70 75 6c 61 74 65 20 74 68 65 20 6e 65 77 20 74  pulate the new t
2cc76 61 62 6c 65 2e 20 54 68 65 20 72 6f 6f 74 2d 70  able. The root-p
2cc77 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  age number for t
2cc78 68 65 0a 20 20 20 20 2a 2a 20 6e 65 77 20 74 61  he.    ** new ta
2cc79 62 6c 65 20 69 73 20 69 6e 20 72 65 67 69 73 74  ble is in regist
2cc7a 65 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f  er pParse->regRo
2cc7b 6f 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ot..    **.    *
2cc7c 2a 20 4f 6e 63 65 20 74 68 65 20 53 45 4c 45 43  * Once the SELEC
2cc7d 54 20 68 61 73 20 62 65 65 6e 20 63 6f 64 65 64  T has been coded
2cc7e 20 62 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63   by sqlite3Selec
2cc7f 74 28 29 2c 20 69 74 20 69 73 20 69 6e 20 61 0a  t(), it is in a.
2cc80 20 20 20 20 2a 2a 20 73 75 69 74 61 62 6c 65 20      ** suitable 
2cc81 73 74 61 74 65 20 74 6f 20 71 75 65 72 79 20 66  state to query f
2cc82 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  or the column na
2cc83 6d 65 73 20 61 6e 64 20 74 79 70 65 73 20 74 6f  mes and types to
2cc84 20 62 65 20 75 73 65 64 0a 20 20 20 20 2a 2a 20   be used.    ** 
2cc85 62 79 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  by the new table
2cc86 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
2cc87 41 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 77  A shared-cache w
2cc88 72 69 74 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74  rite-lock is not
2cc89 20 72 65 71 75 69 72 65 64 20 74 6f 20 77 72 69   required to wri
2cc8a 74 65 20 74 6f 20 74 68 65 20 6e 65 77 20 74 61  te to the new ta
2cc8b 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 61 73 20 61  ble,.    ** as a
2cc8c 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 20 6d 75 73   schema-lock mus
2cc8d 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  t have already b
2cc8e 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 74 6f 20  een obtained to 
2cc8f 63 72 65 61 74 65 20 69 74 2e 20 53 69 6e 63 65  create it. Since
2cc90 0a 20 20 20 20 2a 2a 20 61 20 73 63 68 65 6d 61  .    ** a schema
2cc91 2d 6c 6f 63 6b 20 65 78 63 6c 75 64 65 73 20 61  -lock excludes a
2cc92 6c 6c 20 6f 74 68 65 72 20 64 61 74 61 62 61 73  ll other databas
2cc93 65 20 75 73 65 72 73 2c 20 74 68 65 20 77 72 69  e users, the wri
2cc94 74 65 2d 6c 6f 63 6b 20 77 6f 75 6c 64 0a 20 20  te-lock would.  
2cc95 20 20 2a 2a 20 62 65 20 72 65 64 75 6e 64 61 6e    ** be redundan
2cc96 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  t..    */.    if
2cc97 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ( pSelect ){.   
2cc98 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65     SelectDest de
2cc99 73 74 3b 0a 20 20 20 20 20 20 54 61 62 6c 65 20  st;.      Table 
2cc9a 2a 70 53 65 6c 54 61 62 3b 0a 0a 20 20 20 20 20  *pSelTab;..     
2cc9b 20 61 73 73 65 72 74 28 70 50 61 72 73 65 2d 3e   assert(pParse->
2cc9c 6e 54 61 62 3d 3d 31 29 3b 0a 20 20 20 20 20 20  nTab==1);.      
2cc9d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2cc9e 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  3(v, OP_OpenWrit
2cc9f 65 2c 20 31 2c 20 70 50 61 72 73 65 2d 3e 72 65  e, 1, pParse->re
2cca0 67 52 6f 6f 74 2c 20 69 44 62 29 3b 0a 20 20 20  gRoot, iDb);.   
2cca1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2cca2 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
2cca3 5f 50 32 49 53 52 45 47 29 3b 0a 20 20 20 20 20  _P2ISREG);.     
2cca4 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20   pParse->nTab = 
2cca5 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  2;.      sqlite3
2cca6 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26  SelectDestInit(&
2cca7 64 65 73 74 2c 20 53 52 54 5f 54 61 62 6c 65 2c  dest, SRT_Table,
2cca8 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
2cca9 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
2ccaa 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74 29   pSelect, &dest)
2ccab 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2ccac 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2ccad 43 6c 6f 73 65 2c 20 31 29 3b 0a 20 20 20 20 20  Close, 1);.     
2ccae 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
2ccaf 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  r==0 ){.        
2ccb0 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65  pSelTab = sqlite
2ccb1 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65  3ResultSetOfSele
2ccb2 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  ct(pParse, pSele
2ccb3 63 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ct);.        if(
2ccb4 20 70 53 65 6c 54 61 62 3d 3d 30 20 29 20 72 65   pSelTab==0 ) re
2ccb5 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 61 73  turn;.        as
2ccb6 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 3d 3d 30  sert( p->aCol==0
2ccb7 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e   );.        p->n
2ccb8 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e 6e  Col = pSelTab->n
2ccb9 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  Col;.        p->
2ccba 61 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e  aCol = pSelTab->
2ccbb 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 53  aCol;.        pS
2ccbc 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30 3b  elTab->nCol = 0;
2ccbd 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54 61 62  .        pSelTab
2ccbe 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  ->aCol = 0;.    
2ccbf 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
2ccc0 65 54 61 62 6c 65 28 64 62 2c 20 70 53 65 6c 54  eTable(db, pSelT
2ccc1 61 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ab);.      }.   
2ccc2 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75   }..    /* Compu
2ccc3 74 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  te the complete 
2ccc4 74 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41  text of the CREA
2ccc5 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  TE statement */.
2ccc6 20 20 20 20 69 66 28 20 70 53 65 6c 65 63 74 20      if( pSelect 
2ccc7 29 7b 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d  ){.      zStmt =
2ccc8 20 63 72 65 61 74 65 54 61 62 6c 65 53 74 6d 74   createTableStmt
2ccc9 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d 65 6c  (db, p);.    }el
2ccca 73 65 7b 0a 20 20 20 20 20 20 6e 20 3d 20 28 69  se{.      n = (i
2cccb 6e 74 29 28 70 45 6e 64 2d 3e 7a 20 2d 20 70 50  nt)(pEnd->z - pP
2cccc 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e  arse->sNameToken
2cccd 2e 7a 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 7a  .z) + 1;.      z
2ccce 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50  Stmt = sqlite3MP
2cccf 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20  rintf(db, .     
2ccd0 20 20 20 20 20 22 43 52 45 41 54 45 20 25 73 20       "CREATE %s 
2ccd1 25 2e 2a 73 22 2c 20 7a 54 79 70 65 32 2c 20 6e  %.*s", zType2, n
2ccd2 2c 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54  , pParse->sNameT
2ccd3 6f 6b 65 6e 2e 7a 0a 20 20 20 20 20 20 29 3b 0a  oken.z.      );.
2ccd4 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 20      }..    /* A 
2ccd5 73 6c 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63  slot for the rec
2ccd6 6f 72 64 20 68 61 73 20 61 6c 72 65 61 64 79 20  ord has already 
2ccd7 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69  been allocated i
2ccd8 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51  n the .    ** SQ
2ccd9 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c  LITE_MASTER tabl
2ccda 65 2e 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64  e.  We just need
2ccdb 20 74 6f 20 75 70 64 61 74 65 20 74 68 61 74 20   to update that 
2ccdc 73 6c 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20  slot with all.  
2ccdd 20 20 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61    ** the informa
2ccde 74 69 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65  tion we've colle
2ccdf 63 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  cted..    */.   
2cce0 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
2cce1 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  rse(pParse,.    
2cce2 20 20 22 55 50 44 41 54 45 20 25 51 2e 25 73 20    "UPDATE %Q.%s 
2cce3 22 0a 20 20 20 20 20 20 20 20 20 22 53 45 54 20  ".         "SET 
2cce4 74 79 70 65 3d 27 25 73 27 2c 20 6e 61 6d 65 3d  type='%s', name=
2cce5 25 51 2c 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 2c  %Q, tbl_name=%Q,
2cce6 20 72 6f 6f 74 70 61 67 65 3d 23 25 64 2c 20 73   rootpage=#%d, s
2cce7 71 6c 3d 25 51 20 22 0a 20 20 20 20 20 20 20 22  ql=%Q ".       "
2cce8 57 48 45 52 45 20 72 6f 77 69 64 3d 23 25 64 22  WHERE rowid=#%d"
2cce9 2c 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b  ,.      db->aDb[
2ccea 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45  iDb].zName, SCHE
2cceb 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a 20  MA_TABLE(iDb),. 
2ccec 20 20 20 20 20 7a 54 79 70 65 2c 0a 20 20 20 20       zType,.    
2cced 20 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20    p->zName,.    
2ccee 20 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20    p->zName,.    
2ccef 20 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f    pParse->regRoo
2ccf0 74 2c 0a 20 20 20 20 20 20 7a 53 74 6d 74 2c 0a  t,.      zStmt,.
2ccf1 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 65        pParse->re
2ccf2 67 52 6f 77 69 64 0a 20 20 20 20 29 3b 0a 20 20  gRowid.    );.  
2ccf3 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2ccf4 64 62 2c 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20  db, zStmt);.    
2ccf5 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f  sqlite3ChangeCoo
2ccf6 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29  kie(pParse, iDb)
2ccf7 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
2ccf8 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
2ccf9 4d 45 4e 54 0a 20 20 20 20 2f 2a 20 43 68 65 63  MENT.    /* Chec
2ccfa 6b 20 74 6f 20 73 65 65 20 69 66 20 77 65 20 6e  k to see if we n
2ccfb 65 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 6e  eed to create an
2ccfc 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
2ccfd 20 74 61 62 6c 65 20 66 6f 72 0a 20 20 20 20 2a   table for.    *
2ccfe 2a 20 6b 65 65 70 69 6e 67 20 74 72 61 63 6b 20  * keeping track 
2ccff 6f 66 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  of autoincrement
2cd00 20 6b 65 79 73 2e 0a 20 20 20 20 2a 2f 0a 20 20   keys..    */.  
2cd01 20 20 69 66 28 20 70 2d 3e 74 61 62 46 6c 61 67    if( p->tabFlag
2cd02 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65  s & TF_Autoincre
2cd03 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 44 62  ment ){.      Db
2cd04 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62   *pDb = &db->aDb
2cd05 5b 69 44 62 5d 3b 0a 20 20 20 20 20 20 61 73 73  [iDb];.      ass
2cd06 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65  ert( sqlite3Sche
2cd07 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20  maMutexHeld(db, 
2cd08 69 44 62 2c 20 30 29 20 29 3b 0a 20 20 20 20 20  iDb, 0) );.     
2cd09 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d   if( pDb->pSchem
2cd0a 61 2d 3e 70 53 65 71 54 61 62 3d 3d 30 20 29 7b  a->pSeqTab==0 ){
2cd0b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2cd0c 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
2cd0d 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 22 43  se,.          "C
2cd0e 52 45 41 54 45 20 54 41 42 4c 45 20 25 51 2e 73  REATE TABLE %Q.s
2cd0f 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 28 6e  qlite_sequence(n
2cd10 61 6d 65 2c 73 65 71 29 22 2c 0a 20 20 20 20 20  ame,seq)",.     
2cd11 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 0a       pDb->zName.
2cd12 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
2cd13 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a   }.    }.#endif.
2cd14 0a 20 20 20 20 2f 2a 20 52 65 70 61 72 73 65 20  .    /* Reparse 
2cd15 65 76 65 72 79 74 68 69 6e 67 20 74 6f 20 75 70  everything to up
2cd16 64 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e 61  date our interna
2cd17 6c 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  l data structure
2cd18 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  s */.    sqlite3
2cd19 56 64 62 65 41 64 64 50 61 72 73 65 53 63 68 65  VdbeAddParseSche
2cd1a 6d 61 4f 70 28 76 2c 20 69 44 62 2c 0a 20 20 20  maOp(v, iDb,.   
2cd1b 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2cd1c 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
2cd1d 74 62 6c 5f 6e 61 6d 65 3d 27 25 71 27 22 2c 20  tbl_name='%q'", 
2cd1e 70 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 7d 0a  p->zName));.  }.
2cd1f 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 74  ..  /* Add the t
2cd20 61 62 6c 65 20 74 6f 20 74 68 65 20 69 6e 2d 6d  able to the in-m
2cd21 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61  emory representa
2cd22 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61  tion of the data
2cd23 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  base..  */.  if(
2cd24 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29   db->init.busy )
2cd25 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 4f 6c  {.    Table *pOl
2cd26 64 3b 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70  d;.    Schema *p
2cd27 53 63 68 65 6d 61 20 3d 20 70 2d 3e 70 53 63 68  Schema = p->pSch
2cd28 65 6d 61 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ema;.    assert(
2cd29 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d 75   sqlite3SchemaMu
2cd2a 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44 62 2c  texHeld(db, iDb,
2cd2b 20 30 29 20 29 3b 0a 20 20 20 20 70 4f 6c 64 20   0) );.    pOld 
2cd2c 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
2cd2d 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62  ert(&pSchema->tb
2cd2e 6c 48 61 73 68 2c 20 70 2d 3e 7a 4e 61 6d 65 2c  lHash, p->zName,
2cd2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2cd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2cd31 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 2d  lite3Strlen30(p-
2cd32 3e 7a 4e 61 6d 65 29 2c 70 29 3b 0a 20 20 20 20  >zName),p);.    
2cd33 69 66 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20  if( pOld ){.    
2cd34 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 4f 6c    assert( p==pOl
2cd35 64 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20  d );  /* Malloc 
2cd36 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64  must have failed
2cd37 20 69 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65   inside HashInse
2cd38 72 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 64 62  rt() */.      db
2cd39 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
2cd3a 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   1;.      return
2cd3b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72  ;.    }.    pPar
2cd3c 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20  se->pNewTable = 
2cd3d 30 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73  0;.    db->flags
2cd3e 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72   |= SQLITE_Inter
2cd3f 6e 43 68 61 6e 67 65 73 3b 0a 0a 23 69 66 6e 64  nChanges;..#ifnd
2cd40 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2cd41 4c 54 45 52 54 41 42 4c 45 0a 20 20 20 20 69 66  LTERTABLE.    if
2cd42 28 20 21 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b  ( !p->pSelect ){
2cd43 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
2cd44 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73  r *zName = (cons
2cd45 74 20 63 68 61 72 20 2a 29 70 50 61 72 73 65 2d  t char *)pParse-
2cd46 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b 0a 20  >sNameToken.z;. 
2cd47 20 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a       int nName;.
2cd48 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
2cd49 53 65 6c 65 63 74 20 26 26 20 70 43 6f 6e 73 20  Select && pCons 
2cd4a 26 26 20 70 45 6e 64 20 29 3b 0a 20 20 20 20 20  && pEnd );.     
2cd4b 20 69 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d 3d 30   if( pCons->z==0
2cd4c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6e   ){.        pCon
2cd4d 73 20 3d 20 70 45 6e 64 3b 0a 20 20 20 20 20 20  s = pEnd;.      
2cd4e 7d 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 3d 20  }.      nName = 
2cd4f 28 69 6e 74 29 28 28 63 6f 6e 73 74 20 63 68 61  (int)((const cha
2cd50 72 20 2a 29 70 43 6f 6e 73 2d 3e 7a 20 2d 20 7a  r *)pCons->z - z
2cd51 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 2d 3e  Name);.      p->
2cd52 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20 31  addColOffset = 1
2cd53 33 20 2b 20 73 71 6c 69 74 65 33 55 74 66 38 43  3 + sqlite3Utf8C
2cd54 68 61 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20 6e 4e  harLen(zName, nN
2cd55 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  ame);.    }.#end
2cd56 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65  if.  }.}..#ifnde
2cd57 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2cd58 45 57 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72  EW./*.** The par
2cd59 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
2cd5a 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20  outine in order 
2cd5b 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  to create a new 
2cd5c 56 49 45 57 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  VIEW.*/.SQLITE_P
2cd5d 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2cd5e 74 65 33 43 72 65 61 74 65 56 69 65 77 28 0a 20  te3CreateView(. 
2cd5f 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2cd60 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69      /* The parsi
2cd61 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2cd62 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e 2c 20 20  Token *pBegin,  
2cd63 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41 54 45     /* The CREATE
2cd64 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69   token that begi
2cd65 6e 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  ns the statement
2cd66 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
2cd67 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  me1,     /* The 
2cd68 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c 64 73  token that holds
2cd69 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
2cd6a 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65 6e   view */.  Token
2cd6b 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 2f 2a   *pName2,     /*
2cd6c 20 54 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20   The token that 
2cd6d 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f  holds the name o
2cd6e 66 20 74 68 65 20 76 69 65 77 20 2a 2f 0a 20 20  f the view */.  
2cd6f 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c  Select *pSelect,
2cd70 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73     /* A SELECT s
2cd71 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 77 69  tatement that wi
2cd72 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e 65  ll become the ne
2cd73 77 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20  w view */.  int 
2cd74 69 73 54 65 6d 70 2c 20 20 20 20 20 20 20 20 2f  isTemp,        /
2cd75 2a 20 54 52 55 45 20 66 6f 72 20 61 20 54 45 4d  * TRUE for a TEM
2cd76 50 4f 52 41 52 59 20 76 69 65 77 20 2a 2f 0a 20  PORARY view */. 
2cd77 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20 20 20   int noErr      
2cd78 20 20 20 20 2f 2a 20 53 75 70 70 72 65 73 73 20      /* Suppress 
2cd79 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69  error messages i
2cd7a 66 20 56 49 45 57 20 61 6c 72 65 61 64 79 20 65  f VIEW already e
2cd7b 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61  xists */.){.  Ta
2cd7c 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 3b  ble *p;.  int n;
2cd7d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2cd7e 3b 0a 20 20 54 6f 6b 65 6e 20 73 45 6e 64 3b 0a  ;.  Token sEnd;.
2cd7f 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b 0a    DbFixer sFix;.
2cd80 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 3d    Token *pName =
2cd81 20 30 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20   0;.  int iDb;. 
2cd82 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2cd83 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66  Parse->db;..  if
2cd84 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 30  ( pParse->nVar>0
2cd85 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2cd86 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2cd87 22 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20  "parameters are 
2cd88 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 69 6e 20 76  not allowed in v
2cd89 69 65 77 73 22 29 3b 0a 20 20 20 20 73 71 6c 69  iews");.    sqli
2cd8a 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2cd8b 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20  db, pSelect);.  
2cd8c 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
2cd8d 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
2cd8e 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31  e(pParse, pName1
2cd8f 2c 20 70 4e 61 6d 65 32 2c 20 69 73 54 65 6d 70  , pName2, isTemp
2cd90 2c 20 31 2c 20 30 2c 20 6e 6f 45 72 72 29 3b 0a  , 1, 0, noErr);.
2cd91 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e    p = pParse->pN
2cd92 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70  ewTable;.  if( p
2cd93 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e  ==0 || pParse->n
2cd94 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Err ){.    sqlit
2cd95 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
2cd96 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20  b, pSelect);.   
2cd97 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73   return;.  }.  s
2cd98 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
2cd99 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31  e(pParse, pName1
2cd9a 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65  , pName2, &pName
2cd9b 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  );.  iDb = sqlit
2cd9c 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
2cd9d 64 62 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b  db, p->pSchema);
2cd9e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69  .  if( sqlite3Fi
2cd9f 78 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61  xInit(&sFix, pPa
2cda0 72 73 65 2c 20 69 44 62 2c 20 22 76 69 65 77 22  rse, iDb, "view"
2cda1 2c 20 70 4e 61 6d 65 29 0a 20 20 20 20 26 26 20  , pName).    && 
2cda2 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74  sqlite3FixSelect
2cda3 28 26 73 46 69 78 2c 20 70 53 65 6c 65 63 74 29  (&sFix, pSelect)
2cda4 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  .  ){.    sqlite
2cda5 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2cda6 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  , pSelect);.    
2cda7 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  return;.  }..  /
2cda8 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  * Make a copy of
2cda9 20 74 68 65 20 65 6e 74 69 72 65 20 53 45 4c 45   the entire SELE
2cdaa 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  CT statement tha
2cdab 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 76 69  t defines the vi
2cdac 65 77 2e 0a 20 20 2a 2a 20 54 68 69 73 20 77 69  ew..  ** This wi
2cdad 6c 6c 20 66 6f 72 63 65 20 61 6c 6c 20 74 68 65  ll force all the
2cdae 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61   Expr.token.z va
2cdaf 6c 75 65 73 20 74 6f 20 62 65 20 64 79 6e 61 6d  lues to be dynam
2cdb0 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 61 6c 6c 6f  ically.  ** allo
2cdb1 63 61 74 65 64 20 72 61 74 68 65 72 20 74 68 61  cated rather tha
2cdb2 6e 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 69  n point to the i
2cdb3 6e 70 75 74 20 73 74 72 69 6e 67 20 2d 20 77 68  nput string - wh
2cdb4 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 0a 20  ich means that. 
2cdb5 20 2a 2a 20 74 68 65 79 20 77 69 6c 6c 20 70 65   ** they will pe
2cdb6 72 73 69 73 74 20 61 66 74 65 72 20 74 68 65 20  rsist after the 
2cdb7 63 75 72 72 65 6e 74 20 73 71 6c 69 74 65 33 5f  current sqlite3_
2cdb8 65 78 65 63 28 29 20 63 61 6c 6c 20 72 65 74 75  exec() call retu
2cdb9 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70  rns..  */.  p->p
2cdba 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33  Select = sqlite3
2cdbb 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 53  SelectDup(db, pS
2cdbc 65 6c 65 63 74 2c 20 45 58 50 52 44 55 50 5f 52  elect, EXPRDUP_R
2cdbd 45 44 55 43 45 29 3b 0a 20 20 73 71 6c 69 74 65  EDUCE);.  sqlite
2cdbe 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2cdbf 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 69 66  , pSelect);.  if
2cdc0 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2cdc1 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
2cdc2 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62 2d  ;.  }.  if( !db-
2cdc3 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20  >init.busy ){.  
2cdc4 20 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74    sqlite3ViewGet
2cdc5 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
2cdc6 73 65 2c 20 70 29 3b 0a 20 20 7d 0a 0a 20 20 2f  se, p);.  }..  /
2cdc7 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 65 6e 64  * Locate the end
2cdc8 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 56   of the CREATE V
2cdc9 49 45 57 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  IEW statement.  
2cdca 4d 61 6b 65 20 73 45 6e 64 20 70 6f 69 6e 74 20  Make sEnd point 
2cdcb 74 6f 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 2e  to.  ** the end.
2cdcc 0a 20 20 2a 2f 0a 20 20 73 45 6e 64 20 3d 20 70  .  */.  sEnd = p
2cdcd 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
2cdce 6e 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  n;.  if( ALWAYS(
2cdcf 73 45 6e 64 2e 7a 5b 30 5d 21 3d 30 29 20 26 26  sEnd.z[0]!=0) &&
2cdd0 20 73 45 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20   sEnd.z[0]!=';' 
2cdd1 29 7b 0a 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d  ){.    sEnd.z +=
2cdd2 20 73 45 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73   sEnd.n;.  }.  s
2cdd3 45 6e 64 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d  End.n = 0;.  n =
2cdd4 20 28 69 6e 74 29 28 73 45 6e 64 2e 7a 20 2d 20   (int)(sEnd.z - 
2cdd5 70 42 65 67 69 6e 2d 3e 7a 29 3b 0a 20 20 7a 20  pBegin->z);.  z 
2cdd6 3d 20 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77  = pBegin->z;.  w
2cdd7 68 69 6c 65 28 20 41 4c 57 41 59 53 28 6e 3e 30  hile( ALWAYS(n>0
2cdd8 29 20 26 26 20 73 71 6c 69 74 65 33 49 73 73 70  ) && sqlite3Issp
2cdd9 61 63 65 28 7a 5b 6e 2d 31 5d 29 20 29 7b 20 6e  ace(z[n-1]) ){ n
2cdda 2d 2d 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 3d  --; }.  sEnd.z =
2cddb 20 26 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e 64   &z[n-1];.  sEnd
2cddc 2e 6e 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 73  .n = 1;..  /* Us
2cddd 65 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c  e sqlite3EndTabl
2cdde 65 28 29 20 74 6f 20 61 64 64 20 74 68 65 20 76  e() to add the v
2cddf 69 65 77 20 74 6f 20 74 68 65 20 53 51 4c 49 54  iew to the SQLIT
2cde0 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 2a  E_MASTER table *
2cde1 2f 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61  /.  sqlite3EndTa
2cde2 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 26  ble(pParse, 0, &
2cde3 73 45 6e 64 2c 20 30 29 3b 0a 20 20 72 65 74 75  sEnd, 0);.  retu
2cde4 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  rn;.}.#endif /* 
2cde5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
2cde6 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65   */..#if !define
2cde7 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
2cde8 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  EW) || !defined(
2cde9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2cdea 55 41 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a 20  UALTABLE)./*.** 
2cdeb 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  The Table struct
2cdec 75 72 65 20 70 54 61 62 6c 65 20 69 73 20 72 65  ure pTable is re
2cded 61 6c 6c 79 20 61 20 56 49 45 57 2e 20 20 46 69  ally a VIEW.  Fi
2cdee 6c 6c 20 69 6e 20 74 68 65 20 6e 61 6d 65 73 20  ll in the names 
2cdef 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e  of.** the column
2cdf0 73 20 6f 66 20 74 68 65 20 76 69 65 77 20 69 6e  s of the view in
2cdf1 20 74 68 65 20 70 54 61 62 6c 65 20 73 74 72 75   the pTable stru
2cdf2 63 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 74  cture.  Return t
2cdf3 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20  he number.** of 
2cdf4 65 72 72 6f 72 73 2e 20 20 49 66 20 61 6e 20 65  errors.  If an e
2cdf5 72 72 6f 72 20 69 73 20 73 65 65 6e 20 6c 65 61  rror is seen lea
2cdf6 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ve an error mess
2cdf7 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a  age in pParse->z
2cdf8 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54  ErrMsg..*/.SQLIT
2cdf9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2cdfa 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
2cdfb 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 20 2a 70  mnNames(Parse *p
2cdfc 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54  Parse, Table *pT
2cdfd 61 62 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a  able){.  Table *
2cdfe 70 53 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 20  pSelTab;   /* A 
2cdff 66 61 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d 20  fake table from 
2ce00 77 68 69 63 68 20 77 65 20 67 65 74 20 74 68 65  which we get the
2ce01 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20   result set */. 
2ce02 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20   Select *pSel;  
2ce03 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 68     /* Copy of th
2ce04 65 20 53 45 4c 45 43 54 20 74 68 61 74 20 69 6d  e SELECT that im
2ce05 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65  plements the vie
2ce06 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 20  w */.  int nErr 
2ce07 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62  = 0;     /* Numb
2ce08 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63  er of errors enc
2ce09 6f 75 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e  ountered */.  in
2ce0a 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  t n;            
2ce0b 2f 2a 20 54 65 6d 70 6f 72 61 72 69 6c 79 20 68  /* Temporarily h
2ce0c 6f 6c 64 73 20 74 68 65 20 6e 75 6d 62 65 72 20  olds the number 
2ce0d 6f 66 20 63 75 72 73 6f 72 73 20 61 73 73 69 67  of cursors assig
2ce0e 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ned */.  sqlite3
2ce0f 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2ce10 62 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20  b;  /* Database 
2ce11 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 6d  connection for m
2ce12 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 2a 2f 0a  alloc errors */.
2ce13 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76    int (*xAuth)(v
2ce14 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
2ce15 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
2ce16 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
2ce17 73 74 20 63 68 61 72 2a 29 3b 0a 0a 20 20 61 73  st char*);..  as
2ce18 73 65 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a  sert( pTable );.
2ce19 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2ce1a 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2ce1b 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  E.  if( sqlite3V
2ce1c 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 70  tabCallConnect(p
2ce1d 50 61 72 73 65 2c 20 70 54 61 62 6c 65 29 20 29  Parse, pTable) )
2ce1e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
2ce1f 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
2ce20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
2ce21 54 61 62 6c 65 29 20 29 20 72 65 74 75 72 6e 20  Table) ) return 
2ce22 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  0;.#endif..#ifnd
2ce23 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2ce24 49 45 57 0a 20 20 2f 2a 20 41 20 70 6f 73 69 74  IEW.  /* A posit
2ce25 69 76 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 74  ive nCol means t
2ce26 68 65 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73  he columns names
2ce27 20 66 6f 72 20 74 68 69 73 20 76 69 65 77 20 61   for this view a
2ce28 72 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20  re.  ** already 
2ce29 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66  known..  */.  if
2ce2a 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 30  ( pTable->nCol>0
2ce2b 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20   ) return 0;..  
2ce2c 2f 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6e 43  /* A negative nC
2ce2d 6f 6c 20 69 73 20 61 20 73 70 65 63 69 61 6c 20  ol is a special 
2ce2e 6d 61 72 6b 65 72 20 6d 65 61 6e 69 6e 67 20 74  marker meaning t
2ce2f 68 61 74 20 77 65 20 61 72 65 20 63 75 72 72 65  hat we are curre
2ce30 6e 74 6c 79 0a 20 20 2a 2a 20 74 72 79 69 6e 67  ntly.  ** trying
2ce31 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20   to compute the 
2ce32 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49  column names.  I
2ce33 66 20 77 65 20 65 6e 74 65 72 20 74 68 69 73 20  f we enter this 
2ce34 72 6f 75 74 69 6e 65 20 77 69 74 68 0a 20 20 2a  routine with.  *
2ce35 2a 20 61 20 6e 65 67 61 74 69 76 65 20 6e 43 6f  * a negative nCo
2ce36 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 77 6f 20  l, it means two 
2ce37 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20 66 6f  or more views fo
2ce38 72 6d 20 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65 20  rm a loop, like 
2ce39 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  this:.  **.  ** 
2ce3a 20 20 20 20 43 52 45 41 54 45 20 56 49 45 57 20      CREATE VIEW 
2ce3b 6f 6e 65 20 41 53 20 53 45 4c 45 43 54 20 2a 20  one AS SELECT * 
2ce3c 46 52 4f 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20 20  FROM two;.  **  
2ce3d 20 20 20 43 52 45 41 54 45 20 56 49 45 57 20 74     CREATE VIEW t
2ce3e 77 6f 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46  wo AS SELECT * F
2ce3f 52 4f 4d 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20  ROM one;.  **.  
2ce40 2a 2a 20 41 63 74 75 61 6c 6c 79 2c 20 74 68 65  ** Actually, the
2ce41 20 65 72 72 6f 72 20 61 62 6f 76 65 20 69 73 20   error above is 
2ce42 6e 6f 77 20 63 61 75 67 68 74 20 70 72 69 6f 72  now caught prior
2ce43 20 74 6f 20 72 65 61 63 68 69 6e 67 20 74 68 69   to reaching thi
2ce44 73 20 70 6f 69 6e 74 2e 0a 20 20 2a 2a 20 42 75  s point..  ** Bu
2ce45 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  t the following 
2ce46 74 65 73 74 20 69 73 20 73 74 69 6c 6c 20 69 6d  test is still im
2ce47 70 6f 72 74 61 6e 74 20 61 73 20 69 74 20 64 6f  portant as it do
2ce48 65 73 20 63 6f 6d 65 20 75 70 0a 20 20 2a 2a 20  es come up.  ** 
2ce49 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  in the following
2ce4a 3a 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 20 20 20  :.  ** .  **    
2ce4b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6d 61   CREATE TABLE ma
2ce4c 69 6e 2e 65 78 31 28 61 29 3b 0a 20 20 2a 2a 20  in.ex1(a);.  ** 
2ce4d 20 20 20 20 43 52 45 41 54 45 20 54 45 4d 50 20      CREATE TEMP 
2ce4e 56 49 45 57 20 65 78 31 20 41 53 20 53 45 4c 45  VIEW ex1 AS SELE
2ce4f 43 54 20 61 20 46 52 4f 4d 20 65 78 31 3b 0a 20  CT a FROM ex1;. 
2ce50 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 2a   **     SELECT *
2ce51 20 46 52 4f 4d 20 74 65 6d 70 2e 65 78 31 3b 0a   FROM temp.ex1;.
2ce52 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c    */.  if( pTabl
2ce53 65 2d 3e 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20 20  e->nCol<0 ){.   
2ce54 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2ce55 28 70 50 61 72 73 65 2c 20 22 76 69 65 77 20 25  (pParse, "view %
2ce56 73 20 69 73 20 63 69 72 63 75 6c 61 72 6c 79 20  s is circularly 
2ce57 64 65 66 69 6e 65 64 22 2c 20 70 54 61 62 6c 65  defined", pTable
2ce58 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65  ->zName);.    re
2ce59 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 73  turn 1;.  }.  as
2ce5a 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e 43  sert( pTable->nC
2ce5b 6f 6c 3e 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49  ol>=0 );..  /* I
2ce5c 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66 61  f we get this fa
2ce5d 72 2c 20 69 74 20 6d 65 61 6e 73 20 77 65 20 6e  r, it means we n
2ce5e 65 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74  eed to compute t
2ce5f 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 73 2e 0a  he table names..
2ce60 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74    ** Note that t
2ce61 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  he call to sqlit
2ce62 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
2ce63 65 63 74 28 29 20 77 69 6c 6c 20 65 78 70 61 6e  ect() will expan
2ce64 64 20 61 6e 79 0a 20 20 2a 2a 20 22 2a 22 20 65  d any.  ** "*" e
2ce65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 72  lements in the r
2ce66 65 73 75 6c 74 73 20 73 65 74 20 6f 66 20 74 68  esults set of th
2ce67 65 20 76 69 65 77 20 61 6e 64 20 77 69 6c 6c 20  e view and will 
2ce68 61 73 73 69 67 6e 20 63 75 72 73 6f 72 73 0a 20  assign cursors. 
2ce69 20 2a 2a 20 74 6f 20 74 68 65 20 65 6c 65 6d 65   ** to the eleme
2ce6a 6e 74 73 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  nts of the FROM 
2ce6b 63 6c 61 75 73 65 2e 20 20 42 75 74 20 77 65 20  clause.  But we 
2ce6c 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 73  do not want thes
2ce6d 65 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74  e changes.  ** t
2ce6e 6f 20 62 65 20 70 65 72 6d 61 6e 65 6e 74 2e 20  o be permanent. 
2ce6f 20 53 6f 20 74 68 65 20 63 6f 6d 70 75 74 61 74   So the computat
2ce70 69 6f 6e 20 69 73 20 64 6f 6e 65 20 6f 6e 20 61  ion is done on a
2ce71 20 63 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c   copy of the SEL
2ce72 45 43 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  ECT.  ** stateme
2ce73 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  nt that defines 
2ce74 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 20  the view..  */. 
2ce75 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d   assert( pTable-
2ce76 3e 70 53 65 6c 65 63 74 20 29 3b 0a 20 20 70 53  >pSelect );.  pS
2ce77 65 6c 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  el = sqlite3Sele
2ce78 63 74 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65  ctDup(db, pTable
2ce79 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20  ->pSelect, 0);. 
2ce7a 20 69 66 28 20 70 53 65 6c 20 29 7b 0a 20 20 20   if( pSel ){.   
2ce7b 20 75 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73   u8 enableLookas
2ce7c 69 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ide = db->lookas
2ce7d 69 64 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20  ide.bEnabled;.  
2ce7e 20 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54    n = pParse->nT
2ce7f 61 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  ab;.    sqlite3S
2ce80 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73  rcListAssignCurs
2ce81 6f 72 73 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ors(pParse, pSel
2ce82 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 70 54 61  ->pSrc);.    pTa
2ce83 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a  ble->nCol = -1;.
2ce84 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
2ce85 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a  e.bEnabled = 0;.
2ce86 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2ce87 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
2ce88 4e 0a 20 20 20 20 78 41 75 74 68 20 3d 20 64 62  N.    xAuth = db
2ce89 2d 3e 78 41 75 74 68 3b 0a 20 20 20 20 64 62 2d  ->xAuth;.    db-
2ce8a 3e 78 41 75 74 68 20 3d 20 30 3b 0a 20 20 20 20  >xAuth = 0;.    
2ce8b 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65  pSelTab = sqlite
2ce8c 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65  3ResultSetOfSele
2ce8d 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29  ct(pParse, pSel)
2ce8e 3b 0a 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20  ;.    db->xAuth 
2ce8f 3d 20 78 41 75 74 68 3b 0a 23 65 6c 73 65 0a 20  = xAuth;.#else. 
2ce90 20 20 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c     pSelTab = sql
2ce91 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53  ite3ResultSetOfS
2ce92 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53  elect(pParse, pS
2ce93 65 6c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  el);.#endif.    
2ce94 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
2ce95 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 4c  nabled = enableL
2ce96 6f 6f 6b 61 73 69 64 65 3b 0a 20 20 20 20 70 50  ookaside;.    pP
2ce97 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b 0a  arse->nTab = n;.
2ce98 20 20 20 20 69 66 28 20 70 53 65 6c 54 61 62 20      if( pSelTab 
2ce99 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
2ce9a 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d 30   pTable->aCol==0
2ce9b 20 29 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65   );.      pTable
2ce9c 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62  ->nCol = pSelTab
2ce9d 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 54  ->nCol;.      pT
2ce9e 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53 65  able->aCol = pSe
2ce9f 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20  lTab->aCol;.    
2cea0 20 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20    pSelTab->nCol 
2cea1 3d 20 30 3b 0a 20 20 20 20 20 20 70 53 65 6c 54  = 0;.      pSelT
2cea2 61 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20  ab->aCol = 0;.  
2cea3 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
2cea4 65 54 61 62 6c 65 28 64 62 2c 20 70 53 65 6c 54  eTable(db, pSelT
2cea5 61 62 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ab);.      asser
2cea6 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61  t( sqlite3Schema
2cea7 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 30 2c  MutexHeld(db, 0,
2cea8 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61   pTable->pSchema
2cea9 29 20 29 3b 0a 20 20 20 20 20 20 70 54 61 62 6c  ) );.      pTabl
2ceaa 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67  e->pSchema->flag
2ceab 73 20 7c 3d 20 44 42 5f 55 6e 72 65 73 65 74 56  s |= DB_UnresetV
2ceac 69 65 77 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  iews;.    }else{
2cead 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e  .      pTable->n
2ceae 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 6e  Col = 0;.      n
2ceaf 45 72 72 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20  Err++;.    }.   
2ceb0 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2ceb1 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 29 3b 0a  lete(db, pSel);.
2ceb2 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 6e    } else {.    n
2ceb3 45 72 72 2b 2b 3b 0a 20 20 7d 0a 23 65 6e 64 69  Err++;.  }.#endi
2ceb4 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2ceb5 5f 56 49 45 57 20 2a 2f 0a 20 20 72 65 74 75 72  _VIEW */.  retur
2ceb6 6e 20 6e 45 72 72 3b 20 20 0a 7d 0a 23 65 6e 64  n nErr;  .}.#end
2ceb7 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
2ceb8 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
2ceb9 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
2ceba 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2cebb 54 41 42 4c 45 29 20 2a 2f 0a 0a 23 69 66 6e 64  TABLE) */..#ifnd
2cebc 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2cebd 49 45 57 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  IEW./*.** Clear 
2cebe 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
2cebf 20 66 72 6f 6d 20 65 76 65 72 79 20 56 49 45 57   from every VIEW
2cec0 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 64 78   in database idx
2cec1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2cec2 20 73 71 6c 69 74 65 56 69 65 77 52 65 73 65 74   sqliteViewReset
2cec3 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  All(sqlite3 *db,
2cec4 20 69 6e 74 20 69 64 78 29 7b 0a 20 20 48 61 73   int idx){.  Has
2cec5 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 61 73 73 65  hElem *i;.  asse
2cec6 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d  rt( sqlite3Schem
2cec7 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69  aMutexHeld(db, i
2cec8 64 78 2c 20 30 29 20 29 3b 0a 20 20 69 66 28 20  dx, 0) );.  if( 
2cec9 21 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 64  !DbHasProperty(d
2ceca 62 2c 20 69 64 78 2c 20 44 42 5f 55 6e 72 65 73  b, idx, DB_Unres
2cecb 65 74 56 69 65 77 73 29 20 29 20 72 65 74 75 72  etViews) ) retur
2cecc 6e 3b 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74  n;.  for(i=sqlit
2cecd 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e  eHashFirst(&db->
2cece 61 44 62 5b 69 64 78 5d 2e 70 53 63 68 65 6d 61  aDb[idx].pSchema
2cecf 2d 3e 74 62 6c 48 61 73 68 29 3b 20 69 3b 69 3d  ->tblHash); i;i=
2ced0 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69  sqliteHashNext(i
2ced1 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  )){.    Table *p
2ced2 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68  Tab = sqliteHash
2ced3 44 61 74 61 28 69 29 3b 0a 20 20 20 20 69 66 28  Data(i);.    if(
2ced4 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29   pTab->pSelect )
2ced5 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 44 65  {.      sqliteDe
2ced6 6c 65 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  leteColumnNames(
2ced7 64 62 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20  db, pTab);.     
2ced8 20 70 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b   pTab->aCol = 0;
2ced9 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 43 6f  .      pTab->nCo
2ceda 6c 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  l = 0;.    }.  }
2cedb 0a 20 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72  .  DbClearProper
2cedc 74 79 28 64 62 2c 20 69 64 78 2c 20 44 42 5f 55  ty(db, idx, DB_U
2cedd 6e 72 65 73 65 74 56 69 65 77 73 29 3b 0a 7d 0a  nresetViews);.}.
2cede 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
2cedf 71 6c 69 74 65 56 69 65 77 52 65 73 65 74 41 6c  qliteViewResetAl
2cee0 6c 28 41 2c 42 29 0a 23 65 6e 64 69 66 20 2f 2a  l(A,B).#endif /*
2cee1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
2cee2 57 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  W */../*.** This
2cee3 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
2cee4 6c 65 64 20 62 79 20 74 68 65 20 56 44 42 45 20  led by the VDBE 
2cee5 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20 69 6e  to adjust the in
2cee6 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a 2a  ternal schema.**
2cee7 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20   used by SQLite 
2cee8 77 68 65 6e 20 74 68 65 20 62 74 72 65 65 20 6c  when the btree l
2cee9 61 79 65 72 20 6d 6f 76 65 73 20 61 20 74 61 62  ayer moves a tab
2ceea 6c 65 20 72 6f 6f 74 20 70 61 67 65 2e 20 54 68  le root page. Th
2ceeb 65 0a 2a 2a 20 72 6f 6f 74 2d 70 61 67 65 20 6f  e.** root-page o
2ceec 66 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  f a table or ind
2ceed 65 78 20 69 6e 20 64 61 74 61 62 61 73 65 20 69  ex in database i
2ceee 44 62 20 68 61 73 20 63 68 61 6e 67 65 64 20 66  Db has changed f
2ceef 72 6f 6d 20 69 46 72 6f 6d 0a 2a 2a 20 74 6f 20  rom iFrom.** to 
2cef0 69 54 6f 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65  iTo..**.** Ticke
2cef1 74 20 23 31 37 32 38 3a 20 20 54 68 65 20 73 79  t #1728:  The sy
2cef2 6d 62 6f 6c 20 74 61 62 6c 65 20 6d 69 67 68 74  mbol table might
2cef3 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 69   still contain i
2cef4 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 6f 6e  nformation.** on
2cef5 20 74 61 62 6c 65 73 20 61 6e 64 2f 6f 72 20 69   tables and/or i
2cef6 6e 64 69 63 65 73 20 74 68 61 74 20 61 72 65 20  ndices that are 
2cef7 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 62  the process of b
2cef8 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 2a 2a  eing deleted..**
2cef9 20 49 66 20 79 6f 75 20 61 72 65 20 75 6e 6c 75   If you are unlu
2cefa 63 6b 79 2c 20 6f 6e 65 20 6f 66 20 74 68 6f 73  cky, one of thos
2cefb 65 20 64 65 6c 65 74 65 64 20 69 6e 64 69 63 65  e deleted indice
2cefc 73 20 6f 72 20 74 61 62 6c 65 73 20 6d 69 67 68  s or tables migh
2cefd 74 0a 2a 2a 20 68 61 76 65 20 74 68 65 20 73 61  t.** have the sa
2cefe 6d 65 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62  me rootpage numb
2ceff 65 72 20 61 73 20 74 68 65 20 72 65 61 6c 20 74  er as the real t
2cf00 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74 68  able or index th
2cf01 61 74 20 69 73 0a 2a 2a 20 62 65 69 6e 67 20 6d  at is.** being m
2cf02 6f 76 65 64 2e 20 20 53 6f 20 77 65 20 63 61 6e  oved.  So we can
2cf03 6e 6f 74 20 73 74 6f 70 20 73 65 61 72 63 68 69  not stop searchi
2cf04 6e 67 20 61 66 74 65 72 20 74 68 65 20 66 69 72  ng after the fir
2cf05 73 74 20 6d 61 74 63 68 20 0a 2a 2a 20 62 65 63  st match .** bec
2cf06 61 75 73 65 20 74 68 65 20 66 69 72 73 74 20 6d  ause the first m
2cf07 61 74 63 68 20 6d 69 67 68 74 20 62 65 20 66 6f  atch might be fo
2cf08 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 65 6c  r one of the del
2cf09 65 74 65 64 20 69 6e 64 69 63 65 73 0a 2a 2a 20  eted indices.** 
2cf0a 6f 72 20 74 61 62 6c 65 73 20 61 6e 64 20 6e 6f  or tables and no
2cf0b 74 20 74 68 65 20 74 61 62 6c 65 2f 69 6e 64 65  t the table/inde
2cf0c 78 20 74 68 61 74 20 69 73 20 61 63 74 75 61 6c  x that is actual
2cf0d 6c 79 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 0a  ly being moved..
2cf0e 2a 2a 20 57 65 20 6d 75 73 74 20 63 6f 6e 74 69  ** We must conti
2cf0f 6e 75 65 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69  nue looping unti
2cf10 6c 20 61 6c 6c 20 74 61 62 6c 65 73 20 61 6e 64  l all tables and
2cf11 20 69 6e 64 69 63 65 73 20 77 69 74 68 0a 2a 2a   indices with.**
2cf12 20 72 6f 6f 74 70 61 67 65 3d 3d 69 46 72 6f 6d   rootpage==iFrom
2cf13 20 68 61 76 65 20 62 65 65 6e 20 63 6f 6e 76 65   have been conve
2cf14 72 74 65 64 20 74 6f 20 68 61 76 65 20 61 20 72  rted to have a r
2cf15 6f 6f 74 70 61 67 65 20 6f 66 20 69 54 6f 0a 2a  ootpage of iTo.*
2cf16 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65  * in order to be
2cf17 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 77 65   certain that we
2cf18 20 67 6f 74 20 74 68 65 20 72 69 67 68 74 20 6f   got the right o
2cf19 6e 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ne..*/.#ifndef S
2cf1a 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
2cf1b 41 43 55 55 4d 0a 53 51 4c 49 54 45 5f 50 52 49  ACUUM.SQLITE_PRI
2cf1c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2cf1d 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 73  3RootPageMoved(s
2cf1e 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
2cf1f 69 44 62 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20  iDb, int iFrom, 
2cf20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 48 61 73 68  int iTo){.  Hash
2cf21 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 48  Elem *pElem;.  H
2cf22 61 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 44 62  ash *pHash;.  Db
2cf23 20 2a 70 44 62 3b 0a 0a 20 20 61 73 73 65 72 74   *pDb;..  assert
2cf24 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d  ( sqlite3SchemaM
2cf25 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44 62  utexHeld(db, iDb
2cf26 2c 20 30 29 20 29 3b 0a 20 20 70 44 62 20 3d 20  , 0) );.  pDb = 
2cf27 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20  &db->aDb[iDb];. 
2cf28 20 70 48 61 73 68 20 3d 20 26 70 44 62 2d 3e 70   pHash = &pDb->p
2cf29 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b  Schema->tblHash;
2cf2a 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c  .  for(pElem=sql
2cf2b 69 74 65 48 61 73 68 46 69 72 73 74 28 70 48 61  iteHashFirst(pHa
2cf2c 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65  sh); pElem; pEle
2cf2d 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  m=sqliteHashNext
2cf2e 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 54 61  (pElem)){.    Ta
2cf2f 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69  ble *pTab = sqli
2cf30 74 65 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d  teHashData(pElem
2cf31 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d  );.    if( pTab-
2cf32 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a  >tnum==iFrom ){.
2cf33 20 20 20 20 20 20 70 54 61 62 2d 3e 74 6e 75 6d        pTab->tnum
2cf34 20 3d 20 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20   = iTo;.    }.  
2cf35 7d 0a 20 20 70 48 61 73 68 20 3d 20 26 70 44 62  }.  pHash = &pDb
2cf36 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61  ->pSchema->idxHa
2cf37 73 68 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d  sh;.  for(pElem=
2cf38 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
2cf39 70 48 61 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70  pHash); pElem; p
2cf3a 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e  Elem=sqliteHashN
2cf3b 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20  ext(pElem)){.   
2cf3c 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 73   Index *pIdx = s
2cf3d 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45  qliteHashData(pE
2cf3e 6c 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 49  lem);.    if( pI
2cf3f 64 78 2d 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d 20  dx->tnum==iFrom 
2cf40 29 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 74  ){.      pIdx->t
2cf41 6e 75 6d 20 3d 20 69 54 6f 3b 0a 20 20 20 20 7d  num = iTo;.    }
2cf42 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  .  }.}.#endif../
2cf43 2a 0a 2a 2a 20 57 72 69 74 65 20 63 6f 64 65 20  *.** Write code 
2cf44 74 6f 20 65 72 61 73 65 20 74 68 65 20 74 61 62  to erase the tab
2cf45 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67  le with root-pag
2cf46 65 20 69 54 61 62 6c 65 20 66 72 6f 6d 20 64 61  e iTable from da
2cf47 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20 41  tabase iDb..** A
2cf48 6c 73 6f 20 77 72 69 74 65 20 63 6f 64 65 20 74  lso write code t
2cf49 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 73 71 6c  o modify the sql
2cf4a 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
2cf4b 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c 20 73 63   and internal sc
2cf4c 68 65 6d 61 0a 2a 2a 20 69 66 20 61 20 72 6f 6f  hema.** if a roo
2cf4d 74 2d 70 61 67 65 20 6f 66 20 61 6e 6f 74 68 65  t-page of anothe
2cf4e 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 64  r table is moved
2cf4f 20 62 79 20 74 68 65 20 62 74 72 65 65 2d 6c 61   by the btree-la
2cf50 79 65 72 20 77 68 69 6c 73 74 0a 2a 2a 20 65 72  yer whilst.** er
2cf51 61 73 69 6e 67 20 69 54 61 62 6c 65 20 28 74 68  asing iTable (th
2cf52 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 77 69  is can happen wi
2cf53 74 68 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75  th an auto-vacuu
2cf54 6d 20 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f 20  m database)..*/ 
2cf55 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 73  .static void des
2cf56 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 50 61 72  troyRootPage(Par
2cf57 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
2cf58 69 54 61 62 6c 65 2c 20 69 6e 74 20 69 44 62 29  iTable, int iDb)
2cf59 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71  {.  Vdbe *v = sq
2cf5a 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
2cf5b 72 73 65 29 3b 0a 20 20 69 6e 74 20 72 31 20 3d  rse);.  int r1 =
2cf5c 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2cf5d 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71  eg(pParse);.  sq
2cf5e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2cf5f 76 2c 20 4f 50 5f 44 65 73 74 72 6f 79 2c 20 69  v, OP_Destroy, i
2cf60 54 61 62 6c 65 2c 20 72 31 2c 20 69 44 62 29 3b  Table, r1, iDb);
2cf61 0a 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f  .  sqlite3MayAbo
2cf62 72 74 28 70 50 61 72 73 65 29 3b 0a 23 69 66 6e  rt(pParse);.#ifn
2cf63 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2cf64 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20  AUTOVACUUM.  /* 
2cf65 4f 50 5f 44 65 73 74 72 6f 79 20 73 74 6f 72 65  OP_Destroy store
2cf66 73 20 61 6e 20 69 6e 20 69 6e 74 65 67 65 72 20  s an in integer 
2cf67 72 31 2e 20 49 66 20 74 68 69 73 20 69 6e 74 65  r1. If this inte
2cf68 67 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f 6e 2d  ger.  ** is non-
2cf69 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 69 73  zero, then it is
2cf6a 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e   the root page n
2cf6b 75 6d 62 65 72 20 6f 66 20 61 20 74 61 62 6c 65  umber of a table
2cf6c 20 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a 20 6c   moved to.  ** l
2cf6d 6f 63 61 74 69 6f 6e 20 69 54 61 62 6c 65 2e 20  ocation iTable. 
2cf6e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
2cf6f 64 65 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20  de modifies the 
2cf70 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
2cf71 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65 66 6c  ble to.  ** refl
2cf72 65 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a 0a 20  ect this..  **. 
2cf73 20 2a 2a 20 54 68 65 20 22 23 4e 4e 4e 22 20 69   ** The "#NNN" i
2cf74 6e 20 74 68 65 20 53 51 4c 20 69 73 20 61 20 73  n the SQL is a s
2cf75 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20  pecial constant 
2cf76 74 68 61 74 20 6d 65 61 6e 73 20 77 68 61 74 65  that means whate
2cf77 76 65 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20 69  ver value.  ** i
2cf78 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 4e 4e  s in register NN
2cf79 4e 2e 20 20 53 65 65 20 67 72 61 6d 6d 61 72 20  N.  See grammar 
2cf7a 72 75 6c 65 73 20 61 73 73 6f 63 69 61 74 65 64  rules associated
2cf7b 20 77 69 74 68 20 74 68 65 20 54 4b 5f 52 45 47   with the TK_REG
2cf7c 49 53 54 45 52 0a 20 20 2a 2a 20 74 6f 6b 65 6e  ISTER.  ** token
2cf7d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
2cf7e 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a  information..  *
2cf7f 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65  /.  sqlite3Neste
2cf80 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a  dParse(pParse, .
2cf81 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e       "UPDATE %Q.
2cf82 25 73 20 53 45 54 20 72 6f 6f 74 70 61 67 65 3d  %s SET rootpage=
2cf83 25 64 20 57 48 45 52 45 20 23 25 64 20 41 4e 44  %d WHERE #%d AND
2cf84 20 72 6f 6f 74 70 61 67 65 3d 23 25 64 22 2c 0a   rootpage=#%d",.
2cf85 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d       pParse->db-
2cf86 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
2cf87 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
2cf88 62 29 2c 20 69 54 61 62 6c 65 2c 20 72 31 2c 20  b), iTable, r1, 
2cf89 72 31 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71  r1);.#endif.  sq
2cf8a 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2cf8b 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
2cf8c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
2cf8d 56 44 42 45 20 63 6f 64 65 20 74 6f 20 65 72 61  VDBE code to era
2cf8e 73 65 20 74 61 62 6c 65 20 70 54 61 62 20 61 6e  se table pTab an
2cf8f 64 20 61 6c 6c 20 61 73 73 6f 63 69 61 74 65 64  d all associated
2cf90 20 69 6e 64 69 63 65 73 20 6f 6e 20 64 69 73 6b   indices on disk
2cf91 2e 0a 2a 2a 20 43 6f 64 65 20 74 6f 20 75 70 64  ..** Code to upd
2cf92 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f 6d  ate the sqlite_m
2cf93 61 73 74 65 72 20 74 61 62 6c 65 73 20 61 6e 64  aster tables and
2cf94 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61   internal schema
2cf95 20 64 65 66 69 6e 69 74 69 6f 6e 73 0a 2a 2a 20   definitions.** 
2cf96 69 6e 20 63 61 73 65 20 61 20 72 6f 6f 74 2d 70  in case a root-p
2cf97 61 67 65 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f  age belonging to
2cf98 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 69   another table i
2cf99 73 20 6d 6f 76 65 64 20 62 79 20 74 68 65 20 62  s moved by the b
2cf9a 74 72 65 65 20 6c 61 79 65 72 0a 2a 2a 20 69 73  tree layer.** is
2cf9b 20 61 6c 73 6f 20 61 64 64 65 64 20 28 74 68 69   also added (thi
2cf9c 73 20 63 61 6e 20 68 61 70 70 65 6e 20 77 69 74  s can happen wit
2cf9d 68 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  h an auto-vacuum
2cf9e 20 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f 0a 73   database)..*/.s
2cf9f 74 61 74 69 63 20 76 6f 69 64 20 64 65 73 74 72  tatic void destr
2cfa0 6f 79 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70  oyTable(Parse *p
2cfa1 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54  Parse, Table *pT
2cfa2 61 62 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  ab){.#ifdef SQLI
2cfa3 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
2cfa4 55 4d 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  UM.  Index *pIdx
2cfa5 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71  ;.  int iDb = sq
2cfa6 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
2cfa7 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ex(pParse->db, p
2cfa8 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
2cfa9 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 65   destroyRootPage
2cfaa 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 74  (pParse, pTab->t
2cfab 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 66 6f 72  num, iDb);.  for
2cfac 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
2cfad 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
2cfae 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
2cfaf 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 65   destroyRootPage
2cfb0 28 70 50 61 72 73 65 2c 20 70 49 64 78 2d 3e 74  (pParse, pIdx->t
2cfb1 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 23  num, iDb);.  }.#
2cfb2 65 6c 73 65 0a 20 20 2f 2a 20 49 66 20 74 68 65  else.  /* If the
2cfb3 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 62 65   database may be
2cfb4 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 61 70   auto-vacuum cap
2cfb5 61 62 6c 65 20 28 69 66 20 53 51 4c 49 54 45 5f  able (if SQLITE_
2cfb6 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
2cfb7 20 20 2a 2a 20 69 73 20 6e 6f 74 20 64 65 66 69    ** is not defi
2cfb8 6e 65 64 29 2c 20 74 68 65 6e 20 69 74 20 69 73  ned), then it is
2cfb9 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 63 61   important to ca
2cfba 6c 6c 20 4f 50 5f 44 65 73 74 72 6f 79 20 6f 6e  ll OP_Destroy on
2cfbb 20 74 68 65 0a 20 20 2a 2a 20 74 61 62 6c 65 20   the.  ** table 
2cfbc 61 6e 64 20 69 6e 64 65 78 20 72 6f 6f 74 2d 70  and index root-p
2cfbd 61 67 65 73 20 69 6e 20 6f 72 64 65 72 2c 20 73  ages in order, s
2cfbe 74 61 72 74 69 6e 67 20 77 69 74 68 20 74 68 65  tarting with the
2cfbf 20 6e 75 6d 65 72 69 63 61 6c 6c 79 20 0a 20 20   numerically .  
2cfc0 2a 2a 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d  ** largest root-
2cfc1 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 54 68 69  page number. Thi
2cfc2 73 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61  s guarantees tha
2cfc3 74 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 72 6f  t none of the ro
2cfc4 6f 74 2d 70 61 67 65 73 0a 20 20 2a 2a 20 74 6f  ot-pages.  ** to
2cfc5 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 69 73   be destroyed is
2cfc6 20 72 65 6c 6f 63 61 74 65 64 20 62 79 20 61 6e   relocated by an
2cfc7 20 65 61 72 6c 69 65 72 20 4f 50 5f 44 65 73 74   earlier OP_Dest
2cfc8 72 6f 79 2e 20 69 2e 65 2e 20 69 66 20 74 68 65  roy. i.e. if the
2cfc9 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  .  ** following 
2cfca 77 65 72 65 20 63 6f 64 65 64 3a 0a 20 20 2a 2a  were coded:.  **
2cfcb 0a 20 20 2a 2a 20 4f 50 5f 44 65 73 74 72 6f 79  .  ** OP_Destroy
2cfcc 20 34 20 30 0a 20 20 2a 2a 20 2e 2e 2e 0a 20 20   4 0.  ** ....  
2cfcd 2a 2a 20 4f 50 5f 44 65 73 74 72 6f 79 20 35 20  ** OP_Destroy 5 
2cfce 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 6e 64 20  0.  **.  ** and 
2cfcf 72 6f 6f 74 20 70 61 67 65 20 35 20 68 61 70 70  root page 5 happ
2cfd0 65 6e 65 64 20 74 6f 20 62 65 20 74 68 65 20 6c  ened to be the l
2cfd1 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65  argest root-page
2cfd2 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 20   number in the. 
2cfd3 20 2a 2a 20 64 61 74 61 62 61 73 65 2c 20 74 68   ** database, th
2cfd4 65 6e 20 72 6f 6f 74 20 70 61 67 65 20 35 20 77  en root page 5 w
2cfd5 6f 75 6c 64 20 62 65 20 6d 6f 76 65 64 20 74 6f  ould be moved to
2cfd6 20 70 61 67 65 20 34 20 62 79 20 74 68 65 20 0a   page 4 by the .
2cfd7 20 20 2a 2a 20 22 4f 50 5f 44 65 73 74 72 6f 79    ** "OP_Destroy
2cfd8 20 34 20 30 22 20 6f 70 63 6f 64 65 2e 20 54 68   4 0" opcode. Th
2cfd9 65 20 73 75 62 73 65 71 75 65 6e 74 20 22 4f 50  e subsequent "OP
2cfda 5f 44 65 73 74 72 6f 79 20 35 20 30 22 20 77 6f  _Destroy 5 0" wo
2cfdb 75 6c 64 20 68 69 74 0a 20 20 2a 2a 20 61 20 66  uld hit.  ** a f
2cfdc 72 65 65 2d 6c 69 73 74 20 70 61 67 65 2e 0a 20  ree-list page.. 
2cfdd 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 20 3d   */.  int iTab =
2cfde 20 70 54 61 62 2d 3e 74 6e 75 6d 3b 0a 20 20 69   pTab->tnum;.  i
2cfdf 6e 74 20 69 44 65 73 74 72 6f 79 65 64 20 3d 20  nt iDestroyed = 
2cfe0 30 3b 0a 0a 20 20 77 68 69 6c 65 28 20 31 20 29  0;..  while( 1 )
2cfe1 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  {.    Index *pId
2cfe2 78 3b 0a 20 20 20 20 69 6e 74 20 69 4c 61 72 67  x;.    int iLarg
2cfe3 65 73 74 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66  est = 0;..    if
2cfe4 28 20 69 44 65 73 74 72 6f 79 65 64 3d 3d 30 20  ( iDestroyed==0 
2cfe5 7c 7c 20 69 54 61 62 3c 69 44 65 73 74 72 6f 79  || iTab<iDestroy
2cfe6 65 64 20 29 7b 0a 20 20 20 20 20 20 69 4c 61 72  ed ){.      iLar
2cfe7 67 65 73 74 20 3d 20 69 54 61 62 3b 0a 20 20 20  gest = iTab;.   
2cfe8 20 7d 0a 20 20 20 20 66 6f 72 28 70 49 64 78 3d   }.    for(pIdx=
2cfe9 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
2cfea 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
2cfeb 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 6e 74  Next){.      int
2cfec 20 69 49 64 78 20 3d 20 70 49 64 78 2d 3e 74 6e   iIdx = pIdx->tn
2cfed 75 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  um;.      assert
2cfee 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d  ( pIdx->pSchema=
2cfef 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29  =pTab->pSchema )
2cff0 3b 0a 20 20 20 20 20 20 69 66 28 20 28 69 44 65  ;.      if( (iDe
2cff1 73 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 28 69  stroyed==0 || (i
2cff2 49 64 78 3c 69 44 65 73 74 72 6f 79 65 64 29 29  Idx<iDestroyed))
2cff3 20 26 26 20 69 49 64 78 3e 69 4c 61 72 67 65 73   && iIdx>iLarges
2cff4 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c 61  t ){.        iLa
2cff5 72 67 65 73 74 20 3d 20 69 49 64 78 3b 0a 20 20  rgest = iIdx;.  
2cff6 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2cff7 69 66 28 20 69 4c 61 72 67 65 73 74 3d 3d 30 20  if( iLargest==0 
2cff8 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  ){.      return;
2cff9 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2cffa 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69    int iDb = sqli
2cffb 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
2cffc 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
2cffd 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  b->pSchema);.   
2cffe 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d     assert( iDb>=
2cfff 30 20 26 26 20 69 44 62 3c 70 50 61 72 73 65 2d  0 && iDb<pParse-
2d000 3e 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20  >db->nDb );.    
2d001 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67    destroyRootPag
2d002 65 28 70 50 61 72 73 65 2c 20 69 4c 61 72 67 65  e(pParse, iLarge
2d003 73 74 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  st, iDb);.      
2d004 69 44 65 73 74 72 6f 79 65 64 20 3d 20 69 4c 61  iDestroyed = iLa
2d005 72 67 65 73 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  rgest;.    }.  }
2d006 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
2d007 20 52 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20   Remove entries 
2d008 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f  from the sqlite_
2d009 73 74 61 74 4e 20 74 61 62 6c 65 73 20 28 66 6f  statN tables (fo
2d00a 72 20 4e 20 69 6e 20 28 31 2c 32 2c 33 29 29 0a  r N in (1,2,3)).
2d00b 2a 2a 20 61 66 74 65 72 20 61 20 44 52 4f 50 20  ** after a DROP 
2d00c 49 4e 44 45 58 20 6f 72 20 44 52 4f 50 20 54 41  INDEX or DROP TA
2d00d 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a  BLE command..*/.
2d00e 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
2d00f 74 65 33 43 6c 65 61 72 53 74 61 74 54 61 62 6c  te3ClearStatTabl
2d010 65 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  es(.  Parse *pPa
2d011 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rse,         /* 
2d012 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
2d013 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ext */.  int iDb
2d014 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2d015 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
2d016 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 6f 6e 73  number */.  cons
2d017 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20  t char *zType,  
2d018 20 20 20 2f 2a 20 22 69 64 78 22 20 6f 72 20 22     /* "idx" or "
2d019 74 62 6c 22 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tbl" */.  const 
2d01a 63 68 61 72 20 2a 7a 4e 61 6d 65 20 20 20 20 20  char *zName     
2d01b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 69 6e 64 65   /* Name of inde
2d01c 78 20 6f 72 20 74 61 62 6c 65 20 2a 2f 0a 29 7b  x or table */.){
2d01d 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73  .  int i;.  cons
2d01e 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 20  t char *zDbName 
2d01f 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  = pParse->db->aD
2d020 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
2d021 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 33 3b 20 69  for(i=1; i<=3; i
2d022 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 54  ++){.    char zT
2d023 61 62 5b 32 34 5d 3b 0a 20 20 20 20 73 71 6c 69  ab[24];.    sqli
2d024 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
2d025 65 6f 66 28 7a 54 61 62 29 2c 7a 54 61 62 2c 22  eof(zTab),zTab,"
2d026 73 71 6c 69 74 65 5f 73 74 61 74 25 64 22 2c 69  sqlite_stat%d",i
2d027 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  );.    if( sqlit
2d028 65 33 46 69 6e 64 54 61 62 6c 65 28 70 50 61 72  e3FindTable(pPar
2d029 73 65 2d 3e 64 62 2c 20 7a 54 61 62 2c 20 7a 44  se->db, zTab, zD
2d02a 62 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20  bName) ){.      
2d02b 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
2d02c 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  se(pParse,.     
2d02d 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20     "DELETE FROM 
2d02e 25 51 2e 25 73 20 57 48 45 52 45 20 25 73 3d 25  %Q.%s WHERE %s=%
2d02f 51 22 2c 0a 20 20 20 20 20 20 20 20 7a 44 62 4e  Q",.        zDbN
2d030 61 6d 65 2c 20 7a 54 61 62 2c 20 7a 54 79 70 65  ame, zTab, zType
2d031 2c 20 7a 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b  , zName.      );
2d032 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
2d033 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
2d034 65 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62 6c  e to drop a tabl
2d035 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
2d036 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2d037 33 43 6f 64 65 44 72 6f 70 54 61 62 6c 65 28 50  3CodeDropTable(P
2d038 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
2d039 62 6c 65 20 2a 70 54 61 62 2c 20 69 6e 74 20 69  ble *pTab, int i
2d03a 44 62 2c 20 69 6e 74 20 69 73 56 69 65 77 29 7b  Db, int isView){
2d03b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71  .  Vdbe *v;.  sq
2d03c 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2d03d 73 65 2d 3e 64 62 3b 0a 20 20 54 72 69 67 67 65  se->db;.  Trigge
2d03e 72 20 2a 70 54 72 69 67 67 65 72 3b 0a 20 20 44  r *pTrigger;.  D
2d03f 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  b *pDb = &db->aD
2d040 62 5b 69 44 62 5d 3b 0a 0a 20 20 76 20 3d 20 73  b[iDb];..  v = s
2d041 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
2d042 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28  arse);.  assert(
2d043 20 76 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74   v!=0 );.  sqlit
2d044 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
2d045 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c  ation(pParse, 1,
2d046 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20   iDb);..#ifndef 
2d047 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2d048 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 49  UALTABLE.  if( I
2d049 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
2d04a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
2d04b 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56 42  eAddOp0(v, OP_VB
2d04c 65 67 69 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  egin);.  }.#endi
2d04d 66 0a 0a 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c  f..  /* Drop all
2d04e 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 69   triggers associ
2d04f 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74 61  ated with the ta
2d050 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65  ble being droppe
2d051 64 2e 20 43 6f 64 65 0a 20 20 2a 2a 20 69 73 20  d. Code.  ** is 
2d052 67 65 6e 65 72 61 74 65 64 20 74 6f 20 72 65 6d  generated to rem
2d053 6f 76 65 20 65 6e 74 72 69 65 73 20 66 72 6f 6d  ove entries from
2d054 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 61   sqlite_master a
2d055 6e 64 2f 6f 72 0a 20 20 2a 2a 20 73 71 6c 69 74  nd/or.  ** sqlit
2d056 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 69 66  e_temp_master if
2d057 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2f 0a   required..  */.
2d058 20 20 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c    pTrigger = sql
2d059 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28  ite3TriggerList(
2d05a 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20  pParse, pTab);. 
2d05b 20 77 68 69 6c 65 28 20 70 54 72 69 67 67 65 72   while( pTrigger
2d05c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
2d05d 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d  pTrigger->pSchem
2d05e 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  a==pTab->pSchema
2d05f 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 70 54 72   || .        pTr
2d060 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d  igger->pSchema==
2d061 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65  db->aDb[1].pSche
2d062 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ma );.    sqlite
2d063 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28  3DropTriggerPtr(
2d064 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
2d065 29 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 20  );.    pTrigger 
2d066 3d 20 70 54 72 69 67 67 65 72 2d 3e 70 4e 65 78  = pTrigger->pNex
2d067 74 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  t;.  }..#ifndef 
2d068 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
2d069 49 4e 43 52 45 4d 45 4e 54 0a 20 20 2f 2a 20 52  INCREMENT.  /* R
2d06a 65 6d 6f 76 65 20 61 6e 79 20 65 6e 74 72 69 65  emove any entrie
2d06b 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f  s of the sqlite_
2d06c 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 61  sequence table a
2d06d 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 20  ssociated with. 
2d06e 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 62 65   ** the table be
2d06f 69 6e 67 20 64 72 6f 70 70 65 64 2e 20 54 68 69  ing dropped. Thi
2d070 73 20 69 73 20 64 6f 6e 65 20 62 65 66 6f 72 65  s is done before
2d071 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 64 72   the table is dr
2d072 6f 70 70 65 64 0a 20 20 2a 2a 20 61 74 20 74 68  opped.  ** at th
2d073 65 20 62 74 72 65 65 20 6c 65 76 65 6c 2c 20 69  e btree level, i
2d074 6e 20 63 61 73 65 20 74 68 65 20 73 71 6c 69 74  n case the sqlit
2d075 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65  e_sequence table
2d076 20 6e 65 65 64 73 20 74 6f 0a 20 20 2a 2a 20 6d   needs to.  ** m
2d077 6f 76 65 20 61 73 20 61 20 72 65 73 75 6c 74 20  ove as a result 
2d078 6f 66 20 74 68 65 20 64 72 6f 70 20 28 63 61 6e  of the drop (can
2d079 20 68 61 70 70 65 6e 20 69 6e 20 61 75 74 6f 2d   happen in auto-
2d07a 76 61 63 75 75 6d 20 6d 6f 64 65 29 2e 0a 20 20  vacuum mode)..  
2d07b 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74  */.  if( pTab->t
2d07c 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74  abFlags & TF_Aut
2d07d 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20  oincrement ){.  
2d07e 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
2d07f 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
2d080 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20     "DELETE FROM 
2d081 25 51 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  %Q.sqlite_sequen
2d082 63 65 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51  ce WHERE name=%Q
2d083 22 2c 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e  ",.      pDb->zN
2d084 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ame, pTab->zName
2d085 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 23 65 6e 64  .    );.  }.#end
2d086 69 66 0a 0a 20 20 2f 2a 20 44 72 6f 70 20 61 6c  if..  /* Drop al
2d087 6c 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20  l SQLITE_MASTER 
2d088 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20  table and index 
2d089 65 6e 74 72 69 65 73 20 74 68 61 74 20 72 65 66  entries that ref
2d08a 65 72 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 74  er to the.  ** t
2d08b 61 62 6c 65 2e 20 54 68 65 20 70 72 6f 67 72 61  able. The progra
2d08c 6d 20 6e 61 6d 65 20 6c 6f 6f 70 73 20 74 68 72  m name loops thr
2d08d 6f 75 67 68 20 74 68 65 20 6d 61 73 74 65 72 20  ough the master 
2d08e 74 61 62 6c 65 20 61 6e 64 20 64 65 6c 65 74 65  table and delete
2d08f 73 0a 20 20 2a 2a 20 65 76 65 72 79 20 72 6f 77  s.  ** every row
2d090 20 74 68 61 74 20 72 65 66 65 72 73 20 74 6f 20   that refers to 
2d091 61 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 73  a table of the s
2d092 61 6d 65 20 6e 61 6d 65 20 61 73 20 74 68 65 20  ame name as the 
2d093 6f 6e 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 64  one being.  ** d
2d094 72 6f 70 70 65 64 2e 20 54 72 69 67 67 65 72 73  ropped. Triggers
2d095 20 61 72 65 20 68 61 6e 64 6c 65 64 20 73 65 70   are handled sep
2d096 65 72 61 74 65 6c 79 20 62 65 63 61 75 73 65 20  erately because 
2d097 61 20 74 72 69 67 67 65 72 20 63 61 6e 20 62 65  a trigger can be
2d098 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 69 6e  .  ** created in
2d099 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61   the temp databa
2d09a 73 65 20 74 68 61 74 20 72 65 66 65 72 73 20 74  se that refers t
2d09b 6f 20 61 20 74 61 62 6c 65 20 69 6e 20 61 6e 6f  o a table in ano
2d09c 74 68 65 72 0a 20 20 2a 2a 20 64 61 74 61 62 61  ther.  ** databa
2d09d 73 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  se..  */.  sqlit
2d09e 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
2d09f 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 44 45  arse, .      "DE
2d0a0 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20  LETE FROM %Q.%s 
2d0a1 57 48 45 52 45 20 74 62 6c 5f 6e 61 6d 65 3d 25  WHERE tbl_name=%
2d0a2 51 20 61 6e 64 20 74 79 70 65 21 3d 27 74 72 69  Q and type!='tri
2d0a3 67 67 65 72 27 22 2c 0a 20 20 20 20 20 20 70 44  gger'",.      pD
2d0a4 62 2d 3e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41  b->zName, SCHEMA
2d0a5 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70 54 61  _TABLE(iDb), pTa
2d0a6 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  b->zName);.  if(
2d0a7 20 21 69 73 56 69 65 77 20 26 26 20 21 49 73 56   !isView && !IsV
2d0a8 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
2d0a9 20 20 20 20 64 65 73 74 72 6f 79 54 61 62 6c 65      destroyTable
2d0aa 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a  (pParse, pTab);.
2d0ab 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65    }..  /* Remove
2d0ac 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79   the table entry
2d0ad 20 66 72 6f 6d 20 53 51 4c 69 74 65 27 73 20 69   from SQLite's i
2d0ae 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 61  nternal schema a
2d0af 6e 64 20 6d 6f 64 69 66 79 0a 20 20 2a 2a 20 74  nd modify.  ** t
2d0b0 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  he schema cookie
2d0b1 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 49 73 56  ..  */.  if( IsV
2d0b2 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
2d0b3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2d0b4 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 44 65 73  ddOp4(v, OP_VDes
2d0b5 74 72 6f 79 2c 20 69 44 62 2c 20 30 2c 20 30 2c  troy, iDb, 0, 0,
2d0b6 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29   pTab->zName, 0)
2d0b7 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
2d0b8 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2d0b9 44 72 6f 70 54 61 62 6c 65 2c 20 69 44 62 2c 20  DropTable, iDb, 
2d0ba 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  0, 0, pTab->zNam
2d0bb 65 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  e, 0);.  sqlite3
2d0bc 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61  ChangeCookie(pPa
2d0bd 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c  rse, iDb);.  sql
2d0be 69 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28  iteViewResetAll(
2d0bf 64 62 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a  db, iDb);.}../*.
2d0c0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2d0c1 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 6f 20  is called to do 
2d0c2 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 20 44 52  the work of a DR
2d0c3 4f 50 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  OP TABLE stateme
2d0c4 6e 74 2e 0a 2a 2a 20 70 4e 61 6d 65 20 69 73 20  nt..** pName is 
2d0c5 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
2d0c6 74 61 62 6c 65 20 74 6f 20 62 65 20 64 72 6f 70  table to be drop
2d0c7 70 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ped..*/.SQLITE_P
2d0c8 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2d0c9 74 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61 72  te3DropTable(Par
2d0ca 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c  se *pParse, SrcL
2d0cb 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20  ist *pName, int 
2d0cc 69 73 56 69 65 77 2c 20 69 6e 74 20 6e 6f 45 72  isView, int noEr
2d0cd 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  r){.  Table *pTa
2d0ce 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  b;.  Vdbe *v;.  
2d0cf 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2d0d0 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
2d0d1 69 44 62 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e  iDb;..  if( db->
2d0d2 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
2d0d3 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
2d0d4 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20  op_table;.  }.  
2d0d5 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
2d0d6 6e 45 72 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73  nErr==0 );.  ass
2d0d7 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63  ert( pName->nSrc
2d0d8 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 6e 6f 45  ==1 );.  if( noE
2d0d9 72 72 20 29 20 64 62 2d 3e 73 75 70 70 72 65 73  rr ) db->suppres
2d0da 73 45 72 72 2b 2b 3b 0a 20 20 70 54 61 62 20 3d  sErr++;.  pTab =
2d0db 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61   sqlite3LocateTa
2d0dc 62 6c 65 49 74 65 6d 28 70 50 61 72 73 65 2c 20  bleItem(pParse, 
2d0dd 69 73 56 69 65 77 2c 20 26 70 4e 61 6d 65 2d 3e  isView, &pName->
2d0de 61 5b 30 5d 29 3b 0a 20 20 69 66 28 20 6e 6f 45  a[0]);.  if( noE
2d0df 72 72 20 29 20 64 62 2d 3e 73 75 70 70 72 65 73  rr ) db->suppres
2d0e0 73 45 72 72 2d 2d 3b 0a 0a 20 20 69 66 28 20 70  sErr--;..  if( p
2d0e1 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  Tab==0 ){.    if
2d0e2 28 20 6e 6f 45 72 72 20 29 20 73 71 6c 69 74 65  ( noErr ) sqlite
2d0e3 33 43 6f 64 65 56 65 72 69 66 79 4e 61 6d 65 64  3CodeVerifyNamed
2d0e4 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 70  Schema(pParse, p
2d0e5 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61  Name->a[0].zData
2d0e6 62 61 73 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  base);.    goto 
2d0e7 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b  exit_drop_table;
2d0e8 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c  .  }.  iDb = sql
2d0e9 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
2d0ea 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  x(db, pTab->pSch
2d0eb 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ema);.  assert( 
2d0ec 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
2d0ed 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 49  ->nDb );..  /* I
2d0ee 66 20 70 54 61 62 20 69 73 20 61 20 76 69 72 74  f pTab is a virt
2d0ef 75 61 6c 20 74 61 62 6c 65 2c 20 63 61 6c 6c 20  ual table, call 
2d0f0 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d  ViewGetColumnNam
2d0f1 65 73 28 29 20 74 6f 20 65 6e 73 75 72 65 0a 20  es() to ensure. 
2d0f2 20 2a 2a 20 69 74 20 69 73 20 69 6e 69 74 69 61   ** it is initia
2d0f3 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  lized..  */.  if
2d0f4 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
2d0f5 29 20 26 26 20 73 71 6c 69 74 65 33 56 69 65 77  ) && sqlite3View
2d0f6 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  GetColumnNames(p
2d0f7 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a  Parse, pTab) ){.
2d0f8 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
2d0f9 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 69  op_table;.  }.#i
2d0fa 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2d0fb 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
2d0fc 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65    {.    int code
2d0fd 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
2d0fe 20 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d 41 5f   *zTab = SCHEMA_
2d0ff 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 20 20  TABLE(iDb);.    
2d100 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
2d101 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
2d102 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  Name;.    const 
2d103 63 68 61 72 20 2a 7a 41 72 67 32 20 3d 20 30 3b  char *zArg2 = 0;
2d104 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2d105 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2d106 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c  , SQLITE_DELETE,
2d107 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 29 7b   zTab, 0, zDb)){
2d108 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
2d109 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20  _drop_table;.   
2d10a 20 7d 0a 20 20 20 20 69 66 28 20 69 73 56 69 65   }.    if( isVie
2d10b 77 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  w ){.      if( !
2d10c 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
2d10d 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  Db==1 ){.       
2d10e 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44   code = SQLITE_D
2d10f 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 3b 0a 20  ROP_TEMP_VIEW;. 
2d110 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2d111 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
2d112 45 5f 44 52 4f 50 5f 56 49 45 57 3b 0a 20 20 20  E_DROP_VIEW;.   
2d113 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
2d114 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2d115 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20  TABLE.    }else 
2d116 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
2d117 61 62 29 20 29 7b 0a 20 20 20 20 20 20 63 6f 64  ab) ){.      cod
2d118 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  e = SQLITE_DROP_
2d119 56 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7a 41  VTABLE;.      zA
2d11a 72 67 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74  rg2 = sqlite3Get
2d11b 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29  VTable(db, pTab)
2d11c 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61 6d 65 3b 0a 23  ->pMod->zName;.#
2d11d 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b  endif.    }else{
2d11e 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54  .      if( !OMIT
2d11f 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d  _TEMPDB && iDb==
2d120 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64  1 ){.        cod
2d121 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  e = SQLITE_DROP_
2d122 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20  TEMP_TABLE;.    
2d123 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2d124 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44   code = SQLITE_D
2d125 52 4f 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20  ROP_TABLE;.     
2d126 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
2d127 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
2d128 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20  k(pParse, code, 
2d129 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 41 72  pTab->zName, zAr
2d12a 67 32 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20  g2, zDb) ){.    
2d12b 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
2d12c 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20  _table;.    }.  
2d12d 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
2d12e 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
2d12f 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 70 54  QLITE_DELETE, pT
2d130 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44  ab->zName, 0, zD
2d131 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
2d132 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65   exit_drop_table
2d133 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
2d134 69 66 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  if.  if( sqlite3
2d135 53 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a  StrNICmp(pTab->z
2d136 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c  Name, "sqlite_",
2d137 20 37 29 3d 3d 30 20 0a 20 20 20 20 26 26 20 73   7)==0 .    && s
2d138 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70  qlite3StrNICmp(p
2d139 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c  Tab->zName, "sql
2d13a 69 74 65 5f 73 74 61 74 22 2c 20 31 31 29 21 3d  ite_stat", 11)!=
2d13b 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
2d13c 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2d13d 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 6e   "table %s may n
2d13e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22 2c 20  ot be dropped", 
2d13f 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
2d140 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
2d141 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66  _table;.  }..#if
2d142 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2d143 5f 56 49 45 57 0a 20 20 2f 2a 20 45 6e 73 75 72  _VIEW.  /* Ensur
2d144 65 20 44 52 4f 50 20 54 41 42 4c 45 20 69 73 20  e DROP TABLE is 
2d145 6e 6f 74 20 75 73 65 64 20 6f 6e 20 61 20 76 69  not used on a vi
2d146 65 77 2c 20 61 6e 64 20 44 52 4f 50 20 56 49 45  ew, and DROP VIE
2d147 57 20 69 73 20 6e 6f 74 20 75 73 65 64 0a 20 20  W is not used.  
2d148 2a 2a 20 6f 6e 20 61 20 74 61 62 6c 65 2e 0a 20  ** on a table.. 
2d149 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77   */.  if( isView
2d14a 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63   && pTab->pSelec
2d14b 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  t==0 ){.    sqli
2d14c 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2d14d 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20 54 41  se, "use DROP TA
2d14e 42 4c 45 20 74 6f 20 64 65 6c 65 74 65 20 74 61  BLE to delete ta
2d14f 62 6c 65 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a  ble %s", pTab->z
2d150 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
2d151 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b  exit_drop_table;
2d152 0a 20 20 7d 0a 20 20 69 66 28 20 21 69 73 56 69  .  }.  if( !isVi
2d153 65 77 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c  ew && pTab->pSel
2d154 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ect ){.    sqlit
2d155 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2d156 65 2c 20 22 75 73 65 20 44 52 4f 50 20 56 49 45  e, "use DROP VIE
2d157 57 20 74 6f 20 64 65 6c 65 74 65 20 76 69 65 77  W to delete view
2d158 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d   %s", pTab->zNam
2d159 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69  e);.    goto exi
2d15a 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20  t_drop_table;.  
2d15b 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47  }.#endif..  /* G
2d15c 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
2d15d 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65  remove the table
2d15e 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 74 65 72   from the master
2d15f 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 6e 20 64   table.  ** on d
2d160 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20  isk..  */.  v = 
2d161 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2d162 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20  Parse);.  if( v 
2d163 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  ){.    sqlite3Be
2d164 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
2d165 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62  n(pParse, 1, iDb
2d166 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6c  );.    sqlite3Cl
2d167 65 61 72 53 74 61 74 54 61 62 6c 65 73 28 70 50  earStatTables(pP
2d168 61 72 73 65 2c 20 69 44 62 2c 20 22 74 62 6c 22  arse, iDb, "tbl"
2d169 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
2d16a 20 20 20 20 73 71 6c 69 74 65 33 46 6b 44 72 6f      sqlite3FkDro
2d16b 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  pTable(pParse, p
2d16c 4e 61 6d 65 2c 20 70 54 61 62 29 3b 0a 20 20 20  Name, pTab);.   
2d16d 20 73 71 6c 69 74 65 33 43 6f 64 65 44 72 6f 70   sqlite3CodeDrop
2d16e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  Table(pParse, pT
2d16f 61 62 2c 20 69 44 62 2c 20 69 73 56 69 65 77 29  ab, iDb, isView)
2d170 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f 64 72 6f 70  ;.  }..exit_drop
2d171 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69 74 65  _table:.  sqlite
2d172 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64  3SrcListDelete(d
2d173 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a  b, pName);.}../*
2d174 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2d175 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 72   is called to cr
2d176 65 61 74 65 20 61 20 6e 65 77 20 66 6f 72 65 69  eate a new forei
2d177 67 6e 20 6b 65 79 20 6f 6e 20 74 68 65 20 74 61  gn key on the ta
2d178 62 6c 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79  ble.** currently
2d179 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
2d17a 69 6f 6e 2e 20 20 70 46 72 6f 6d 43 6f 6c 20 64  ion.  pFromCol d
2d17b 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68 20  etermines which 
2d17c 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68  columns.** in th
2d17d 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 20  e current table 
2d17e 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 6f 72  point to the for
2d17f 65 69 67 6e 20 6b 65 79 2e 20 20 49 66 20 70 46  eign key.  If pF
2d180 72 6f 6d 43 6f 6c 3d 3d 30 20 74 68 65 6e 0a 2a  romCol==0 then.*
2d181 2a 20 63 6f 6e 6e 65 63 74 20 74 68 65 20 6b 65  * connect the ke
2d182 79 20 74 6f 20 74 68 65 20 6c 61 73 74 20 63 6f  y to the last co
2d183 6c 75 6d 6e 20 69 6e 73 65 72 74 65 64 2e 20 20  lumn inserted.  
2d184 70 54 6f 20 69 73 20 74 68 65 20 6e 61 6d 65 20  pTo is the name 
2d185 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20  of.** the table 
2d186 72 65 66 65 72 72 65 64 20 74 6f 2e 20 20 70 54  referred to.  pT
2d187 6f 43 6f 6c 20 69 73 20 61 20 6c 69 73 74 20 6f  oCol is a list o
2d188 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  f tables in the 
2d189 6f 74 68 65 72 0a 2a 2a 20 70 54 6f 20 74 61 62  other.** pTo tab
2d18a 6c 65 20 74 68 61 74 20 74 68 65 20 66 6f 72 65  le that the fore
2d18b 69 67 6e 20 6b 65 79 20 70 6f 69 6e 74 73 20 74  ign key points t
2d18c 6f 2e 20 20 66 6c 61 67 73 20 63 6f 6e 74 61 69  o.  flags contai
2d18d 6e 73 20 61 6c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d  ns all.** inform
2d18e 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
2d18f 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74  conflict resolut
2d190 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 73  ion algorithms s
2d191 70 65 63 69 66 69 65 64 0a 2a 2a 20 69 6e 20 74  pecified.** in t
2d192 68 65 20 4f 4e 20 44 45 4c 45 54 45 2c 20 4f 4e  he ON DELETE, ON
2d193 20 55 50 44 41 54 45 20 61 6e 64 20 4f 4e 20 49   UPDATE and ON I
2d194 4e 53 45 52 54 20 63 6c 61 75 73 65 73 2e 0a 2a  NSERT clauses..*
2d195 2a 0a 2a 2a 20 41 6e 20 46 4b 65 79 20 73 74 72  *.** An FKey str
2d196 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65  ucture is create
2d197 64 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20 74  d and added to t
2d198 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74  he table current
2d199 6c 79 0a 2a 2a 20 75 6e 64 65 72 20 63 6f 6e 73  ly.** under cons
2d19a 74 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20  truction in the 
2d19b 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
2d19c 65 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54  e field..**.** T
2d19d 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69  he foreign key i
2d19e 73 20 73 65 74 20 66 6f 72 20 49 4d 4d 45 44 49  s set for IMMEDI
2d19f 41 54 45 20 70 72 6f 63 65 73 73 69 6e 67 2e 20  ATE processing. 
2d1a0 20 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 61   A subsequent ca
2d1a1 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  ll.** to sqlite3
2d1a2 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28  DeferForeignKey(
2d1a3 29 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74  ) might change t
2d1a4 68 69 73 20 74 6f 20 44 45 46 45 52 52 45 44 2e  his to DEFERRED.
2d1a5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2d1a6 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
2d1a7 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28  reateForeignKey(
2d1a8 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2d1a9 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69  ,       /* Parsi
2d1aa 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2d1ab 45 78 70 72 4c 69 73 74 20 2a 70 46 72 6f 6d 43  ExprList *pFromC
2d1ac 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20  ol,  /* Columns 
2d1ad 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 74 68  in this table th
2d1ae 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 74 68 65  at point to othe
2d1af 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b  r table */.  Tok
2d1b0 65 6e 20 2a 70 54 6f 2c 20 20 20 20 20 20 20 20  en *pTo,        
2d1b1 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
2d1b2 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a   other table */.
2d1b3 20 20 45 78 70 72 4c 69 73 74 20 2a 70 54 6f 43    ExprList *pToC
2d1b4 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e  ol,    /* Column
2d1b5 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 74  s in the other t
2d1b6 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  able */.  int fl
2d1b7 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f  ags            /
2d1b8 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c  * Conflict resol
2d1b9 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73  ution algorithms
2d1ba 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  . */.){.  sqlite
2d1bb 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2d1bc 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  db;.#ifndef SQLI
2d1bd 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
2d1be 4b 45 59 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65  KEY.  FKey *pFKe
2d1bf 79 20 3d 20 30 3b 0a 20 20 46 4b 65 79 20 2a 70  y = 0;.  FKey *p
2d1c0 4e 65 78 74 54 6f 3b 0a 20 20 54 61 62 6c 65 20  NextTo;.  Table 
2d1c1 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  *p = pParse->pNe
2d1c2 77 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 6e 42  wTable;.  int nB
2d1c3 79 74 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  yte;.  int i;.  
2d1c4 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 63 68 61 72  int nCol;.  char
2d1c5 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   *z;..  assert( 
2d1c6 70 54 6f 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  pTo!=0 );.  if( 
2d1c7 70 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41  p==0 || IN_DECLA
2d1c8 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 66  RE_VTAB ) goto f
2d1c9 6b 5f 65 6e 64 3b 0a 20 20 69 66 28 20 70 46 72  k_end;.  if( pFr
2d1ca 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20  omCol==0 ){.    
2d1cb 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d 3e 6e 43  int iCol = p->nC
2d1cc 6f 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 4e 45  ol-1;.    if( NE
2d1cd 56 45 52 28 69 43 6f 6c 3c 30 29 20 29 20 67 6f  VER(iCol<0) ) go
2d1ce 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 69  to fk_end;.    i
2d1cf 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f  f( pToCol && pTo
2d1d0 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b  Col->nExpr!=1 ){
2d1d1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2d1d2 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2d1d3 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 25  foreign key on %
2d1d4 73 22 0a 20 20 20 20 20 20 20 20 20 22 20 73 68  s".         " sh
2d1d5 6f 75 6c 64 20 72 65 66 65 72 65 6e 63 65 20 6f  ould reference o
2d1d6 6e 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 6f  nly one column o
2d1d7 66 20 74 61 62 6c 65 20 25 54 22 2c 0a 20 20 20  f table %T",.   
2d1d8 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 43        p->aCol[iC
2d1d9 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 70 54 6f 29 3b  ol].zName, pTo);
2d1da 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65  .      goto fk_e
2d1db 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 43  nd;.    }.    nC
2d1dc 6f 6c 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20  ol = 1;.  }else 
2d1dd 69 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54  if( pToCol && pT
2d1de 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 70 46 72  oCol->nExpr!=pFr
2d1df 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 20 29 7b 0a  omCol->nExpr ){.
2d1e0 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2d1e1 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20  Msg(pParse,.    
2d1e2 20 20 20 20 22 6e 75 6d 62 65 72 20 6f 66 20 63      "number of c
2d1e3 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69 67  olumns in foreig
2d1e4 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 6d  n key does not m
2d1e5 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65 72 20  atch the number 
2d1e6 6f 66 20 22 0a 20 20 20 20 20 20 20 20 22 63 6f  of ".        "co
2d1e7 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 66  lumns in the ref
2d1e8 65 72 65 6e 63 65 64 20 74 61 62 6c 65 22 29 3b  erenced table");
2d1e9 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64  .    goto fk_end
2d1ea 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
2d1eb 43 6f 6c 20 3d 20 70 46 72 6f 6d 43 6f 6c 2d 3e  Col = pFromCol->
2d1ec 6e 45 78 70 72 3b 0a 20 20 7d 0a 20 20 6e 42 79  nExpr;.  }.  nBy
2d1ed 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 46 4b  te = sizeof(*pFK
2d1ee 65 79 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73  ey) + (nCol-1)*s
2d1ef 69 7a 65 6f 66 28 70 46 4b 65 79 2d 3e 61 43 6f  izeof(pFKey->aCo
2d1f0 6c 5b 30 5d 29 20 2b 20 70 54 6f 2d 3e 6e 20 2b  l[0]) + pTo->n +
2d1f1 20 31 3b 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c   1;.  if( pToCol
2d1f2 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
2d1f3 20 69 3c 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72   i<pToCol->nExpr
2d1f4 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 42  ; i++){.      nB
2d1f5 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 53 74  yte += sqlite3St
2d1f6 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d 3e 61  rlen30(pToCol->a
2d1f7 5b 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20 31 3b 0a  [i].zName) + 1;.
2d1f8 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65      }.  }.  pFKe
2d1f9 79 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  y = sqlite3DbMal
2d1fa 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74  locZero(db, nByt
2d1fb 65 20 29 3b 0a 20 20 69 66 28 20 70 46 4b 65 79  e );.  if( pFKey
2d1fc 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ==0 ){.    goto 
2d1fd 66 6b 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 46  fk_end;.  }.  pF
2d1fe 4b 65 79 2d 3e 70 46 72 6f 6d 20 3d 20 70 3b 0a  Key->pFrom = p;.
2d1ff 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72    pFKey->pNextFr
2d200 6f 6d 20 3d 20 70 2d 3e 70 46 4b 65 79 3b 0a 20  om = p->pFKey;. 
2d201 20 7a 20 3d 20 28 63 68 61 72 2a 29 26 70 46 4b   z = (char*)&pFK
2d202 65 79 2d 3e 61 43 6f 6c 5b 6e 43 6f 6c 5d 3b 0a  ey->aCol[nCol];.
2d203 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20 7a    pFKey->zTo = z
2d204 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54  ;.  memcpy(z, pT
2d205 6f 2d 3e 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a 20  o->z, pTo->n);. 
2d206 20 7a 5b 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b 0a   z[pTo->n] = 0;.
2d207 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65    sqlite3Dequote
2d208 28 7a 29 3b 0a 20 20 7a 20 2b 3d 20 70 54 6f 2d  (z);.  z += pTo-
2d209 3e 6e 2b 31 3b 0a 20 20 70 46 4b 65 79 2d 3e 6e  >n+1;.  pFKey->n
2d20a 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20 69 66  Col = nCol;.  if
2d20b 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b  ( pFromCol==0 ){
2d20c 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c  .    pFKey->aCol
2d20d 5b 30 5d 2e 69 46 72 6f 6d 20 3d 20 70 2d 3e 6e  [0].iFrom = p->n
2d20e 43 6f 6c 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Col-1;.  }else{.
2d20f 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
2d210 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
2d211 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f   int j;.      fo
2d212 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 43 6f 6c  r(j=0; j<p->nCol
2d213 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
2d214 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
2d215 6d 70 28 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e  mp(p->aCol[j].zN
2d216 61 6d 65 2c 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61  ame, pFromCol->a
2d217 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  [i].zName)==0 ){
2d218 0a 20 20 20 20 20 20 20 20 20 20 70 46 4b 65 79  .          pFKey
2d219 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 20  ->aCol[i].iFrom 
2d21a 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 62  = j;.          b
2d21b 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
2d21c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2d21d 28 20 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 7b 0a  ( j>=p->nCol ){.
2d21e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2d21f 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2d220 0a 20 20 20 20 20 20 20 20 20 20 22 75 6e 6b 6e  .          "unkn
2d221 6f 77 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25 73 5c  own column \"%s\
2d222 22 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79  " in foreign key
2d223 20 64 65 66 69 6e 69 74 69 6f 6e 22 2c 20 0a 20   definition", . 
2d224 20 20 20 20 20 20 20 20 20 70 46 72 6f 6d 43 6f           pFromCo
2d225 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a  l->a[i].zName);.
2d226 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f          goto fk_
2d227 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  end;.      }.   
2d228 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 6f   }.  }.  if( pTo
2d229 43 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  Col ){.    for(i
2d22a 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
2d22b 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20  {.      int n = 
2d22c 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2d22d 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61  pToCol->a[i].zNa
2d22e 6d 65 29 3b 0a 20 20 20 20 20 20 70 46 4b 65 79  me);.      pFKey
2d22f 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 20 3d  ->aCol[i].zCol =
2d230 20 7a 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   z;.      memcpy
2d231 28 7a 2c 20 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d  (z, pToCol->a[i]
2d232 2e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20  .zName, n);.    
2d233 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 20 20    z[n] = 0;.    
2d234 20 20 7a 20 2b 3d 20 6e 2b 31 3b 0a 20 20 20 20    z += n+1;.    
2d235 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d 3e 69  }.  }.  pFKey->i
2d236 73 44 65 66 65 72 72 65 64 20 3d 20 30 3b 0a 20  sDeferred = 0;. 
2d237 20 70 46 4b 65 79 2d 3e 61 41 63 74 69 6f 6e 5b   pFKey->aAction[
2d238 30 5d 20 3d 20 28 75 38 29 28 66 6c 61 67 73 20  0] = (u8)(flags 
2d239 26 20 30 78 66 66 29 3b 20 20 20 20 20 20 20 20  & 0xff);        
2d23a 20 20 20 20 2f 2a 20 4f 4e 20 44 45 4c 45 54 45      /* ON DELETE
2d23b 20 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 70 46 4b   action */.  pFK
2d23c 65 79 2d 3e 61 41 63 74 69 6f 6e 5b 31 5d 20 3d  ey->aAction[1] =
2d23d 20 28 75 38 29 28 28 66 6c 61 67 73 20 3e 3e 20   (u8)((flags >> 
2d23e 38 20 29 20 26 20 30 78 66 66 29 3b 20 20 20 20  8 ) & 0xff);    
2d23f 2f 2a 20 4f 4e 20 55 50 44 41 54 45 20 61 63 74  /* ON UPDATE act
2d240 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ion */..  assert
2d241 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d  ( sqlite3SchemaM
2d242 75 74 65 78 48 65 6c 64 28 64 62 2c 20 30 2c 20  utexHeld(db, 0, 
2d243 70 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20  p->pSchema) );. 
2d244 20 70 4e 65 78 74 54 6f 20 3d 20 28 46 4b 65 79   pNextTo = (FKey
2d245 20 2a 29 73 71 6c 69 74 65 33 48 61 73 68 49 6e   *)sqlite3HashIn
2d246 73 65 72 74 28 26 70 2d 3e 70 53 63 68 65 6d 61  sert(&p->pSchema
2d247 2d 3e 66 6b 65 79 48 61 73 68 2c 20 0a 20 20 20  ->fkeyHash, .   
2d248 20 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 73     pFKey->zTo, s
2d249 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70  qlite3Strlen30(p
2d24a 46 4b 65 79 2d 3e 7a 54 6f 29 2c 20 28 76 6f 69  FKey->zTo), (voi
2d24b 64 20 2a 29 70 46 4b 65 79 0a 20 20 29 3b 0a 20  d *)pFKey.  );. 
2d24c 20 69 66 28 20 70 4e 65 78 74 54 6f 3d 3d 70 46   if( pNextTo==pF
2d24d 4b 65 79 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  Key ){.    db->m
2d24e 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
2d24f 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64  .    goto fk_end
2d250 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 65 78  ;.  }.  if( pNex
2d251 74 54 6f 20 29 7b 0a 20 20 20 20 61 73 73 65 72  tTo ){.    asser
2d252 74 28 20 70 4e 65 78 74 54 6f 2d 3e 70 50 72 65  t( pNextTo->pPre
2d253 76 54 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 70 46  vTo==0 );.    pF
2d254 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 70  Key->pNextTo = p
2d255 4e 65 78 74 54 6f 3b 0a 20 20 20 20 70 4e 65 78  NextTo;.    pNex
2d256 74 54 6f 2d 3e 70 50 72 65 76 54 6f 20 3d 20 70  tTo->pPrevTo = p
2d257 46 4b 65 79 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  FKey;.  }..  /* 
2d258 4c 69 6e 6b 20 74 68 65 20 66 6f 72 65 69 67 6e  Link the foreign
2d259 20 6b 65 79 20 74 6f 20 74 68 65 20 74 61 62 6c   key to the tabl
2d25a 65 20 61 73 20 74 68 65 20 6c 61 73 74 20 73 74  e as the last st
2d25b 65 70 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 46  ep..  */.  p->pF
2d25c 4b 65 79 20 3d 20 70 46 4b 65 79 3b 0a 20 20 70  Key = pFKey;.  p
2d25d 46 4b 65 79 20 3d 20 30 3b 0a 0a 66 6b 5f 65 6e  FKey = 0;..fk_en
2d25e 64 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  d:.  sqlite3DbFr
2d25f 65 65 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a 23  ee(db, pFKey);.#
2d260 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
2d261 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  d(SQLITE_OMIT_FO
2d262 52 45 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a 20 20  REIGN_KEY) */.  
2d263 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2d264 65 6c 65 74 65 28 64 62 2c 20 70 46 72 6f 6d 43  elete(db, pFromC
2d265 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  ol);.  sqlite3Ex
2d266 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
2d267 20 70 54 6f 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a 0a   pToCol);.}../*.
2d268 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2d269 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61  is called when a
2d26a 6e 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45  n INITIALLY IMME
2d26b 44 49 41 54 45 20 6f 72 20 49 4e 49 54 49 41 4c  DIATE or INITIAL
2d26c 4c 59 20 44 45 46 45 52 52 45 44 0a 2a 2a 20 63  LY DEFERRED.** c
2d26d 6c 61 75 73 65 20 69 73 20 73 65 65 6e 20 61 73  lause is seen as
2d26e 20 70 61 72 74 20 6f 66 20 61 20 66 6f 72 65 69   part of a forei
2d26f 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f  gn key definitio
2d270 6e 2e 20 20 54 68 65 20 69 73 44 65 66 65 72 72  n.  The isDeferr
2d271 65 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  ed.** parameter 
2d272 69 73 20 31 20 66 6f 72 20 49 4e 49 54 49 41 4c  is 1 for INITIAL
2d273 4c 59 20 44 45 46 45 52 52 45 44 20 61 6e 64 20  LY DEFERRED and 
2d274 30 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20  0 for INITIALLY 
2d275 49 4d 4d 45 44 49 41 54 45 2e 0a 2a 2a 20 54 68  IMMEDIATE..** Th
2d276 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68  e behavior of th
2d277 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
2d278 63 72 65 61 74 65 64 20 66 6f 72 65 69 67 6e 20  created foreign 
2d279 6b 65 79 20 69 73 20 61 64 6a 75 73 74 65 64 0a  key is adjusted.
2d27a 2a 2a 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a  ** accordingly..
2d27b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2d27c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
2d27d 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 50 61  ferForeignKey(Pa
2d27e 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
2d27f 20 69 73 44 65 66 65 72 72 65 64 29 7b 0a 23 69   isDeferred){.#i
2d280 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2d281 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20  T_FOREIGN_KEY.  
2d282 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 46  Table *pTab;.  F
2d283 4b 65 79 20 2a 70 46 4b 65 79 3b 0a 20 20 69 66  Key *pFKey;.  if
2d284 28 20 28 70 54 61 62 20 3d 20 70 50 61 72 73 65  ( (pTab = pParse
2d285 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20  ->pNewTable)==0 
2d286 7c 7c 20 28 70 46 4b 65 79 20 3d 20 70 54 61 62  || (pFKey = pTab
2d287 2d 3e 70 46 4b 65 79 29 3d 3d 30 20 29 20 72 65  ->pFKey)==0 ) re
2d288 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  turn;.  assert( 
2d289 69 73 44 65 66 65 72 72 65 64 3d 3d 30 20 7c 7c  isDeferred==0 ||
2d28a 20 69 73 44 65 66 65 72 72 65 64 3d 3d 31 20 29   isDeferred==1 )
2d28b 3b 20 2f 2a 20 45 56 3a 20 52 2d 33 30 33 32 33  ; /* EV: R-30323
2d28c 2d 32 31 39 31 37 20 2a 2f 0a 20 20 70 46 4b 65  -21917 */.  pFKe
2d28d 79 2d 3e 69 73 44 65 66 65 72 72 65 64 20 3d 20  y->isDeferred = 
2d28e 28 75 38 29 69 73 44 65 66 65 72 72 65 64 3b 0a  (u8)isDeferred;.
2d28f 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
2d290 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
2d291 61 74 20 77 69 6c 6c 20 65 72 61 73 65 20 61 6e  at will erase an
2d292 64 20 72 65 66 69 6c 6c 20 69 6e 64 65 78 20 2a  d refill index *
2d293 70 49 64 78 2e 20 20 54 68 69 73 20 69 73 0a 2a  pIdx.  This is.*
2d294 2a 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61  * used to initia
2d295 6c 69 7a 65 20 61 20 6e 65 77 6c 79 20 63 72 65  lize a newly cre
2d296 61 74 65 64 20 69 6e 64 65 78 20 6f 72 20 74 6f  ated index or to
2d297 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a   recompute the.*
2d298 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20  * content of an 
2d299 69 6e 64 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  index in respons
2d29a 65 20 74 6f 20 61 20 52 45 49 4e 44 45 58 20 63  e to a REINDEX c
2d29b 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66  ommand..**.** if
2d29c 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20   memRootPage is 
2d29d 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 69 74  not negative, it
2d29e 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
2d29f 69 6e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a 2a  index is newly.*
2d2a0 2a 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20  * created.  The 
2d2a1 72 65 67 69 73 74 65 72 20 73 70 65 63 69 66 69  register specifi
2d2a2 65 64 20 62 79 20 6d 65 6d 52 6f 6f 74 50 61 67  ed by memRootPag
2d2a3 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a  e contains the.*
2d2a4 2a 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62  * root page numb
2d2a5 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e  er of the index.
2d2a6 20 20 49 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65    If memRootPage
2d2a7 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68   is negative, th
2d2a8 65 6e 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20  en.** the index 
2d2a9 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61  already exists a
2d2aa 6e 64 20 6d 75 73 74 20 62 65 20 63 6c 65 61 72  nd must be clear
2d2ab 65 64 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20  ed before being 
2d2ac 72 65 66 69 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20  refilled and.** 
2d2ad 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  the root page nu
2d2ae 6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65  mber of the inde
2d2af 78 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20  x is taken from 
2d2b0 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2e 0a 2a 2f  pIndex->tnum..*/
2d2b1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
2d2b2 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28  ite3RefillIndex(
2d2b3 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49  Parse *pParse, I
2d2b4 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 69 6e  ndex *pIndex, in
2d2b5 74 20 6d 65 6d 52 6f 6f 74 50 61 67 65 29 7b 0a  t memRootPage){.
2d2b6 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2d2b7 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b 20  pIndex->pTable; 
2d2b8 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68   /* The table th
2d2b9 61 74 20 69 73 20 69 6e 64 65 78 65 64 20 2a 2f  at is indexed */
2d2ba 0a 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70 50  .  int iTab = pP
2d2bb 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20  arse->nTab++;   
2d2bc 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f    /* Btree curso
2d2bd 72 20 75 73 65 64 20 66 6f 72 20 70 54 61 62 20  r used for pTab 
2d2be 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20  */.  int iIdx = 
2d2bf 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20  pParse->nTab++; 
2d2c0 20 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72      /* Btree cur
2d2c1 73 6f 72 20 75 73 65 64 20 66 6f 72 20 70 49 6e  sor used for pIn
2d2c2 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 53 6f  dex */.  int iSo
2d2c3 72 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  rter;           
2d2c4 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
2d2c5 72 20 6f 70 65 6e 65 64 20 62 79 20 4f 70 65 6e  r opened by Open
2d2c6 53 6f 72 74 65 72 20 28 69 66 20 69 6e 20 75 73  Sorter (if in us
2d2c7 65 29 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  e) */.  int addr
2d2c8 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
2d2c9 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
2d2ca 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70  s of top of loop
2d2cb 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 32 3b   */.  int addr2;
2d2cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d2cd 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
2d2ce 74 6f 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20 6e  to jump to for n
2d2cf 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 2a 2f  ext iteration */
2d2d0 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20  .  int tnum;    
2d2d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d2d2 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f    /* Root page o
2d2d3 66 20 69 6e 64 65 78 20 2a 2f 0a 20 20 56 64 62  f index */.  Vdb
2d2d4 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
2d2d5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47              /* G
2d2d6 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74  enerate code int
2d2d7 6f 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 6d  o this virtual m
2d2d8 61 63 68 69 6e 65 20 2a 2f 0a 20 20 4b 65 79 49  achine */.  KeyI
2d2d9 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20 20 20 20  nfo *pKey;      
2d2da 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65             /* Ke
2d2db 79 49 6e 66 6f 20 66 6f 72 20 69 6e 64 65 78 20  yInfo for index 
2d2dc 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
2d2dd 5f 4f 4d 49 54 5f 4d 45 52 47 45 5f 53 4f 52 54  _OMIT_MERGE_SORT
2d2de 0a 20 20 69 6e 74 20 72 65 67 49 64 78 4b 65 79  .  int regIdxKey
2d2df 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2d2e0 20 20 2f 2a 20 52 65 67 69 73 74 65 72 73 20 63    /* Registers c
2d2e1 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 69 6e  ontaining the in
2d2e2 64 65 78 20 6b 65 79 20 2a 2f 0a 23 65 6e 64 69  dex key */.#endi
2d2e3 66 0a 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72  f.  int regRecor
2d2e4 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
2d2e5 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
2d2e6 6f 6c 64 69 6e 67 20 61 73 73 65 6d 62 6c 69 65  olding assemblie
2d2e7 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 2a  d index record *
2d2e8 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
2d2e9 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
2d2ea 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
2d2eb 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2d2ec 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c  .  int iDb = sql
2d2ed 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
2d2ee 78 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53  x(db, pIndex->pS
2d2ef 63 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66  chema);..#ifndef
2d2f0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
2d2f1 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28  HORIZATION.  if(
2d2f2 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
2d2f3 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
2d2f4 5f 52 45 49 4e 44 45 58 2c 20 70 49 6e 64 65 78  _REINDEX, pIndex
2d2f5 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20  ->zName, 0,.    
2d2f6 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a    db->aDb[iDb].z
2d2f7 4e 61 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65  Name ) ){.    re
2d2f8 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  turn;.  }.#endif
2d2f9 0a 0a 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61  ..  /* Require a
2d2fa 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74   write-lock on t
2d2fb 68 65 20 74 61 62 6c 65 20 74 6f 20 70 65 72 66  he table to perf
2d2fc 6f 72 6d 20 74 68 69 73 20 6f 70 65 72 61 74 69  orm this operati
2d2fd 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54  on */.  sqlite3T
2d2fe 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c  ableLock(pParse,
2d2ff 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d   iDb, pTab->tnum
2d300 2c 20 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 1, pTab->zName
2d301 29 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  );..  v = sqlite
2d302 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
2d303 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72  ;.  if( v==0 ) r
2d304 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d  eturn;.  if( mem
2d305 52 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20  RootPage>=0 ){. 
2d306 20 20 20 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f     tnum = memRoo
2d307 74 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  tPage;.  }else{.
2d308 20 20 20 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65      tnum = pInde
2d309 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c  x->tnum;.    sql
2d30a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2d30b 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 74 6e 75 6d  , OP_Clear, tnum
2d30c 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 4b  , iDb);.  }.  pK
2d30d 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
2d30e 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
2d30f 20 70 49 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69   pIndex);.  sqli
2d310 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2d311 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69   OP_OpenWrite, i
2d312 49 64 78 2c 20 74 6e 75 6d 2c 20 69 44 62 2c 20  Idx, tnum, iDb, 
2d313 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2d314 20 20 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65       (char *)pKe
2d315 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  y, P4_KEYINFO_HA
2d316 4e 44 4f 46 46 29 3b 0a 20 20 73 71 6c 69 74 65  NDOFF);.  sqlite
2d317 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
2d318 20 4f 50 46 4c 41 47 5f 42 55 4c 4b 43 53 52 7c   OPFLAG_BULKCSR|
2d319 28 28 6d 65 6d 52 6f 6f 74 50 61 67 65 3e 3d 30  ((memRootPage>=0
2d31a 29 3f 4f 50 46 4c 41 47 5f 50 32 49 53 52 45 47  )?OPFLAG_P2ISREG
2d31b 3a 30 29 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  :0));..#ifndef S
2d31c 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 52 47 45  QLITE_OMIT_MERGE
2d31d 5f 53 4f 52 54 0a 20 20 2f 2a 20 4f 70 65 6e 20  _SORT.  /* Open 
2d31e 74 68 65 20 73 6f 72 74 65 72 20 63 75 72 73 6f  the sorter curso
2d31f 72 20 69 66 20 77 65 20 61 72 65 20 74 6f 20 75  r if we are to u
2d320 73 65 20 6f 6e 65 2e 20 2a 2f 0a 20 20 69 53 6f  se one. */.  iSo
2d321 72 74 65 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  rter = pParse->n
2d322 54 61 62 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33  Tab++;.  sqlite3
2d323 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2d324 5f 53 6f 72 74 65 72 4f 70 65 6e 2c 20 69 53 6f  _SorterOpen, iSo
2d325 72 74 65 72 2c 20 30 2c 20 30 2c 20 28 63 68 61  rter, 0, 0, (cha
2d326 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49  r*)pKey, P4_KEYI
2d327 4e 46 4f 29 3b 0a 23 65 6c 73 65 0a 20 20 69 53  NFO);.#else.  iS
2d328 6f 72 74 65 72 20 3d 20 69 54 61 62 3b 0a 23 65  orter = iTab;.#e
2d329 6e 64 69 66 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20  ndif..  /* Open 
2d32a 74 68 65 20 74 61 62 6c 65 2e 20 4c 6f 6f 70 20  the table. Loop 
2d32b 74 68 72 6f 75 67 68 20 61 6c 6c 20 72 6f 77 73  through all rows
2d32c 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2c 20 69   of the table, i
2d32d 6e 73 65 72 74 69 6e 67 20 69 6e 64 65 78 0a 20  nserting index. 
2d32e 20 2a 2a 20 72 65 63 6f 72 64 73 20 69 6e 74 6f   ** records into
2d32f 20 74 68 65 20 73 6f 72 74 65 72 2e 20 2a 2f 0a   the sorter. */.
2d330 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62    sqlite3OpenTab
2d331 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 2c  le(pParse, iTab,
2d332 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f   iDb, pTab, OP_O
2d333 70 65 6e 52 65 61 64 29 3b 0a 20 20 61 64 64 72  penRead);.  addr
2d334 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
2d335 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69  ddOp2(v, OP_Rewi
2d336 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20 20  nd, iTab, 0);.  
2d337 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69  regRecord = sqli
2d338 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2d339 61 72 73 65 29 3b 0a 0a 23 69 66 6e 64 65 66 20  arse);..#ifndef 
2d33a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 52 47  SQLITE_OMIT_MERG
2d33b 45 5f 53 4f 52 54 0a 20 20 73 71 6c 69 74 65 33  E_SORT.  sqlite3
2d33c 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79  GenerateIndexKey
2d33d 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c  (pParse, pIndex,
2d33e 20 69 54 61 62 2c 20 72 65 67 52 65 63 6f 72 64   iTab, regRecord
2d33f 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  , 1);.  sqlite3V
2d340 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2d341 53 6f 72 74 65 72 49 6e 73 65 72 74 2c 20 69 53  SorterInsert, iS
2d342 6f 72 74 65 72 2c 20 72 65 67 52 65 63 6f 72 64  orter, regRecord
2d343 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2d344 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
2d345 74 2c 20 69 54 61 62 2c 20 61 64 64 72 31 2b 31  t, iTab, addr1+1
2d346 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2d347 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
2d348 31 29 3b 0a 20 20 61 64 64 72 31 20 3d 20 73 71  1);.  addr1 = sq
2d349 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2d34a 76 2c 20 4f 50 5f 53 6f 72 74 65 72 53 6f 72 74  v, OP_SorterSort
2d34b 2c 20 69 53 6f 72 74 65 72 2c 20 30 29 3b 0a 20  , iSorter, 0);. 
2d34c 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 6f 6e 45   if( pIndex->onE
2d34d 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b  rror!=OE_None ){
2d34e 0a 20 20 20 20 69 6e 74 20 6a 32 20 3d 20 73 71  .    int j2 = sq
2d34f 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
2d350 41 64 64 72 28 76 29 20 2b 20 33 3b 0a 20 20 20  Addr(v) + 3;.   
2d351 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2d352 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
2d353 2c 20 6a 32 29 3b 0a 20 20 20 20 61 64 64 72 32  , j2);.    addr2
2d354 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
2d355 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
2d356 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d357 4f 70 33 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72  Op3(v, OP_Sorter
2d358 43 6f 6d 70 61 72 65 2c 20 69 53 6f 72 74 65 72  Compare, iSorter
2d359 2c 20 6a 32 2c 20 72 65 67 52 65 63 6f 72 64 29  , j2, regRecord)
2d35a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 6c  ;.    sqlite3Hal
2d35b 74 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 20  tConstraint(.   
2d35c 20 20 20 20 20 70 50 61 72 73 65 2c 20 4f 45 5f       pParse, OE_
2d35d 41 62 6f 72 74 2c 20 22 69 6e 64 65 78 65 64 20  Abort, "indexed 
2d35e 63 6f 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20  columns are not 
2d35f 75 6e 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54  unique", P4_STAT
2d360 49 43 0a 20 20 20 20 29 3b 0a 20 20 7d 65 6c 73  IC.    );.  }els
2d361 65 7b 0a 20 20 20 20 61 64 64 72 32 20 3d 20 73  e{.    addr2 = s
2d362 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2d363 74 41 64 64 72 28 76 29 3b 0a 20 20 7d 0a 20 20  tAddr(v);.  }.  
2d364 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d365 32 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 44 61  2(v, OP_SorterDa
2d366 74 61 2c 20 69 53 6f 72 74 65 72 2c 20 72 65 67  ta, iSorter, reg
2d367 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74  Record);.  sqlit
2d368 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2d369 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 49  OP_IdxInsert, iI
2d36a 64 78 2c 20 72 65 67 52 65 63 6f 72 64 2c 20 31  dx, regRecord, 1
2d36b 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2d36c 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c  ChangeP5(v, OPFL
2d36d 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54  AG_USESEEKRESULT
2d36e 29 3b 0a 23 65 6c 73 65 0a 20 20 72 65 67 49 64  );.#else.  regId
2d36f 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 47 65  xKey = sqlite3Ge
2d370 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70  nerateIndexKey(p
2d371 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69  Parse, pIndex, i
2d372 54 61 62 2c 20 72 65 67 52 65 63 6f 72 64 2c 20  Tab, regRecord, 
2d373 31 29 3b 0a 20 20 61 64 64 72 32 20 3d 20 61 64  1);.  addr2 = ad
2d374 64 72 31 20 2b 20 31 3b 0a 20 20 69 66 28 20 70  dr1 + 1;.  if( p
2d375 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d  Index->onError!=
2d376 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20 20 63  OE_None ){.    c
2d377 6f 6e 73 74 20 69 6e 74 20 72 65 67 52 6f 77 69  onst int regRowi
2d378 64 20 3d 20 72 65 67 49 64 78 4b 65 79 20 2b 20  d = regIdxKey + 
2d379 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  pIndex->nColumn;
2d37a 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6a  .    const int j
2d37b 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  2 = sqlite3VdbeC
2d37c 75 72 72 65 6e 74 41 64 64 72 28 76 29 20 2b 20  urrentAddr(v) + 
2d37d 32 3b 0a 20 20 20 20 76 6f 69 64 20 2a 20 63 6f  2;.    void * co
2d37e 6e 73 74 20 70 52 65 67 4b 65 79 20 3d 20 53 51  nst pRegKey = SQ
2d37f 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
2d380 72 65 67 49 64 78 4b 65 79 29 3b 0a 0a 20 20 20  regIdxKey);..   
2d381 20 2f 2a 20 54 68 65 20 72 65 67 69 73 74 65 72   /* The register
2d382 73 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68  s accessed by th
2d383 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f 70  e OP_IsUnique op
2d384 63 6f 64 65 20 77 65 72 65 20 61 6c 6c 6f 63 61  code were alloca
2d385 74 65 64 0a 20 20 20 20 2a 2a 20 75 73 69 6e 67  ted.    ** using
2d386 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2d387 61 6e 67 65 28 29 20 69 6e 73 69 64 65 20 6f 66  ange() inside of
2d388 20 74 68 65 20 73 71 6c 69 74 65 33 47 65 6e 65   the sqlite3Gene
2d389 72 61 74 65 49 6e 64 65 78 4b 65 79 28 29 0a 20  rateIndexKey(). 
2d38a 20 20 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76 65     ** call above
2d38b 2e 20 4a 75 73 74 20 62 65 66 6f 72 65 20 74 68  . Just before th
2d38c 61 74 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20  at function was 
2d38d 66 72 65 65 64 20 74 68 65 79 20 77 65 72 65 20  freed they were 
2d38e 72 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a 20  released.    ** 
2d38f 28 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65 20  (made available 
2d390 74 6f 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20  to the compiler 
2d391 66 6f 72 20 72 65 75 73 65 29 20 75 73 69 6e 67  for reuse) using
2d392 20 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33   .    ** sqlite3
2d393 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
2d394 28 29 2e 20 53 6f 20 69 6e 20 73 6f 6d 65 20 77  (). So in some w
2d395 61 79 73 20 68 61 76 69 6e 67 20 74 68 65 20 4f  ays having the O
2d396 50 5f 49 73 55 6e 69 71 75 65 0a 20 20 20 20 2a  P_IsUnique.    *
2d397 2a 20 6f 70 63 6f 64 65 20 75 73 65 20 74 68 65  * opcode use the
2d398 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 77   values stored w
2d399 69 74 68 69 6e 20 73 65 65 6d 73 20 64 61 6e 67  ithin seems dang
2d39a 65 72 6f 75 73 2e 20 48 6f 77 65 76 65 72 2c 20  erous. However, 
2d39b 73 69 6e 63 65 0a 20 20 20 20 2a 2a 20 77 65 20  since.    ** we 
2d39c 63 61 6e 20 62 65 20 73 75 72 65 20 74 68 61 74  can be sure that
2d39d 20 6e 6f 20 6f 74 68 65 72 20 74 65 6d 70 20 72   no other temp r
2d39e 65 67 69 73 74 65 72 73 20 68 61 76 65 20 62 65  egisters have be
2d39f 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20  en allocated.   
2d3a0 20 2a 2a 20 73 69 6e 63 65 20 73 71 6c 69 74 65   ** since sqlite
2d3a1 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
2d3a2 65 28 29 20 77 61 73 20 63 61 6c 6c 65 64 2c 20  e() was called, 
2d3a3 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 64 6f  it is safe to do
2d3a4 20 73 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   so..    */.    
2d3a5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d3a6 34 28 76 2c 20 4f 50 5f 49 73 55 6e 69 71 75 65  4(v, OP_IsUnique
2d3a7 2c 20 69 49 64 78 2c 20 6a 32 2c 20 72 65 67 52  , iIdx, j2, regR
2d3a8 6f 77 69 64 2c 20 70 52 65 67 4b 65 79 2c 20 50  owid, pRegKey, P
2d3a9 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 73 71  4_INT32);.    sq
2d3aa 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61  lite3HaltConstra
2d3ab 69 6e 74 28 0a 20 20 20 20 20 20 20 20 70 50 61  int(.        pPa
2d3ac 72 73 65 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 22  rse, OE_Abort, "
2d3ad 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 73 20  indexed columns 
2d3ae 61 72 65 20 6e 6f 74 20 75 6e 69 71 75 65 22 2c  are not unique",
2d3af 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d   P4_STATIC);.  }
2d3b0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2d3b1 64 4f 70 33 28 76 2c 20 4f 50 5f 49 64 78 49 6e  dOp3(v, OP_IdxIn
2d3b2 73 65 72 74 2c 20 69 49 64 78 2c 20 72 65 67 52  sert, iIdx, regR
2d3b3 65 63 6f 72 64 2c 20 30 29 3b 0a 20 20 73 71 6c  ecord, 0);.  sql
2d3b4 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
2d3b5 28 76 2c 20 4f 50 46 4c 41 47 5f 55 53 45 53 45  (v, OPFLAG_USESE
2d3b6 45 4b 52 45 53 55 4c 54 29 3b 0a 23 65 6e 64 69  EKRESULT);.#endi
2d3b7 66 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  f.  sqlite3Relea
2d3b8 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
2d3b9 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20  , regRecord);.  
2d3ba 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d3bb 32 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 4e 65  2(v, OP_SorterNe
2d3bc 78 74 2c 20 69 53 6f 72 74 65 72 2c 20 61 64 64  xt, iSorter, add
2d3bd 72 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  r2);.  sqlite3Vd
2d3be 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
2d3bf 64 72 31 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  dr1);..  sqlite3
2d3c0 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2d3c1 5f 43 6c 6f 73 65 2c 20 69 54 61 62 29 3b 0a 20  _Close, iTab);. 
2d3c2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2d3c3 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p1(v, OP_Close, 
2d3c4 69 49 64 78 29 3b 0a 20 20 73 71 6c 69 74 65 33  iIdx);.  sqlite3
2d3c5 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2d3c6 5f 43 6c 6f 73 65 2c 20 69 53 6f 72 74 65 72 29  _Close, iSorter)
2d3c7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
2d3c8 65 20 61 20 6e 65 77 20 69 6e 64 65 78 20 66 6f  e a new index fo
2d3c9 72 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 2e 20  r an SQL table. 
2d3ca 20 70 4e 61 6d 65 31 2e 70 4e 61 6d 65 32 20 69   pName1.pName2 i
2d3cb 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
2d3cc 65 20 69 6e 64 65 78 20 0a 2a 2a 20 61 6e 64 20  e index .** and 
2d3cd 70 54 62 6c 4c 69 73 74 20 69 73 20 74 68 65 20  pTblList is the 
2d3ce 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
2d3cf 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20  e that is to be 
2d3d0 69 6e 64 65 78 65 64 2e 20 20 42 6f 74 68 20 77  indexed.  Both w
2d3d1 69 6c 6c 20 0a 2a 2a 20 62 65 20 4e 55 4c 4c 20  ill .** be NULL 
2d3d2 66 6f 72 20 61 20 70 72 69 6d 61 72 79 20 6b 65  for a primary ke
2d3d3 79 20 6f 72 20 61 6e 20 69 6e 64 65 78 20 74 68  y or an index th
2d3d4 61 74 20 69 73 20 63 72 65 61 74 65 64 20 74 6f  at is created to
2d3d5 20 73 61 74 69 73 66 79 20 61 0a 2a 2a 20 55 4e   satisfy a.** UN
2d3d6 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e  IQUE constraint.
2d3d7 20 20 49 66 20 70 54 61 62 6c 65 20 61 6e 64 20    If pTable and 
2d3d8 70 49 6e 64 65 78 20 61 72 65 20 4e 55 4c 4c 2c  pIndex are NULL,
2d3d9 20 75 73 65 20 70 50 61 72 73 65 2d 3e 70 4e 65   use pParse->pNe
2d3da 77 54 61 62 6c 65 0a 2a 2a 20 61 73 20 74 68 65  wTable.** as the
2d3db 20 74 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64   table to be ind
2d3dc 65 78 65 64 2e 20 20 70 50 61 72 73 65 2d 3e 70  exed.  pParse->p
2d3dd 4e 65 77 54 61 62 6c 65 20 69 73 20 61 20 74 61  NewTable is a ta
2d3de 62 6c 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 63  ble that is.** c
2d3df 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63  urrently being c
2d3e0 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 61 20  onstructed by a 
2d3e1 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
2d3e2 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c  tement..**.** pL
2d3e3 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66  ist is a list of
2d3e4 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69   columns to be i
2d3e5 6e 64 65 78 65 64 2e 20 20 70 4c 69 73 74 20 77  ndexed.  pList w
2d3e6 69 6c 6c 20 62 65 20 4e 55 4c 4c 20 69 66 20 74  ill be NULL if t
2d3e7 68 69 73 0a 2a 2a 20 69 73 20 61 20 70 72 69 6d  his.** is a prim
2d3e8 61 72 79 20 6b 65 79 20 6f 72 20 75 6e 69 71 75  ary key or uniqu
2d3e9 65 2d 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20  e-constraint on 
2d3ea 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
2d3eb 63 6f 6c 75 6d 6e 20 61 64 64 65 64 0a 2a 2a 20  column added.** 
2d3ec 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  to the table cur
2d3ed 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e  rently under con
2d3ee 73 74 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2a 0a  struction.  .**.
2d3ef 2a 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 20  ** If the index 
2d3f0 69 73 20 63 72 65 61 74 65 64 20 73 75 63 63 65  is created succe
2d3f1 73 73 66 75 6c 6c 79 2c 20 72 65 74 75 72 6e 20  ssfully, return 
2d3f2 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
2d3f3 20 6e 65 77 20 49 6e 64 65 78 0a 2a 2a 20 73 74   new Index.** st
2d3f4 72 75 63 74 75 72 65 2e 20 54 68 69 73 20 69 73  ructure. This is
2d3f5 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33   used by sqlite3
2d3f6 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28 29 20  AddPrimaryKey() 
2d3f7 74 6f 20 6d 61 72 6b 20 74 68 65 20 69 6e 64 65  to mark the inde
2d3f8 78 0a 2a 2a 20 61 73 20 74 68 65 20 74 61 62 6c  x.** as the tabl
2d3f9 65 73 20 70 72 69 6d 61 72 79 20 6b 65 79 20 28  es primary key (
2d3fa 49 6e 64 65 78 2e 61 75 74 6f 49 6e 64 65 78 3d  Index.autoIndex=
2d3fb 3d 32 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  =2)..*/.SQLITE_P
2d3fc 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73 71  RIVATE Index *sq
2d3fd 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78  lite3CreateIndex
2d3fe 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2d3ff 65 2c 20 20 20 20 20 2f 2a 20 41 6c 6c 20 69 6e  e,     /* All in
2d400 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
2d401 74 68 69 73 20 70 61 72 73 65 20 2a 2f 0a 20 20  this parse */.  
2d402 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20  Token *pName1,  
2d403 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74     /* First part
2d404 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20   of index name. 
2d405 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
2d406 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20   Token *pName2, 
2d407 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61      /* Second pa
2d408 72 74 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65  rt of index name
2d409 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f  . May be NULL */
2d40a 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 62 6c  .  SrcList *pTbl
2d40b 4e 61 6d 65 2c 20 2f 2a 20 54 61 62 6c 65 20 74  Name, /* Table t
2d40c 6f 20 69 6e 64 65 78 2e 20 55 73 65 20 70 50 61  o index. Use pPa
2d40d 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69  rse->pNewTable i
2d40e 66 20 30 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  f 0 */.  ExprLis
2d40f 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 41  t *pList,   /* A
2d410 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   list of columns
2d411 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a   to be indexed *
2d412 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c  /.  int onError,
2d413 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f         /* OE_Abo
2d414 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f  rt, OE_Ignore, O
2d415 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45  E_Replace, or OE
2d416 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e  _None */.  Token
2d417 20 2a 70 53 74 61 72 74 2c 20 20 20 20 20 2f 2a   *pStart,     /*
2d418 20 54 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65   The CREATE toke
2d419 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 74 68  n that begins th
2d41a 69 73 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  is statement */.
2d41b 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20    Token *pEnd,  
2d41c 20 20 20 20 20 2f 2a 20 54 68 65 20 22 29 22 20       /* The ")" 
2d41d 74 68 61 74 20 63 6c 6f 73 65 73 20 74 68 65 20  that closes the 
2d41e 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61  CREATE INDEX sta
2d41f 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  tement */.  int 
2d420 73 6f 72 74 4f 72 64 65 72 2c 20 20 20 20 20 2f  sortOrder,     /
2d421 2a 20 53 6f 72 74 20 6f 72 64 65 72 20 6f 66 20  * Sort order of 
2d422 70 72 69 6d 61 72 79 20 6b 65 79 20 77 68 65 6e  primary key when
2d423 20 70 4c 69 73 74 3d 3d 4e 55 4c 4c 20 2a 2f 0a   pList==NULL */.
2d424 20 20 69 6e 74 20 69 66 4e 6f 74 45 78 69 73 74    int ifNotExist
2d425 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20 65 72 72       /* Omit err
2d426 6f 72 20 69 66 20 69 6e 64 65 78 20 61 6c 72 65  or if index alre
2d427 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b  ady exists */.){
2d428 0a 20 20 49 6e 64 65 78 20 2a 70 52 65 74 20 3d  .  Index *pRet =
2d429 20 30 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74   0;     /* Point
2d42a 65 72 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a  er to return */.
2d42b 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2d42c 30 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20  0;     /* Table 
2d42d 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f  to be indexed */
2d42e 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78  .  Index *pIndex
2d42f 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 69   = 0;   /* The i
2d430 6e 64 65 78 20 74 6f 20 62 65 20 63 72 65 61 74  ndex to be creat
2d431 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e  ed */.  char *zN
2d432 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ame = 0;     /* 
2d433 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65  Name of the inde
2d434 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  x */.  int nName
2d435 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
2d436 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
2d437 65 72 73 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a  ers in zName */.
2d438 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 54 6f    int i, j;.  To
2d439 6b 65 6e 20 6e 75 6c 6c 49 64 3b 20 20 20 20 20  ken nullId;     
2d43a 20 20 20 2f 2a 20 46 61 6b 65 20 74 6f 6b 65 6e     /* Fake token
2d43b 20 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 49 44   for an empty ID
2d43c 20 6c 69 73 74 20 2a 2f 0a 20 20 44 62 46 69 78   list */.  DbFix
2d43d 65 72 20 73 46 69 78 3b 20 20 20 20 20 20 20 20  er sFix;        
2d43e 2f 2a 20 46 6f 72 20 61 73 73 69 67 6e 69 6e 67  /* For assigning
2d43f 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 73 20   database names 
2d440 74 6f 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 69  to pTable */.  i
2d441 6e 74 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b  nt sortOrderMask
2d442 3b 20 20 20 2f 2a 20 31 20 74 6f 20 68 6f 6e 6f  ;   /* 1 to hono
2d443 72 20 44 45 53 43 20 69 6e 20 69 6e 64 65 78 2e  r DESC in index.
2d444 20 20 30 20 74 6f 20 69 67 6e 6f 72 65 2e 20 2a    0 to ignore. *
2d445 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
2d446 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2d447 44 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20  Db *pDb;        
2d448 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63       /* The spec
2d449 69 66 69 63 20 74 61 62 6c 65 20 63 6f 6e 74 61  ific table conta
2d44a 69 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 65  ining the indexe
2d44b 64 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  d database */.  
2d44c 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
2d44d 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
2d44e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68   the database th
2d44f 61 74 20 69 73 20 62 65 69 6e 67 20 77 72 69 74  at is being writ
2d450 74 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ten */.  Token *
2d451 70 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 2f 2a  pName = 0;    /*
2d452 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d   Unqualified nam
2d453 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 74  e of the index t
2d454 6f 20 63 72 65 61 74 65 20 2a 2f 0a 20 20 73 74  o create */.  st
2d455 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
2d456 65 6d 20 2a 70 4c 69 73 74 49 74 65 6d 3b 20 2f  em *pListItem; /
2d457 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  * For looping ov
2d458 65 72 20 70 4c 69 73 74 20 2a 2f 0a 20 20 69 6e  er pList */.  in
2d459 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 45  t nCol;.  int nE
2d45a 78 74 72 61 20 3d 20 30 3b 0a 20 20 63 68 61 72  xtra = 0;.  char
2d45b 20 2a 7a 45 78 74 72 61 3b 0a 0a 20 20 61 73 73   *zExtra;..  ass
2d45c 65 72 74 28 20 70 53 74 61 72 74 3d 3d 30 20 7c  ert( pStart==0 |
2d45d 7c 20 70 45 6e 64 21 3d 30 20 29 3b 20 2f 2a 20  | pEnd!=0 ); /* 
2d45e 70 45 6e 64 20 6d 75 73 74 20 62 65 20 6e 6f 6e  pEnd must be non
2d45f 2d 4e 55 4c 4c 20 69 66 20 70 53 74 61 72 74 20  -NULL if pStart 
2d460 69 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  is */.  assert( 
2d461 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20  pParse->nErr==0 
2d462 29 3b 20 20 20 20 20 20 2f 2a 20 4e 65 76 65 72  );      /* Never
2d463 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 72 69   called with pri
2d464 6f 72 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69  or errors */.  i
2d465 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2d466 6c 65 64 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52  led || IN_DECLAR
2d467 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 67 6f  E_VTAB ){.    go
2d468 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
2d469 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ndex;.  }.  if( 
2d46a 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
2d46b 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
2d46c 72 73 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  rse) ){.    goto
2d46d 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
2d46e 65 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20  ex;.  }..  /*.  
2d46f 2a 2a 20 46 69 6e 64 20 74 68 65 20 74 61 62 6c  ** Find the tabl
2d470 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20  e that is to be 
2d471 69 6e 64 65 78 65 64 2e 20 20 52 65 74 75 72 6e  indexed.  Return
2d472 20 65 61 72 6c 79 20 69 66 20 6e 6f 74 20 66 6f   early if not fo
2d473 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  und..  */.  if( 
2d474 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 0a  pTblName!=0 ){..
2d475 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 74      /* Use the t
2d476 77 6f 2d 70 61 72 74 20 69 6e 64 65 78 20 6e 61  wo-part index na
2d477 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  me to determine 
2d478 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20  the database .  
2d479 20 20 2a 2a 20 74 6f 20 73 65 61 72 63 68 20 66    ** to search f
2d47a 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 27 46  or the table. 'F
2d47b 69 78 27 20 74 68 65 20 74 61 62 6c 65 20 6e 61  ix' the table na
2d47c 6d 65 20 74 6f 20 74 68 69 73 20 64 62 0a 20 20  me to this db.  
2d47d 20 20 2a 2a 20 62 65 66 6f 72 65 20 6c 6f 6f 6b    ** before look
2d47e 69 6e 67 20 75 70 20 74 68 65 20 74 61 62 6c 65  ing up the table
2d47f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
2d480 65 72 74 28 20 70 4e 61 6d 65 31 20 26 26 20 70  ert( pName1 && p
2d481 4e 61 6d 65 32 20 29 3b 0a 20 20 20 20 69 44 62  Name2 );.    iDb
2d482 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72   = sqlite3TwoPar
2d483 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e  tName(pParse, pN
2d484 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70  ame1, pName2, &p
2d485 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69  Name);.    if( i
2d486 44 62 3c 30 20 29 20 67 6f 74 6f 20 65 78 69 74  Db<0 ) goto exit
2d487 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
2d488 20 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65     assert( pName
2d489 20 26 26 20 70 4e 61 6d 65 2d 3e 7a 20 29 3b 0a   && pName->z );.
2d48a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2d48b 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 20 20  OMIT_TEMPDB.    
2d48c 2f 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 20  /* If the index 
2d48d 6e 61 6d 65 20 77 61 73 20 75 6e 71 75 61 6c 69  name was unquali
2d48e 66 69 65 64 2c 20 63 68 65 63 6b 20 69 66 20 74  fied, check if t
2d48f 68 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20  he table.    ** 
2d490 69 73 20 61 20 74 65 6d 70 20 74 61 62 6c 65 2e  is a temp table.
2d491 20 49 66 20 73 6f 2c 20 73 65 74 20 74 68 65 20   If so, set the 
2d492 64 61 74 61 62 61 73 65 20 74 6f 20 31 2e 20 44  database to 1. D
2d493 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 0a 20 20  o not do this.  
2d494 20 20 2a 2a 20 69 66 20 69 6e 69 74 69 61 6c 69    ** if initiali
2d495 73 69 6e 67 20 61 20 64 61 74 61 62 61 73 65 20  sing a database 
2d496 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20  schema..    */. 
2d497 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74     if( !db->init
2d498 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 70  .busy ){.      p
2d499 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Tab = sqlite3Src
2d49a 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73  ListLookup(pPars
2d49b 65 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a 20 20  e, pTblName);.  
2d49c 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e      if( pName2->
2d49d 6e 3d 3d 30 20 26 26 20 70 54 61 62 20 26 26 20  n==0 && pTab && 
2d49e 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3d 3d 64  pTab->pSchema==d
2d49f 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d  b->aDb[1].pSchem
2d4a0 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69 44 62  a ){.        iDb
2d4a1 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
2d4a2 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
2d4a3 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e  if( sqlite3FixIn
2d4a4 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65  it(&sFix, pParse
2d4a5 2c 20 69 44 62 2c 20 22 69 6e 64 65 78 22 2c 20  , iDb, "index", 
2d4a6 70 4e 61 6d 65 29 20 26 26 0a 20 20 20 20 20 20  pName) &&.      
2d4a7 20 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c    sqlite3FixSrcL
2d4a8 69 73 74 28 26 73 46 69 78 2c 20 70 54 62 6c 4e  ist(&sFix, pTblN
2d4a9 61 6d 65 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  ame).    ){.    
2d4aa 20 20 2f 2a 20 42 65 63 61 75 73 65 20 74 68 65    /* Because the
2d4ab 20 70 61 72 73 65 72 20 63 6f 6e 73 74 72 75 63   parser construc
2d4ac 74 73 20 70 54 62 6c 4e 61 6d 65 20 66 72 6f 6d  ts pTblName from
2d4ad 20 61 20 73 69 6e 67 6c 65 20 69 64 65 6e 74 69   a single identi
2d4ae 66 69 65 72 2c 0a 20 20 20 20 20 20 2a 2a 20 73  fier,.      ** s
2d4af 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74  qlite3FixSrcList
2d4b0 20 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e   can never fail.
2d4b1 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
2d4b2 28 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  (0);.    }.    p
2d4b3 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  Tab = sqlite3Loc
2d4b4 61 74 65 54 61 62 6c 65 49 74 65 6d 28 70 50 61  ateTableItem(pPa
2d4b5 72 73 65 2c 20 30 2c 20 26 70 54 62 6c 4e 61 6d  rse, 0, &pTblNam
2d4b6 65 2d 3e 61 5b 30 5d 29 3b 0a 20 20 20 20 61 73  e->a[0]);.    as
2d4b7 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63  sert( db->malloc
2d4b8 46 61 69 6c 65 64 3d 3d 30 20 7c 7c 20 70 54 61  Failed==0 || pTa
2d4b9 62 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  b==0 );.    if( 
2d4ba 70 54 61 62 3d 3d 30 20 29 20 67 6f 74 6f 20 65  pTab==0 ) goto e
2d4bb 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
2d4bc 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62  ;.    assert( db
2d4bd 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
2d4be 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d  ma==pTab->pSchem
2d4bf 61 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  a );.  }else{.  
2d4c0 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 3d    assert( pName=
2d4c1 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
2d4c2 28 20 70 53 74 61 72 74 3d 3d 30 20 29 3b 0a 20  ( pStart==0 );. 
2d4c3 20 20 20 70 54 61 62 20 3d 20 70 50 61 72 73 65     pTab = pParse
2d4c4 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 20  ->pNewTable;.   
2d4c5 20 69 66 28 20 21 70 54 61 62 20 29 20 67 6f 74   if( !pTab ) got
2d4c6 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
2d4c7 64 65 78 3b 0a 20 20 20 20 69 44 62 20 3d 20 73  dex;.    iDb = s
2d4c8 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
2d4c9 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53  dex(db, pTab->pS
2d4ca 63 68 65 6d 61 29 3b 0a 20 20 7d 0a 20 20 70 44  chema);.  }.  pD
2d4cb 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
2d4cc 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54  ];..  assert( pT
2d4cd 61 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ab!=0 );.  asser
2d4ce 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d  t( pParse->nErr=
2d4cf 3d 30 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  =0 );.  if( sqli
2d4d0 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62  te3StrNICmp(pTab
2d4d1 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65  ->zName, "sqlite
2d4d2 5f 22 2c 20 37 29 3d 3d 30 20 0a 20 20 20 20 20  _", 7)==0 .     
2d4d3 20 20 26 26 20 6d 65 6d 63 6d 70 28 26 70 54 61    && memcmp(&pTa
2d4d4 62 2d 3e 7a 4e 61 6d 65 5b 37 5d 2c 22 61 6c 74  b->zName[7],"alt
2d4d5 65 72 74 61 62 5f 22 2c 39 29 21 3d 30 20 29 7b  ertab_",9)!=0 ){
2d4d6 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2d4d7 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61  rMsg(pParse, "ta
2d4d8 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62  ble %s may not b
2d4d9 65 20 69 6e 64 65 78 65 64 22 2c 20 70 54 61 62  e indexed", pTab
2d4da 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  ->zName);.    go
2d4db 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
2d4dc 6e 64 65 78 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  ndex;.  }.#ifnde
2d4dd 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2d4de 45 57 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70  EW.  if( pTab->p
2d4df 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71  Select ){.    sq
2d4e0 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2d4e1 61 72 73 65 2c 20 22 76 69 65 77 73 20 6d 61 79  arse, "views may
2d4e2 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22   not be indexed"
2d4e3 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  );.    goto exit
2d4e4 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
2d4e5 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65   }.#endif.#ifnde
2d4e6 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2d4e7 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28  RTUALTABLE.  if(
2d4e8 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
2d4e9 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2d4ea 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2d4eb 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20  "virtual tables 
2d4ec 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78  may not be index
2d4ed 65 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ed");.    goto e
2d4ee 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
2d4ef 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
2d4f0 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65  /*.  ** Find the
2d4f1 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64   name of the ind
2d4f2 65 78 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74  ex.  Make sure t
2d4f3 68 65 72 65 20 69 73 20 6e 6f 74 20 61 6c 72 65  here is not alre
2d4f4 61 64 79 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2a  ady another.  **
2d4f5 20 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 20   index or table 
2d4f6 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61  with the same na
2d4f7 6d 65 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  me.  .  **.  ** 
2d4f8 45 78 63 65 70 74 69 6f 6e 3a 20 20 49 66 20 77  Exception:  If w
2d4f9 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 74 68  e are reading th
2d4fa 65 20 6e 61 6d 65 73 20 6f 66 20 70 65 72 6d 61  e names of perma
2d4fb 6e 65 6e 74 20 69 6e 64 69 63 65 73 20 66 72 6f  nent indices fro
2d4fc 6d 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74  m the.  ** sqlit
2d4fd 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 28  e_master table (
2d4fe 62 65 63 61 75 73 65 20 73 6f 6d 65 20 6f 74 68  because some oth
2d4ff 65 72 20 70 72 6f 63 65 73 73 20 63 68 61 6e 67  er process chang
2d500 65 64 20 74 68 65 20 73 63 68 65 6d 61 29 20 61  ed the schema) a
2d501 6e 64 0a 20 20 2a 2a 20 6f 6e 65 20 6f 66 20 74  nd.  ** one of t
2d502 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 63  he index names c
2d503 6f 6c 6c 69 64 65 73 20 77 69 74 68 20 74 68 65  ollides with the
2d504 20 6e 61 6d 65 20 6f 66 20 61 20 74 65 6d 70 6f   name of a tempo
2d505 72 61 72 79 20 74 61 62 6c 65 20 6f 72 0a 20 20  rary table or.  
2d506 2a 2a 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 77  ** index, then w
2d507 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20  e will continue 
2d508 74 6f 20 70 72 6f 63 65 73 73 20 74 68 69 73 20  to process this 
2d509 69 6e 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  index..  **.  **
2d50a 20 49 66 20 70 4e 61 6d 65 3d 3d 30 20 69 74 20   If pName==0 it 
2d50b 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 61 72  means that we ar
2d50c 65 0a 20 20 2a 2a 20 64 65 61 6c 69 6e 67 20 77  e.  ** dealing w
2d50d 69 74 68 20 61 20 70 72 69 6d 61 72 79 20 6b 65  ith a primary ke
2d50e 79 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73  y or UNIQUE cons
2d50f 74 72 61 69 6e 74 2e 20 20 57 65 20 68 61 76 65  traint.  We have
2d510 20 74 6f 20 69 6e 76 65 6e 74 20 6f 75 72 0a 20   to invent our. 
2d511 20 2a 2a 20 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20   ** own name..  
2d512 2a 2f 0a 20 20 69 66 28 20 70 4e 61 6d 65 20 29  */.  if( pName )
2d513 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71  {.    zName = sq
2d514 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
2d515 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20  en(db, pName);. 
2d516 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20     if( zName==0 
2d517 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61  ) goto exit_crea
2d518 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 61 73  te_index;.    as
2d519 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 7a 21 3d  sert( pName->z!=
2d51a 30 20 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c  0 );.    if( SQL
2d51b 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43  ITE_OK!=sqlite3C
2d51c 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70  heckObjectName(p
2d51d 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b  Parse, zName) ){
2d51e 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
2d51f 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
2d520 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64 62     }.    if( !db
2d521 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
2d522 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
2d523 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e  FindTable(db, zN
2d524 61 6d 65 2c 20 30 29 21 3d 30 20 29 7b 0a 20 20  ame, 0)!=0 ){.  
2d525 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2d526 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
2d527 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
2d528 61 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 25 73  a table named %s
2d529 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  ", zName);.     
2d52a 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
2d52b 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 20  ate_index;.     
2d52c 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
2d52d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65   sqlite3FindInde
2d52e 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 44 62  x(db, zName, pDb
2d52f 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20  ->zName)!=0 ){. 
2d530 20 20 20 20 20 69 66 28 20 21 69 66 4e 6f 74 45       if( !ifNotE
2d531 78 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  xist ){.        
2d532 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2d533 70 50 61 72 73 65 2c 20 22 69 6e 64 65 78 20 25  pParse, "index %
2d534 73 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  s already exists
2d535 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  ", zName);.     
2d536 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2d537 61 73 73 65 72 74 28 20 21 64 62 2d 3e 69 6e 69  assert( !db->ini
2d538 74 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 20 20  t.busy );.      
2d539 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72    sqlite3CodeVer
2d53a 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65  ifySchema(pParse
2d53b 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 7d 0a  , iDb);.      }.
2d53c 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
2d53d 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
2d53e 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
2d53f 20 69 6e 74 20 6e 3b 0a 20 20 20 20 49 6e 64 65   int n;.    Inde
2d540 78 20 2a 70 4c 6f 6f 70 3b 0a 20 20 20 20 66 6f  x *pLoop;.    fo
2d541 72 28 70 4c 6f 6f 70 3d 70 54 61 62 2d 3e 70 49  r(pLoop=pTab->pI
2d542 6e 64 65 78 2c 20 6e 3d 31 3b 20 70 4c 6f 6f 70  ndex, n=1; pLoop
2d543 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e 70  ; pLoop=pLoop->p
2d544 4e 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20  Next, n++){}.   
2d545 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
2d546 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c  MPrintf(db, "sql
2d547 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 25 73  ite_autoindex_%s
2d548 5f 25 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  _%d", pTab->zNam
2d549 65 2c 20 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a  e, n);.    if( z
2d54a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Name==0 ){.     
2d54b 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
2d54c 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20  e_index;.    }. 
2d54d 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66   }..  /* Check f
2d54e 6f 72 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  or authorization
2d54f 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 69 6e   to create an in
2d550 64 65 78 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  dex..  */.#ifnde
2d551 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
2d552 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a  THORIZATION.  {.
2d553 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2d554 7a 44 62 20 3d 20 70 44 62 2d 3e 7a 4e 61 6d 65  zDb = pDb->zName
2d555 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
2d556 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
2d557 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54  e, SQLITE_INSERT
2d558 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
2d559 44 62 29 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a  Db), 0, zDb) ){.
2d55a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
2d55b 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
2d55c 20 20 7d 0a 20 20 20 20 69 20 3d 20 53 51 4c 49    }.    i = SQLI
2d55d 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58 3b  TE_CREATE_INDEX;
2d55e 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54  .    if( !OMIT_T
2d55f 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20  EMPDB && iDb==1 
2d560 29 20 69 20 3d 20 53 51 4c 49 54 45 5f 43 52 45  ) i = SQLITE_CRE
2d561 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 3b 0a  ATE_TEMP_INDEX;.
2d562 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
2d563 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2d564 20 69 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d   i, zName, pTab-
2d565 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a  >zName, zDb) ){.
2d566 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
2d567 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
2d568 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a    }.  }.#endif..
2d569 20 20 2f 2a 20 49 66 20 70 4c 69 73 74 3d 3d 30    /* If pList==0
2d56a 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20  , it means this 
2d56b 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c  routine was call
2d56c 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 70 72 69  ed to make a pri
2d56d 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 20 6f 75  mary.  ** key ou
2d56e 74 20 6f 66 20 74 68 65 20 6c 61 73 74 20 63 6f  t of the last co
2d56f 6c 75 6d 6e 20 61 64 64 65 64 20 74 6f 20 74 68  lumn added to th
2d570 65 20 74 61 62 6c 65 20 75 6e 64 65 72 20 63 6f  e table under co
2d571 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20 20 2a 2a  nstruction..  **
2d572 20 53 6f 20 63 72 65 61 74 65 20 61 20 66 61 6b   So create a fak
2d573 65 20 6c 69 73 74 20 74 6f 20 73 69 6d 75 6c 61  e list to simula
2d574 74 65 20 74 68 69 73 2e 0a 20 20 2a 2f 0a 20 20  te this..  */.  
2d575 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a  if( pList==0 ){.
2d576 20 20 20 20 6e 75 6c 6c 49 64 2e 7a 20 3d 20 70      nullId.z = p
2d577 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61 62 2d 3e  Tab->aCol[pTab->
2d578 6e 43 6f 6c 2d 31 5d 2e 7a 4e 61 6d 65 3b 0a 20  nCol-1].zName;. 
2d579 20 20 20 6e 75 6c 6c 49 64 2e 6e 20 3d 20 73 71     nullId.n = sq
2d57a 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 28 63  lite3Strlen30((c
2d57b 68 61 72 2a 29 6e 75 6c 6c 49 64 2e 7a 29 3b 0a  har*)nullId.z);.
2d57c 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69      pList = sqli
2d57d 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2d57e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 30 29 3b  d(pParse, 0, 0);
2d57f 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  .    if( pList==
2d580 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72  0 ) goto exit_cr
2d581 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
2d582 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53  sqlite3ExprListS
2d583 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  etName(pParse, p
2d584 4c 69 73 74 2c 20 26 6e 75 6c 6c 49 64 2c 20 30  List, &nullId, 0
2d585 29 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b  );.    pList->a[
2d586 30 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28  0].sortOrder = (
2d587 75 38 29 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20  u8)sortOrder;.  
2d588 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  }..  /* Figure o
2d589 75 74 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 65  ut how many byte
2d58a 73 20 6f 66 20 73 70 61 63 65 20 61 72 65 20 72  s of space are r
2d58b 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65  equired to store
2d58c 20 65 78 70 6c 69 63 69 74 6c 79 0a 20 20 2a 2a   explicitly.  **
2d58d 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 6c 61   specified colla
2d58e 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
2d58f 6d 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  mes..  */.  for(
2d590 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45  i=0; i<pList->nE
2d591 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45  xpr; i++){.    E
2d592 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 4c 69  xpr *pExpr = pLi
2d593 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  st->a[i].pExpr;.
2d594 20 20 20 20 69 66 28 20 70 45 78 70 72 20 29 7b      if( pExpr ){
2d595 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  .      CollSeq *
2d596 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70  pColl = pExpr->p
2d597 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 2f 2a 20 45  Coll;.      /* E
2d598 69 74 68 65 72 20 70 43 6f 6c 6c 21 3d 30 20 6f  ither pColl!=0 o
2d599 72 20 74 68 65 72 65 20 77 61 73 20 61 6e 20 4f  r there was an O
2d59a 4f 4d 20 66 61 69 6c 75 72 65 2e 20 20 42 75 74  OM failure.  But
2d59b 20 69 66 20 61 6e 20 4f 4f 4d 0a 20 20 20 20 20   if an OOM.     
2d59c 20 2a 2a 20 66 61 69 6c 75 72 65 20 77 65 20 68   ** failure we h
2d59d 61 76 65 20 71 75 69 74 20 62 65 66 6f 72 65 20  ave quit before 
2d59e 72 65 61 63 68 69 6e 67 20 74 68 69 73 20 70 6f  reaching this po
2d59f 69 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  int. */.      if
2d5a0 28 20 41 4c 57 41 59 53 28 70 43 6f 6c 6c 29 20  ( ALWAYS(pColl) 
2d5a1 29 7b 0a 20 20 20 20 20 20 20 20 6e 45 78 74 72  ){.        nExtr
2d5a2 61 20 2b 3d 20 28 31 20 2b 20 73 71 6c 69 74 65  a += (1 + sqlite
2d5a3 33 53 74 72 6c 65 6e 33 30 28 70 43 6f 6c 6c 2d  3Strlen30(pColl-
2d5a4 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20  >zName));.      
2d5a5 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
2d5a6 2a 20 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65  * .  ** Allocate
2d5a7 20 74 68 65 20 69 6e 64 65 78 20 73 74 72 75 63   the index struc
2d5a8 74 75 72 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e 4e  ture. .  */.  nN
2d5a9 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
2d5aa 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20  len30(zName);.  
2d5ab 6e 43 6f 6c 20 3d 20 70 4c 69 73 74 2d 3e 6e 45  nCol = pList->nE
2d5ac 78 70 72 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20  xpr;.  pIndex = 
2d5ad 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2d5ae 65 72 6f 28 64 62 2c 20 0a 20 20 20 20 20 20 52  ero(db, .      R
2d5af 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 49 6e 64  OUND8(sizeof(Ind
2d5b0 65 78 29 29 20 2b 20 20 20 20 20 20 20 20 20 20  ex)) +          
2d5b1 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 74 72      /* Index str
2d5b2 75 63 74 75 72 65 20 20 2a 2f 0a 20 20 20 20 20  ucture  */.     
2d5b3 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 74   ROUND8(sizeof(t
2d5b4 52 6f 77 63 6e 74 29 2a 28 6e 43 6f 6c 2b 31 29  Rowcnt)*(nCol+1)
2d5b5 29 20 2b 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61  ) +   /* Index.a
2d5b6 69 52 6f 77 45 73 74 20 20 20 2a 2f 0a 20 20 20  iRowEst   */.   
2d5b7 20 20 20 73 69 7a 65 6f 66 28 63 68 61 72 20 2a     sizeof(char *
2d5b8 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20  )*nCol +        
2d5b9 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2d5ba 2e 61 7a 43 6f 6c 6c 20 20 20 20 20 2a 2f 0a 20  .azColl     */. 
2d5bb 20 20 20 20 20 73 69 7a 65 6f 66 28 69 6e 74 29       sizeof(int)
2d5bc 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20 20  *nCol +         
2d5bd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
2d5be 65 78 2e 61 69 43 6f 6c 75 6d 6e 20 20 20 2a 2f  ex.aiColumn   */
2d5bf 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 75 38  .      sizeof(u8
2d5c0 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20  )*nCol +        
2d5c1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2d5c2 6e 64 65 78 2e 61 53 6f 72 74 4f 72 64 65 72 20  ndex.aSortOrder 
2d5c3 2a 2f 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 2b  */.      nName +
2d5c4 20 31 20 2b 20 20 20 20 20 20 20 20 20 20 20 20   1 +            
2d5c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2d5c6 20 49 6e 64 65 78 2e 7a 4e 61 6d 65 20 20 20 20   Index.zName    
2d5c7 20 20 2a 2f 0a 20 20 20 20 20 20 6e 45 78 74 72    */.      nExtr
2d5c8 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a               
2d5c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d5ca 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  /* Collation seq
2d5cb 75 65 6e 63 65 20 6e 61 6d 65 73 20 2a 2f 0a 20  uence names */. 
2d5cc 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   );.  if( db->ma
2d5cd 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2d5ce 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
2d5cf 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20  te_index;.  }.  
2d5d0 7a 45 78 74 72 61 20 3d 20 28 63 68 61 72 2a 29  zExtra = (char*)
2d5d1 70 49 6e 64 65 78 3b 0a 20 20 70 49 6e 64 65 78  pIndex;.  pIndex
2d5d2 2d 3e 61 69 52 6f 77 45 73 74 20 3d 20 28 74 52  ->aiRowEst = (tR
2d5d3 6f 77 63 6e 74 2a 29 26 7a 45 78 74 72 61 5b 52  owcnt*)&zExtra[R
2d5d4 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 49 6e 64  OUND8(sizeof(Ind
2d5d5 65 78 29 29 5d 3b 0a 20 20 70 49 6e 64 65 78 2d  ex))];.  pIndex-
2d5d6 3e 61 7a 43 6f 6c 6c 20 3d 20 28 63 68 61 72 2a  >azColl = (char*
2d5d7 2a 29 0a 20 20 20 20 20 28 28 63 68 61 72 2a 29  *).     ((char*)
2d5d8 70 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74  pIndex->aiRowEst
2d5d9 20 2b 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66   + ROUND8(sizeof
2d5da 28 74 52 6f 77 63 6e 74 29 2a 6e 43 6f 6c 2b 31  (tRowcnt)*nCol+1
2d5db 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49  ));.  assert( EI
2d5dc 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
2d5dd 4e 54 28 70 49 6e 64 65 78 2d 3e 61 69 52 6f 77  NT(pIndex->aiRow
2d5de 45 73 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Est) );.  assert
2d5df 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  ( EIGHT_BYTE_ALI
2d5e0 47 4e 4d 45 4e 54 28 70 49 6e 64 65 78 2d 3e 61  GNMENT(pIndex->a
2d5e1 7a 43 6f 6c 6c 29 20 29 3b 0a 20 20 70 49 6e 64  zColl) );.  pInd
2d5e2 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 20 3d 20 28  ex->aiColumn = (
2d5e3 69 6e 74 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e  int *)(&pIndex->
2d5e4 61 7a 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20  azColl[nCol]);. 
2d5e5 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72   pIndex->aSortOr
2d5e6 64 65 72 20 3d 20 28 75 38 20 2a 29 28 26 70 49  der = (u8 *)(&pI
2d5e7 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e  ndex->aiColumn[n
2d5e8 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d  Col]);.  pIndex-
2d5e9 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 20 2a  >zName = (char *
2d5ea 29 28 26 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74  )(&pIndex->aSort
2d5eb 4f 72 64 65 72 5b 6e 43 6f 6c 5d 29 3b 0a 20 20  Order[nCol]);.  
2d5ec 7a 45 78 74 72 61 20 3d 20 28 63 68 61 72 20 2a  zExtra = (char *
2d5ed 29 28 26 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65  )(&pIndex->zName
2d5ee 5b 6e 4e 61 6d 65 2b 31 5d 29 3b 0a 20 20 6d 65  [nName+1]);.  me
2d5ef 6d 63 70 79 28 70 49 6e 64 65 78 2d 3e 7a 4e 61  mcpy(pIndex->zNa
2d5f0 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  me, zName, nName
2d5f1 2b 31 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 70  +1);.  pIndex->p
2d5f2 54 61 62 6c 65 20 3d 20 70 54 61 62 3b 0a 20 20  Table = pTab;.  
2d5f3 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20  pIndex->nColumn 
2d5f4 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a  = pList->nExpr;.
2d5f5 20 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f    pIndex->onErro
2d5f6 72 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b  r = (u8)onError;
2d5f7 0a 20 20 70 49 6e 64 65 78 2d 3e 61 75 74 6f 49  .  pIndex->autoI
2d5f8 6e 64 65 78 20 3d 20 28 75 38 29 28 70 4e 61 6d  ndex = (u8)(pNam
2d5f9 65 3d 3d 30 29 3b 0a 20 20 70 49 6e 64 65 78 2d  e==0);.  pIndex-
2d5fa 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61  >pSchema = db->a
2d5fb 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b  Db[iDb].pSchema;
2d5fc 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
2d5fd 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c  e3SchemaMutexHel
2d5fe 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20 29 3b  d(db, iDb, 0) );
2d5ff 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20  ..  /* Check to 
2d600 73 65 65 20 69 66 20 77 65 20 73 68 6f 75 6c 64  see if we should
2d601 20 68 6f 6e 6f 72 20 44 45 53 43 20 72 65 71 75   honor DESC requ
2d602 65 73 74 73 20 6f 6e 20 69 6e 64 65 78 20 63 6f  ests on index co
2d603 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 66 28  lumns.  */.  if(
2d604 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66   pDb->pSchema->f
2d605 69 6c 65 5f 66 6f 72 6d 61 74 3e 3d 34 20 29 7b  ile_format>=4 ){
2d606 0a 20 20 20 20 73 6f 72 74 4f 72 64 65 72 4d 61  .    sortOrderMa
2d607 73 6b 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 48 6f  sk = -1;   /* Ho
2d608 6e 6f 72 20 44 45 53 43 20 2a 2f 0a 20 20 7d 65  nor DESC */.  }e
2d609 6c 73 65 7b 0a 20 20 20 20 73 6f 72 74 4f 72 64  lse{.    sortOrd
2d60a 65 72 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20 2f  erMask = 0;    /
2d60b 2a 20 49 67 6e 6f 72 65 20 44 45 53 43 20 2a 2f  * Ignore DESC */
2d60c 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 63 61 6e 20  .  }..  /* Scan 
2d60d 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65  the names of the
2d60e 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
2d60f 74 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 65  table to be inde
2d610 78 65 64 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 61  xed and.  ** loa
2d611 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64  d the column ind
2d612 69 63 65 73 20 69 6e 74 6f 20 74 68 65 20 49 6e  ices into the In
2d613 64 65 78 20 73 74 72 75 63 74 75 72 65 2e 20 20  dex structure.  
2d614 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72 0a  Report an error.
2d615 20 20 2a 2a 20 69 66 20 61 6e 79 20 63 6f 6c 75    ** if any colu
2d616 6d 6e 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2e  mn is not found.
2d617 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a  .  **.  ** TODO:
2d618 20 20 41 64 64 20 61 20 74 65 73 74 20 74 6f 20    Add a test to 
2d619 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
2d61a 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 69  he same column i
2d61b 73 20 6e 6f 74 20 6e 61 6d 65 64 0a 20 20 2a 2a  s not named.  **
2d61c 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 20   more than once 
2d61d 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20  within the same 
2d61e 69 6e 64 65 78 2e 20 20 4f 6e 6c 79 20 74 68 65  index.  Only the
2d61f 20 66 69 72 73 74 20 69 6e 73 74 61 6e 63 65 20   first instance 
2d620 6f 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6c 75  of.  ** the colu
2d621 6d 6e 20 77 69 6c 6c 20 65 76 65 72 20 62 65 20  mn will ever be 
2d622 75 73 65 64 20 62 79 20 74 68 65 20 6f 70 74 69  used by the opti
2d623 6d 69 7a 65 72 2e 20 20 4e 6f 74 65 20 74 68 61  mizer.  Note tha
2d624 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a  t using the.  **
2d625 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 6d 6f 72   same column mor
2d626 65 20 74 68 61 6e 20 6f 6e 63 65 20 63 61 6e 6e  e than once cann
2d627 6f 74 20 62 65 20 61 6e 20 65 72 72 6f 72 20 62  ot be an error b
2d628 65 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c  ecause that woul
2d629 64 20 0a 20 20 2a 2a 20 62 72 65 61 6b 20 62 61  d .  ** break ba
2d62a 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62  ckwards compatib
2d62b 69 6c 69 74 79 20 2d 20 69 74 20 6e 65 65 64 73  ility - it needs
2d62c 20 74 6f 20 62 65 20 61 20 77 61 72 6e 69 6e 67   to be a warning
2d62d 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
2d62e 2c 20 70 4c 69 73 74 49 74 65 6d 3d 70 4c 69 73  , pListItem=pLis
2d62f 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  t->a; i<pList->n
2d630 45 78 70 72 3b 20 69 2b 2b 2c 20 70 4c 69 73 74  Expr; i++, pList
2d631 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e  Item++){.    con
2d632 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 4e 61 6d  st char *zColNam
2d633 65 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 7a  e = pListItem->z
2d634 4e 61 6d 65 3b 0a 20 20 20 20 43 6f 6c 75 6d 6e  Name;.    Column
2d635 20 2a 70 54 61 62 43 6f 6c 3b 0a 20 20 20 20 69   *pTabCol;.    i
2d636 6e 74 20 72 65 71 75 65 73 74 65 64 53 6f 72 74  nt requestedSort
2d637 4f 72 64 65 72 3b 0a 20 20 20 20 63 68 61 72 20  Order;.    char 
2d638 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20  *zColl;         
2d639 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
2d63a 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2d63b 6e 61 6d 65 20 2a 2f 0a 0a 20 20 20 20 66 6f 72  name */..    for
2d63c 28 6a 3d 30 2c 20 70 54 61 62 43 6f 6c 3d 70 54  (j=0, pTabCol=pT
2d63d 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62  ab->aCol; j<pTab
2d63e 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 54 61  ->nCol; j++, pTa
2d63f 62 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 69  bCol++){.      i
2d640 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
2d641 70 28 7a 43 6f 6c 4e 61 6d 65 2c 20 70 54 61 62  p(zColName, pTab
2d642 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29  Col->zName)==0 )
2d643 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2d644 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e    if( j>=pTab->n
2d645 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Col ){.      sql
2d646 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2d647 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 68  rse, "table %s h
2d648 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d  as no column nam
2d649 65 64 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20  ed %s",.        
2d64a 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f  pTab->zName, zCo
2d64b 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 50  lName);.      pP
2d64c 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d  arse->checkSchem
2d64d 61 20 3d 20 31 3b 0a 20 20 20 20 20 20 67 6f 74  a = 1;.      got
2d64e 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
2d64f 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  dex;.    }.    p
2d650 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  Index->aiColumn[
2d651 69 5d 20 3d 20 6a 3b 0a 20 20 20 20 2f 2a 20 4a  i] = j;.    /* J
2d652 75 73 74 69 66 69 63 61 74 69 6f 6e 20 6f 66 20  ustification of 
2d653 74 68 65 20 41 4c 57 41 59 53 28 70 4c 69 73 74  the ALWAYS(pList
2d654 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f  Item->pExpr->pCo
2d655 6c 6c 29 3a 20 20 42 65 63 61 75 73 65 20 6f 66  ll):  Because of
2d656 0a 20 20 20 20 2a 2a 20 74 68 65 20 77 61 79 20  .    ** the way 
2d657 74 68 65 20 22 69 64 78 6c 69 73 74 22 20 6e 6f  the "idxlist" no
2d658 6e 2d 74 65 72 6d 69 6e 61 6c 20 69 73 20 63 6f  n-terminal is co
2d659 6e 73 74 72 75 63 74 65 64 20 62 79 20 74 68 65  nstructed by the
2d65a 20 70 61 72 73 65 72 2c 0a 20 20 20 20 2a 2a 20   parser,.    ** 
2d65b 69 66 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45  if pListItem->pE
2d65c 78 70 72 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 20  xpr is not null 
2d65d 74 68 65 6e 20 65 69 74 68 65 72 20 70 4c 69 73  then either pLis
2d65e 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43  tItem->pExpr->pC
2d65f 6f 6c 6c 0a 20 20 20 20 2a 2a 20 6d 75 73 74 20  oll.    ** must 
2d660 65 78 69 73 74 20 6f 72 20 65 6c 73 65 20 74 68  exist or else th
2d661 65 72 65 20 6d 75 73 74 20 68 61 76 65 20 62 65  ere must have be
2d662 65 6e 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e  en an OOM error.
2d663 20 20 42 75 74 20 69 66 20 74 68 65 72 65 0a 20    But if there. 
2d664 20 20 20 2a 2a 20 77 61 73 20 61 6e 20 4f 4f 4d     ** was an OOM
2d665 20 65 72 72 6f 72 2c 20 77 65 20 77 6f 75 6c 64   error, we would
2d666 20 6e 65 76 65 72 20 72 65 61 63 68 20 74 68 69   never reach thi
2d667 73 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 20 20  s point. */.    
2d668 69 66 28 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70  if( pListItem->p
2d669 45 78 70 72 20 26 26 20 41 4c 57 41 59 53 28 70  Expr && ALWAYS(p
2d66a 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d  ListItem->pExpr-
2d66b 3e 70 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20  >pColl) ){.     
2d66c 20 69 6e 74 20 6e 43 6f 6c 6c 3b 0a 20 20 20 20   int nColl;.    
2d66d 20 20 7a 43 6f 6c 6c 20 3d 20 70 4c 69 73 74 49    zColl = pListI
2d66e 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c  tem->pExpr->pCol
2d66f 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  l->zName;.      
2d670 6e 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 53  nColl = sqlite3S
2d671 74 72 6c 65 6e 33 30 28 7a 43 6f 6c 6c 29 20 2b  trlen30(zColl) +
2d672 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   1;.      assert
2d673 28 20 6e 45 78 74 72 61 3e 3d 6e 43 6f 6c 6c 20  ( nExtra>=nColl 
2d674 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
2d675 7a 45 78 74 72 61 2c 20 7a 43 6f 6c 6c 2c 20 6e  zExtra, zColl, n
2d676 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 7a 43 6f  Coll);.      zCo
2d677 6c 6c 20 3d 20 7a 45 78 74 72 61 3b 0a 20 20 20  ll = zExtra;.   
2d678 20 20 20 7a 45 78 74 72 61 20 2b 3d 20 6e 43 6f     zExtra += nCo
2d679 6c 6c 3b 0a 20 20 20 20 20 20 6e 45 78 74 72 61  ll;.      nExtra
2d67a 20 2d 3d 20 6e 43 6f 6c 6c 3b 0a 20 20 20 20 7d   -= nColl;.    }
2d67b 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 43 6f 6c  else{.      zCol
2d67c 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a  l = pTab->aCol[j
2d67d 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 69  ].zColl;.      i
2d67e 66 28 20 21 7a 43 6f 6c 6c 20 29 7b 0a 20 20 20  f( !zColl ){.   
2d67f 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 22 42 49       zColl = "BI
2d680 4e 41 52 59 22 3b 0a 20 20 20 20 20 20 7d 0a 20  NARY";.      }. 
2d681 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64 62     }.    if( !db
2d682 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 21  ->init.busy && !
2d683 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c  sqlite3LocateCol
2d684 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f  lSeq(pParse, zCo
2d685 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  ll) ){.      got
2d686 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
2d687 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  dex;.    }.    p
2d688 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d  Index->azColl[i]
2d689 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 20 20 72 65   = zColl;.    re
2d68a 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65 72  questedSortOrder
2d68b 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 73 6f   = pListItem->so
2d68c 72 74 4f 72 64 65 72 20 26 20 73 6f 72 74 4f 72  rtOrder & sortOr
2d68d 64 65 72 4d 61 73 6b 3b 0a 20 20 20 20 70 49 6e  derMask;.    pIn
2d68e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  dex->aSortOrder[
2d68f 69 5d 20 3d 20 28 75 38 29 72 65 71 75 65 73 74  i] = (u8)request
2d690 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 7d  edSortOrder;.  }
2d691 0a 20 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c  .  sqlite3Defaul
2d692 74 52 6f 77 45 73 74 28 70 49 6e 64 65 78 29 3b  tRowEst(pIndex);
2d693 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 70 50  ..  if( pTab==pP
2d694 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
2d695 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 72  ){.    /* This r
2d696 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20  outine has been 
2d697 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 65  called to create
2d698 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 69 6e   an automatic in
2d699 64 65 78 20 61 73 20 61 0a 20 20 20 20 2a 2a 20  dex as a.    ** 
2d69a 72 65 73 75 6c 74 20 6f 66 20 61 20 50 52 49 4d  result of a PRIM
2d69b 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55  ARY KEY or UNIQU
2d69c 45 20 63 6c 61 75 73 65 20 6f 6e 20 61 20 63 6f  E clause on a co
2d69d 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c  lumn definition,
2d69e 20 6f 72 0a 20 20 20 20 2a 2a 20 61 20 50 52 49   or.    ** a PRI
2d69f 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51  MARY KEY or UNIQ
2d6a0 55 45 20 63 6c 61 75 73 65 20 66 6f 6c 6c 6f 77  UE clause follow
2d6a1 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64  ing the column d
2d6a2 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 20 20 20 20  efinitions..    
2d6a3 2a 2a 20 69 2e 65 2e 20 6f 6e 65 20 6f 66 3a 0a  ** i.e. one of:.
2d6a4 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 52      **.    ** CR
2d6a5 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 20 50  EATE TABLE t(x P
2d6a6 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 3b 0a  RIMARY KEY, y);.
2d6a7 20 20 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41      ** CREATE TA
2d6a8 42 4c 45 20 74 28 78 2c 20 79 2c 20 55 4e 49 51  BLE t(x, y, UNIQ
2d6a9 55 45 28 78 2c 20 79 29 29 3b 0a 20 20 20 20 2a  UE(x, y));.    *
2d6aa 2a 0a 20 20 20 20 2a 2a 20 45 69 74 68 65 72 20  *.    ** Either 
2d6ab 77 61 79 2c 20 63 68 65 63 6b 20 74 6f 20 73 65  way, check to se
2d6ac 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 61  e if the table a
2d6ad 6c 72 65 61 64 79 20 68 61 73 20 73 75 63 68 20  lready has such 
2d6ae 61 6e 20 69 6e 64 65 78 2e 20 49 66 0a 20 20 20  an index. If.   
2d6af 20 2a 2a 20 73 6f 2c 20 64 6f 6e 27 74 20 62 6f   ** so, don't bo
2d6b0 74 68 65 72 20 63 72 65 61 74 69 6e 67 20 74 68  ther creating th
2d6b1 69 73 20 6f 6e 65 2e 20 54 68 69 73 20 6f 6e 6c  is one. This onl
2d6b2 79 20 61 70 70 6c 69 65 73 20 74 6f 0a 20 20 20  y applies to.   
2d6b3 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   ** automaticall
2d6b4 79 20 63 72 65 61 74 65 64 20 69 6e 64 69 63 65  y created indice
2d6b5 73 2e 20 55 73 65 72 73 20 63 61 6e 20 64 6f 20  s. Users can do 
2d6b6 61 73 20 74 68 65 79 20 77 69 73 68 20 77 69 74  as they wish wit
2d6b7 68 0a 20 20 20 20 2a 2a 20 65 78 70 6c 69 63 69  h.    ** explici
2d6b8 74 20 69 6e 64 69 63 65 73 2e 0a 20 20 20 20 2a  t indices..    *
2d6b9 2a 0a 20 20 20 20 2a 2a 20 54 77 6f 20 55 4e 49  *.    ** Two UNI
2d6ba 51 55 45 20 6f 72 20 50 52 49 4d 41 52 59 20 4b  QUE or PRIMARY K
2d6bb 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61  EY constraints a
2d6bc 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71  re considered eq
2d6bd 75 69 76 61 6c 65 6e 74 0a 20 20 20 20 2a 2a 20  uivalent.    ** 
2d6be 28 61 6e 64 20 74 68 75 73 20 73 75 70 70 72 65  (and thus suppre
2d6bf 73 73 69 6e 67 20 74 68 65 20 73 65 63 6f 6e 64  ssing the second
2d6c0 20 6f 6e 65 29 20 65 76 65 6e 20 69 66 20 74 68   one) even if th
2d6c1 65 79 20 68 61 76 65 20 64 69 66 66 65 72 65 6e  ey have differen
2d6c2 74 0a 20 20 20 20 2a 2a 20 73 6f 72 74 20 6f 72  t.    ** sort or
2d6c3 64 65 72 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  ders..    **.   
2d6c4 20 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   ** If there are
2d6c5 20 64 69 66 66 65 72 65 6e 74 20 63 6f 6c 6c 61   different colla
2d6c6 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 6f  ting sequences o
2d6c7 72 20 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73  r if the columns
2d6c8 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 63   of.    ** the c
2d6c9 6f 6e 73 74 72 61 69 6e 74 20 6f 63 63 75 72 20  onstraint occur 
2d6ca 69 6e 20 64 69 66 66 65 72 65 6e 74 20 6f 72 64  in different ord
2d6cb 65 72 73 2c 20 74 68 65 6e 20 74 68 65 20 63 6f  ers, then the co
2d6cc 6e 73 74 72 61 69 6e 74 73 20 61 72 65 0a 20 20  nstraints are.  
2d6cd 20 20 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 20    ** considered 
2d6ce 64 69 73 74 69 6e 63 74 20 61 6e 64 20 62 6f 74  distinct and bot
2d6cf 68 20 72 65 73 75 6c 74 20 69 6e 20 73 65 70 61  h result in sepa
2d6d0 72 61 74 65 20 69 6e 64 69 63 65 73 2e 0a 20 20  rate indices..  
2d6d1 20 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a    */.    Index *
2d6d2 70 49 64 78 3b 0a 20 20 20 20 66 6f 72 28 70 49  pIdx;.    for(pI
2d6d3 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
2d6d4 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
2d6d5 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
2d6d6 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20 61 73 73  int k;.      ass
2d6d7 65 72 74 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72  ert( pIdx->onErr
2d6d8 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 20  or!=OE_None );. 
2d6d9 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64       assert( pId
2d6da 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29 3b 0a  x->autoIndex );.
2d6db 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
2d6dc 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f  ndex->onError!=O
2d6dd 45 5f 4e 6f 6e 65 20 29 3b 0a 0a 20 20 20 20 20  E_None );..     
2d6de 20 69 66 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75   if( pIdx->nColu
2d6df 6d 6e 21 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c  mn!=pIndex->nCol
2d6e0 75 6d 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  umn ) continue;.
2d6e1 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b        for(k=0; k
2d6e2 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  <pIdx->nColumn; 
2d6e3 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  k++){.        co
2d6e4 6e 73 74 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20  nst char *z1;.  
2d6e5 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
2d6e6 20 2a 7a 32 3b 0a 20 20 20 20 20 20 20 20 69 66   *z2;.        if
2d6e7 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ( pIdx->aiColumn
2d6e8 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 69 43  [k]!=pIndex->aiC
2d6e9 6f 6c 75 6d 6e 5b 6b 5d 20 29 20 62 72 65 61 6b  olumn[k] ) break
2d6ea 3b 0a 20 20 20 20 20 20 20 20 7a 31 20 3d 20 70  ;.        z1 = p
2d6eb 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a  Idx->azColl[k];.
2d6ec 20 20 20 20 20 20 20 20 7a 32 20 3d 20 70 49 6e          z2 = pIn
2d6ed 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a  dex->azColl[k];.
2d6ee 20 20 20 20 20 20 20 20 69 66 28 20 7a 31 21 3d          if( z1!=
2d6ef 7a 32 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  z2 && sqlite3Str
2d6f0 49 43 6d 70 28 7a 31 2c 20 7a 32 29 20 29 20 62  ICmp(z1, z2) ) b
2d6f1 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2d6f2 20 20 20 20 69 66 28 20 6b 3d 3d 70 49 64 78 2d      if( k==pIdx-
2d6f3 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  >nColumn ){.    
2d6f4 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e      if( pIdx->on
2d6f5 45 72 72 6f 72 21 3d 70 49 6e 64 65 78 2d 3e 6f  Error!=pIndex->o
2d6f6 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20  nError ){.      
2d6f7 20 20 20 20 2f 2a 20 54 68 69 73 20 63 6f 6e 73      /* This cons
2d6f8 74 72 61 69 6e 74 20 63 72 65 61 74 65 73 20 74  traint creates t
2d6f9 68 65 20 73 61 6d 65 20 69 6e 64 65 78 20 61 73  he same index as
2d6fa 20 61 20 70 72 65 76 69 6f 75 73 0a 20 20 20 20   a previous.    
2d6fb 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61        ** constra
2d6fc 69 6e 74 20 73 70 65 63 69 66 69 65 64 20 73 6f  int specified so
2d6fd 6d 65 77 68 65 72 65 20 69 6e 20 74 68 65 20 43  mewhere in the C
2d6fe 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
2d6ff 65 6d 65 6e 74 2e 0a 20 20 20 20 20 20 20 20 20  ement..         
2d700 20 2a 2a 20 48 6f 77 65 76 65 72 20 74 68 65 20   ** However the 
2d701 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75  ON CONFLICT clau
2d702 73 65 73 20 61 72 65 20 64 69 66 66 65 72 65 6e  ses are differen
2d703 74 2e 20 49 66 20 62 6f 74 68 20 74 68 69 73 20  t. If both this 
2d704 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  .          ** co
2d705 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 74 68 65  nstraint and the
2d706 20 70 72 65 76 69 6f 75 73 20 65 71 75 69 76 61   previous equiva
2d707 6c 65 6e 74 20 63 6f 6e 73 74 72 61 69 6e 74 20  lent constraint 
2d708 68 61 76 65 20 65 78 70 6c 69 63 69 74 0a 20 20  have explicit.  
2d709 20 20 20 20 20 20 20 20 2a 2a 20 4f 4e 20 43 4f          ** ON CO
2d70a 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 73 20 74  NFLICT clauses t
2d70b 68 69 73 20 69 73 20 61 6e 20 65 72 72 6f 72 2e  his is an error.
2d70c 20 4f 74 68 65 72 77 69 73 65 2c 20 75 73 65 20   Otherwise, use 
2d70d 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  the.          **
2d70e 20 65 78 70 6c 69 63 69 74 6c 79 20 73 70 65 63   explicitly spec
2d70f 69 66 69 65 64 20 62 65 68 61 76 69 6f 75 72 20  ified behaviour 
2d710 66 6f 72 20 74 68 65 20 69 6e 64 65 78 2e 0a 20  for the index.. 
2d711 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
2d712 20 20 20 20 20 20 69 66 28 20 21 28 70 49 64 78        if( !(pIdx
2d713 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65  ->onError==OE_De
2d714 66 61 75 6c 74 20 7c 7c 20 70 49 6e 64 65 78 2d  fault || pIndex-
2d715 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66  >onError==OE_Def
2d716 61 75 6c 74 29 20 29 7b 0a 20 20 20 20 20 20 20  ault) ){.       
2d717 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2d718 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20  rMsg(pParse, .  
2d719 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63                "c
2d71a 6f 6e 66 6c 69 63 74 69 6e 67 20 4f 4e 20 43 4f  onflicting ON CO
2d71b 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 73 20 73  NFLICT clauses s
2d71c 70 65 63 69 66 69 65 64 22 2c 20 30 29 3b 0a 20  pecified", 0);. 
2d71d 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2d71e 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6f       if( pIdx->o
2d71f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75  nError==OE_Defau
2d720 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  lt ){.          
2d721 20 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 20    pIdx->onError 
2d722 3d 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f  = pIndex->onErro
2d723 72 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  r;.          }. 
2d724 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2d725 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
2d726 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 20 20 7d  e_index;.      }
2d727 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2d728 20 4c 69 6e 6b 20 74 68 65 20 6e 65 77 20 49 6e   Link the new In
2d729 64 65 78 20 73 74 72 75 63 74 75 72 65 20 74 6f  dex structure to
2d72a 20 69 74 73 20 74 61 62 6c 65 20 61 6e 64 20 74   its table and t
2d72b 6f 20 74 68 65 20 6f 74 68 65 72 0a 20 20 2a 2a  o the other.  **
2d72c 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
2d72d 61 73 65 20 73 74 72 75 63 74 75 72 65 73 2e 20  ase structures. 
2d72e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
2d72f 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
2d730 20 49 6e 64 65 78 20 2a 70 3b 0a 20 20 20 20 61   Index *p;.    a
2d731 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 63  ssert( sqlite3Sc
2d732 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62  hemaMutexHeld(db
2d733 2c 20 30 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63  , 0, pIndex->pSc
2d734 68 65 6d 61 29 20 29 3b 0a 20 20 20 20 70 20 3d  hema) );.    p =
2d735 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
2d736 72 74 28 26 70 49 6e 64 65 78 2d 3e 70 53 63 68  rt(&pIndex->pSch
2d737 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 0a 20  ema->idxHash, . 
2d738 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d739 20 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d           pIndex-
2d73a 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53  >zName, sqlite3S
2d73b 74 72 6c 65 6e 33 30 28 70 49 6e 64 65 78 2d 3e  trlen30(pIndex->
2d73c 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20  zName),.        
2d73d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d73e 20 20 70 49 6e 64 65 78 29 3b 0a 20 20 20 20 69    pIndex);.    i
2d73f 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 61 73  f( p ){.      as
2d740 73 65 72 74 28 20 70 3d 3d 70 49 6e 64 65 78 20  sert( p==pIndex 
2d741 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75  );  /* Malloc mu
2d742 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a  st have failed *
2d743 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  /.      db->mall
2d744 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
2d745 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
2d746 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
2d747 7d 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20  }.    db->flags 
2d748 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  |= SQLITE_Intern
2d749 43 68 61 6e 67 65 73 3b 0a 20 20 20 20 69 66 28  Changes;.    if(
2d74a 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a   pTblName!=0 ){.
2d74b 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 74 6e        pIndex->tn
2d74c 75 6d 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65  um = db->init.ne
2d74d 77 54 6e 75 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d  wTnum;.    }.  }
2d74e 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62  ..  /* If the db
2d74f 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 30  ->init.busy is 0
2d750 20 74 68 65 6e 20 63 72 65 61 74 65 20 74 68 65   then create the
2d751 20 69 6e 64 65 78 20 6f 6e 20 64 69 73 6b 2e 20   index on disk. 
2d752 20 54 68 69 73 0a 20 20 2a 2a 20 69 6e 76 6f 6c   This.  ** invol
2d753 76 65 73 20 77 72 69 74 69 6e 67 20 74 68 65 20  ves writing the 
2d754 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20 6d  index into the m
2d755 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20  aster table and 
2d756 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68 65 0a 20  filling in the. 
2d757 20 2a 2a 20 69 6e 64 65 78 20 77 69 74 68 20 74   ** index with t
2d758 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65  he current table
2d759 20 63 6f 6e 74 65 6e 74 73 2e 0a 20 20 2a 2a 0a   contents..  **.
2d75a 20 20 2a 2a 20 54 68 65 20 64 62 2d 3e 69 6e 69    ** The db->ini
2d75b 74 2e 62 75 73 79 20 69 73 20 30 20 77 68 65 6e  t.busy is 0 when
2d75c 20 74 68 65 20 75 73 65 72 20 66 69 72 73 74 20   the user first 
2d75d 65 6e 74 65 72 73 20 61 20 43 52 45 41 54 45 20  enters a CREATE 
2d75e 49 4e 44 45 58 20 0a 20 20 2a 2a 20 63 6f 6d 6d  INDEX .  ** comm
2d75f 61 6e 64 2e 20 20 64 62 2d 3e 69 6e 69 74 2e 62  and.  db->init.b
2d760 75 73 79 20 69 73 20 31 20 77 68 65 6e 20 61 20  usy is 1 when a 
2d761 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e  database is open
2d762 65 64 20 61 6e 64 20 0a 20 20 2a 2a 20 43 52 45  ed and .  ** CRE
2d763 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d  ATE INDEX statem
2d764 65 6e 74 73 20 61 72 65 20 72 65 61 64 20 6f 75  ents are read ou
2d765 74 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20  t of the master 
2d766 74 61 62 6c 65 2e 20 20 49 6e 0a 20 20 2a 2a 20  table.  In.  ** 
2d767 74 68 65 20 6c 61 74 74 65 72 20 63 61 73 65 20  the latter case 
2d768 74 68 65 20 69 6e 64 65 78 20 61 6c 72 65 61 64  the index alread
2d769 79 20 65 78 69 73 74 73 20 6f 6e 20 64 69 73 6b  y exists on disk
2d76a 2c 20 77 68 69 63 68 20 69 73 20 77 68 79 0a 20  , which is why. 
2d76b 20 2a 2a 20 77 65 20 64 6f 6e 27 74 20 77 61 6e   ** we don't wan
2d76c 74 20 74 6f 20 72 65 63 72 65 61 74 65 20 69 74  t to recreate it
2d76d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70  ..  **.  ** If p
2d76e 54 62 6c 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65  TblName==0 it me
2d76f 61 6e 73 20 74 68 69 73 20 69 6e 64 65 78 20 69  ans this index i
2d770 73 20 67 65 6e 65 72 61 74 65 64 20 61 73 20 61  s generated as a
2d771 20 70 72 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a   primary key.  *
2d772 2a 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73  * or UNIQUE cons
2d773 74 72 61 69 6e 74 20 6f 66 20 61 20 43 52 45 41  traint of a CREA
2d774 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
2d775 6e 74 2e 20 20 53 69 6e 63 65 20 74 68 65 20 74  nt.  Since the t
2d776 61 62 6c 65 0a 20 20 2a 2a 20 68 61 73 20 6a 75  able.  ** has ju
2d777 73 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c  st been created,
2d778 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20   it contains no 
2d779 64 61 74 61 20 61 6e 64 20 74 68 65 20 69 6e 64  data and the ind
2d77a 65 78 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ex initializatio
2d77b 6e 0a 20 20 2a 2a 20 73 74 65 70 20 63 61 6e 20  n.  ** step can 
2d77c 62 65 20 73 6b 69 70 70 65 64 2e 0a 20 20 2a 2f  be skipped..  */
2d77d 0a 20 20 65 6c 73 65 7b 20 2f 2a 20 69 66 28 20  .  else{ /* if( 
2d77e 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30  db->init.busy==0
2d77f 20 29 20 2a 2f 0a 20 20 20 20 56 64 62 65 20 2a   ) */.    Vdbe *
2d780 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74  v;.    char *zSt
2d781 6d 74 3b 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d  mt;.    int iMem
2d782 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2d783 6d 3b 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69  m;..    v = sqli
2d784 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2d785 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30  e);.    if( v==0
2d786 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65   ) goto exit_cre
2d787 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 0a 20 20 20  ate_index;...   
2d788 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 72   /* Create the r
2d789 6f 6f 74 70 61 67 65 20 66 6f 72 20 74 68 65 20  ootpage for the 
2d78a 69 6e 64 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20  index.    */.   
2d78b 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
2d78c 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
2d78d 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20  se, 1, iDb);.   
2d78e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2d78f 70 32 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 49  p2(v, OP_CreateI
2d790 6e 64 65 78 2c 20 69 44 62 2c 20 69 4d 65 6d 29  ndex, iDb, iMem)
2d791 3b 0a 0a 20 20 20 20 2f 2a 20 47 61 74 68 65 72  ;..    /* Gather
2d792 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65   the complete te
2d793 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  xt of the CREATE
2d794 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74   INDEX statement
2d795 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65   into.    ** the
2d796 20 7a 53 74 6d 74 20 76 61 72 69 61 62 6c 65 0a   zStmt variable.
2d797 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
2d798 53 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 61  Start ){.      a
2d799 73 73 65 72 74 28 20 70 45 6e 64 21 3d 30 20 29  ssert( pEnd!=0 )
2d79a 3b 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 61 6d  ;.      /* A nam
2d79b 65 64 20 69 6e 64 65 78 20 77 69 74 68 20 61 6e  ed index with an
2d79c 20 65 78 70 6c 69 63 69 74 20 43 52 45 41 54 45   explicit CREATE
2d79d 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74   INDEX statement
2d79e 20 2a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20   */.      zStmt 
2d79f 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2d7a0 28 64 62 2c 20 22 43 52 45 41 54 45 25 73 20 49  (db, "CREATE%s I
2d7a1 4e 44 45 58 20 25 2e 2a 73 22 2c 0a 20 20 20 20  NDEX %.*s",.    
2d7a2 20 20 20 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f      onError==OE_
2d7a3 4e 6f 6e 65 20 3f 20 22 22 20 3a 20 22 20 55 4e  None ? "" : " UN
2d7a4 49 51 55 45 22 2c 0a 20 20 20 20 20 20 20 20 28  IQUE",.        (
2d7a5 69 6e 74 29 28 70 45 6e 64 2d 3e 7a 20 2d 20 70  int)(pEnd->z - p
2d7a6 4e 61 6d 65 2d 3e 7a 29 20 2b 20 31 2c 0a 20 20  Name->z) + 1,.  
2d7a7 20 20 20 20 20 20 70 4e 61 6d 65 2d 3e 7a 29 3b        pName->z);
2d7a8 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2d7a9 20 20 2f 2a 20 41 6e 20 61 75 74 6f 6d 61 74 69    /* An automati
2d7aa 63 20 69 6e 64 65 78 20 63 72 65 61 74 65 64 20  c index created 
2d7ab 62 79 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59  by a PRIMARY KEY
2d7ac 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74   or UNIQUE const
2d7ad 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 2f  raint */.      /
2d7ae 2a 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65  * zStmt = sqlite
2d7af 33 4d 50 72 69 6e 74 66 28 22 22 29 3b 20 2a 2f  3MPrintf(""); */
2d7b0 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 30  .      zStmt = 0
2d7b1 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2d7b2 41 64 64 20 61 6e 20 65 6e 74 72 79 20 69 6e 20  Add an entry in 
2d7b3 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 66 6f  sqlite_master fo
2d7b4 72 20 74 68 69 73 20 69 6e 64 65 78 0a 20 20 20  r this index.   
2d7b5 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e   */.    sqlite3N
2d7b6 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
2d7b7 65 2c 20 0a 20 20 20 20 20 20 20 20 22 49 4e 53  e, .        "INS
2d7b8 45 52 54 20 49 4e 54 4f 20 25 51 2e 25 73 20 56  ERT INTO %Q.%s V
2d7b9 41 4c 55 45 53 28 27 69 6e 64 65 78 27 2c 25 51  ALUES('index',%Q
2d7ba 2c 25 51 2c 23 25 64 2c 25 51 29 3b 22 2c 0a 20  ,%Q,#%d,%Q);",. 
2d7bb 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69         db->aDb[i
2d7bc 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d  Db].zName, SCHEM
2d7bd 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20  A_TABLE(iDb),.  
2d7be 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e        pIndex->zN
2d7bf 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 70 54 61  ame,.        pTa
2d7c0 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  b->zName,.      
2d7c1 20 20 69 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20    iMem,.        
2d7c2 7a 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20 20 20  zStmt.    );.   
2d7c3 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2d7c4 62 2c 20 7a 53 74 6d 74 29 3b 0a 0a 20 20 20 20  b, zStmt);..    
2d7c5 2f 2a 20 46 69 6c 6c 20 74 68 65 20 69 6e 64 65  /* Fill the inde
2d7c6 78 20 77 69 74 68 20 64 61 74 61 20 61 6e 64 20  x with data and 
2d7c7 72 65 70 61 72 73 65 20 74 68 65 20 73 63 68 65  reparse the sche
2d7c8 6d 61 2e 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45  ma. Code an OP_E
2d7c9 78 70 69 72 65 0a 20 20 20 20 2a 2a 20 74 6f 20  xpire.    ** to 
2d7ca 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70  invalidate all p
2d7cb 72 65 2d 63 6f 6d 70 69 6c 65 64 20 73 74 61 74  re-compiled stat
2d7cc 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20  ements..    */. 
2d7cd 20 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 20     if( pTblName 
2d7ce 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2d7cf 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72  RefillIndex(pPar
2d7d0 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 4d 65 6d  se, pIndex, iMem
2d7d1 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2d7d2 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61  ChangeCookie(pPa
2d7d3 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  rse, iDb);.     
2d7d4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 50   sqlite3VdbeAddP
2d7d5 61 72 73 65 53 63 68 65 6d 61 4f 70 28 76 2c 20  arseSchemaOp(v, 
2d7d6 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20 73 71  iDb,.         sq
2d7d7 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2d7d8 20 22 6e 61 6d 65 3d 27 25 71 27 20 41 4e 44 20   "name='%q' AND 
2d7d9 74 79 70 65 3d 27 69 6e 64 65 78 27 22 2c 20 70  type='index'", p
2d7da 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a  Index->zName));.
2d7db 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2d7dc 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 45 78  eAddOp1(v, OP_Ex
2d7dd 70 69 72 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  pire, 0);.    }.
2d7de 20 20 7d 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 61    }..  /* When a
2d7df 64 64 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 74  dding an index t
2d7e0 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e  o the list of in
2d7e1 64 69 63 65 73 20 66 6f 72 20 61 20 74 61 62 6c  dices for a tabl
2d7e2 65 2c 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72  e, make.  ** sur
2d7e3 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6c 61  e all indices la
2d7e4 62 65 6c 65 64 20 4f 45 5f 52 65 70 6c 61 63 65  beled OE_Replace
2d7e5 20 63 6f 6d 65 20 61 66 74 65 72 20 61 6c 6c 20   come after all 
2d7e6 74 68 6f 73 65 20 6c 61 62 65 6c 65 64 0a 20 20  those labeled.  
2d7e7 2a 2a 20 4f 45 5f 49 67 6e 6f 72 65 2e 20 20 54  ** OE_Ignore.  T
2d7e8 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79  his is necessary
2d7e9 20 66 6f 72 20 74 68 65 20 63 6f 72 72 65 63 74   for the correct
2d7ea 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63   constraint chec
2d7eb 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e  k.  ** processin
2d7ec 67 20 28 69 6e 20 73 71 6c 69 74 65 33 47 65 6e  g (in sqlite3Gen
2d7ed 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
2d7ee 68 65 63 6b 73 28 29 29 20 61 73 20 70 61 72 74  hecks()) as part
2d7ef 20 6f 66 0a 20 20 2a 2a 20 55 50 44 41 54 45 20   of.  ** UPDATE 
2d7f0 61 6e 64 20 49 4e 53 45 52 54 20 73 74 61 74 65  and INSERT state
2d7f1 6d 65 6e 74 73 2e 20 20 0a 20 20 2a 2f 0a 20 20  ments.  .  */.  
2d7f2 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  if( db->init.bus
2d7f3 79 20 7c 7c 20 70 54 62 6c 4e 61 6d 65 3d 3d 30  y || pTblName==0
2d7f4 20 29 7b 0a 20 20 20 20 69 66 28 20 6f 6e 45 72   ){.    if( onEr
2d7f5 72 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20  ror!=OE_Replace 
2d7f6 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d  || pTab->pIndex=
2d7f7 3d 30 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70  =0.         || p
2d7f8 54 61 62 2d 3e 70 49 6e 64 65 78 2d 3e 6f 6e 45  Tab->pIndex->onE
2d7f9 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65  rror==OE_Replace
2d7fa 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d  ){.      pIndex-
2d7fb 3e 70 4e 65 78 74 20 3d 20 70 54 61 62 2d 3e 70  >pNext = pTab->p
2d7fc 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 70 54 61  Index;.      pTa
2d7fd 62 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e 64  b->pIndex = pInd
2d7fe 65 78 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ex;.    }else{. 
2d7ff 20 20 20 20 20 49 6e 64 65 78 20 2a 70 4f 74 68       Index *pOth
2d800 65 72 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65  er = pTab->pInde
2d801 78 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  x;.      while( 
2d802 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20 26 26  pOther->pNext &&
2d803 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 2d 3e   pOther->pNext->
2d804 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70 6c  onError!=OE_Repl
2d805 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ace ){.        p
2d806 4f 74 68 65 72 20 3d 20 70 4f 74 68 65 72 2d 3e  Other = pOther->
2d807 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  pNext;.      }. 
2d808 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65       pIndex->pNe
2d809 78 74 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65  xt = pOther->pNe
2d80a 78 74 3b 0a 20 20 20 20 20 20 70 4f 74 68 65 72  xt;.      pOther
2d80b 2d 3e 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78  ->pNext = pIndex
2d80c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65 74  ;.    }.    pRet
2d80d 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20 20 70   = pIndex;.    p
2d80e 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 7d 0a 0a  Index = 0;.  }..
2d80f 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70 20 62 65    /* Clean up be
2d810 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a  fore exiting */.
2d811 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
2d812 78 3a 0a 20 20 69 66 28 20 70 49 6e 64 65 78 20  x:.  if( pIndex 
2d813 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ){.    sqlite3Db
2d814 46 72 65 65 28 64 62 2c 20 70 49 6e 64 65 78 2d  Free(db, pIndex-
2d815 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 20 20 73  >zColAff);.    s
2d816 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2d817 20 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20 20   pIndex);.  }.  
2d818 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2d819 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29  elete(db, pList)
2d81a 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  ;.  sqlite3SrcLi
2d81b 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 62  stDelete(db, pTb
2d81c 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  lName);.  sqlite
2d81d 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d  3DbFree(db, zNam
2d81e 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65  e);.  return pRe
2d81f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c  t;.}../*.** Fill
2d820 20 74 68 65 20 49 6e 64 65 78 2e 61 69 52 6f 77   the Index.aiRow
2d821 45 73 74 5b 5d 20 61 72 72 61 79 20 77 69 74 68  Est[] array with
2d822 20 64 65 66 61 75 6c 74 20 69 6e 66 6f 72 6d 61   default informa
2d823 74 69 6f 6e 20 2d 20 69 6e 66 6f 72 6d 61 74 69  tion - informati
2d824 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 75 73 65 64  on.** to be used
2d825 20 77 68 65 6e 20 77 65 20 68 61 76 65 20 6e 6f   when we have no
2d826 74 20 72 75 6e 20 74 68 65 20 41 4e 41 4c 59 5a  t run the ANALYZ
2d827 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a  E command..**.**
2d828 20 61 69 52 6f 77 45 73 74 5b 30 5d 20 69 73 20   aiRowEst[0] is 
2d829 73 75 70 70 6f 73 65 20 74 6f 20 63 6f 6e 74 61  suppose to conta
2d82a 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  in the number of
2d82b 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65   elements in the
2d82c 20 69 6e 64 65 78 2e 0a 2a 2a 20 53 69 6e 63 65   index..** Since
2d82d 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 2c   we do not know,
2d82e 20 67 75 65 73 73 20 31 20 6d 69 6c 6c 69 6f 6e   guess 1 million
2d82f 2e 20 20 61 69 52 6f 77 45 73 74 5b 31 5d 20 69  .  aiRowEst[1] i
2d830 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66  s an estimate of
2d831 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
2d832 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61  f rows in the ta
2d833 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 20 61  ble that match a
2d834 6e 79 20 70 61 72 74 69 63 75 6c 61 72 20 76 61  ny particular va
2d835 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69  lue of the.** fi
2d836 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  rst column of th
2d837 65 20 69 6e 64 65 78 2e 20 20 61 69 52 6f 77 45  e index.  aiRowE
2d838 73 74 5b 32 5d 20 69 73 20 61 6e 20 65 73 74 69  st[2] is an esti
2d839 6d 61 74 65 20 6f 66 20 74 68 65 20 6e 75 6d 62  mate of the numb
2d83a 65 72 0a 2a 2a 20 6f 66 20 72 6f 77 73 20 74 68  er.** of rows th
2d83b 61 74 20 6d 61 74 63 68 20 61 6e 79 20 70 61 72  at match any par
2d83c 74 69 63 75 6c 61 72 20 63 6f 6d 62 69 6e 69 61  ticular combinia
2d83d 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 69 72 73  tion of the firs
2d83e 74 20 32 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 6f  t 2 columns.** o
2d83f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 41 6e  f the index.  An
2d840 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 74 20  d so forth.  It 
2d841 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65 20 74  must always be t
2d842 68 65 20 63 61 73 65 20 74 68 61 74 0a 2a 0a 2a  he case that.*.*
2d843 2a 20 20 20 20 20 20 20 20 20 20 20 61 69 52 6f  *           aiRo
2d844 77 45 73 74 5b 4e 5d 3c 3d 61 69 52 6f 77 45 73  wEst[N]<=aiRowEs
2d845 74 5b 4e 2d 31 5d 0a 2a 2a 20 20 20 20 20 20 20  t[N-1].**       
2d846 20 20 20 20 61 69 52 6f 77 45 73 74 5b 4e 5d 3e      aiRowEst[N]>
2d847 3d 31 0a 2a 2a 0a 2a 2a 20 41 70 61 72 74 20 66  =1.**.** Apart f
2d848 72 6f 6d 20 74 68 61 74 2c 20 77 65 20 68 61 76  rom that, we hav
2d849 65 20 6c 69 74 74 6c 65 20 74 6f 20 67 6f 20 6f  e little to go o
2d84a 6e 20 62 65 73 69 64 65 73 20 69 6e 74 75 69 74  n besides intuit
2d84b 69 6f 6e 20 61 73 20 74 6f 0a 2a 2a 20 68 6f 77  ion as to.** how
2d84c 20 61 69 52 6f 77 45 73 74 5b 5d 20 73 68 6f 75   aiRowEst[] shou
2d84d 6c 64 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  ld be initialize
2d84e 64 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 73 20  d.  The numbers 
2d84f 67 65 6e 65 72 61 74 65 64 20 68 65 72 65 0a 2a  generated here.*
2d850 2a 20 61 72 65 20 62 61 73 65 64 20 6f 6e 20 74  * are based on t
2d851 79 70 69 63 61 6c 20 76 61 6c 75 65 73 20 66 6f  ypical values fo
2d852 75 6e 64 20 69 6e 20 61 63 74 75 61 6c 20 69 6e  und in actual in
2d853 64 69 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  dices..*/.SQLITE
2d854 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2d855 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45  lite3DefaultRowE
2d856 73 74 28 49 6e 64 65 78 20 2a 70 49 64 78 29 7b  st(Index *pIdx){
2d857 0a 20 20 74 52 6f 77 63 6e 74 20 2a 61 20 3d 20  .  tRowcnt *a = 
2d858 70 49 64 78 2d 3e 61 69 52 6f 77 45 73 74 3b 0a  pIdx->aiRowEst;.
2d859 20 20 69 6e 74 20 69 3b 0a 20 20 74 52 6f 77 63    int i;.  tRowc
2d85a 6e 74 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  nt n;.  assert( 
2d85b 61 21 3d 30 20 29 3b 0a 20 20 61 5b 30 5d 20 3d  a!=0 );.  a[0] =
2d85c 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 6e   pIdx->pTable->n
2d85d 52 6f 77 45 73 74 3b 0a 20 20 69 66 28 20 61 5b  RowEst;.  if( a[
2d85e 30 5d 3c 31 30 20 29 20 61 5b 30 5d 20 3d 20 31  0]<10 ) a[0] = 1
2d85f 30 3b 0a 20 20 6e 20 3d 20 31 30 3b 0a 20 20 66  0;.  n = 10;.  f
2d860 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 49 64 78 2d  or(i=1; i<=pIdx-
2d861 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a  >nColumn; i++){.
2d862 20 20 20 20 61 5b 69 5d 20 3d 20 6e 3b 0a 20 20      a[i] = n;.  
2d863 20 20 69 66 28 20 6e 3e 35 20 29 20 6e 2d 2d 3b    if( n>5 ) n--;
2d864 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 64 78 2d  .  }.  if( pIdx-
2d865 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e  >onError!=OE_Non
2d866 65 20 29 7b 0a 20 20 20 20 61 5b 70 49 64 78 2d  e ){.    a[pIdx-
2d867 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 31 3b 0a 20  >nColumn] = 1;. 
2d868 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
2d869 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 72   routine will dr
2d86a 6f 70 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6e  op an existing n
2d86b 61 6d 65 64 20 69 6e 64 65 78 2e 20 20 54 68 69  amed index.  Thi
2d86c 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6d 70  s routine.** imp
2d86d 6c 65 6d 65 6e 74 73 20 74 68 65 20 44 52 4f 50  lements the DROP
2d86e 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74   INDEX statement
2d86f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2d870 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2d871 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65 20  DropIndex(Parse 
2d872 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74  *pParse, SrcList
2d873 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 69 66 45   *pName, int ifE
2d874 78 69 73 74 73 29 7b 0a 20 20 49 6e 64 65 78 20  xists){.  Index 
2d875 2a 70 49 6e 64 65 78 3b 0a 20 20 56 64 62 65 20  *pIndex;.  Vdbe 
2d876 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  *v;.  sqlite3 *d
2d877 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2d878 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 61 73    int iDb;..  as
2d879 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 45  sert( pParse->nE
2d87a 72 72 3d 3d 30 20 29 3b 20 20 20 2f 2a 20 4e 65  rr==0 );   /* Ne
2d87b 76 65 72 20 63 61 6c 6c 65 64 20 77 69 74 68 20  ver called with 
2d87c 70 72 69 6f 72 20 65 72 72 6f 72 73 20 2a 2f 0a  prior errors */.
2d87d 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2d87e 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
2d87f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64  to exit_drop_ind
2d880 65 78 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  ex;.  }.  assert
2d881 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31  ( pName->nSrc==1
2d882 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45   );.  if( SQLITE
2d883 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64  _OK!=sqlite3Read
2d884 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
2d885 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
2d886 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a  drop_index;.  }.
2d887 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74    pIndex = sqlit
2d888 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20  e3FindIndex(db, 
2d889 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d  pName->a[0].zNam
2d88a 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a  e, pName->a[0].z
2d88b 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28  Database);.  if(
2d88c 20 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20   pIndex==0 ){.  
2d88d 20 20 69 66 28 20 21 69 66 45 78 69 73 74 73 20    if( !ifExists 
2d88e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2d88f 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2d890 20 22 6e 6f 20 73 75 63 68 20 69 6e 64 65 78 3a   "no such index:
2d891 20 25 53 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b   %S", pName, 0);
2d892 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2d893 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72    sqlite3CodeVer
2d894 69 66 79 4e 61 6d 65 64 53 63 68 65 6d 61 28 70  ifyNamedSchema(p
2d895 50 61 72 73 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b  Parse, pName->a[
2d896 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  0].zDatabase);. 
2d897 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d     }.    pParse-
2d898 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31  >checkSchema = 1
2d899 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
2d89a 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a  drop_index;.  }.
2d89b 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 61 75    if( pIndex->au
2d89c 74 6f 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73  toIndex ){.    s
2d89d 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2d89e 50 61 72 73 65 2c 20 22 69 6e 64 65 78 20 61 73  Parse, "index as
2d89f 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 55 4e  sociated with UN
2d8a0 49 51 55 45 20 22 0a 20 20 20 20 20 20 22 6f 72  IQUE ".      "or
2d8a1 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e   PRIMARY KEY con
2d8a2 73 74 72 61 69 6e 74 20 63 61 6e 6e 6f 74 20 62  straint cannot b
2d8a3 65 20 64 72 6f 70 70 65 64 22 2c 20 30 29 3b 0a  e dropped", 0);.
2d8a4 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
2d8a5 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20  op_index;.  }.  
2d8a6 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
2d8a7 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
2d8a8 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 3b  Index->pSchema);
2d8a9 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2d8aa 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
2d8ab 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63  ON.  {.    int c
2d8ac 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f  ode = SQLITE_DRO
2d8ad 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 54 61 62  P_INDEX;.    Tab
2d8ae 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 6e 64 65  le *pTab = pInde
2d8af 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20 63  x->pTable;.    c
2d8b0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d  onst char *zDb =
2d8b1 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
2d8b2 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ame;.    const c
2d8b3 68 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45  har *zTab = SCHE
2d8b4 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20  MA_TABLE(iDb);. 
2d8b5 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
2d8b6 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
2d8b7 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a  SQLITE_DELETE, z
2d8b8 54 61 62 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a  Tab, 0, zDb) ){.
2d8b9 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
2d8ba 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20 20  drop_index;.    
2d8bb 7d 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f  }.    if( !OMIT_
2d8bc 54 45 4d 50 44 42 20 26 26 20 69 44 62 20 29 20  TEMPDB && iDb ) 
2d8bd 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
2d8be 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 3b 0a 20  OP_TEMP_INDEX;. 
2d8bf 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
2d8c0 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
2d8c1 63 6f 64 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e  code, pIndex->zN
2d8c2 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ame, pTab->zName
2d8c3 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20  , zDb) ){.      
2d8c4 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69  goto exit_drop_i
2d8c5 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ndex;.    }.  }.
2d8c6 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e  #endif..  /* Gen
2d8c7 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 72 65  erate code to re
2d8c8 6d 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20 61  move the index a
2d8c9 6e 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 74  nd from the mast
2d8ca 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 20  er table */.  v 
2d8cb 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2d8cc 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
2d8cd 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  v ){.    sqlite3
2d8ce 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
2d8cf 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69  ion(pParse, 1, i
2d8d0 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
2d8d1 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
2d8d2 73 65 2c 0a 20 20 20 20 20 20 20 22 44 45 4c 45  se,.       "DELE
2d8d3 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48  TE FROM %Q.%s WH
2d8d4 45 52 45 20 6e 61 6d 65 3d 25 51 20 41 4e 44 20  ERE name=%Q AND 
2d8d5 74 79 70 65 3d 27 69 6e 64 65 78 27 22 2c 0a 20  type='index'",. 
2d8d6 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
2d8d7 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41  b].zName, SCHEMA
2d8d8 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70 49 6e  _TABLE(iDb), pIn
2d8d9 64 65 78 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 29  dex->zName.    )
2d8da 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6c 65  ;.    sqlite3Cle
2d8db 61 72 53 74 61 74 54 61 62 6c 65 73 28 70 50 61  arStatTables(pPa
2d8dc 72 73 65 2c 20 69 44 62 2c 20 22 69 64 78 22 2c  rse, iDb, "idx",
2d8dd 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 3b   pIndex->zName);
2d8de 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e  .    sqlite3Chan
2d8df 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c  geCookie(pParse,
2d8e0 20 69 44 62 29 3b 0a 20 20 20 20 64 65 73 74 72   iDb);.    destr
2d8e1 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73  oyRootPage(pPars
2d8e2 65 2c 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2c  e, pIndex->tnum,
2d8e3 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74   iDb);.    sqlit
2d8e4 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2d8e5 4f 50 5f 44 72 6f 70 49 6e 64 65 78 2c 20 69 44  OP_DropIndex, iD
2d8e6 62 2c 20 30 2c 20 30 2c 20 70 49 6e 64 65 78 2d  b, 0, 0, pIndex-
2d8e7 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a  >zName, 0);.  }.
2d8e8 0a 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78  .exit_drop_index
2d8e9 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  :.  sqlite3SrcLi
2d8ea 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61  stDelete(db, pNa
2d8eb 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 41  me);.}../*.** pA
2d8ec 72 72 61 79 20 69 73 20 61 20 70 6f 69 6e 74 65  rray is a pointe
2d8ed 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66  r to an array of
2d8ee 20 6f 62 6a 65 63 74 73 2e 20 45 61 63 68 20 6f   objects. Each o
2d8ef 62 6a 65 63 74 20 69 6e 20 74 68 65 0a 2a 2a 20  bject in the.** 
2d8f0 61 72 72 61 79 20 69 73 20 73 7a 45 6e 74 72 79  array is szEntry
2d8f1 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20   bytes in size. 
2d8f2 54 68 69 73 20 72 6f 75 74 69 6e 65 20 75 73 65  This routine use
2d8f3 73 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c  s sqlite3DbReall
2d8f4 6f 63 28 29 0a 2a 2a 20 74 6f 20 65 78 74 65 6e  oc().** to exten
2d8f5 64 20 74 68 65 20 61 72 72 61 79 20 73 6f 20 74  d the array so t
2d8f6 68 61 74 20 74 68 65 72 65 20 69 73 20 73 70 61  hat there is spa
2d8f7 63 65 20 66 6f 72 20 61 20 6e 65 77 20 6f 62 6a  ce for a new obj
2d8f8 65 63 74 20 61 74 20 74 68 65 20 65 6e 64 2e 0a  ect at the end..
2d8f9 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20  **.** When this 
2d8fa 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
2d8fb 65 64 2c 20 2a 70 6e 45 6e 74 72 79 20 63 6f 6e  ed, *pnEntry con
2d8fc 74 61 69 6e 73 20 74 68 65 20 63 75 72 72 65 6e  tains the curren
2d8fd 74 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 68 65  t size of.** the
2d8fe 20 61 72 72 61 79 20 28 69 6e 20 65 6e 74 72 69   array (in entri
2d8ff 65 73 20 2d 20 73 6f 20 74 68 65 20 61 6c 6c 6f  es - so the allo
2d900 63 61 74 69 6f 6e 20 69 73 20 28 28 2a 70 6e 45  cation is ((*pnE
2d901 6e 74 72 79 29 20 2a 20 73 7a 45 6e 74 72 79 29  ntry) * szEntry)
2d902 20 62 79 74 65 73 0a 2a 2a 20 69 6e 20 74 6f 74   bytes.** in tot
2d903 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  al)..**.** If th
2d904 65 20 72 65 61 6c 6c 6f 63 28 29 20 69 73 20 73  e realloc() is s
2d905 75 63 63 65 73 73 66 75 6c 20 28 69 2e 65 2e 20  uccessful (i.e. 
2d906 69 66 20 6e 6f 20 4f 4f 4d 20 63 6f 6e 64 69 74  if no OOM condit
2d907 69 6f 6e 20 6f 63 63 75 72 73 29 2c 20 74 68 65  ion occurs), the
2d908 0a 2a 2a 20 73 70 61 63 65 20 61 6c 6c 6f 63 61  .** space alloca
2d909 74 65 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20  ted for the new 
2d90a 6f 62 6a 65 63 74 20 69 73 20 7a 65 72 6f 65 64  object is zeroed
2d90b 2c 20 2a 70 6e 45 6e 74 72 79 20 75 70 64 61 74  , *pnEntry updat
2d90c 65 64 20 74 6f 0a 2a 2a 20 72 65 66 6c 65 63 74  ed to.** reflect
2d90d 20 74 68 65 20 6e 65 77 20 73 69 7a 65 20 6f 66   the new size of
2d90e 20 74 68 65 20 61 72 72 61 79 20 61 6e 64 20 61   the array and a
2d90f 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2d910 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  new allocation.*
2d911 2a 20 72 65 74 75 72 6e 65 64 2e 20 2a 70 49 64  * returned. *pId
2d912 78 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  x is set to the 
2d913 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 65 77  index of the new
2d914 20 61 72 72 61 79 20 65 6e 74 72 79 20 69 6e 20   array entry in 
2d915 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a  this case..**.**
2d916 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74   Otherwise, if t
2d917 68 65 20 72 65 61 6c 6c 6f 63 28 29 20 66 61 69  he realloc() fai
2d918 6c 73 2c 20 2a 70 49 64 78 20 69 73 20 73 65 74  ls, *pIdx is set
2d919 20 74 6f 20 2d 31 2c 20 2a 70 6e 45 6e 74 72 79   to -1, *pnEntry
2d91a 20 72 65 6d 61 69 6e 73 0a 2a 2a 20 75 6e 63 68   remains.** unch
2d91b 61 6e 67 65 64 20 61 6e 64 20 61 20 63 6f 70 79  anged and a copy
2d91c 20 6f 66 20 70 41 72 72 61 79 20 72 65 74 75 72   of pArray retur
2d91d 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
2d91e 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
2d91f 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74  ite3ArrayAllocat
2d920 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
2d921 2c 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63  ,      /* Connec
2d922 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f  tion to notify o
2d923 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  f malloc failure
2d924 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72  s */.  void *pAr
2d925 72 61 79 2c 20 20 20 20 20 2f 2a 20 41 72 72 61  ray,     /* Arra
2d926 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20 4d  y of objects.  M
2d927 69 67 68 74 20 62 65 20 72 65 61 6c 6c 6f 63 61  ight be realloca
2d928 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45  ted */.  int szE
2d929 6e 74 72 79 2c 20 20 20 20 20 20 2f 2a 20 53 69  ntry,      /* Si
2d92a 7a 65 20 6f 66 20 65 61 63 68 20 6f 62 6a 65 63  ze of each objec
2d92b 74 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 2a  t in the array *
2d92c 2f 0a 20 20 69 6e 74 20 2a 70 6e 45 6e 74 72 79  /.  int *pnEntry
2d92d 2c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ,     /* Number 
2d92e 6f 66 20 6f 62 6a 65 63 74 73 20 63 75 72 72 65  of objects curre
2d92f 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20  ntly in use */. 
2d930 20 69 6e 74 20 2a 70 49 64 78 20 20 20 20 20 20   int *pIdx      
2d931 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
2d932 69 6e 64 65 78 20 6f 66 20 61 20 6e 65 77 20 73  index of a new s
2d933 6c 6f 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  lot here */.){. 
2d934 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20   char *z;.  int 
2d935 6e 20 3d 20 2a 70 6e 45 6e 74 72 79 3b 0a 20 20  n = *pnEntry;.  
2d936 69 66 28 20 28 6e 20 26 20 28 6e 2d 31 29 29 3d  if( (n & (n-1))=
2d937 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 73 7a  =0 ){.    int sz
2d938 20 3d 20 28 6e 3d 3d 30 29 20 3f 20 31 20 3a 20   = (n==0) ? 1 : 
2d939 32 2a 6e 3b 0a 20 20 20 20 76 6f 69 64 20 2a 70  2*n;.    void *p
2d93a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52  New = sqlite3DbR
2d93b 65 61 6c 6c 6f 63 28 64 62 2c 20 70 41 72 72 61  ealloc(db, pArra
2d93c 79 2c 20 73 7a 2a 73 7a 45 6e 74 72 79 29 3b 0a  y, sz*szEntry);.
2d93d 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20      if( pNew==0 
2d93e 29 7b 0a 20 20 20 20 20 20 2a 70 49 64 78 20 3d  ){.      *pIdx =
2d93f 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72   -1;.      retur
2d940 6e 20 70 41 72 72 61 79 3b 0a 20 20 20 20 7d 0a  n pArray;.    }.
2d941 20 20 20 20 70 41 72 72 61 79 20 3d 20 70 4e 65      pArray = pNe
2d942 77 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68  w;.  }.  z = (ch
2d943 61 72 2a 29 70 41 72 72 61 79 3b 0a 20 20 6d 65  ar*)pArray;.  me
2d944 6d 73 65 74 28 26 7a 5b 6e 20 2a 20 73 7a 45 6e  mset(&z[n * szEn
2d945 74 72 79 5d 2c 20 30 2c 20 73 7a 45 6e 74 72 79  try], 0, szEntry
2d946 29 3b 0a 20 20 2a 70 49 64 78 20 3d 20 6e 3b 0a  );.  *pIdx = n;.
2d947 20 20 2b 2b 2a 70 6e 45 6e 74 72 79 3b 0a 20 20    ++*pnEntry;.  
2d948 72 65 74 75 72 6e 20 70 41 72 72 61 79 3b 0a 7d  return pArray;.}
2d949 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61  ../*.** Append a
2d94a 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20   new element to 
2d94b 74 68 65 20 67 69 76 65 6e 20 49 64 4c 69 73 74  the given IdList
2d94c 2e 20 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  .  Create a new 
2d94d 49 64 4c 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65  IdList if.** nee
2d94e 64 20 62 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65  d be..**.** A ne
2d94f 77 20 49 64 4c 69 73 74 20 69 73 20 72 65 74 75  w IdList is retu
2d950 72 6e 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66  rned, or NULL if
2d951 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e   malloc() fails.
2d952 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2d953 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74  TE IdList *sqlit
2d954 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73  e3IdListAppend(s
2d955 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69  qlite3 *db, IdLi
2d956 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e  st *pList, Token
2d957 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74   *pToken){.  int
2d958 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d   i;.  if( pList=
2d959 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20  =0 ){.    pList 
2d95a 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2d95b 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2d95c 28 49 64 4c 69 73 74 29 20 29 3b 0a 20 20 20 20  (IdList) );.    
2d95d 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72  if( pList==0 ) r
2d95e 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70  eturn 0;.  }.  p
2d95f 4c 69 73 74 2d 3e 61 20 3d 20 73 71 6c 69 74 65  List->a = sqlite
2d960 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a  3ArrayAllocate(.
2d961 20 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20        db,.      
2d962 70 4c 69 73 74 2d 3e 61 2c 0a 20 20 20 20 20 20  pList->a,.      
2d963 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b  sizeof(pList->a[
2d964 30 5d 29 2c 0a 20 20 20 20 20 20 26 70 4c 69 73  0]),.      &pLis
2d965 74 2d 3e 6e 49 64 2c 0a 20 20 20 20 20 20 26 69  t->nId,.      &i
2d966 0a 20 20 29 3b 0a 20 20 69 66 28 20 69 3c 30 20  .  );.  if( i<0 
2d967 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64  ){.    sqlite3Id
2d968 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2d969 4c 69 73 74 29 3b 0a 20 20 20 20 72 65 74 75 72  List);.    retur
2d96a 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74  n 0;.  }.  pList
2d96b 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 3d 20 73  ->a[i].zName = s
2d96c 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
2d96d 6b 65 6e 28 64 62 2c 20 70 54 6f 6b 65 6e 29 3b  ken(db, pToken);
2d96e 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74 3b  .  return pList;
2d96f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
2d970 20 61 6e 20 49 64 4c 69 73 74 2e 0a 2a 2f 0a 53   an IdList..*/.S
2d971 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2d972 69 64 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  id sqlite3IdList
2d973 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a  Delete(sqlite3 *
2d974 64 62 2c 20 49 64 4c 69 73 74 20 2a 70 4c 69 73  db, IdList *pLis
2d975 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  t){.  int i;.  i
2d976 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
2d977 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  turn;.  for(i=0;
2d978 20 69 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 69   i<pList->nId; i
2d979 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
2d97a 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74  DbFree(db, pList
2d97b 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  ->a[i].zName);. 
2d97c 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72   }.  sqlite3DbFr
2d97d 65 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 29  ee(db, pList->a)
2d97e 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
2d97f 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a  e(db, pList);.}.
2d980 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
2d981 65 20 69 6e 64 65 78 20 69 6e 20 70 4c 69 73 74  e index in pList
2d982 20 6f 66 20 74 68 65 20 69 64 65 6e 74 69 66 69   of the identifi
2d983 65 72 20 6e 61 6d 65 64 20 7a 49 64 2e 20 20 52  er named zId.  R
2d984 65 74 75 72 6e 20 2d 31 0a 2a 2a 20 69 66 20 6e  eturn -1.** if n
2d985 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c  ot found..*/.SQL
2d986 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2d987 73 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64  sqlite3IdListInd
2d988 65 78 28 49 64 4c 69 73 74 20 2a 70 4c 69 73 74  ex(IdList *pList
2d989 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
2d98a 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ame){.  int i;. 
2d98b 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20   if( pList==0 ) 
2d98c 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 66 6f 72  return -1;.  for
2d98d 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
2d98e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  Id; i++){.    if
2d98f 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
2d990 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61  (pList->a[i].zNa
2d991 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 20  me, zName)==0 ) 
2d992 72 65 74 75 72 6e 20 69 3b 0a 20 20 7d 0a 20 20  return i;.  }.  
2d993 72 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a  return -1;.}../*
2d994 0a 2a 2a 20 45 78 70 61 6e 64 20 74 68 65 20 73  .** Expand the s
2d995 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 66  pace allocated f
2d996 6f 72 20 74 68 65 20 67 69 76 65 6e 20 53 72 63  or the given Src
2d997 4c 69 73 74 20 6f 62 6a 65 63 74 20 62 79 0a 2a  List object by.*
2d998 2a 20 63 72 65 61 74 69 6e 67 20 6e 45 78 74 72  * creating nExtr
2d999 61 20 6e 65 77 20 73 6c 6f 74 73 20 62 65 67 69  a new slots begi
2d99a 6e 6e 69 6e 67 20 61 74 20 69 53 74 61 72 74 2e  nning at iStart.
2d99b 20 20 69 53 74 61 72 74 20 69 73 20 7a 65 72 6f    iStart is zero
2d99c 20 62 61 73 65 64 2e 0a 2a 2a 20 4e 65 77 20 73   based..** New s
2d99d 6c 6f 74 73 20 61 72 65 20 7a 65 72 6f 65 64 2e  lots are zeroed.
2d99e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  .**.** For examp
2d99f 6c 65 2c 20 73 75 70 70 6f 73 65 20 61 20 53 72  le, suppose a Sr
2d9a0 63 4c 69 73 74 20 69 6e 69 74 69 61 6c 6c 79 20  cList initially 
2d9a1 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20 65 6e 74  contains two ent
2d9a2 72 69 65 73 3a 20 41 2c 42 2e 0a 2a 2a 20 54 6f  ries: A,B..** To
2d9a3 20 61 70 70 65 6e 64 20 33 20 6e 65 77 20 65 6e   append 3 new en
2d9a4 74 72 69 65 73 20 6f 6e 74 6f 20 74 68 65 20 65  tries onto the e
2d9a5 6e 64 2c 20 64 6f 20 74 68 69 73 3a 0a 2a 2a 0a  nd, do this:.**.
2d9a6 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 53 72 63  **    sqlite3Src
2d9a7 4c 69 73 74 45 6e 6c 61 72 67 65 28 64 62 2c 20  ListEnlarge(db, 
2d9a8 70 53 72 63 6c 69 73 74 2c 20 33 2c 20 32 29 3b  pSrclist, 3, 2);
2d9a9 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65  .**.** After the
2d9aa 20 63 61 6c 6c 20 61 62 6f 76 65 20 69 74 20 77   call above it w
2d9ab 6f 75 6c 64 20 63 6f 6e 74 61 69 6e 3a 20 20 41  ould contain:  A
2d9ac 2c 20 42 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e  , B, nil, nil, n
2d9ad 69 6c 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 53  il..** If the iS
2d9ae 74 61 72 74 20 61 72 67 75 6d 65 6e 74 20 68 61  tart argument ha
2d9af 64 20 62 65 65 6e 20 31 20 69 6e 73 74 65 61 64  d been 1 instead
2d9b0 20 6f 66 20 32 2c 20 74 68 65 6e 20 74 68 65 20   of 2, then the 
2d9b1 72 65 73 75 6c 74 0a 2a 2a 20 77 6f 75 6c 64 20  result.** would 
2d9b2 68 61 76 65 20 62 65 65 6e 3a 20 20 41 2c 20 6e  have been:  A, n
2d9b3 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 42 2e  il, nil, nil, B.
2d9b4 20 20 54 6f 20 70 72 65 70 65 6e 64 20 74 68 65    To prepend the
2d9b5 20 6e 65 77 20 73 6c 6f 74 73 2c 0a 2a 2a 20 74   new slots,.** t
2d9b6 68 65 20 69 53 74 61 72 74 20 76 61 6c 75 65 20  he iStart value 
2d9b7 77 6f 75 6c 64 20 62 65 20 30 2e 20 20 54 68 65  would be 0.  The
2d9b8 20 72 65 73 75 6c 74 20 74 68 65 6e 20 77 6f 75   result then wou
2d9b9 6c 64 0a 2a 2a 20 62 65 3a 20 6e 69 6c 2c 20 6e  ld.** be: nil, n
2d9ba 69 6c 2c 20 6e 69 6c 2c 20 41 2c 20 42 2e 0a 2a  il, nil, A, B..*
2d9bb 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79  *.** If a memory
2d9bc 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
2d9bd 73 20 74 68 65 20 53 72 63 4c 69 73 74 20 69 73  s the SrcList is
2d9be 20 75 6e 63 68 61 6e 67 65 64 2e 20 20 54 68 65   unchanged.  The
2d9bf 0a 2a 2a 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  .** db->mallocFa
2d9c0 69 6c 65 64 20 66 6c 61 67 20 77 69 6c 6c 20 62  iled flag will b
2d9c1 65 20 73 65 74 20 74 6f 20 74 72 75 65 2e 0a 2a  e set to true..*
2d9c2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2d9c3 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65   SrcList *sqlite
2d9c4 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28  3SrcListEnlarge(
2d9c5 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2d9c6 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2d9c7 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  e connection to 
2d9c8 6e 6f 74 69 66 79 20 6f 66 20 4f 4f 4d 20 65 72  notify of OOM er
2d9c9 72 6f 72 73 20 2a 2f 0a 20 20 53 72 63 4c 69 73  rors */.  SrcLis
2d9ca 74 20 2a 70 53 72 63 2c 20 20 20 20 20 2f 2a 20  t *pSrc,     /* 
2d9cb 54 68 65 20 53 72 63 4c 69 73 74 20 74 6f 20 62  The SrcList to b
2d9cc 65 20 65 6e 6c 61 72 67 65 64 20 2a 2f 0a 20 20  e enlarged */.  
2d9cd 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 20 20 20  int nExtra,     
2d9ce 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2d9cf 6e 65 77 20 73 6c 6f 74 73 20 74 6f 20 61 64 64  new slots to add
2d9d0 20 74 6f 20 70 53 72 63 2d 3e 61 5b 5d 20 2a 2f   to pSrc->a[] */
2d9d1 0a 20 20 69 6e 74 20 69 53 74 61 72 74 20 20 20  .  int iStart   
2d9d2 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
2d9d3 6e 20 70 53 72 63 2d 3e 61 5b 5d 20 6f 66 20 66  n pSrc->a[] of f
2d9d4 69 72 73 74 20 6e 65 77 20 73 6c 6f 74 20 2a 2f  irst new slot */
2d9d5 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20  .){.  int i;..  
2d9d6 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69  /* Sanity checki
2d9d7 6e 67 20 6f 6e 20 63 61 6c 6c 69 6e 67 20 70 61  ng on calling pa
2d9d8 72 61 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73  rameters */.  as
2d9d9 73 65 72 74 28 20 69 53 74 61 72 74 3e 3d 30 20  sert( iStart>=0 
2d9da 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 45 78  );.  assert( nEx
2d9db 74 72 61 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65  tra>=1 );.  asse
2d9dc 72 74 28 20 70 53 72 63 21 3d 30 20 29 3b 0a 20  rt( pSrc!=0 );. 
2d9dd 20 61 73 73 65 72 74 28 20 69 53 74 61 72 74 3c   assert( iStart<
2d9de 3d 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 0a  =pSrc->nSrc );..
2d9df 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 64    /* Allocate ad
2d9e0 64 69 74 69 6f 6e 61 6c 20 73 70 61 63 65 20 69  ditional space i
2d9e1 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 69 66  f needed */.  if
2d9e2 28 20 70 53 72 63 2d 3e 6e 53 72 63 2b 6e 45 78  ( pSrc->nSrc+nEx
2d9e3 74 72 61 3e 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63  tra>pSrc->nAlloc
2d9e4 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20   ){.    SrcList 
2d9e5 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e  *pNew;.    int n
2d9e6 41 6c 6c 6f 63 20 3d 20 70 53 72 63 2d 3e 6e 53  Alloc = pSrc->nS
2d9e7 72 63 2b 6e 45 78 74 72 61 3b 0a 20 20 20 20 69  rc+nExtra;.    i
2d9e8 6e 74 20 6e 47 6f 74 3b 0a 20 20 20 20 70 4e 65  nt nGot;.    pNe
2d9e9 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  w = sqlite3DbRea
2d9ea 6c 6c 6f 63 28 64 62 2c 20 70 53 72 63 2c 0a 20  lloc(db, pSrc,. 
2d9eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69                si
2d9ec 7a 65 6f 66 28 2a 70 53 72 63 29 20 2b 20 28 6e  zeof(*pSrc) + (n
2d9ed 41 6c 6c 6f 63 2d 31 29 2a 73 69 7a 65 6f 66 28  Alloc-1)*sizeof(
2d9ee 70 53 72 63 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20  pSrc->a[0]) );. 
2d9ef 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29     if( pNew==0 )
2d9f0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
2d9f1 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2d9f2 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   );.      return
2d9f3 20 70 53 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20   pSrc;.    }.   
2d9f4 20 70 53 72 63 20 3d 20 70 4e 65 77 3b 0a 20 20   pSrc = pNew;.  
2d9f5 20 20 6e 47 6f 74 20 3d 20 28 73 71 6c 69 74 65    nGot = (sqlite
2d9f6 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62  3DbMallocSize(db
2d9f7 2c 20 70 4e 65 77 29 20 2d 20 73 69 7a 65 6f 66  , pNew) - sizeof
2d9f8 28 2a 70 53 72 63 29 29 2f 73 69 7a 65 6f 66 28  (*pSrc))/sizeof(
2d9f9 70 53 72 63 2d 3e 61 5b 30 5d 29 2b 31 3b 0a 20  pSrc->a[0])+1;. 
2d9fa 20 20 20 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20     pSrc->nAlloc 
2d9fb 3d 20 28 75 31 36 29 6e 47 6f 74 3b 0a 20 20 7d  = (u16)nGot;.  }
2d9fc 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 65 78 69 73  ..  /* Move exis
2d9fd 74 69 6e 67 20 73 6c 6f 74 73 20 74 68 61 74 20  ting slots that 
2d9fe 63 6f 6d 65 20 61 66 74 65 72 20 74 68 65 20 6e  come after the n
2d9ff 65 77 6c 79 20 69 6e 73 65 72 74 65 64 20 73 6c  ewly inserted sl
2da00 6f 74 73 0a 20 20 2a 2a 20 6f 75 74 20 6f 66 20  ots.  ** out of 
2da01 74 68 65 20 77 61 79 20 2a 2f 0a 20 20 66 6f 72  the way */.  for
2da02 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2d 31 3b  (i=pSrc->nSrc-1;
2da03 20 69 3e 3d 69 53 74 61 72 74 3b 20 69 2d 2d 29   i>=iStart; i--)
2da04 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 2b  {.    pSrc->a[i+
2da05 6e 45 78 74 72 61 5d 20 3d 20 70 53 72 63 2d 3e  nExtra] = pSrc->
2da06 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 70 53 72 63  a[i];.  }.  pSrc
2da07 2d 3e 6e 53 72 63 20 2b 3d 20 28 69 31 36 29 6e  ->nSrc += (i16)n
2da08 45 78 74 72 61 3b 0a 0a 20 20 2f 2a 20 5a 65 72  Extra;..  /* Zer
2da09 6f 20 74 68 65 20 6e 65 77 6c 79 20 61 6c 6c 6f  o the newly allo
2da0a 63 61 74 65 64 20 73 6c 6f 74 73 20 2a 2f 0a 20  cated slots */. 
2da0b 20 6d 65 6d 73 65 74 28 26 70 53 72 63 2d 3e 61   memset(&pSrc->a
2da0c 5b 69 53 74 61 72 74 5d 2c 20 30 2c 20 73 69 7a  [iStart], 0, siz
2da0d 65 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 2a  eof(pSrc->a[0])*
2da0e 6e 45 78 74 72 61 29 3b 0a 20 20 66 6f 72 28 69  nExtra);.  for(i
2da0f 3d 69 53 74 61 72 74 3b 20 69 3c 69 53 74 61 72  =iStart; i<iStar
2da10 74 2b 6e 45 78 74 72 61 3b 20 69 2b 2b 29 7b 0a  t+nExtra; i++){.
2da11 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 5d 2e 69      pSrc->a[i].i
2da12 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 7d  Cursor = -1;.  }
2da13 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 20  ..  /* Return a 
2da14 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65  pointer to the e
2da15 6e 6c 61 72 67 65 64 20 53 72 63 4c 69 73 74 20  nlarged SrcList 
2da16 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70 53 72 63  */.  return pSrc
2da17 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65  ;.}.../*.** Appe
2da18 6e 64 20 61 20 6e 65 77 20 74 61 62 6c 65 20 6e  nd a new table n
2da19 61 6d 65 20 74 6f 20 74 68 65 20 67 69 76 65 6e  ame to the given
2da1a 20 53 72 63 4c 69 73 74 2e 20 20 43 72 65 61 74   SrcList.  Creat
2da1b 65 20 61 20 6e 65 77 20 53 72 63 4c 69 73 74 20  e a new SrcList 
2da1c 69 66 0a 2a 2a 20 6e 65 65 64 20 62 65 2e 20 20  if.** need be.  
2da1d 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 63  A new entry is c
2da1e 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 53 72  reated in the Sr
2da1f 63 4c 69 73 74 20 65 76 65 6e 20 69 66 20 70 54  cList even if pT
2da20 61 62 6c 65 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a  able is NULL..**
2da21 0a 2a 2a 20 41 20 53 72 63 4c 69 73 74 20 69 73  .** A SrcList is
2da22 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20 4e 55   returned, or NU
2da23 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 61  LL if there is a
2da24 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 54 68  n OOM error.  Th
2da25 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 53 72  e returned.** Sr
2da26 63 4c 69 73 74 20 6d 69 67 68 74 20 62 65 20 74  cList might be t
2da27 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 53  he same as the S
2da28 72 63 4c 69 73 74 20 74 68 61 74 20 77 61 73 20  rcList that was 
2da29 69 6e 70 75 74 20 6f 72 20 69 74 20 6d 69 67 68  input or it migh
2da2a 74 20 62 65 0a 2a 2a 20 61 20 6e 65 77 20 6f 6e  t be.** a new on
2da2b 65 2e 20 20 49 66 20 61 6e 20 4f 4f 4d 20 65 72  e.  If an OOM er
2da2c 72 6f 72 20 64 6f 65 73 20 6f 63 63 75 72 73 2c  ror does occurs,
2da2d 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20   then the prior 
2da2e 76 61 6c 75 65 20 6f 66 20 70 4c 69 73 74 0a 2a  value of pList.*
2da2f 2a 20 74 68 61 74 20 69 73 20 69 6e 70 75 74 20  * that is input 
2da30 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
2da31 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
2da32 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66   freed..**.** If
2da33 20 70 44 61 74 61 62 61 73 65 20 69 73 20 6e 6f   pDatabase is no
2da34 74 20 6e 75 6c 6c 2c 20 69 74 20 6d 65 61 6e 73  t null, it means
2da35 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20   that the table 
2da36 68 61 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 0a  has an optional.
2da37 2a 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  ** database name
2da38 20 70 72 65 66 69 78 2e 20 20 4c 69 6b 65 20 74   prefix.  Like t
2da39 68 69 73 3a 20 20 22 64 61 74 61 62 61 73 65 2e  his:  "database.
2da3a 74 61 62 6c 65 22 2e 20 20 54 68 65 20 70 44 61  table".  The pDa
2da3b 74 61 62 61 73 65 0a 2a 2a 20 70 6f 69 6e 74 73  tabase.** points
2da3c 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6e 61   to the table na
2da3d 6d 65 20 61 6e 64 20 74 68 65 20 70 54 61 62 6c  me and the pTabl
2da3e 65 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  e points to the 
2da3f 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2e 0a 2a  database name..*
2da40 2a 20 54 68 65 20 53 72 63 4c 69 73 74 2e 61 5b  * The SrcList.a[
2da41 5d 2e 7a 4e 61 6d 65 20 66 69 65 6c 64 20 69 73  ].zName field is
2da42 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65   filled with the
2da43 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 68 69 63   table name whic
2da44 68 20 6d 69 67 68 74 0a 2a 2a 20 63 6f 6d 65 20  h might.** come 
2da45 66 72 6f 6d 20 70 54 61 62 6c 65 20 28 69 66 20  from pTable (if 
2da46 70 44 61 74 61 62 61 73 65 20 69 73 20 4e 55 4c  pDatabase is NUL
2da47 4c 29 20 6f 72 20 66 72 6f 6d 20 70 44 61 74 61  L) or from pData
2da48 62 61 73 65 2e 20 20 0a 2a 2a 20 53 72 63 4c 69  base.  .** SrcLi
2da49 73 74 2e 61 5b 5d 2e 7a 44 61 74 61 62 61 73 65  st.a[].zDatabase
2da4a 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20   is filled with 
2da4b 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
2da4c 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 2c 0a 2a  e from pTable,.*
2da4d 2a 20 6f 72 20 77 69 74 68 20 4e 55 4c 4c 20 69  * or with NULL i
2da4e 66 20 6e 6f 20 64 61 74 61 62 61 73 65 20 69 73  f no database is
2da4f 20 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a   specified..**.*
2da50 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * In other words
2da51 2c 20 69 66 20 63 61 6c 6c 20 6c 69 6b 65 20 74  , if call like t
2da52 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  his:.**.**      
2da53 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73     sqlite3SrcLis
2da54 74 41 70 70 65 6e 64 28 44 2c 41 2c 42 2c 30 29  tAppend(D,A,B,0)
2da55 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 42 20 69  ;.**.** Then B i
2da56 73 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 61  s a table name a
2da57 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  nd the database 
2da58 6e 61 6d 65 20 69 73 20 75 6e 73 70 65 63 69 66  name is unspecif
2da59 69 65 64 2e 20 20 49 66 20 63 61 6c 6c 65 64 0a  ied.  If called.
2da5a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ** like this:.**
2da5b 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 71 6c 69  .**         sqli
2da5c 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
2da5d 28 44 2c 41 2c 42 2c 43 29 3b 0a 2a 2a 0a 2a 2a  (D,A,B,C);.**.**
2da5e 20 54 68 65 6e 20 43 20 69 73 20 74 68 65 20 74   Then C is the t
2da5f 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 42 20  able name and B 
2da60 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  is the database 
2da61 6e 61 6d 65 2e 20 20 49 66 20 43 20 69 73 20 64  name.  If C is d
2da62 65 66 69 6e 65 64 0a 2a 2a 20 74 68 65 6e 20 73  efined.** then s
2da63 6f 20 69 73 20 42 2e 20 20 49 6e 20 6f 74 68 65  o is B.  In othe
2da64 72 20 77 6f 72 64 73 2c 20 77 65 20 6e 65 76 65  r words, we neve
2da65 72 20 68 61 76 65 20 61 20 63 61 73 65 20 77 68  r have a case wh
2da66 65 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ere:.**.**      
2da67 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73     sqlite3SrcLis
2da68 74 41 70 70 65 6e 64 28 44 2c 41 2c 30 2c 43 29  tAppend(D,A,0,C)
2da69 3b 0a 2a 2a 0a 2a 2a 20 42 6f 74 68 20 70 54 61  ;.**.** Both pTa
2da6a 62 6c 65 20 61 6e 64 20 70 44 61 74 61 62 61 73  ble and pDatabas
2da6b 65 20 61 72 65 20 61 73 73 75 6d 65 64 20 74 6f  e are assumed to
2da6c 20 62 65 20 71 75 6f 74 65 64 2e 20 20 54 68 65   be quoted.  The
2da6d 79 20 61 72 65 20 64 65 71 75 6f 74 65 64 0a 2a  y are dequoted.*
2da6e 2a 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20 61  * before being a
2da6f 64 64 65 64 20 74 6f 20 74 68 65 20 53 72 63 4c  dded to the SrcL
2da70 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ist..*/.SQLITE_P
2da71 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a  RIVATE SrcList *
2da72 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
2da73 70 65 6e 64 28 0a 20 20 73 71 6c 69 74 65 33 20  pend(.  sqlite3 
2da74 2a 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 43  *db,        /* C
2da75 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74  onnection to not
2da76 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61  ify of malloc fa
2da77 69 6c 75 72 65 73 20 2a 2f 0a 20 20 53 72 63 4c  ilures */.  SrcL
2da78 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20  ist *pList,     
2da79 2f 2a 20 41 70 70 65 6e 64 20 74 6f 20 74 68 69  /* Append to thi
2da7a 73 20 53 72 63 4c 69 73 74 2e 20 4e 55 4c 4c 20  s SrcList. NULL 
2da7b 63 72 65 61 74 65 73 20 61 20 6e 65 77 20 53 72  creates a new Sr
2da7c 63 4c 69 73 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e  cList */.  Token
2da7d 20 2a 70 54 61 62 6c 65 2c 20 20 20 20 20 20 2f   *pTable,      /
2da7e 2a 20 54 61 62 6c 65 20 74 6f 20 61 70 70 65 6e  * Table to appen
2da7f 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44  d */.  Token *pD
2da80 61 74 61 62 61 73 65 20 20 20 20 2f 2a 20 44 61  atabase    /* Da
2da81 74 61 62 61 73 65 20 6f 66 20 74 68 65 20 74 61  tabase of the ta
2da82 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75  ble */.){.  stru
2da83 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2da84 2a 70 49 74 65 6d 3b 0a 20 20 61 73 73 65 72 74  *pItem;.  assert
2da85 28 20 70 44 61 74 61 62 61 73 65 3d 3d 30 20 7c  ( pDatabase==0 |
2da86 7c 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 20 20  | pTable!=0 );  
2da87 2f 2a 20 43 61 6e 6e 6f 74 20 68 61 76 65 20 43  /* Cannot have C
2da88 20 77 69 74 68 6f 75 74 20 42 20 2a 2f 0a 20 20   without B */.  
2da89 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a  if( pList==0 ){.
2da8a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69      pList = sqli
2da8b 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
2da8c 64 62 2c 20 73 69 7a 65 6f 66 28 53 72 63 4c 69  db, sizeof(SrcLi
2da8d 73 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70  st) );.    if( p
2da8e 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  List==0 ) return
2da8f 20 30 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e   0;.    pList->n
2da90 41 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 7d 0a 20  Alloc = 1;.  }. 
2da91 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33   pList = sqlite3
2da92 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 64  SrcListEnlarge(d
2da93 62 2c 20 70 4c 69 73 74 2c 20 31 2c 20 70 4c 69  b, pList, 1, pLi
2da94 73 74 2d 3e 6e 53 72 63 29 3b 0a 20 20 69 66 28  st->nSrc);.  if(
2da95 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2da96 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
2da97 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62  SrcListDelete(db
2da98 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 72 65  , pList);.    re
2da99 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 49  turn 0;.  }.  pI
2da9a 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b  tem = &pList->a[
2da9b 70 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 5d 3b 0a  pList->nSrc-1];.
2da9c 20 20 69 66 28 20 70 44 61 74 61 62 61 73 65 20    if( pDatabase 
2da9d 26 26 20 70 44 61 74 61 62 61 73 65 2d 3e 7a 3d  && pDatabase->z=
2da9e 3d 30 20 29 7b 0a 20 20 20 20 70 44 61 74 61 62  =0 ){.    pDatab
2da9f 61 73 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  ase = 0;.  }.  i
2daa0 66 28 20 70 44 61 74 61 62 61 73 65 20 29 7b 0a  f( pDatabase ){.
2daa1 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 65 6d 70      Token *pTemp
2daa2 20 3d 20 70 44 61 74 61 62 61 73 65 3b 0a 20 20   = pDatabase;.  
2daa3 20 20 70 44 61 74 61 62 61 73 65 20 3d 20 70 54    pDatabase = pT
2daa4 61 62 6c 65 3b 0a 20 20 20 20 70 54 61 62 6c 65  able;.    pTable
2daa5 20 3d 20 70 54 65 6d 70 3b 0a 20 20 7d 0a 20 20   = pTemp;.  }.  
2daa6 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73  pItem->zName = s
2daa7 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
2daa8 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 29 3b  ken(db, pTable);
2daa9 0a 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62  .  pItem->zDatab
2daaa 61 73 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ase = sqlite3Nam
2daab 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
2daac 44 61 74 61 62 61 73 65 29 3b 0a 20 20 72 65 74  Database);.  ret
2daad 75 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a  urn pList;.}../*
2daae 0a 2a 2a 20 41 73 73 69 67 6e 20 56 64 62 65 43  .** Assign VdbeC
2daaf 75 72 73 6f 72 20 69 6e 64 65 78 20 6e 75 6d 62  ursor index numb
2dab0 65 72 73 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65  ers to all table
2dab1 73 20 69 6e 20 61 20 53 72 63 4c 69 73 74 0a 2a  s in a SrcList.*
2dab2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2dab3 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63   void sqlite3Src
2dab4 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72  ListAssignCursor
2dab5 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  s(Parse *pParse,
2dab6 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 29   SrcList *pList)
2dab7 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  {.  int i;.  str
2dab8 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2dab9 20 2a 70 49 74 65 6d 3b 0a 20 20 61 73 73 65 72   *pItem;.  asser
2daba 74 28 70 4c 69 73 74 20 7c 7c 20 70 50 61 72 73  t(pList || pPars
2dabb 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
2dabc 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 4c 69  led );.  if( pLi
2dabd 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  st ){.    for(i=
2dabe 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e  0, pItem=pList->
2dabf 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63  a; i<pList->nSrc
2dac0 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
2dac1 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d  .      if( pItem
2dac2 2d 3e 69 43 75 72 73 6f 72 3e 3d 30 20 29 20 62  ->iCursor>=0 ) b
2dac3 72 65 61 6b 3b 0a 20 20 20 20 20 20 70 49 74 65  reak;.      pIte
2dac4 6d 2d 3e 69 43 75 72 73 6f 72 20 3d 20 70 50 61  m->iCursor = pPa
2dac5 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
2dac6 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53     if( pItem->pS
2dac7 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20  elect ){.       
2dac8 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
2dac9 73 73 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61  ssignCursors(pPa
2daca 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c  rse, pItem->pSel
2dacb 65 63 74 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20  ect->pSrc);.    
2dacc 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
2dacd 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  ./*.** Delete an
2dace 20 65 6e 74 69 72 65 20 53 72 63 4c 69 73 74 20   entire SrcList 
2dacf 69 6e 63 6c 75 64 69 6e 67 20 61 6c 6c 20 69 74  including all it
2dad0 73 20 73 75 62 73 74 72 75 63 74 75 72 65 2e 0a  s substructure..
2dad1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2dad2 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72  E void sqlite3Sr
2dad3 63 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69  cListDelete(sqli
2dad4 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74  te3 *db, SrcList
2dad5 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20   *pList){.  int 
2dad6 69 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  i;.  struct SrcL
2dad7 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
2dad8 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20  .  if( pList==0 
2dad9 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28  ) return;.  for(
2dada 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20  pItem=pList->a, 
2dadb 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53  i=0; i<pList->nS
2dadc 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  rc; i++, pItem++
2dadd 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ){.    sqlite3Db
2dade 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e  Free(db, pItem->
2dadf 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20  zDatabase);.    
2dae0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2dae1 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b  , pItem->zName);
2dae2 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2dae3 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 41  ee(db, pItem->zA
2dae4 6c 69 61 73 29 3b 0a 20 20 20 20 73 71 6c 69 74  lias);.    sqlit
2dae5 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74  e3DbFree(db, pIt
2dae6 65 6d 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20 20 20  em->zIndex);.   
2dae7 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
2dae8 62 6c 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70  ble(db, pItem->p
2dae9 54 61 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Tab);.    sqlite
2daea 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2daeb 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74  , pItem->pSelect
2daec 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
2daed 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74  prDelete(db, pIt
2daee 65 6d 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 73 71  em->pOn);.    sq
2daef 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
2daf0 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 55 73  e(db, pItem->pUs
2daf1 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ing);.  }.  sqli
2daf2 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
2daf3 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ist);.}../*.** T
2daf4 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
2daf5 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
2daf6 73 65 72 20 74 6f 20 61 64 64 20 61 20 6e 65 77  ser to add a new
2daf7 20 74 65 72 6d 20 74 6f 20 74 68 65 0a 2a 2a 20   term to the.** 
2daf8 65 6e 64 20 6f 66 20 61 20 67 72 6f 77 69 6e 67  end of a growing
2daf9 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54   FROM clause.  T
2dafa 68 65 20 22 70 22 20 70 61 72 61 6d 65 74 65 72  he "p" parameter
2dafb 20 69 73 20 74 68 65 20 70 61 72 74 20 6f 66 0a   is the part of.
2dafc 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  ** the FROM clau
2dafd 73 65 20 74 68 61 74 20 68 61 73 20 61 6c 72 65  se that has alre
2dafe 61 64 79 20 62 65 65 6e 20 63 6f 6e 73 74 72 75  ady been constru
2daff 63 74 65 64 2e 20 20 22 70 22 20 69 73 20 4e 55  cted.  "p" is NU
2db00 4c 4c 0a 2a 2a 20 69 66 20 74 68 69 73 20 69 73  LL.** if this is
2db01 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 20   the first term 
2db02 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
2db03 73 65 2e 20 20 70 54 61 62 6c 65 20 61 6e 64 20  se.  pTable and 
2db04 70 44 61 74 61 62 61 73 65 0a 2a 2a 20 61 72 65  pDatabase.** are
2db05 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
2db06 20 74 61 62 6c 65 20 61 6e 64 20 64 61 74 61 62   table and datab
2db07 61 73 65 20 6e 61 6d 65 64 20 69 6e 20 74 68 65  ase named in the
2db08 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
2db09 6d 2e 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 20  m..** pDatabase 
2db0a 69 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 64  is NULL if the d
2db0b 61 74 61 62 61 73 65 20 6e 61 6d 65 20 71 75 61  atabase name qua
2db0c 6c 69 66 69 65 72 20 69 73 20 6d 69 73 73 69 6e  lifier is missin
2db0d 67 20 2d 20 74 68 65 0a 2a 2a 20 75 73 75 61 6c  g - the.** usual
2db0e 20 63 61 73 65 2e 20 20 49 66 20 74 68 65 20 74   case.  If the t
2db0f 65 72 6d 20 68 61 73 20 61 20 61 6c 69 61 73 2c  erm has a alias,
2db10 20 74 68 65 6e 20 70 41 6c 69 61 73 20 70 6f 69   then pAlias poi
2db11 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c  nts to the.** al
2db12 69 61 73 20 74 6f 6b 65 6e 2e 20 20 49 66 20 74  ias token.  If t
2db13 68 65 20 74 65 72 6d 20 69 73 20 61 20 73 75 62  he term is a sub
2db14 71 75 65 72 79 2c 20 74 68 65 6e 20 70 53 75 62  query, then pSub
2db15 71 75 65 72 79 20 69 73 20 74 68 65 0a 2a 2a 20  query is the.** 
2db16 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2db17 20 74 68 61 74 20 74 68 65 20 73 75 62 71 75 65   that the subque
2db18 72 79 20 65 6e 63 6f 64 65 73 2e 20 20 54 68 65  ry encodes.  The
2db19 20 70 54 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 70   pTable and.** p
2db1a 44 61 74 61 62 61 73 65 20 70 61 72 61 6d 65 74  Database paramet
2db1b 65 72 73 20 61 72 65 20 4e 55 4c 4c 20 66 6f 72  ers are NULL for
2db1c 20 73 75 62 71 75 65 72 69 65 73 2e 20 20 54 68   subqueries.  Th
2db1d 65 20 70 4f 6e 20 61 6e 64 20 70 55 73 69 6e 67  e pOn and pUsing
2db1e 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61  .** parameters a
2db1f 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  re the content o
2db20 66 20 74 68 65 20 4f 4e 20 61 6e 64 20 55 53 49  f the ON and USI
2db21 4e 47 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a  NG clauses..**.*
2db22 2a 20 52 65 74 75 72 6e 20 61 20 6e 65 77 20 53  * Return a new S
2db23 72 63 4c 69 73 74 20 77 68 69 63 68 20 65 6e 63  rcList which enc
2db24 6f 64 65 73 20 69 73 20 74 68 65 20 46 52 4f 4d  odes is the FROM
2db25 20 77 69 74 68 20 74 68 65 20 6e 65 77 0a 2a 2a   with the new.**
2db26 20 74 65 72 6d 20 61 64 64 65 64 2e 0a 2a 2f 0a   term added..*/.
2db27 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
2db28 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53  rcList *sqlite3S
2db29 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d  rcListAppendFrom
2db2a 54 65 72 6d 28 0a 20 20 50 61 72 73 65 20 2a 70  Term(.  Parse *p
2db2b 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
2db2c 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2db2d 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  xt */.  SrcList 
2db2e 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
2db2f 2f 2a 20 54 68 65 20 6c 65 66 74 20 70 61 72 74  /* The left part
2db30 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   of the FROM cla
2db31 75 73 65 20 61 6c 72 65 61 64 79 20 73 65 65 6e  use already seen
2db32 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61   */.  Token *pTa
2db33 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ble,          /*
2db34 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   Name of the tab
2db35 6c 65 20 74 6f 20 61 64 64 20 74 6f 20 74 68 65  le to add to the
2db36 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
2db37 20 20 54 6f 6b 65 6e 20 2a 70 44 61 74 61 62 61    Token *pDataba
2db38 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  se,       /* Nam
2db39 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
2db3a 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61  e containing pTa
2db3b 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ble */.  Token *
2db3c 70 41 6c 69 61 73 2c 20 20 20 20 20 20 20 20 20  pAlias,         
2db3d 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 68 61   /* The right-ha
2db3e 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 41  nd side of the A
2db3f 53 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20  S subexpression 
2db40 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75  */.  Select *pSu
2db41 62 71 75 65 72 79 2c 20 20 20 20 20 20 2f 2a 20  bquery,      /* 
2db42 41 20 73 75 62 71 75 65 72 79 20 75 73 65 64 20  A subquery used 
2db43 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 61  in place of a ta
2db44 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78  ble name */.  Ex
2db45 70 72 20 2a 70 4f 6e 2c 20 20 20 20 20 20 20 20  pr *pOn,        
2db46 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 4e 20        /* The ON 
2db47 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e  clause of a join
2db48 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 55   */.  IdList *pU
2db49 73 69 6e 67 20 20 20 20 20 20 20 20 20 20 2f 2a  sing          /*
2db4a 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73   The USING claus
2db4b 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 29  e of a join */.)
2db4c 7b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  {.  struct SrcLi
2db4d 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
2db4e 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2db4f 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66  pParse->db;.  if
2db50 28 20 21 70 20 26 26 20 28 70 4f 6e 20 7c 7c 20  ( !p && (pOn || 
2db51 70 55 73 69 6e 67 29 20 29 7b 0a 20 20 20 20 73  pUsing) ){.    s
2db52 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2db53 50 61 72 73 65 2c 20 22 61 20 4a 4f 49 4e 20 63  Parse, "a JOIN c
2db54 6c 61 75 73 65 20 69 73 20 72 65 71 75 69 72 65  lause is require
2db55 64 20 62 65 66 6f 72 65 20 25 73 22 2c 20 0a 20  d before %s", . 
2db56 20 20 20 20 20 28 70 4f 6e 20 3f 20 22 4f 4e 22       (pOn ? "ON"
2db57 20 3a 20 22 55 53 49 4e 47 22 29 0a 20 20 20 20   : "USING").    
2db58 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 70 70 65  );.    goto appe
2db59 6e 64 5f 66 72 6f 6d 5f 65 72 72 6f 72 3b 0a 20  nd_from_error;. 
2db5a 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33   }.  p = sqlite3
2db5b 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62  SrcListAppend(db
2db5c 2c 20 70 2c 20 70 54 61 62 6c 65 2c 20 70 44 61  , p, pTable, pDa
2db5d 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70  tabase);.  if( p
2db5e 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 2d 3e  ==0 || NEVER(p->
2db5f 6e 53 72 63 3d 3d 30 29 20 29 7b 0a 20 20 20 20  nSrc==0) ){.    
2db60 67 6f 74 6f 20 61 70 70 65 6e 64 5f 66 72 6f 6d  goto append_from
2db61 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 49  _error;.  }.  pI
2db62 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e  tem = &p->a[p->n
2db63 53 72 63 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74  Src-1];.  assert
2db64 28 20 70 41 6c 69 61 73 21 3d 30 20 29 3b 0a 20  ( pAlias!=0 );. 
2db65 20 69 66 28 20 70 41 6c 69 61 73 2d 3e 6e 20 29   if( pAlias->n )
2db66 7b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 41 6c  {.    pItem->zAl
2db67 69 61 73 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ias = sqlite3Nam
2db68 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
2db69 41 6c 69 61 73 29 3b 0a 20 20 7d 0a 20 20 70 49  Alias);.  }.  pI
2db6a 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70  tem->pSelect = p
2db6b 53 75 62 71 75 65 72 79 3b 0a 20 20 70 49 74 65  Subquery;.  pIte
2db6c 6d 2d 3e 70 4f 6e 20 3d 20 70 4f 6e 3b 0a 20 20  m->pOn = pOn;.  
2db6d 70 49 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d 20  pItem->pUsing = 
2db6e 70 55 73 69 6e 67 3b 0a 20 20 72 65 74 75 72 6e  pUsing;.  return
2db6f 20 70 3b 0a 0a 20 61 70 70 65 6e 64 5f 66 72 6f   p;.. append_fro
2db70 6d 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72  m_error:.  asser
2db71 74 28 20 70 3d 3d 30 20 29 3b 0a 20 20 73 71 6c  t( p==0 );.  sql
2db72 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2db73 62 2c 20 70 4f 6e 29 3b 0a 20 20 73 71 6c 69 74  b, pOn);.  sqlit
2db74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64  e3IdListDelete(d
2db75 62 2c 20 70 55 73 69 6e 67 29 3b 0a 20 20 73 71  b, pUsing);.  sq
2db76 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
2db77 65 28 64 62 2c 20 70 53 75 62 71 75 65 72 79 29  e(db, pSubquery)
2db78 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
2db79 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 49 4e  ./*.** Add an IN
2db7a 44 45 58 45 44 20 42 59 20 6f 72 20 4e 4f 54 20  DEXED BY or NOT 
2db7b 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20 74  INDEXED clause t
2db7c 6f 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  o the most recen
2db7d 74 6c 79 20 61 64 64 65 64 20 0a 2a 2a 20 65 6c  tly added .** el
2db7e 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 73 6f 75  ement of the sou
2db7f 72 63 65 2d 6c 69 73 74 20 70 61 73 73 65 64 20  rce-list passed 
2db80 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
2db81 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  gument..*/.SQLIT
2db82 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2db83 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64  qlite3SrcListInd
2db84 65 78 65 64 42 79 28 50 61 72 73 65 20 2a 70 50  exedBy(Parse *pP
2db85 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70  arse, SrcList *p
2db86 2c 20 54 6f 6b 65 6e 20 2a 70 49 6e 64 65 78 65  , Token *pIndexe
2db87 64 42 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20  dBy){.  assert( 
2db88 70 49 6e 64 65 78 65 64 42 79 21 3d 30 20 29 3b  pIndexedBy!=0 );
2db89 0a 20 20 69 66 28 20 70 20 26 26 20 41 4c 57 41  .  if( p && ALWA
2db8a 59 53 28 70 2d 3e 6e 53 72 63 3e 30 29 20 29 7b  YS(p->nSrc>0) ){
2db8b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
2db8c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
2db8d 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d  = &p->a[p->nSrc-
2db8e 31 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  1];.    assert( 
2db8f 70 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65  pItem->notIndexe
2db90 64 3d 3d 30 20 26 26 20 70 49 74 65 6d 2d 3e 7a  d==0 && pItem->z
2db91 49 6e 64 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20  Index==0 );.    
2db92 69 66 28 20 70 49 6e 64 65 78 65 64 42 79 2d 3e  if( pIndexedBy->
2db93 6e 3d 3d 31 20 26 26 20 21 70 49 6e 64 65 78 65  n==1 && !pIndexe
2db94 64 42 79 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20  dBy->z ){.      
2db95 2f 2a 20 41 20 22 4e 4f 54 20 49 4e 44 45 58 45  /* A "NOT INDEXE
2db96 44 22 20 63 6c 61 75 73 65 20 77 61 73 20 73 75  D" clause was su
2db97 70 70 6c 69 65 64 2e 20 53 65 65 20 70 61 72 73  pplied. See pars
2db98 65 2e 79 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f  e.y .      ** co
2db99 6e 73 74 72 75 63 74 20 22 69 6e 64 65 78 65 64  nstruct "indexed
2db9a 5f 6f 70 74 22 20 66 6f 72 20 64 65 74 61 69 6c  _opt" for detail
2db9b 73 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65  s. */.      pIte
2db9c 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20  m->notIndexed = 
2db9d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
2db9e 20 20 20 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65      pItem->zInde
2db9f 78 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  x = sqlite3NameF
2dba0 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d  romToken(pParse-
2dba1 3e 64 62 2c 20 70 49 6e 64 65 78 65 64 42 79 29  >db, pIndexedBy)
2dba2 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
2dba3 2a 0a 2a 2a 20 57 68 65 6e 20 62 75 69 6c 64 69  *.** When buildi
2dba4 6e 67 20 75 70 20 61 20 46 52 4f 4d 20 63 6c 61  ng up a FROM cla
2dba5 75 73 65 20 69 6e 20 74 68 65 20 70 61 72 73 65  use in the parse
2dba6 72 2c 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72  r, the join oper
2dba7 61 74 6f 72 0a 2a 2a 20 69 73 20 69 6e 69 74 69  ator.** is initi
2dba8 61 6c 6c 79 20 61 74 74 61 63 68 65 64 20 74 6f  ally attached to
2dba9 20 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e   the left operan
2dbaa 64 2e 20 20 42 75 74 20 74 68 65 20 63 6f 64 65  d.  But the code
2dbab 20 67 65 6e 65 72 61 74 6f 72 0a 2a 2a 20 65 78   generator.** ex
2dbac 70 65 63 74 73 20 74 68 65 20 6a 6f 69 6e 20 6f  pects the join o
2dbad 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 6f 6e  perator to be on
2dbae 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61   the right opera
2dbaf 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  nd.  This routin
2dbb0 65 0a 2a 2a 20 53 68 69 66 74 73 20 61 6c 6c 20  e.** Shifts all 
2dbb1 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 73 20 66  join operators f
2dbb2 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68  rom left to righ
2dbb3 74 20 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 20  t for an entire 
2dbb4 46 52 4f 4d 0a 2a 2a 20 63 6c 61 75 73 65 2e 0a  FROM.** clause..
2dbb5 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 20 53  **.** Example: S
2dbb6 75 70 70 6f 73 65 20 74 68 65 20 6a 6f 69 6e 20  uppose the join 
2dbb7 69 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  is like this:.**
2dbb8 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 41 20  .**           A 
2dbb9 6e 61 74 75 72 61 6c 20 63 72 6f 73 73 20 6a 6f  natural cross jo
2dbba 69 6e 20 42 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f  in B.**.** The o
2dbbb 70 65 72 61 74 6f 72 20 69 73 20 22 6e 61 74 75  perator is "natu
2dbbc 72 61 6c 20 63 72 6f 73 73 20 6a 6f 69 6e 22 2e  ral cross join".
2dbbd 20 20 54 68 65 20 41 20 61 6e 64 20 42 20 6f 70    The A and B op
2dbbe 65 72 61 6e 64 73 20 61 72 65 20 73 74 6f 72 65  erands are store
2dbbf 64 0a 2a 2a 20 69 6e 20 70 2d 3e 61 5b 30 5d 20  d.** in p->a[0] 
2dbc0 61 6e 64 20 70 2d 3e 61 5b 31 5d 2c 20 72 65 73  and p->a[1], res
2dbc1 70 65 63 74 69 76 65 6c 79 2e 20 20 54 68 65 20  pectively.  The 
2dbc2 70 61 72 73 65 72 20 69 6e 69 74 69 61 6c 6c 79  parser initially
2dbc3 20 73 74 6f 72 65 73 20 74 68 65 0a 2a 2a 20 6f   stores the.** o
2dbc4 70 65 72 61 74 6f 72 20 77 69 74 68 20 41 2e 20  perator with A. 
2dbc5 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68   This routine sh
2dbc6 69 66 74 73 20 74 68 61 74 20 6f 70 65 72 61 74  ifts that operat
2dbc7 6f 72 20 6f 76 65 72 20 74 6f 20 42 2e 0a 2a 2f  or over to B..*/
2dbc8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2dbc9 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c  void sqlite3SrcL
2dbca 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65  istShiftJoinType
2dbcb 28 53 72 63 4c 69 73 74 20 2a 70 29 7b 0a 20 20  (SrcList *p){.  
2dbcc 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 6e 74  if( p ){.    int
2dbcd 20 69 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   i;.    assert( 
2dbce 70 2d 3e 61 20 7c 7c 20 70 2d 3e 6e 53 72 63 3d  p->a || p->nSrc=
2dbcf 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  =0 );.    for(i=
2dbd0 70 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 30 3b 20  p->nSrc-1; i>0; 
2dbd1 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61  i--){.      p->a
2dbd2 5b 69 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 70  [i].jointype = p
2dbd3 2d 3e 61 5b 69 2d 31 5d 2e 6a 6f 69 6e 74 79 70  ->a[i-1].jointyp
2dbd4 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  e;.    }.    p->
2dbd5 61 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20  a[0].jointype = 
2dbd6 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
2dbd7 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74  Begin a transact
2dbd8 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ion.*/.SQLITE_PR
2dbd9 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2dbda 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69  e3BeginTransacti
2dbdb 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  on(Parse *pParse
2dbdc 2c 20 69 6e 74 20 74 79 70 65 29 7b 0a 20 20 73  , int type){.  s
2dbdd 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64  qlite3 *db;.  Vd
2dbde 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 3b 0a  be *v;.  int i;.
2dbdf 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
2dbe0 65 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70  e!=0 );.  db = p
2dbe1 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73  Parse->db;.  ass
2dbe2 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 2f 2a  ert( db!=0 );./*
2dbe3 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 30 5d    if( db->aDb[0]
2dbe4 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  .pBt==0 ) return
2dbe5 3b 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74  ; */.  if( sqlit
2dbe6 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
2dbe7 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  se, SQLITE_TRANS
2dbe8 41 43 54 49 4f 4e 2c 20 22 42 45 47 49 4e 22 2c  ACTION, "BEGIN",
2dbe9 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65   0, 0) ){.    re
2dbea 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d 20  turn;.  }.  v = 
2dbeb 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2dbec 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76  Parse);.  if( !v
2dbed 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
2dbee 20 74 79 70 65 21 3d 54 4b 5f 44 45 46 45 52 52   type!=TK_DEFERR
2dbef 45 44 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  ED ){.    for(i=
2dbf0 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
2dbf1 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
2dbf2 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2dbf3 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69  P_Transaction, i
2dbf4 2c 20 28 74 79 70 65 3d 3d 54 4b 5f 45 58 43 4c  , (type==TK_EXCL
2dbf5 55 53 49 56 45 29 2b 31 29 3b 0a 20 20 20 20 20  USIVE)+1);.     
2dbf6 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73   sqlite3VdbeUses
2dbf7 42 74 72 65 65 28 76 2c 20 69 29 3b 0a 20 20 20  Btree(v, i);.   
2dbf8 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
2dbf9 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2dbfa 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 30 2c 20  _AutoCommit, 0, 
2dbfb 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  0);.}../*.** Com
2dbfc 6d 69 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f  mit a transactio
2dbfd 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  n.*/.SQLITE_PRIV
2dbfe 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2dbff 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f  CommitTransactio
2dc00 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  n(Parse *pParse)
2dc01 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20  {.  Vdbe *v;..  
2dc02 61 73 73 65 72 74 28 20 70 50 61 72 73 65 21 3d  assert( pParse!=
2dc03 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
2dc04 50 61 72 73 65 2d 3e 64 62 21 3d 30 20 29 3b 0a  Parse->db!=0 );.
2dc05 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
2dc06 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
2dc07 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f  QLITE_TRANSACTIO
2dc08 4e 2c 20 22 43 4f 4d 4d 49 54 22 2c 20 30 2c 20  N, "COMMIT", 0, 
2dc09 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  0) ){.    return
2dc0a 3b 0a 20 20 7d 0a 20 20 76 20 3d 20 73 71 6c 69  ;.  }.  v = sqli
2dc0b 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2dc0c 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20  e);.  if( v ){. 
2dc0d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2dc0e 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f 43  dOp2(v, OP_AutoC
2dc0f 6f 6d 6d 69 74 2c 20 31 2c 20 30 29 3b 0a 20 20  ommit, 1, 0);.  
2dc10 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62  }.}../*.** Rollb
2dc11 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ack a transactio
2dc12 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  n.*/.SQLITE_PRIV
2dc13 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2dc14 52 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74  RollbackTransact
2dc15 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73  ion(Parse *pPars
2dc16 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a  e){.  Vdbe *v;..
2dc17 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
2dc18 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
2dc19 20 70 50 61 72 73 65 2d 3e 64 62 21 3d 30 20 29   pParse->db!=0 )
2dc1a 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  ;.  if( sqlite3A
2dc1b 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2dc1c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54   SQLITE_TRANSACT
2dc1d 49 4f 4e 2c 20 22 52 4f 4c 4c 42 41 43 4b 22 2c  ION, "ROLLBACK",
2dc1e 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65   0, 0) ){.    re
2dc1f 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d 20  turn;.  }.  v = 
2dc20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2dc21 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20  Parse);.  if( v 
2dc22 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2dc23 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
2dc24 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 31 29  utoCommit, 1, 1)
2dc25 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
2dc26 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2dc27 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61  called by the pa
2dc28 72 73 65 72 20 77 68 65 6e 20 69 74 20 70 61 72  rser when it par
2dc29 73 65 73 20 61 20 63 6f 6d 6d 61 6e 64 20 74 6f  ses a command to
2dc2a 20 63 72 65 61 74 65 2c 0a 2a 2a 20 72 65 6c 65   create,.** rele
2dc2b 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20  ase or rollback 
2dc2c 61 6e 20 53 51 4c 20 73 61 76 65 70 6f 69 6e 74  an SQL savepoint
2dc2d 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  . .*/.SQLITE_PRI
2dc2e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2dc2f 33 53 61 76 65 70 6f 69 6e 74 28 50 61 72 73 65  3Savepoint(Parse
2dc30 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 70   *pParse, int op
2dc31 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b  , Token *pName){
2dc32 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d  .  char *zName =
2dc33 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
2dc34 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62  Token(pParse->db
2dc35 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  , pName);.  if( 
2dc36 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 62  zName ){.    Vdb
2dc37 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
2dc38 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 23  tVdbe(pParse);.#
2dc39 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2dc3a 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
2dc3b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
2dc3c 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61  t char * const a
2dc3d 7a 5b 5d 20 3d 20 7b 20 22 42 45 47 49 4e 22 2c  z[] = { "BEGIN",
2dc3e 20 22 52 45 4c 45 41 53 45 22 2c 20 22 52 4f 4c   "RELEASE", "ROL
2dc3f 4c 42 41 43 4b 22 20 7d 3b 0a 20 20 20 20 61 73  LBACK" };.    as
2dc40 73 65 72 74 28 20 21 53 41 56 45 50 4f 49 4e 54  sert( !SAVEPOINT
2dc41 5f 42 45 47 49 4e 20 26 26 20 53 41 56 45 50 4f  _BEGIN && SAVEPO
2dc42 49 4e 54 5f 52 45 4c 45 41 53 45 3d 3d 31 20 26  INT_RELEASE==1 &
2dc43 26 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  & SAVEPOINT_ROLL
2dc44 42 41 43 4b 3d 3d 32 20 29 3b 0a 23 65 6e 64 69  BACK==2 );.#endi
2dc45 66 0a 20 20 20 20 69 66 28 20 21 76 20 7c 7c 20  f.    if( !v || 
2dc46 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
2dc47 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
2dc48 53 41 56 45 50 4f 49 4e 54 2c 20 61 7a 5b 6f 70  SAVEPOINT, az[op
2dc49 5d 2c 20 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a  ], zName, 0) ){.
2dc4a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2dc4b 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ree(pParse->db, 
2dc4c 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65  zName);.      re
2dc4d 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
2dc4e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2dc4f 34 28 76 2c 20 4f 50 5f 53 61 76 65 70 6f 69 6e  4(v, OP_Savepoin
2dc50 74 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 7a 4e 61  t, op, 0, 0, zNa
2dc51 6d 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b  me, P4_DYNAMIC);
2dc52 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  .  }.}../*.** Ma
2dc53 6b 65 20 73 75 72 65 20 74 68 65 20 54 45 4d 50  ke sure the TEMP
2dc54 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   database is ope
2dc55 6e 20 61 6e 64 20 61 76 61 69 6c 61 62 6c 65 20  n and available 
2dc56 66 6f 72 20 75 73 65 2e 20 20 52 65 74 75 72 6e  for use.  Return
2dc57 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
2dc58 66 20 65 72 72 6f 72 73 2e 20 20 4c 65 61 76 65  f errors.  Leave
2dc59 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61   any error messa
2dc5a 67 65 73 20 69 6e 20 74 68 65 20 70 50 61 72 73  ges in the pPars
2dc5b 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  e structure..*/.
2dc5c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2dc5d 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65  nt sqlite3OpenTe
2dc5e 6d 70 44 61 74 61 62 61 73 65 28 50 61 72 73 65  mpDatabase(Parse
2dc5f 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c   *pParse){.  sql
2dc60 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2dc61 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d  e->db;.  if( db-
2dc62 3e 61 44 62 5b 31 5d 2e 70 42 74 3d 3d 30 20 26  >aDb[1].pBt==0 &
2dc63 26 20 21 70 50 61 72 73 65 2d 3e 65 78 70 6c 61  & !pParse->expla
2dc64 69 6e 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  in ){.    int rc
2dc65 3b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74  ;.    Btree *pBt
2dc66 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  ;.    static con
2dc67 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 0a  st int flags = .
2dc68 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
2dc69 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
2dc6a 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49  |.          SQLI
2dc6b 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c  TE_OPEN_CREATE |
2dc6c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  .          SQLIT
2dc6d 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
2dc6e 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c   |.          SQL
2dc6f 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
2dc70 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20 20  NCLOSE |.       
2dc71 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54     SQLITE_OPEN_T
2dc72 45 4d 50 5f 44 42 3b 0a 0a 20 20 20 20 72 63 20  EMP_DB;..    rc 
2dc73 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70  = sqlite3BtreeOp
2dc74 65 6e 28 64 62 2d 3e 70 56 66 73 2c 20 30 2c 20  en(db->pVfs, 0, 
2dc75 64 62 2c 20 26 70 42 74 2c 20 30 2c 20 66 6c 61  db, &pBt, 0, fla
2dc76 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  gs);.    if( rc!
2dc77 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2dc78 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2dc79 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e 61  Msg(pParse, "una
2dc7a 62 6c 65 20 74 6f 20 6f 70 65 6e 20 61 20 74 65  ble to open a te
2dc7b 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
2dc7c 20 22 0a 20 20 20 20 20 20 20 20 22 66 69 6c 65   ".        "file
2dc7d 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65 6d   for storing tem
2dc7e 70 6f 72 61 72 79 20 74 61 62 6c 65 73 22 29 3b  porary tables");
2dc7f 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72  .      pParse->r
2dc80 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 72 65  c = rc;.      re
2dc81 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
2dc82 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74    db->aDb[1].pBt
2dc83 20 3d 20 70 42 74 3b 0a 20 20 20 20 61 73 73 65   = pBt;.    asse
2dc84 72 74 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  rt( db->aDb[1].p
2dc85 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 69 66  Schema );.    if
2dc86 28 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d  ( SQLITE_NOMEM==
2dc87 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50  sqlite3BtreeSetP
2dc88 61 67 65 53 69 7a 65 28 70 42 74 2c 20 64 62 2d  ageSize(pBt, db-
2dc89 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 2c 20 2d  >nextPagesize, -
2dc8a 31 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 64  1, 0) ){.      d
2dc8b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2dc8c 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 1;.      retur
2dc8d 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
2dc8e 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
2dc8f 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 56 44 42  .** Generate VDB
2dc90 45 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  E code that will
2dc91 20 76 65 72 69 66 79 20 74 68 65 20 73 63 68 65   verify the sche
2dc92 6d 61 20 63 6f 6f 6b 69 65 20 61 6e 64 20 73 74  ma cookie and st
2dc93 61 72 74 0a 2a 2a 20 61 20 72 65 61 64 2d 74 72  art.** a read-tr
2dc94 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 61 6c  ansaction for al
2dc95 6c 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65  l named database
2dc96 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74   files..**.** It
2dc97 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 68   is important th
2dc98 61 74 20 61 6c 6c 20 73 63 68 65 6d 61 20 63 6f  at all schema co
2dc99 6f 6b 69 65 73 20 62 65 20 76 65 72 69 66 69 65  okies be verifie
2dc9a 64 20 61 6e 64 20 61 6c 6c 0a 2a 2a 20 72 65 61  d and all.** rea
2dc9b 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 62  d transactions b
2dc9c 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65  e started before
2dc9d 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 68   anything else h
2dc9e 61 70 70 65 6e 73 20 69 6e 0a 2a 2a 20 74 68 65  appens in.** the
2dc9f 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 20 20   VDBE program.  
2dca0 42 75 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65  But this routine
2dca1 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61   can be called a
2dca2 66 74 65 72 20 6d 75 63 68 20 6f 74 68 65 72 0a  fter much other.
2dca3 2a 2a 20 63 6f 64 65 20 68 61 73 20 62 65 65 6e  ** code has been
2dca4 20 67 65 6e 65 72 61 74 65 64 2e 20 20 53 6f 20   generated.  So 
2dca5 68 65 72 65 20 69 73 20 77 68 61 74 20 77 65 20  here is what we 
2dca6 64 6f 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  do:.**.** The fi
2dca7 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f  rst time this ro
2dca8 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
2dca9 20 77 65 20 63 6f 64 65 20 61 6e 20 4f 50 5f 47   we code an OP_G
2dcaa 6f 74 6f 20 74 68 61 74 0a 2a 2a 20 77 69 6c 6c  oto that.** will
2dcab 20 6a 75 6d 70 20 74 6f 20 61 20 73 75 62 72 6f   jump to a subro
2dcac 75 74 69 6e 65 20 61 74 20 74 68 65 20 65 6e 64  utine at the end
2dcad 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e   of the program.
2dcae 20 20 54 68 65 6e 20 77 65 0a 2a 2a 20 72 65 63    Then we.** rec
2dcaf 6f 72 64 20 65 76 65 72 79 20 64 61 74 61 62 61  ord every databa
2dcb0 73 65 20 74 68 61 74 20 6e 65 65 64 73 20 69 74  se that needs it
2dcb1 73 20 73 63 68 65 6d 61 20 76 65 72 69 66 69 65  s schema verifie
2dcb2 64 20 69 6e 20 74 68 65 0a 2a 2a 20 70 50 61 72  d in the.** pPar
2dcb3 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 66  se->cookieMask f
2dcb4 69 65 6c 64 2e 20 20 4c 61 74 65 72 2c 20 61 66  ield.  Later, af
2dcb5 74 65 72 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f  ter all other co
2dcb6 64 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 67  de has been.** g
2dcb7 65 6e 65 72 61 74 65 64 2c 20 74 68 65 20 73 75  enerated, the su
2dcb8 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64 6f  broutine that do
2dcb9 65 73 20 74 68 65 20 63 6f 6f 6b 69 65 20 76 65  es the cookie ve
2dcba 72 69 66 69 63 61 74 69 6f 6e 73 20 61 6e 64 0a  rifications and.
2dcbb 2a 2a 20 73 74 61 72 74 73 20 74 68 65 20 74 72  ** starts the tr
2dcbc 61 6e 73 61 63 74 69 6f 6e 73 20 77 69 6c 6c 20  ansactions will 
2dcbd 62 65 20 63 6f 64 65 64 20 61 6e 64 20 74 68 65  be coded and the
2dcbe 20 4f 50 5f 47 6f 74 6f 20 50 32 20 76 61 6c 75   OP_Goto P2 valu
2dcbf 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 6d 61 64  e.** will be mad
2dcc0 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  e to point to th
2dcc1 61 74 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20  at subroutine.  
2dcc2 54 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f  The generation o
2dcc3 66 20 74 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 20  f the.** cookie 
2dcc4 76 65 72 69 66 69 63 61 74 69 6f 6e 20 73 75 62  verification sub
2dcc5 72 6f 75 74 69 6e 65 20 63 6f 64 65 20 68 61 70  routine code hap
2dcc6 70 65 6e 73 20 69 6e 20 73 71 6c 69 74 65 33 46  pens in sqlite3F
2dcc7 69 6e 69 73 68 43 6f 64 69 6e 67 28 29 2e 0a 2a  inishCoding()..*
2dcc8 2a 0a 2a 2a 20 49 66 20 69 44 62 3c 30 20 74 68  *.** If iDb<0 th
2dcc9 65 6e 20 63 6f 64 65 20 74 68 65 20 4f 50 5f 47  en code the OP_G
2dcca 6f 74 6f 20 6f 6e 6c 79 20 2d 20 64 6f 6e 27 74  oto only - don't
2dccb 20 73 65 74 20 66 6c 61 67 20 74 6f 20 76 65 72   set flag to ver
2dccc 69 66 79 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d  ify the.** schem
2dccd 61 20 6f 6e 20 61 6e 79 20 64 61 74 61 62 61 73  a on any databas
2dcce 65 73 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65  es.  This can be
2dccf 20 75 73 65 64 20 74 6f 20 70 6f 73 69 74 69 6f   used to positio
2dcd0 6e 20 74 68 65 20 4f 50 5f 47 6f 74 6f 0a 2a 2a  n the OP_Goto.**
2dcd1 20 65 61 72 6c 79 20 69 6e 20 74 68 65 20 63 6f   early in the co
2dcd2 64 65 2c 20 62 65 66 6f 72 65 20 77 65 20 6b 6e  de, before we kn
2dcd3 6f 77 20 69 66 20 61 6e 79 20 64 61 74 61 62 61  ow if any databa
2dcd4 73 65 20 74 61 62 6c 65 73 20 77 69 6c 6c 20 62  se tables will b
2dcd5 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  e used..*/.SQLIT
2dcd6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2dcd7 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
2dcd8 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50  Schema(Parse *pP
2dcd9 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a  arse, int iDb){.
2dcda 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76    Parse *pToplev
2dcdb 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73  el = sqlite3Pars
2dcdc 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65  eToplevel(pParse
2dcdd 29 3b 0a 0a 20 20 69 66 28 20 70 54 6f 70 6c 65  );..  if( pTople
2dcde 76 65 6c 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3d  vel->cookieGoto=
2dcdf 3d 30 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a  =0 ){.    Vdbe *
2dce0 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
2dce1 62 65 28 70 54 6f 70 6c 65 76 65 6c 29 3b 0a 20  be(pToplevel);. 
2dce2 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65     if( v==0 ) re
2dce3 74 75 72 6e 3b 20 20 2f 2a 20 54 68 69 73 20 6f  turn;  /* This o
2dce4 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 66 20 74  nly happens if t
2dce5 68 65 72 65 20 77 61 73 20 61 20 70 72 69 6f 72  here was a prior
2dce6 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 70 54   error */.    pT
2dce7 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65 47  oplevel->cookieG
2dce8 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33 56 64 62  oto = sqlite3Vdb
2dce9 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2dcea 74 6f 2c 20 30 2c 20 30 29 2b 31 3b 0a 20 20 7d  to, 0, 0)+1;.  }
2dceb 0a 20 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b  .  if( iDb>=0 ){
2dcec 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
2dced 20 3d 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 64 62   = pToplevel->db
2dcee 3b 0a 20 20 20 20 79 44 62 4d 61 73 6b 20 6d 61  ;.    yDbMask ma
2dcef 73 6b 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28  sk;..    assert(
2dcf0 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
2dcf1 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
2dcf2 61 44 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20  aDb[iDb].pBt!=0 
2dcf3 7c 7c 20 69 44 62 3d 3d 31 20 29 3b 0a 20 20 20  || iDb==1 );.   
2dcf4 20 61 73 73 65 72 74 28 20 69 44 62 3c 53 51 4c   assert( iDb<SQL
2dcf5 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44  ITE_MAX_ATTACHED
2dcf6 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  +2 );.    assert
2dcf7 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 4d  ( sqlite3SchemaM
2dcf8 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69 44 62  utexHeld(db, iDb
2dcf9 2c 20 30 29 20 29 3b 0a 20 20 20 20 6d 61 73 6b  , 0) );.    mask
2dcfa 20 3d 20 28 28 79 44 62 4d 61 73 6b 29 31 29 3c   = ((yDbMask)1)<
2dcfb 3c 69 44 62 3b 0a 20 20 20 20 69 66 28 20 28 70  <iDb;.    if( (p
2dcfc 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65  Toplevel->cookie
2dcfd 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20  Mask & mask)==0 
2dcfe 29 7b 0a 20 20 20 20 20 20 70 54 6f 70 6c 65 76  ){.      pToplev
2dcff 65 6c 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 7c  el->cookieMask |
2dd00 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 70 54  = mask;.      pT
2dd01 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65 56  oplevel->cookieV
2dd02 61 6c 75 65 5b 69 44 62 5d 20 3d 20 64 62 2d 3e  alue[iDb] = db->
2dd03 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
2dd04 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b  ->schema_cookie;
2dd05 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54  .      if( !OMIT
2dd06 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d  _TEMPDB && iDb==
2dd07 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  1 ){.        sql
2dd08 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61  ite3OpenTempData
2dd09 62 61 73 65 28 70 54 6f 70 6c 65 76 65 6c 29 3b  base(pToplevel);
2dd0a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2dd0b 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 61   }.}../*.** If a
2dd0c 72 67 75 6d 65 6e 74 20 7a 44 62 20 69 73 20 4e  rgument zDb is N
2dd0d 55 4c 4c 2c 20 74 68 65 6e 20 63 61 6c 6c 20 73  ULL, then call s
2dd0e 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
2dd0f 53 63 68 65 6d 61 28 29 20 66 6f 72 20 65 61 63  Schema() for eac
2dd10 68 20 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64  h .** attached d
2dd11 61 74 61 62 61 73 65 2e 20 4f 74 68 65 72 77 69  atabase. Otherwi
2dd12 73 65 2c 20 69 6e 76 6f 6b 65 20 69 74 20 66 6f  se, invoke it fo
2dd13 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  r the database n
2dd14 61 6d 65 64 20 7a 44 62 20 6f 6e 6c 79 2e 0a 2a  amed zDb only..*
2dd15 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2dd16 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64   void sqlite3Cod
2dd17 65 56 65 72 69 66 79 4e 61 6d 65 64 53 63 68 65  eVerifyNamedSche
2dd18 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ma(Parse *pParse
2dd19 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  , const char *zD
2dd1a 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  b){.  sqlite3 *d
2dd1b 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2dd1c 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
2dd1d 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
2dd1e 2b 2b 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 62  ++){.    Db *pDb
2dd1f 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a   = &db->aDb[i];.
2dd20 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74      if( pDb->pBt
2dd21 20 26 26 20 28 21 7a 44 62 20 7c 7c 20 30 3d 3d   && (!zDb || 0==
2dd22 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2dd23 44 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 29  Db, pDb->zName))
2dd24 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2dd25 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d  3CodeVerifySchem
2dd26 61 28 70 50 61 72 73 65 2c 20 69 29 3b 0a 20 20  a(pParse, i);.  
2dd27 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
2dd28 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 63   Generate VDBE c
2dd29 6f 64 65 20 74 68 61 74 20 70 72 65 70 61 72 65  ode that prepare
2dd2a 73 20 66 6f 72 20 64 6f 69 6e 67 20 61 6e 20 6f  s for doing an o
2dd2b 70 65 72 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a  peration that.**
2dd2c 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 68   might change th
2dd2d 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  e database..**.*
2dd2e 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
2dd2f 74 61 72 74 73 20 61 20 6e 65 77 20 74 72 61 6e  tarts a new tran
2dd30 73 61 63 74 69 6f 6e 20 69 66 20 77 65 20 61 72  saction if we ar
2dd31 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 77 69  e not already wi
2dd32 74 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 61  thin.** a transa
2dd33 63 74 69 6f 6e 2e 20 20 49 66 20 77 65 20 61 72  ction.  If we ar
2dd34 65 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e  e already within
2dd35 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20   a transaction, 
2dd36 74 68 65 6e 20 61 20 63 68 65 63 6b 70 6f 69 6e  then a checkpoin
2dd37 74 0a 2a 2a 20 69 73 20 73 65 74 20 69 66 20 74  t.** is set if t
2dd38 68 65 20 73 65 74 53 74 61 74 65 6d 65 6e 74 20  he setStatement 
2dd39 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75  parameter is tru
2dd3a 65 2e 20 20 41 20 63 68 65 63 6b 70 6f 69 6e 74  e.  A checkpoint
2dd3b 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 73 65   should.** be se
2dd3c 74 20 66 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73  t for operations
2dd3d 20 74 68 61 74 20 6d 69 67 68 74 20 66 61 69 6c   that might fail
2dd3e 20 28 64 75 65 20 74 6f 20 61 20 63 6f 6e 73 74   (due to a const
2dd3f 72 61 69 6e 74 29 20 70 61 72 74 20 6f 66 0a 2a  raint) part of.*
2dd40 2a 20 74 68 65 20 77 61 79 20 74 68 72 6f 75 67  * the way throug
2dd41 68 20 61 6e 64 20 77 68 69 63 68 20 77 69 6c 6c  h and which will
2dd42 20 6e 65 65 64 20 74 6f 20 75 6e 64 6f 20 73 6f   need to undo so
2dd43 6d 65 20 77 72 69 74 65 73 20 77 69 74 68 6f 75  me writes withou
2dd44 74 20 68 61 76 69 6e 67 20 74 6f 0a 2a 2a 20 72  t having to.** r
2dd45 6f 6c 6c 62 61 63 6b 20 74 68 65 20 77 68 6f 6c  ollback the whol
2dd46 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  e transaction.  
2dd47 46 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 77  For operations w
2dd48 68 65 72 65 20 61 6c 6c 20 63 6f 6e 73 74 72 61  here all constra
2dd49 69 6e 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20 63  ints.** can be c
2dd4a 68 65 63 6b 65 64 20 62 65 66 6f 72 65 20 61 6e  hecked before an
2dd4b 79 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61  y changes are ma
2dd4c 64 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  de to the databa
2dd4d 73 65 2c 20 69 74 20 69 73 20 6e 65 76 65 72 0a  se, it is never.
2dd4e 2a 2a 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20  ** necessary to 
2dd4f 75 6e 64 6f 20 61 20 77 72 69 74 65 20 61 6e 64  undo a write and
2dd50 20 74 68 65 20 63 68 65 63 6b 70 6f 69 6e 74 20   the checkpoint 
2dd51 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 65  should not be se
2dd52 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
2dd53 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2dd54 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
2dd55 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72  tion(Parse *pPar
2dd56 73 65 2c 20 69 6e 74 20 73 65 74 53 74 61 74 65  se, int setState
2dd57 6d 65 6e 74 2c 20 69 6e 74 20 69 44 62 29 7b 0a  ment, int iDb){.
2dd58 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76    Parse *pToplev
2dd59 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73  el = sqlite3Pars
2dd5a 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65  eToplevel(pParse
2dd5b 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65  );.  sqlite3Code
2dd5c 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61  VerifySchema(pPa
2dd5d 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 70 54 6f  rse, iDb);.  pTo
2dd5e 70 6c 65 76 65 6c 2d 3e 77 72 69 74 65 4d 61 73  plevel->writeMas
2dd5f 6b 20 7c 3d 20 28 28 79 44 62 4d 61 73 6b 29 31  k |= ((yDbMask)1
2dd60 29 3c 3c 69 44 62 3b 0a 20 20 70 54 6f 70 6c 65  )<<iDb;.  pTople
2dd61 76 65 6c 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74  vel->isMultiWrit
2dd62 65 20 7c 3d 20 73 65 74 53 74 61 74 65 6d 65 6e  e |= setStatemen
2dd63 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 69  t;.}../*.** Indi
2dd64 63 61 74 65 20 74 68 61 74 20 74 68 65 20 73 74  cate that the st
2dd65 61 74 65 6d 65 6e 74 20 63 75 72 72 65 6e 74 6c  atement currentl
2dd66 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  y under construc
2dd67 74 69 6f 6e 20 6d 69 67 68 74 20 77 72 69 74 65  tion might write
2dd68 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  .** more than on
2dd69 65 20 65 6e 74 72 79 20 28 65 78 61 6d 70 6c 65  e entry (example
2dd6a 3a 20 64 65 6c 65 74 69 6e 67 20 6f 6e 65 20 72  : deleting one r
2dd6b 6f 77 20 74 68 65 6e 20 69 6e 73 65 72 74 69 6e  ow then insertin
2dd6c 67 20 61 6e 6f 74 68 65 72 2c 0a 2a 2a 20 69 6e  g another,.** in
2dd6d 73 65 72 74 69 6e 67 20 6d 75 6c 74 69 70 6c 65  serting multiple
2dd6e 20 72 6f 77 73 20 69 6e 20 61 20 74 61 62 6c 65   rows in a table
2dd6f 2c 20 6f 72 20 69 6e 73 65 72 74 69 6e 67 20 61  , or inserting a
2dd70 20 72 6f 77 20 61 6e 64 20 69 6e 64 65 78 20 65   row and index e
2dd71 6e 74 72 69 65 73 2e 29 0a 2a 2a 20 49 66 20 61  ntries.).** If a
2dd72 6e 20 61 62 6f 72 74 20 6f 63 63 75 72 73 20 61  n abort occurs a
2dd73 66 74 65 72 20 73 6f 6d 65 20 6f 66 20 74 68 65  fter some of the
2dd74 73 65 20 77 72 69 74 65 73 20 68 61 76 65 20 63  se writes have c
2dd75 6f 6d 70 6c 65 74 65 64 2c 20 74 68 65 6e 20 69  ompleted, then i
2dd76 74 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 6e 65 63  t will.** be nec
2dd77 65 73 73 61 72 79 20 74 6f 20 75 6e 64 6f 20 74  essary to undo t
2dd78 68 65 20 63 6f 6d 70 6c 65 74 65 64 20 77 72 69  he completed wri
2dd79 74 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  tes..*/.SQLITE_P
2dd7a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2dd7b 74 65 33 4d 75 6c 74 69 57 72 69 74 65 28 50 61  te3MultiWrite(Pa
2dd7c 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
2dd7d 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c  Parse *pToplevel
2dd7e 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54   = sqlite3ParseT
2dd7f 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b  oplevel(pParse);
2dd80 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 69 73  .  pToplevel->is
2dd81 4d 75 6c 74 69 57 72 69 74 65 20 3d 20 31 3b 0a  MultiWrite = 1;.
2dd82 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 63 6f  }../* .** The co
2dd83 64 65 20 67 65 6e 65 72 61 74 6f 72 20 63 61 6c  de generator cal
2dd84 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
2dd85 69 66 20 69 73 20 64 69 73 63 6f 76 65 72 73 20  if is discovers 
2dd86 74 68 61 74 20 69 74 20 69 73 0a 2a 2a 20 70 6f  that it is.** po
2dd87 73 73 69 62 6c 65 20 74 6f 20 61 62 6f 72 74 20  ssible to abort 
2dd88 61 20 73 74 61 74 65 6d 65 6e 74 20 70 72 69 6f  a statement prio
2dd89 72 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e  r to completion.
2dd8a 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 0a 2a    In order to .*
2dd8b 2a 20 70 65 72 66 6f 72 6d 20 74 68 69 73 20 61  * perform this a
2dd8c 62 6f 72 74 20 77 69 74 68 6f 75 74 20 63 6f 72  bort without cor
2dd8d 72 75 70 74 69 6e 67 20 74 68 65 20 64 61 74 61  rupting the data
2dd8e 62 61 73 65 2c 20 77 65 20 6e 65 65 64 20 74 6f  base, we need to
2dd8f 20 6d 61 6b 65 0a 2a 2a 20 73 75 72 65 20 74 68   make.** sure th
2dd90 61 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  at the statement
2dd91 20 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79   is protected by
2dd92 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61   a statement tra
2dd93 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nsaction..**.** 
2dd94 54 65 63 68 6e 69 63 61 6c 6c 79 2c 20 77 65 20  Technically, we 
2dd95 6f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 73 65 74  only need to set
2dd96 20 74 68 65 20 6d 61 79 41 62 6f 72 74 20 66 6c   the mayAbort fl
2dd97 61 67 20 69 66 20 74 68 65 0a 2a 2a 20 69 73 4d  ag if the.** isM
2dd98 75 6c 74 69 57 72 69 74 65 20 66 6c 61 67 20 77  ultiWrite flag w
2dd99 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65  as previously se
2dd9a 74 2e 20 20 54 68 65 72 65 20 69 73 20 61 20 74  t.  There is a t
2dd9b 69 6d 65 20 64 65 70 65 6e 64 65 6e 63 79 0a 2a  ime dependency.*
2dd9c 2a 20 73 75 63 68 20 74 68 61 74 20 74 68 65 20  * such that the 
2dd9d 61 62 6f 72 74 20 6d 75 73 74 20 6f 63 63 75 72  abort must occur
2dd9e 20 61 66 74 65 72 20 74 68 65 20 6d 75 6c 74 69   after the multi
2dd9f 77 72 69 74 65 2e 20 20 54 68 69 73 20 6d 61 6b  write.  This mak
2dda0 65 73 0a 2a 2a 20 73 6f 6d 65 20 73 74 61 74 65  es.** some state
2dda1 6d 65 6e 74 73 20 69 6e 76 6f 6c 76 69 6e 67 20  ments involving 
2dda2 74 68 65 20 52 45 50 4c 41 43 45 20 63 6f 6e 66  the REPLACE conf
2dda3 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20  lict resolution 
2dda4 61 6c 67 6f 72 69 74 68 6d 0a 2a 2a 20 67 6f 20  algorithm.** go 
2dda5 61 20 6c 69 74 74 6c 65 20 66 61 73 74 65 72 2e  a little faster.
2dda6 20 20 42 75 74 20 74 61 6b 69 6e 67 20 61 64 76    But taking adv
2dda7 61 6e 74 61 67 65 20 6f 66 20 74 68 69 73 20 74  antage of this t
2dda8 69 6d 65 20 64 65 70 65 6e 64 65 6e 63 79 0a 2a  ime dependency.*
2dda9 2a 20 6d 61 6b 65 73 20 69 74 20 6d 6f 72 65 20  * makes it more 
2ddaa 64 69 66 66 69 63 75 6c 74 20 74 6f 20 70 72 6f  difficult to pro
2ddab 76 65 20 74 68 61 74 20 74 68 65 20 63 6f 64 65  ve that the code
2ddac 20 69 73 20 63 6f 72 72 65 63 74 20 28 69 6e 20   is correct (in 
2ddad 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 2c 20  .** particular, 
2ddae 69 74 20 70 72 65 76 65 6e 74 73 20 75 73 20 66  it prevents us f
2ddaf 72 6f 6d 20 77 72 69 74 69 6e 67 20 61 6e 20 65  rom writing an e
2ddb0 66 66 65 63 74 69 76 65 0a 2a 2a 20 69 6d 70 6c  ffective.** impl
2ddb1 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71  ementation of sq
2ddb2 6c 69 74 65 33 41 73 73 65 72 74 4d 61 79 41 62  lite3AssertMayAb
2ddb3 6f 72 74 28 29 29 20 61 6e 64 20 73 6f 20 77 65  ort()) and so we
2ddb4 20 68 61 76 65 20 63 68 6f 73 65 6e 0a 2a 2a 20   have chosen.** 
2ddb5 74 6f 20 74 61 6b 65 20 74 68 65 20 73 61 66 65  to take the safe
2ddb6 20 72 6f 75 74 65 20 61 6e 64 20 73 6b 69 70 20   route and skip 
2ddb7 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  the optimization
2ddb8 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2ddb9 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2ddba 4d 61 79 41 62 6f 72 74 28 50 61 72 73 65 20 2a  MayAbort(Parse *
2ddbb 70 50 61 72 73 65 29 7b 0a 20 20 50 61 72 73 65  pParse){.  Parse
2ddbc 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71   *pToplevel = sq
2ddbd 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76  lite3ParseToplev
2ddbe 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 70 54  el(pParse);.  pT
2ddbf 6f 70 6c 65 76 65 6c 2d 3e 6d 61 79 41 62 6f 72  oplevel->mayAbor
2ddc0 74 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t = 1;.}../*.** 
2ddc1 43 6f 64 65 20 61 6e 20 4f 50 5f 48 61 6c 74 20  Code an OP_Halt 
2ddc2 74 68 61 74 20 63 61 75 73 65 73 20 74 68 65 20  that causes the 
2ddc3 76 64 62 65 20 74 6f 20 72 65 74 75 72 6e 20 61  vdbe to return a
2ddc4 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  n SQLITE_CONSTRA
2ddc5 49 4e 54 0a 2a 2a 20 65 72 72 6f 72 2e 20 54 68  INT.** error. Th
2ddc6 65 20 6f 6e 45 72 72 6f 72 20 70 61 72 61 6d 65  e onError parame
2ddc7 74 65 72 20 64 65 74 65 72 6d 69 6e 65 73 20 77  ter determines w
2ddc8 68 69 63 68 20 28 69 66 20 61 6e 79 29 20 6f 66  hich (if any) of
2ddc9 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a   the statement.*
2ddca 2a 20 61 6e 64 2f 6f 72 20 63 75 72 72 65 6e 74  * and/or current
2ddcb 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
2ddcc 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a  rolled back..*/.
2ddcd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2ddce 6f 69 64 20 73 71 6c 69 74 65 33 48 61 6c 74 43  oid sqlite3HaltC
2ddcf 6f 6e 73 74 72 61 69 6e 74 28 50 61 72 73 65 20  onstraint(Parse 
2ddd0 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 6e 45  *pParse, int onE
2ddd1 72 72 6f 72 2c 20 63 68 61 72 20 2a 70 34 2c 20  rror, char *p4, 
2ddd2 69 6e 74 20 70 34 74 79 70 65 29 7b 0a 20 20 56  int p4type){.  V
2ddd3 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
2ddd4 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
2ddd5 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d  .  if( onError==
2ddd6 4f 45 5f 41 62 6f 72 74 20 29 7b 0a 20 20 20 20  OE_Abort ){.    
2ddd7 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28  sqlite3MayAbort(
2ddd8 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 73  pParse);.  }.  s
2ddd9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2ddda 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c  (v, OP_Halt, SQL
2dddb 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20  ITE_CONSTRAINT, 
2dddc 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 70 34 2c 20  onError, 0, p4, 
2dddd 70 34 74 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  p4type);.}../*.*
2ddde 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
2dddf 66 20 70 49 6e 64 65 78 20 75 73 65 73 20 74 68  f pIndex uses th
2dde0 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
2dde1 65 6e 63 65 20 70 43 6f 6c 6c 2e 20 20 52 65 74  ence pColl.  Ret
2dde2 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 69  urn.** true if i
2dde3 74 20 64 6f 65 73 20 61 6e 64 20 66 61 6c 73 65  t does and false
2dde4 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e   if it does not.
2dde5 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
2dde6 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a  TE_OMIT_REINDEX.
2dde7 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 6c 61  static int colla
2dde8 74 69 6f 6e 4d 61 74 63 68 28 63 6f 6e 73 74 20  tionMatch(const 
2dde9 63 68 61 72 20 2a 7a 43 6f 6c 6c 2c 20 49 6e 64  char *zColl, Ind
2ddea 65 78 20 2a 70 49 6e 64 65 78 29 7b 0a 20 20 69  ex *pIndex){.  i
2ddeb 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20  nt i;.  assert( 
2ddec 7a 43 6f 6c 6c 21 3d 30 20 29 3b 0a 20 20 66 6f  zColl!=0 );.  fo
2dded 72 28 69 3d 30 3b 20 69 3c 70 49 6e 64 65 78 2d  r(i=0; i<pIndex-
2ddee 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a  >nColumn; i++){.
2ddef 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2ddf0 7a 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f  z = pIndex->azCo
2ddf1 6c 6c 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65 72  ll[i];.    asser
2ddf2 74 28 20 7a 21 3d 30 20 29 3b 0a 20 20 20 20 69  t( z!=0 );.    i
2ddf3 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  f( 0==sqlite3Str
2ddf4 49 43 6d 70 28 7a 2c 20 7a 43 6f 6c 6c 29 20 29  ICmp(z, zColl) )
2ddf5 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
2ddf6 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
2ddf7 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66  turn 0;.}.#endif
2ddf8 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70 75 74  ../*.** Recomput
2ddf9 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66  e all indices of
2ddfa 20 70 54 61 62 20 74 68 61 74 20 75 73 65 20 74   pTab that use t
2ddfb 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
2ddfc 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 0a 2a 2a 20  uence pColl..** 
2ddfd 49 66 20 70 43 6f 6c 6c 3d 3d 30 20 74 68 65 6e  If pColl==0 then
2ddfe 20 72 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69   recompute all i
2ddff 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62 2e 0a  ndices of pTab..
2de00 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
2de01 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73  E_OMIT_REINDEX.s
2de02 74 61 74 69 63 20 76 6f 69 64 20 72 65 69 6e 64  tatic void reind
2de03 65 78 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70  exTable(Parse *p
2de04 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54  Parse, Table *pT
2de05 61 62 2c 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ab, char const *
2de06 7a 43 6f 6c 6c 29 7b 0a 20 20 49 6e 64 65 78 20  zColl){.  Index 
2de07 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20  *pIndex;        
2de08 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65        /* An inde
2de09 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
2de0a 68 20 70 54 61 62 20 2a 2f 0a 0a 20 20 66 6f 72  h pTab */..  for
2de0b 28 70 49 6e 64 65 78 3d 70 54 61 62 2d 3e 70 49  (pIndex=pTab->pI
2de0c 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 70 49  ndex; pIndex; pI
2de0d 6e 64 65 78 3d 70 49 6e 64 65 78 2d 3e 70 4e 65  ndex=pIndex->pNe
2de0e 78 74 29 7b 0a 20 20 20 20 69 66 28 20 7a 43 6f  xt){.    if( zCo
2de0f 6c 6c 3d 3d 30 20 7c 7c 20 63 6f 6c 6c 61 74 69  ll==0 || collati
2de10 6f 6e 4d 61 74 63 68 28 7a 43 6f 6c 6c 2c 20 70  onMatch(zColl, p
2de11 49 6e 64 65 78 29 20 29 7b 0a 20 20 20 20 20 20  Index) ){.      
2de12 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65  int iDb = sqlite
2de13 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
2de14 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d  Parse->db, pTab-
2de15 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20  >pSchema);.     
2de16 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
2de17 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
2de18 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20  se, 0, iDb);.   
2de19 20 20 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c     sqlite3Refill
2de1a 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 49  Index(pParse, pI
2de1b 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20 20 7d  ndex, -1);.    }
2de1c 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  .  }.}.#endif../
2de1d 2a 0a 2a 2a 20 52 65 63 6f 6d 70 75 74 65 20 61  *.** Recompute a
2de1e 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 61 6c  ll indices of al
2de1f 6c 20 74 61 62 6c 65 73 20 69 6e 20 61 6c 6c 20  l tables in all 
2de20 64 61 74 61 62 61 73 65 73 20 77 68 65 72 65 20  databases where 
2de21 74 68 65 0a 2a 2a 20 69 6e 64 69 63 65 73 20 75  the.** indices u
2de22 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  se the collating
2de23 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e   sequence pColl.
2de24 20 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20 74 68    If pColl==0 th
2de25 65 6e 20 72 65 63 6f 6d 70 75 74 65 0a 2a 2a 20  en recompute.** 
2de26 61 6c 6c 20 69 6e 64 69 63 65 73 20 65 76 65 72  all indices ever
2de27 79 77 68 65 72 65 2e 0a 2a 2f 0a 23 69 66 6e 64  ywhere..*/.#ifnd
2de28 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52  ef SQLITE_OMIT_R
2de29 45 49 4e 44 45 58 0a 73 74 61 74 69 63 20 76 6f  EINDEX.static vo
2de2a 69 64 20 72 65 69 6e 64 65 78 44 61 74 61 62 61  id reindexDataba
2de2b 73 65 73 28 50 61 72 73 65 20 2a 70 50 61 72 73  ses(Parse *pPars
2de2c 65 2c 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  e, char const *z
2de2d 43 6f 6c 6c 29 7b 0a 20 20 44 62 20 2a 70 44 62  Coll){.  Db *pDb
2de2e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2de2f 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65       /* A single
2de30 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69   database */.  i
2de31 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
2de32 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2de33 65 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78  e database index
2de34 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 71 6c   number */.  sql
2de35 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2de36 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54 68 65 20  e->db;   /* The 
2de37 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2de38 69 6f 6e 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65  ion */.  HashEle
2de39 6d 20 2a 6b 3b 20 20 20 20 20 20 20 20 20 20 20  m *k;           
2de3a 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70       /* For loop
2de3b 69 6e 67 20 6f 76 65 72 20 74 61 62 6c 65 73 20  ing over tables 
2de3c 69 6e 20 70 44 62 20 2a 2f 0a 20 20 54 61 62 6c  in pDb */.  Tabl
2de3d 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
2de3e 20 20 20 20 20 20 20 20 2f 2a 20 41 20 74 61 62          /* A tab
2de3f 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  le in the databa
2de40 73 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  se */..  assert(
2de41 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
2de42 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29  dsAllMutexes(db)
2de43 20 29 3b 20 20 2f 2a 20 4e 65 65 64 65 64 20 66   );  /* Needed f
2de44 6f 72 20 73 63 68 65 6d 61 20 61 63 63 65 73 73  or schema access
2de45 20 2a 2f 0a 20 20 66 6f 72 28 69 44 62 3d 30 2c   */.  for(iDb=0,
2de46 20 70 44 62 3d 64 62 2d 3e 61 44 62 3b 20 69 44   pDb=db->aDb; iD
2de47 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b  b<db->nDb; iDb++
2de48 2c 20 70 44 62 2b 2b 29 7b 0a 20 20 20 20 61 73  , pDb++){.    as
2de49 73 65 72 74 28 20 70 44 62 21 3d 30 20 29 3b 0a  sert( pDb!=0 );.
2de4a 20 20 20 20 66 6f 72 28 6b 3d 73 71 6c 69 74 65      for(k=sqlite
2de4b 48 61 73 68 46 69 72 73 74 28 26 70 44 62 2d 3e  HashFirst(&pDb->
2de4c 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68  pSchema->tblHash
2de4d 29 3b 20 20 6b 3b 20 6b 3d 73 71 6c 69 74 65 48  );  k; k=sqliteH
2de4e 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20 20 20  ashNext(k)){.   
2de4f 20 20 20 70 54 61 62 20 3d 20 28 54 61 62 6c 65     pTab = (Table
2de50 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  *)sqliteHashData
2de51 28 6b 29 3b 0a 20 20 20 20 20 20 72 65 69 6e 64  (k);.      reind
2de52 65 78 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  exTable(pParse, 
2de53 70 54 61 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20  pTab, zColl);.  
2de54 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66    }.  }.}.#endif
2de55 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2de56 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 52 45   code for the RE
2de57 49 4e 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  INDEX command..*
2de58 2a 0a 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e  *.**        REIN
2de59 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20  DEX             
2de5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
2de5b 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 52 45  - 1.**        RE
2de5c 49 4e 44 45 58 20 20 3c 63 6f 6c 6c 61 74 69 6f  INDEX  <collatio
2de5d 6e 3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n>              
2de5e 20 2d 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20 20   -- 2.**        
2de5f 52 45 49 4e 44 45 58 20 20 3f 3c 64 61 74 61 62  REINDEX  ?<datab
2de60 61 73 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65  ase>.?<tablename
2de61 3e 20 20 2d 2d 20 33 0a 2a 2a 20 20 20 20 20 20  >  -- 3.**      
2de62 20 20 52 45 49 4e 44 45 58 20 20 3f 3c 64 61 74    REINDEX  ?<dat
2de63 61 62 61 73 65 3e 2e 3f 3c 69 6e 64 65 78 6e 61  abase>.?<indexna
2de64 6d 65 3e 20 20 2d 2d 20 34 0a 2a 2a 0a 2a 2a 20  me>  -- 4.**.** 
2de65 46 6f 72 6d 20 31 20 63 61 75 73 65 73 20 61 6c  Form 1 causes al
2de66 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c  l indices in all
2de67 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
2de68 73 65 73 20 74 6f 20 62 65 20 72 65 62 75 69 6c  ses to be rebuil
2de69 74 2e 0a 2a 2a 20 46 6f 72 6d 20 32 20 72 65 62  t..** Form 2 reb
2de6a 75 69 6c 64 73 20 61 6c 6c 20 69 6e 64 69 63 65  uilds all indice
2de6b 73 20 69 6e 20 61 6c 6c 20 64 61 74 61 62 61 73  s in all databas
2de6c 65 73 20 74 68 61 74 20 75 73 65 20 74 68 65 20  es that use the 
2de6d 6e 61 6d 65 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69  named.** collati
2de6e 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 20 20 46 6f  ng function.  Fo
2de6f 72 6d 73 20 33 20 61 6e 64 20 34 20 72 65 62 75  rms 3 and 4 rebu
2de70 69 6c 64 20 74 68 65 20 6e 61 6d 65 64 20 69 6e  ild the named in
2de71 64 65 78 20 6f 72 20 61 6c 6c 0a 2a 2a 20 69 6e  dex or all.** in
2de72 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 65 64  dices associated
2de73 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 64 20   with the named 
2de74 74 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65  table..*/.#ifnde
2de75 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45  f SQLITE_OMIT_RE
2de76 49 4e 44 45 58 0a 53 51 4c 49 54 45 5f 50 52 49  INDEX.SQLITE_PRI
2de77 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2de78 33 52 65 69 6e 64 65 78 28 50 61 72 73 65 20 2a  3Reindex(Parse *
2de79 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70  pParse, Token *p
2de7a 4e 61 6d 65 31 2c 20 54 6f 6b 65 6e 20 2a 70 4e  Name1, Token *pN
2de7b 61 6d 65 32 29 7b 0a 20 20 43 6f 6c 6c 53 65 71  ame2){.  CollSeq
2de7c 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20   *pColl;        
2de7d 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e       /* Collatin
2de7e 67 20 73 65 71 75 65 6e 63 65 20 74 6f 20 62 65  g sequence to be
2de7f 20 72 65 69 6e 64 65 78 65 64 2c 20 6f 72 20 4e   reindexed, or N
2de80 55 4c 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ULL */.  char *z
2de81 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2de82 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2de83 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  a table or index
2de84 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2de85 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20   *zDb;          
2de86 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
2de87 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 54   database */.  T
2de88 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
2de89 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
2de8a 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74  table in the dat
2de8b 61 62 61 73 65 20 2a 2f 0a 20 20 49 6e 64 65 78  abase */.  Index
2de8c 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 20 20   *pIndex;       
2de8d 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64         /* An ind
2de8e 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ex associated wi
2de8f 74 68 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74  th pTab */.  int
2de90 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
2de91 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2de92 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 6e  database index n
2de93 75 6d 62 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74  umber */.  sqlit
2de94 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2de95 3e 64 62 3b 20 20 20 2f 2a 20 54 68 65 20 64 61  >db;   /* The da
2de96 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2de97 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4f  n */.  Token *pO
2de98 62 6a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  bjName;         
2de99 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
2de9a 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  e table or index
2de9b 20 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64   to be reindexed
2de9c 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74   */..  /* Read t
2de9d 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
2de9e 6d 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  ma. If an error 
2de9f 6f 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e  occurs, leave an
2dea0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20   error message. 
2dea1 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20   ** and code in 
2dea2 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72  pParse and retur
2dea3 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28  n NULL. */.  if(
2dea4 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69   SQLITE_OK!=sqli
2dea5 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
2dea6 61 72 73 65 29 20 29 7b 0a 20 20 20 20 72 65 74  arse) ){.    ret
2dea7 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  urn;.  }..  if( 
2dea8 70 4e 61 6d 65 31 3d 3d 30 20 29 7b 0a 20 20 20  pName1==0 ){.   
2dea9 20 72 65 69 6e 64 65 78 44 61 74 61 62 61 73 65   reindexDatabase
2deaa 73 28 70 50 61 72 73 65 2c 20 30 29 3b 0a 20 20  s(pParse, 0);.  
2deab 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73    return;.  }els
2deac 65 20 69 66 28 20 4e 45 56 45 52 28 70 4e 61 6d  e if( NEVER(pNam
2dead 65 32 3d 3d 30 29 20 7c 7c 20 70 4e 61 6d 65 32  e2==0) || pName2
2deae 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68  ->z==0 ){.    ch
2deaf 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20 20 61  ar *zColl;.    a
2deb0 73 73 65 72 74 28 20 70 4e 61 6d 65 31 2d 3e 7a  ssert( pName1->z
2deb1 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 6c 20 3d 20   );.    zColl = 
2deb2 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
2deb3 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c  oken(pParse->db,
2deb4 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69 66   pName1);.    if
2deb5 28 20 21 7a 43 6f 6c 6c 20 29 20 72 65 74 75 72  ( !zColl ) retur
2deb6 6e 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73  n;.    pColl = s
2deb7 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
2deb8 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a  q(db, ENC(db), z
2deb9 43 6f 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 69 66  Coll, 0);.    if
2deba 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20  ( pColl ){.     
2debb 20 72 65 69 6e 64 65 78 44 61 74 61 62 61 73 65   reindexDatabase
2debc 73 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29  s(pParse, zColl)
2debd 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
2debe 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29  bFree(db, zColl)
2debf 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
2dec0 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2dec1 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c  3DbFree(db, zCol
2dec2 6c 29 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20  l);.  }.  iDb = 
2dec3 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61  sqlite3TwoPartNa
2dec4 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65  me(pParse, pName
2dec5 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4f 62 6a  1, pName2, &pObj
2dec6 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69 44 62  Name);.  if( iDb
2dec7 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a  <0 ) return;.  z
2dec8 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
2dec9 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4f 62 6a  omToken(db, pObj
2deca 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 3d 3d  Name);.  if( z==
2decb 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 44  0 ) return;.  zD
2decc 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  b = db->aDb[iDb]
2decd 2e 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62 20 3d  .zName;.  pTab =
2dece 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
2decf 65 28 64 62 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20  e(db, z, zDb);. 
2ded0 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20   if( pTab ){.   
2ded1 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28 70 50   reindexTable(pP
2ded2 61 72 73 65 2c 20 70 54 61 62 2c 20 30 29 3b 0a  arse, pTab, 0);.
2ded3 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2ded4 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 72 65  e(db, z);.    re
2ded5 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 49 6e 64  turn;.  }.  pInd
2ded6 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ex = sqlite3Find
2ded7 49 6e 64 65 78 28 64 62 2c 20 7a 2c 20 7a 44 62  Index(db, z, zDb
2ded8 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
2ded9 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 69 66 28  ee(db, z);.  if(
2deda 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73   pIndex ){.    s
2dedb 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
2dedc 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
2dedd 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  , 0, iDb);.    s
2dede 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65  qlite3RefillInde
2dedf 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78  x(pParse, pIndex
2dee0 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72  , -1);.    retur
2dee1 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  n;.  }.  sqlite3
2dee2 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2dee3 20 22 75 6e 61 62 6c 65 20 74 6f 20 69 64 65 6e   "unable to iden
2dee4 74 69 66 79 20 74 68 65 20 6f 62 6a 65 63 74 20  tify the object 
2dee5 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 22  to be reindexed"
2dee6 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  );.}.#endif../*.
2dee7 2a 2a 20 52 65 74 75 72 6e 20 61 20 64 79 6e 61  ** Return a dyna
2dee8 6d 69 63 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  micly allocated 
2dee9 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
2deea 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73  e that can be us
2deeb 65 64 0a 2a 2a 20 77 69 74 68 20 4f 50 5f 4f 70  ed.** with OP_Op
2deec 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65  enRead or OP_Ope
2deed 6e 57 72 69 74 65 20 74 6f 20 61 63 63 65 73 73  nWrite to access
2deee 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 20   database index 
2deef 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  pIdx..**.** If s
2def0 75 63 63 65 73 73 66 75 6c 2c 20 61 20 70 6f 69  uccessful, a poi
2def1 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20  nter to the new 
2def2 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65 74  structure is ret
2def3 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63  urned. In this c
2def4 61 73 65 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65  ase.** the calle
2def5 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  r is responsible
2def6 20 66 6f 72 20 63 61 6c 6c 69 6e 67 20 73 71 6c   for calling sql
2def7 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 29  ite3DbFree(db, )
2def8 20 6f 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64   on the returned
2def9 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 49 66   .** pointer. If
2defa 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
2defb 20 28 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20   (out of memory 
2defc 6f 72 20 6d 69 73 73 69 6e 67 20 63 6f 6c 6c 61  or missing colla
2defd 74 69 6f 6e 20 0a 2a 2a 20 73 65 71 75 65 6e 63  tion .** sequenc
2defe 65 29 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  e), NULL is retu
2deff 72 6e 65 64 20 61 6e 64 20 74 68 65 20 73 74 61  rned and the sta
2df00 74 65 20 6f 66 20 70 50 61 72 73 65 20 75 70 64  te of pParse upd
2df01 61 74 65 64 20 74 6f 20 72 65 66 6c 65 63 74 0a  ated to reflect.
2df02 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2f  ** the error..*/
2df03 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2df04 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33  KeyInfo *sqlite3
2df05 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 72  IndexKeyinfo(Par
2df06 73 65 20 2a 70 50 61 72 73 65 2c 20 49 6e 64 65  se *pParse, Inde
2df07 78 20 2a 70 49 64 78 29 7b 0a 20 20 69 6e 74 20  x *pIdx){.  int 
2df08 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20  i;.  int nCol = 
2df09 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20  pIdx->nColumn;. 
2df0a 20 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69   int nBytes = si
2df0b 7a 65 6f 66 28 4b 65 79 49 6e 66 6f 29 20 2b 20  zeof(KeyInfo) + 
2df0c 28 6e 43 6f 6c 2d 31 29 2a 73 69 7a 65 6f 66 28  (nCol-1)*sizeof(
2df0d 43 6f 6c 6c 53 65 71 2a 29 20 2b 20 6e 43 6f 6c  CollSeq*) + nCol
2df0e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
2df0f 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2df10 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20  KeyInfo *pKey = 
2df11 28 4b 65 79 49 6e 66 6f 20 2a 29 73 71 6c 69 74  (KeyInfo *)sqlit
2df12 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
2df13 62 2c 20 6e 42 79 74 65 73 29 3b 0a 0a 20 20 69  b, nBytes);..  i
2df14 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 70  f( pKey ){.    p
2df15 4b 65 79 2d 3e 64 62 20 3d 20 70 50 61 72 73 65  Key->db = pParse
2df16 2d 3e 64 62 3b 0a 20 20 20 20 70 4b 65 79 2d 3e  ->db;.    pKey->
2df17 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38  aSortOrder = (u8
2df18 20 2a 29 26 28 70 4b 65 79 2d 3e 61 43 6f 6c 6c   *)&(pKey->aColl
2df19 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 20 20 61 73 73  [nCol]);.    ass
2df1a 65 72 74 28 20 26 70 4b 65 79 2d 3e 61 53 6f 72  ert( &pKey->aSor
2df1b 74 4f 72 64 65 72 5b 6e 43 6f 6c 5d 3d 3d 26 28  tOrder[nCol]==&(
2df1c 28 28 75 38 20 2a 29 70 4b 65 79 29 5b 6e 42 79  ((u8 *)pKey)[nBy
2df1d 74 65 73 5d 29 20 29 3b 0a 20 20 20 20 66 6f 72  tes]) );.    for
2df1e 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
2df1f 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  +){.      char *
2df20 7a 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e 61 7a  zColl = pIdx->az
2df21 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 61  Coll[i];.      a
2df22 73 73 65 72 74 28 20 7a 43 6f 6c 6c 20 29 3b 0a  ssert( zColl );.
2df23 20 20 20 20 20 20 70 4b 65 79 2d 3e 61 43 6f 6c        pKey->aCol
2df24 6c 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 4c 6f  l[i] = sqlite3Lo
2df25 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72  cateCollSeq(pPar
2df26 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20  se, zColl);.    
2df27 20 20 70 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64    pKey->aSortOrd
2df28 65 72 5b 69 5d 20 3d 20 70 49 64 78 2d 3e 61 53  er[i] = pIdx->aS
2df29 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20 20  ortOrder[i];.   
2df2a 20 7d 0a 20 20 20 20 70 4b 65 79 2d 3e 6e 46 69   }.    pKey->nFi
2df2b 65 6c 64 20 3d 20 28 75 31 36 29 6e 43 6f 6c 3b  eld = (u16)nCol;
2df2c 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 72  .  }..  if( pPar
2df2d 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20  se->nErr ){.    
2df2e 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2df2f 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20 70 4b 65  , pKey);.    pKe
2df30 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  y = 0;.  }.  ret
2df31 75 72 6e 20 70 4b 65 79 3b 0a 7d 0a 0a 2f 2a 2a  urn pKey;.}../**
2df32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
2df33 20 6f 66 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a   of build.c ****
2df34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2df35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2df36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
2df37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
2df38 69 6e 20 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b  in file callback
2df39 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2df3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2df3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
2df3c 2a 2a 20 32 30 30 35 20 4d 61 79 20 32 33 20 0a  ** 2005 May 23 .
2df3d 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
2df3e 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2df3f 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2df40 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2df41 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2df42 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2df43 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
2df44 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2df45 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2df46 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2df47 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2df48 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2df49 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2df4a 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2df4b 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
2df4c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
2df4d 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
2df4e 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
2df4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2df50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2df51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2df52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2df53 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
2df54 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e  ontains function
2df55 73 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  s used to access
2df56 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 68 61   the internal ha
2df57 73 68 20 74 61 62 6c 65 73 0a 2a 2a 20 6f 66 20  sh tables.** of 
2df58 75 73 65 72 20 64 65 66 69 6e 65 64 20 66 75 6e  user defined fun
2df59 63 74 69 6f 6e 73 20 61 6e 64 20 63 6f 6c 6c 61  ctions and colla
2df5a 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 2e 0a  tion sequences..
2df5b 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b  */.../*.** Invok
2df5c 65 20 74 68 65 20 27 63 6f 6c 6c 61 74 69 6f 6e  e the 'collation
2df5d 20 6e 65 65 64 65 64 27 20 63 61 6c 6c 62 61 63   needed' callbac
2df5e 6b 20 74 6f 20 72 65 71 75 65 73 74 20 61 20 63  k to request a c
2df5f 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2df60 65 0a 2a 2a 20 69 6e 20 74 68 65 20 65 6e 63 6f  e.** in the enco
2df61 64 69 6e 67 20 65 6e 63 20 6f 66 20 6e 61 6d 65  ding enc of name
2df62 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e   zName, length n
2df63 4e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Name..*/.static 
2df64 76 6f 69 64 20 63 61 6c 6c 43 6f 6c 6c 4e 65 65  void callCollNee
2df65 64 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ded(sqlite3 *db,
2df66 20 69 6e 74 20 65 6e 63 2c 20 63 6f 6e 73 74 20   int enc, const 
2df67 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
2df68 61 73 73 65 72 74 28 20 21 64 62 2d 3e 78 43 6f  assert( !db->xCo
2df69 6c 6c 4e 65 65 64 65 64 20 7c 7c 20 21 64 62 2d  llNeeded || !db-
2df6a 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 29  >xCollNeeded16 )
2df6b 3b 0a 20 20 69 66 28 20 64 62 2d 3e 78 43 6f 6c  ;.  if( db->xCol
2df6c 6c 4e 65 65 64 65 64 20 29 7b 0a 20 20 20 20 63  lNeeded ){.    c
2df6d 68 61 72 20 2a 7a 45 78 74 65 72 6e 61 6c 20 3d  har *zExternal =
2df6e 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
2df6f 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  (db, zName);.   
2df70 20 69 66 28 20 21 7a 45 78 74 65 72 6e 61 6c 20   if( !zExternal 
2df71 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 64 62  ) return;.    db
2df72 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 28 64 62  ->xCollNeeded(db
2df73 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67  ->pCollNeededArg
2df74 2c 20 64 62 2c 20 65 6e 63 2c 20 7a 45 78 74 65  , db, enc, zExte
2df75 72 6e 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74  rnal);.    sqlit
2df76 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 78  e3DbFree(db, zEx
2df77 74 65 72 6e 61 6c 29 3b 0a 20 20 7d 0a 23 69 66  ternal);.  }.#if
2df78 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2df79 5f 55 54 46 31 36 0a 20 20 69 66 28 20 64 62 2d  _UTF16.  if( db-
2df7a 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 29  >xCollNeeded16 )
2df7b 7b 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74  {.    char const
2df7c 20 2a 7a 45 78 74 65 72 6e 61 6c 3b 0a 20 20 20   *zExternal;.   
2df7d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2df7e 70 54 6d 70 20 3d 20 73 71 6c 69 74 65 33 56 61  pTmp = sqlite3Va
2df7f 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20  lueNew(db);.    
2df80 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53  sqlite3ValueSetS
2df81 74 72 28 70 54 6d 70 2c 20 2d 31 2c 20 7a 4e 61  tr(pTmp, -1, zNa
2df82 6d 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  me, SQLITE_UTF8,
2df83 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
2df84 0a 20 20 20 20 7a 45 78 74 65 72 6e 61 6c 20 3d  .    zExternal =
2df85 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78   sqlite3ValueTex
2df86 74 28 70 54 6d 70 2c 20 53 51 4c 49 54 45 5f 55  t(pTmp, SQLITE_U
2df87 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 20 20 20  TF16NATIVE);.   
2df88 20 69 66 28 20 7a 45 78 74 65 72 6e 61 6c 20 29   if( zExternal )
2df89 7b 0a 20 20 20 20 20 20 64 62 2d 3e 78 43 6f 6c  {.      db->xCol
2df8a 6c 4e 65 65 64 65 64 31 36 28 64 62 2d 3e 70 43  lNeeded16(db->pC
2df8b 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 64 62  ollNeededArg, db
2df8c 2c 20 28 69 6e 74 29 45 4e 43 28 64 62 29 2c 20  , (int)ENC(db), 
2df8d 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20 20 20  zExternal);.    
2df8e 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c  }.    sqlite3Val
2df8f 75 65 46 72 65 65 28 70 54 6d 70 29 3b 0a 20 20  ueFree(pTmp);.  
2df90 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
2df91 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
2df92 73 20 63 61 6c 6c 65 64 20 69 66 20 74 68 65 20  s called if the 
2df93 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72  collation factor
2df94 79 20 66 61 69 6c 73 20 74 6f 20 64 65 6c 69 76  y fails to deliv
2df95 65 72 20 61 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f  er a.** collatio
2df96 6e 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 74 68  n function in th
2df97 65 20 62 65 73 74 20 65 6e 63 6f 64 69 6e 67 20  e best encoding 
2df98 62 75 74 20 74 68 65 72 65 20 6d 61 79 20 62 65  but there may be
2df99 20 6f 74 68 65 72 20 76 65 72 73 69 6f 6e 73 0a   other versions.
2df9a 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f 6c 6c 61  ** of this colla
2df9b 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 28 66  tion function (f
2df9c 6f 72 20 6f 74 68 65 72 20 74 65 78 74 20 65 6e  or other text en
2df9d 63 6f 64 69 6e 67 73 29 20 61 76 61 69 6c 61 62  codings) availab
2df9e 6c 65 2e 20 55 73 65 20 6f 6e 65 0a 2a 2a 20 6f  le. Use one.** o
2df9f 66 20 74 68 65 73 65 20 69 6e 73 74 65 61 64 20  f these instead 
2dfa0 69 66 20 74 68 65 79 20 65 78 69 73 74 2e 20 41  if they exist. A
2dfa1 76 6f 69 64 20 61 20 55 54 46 2d 38 20 3c 2d 3e  void a UTF-8 <->
2dfa2 20 55 54 46 2d 31 36 20 63 6f 6e 76 65 72 73 69   UTF-16 conversi
2dfa3 6f 6e 20 69 66 0a 2a 2a 20 70 6f 73 73 69 62 6c  on if.** possibl
2dfa4 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
2dfa5 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 73 71   synthCollSeq(sq
2dfa6 6c 69 74 65 33 20 2a 64 62 2c 20 43 6f 6c 6c 53  lite3 *db, CollS
2dfa7 65 71 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 43 6f  eq *pColl){.  Co
2dfa8 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 32 3b 0a 20  llSeq *pColl2;. 
2dfa9 20 63 68 61 72 20 2a 7a 20 3d 20 70 43 6f 6c 6c   char *z = pColl
2dfaa 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 69  ->zName;.  int i
2dfab 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
2dfac 20 75 38 20 61 45 6e 63 5b 5d 20 3d 20 7b 20 53   u8 aEnc[] = { S
2dfad 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53  QLITE_UTF16BE, S
2dfae 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 53  QLITE_UTF16LE, S
2dfaf 51 4c 49 54 45 5f 55 54 46 38 20 7d 3b 0a 20 20  QLITE_UTF8 };.  
2dfb0 66 6f 72 28 69 3d 30 3b 20 69 3c 33 3b 20 69 2b  for(i=0; i<3; i+
2dfb1 2b 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 32 20 3d  +){.    pColl2 =
2dfb2 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
2dfb3 53 65 71 28 64 62 2c 20 61 45 6e 63 5b 69 5d 2c  Seq(db, aEnc[i],
2dfb4 20 7a 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20   z, 0);.    if( 
2dfb5 70 43 6f 6c 6c 32 2d 3e 78 43 6d 70 21 3d 30 20  pColl2->xCmp!=0 
2dfb6 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
2dfb7 70 43 6f 6c 6c 2c 20 70 43 6f 6c 6c 32 2c 20 73  pColl, pColl2, s
2dfb8 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 29 29 3b  izeof(CollSeq));
2dfb9 0a 20 20 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 44  .      pColl->xD
2dfba 65 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  el = 0;         
2dfbb 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 70 79 20 74  /* Do not copy t
2dfbc 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 2a 2f  he destructor */
2dfbd 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
2dfbe 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
2dfbf 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
2dfc0 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a  TE_ERROR;.}../*.
2dfc1 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
2dfc2 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
2dfc3 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65  for invoking the
2dfc4 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f   collation facto
2dfc5 72 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 6f  ry callback.** o
2dfc6 72 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 61  r substituting a
2dfc7 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2dfc8 6e 63 65 20 6f 66 20 61 20 64 69 66 66 65 72 65  nce of a differe
2dfc9 6e 74 20 65 6e 63 6f 64 69 6e 67 20 77 68 65 6e  nt encoding when
2dfca 20 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 65   the.** requeste
2dfcb 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  d collation sequ
2dfcc 65 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 61 69  ence is not avai
2dfcd 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 64 65 73  lable in the des
2dfce 69 72 65 64 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a  ired encoding..*
2dfcf 2a 20 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e  * .** If it is n
2dfd0 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 43  ot NULL, then pC
2dfd1 6f 6c 6c 20 6d 75 73 74 20 70 6f 69 6e 74 20 74  oll must point t
2dfd2 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  o the database n
2dfd3 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 0a  ative encoding .
2dfd4 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ** collation seq
2dfd5 75 65 6e 63 65 20 77 69 74 68 20 6e 61 6d 65 20  uence with name 
2dfd6 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e  zName, length nN
2dfd7 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ame..**.** The r
2dfd8 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 65  eturn value is e
2dfd9 69 74 68 65 72 20 74 68 65 20 63 6f 6c 6c 61 74  ither the collat
2dfda 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 74 6f 20  ion sequence to 
2dfdb 62 65 20 75 73 65 64 20 69 6e 20 64 61 74 61 62  be used in datab
2dfdc 61 73 65 0a 2a 2a 20 64 62 20 66 6f 72 20 63 6f  ase.** db for co
2dfdd 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6e 61 6d  llation type nam
2dfde 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20  e zName, length 
2dfdf 6e 4e 61 6d 65 2c 20 6f 72 20 4e 55 4c 4c 2c 20  nName, or NULL, 
2dfe0 69 66 20 6e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 0a  if no collation.
2dfe1 2a 2a 20 73 65 71 75 65 6e 63 65 20 63 61 6e 20  ** sequence can 
2dfe2 62 65 20 66 6f 75 6e 64 2e 20 20 49 66 20 6e 6f  be found.  If no
2dfe3 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 20 66 6f   collation is fo
2dfe4 75 6e 64 2c 20 6c 65 61 76 65 20 61 6e 20 65 72  und, leave an er
2dfe5 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a  ror message..**.
2dfe6 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 73 71 6c  ** See also: sql
2dfe7 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65  ite3LocateCollSe
2dfe8 71 28 29 2c 20 73 71 6c 69 74 65 33 46 69 6e 64  q(), sqlite3Find
2dfe9 43 6f 6c 6c 53 65 71 28 29 0a 2a 2f 0a 53 51 4c  CollSeq().*/.SQL
2dfea 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c  ITE_PRIVATE Coll
2dfeb 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74 43  Seq *sqlite3GetC
2dfec 6f 6c 6c 53 65 71 28 0a 20 20 50 61 72 73 65 20  ollSeq(.  Parse 
2dfed 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2dfee 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2dfef 78 74 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20  xt */.  u8 enc, 
2dff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2dff1 20 54 68 65 20 64 65 73 69 72 65 64 20 65 6e 63   The desired enc
2dff2 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f  oding for the co
2dff3 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2dff4 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70   */.  CollSeq *p
2dff5 43 6f 6c 6c 2c 20 20 20 20 20 20 20 2f 2a 20 43  Coll,       /* C
2dff6 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2dff7 65 20 77 69 74 68 20 6e 61 74 69 76 65 20 65 6e  e with native en
2dff8 63 6f 64 69 6e 67 2c 20 6f 72 20 4e 55 4c 4c 20  coding, or NULL 
2dff9 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2dffa 2a 7a 4e 61 6d 65 20 20 20 20 20 2f 2a 20 43 6f  *zName     /* Co
2dffb 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2dffc 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 43 6f   name */.){.  Co
2dffd 6c 6c 53 65 71 20 2a 70 3b 0a 20 20 73 71 6c 69  llSeq *p;.  sqli
2dffe 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2dfff 2d 3e 64 62 3b 0a 0a 20 20 70 20 3d 20 70 43 6f  ->db;..  p = pCo
2e000 6c 6c 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a  ll;.  if( !p ){.
2e001 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46      p = sqlite3F
2e002 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65  indCollSeq(db, e
2e003 6e 63 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  nc, zName, 0);. 
2e004 20 7d 0a 20 20 69 66 28 20 21 70 20 7c 7c 20 21   }.  if( !p || !
2e005 70 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 20 20 2f  p->xCmp ){.    /
2e006 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  * No collation s
2e007 65 71 75 65 6e 63 65 20 6f 66 20 74 68 69 73 20  equence of this 
2e008 74 79 70 65 20 66 6f 72 20 74 68 69 73 20 65 6e  type for this en
2e009 63 6f 64 69 6e 67 20 69 73 20 72 65 67 69 73 74  coding is regist
2e00a 65 72 65 64 2e 0a 20 20 20 20 2a 2a 20 43 61 6c  ered..    ** Cal
2e00b 6c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  l the collation 
2e00c 66 61 63 74 6f 72 79 20 74 6f 20 73 65 65 20 69  factory to see i
2e00d 66 20 69 74 20 63 61 6e 20 73 75 70 70 6c 79 20  f it can supply 
2e00e 75 73 20 77 69 74 68 20 6f 6e 65 2e 0a 20 20 20  us with one..   
2e00f 20 2a 2f 0a 20 20 20 20 63 61 6c 6c 43 6f 6c 6c   */.    callColl
2e010 4e 65 65 64 65 64 28 64 62 2c 20 65 6e 63 2c 20  Needed(db, enc, 
2e011 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 20 3d 20  zName);.    p = 
2e012 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
2e013 65 71 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d  eq(db, enc, zNam
2e014 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  e, 0);.  }.  if(
2e015 20 70 20 26 26 20 21 70 2d 3e 78 43 6d 70 20 26   p && !p->xCmp &
2e016 26 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 64  & synthCollSeq(d
2e017 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 70 20 3d  b, p) ){.    p =
2e018 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
2e019 28 20 21 70 20 7c 7c 20 70 2d 3e 78 43 6d 70 20  ( !p || p->xCmp 
2e01a 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b  );.  if( p==0 ){
2e01b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2e01c 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
2e01d 20 73 75 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20   such collation 
2e01e 73 65 71 75 65 6e 63 65 3a 20 25 73 22 2c 20 7a  sequence: %s", z
2e01f 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Name);.  }.  ret
2e020 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
2e021 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
2e022 63 61 6c 6c 65 64 20 6f 6e 20 61 20 63 6f 6c 6c  called on a coll
2e023 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 62  ation sequence b
2e024 65 66 6f 72 65 20 69 74 20 69 73 20 75 73 65 64  efore it is used
2e025 20 74 6f 0a 2a 2a 20 63 68 65 63 6b 20 74 68 61   to.** check tha
2e026 74 20 69 74 20 69 73 20 64 65 66 69 6e 65 64 2e  t it is defined.
2e027 20 41 6e 20 75 6e 64 65 66 69 6e 65 64 20 63 6f   An undefined co
2e028 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2e029 20 65 78 69 73 74 73 20 77 68 65 6e 0a 2a 2a 20   exists when.** 
2e02a 61 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f  a database is lo
2e02b 61 64 65 64 20 74 68 61 74 20 63 6f 6e 74 61 69  aded that contai
2e02c 6e 73 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  ns references to
2e02d 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2e02e 6e 63 65 73 0a 2a 2a 20 74 68 61 74 20 68 61 76  nces.** that hav
2e02f 65 20 6e 6f 74 20 62 65 65 6e 20 64 65 66 69 6e  e not been defin
2e030 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 72  ed by sqlite3_cr
2e031 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29  eate_collation()
2e032 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72   etc..**.** If r
2e033 65 71 75 69 72 65 64 2c 20 74 68 69 73 20 72 6f  equired, this ro
2e034 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 68 65 20  utine calls the 
2e035 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64 65  'collation neede
2e036 64 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f 0a 2a  d' callback to.*
2e037 2a 20 72 65 71 75 65 73 74 20 61 20 64 65 66 69  * request a defi
2e038 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f  nition of the co
2e039 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2e03a 2e 20 49 66 20 74 68 69 73 20 64 6f 65 73 6e 27  . If this doesn'
2e03b 74 20 77 6f 72 6b 2c 20 0a 2a 2a 20 61 6e 20 65  t work, .** an e
2e03c 71 75 69 76 61 6c 65 6e 74 20 63 6f 6c 6c 61 74  quivalent collat
2e03d 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 68 61  ing sequence tha
2e03e 74 20 75 73 65 73 20 61 20 74 65 78 74 20 65 6e  t uses a text en
2e03f 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 74  coding different
2e040 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6d 61 69  .** from the mai
2e041 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 73 75  n database is su
2e042 62 73 74 69 74 75 74 65 64 2c 20 69 66 20 6f 6e  bstituted, if on
2e043 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a  e is available..
2e044 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2e045 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65  E int sqlite3Che
2e046 63 6b 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20  ckCollSeq(Parse 
2e047 2a 70 50 61 72 73 65 2c 20 43 6f 6c 6c 53 65 71  *pParse, CollSeq
2e048 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 66 28 20   *pColl){.  if( 
2e049 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 63 6f 6e  pColl ){.    con
2e04a 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d  st char *zName =
2e04b 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20   pColl->zName;. 
2e04c 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
2e04d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20   pParse->db;.   
2e04e 20 43 6f 6c 6c 53 65 71 20 2a 70 20 3d 20 73 71   CollSeq *p = sq
2e04f 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28  lite3GetCollSeq(
2e050 70 50 61 72 73 65 2c 20 45 4e 43 28 64 62 29 2c  pParse, ENC(db),
2e051 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 29 3b 0a   pColl, zName);.
2e052 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20      if( !p ){.  
2e053 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2e054 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
2e055 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 43     assert( p==pC
2e056 6f 6c 6c 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74  oll );.  }.  ret
2e057 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
2e058 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65  ..../*.** Locate
2e059 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65   and return an e
2e05a 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 64 62  ntry from the db
2e05b 2e 61 43 6f 6c 6c 53 65 71 20 68 61 73 68 20 74  .aCollSeq hash t
2e05c 61 62 6c 65 2e 20 49 66 20 74 68 65 20 65 6e 74  able. If the ent
2e05d 72 79 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20  ry.** specified 
2e05e 62 79 20 7a 4e 61 6d 65 20 61 6e 64 20 6e 4e 61  by zName and nNa
2e05f 6d 65 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 20  me is not found 
2e060 61 6e 64 20 70 61 72 61 6d 65 74 65 72 20 27 63  and parameter 'c
2e061 72 65 61 74 65 27 20 69 73 0a 2a 2a 20 74 72 75  reate' is.** tru
2e062 65 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61  e, then create a
2e063 20 6e 65 77 20 65 6e 74 72 79 2e 20 4f 74 68 65   new entry. Othe
2e064 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c  rwise return NUL
2e065 4c 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 70 6f  L..**.** Each po
2e066 69 6e 74 65 72 20 73 74 6f 72 65 64 20 69 6e 20  inter stored in 
2e067 74 68 65 20 73 71 6c 69 74 65 33 2e 61 43 6f 6c  the sqlite3.aCol
2e068 6c 53 65 71 20 68 61 73 68 20 74 61 62 6c 65 20  lSeq hash table 
2e069 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 2a 2a 20 61  contains an.** a
2e06a 72 72 61 79 20 6f 66 20 74 68 72 65 65 20 43 6f  rray of three Co
2e06b 6c 6c 53 65 71 20 73 74 72 75 63 74 75 72 65 73  llSeq structures
2e06c 2e 20 54 68 65 20 66 69 72 73 74 20 69 73 20 74  . The first is t
2e06d 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
2e06e 75 65 6e 63 65 0a 2a 2a 20 70 72 65 66 66 65 72  uence.** preffer
2e06f 72 65 64 20 66 6f 72 20 55 54 46 2d 38 2c 20 74  red for UTF-8, t
2e070 68 65 20 73 65 63 6f 6e 64 20 55 54 46 2d 31 36  he second UTF-16
2e071 6c 65 2c 20 61 6e 64 20 74 68 65 20 74 68 69 72  le, and the thir
2e072 64 20 55 54 46 2d 31 36 62 65 2e 0a 2a 2a 0a 2a  d UTF-16be..**.*
2e073 2a 20 53 74 6f 72 65 64 20 69 6d 6d 65 64 69 61  * Stored immedia
2e074 74 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 74  tely after the t
2e075 68 72 65 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  hree collation s
2e076 65 71 75 65 6e 63 65 73 20 69 73 20 61 20 63 6f  equences is a co
2e077 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c  py of.** the col
2e078 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2e079 6e 61 6d 65 2e 20 41 20 70 6f 69 6e 74 65 72 20  name. A pointer 
2e07a 74 6f 20 74 68 69 73 20 73 74 72 69 6e 67 20 69  to this string i
2e07b 73 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 65  s stored in.** e
2e07c 61 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ach collation se
2e07d 71 75 65 6e 63 65 20 73 74 72 75 63 74 75 72 65  quence structure
2e07e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c  ..*/.static Coll
2e07f 53 65 71 20 2a 66 69 6e 64 43 6f 6c 6c 53 65 71  Seq *findCollSeq
2e080 45 6e 74 72 79 28 0a 20 20 73 71 6c 69 74 65 33  Entry(.  sqlite3
2e081 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f   *db,          /
2e082 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
2e083 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ction */.  const
2e084 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20   char *zName,   
2e085 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
2e086 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2e087 63 65 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61  ce */.  int crea
2e088 74 65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  te            /*
2e089 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 65 6e   Create a new en
2e08a 74 72 79 20 69 66 20 74 72 75 65 20 2a 2f 0a 29  try if true */.)
2e08b 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
2e08c 6c 6c 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20  ll;.  int nName 
2e08d 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
2e08e 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 43 6f 6c  0(zName);.  pCol
2e08f 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  l = sqlite3HashF
2e090 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  ind(&db->aCollSe
2e091 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  q, zName, nName)
2e092 3b 0a 0a 20 20 69 66 28 20 30 3d 3d 70 43 6f 6c  ;..  if( 0==pCol
2e093 6c 20 26 26 20 63 72 65 61 74 65 20 29 7b 0a 20  l && create ){. 
2e094 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
2e095 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
2e096 62 2c 20 33 2a 73 69 7a 65 6f 66 28 2a 70 43 6f  b, 3*sizeof(*pCo
2e097 6c 6c 29 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 20  ll) + nName + 1 
2e098 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c  );.    if( pColl
2e099 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65   ){.      CollSe
2e09a 71 20 2a 70 44 65 6c 20 3d 20 30 3b 0a 20 20 20  q *pDel = 0;.   
2e09b 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d     pColl[0].zNam
2e09c 65 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c  e = (char*)&pCol
2e09d 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c  l[3];.      pCol
2e09e 6c 5b 30 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54  l[0].enc = SQLIT
2e09f 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 43  E_UTF8;.      pC
2e0a0 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20 28  oll[1].zName = (
2e0a1 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b  char*)&pColl[3];
2e0a2 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 31 5d 2e  .      pColl[1].
2e0a3 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  enc = SQLITE_UTF
2e0a4 31 36 4c 45 3b 0a 20 20 20 20 20 20 70 43 6f 6c  16LE;.      pCol
2e0a5 6c 5b 32 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68  l[2].zName = (ch
2e0a6 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20  ar*)&pColl[3];. 
2e0a7 20 20 20 20 20 70 43 6f 6c 6c 5b 32 5d 2e 65 6e       pColl[2].en
2e0a8 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36  c = SQLITE_UTF16
2e0a9 42 45 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  BE;.      memcpy
2e0aa 28 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c  (pColl[0].zName,
2e0ab 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a   zName, nName);.
2e0ac 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a        pColl[0].z
2e0ad 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b  Name[nName] = 0;
2e0ae 0a 20 20 20 20 20 20 70 44 65 6c 20 3d 20 73 71  .      pDel = sq
2e0af 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
2e0b0 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 70  &db->aCollSeq, p
2e0b1 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 6e  Coll[0].zName, n
2e0b2 4e 61 6d 65 2c 20 70 43 6f 6c 6c 29 3b 0a 0a 20  Name, pColl);.. 
2e0b3 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c       /* If a mal
2e0b4 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 6f 63  loc() failure oc
2e0b5 63 75 72 72 65 64 20 69 6e 20 73 71 6c 69 74 65  curred in sqlite
2e0b6 33 48 61 73 68 49 6e 73 65 72 74 28 29 2c 20 69  3HashInsert(), i
2e0b7 74 20 77 69 6c 6c 20 0a 20 20 20 20 20 20 2a 2a  t will .      **
2e0b8 20 72 65 74 75 72 6e 20 74 68 65 20 70 43 6f 6c   return the pCol
2e0b9 6c 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20  l pointer to be 
2e0ba 64 65 6c 65 74 65 64 20 28 62 65 63 61 75 73 65  deleted (because
2e0bb 20 69 74 20 77 61 73 6e 27 74 20 61 64 64 65 64   it wasn't added
2e0bc 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65  .      ** to the
2e0bd 20 68 61 73 68 20 74 61 62 6c 65 29 2e 0a 20 20   hash table)..  
2e0be 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73      */.      ass
2e0bf 65 72 74 28 20 70 44 65 6c 3d 3d 30 20 7c 7c 20  ert( pDel==0 || 
2e0c0 70 44 65 6c 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20  pDel==pColl );. 
2e0c1 20 20 20 20 20 69 66 28 20 70 44 65 6c 21 3d 30       if( pDel!=0
2e0c2 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e   ){.        db->
2e0c3 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
2e0c4 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2e0c5 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 65 6c  3DbFree(db, pDel
2e0c6 29 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  );.        pColl
2e0c7 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
2e0c8 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2e0c9 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   pColl;.}../*.**
2e0ca 20 50 61 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65   Parameter zName
2e0cb 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 55 54 46   points to a UTF
2e0cc 2d 38 20 65 6e 63 6f 64 65 64 20 73 74 72 69 6e  -8 encoded strin
2e0cd 67 20 6e 4e 61 6d 65 20 62 79 74 65 73 20 6c 6f  g nName bytes lo
2e0ce 6e 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ng..** Return th
2e0cf 65 20 43 6f 6c 6c 53 65 71 2a 20 70 6f 69 6e 74  e CollSeq* point
2e0d0 65 72 20 66 6f 72 20 74 68 65 20 63 6f 6c 6c 61  er for the colla
2e0d1 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
2e0d2 6d 65 64 20 7a 4e 61 6d 65 0a 2a 2a 20 66 6f 72  med zName.** for
2e0d3 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 27 65   the encoding 'e
2e0d4 6e 63 27 20 66 72 6f 6d 20 74 68 65 20 64 61 74  nc' from the dat
2e0d5 61 62 61 73 65 20 27 64 62 27 2e 0a 2a 2a 0a 2a  abase 'db'..**.*
2e0d6 2a 20 49 66 20 74 68 65 20 65 6e 74 72 79 20 73  * If the entry s
2e0d7 70 65 63 69 66 69 65 64 20 69 73 20 6e 6f 74 20  pecified is not 
2e0d8 66 6f 75 6e 64 20 61 6e 64 20 27 63 72 65 61 74  found and 'creat
2e0d9 65 27 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e  e' is true, then
2e0da 20 63 72 65 61 74 65 20 61 0a 2a 2a 20 6e 65 77   create a.** new
2e0db 20 65 6e 74 72 79 2e 20 20 4f 74 68 65 72 77 69   entry.  Otherwi
2e0dc 73 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a  se return NULL..
2e0dd 2a 2a 0a 2a 2a 20 41 20 73 65 70 61 72 61 74 65  **.** A separate
2e0de 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65   function sqlite
2e0df 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 29  3LocateCollSeq()
2e0e0 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72   is a wrapper ar
2e0e1 6f 75 6e 64 0a 2a 2a 20 74 68 69 73 20 72 6f 75  ound.** this rou
2e0e2 74 69 6e 65 2e 20 20 73 71 6c 69 74 65 33 4c 6f  tine.  sqlite3Lo
2e0e3 63 61 74 65 43 6f 6c 6c 53 65 71 28 29 20 69 6e  cateCollSeq() in
2e0e4 76 6f 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61 74  vokes the collat
2e0e5 69 6f 6e 20 66 61 63 74 6f 72 79 0a 2a 2a 20 69  ion factory.** i
2e0e6 66 20 6e 65 63 65 73 73 61 72 79 20 61 6e 64 20  f necessary and 
2e0e7 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72 72  generates an err
2e0e8 6f 72 20 6d 65 73 73 61 67 65 20 69 66 20 74 68  or message if th
2e0e9 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
2e0ea 65 6e 63 65 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62  ence.** cannot b
2e0eb 65 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 53  e found..**.** S
2e0ec 65 65 20 61 6c 73 6f 3a 20 73 71 6c 69 74 65 33  ee also: sqlite3
2e0ed 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 29 2c  LocateCollSeq(),
2e0ee 20 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53   sqlite3GetCollS
2e0ef 65 71 28 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  eq().*/.SQLITE_P
2e0f0 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a  RIVATE CollSeq *
2e0f1 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
2e0f2 65 71 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  eq(.  sqlite3 *d
2e0f3 62 2c 0a 20 20 75 38 20 65 6e 63 2c 0a 20 20 63  b,.  u8 enc,.  c
2e0f4 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2e0f5 2c 0a 20 20 69 6e 74 20 63 72 65 61 74 65 0a 29  ,.  int create.)
2e0f6 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
2e0f7 6c 6c 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20  ll;.  if( zName 
2e0f8 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 66  ){.    pColl = f
2e0f9 69 6e 64 43 6f 6c 6c 53 65 71 45 6e 74 72 79 28  indCollSeqEntry(
2e0fa 64 62 2c 20 7a 4e 61 6d 65 2c 20 63 72 65 61 74  db, zName, creat
2e0fb 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e);.  }else{.   
2e0fc 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66   pColl = db->pDf
2e0fd 6c 74 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 61 73  ltColl;.  }.  as
2e0fe 73 65 72 74 28 20 53 51 4c 49 54 45 5f 55 54 46  sert( SQLITE_UTF
2e0ff 38 3d 3d 31 20 26 26 20 53 51 4c 49 54 45 5f 55  8==1 && SQLITE_U
2e100 54 46 31 36 4c 45 3d 3d 32 20 26 26 20 53 51 4c  TF16LE==2 && SQL
2e101 49 54 45 5f 55 54 46 31 36 42 45 3d 3d 33 20 29  ITE_UTF16BE==3 )
2e102 3b 0a 20 20 61 73 73 65 72 74 28 20 65 6e 63 3e  ;.  assert( enc>
2e103 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26 20  =SQLITE_UTF8 && 
2e104 65 6e 63 3c 3d 53 51 4c 49 54 45 5f 55 54 46 31  enc<=SQLITE_UTF1
2e105 36 42 45 20 29 3b 0a 20 20 69 66 28 20 70 43 6f  6BE );.  if( pCo
2e106 6c 6c 20 29 20 70 43 6f 6c 6c 20 2b 3d 20 65 6e  ll ) pColl += en
2e107 63 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 70 43  c-1;.  return pC
2e108 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 20 44 75 72 69 6e  oll;.}../* Durin
2e109 67 20 74 68 65 20 73 65 61 72 63 68 20 66 6f 72  g the search for
2e10a 20 74 68 65 20 62 65 73 74 20 66 75 6e 63 74 69   the best functi
2e10b 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20 74  on definition, t
2e10c 68 69 73 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a  his procedure.**
2e10d 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 74 65   is called to te
2e10e 73 74 20 68 6f 77 20 77 65 6c 6c 20 74 68 65 20  st how well the 
2e10f 66 75 6e 63 74 69 6f 6e 20 70 61 73 73 65 64 20  function passed 
2e110 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
2e111 75 6d 65 6e 74 0a 2a 2a 20 6d 61 74 63 68 65 73  ument.** matches
2e112 20 74 68 65 20 72 65 71 75 65 73 74 20 66 6f 72   the request for
2e113 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   a function with
2e114 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 20   nArg arguments 
2e115 69 6e 20 61 20 73 79 73 74 65 6d 0a 2a 2a 20 74  in a system.** t
2e116 68 61 74 20 75 73 65 73 20 65 6e 63 6f 64 69 6e  hat uses encodin
2e117 67 20 65 6e 63 2e 20 54 68 65 20 76 61 6c 75 65  g enc. The value
2e118 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 61   returned indica
2e119 74 65 73 20 68 6f 77 20 77 65 6c 6c 20 74 68 65  tes how well the
2e11a 0a 2a 2a 20 72 65 71 75 65 73 74 20 69 73 20 6d  .** request is m
2e11b 61 74 63 68 65 64 2e 20 41 20 68 69 67 68 65 72  atched. A higher
2e11c 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73   value indicates
2e11d 20 61 20 62 65 74 74 65 72 20 6d 61 74 63 68 2e   a better match.
2e11e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 41 72 67 20 69  .**.** If nArg i
2e11f 73 20 2d 31 20 74 68 61 74 20 6d 65 61 6e 73 20  s -1 that means 
2e120 74 6f 20 6f 6e 6c 79 20 72 65 74 75 72 6e 20 61  to only return a
2e121 20 6d 61 74 63 68 20 28 6e 6f 6e 2d 7a 65 72 6f   match (non-zero
2e122 29 20 69 66 20 70 2d 3e 6e 41 72 67 0a 2a 2a 20  ) if p->nArg.** 
2e123 69 73 20 61 6c 73 6f 20 2d 31 2e 20 20 49 6e 20  is also -1.  In 
2e124 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 77 65 20  other words, we 
2e125 61 72 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f  are searching fo
2e126 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  r a function tha
2e127 74 0a 2a 2a 20 74 61 6b 65 73 20 61 20 76 61 72  t.** takes a var
2e128 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 6f 66 20  iable number of 
2e129 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  arguments..**.**
2e12a 20 49 66 20 6e 41 72 67 20 69 73 20 2d 32 20 74   If nArg is -2 t
2e12b 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77  hat means that w
2e12c 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67 20  e are searching 
2e12d 66 6f 72 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e  for any function
2e12e 20 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73 73 20   .** regardless 
2e12f 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
2e130 20 61 72 67 75 6d 65 6e 74 73 20 69 74 20 75 73   arguments it us
2e131 65 73 2c 20 73 6f 20 72 65 74 75 72 6e 20 61 20  es, so return a 
2e132 70 6f 73 69 74 69 76 65 0a 2a 2a 20 6d 61 74 63  positive.** matc
2e133 68 20 73 63 6f 72 65 20 66 6f 72 20 61 6e 79 0a  h score for any.
2e134 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  **.** The return
2e135 65 64 20 76 61 6c 75 65 20 69 73 20 61 6c 77 61  ed value is alwa
2e136 79 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64  ys between 0 and
2e137 20 36 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a   6, as follows:.
2e138 2a 2a 0a 2a 2a 20 30 3a 20 4e 6f 74 20 61 20 6d  **.** 0: Not a m
2e139 61 74 63 68 2e 0a 2a 2a 20 31 3a 20 55 54 46 38  atch..** 1: UTF8
2e13a 2f 31 36 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72  /16 conversion r
2e13b 65 71 75 69 72 65 64 20 61 6e 64 20 66 75 6e 63  equired and func
2e13c 74 69 6f 6e 20 74 61 6b 65 73 20 61 6e 79 20 6e  tion takes any n
2e13d 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
2e13e 74 73 2e 0a 2a 2a 20 32 3a 20 55 54 46 31 36 20  ts..** 2: UTF16 
2e13f 62 79 74 65 20 6f 72 64 65 72 20 63 68 61 6e 67  byte order chang
2e140 65 20 72 65 71 75 69 72 65 64 20 61 6e 64 20 66  e required and f
2e141 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 6e  unction takes an
2e142 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 73  y number of args
2e143 2e 0a 2a 2a 20 33 3a 20 65 6e 63 6f 64 69 6e 67  ..** 3: encoding
2e144 20 6d 61 74 63 68 65 73 20 61 6e 64 20 66 75 6e   matches and fun
2e145 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 6e 79 20  ction takes any 
2e146 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
2e147 6e 74 73 0a 2a 2a 20 34 3a 20 55 54 46 38 2f 31  nts.** 4: UTF8/1
2e148 36 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 65 71  6 conversion req
2e149 75 69 72 65 64 20 2d 20 61 72 67 75 6d 65 6e 74  uired - argument
2e14a 20 63 6f 75 6e 74 20 6d 61 74 63 68 65 73 20 65   count matches e
2e14b 78 61 63 74 6c 79 0a 2a 2a 20 35 3a 20 55 54 46  xactly.** 5: UTF
2e14c 31 36 20 62 79 74 65 20 6f 72 64 65 72 20 63 6f  16 byte order co
2e14d 6e 76 65 72 73 69 6f 6e 20 72 65 71 75 69 72 65  nversion require
2e14e 64 20 2d 20 61 72 67 75 6d 65 6e 74 20 63 6f 75  d - argument cou
2e14f 6e 74 20 6d 61 74 63 68 65 73 20 65 78 61 63 74  nt matches exact
2e150 6c 79 0a 2a 2a 20 36 3a 20 50 65 72 66 65 63 74  ly.** 6: Perfect
2e151 20 6d 61 74 63 68 3a 20 20 65 6e 63 6f 64 69 6e   match:  encodin
2e152 67 20 61 6e 64 20 61 72 67 75 6d 65 6e 74 20 63  g and argument c
2e153 6f 75 6e 74 20 6d 61 74 63 68 20 65 78 61 63 74  ount match exact
2e154 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 41 72  ly..**.** If nAr
2e155 67 3d 3d 28 2d 32 29 20 74 68 65 6e 20 61 6e 79  g==(-2) then any
2e156 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61   function with a
2e157 20 6e 6f 6e 2d 6e 75 6c 6c 20 78 53 74 65 70 20   non-null xStep 
2e158 6f 72 20 78 46 75 6e 63 20 69 73 0a 2a 2a 20 61  or xFunc is.** a
2e159 20 70 65 72 66 65 63 74 20 6d 61 74 63 68 20 61   perfect match a
2e15a 6e 64 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e 20  nd any function 
2e15b 77 69 74 68 20 62 6f 74 68 20 78 53 74 65 70 20  with both xStep 
2e15c 61 6e 64 20 78 46 75 6e 63 20 4e 55 4c 4c 20 69  and xFunc NULL i
2e15d 73 0a 2a 2a 20 61 20 6e 6f 6e 2d 6d 61 74 63 68  s.** a non-match
2e15e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 55 4e  ..*/.#define FUN
2e15f 43 5f 50 45 52 46 45 43 54 5f 4d 41 54 43 48 20  C_PERFECT_MATCH 
2e160 36 20 20 2f 2a 20 54 68 65 20 73 63 6f 72 65 20  6  /* The score 
2e161 66 6f 72 20 61 20 70 65 72 66 65 63 74 20 6d 61  for a perfect ma
2e162 74 63 68 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  tch */.static in
2e163 74 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28 0a  t matchQuality(.
2e164 20 20 46 75 6e 63 44 65 66 20 2a 70 2c 20 20 20    FuncDef *p,   
2e165 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f    /* The functio
2e166 6e 20 77 65 20 61 72 65 20 65 76 61 6c 75 61 74  n we are evaluat
2e167 69 6e 67 20 66 6f 72 20 6d 61 74 63 68 20 71 75  ing for match qu
2e168 61 6c 69 74 79 20 2a 2f 0a 20 20 69 6e 74 20 6e  ality */.  int n
2e169 41 72 67 2c 20 20 20 20 20 20 20 2f 2a 20 44 65  Arg,       /* De
2e16a 73 69 72 65 64 20 6e 75 6d 62 65 72 20 6f 66 20  sired number of 
2e16b 61 72 67 75 6d 65 6e 74 73 2e 20 20 28 2d 31 29  arguments.  (-1)
2e16c 3d 3d 61 6e 79 20 2a 2f 0a 20 20 75 38 20 65 6e  ==any */.  u8 en
2e16d 63 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65  c          /* De
2e16e 73 69 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64  sired text encod
2e16f 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ing */.){.  int 
2e170 6d 61 74 63 68 3b 0a 0a 20 20 2f 2a 20 6e 41 72  match;..  /* nAr
2e171 67 20 6f 66 20 2d 32 20 69 73 20 61 20 73 70 65  g of -2 is a spe
2e172 63 69 61 6c 20 63 61 73 65 20 2a 2f 0a 20 20 69  cial case */.  i
2e173 66 28 20 6e 41 72 67 3d 3d 28 2d 32 29 20 29 20  f( nArg==(-2) ) 
2e174 72 65 74 75 72 6e 20 28 70 2d 3e 78 46 75 6e 63  return (p->xFunc
2e175 3d 3d 30 20 26 26 20 70 2d 3e 78 53 74 65 70 3d  ==0 && p->xStep=
2e176 3d 30 29 20 3f 20 30 20 3a 20 46 55 4e 43 5f 50  =0) ? 0 : FUNC_P
2e177 45 52 46 45 43 54 5f 4d 41 54 43 48 3b 0a 0a 20  ERFECT_MATCH;.. 
2e178 20 2f 2a 20 57 72 6f 6e 67 20 6e 75 6d 62 65 72   /* Wrong number
2e179 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6d 65   of arguments me
2e17a 61 6e 73 20 22 6e 6f 20 6d 61 74 63 68 22 20 2a  ans "no match" *
2e17b 2f 0a 20 20 69 66 28 20 70 2d 3e 6e 41 72 67 21  /.  if( p->nArg!
2e17c 3d 6e 41 72 67 20 26 26 20 70 2d 3e 6e 41 72 67  =nArg && p->nArg
2e17d 3e 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  >=0 ) return 0;.
2e17e 0a 20 20 2f 2a 20 47 69 76 65 20 61 20 62 65 74  .  /* Give a bet
2e17f 74 65 72 20 73 63 6f 72 65 20 74 6f 20 61 20 66  ter score to a f
2e180 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 73  unction with a s
2e181 70 65 63 69 66 69 63 20 6e 75 6d 62 65 72 20 6f  pecific number o
2e182 66 20 61 72 67 75 6d 65 6e 74 73 0a 20 20 2a 2a  f arguments.  **
2e183 20 74 68 61 6e 20 74 6f 20 66 75 6e 63 74 69 6f   than to functio
2e184 6e 20 74 68 61 74 20 61 63 63 65 70 74 73 20 61  n that accepts a
2e185 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  ny number of arg
2e186 75 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 28  uments. */.  if(
2e187 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 29   p->nArg==nArg )
2e188 7b 0a 20 20 20 20 6d 61 74 63 68 20 3d 20 34 3b  {.    match = 4;
2e189 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 61  .  }else{.    ma
2e18a 74 63 68 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  tch = 1;.  }..  
2e18b 2f 2a 20 42 6f 6e 75 73 20 70 6f 69 6e 74 73 20  /* Bonus points 
2e18c 69 66 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f  if the text enco
2e18d 64 69 6e 67 20 6d 61 74 63 68 65 73 20 2a 2f 0a  ding matches */.
2e18e 20 20 69 66 28 20 65 6e 63 3d 3d 70 2d 3e 69 50    if( enc==p->iP
2e18f 72 65 66 45 6e 63 20 29 7b 0a 20 20 20 20 6d 61  refEnc ){.    ma
2e190 74 63 68 20 2b 3d 20 32 3b 20 20 2f 2a 20 45 78  tch += 2;  /* Ex
2e191 61 63 74 20 65 6e 63 6f 64 69 6e 67 20 6d 61 74  act encoding mat
2e192 63 68 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66  ch */.  }else if
2e193 28 20 28 65 6e 63 20 26 20 70 2d 3e 69 50 72 65  ( (enc & p->iPre
2e194 66 45 6e 63 20 26 20 32 29 21 3d 30 20 29 7b 0a  fEnc & 2)!=0 ){.
2e195 20 20 20 20 6d 61 74 63 68 20 2b 3d 20 31 3b 20      match += 1; 
2e196 20 2f 2a 20 42 6f 74 68 20 61 72 65 20 55 54 46   /* Both are UTF
2e197 31 36 2c 20 62 75 74 20 77 69 74 68 20 64 69 66  16, but with dif
2e198 66 65 72 65 6e 74 20 62 79 74 65 20 6f 72 64 65  ferent byte orde
2e199 72 73 20 2a 2f 0a 20 20 7d 0a 0a 20 20 72 65 74  rs */.  }..  ret
2e19a 75 72 6e 20 6d 61 74 63 68 3b 0a 7d 0a 0a 2f 2a  urn match;.}../*
2e19b 0a 2a 2a 20 53 65 61 72 63 68 20 61 20 46 75 6e  .** Search a Fun
2e19c 63 44 65 66 48 61 73 68 20 66 6f 72 20 61 20 66  cDefHash for a f
2e19d 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65  unction with the
2e19e 20 67 69 76 65 6e 20 6e 61 6d 65 2e 20 20 52 65   given name.  Re
2e19f 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  turn.** a pointe
2e1a0 72 20 74 6f 20 74 68 65 20 6d 61 74 63 68 69 6e  r to the matchin
2e1a1 67 20 46 75 6e 63 44 65 66 20 69 66 20 66 6f 75  g FuncDef if fou
2e1a2 6e 64 2c 20 6f 72 20 30 20 69 66 20 74 68 65 72  nd, or 0 if ther
2e1a3 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a  e is no match..*
2e1a4 2f 0a 73 74 61 74 69 63 20 46 75 6e 63 44 65 66  /.static FuncDef
2e1a5 20 2a 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68   *functionSearch
2e1a6 28 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20  (.  FuncDefHash 
2e1a7 2a 70 48 61 73 68 2c 20 20 2f 2a 20 48 61 73 68  *pHash,  /* Hash
2e1a8 20 74 61 62 6c 65 20 74 6f 20 73 65 61 72 63 68   table to search
2e1a9 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20   */.  int h,    
2e1aa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
2e1ab 73 68 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 2a  sh of the name *
2e1ac 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2e1ad 7a 46 75 6e 63 2c 20 20 20 2f 2a 20 4e 61 6d 65  zFunc,   /* Name
2e1ae 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a   of function */.
2e1af 20 20 69 6e 74 20 6e 46 75 6e 63 20 20 20 20 20    int nFunc     
2e1b0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2e1b1 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 46 75   of bytes in zFu
2e1b2 6e 63 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e 63 44  nc */.){.  FuncD
2e1b3 65 66 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70  ef *p;.  for(p=p
2e1b4 48 61 73 68 2d 3e 61 5b 68 5d 3b 20 70 3b 20 70  Hash->a[h]; p; p
2e1b5 3d 70 2d 3e 70 48 61 73 68 29 7b 0a 20 20 20 20  =p->pHash){.    
2e1b6 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49  if( sqlite3StrNI
2e1b7 43 6d 70 28 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 46  Cmp(p->zName, zF
2e1b8 75 6e 63 2c 20 6e 46 75 6e 63 29 3d 3d 30 20 26  unc, nFunc)==0 &
2e1b9 26 20 70 2d 3e 7a 4e 61 6d 65 5b 6e 46 75 6e 63  & p->zName[nFunc
2e1ba 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  ]==0 ){.      re
2e1bb 74 75 72 6e 20 70 3b 0a 20 20 20 20 7d 0a 20 20  turn p;.    }.  
2e1bc 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2e1bd 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20  ./*.** Insert a 
2e1be 6e 65 77 20 46 75 6e 63 44 65 66 20 69 6e 74 6f  new FuncDef into
2e1bf 20 61 20 46 75 6e 63 44 65 66 48 61 73 68 20 68   a FuncDefHash h
2e1c0 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  ash table..*/.SQ
2e1c1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2e1c2 64 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66  d sqlite3FuncDef
2e1c3 49 6e 73 65 72 74 28 0a 20 20 46 75 6e 63 44 65  Insert(.  FuncDe
2e1c4 66 48 61 73 68 20 2a 70 48 61 73 68 2c 20 20 2f  fHash *pHash,  /
2e1c5 2a 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65  * The hash table
2e1c6 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 69   into which to i
2e1c7 6e 73 65 72 74 20 2a 2f 0a 20 20 46 75 6e 63 44  nsert */.  FuncD
2e1c8 65 66 20 2a 70 44 65 66 20 20 20 20 20 20 20 20  ef *pDef        
2e1c9 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20  /* The function 
2e1ca 64 65 66 69 6e 69 74 69 6f 6e 20 74 6f 20 69 6e  definition to in
2e1cb 73 65 72 74 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e  sert */.){.  Fun
2e1cc 63 44 65 66 20 2a 70 4f 74 68 65 72 3b 0a 20 20  cDef *pOther;.  
2e1cd 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  int nName = sqli
2e1ce 74 65 33 53 74 72 6c 65 6e 33 30 28 70 44 65 66  te3Strlen30(pDef
2e1cf 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 75 38 20 63  ->zName);.  u8 c
2e1d0 31 20 3d 20 28 75 38 29 70 44 65 66 2d 3e 7a 4e  1 = (u8)pDef->zN
2e1d1 61 6d 65 5b 30 5d 3b 0a 20 20 69 6e 74 20 68 20  ame[0];.  int h 
2e1d2 3d 20 28 73 71 6c 69 74 65 33 55 70 70 65 72 54  = (sqlite3UpperT
2e1d3 6f 4c 6f 77 65 72 5b 63 31 5d 20 2b 20 6e 4e 61  oLower[c1] + nNa
2e1d4 6d 65 29 20 25 20 41 72 72 61 79 53 69 7a 65 28  me) % ArraySize(
2e1d5 70 48 61 73 68 2d 3e 61 29 3b 0a 20 20 70 4f 74  pHash->a);.  pOt
2e1d6 68 65 72 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65  her = functionSe
2e1d7 61 72 63 68 28 70 48 61 73 68 2c 20 68 2c 20 70  arch(pHash, h, p
2e1d8 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  Def->zName, nNam
2e1d9 65 29 3b 0a 20 20 69 66 28 20 70 4f 74 68 65 72  e);.  if( pOther
2e1da 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
2e1db 70 4f 74 68 65 72 21 3d 70 44 65 66 20 26 26 20  pOther!=pDef && 
2e1dc 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 21 3d 70  pOther->pNext!=p
2e1dd 44 65 66 20 29 3b 0a 20 20 20 20 70 44 65 66 2d  Def );.    pDef-
2e1de 3e 70 4e 65 78 74 20 3d 20 70 4f 74 68 65 72 2d  >pNext = pOther-
2e1df 3e 70 4e 65 78 74 3b 0a 20 20 20 20 70 4f 74 68  >pNext;.    pOth
2e1e0 65 72 2d 3e 70 4e 65 78 74 20 3d 20 70 44 65 66  er->pNext = pDef
2e1e1 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
2e1e2 44 65 66 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a  Def->pNext = 0;.
2e1e3 20 20 20 20 70 44 65 66 2d 3e 70 48 61 73 68 20      pDef->pHash 
2e1e4 3d 20 70 48 61 73 68 2d 3e 61 5b 68 5d 3b 0a 20  = pHash->a[h];. 
2e1e5 20 20 20 70 48 61 73 68 2d 3e 61 5b 68 5d 20 3d     pHash->a[h] =
2e1e6 20 70 44 65 66 3b 0a 20 20 7d 0a 7d 0a 20 20 0a   pDef;.  }.}.  .
2e1e7 20 20 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65    ../*.** Locate
2e1e8 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e   a user function
2e1e9 20 67 69 76 65 6e 20 61 20 6e 61 6d 65 2c 20 61   given a name, a
2e1ea 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
2e1eb 65 6e 74 73 20 61 6e 64 20 61 20 66 6c 61 67 0a  ents and a flag.
2e1ec 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 77 68  ** indicating wh
2e1ed 65 74 68 65 72 20 74 68 65 20 66 75 6e 63 74 69  ether the functi
2e1ee 6f 6e 20 70 72 65 66 65 72 73 20 55 54 46 2d 31  on prefers UTF-1
2e1ef 36 20 6f 76 65 72 20 55 54 46 2d 38 2e 20 20 52  6 over UTF-8.  R
2e1f0 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74  eturn a.** point
2e1f1 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65  er to the FuncDe
2e1f2 66 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  f structure that
2e1f3 20 64 65 66 69 6e 65 73 20 74 68 61 74 20 66 75   defines that fu
2e1f4 6e 63 74 69 6f 6e 2c 20 6f 72 20 72 65 74 75 72  nction, or retur
2e1f5 6e 0a 2a 2a 20 4e 55 4c 4c 20 69 66 20 74 68 65  n.** NULL if the
2e1f6 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e   function does n
2e1f7 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ot exist..**.** 
2e1f8 49 66 20 74 68 65 20 63 72 65 61 74 65 46 6c 61  If the createFla
2e1f9 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 72  g argument is tr
2e1fa 75 65 2c 20 74 68 65 6e 20 61 20 6e 65 77 20 28  ue, then a new (
2e1fb 62 6c 61 6e 6b 29 20 46 75 6e 63 44 65 66 0a 2a  blank) FuncDef.*
2e1fc 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63  * structure is c
2e1fd 72 65 61 74 65 64 20 61 6e 64 20 6c 69 6b 65 64  reated and liked
2e1fe 20 69 6e 74 6f 20 74 68 65 20 22 64 62 22 20 73   into the "db" s
2e1ff 74 72 75 63 74 75 72 65 20 69 66 20 61 0a 2a 2a  tructure if a.**
2e200 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 66 75 6e   no matching fun
2e201 63 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79  ction previously
2e202 20 65 78 69 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   existed..**.** 
2e203 49 66 20 6e 41 72 67 20 69 73 20 2d 32 2c 20 74  If nArg is -2, t
2e204 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 76 61  hen the first va
2e205 6c 69 64 20 66 75 6e 63 74 69 6f 6e 20 66 6f 75  lid function fou
2e206 6e 64 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  nd is returned. 
2e207 20 41 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69   A.** function i
2e208 73 20 76 61 6c 69 64 20 69 66 20 65 69 74 68 65  s valid if eithe
2e209 72 20 78 46 75 6e 63 20 6f 72 20 78 53 74 65 70  r xFunc or xStep
2e20a 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54   is non-zero.  T
2e20b 68 65 20 6e 41 72 67 3d 3d 28 2d 32 29 0a 2a 2a  he nArg==(-2).**
2e20c 20 63 61 73 65 20 69 73 20 75 73 65 64 20 74 6f   case is used to
2e20d 20 73 65 65 20 69 66 20 7a 4e 61 6d 65 20 69 73   see if zName is
2e20e 20 61 20 76 61 6c 69 64 20 66 75 6e 63 74 69 6f   a valid functio
2e20f 6e 20 6e 61 6d 65 20 66 6f 72 20 73 6f 6d 65 20  n name for some 
2e210 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 61 72 67  number.** of arg
2e211 75 6d 65 6e 74 73 2e 20 20 49 66 20 6e 41 72 67  uments.  If nArg
2e212 20 69 73 20 2d 32 2c 20 74 68 65 6e 20 63 72 65   is -2, then cre
2e213 61 74 65 46 6c 61 67 20 6d 75 73 74 20 62 65 20  ateFlag must be 
2e214 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 72 65 61  0..**.** If crea
2e215 74 65 46 6c 61 67 20 69 73 20 66 61 6c 73 65 2c  teFlag is false,
2e216 20 74 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e   then a function
2e217 20 77 69 74 68 20 74 68 65 20 72 65 71 75 69 72   with the requir
2e218 65 64 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20 6e  ed name and.** n
2e219 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
2e21a 74 73 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e  ts may be return
2e21b 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 20 65  ed even if the e
2e21c 54 65 78 74 52 65 70 20 66 6c 61 67 20 64 6f 65  TextRep flag doe
2e21d 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74 63 68 20 74  s not.** match t
2e21e 68 61 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a  hat requested..*
2e21f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2e220 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 65   FuncDef *sqlite
2e221 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 0a 20  3FindFunction(. 
2e222 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
2e223 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64      /* An open d
2e224 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e  atabase */.  con
2e225 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
2e226 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66  /* Name of the f
2e227 75 6e 63 74 69 6f 6e 2e 20 20 4e 6f 74 20 6e 75  unction.  Not nu
2e228 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f  ll-terminated */
2e229 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 20 20 20  .  int nName,   
2e22a 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2e22b 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  of characters in
2e22c 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69   the name */.  i
2e22d 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20  nt nArg,        
2e22e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
2e22f 72 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65  rguments.  -1 me
2e230 61 6e 73 20 61 6e 79 20 6e 75 6d 62 65 72 20 2a  ans any number *
2e231 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20  /.  u8 enc,     
2e232 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65 72         /* Prefer
2e233 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  red text encodin
2e234 67 20 2a 2f 0a 20 20 75 38 20 63 72 65 61 74 65  g */.  u8 create
2e235 46 6c 61 67 20 20 20 20 20 20 2f 2a 20 43 72 65  Flag      /* Cre
2e236 61 74 65 20 6e 65 77 20 65 6e 74 72 79 20 69 66  ate new entry if
2e237 20 74 72 75 65 20 61 6e 64 20 64 6f 65 73 20 6e   true and does n
2e238 6f 74 20 6f 74 68 65 72 77 69 73 65 20 65 78 69  ot otherwise exi
2e239 73 74 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e 63 44  st */.){.  FuncD
2e23a 65 66 20 2a 70 3b 20 20 20 20 20 20 20 20 20 2f  ef *p;         /
2e23b 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
2e23c 62 6c 65 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66  ble */.  FuncDef
2e23d 20 2a 70 42 65 73 74 20 3d 20 30 3b 20 2f 2a 20   *pBest = 0; /* 
2e23e 42 65 73 74 20 6d 61 74 63 68 20 66 6f 75 6e 64  Best match found
2e23f 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74   so far */.  int
2e240 20 62 65 73 74 53 63 6f 72 65 20 3d 20 30 3b 20   bestScore = 0; 
2e241 20 2f 2a 20 53 63 6f 72 65 20 6f 66 20 62 65 73   /* Score of bes
2e242 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 69 6e 74  t match */.  int
2e243 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   h;             
2e244 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20 2a   /* Hash value *
2e245 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 41 72  /..  assert( nAr
2e246 67 3e 3d 28 2d 32 29 20 29 3b 0a 20 20 61 73 73  g>=(-2) );.  ass
2e247 65 72 74 28 20 6e 41 72 67 3e 3d 28 2d 31 29 20  ert( nArg>=(-1) 
2e248 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30  || createFlag==0
2e249 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 6e   );.  assert( en
2e24a 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c  c==SQLITE_UTF8 |
2e24b 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  | enc==SQLITE_UT
2e24c 46 31 36 4c 45 20 7c 7c 20 65 6e 63 3d 3d 53 51  F16LE || enc==SQ
2e24d 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a  LITE_UTF16BE );.
2e24e 20 20 68 20 3d 20 28 73 71 6c 69 74 65 33 55 70    h = (sqlite3Up
2e24f 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29 7a  perToLower[(u8)z
2e250 4e 61 6d 65 5b 30 5d 5d 20 2b 20 6e 4e 61 6d 65  Name[0]] + nName
2e251 29 20 25 20 41 72 72 61 79 53 69 7a 65 28 64 62  ) % ArraySize(db
2e252 2d 3e 61 46 75 6e 63 2e 61 29 3b 0a 0a 20 20 2f  ->aFunc.a);..  /
2e253 2a 20 46 69 72 73 74 20 73 65 61 72 63 68 20 66  * First search f
2e254 6f 72 20 61 20 6d 61 74 63 68 20 61 6d 6f 6e 67  or a match among
2e255 73 74 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  st the applicati
2e256 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
2e257 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d  ions..  */.  p =
2e258 20 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68 28   functionSearch(
2e259 26 64 62 2d 3e 61 46 75 6e 63 2c 20 68 2c 20 7a  &db->aFunc, h, z
2e25a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
2e25b 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20  while( p ){.    
2e25c 69 6e 74 20 73 63 6f 72 65 20 3d 20 6d 61 74 63  int score = matc
2e25d 68 51 75 61 6c 69 74 79 28 70 2c 20 6e 41 72 67  hQuality(p, nArg
2e25e 2c 20 65 6e 63 29 3b 0a 20 20 20 20 69 66 28 20  , enc);.    if( 
2e25f 73 63 6f 72 65 3e 62 65 73 74 53 63 6f 72 65 20  score>bestScore 
2e260 29 7b 0a 20 20 20 20 20 20 70 42 65 73 74 20 3d  ){.      pBest =
2e261 20 70 3b 0a 20 20 20 20 20 20 62 65 73 74 53 63   p;.      bestSc
2e262 6f 72 65 20 3d 20 73 63 6f 72 65 3b 0a 20 20 20  ore = score;.   
2e263 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e   }.    p = p->pN
2e264 65 78 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  ext;.  }..  /* I
2e265 66 20 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 6f  f no match is fo
2e266 75 6e 64 2c 20 73 65 61 72 63 68 20 74 68 65 20  und, search the 
2e267 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f  built-in functio
2e268 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  ns..  **.  ** If
2e269 20 74 68 65 20 53 51 4c 49 54 45 5f 50 72 65 66   the SQLITE_Pref
2e26a 65 72 42 75 69 6c 74 69 6e 20 66 6c 61 67 20 69  erBuiltin flag i
2e26b 73 20 73 65 74 2c 20 74 68 65 6e 20 73 65 61 72  s set, then sear
2e26c 63 68 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 0a  ch the built-in.
2e26d 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 65    ** functions e
2e26e 76 65 6e 20 69 66 20 61 20 70 72 69 6f 72 20 61  ven if a prior a
2e26f 70 70 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  pp-defined funct
2e270 69 6f 6e 20 77 61 73 20 66 6f 75 6e 64 2e 20 20  ion was found.  
2e271 41 6e 64 20 67 69 76 65 0a 20 20 2a 2a 20 70 72  And give.  ** pr
2e272 69 6f 72 69 74 79 20 74 6f 20 62 75 69 6c 74 2d  iority to built-
2e273 69 6e 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20  in functions..  
2e274 2a 2a 0a 20 20 2a 2a 20 45 78 63 65 70 74 2c 20  **.  ** Except, 
2e275 69 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73  if createFlag is
2e276 20 74 72 75 65 2c 20 74 68 61 74 20 6d 65 61 6e   true, that mean
2e277 73 20 74 68 61 74 20 77 65 20 61 72 65 20 74 72  s that we are tr
2e278 79 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 69 6e 73  ying to.  ** ins
2e279 74 61 6c 6c 20 61 20 6e 65 77 20 66 75 6e 63 74  tall a new funct
2e27a 69 6f 6e 2e 20 20 57 68 61 74 65 76 65 72 20 46  ion.  Whatever F
2e27b 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65  uncDef structure
2e27c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 74 20   is returned it 
2e27d 77 69 6c 6c 0a 20 20 2a 2a 20 68 61 76 65 20 66  will.  ** have f
2e27e 69 65 6c 64 73 20 6f 76 65 72 77 72 69 74 74 65  ields overwritte
2e27f 6e 20 77 69 74 68 20 6e 65 77 20 69 6e 66 6f 72  n with new infor
2e280 6d 61 74 69 6f 6e 20 61 70 70 72 6f 70 72 69 61  mation appropria
2e281 74 65 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20  te for the.  ** 
2e282 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e 20 20 42  new function.  B
2e283 75 74 20 74 68 65 20 46 75 6e 63 44 65 66 73 20  ut the FuncDefs 
2e284 66 6f 72 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e  for built-in fun
2e285 63 74 69 6f 6e 73 20 61 72 65 20 72 65 61 64 2d  ctions are read-
2e286 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53 6f 20 77 65  only..  ** So we
2e287 20 6d 75 73 74 20 6e 6f 74 20 73 65 61 72 63 68   must not search
2e288 20 66 6f 72 20 62 75 69 6c 74 2d 69 6e 73 20 77   for built-ins w
2e289 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e  hen creating a n
2e28a 65 77 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a  ew function..  *
2e28b 2f 20 0a 20 20 69 66 28 20 21 63 72 65 61 74 65  / .  if( !create
2e28c 46 6c 61 67 20 26 26 20 28 70 42 65 73 74 3d 3d  Flag && (pBest==
2e28d 30 20 7c 7c 20 28 64 62 2d 3e 66 6c 61 67 73 20  0 || (db->flags 
2e28e 26 20 53 51 4c 49 54 45 5f 50 72 65 66 65 72 42  & SQLITE_PreferB
2e28f 75 69 6c 74 69 6e 29 21 3d 30 29 20 29 7b 0a 20  uiltin)!=0) ){. 
2e290 20 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a     FuncDefHash *
2e291 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28  pHash = &GLOBAL(
2e292 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c  FuncDefHash, sql
2e293 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69  ite3GlobalFuncti
2e294 6f 6e 73 29 3b 0a 20 20 20 20 62 65 73 74 53 63  ons);.    bestSc
2e295 6f 72 65 20 3d 20 30 3b 0a 20 20 20 20 70 20 3d  ore = 0;.    p =
2e296 20 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68 28   functionSearch(
2e297 70 48 61 73 68 2c 20 68 2c 20 7a 4e 61 6d 65 2c  pHash, h, zName,
2e298 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 77 68 69   nName);.    whi
2e299 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 20 20 69  le( p ){.      i
2e29a 6e 74 20 73 63 6f 72 65 20 3d 20 6d 61 74 63 68  nt score = match
2e29b 51 75 61 6c 69 74 79 28 70 2c 20 6e 41 72 67 2c  Quality(p, nArg,
2e29c 20 65 6e 63 29 3b 0a 20 20 20 20 20 20 69 66 28   enc);.      if(
2e29d 20 73 63 6f 72 65 3e 62 65 73 74 53 63 6f 72 65   score>bestScore
2e29e 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 65 73   ){.        pBes
2e29f 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62  t = p;.        b
2e2a0 65 73 74 53 63 6f 72 65 20 3d 20 73 63 6f 72 65  estScore = score
2e2a1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2e2a2 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20  p = p->pNext;.  
2e2a3 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
2e2a4 20 74 68 65 20 63 72 65 61 74 65 46 6c 61 67 20   the createFlag 
2e2a5 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75  parameter is tru
2e2a6 65 20 61 6e 64 20 74 68 65 20 73 65 61 72 63 68  e and the search
2e2a7 20 64 69 64 20 6e 6f 74 20 72 65 76 65 61 6c 20   did not reveal 
2e2a8 61 6e 0a 20 20 2a 2a 20 65 78 61 63 74 20 6d 61  an.  ** exact ma
2e2a9 74 63 68 20 66 6f 72 20 74 68 65 20 6e 61 6d 65  tch for the name
2e2aa 2c 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  , number of argu
2e2ab 6d 65 6e 74 73 20 61 6e 64 20 65 6e 63 6f 64 69  ments and encodi
2e2ac 6e 67 2c 20 74 68 65 6e 20 61 64 64 20 61 0a 20  ng, then add a. 
2e2ad 20 2a 2a 20 6e 65 77 20 65 6e 74 72 79 20 74 6f   ** new entry to
2e2ae 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
2e2af 61 6e 64 20 72 65 74 75 72 6e 20 69 74 2e 0a 20  and return it.. 
2e2b0 20 2a 2f 0a 20 20 69 66 28 20 63 72 65 61 74 65   */.  if( create
2e2b1 46 6c 61 67 20 26 26 20 62 65 73 74 53 63 6f 72  Flag && bestScor
2e2b2 65 3c 46 55 4e 43 5f 50 45 52 46 45 43 54 5f 4d  e<FUNC_PERFECT_M
2e2b3 41 54 43 48 20 26 26 20 0a 20 20 20 20 20 20 28  ATCH && .      (
2e2b4 70 42 65 73 74 20 3d 20 73 71 6c 69 74 65 33 44  pBest = sqlite3D
2e2b5 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
2e2b6 73 69 7a 65 6f 66 28 2a 70 42 65 73 74 29 2b 6e  sizeof(*pBest)+n
2e2b7 4e 61 6d 65 2b 31 29 29 21 3d 30 20 29 7b 0a 20  Name+1))!=0 ){. 
2e2b8 20 20 20 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 20     pBest->zName 
2e2b9 3d 20 28 63 68 61 72 20 2a 29 26 70 42 65 73 74  = (char *)&pBest
2e2ba 5b 31 5d 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e  [1];.    pBest->
2e2bb 6e 41 72 67 20 3d 20 28 75 31 36 29 6e 41 72 67  nArg = (u16)nArg
2e2bc 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 69 50 72  ;.    pBest->iPr
2e2bd 65 66 45 6e 63 20 3d 20 65 6e 63 3b 0a 20 20 20  efEnc = enc;.   
2e2be 20 6d 65 6d 63 70 79 28 70 42 65 73 74 2d 3e 7a   memcpy(pBest->z
2e2bf 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  Name, zName, nNa
2e2c0 6d 65 29 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e  me);.    pBest->
2e2c1 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30  zName[nName] = 0
2e2c2 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e  ;.    sqlite3Fun
2e2c3 63 44 65 66 49 6e 73 65 72 74 28 26 64 62 2d 3e  cDefInsert(&db->
2e2c4 61 46 75 6e 63 2c 20 70 42 65 73 74 29 3b 0a 20  aFunc, pBest);. 
2e2c5 20 7d 0a 0a 20 20 69 66 28 20 70 42 65 73 74 20   }..  if( pBest 
2e2c6 26 26 20 28 70 42 65 73 74 2d 3e 78 53 74 65 70  && (pBest->xStep
2e2c7 20 7c 7c 20 70 42 65 73 74 2d 3e 78 46 75 6e 63   || pBest->xFunc
2e2c8 20 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 29 20   || createFlag) 
2e2c9 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42  ){.    return pB
2e2ca 65 73 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  est;.  }.  retur
2e2cb 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  n 0;.}../*.** Fr
2e2cc 65 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73  ee all resources
2e2cd 20 68 65 6c 64 20 62 79 20 74 68 65 20 73 63 68   held by the sch
2e2ce 65 6d 61 20 73 74 72 75 63 74 75 72 65 2e 20 54  ema structure. T
2e2cf 68 65 20 76 6f 69 64 2a 20 61 72 67 75 6d 65 6e  he void* argumen
2e2d0 74 20 70 6f 69 6e 74 73 0a 2a 2a 20 61 74 20 61  t points.** at a
2e2d1 20 53 63 68 65 6d 61 20 73 74 72 75 63 74 2e 20   Schema struct. 
2e2d2 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f  This function do
2e2d3 65 73 20 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69  es not call sqli
2e2d4 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 29 20  te3DbFree(db, ) 
2e2d5 6f 6e 20 74 68 65 20 0a 2a 2a 20 70 6f 69 6e 74  on the .** point
2e2d6 65 72 20 69 74 73 65 6c 66 2c 20 69 74 20 6a 75  er itself, it ju
2e2d7 73 74 20 63 6c 65 61 6e 73 20 75 70 20 73 75 62  st cleans up sub
2e2d8 73 69 64 69 61 72 79 20 72 65 73 6f 75 72 63 65  sidiary resource
2e2d9 73 20 28 69 2e 65 2e 20 74 68 65 20 63 6f 6e 74  s (i.e. the cont
2e2da 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 73  ents.** of the s
2e2db 63 68 65 6d 61 20 68 61 73 68 20 74 61 62 6c 65  chema hash table
2e2dc 73 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 63  s)..**.** The Sc
2e2dd 68 65 6d 61 2e 63 61 63 68 65 5f 73 69 7a 65 20  hema.cache_size 
2e2de 76 61 72 69 61 62 6c 65 20 69 73 20 6e 6f 74 20  variable is not 
2e2df 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49  cleared..*/.SQLI
2e2e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2e2e1 73 71 6c 69 74 65 33 53 63 68 65 6d 61 43 6c 65  sqlite3SchemaCle
2e2e2 61 72 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 48  ar(void *p){.  H
2e2e3 61 73 68 20 74 65 6d 70 31 3b 0a 20 20 48 61 73  ash temp1;.  Has
2e2e4 68 20 74 65 6d 70 32 3b 0a 20 20 48 61 73 68 45  h temp2;.  HashE
2e2e5 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 53 63  lem *pElem;.  Sc
2e2e6 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20  hema *pSchema = 
2e2e7 28 53 63 68 65 6d 61 20 2a 29 70 3b 0a 0a 20 20  (Schema *)p;..  
2e2e8 74 65 6d 70 31 20 3d 20 70 53 63 68 65 6d 61 2d  temp1 = pSchema-
2e2e9 3e 74 62 6c 48 61 73 68 3b 0a 20 20 74 65 6d 70  >tblHash;.  temp
2e2ea 32 20 3d 20 70 53 63 68 65 6d 61 2d 3e 74 72 69  2 = pSchema->tri
2e2eb 67 48 61 73 68 3b 0a 20 20 73 71 6c 69 74 65 33  gHash;.  sqlite3
2e2ec 48 61 73 68 49 6e 69 74 28 26 70 53 63 68 65 6d  HashInit(&pSchem
2e2ed 61 2d 3e 74 72 69 67 48 61 73 68 29 3b 0a 20 20  a->trigHash);.  
2e2ee 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
2e2ef 28 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61  (&pSchema->idxHa
2e2f0 73 68 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d  sh);.  for(pElem
2e2f1 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
2e2f2 28 26 74 65 6d 70 32 29 3b 20 70 45 6c 65 6d 3b  (&temp2); pElem;
2e2f3 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73   pElem=sqliteHas
2e2f4 68 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20  hNext(pElem)){. 
2e2f5 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
2e2f6 54 72 69 67 67 65 72 28 30 2c 20 28 54 72 69 67  Trigger(0, (Trig
2e2f7 67 65 72 2a 29 73 71 6c 69 74 65 48 61 73 68 44  ger*)sqliteHashD
2e2f8 61 74 61 28 70 45 6c 65 6d 29 29 3b 0a 20 20 7d  ata(pElem));.  }
2e2f9 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c  .  sqlite3HashCl
2e2fa 65 61 72 28 26 74 65 6d 70 32 29 3b 0a 20 20 73  ear(&temp2);.  s
2e2fb 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26  qlite3HashInit(&
2e2fc 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68  pSchema->tblHash
2e2fd 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73  );.  for(pElem=s
2e2fe 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
2e2ff 74 65 6d 70 31 29 3b 20 70 45 6c 65 6d 3b 20 70  temp1); pElem; p
2e300 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e  Elem=sqliteHashN
2e301 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20  ext(pElem)){.   
2e302 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73   Table *pTab = s
2e303 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45  qliteHashData(pE
2e304 6c 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  lem);.    sqlite
2e305 33 44 65 6c 65 74 65 54 61 62 6c 65 28 30 2c 20  3DeleteTable(0, 
2e306 70 54 61 62 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  pTab);.  }.  sql
2e307 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 74  ite3HashClear(&t
2e308 65 6d 70 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  emp1);.  sqlite3
2e309 48 61 73 68 43 6c 65 61 72 28 26 70 53 63 68 65  HashClear(&pSche
2e30a 6d 61 2d 3e 66 6b 65 79 48 61 73 68 29 3b 0a 20  ma->fkeyHash);. 
2e30b 20 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61   pSchema->pSeqTa
2e30c 62 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 53 63  b = 0;.  if( pSc
2e30d 68 65 6d 61 2d 3e 66 6c 61 67 73 20 26 20 44 42  hema->flags & DB
2e30e 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 29 7b  _SchemaLoaded ){
2e30f 0a 20 20 20 20 70 53 63 68 65 6d 61 2d 3e 69 47  .    pSchema->iG
2e310 65 6e 65 72 61 74 69 6f 6e 2b 2b 3b 0a 20 20 20  eneration++;.   
2e311 20 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 20   pSchema->flags 
2e312 26 3d 20 7e 44 42 5f 53 63 68 65 6d 61 4c 6f 61  &= ~DB_SchemaLoa
2e313 64 65 64 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ded;.  }.}../*.*
2e314 2a 20 46 69 6e 64 20 61 6e 64 20 72 65 74 75 72  * Find and retur
2e315 6e 20 74 68 65 20 73 63 68 65 6d 61 20 61 73 73  n the schema ass
2e316 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 42  ociated with a B
2e317 54 72 65 65 2e 20 20 43 72 65 61 74 65 0a 2a 2a  Tree.  Create.**
2e318 20 61 20 6e 65 77 20 6f 6e 65 20 69 66 20 6e 65   a new one if ne
2e319 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49  cessary..*/.SQLI
2e31a 54 45 5f 50 52 49 56 41 54 45 20 53 63 68 65 6d  TE_PRIVATE Schem
2e31b 61 20 2a 73 71 6c 69 74 65 33 53 63 68 65 6d 61  a *sqlite3Schema
2e31c 47 65 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Get(sqlite3 *db,
2e31d 20 42 74 72 65 65 20 2a 70 42 74 29 7b 0a 20 20   Btree *pBt){.  
2e31e 53 63 68 65 6d 61 20 2a 20 70 3b 0a 20 20 69 66  Schema * p;.  if
2e31f 28 20 70 42 74 20 29 7b 0a 20 20 20 20 70 20 3d  ( pBt ){.    p =
2e320 20 28 53 63 68 65 6d 61 20 2a 29 73 71 6c 69 74   (Schema *)sqlit
2e321 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 70 42  e3BtreeSchema(pB
2e322 74 2c 20 73 69 7a 65 6f 66 28 53 63 68 65 6d 61  t, sizeof(Schema
2e323 29 2c 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61  ), sqlite3Schema
2e324 43 6c 65 61 72 29 3b 0a 20 20 7d 65 6c 73 65 7b  Clear);.  }else{
2e325 0a 20 20 20 20 70 20 3d 20 28 53 63 68 65 6d 61  .    p = (Schema
2e326 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   *)sqlite3DbMall
2e327 6f 63 5a 65 72 6f 28 30 2c 20 73 69 7a 65 6f 66  ocZero(0, sizeof
2e328 28 53 63 68 65 6d 61 29 29 3b 0a 20 20 7d 0a 20  (Schema));.  }. 
2e329 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 64   if( !p ){.    d
2e32a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2e32b 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 20  = 1;.  }else if 
2e32c 28 20 30 3d 3d 70 2d 3e 66 69 6c 65 5f 66 6f 72  ( 0==p->file_for
2e32d 6d 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  mat ){.    sqlit
2e32e 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d 3e 74  e3HashInit(&p->t
2e32f 62 6c 48 61 73 68 29 3b 0a 20 20 20 20 73 71 6c  blHash);.    sql
2e330 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d  ite3HashInit(&p-
2e331 3e 69 64 78 48 61 73 68 29 3b 0a 20 20 20 20 73  >idxHash);.    s
2e332 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26  qlite3HashInit(&
2e333 70 2d 3e 74 72 69 67 48 61 73 68 29 3b 0a 20 20  p->trigHash);.  
2e334 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
2e335 74 28 26 70 2d 3e 66 6b 65 79 48 61 73 68 29 3b  t(&p->fkeyHash);
2e336 0a 20 20 20 20 70 2d 3e 65 6e 63 20 3d 20 53 51  .    p->enc = SQ
2e337 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 7d 0a 20  LITE_UTF8;.  }. 
2e338 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
2e339 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
2e33a 64 20 6f 66 20 63 61 6c 6c 62 61 63 6b 2e 63 20  d of callback.c 
2e33b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e33c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e33d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2e33e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
2e33f 67 69 6e 20 66 69 6c 65 20 64 65 6c 65 74 65 2e  gin file delete.
2e340 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2e341 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e342 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2e343 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
2e344 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
2e345 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
2e346 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2e347 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
2e348 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
2e349 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
2e34a 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
2e34b 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
2e34c 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2e34d 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2e34e 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2e34f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2e350 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2e351 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2e352 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2e353 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
2e354 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
2e355 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
2e356 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e357 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e358 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e359 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e35a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
2e35b 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64  e contains C cod
2e35c 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  e routines that 
2e35d 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68  are called by th
2e35e 65 20 70 61 72 73 65 72 0a 2a 2a 20 69 6e 20 6f  e parser.** in o
2e35f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65  rder to generate
2e360 20 63 6f 64 65 20 66 6f 72 20 44 45 4c 45 54 45   code for DELETE
2e361 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e 74 73   FROM statements
2e362 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 68 69 6c  ..*/../*.** Whil
2e363 65 20 61 20 53 72 63 4c 69 73 74 20 63 61 6e 20  e a SrcList can 
2e364 69 6e 20 67 65 6e 65 72 61 6c 20 72 65 70 72 65  in general repre
2e365 73 65 6e 74 20 6d 75 6c 74 69 70 6c 65 20 74 61  sent multiple ta
2e366 62 6c 65 73 20 61 6e 64 20 73 75 62 71 75 65 72  bles and subquer
2e367 69 65 73 0a 2a 2a 20 28 61 73 20 69 6e 20 74 68  ies.** (as in th
2e368 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
2e369 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2e36a 65 6e 74 29 20 69 6e 20 74 68 69 73 20 63 61 73  ent) in this cas
2e36b 65 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a  e it contains.**
2e36c 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 73   the name of a s
2e36d 69 6e 67 6c 65 20 74 61 62 6c 65 2c 20 61 73 20  ingle table, as 
2e36e 6f 6e 65 20 6d 69 67 68 74 20 66 69 6e 64 20 69  one might find i
2e36f 6e 20 61 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c  n an INSERT, DEL
2e370 45 54 45 2c 0a 2a 2a 20 6f 72 20 55 50 44 41 54  ETE,.** or UPDAT
2e371 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 4c 6f  E statement.  Lo
2e372 6f 6b 20 75 70 20 74 68 61 74 20 74 61 62 6c 65  ok up that table
2e373 20 69 6e 20 74 68 65 20 73 79 6d 62 6f 6c 20 74   in the symbol t
2e374 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 72 65 74 75  able and.** retu
2e375 72 6e 20 61 20 70 6f 69 6e 74 65 72 2e 20 20 53  rn a pointer.  S
2e376 65 74 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  et an error mess
2e377 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e 20 4e  age and return N
2e378 55 4c 4c 20 69 66 20 74 68 65 20 74 61 62 6c 65  ULL if the table
2e379 20 0a 2a 2a 20 6e 61 6d 65 20 69 73 20 6e 6f 74   .** name is not
2e37a 20 66 6f 75 6e 64 20 6f 72 20 69 66 20 61 6e 79   found or if any
2e37b 20 6f 74 68 65 72 20 65 72 72 6f 72 20 6f 63 63   other error occ
2e37c 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  urs..**.** The f
2e37d 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 20  ollowing fields 
2e37e 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  are initialized 
2e37f 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e 20 70  appropriate in p
2e380 53 72 63 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 53  Src:.**.**    pS
2e381 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 20 20 20  rc->a[0].pTab   
2e382 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 74      Pointer to t
2e383 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 0a  he Table object.
2e384 2a 2a 20 20 20 20 70 53 72 63 2d 3e 61 5b 30 5d  **    pSrc->a[0]
2e385 2e 70 49 6e 64 65 78 20 20 20 20 20 50 6f 69 6e  .pIndex     Poin
2e386 74 65 72 20 74 6f 20 74 68 65 20 49 4e 44 45 58  ter to the INDEX
2e387 45 44 20 42 59 20 69 6e 64 65 78 2c 20 69 66 20  ED BY index, if 
2e388 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 2a 2a 0a  there is one.**.
2e389 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2e38a 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33  E Table *sqlite3
2e38b 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61  SrcListLookup(Pa
2e38c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63  rse *pParse, Src
2e38d 4c 69 73 74 20 2a 70 53 72 63 29 7b 0a 20 20 73  List *pSrc){.  s
2e38e 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
2e38f 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53 72 63  em *pItem = pSrc
2e390 2d 3e 61 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  ->a;.  Table *pT
2e391 61 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49  ab;.  assert( pI
2e392 74 65 6d 20 26 26 20 70 53 72 63 2d 3e 6e 53 72  tem && pSrc->nSr
2e393 63 3d 3d 31 20 29 3b 0a 20 20 70 54 61 62 20 3d  c==1 );.  pTab =
2e394 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61   sqlite3LocateTa
2e395 62 6c 65 49 74 65 6d 28 70 50 61 72 73 65 2c 20  bleItem(pParse, 
2e396 30 2c 20 70 49 74 65 6d 29 3b 0a 20 20 73 71 6c  0, pItem);.  sql
2e397 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
2e398 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 74 65  pParse->db, pIte
2e399 6d 2d 3e 70 54 61 62 29 3b 0a 20 20 70 49 74 65  m->pTab);.  pIte
2e39a 6d 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a  m->pTab = pTab;.
2e39b 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20    if( pTab ){.  
2e39c 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a    pTab->nRef++;.
2e39d 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
2e39e 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70  3IndexedByLookup
2e39f 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 29 20  (pParse, pItem) 
2e3a0 29 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 30 3b  ){.    pTab = 0;
2e3a1 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 54  .  }.  return pT
2e3a2 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  ab;.}../*.** Che
2e3a3 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ck to make sure 
2e3a4 74 68 65 20 67 69 76 65 6e 20 74 61 62 6c 65 20  the given table 
2e3a5 69 73 20 77 72 69 74 61 62 6c 65 2e 20 20 49 66  is writable.  If
2e3a6 20 69 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 77 72   it is not.** wr
2e3a7 69 74 61 62 6c 65 2c 20 67 65 6e 65 72 61 74 65  itable, generate
2e3a8 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
2e3a9 65 20 61 6e 64 20 72 65 74 75 72 6e 20 31 2e 20  e and return 1. 
2e3aa 20 49 66 20 69 74 20 69 73 0a 2a 2a 20 77 72 69   If it is.** wri
2e3ab 74 61 62 6c 65 20 72 65 74 75 72 6e 20 30 3b 0a  table return 0;.
2e3ac 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2e3ad 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52  E int sqlite3IsR
2e3ae 65 61 64 4f 6e 6c 79 28 50 61 72 73 65 20 2a 70  eadOnly(Parse *p
2e3af 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54  Parse, Table *pT
2e3b0 61 62 2c 20 69 6e 74 20 76 69 65 77 4f 6b 29 7b  ab, int viewOk){
2e3b1 0a 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 73  .  /* A table is
2e3b2 20 6e 6f 74 20 77 72 69 74 61 62 6c 65 20 75 6e   not writable un
2e3b3 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  der the followin
2e3b4 67 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73 3a  g circumstances:
2e3b5 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 31 29 20  .  **.  **   1) 
2e3b6 49 74 20 69 73 20 61 20 76 69 72 74 75 61 6c 20  It is a virtual 
2e3b7 74 61 62 6c 65 20 61 6e 64 20 6e 6f 20 69 6d 70  table and no imp
2e3b8 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
2e3b9 68 65 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f  he xUpdate metho
2e3ba 64 0a 20 20 2a 2a 20 20 20 20 20 20 68 61 73 20  d.  **      has 
2e3bb 62 65 65 6e 20 70 72 6f 76 69 64 65 64 2c 20 6f  been provided, o
2e3bc 72 0a 20 20 2a 2a 20 20 20 32 29 20 49 74 20 69  r.  **   2) It i
2e3bd 73 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65  s a system table
2e3be 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61   (i.e. sqlite_ma
2e3bf 73 74 65 72 29 2c 20 74 68 69 73 20 63 61 6c 6c  ster), this call
2e3c0 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 20 20 20   is not.  **    
2e3c1 20 20 70 61 72 74 20 6f 66 20 61 20 6e 65 73 74    part of a nest
2e3c2 65 64 20 70 61 72 73 65 20 61 6e 64 20 77 72 69  ed parse and wri
2e3c3 74 61 62 6c 65 5f 73 63 68 65 6d 61 20 70 72 61  table_schema pra
2e3c4 67 6d 61 20 68 61 73 20 6e 6f 74 20 0a 20 20 2a  gma has not .  *
2e3c5 2a 20 20 20 20 20 20 62 65 65 6e 20 73 70 65 63  *      been spec
2e3c6 69 66 69 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  ified..  **.  **
2e3c7 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 20   In either case 
2e3c8 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d  leave an error m
2e3c9 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65  essage in pParse
2e3ca 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d   and return non-
2e3cb 7a 65 72 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  zero..  */.  if(
2e3cc 20 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61   ( IsVirtual(pTa
2e3cd 62 29 20 0a 20 20 20 20 20 26 26 20 73 71 6c 69  b) .     && sqli
2e3ce 74 65 33 47 65 74 56 54 61 62 6c 65 28 70 50 61  te3GetVTable(pPa
2e3cf 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 29 2d 3e  rse->db, pTab)->
2e3d0 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  pMod->pModule->x
2e3d1 55 70 64 61 74 65 3d 3d 30 20 29 0a 20 20 20 7c  Update==0 ).   |
2e3d2 7c 20 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c  | ( (pTab->tabFl
2e3d3 61 67 73 20 26 20 54 46 5f 52 65 61 64 6f 6e 6c  ags & TF_Readonl
2e3d4 79 29 21 3d 30 0a 20 20 20 20 20 26 26 20 28 70  y)!=0.     && (p
2e3d5 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73  Parse->db->flags
2e3d6 20 26 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53   & SQLITE_WriteS
2e3d7 63 68 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 26  chema)==0.     &
2e3d8 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  & pParse->nested
2e3d9 3d 3d 30 20 29 0a 20 20 29 7b 0a 20 20 20 20 73  ==0 ).  ){.    s
2e3da 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2e3db 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73  Parse, "table %s
2e3dc 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69   may not be modi
2e3dd 66 69 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  fied", pTab->zNa
2e3de 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  me);.    return 
2e3df 31 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  1;.  }..#ifndef 
2e3e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
2e3e1 0a 20 20 69 66 28 20 21 76 69 65 77 4f 6b 20 26  .  if( !viewOk &
2e3e2 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  & pTab->pSelect 
2e3e3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2e3e4 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 63  rorMsg(pParse,"c
2e3e5 61 6e 6e 6f 74 20 6d 6f 64 69 66 79 20 25 73 20  annot modify %s 
2e3e6 62 65 63 61 75 73 65 20 69 74 20 69 73 20 61 20  because it is a 
2e3e7 76 69 65 77 22 2c 70 54 61 62 2d 3e 7a 4e 61 6d  view",pTab->zNam
2e3e8 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  e);.    return 1
2e3e9 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  ;.  }.#endif.  r
2e3ea 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 23 69 66  eturn 0;.}...#if
2e3eb 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2e3ec 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21  _OMIT_VIEW) && !
2e3ed 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2e3ee 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 2f 2a 0a  MIT_TRIGGER)./*.
2e3ef 2a 2a 20 45 76 61 6c 75 61 74 65 20 61 20 76 69  ** Evaluate a vi
2e3f0 65 77 20 61 6e 64 20 73 74 6f 72 65 20 69 74 73  ew and store its
2e3f1 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 65 70   result in an ep
2e3f2 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 20 20  hemeral table.  
2e3f3 54 68 65 0a 2a 2a 20 70 57 68 65 72 65 20 61 72  The.** pWhere ar
2e3f4 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 6f 70 74  gument is an opt
2e3f5 69 6f 6e 61 6c 20 57 48 45 52 45 20 63 6c 61 75  ional WHERE clau
2e3f6 73 65 20 74 68 61 74 20 72 65 73 74 72 69 63 74  se that restrict
2e3f7 73 20 74 68 65 0a 2a 2a 20 73 65 74 20 6f 66 20  s the.** set of 
2e3f8 72 6f 77 73 20 69 6e 20 74 68 65 20 76 69 65 77  rows in the view
2e3f9 20 74 68 61 74 20 61 72 65 20 74 6f 20 62 65 20   that are to be 
2e3fa 61 64 64 65 64 20 74 6f 20 74 68 65 20 65 70 68  added to the eph
2e3fb 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f  emeral table..*/
2e3fc 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2e3fd 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65  void sqlite3Mate
2e3fe 72 69 61 6c 69 7a 65 56 69 65 77 28 0a 20 20 50  rializeView(.  P
2e3ff 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2e400 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2e401 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
2e402 65 20 2a 70 56 69 65 77 2c 20 20 20 20 20 20 20  e *pView,       
2e403 20 2f 2a 20 56 69 65 77 20 64 65 66 69 6e 69 74   /* View definit
2e404 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ion */.  Expr *p
2e405 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 2f 2a  Where,        /*
2e406 20 4f 70 74 69 6f 6e 61 6c 20 57 48 45 52 45 20   Optional WHERE 
2e407 63 6c 61 75 73 65 20 74 6f 20 62 65 20 61 64 64  clause to be add
2e408 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  ed */.  int iCur
2e409 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e40a 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f  Cursor number fo
2e40b 72 20 65 70 68 65 6d 65 72 69 61 6c 20 74 61 62  r ephemerial tab
2e40c 6c 65 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65 63  le */.){.  Selec
2e40d 74 44 65 73 74 20 64 65 73 74 3b 0a 20 20 53 65  tDest dest;.  Se
2e40e 6c 65 63 74 20 2a 70 44 75 70 3b 0a 20 20 73 71  lect *pDup;.  sq
2e40f 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2e410 73 65 2d 3e 64 62 3b 0a 0a 20 20 70 44 75 70 20  se->db;..  pDup 
2e411 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
2e412 75 70 28 64 62 2c 20 70 56 69 65 77 2d 3e 70 53  up(db, pView->pS
2e413 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 69 66 28  elect, 0);.  if(
2e414 20 70 57 68 65 72 65 20 29 7b 0a 20 20 20 20 53   pWhere ){.    S
2e415 72 63 4c 69 73 74 20 2a 70 46 72 6f 6d 3b 0a 20  rcList *pFrom;. 
2e416 20 20 20 0a 20 20 20 20 70 57 68 65 72 65 20 3d     .    pWhere =
2e417 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
2e418 64 62 2c 20 70 57 68 65 72 65 2c 20 30 29 3b 0a  db, pWhere, 0);.
2e419 20 20 20 20 70 46 72 6f 6d 20 3d 20 73 71 6c 69      pFrom = sqli
2e41a 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
2e41b 28 64 62 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  (db, 0, 0, 0);. 
2e41c 20 20 20 69 66 28 20 70 46 72 6f 6d 20 29 7b 0a     if( pFrom ){.
2e41d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46        assert( pF
2e41e 72 6f 6d 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a  rom->nSrc==1 );.
2e41f 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 61 5b 30        pFrom->a[0
2e420 5d 2e 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74  ].zAlias = sqlit
2e421 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
2e422 56 69 65 77 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  View->zName);.  
2e423 20 20 20 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e      pFrom->a[0].
2e424 70 53 65 6c 65 63 74 20 3d 20 70 44 75 70 3b 0a  pSelect = pDup;.
2e425 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46        assert( pF
2e426 72 6f 6d 2d 3e 61 5b 30 5d 2e 70 4f 6e 3d 3d 30  rom->a[0].pOn==0
2e427 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
2e428 28 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 70 55  ( pFrom->a[0].pU
2e429 73 69 6e 67 3d 3d 30 20 29 3b 0a 20 20 20 20 7d  sing==0 );.    }
2e42a 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
2e42b 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2e42c 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 7d  db, pDup);.    }
2e42d 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69  .    pDup = sqli
2e42e 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61  te3SelectNew(pPa
2e42f 72 73 65 2c 20 30 2c 20 70 46 72 6f 6d 2c 20 70  rse, 0, pFrom, p
2e430 57 68 65 72 65 2c 20 30 2c 20 30 2c 20 30 2c 20  Where, 0, 0, 0, 
2e431 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  0, 0, 0);.    if
2e432 28 20 70 44 75 70 20 29 20 70 44 75 70 2d 3e 73  ( pDup ) pDup->s
2e433 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 4d 61  elFlags |= SF_Ma
2e434 74 65 72 69 61 6c 69 7a 65 3b 0a 20 20 7d 0a 20  terialize;.  }. 
2e435 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2e436 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52  stInit(&dest, SR
2e437 54 5f 45 70 68 65 6d 54 61 62 2c 20 69 43 75 72  T_EphemTab, iCur
2e438 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  );.  sqlite3Sele
2e439 63 74 28 70 50 61 72 73 65 2c 20 70 44 75 70 2c  ct(pParse, pDup,
2e43a 20 26 64 65 73 74 29 3b 0a 20 20 73 71 6c 69 74   &dest);.  sqlit
2e43b 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
2e43c 62 2c 20 70 44 75 70 29 3b 0a 7d 0a 23 65 6e 64  b, pDup);.}.#end
2e43d 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
2e43e 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
2e43f 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
2e440 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
2e441 29 20 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65  ) */..#if define
2e442 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
2e443 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49  UPDATE_DELETE_LI
2e444 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64  MIT) && !defined
2e445 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42  (SQLITE_OMIT_SUB
2e446 51 55 45 52 59 29 0a 2f 2a 0a 2a 2a 20 47 65 6e  QUERY)./*.** Gen
2e447 65 72 61 74 65 20 61 6e 20 65 78 70 72 65 73 73  erate an express
2e448 69 6f 6e 20 74 72 65 65 20 74 6f 20 69 6d 70 6c  ion tree to impl
2e449 65 6d 65 6e 74 20 74 68 65 20 57 48 45 52 45 2c  ement the WHERE,
2e44a 20 4f 52 44 45 52 20 42 59 2c 0a 2a 2a 20 61 6e   ORDER BY,.** an
2e44b 64 20 4c 49 4d 49 54 2f 4f 46 46 53 45 54 20 70  d LIMIT/OFFSET p
2e44c 6f 72 74 69 6f 6e 20 6f 66 20 44 45 4c 45 54 45  ortion of DELETE
2e44d 20 61 6e 64 20 55 50 44 41 54 45 20 73 74 61 74   and UPDATE stat
2e44e 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 20 20  ements..**.**   
2e44f 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61    DELETE FROM ta
2e450 62 6c 65 5f 77 78 79 7a 20 57 48 45 52 45 20 61  ble_wxyz WHERE a
2e451 3c 35 20 4f 52 44 45 52 20 42 59 20 61 20 4c 49  <5 ORDER BY a LI
2e452 4d 49 54 20 31 3b 0a 2a 2a 20 20 20 20 20 20 20  MIT 1;.**       
2e453 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e454 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f       \__________
2e455 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2e456 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  /.**            
2e457 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e458 20 20 20 70 4c 69 6d 69 74 57 68 65 72 65 20 28     pLimitWhere (
2e459 70 49 6e 43 6c 61 75 73 65 29 0a 2a 2f 0a 53 51  pInClause).*/.SQ
2e45a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
2e45b 72 20 2a 73 71 6c 69 74 65 33 4c 69 6d 69 74 57  r *sqlite3LimitW
2e45c 68 65 72 65 28 0a 20 20 50 61 72 73 65 20 2a 70  here(.  Parse *p
2e45d 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
2e45e 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
2e45f 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
2e460 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20  SrcList *pSrc,  
2e461 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e462 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2e463 2d 2d 20 77 68 69 63 68 20 74 61 62 6c 65 73 20  -- which tables 
2e464 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70  to scan */.  Exp
2e465 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20  r *pWhere,      
2e466 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2e467 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20   WHERE clause.  
2e468 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20  May be null */. 
2e469 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
2e46a 72 42 79 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  rBy,          /*
2e46b 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   The ORDER BY cl
2e46c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75  ause.  May be nu
2e46d 6c 6c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c  ll */.  Expr *pL
2e46e 69 6d 69 74 2c 20 20 20 20 20 20 20 20 20 20 20  imit,           
2e46f 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49       /* The LIMI
2e470 54 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62  T clause.  May b
2e471 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78 70 72  e null */.  Expr
2e472 20 2a 70 4f 66 66 73 65 74 2c 20 20 20 20 20 20   *pOffset,      
2e473 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2e474 4f 46 46 53 45 54 20 63 6c 61 75 73 65 2e 20 20  OFFSET clause.  
2e475 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20  May be null */. 
2e476 20 63 68 61 72 20 2a 7a 53 74 6d 74 54 79 70 65   char *zStmtType
2e477 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e478 20 45 69 74 68 65 72 20 44 45 4c 45 54 45 20 6f   Either DELETE o
2e479 72 20 55 50 44 41 54 45 2e 20 20 46 6f 72 20 65  r UPDATE.  For e
2e47a 72 72 6f 72 20 6d 65 73 73 61 67 65 73 2e 20 2a  rror messages. *
2e47b 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 57 68  /.){.  Expr *pWh
2e47c 65 72 65 52 6f 77 69 64 20 3d 20 4e 55 4c 4c 3b  ereRowid = NULL;
2e47d 20 20 20 20 2f 2a 20 57 48 45 52 45 20 72 6f 77      /* WHERE row
2e47e 69 64 20 2e 2e 20 2a 2f 0a 20 20 45 78 70 72 20  id .. */.  Expr 
2e47f 2a 70 49 6e 43 6c 61 75 73 65 20 3d 20 4e 55 4c  *pInClause = NUL
2e480 4c 3b 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45  L;      /* WHERE
2e481 20 72 6f 77 69 64 20 49 4e 20 28 20 73 65 6c 65   rowid IN ( sele
2e482 63 74 20 29 20 2a 2f 0a 20 20 45 78 70 72 20 2a  ct ) */.  Expr *
2e483 70 53 65 6c 65 63 74 52 6f 77 69 64 20 3d 20 4e  pSelectRowid = N
2e484 55 4c 4c 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54  ULL;   /* SELECT
2e485 20 72 6f 77 69 64 20 2e 2e 2e 20 2a 2f 0a 20 20   rowid ... */.  
2e486 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
2e487 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 2f 2a 20   = NULL;     /* 
2e488 45 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20  Expression list 
2e489 63 6f 6e 74 61 6e 69 6e 67 20 6f 6e 6c 79 20 70  contaning only p
2e48a 53 65 6c 65 63 74 52 6f 77 69 64 20 2a 2f 0a 20  SelectRowid */. 
2e48b 20 53 72 63 4c 69 73 74 20 2a 70 53 65 6c 65 63   SrcList *pSelec
2e48c 74 53 72 63 20 3d 20 4e 55 4c 4c 3b 20 20 2f 2a  tSrc = NULL;  /*
2e48d 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20 46 52   SELECT rowid FR
2e48e 4f 4d 20 78 20 2e 2e 2e 20 28 64 75 70 20 6f 66  OM x ... (dup of
2e48f 20 70 53 72 63 29 20 2a 2f 0a 20 20 53 65 6c 65   pSrc) */.  Sele
2e490 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 4e 55  ct *pSelect = NU
2e491 4c 4c 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70  LL;      /* Comp
2e492 6c 65 74 65 20 53 45 4c 45 43 54 20 74 72 65 65  lete SELECT tree
2e493 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20   */..  /* Check 
2e494 74 68 61 74 20 74 68 65 72 65 20 69 73 6e 27 74  that there isn't
2e495 20 61 6e 20 4f 52 44 45 52 20 42 59 20 77 69 74   an ORDER BY wit
2e496 68 6f 75 74 20 61 20 4c 49 4d 49 54 20 63 6c 61  hout a LIMIT cla
2e497 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  use..  */.  if( 
2e498 70 4f 72 64 65 72 42 79 20 26 26 20 28 70 4c 69  pOrderBy && (pLi
2e499 6d 69 74 20 3d 3d 20 30 29 20 29 20 7b 0a 20 20  mit == 0) ) {.  
2e49a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2e49b 67 28 70 50 61 72 73 65 2c 20 22 4f 52 44 45 52  g(pParse, "ORDER
2e49c 20 42 59 20 77 69 74 68 6f 75 74 20 4c 49 4d 49   BY without LIMI
2e49d 54 20 6f 6e 20 25 73 22 2c 20 7a 53 74 6d 74 54  T on %s", zStmtT
2e49e 79 70 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6c  ype);.    goto l
2e49f 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e  imit_where_clean
2e4a0 75 70 5f 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  up_2;.  }..  /* 
2e4a1 57 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74 6f 20  We only need to 
2e4a2 67 65 6e 65 72 61 74 65 20 61 20 73 65 6c 65 63  generate a selec
2e4a3 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 66 20  t expression if 
2e4a4 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20  there.  ** is a 
2e4a5 6c 69 6d 69 74 2f 6f 66 66 73 65 74 20 74 65 72  limit/offset ter
2e4a6 6d 20 74 6f 20 65 6e 66 6f 72 63 65 2e 0a 20 20  m to enforce..  
2e4a7 2a 2f 0a 20 20 69 66 28 20 70 4c 69 6d 69 74 20  */.  if( pLimit 
2e4a8 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20 2f 2a 20  == 0 ) {.    /* 
2e4a9 69 66 20 70 4c 69 6d 69 74 20 69 73 20 6e 75 6c  if pLimit is nul
2e4aa 6c 2c 20 70 4f 66 66 73 65 74 20 77 69 6c 6c 20  l, pOffset will 
2e4ab 61 6c 77 61 79 73 20 62 65 20 6e 75 6c 6c 20 61  always be null a
2e4ac 73 20 77 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 61  s well. */.    a
2e4ad 73 73 65 72 74 28 20 70 4f 66 66 73 65 74 20 3d  ssert( pOffset =
2e4ae 3d 20 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72  = 0 );.    retur
2e4af 6e 20 70 57 68 65 72 65 3b 0a 20 20 7d 0a 0a 20  n pWhere;.  }.. 
2e4b0 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73   /* Generate a s
2e4b1 65 6c 65 63 74 20 65 78 70 72 65 73 73 69 6f 6e  elect expression
2e4b2 20 74 72 65 65 20 74 6f 20 65 6e 66 6f 72 63 65   tree to enforce
2e4b3 20 74 68 65 20 6c 69 6d 69 74 2f 6f 66 66 73 65   the limit/offse
2e4b4 74 20 0a 20 20 2a 2a 20 74 65 72 6d 20 66 6f 72  t .  ** term for
2e4b5 20 74 68 65 20 44 45 4c 45 54 45 20 6f 72 20 55   the DELETE or U
2e4b6 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e  PDATE statement.
2e4b7 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20    For example:. 
2e4b8 20 2a 2a 20 20 20 44 45 4c 45 54 45 20 46 52 4f   **   DELETE FRO
2e4b9 4d 20 74 61 62 6c 65 5f 61 20 57 48 45 52 45 20  M table_a WHERE 
2e4ba 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20 42 59 20  col1=1 ORDER BY 
2e4bb 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20 4f 46 46  col2 LIMIT 1 OFF
2e4bc 53 45 54 20 31 0a 20 20 2a 2a 20 62 65 63 6f 6d  SET 1.  ** becom
2e4bd 65 73 3a 0a 20 20 2a 2a 20 20 20 44 45 4c 45 54  es:.  **   DELET
2e4be 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57  E FROM table_a W
2e4bf 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20 28 20  HERE rowid IN ( 
2e4c0 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  .  **     SELECT
2e4c1 20 72 6f 77 69 64 20 46 52 4f 4d 20 74 61 62 6c   rowid FROM tabl
2e4c2 65 5f 61 20 57 48 45 52 45 20 63 6f 6c 31 3d 31  e_a WHERE col1=1
2e4c3 20 4f 52 44 45 52 20 42 59 20 63 6f 6c 32 20 4c   ORDER BY col2 L
2e4c4 49 4d 49 54 20 31 20 4f 46 46 53 45 54 20 31 0a  IMIT 1 OFFSET 1.
2e4c5 20 20 2a 2a 20 20 20 29 3b 0a 20 20 2a 2f 0a 0a    **   );.  */..
2e4c6 20 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20 3d    pSelectRowid =
2e4c7 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2e4c8 61 72 73 65 2c 20 54 4b 5f 52 4f 57 2c 20 30 2c  arse, TK_ROW, 0,
2e4c9 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53   0, 0);.  if( pS
2e4ca 65 6c 65 63 74 52 6f 77 69 64 20 3d 3d 20 30 20  electRowid == 0 
2e4cb 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65  ) goto limit_whe
2e4cc 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20  re_cleanup_2;.  
2e4cd 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33  pEList = sqlite3
2e4ce 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
2e4cf 50 61 72 73 65 2c 20 30 2c 20 70 53 65 6c 65 63  Parse, 0, pSelec
2e4d0 74 52 6f 77 69 64 29 3b 0a 20 20 69 66 28 20 70  tRowid);.  if( p
2e4d1 45 4c 69 73 74 20 3d 3d 20 30 20 29 20 67 6f 74  EList == 0 ) got
2e4d2 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c  o limit_where_cl
2e4d3 65 61 6e 75 70 5f 32 3b 0a 0a 20 20 2f 2a 20 64  eanup_2;..  /* d
2e4d4 75 70 6c 69 63 61 74 65 20 74 68 65 20 46 52 4f  uplicate the FRO
2e4d5 4d 20 63 6c 61 75 73 65 20 61 73 20 69 74 20 69  M clause as it i
2e4d6 73 20 6e 65 65 64 65 64 20 62 79 20 62 6f 74 68  s needed by both
2e4d7 20 74 68 65 20 44 45 4c 45 54 45 2f 55 50 44 41   the DELETE/UPDA
2e4d8 54 45 20 74 72 65 65 0a 20 20 2a 2a 20 61 6e 64  TE tree.  ** and
2e4d9 20 74 68 65 20 53 45 4c 45 43 54 20 73 75 62 74   the SELECT subt
2e4da 72 65 65 2e 20 2a 2f 0a 20 20 70 53 65 6c 65 63  ree. */.  pSelec
2e4db 74 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72  tSrc = sqlite3Sr
2e4dc 63 4c 69 73 74 44 75 70 28 70 50 61 72 73 65 2d  cListDup(pParse-
2e4dd 3e 64 62 2c 20 70 53 72 63 2c 20 30 29 3b 0a 20  >db, pSrc, 0);. 
2e4de 20 69 66 28 20 70 53 65 6c 65 63 74 53 72 63 20   if( pSelectSrc 
2e4df 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20 73 71 6c  == 0 ) {.    sql
2e4e0 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2e4e1 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
2e4e2 45 4c 69 73 74 29 3b 0a 20 20 20 20 67 6f 74 6f  EList);.    goto
2e4e3 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65   limit_where_cle
2e4e4 61 6e 75 70 5f 32 3b 0a 20 20 7d 0a 0a 20 20 2f  anup_2;.  }..  /
2e4e5 2a 20 67 65 6e 65 72 61 74 65 20 74 68 65 20 53  * generate the S
2e4e6 45 4c 45 43 54 20 65 78 70 72 65 73 73 69 6f 6e  ELECT expression
2e4e7 20 74 72 65 65 2e 20 2a 2f 0a 20 20 70 53 65 6c   tree. */.  pSel
2e4e8 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ect = sqlite3Sel
2e4e9 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c 70 45  ectNew(pParse,pE
2e4ea 4c 69 73 74 2c 70 53 65 6c 65 63 74 53 72 63 2c  List,pSelectSrc,
2e4eb 70 57 68 65 72 65 2c 30 2c 30 2c 0a 20 20 20 20  pWhere,0,0,.    
2e4ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e4ed 20 20 20 20 20 20 20 20 20 70 4f 72 64 65 72 42           pOrderB
2e4ee 79 2c 30 2c 70 4c 69 6d 69 74 2c 70 4f 66 66 73  y,0,pLimit,pOffs
2e4ef 65 74 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 65  et);.  if( pSele
2e4f0 63 74 20 3d 3d 20 30 20 29 20 72 65 74 75 72 6e  ct == 0 ) return
2e4f1 20 30 3b 0a 0a 20 20 2f 2a 20 6e 6f 77 20 67 65   0;..  /* now ge
2e4f2 6e 65 72 61 74 65 20 74 68 65 20 6e 65 77 20 57  nerate the new W
2e4f3 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20 63 6c  HERE rowid IN cl
2e4f4 61 75 73 65 20 66 6f 72 20 74 68 65 20 44 45 4c  ause for the DEL
2e4f5 45 54 45 2f 55 44 50 41 54 45 20 2a 2f 0a 20 20  ETE/UDPATE */.  
2e4f6 70 57 68 65 72 65 52 6f 77 69 64 20 3d 20 73 71  pWhereRowid = sq
2e4f7 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2e4f8 65 2c 20 54 4b 5f 52 4f 57 2c 20 30 2c 20 30 2c  e, TK_ROW, 0, 0,
2e4f9 20 30 29 3b 0a 20 20 69 66 28 20 70 57 68 65 72   0);.  if( pWher
2e4fa 65 52 6f 77 69 64 20 3d 3d 20 30 20 29 20 67 6f  eRowid == 0 ) go
2e4fb 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63  to limit_where_c
2e4fc 6c 65 61 6e 75 70 5f 31 3b 0a 20 20 70 49 6e 43  leanup_1;.  pInC
2e4fd 6c 61 75 73 65 20 3d 20 73 71 6c 69 74 65 33 50  lause = sqlite3P
2e4fe 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2e4ff 49 4e 2c 20 70 57 68 65 72 65 52 6f 77 69 64 2c  IN, pWhereRowid,
2e500 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 49   0, 0);.  if( pI
2e501 6e 43 6c 61 75 73 65 20 3d 3d 20 30 20 29 20 67  nClause == 0 ) g
2e502 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f  oto limit_where_
2e503 63 6c 65 61 6e 75 70 5f 31 3b 0a 0a 20 20 70 49  cleanup_1;..  pI
2e504 6e 43 6c 61 75 73 65 2d 3e 78 2e 70 53 65 6c 65  nClause->x.pSele
2e505 63 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20  ct = pSelect;.  
2e506 70 49 6e 43 6c 61 75 73 65 2d 3e 66 6c 61 67 73  pInClause->flags
2e507 20 7c 3d 20 45 50 5f 78 49 73 53 65 6c 65 63 74   |= EP_xIsSelect
2e508 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53  ;.  sqlite3ExprS
2e509 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c  etHeight(pParse,
2e50a 20 70 49 6e 43 6c 61 75 73 65 29 3b 0a 20 20 72   pInClause);.  r
2e50b 65 74 75 72 6e 20 70 49 6e 43 6c 61 75 73 65 3b  eturn pInClause;
2e50c 0a 0a 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67  ..  /* something
2e50d 20 77 65 6e 74 20 77 72 6f 6e 67 2e 20 63 6c 65   went wrong. cle
2e50e 61 6e 20 75 70 20 61 6e 79 74 68 69 6e 67 20 61  an up anything a
2e50f 6c 6c 6f 63 61 74 65 64 2e 20 2a 2f 0a 6c 69 6d  llocated. */.lim
2e510 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
2e511 5f 31 3a 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  _1:.  sqlite3Sel
2e512 65 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  ectDelete(pParse
2e513 2d 3e 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a  ->db, pSelect);.
2e514 20 20 72 65 74 75 72 6e 20 30 3b 0a 0a 6c 69 6d    return 0;..lim
2e515 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
2e516 5f 32 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70  _2:.  sqlite3Exp
2e517 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  rDelete(pParse->
2e518 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 73  db, pWhere);.  s
2e519 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
2e51a 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
2e51b 20 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 73 71   pOrderBy);.  sq
2e51c 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2e51d 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 69 6d  pParse->db, pLim
2e51e 69 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  it);.  sqlite3Ex
2e51f 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  prDelete(pParse-
2e520 3e 64 62 2c 20 70 4f 66 66 73 65 74 29 3b 0a 20  >db, pOffset);. 
2e521 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e   return 0;.}.#en
2e522 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53  dif /* defined(S
2e523 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44  QLITE_ENABLE_UPD
2e524 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54  ATE_DELETE_LIMIT
2e525 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
2e526 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
2e527 52 59 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65  RY) */../*.** Ge
2e528 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
2e529 61 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 73 74  a DELETE FROM st
2e52a 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20  atement..**.**  
2e52b 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74     DELETE FROM t
2e52c 61 62 6c 65 5f 77 78 79 7a 20 57 48 45 52 45 20  able_wxyz WHERE 
2e52d 61 3c 35 20 41 4e 44 20 62 20 4e 4f 54 20 4e 55  a<5 AND b NOT NU
2e52e 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  LL;.**          
2e52f 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f         \________
2e530 2f 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f  /       \_______
2e531 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20  _________/.**   
2e532 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2e533 54 61 62 4c 69 73 74 20 20 20 20 20 20 20 20 20  TabList         
2e534 20 20 20 20 20 70 57 68 65 72 65 0a 2a 2f 0a 53       pWhere.*/.S
2e535 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2e536 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  id sqlite3Delete
2e537 46 72 6f 6d 28 0a 20 20 50 61 72 73 65 20 2a 70  From(.  Parse *p
2e538 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
2e539 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
2e53a 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73  text */.  SrcLis
2e53b 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20  t *pTabList,    
2e53c 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 66 72   /* The table fr
2e53d 6f 6d 20 77 68 69 63 68 20 77 65 20 73 68 6f 75  om which we shou
2e53e 6c 64 20 64 65 6c 65 74 65 20 74 68 69 6e 67 73  ld delete things
2e53f 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65   */.  Expr *pWhe
2e540 72 65 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  re           /* 
2e541 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
2e542 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a  .  May be null *
2e543 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 20  /.){.  Vdbe *v; 
2e544 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e545 20 54 68 65 20 76 69 72 74 75 61 6c 20 64 61 74   The virtual dat
2e546 61 62 61 73 65 20 65 6e 67 69 6e 65 20 2a 2f 0a  abase engine */.
2e547 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
2e548 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2e549 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68  table from which
2e54a 20 72 65 63 6f 72 64 73 20 77 69 6c 6c 20 62 65   records will be
2e54b 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 63 6f   deleted */.  co
2e54c 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  nst char *zDb;  
2e54d 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2e54e 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67  database holding
2e54f 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 65   pTab */.  int e
2e550 6e 64 2c 20 61 64 64 72 20 3d 20 30 3b 20 20 20  nd, addr = 0;   
2e551 20 20 2f 2a 20 41 20 63 6f 75 70 6c 65 20 61 64    /* A couple ad
2e552 64 72 65 73 73 65 73 20 6f 66 20 67 65 6e 65 72  dresses of gener
2e553 61 74 65 64 20 63 6f 64 65 20 2a 2f 0a 20 20 69  ated code */.  i
2e554 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
2e555 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2e556 75 6e 74 65 72 20 2a 2f 0a 20 20 57 68 65 72 65  unter */.  Where
2e557 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20  Info *pWInfo;   
2e558 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
2e559 20 61 62 6f 75 74 20 74 68 65 20 57 48 45 52 45   about the WHERE
2e55a 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 49 6e 64   clause */.  Ind
2e55b 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
2e55c 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
2e55d 6e 67 20 6f 76 65 72 20 69 6e 64 69 63 65 73 20  ng over indices 
2e55e 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
2e55f 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20    int iCur;     
2e560 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45           /* VDBE
2e561 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
2e562 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20 73 71 6c  or pTab */.  sql
2e563 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
2e564 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64 61 74 61      /* Main data
2e565 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20 2a  base structure *
2e566 2f 0a 20 20 41 75 74 68 43 6f 6e 74 65 78 74 20  /.  AuthContext 
2e567 73 43 6f 6e 74 65 78 74 3b 20 20 2f 2a 20 41 75  sContext;  /* Au
2e568 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74  thorization cont
2e569 65 78 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e  ext */.  NameCon
2e56a 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20  text sNC;       
2e56b 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20  /* Name context 
2e56c 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65  to resolve expre
2e56d 73 73 69 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20 69  ssions in */.  i
2e56e 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
2e56f 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2e570 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e  e number */.  in
2e571 74 20 6d 65 6d 43 6e 74 20 3d 20 2d 31 3b 20 20  t memCnt = -1;  
2e572 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
2e573 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 63 68 61  ell used for cha
2e574 6e 67 65 20 63 6f 75 6e 74 69 6e 67 20 2a 2f 0a  nge counting */.
2e575 20 20 69 6e 74 20 72 63 61 75 74 68 3b 20 20 20    int rcauth;   
2e576 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
2e577 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 75  e returned by au
2e578 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c  thorization call
2e579 62 61 63 6b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  back */..#ifndef
2e57a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
2e57b 47 47 45 52 0a 20 20 69 6e 74 20 69 73 56 69 65  GGER.  int isVie
2e57c 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
2e57d 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
2e57e 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 64 65 6c  ttempting to del
2e57f 65 74 65 20 66 72 6f 6d 20 61 20 76 69 65 77 20  ete from a view 
2e580 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  */.  Trigger *pT
2e581 72 69 67 67 65 72 3b 20 20 20 20 20 20 20 20 20  rigger;         
2e582 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62    /* List of tab
2e583 6c 65 20 74 72 69 67 67 65 72 73 2c 20 69 66 20  le triggers, if 
2e584 72 65 71 75 69 72 65 64 20 2a 2f 0a 23 65 6e 64  required */.#end
2e585 69 66 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73 43  if..  memset(&sC
2e586 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f  ontext, 0, sizeo
2e587 66 28 73 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20  f(sContext));.  
2e588 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2e589 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
2e58a 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
2e58b 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
2e58c 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
2e58d 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61  cleanup;.  }.  a
2e58e 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d  ssert( pTabList-
2e58f 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 0a 20 20 2f  >nSrc==1 );..  /
2e590 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62  * Locate the tab
2e591 6c 65 20 77 68 69 63 68 20 77 65 20 77 61 6e 74  le which we want
2e592 20 74 6f 20 64 65 6c 65 74 65 2e 20 20 54 68 69   to delete.  Thi
2e593 73 20 74 61 62 6c 65 20 68 61 73 20 74 6f 20 62  s table has to b
2e594 65 0a 20 20 2a 2a 20 70 75 74 20 69 6e 20 61 6e  e.  ** put in an
2e595 20 53 72 63 4c 69 73 74 20 73 74 72 75 63 74 75   SrcList structu
2e596 72 65 20 62 65 63 61 75 73 65 20 73 6f 6d 65 20  re because some 
2e597 6f 66 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e  of the subroutin
2e598 65 73 20 77 65 0a 20 20 2a 2a 20 77 69 6c 6c 20  es we.  ** will 
2e599 62 65 20 63 61 6c 6c 69 6e 67 20 61 72 65 20 64  be calling are d
2e59a 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b 20  esigned to work 
2e59b 77 69 74 68 20 6d 75 6c 74 69 70 6c 65 20 74 61  with multiple ta
2e59c 62 6c 65 73 20 61 6e 64 20 65 78 70 65 63 74 0a  bles and expect.
2e59d 20 20 2a 2a 20 61 6e 20 53 72 63 4c 69 73 74 2a    ** an SrcList*
2e59e 20 70 61 72 61 6d 65 74 65 72 20 69 6e 73 74 65   parameter inste
2e59f 61 64 20 6f 66 20 6a 75 73 74 20 61 20 54 61 62  ad of just a Tab
2e5a0 6c 65 2a 20 70 61 72 61 6d 65 74 65 72 2e 0a 20  le* parameter.. 
2e5a1 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c   */.  pTab = sql
2e5a2 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
2e5a3 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  p(pParse, pTabLi
2e5a4 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d  st);.  if( pTab=
2e5a5 3d 30 20 29 20 20 67 6f 74 6f 20 64 65 6c 65 74  =0 )  goto delet
2e5a6 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a  e_from_cleanup;.
2e5a7 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
2e5a8 20 69 66 20 77 65 20 68 61 76 65 20 61 6e 79 20   if we have any 
2e5a9 74 72 69 67 67 65 72 73 20 61 6e 64 20 69 66 20  triggers and if 
2e5aa 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 0a  the table being.
2e5ab 20 20 2a 2a 20 64 65 6c 65 74 65 64 20 66 72 6f    ** deleted fro
2e5ac 6d 20 69 73 20 61 20 76 69 65 77 0a 20 20 2a 2f  m is a view.  */
2e5ad 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2e5ae 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 70  OMIT_TRIGGER.  p
2e5af 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65  Trigger = sqlite
2e5b0 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 70  3TriggersExist(p
2e5b1 50 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f  Parse, pTab, TK_
2e5b2 44 45 4c 45 54 45 2c 20 30 2c 20 30 29 3b 0a 20  DELETE, 0, 0);. 
2e5b3 20 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e   isView = pTab->
2e5b4 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73  pSelect!=0;.#els
2e5b5 65 0a 23 20 64 65 66 69 6e 65 20 70 54 72 69 67  e.# define pTrig
2e5b6 67 65 72 20 30 0a 23 20 64 65 66 69 6e 65 20 69  ger 0.# define i
2e5b7 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 23  sView 0.#endif.#
2e5b8 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
2e5b9 54 5f 56 49 45 57 0a 23 20 75 6e 64 65 66 20 69  T_VIEW.# undef i
2e5ba 73 56 69 65 77 0a 23 20 64 65 66 69 6e 65 20 69  sView.# define i
2e5bb 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 0a  sView 0.#endif..
2e5bc 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73 20    /* If pTab is 
2e5bd 72 65 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 6d  really a view, m
2e5be 61 6b 65 20 73 75 72 65 20 69 74 20 68 61 73 20  ake sure it has 
2e5bf 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
2e5c0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
2e5c1 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
2e5c2 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70  nNames(pParse, p
2e5c3 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Tab) ){.    goto
2e5c4 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65   delete_from_cle
2e5c5 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  anup;.  }..  if(
2e5c6 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e   sqlite3IsReadOn
2e5c7 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ly(pParse, pTab,
2e5c8 20 28 70 54 72 69 67 67 65 72 3f 31 3a 30 29 29   (pTrigger?1:0))
2e5c9 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c   ){.    goto del
2e5ca 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70  ete_from_cleanup
2e5cb 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71  ;.  }.  iDb = sq
2e5cc 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
2e5cd 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  ex(db, pTab->pSc
2e5ce 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28  hema);.  assert(
2e5cf 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
2e5d0 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b    zDb = db->aDb[
2e5d1 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63  iDb].zName;.  rc
2e5d2 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33 41 75  auth = sqlite3Au
2e5d3 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
2e5d4 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 70  SQLITE_DELETE, p
2e5d5 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a  Tab->zName, 0, z
2e5d6 44 62 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  Db);.  assert( r
2e5d7 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 4f 4b  cauth==SQLITE_OK
2e5d8 20 7c 7c 20 72 63 61 75 74 68 3d 3d 53 51 4c 49   || rcauth==SQLI
2e5d9 54 45 5f 44 45 4e 59 20 7c 7c 20 72 63 61 75 74  TE_DENY || rcaut
2e5da 68 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  h==SQLITE_IGNORE
2e5db 20 29 3b 0a 20 20 69 66 28 20 72 63 61 75 74 68   );.  if( rcauth
2e5dc 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b  ==SQLITE_DENY ){
2e5dd 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65  .    goto delete
2e5de 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
2e5df 20 7d 0a 20 20 61 73 73 65 72 74 28 21 69 73 56   }.  assert(!isV
2e5e0 69 65 77 20 7c 7c 20 70 54 72 69 67 67 65 72 29  iew || pTrigger)
2e5e1 3b 0a 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20 20  ;..  /* Assign  
2e5e2 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 6f  cursor number to
2e5e3 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 61   the table and a
2e5e4 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73 2e 0a  ll its indices..
2e5e5 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
2e5e6 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31  TabList->nSrc==1
2e5e7 20 29 3b 0a 20 20 69 43 75 72 20 3d 20 70 54 61   );.  iCur = pTa
2e5e8 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75 72  bList->a[0].iCur
2e5e9 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  sor = pParse->nT
2e5ea 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 64 78  ab++;.  for(pIdx
2e5eb 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
2e5ec 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
2e5ed 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 50 61 72  pNext){.    pPar
2e5ee 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a  se->nTab++;.  }.
2e5ef 0a 20 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20  .  /* Start the 
2e5f0 76 69 65 77 20 63 6f 6e 74 65 78 74 0a 20 20 2a  view context.  *
2e5f1 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29  /.  if( isView )
2e5f2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74  {.    sqlite3Aut
2e5f3 68 43 6f 6e 74 65 78 74 50 75 73 68 28 70 50 61  hContextPush(pPa
2e5f4 72 73 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20  rse, &sContext, 
2e5f5 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
2e5f6 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65  }..  /* Begin ge
2e5f7 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a 20  nerating code.. 
2e5f8 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   */.  v = sqlite
2e5f9 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
2e5fa 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a  ;.  if( v==0 ){.
2e5fb 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f      goto delete_
2e5fc 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  from_cleanup;.  
2e5fd 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  }.  if( pParse->
2e5fe 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69  nested==0 ) sqli
2e5ff 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e  te3VdbeCountChan
2e600 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65  ges(v);.  sqlite
2e601 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
2e602 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20  tion(pParse, 1, 
2e603 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77  iDb);..  /* If w
2e604 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20  e are trying to 
2e605 64 65 6c 65 74 65 20 66 72 6f 6d 20 61 20 76 69  delete from a vi
2e606 65 77 2c 20 72 65 61 6c 69 7a 65 20 74 68 61 74  ew, realize that
2e607 20 76 69 65 77 20 69 6e 74 6f 0a 20 20 2a 2a 20   view into.  ** 
2e608 61 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  a ephemeral tabl
2e609 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65 66  e..  */.#if !def
2e60a 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2e60b 5f 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e  _VIEW) && !defin
2e60c 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ed(SQLITE_OMIT_T
2e60d 52 49 47 47 45 52 29 0a 20 20 69 66 28 20 69 73  RIGGER).  if( is
2e60e 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69  View ){.    sqli
2e60f 74 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69  te3MaterializeVi
2e610 65 77 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ew(pParse, pTab,
2e611 20 70 57 68 65 72 65 2c 20 69 43 75 72 29 3b 0a   pWhere, iCur);.
2e612 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
2e613 20 52 65 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c   Resolve the col
2e614 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 74 68 65  umn names in the
2e615 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 20   WHERE clause.. 
2e616 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e   */.  memset(&sN
2e617 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
2e618 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65  ));.  sNC.pParse
2e619 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 4e 43   = pParse;.  sNC
2e61a 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62  .pSrcList = pTab
2e61b 4c 69 73 74 3b 0a 20 20 69 66 28 20 73 71 6c 69  List;.  if( sqli
2e61c 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
2e61d 6d 65 73 28 26 73 4e 43 2c 20 70 57 68 65 72 65  mes(&sNC, pWhere
2e61e 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65  ) ){.    goto de
2e61f 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75  lete_from_cleanu
2e620 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69  p;.  }..  /* Ini
2e621 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 75 6e  tialize the coun
2e622 74 65 72 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ter of the numbe
2e623 72 20 6f 66 20 72 6f 77 73 20 64 65 6c 65 74 65  r of rows delete
2e624 64 2c 20 69 66 0a 20 20 2a 2a 20 77 65 20 61 72  d, if.  ** we ar
2e625 65 20 63 6f 75 6e 74 69 6e 67 20 72 6f 77 73 2e  e counting rows.
2e626 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
2e627 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43  flags & SQLITE_C
2e628 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20 20 20  ountRows ){.    
2e629 6d 65 6d 43 6e 74 20 3d 20 2b 2b 70 50 61 72 73  memCnt = ++pPars
2e62a 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
2e62b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2e62c 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
2e62d 20 6d 65 6d 43 6e 74 29 3b 0a 20 20 7d 0a 0a 23   memCnt);.  }..#
2e62e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2e62f 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49  IT_TRUNCATE_OPTI
2e630 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 53 70  MIZATION.  /* Sp
2e631 65 63 69 61 6c 20 63 61 73 65 3a 20 41 20 44 45  ecial case: A DE
2e632 4c 45 54 45 20 77 69 74 68 6f 75 74 20 61 20 57  LETE without a W
2e633 48 45 52 45 20 63 6c 61 75 73 65 20 64 65 6c 65  HERE clause dele
2e634 74 65 73 20 65 76 65 72 79 74 68 69 6e 67 2e 0a  tes everything..
2e635 20 20 2a 2a 20 49 74 20 69 73 20 65 61 73 69 65    ** It is easie
2e636 72 20 6a 75 73 74 20 74 6f 20 65 72 61 73 65 20  r just to erase 
2e637 74 68 65 20 77 68 6f 6c 65 20 74 61 62 6c 65 2e  the whole table.
2e638 20 50 72 69 6f 72 20 74 6f 20 76 65 72 73 69 6f   Prior to versio
2e639 6e 20 33 2e 36 2e 35 2c 0a 20 20 2a 2a 20 74 68  n 3.6.5,.  ** th
2e63a 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  is optimization 
2e63b 63 61 75 73 65 64 20 74 68 65 20 72 6f 77 20 63  caused the row c
2e63c 68 61 6e 67 65 20 63 6f 75 6e 74 20 28 74 68 65  hange count (the
2e63d 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
2e63e 62 79 20 0a 20 20 2a 2a 20 41 50 49 20 66 75 6e  by .  ** API fun
2e63f 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 5f 63 6f  ction sqlite3_co
2e640 75 6e 74 5f 63 68 61 6e 67 65 73 29 20 74 6f 20  unt_changes) to 
2e641 62 65 20 73 65 74 20 69 6e 63 6f 72 72 65 63 74  be set incorrect
2e642 6c 79 2e 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  ly.  */.  if( rc
2e643 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  auth==SQLITE_OK 
2e644 26 26 20 70 57 68 65 72 65 3d 3d 30 20 26 26 20  && pWhere==0 && 
2e645 21 70 54 72 69 67 67 65 72 20 26 26 20 21 49 73  !pTrigger && !Is
2e646 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 0a 20  Virtual(pTab) . 
2e647 20 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 46    && 0==sqlite3F
2e648 6b 52 65 71 75 69 72 65 64 28 70 50 61 72 73 65  kRequired(pParse
2e649 2c 20 70 54 61 62 2c 20 30 2c 20 30 29 0a 20 20  , pTab, 0, 0).  
2e64a 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  ){.    assert( !
2e64b 69 73 56 69 65 77 20 29 3b 0a 20 20 20 20 73 71  isView );.    sq
2e64c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2e64d 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70 54 61  v, OP_Clear, pTa
2e64e 62 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 20 6d 65  b->tnum, iDb, me
2e64f 6d 43 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20  mCnt,.          
2e650 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62              pTab
2e651 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54  ->zName, P4_STAT
2e652 49 43 29 3b 0a 20 20 20 20 66 6f 72 28 70 49 64  IC);.    for(pId
2e653 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
2e654 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
2e655 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61  >pNext){.      a
2e656 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70 53 63  ssert( pIdx->pSc
2e657 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68  hema==pTab->pSch
2e658 65 6d 61 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ema );.      sql
2e659 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2e65a 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70 49 64 78  , OP_Clear, pIdx
2e65b 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20  ->tnum, iDb);.  
2e65c 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64    }.  }else.#end
2e65d 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
2e65e 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54 49 4d  T_TRUNCATE_OPTIM
2e65f 49 5a 41 54 49 4f 4e 20 2a 2f 0a 20 20 2f 2a 20  IZATION */.  /* 
2e660 54 68 65 20 75 73 75 61 6c 20 63 61 73 65 3a 20  The usual case: 
2e661 54 68 65 72 65 20 69 73 20 61 20 57 48 45 52 45  There is a WHERE
2e662 20 63 6c 61 75 73 65 20 73 6f 20 77 65 20 68 61   clause so we ha
2e663 76 65 20 74 6f 20 73 63 61 6e 20 74 68 72 6f 75  ve to scan throu
2e664 67 68 0a 20 20 2a 2a 20 74 68 65 20 74 61 62 6c  gh.  ** the tabl
2e665 65 20 61 6e 64 20 70 69 63 6b 20 77 68 69 63 68  e and pick which
2e666 20 72 65 63 6f 72 64 73 20 74 6f 20 64 65 6c 65   records to dele
2e667 74 65 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20  te..  */.  {.   
2e668 20 69 6e 74 20 69 52 6f 77 53 65 74 20 3d 20 2b   int iRowSet = +
2e669 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20  +pParse->nMem;  
2e66a 20 2f 2a 20 52 65 67 69 73 74 65 72 20 66 6f 72   /* Register for
2e66b 20 72 6f 77 73 65 74 20 6f 66 20 72 6f 77 73 20   rowset of rows 
2e66c 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 20  to delete */.   
2e66d 20 69 6e 74 20 69 52 6f 77 69 64 20 3d 20 2b 2b   int iRowid = ++
2e66e 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20  pParse->nMem;   
2e66f 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 73 74 6f   /* Used for sto
2e670 72 69 6e 67 20 72 6f 77 69 64 20 76 61 6c 75 65  ring rowid value
2e671 73 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65  s. */.    int re
2e672 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20  gRowid;         
2e673 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63 74            /* Act
2e674 75 61 6c 20 72 65 67 69 73 74 65 72 20 63 6f 6e  ual register con
2e675 74 61 69 6e 69 6e 67 20 72 6f 77 69 64 73 20 2a  taining rowids *
2e676 2f 0a 0a 20 20 20 20 2f 2a 20 43 6f 6c 6c 65 63  /..    /* Collec
2e677 74 20 72 6f 77 69 64 73 20 6f 66 20 65 76 65 72  t rowids of ever
2e678 79 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65  y row to be dele
2e679 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ted..    */.    
2e67a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2e67b 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
2e67c 20 69 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 70   iRowSet);.    p
2e67d 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57  WInfo = sqlite3W
2e67e 68 65 72 65 42 65 67 69 6e 28 0a 20 20 20 20 20  hereBegin(.     
2e67f 20 20 20 70 50 61 72 73 65 2c 20 70 54 61 62 4c     pParse, pTabL
2e680 69 73 74 2c 20 70 57 68 65 72 65 2c 20 30 2c 20  ist, pWhere, 0, 
2e681 30 2c 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41  0, WHERE_DUPLICA
2e682 54 45 53 5f 4f 4b 2c 20 30 0a 20 20 20 20 29 3b  TES_OK, 0.    );
2e683 0a 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d  .    if( pWInfo=
2e684 3d 30 20 29 20 67 6f 74 6f 20 64 65 6c 65 74 65  =0 ) goto delete
2e685 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
2e686 20 20 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71     regRowid = sq
2e687 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74  lite3ExprCodeGet
2e688 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70  Column(pParse, p
2e689 54 61 62 2c 20 2d 31 2c 20 69 43 75 72 2c 20 69  Tab, -1, iCur, i
2e68a 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20 20 20 73  Rowid, 0);.    s
2e68b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2e68c 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 41 64 64  (v, OP_RowSetAdd
2e68d 2c 20 69 52 6f 77 53 65 74 2c 20 72 65 67 52 6f  , iRowSet, regRo
2e68e 77 69 64 29 3b 0a 20 20 20 20 69 66 28 20 64 62  wid);.    if( db
2e68f 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
2e690 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20  _CountRows ){.  
2e691 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2e692 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
2e693 6d 6d 2c 20 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a  mm, memCnt, 1);.
2e694 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2e695 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f  3WhereEnd(pWInfo
2e696 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74  );..    /* Delet
2e697 65 20 65 76 65 72 79 20 69 74 65 6d 20 77 68 6f  e every item who
2e698 73 65 20 6b 65 79 20 77 61 73 20 77 72 69 74 74  se key was writt
2e699 65 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 64  en to the list d
2e69a 75 72 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a  uring the.    **
2e69b 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 2e 20   database scan. 
2e69c 20 57 65 20 68 61 76 65 20 74 6f 20 64 65 6c 65   We have to dele
2e69d 74 65 20 69 74 65 6d 73 20 61 66 74 65 72 20 74  te items after t
2e69e 68 65 20 73 63 61 6e 20 69 73 20 63 6f 6d 70 6c  he scan is compl
2e69f 65 74 65 0a 20 20 20 20 2a 2a 20 62 65 63 61 75  ete.    ** becau
2e6a0 73 65 20 64 65 6c 65 74 69 6e 67 20 61 6e 20 69  se deleting an i
2e6a1 74 65 6d 20 63 61 6e 20 63 68 61 6e 67 65 20 74  tem can change t
2e6a2 68 65 20 73 63 61 6e 20 6f 72 64 65 72 2e 20 20  he scan order.  
2e6a3 2a 2f 0a 20 20 20 20 65 6e 64 20 3d 20 73 71 6c  */.    end = sql
2e6a4 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2e6a5 6c 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 55 6e  l(v);..    /* Un
2e6a6 6c 65 73 73 20 74 68 69 73 20 69 73 20 61 20 76  less this is a v
2e6a7 69 65 77 2c 20 6f 70 65 6e 20 63 75 72 73 6f 72  iew, open cursor
2e6a8 73 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  s for the table 
2e6a9 77 65 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 64  we are .    ** d
2e6aa 65 6c 65 74 69 6e 67 20 66 72 6f 6d 20 61 6e 64  eleting from and
2e6ab 20 61 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73   all its indices
2e6ac 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 20 76  . If this is a v
2e6ad 69 65 77 2c 20 74 68 65 6e 20 74 68 65 0a 20 20  iew, then the.  
2e6ae 20 20 2a 2a 20 6f 6e 6c 79 20 65 66 66 65 63 74    ** only effect
2e6af 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20   this statement 
2e6b0 68 61 73 20 69 73 20 74 6f 20 66 69 72 65 20 74  has is to fire t
2e6b1 68 65 20 49 4e 53 54 45 41 44 20 4f 46 20 0a 20  he INSTEAD OF . 
2e6b2 20 20 20 2a 2a 20 74 72 69 67 67 65 72 73 2e 20     ** triggers. 
2e6b3 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 56   */.    if( !isV
2e6b4 69 65 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  iew ){.      sql
2e6b5 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64  ite3OpenTableAnd
2e6b6 49 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20  Indices(pParse, 
2e6b7 70 54 61 62 2c 20 69 43 75 72 2c 20 4f 50 5f 4f  pTab, iCur, OP_O
2e6b8 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 7d  penWrite);.    }
2e6b9 0a 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  ..    addr = sql
2e6ba 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2e6bb 2c 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 2c  , OP_RowSetRead,
2e6bc 20 69 52 6f 77 53 65 74 2c 20 65 6e 64 2c 20 69   iRowSet, end, i
2e6bd 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20  Rowid);..    /* 
2e6be 44 65 6c 65 74 65 20 74 68 65 20 72 6f 77 20 2a  Delete the row *
2e6bf 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
2e6c0 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2e6c1 4c 45 0a 20 20 20 20 69 66 28 20 49 73 56 69 72  LE.    if( IsVir
2e6c2 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
2e6c3 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2e6c4 70 56 54 61 62 20 3d 20 28 63 6f 6e 73 74 20 63  pVTab = (const c
2e6c5 68 61 72 20 2a 29 73 71 6c 69 74 65 33 47 65 74  har *)sqlite3Get
2e6c6 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29  VTable(db, pTab)
2e6c7 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2e6c8 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28  tabMakeWritable(
2e6c9 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20  pParse, pTab);. 
2e6ca 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2e6cb 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70  AddOp4(v, OP_VUp
2e6cc 64 61 74 65 2c 20 30 2c 20 31 2c 20 69 52 6f 77  date, 0, 1, iRow
2e6cd 69 64 2c 20 70 56 54 61 62 2c 20 50 34 5f 56 54  id, pVTab, P4_VT
2e6ce 41 42 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  AB);.      sqlit
2e6cf 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
2e6d0 2c 20 4f 45 5f 41 62 6f 72 74 29 3b 0a 20 20 20  , OE_Abort);.   
2e6d1 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f     sqlite3MayAbo
2e6d2 72 74 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  rt(pParse);.    
2e6d3 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20  }else.#endif.   
2e6d4 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 75   {.      int cou
2e6d5 6e 74 20 3d 20 28 70 50 61 72 73 65 2d 3e 6e 65  nt = (pParse->ne
2e6d6 73 74 65 64 3d 3d 30 29 3b 20 20 20 20 2f 2a 20  sted==0);    /* 
2e6d7 54 72 75 65 20 74 6f 20 63 6f 75 6e 74 20 63 68  True to count ch
2e6d8 61 6e 67 65 73 20 2a 2f 0a 20 20 20 20 20 20 73  anges */.      s
2e6d9 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f  qlite3GenerateRo
2e6da 77 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20  wDelete(pParse, 
2e6db 70 54 61 62 2c 20 69 43 75 72 2c 20 69 52 6f 77  pTab, iCur, iRow
2e6dc 69 64 2c 20 63 6f 75 6e 74 2c 20 70 54 72 69 67  id, count, pTrig
2e6dd 67 65 72 2c 20 4f 45 5f 44 65 66 61 75 6c 74 29  ger, OE_Default)
2e6de 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2e6df 45 6e 64 20 6f 66 20 74 68 65 20 64 65 6c 65 74  End of the delet
2e6e0 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 73 71  e loop */.    sq
2e6e1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2e6e2 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61  v, OP_Goto, 0, a
2e6e3 64 64 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ddr);.    sqlite
2e6e4 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2e6e5 6c 28 76 2c 20 65 6e 64 29 3b 0a 0a 20 20 20 20  l(v, end);..    
2e6e6 2f 2a 20 43 6c 6f 73 65 20 74 68 65 20 63 75 72  /* Close the cur
2e6e7 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 65  sors open on the
2e6e8 20 74 61 62 6c 65 20 61 6e 64 20 69 74 73 20 69   table and its i
2e6e9 6e 64 65 78 65 73 2e 20 2a 2f 0a 20 20 20 20 69  ndexes. */.    i
2e6ea 66 28 20 21 69 73 56 69 65 77 20 26 26 20 21 49  f( !isView && !I
2e6eb 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
2e6ec 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 2c  {.      for(i=1,
2e6ed 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64   pIdx=pTab->pInd
2e6ee 65 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c 20 70  ex; pIdx; i++, p
2e6ef 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
2e6f0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2e6f1 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2e6f2 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 20 2b 20  P_Close, iCur + 
2e6f3 69 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 29 3b 0a  i, pIdx->tnum);.
2e6f4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
2e6f5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2e6f6 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75  v, OP_Close, iCu
2e6f7 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  r);.    }.  }.. 
2e6f8 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 73   /* Update the s
2e6f9 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74  qlite_sequence t
2e6fa 61 62 6c 65 20 62 79 20 73 74 6f 72 69 6e 67 20  able by storing 
2e6fb 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  the content of t
2e6fc 68 65 0a 20 20 2a 2a 20 6d 61 78 69 6d 75 6d 20  he.  ** maximum 
2e6fd 72 6f 77 69 64 20 63 6f 75 6e 74 65 72 20 76 61  rowid counter va
2e6fe 6c 75 65 73 20 72 65 63 6f 72 64 65 64 20 77 68  lues recorded wh
2e6ff 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e  ile inserting in
2e700 74 6f 0a 20 20 2a 2a 20 61 75 74 6f 69 6e 63 72  to.  ** autoincr
2e701 65 6d 65 6e 74 20 74 61 62 6c 65 73 2e 0a 20 20  ement tables..  
2e702 2a 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  */.  if( pParse-
2e703 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26 20 70 50  >nested==0 && pP
2e704 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61  arse->pTriggerTa
2e705 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  b==0 ){.    sqli
2e706 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  te3Autoincrement
2e707 45 6e 64 28 70 50 61 72 73 65 29 3b 0a 20 20 7d  End(pParse);.  }
2e708 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68  ..  /* Return th
2e709 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
2e70a 20 74 68 61 74 20 77 65 72 65 20 64 65 6c 65 74   that were delet
2e70b 65 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74  ed. If this rout
2e70c 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e  ine is .  ** gen
2e70d 65 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63  erating code bec
2e70e 61 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74  ause of a call t
2e70f 6f 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50  o sqlite3NestedP
2e710 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20  arse(), do not. 
2e711 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63   ** invoke the c
2e712 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
2e713 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62  ..  */.  if( (db
2e714 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43  ->flags&SQLITE_C
2e715 6f 75 6e 74 52 6f 77 73 29 20 26 26 20 21 70 50  ountRows) && !pP
2e716 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20  arse->nested && 
2e717 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65  !pParse->pTrigge
2e718 72 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69  rTab ){.    sqli
2e719 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2e71a 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 6d   OP_ResultRow, m
2e71b 65 6d 43 6e 74 2c 20 31 29 3b 0a 20 20 20 20 73  emCnt, 1);.    s
2e71c 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
2e71d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
2e71e 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2e71f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
2e720 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 20  AME_NAME, "rows 
2e721 64 65 6c 65 74 65 64 22 2c 20 53 51 4c 49 54 45  deleted", SQLITE
2e722 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 0a 64  _STATIC);.  }..d
2e723 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
2e724 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 41 75 74  up:.  sqlite3Aut
2e725 68 43 6f 6e 74 65 78 74 50 6f 70 28 26 73 43 6f  hContextPop(&sCo
2e726 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65  ntext);.  sqlite
2e727 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64  3SrcListDelete(d
2e728 62 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20  b, pTabList);.  
2e729 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2e72a 65 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20  e(db, pWhere);. 
2e72b 20 72 65 74 75 72 6e 3b 0a 7d 0a 2f 2a 20 4d 61   return;.}./* Ma
2e72c 6b 65 20 73 75 72 65 20 22 69 73 56 69 65 77 22  ke sure "isView"
2e72d 20 61 6e 64 20 6f 74 68 65 72 20 6d 61 63 72 6f   and other macro
2e72e 73 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 20  s defined above 
2e72f 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 20 4f  are undefined. O
2e730 74 68 65 72 77 69 73 65 0a 2a 2a 20 74 68 65 6c  therwise.** thel
2e731 79 20 6d 61 79 20 69 6e 74 65 72 66 65 72 65 20  y may interfere 
2e732 77 69 74 68 20 63 6f 6d 70 69 6c 61 74 69 6f 6e  with compilation
2e733 20 6f 66 20 6f 74 68 65 72 20 66 75 6e 63 74 69   of other functi
2e734 6f 6e 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ons in this file
2e735 0a 2a 2a 20 28 6f 72 20 69 6e 20 61 6e 6f 74 68  .** (or in anoth
2e736 65 72 20 66 69 6c 65 2c 20 69 66 20 74 68 69 73  er file, if this
2e737 20 66 69 6c 65 20 62 65 63 6f 6d 65 73 20 70 61   file becomes pa
2e738 72 74 20 6f 66 20 74 68 65 20 61 6d 61 6c 67 61  rt of the amalga
2e739 6d 61 74 69 6f 6e 29 2e 20 20 2a 2f 0a 23 69 66  mation).  */.#if
2e73a 64 65 66 20 69 73 56 69 65 77 0a 20 23 75 6e 64  def isView. #und
2e73b 65 66 20 69 73 56 69 65 77 0a 23 65 6e 64 69 66  ef isView.#endif
2e73c 0a 23 69 66 64 65 66 20 70 54 72 69 67 67 65 72  .#ifdef pTrigger
2e73d 0a 20 23 75 6e 64 65 66 20 70 54 72 69 67 67 65  . #undef pTrigge
2e73e 72 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  r.#endif../*.** 
2e73f 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e  This routine gen
2e740 65 72 61 74 65 73 20 56 44 42 45 20 63 6f 64 65  erates VDBE code
2e741 20 74 68 61 74 20 63 61 75 73 65 73 20 61 20 73   that causes a s
2e742 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 0a 2a  ingle row of a.*
2e743 2a 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 74  * single table t
2e744 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 0a 2a 2a  o be deleted..**
2e745 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6d 75 73  .** The VDBE mus
2e746 74 20 62 65 20 69 6e 20 61 20 70 61 72 74 69 63  t be in a partic
2e747 75 6c 61 72 20 73 74 61 74 65 20 77 68 65 6e 20  ular state when 
2e748 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
2e749 63 61 6c 6c 65 64 2e 0a 2a 2a 20 54 68 65 73 65  called..** These
2e74a 20 61 72 65 20 74 68 65 20 72 65 71 75 69 72 65   are the require
2e74b 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  ments:.**.**   1
2e74c 2e 20 20 41 20 72 65 61 64 2f 77 72 69 74 65 20  .  A read/write 
2e74d 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  cursor pointing 
2e74e 74 6f 20 70 54 61 62 2c 20 74 68 65 20 74 61 62  to pTab, the tab
2e74f 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
2e750 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 74  e row.**       t
2e751 6f 20 62 65 20 64 65 6c 65 74 65 64 2c 20 6d 75  o be deleted, mu
2e752 73 74 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20  st be opened as 
2e753 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 24 69  cursor number $i
2e754 43 75 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20  Cur..**.**   2. 
2e755 20 52 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   Read/write curs
2e756 6f 72 73 20 66 6f 72 20 61 6c 6c 20 69 6e 64 69  ors for all indi
2e757 63 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73 74  ces of pTab must
2e758 20 62 65 20 6f 70 65 6e 20 61 73 0a 2a 2a 20 20   be open as.**  
2e759 20 20 20 20 20 63 75 72 73 6f 72 20 6e 75 6d 62       cursor numb
2e75a 65 72 20 62 61 73 65 2b 69 20 66 6f 72 20 74 68  er base+i for th
2e75b 65 20 69 2d 74 68 20 69 6e 64 65 78 2e 0a 2a 2a  e i-th index..**
2e75c 0a 2a 2a 20 20 20 33 2e 20 20 54 68 65 20 72 65  .**   3.  The re
2e75d 63 6f 72 64 20 6e 75 6d 62 65 72 20 6f 66 20 74  cord number of t
2e75e 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c  he row to be del
2e75f 65 74 65 64 20 6d 75 73 74 20 62 65 20 73 74 6f  eted must be sto
2e760 72 65 64 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20  red in.**       
2e761 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 52 6f 77  memory cell iRow
2e762 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  id..**.** This r
2e763 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
2e764 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20   code to remove 
2e765 62 6f 74 68 20 74 68 65 20 74 61 62 6c 65 20 72  both the table r
2e766 65 63 6f 72 64 20 61 6e 64 20 61 6c 6c 20 0a 2a  ecord and all .*
2e767 2a 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20  * index entries 
2e768 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 68  that point to th
2e769 61 74 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 53 51  at record..*/.SQ
2e76a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2e76b 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  d sqlite3Generat
2e76c 65 52 6f 77 44 65 6c 65 74 65 28 0a 20 20 50 61  eRowDelete(.  Pa
2e76d 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2e76e 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
2e76f 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
2e770 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54  pTab,       /* T
2e771 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
2e772 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65  the row to be de
2e773 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  leted */.  int i
2e774 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Cur,          /*
2e775 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
2e776 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  or the table */.
2e777 20 20 69 6e 74 20 69 52 6f 77 69 64 2c 20 20 20    int iRowid,   
2e778 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
2e779 65 6c 6c 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ell that contain
2e77a 73 20 74 68 65 20 72 6f 77 69 64 20 74 6f 20 64  s the rowid to d
2e77b 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 63  elete */.  int c
2e77c 6f 75 6e 74 2c 20 20 20 20 20 20 20 20 20 2f 2a  ount,         /*
2e77d 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 69 6e   If non-zero, in
2e77e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 6f 77 20  crement the row 
2e77f 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 2a  change counter *
2e780 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  /.  Trigger *pTr
2e781 69 67 67 65 72 2c 20 2f 2a 20 4c 69 73 74 20 6f  igger, /* List o
2e782 66 20 74 72 69 67 67 65 72 73 20 74 6f 20 28 70  f triggers to (p
2e783 6f 74 65 6e 74 69 61 6c 6c 79 29 20 66 69 72 65  otentially) fire
2e784 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 63 6f 6e 66   */.  int onconf
2e785 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61           /* Defa
2e786 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  ult ON CONFLICT 
2e787 70 6f 6c 69 63 79 20 66 6f 72 20 74 72 69 67 67  policy for trigg
2e788 65 72 73 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65  ers */.){.  Vdbe
2e789 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2e78a 64 62 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 56  dbe;        /* V
2e78b 64 62 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 6c  dbe */.  int iOl
2e78c 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  d = 0;          
2e78d 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
2e78e 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 4f 4c  t register in OL
2e78f 44 2e 2a 20 61 72 72 61 79 20 2a 2f 0a 20 20 69  D.* array */.  i
2e790 6e 74 20 69 4c 61 62 65 6c 3b 20 20 20 20 20 20  nt iLabel;      
2e791 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2e792 2a 20 4c 61 62 65 6c 20 72 65 73 6f 6c 76 65 64  * Label resolved
2e793 20 74 6f 20 65 6e 64 20 6f 66 20 67 65 6e 65 72   to end of gener
2e794 61 74 65 64 20 63 6f 64 65 20 2a 2f 0a 0a 20 20  ated code */..  
2e795 2f 2a 20 56 64 62 65 20 69 73 20 67 75 61 72 61  /* Vdbe is guara
2e796 6e 74 65 65 64 20 74 6f 20 68 61 76 65 20 62 65  nteed to have be
2e797 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  en allocated by 
2e798 74 68 69 73 20 73 74 61 67 65 2e 20 2a 2f 0a 20  this stage. */. 
2e799 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 0a 20   assert( v );.. 
2e79a 20 2f 2a 20 53 65 65 6b 20 63 75 72 73 6f 72 20   /* Seek cursor 
2e79b 69 43 75 72 20 74 6f 20 74 68 65 20 72 6f 77 20  iCur to the row 
2e79c 74 6f 20 64 65 6c 65 74 65 2e 20 49 66 20 74 68  to delete. If th
2e79d 69 73 20 72 6f 77 20 6e 6f 20 6c 6f 6e 67 65 72  is row no longer
2e79e 20 65 78 69 73 74 73 20 0a 20 20 2a 2a 20 28 74   exists .  ** (t
2e79f 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 69  his can happen i
2e7a0 66 20 61 20 74 72 69 67 67 65 72 20 70 72 6f 67  f a trigger prog
2e7a1 72 61 6d 20 68 61 73 20 61 6c 72 65 61 64 79 20  ram has already 
2e7a2 64 65 6c 65 74 65 64 20 69 74 29 2c 20 64 6f 0a  deleted it), do.
2e7a3 20 20 2a 2a 20 6e 6f 74 20 61 74 74 65 6d 70 74    ** not attempt
2e7a4 20 74 6f 20 64 65 6c 65 74 65 20 69 74 20 6f 72   to delete it or
2e7a5 20 66 69 72 65 20 61 6e 79 20 44 45 4c 45 54 45   fire any DELETE
2e7a6 20 74 72 69 67 67 65 72 73 2e 20 20 2a 2f 0a 20   triggers.  */. 
2e7a7 20 69 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65   iLabel = sqlite
2e7a8 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
2e7a9 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2e7aa 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp3(v, OP_Not
2e7ab 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 69 4c  Exists, iCur, iL
2e7ac 61 62 65 6c 2c 20 69 52 6f 77 69 64 29 3b 0a 20  abel, iRowid);. 
2e7ad 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61  .  /* If there a
2e7ae 72 65 20 61 6e 79 20 74 72 69 67 67 65 72 73 20  re any triggers 
2e7af 74 6f 20 66 69 72 65 2c 20 61 6c 6c 6f 63 61 74  to fire, allocat
2e7b0 65 20 61 20 72 61 6e 67 65 20 6f 66 20 72 65 67  e a range of reg
2e7b1 69 73 74 65 72 73 20 74 6f 0a 20 20 2a 2a 20 75  isters to.  ** u
2e7b2 73 65 20 66 6f 72 20 74 68 65 20 6f 6c 64 2e 2a  se for the old.*
2e7b3 20 72 65 66 65 72 65 6e 63 65 73 20 69 6e 20 74   references in t
2e7b4 68 65 20 74 72 69 67 67 65 72 73 2e 20 20 2a 2f  he triggers.  */
2e7b5 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 6b  .  if( sqlite3Fk
2e7b6 52 65 71 75 69 72 65 64 28 70 50 61 72 73 65 2c  Required(pParse,
2e7b7 20 70 54 61 62 2c 20 30 2c 20 30 29 20 7c 7c 20   pTab, 0, 0) || 
2e7b8 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
2e7b9 75 33 32 20 6d 61 73 6b 3b 20 20 20 20 20 20 20  u32 mask;       
2e7ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e7bb 20 4d 61 73 6b 20 6f 66 20 4f 4c 44 2e 2a 20 63   Mask of OLD.* c
2e7bc 6f 6c 75 6d 6e 73 20 69 6e 20 75 73 65 20 2a 2f  olumns in use */
2e7bd 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20  .    int iCol;  
2e7be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e7bf 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 75     /* Iterator u
2e7c0 73 65 64 20 77 68 69 6c 65 20 70 6f 70 75 6c 61  sed while popula
2e7c1 74 69 6e 67 20 4f 4c 44 2e 2a 20 2a 2f 0a 0a 20  ting OLD.* */.. 
2e7c2 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 6f 75 6c     /* TODO: Coul
2e7c3 64 20 75 73 65 20 74 65 6d 70 6f 72 61 72 79 20  d use temporary 
2e7c4 72 65 67 69 73 74 65 72 73 20 68 65 72 65 2e 20  registers here. 
2e7c5 41 6c 73 6f 20 63 6f 75 6c 64 20 61 74 74 65 6d  Also could attem
2e7c6 70 74 20 74 6f 0a 20 20 20 20 2a 2a 20 61 76 6f  pt to.    ** avo
2e7c7 69 64 20 63 6f 70 79 69 6e 67 20 74 68 65 20 63  id copying the c
2e7c8 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 72  ontents of the r
2e7c9 6f 77 69 64 20 72 65 67 69 73 74 65 72 2e 20 20  owid register.  
2e7ca 2a 2f 0a 20 20 20 20 6d 61 73 6b 20 3d 20 73 71  */.    mask = sq
2e7cb 6c 69 74 65 33 54 72 69 67 67 65 72 43 6f 6c 6d  lite3TriggerColm
2e7cc 61 73 6b 28 0a 20 20 20 20 20 20 20 20 70 50 61  ask(.        pPa
2e7cd 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 30  rse, pTrigger, 0
2e7ce 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 42 45 46  , 0, TRIGGER_BEF
2e7cf 4f 52 45 7c 54 52 49 47 47 45 52 5f 41 46 54 45  ORE|TRIGGER_AFTE
2e7d0 52 2c 20 70 54 61 62 2c 20 6f 6e 63 6f 6e 66 0a  R, pTab, onconf.
2e7d1 20 20 20 20 29 3b 0a 20 20 20 20 6d 61 73 6b 20      );.    mask 
2e7d2 7c 3d 20 73 71 6c 69 74 65 33 46 6b 4f 6c 64 6d  |= sqlite3FkOldm
2e7d3 61 73 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62  ask(pParse, pTab
2e7d4 29 3b 0a 20 20 20 20 69 4f 6c 64 20 3d 20 70 50  );.    iOld = pP
2e7d5 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20  arse->nMem+1;.  
2e7d6 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
2e7d7 3d 20 28 31 20 2b 20 70 54 61 62 2d 3e 6e 43 6f  = (1 + pTab->nCo
2e7d8 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75  l);..    /* Popu
2e7d9 6c 61 74 65 20 74 68 65 20 4f 4c 44 2e 2a 20 70  late the OLD.* p
2e7da 73 65 75 64 6f 2d 74 61 62 6c 65 20 72 65 67 69  seudo-table regi
2e7db 73 74 65 72 20 61 72 72 61 79 2e 20 54 68 65 73  ster array. Thes
2e7dc 65 20 76 61 6c 75 65 73 20 77 69 6c 6c 20 62 65  e values will be
2e7dd 20 0a 20 20 20 20 2a 2a 20 75 73 65 64 20 62 79   .    ** used by
2e7de 20 61 6e 79 20 42 45 46 4f 52 45 20 61 6e 64 20   any BEFORE and 
2e7df 41 46 54 45 52 20 74 72 69 67 67 65 72 73 20 74  AFTER triggers t
2e7e0 68 61 74 20 65 78 69 73 74 2e 20 20 2a 2f 0a 20  hat exist.  */. 
2e7e1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2e7e2 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c  dOp2(v, OP_Copy,
2e7e3 20 69 52 6f 77 69 64 2c 20 69 4f 6c 64 29 3b 0a   iRowid, iOld);.
2e7e4 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20      for(iCol=0; 
2e7e5 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  iCol<pTab->nCol;
2e7e6 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20   iCol++){.      
2e7e7 69 66 28 20 6d 61 73 6b 3d 3d 30 78 66 66 66 66  if( mask==0xffff
2e7e8 66 66 66 66 20 7c 7c 20 6d 61 73 6b 26 28 31 3c  ffff || mask&(1<
2e7e9 3c 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20  <iCol) ){.      
2e7ea 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2e7eb 65 47 65 74 43 6f 6c 75 6d 6e 4f 66 54 61 62 6c  eGetColumnOfTabl
2e7ec 65 28 76 2c 20 70 54 61 62 2c 20 69 43 75 72 2c  e(v, pTab, iCur,
2e7ed 20 69 43 6f 6c 2c 20 69 4f 6c 64 2b 69 43 6f 6c   iCol, iOld+iCol
2e7ee 2b 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  +1);.      }.   
2e7ef 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b   }..    /* Invok
2e7f0 65 20 42 45 46 4f 52 45 20 44 45 4c 45 54 45 20  e BEFORE DELETE 
2e7f1 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73  trigger programs
2e7f2 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  . */.    sqlite3
2e7f3 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70  CodeRowTrigger(p
2e7f4 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c  Parse, pTrigger,
2e7f5 20 0a 20 20 20 20 20 20 20 20 54 4b 5f 44 45 4c   .        TK_DEL
2e7f6 45 54 45 2c 20 30 2c 20 54 52 49 47 47 45 52 5f  ETE, 0, TRIGGER_
2e7f7 42 45 46 4f 52 45 2c 20 70 54 61 62 2c 20 69 4f  BEFORE, pTab, iO
2e7f8 6c 64 2c 20 6f 6e 63 6f 6e 66 2c 20 69 4c 61 62  ld, onconf, iLab
2e7f9 65 6c 0a 20 20 20 20 29 3b 0a 0a 20 20 20 20 2f  el.    );..    /
2e7fa 2a 20 53 65 65 6b 20 74 68 65 20 63 75 72 73 6f  * Seek the curso
2e7fb 72 20 74 6f 20 74 68 65 20 72 6f 77 20 74 6f 20  r to the row to 
2e7fc 62 65 20 64 65 6c 65 74 65 64 20 61 67 61 69 6e  be deleted again
2e7fd 2e 20 49 74 20 6d 61 79 20 62 65 20 74 68 61 74  . It may be that
2e7fe 0a 20 20 20 20 2a 2a 20 74 68 65 20 42 45 46 4f  .    ** the BEFO
2e7ff 52 45 20 74 72 69 67 67 65 72 73 20 63 6f 64 65  RE triggers code
2e800 64 20 61 62 6f 76 65 20 68 61 76 65 20 61 6c 72  d above have alr
2e801 65 61 64 79 20 72 65 6d 6f 76 65 64 20 74 68 65  eady removed the
2e802 20 72 6f 77 0a 20 20 20 20 2a 2a 20 62 65 69 6e   row.    ** bein
2e803 67 20 64 65 6c 65 74 65 64 2e 20 44 6f 20 6e 6f  g deleted. Do no
2e804 74 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 6c  t attempt to del
2e805 65 74 65 20 74 68 65 20 72 6f 77 20 61 20 73 65  ete the row a se
2e806 63 6f 6e 64 20 74 69 6d 65 2c 20 61 6e 64 20 0a  cond time, and .
2e807 20 20 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 66 69      ** do not fi
2e808 72 65 20 41 46 54 45 52 20 74 72 69 67 67 65 72  re AFTER trigger
2e809 73 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  s.  */.    sqlit
2e80a 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2e80b 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43  OP_NotExists, iC
2e80c 75 72 2c 20 69 4c 61 62 65 6c 2c 20 69 52 6f 77  ur, iLabel, iRow
2e80d 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20  id);..    /* Do 
2e80e 46 4b 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 54  FK processing. T
2e80f 68 69 73 20 63 61 6c 6c 20 63 68 65 63 6b 73 20  his call checks 
2e810 74 68 61 74 20 61 6e 79 20 46 4b 20 63 6f 6e 73  that any FK cons
2e811 74 72 61 69 6e 74 73 20 74 68 61 74 0a 20 20 20  traints that.   
2e812 20 2a 2a 20 72 65 66 65 72 20 74 6f 20 74 68 69   ** refer to thi
2e813 73 20 74 61 62 6c 65 20 28 69 2e 65 2e 20 63 6f  s table (i.e. co
2e814 6e 73 74 72 61 69 6e 74 73 20 61 74 74 61 63 68  nstraints attach
2e815 65 64 20 74 6f 20 6f 74 68 65 72 20 74 61 62 6c  ed to other tabl
2e816 65 73 29 20 0a 20 20 20 20 2a 2a 20 61 72 65 20  es) .    ** are 
2e817 6e 6f 74 20 76 69 6f 6c 61 74 65 64 20 62 79 20  not violated by 
2e818 64 65 6c 65 74 69 6e 67 20 74 68 69 73 20 72 6f  deleting this ro
2e819 77 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  w.  */.    sqlit
2e81a 65 33 46 6b 43 68 65 63 6b 28 70 50 61 72 73 65  e3FkCheck(pParse
2e81b 2c 20 70 54 61 62 2c 20 69 4f 6c 64 2c 20 30 29  , pTab, iOld, 0)
2e81c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c 65  ;.  }..  /* Dele
2e81d 74 65 20 74 68 65 20 69 6e 64 65 78 20 61 6e 64  te the index and
2e81e 20 74 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 20   table entries. 
2e81f 53 6b 69 70 20 74 68 69 73 20 73 74 65 70 20 69  Skip this step i
2e820 66 20 70 54 61 62 20 69 73 20 72 65 61 6c 6c 79  f pTab is really
2e821 0a 20 20 2a 2a 20 61 20 76 69 65 77 20 28 69 6e  .  ** a view (in
2e822 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20   which case the 
2e823 6f 6e 6c 79 20 65 66 66 65 63 74 20 6f 66 20 74  only effect of t
2e824 68 65 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d  he DELETE statem
2e825 65 6e 74 20 69 73 20 74 6f 0a 20 20 2a 2a 20 66  ent is to.  ** f
2e826 69 72 65 20 74 68 65 20 49 4e 53 54 45 41 44 20  ire the INSTEAD 
2e827 4f 46 20 74 72 69 67 67 65 72 73 29 2e 20 20 2a  OF triggers).  *
2e828 2f 20 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70  / .  if( pTab->p
2e829 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20  Select==0 ){.   
2e82a 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
2e82b 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 70  RowIndexDelete(p
2e82c 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75  Parse, pTab, iCu
2e82d 72 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  r, 0);.    sqlit
2e82e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2e82f 4f 50 5f 44 65 6c 65 74 65 2c 20 69 43 75 72 2c  OP_Delete, iCur,
2e830 20 28 63 6f 75 6e 74 3f 4f 50 46 4c 41 47 5f 4e   (count?OPFLAG_N
2e831 43 48 41 4e 47 45 3a 30 29 29 3b 0a 20 20 20 20  CHANGE:0));.    
2e832 69 66 28 20 63 6f 75 6e 74 20 29 7b 0a 20 20 20  if( count ){.   
2e833 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2e834 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 54  angeP4(v, -1, pT
2e835 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 54 52  ab->zName, P4_TR
2e836 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 7d 0a  ANSIENT);.    }.
2e837 20 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20 61 6e 79    }..  /* Do any
2e838 20 4f 4e 20 43 41 53 43 41 44 45 2c 20 53 45 54   ON CASCADE, SET
2e839 20 4e 55 4c 4c 20 6f 72 20 53 45 54 20 44 45 46   NULL or SET DEF
2e83a 41 55 4c 54 20 6f 70 65 72 61 74 69 6f 6e 73 20  AULT operations 
2e83b 72 65 71 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a  required to.  **
2e83c 20 68 61 6e 64 6c 65 20 72 6f 77 73 20 28 70 6f   handle rows (po
2e83d 73 73 69 62 6c 79 20 69 6e 20 6f 74 68 65 72 20  ssibly in other 
2e83e 74 61 62 6c 65 73 29 20 74 68 61 74 20 72 65 66  tables) that ref
2e83f 65 72 20 76 69 61 20 61 20 66 6f 72 65 69 67 6e  er via a foreign
2e840 20 6b 65 79 0a 20 20 2a 2a 20 74 6f 20 74 68 65   key.  ** to the
2e841 20 72 6f 77 20 6a 75 73 74 20 64 65 6c 65 74 65   row just delete
2e842 64 2e 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65 33  d. */ .  sqlite3
2e843 46 6b 41 63 74 69 6f 6e 73 28 70 50 61 72 73 65  FkActions(pParse
2e844 2c 20 70 54 61 62 2c 20 30 2c 20 69 4f 6c 64 29  , pTab, 0, iOld)
2e845 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 41  ;..  /* Invoke A
2e846 46 54 45 52 20 44 45 4c 45 54 45 20 74 72 69 67  FTER DELETE trig
2e847 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 20 2a 2f  ger programs. */
2e848 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f  .  sqlite3CodeRo
2e849 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c  wTrigger(pParse,
2e84a 20 70 54 72 69 67 67 65 72 2c 20 0a 20 20 20 20   pTrigger, .    
2e84b 20 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20    TK_DELETE, 0, 
2e84c 54 52 49 47 47 45 52 5f 41 46 54 45 52 2c 20 70  TRIGGER_AFTER, p
2e84d 54 61 62 2c 20 69 4f 6c 64 2c 20 6f 6e 63 6f 6e  Tab, iOld, oncon
2e84e 66 2c 20 69 4c 61 62 65 6c 0a 20 20 29 3b 0a 0a  f, iLabel.  );..
2e84f 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69    /* Jump here i
2e850 66 20 74 68 65 20 72 6f 77 20 68 61 64 20 61 6c  f the row had al
2e851 72 65 61 64 79 20 62 65 65 6e 20 64 65 6c 65 74  ready been delet
2e852 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 42 45  ed before any BE
2e853 46 4f 52 45 0a 20 20 2a 2a 20 74 72 69 67 67 65  FORE.  ** trigge
2e854 72 20 70 72 6f 67 72 61 6d 73 20 77 65 72 65 20  r programs were 
2e855 69 6e 76 6f 6b 65 64 2e 20 4f 72 20 69 66 20 61  invoked. Or if a
2e856 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
2e857 20 74 68 72 6f 77 73 20 61 20 0a 20 20 2a 2a 20   throws a .  ** 
2e858 52 41 49 53 45 28 49 47 4e 4f 52 45 29 20 65 78  RAISE(IGNORE) ex
2e859 63 65 70 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 73  ception.  */.  s
2e85a 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
2e85b 65 4c 61 62 65 6c 28 76 2c 20 69 4c 61 62 65 6c  eLabel(v, iLabel
2e85c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
2e85d 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
2e85e 65 73 20 56 44 42 45 20 63 6f 64 65 20 74 68 61  es VDBE code tha
2e85f 74 20 63 61 75 73 65 73 20 74 68 65 20 64 65 6c  t causes the del
2e860 65 74 69 6f 6e 20 6f 66 20 61 6c 6c 0a 2a 2a 20  etion of all.** 
2e861 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 61 73  index entries as
2e862 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
2e863 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20  single row of a 
2e864 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e 0a 2a 2a  single table..**
2e865 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6d 75 73  .** The VDBE mus
2e866 74 20 62 65 20 69 6e 20 61 20 70 61 72 74 69 63  t be in a partic
2e867 75 6c 61 72 20 73 74 61 74 65 20 77 68 65 6e 20  ular state when 
2e868 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
2e869 63 61 6c 6c 65 64 2e 0a 2a 2a 20 54 68 65 73 65  called..** These
2e86a 20 61 72 65 20 74 68 65 20 72 65 71 75 69 72 65   are the require
2e86b 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  ments:.**.**   1
2e86c 2e 20 20 41 20 72 65 61 64 2f 77 72 69 74 65 20  .  A read/write 
2e86d 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  cursor pointing 
2e86e 74 6f 20 70 54 61 62 2c 20 74 68 65 20 74 61 62  to pTab, the tab
2e86f 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
2e870 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 74  e row.**       t
2e871 6f 20 62 65 20 64 65 6c 65 74 65 64 2c 20 6d 75  o be deleted, mu
2e872 73 74 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20  st be opened as 
2e873 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 22 69  cursor number "i
2e874 43 75 72 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e  Cur"..**.**   2.
2e875 20 20 52 65 61 64 2f 77 72 69 74 65 20 63 75 72    Read/write cur
2e876 73 6f 72 73 20 66 6f 72 20 61 6c 6c 20 69 6e 64  sors for all ind
2e877 69 63 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73  ices of pTab mus
2e878 74 20 62 65 20 6f 70 65 6e 20 61 73 0a 2a 2a 20  t be open as.** 
2e879 20 20 20 20 20 20 63 75 72 73 6f 72 20 6e 75 6d        cursor num
2e87a 62 65 72 20 69 43 75 72 2b 69 20 66 6f 72 20 74  ber iCur+i for t
2e87b 68 65 20 69 2d 74 68 20 69 6e 64 65 78 2e 0a 2a  he i-th index..*
2e87c 2a 0a 2a 2a 20 20 20 33 2e 20 20 54 68 65 20 22  *.**   3.  The "
2e87d 69 43 75 72 22 20 63 75 72 73 6f 72 20 6d 75 73  iCur" cursor mus
2e87e 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f  t be pointing to
2e87f 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 69 73   the row that is
2e880 20 74 6f 20 62 65 0a 2a 2a 20 20 20 20 20 20 20   to be.**       
2e881 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  deleted..*/.SQLI
2e882 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2e883 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52  sqlite3GenerateR
2e884 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 0a 20  owIndexDelete(. 
2e885 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2e886 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61      /* Parsing a
2e887 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69  nd code generati
2e888 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2e889 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
2e88a 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e 74     /* Table cont
2e88b 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 20 74  aining the row t
2e88c 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a  o be deleted */.
2e88d 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20    int iCur,     
2e88e 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e       /* Cursor n
2e88f 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61  umber for the ta
2e890 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52  ble */.  int *aR
2e891 65 67 49 64 78 20 20 20 20 20 20 20 2f 2a 20 4f  egIdx       /* O
2e892 6e 6c 79 20 64 65 6c 65 74 65 20 69 66 20 61 52  nly delete if aR
2e893 65 67 49 64 78 21 3d 30 20 26 26 20 61 52 65 67  egIdx!=0 && aReg
2e894 49 64 78 5b 69 5d 3e 30 20 2a 2f 0a 29 7b 0a 20  Idx[i]>0 */.){. 
2e895 20 69 6e 74 20 69 3b 0a 20 20 49 6e 64 65 78 20   int i;.  Index 
2e896 2a 70 49 64 78 3b 0a 20 20 69 6e 74 20 72 31 3b  *pIdx;.  int r1;
2e897 0a 0a 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64  ..  for(i=1, pId
2e898 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
2e899 70 49 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78 3d  pIdx; i++, pIdx=
2e89a 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
2e89b 20 20 69 66 28 20 61 52 65 67 49 64 78 21 3d 30    if( aRegIdx!=0
2e89c 20 26 26 20 61 52 65 67 49 64 78 5b 69 2d 31 5d   && aRegIdx[i-1]
2e89d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
2e89e 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
2e89f 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79  GenerateIndexKey
2e8a0 28 70 50 61 72 73 65 2c 20 70 49 64 78 2c 20 69  (pParse, pIdx, i
2e8a1 43 75 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  Cur, 0, 0);.    
2e8a2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2e8a3 33 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  3(pParse->pVdbe,
2e8a4 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 2c 20 69   OP_IdxDelete, i
2e8a5 43 75 72 2b 69 2c 20 72 31 2c 70 49 64 78 2d 3e  Cur+i, r1,pIdx->
2e8a6 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 7d 0a  nColumn+1);.  }.
2e8a7 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
2e8a8 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
2e8a9 20 61 73 73 65 6d 62 6c 65 20 61 6e 20 69 6e 64   assemble an ind
2e8aa 65 78 20 6b 65 79 20 61 6e 64 20 70 75 74 20 69  ex key and put i
2e8ab 74 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a  t in register.**
2e8ac 20 72 65 67 4f 75 74 2e 20 20 54 68 65 20 6b 65   regOut.  The ke
2e8ad 79 20 77 69 74 68 20 62 65 20 66 6f 72 20 69 6e  y with be for in
2e8ae 64 65 78 20 70 49 64 78 20 77 68 69 63 68 20 69  dex pIdx which i
2e8af 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20 70 54  s an index on pT
2e8b0 61 62 2e 0a 2a 2a 20 69 43 75 72 20 69 73 20 74  ab..** iCur is t
2e8b1 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20 63 75  he index of a cu
2e8b2 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65  rsor open on the
2e8b3 20 70 54 61 62 20 74 61 62 6c 65 20 61 6e 64 20   pTab table and 
2e8b4 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 74  pointing to.** t
2e8b5 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 6e 65  he entry that ne
2e8b6 65 64 73 20 69 6e 64 65 78 69 6e 67 2e 0a 2a 2a  eds indexing..**
2e8b7 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 72 65 67  .** Return a reg
2e8b8 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68 69  ister number whi
2e8b9 63 68 20 69 73 20 74 68 65 20 66 69 72 73 74 20  ch is the first 
2e8ba 69 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a  in a block of.**
2e8bb 20 72 65 67 69 73 74 65 72 73 20 74 68 61 74 20   registers that 
2e8bc 68 6f 6c 64 73 20 74 68 65 20 65 6c 65 6d 65 6e  holds the elemen
2e8bd 74 73 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  ts of the index 
2e8be 6b 65 79 2e 20 20 54 68 65 0a 2a 2a 20 62 6c 6f  key.  The.** blo
2e8bf 63 6b 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  ck of registers 
2e8c0 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
2e8c1 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 20   deallocated by 
2e8c2 74 68 65 20 74 69 6d 65 0a 2a 2a 20 74 68 69 73  the time.** this
2e8c3 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
2e8c4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2e8c5 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47  ATE int sqlite3G
2e8c6 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28  enerateIndexKey(
2e8c7 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2e8c8 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67  ,     /* Parsing
2e8c9 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 49 6e   context */.  In
2e8ca 64 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20  dex *pIdx,      
2e8cb 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 66 6f   /* The index fo
2e8cc 72 20 77 68 69 63 68 20 74 6f 20 67 65 6e 65 72  r which to gener
2e8cd 61 74 65 20 61 20 6b 65 79 20 2a 2f 0a 20 20 69  ate a key */.  i
2e8ce 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20  nt iCur,        
2e8cf 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62    /* Cursor numb
2e8d0 65 72 20 66 6f 72 20 74 68 65 20 70 49 64 78 2d  er for the pIdx-
2e8d1 3e 70 54 61 62 6c 65 20 74 61 62 6c 65 20 2a 2f  >pTable table */
2e8d2 0a 20 20 69 6e 74 20 72 65 67 4f 75 74 2c 20 20  .  int regOut,  
2e8d3 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
2e8d4 68 65 20 6e 65 77 20 69 6e 64 65 78 20 6b 65 79  he new index key
2e8d5 20 74 6f 20 74 68 69 73 20 72 65 67 69 73 74 65   to this registe
2e8d6 72 20 2a 2f 0a 20 20 69 6e 74 20 64 6f 4d 61 6b  r */.  int doMak
2e8d7 65 52 65 63 20 20 20 20 20 20 2f 2a 20 52 75 6e  eRec      /* Run
2e8d8 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f   the OP_MakeReco
2e8d9 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  rd instruction i
2e8da 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 56  f true */.){.  V
2e8db 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
2e8dc 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6a 3b  >pVdbe;.  int j;
2e8dd 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d  .  Table *pTab =
2e8de 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 20   pIdx->pTable;. 
2e8df 20 69 6e 74 20 72 65 67 42 61 73 65 3b 0a 20 20   int regBase;.  
2e8e0 69 6e 74 20 6e 43 6f 6c 3b 0a 0a 20 20 6e 43 6f  int nCol;..  nCo
2e8e1 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  l = pIdx->nColum
2e8e2 6e 3b 0a 20 20 72 65 67 42 61 73 65 20 3d 20 73  n;.  regBase = s
2e8e3 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
2e8e4 67 65 28 70 50 61 72 73 65 2c 20 6e 43 6f 6c 2b  ge(pParse, nCol+
2e8e5 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  1);.  sqlite3Vdb
2e8e6 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
2e8e7 77 69 64 2c 20 69 43 75 72 2c 20 72 65 67 42 61  wid, iCur, regBa
2e8e8 73 65 2b 6e 43 6f 6c 29 3b 0a 20 20 66 6f 72 28  se+nCol);.  for(
2e8e9 6a 3d 30 3b 20 6a 3c 6e 43 6f 6c 3b 20 6a 2b 2b  j=0; j<nCol; j++
2e8ea 29 7b 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d  ){.    int idx =
2e8eb 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
2e8ec 6a 5d 3b 0a 20 20 20 20 69 66 28 20 69 64 78 3d  j];.    if( idx=
2e8ed 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a  =pTab->iPKey ){.
2e8ee 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2e8ef 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
2e8f0 6f 70 79 2c 20 72 65 67 42 61 73 65 2b 6e 43 6f  opy, regBase+nCo
2e8f1 6c 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20  l, regBase+j);. 
2e8f2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2e8f3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2e8f4 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
2e8f5 69 43 75 72 2c 20 69 64 78 2c 20 72 65 67 42 61  iCur, idx, regBa
2e8f6 73 65 2b 6a 29 3b 0a 20 20 20 20 20 20 73 71 6c  se+j);.      sql
2e8f7 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c  ite3ColumnDefaul
2e8f8 74 28 76 2c 20 70 54 61 62 2c 20 69 64 78 2c 20  t(v, pTab, idx, 
2e8f9 2d 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  -1);.    }.  }. 
2e8fa 20 69 66 28 20 64 6f 4d 61 6b 65 52 65 63 20 29   if( doMakeRec )
2e8fb 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
2e8fc 20 2a 7a 41 66 66 3b 0a 20 20 20 20 69 66 28 20   *zAff;.    if( 
2e8fd 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 0a 20 20  pTab->pSelect.  
2e8fe 20 20 20 7c 7c 20 4f 70 74 69 6d 69 7a 61 74 69     || Optimizati
2e8ff 6f 6e 44 69 73 61 62 6c 65 64 28 70 50 61 72 73  onDisabled(pPars
2e900 65 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 49 64  e->db, SQLITE_Id
2e901 78 52 65 61 6c 41 73 49 6e 74 29 0a 20 20 20 20  xRealAsInt).    
2e902 29 7b 0a 20 20 20 20 20 20 7a 41 66 66 20 3d 20  ){.      zAff = 
2e903 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
2e904 20 20 20 20 7a 41 66 66 20 3d 20 73 71 6c 69 74      zAff = sqlit
2e905 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53  e3IndexAffinityS
2e906 74 72 28 76 2c 20 70 49 64 78 29 3b 0a 20 20 20  tr(v, pIdx);.   
2e907 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
2e908 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
2e909 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42 61  akeRecord, regBa
2e90a 73 65 2c 20 6e 43 6f 6c 2b 31 2c 20 72 65 67 4f  se, nCol+1, regO
2e90b 75 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ut);.    sqlite3
2e90c 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
2e90d 2d 31 2c 20 7a 41 66 66 2c 20 50 34 5f 54 52 41  -1, zAff, P4_TRA
2e90e 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 20 20 73  NSIENT);.  }.  s
2e90f 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2e910 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  pRange(pParse, r
2e911 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b  egBase, nCol+1);
2e912 0a 20 20 72 65 74 75 72 6e 20 72 65 67 42 61 73  .  return regBas
2e913 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  e;.}../*********
2e914 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 65 6c  ***** End of del
2e915 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ete.c **********
2e916 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e917 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e918 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
2e919 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
2e91a 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   func.c ********
2e91b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e91c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e91d 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 32  ****/./*.** 2002
2e91e 20 46 65 62 72 75 61 72 79 20 32 33 0a 2a 2a 0a   February 23.**.
2e91f 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2e920 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2e921 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2e922 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2e923 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
2e924 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
2e925 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
2e926 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
2e927 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
2e928 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2e929 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2e92a 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2e92b 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
2e92c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2e92d 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
2e92e 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
2e92f 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
2e930 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
2e931 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e932 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e933 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e934 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
2e935 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
2e936 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
2e937 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
2e938 20 76 61 72 69 6f 75 73 20 53 51 4c 0a 2a 2a 20   various SQL.** 
2e939 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 53 51 4c  functions of SQL
2e93a 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ite.  .**.** The
2e93b 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65  re is only one e
2e93c 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69  xported symbol i
2e93d 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68  n this file - th
2e93e 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71  e function.** sq
2e93f 6c 69 74 65 52 65 67 69 73 74 65 72 42 75 69 6c  liteRegisterBuil
2e940 64 69 6e 46 75 6e 63 74 69 6f 6e 73 28 29 20 66  dinFunctions() f
2e941 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74  ound at the bott
2e942 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a  om of the file..
2e943 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64  ** All other cod
2e944 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65  e has file scope
2e945 2e 0a 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65  ..*/./* #include
2e946 20 3c 73 74 64 6c 69 62 2e 68 3e 20 2a 2f 0a 2f   <stdlib.h> */./
2e947 2a 20 23 69 6e 63 6c 75 64 65 20 3c 61 73 73 65  * #include <asse
2e948 72 74 2e 68 3e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  rt.h> */../*.** 
2e949 52 65 74 75 72 6e 20 74 68 65 20 63 6f 6c 6c 61  Return the colla
2e94a 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 61 73  ting function as
2e94b 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
2e94c 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
2e94d 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c  tic CollSeq *sql
2e94e 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
2e94f 65 71 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  eq(sqlite3_conte
2e950 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20  xt *context){.  
2e951 72 65 74 75 72 6e 20 63 6f 6e 74 65 78 74 2d 3e  return context->
2e952 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pColl;.}../*.** 
2e953 49 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68  Indicate that th
2e954 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 6c 6f  e accumulator lo
2e955 61 64 20 73 68 6f 75 6c 64 20 62 65 20 73 6b 69  ad should be ski
2e956 70 70 65 64 20 6f 6e 20 74 68 69 73 0a 2a 2a 20  pped on this.** 
2e957 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  iteration of the
2e958 20 61 67 67 72 65 67 61 74 65 20 6c 6f 6f 70 2e   aggregate loop.
2e959 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2e95a 73 71 6c 69 74 65 33 53 6b 69 70 41 63 63 75 6d  sqlite3SkipAccum
2e95b 75 6c 61 74 6f 72 4c 6f 61 64 28 73 71 6c 69 74  ulatorLoad(sqlit
2e95c 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
2e95d 65 78 74 29 7b 0a 20 20 63 6f 6e 74 65 78 74 2d  ext){.  context-
2e95e 3e 73 6b 69 70 46 6c 61 67 20 3d 20 31 3b 0a 7d  >skipFlag = 1;.}
2e95f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
2e960 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 6f  tation of the no
2e961 6e 2d 61 67 67 72 65 67 61 74 65 20 6d 69 6e 28  n-aggregate min(
2e962 29 20 61 6e 64 20 6d 61 78 28 29 20 66 75 6e 63  ) and max() func
2e963 74 69 6f 6e 73 0a 2a 2f 0a 73 74 61 74 69 63 20  tions.*/.static 
2e964 76 6f 69 64 20 6d 69 6e 6d 61 78 46 75 6e 63 28  void minmaxFunc(
2e965 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
2e966 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
2e967 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
2e968 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
2e969 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
2e96a 74 20 6d 61 73 6b 3b 20 20 20 20 2f 2a 20 30 20  t mask;    /* 0 
2e96b 66 6f 72 20 6d 69 6e 28 29 20 6f 72 20 30 78 66  for min() or 0xf
2e96c 66 66 66 66 66 66 66 20 66 6f 72 20 6d 61 78 28  fffffff for max(
2e96d 29 20 2a 2f 0a 20 20 69 6e 74 20 69 42 65 73 74  ) */.  int iBest
2e96e 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  ;.  CollSeq *pCo
2e96f 6c 6c 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61  ll;..  assert( a
2e970 72 67 63 3e 31 20 29 3b 0a 20 20 6d 61 73 6b 20  rgc>1 );.  mask 
2e971 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  = sqlite3_user_d
2e972 61 74 61 28 63 6f 6e 74 65 78 74 29 3d 3d 30 20  ata(context)==0 
2e973 3f 20 30 20 3a 20 2d 31 3b 0a 20 20 70 43 6f 6c  ? 0 : -1;.  pCol
2e974 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46 75  l = sqlite3GetFu
2e975 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78  ncCollSeq(contex
2e976 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  t);.  assert( pC
2e977 6f 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  oll );.  assert(
2e978 20 6d 61 73 6b 3d 3d 2d 31 20 7c 7c 20 6d 61 73   mask==-1 || mas
2e979 6b 3d 3d 30 20 29 3b 0a 20 20 69 42 65 73 74 20  k==0 );.  iBest 
2e97a 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  = 0;.  if( sqlit
2e97b 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
2e97c 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e  gv[0])==SQLITE_N
2e97d 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ULL ) return;.  
2e97e 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67 63 3b  for(i=1; i<argc;
2e97f 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73   i++){.    if( s
2e980 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
2e981 65 28 61 72 67 76 5b 69 5d 29 3d 3d 53 51 4c 49  e(argv[i])==SQLI
2e982 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  TE_NULL ) return
2e983 3b 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 74  ;.    if( (sqlit
2e984 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 61 72 67  e3MemCompare(arg
2e985 76 5b 69 42 65 73 74 5d 2c 20 61 72 67 76 5b 69  v[iBest], argv[i
2e986 5d 2c 20 70 43 6f 6c 6c 29 5e 6d 61 73 6b 29 3e  ], pColl)^mask)>
2e987 3d 30 20 29 7b 0a 20 20 20 20 20 20 74 65 73 74  =0 ){.      test
2e988 63 61 73 65 28 20 6d 61 73 6b 3d 3d 30 20 29 3b  case( mask==0 );
2e989 0a 20 20 20 20 20 20 69 42 65 73 74 20 3d 20 69  .      iBest = i
2e98a 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
2e98b 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
2e98c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76  ue(context, argv
2e98d 5b 69 42 65 73 74 5d 29 3b 0a 7d 0a 0a 2f 2a 0a  [iBest]);.}../*.
2e98e 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 79  ** Return the ty
2e98f 70 65 20 6f 66 20 74 68 65 20 61 72 67 75 6d 65  pe of the argume
2e990 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  nt..*/.static vo
2e991 69 64 20 74 79 70 65 6f 66 46 75 6e 63 28 0a 20  id typeofFunc(. 
2e992 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2e993 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
2e994 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
2e995 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
2e996 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  .){.  const char
2e997 20 2a 7a 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45   *z = 0;.  UNUSE
2e998 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
2e999 73 65 64 29 3b 0a 20 20 73 77 69 74 63 68 28 20  sed);.  switch( 
2e99a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
2e99b 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20  pe(argv[0]) ){. 
2e99c 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49     case SQLITE_I
2e99d 4e 54 45 47 45 52 3a 20 7a 20 3d 20 22 69 6e 74  NTEGER: z = "int
2e99e 65 67 65 72 22 3b 20 62 72 65 61 6b 3b 0a 20 20  eger"; break;.  
2e99f 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45    case SQLITE_TE
2e9a0 58 54 3a 20 20 20 20 7a 20 3d 20 22 74 65 78 74  XT:    z = "text
2e9a1 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ";    break;.   
2e9a2 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f   case SQLITE_FLO
2e9a3 41 54 3a 20 20 20 7a 20 3d 20 22 72 65 61 6c 22  AT:   z = "real"
2e9a4 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  ;    break;.    
2e9a5 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42  case SQLITE_BLOB
2e9a6 3a 20 20 20 20 7a 20 3d 20 22 62 6c 6f 62 22 3b  :    z = "blob";
2e9a7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64      break;.    d
2e9a8 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20 20 20  efault:         
2e9a9 20 20 20 20 7a 20 3d 20 22 6e 75 6c 6c 22 3b 20      z = "null"; 
2e9aa 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20     break;.  }.  
2e9ab 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
2e9ac 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20  ext(context, z, 
2e9ad 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  -1, SQLITE_STATI
2e9ae 43 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d  C);.}.../*.** Im
2e9af 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
2e9b0 74 68 65 20 6c 65 6e 67 74 68 28 29 20 66 75 6e  the length() fun
2e9b1 63 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20  ction.*/.static 
2e9b2 76 6f 69 64 20 6c 65 6e 67 74 68 46 75 6e 63 28  void lengthFunc(
2e9b3 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
2e9b4 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
2e9b5 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
2e9b6 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
2e9b7 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 0a 20  ){.  int len;.. 
2e9b8 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31   assert( argc==1
2e9b9 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   );.  UNUSED_PAR
2e9ba 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20  AMETER(argc);.  
2e9bb 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f  switch( sqlite3_
2e9bc 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
2e9bd 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20  0]) ){.    case 
2e9be 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 0a 20 20 20  SQLITE_BLOB:.   
2e9bf 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54   case SQLITE_INT
2e9c0 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 53  EGER:.    case S
2e9c1 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20  QLITE_FLOAT: {. 
2e9c2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
2e9c3 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c  ult_int(context,
2e9c4 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
2e9c5 79 74 65 73 28 61 72 67 76 5b 30 5d 29 29 3b 0a  ytes(argv[0]));.
2e9c6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e9c7 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
2e9c8 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20  TE_TEXT: {.     
2e9c9 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
2e9ca 63 68 61 72 20 2a 7a 20 3d 20 73 71 6c 69 74 65  char *z = sqlite
2e9cb 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
2e9cc 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28  v[0]);.      if(
2e9cd 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   z==0 ) return;.
2e9ce 20 20 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20        len = 0;. 
2e9cf 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 20 29       while( *z )
2e9d0 7b 0a 20 20 20 20 20 20 20 20 6c 65 6e 2b 2b 3b  {.        len++;
2e9d1 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  .        SQLITE_
2e9d2 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20  SKIP_UTF8(z);.  
2e9d3 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
2e9d4 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63  te3_result_int(c
2e9d5 6f 6e 74 65 78 74 2c 20 6c 65 6e 29 3b 0a 20 20  ontext, len);.  
2e9d6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2e9d7 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
2e9d8 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
2e9d9 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f 6e 74 65 78  sult_null(contex
2e9da 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  t);.      break;
2e9db 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
2e9dc 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
2e9dd 6f 6e 20 6f 66 20 74 68 65 20 61 62 73 28 29 20  on of the abs() 
2e9de 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
2e9df 49 4d 50 3a 20 52 2d 32 33 39 37 39 2d 32 36 38  IMP: R-23979-268
2e9e0 35 35 20 54 68 65 20 61 62 73 28 58 29 20 66 75  55 The abs(X) fu
2e9e1 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
2e9e2 68 65 20 61 62 73 6f 6c 75 74 65 20 76 61 6c 75  he absolute valu
2e9e3 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 75 6d 65  e of.** the nume
2e9e4 72 69 63 20 61 72 67 75 6d 65 6e 74 20 58 2e 20  ric argument X. 
2e9e5 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2e9e6 61 62 73 46 75 6e 63 28 73 71 6c 69 74 65 33 5f  absFunc(sqlite3_
2e9e7 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2e9e8 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
2e9e9 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
2e9ea 29 7b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  ){.  assert( arg
2e9eb 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44  c==1 );.  UNUSED
2e9ec 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29  _PARAMETER(argc)
2e9ed 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c 69  ;.  switch( sqli
2e9ee 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
2e9ef 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63  rgv[0]) ){.    c
2e9f0 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  ase SQLITE_INTEG
2e9f1 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 36 34 20  ER: {.      i64 
2e9f2 69 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76  iVal = sqlite3_v
2e9f3 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b  alue_int64(argv[
2e9f4 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69  0]);.      if( i
2e9f5 56 61 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  Val<0 ){.       
2e9f6 20 69 66 28 20 28 69 56 61 6c 3c 3c 31 29 3d 3d   if( (iVal<<1)==
2e9f7 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  0 ){.          /
2e9f8 2a 20 49 4d 50 3a 20 52 2d 33 35 34 36 30 2d 31  * IMP: R-35460-1
2e9f9 35 30 38 34 20 49 66 20 58 20 69 73 20 74 68 65  5084 If X is the
2e9fa 20 69 6e 74 65 67 65 72 20 2d 39 32 32 33 33 37   integer -922337
2e9fb 32 30 33 36 38 35 34 37 37 35 38 30 37 20 74 68  2036854775807 th
2e9fc 65 6e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  en.          ** 
2e9fd 61 62 73 28 58 29 20 74 68 72 6f 77 73 20 61 6e  abs(X) throws an
2e9fe 20 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f   integer overflo
2e9ff 77 20 65 72 72 6f 72 20 73 69 6e 63 65 20 74 68  w error since th
2ea00 65 72 65 20 69 73 20 6e 6f 0a 20 20 20 20 20 20  ere is no.      
2ea01 20 20 20 20 2a 2a 20 65 71 75 69 76 61 6c 65 6e      ** equivalen
2ea02 74 20 70 6f 73 69 74 69 76 65 20 36 34 2d 62 69  t positive 64-bi
2ea03 74 20 74 77 6f 20 63 6f 6d 70 6c 65 6d 65 6e 74  t two complement
2ea04 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 20   value. */.     
2ea05 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
2ea06 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
2ea07 74 2c 20 22 69 6e 74 65 67 65 72 20 6f 76 65 72  t, "integer over
2ea08 66 6c 6f 77 22 2c 20 2d 31 29 3b 0a 20 20 20 20  flow", -1);.    
2ea09 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
2ea0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2ea0b 69 56 61 6c 20 3d 20 2d 69 56 61 6c 3b 0a 20 20  iVal = -iVal;.  
2ea0c 20 20 20 20 7d 20 0a 20 20 20 20 20 20 73 71 6c      } .      sql
2ea0d 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
2ea0e 34 28 63 6f 6e 74 65 78 74 2c 20 69 56 61 6c 29  4(context, iVal)
2ea0f 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2ea10 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
2ea11 4c 49 54 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20  LITE_NULL: {.   
2ea12 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 33 37 34     /* IMP: R-374
2ea13 33 34 2d 31 39 39 32 39 20 41 62 73 28 58 29 20  34-19929 Abs(X) 
2ea14 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20  returns NULL if 
2ea15 58 20 69 73 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20  X is NULL. */.  
2ea16 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
2ea17 6c 74 5f 6e 75 6c 6c 28 63 6f 6e 74 65 78 74 29  lt_null(context)
2ea18 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2ea19 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74     }.    default
2ea1a 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 42 65 63  : {.      /* Bec
2ea1b 61 75 73 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  ause sqlite3_val
2ea1c 75 65 5f 64 6f 75 62 6c 65 28 29 20 72 65 74 75  ue_double() retu
2ea1d 72 6e 73 20 30 2e 30 20 69 66 20 74 68 65 20 61  rns 0.0 if the a
2ea1e 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 0a 20  rgument is not. 
2ea1f 20 20 20 20 20 2a 2a 20 73 6f 6d 65 74 68 69 6e       ** somethin
2ea20 67 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f  g that can be co
2ea21 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 20 6e  nverted into a n
2ea22 75 6d 62 65 72 2c 20 77 65 20 68 61 76 65 3a 0a  umber, we have:.
2ea23 20 20 20 20 20 20 2a 2a 20 49 4d 50 3a 20 52 2d        ** IMP: R-
2ea24 35 37 33 32 36 2d 33 31 35 34 31 20 41 62 73 28  57326-31541 Abs(
2ea25 58 29 20 72 65 74 75 72 6e 20 30 2e 30 20 69 66  X) return 0.0 if
2ea26 20 58 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f   X is a string o
2ea27 72 20 62 6c 6f 62 20 74 68 61 74 0a 20 20 20 20  r blob that.    
2ea28 20 20 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63    ** cannot be c
2ea29 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 20 6e 75  onverted to a nu
2ea2a 6d 65 72 69 63 20 76 61 6c 75 65 2e 20 0a 20 20  meric value. .  
2ea2b 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 75      */.      dou
2ea2c 62 6c 65 20 72 56 61 6c 20 3d 20 73 71 6c 69 74  ble rVal = sqlit
2ea2d 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
2ea2e 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20  argv[0]);.      
2ea2f 69 66 28 20 72 56 61 6c 3c 30 20 29 20 72 56 61  if( rVal<0 ) rVa
2ea30 6c 20 3d 20 2d 72 56 61 6c 3b 0a 20 20 20 20 20  l = -rVal;.     
2ea31 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2ea32 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20  double(context, 
2ea33 72 56 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  rVal);.      bre
2ea34 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  ak;.    }.  }.}.
2ea35 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
2ea36 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 69 6e 73  ation of the ins
2ea37 74 72 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  tr() function..*
2ea38 2a 0a 2a 2a 20 69 6e 73 74 72 28 68 61 79 73 74  *.** instr(hayst
2ea39 61 63 6b 2c 6e 65 65 64 6c 65 29 20 66 69 6e 64  ack,needle) find
2ea3a 73 20 74 68 65 20 66 69 72 73 74 20 6f 63 63 75  s the first occu
2ea3b 72 72 65 6e 63 65 20 6f 66 20 6e 65 65 64 6c 65  rrence of needle
2ea3c 0a 2a 2a 20 69 6e 20 68 61 79 73 74 61 63 6b 20  .** in haystack 
2ea3d 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 65 20  and returns the 
2ea3e 6e 75 6d 62 65 72 20 6f 66 20 70 72 65 76 69 6f  number of previo
2ea3f 75 73 20 63 68 61 72 61 63 74 65 72 73 20 70 6c  us characters pl
2ea40 75 73 20 31 2c 0a 2a 2a 20 6f 72 20 30 20 69 66  us 1,.** or 0 if
2ea41 20 6e 65 65 64 6c 65 20 64 6f 65 73 20 6e 6f 74   needle does not
2ea42 20 6f 63 63 75 72 20 77 69 74 68 69 6e 20 68 61   occur within ha
2ea43 79 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66  ystack..**.** If
2ea44 20 62 6f 74 68 20 68 61 79 73 74 61 63 6b 20 61   both haystack a
2ea45 6e 64 20 6e 65 65 64 6c 65 20 61 72 65 20 42 4c  nd needle are BL
2ea46 4f 42 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65  OBs, then the re
2ea47 73 75 6c 74 20 69 73 20 6f 6e 65 20 6d 6f 72 65  sult is one more
2ea48 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d   than.** the num
2ea49 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
2ea4a 68 61 79 73 74 61 63 6b 20 70 72 69 6f 72 20 74  haystack prior t
2ea4b 6f 20 74 68 65 20 66 69 72 73 74 20 6f 63 63 75  o the first occu
2ea4c 72 72 65 6e 63 65 20 6f 66 20 6e 65 65 64 6c 65  rrence of needle
2ea4d 2c 0a 2a 2a 20 6f 72 20 30 20 69 66 20 6e 65 65  ,.** or 0 if nee
2ea4e 64 6c 65 20 6e 65 76 65 72 20 6f 63 63 75 72 73  dle never occurs
2ea4f 20 69 6e 20 68 61 79 73 74 61 63 6b 2e 0a 2a 2f   in haystack..*/
2ea50 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 73  .static void ins
2ea51 74 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  trFunc(.  sqlite
2ea52 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
2ea53 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
2ea54 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
2ea55 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73  **argv.){.  cons
2ea56 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
2ea57 2a 7a 48 61 79 73 74 61 63 6b 3b 0a 20 20 63 6f  *zHaystack;.  co
2ea58 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
2ea59 72 20 2a 7a 4e 65 65 64 6c 65 3b 0a 20 20 69 6e  r *zNeedle;.  in
2ea5a 74 20 6e 48 61 79 73 74 61 63 6b 3b 0a 20 20 69  t nHaystack;.  i
2ea5b 6e 74 20 6e 4e 65 65 64 6c 65 3b 0a 20 20 69 6e  nt nNeedle;.  in
2ea5c 74 20 74 79 70 65 48 61 79 73 74 61 63 6b 2c 20  t typeHaystack, 
2ea5d 74 79 70 65 4e 65 65 64 6c 65 3b 0a 20 20 69 6e  typeNeedle;.  in
2ea5e 74 20 4e 20 3d 20 31 3b 0a 20 20 69 6e 74 20 69  t N = 1;.  int i
2ea5f 73 54 65 78 74 3b 0a 0a 20 20 55 4e 55 53 45 44  sText;..  UNUSED
2ea60 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29  _PARAMETER(argc)
2ea61 3b 0a 20 20 74 79 70 65 48 61 79 73 74 61 63 6b  ;.  typeHaystack
2ea62 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
2ea63 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a  _type(argv[0]);.
2ea64 20 20 74 79 70 65 4e 65 65 64 6c 65 20 3d 20 73    typeNeedle = s
2ea65 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
2ea66 65 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66  e(argv[1]);.  if
2ea67 28 20 74 79 70 65 48 61 79 73 74 61 63 6b 3d 3d  ( typeHaystack==
2ea68 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 7c 7c 20 74  SQLITE_NULL || t
2ea69 79 70 65 4e 65 65 64 6c 65 3d 3d 53 51 4c 49 54  ypeNeedle==SQLIT
2ea6a 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b  E_NULL ) return;
2ea6b 0a 20 20 6e 48 61 79 73 74 61 63 6b 20 3d 20 73  .  nHaystack = s
2ea6c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
2ea6d 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e  es(argv[0]);.  n
2ea6e 4e 65 65 64 6c 65 20 3d 20 73 71 6c 69 74 65 33  Needle = sqlite3
2ea6f 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
2ea70 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 74 79 70  v[1]);.  if( typ
2ea71 65 48 61 79 73 74 61 63 6b 3d 3d 53 51 4c 49 54  eHaystack==SQLIT
2ea72 45 5f 42 4c 4f 42 20 26 26 20 74 79 70 65 4e 65  E_BLOB && typeNe
2ea73 65 64 6c 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f  edle==SQLITE_BLO
2ea74 42 20 29 7b 0a 20 20 20 20 7a 48 61 79 73 74 61  B ){.    zHaysta
2ea75 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ck = sqlite3_val
2ea76 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29  ue_blob(argv[0])
2ea77 3b 0a 20 20 20 20 7a 4e 65 65 64 6c 65 20 3d 20  ;.    zNeedle = 
2ea78 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
2ea79 6f 62 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  ob(argv[1]);.   
2ea7a 20 69 73 54 65 78 74 20 3d 20 30 3b 0a 20 20 7d   isText = 0;.  }
2ea7b 65 6c 73 65 7b 0a 20 20 20 20 7a 48 61 79 73 74  else{.    zHayst
2ea7c 61 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ack = sqlite3_va
2ea7d 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
2ea7e 29 3b 0a 20 20 20 20 7a 4e 65 65 64 6c 65 20 3d  );.    zNeedle =
2ea7f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2ea80 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  ext(argv[1]);.  
2ea81 20 20 69 73 54 65 78 74 20 3d 20 31 3b 0a 20 20    isText = 1;.  
2ea82 7d 0a 20 20 77 68 69 6c 65 28 20 6e 4e 65 65 64  }.  while( nNeed
2ea83 6c 65 3c 3d 6e 48 61 79 73 74 61 63 6b 20 26 26  le<=nHaystack &&
2ea84 20 6d 65 6d 63 6d 70 28 7a 48 61 79 73 74 61 63   memcmp(zHaystac
2ea85 6b 2c 20 7a 4e 65 65 64 6c 65 2c 20 6e 4e 65 65  k, zNeedle, nNee
2ea86 64 6c 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 4e  dle)!=0 ){.    N
2ea87 2b 2b 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20  ++;.    do{.    
2ea88 20 20 6e 48 61 79 73 74 61 63 6b 2d 2d 3b 0a 20    nHaystack--;. 
2ea89 20 20 20 20 20 7a 48 61 79 73 74 61 63 6b 2b 2b       zHaystack++
2ea8a 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 69 73  ;.    }while( is
2ea8b 54 65 78 74 20 26 26 20 28 7a 48 61 79 73 74 61  Text && (zHaysta
2ea8c 63 6b 5b 30 5d 26 30 78 63 30 29 3d 3d 30 78 38  ck[0]&0xc0)==0x8
2ea8d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  0 );.  }.  if( n
2ea8e 4e 65 65 64 6c 65 3e 6e 48 61 79 73 74 61 63 6b  Needle>nHaystack
2ea8f 20 29 20 4e 20 3d 20 30 3b 0a 20 20 73 71 6c 69   ) N = 0;.  sqli
2ea90 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63  te3_result_int(c
2ea91 6f 6e 74 65 78 74 2c 20 4e 29 3b 0a 7d 0a 0a 2f  ontext, N);.}../
2ea92 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
2ea93 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 62 73 74  ion of the subst
2ea94 72 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  r() function..**
2ea95 0a 2a 2a 20 73 75 62 73 74 72 28 78 2c 70 31 2c  .** substr(x,p1,
2ea96 70 32 29 20 20 72 65 74 75 72 6e 73 20 70 32 20  p2)  returns p2 
2ea97 63 68 61 72 61 63 74 65 72 73 20 6f 66 20 78 5b  characters of x[
2ea98 5d 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68  ] beginning with
2ea99 20 70 31 2e 0a 2a 2a 20 70 31 20 69 73 20 31 2d   p1..** p1 is 1-
2ea9a 69 6e 64 65 78 65 64 2e 20 20 53 6f 20 73 75 62  indexed.  So sub
2ea9b 73 74 72 28 78 2c 31 2c 31 29 20 72 65 74 75 72  str(x,1,1) retur
2ea9c 6e 73 20 74 68 65 20 66 69 72 73 74 20 63 68 61  ns the first cha
2ea9d 72 61 63 74 65 72 0a 2a 2a 20 6f 66 20 78 2e 20  racter.** of x. 
2ea9e 20 49 66 20 78 20 69 73 20 74 65 78 74 2c 20 74   If x is text, t
2ea9f 68 65 6e 20 77 65 20 61 63 74 75 61 6c 6c 79 20  hen we actually 
2eaa0 63 6f 75 6e 74 20 55 54 46 2d 38 20 63 68 61 72  count UTF-8 char
2eaa1 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 20 78 20  acters..** If x 
2eaa2 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 65 6e 20  is a blob, then 
2eaa3 77 65 20 63 6f 75 6e 74 20 62 79 74 65 73 2e 0a  we count bytes..
2eaa4 2a 2a 0a 2a 2a 20 49 66 20 70 31 20 69 73 20 6e  **.** If p1 is n
2eaa5 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 77 65  egative, then we
2eaa6 20 62 65 67 69 6e 20 61 62 73 28 70 31 29 20 66   begin abs(p1) f
2eaa7 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 78  rom the end of x
2eaa8 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 32 20  []..**.** If p2 
2eaa9 69 73 20 6e 65 67 61 74 69 76 65 2c 20 72 65 74  is negative, ret
2eaaa 75 72 6e 20 74 68 65 20 70 32 20 63 68 61 72 61  urn the p2 chara
2eaab 63 74 65 72 73 20 70 72 65 63 65 65 64 69 6e 67  cters preceeding
2eaac 20 70 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76   p1..*/.static v
2eaad 6f 69 64 20 73 75 62 73 74 72 46 75 6e 63 28 0a  oid substrFunc(.
2eaae 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
2eaaf 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
2eab0 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
2eab1 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
2eab2 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  {.  const unsign
2eab3 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 6f  ed char *z;.  co
2eab4 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
2eab5 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 6c 65 6e  r *z2;.  int len
2eab6 3b 0a 20 20 69 6e 74 20 70 30 74 79 70 65 3b 0a  ;.  int p0type;.
2eab7 20 20 69 36 34 20 70 31 2c 20 70 32 3b 0a 20 20    i64 p1, p2;.  
2eab8 69 6e 74 20 6e 65 67 50 32 20 3d 20 30 3b 0a 0a  int negP2 = 0;..
2eab9 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
2eaba 33 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a  3 || argc==2 );.
2eabb 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
2eabc 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31 5d  lue_type(argv[1]
2eabd 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 0a 20  )==SQLITE_NULL. 
2eabe 20 20 7c 7c 20 28 61 72 67 63 3d 3d 33 20 26 26    || (argc==3 &&
2eabf 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2eac0 79 70 65 28 61 72 67 76 5b 32 5d 29 3d 3d 53 51  ype(argv[2])==SQ
2eac1 4c 49 54 45 5f 4e 55 4c 4c 29 0a 20 20 29 7b 0a  LITE_NULL).  ){.
2eac2 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2eac3 20 20 70 30 74 79 70 65 20 3d 20 73 71 6c 69 74    p0type = sqlit
2eac4 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
2eac5 67 76 5b 30 5d 29 3b 0a 20 20 70 31 20 3d 20 73  gv[0]);.  p1 = s
2eac6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
2eac7 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28  (argv[1]);.  if(
2eac8 20 70 30 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f   p0type==SQLITE_
2eac9 42 4c 4f 42 20 29 7b 0a 20 20 20 20 6c 65 6e 20  BLOB ){.    len 
2eaca 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
2eacb 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
2eacc 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f      z = sqlite3_
2eacd 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b  value_blob(argv[
2eace 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d  0]);.    if( z==
2eacf 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
2ead0 61 73 73 65 72 74 28 20 6c 65 6e 3d 3d 73 71 6c  assert( len==sql
2ead1 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
2ead2 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 7d  (argv[0]) );.  }
2ead3 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71  else{.    z = sq
2ead4 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2ead5 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69  (argv[0]);.    i
2ead6 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( z==0 ) return
2ead7 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20  ;.    len = 0;. 
2ead8 20 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20     if( p1<0 ){. 
2ead9 20 20 20 20 20 66 6f 72 28 7a 32 3d 7a 3b 20 2a       for(z2=z; *
2eada 7a 32 3b 20 6c 65 6e 2b 2b 29 7b 0a 20 20 20 20  z2; len++){.    
2eadb 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f      SQLITE_SKIP_
2eadc 55 54 46 38 28 7a 32 29 3b 0a 20 20 20 20 20 20  UTF8(z2);.      
2eadd 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  }.    }.  }.  if
2eade 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20  ( argc==3 ){.   
2eadf 20 70 32 20 3d 20 73 71 6c 69 74 65 33 5f 76 61   p2 = sqlite3_va
2eae0 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29  lue_int(argv[2])
2eae1 3b 0a 20 20 20 20 69 66 28 20 70 32 3c 30 20 29  ;.    if( p2<0 )
2eae2 7b 0a 20 20 20 20 20 20 70 32 20 3d 20 2d 70 32  {.      p2 = -p2
2eae3 3b 0a 20 20 20 20 20 20 6e 65 67 50 32 20 3d 20  ;.      negP2 = 
2eae4 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  1;.    }.  }else
2eae5 7b 0a 20 20 20 20 70 32 20 3d 20 73 71 6c 69 74  {.    p2 = sqlit
2eae6 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
2eae7 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e 61  ndle(context)->a
2eae8 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
2eae9 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a  IT_LENGTH];.  }.
2eaea 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20    if( p1<0 ){.  
2eaeb 20 20 70 31 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20    p1 += len;.   
2eaec 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 20   if( p1<0 ){.   
2eaed 20 20 20 70 32 20 2b 3d 20 70 31 3b 0a 20 20 20     p2 += p1;.   
2eaee 20 20 20 69 66 28 20 70 32 3c 30 20 29 20 70 32     if( p2<0 ) p2
2eaef 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 31 20 3d   = 0;.      p1 =
2eaf0 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73   0;.    }.  }els
2eaf1 65 20 69 66 28 20 70 31 3e 30 20 29 7b 0a 20 20  e if( p1>0 ){.  
2eaf2 20 20 70 31 2d 2d 3b 0a 20 20 7d 65 6c 73 65 20    p1--;.  }else 
2eaf3 69 66 28 20 70 32 3e 30 20 29 7b 0a 20 20 20 20  if( p2>0 ){.    
2eaf4 70 32 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  p2--;.  }.  if( 
2eaf5 6e 65 67 50 32 20 29 7b 0a 20 20 20 20 70 31 20  negP2 ){.    p1 
2eaf6 2d 3d 20 70 32 3b 0a 20 20 20 20 69 66 28 20 70  -= p2;.    if( p
2eaf7 31 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 32 20  1<0 ){.      p2 
2eaf8 2b 3d 20 70 31 3b 0a 20 20 20 20 20 20 70 31 20  += p1;.      p1 
2eaf9 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 0;.    }.  }. 
2eafa 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20 26   assert( p1>=0 &
2eafb 26 20 70 32 3e 3d 30 20 29 3b 0a 20 20 69 66 28  & p2>=0 );.  if(
2eafc 20 70 30 74 79 70 65 21 3d 53 51 4c 49 54 45 5f   p0type!=SQLITE_
2eafd 42 4c 4f 42 20 29 7b 0a 20 20 20 20 77 68 69 6c  BLOB ){.    whil
2eafe 65 28 20 2a 7a 20 26 26 20 70 31 20 29 7b 0a 20  e( *z && p1 ){. 
2eaff 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50       SQLITE_SKIP
2eb00 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20  _UTF8(z);.      
2eb01 70 31 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  p1--;.    }.    
2eb02 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32 20 26 26  for(z2=z; *z2 &&
2eb03 20 70 32 3b 20 70 32 2d 2d 29 7b 0a 20 20 20 20   p2; p2--){.    
2eb04 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54    SQLITE_SKIP_UT
2eb05 46 38 28 7a 32 29 3b 0a 20 20 20 20 7d 0a 20 20  F8(z2);.    }.  
2eb06 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2eb07 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28  _text(context, (
2eb08 63 68 61 72 2a 29 7a 2c 20 28 69 6e 74 29 28 7a  char*)z, (int)(z
2eb09 32 2d 7a 29 2c 20 53 51 4c 49 54 45 5f 54 52 41  2-z), SQLITE_TRA
2eb0a 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65  NSIENT);.  }else
2eb0b 7b 0a 20 20 20 20 69 66 28 20 70 31 2b 70 32 3e  {.    if( p1+p2>
2eb0c 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 70 32 20  len ){.      p2 
2eb0d 3d 20 6c 65 6e 2d 70 31 3b 0a 20 20 20 20 20 20  = len-p1;.      
2eb0e 69 66 28 20 70 32 3c 30 20 29 20 70 32 20 3d 20  if( p2<0 ) p2 = 
2eb0f 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  0;.    }.    sql
2eb10 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
2eb11 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a  (context, (char*
2eb12 29 26 7a 5b 70 31 5d 2c 20 28 69 6e 74 29 70 32  )&z[p1], (int)p2
2eb13 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
2eb14 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  NT);.  }.}../*.*
2eb15 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
2eb16 20 6f 66 20 74 68 65 20 72 6f 75 6e 64 28 29 20   of the round() 
2eb17 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 23 69 66 6e  function.*/.#ifn
2eb18 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2eb19 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 73  FLOATING_POINT.s
2eb1a 74 61 74 69 63 20 76 6f 69 64 20 72 6f 75 6e 64  tatic void round
2eb1b 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e  Func(sqlite3_con
2eb1c 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69  text *context, i
2eb1d 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
2eb1e 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a  _value **argv){.
2eb1f 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 64    int n = 0;.  d
2eb20 6f 75 62 6c 65 20 72 3b 0a 20 20 63 68 61 72 20  ouble r;.  char 
2eb21 2a 7a 42 75 66 3b 0a 20 20 61 73 73 65 72 74 28  *zBuf;.  assert(
2eb22 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63   argc==1 || argc
2eb23 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 61 72 67  ==2 );.  if( arg
2eb24 63 3d 3d 32 20 29 7b 0a 20 20 20 20 69 66 28 20  c==2 ){.    if( 
2eb25 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c  SQLITE_NULL==sql
2eb26 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
2eb27 61 72 67 76 5b 31 5d 29 20 29 20 72 65 74 75 72  argv[1]) ) retur
2eb28 6e 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74  n;.    n = sqlit
2eb29 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67  e3_value_int(arg
2eb2a 76 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e  v[1]);.    if( n
2eb2b 3e 33 30 20 29 20 6e 20 3d 20 33 30 3b 0a 20 20  >30 ) n = 30;.  
2eb2c 20 20 69 66 28 20 6e 3c 30 20 29 20 6e 20 3d 20    if( n<0 ) n = 
2eb2d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  0;.  }.  if( sql
2eb2e 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
2eb2f 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45  argv[0])==SQLITE
2eb30 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a  _NULL ) return;.
2eb31 20 20 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    r = sqlite3_va
2eb32 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b  lue_double(argv[
2eb33 30 5d 29 3b 0a 20 20 2f 2a 20 49 66 20 59 3d 3d  0]);.  /* If Y==
2eb34 30 20 61 6e 64 20 58 20 77 69 6c 6c 20 66 69 74  0 and X will fit
2eb35 20 69 6e 20 61 20 36 34 2d 62 69 74 20 69 6e 74   in a 64-bit int
2eb36 2c 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 74 68  ,.  ** handle th
2eb37 65 20 72 6f 75 6e 64 69 6e 67 20 64 69 72 65 63  e rounding direc
2eb38 74 6c 79 2c 0a 20 20 2a 2a 20 6f 74 68 65 72 77  tly,.  ** otherw
2eb39 69 73 65 20 75 73 65 20 70 72 69 6e 74 66 2e 0a  ise use printf..
2eb3a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 3d 3d 30 20    */.  if( n==0 
2eb3b 26 26 20 72 3e 3d 30 20 26 26 20 72 3c 4c 41 52  && r>=0 && r<LAR
2eb3c 47 45 53 54 5f 49 4e 54 36 34 2d 31 20 29 7b 0a  GEST_INT64-1 ){.
2eb3d 20 20 20 20 72 20 3d 20 28 64 6f 75 62 6c 65 29      r = (double)
2eb3e 28 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 28  ((sqlite_int64)(
2eb3f 72 2b 30 2e 35 29 29 3b 0a 20 20 7d 65 6c 73 65  r+0.5));.  }else
2eb40 20 69 66 28 20 6e 3d 3d 30 20 26 26 20 72 3c 30   if( n==0 && r<0
2eb41 20 26 26 20 28 2d 72 29 3c 4c 41 52 47 45 53 54   && (-r)<LARGEST
2eb42 5f 49 4e 54 36 34 2d 31 20 29 7b 0a 20 20 20 20  _INT64-1 ){.    
2eb43 72 20 3d 20 2d 28 64 6f 75 62 6c 65 29 28 28 73  r = -(double)((s
2eb44 71 6c 69 74 65 5f 69 6e 74 36 34 29 28 28 2d 72  qlite_int64)((-r
2eb45 29 2b 30 2e 35 29 29 3b 0a 20 20 7d 65 6c 73 65  )+0.5));.  }else
2eb46 7b 0a 20 20 20 20 7a 42 75 66 20 3d 20 73 71 6c  {.    zBuf = sql
2eb47 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 2e  ite3_mprintf("%.
2eb48 2a 66 22 2c 6e 2c 72 29 3b 0a 20 20 20 20 69 66  *f",n,r);.    if
2eb49 28 20 7a 42 75 66 3d 3d 30 20 29 7b 0a 20 20 20  ( zBuf==0 ){.   
2eb4a 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
2eb4b 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f  t_error_nomem(co
2eb4c 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 72 65  ntext);.      re
2eb4d 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
2eb4e 73 71 6c 69 74 65 33 41 74 6f 46 28 7a 42 75 66  sqlite3AtoF(zBuf
2eb4f 2c 20 26 72 2c 20 73 71 6c 69 74 65 33 53 74 72  , &r, sqlite3Str
2eb50 6c 65 6e 33 30 28 7a 42 75 66 29 2c 20 53 51 4c  len30(zBuf), SQL
2eb51 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 73  ITE_UTF8);.    s
2eb52 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 42 75 66  qlite3_free(zBuf
2eb53 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
2eb54 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63  _result_double(c
2eb55 6f 6e 74 65 78 74 2c 20 72 29 3b 0a 7d 0a 23 65  ontext, r);.}.#e
2eb56 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  ndif../*.** Allo
2eb57 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 73  cate nByte bytes
2eb58 20 6f 66 20 73 70 61 63 65 20 75 73 69 6e 67 20   of space using 
2eb59 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
2eb5a 2e 20 49 66 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f  . If the.** allo
2eb5b 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 63 61  cation fails, ca
2eb5c 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ll sqlite3_resul
2eb5d 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 29 20  t_error_nomem() 
2eb5e 74 6f 20 6e 6f 74 69 66 79 0a 2a 2a 20 74 68 65  to notify.** the
2eb5f 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
2eb60 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68   that malloc() h
2eb61 61 73 20 66 61 69 6c 65 64 20 61 6e 64 20 72 65  as failed and re
2eb62 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66  turn NULL..** If
2eb63 20 6e 42 79 74 65 20 69 73 20 6c 61 72 67 65 72   nByte is larger
2eb64 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75   than the maximu
2eb65 6d 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62  m string or blob
2eb66 20 6c 65 6e 67 74 68 2c 20 74 68 65 6e 0a 2a 2a   length, then.**
2eb67 20 72 61 69 73 65 20 61 6e 20 53 51 4c 49 54 45   raise an SQLITE
2eb68 5f 54 4f 4f 42 49 47 20 65 78 63 65 70 74 69 6f  _TOOBIG exceptio
2eb69 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c  n and return NUL
2eb6a 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  L..*/.static voi
2eb6b 64 20 2a 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63  d *contextMalloc
2eb6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2eb6d 20 2a 63 6f 6e 74 65 78 74 2c 20 69 36 34 20 6e   *context, i64 n
2eb6e 42 79 74 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a  Byte){.  char *z
2eb6f 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
2eb70 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
2eb71 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
2eb72 65 78 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ext);.  assert( 
2eb73 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20 74 65 73  nByte>0 );.  tes
2eb74 74 63 61 73 65 28 20 6e 42 79 74 65 3d 3d 64 62  tcase( nByte==db
2eb75 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
2eb76 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b  LIMIT_LENGTH] );
2eb77 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 42 79  .  testcase( nBy
2eb78 74 65 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  te==db->aLimit[S
2eb79 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
2eb7a 54 48 5d 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e  TH]+1 );.  if( n
2eb7b 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  Byte>db->aLimit[
2eb7c 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
2eb7d 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69  GTH] ){.    sqli
2eb7e 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2eb7f 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29  _toobig(context)
2eb80 3b 0a 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20 7d  ;.    z = 0;.  }
2eb81 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71  else{.    z = sq
2eb82 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 28 69 6e 74  lite3Malloc((int
2eb83 29 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28  )nByte);.    if(
2eb84 20 21 7a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c   !z ){.      sql
2eb85 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2eb86 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29  r_nomem(context)
2eb87 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
2eb88 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn z;.}../*.**
2eb89 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
2eb8a 6f 66 20 74 68 65 20 75 70 70 65 72 28 29 20 61  of the upper() a
2eb8b 6e 64 20 6c 6f 77 65 72 28 29 20 53 51 4c 20 66  nd lower() SQL f
2eb8c 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61  unctions..*/.sta
2eb8d 74 69 63 20 76 6f 69 64 20 75 70 70 65 72 46 75  tic void upperFu
2eb8e 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nc(sqlite3_conte
2eb8f 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74  xt *context, int
2eb90 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
2eb91 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20  alue **argv){.  
2eb92 63 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73  char *z1;.  cons
2eb93 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e  t char *z2;.  in
2eb94 74 20 69 2c 20 6e 3b 0a 20 20 55 4e 55 53 45 44  t i, n;.  UNUSED
2eb95 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29  _PARAMETER(argc)
2eb96 3b 0a 20 20 7a 32 20 3d 20 28 63 68 61 72 2a 29  ;.  z2 = (char*)
2eb97 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2eb98 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e  xt(argv[0]);.  n
2eb99 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
2eb9a 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b  _bytes(argv[0]);
2eb9b 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61  .  /* Verify tha
2eb9c 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5f 62  t the call to _b
2eb9d 79 74 65 73 28 29 20 64 6f 65 73 20 6e 6f 74 20  ytes() does not 
2eb9e 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 5f  invalidate the _
2eb9f 74 65 78 74 28 29 20 70 6f 69 6e 74 65 72 20 2a  text() pointer *
2eba0 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 32 3d 3d  /.  assert( z2==
2eba1 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76  (char*)sqlite3_v
2eba2 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
2eba3 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 32 20 29  ]) );.  if( z2 )
2eba4 7b 0a 20 20 20 20 7a 31 20 3d 20 63 6f 6e 74 65  {.    z1 = conte
2eba5 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74  xtMalloc(context
2eba6 2c 20 28 28 69 36 34 29 6e 29 2b 31 29 3b 0a 20  , ((i64)n)+1);. 
2eba7 20 20 20 69 66 28 20 7a 31 20 29 7b 0a 20 20 20     if( z1 ){.   
2eba8 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b     for(i=0; i<n;
2eba9 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a   i++){.        z
2ebaa 31 5b 69 5d 20 3d 20 28 63 68 61 72 29 73 71 6c  1[i] = (char)sql
2ebab 69 74 65 33 54 6f 75 70 70 65 72 28 7a 32 5b 69  ite3Toupper(z2[i
2ebac 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ]);.      }.    
2ebad 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2ebae 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a  _text(context, z
2ebaf 31 2c 20 6e 2c 20 73 71 6c 69 74 65 33 5f 66 72  1, n, sqlite3_fr
2ebb0 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ee);.    }.  }.}
2ebb1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 77  .static void low
2ebb2 65 72 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63  erFunc(sqlite3_c
2ebb3 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
2ebb4 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
2ebb5 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29  e3_value **argv)
2ebb6 7b 0a 20 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20  {.  char *z1;.  
2ebb7 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a  const char *z2;.
2ebb8 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 55 4e    int i, n;.  UN
2ebb9 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
2ebba 72 67 63 29 3b 0a 20 20 7a 32 20 3d 20 28 63 68  rgc);.  z2 = (ch
2ebbb 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
2ebbc 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
2ebbd 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76  .  n = sqlite3_v
2ebbe 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
2ebbf 30 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 69 66 79  0]);.  /* Verify
2ebc0 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20 74   that the call t
2ebc1 6f 20 5f 62 79 74 65 73 28 29 20 64 6f 65 73 20  o _bytes() does 
2ebc2 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20 74  not invalidate t
2ebc3 68 65 20 5f 74 65 78 74 28 29 20 70 6f 69 6e 74  he _text() point
2ebc4 65 72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  er */.  assert( 
2ebc5 7a 32 3d 3d 28 63 68 61 72 2a 29 73 71 6c 69 74  z2==(char*)sqlit
2ebc6 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
2ebc7 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20  gv[0]) );.  if( 
2ebc8 7a 32 20 29 7b 0a 20 20 20 20 7a 31 20 3d 20 63  z2 ){.    z1 = c
2ebc9 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e  ontextMalloc(con
2ebca 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2b 31  text, ((i64)n)+1
2ebcb 29 3b 0a 20 20 20 20 69 66 28 20 7a 31 20 29 7b  );.    if( z1 ){
2ebcc 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
2ebcd 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  i<n; i++){.     
2ebce 20 20 20 7a 31 5b 69 5d 20 3d 20 73 71 6c 69 74     z1[i] = sqlit
2ebcf 65 33 54 6f 6c 6f 77 65 72 28 7a 32 5b 69 5d 29  e3Tolower(z2[i])
2ebd0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2ebd1 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
2ebd2 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 31 2c  ext(context, z1,
2ebd3 20 6e 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65   n, sqlite3_free
2ebd4 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
2ebd5 2f 2a 0a 2a 2a 20 54 68 65 20 43 4f 41 4c 45 53  /*.** The COALES
2ebd6 43 45 28 29 20 61 6e 64 20 49 46 4e 55 4c 4c 28  CE() and IFNULL(
2ebd7 29 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  ) functions are 
2ebd8 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 56  implemented as V
2ebd9 44 42 45 20 63 6f 64 65 20 73 6f 0a 2a 2a 20 74  DBE code so.** t
2ebda 68 61 74 20 75 6e 75 73 65 64 20 61 72 67 75 6d  hat unused argum
2ebdb 65 6e 74 20 76 61 6c 75 65 73 20 64 6f 20 6e 6f  ent values do no
2ebdc 74 20 68 61 76 65 20 74 6f 20 62 65 20 63 6f 6d  t have to be com
2ebdd 70 75 74 65 64 2e 20 20 48 6f 77 65 76 65 72 2c  puted.  However,
2ebde 20 77 65 0a 2a 2a 20 73 74 69 6c 6c 20 6e 65 65   we.** still nee
2ebdf 64 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 66  d some kind of f
2ebe0 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  unction implemen
2ebe1 74 61 74 69 6f 6e 20 66 6f 72 20 74 68 69 73 20  tation for this 
2ebe2 72 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a 20 74  routines in.** t
2ebe3 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 61 62 6c  he function tabl
2ebe4 65 2e 20 20 54 68 61 74 20 66 75 6e 63 74 69 6f  e.  That functio
2ebe5 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
2ebe6 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 63   will never be c
2ebe7 61 6c 6c 65 64 0a 2a 2a 20 73 6f 20 69 74 20 64  alled.** so it d
2ebe8 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 77 68  oesn't matter wh
2ebe9 61 74 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  at the implement
2ebea 61 74 69 6f 6e 20 69 73 2e 20 20 57 65 20 6d 69  ation is.  We mi
2ebeb 67 68 74 20 61 73 20 77 65 6c 6c 20 75 73 65 0a  ght as well use.
2ebec 2a 2a 20 74 68 65 20 22 76 65 72 73 69 6f 6e 28  ** the "version(
2ebed 29 22 20 66 75 6e 63 74 69 6f 6e 20 61 73 20 61  )" function as a
2ebee 20 73 75 62 73 74 69 74 75 74 65 2e 0a 2a 2f 0a   substitute..*/.
2ebef 23 64 65 66 69 6e 65 20 69 66 6e 75 6c 6c 46 75  #define ifnullFu
2ebf0 6e 63 20 76 65 72 73 69 6f 6e 46 75 6e 63 20 20  nc versionFunc  
2ebf1 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 66   /* Substitute f
2ebf2 75 6e 63 74 69 6f 6e 20 2d 20 6e 65 76 65 72 20  unction - never 
2ebf3 63 61 6c 6c 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  called */../*.**
2ebf4 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
2ebf5 6f 66 20 72 61 6e 64 6f 6d 28 29 2e 20 20 52 65  of random().  Re
2ebf6 74 75 72 6e 20 61 20 72 61 6e 64 6f 6d 20 69 6e  turn a random in
2ebf7 74 65 67 65 72 2e 20 20 0a 2a 2f 0a 73 74 61 74  teger.  .*/.stat
2ebf8 69 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d 46 75  ic void randomFu
2ebf9 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
2ebfa 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
2ebfb 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
2ebfc 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2ebfd 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73  *NotUsed2.){.  s
2ebfe 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 3b 0a 20  qlite_int64 r;. 
2ebff 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
2ec00 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
2ec01 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33  sed2);.  sqlite3
2ec02 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65  _randomness(size
2ec03 6f 66 28 72 29 2c 20 26 72 29 3b 0a 20 20 69 66  of(r), &r);.  if
2ec04 28 20 72 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20  ( r<0 ){.    /* 
2ec05 57 65 20 6e 65 65 64 20 74 6f 20 70 72 65 76 65  We need to preve
2ec06 6e 74 20 61 20 72 61 6e 64 6f 6d 20 6e 75 6d 62  nt a random numb
2ec07 65 72 20 6f 66 20 30 78 38 30 30 30 30 30 30 30  er of 0x80000000
2ec08 30 30 30 30 30 30 30 30 20 0a 20 20 20 20 2a 2a  00000000 .    **
2ec09 20 28 6f 72 20 2d 39 32 32 33 33 37 32 30 33 36   (or -9223372036
2ec0a 38 35 34 37 37 35 38 30 38 29 20 73 69 6e 63 65  854775808) since
2ec0b 20 77 68 65 6e 20 79 6f 75 20 64 6f 20 61 62 73   when you do abs
2ec0c 28 29 20 6f 66 20 74 68 61 74 0a 20 20 20 20 2a  () of that.    *
2ec0d 2a 20 6e 75 6d 62 65 72 20 6f 66 20 79 6f 75 20  * number of you 
2ec0e 67 65 74 20 74 68 65 20 73 61 6d 65 20 76 61 6c  get the same val
2ec0f 75 65 20 62 61 63 6b 20 61 67 61 69 6e 2e 20 20  ue back again.  
2ec10 54 6f 20 64 6f 20 74 68 69 73 0a 20 20 20 20 2a  To do this.    *
2ec11 2a 20 69 6e 20 61 20 77 61 79 20 74 68 61 74 20  * in a way that 
2ec12 69 73 20 74 65 73 74 61 62 6c 65 2c 20 6d 61 73  is testable, mas
2ec13 6b 20 74 68 65 20 73 69 67 6e 20 62 69 74 20 6f  k the sign bit o
2ec14 66 66 20 6f 66 20 6e 65 67 61 74 69 76 65 0a 20  ff of negative. 
2ec15 20 20 20 2a 2a 20 76 61 6c 75 65 73 2c 20 72 65     ** values, re
2ec16 73 75 6c 74 69 6e 67 20 69 6e 20 61 20 70 6f 73  sulting in a pos
2ec17 69 74 69 76 65 20 76 61 6c 75 65 2e 20 20 54 68  itive value.  Th
2ec18 65 6e 20 74 61 6b 65 20 74 68 65 20 0a 20 20 20  en take the .   
2ec19 20 2a 2a 20 32 73 20 63 6f 6d 70 6c 65 6d 65 6e   ** 2s complemen
2ec1a 74 20 6f 66 20 74 68 61 74 20 70 6f 73 69 74 69  t of that positi
2ec1b 76 65 20 76 61 6c 75 65 2e 20 20 54 68 65 20 65  ve value.  The e
2ec1c 6e 64 20 72 65 73 75 6c 74 20 63 61 6e 0a 20 20  nd result can.  
2ec1d 20 20 2a 2a 20 74 68 65 72 65 66 6f 72 65 20 62    ** therefore b
2ec1e 65 20 6e 6f 20 6c 65 73 73 20 74 68 61 6e 20 2d  e no less than -
2ec1f 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35  9223372036854775
2ec20 38 30 37 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  807..    */.    
2ec21 72 20 3d 20 2d 28 72 20 26 20 4c 41 52 47 45 53  r = -(r & LARGES
2ec22 54 5f 49 4e 54 36 34 29 3b 0a 20 20 7d 0a 20 20  T_INT64);.  }.  
2ec23 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
2ec24 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 72 29  nt64(context, r)
2ec25 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
2ec26 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e  mentation of ran
2ec27 64 6f 6d 62 6c 6f 62 28 4e 29 2e 20 20 52 65 74  domblob(N).  Ret
2ec28 75 72 6e 20 61 20 72 61 6e 64 6f 6d 20 62 6c 6f  urn a random blo
2ec29 62 0a 2a 2a 20 74 68 61 74 20 69 73 20 4e 20 62  b.** that is N b
2ec2a 79 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2f 0a 73 74  ytes long..*/.st
2ec2b 61 74 69 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d  atic void random
2ec2c 42 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f  Blob(.  sqlite3_
2ec2d 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2ec2e 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
2ec2f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2ec30 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b  argv.){.  int n;
2ec31 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
2ec32 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 61   *p;.  assert( a
2ec33 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53  rgc==1 );.  UNUS
2ec34 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67  ED_PARAMETER(arg
2ec35 63 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65  c);.  n = sqlite
2ec36 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76  3_value_int(argv
2ec37 5b 30 5d 29 3b 0a 20 20 69 66 28 20 6e 3c 31 20  [0]);.  if( n<1 
2ec38 29 7b 0a 20 20 20 20 6e 20 3d 20 31 3b 0a 20 20  ){.    n = 1;.  
2ec39 7d 0a 20 20 70 20 3d 20 63 6f 6e 74 65 78 74 4d  }.  p = contextM
2ec3a 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 6e  alloc(context, n
2ec3b 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  );.  if( p ){.  
2ec3c 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d    sqlite3_random
2ec3d 6e 65 73 73 28 6e 2c 20 70 29 3b 0a 20 20 20 20  ness(n, p);.    
2ec3e 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
2ec3f 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 63 68  lob(context, (ch
2ec40 61 72 2a 29 70 2c 20 6e 2c 20 73 71 6c 69 74 65  ar*)p, n, sqlite
2ec41 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a  3_free);.  }.}..
2ec42 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
2ec43 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61 73 74  tion of the last
2ec44 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20  _insert_rowid() 
2ec45 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  SQL function.  T
2ec46 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c  he return.** val
2ec47 75 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ue is the same a
2ec48 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 61  s the sqlite3_la
2ec49 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
2ec4a 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a  ) API function..
2ec4b 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  */.static void l
2ec4c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
2ec4d 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
2ec4e 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20  ext *context, . 
2ec4f 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0a 20   int NotUsed, . 
2ec50 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2ec51 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73  *NotUsed2.){.  s
2ec52 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
2ec53 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
2ec54 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
2ec55 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
2ec56 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
2ec57 74 55 73 65 64 32 29 3b 0a 20 20 2f 2a 20 49 4d  tUsed2);.  /* IM
2ec58 50 3a 20 52 2d 35 31 35 31 33 2d 31 32 30 32 36  P: R-51513-12026
2ec59 20 54 68 65 20 6c 61 73 74 5f 69 6e 73 65 72 74   The last_insert
2ec5a 5f 72 6f 77 69 64 28 29 20 53 51 4c 20 66 75 6e  _rowid() SQL fun
2ec5b 63 74 69 6f 6e 20 69 73 20 61 0a 20 20 2a 2a 20  ction is a.  ** 
2ec5c 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74  wrapper around t
2ec5d 68 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  he sqlite3_last_
2ec5e 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 43  insert_rowid() C
2ec5f 2f 43 2b 2b 20 69 6e 74 65 72 66 61 63 65 0a 20  /C++ interface. 
2ec60 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f   ** function. */
2ec61 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
2ec62 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c  t_int64(context,
2ec63 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e   sqlite3_last_in
2ec64 73 65 72 74 5f 72 6f 77 69 64 28 64 62 29 29 3b  sert_rowid(db));
2ec65 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
2ec66 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
2ec67 63 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75  changes() SQL fu
2ec68 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4d  nction..**.** IM
2ec69 50 3a 20 52 2d 36 32 30 37 33 2d 31 31 32 30 39  P: R-62073-11209
2ec6a 20 54 68 65 20 63 68 61 6e 67 65 73 28 29 20 53   The changes() S
2ec6b 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  QL function is a
2ec6c 20 77 72 61 70 70 65 72 0a 2a 2a 20 61 72 6f 75   wrapper.** arou
2ec6d 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  nd the sqlite3_c
2ec6e 68 61 6e 67 65 73 28 29 20 43 2f 43 2b 2b 20 66  hanges() C/C++ f
2ec6f 75 6e 63 74 69 6f 6e 20 61 6e 64 20 68 65 6e 63  unction and henc
2ec70 65 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 73 61  e follows the sa
2ec71 6d 65 0a 2a 2a 20 72 75 6c 65 73 20 66 6f 72 20  me.** rules for 
2ec72 63 6f 75 6e 74 69 6e 67 20 63 68 61 6e 67 65 73  counting changes
2ec73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2ec74 20 63 68 61 6e 67 65 73 28 0a 20 20 73 71 6c 69   changes(.  sqli
2ec75 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
2ec76 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55  text,.  int NotU
2ec77 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  sed,.  sqlite3_v
2ec78 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a  alue **NotUsed2.
2ec79 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
2ec7a 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
2ec7b 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
2ec7c 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f  text);.  UNUSED_
2ec7d 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
2ec7e 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20  ed, NotUsed2);. 
2ec7f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2ec80 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c  int(context, sql
2ec81 69 74 65 33 5f 63 68 61 6e 67 65 73 28 64 62 29  ite3_changes(db)
2ec82 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  );.}../*.** Impl
2ec83 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
2ec84 65 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28  e total_changes(
2ec85 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20  ) SQL function. 
2ec86 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
2ec87 65 20 69 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65  e is.** the same
2ec88 20 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f   as the sqlite3_
2ec89 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20  total_changes() 
2ec8a 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  API function..*/
2ec8b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 6f 74  .static void tot
2ec8c 61 6c 5f 63 68 61 6e 67 65 73 28 0a 20 20 73 71  al_changes(.  sq
2ec8d 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
2ec8e 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f  ontext,.  int No
2ec8f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33  tUsed,.  sqlite3
2ec90 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64  _value **NotUsed
2ec91 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  2.){.  sqlite3 *
2ec92 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
2ec93 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
2ec94 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45  ontext);.  UNUSE
2ec95 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74  D_PARAMETER2(Not
2ec96 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b  Used, NotUsed2);
2ec97 0a 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 35 32 37  .  /* IMP: R-527
2ec98 35 36 2d 34 31 39 39 33 20 54 68 69 73 20 66 75  56-41993 This fu
2ec99 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72 61 70  nction is a wrap
2ec9a 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 0a 20  per around the. 
2ec9b 20 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 74 61   ** sqlite3_tota
2ec9c 6c 5f 63 68 61 6e 67 65 73 28 29 20 43 2f 43 2b  l_changes() C/C+
2ec9d 2b 20 69 6e 74 65 72 66 61 63 65 2e 20 2a 2f 0a  + interface. */.
2ec9e 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2ec9f 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71  _int(context, sq
2eca0 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e  lite3_total_chan
2eca1 67 65 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a  ges(db));.}../*.
2eca2 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 64  ** A structure d
2eca3 65 66 69 6e 69 6e 67 20 68 6f 77 20 74 6f 20 64  efining how to d
2eca4 6f 20 47 4c 4f 42 2d 73 74 79 6c 65 20 63 6f 6d  o GLOB-style com
2eca5 70 61 72 69 73 6f 6e 73 2e 0a 2a 2f 0a 73 74 72  parisons..*/.str
2eca6 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  uct compareInfo 
2eca7 7b 0a 20 20 75 38 20 6d 61 74 63 68 41 6c 6c 3b  {.  u8 matchAll;
2eca8 0a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65 3b 0a  .  u8 matchOne;.
2eca9 20 20 75 38 20 6d 61 74 63 68 53 65 74 3b 0a 20    u8 matchSet;. 
2ecaa 20 75 38 20 6e 6f 43 61 73 65 3b 0a 7d 3b 0a 0a   u8 noCase;.};..
2ecab 2f 2a 0a 2a 2a 20 46 6f 72 20 4c 49 4b 45 20 61  /*.** For LIKE a
2ecac 6e 64 20 47 4c 4f 42 20 6d 61 74 63 68 69 6e 67  nd GLOB matching
2ecad 20 6f 6e 20 45 42 43 44 49 43 20 6d 61 63 68 69   on EBCDIC machi
2ecae 6e 65 73 2c 20 61 73 73 75 6d 65 20 74 68 61 74  nes, assume that
2ecaf 20 65 76 65 72 79 0a 2a 2a 20 63 68 61 72 61 63   every.** charac
2ecb0 74 65 72 20 69 73 20 65 78 61 63 74 6c 79 20 6f  ter is exactly o
2ecb1 6e 65 20 62 79 74 65 20 69 6e 20 73 69 7a 65 2e  ne byte in size.
2ecb2 20 20 41 6c 73 6f 2c 20 61 6c 6c 20 63 68 61 72    Also, all char
2ecb3 61 63 74 65 72 73 20 61 72 65 0a 2a 2a 20 61 62  acters are.** ab
2ecb4 6c 65 20 74 6f 20 70 61 72 74 69 63 69 70 61 74  le to participat
2ecb5 65 20 69 6e 20 75 70 70 65 72 2d 63 61 73 65 2d  e in upper-case-
2ecb6 74 6f 2d 6c 6f 77 65 72 2d 63 61 73 65 20 6d 61  to-lower-case ma
2ecb7 70 70 69 6e 67 73 20 69 6e 20 45 42 43 44 49 43  ppings in EBCDIC
2ecb8 0a 2a 2a 20 77 68 65 72 65 61 73 20 6f 6e 6c 79  .** whereas only
2ecb9 20 63 68 61 72 61 63 74 65 72 73 20 6c 65 73 73   characters less
2ecba 20 74 68 61 6e 20 30 78 38 30 20 64 6f 20 69 6e   than 0x80 do in
2ecbb 20 41 53 43 49 49 2e 0a 2a 2f 0a 23 69 66 20 64   ASCII..*/.#if d
2ecbc 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 42  efined(SQLITE_EB
2ecbd 43 44 49 43 29 0a 23 20 64 65 66 69 6e 65 20 73  CDIC).# define s
2ecbe 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 41  qlite3Utf8Read(A
2ecbf 29 20 20 20 20 28 2a 28 28 2a 41 29 2b 2b 29 29  )    (*((*A)++))
2ecc0 0a 23 20 64 65 66 69 6e 65 20 47 6c 6f 67 55 70  .# define GlogUp
2ecc1 70 65 72 54 6f 4c 6f 77 65 72 28 41 29 20 20 20  perToLower(A)   
2ecc2 41 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72  A = sqlite3Upper
2ecc3 54 6f 4c 6f 77 65 72 5b 41 5d 0a 23 65 6c 73 65  ToLower[A].#else
2ecc4 0a 23 20 64 65 66 69 6e 65 20 47 6c 6f 67 55 70  .# define GlogUp
2ecc5 70 65 72 54 6f 4c 6f 77 65 72 28 41 29 20 20 20  perToLower(A)   
2ecc6 69 66 28 20 21 28 28 41 29 26 7e 30 78 37 66 29  if( !((A)&~0x7f)
2ecc7 20 29 7b 20 41 20 3d 20 73 71 6c 69 74 65 33 55   ){ A = sqlite3U
2ecc8 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41 5d 3b 20  pperToLower[A]; 
2ecc9 7d 0a 23 65 6e 64 69 66 0a 0a 73 74 61 74 69 63  }.#endif..static
2ecca 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f   const struct co
2eccb 6d 70 61 72 65 49 6e 66 6f 20 67 6c 6f 62 49 6e  mpareInfo globIn
2eccc 66 6f 20 3d 20 7b 20 27 2a 27 2c 20 27 3f 27 2c  fo = { '*', '?',
2eccd 20 27 5b 27 2c 20 30 20 7d 3b 0a 2f 2a 20 54 68   '[', 0 };./* Th
2ecce 65 20 63 6f 72 72 65 63 74 20 53 51 4c 2d 39 32  e correct SQL-92
2eccf 20 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 72   behavior is for
2ecd0 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74   the LIKE operat
2ecd1 6f 72 20 74 6f 20 69 67 6e 6f 72 65 0a 2a 2a 20  or to ignore.** 
2ecd2 63 61 73 65 2e 20 20 54 68 75 73 20 20 27 61 27  case.  Thus  'a'
2ecd3 20 4c 49 4b 45 20 27 41 27 20 77 6f 75 6c 64 20   LIKE 'A' would 
2ecd4 62 65 20 74 72 75 65 2e 20 2a 2f 0a 73 74 61 74  be true. */.stat
2ecd5 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  ic const struct 
2ecd6 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b 65  compareInfo like
2ecd7 49 6e 66 6f 4e 6f 72 6d 20 3d 20 7b 20 27 25 27  InfoNorm = { '%'
2ecd8 2c 20 27 5f 27 2c 20 20 20 30 2c 20 31 20 7d 3b  , '_',   0, 1 };
2ecd9 0a 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 43 41  ./* If SQLITE_CA
2ecda 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b  SE_SENSITIVE_LIK
2ecdb 45 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68  E is defined, th
2ecdc 65 6e 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72  en the LIKE oper
2ecdd 61 74 6f 72 0a 2a 2a 20 69 73 20 63 61 73 65 20  ator.** is case 
2ecde 73 65 6e 73 69 74 69 76 65 20 63 61 75 73 69 6e  sensitive causin
2ecdf 67 20 27 61 27 20 4c 49 4b 45 20 27 41 27 20 74  g 'a' LIKE 'A' t
2ece0 6f 20 62 65 20 66 61 6c 73 65 20 2a 2f 0a 73 74  o be false */.st
2ece1 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63  atic const struc
2ece2 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69  t compareInfo li
2ece3 6b 65 49 6e 66 6f 41 6c 74 20 3d 20 7b 20 27 25  keInfoAlt = { '%
2ece4 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 30 20 7d  ', '_',   0, 0 }
2ece5 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  ;../*.** Compare
2ece6 20 74 77 6f 20 55 54 46 2d 38 20 73 74 72 69 6e   two UTF-8 strin
2ece7 67 73 20 66 6f 72 20 65 71 75 61 6c 69 74 79 20  gs for equality 
2ece8 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74 20  where the first 
2ece9 73 74 72 69 6e 67 20 63 61 6e 0a 2a 2a 20 70 6f  string can.** po
2ecea 74 65 6e 74 69 61 6c 6c 79 20 62 65 20 61 20 22  tentially be a "
2eceb 67 6c 6f 62 22 20 65 78 70 72 65 73 73 69 6f 6e  glob" expression
2ecec 2e 20 20 52 65 74 75 72 6e 20 74 72 75 65 20 28  .  Return true (
2eced 31 29 20 69 66 20 74 68 65 79 0a 2a 2a 20 61 72  1) if they.** ar
2ecee 65 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20 66  e the same and f
2ecef 61 6c 73 65 20 28 30 29 20 69 66 20 74 68 65 79  alse (0) if they
2ecf0 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e 0a   are different..
2ecf1 2a 2a 0a 2a 2a 20 47 6c 6f 62 62 69 6e 67 20 72  **.** Globbing r
2ecf2 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ules:.**.**     
2ecf3 20 27 2a 27 20 20 20 20 20 20 20 4d 61 74 63 68   '*'       Match
2ecf4 65 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65 20  es any sequence 
2ecf5 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  of zero or more 
2ecf6 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a  characters..**.*
2ecf7 2a 20 20 20 20 20 20 27 3f 27 20 20 20 20 20 20  *      '?'      
2ecf8 20 4d 61 74 63 68 65 73 20 65 78 61 63 74 6c 79   Matches exactly
2ecf9 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 2e 0a   one character..
2ecfa 2a 2a 0a 2a 2a 20 20 20 20 20 5b 2e 2e 2e 5d 20  **.**     [...] 
2ecfb 20 20 20 20 20 4d 61 74 63 68 65 73 20 6f 6e 65       Matches one
2ecfc 20 63 68 61 72 61 63 74 65 72 20 66 72 6f 6d 20   character from 
2ecfd 74 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69 73  the enclosed lis
2ecfe 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  t of.**         
2ecff 20 20 20 20 20 20 20 63 68 61 72 61 63 74 65 72         character
2ed00 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 5e 2e  s..**.**     [^.
2ed01 2e 2e 5d 20 20 20 20 20 4d 61 74 63 68 65 73 20  ..]     Matches 
2ed02 6f 6e 65 20 63 68 61 72 61 63 74 65 72 20 6e 6f  one character no
2ed03 74 20 69 6e 20 74 68 65 20 65 6e 63 6c 6f 73 65  t in the enclose
2ed04 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 57 69  d list..**.** Wi
2ed05 74 68 20 74 68 65 20 5b 2e 2e 2e 5d 20 61 6e 64  th the [...] and
2ed06 20 5b 5e 2e 2e 2e 5d 20 6d 61 74 63 68 69 6e 67   [^...] matching
2ed07 2c 20 61 20 27 5d 27 20 63 68 61 72 61 63 74 65  , a ']' characte
2ed08 72 20 63 61 6e 20 62 65 20 69 6e 63 6c 75 64 65  r can be include
2ed09 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6c 69 73 74  d.** in the list
2ed0a 20 62 79 20 6d 61 6b 69 6e 67 20 69 74 20 74 68   by making it th
2ed0b 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65  e first characte
2ed0c 72 20 61 66 74 65 72 20 27 5b 27 20 6f 72 20 27  r after '[' or '
2ed0d 5e 27 2e 20 20 41 0a 2a 2a 20 72 61 6e 67 65 20  ^'.  A.** range 
2ed0e 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 63 61  of characters ca
2ed0f 6e 20 62 65 20 73 70 65 63 69 66 69 65 64 20 75  n be specified u
2ed10 73 69 6e 67 20 27 2d 27 2e 20 20 45 78 61 6d 70  sing '-'.  Examp
2ed11 6c 65 3a 0a 2a 2a 20 22 5b 61 2d 7a 5d 22 20 6d  le:.** "[a-z]" m
2ed12 61 74 63 68 65 73 20 61 6e 79 20 73 69 6e 67 6c  atches any singl
2ed13 65 20 6c 6f 77 65 72 2d 63 61 73 65 20 6c 65 74  e lower-case let
2ed14 74 65 72 2e 20 20 54 6f 20 6d 61 74 63 68 20 61  ter.  To match a
2ed15 20 27 2d 27 2c 20 6d 61 6b 65 0a 2a 2a 20 69 74   '-', make.** it
2ed16 20 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 63   the last charac
2ed17 74 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 2e  ter in the list.
2ed18 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2ed19 69 6e 65 20 69 73 20 75 73 75 61 6c 6c 79 20 71  ine is usually q
2ed1a 75 69 63 6b 2c 20 62 75 74 20 63 61 6e 20 62 65  uick, but can be
2ed1b 20 4e 2a 2a 32 20 69 6e 20 74 68 65 20 77 6f 72   N**2 in the wor
2ed1c 73 74 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 48  st case..**.** H
2ed1d 69 6e 74 73 3a 20 74 6f 20 6d 61 74 63 68 20 27  ints: to match '
2ed1e 2a 27 20 6f 72 20 27 3f 27 2c 20 70 75 74 20 74  *' or '?', put t
2ed1f 68 65 6d 20 69 6e 20 22 5b 5d 22 2e 20 20 4c 69  hem in "[]".  Li
2ed20 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
2ed21 20 20 20 20 20 20 20 61 62 63 5b 2a 5d 78 79 7a         abc[*]xyz
2ed22 20 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 20          Matches 
2ed23 22 61 62 63 2a 78 79 7a 22 20 6f 6e 6c 79 0a 2a  "abc*xyz" only.*
2ed24 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 74  /.static int pat
2ed25 74 65 72 6e 43 6f 6d 70 61 72 65 28 0a 20 20 63  ternCompare(.  c
2ed26 6f 6e 73 74 20 75 38 20 2a 7a 50 61 74 74 65 72  onst u8 *zPatter
2ed27 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
2ed28 2f 2a 20 54 68 65 20 67 6c 6f 62 20 70 61 74 74  /* The glob patt
2ed29 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75  ern */.  const u
2ed2a 38 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20 20 20  8 *zString,     
2ed2b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2ed2c 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 6d 70 61   string to compa
2ed2d 72 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 67  re against the g
2ed2e 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  lob */.  const s
2ed2f 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66  truct compareInf
2ed30 6f 20 2a 70 49 6e 66 6f 2c 20 2f 2a 20 49 6e 66  o *pInfo, /* Inf
2ed31 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 68  ormation about h
2ed32 6f 77 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d  ow to do the com
2ed33 70 61 72 65 20 2a 2f 0a 20 20 75 33 32 20 65 73  pare */.  u32 es
2ed34 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
2ed35 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2ed36 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63 74  e escape charact
2ed37 65 72 20 2a 2f 0a 29 7b 0a 20 20 75 33 32 20 63  er */.){.  u32 c
2ed38 2c 20 63 32 3b 0a 20 20 69 6e 74 20 69 6e 76 65  , c2;.  int inve
2ed39 72 74 3b 0a 20 20 69 6e 74 20 73 65 65 6e 3b 0a  rt;.  int seen;.
2ed3a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65 20 3d 20    u8 matchOne = 
2ed3b 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 4f 6e 65 3b  pInfo->matchOne;
2ed3c 0a 20 20 75 38 20 6d 61 74 63 68 41 6c 6c 20 3d  .  u8 matchAll =
2ed3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 41 6c 6c   pInfo->matchAll
2ed3e 3b 0a 20 20 75 38 20 6d 61 74 63 68 53 65 74 20  ;.  u8 matchSet 
2ed3f 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 53 65  = pInfo->matchSe
2ed40 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65 20 3d  t;.  u8 noCase =
2ed41 20 70 49 6e 66 6f 2d 3e 6e 6f 43 61 73 65 3b 20   pInfo->noCase; 
2ed42 0a 20 20 69 6e 74 20 70 72 65 76 45 73 63 61 70  .  int prevEscap
2ed43 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72  e = 0;     /* Tr
2ed44 75 65 20 69 66 20 74 68 65 20 70 72 65 76 69 6f  ue if the previo
2ed45 75 73 20 63 68 61 72 61 63 74 65 72 20 77 61 73  us character was
2ed46 20 27 65 73 63 61 70 65 27 20 2a 2f 0a 0a 20 20   'escape' */..  
2ed47 77 68 69 6c 65 28 20 28 63 20 3d 20 73 71 6c 69  while( (c = sqli
2ed48 74 65 33 55 74 66 38 52 65 61 64 28 26 7a 50 61  te3Utf8Read(&zPa
2ed49 74 74 65 72 6e 29 29 21 3d 30 20 29 7b 0a 20 20  ttern))!=0 ){.  
2ed4a 20 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 41 6c    if( c==matchAl
2ed4b 6c 20 26 26 20 21 70 72 65 76 45 73 63 61 70 65  l && !prevEscape
2ed4c 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   ){.      while(
2ed4d 20 28 63 3d 73 71 6c 69 74 65 33 55 74 66 38 52   (c=sqlite3Utf8R
2ed4e 65 61 64 28 26 7a 50 61 74 74 65 72 6e 29 29 20  ead(&zPattern)) 
2ed4f 3d 3d 20 6d 61 74 63 68 41 6c 6c 0a 20 20 20 20  == matchAll.    
2ed50 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 63 20             || c 
2ed51 3d 3d 20 6d 61 74 63 68 4f 6e 65 20 29 7b 0a 20  == matchOne ){. 
2ed52 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 6d 61         if( c==ma
2ed53 74 63 68 4f 6e 65 20 26 26 20 73 71 6c 69 74 65  tchOne && sqlite
2ed54 33 55 74 66 38 52 65 61 64 28 26 7a 53 74 72 69  3Utf8Read(&zStri
2ed55 6e 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ng)==0 ){.      
2ed56 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2ed57 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2ed58 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29        if( c==0 )
2ed59 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
2ed5a 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20   1;.      }else 
2ed5b 69 66 28 20 63 3d 3d 65 73 63 20 29 7b 0a 20 20  if( c==esc ){.  
2ed5c 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65        c = sqlite
2ed5d 33 55 74 66 38 52 65 61 64 28 26 7a 50 61 74 74  3Utf8Read(&zPatt
2ed5e 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ern);.        if
2ed5f 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( c==0 ){.      
2ed60 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2ed61 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
2ed62 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68  lse if( c==match
2ed63 53 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61  Set ){.        a
2ed64 73 73 65 72 74 28 20 65 73 63 3d 3d 30 20 29 3b  ssert( esc==0 );
2ed65 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
2ed66 20 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49   is GLOB, not LI
2ed67 4b 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73  KE */.        as
2ed68 73 65 72 74 28 20 6d 61 74 63 68 53 65 74 3c 30  sert( matchSet<0
2ed69 78 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27 20 69  x80 );  /* '[' i
2ed6a 73 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20  s a single-byte 
2ed6b 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 20  character */.   
2ed6c 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 74       while( *zSt
2ed6d 72 69 6e 67 20 26 26 20 70 61 74 74 65 72 6e 43  ring && patternC
2ed6e 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65 72 6e  ompare(&zPattern
2ed6f 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c 70 49 6e  [-1],zString,pIn
2ed70 66 6f 2c 65 73 63 29 3d 3d 30 20 29 7b 0a 20 20  fo,esc)==0 ){.  
2ed71 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53          SQLITE_S
2ed72 4b 49 50 5f 55 54 46 38 28 7a 53 74 72 69 6e 67  KIP_UTF8(zString
2ed73 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
2ed74 20 20 20 20 20 72 65 74 75 72 6e 20 2a 7a 53 74       return *zSt
2ed75 72 69 6e 67 21 3d 30 3b 0a 20 20 20 20 20 20 7d  ring!=0;.      }
2ed76 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63  .      while( (c
2ed77 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
2ed78 65 61 64 28 26 7a 53 74 72 69 6e 67 29 29 21 3d  ead(&zString))!=
2ed79 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  0 ){.        if(
2ed7a 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20 20   noCase ){.     
2ed7b 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f       GlogUpperTo
2ed7c 4c 6f 77 65 72 28 63 32 29 3b 0a 20 20 20 20 20  Lower(c2);.     
2ed7d 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f       GlogUpperTo
2ed7e 4c 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20 20  Lower(c);.      
2ed7f 20 20 20 20 77 68 69 6c 65 28 20 63 32 20 21 3d      while( c2 !=
2ed80 20 30 20 26 26 20 63 32 20 21 3d 20 63 20 29 7b   0 && c2 != c ){
2ed81 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 32 20  .            c2 
2ed82 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
2ed83 64 28 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20  d(&zString);.   
2ed84 20 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70 70           GlogUpp
2ed85 65 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b 0a 20  erToLower(c2);. 
2ed86 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2ed87 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2ed88 20 20 20 20 77 68 69 6c 65 28 20 63 32 20 21 3d      while( c2 !=
2ed89 20 30 20 26 26 20 63 32 20 21 3d 20 63 20 29 7b   0 && c2 != c ){
2ed8a 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 32 20  .            c2 
2ed8b 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
2ed8c 64 28 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20  d(&zString);.   
2ed8d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2ed8e 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 63   }.        if( c
2ed8f 32 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  2==0 ) return 0;
2ed90 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 61 74  .        if( pat
2ed91 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a 50 61 74  ternCompare(zPat
2ed92 74 65 72 6e 2c 7a 53 74 72 69 6e 67 2c 70 49 6e  tern,zString,pIn
2ed93 66 6f 2c 65 73 63 29 20 29 20 72 65 74 75 72 6e  fo,esc) ) return
2ed94 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
2ed95 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
2ed96 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74  }else if( c==mat
2ed97 63 68 4f 6e 65 20 26 26 20 21 70 72 65 76 45 73  chOne && !prevEs
2ed98 63 61 70 65 20 29 7b 0a 20 20 20 20 20 20 69 66  cape ){.      if
2ed99 28 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  ( sqlite3Utf8Rea
2ed9a 64 28 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29  d(&zString)==0 )
2ed9b 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
2ed9c 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
2ed9d 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74  }else if( c==mat
2ed9e 63 68 53 65 74 20 29 7b 0a 20 20 20 20 20 20 75  chSet ){.      u
2ed9f 33 32 20 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a  32 prior_c = 0;.
2eda0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 73        assert( es
2eda1 63 3d 3d 30 20 29 3b 20 20 20 20 2f 2a 20 54 68  c==0 );    /* Th
2eda2 69 73 20 6f 6e 6c 79 20 6f 63 63 75 72 73 20 66  is only occurs f
2eda3 6f 72 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b  or GLOB, not LIK
2eda4 45 20 2a 2f 0a 20 20 20 20 20 20 73 65 65 6e 20  E */.      seen 
2eda5 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 76 65 72  = 0;.      inver
2eda6 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 3d  t = 0;.      c =
2eda7 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
2eda8 28 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20  (&zString);.    
2eda9 20 20 69 66 28 20 63 3d 3d 30 20 29 20 72 65 74    if( c==0 ) ret
2edaa 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 63 32 20  urn 0;.      c2 
2edab 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
2edac 64 28 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20  d(&zPattern);.  
2edad 20 20 20 20 69 66 28 20 63 32 3d 3d 27 5e 27 20      if( c2=='^' 
2edae 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 65 72  ){.        inver
2edaf 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63  t = 1;.        c
2edb0 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
2edb1 65 61 64 28 26 7a 50 61 74 74 65 72 6e 29 3b 0a  ead(&zPattern);.
2edb2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2edb3 28 20 63 32 3d 3d 27 5d 27 20 29 7b 0a 20 20 20  ( c2==']' ){.   
2edb4 20 20 20 20 20 69 66 28 20 63 3d 3d 27 5d 27 20       if( c==']' 
2edb5 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20  ) seen = 1;.    
2edb6 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33      c2 = sqlite3
2edb7 55 74 66 38 52 65 61 64 28 26 7a 50 61 74 74 65  Utf8Read(&zPatte
2edb8 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rn);.      }.   
2edb9 20 20 20 77 68 69 6c 65 28 20 63 32 20 26 26 20     while( c2 && 
2edba 63 32 21 3d 27 5d 27 20 29 7b 0a 20 20 20 20 20  c2!=']' ){.     
2edbb 20 20 20 69 66 28 20 63 32 3d 3d 27 2d 27 20 26     if( c2=='-' &
2edbc 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d 27  & zPattern[0]!='
2edbd 5d 27 20 26 26 20 7a 50 61 74 74 65 72 6e 5b 30  ]' && zPattern[0
2edbe 5d 21 3d 30 20 26 26 20 70 72 69 6f 72 5f 63 3e  ]!=0 && prior_c>
2edbf 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63  0 ){.          c
2edc0 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
2edc1 65 61 64 28 26 7a 50 61 74 74 65 72 6e 29 3b 0a  ead(&zPattern);.
2edc2 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 3e            if( c>
2edc3 3d 70 72 69 6f 72 5f 63 20 26 26 20 63 3c 3d 63  =prior_c && c<=c
2edc4 32 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20  2 ) seen = 1;.  
2edc5 20 20 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20          prior_c 
2edc6 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  = 0;.        }el
2edc7 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  se{.          if
2edc8 28 20 63 3d 3d 63 32 20 29 7b 0a 20 20 20 20 20  ( c==c2 ){.     
2edc9 20 20 20 20 20 20 20 73 65 65 6e 20 3d 20 31 3b         seen = 1;
2edca 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
2edcb 20 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d         prior_c =
2edcc 20 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   c2;.        }. 
2edcd 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69         c2 = sqli
2edce 74 65 33 55 74 66 38 52 65 61 64 28 26 7a 50 61  te3Utf8Read(&zPa
2edcf 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a  ttern);.      }.
2edd0 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 30 20        if( c2==0 
2edd1 7c 7c 20 28 73 65 65 6e 20 5e 20 69 6e 76 65 72  || (seen ^ inver
2edd2 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  t)==0 ){.       
2edd3 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
2edd4 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
2edd5 20 65 73 63 3d 3d 63 20 26 26 20 21 70 72 65 76   esc==c && !prev
2edd6 45 73 63 61 70 65 20 29 7b 0a 20 20 20 20 20 20  Escape ){.      
2edd7 70 72 65 76 45 73 63 61 70 65 20 3d 20 31 3b 0a  prevEscape = 1;.
2edd8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2edd9 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66   c2 = sqlite3Utf
2edda 38 52 65 61 64 28 26 7a 53 74 72 69 6e 67 29 3b  8Read(&zString);
2eddb 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61 73  .      if( noCas
2eddc 65 20 29 7b 0a 20 20 20 20 20 20 20 20 47 6c 6f  e ){.        Glo
2eddd 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 29  gUpperToLower(c)
2edde 3b 0a 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70  ;.        GlogUp
2eddf 70 65 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b 0a  perToLower(c2);.
2ede0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2ede1 28 20 63 21 3d 63 32 20 29 7b 0a 20 20 20 20 20  ( c!=c2 ){.     
2ede2 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
2ede3 20 20 20 7d 0a 20 20 20 20 20 20 70 72 65 76 45     }.      prevE
2ede4 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 20 20 7d  scape = 0;.    }
2ede5 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a  .  }.  return *z
2ede6 53 74 72 69 6e 67 3d 3d 30 3b 0a 7d 0a 0a 2f 2a  String==0;.}../*
2ede7 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75  .** Count the nu
2ede8 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68  mber of times th
2ede9 61 74 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72  at the LIKE oper
2edea 61 74 6f 72 20 28 6f 72 20 47 4c 4f 42 20 77 68  ator (or GLOB wh
2edeb 69 63 68 20 69 73 0a 2a 2a 20 6a 75 73 74 20 61  ich is.** just a
2edec 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 20 4c 49   variation of LI
2eded 4b 45 29 20 67 65 74 73 20 63 61 6c 6c 65 64 2e  KE) gets called.
2edee 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 66    This is used f
2edef 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 6f 6e  or testing.** on
2edf0 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ly..*/.#ifdef SQ
2edf1 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
2edf2 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2edf3 5f 6c 69 6b 65 5f 63 6f 75 6e 74 20 3d 20 30 3b  _like_count = 0;
2edf4 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
2edf5 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
2edf6 66 20 74 68 65 20 6c 69 6b 65 28 29 20 53 51 4c  f the like() SQL
2edf7 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73   function.  This
2edf8 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d   function implem
2edf9 65 6e 74 73 0a 2a 2a 20 74 68 65 20 62 75 69 6c  ents.** the buil
2edfa 64 2d 69 6e 20 4c 49 4b 45 20 6f 70 65 72 61 74  d-in LIKE operat
2edfb 6f 72 2e 20 20 54 68 65 20 66 69 72 73 74 20 61  or.  The first a
2edfc 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66  rgument to the f
2edfd 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65 0a 2a  unction is the.*
2edfe 2a 20 70 61 74 74 65 72 6e 20 61 6e 64 20 74 68  * pattern and th
2edff 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
2ee00 74 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 2e  t is the string.
2ee01 20 20 53 6f 2c 20 74 68 65 20 53 51 4c 20 73 74    So, the SQL st
2ee02 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20  atements:.**.** 
2ee03 20 20 20 20 20 20 41 20 4c 49 4b 45 20 42 0a 2a        A LIKE B.*
2ee04 2a 0a 2a 2a 20 69 73 20 69 6d 70 6c 65 6d 65 6e  *.** is implemen
2ee05 74 65 64 20 61 73 20 6c 69 6b 65 28 42 2c 41 29  ted as like(B,A)
2ee06 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 61 6d  ..**.** This sam
2ee07 65 20 66 75 6e 63 74 69 6f 6e 20 28 77 69 74 68  e function (with
2ee08 20 61 20 64 69 66 66 65 72 65 6e 74 20 63 6f 6d   a different com
2ee09 70 61 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75  pareInfo structu
2ee0a 72 65 29 20 63 6f 6d 70 75 74 65 73 0a 2a 2a 20  re) computes.** 
2ee0b 74 68 65 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f  the GLOB operato
2ee0c 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
2ee0d 64 20 6c 69 6b 65 46 75 6e 63 28 0a 20 20 73 71  d likeFunc(.  sq
2ee0e 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
2ee0f 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 20 61  ontext, .  int a
2ee10 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f  rgc, .  sqlite3_
2ee11 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
2ee12 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
2ee13 20 63 68 61 72 20 2a 7a 41 2c 20 2a 7a 42 3b 0a   char *zA, *zB;.
2ee14 20 20 75 33 32 20 65 73 63 61 70 65 20 3d 20 30    u32 escape = 0
2ee15 3b 0a 20 20 69 6e 74 20 6e 50 61 74 3b 0a 20 20  ;.  int nPat;.  
2ee16 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
2ee17 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
2ee18 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
2ee19 3b 0a 0a 20 20 7a 42 20 3d 20 73 71 6c 69 74 65  ;..  zB = sqlite
2ee1a 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
2ee1b 76 5b 30 5d 29 3b 0a 20 20 7a 41 20 3d 20 73 71  v[0]);.  zA = sq
2ee1c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2ee1d 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20 2f 2a  (argv[1]);..  /*
2ee1e 20 4c 69 6d 69 74 20 74 68 65 20 6c 65 6e 67 74   Limit the lengt
2ee1f 68 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f 72  h of the LIKE or
2ee20 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f   GLOB pattern to
2ee21 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a   avoid problems.
2ee22 20 20 2a 2a 20 6f 66 20 64 65 65 70 20 72 65 63    ** of deep rec
2ee23 75 72 73 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62  ursion and N*N b
2ee24 65 68 61 76 69 6f 72 20 69 6e 20 70 61 74 74 65  ehavior in patte
2ee25 72 6e 43 6f 6d 70 61 72 65 28 29 2e 0a 20 20 2a  rnCompare()..  *
2ee26 2f 0a 20 20 6e 50 61 74 20 3d 20 73 71 6c 69 74  /.  nPat = sqlit
2ee27 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
2ee28 72 67 76 5b 30 5d 29 3b 0a 20 20 74 65 73 74 63  rgv[0]);.  testc
2ee29 61 73 65 28 20 6e 50 61 74 3d 3d 64 62 2d 3e 61  ase( nPat==db->a
2ee2a 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
2ee2b 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  IT_LIKE_PATTERN_
2ee2c 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 74 65 73  LENGTH] );.  tes
2ee2d 74 63 61 73 65 28 20 6e 50 61 74 3d 3d 64 62 2d  tcase( nPat==db-
2ee2e 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
2ee2f 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52  IMIT_LIKE_PATTER
2ee30 4e 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20  N_LENGTH]+1 );. 
2ee31 20 69 66 28 20 6e 50 61 74 20 3e 20 64 62 2d 3e   if( nPat > db->
2ee32 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
2ee33 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MIT_LIKE_PATTERN
2ee34 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
2ee35 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2ee36 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c  rror(context, "L
2ee37 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74  IKE or GLOB patt
2ee38 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22  ern too complex"
2ee39 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72  , -1);.    retur
2ee3a 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  n;.  }.  assert(
2ee3b 20 7a 42 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c   zB==sqlite3_val
2ee3c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
2ee3d 20 29 3b 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67   );  /* Encoding
2ee3e 20 64 69 64 20 6e 6f 74 20 63 68 61 6e 67 65 20   did not change 
2ee3f 2a 2f 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d  */..  if( argc==
2ee40 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  3 ){.    /* The 
2ee41 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65 72  escape character
2ee42 20 73 74 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e   string must con
2ee43 73 69 73 74 20 6f 66 20 61 20 73 69 6e 67 6c 65  sist of a single
2ee44 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
2ee45 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69  ..    ** Otherwi
2ee46 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72  se, return an er
2ee47 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ror..    */.    
2ee48 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
2ee49 68 61 72 20 2a 7a 45 73 63 20 3d 20 73 71 6c 69  har *zEsc = sqli
2ee4a 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
2ee4b 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[2]);.    if(
2ee4c 20 7a 45 73 63 3d 3d 30 20 29 20 72 65 74 75 72   zEsc==0 ) retur
2ee4d 6e 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  n;.    if( sqlit
2ee4e 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 28 63  e3Utf8CharLen((c
2ee4f 68 61 72 2a 29 7a 45 73 63 2c 20 2d 31 29 21 3d  har*)zEsc, -1)!=
2ee50 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  1 ){.      sqlit
2ee51 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2ee52 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20  context, .      
2ee53 20 20 20 20 22 45 53 43 41 50 45 20 65 78 70 72      "ESCAPE expr
2ee54 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 61  ession must be a
2ee55 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65   single characte
2ee56 72 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72  r", -1);.      r
2ee57 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
2ee58 20 65 73 63 61 70 65 20 3d 20 73 71 6c 69 74 65   escape = sqlite
2ee59 33 55 74 66 38 52 65 61 64 28 26 7a 45 73 63 29  3Utf8Read(&zEsc)
2ee5a 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 41 20 26  ;.  }.  if( zA &
2ee5b 26 20 7a 42 20 29 7b 0a 20 20 20 20 73 74 72 75  & zB ){.    stru
2ee5c 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a  ct compareInfo *
2ee5d 70 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 5f  pInfo = sqlite3_
2ee5e 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
2ee5f 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  t);.#ifdef SQLIT
2ee60 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74  E_TEST.    sqlit
2ee61 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 2b 2b 3b  e3_like_count++;
2ee62 0a 23 65 6e 64 69 66 0a 20 20 20 20 0a 20 20 20  .#endif.    .   
2ee63 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2ee64 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 61 74  int(context, pat
2ee65 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a 42 2c 20  ternCompare(zB, 
2ee66 7a 41 2c 20 70 49 6e 66 6f 2c 20 65 73 63 61 70  zA, pInfo, escap
2ee67 65 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  e));.  }.}../*.*
2ee68 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
2ee69 20 6f 66 20 74 68 65 20 4e 55 4c 4c 49 46 28 78   of the NULLIF(x
2ee6a 2c 79 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  ,y) function.  T
2ee6b 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 68 65  he result is the
2ee6c 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65   first.** argume
2ee6d 6e 74 20 69 66 20 74 68 65 20 61 72 67 75 6d 65  nt if the argume
2ee6e 6e 74 73 20 61 72 65 20 64 69 66 66 65 72 65 6e  nts are differen
2ee6f 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 69  t.  The result i
2ee70 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 0a 2a 2a  s NULL if the.**
2ee71 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 65   arguments are e
2ee72 71 75 61 6c 20 74 6f 20 65 61 63 68 20 6f 74 68  qual to each oth
2ee73 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
2ee74 69 64 20 6e 75 6c 6c 69 66 46 75 6e 63 28 0a 20  id nullifFunc(. 
2ee75 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2ee76 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
2ee77 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
2ee78 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
2ee79 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  .){.  CollSeq *p
2ee7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65  Coll = sqlite3Ge
2ee7b 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e  tFuncCollSeq(con
2ee7c 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f  text);.  UNUSED_
2ee7d 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
2ee7e 64 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  d);.  if( sqlite
2ee7f 33 4d 65 6d 43 6f 6d 70 61 72 65 28 61 72 67 76  3MemCompare(argv
2ee80 5b 30 5d 2c 20 61 72 67 76 5b 31 5d 2c 20 70 43  [0], argv[1], pC
2ee81 6f 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 73  oll)!=0 ){.    s
2ee82 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61  qlite3_result_va
2ee83 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  lue(context, arg
2ee84 76 5b 30 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  v[0]);.  }.}../*
2ee85 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
2ee86 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
2ee87 5f 76 65 72 73 69 6f 6e 28 29 20 66 75 6e 63 74  _version() funct
2ee88 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c 74  ion.  The result
2ee89 20 69 73 20 74 68 65 20 76 65 72 73 69 6f 6e 0a   is the version.
2ee8a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65  ** of the SQLite
2ee8b 20 6c 69 62 72 61 72 79 20 74 68 61 74 20 69 73   library that is
2ee8c 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61   running..*/.sta
2ee8d 74 69 63 20 76 6f 69 64 20 76 65 72 73 69 6f 6e  tic void version
2ee8e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
2ee8f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2ee90 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c  ,.  int NotUsed,
2ee91 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
2ee92 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20   **NotUsed2.){. 
2ee93 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
2ee94 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
2ee95 73 65 64 32 29 3b 0a 20 20 2f 2a 20 49 4d 50 3a  sed2);.  /* IMP:
2ee96 20 52 2d 34 38 36 39 39 2d 34 38 36 31 37 20 54   R-48699-48617 T
2ee97 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2ee98 61 6e 20 53 51 4c 20 77 72 61 70 70 65 72 20 61  an SQL wrapper a
2ee99 72 6f 75 6e 64 20 74 68 65 0a 20 20 2a 2a 20 73  round the.  ** s
2ee9a 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
2ee9b 6e 28 29 20 43 2d 69 6e 74 65 72 66 61 63 65 2e  n() C-interface.
2ee9c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65   */.  sqlite3_re
2ee9d 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
2ee9e 74 2c 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  t, sqlite3_libve
2ee9f 72 73 69 6f 6e 28 29 2c 20 2d 31 2c 20 53 51 4c  rsion(), -1, SQL
2eea0 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a  ITE_STATIC);.}..
2eea1 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
2eea2 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
2eea3 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 20 66  te_source_id() f
2eea4 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 72 65 73  unction. The res
2eea5 75 6c 74 20 69 73 20 61 20 73 74 72 69 6e 67 0a  ult is a string.
2eea6 2a 2a 20 74 68 61 74 20 69 64 65 6e 74 69 66 69  ** that identifi
2eea7 65 73 20 74 68 65 20 70 61 72 74 69 63 75 6c 61  es the particula
2eea8 72 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  r version of the
2eea9 20 73 6f 75 72 63 65 20 63 6f 64 65 20 75 73 65   source code use
2eeaa 64 20 74 6f 20 62 75 69 6c 64 0a 2a 2a 20 53 51  d to build.** SQ
2eeab 4c 69 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Lite..*/.static 
2eeac 76 6f 69 64 20 73 6f 75 72 63 65 69 64 46 75 6e  void sourceidFun
2eead 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
2eeae 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
2eeaf 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20   int NotUsed,.  
2eeb0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2eeb1 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e  NotUsed2.){.  UN
2eeb2 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
2eeb3 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64  NotUsed, NotUsed
2eeb4 32 29 3b 0a 20 20 2f 2a 20 49 4d 50 3a 20 52 2d  2);.  /* IMP: R-
2eeb5 32 34 34 37 30 2d 33 31 31 33 36 20 54 68 69 73  24470-31136 This
2eeb6 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6e 20   function is an 
2eeb7 53 51 4c 20 77 72 61 70 70 65 72 20 61 72 6f 75  SQL wrapper arou
2eeb8 6e 64 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69  nd the.  ** sqli
2eeb9 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29 20 43  te3_sourceid() C
2eeba 20 69 6e 74 65 72 66 61 63 65 2e 20 2a 2f 0a 20   interface. */. 
2eebb 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2eebc 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 71  text(context, sq
2eebd 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29  lite3_sourceid()
2eebe 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41  , -1, SQLITE_STA
2eebf 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  TIC);.}../*.** I
2eec0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
2eec1 20 74 68 65 20 73 71 6c 69 74 65 5f 6c 6f 67 28   the sqlite_log(
2eec2 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69  ) function.  Thi
2eec3 73 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61  s is a wrapper a
2eec4 72 6f 75 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33  round.** sqlite3
2eec5 5f 6c 6f 67 28 29 2e 20 20 54 68 65 20 72 65 74  _log().  The ret
2eec6 75 72 6e 20 76 61 6c 75 65 20 69 73 20 4e 55 4c  urn value is NUL
2eec7 4c 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e  L.  The function
2eec8 20 65 78 69 73 74 73 20 70 75 72 65 6c 79 20 66   exists purely f
2eec9 6f 72 0a 2a 2a 20 69 74 73 20 73 69 64 65 2d 65  or.** its side-e
2eeca 66 66 65 63 74 73 2e 0a 2a 2f 0a 73 74 61 74 69  ffects..*/.stati
2eecb 63 20 76 6f 69 64 20 65 72 72 6c 6f 67 46 75 6e  c void errlogFun
2eecc 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
2eecd 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
2eece 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
2eecf 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
2eed0 76 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  v.){.  UNUSED_PA
2eed1 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20  RAMETER(argc);. 
2eed2 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
2eed3 52 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71  R(context);.  sq
2eed4 6c 69 74 65 33 5f 6c 6f 67 28 73 71 6c 69 74 65  lite3_log(sqlite
2eed5 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76  3_value_int(argv
2eed6 5b 30 5d 29 2c 20 22 25 73 22 2c 20 73 71 6c 69  [0]), "%s", sqli
2eed7 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
2eed8 72 67 76 5b 31 5d 29 29 3b 0a 7d 0a 0a 2f 2a 0a  rgv[1]));.}../*.
2eed9 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
2eeda 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f  n of the sqlite_
2eedb 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73  compileoption_us
2eedc 65 64 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  ed() function..*
2eedd 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20  * The result is 
2eede 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 20  an integer that 
2eedf 69 64 65 6e 74 69 66 69 65 73 20 69 66 20 74 68  identifies if th
2eee0 65 20 63 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6f  e compiler optio
2eee1 6e 0a 2a 2a 20 77 61 73 20 75 73 65 64 20 74 6f  n.** was used to
2eee2 20 62 75 69 6c 64 20 53 51 4c 69 74 65 2e 0a 2a   build SQLite..*
2eee3 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
2eee4 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54  _OMIT_COMPILEOPT
2eee5 49 4f 4e 5f 44 49 41 47 53 0a 73 74 61 74 69 63  ION_DIAGS.static
2eee6 20 76 6f 69 64 20 63 6f 6d 70 69 6c 65 6f 70 74   void compileopt
2eee7 69 6f 6e 75 73 65 64 46 75 6e 63 28 0a 20 20 73  ionusedFunc(.  s
2eee8 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
2eee9 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
2eeea 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
2eeeb 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
2eeec 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 70   const char *zOp
2eeed 74 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 74 28  tName;.  assert(
2eeee 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e   argc==1 );.  UN
2eeef 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
2eef0 72 67 63 29 3b 0a 20 20 2f 2a 20 49 4d 50 3a 20  rgc);.  /* IMP: 
2eef1 52 2d 33 39 35 36 34 2d 33 36 33 30 35 20 54 68  R-39564-36305 Th
2eef2 65 20 73 71 6c 69 74 65 5f 63 6f 6d 70 69 6c 65  e sqlite_compile
2eef3 6f 70 74 69 6f 6e 5f 75 73 65 64 28 29 20 53 51  option_used() SQ
2eef4 4c 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  L.  ** function 
2eef5 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72 6f  is a wrapper aro
2eef6 75 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f  und the sqlite3_
2eef7 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73  compileoption_us
2eef8 65 64 28 29 20 43 2f 43 2b 2b 0a 20 20 2a 2a 20  ed() C/C++.  ** 
2eef9 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  function..  */. 
2eefa 20 69 66 28 20 28 7a 4f 70 74 4e 61 6d 65 20 3d   if( (zOptName =
2eefb 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71   (const char*)sq
2eefc 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2eefd 28 61 72 67 76 5b 30 5d 29 29 21 3d 30 20 29 7b  (argv[0]))!=0 ){
2eefe 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
2eeff 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c  ult_int(context,
2ef00 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65   sqlite3_compile
2ef01 6f 70 74 69 6f 6e 5f 75 73 65 64 28 7a 4f 70 74  option_used(zOpt
2ef02 4e 61 6d 65 29 29 3b 0a 20 20 7d 0a 7d 0a 23 65  Name));.  }.}.#e
2ef03 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2ef04 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f  MIT_COMPILEOPTIO
2ef05 4e 5f 44 49 41 47 53 20 2a 2f 0a 0a 2f 2a 0a 2a  N_DIAGS */../*.*
2ef06 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
2ef07 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 63   of the sqlite_c
2ef08 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74  ompileoption_get
2ef09 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a  () function. .**
2ef0a 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   The result is a
2ef0b 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 64 65   string that ide
2ef0c 6e 74 69 66 69 65 73 20 74 68 65 20 63 6f 6d 70  ntifies the comp
2ef0d 69 6c 65 72 20 6f 70 74 69 6f 6e 73 20 0a 2a 2a  iler options .**
2ef0e 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 53   used to build S
2ef0f 51 4c 69 74 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65  QLite..*/.#ifnde
2ef10 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f  f SQLITE_OMIT_CO
2ef11 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47  MPILEOPTION_DIAG
2ef12 53 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  S.static void co
2ef13 6d 70 69 6c 65 6f 70 74 69 6f 6e 67 65 74 46 75  mpileoptiongetFu
2ef14 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
2ef15 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
2ef16 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
2ef17 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
2ef18 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20  gv.){.  int n;. 
2ef19 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31   assert( argc==1
2ef1a 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   );.  UNUSED_PAR
2ef1b 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20  AMETER(argc);.  
2ef1c 2f 2a 20 49 4d 50 3a 20 52 2d 30 34 39 32 32 2d  /* IMP: R-04922-
2ef1d 32 34 30 37 36 20 54 68 65 20 73 71 6c 69 74 65  24076 The sqlite
2ef1e 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67  _compileoption_g
2ef1f 65 74 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f  et() SQL functio
2ef20 6e 0a 20 20 2a 2a 20 69 73 20 61 20 77 72 61 70  n.  ** is a wrap
2ef21 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20 73  per around the s
2ef22 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70  qlite3_compileop
2ef23 74 69 6f 6e 5f 67 65 74 28 29 20 43 2f 43 2b 2b  tion_get() C/C++
2ef24 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a   function..  */.
2ef25 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    n = sqlite3_va
2ef26 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 30 5d 29  lue_int(argv[0])
2ef27 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
2ef28 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
2ef29 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65   sqlite3_compile
2ef2a 6f 70 74 69 6f 6e 5f 67 65 74 28 6e 29 2c 20 2d  option_get(n), -
2ef2b 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
2ef2c 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
2ef2d 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49  QLITE_OMIT_COMPI
2ef2e 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53 20 2a  LEOPTION_DIAGS *
2ef2f 2f 0a 0a 2f 2a 20 41 72 72 61 79 20 66 6f 72 20  /../* Array for 
2ef30 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d 20  converting from 
2ef31 68 61 6c 66 2d 62 79 74 65 73 20 28 6e 79 62 62  half-bytes (nybb
2ef32 6c 65 73 29 20 69 6e 74 6f 20 41 53 43 49 49 20  les) into ASCII 
2ef33 68 65 78 0a 2a 2a 20 64 69 67 69 74 73 2e 20 2a  hex.** digits. *
2ef34 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
2ef35 68 61 72 20 68 65 78 64 69 67 69 74 73 5b 5d 20  har hexdigits[] 
2ef36 3d 20 7b 0a 20 20 27 30 27 2c 20 27 31 27 2c 20  = {.  '0', '1', 
2ef37 27 32 27 2c 20 27 33 27 2c 20 27 34 27 2c 20 27  '2', '3', '4', '
2ef38 35 27 2c 20 27 36 27 2c 20 27 37 27 2c 0a 20 20  5', '6', '7',.  
2ef39 27 38 27 2c 20 27 39 27 2c 20 27 41 27 2c 20 27  '8', '9', 'A', '
2ef3a 42 27 2c 20 27 43 27 2c 20 27 44 27 2c 20 27 45  B', 'C', 'D', 'E
2ef3b 27 2c 20 27 46 27 20 0a 7d 3b 0a 0a 2f 2a 0a 2a  ', 'F' .};../*.*
2ef3c 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d  * EXPERIMENTAL -
2ef3d 20 54 68 69 73 20 69 73 20 6e 6f 74 20 61 6e 20   This is not an 
2ef3e 6f 66 66 69 63 69 61 6c 20 66 75 6e 63 74 69 6f  official functio
2ef3f 6e 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63  n.  The interfac
2ef40 65 20 6d 61 79 0a 2a 2a 20 63 68 61 6e 67 65 2e  e may.** change.
2ef41 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20    This function 
2ef42 6d 61 79 20 64 69 73 61 70 70 65 61 72 2e 20 20  may disappear.  
2ef43 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 63 6f 64  Do not write cod
2ef44 65 20 74 68 61 74 20 64 65 70 65 6e 64 73 0a 2a  e that depends.*
2ef45 2a 20 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 69  * on this functi
2ef46 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  on..**.** Implem
2ef47 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
2ef48 51 55 4f 54 45 28 29 20 66 75 6e 63 74 69 6f 6e  QUOTE() function
2ef49 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  .  This function
2ef4a 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 0a   takes a single.
2ef4b 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66  ** argument.  If
2ef4c 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73   the argument is
2ef4d 20 6e 75 6d 65 72 69 63 2c 20 74 68 65 20 72 65   numeric, the re
2ef4e 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68  turn value is th
2ef4f 65 20 73 61 6d 65 20 61 73 0a 2a 2a 20 74 68 65  e same as.** the
2ef50 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 74   argument.  If t
2ef51 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 4e  he argument is N
2ef52 55 4c 4c 2c 20 74 68 65 20 72 65 74 75 72 6e 20  ULL, the return 
2ef53 76 61 6c 75 65 20 69 73 20 74 68 65 20 73 74 72  value is the str
2ef54 69 6e 67 0a 2a 2a 20 22 4e 55 4c 4c 22 2e 20 20  ing.** "NULL".  
2ef55 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61  Otherwise, the a
2ef56 72 67 75 6d 65 6e 74 20 69 73 20 65 6e 63 6c 6f  rgument is enclo
2ef57 73 65 64 20 69 6e 20 73 69 6e 67 6c 65 20 71 75  sed in single qu
2ef58 6f 74 65 73 20 77 69 74 68 0a 2a 2a 20 73 69 6e  otes with.** sin
2ef59 67 6c 65 2d 71 75 6f 74 65 20 65 73 63 61 70 65  gle-quote escape
2ef5a 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
2ef5b 64 20 71 75 6f 74 65 46 75 6e 63 28 73 71 6c 69  d quoteFunc(sqli
2ef5c 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
2ef5d 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
2ef5e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2ef5f 61 72 67 76 29 7b 0a 20 20 61 73 73 65 72 74 28  argv){.  assert(
2ef60 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e   argc==1 );.  UN
2ef61 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
2ef62 72 67 63 29 3b 0a 20 20 73 77 69 74 63 68 28 20  rgc);.  switch( 
2ef63 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
2ef64 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20  pe(argv[0]) ){. 
2ef65 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46     case SQLITE_F
2ef66 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 64 6f  LOAT: {.      do
2ef67 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 20  uble r1, r2;.   
2ef68 20 20 20 63 68 61 72 20 7a 42 75 66 5b 35 30 5d     char zBuf[50]
2ef69 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c  ;.      r1 = sql
2ef6a 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
2ef6b 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20  e(argv[0]);.    
2ef6c 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
2ef6d 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c  tf(sizeof(zBuf),
2ef6e 20 7a 42 75 66 2c 20 22 25 21 2e 31 35 67 22 2c   zBuf, "%!.15g",
2ef6f 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   r1);.      sqli
2ef70 74 65 33 41 74 6f 46 28 7a 42 75 66 2c 20 26 72  te3AtoF(zBuf, &r
2ef71 32 2c 20 32 30 2c 20 53 51 4c 49 54 45 5f 55 54  2, 20, SQLITE_UT
2ef72 46 38 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  F8);.      if( r
2ef73 31 21 3d 72 32 20 29 7b 0a 20 20 20 20 20 20 20  1!=r2 ){.       
2ef74 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
2ef75 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20  f(sizeof(zBuf), 
2ef76 7a 42 75 66 2c 20 22 25 21 2e 32 30 65 22 2c 20  zBuf, "%!.20e", 
2ef77 72 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  r1);.      }.   
2ef78 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
2ef79 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
2ef7a 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45  zBuf, -1, SQLITE
2ef7b 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20  _TRANSIENT);.   
2ef7c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2ef7d 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2ef7e 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20  INTEGER: {.     
2ef7f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2ef80 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61  value(context, a
2ef81 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 62  rgv[0]);.      b
2ef82 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2ef83 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42  case SQLITE_BLOB
2ef84 3a 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  : {.      char *
2ef85 7a 54 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20  zText = 0;.     
2ef86 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 42 6c   char const *zBl
2ef87 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ob = sqlite3_val
2ef88 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29  ue_blob(argv[0])
2ef89 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 6c 6f  ;.      int nBlo
2ef8a 62 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  b = sqlite3_valu
2ef8b 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
2ef8c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2ef8d 7a 42 6c 6f 62 3d 3d 73 71 6c 69 74 65 33 5f 76  zBlob==sqlite3_v
2ef8e 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30  alue_blob(argv[0
2ef8f 5d 29 20 29 3b 20 2f 2a 20 4e 6f 20 65 6e 63 6f  ]) ); /* No enco
2ef90 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20  ding change */. 
2ef91 20 20 20 20 20 7a 54 65 78 74 20 3d 20 28 63 68       zText = (ch
2ef92 61 72 20 2a 29 63 6f 6e 74 65 78 74 4d 61 6c 6c  ar *)contextMall
2ef93 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 32 2a 28  oc(context, (2*(
2ef94 69 36 34 29 6e 42 6c 6f 62 29 2b 34 29 3b 20 0a  i64)nBlob)+4); .
2ef95 20 20 20 20 20 20 69 66 28 20 7a 54 65 78 74 20        if( zText 
2ef96 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
2ef97 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  ;.        for(i=
2ef98 30 3b 20 69 3c 6e 42 6c 6f 62 3b 20 69 2b 2b 29  0; i<nBlob; i++)
2ef99 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 78  {.          zTex
2ef9a 74 5b 28 69 2a 32 29 2b 32 5d 20 3d 20 68 65 78  t[(i*2)+2] = hex
2ef9b 64 69 67 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d  digits[(zBlob[i]
2ef9c 3e 3e 34 29 26 30 78 30 46 5d 3b 0a 20 20 20 20  >>4)&0x0F];.    
2ef9d 20 20 20 20 20 20 7a 54 65 78 74 5b 28 69 2a 32        zText[(i*2
2ef9e 29 2b 33 5d 20 3d 20 68 65 78 64 69 67 69 74 73  )+3] = hexdigits
2ef9f 5b 28 7a 42 6c 6f 62 5b 69 5d 29 26 30 78 30 46  [(zBlob[i])&0x0F
2efa0 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ];.        }.   
2efa1 20 20 20 20 20 7a 54 65 78 74 5b 28 6e 42 6c 6f       zText[(nBlo
2efa2 62 2a 32 29 2b 32 5d 20 3d 20 27 5c 27 27 3b 0a  b*2)+2] = '\'';.
2efa3 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28 6e          zText[(n
2efa4 42 6c 6f 62 2a 32 29 2b 33 5d 20 3d 20 27 5c 30  Blob*2)+3] = '\0
2efa5 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74  ';.        zText
2efa6 5b 30 5d 20 3d 20 27 58 27 3b 0a 20 20 20 20 20  [0] = 'X';.     
2efa7 20 20 20 7a 54 65 78 74 5b 31 5d 20 3d 20 27 5c     zText[1] = '\
2efa8 27 27 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  '';.        sqli
2efa9 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
2efaa 63 6f 6e 74 65 78 74 2c 20 7a 54 65 78 74 2c 20  context, zText, 
2efab 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  -1, SQLITE_TRANS
2efac 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 20 20 73  IENT);.        s
2efad 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65 78  qlite3_free(zTex
2efae 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
2efaf 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2efb0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54     case SQLITE_T
2efb1 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  EXT: {.      int
2efb2 20 69 2c 6a 3b 0a 20 20 20 20 20 20 75 36 34 20   i,j;.      u64 
2efb3 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75  n;.      const u
2efb4 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41  nsigned char *zA
2efb5 72 67 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  rg = sqlite3_val
2efb6 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
2efb7 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 3b  ;.      char *z;
2efb8 0a 0a 20 20 20 20 20 20 69 66 28 20 7a 41 72 67  ..      if( zArg
2efb9 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
2efba 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 6e 3d 30      for(i=0, n=0
2efbb 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b  ; zArg[i]; i++){
2efbc 20 69 66 28 20 7a 41 72 67 5b 69 5d 3d 3d 27 5c   if( zArg[i]=='\
2efbd 27 27 20 29 20 6e 2b 2b 3b 20 7d 0a 20 20 20 20  '' ) n++; }.    
2efbe 20 20 7a 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c    z = contextMal
2efbf 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69  loc(context, ((i
2efc0 36 34 29 69 29 2b 28 28 69 36 34 29 6e 29 2b 33  64)i)+((i64)n)+3
2efc1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29  );.      if( z )
2efc2 7b 0a 20 20 20 20 20 20 20 20 7a 5b 30 5d 20 3d  {.        z[0] =
2efc3 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 66   '\'';.        f
2efc4 6f 72 28 69 3d 30 2c 20 6a 3d 31 3b 20 7a 41 72  or(i=0, j=1; zAr
2efc5 67 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  g[i]; i++){.    
2efc6 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a        z[j++] = z
2efc7 41 72 67 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20  Arg[i];.        
2efc8 20 20 69 66 28 20 7a 41 72 67 5b 69 5d 3d 3d 27    if( zArg[i]=='
2efc9 5c 27 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  \'' ){.         
2efca 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 27     z[j++] = '\''
2efcb 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
2efcc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2efcd 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20  z[j++] = '\'';. 
2efce 20 20 20 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b         z[j] = 0;
2efcf 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2efd0 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
2efd1 74 65 78 74 2c 20 7a 2c 20 6a 2c 20 73 71 6c 69  text, z, j, sqli
2efd2 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 20  te3_free);.     
2efd3 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
2efd4 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
2efd5 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  t: {.      asser
2efd6 74 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t( sqlite3_value
2efd7 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d  _type(argv[0])==
2efd8 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 3b 0a 20  SQLITE_NULL );. 
2efd9 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
2efda 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
2efdb 2c 20 22 4e 55 4c 4c 22 2c 20 34 2c 20 53 51 4c  , "NULL", 4, SQL
2efdc 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
2efdd 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2efde 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
2efdf 20 68 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 2e   hex() function.
2efe0 20 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20    Interpret the 
2efe1 61 72 67 75 6d 65 6e 74 20 61 73 20 61 20 62 6c  argument as a bl
2efe2 6f 62 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61  ob.  Return.** a
2efe3 20 68 65 78 61 64 65 63 69 6d 61 6c 20 72 65 6e   hexadecimal ren
2efe4 64 65 72 69 6e 67 20 61 73 20 74 65 78 74 2e 0a  dering as text..
2efe5 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68  */.static void h
2efe6 65 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  exFunc(.  sqlite
2efe7 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
2efe8 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
2efe9 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
2efea 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20  **argv.){.  int 
2efeb 69 2c 20 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e  i, n;.  const un
2efec 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 42 6c  signed char *pBl
2efed 6f 62 3b 0a 20 20 63 68 61 72 20 2a 7a 48 65 78  ob;.  char *zHex
2efee 2c 20 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28 20  , *z;.  assert( 
2efef 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55  argc==1 );.  UNU
2eff0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72  SED_PARAMETER(ar
2eff1 67 63 29 3b 0a 20 20 70 42 6c 6f 62 20 3d 20 73  gc);.  pBlob = s
2eff2 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
2eff3 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20  b(argv[0]);.  n 
2eff4 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
2eff5 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
2eff6 20 20 61 73 73 65 72 74 28 20 70 42 6c 6f 62 3d    assert( pBlob=
2eff7 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  =sqlite3_value_b
2eff8 6c 6f 62 28 61 72 67 76 5b 30 5d 29 20 29 3b 20  lob(argv[0]) ); 
2eff9 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20   /* No encoding 
2effa 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a 20 3d 20  change */.  z = 
2effb 7a 48 65 78 20 3d 20 63 6f 6e 74 65 78 74 4d 61  zHex = contextMa
2effc 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28  lloc(context, ((
2effd 69 36 34 29 6e 29 2a 32 20 2b 20 31 29 3b 0a 20  i64)n)*2 + 1);. 
2effe 20 69 66 28 20 7a 48 65 78 20 29 7b 0a 20 20 20   if( zHex ){.   
2efff 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69   for(i=0; i<n; i
2f000 2b 2b 2c 20 70 42 6c 6f 62 2b 2b 29 7b 0a 20 20  ++, pBlob++){.  
2f001 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
2f002 72 20 63 20 3d 20 2a 70 42 6c 6f 62 3b 0a 20 20  r c = *pBlob;.  
2f003 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 78      *(z++) = hex
2f004 64 69 67 69 74 73 5b 28 63 3e 3e 34 29 26 30 78  digits[(c>>4)&0x
2f005 66 5d 3b 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29  f];.      *(z++)
2f006 20 3d 20 68 65 78 64 69 67 69 74 73 5b 63 26 30   = hexdigits[c&0
2f007 78 66 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a  xf];.    }.    *
2f008 7a 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  z = 0;.    sqlit
2f009 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
2f00a 6f 6e 74 65 78 74 2c 20 7a 48 65 78 2c 20 6e 2a  ontext, zHex, n*
2f00b 32 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29  2, sqlite3_free)
2f00c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
2f00d 68 65 20 7a 65 72 6f 62 6c 6f 62 28 4e 29 20 66  he zeroblob(N) f
2f00e 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
2f00f 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 62 6c  a zero-filled bl
2f010 6f 62 20 6f 66 20 73 69 7a 65 20 4e 20 62 79 74  ob of size N byt
2f011 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  es..*/.static vo
2f012 69 64 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 28  id zeroblobFunc(
2f013 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
2f014 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
2f015 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
2f016 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
2f017 29 7b 0a 20 20 69 36 34 20 6e 3b 0a 20 20 73 71  ){.  i64 n;.  sq
2f018 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
2f019 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
2f01a 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a  andle(context);.
2f01b 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
2f01c 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  1 );.  UNUSED_PA
2f01d 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20  RAMETER(argc);. 
2f01e 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   n = sqlite3_val
2f01f 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d  ue_int64(argv[0]
2f020 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e  );.  testcase( n
2f021 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ==db->aLimit[SQL
2f022 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
2f023 5d 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  ] );.  testcase(
2f024 20 6e 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53   n==db->aLimit[S
2f025 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
2f026 54 48 5d 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e  TH]+1 );.  if( n
2f027 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
2f028 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
2f029 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2f02a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
2f02b 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  big(context);.  
2f02c 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
2f02d 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c  e3_result_zerobl
2f02e 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 69 6e 74  ob(context, (int
2f02f 29 6e 29 3b 20 2f 2a 20 49 4d 50 3a 20 52 2d 30  )n); /* IMP: R-0
2f030 30 32 39 33 2d 36 34 39 39 34 20 2a 2f 0a 20 20  0293-64994 */.  
2f031 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72  }.}../*.** The r
2f032 65 70 6c 61 63 65 28 29 20 66 75 6e 63 74 69 6f  eplace() functio
2f033 6e 2e 20 20 54 68 72 65 65 20 61 72 67 75 6d 65  n.  Three argume
2f034 6e 74 73 20 61 72 65 20 61 6c 6c 20 73 74 72 69  nts are all stri
2f035 6e 67 73 3a 20 63 61 6c 6c 0a 2a 2a 20 74 68 65  ngs: call.** the
2f036 6d 20 41 2c 20 42 2c 20 61 6e 64 20 43 2e 20 54  m A, B, and C. T
2f037 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 73  he result is als
2f038 6f 20 61 20 73 74 72 69 6e 67 20 77 68 69 63 68  o a string which
2f039 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a 20 66   is derived.** f
2f03a 72 6f 6d 20 41 20 62 79 20 72 65 70 6c 61 63 69  rom A by replaci
2f03b 6e 67 20 65 76 65 72 79 20 6f 63 63 75 72 61 6e  ng every occuran
2f03c 63 65 20 6f 66 20 42 20 77 69 74 68 20 43 2e 20  ce of B with C. 
2f03d 20 54 68 65 20 6d 61 74 63 68 0a 2a 2a 20 6d 75   The match.** mu
2f03e 73 74 20 62 65 20 65 78 61 63 74 2e 20 20 43 6f  st be exact.  Co
2f03f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2f040 73 20 61 72 65 20 6e 6f 74 20 75 73 65 64 2e 0a  s are not used..
2f041 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
2f042 65 70 6c 61 63 65 46 75 6e 63 28 0a 20 20 73 71  eplaceFunc(.  sq
2f043 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
2f044 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
2f045 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
2f046 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
2f047 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
2f048 68 61 72 20 2a 7a 53 74 72 3b 20 20 20 20 20 20  har *zStr;      
2f049 20 20 2f 2a 20 54 68 65 20 69 6e 70 75 74 20 73    /* The input s
2f04a 74 72 69 6e 67 20 41 20 2a 2f 0a 20 20 63 6f 6e  tring A */.  con
2f04b 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
2f04c 20 2a 7a 50 61 74 74 65 72 6e 3b 20 20 20 20 2f   *zPattern;    /
2f04d 2a 20 54 68 65 20 70 61 74 74 65 72 6e 20 73 74  * The pattern st
2f04e 72 69 6e 67 20 42 20 2a 2f 0a 20 20 63 6f 6e 73  ring B */.  cons
2f04f 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
2f050 2a 7a 52 65 70 3b 20 20 20 20 20 20 20 20 2f 2a  *zRep;        /*
2f051 20 54 68 65 20 72 65 70 6c 61 63 65 6d 65 6e 74   The replacement
2f052 20 73 74 72 69 6e 67 20 43 20 2a 2f 0a 20 20 75   string C */.  u
2f053 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f  nsigned char *zO
2f054 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
2f055 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 2a   /* The output *
2f056 2f 0a 20 20 69 6e 74 20 6e 53 74 72 3b 20 20 20  /.  int nStr;   
2f057 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f058 53 69 7a 65 20 6f 66 20 7a 53 74 72 20 2a 2f 0a  Size of zStr */.
2f059 20 20 69 6e 74 20 6e 50 61 74 74 65 72 6e 3b 20    int nPattern; 
2f05a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
2f05b 7a 65 20 6f 66 20 7a 50 61 74 74 65 72 6e 20 2a  ze of zPattern *
2f05c 2f 0a 20 20 69 6e 74 20 6e 52 65 70 3b 20 20 20  /.  int nRep;   
2f05d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f05e 53 69 7a 65 20 6f 66 20 7a 52 65 70 20 2a 2f 0a  Size of zRep */.
2f05f 20 20 69 36 34 20 6e 4f 75 74 3b 20 20 20 20 20    i64 nOut;     
2f060 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
2f061 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 7a 4f  ximum size of zO
2f062 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 6f 70  ut */.  int loop
2f063 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20  Limit;          
2f064 20 2f 2a 20 4c 61 73 74 20 7a 53 74 72 5b 5d 20   /* Last zStr[] 
2f065 74 68 61 74 20 6d 69 67 68 74 20 6d 61 74 63 68  that might match
2f066 20 7a 50 61 74 74 65 72 6e 5b 5d 20 2a 2f 0a 20   zPattern[] */. 
2f067 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20   int i, j;      
2f068 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
2f069 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 0a 20  p counters */.. 
2f06a 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 33   assert( argc==3
2f06b 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   );.  UNUSED_PAR
2f06c 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20  AMETER(argc);.  
2f06d 7a 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 76  zStr = sqlite3_v
2f06e 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
2f06f 5d 29 3b 0a 20 20 69 66 28 20 7a 53 74 72 3d 3d  ]);.  if( zStr==
2f070 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e 53  0 ) return;.  nS
2f071 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  tr = sqlite3_val
2f072 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
2f073 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53 74  );.  assert( zSt
2f074 72 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r==sqlite3_value
2f075 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29  _text(argv[0]) )
2f076 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e  ;  /* No encodin
2f077 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a 50  g change */.  zP
2f078 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65 33  attern = sqlite3
2f079 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
2f07a 5b 31 5d 29 3b 0a 20 20 69 66 28 20 7a 50 61 74  [1]);.  if( zPat
2f07b 74 65 72 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 61  tern==0 ){.    a
2f07c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76  ssert( sqlite3_v
2f07d 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31  alue_type(argv[1
2f07e 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 0a  ])==SQLITE_NULL.
2f07f 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73              || s
2f080 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
2f081 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
2f082 29 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  )->mallocFailed 
2f083 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
2f084 20 7d 0a 20 20 69 66 28 20 7a 50 61 74 74 65 72   }.  if( zPatter
2f085 6e 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 61  n[0]==0 ){.    a
2f086 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76  ssert( sqlite3_v
2f087 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31  alue_type(argv[1
2f088 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])!=SQLITE_NULL 
2f089 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
2f08a 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74  esult_value(cont
2f08b 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20  ext, argv[0]);. 
2f08c 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
2f08d 20 6e 50 61 74 74 65 72 6e 20 3d 20 73 71 6c 69   nPattern = sqli
2f08e 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
2f08f 61 72 67 76 5b 31 5d 29 3b 0a 20 20 61 73 73 65  argv[1]);.  asse
2f090 72 74 28 20 7a 50 61 74 74 65 72 6e 3d 3d 73 71  rt( zPattern==sq
2f091 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2f092 28 61 72 67 76 5b 31 5d 29 20 29 3b 20 20 2f 2a  (argv[1]) );  /*
2f093 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61   No encoding cha
2f094 6e 67 65 20 2a 2f 0a 20 20 7a 52 65 70 20 3d 20  nge */.  zRep = 
2f095 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2f096 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 69  xt(argv[2]);.  i
2f097 66 28 20 7a 52 65 70 3d 3d 30 20 29 20 72 65 74  f( zRep==0 ) ret
2f098 75 72 6e 3b 0a 20 20 6e 52 65 70 20 3d 20 73 71  urn;.  nRep = sq
2f099 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
2f09a 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 61 73  s(argv[2]);.  as
2f09b 73 65 72 74 28 20 7a 52 65 70 3d 3d 73 71 6c 69  sert( zRep==sqli
2f09c 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
2f09d 72 67 76 5b 32 5d 29 20 29 3b 0a 20 20 6e 4f 75  rgv[2]) );.  nOu
2f09e 74 20 3d 20 6e 53 74 72 20 2b 20 31 3b 0a 20 20  t = nStr + 1;.  
2f09f 61 73 73 65 72 74 28 20 6e 4f 75 74 3c 53 51 4c  assert( nOut<SQL
2f0a0 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29  ITE_MAX_LENGTH )
2f0a1 3b 0a 20 20 7a 4f 75 74 20 3d 20 63 6f 6e 74 65  ;.  zOut = conte
2f0a2 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74  xtMalloc(context
2f0a3 2c 20 28 69 36 34 29 6e 4f 75 74 29 3b 0a 20 20  , (i64)nOut);.  
2f0a4 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20  if( zOut==0 ){. 
2f0a5 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
2f0a6 20 6c 6f 6f 70 4c 69 6d 69 74 20 3d 20 6e 53 74   loopLimit = nSt
2f0a7 72 20 2d 20 6e 50 61 74 74 65 72 6e 3b 20 20 0a  r - nPattern;  .
2f0a8 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 3d    for(i=j=0; i<=
2f0a9 6c 6f 6f 70 4c 69 6d 69 74 3b 20 69 2b 2b 29 7b  loopLimit; i++){
2f0aa 0a 20 20 20 20 69 66 28 20 7a 53 74 72 5b 69 5d  .    if( zStr[i]
2f0ab 21 3d 7a 50 61 74 74 65 72 6e 5b 30 5d 20 7c 7c  !=zPattern[0] ||
2f0ac 20 6d 65 6d 63 6d 70 28 26 7a 53 74 72 5b 69 5d   memcmp(&zStr[i]
2f0ad 2c 20 7a 50 61 74 74 65 72 6e 2c 20 6e 50 61 74  , zPattern, nPat
2f0ae 74 65 72 6e 29 20 29 7b 0a 20 20 20 20 20 20 7a  tern) ){.      z
2f0af 4f 75 74 5b 6a 2b 2b 5d 20 3d 20 7a 53 74 72 5b  Out[j++] = zStr[
2f0b0 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  i];.    }else{. 
2f0b1 20 20 20 20 20 75 38 20 2a 7a 4f 6c 64 3b 0a 20       u8 *zOld;. 
2f0b2 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62       sqlite3 *db
2f0b3 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
2f0b4 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
2f0b5 74 65 78 74 29 3b 0a 20 20 20 20 20 20 6e 4f 75  text);.      nOu
2f0b6 74 20 2b 3d 20 6e 52 65 70 20 2d 20 6e 50 61 74  t += nRep - nPat
2f0b7 74 65 72 6e 3b 0a 20 20 20 20 20 20 74 65 73 74  tern;.      test
2f0b8 63 61 73 65 28 20 6e 4f 75 74 2d 31 3d 3d 64 62  case( nOut-1==db
2f0b9 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
2f0ba 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b  LIMIT_LENGTH] );
2f0bb 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2f0bc 20 6e 4f 75 74 2d 32 3d 3d 64 62 2d 3e 61 4c 69   nOut-2==db->aLi
2f0bd 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
2f0be 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 20 20  _LENGTH] );.    
2f0bf 20 20 69 66 28 20 6e 4f 75 74 2d 31 3e 64 62 2d    if( nOut-1>db-
2f0c0 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
2f0c1 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
2f0c2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2f0c3 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
2f0c4 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  big(context);.  
2f0c5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
2f0c6 65 65 28 7a 4f 75 74 29 3b 0a 20 20 20 20 20 20  ee(zOut);.      
2f0c7 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
2f0c8 7d 0a 20 20 20 20 20 20 7a 4f 6c 64 20 3d 20 7a  }.      zOld = z
2f0c9 4f 75 74 3b 0a 20 20 20 20 20 20 7a 4f 75 74 20  Out;.      zOut 
2f0ca 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
2f0cb 63 28 7a 4f 75 74 2c 20 28 69 6e 74 29 6e 4f 75  c(zOut, (int)nOu
2f0cc 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4f  t);.      if( zO
2f0cd 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ut==0 ){.       
2f0ce 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2f0cf 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74  error_nomem(cont
2f0d0 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ext);.        sq
2f0d1 6c 69 74 65 33 5f 66 72 65 65 28 7a 4f 6c 64 29  lite3_free(zOld)
2f0d2 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2f0d3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2f0d4 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c  memcpy(&zOut[j],
2f0d5 20 7a 52 65 70 2c 20 6e 52 65 70 29 3b 0a 20 20   zRep, nRep);.  
2f0d6 20 20 20 20 6a 20 2b 3d 20 6e 52 65 70 3b 0a 20      j += nRep;. 
2f0d7 20 20 20 20 20 69 20 2b 3d 20 6e 50 61 74 74 65       i += nPatte
2f0d8 72 6e 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn-1;.    }.  }.
2f0d9 20 20 61 73 73 65 72 74 28 20 6a 2b 6e 53 74 72    assert( j+nStr
2f0da 2d 69 2b 31 3d 3d 6e 4f 75 74 20 29 3b 0a 20 20  -i+1==nOut );.  
2f0db 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c  memcpy(&zOut[j],
2f0dc 20 26 7a 53 74 72 5b 69 5d 2c 20 6e 53 74 72 2d   &zStr[i], nStr-
2f0dd 69 29 3b 0a 20 20 6a 20 2b 3d 20 6e 53 74 72 20  i);.  j += nStr 
2f0de 2d 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 6a  - i;.  assert( j
2f0df 3c 3d 6e 4f 75 74 20 29 3b 0a 20 20 7a 4f 75 74  <=nOut );.  zOut
2f0e0 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  [j] = 0;.  sqlit
2f0e1 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
2f0e2 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a  ontext, (char*)z
2f0e3 4f 75 74 2c 20 6a 2c 20 73 71 6c 69 74 65 33 5f  Out, j, sqlite3_
2f0e4 66 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  free);.}../*.** 
2f0e5 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
2f0e6 66 20 74 68 65 20 54 52 49 4d 28 29 2c 20 4c 54  f the TRIM(), LT
2f0e7 52 49 4d 28 29 2c 20 61 6e 64 20 52 54 52 49 4d  RIM(), and RTRIM
2f0e8 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a  () functions..**
2f0e9 20 54 68 65 20 75 73 65 72 64 61 74 61 20 69 73   The userdata is
2f0ea 20 30 78 31 20 66 6f 72 20 6c 65 66 74 20 74 72   0x1 for left tr
2f0eb 69 6d 2c 20 30 78 32 20 66 6f 72 20 72 69 67 68  im, 0x2 for righ
2f0ec 74 20 74 72 69 6d 2c 20 30 78 33 20 66 6f 72 20  t trim, 0x3 for 
2f0ed 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  both..*/.static 
2f0ee 76 6f 69 64 20 74 72 69 6d 46 75 6e 63 28 0a 20  void trimFunc(. 
2f0ef 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2f0f0 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
2f0f1 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
2f0f2 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
2f0f3 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
2f0f4 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20 20 20  d char *zIn;    
2f0f5 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74       /* Input st
2f0f6 72 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ring */.  const 
2f0f7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
2f0f8 43 68 61 72 53 65 74 3b 20 20 20 20 2f 2a 20 53  CharSet;    /* S
2f0f9 65 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  et of characters
2f0fa 20 74 6f 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e   to trim */.  in
2f0fb 74 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 20 20  t nIn;          
2f0fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f0fd 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
2f0fe 65 73 20 69 6e 20 69 6e 70 75 74 20 2a 2f 0a 20  es in input */. 
2f0ff 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20   int flags;     
2f100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f101 20 20 20 2f 2a 20 31 3a 20 74 72 69 6d 6c 65 66     /* 1: trimlef
2f102 74 20 20 32 3a 20 74 72 69 6d 72 69 67 68 74 20  t  2: trimright 
2f103 20 33 3a 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e   3: trim */.  in
2f104 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
2f105 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f106 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
2f107 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
2f108 61 72 20 2a 61 4c 65 6e 20 3d 20 30 3b 20 20 20  ar *aLen = 0;   
2f109 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
2f10a 20 6f 66 20 65 61 63 68 20 63 68 61 72 61 63 74   of each charact
2f10b 65 72 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a  er in zCharSet *
2f10c 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
2f10d 72 20 2a 2a 61 7a 43 68 61 72 20 3d 20 30 3b 20  r **azChar = 0; 
2f10e 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 76 69 64        /* Individ
2f10f 75 61 6c 20 63 68 61 72 61 63 74 65 72 73 20 69  ual characters i
2f110 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20 20  n zCharSet */.  
2f111 69 6e 74 20 6e 43 68 61 72 3b 20 20 20 20 20 20  int nChar;      
2f112 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f113 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
2f114 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43 68  haracters in zCh
2f115 61 72 53 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20  arSet */..  if( 
2f116 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
2f117 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c  pe(argv[0])==SQL
2f118 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_NULL ){.    
2f119 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 7a 49  return;.  }.  zI
2f11a 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
2f11b 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
2f11c 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20 29 20  .  if( zIn==0 ) 
2f11d 72 65 74 75 72 6e 3b 0a 20 20 6e 49 6e 20 3d 20  return;.  nIn = 
2f11e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
2f11f 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
2f120 61 73 73 65 72 74 28 20 7a 49 6e 3d 3d 73 71 6c  assert( zIn==sql
2f121 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
2f122 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66  argv[0]) );.  if
2f123 28 20 61 72 67 63 3d 3d 31 20 29 7b 0a 20 20 20  ( argc==1 ){.   
2f124 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
2f125 73 69 67 6e 65 64 20 63 68 61 72 20 6c 65 6e 4f  signed char lenO
2f126 6e 65 5b 5d 20 3d 20 7b 20 31 20 7d 3b 0a 20 20  ne[] = { 1 };.  
2f127 20 20 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65    static unsigne
2f128 64 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61  d char * const a
2f129 7a 4f 6e 65 5b 5d 20 3d 20 7b 20 28 75 38 2a 29  zOne[] = { (u8*)
2f12a 22 20 22 20 7d 3b 0a 20 20 20 20 6e 43 68 61 72  " " };.    nChar
2f12b 20 3d 20 31 3b 0a 20 20 20 20 61 4c 65 6e 20 3d   = 1;.    aLen =
2f12c 20 28 75 38 2a 29 6c 65 6e 4f 6e 65 3b 0a 20 20   (u8*)lenOne;.  
2f12d 20 20 61 7a 43 68 61 72 20 3d 20 28 75 6e 73 69    azChar = (unsi
2f12e 67 6e 65 64 20 63 68 61 72 20 2a 2a 29 61 7a 4f  gned char **)azO
2f12f 6e 65 3b 0a 20 20 20 20 7a 43 68 61 72 53 65 74  ne;.    zCharSet
2f130 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
2f131 28 20 28 7a 43 68 61 72 53 65 74 20 3d 20 73 71  ( (zCharSet = sq
2f132 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2f133 28 61 72 67 76 5b 31 5d 29 29 3d 3d 30 20 29 7b  (argv[1]))==0 ){
2f134 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
2f135 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20  else{.    const 
2f136 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
2f137 3b 0a 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61  ;.    for(z=zCha
2f138 72 53 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a  rSet, nChar=0; *
2f139 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20  z; nChar++){.   
2f13a 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55     SQLITE_SKIP_U
2f13b 54 46 38 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20  TF8(z);.    }.  
2f13c 20 20 69 66 28 20 6e 43 68 61 72 3e 30 20 29 7b    if( nChar>0 ){
2f13d 0a 20 20 20 20 20 20 61 7a 43 68 61 72 20 3d 20  .      azChar = 
2f13e 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f  contextMalloc(co
2f13f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e 43 68  ntext, ((i64)nCh
2f140 61 72 29 2a 28 73 69 7a 65 6f 66 28 63 68 61 72  ar)*(sizeof(char
2f141 2a 29 2b 31 29 29 3b 0a 20 20 20 20 20 20 69 66  *)+1));.      if
2f142 28 20 61 7a 43 68 61 72 3d 3d 30 20 29 7b 0a 20  ( azChar==0 ){. 
2f143 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
2f144 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 4c 65       }.      aLe
2f145 6e 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  n = (unsigned ch
2f146 61 72 2a 29 26 61 7a 43 68 61 72 5b 6e 43 68 61  ar*)&azChar[nCha
2f147 72 5d 3b 0a 20 20 20 20 20 20 66 6f 72 28 7a 3d  r];.      for(z=
2f148 7a 43 68 61 72 53 65 74 2c 20 6e 43 68 61 72 3d  zCharSet, nChar=
2f149 30 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b  0; *z; nChar++){
2f14a 0a 20 20 20 20 20 20 20 20 61 7a 43 68 61 72 5b  .        azChar[
2f14b 6e 43 68 61 72 5d 20 3d 20 28 75 6e 73 69 67 6e  nChar] = (unsign
2f14c 65 64 20 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20  ed char *)z;.   
2f14d 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50       SQLITE_SKIP
2f14e 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20  _UTF8(z);.      
2f14f 20 20 61 4c 65 6e 5b 6e 43 68 61 72 5d 20 3d 20    aLen[nChar] = 
2f150 28 75 38 29 28 7a 20 2d 20 61 7a 43 68 61 72 5b  (u8)(z - azChar[
2f151 6e 43 68 61 72 5d 29 3b 0a 20 20 20 20 20 20 7d  nChar]);.      }
2f152 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
2f153 20 6e 43 68 61 72 3e 30 20 29 7b 0a 20 20 20 20   nChar>0 ){.    
2f154 66 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 50  flags = SQLITE_P
2f155 54 52 5f 54 4f 5f 49 4e 54 28 73 71 6c 69 74 65  TR_TO_INT(sqlite
2f156 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74  3_user_data(cont
2f157 65 78 74 29 29 3b 0a 20 20 20 20 69 66 28 20 66  ext));.    if( f
2f158 6c 61 67 73 20 26 20 31 20 29 7b 0a 20 20 20 20  lags & 1 ){.    
2f159 20 20 77 68 69 6c 65 28 20 6e 49 6e 3e 30 20 29    while( nIn>0 )
2f15a 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c 65  {.        int le
2f15b 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66  n = 0;.        f
2f15c 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b  or(i=0; i<nChar;
2f15d 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
2f15e 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a   len = aLen[i];.
2f15f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 65            if( le
2f160 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63 6d 70  n<=nIn && memcmp
2f161 28 7a 49 6e 2c 20 61 7a 43 68 61 72 5b 69 5d 2c  (zIn, azChar[i],
2f162 20 6c 65 6e 29 3d 3d 30 20 29 20 62 72 65 61 6b   len)==0 ) break
2f163 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2f164 20 20 20 20 69 66 28 20 69 3e 3d 6e 43 68 61 72      if( i>=nChar
2f165 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
2f166 20 20 7a 49 6e 20 2b 3d 20 6c 65 6e 3b 0a 20 20    zIn += len;.  
2f167 20 20 20 20 20 20 6e 49 6e 20 2d 3d 20 6c 65 6e        nIn -= len
2f168 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2f169 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20      if( flags & 
2f16a 32 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65  2 ){.      while
2f16b 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20 20  ( nIn>0 ){.     
2f16c 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a     int len = 0;.
2f16d 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
2f16e 20 69 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a   i<nChar; i++){.
2f16f 20 20 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20            len = 
2f170 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20  aLen[i];.       
2f171 20 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e 20     if( len<=nIn 
2f172 26 26 20 6d 65 6d 63 6d 70 28 26 7a 49 6e 5b 6e  && memcmp(&zIn[n
2f173 49 6e 2d 6c 65 6e 5d 2c 61 7a 43 68 61 72 5b 69  In-len],azChar[i
2f174 5d 2c 6c 65 6e 29 3d 3d 30 20 29 20 62 72 65 61  ],len)==0 ) brea
2f175 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
2f176 20 20 20 20 20 69 66 28 20 69 3e 3d 6e 43 68 61       if( i>=nCha
2f177 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  r ) break;.     
2f178 20 20 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20     nIn -= len;. 
2f179 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
2f17a 20 69 66 28 20 7a 43 68 61 72 53 65 74 20 29 7b   if( zCharSet ){
2f17b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
2f17c 72 65 65 28 61 7a 43 68 61 72 29 3b 0a 20 20 20  ree(azChar);.   
2f17d 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
2f17e 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
2f17f 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 49 6e  text, (char*)zIn
2f180 2c 20 6e 49 6e 2c 20 53 51 4c 49 54 45 5f 54 52  , nIn, SQLITE_TR
2f181 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a 0a 2f 2a  ANSIENT);.}.../*
2f182 20 49 4d 50 3a 20 52 2d 32 35 33 36 31 2d 31 36   IMP: R-25361-16
2f183 31 35 30 20 54 68 69 73 20 66 75 6e 63 74 69 6f  150 This functio
2f184 6e 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f  n is omitted fro
2f185 6d 20 53 51 4c 69 74 65 20 62 79 20 64 65 66 61  m SQLite by defa
2f186 75 6c 74 2e 20 49 74 0a 2a 2a 20 69 73 20 6f 6e  ult. It.** is on
2f187 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20  ly available if 
2f188 74 68 65 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44  the SQLITE_SOUND
2f189 45 58 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  EX compile-time 
2f18a 6f 70 74 69 6f 6e 20 69 73 20 75 73 65 64 0a 2a  option is used.*
2f18b 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73  * when SQLite is
2f18c 20 62 75 69 6c 74 2e 0a 2a 2f 0a 23 69 66 64 65   built..*/.#ifde
2f18d 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58  f SQLITE_SOUNDEX
2f18e 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  ./*.** Compute t
2f18f 68 65 20 73 6f 75 6e 64 65 78 20 65 6e 63 6f 64  he soundex encod
2f190 69 6e 67 20 6f 66 20 61 20 77 6f 72 64 2e 0a 2a  ing of a word..*
2f191 2a 0a 2a 2a 20 49 4d 50 3a 20 52 2d 35 39 37 38  *.** IMP: R-5978
2f192 32 2d 30 30 30 37 32 20 54 68 65 20 73 6f 75 6e  2-00072 The soun
2f193 64 65 78 28 58 29 20 66 75 6e 63 74 69 6f 6e 20  dex(X) function 
2f194 72 65 74 75 72 6e 73 20 61 20 73 74 72 69 6e 67  returns a string
2f195 20 74 68 61 74 20 69 73 20 74 68 65 0a 2a 2a 20   that is the.** 
2f196 73 6f 75 6e 64 65 78 20 65 6e 63 6f 64 69 6e 67  soundex encoding
2f197 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 58   of the string X
2f198 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  . .*/.static voi
2f199 64 20 73 6f 75 6e 64 65 78 46 75 6e 63 28 0a 20  d soundexFunc(. 
2f19a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2f19b 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
2f19c 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
2f19d 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
2f19e 0a 20 20 63 68 61 72 20 7a 52 65 73 75 6c 74 5b  .  char zResult[
2f19f 38 5d 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a  8];.  const u8 *
2f1a0 7a 49 6e 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  zIn;.  int i, j;
2f1a1 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
2f1a2 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 43  unsigned char iC
2f1a3 6f 64 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 30 2c  ode[] = {.    0,
2f1a4 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2f1a5 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2f1a6 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20  , 0, 0, 0, 0,.  
2f1a7 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c    0, 0, 0, 0, 0,
2f1a8 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2f1a9 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2f1aa 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30  ,.    0, 0, 0, 0
2f1ab 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2f1ac 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2f1ad 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20  0, 0,.    0, 0, 
2f1ae 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2f1af 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2f1b0 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c   0, 0, 0,.    0,
2f1b1 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20   0, 1, 2, 3, 0, 
2f1b2 31 2c 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32  1, 2, 0, 0, 2, 2
2f1b3 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20  , 4, 5, 5, 0,.  
2f1b4 20 20 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c    1, 2, 6, 2, 3,
2f1b5 20 30 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20   0, 1, 0, 2, 0, 
2f1b6 32 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  2, 0, 0, 0, 0, 0
2f1b7 2c 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 32  ,.    0, 0, 1, 2
2f1b8 2c 20 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c  , 3, 0, 1, 2, 0,
2f1b9 20 30 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20   0, 2, 2, 4, 5, 
2f1ba 35 2c 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20  5, 0,.    1, 2, 
2f1bb 36 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30  6, 2, 3, 0, 1, 0
2f1bc 2c 20 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c  , 2, 0, 2, 0, 0,
2f1bd 20 30 2c 20 30 2c 20 30 2c 0a 20 20 7d 3b 0a 20   0, 0, 0,.  };. 
2f1be 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31   assert( argc==1
2f1bf 20 29 3b 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a   );.  zIn = (u8*
2f1c0 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
2f1c1 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
2f1c2 69 66 28 20 7a 49 6e 3d 3d 30 20 29 20 7a 49 6e  if( zIn==0 ) zIn
2f1c3 20 3d 20 28 75 38 2a 29 22 22 3b 0a 20 20 66 6f   = (u8*)"";.  fo
2f1c4 72 28 69 3d 30 3b 20 7a 49 6e 5b 69 5d 20 26 26  r(i=0; zIn[i] &&
2f1c5 20 21 73 71 6c 69 74 65 33 49 73 61 6c 70 68 61   !sqlite3Isalpha
2f1c6 28 7a 49 6e 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d  (zIn[i]); i++){}
2f1c7 0a 20 20 69 66 28 20 7a 49 6e 5b 69 5d 20 29 7b  .  if( zIn[i] ){
2f1c8 0a 20 20 20 20 75 38 20 70 72 65 76 63 6f 64 65  .    u8 prevcode
2f1c9 20 3d 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26   = iCode[zIn[i]&
2f1ca 30 78 37 66 5d 3b 0a 20 20 20 20 7a 52 65 73 75  0x7f];.    zResu
2f1cb 6c 74 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 54  lt[0] = sqlite3T
2f1cc 6f 75 70 70 65 72 28 7a 49 6e 5b 69 5d 29 3b 0a  oupper(zIn[i]);.
2f1cd 20 20 20 20 66 6f 72 28 6a 3d 31 3b 20 6a 3c 34      for(j=1; j<4
2f1ce 20 26 26 20 7a 49 6e 5b 69 5d 3b 20 69 2b 2b 29   && zIn[i]; i++)
2f1cf 7b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65  {.      int code
2f1d0 20 3d 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26   = iCode[zIn[i]&
2f1d1 30 78 37 66 5d 3b 0a 20 20 20 20 20 20 69 66 28  0x7f];.      if(
2f1d2 20 63 6f 64 65 3e 30 20 29 7b 0a 20 20 20 20 20   code>0 ){.     
2f1d3 20 20 20 69 66 28 20 63 6f 64 65 21 3d 70 72 65     if( code!=pre
2f1d4 76 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20  vcode ){.       
2f1d5 20 20 20 70 72 65 76 63 6f 64 65 20 3d 20 63 6f     prevcode = co
2f1d6 64 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 52  de;.          zR
2f1d7 65 73 75 6c 74 5b 6a 2b 2b 5d 20 3d 20 63 6f 64  esult[j++] = cod
2f1d8 65 20 2b 20 27 30 27 3b 0a 20 20 20 20 20 20 20  e + '0';.       
2f1d9 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
2f1da 20 20 20 20 20 20 20 20 70 72 65 76 63 6f 64 65          prevcode
2f1db 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
2f1dc 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 6a    }.    while( j
2f1dd 3c 34 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 73  <4 ){.      zRes
2f1de 75 6c 74 5b 6a 2b 2b 5d 20 3d 20 27 30 27 3b 0a  ult[j++] = '0';.
2f1df 20 20 20 20 7d 0a 20 20 20 20 7a 52 65 73 75 6c      }.    zResul
2f1e0 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71  t[j] = 0;.    sq
2f1e1 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2f1e2 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 73 75  t(context, zResu
2f1e3 6c 74 2c 20 34 2c 20 53 51 4c 49 54 45 5f 54 52  lt, 4, SQLITE_TR
2f1e4 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73  ANSIENT);.  }els
2f1e5 65 7b 0a 20 20 20 20 2f 2a 20 49 4d 50 3a 20 52  e{.    /* IMP: R
2f1e6 2d 36 34 38 39 34 2d 35 30 33 32 31 20 54 68 65  -64894-50321 The
2f1e7 20 73 74 72 69 6e 67 20 22 3f 30 30 30 22 20 69   string "?000" i
2f1e8 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68  s returned if th
2f1e9 65 20 61 72 67 75 6d 65 6e 74 0a 20 20 20 20 2a  e argument.    *
2f1ea 2a 20 69 73 20 4e 55 4c 4c 20 6f 72 20 63 6f 6e  * is NULL or con
2f1eb 74 61 69 6e 73 20 6e 6f 20 41 53 43 49 49 20 61  tains no ASCII a
2f1ec 6c 70 68 61 62 65 74 69 63 20 63 68 61 72 61 63  lphabetic charac
2f1ed 74 65 72 73 2e 20 2a 2f 0a 20 20 20 20 73 71 6c  ters. */.    sql
2f1ee 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2f1ef 28 63 6f 6e 74 65 78 74 2c 20 22 3f 30 30 30 22  (context, "?000"
2f1f0 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  , 4, SQLITE_STAT
2f1f1 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  IC);.  }.}.#endi
2f1f2 66 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f 55 4e  f /* SQLITE_SOUN
2f1f3 44 45 58 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  DEX */..#ifndef 
2f1f4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
2f1f5 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a  _EXTENSION./*.**
2f1f6 20 41 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74   A function that
2f1f7 20 6c 6f 61 64 73 20 61 20 73 68 61 72 65 64 2d   loads a shared-
2f1f8 6c 69 62 72 61 72 79 20 65 78 74 65 6e 73 69 6f  library extensio
2f1f9 6e 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 4e  n then returns N
2f1fa 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ULL..*/.static v
2f1fb 6f 69 64 20 6c 6f 61 64 45 78 74 28 73 71 6c 69  oid loadExt(sqli
2f1fc 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
2f1fd 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
2f1fe 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2f1ff 61 72 67 76 29 7b 0a 20 20 63 6f 6e 73 74 20 63  argv){.  const c
2f200 68 61 72 20 2a 7a 46 69 6c 65 20 3d 20 28 63 6f  har *zFile = (co
2f201 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
2f202 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
2f203 67 76 5b 30 5d 29 3b 0a 20 20 63 6f 6e 73 74 20  gv[0]);.  const 
2f204 63 68 61 72 20 2a 7a 50 72 6f 63 3b 0a 20 20 73  char *zProc;.  s
2f205 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
2f206 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
2f207 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
2f208 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
2f209 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 61 72 67   = 0;..  if( arg
2f20a 63 3d 3d 32 20 29 7b 0a 20 20 20 20 7a 50 72 6f  c==2 ){.    zPro
2f20b 63 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  c = (const char 
2f20c 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
2f20d 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  text(argv[1]);. 
2f20e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 50 72 6f   }else{.    zPro
2f20f 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  c = 0;.  }.  if(
2f210 20 7a 46 69 6c 65 20 26 26 20 73 71 6c 69 74 65   zFile && sqlite
2f211 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
2f212 28 64 62 2c 20 7a 46 69 6c 65 2c 20 7a 50 72 6f  (db, zFile, zPro
2f213 63 2c 20 26 7a 45 72 72 4d 73 67 29 20 29 7b 0a  c, &zErrMsg) ){.
2f214 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
2f215 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
2f216 2c 20 7a 45 72 72 4d 73 67 2c 20 2d 31 29 3b 0a  , zErrMsg, -1);.
2f217 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
2f218 28 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d  (zErrMsg);.  }.}
2f219 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
2f21a 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
2f21b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
2f21c 75 63 74 75 72 65 20 68 6f 6c 64 73 20 74 68 65  ucture holds the
2f21d 20 63 6f 6e 74 65 78 74 20 6f 66 20 61 0a 2a 2a   context of a.**
2f21e 20 73 75 6d 28 29 20 6f 72 20 61 76 67 28 29 20   sum() or avg() 
2f21f 61 67 67 72 65 67 61 74 65 20 63 6f 6d 70 75 74  aggregate comput
2f220 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65  ation..*/.typede
2f221 66 20 73 74 72 75 63 74 20 53 75 6d 43 74 78 20  f struct SumCtx 
2f222 53 75 6d 43 74 78 3b 0a 73 74 72 75 63 74 20 53  SumCtx;.struct S
2f223 75 6d 43 74 78 20 7b 0a 20 20 64 6f 75 62 6c 65  umCtx {.  double
2f224 20 72 53 75 6d 3b 20 20 20 20 20 20 2f 2a 20 46   rSum;      /* F
2f225 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75  loating point su
2f226 6d 20 2a 2f 0a 20 20 69 36 34 20 69 53 75 6d 3b  m */.  i64 iSum;
2f227 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65           /* Inte
2f228 67 65 72 20 73 75 6d 20 2a 2f 20 20 20 0a 20 20  ger sum */   .  
2f229 69 36 34 20 63 6e 74 3b 20 20 20 20 20 20 20 20  i64 cnt;        
2f22a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
2f22b 6c 65 6d 65 6e 74 73 20 73 75 6d 6d 65 64 20 2a  lements summed *
2f22c 2f 0a 20 20 75 38 20 6f 76 65 72 66 6c 6f 77 3b  /.  u8 overflow;
2f22d 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
2f22e 20 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f   integer overflo
2f22f 77 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 61  w seen */.  u8 a
2f230 70 70 72 6f 78 3b 20 20 20 20 20 20 20 20 2f 2a  pprox;        /*
2f231 20 54 72 75 65 20 69 66 20 6e 6f 6e 2d 69 6e 74   True if non-int
2f232 65 67 65 72 20 76 61 6c 75 65 20 77 61 73 20 69  eger value was i
2f233 6e 70 75 74 20 74 6f 20 74 68 65 20 73 75 6d 20  nput to the sum 
2f234 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75  */.};../*.** Rou
2f235 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 63 6f  tines used to co
2f236 6d 70 75 74 65 20 74 68 65 20 73 75 6d 2c 20 61  mpute the sum, a
2f237 76 65 72 61 67 65 2c 20 61 6e 64 20 74 6f 74 61  verage, and tota
2f238 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 55 4d  l..**.** The SUM
2f239 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 6c 6c  () function foll
2f23a 6f 77 73 20 74 68 65 20 28 62 72 6f 6b 65 6e 29  ows the (broken)
2f23b 20 53 51 4c 20 73 74 61 6e 64 61 72 64 20 77 68   SQL standard wh
2f23c 69 63 68 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61  ich means.** tha
2f23d 74 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c  t it returns NUL
2f23e 4c 20 69 66 20 69 74 20 73 75 6d 73 20 6f 76 65  L if it sums ove
2f23f 72 20 6e 6f 20 69 6e 70 75 74 73 2e 20 20 54 4f  r no inputs.  TO
2f240 54 41 4c 20 72 65 74 75 72 6e 73 0a 2a 2a 20 30  TAL returns.** 0
2f241 2e 30 20 69 6e 20 74 68 61 74 20 63 61 73 65 2e  .0 in that case.
2f242 20 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c 20 54    In addition, T
2f243 4f 54 41 4c 20 61 6c 77 61 79 73 20 72 65 74 75  OTAL always retu
2f244 72 6e 73 20 61 20 66 6c 6f 61 74 20 77 68 65 72  rns a float wher
2f245 65 0a 2a 2a 20 53 55 4d 20 6d 69 67 68 74 20 72  e.** SUM might r
2f246 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72  eturn an integer
2f247 20 69 66 20 69 74 20 6e 65 76 65 72 20 65 6e 63   if it never enc
2f248 6f 75 6e 74 65 72 73 20 61 20 66 6c 6f 61 74 69  ounters a floati
2f249 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75  ng point.** valu
2f24a 65 2e 20 20 54 4f 54 41 4c 20 6e 65 76 65 72 20  e.  TOTAL never 
2f24b 66 61 69 6c 73 2c 20 62 75 74 20 53 55 4d 20 6d  fails, but SUM m
2f24c 69 67 68 74 20 74 68 72 6f 75 67 68 20 61 6e 20  ight through an 
2f24d 65 78 63 65 70 74 69 6f 6e 20 69 66 0a 2a 2a 20  exception if.** 
2f24e 69 74 20 6f 76 65 72 66 6c 6f 77 73 20 61 6e 20  it overflows an 
2f24f 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 73 74 61 74  integer..*/.stat
2f250 69 63 20 76 6f 69 64 20 73 75 6d 53 74 65 70 28  ic void sumStep(
2f251 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
2f252 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72  *context, int ar
2f253 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
2f254 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 53 75 6d  e **argv){.  Sum
2f255 43 74 78 20 2a 70 3b 0a 20 20 69 6e 74 20 74 79  Ctx *p;.  int ty
2f256 70 65 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72  pe;.  assert( ar
2f257 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45  gc==1 );.  UNUSE
2f258 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
2f259 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  );.  p = sqlite3
2f25a 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
2f25b 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65  xt(context, size
2f25c 6f 66 28 2a 70 29 29 3b 0a 20 20 74 79 70 65 20  of(*p));.  type 
2f25d 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
2f25e 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 61 72 67  numeric_type(arg
2f25f 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 70 20 26  v[0]);.  if( p &
2f260 26 20 74 79 70 65 21 3d 53 51 4c 49 54 45 5f 4e  & type!=SQLITE_N
2f261 55 4c 4c 20 29 7b 0a 20 20 20 20 70 2d 3e 63 6e  ULL ){.    p->cn
2f262 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20 74 79 70  t++;.    if( typ
2f263 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  e==SQLITE_INTEGE
2f264 52 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76  R ){.      i64 v
2f265 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
2f266 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b  _int64(argv[0]);
2f267 0a 20 20 20 20 20 20 70 2d 3e 72 53 75 6d 20 2b  .      p->rSum +
2f268 3d 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 28  = v;.      if( (
2f269 70 2d 3e 61 70 70 72 6f 78 7c 70 2d 3e 6f 76 65  p->approx|p->ove
2f26a 72 66 6c 6f 77 29 3d 3d 30 20 26 26 20 73 71 6c  rflow)==0 && sql
2f26b 69 74 65 33 41 64 64 49 6e 74 36 34 28 26 70 2d  ite3AddInt64(&p-
2f26c 3e 69 53 75 6d 2c 20 76 29 20 29 7b 0a 20 20 20  >iSum, v) ){.   
2f26d 20 20 20 20 20 70 2d 3e 6f 76 65 72 66 6c 6f 77       p->overflow
2f26e 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
2f26f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
2f270 2d 3e 72 53 75 6d 20 2b 3d 20 73 71 6c 69 74 65  ->rSum += sqlite
2f271 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61  3_value_double(a
2f272 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70  rgv[0]);.      p
2f273 2d 3e 61 70 70 72 6f 78 20 3d 20 31 3b 0a 20 20  ->approx = 1;.  
2f274 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63    }.  }.}.static
2f275 20 76 6f 69 64 20 73 75 6d 46 69 6e 61 6c 69 7a   void sumFinaliz
2f276 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e(sqlite3_contex
2f277 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53  t *context){.  S
2f278 75 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20  umCtx *p;.  p = 
2f279 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
2f27a 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
2f27b 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26  t, 0);.  if( p &
2f27c 26 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20  & p->cnt>0 ){.  
2f27d 20 20 69 66 28 20 70 2d 3e 6f 76 65 72 66 6c 6f    if( p->overflo
2f27e 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  w ){.      sqlit
2f27f 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2f280 63 6f 6e 74 65 78 74 2c 22 69 6e 74 65 67 65 72  context,"integer
2f281 20 6f 76 65 72 66 6c 6f 77 22 2c 2d 31 29 3b 0a   overflow",-1);.
2f282 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d      }else if( p-
2f283 3e 61 70 70 72 6f 78 20 29 7b 0a 20 20 20 20 20  >approx ){.     
2f284 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2f285 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20  double(context, 
2f286 70 2d 3e 72 53 75 6d 29 3b 0a 20 20 20 20 7d 65  p->rSum);.    }e
2f287 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
2f288 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28  e3_result_int64(
2f289 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 69 53 75 6d  context, p->iSum
2f28a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73  );.    }.  }.}.s
2f28b 74 61 74 69 63 20 76 6f 69 64 20 61 76 67 46 69  tatic void avgFi
2f28c 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63  nalize(sqlite3_c
2f28d 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29  ontext *context)
2f28e 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20  {.  SumCtx *p;. 
2f28f 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67   p = sqlite3_agg
2f290 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63  regate_context(c
2f291 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66  ontext, 0);.  if
2f292 28 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e 30 20  ( p && p->cnt>0 
2f293 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
2f294 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e  esult_double(con
2f295 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d 2f 28 64  text, p->rSum/(d
2f296 6f 75 62 6c 65 29 70 2d 3e 63 6e 74 29 3b 0a 20  ouble)p->cnt);. 
2f297 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
2f298 20 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 28 73   totalFinalize(s
2f299 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
2f29a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43  context){.  SumC
2f29b 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c  tx *p;.  p = sql
2f29c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
2f29d 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  ontext(context, 
2f29e 30 29 3b 0a 20 20 2f 2a 20 28 64 6f 75 62 6c 65  0);.  /* (double
2f29f 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51  )0 In case of SQ
2f2a0 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
2f2a1 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20  NG_POINT... */. 
2f2a2 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2f2a3 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20  double(context, 
2f2a4 70 20 3f 20 70 2d 3e 72 53 75 6d 20 3a 20 28 64  p ? p->rSum : (d
2f2a5 6f 75 62 6c 65 29 30 29 3b 0a 7d 0a 0a 2f 2a 0a  ouble)0);.}../*.
2f2a6 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
2f2a7 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73   structure keeps
2f2a8 20 74 72 61 63 6b 20 6f 66 20 73 74 61 74 65 20   track of state 
2f2a9 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
2f2aa 74 68 65 0a 2a 2a 20 63 6f 75 6e 74 28 29 20 61  the.** count() a
2f2ab 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
2f2ac 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  n..*/.typedef st
2f2ad 72 75 63 74 20 43 6f 75 6e 74 43 74 78 20 43 6f  ruct CountCtx Co
2f2ae 75 6e 74 43 74 78 3b 0a 73 74 72 75 63 74 20 43  untCtx;.struct C
2f2af 6f 75 6e 74 43 74 78 20 7b 0a 20 20 69 36 34 20  ountCtx {.  i64 
2f2b0 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75  n;.};../*.** Rou
2f2b1 74 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65  tines to impleme
2f2b2 6e 74 20 74 68 65 20 63 6f 75 6e 74 28 29 20 61  nt the count() a
2f2b3 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
2f2b4 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
2f2b5 64 20 63 6f 75 6e 74 53 74 65 70 28 73 71 6c 69  d countStep(sqli
2f2b6 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
2f2b7 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
2f2b8 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2f2b9 61 72 67 76 29 7b 0a 20 20 43 6f 75 6e 74 43 74  argv){.  CountCt
2f2ba 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69  x *p;.  p = sqli
2f2bb 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
2f2bc 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73  ntext(context, s
2f2bd 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66  izeof(*p));.  if
2f2be 28 20 28 61 72 67 63 3d 3d 30 20 7c 7c 20 53 51  ( (argc==0 || SQ
2f2bf 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74  LITE_NULL!=sqlit
2f2c0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
2f2c1 67 76 5b 30 5d 29 29 20 26 26 20 70 20 29 7b 0a  gv[0])) && p ){.
2f2c2 20 20 20 20 70 2d 3e 6e 2b 2b 3b 0a 20 20 7d 0a      p->n++;.  }.
2f2c3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2f2c4 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a  OMIT_DEPRECATED.
2f2c5 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 33    /* The sqlite3
2f2c6 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74  _aggregate_count
2f2c7 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64  () function is d
2f2c8 65 70 72 65 63 61 74 65 64 2e 20 20 42 75 74 20  eprecated.  But 
2f2c9 6a 75 73 74 20 74 6f 20 6d 61 6b 65 0a 20 20 2a  just to make.  *
2f2ca 2a 20 73 75 72 65 20 69 74 20 73 74 69 6c 6c 20  * sure it still 
2f2cb 6f 70 65 72 61 74 65 73 20 63 6f 72 72 65 63 74  operates correct
2f2cc 6c 79 2c 20 76 65 72 69 66 79 20 74 68 61 74 20  ly, verify that 
2f2cd 69 74 73 20 63 6f 75 6e 74 20 61 67 72 65 65 73  its count agrees
2f2ce 20 77 69 74 68 20 6f 75 72 20 0a 20 20 2a 2a 20   with our .  ** 
2f2cf 69 6e 74 65 72 6e 61 6c 20 63 6f 75 6e 74 20 77  internal count w
2f2d0 68 65 6e 20 75 73 69 6e 67 20 63 6f 75 6e 74 28  hen using count(
2f2d1 2a 29 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20  *) and when the 
2f2d2 74 6f 74 61 6c 20 63 6f 75 6e 74 20 63 61 6e 20  total count can 
2f2d3 62 65 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 65  be.  ** expresse
2f2d4 64 20 61 73 20 61 20 33 32 2d 62 69 74 20 69 6e  d as a 32-bit in
2f2d5 74 65 67 65 72 2e 20 2a 2f 0a 20 20 61 73 73 65  teger. */.  asse
2f2d6 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20 70  rt( argc==1 || p
2f2d7 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 3e 30 78 37 66  ==0 || p->n>0x7f
2f2d8 66 66 66 66 66 66 0a 20 20 20 20 20 20 20 20 20  ffffff.         
2f2d9 20 7c 7c 20 70 2d 3e 6e 3d 3d 73 71 6c 69 74 65   || p->n==sqlite
2f2da 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e  3_aggregate_coun
2f2db 74 28 63 6f 6e 74 65 78 74 29 20 29 3b 0a 23 65  t(context) );.#e
2f2dc 6e 64 69 66 0a 7d 20 20 20 0a 73 74 61 74 69 63  ndif.}   .static
2f2dd 20 76 6f 69 64 20 63 6f 75 6e 74 46 69 6e 61 6c   void countFinal
2f2de 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ize(sqlite3_cont
2f2df 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20  ext *context){. 
2f2e0 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20 20   CountCtx *p;.  
2f2e1 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72  p = sqlite3_aggr
2f2e2 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
2f2e3 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 73 71 6c  ntext, 0);.  sql
2f2e4 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
2f2e5 34 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20 70  4(context, p ? p
2f2e6 2d 3e 6e 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  ->n : 0);.}../*.
2f2e7 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 69  ** Routines to i
2f2e8 6d 70 6c 65 6d 65 6e 74 20 6d 69 6e 28 29 20 61  mplement min() a
2f2e9 6e 64 20 6d 61 78 28 29 20 61 67 67 72 65 67 61  nd max() aggrega
2f2ea 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f  te functions..*/
2f2eb 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69 6e  .static void min
2f2ec 6d 61 78 53 74 65 70 28 0a 20 20 73 71 6c 69 74  maxStep(.  sqlit
2f2ed 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
2f2ee 65 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f 74 55  ext, .  int NotU
2f2ef 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f  sed, .  sqlite3_
2f2f0 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
2f2f1 20 20 4d 65 6d 20 2a 70 41 72 67 20 20 3d 20 28    Mem *pArg  = (
2f2f2 4d 65 6d 20 2a 29 61 72 67 76 5b 30 5d 3b 0a 20  Mem *)argv[0];. 
2f2f3 20 4d 65 6d 20 2a 70 42 65 73 74 3b 0a 20 20 55   Mem *pBest;.  U
2f2f4 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
2f2f5 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 70 42 65  NotUsed);..  pBe
2f2f6 73 74 20 3d 20 28 4d 65 6d 20 2a 29 73 71 6c 69  st = (Mem *)sqli
2f2f7 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
2f2f8 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73  ntext(context, s
2f2f9 69 7a 65 6f 66 28 2a 70 42 65 73 74 29 29 3b 0a  izeof(*pBest));.
2f2fa 20 20 69 66 28 20 21 70 42 65 73 74 20 29 20 72    if( !pBest ) r
2f2fb 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 73 71  eturn;..  if( sq
2f2fc 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
2f2fd 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54  (argv[0])==SQLIT
2f2fe 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 66  E_NULL ){.    if
2f2ff 28 20 70 42 65 73 74 2d 3e 66 6c 61 67 73 20 29  ( pBest->flags )
2f300 20 73 71 6c 69 74 65 33 53 6b 69 70 41 63 63 75   sqlite3SkipAccu
2f301 6d 75 6c 61 74 6f 72 4c 6f 61 64 28 63 6f 6e 74  mulatorLoad(cont
2f302 65 78 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ext);.  }else if
2f303 28 20 70 42 65 73 74 2d 3e 66 6c 61 67 73 20 29  ( pBest->flags )
2f304 7b 0a 20 20 20 20 69 6e 74 20 6d 61 78 3b 0a 20  {.    int max;. 
2f305 20 20 20 69 6e 74 20 63 6d 70 3b 0a 20 20 20 20     int cmp;.    
2f306 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d  CollSeq *pColl =
2f307 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43   sqlite3GetFuncC
2f308 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b  ollSeq(context);
2f309 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 73 74 65  .    /* This ste
2f30a 70 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  p function is us
2f30b 65 64 20 66 6f 72 20 62 6f 74 68 20 74 68 65 20  ed for both the 
2f30c 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20  min() and max() 
2f30d 61 67 67 72 65 67 61 74 65 73 2c 0a 20 20 20 20  aggregates,.    
2f30e 2a 2a 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66  ** the only diff
2f30f 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74  erence between t
2f310 68 65 20 74 77 6f 20 62 65 69 6e 67 20 74 68 61  he two being tha
2f311 74 20 74 68 65 20 73 65 6e 73 65 20 6f 66 20 74  t the sense of t
2f312 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72  he.    ** compar
2f313 69 73 6f 6e 20 69 73 20 69 6e 76 65 72 74 65 64  ison is inverted
2f314 2e 20 46 6f 72 20 74 68 65 20 6d 61 78 28 29 20  . For the max() 
2f315 61 67 67 72 65 67 61 74 65 2c 20 74 68 65 0a 20  aggregate, the. 
2f316 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 75 73     ** sqlite3_us
2f317 65 72 5f 64 61 74 61 28 29 20 66 75 6e 63 74 69  er_data() functi
2f318 6f 6e 20 72 65 74 75 72 6e 73 20 28 76 6f 69 64  on returns (void
2f319 20 2a 29 2d 31 2e 20 46 6f 72 20 6d 69 6e 28 29   *)-1. For min()
2f31a 20 69 74 0a 20 20 20 20 2a 2a 20 72 65 74 75 72   it.    ** retur
2f31b 6e 73 20 28 76 6f 69 64 20 2a 29 64 62 2c 20 77  ns (void *)db, w
2f31c 68 65 72 65 20 64 62 20 69 73 20 74 68 65 20 73  here db is the s
2f31d 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73 65  qlite3* database
2f31e 20 70 6f 69 6e 74 65 72 2e 0a 20 20 20 20 2a 2a   pointer..    **
2f31f 20 54 68 65 72 65 66 6f 72 65 20 74 68 65 20 6e   Therefore the n
2f320 65 78 74 20 73 74 61 74 65 6d 65 6e 74 20 73 65  ext statement se
2f321 74 73 20 76 61 72 69 61 62 6c 65 20 27 6d 61 78  ts variable 'max
2f322 27 20 74 6f 20 31 20 66 6f 72 20 74 68 65 20 6d  ' to 1 for the m
2f323 61 78 28 29 0a 20 20 20 20 2a 2a 20 61 67 67 72  ax().    ** aggr
2f324 65 67 61 74 65 2c 20 6f 72 20 30 20 66 6f 72 20  egate, or 0 for 
2f325 6d 69 6e 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  min()..    */.  
2f326 20 20 6d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f    max = sqlite3_
2f327 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
2f328 74 29 21 3d 30 3b 0a 20 20 20 20 63 6d 70 20 3d  t)!=0;.    cmp =
2f329 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61   sqlite3MemCompa
2f32a 72 65 28 70 42 65 73 74 2c 20 70 41 72 67 2c 20  re(pBest, pArg, 
2f32b 70 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20  pColl);.    if( 
2f32c 28 6d 61 78 20 26 26 20 63 6d 70 3c 30 29 20 7c  (max && cmp<0) |
2f32d 7c 20 28 21 6d 61 78 20 26 26 20 63 6d 70 3e 30  | (!max && cmp>0
2f32e 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
2f32f 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 70 42  e3VdbeMemCopy(pB
2f330 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 20 20  est, pArg);.    
2f331 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
2f332 69 74 65 33 53 6b 69 70 41 63 63 75 6d 75 6c 61  ite3SkipAccumula
2f333 74 6f 72 4c 6f 61 64 28 63 6f 6e 74 65 78 74 29  torLoad(context)
2f334 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
2f335 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2f336 4d 65 6d 43 6f 70 79 28 70 42 65 73 74 2c 20 70  MemCopy(pBest, p
2f337 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74  Arg);.  }.}.stat
2f338 69 63 20 76 6f 69 64 20 6d 69 6e 4d 61 78 46 69  ic void minMaxFi
2f339 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63  nalize(sqlite3_c
2f33a 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29  ontext *context)
2f33b 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  {.  sqlite3_valu
2f33c 65 20 2a 70 52 65 73 3b 0a 20 20 70 52 65 73 20  e *pRes;.  pRes 
2f33d 3d 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  = (sqlite3_value
2f33e 20 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65   *)sqlite3_aggre
2f33f 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e  gate_context(con
2f340 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20  text, 0);.  if( 
2f341 70 52 65 73 20 29 7b 0a 20 20 20 20 69 66 28 20  pRes ){.    if( 
2f342 70 52 65 73 2d 3e 66 6c 61 67 73 20 29 7b 0a 20  pRes->flags ){. 
2f343 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
2f344 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78  ult_value(contex
2f345 74 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 0a  t, pRes);.    }.
2f346 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
2f347 65 6d 52 65 6c 65 61 73 65 28 70 52 65 73 29 3b  emRelease(pRes);
2f348 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 67 72  .  }.}../*.** gr
2f349 6f 75 70 5f 63 6f 6e 63 61 74 28 45 58 50 52 2c  oup_concat(EXPR,
2f34a 20 3f 53 45 50 41 52 41 54 4f 52 3f 29 0a 2a 2f   ?SEPARATOR?).*/
2f34b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 72 6f  .static void gro
2f34c 75 70 43 6f 6e 63 61 74 53 74 65 70 28 0a 20 20  upConcatStep(.  
2f34d 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
2f34e 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
2f34f 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
2f350 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
2f351 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56    const char *zV
2f352 61 6c 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 2a  al;.  StrAccum *
2f353 70 41 63 63 75 6d 3b 0a 20 20 63 6f 6e 73 74 20  pAccum;.  const 
2f354 63 68 61 72 20 2a 7a 53 65 70 3b 0a 20 20 69 6e  char *zSep;.  in
2f355 74 20 6e 56 61 6c 2c 20 6e 53 65 70 3b 0a 20 20  t nVal, nSep;.  
2f356 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20  assert( argc==1 
2f357 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20  || argc==2 );.  
2f358 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
2f359 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d  e_type(argv[0])=
2f35a 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72  =SQLITE_NULL ) r
2f35b 65 74 75 72 6e 3b 0a 20 20 70 41 63 63 75 6d 20  eturn;.  pAccum 
2f35c 3d 20 28 53 74 72 41 63 63 75 6d 2a 29 73 71 6c  = (StrAccum*)sql
2f35d 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
2f35e 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  ontext(context, 
2f35f 73 69 7a 65 6f 66 28 2a 70 41 63 63 75 6d 29 29  sizeof(*pAccum))
2f360 3b 0a 0a 20 20 69 66 28 20 70 41 63 63 75 6d 20  ;..  if( pAccum 
2f361 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ){.    sqlite3 *
2f362 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
2f363 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
2f364 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 69 6e 74  ontext);.    int
2f365 20 66 69 72 73 74 54 65 72 6d 20 3d 20 70 41 63   firstTerm = pAc
2f366 63 75 6d 2d 3e 75 73 65 4d 61 6c 6c 6f 63 3d 3d  cum->useMalloc==
2f367 30 3b 0a 20 20 20 20 70 41 63 63 75 6d 2d 3e 75  0;.    pAccum->u
2f368 73 65 4d 61 6c 6c 6f 63 20 3d 20 32 3b 0a 20 20  seMalloc = 2;.  
2f369 20 20 70 41 63 63 75 6d 2d 3e 6d 78 41 6c 6c 6f    pAccum->mxAllo
2f36a 63 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  c = db->aLimit[S
2f36b 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
2f36c 54 48 5d 3b 0a 20 20 20 20 69 66 28 20 21 66 69  TH];.    if( !fi
2f36d 72 73 74 54 65 72 6d 20 29 7b 0a 20 20 20 20 20  rstTerm ){.     
2f36e 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a   if( argc==2 ){.
2f36f 20 20 20 20 20 20 20 20 7a 53 65 70 20 3d 20 28          zSep = (
2f370 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
2f371 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d  lue_text(argv[1]
2f372 29 3b 0a 20 20 20 20 20 20 20 20 6e 53 65 70 20  );.        nSep 
2f373 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
2f374 62 79 74 65 73 28 61 72 67 76 5b 31 5d 29 3b 0a  bytes(argv[1]);.
2f375 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2f376 20 20 20 20 20 7a 53 65 70 20 3d 20 22 2c 22 3b       zSep = ",";
2f377 0a 20 20 20 20 20 20 20 20 6e 53 65 70 20 3d 20  .        nSep = 
2f378 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
2f379 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
2f37a 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a  Append(pAccum, z
2f37b 53 65 70 2c 20 6e 53 65 70 29 3b 0a 20 20 20 20  Sep, nSep);.    
2f37c 7d 0a 20 20 20 20 7a 56 61 6c 20 3d 20 28 63 68  }.    zVal = (ch
2f37d 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
2f37e 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
2f37f 0a 20 20 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69  .    nVal = sqli
2f380 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
2f381 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 73 71  argv[0]);.    sq
2f382 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
2f383 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 56 61 6c  end(pAccum, zVal
2f384 2c 20 6e 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 73  , nVal);.  }.}.s
2f385 74 61 74 69 63 20 76 6f 69 64 20 67 72 6f 75 70  tatic void group
2f386 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65 28 73  ConcatFinalize(s
2f387 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
2f388 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 74 72 41  context){.  StrA
2f389 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20  ccum *pAccum;.  
2f38a 70 41 63 63 75 6d 20 3d 20 73 71 6c 69 74 65 33  pAccum = sqlite3
2f38b 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
2f38c 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  xt(context, 0);.
2f38d 20 20 69 66 28 20 70 41 63 63 75 6d 20 29 7b 0a    if( pAccum ){.
2f38e 20 20 20 20 69 66 28 20 70 41 63 63 75 6d 2d 3e      if( pAccum->
2f38f 74 6f 6f 42 69 67 20 29 7b 0a 20 20 20 20 20 20  tooBig ){.      
2f390 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2f391 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74  rror_toobig(cont
2f392 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  ext);.    }else 
2f393 69 66 28 20 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c  if( pAccum->mall
2f394 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
2f395 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2f396 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e  _error_nomem(con
2f397 74 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  text);.    }else
2f398 7b 20 20 20 20 0a 20 20 20 20 20 20 73 71 6c 69  {    .      sqli
2f399 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
2f39a 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33  context, sqlite3
2f39b 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 70  StrAccumFinish(p
2f39c 41 63 63 75 6d 29 2c 20 2d 31 2c 20 0a 20 20 20  Accum), -1, .   
2f39d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f39e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
2f39f 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ree);.    }.  }.
2f3a0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
2f3a1 75 74 69 6e 65 20 64 6f 65 73 20 70 65 72 2d 63  utine does per-c
2f3a2 6f 6e 6e 65 63 74 69 6f 6e 20 66 75 6e 63 74 69  onnection functi
2f3a3 6f 6e 20 72 65 67 69 73 74 72 61 74 69 6f 6e 2e  on registration.
2f3a4 20 20 4d 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 65    Most.** of the
2f3a5 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69   built-in functi
2f3a6 6f 6e 73 20 61 62 6f 76 65 20 61 72 65 20 70 61  ons above are pa
2f3a7 72 74 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c  rt of the global
2f3a8 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 2e 0a 2a   function set..*
2f3a9 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f  * This routine o
2f3aa 6e 6c 79 20 64 65 61 6c 73 20 77 69 74 68 20 74  nly deals with t
2f3ab 68 6f 73 65 20 74 68 61 74 20 61 72 65 20 6e 6f  hose that are no
2f3ac 74 20 67 6c 6f 62 61 6c 2e 0a 2a 2f 0a 53 51 4c  t global..*/.SQL
2f3ad 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2f3ae 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
2f3af 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73  BuiltinFunctions
2f3b0 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
2f3b1 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
2f3b2 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
2f3b3 69 6f 6e 28 64 62 2c 20 22 4d 41 54 43 48 22 2c  ion(db, "MATCH",
2f3b4 20 32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72   2);.  assert( r
2f3b5 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
2f3b6 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  || rc==SQLITE_OK
2f3b7 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   );.  if( rc==SQ
2f3b8 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  LITE_NOMEM ){.  
2f3b9 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
2f3ba 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ed = 1;.  }.}../
2f3bb 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 4c 49 4b  *.** Set the LIK
2f3bc 45 4f 50 54 20 66 6c 61 67 20 6f 6e 20 74 68 65  EOPT flag on the
2f3bd 20 32 2d 61 72 67 75 6d 65 6e 74 20 66 75 6e 63   2-argument func
2f3be 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69  tion with the gi
2f3bf 76 65 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61  ven name..*/.sta
2f3c0 74 69 63 20 76 6f 69 64 20 73 65 74 4c 69 6b 65  tic void setLike
2f3c1 4f 70 74 46 6c 61 67 28 73 71 6c 69 74 65 33 20  OptFlag(sqlite3 
2f3c2 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
2f3c3 2a 7a 4e 61 6d 65 2c 20 75 38 20 66 6c 61 67 56  *zName, u8 flagV
2f3c4 61 6c 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  al){.  FuncDef *
2f3c5 70 44 65 66 3b 0a 20 20 70 44 65 66 20 3d 20 73  pDef;.  pDef = s
2f3c6 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
2f3c7 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 73 71  on(db, zName, sq
2f3c8 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
2f3c9 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20  ame),.          
2f3ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f3cb 20 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46     2, SQLITE_UTF
2f3cc 38 2c 20 30 29 3b 0a 20 20 69 66 28 20 41 4c 57  8, 0);.  if( ALW
2f3cd 41 59 53 28 70 44 65 66 29 20 29 7b 0a 20 20 20  AYS(pDef) ){.   
2f3ce 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 3d 20 66   pDef->flags = f
2f3cf 6c 61 67 56 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f  lagVal;.  }.}../
2f3d0 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68  *.** Register th
2f3d1 65 20 62 75 69 6c 74 2d 69 6e 20 4c 49 4b 45 20  e built-in LIKE 
2f3d2 61 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f  and GLOB functio
2f3d3 6e 73 2e 20 20 54 68 65 20 63 61 73 65 53 65 6e  ns.  The caseSen
2f3d4 73 69 74 69 76 65 0a 2a 2a 20 70 61 72 61 6d 65  sitive.** parame
2f3d5 74 65 72 20 64 65 74 65 72 6d 69 6e 65 73 20 77  ter determines w
2f3d6 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
2f3d7 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20  e LIKE operator 
2f3d8 69 73 20 63 61 73 65 0a 2a 2a 20 73 65 6e 73 69  is case.** sensi
2f3d9 74 69 76 65 2e 20 20 47 4c 4f 42 20 69 73 20 61  tive.  GLOB is a
2f3da 6c 77 61 79 73 20 63 61 73 65 20 73 65 6e 73 69  lways case sensi
2f3db 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tive..*/.SQLITE_
2f3dc 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2f3dd 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65  ite3RegisterLike
2f3de 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65  Functions(sqlite
2f3df 33 20 2a 64 62 2c 20 69 6e 74 20 63 61 73 65 53  3 *db, int caseS
2f3e0 65 6e 73 69 74 69 76 65 29 7b 0a 20 20 73 74 72  ensitive){.  str
2f3e1 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  uct compareInfo 
2f3e2 2a 70 49 6e 66 6f 3b 0a 20 20 69 66 28 20 63 61  *pInfo;.  if( ca
2f3e3 73 65 53 65 6e 73 69 74 69 76 65 20 29 7b 0a 20  seSensitive ){. 
2f3e4 20 20 20 70 49 6e 66 6f 20 3d 20 28 73 74 72 75     pInfo = (stru
2f3e5 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29  ct compareInfo*)
2f3e6 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 3b 0a 20 20  &likeInfoAlt;.  
2f3e7 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f  }else{.    pInfo
2f3e8 20 3d 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61   = (struct compa
2f3e9 72 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e 66  reInfo*)&likeInf
2f3ea 6f 4e 6f 72 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c  oNorm;.  }.  sql
2f3eb 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64  ite3CreateFunc(d
2f3ec 62 2c 20 22 6c 69 6b 65 22 2c 20 32 2c 20 53 51  b, "like", 2, SQ
2f3ed 4c 49 54 45 5f 55 54 46 38 2c 20 70 49 6e 66 6f  LITE_UTF8, pInfo
2f3ee 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30  , likeFunc, 0, 0
2f3ef 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 43  , 0);.  sqlite3C
2f3f0 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 6c  reateFunc(db, "l
2f3f1 69 6b 65 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f  ike", 3, SQLITE_
2f3f2 55 54 46 38 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b  UTF8, pInfo, lik
2f3f3 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20 30 29 3b  eFunc, 0, 0, 0);
2f3f4 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65  .  sqlite3Create
2f3f5 46 75 6e 63 28 64 62 2c 20 22 67 6c 6f 62 22 2c  Func(db, "glob",
2f3f6 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
2f3f7 20 0a 20 20 20 20 20 20 28 73 74 72 75 63 74 20   .      (struct 
2f3f8 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26 67 6c  compareInfo*)&gl
2f3f9 6f 62 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63  obInfo, likeFunc
2f3fa 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 73 65  , 0, 0, 0);.  se
2f3fb 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 64 62 2c  tLikeOptFlag(db,
2f3fc 20 22 67 6c 6f 62 22 2c 20 53 51 4c 49 54 45 5f   "glob", SQLITE_
2f3fd 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c 49  FUNC_LIKE | SQLI
2f3fe 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 3b 0a 20  TE_FUNC_CASE);. 
2f3ff 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28   setLikeOptFlag(
2f400 64 62 2c 20 22 6c 69 6b 65 22 2c 20 0a 20 20 20  db, "like", .   
2f401 20 20 20 63 61 73 65 53 65 6e 73 69 74 69 76 65     caseSensitive
2f402 20 3f 20 28 53 51 4c 49 54 45 5f 46 55 4e 43 5f   ? (SQLITE_FUNC_
2f403 4c 49 4b 45 20 7c 20 53 51 4c 49 54 45 5f 46 55  LIKE | SQLITE_FU
2f404 4e 43 5f 43 41 53 45 29 20 3a 20 53 51 4c 49 54  NC_CASE) : SQLIT
2f405 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 3b 0a 7d 0a  E_FUNC_LIKE);.}.
2f406 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 70 6f 69  ./*.** pExpr poi
2f407 6e 74 73 20 74 6f 20 61 6e 20 65 78 70 72 65 73  nts to an expres
2f408 73 69 6f 6e 20 77 68 69 63 68 20 69 6d 70 6c 65  sion which imple
2f409 6d 65 6e 74 73 20 61 20 66 75 6e 63 74 69 6f 6e  ments a function
2f40a 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 20 61  .  If.** it is a
2f40b 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20 61 70  ppropriate to ap
2f40c 70 6c 79 20 74 68 65 20 4c 49 4b 45 20 6f 70 74  ply the LIKE opt
2f40d 69 6d 69 7a 61 74 69 6f 6e 20 74 6f 20 74 68 61  imization to tha
2f40e 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68  t function.** th
2f40f 65 6e 20 73 65 74 20 61 57 63 5b 30 5d 20 74 68  en set aWc[0] th
2f410 72 6f 75 67 68 20 61 57 63 5b 32 5d 20 74 6f 20  rough aWc[2] to 
2f411 74 68 65 20 77 69 6c 64 63 61 72 64 20 63 68 61  the wildcard cha
2f412 72 61 63 74 65 72 73 20 61 6e 64 0a 2a 2a 20 72  racters and.** r
2f413 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66 20  eturn TRUE.  If 
2f414 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
2f415 6e 6f 74 20 61 20 4c 49 4b 45 2d 73 74 79 6c 65  not a LIKE-style
2f416 20 66 75 6e 63 74 69 6f 6e 20 74 68 65 6e 0a 2a   function then.*
2f417 2a 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a  * return FALSE..
2f418 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2f419 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4c  E int sqlite3IsL
2f41a 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69  ikeFunction(sqli
2f41b 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70  te3 *db, Expr *p
2f41c 45 78 70 72 2c 20 69 6e 74 20 2a 70 49 73 4e 6f  Expr, int *pIsNo
2f41d 63 61 73 65 2c 20 63 68 61 72 20 2a 61 57 63 29  case, char *aWc)
2f41e 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65  {.  FuncDef *pDe
2f41f 66 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  f;.  if( pExpr->
2f420 6f 70 21 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20  op!=TK_FUNCTION 
2f421 0a 20 20 20 7c 7c 20 21 70 45 78 70 72 2d 3e 78  .   || !pExpr->x
2f422 2e 70 4c 69 73 74 20 0a 20 20 20 7c 7c 20 70 45  .pList .   || pE
2f423 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45  xpr->x.pList->nE
2f424 78 70 72 21 3d 32 0a 20 20 29 7b 0a 20 20 20 20  xpr!=2.  ){.    
2f425 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
2f426 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2f427 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
2f428 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
2f429 0a 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65  .  pDef = sqlite
2f42a 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62  3FindFunction(db
2f42b 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  , pExpr->u.zToke
2f42c 6e 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  n, .            
2f42d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f42e 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2f42f 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e  (pExpr->u.zToken
2f430 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
2f431 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f432 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  2, SQLITE_UTF8, 
2f433 30 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28  0);.  if( NEVER(
2f434 70 44 65 66 3d 3d 30 29 20 7c 7c 20 28 70 44 65  pDef==0) || (pDe
2f435 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f->flags & SQLIT
2f436 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 3d 3d 30 20  E_FUNC_LIKE)==0 
2f437 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
2f438 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6d  .  }..  /* The m
2f439 65 6d 63 70 79 28 29 20 73 74 61 74 65 6d 65 6e  emcpy() statemen
2f43a 74 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74  t assumes that t
2f43b 68 65 20 77 69 6c 64 63 61 72 64 20 63 68 61 72  he wildcard char
2f43c 61 63 74 65 72 73 20 61 72 65 0a 20 20 2a 2a 20  acters are.  ** 
2f43d 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20  the first three 
2f43e 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68  statements in th
2f43f 65 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 73 74  e compareInfo st
2f440 72 75 63 74 75 72 65 2e 20 20 54 68 65 0a 20 20  ructure.  The.  
2f441 2a 2a 20 61 73 73 65 72 74 73 28 29 20 74 68 61  ** asserts() tha
2f442 74 20 66 6f 6c 6c 6f 77 20 76 65 72 69 66 79 20  t follow verify 
2f443 74 68 61 74 20 61 73 73 75 6d 70 74 69 6f 6e 0a  that assumption.
2f444 20 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 61 57    */.  memcpy(aW
2f445 63 2c 20 70 44 65 66 2d 3e 70 55 73 65 72 44 61  c, pDef->pUserDa
2f446 74 61 2c 20 33 29 3b 0a 20 20 61 73 73 65 72 74  ta, 3);.  assert
2f447 28 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e  ( (char*)&likeIn
2f448 66 6f 41 6c 74 20 3d 3d 20 28 63 68 61 72 2a 29  foAlt == (char*)
2f449 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74  &likeInfoAlt.mat
2f44a 63 68 41 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72  chAll );.  asser
2f44b 74 28 20 26 28 28 63 68 61 72 2a 29 26 6c 69 6b  t( &((char*)&lik
2f44c 65 49 6e 66 6f 41 6c 74 29 5b 31 5d 20 3d 3d 20  eInfoAlt)[1] == 
2f44d 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f  (char*)&likeInfo
2f44e 41 6c 74 2e 6d 61 74 63 68 4f 6e 65 20 29 3b 0a  Alt.matchOne );.
2f44f 20 20 61 73 73 65 72 74 28 20 26 28 28 63 68 61    assert( &((cha
2f450 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 29  r*)&likeInfoAlt)
2f451 5b 32 5d 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c  [2] == (char*)&l
2f452 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68  ikeInfoAlt.match
2f453 53 65 74 20 29 3b 0a 20 20 2a 70 49 73 4e 6f 63  Set );.  *pIsNoc
2f454 61 73 65 20 3d 20 28 70 44 65 66 2d 3e 66 6c 61  ase = (pDef->fla
2f455 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43  gs & SQLITE_FUNC
2f456 5f 43 41 53 45 29 3d 3d 30 3b 0a 20 20 72 65 74  _CASE)==0;.  ret
2f457 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 1;.}../*.** 
2f458 41 6c 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20 46  All all of the F
2f459 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65  uncDef structure
2f45a 73 20 69 6e 20 74 68 65 20 61 42 75 69 6c 74 69  s in the aBuilti
2f45b 6e 46 75 6e 63 5b 5d 20 61 72 72 61 79 20 61 62  nFunc[] array ab
2f45c 6f 76 65 0a 2a 2a 20 74 6f 20 74 68 65 20 67 6c  ove.** to the gl
2f45d 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 20 68 61  obal function ha
2f45e 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20  sh table.  This 
2f45f 6f 63 63 75 72 73 20 61 74 20 73 74 61 72 74 2d  occurs at start-
2f460 74 69 6d 65 20 28 61 73 0a 2a 2a 20 61 20 63 6f  time (as.** a co
2f461 6e 73 65 71 75 65 6e 63 65 20 6f 66 20 63 61 6c  nsequence of cal
2f462 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69  ling sqlite3_ini
2f463 74 69 61 6c 69 7a 65 28 29 29 2e 0a 2a 2a 0a 2a  tialize())..**.*
2f464 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75  * After this rou
2f465 74 69 6e 65 20 72 75 6e 73 0a 2a 2f 0a 53 51 4c  tine runs.*/.SQL
2f466 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2f467 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
2f468 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28  GlobalFunctions(
2f469 76 6f 69 64 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a  void){.  /*.  **
2f46a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
2f46b 72 72 61 79 20 68 6f 6c 64 73 20 46 75 6e 63 44  rray holds FuncD
2f46c 65 66 20 73 74 72 75 63 74 75 72 65 73 20 66 6f  ef structures fo
2f46d 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 75 6e  r all of the fun
2f46e 63 74 69 6f 6e 73 0a 20 20 2a 2a 20 64 65 66 69  ctions.  ** defi
2f46f 6e 65 64 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ned in this file
2f470 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
2f471 61 72 72 61 79 20 63 61 6e 6e 6f 74 20 62 65 20  array cannot be 
2f472 63 6f 6e 73 74 61 6e 74 20 73 69 6e 63 65 20 63  constant since c
2f473 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20  hanges are made 
2f474 74 6f 20 74 68 65 0a 20 20 2a 2a 20 46 75 6e 63  to the.  ** Func
2f475 44 65 66 2e 70 48 61 73 68 20 65 6c 65 6d 65 6e  Def.pHash elemen
2f476 74 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65  ts at start-time
2f477 2e 20 20 54 68 65 20 65 6c 65 6d 65 6e 74 73 20  .  The elements 
2f478 6f 66 20 74 68 69 73 20 61 72 72 61 79 0a 20 20  of this array.  
2f479 2a 2a 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79  ** are read-only
2f47a 20 61 66 74 65 72 20 69 6e 69 74 69 61 6c 69 7a   after initializ
2f47b 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74  ation is complet
2f47c 65 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63  e..  */.  static
2f47d 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63   SQLITE_WSD Func
2f47e 44 65 66 20 61 42 75 69 6c 74 69 6e 46 75 6e 63  Def aBuiltinFunc
2f47f 5b 5d 20 3d 20 7b 0a 20 20 20 20 46 55 4e 43 54  [] = {.    FUNCT
2f480 49 4f 4e 28 6c 74 72 69 6d 2c 20 20 20 20 20 20  ION(ltrim,      
2f481 20 20 20 20 20 20 20 20 31 2c 20 31 2c 20 30 2c          1, 1, 0,
2f482 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20   trimFunc       
2f483 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
2f484 4e 28 6c 74 72 69 6d 2c 20 20 20 20 20 20 20 20  N(ltrim,        
2f485 20 20 20 20 20 20 32 2c 20 31 2c 20 30 2c 20 74        2, 1, 0, t
2f486 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20  rimFunc         
2f487 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
2f488 72 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20  rtrim,          
2f489 20 20 20 20 31 2c 20 32 2c 20 30 2c 20 74 72 69      1, 2, 0, tri
2f48a 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c  mFunc         ),
2f48b 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 74  .    FUNCTION(rt
2f48c 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  rim,            
2f48d 20 20 32 2c 20 32 2c 20 30 2c 20 74 72 69 6d 46    2, 2, 0, trimF
2f48e 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20  unc         ),. 
2f48f 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 72 69 6d     FUNCTION(trim
2f490 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2f491 31 2c 20 33 2c 20 30 2c 20 74 72 69 6d 46 75 6e  1, 3, 0, trimFun
2f492 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20  c         ),.   
2f493 20 46 55 4e 43 54 49 4f 4e 28 74 72 69 6d 2c 20   FUNCTION(trim, 
2f494 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c                2,
2f495 20 33 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20   3, 0, trimFunc 
2f496 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
2f497 55 4e 43 54 49 4f 4e 28 6d 69 6e 2c 20 20 20 20  UNCTION(min,    
2f498 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30             -1, 0
2f499 2c 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e 63 20  , 1, minmaxFunc 
2f49a 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
2f49b 43 54 49 4f 4e 28 6d 69 6e 2c 20 20 20 20 20 20  CTION(min,      
2f49c 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20            0, 0, 
2f49d 31 2c 20 30 20 20 20 20 20 20 20 20 20 20 20 20  1, 0            
2f49e 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45      ),.    AGGRE
2f49f 47 41 54 45 28 6d 69 6e 2c 20 20 20 20 20 20 20  GATE(min,       
2f4a0 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 31 2c          1, 0, 1,
2f4a1 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20 20 20   minmaxStep,    
2f4a2 20 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65    minMaxFinalize
2f4a3 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
2f4a4 28 6d 61 78 2c 20 20 20 20 20 20 20 20 20 20 20  (max,           
2f4a5 20 20 20 20 2d 31 2c 20 31 2c 20 31 2c 20 6d 69      -1, 1, 1, mi
2f4a6 6e 6d 61 78 46 75 6e 63 20 20 20 20 20 20 20 29  nmaxFunc       )
2f4a7 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d  ,.    FUNCTION(m
2f4a8 61 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ax,             
2f4a9 20 20 20 30 2c 20 31 2c 20 31 2c 20 30 20 20 20     0, 1, 1, 0   
2f4aa 20 20 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a               ),.
2f4ab 20 20 20 20 41 47 47 52 45 47 41 54 45 28 6d 61      AGGREGATE(ma
2f4ac 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
2f4ad 20 31 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78   1, 1, 1, minmax
2f4ae 53 74 65 70 2c 20 20 20 20 20 20 6d 69 6e 4d 61  Step,      minMa
2f4af 78 46 69 6e 61 6c 69 7a 65 20 29 2c 0a 20 20 20  xFinalize ),.   
2f4b0 20 46 55 4e 43 54 49 4f 4e 32 28 74 79 70 65 6f   FUNCTION2(typeo
2f4b1 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 31 2c  f,            1,
2f4b2 20 30 2c 20 30 2c 20 74 79 70 65 6f 66 46 75 6e   0, 0, typeofFun
2f4b3 63 2c 20 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  c,  SQLITE_FUNC_
2f4b4 54 59 50 45 4f 46 29 2c 0a 20 20 20 20 46 55 4e  TYPEOF),.    FUN
2f4b5 43 54 49 4f 4e 32 28 6c 65 6e 67 74 68 2c 20 20  CTION2(length,  
2f4b6 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
2f4b7 30 2c 20 6c 65 6e 67 74 68 46 75 6e 63 2c 20 20  0, lengthFunc,  
2f4b8 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 45 4e 47  SQLITE_FUNC_LENG
2f4b9 54 48 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f  TH),.    FUNCTIO
2f4ba 4e 28 69 6e 73 74 72 2c 20 20 20 20 20 20 20 20  N(instr,        
2f4bb 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 69        2, 0, 0, i
2f4bc 6e 73 74 72 46 75 6e 63 20 20 20 20 20 20 20 20  nstrFunc        
2f4bd 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
2f4be 73 75 62 73 74 72 2c 20 20 20 20 20 20 20 20 20  substr,         
2f4bf 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 73 75 62      2, 0, 0, sub
2f4c0 73 74 72 46 75 6e 63 20 20 20 20 20 20 20 29 2c  strFunc       ),
2f4c1 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75  .    FUNCTION(su
2f4c2 62 73 74 72 2c 20 20 20 20 20 20 20 20 20 20 20  bstr,           
2f4c3 20 20 33 2c 20 30 2c 20 30 2c 20 73 75 62 73 74    3, 0, 0, subst
2f4c4 72 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20  rFunc       ),. 
2f4c5 20 20 20 46 55 4e 43 54 49 4f 4e 28 61 62 73 2c     FUNCTION(abs,
2f4c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4c7 31 2c 20 30 2c 20 30 2c 20 61 62 73 46 75 6e 63  1, 0, 0, absFunc
2f4c8 20 20 20 20 20 20 20 20 20 20 29 2c 0a 23 69 66            ),.#if
2f4c9 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2f4ca 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
2f4cb 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75      FUNCTION(rou
2f4cc 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nd,             
2f4cd 20 31 2c 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46   1, 0, 0, roundF
2f4ce 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20  unc        ),.  
2f4cf 20 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64    FUNCTION(round
2f4d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32  ,              2
2f4d1 2c 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e  , 0, 0, roundFun
2f4d2 63 20 20 20 20 20 20 20 20 29 2c 0a 23 65 6e 64  c        ),.#end
2f4d3 69 66 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  if.    FUNCTION(
2f4d4 75 70 70 65 72 2c 20 20 20 20 20 20 20 20 20 20  upper,          
2f4d5 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 75 70 70      1, 0, 0, upp
2f4d6 65 72 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c  erFunc        ),
2f4d7 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 6f  .    FUNCTION(lo
2f4d8 77 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  wer,            
2f4d9 20 20 31 2c 20 30 2c 20 30 2c 20 6c 6f 77 65 72    1, 0, 0, lower
2f4da 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20  Func        ),. 
2f4db 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c     FUNCTION(coal
2f4dc 65 73 63 65 2c 20 20 20 20 20 20 20 20 20 20 20  esce,           
2f4dd 31 2c 20 30 2c 20 30 2c 20 30 20 20 20 20 20 20  1, 0, 0, 0      
2f4de 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20            ),.   
2f4df 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73   FUNCTION(coales
2f4e0 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 30 2c  ce,           0,
2f4e1 20 30 2c 20 30 2c 20 30 20 20 20 20 20 20 20 20   0, 0, 0        
2f4e2 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
2f4e3 55 4e 43 54 49 4f 4e 32 28 63 6f 61 6c 65 73 63  UNCTION2(coalesc
2f4e4 65 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30  e,         -1, 0
2f4e5 2c 20 30 2c 20 69 66 6e 75 6c 6c 46 75 6e 63 2c  , 0, ifnullFunc,
2f4e6 20 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f    SQLITE_FUNC_CO
2f4e7 41 4c 45 53 43 45 29 2c 0a 20 20 20 20 46 55 4e  ALESCE),.    FUN
2f4e8 43 54 49 4f 4e 28 68 65 78 2c 20 20 20 20 20 20  CTION(hex,      
2f4e9 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
2f4ea 30 2c 20 68 65 78 46 75 6e 63 20 20 20 20 20 20  0, hexFunc      
2f4eb 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
2f4ec 49 4f 4e 32 28 69 66 6e 75 6c 6c 2c 20 20 20 20  ION2(ifnull,    
2f4ed 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c          2, 0, 0,
2f4ee 20 69 66 6e 75 6c 6c 46 75 6e 63 2c 20 20 53 51   ifnullFunc,  SQ
2f4ef 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41 4c 45 53  LITE_FUNC_COALES
2f4f0 43 45 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f  CE),.    FUNCTIO
2f4f1 4e 28 72 61 6e 64 6f 6d 2c 20 20 20 20 20 20 20  N(random,       
2f4f2 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 72        0, 0, 0, r
2f4f3 61 6e 64 6f 6d 46 75 6e 63 20 20 20 20 20 20 20  andomFunc       
2f4f4 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
2f4f5 72 61 6e 64 6f 6d 62 6c 6f 62 2c 20 20 20 20 20  randomblob,     
2f4f6 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 72 61 6e      1, 0, 0, ran
2f4f7 64 6f 6d 42 6c 6f 62 20 20 20 20 20 20 20 29 2c  domBlob       ),
2f4f8 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6e 75  .    FUNCTION(nu
2f4f9 6c 6c 69 66 2c 20 20 20 20 20 20 20 20 20 20 20  llif,           
2f4fa 20 20 32 2c 20 30 2c 20 31 2c 20 6e 75 6c 6c 69    2, 0, 1, nulli
2f4fb 66 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20  fFunc       ),. 
2f4fc 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69     FUNCTION(sqli
2f4fd 74 65 5f 76 65 72 73 69 6f 6e 2c 20 20 20 20 20  te_version,     
2f4fe 30 2c 20 30 2c 20 30 2c 20 76 65 72 73 69 6f 6e  0, 0, 0, version
2f4ff 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20  Func      ),.   
2f500 20 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65   FUNCTION(sqlite
2f501 5f 73 6f 75 72 63 65 5f 69 64 2c 20 20 20 30 2c  _source_id,   0,
2f502 20 30 2c 20 30 2c 20 73 6f 75 72 63 65 69 64 46   0, 0, sourceidF
2f503 75 6e 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46  unc     ),.    F
2f504 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f 6c  UNCTION(sqlite_l
2f505 6f 67 2c 20 20 20 20 20 20 20 20 20 32 2c 20 30  og,         2, 0
2f506 2c 20 30 2c 20 65 72 72 6c 6f 67 46 75 6e 63 20  , 0, errlogFunc 
2f507 20 20 20 20 20 20 29 2c 0a 23 69 66 6e 64 65 66        ),.#ifndef
2f508 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
2f509 50 49 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53  PILEOPTION_DIAGS
2f50a 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 71  .    FUNCTION(sq
2f50b 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 6f 70 74 69  lite_compileopti
2f50c 6f 6e 5f 75 73 65 64 2c 31 2c 20 30 2c 20 30 2c  on_used,1, 0, 0,
2f50d 20 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 75 73   compileoptionus
2f50e 65 64 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46  edFunc  ),.    F
2f50f 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f 63  UNCTION(sqlite_c
2f510 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74  ompileoption_get
2f511 2c 20 31 2c 20 30 2c 20 30 2c 20 63 6f 6d 70 69  , 1, 0, 0, compi
2f512 6c 65 6f 70 74 69 6f 6e 67 65 74 46 75 6e 63 20  leoptiongetFunc 
2f513 20 29 2c 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51   ),.#endif /* SQ
2f514 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c  LITE_OMIT_COMPIL
2f515 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53 20 2a 2f  EOPTION_DIAGS */
2f516 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 71 75  .    FUNCTION(qu
2f517 6f 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ote,            
2f518 20 20 31 2c 20 30 2c 20 30 2c 20 71 75 6f 74 65    1, 0, 0, quote
2f519 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20  Func        ),. 
2f51a 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 61 73 74     FUNCTION(last
2f51b 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c 20 20  _insert_rowid,  
2f51c 30 2c 20 30 2c 20 30 2c 20 6c 61 73 74 5f 69 6e  0, 0, 0, last_in
2f51d 73 65 72 74 5f 72 6f 77 69 64 29 2c 0a 20 20 20  sert_rowid),.   
2f51e 20 46 55 4e 43 54 49 4f 4e 28 63 68 61 6e 67 65   FUNCTION(change
2f51f 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 30 2c  s,            0,
2f520 20 30 2c 20 30 2c 20 63 68 61 6e 67 65 73 20 20   0, 0, changes  
2f521 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
2f522 55 4e 43 54 49 4f 4e 28 74 6f 74 61 6c 5f 63 68  UNCTION(total_ch
2f523 61 6e 67 65 73 2c 20 20 20 20 20 20 30 2c 20 30  anges,      0, 0
2f524 2c 20 30 2c 20 74 6f 74 61 6c 5f 63 68 61 6e 67  , 0, total_chang
2f525 65 73 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e  es    ),.    FUN
2f526 43 54 49 4f 4e 28 72 65 70 6c 61 63 65 2c 20 20  CTION(replace,  
2f527 20 20 20 20 20 20 20 20 20 20 33 2c 20 30 2c 20            3, 0, 
2f528 30 2c 20 72 65 70 6c 61 63 65 46 75 6e 63 20 20  0, replaceFunc  
2f529 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
2f52a 49 4f 4e 28 7a 65 72 6f 62 6c 6f 62 2c 20 20 20  ION(zeroblob,   
2f52b 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
2f52c 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20 20   zeroblobFunc   
2f52d 20 20 29 2c 0a 20 20 23 69 66 64 65 66 20 53 51    ),.  #ifdef SQ
2f52e 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0a 20 20 20  LITE_SOUNDEX.   
2f52f 20 46 55 4e 43 54 49 4f 4e 28 73 6f 75 6e 64 65   FUNCTION(sounde
2f530 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 31 2c  x,            1,
2f531 20 30 2c 20 30 2c 20 73 6f 75 6e 64 65 78 46 75   0, 0, soundexFu
2f532 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 23 65 6e  nc      ),.  #en
2f533 64 69 66 0a 20 20 23 69 66 6e 64 65 66 20 53 51  dif.  #ifndef SQ
2f534 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
2f535 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20 46 55 4e  XTENSION.    FUN
2f536 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e  CTION(load_exten
2f537 73 69 6f 6e 2c 20 20 20 20 20 31 2c 20 30 2c 20  sion,     1, 0, 
2f538 30 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20  0, loadExt      
2f539 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
2f53a 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73 69  ION(load_extensi
2f53b 6f 6e 2c 20 20 20 20 20 32 2c 20 30 2c 20 30 2c  on,     2, 0, 0,
2f53c 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20 20   loadExt        
2f53d 20 20 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20 20    ),.  #endif.  
2f53e 20 20 41 47 47 52 45 47 41 54 45 28 73 75 6d 2c    AGGREGATE(sum,
2f53f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
2f540 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c  , 0, 0, sumStep,
2f541 20 20 20 20 20 20 20 20 20 73 75 6d 46 69 6e 61           sumFina
2f542 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41  lize    ),.    A
2f543 47 47 52 45 47 41 54 45 28 74 6f 74 61 6c 2c 20  GGREGATE(total, 
2f544 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
2f545 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20  , 0, sumStep,   
2f546 20 20 20 20 20 20 74 6f 74 61 6c 46 69 6e 61 6c        totalFinal
2f547 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47  ize    ),.    AG
2f548 47 52 45 47 41 54 45 28 61 76 67 2c 20 20 20 20  GREGATE(avg,    
2f549 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
2f54a 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20   0, sumStep,    
2f54b 20 20 20 20 20 61 76 67 46 69 6e 61 6c 69 7a 65       avgFinalize
2f54c 20 20 20 20 29 2c 0a 20 2f 2a 20 41 47 47 52 45      ),. /* AGGRE
2f54d 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20  GATE(count,     
2f54e 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c          0, 0, 0,
2f54f 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20   countStep,     
2f550 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20    countFinalize 
2f551 20 29 2c 20 2a 2f 0a 20 20 20 20 7b 30 2c 53 51   ), */.    {0,SQ
2f552 4c 49 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45  LITE_UTF8,SQLITE
2f553 5f 46 55 4e 43 5f 43 4f 55 4e 54 2c 30 2c 30 2c  _FUNC_COUNT,0,0,
2f554 30 2c 63 6f 75 6e 74 53 74 65 70 2c 63 6f 75 6e  0,countStep,coun
2f555 74 46 69 6e 61 6c 69 7a 65 2c 22 63 6f 75 6e 74  tFinalize,"count
2f556 22 2c 30 2c 30 7d 2c 0a 20 20 20 20 41 47 47 52  ",0,0},.    AGGR
2f557 45 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20  EGATE(count,    
2f558 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
2f559 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20  , countStep,    
2f55a 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65     countFinalize
2f55b 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41    ),.    AGGREGA
2f55c 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c  TE(group_concat,
2f55d 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 67        1, 0, 0, g
2f55e 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20  roupConcatStep, 
2f55f 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c  groupConcatFinal
2f560 69 7a 65 29 2c 0a 20 20 20 20 41 47 47 52 45 47  ize),.    AGGREG
2f561 41 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74  ATE(group_concat
2f562 2c 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20  ,      2, 0, 0, 
2f563 67 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c  groupConcatStep,
2f564 20 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61   groupConcatFina
2f565 6c 69 7a 65 29 2c 0a 20 20 0a 20 20 20 20 4c 49  lize),.  .    LI
2f566 4b 45 46 55 4e 43 28 67 6c 6f 62 2c 20 32 2c 20  KEFUNC(glob, 2, 
2f567 26 67 6c 6f 62 49 6e 66 6f 2c 20 53 51 4c 49 54  &globInfo, SQLIT
2f568 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49  E_FUNC_LIKE|SQLI
2f569 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20  TE_FUNC_CASE),. 
2f56a 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43   #ifdef SQLITE_C
2f56b 41 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49  ASE_SENSITIVE_LI
2f56c 4b 45 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28  KE.    LIKEFUNC(
2f56d 6c 69 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e  like, 2, &likeIn
2f56e 66 6f 41 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55  foAlt, SQLITE_FU
2f56f 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46  NC_LIKE|SQLITE_F
2f570 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 20 20 4c  UNC_CASE),.    L
2f571 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c  IKEFUNC(like, 3,
2f572 20 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53   &likeInfoAlt, S
2f573 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c  QLITE_FUNC_LIKE|
2f574 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45  SQLITE_FUNC_CASE
2f575 29 2c 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 4c  ),.  #else.    L
2f576 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c  IKEFUNC(like, 2,
2f577 20 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20   &likeInfoNorm, 
2f578 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45  SQLITE_FUNC_LIKE
2f579 29 2c 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28  ),.    LIKEFUNC(
2f57a 6c 69 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e  like, 3, &likeIn
2f57b 66 6f 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46  foNorm, SQLITE_F
2f57c 55 4e 43 5f 4c 49 4b 45 29 2c 0a 20 20 23 65 6e  UNC_LIKE),.  #en
2f57d 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20  dif.  };..  int 
2f57e 69 3b 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68  i;.  FuncDefHash
2f57f 20 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41   *pHash = &GLOBA
2f580 4c 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73  L(FuncDefHash, s
2f581 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63  qlite3GlobalFunc
2f582 74 69 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65  tions);.  FuncDe
2f583 66 20 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e 63  f *aFunc = (Func
2f584 44 65 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e  Def*)&GLOBAL(Fun
2f585 63 44 65 66 2c 20 61 42 75 69 6c 74 69 6e 46 75  cDef, aBuiltinFu
2f586 6e 63 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  nc);..  for(i=0;
2f587 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 42 75   i<ArraySize(aBu
2f588 69 6c 74 69 6e 46 75 6e 63 29 3b 20 69 2b 2b 29  iltinFunc); i++)
2f589 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e  {.    sqlite3Fun
2f58a 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 73 68  cDefInsert(pHash
2f58b 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20  , &aFunc[i]);.  
2f58c 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69 73  }.  sqlite3Regis
2f58d 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74  terDateTimeFunct
2f58e 69 6f 6e 73 28 29 3b 0a 23 69 66 6e 64 65 66 20  ions();.#ifndef 
2f58f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45  SQLITE_OMIT_ALTE
2f590 52 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33  RTABLE.  sqlite3
2f591 41 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 29  AlterFunctions()
2f592 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 2a 2a  ;.#endif.}../***
2f593 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2f594 6f 66 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a  of func.c ******
2f595 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f596 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f597 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2f598 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2f599 6e 20 66 69 6c 65 20 66 6b 65 79 2e 63 20 2a 2a  n file fkey.c **
2f59a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f59b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f59c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
2f59d 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
2f59e 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
2f59f 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
2f5a0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
2f5a1 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
2f5a2 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
2f5a3 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
2f5a4 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
2f5a5 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
2f5a6 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
2f5a7 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
2f5a8 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
2f5a9 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
2f5aa 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
2f5ab 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
2f5ac 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
2f5ad 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
2f5ae 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
2f5af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f5b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f5b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
2f5b3 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
2f5b4 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 62 79  ins code used by
2f5b5 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 74 6f   the compiler to
2f5b6 20 61 64 64 20 66 6f 72 65 69 67 6e 20 6b 65 79   add foreign key
2f5b7 0a 2a 2a 20 73 75 70 70 6f 72 74 20 74 6f 20 63  .** support to c
2f5b8 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74  ompiled SQL stat
2f5b9 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 0a 23 69 66 6e  ements..*/..#ifn
2f5ba 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2f5bb 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 23 69 66 6e  FOREIGN_KEY.#ifn
2f5bc 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2f5bd 54 52 49 47 47 45 52 0a 0a 2f 2a 0a 2a 2a 20 44  TRIGGER../*.** D
2f5be 65 66 65 72 72 65 64 20 61 6e 64 20 49 6d 6d 65  eferred and Imme
2f5bf 64 69 61 74 65 20 46 4b 73 0a 2a 2a 20 2d 2d 2d  diate FKs.** ---
2f5c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2f5c1 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 46 6f  -------.**.** Fo
2f5c2 72 65 69 67 6e 20 6b 65 79 73 20 69 6e 20 53 51  reign keys in SQ
2f5c3 4c 69 74 65 20 63 6f 6d 65 20 69 6e 20 74 77 6f  Lite come in two
2f5c4 20 66 6c 61 76 6f 75 72 73 3a 20 64 65 66 65 72   flavours: defer
2f5c5 72 65 64 20 61 6e 64 20 69 6d 6d 65 64 69 61 74  red and immediat
2f5c6 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 6d 6d 65  e..** If an imme
2f5c7 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65  diate foreign ke
2f5c8 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20  y constraint is 
2f5c9 76 69 6f 6c 61 74 65 64 2c 20 53 51 4c 49 54 45  violated, SQLITE
2f5ca 5f 43 4f 4e 53 54 52 41 49 4e 54 0a 2a 2a 20 69  _CONSTRAINT.** i
2f5cb 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
2f5cc 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65  he current state
2f5cd 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
2f5ce 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66   rolled back. If
2f5cf 20 61 20 0a 2a 2a 20 64 65 66 65 72 72 65 64 20   a .** deferred 
2f5d0 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
2f5d1 74 72 61 69 6e 74 20 69 73 20 76 69 6f 6c 61 74  traint is violat
2f5d2 65 64 2c 20 6e 6f 20 61 63 74 69 6f 6e 20 69 73  ed, no action is
2f5d3 20 74 61 6b 65 6e 20 0a 2a 2a 20 69 6d 6d 65 64   taken .** immed
2f5d4 69 61 74 65 6c 79 2e 20 48 6f 77 65 76 65 72 20  iately. However 
2f5d5 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  if the applicati
2f5d6 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 63  on attempts to c
2f5d7 6f 6d 6d 69 74 20 74 68 65 20 0a 2a 2a 20 74 72  ommit the .** tr
2f5d8 61 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65  ansaction before
2f5d9 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e 73   fixing the cons
2f5da 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
2f5db 2c 20 74 68 65 20 61 74 74 65 6d 70 74 20 66 61  , the attempt fa
2f5dc 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 65 72  ils..**.** Defer
2f5dd 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  red constraints 
2f5de 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  are implemented 
2f5df 75 73 69 6e 67 20 61 20 73 69 6d 70 6c 65 20 63  using a simple c
2f5e0 6f 75 6e 74 65 72 20 61 73 73 6f 63 69 61 74 65  ounter associate
2f5e1 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 64 61  d.** with the da
2f5e2 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 54  tabase handle. T
2f5e3 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 73 65  he counter is se
2f5e4 74 20 74 6f 20 7a 65 72 6f 20 65 61 63 68 20 74  t to zero each t
2f5e5 69 6d 65 20 61 20 0a 2a 2a 20 64 61 74 61 62 61  ime a .** databa
2f5e6 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  se transaction i
2f5e7 73 20 6f 70 65 6e 65 64 2e 20 45 61 63 68 20 74  s opened. Each t
2f5e8 69 6d 65 20 61 20 73 74 61 74 65 6d 65 6e 74 20  ime a statement 
2f5e9 69 73 20 65 78 65 63 75 74 65 64 20 0a 2a 2a 20  is executed .** 
2f5ea 74 68 61 74 20 63 61 75 73 65 73 20 61 20 66 6f  that causes a fo
2f5eb 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74  reign key violat
2f5ec 69 6f 6e 2c 20 74 68 65 20 63 6f 75 6e 74 65 72  ion, the counter
2f5ed 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e   is incremented.
2f5ee 20 45 61 63 68 0a 2a 2a 20 74 69 6d 65 20 61 20   Each.** time a 
2f5ef 73 74 61 74 65 6d 65 6e 74 20 69 73 20 65 78 65  statement is exe
2f5f0 63 75 74 65 64 20 74 68 61 74 20 72 65 6d 6f 76  cuted that remov
2f5f1 65 73 20 61 6e 20 65 78 69 73 74 69 6e 67 20 76  es an existing v
2f5f2 69 6f 6c 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a  iolation from.**
2f5f3 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74   the database, t
2f5f4 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 64 65  he counter is de
2f5f5 63 72 65 6d 65 6e 74 65 64 2e 20 57 68 65 6e 20  cremented. When 
2f5f6 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
2f5f7 69 73 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2c  is.** committed,
2f5f8 20 74 68 65 20 63 6f 6d 6d 69 74 20 66 61 69 6c   the commit fail
2f5f9 73 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74  s if the current
2f5fa 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 6f   value of the co
2f5fb 75 6e 74 65 72 20 69 73 0a 2a 2a 20 67 72 65 61  unter is.** grea
2f5fc 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e 20 54  ter than zero. T
2f5fd 68 69 73 20 73 63 68 65 6d 65 20 68 61 73 20 74  his scheme has t
2f5fe 77 6f 20 62 69 67 20 64 72 61 77 62 61 63 6b 73  wo big drawbacks
2f5ff 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 57 68 65 6e  :.**.**   * When
2f600 20 61 20 63 6f 6d 6d 69 74 20 66 61 69 6c 73 20   a commit fails 
2f601 64 75 65 20 74 6f 20 61 20 64 65 66 65 72 72 65  due to a deferre
2f602 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  d foreign key co
2f603 6e 73 74 72 61 69 6e 74 2c 20 0a 2a 2a 20 20 20  nstraint, .**   
2f604 20 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61    there is no wa
2f605 79 20 74 6f 20 74 65 6c 6c 20 77 68 69 63 68 20  y to tell which 
2f606 66 6f 72 65 69 67 6e 20 63 6f 6e 73 74 72 61 69  foreign constrai
2f607 6e 74 20 69 73 20 6e 6f 74 20 73 61 74 69 73 66  nt is not satisf
2f608 69 65 64 2c 0a 2a 2a 20 20 20 20 20 6f 72 20 77  ied,.**     or w
2f609 68 69 63 68 20 72 6f 77 20 69 74 20 69 73 20 6e  hich row it is n
2f60a 6f 74 20 73 61 74 69 73 66 69 65 64 20 66 6f 72  ot satisfied for
2f60b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74  ..**.**   * If t
2f60c 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
2f60d 61 69 6e 73 20 66 6f 72 65 69 67 6e 20 6b 65 79  ains foreign key
2f60e 20 76 69 6f 6c 61 74 69 6f 6e 73 20 77 68 65 6e   violations when
2f60f 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 74 72 61   the .**     tra
2f610 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e  nsaction is open
2f611 65 64 2c 20 74 68 69 73 20 6d 61 79 20 63 61 75  ed, this may cau
2f612 73 65 20 74 68 65 20 6d 65 63 68 61 6e 69 73 6d  se the mechanism
2f613 20 74 6f 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e   to malfunction.
2f614 0a 2a 2a 0a 2a 2a 20 44 65 73 70 69 74 65 20 74  .**.** Despite t
2f615 68 65 73 65 20 70 72 6f 62 6c 65 6d 73 2c 20 74  hese problems, t
2f616 68 69 73 20 61 70 70 72 6f 61 63 68 20 69 73 20  his approach is 
2f617 61 64 6f 70 74 65 64 20 61 73 20 69 74 20 73 65  adopted as it se
2f618 65 6d 73 20 73 69 6d 70 6c 65 72 0a 2a 2a 20 74  ems simpler.** t
2f619 68 61 6e 20 74 68 65 20 61 6c 74 65 72 6e 61 74  han the alternat
2f61a 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 53 45  ives..**.** INSE
2f61b 52 54 20 6f 70 65 72 61 74 69 6f 6e 73 3a 0a 2a  RT operations:.*
2f61c 2a 0a 2a 2a 20 20 20 49 2e 31 29 20 46 6f 72 20  *.**   I.1) For 
2f61d 65 61 63 68 20 46 4b 20 66 6f 72 20 77 68 69 63  each FK for whic
2f61e 68 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74  h the table is t
2f61f 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2c 20  he child table, 
2f620 73 65 61 72 63 68 0a 2a 2a 20 20 20 20 20 20 20  search.**       
2f621 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
2f622 65 20 66 6f 72 20 61 20 6d 61 74 63 68 2e 20 49  e for a match. I
2f623 66 20 6e 6f 6e 65 20 69 73 20 66 6f 75 6e 64 20  f none is found 
2f624 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 0a 2a 2a  increment the.**
2f625 20 20 20 20 20 20 20 20 63 6f 6e 73 74 72 61 69          constrai
2f626 6e 74 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a  nt counter..**.*
2f627 2a 20 20 20 49 2e 32 29 20 46 6f 72 20 65 61 63  *   I.2) For eac
2f628 68 20 46 4b 20 66 6f 72 20 77 68 69 63 68 20 74  h FK for which t
2f629 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  he table is the 
2f62a 70 61 72 65 6e 74 20 74 61 62 6c 65 2c 20 0a 2a  parent table, .*
2f62b 2a 20 20 20 20 20 20 20 20 73 65 61 72 63 68 20  *        search 
2f62c 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 20  the child table 
2f62d 66 6f 72 20 72 6f 77 73 20 74 68 61 74 20 63 6f  for rows that co
2f62e 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20  rrespond to the 
2f62f 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20 72 6f  new.**        ro
2f630 77 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  w in the parent 
2f631 74 61 62 6c 65 2e 20 44 65 63 72 65 6d 65 6e 74  table. Decrement
2f632 20 74 68 65 20 63 6f 75 6e 74 65 72 20 66 6f 72   the counter for
2f633 20 65 61 63 68 20 72 6f 77 0a 2a 2a 20 20 20 20   each row.**    
2f634 20 20 20 20 66 6f 75 6e 64 20 28 61 73 20 74 68      found (as th
2f635 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20  e constraint is 
2f636 6e 6f 77 20 73 61 74 69 73 66 69 65 64 29 2e 0a  now satisfied)..
2f637 2a 2a 0a 2a 2a 20 44 45 4c 45 54 45 20 6f 70 65  **.** DELETE ope
2f638 72 61 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20  rations:.**.**  
2f639 20 44 2e 31 29 20 46 6f 72 20 65 61 63 68 20 46   D.1) For each F
2f63a 4b 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20  K for which the 
2f63b 74 61 62 6c 65 20 69 73 20 74 68 65 20 63 68 69  table is the chi
2f63c 6c 64 20 74 61 62 6c 65 2c 20 0a 2a 2a 20 20 20  ld table, .**   
2f63d 20 20 20 20 20 73 65 61 72 63 68 20 74 68 65 20       search the 
2f63e 70 61 72 65 6e 74 20 74 61 62 6c 65 20 66 6f 72  parent table for
2f63f 20 61 20 72 6f 77 20 74 68 61 74 20 63 6f 72 72   a row that corr
2f640 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 0a  esponds to the .
2f641 2a 2a 20 20 20 20 20 20 20 20 64 65 6c 65 74 65  **        delete
2f642 64 20 72 6f 77 20 69 6e 20 74 68 65 20 63 68 69  d row in the chi
2f643 6c 64 20 74 61 62 6c 65 2e 20 49 66 20 73 75 63  ld table. If suc
2f644 68 20 61 20 72 6f 77 20 69 73 20 6e 6f 74 20 66  h a row is not f
2f645 6f 75 6e 64 2c 20 0a 2a 2a 20 20 20 20 20 20 20  ound, .**       
2f646 20 64 65 63 72 65 6d 65 6e 74 20 74 68 65 20 63   decrement the c
2f647 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20  ounter..**.**   
2f648 44 2e 32 29 20 46 6f 72 20 65 61 63 68 20 46 4b  D.2) For each FK
2f649 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 74   for which the t
2f64a 61 62 6c 65 20 69 73 20 74 68 65 20 70 61 72 65  able is the pare
2f64b 6e 74 20 74 61 62 6c 65 2c 20 73 65 61 72 63 68  nt table, search
2f64c 20 0a 2a 2a 20 20 20 20 20 20 20 20 74 68 65 20   .**        the 
2f64d 63 68 69 6c 64 20 74 61 62 6c 65 20 66 6f 72 20  child table for 
2f64e 72 6f 77 73 20 74 68 61 74 20 63 6f 72 72 65 73  rows that corres
2f64f 70 6f 6e 64 20 74 6f 20 74 68 65 20 64 65 6c 65  pond to the dele
2f650 74 65 64 20 72 6f 77 20 0a 2a 2a 20 20 20 20 20  ted row .**     
2f651 20 20 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74     in the parent
2f652 20 74 61 62 6c 65 2e 20 46 6f 72 20 65 61 63 68   table. For each
2f653 20 66 6f 75 6e 64 20 69 6e 63 72 65 6d 65 6e 74   found increment
2f654 20 74 68 65 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a   the counter..**
2f655 0a 2a 2a 20 55 50 44 41 54 45 20 6f 70 65 72 61  .** UPDATE opera
2f656 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 41  tions:.**.**   A
2f657 6e 20 55 50 44 41 54 45 20 63 6f 6d 6d 61 6e 64  n UPDATE command
2f658 20 72 65 71 75 69 72 65 73 20 74 68 61 74 20 61   requires that a
2f659 6c 6c 20 34 20 73 74 65 70 73 20 61 62 6f 76 65  ll 4 steps above
2f65a 20 61 72 65 20 74 61 6b 65 6e 2c 20 62 75 74 20   are taken, but 
2f65b 6f 6e 6c 79 0a 2a 2a 20 20 20 66 6f 72 20 46 4b  only.**   for FK
2f65c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f 72   constraints for
2f65d 20 77 68 69 63 68 20 74 68 65 20 61 66 66 65 63   which the affec
2f65e 74 65 64 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20  ted columns are 
2f65f 61 63 74 75 61 6c 6c 79 20 0a 2a 2a 20 20 20 6d  actually .**   m
2f660 6f 64 69 66 69 65 64 20 28 76 61 6c 75 65 73 20  odified (values 
2f661 6d 75 73 74 20 62 65 20 63 6f 6d 70 61 72 65 64  must be compared
2f662 20 61 74 20 72 75 6e 74 69 6d 65 29 2e 0a 2a 2a   at runtime)..**
2f663 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 49 2e  .** Note that I.
2f664 31 20 61 6e 64 20 44 2e 31 20 61 72 65 20 76 65  1 and D.1 are ve
2f665 72 79 20 73 69 6d 69 6c 61 72 20 6f 70 65 72 61  ry similar opera
2f666 74 69 6f 6e 73 2c 20 61 73 20 61 72 65 20 49 2e  tions, as are I.
2f667 32 20 61 6e 64 20 44 2e 32 2e 0a 2a 2a 20 54 68  2 and D.2..** Th
2f668 69 73 20 73 69 6d 70 6c 69 66 69 65 73 20 74 68  is simplifies th
2f669 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
2f66a 20 61 20 62 69 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f   a bit..**.** Fo
2f66b 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f  r the purposes o
2f66c 66 20 69 6d 6d 65 64 69 61 74 65 20 46 4b 20 63  f immediate FK c
2f66d 6f 6e 73 74 72 61 69 6e 74 73 2c 20 74 68 65 20  onstraints, the 
2f66e 4f 52 20 52 45 50 4c 41 43 45 20 63 6f 6e 66 6c  OR REPLACE confl
2f66f 69 63 74 0a 2a 2a 20 72 65 73 6f 6c 75 74 69 6f  ict.** resolutio
2f670 6e 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  n is considered 
2f671 74 6f 20 64 65 6c 65 74 65 20 72 6f 77 73 20 62  to delete rows b
2f672 65 66 6f 72 65 20 74 68 65 20 6e 65 77 20 72 6f  efore the new ro
2f673 77 20 69 73 20 69 6e 73 65 72 74 65 64 2e 0a 2a  w is inserted..*
2f674 2a 20 49 66 20 61 20 64 65 6c 65 74 65 20 63 61  * If a delete ca
2f675 75 73 65 64 20 62 79 20 4f 52 20 52 45 50 4c 41  used by OR REPLA
2f676 43 45 20 76 69 6f 6c 61 74 65 73 20 61 6e 20 46  CE violates an F
2f677 4b 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 61 6e  K constraint, an
2f678 20 65 78 63 65 70 74 69 6f 6e 0a 2a 2a 20 69 73   exception.** is
2f679 20 74 68 72 6f 77 6e 2c 20 65 76 65 6e 20 69 66   thrown, even if
2f67a 20 74 68 65 20 46 4b 20 63 6f 6e 73 74 72 61 69   the FK constrai
2f67b 6e 74 20 77 6f 75 6c 64 20 62 65 20 73 61 74 69  nt would be sati
2f67c 73 66 69 65 64 20 61 66 74 65 72 20 74 68 65 20  sfied after the 
2f67d 6e 65 77 20 0a 2a 2a 20 72 6f 77 20 69 73 20 69  new .** row is i
2f67e 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  nserted..**.** I
2f67f 6d 6d 65 64 69 61 74 65 20 63 6f 6e 73 74 72 61  mmediate constra
2f680 69 6e 74 73 20 61 72 65 20 75 73 75 61 6c 6c 79  ints are usually
2f681 20 68 61 6e 64 6c 65 64 20 73 69 6d 69 6c 61 72   handled similar
2f682 6c 79 2e 20 54 68 65 20 6f 6e 6c 79 20 64 69 66  ly. The only dif
2f683 66 65 72 65 6e 63 65 20 0a 2a 2a 20 69 73 20 74  ference .** is t
2f684 68 61 74 20 74 68 65 20 63 6f 75 6e 74 65 72 20  hat the counter 
2f685 75 73 65 64 20 69 73 20 73 74 6f 72 65 64 20 61  used is stored a
2f686 73 20 70 61 72 74 20 6f 66 20 65 61 63 68 20 69  s part of each i
2f687 6e 64 69 76 69 64 75 61 6c 20 73 74 61 74 65 6d  ndividual statem
2f688 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 28 73  ent.** object (s
2f689 74 72 75 63 74 20 56 64 62 65 29 2e 20 49 66 2c  truct Vdbe). If,
2f68a 20 61 66 74 65 72 20 74 68 65 20 73 74 61 74 65   after the state
2f68b 6d 65 6e 74 20 68 61 73 20 72 75 6e 2c 20 69 74  ment has run, it
2f68c 73 20 69 6d 6d 65 64 69 61 74 65 0a 2a 2a 20 63  s immediate.** c
2f68d 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65  onstraint counte
2f68e 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  r is greater tha
2f68f 6e 20 7a 65 72 6f 2c 20 69 74 20 72 65 74 75 72  n zero, it retur
2f690 6e 73 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  ns SQLITE_CONSTR
2f691 41 49 4e 54 0a 2a 2a 20 61 6e 64 20 74 68 65 20  AINT.** and the 
2f692 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
2f693 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20  ction is rolled 
2f694 62 61 63 6b 2e 20 41 6e 20 65 78 63 65 70 74 69  back. An excepti
2f695 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 52 54 0a  on is an INSERT.
2f696 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  ** statement tha
2f697 74 20 69 6e 73 65 72 74 73 20 61 20 73 69 6e 67  t inserts a sing
2f698 6c 65 20 72 6f 77 20 6f 6e 6c 79 20 28 6e 6f 20  le row only (no 
2f699 74 72 69 67 67 65 72 73 29 2e 20 49 6e 20 74 68  triggers). In th
2f69a 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69 6e 73 74  is case,.** inst
2f69b 65 61 64 20 6f 66 20 75 73 69 6e 67 20 61 20 63  ead of using a c
2f69c 6f 75 6e 74 65 72 2c 20 61 6e 20 65 78 63 65 70  ounter, an excep
2f69d 74 69 6f 6e 20 69 73 20 74 68 72 6f 77 6e 20 69  tion is thrown i
2f69e 6d 6d 65 64 69 61 74 65 6c 79 20 69 66 20 74 68  mmediately if th
2f69f 65 0a 2a 2a 20 49 4e 53 45 52 54 20 76 69 6f 6c  e.** INSERT viol
2f6a0 61 74 65 73 20 61 20 66 6f 72 65 69 67 6e 20 6b  ates a foreign k
2f6a1 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54  ey constraint. T
2f6a2 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79  his is necessary
2f6a3 20 61 73 20 73 75 63 68 0a 2a 2a 20 61 6e 20 49   as such.** an I
2f6a4 4e 53 45 52 54 20 64 6f 65 73 20 6e 6f 74 20 6f  NSERT does not o
2f6a5 70 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20  pen a statement 
2f6a6 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a  transaction..**.
2f6a7 2a 2a 20 54 4f 44 4f 3a 20 48 6f 77 20 73 68 6f  ** TODO: How sho
2f6a8 75 6c 64 20 64 72 6f 70 70 69 6e 67 20 61 20 74  uld dropping a t
2f6a9 61 62 6c 65 20 62 65 20 68 61 6e 64 6c 65 64 3f  able be handled?
2f6aa 20 48 6f 77 20 73 68 6f 75 6c 64 20 72 65 6e 61   How should rena
2f6ab 6d 69 6e 67 20 61 20 0a 2a 2a 20 74 61 62 6c 65  ming a .** table
2f6ac 20 62 65 20 68 61 6e 64 6c 65 64 3f 0a 2a 2a 0a   be handled?.**.
2f6ad 2a 2a 0a 2a 2a 20 51 75 65 72 79 20 41 50 49 20  **.** Query API 
2f6ae 4e 6f 74 65 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d  Notes.** -------
2f6af 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 42  --------.**.** B
2f6b0 65 66 6f 72 65 20 63 6f 64 69 6e 67 20 61 6e 20  efore coding an 
2f6b1 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45  UPDATE or DELETE
2f6b2 20 72 6f 77 20 6f 70 65 72 61 74 69 6f 6e 2c 20   row operation, 
2f6b3 74 68 65 20 63 6f 64 65 2d 67 65 6e 65 72 61 74  the code-generat
2f6b4 6f 72 0a 2a 2a 20 66 6f 72 20 74 68 6f 73 65 20  or.** for those 
2f6b5 74 77 6f 20 6f 70 65 72 61 74 69 6f 6e 73 20 6e  two operations n
2f6b6 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65  eeds to know whe
2f6b7 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
2f6b8 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 72 65 71  operation.** req
2f6b9 75 69 72 65 73 20 61 6e 79 20 46 4b 20 70 72 6f  uires any FK pro
2f6ba 63 65 73 73 69 6e 67 20 61 6e 64 2c 20 69 66 20  cessing and, if 
2f6bb 73 6f 2c 20 77 68 69 63 68 20 63 6f 6c 75 6d 6e  so, which column
2f6bc 73 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61  s of the origina
2f6bd 6c 0a 2a 2a 20 72 6f 77 20 61 72 65 20 72 65 71  l.** row are req
2f6be 75 69 72 65 64 20 62 79 20 74 68 65 20 46 4b 20  uired by the FK 
2f6bf 70 72 6f 63 65 73 73 69 6e 67 20 56 44 42 45 20  processing VDBE 
2f6c0 63 6f 64 65 20 28 69 2e 65 2e 20 69 66 20 46 4b  code (i.e. if FK
2f6c1 73 20 77 65 72 65 0a 2a 2a 20 69 6d 70 6c 65 6d  s were.** implem
2f6c2 65 6e 74 65 64 20 75 73 69 6e 67 20 74 72 69 67  ented using trig
2f6c3 67 65 72 73 2c 20 77 68 69 63 68 20 6f 66 20 74  gers, which of t
2f6c4 68 65 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73  he old.* columns
2f6c5 20 77 6f 75 6c 64 20 62 65 20 0a 2a 2a 20 61 63   would be .** ac
2f6c6 63 65 73 73 65 64 29 2e 20 4e 6f 20 69 6e 66 6f  cessed). No info
2f6c7 72 6d 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69  rmation is requi
2f6c8 72 65 64 20 62 79 20 74 68 65 20 63 6f 64 65 2d  red by the code-
2f6c9 67 65 6e 65 72 61 74 6f 72 20 62 65 66 6f 72 65  generator before
2f6ca 0a 2a 2a 20 63 6f 64 69 6e 67 20 61 6e 20 49 4e  .** coding an IN
2f6cb 53 45 52 54 20 6f 70 65 72 61 74 69 6f 6e 2e 20  SERT operation. 
2f6cc 54 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 75 73  The functions us
2f6cd 65 64 20 62 79 20 74 68 65 20 55 50 44 41 54 45  ed by the UPDATE
2f6ce 2f 44 45 4c 45 54 45 0a 2a 2a 20 67 65 6e 65 72  /DELETE.** gener
2f6cf 61 74 69 6f 6e 20 63 6f 64 65 20 74 6f 20 71 75  ation code to qu
2f6d0 65 72 79 20 66 6f 72 20 74 68 69 73 20 69 6e 66  ery for this inf
2f6d1 6f 72 6d 61 74 69 6f 6e 20 61 72 65 3a 0a 2a 2a  ormation are:.**
2f6d2 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46 6b 52  .**   sqlite3FkR
2f6d3 65 71 75 69 72 65 64 28 29 20 2d 20 54 65 73 74  equired() - Test
2f6d4 20 74 6f 20 73 65 65 20 69 66 20 46 4b 20 70 72   to see if FK pr
2f6d5 6f 63 65 73 73 69 6e 67 20 69 73 20 72 65 71 75  ocessing is requ
2f6d6 69 72 65 64 2e 0a 2a 2a 20 20 20 73 71 6c 69 74  ired..**   sqlit
2f6d7 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 29 20 20 2d  e3FkOldmask()  -
2f6d8 20 51 75 65 72 79 20 66 6f 72 20 74 68 65 20 73   Query for the s
2f6d9 65 74 20 6f 66 20 72 65 71 75 69 72 65 64 20 6f  et of required o
2f6da 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a  ld.* columns..**
2f6db 0a 2a 2a 0a 2a 2a 20 45 78 74 65 72 6e 61 6c 6c  .**.** Externall
2f6dc 79 20 61 63 63 65 73 73 69 62 6c 65 20 6d 6f 64  y accessible mod
2f6dd 75 6c 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a  ule functions.**
2f6de 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
2f6df 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2f6e0 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 20  -------.**.**   
2f6e1 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28 29  sqlite3FkCheck()
2f6e2 20 20 20 20 2d 20 43 68 65 63 6b 20 66 6f 72 20      - Check for 
2f6e3 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c  foreign key viol
2f6e4 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 73 71 6c  ations..**   sql
2f6e5 69 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 29 20  ite3FkActions() 
2f6e6 20 2d 20 43 6f 64 65 20 74 72 69 67 67 65 72 73   - Code triggers
2f6e7 20 66 6f 72 20 4f 4e 20 55 50 44 41 54 45 2f 4f   for ON UPDATE/O
2f6e8 4e 20 44 45 4c 45 54 45 20 61 63 74 69 6f 6e 73  N DELETE actions
2f6e9 2e 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46 6b  ..**   sqlite3Fk
2f6ea 44 65 6c 65 74 65 28 29 20 20 20 2d 20 44 65 6c  Delete()   - Del
2f6eb 65 74 65 20 61 6e 20 46 4b 65 79 20 73 74 72 75  ete an FKey stru
2f6ec 63 74 75 72 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  cture..*/../*.**
2f6ed 20 56 44 42 45 20 43 61 6c 6c 69 6e 67 20 43 6f   VDBE Calling Co
2f6ee 6e 76 65 6e 74 69 6f 6e 0a 2a 2a 20 2d 2d 2d 2d  nvention.** ----
2f6ef 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2f6f0 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c  ---.**.** Exampl
2f6f1 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 46 6f 72 20 74  e:.**.**   For t
2f6f2 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 49 4e 53  he following INS
2f6f3 45 52 54 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a  ERT statement:.*
2f6f4 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20  *.**     CREATE 
2f6f5 54 41 42 4c 45 20 74 31 28 61 2c 20 62 20 49 4e  TABLE t1(a, b IN
2f6f6 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
2f6f7 59 2c 20 63 29 3b 0a 2a 2a 20 20 20 20 20 49 4e  Y, c);.**     IN
2f6f8 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
2f6f9 55 45 53 28 31 2c 20 32 2c 20 33 2e 31 29 3b 0a  UES(1, 2, 3.1);.
2f6fa 2a 2a 0a 2a 2a 20 20 20 52 65 67 69 73 74 65 72  **.**   Register
2f6fb 20 28 78 29 3a 20 20 20 20 20 20 20 20 32 20 20   (x):        2  
2f6fc 20 20 28 74 79 70 65 20 69 6e 74 65 67 65 72 29    (type integer)
2f6fd 0a 2a 2a 20 20 20 52 65 67 69 73 74 65 72 20 28  .**   Register (
2f6fe 78 2b 31 29 3a 20 20 20 20 20 20 31 20 20 20 20  x+1):      1    
2f6ff 28 74 79 70 65 20 69 6e 74 65 67 65 72 29 0a 2a  (type integer).*
2f700 2a 20 20 20 52 65 67 69 73 74 65 72 20 28 78 2b  *   Register (x+
2f701 32 29 3a 20 20 20 20 20 20 4e 55 4c 4c 20 28 74  2):      NULL (t
2f702 79 70 65 20 4e 55 4c 4c 29 0a 2a 2a 20 20 20 52  ype NULL).**   R
2f703 65 67 69 73 74 65 72 20 28 78 2b 33 29 3a 20 20  egister (x+3):  
2f704 20 20 20 20 33 2e 31 20 20 28 74 79 70 65 20 72      3.1  (type r
2f705 65 61 6c 29 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  eal).*/../*.** A
2f706 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
2f707 73 74 72 61 69 6e 74 20 72 65 71 75 69 72 65 73  straint requires
2f708 20 74 68 61 74 20 74 68 65 20 6b 65 79 20 63 6f   that the key co
2f709 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 70 61 72  lumns in the par
2f70a 65 6e 74 0a 2a 2a 20 74 61 62 6c 65 20 61 72 65  ent.** table are
2f70b 20 63 6f 6c 6c 65 63 74 69 76 65 6c 79 20 73 75   collectively su
2f70c 62 6a 65 63 74 20 74 6f 20 61 20 55 4e 49 51 55  bject to a UNIQU
2f70d 45 20 6f 72 20 50 52 49 4d 41 52 59 20 4b 45 59  E or PRIMARY KEY
2f70e 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2a 20   constraint..** 
2f70f 47 69 76 65 6e 20 74 68 61 74 20 70 50 61 72 65  Given that pPare
2f710 6e 74 20 69 73 20 74 68 65 20 70 61 72 65 6e 74  nt is the parent
2f711 20 74 61 62 6c 65 20 66 6f 72 20 66 6f 72 65 69   table for forei
2f712 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
2f713 74 20 70 46 4b 65 79 2c 20 0a 2a 2a 20 73 65 61  t pFKey, .** sea
2f714 72 63 68 20 74 68 65 20 73 63 68 65 6d 61 20 61  rch the schema a
2f715 20 75 6e 69 71 75 65 20 69 6e 64 65 78 20 6f 6e   unique index on
2f716 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20   the parent key 
2f717 63 6f 6c 75 6d 6e 73 2e 20 0a 2a 2a 0a 2a 2a 20  columns. .**.** 
2f718 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 7a  If successful, z
2f719 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ero is returned.
2f71a 20 49 66 20 74 68 65 20 70 61 72 65 6e 74 20 6b   If the parent k
2f71b 65 79 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52  ey is an INTEGER
2f71c 20 50 52 49 4d 41 52 59 20 0a 2a 2a 20 4b 45 59   PRIMARY .** KEY
2f71d 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 6f 75   column, then ou
2f71e 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 2a 70  tput variable *p
2f71f 70 49 64 78 20 69 73 20 73 65 74 20 74 6f 20 4e  pIdx is set to N
2f720 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ULL. Otherwise, 
2f721 2a 70 70 49 64 78 20 0a 2a 2a 20 69 73 20 73 65  *ppIdx .** is se
2f722 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  t to point to th
2f723 65 20 75 6e 69 71 75 65 20 69 6e 64 65 78 2e 20  e unique index. 
2f724 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 70  .** .** If the p
2f725 61 72 65 6e 74 20 6b 65 79 20 63 6f 6e 73 69 73  arent key consis
2f726 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 63  ts of a single c
2f727 6f 6c 75 6d 6e 20 28 74 68 65 20 66 6f 72 65 69  olumn (the forei
2f728 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
2f729 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 20 63 6f  t.** is not a co
2f72a 6d 70 6f 73 69 74 65 20 66 6f 72 65 69 67 6e 20  mposite foreign 
2f72b 6b 65 79 29 2c 20 6f 75 74 70 75 74 20 76 61 72  key), output var
2f72c 69 61 62 6c 65 20 2a 70 61 69 43 6f 6c 20 69 73  iable *paiCol is
2f72d 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a   set to NULL..**
2f72e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 69   Otherwise, it i
2f72f 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74  s set to point t
2f730 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61  o an allocated a
2f731 72 72 61 79 20 6f 66 20 73 69 7a 65 20 4e 2c 20  rray of size N, 
2f732 77 68 65 72 65 0a 2a 2a 20 4e 20 69 73 20 74 68  where.** N is th
2f733 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
2f734 6d 6e 73 20 69 6e 20 74 68 65 20 70 61 72 65 6e  mns in the paren
2f735 74 20 6b 65 79 2e 20 54 68 65 20 66 69 72 73 74  t key. The first
2f736 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 0a   element of the.
2f737 2a 2a 20 61 72 72 61 79 20 69 73 20 74 68 65 20  ** array is the 
2f738 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 68 69  index of the chi
2f739 6c 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20  ld table column 
2f73a 74 68 61 74 20 69 73 20 6d 61 70 70 65 64 20 62  that is mapped b
2f73b 79 20 74 68 65 20 46 4b 0a 2a 2a 20 63 6f 6e 73  y the FK.** cons
2f73c 74 72 61 69 6e 74 20 74 6f 20 74 68 65 20 70 61  traint to the pa
2f73d 72 65 6e 74 20 74 61 62 6c 65 20 63 6f 6c 75 6d  rent table colum
2f73e 6e 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  n stored in the 
2f73f 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e  left-most column
2f740 0a 2a 2a 20 6f 66 20 69 6e 64 65 78 20 2a 70 70  .** of index *pp
2f741 49 64 78 2e 20 54 68 65 20 73 65 63 6f 6e 64 20  Idx. The second 
2f742 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61  element of the a
2f743 72 72 61 79 20 69 73 20 74 68 65 20 69 6e 64 65  rray is the inde
2f744 78 20 6f 66 20 74 68 65 0a 2a 2a 20 63 68 69 6c  x of the.** chil
2f745 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74  d table column t
2f746 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  hat corresponds 
2f747 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 65  to the second le
2f748 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f  ft-most column o
2f749 66 0a 2a 2a 20 2a 70 70 49 64 78 2c 20 61 6e 64  f.** *ppIdx, and
2f74a 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66   so on..**.** If
2f74b 20 74 68 65 20 72 65 71 75 69 72 65 64 20 69 6e   the required in
2f74c 64 65 78 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f  dex cannot be fo
2f74d 75 6e 64 2c 20 65 69 74 68 65 72 20 62 65 63 61  und, either beca
2f74e 75 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20  use:.**.**   1) 
2f74f 54 68 65 20 6e 61 6d 65 64 20 70 61 72 65 6e 74  The named parent
2f750 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 64 6f 20   key columns do 
2f751 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 0a 2a 2a  not exist, or.**
2f752 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 6e 61 6d  .**   2) The nam
2f753 65 64 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f  ed parent key co
2f754 6c 75 6d 6e 73 20 64 6f 20 65 78 69 73 74 2c 20  lumns do exist, 
2f755 62 75 74 20 61 72 65 20 6e 6f 74 20 73 75 62 6a  but are not subj
2f756 65 63 74 20 74 6f 20 61 0a 2a 2a 20 20 20 20 20  ect to a.**     
2f757 20 55 4e 49 51 55 45 20 6f 72 20 50 52 49 4d 41   UNIQUE or PRIMA
2f758 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e  RY KEY constrain
2f759 74 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 33 29  t, or.**.**   3)
2f75a 20 4e 6f 20 70 61 72 65 6e 74 20 6b 65 79 20 63   No parent key c
2f75b 6f 6c 75 6d 6e 73 20 77 65 72 65 20 70 72 6f 76  olumns were prov
2f75c 69 64 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20  ided explicitly 
2f75d 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a  as part of the.*
2f75e 2a 20 20 20 20 20 20 66 6f 72 65 69 67 6e 20 6b  *      foreign k
2f75f 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20 61  ey definition, a
2f760 6e 64 20 74 68 65 20 70 61 72 65 6e 74 20 74 61  nd the parent ta
2f761 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20 68 61 76  ble does not hav
2f762 65 20 61 0a 2a 2a 20 20 20 20 20 20 50 52 49 4d  e a.**      PRIM
2f763 41 52 59 20 4b 45 59 2c 20 6f 72 0a 2a 2a 0a 2a  ARY KEY, or.**.*
2f764 2a 20 20 20 34 29 20 4e 6f 20 70 61 72 65 6e 74  *   4) No parent
2f765 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 77 65 72   key columns wer
2f766 65 20 70 72 6f 76 69 64 65 64 20 65 78 70 6c 69  e provided expli
2f767 63 69 74 6c 79 20 61 73 20 70 61 72 74 20 6f 66  citly as part of
2f768 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 66 6f 72   the.**      for
2f769 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74  eign key definit
2f76a 69 6f 6e 2c 20 61 6e 64 20 74 68 65 20 50 52 49  ion, and the PRI
2f76b 4d 41 52 59 20 4b 45 59 20 6f 66 20 74 68 65 20  MARY KEY of the 
2f76c 70 61 72 65 6e 74 20 74 61 62 6c 65 20 0a 2a 2a  parent table .**
2f76d 20 20 20 20 20 20 63 6f 6e 73 69 73 74 73 20 6f        consists o
2f76e 66 20 61 20 61 20 64 69 66 66 65 72 65 6e 74 20  f a a different 
2f76f 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
2f770 73 20 74 6f 20 74 68 65 20 63 68 69 6c 64 20 6b  s to the child k
2f771 65 79 20 69 6e 20 0a 2a 2a 20 20 20 20 20 20 74  ey in .**      t
2f772 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2e 0a  he child table..
2f773 2a 2a 0a 2a 2a 20 74 68 65 6e 20 6e 6f 6e 2d 7a  **.** then non-z
2f774 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2c  ero is returned,
2f775 20 61 6e 64 20 61 20 22 66 6f 72 65 69 67 6e 20   and a "foreign 
2f776 6b 65 79 20 6d 69 73 6d 61 74 63 68 22 20 65 72  key mismatch" er
2f777 72 6f 72 20 6c 6f 61 64 65 64 0a 2a 2a 20 69 6e  ror loaded.** in
2f778 74 6f 20 70 50 61 72 73 65 2e 20 49 66 20 61 6e  to pParse. If an
2f779 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72   OOM error occur
2f77a 73 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 72  s, non-zero is r
2f77b 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 0a  eturned and the.
2f77c 2a 2a 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d  ** pParse->db->m
2f77d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
2f77e 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74   is set..*/.stat
2f77f 69 63 20 69 6e 74 20 6c 6f 63 61 74 65 46 6b 65  ic int locateFke
2f780 79 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20  yIndex(.  Parse 
2f781 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2f782 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
2f783 73 65 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 74  se context to st
2f784 6f 72 65 20 61 6e 79 20 65 72 72 6f 72 20 69 6e  ore any error in
2f785 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 50 61   */.  Table *pPa
2f786 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20  rent,           
2f787 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20        /* Parent 
2f788 74 61 62 6c 65 20 6f 66 20 46 4b 20 63 6f 6e 73  table of FK cons
2f789 74 72 61 69 6e 74 20 70 46 4b 65 79 20 2a 2f 0a  traint pFKey */.
2f78a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20 20    FKey *pFKey,  
2f78b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f78c 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79    /* Foreign key
2f78d 20 74 6f 20 66 69 6e 64 20 69 6e 64 65 78 20 66   to find index f
2f78e 6f 72 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 2a  or */.  Index **
2f78f 70 70 49 64 78 2c 20 20 20 20 20 20 20 20 20 20  ppIdx,          
2f790 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
2f791 55 6e 69 71 75 65 20 69 6e 64 65 78 20 6f 6e 20  Unique index on 
2f792 70 61 72 65 6e 74 20 74 61 62 6c 65 20 2a 2f 0a  parent table */.
2f793 20 20 69 6e 74 20 2a 2a 70 61 69 43 6f 6c 20 20    int **paiCol  
2f794 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f795 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 70 20 6f 66    /* OUT: Map of
2f796 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 20 69   index columns i
2f797 6e 20 70 46 4b 65 79 20 2a 2f 0a 29 7b 0a 20 20  n pFKey */.){.  
2f798 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 30 3b  Index *pIdx = 0;
2f799 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f79a 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
2f79b 72 65 74 75 72 6e 20 76 69 61 20 2a 70 70 49 64  return via *ppId
2f79c 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f  x */.  int *aiCo
2f79d 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  l = 0;          
2f79e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
2f79f 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 76 69  lue to return vi
2f7a0 61 20 2a 70 61 69 43 6f 6c 20 2a 2f 0a 20 20 69  a *paiCol */.  i
2f7a1 6e 74 20 6e 43 6f 6c 20 3d 20 70 46 4b 65 79 2d  nt nCol = pFKey-
2f7a2 3e 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20  >nCol;          
2f7a3 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2f7a4 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 61 72 65 6e  columns in paren
2f7a5 74 20 6b 65 79 20 2a 2f 0a 20 20 63 68 61 72 20  t key */.  char 
2f7a6 2a 7a 4b 65 79 20 3d 20 70 46 4b 65 79 2d 3e 61  *zKey = pFKey->a
2f7a7 43 6f 6c 5b 30 5d 2e 7a 43 6f 6c 3b 20 20 20 2f  Col[0].zCol;   /
2f7a8 2a 20 4e 61 6d 65 20 6f 66 20 6c 65 66 74 2d 6d  * Name of left-m
2f7a9 6f 73 74 20 70 61 72 65 6e 74 20 6b 65 79 20 63  ost parent key c
2f7aa 6f 6c 75 6d 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 54  olumn */..  /* T
2f7ab 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73  he caller is res
2f7ac 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 7a 65 72  ponsible for zer
2f7ad 6f 69 6e 67 20 6f 75 74 70 75 74 20 70 61 72 61  oing output para
2f7ae 6d 65 74 65 72 73 2e 20 2a 2f 0a 20 20 61 73 73  meters. */.  ass
2f7af 65 72 74 28 20 70 70 49 64 78 20 26 26 20 2a 70  ert( ppIdx && *p
2f7b0 70 49 64 78 3d 3d 30 20 29 3b 0a 20 20 61 73 73  pIdx==0 );.  ass
2f7b1 65 72 74 28 20 21 70 61 69 43 6f 6c 20 7c 7c 20  ert( !paiCol || 
2f7b2 2a 70 61 69 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20  *paiCol==0 );.  
2f7b3 61 73 73 65 72 74 28 20 70 50 61 72 73 65 20 29  assert( pParse )
2f7b4 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  ;..  /* If this 
2f7b5 69 73 20 61 20 6e 6f 6e 2d 63 6f 6d 70 6f 73 69  is a non-composi
2f7b6 74 65 20 28 73 69 6e 67 6c 65 20 63 6f 6c 75 6d  te (single colum
2f7b7 6e 29 20 66 6f 72 65 69 67 6e 20 6b 65 79 2c 20  n) foreign key, 
2f7b8 63 68 65 63 6b 20 69 66 20 69 74 20 0a 20 20 2a  check if it .  *
2f7b9 2a 20 6d 61 70 73 20 74 6f 20 74 68 65 20 49 4e  * maps to the IN
2f7ba 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
2f7bb 59 20 6f 66 20 74 61 62 6c 65 20 70 50 61 72 65  Y of table pPare
2f7bc 6e 74 2e 20 49 66 20 73 6f 2c 20 6c 65 61 76 65  nt. If so, leave
2f7bd 20 2a 70 70 49 64 78 20 0a 20 20 2a 2a 20 61 6e   *ppIdx .  ** an
2f7be 64 20 2a 70 61 69 43 6f 6c 20 73 65 74 20 74 6f  d *paiCol set to
2f7bf 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e   zero and return
2f7c0 20 65 61 72 6c 79 2e 20 0a 20 20 2a 2a 0a 20 20   early. .  **.  
2f7c1 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6f  ** Otherwise, fo
2f7c2 72 20 61 20 63 6f 6d 70 6f 73 69 74 65 20 66 6f  r a composite fo
2f7c3 72 65 69 67 6e 20 6b 65 79 20 28 6d 6f 72 65 20  reign key (more 
2f7c4 74 68 61 6e 20 6f 6e 65 20 63 6f 6c 75 6d 6e 29  than one column)
2f7c5 2c 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a 2a 20  , allocate.  ** 
2f7c6 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 61 69  space for the ai
2f7c7 43 6f 6c 20 61 72 72 61 79 20 28 72 65 74 75 72  Col array (retur
2f7c8 6e 65 64 20 76 69 61 20 6f 75 74 70 75 74 20 70  ned via output p
2f7c9 61 72 61 6d 65 74 65 72 20 2a 70 61 69 43 6f 6c  arameter *paiCol
2f7ca 29 2e 0a 20 20 2a 2a 20 4e 6f 6e 2d 63 6f 6d 70  )..  ** Non-comp
2f7cb 6f 73 69 74 65 20 66 6f 72 65 69 67 6e 20 6b 65  osite foreign ke
2f7cc 79 73 20 64 6f 20 6e 6f 74 20 72 65 71 75 69 72  ys do not requir
2f7cd 65 20 74 68 65 20 61 69 43 6f 6c 20 61 72 72 61  e the aiCol arra
2f7ce 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 43  y..  */.  if( nC
2f7cf 6f 6c 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20  ol==1 ){.    /* 
2f7d0 54 68 65 20 46 4b 20 6d 61 70 73 20 74 6f 20 74  The FK maps to t
2f7d1 68 65 20 49 50 4b 20 69 66 20 61 6e 79 20 6f 66  he IPK if any of
2f7d2 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   the following a
2f7d3 72 65 20 74 72 75 65 3a 0a 20 20 20 20 2a 2a 0a  re true:.    **.
2f7d4 20 20 20 20 2a 2a 20 20 20 31 29 20 54 68 65 72      **   1) Ther
2f7d5 65 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20  e is an INTEGER 
2f7d6 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75  PRIMARY KEY colu
2f7d7 6d 6e 20 61 6e 64 20 74 68 65 20 46 4b 20 69 73  mn and the FK is
2f7d8 20 69 6d 70 6c 69 63 69 74 6c 79 20 0a 20 20 20   implicitly .   
2f7d9 20 2a 2a 20 20 20 20 20 20 6d 61 70 70 65 64 20   **      mapped 
2f7da 74 6f 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  to the primary k
2f7db 65 79 20 6f 66 20 74 61 62 6c 65 20 70 50 61 72  ey of table pPar
2f7dc 65 6e 74 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 20  ent, or.    **  
2f7dd 20 32 29 20 54 68 65 20 46 4b 20 69 73 20 65 78   2) The FK is ex
2f7de 70 6c 69 63 69 74 6c 79 20 6d 61 70 70 65 64 20  plicitly mapped 
2f7df 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 6c  to a column decl
2f7e0 61 72 65 64 20 61 73 20 49 4e 54 45 47 45 52 0a  ared as INTEGER.
2f7e1 20 20 20 20 2a 2a 20 20 20 20 20 20 50 52 49 4d      **      PRIM
2f7e2 41 52 59 20 4b 45 59 2e 0a 20 20 20 20 2a 2f 0a  ARY KEY..    */.
2f7e3 20 20 20 20 69 66 28 20 70 50 61 72 65 6e 74 2d      if( pParent-
2f7e4 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 20  >iPKey>=0 ){.   
2f7e5 20 20 20 69 66 28 20 21 7a 4b 65 79 20 29 20 72     if( !zKey ) r
2f7e6 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 69  eturn 0;.      i
2f7e7 66 28 20 21 73 71 6c 69 74 65 33 53 74 72 49 43  f( !sqlite3StrIC
2f7e8 6d 70 28 70 50 61 72 65 6e 74 2d 3e 61 43 6f 6c  mp(pParent->aCol
2f7e9 5b 70 50 61 72 65 6e 74 2d 3e 69 50 4b 65 79 5d  [pParent->iPKey]
2f7ea 2e 7a 4e 61 6d 65 2c 20 7a 4b 65 79 29 20 29 20  .zName, zKey) ) 
2f7eb 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
2f7ec 20 20 7d 65 6c 73 65 20 69 66 28 20 70 61 69 43    }else if( paiC
2f7ed 6f 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  ol ){.    assert
2f7ee 28 20 6e 43 6f 6c 3e 31 20 29 3b 0a 20 20 20 20  ( nCol>1 );.    
2f7ef 61 69 43 6f 6c 20 3d 20 28 69 6e 74 20 2a 29 73  aiCol = (int *)s
2f7f0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
2f7f1 77 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6e 43  w(pParse->db, nC
2f7f2 6f 6c 2a 73 69 7a 65 6f 66 28 69 6e 74 29 29 3b  ol*sizeof(int));
2f7f3 0a 20 20 20 20 69 66 28 20 21 61 69 43 6f 6c 20  .    if( !aiCol 
2f7f4 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
2f7f5 2a 70 61 69 43 6f 6c 20 3d 20 61 69 43 6f 6c 3b  *paiCol = aiCol;
2f7f6 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70 49 64 78  .  }..  for(pIdx
2f7f7 3d 70 50 61 72 65 6e 74 2d 3e 70 49 6e 64 65 78  =pParent->pIndex
2f7f8 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
2f7f9 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  x->pNext){.    i
2f7fa 66 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  f( pIdx->nColumn
2f7fb 3d 3d 6e 43 6f 6c 20 26 26 20 70 49 64 78 2d 3e  ==nCol && pIdx->
2f7fc 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
2f7fd 20 29 7b 20 0a 20 20 20 20 20 20 2f 2a 20 70 49   ){ .      /* pI
2f7fe 64 78 20 69 73 20 61 20 55 4e 49 51 55 45 20 69  dx is a UNIQUE i
2f7ff 6e 64 65 78 20 28 6f 72 20 61 20 50 52 49 4d 41  ndex (or a PRIMA
2f800 52 59 20 4b 45 59 29 20 61 6e 64 20 68 61 73 20  RY KEY) and has 
2f801 74 68 65 20 72 69 67 68 74 20 6e 75 6d 62 65 72  the right number
2f802 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 63 6f 6c  .      ** of col
2f803 75 6d 6e 73 2e 20 49 66 20 65 61 63 68 20 69 6e  umns. If each in
2f804 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 63 6f 72  dexed column cor
2f805 72 65 73 70 6f 6e 64 73 20 74 6f 20 61 20 66 6f  responds to a fo
2f806 72 65 69 67 6e 20 6b 65 79 0a 20 20 20 20 20 20  reign key.      
2f807 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20 70 46 4b  ** column of pFK
2f808 65 79 2c 20 74 68 65 6e 20 74 68 69 73 20 69 6e  ey, then this in
2f809 64 65 78 20 69 73 20 61 20 77 69 6e 6e 65 72 2e  dex is a winner.
2f80a 20 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28 20    */..      if( 
2f80b 7a 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  zKey==0 ){.     
2f80c 20 20 20 2f 2a 20 49 66 20 7a 4b 65 79 20 69 73     /* If zKey is
2f80d 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73   NULL, then this
2f80e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20   foreign key is 
2f80f 69 6d 70 6c 69 63 69 74 6c 79 20 6d 61 70 70 65  implicitly mappe
2f810 64 20 74 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a  d to .        **
2f811 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
2f812 20 6f 66 20 74 61 62 6c 65 20 70 50 61 72 65 6e   of table pParen
2f813 74 2e 20 54 68 65 20 50 52 49 4d 41 52 59 20 4b  t. The PRIMARY K
2f814 45 59 20 69 6e 64 65 78 20 6d 61 79 20 62 65 20  EY index may be 
2f815 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 64 65 6e  .        ** iden
2f816 74 69 66 69 65 64 20 62 79 20 74 68 65 20 74 65  tified by the te
2f817 73 74 20 28 49 6e 64 65 78 2e 61 75 74 6f 49 6e  st (Index.autoIn
2f818 64 65 78 3d 3d 32 29 2e 20 20 2a 2f 0a 20 20 20  dex==2).  */.   
2f819 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61       if( pIdx->a
2f81a 75 74 6f 49 6e 64 65 78 3d 3d 32 20 29 7b 0a 20  utoIndex==2 ){. 
2f81b 20 20 20 20 20 20 20 20 20 69 66 28 20 61 69 43           if( aiC
2f81c 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ol ){.          
2f81d 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20    int i;.       
2f81e 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
2f81f 6e 43 6f 6c 3b 20 69 2b 2b 29 20 61 69 43 6f 6c  nCol; i++) aiCol
2f820 5b 69 5d 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f  [i] = pFKey->aCo
2f821 6c 5b 69 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20 20  l[i].iFrom;.    
2f822 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2f823 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
2f824 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
2f825 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 7a 4b          /* If zK
2f826 65 79 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20  ey is non-NULL, 
2f827 74 68 65 6e 20 74 68 69 73 20 66 6f 72 65 69 67  then this foreig
2f828 6e 20 6b 65 79 20 77 61 73 20 64 65 63 6c 61 72  n key was declar
2f829 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a  ed to.        **
2f82a 20 6d 61 70 20 74 6f 20 61 6e 20 65 78 70 6c 69   map to an expli
2f82b 63 69 74 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75  cit list of colu
2f82c 6d 6e 73 20 69 6e 20 74 61 62 6c 65 20 70 50 61  mns in table pPa
2f82d 72 65 6e 74 2e 20 43 68 65 63 6b 20 69 66 20 74  rent. Check if t
2f82e 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  his.        ** i
2f82f 6e 64 65 78 20 6d 61 74 63 68 65 73 20 74 68 6f  ndex matches tho
2f830 73 65 20 63 6f 6c 75 6d 6e 73 2e 20 41 6c 73 6f  se columns. Also
2f831 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65  , check that the
2f832 20 69 6e 64 65 78 20 75 73 65 73 0a 20 20 20 20   index uses.    
2f833 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75      ** the defau
2f834 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  lt collation seq
2f835 75 65 6e 63 65 73 20 66 6f 72 20 65 61 63 68 20  uences for each 
2f836 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 20 20 20  column. */.     
2f837 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20     int i, j;.   
2f838 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
2f839 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
2f83a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d        int iCol =
2f83b 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
2f83c 69 5d 3b 20 20 20 20 20 2f 2a 20 49 6e 64 65 78  i];     /* Index
2f83d 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 61   of column in pa
2f83e 72 65 6e 74 20 74 62 6c 20 2a 2f 0a 20 20 20 20  rent tbl */.    
2f83f 20 20 20 20 20 20 63 68 61 72 20 2a 7a 44 66 6c        char *zDfl
2f840 74 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  tColl;          
2f841 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 2e 20          /* Def. 
2f842 63 6f 6c 6c 61 74 69 6f 6e 20 66 6f 72 20 63 6f  collation for co
2f843 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  lumn */.        
2f844 20 20 63 68 61 72 20 2a 7a 49 64 78 43 6f 6c 3b    char *zIdxCol;
2f845 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f846 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 69      /* Name of i
2f847 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f  ndexed column */
2f848 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ..          /* I
2f849 66 20 74 68 65 20 69 6e 64 65 78 20 75 73 65 73  f the index uses
2f84a 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71   a collation seq
2f84b 75 65 6e 63 65 20 74 68 61 74 20 69 73 20 64 69  uence that is di
2f84c 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 20 20 20  fferent from.   
2f84d 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 65         ** the de
2f84e 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20  fault collation 
2f84f 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65  sequence for the
2f850 20 63 6f 6c 75 6d 6e 2c 20 74 68 69 73 20 69 6e   column, this in
2f851 64 65 78 20 69 73 0a 20 20 20 20 20 20 20 20 20  dex is.         
2f852 20 2a 2a 20 75 6e 75 73 61 62 6c 65 2e 20 42 61   ** unusable. Ba
2f853 69 6c 20 6f 75 74 20 65 61 72 6c 79 20 69 6e 20  il out early in 
2f854 74 68 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a 20  this case.  */. 
2f855 20 20 20 20 20 20 20 20 20 7a 44 66 6c 74 43 6f           zDfltCo
2f856 6c 6c 20 3d 20 70 50 61 72 65 6e 74 2d 3e 61 43  ll = pParent->aC
2f857 6f 6c 5b 69 43 6f 6c 5d 2e 7a 43 6f 6c 6c 3b 0a  ol[iCol].zColl;.
2f858 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 7a            if( !z
2f859 44 66 6c 74 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  DfltColl ){.    
2f85a 20 20 20 20 20 20 20 20 7a 44 66 6c 74 43 6f 6c          zDfltCol
2f85b 6c 20 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20 20  l = "BINARY";.  
2f85c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2f85d 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
2f85e 74 72 49 43 6d 70 28 70 49 64 78 2d 3e 61 7a 43  trICmp(pIdx->azC
2f85f 6f 6c 6c 5b 69 5d 2c 20 7a 44 66 6c 74 43 6f 6c  oll[i], zDfltCol
2f860 6c 29 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20  l) ) break;..   
2f861 20 20 20 20 20 20 20 7a 49 64 78 43 6f 6c 20 3d         zIdxCol =
2f862 20 70 50 61 72 65 6e 74 2d 3e 61 43 6f 6c 5b 69   pParent->aCol[i
2f863 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  Col].zName;.    
2f864 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
2f865 3c 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20  <nCol; j++){.   
2f866 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
2f867 69 74 65 33 53 74 72 49 43 6d 70 28 70 46 4b 65  ite3StrICmp(pFKe
2f868 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 2c  y->aCol[j].zCol,
2f869 20 7a 49 64 78 43 6f 6c 29 3d 3d 30 20 29 7b 0a   zIdxCol)==0 ){.
2f86a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
2f86b 28 20 61 69 43 6f 6c 20 29 20 61 69 43 6f 6c 5b  ( aiCol ) aiCol[
2f86c 69 5d 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f 6c  i] = pFKey->aCol
2f86d 5b 6a 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20 20 20  [j].iFrom;.     
2f86e 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f86f 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2f870 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2f871 20 20 20 20 69 66 28 20 6a 3d 3d 6e 43 6f 6c 20      if( j==nCol 
2f872 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ) break;.       
2f873 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69   }.        if( i
2f874 3d 3d 6e 43 6f 6c 20 29 20 62 72 65 61 6b 3b 20  ==nCol ) break; 
2f875 20 20 20 20 20 2f 2a 20 70 49 64 78 20 69 73 20       /* pIdx is 
2f876 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20  usable */.      
2f877 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69  }.    }.  }..  i
2f878 66 28 20 21 70 49 64 78 20 29 7b 0a 20 20 20 20  f( !pIdx ){.    
2f879 69 66 28 20 21 70 50 61 72 73 65 2d 3e 64 69 73  if( !pParse->dis
2f87a 61 62 6c 65 54 72 69 67 67 65 72 73 20 29 7b 0a  ableTriggers ){.
2f87b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2f87c 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 66  orMsg(pParse, "f
2f87d 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61  oreign key misma
2f87e 74 63 68 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20  tch");.    }.   
2f87f 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
2f880 50 61 72 73 65 2d 3e 64 62 2c 20 61 69 43 6f 6c  Parse->db, aiCol
2f881 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
2f882 0a 20 20 7d 0a 0a 20 20 2a 70 70 49 64 78 20 3d  .  }..  *ppIdx =
2f883 20 70 49 64 78 3b 0a 20 20 72 65 74 75 72 6e 20   pIdx;.  return 
2f884 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  0;.}../*.** This
2f885 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
2f886 6c 65 64 20 77 68 65 6e 20 61 20 72 6f 77 20 69  led when a row i
2f887 73 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  s inserted into 
2f888 6f 72 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20  or deleted from 
2f889 74 68 65 20 0a 2a 2a 20 63 68 69 6c 64 20 74 61  the .** child ta
2f88a 62 6c 65 20 6f 66 20 66 6f 72 65 69 67 6e 20 6b  ble of foreign k
2f88b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 46  ey constraint pF
2f88c 4b 65 79 2e 20 49 66 20 61 6e 20 53 51 4c 20 55  Key. If an SQL U
2f88d 50 44 41 54 45 20 69 73 20 65 78 65 63 75 74 65  PDATE is execute
2f88e 64 20 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 68 69  d .** on the chi
2f88f 6c 64 20 74 61 62 6c 65 20 6f 66 20 70 46 4b 65  ld table of pFKe
2f890 79 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  y, this function
2f891 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 77 69 63   is invoked twic
2f892 65 20 66 6f 72 20 65 61 63 68 20 72 6f 77 0a 2a  e for each row.*
2f893 2a 20 61 66 66 65 63 74 65 64 20 2d 20 6f 6e 63  * affected - onc
2f894 65 20 74 6f 20 22 64 65 6c 65 74 65 22 20 74 68  e to "delete" th
2f895 65 20 6f 6c 64 20 72 6f 77 2c 20 61 6e 64 20 74  e old row, and t
2f896 68 65 6e 20 61 67 61 69 6e 20 74 6f 20 22 69 6e  hen again to "in
2f897 73 65 72 74 22 20 74 68 65 0a 2a 2a 20 6e 65 77  sert" the.** new
2f898 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68   row..**.** Each
2f899 20 74 69 6d 65 20 69 74 20 69 73 20 63 61 6c 6c   time it is call
2f89a 65 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ed, this functio
2f89b 6e 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45  n generates VDBE
2f89c 20 63 6f 64 65 20 74 6f 20 6c 6f 63 61 74 65 20   code to locate 
2f89d 74 68 65 0a 2a 2a 20 72 6f 77 20 69 6e 20 74 68  the.** row in th
2f89e 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 74  e parent table t
2f89f 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  hat corresponds 
2f8a0 74 6f 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67  to the row being
2f8a1 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 0a   inserted into .
2f8a2 2a 2a 20 6f 72 20 64 65 6c 65 74 65 64 20 66 72  ** or deleted fr
2f8a3 6f 6d 20 74 68 65 20 63 68 69 6c 64 20 74 61 62  om the child tab
2f8a4 6c 65 2e 20 49 66 20 74 68 65 20 70 61 72 65 6e  le. If the paren
2f8a5 74 20 72 6f 77 20 63 61 6e 20 62 65 20 66 6f 75  t row can be fou
2f8a6 6e 64 2c 20 6e 6f 20 0a 2a 2a 20 73 70 65 63 69  nd, no .** speci
2f8a7 61 6c 20 61 63 74 69 6f 6e 20 69 73 20 74 61 6b  al action is tak
2f8a8 65 6e 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  en. Otherwise, i
2f8a9 66 20 74 68 65 20 70 61 72 65 6e 74 20 72 6f 77  f the parent row
2f8aa 20 63 61 6e 20 2a 6e 6f 74 2a 20 62 65 0a 2a 2a   can *not* be.**
2f8ab 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 70 61   found in the pa
2f8ac 72 65 6e 74 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a  rent table:.**.*
2f8ad 2a 20 20 20 4f 70 65 72 61 74 69 6f 6e 20 7c 20  *   Operation | 
2f8ae 46 4b 20 74 79 70 65 20 20 20 7c 20 41 63 74 69  FK type   | Acti
2f8af 6f 6e 20 74 61 6b 65 6e 0a 2a 2a 20 20 20 2d 2d  on taken.**   --
2f8b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2f8b1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2f8b2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2f8b3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2f8b4 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 49 4e  --------.**   IN
2f8b5 53 45 52 54 20 20 20 20 20 20 69 6d 6d 65 64 69  SERT      immedi
2f8b6 61 74 65 20 20 20 49 6e 63 72 65 6d 65 6e 74 20  ate   Increment 
2f8b7 74 68 65 20 22 69 6d 6d 65 64 69 61 74 65 20 63  the "immediate c
2f8b8 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65  onstraint counte
2f8b9 72 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 45 4c 45  r"..**.**   DELE
2f8ba 54 45 20 20 20 20 20 20 69 6d 6d 65 64 69 61 74  TE      immediat
2f8bb 65 20 20 20 44 65 63 72 65 6d 65 6e 74 20 74 68  e   Decrement th
2f8bc 65 20 22 69 6d 6d 65 64 69 61 74 65 20 63 6f 6e  e "immediate con
2f8bd 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22  straint counter"
2f8be 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 53 45 52 54  ..**.**   INSERT
2f8bf 20 20 20 20 20 20 64 65 66 65 72 72 65 64 20 20        deferred  
2f8c0 20 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20    Increment the 
2f8c1 22 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72  "deferred constr
2f8c2 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a  aint counter"..*
2f8c3 2a 0a 2a 2a 20 20 20 44 45 4c 45 54 45 20 20 20  *.**   DELETE   
2f8c4 20 20 20 64 65 66 65 72 72 65 64 20 20 20 20 44     deferred    D
2f8c5 65 63 72 65 6d 65 6e 74 20 74 68 65 20 22 64 65  ecrement the "de
2f8c6 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  ferred constrain
2f8c7 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a 2a  t counter"..**.*
2f8c8 2a 20 54 68 65 73 65 20 6f 70 65 72 61 74 69 6f  * These operatio
2f8c9 6e 73 20 61 72 65 20 69 64 65 6e 74 69 66 69 65  ns are identifie
2f8ca 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74  d in the comment
2f8cb 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74   at the top of t
2f8cc 68 69 73 20 66 69 6c 65 20 0a 2a 2a 20 28 66 6b  his file .** (fk
2f8cd 65 79 2e 63 29 20 61 73 20 22 49 2e 31 22 20 61  ey.c) as "I.1" a
2f8ce 6e 64 20 22 44 2e 31 22 2e 0a 2a 2f 0a 73 74 61  nd "D.1"..*/.sta
2f8cf 74 69 63 20 76 6f 69 64 20 66 6b 4c 6f 6f 6b 75  tic void fkLooku
2f8d0 70 50 61 72 65 6e 74 28 0a 20 20 50 61 72 73 65  pParent(.  Parse
2f8d1 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2f8d2 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
2f8d3 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20  t */.  int iDb, 
2f8d4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f8d5 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61 73  Index of databas
2f8d6 65 20 68 6f 75 73 69 6e 67 20 70 54 61 62 20 2a  e housing pTab *
2f8d7 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
2f8d8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
2f8d9 65 6e 74 20 74 61 62 6c 65 20 6f 66 20 46 4b 20  ent table of FK 
2f8da 70 46 4b 65 79 20 2a 2f 0a 20 20 49 6e 64 65 78  pFKey */.  Index
2f8db 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20 20 20   *pIdx,         
2f8dc 20 2f 2a 20 55 6e 69 71 75 65 20 69 6e 64 65 78   /* Unique index
2f8dd 20 6f 6e 20 70 61 72 65 6e 74 20 6b 65 79 20 63   on parent key c
2f8de 6f 6c 75 6d 6e 73 20 69 6e 20 70 54 61 62 20 2a  olumns in pTab *
2f8df 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c  /.  FKey *pFKey,
2f8e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
2f8e1 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
2f8e2 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69  int */.  int *ai
2f8e3 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Col,           /
2f8e4 2a 20 4d 61 70 20 66 72 6f 6d 20 70 61 72 65 6e  * Map from paren
2f8e5 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 74 6f  t key columns to
2f8e6 20 63 68 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c   child table col
2f8e7 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65  umns */.  int re
2f8e8 67 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 20  gData,          
2f8e9 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 72  /* Address of ar
2f8ea 72 61 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 63  ray containing c
2f8eb 68 69 6c 64 20 74 61 62 6c 65 20 72 6f 77 20 2a  hild table row *
2f8ec 2f 0a 20 20 69 6e 74 20 6e 49 6e 63 72 2c 20 20  /.  int nIncr,  
2f8ed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63            /* Inc
2f8ee 72 65 6d 65 6e 74 20 63 6f 6e 73 74 72 61 69 6e  rement constrain
2f8ef 74 20 63 6f 75 6e 74 65 72 20 62 79 20 74 68 69  t counter by thi
2f8f0 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 67 6e  s */.  int isIgn
2f8f1 6f 72 65 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ore          /* 
2f8f2 49 66 20 74 72 75 65 2c 20 70 72 65 74 65 6e 64  If true, pretend
2f8f3 20 70 54 61 62 20 63 6f 6e 74 61 69 6e 73 20 61   pTab contains a
2f8f4 6c 6c 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 2a  ll NULL values *
2f8f5 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  /.){.  int i;   
2f8f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8f8 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
2f8f9 69 61 62 6c 65 20 2a 2f 0a 20 20 56 64 62 65 20  iable */.  Vdbe 
2f8fa 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
2f8fb 64 62 65 28 70 50 61 72 73 65 29 3b 20 20 20 20  dbe(pParse);    
2f8fc 20 20 20 20 20 2f 2a 20 56 64 62 65 20 74 6f 20       /* Vdbe to 
2f8fd 61 64 64 20 63 6f 64 65 20 74 6f 20 2a 2f 0a 20  add code to */. 
2f8fe 20 69 6e 74 20 69 43 75 72 20 3d 20 70 50 61 72   int iCur = pPar
2f8ff 73 65 2d 3e 6e 54 61 62 20 2d 20 31 3b 20 20 20  se->nTab - 1;   
2f900 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
2f901 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 6f 20 75  rsor number to u
2f902 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 6b 20  se */.  int iOk 
2f903 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2f904 65 4c 61 62 65 6c 28 76 29 3b 20 20 20 20 20 20  eLabel(v);      
2f905 20 20 2f 2a 20 6a 75 6d 70 20 68 65 72 65 20 69    /* jump here i
2f906 66 20 70 61 72 65 6e 74 20 6b 65 79 20 66 6f 75  f parent key fou
2f907 6e 64 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 6e  nd */..  /* If n
2f908 49 6e 63 72 20 69 73 20 6c 65 73 73 20 74 68 61  Incr is less tha
2f909 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 63 68 65  n zero, then che
2f90a 63 6b 20 61 74 20 72 75 6e 74 69 6d 65 20 69 66  ck at runtime if
2f90b 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 0a 20   there are any. 
2f90c 20 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   ** outstanding 
2f90d 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 72  constraints to r
2f90e 65 73 6f 6c 76 65 2e 20 49 66 20 74 68 65 72 65  esolve. If there
2f90f 20 61 72 65 20 6e 6f 74 2c 20 74 68 65 72 65 20   are not, there 
2f910 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a 20  is no need.  ** 
2f911 74 6f 20 63 68 65 63 6b 20 69 66 20 64 65 6c 65  to check if dele
2f912 74 69 6e 67 20 74 68 69 73 20 72 6f 77 20 72 65  ting this row re
2f913 73 6f 6c 76 65 73 20 61 6e 79 20 6f 75 74 73 74  solves any outst
2f914 61 6e 64 69 6e 67 20 76 69 6f 6c 61 74 69 6f 6e  anding violation
2f915 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 68 65  s..  **.  ** Che
2f916 63 6b 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65  ck if any of the
2f917 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20   key columns in 
2f918 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 20  the child table 
2f919 72 6f 77 20 61 72 65 20 4e 55 4c 4c 2e 20 49 66  row are NULL. If
2f91a 20 0a 20 20 2a 2a 20 61 6e 79 20 61 72 65 2c 20   .  ** any are, 
2f91b 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61  then the constra
2f91c 69 6e 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65  int is considere
2f91d 64 20 73 61 74 69 73 66 69 65 64 2e 20 4e 6f 20  d satisfied. No 
2f91e 6e 65 65 64 20 74 6f 20 0a 20 20 2a 2a 20 73 65  need to .  ** se
2f91f 61 72 63 68 20 66 6f 72 20 61 20 6d 61 74 63 68  arch for a match
2f920 69 6e 67 20 72 6f 77 20 69 6e 20 74 68 65 20 70  ing row in the p
2f921 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 20 2a 2f  arent table.  */
2f922 0a 20 20 69 66 28 20 6e 49 6e 63 72 3c 30 20 29  .  if( nIncr<0 )
2f923 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
2f924 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 6b  eAddOp2(v, OP_Fk
2f925 49 66 5a 65 72 6f 2c 20 70 46 4b 65 79 2d 3e 69  IfZero, pFKey->i
2f926 73 44 65 66 65 72 72 65 64 2c 20 69 4f 6b 29 3b  sDeferred, iOk);
2f927 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
2f928 69 3c 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69  i<pFKey->nCol; i
2f929 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 69 52 65  ++){.    int iRe
2f92a 67 20 3d 20 61 69 43 6f 6c 5b 69 5d 20 2b 20 72  g = aiCol[i] + r
2f92b 65 67 44 61 74 61 20 2b 20 31 3b 0a 20 20 20 20  egData + 1;.    
2f92c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2f92d 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  2(v, OP_IsNull, 
2f92e 69 52 65 67 2c 20 69 4f 6b 29 3b 0a 20 20 7d 0a  iReg, iOk);.  }.
2f92f 0a 20 20 69 66 28 20 69 73 49 67 6e 6f 72 65 3d  .  if( isIgnore=
2f930 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 49  =0 ){.    if( pI
2f931 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  dx==0 ){.      /
2f932 2a 20 49 66 20 70 49 64 78 20 69 73 20 4e 55 4c  * If pIdx is NUL
2f933 4c 2c 20 74 68 65 6e 20 74 68 65 20 70 61 72 65  L, then the pare
2f934 6e 74 20 6b 65 79 20 69 73 20 74 68 65 20 49 4e  nt key is the IN
2f935 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
2f936 59 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d  Y.      ** colum
2f937 6e 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20  n of the parent 
2f938 74 61 62 6c 65 20 28 74 61 62 6c 65 20 70 54 61  table (table pTa
2f939 62 29 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e  b).  */.      in
2f93a 74 20 69 4d 75 73 74 42 65 49 6e 74 3b 20 20 20  t iMustBeInt;   
2f93b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2f93c 64 64 72 65 73 73 20 6f 66 20 4d 75 73 74 42 65  ddress of MustBe
2f93d 49 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  Int instruction 
2f93e 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67  */.      int reg
2f93f 54 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 47 65  Temp = sqlite3Ge
2f940 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2f941 3b 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 49 6e  ;.  .      /* In
2f942 76 6f 6b 65 20 4d 75 73 74 42 65 49 6e 74 20 74  voke MustBeInt t
2f943 6f 20 63 6f 65 72 63 65 20 74 68 65 20 63 68 69  o coerce the chi
2f944 6c 64 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20  ld key value to 
2f945 61 6e 20 69 6e 74 65 67 65 72 20 28 69 2e 65 2e  an integer (i.e.
2f946 20 0a 20 20 20 20 20 20 2a 2a 20 61 70 70 6c 79   .      ** apply
2f947 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66   the affinity of
2f948 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 29   the parent key)
2f949 2e 20 49 66 20 74 68 69 73 20 66 61 69 6c 73 2c  . If this fails,
2f94a 20 74 68 65 6e 20 74 68 65 72 65 0a 20 20 20 20   then there.    
2f94b 20 20 2a 2a 20 69 73 20 6e 6f 20 6d 61 74 63 68    ** is no match
2f94c 69 6e 67 20 70 61 72 65 6e 74 20 6b 65 79 2e 20  ing parent key. 
2f94d 42 65 66 6f 72 65 20 75 73 69 6e 67 20 4d 75 73  Before using Mus
2f94e 74 42 65 49 6e 74 2c 20 6d 61 6b 65 20 61 20 63  tBeInt, make a c
2f94f 6f 70 79 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20  opy of.      ** 
2f950 74 68 65 20 76 61 6c 75 65 2e 20 4f 74 68 65 72  the value. Other
2f951 77 69 73 65 2c 20 74 68 65 20 76 61 6c 75 65 20  wise, the value 
2f952 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68  inserted into th
2f953 65 20 63 68 69 6c 64 20 6b 65 79 20 63 6f 6c 75  e child key colu
2f954 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c  mn.      ** will
2f955 20 68 61 76 65 20 49 4e 54 45 47 45 52 20 61 66   have INTEGER af
2f956 66 69 6e 69 74 79 20 61 70 70 6c 69 65 64 20 74  finity applied t
2f957 6f 20 69 74 2c 20 77 68 69 63 68 20 6d 61 79 20  o it, which may 
2f958 6e 6f 74 20 62 65 20 63 6f 72 72 65 63 74 2e 20  not be correct. 
2f959 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
2f95a 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2f95b 50 5f 53 43 6f 70 79 2c 20 61 69 43 6f 6c 5b 30  P_SCopy, aiCol[0
2f95c 5d 2b 31 2b 72 65 67 44 61 74 61 2c 20 72 65 67  ]+1+regData, reg
2f95d 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 69 4d 75  Temp);.      iMu
2f95e 73 74 42 65 49 6e 74 20 3d 20 73 71 6c 69 74 65  stBeInt = sqlite
2f95f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2f960 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 65 67  P_MustBeInt, reg
2f961 54 65 6d 70 2c 20 30 29 3b 0a 20 20 0a 20 20 20  Temp, 0);.  .   
2f962 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 72     /* If the par
2f963 65 6e 74 20 74 61 62 6c 65 20 69 73 20 74 68 65  ent table is the
2f964 20 73 61 6d 65 20 61 73 20 74 68 65 20 63 68 69   same as the chi
2f965 6c 64 20 74 61 62 6c 65 2c 20 61 6e 64 20 77 65  ld table, and we
2f966 20 61 72 65 20 61 62 6f 75 74 0a 20 20 20 20 20   are about.     
2f967 20 2a 2a 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74   ** to increment
2f968 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 2d   the constraint-
2f969 63 6f 75 6e 74 65 72 20 28 69 2e 65 2e 20 74 68  counter (i.e. th
2f96a 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20  is is an INSERT 
2f96b 6f 70 65 72 61 74 69 6f 6e 29 2c 0a 20 20 20 20  operation),.    
2f96c 20 20 2a 2a 20 74 68 65 6e 20 63 68 65 63 6b 20    ** then check 
2f96d 69 66 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67  if the row being
2f96e 20 69 6e 73 65 72 74 65 64 20 6d 61 74 63 68 65   inserted matche
2f96f 73 20 69 74 73 65 6c 66 2e 20 49 66 20 73 6f 2c  s itself. If so,
2f970 20 64 6f 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a   do not.      **
2f971 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 63   increment the c
2f972 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65  onstraint-counte
2f973 72 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  r.  */.      if(
2f974 20 70 54 61 62 3d 3d 70 46 4b 65 79 2d 3e 70 46   pTab==pFKey->pF
2f975 72 6f 6d 20 26 26 20 6e 49 6e 63 72 3d 3d 31 20  rom && nIncr==1 
2f976 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2f977 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2f978 4f 50 5f 45 71 2c 20 72 65 67 44 61 74 61 2c 20  OP_Eq, regData, 
2f979 69 4f 6b 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20  iOk, regTemp);. 
2f97a 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20       }.  .      
2f97b 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
2f97c 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69  (pParse, iCur, i
2f97d 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65  Db, pTab, OP_Ope
2f97e 6e 52 65 61 64 29 3b 0a 20 20 20 20 20 20 73 71  nRead);.      sq
2f97f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2f980 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
2f981 20 69 43 75 72 2c 20 30 2c 20 72 65 67 54 65 6d   iCur, 0, regTem
2f982 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  p);.      sqlite
2f983 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2f984 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 4f 6b 29 3b  P_Goto, 0, iOk);
2f985 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2f986 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 73 71  beJumpHere(v, sq
2f987 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
2f988 41 64 64 72 28 76 29 2d 32 29 3b 0a 20 20 20 20  Addr(v)-2);.    
2f989 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2f98a 70 48 65 72 65 28 76 2c 20 69 4d 75 73 74 42 65  pHere(v, iMustBe
2f98b 49 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Int);.      sqli
2f98c 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2f98d 67 28 70 50 61 72 73 65 2c 20 72 65 67 54 65 6d  g(pParse, regTem
2f98e 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  p);.    }else{. 
2f98f 20 20 20 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20       int nCol = 
2f990 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 0a 20 20 20  pFKey->nCol;.   
2f991 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70 20 3d     int regTemp =
2f992 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2f993 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 43 6f  ange(pParse, nCo
2f994 6c 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65  l);.      int re
2f995 67 52 65 63 20 3d 20 73 71 6c 69 74 65 33 47 65  gRec = sqlite3Ge
2f996 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2f997 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20  ;.      KeyInfo 
2f998 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49  *pKey = sqlite3I
2f999 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
2f99a 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 0a 20 20  se, pIdx);.  .  
2f99b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2f99c 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp3(v, OP_Open
2f99d 52 65 61 64 2c 20 69 43 75 72 2c 20 70 49 64 78  Read, iCur, pIdx
2f99e 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20  ->tnum, iDb);.  
2f99f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2f9a0 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28  hangeP4(v, -1, (
2f9a1 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b  char*)pKey, P4_K
2f9a2 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
2f9a3 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
2f9a4 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
2f9a5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2f9a6 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f  eAddOp2(v, OP_Co
2f9a7 70 79 2c 20 61 69 43 6f 6c 5b 69 5d 2b 31 2b 72  py, aiCol[i]+1+r
2f9a8 65 67 44 61 74 61 2c 20 72 65 67 54 65 6d 70 2b  egData, regTemp+
2f9a9 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20  i);.      }.  . 
2f9aa 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70       /* If the p
2f9ab 61 72 65 6e 74 20 74 61 62 6c 65 20 69 73 20 74  arent table is t
2f9ac 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 63  he same as the c
2f9ad 68 69 6c 64 20 74 61 62 6c 65 2c 20 61 6e 64 20  hild table, and 
2f9ae 77 65 20 61 72 65 20 61 62 6f 75 74 0a 20 20 20  we are about.   
2f9af 20 20 20 2a 2a 20 74 6f 20 69 6e 63 72 65 6d 65     ** to increme
2f9b0 6e 74 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  nt the constrain
2f9b1 74 2d 63 6f 75 6e 74 65 72 20 28 69 2e 65 2e 20  t-counter (i.e. 
2f9b2 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52  this is an INSER
2f9b3 54 20 6f 70 65 72 61 74 69 6f 6e 29 2c 0a 20 20  T operation),.  
2f9b4 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 68 65 63      ** then chec
2f9b5 6b 20 69 66 20 74 68 65 20 72 6f 77 20 62 65 69  k if the row bei
2f9b6 6e 67 20 69 6e 73 65 72 74 65 64 20 6d 61 74 63  ng inserted matc
2f9b7 68 65 73 20 69 74 73 65 6c 66 2e 20 49 66 20 73  hes itself. If s
2f9b8 6f 2c 20 64 6f 20 6e 6f 74 0a 20 20 20 20 20 20  o, do not.      
2f9b9 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65  ** increment the
2f9ba 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e   constraint-coun
2f9bb 74 65 72 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20  ter. .      **. 
2f9bc 20 20 20 20 20 2a 2a 20 49 66 20 61 6e 79 20 6f       ** If any o
2f9bd 66 20 74 68 65 20 70 61 72 65 6e 74 2d 6b 65 79  f the parent-key
2f9be 20 76 61 6c 75 65 73 20 61 72 65 20 4e 55 4c 4c   values are NULL
2f9bf 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63  , then the row c
2f9c0 61 6e 6e 6f 74 20 6d 61 74 63 68 20 0a 20 20 20  annot match .   
2f9c1 20 20 20 2a 2a 20 69 74 73 65 6c 66 2e 20 53 6f     ** itself. So
2f9c2 20 73 65 74 20 4a 55 4d 50 49 46 4e 55 4c 4c 20   set JUMPIFNULL 
2f9c3 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20  to make sure we 
2f9c4 64 6f 20 74 68 65 20 4f 50 5f 46 6f 75 6e 64 20  do the OP_Found 
2f9c5 69 66 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20  if any.      ** 
2f9c6 6f 66 20 74 68 65 20 70 61 72 65 6e 74 2d 6b 65  of the parent-ke
2f9c7 79 20 76 61 6c 75 65 73 20 61 72 65 20 4e 55 4c  y values are NUL
2f9c8 4c 20 28 61 74 20 74 68 69 73 20 70 6f 69 6e 74  L (at this point
2f9c9 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 74 68 61   it is known tha
2f9ca 74 0a 20 20 20 20 20 20 2a 2a 20 6e 6f 6e 65 20  t.      ** none 
2f9cb 6f 66 20 74 68 65 20 63 68 69 6c 64 20 6b 65 79  of the child key
2f9cc 20 76 61 6c 75 65 73 20 61 72 65 29 2e 0a 20 20   values are)..  
2f9cd 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
2f9ce 20 70 54 61 62 3d 3d 70 46 4b 65 79 2d 3e 70 46   pTab==pFKey->pF
2f9cf 72 6f 6d 20 26 26 20 6e 49 6e 63 72 3d 3d 31 20  rom && nIncr==1 
2f9d0 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
2f9d1 4a 75 6d 70 20 3d 20 73 71 6c 69 74 65 33 56 64  Jump = sqlite3Vd
2f9d2 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2f9d3 20 2b 20 6e 43 6f 6c 20 2b 20 31 3b 0a 20 20 20   + nCol + 1;.   
2f9d4 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
2f9d5 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
2f9d6 20 20 20 20 20 20 69 6e 74 20 69 43 68 69 6c 64        int iChild
2f9d7 20 3d 20 61 69 43 6f 6c 5b 69 5d 2b 31 2b 72 65   = aiCol[i]+1+re
2f9d8 67 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 20  gData;.         
2f9d9 20 69 6e 74 20 69 50 61 72 65 6e 74 20 3d 20 70   int iParent = p
2f9da 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
2f9db 2b 31 2b 72 65 67 44 61 74 61 3b 0a 20 20 20 20  +1+regData;.    
2f9dc 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61 69        assert( ai
2f9dd 43 6f 6c 5b 69 5d 21 3d 70 54 61 62 2d 3e 69 50  Col[i]!=pTab->iP
2f9de 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20 20  Key );.         
2f9df 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c   if( pIdx->aiCol
2f9e0 75 6d 6e 5b 69 5d 3d 3d 70 54 61 62 2d 3e 69 50  umn[i]==pTab->iP
2f9e1 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Key ){.         
2f9e2 20 20 20 2f 2a 20 54 68 65 20 70 61 72 65 6e 74     /* The parent
2f9e3 20 6b 65 79 20 69 73 20 61 20 63 6f 6d 70 6f 73   key is a compos
2f9e4 69 74 65 20 6b 65 79 20 74 68 61 74 20 69 6e 63  ite key that inc
2f9e5 6c 75 64 65 73 20 74 68 65 20 49 50 4b 20 63 6f  ludes the IPK co
2f9e6 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  lumn */.        
2f9e7 20 20 20 20 69 50 61 72 65 6e 74 20 3d 20 72 65      iParent = re
2f9e8 67 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 20  gData;.         
2f9e9 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c   }.          sql
2f9ea 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2f9eb 2c 20 4f 50 5f 4e 65 2c 20 69 43 68 69 6c 64 2c  , OP_Ne, iChild,
2f9ec 20 69 4a 75 6d 70 2c 20 69 50 61 72 65 6e 74 29   iJump, iParent)
2f9ed 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
2f9ee 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
2f9ef 76 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  v, SQLITE_JUMPIF
2f9f0 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20 7d  NULL);.        }
2f9f1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2f9f2 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2f9f3 5f 47 6f 74 6f 2c 20 30 2c 20 69 4f 6b 29 3b 0a  _Goto, 0, iOk);.
2f9f4 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20        }.  .     
2f9f5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2f9f6 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
2f9f7 6f 72 64 2c 20 72 65 67 54 65 6d 70 2c 20 6e 43  ord, regTemp, nC
2f9f8 6f 6c 2c 20 72 65 67 52 65 63 29 3b 0a 20 20 20  ol, regRec);.   
2f9f9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2f9fa 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 73 71  angeP4(v, -1, sq
2f9fb 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69  lite3IndexAffini
2f9fc 74 79 53 74 72 28 76 2c 70 49 64 78 29 2c 20 50  tyStr(v,pIdx), P
2f9fd 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  4_TRANSIENT);.  
2f9fe 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2f9ff 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50 5f 46  ddOp4Int(v, OP_F
2fa00 6f 75 6e 64 2c 20 69 43 75 72 2c 20 69 4f 6b 2c  ound, iCur, iOk,
2fa01 20 72 65 67 52 65 63 2c 20 30 29 3b 0a 20 20 0a   regRec, 0);.  .
2fa02 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
2fa03 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
2fa04 73 65 2c 20 72 65 67 52 65 63 29 3b 0a 20 20 20  se, regRec);.   
2fa05 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2fa06 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  eTempRange(pPars
2fa07 65 2c 20 72 65 67 54 65 6d 70 2c 20 6e 43 6f 6c  e, regTemp, nCol
2fa08 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
2fa09 69 66 28 20 21 70 46 4b 65 79 2d 3e 69 73 44 65  if( !pFKey->isDe
2fa0a 66 65 72 72 65 64 20 26 26 20 21 70 50 61 72 73  ferred && !pPars
2fa0b 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 26 26 20  e->pToplevel && 
2fa0c 21 70 50 61 72 73 65 2d 3e 69 73 4d 75 6c 74 69  !pParse->isMulti
2fa0d 57 72 69 74 65 20 29 7b 0a 20 20 20 20 2f 2a 20  Write ){.    /* 
2fa0e 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 49 66  Special case: If
2fa0f 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45   this is an INSE
2fa10 52 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  RT statement tha
2fa11 74 20 77 69 6c 6c 20 69 6e 73 65 72 74 20 65 78  t will insert ex
2fa12 61 63 74 6c 79 0a 20 20 20 20 2a 2a 20 6f 6e 65  actly.    ** one
2fa13 20 72 6f 77 20 69 6e 74 6f 20 74 68 65 20 74 61   row into the ta
2fa14 62 6c 65 2c 20 72 61 69 73 65 20 61 20 63 6f 6e  ble, raise a con
2fa15 73 74 72 61 69 6e 74 20 69 6d 6d 65 64 69 61 74  straint immediat
2fa16 65 6c 79 20 69 6e 73 74 65 61 64 20 6f 66 0a 20  ely instead of. 
2fa17 20 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 69     ** incrementi
2fa18 6e 67 20 61 20 63 6f 75 6e 74 65 72 2e 20 54 68  ng a counter. Th
2fa19 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20  is is necessary 
2fa1a 61 73 20 74 68 65 20 56 4d 20 63 6f 64 65 20 69  as the VM code i
2fa1b 73 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 67  s being.    ** g
2fa1c 65 6e 65 72 61 74 65 64 20 66 6f 72 20 77 69 6c  enerated for wil
2fa1d 6c 20 6e 6f 74 20 6f 70 65 6e 20 61 20 73 74 61  l not open a sta
2fa1e 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
2fa1f 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  on.  */.    asse
2fa20 72 74 28 20 6e 49 6e 63 72 3d 3d 31 20 29 3b 0a  rt( nIncr==1 );.
2fa21 20 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43      sqlite3HaltC
2fa22 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 20 20 20  onstraint(.     
2fa23 20 20 20 70 50 61 72 73 65 2c 20 4f 45 5f 41 62     pParse, OE_Ab
2fa24 6f 72 74 2c 20 22 66 6f 72 65 69 67 6e 20 6b 65  ort, "foreign ke
2fa25 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
2fa26 6c 65 64 22 2c 20 50 34 5f 53 54 41 54 49 43 0a  led", P4_STATIC.
2fa27 20 20 20 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a      );.  }else{.
2fa28 20 20 20 20 69 66 28 20 6e 49 6e 63 72 3e 30 20      if( nIncr>0 
2fa29 26 26 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65  && pFKey->isDefe
2fa2a 72 72 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20  rred==0 ){.     
2fa2b 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70   sqlite3ParseTop
2fa2c 6c 65 76 65 6c 28 70 50 61 72 73 65 29 2d 3e 6d  level(pParse)->m
2fa2d 61 79 41 62 6f 72 74 20 3d 20 31 3b 0a 20 20 20  ayAbort = 1;.   
2fa2e 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
2fa2f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46  beAddOp2(v, OP_F
2fa30 6b 43 6f 75 6e 74 65 72 2c 20 70 46 4b 65 79 2d  kCounter, pFKey-
2fa31 3e 69 73 44 65 66 65 72 72 65 64 2c 20 6e 49 6e  >isDeferred, nIn
2fa32 63 72 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69  cr);.  }..  sqli
2fa33 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
2fa34 62 65 6c 28 76 2c 20 69 4f 6b 29 3b 0a 20 20 73  bel(v, iOk);.  s
2fa35 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2fa36 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43  (v, OP_Close, iC
2fa37 75 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ur);.}../*.** Th
2fa38 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
2fa39 61 6c 6c 65 64 20 74 6f 20 67 65 6e 65 72 61 74  alled to generat
2fa3a 65 20 63 6f 64 65 20 65 78 65 63 75 74 65 64 20  e code executed 
2fa3b 77 68 65 6e 20 61 20 72 6f 77 20 69 73 20 64 65  when a row is de
2fa3c 6c 65 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68  leted.** from th
2fa3d 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 6f  e parent table o
2fa3e 66 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  f foreign key co
2fa3f 6e 73 74 72 61 69 6e 74 20 70 46 4b 65 79 20 61  nstraint pFKey a
2fa40 6e 64 2c 20 69 66 20 70 46 4b 65 79 20 69 73 20  nd, if pFKey is 
2fa41 0a 2a 2a 20 64 65 66 65 72 72 65 64 2c 20 77 68  .** deferred, wh
2fa42 65 6e 20 61 20 72 6f 77 20 69 73 20 69 6e 73 65  en a row is inse
2fa43 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 73 61  rted into the sa
2fa44 6d 65 20 74 61 62 6c 65 2e 20 57 68 65 6e 20 67  me table. When g
2fa45 65 6e 65 72 61 74 69 6e 67 0a 2a 2a 20 63 6f 64  enerating.** cod
2fa46 65 20 66 6f 72 20 61 6e 20 53 51 4c 20 55 50 44  e for an SQL UPD
2fa47 41 54 45 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74  ATE operation, t
2fa48 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
2fa49 20 62 65 20 63 61 6c 6c 65 64 20 74 77 69 63 65   be called twice
2fa4a 20 2d 0a 2a 2a 20 6f 6e 63 65 20 74 6f 20 22 64   -.** once to "d
2fa4b 65 6c 65 74 65 22 20 74 68 65 20 6f 6c 64 20 72  elete" the old r
2fa4c 6f 77 20 61 6e 64 20 6f 6e 63 65 20 74 6f 20 22  ow and once to "
2fa4d 69 6e 73 65 72 74 22 20 74 68 65 20 6e 65 77 20  insert" the new 
2fa4e 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  row..**.** The c
2fa4f 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 62 79  ode generated by
2fa50 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   this function s
2fa51 63 61 6e 73 20 74 68 72 6f 75 67 68 20 74 68 65  cans through the
2fa52 20 72 6f 77 73 20 69 6e 20 74 68 65 20 63 68 69   rows in the chi
2fa53 6c 64 0a 2a 2a 20 74 61 62 6c 65 20 74 68 61 74  ld.** table that
2fa54 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74   correspond to t
2fa55 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20  he parent table 
2fa56 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74 65  row being delete
2fa57 64 20 6f 72 20 69 6e 73 65 72 74 65 64 2e 0a 2a  d or inserted..*
2fa58 2a 20 46 6f 72 20 65 61 63 68 20 63 68 69 6c 64  * For each child
2fa59 20 72 6f 77 20 66 6f 75 6e 64 2c 20 6f 6e 65 20   row found, one 
2fa5a 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2fa5b 20 61 63 74 69 6f 6e 73 20 69 73 20 74 61 6b 65   actions is take
2fa5c 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 70 65 72 61  n:.**.**   Opera
2fa5d 74 69 6f 6e 20 7c 20 46 4b 20 74 79 70 65 20 20  tion | FK type  
2fa5e 20 7c 20 41 63 74 69 6f 6e 20 74 61 6b 65 6e 0a   | Action taken.
2fa5f 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
2fa60 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2fa61 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2fa62 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2fa63 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
2fa64 2a 2a 20 20 20 44 45 4c 45 54 45 20 20 20 20 20  **   DELETE     
2fa65 20 69 6d 6d 65 64 69 61 74 65 20 20 20 49 6e 63   immediate   Inc
2fa66 72 65 6d 65 6e 74 20 74 68 65 20 22 69 6d 6d 65  rement the "imme
2fa67 64 69 61 74 65 20 63 6f 6e 73 74 72 61 69 6e 74  diate constraint
2fa68 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 20 20 20   counter"..**   
2fa69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa6a 20 20 20 20 20 20 20 20 4f 72 2c 20 69 66 20 74          Or, if t
2fa6b 68 65 20 4f 4e 20 28 55 50 44 41 54 45 7c 44 45  he ON (UPDATE|DE
2fa6c 4c 45 54 45 29 20 61 63 74 69 6f 6e 20 69 73 20  LETE) action is 
2fa6d 52 45 53 54 52 49 43 54 2c 0a 2a 2a 20 20 20 20  RESTRICT,.**    
2fa6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa6f 20 20 20 20 20 20 20 74 68 72 6f 77 20 61 20 22         throw a "
2fa70 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
2fa71 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22 20 65  traint failed" e
2fa72 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  xception..**.** 
2fa73 20 20 49 4e 53 45 52 54 20 20 20 20 20 20 69 6d    INSERT      im
2fa74 6d 65 64 69 61 74 65 20 20 20 44 65 63 72 65 6d  mediate   Decrem
2fa75 65 6e 74 20 74 68 65 20 22 69 6d 6d 65 64 69 61  ent the "immedia
2fa76 74 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f  te constraint co
2fa77 75 6e 74 65 72 22 2e 0a 2a 2a 0a 2a 2a 20 20 20  unter"..**.**   
2fa78 44 45 4c 45 54 45 20 20 20 20 20 20 64 65 66 65  DELETE      defe
2fa79 72 72 65 64 20 20 20 20 49 6e 63 72 65 6d 65 6e  rred    Incremen
2fa7a 74 20 74 68 65 20 22 64 65 66 65 72 72 65 64 20  t the "deferred 
2fa7b 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74  constraint count
2fa7c 65 72 22 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20  er"..**         
2fa7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa7e 20 20 4f 72 2c 20 69 66 20 74 68 65 20 4f 4e 20    Or, if the ON 
2fa7f 28 55 50 44 41 54 45 7c 44 45 4c 45 54 45 29 20  (UPDATE|DELETE) 
2fa80 61 63 74 69 6f 6e 20 69 73 20 52 45 53 54 52 49  action is RESTRI
2fa81 43 54 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  CT,.**          
2fa82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa83 20 74 68 72 6f 77 20 61 20 22 66 6f 72 65 69 67   throw a "foreig
2fa84 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
2fa85 20 66 61 69 6c 65 64 22 20 65 78 63 65 70 74 69   failed" excepti
2fa86 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 53 45  on..**.**   INSE
2fa87 52 54 20 20 20 20 20 20 64 65 66 65 72 72 65 64  RT      deferred
2fa88 20 20 20 20 44 65 63 72 65 6d 65 6e 74 20 74 68      Decrement th
2fa89 65 20 22 64 65 66 65 72 72 65 64 20 63 6f 6e 73  e "deferred cons
2fa8a 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e  traint counter".
2fa8b 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 6f 70 65  .**.** These ope
2fa8c 72 61 74 69 6f 6e 73 20 61 72 65 20 69 64 65 6e  rations are iden
2fa8d 74 69 66 69 65 64 20 69 6e 20 74 68 65 20 63 6f  tified in the co
2fa8e 6d 6d 65 6e 74 20 61 74 20 74 68 65 20 74 6f 70  mment at the top
2fa8f 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 0a 2a   of this file .*
2fa90 2a 20 28 66 6b 65 79 2e 63 29 20 61 73 20 22 49  * (fkey.c) as "I
2fa91 2e 32 22 20 61 6e 64 20 22 44 2e 32 22 2e 0a 2a  .2" and "D.2"..*
2fa92 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 6b  /.static void fk
2fa93 53 63 61 6e 43 68 69 6c 64 72 65 6e 28 0a 20 20  ScanChildren(.  
2fa94 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2fa95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa96 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74  /* Parse context
2fa97 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
2fa98 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  Src,            
2fa99 20 20 20 20 20 20 2f 2a 20 53 72 63 4c 69 73 74        /* SrcList
2fa9a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2fa9b 74 61 62 6c 65 20 74 6f 20 73 63 61 6e 20 2a 2f  table to scan */
2fa9c 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 0a  .  Table *pTab,.
2fa9d 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20 20    Index *pIdx,  
2fa9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa9f 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79    /* Foreign key
2faa0 20 69 6e 64 65 78 20 2a 2f 0a 20 20 46 4b 65 79   index */.  FKey
2faa1 20 2a 70 46 4b 65 79 2c 20 20 20 20 20 20 20 20   *pFKey,        
2faa2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
2faa3 6f 72 65 69 67 6e 20 6b 65 79 20 72 65 6c 61 74  oreign key relat
2faa4 69 6f 6e 73 68 69 70 20 2a 2f 0a 20 20 69 6e 74  ionship */.  int
2faa5 20 2a 61 69 43 6f 6c 2c 20 20 20 20 20 20 20 20   *aiCol,        
2faa6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2faa7 4d 61 70 20 66 72 6f 6d 20 70 49 64 78 20 63 6f  Map from pIdx co
2faa8 6c 73 20 74 6f 20 63 68 69 6c 64 20 74 61 62 6c  ls to child tabl
2faa9 65 20 63 6f 6c 73 20 2a 2f 0a 20 20 69 6e 74 20  e cols */.  int 
2faaa 72 65 67 44 61 74 61 2c 20 20 20 20 20 20 20 20  regData,        
2faab 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2faac 65 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65 20  eferenced table 
2faad 64 61 74 61 20 73 74 61 72 74 73 20 68 65 72 65  data starts here
2faae 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 63 72 20   */.  int nIncr 
2faaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fab0 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20        /* Amount 
2fab1 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 64 65 66  to increment def
2fab2 65 72 72 65 64 20 63 6f 75 6e 74 65 72 20 62 79  erred counter by
2fab3 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
2fab4 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2fab5 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61  b;       /* Data
2fab6 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  base handle */. 
2fab7 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
2fab8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fab9 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
2faba 69 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20  iable */.  Expr 
2fabb 2a 70 57 68 65 72 65 20 3d 20 30 3b 20 20 20 20  *pWhere = 0;    
2fabc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48             /* WH
2fabd 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20 73 63  ERE clause to sc
2fabe 61 6e 20 77 69 74 68 20 2a 2f 0a 20 20 4e 61 6d  an with */.  Nam
2fabf 65 43 6f 6e 74 65 78 74 20 73 4e 61 6d 65 43 6f  eContext sNameCo
2fac0 6e 74 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20  ntext;       /* 
2fac1 43 6f 6e 74 65 78 74 20 75 73 65 64 20 74 6f 20  Context used to 
2fac2 72 65 73 6f 6c 76 65 20 57 48 45 52 45 20 63 6c  resolve WHERE cl
2fac3 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 49  ause */.  WhereI
2fac4 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20  nfo *pWInfo;    
2fac5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
2fac6 74 65 78 74 20 75 73 65 64 20 62 79 20 73 71 6c  text used by sql
2fac7 69 74 65 33 57 68 65 72 65 58 58 58 28 29 20 2a  ite3WhereXXX() *
2fac8 2f 0a 20 20 69 6e 74 20 69 46 6b 49 66 5a 65 72  /.  int iFkIfZer
2fac9 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  o = 0;          
2faca 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
2facb 66 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 20 2a 2f  f OP_FkIfZero */
2facc 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c  .  Vdbe *v = sql
2facd 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2face 73 65 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  se);..  assert( 
2facf 21 70 49 64 78 20 7c 7c 20 70 49 64 78 2d 3e 70  !pIdx || pIdx->p
2fad0 54 61 62 6c 65 3d 3d 70 54 61 62 20 29 3b 0a 0a  Table==pTab );..
2fad1 20 20 69 66 28 20 6e 49 6e 63 72 3c 30 20 29 7b    if( nIncr<0 ){
2fad2 0a 20 20 20 20 69 46 6b 49 66 5a 65 72 6f 20 3d  .    iFkIfZero =
2fad3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2fad4 70 32 28 76 2c 20 4f 50 5f 46 6b 49 66 5a 65 72  p2(v, OP_FkIfZer
2fad5 6f 2c 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65  o, pFKey->isDefe
2fad6 72 72 65 64 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20  rred, 0);.  }.. 
2fad7 20 2f 2a 20 43 72 65 61 74 65 20 61 6e 20 45 78   /* Create an Ex
2fad8 70 72 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73  pr object repres
2fad9 65 6e 74 69 6e 67 20 61 6e 20 53 51 4c 20 65 78  enting an SQL ex
2fada 70 72 65 73 73 69 6f 6e 20 6c 69 6b 65 3a 0a 20  pression like:. 
2fadb 20 2a 2a 0a 20 20 2a 2a 20 20 20 3c 70 61 72 65   **.  **   <pare
2fadc 6e 74 2d 6b 65 79 31 3e 20 3d 20 3c 63 68 69 6c  nt-key1> = <chil
2fadd 64 2d 6b 65 79 31 3e 20 41 4e 44 20 3c 70 61 72  d-key1> AND <par
2fade 65 6e 74 2d 6b 65 79 32 3e 20 3d 20 3c 63 68 69  ent-key2> = <chi
2fadf 6c 64 2d 6b 65 79 32 3e 20 2e 2e 2e 0a 20 20 2a  ld-key2> ....  *
2fae0 2a 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6c 6c 61  *.  ** The colla
2fae1 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 75 73  tion sequence us
2fae2 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61  ed for the compa
2fae3 72 69 73 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20  rison should be 
2fae4 74 68 61 74 20 6f 66 0a 20 20 2a 2a 20 74 68 65  that of.  ** the
2fae5 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75   parent key colu
2fae6 6d 6e 73 2e 20 54 68 65 20 61 66 66 69 6e 69 74  mns. The affinit
2fae7 79 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20  y of the parent 
2fae8 6b 65 79 20 63 6f 6c 75 6d 6e 20 73 68 6f 75 6c  key column shoul
2fae9 64 0a 20 20 2a 2a 20 62 65 20 61 70 70 6c 69 65  d.  ** be applie
2faea 64 20 74 6f 20 65 61 63 68 20 63 68 69 6c 64 20  d to each child 
2faeb 6b 65 79 20 76 61 6c 75 65 20 62 65 66 6f 72 65  key value before
2faec 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
2faed 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a 20 20 2a  takes place..  *
2faee 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  /.  for(i=0; i<p
2faef 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  FKey->nCol; i++)
2faf0 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66  {.    Expr *pLef
2faf1 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
2faf2 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 72 6f      /* Value fro
2faf3 6d 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 72  m parent table r
2faf4 6f 77 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a  ow */.    Expr *
2faf5 70 52 69 67 68 74 3b 20 20 20 20 20 20 20 20 20  pRight;         
2faf6 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
2faf7 6e 20 72 65 66 20 74 6f 20 63 68 69 6c 64 20 74  n ref to child t
2faf8 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70 72  able */.    Expr
2faf9 20 2a 70 45 71 3b 20 20 20 20 20 20 20 20 20 20   *pEq;          
2fafa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
2fafb 72 65 73 73 69 6f 6e 20 28 70 4c 65 66 74 20 3d  ression (pLeft =
2fafc 20 70 52 69 67 68 74 29 20 2a 2f 0a 20 20 20 20   pRight) */.    
2fafd 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20  int iCol;       
2fafe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2faff 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e   Index of column
2fb00 20 69 6e 20 63 68 69 6c 64 20 74 61 62 6c 65 20   in child table 
2fb01 2a 2f 20 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  */ .    const ch
2fb02 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20  ar *zCol;       
2fb03 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
2fb04 20 63 6f 6c 75 6d 6e 20 69 6e 20 63 68 69 6c 64   column in child
2fb05 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 20 70   table */..    p
2fb06 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78  Left = sqlite3Ex
2fb07 70 72 28 64 62 2c 20 54 4b 5f 52 45 47 49 53 54  pr(db, TK_REGIST
2fb08 45 52 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ER, 0);.    if( 
2fb09 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 2f  pLeft ){.      /
2fb0a 2a 20 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74  * Set the collat
2fb0b 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 61 6e 64  ion sequence and
2fb0c 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65   affinity of the
2fb0d 20 4c 48 53 20 6f 66 20 65 61 63 68 20 54 4b 5f   LHS of each TK_
2fb0e 45 51 0a 20 20 20 20 20 20 2a 2a 20 65 78 70 72  EQ.      ** expr
2fb0f 65 73 73 69 6f 6e 20 74 6f 20 74 68 65 20 70 61  ession to the pa
2fb10 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 20  rent key column 
2fb11 64 65 66 61 75 6c 74 73 2e 20 20 2a 2f 0a 20 20  defaults.  */.  
2fb12 20 20 20 20 69 66 28 20 70 49 64 78 20 29 7b 0a      if( pIdx ){.
2fb13 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a          Column *
2fb14 70 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 69 43  pCol;.        iC
2fb15 6f 6c 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c  ol = pIdx->aiCol
2fb16 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20  umn[i];.        
2fb17 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43  pCol = &pTab->aC
2fb18 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 20 20  ol[iCol];.      
2fb19 20 20 69 66 28 20 70 54 61 62 2d 3e 69 50 4b 65    if( pTab->iPKe
2fb1a 79 3d 3d 69 43 6f 6c 20 29 20 69 43 6f 6c 20 3d  y==iCol ) iCol =
2fb1b 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 70 4c 65   -1;.        pLe
2fb1c 66 74 2d 3e 69 54 61 62 6c 65 20 3d 20 72 65 67  ft->iTable = reg
2fb1d 44 61 74 61 2b 69 43 6f 6c 2b 31 3b 0a 20 20 20  Data+iCol+1;.   
2fb1e 20 20 20 20 20 70 4c 65 66 74 2d 3e 61 66 66 69       pLeft->affi
2fb1f 6e 69 74 79 20 3d 20 70 43 6f 6c 2d 3e 61 66 66  nity = pCol->aff
2fb20 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20 70  inity;.        p
2fb21 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71  Left->pColl = sq
2fb22 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
2fb23 65 71 28 70 50 61 72 73 65 2c 20 70 43 6f 6c 2d  eq(pParse, pCol-
2fb24 3e 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 7d  >zColl);.      }
2fb25 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 4c  else{.        pL
2fb26 65 66 74 2d 3e 69 54 61 62 6c 65 20 3d 20 72 65  eft->iTable = re
2fb27 67 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 70  gData;.        p
2fb28 4c 65 66 74 2d 3e 61 66 66 69 6e 69 74 79 20 3d  Left->affinity =
2fb29 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
2fb2a 47 45 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  GER;.      }.   
2fb2b 20 7d 0a 20 20 20 20 69 43 6f 6c 20 3d 20 61 69   }.    iCol = ai
2fb2c 43 6f 6c 20 3f 20 61 69 43 6f 6c 5b 69 5d 20 3a  Col ? aiCol[i] :
2fb2d 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e   pFKey->aCol[0].
2fb2e 69 46 72 6f 6d 3b 0a 20 20 20 20 61 73 73 65 72  iFrom;.    asser
2fb2f 74 28 20 69 43 6f 6c 3e 3d 30 20 29 3b 0a 20 20  t( iCol>=0 );.  
2fb30 20 20 7a 43 6f 6c 20 3d 20 70 46 4b 65 79 2d 3e    zCol = pFKey->
2fb31 70 46 72 6f 6d 2d 3e 61 43 6f 6c 5b 69 43 6f 6c  pFrom->aCol[iCol
2fb32 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 70 52 69  ].zName;.    pRi
2fb33 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ght = sqlite3Exp
2fb34 72 28 64 62 2c 20 54 4b 5f 49 44 2c 20 7a 43 6f  r(db, TK_ID, zCo
2fb35 6c 29 3b 0a 20 20 20 20 70 45 71 20 3d 20 73 71  l);.    pEq = sq
2fb36 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2fb37 65 2c 20 54 4b 5f 45 51 2c 20 70 4c 65 66 74 2c  e, TK_EQ, pLeft,
2fb38 20 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 20   pRight, 0);.   
2fb39 20 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65   pWhere = sqlite
2fb3a 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70 57 68  3ExprAnd(db, pWh
2fb3b 65 72 65 2c 20 70 45 71 29 3b 0a 20 20 7d 0a 0a  ere, pEq);.  }..
2fb3c 20 20 2f 2a 20 49 66 20 74 68 65 20 63 68 69 6c    /* If the chil
2fb3d 64 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 73  d table is the s
2fb3e 61 6d 65 20 61 73 20 74 68 65 20 70 61 72 65 6e  ame as the paren
2fb3f 74 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 69  t table, and thi
2fb40 73 20 73 63 61 6e 0a 20 20 2a 2a 20 69 73 20 74  s scan.  ** is t
2fb41 61 6b 69 6e 67 20 70 6c 61 63 65 20 61 73 20 70  aking place as p
2fb42 61 72 74 20 6f 66 20 61 20 44 45 4c 45 54 45 20  art of a DELETE 
2fb43 6f 70 65 72 61 74 69 6f 6e 20 28 6f 70 65 72 61  operation (opera
2fb44 74 69 6f 6e 20 44 2e 32 29 2c 20 6f 6d 69 74 20  tion D.2), omit 
2fb45 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65 69  the.  ** row bei
2fb46 6e 67 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20  ng deleted from 
2fb47 74 68 65 20 73 63 61 6e 20 62 79 20 61 64 64 69  the scan by addi
2fb48 6e 67 20 28 24 72 6f 77 69 64 20 21 3d 20 72 6f  ng ($rowid != ro
2fb49 77 69 64 29 20 74 6f 20 74 68 65 20 57 48 45 52  wid) to the WHER
2fb4a 45 20 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2c 20  E .  ** clause, 
2fb4b 77 68 65 72 65 20 24 72 6f 77 69 64 20 69 73 20  where $rowid is 
2fb4c 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  the rowid of the
2fb4d 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74   row being delet
2fb4e 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 54  ed.  */.  if( pT
2fb4f 61 62 3d 3d 70 46 4b 65 79 2d 3e 70 46 72 6f 6d  ab==pFKey->pFrom
2fb50 20 26 26 20 6e 49 6e 63 72 3e 30 20 29 7b 0a 20   && nIncr>0 ){. 
2fb51 20 20 20 45 78 70 72 20 2a 70 45 71 3b 20 20 20     Expr *pEq;   
2fb52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fb53 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 28   /* Expression (
2fb54 70 4c 65 66 74 20 3d 20 70 52 69 67 68 74 29 20  pLeft = pRight) 
2fb55 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4c 65  */.    Expr *pLe
2fb56 66 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ft;             
2fb57 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 72       /* Value fr
2fb58 6f 6d 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20  om parent table 
2fb59 72 6f 77 20 2a 2f 0a 20 20 20 20 45 78 70 72 20  row */.    Expr 
2fb5a 2a 70 52 69 67 68 74 3b 20 20 20 20 20 20 20 20  *pRight;        
2fb5b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
2fb5c 6d 6e 20 72 65 66 20 74 6f 20 63 68 69 6c 64 20  mn ref to child 
2fb5d 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 70 4c 65  table */.    pLe
2fb5e 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ft = sqlite3Expr
2fb5f 28 64 62 2c 20 54 4b 5f 52 45 47 49 53 54 45 52  (db, TK_REGISTER
2fb60 2c 20 30 29 3b 0a 20 20 20 20 70 52 69 67 68 74  , 0);.    pRight
2fb61 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
2fb62 62 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 29  b, TK_COLUMN, 0)
2fb63 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 20  ;.    if( pLeft 
2fb64 26 26 20 70 52 69 67 68 74 20 29 7b 0a 20 20 20  && pRight ){.   
2fb65 20 20 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65     pLeft->iTable
2fb66 20 3d 20 72 65 67 44 61 74 61 3b 0a 20 20 20 20   = regData;.    
2fb67 20 20 70 4c 65 66 74 2d 3e 61 66 66 69 6e 69 74    pLeft->affinit
2fb68 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49  y = SQLITE_AFF_I
2fb69 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 70 52  NTEGER;.      pR
2fb6a 69 67 68 74 2d 3e 69 54 61 62 6c 65 20 3d 20 70  ight->iTable = p
2fb6b 53 72 63 2d 3e 61 5b 30 5d 2e 69 43 75 72 73 6f  Src->a[0].iCurso
2fb6c 72 3b 0a 20 20 20 20 20 20 70 52 69 67 68 74 2d  r;.      pRight-
2fb6d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20  >iColumn = -1;. 
2fb6e 20 20 20 7d 0a 20 20 20 20 70 45 71 20 3d 20 73     }.    pEq = s
2fb6f 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2fb70 73 65 2c 20 54 4b 5f 4e 45 2c 20 70 4c 65 66 74  se, TK_NE, pLeft
2fb71 2c 20 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20  , pRight, 0);.  
2fb72 20 20 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74    pWhere = sqlit
2fb73 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70 57  e3ExprAnd(db, pW
2fb74 68 65 72 65 2c 20 70 45 71 29 3b 0a 20 20 7d 0a  here, pEq);.  }.
2fb75 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68  .  /* Resolve th
2fb76 65 20 72 65 66 65 72 65 6e 63 65 73 20 69 6e 20  e references in 
2fb77 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2fb78 2e 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73  . */.  memset(&s
2fb79 4e 61 6d 65 43 6f 6e 74 65 78 74 2c 20 30 2c 20  NameContext, 0, 
2fb7a 73 69 7a 65 6f 66 28 4e 61 6d 65 43 6f 6e 74 65  sizeof(NameConte
2fb7b 78 74 29 29 3b 0a 20 20 73 4e 61 6d 65 43 6f 6e  xt));.  sNameCon
2fb7c 74 65 78 74 2e 70 53 72 63 4c 69 73 74 20 3d 20  text.pSrcList = 
2fb7d 70 53 72 63 3b 0a 20 20 73 4e 61 6d 65 43 6f 6e  pSrc;.  sNameCon
2fb7e 74 65 78 74 2e 70 50 61 72 73 65 20 3d 20 70 50  text.pParse = pP
2fb7f 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 52  arse;.  sqlite3R
2fb80 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
2fb81 26 73 4e 61 6d 65 43 6f 6e 74 65 78 74 2c 20 70  &sNameContext, p
2fb82 57 68 65 72 65 29 3b 0a 0a 20 20 2f 2a 20 43 72  Where);..  /* Cr
2fb83 65 61 74 65 20 56 44 42 45 20 74 6f 20 6c 6f 6f  eate VDBE to loo
2fb84 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 6e  p through the en
2fb85 74 72 69 65 73 20 69 6e 20 70 53 72 63 20 74 68  tries in pSrc th
2fb86 61 74 20 6d 61 74 63 68 20 74 68 65 20 57 48 45  at match the WHE
2fb87 52 45 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2e 20  RE.  ** clause. 
2fb88 49 66 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  If the constrain
2fb89 74 20 69 73 20 6e 6f 74 20 64 65 66 65 72 72 65  t is not deferre
2fb8a 64 2c 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65  d, throw an exce
2fb8b 70 74 69 6f 6e 20 66 6f 72 0a 20 20 2a 2a 20 65  ption for.  ** e
2fb8c 61 63 68 20 72 6f 77 20 66 6f 75 6e 64 2e 20 4f  ach row found. O
2fb8d 74 68 65 72 77 69 73 65 2c 20 66 6f 72 20 64 65  therwise, for de
2fb8e 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  ferred constrain
2fb8f 74 73 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74 68  ts, increment th
2fb90 65 0a 20 20 2a 2a 20 64 65 66 65 72 72 65 64 20  e.  ** deferred 
2fb91 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74  constraint count
2fb92 65 72 20 62 79 20 6e 49 6e 63 72 20 66 6f 72 20  er by nIncr for 
2fb93 65 61 63 68 20 72 6f 77 20 73 65 6c 65 63 74 65  each row selecte
2fb94 64 2e 20 20 2a 2f 0a 20 20 70 57 49 6e 66 6f 20  d.  */.  pWInfo 
2fb95 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  = sqlite3WhereBe
2fb96 67 69 6e 28 70 50 61 72 73 65 2c 20 70 53 72 63  gin(pParse, pSrc
2fb97 2c 20 70 57 68 65 72 65 2c 20 30 2c 20 30 2c 20  , pWhere, 0, 0, 
2fb98 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 49 6e  0, 0);.  if( nIn
2fb99 63 72 3e 30 20 26 26 20 70 46 4b 65 79 2d 3e 69  cr>0 && pFKey->i
2fb9a 73 44 65 66 65 72 72 65 64 3d 3d 30 20 29 7b 0a  sDeferred==0 ){.
2fb9b 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
2fb9c 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29  Toplevel(pParse)
2fb9d 2d 3e 6d 61 79 41 62 6f 72 74 20 3d 20 31 3b 0a  ->mayAbort = 1;.
2fb9e 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
2fb9f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 6b  eAddOp2(v, OP_Fk
2fba0 43 6f 75 6e 74 65 72 2c 20 70 46 4b 65 79 2d 3e  Counter, pFKey->
2fba1 69 73 44 65 66 65 72 72 65 64 2c 20 6e 49 6e 63  isDeferred, nInc
2fba2 72 29 3b 0a 20 20 69 66 28 20 70 57 49 6e 66 6f  r);.  if( pWInfo
2fba3 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 57   ){.    sqlite3W
2fba4 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b  hereEnd(pWInfo);
2fba5 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e  .  }..  /* Clean
2fba6 20 75 70 20 74 68 65 20 57 48 45 52 45 20 63 6c   up the WHERE cl
2fba7 61 75 73 65 20 63 6f 6e 73 74 72 75 63 74 65 64  ause constructed
2fba8 20 61 62 6f 76 65 2e 20 2a 2f 0a 20 20 73 71 6c   above. */.  sql
2fba9 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2fbaa 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 69 66  b, pWhere);.  if
2fbab 28 20 69 46 6b 49 66 5a 65 72 6f 20 29 7b 0a 20  ( iFkIfZero ){. 
2fbac 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
2fbad 6d 70 48 65 72 65 28 76 2c 20 69 46 6b 49 66 5a  mpHere(v, iFkIfZ
2fbae 65 72 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ero);.  }.}../*.
2fbaf 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
2fbb0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
2fbb1 65 72 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f  er to the head o
2fbb2 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  f a linked list 
2fbb3 6f 66 20 46 4b 0a 2a 2a 20 63 6f 6e 73 74 72 61  of FK.** constra
2fbb4 69 6e 74 73 20 66 6f 72 20 77 68 69 63 68 20 74  ints for which t
2fbb5 61 62 6c 65 20 70 54 61 62 20 69 73 20 74 68 65  able pTab is the
2fbb6 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 46   parent table. F
2fbb7 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 67  or example,.** g
2fbb8 69 76 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  iven the followi
2fbb9 6e 67 20 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a  ng schema:.**.**
2fbba 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
2fbbb 74 31 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59  t1(a PRIMARY KEY
2fbbc 29 3b 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54  );.**   CREATE T
2fbbd 41 42 4c 45 20 74 32 28 62 20 52 45 46 45 52 45  ABLE t2(b REFERE
2fbbe 4e 43 45 53 20 74 31 28 61 29 3b 0a 2a 2a 0a 2a  NCES t1(a);.**.*
2fbbf 2a 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 66  * Calling this f
2fbc0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 61 62  unction with tab
2fbc1 6c 65 20 22 74 31 22 20 61 73 20 61 6e 20 61 72  le "t1" as an ar
2fbc2 67 75 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61  gument returns a
2fbc3 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74   pointer.** to t
2fbc4 68 65 20 46 4b 65 79 20 73 74 72 75 63 74 75 72  he FKey structur
2fbc5 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  e representing t
2fbc6 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  he foreign key c
2fbc7 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 61 62  onstraint on tab
2fbc8 6c 65 0a 2a 2a 20 22 74 32 22 2e 20 43 61 6c 6c  le.** "t2". Call
2fbc9 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ing this functio
2fbca 6e 20 77 69 74 68 20 22 74 32 22 20 61 73 20 74  n with "t2" as t
2fbcb 68 65 20 61 72 67 75 6d 65 6e 74 20 77 6f 75 6c  he argument woul
2fbcc 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 4e 55  d return a.** NU
2fbcd 4c 4c 20 70 6f 69 6e 74 65 72 20 28 61 73 20 74  LL pointer (as t
2fbce 68 65 72 65 20 61 72 65 20 6e 6f 20 46 4b 20 63  here are no FK c
2fbcf 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f 72 20 77  onstraints for w
2fbd0 68 69 63 68 20 74 32 20 69 73 20 74 68 65 20 70  hich t2 is the p
2fbd1 61 72 65 6e 74 0a 2a 2a 20 74 61 62 6c 65 29 2e  arent.** table).
2fbd2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2fbd3 54 45 20 46 4b 65 79 20 2a 73 71 6c 69 74 65 33  TE FKey *sqlite3
2fbd4 46 6b 52 65 66 65 72 65 6e 63 65 73 28 54 61 62  FkReferences(Tab
2fbd5 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e 74  le *pTab){.  int
2fbd6 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
2fbd7 53 74 72 6c 65 6e 33 30 28 70 54 61 62 2d 3e 7a  Strlen30(pTab->z
2fbd8 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  Name);.  return 
2fbd9 28 46 4b 65 79 20 2a 29 73 71 6c 69 74 65 33 48  (FKey *)sqlite3H
2fbda 61 73 68 46 69 6e 64 28 26 70 54 61 62 2d 3e 70  ashFind(&pTab->p
2fbdb 53 63 68 65 6d 61 2d 3e 66 6b 65 79 48 61 73 68  Schema->fkeyHash
2fbdc 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 6e  , pTab->zName, n
2fbdd 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Name);.}../*.** 
2fbde 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
2fbdf 65 6e 74 20 69 73 20 61 20 54 72 69 67 67 65 72  ent is a Trigger
2fbe0 20 73 74 72 75 63 74 75 72 65 20 61 6c 6c 6f 63   structure alloc
2fbe1 61 74 65 64 20 62 79 20 74 68 65 20 0a 2a 2a 20  ated by the .** 
2fbe2 66 6b 41 63 74 69 6f 6e 54 72 69 67 67 65 72 28  fkActionTrigger(
2fbe3 29 20 72 6f 75 74 69 6e 65 2e 20 54 68 69 73 20  ) routine. This 
2fbe4 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65 73  function deletes
2fbe5 20 74 68 65 20 54 72 69 67 67 65 72 20 73 74 72   the Trigger str
2fbe6 75 63 74 75 72 65 0a 2a 2a 20 61 6e 64 20 61 6c  ucture.** and al
2fbe7 6c 20 6f 66 20 69 74 73 20 73 75 62 2d 63 6f 6d  l of its sub-com
2fbe8 70 6f 6e 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  ponents..**.** T
2fbe9 68 65 20 54 72 69 67 67 65 72 20 73 74 72 75 63  he Trigger struc
2fbea 74 75 72 65 20 6f 72 20 61 6e 79 20 6f 66 20 69  ture or any of i
2fbeb 74 73 20 73 75 62 2d 63 6f 6d 70 6f 6e 65 6e 74  ts sub-component
2fbec 73 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61 74  s may be allocat
2fbed 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 6c  ed from.** the l
2fbee 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20  ookaside buffer 
2fbef 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 64 61 74  belonging to dat
2fbf0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 4d  abase handle dbM
2fbf1 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  em..*/.static vo
2fbf2 69 64 20 66 6b 54 72 69 67 67 65 72 44 65 6c 65  id fkTriggerDele
2fbf3 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 4d 65  te(sqlite3 *dbMe
2fbf4 6d 2c 20 54 72 69 67 67 65 72 20 2a 70 29 7b 0a  m, Trigger *p){.
2fbf5 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 54    if( p ){.    T
2fbf6 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65  riggerStep *pSte
2fbf7 70 20 3d 20 70 2d 3e 73 74 65 70 5f 6c 69 73 74  p = p->step_list
2fbf8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
2fbf9 72 44 65 6c 65 74 65 28 64 62 4d 65 6d 2c 20 70  rDelete(dbMem, p
2fbfa 53 74 65 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20  Step->pWhere);. 
2fbfb 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
2fbfc 73 74 44 65 6c 65 74 65 28 64 62 4d 65 6d 2c 20  stDelete(dbMem, 
2fbfd 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74  pStep->pExprList
2fbfe 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  );.    sqlite3Se
2fbff 6c 65 63 74 44 65 6c 65 74 65 28 64 62 4d 65 6d  lectDelete(dbMem
2fc00 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65 63 74  , pStep->pSelect
2fc01 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
2fc02 70 72 44 65 6c 65 74 65 28 64 62 4d 65 6d 2c 20  prDelete(dbMem, 
2fc03 70 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 20 20 73  p->pWhen);.    s
2fc04 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 4d  qlite3DbFree(dbM
2fc05 65 6d 2c 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  em, p);.  }.}../
2fc06 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
2fc07 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  on is called to 
2fc08 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  generate code th
2fc09 61 74 20 72 75 6e 73 20 77 68 65 6e 20 74 61 62  at runs when tab
2fc0a 6c 65 20 70 54 61 62 20 69 73 0a 2a 2a 20 62 65  le pTab is.** be
2fc0b 69 6e 67 20 64 72 6f 70 70 65 64 20 66 72 6f 6d  ing dropped from
2fc0c 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 54   the database. T
2fc0d 68 65 20 53 72 63 4c 69 73 74 20 70 61 73 73 65  he SrcList passe
2fc0e 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
2fc0f 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74  argument.** to t
2fc10 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e  his function con
2fc11 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 65  tains a single e
2fc12 6e 74 72 79 20 67 75 61 72 61 6e 74 65 65 64 20  ntry guaranteed 
2fc13 74 6f 20 72 65 73 6f 6c 76 65 20 74 6f 0a 2a 2a  to resolve to.**
2fc14 20 74 61 62 6c 65 20 70 54 61 62 2e 0a 2a 2a 0a   table pTab..**.
2fc15 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 6e 6f 20  ** Normally, no 
2fc16 63 6f 64 65 20 69 73 20 72 65 71 75 69 72 65 64  code is required
2fc17 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 65 69  . However, if ei
2fc18 74 68 65 72 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29  ther.**.**   (a)
2fc19 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 74 68   The table is th
2fc1a 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 6f  e parent table o
2fc1b 66 20 61 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e  f a FK constrain
2fc1c 74 2c 20 6f 72 0a 2a 2a 20 20 20 28 62 29 20 54  t, or.**   (b) T
2fc1d 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  he table is the 
2fc1e 63 68 69 6c 64 20 74 61 62 6c 65 20 6f 66 20 61  child table of a
2fc1f 20 64 65 66 65 72 72 65 64 20 46 4b 20 63 6f 6e   deferred FK con
2fc20 73 74 72 61 69 6e 74 20 61 6e 64 20 69 74 20 69  straint and it i
2fc21 73 0a 2a 2a 20 20 20 20 20 20 20 64 65 74 65 72  s.**       deter
2fc22 6d 69 6e 65 64 20 61 74 20 72 75 6e 74 69 6d 65  mined at runtime
2fc23 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20   that there are 
2fc24 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 65 66 65  outstanding defe
2fc25 72 72 65 64 20 46 4b 20 0a 2a 2a 20 20 20 20 20  rred FK .**     
2fc26 20 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f    constraint vio
2fc27 6c 61 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 64  lations in the d
2fc28 61 74 61 62 61 73 65 2c 0a 2a 2a 0a 2a 2a 20 74  atabase,.**.** t
2fc29 68 65 6e 20 74 68 65 20 65 71 75 69 76 61 6c 65  hen the equivale
2fc2a 6e 74 20 6f 66 20 22 44 45 4c 45 54 45 20 46 52  nt of "DELETE FR
2fc2b 4f 4d 20 3c 74 62 6c 3e 22 20 69 73 20 65 78 65  OM <tbl>" is exe
2fc2c 63 75 74 65 64 20 62 65 66 6f 72 65 20 64 72 6f  cuted before dro
2fc2d 70 70 69 6e 67 0a 2a 2a 20 74 68 65 20 74 61 62  pping.** the tab
2fc2e 6c 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  le from the data
2fc2f 62 61 73 65 2e 20 54 72 69 67 67 65 72 73 20 61  base. Triggers a
2fc30 72 65 20 64 69 73 61 62 6c 65 64 20 77 68 69 6c  re disabled whil
2fc31 65 20 72 75 6e 6e 69 6e 67 20 74 68 69 73 0a 2a  e running this.*
2fc32 2a 20 44 45 4c 45 54 45 2c 20 62 75 74 20 66 6f  * DELETE, but fo
2fc33 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e  reign key action
2fc34 73 20 61 72 65 20 6e 6f 74 2e 0a 2a 2f 0a 53 51  s are not..*/.SQ
2fc35 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2fc36 64 20 73 71 6c 69 74 65 33 46 6b 44 72 6f 70 54  d sqlite3FkDropT
2fc37 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72  able(Parse *pPar
2fc38 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61  se, SrcList *pNa
2fc39 6d 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29  me, Table *pTab)
2fc3a 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
2fc3b 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2fc3c 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26 53  if( (db->flags&S
2fc3d 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79  QLITE_ForeignKey
2fc3e 73 29 20 26 26 20 21 49 73 56 69 72 74 75 61 6c  s) && !IsVirtual
2fc3f 28 70 54 61 62 29 20 26 26 20 21 70 54 61 62 2d  (pTab) && !pTab-
2fc40 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
2fc41 69 6e 74 20 69 53 6b 69 70 20 3d 20 30 3b 0a 20  int iSkip = 0;. 
2fc42 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c     Vdbe *v = sql
2fc43 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2fc44 73 65 29 3b 0a 0a 20 20 20 20 61 73 73 65 72 74  se);..    assert
2fc45 28 20 76 20 29 3b 20 20 20 20 20 20 20 20 20 20  ( v );          
2fc46 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20          /* VDBE 
2fc47 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
2fc48 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20   allocated */.  
2fc49 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 6b 52    if( sqlite3FkR
2fc4a 65 66 65 72 65 6e 63 65 73 28 70 54 61 62 29 3d  eferences(pTab)=
2fc4b 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53  =0 ){.      /* S
2fc4c 65 61 72 63 68 20 66 6f 72 20 61 20 64 65 66 65  earch for a defe
2fc4d 72 72 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79  rred foreign key
2fc4e 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f 72 20   constraint for 
2fc4f 77 68 69 63 68 20 74 68 69 73 20 74 61 62 6c 65  which this table
2fc50 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65  .      ** is the
2fc51 20 63 68 69 6c 64 20 74 61 62 6c 65 2e 20 49 66   child table. If
2fc52 20 6f 6e 65 20 63 61 6e 6e 6f 74 20 62 65 20 66   one cannot be f
2fc53 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 77 69 74  ound, return wit
2fc54 68 6f 75 74 20 0a 20 20 20 20 20 20 2a 2a 20 67  hout .      ** g
2fc55 65 6e 65 72 61 74 69 6e 67 20 61 6e 79 20 56 44  enerating any VD
2fc56 42 45 20 63 6f 64 65 2e 20 49 66 20 6f 6e 65 20  BE code. If one 
2fc57 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 74 68  can be found, th
2fc58 65 6e 20 6a 75 6d 70 20 6f 76 65 72 0a 20 20 20  en jump over.   
2fc59 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 69 72 65     ** the entire
2fc5a 20 44 45 4c 45 54 45 20 69 66 20 74 68 65 72 65   DELETE if there
2fc5b 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64   are no outstand
2fc5c 69 6e 67 20 64 65 66 65 72 72 65 64 20 63 6f 6e  ing deferred con
2fc5d 73 74 72 61 69 6e 74 73 0a 20 20 20 20 20 20 2a  straints.      *
2fc5e 2a 20 77 68 65 6e 20 74 68 69 73 20 73 74 61 74  * when this stat
2fc5f 65 6d 65 6e 74 20 69 73 20 72 75 6e 2e 20 20 2a  ement is run.  *
2fc60 2f 0a 20 20 20 20 20 20 46 4b 65 79 20 2a 70 3b  /.      FKey *p;
2fc61 0a 20 20 20 20 20 20 66 6f 72 28 70 3d 70 54 61  .      for(p=pTa
2fc62 62 2d 3e 70 46 4b 65 79 3b 20 70 3b 20 70 3d 70  b->pFKey; p; p=p
2fc63 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20  ->pNextFrom){.  
2fc64 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 73 44        if( p->isD
2fc65 65 66 65 72 72 65 64 20 29 20 62 72 65 61 6b 3b  eferred ) break;
2fc66 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2fc67 66 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a  f( !p ) return;.
2fc68 20 20 20 20 20 20 69 53 6b 69 70 20 3d 20 73 71        iSkip = sq
2fc69 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
2fc6a 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c  el(v);.      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 46 6b 49 66 5a 65 72 6f 2c 20 31  , OP_FkIfZero, 1
2fc6d 2c 20 69 53 6b 69 70 29 3b 0a 20 20 20 20 7d 0a  , iSkip);.    }.
2fc6e 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73  .    pParse->dis
2fc6f 61 62 6c 65 54 72 69 67 67 65 72 73 20 3d 20 31  ableTriggers = 1
2fc70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c  ;.    sqlite3Del
2fc71 65 74 65 46 72 6f 6d 28 70 50 61 72 73 65 2c 20  eteFrom(pParse, 
2fc72 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75  sqlite3SrcListDu
2fc73 70 28 64 62 2c 20 70 4e 61 6d 65 2c 20 30 29 2c  p(db, pName, 0),
2fc74 20 30 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d   0);.    pParse-
2fc75 3e 64 69 73 61 62 6c 65 54 72 69 67 67 65 72 73  >disableTriggers
2fc76 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66   = 0;..    /* If
2fc77 20 74 68 65 20 44 45 4c 45 54 45 20 68 61 73 20   the DELETE has 
2fc78 67 65 6e 65 72 61 74 65 64 20 69 6d 6d 65 64 69  generated immedi
2fc79 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ate foreign key 
2fc7a 63 6f 6e 73 74 72 61 69 6e 74 20 0a 20 20 20 20  constraint .    
2fc7b 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 73 2c 20 68  ** violations, h
2fc7c 61 6c 74 20 74 68 65 20 56 44 42 45 20 61 6e 64  alt the VDBE and
2fc7d 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   return an error
2fc7e 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   at this point, 
2fc7f 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 61 6e  before.    ** an
2fc80 79 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20  y modifications 
2fc81 74 6f 20 74 68 65 20 73 63 68 65 6d 61 20 61 72  to the schema ar
2fc82 65 20 6d 61 64 65 2e 20 54 68 69 73 20 69 73 20  e made. This is 
2fc83 62 65 63 61 75 73 65 20 73 74 61 74 65 6d 65 6e  because statemen
2fc84 74 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63  t.    ** transac
2fc85 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 62  tions are not ab
2fc86 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 73  le to rollback s
2fc87 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2e 20 20  chema changes.  
2fc88 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
2fc89 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46  beAddOp2(v, OP_F
2fc8a 6b 49 66 5a 65 72 6f 2c 20 30 2c 20 73 71 6c 69  kIfZero, 0, sqli
2fc8b 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
2fc8c 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 73 71  dr(v)+2);.    sq
2fc8d 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61  lite3HaltConstra
2fc8e 69 6e 74 28 0a 20 20 20 20 20 20 20 20 70 50 61  int(.        pPa
2fc8f 72 73 65 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 22  rse, OE_Abort, "
2fc90 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
2fc91 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22 2c 20  traint failed", 
2fc92 50 34 5f 53 54 41 54 49 43 0a 20 20 20 20 29 3b  P4_STATIC.    );
2fc93 0a 0a 20 20 20 20 69 66 28 20 69 53 6b 69 70 20  ..    if( iSkip 
2fc94 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2fc95 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2fc96 28 76 2c 20 69 53 6b 69 70 29 3b 0a 20 20 20 20  (v, iSkip);.    
2fc97 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  }.  }.}../*.** T
2fc98 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2fc99 63 61 6c 6c 65 64 20 77 68 65 6e 20 69 6e 73 65  called when inse
2fc9a 72 74 69 6e 67 2c 20 64 65 6c 65 74 69 6e 67 20  rting, deleting 
2fc9b 6f 72 20 75 70 64 61 74 69 6e 67 20 61 20 72 6f  or updating a ro
2fc9c 77 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 20 70 54  w of.** table pT
2fc9d 61 62 20 74 6f 20 67 65 6e 65 72 61 74 65 20 56  ab to generate V
2fc9e 44 42 45 20 63 6f 64 65 20 74 6f 20 70 65 72 66  DBE code to perf
2fc9f 6f 72 6d 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  orm foreign key 
2fca0 63 6f 6e 73 74 72 61 69 6e 74 20 0a 2a 2a 20 70  constraint .** p
2fca1 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 74 68  rocessing for th
2fca2 65 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a  e operation..**.
2fca3 2a 2a 20 46 6f 72 20 61 20 44 45 4c 45 54 45 20  ** For a DELETE 
2fca4 6f 70 65 72 61 74 69 6f 6e 2c 20 70 61 72 61 6d  operation, param
2fca5 65 74 65 72 20 72 65 67 4f 6c 64 20 69 73 20 70  eter regOld is p
2fca6 61 73 73 65 64 20 74 68 65 20 69 6e 64 65 78 20  assed the index 
2fca7 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  of the.** first 
2fca8 72 65 67 69 73 74 65 72 20 69 6e 20 61 6e 20 61  register in an a
2fca9 72 72 61 79 20 6f 66 20 28 70 54 61 62 2d 3e 6e  rray of (pTab->n
2fcaa 43 6f 6c 2b 31 29 20 72 65 67 69 73 74 65 72 73  Col+1) registers
2fcab 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 0a   containing the.
2fcac 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  ** rowid of the 
2fcad 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74 65  row being delete
2fcae 64 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 65  d, followed by e
2fcaf 61 63 68 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  ach of the colum
2fcb0 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6f 66 20 74  n values.** of t
2fcb1 68 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c  he row being del
2fcb2 65 74 65 64 2c 20 66 72 6f 6d 20 6c 65 66 74 20  eted, from left 
2fcb3 74 6f 20 72 69 67 68 74 2e 20 50 61 72 61 6d 65  to right. Parame
2fcb4 74 65 72 20 72 65 67 4e 65 77 20 69 73 20 70 61  ter regNew is pa
2fcb5 73 73 65 64 0a 2a 2a 20 7a 65 72 6f 20 69 6e 20  ssed.** zero in 
2fcb6 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a  this case..**.**
2fcb7 20 46 6f 72 20 61 6e 20 49 4e 53 45 52 54 20 6f   For an INSERT o
2fcb8 70 65 72 61 74 69 6f 6e 2c 20 72 65 67 4f 6c 64  peration, regOld
2fcb9 20 69 73 20 70 61 73 73 65 64 20 7a 65 72 6f 20   is passed zero 
2fcba 61 6e 64 20 72 65 67 4e 65 77 20 69 73 20 70 61  and regNew is pa
2fcbb 73 73 65 64 20 74 68 65 0a 2a 2a 20 66 69 72 73  ssed the.** firs
2fcbc 74 20 72 65 67 69 73 74 65 72 20 6f 66 20 61 6e  t register of an
2fcbd 20 61 72 72 61 79 20 6f 66 20 28 70 54 61 62 2d   array of (pTab-
2fcbe 3e 6e 43 6f 6c 2b 31 29 20 72 65 67 69 73 74 65  >nCol+1) registe
2fcbf 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  rs containing th
2fcc0 65 20 6e 65 77 0a 2a 2a 20 72 6f 77 20 64 61 74  e new.** row dat
2fcc1 61 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e 20  a..**.** For an 
2fcc2 55 50 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e  UPDATE operation
2fcc3 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
2fcc4 69 73 20 63 61 6c 6c 65 64 20 74 77 69 63 65 2e  is called twice.
2fcc5 20 4f 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20   Once before.** 
2fcc6 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 72 65 63  the original rec
2fcc7 6f 72 64 20 69 73 20 64 65 6c 65 74 65 64 20 66  ord is deleted f
2fcc8 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 75 73  rom the table us
2fcc9 69 6e 67 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  ing the calling 
2fcca 63 6f 6e 76 65 6e 74 69 6f 6e 0a 2a 2a 20 64 65  convention.** de
2fccb 73 63 72 69 62 65 64 20 66 6f 72 20 44 45 4c 45  scribed for DELE
2fccc 54 45 2e 20 54 68 65 6e 20 61 67 61 69 6e 20 61  TE. Then again a
2fccd 66 74 65 72 20 74 68 65 20 6f 72 69 67 69 6e 61  fter the origina
2fcce 6c 20 72 65 63 6f 72 64 20 69 73 20 64 65 6c 65  l record is dele
2fccf 74 65 64 0a 2a 2a 20 62 75 74 20 62 65 66 6f 72  ted.** but befor
2fcd0 65 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64  e the new record
2fcd1 20 69 73 20 69 6e 73 65 72 74 65 64 20 75 73 69   is inserted usi
2fcd2 6e 67 20 74 68 65 20 49 4e 53 45 52 54 20 63 6f  ng the INSERT co
2fcd3 6e 76 65 6e 74 69 6f 6e 2e 20 0a 2a 2f 0a 53 51  nvention. .*/.SQ
2fcd4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2fcd5 64 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b  d sqlite3FkCheck
2fcd6 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2fcd7 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
2fcd8 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e      /* Parse con
2fcd9 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20  text */.  Table 
2fcda 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 20  *pTab,          
2fcdb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77            /* Row
2fcdc 20 69 73 20 62 65 69 6e 67 20 64 65 6c 65 74 65   is being delete
2fcdd 64 20 66 72 6f 6d 20 74 68 69 73 20 74 61 62 6c  d from this tabl
2fcde 65 20 2a 2f 20 0a 20 20 69 6e 74 20 72 65 67 4f  e */ .  int regO
2fcdf 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ld,             
2fce0 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69          /* Previ
2fce1 6f 75 73 20 72 6f 77 20 64 61 74 61 20 69 73 20  ous row data is 
2fce2 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20  stored here */. 
2fce3 20 69 6e 74 20 72 65 67 4e 65 77 20 20 20 20 20   int regNew     
2fce4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fce5 20 2f 2a 20 4e 65 77 20 72 6f 77 20 64 61 74 61   /* New row data
2fce6 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20   is stored here 
2fce7 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
2fce8 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2fce9 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62  ;       /* Datab
2fcea 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ase handle */.  
2fceb 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 20  FKey *pFKey;    
2fcec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fced 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72 61  /* Used to itera
2fcee 74 65 20 74 68 72 6f 75 67 68 20 46 4b 73 20 2a  te through FKs *
2fcef 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
2fcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fcf1 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
2fcf2 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
2fcf3 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 63 6f  ing pTab */.  co
2fcf4 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  nst char *zDb;  
2fcf5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2fcf6 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73   Name of databas
2fcf7 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61  e containing pTa
2fcf8 62 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 67 6e  b */.  int isIgn
2fcf9 6f 72 65 45 72 72 6f 72 73 20 3d 20 70 50 61 72  oreErrors = pPar
2fcfa 73 65 2d 3e 64 69 73 61 62 6c 65 54 72 69 67 67  se->disableTrigg
2fcfb 65 72 73 3b 0a 0a 20 20 2f 2a 20 45 78 61 63 74  ers;..  /* Exact
2fcfc 6c 79 20 6f 6e 65 20 6f 66 20 72 65 67 4f 6c 64  ly one of regOld
2fcfd 20 61 6e 64 20 72 65 67 4e 65 77 20 73 68 6f 75   and regNew shou
2fcfe 6c 64 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f 2e 20  ld be non-zero. 
2fcff 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 72 65  */.  assert( (re
2fd00 67 4f 6c 64 3d 3d 30 29 21 3d 28 72 65 67 4e 65  gOld==0)!=(regNe
2fd01 77 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  w==0) );..  /* I
2fd02 66 20 66 6f 72 65 69 67 6e 2d 6b 65 79 73 20 61  f foreign-keys a
2fd03 72 65 20 64 69 73 61 62 6c 65 64 2c 20 74 68 69  re disabled, thi
2fd04 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
2fd05 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20  no-op. */.  if( 
2fd06 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  (db->flags&SQLIT
2fd07 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 29 3d 3d  E_ForeignKeys)==
2fd08 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69  0 ) return;..  i
2fd09 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
2fd0a 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
2fd0b 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
2fd0c 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
2fd0d 62 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20  b].zName;..  /* 
2fd0e 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 61 6c 6c  Loop through all
2fd0f 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   the foreign key
2fd10 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f 72   constraints for
2fd11 20 77 68 69 63 68 20 70 54 61 62 20 69 73 20 74   which pTab is t
2fd12 68 65 0a 20 20 2a 2a 20 63 68 69 6c 64 20 74 61  he.  ** child ta
2fd13 62 6c 65 20 28 74 68 65 20 74 61 62 6c 65 20 74  ble (the table t
2fd14 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20  hat the foreign 
2fd15 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 20 69  key definition i
2fd16 73 20 70 61 72 74 20 6f 66 29 2e 20 20 2a 2f 0a  s part of).  */.
2fd17 20 20 66 6f 72 28 70 46 4b 65 79 3d 70 54 61 62    for(pFKey=pTab
2fd18 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b 20  ->pFKey; pFKey; 
2fd19 70 46 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65  pFKey=pFKey->pNe
2fd1a 78 74 46 72 6f 6d 29 7b 0a 20 20 20 20 54 61 62  xtFrom){.    Tab
2fd1b 6c 65 20 2a 70 54 6f 3b 20 20 20 20 20 20 20 20  le *pTo;        
2fd1c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
2fd1d 72 65 6e 74 20 74 61 62 6c 65 20 6f 66 20 66 6f  rent table of fo
2fd1e 72 65 69 67 6e 20 6b 65 79 20 70 46 4b 65 79 20  reign key pFKey 
2fd1f 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  */.    Index *pI
2fd20 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  dx = 0;         
2fd21 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 6e       /* Index on
2fd22 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20   key columns in 
2fd23 70 54 6f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a  pTo */.    int *
2fd24 61 69 46 72 65 65 20 3d 20 30 3b 0a 20 20 20 20  aiFree = 0;.    
2fd25 69 6e 74 20 2a 61 69 43 6f 6c 3b 0a 20 20 20 20  int *aiCol;.    
2fd26 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20 69 6e  int iCol;.    in
2fd27 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 69 73 49  t i;.    int isI
2fd28 67 6e 6f 72 65 20 3d 20 30 3b 0a 0a 20 20 20 20  gnore = 0;..    
2fd29 2f 2a 20 46 69 6e 64 20 74 68 65 20 70 61 72 65  /* Find the pare
2fd2a 6e 74 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73  nt table of this
2fd2b 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 41 6c   foreign key. Al
2fd2c 73 6f 20 66 69 6e 64 20 61 20 75 6e 69 71 75 65  so find a unique
2fd2d 20 69 6e 64 65 78 20 0a 20 20 20 20 2a 2a 20 6f   index .    ** o
2fd2e 6e 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79  n the parent key
2fd2f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
2fd30 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 49 66  parent table. If
2fd31 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65   either of these
2fd32 20 0a 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 20   .    ** schema 
2fd33 69 74 65 6d 73 20 63 61 6e 6e 6f 74 20 62 65 20  items cannot be 
2fd34 6c 6f 63 61 74 65 64 2c 20 73 65 74 20 61 6e 20  located, set an 
2fd35 65 72 72 6f 72 20 69 6e 20 70 50 61 72 73 65 20  error in pParse 
2fd36 61 6e 64 20 72 65 74 75 72 6e 20 0a 20 20 20 20  and return .    
2fd37 2a 2a 20 65 61 72 6c 79 2e 20 20 2a 2f 0a 20 20  ** early.  */.  
2fd38 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 69    if( pParse->di
2fd39 73 61 62 6c 65 54 72 69 67 67 65 72 73 20 29 7b  sableTriggers ){
2fd3a 0a 20 20 20 20 20 20 70 54 6f 20 3d 20 73 71 6c  .      pTo = sql
2fd3b 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
2fd3c 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 7a 44  , pFKey->zTo, zD
2fd3d 62 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  b);.    }else{. 
2fd3e 20 20 20 20 20 70 54 6f 20 3d 20 73 71 6c 69 74       pTo = sqlit
2fd3f 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50  e3LocateTable(pP
2fd40 61 72 73 65 2c 20 30 2c 20 70 46 4b 65 79 2d 3e  arse, 0, pFKey->
2fd41 7a 54 6f 2c 20 7a 44 62 29 3b 0a 20 20 20 20 7d  zTo, zDb);.    }
2fd42 0a 20 20 20 20 69 66 28 20 21 70 54 6f 20 7c 7c  .    if( !pTo ||
2fd43 20 6c 6f 63 61 74 65 46 6b 65 79 49 6e 64 65 78   locateFkeyIndex
2fd44 28 70 50 61 72 73 65 2c 20 70 54 6f 2c 20 70 46  (pParse, pTo, pF
2fd45 4b 65 79 2c 20 26 70 49 64 78 2c 20 26 61 69 46  Key, &pIdx, &aiF
2fd46 72 65 65 29 20 29 7b 0a 20 20 20 20 20 20 61 73  ree) ){.      as
2fd47 73 65 72 74 28 20 69 73 49 67 6e 6f 72 65 45 72  sert( isIgnoreEr
2fd48 72 6f 72 73 3d 3d 30 20 7c 7c 20 28 72 65 67 4f  rors==0 || (regO
2fd49 6c 64 21 3d 30 20 26 26 20 72 65 67 4e 65 77 3d  ld!=0 && regNew=
2fd4a 3d 30 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  =0) );.      if(
2fd4b 20 21 69 73 49 67 6e 6f 72 65 45 72 72 6f 72 73   !isIgnoreErrors
2fd4c 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
2fd4d 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20  iled ) return;. 
2fd4e 20 20 20 20 20 69 66 28 20 70 54 6f 3d 3d 30 20       if( pTo==0 
2fd4f 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ){.        /* If
2fd50 20 69 73 49 67 6e 6f 72 65 45 72 72 6f 72 73 20   isIgnoreErrors 
2fd51 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 61 20  is true, then a 
2fd52 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67 20 64  table is being d
2fd53 72 6f 70 70 65 64 2e 20 49 6e 20 74 68 69 73 0a  ropped. In this.
2fd54 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65 20          ** case 
2fd55 53 51 4c 69 74 65 20 72 75 6e 73 20 61 20 22 44  SQLite runs a "D
2fd56 45 4c 45 54 45 20 46 52 4f 4d 20 78 78 78 22 20  ELETE FROM xxx" 
2fd57 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  on the table bei
2fd58 6e 67 20 64 72 6f 70 70 65 64 0a 20 20 20 20 20  ng dropped.     
2fd59 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 61 63 74     ** before act
2fd5a 75 61 6c 6c 79 20 64 72 6f 70 70 69 6e 67 20 69  ually dropping i
2fd5b 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 68  t in order to ch
2fd5c 65 63 6b 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e  eck FK constrain
2fd5d 74 73 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49  ts..        ** I
2fd5e 66 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62  f the parent tab
2fd5f 6c 65 20 6f 66 20 61 6e 20 46 4b 20 63 6f 6e 73  le of an FK cons
2fd60 74 72 61 69 6e 74 20 6f 6e 20 74 68 65 20 63 75  traint on the cu
2fd61 72 72 65 6e 74 20 74 61 62 6c 65 20 69 73 0a 20  rrent table is. 
2fd62 20 20 20 20 20 20 20 2a 2a 20 6d 69 73 73 69 6e         ** missin
2fd63 67 2c 20 62 65 68 61 76 65 20 61 73 20 69 66 20  g, behave as if 
2fd64 69 74 20 69 73 20 65 6d 70 74 79 2e 20 69 2e 65  it is empty. i.e
2fd65 2e 20 64 65 63 72 65 6d 65 6e 74 20 74 68 65 20  . decrement the 
2fd66 72 65 6c 65 76 61 6e 74 0a 20 20 20 20 20 20 20  relevant.       
2fd67 20 2a 2a 20 46 4b 20 63 6f 75 6e 74 65 72 20 66   ** FK counter f
2fd68 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66 20 74  or each row of t
2fd69 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65  he current table
2fd6a 20 77 69 74 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 6b   with non-NULL k
2fd6b 65 79 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  eys..        */.
2fd6c 20 20 20 20 20 20 20 20 56 64 62 65 20 2a 76 20          Vdbe *v 
2fd6d 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2fd6e 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2fd6f 20 20 69 6e 74 20 69 4a 75 6d 70 20 3d 20 73 71    int iJump = sq
2fd70 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
2fd71 41 64 64 72 28 76 29 20 2b 20 70 46 4b 65 79 2d  Addr(v) + pFKey-
2fd72 3e 6e 43 6f 6c 20 2b 20 31 3b 0a 20 20 20 20 20  >nCol + 1;.     
2fd73 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46     for(i=0; i<pF
2fd74 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Key->nCol; i++){
2fd75 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
2fd76 52 65 67 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f  Reg = pFKey->aCo
2fd77 6c 5b 69 5d 2e 69 46 72 6f 6d 20 2b 20 72 65 67  l[i].iFrom + reg
2fd78 4f 6c 64 20 2b 20 31 3b 0a 20 20 20 20 20 20 20  Old + 1;.       
2fd79 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2fd7a 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c  dOp2(v, OP_IsNul
2fd7b 6c 2c 20 69 52 65 67 2c 20 69 4a 75 6d 70 29 3b  l, iReg, iJump);
2fd7c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2fd7d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2fd7e 64 4f 70 32 28 76 2c 20 4f 50 5f 46 6b 43 6f 75  dOp2(v, OP_FkCou
2fd7f 6e 74 65 72 2c 20 70 46 4b 65 79 2d 3e 69 73 44  nter, pFKey->isD
2fd80 65 66 65 72 72 65 64 2c 20 2d 31 29 3b 0a 20 20  eferred, -1);.  
2fd81 20 20 20 20 7d 0a 20 20 20 20 20 20 63 6f 6e 74      }.      cont
2fd82 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  inue;.    }.    
2fd83 61 73 73 65 72 74 28 20 70 46 4b 65 79 2d 3e 6e  assert( pFKey->n
2fd84 43 6f 6c 3d 3d 31 20 7c 7c 20 28 61 69 46 72 65  Col==1 || (aiFre
2fd85 65 20 26 26 20 70 49 64 78 29 20 29 3b 0a 0a 20  e && pIdx) );.. 
2fd86 20 20 20 69 66 28 20 61 69 46 72 65 65 20 29 7b     if( aiFree ){
2fd87 0a 20 20 20 20 20 20 61 69 43 6f 6c 20 3d 20 61  .      aiCol = a
2fd88 69 46 72 65 65 3b 0a 20 20 20 20 7d 65 6c 73 65  iFree;.    }else
2fd89 7b 0a 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 70  {.      iCol = p
2fd8a 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46  FKey->aCol[0].iF
2fd8b 72 6f 6d 3b 0a 20 20 20 20 20 20 61 69 43 6f 6c  rom;.      aiCol
2fd8c 20 3d 20 26 69 43 6f 6c 3b 0a 20 20 20 20 7d 0a   = &iCol;.    }.
2fd8d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
2fd8e 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  FKey->nCol; i++)
2fd8f 7b 0a 20 20 20 20 20 20 69 66 28 20 61 69 43 6f  {.      if( aiCo
2fd90 6c 5b 69 5d 3d 3d 70 54 61 62 2d 3e 69 50 4b 65  l[i]==pTab->iPKe
2fd91 79 20 29 7b 0a 20 20 20 20 20 20 20 20 61 69 43  y ){.        aiC
2fd92 6f 6c 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 20 20  ol[i] = -1;.    
2fd93 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
2fd94 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
2fd95 41 54 49 4f 4e 0a 20 20 20 20 20 20 2f 2a 20 52  ATION.      /* R
2fd96 65 71 75 65 73 74 20 70 65 72 6d 69 73 73 69 6f  equest permissio
2fd97 6e 20 74 6f 20 72 65 61 64 20 74 68 65 20 70 61  n to read the pa
2fd98 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73  rent key columns
2fd99 2e 20 49 66 20 74 68 65 20 0a 20 20 20 20 20 20  . If the .      
2fd9a 2a 2a 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ** authorization
2fd9b 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
2fd9c 73 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2c  s SQLITE_IGNORE,
2fd9d 20 62 65 68 61 76 65 20 61 73 20 69 66 20 61 6e   behave as if an
2fd9e 79 0a 20 20 20 20 20 20 2a 2a 20 76 61 6c 75 65  y.      ** value
2fd9f 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  s read from the 
2fda0 70 61 72 65 6e 74 20 74 61 62 6c 65 20 61 72 65  parent table are
2fda1 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20   NULL. */.      
2fda2 69 66 28 20 64 62 2d 3e 78 41 75 74 68 20 29 7b  if( db->xAuth ){
2fda3 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 61  .        int rca
2fda4 75 74 68 3b 0a 20 20 20 20 20 20 20 20 63 68 61  uth;.        cha
2fda5 72 20 2a 7a 43 6f 6c 20 3d 20 70 54 6f 2d 3e 61  r *zCol = pTo->a
2fda6 43 6f 6c 5b 70 49 64 78 20 3f 20 70 49 64 78 2d  Col[pIdx ? pIdx-
2fda7 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 3a 20 70  >aiColumn[i] : p
2fda8 54 6f 2d 3e 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65  To->iPKey].zName
2fda9 3b 0a 20 20 20 20 20 20 20 20 72 63 61 75 74 68  ;.        rcauth
2fdaa 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 52 65   = sqlite3AuthRe
2fdab 61 64 43 6f 6c 28 70 50 61 72 73 65 2c 20 70 54  adCol(pParse, pT
2fdac 6f 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 2c 20  o->zName, zCol, 
2fdad 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 69 73  iDb);.        is
2fdae 49 67 6e 6f 72 65 20 3d 20 28 72 63 61 75 74 68  Ignore = (rcauth
2fdaf 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 29  ==SQLITE_IGNORE)
2fdb0 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
2fdb1 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54  .    }..    /* T
2fdb2 61 6b 65 20 61 20 73 68 61 72 65 64 2d 63 61 63  ake a shared-cac
2fdb3 68 65 20 61 64 76 69 73 6f 72 79 20 72 65 61 64  he advisory read
2fdb4 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 70 61 72  -lock on the par
2fdb5 65 6e 74 20 74 61 62 6c 65 2e 20 41 6c 6c 6f 63  ent table. Alloc
2fdb6 61 74 65 20 0a 20 20 20 20 2a 2a 20 61 20 63 75  ate .    ** a cu
2fdb7 72 73 6f 72 20 74 6f 20 75 73 65 20 74 6f 20 73  rsor to use to s
2fdb8 65 61 72 63 68 20 74 68 65 20 75 6e 69 71 75 65  earch the unique
2fdb9 20 69 6e 64 65 78 20 6f 6e 20 74 68 65 20 70 61   index on the pa
2fdba 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73  rent key columns
2fdbb 20 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20   .    ** in the 
2fdbc 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 20 2a  parent table.  *
2fdbd 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 54 61 62  /.    sqlite3Tab
2fdbe 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69  leLock(pParse, i
2fdbf 44 62 2c 20 70 54 6f 2d 3e 74 6e 75 6d 2c 20 30  Db, pTo->tnum, 0
2fdc0 2c 20 70 54 6f 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  , pTo->zName);. 
2fdc1 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b     pParse->nTab+
2fdc2 2b 3b 0a 0a 20 20 20 20 69 66 28 20 72 65 67 4f  +;..    if( regO
2fdc3 6c 64 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  ld!=0 ){.      /
2fdc4 2a 20 41 20 72 6f 77 20 69 73 20 62 65 69 6e 67  * A row is being
2fdc5 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
2fdc6 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2e 20 53  e child table. S
2fdc7 65 61 72 63 68 20 66 6f 72 20 74 68 65 20 70 61  earch for the pa
2fdc8 72 65 6e 74 2e 0a 20 20 20 20 20 20 2a 2a 20 49  rent..      ** I
2fdc9 66 20 74 68 65 20 70 61 72 65 6e 74 20 64 6f 65  f the parent doe
2fdca 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 72 65 6d  s not exist, rem
2fdcb 6f 76 69 6e 67 20 74 68 65 20 63 68 69 6c 64 20  oving the child 
2fdcc 72 6f 77 20 72 65 73 6f 6c 76 65 73 20 61 6e 20  row resolves an 
2fdcd 0a 20 20 20 20 20 20 2a 2a 20 6f 75 74 73 74 61  .      ** outsta
2fdce 6e 64 69 6e 67 20 66 6f 72 65 69 67 6e 20 6b 65  nding foreign ke
2fdcf 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f  y constraint vio
2fdd0 6c 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20  lation. */.     
2fdd1 20 66 6b 4c 6f 6f 6b 75 70 50 61 72 65 6e 74 28   fkLookupParent(
2fdd2 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 6f  pParse, iDb, pTo
2fdd3 2c 20 70 49 64 78 2c 20 70 46 4b 65 79 2c 20 61  , pIdx, pFKey, a
2fdd4 69 43 6f 6c 2c 20 72 65 67 4f 6c 64 2c 20 2d 31  iCol, regOld, -1
2fdd5 2c 69 73 49 67 6e 6f 72 65 29 3b 0a 20 20 20 20  ,isIgnore);.    
2fdd6 7d 0a 20 20 20 20 69 66 28 20 72 65 67 4e 65 77  }.    if( regNew
2fdd7 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  !=0 ){.      /* 
2fdd8 41 20 72 6f 77 20 69 73 20 62 65 69 6e 67 20 61  A row is being a
2fdd9 64 64 65 64 20 74 6f 20 74 68 65 20 63 68 69 6c  dded to the chil
2fdda 64 20 74 61 62 6c 65 2e 20 49 66 20 61 20 70 61  d table. If a pa
2fddb 72 65 6e 74 20 72 6f 77 20 63 61 6e 6e 6f 74 0a  rent row cannot.
2fddc 20 20 20 20 20 20 2a 2a 20 62 65 20 66 6f 75 6e        ** be foun
2fddd 64 2c 20 61 64 64 69 6e 67 20 74 68 65 20 63 68  d, adding the ch
2fdde 69 6c 64 20 72 6f 77 20 68 61 73 20 76 69 6f 6c  ild row has viol
2fddf 61 74 65 64 20 74 68 65 20 46 4b 20 63 6f 6e 73  ated the FK cons
2fde0 74 72 61 69 6e 74 2e 20 2a 2f 20 0a 20 20 20 20  traint. */ .    
2fde1 20 20 66 6b 4c 6f 6f 6b 75 70 50 61 72 65 6e 74    fkLookupParent
2fde2 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54  (pParse, iDb, pT
2fde3 6f 2c 20 70 49 64 78 2c 20 70 46 4b 65 79 2c 20  o, pIdx, pFKey, 
2fde4 61 69 43 6f 6c 2c 20 72 65 67 4e 65 77 2c 20 2b  aiCol, regNew, +
2fde5 31 2c 69 73 49 67 6e 6f 72 65 29 3b 0a 20 20 20  1,isIgnore);.   
2fde6 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 44   }..    sqlite3D
2fde7 62 46 72 65 65 28 64 62 2c 20 61 69 46 72 65 65  bFree(db, aiFree
2fde8 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 6f  );.  }..  /* Loo
2fde9 70 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74 68  p through all th
2fdea 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  e foreign key co
2fdeb 6e 73 74 72 61 69 6e 74 73 20 74 68 61 74 20 72  nstraints that r
2fdec 65 66 65 72 20 74 6f 20 74 68 69 73 20 74 61 62  efer to this tab
2fded 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 46 4b 65  le */.  for(pFKe
2fdee 79 20 3d 20 73 71 6c 69 74 65 33 46 6b 52 65 66  y = sqlite3FkRef
2fdef 65 72 65 6e 63 65 73 28 70 54 61 62 29 3b 20 70  erences(pTab); p
2fdf0 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70 46 4b 65  FKey; pFKey=pFKe
2fdf1 79 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20  y->pNextTo){.   
2fdf2 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 30   Index *pIdx = 0
2fdf3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2fdf4 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e  * Foreign key in
2fdf5 64 65 78 20 66 6f 72 20 70 46 4b 65 79 20 2a 2f  dex for pFKey */
2fdf6 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53  .    SrcList *pS
2fdf7 72 63 3b 0a 20 20 20 20 69 6e 74 20 2a 61 69 43  rc;.    int *aiC
2fdf8 6f 6c 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 28  ol = 0;..    if(
2fdf9 20 21 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72   !pFKey->isDefer
2fdfa 72 65 64 20 26 26 20 21 70 50 61 72 73 65 2d 3e  red && !pParse->
2fdfb 70 54 6f 70 6c 65 76 65 6c 20 26 26 20 21 70 50  pToplevel && !pP
2fdfc 61 72 73 65 2d 3e 69 73 4d 75 6c 74 69 57 72 69  arse->isMultiWri
2fdfd 74 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  te ){.      asse
2fdfe 72 74 28 20 72 65 67 4f 6c 64 3d 3d 30 20 26 26  rt( regOld==0 &&
2fdff 20 72 65 67 4e 65 77 21 3d 30 20 29 3b 0a 20 20   regNew!=0 );.  
2fe00 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 69 6e 67      /* Inserting
2fe01 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 69 6e   a single row in
2fe02 74 6f 20 61 20 70 61 72 65 6e 74 20 74 61 62 6c  to a parent tabl
2fe03 65 20 63 61 6e 6e 6f 74 20 63 61 75 73 65 20 61  e cannot cause a
2fe04 6e 20 69 6d 6d 65 64 69 61 74 65 0a 20 20 20 20  n immediate.    
2fe05 20 20 2a 2a 20 66 6f 72 65 69 67 6e 20 6b 65 79    ** foreign key
2fe06 20 76 69 6f 6c 61 74 69 6f 6e 2e 20 53 6f 20 64   violation. So d
2fe07 6f 20 6e 6f 74 68 69 6e 67 20 69 6e 20 74 68 69  o nothing in thi
2fe08 73 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20 20  s case.  */.    
2fe09 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
2fe0a 7d 0a 0a 20 20 20 20 69 66 28 20 6c 6f 63 61 74  }..    if( locat
2fe0b 65 46 6b 65 79 49 6e 64 65 78 28 70 50 61 72 73  eFkeyIndex(pPars
2fe0c 65 2c 20 70 54 61 62 2c 20 70 46 4b 65 79 2c 20  e, pTab, pFKey, 
2fe0d 26 70 49 64 78 2c 20 26 61 69 43 6f 6c 29 20 29  &pIdx, &aiCol) )
2fe0e 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 49  {.      if( !isI
2fe0f 67 6e 6f 72 65 45 72 72 6f 72 73 20 7c 7c 20 64  gnoreErrors || d
2fe10 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2fe11 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20  ) return;.      
2fe12 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a  continue;.    }.
2fe13 20 20 20 20 61 73 73 65 72 74 28 20 61 69 43 6f      assert( aiCo
2fe14 6c 20 7c 7c 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c  l || pFKey->nCol
2fe15 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 43  ==1 );..    /* C
2fe16 72 65 61 74 65 20 61 20 53 72 63 4c 69 73 74 20  reate a SrcList 
2fe17 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
2fe18 6e 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 74 61  ning a single ta
2fe19 62 6c 65 20 28 74 68 65 20 74 61 62 6c 65 20 0a  ble (the table .
2fe1a 20 20 20 20 2a 2a 20 74 68 65 20 66 6f 72 65 69      ** the forei
2fe1b 67 6e 20 6b 65 79 20 74 68 61 74 20 72 65 66 65  gn key that refe
2fe1c 72 73 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65  rs to this table
2fe1d 20 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 29   is attached to)
2fe1e 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a 20 69 73  . This.    ** is
2fe1f 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68   required for th
2fe20 65 20 73 71 6c 69 74 65 33 57 68 65 72 65 58 58  e sqlite3WhereXX
2fe21 58 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 20  X() interface.  
2fe22 2a 2f 0a 20 20 20 20 70 53 72 63 20 3d 20 73 71  */.    pSrc = sq
2fe23 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
2fe24 6e 64 28 64 62 2c 20 30 2c 20 30 2c 20 30 29 3b  nd(db, 0, 0, 0);
2fe25 0a 20 20 20 20 69 66 28 20 70 53 72 63 20 29 7b  .    if( pSrc ){
2fe26 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72  .      struct Sr
2fe27 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
2fe28 6d 20 3d 20 70 53 72 63 2d 3e 61 3b 0a 20 20 20  m = pSrc->a;.   
2fe29 20 20 20 70 49 74 65 6d 2d 3e 70 54 61 62 20 3d     pItem->pTab =
2fe2a 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 3b 0a 20   pFKey->pFrom;. 
2fe2b 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d       pItem->zNam
2fe2c 65 20 3d 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d  e = pFKey->pFrom
2fe2d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 70  ->zName;.      p
2fe2e 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 6e 52 65 66  Item->pTab->nRef
2fe2f 2b 2b 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d  ++;.      pItem-
2fe30 3e 69 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73  >iCursor = pPars
2fe31 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 0a 20 20  e->nTab++;.  .  
2fe32 20 20 20 20 69 66 28 20 72 65 67 4e 65 77 21 3d      if( regNew!=
2fe33 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6b 53  0 ){.        fkS
2fe34 63 61 6e 43 68 69 6c 64 72 65 6e 28 70 50 61 72  canChildren(pPar
2fe35 73 65 2c 20 70 53 72 63 2c 20 70 54 61 62 2c 20  se, pSrc, pTab, 
2fe36 70 49 64 78 2c 20 70 46 4b 65 79 2c 20 61 69 43  pIdx, pFKey, aiC
2fe37 6f 6c 2c 20 72 65 67 4e 65 77 2c 20 2d 31 29 3b  ol, regNew, -1);
2fe38 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2fe39 66 28 20 72 65 67 4f 6c 64 21 3d 30 20 29 7b 0a  f( regOld!=0 ){.
2fe3a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
2fe3b 65 72 65 20 69 73 20 61 20 52 45 53 54 52 49 43  ere is a RESTRIC
2fe3c 54 20 61 63 74 69 6f 6e 20 63 6f 6e 66 69 67 75  T action configu
2fe3d 72 65 64 20 66 6f 72 20 74 68 65 20 63 75 72 72  red for the curr
2fe3e 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 0a 20 20  ent operation.  
2fe3f 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20        ** on the 
2fe40 70 61 72 65 6e 74 20 74 61 62 6c 65 20 6f 66 20  parent table of 
2fe41 74 68 69 73 20 46 4b 2c 20 74 68 65 6e 20 74 68  this FK, then th
2fe42 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f 6e  row an exception
2fe43 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6d 6d   .        ** imm
2fe44 65 64 69 61 74 65 6c 79 20 69 66 20 74 68 65 20  ediately if the 
2fe45 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73  FK constraint is
2fe46 20 76 69 6f 6c 61 74 65 64 2c 20 65 76 65 6e 20   violated, even 
2fe47 69 66 20 74 68 69 73 20 69 73 20 61 0a 20 20 20  if this is a.   
2fe48 20 20 20 20 20 2a 2a 20 64 65 66 65 72 72 65 64       ** deferred
2fe49 20 74 72 69 67 67 65 72 2e 20 54 68 61 74 27 73   trigger. That's
2fe4a 20 77 68 61 74 20 52 45 53 54 52 49 43 54 20 6d   what RESTRICT m
2fe4b 65 61 6e 73 2e 20 54 6f 20 64 65 66 65 72 20 63  eans. To defer c
2fe4c 68 65 63 6b 69 6e 67 0a 20 20 20 20 20 20 20 20  hecking.        
2fe4d 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  ** the constrain
2fe4e 74 2c 20 74 68 65 20 46 4b 20 73 68 6f 75 6c 64  t, the FK should
2fe4f 20 73 70 65 63 69 66 79 20 4e 4f 20 41 43 54 49   specify NO ACTI
2fe50 4f 4e 20 28 72 65 70 72 65 73 65 6e 74 65 64 0a  ON (represented.
2fe51 20 20 20 20 20 20 20 20 2a 2a 20 75 73 69 6e 67          ** using
2fe52 20 4f 45 5f 4e 6f 6e 65 29 2e 20 4e 4f 20 41 43   OE_None). NO AC
2fe53 54 49 4f 4e 20 69 73 20 74 68 65 20 64 65 66 61  TION is the defa
2fe54 75 6c 74 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20  ult.  */.       
2fe55 20 66 6b 53 63 61 6e 43 68 69 6c 64 72 65 6e 28   fkScanChildren(
2fe56 70 50 61 72 73 65 2c 20 70 53 72 63 2c 20 70 54  pParse, pSrc, pT
2fe57 61 62 2c 20 70 49 64 78 2c 20 70 46 4b 65 79 2c  ab, pIdx, pFKey,
2fe58 20 61 69 43 6f 6c 2c 20 72 65 67 4f 6c 64 2c 20   aiCol, regOld, 
2fe59 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  1);.      }.    
2fe5a 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d    pItem->zName =
2fe5b 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   0;.      sqlite
2fe5c 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64  3SrcListDelete(d
2fe5d 62 2c 20 70 53 72 63 29 3b 0a 20 20 20 20 7d 0a  b, pSrc);.    }.
2fe5e 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2fe5f 65 28 64 62 2c 20 61 69 43 6f 6c 29 3b 0a 20 20  e(db, aiCol);.  
2fe60 7d 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 43 4f 4c  }.}..#define COL
2fe61 55 4d 4e 5f 4d 41 53 4b 28 78 29 20 28 28 28 78  UMN_MASK(x) (((x
2fe62 29 3e 33 31 29 20 3f 20 30 78 66 66 66 66 66 66  )>31) ? 0xffffff
2fe63 66 66 20 3a 20 28 28 75 33 32 29 31 3c 3c 28 78  ff : ((u32)1<<(x
2fe64 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  )))../*.** This 
2fe65 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
2fe66 65 64 20 62 65 66 6f 72 65 20 67 65 6e 65 72 61  ed before genera
2fe67 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 75 70 64  ting code to upd
2fe68 61 74 65 20 6f 72 20 64 65 6c 65 74 65 20 61 20  ate or delete a 
2fe69 0a 2a 2a 20 72 6f 77 20 63 6f 6e 74 61 69 6e 65  .** row containe
2fe6a 64 20 69 6e 20 74 61 62 6c 65 20 70 54 61 62 2e  d in table pTab.
2fe6b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2fe6c 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 46 6b  TE u32 sqlite3Fk
2fe6d 4f 6c 64 6d 61 73 6b 28 0a 20 20 50 61 72 73 65  Oldmask(.  Parse
2fe6e 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2fe6f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
2fe70 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
2fe71 20 54 61 62 6c 65 20 2a 70 54 61 62 20 20 20 20   Table *pTab    
2fe72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe73 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20   /* Table being 
2fe74 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 29 7b 0a 20  modified */.){. 
2fe75 20 75 33 32 20 6d 61 73 6b 20 3d 20 30 3b 0a 20   u32 mask = 0;. 
2fe76 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d   if( pParse->db-
2fe77 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f  >flags&SQLITE_Fo
2fe78 72 65 69 67 6e 4b 65 79 73 20 29 7b 0a 20 20 20  reignKeys ){.   
2fe79 20 46 4b 65 79 20 2a 70 3b 0a 20 20 20 20 69 6e   FKey *p;.    in
2fe7a 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 70 3d 70  t i;.    for(p=p
2fe7b 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70 3b 20 70  Tab->pFKey; p; p
2fe7c 3d 70 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a  =p->pNextFrom){.
2fe7d 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
2fe7e 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 20 6d  <p->nCol; i++) m
2fe7f 61 73 6b 20 7c 3d 20 43 4f 4c 55 4d 4e 5f 4d 41  ask |= COLUMN_MA
2fe80 53 4b 28 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46  SK(p->aCol[i].iF
2fe81 72 6f 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rom);.    }.    
2fe82 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 46 6b 52  for(p=sqlite3FkR
2fe83 65 66 65 72 65 6e 63 65 73 28 70 54 61 62 29 3b  eferences(pTab);
2fe84 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 54 6f   p; p=p->pNextTo
2fe85 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a  ){.      Index *
2fe86 70 49 64 78 20 3d 20 30 3b 0a 20 20 20 20 20 20  pIdx = 0;.      
2fe87 6c 6f 63 61 74 65 46 6b 65 79 49 6e 64 65 78 28  locateFkeyIndex(
2fe88 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 2c  pParse, pTab, p,
2fe89 20 26 70 49 64 78 2c 20 30 29 3b 0a 20 20 20 20   &pIdx, 0);.    
2fe8a 20 20 69 66 28 20 70 49 64 78 20 29 7b 0a 20 20    if( pIdx ){.  
2fe8b 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
2fe8c 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  <pIdx->nColumn; 
2fe8d 69 2b 2b 29 20 6d 61 73 6b 20 7c 3d 20 43 4f 4c  i++) mask |= COL
2fe8e 55 4d 4e 5f 4d 41 53 4b 28 70 49 64 78 2d 3e 61  UMN_MASK(pIdx->a
2fe8f 69 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20 20  iColumn[i]);.   
2fe90 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
2fe91 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a   return mask;.}.
2fe92 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
2fe93 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62  tion is called b
2fe94 65 66 6f 72 65 20 67 65 6e 65 72 61 74 69 6e 67  efore generating
2fe95 20 63 6f 64 65 20 74 6f 20 75 70 64 61 74 65 20   code to update 
2fe96 6f 72 20 64 65 6c 65 74 65 20 61 20 0a 2a 2a 20  or delete a .** 
2fe97 72 6f 77 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  row contained in
2fe98 20 74 61 62 6c 65 20 70 54 61 62 2e 20 49 66 20   table pTab. If 
2fe99 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  the operation is
2fe9a 20 61 20 44 45 4c 45 54 45 2c 20 74 68 65 6e 0a   a DELETE, then.
2fe9b 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 61 43 68  ** parameter aCh
2fe9c 61 6e 67 65 20 69 73 20 70 61 73 73 65 64 20 61  ange is passed a
2fe9d 20 4e 55 4c 4c 20 76 61 6c 75 65 2e 20 46 6f 72   NULL value. For
2fe9e 20 61 6e 20 55 50 44 41 54 45 2c 20 61 43 68 61   an UPDATE, aCha
2fe9f 6e 67 65 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f  nge points.** to
2fea0 20 61 6e 20 61 72 72 61 79 20 6f 66 20 73 69 7a   an array of siz
2fea1 65 20 4e 2c 20 77 68 65 72 65 20 4e 20 69 73 20  e N, where N is 
2fea2 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
2fea3 6c 75 6d 6e 73 20 69 6e 20 74 61 62 6c 65 20 70  lumns in table p
2fea4 54 61 62 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69  Tab..** If the i
2fea5 27 74 68 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f  'th column is no
2fea6 74 20 6d 6f 64 69 66 69 65 64 20 62 79 20 74 68  t modified by th
2fea7 65 20 55 50 44 41 54 45 2c 20 74 68 65 6e 20 74  e UPDATE, then t
2fea8 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
2fea9 20 0a 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68   .** entry in th
2feaa 65 20 61 43 68 61 6e 67 65 5b 5d 20 61 72 72 61  e aChange[] arra
2feab 79 20 69 73 20 73 65 74 20 74 6f 20 2d 31 2e 20  y is set to -1. 
2feac 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73  If the column is
2fead 20 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 74 68   modified,.** th
2feae 65 20 76 61 6c 75 65 20 69 73 20 30 20 6f 72 20  e value is 0 or 
2feaf 67 72 65 61 74 65 72 2e 20 50 61 72 61 6d 65 74  greater. Paramet
2feb0 65 72 20 63 68 6e 67 52 6f 77 69 64 20 69 73 20  er chngRowid is 
2feb1 73 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 74  set to true if t
2feb2 68 65 0a 2a 2a 20 55 50 44 41 54 45 20 73 74 61  he.** UPDATE sta
2feb3 74 65 6d 65 6e 74 20 6d 6f 64 69 66 69 65 73 20  tement modifies 
2feb4 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64 73  the rowid fields
2feb5 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a   of the table..*
2feb6 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 66 6f 72 65  *.** If any fore
2feb7 69 67 6e 20 6b 65 79 20 70 72 6f 63 65 73 73 69  ign key processi
2feb8 6e 67 20 77 69 6c 6c 20 62 65 20 72 65 71 75 69  ng will be requi
2feb9 72 65 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69  red, this functi
2feba 6f 6e 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 72  on returns.** tr
2febb 75 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  ue. If there is 
2febc 6e 6f 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 72  no foreign key r
2febd 65 6c 61 74 65 64 20 70 72 6f 63 65 73 73 69 6e  elated processin
2febe 67 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  g, this function
2febf 20 0a 2a 2a 20 72 65 74 75 72 6e 73 20 66 61 6c   .** returns fal
2fec0 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
2fec1 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2fec2 33 46 6b 52 65 71 75 69 72 65 64 28 0a 20 20 50  3FkRequired(.  P
2fec3 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2fec4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2fec5 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  * Parse context 
2fec6 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2fec7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2fec8 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65       /* Table be
2fec9 69 6e 67 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a  ing modified */.
2feca 20 20 69 6e 74 20 2a 61 43 68 61 6e 67 65 2c 20    int *aChange, 
2fecb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fecc 20 20 2f 2a 20 4e 6f 6e 2d 4e 55 4c 4c 20 66 6f    /* Non-NULL fo
2fecd 72 20 55 50 44 41 54 45 20 6f 70 65 72 61 74 69  r UPDATE operati
2fece 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 63 68 6e  ons */.  int chn
2fecf 67 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20  gRowid          
2fed0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2fed1 20 66 6f 72 20 55 50 44 41 54 45 20 74 68 61 74   for UPDATE that
2fed2 20 61 66 66 65 63 74 73 20 72 6f 77 69 64 20 2a   affects rowid *
2fed3 2f 0a 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73  /.){.  if( pPars
2fed4 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  e->db->flags&SQL
2fed5 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20  ITE_ForeignKeys 
2fed6 29 7b 0a 20 20 20 20 69 66 28 20 21 61 43 68 61  ){.    if( !aCha
2fed7 6e 67 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  nge ){.      /* 
2fed8 41 20 44 45 4c 45 54 45 20 6f 70 65 72 61 74 69  A DELETE operati
2fed9 6f 6e 2e 20 46 6f 72 65 69 67 6e 20 6b 65 79 20  on. Foreign key 
2feda 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 72 65  processing is re
2fedb 71 75 69 72 65 64 20 69 66 20 74 68 65 20 0a 20  quired if the . 
2fedc 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 69 6e       ** table in
2fedd 20 71 75 65 73 74 69 6f 6e 20 69 73 20 65 69 74   question is eit
2fede 68 65 72 20 74 68 65 20 63 68 69 6c 64 20 6f 72  her the child or
2fedf 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 66 6f   parent table fo
2fee0 72 20 61 6e 79 20 0a 20 20 20 20 20 20 2a 2a 20  r any .      ** 
2fee1 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
2fee2 74 72 61 69 6e 74 2e 20 20 2a 2f 0a 20 20 20 20  traint.  */.    
2fee3 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65    return (sqlite
2fee4 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54  3FkReferences(pT
2fee5 61 62 29 20 7c 7c 20 70 54 61 62 2d 3e 70 46 4b  ab) || pTab->pFK
2fee6 65 79 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ey);.    }else{.
2fee7 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73        /* This is
2fee8 20 61 6e 20 55 50 44 41 54 45 2e 20 46 6f 72 65   an UPDATE. Fore
2fee9 69 67 6e 20 6b 65 79 20 70 72 6f 63 65 73 73 69  ign key processi
2feea 6e 67 20 69 73 20 6f 6e 6c 79 20 72 65 71 75 69  ng is only requi
2feeb 72 65 64 20 69 66 20 74 68 65 0a 20 20 20 20 20  red if the.     
2feec 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 6d 6f   ** operation mo
2feed 64 69 66 69 65 73 20 6f 6e 65 20 6f 72 20 6d 6f  difies one or mo
2feee 72 65 20 63 68 69 6c 64 20 6f 72 20 70 61 72 65  re child or pare
2feef 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 2e 20  nt key columns. 
2fef0 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  */.      int i;.
2fef1 20 20 20 20 20 20 46 4b 65 79 20 2a 70 3b 0a 0a        FKey *p;..
2fef2 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69        /* Check i
2fef3 66 20 61 6e 79 20 63 68 69 6c 64 20 6b 65 79 20  f any child key 
2fef4 63 6f 6c 75 6d 6e 73 20 61 72 65 20 62 65 69 6e  columns are bein
2fef5 67 20 6d 6f 64 69 66 69 65 64 2e 20 2a 2f 0a 20  g modified. */. 
2fef6 20 20 20 20 20 66 6f 72 28 70 3d 70 54 61 62 2d       for(p=pTab-
2fef7 3e 70 46 4b 65 79 3b 20 70 3b 20 70 3d 70 2d 3e  >pFKey; p; p=p->
2fef8 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20 20  pNextFrom){.    
2fef9 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
2fefa 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
2fefb 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 68 69          int iChi
2fefc 6c 64 4b 65 79 20 3d 20 70 2d 3e 61 43 6f 6c 5b  ldKey = p->aCol[
2fefd 69 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20 20 20 20  i].iFrom;.      
2fefe 20 20 20 20 69 66 28 20 61 43 68 61 6e 67 65 5b      if( aChange[
2feff 69 43 68 69 6c 64 4b 65 79 5d 3e 3d 30 20 29 20  iChildKey]>=0 ) 
2ff00 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
2ff01 20 20 20 20 69 66 28 20 69 43 68 69 6c 64 4b 65      if( iChildKe
2ff02 79 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 26  y==pTab->iPKey &
2ff03 26 20 63 68 6e 67 52 6f 77 69 64 20 29 20 72 65  & chngRowid ) re
2ff04 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20  turn 1;.        
2ff05 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  }.      }..     
2ff06 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 6e 79   /* Check if any
2ff07 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75   parent key colu
2ff08 6d 6e 73 20 61 72 65 20 62 65 69 6e 67 20 6d 6f  mns are being mo
2ff09 64 69 66 69 65 64 2e 20 2a 2f 0a 20 20 20 20 20  dified. */.     
2ff0a 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 46 6b   for(p=sqlite3Fk
2ff0b 52 65 66 65 72 65 6e 63 65 73 28 70 54 61 62 29  References(pTab)
2ff0c 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 54  ; p; p=p->pNextT
2ff0d 6f 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  o){.        for(
2ff0e 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20  i=0; i<p->nCol; 
2ff0f 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
2ff10 63 68 61 72 20 2a 7a 4b 65 79 20 3d 20 70 2d 3e  char *zKey = p->
2ff11 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 3b 0a 20 20  aCol[i].zCol;.  
2ff12 20 20 20 20 20 20 20 20 69 6e 74 20 69 4b 65 79          int iKey
2ff13 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  ;.          for(
2ff14 69 4b 65 79 3d 30 3b 20 69 4b 65 79 3c 70 54 61  iKey=0; iKey<pTa
2ff15 62 2d 3e 6e 43 6f 6c 3b 20 69 4b 65 79 2b 2b 29  b->nCol; iKey++)
2ff16 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 43 6f  {.            Co
2ff17 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54  lumn *pCol = &pT
2ff18 61 62 2d 3e 61 43 6f 6c 5b 69 4b 65 79 5d 3b 0a  ab->aCol[iKey];.
2ff19 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
2ff1a 28 7a 4b 65 79 20 3f 20 21 73 71 6c 69 74 65 33  (zKey ? !sqlite3
2ff1b 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e  StrICmp(pCol->zN
2ff1c 61 6d 65 2c 20 7a 4b 65 79 29 0a 20 20 20 20 20  ame, zKey).     
2ff1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ff1e 20 3a 20 28 70 43 6f 6c 2d 3e 63 6f 6c 46 6c 61   : (pCol->colFla
2ff1f 67 73 20 26 20 43 4f 4c 46 4c 41 47 5f 50 52 49  gs & COLFLAG_PRI
2ff20 4d 4b 45 59 29 21 3d 30 29 20 29 7b 0a 20 20 20  MKEY)!=0) ){.   
2ff21 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61             if( a
2ff22 43 68 61 6e 67 65 5b 69 4b 65 79 5d 3e 3d 30 20  Change[iKey]>=0 
2ff23 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
2ff24 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 4b            if( iK
2ff25 65 79 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20  ey==pTab->iPKey 
2ff26 26 26 20 63 68 6e 67 52 6f 77 69 64 20 29 20 72  && chngRowid ) r
2ff27 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20  eturn 1;.       
2ff28 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2ff29 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
2ff2a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
2ff2b 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
2ff2c 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
2ff2d 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  n is called when
2ff2e 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45   an UPDATE or DE
2ff2f 4c 45 54 45 20 6f 70 65 72 61 74 69 6f 6e 20 69  LETE operation i
2ff30 73 20 62 65 69 6e 67 20 0a 2a 2a 20 63 6f 6d 70  s being .** comp
2ff31 69 6c 65 64 20 6f 6e 20 74 61 62 6c 65 20 70 54  iled on table pT
2ff32 61 62 2c 20 77 68 69 63 68 20 69 73 20 74 68 65  ab, which is the
2ff33 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 6f 66   parent table of
2ff34 20 66 6f 72 65 69 67 6e 2d 6b 65 79 20 70 46 4b   foreign-key pFK
2ff35 65 79 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 75  ey..** If the cu
2ff36 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrent operation 
2ff37 69 73 20 61 6e 20 55 50 44 41 54 45 2c 20 74 68  is an UPDATE, th
2ff38 65 6e 20 74 68 65 20 70 43 68 61 6e 67 65 73 20  en the pChanges 
2ff39 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20  parameter is.** 
2ff3a 70 61 73 73 65 64 20 61 20 70 6f 69 6e 74 65 72  passed a pointer
2ff3b 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20   to the list of 
2ff3c 63 6f 6c 75 6d 6e 73 20 62 65 69 6e 67 20 6d 6f  columns being mo
2ff3d 64 69 66 69 65 64 2e 20 49 66 20 69 74 20 69 73  dified. If it is
2ff3e 20 61 0a 2a 2a 20 44 45 4c 45 54 45 2c 20 70 43   a.** DELETE, pC
2ff3f 68 61 6e 67 65 73 20 69 73 20 70 61 73 73 65 64  hanges is passed
2ff40 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e   a NULL pointer.
2ff41 0a 2a 2a 0a 2a 2a 20 49 74 20 72 65 74 75 72 6e  .**.** It return
2ff42 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
2ff43 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74 75   Trigger structu
2ff44 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20  re containing a 
2ff45 74 72 69 67 67 65 72 0a 2a 2a 20 65 71 75 69 76  trigger.** equiv
2ff46 61 6c 65 6e 74 20 74 6f 20 74 68 65 20 4f 4e 20  alent to the ON 
2ff47 55 50 44 41 54 45 20 6f 72 20 4f 4e 20 44 45 4c  UPDATE or ON DEL
2ff48 45 54 45 20 61 63 74 69 6f 6e 20 73 70 65 63 69  ETE action speci
2ff49 66 69 65 64 20 62 79 20 70 46 4b 65 79 2e 0a 2a  fied by pFKey..*
2ff4a 2a 20 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20  * If the action 
2ff4b 69 73 20 22 4e 4f 20 41 43 54 49 4f 4e 22 20 6f  is "NO ACTION" o
2ff4c 72 20 22 52 45 53 54 52 49 43 54 22 2c 20 74 68  r "RESTRICT", th
2ff4d 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  en a NULL pointe
2ff4e 72 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  r is.** returned
2ff4f 20 28 74 68 65 73 65 20 61 63 74 69 6f 6e 73 20   (these actions 
2ff50 72 65 71 75 69 72 65 20 6e 6f 20 73 70 65 63 69  require no speci
2ff51 61 6c 20 68 61 6e 64 6c 69 6e 67 20 62 79 20 74  al handling by t
2ff52 68 65 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 73  he triggers.** s
2ff53 75 62 2d 73 79 73 74 65 6d 2c 20 63 6f 64 65 20  ub-system, code 
2ff54 66 6f 72 20 74 68 65 6d 20 69 73 20 63 72 65 61  for them is crea
2ff55 74 65 64 20 62 79 20 66 6b 53 63 61 6e 43 68 69  ted by fkScanChi
2ff56 6c 64 72 65 6e 28 29 29 2e 0a 2a 2a 0a 2a 2a 20  ldren())..**.** 
2ff57 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20  For example, if 
2ff58 70 46 4b 65 79 20 69 73 20 74 68 65 20 66 6f 72  pFKey is the for
2ff59 65 69 67 6e 20 6b 65 79 20 61 6e 64 20 70 54 61  eign key and pTa
2ff5a 62 20 69 73 20 74 61 62 6c 65 20 22 70 22 20 69  b is table "p" i
2ff5b 6e 20 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77  n .** the follow
2ff5c 69 6e 67 20 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a  ing schema:.**.*
2ff5d 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45  *   CREATE TABLE
2ff5e 20 70 28 70 6b 20 50 52 49 4d 41 52 59 20 4b 45   p(pk PRIMARY KE
2ff5f 59 29 3b 0a 2a 2a 20 20 20 43 52 45 41 54 45 20  Y);.**   CREATE 
2ff60 54 41 42 4c 45 20 63 28 63 6b 20 52 45 46 45 52  TABLE c(ck REFER
2ff61 45 4e 43 45 53 20 70 20 4f 4e 20 44 45 4c 45 54  ENCES p ON DELET
2ff62 45 20 43 41 53 43 41 44 45 29 3b 0a 2a 2a 0a 2a  E CASCADE);.**.*
2ff63 2a 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72  * then the retur
2ff64 6e 65 64 20 74 72 69 67 67 65 72 20 73 74 72 75  ned trigger stru
2ff65 63 74 75 72 65 20 69 73 20 65 71 75 69 76 61 6c  cture is equival
2ff66 65 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20  ent to:.**.**   
2ff67 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 2e  CREATE TRIGGER .
2ff68 2e 2e 20 44 45 4c 45 54 45 20 4f 4e 20 70 20 42  .. DELETE ON p B
2ff69 45 47 49 4e 0a 2a 2a 20 20 20 20 20 44 45 4c 45  EGIN.**     DELE
2ff6a 54 45 20 46 52 4f 4d 20 63 20 57 48 45 52 45 20  TE FROM c WHERE 
2ff6b 63 6b 20 3d 20 6f 6c 64 2e 70 6b 3b 0a 2a 2a 20  ck = old.pk;.** 
2ff6c 20 20 45 4e 44 3b 0a 2a 2a 0a 2a 2a 20 54 68 65    END;.**.** The
2ff6d 20 72 65 74 75 72 6e 65 64 20 70 6f 69 6e 74 65   returned pointe
2ff6e 72 20 69 73 20 63 61 63 68 65 64 20 61 73 20 70  r is cached as p
2ff6f 61 72 74 20 6f 66 20 74 68 65 20 66 6f 72 65 69  art of the forei
2ff70 67 6e 20 6b 65 79 20 6f 62 6a 65 63 74 2e 20 49  gn key object. I
2ff71 74 0a 2a 2a 20 69 73 20 65 76 65 6e 74 75 61 6c  t.** is eventual
2ff72 6c 79 20 66 72 65 65 64 20 61 6c 6f 6e 67 20 77  ly freed along w
2ff73 69 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20  ith the rest of 
2ff74 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  the foreign key 
2ff75 6f 62 6a 65 63 74 20 62 79 20 0a 2a 2a 20 73 71  object by .** sq
2ff76 6c 69 74 65 33 46 6b 44 65 6c 65 74 65 28 29 2e  lite3FkDelete().
2ff77 0a 2a 2f 0a 73 74 61 74 69 63 20 54 72 69 67 67  .*/.static Trigg
2ff78 65 72 20 2a 66 6b 41 63 74 69 6f 6e 54 72 69 67  er *fkActionTrig
2ff79 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ger(.  Parse *pP
2ff7a 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
2ff7b 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20         /* Parse 
2ff7c 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
2ff7d 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  le *pTab,       
2ff7e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ff7f 54 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61  Table being upda
2ff80 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 20 66  ted or deleted f
2ff81 72 6f 6d 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70  rom */.  FKey *p
2ff82 46 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20  FKey,           
2ff83 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 65           /* Fore
2ff84 69 67 6e 20 6b 65 79 20 74 6f 20 67 65 74 20 61  ign key to get a
2ff85 63 74 69 6f 6e 20 66 6f 72 20 2a 2f 0a 20 20 45  ction for */.  E
2ff86 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65  xprList *pChange
2ff87 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  s              /
2ff88 2a 20 43 68 61 6e 67 65 2d 6c 69 73 74 20 66 6f  * Change-list fo
2ff89 72 20 55 50 44 41 54 45 2c 20 4e 55 4c 4c 20 66  r UPDATE, NULL f
2ff8a 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a 29 7b 0a  or DELETE */.){.
2ff8b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2ff8c 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
2ff8d 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
2ff8e 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61 63  ndle */.  int ac
2ff8f 74 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20  tion;           
2ff90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
2ff91 20 6f 66 20 4f 45 5f 4e 6f 6e 65 2c 20 4f 45 5f   of OE_None, OE_
2ff92 43 61 73 63 61 64 65 20 65 74 63 2e 20 2a 2f 0a  Cascade etc. */.
2ff93 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
2ff94 67 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  ger;            
2ff95 20 20 2f 2a 20 54 72 69 67 67 65 72 20 64 65 66    /* Trigger def
2ff96 69 6e 69 74 69 6f 6e 20 74 6f 20 72 65 74 75 72  inition to retur
2ff97 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 41 63 74 69  n */.  int iActi
2ff98 6f 6e 20 3d 20 28 70 43 68 61 6e 67 65 73 21 3d  on = (pChanges!=
2ff99 30 29 3b 20 20 20 20 2f 2a 20 31 20 66 6f 72 20  0);    /* 1 for 
2ff9a 55 50 44 41 54 45 2c 20 30 20 66 6f 72 20 44 45  UPDATE, 0 for DE
2ff9b 4c 45 54 45 20 2a 2f 0a 0a 20 20 61 63 74 69 6f  LETE */..  actio
2ff9c 6e 20 3d 20 70 46 4b 65 79 2d 3e 61 41 63 74 69  n = pFKey->aActi
2ff9d 6f 6e 5b 69 41 63 74 69 6f 6e 5d 3b 0a 20 20 70  on[iAction];.  p
2ff9e 54 72 69 67 67 65 72 20 3d 20 70 46 4b 65 79 2d  Trigger = pFKey-
2ff9f 3e 61 70 54 72 69 67 67 65 72 5b 69 41 63 74 69  >apTrigger[iActi
2ffa0 6f 6e 5d 3b 0a 0a 20 20 69 66 28 20 61 63 74 69  on];..  if( acti
2ffa1 6f 6e 21 3d 4f 45 5f 4e 6f 6e 65 20 26 26 20 21  on!=OE_None && !
2ffa2 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
2ffa3 75 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69  u8 enableLookasi
2ffa4 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  de;           /*
2ffa5 20 43 6f 70 79 20 6f 66 20 64 62 2d 3e 6c 6f 6f   Copy of db->loo
2ffa6 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
2ffa7 2a 2f 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73  */.    char cons
2ffa8 74 20 2a 7a 46 72 6f 6d 3b 20 20 20 20 20 20 20  t *zFrom;       
2ffa9 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2ffaa 63 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f 0a 20  child table */. 
2ffab 20 20 20 69 6e 74 20 6e 46 72 6f 6d 3b 20 20 20     int nFrom;   
2ffac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ffad 20 2f 2a 20 4c 65 6e 67 74 68 20 69 6e 20 62 79   /* Length in by
2ffae 74 65 73 20 6f 66 20 7a 46 72 6f 6d 20 2a 2f 0a  tes of zFrom */.
2ffaf 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20      Index *pIdx 
2ffb0 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
2ffb1 20 20 2f 2a 20 50 61 72 65 6e 74 20 6b 65 79 20    /* Parent key 
2ffb2 69 6e 64 65 78 20 66 6f 72 20 74 68 69 73 20 46  index for this F
2ffb3 4b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 61 69  K */.    int *ai
2ffb4 43 6f 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Col = 0;        
2ffb5 20 20 20 20 20 20 20 2f 2a 20 63 68 69 6c 64 20         /* child 
2ffb6 74 61 62 6c 65 20 63 6f 6c 73 20 2d 3e 20 70 61  table cols -> pa
2ffb7 72 65 6e 74 20 6b 65 79 20 63 6f 6c 73 20 2a 2f  rent key cols */
2ffb8 0a 20 20 20 20 54 72 69 67 67 65 72 53 74 65 70  .    TriggerStep
2ffb9 20 2a 70 53 74 65 70 20 3d 20 30 3b 20 20 20 20   *pStep = 0;    
2ffba 20 20 20 20 2f 2a 20 46 69 72 73 74 20 28 6f 6e      /* First (on
2ffbb 6c 79 29 20 73 74 65 70 20 6f 66 20 74 72 69 67  ly) step of trig
2ffbc 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  ger program */. 
2ffbd 20 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 20     Expr *pWhere 
2ffbe 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
2ffbf 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65   /* WHERE clause
2ffc0 20 6f 66 20 74 72 69 67 67 65 72 20 73 74 65 70   of trigger step
2ffc1 20 2a 2f 0a 20 20 20 20 45 78 70 72 4c 69 73 74   */.    ExprList
2ffc2 20 2a 70 4c 69 73 74 20 3d 20 30 3b 20 20 20 20   *pList = 0;    
2ffc3 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 73        /* Changes
2ffc4 20 6c 69 73 74 20 69 66 20 4f 4e 20 55 50 44 41   list if ON UPDA
2ffc5 54 45 20 43 41 53 43 41 44 45 20 2a 2f 0a 20 20  TE CASCADE */.  
2ffc6 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
2ffc7 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
2ffc8 2f 2a 20 49 66 20 52 45 53 54 52 49 43 54 2c 20  /* If RESTRICT, 
2ffc9 22 53 45 4c 45 43 54 20 52 41 49 53 45 28 2e 2e  "SELECT RAISE(..
2ffca 2e 29 22 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  .)" */.    int i
2ffcb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2ffcc 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
2ffcd 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
2ffce 0a 20 20 20 20 45 78 70 72 20 2a 70 57 68 65 6e  .    Expr *pWhen
2ffcf 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2ffd0 20 20 20 2f 2a 20 57 48 45 4e 20 63 6c 61 75 73     /* WHEN claus
2ffd1 65 20 66 6f 72 20 74 68 65 20 74 72 69 67 67 65  e for the trigge
2ffd2 72 20 2a 2f 0a 0a 20 20 20 20 69 66 28 20 6c 6f  r */..    if( lo
2ffd3 63 61 74 65 46 6b 65 79 49 6e 64 65 78 28 70 50  cateFkeyIndex(pP
2ffd4 61 72 73 65 2c 20 70 54 61 62 2c 20 70 46 4b 65  arse, pTab, pFKe
2ffd5 79 2c 20 26 70 49 64 78 2c 20 26 61 69 43 6f 6c  y, &pIdx, &aiCol
2ffd6 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
2ffd7 20 20 61 73 73 65 72 74 28 20 61 69 43 6f 6c 20    assert( aiCol 
2ffd8 7c 7c 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3d 3d  || pFKey->nCol==
2ffd9 31 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d  1 );..    for(i=
2ffda 30 3b 20 69 3c 70 46 4b 65 79 2d 3e 6e 43 6f 6c  0; i<pFKey->nCol
2ffdb 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 54 6f  ; i++){.      To
2ffdc 6b 65 6e 20 74 4f 6c 64 20 3d 20 7b 20 22 6f 6c  ken tOld = { "ol
2ffdd 64 22 2c 20 33 20 7d 3b 20 20 2f 2a 20 4c 69 74  d", 3 };  /* Lit
2ffde 65 72 61 6c 20 22 6f 6c 64 22 20 74 6f 6b 65 6e  eral "old" token
2ffdf 20 2a 2f 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20   */.      Token 
2ffe0 74 4e 65 77 20 3d 20 7b 20 22 6e 65 77 22 2c 20  tNew = { "new", 
2ffe1 33 20 7d 3b 20 20 2f 2a 20 4c 69 74 65 72 61 6c  3 };  /* Literal
2ffe2 20 22 6e 65 77 22 20 74 6f 6b 65 6e 20 2a 2f 0a   "new" token */.
2ffe3 20 20 20 20 20 20 54 6f 6b 65 6e 20 74 46 72 6f        Token tFro
2ffe4 6d 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  mCol;           
2ffe5 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c    /* Name of col
2ffe6 75 6d 6e 20 69 6e 20 63 68 69 6c 64 20 74 61 62  umn in child tab
2ffe7 6c 65 20 2a 2f 0a 20 20 20 20 20 20 54 6f 6b 65  le */.      Toke
2ffe8 6e 20 74 54 6f 43 6f 6c 3b 20 20 20 20 20 20 20  n tToCol;       
2ffe9 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2ffea 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 61 72  of column in par
2ffeb 65 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  ent table */.   
2ffec 20 20 20 69 6e 74 20 69 46 72 6f 6d 43 6f 6c 3b     int iFromCol;
2ffed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2ffee 2a 20 49 64 78 20 6f 66 20 63 6f 6c 75 6d 6e 20  * Idx of column 
2ffef 69 6e 20 63 68 69 6c 64 20 74 61 62 6c 65 20 2a  in child table *
2fff0 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45  /.      Expr *pE
2fff1 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  q;              
2fff2 20 20 20 20 2f 2a 20 74 46 72 6f 6d 43 6f 6c 20      /* tFromCol 
2fff3 3d 20 4f 4c 44 2e 74 54 6f 43 6f 6c 20 2a 2f 0a  = OLD.tToCol */.
2fff4 0a 20 20 20 20 20 20 69 46 72 6f 6d 43 6f 6c 20  .      iFromCol 
2fff5 3d 20 61 69 43 6f 6c 20 3f 20 61 69 43 6f 6c 5b  = aiCol ? aiCol[
2fff6 69 5d 20 3a 20 70 46 4b 65 79 2d 3e 61 43 6f 6c  i] : pFKey->aCol
2fff7 5b 30 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20 20 20  [0].iFrom;.     
2fff8 20 61 73 73 65 72 74 28 20 69 46 72 6f 6d 43 6f   assert( iFromCo
2fff9 6c 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 54  l>=0 );.      tT
2fffa 6f 43 6f 6c 2e 7a 20 3d 20 70 49 64 78 20 3f 20  oCol.z = pIdx ? 
2fffb 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d  pTab->aCol[pIdx-
2fffc 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 2e 7a 4e  >aiColumn[i]].zN
2fffd 61 6d 65 20 3a 20 22 6f 69 64 22 3b 0a 20 20 20  ame : "oid";.   
2fffe 20 20 20 74 46 72 6f 6d 43 6f 6c 2e 7a 20 3d 20     tFromCol.z = 
2ffff 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d 3e 61 43  pFKey->pFrom->aC
30000 6f 6c 5b 69 46 72 6f 6d 43 6f 6c 5d 2e 7a 4e 61  ol[iFromCol].zNa
30001 6d 65 3b 0a 0a 20 20 20 20 20 20 74 54 6f 43 6f  me;..      tToCo
30002 6c 2e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  l.n = sqlite3Str
30003 6c 65 6e 33 30 28 74 54 6f 43 6f 6c 2e 7a 29 3b  len30(tToCol.z);
30004 0a 20 20 20 20 20 20 74 46 72 6f 6d 43 6f 6c 2e  .      tFromCol.
30005 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
30006 6e 33 30 28 74 46 72 6f 6d 43 6f 6c 2e 7a 29 3b  n30(tFromCol.z);
30007 0a 0a 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74  ..      /* Creat
30008 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  e the expression
30009 20 22 4f 4c 44 2e 7a 54 6f 43 6f 6c 20 3d 20 7a   "OLD.zToCol = z
3000a 46 72 6f 6d 43 6f 6c 22 2e 20 49 74 20 69 73 20  FromCol". It is 
3000b 69 6d 70 6f 72 74 61 6e 74 0a 20 20 20 20 20 20  important.      
3000c 2a 2a 20 74 68 61 74 20 74 68 65 20 22 4f 4c 44  ** that the "OLD
3000d 2e 7a 54 6f 43 6f 6c 22 20 74 65 72 6d 20 69 73  .zToCol" term is
3000e 20 6f 6e 20 74 68 65 20 4c 48 53 20 6f 66 20 74   on the LHS of t
3000f 68 65 20 3d 20 6f 70 65 72 61 74 6f 72 2c 20 73  he = operator, s
30010 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20  o.      ** that 
30011 74 68 65 20 61 66 66 69 6e 69 74 79 20 61 6e 64  the affinity and
30012 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
30013 6e 63 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  nce associated w
30014 69 74 68 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  ith the.      **
30015 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 61 72   parent table ar
30016 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 63  e used for the c
30017 6f 6d 70 61 72 69 73 6f 6e 2e 20 2a 2f 0a 20 20  omparison. */.  
30018 20 20 20 20 70 45 71 20 3d 20 73 71 6c 69 74 65      pEq = sqlite
30019 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
3001a 4b 5f 45 51 2c 0a 20 20 20 20 20 20 20 20 20 20  K_EQ,.          
3001b 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
3001c 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 0a 20 20  rse, TK_DOT, .  
3001d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3001e 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
3001f 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 4f 6c  K_ID, 0, 0, &tOl
30020 64 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  d),.            
30021 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
30022 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
30023 2c 20 26 74 54 6f 43 6f 6c 29 0a 20 20 20 20 20  , &tToCol).     
30024 20 20 20 20 20 2c 20 30 29 2c 0a 20 20 20 20 20       , 0),.     
30025 20 20 20 20 20 73 71 6c 69 74 65 33 50 45 78 70       sqlite3PExp
30026 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
30027 20 30 2c 20 30 2c 20 26 74 46 72 6f 6d 43 6f 6c   0, 0, &tFromCol
30028 29 0a 20 20 20 20 20 20 2c 20 30 29 3b 0a 20 20  ).      , 0);.  
30029 20 20 20 20 70 57 68 65 72 65 20 3d 20 73 71 6c      pWhere = sql
3002a 69 74 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20  ite3ExprAnd(db, 
3002b 70 57 68 65 72 65 2c 20 70 45 71 29 3b 0a 0a 20  pWhere, pEq);.. 
3002c 20 20 20 20 20 2f 2a 20 46 6f 72 20 4f 4e 20 55       /* For ON U
3002d 50 44 41 54 45 2c 20 63 6f 6e 73 74 72 75 63 74  PDATE, construct
3002e 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20 6f   the next term o
3002f 66 20 74 68 65 20 57 48 45 4e 20 63 6c 61 75 73  f the WHEN claus
30030 65 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20  e..      ** The 
30031 66 69 6e 61 6c 20 57 48 45 4e 20 63 6c 61 75 73  final WHEN claus
30032 65 20 77 69 6c 6c 20 62 65 20 6c 69 6b 65 20 74  e will be like t
30033 68 69 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20  his:.      **.  
30034 20 20 20 20 2a 2a 20 20 20 20 57 48 45 4e 20 4e      **    WHEN N
30035 4f 54 28 6f 6c 64 2e 63 6f 6c 31 20 49 53 20 6e  OT(old.col1 IS n
30036 65 77 2e 63 6f 6c 31 20 41 4e 44 20 2e 2e 2e 20  ew.col1 AND ... 
30037 41 4e 44 20 6f 6c 64 2e 63 6f 6c 4e 20 49 53 20  AND old.colN IS 
30038 6e 65 77 2e 63 6f 6c 4e 29 0a 20 20 20 20 20 20  new.colN).      
30039 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43 68  */.      if( pCh
3003a 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 20 20  anges ){.       
3003b 20 70 45 71 20 3d 20 73 71 6c 69 74 65 33 50 45   pEq = sqlite3PE
3003c 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
3003d 53 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  S,.            s
3003e 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
3003f 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 0a 20 20 20  se, TK_DOT, .   
30040 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
30041 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
30042 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 4f  TK_ID, 0, 0, &tO
30043 6c 64 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ld),.           
30044 20 20 20 73 71 6c 69 74 65 33 50 45 78 70 72 28     sqlite3PExpr(
30045 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30  pParse, TK_ID, 0
30046 2c 20 30 2c 20 26 74 54 6f 43 6f 6c 29 2c 0a 20  , 0, &tToCol),. 
30047 20 20 20 20 20 20 20 20 20 20 20 20 20 30 29 2c               0),
30048 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
30049 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
3004a 2c 20 54 4b 5f 44 4f 54 2c 20 0a 20 20 20 20 20  , TK_DOT, .     
3004b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
3004c 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
3004d 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 4e 65 77  _ID, 0, 0, &tNew
3004e 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
3004f 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
30050 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20  arse, TK_ID, 0, 
30051 30 2c 20 26 74 54 6f 43 6f 6c 29 2c 0a 20 20 20  0, &tToCol),.   
30052 20 20 20 20 20 20 20 20 20 20 20 30 29 2c 0a 20             0),. 
30053 20 20 20 20 20 20 20 20 20 20 20 30 29 3b 0a 20             0);. 
30054 20 20 20 20 20 20 20 70 57 68 65 6e 20 3d 20 73         pWhen = s
30055 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64 62  qlite3ExprAnd(db
30056 2c 20 70 57 68 65 6e 2c 20 70 45 71 29 3b 0a 20  , pWhen, pEq);. 
30057 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20       }.  .      
30058 69 66 28 20 61 63 74 69 6f 6e 21 3d 4f 45 5f 52  if( action!=OE_R
30059 65 73 74 72 69 63 74 20 26 26 20 28 61 63 74 69  estrict && (acti
3005a 6f 6e 21 3d 4f 45 5f 43 61 73 63 61 64 65 20 7c  on!=OE_Cascade |
3005b 7c 20 70 43 68 61 6e 67 65 73 29 20 29 7b 0a 20  | pChanges) ){. 
3005c 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65         Expr *pNe
3005d 77 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61  w;.        if( a
3005e 63 74 69 6f 6e 3d 3d 4f 45 5f 43 61 73 63 61 64  ction==OE_Cascad
3005f 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  e ){.          p
30060 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 50 45 78  New = sqlite3PEx
30061 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f  pr(pParse, TK_DO
30062 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  T, .            
30063 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
30064 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
30065 2c 20 26 74 4e 65 77 29 2c 0a 20 20 20 20 20 20  , &tNew),.      
30066 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45 78        sqlite3PEx
30067 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44  pr(pParse, TK_ID
30068 2c 20 30 2c 20 30 2c 20 26 74 54 6f 43 6f 6c 29  , 0, 0, &tToCol)
30069 0a 20 20 20 20 20 20 20 20 20 20 2c 20 30 29 3b  .          , 0);
3006a 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
3006b 66 28 20 61 63 74 69 6f 6e 3d 3d 4f 45 5f 53 65  f( action==OE_Se
3006c 74 44 66 6c 74 20 29 7b 0a 20 20 20 20 20 20 20  tDflt ){.       
3006d 20 20 20 45 78 70 72 20 2a 70 44 66 6c 74 20 3d     Expr *pDflt =
3006e 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d 3e 61   pFKey->pFrom->a
3006f 43 6f 6c 5b 69 46 72 6f 6d 43 6f 6c 5d 2e 70 44  Col[iFromCol].pD
30070 66 6c 74 3b 0a 20 20 20 20 20 20 20 20 20 20 69  flt;.          i
30071 66 28 20 70 44 66 6c 74 20 29 7b 0a 20 20 20 20  f( pDflt ){.    
30072 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73          pNew = s
30073 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
30074 2c 20 70 44 66 6c 74 2c 20 30 29 3b 0a 20 20 20  , pDflt, 0);.   
30075 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
30076 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d            pNew =
30077 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
30078 61 72 73 65 2c 20 54 4b 5f 4e 55 4c 4c 2c 20 30  arse, TK_NULL, 0
30079 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 0, 0);.       
3007a 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c     }.        }el
3007b 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e  se{.          pN
3007c 65 77 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  ew = sqlite3PExp
3007d 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 55 4c  r(pParse, TK_NUL
3007e 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20  L, 0, 0, 0);.   
3007f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
30080 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
30081 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
30082 72 73 65 2c 20 70 4c 69 73 74 2c 20 70 4e 65 77  rse, pList, pNew
30083 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
30084 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d  e3ExprListSetNam
30085 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c  e(pParse, pList,
30086 20 26 74 46 72 6f 6d 43 6f 6c 2c 20 30 29 3b 0a   &tFromCol, 0);.
30087 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
30088 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
30089 64 62 2c 20 61 69 43 6f 6c 29 3b 0a 0a 20 20 20  db, aiCol);..   
3008a 20 7a 46 72 6f 6d 20 3d 20 70 46 4b 65 79 2d 3e   zFrom = pFKey->
3008b 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  pFrom->zName;.  
3008c 20 20 6e 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65    nFrom = sqlite
3008d 33 53 74 72 6c 65 6e 33 30 28 7a 46 72 6f 6d 29  3Strlen30(zFrom)
3008e 3b 0a 0a 20 20 20 20 69 66 28 20 61 63 74 69 6f  ;..    if( actio
3008f 6e 3d 3d 4f 45 5f 52 65 73 74 72 69 63 74 20 29  n==OE_Restrict )
30090 7b 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20 74 46  {.      Token tF
30091 72 6f 6d 3b 0a 20 20 20 20 20 20 45 78 70 72 20  rom;.      Expr 
30092 2a 70 52 61 69 73 65 3b 20 0a 0a 20 20 20 20 20  *pRaise; ..     
30093 20 74 46 72 6f 6d 2e 7a 20 3d 20 7a 46 72 6f 6d   tFrom.z = zFrom
30094 3b 0a 20 20 20 20 20 20 74 46 72 6f 6d 2e 6e 20  ;.      tFrom.n 
30095 3d 20 6e 46 72 6f 6d 3b 0a 20 20 20 20 20 20 70  = nFrom;.      p
30096 52 61 69 73 65 20 3d 20 73 71 6c 69 74 65 33 45  Raise = sqlite3E
30097 78 70 72 28 64 62 2c 20 54 4b 5f 52 41 49 53 45  xpr(db, TK_RAISE
30098 2c 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  , "foreign key c
30099 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
3009a 22 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52  ");.      if( pR
3009b 61 69 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20  aise ){.        
3009c 70 52 61 69 73 65 2d 3e 61 66 66 69 6e 69 74 79  pRaise->affinity
3009d 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   = OE_Abort;.   
3009e 20 20 20 7d 0a 20 20 20 20 20 20 70 53 65 6c 65     }.      pSele
3009f 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  ct = sqlite3Sele
300a0 63 74 4e 65 77 28 70 50 61 72 73 65 2c 20 0a 20  ctNew(pParse, . 
300a1 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
300a2 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
300a3 50 61 72 73 65 2c 20 30 2c 20 70 52 61 69 73 65  Parse, 0, pRaise
300a4 29 2c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ),.          sql
300a5 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
300a6 64 28 64 62 2c 20 30 2c 20 26 74 46 72 6f 6d 2c  d(db, 0, &tFrom,
300a7 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 70   0),.          p
300a8 57 68 65 72 65 2c 0a 20 20 20 20 20 20 20 20 20  Where,.         
300a9 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
300aa 30 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20  0.      );.     
300ab 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 20   pWhere = 0;.   
300ac 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 73 61 62   }..    /* Disab
300ad 6c 65 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d  le lookaside mem
300ae 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a  ory allocation *
300af 2f 0a 20 20 20 20 65 6e 61 62 6c 65 4c 6f 6f 6b  /.    enableLook
300b0 61 73 69 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b  aside = db->look
300b1 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 3b 0a  aside.bEnabled;.
300b2 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
300b3 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a  e.bEnabled = 0;.
300b4 0a 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20  .    pTrigger = 
300b5 28 54 72 69 67 67 65 72 20 2a 29 73 71 6c 69 74  (Trigger *)sqlit
300b6 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
300b7 62 2c 20 0a 20 20 20 20 20 20 20 20 73 69 7a 65  b, .        size
300b8 6f 66 28 54 72 69 67 67 65 72 29 20 2b 20 20 20  of(Trigger) +   
300b9 20 20 20 20 20 20 2f 2a 20 73 74 72 75 63 74 20        /* struct 
300ba 54 72 69 67 67 65 72 20 2a 2f 0a 20 20 20 20 20  Trigger */.     
300bb 20 20 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65     sizeof(Trigge
300bc 72 53 74 65 70 29 20 2b 20 20 20 20 20 2f 2a 20  rStep) +     /* 
300bd 53 69 6e 67 6c 65 20 73 74 65 70 20 69 6e 20 74  Single step in t
300be 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a  rigger program *
300bf 2f 0a 20 20 20 20 20 20 20 20 6e 46 72 6f 6d 20  /.        nFrom 
300c0 2b 20 31 20 20 20 20 20 20 20 20 20 20 20 20 20  + 1             
300c1 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72      /* Space for
300c2 20 70 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a   pStep->target.z
300c3 20 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69   */.    );.    i
300c4 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20  f( pTrigger ){. 
300c5 20 20 20 20 20 70 53 74 65 70 20 3d 20 70 54 72       pStep = pTr
300c6 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73 74  igger->step_list
300c7 20 3d 20 28 54 72 69 67 67 65 72 53 74 65 70 20   = (TriggerStep 
300c8 2a 29 26 70 54 72 69 67 67 65 72 5b 31 5d 3b 0a  *)&pTrigger[1];.
300c9 20 20 20 20 20 20 70 53 74 65 70 2d 3e 74 61 72        pStep->tar
300ca 67 65 74 2e 7a 20 3d 20 28 63 68 61 72 20 2a 29  get.z = (char *)
300cb 26 70 53 74 65 70 5b 31 5d 3b 0a 20 20 20 20 20  &pStep[1];.     
300cc 20 70 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 6e   pStep->target.n
300cd 20 3d 20 6e 46 72 6f 6d 3b 0a 20 20 20 20 20 20   = nFrom;.      
300ce 6d 65 6d 63 70 79 28 28 63 68 61 72 20 2a 29 70  memcpy((char *)p
300cf 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a 2c 20  Step->target.z, 
300d0 7a 46 72 6f 6d 2c 20 6e 46 72 6f 6d 29 3b 0a 20  zFrom, nFrom);. 
300d1 20 0a 20 20 20 20 20 20 70 53 74 65 70 2d 3e 70   .      pStep->p
300d2 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
300d3 78 70 72 44 75 70 28 64 62 2c 20 70 57 68 65 72  xprDup(db, pWher
300d4 65 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43  e, EXPRDUP_REDUC
300d5 45 29 3b 0a 20 20 20 20 20 20 70 53 74 65 70 2d  E);.      pStep-
300d6 3e 70 45 78 70 72 4c 69 73 74 20 3d 20 73 71 6c  >pExprList = sql
300d7 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
300d8 64 62 2c 20 70 4c 69 73 74 2c 20 45 58 50 52 44  db, pList, EXPRD
300d9 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20  UP_REDUCE);.    
300da 20 20 70 53 74 65 70 2d 3e 70 53 65 6c 65 63 74    pStep->pSelect
300db 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
300dc 44 75 70 28 64 62 2c 20 70 53 65 6c 65 63 74 2c  Dup(db, pSelect,
300dd 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29   EXPRDUP_REDUCE)
300de 3b 0a 20 20 20 20 20 20 69 66 28 20 70 57 68 65  ;.      if( pWhe
300df 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 70 57 68  n ){.        pWh
300e0 65 6e 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  en = sqlite3PExp
300e1 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54  r(pParse, TK_NOT
300e2 2c 20 70 57 68 65 6e 2c 20 30 2c 20 30 29 3b 0a  , pWhen, 0, 0);.
300e3 20 20 20 20 20 20 20 20 70 54 72 69 67 67 65 72          pTrigger
300e4 2d 3e 70 57 68 65 6e 20 3d 20 73 71 6c 69 74 65  ->pWhen = sqlite
300e5 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 57 68  3ExprDup(db, pWh
300e6 65 6e 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55  en, EXPRDUP_REDU
300e7 43 45 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  CE);.      }.   
300e8 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 2d 65 6e   }..    /* Re-en
300e9 61 62 6c 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69  able the lookasi
300ea 64 65 20 62 75 66 66 65 72 2c 20 69 66 20 69 74  de buffer, if it
300eb 20 77 61 73 20 64 69 73 61 62 6c 65 64 20 65 61   was disabled ea
300ec 72 6c 69 65 72 2e 20 2a 2f 0a 20 20 20 20 64 62  rlier. */.    db
300ed 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
300ee 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 4c 6f 6f  bled = enableLoo
300ef 6b 61 73 69 64 65 3b 0a 0a 20 20 20 20 73 71 6c  kaside;..    sql
300f0 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
300f1 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 20 20  b, pWhere);.    
300f2 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
300f3 65 28 64 62 2c 20 70 57 68 65 6e 29 3b 0a 20 20  e(db, pWhen);.  
300f4 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
300f5 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73  tDelete(db, pLis
300f6 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  t);.    sqlite3S
300f7 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
300f8 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 69 66  pSelect);.    if
300f9 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
300fa 65 64 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 66  ed==1 ){.      f
300fb 6b 54 72 69 67 67 65 72 44 65 6c 65 74 65 28 64  kTriggerDelete(d
300fc 62 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20  b, pTrigger);.  
300fd 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
300fe 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
300ff 70 53 74 65 70 21 3d 30 20 29 3b 0a 0a 20 20 20  pStep!=0 );..   
30100 20 73 77 69 74 63 68 28 20 61 63 74 69 6f 6e 20   switch( action 
30101 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45  ){.      case OE
30102 5f 52 65 73 74 72 69 63 74 3a 0a 20 20 20 20 20  _Restrict:.     
30103 20 20 20 70 53 74 65 70 2d 3e 6f 70 20 3d 20 54     pStep->op = T
30104 4b 5f 53 45 4c 45 43 54 3b 20 0a 20 20 20 20 20  K_SELECT; .     
30105 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30106 63 61 73 65 20 4f 45 5f 43 61 73 63 61 64 65 3a  case OE_Cascade:
30107 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70   .        if( !p
30108 43 68 61 6e 67 65 73 20 29 7b 20 0a 20 20 20 20  Changes ){ .    
30109 20 20 20 20 20 20 70 53 74 65 70 2d 3e 6f 70 20        pStep->op 
3010a 3d 20 54 4b 5f 44 45 4c 45 54 45 3b 20 0a 20 20  = TK_DELETE; .  
3010b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 20 0a          break; .
3010c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3010d 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20  default:.       
3010e 20 70 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f   pStep->op = TK_
3010f 55 50 44 41 54 45 3b 0a 20 20 20 20 7d 0a 20 20  UPDATE;.    }.  
30110 20 20 70 53 74 65 70 2d 3e 70 54 72 69 67 20 3d    pStep->pTrig =
30111 20 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20 70   pTrigger;.    p
30112 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61  Trigger->pSchema
30113 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61   = pTab->pSchema
30114 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e  ;.    pTrigger->
30115 70 54 61 62 53 63 68 65 6d 61 20 3d 20 70 54 61  pTabSchema = pTa
30116 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20  b->pSchema;.    
30117 70 46 4b 65 79 2d 3e 61 70 54 72 69 67 67 65 72  pFKey->apTrigger
30118 5b 69 41 63 74 69 6f 6e 5d 20 3d 20 70 54 72 69  [iAction] = pTri
30119 67 67 65 72 3b 0a 20 20 20 20 70 54 72 69 67 67  gger;.    pTrigg
3011a 65 72 2d 3e 6f 70 20 3d 20 28 70 43 68 61 6e 67  er->op = (pChang
3011b 65 73 20 3f 20 54 4b 5f 55 50 44 41 54 45 20 3a  es ? TK_UPDATE :
3011c 20 54 4b 5f 44 45 4c 45 54 45 29 3b 0a 20 20 7d   TK_DELETE);.  }
3011d 0a 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67  ..  return pTrig
3011e 67 65 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ger;.}../*.** Th
3011f 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
30120 61 6c 6c 65 64 20 77 68 65 6e 20 64 65 6c 65 74  alled when delet
30121 69 6e 67 20 6f 72 20 75 70 64 61 74 69 6e 67 20  ing or updating 
30122 61 20 72 6f 77 20 74 6f 20 69 6d 70 6c 65 6d 65  a row to impleme
30123 6e 74 0a 2a 2a 20 61 6e 79 20 72 65 71 75 69 72  nt.** any requir
30124 65 64 20 43 41 53 43 41 44 45 2c 20 53 45 54 20  ed CASCADE, SET 
30125 4e 55 4c 4c 20 6f 72 20 53 45 54 20 44 45 46 41  NULL or SET DEFA
30126 55 4c 54 20 61 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  ULT actions..*/.
30127 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
30128 6f 69 64 20 73 71 6c 69 74 65 33 46 6b 41 63 74  oid sqlite3FkAct
30129 69 6f 6e 73 28 0a 20 20 50 61 72 73 65 20 2a 70  ions(.  Parse *p
3012a 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
3012b 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
3012c 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61   context */.  Ta
3012d 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
3012e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3012f 20 54 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64   Table being upd
30130 61 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 20  ated or deleted 
30131 66 72 6f 6d 20 2a 2f 0a 20 20 45 78 70 72 4c 69  from */.  ExprLi
30132 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 20  st *pChanges,   
30133 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61            /* Cha
30134 6e 67 65 2d 6c 69 73 74 20 66 6f 72 20 55 50 44  nge-list for UPD
30135 41 54 45 2c 20 4e 55 4c 4c 20 66 6f 72 20 44 45  ATE, NULL for DE
30136 4c 45 54 45 20 2a 2f 0a 20 20 69 6e 74 20 72 65  LETE */.  int re
30137 67 4f 6c 64 20 20 20 20 20 20 20 20 20 20 20 20  gOld            
30138 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
30139 72 65 73 73 20 6f 66 20 61 72 72 61 79 20 63 6f  ress of array co
3013a 6e 74 61 69 6e 69 6e 67 20 6f 6c 64 20 72 6f 77  ntaining old row
3013b 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 49 66 20 66   */.){.  /* If f
3013c 6f 72 65 69 67 6e 2d 6b 65 79 20 73 75 70 70 6f  oreign-key suppo
3013d 72 74 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 69  rt is enabled, i
3013e 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61  terate through a
3013f 6c 6c 20 46 4b 73 20 74 68 61 74 20 0a 20 20 2a  ll FKs that .  *
30140 2a 20 72 65 66 65 72 20 74 6f 20 74 61 62 6c 65  * refer to table
30141 20 70 54 61 62 2e 20 49 66 20 74 68 65 72 65 20   pTab. If there 
30142 69 73 20 61 6e 20 61 63 74 69 6f 6e 20 61 73 73  is an action ass
30143 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
30144 20 46 4b 20 0a 20 20 2a 2a 20 66 6f 72 20 74 68   FK .  ** for th
30145 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 28 65 69  is operation (ei
30146 74 68 65 72 20 75 70 64 61 74 65 20 6f 72 20 64  ther update or d
30147 65 6c 65 74 65 29 2c 20 69 6e 76 6f 6b 65 20 74  elete), invoke t
30148 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a 20  he associated . 
30149 20 2a 2a 20 74 72 69 67 67 65 72 20 73 75 62 2d   ** trigger sub-
3014a 70 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 69  program.  */.  i
3014b 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66  f( pParse->db->f
3014c 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65  lags&SQLITE_Fore
3014d 69 67 6e 4b 65 79 73 20 29 7b 0a 20 20 20 20 46  ignKeys ){.    F
3014e 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 20  Key *pFKey;     
3014f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
30150 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c  Iterator variabl
30151 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 46 4b  e */.    for(pFK
30152 65 79 20 3d 20 73 71 6c 69 74 65 33 46 6b 52 65  ey = sqlite3FkRe
30153 66 65 72 65 6e 63 65 73 28 70 54 61 62 29 3b 20  ferences(pTab); 
30154 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70 46 4b  pFKey; pFKey=pFK
30155 65 79 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a 20 20  ey->pNextTo){.  
30156 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 41 63      Trigger *pAc
30157 74 69 6f 6e 20 3d 20 66 6b 41 63 74 69 6f 6e 54  tion = fkActionT
30158 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70  rigger(pParse, p
30159 54 61 62 2c 20 70 46 4b 65 79 2c 20 70 43 68 61  Tab, pFKey, pCha
3015a 6e 67 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28  nges);.      if(
3015b 20 70 41 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20   pAction ){.    
3015c 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52      sqlite3CodeR
3015d 6f 77 54 72 69 67 67 65 72 44 69 72 65 63 74 28  owTriggerDirect(
3015e 70 50 61 72 73 65 2c 20 70 41 63 74 69 6f 6e 2c  pParse, pAction,
3015f 20 70 54 61 62 2c 20 72 65 67 4f 6c 64 2c 20 4f   pTab, regOld, O
30160 45 5f 41 62 6f 72 74 2c 20 30 29 3b 0a 20 20 20  E_Abort, 0);.   
30161 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d     }.    }.  }.}
30162 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64  ..#endif /* ifnd
30163 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
30164 52 49 47 47 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  RIGGER */../*.**
30165 20 46 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79   Free all memory
30166 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
30167 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66   foreign key def
30168 69 6e 69 74 69 6f 6e 73 20 61 74 74 61 63 68 65  initions attache
30169 64 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 20 70 54  d to.** table pT
3016a 61 62 2e 20 52 65 6d 6f 76 65 20 74 68 65 20 64  ab. Remove the d
3016b 65 6c 65 74 65 64 20 66 6f 72 65 69 67 6e 20 6b  eleted foreign k
3016c 65 79 73 20 66 72 6f 6d 20 74 68 65 20 53 63 68  eys from the Sch
3016d 65 6d 61 2e 66 6b 65 79 48 61 73 68 0a 2a 2a 20  ema.fkeyHash.** 
3016e 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53  hash table..*/.S
3016f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
30170 69 64 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65  id sqlite3FkDele
30171 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  te(sqlite3 *db, 
30172 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20  Table *pTab){.  
30173 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 20  FKey *pFKey;    
30174 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30175 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
30176 61 62 6c 65 20 2a 2f 0a 20 20 46 4b 65 79 20 2a  able */.  FKey *
30177 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  pNext;          
30178 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70            /* Cop
30179 79 20 6f 66 20 70 46 4b 65 79 2d 3e 70 4e 65 78  y of pFKey->pNex
3017a 74 46 72 6f 6d 20 2a 2f 0a 0a 20 20 61 73 73 65  tFrom */..  asse
3017b 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  rt( db==0 || sql
3017c 69 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48  ite3SchemaMutexH
3017d 65 6c 64 28 64 62 2c 20 30 2c 20 70 54 61 62 2d  eld(db, 0, pTab-
3017e 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 20 66  >pSchema) );.  f
3017f 6f 72 28 70 46 4b 65 79 3d 70 54 61 62 2d 3e 70  or(pFKey=pTab->p
30180 46 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46 4b  FKey; pFKey; pFK
30181 65 79 3d 70 4e 65 78 74 29 7b 0a 0a 20 20 20 20  ey=pNext){..    
30182 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 46 4b  /* Remove the FK
30183 20 66 72 6f 6d 20 74 68 65 20 66 6b 65 79 48 61   from the fkeyHa
30184 73 68 20 68 61 73 68 20 74 61 62 6c 65 2e 20 2a  sh hash table. *
30185 2f 0a 20 20 20 20 69 66 28 20 21 64 62 20 7c 7c  /.    if( !db ||
30186 20 64 62 2d 3e 70 6e 42 79 74 65 73 46 72 65 65   db->pnBytesFree
30187 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  d==0 ){.      if
30188 28 20 70 46 4b 65 79 2d 3e 70 50 72 65 76 54 6f  ( pFKey->pPrevTo
30189 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 4b 65   ){.        pFKe
3018a 79 2d 3e 70 50 72 65 76 54 6f 2d 3e 70 4e 65 78  y->pPrevTo->pNex
3018b 74 54 6f 20 3d 20 70 46 4b 65 79 2d 3e 70 4e 65  tTo = pFKey->pNe
3018c 78 74 54 6f 3b 0a 20 20 20 20 20 20 7d 65 6c 73  xtTo;.      }els
3018d 65 7b 0a 20 20 20 20 20 20 20 20 76 6f 69 64 20  e{.        void 
3018e 2a 70 20 3d 20 28 76 6f 69 64 20 2a 29 70 46 4b  *p = (void *)pFK
3018f 65 79 2d 3e 70 4e 65 78 74 54 6f 3b 0a 20 20 20  ey->pNextTo;.   
30190 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
30191 2a 7a 20 3d 20 28 70 20 3f 20 70 46 4b 65 79 2d  *z = (p ? pFKey-
30192 3e 70 4e 65 78 74 54 6f 2d 3e 7a 54 6f 20 3a 20  >pNextTo->zTo : 
30193 70 46 4b 65 79 2d 3e 7a 54 6f 29 3b 0a 20 20 20  pFKey->zTo);.   
30194 20 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68       sqlite3Hash
30195 49 6e 73 65 72 74 28 26 70 54 61 62 2d 3e 70 53  Insert(&pTab->pS
30196 63 68 65 6d 61 2d 3e 66 6b 65 79 48 61 73 68 2c  chema->fkeyHash,
30197 20 7a 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65   z, sqlite3Strle
30198 6e 33 30 28 7a 29 2c 20 70 29 3b 0a 20 20 20 20  n30(z), p);.    
30199 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 46    }.      if( pF
3019a 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 29 7b 0a  Key->pNextTo ){.
3019b 20 20 20 20 20 20 20 20 70 46 4b 65 79 2d 3e 70          pFKey->p
3019c 4e 65 78 74 54 6f 2d 3e 70 50 72 65 76 54 6f 20  NextTo->pPrevTo 
3019d 3d 20 70 46 4b 65 79 2d 3e 70 50 72 65 76 54 6f  = pFKey->pPrevTo
3019e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
3019f 0a 20 20 20 20 2f 2a 20 45 56 3a 20 52 2d 33 30  .    /* EV: R-30
301a0 33 32 33 2d 32 31 39 31 37 20 45 61 63 68 20 66  323-21917 Each f
301a1 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
301a2 72 61 69 6e 74 20 69 6e 20 53 51 4c 69 74 65 20  raint in SQLite 
301a3 69 73 0a 20 20 20 20 2a 2a 20 63 6c 61 73 73 69  is.    ** classi
301a4 66 69 65 64 20 61 73 20 65 69 74 68 65 72 20 69  fied as either i
301a5 6d 6d 65 64 69 61 74 65 20 6f 72 20 64 65 66 65  mmediate or defe
301a6 72 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  rred..    */.   
301a7 20 61 73 73 65 72 74 28 20 70 46 4b 65 79 2d 3e   assert( pFKey->
301a8 69 73 44 65 66 65 72 72 65 64 3d 3d 30 20 7c 7c  isDeferred==0 ||
301a9 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72   pFKey->isDeferr
301aa 65 64 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 2f 2a  ed==1 );..    /*
301ab 20 44 65 6c 65 74 65 20 61 6e 79 20 74 72 69 67   Delete any trig
301ac 67 65 72 73 20 63 72 65 61 74 65 64 20 74 6f 20  gers created to 
301ad 69 6d 70 6c 65 6d 65 6e 74 20 61 63 74 69 6f 6e  implement action
301ae 73 20 66 6f 72 20 74 68 69 73 20 46 4b 2e 20 2a  s for this FK. *
301af 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
301b0 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
301b1 20 20 66 6b 54 72 69 67 67 65 72 44 65 6c 65 74    fkTriggerDelet
301b2 65 28 64 62 2c 20 70 46 4b 65 79 2d 3e 61 70 54  e(db, pFKey->apT
301b3 72 69 67 67 65 72 5b 30 5d 29 3b 0a 20 20 20 20  rigger[0]);.    
301b4 66 6b 54 72 69 67 67 65 72 44 65 6c 65 74 65 28  fkTriggerDelete(
301b5 64 62 2c 20 70 46 4b 65 79 2d 3e 61 70 54 72 69  db, pFKey->apTri
301b6 67 67 65 72 5b 31 5d 29 3b 0a 23 65 6e 64 69 66  gger[1]);.#endif
301b7 0a 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 46  ..    pNext = pF
301b8 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 3b 0a  Key->pNextFrom;.
301b9 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
301ba 65 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a 20 20  e(db, pFKey);.  
301bb 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66  }.}.#endif /* if
301bc 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
301bd 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 20 2a 2f 0a  _FOREIGN_KEY */.
301be 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
301bf 20 45 6e 64 20 6f 66 20 66 6b 65 79 2e 63 20 2a   End of fkey.c *
301c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
301c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
301c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
301c3 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
301c4 20 42 65 67 69 6e 20 66 69 6c 65 20 69 6e 73 65   Begin file inse
301c5 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  rt.c ***********
301c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
301c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
301c8 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
301c9 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
301ca 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
301cb 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
301cc 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
301cd 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
301ce 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
301cf 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
301d0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
301d1 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
301d2 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
301d3 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
301d4 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
301d5 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
301d6 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
301d7 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
301d8 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
301d9 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
301da 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
301db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
301dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
301dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
301de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
301df 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
301e0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20  file contains C 
301e1 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68  code routines th
301e2 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79  at are called by
301e3 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 74   the parser.** t
301e4 6f 20 68 61 6e 64 6c 65 20 49 4e 53 45 52 54 20  o handle INSERT 
301e5 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 53 51  statements in SQ
301e6 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Lite..*/../*.** 
301e7 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
301e8 61 74 20 77 69 6c 6c 20 6f 70 65 6e 20 61 20 74  at will open a t
301e9 61 62 6c 65 20 66 6f 72 20 72 65 61 64 69 6e 67  able for reading
301ea 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
301eb 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
301ec 4f 70 65 6e 54 61 62 6c 65 28 0a 20 20 50 61 72  OpenTable(.  Par
301ed 73 65 20 2a 70 2c 20 20 20 20 20 20 20 2f 2a 20  se *p,       /* 
301ee 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e  Generate code in
301ef 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f 0a  to this VDBE */.
301f0 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20    int iCur,     
301f1 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
301f2 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 74 61  number of the ta
301f3 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ble */.  int iDb
301f4 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
301f5 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 69  database index i
301f6 6e 20 73 71 6c 69 74 65 33 2e 61 44 62 5b 5d 20  n sqlite3.aDb[] 
301f7 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
301f8 2c 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c  ,    /* The tabl
301f9 65 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a  e to be opened *
301fa 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64 65 20 20  /.  int opcode  
301fb 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e 52 65      /* OP_OpenRe
301fc 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e 57 72 69  ad or OP_OpenWri
301fd 74 65 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20  te */.){.  Vdbe 
301fe 2a 76 3b 0a 20 20 61 73 73 65 72 74 28 20 21 49  *v;.  assert( !I
301ff 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
30200 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ;.  v = sqlite3G
30201 65 74 56 64 62 65 28 70 29 3b 0a 20 20 61 73 73  etVdbe(p);.  ass
30202 65 72 74 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f  ert( opcode==OP_
30203 4f 70 65 6e 57 72 69 74 65 20 7c 7c 20 6f 70 63  OpenWrite || opc
30204 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 52 65 61 64  ode==OP_OpenRead
30205 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62   );.  sqlite3Tab
30206 6c 65 4c 6f 63 6b 28 70 2c 20 69 44 62 2c 20 70  leLock(p, iDb, p
30207 54 61 62 2d 3e 74 6e 75 6d 2c 20 28 6f 70 63 6f  Tab->tnum, (opco
30208 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65  de==OP_OpenWrite
30209 29 3f 31 3a 30 2c 20 70 54 61 62 2d 3e 7a 4e 61  )?1:0, pTab->zNa
3020a 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  me);.  sqlite3Vd
3020b 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 63 6f  beAddOp3(v, opco
3020c 64 65 2c 20 69 43 75 72 2c 20 70 54 61 62 2d 3e  de, iCur, pTab->
3020d 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 73 71  tnum, iDb);.  sq
3020e 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
3020f 34 28 76 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  4(v, -1, SQLITE_
30210 49 4e 54 5f 54 4f 5f 50 54 52 28 70 54 61 62 2d  INT_TO_PTR(pTab-
30211 3e 6e 43 6f 6c 29 2c 20 50 34 5f 49 4e 54 33 32  >nCol), P4_INT32
30212 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  );.  VdbeComment
30213 28 28 76 2c 20 22 25 73 22 2c 20 70 54 61 62 2d  ((v, "%s", pTab-
30214 3e 7a 4e 61 6d 65 29 29 3b 0a 7d 0a 0a 2f 2a 0a  >zName));.}../*.
30215 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
30216 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d  ter to the colum
30217 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e  n affinity strin
30218 67 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  g associated wit
30219 68 20 69 6e 64 65 78 0a 2a 2a 20 70 49 64 78 2e  h index.** pIdx.
3021a 20 41 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69   A column affini
3021b 74 79 20 73 74 72 69 6e 67 20 68 61 73 20 6f 6e  ty string has on
3021c 65 20 63 68 61 72 61 63 74 65 72 20 66 6f 72 20  e character for 
3021d 65 61 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 0a  each column in .
3021e 2a 2a 20 74 68 65 20 74 61 62 6c 65 2c 20 61 63  ** the table, ac
3021f 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 61  cording to the a
30220 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 63  ffinity of the c
30221 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a 20 20 43 68  olumn:.**.**  Ch
30222 61 72 61 63 74 65 72 20 20 20 20 20 20 43 6f 6c  aracter      Col
30223 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20  umn affinity.** 
30224 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
30225 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
30226 2a 2a 20 20 27 61 27 20 20 20 20 20 20 20 20 20  **  'a'         
30227 20 20 20 54 45 58 54 0a 2a 2a 20 20 27 62 27 20     TEXT.**  'b' 
30228 20 20 20 20 20 20 20 20 20 20 20 4e 4f 4e 45 0a             NONE.
30229 2a 2a 20 20 27 63 27 20 20 20 20 20 20 20 20 20  **  'c'         
3022a 20 20 20 4e 55 4d 45 52 49 43 0a 2a 2a 20 20 27     NUMERIC.**  '
3022b 64 27 20 20 20 20 20 20 20 20 20 20 20 20 49 4e  d'            IN
3022c 54 45 47 45 52 0a 2a 2a 20 20 27 65 27 20 20 20  TEGER.**  'e'   
3022d 20 20 20 20 20 20 20 20 20 52 45 41 4c 0a 2a 2a           REAL.**
3022e 0a 2a 2a 20 41 6e 20 65 78 74 72 61 20 27 64 27  .** An extra 'd'
3022f 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20   is appended to 
30230 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 73  the end of the s
30231 74 72 69 6e 67 20 74 6f 20 63 6f 76 65 72 20 74  tring to cover t
30232 68 65 0a 2a 2a 20 72 6f 77 69 64 20 74 68 61 74  he.** rowid that
30233 20 61 70 70 65 61 72 73 20 61 73 20 74 68 65 20   appears as the 
30234 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 69 6e 20 65  last column in e
30235 76 65 72 79 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a  very index..**.*
30236 2a 20 4d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65  * Memory for the
30237 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69   buffer containi
30238 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e  ng the column in
30239 64 65 78 20 61 66 66 69 6e 69 74 79 20 73 74 72  dex affinity str
3023a 69 6e 67 0a 2a 2a 20 69 73 20 6d 61 6e 61 67 65  ing.** is manage
3023b 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65  d along with the
3023c 20 72 65 73 74 20 6f 66 20 74 68 65 20 49 6e 64   rest of the Ind
3023d 65 78 20 73 74 72 75 63 74 75 72 65 2e 20 49 74  ex structure. It
3023e 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 72 65 6c 65   will be.** rele
3023f 61 73 65 64 20 77 68 65 6e 20 73 71 6c 69 74 65  ased when sqlite
30240 33 44 65 6c 65 74 65 49 6e 64 65 78 28 29 20 69  3DeleteIndex() i
30241 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c  s called..*/.SQL
30242 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
30243 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 49  t char *sqlite3I
30244 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28  ndexAffinityStr(
30245 56 64 62 65 20 2a 76 2c 20 49 6e 64 65 78 20 2a  Vdbe *v, Index *
30246 70 49 64 78 29 7b 0a 20 20 69 66 28 20 21 70 49  pIdx){.  if( !pI
30247 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20  dx->zColAff ){. 
30248 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20     /* The first 
30249 74 69 6d 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66  time a column af
3024a 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f  finity string fo
3024b 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69  r a particular i
3024c 6e 64 65 78 20 69 73 0a 20 20 20 20 2a 2a 20 72  ndex is.    ** r
3024d 65 71 75 69 72 65 64 2c 20 69 74 20 69 73 20 61  equired, it is a
3024e 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70  llocated and pop
3024f 75 6c 61 74 65 64 20 68 65 72 65 2e 20 49 74 20  ulated here. It 
30250 69 73 20 74 68 65 6e 20 73 74 6f 72 65 64 20 61  is then stored a
30251 73 0a 20 20 20 20 2a 2a 20 61 20 6d 65 6d 62 65  s.    ** a membe
30252 72 20 6f 66 20 74 68 65 20 49 6e 64 65 78 20 73  r of the Index s
30253 74 72 75 63 74 75 72 65 20 66 6f 72 20 73 75 62  tructure for sub
30254 73 65 71 75 65 6e 74 20 75 73 65 2e 0a 20 20 20  sequent use..   
30255 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63   **.    ** The c
30256 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73  olumn affinity s
30257 74 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74  tring will event
30258 75 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64  ually be deleted
30259 20 62 79 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74   by.    ** sqlit
3025a 65 44 65 6c 65 74 65 49 6e 64 65 78 28 29 20 77  eDeleteIndex() w
3025b 68 65 6e 20 74 68 65 20 49 6e 64 65 78 20 73 74  hen the Index st
3025c 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 20 69  ructure itself i
3025d 73 20 63 6c 65 61 6e 65 64 0a 20 20 20 20 2a 2a  s cleaned.    **
3025e 20 75 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   up..    */.    
3025f 69 6e 74 20 6e 3b 0a 20 20 20 20 54 61 62 6c 65  int n;.    Table
30260 20 2a 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70   *pTab = pIdx->p
30261 54 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74  Table;.    sqlit
30262 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
30263 56 64 62 65 44 62 28 76 29 3b 0a 20 20 20 20 70  VdbeDb(v);.    p
30264 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 28  Idx->zColAff = (
30265 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62  char *)sqlite3Db
30266 4d 61 6c 6c 6f 63 52 61 77 28 30 2c 20 70 49 64  MallocRaw(0, pId
30267 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20  x->nColumn+2);. 
30268 20 20 20 69 66 28 20 21 70 49 64 78 2d 3e 7a 43     if( !pIdx->zC
30269 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 20 20 64  olAff ){.      d
3026a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
3026b 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 1;.      retur
3026c 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  n 0;.    }.    f
3026d 6f 72 28 6e 3d 30 3b 20 6e 3c 70 49 64 78 2d 3e  or(n=0; n<pIdx->
3026e 6e 43 6f 6c 75 6d 6e 3b 20 6e 2b 2b 29 7b 0a 20  nColumn; n++){. 
3026f 20 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41       pIdx->zColA
30270 66 66 5b 6e 5d 20 3d 20 70 54 61 62 2d 3e 61 43  ff[n] = pTab->aC
30271 6f 6c 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d  ol[pIdx->aiColum
30272 6e 5b 6e 5d 5d 2e 61 66 66 69 6e 69 74 79 3b 0a  n[n]].affinity;.
30273 20 20 20 20 7d 0a 20 20 20 20 70 49 64 78 2d 3e      }.    pIdx->
30274 7a 43 6f 6c 41 66 66 5b 6e 2b 2b 5d 20 3d 20 53  zColAff[n++] = S
30275 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
30276 52 3b 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f  R;.    pIdx->zCo
30277 6c 41 66 66 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d  lAff[n] = 0;.  }
30278 0a 20 0a 20 20 72 65 74 75 72 6e 20 70 49 64 78  . .  return pIdx
30279 2d 3e 7a 43 6f 6c 41 66 66 3b 0a 7d 0a 0a 2f 2a  ->zColAff;.}../*
3027a 0a 2a 2a 20 53 65 74 20 50 34 20 6f 66 20 74 68  .** Set P4 of th
3027b 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
3027c 69 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 65 20  inserted opcode 
3027d 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69  to a column affi
3027e 6e 69 74 79 0a 2a 2a 20 73 74 72 69 6e 67 20 66  nity.** string f
3027f 6f 72 20 74 61 62 6c 65 20 70 54 61 62 2e 20 41  or table pTab. A
30280 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
30281 20 73 74 72 69 6e 67 20 68 61 73 20 6f 6e 65 20   string has one 
30282 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 66 6f 72  character.** for
30283 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 64   each column ind
30284 65 78 65 64 20 62 79 20 74 68 65 20 69 6e 64 65  exed by the inde
30285 78 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  x, according to 
30286 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  the affinity of 
30287 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 3a 0a 2a  the.** column:.*
30288 2a 0a 2a 2a 20 20 43 68 61 72 61 63 74 65 72 20  *.**  Character 
30289 20 20 20 20 20 43 6f 6c 75 6d 6e 20 61 66 66 69       Column affi
3028a 6e 69 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d  nity.**  -------
3028b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3028c 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20  -------.**  'a' 
3028d 20 20 20 20 20 20 20 20 20 20 20 54 45 58 54 0a             TEXT.
3028e 2a 2a 20 20 27 62 27 20 20 20 20 20 20 20 20 20  **  'b'         
3028f 20 20 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 20     NONE.**  'c' 
30290 20 20 20 20 20 20 20 20 20 20 20 4e 55 4d 45 52             NUMER
30291 49 43 0a 2a 2a 20 20 27 64 27 20 20 20 20 20 20  IC.**  'd'      
30292 20 20 20 20 20 20 49 4e 54 45 47 45 52 0a 2a 2a        INTEGER.**
30293 20 20 27 65 27 20 20 20 20 20 20 20 20 20 20 20    'e'           
30294 20 52 45 41 4c 0a 2a 2f 0a 53 51 4c 49 54 45 5f   REAL.*/.SQLITE_
30295 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
30296 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74  ite3TableAffinit
30297 79 53 74 72 28 56 64 62 65 20 2a 76 2c 20 54 61  yStr(Vdbe *v, Ta
30298 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 2f 2a  ble *pTab){.  /*
30299 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   The first time 
3029a 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74  a column affinit
3029b 79 20 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70  y string for a p
3029c 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 0a  articular table.
3029d 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64    ** is required
3029e 2c 20 69 74 20 69 73 20 61 6c 6c 6f 63 61 74 65  , it is allocate
3029f 64 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20  d and populated 
302a0 68 65 72 65 2e 20 49 74 20 69 73 20 74 68 65 6e  here. It is then
302a1 20 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 61 73   .  ** stored as
302a2 20 61 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65   a member of the
302a3 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
302a4 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20   for subsequent 
302a5 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  use..  **.  ** T
302a6 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69  he column affini
302a7 74 79 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 65  ty string will e
302a8 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 64 65 6c  ventually be del
302a9 65 74 65 64 20 62 79 0a 20 20 2a 2a 20 73 71 6c  eted by.  ** sql
302aa 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
302ab 29 20 77 68 65 6e 20 74 68 65 20 54 61 62 6c 65  ) when the Table
302ac 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c   structure itsel
302ad 66 20 69 73 20 63 6c 65 61 6e 65 64 20 75 70 2e  f is cleaned up.
302ae 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 54 61  .  */.  if( !pTa
302af 62 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20  b->zColAff ){.  
302b0 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b    char *zColAff;
302b1 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
302b2 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
302b3 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 3b 0a  lite3VdbeDb(v);.
302b4 0a 20 20 20 20 7a 43 6f 6c 41 66 66 20 3d 20 28  .    zColAff = (
302b5 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62  char *)sqlite3Db
302b6 4d 61 6c 6c 6f 63 52 61 77 28 30 2c 20 70 54 61  MallocRaw(0, pTa
302b7 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 20 20 20 20  b->nCol+1);.    
302b8 69 66 28 20 21 7a 43 6f 6c 41 66 66 20 29 7b 0a  if( !zColAff ){.
302b9 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63        db->malloc
302ba 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
302bb 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
302bc 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
302bd 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
302be 7b 0a 20 20 20 20 20 20 7a 43 6f 6c 41 66 66 5b  {.      zColAff[
302bf 69 5d 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  i] = pTab->aCol[
302c0 69 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 20  i].affinity;.   
302c1 20 7d 0a 20 20 20 20 7a 43 6f 6c 41 66 66 5b 70   }.    zColAff[p
302c2 54 61 62 2d 3e 6e 43 6f 6c 5d 20 3d 20 27 5c 30  Tab->nCol] = '\0
302c3 27 3b 0a 0a 20 20 20 20 70 54 61 62 2d 3e 7a 43  ';..    pTab->zC
302c4 6f 6c 41 66 66 20 3d 20 7a 43 6f 6c 41 66 66 3b  olAff = zColAff;
302c5 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 56  .  }..  sqlite3V
302c6 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
302c7 31 2c 20 70 54 61 62 2d 3e 7a 43 6f 6c 41 66 66  1, pTab->zColAff
302c8 2c 20 50 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b  , P4_TRANSIENT);
302c9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
302ca 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65   non-zero if the
302cb 20 74 61 62 6c 65 20 70 54 61 62 20 69 6e 20 64   table pTab in d
302cc 61 74 61 62 61 73 65 20 69 44 62 20 6f 72 20 61  atabase iDb or a
302cd 6e 79 20 6f 66 20 69 74 73 20 69 6e 64 69 63 65  ny of its indice
302ce 73 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f  s.** have been o
302cf 70 65 6e 65 64 20 61 74 20 61 6e 79 20 70 6f 69  pened at any poi
302d0 6e 74 20 69 6e 20 74 68 65 20 56 44 42 45 20 70  nt in the VDBE p
302d1 72 6f 67 72 61 6d 20 62 65 67 69 6e 6e 69 6e 67  rogram beginning
302d2 20 61 74 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20   at location.** 
302d3 69 53 74 61 72 74 41 64 64 72 20 74 68 72 6f 75  iStartAddr throu
302d4 67 68 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ght the end of t
302d5 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 54 68 69  he program.  Thi
302d6 73 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 65  s is used to see
302d7 20 69 66 20 0a 2a 2a 20 61 20 73 74 61 74 65 6d   if .** a statem
302d8 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ent of the form 
302d9 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 69   "INSERT INTO <i
302da 44 62 2c 20 70 54 61 62 3e 20 53 45 4c 45 43 54  Db, pTab> SELECT
302db 20 2e 2e 2e 22 20 63 61 6e 20 0a 2a 2a 20 72 75   ..." can .** ru
302dc 6e 20 77 69 74 68 6f 75 74 20 75 73 69 6e 67 20  n without using 
302dd 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
302de 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 73 20  for the results 
302df 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 20 0a  of the SELECT. .
302e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
302e1 61 64 73 54 61 62 6c 65 28 50 61 72 73 65 20 2a  adsTable(Parse *
302e2 70 2c 20 69 6e 74 20 69 53 74 61 72 74 41 64 64  p, int iStartAdd
302e3 72 2c 20 69 6e 74 20 69 44 62 2c 20 54 61 62 6c  r, int iDb, Tabl
302e4 65 20 2a 70 54 61 62 29 7b 0a 20 20 56 64 62 65  e *pTab){.  Vdbe
302e5 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
302e6 56 64 62 65 28 70 29 3b 0a 20 20 69 6e 74 20 69  Vdbe(p);.  int i
302e7 3b 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d 20 73  ;.  int iEnd = s
302e8 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
302e9 74 41 64 64 72 28 76 29 3b 0a 23 69 66 6e 64 65  tAddr(v);.#ifnde
302ea 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
302eb 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 56 54 61  RTUALTABLE.  VTa
302ec 62 6c 65 20 2a 70 56 54 61 62 20 3d 20 49 73 56  ble *pVTab = IsV
302ed 69 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 73  irtual(pTab) ? s
302ee 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28  qlite3GetVTable(
302ef 70 2d 3e 64 62 2c 20 70 54 61 62 29 20 3a 20 30  p->db, pTab) : 0
302f0 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 66 6f 72 28  ;.#endif..  for(
302f1 69 3d 69 53 74 61 72 74 41 64 64 72 3b 20 69 3c  i=iStartAddr; i<
302f2 69 45 6e 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  iEnd; i++){.    
302f3 56 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20 73 71  VdbeOp *pOp = sq
302f4 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76  lite3VdbeGetOp(v
302f5 2c 20 69 29 3b 0a 20 20 20 20 61 73 73 65 72 74  , i);.    assert
302f6 28 20 70 4f 70 21 3d 30 20 29 3b 0a 20 20 20 20  ( pOp!=0 );.    
302f7 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
302f8 3d 4f 50 5f 4f 70 65 6e 52 65 61 64 20 26 26 20  =OP_OpenRead && 
302f9 70 4f 70 2d 3e 70 33 3d 3d 69 44 62 20 29 7b 0a  pOp->p3==iDb ){.
302fa 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e        Index *pIn
302fb 64 65 78 3b 0a 20 20 20 20 20 20 69 6e 74 20 74  dex;.      int t
302fc 6e 75 6d 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  num = pOp->p2;. 
302fd 20 20 20 20 20 69 66 28 20 74 6e 75 6d 3d 3d 70       if( tnum==p
302fe 54 61 62 2d 3e 74 6e 75 6d 20 29 7b 0a 20 20 20  Tab->tnum ){.   
302ff 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
30300 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72       }.      for
30301 28 70 49 6e 64 65 78 3d 70 54 61 62 2d 3e 70 49  (pIndex=pTab->pI
30302 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 70 49  ndex; pIndex; pI
30303 6e 64 65 78 3d 70 49 6e 64 65 78 2d 3e 70 4e 65  ndex=pIndex->pNe
30304 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  xt){.        if(
30305 20 74 6e 75 6d 3d 3d 70 49 6e 64 65 78 2d 3e 74   tnum==pIndex->t
30306 6e 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  num ){.         
30307 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
30308 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
30309 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
3030a 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
3030b 42 4c 45 0a 20 20 20 20 69 66 28 20 70 4f 70 2d  BLE.    if( pOp-
3030c 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 4f 70 65  >opcode==OP_VOpe
3030d 6e 20 26 26 20 70 4f 70 2d 3e 70 34 2e 70 56 74  n && pOp->p4.pVt
3030e 61 62 3d 3d 70 56 54 61 62 20 29 7b 0a 20 20 20  ab==pVTab ){.   
3030f 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
30310 70 34 2e 70 56 74 61 62 21 3d 30 20 29 3b 0a 20  p4.pVtab!=0 );. 
30311 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
30312 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41  ->p4type==P4_VTA
30313 42 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  B );.      retur
30314 6e 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  n 1;.    }.#endi
30315 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  f.  }.  return 0
30316 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
30317 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
30318 52 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 4c 6f 63  REMENT./*.** Loc
30319 61 74 65 20 6f 72 20 63 72 65 61 74 65 20 61 6e  ate or create an
3031a 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72   AutoincInfo str
3031b 75 63 74 75 72 65 20 61 73 73 6f 63 69 61 74 65  ucture associate
3031c 64 20 77 69 74 68 20 74 61 62 6c 65 20 70 54 61  d with table pTa
3031d 62 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 69 6e  b.** which is in
3031e 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 20   database iDb.  
3031f 52 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73  Return the regis
30320 74 65 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  ter number for t
30321 68 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74  he register.** t
30322 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6d 61  hat holds the ma
30323 78 69 6d 75 6d 20 72 6f 77 69 64 2e 0a 2a 2a 0a  ximum rowid..**.
30324 2a 2a 20 54 68 65 72 65 20 69 73 20 61 74 20 6d  ** There is at m
30325 6f 73 74 20 6f 6e 65 20 41 75 74 6f 69 6e 63 49  ost one AutoincI
30326 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 70 65  nfo structure pe
30327 72 20 74 61 62 6c 65 20 65 76 65 6e 20 69 66 20  r table even if 
30328 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 61 62 6c  the.** same tabl
30329 65 20 69 73 20 61 75 74 6f 69 6e 63 72 65 6d 65  e is autoincreme
3032a 6e 74 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69  nted multiple ti
3032b 6d 65 73 20 64 75 65 20 74 6f 20 69 6e 73 65 72  mes due to inser
3032c 74 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 72 69  ts within.** tri
3032d 67 67 65 72 73 2e 20 20 41 20 6e 65 77 20 41 75  ggers.  A new Au
3032e 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72 75 63 74  toincInfo struct
3032f 75 72 65 20 69 73 20 63 72 65 61 74 65 64 20 69  ure is created i
30330 66 20 74 68 69 73 20 69 73 20 74 68 65 0a 2a 2a  f this is the.**
30331 20 66 69 72 73 74 20 75 73 65 20 6f 66 20 74 61   first use of ta
30332 62 6c 65 20 70 54 61 62 2e 20 20 4f 6e 20 32 6e  ble pTab.  On 2n
30333 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74  d and subsequent
30334 20 75 73 65 73 2c 20 74 68 65 20 6f 72 69 67 69   uses, the origi
30335 6e 61 6c 0a 2a 2a 20 41 75 74 6f 69 6e 63 49 6e  nal.** AutoincIn
30336 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20  fo structure is 
30337 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 65  used..**.** Thre
30338 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  e memory locatio
30339 6e 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64  ns are allocated
3033a 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 52  :.**.**   (1)  R
3033b 65 67 69 73 74 65 72 20 74 6f 20 68 6f 6c 64 20  egister to hold 
3033c 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
3033d 70 54 61 62 20 74 61 62 6c 65 2e 0a 2a 2a 20 20  pTab table..**  
3033e 20 28 32 29 20 20 52 65 67 69 73 74 65 72 20 74   (2)  Register t
3033f 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 78 69 6d  o hold the maxim
30340 75 6d 20 52 4f 57 49 44 20 6f 66 20 70 54 61 62  um ROWID of pTab
30341 2e 0a 2a 2a 20 20 20 28 33 29 20 20 52 65 67 69  ..**   (3)  Regi
30342 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65  ster to hold the
30343 20 72 6f 77 69 64 20 69 6e 20 73 71 6c 69 74 65   rowid in sqlite
30344 5f 73 65 71 75 65 6e 63 65 20 6f 66 20 70 54 61  _sequence of pTa
30345 62 0a 2a 2a 0a 2a 2a 20 54 68 65 20 32 6e 64 20  b.**.** The 2nd 
30346 72 65 67 69 73 74 65 72 20 69 73 20 74 68 65 20  register is the 
30347 6f 6e 65 20 74 68 61 74 20 69 73 20 72 65 74 75  one that is retu
30348 72 6e 65 64 2e 20 20 54 68 61 74 20 69 73 20 61  rned.  That is a
30349 6c 6c 20 74 68 65 0a 2a 2a 20 69 6e 73 65 72 74  ll the.** insert
3034a 20 72 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74   routine needs t
3034b 6f 20 6b 6e 6f 77 20 61 62 6f 75 74 2e 0a 2a 2f  o know about..*/
3034c 0a 73 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f  .static int auto
3034d 49 6e 63 42 65 67 69 6e 28 0a 20 20 50 61 72 73  IncBegin(.  Pars
3034e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
3034f 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
30350 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  xt */.  int iDb,
30351 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
30352 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
30353 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61  base holding pTa
30354 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  b */.  Table *pT
30355 61 62 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ab         /* Th
30356 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 77  e table we are w
30357 72 69 74 69 6e 67 20 74 6f 20 2a 2f 0a 29 7b 0a  riting to */.){.
30358 20 20 69 6e 74 20 6d 65 6d 49 64 20 3d 20 30 3b    int memId = 0;
30359 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
3035a 72 20 68 6f 6c 64 69 6e 67 20 6d 61 78 69 6d 75  r holding maximu
3035b 6d 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 66 28  m rowid */.  if(
3035c 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20   pTab->tabFlags 
3035d 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65  & TF_Autoincreme
3035e 6e 74 20 29 7b 0a 20 20 20 20 50 61 72 73 65 20  nt ){.    Parse 
3035f 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c  *pToplevel = sql
30360 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65  ite3ParseTopleve
30361 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 41  l(pParse);.    A
30362 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 49 6e 66  utoincInfo *pInf
30363 6f 3b 0a 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20  o;..    pInfo = 
30364 70 54 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e 63  pToplevel->pAinc
30365 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 49 6e  ;.    while( pIn
30366 66 6f 20 26 26 20 70 49 6e 66 6f 2d 3e 70 54 61  fo && pInfo->pTa
30367 62 21 3d 70 54 61 62 20 29 7b 20 70 49 6e 66 6f  b!=pTab ){ pInfo
30368 20 3d 20 70 49 6e 66 6f 2d 3e 70 4e 65 78 74 3b   = pInfo->pNext;
30369 20 7d 0a 20 20 20 20 69 66 28 20 70 49 6e 66 6f   }.    if( pInfo
3036a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 6e  ==0 ){.      pIn
3036b 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  fo = sqlite3DbMa
3036c 6c 6c 6f 63 52 61 77 28 70 50 61 72 73 65 2d 3e  llocRaw(pParse->
3036d 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 49 6e 66  db, sizeof(*pInf
3036e 6f 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  o));.      if( p
3036f 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e  Info==0 ) return
30370 20 30 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d   0;.      pInfo-
30371 3e 70 4e 65 78 74 20 3d 20 70 54 6f 70 6c 65 76  >pNext = pToplev
30372 65 6c 2d 3e 70 41 69 6e 63 3b 0a 20 20 20 20 20  el->pAinc;.     
30373 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e   pToplevel->pAin
30374 63 20 3d 20 70 49 6e 66 6f 3b 0a 20 20 20 20 20  c = pInfo;.     
30375 20 70 49 6e 66 6f 2d 3e 70 54 61 62 20 3d 20 70   pInfo->pTab = p
30376 54 61 62 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f  Tab;.      pInfo
30377 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20  ->iDb = iDb;.   
30378 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 4d     pToplevel->nM
30379 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 20 20  em++;           
3037a 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
3037b 65 72 20 74 6f 20 68 6f 6c 64 20 6e 61 6d 65 20  er to hold name 
3037c 6f 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  of table */.    
3037d 20 20 70 49 6e 66 6f 2d 3e 72 65 67 43 74 72 20    pInfo->regCtr 
3037e 3d 20 2b 2b 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e  = ++pToplevel->n
3037f 4d 65 6d 3b 20 20 2f 2a 20 4d 61 78 20 72 6f 77  Mem;  /* Max row
30380 69 64 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  id register */. 
30381 20 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e       pToplevel->
30382 6e 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20  nMem++;         
30383 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69           /* Rowi
30384 64 20 69 6e 20 73 71 6c 69 74 65 5f 73 65 71 75  d in sqlite_sequ
30385 65 6e 63 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  ence */.    }.  
30386 20 20 6d 65 6d 49 64 20 3d 20 70 49 6e 66 6f 2d    memId = pInfo-
30387 3e 72 65 67 43 74 72 3b 0a 20 20 7d 0a 20 20 72  >regCtr;.  }.  r
30388 65 74 75 72 6e 20 6d 65 6d 49 64 3b 0a 7d 0a 0a  eturn memId;.}..
30389 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
3038a 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64  ne generates cod
3038b 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e 69 74  e that will init
3038c 69 61 6c 69 7a 65 20 61 6c 6c 20 6f 66 20 74 68  ialize all of th
3038d 65 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 75 73  e.** register us
3038e 65 64 20 62 79 20 74 68 65 20 61 75 74 6f 69 6e  ed by the autoin
3038f 63 72 65 6d 65 6e 74 20 74 72 61 63 6b 65 72 2e  crement tracker.
30390 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49    .*/.SQLITE_PRI
30391 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
30392 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65  3AutoincrementBe
30393 67 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73  gin(Parse *pPars
30394 65 29 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66  e){.  AutoincInf
30395 6f 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  o *p;           
30396 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
30397 61 62 6f 75 74 20 61 6e 20 41 55 54 4f 49 4e 43  about an AUTOINC
30398 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 73 71 6c 69  REMENT */.  sqli
30399 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
3039a 2d 3e 64 62 3b 20 20 2f 2a 20 54 68 65 20 64 61  ->db;  /* The da
3039b 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3039c 6e 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20  n */.  Db *pDb; 
3039d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3039e 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6f 6e    /* Database on
3039f 6c 79 20 61 75 74 6f 69 6e 63 20 74 61 62 6c 65  ly autoinc table
303a0 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 49 64 3b   */.  int memId;
303a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
303a2 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
303a3 64 69 6e 67 20 6d 61 78 20 72 6f 77 69 64 20 2a  ding max rowid *
303a4 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20  /.  int addr;   
303a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
303a6 2a 20 41 20 56 44 42 45 20 61 64 64 72 65 73 73  * A VDBE address
303a7 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20   */.  Vdbe *v = 
303a8 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20  pParse->pVdbe;  
303a9 20 2f 2a 20 56 44 42 45 20 75 6e 64 65 72 20 63   /* VDBE under c
303aa 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 0a  onstruction */..
303ab 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e    /* This routin
303ac 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65  e is never calle
303ad 64 20 64 75 72 69 6e 67 20 74 72 69 67 67 65 72  d during trigger
303ae 2d 67 65 6e 65 72 61 74 69 6f 6e 2e 20 20 49 74  -generation.  It
303af 20 69 73 0a 20 20 2a 2a 20 6f 6e 6c 79 20 63 61   is.  ** only ca
303b0 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 74 6f  lled from the to
303b1 70 2d 6c 65 76 65 6c 20 2a 2f 0a 20 20 61 73 73  p-level */.  ass
303b2 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 54 72  ert( pParse->pTr
303b3 69 67 67 65 72 54 61 62 3d 3d 30 20 29 3b 0a 20  iggerTab==0 );. 
303b4 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 3d   assert( pParse=
303b5 3d 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70  =sqlite3ParseTop
303b6 6c 65 76 65 6c 28 70 50 61 72 73 65 29 20 29 3b  level(pParse) );
303b7 0a 0a 20 20 61 73 73 65 72 74 28 20 76 20 29 3b  ..  assert( v );
303b8 20 20 20 2f 2a 20 57 65 20 66 61 69 6c 65 64 20     /* We failed 
303b9 6c 6f 6e 67 20 61 67 6f 20 69 66 20 74 68 69 73  long ago if this
303ba 20 69 73 20 6e 6f 74 20 73 6f 20 2a 2f 0a 20 20   is not so */.  
303bb 66 6f 72 28 70 20 3d 20 70 50 61 72 73 65 2d 3e  for(p = pParse->
303bc 70 41 69 6e 63 3b 20 70 3b 20 70 20 3d 20 70 2d  pAinc; p; p = p-
303bd 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 44 62  >pNext){.    pDb
303be 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70 2d 3e 69   = &db->aDb[p->i
303bf 44 62 5d 3b 0a 20 20 20 20 6d 65 6d 49 64 20 3d  Db];.    memId =
303c0 20 70 2d 3e 72 65 67 43 74 72 3b 0a 20 20 20 20   p->regCtr;.    
303c1 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53  assert( sqlite3S
303c2 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28 64  chemaMutexHeld(d
303c3 62 2c 20 30 2c 20 70 44 62 2d 3e 70 53 63 68 65  b, 0, pDb->pSche
303c4 6d 61 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ma) );.    sqlit
303c5 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
303c6 73 65 2c 20 30 2c 20 70 2d 3e 69 44 62 2c 20 70  se, 0, p->iDb, p
303c7 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65  Db->pSchema->pSe
303c8 71 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  qTab, OP_OpenRea
303c9 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  d);.    sqlite3V
303ca 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
303cb 4e 75 6c 6c 2c 20 30 2c 20 6d 65 6d 49 64 2c 20  Null, 0, memId, 
303cc 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20 20 61 64  memId+1);.    ad
303cd 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
303ce 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
303cf 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
303d0 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
303d1 6e 67 38 2c 20 30 2c 20 6d 65 6d 49 64 2d 31 2c  ng8, 0, memId-1,
303d2 20 30 2c 20 70 2d 3e 70 54 61 62 2d 3e 7a 4e 61   0, p->pTab->zNa
303d3 6d 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  me, 0);.    sqli
303d4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
303d5 20 4f 50 5f 52 65 77 69 6e 64 2c 20 30 2c 20 61   OP_Rewind, 0, a
303d6 64 64 72 2b 39 29 3b 0a 20 20 20 20 73 71 6c 69  ddr+9);.    sqli
303d7 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
303d8 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30   OP_Column, 0, 0
303d9 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71  , memId);.    sq
303da 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
303db 76 2c 20 4f 50 5f 4e 65 2c 20 6d 65 6d 49 64 2d  v, OP_Ne, memId-
303dc 31 2c 20 61 64 64 72 2b 37 2c 20 6d 65 6d 49 64  1, addr+7, memId
303dd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
303de 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51  beChangeP5(v, SQ
303df 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
303e0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
303e1 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
303e2 77 69 64 2c 20 30 2c 20 6d 65 6d 49 64 2b 31 29  wid, 0, memId+1)
303e3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
303e4 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
303e5 6c 75 6d 6e 2c 20 30 2c 20 31 2c 20 6d 65 6d 49  lumn, 0, 1, memI
303e6 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  d);.    sqlite3V
303e7 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
303e8 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 2b 39 29  Goto, 0, addr+9)
303e9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
303ea 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
303eb 78 74 2c 20 30 2c 20 61 64 64 72 2b 32 29 3b 0a  xt, 0, addr+2);.
303ec 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
303ed 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
303ee 67 65 72 2c 20 30 2c 20 6d 65 6d 49 64 29 3b 0a  ger, 0, memId);.
303ef 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
303f0 64 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp0(v, OP_Clos
303f1 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  e);.  }.}../*.**
303f2 20 55 70 64 61 74 65 20 74 68 65 20 6d 61 78 69   Update the maxi
303f3 6d 75 6d 20 72 6f 77 69 64 20 66 6f 72 20 61 6e  mum rowid for an
303f4 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 63   autoincrement c
303f5 61 6c 63 75 6c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  alculation..**.*
303f6 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
303f7 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20  hould be called 
303f8 77 68 65 6e 20 74 68 65 20 74 6f 70 20 6f 66 20  when the top of 
303f9 74 68 65 20 73 74 61 63 6b 20 68 6f 6c 64 73 20  the stack holds 
303fa 61 0a 2a 2a 20 6e 65 77 20 72 6f 77 69 64 20 74  a.** new rowid t
303fb 68 61 74 20 69 73 20 61 62 6f 75 74 20 74 6f 20  hat is about to 
303fc 62 65 20 69 6e 73 65 72 74 65 64 2e 20 20 49 66  be inserted.  If
303fd 20 74 68 61 74 20 6e 65 77 20 72 6f 77 69 64 20   that new rowid 
303fe 69 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61  is.** larger tha
303ff 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f  n the maximum ro
30400 77 69 64 20 69 6e 20 74 68 65 20 6d 65 6d 49 64  wid in the memId
30401 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2c 20 74 68   memory cell, th
30402 65 6e 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79  en the.** memory
30403 20 63 65 6c 6c 20 69 73 20 75 70 64 61 74 65 64   cell is updated
30404 2e 20 20 54 68 65 20 73 74 61 63 6b 20 69 73 20  .  The stack is 
30405 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74  unchanged..*/.st
30406 61 74 69 63 20 76 6f 69 64 20 61 75 74 6f 49 6e  atic void autoIn
30407 63 53 74 65 70 28 50 61 72 73 65 20 2a 70 50 61  cStep(Parse *pPa
30408 72 73 65 2c 20 69 6e 74 20 6d 65 6d 49 64 2c 20  rse, int memId, 
30409 69 6e 74 20 72 65 67 52 6f 77 69 64 29 7b 0a 20  int regRowid){. 
3040a 20 69 66 28 20 6d 65 6d 49 64 3e 30 20 29 7b 0a   if( memId>0 ){.
3040b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
3040c 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 56  ddOp2(pParse->pV
3040d 64 62 65 2c 20 4f 50 5f 4d 65 6d 4d 61 78 2c 20  dbe, OP_MemMax, 
3040e 6d 65 6d 49 64 2c 20 72 65 67 52 6f 77 69 64 29  memId, regRowid)
3040f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
30410 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65  his routine gene
30411 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 6e  rates the code n
30412 65 65 64 65 64 20 74 6f 20 77 72 69 74 65 20 61  eeded to write a
30413 75 74 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a 2a 20  utoincrement.** 
30414 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 76 61  maximum rowid va
30415 6c 75 65 73 20 62 61 63 6b 20 69 6e 74 6f 20 74  lues back into t
30416 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  he sqlite_sequen
30417 63 65 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 20  ce register..** 
30418 45 76 65 72 79 20 73 74 61 74 65 6d 65 6e 74 20  Every statement 
30419 74 68 61 74 20 6d 69 67 68 74 20 64 6f 20 61 6e  that might do an
3041a 20 49 4e 53 45 52 54 20 69 6e 74 6f 20 61 6e 20   INSERT into an 
3041b 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a 2a  autoincrement.**
3041c 20 74 61 62 6c 65 20 28 65 69 74 68 65 72 20 64   table (either d
3041d 69 72 65 63 74 6c 79 20 6f 72 20 74 68 72 6f 75  irectly or throu
3041e 67 68 20 74 72 69 67 67 65 72 73 29 20 6e 65 65  gh triggers) nee
3041f 64 73 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 0a  ds to call this.
30420 2a 2a 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20  ** routine just 
30421 62 65 66 6f 72 65 20 74 68 65 20 22 65 78 69 74  before the "exit
30422 22 20 63 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54  " code..*/.SQLIT
30423 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
30424 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d  qlite3Autoincrem
30425 65 6e 74 45 6e 64 28 50 61 72 73 65 20 2a 70 50  entEnd(Parse *pP
30426 61 72 73 65 29 7b 0a 20 20 41 75 74 6f 69 6e 63  arse){.  Autoinc
30427 49 6e 66 6f 20 2a 70 3b 0a 20 20 56 64 62 65 20  Info *p;.  Vdbe 
30428 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
30429 62 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  be;.  sqlite3 *d
3042a 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
3042b 0a 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a  .  assert( v );.
3042c 20 20 66 6f 72 28 70 20 3d 20 70 50 61 72 73 65    for(p = pParse
3042d 2d 3e 70 41 69 6e 63 3b 20 70 3b 20 70 20 3d 20  ->pAinc; p; p = 
3042e 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 44  p->pNext){.    D
3042f 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  b *pDb = &db->aD
30430 62 5b 70 2d 3e 69 44 62 5d 3b 0a 20 20 20 20 69  b[p->iDb];.    i
30431 6e 74 20 6a 31 2c 20 6a 32 2c 20 6a 33 2c 20 6a  nt j1, j2, j3, j
30432 34 2c 20 6a 35 3b 0a 20 20 20 20 69 6e 74 20 69  4, j5;.    int i
30433 52 65 63 3b 0a 20 20 20 20 69 6e 74 20 6d 65 6d  Rec;.    int mem
30434 49 64 20 3d 20 70 2d 3e 72 65 67 43 74 72 3b 0a  Id = p->regCtr;.
30435 0a 20 20 20 20 69 52 65 63 20 3d 20 73 71 6c 69  .    iRec = sqli
30436 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
30437 61 72 73 65 29 3b 0a 20 20 20 20 61 73 73 65 72  arse);.    asser
30438 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61  t( sqlite3Schema
30439 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 30 2c  MutexHeld(db, 0,
3043a 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 29 20 29   pDb->pSchema) )
3043b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65  ;.    sqlite3Ope
3043c 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30  nTable(pParse, 0
3043d 2c 20 70 2d 3e 69 44 62 2c 20 70 44 62 2d 3e 70  , p->iDb, pDb->p
3043e 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 2c  Schema->pSeqTab,
3043f 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a   OP_OpenWrite);.
30440 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33      j1 = sqlite3
30441 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
30442 5f 4e 6f 74 4e 75 6c 6c 2c 20 6d 65 6d 49 64 2b  _NotNull, memId+
30443 31 29 3b 0a 20 20 20 20 6a 32 20 3d 20 73 71 6c  1);.    j2 = sql
30444 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76  ite3VdbeAddOp0(v
30445 2c 20 4f 50 5f 52 65 77 69 6e 64 29 3b 0a 20 20  , OP_Rewind);.  
30446 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64    j3 = sqlite3Vd
30447 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
30448 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 69 52 65  olumn, 0, 0, iRe
30449 63 29 3b 0a 20 20 20 20 6a 34 20 3d 20 73 71 6c  c);.    j4 = sql
3044a 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
3044b 2c 20 4f 50 5f 45 71 2c 20 6d 65 6d 49 64 2d 31  , OP_Eq, memId-1
3044c 2c 20 30 2c 20 69 52 65 63 29 3b 0a 20 20 20 20  , 0, iRec);.    
3044d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
3044e 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 30 2c  2(v, OP_Next, 0,
3044f 20 6a 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   j3);.    sqlite
30450 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
30451 20 6a 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   j2);.    sqlite
30452 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
30453 50 5f 4e 65 77 52 6f 77 69 64 2c 20 30 2c 20 6d  P_NewRowid, 0, m
30454 65 6d 49 64 2b 31 29 3b 0a 20 20 20 20 6a 35 20  emId+1);.    j5 
30455 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
30456 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b  Op0(v, OP_Goto);
30457 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
30458 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 34 29 3b  JumpHere(v, j4);
30459 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
3045a 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
3045b 69 64 2c 20 30 2c 20 6d 65 6d 49 64 2b 31 29 3b  id, 0, memId+1);
3045c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
3045d 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
3045e 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
3045f 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 35 29 3b  JumpHere(v, j5);
30460 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
30461 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
30462 65 52 65 63 6f 72 64 2c 20 6d 65 6d 49 64 2d 31  eRecord, memId-1
30463 2c 20 32 2c 20 69 52 65 63 29 3b 0a 20 20 20 20  , 2, iRec);.    
30464 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
30465 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
30466 30 2c 20 69 52 65 63 2c 20 6d 65 6d 49 64 2b 31  0, iRec, memId+1
30467 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
30468 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50  beChangeP5(v, OP
30469 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20  FLAG_APPEND);.  
3046a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3046b 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29  Op0(v, OP_Close)
3046c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c  ;.    sqlite3Rel
3046d 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
3046e 73 65 2c 20 69 52 65 63 29 3b 0a 20 20 7d 0a 7d  se, iRec);.  }.}
3046f 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a 20 49 66 20  .#else./*.** If 
30470 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
30471 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 64 65 66  INCREMENT is def
30472 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 74  ined, then the t
30473 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a  hree routines.**
30474 20 61 62 6f 76 65 20 61 72 65 20 61 6c 6c 20 6e   above are all n
30475 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 64 65 66 69 6e  o-ops.*/.# defin
30476 65 20 61 75 74 6f 49 6e 63 42 65 67 69 6e 28 41  e autoIncBegin(A
30477 2c 42 2c 43 29 20 28 30 29 0a 23 20 64 65 66 69  ,B,C) (0).# defi
30478 6e 65 20 61 75 74 6f 49 6e 63 53 74 65 70 28 41  ne autoIncStep(A
30479 2c 42 2c 43 29 0a 23 65 6e 64 69 66 20 2f 2a 20  ,B,C).#endif /* 
3047a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
3047b 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 0a 2f  INCREMENT */.../
3047c 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
3047d 64 65 20 66 6f 72 20 61 20 63 6f 2d 72 6f 75 74  de for a co-rout
3047e 69 6e 65 20 74 68 61 74 20 77 69 6c 6c 20 65 76  ine that will ev
3047f 61 6c 75 61 74 65 20 61 20 73 75 62 71 75 65 72  aluate a subquer
30480 79 20 6f 6e 65 0a 2a 2a 20 72 6f 77 20 61 74 20  y one.** row at 
30481 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  a time..**.** Th
30482 65 20 70 53 65 6c 65 63 74 20 70 61 72 61 6d 65  e pSelect parame
30483 74 65 72 20 69 73 20 74 68 65 20 73 75 62 71 75  ter is the subqu
30484 65 72 79 20 74 68 61 74 20 74 68 65 20 63 6f 2d  ery that the co-
30485 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 76 61  routine will eva
30486 6c 75 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 66 6f  luation..** Info
30487 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
30488 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 63 6f  e location of co
30489 2d 72 6f 75 74 69 6e 65 20 61 6e 64 20 74 68 65  -routine and the
3048a 20 72 65 67 69 73 74 65 72 73 20 69 74 20 77 69   registers it wi
3048b 6c 6c 20 75 73 65 0a 2a 2a 20 69 73 20 72 65 74  ll use.** is ret
3048c 75 72 6e 65 64 20 62 79 20 66 69 6c 6c 69 6e 67  urned by filling
3048d 20 69 6e 20 74 68 65 20 70 44 65 73 74 20 6f 62   in the pDest ob
3048e 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69  ject..**.** Regi
3048f 73 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 63 61  sters are alloca
30490 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ted as follows:.
30491 2a 2a 0a 2a 2a 20 20 20 70 44 65 73 74 2d 3e 69  **.**   pDest->i
30492 53 44 50 61 72 6d 20 20 20 20 20 20 54 68 65 20  SDParm      The 
30493 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67  register holding
30494 20 74 68 65 20 6e 65 78 74 20 65 6e 74 72 79 2d   the next entry-
30495 70 6f 69 6e 74 20 6f 66 20 74 68 65 0a 2a 2a 20  point of the.** 
30496 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30497 20 20 20 20 20 20 63 6f 2d 72 6f 75 74 69 6e 65        co-routine
30498 2e 20 20 52 75 6e 20 74 68 65 20 63 6f 2d 72 6f  .  Run the co-ro
30499 75 74 69 6e 65 20 74 6f 20 69 74 73 20 6e 65 78  utine to its nex
3049a 74 20 62 72 65 61 6b 70 6f 69 6e 74 0a 2a 2a 20  t breakpoint.** 
3049b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3049c 20 20 20 20 20 20 62 79 20 63 61 6c 6c 69 6e 67        by calling
3049d 20 22 4f 50 5f 59 69 65 6c 64 20 24 58 22 20 77   "OP_Yield $X" w
3049e 68 65 72 65 20 24 58 20 69 73 20 70 44 65 73 74  here $X is pDest
3049f 2d 3e 69 53 44 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a  ->iSDParm..**.**
304a0 20 20 20 70 44 65 73 74 2d 3e 69 53 44 50 61 72     pDest->iSDPar
304a1 6d 2b 31 20 20 20 20 54 68 65 20 72 65 67 69 73  m+1    The regis
304a2 74 65 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  ter holding the 
304a3 22 63 6f 6d 70 6c 65 74 65 64 22 20 66 6c 61 67  "completed" flag
304a4 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20   for the.**     
304a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
304a6 20 20 63 6f 2d 72 6f 75 74 69 6e 65 2e 20 54 68    co-routine. Th
304a7 69 73 20 72 65 67 69 73 74 65 72 20 69 73 20 30  is register is 0
304a8 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   if the previous
304a9 20 59 69 65 6c 64 0a 2a 2a 20 20 20 20 20 20 20   Yield.**       
304aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
304ab 67 65 6e 65 72 61 74 65 64 20 61 20 6e 65 77 20  generated a new 
304ac 72 65 73 75 6c 74 20 72 6f 77 2c 20 6f 72 20 31  result row, or 1
304ad 20 69 66 20 74 68 65 20 73 75 62 71 75 65 72 79   if the subquery
304ae 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
304af 20 20 20 20 20 20 20 20 20 20 68 61 73 20 63 6f            has co
304b0 6d 70 6c 65 74 65 64 2e 20 20 49 66 20 74 68 65  mpleted.  If the
304b1 20 59 69 65 6c 64 20 69 73 20 63 61 6c 6c 65 64   Yield is called
304b2 20 61 67 61 69 6e 0a 2a 2a 20 20 20 20 20 20 20   again.**       
304b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
304b4 61 66 74 65 72 20 74 68 69 73 20 72 65 67 69 73  after this regis
304b5 74 65 72 20 62 65 63 6f 6d 65 73 20 31 2c 20 74  ter becomes 1, t
304b6 68 65 6e 20 74 68 65 20 56 44 42 45 20 77 69 6c  hen the VDBE wil
304b7 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  l.**            
304b8 20 20 20 20 20 20 20 20 20 20 20 68 61 6c 74 20             halt 
304b9 77 69 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 49  with an SQLITE_I
304ba 4e 54 45 52 4e 41 4c 20 65 72 72 6f 72 2e 0a 2a  NTERNAL error..*
304bb 2a 0a 2a 2a 20 20 20 70 44 65 73 74 2d 3e 69 53  *.**   pDest->iS
304bc 64 73 74 20 20 20 20 20 20 20 20 46 69 72 73 74  dst        First
304bd 20 72 65 73 75 6c 74 20 72 65 67 69 73 74 65 72   result register
304be 2e 0a 2a 2a 0a 2a 2a 20 20 20 70 44 65 73 74 2d  ..**.**   pDest-
304bf 3e 6e 53 64 73 74 20 20 20 20 20 20 20 20 4e 75  >nSdst        Nu
304c0 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 72  mber of result r
304c1 65 67 69 73 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  egisters..**.** 
304c2 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 6e  This routine han
304c3 64 6c 65 73 20 61 6c 6c 20 6f 66 20 74 68 65 20  dles all of the 
304c4 72 65 67 69 73 74 65 72 20 61 6c 6c 6f 63 61 74  register allocat
304c5 69 6f 6e 20 61 6e 64 20 66 69 6c 6c 73 20 69 6e  ion and fills in
304c6 20 74 68 65 0a 2a 2a 20 70 44 65 73 74 20 73 74   the.** pDest st
304c7 72 75 63 74 75 72 65 20 61 70 70 72 6f 70 72 69  ructure appropri
304c8 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 48 65 72  ately..**.** Her
304c9 65 20 69 73 20 61 20 73 63 68 65 6d 61 74 69 63  e is a schematic
304ca 20 6f 66 20 74 68 65 20 67 65 6e 65 72 61 74 65   of the generate
304cb 64 20 63 6f 64 65 20 61 73 73 75 6d 69 6e 67 20  d code assuming 
304cc 74 68 61 74 20 58 20 69 73 20 74 68 65 20 0a 2a  that X is the .*
304cd 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 20 65 6e 74  * co-routine ent
304ce 72 79 2d 70 6f 69 6e 74 20 72 65 67 69 73 74 65  ry-point registe
304cf 72 20 72 65 67 5b 70 44 65 73 74 2d 3e 69 53 44  r reg[pDest->iSD
304d0 50 61 72 6d 5d 2c 20 74 68 61 74 20 45 4f 46 20  Parm], that EOF 
304d1 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65  is the.** comple
304d2 74 65 64 20 66 6c 61 67 20 72 65 67 5b 70 44 65  ted flag reg[pDe
304d3 73 74 2d 3e 69 53 44 50 61 72 6d 2b 31 5d 2c 20  st->iSDParm+1], 
304d4 61 6e 64 20 52 20 61 6e 64 20 53 20 61 72 65 20  and R and S are 
304d5 74 68 65 20 72 61 6e 67 65 20 6f 66 0a 2a 2a 20  the range of.** 
304d6 72 65 67 69 73 74 65 72 73 20 74 68 61 74 20 68  registers that h
304d7 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 20 73  old the result s
304d8 65 74 2c 20 72 65 67 5b 70 44 65 73 74 2d 3e 69  et, reg[pDest->i
304d9 53 64 73 74 5d 20 74 68 72 6f 75 67 68 0a 2a 2a  Sdst] through.**
304da 20 72 65 67 5b 70 44 65 73 74 2d 3e 69 53 64 73   reg[pDest->iSds
304db 74 2b 70 44 65 73 74 2d 3e 6e 53 64 73 74 2d 31  t+pDest->nSdst-1
304dc 5d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  ]:.**.**        
304dd 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 20   X <- A.**      
304de 20 20 20 45 4f 46 20 3c 2d 20 30 0a 2a 2a 20 20     EOF <- 0.**  
304df 20 20 20 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a         goto B.**
304e0 20 20 20 20 20 20 41 3a 20 73 65 74 75 70 20 66        A: setup f
304e1 6f 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a  or the SELECT.**
304e2 20 20 20 20 20 20 20 20 20 6c 6f 6f 70 20 72 6f           loop ro
304e3 77 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54  ws in the SELECT
304e4 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6c 6f  .**           lo
304e5 61 64 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20  ad results into 
304e6 72 65 67 69 73 74 65 72 73 20 52 2e 2e 53 0a 2a  registers R..S.*
304e7 2a 20 20 20 20 20 20 20 20 20 20 20 79 69 65 6c  *           yiel
304e8 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 65  d X.**         e
304e9 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20 20  nd loop.**      
304ea 20 20 20 63 6c 65 61 6e 75 70 20 61 66 74 65 72     cleanup after
304eb 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20   the SELECT.**  
304ec 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 31 0a         EOF <- 1.
304ed 2a 2a 20 20 20 20 20 20 20 20 20 79 69 65 6c 64  **         yield
304ee 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 68 61   X.**         ha
304ef 6c 74 2d 65 72 72 6f 72 0a 2a 2a 20 20 20 20 20  lt-error.**     
304f0 20 42 3a 0a 2a 2a 0a 2a 2a 20 54 6f 20 75 73 65   B:.**.** To use
304f1 20 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65   this subroutine
304f2 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 67 65 6e  , the caller gen
304f3 65 72 61 74 65 73 20 63 6f 64 65 20 61 73 20 66  erates code as f
304f4 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
304f5 20 20 20 20 20 20 5b 20 43 6f 2d 72 6f 75 74 69        [ Co-routi
304f6 6e 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  ne generated by 
304f7 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 2c  this subroutine,
304f8 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 5d 0a 2a   shown above ].*
304f9 2a 20 20 20 20 20 20 53 3a 20 79 69 65 6c 64 20  *      S: yield 
304fa 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20  X.**         if 
304fb 45 4f 46 20 67 6f 74 6f 20 45 0a 2a 2a 20 20 20  EOF goto E.**   
304fc 20 20 20 20 20 20 69 66 20 73 6b 69 70 20 74 68        if skip th
304fd 69 73 20 72 6f 77 2c 20 67 6f 74 6f 20 43 0a 2a  is row, goto C.*
304fe 2a 20 20 20 20 20 20 20 20 20 69 66 20 74 65 72  *         if ter
304ff 6d 69 6e 61 74 65 20 6c 6f 6f 70 2c 20 67 6f 74  minate loop, got
30500 6f 20 45 0a 2a 2a 20 20 20 20 20 20 20 20 20 64  o E.**         d
30501 65 61 6c 20 77 69 74 68 20 74 68 69 73 20 72 6f  eal with this ro
30502 77 0a 2a 2a 20 20 20 20 20 20 43 3a 20 67 6f 74  w.**      C: got
30503 6f 20 53 0a 2a 2a 20 20 20 20 20 20 45 3a 0a 2a  o S.**      E:.*
30504 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
30505 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65   int sqlite3Code
30506 43 6f 72 6f 75 74 69 6e 65 28 50 61 72 73 65 20  Coroutine(Parse 
30507 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20  *pParse, Select 
30508 2a 70 53 65 6c 65 63 74 2c 20 53 65 6c 65 63 74  *pSelect, Select
30509 44 65 73 74 20 2a 70 44 65 73 74 29 7b 0a 20 20  Dest *pDest){.  
3050a 69 6e 74 20 72 65 67 59 69 65 6c 64 3b 20 20 20  int regYield;   
3050b 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
3050c 68 6f 6c 64 69 6e 67 20 63 6f 2d 72 6f 75 74 69  holding co-routi
3050d 6e 65 20 65 6e 74 72 79 2d 70 6f 69 6e 74 20 2a  ne entry-point *
3050e 2f 0a 20 20 69 6e 74 20 72 65 67 45 6f 66 3b 20  /.  int regEof; 
3050f 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
30510 74 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f 2d 72  ter holding co-r
30511 6f 75 74 69 6e 65 20 63 6f 6d 70 6c 65 74 69 6f  outine completio
30512 6e 20 66 6c 61 67 20 2a 2f 0a 20 20 69 6e 74 20  n flag */.  int 
30513 61 64 64 72 54 6f 70 3b 20 20 20 20 20 20 20 20  addrTop;        
30514 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 63 6f  /* Top of the co
30515 2d 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e  -routine */.  in
30516 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20 20 20  t j1;           
30517 20 20 2f 2a 20 4a 75 6d 70 20 69 6e 73 74 72 75    /* Jump instru
30518 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72  ction */.  int r
30519 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  c;             /
3051a 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f  * Result code */
3051b 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20  .  Vdbe *v;     
3051c 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 75         /* VDBE u
3051d 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
3051e 6e 20 2a 2f 0a 0a 20 20 72 65 67 59 69 65 6c 64  n */..  regYield
3051f 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
30520 6d 3b 0a 20 20 72 65 67 45 6f 66 20 3d 20 2b 2b  m;.  regEof = ++
30521 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
30522 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
30523 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 64  be(pParse);.  ad
30524 64 72 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  drTop = sqlite3V
30525 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
30526 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
30527 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
30528 65 67 65 72 2c 20 61 64 64 72 54 6f 70 2b 32 2c  eger, addrTop+2,
30529 20 72 65 67 59 69 65 6c 64 29 3b 20 2f 2a 20 58   regYield); /* X
3052a 20 3c 2d 20 41 20 2a 2f 0a 20 20 56 64 62 65 43   <- A */.  VdbeC
3052b 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 43 6f 2d 72  omment((v, "Co-r
3052c 6f 75 74 69 6e 65 20 65 6e 74 72 79 20 70 6f 69  outine entry poi
3052d 6e 74 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33  nt"));.  sqlite3
3052e 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
3052f 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67  _Integer, 0, reg
30530 45 6f 66 29 3b 20 20 20 20 20 20 20 20 20 20 20  Eof);           
30531 2f 2a 20 45 4f 46 20 3c 2d 20 30 20 2a 2f 0a 20  /* EOF <- 0 */. 
30532 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
30533 20 22 43 6f 2d 72 6f 75 74 69 6e 65 20 63 6f 6d   "Co-routine com
30534 70 6c 65 74 69 6f 6e 20 66 6c 61 67 22 29 29 3b  pletion flag"));
30535 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
30536 44 65 73 74 49 6e 69 74 28 70 44 65 73 74 2c 20  DestInit(pDest, 
30537 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 72  SRT_Coroutine, r
30538 65 67 59 69 65 6c 64 29 3b 0a 20 20 6a 31 20 3d  egYield);.  j1 =
30539 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
3053a 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
3053b 2c 20 30 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  , 0);.  rc = sql
3053c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
3053d 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 44 65 73  e, pSelect, pDes
3053e 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  t);.  assert( pP
3053f 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 7c 7c  arse->nErr==0 ||
30540 20 72 63 20 29 3b 0a 20 20 69 66 28 20 70 50 61   rc );.  if( pPa
30541 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
30542 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c  ailed && rc==SQL
30543 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 53 51  ITE_OK ) rc = SQ
30544 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 69 66  LITE_NOMEM;.  if
30545 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
30546 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
30547 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
30548 67 65 72 2c 20 31 2c 20 72 65 67 45 6f 66 29 3b  ger, 1, regEof);
30549 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
3054a 4f 46 20 3c 2d 20 31 20 2a 2f 0a 20 20 73 71 6c  OF <- 1 */.  sql
3054b 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
3054c 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67 59  , OP_Yield, regY
3054d 69 65 6c 64 29 3b 20 20 20 2f 2a 20 79 69 65 6c  ield);   /* yiel
3054e 64 20 58 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  d X */.  sqlite3
3054f 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
30550 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 49 4e  _Halt, SQLITE_IN
30551 54 45 52 4e 41 4c 2c 20 4f 45 5f 41 62 6f 72 74  TERNAL, OE_Abort
30552 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  );.  VdbeComment
30553 28 28 76 2c 20 22 45 6e 64 20 6f 66 20 63 6f 72  ((v, "End of cor
30554 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 73 71 6c  outine"));.  sql
30555 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
30556 28 76 2c 20 6a 31 29 3b 20 20 20 20 20 20 20 20  (v, j1);        
30557 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30558 20 20 20 20 20 2f 2a 20 6c 61 62 65 6c 20 42 3a       /* label B:
30559 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   */.  return rc;
3055a 0a 7d 0a 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  .}..../* Forward
3055b 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a   declaration */.
3055c 73 74 61 74 69 63 20 69 6e 74 20 78 66 65 72 4f  static int xferO
3055d 70 74 69 6d 69 7a 61 74 69 6f 6e 28 0a 20 20 50  ptimization(.  P
3055e 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
3055f 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63       /* Parser c
30560 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
30561 65 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 20  e *pDest,       
30562 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 77    /* The table w
30563 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20  e are inserting 
30564 69 6e 74 6f 20 2a 2f 0a 20 20 53 65 6c 65 63 74  into */.  Select
30565 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20   *pSelect,      
30566 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61 74  /* A SELECT stat
30567 65 6d 65 6e 74 20 74 6f 20 75 73 65 20 61 73 20  ement to use as 
30568 74 68 65 20 64 61 74 61 20 73 6f 75 72 63 65 20  the data source 
30569 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72  */.  int onError
3056a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f  ,          /* Ho
3056b 77 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73  w to handle cons
3056c 74 72 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f  traint errors */
3056d 0a 20 20 69 6e 74 20 69 44 62 44 65 73 74 20 20  .  int iDbDest  
3056e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3056f 64 61 74 61 62 61 73 65 20 6f 66 20 70 44 65 73  database of pDes
30570 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54  t */.);../*.** T
30571 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
30572 61 6c 6c 20 74 6f 20 68 61 6e 64 6c 65 20 53 51  all to handle SQ
30573 4c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  L of the followi
30574 6e 67 20 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20  ng forms:.**.** 
30575 20 20 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 54     insert into T
30576 41 42 4c 45 20 28 49 44 4c 49 53 54 29 20 76 61  ABLE (IDLIST) va
30577 6c 75 65 73 28 45 58 50 52 4c 49 53 54 29 0a 2a  lues(EXPRLIST).*
30578 2a 20 20 20 20 69 6e 73 65 72 74 20 69 6e 74 6f  *    insert into
30579 20 54 41 42 4c 45 20 28 49 44 4c 49 53 54 29 20   TABLE (IDLIST) 
3057a 73 65 6c 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65  select.**.** The
3057b 20 49 44 4c 49 53 54 20 66 6f 6c 6c 6f 77 69 6e   IDLIST followin
3057c 67 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  g the table name
3057d 20 69 73 20 61 6c 77 61 79 73 20 6f 70 74 69 6f   is always optio
3057e 6e 61 6c 2e 20 20 49 66 20 6f 6d 69 74 74 65 64  nal.  If omitted
3057f 2c 0a 2a 2a 20 74 68 65 6e 20 61 20 6c 69 73 74  ,.** then a list
30580 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20   of all columns 
30581 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 69 73  for the table is
30582 20 73 75 62 73 74 69 74 75 74 65 64 2e 20 20 54   substituted.  T
30583 68 65 20 49 44 4c 49 53 54 0a 2a 2a 20 61 70 70  he IDLIST.** app
30584 65 61 72 73 20 69 6e 20 74 68 65 20 70 43 6f 6c  ears in the pCol
30585 75 6d 6e 20 70 61 72 61 6d 65 74 65 72 2e 20 20  umn parameter.  
30586 70 43 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c 20  pColumn is NULL 
30587 69 66 20 49 44 4c 49 53 54 20 69 73 20 6f 6d 69  if IDLIST is omi
30588 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tted..**.** The 
30589 70 4c 69 73 74 20 70 61 72 61 6d 65 74 65 72 20  pList parameter 
3058a 68 6f 6c 64 73 20 45 58 50 52 4c 49 53 54 20 69  holds EXPRLIST i
3058b 6e 20 74 68 65 20 66 69 72 73 74 20 66 6f 72 6d  n the first form
3058c 20 6f 66 20 74 68 65 20 49 4e 53 45 52 54 0a 2a   of the INSERT.*
3058d 2a 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76  * statement abov
3058e 65 2c 20 61 6e 64 20 70 53 65 6c 65 63 74 20 69  e, and pSelect i
3058f 73 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 74 68 65  s NULL.  For the
30590 20 73 65 63 6f 6e 64 20 66 6f 72 6d 2c 20 70 4c   second form, pL
30591 69 73 74 20 69 73 0a 2a 2a 20 4e 55 4c 4c 20 61  ist is.** NULL a
30592 6e 64 20 70 53 65 6c 65 63 74 20 69 73 20 61 20  nd pSelect is a 
30593 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73  pointer to the s
30594 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20  elect statement 
30595 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65  used to generate
30596 0a 2a 2a 20 64 61 74 61 20 66 6f 72 20 74 68 65  .** data for the
30597 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 54   insert..**.** T
30598 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65  he code generate
30599 64 20 66 6f 6c 6c 6f 77 73 20 6f 6e 65 20 6f 66  d follows one of
3059a 20 66 6f 75 72 20 74 65 6d 70 6c 61 74 65 73 2e   four templates.
3059b 20 20 46 6f 72 20 61 20 73 69 6d 70 6c 65 0a 2a    For a simple.*
3059c 2a 20 73 65 6c 65 63 74 20 77 69 74 68 20 64 61  * select with da
3059d 74 61 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61  ta coming from a
3059e 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 2c 20   VALUES clause, 
3059f 74 68 65 20 63 6f 64 65 20 65 78 65 63 75 74 65  the code execute
305a0 73 0a 2a 2a 20 6f 6e 63 65 20 73 74 72 61 69 67  s.** once straig
305a1 68 74 20 64 6f 77 6e 20 74 68 72 6f 75 67 68 2e  ht down through.
305a2 20 20 50 73 65 75 64 6f 2d 63 6f 64 65 20 66 6f    Pseudo-code fo
305a3 6c 6c 6f 77 73 20 28 77 65 20 63 61 6c 6c 20 74  llows (we call t
305a4 68 69 73 0a 2a 2a 20 74 68 65 20 22 31 73 74 20  his.** the "1st 
305a5 74 65 6d 70 6c 61 74 65 22 29 3a 0a 2a 2a 0a 2a  template"):.**.*
305a6 2a 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 77  *         open w
305a7 72 69 74 65 20 63 75 72 73 6f 72 20 74 6f 20 3c  rite cursor to <
305a8 74 61 62 6c 65 3e 20 61 6e 64 20 69 74 73 20 69  table> and its i
305a9 6e 64 69 63 65 73 0a 2a 2a 20 20 20 20 20 20 20  ndices.**       
305aa 20 20 70 75 74 73 20 56 41 4c 55 45 53 20 63 6c    puts VALUES cl
305ab 61 75 73 65 20 65 78 70 72 65 73 73 69 6f 6e 73  ause expressions
305ac 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 0a   onto the stack.
305ad 2a 2a 20 20 20 20 20 20 20 20 20 77 72 69 74 65  **         write
305ae 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 72   the resulting r
305af 65 63 6f 72 64 20 69 6e 74 6f 20 3c 74 61 62 6c  ecord into <tabl
305b0 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6c  e>.**         cl
305b1 65 61 6e 75 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eanup.**.** The 
305b2 74 68 72 65 65 20 72 65 6d 61 69 6e 69 6e 67 20  three remaining 
305b3 74 65 6d 70 6c 61 74 65 73 20 61 73 73 75 6d 65  templates assume
305b4 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
305b5 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a  s of the form.**
305b6 0a 2a 2a 20 20 20 49 4e 53 45 52 54 20 49 4e 54  .**   INSERT INT
305b7 4f 20 3c 74 61 62 6c 65 3e 20 53 45 4c 45 43 54  O <table> SELECT
305b8 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68   ....**.** If th
305b9 65 20 53 45 4c 45 43 54 20 63 6c 61 75 73 65 20  e SELECT clause 
305ba 69 73 20 6f 66 20 74 68 65 20 72 65 73 74 72 69  is of the restri
305bb 63 74 65 64 20 66 6f 72 6d 20 22 53 45 4c 45 43  cted form "SELEC
305bc 54 20 2a 20 46 52 4f 4d 20 3c 74 61 62 6c 65 32  T * FROM <table2
305bd 3e 22 20 2d 0a 2a 2a 20 69 6e 20 6f 74 68 65 72  >" -.** in other
305be 20 77 6f 72 64 73 20 69 66 20 74 68 65 20 53 45   words if the SE
305bf 4c 45 43 54 20 70 75 6c 6c 73 20 61 6c 6c 20 63  LECT pulls all c
305c0 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 61 20 73 69  olumns from a si
305c1 6e 67 6c 65 20 74 61 62 6c 65 0a 2a 2a 20 61 6e  ngle table.** an
305c2 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20 57 48  d there is no WH
305c3 45 52 45 20 6f 72 20 4c 49 4d 49 54 20 6f 72 20  ERE or LIMIT or 
305c4 47 52 4f 55 50 20 42 59 20 6f 72 20 4f 52 44 45  GROUP BY or ORDE
305c5 52 20 42 59 20 63 6c 61 75 73 65 73 2c 20 61 6e  R BY clauses, an
305c6 64 0a 2a 2a 20 69 66 20 3c 74 61 62 6c 65 32 3e  d.** if <table2>
305c7 20 61 6e 64 20 3c 74 61 62 6c 65 31 3e 20 61 72   and <table1> ar
305c8 65 20 64 69 73 74 69 6e 63 74 20 74 61 62 6c 65  e distinct table
305c9 73 20 62 75 74 20 68 61 76 65 20 69 64 65 6e 74  s but have ident
305ca 69 63 61 6c 0a 2a 2a 20 73 63 68 65 6d 61 73 2c  ical.** schemas,
305cb 20 69 6e 63 6c 75 64 69 6e 67 20 61 6c 6c 20 74   including all t
305cc 68 65 20 73 61 6d 65 20 69 6e 64 69 63 65 73 2c  he same indices,
305cd 20 74 68 65 6e 20 61 20 73 70 65 63 69 61 6c 20   then a special 
305ce 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20  optimization.** 
305cf 69 73 20 69 6e 76 6f 6b 65 64 20 74 68 61 74 20  is invoked that 
305d0 63 6f 70 69 65 73 20 72 61 77 20 72 65 63 6f 72  copies raw recor
305d1 64 73 20 66 72 6f 6d 20 3c 74 61 62 6c 65 32 3e  ds from <table2>
305d2 20 6f 76 65 72 20 74 6f 20 3c 74 61 62 6c 65 31   over to <table1
305d3 3e 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 78 66  >..** See the xf
305d4 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28 29  erOptimization()
305d5 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68   function for th
305d6 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
305d7 20 6f 66 20 74 68 69 73 0a 2a 2a 20 74 65 6d 70   of this.** temp
305d8 6c 61 74 65 2e 20 20 54 68 69 73 20 69 73 20 74  late.  This is t
305d9 68 65 20 32 6e 64 20 74 65 6d 70 6c 61 74 65 2e  he 2nd template.
305da 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f  .**.**         o
305db 70 65 6e 20 61 20 77 72 69 74 65 20 63 75 72 73  pen a write curs
305dc 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a  or to <table>.**
305dd 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 72 65           open re
305de 61 64 20 63 75 72 73 6f 72 20 6f 6e 20 3c 74 61  ad cursor on <ta
305df 62 6c 65 32 3e 0a 2a 2a 20 20 20 20 20 20 20 20  ble2>.**        
305e0 20 74 72 61 6e 73 66 65 72 20 61 6c 6c 20 72 65   transfer all re
305e1 63 6f 72 64 73 20 69 6e 20 3c 74 61 62 6c 65 32  cords in <table2
305e2 3e 20 6f 76 65 72 20 74 6f 20 3c 74 61 62 6c 65  > over to <table
305e3 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 6f  >.**         clo
305e4 73 65 20 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20  se cursors.**   
305e5 20 20 20 20 20 20 66 6f 72 65 61 63 68 20 69 6e        foreach in
305e6 64 65 78 20 6f 6e 20 3c 74 61 62 6c 65 3e 0a 2a  dex on <table>.*
305e7 2a 20 20 20 20 20 20 20 20 20 20 20 6f 70 65 6e  *           open
305e8 20 61 20 77 72 69 74 65 20 63 75 72 73 6f 72 20   a write cursor 
305e9 6f 6e 20 74 68 65 20 3c 74 61 62 6c 65 3e 20 69  on the <table> i
305ea 6e 64 65 78 0a 2a 2a 20 20 20 20 20 20 20 20 20  ndex.**         
305eb 20 20 6f 70 65 6e 20 61 20 72 65 61 64 20 63 75    open a read cu
305ec 72 73 6f 72 20 6f 6e 20 74 68 65 20 63 6f 72 72  rsor on the corr
305ed 65 73 70 6f 6e 64 69 6e 67 20 3c 74 61 62 6c 65  esponding <table
305ee 32 3e 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20  2> index.**     
305ef 20 20 20 20 20 20 74 72 61 6e 73 66 65 72 20 61        transfer a
305f0 6c 6c 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20  ll records from 
305f1 74 68 65 20 72 65 61 64 20 74 6f 20 74 68 65 20  the read to the 
305f2 77 72 69 74 65 20 63 75 72 73 6f 72 73 0a 2a 2a  write cursors.**
305f3 20 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65             close
305f4 20 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20   cursors.**     
305f5 20 20 20 20 65 6e 64 20 66 6f 72 65 61 63 68 0a      end foreach.
305f6 2a 2a 0a 2a 2a 20 54 68 65 20 33 72 64 20 74 65  **.** The 3rd te
305f7 6d 70 6c 61 74 65 20 69 73 20 66 6f 72 20 77 68  mplate is for wh
305f8 65 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 74 65  en the second te
305f9 6d 70 6c 61 74 65 20 64 6f 65 73 20 6e 6f 74 20  mplate does not 
305fa 61 70 70 6c 79 0a 2a 2a 20 61 6e 64 20 74 68 65  apply.** and the
305fb 20 53 45 4c 45 43 54 20 63 6c 61 75 73 65 20 64   SELECT clause d
305fc 6f 65 73 20 6e 6f 74 20 72 65 61 64 20 66 72 6f  oes not read fro
305fd 6d 20 3c 74 61 62 6c 65 3e 20 61 74 20 61 6e 79  m <table> at any
305fe 20 74 69 6d 65 2e 0a 2a 2a 20 54 68 65 20 67 65   time..** The ge
305ff 6e 65 72 61 74 65 64 20 63 6f 64 65 20 66 6f 6c  nerated code fol
30600 6c 6f 77 73 20 74 68 69 73 20 74 65 6d 70 6c 61  lows this templa
30601 74 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  te:.**.**       
30602 20 20 45 4f 46 20 3c 2d 20 30 0a 2a 2a 20 20 20    EOF <- 0.**   
30603 20 20 20 20 20 20 58 20 3c 2d 20 41 0a 2a 2a 20        X <- A.** 
30604 20 20 20 20 20 20 20 20 67 6f 74 6f 20 42 0a 2a          goto B.*
30605 2a 20 20 20 20 20 20 41 3a 20 73 65 74 75 70 20  *      A: setup 
30606 66 6f 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a  for the SELECT.*
30607 2a 20 20 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f  *         loop o
30608 76 65 72 20 74 68 65 20 72 6f 77 73 20 69 6e 20  ver the rows in 
30609 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20  the SELECT.**   
3060a 20 20 20 20 20 20 20 20 6c 6f 61 64 20 76 61 6c          load val
3060b 75 65 73 20 69 6e 74 6f 20 72 65 67 69 73 74 65  ues into registe
3060c 72 73 20 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20  rs R..R+n.**    
3060d 20 20 20 20 20 20 20 79 69 65 6c 64 20 58 0a 2a         yield X.*
3060e 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f  *         end lo
3060f 6f 70 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6c  op.**         cl
30610 65 61 6e 75 70 20 61 66 74 65 72 20 74 68 65 20  eanup after the 
30611 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  SELECT.**       
30612 20 20 45 4f 46 20 3c 2d 20 31 0a 2a 2a 20 20 20    EOF <- 1.**   
30613 20 20 20 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a        yield X.**
30614 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 41 0a           goto A.
30615 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70 65 6e 20  **      B: open 
30616 77 72 69 74 65 20 63 75 72 73 6f 72 20 74 6f 20  write cursor to 
30617 3c 74 61 62 6c 65 3e 20 61 6e 64 20 69 74 73 20  <table> and its 
30618 69 6e 64 69 63 65 73 0a 2a 2a 20 20 20 20 20 20  indices.**      
30619 43 3a 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20  C: yield X.**   
3061a 20 20 20 20 20 20 69 66 20 45 4f 46 20 67 6f 74        if EOF got
3061b 6f 20 44 0a 2a 2a 20 20 20 20 20 20 20 20 20 69  o D.**         i
3061c 6e 73 65 72 74 20 74 68 65 20 73 65 6c 65 63 74  nsert the select
3061d 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 3c 74 61   result into <ta
3061e 62 6c 65 3e 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e  ble> from R..R+n
3061f 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74 6f  .**         goto
30620 20 43 0a 2a 2a 20 20 20 20 20 20 44 3a 20 63 6c   C.**      D: cl
30621 65 61 6e 75 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eanup.**.** The 
30622 34 74 68 20 74 65 6d 70 6c 61 74 65 20 69 73 20  4th template is 
30623 75 73 65 64 20 69 66 20 74 68 65 20 69 6e 73 65  used if the inse
30624 72 74 20 73 74 61 74 65 6d 65 6e 74 20 74 61 6b  rt statement tak
30625 65 73 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 73  es its.** values
30626 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20 62   from a SELECT b
30627 75 74 20 74 68 65 20 64 61 74 61 20 69 73 20 62  ut the data is b
30628 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e  eing inserted in
30629 74 6f 20 61 20 74 61 62 6c 65 0a 2a 2a 20 74 68  to a table.** th
3062a 61 74 20 69 73 20 61 6c 73 6f 20 72 65 61 64 20  at is also read 
3062b 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 53  as part of the S
3062c 45 4c 45 43 54 2e 20 20 49 6e 20 74 68 65 20 74  ELECT.  In the t
3062d 68 69 72 64 20 66 6f 72 6d 2c 0a 2a 2a 20 77 65  hird form,.** we
3062e 20 68 61 76 65 20 74 6f 20 75 73 65 20 61 20 69   have to use a i
3062f 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
30630 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72  e to store the r
30631 65 73 75 6c 74 73 20 6f 66 0a 2a 2a 20 74 68 65  esults of.** the
30632 20 73 65 6c 65 63 74 2e 20 20 54 68 65 20 74 65   select.  The te
30633 6d 70 6c 61 74 65 20 69 73 20 6c 69 6b 65 20 74  mplate is like t
30634 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  his:.**.**      
30635 20 20 20 45 4f 46 20 3c 2d 20 30 0a 2a 2a 20 20     EOF <- 0.**  
30636 20 20 20 20 20 20 20 58 20 3c 2d 20 41 0a 2a 2a         X <- A.**
30637 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 42 0a           goto B.
30638 2a 2a 20 20 20 20 20 20 41 3a 20 73 65 74 75 70  **      A: setup
30639 20 66 6f 72 20 74 68 65 20 53 45 4c 45 43 54 0a   for the SELECT.
3063a 2a 2a 20 20 20 20 20 20 20 20 20 6c 6f 6f 70 20  **         loop 
3063b 6f 76 65 72 20 74 68 65 20 74 61 62 6c 65 73 20  over the tables 
3063c 69 6e 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a  in the SELECT.**
3063d 20 20 20 20 20 20 20 20 20 20 20 6c 6f 61 64 20             load 
3063e 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73  value into regis
3063f 74 65 72 20 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20  ter R..R+n.**   
30640 20 20 20 20 20 20 20 20 79 69 65 6c 64 20 58 0a          yield X.
30641 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c  **         end l
30642 6f 6f 70 0a 2a 2a 20 20 20 20 20 20 20 20 20 63  oop.**         c
30643 6c 65 61 6e 75 70 20 61 66 74 65 72 20 74 68 65  leanup after the
30644 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20   SELECT.**      
30645 20 20 20 45 4f 46 20 3c 2d 20 31 0a 2a 2a 20 20     EOF <- 1.**  
30646 20 20 20 20 20 20 20 79 69 65 6c 64 20 58 0a 2a         yield X.*
30647 2a 20 20 20 20 20 20 20 20 20 68 61 6c 74 2d 65  *         halt-e
30648 72 72 6f 72 0a 2a 2a 20 20 20 20 20 20 42 3a 20  rror.**      B: 
30649 6f 70 65 6e 20 74 65 6d 70 20 74 61 62 6c 65 0a  open temp table.
3064a 2a 2a 20 20 20 20 20 20 4c 3a 20 79 69 65 6c 64  **      L: yield
3064b 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 66   X.**         if
3064c 20 45 4f 46 20 67 6f 74 6f 20 4d 0a 2a 2a 20 20   EOF goto M.**  
3064d 20 20 20 20 20 20 20 69 6e 73 65 72 74 20 72 6f         insert ro
3064e 77 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e 20 69 6e  w from R..R+n in
3064f 74 6f 20 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a  to temp table.**
30650 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 4c 0a           goto L.
30651 2a 2a 20 20 20 20 20 20 4d 3a 20 6f 70 65 6e 20  **      M: open 
30652 77 72 69 74 65 20 63 75 72 73 6f 72 20 74 6f 20  write cursor to 
30653 3c 74 61 62 6c 65 3e 20 61 6e 64 20 69 74 73 20  <table> and its 
30654 69 6e 64 69 63 65 73 0a 2a 2a 20 20 20 20 20 20  indices.**      
30655 20 20 20 72 65 77 69 6e 64 20 74 65 6d 70 20 74     rewind temp t
30656 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 43 3a 20  able.**      C: 
30657 6c 6f 6f 70 20 6f 76 65 72 20 72 6f 77 73 20 6f  loop over rows o
30658 66 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74  f intermediate t
30659 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  able.**         
3065a 20 20 74 72 61 6e 73 66 65 72 20 76 61 6c 75 65    transfer value
3065b 73 20 66 6f 72 6d 20 69 6e 74 65 72 6d 65 64 69  s form intermedi
3065c 61 74 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 3c  ate table into <
3065d 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20  table>.**       
3065e 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20    end loop.**   
3065f 20 20 20 44 3a 20 63 6c 65 61 6e 75 70 0a 2a 2f     D: cleanup.*/
30660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
30661 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 73 65  void sqlite3Inse
30662 72 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  rt(.  Parse *pPa
30663 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
30664 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
30665 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
30666 4c 69 73 74 2c 20 20 20 20 2f 2a 20 4e 61 6d 65  List,    /* Name
30667 20 6f 66 20 74 61 62 6c 65 20 69 6e 74 6f 20 77   of table into w
30668 68 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65  hich we are inse
30669 72 74 69 6e 67 20 2a 2f 0a 20 20 45 78 70 72 4c  rting */.  ExprL
3066a 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20  ist *pList,     
3066b 20 2f 2a 20 4c 69 73 74 20 6f 66 20 76 61 6c 75   /* List of valu
3066c 65 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  es to be inserte
3066d 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  d */.  Select *p
3066e 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20  Select,      /* 
3066f 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  A SELECT stateme
30670 6e 74 20 74 6f 20 75 73 65 20 61 73 20 74 68 65  nt to use as the
30671 20 64 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a   data source */.
30672 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d    IdList *pColum
30673 6e 2c 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d  n,      /* Colum
30674 6e 20 6e 61 6d 65 73 20 63 6f 72 72 65 73 70 6f  n names correspo
30675 6e 64 69 6e 67 20 74 6f 20 49 44 4c 49 53 54 2e  nding to IDLIST.
30676 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f   */.  int onErro
30677 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48  r           /* H
30678 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e  ow to handle con
30679 73 74 72 61 69 6e 74 20 65 72 72 6f 72 73 20 2a  straint errors *
3067a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
3067b 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  db;          /* 
3067c 54 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  The main databas
3067d 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
3067e 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
3067f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
30680 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20 69 6e  ble to insert in
30681 74 6f 2e 20 20 61 6b 61 20 54 41 42 4c 45 20 2a  to.  aka TABLE *
30682 2f 0a 20 20 63 68 61 72 20 2a 7a 54 61 62 3b 20  /.  char *zTab; 
30683 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
30684 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69  e of the table i
30685 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65  nto which we are
30686 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20   inserting */.  
30687 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
30688 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
30689 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 6f   the database ho
3068a 6c 64 69 6e 67 20 74 68 69 73 20 74 61 62 6c 65  lding this table
3068b 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20   */.  int i, j, 
3068c 69 64 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c  idx;        /* L
3068d 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
3068e 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
3068f 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72          /* Gener
30690 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68  ate code into th
30691 69 73 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  is virtual machi
30692 6e 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70  ne */.  Index *p
30693 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Idx;          /*
30694 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
30695 72 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68 65  r indices of the
30696 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
30697 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  nColumn;        
30698 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
30699 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 64 61  olumns in the da
3069a 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 69 64  ta */.  int nHid
3069b 64 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  den = 0;      /*
3069c 20 4e 75 6d 62 65 72 20 6f 66 20 68 69 64 64 65   Number of hidde
3069d 6e 20 63 6f 6c 75 6d 6e 73 20 69 66 20 54 41 42  n columns if TAB
3069e 4c 45 20 69 73 20 76 69 72 74 75 61 6c 20 2a 2f  LE is virtual */
3069f 0a 20 20 69 6e 74 20 62 61 73 65 43 75 72 20 3d  .  int baseCur =
306a0 20 30 3b 20 20 20 20 20 20 2f 2a 20 56 44 42 45   0;      /* VDBE
306a1 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
306a2 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74  or pTab */.  int
306a3 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b   keyColumn = -1;
306a4 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 74 68 61     /* Column tha
306a5 74 20 69 73 20 74 68 65 20 49 4e 54 45 47 45 52  t is the INTEGER
306a6 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a   PRIMARY KEY */.
306a7 20 20 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b    int endOfLoop;
306a8 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c          /* Label
306a9 20 66 6f 72 20 74 68 65 20 65 6e 64 20 6f 66 20   for the end of 
306aa 74 68 65 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f  the insertion lo
306ab 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65 54  op */.  int useT
306ac 65 6d 70 54 61 62 6c 65 20 3d 20 30 3b 20 2f 2a  empTable = 0; /*
306ad 20 53 74 6f 72 65 20 53 45 4c 45 43 54 20 72 65   Store SELECT re
306ae 73 75 6c 74 73 20 69 6e 20 69 6e 74 65 72 6d 65  sults in interme
306af 64 69 61 74 65 20 74 61 62 6c 65 20 2a 2f 0a 20  diate table */. 
306b0 20 69 6e 74 20 73 72 63 54 61 62 20 3d 20 30 3b   int srcTab = 0;
306b1 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 63         /* Data c
306b2 6f 6d 65 73 20 66 72 6f 6d 20 74 68 69 73 20 74  omes from this t
306b3 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20  emporary cursor 
306b4 69 66 20 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20  if >=0 */.  int 
306b5 61 64 64 72 49 6e 73 54 6f 70 20 3d 20 30 3b 20  addrInsTop = 0; 
306b6 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 6c 61 62    /* Jump to lab
306b7 65 6c 20 22 44 22 20 2a 2f 0a 20 20 69 6e 74 20  el "D" */.  int 
306b8 61 64 64 72 43 6f 6e 74 20 3d 20 30 3b 20 20 20  addrCont = 0;   
306b9 20 20 2f 2a 20 54 6f 70 20 6f 66 20 69 6e 73 65    /* Top of inse
306ba 72 74 20 6c 6f 6f 70 2e 20 4c 61 62 65 6c 20 22  rt loop. Label "
306bb 43 22 20 69 6e 20 74 65 6d 70 6c 61 74 65 73 20  C" in templates 
306bc 33 20 61 6e 64 20 34 20 2a 2f 0a 20 20 69 6e 74  3 and 4 */.  int
306bd 20 61 64 64 72 53 65 6c 65 63 74 20 3d 20 30 3b   addrSelect = 0;
306be 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
306bf 20 63 6f 72 6f 75 74 69 6e 65 20 74 68 61 74 20   coroutine that 
306c0 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 53  implements the S
306c1 45 4c 45 43 54 20 2a 2f 0a 20 20 53 65 6c 65 63  ELECT */.  Selec
306c2 74 44 65 73 74 20 64 65 73 74 3b 20 20 20 20 20  tDest dest;     
306c3 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20   /* Destination 
306c4 66 6f 72 20 53 45 4c 45 43 54 20 6f 6e 20 72 68  for SELECT on rh
306c5 73 20 6f 66 20 49 4e 53 45 52 54 20 2a 2f 0a 20  s of INSERT */. 
306c6 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
306c7 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
306c8 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64  of database hold
306c9 69 6e 67 20 54 41 42 4c 45 20 2a 2f 0a 20 20 44  ing TABLE */.  D
306ca 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20  b *pDb;         
306cb 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
306cc 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
306cd 74 61 62 6c 65 20 62 65 69 6e 67 20 69 6e 73 65  table being inse
306ce 72 74 65 64 20 69 6e 74 6f 20 2a 2f 0a 20 20 69  rted into */.  i
306cf 6e 74 20 61 70 70 65 6e 64 46 6c 61 67 20 3d 20  nt appendFlag = 
306d0 30 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  0;   /* True if 
306d1 74 68 65 20 69 6e 73 65 72 74 20 69 73 20 6c 69  the insert is li
306d2 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70  kely to be an ap
306d3 70 65 6e 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65  pend */..  /* Re
306d4 67 69 73 74 65 72 20 61 6c 6c 6f 63 61 74 69 6f  gister allocatio
306d5 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46  ns */.  int regF
306d6 72 6f 6d 53 65 6c 65 63 74 20 3d 20 30 3b 2f 2a  romSelect = 0;/*
306d7 20 42 61 73 65 20 72 65 67 69 73 74 65 72 20 66   Base register f
306d8 6f 72 20 64 61 74 61 20 63 6f 6d 69 6e 67 20 66  or data coming f
306d9 72 6f 6d 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20  rom SELECT */.  
306da 69 6e 74 20 72 65 67 41 75 74 6f 69 6e 63 20 3d  int regAutoinc =
306db 20 30 3b 20 20 20 2f 2a 20 52 65 67 69 73 74 65   0;   /* Registe
306dc 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 41 55  r holding the AU
306dd 54 4f 49 4e 43 52 45 4d 45 4e 54 20 63 6f 75 6e  TOINCREMENT coun
306de 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ter */.  int reg
306df 52 6f 77 43 6f 75 6e 74 20 3d 20 30 3b 20 20 2f  RowCount = 0;  /
306e0 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73  * Memory cell us
306e1 65 64 20 66 6f 72 20 74 68 65 20 72 6f 77 20 63  ed for the row c
306e2 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
306e3 72 65 67 49 6e 73 3b 20 20 20 20 20 20 20 20 20  regIns;         
306e4 20 20 2f 2a 20 42 6c 6f 63 6b 20 6f 66 20 72 65    /* Block of re
306e5 67 73 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64  gs holding rowid
306e6 2b 64 61 74 61 20 62 65 69 6e 67 20 69 6e 73 65  +data being inse
306e7 72 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65  rted */.  int re
306e8 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20  gRowid;         
306e9 2f 2a 20 72 65 67 69 73 74 65 72 73 20 68 6f 6c  /* registers hol
306ea 64 69 6e 67 20 69 6e 73 65 72 74 20 72 6f 77 69  ding insert rowi
306eb 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61  d */.  int regDa
306ec 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ta;          /* 
306ed 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67  register holding
306ee 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 74 6f   first column to
306ef 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 69 6e 74   insert */.  int
306f0 20 72 65 67 45 6f 66 20 3d 20 30 3b 20 20 20 20   regEof = 0;    
306f1 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 72     /* Register r
306f2 65 63 6f 72 64 69 6e 67 20 65 6e 64 20 6f 66 20  ecording end of 
306f3 53 45 4c 45 43 54 20 64 61 74 61 20 2a 2f 0a 20  SELECT data */. 
306f4 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20 3d 20   int *aRegIdx = 
306f5 30 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 72 65  0;     /* One re
306f6 67 69 73 74 65 72 20 61 6c 6c 6f 63 61 74 65 64  gister allocated
306f7 20 74 6f 20 65 61 63 68 20 69 6e 64 65 78 20 2a   to each index *
306f8 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
306f9 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
306fa 20 69 6e 74 20 69 73 56 69 65 77 3b 20 20 20 20   int isView;    
306fb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
306fc 54 72 75 65 20 69 66 20 61 74 74 65 6d 70 74 69  True if attempti
306fd 6e 67 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74  ng to insert int
306fe 6f 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 54 72  o a view */.  Tr
306ff 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b  igger *pTrigger;
30700 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73            /* Lis
30701 74 20 6f 66 20 74 72 69 67 67 65 72 73 20 6f 6e  t of triggers on
30702 20 70 54 61 62 2c 20 69 66 20 72 65 71 75 69 72   pTab, if requir
30703 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74 6d 61 73  ed */.  int tmas
30704 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
30705 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 74      /* Mask of t
30706 72 69 67 67 65 72 20 74 69 6d 65 73 20 2a 2f 0a  rigger times */.
30707 23 65 6e 64 69 66 0a 0a 20 20 64 62 20 3d 20 70  #endif..  db = p
30708 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 6d 65 6d  Parse->db;.  mem
30709 73 65 74 28 26 64 65 73 74 2c 20 30 2c 20 73 69  set(&dest, 0, si
3070a 7a 65 6f 66 28 64 65 73 74 29 29 3b 0a 20 20 69  zeof(dest));.  i
3070b 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
3070c 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
3070d 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  led ){.    goto 
3070e 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
3070f 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65    }..  /* Locate
30710 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20   the table into 
30711 77 68 69 63 68 20 77 65 20 77 69 6c 6c 20 62 65  which we will be
30712 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 69   inserting new i
30713 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2f  nformation..  */
30714 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 4c  .  assert( pTabL
30715 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a  ist->nSrc==1 );.
30716 20 20 7a 54 61 62 20 3d 20 70 54 61 62 4c 69 73    zTab = pTabLis
30717 74 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 3b 0a 20  t->a[0].zName;. 
30718 20 69 66 28 20 4e 45 56 45 52 28 7a 54 61 62 3d   if( NEVER(zTab=
30719 3d 30 29 20 29 20 67 6f 74 6f 20 69 6e 73 65 72  =0) ) goto inser
3071a 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 70 54 61  t_cleanup;.  pTa
3071b 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  b = sqlite3SrcLi
3071c 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c  stLookup(pParse,
3071d 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 69 66   pTabList);.  if
3071e 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20  ( pTab==0 ){.   
3071f 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
30720 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 44 62 20  anup;.  }.  iDb 
30721 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
30722 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
30723 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73  >pSchema);.  ass
30724 65 72 74 28 20 69 44 62 3c 64 62 2d 3e 6e 44 62  ert( iDb<db->nDb
30725 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d   );.  pDb = &db-
30726 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 7a 44 62  >aDb[iDb];.  zDb
30727 20 3d 20 70 44 62 2d 3e 7a 4e 61 6d 65 3b 0a 20   = pDb->zName;. 
30728 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
30729 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
3072a 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 70 54 61  LITE_INSERT, pTa
3072b 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 62  b->zName, 0, zDb
3072c 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e  ) ){.    goto in
3072d 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
3072e 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  }..  /* Figure o
3072f 75 74 20 69 66 20 77 65 20 68 61 76 65 20 61 6e  ut if we have an
30730 79 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 69  y triggers and i
30731 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  f the table bein
30732 67 0a 20 20 2a 2a 20 69 6e 73 65 72 74 65 64 20  g.  ** inserted 
30733 69 6e 74 6f 20 69 73 20 61 20 76 69 65 77 0a 20  into is a view. 
30734 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
30735 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
30736 20 20 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c    pTrigger = sql
30737 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73  ite3TriggersExis
30738 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  t(pParse, pTab, 
30739 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c 20 26 74  TK_INSERT, 0, &t
3073a 6d 61 73 6b 29 3b 0a 20 20 69 73 56 69 65 77 20  mask);.  isView 
3073b 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 21  = pTab->pSelect!
3073c 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  =0;.#else.# defi
3073d 6e 65 20 70 54 72 69 67 67 65 72 20 30 0a 23 20  ne pTrigger 0.# 
3073e 64 65 66 69 6e 65 20 74 6d 61 73 6b 20 30 0a 23  define tmask 0.#
3073f 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30   define isView 0
30740 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
30741 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a  QLITE_OMIT_VIEW.
30742 23 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23  # undef isView.#
30743 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30   define isView 0
30744 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74  .#endif.  assert
30745 28 20 28 70 54 72 69 67 67 65 72 20 26 26 20 74  ( (pTrigger && t
30746 6d 61 73 6b 29 20 7c 7c 20 28 70 54 72 69 67 67  mask) || (pTrigg
30747 65 72 3d 3d 30 20 26 26 20 74 6d 61 73 6b 3d 3d  er==0 && tmask==
30748 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70  0) );..  /* If p
30749 54 61 62 20 69 73 20 72 65 61 6c 6c 79 20 61 20  Tab is really a 
3074a 76 69 65 77 2c 20 6d 61 6b 65 20 73 75 72 65 20  view, make sure 
3074b 69 74 20 68 61 73 20 62 65 65 6e 20 69 6e 69 74  it has been init
3074c 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2a 20 56 69  ialized..  ** Vi
3074d 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
3074e 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  () is a no-op if
3074f 20 70 54 61 62 20 69 73 20 6e 6f 74 20 61 20 76   pTab is not a v
30750 69 65 77 20 28 6f 72 20 76 69 72 74 75 61 6c 20  iew (or virtual 
30751 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20 74 61 62  .  ** module tab
30752 6c 65 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  le)..  */.  if( 
30753 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f  sqlite3ViewGetCo
30754 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
30755 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67  , pTab) ){.    g
30756 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
30757 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e  up;.  }..  /* En
30758 73 75 72 65 20 74 68 61 74 3a 0a 20 20 2a 20 20  sure that:.  *  
30759 28 61 29 20 74 68 65 20 74 61 62 6c 65 20 69 73  (a) the table is
3075a 20 6e 6f 74 20 72 65 61 64 2d 6f 6e 6c 79 2c 20   not read-only, 
3075b 0a 20 20 2a 20 20 28 62 29 20 74 68 61 74 20 69  .  *  (b) that i
3075c 66 20 69 74 20 69 73 20 61 20 76 69 65 77 20 74  f it is a view t
3075d 68 65 6e 20 4f 4e 20 49 4e 53 45 52 54 20 74 72  hen ON INSERT tr
3075e 69 67 67 65 72 73 20 65 78 69 73 74 0a 20 20 2a  iggers exist.  *
3075f 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49  /.  if( sqlite3I
30760 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72 73 65  sReadOnly(pParse
30761 2c 20 70 54 61 62 2c 20 74 6d 61 73 6b 29 20 29  , pTab, tmask) )
30762 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72  {.    goto inser
30763 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  t_cleanup;.  }..
30764 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20    /* Allocate a 
30765 56 44 42 45 0a 20 20 2a 2f 0a 20 20 76 20 3d 20  VDBE.  */.  v = 
30766 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
30767 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d  Parse);.  if( v=
30768 3d 30 20 29 20 67 6f 74 6f 20 69 6e 73 65 72 74  =0 ) goto insert
30769 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20  _cleanup;.  if( 
3076a 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
3076b 30 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 43  0 ) sqlite3VdbeC
3076c 6f 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b 0a  ountChanges(v);.
3076d 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
3076e 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
3076f 72 73 65 2c 20 70 53 65 6c 65 63 74 20 7c 7c 20  rse, pSelect || 
30770 70 54 72 69 67 67 65 72 2c 20 69 44 62 29 3b 0a  pTrigger, iDb);.
30771 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
30772 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 0a 20 20  OMIT_XFER_OPT.  
30773 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74 65 6d  /* If the statem
30774 65 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f  ent is of the fo
30775 72 6d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  rm.  **.  **    
30776 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c     INSERT INTO <
30777 74 61 62 6c 65 31 3e 20 53 45 4c 45 43 54 20 2a  table1> SELECT *
30778 20 46 52 4f 4d 20 3c 74 61 62 6c 65 32 3e 3b 0a   FROM <table2>;.
30779 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 73    **.  ** Then s
3077a 70 65 63 69 61 6c 20 6f 70 74 69 6d 69 7a 61 74  pecial optimizat
3077b 69 6f 6e 73 20 63 61 6e 20 62 65 20 61 70 70 6c  ions can be appl
3077c 69 65 64 20 74 68 61 74 20 6d 61 6b 65 20 74 68  ied that make th
3077d 65 20 74 72 61 6e 73 66 65 72 0a 20 20 2a 2a 20  e transfer.  ** 
3077e 76 65 72 79 20 66 61 73 74 20 61 6e 64 20 77 68  very fast and wh
3077f 69 63 68 20 72 65 64 75 63 65 20 66 72 61 67 6d  ich reduce fragm
30780 65 6e 74 61 74 69 6f 6e 20 6f 66 20 69 6e 64 69  entation of indi
30781 63 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ces..  **.  ** T
30782 68 69 73 20 69 73 20 74 68 65 20 32 6e 64 20 74  his is the 2nd t
30783 65 6d 70 6c 61 74 65 2e 0a 20 20 2a 2f 0a 20 20  emplate..  */.  
30784 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26  if( pColumn==0 &
30785 26 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74 69  & xferOptimizati
30786 6f 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  on(pParse, pTab,
30787 20 70 53 65 6c 65 63 74 2c 20 6f 6e 45 72 72 6f   pSelect, onErro
30788 72 2c 20 69 44 62 29 20 29 7b 0a 20 20 20 20 61  r, iDb) ){.    a
30789 73 73 65 72 74 28 20 21 70 54 72 69 67 67 65 72  ssert( !pTrigger
3078a 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
3078b 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20  pList==0 );.    
3078c 67 6f 74 6f 20 69 6e 73 65 72 74 5f 65 6e 64 3b  goto insert_end;
3078d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
3078e 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f  QLITE_OMIT_XFER_
3078f 4f 50 54 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  OPT */..  /* If 
30790 74 68 69 73 20 69 73 20 61 6e 20 41 55 54 4f 49  this is an AUTOI
30791 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c 65 2c 20  NCREMENT table, 
30792 6c 6f 6f 6b 20 75 70 20 74 68 65 20 73 65 71 75  look up the sequ
30793 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 6e 20 74  ence number in t
30794 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 73  he.  ** sqlite_s
30795 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 61 6e  equence table an
30796 64 20 73 74 6f 72 65 20 69 74 20 69 6e 20 6d 65  d store it in me
30797 6d 6f 72 79 20 63 65 6c 6c 20 72 65 67 41 75 74  mory cell regAut
30798 6f 69 6e 63 2e 0a 20 20 2a 2f 0a 20 20 72 65 67  oinc..  */.  reg
30799 41 75 74 6f 69 6e 63 20 3d 20 61 75 74 6f 49 6e  Autoinc = autoIn
3079a 63 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 69  cBegin(pParse, i
3079b 44 62 2c 20 70 54 61 62 29 3b 0a 0a 20 20 2f 2a  Db, pTab);..  /*
3079c 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20   Figure out how 
3079d 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  many columns of 
3079e 64 61 74 61 20 61 72 65 20 73 75 70 70 6c 69 65  data are supplie
3079f 64 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 0a  d.  If the data.
307a0 20 20 2a 2a 20 69 73 20 63 6f 6d 69 6e 67 20 66    ** is coming f
307a1 72 6f 6d 20 61 20 53 45 4c 45 43 54 20 73 74 61  rom a SELECT sta
307a2 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 67 65 6e  tement, then gen
307a3 65 72 61 74 65 20 61 20 63 6f 2d 72 6f 75 74 69  erate a co-routi
307a4 6e 65 20 74 68 61 74 0a 20 20 2a 2a 20 70 72 6f  ne that.  ** pro
307a5 64 75 63 65 73 20 61 20 73 69 6e 67 6c 65 20 72  duces a single r
307a6 6f 77 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ow of the SELECT
307a7 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f 63 61 74   on each invocat
307a8 69 6f 6e 2e 20 20 54 68 65 0a 20 20 2a 2a 20 63  ion.  The.  ** c
307a9 6f 2d 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65  o-routine is the
307aa 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20 74   common header t
307ab 6f 20 74 68 65 20 33 72 64 20 61 6e 64 20 34 74  o the 3rd and 4t
307ac 68 20 74 65 6d 70 6c 61 74 65 73 2e 0a 20 20 2a  h templates..  *
307ad 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 20  /.  if( pSelect 
307ae 29 7b 0a 20 20 20 20 2f 2a 20 44 61 74 61 20 69  ){.    /* Data i
307af 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 20  s coming from a 
307b0 53 45 4c 45 43 54 2e 20 20 47 65 6e 65 72 61 74  SELECT.  Generat
307b1 65 20 61 20 63 6f 2d 72 6f 75 74 69 6e 65 20 74  e a co-routine t
307b2 6f 20 72 75 6e 20 74 68 61 74 0a 20 20 20 20 2a  o run that.    *
307b3 2a 20 53 45 4c 45 43 54 2e 20 2a 2f 0a 20 20 20  * SELECT. */.   
307b4 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
307b5 33 43 6f 64 65 43 6f 72 6f 75 74 69 6e 65 28 70  3CodeCoroutine(p
307b6 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20  Parse, pSelect, 
307b7 26 64 65 73 74 29 3b 0a 20 20 20 20 69 66 28 20  &dest);.    if( 
307b8 72 63 20 29 20 67 6f 74 6f 20 69 6e 73 65 72 74  rc ) goto insert
307b9 5f 63 6c 65 61 6e 75 70 3b 0a 0a 20 20 20 20 72  _cleanup;..    r
307ba 65 67 45 6f 66 20 3d 20 64 65 73 74 2e 69 53 44  egEof = dest.iSD
307bb 50 61 72 6d 20 2b 20 31 3b 0a 20 20 20 20 72 65  Parm + 1;.    re
307bc 67 46 72 6f 6d 53 65 6c 65 63 74 20 3d 20 64 65  gFromSelect = de
307bd 73 74 2e 69 53 64 73 74 3b 0a 20 20 20 20 61 73  st.iSdst;.    as
307be 73 65 72 74 28 20 70 53 65 6c 65 63 74 2d 3e 70  sert( pSelect->p
307bf 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 6e 43 6f  EList );.    nCo
307c0 6c 75 6d 6e 20 3d 20 70 53 65 6c 65 63 74 2d 3e  lumn = pSelect->
307c1 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20  pEList->nExpr;. 
307c2 20 20 20 61 73 73 65 72 74 28 20 64 65 73 74 2e     assert( dest.
307c3 6e 53 64 73 74 3d 3d 6e 43 6f 6c 75 6d 6e 20 29  nSdst==nColumn )
307c4 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 75 73  ;..    /* Set us
307c5 65 54 65 6d 70 54 61 62 6c 65 20 74 6f 20 54 52  eTempTable to TR
307c6 55 45 20 69 66 20 74 68 65 20 72 65 73 75 6c 74  UE if the result
307c7 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73   of the SELECT s
307c8 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20  tatement.    ** 
307c9 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65  should be writte
307ca 6e 20 69 6e 74 6f 20 61 20 74 65 6d 70 6f 72 61  n into a tempora
307cb 72 79 20 74 61 62 6c 65 20 28 74 65 6d 70 6c 61  ry table (templa
307cc 74 65 20 34 29 2e 20 20 53 65 74 20 74 6f 0a 20  te 4).  Set to. 
307cd 20 20 20 2a 2a 20 46 41 4c 53 45 20 69 66 20 65     ** FALSE if e
307ce 61 63 68 2a 20 72 6f 77 20 6f 66 20 74 68 65 20  ach* row of the 
307cf 53 45 4c 45 43 54 20 63 61 6e 20 62 65 20 77 72  SELECT can be wr
307d0 69 74 74 65 6e 20 64 69 72 65 63 74 6c 79 20 69  itten directly i
307d1 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 64  nto.    ** the d
307d2 65 73 74 69 6e 61 74 69 6f 6e 20 74 61 62 6c 65  estination table
307d3 20 28 74 65 6d 70 6c 61 74 65 20 33 29 2e 0a 20   (template 3).. 
307d4 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 74     **.    ** A t
307d5 65 6d 70 20 74 61 62 6c 65 20 6d 75 73 74 20 62  emp table must b
307d6 65 20 75 73 65 64 20 69 66 20 74 68 65 20 74 61  e used if the ta
307d7 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61 74 65  ble being update
307d8 64 20 69 73 20 61 6c 73 6f 20 6f 6e 65 0a 20 20  d is also one.  
307d9 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c    ** of the tabl
307da 65 73 20 62 65 69 6e 67 20 72 65 61 64 20 62 79  es being read by
307db 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
307dc 65 6d 65 6e 74 2e 20 20 41 6c 73 6f 20 75 73 65  ement.  Also use
307dd 20 61 20 0a 20 20 20 20 2a 2a 20 74 65 6d 70 20   a .    ** temp 
307de 74 61 62 6c 65 20 69 6e 20 74 68 65 20 63 61 73  table in the cas
307df 65 20 6f 66 20 72 6f 77 20 74 72 69 67 67 65 72  e of row trigger
307e0 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  s..    */.    if
307e1 28 20 70 54 72 69 67 67 65 72 20 7c 7c 20 72 65  ( pTrigger || re
307e2 61 64 73 54 61 62 6c 65 28 70 50 61 72 73 65 2c  adsTable(pParse,
307e3 20 61 64 64 72 53 65 6c 65 63 74 2c 20 69 44 62   addrSelect, iDb
307e4 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  , pTab) ){.     
307e5 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 3d 20   useTempTable = 
307e6 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  1;.    }..    if
307e7 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29  ( useTempTable )
307e8 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76 6f 6b  {.      /* Invok
307e9 65 20 74 68 65 20 63 6f 72 6f 75 74 69 6e 65 20  e the coroutine 
307ea 74 6f 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72  to extract infor
307eb 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20  mation from the 
307ec 53 45 4c 45 43 54 0a 20 20 20 20 20 20 2a 2a 20  SELECT.      ** 
307ed 61 6e 64 20 61 64 64 20 69 74 20 74 6f 20 61 20  and add it to a 
307ee 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20  transient table 
307ef 73 72 63 54 61 62 2e 20 20 54 68 65 20 63 6f 64  srcTab.  The cod
307f0 65 20 67 65 6e 65 72 61 74 65 64 0a 20 20 20 20  e generated.    
307f1 20 20 2a 2a 20 68 65 72 65 20 69 73 20 66 72 6f    ** here is fro
307f2 6d 20 74 68 65 20 34 74 68 20 74 65 6d 70 6c 61  m the 4th templa
307f3 74 65 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  te:.      **.   
307f4 20 20 20 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70     **      B: op
307f5 65 6e 20 74 65 6d 70 20 74 61 62 6c 65 0a 20 20  en temp table.  
307f6 20 20 20 20 2a 2a 20 20 20 20 20 20 4c 3a 20 79      **      L: y
307f7 69 65 6c 64 20 58 0a 20 20 20 20 20 20 2a 2a 20  ield X.      ** 
307f8 20 20 20 20 20 20 20 20 69 66 20 45 4f 46 20 67          if EOF g
307f9 6f 74 6f 20 4d 0a 20 20 20 20 20 20 2a 2a 20 20  oto M.      **  
307fa 20 20 20 20 20 20 20 69 6e 73 65 72 74 20 72 6f         insert ro
307fb 77 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e 20 69 6e  w from R..R+n in
307fc 74 6f 20 74 65 6d 70 20 74 61 62 6c 65 0a 20 20  to temp table.  
307fd 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 67      **         g
307fe 6f 74 6f 20 4c 0a 20 20 20 20 20 20 2a 2a 20 20  oto L.      **  
307ff 20 20 20 20 4d 3a 20 2e 2e 2e 0a 20 20 20 20 20      M: ....     
30800 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65   */.      int re
30801 67 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 2f  gRec;          /
30802 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 68 6f  * Register to ho
30803 6c 64 20 70 61 63 6b 65 64 20 72 65 63 6f 72 64  ld packed record
30804 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65   */.      int re
30805 67 54 65 6d 70 52 6f 77 69 64 3b 20 20 20 20 2f  gTempRowid;    /
30806 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 68 6f  * Register to ho
30807 6c 64 20 74 65 6d 70 20 74 61 62 6c 65 20 52 4f  ld temp table RO
30808 57 49 44 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  WID */.      int
30809 20 61 64 64 72 54 6f 70 3b 20 20 20 20 20 20 20   addrTop;       
3080a 20 20 2f 2a 20 4c 61 62 65 6c 20 22 4c 22 20 2a    /* Label "L" *
3080b 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  /.      int addr
3080c 49 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  If;          /* 
3080d 41 64 64 72 65 73 73 20 6f 66 20 6a 75 6d 70 20  Address of jump 
3080e 74 6f 20 4d 20 2a 2f 0a 0a 20 20 20 20 20 20 73  to M */..      s
3080f 72 63 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e  rcTab = pParse->
30810 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 72 65  nTab++;.      re
30811 67 52 65 63 20 3d 20 73 71 6c 69 74 65 33 47 65  gRec = sqlite3Ge
30812 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
30813 3b 0a 20 20 20 20 20 20 72 65 67 54 65 6d 70 52  ;.      regTempR
30814 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65  owid = sqlite3Ge
30815 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
30816 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
30817 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
30818 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 73  OpenEphemeral, s
30819 72 63 54 61 62 2c 20 6e 43 6f 6c 75 6d 6e 29 3b  rcTab, nColumn);
3081a 0a 20 20 20 20 20 20 61 64 64 72 54 6f 70 20 3d  .      addrTop =
3081b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
3081c 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
3081d 64 65 73 74 2e 69 53 44 50 61 72 6d 29 3b 0a 20  dest.iSDParm);. 
3081e 20 20 20 20 20 61 64 64 72 49 66 20 3d 20 73 71       addrIf = sq
3081f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
30820 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66  v, OP_If, regEof
30821 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
30822 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
30823 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
30824 46 72 6f 6d 53 65 6c 65 63 74 2c 20 6e 43 6f 6c  FromSelect, nCol
30825 75 6d 6e 2c 20 72 65 67 52 65 63 29 3b 0a 20 20  umn, regRec);.  
30826 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
30827 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
30828 6f 77 69 64 2c 20 73 72 63 54 61 62 2c 20 72 65  owid, srcTab, re
30829 67 54 65 6d 70 52 6f 77 69 64 29 3b 0a 20 20 20  gTempRowid);.   
3082a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
3082b 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
3082c 74 2c 20 73 72 63 54 61 62 2c 20 72 65 67 52 65  t, srcTab, regRe
3082d 63 2c 20 72 65 67 54 65 6d 70 52 6f 77 69 64 29  c, regTempRowid)
3082e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
3082f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
30830 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 54 6f 70  Goto, 0, addrTop
30831 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
30832 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
30833 61 64 64 72 49 66 29 3b 0a 20 20 20 20 20 20 73  addrIf);.      s
30834 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
30835 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
30836 52 65 63 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Rec);.      sqli
30837 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
30838 67 28 70 50 61 72 73 65 2c 20 72 65 67 54 65 6d  g(pParse, regTem
30839 70 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20  pRowid);.    }. 
3083a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
3083b 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 20  his is the case 
3083c 69 66 20 74 68 65 20 64 61 74 61 20 66 6f 72 20  if the data for 
3083d 74 68 65 20 49 4e 53 45 52 54 20 69 73 20 63 6f  the INSERT is co
3083e 6d 69 6e 67 20 66 72 6f 6d 20 61 20 56 41 4c 55  ming from a VALU
3083f 45 53 0a 20 20 20 20 2a 2a 20 63 6c 61 75 73 65  ES.    ** clause
30840 0a 20 20 20 20 2a 2f 0a 20 20 20 20 4e 61 6d 65  .    */.    Name
30841 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20  Context sNC;.   
30842 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c   memset(&sNC, 0,
30843 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20   sizeof(sNC));. 
30844 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20     sNC.pParse = 
30845 70 50 61 72 73 65 3b 0a 20 20 20 20 73 72 63 54  pParse;.    srcT
30846 61 62 20 3d 20 2d 31 3b 0a 20 20 20 20 61 73 73  ab = -1;.    ass
30847 65 72 74 28 20 75 73 65 54 65 6d 70 54 61 62 6c  ert( useTempTabl
30848 65 3d 3d 30 20 29 3b 0a 20 20 20 20 6e 43 6f 6c  e==0 );.    nCol
30849 75 6d 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c  umn = pList ? pL
3084a 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 0a  ist->nExpr : 0;.
3084b 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
3084c 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
3084d 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
3084e 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
3084f 26 73 4e 43 2c 20 70 4c 69 73 74 2d 3e 61 5b 69  &sNC, pList->a[i
30850 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ].pExpr) ){.    
30851 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
30852 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d  cleanup;.      }
30853 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
30854 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6e   Make sure the n
30855 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
30856 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   in the source d
30857 61 74 61 20 6d 61 74 63 68 65 73 20 74 68 65 20  ata matches the 
30858 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 63  number.  ** of c
30859 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 73  olumns to be ins
3085a 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74  erted into the t
3085b 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  able..  */.  if(
3085c 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
3085d 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
3085e 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
3085f 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 48 69 64 64  ++){.      nHidd
30860 65 6e 20 2b 3d 20 28 49 73 48 69 64 64 65 6e 43  en += (IsHiddenC
30861 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43 6f  olumn(&pTab->aCo
30862 6c 5b 69 5d 29 20 3f 20 31 20 3a 20 30 29 3b 0a  l[i]) ? 1 : 0);.
30863 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
30864 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 6e 43  pColumn==0 && nC
30865 6f 6c 75 6d 6e 20 26 26 20 6e 43 6f 6c 75 6d 6e  olumn && nColumn
30866 21 3d 28 70 54 61 62 2d 3e 6e 43 6f 6c 2d 6e 48  !=(pTab->nCol-nH
30867 69 64 64 65 6e 29 20 29 7b 0a 20 20 20 20 73 71  idden) ){.    sq
30868 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
30869 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 22 74  arse, .       "t
3086a 61 62 6c 65 20 25 53 20 68 61 73 20 25 64 20 63  able %S has %d c
3086b 6f 6c 75 6d 6e 73 20 62 75 74 20 25 64 20 76 61  olumns but %d va
3086c 6c 75 65 73 20 77 65 72 65 20 73 75 70 70 6c 69  lues were suppli
3086d 65 64 22 2c 0a 20 20 20 20 20 20 20 70 54 61 62  ed",.       pTab
3086e 4c 69 73 74 2c 20 30 2c 20 70 54 61 62 2d 3e 6e  List, 0, pTab->n
3086f 43 6f 6c 2d 6e 48 69 64 64 65 6e 2c 20 6e 43 6f  Col-nHidden, nCo
30870 6c 75 6d 6e 29 3b 0a 20 20 20 20 67 6f 74 6f 20  lumn);.    goto 
30871 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
30872 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d    }.  if( pColum
30873 6e 21 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 21  n!=0 && nColumn!
30874 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 20 29 7b  =pColumn->nId ){
30875 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
30876 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 64  rMsg(pParse, "%d
30877 20 76 61 6c 75 65 73 20 66 6f 72 20 25 64 20 63   values for %d c
30878 6f 6c 75 6d 6e 73 22 2c 20 6e 43 6f 6c 75 6d 6e  olumns", nColumn
30879 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 3b  , pColumn->nId);
3087a 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
3087b 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
3087c 20 2f 2a 20 49 66 20 74 68 65 20 49 4e 53 45 52   /* If the INSER
3087d 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 63 6c  T statement incl
3087e 75 64 65 64 20 61 6e 20 49 44 4c 49 53 54 20 74  uded an IDLIST t
3087f 65 72 6d 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73  erm, then make s
30880 75 72 65 0a 20 20 2a 2a 20 61 6c 6c 20 65 6c 65  ure.  ** all ele
30881 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 49 44 4c  ments of the IDL
30882 49 53 54 20 72 65 61 6c 6c 79 20 61 72 65 20 63  IST really are c
30883 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61  olumns of the ta
30884 62 6c 65 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65  ble and .  ** re
30885 6d 65 6d 62 65 72 20 74 68 65 20 63 6f 6c 75 6d  member the colum
30886 6e 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 0a  n indices..  **.
30887 20 20 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c    ** If the tabl
30888 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52  e has an INTEGER
30889 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c   PRIMARY KEY col
3088a 75 6d 6e 20 61 6e 64 20 74 68 61 74 20 63 6f 6c  umn and that col
3088b 75 6d 6e 0a 20 20 2a 2a 20 69 73 20 6e 61 6d 65  umn.  ** is name
3088c 64 20 69 6e 20 74 68 65 20 49 44 4c 49 53 54 2c  d in the IDLIST,
3088d 20 74 68 65 6e 20 72 65 63 6f 72 64 20 69 6e 20   then record in 
3088e 74 68 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76 61  the keyColumn va
3088f 72 69 61 62 6c 65 0a 20 20 2a 2a 20 74 68 65 20  riable.  ** the 
30890 69 6e 64 65 78 20 69 6e 74 6f 20 49 44 4c 49 53  index into IDLIS
30891 54 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79  T of the primary
30892 20 6b 65 79 20 63 6f 6c 75 6d 6e 2e 20 20 6b 65   key column.  ke
30893 79 43 6f 6c 75 6d 6e 20 69 73 0a 20 20 2a 2a 20  yColumn is.  ** 
30894 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
30895 20 70 72 69 6d 61 72 79 20 6b 65 79 20 61 73 20   primary key as 
30896 69 74 20 61 70 70 65 61 72 73 20 69 6e 20 49 44  it appears in ID
30897 4c 49 53 54 2c 20 6e 6f 74 20 61 73 0a 20 20 2a  LIST, not as.  *
30898 2a 20 69 73 20 61 70 70 65 61 72 73 20 69 6e 20  * is appears in 
30899 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62  the original tab
3089a 6c 65 2e 20 20 28 54 68 65 20 69 6e 64 65 78 20  le.  (The index 
3089b 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79 0a 20  of the primary. 
3089c 20 2a 2a 20 6b 65 79 20 69 6e 20 74 68 65 20 6f   ** key in the o
3089d 72 69 67 69 6e 61 6c 20 74 61 62 6c 65 20 69 73  riginal table is
3089e 20 70 54 61 62 2d 3e 69 50 4b 65 79 2e 29 0a 20   pTab->iPKey.). 
3089f 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d   */.  if( pColum
308a0 6e 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  n ){.    for(i=0
308a1 3b 20 69 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64  ; i<pColumn->nId
308a2 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 43  ; i++){.      pC
308a3 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20  olumn->a[i].idx 
308a4 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  = -1;.    }.    
308a5 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 6f 6c 75  for(i=0; i<pColu
308a6 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20  mn->nId; i++){. 
308a7 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
308a8 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29  pTab->nCol; j++)
308a9 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  {.        if( sq
308aa 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f  lite3StrICmp(pCo
308ab 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  lumn->a[i].zName
308ac 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e  , pTab->aCol[j].
308ad 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
308ae 20 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e         pColumn->
308af 61 5b 69 5d 2e 69 64 78 20 3d 20 6a 3b 0a 20 20  a[i].idx = j;.  
308b0 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 70          if( j==p
308b1 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
308b2 20 20 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c            keyCol
308b3 75 6d 6e 20 3d 20 69 3b 0a 20 20 20 20 20 20 20  umn = i;.       
308b4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62     }.          b
308b5 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
308b6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
308b7 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20  ( j>=pTab->nCol 
308b8 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  ){.        if( s
308b9 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 70 43  qlite3IsRowid(pC
308ba 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  olumn->a[i].zNam
308bb 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  e) ){.          
308bc 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a 20  keyColumn = i;. 
308bd 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
308be 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
308bf 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
308c0 22 74 61 62 6c 65 20 25 53 20 68 61 73 20 6e 6f  "table %S has no
308c1 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73   column named %s
308c2 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
308c3 20 70 54 61 62 4c 69 73 74 2c 20 30 2c 20 70 43   pTabList, 0, pC
308c4 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  olumn->a[i].zNam
308c5 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50  e);.          pP
308c6 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d  arse->checkSchem
308c7 61 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  a = 1;.         
308c8 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
308c9 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a  anup;.        }.
308ca 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
308cb 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  }..  /* If there
308cc 20 69 73 20 6e 6f 20 49 44 4c 49 53 54 20 74 65   is no IDLIST te
308cd 72 6d 20 62 75 74 20 74 68 65 20 74 61 62 6c 65  rm but the table
308ce 20 68 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20   has an integer 
308cf 70 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79  primary.  ** key
308d0 2c 20 74 68 65 20 73 65 74 20 74 68 65 20 6b 65  , the set the ke
308d1 79 43 6f 6c 75 6d 6e 20 76 61 72 69 61 62 6c 65  yColumn variable
308d2 20 74 6f 20 74 68 65 20 70 72 69 6d 61 72 79 20   to the primary 
308d3 6b 65 79 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78  key column index
308d4 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69  .  ** in the ori
308d5 67 69 6e 61 6c 20 74 61 62 6c 65 20 64 65 66 69  ginal table defi
308d6 6e 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69  nition..  */.  i
308d7 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26  f( pColumn==0 &&
308d8 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20   nColumn>0 ){.  
308d9 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 70 54    keyColumn = pT
308da 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 7d 0a 20  ab->iPKey;.  }. 
308db 20 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c     .  /* Initial
308dc 69 7a 65 20 74 68 65 20 63 6f 75 6e 74 20 6f 66  ize the count of
308dd 20 72 6f 77 73 20 74 6f 20 62 65 20 69 6e 73 65   rows to be inse
308de 72 74 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20  rted.  */.  if( 
308df 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
308e0 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a  TE_CountRows ){.
308e1 20 20 20 20 72 65 67 52 6f 77 43 6f 75 6e 74 20      regRowCount 
308e2 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
308e3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
308e4 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
308e5 74 65 67 65 72 2c 20 30 2c 20 72 65 67 52 6f 77  teger, 0, regRow
308e6 43 6f 75 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f  Count);.  }..  /
308e7 2a 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74  * If this is not
308e8 20 61 20 76 69 65 77 2c 20 6f 70 65 6e 20 74 68   a view, open th
308e9 65 20 74 61 62 6c 65 20 61 6e 64 20 61 6e 64 20  e table and and 
308ea 61 6c 6c 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20  all indices */. 
308eb 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a   if( !isView ){.
308ec 20 20 20 20 69 6e 74 20 6e 49 64 78 3b 0a 0a 20      int nIdx;.. 
308ed 20 20 20 62 61 73 65 43 75 72 20 3d 20 70 50 61     baseCur = pPa
308ee 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 6e  rse->nTab;.    n
308ef 49 64 78 20 3d 20 73 71 6c 69 74 65 33 4f 70 65  Idx = sqlite3Ope
308f0 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73  nTableAndIndices
308f1 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62  (pParse, pTab, b
308f2 61 73 65 43 75 72 2c 20 4f 50 5f 4f 70 65 6e 57  aseCur, OP_OpenW
308f3 72 69 74 65 29 3b 0a 20 20 20 20 61 52 65 67 49  rite);.    aRegI
308f4 64 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  dx = sqlite3DbMa
308f5 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
308f6 6f 66 28 69 6e 74 29 2a 28 6e 49 64 78 2b 31 29  of(int)*(nIdx+1)
308f7 29 3b 0a 20 20 20 20 69 66 28 20 61 52 65 67 49  );.    if( aRegI
308f8 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67  dx==0 ){.      g
308f9 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
308fa 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f  up;.    }.    fo
308fb 72 28 69 3d 30 3b 20 69 3c 6e 49 64 78 3b 20 69  r(i=0; i<nIdx; i
308fc 2b 2b 29 7b 0a 20 20 20 20 20 20 61 52 65 67 49  ++){.      aRegI
308fd 64 78 5b 69 5d 20 3d 20 2b 2b 70 50 61 72 73 65  dx[i] = ++pParse
308fe 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20  ->nMem;.    }.  
308ff 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20  }..  /* This is 
30900 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6d  the top of the m
30901 61 69 6e 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f  ain insertion lo
30902 6f 70 20 2a 2f 0a 20 20 69 66 28 20 75 73 65 54  op */.  if( useT
30903 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
30904 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f  /* This block co
30905 64 65 73 20 74 68 65 20 74 6f 70 20 6f 66 20 6c  des the top of l
30906 6f 6f 70 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63  oop only.  The c
30907 6f 6d 70 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20  omplete loop is 
30908 74 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f  the.    ** follo
30909 77 69 6e 67 20 70 73 65 75 64 6f 63 6f 64 65 20  wing pseudocode 
3090a 28 74 65 6d 70 6c 61 74 65 20 34 29 3a 0a 20 20  (template 4):.  
3090b 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
3090c 20 20 20 20 72 65 77 69 6e 64 20 74 65 6d 70 20      rewind temp 
3090d 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 20 20 20  table.    **    
3090e 20 20 43 3a 20 6c 6f 6f 70 20 6f 76 65 72 20 72    C: loop over r
3090f 6f 77 73 20 6f 66 20 69 6e 74 65 72 6d 65 64 69  ows of intermedi
30910 61 74 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a  ate table.    **
30911 20 20 20 20 20 20 20 20 20 20 20 74 72 61 6e 73             trans
30912 66 65 72 20 76 61 6c 75 65 73 20 66 6f 72 6d 20  fer values form 
30913 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62  intermediate tab
30914 6c 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a  le into <table>.
30915 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 65      **         e
30916 6e 64 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 20  nd loop.    **  
30917 20 20 20 20 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a      D: ....    *
30918 2f 0a 20 20 20 20 61 64 64 72 49 6e 73 54 6f 70  /.    addrInsTop
30919 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
3091a 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp1(v, OP_Rewin
3091b 64 2c 20 73 72 63 54 61 62 29 3b 0a 20 20 20 20  d, srcTab);.    
3091c 61 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74  addrCont = sqlit
3091d 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
3091e 72 28 76 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  r(v);.  }else if
3091f 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ( pSelect ){.   
30920 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63   /* This block c
30921 6f 64 65 73 20 74 68 65 20 74 6f 70 20 6f 66 20  odes the top of 
30922 6c 6f 6f 70 20 6f 6e 6c 79 2e 20 20 54 68 65 20  loop only.  The 
30923 63 6f 6d 70 6c 65 74 65 20 6c 6f 6f 70 20 69 73  complete loop is
30924 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c   the.    ** foll
30925 6f 77 69 6e 67 20 70 73 65 75 64 6f 63 6f 64 65  owing pseudocode
30926 20 28 74 65 6d 70 6c 61 74 65 20 33 29 3a 0a 20   (template 3):. 
30927 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
30928 20 20 43 3a 20 79 69 65 6c 64 20 58 0a 20 20 20    C: yield X.   
30929 20 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20 45   **         if E
3092a 4f 46 20 67 6f 74 6f 20 44 0a 20 20 20 20 2a 2a  OF goto D.    **
3092b 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 20           insert 
3092c 74 68 65 20 73 65 6c 65 63 74 20 72 65 73 75 6c  the select resul
3092d 74 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20 66  t into <table> f
3092e 72 6f 6d 20 52 2e 2e 52 2b 6e 0a 20 20 20 20 2a  rom R..R+n.    *
3092f 2a 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 43  *         goto C
30930 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 44 3a 20  .    **      D: 
30931 2e 2e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  ....    */.    a
30932 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65  ddrCont = sqlite
30933 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
30934 50 5f 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 53  P_Yield, dest.iS
30935 44 50 61 72 6d 29 3b 0a 20 20 20 20 61 64 64 72  DParm);.    addr
30936 49 6e 73 54 6f 70 20 3d 20 73 71 6c 69 74 65 33  InsTop = sqlite3
30937 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
30938 5f 49 66 2c 20 72 65 67 45 6f 66 29 3b 0a 20 20  _If, regEof);.  
30939 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
3093a 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 68   registers for h
3093b 6f 6c 64 69 6e 67 20 74 68 65 20 72 6f 77 69 64  olding the rowid
3093c 20 6f 66 20 74 68 65 20 6e 65 77 20 72 6f 77 2c   of the new row,
3093d 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  .  ** the conten
3093e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 72 6f 77  t of the new row
3093f 2c 20 61 6e 64 20 74 68 65 20 61 73 73 65 6d 62  , and the assemb
30940 6c 69 65 64 20 72 6f 77 20 72 65 63 6f 72 64 2e  lied row record.
30941 0a 20 20 2a 2f 0a 20 20 72 65 67 52 6f 77 69 64  .  */.  regRowid
30942 20 3d 20 72 65 67 49 6e 73 20 3d 20 70 50 61 72   = regIns = pPar
30943 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 70 50  se->nMem+1;.  pP
30944 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54  arse->nMem += pT
30945 61 62 2d 3e 6e 43 6f 6c 20 2b 20 31 3b 0a 20 20  ab->nCol + 1;.  
30946 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
30947 61 62 29 20 29 7b 0a 20 20 20 20 72 65 67 52 6f  ab) ){.    regRo
30948 77 69 64 2b 2b 3b 0a 20 20 20 20 70 50 61 72 73  wid++;.    pPars
30949 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 7d 0a 20  e->nMem++;.  }. 
3094a 20 72 65 67 44 61 74 61 20 3d 20 72 65 67 52 6f   regData = regRo
3094b 77 69 64 2b 31 3b 0a 0a 20 20 2f 2a 20 52 75 6e  wid+1;..  /* Run
3094c 20 74 68 65 20 42 45 46 4f 52 45 20 61 6e 64 20   the BEFORE and 
3094d 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67  INSTEAD OF trigg
3094e 65 72 73 2c 20 69 66 20 74 68 65 72 65 20 61 72  ers, if there ar
3094f 65 20 61 6e 79 0a 20 20 2a 2f 0a 20 20 65 6e 64  e any.  */.  end
30950 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33  OfLoop = sqlite3
30951 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
30952 3b 0a 20 20 69 66 28 20 74 6d 61 73 6b 20 26 20  ;.  if( tmask & 
30953 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 29  TRIGGER_BEFORE )
30954 7b 0a 20 20 20 20 69 6e 74 20 72 65 67 43 6f 6c  {.    int regCol
30955 73 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  s = sqlite3GetTe
30956 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
30957 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 0a  pTab->nCol+1);..
30958 20 20 20 20 2f 2a 20 62 75 69 6c 64 20 74 68 65      /* build the
30959 20 4e 45 57 2e 2a 20 72 65 66 65 72 65 6e 63 65   NEW.* reference
3095a 20 72 6f 77 2e 20 20 4e 6f 74 65 20 74 68 61 74   row.  Note that
3095b 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   if there is an 
3095c 49 4e 54 45 47 45 52 0a 20 20 20 20 2a 2a 20 50  INTEGER.    ** P
3095d 52 49 4d 41 52 59 20 4b 45 59 20 69 6e 74 6f 20  RIMARY KEY into 
3095e 77 68 69 63 68 20 61 20 4e 55 4c 4c 20 69 73 20  which a NULL is 
3095f 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 2c 20  being inserted, 
30960 74 68 61 74 20 4e 55 4c 4c 20 77 69 6c 6c 20 62  that NULL will b
30961 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61  e.    ** transla
30962 74 65 64 20 69 6e 74 6f 20 61 20 75 6e 69 71 75  ted into a uniqu
30963 65 20 49 44 20 66 6f 72 20 74 68 65 20 72 6f 77  e ID for the row
30964 2e 20 20 42 75 74 20 6f 6e 20 61 20 42 45 46 4f  .  But on a BEFO
30965 52 45 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20  RE trigger,.    
30966 2a 2a 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f  ** we do not kno
30967 77 20 77 68 61 74 20 74 68 65 20 75 6e 69 71 75  w what the uniqu
30968 65 20 49 44 20 77 69 6c 6c 20 62 65 20 28 62 65  e ID will be (be
30969 63 61 75 73 65 20 74 68 65 20 69 6e 73 65 72 74  cause the insert
3096a 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20   has.    ** not 
3096b 68 61 70 70 65 6e 65 64 20 79 65 74 29 20 73 6f  happened yet) so
3096c 20 77 65 20 73 75 62 73 74 69 74 75 74 65 20 61   we substitute a
3096d 20 72 6f 77 69 64 20 6f 66 20 2d 31 0a 20 20 20   rowid of -1.   
3096e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6b 65 79 43   */.    if( keyC
3096f 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20  olumn<0 ){.     
30970 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
30971 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
30972 2c 20 2d 31 2c 20 72 65 67 43 6f 6c 73 29 3b 0a  , -1, regCols);.
30973 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
30974 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 20 20 69   int j1;.      i
30975 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  f( useTempTable 
30976 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
30977 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
30978 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61  OP_Column, srcTa
30979 62 2c 20 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72 65  b, keyColumn, re
3097a 67 43 6f 6c 73 29 3b 0a 20 20 20 20 20 20 7d 65  gCols);.      }e
3097b 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
3097c 65 72 74 28 20 70 53 65 6c 65 63 74 3d 3d 30 20  ert( pSelect==0 
3097d 29 3b 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65  );  /* Otherwise
3097e 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 69 73   useTempTable is
3097f 20 74 72 75 65 20 2a 2f 0a 20 20 20 20 20 20 20   true */.       
30980 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
30981 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e  (pParse, pList->
30982 61 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e 70 45 78  a[keyColumn].pEx
30983 70 72 2c 20 72 65 67 43 6f 6c 73 29 3b 0a 20 20  pr, regCols);.  
30984 20 20 20 20 7d 0a 20 20 20 20 20 20 6a 31 20 3d      }.      j1 =
30985 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
30986 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c  p1(v, OP_NotNull
30987 2c 20 72 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20  , regCols);.    
30988 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
30989 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
3098a 72 2c 20 2d 31 2c 20 72 65 67 43 6f 6c 73 29 3b  r, -1, regCols);
3098b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
3098c 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
3098d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
3098e 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
3098f 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 65 67 43  _MustBeInt, regC
30990 6f 6c 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ols);.    }..   
30991 20 2f 2a 20 43 61 6e 6e 6f 74 20 68 61 76 65 20   /* Cannot have 
30992 74 72 69 67 67 65 72 73 20 6f 6e 20 61 20 76 69  triggers on a vi
30993 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 49 66 20  rtual table. If 
30994 69 74 20 77 65 72 65 20 70 6f 73 73 69 62 6c 65  it were possible
30995 2c 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 62 6c  ,.    ** this bl
30996 6f 63 6b 20 77 6f 75 6c 64 20 68 61 76 65 20 74  ock would have t
30997 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 20 68 69  o account for hi
30998 64 64 65 6e 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20  dden column..   
30999 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
3099a 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29  !IsVirtual(pTab)
3099b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61   );..    /* Crea
3099c 74 65 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d  te the new colum
3099d 6e 20 64 61 74 61 0a 20 20 20 20 2a 2f 0a 20 20  n data.    */.  
3099e 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
3099f 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  b->nCol; i++){. 
309a0 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e       if( pColumn
309a1 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6a  ==0 ){.        j
309a2 20 3d 20 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = i;.      }els
309a3 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a  e{.        for(j
309a4 3d 30 3b 20 6a 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e  =0; j<pColumn->n
309a5 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  Id; j++){.      
309a6 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 2d      if( pColumn-
309a7 3e 61 5b 6a 5d 2e 69 64 78 3d 3d 69 20 29 20 62  >a[j].idx==i ) b
309a8 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
309a9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
309aa 28 20 28 21 75 73 65 54 65 6d 70 54 61 62 6c 65  ( (!useTempTable
309ab 20 26 26 20 21 70 4c 69 73 74 29 20 7c 7c 20 28   && !pList) || (
309ac 70 43 6f 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70 43  pColumn && j>=pC
309ad 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 20 29 7b 0a 20  olumn->nId) ){. 
309ae 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
309af 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
309b0 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66  Tab->aCol[i].pDf
309b1 6c 74 2c 20 72 65 67 43 6f 6c 73 2b 69 2b 31 29  lt, regCols+i+1)
309b2 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
309b3 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29  ( useTempTable )
309b4 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
309b5 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
309b6 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62  P_Column, srcTab
309b7 2c 20 6a 2c 20 72 65 67 43 6f 6c 73 2b 69 2b 31  , j, regCols+i+1
309b8 29 3b 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ); .      }else{
309b9 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
309ba 20 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 2f   pSelect==0 ); /
309bb 2a 20 4f 74 68 65 72 77 69 73 65 20 75 73 65 54  * Otherwise useT
309bc 65 6d 70 54 61 62 6c 65 20 69 73 20 74 72 75 65  empTable is true
309bd 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69   */.        sqli
309be 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64 43 61  te3ExprCodeAndCa
309bf 63 68 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73  che(pParse, pLis
309c0 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 72  t->a[j].pExpr, r
309c1 65 67 43 6f 6c 73 2b 69 2b 31 29 3b 0a 20 20 20  egCols+i+1);.   
309c2 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
309c3 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  /* If this is an
309c4 20 49 4e 53 45 52 54 20 6f 6e 20 61 20 76 69 65   INSERT on a vie
309c5 77 20 77 69 74 68 20 61 6e 20 49 4e 53 54 45 41  w with an INSTEA
309c6 44 20 4f 46 20 49 4e 53 45 52 54 20 74 72 69 67  D OF INSERT trig
309c7 67 65 72 2c 0a 20 20 20 20 2a 2a 20 64 6f 20 6e  ger,.    ** do n
309c8 6f 74 20 61 74 74 65 6d 70 74 20 61 6e 79 20 63  ot attempt any c
309c9 6f 6e 76 65 72 73 69 6f 6e 73 20 62 65 66 6f 72  onversions befor
309ca 65 20 61 73 73 65 6d 62 6c 69 6e 67 20 74 68 65  e assembling the
309cb 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2a 20   record..    ** 
309cc 49 66 20 74 68 69 73 20 69 73 20 61 20 72 65 61  If this is a rea
309cd 6c 20 74 61 62 6c 65 2c 20 61 74 74 65 6d 70 74  l table, attempt
309ce 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73 20   conversions as 
309cf 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 0a  required by the.
309d0 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 63 6f 6c      ** table col
309d1 75 6d 6e 20 61 66 66 69 6e 69 74 69 65 73 2e 0a  umn affinities..
309d2 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
309d3 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20  isView ){.      
309d4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
309d5 32 28 76 2c 20 4f 50 5f 41 66 66 69 6e 69 74 79  2(v, OP_Affinity
309d6 2c 20 72 65 67 43 6f 6c 73 2b 31 2c 20 70 54 61  , regCols+1, pTa
309d7 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20  b->nCol);.      
309d8 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69  sqlite3TableAffi
309d9 6e 69 74 79 53 74 72 28 76 2c 20 70 54 61 62 29  nityStr(v, pTab)
309da 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
309db 46 69 72 65 20 42 45 46 4f 52 45 20 6f 72 20 49  Fire BEFORE or I
309dc 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65  NSTEAD OF trigge
309dd 72 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  rs */.    sqlite
309de 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
309df 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
309e0 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c 20  , TK_INSERT, 0, 
309e1 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20  TRIGGER_BEFORE, 
309e2 0a 20 20 20 20 20 20 20 20 70 54 61 62 2c 20 72  .        pTab, r
309e3 65 67 43 6f 6c 73 2d 70 54 61 62 2d 3e 6e 43 6f  egCols-pTab->nCo
309e4 6c 2d 31 2c 20 6f 6e 45 72 72 6f 72 2c 20 65 6e  l-1, onError, en
309e5 64 4f 66 4c 6f 6f 70 29 3b 0a 0a 20 20 20 20 73  dOfLoop);..    s
309e6 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
309e7 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  pRange(pParse, r
309e8 65 67 43 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43  egCols, pTab->nC
309e9 6f 6c 2b 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ol+1);.  }..  /*
309ea 20 50 75 73 68 20 74 68 65 20 72 65 63 6f 72 64   Push the record
309eb 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
309ec 6e 65 77 20 65 6e 74 72 79 20 6f 6e 74 6f 20 74  new entry onto t
309ed 68 65 20 73 74 61 63 6b 2e 20 20 54 68 65 0a 20  he stack.  The. 
309ee 20 2a 2a 20 72 65 63 6f 72 64 20 6e 75 6d 62 65   ** record numbe
309ef 72 20 69 73 20 61 20 72 61 6e 64 6f 6d 6c 79 20  r is a randomly 
309f0 67 65 6e 65 72 61 74 65 20 69 6e 74 65 67 65 72  generate integer
309f1 20 63 72 65 61 74 65 64 20 62 79 20 4e 65 77 52   created by NewR
309f2 6f 77 69 64 0a 20 20 2a 2a 20 65 78 63 65 70 74  owid.  ** except
309f3 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20   when the table 
309f4 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50  has an INTEGER P
309f5 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d  RIMARY KEY colum
309f6 6e 2c 20 69 6e 20 77 68 69 63 68 0a 20 20 2a 2a  n, in which.  **
309f7 20 63 61 73 65 20 74 68 65 20 72 65 63 6f 72 64   case the record
309f8 20 6e 75 6d 62 65 72 20 69 73 20 74 68 65 20 73   number is the s
309f9 61 6d 65 20 61 73 20 74 68 61 74 20 63 6f 6c 75  ame as that colu
309fa 6d 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  mn. .  */.  if( 
309fb 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 69  !isView ){.    i
309fc 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
309fd 62 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  b) ){.      /* T
309fe 68 65 20 72 6f 77 20 74 68 61 74 20 74 68 65 20  he row that the 
309ff 56 55 70 64 61 74 65 20 6f 70 63 6f 64 65 20 77  VUpdate opcode w
30a00 69 6c 6c 20 64 65 6c 65 74 65 3a 20 6e 6f 6e 65  ill delete: none
30a01 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
30a02 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
30a03 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 49 6e  P_Null, 0, regIn
30a04 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  s);.    }.    if
30a05 28 20 6b 65 79 43 6f 6c 75 6d 6e 3e 3d 30 20 29  ( keyColumn>=0 )
30a06 7b 0a 20 20 20 20 20 20 69 66 28 20 75 73 65 54  {.      if( useT
30a07 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
30a08 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
30a09 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
30a0a 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65 79 43  mn, srcTab, keyC
30a0b 6f 6c 75 6d 6e 2c 20 72 65 67 52 6f 77 69 64 29  olumn, regRowid)
30a0c 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
30a0d 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ( pSelect ){.   
30a0e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
30a0f 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f  AddOp2(v, OP_SCo
30a10 70 79 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65 63  py, regFromSelec
30a11 74 2b 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72 65 67  t+keyColumn, reg
30a12 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 7d 65  Rowid);.      }e
30a13 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 56 64 62  lse{.        Vdb
30a14 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20  eOp *pOp;.      
30a15 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
30a16 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d  e(pParse, pList-
30a17 3e 61 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e 70 45  >a[keyColumn].pE
30a18 78 70 72 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  xpr, regRowid);.
30a19 20 20 20 20 20 20 20 20 70 4f 70 20 3d 20 73 71          pOp = sq
30a1a 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76  lite3VdbeGetOp(v
30a1b 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 69  , -1);.        i
30a1c 66 28 20 41 4c 57 41 59 53 28 70 4f 70 29 20 26  f( ALWAYS(pOp) &
30a1d 26 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  & pOp->opcode==O
30a1e 50 5f 4e 75 6c 6c 20 26 26 20 21 49 73 56 69 72  P_Null && !IsVir
30a1f 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
30a20 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 46 6c          appendFl
30a21 61 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ag = 1;.        
30a22 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20    pOp->opcode = 
30a23 4f 50 5f 4e 65 77 52 6f 77 69 64 3b 0a 20 20 20  OP_NewRowid;.   
30a24 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d         pOp->p1 =
30a25 20 62 61 73 65 43 75 72 3b 0a 20 20 20 20 20 20   baseCur;.      
30a26 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 72 65      pOp->p2 = re
30a27 67 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 20 20  gRowid;.        
30a28 20 20 70 4f 70 2d 3e 70 33 20 3d 20 72 65 67 41    pOp->p3 = regA
30a29 75 74 6f 69 6e 63 3b 0a 20 20 20 20 20 20 20 20  utoinc;.        
30a2a 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
30a2b 2f 2a 20 49 66 20 74 68 65 20 50 52 49 4d 41 52  /* If the PRIMAR
30a2c 59 20 4b 45 59 20 65 78 70 72 65 73 73 69 6f 6e  Y KEY expression
30a2d 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 75   is NULL, then u
30a2e 73 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 0a 20  se OP_NewRowid. 
30a2f 20 20 20 20 20 2a 2a 20 74 6f 20 67 65 6e 65 72       ** to gener
30a30 61 74 65 20 61 20 75 6e 69 71 75 65 20 70 72 69  ate a unique pri
30a31 6d 61 72 79 20 6b 65 79 20 76 61 6c 75 65 2e 0a  mary key value..
30a32 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
30a33 66 28 20 21 61 70 70 65 6e 64 46 6c 61 67 20 29  f( !appendFlag )
30a34 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 31  {.        int j1
30a35 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 49  ;.        if( !I
30a36 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
30a37 7b 0a 20 20 20 20 20 20 20 20 20 20 6a 31 20 3d  {.          j1 =
30a38 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
30a39 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c  p1(v, OP_NotNull
30a3a 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
30a3b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
30a3c 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
30a3d 65 77 52 6f 77 69 64 2c 20 62 61 73 65 43 75 72  ewRowid, baseCur
30a3e 2c 20 72 65 67 52 6f 77 69 64 2c 20 72 65 67 41  , regRowid, regA
30a3f 75 74 6f 69 6e 63 29 3b 0a 20 20 20 20 20 20 20  utoinc);.       
30a40 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
30a41 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20  mpHere(v, j1);. 
30a42 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
30a43 20 20 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c          j1 = sql
30a44 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
30a45 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 20 20  ddr(v);.        
30a46 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
30a47 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c  Op2(v, OP_IsNull
30a48 2c 20 72 65 67 52 6f 77 69 64 2c 20 6a 31 2b 32  , regRowid, j1+2
30a49 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
30a4a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
30a4b 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73  AddOp1(v, OP_Mus
30a4c 74 42 65 49 6e 74 2c 20 72 65 67 52 6f 77 69 64  tBeInt, regRowid
30a4d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
30a4e 65 6c 73 65 20 69 66 28 20 49 73 56 69 72 74 75  else if( IsVirtu
30a4f 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
30a50 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
30a51 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
30a52 30 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  0, regRowid);.  
30a53 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
30a54 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
30a55 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c  (v, OP_NewRowid,
30a56 20 62 61 73 65 43 75 72 2c 20 72 65 67 52 6f 77   baseCur, regRow
30a57 69 64 2c 20 72 65 67 41 75 74 6f 69 6e 63 29 3b  id, regAutoinc);
30a58 0a 20 20 20 20 20 20 61 70 70 65 6e 64 46 6c 61  .      appendFla
30a59 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  g = 1;.    }.   
30a5a 20 61 75 74 6f 49 6e 63 53 74 65 70 28 70 50 61   autoIncStep(pPa
30a5b 72 73 65 2c 20 72 65 67 41 75 74 6f 69 6e 63 2c  rse, regAutoinc,
30a5c 20 72 65 67 52 6f 77 69 64 29 3b 0a 0a 20 20 20   regRowid);..   
30a5d 20 2f 2a 20 50 75 73 68 20 6f 6e 74 6f 20 74 68   /* Push onto th
30a5e 65 20 73 74 61 63 6b 2c 20 64 61 74 61 20 66 6f  e stack, data fo
30a5f 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 6f 66  r all columns of
30a60 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 2c 20   the new entry, 
30a61 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a  beginning.    **
30a62 20 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20   with the first 
30a63 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20  column..    */. 
30a64 20 20 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b 0a     nHidden = 0;.
30a65 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
30a66 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Tab->nCol; i++){
30a67 0a 20 20 20 20 20 20 69 6e 74 20 69 52 65 67 53  .      int iRegS
30a68 74 6f 72 65 20 3d 20 72 65 67 52 6f 77 69 64 2b  tore = regRowid+
30a69 31 2b 69 3b 0a 20 20 20 20 20 20 69 66 28 20 69  1+i;.      if( i
30a6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b  ==pTab->iPKey ){
30a6b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
30a6c 76 61 6c 75 65 20 6f 66 20 74 68 65 20 49 4e 54  value of the INT
30a6d 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
30a6e 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6c 77 61 79   column is alway
30a6f 73 20 61 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20  s a NULL..      
30a70 20 20 2a 2a 20 57 68 65 6e 65 76 65 72 20 74 68    ** Whenever th
30a71 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 61  is column is rea
30a72 64 2c 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75  d, the record nu
30a73 6d 62 65 72 20 77 69 6c 6c 20 62 65 20 73 75 62  mber will be sub
30a74 73 74 69 74 75 74 65 64 0a 20 20 20 20 20 20 20  stituted.       
30a75 20 2a 2a 20 69 6e 20 69 74 73 20 70 6c 61 63 65   ** in its place
30a76 2e 20 20 53 6f 20 77 69 6c 6c 20 66 69 6c 6c 20  .  So will fill 
30a77 74 68 69 73 20 63 6f 6c 75 6d 6e 20 77 69 74 68  this column with
30a78 20 61 20 4e 55 4c 4c 20 74 6f 20 61 76 6f 69 64   a NULL to avoid
30a79 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 6b 69  .        ** taki
30a7a 6e 67 20 75 70 20 64 61 74 61 20 73 70 61 63 65  ng up data space
30a7b 20 77 69 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f   with informatio
30a7c 6e 20 74 68 61 74 20 77 69 6c 6c 20 6e 65 76 65  n that will neve
30a7d 72 20 62 65 20 75 73 65 64 2e 20 2a 2f 0a 20 20  r be used. */.  
30a7e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
30a7f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
30a80 6c 6c 2c 20 30 2c 20 69 52 65 67 53 74 6f 72 65  ll, 0, iRegStore
30a81 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69  );.        conti
30a82 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nue;.      }.   
30a83 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d     if( pColumn==
30a84 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  0 ){.        if(
30a85 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28   IsHiddenColumn(
30a86 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20  &pTab->aCol[i]) 
30a87 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  ){.          ass
30a88 65 72 74 28 20 49 73 56 69 72 74 75 61 6c 28 70  ert( IsVirtual(p
30a89 54 61 62 29 20 29 3b 0a 20 20 20 20 20 20 20 20  Tab) );.        
30a8a 20 20 6a 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20    j = -1;.      
30a8b 20 20 20 20 6e 48 69 64 64 65 6e 2b 2b 3b 0a 20      nHidden++;. 
30a8c 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
30a8d 20 20 20 20 20 20 20 20 6a 20 3d 20 69 20 2d 20          j = i - 
30a8e 6e 48 69 64 64 65 6e 3b 0a 20 20 20 20 20 20 20  nHidden;.       
30a8f 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
30a90 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
30a91 20 6a 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b   j<pColumn->nId;
30a92 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   j++){.         
30a93 20 69 66 28 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b   if( pColumn->a[
30a94 6a 5d 2e 69 64 78 3d 3d 69 20 29 20 62 72 65 61  j].idx==i ) brea
30a95 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
30a96 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a     }.      if( j
30a97 3c 30 20 7c 7c 20 6e 43 6f 6c 75 6d 6e 3d 3d 30  <0 || nColumn==0
30a98 20 7c 7c 20 28 70 43 6f 6c 75 6d 6e 20 26 26 20   || (pColumn && 
30a99 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29  j>=pColumn->nId)
30a9a 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
30a9b 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
30a9c 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  se, pTab->aCol[i
30a9d 5d 2e 70 44 66 6c 74 2c 20 69 52 65 67 53 74 6f  ].pDflt, iRegSto
30a9e 72 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  re);.      }else
30a9f 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c   if( useTempTabl
30aa0 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e ){.        sql
30aa1 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
30aa2 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63  , OP_Column, src
30aa3 54 61 62 2c 20 6a 2c 20 69 52 65 67 53 74 6f 72  Tab, j, iRegStor
30aa4 65 29 3b 20 0a 20 20 20 20 20 20 7d 65 6c 73 65  e); .      }else
30aa5 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a   if( pSelect ){.
30aa6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
30aa7 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
30aa8 53 43 6f 70 79 2c 20 72 65 67 46 72 6f 6d 53 65  SCopy, regFromSe
30aa9 6c 65 63 74 2b 6a 2c 20 69 52 65 67 53 74 6f 72  lect+j, iRegStor
30aaa 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  e);.      }else{
30aab 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
30aac 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
30aad 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78   pList->a[j].pEx
30aae 70 72 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 0a  pr, iRegStore);.
30aaf 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
30ab0 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
30ab1 6f 64 65 20 74 6f 20 63 68 65 63 6b 20 63 6f 6e  ode to check con
30ab2 73 74 72 61 69 6e 74 73 20 61 6e 64 20 67 65 6e  straints and gen
30ab3 65 72 61 74 65 20 69 6e 64 65 78 20 6b 65 79 73  erate index keys
30ab4 20 61 6e 64 0a 20 20 20 20 2a 2a 20 64 6f 20 74   and.    ** do t
30ab5 68 65 20 69 6e 73 65 72 74 69 6f 6e 2e 0a 20 20  he insertion..  
30ab6 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
30ab7 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
30ab8 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 49 73  TABLE.    if( Is
30ab9 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
30aba 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
30abb 72 20 2a 70 56 54 61 62 20 3d 20 28 63 6f 6e 73  r *pVTab = (cons
30abc 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
30abd 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54  GetVTable(db, pT
30abe 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ab);.      sqlit
30abf 65 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62  e3VtabMakeWritab
30ac0 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  le(pParse, pTab)
30ac1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
30ac2 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
30ac3 56 55 70 64 61 74 65 2c 20 31 2c 20 70 54 61 62  VUpdate, 1, pTab
30ac4 2d 3e 6e 43 6f 6c 2b 32 2c 20 72 65 67 49 6e 73  ->nCol+2, regIns
30ac5 2c 20 70 56 54 61 62 2c 20 50 34 5f 56 54 41 42  , pVTab, P4_VTAB
30ac6 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
30ac7 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
30ac8 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61  onError==OE_Defa
30ac9 75 6c 74 20 3f 20 4f 45 5f 41 62 6f 72 74 20 3a  ult ? OE_Abort :
30aca 20 6f 6e 45 72 72 6f 72 29 3b 0a 20 20 20 20 20   onError);.     
30acb 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74   sqlite3MayAbort
30acc 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 65  (pParse);.    }e
30acd 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b  lse.#endif.    {
30ace 0a 20 20 20 20 20 20 69 6e 74 20 69 73 52 65 70  .      int isRep
30acf 6c 61 63 65 3b 20 20 20 20 2f 2a 20 53 65 74 20  lace;    /* Set 
30ad0 74 6f 20 74 72 75 65 20 69 66 20 63 6f 6e 73 74  to true if const
30ad1 72 61 69 6e 74 73 20 6d 61 79 20 63 61 75 73 65  raints may cause
30ad2 20 61 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 20   a replace */.  
30ad3 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72      sqlite3Gener
30ad4 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65  ateConstraintChe
30ad5 63 6b 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  cks(pParse, pTab
30ad6 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 49 6e  , baseCur, regIn
30ad7 73 2c 20 61 52 65 67 49 64 78 2c 0a 20 20 20 20  s, aRegIdx,.    
30ad8 20 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d 6e 3e        keyColumn>
30ad9 3d 30 2c 20 30 2c 20 6f 6e 45 72 72 6f 72 2c 20  =0, 0, onError, 
30ada 65 6e 64 4f 66 4c 6f 6f 70 2c 20 26 69 73 52 65  endOfLoop, &isRe
30adb 70 6c 61 63 65 0a 20 20 20 20 20 20 29 3b 0a 20  place.      );. 
30adc 20 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43 68       sqlite3FkCh
30add 65 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62  eck(pParse, pTab
30ade 2c 20 30 2c 20 72 65 67 49 6e 73 29 3b 0a 20 20  , 0, regIns);.  
30adf 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c      sqlite3Compl
30ae0 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 0a 20 20  eteInsertion(.  
30ae1 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20          pParse, 
30ae2 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 72  pTab, baseCur, r
30ae3 65 67 49 6e 73 2c 20 61 52 65 67 49 64 78 2c 20  egIns, aRegIdx, 
30ae4 30 2c 20 61 70 70 65 6e 64 46 6c 61 67 2c 20 69  0, appendFlag, i
30ae5 73 52 65 70 6c 61 63 65 3d 3d 30 0a 20 20 20 20  sReplace==0.    
30ae6 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a    );.    }.  }..
30ae7 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
30ae8 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 74 68  count of rows th
30ae9 61 74 20 61 72 65 20 69 6e 73 65 72 74 65 64 0a  at are inserted.
30aea 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e    */.  if( (db->
30aeb 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43  flags & SQLITE_C
30aec 6f 75 6e 74 52 6f 77 73 29 21 3d 30 20 29 7b 0a  ountRows)!=0 ){.
30aed 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
30aee 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
30aef 6d 6d 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c  mm, regRowCount,
30af0 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20   1);.  }..  if( 
30af1 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
30af2 2f 2a 20 43 6f 64 65 20 41 46 54 45 52 20 74 72  /* Code AFTER tr
30af3 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 73 71  iggers */.    sq
30af4 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67  lite3CodeRowTrig
30af5 67 65 72 28 70 50 61 72 73 65 2c 20 70 54 72 69  ger(pParse, pTri
30af6 67 67 65 72 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  gger, TK_INSERT,
30af7 20 30 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45   0, TRIGGER_AFTE
30af8 52 2c 20 0a 20 20 20 20 20 20 20 20 70 54 61 62  R, .        pTab
30af9 2c 20 72 65 67 44 61 74 61 2d 32 2d 70 54 61 62  , regData-2-pTab
30afa 2d 3e 6e 43 6f 6c 2c 20 6f 6e 45 72 72 6f 72 2c  ->nCol, onError,
30afb 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 7d   endOfLoop);.  }
30afc 0a 0a 20 20 2f 2a 20 54 68 65 20 62 6f 74 74 6f  ..  /* The botto
30afd 6d 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 69 6e  m of the main in
30afe 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 2c 20 69 66  sertion loop, if
30aff 20 74 68 65 20 64 61 74 61 20 73 6f 75 72 63 65   the data source
30b00 0a 20 20 2a 2a 20 69 73 20 61 20 53 45 4c 45 43  .  ** is a SELEC
30b01 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 2a  T statement..  *
30b02 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  /.  sqlite3VdbeR
30b03 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65  esolveLabel(v, e
30b04 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 69 66 28  ndOfLoop);.  if(
30b05 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b   useTempTable ){
30b06 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
30b07 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
30b08 74 2c 20 73 72 63 54 61 62 2c 20 61 64 64 72 43  t, srcTab, addrC
30b09 6f 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ont);.    sqlite
30b0a 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
30b0b 20 61 64 64 72 49 6e 73 54 6f 70 29 3b 0a 20 20   addrInsTop);.  
30b0c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
30b0d 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op1(v, OP_Close,
30b0e 20 73 72 63 54 61 62 29 3b 0a 20 20 7d 65 6c 73   srcTab);.  }els
30b0f 65 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b  e if( pSelect ){
30b10 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
30b11 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
30b12 6f 2c 20 30 2c 20 61 64 64 72 43 6f 6e 74 29 3b  o, 0, addrCont);
30b13 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
30b14 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
30b15 49 6e 73 54 6f 70 29 3b 0a 20 20 7d 0a 0a 20 20  InsTop);.  }..  
30b16 69 66 28 20 21 49 73 56 69 72 74 75 61 6c 28 70  if( !IsVirtual(p
30b17 54 61 62 29 20 26 26 20 21 69 73 56 69 65 77 20  Tab) && !isView 
30b18 29 7b 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65 20  ){.    /* Close 
30b19 61 6c 6c 20 74 61 62 6c 65 73 20 6f 70 65 6e 65  all tables opene
30b1a 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  d */.    sqlite3
30b1b 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
30b1c 5f 43 6c 6f 73 65 2c 20 62 61 73 65 43 75 72 29  _Close, baseCur)
30b1d 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d 31 2c  ;.    for(idx=1,
30b1e 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64   pIdx=pTab->pInd
30b1f 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
30b20 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 64 78 2b  Idx->pNext, idx+
30b21 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
30b22 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
30b23 50 5f 43 6c 6f 73 65 2c 20 69 64 78 2b 62 61 73  P_Close, idx+bas
30b24 65 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  eCur);.    }.  }
30b25 0a 0a 69 6e 73 65 72 74 5f 65 6e 64 3a 0a 20 20  ..insert_end:.  
30b26 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 71  /* Update the sq
30b27 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61  lite_sequence ta
30b28 62 6c 65 20 62 79 20 73 74 6f 72 69 6e 67 20 74  ble by storing t
30b29 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  he content of th
30b2a 65 0a 20 20 2a 2a 20 6d 61 78 69 6d 75 6d 20 72  e.  ** maximum r
30b2b 6f 77 69 64 20 63 6f 75 6e 74 65 72 20 76 61 6c  owid counter val
30b2c 75 65 73 20 72 65 63 6f 72 64 65 64 20 77 68 69  ues recorded whi
30b2d 6c 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74  le inserting int
30b2e 6f 0a 20 20 2a 2a 20 61 75 74 6f 69 6e 63 72 65  o.  ** autoincre
30b2f 6d 65 6e 74 20 74 61 62 6c 65 73 2e 0a 20 20 2a  ment tables..  *
30b30 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  /.  if( pParse->
30b31 6e 65 73 74 65 64 3d 3d 30 20 26 26 20 70 50 61  nested==0 && pPa
30b32 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62  rse->pTriggerTab
30b33 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
30b34 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45  e3AutoincrementE
30b35 6e 64 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a  nd(pParse);.  }.
30b36 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72  .  /*.  ** Retur
30b37 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
30b38 72 6f 77 73 20 69 6e 73 65 72 74 65 64 2e 20 49  rows inserted. I
30b39 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  f this routine i
30b3a 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74 69  s .  ** generati
30b3b 6e 67 20 63 6f 64 65 20 62 65 63 61 75 73 65 20  ng code because 
30b3c 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c  of a call to sql
30b3d 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
30b3e 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69  ), do not.  ** i
30b3f 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61  nvoke the callba
30b40 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a  ck function..  *
30b41 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61  /.  if( (db->fla
30b42 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52  gs&SQLITE_CountR
30b43 6f 77 73 29 20 26 26 20 21 70 50 61 72 73 65 2d  ows) && !pParse-
30b44 3e 6e 65 73 74 65 64 20 26 26 20 21 70 50 61 72  >nested && !pPar
30b45 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20  se->pTriggerTab 
30b46 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
30b47 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
30b48 65 73 75 6c 74 52 6f 77 2c 20 72 65 67 52 6f 77  esultRow, regRow
30b49 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20 20 20 73  Count, 1);.    s
30b4a 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
30b4b 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
30b4c 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
30b4d 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
30b4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 20  AME_NAME, "rows 
30b4f 69 6e 73 65 72 74 65 64 22 2c 20 53 51 4c 49 54  inserted", SQLIT
30b50 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 0a  E_STATIC);.  }..
30b51 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3a 0a  insert_cleanup:.
30b52 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
30b53 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 4c  Delete(db, pTabL
30b54 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ist);.  sqlite3E
30b55 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
30b56 2c 20 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69  , pList);.  sqli
30b57 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
30b58 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20  db, pSelect);.  
30b59 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c  sqlite3IdListDel
30b5a 65 74 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 29  ete(db, pColumn)
30b5b 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
30b5c 65 28 64 62 2c 20 61 52 65 67 49 64 78 29 3b 0a  e(db, aRegIdx);.
30b5d 7d 0a 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  }../* Make sure 
30b5e 22 69 73 56 69 65 77 22 20 61 6e 64 20 6f 74 68  "isView" and oth
30b5f 65 72 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65  er macros define
30b60 64 20 61 62 6f 76 65 20 61 72 65 20 75 6e 64 65  d above are unde
30b61 66 69 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  fined. Otherwise
30b62 0a 2a 2a 20 74 68 65 6c 79 20 6d 61 79 20 69 6e  .** thely may in
30b63 74 65 72 66 65 72 65 20 77 69 74 68 20 63 6f 6d  terfere with com
30b64 70 69 6c 61 74 69 6f 6e 20 6f 66 20 6f 74 68 65  pilation of othe
30b65 72 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74  r functions in t
30b66 68 69 73 20 66 69 6c 65 0a 2a 2a 20 28 6f 72 20  his file.** (or 
30b67 69 6e 20 61 6e 6f 74 68 65 72 20 66 69 6c 65 2c  in another file,
30b68 20 69 66 20 74 68 69 73 20 66 69 6c 65 20 62 65   if this file be
30b69 63 6f 6d 65 73 20 70 61 72 74 20 6f 66 20 74 68  comes part of th
30b6a 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 29 2e  e amalgamation).
30b6b 20 20 2a 2f 0a 23 69 66 64 65 66 20 69 73 56 69    */.#ifdef isVi
30b6c 65 77 0a 20 23 75 6e 64 65 66 20 69 73 56 69 65  ew. #undef isVie
30b6d 77 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  w.#endif.#ifdef 
30b6e 70 54 72 69 67 67 65 72 0a 20 23 75 6e 64 65 66  pTrigger. #undef
30b6f 20 70 54 72 69 67 67 65 72 0a 23 65 6e 64 69 66   pTrigger.#endif
30b70 0a 23 69 66 64 65 66 20 74 6d 61 73 6b 0a 20 23  .#ifdef tmask. #
30b71 75 6e 64 65 66 20 74 6d 61 73 6b 0a 23 65 6e 64  undef tmask.#end
30b72 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  if.../*.** Gener
30b73 61 74 65 20 63 6f 64 65 20 74 6f 20 64 6f 20 63  ate code to do c
30b74 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b 73  onstraint checks
30b75 20 70 72 69 6f 72 20 74 6f 20 61 6e 20 49 4e 53   prior to an INS
30b76 45 52 54 20 6f 72 20 61 6e 20 55 50 44 41 54 45  ERT or an UPDATE
30b77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75  ..**.** The inpu
30b78 74 20 69 73 20 61 20 72 61 6e 67 65 20 6f 66 20  t is a range of 
30b79 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67 69  consecutive regi
30b7a 73 74 65 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73  sters as follows
30b7b 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 31 2e 20 20 54  :.**.**    1.  T
30b7c 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  he rowid of the 
30b7d 72 6f 77 20 61 66 74 65 72 20 74 68 65 20 75 70  row after the up
30b7e 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 32  date..**.**    2
30b7f 2e 20 20 54 68 65 20 64 61 74 61 20 69 6e 20 74  .  The data in t
30b80 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20  he first column 
30b81 6f 66 20 74 68 65 20 65 6e 74 72 79 20 61 66 74  of the entry aft
30b82 65 72 20 74 68 65 20 75 70 64 61 74 65 2e 0a 2a  er the update..*
30b83 2a 0a 2a 2a 20 20 20 20 69 2e 20 20 44 61 74 61  *.**    i.  Data
30b84 20 66 72 6f 6d 20 6d 69 64 64 6c 65 20 63 6f 6c   from middle col
30b85 75 6d 6e 73 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 20 20  umns....**.**   
30b86 20 4e 2e 20 20 54 68 65 20 64 61 74 61 20 69 6e   N.  The data in
30b87 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e   the last column
30b88 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 61 66   of the entry af
30b89 74 65 72 20 74 68 65 20 75 70 64 61 74 65 2e 0a  ter the update..
30b8a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 52 6f 77  **.** The regRow
30b8b 69 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  id parameter is 
30b8c 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
30b8d 20 72 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69   register contai
30b8e 6e 69 6e 67 20 28 31 29 2e 0a 2a 2a 0a 2a 2a 20  ning (1)..**.** 
30b8f 49 66 20 69 73 55 70 64 61 74 65 20 69 73 20 74  If isUpdate is t
30b90 72 75 65 20 61 6e 64 20 72 6f 77 69 64 43 68 6e  rue and rowidChn
30b91 67 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  g is non-zero, t
30b92 68 65 6e 20 72 6f 77 69 64 43 68 6e 67 20 63 6f  hen rowidChng co
30b93 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 61 64  ntains.** the ad
30b94 64 72 65 73 73 20 6f 66 20 61 20 72 65 67 69 73  dress of a regis
30b95 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ter containing t
30b96 68 65 20 72 6f 77 69 64 20 62 65 66 6f 72 65 20  he rowid before 
30b97 74 68 65 20 75 70 64 61 74 65 20 74 61 6b 65 73  the update takes
30b98 0a 2a 2a 20 70 6c 61 63 65 2e 20 69 73 55 70 64  .** place. isUpd
30b99 61 74 65 20 69 73 20 74 72 75 65 20 66 6f 72 20  ate is true for 
30b9a 55 50 44 41 54 45 73 20 61 6e 64 20 66 61 6c 73  UPDATEs and fals
30b9b 65 20 66 6f 72 20 49 4e 53 45 52 54 73 2e 20 49  e for INSERTs. I
30b9c 66 20 69 73 55 70 64 61 74 65 0a 2a 2a 20 69 73  f isUpdate.** is
30b9d 20 66 61 6c 73 65 2c 20 69 6e 64 69 63 61 74 69   false, indicati
30b9e 6e 67 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61  ng an INSERT sta
30b9f 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 61 20 6e  tement, then a n
30ba0 6f 6e 2d 7a 65 72 6f 20 72 6f 77 69 64 43 68 6e  on-zero rowidChn
30ba1 67 20 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20  g .** indicates 
30ba2 74 68 61 74 20 74 68 65 20 72 6f 77 69 64 20 77  that the rowid w
30ba3 61 73 20 65 78 70 6c 69 63 69 74 6c 79 20 73 70  as explicitly sp
30ba4 65 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20  ecified as part 
30ba5 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 53 45 52 54  of the.** INSERT
30ba6 20 73 74 61 74 65 6d 65 6e 74 2e 20 49 66 20 72   statement. If r
30ba7 6f 77 69 64 43 68 6e 67 20 69 73 20 66 61 6c 73  owidChng is fals
30ba8 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  e, it means that
30ba9 20 20 74 68 65 20 72 6f 77 69 64 20 69 73 0a 2a    the rowid is.*
30baa 2a 20 63 6f 6d 70 75 74 65 64 20 61 75 74 6f 6d  * computed autom
30bab 61 74 69 63 61 6c 6c 79 20 69 6e 20 61 6e 20 69  atically in an i
30bac 6e 73 65 72 74 20 6f 72 20 74 68 61 74 20 74 68  nsert or that th
30bad 65 20 72 6f 77 69 64 20 76 61 6c 75 65 20 69 73  e rowid value is
30bae 20 6e 6f 74 20 0a 2a 2a 20 6d 6f 64 69 66 69 65   not .** modifie
30baf 64 20 62 79 20 61 6e 20 75 70 64 61 74 65 2e 0a  d by an update..
30bb0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67  **.** The code g
30bb1 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 69 73  enerated by this
30bb2 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 20 6e   routine store n
30bb3 65 77 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73  ew index entries
30bb4 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65   into.** registe
30bb5 72 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  rs identified by
30bb6 20 61 52 65 67 49 64 78 5b 5d 2e 20 20 4e 6f 20   aRegIdx[].  No 
30bb7 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 63  index entry is c
30bb8 72 65 61 74 65 64 20 66 6f 72 0a 2a 2a 20 69 6e  reated for.** in
30bb9 64 69 63 65 73 20 77 68 65 72 65 20 61 52 65 67  dices where aReg
30bba 49 64 78 5b 69 5d 3d 3d 30 2e 20 20 54 68 65 20  Idx[i]==0.  The 
30bbb 6f 72 64 65 72 20 6f 66 20 69 6e 64 69 63 65 73  order of indices
30bbc 20 69 6e 20 61 52 65 67 49 64 78 5b 5d 20 69 73   in aRegIdx[] is
30bbd 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20  .** the same as 
30bbe 74 68 65 20 6f 72 64 65 72 20 6f 66 20 69 6e 64  the order of ind
30bbf 69 63 65 73 20 6f 6e 20 74 68 65 20 6c 69 6e 6b  ices on the link
30bc0 65 64 20 6c 69 73 74 20 6f 66 20 69 6e 64 69 63  ed list of indic
30bc1 65 73 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 74  es.** attached t
30bc2 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a  o the table..**.
30bc3 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
30bc4 61 6c 73 6f 20 67 65 6e 65 72 61 74 65 73 20 63  also generates c
30bc5 6f 64 65 20 74 6f 20 63 68 65 63 6b 20 63 6f 6e  ode to check con
30bc6 73 74 72 61 69 6e 74 73 2e 20 20 4e 4f 54 20 4e  straints.  NOT N
30bc7 55 4c 4c 2c 0a 2a 2a 20 43 48 45 43 4b 2c 20 61  ULL,.** CHECK, a
30bc8 6e 64 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72  nd UNIQUE constr
30bc9 61 69 6e 74 73 20 61 72 65 20 61 6c 6c 20 63 68  aints are all ch
30bca 65 63 6b 65 64 2e 20 20 49 66 20 61 20 63 6f 6e  ecked.  If a con
30bcb 73 74 72 61 69 6e 74 20 66 61 69 6c 73 2c 0a 2a  straint fails,.*
30bcc 2a 20 74 68 65 6e 20 74 68 65 20 61 70 70 72 6f  * then the appro
30bcd 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20 69 73  priate action is
30bce 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 54 68 65   performed.  The
30bcf 72 65 20 61 72 65 20 66 69 76 65 20 70 6f 73 73  re are five poss
30bd0 69 62 6c 65 0a 2a 2a 20 61 63 74 69 6f 6e 73 3a  ible.** actions:
30bd1 20 52 4f 4c 4c 42 41 43 4b 2c 20 41 42 4f 52 54   ROLLBACK, ABORT
30bd2 2c 20 46 41 49 4c 2c 20 52 45 50 4c 41 43 45 2c  , FAIL, REPLACE,
30bd3 20 61 6e 64 20 49 47 4e 4f 52 45 2e 0a 2a 2a 0a   and IGNORE..**.
30bd4 2a 2a 20 20 43 6f 6e 73 74 72 61 69 6e 74 20 74  **  Constraint t
30bd5 79 70 65 20 20 41 63 74 69 6f 6e 20 20 20 20 20  ype  Action     
30bd6 20 20 57 68 61 74 20 48 61 70 70 65 6e 73 0a 2a    What Happens.*
30bd7 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *  -------------
30bd8 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --  ----------  
30bd9 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
30bda 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
30bdb 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 61 6e  ---------.**  an
30bdc 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 52  y              R
30bdd 4f 4c 4c 42 41 43 4b 20 20 20 20 20 54 68 65 20  OLLBACK     The 
30bde 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
30bdf 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61  ion is rolled ba
30be0 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20  ck and.**       
30be1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30be2 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
30be3 5f 65 78 65 63 28 29 20 72 65 74 75 72 6e 73 20  _exec() returns 
30be4 69 6d 6d 65 64 69 61 74 65 6c 79 20 77 69 74 68  immediately with
30be5 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   a.**           
30be6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30be7 20 20 20 20 20 72 65 74 75 72 6e 20 63 6f 64 65       return code
30be8 20 6f 66 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54   of SQLITE_CONST
30be9 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e  RAINT..**.**  an
30bea 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 41  y              A
30beb 42 4f 52 54 20 20 20 20 20 20 20 20 42 61 63 6b  BORT        Back
30bec 20 6f 75 74 20 63 68 61 6e 67 65 73 20 66 72 6f   out changes fro
30bed 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f  m the current co
30bee 6d 6d 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  mmand.**        
30bef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30bf0 20 20 20 20 20 20 20 20 6f 6e 6c 79 20 28 64 6f          only (do
30bf1 20 6e 6f 74 20 64 6f 20 61 20 63 6f 6d 70 6c 65   not do a comple
30bf2 74 65 20 72 6f 6c 6c 62 61 63 6b 29 20 74 68 65  te rollback) the
30bf3 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  n.**            
30bf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30bf5 20 20 20 20 63 61 75 73 65 20 73 71 6c 69 74 65      cause sqlite
30bf6 33 5f 65 78 65 63 28 29 20 74 6f 20 72 65 74 75  3_exec() to retu
30bf7 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a  rn immediately.*
30bf8 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
30bf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30bfa 20 77 69 74 68 20 53 51 4c 49 54 45 5f 43 4f 4e   with SQLITE_CON
30bfb 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20  STRAINT..**.**  
30bfc 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20  any             
30bfd 20 46 41 49 4c 20 20 20 20 20 20 20 20 20 53 71   FAIL         Sq
30bfe 6c 69 74 65 33 5f 65 78 65 63 28 29 20 72 65 74  lite3_exec() ret
30bff 75 72 6e 73 20 69 6d 6d 65 64 69 61 74 65 6c 79  urns immediately
30c00 20 77 69 74 68 20 61 0a 2a 2a 20 20 20 20 20 20   with a.**      
30c01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30c02 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
30c03 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f   code of SQLITE_
30c04 43 4f 4e 53 54 52 41 49 4e 54 2e 20 20 54 68 65  CONSTRAINT.  The
30c05 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
30c06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30c07 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69     transaction i
30c08 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63  s not rolled bac
30c09 6b 20 61 6e 64 20 61 6e 79 0a 2a 2a 20 20 20 20  k and any.**    
30c0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30c0b 20 20 20 20 20 20 20 20 20 20 20 20 70 72 69 6f              prio
30c0c 72 20 63 68 61 6e 67 65 73 20 61 72 65 20 72 65  r changes are re
30c0d 74 61 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 61  tained..**.**  a
30c0e 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ny              
30c0f 49 47 4e 4f 52 45 20 20 20 20 20 20 20 54 68 65  IGNORE       The
30c10 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 61   record number a
30c11 6e 64 20 64 61 74 61 20 69 73 20 70 6f 70 70 65  nd data is poppe
30c12 64 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20  d from.**       
30c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30c14 20 20 20 20 20 20 20 20 20 74 68 65 20 73 74 61           the sta
30c15 63 6b 20 61 6e 64 20 74 68 65 72 65 20 69 73 20  ck and there is 
30c16 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d  an immediate jum
30c17 70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  p.**            
30c18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30c19 20 20 20 20 74 6f 20 6c 61 62 65 6c 20 69 67 6e      to label ign
30c1a 6f 72 65 44 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 20  oreDest..**.**  
30c1b 4e 4f 54 20 4e 55 4c 4c 20 20 20 20 20 20 20 20  NOT NULL        
30c1c 20 52 45 50 4c 41 43 45 20 20 20 20 20 20 54 68   REPLACE      Th
30c1d 65 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20  e NULL value is 
30c1e 72 65 70 6c 61 63 65 20 62 79 20 74 68 65 20 64  replace by the d
30c1f 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20  efault.**       
30c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30c21 20 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 66           value f
30c22 6f 72 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 20  or that column. 
30c23 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   If the default 
30c24 76 61 6c 75 65 0a 2a 2a 20 20 20 20 20 20 20 20  value.**        
30c25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30c26 20 20 20 20 20 20 20 20 69 73 20 4e 55 4c 4c 2c          is NULL,
30c27 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 74   the action is t
30c28 68 65 20 73 61 6d 65 20 61 73 20 41 42 4f 52 54  he same as ABORT
30c29 2e 0a 2a 2a 0a 2a 2a 20 20 55 4e 49 51 55 45 20  ..**.**  UNIQUE 
30c2a 20 20 20 20 20 20 20 20 20 20 52 45 50 4c 41 43            REPLAC
30c2b 45 20 20 20 20 20 20 54 68 65 20 6f 74 68 65 72  E      The other
30c2c 20 72 6f 77 20 74 68 61 74 20 63 6f 6e 66 6c 69   row that confli
30c2d 63 74 73 20 77 69 74 68 20 74 68 65 20 72 6f 77  cts with the row
30c2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
30c2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30c30 20 20 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65     being inserte
30c31 64 20 69 73 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a  d is removed..**
30c32 0a 2a 2a 20 20 43 48 45 43 4b 20 20 20 20 20 20  .**  CHECK      
30c33 20 20 20 20 20 20 52 45 50 4c 41 43 45 20 20 20        REPLACE   
30c34 20 20 20 49 6c 6c 65 67 61 6c 2e 20 20 54 68 65     Illegal.  The
30c35 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 65   results in an e
30c36 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  xception..**.** 
30c37 57 68 69 63 68 20 61 63 74 69 6f 6e 20 74 6f 20  Which action to 
30c38 74 61 6b 65 20 69 73 20 64 65 74 65 72 6d 69 6e  take is determin
30c39 65 64 20 62 79 20 74 68 65 20 6f 76 65 72 72 69  ed by the overri
30c3a 64 65 45 72 72 6f 72 20 70 61 72 61 6d 65 74 65  deError paramete
30c3b 72 2e 0a 2a 2a 20 4f 72 20 69 66 20 6f 76 65 72  r..** Or if over
30c3c 72 69 64 65 45 72 72 6f 72 3d 3d 4f 45 5f 44 65  rideError==OE_De
30c3d 66 61 75 6c 74 2c 20 74 68 65 6e 20 74 68 65 20  fault, then the 
30c3e 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 20  pParse->onError 
30c3f 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20  parameter.** is 
30c40 75 73 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61  used.  Or if pPa
30c41 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45  rse->onError==OE
30c42 5f 44 65 66 61 75 6c 74 20 74 68 65 6e 20 74 68  _Default then th
30c43 65 20 6f 6e 45 72 72 6f 72 20 76 61 6c 75 65 0a  e onError value.
30c44 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6e 73 74  ** for the const
30c45 72 61 69 6e 74 20 69 73 20 75 73 65 64 2e 0a 2a  raint is used..*
30c46 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67  *.** The calling
30c47 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6f 70   routine must op
30c48 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 20  en a read/write 
30c49 63 75 72 73 6f 72 20 66 6f 72 20 70 54 61 62 20  cursor for pTab 
30c4a 77 69 74 68 0a 2a 2a 20 63 75 72 73 6f 72 20 6e  with.** cursor n
30c4b 75 6d 62 65 72 20 22 62 61 73 65 43 75 72 22 2e  umber "baseCur".
30c4c 20 20 41 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66    All indices of
30c4d 20 70 54 61 62 20 6d 75 73 74 20 61 6c 73 6f 20   pTab must also 
30c4e 68 61 76 65 20 6f 70 65 6e 0a 2a 2a 20 72 65 61  have open.** rea
30c4f 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 73 20  d/write cursors 
30c50 77 69 74 68 20 63 75 72 73 6f 72 20 6e 75 6d 62  with cursor numb
30c51 65 72 20 62 61 73 65 43 75 72 2b 69 20 66 6f 72  er baseCur+i for
30c52 20 74 68 65 20 69 2d 74 68 20 63 75 72 73 6f 72   the i-th cursor
30c53 2e 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20  ..** Except, if 
30c54 74 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 73 73  there is no poss
30c55 69 62 69 6c 69 74 79 20 6f 66 20 61 20 52 45 50  ibility of a REP
30c56 4c 41 43 45 20 61 63 74 69 6f 6e 20 74 68 65 6e  LACE action then
30c57 0a 2a 2a 20 63 75 72 73 6f 72 73 20 64 6f 20 6e  .** cursors do n
30c58 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6f 70  ot need to be op
30c59 65 6e 20 66 6f 72 20 69 6e 64 69 63 65 73 20 77  en for indices w
30c5a 68 65 72 65 20 61 52 65 67 49 64 78 5b 69 5d 3d  here aRegIdx[i]=
30c5b 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  =0..*/.SQLITE_PR
30c5c 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
30c5d 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72  e3GenerateConstr
30c5e 61 69 6e 74 43 68 65 63 6b 73 28 0a 20 20 50 61  aintChecks(.  Pa
30c5f 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
30c60 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
30c61 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
30c62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  le *pTab,       
30c63 20 2f 2a 20 74 68 65 20 74 61 62 6c 65 20 69 6e   /* the table in
30c64 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20  to which we are 
30c65 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 69  inserting */.  i
30c66 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20 20 20  nt baseCur,     
30c67 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61     /* Index of a
30c68 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   read/write curs
30c69 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 70  or pointing at p
30c6a 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  Tab */.  int reg
30c6b 52 6f 77 69 64 2c 20 20 20 20 20 20 20 2f 2a 20  Rowid,       /* 
30c6c 49 6e 64 65 78 20 6f 66 20 74 68 65 20 72 61 6e  Index of the ran
30c6d 67 65 20 6f 66 20 69 6e 70 75 74 20 72 65 67 69  ge of input regi
30c6e 73 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  sters */.  int *
30c6f 61 52 65 67 49 64 78 2c 20 20 20 20 20 20 20 2f  aRegIdx,       /
30c70 2a 20 52 65 67 69 73 74 65 72 20 75 73 65 64 20  * Register used 
30c71 62 79 20 65 61 63 68 20 69 6e 64 65 78 2e 20 20  by each index.  
30c72 30 20 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64  0 for unused ind
30c73 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 6f  ices */.  int ro
30c74 77 69 64 43 68 6e 67 2c 20 20 20 20 20 20 2f 2a  widChng,      /*
30c75 20 54 72 75 65 20 69 66 20 74 68 65 20 72 6f 77   True if the row
30c76 69 64 20 6d 69 67 68 74 20 63 6f 6c 6c 69 64 65  id might collide
30c77 20 77 69 74 68 20 65 78 69 73 74 69 6e 67 20 65   with existing e
30c78 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73  ntry */.  int is
30c79 55 70 64 61 74 65 2c 20 20 20 20 20 20 20 2f 2a  Update,       /*
30c7a 20 54 72 75 65 20 66 6f 72 20 55 50 44 41 54 45   True for UPDATE
30c7b 2c 20 46 61 6c 73 65 20 66 6f 72 20 49 4e 53 45  , False for INSE
30c7c 52 54 20 2a 2f 0a 20 20 69 6e 74 20 6f 76 65 72  RT */.  int over
30c7d 72 69 64 65 45 72 72 6f 72 2c 20 20 2f 2a 20 4f  rideError,  /* O
30c7e 76 65 72 72 69 64 65 20 6f 6e 45 72 72 6f 72 20  verride onError 
30c7f 74 6f 20 74 68 69 73 20 69 66 20 6e 6f 74 20 4f  to this if not O
30c80 45 5f 44 65 66 61 75 6c 74 20 2a 2f 0a 20 20 69  E_Default */.  i
30c81 6e 74 20 69 67 6e 6f 72 65 44 65 73 74 2c 20 20  nt ignoreDest,  
30c82 20 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68     /* Jump to th
30c83 69 73 20 6c 61 62 65 6c 20 6f 6e 20 61 6e 20 4f  is label on an O
30c84 45 5f 49 67 6e 6f 72 65 20 72 65 73 6f 6c 75 74  E_Ignore resolut
30c85 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 62  ion */.  int *pb
30c86 4d 61 79 52 65 70 6c 61 63 65 20 20 20 2f 2a 20  MayReplace   /* 
30c87 4f 55 54 3a 20 53 65 74 20 74 6f 20 74 72 75 65  OUT: Set to true
30c88 20 69 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 6d   if constraint m
30c89 61 79 20 63 61 75 73 65 20 61 20 72 65 70 6c 61  ay cause a repla
30c8a 63 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  ce */.){.  int i
30c8b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
30c8c 2a 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * loop counter *
30c8d 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
30c8e 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20          /* VDBE 
30c8f 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 74 69 6f  under construtio
30c90 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  n */.  int nCol;
30c91 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
30c92 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
30c93 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72  */.  int onError
30c94 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66  ;        /* Conf
30c95 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20  lict resolution 
30c96 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 69 6e  strategy */.  in
30c97 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20 20 20  t j1;           
30c98 20 20 2f 2a 20 41 64 64 72 65 73 73 73 20 6f 66    /* Addresss of
30c99 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f   jump instructio
30c9a 6e 20 2a 2f 0a 20 20 69 6e 74 20 6a 32 20 3d 20  n */.  int j2 = 
30c9b 30 2c 20 6a 33 3b 20 20 20 20 20 2f 2a 20 41 64  0, j3;     /* Ad
30c9c 64 72 65 73 73 65 73 20 6f 66 20 6a 75 6d 70 20  dresses of jump 
30c9d 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a  instructions */.
30c9e 20 20 69 6e 74 20 72 65 67 44 61 74 61 3b 20 20    int regData;  
30c9f 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
30ca0 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 72  r containing fir
30ca1 73 74 20 64 61 74 61 20 63 6f 6c 75 6d 6e 20 2a  st data column *
30ca2 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20  /.  int iCur;   
30ca3 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
30ca4 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 2a   cursor number *
30ca5 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  /.  Index *pIdx;
30ca6 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
30ca7 74 65 72 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68  ter to one of th
30ca8 65 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 73  e indices */.  s
30ca9 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
30caa 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
30cab 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
30cac 69 6e 74 20 73 65 65 6e 52 65 70 6c 61 63 65 20  int seenReplace 
30cad 3d 20 30 3b 20 2f 2a 20 54 72 75 65 20 69 66 20  = 0; /* True if 
30cae 52 45 50 4c 41 43 45 20 69 73 20 75 73 65 64 20  REPLACE is used 
30caf 74 6f 20 72 65 73 6f 6c 76 65 20 49 4e 54 20 50  to resolve INT P
30cb0 4b 20 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20  K conflict */.  
30cb1 69 6e 74 20 72 65 67 4f 6c 64 52 6f 77 69 64 20  int regOldRowid 
30cb2 3d 20 28 72 6f 77 69 64 43 68 6e 67 20 26 26 20  = (rowidChng && 
30cb3 69 73 55 70 64 61 74 65 29 20 3f 20 72 6f 77 69  isUpdate) ? rowi
30cb4 64 43 68 6e 67 20 3a 20 72 65 67 52 6f 77 69 64  dChng : regRowid
30cb5 3b 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  ;..  db = pParse
30cb6 2d 3e 64 62 3b 0a 20 20 76 20 3d 20 73 71 6c 69  ->db;.  v = sqli
30cb7 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
30cb8 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21  e);.  assert( v!
30cb9 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
30cba 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30  pTab->pSelect==0
30cbb 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61 62   );  /* This tab
30cbc 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45 57  le is not a VIEW
30cbd 20 2a 2f 0a 20 20 6e 43 6f 6c 20 3d 20 70 54 61   */.  nCol = pTa
30cbe 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 72 65 67 44 61  b->nCol;.  regDa
30cbf 74 61 20 3d 20 72 65 67 52 6f 77 69 64 20 2b 20  ta = regRowid + 
30cc0 31 3b 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c  1;..  /* Test al
30cc1 6c 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74  l NOT NULL const
30cc2 72 61 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 66  raints..  */.  f
30cc3 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20  or(i=0; i<nCol; 
30cc4 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3d  i++){.    if( i=
30cc5 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a  =pTab->iPKey ){.
30cc6 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
30cc7 20 20 20 20 7d 0a 20 20 20 20 6f 6e 45 72 72 6f      }.    onErro
30cc8 72 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  r = pTab->aCol[i
30cc9 5d 2e 6e 6f 74 4e 75 6c 6c 3b 0a 20 20 20 20 69  ].notNull;.    i
30cca 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e  f( onError==OE_N
30ccb 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  one ) continue;.
30ccc 20 20 20 20 69 66 28 20 6f 76 65 72 72 69 64 65      if( override
30ccd 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c  Error!=OE_Defaul
30cce 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72  t ){.      onErr
30ccf 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72  or = overrideErr
30cd0 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  or;.    }else if
30cd1 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65  ( onError==OE_De
30cd2 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f  fault ){.      o
30cd3 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72  nError = OE_Abor
30cd4 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
30cd5 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70   onError==OE_Rep
30cd6 6c 61 63 65 20 26 26 20 70 54 61 62 2d 3e 61 43  lace && pTab->aC
30cd7 6f 6c 5b 69 5d 2e 70 44 66 6c 74 3d 3d 30 20 29  ol[i].pDflt==0 )
30cd8 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20  {.      onError 
30cd9 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20  = OE_Abort;.    
30cda 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e  }.    assert( on
30cdb 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61  Error==OE_Rollba
30cdc 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f  ck || onError==O
30cdd 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72  E_Abort || onErr
30cde 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20  or==OE_Fail.    
30cdf 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d      || onError==
30ce0 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45  OE_Ignore || onE
30ce1 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65  rror==OE_Replace
30ce2 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20   );.    switch( 
30ce3 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20  onError ){.     
30ce4 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a   case OE_Abort:.
30ce5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d          sqlite3M
30ce6 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b  ayAbort(pParse);
30ce7 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52  .      case OE_R
30ce8 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 63  ollback:.      c
30ce9 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20  ase OE_Fail: {. 
30cea 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d 73         char *zMs
30ceb 67 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  g;.        sqlit
30cec 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
30ced 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 2c 0a 20  OP_HaltIfNull,. 
30cee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30cef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30cf0 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
30cf1 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 72 65 67  NT, onError, reg
30cf2 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 20 20 20  Data+i);.       
30cf3 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d   zMsg = sqlite3M
30cf4 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 2e 25  Printf(db, "%s.%
30cf5 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55 4c  s may not be NUL
30cf6 4c 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  L",.            
30cf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30cf8 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 70    pTab->zName, p
30cf9 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61  Tab->aCol[i].zNa
30cfa 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  me);.        sql
30cfb 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
30cfc 28 76 2c 20 2d 31 2c 20 7a 4d 73 67 2c 20 50 34  (v, -1, zMsg, P4
30cfd 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20  _DYNAMIC);.     
30cfe 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30cff 7d 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f  }.      case OE_
30d00 49 67 6e 6f 72 65 3a 20 7b 0a 20 20 20 20 20 20  Ignore: {.      
30d01 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
30d02 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c  Op2(v, OP_IsNull
30d03 2c 20 72 65 67 44 61 74 61 2b 69 2c 20 69 67 6e  , regData+i, ign
30d04 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20  oreDest);.      
30d05 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
30d06 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20  .      default: 
30d07 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
30d08 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65  ( onError==OE_Re
30d09 70 6c 61 63 65 20 29 3b 0a 20 20 20 20 20 20 20  place );.       
30d0a 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
30d0b 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f  eAddOp1(v, OP_No
30d0c 74 4e 75 6c 6c 2c 20 72 65 67 44 61 74 61 2b 69  tNull, regData+i
30d0d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
30d0e 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
30d0f 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  e, pTab->aCol[i]
30d10 2e 70 44 66 6c 74 2c 20 72 65 67 44 61 74 61 2b  .pDflt, regData+
30d11 69 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  i);.        sqli
30d12 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
30d13 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20 20 20  v, j1);.        
30d14 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
30d15 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
30d16 65 73 74 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f  est all CHECK co
30d17 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2f 0a 23  nstraints.  */.#
30d18 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
30d19 49 54 5f 43 48 45 43 4b 0a 20 20 69 66 28 20 70  IT_CHECK.  if( p
30d1a 54 61 62 2d 3e 70 43 68 65 63 6b 20 26 26 20 28  Tab->pCheck && (
30d1b 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
30d1c 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 29  TE_IgnoreChecks)
30d1d 3d 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72 4c  ==0 ){.    ExprL
30d1e 69 73 74 20 2a 70 43 68 65 63 6b 20 3d 20 70 54  ist *pCheck = pT
30d1f 61 62 2d 3e 70 43 68 65 63 6b 3b 0a 20 20 20 20  ab->pCheck;.    
30d20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 20 3d  pParse->ckBase =
30d21 20 72 65 67 44 61 74 61 3b 0a 20 20 20 20 6f 6e   regData;.    on
30d22 45 72 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65  Error = override
30d23 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c  Error!=OE_Defaul
30d24 74 20 3f 20 6f 76 65 72 72 69 64 65 45 72 72 6f  t ? overrideErro
30d25 72 20 3a 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20  r : OE_Abort;.  
30d26 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68    for(i=0; i<pCh
30d27 65 63 6b 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  eck->nExpr; i++)
30d28 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 6c 6c 4f  {.      int allO
30d29 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  k = sqlite3VdbeM
30d2a 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
30d2b 20 20 20 45 78 70 72 20 2a 70 44 75 70 20 3d 20     Expr *pDup = 
30d2c 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
30d2d 62 2c 20 70 43 68 65 63 6b 2d 3e 61 5b 69 5d 2e  b, pCheck->a[i].
30d2e 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 20 20 20  pExpr, 0);.     
30d2f 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
30d30 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
30d31 20 20 61 73 73 65 72 74 28 20 70 44 75 70 21 3d    assert( pDup!=
30d32 30 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 );.        sql
30d33 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
30d34 50 61 72 73 65 2c 20 70 44 75 70 2c 20 61 6c 6c  Parse, pDup, all
30d35 4f 6b 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  Ok, SQLITE_JUMPI
30d36 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20  FNULL);.        
30d37 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
30d38 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20  Ignore ){.      
30d39 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
30d3a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
30d3b 2c 20 30 2c 20 69 67 6e 6f 72 65 44 65 73 74 29  , 0, ignoreDest)
30d3c 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
30d3d 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20  .          char 
30d3e 2a 7a 43 6f 6e 73 4e 61 6d 65 20 3d 20 70 43 68  *zConsName = pCh
30d3f 65 63 6b 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b  eck->a[i].zName;
30d40 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6f  .          if( o
30d41 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61  nError==OE_Repla
30d42 63 65 20 29 20 6f 6e 45 72 72 6f 72 20 3d 20 4f  ce ) onError = O
30d43 45 5f 41 62 6f 72 74 3b 20 2f 2a 20 49 4d 50 3a  E_Abort; /* IMP:
30d44 20 52 2d 31 35 35 36 39 2d 36 33 36 32 35 20 2a   R-15569-63625 *
30d45 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  /.          if( 
30d46 7a 43 6f 6e 73 4e 61 6d 65 20 29 7b 0a 20 20 20  zConsName ){.   
30d47 20 20 20 20 20 20 20 20 20 7a 43 6f 6e 73 4e 61           zConsNa
30d48 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  me = sqlite3MPri
30d49 6e 74 66 28 64 62 2c 20 22 63 6f 6e 73 74 72 61  ntf(db, "constra
30d4a 69 6e 74 20 25 73 20 66 61 69 6c 65 64 22 2c 20  int %s failed", 
30d4b 7a 43 6f 6e 73 4e 61 6d 65 29 3b 0a 20 20 20 20  zConsName);.    
30d4c 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
30d4d 20 20 20 20 20 20 20 20 20 7a 43 6f 6e 73 4e 61           zConsNa
30d4e 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  me = 0;.        
30d4f 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71    }.          sq
30d50 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61  lite3HaltConstra
30d51 69 6e 74 28 70 50 61 72 73 65 2c 20 6f 6e 45 72  int(pParse, onEr
30d52 72 6f 72 2c 20 7a 43 6f 6e 73 4e 61 6d 65 2c 20  ror, zConsName, 
30d53 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  P4_DYNAMIC);.   
30d54 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73       }.        s
30d55 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
30d56 65 4c 61 62 65 6c 28 76 2c 20 61 6c 6c 4f 6b 29  eLabel(v, allOk)
30d57 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
30d58 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
30d59 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20  e(db, pDup);.   
30d5a 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a   }.  }.#endif /*
30d5b 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
30d5c 5f 4f 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a  _OMIT_CHECK) */.
30d5d 0a 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65  .  /* If we have
30d5e 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d   an INTEGER PRIM
30d5f 41 52 59 20 4b 45 59 2c 20 6d 61 6b 65 20 73 75  ARY KEY, make su
30d60 72 65 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  re the primary k
30d61 65 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e  ey.  ** of the n
30d62 65 77 20 72 65 63 6f 72 64 20 64 6f 65 73 20 6e  ew record does n
30d63 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78  ot previously ex
30d64 69 73 74 2e 20 20 45 78 63 65 70 74 2c 20 69 66  ist.  Except, if
30d65 20 74 68 69 73 0a 20 20 2a 2a 20 69 73 20 61 6e   this.  ** is an
30d66 20 55 50 44 41 54 45 20 61 6e 64 20 74 68 65 20   UPDATE and the 
30d67 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 6e  primary key is n
30d68 6f 74 20 63 68 61 6e 67 69 6e 67 2c 20 74 68 61  ot changing, tha
30d69 74 20 69 73 20 4f 4b 2e 0a 20 20 2a 2f 0a 20 20  t is OK..  */.  
30d6a 69 66 28 20 72 6f 77 69 64 43 68 6e 67 20 29 7b  if( rowidChng ){
30d6b 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 70  .    onError = p
30d6c 54 61 62 2d 3e 6b 65 79 43 6f 6e 66 3b 0a 20 20  Tab->keyConf;.  
30d6d 20 20 69 66 28 20 6f 76 65 72 72 69 64 65 45 72    if( overrideEr
30d6e 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror!=OE_Default 
30d6f 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
30d70 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72   = overrideError
30d71 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
30d72 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61  onError==OE_Defa
30d73 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45  ult ){.      onE
30d74 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  rror = OE_Abort;
30d75 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 20  .    }.    .    
30d76 69 66 28 20 69 73 55 70 64 61 74 65 20 29 7b 0a  if( isUpdate ){.
30d77 20 20 20 20 20 20 6a 32 20 3d 20 73 71 6c 69 74        j2 = sqlit
30d78 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
30d79 4f 50 5f 45 71 2c 20 72 65 67 52 6f 77 69 64 2c  OP_Eq, regRowid,
30d7a 20 30 2c 20 72 6f 77 69 64 43 68 6e 67 29 3b 0a   0, rowidChng);.
30d7b 20 20 20 20 7d 0a 20 20 20 20 6a 33 20 3d 20 73      }.    j3 = s
30d7c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
30d7d 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
30d7e 2c 20 62 61 73 65 43 75 72 2c 20 30 2c 20 72 65  , baseCur, 0, re
30d7f 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 77 69  gRowid);.    swi
30d80 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a  tch( onError ){.
30d81 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b        default: {
30d82 0a 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  .        onError
30d83 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   = OE_Abort;.   
30d84 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72       /* Fall thr
30d85 75 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20  u into the next 
30d86 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a  case */.      }.
30d87 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52 6f        case OE_Ro
30d88 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 63 61  llback:.      ca
30d89 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20  se OE_Abort:.   
30d8a 20 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a     case OE_Fail:
30d8b 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
30d8c 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74  e3HaltConstraint
30d8d 28 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 72  (.          pPar
30d8e 73 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 22 50 52  se, onError, "PR
30d8f 49 4d 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62  IMARY KEY must b
30d90 65 20 75 6e 69 71 75 65 22 2c 20 50 34 5f 53 54  e unique", P4_ST
30d91 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 62  ATIC);.        b
30d92 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
30d93 20 20 20 20 63 61 73 65 20 4f 45 5f 52 65 70 6c      case OE_Repl
30d94 61 63 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 2f  ace: {.        /
30d95 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 44  * If there are D
30d96 45 4c 45 54 45 20 74 72 69 67 67 65 72 73 20 6f  ELETE triggers o
30d97 6e 20 74 68 69 73 20 74 61 62 6c 65 20 61 6e 64  n this table and
30d98 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
30d99 72 65 63 75 72 73 69 76 65 2d 74 72 69 67 67 65  recursive-trigge
30d9a 72 73 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  rs flag is set, 
30d9b 63 61 6c 6c 20 47 65 6e 65 72 61 74 65 52 6f 77  call GenerateRow
30d9c 44 65 6c 65 74 65 28 29 20 74 6f 0a 20 20 20 20  Delete() to.    
30d9d 20 20 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74 68      ** remove th
30d9e 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 6f  e conflicting ro
30d9f 77 20 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65  w from the table
30da0 2e 20 54 68 69 73 20 77 69 6c 6c 20 66 69 72 65  . This will fire
30da1 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20  .        ** the 
30da2 74 72 69 67 67 65 72 73 20 61 6e 64 20 72 65 6d  triggers and rem
30da3 6f 76 65 20 62 6f 74 68 20 74 68 65 20 74 61 62  ove both the tab
30da4 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 62 2d 74  le and index b-t
30da5 72 65 65 20 65 6e 74 72 69 65 73 2e 0a 20 20 20  ree entries..   
30da6 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
30da7 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  ** Otherwise, if
30da8 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 74 72   there are no tr
30da9 69 67 67 65 72 73 20 6f 72 20 74 68 65 20 72 65  iggers or the re
30daa 63 75 72 73 69 76 65 2d 74 72 69 67 67 65 72 73  cursive-triggers
30dab 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 6c 61 67  .        ** flag
30dac 20 69 73 20 6e 6f 74 20 73 65 74 2c 20 62 75 74   is not set, but
30dad 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 6f   the table has o
30dae 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e 64 65 78  ne or more index
30daf 65 73 2c 20 63 61 6c 6c 20 0a 20 20 20 20 20 20  es, call .      
30db0 20 20 2a 2a 20 47 65 6e 65 72 61 74 65 52 6f 77    ** GenerateRow
30db1 49 6e 64 65 78 44 65 6c 65 74 65 28 29 2e 20 54  IndexDelete(). T
30db2 68 69 73 20 72 65 6d 6f 76 65 73 20 74 68 65 20  his removes the 
30db3 69 6e 64 65 78 20 62 2d 74 72 65 65 20 65 6e 74  index b-tree ent
30db4 72 69 65 73 20 0a 20 20 20 20 20 20 20 20 2a 2a  ries .        **
30db5 20 6f 6e 6c 79 2e 20 54 68 65 20 74 61 62 6c 65   only. The table
30db6 20 62 2d 74 72 65 65 20 65 6e 74 72 79 20 77 69   b-tree entry wi
30db7 6c 6c 20 62 65 20 72 65 70 6c 61 63 65 64 20 62  ll be replaced b
30db8 79 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20  y the new entry 
30db9 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 68 65 6e  .        ** when
30dba 20 69 74 20 69 73 20 69 6e 73 65 72 74 65 64 2e   it is inserted.
30dbb 20 20 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20    .        **.  
30dbc 20 20 20 20 20 20 2a 2a 20 49 66 20 65 69 74 68        ** If eith
30dbd 65 72 20 47 65 6e 65 72 61 74 65 52 6f 77 44 65  er GenerateRowDe
30dbe 6c 65 74 65 28 29 20 6f 72 20 47 65 6e 65 72 61  lete() or Genera
30dbf 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65  teRowIndexDelete
30dc0 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 0a 20 20  () is called,.  
30dc1 20 20 20 20 20 20 2a 2a 20 61 6c 73 6f 20 69 6e        ** also in
30dc2 76 6f 6b 65 20 4d 75 6c 74 69 57 72 69 74 65 28  voke MultiWrite(
30dc3 29 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68  ) to indicate th
30dc4 61 74 20 74 68 69 73 20 56 44 42 45 20 6d 61 79  at this VDBE may
30dc5 20 72 65 71 75 69 72 65 0a 20 20 20 20 20 20 20   require.       
30dc6 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 72 6f   ** statement ro
30dc7 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 65 20 73  llback (if the s
30dc8 74 61 74 65 6d 65 6e 74 20 69 73 20 61 62 6f 72  tatement is abor
30dc9 74 65 64 20 61 66 74 65 72 20 74 68 65 20 64 65  ted after the de
30dca 6c 65 74 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  lete.        ** 
30dcb 74 61 6b 65 73 20 70 6c 61 63 65 29 2e 20 45 61  takes place). Ea
30dcc 72 6c 69 65 72 20 76 65 72 73 69 6f 6e 73 20 63  rlier versions c
30dcd 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 4d 75 6c  alled sqlite3Mul
30dce 74 69 57 72 69 74 65 28 29 20 72 65 67 61 72 64  tiWrite() regard
30dcf 6c 65 73 73 2c 0a 20 20 20 20 20 20 20 20 2a 2a  less,.        **
30dd0 20 62 75 74 20 62 65 69 6e 67 20 6d 6f 72 65 20   but being more 
30dd1 73 65 6c 65 63 74 69 76 65 20 68 65 72 65 20 61  selective here a
30dd2 6c 6c 6f 77 73 20 73 74 61 74 65 6d 65 6e 74 73  llows statements
30dd3 20 6c 69 6b 65 3a 0a 20 20 20 20 20 20 20 20 2a   like:.        *
30dd4 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 52  *.        **   R
30dd5 45 50 4c 41 43 45 20 49 4e 54 4f 20 74 28 72 6f  EPLACE INTO t(ro
30dd6 77 69 64 29 20 56 41 4c 55 45 53 28 24 6e 65 77  wid) VALUES($new
30dd7 72 6f 77 69 64 29 0a 20 20 20 20 20 20 20 20 2a  rowid).        *
30dd8 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20  *.        ** to 
30dd9 72 75 6e 20 77 69 74 68 6f 75 74 20 61 20 73 74  run without a st
30dda 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
30ddb 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  if there are no 
30ddc 69 6e 64 65 78 65 73 20 6f 6e 20 74 68 65 0a 20  indexes on the. 
30ddd 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e         ** table.
30dde 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
30ddf 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 54 72      Trigger *pTr
30de0 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 20 20 20  igger = 0;.     
30de1 20 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73     if( db->flags
30de2 26 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67 67  &SQLITE_RecTrigg
30de3 65 72 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ers ){.         
30de4 20 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69   pTrigger = sqli
30de5 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74  te3TriggersExist
30de6 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 54  (pParse, pTab, T
30de7 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20 30 29 3b  K_DELETE, 0, 0);
30de8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
30de9 20 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20     if( pTrigger 
30dea 7c 7c 20 73 71 6c 69 74 65 33 46 6b 52 65 71 75  || sqlite3FkRequ
30deb 69 72 65 64 28 70 50 61 72 73 65 2c 20 70 54 61  ired(pParse, pTa
30dec 62 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20  b, 0, 0) ){.    
30ded 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 75 6c        sqlite3Mul
30dee 74 69 57 72 69 74 65 28 70 50 61 72 73 65 29 3b  tiWrite(pParse);
30def 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
30df0 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c  e3GenerateRowDel
30df1 65 74 65 28 0a 20 20 20 20 20 20 20 20 20 20 20  ete(.           
30df2 20 20 20 70 50 61 72 73 65 2c 20 70 54 61 62 2c     pParse, pTab,
30df3 20 62 61 73 65 43 75 72 2c 20 72 65 67 52 6f 77   baseCur, regRow
30df4 69 64 2c 20 30 2c 20 70 54 72 69 67 67 65 72 2c  id, 0, pTrigger,
30df5 20 4f 45 5f 52 65 70 6c 61 63 65 0a 20 20 20 20   OE_Replace.    
30df6 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20        );.       
30df7 20 7d 65 6c 73 65 20 69 66 28 20 70 54 61 62 2d   }else if( pTab-
30df8 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20  >pIndex ){.     
30df9 20 20 20 20 20 73 71 6c 69 74 65 33 4d 75 6c 74       sqlite3Mult
30dfa 69 57 72 69 74 65 28 70 50 61 72 73 65 29 3b 0a  iWrite(pParse);.
30dfb 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
30dfc 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65  3GenerateRowInde
30dfd 78 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20  xDelete(pParse, 
30dfe 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 30  pTab, baseCur, 0
30dff 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
30e00 20 20 20 20 20 73 65 65 6e 52 65 70 6c 61 63 65       seenReplace
30e01 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72   = 1;.        br
30e02 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
30e03 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72     case OE_Ignor
30e04 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73 73  e: {.        ass
30e05 65 72 74 28 20 73 65 65 6e 52 65 70 6c 61 63 65  ert( seenReplace
30e06 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 73  ==0 );.        s
30e07 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
30e08 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
30e09 69 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20  ignoreDest);.   
30e0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30e0b 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
30e0c 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
30e0d 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20 69 66  e(v, j3);.    if
30e0e 28 20 69 73 55 70 64 61 74 65 20 29 7b 0a 20 20  ( isUpdate ){.  
30e0f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
30e10 75 6d 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a  umpHere(v, j2);.
30e11 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
30e12 54 65 73 74 20 61 6c 6c 20 55 4e 49 51 55 45 20  Test all UNIQUE 
30e13 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 79 20 63  constraints by c
30e14 72 65 61 74 69 6e 67 20 65 6e 74 72 69 65 73 20  reating entries 
30e15 66 6f 72 20 65 61 63 68 20 55 4e 49 51 55 45 0a  for each UNIQUE.
30e16 20 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20 6d    ** index and m
30e17 61 6b 69 6e 67 20 73 75 72 65 20 74 68 61 74 20  aking sure that 
30e18 64 75 70 6c 69 63 61 74 65 20 65 6e 74 72 69 65  duplicate entrie
30e19 73 20 64 6f 20 6e 6f 74 20 61 6c 72 65 61 64 79  s do not already
30e1a 20 65 78 69 73 74 2e 0a 20 20 2a 2a 20 41 64 64   exist..  ** Add
30e1b 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 73   the new records
30e1c 20 74 6f 20 74 68 65 20 69 6e 64 69 63 65 73 20   to the indices 
30e1d 61 73 20 77 65 20 67 6f 2e 0a 20 20 2a 2f 0a 20  as we go..  */. 
30e1e 20 66 6f 72 28 69 43 75 72 3d 30 2c 20 70 49 64   for(iCur=0, pId
30e1f 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
30e20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
30e21 3e 70 4e 65 78 74 2c 20 69 43 75 72 2b 2b 29 7b  >pNext, iCur++){
30e22 0a 20 20 20 20 69 6e 74 20 72 65 67 49 64 78 3b  .    int regIdx;
30e23 0a 20 20 20 20 69 6e 74 20 72 65 67 52 3b 0a 0a  .    int regR;..
30e24 20 20 20 20 69 66 28 20 61 52 65 67 49 64 78 5b      if( aRegIdx[
30e25 69 43 75 72 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  iCur]==0 ) conti
30e26 6e 75 65 3b 20 20 2f 2a 20 53 6b 69 70 20 75 6e  nue;  /* Skip un
30e27 75 73 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a  used indices */.
30e28 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61  .    /* Create a
30e29 20 6b 65 79 20 66 6f 72 20 61 63 63 65 73 73 69   key for accessi
30e2a 6e 67 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74  ng the index ent
30e2b 72 79 20 2a 2f 0a 20 20 20 20 72 65 67 49 64 78  ry */.    regIdx
30e2c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
30e2d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70  pRange(pParse, p
30e2e 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b  Idx->nColumn+1);
30e2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
30e30 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  pIdx->nColumn; i
30e31 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  ++){.      int i
30e32 64 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c  dx = pIdx->aiCol
30e33 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66  umn[i];.      if
30e34 28 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b  ( idx==pTab->iPK
30e35 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ey ){.        sq
30e36 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
30e37 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67  v, OP_SCopy, reg
30e38 52 6f 77 69 64 2c 20 72 65 67 49 64 78 2b 69 29  Rowid, regIdx+i)
30e39 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
30e3a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
30e3b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
30e3c 43 6f 70 79 2c 20 72 65 67 44 61 74 61 2b 69 64  Copy, regData+id
30e3d 78 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20  x, regIdx+i);.  
30e3e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
30e3f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
30e40 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72  2(v, OP_SCopy, r
30e41 65 67 52 6f 77 69 64 2c 20 72 65 67 49 64 78 2b  egRowid, regIdx+
30e42 69 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  i);.    sqlite3V
30e43 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
30e44 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 49  MakeRecord, regI
30e45 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  dx, pIdx->nColum
30e46 6e 2b 31 2c 20 61 52 65 67 49 64 78 5b 69 43 75  n+1, aRegIdx[iCu
30e47 72 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  r]);.    sqlite3
30e48 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
30e49 2d 31 2c 20 73 71 6c 69 74 65 33 49 6e 64 65 78  -1, sqlite3Index
30e4a 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70  AffinityStr(v, p
30e4b 49 64 78 29 2c 20 50 34 5f 54 52 41 4e 53 49 45  Idx), P4_TRANSIE
30e4c 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  NT);.    sqlite3
30e4d 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
30e4e 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
30e4f 72 65 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43  regIdx, pIdx->nC
30e50 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20 20 20 20 2f  olumn+1);..    /
30e51 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 61 74 20  * Find out what 
30e52 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20 69  action to take i
30e53 6e 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20  n case there is 
30e54 61 6e 20 69 6e 64 65 78 69 6e 67 20 63 6f 6e 66  an indexing conf
30e55 6c 69 63 74 20 2a 2f 0a 20 20 20 20 6f 6e 45 72  lict */.    onEr
30e56 72 6f 72 20 3d 20 70 49 64 78 2d 3e 6f 6e 45 72  ror = pIdx->onEr
30e57 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45  ror;.    if( onE
30e58 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 29 7b  rror==OE_None ){
30e59 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52   .      sqlite3R
30e5a 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28  eleaseTempRange(
30e5b 70 50 61 72 73 65 2c 20 72 65 67 49 64 78 2c 20  pParse, regIdx, 
30e5c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29  pIdx->nColumn+1)
30e5d 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  ;.      continue
30e5e 3b 20 20 2f 2a 20 70 49 64 78 20 69 73 20 6e 6f  ;  /* pIdx is no
30e5f 74 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78  t a UNIQUE index
30e60 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   */.    }.    if
30e61 28 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 21  ( overrideError!
30e62 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20  =OE_Default ){. 
30e63 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f       onError = o
30e64 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0a 20 20  verrideError;.  
30e65 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45 72    }else if( onEr
30e66 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror==OE_Default 
30e67 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
30e68 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   = OE_Abort;.   
30e69 20 7d 0a 20 20 20 20 69 66 28 20 73 65 65 6e 52   }.    if( seenR
30e6a 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20  eplace ){.      
30e6b 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
30e6c 49 67 6e 6f 72 65 20 29 20 6f 6e 45 72 72 6f 72  Ignore ) onError
30e6d 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 0a 20   = OE_Replace;. 
30e6e 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 6f 6e       else if( on
30e6f 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 20 29  Error==OE_Fail )
30e70 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62   onError = OE_Ab
30e71 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a  ort;.    }.    .
30e72 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20      /* Check to 
30e73 73 65 65 20 69 66 20 74 68 65 20 6e 65 77 20 69  see if the new i
30e74 6e 64 65 78 20 65 6e 74 72 79 20 77 69 6c 6c 20  ndex entry will 
30e75 62 65 20 75 6e 69 71 75 65 20 2a 2f 0a 20 20 20  be unique */.   
30e76 20 72 65 67 52 20 3d 20 73 71 6c 69 74 65 33 47   regR = sqlite3G
30e77 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
30e78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
30e79 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
30e7a 43 6f 70 79 2c 20 72 65 67 4f 6c 64 52 6f 77 69  Copy, regOldRowi
30e7b 64 2c 20 72 65 67 52 29 3b 0a 20 20 20 20 6a 33  d, regR);.    j3
30e7c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
30e7d 64 4f 70 34 28 76 2c 20 4f 50 5f 49 73 55 6e 69  dOp4(v, OP_IsUni
30e7e 71 75 65 2c 20 62 61 73 65 43 75 72 2b 69 43 75  que, baseCur+iCu
30e7f 72 2b 31 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  r+1, 0,.        
30e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e81 20 20 20 72 65 67 52 2c 20 53 51 4c 49 54 45 5f     regR, SQLITE_
30e82 49 4e 54 5f 54 4f 5f 50 54 52 28 72 65 67 49 64  INT_TO_PTR(regId
30e83 78 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  x),.            
30e84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
30e85 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 73 71  4_INT32);.    sq
30e86 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
30e87 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  Range(pParse, re
30e88 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c  gIdx, pIdx->nCol
30e89 75 6d 6e 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20  umn+1);..    /* 
30e8a 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
30e8b 61 74 20 65 78 65 63 75 74 65 73 20 69 66 20 74  at executes if t
30e8c 68 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74  he new index ent
30e8d 72 79 20 69 73 20 6e 6f 74 20 75 6e 69 71 75 65  ry is not unique
30e8e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
30e8f 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c  onError==OE_Roll
30e90 62 61 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d  back || onError=
30e91 3d 4f 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45  =OE_Abort || onE
30e92 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20  rror==OE_Fail.  
30e93 20 20 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72        || onError
30e94 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f  ==OE_Ignore || o
30e95 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61  nError==OE_Repla
30e96 63 65 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68  ce );.    switch
30e97 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20  ( onError ){.   
30e98 20 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62     case OE_Rollb
30e99 61 63 6b 3a 0a 20 20 20 20 20 20 63 61 73 65 20  ack:.      case 
30e9a 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20  OE_Abort:.      
30e9b 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a  case OE_Fail: {.
30e9c 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20          int j;. 
30e9d 20 20 20 20 20 20 20 53 74 72 41 63 63 75 6d 20         StrAccum 
30e9e 65 72 72 4d 73 67 3b 0a 20 20 20 20 20 20 20 20  errMsg;.        
30e9f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70  const char *zSep
30ea0 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  ;.        char *
30ea1 7a 45 72 72 3b 0a 0a 20 20 20 20 20 20 20 20 73  zErr;..        s
30ea2 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e  qlite3StrAccumIn
30ea3 69 74 28 26 65 72 72 4d 73 67 2c 20 30 2c 20 30  it(&errMsg, 0, 0
30ea4 2c 20 32 30 30 29 3b 0a 20 20 20 20 20 20 20 20  , 200);.        
30ea5 65 72 72 4d 73 67 2e 64 62 20 3d 20 64 62 3b 0a  errMsg.db = db;.
30ea6 20 20 20 20 20 20 20 20 7a 53 65 70 20 3d 20 70          zSep = p
30ea7 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31 20 3f  Idx->nColumn>1 ?
30ea8 20 22 63 6f 6c 75 6d 6e 73 20 22 20 3a 20 22 63   "columns " : "c
30ea9 6f 6c 75 6d 6e 20 22 3b 0a 20 20 20 20 20 20 20  olumn ";.       
30eaa 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78   for(j=0; j<pIdx
30eab 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b  ->nColumn; j++){
30eac 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20  .          char 
30ead 2a 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43  *zCol = pTab->aC
30eae 6f 6c 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d  ol[pIdx->aiColum
30eaf 6e 5b 6a 5d 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  n[j]].zName;.   
30eb0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
30eb1 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 65 72  rAccumAppend(&er
30eb2 72 4d 73 67 2c 20 7a 53 65 70 2c 20 2d 31 29 3b  rMsg, zSep, -1);
30eb3 0a 20 20 20 20 20 20 20 20 20 20 7a 53 65 70 20  .          zSep 
30eb4 3d 20 22 2c 20 22 3b 0a 20 20 20 20 20 20 20 20  = ", ";.        
30eb5 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
30eb6 6d 41 70 70 65 6e 64 28 26 65 72 72 4d 73 67 2c  mAppend(&errMsg,
30eb7 20 7a 43 6f 6c 2c 20 2d 31 29 3b 0a 20 20 20 20   zCol, -1);.    
30eb8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
30eb9 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
30eba 65 6e 64 28 26 65 72 72 4d 73 67 2c 0a 20 20 20  end(&errMsg,.   
30ebb 20 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 6e           pIdx->n
30ebc 43 6f 6c 75 6d 6e 3e 31 20 3f 20 22 20 61 72 65  Column>1 ? " are
30ebd 20 6e 6f 74 20 75 6e 69 71 75 65 22 20 3a 20 22   not unique" : "
30ebe 20 69 73 20 6e 6f 74 20 75 6e 69 71 75 65 22 2c   is not unique",
30ebf 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 7a 45   -1);.        zE
30ec0 72 72 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41  rr = sqlite3StrA
30ec1 63 63 75 6d 46 69 6e 69 73 68 28 26 65 72 72 4d  ccumFinish(&errM
30ec2 73 67 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  sg);.        sql
30ec3 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69  ite3HaltConstrai
30ec4 6e 74 28 70 50 61 72 73 65 2c 20 6f 6e 45 72 72  nt(pParse, onErr
30ec5 6f 72 2c 20 7a 45 72 72 2c 20 30 29 3b 0a 20 20  or, zErr, 0);.  
30ec6 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
30ec7 72 65 65 28 65 72 72 4d 73 67 2e 64 62 2c 20 7a  ree(errMsg.db, z
30ec8 45 72 72 29 3b 0a 20 20 20 20 20 20 20 20 62 72  Err);.        br
30ec9 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
30eca 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72     case OE_Ignor
30ecb 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73 73  e: {.        ass
30ecc 65 72 74 28 20 73 65 65 6e 52 65 70 6c 61 63 65  ert( seenReplace
30ecd 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 73  ==0 );.        s
30ece 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
30ecf 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
30ed0 69 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20  ignoreDest);.   
30ed1 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30ed2 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c    }.      defaul
30ed3 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 54 72 69  t: {.        Tri
30ed4 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 20 3d  gger *pTrigger =
30ed5 20 30 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65   0;.        asse
30ed6 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  rt( onError==OE_
30ed7 52 65 70 6c 61 63 65 20 29 3b 0a 20 20 20 20 20  Replace );.     
30ed8 20 20 20 73 71 6c 69 74 65 33 4d 75 6c 74 69 57     sqlite3MultiW
30ed9 72 69 74 65 28 70 50 61 72 73 65 29 3b 0a 20 20  rite(pParse);.  
30eda 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 66 6c        if( db->fl
30edb 61 67 73 26 53 51 4c 49 54 45 5f 52 65 63 54 72  ags&SQLITE_RecTr
30edc 69 67 67 65 72 73 20 29 7b 0a 20 20 20 20 20 20  iggers ){.      
30edd 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20 73      pTrigger = s
30ede 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78  qlite3TriggersEx
30edf 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62  ist(pParse, pTab
30ee0 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20  , TK_DELETE, 0, 
30ee1 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  0);.        }.  
30ee2 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e        sqlite3Gen
30ee3 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 0a  erateRowDelete(.
30ee4 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72              pPar
30ee5 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75  se, pTab, baseCu
30ee6 72 2c 20 72 65 67 52 2c 20 30 2c 20 70 54 72 69  r, regR, 0, pTri
30ee7 67 67 65 72 2c 20 4f 45 5f 52 65 70 6c 61 63 65  gger, OE_Replace
30ee8 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .        );.    
30ee9 20 20 20 20 73 65 65 6e 52 65 70 6c 61 63 65 20      seenReplace 
30eea 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = 1;.        bre
30eeb 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
30eec 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
30eed 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29  eJumpHere(v, j3)
30eee 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c  ;.    sqlite3Rel
30eef 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
30ef0 73 65 2c 20 72 65 67 52 29 3b 0a 20 20 7d 0a 20  se, regR);.  }. 
30ef1 20 0a 20 20 69 66 28 20 70 62 4d 61 79 52 65 70   .  if( pbMayRep
30ef2 6c 61 63 65 20 29 7b 0a 20 20 20 20 2a 70 62 4d  lace ){.    *pbM
30ef3 61 79 52 65 70 6c 61 63 65 20 3d 20 73 65 65 6e  ayReplace = seen
30ef4 52 65 70 6c 61 63 65 3b 0a 20 20 7d 0a 7d 0a 0a  Replace;.  }.}..
30ef5 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
30ef6 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64  ne generates cod
30ef7 65 20 74 6f 20 66 69 6e 69 73 68 20 74 68 65 20  e to finish the 
30ef8 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54 45  INSERT or UPDATE
30ef9 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 74 68   operation.** th
30efa 61 74 20 77 61 73 20 73 74 61 72 74 65 64 20 62  at was started b
30efb 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74  y a prior call t
30efc 6f 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  o sqlite3Generat
30efd 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b  eConstraintCheck
30efe 73 2e 0a 2a 2a 20 41 20 63 6f 6e 73 65 63 75 74  s..** A consecut
30eff 69 76 65 20 72 61 6e 67 65 20 6f 66 20 72 65 67  ive range of reg
30f00 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20  isters starting 
30f01 61 74 20 72 65 67 52 6f 77 69 64 20 63 6f 6e 74  at regRowid cont
30f02 61 69 6e 73 20 74 68 65 0a 2a 2a 20 72 6f 77 69  ains the.** rowi
30f03 64 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e  d and the conten
30f04 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  t to be inserted
30f05 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75  ..**.** The argu
30f06 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 72 6f  ments to this ro
30f07 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 20  utine should be 
30f08 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
30f09 66 69 72 73 74 20 73 69 78 0a 2a 2a 20 61 72 67  first six.** arg
30f0a 75 6d 65 6e 74 73 20 74 6f 20 73 71 6c 69 74 65  uments to sqlite
30f0b 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61  3GenerateConstra
30f0c 69 6e 74 43 68 65 63 6b 73 2e 0a 2a 2f 0a 53 51  intChecks..*/.SQ
30f0d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
30f0e 64 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74  d sqlite3Complet
30f0f 65 49 6e 73 65 72 74 69 6f 6e 28 0a 20 20 50 61  eInsertion(.  Pa
30f10 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
30f11 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
30f12 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
30f13 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  le *pTab,       
30f14 20 2f 2a 20 74 68 65 20 74 61 62 6c 65 20 69 6e   /* the table in
30f15 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20  to which we are 
30f16 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 69  inserting */.  i
30f17 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20 20 20  nt baseCur,     
30f18 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61     /* Index of a
30f19 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   read/write curs
30f1a 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 70  or pointing at p
30f1b 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  Tab */.  int reg
30f1c 52 6f 77 69 64 2c 20 20 20 20 20 20 20 2f 2a 20  Rowid,       /* 
30f1d 52 61 6e 67 65 20 6f 66 20 63 6f 6e 74 65 6e 74  Range of content
30f1e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49   */.  int *aRegI
30f1f 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 52 65 67  dx,       /* Reg
30f20 69 73 74 65 72 20 75 73 65 64 20 62 79 20 65 61  ister used by ea
30f21 63 68 20 69 6e 64 65 78 2e 20 20 30 20 66 6f 72  ch index.  0 for
30f22 20 75 6e 75 73 65 64 20 69 6e 64 69 63 65 73 20   unused indices 
30f23 2a 2f 0a 20 20 69 6e 74 20 69 73 55 70 64 61 74  */.  int isUpdat
30f24 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  e,       /* True
30f25 20 66 6f 72 20 55 50 44 41 54 45 2c 20 46 61 6c   for UPDATE, Fal
30f26 73 65 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f  se for INSERT */
30f27 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 42 69 61  .  int appendBia
30f28 73 2c 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  s,     /* True i
30f29 66 20 74 68 69 73 20 69 73 20 6c 69 6b 65 6c 79  f this is likely
30f2a 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e 64   to be an append
30f2b 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65 53 65 65   */.  int useSee
30f2c 6b 52 65 73 75 6c 74 20 20 20 2f 2a 20 54 72 75  kResult   /* Tru
30f2d 65 20 74 6f 20 73 65 74 20 74 68 65 20 55 53 45  e to set the USE
30f2e 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61 67 20  SEEKRESULT flag 
30f2f 6f 6e 20 4f 50 5f 5b 49 64 78 5d 49 6e 73 65 72  on OP_[Idx]Inser
30f30 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  t */.){.  int i;
30f31 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e  .  Vdbe *v;.  in
30f32 74 20 6e 49 64 78 3b 0a 20 20 49 6e 64 65 78 20  t nIdx;.  Index 
30f33 2a 70 49 64 78 3b 0a 20 20 75 38 20 70 69 6b 5f  *pIdx;.  u8 pik_
30f34 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 72 65 67  flags;.  int reg
30f35 44 61 74 61 3b 0a 20 20 69 6e 74 20 72 65 67 52  Data;.  int regR
30f36 65 63 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74  ec;..  v = sqlit
30f37 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
30f38 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d  );.  assert( v!=
30f39 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
30f3a 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20  Tab->pSelect==0 
30f3b 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61 62 6c  );  /* This tabl
30f3c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45 57 20  e is not a VIEW 
30f3d 2a 2f 0a 20 20 66 6f 72 28 6e 49 64 78 3d 30 2c  */.  for(nIdx=0,
30f3e 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64   pIdx=pTab->pInd
30f3f 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
30f40 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6e 49 64 78  Idx->pNext, nIdx
30f41 2b 2b 29 7b 7d 0a 20 20 66 6f 72 28 69 3d 6e 49  ++){}.  for(i=nI
30f42 64 78 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29  dx-1; i>=0; i--)
30f43 7b 0a 20 20 20 20 69 66 28 20 61 52 65 67 49 64  {.    if( aRegId
30f44 78 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  x[i]==0 ) contin
30f45 75 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ue;.    sqlite3V
30f46 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
30f47 49 64 78 49 6e 73 65 72 74 2c 20 62 61 73 65 43  IdxInsert, baseC
30f48 75 72 2b 69 2b 31 2c 20 61 52 65 67 49 64 78 5b  ur+i+1, aRegIdx[
30f49 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 75 73 65  i]);.    if( use
30f4a 53 65 65 6b 52 65 73 75 6c 74 20 29 7b 0a 20 20  SeekResult ){.  
30f4b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
30f4c 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41  hangeP5(v, OPFLA
30f4d 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29  G_USESEEKRESULT)
30f4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
30f4f 67 44 61 74 61 20 3d 20 72 65 67 52 6f 77 69 64  gData = regRowid
30f50 20 2b 20 31 3b 0a 20 20 72 65 67 52 65 63 20 3d   + 1;.  regRec =
30f51 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
30f52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71  eg(pParse);.  sq
30f53 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
30f54 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
30f55 2c 20 72 65 67 44 61 74 61 2c 20 70 54 61 62 2d  , regData, pTab-
30f56 3e 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29 3b 0a  >nCol, regRec);.
30f57 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66    sqlite3TableAf
30f58 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 54 61  finityStr(v, pTa
30f59 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  b);.  sqlite3Exp
30f5a 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
30f5b 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
30f5c 44 61 74 61 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  Data, pTab->nCol
30f5d 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  );.  if( pParse-
30f5e 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20 70  >nested ){.    p
30f5f 69 6b 5f 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20  ik_flags = 0;.  
30f60 7d 65 6c 73 65 7b 0a 20 20 20 20 70 69 6b 5f 66  }else{.    pik_f
30f61 6c 61 67 73 20 3d 20 4f 50 46 4c 41 47 5f 4e 43  lags = OPFLAG_NC
30f62 48 41 4e 47 45 3b 0a 20 20 20 20 70 69 6b 5f 66  HANGE;.    pik_f
30f63 6c 61 67 73 20 7c 3d 20 28 69 73 55 70 64 61 74  lags |= (isUpdat
30f64 65 3f 4f 50 46 4c 41 47 5f 49 53 55 50 44 41 54  e?OPFLAG_ISUPDAT
30f65 45 3a 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57  E:OPFLAG_LASTROW
30f66 49 44 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 61  ID);.  }.  if( a
30f67 70 70 65 6e 64 42 69 61 73 20 29 7b 0a 20 20 20  ppendBias ){.   
30f68 20 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20 4f 50   pik_flags |= OP
30f69 46 4c 41 47 5f 41 50 50 45 4e 44 3b 0a 20 20 7d  FLAG_APPEND;.  }
30f6a 0a 20 20 69 66 28 20 75 73 65 53 65 65 6b 52 65  .  if( useSeekRe
30f6b 73 75 6c 74 20 29 7b 0a 20 20 20 20 70 69 6b 5f  sult ){.    pik_
30f6c 66 6c 61 67 73 20 7c 3d 20 4f 50 46 4c 41 47 5f  flags |= OPFLAG_
30f6d 55 53 45 53 45 45 4b 52 45 53 55 4c 54 3b 0a 20  USESEEKRESULT;. 
30f6e 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
30f6f 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73  AddOp3(v, OP_Ins
30f70 65 72 74 2c 20 62 61 73 65 43 75 72 2c 20 72 65  ert, baseCur, re
30f71 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64 29 3b  gRec, regRowid);
30f72 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e  .  if( !pParse->
30f73 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20 73 71  nested ){.    sq
30f74 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
30f75 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a  4(v, -1, pTab->z
30f76 4e 61 6d 65 2c 20 50 34 5f 54 52 41 4e 53 49 45  Name, P4_TRANSIE
30f77 4e 54 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  NT);.  }.  sqlit
30f78 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
30f79 2c 20 70 69 6b 5f 66 6c 61 67 73 29 3b 0a 7d 0a  , pik_flags);.}.
30f7a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
30f7b 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 6f  code that will o
30f7c 70 65 6e 20 63 75 72 73 6f 72 73 20 66 6f 72 20  pen cursors for 
30f7d 61 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20  a table and for 
30f7e 61 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20 6f  all.** indices o
30f7f 66 20 74 68 61 74 20 74 61 62 6c 65 2e 20 20 54  f that table.  T
30f80 68 65 20 22 62 61 73 65 43 75 72 22 20 70 61 72  he "baseCur" par
30f81 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 63 75  ameter is the cu
30f82 72 73 6f 72 20 6e 75 6d 62 65 72 20 75 73 65 64  rsor number used
30f83 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c  .** for the tabl
30f84 65 2e 20 20 49 6e 64 69 63 65 73 20 61 72 65 20  e.  Indices are 
30f85 6f 70 65 6e 65 64 20 6f 6e 20 73 75 62 73 65 71  opened on subseq
30f86 75 65 6e 74 20 63 75 72 73 6f 72 73 2e 0a 2a 2a  uent cursors..**
30f87 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
30f88 75 6d 62 65 72 20 6f 66 20 69 6e 64 69 63 65 73  umber of indices
30f89 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a   on the table..*
30f8a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
30f8b 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e   int sqlite3Open
30f8c 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28  TableAndIndices(
30f8d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
30f8e 2c 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63  ,   /* Parsing c
30f8f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
30f90 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20  e *pTab,     /* 
30f91 54 61 62 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e  Table to be open
30f92 65 64 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65  ed */.  int base
30f93 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 72 73  Cur,     /* Curs
30f94 6f 72 20 6e 75 6d 62 65 72 20 61 73 73 69 67 6e  or number assign
30f95 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ed to the table 
30f96 2a 2f 0a 20 20 69 6e 74 20 6f 70 20 20 20 20 20  */.  int op     
30f97 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e        /* OP_Open
30f98 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e 57  Read or OP_OpenW
30f99 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  rite */.){.  int
30f9a 20 69 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20   i;.  int iDb;. 
30f9b 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
30f9c 56 64 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20  Vdbe *v;..  if( 
30f9d 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
30f9e 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 44  ) return 0;.  iD
30f9f 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
30fa0 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
30fa1 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  >db, pTab->pSche
30fa2 6d 61 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74  ma);.  v = sqlit
30fa3 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
30fa4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d  );.  assert( v!=
30fa5 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 70  0 );.  sqlite3Op
30fa6 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  enTable(pParse, 
30fa7 62 61 73 65 43 75 72 2c 20 69 44 62 2c 20 70 54  baseCur, iDb, pT
30fa8 61 62 2c 20 6f 70 29 3b 0a 20 20 66 6f 72 28 69  ab, op);.  for(i
30fa9 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70  =1, pIdx=pTab->p
30faa 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
30fab 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69  x=pIdx->pNext, i
30fac 2b 2b 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f  ++){.    KeyInfo
30fad 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33   *pKey = sqlite3
30fae 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61  IndexKeyinfo(pPa
30faf 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20  rse, pIdx);.    
30fb0 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70 53  assert( pIdx->pS
30fb1 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63  chema==pTab->pSc
30fb2 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c 69  hema );.    sqli
30fb3 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
30fb4 20 6f 70 2c 20 69 2b 62 61 73 65 43 75 72 2c 20   op, i+baseCur, 
30fb5 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c  pIdx->tnum, iDb,
30fb6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
30fb7 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
30fb8 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  ey, P4_KEYINFO_H
30fb9 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62  ANDOFF);.    Vdb
30fba 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73  eComment((v, "%s
30fbb 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29  ", pIdx->zName))
30fbc 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72  ;.  }.  if( pPar
30fbd 73 65 2d 3e 6e 54 61 62 3c 62 61 73 65 43 75 72  se->nTab<baseCur
30fbe 2b 69 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65  +i ){.    pParse
30fbf 2d 3e 6e 54 61 62 20 3d 20 62 61 73 65 43 75 72  ->nTab = baseCur
30fc0 2b 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  +i;.  }.  return
30fc1 20 69 2d 31 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66   i-1;.}...#ifdef
30fc2 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a   SQLITE_TEST./*.
30fc3 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
30fc4 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
30fc5 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
30fc6 77 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20  whenever the.** 
30fc7 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a  transfer optimiz
30fc8 61 74 69 6f 6e 20 69 73 20 75 73 65 64 2e 20 20  ation is used.  
30fc9 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72  This is used for
30fca 20 74 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72 70   testing.** purp
30fcb 6f 73 65 73 20 6f 6e 6c 79 20 2d 20 74 6f 20 6d  oses only - to m
30fcc 61 6b 65 20 73 75 72 65 20 74 68 65 20 74 72 61  ake sure the tra
30fcd 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69  nsfer optimizati
30fce 6f 6e 20 72 65 61 6c 6c 79 0a 2a 2a 20 69 73 20  on really.** is 
30fcf 68 61 70 70 65 6e 69 6e 67 20 77 68 65 6e 20 69  happening when i
30fd0 74 20 69 73 20 73 75 70 70 6f 73 65 20 74 6f 2e  t is suppose to.
30fd1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
30fd2 6e 74 20 73 71 6c 69 74 65 33 5f 78 66 65 72 6f  nt sqlite3_xfero
30fd3 70 74 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66  pt_count;.#endif
30fd4 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20   /* SQLITE_TEST 
30fd5 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  */...#ifndef SQL
30fd6 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50  ITE_OMIT_XFER_OP
30fd7 54 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  T./*.** Check to
30fd8 20 63 6f 6c 6c 61 74 69 6f 6e 20 6e 61 6d 65 73   collation names
30fd9 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 79 20   to see if they 
30fda 61 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a  are compatible..
30fdb 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78 66  */.static int xf
30fdc 65 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c 6c  erCompatibleColl
30fdd 61 74 69 6f 6e 28 63 6f 6e 73 74 20 63 68 61 72  ation(const char
30fde 20 2a 7a 31 2c 20 63 6f 6e 73 74 20 63 68 61 72   *z1, const char
30fdf 20 2a 7a 32 29 7b 0a 20 20 69 66 28 20 7a 31 3d   *z2){.  if( z1=
30fe0 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
30fe1 20 7a 32 3d 3d 30 3b 0a 20 20 7d 0a 20 20 69 66   z2==0;.  }.  if
30fe2 28 20 7a 32 3d 3d 30 20 29 7b 0a 20 20 20 20 72  ( z2==0 ){.    r
30fe3 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72  eturn 0;.  }.  r
30fe4 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72  eturn sqlite3Str
30fe5 49 43 6d 70 28 7a 31 2c 20 7a 32 29 3d 3d 30 3b  ICmp(z1, z2)==0;
30fe6 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  .}.../*.** Check
30fe7 20 74 6f 20 73 65 65 20 69 66 20 69 6e 64 65 78   to see if index
30fe8 20 70 53 72 63 20 69 73 20 63 6f 6d 70 61 74 69   pSrc is compati
30fe9 62 6c 65 20 61 73 20 61 20 73 6f 75 72 63 65 20  ble as a source 
30fea 6f 66 20 64 61 74 61 0a 2a 2a 20 66 6f 72 20 69  of data.** for i
30feb 6e 64 65 78 20 70 44 65 73 74 20 69 6e 20 61 6e  ndex pDest in an
30fec 20 69 6e 73 65 72 74 20 74 72 61 6e 73 66 65 72   insert transfer
30fed 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20   optimization.  
30fee 54 68 65 20 72 75 6c 65 73 0a 2a 2a 20 66 6f 72  The rules.** for
30fef 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 69 6e   a compatible in
30ff0 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  dex:.**.**    * 
30ff1 20 20 54 68 65 20 69 6e 64 65 78 20 69 73 20 6f    The index is o
30ff2 76 65 72 20 74 68 65 20 73 61 6d 65 20 73 65 74  ver the same set
30ff3 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20   of columns.**  
30ff4 20 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20 44    *   The same D
30ff5 45 53 43 20 61 6e 64 20 41 53 43 20 6d 61 72 6b  ESC and ASC mark
30ff6 69 6e 67 73 20 6f 63 63 75 72 73 20 6f 6e 20 61  ings occurs on a
30ff7 6c 6c 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20  ll columns.**   
30ff8 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20 6f 6e   *   The same on
30ff9 45 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e 67  Error processing
30ffa 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49   (OE_Abort, OE_I
30ffb 67 6e 6f 72 65 2c 20 65 74 63 29 0a 2a 2a 20 20  gnore, etc).**  
30ffc 20 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20 63    *   The same c
30ffd 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
30ffe 65 20 6f 6e 20 65 61 63 68 20 63 6f 6c 75 6d 6e  e on each column
30fff 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78  .*/.static int x
31000 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e 64  ferCompatibleInd
31001 65 78 28 49 6e 64 65 78 20 2a 70 44 65 73 74 2c  ex(Index *pDest,
31002 20 49 6e 64 65 78 20 2a 70 53 72 63 29 7b 0a 20   Index *pSrc){. 
31003 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74   int i;.  assert
31004 28 20 70 44 65 73 74 20 26 26 20 70 53 72 63 20  ( pDest && pSrc 
31005 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 65  );.  assert( pDe
31006 73 74 2d 3e 70 54 61 62 6c 65 21 3d 70 53 72 63  st->pTable!=pSrc
31007 2d 3e 70 54 61 62 6c 65 20 29 3b 0a 20 20 69 66  ->pTable );.  if
31008 28 20 70 44 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e  ( pDest->nColumn
31009 21 3d 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 20  !=pSrc->nColumn 
3100a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
3100b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20     /* Different 
3100c 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
3100d 73 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70  s */.  }.  if( p
3100e 44 65 73 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70  Dest->onError!=p
3100f 53 72 63 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a  Src->onError ){.
31010 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
31011 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6e  /* Different con
31012 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e  flict resolution
31013 20 73 74 72 61 74 65 67 69 65 73 20 2a 2f 0a 20   strategies */. 
31014 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
31015 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  pSrc->nColumn; i
31016 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 53 72  ++){.    if( pSr
31017 63 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 21 3d  c->aiColumn[i]!=
31018 70 44 65 73 74 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  pDest->aiColumn[
31019 69 5d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  i] ){.      retu
3101a 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65  rn 0;   /* Diffe
3101b 72 65 6e 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 64  rent columns ind
3101c 65 78 65 64 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  exed */.    }.  
3101d 20 20 69 66 28 20 70 53 72 63 2d 3e 61 53 6f 72    if( pSrc->aSor
3101e 74 4f 72 64 65 72 5b 69 5d 21 3d 70 44 65 73 74  tOrder[i]!=pDest
3101f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20  ->aSortOrder[i] 
31020 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
31021 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e  0;   /* Differen
31022 74 20 73 6f 72 74 20 6f 72 64 65 72 73 20 2a 2f  t sort orders */
31023 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
31024 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f  xferCompatibleCo
31025 6c 6c 61 74 69 6f 6e 28 70 53 72 63 2d 3e 61 7a  llation(pSrc->az
31026 43 6f 6c 6c 5b 69 5d 2c 70 44 65 73 74 2d 3e 61  Coll[i],pDest->a
31027 7a 43 6f 6c 6c 5b 69 5d 29 20 29 7b 0a 20 20 20  zColl[i]) ){.   
31028 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
31029 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6c 6c  * Different coll
3102a 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20  ating sequences 
3102b 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  */.    }.  }..  
3102c 2f 2a 20 49 66 20 6e 6f 20 74 65 73 74 20 61 62  /* If no test ab
3102d 6f 76 65 20 66 61 69 6c 73 20 74 68 65 6e 20 74  ove fails then t
3102e 68 65 20 69 6e 64 69 63 65 73 20 6d 75 73 74 20  he indices must 
3102f 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 2a 2f  be compatible */
31030 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
31031 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 68  /*.** Attempt th
31032 65 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d  e transfer optim
31033 69 7a 61 74 69 6f 6e 20 6f 6e 20 49 4e 53 45 52  ization on INSER
31034 54 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a  Ts of the form.*
31035 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52 54 20  *.**     INSERT 
31036 49 4e 54 4f 20 74 61 62 31 20 53 45 4c 45 43 54  INTO tab1 SELECT
31037 20 2a 20 46 52 4f 4d 20 74 61 62 32 3b 0a 2a 2a   * FROM tab2;.**
31038 0a 2a 2a 20 54 68 65 20 78 66 65 72 20 6f 70 74  .** The xfer opt
31039 69 6d 69 7a 61 74 69 6f 6e 20 74 72 61 6e 73 66  imization transf
3103a 65 72 73 20 72 61 77 20 72 65 63 6f 72 64 73 20  ers raw records 
3103b 66 72 6f 6d 20 74 61 62 32 20 6f 76 65 72 20 74  from tab2 over t
3103c 6f 20 74 61 62 31 2e 20 20 0a 2a 2a 20 43 6f 6c  o tab1.  .** Col
3103d 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 64 65 63  umns are not dec
3103e 6f 64 65 64 20 61 6e 64 20 72 65 61 73 73 65 6d  oded and reassem
3103f 62 6c 69 65 64 2c 20 77 68 69 63 68 20 67 72 65  blied, which gre
31040 61 74 6c 79 20 69 6d 70 72 6f 76 65 73 0a 2a 2a  atly improves.**
31041 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 52   performance.  R
31042 61 77 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73  aw index records
31043 20 61 72 65 20 74 72 61 6e 73 66 65 72 72 65 64   are transferred
31044 20 69 6e 20 74 68 65 20 73 61 6d 65 20 77 61 79   in the same way
31045 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 66 65 72  ..**.** The xfer
31046 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
31047 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20   only attempted 
31048 69 66 20 74 61 62 31 20 61 6e 64 20 74 61 62 32  if tab1 and tab2
31049 20 61 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e   are compatible.
3104a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 6c 6f  .** There are lo
3104b 74 73 20 6f 66 20 72 75 6c 65 73 20 66 6f 72 20  ts of rules for 
3104c 64 65 74 65 72 6d 69 6e 69 6e 67 20 63 6f 6d 70  determining comp
3104d 61 74 69 62 69 6c 69 74 79 20 2d 20 73 65 65 20  atibility - see 
3104e 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 65 6d 62 65  comments.** embe
3104f 64 64 65 64 20 69 6e 20 74 68 65 20 63 6f 64 65  dded in the code
31050 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2a   for details..**
31051 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
31052 20 72 65 74 75 72 6e 73 20 54 52 55 45 20 69 66   returns TRUE if
31053 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f   the optimizatio
31054 6e 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  n is guaranteed 
31055 74 6f 20 62 65 20 75 73 65 64 2e 0a 2a 2a 20 53  to be used..** S
31056 6f 6d 65 74 69 6d 65 73 20 74 68 65 20 78 66 65  ometimes the xfe
31057 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 77  r optimization w
31058 69 6c 6c 20 6f 6e 6c 79 20 77 6f 72 6b 20 69 66  ill only work if
31059 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
3105a 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 65 6d 70   table.** is emp
3105b 74 79 20 2d 20 61 20 66 61 63 74 6f 72 20 74 68  ty - a factor th
3105c 61 74 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 64  at can only be d
3105d 65 74 65 72 6d 69 6e 65 64 20 61 74 20 72 75 6e  etermined at run
3105e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 61 74 0a  -time.  In that.
3105f 2a 2a 20 63 61 73 65 2c 20 74 68 69 73 20 72 6f  ** case, this ro
31060 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20  utine generates 
31061 63 6f 64 65 20 66 6f 72 20 74 68 65 20 78 66 65  code for the xfe
31062 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 62  r optimization b
31063 75 74 20 61 6c 73 6f 0a 2a 2a 20 64 6f 65 73 20  ut also.** does 
31064 61 20 74 65 73 74 20 74 6f 20 73 65 65 20 69 66  a test to see if
31065 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
31066 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 20   table is empty 
31067 61 6e 64 20 6a 75 6d 70 73 20 6f 76 65 72 20 74  and jumps over t
31068 68 65 0a 2a 2a 20 78 66 65 72 20 6f 70 74 69 6d  he.** xfer optim
31069 69 7a 61 74 69 6f 6e 20 63 6f 64 65 20 69 66 20  ization code if 
3106a 74 68 65 20 74 65 73 74 20 66 61 69 6c 73 2e 20  the test fails. 
3106b 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74   In that case, t
3106c 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72  his routine.** r
3106d 65 74 75 72 6e 73 20 46 41 4c 53 45 20 73 6f 20  eturns FALSE so 
3106e 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20  that the caller 
3106f 77 69 6c 6c 20 6b 6e 6f 77 20 74 6f 20 67 6f 20  will know to go 
31070 61 68 65 61 64 20 61 6e 64 20 67 65 6e 65 72 61  ahead and genera
31071 74 65 0a 2a 2a 20 61 6e 20 75 6e 6f 70 74 69 6d  te.** an unoptim
31072 69 7a 65 64 20 74 72 61 6e 73 66 65 72 2e 20 20  ized transfer.  
31073 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73  This routine als
31074 6f 20 72 65 74 75 72 6e 73 20 46 41 4c 53 45 20  o returns FALSE 
31075 69 66 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 6e  if there.** is n
31076 6f 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 68  o chance that th
31077 65 20 78 66 65 72 20 6f 70 74 69 6d 69 7a 61 74  e xfer optimizat
31078 69 6f 6e 20 63 61 6e 20 62 65 20 61 70 70 6c 69  ion can be appli
31079 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  ed..**.** This o
3107a 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 70  ptimization is p
3107b 61 72 74 69 63 75 6c 61 72 6c 79 20 75 73 65 66  articularly usef
3107c 75 6c 20 61 74 20 6d 61 6b 69 6e 67 20 56 41 43  ul at making VAC
3107d 55 55 4d 20 72 75 6e 20 66 61 73 74 65 72 2e 0a  UUM run faster..
3107e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78 66  */.static int xf
3107f 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28 0a  erOptimization(.
31080 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
31081 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
31082 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  r context */.  T
31083 61 62 6c 65 20 2a 70 44 65 73 74 2c 20 20 20 20  able *pDest,    
31084 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
31085 65 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  e we are inserti
31086 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 53 65 6c  ng into */.  Sel
31087 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20  ect *pSelect,   
31088 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73     /* A SELECT s
31089 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73 65 20  tatement to use 
3108a 61 73 20 74 68 65 20 64 61 74 61 20 73 6f 75 72  as the data sour
3108b 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72  ce */.  int onEr
3108c 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ror,          /*
3108d 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 63   How to handle c
3108e 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f 72 73  onstraint errors
3108f 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 44 65 73   */.  int iDbDes
31090 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  t           /* T
31091 68 65 20 64 61 74 61 62 61 73 65 20 6f 66 20 70  he database of p
31092 44 65 73 74 20 2a 2f 0a 29 7b 0a 20 20 45 78 70  Dest */.){.  Exp
31093 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20  rList *pEList;  
31094 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
31095 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   The result set 
31096 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 2a 2f  of the SELECT */
31097 0a 20 20 54 61 62 6c 65 20 2a 70 53 72 63 3b 20  .  Table *pSrc; 
31098 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31099 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
3109a 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
3109b 75 73 65 20 6f 66 20 53 45 4c 45 43 54 20 2a 2f  use of SELECT */
3109c 0a 20 20 49 6e 64 65 78 20 2a 70 53 72 63 49 64  .  Index *pSrcId
3109d 78 2c 20 2a 70 44 65 73 74 49 64 78 3b 20 20 20  x, *pDestIdx;   
3109e 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 61 6e      /* Source an
3109f 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 6e  d destination in
310a0 64 69 63 65 73 20 2a 2f 0a 20 20 73 74 72 75 63  dices */.  struc
310a1 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
310a2 70 49 74 65 6d 3b 20 20 20 20 20 20 2f 2a 20 41  pItem;      /* A
310a3 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70 53 65  n element of pSe
310a4 6c 65 63 74 2d 3e 70 53 72 63 20 2a 2f 0a 20 20  lect->pSrc */.  
310a5 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
310a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
310a7 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
310a8 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 53 72 63   */.  int iDbSrc
310a9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
310aa 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
310ab 74 61 62 61 73 65 20 6f 66 20 70 53 72 63 20 2a  tabase of pSrc *
310ac 2f 0a 20 20 69 6e 74 20 69 53 72 63 2c 20 69 44  /.  int iSrc, iD
310ad 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  est;            
310ae 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 73 20       /* Cursors 
310af 66 72 6f 6d 20 73 6f 75 72 63 65 20 61 6e 64 20  from source and 
310b0 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20  destination */. 
310b1 20 69 6e 74 20 61 64 64 72 31 2c 20 61 64 64 72   int addr1, addr
310b2 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2;              
310b3 20 20 2f 2a 20 4c 6f 6f 70 20 61 64 64 72 65 73    /* Loop addres
310b4 73 65 73 20 2a 2f 0a 20 20 69 6e 74 20 65 6d 70  ses */.  int emp
310b5 74 79 44 65 73 74 54 65 73 74 3b 20 20 20 20 20  tyDestTest;     
310b6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
310b7 72 65 73 73 20 6f 66 20 74 65 73 74 20 66 6f 72  ress of test for
310b8 20 65 6d 70 74 79 20 70 44 65 73 74 20 2a 2f 0a   empty pDest */.
310b9 20 20 69 6e 74 20 65 6d 70 74 79 53 72 63 54 65    int emptySrcTe
310ba 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
310bb 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
310bc 20 74 65 73 74 20 66 6f 72 20 65 6d 70 74 79 20   test for empty 
310bd 70 53 72 63 20 2a 2f 0a 20 20 56 64 62 65 20 2a  pSrc */.  Vdbe *
310be 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
310bf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
310c0 65 20 56 44 42 45 20 77 65 20 61 72 65 20 62 75  e VDBE we are bu
310c1 69 6c 64 69 6e 67 20 2a 2f 0a 20 20 4b 65 79 49  ilding */.  KeyI
310c2 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20 20 20 20  nfo *pKey;      
310c3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
310c4 4b 65 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  Key information 
310c5 66 6f 72 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a  for an index */.
310c6 20 20 69 6e 74 20 72 65 67 41 75 74 6f 69 6e 63    int regAutoinc
310c7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
310c8 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 67     /* Memory reg
310c9 69 73 74 65 72 20 75 73 65 64 20 62 79 20 41 55  ister used by AU
310ca 54 4f 49 4e 43 20 2a 2f 0a 20 20 69 6e 74 20 64  TOINC */.  int d
310cb 65 73 74 48 61 73 55 6e 69 71 75 65 49 64 78 20  estHasUniqueIdx 
310cc 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  = 0;        /* T
310cd 72 75 65 20 69 66 20 70 44 65 73 74 20 68 61 73  rue if pDest has
310ce 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 20   a UNIQUE index 
310cf 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61  */.  int regData
310d0 2c 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20  , regRowid;     
310d1 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
310d2 72 73 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20  rs holding data 
310d3 61 6e 64 20 72 6f 77 69 64 20 2a 2f 0a 0a 20 20  and rowid */..  
310d4 69 66 28 20 70 53 65 6c 65 63 74 3d 3d 30 20 29  if( pSelect==0 )
310d5 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
310d6 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 6f 66 20    /* Must be of 
310d7 74 68 65 20 66 6f 72 6d 20 20 49 4e 53 45 52 54  the form  INSERT
310d8 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54   INTO ... SELECT
310d9 20 2e 2e 2e 20 2a 2f 0a 20 20 7d 0a 20 20 69 66   ... */.  }.  if
310da 28 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  ( sqlite3Trigger
310db 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 44 65  List(pParse, pDe
310dc 73 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  st) ){.    retur
310dd 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 6d  n 0;   /* tab1 m
310de 75 73 74 20 6e 6f 74 20 68 61 76 65 20 74 72 69  ust not have tri
310df 67 67 65 72 73 20 2a 2f 0a 20 20 7d 0a 23 69 66  ggers */.  }.#if
310e0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
310e1 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
310e2 69 66 28 20 70 44 65 73 74 2d 3e 74 61 62 46 6c  if( pDest->tabFl
310e3 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c  ags & TF_Virtual
310e4 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
310e5 3b 20 20 20 2f 2a 20 74 61 62 31 20 6d 75 73 74  ;   /* tab1 must
310e6 20 6e 6f 74 20 62 65 20 61 20 76 69 72 74 75 61   not be a virtua
310e7 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a 23  l table */.  }.#
310e8 65 6e 64 69 66 0a 20 20 69 66 28 20 6f 6e 45 72  endif.  if( onEr
310e9 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror==OE_Default 
310ea 29 7b 0a 20 20 20 20 69 66 28 20 70 44 65 73 74  ){.    if( pDest
310eb 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 20 6f 6e 45  ->iPKey>=0 ) onE
310ec 72 72 6f 72 20 3d 20 70 44 65 73 74 2d 3e 6b 65  rror = pDest->ke
310ed 79 43 6f 6e 66 3b 0a 20 20 20 20 69 66 28 20 6f  yConf;.    if( o
310ee 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75  nError==OE_Defau
310ef 6c 74 20 29 20 6f 6e 45 72 72 6f 72 20 3d 20 4f  lt ) onError = O
310f0 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 20 20 61  E_Abort;.  }.  a
310f1 73 73 65 72 74 28 70 53 65 6c 65 63 74 2d 3e 70  ssert(pSelect->p
310f2 53 72 63 29 3b 20 20 20 2f 2a 20 61 6c 6c 6f 63  Src);   /* alloc
310f3 61 74 65 64 20 65 76 65 6e 20 69 66 20 74 68 65  ated even if the
310f4 72 65 20 69 73 20 6e 6f 20 46 52 4f 4d 20 63 6c  re is no FROM cl
310f5 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 53  ause */.  if( pS
310f6 65 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 6e 53 72  elect->pSrc->nSr
310f7 63 21 3d 31 20 29 7b 0a 20 20 20 20 72 65 74 75  c!=1 ){.    retu
310f8 72 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20  rn 0;   /* FROM 
310f9 63 6c 61 75 73 65 20 6d 75 73 74 20 68 61 76 65  clause must have
310fa 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 74 65 72   exactly one ter
310fb 6d 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70  m */.  }.  if( p
310fc 53 65 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 61 5b  Select->pSrc->a[
310fd 30 5d 2e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  0].pSelect ){.  
310fe 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
310ff 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 63 61 6e   FROM clause can
31100 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 73 75  not contain a su
31101 62 71 75 65 72 79 20 2a 2f 0a 20 20 7d 0a 20 20  bquery */.  }.  
31102 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 57 68  if( pSelect->pWh
31103 65 72 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ere ){.    retur
31104 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54  n 0;   /* SELECT
31105 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20   may not have a 
31106 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
31107 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63    }.  if( pSelec
31108 74 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  t->pOrderBy ){. 
31109 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
3110a 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74  * SELECT may not
3110b 20 68 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42   have an ORDER B
3110c 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a  Y clause */.  }.
3110d 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 6e 65 65 64    /* Do not need
3110e 20 74 6f 20 74 65 73 74 20 66 6f 72 20 61 20 48   to test for a H
3110f 41 56 49 4e 47 20 63 6c 61 75 73 65 2e 20 20 49  AVING clause.  I
31110 66 20 48 41 56 49 4e 47 20 69 73 20 70 72 65 73  f HAVING is pres
31111 65 6e 74 20 62 75 74 0a 20 20 2a 2a 20 74 68 65  ent but.  ** the
31112 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42  re is no ORDER B
31113 59 2c 20 77 65 20 77 69 6c 6c 20 67 65 74 20 61  Y, we will get a
31114 6e 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 66  n error. */.  if
31115 28 20 70 53 65 6c 65 63 74 2d 3e 70 47 72 6f 75  ( pSelect->pGrou
31116 70 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72  pBy ){.    retur
31117 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54  n 0;   /* SELECT
31118 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20   may not have a 
31119 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
3111a 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65  */.  }.  if( pSe
3111b 6c 65 63 74 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a  lect->pLimit ){.
3111c 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
3111d 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f  /* SELECT may no
3111e 74 20 68 61 76 65 20 61 20 4c 49 4d 49 54 20 63  t have a LIMIT c
3111f 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 61  lause */.  }.  a
31120 73 73 65 72 74 28 20 70 53 65 6c 65 63 74 2d 3e  ssert( pSelect->
31121 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20 2f  pOffset==0 );  /
31122 2a 20 4d 75 73 74 20 62 65 20 73 6f 20 69 66 20  * Must be so if 
31123 70 4c 69 6d 69 74 3d 3d 30 20 2a 2f 0a 20 20 69  pLimit==0 */.  i
31124 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69  f( pSelect->pPri
31125 6f 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  or ){.    return
31126 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20   0;   /* SELECT 
31127 6d 61 79 20 6e 6f 74 20 62 65 20 61 20 63 6f 6d  may not be a com
31128 70 6f 75 6e 64 20 71 75 65 72 79 20 2a 2f 0a 20  pound query */. 
31129 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74   }.  if( pSelect
3112a 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f  ->selFlags & SF_
3112b 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20  Distinct ){.    
3112c 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53  return 0;   /* S
3112d 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 62 65  ELECT may not be
3112e 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 7d   DISTINCT */.  }
3112f 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c  .  pEList = pSel
31130 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 61  ect->pEList;.  a
31131 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30  ssert( pEList!=0
31132 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74   );.  if( pEList
31133 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20  ->nExpr!=1 ){.  
31134 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
31135 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   The result set 
31136 6d 75 73 74 20 68 61 76 65 20 65 78 61 63 74 6c  must have exactl
31137 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  y one column */.
31138 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 45    }.  assert( pE
31139 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
3113a 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74   );.  if( pEList
3113b 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70  ->a[0].pExpr->op
3113c 21 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20  !=TK_ALL ){.    
3113d 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54  return 0;   /* T
3113e 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6d 75  he result set mu
3113f 73 74 20 62 65 20 74 68 65 20 73 70 65 63 69 61  st be the specia
31140 6c 20 6f 70 65 72 61 74 6f 72 20 22 2a 22 20 2a  l operator "*" *
31141 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74  /.  }..  /* At t
31142 68 69 73 20 70 6f 69 6e 74 20 77 65 20 68 61 76  his point we hav
31143 65 20 65 73 74 61 62 6c 69 73 68 65 64 20 74 68  e established th
31144 61 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  at the statement
31145 20 69 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20   is of the.  ** 
31146 63 6f 72 72 65 63 74 20 73 79 6e 74 61 63 74 69  correct syntacti
31147 63 20 66 6f 72 6d 20 74 6f 20 70 61 72 74 69 63  c form to partic
31148 69 70 61 74 65 20 69 6e 20 74 68 69 73 20 6f 70  ipate in this op
31149 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 4e 6f 77  timization.  Now
3114a 0a 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f  .  ** we have to
3114b 20 63 68 65 63 6b 20 74 68 65 20 73 65 6d 61 6e   check the seman
3114c 74 69 63 73 2e 0a 20 20 2a 2f 0a 20 20 70 49 74  tics..  */.  pIt
3114d 65 6d 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53  em = pSelect->pS
3114e 72 63 2d 3e 61 3b 0a 20 20 70 53 72 63 20 3d 20  rc->a;.  pSrc = 
3114f 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62  sqlite3LocateTab
31150 6c 65 49 74 65 6d 28 70 50 61 72 73 65 2c 20 30  leItem(pParse, 0
31151 2c 20 70 49 74 65 6d 29 3b 0a 20 20 69 66 28 20  , pItem);.  if( 
31152 70 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 72  pSrc==0 ){.    r
31153 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 46 52  eturn 0;   /* FR
31154 4f 4d 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e  OM clause does n
31155 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 61  ot contain a rea
31156 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a 20  l table */.  }. 
31157 20 69 66 28 20 70 53 72 63 3d 3d 70 44 65 73 74   if( pSrc==pDest
31158 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
31159 3b 20 20 20 2f 2a 20 74 61 62 31 20 61 6e 64 20  ;   /* tab1 and 
3115a 74 61 62 32 20 6d 61 79 20 6e 6f 74 20 62 65 20  tab2 may not be 
3115b 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20 2a  the same table *
3115c 2f 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  /.  }.#ifndef SQ
3115d 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
3115e 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70 53 72  LTABLE.  if( pSr
3115f 63 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  c->tabFlags & TF
31160 5f 56 69 72 74 75 61 6c 20 29 7b 0a 20 20 20 20  _Virtual ){.    
31161 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74  return 0;   /* t
31162 61 62 32 20 6d 75 73 74 20 6e 6f 74 20 62 65 20  ab2 must not be 
31163 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
31164 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  */.  }.#endif.  
31165 69 66 28 20 70 53 72 63 2d 3e 70 53 65 6c 65 63  if( pSrc->pSelec
31166 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
31167 30 3b 20 20 20 2f 2a 20 74 61 62 32 20 6d 61 79  0;   /* tab2 may
31168 20 6e 6f 74 20 62 65 20 61 20 76 69 65 77 20 2a   not be a view *
31169 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73  /.  }.  if( pDes
3116a 74 2d 3e 6e 43 6f 6c 21 3d 70 53 72 63 2d 3e 6e  t->nCol!=pSrc->n
3116b 43 6f 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  Col ){.    retur
3116c 6e 20 30 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72  n 0;   /* Number
3116d 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6d 75 73 74   of columns must
3116e 20 62 65 20 74 68 65 20 73 61 6d 65 20 69 6e 20   be the same in 
3116f 74 61 62 31 20 61 6e 64 20 74 61 62 32 20 2a 2f  tab1 and tab2 */
31170 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73 74  .  }.  if( pDest
31171 2d 3e 69 50 4b 65 79 21 3d 70 53 72 63 2d 3e 69  ->iPKey!=pSrc->i
31172 50 4b 65 79 20 29 7b 0a 20 20 20 20 72 65 74 75  PKey ){.    retu
31173 72 6e 20 30 3b 20 20 20 2f 2a 20 42 6f 74 68 20  rn 0;   /* Both 
31174 74 61 62 6c 65 73 20 6d 75 73 74 20 68 61 76 65  tables must have
31175 20 74 68 65 20 73 61 6d 65 20 49 4e 54 45 47 45   the same INTEGE
31176 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f  R PRIMARY KEY */
31177 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
31178 69 3c 70 44 65 73 74 2d 3e 6e 43 6f 6c 3b 20 69  i<pDest->nCol; i
31179 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 44 65  ++){.    if( pDe
3117a 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66 69  st->aCol[i].affi
3117b 6e 69 74 79 21 3d 70 53 72 63 2d 3e 61 43 6f 6c  nity!=pSrc->aCol
3117c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 20 29 7b 0a  [i].affinity ){.
3117d 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20        return 0; 
3117e 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 6d     /* Affinity m
3117f 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 20  ust be the same 
31180 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 2a  on all columns *
31181 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  /.    }.    if( 
31182 21 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 43  !xferCompatibleC
31183 6f 6c 6c 61 74 69 6f 6e 28 70 44 65 73 74 2d 3e  ollation(pDest->
31184 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 2c 20 70  aCol[i].zColl, p
31185 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f  Src->aCol[i].zCo
31186 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ll) ){.      ret
31187 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 43 6f 6c  urn 0;    /* Col
31188 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
31189 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65  must be the same
3118a 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20   on all columns 
3118b 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  */.    }.    if(
3118c 20 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e   pDest->aCol[i].
3118d 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70 53 72 63  notNull && !pSrc
3118e 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c  ->aCol[i].notNul
3118f 6c 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  l ){.      retur
31190 6e 20 30 3b 20 20 20 20 2f 2a 20 74 61 62 32 20  n 0;    /* tab2 
31191 6d 75 73 74 20 62 65 20 4e 4f 54 20 4e 55 4c 4c  must be NOT NULL
31192 20 69 66 20 74 61 62 31 20 69 73 20 2a 2f 0a 20   if tab1 is */. 
31193 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 70     }.  }.  for(p
31194 44 65 73 74 49 64 78 3d 70 44 65 73 74 2d 3e 70  DestIdx=pDest->p
31195 49 6e 64 65 78 3b 20 70 44 65 73 74 49 64 78 3b  Index; pDestIdx;
31196 20 70 44 65 73 74 49 64 78 3d 70 44 65 73 74 49   pDestIdx=pDestI
31197 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
31198 69 66 28 20 70 44 65 73 74 49 64 78 2d 3e 6f 6e  if( pDestIdx->on
31199 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29  Error!=OE_None )
3119a 7b 0a 20 20 20 20 20 20 64 65 73 74 48 61 73 55  {.      destHasU
3119b 6e 69 71 75 65 49 64 78 20 3d 20 31 3b 0a 20 20  niqueIdx = 1;.  
3119c 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 53 72 63    }.    for(pSrc
3119d 49 64 78 3d 70 53 72 63 2d 3e 70 49 6e 64 65 78  Idx=pSrc->pIndex
3119e 3b 20 70 53 72 63 49 64 78 3b 20 70 53 72 63 49  ; pSrcIdx; pSrcI
3119f 64 78 3d 70 53 72 63 49 64 78 2d 3e 70 4e 65 78  dx=pSrcIdx->pNex
311a0 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 78 66  t){.      if( xf
311a1 65 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65  erCompatibleInde
311a2 78 28 70 44 65 73 74 49 64 78 2c 20 70 53 72 63  x(pDestIdx, pSrc
311a3 49 64 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20  Idx) ) break;.  
311a4 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53 72 63    }.    if( pSrc
311a5 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Idx==0 ){.      
311a6 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20  return 0;    /* 
311a7 70 44 65 73 74 49 64 78 20 68 61 73 20 6e 6f 20  pDestIdx has no 
311a8 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e  corresponding in
311a9 64 65 78 20 69 6e 20 70 53 72 63 20 2a 2f 0a 20  dex in pSrc */. 
311aa 20 20 20 7d 0a 20 20 7d 0a 23 69 66 6e 64 65 66     }.  }.#ifndef
311ab 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
311ac 43 4b 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e  CK.  if( pDest->
311ad 70 43 68 65 63 6b 20 26 26 20 73 71 6c 69 74 65  pCheck && sqlite
311ae 33 45 78 70 72 4c 69 73 74 43 6f 6d 70 61 72 65  3ExprListCompare
311af 28 70 53 72 63 2d 3e 70 43 68 65 63 6b 2c 20 70  (pSrc->pCheck, p
311b0 44 65 73 74 2d 3e 70 43 68 65 63 6b 29 20 29 7b  Dest->pCheck) ){
311b1 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
311b2 20 2f 2a 20 54 61 62 6c 65 73 20 68 61 76 65 20   /* Tables have 
311b3 64 69 66 66 65 72 65 6e 74 20 43 48 45 43 4b 20  different CHECK 
311b4 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 54 69  constraints.  Ti
311b5 63 6b 65 74 20 23 32 32 35 32 20 2a 2f 0a 20 20  cket #2252 */.  
311b6 7d 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  }.#endif.#ifndef
311b7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52   SQLITE_OMIT_FOR
311b8 45 49 47 4e 5f 4b 45 59 0a 20 20 2f 2a 20 44 69  EIGN_KEY.  /* Di
311b9 73 61 6c 6c 6f 77 20 74 68 65 20 74 72 61 6e 73  sallow the trans
311ba 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  fer optimization
311bb 20 69 66 20 74 68 65 20 64 65 73 74 69 6e 61 74   if the destinat
311bc 69 6f 6e 20 74 61 62 6c 65 20 63 6f 6e 73 74 61  ion table consta
311bd 69 6e 73 0a 20 20 2a 2a 20 61 6e 79 20 66 6f 72  ins.  ** any for
311be 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
311bf 69 6e 74 73 2e 20 20 54 68 69 73 20 69 73 20 6d  ints.  This is m
311c0 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20  ore restrictive 
311c1 74 68 61 6e 20 6e 65 63 65 73 73 61 72 79 2e 0a  than necessary..
311c2 20 20 2a 2a 20 42 75 74 20 74 68 65 20 6d 61 69    ** But the mai
311c3 6e 20 62 65 6e 65 66 69 63 69 61 72 79 20 6f 66  n beneficiary of
311c4 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70   the transfer op
311c5 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 74 68  timization is th
311c6 65 20 56 41 43 55 55 4d 20 0a 20 20 2a 2a 20 63  e VACUUM .  ** c
311c7 6f 6d 6d 61 6e 64 2c 20 61 6e 64 20 74 68 65 20  ommand, and the 
311c8 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20 64  VACUUM command d
311c9 69 73 61 62 6c 65 73 20 66 6f 72 65 69 67 6e 20  isables foreign 
311ca 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  key constraints.
311cb 20 20 53 6f 0a 20 20 2a 2a 20 74 68 65 20 65 78    So.  ** the ex
311cc 74 72 61 20 63 6f 6d 70 6c 69 63 61 74 69 6f 6e  tra complication
311cd 20 74 6f 20 6d 61 6b 65 20 74 68 69 73 20 72 75   to make this ru
311ce 6c 65 20 6c 65 73 73 20 72 65 73 74 72 69 63 74  le less restrict
311cf 69 76 65 20 69 73 20 70 72 6f 62 61 62 6c 79 0a  ive is probably.
311d0 20 20 2a 2a 20 6e 6f 74 20 77 6f 72 74 68 20 74    ** not worth t
311d1 68 65 20 65 66 66 6f 72 74 2e 20 20 54 69 63 6b  he effort.  Tick
311d2 65 74 20 5b 36 32 38 34 64 66 38 39 64 65 62 64  et [6284df89debd
311d3 66 61 36 31 64 62 38 30 37 33 65 30 36 32 39 30  fa61db8073e06290
311d4 38 61 66 30 63 39 62 36 31 31 38 65 5d 0a 20 20  8af0c9b6118e].  
311d5 2a 2f 0a 20 20 69 66 28 20 28 70 50 61 72 73 65  */.  if( (pParse
311d6 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ->db->flags & SQ
311d7 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73  LITE_ForeignKeys
311d8 29 21 3d 30 20 26 26 20 70 44 65 73 74 2d 3e 70  )!=0 && pDest->p
311d9 46 4b 65 79 21 3d 30 20 29 7b 0a 20 20 20 20 72  FKey!=0 ){.    r
311da 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65 6e  eturn 0;.  }.#en
311db 64 69 66 0a 20 20 69 66 28 20 28 70 50 61 72 73  dif.  if( (pPars
311dc 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  e->db->flags & S
311dd 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29  QLITE_CountRows)
311de 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=0 ){.    retur
311df 6e 20 30 3b 20 20 2f 2a 20 78 66 65 72 20 6f 70  n 0;  /* xfer op
311e0 74 20 64 6f 65 73 20 6e 6f 74 20 70 6c 61 79 20  t does not play 
311e1 77 65 6c 6c 20 77 69 74 68 20 50 52 41 47 4d 41  well with PRAGMA
311e2 20 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20 2a   count_changes *
311e3 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77  /.  }..  /* If w
311e4 65 20 67 65 74 20 74 68 69 73 20 66 61 72 2c 20  e get this far, 
311e5 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
311e6 65 20 78 66 65 72 20 6f 70 74 69 6d 69 7a 61 74  e xfer optimizat
311e7 69 6f 6e 20 69 73 20 61 74 0a 20 20 2a 2a 20 6c  ion is at.  ** l
311e8 65 61 73 74 20 61 20 70 6f 73 73 69 62 69 6c 69  east a possibili
311e9 74 79 2c 20 74 68 6f 75 67 68 20 69 74 20 6d 69  ty, though it mi
311ea 67 68 74 20 6f 6e 6c 79 20 77 6f 72 6b 20 69 66  ght only work if
311eb 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
311ec 0a 20 20 2a 2a 20 74 61 62 6c 65 20 28 74 61 62  .  ** table (tab
311ed 31 29 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20  1) is initially 
311ee 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 23 69 66 64  empty..  */.#ifd
311ef 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
311f0 20 73 71 6c 69 74 65 33 5f 78 66 65 72 6f 70 74   sqlite3_xferopt
311f1 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66  _count++;.#endif
311f2 0a 20 20 69 44 62 53 72 63 20 3d 20 73 71 6c 69  .  iDbSrc = sqli
311f3 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
311f4 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 72  (pParse->db, pSr
311f5 63 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 76  c->pSchema);.  v
311f6 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
311f7 65 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c  e(pParse);.  sql
311f8 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63  ite3CodeVerifySc
311f9 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62  hema(pParse, iDb
311fa 53 72 63 29 3b 0a 20 20 69 53 72 63 20 3d 20 70  Src);.  iSrc = p
311fb 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
311fc 20 69 44 65 73 74 20 3d 20 70 50 61 72 73 65 2d   iDest = pParse-
311fd 3e 6e 54 61 62 2b 2b 3b 0a 20 20 72 65 67 41 75  >nTab++;.  regAu
311fe 74 6f 69 6e 63 20 3d 20 61 75 74 6f 49 6e 63 42  toinc = autoIncB
311ff 65 67 69 6e 28 70 50 61 72 73 65 2c 20 69 44 62  egin(pParse, iDb
31200 44 65 73 74 2c 20 70 44 65 73 74 29 3b 0a 20 20  Dest, pDest);.  
31201 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
31202 28 70 50 61 72 73 65 2c 20 69 44 65 73 74 2c 20  (pParse, iDest, 
31203 69 44 62 44 65 73 74 2c 20 70 44 65 73 74 2c 20  iDbDest, pDest, 
31204 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20  OP_OpenWrite);. 
31205 20 69 66 28 20 28 70 44 65 73 74 2d 3e 69 50 4b   if( (pDest->iPK
31206 65 79 3c 30 20 26 26 20 70 44 65 73 74 2d 3e 70  ey<0 && pDest->p
31207 49 6e 64 65 78 21 3d 30 29 20 20 20 20 20 20 20  Index!=0)       
31208 20 20 20 2f 2a 20 28 31 29 20 2a 2f 0a 20 20 20     /* (1) */.   
31209 7c 7c 20 64 65 73 74 48 61 73 55 6e 69 71 75 65  || destHasUnique
3120a 49 64 78 20 20 20 20 20 20 20 20 20 20 20 20 20  Idx             
3120b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3120c 20 2f 2a 20 28 32 29 20 2a 2f 0a 20 20 20 7c 7c   /* (2) */.   ||
3120d 20 28 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 41 62   (onError!=OE_Ab
3120e 6f 72 74 20 26 26 20 6f 6e 45 72 72 6f 72 21 3d  ort && onError!=
3120f 4f 45 5f 52 6f 6c 6c 62 61 63 6b 29 20 20 20 2f  OE_Rollback)   /
31210 2a 20 28 33 29 20 2a 2f 0a 20 20 29 7b 0a 20 20  * (3) */.  ){.  
31211 20 20 2f 2a 20 49 6e 20 73 6f 6d 65 20 63 69 72    /* In some cir
31212 63 75 6d 73 74 61 6e 63 65 73 2c 20 77 65 20 61  cumstances, we a
31213 72 65 20 61 62 6c 65 20 74 6f 20 72 75 6e 20 74  re able to run t
31214 68 65 20 78 66 65 72 20 6f 70 74 69 6d 69 7a 61  he xfer optimiza
31215 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79  tion.    ** only
31216 20 69 66 20 74 68 65 20 64 65 73 74 69 6e 61 74   if the destinat
31217 69 6f 6e 20 74 61 62 6c 65 20 69 73 20 69 6e 69  ion table is ini
31218 74 69 61 6c 6c 79 20 65 6d 70 74 79 2e 20 20 54  tially empty.  T
31219 68 69 73 20 63 6f 64 65 20 6d 61 6b 65 73 0a 20  his code makes. 
3121a 20 20 20 2a 2a 20 74 68 61 74 20 64 65 74 65 72     ** that deter
3121b 6d 69 6e 61 74 69 6f 6e 2e 20 20 43 6f 6e 64 69  mination.  Condi
3121c 74 69 6f 6e 73 20 75 6e 64 65 72 20 77 68 69 63  tions under whic
3121d 68 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  h the destinatio
3121e 6e 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 65  n must.    ** be
3121f 20 65 6d 70 74 79 3a 0a 20 20 20 20 2a 2a 0a 20   empty:.    **. 
31220 20 20 20 2a 2a 20 28 31 29 20 54 68 65 72 65 20     ** (1) There 
31221 69 73 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52  is no INTEGER PR
31222 49 4d 41 52 59 20 4b 45 59 20 62 75 74 20 74 68  IMARY KEY but th
31223 65 72 65 20 61 72 65 20 69 6e 64 69 63 65 73 2e  ere are indices.
31224 0a 20 20 20 20 2a 2a 20 20 20 20 20 28 49 66 20  .    **     (If 
31225 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
31226 69 73 20 6e 6f 74 20 69 6e 69 74 69 61 6c 6c 79  is not initially
31227 20 65 6d 70 74 79 2c 20 74 68 65 20 72 6f 77 69   empty, the rowi
31228 64 20 66 69 65 6c 64 73 0a 20 20 20 20 2a 2a 20  d fields.    ** 
31229 20 20 20 20 6f 66 20 69 6e 64 65 78 20 65 6e 74      of index ent
3122a 72 69 65 73 20 6d 69 67 68 74 20 6e 65 65 64 20  ries might need 
3122b 74 6f 20 63 68 61 6e 67 65 2e 29 0a 20 20 20 20  to change.).    
3122c 2a 2a 0a 20 20 20 20 2a 2a 20 28 32 29 20 54 68  **.    ** (2) Th
3122d 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 68 61  e destination ha
3122e 73 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 78  s a unique index
3122f 2e 20 20 28 54 68 65 20 78 66 65 72 20 6f 70 74  .  (The xfer opt
31230 69 6d 69 7a 61 74 69 6f 6e 20 0a 20 20 20 20 2a  imization .    *
31231 2a 20 20 20 20 20 69 73 20 75 6e 61 62 6c 65 20  *     is unable 
31232 74 6f 20 74 65 73 74 20 75 6e 69 71 75 65 6e 65  to test uniquene
31233 73 73 2e 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ss.).    **.    
31234 2a 2a 20 28 33 29 20 6f 6e 45 72 72 6f 72 20 69  ** (3) onError i
31235 73 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65  s something othe
31236 72 20 74 68 61 6e 20 4f 45 5f 41 62 6f 72 74 20  r than OE_Abort 
31237 61 6e 64 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e  and OE_Rollback.
31238 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64 64 72  .    */.    addr
31239 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
3123a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69  ddOp2(v, OP_Rewi
3123b 6e 64 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20  nd, iDest, 0);. 
3123c 20 20 20 65 6d 70 74 79 44 65 73 74 54 65 73 74     emptyDestTest
3123d 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
3123e 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
3123f 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
31240 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
31241 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 7d 65 6c  v, addr1);.  }el
31242 73 65 7b 0a 20 20 20 20 65 6d 70 74 79 44 65 73  se{.    emptyDes
31243 74 54 65 73 74 20 3d 20 30 3b 0a 20 20 7d 0a 20  tTest = 0;.  }. 
31244 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c   sqlite3OpenTabl
31245 65 28 70 50 61 72 73 65 2c 20 69 53 72 63 2c 20  e(pParse, iSrc, 
31246 69 44 62 53 72 63 2c 20 70 53 72 63 2c 20 4f 50  iDbSrc, pSrc, OP
31247 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 65 6d  _OpenRead);.  em
31248 70 74 79 53 72 63 54 65 73 74 20 3d 20 73 71 6c  ptySrcTest = sql
31249 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
3124a 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 53 72  , OP_Rewind, iSr
3124b 63 2c 20 30 29 3b 0a 20 20 72 65 67 44 61 74 61  c, 0);.  regData
3124c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
3124d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
3124e 72 65 67 52 6f 77 69 64 20 3d 20 73 71 6c 69 74  regRowid = sqlit
3124f 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
31250 72 73 65 29 3b 0a 20 20 69 66 28 20 70 44 65 73  rse);.  if( pDes
31251 74 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20  t->iPKey>=0 ){. 
31252 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74     addr1 = sqlit
31253 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
31254 4f 50 5f 52 6f 77 69 64 2c 20 69 53 72 63 2c 20  OP_Rowid, iSrc, 
31255 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 61  regRowid);.    a
31256 64 64 72 32 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr2 = sqlite3Vd
31257 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
31258 6f 74 45 78 69 73 74 73 2c 20 69 44 65 73 74 2c  otExists, iDest,
31259 20 30 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20   0, regRowid);. 
3125a 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f     sqlite3HaltCo
3125b 6e 73 74 72 61 69 6e 74 28 0a 20 20 20 20 20 20  nstraint(.      
3125c 20 20 70 50 61 72 73 65 2c 20 6f 6e 45 72 72 6f    pParse, onErro
3125d 72 2c 20 22 50 52 49 4d 41 52 59 20 4b 45 59 20  r, "PRIMARY KEY 
3125e 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 22 2c  must be unique",
3125f 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
31260 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
31261 48 65 72 65 28 76 2c 20 61 64 64 72 32 29 3b 0a  Here(v, addr2);.
31262 20 20 20 20 61 75 74 6f 49 6e 63 53 74 65 70 28      autoIncStep(
31263 70 50 61 72 73 65 2c 20 72 65 67 41 75 74 6f 69  pParse, regAutoi
31264 6e 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  nc, regRowid);. 
31265 20 7d 65 6c 73 65 20 69 66 28 20 70 44 65 73 74   }else if( pDest
31266 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20  ->pIndex==0 ){. 
31267 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74     addr1 = sqlit
31268 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
31269 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 44 65  OP_NewRowid, iDe
3126a 73 74 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  st, regRowid);. 
3126b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64 72   }else{.    addr
3126c 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
3126d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
3126e 64 2c 20 69 53 72 63 2c 20 72 65 67 52 6f 77 69  d, iSrc, regRowi
3126f 64 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  d);.    assert( 
31270 28 70 44 65 73 74 2d 3e 74 61 62 46 6c 61 67 73  (pDest->tabFlags
31271 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d   & TF_Autoincrem
31272 65 6e 74 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20  ent)==0 );.  }. 
31273 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
31274 70 32 28 76 2c 20 4f 50 5f 52 6f 77 44 61 74 61  p2(v, OP_RowData
31275 2c 20 69 53 72 63 2c 20 72 65 67 44 61 74 61 29  , iSrc, regData)
31276 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
31277 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
31278 72 74 2c 20 69 44 65 73 74 2c 20 72 65 67 44 61  rt, iDest, regDa
31279 74 61 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  ta, regRowid);. 
3127a 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
3127b 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 4e  geP5(v, OPFLAG_N
3127c 43 48 41 4e 47 45 7c 4f 50 46 4c 41 47 5f 4c 41  CHANGE|OPFLAG_LA
3127d 53 54 52 4f 57 49 44 7c 4f 50 46 4c 41 47 5f 41  STROWID|OPFLAG_A
3127e 50 50 45 4e 44 29 3b 0a 20 20 73 71 6c 69 74 65  PPEND);.  sqlite
3127f 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
31280 20 2d 31 2c 20 70 44 65 73 74 2d 3e 7a 4e 61 6d   -1, pDest->zNam
31281 65 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  e, 0);.  sqlite3
31282 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
31283 5f 4e 65 78 74 2c 20 69 53 72 63 2c 20 61 64 64  _Next, iSrc, add
31284 72 31 29 3b 0a 20 20 66 6f 72 28 70 44 65 73 74  r1);.  for(pDest
31285 49 64 78 3d 70 44 65 73 74 2d 3e 70 49 6e 64 65  Idx=pDest->pInde
31286 78 3b 20 70 44 65 73 74 49 64 78 3b 20 70 44 65  x; pDestIdx; pDe
31287 73 74 49 64 78 3d 70 44 65 73 74 49 64 78 2d 3e  stIdx=pDestIdx->
31288 70 4e 65 78 74 29 7b 0a 20 20 20 20 66 6f 72 28  pNext){.    for(
31289 70 53 72 63 49 64 78 3d 70 53 72 63 2d 3e 70 49  pSrcIdx=pSrc->pI
3128a 6e 64 65 78 3b 20 41 4c 57 41 59 53 28 70 53 72  ndex; ALWAYS(pSr
3128b 63 49 64 78 29 3b 20 70 53 72 63 49 64 78 3d 70  cIdx); pSrcIdx=p
3128c 53 72 63 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  SrcIdx->pNext){.
3128d 20 20 20 20 20 20 69 66 28 20 78 66 65 72 43 6f        if( xferCo
3128e 6d 70 61 74 69 62 6c 65 49 6e 64 65 78 28 70 44  mpatibleIndex(pD
3128f 65 73 74 49 64 78 2c 20 70 53 72 63 49 64 78 29  estIdx, pSrcIdx)
31290 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a   ) break;.    }.
31291 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63      assert( pSrc
31292 49 64 78 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  Idx );.    sqlit
31293 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
31294 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72 63 2c 20  OP_Close, iSrc, 
31295 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
31296 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
31297 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30 29  Close, iDest, 0)
31298 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20 73 71 6c  ;.    pKey = sql
31299 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
3129a 28 70 50 61 72 73 65 2c 20 70 53 72 63 49 64 78  (pParse, pSrcIdx
3129b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
3129c 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
3129d 70 65 6e 52 65 61 64 2c 20 69 53 72 63 2c 20 70  penRead, iSrc, p
3129e 53 72 63 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44  SrcIdx->tnum, iD
3129f 62 53 72 63 2c 0a 20 20 20 20 20 20 20 20 20 20  bSrc,.          
312a0 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61              (cha
312a1 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49  r*)pKey, P4_KEYI
312a2 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
312a3 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
312a4 2c 20 22 25 73 22 2c 20 70 53 72 63 49 64 78 2d  , "%s", pSrcIdx-
312a5 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 70 4b  >zName));.    pK
312a6 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
312a7 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
312a8 20 70 44 65 73 74 49 64 78 29 3b 0a 20 20 20 20   pDestIdx);.    
312a9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
312aa 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  4(v, OP_OpenWrit
312ab 65 2c 20 69 44 65 73 74 2c 20 70 44 65 73 74 49  e, iDest, pDestI
312ac 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 44 65 73  dx->tnum, iDbDes
312ad 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
312ae 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
312af 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  pKey, P4_KEYINFO
312b0 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56  _HANDOFF);.    V
312b1 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
312b2 25 73 22 2c 20 70 44 65 73 74 49 64 78 2d 3e 7a  %s", pDestIdx->z
312b3 4e 61 6d 65 29 29 3b 0a 20 20 20 20 61 64 64 72  Name));.    addr
312b4 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
312b5 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69  ddOp2(v, OP_Rewi
312b6 6e 64 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20 20  nd, iSrc, 0);.  
312b7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
312b8 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 4b 65 79  Op2(v, OP_RowKey
312b9 2c 20 69 53 72 63 2c 20 72 65 67 44 61 74 61 29  , iSrc, regData)
312ba 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
312bb 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 64  eAddOp3(v, OP_Id
312bc 78 49 6e 73 65 72 74 2c 20 69 44 65 73 74 2c 20  xInsert, iDest, 
312bd 72 65 67 44 61 74 61 2c 20 31 29 3b 0a 20 20 20  regData, 1);.   
312be 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
312bf 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69  p2(v, OP_Next, i
312c0 53 72 63 2c 20 61 64 64 72 31 2b 31 29 3b 0a 20  Src, addr1+1);. 
312c1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
312c2 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29  mpHere(v, addr1)
312c3 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
312c4 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 65  dbeJumpHere(v, e
312c5 6d 70 74 79 53 72 63 54 65 73 74 29 3b 0a 20 20  mptySrcTest);.  
312c6 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
312c7 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
312c8 67 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74  gRowid);.  sqlit
312c9 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
312ca 28 70 50 61 72 73 65 2c 20 72 65 67 44 61 74 61  (pParse, regData
312cb 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
312cc 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
312cd 73 65 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20 20  se, iSrc, 0);.  
312ce 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
312cf 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69  2(v, OP_Close, i
312d0 44 65 73 74 2c 20 30 29 3b 0a 20 20 69 66 28 20  Dest, 0);.  if( 
312d1 65 6d 70 74 79 44 65 73 74 54 65 73 74 20 29 7b  emptyDestTest ){
312d2 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
312d3 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c  AddOp2(v, OP_Hal
312d4 74 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29  t, SQLITE_OK, 0)
312d5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
312d6 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 65 6d 70  eJumpHere(v, emp
312d7 74 79 44 65 73 74 54 65 73 74 29 3b 0a 20 20 20  tyDestTest);.   
312d8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
312d9 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p2(v, OP_Close, 
312da 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 72  iDest, 0);.    r
312db 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65  eturn 0;.  }else
312dc 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  {.    return 1;.
312dd 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20    }.}.#endif /* 
312de 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52  SQLITE_OMIT_XFER
312df 5f 4f 50 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  _OPT */../******
312e0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
312e1 69 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  insert.c *******
312e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
312e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
312e4 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
312e5 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
312e6 69 6c 65 20 6c 65 67 61 63 79 2e 63 20 2a 2a 2a  ile legacy.c ***
312e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
312e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
312e9 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
312ea 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
312eb 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
312ec 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
312ed 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
312ee 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
312ef 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
312f0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
312f1 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
312f2 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
312f3 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
312f4 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
312f5 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
312f6 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
312f7 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
312f8 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
312f9 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
312fa 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
312fb 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
312fc 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
312fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
312fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
312ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
31301 2a 2a 20 4d 61 69 6e 20 66 69 6c 65 20 66 6f 72  ** Main file for
31302 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
31303 61 72 79 2e 20 20 54 68 65 20 72 6f 75 74 69 6e  ary.  The routin
31304 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 0a  es in this file.
31305 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  ** implement the
31306 20 70 72 6f 67 72 61 6d 6d 65 72 20 69 6e 74 65   programmer inte
31307 72 66 61 63 65 20 74 6f 20 74 68 65 20 6c 69 62  rface to the lib
31308 72 61 72 79 2e 20 20 52 6f 75 74 69 6e 65 73 20  rary.  Routines 
31309 69 6e 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65  in.** other file
3130a 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e  s are for intern
3130b 61 6c 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  al use by SQLite
3130c 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20   and should not 
3130d 62 65 0a 2a 2a 20 61 63 63 65 73 73 65 64 20 62  be.** accessed b
3130e 79 20 75 73 65 72 73 20 6f 66 20 74 68 65 20 6c  y users of the l
3130f 69 62 72 61 72 79 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a  ibrary..*/.../*.
31310 2a 2a 20 45 78 65 63 75 74 65 20 53 51 4c 20 63  ** Execute SQL c
31311 6f 64 65 2e 20 20 52 65 74 75 72 6e 20 6f 6e 65  ode.  Return one
31312 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 20   of the SQLITE_ 
31313 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72 65 0a  success/failure.
31314 2a 2a 20 63 6f 64 65 73 2e 20 20 41 6c 73 6f 20  ** codes.  Also 
31315 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d  write an error m
31316 65 73 73 61 67 65 20 69 6e 74 6f 20 6d 65 6d 6f  essage into memo
31317 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
31318 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64  .** malloc() and
31319 20 6d 61 6b 65 20 2a 70 7a 45 72 72 4d 73 67 20   make *pzErrMsg 
3131a 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 6d 65  point to that me
3131b 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ssage..**.** If 
3131c 74 68 65 20 53 51 4c 20 69 73 20 61 20 71 75 65  the SQL is a que
3131d 72 79 2c 20 74 68 65 6e 20 66 6f 72 20 65 61 63  ry, then for eac
3131e 68 20 72 6f 77 20 69 6e 20 74 68 65 20 71 75 65  h row in the que
3131f 72 79 20 72 65 73 75 6c 74 0a 2a 2a 20 74 68 65  ry result.** the
31320 20 78 43 61 6c 6c 62 61 63 6b 28 29 20 66 75 6e   xCallback() fun
31321 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e  ction is called.
31322 20 20 70 41 72 67 20 62 65 63 6f 6d 65 73 20 74    pArg becomes t
31323 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75  he first.** argu
31324 6d 65 6e 74 20 74 6f 20 78 43 61 6c 6c 62 61 63  ment to xCallbac
31325 6b 28 29 2e 20 20 49 66 20 78 43 61 6c 6c 62 61  k().  If xCallba
31326 63 6b 3d 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f 20  ck=NULL then no 
31327 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 69  callback.** is i
31328 6e 76 6f 6b 65 64 2c 20 65 76 65 6e 20 66 6f 72  nvoked, even for
31329 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 53 51 4c   queries..*/.SQL
3132a 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3132b 74 65 33 5f 65 78 65 63 28 0a 20 20 73 71 6c 69  te3_exec(.  sqli
3132c 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
3132d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
3132e 61 74 61 62 61 73 65 20 6f 6e 20 77 68 69 63 68  atabase on which
3132f 20 74 68 65 20 53 51 4c 20 65 78 65 63 75 74 65   the SQL execute
31330 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
31331 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  r *zSql,        
31332 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 6f     /* The SQL to
31333 20 62 65 20 65 78 65 63 75 74 65 64 20 2a 2f 0a   be executed */.
31334 20 20 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62 61    sqlite3_callba
31335 63 6b 20 78 43 61 6c 6c 62 61 63 6b 2c 20 2f 2a  ck xCallback, /*
31336 20 49 6e 76 6f 6b 65 20 74 68 69 73 20 63 61 6c   Invoke this cal
31337 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 2a 2f  lback routine */
31338 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c 20 20  .  void *pArg,  
31339 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3133a 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74  * First argument
3133b 20 74 6f 20 78 43 61 6c 6c 62 61 63 6b 28 29 20   to xCallback() 
3133c 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72  */.  char **pzEr
3133d 72 4d 73 67 20 20 20 20 20 20 20 20 20 20 20 20  rMsg            
3133e 20 2f 2a 20 57 72 69 74 65 20 65 72 72 6f 72 20   /* Write error 
3133f 6d 65 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f  messages here */
31340 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
31341 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
31342 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
31343 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
31344 20 2a 7a 4c 65 66 74 6f 76 65 72 3b 20 20 20 20   *zLeftover;    
31345 20 20 2f 2a 20 54 61 69 6c 20 6f 66 20 75 6e 70    /* Tail of unp
31346 72 6f 63 65 73 73 65 64 20 53 51 4c 20 2a 2f 0a  rocessed SQL */.
31347 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
31348 70 53 74 6d 74 20 3d 20 30 3b 20 20 20 20 2f 2a  pStmt = 0;    /*
31349 20 54 68 65 20 63 75 72 72 65 6e 74 20 53 51 4c   The current SQL
3134a 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
3134b 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 73 20 3d 20  char **azCols = 
3134c 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  0;          /* N
3134d 61 6d 65 73 20 6f 66 20 72 65 73 75 6c 74 20 63  ames of result c
3134e 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  olumns */.  int 
3134f 6e 52 65 74 72 79 20 3d 20 30 3b 20 20 20 20 20  nRetry = 0;     
31350 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
31351 72 20 6f 66 20 72 65 74 72 79 20 61 74 74 65 6d  r of retry attem
31352 70 74 73 20 2a 2f 0a 20 20 69 6e 74 20 63 61 6c  pts */.  int cal
31353 6c 62 61 63 6b 49 73 49 6e 69 74 3b 20 20 20 20  lbackIsInit;    
31354 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
31355 63 61 6c 6c 62 61 63 6b 20 64 61 74 61 20 69 73  callback data is
31356 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
31357 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 53  .  if( !sqlite3S
31358 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29  afetyCheckOk(db)
31359 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
3135a 5f 4d 49 53 55 53 45 5f 42 4b 50 54 3b 0a 20 20  _MISUSE_BKPT;.  
3135b 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 20 7a 53  if( zSql==0 ) zS
3135c 71 6c 20 3d 20 22 22 3b 0a 0a 20 20 73 71 6c 69  ql = "";..  sqli
3135d 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
3135e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71  db->mutex);.  sq
3135f 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
31360 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20  QLITE_OK, 0);.  
31361 77 68 69 6c 65 28 20 28 72 63 3d 3d 53 51 4c 49  while( (rc==SQLI
31362 54 45 5f 4f 4b 20 7c 7c 20 28 72 63 3d 3d 53 51  TE_OK || (rc==SQ
31363 4c 49 54 45 5f 53 43 48 45 4d 41 20 26 26 20 28  LITE_SCHEMA && (
31364 2b 2b 6e 52 65 74 72 79 29 3c 32 29 29 20 26 26  ++nRetry)<2)) &&
31365 20 7a 53 71 6c 5b 30 5d 20 29 7b 0a 20 20 20 20   zSql[0] ){.    
31366 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 20 20 63 68  int nCol;.    ch
31367 61 72 20 2a 2a 61 7a 56 61 6c 73 20 3d 20 30 3b  ar **azVals = 0;
31368 0a 0a 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b  ..    pStmt = 0;
31369 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
3136a 33 5f 70 72 65 70 61 72 65 28 64 62 2c 20 7a 53  3_prepare(db, zS
3136b 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20  ql, -1, &pStmt, 
3136c 26 7a 4c 65 66 74 6f 76 65 72 29 3b 0a 20 20 20  &zLeftover);.   
3136d 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
3136e 49 54 45 5f 4f 4b 20 7c 7c 20 70 53 74 6d 74 3d  ITE_OK || pStmt=
3136f 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63  =0 );.    if( rc
31370 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
31371 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
31372 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 53     }.    if( !pS
31373 74 6d 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  tmt ){.      /* 
31374 74 68 69 73 20 68 61 70 70 65 6e 73 20 66 6f 72  this happens for
31375 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 72 20 77 68   a comment or wh
31376 69 74 65 2d 73 70 61 63 65 20 2a 2f 0a 20 20 20  ite-space */.   
31377 20 20 20 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f     zSql = zLefto
31378 76 65 72 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69  ver;.      conti
31379 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  nue;.    }..    
3137a 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74 20 3d  callbackIsInit =
3137b 20 30 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 73   0;.    nCol = s
3137c 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f  qlite3_column_co
3137d 75 6e 74 28 70 53 74 6d 74 29 3b 0a 0a 20 20 20  unt(pStmt);..   
3137e 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20   while( 1 ){.   
3137f 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
31380 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
31381 70 28 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20 20  p(pStmt);..     
31382 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 63   /* Invoke the c
31383 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
31384 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a   if required */.
31385 20 20 20 20 20 20 69 66 28 20 78 43 61 6c 6c 62        if( xCallb
31386 61 63 6b 20 26 26 20 28 53 51 4c 49 54 45 5f 52  ack && (SQLITE_R
31387 4f 57 3d 3d 72 63 20 7c 7c 20 0a 20 20 20 20 20  OW==rc || .     
31388 20 20 20 20 20 28 53 51 4c 49 54 45 5f 44 4f 4e       (SQLITE_DON
31389 45 3d 3d 72 63 20 26 26 20 21 63 61 6c 6c 62 61  E==rc && !callba
3138a 63 6b 49 73 49 6e 69 74 0a 20 20 20 20 20 20 20  ckIsInit.       
3138b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3138c 20 20 20 20 26 26 20 64 62 2d 3e 66 6c 61 67 73      && db->flags
3138d 26 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c  &SQLITE_NullCall
3138e 62 61 63 6b 29 29 20 29 7b 0a 20 20 20 20 20 20  back)) ){.      
3138f 20 20 69 66 28 20 21 63 61 6c 6c 62 61 63 6b 49    if( !callbackI
31390 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20  sInit ){.       
31391 20 20 20 61 7a 43 6f 6c 73 20 3d 20 73 71 6c 69     azCols = sqli
31392 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
31393 64 62 2c 20 32 2a 6e 43 6f 6c 2a 73 69 7a 65 6f  db, 2*nCol*sizeo
31394 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 20 2b  f(const char*) +
31395 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69   1);.          i
31396 66 28 20 61 7a 43 6f 6c 73 3d 3d 30 20 29 7b 0a  f( azCols==0 ){.
31397 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f              goto
31398 20 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20   exec_out;.     
31399 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
3139a 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
3139b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
3139c 20 20 20 20 61 7a 43 6f 6c 73 5b 69 5d 20 3d 20      azCols[i] = 
3139d 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
3139e 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 70 53 74 6d  column_name(pStm
3139f 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 20 20 20  t, i);.         
313a0 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 56 64 62     /* sqlite3Vdb
313a1 65 53 65 74 43 6f 6c 4e 61 6d 65 28 29 20 69 6e  eSetColName() in
313a2 73 74 61 6c 6c 73 20 63 6f 6c 75 6d 6e 20 6e 61  stalls column na
313a3 6d 65 73 20 61 73 20 55 54 46 38 0a 20 20 20 20  mes as UTF8.    
313a4 20 20 20 20 20 20 20 20 2a 2a 20 73 74 72 69 6e          ** strin
313a5 67 73 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e  gs so there is n
313a6 6f 20 77 61 79 20 66 6f 72 20 73 71 6c 69 74 65  o way for sqlite
313a7 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 20  3_column_name() 
313a8 74 6f 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 20 20  to fail. */.    
313a9 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
313aa 61 7a 43 6f 6c 73 5b 69 5d 21 3d 30 20 29 3b 0a  azCols[i]!=0 );.
313ab 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
313ac 20 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 49 73        callbackIs
313ad 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  Init = 1;.      
313ae 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
313af 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc==SQLITE_ROW )
313b0 7b 0a 20 20 20 20 20 20 20 20 20 20 61 7a 56 61  {.          azVa
313b1 6c 73 20 3d 20 26 61 7a 43 6f 6c 73 5b 6e 43 6f  ls = &azCols[nCo
313b2 6c 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f  l];.          fo
313b3 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
313b4 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
313b5 20 61 7a 56 61 6c 73 5b 69 5d 20 3d 20 28 63 68   azVals[i] = (ch
313b6 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c  ar *)sqlite3_col
313b7 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20  umn_text(pStmt, 
313b8 69 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  i);.            
313b9 69 66 28 20 21 61 7a 56 61 6c 73 5b 69 5d 20 26  if( !azVals[i] &
313ba 26 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  & sqlite3_column
313bb 5f 74 79 70 65 28 70 53 74 6d 74 2c 20 69 29 21  _type(pStmt, i)!
313bc 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a  =SQLITE_NULL ){.
313bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62                db
313be 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
313bf 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
313c0 20 20 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b    goto exec_out;
313c1 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
313c2 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
313c3 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
313c4 20 78 43 61 6c 6c 62 61 63 6b 28 70 41 72 67 2c   xCallback(pArg,
313c5 20 6e 43 6f 6c 2c 20 61 7a 56 61 6c 73 2c 20 61   nCol, azVals, a
313c6 7a 43 6f 6c 73 29 20 29 7b 0a 20 20 20 20 20 20  zCols) ){.      
313c7 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
313c8 41 42 4f 52 54 3b 0a 20 20 20 20 20 20 20 20 20  ABORT;.         
313c9 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61   sqlite3VdbeFina
313ca 6c 69 7a 65 28 28 56 64 62 65 20 2a 29 70 53 74  lize((Vdbe *)pSt
313cb 6d 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  mt);.          p
313cc 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  Stmt = 0;.      
313cd 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
313ce 28 64 62 2c 20 53 51 4c 49 54 45 5f 41 42 4f 52  (db, SQLITE_ABOR
313cf 54 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  T, 0);.         
313d0 20 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b 0a   goto exec_out;.
313d1 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
313d2 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  }..      if( rc!
313d3 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
313d4 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
313d5 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28  te3VdbeFinalize(
313d6 28 56 64 62 65 20 2a 29 70 53 74 6d 74 29 3b 0a  (Vdbe *)pStmt);.
313d7 20 20 20 20 20 20 20 20 70 53 74 6d 74 20 3d 20          pStmt = 
313d8 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  0;.        if( r
313d9 63 21 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  c!=SQLITE_SCHEMA
313da 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 52   ){.          nR
313db 65 74 72 79 20 3d 20 30 3b 0a 20 20 20 20 20 20  etry = 0;.      
313dc 20 20 20 20 7a 53 71 6c 20 3d 20 7a 4c 65 66 74      zSql = zLeft
313dd 6f 76 65 72 3b 0a 20 20 20 20 20 20 20 20 20 20  over;.          
313de 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73  while( sqlite3Is
313df 73 70 61 63 65 28 7a 53 71 6c 5b 30 5d 29 20 29  space(zSql[0]) )
313e0 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20   zSql++;.       
313e1 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
313e2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
313e3 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
313e4 65 65 28 64 62 2c 20 61 7a 43 6f 6c 73 29 3b 0a  ee(db, azCols);.
313e5 20 20 20 20 61 7a 43 6f 6c 73 20 3d 20 30 3b 0a      azCols = 0;.
313e6 20 20 7d 0a 0a 65 78 65 63 5f 6f 75 74 3a 0a 20    }..exec_out:. 
313e7 20 69 66 28 20 70 53 74 6d 74 20 29 20 73 71 6c   if( pStmt ) sql
313e8 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
313e9 28 28 56 64 62 65 20 2a 29 70 53 74 6d 74 29 3b  ((Vdbe *)pStmt);
313ea 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
313eb 28 64 62 2c 20 61 7a 43 6f 6c 73 29 3b 0a 0a 20  (db, azCols);.. 
313ec 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
313ed 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
313ee 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
313ef 4b 20 26 26 20 41 4c 57 41 59 53 28 72 63 3d 3d  K && ALWAYS(rc==
313f0 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
313f1 64 62 29 29 20 26 26 20 70 7a 45 72 72 4d 73 67  db)) && pzErrMsg
313f2 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 45 72 72   ){.    int nErr
313f3 4d 73 67 20 3d 20 31 20 2b 20 73 71 6c 69 74 65  Msg = 1 + sqlite
313f4 33 53 74 72 6c 65 6e 33 30 28 73 71 6c 69 74 65  3Strlen30(sqlite
313f5 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20  3_errmsg(db));. 
313f6 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73     *pzErrMsg = s
313f7 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 45 72  qlite3Malloc(nEr
313f8 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 28 20 2a  rMsg);.    if( *
313f9 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  pzErrMsg ){.    
313fa 20 20 6d 65 6d 63 70 79 28 2a 70 7a 45 72 72 4d    memcpy(*pzErrM
313fb 73 67 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  sg, sqlite3_errm
313fc 73 67 28 64 62 29 2c 20 6e 45 72 72 4d 73 67 29  sg(db), nErrMsg)
313fd 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
313fe 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
313ff 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 73 71 6c 69  OMEM;.      sqli
31400 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
31401 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20  ITE_NOMEM, 0);. 
31402 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
31403 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20   pzErrMsg ){.   
31404 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a   *pzErrMsg = 0;.
31405 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 28    }..  assert( (
31406 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d  rc&db->errMask)=
31407 3d 72 63 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  =rc );.  sqlite3
31408 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
31409 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
3140a 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  n rc;.}../******
3140b 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
3140c 6c 65 67 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a  legacy.c *******
3140d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3140e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3140f 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
31410 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
31411 69 6c 65 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a  ile loadext.c **
31412 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31413 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31414 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
31415 30 30 36 20 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a  006 June 7.**.**
31416 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
31417 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
31418 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
31419 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
3141a 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
3141b 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
3141c 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
3141d 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
3141e 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
3141f 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
31420 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
31421 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
31422 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
31423 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
31424 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
31425 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
31426 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
31427 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31428 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31429 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3142a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3142b 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
3142c 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
3142d 63 6f 64 65 20 75 73 65 64 20 74 6f 20 64 79 6e  code used to dyn
3142e 61 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 20 65 78  amically load ex
3142f 74 65 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a  tensions into.**
31430 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
31431 61 72 79 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66  ary..*/..#ifndef
31432 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20 23   SQLITE_CORE.  #
31433 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
31434 52 45 20 31 20 20 2f 2a 20 44 69 73 61 62 6c 65  RE 1  /* Disable
31435 20 74 68 65 20 41 50 49 20 72 65 64 65 66 69 6e   the API redefin
31436 69 74 69 6f 6e 20 69 6e 20 73 71 6c 69 74 65 33  ition in sqlite3
31437 65 78 74 2e 68 20 2a 2f 0a 23 65 6e 64 69 66 0a  ext.h */.#endif.
31438 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
31439 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 33 65  Include sqlite3e
3143a 78 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  xt.h in the midd
3143b 6c 65 20 6f 66 20 6c 6f 61 64 65 78 74 2e 63 20  le of loadext.c 
3143c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
3143d 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
3143e 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74  Begin file sqlit
3143f 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  e3ext.h ********
31440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31441 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
31442 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65 20  /*.** 2006 June 
31443 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
31444 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
31445 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
31446 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
31447 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
31448 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
31449 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
3144a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
3144b 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
3144c 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
3144d 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
3144e 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
3144f 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
31450 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
31451 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
31452 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
31453 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
31454 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
31455 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31456 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31457 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31458 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31459 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
3145a 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65  file defines the
3145b 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63   SQLite interfac
3145c 65 20 66 6f 72 20 75 73 65 20 62 79 0a 2a 2a 20  e for use by.** 
3145d 73 68 61 72 65 64 20 6c 69 62 72 61 72 69 65 73  shared libraries
3145e 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20 62 65   that want to be
3145f 20 69 6d 70 6f 72 74 65 64 20 61 73 20 65 78 74   imported as ext
31460 65 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20  ensions into.** 
31461 61 6e 20 53 51 4c 69 74 65 20 69 6e 73 74 61 6e  an SQLite instan
31462 63 65 2e 20 20 53 68 61 72 65 64 20 6c 69 62 72  ce.  Shared libr
31463 61 72 69 65 73 20 74 68 61 74 20 69 6e 74 65 6e  aries that inten
31464 64 20 74 6f 20 62 65 20 6c 6f 61 64 65 64 0a 2a  d to be loaded.*
31465 2a 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73 20  * as extensions 
31466 62 79 20 53 51 4c 69 74 65 20 73 68 6f 75 6c 64  by SQLite should
31467 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73 20 66   #include this f
31468 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 0a  ile instead of .
31469 2a 2a 20 73 71 6c 69 74 65 33 2e 68 2e 0a 2a 2f  ** sqlite3.h..*/
3146a 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45  .#ifndef _SQLITE
3146b 33 45 58 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20  3EXT_H_.#define 
3146c 5f 53 51 4c 49 54 45 33 45 58 54 5f 48 5f 0a 0a  _SQLITE3EXT_H_..
3146d 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
3146e 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
3146f 6e 65 73 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nes sqlite3_api_
31470 72 6f 75 74 69 6e 65 73 3b 0a 0a 2f 2a 0a 2a 2a  routines;../*.**
31471 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
31472 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 70  tructure holds p
31473 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 6f  ointers to all o
31474 66 20 74 68 65 20 53 51 4c 69 74 65 20 41 50 49  f the SQLite API
31475 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  .** routines..**
31476 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 20 49 6e  .** WARNING:  In
31477 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 61   order to mainta
31478 69 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d  in backwards com
31479 70 61 74 69 62 69 6c 69 74 79 2c 20 61 64 64 20  patibility, add 
3147a 6e 65 77 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  new.** interface
3147b 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  s to the end of 
3147c 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f  this structure o
3147d 6e 6c 79 2e 20 20 49 66 20 79 6f 75 20 69 6e 73  nly.  If you ins
3147e 65 72 74 20 6e 65 77 0a 2a 2a 20 69 6e 74 65 72  ert new.** inter
3147f 66 61 63 65 73 20 69 6e 20 74 68 65 20 6d 69 64  faces in the mid
31480 64 6c 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  dle of this stru
31481 63 74 75 72 65 2c 20 74 68 65 6e 20 6f 6c 64 65  cture, then olde
31482 72 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 76  r different.** v
31483 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
31484 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62  e will not be ab
31485 6c 65 20 74 6f 20 6c 6f 61 64 20 65 61 63 68 20  le to load each 
31486 6f 74 68 65 72 73 27 20 73 68 61 72 65 64 0a 2a  others' shared.*
31487 2a 20 6c 69 62 72 61 72 69 65 73 21 0a 2a 2f 0a  * libraries!.*/.
31488 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 61  struct sqlite3_a
31489 70 69 5f 72 6f 75 74 69 6e 65 73 20 7b 0a 20 20  pi_routines {.  
3148a 76 6f 69 64 20 2a 20 28 2a 61 67 67 72 65 67 61  void * (*aggrega
3148b 74 65 5f 63 6f 6e 74 65 78 74 29 28 73 71 6c 69  te_context)(sqli
3148c 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
3148d 20 6e 42 79 74 65 73 29 3b 0a 20 20 69 6e 74 20   nBytes);.  int 
3148e 20 28 2a 61 67 67 72 65 67 61 74 65 5f 63 6f 75   (*aggregate_cou
3148f 6e 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nt)(sqlite3_cont
31490 65 78 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ext*);.  int  (*
31491 62 69 6e 64 5f 62 6c 6f 62 29 28 73 71 6c 69 74  bind_blob)(sqlit
31492 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e  e3_stmt*,int,con
31493 73 74 20 76 6f 69 64 2a 2c 69 6e 74 20 6e 2c 76  st void*,int n,v
31494 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
31495 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 64 6f    int  (*bind_do
31496 75 62 6c 65 29 28 73 71 6c 69 74 65 33 5f 73 74  uble)(sqlite3_st
31497 6d 74 2a 2c 69 6e 74 2c 64 6f 75 62 6c 65 29 3b  mt*,int,double);
31498 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 69  .  int  (*bind_i
31499 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  nt)(sqlite3_stmt
3149a 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e  *,int,int);.  in
3149b 74 20 20 28 2a 62 69 6e 64 5f 69 6e 74 36 34 29  t  (*bind_int64)
3149c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
3149d 6e 74 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29  nt,sqlite_int64)
3149e 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f  ;.  int  (*bind_
3149f 6e 75 6c 6c 29 28 73 71 6c 69 74 65 33 5f 73 74  null)(sqlite3_st
314a0 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  mt*,int);.  int 
314a1 20 28 2a 62 69 6e 64 5f 70 61 72 61 6d 65 74 65   (*bind_paramete
314a2 72 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33  r_count)(sqlite3
314a3 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20  _stmt*);.  int  
314a4 28 2a 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  (*bind_parameter
314a5 5f 69 6e 64 65 78 29 28 73 71 6c 69 74 65 33 5f  _index)(sqlite3_
314a6 73 74 6d 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72  stmt*,const char
314a7 2a 7a 4e 61 6d 65 29 3b 0a 20 20 63 6f 6e 73 74  *zName);.  const
314a8 20 63 68 61 72 20 2a 20 28 2a 62 69 6e 64 5f 70   char * (*bind_p
314a9 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 29 28 73  arameter_name)(s
314aa 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
314ab 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64  );.  int  (*bind
314ac 5f 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f 73  _text)(sqlite3_s
314ad 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  tmt*,int,const c
314ae 68 61 72 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64 28  har*,int n,void(
314af 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e  *)(void*));.  in
314b0 74 20 20 28 2a 62 69 6e 64 5f 74 65 78 74 31 36  t  (*bind_text16
314b1 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
314b2 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
314b3 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
314b4 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69  *));.  int  (*bi
314b5 6e 64 5f 76 61 6c 75 65 29 28 73 71 6c 69 74 65  nd_value)(sqlite
314b6 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73  3_stmt*,int,cons
314b7 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t sqlite3_value*
314b8 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 75 73 79  );.  int  (*busy
314b9 5f 68 61 6e 64 6c 65 72 29 28 73 71 6c 69 74 65  _handler)(sqlite
314ba 33 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c  3*,int(*)(void*,
314bb 69 6e 74 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69  int),void*);.  i
314bc 6e 74 20 20 28 2a 62 75 73 79 5f 74 69 6d 65 6f  nt  (*busy_timeo
314bd 75 74 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  ut)(sqlite3*,int
314be 20 6d 73 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63   ms);.  int  (*c
314bf 68 61 6e 67 65 73 29 28 73 71 6c 69 74 65 33 2a  hanges)(sqlite3*
314c0 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6c 6f 73  );.  int  (*clos
314c1 65 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20  e)(sqlite3*);.  
314c2 69 6e 74 20 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e  int  (*collation
314c3 5f 6e 65 65 64 65 64 29 28 73 71 6c 69 74 65 33  _needed)(sqlite3
314c4 2a 2c 76 6f 69 64 2a 2c 76 6f 69 64 28 2a 29 28  *,void*,void(*)(
314c5 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 0a  void*,sqlite3*,.
314c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
314c7 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 65             int e
314c8 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68  TextRep,const ch
314c9 61 72 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ar*));.  int  (*
314ca 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
314cb 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69  16)(sqlite3*,voi
314cc 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  d*,void(*)(void*
314cd 2c 73 71 6c 69 74 65 33 2a 2c 0a 20 20 20 20 20  ,sqlite3*,.     
314ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
314cf 20 20 20 20 20 20 20 20 69 6e 74 20 65 54 65 78          int eTex
314d0 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  tRep,const void*
314d1 29 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  ));.  const void
314d2 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 62 6c 6f 62   * (*column_blob
314d3 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
314d4 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74  int iCol);.  int
314d5 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79 74 65 73    (*column_bytes
314d6 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
314d7 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74  int iCol);.  int
314d8 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79 74 65 73    (*column_bytes
314d9 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  16)(sqlite3_stmt
314da 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69  *,int iCol);.  i
314db 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 63 6f 75  nt  (*column_cou
314dc 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  nt)(sqlite3_stmt
314dd 2a 70 53 74 6d 74 29 3b 0a 20 20 63 6f 6e 73 74  *pStmt);.  const
314de 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e   char * (*column
314df 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 29 28  _database_name)(
314e0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
314e1 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  t);.  const void
314e2 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 61 74 61   * (*column_data
314e3 62 61 73 65 5f 6e 61 6d 65 31 36 29 28 73 71 6c  base_name16)(sql
314e4 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
314e5 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  .  const char * 
314e6 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  (*column_decltyp
314e7 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
314e8 2c 69 6e 74 20 69 29 3b 0a 20 20 63 6f 6e 73 74  ,int i);.  const
314e9 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e   void * (*column
314ea 5f 64 65 63 6c 74 79 70 65 31 36 29 28 73 71 6c  _decltype16)(sql
314eb 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
314ec 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a 63 6f 6c  .  double  (*col
314ed 75 6d 6e 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69  umn_double)(sqli
314ee 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
314ef 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f  ol);.  int  (*co
314f0 6c 75 6d 6e 5f 69 6e 74 29 28 73 71 6c 69 74 65  lumn_int)(sqlite
314f1 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c  3_stmt*,int iCol
314f2 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  );.  sqlite_int6
314f3 34 20 20 28 2a 63 6f 6c 75 6d 6e 5f 69 6e 74 36  4  (*column_int6
314f4 34 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  4)(sqlite3_stmt*
314f5 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 63 6f  ,int iCol);.  co
314f6 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c  nst char * (*col
314f7 75 6d 6e 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65  umn_name)(sqlite
314f8 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20  3_stmt*,int);.  
314f9 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63  const void * (*c
314fa 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 29 28 73 71  olumn_name16)(sq
314fb 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
314fc 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
314fd 20 28 2a 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e   (*column_origin
314fe 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73  _name)(sqlite3_s
314ff 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e  tmt*,int);.  con
31500 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75  st void * (*colu
31501 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36  mn_origin_name16
31502 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
31503 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68  int);.  const ch
31504 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 61  ar * (*column_ta
31505 62 6c 65 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65  ble_name)(sqlite
31506 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20  3_stmt*,int);.  
31507 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63  const void * (*c
31508 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
31509 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  16)(sqlite3_stmt
3150a 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20  *,int);.  const 
3150b 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 20  unsigned char * 
3150c 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74 29 28 73  (*column_text)(s
3150d 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
3150e 20 69 43 6f 6c 29 3b 0a 20 20 63 6f 6e 73 74 20   iCol);.  const 
3150f 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  void * (*column_
31510 74 65 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f  text16)(sqlite3_
31511 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b  stmt*,int iCol);
31512 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e  .  int  (*column
31513 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f 73  _type)(sqlite3_s
31514 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
31515 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a    sqlite3_value*
31516 20 28 2a 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 29   (*column_value)
31517 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
31518 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 76 6f 69 64  nt iCol);.  void
31519 20 2a 20 28 2a 63 6f 6d 6d 69 74 5f 68 6f 6f 6b   * (*commit_hook
3151a 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a  )(sqlite3*,int(*
3151b 29 28 76 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b  )(void*),void*);
3151c 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6d 70 6c 65  .  int  (*comple
3151d 74 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 73  te)(const char*s
3151e 71 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f  ql);.  int  (*co
3151f 6d 70 6c 65 74 65 31 36 29 28 63 6f 6e 73 74 20  mplete16)(const 
31520 76 6f 69 64 2a 73 71 6c 29 3b 0a 20 20 69 6e 74  void*sql);.  int
31521 20 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61    (*create_colla
31522 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a 2c 63  tion)(sqlite3*,c
31523 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76  onst char*,int,v
31524 6f 69 64 2a 2c 0a 20 20 20 20 20 20 20 20 20 20  oid*,.          
31525 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31526 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e   int(*)(void*,in
31527 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
31528 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 29 3b  t,const void*));
31529 0a 20 20 69 6e 74 20 20 28 2a 63 72 65 61 74 65  .  int  (*create
3152a 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 29 28 73 71  _collation16)(sq
3152b 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69  lite3*,const voi
3152c 64 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 0a 20 20  d*,int,void*,.  
3152d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3152e 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 28 2a             int(*
3152f 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
31530 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
31531 74 20 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74  t void*));.  int
31532 20 20 28 2a 63 72 65 61 74 65 5f 66 75 6e 63 74    (*create_funct
31533 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  ion)(sqlite3*,co
31534 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e  nst char*,int,in
31535 74 2c 76 6f 69 64 2a 2c 0a 20 20 20 20 20 20 20  t,void*,.       
31536 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31537 20 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29     void (*xFunc)
31538 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
31539 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
3153a 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20  lue**),.        
3153b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3153c 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28    void (*xStep)(
3153d 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
3153e 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
3153f 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20  ue**),.         
31540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31541 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28   void (*xFinal)(
31542 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
31543 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72 65  ));.  int  (*cre
31544 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 29 28  ate_function16)(
31545 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76  sqlite3*,const v
31546 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69  oid*,int,int,voi
31547 64 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  d*,.            
31548 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31549 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
3154a 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
3154b 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
3154c 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  **),.           
3154d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3154e 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73   void (*xStep)(s
3154f 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
31550 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
31551 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 20  e**),.          
31552 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31553 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29    void (*xFinal)
31554 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
31555 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65  *));.  int (*cre
31556 61 74 65 5f 6d 6f 64 75 6c 65 29 28 73 71 6c 69  ate_module)(sqli
31557 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
31558 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d  ,const sqlite3_m
31559 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 29 3b 0a 20  odule*,void*);. 
3155a 20 69 6e 74 20 20 28 2a 64 61 74 61 5f 63 6f 75   int  (*data_cou
3155b 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  nt)(sqlite3_stmt
3155c 2a 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74  *pStmt);.  sqlit
3155d 65 33 20 2a 20 28 2a 64 62 5f 68 61 6e 64 6c 65  e3 * (*db_handle
3155e 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  )(sqlite3_stmt*)
3155f 3b 0a 20 20 69 6e 74 20 28 2a 64 65 63 6c 61 72  ;.  int (*declar
31560 65 5f 76 74 61 62 29 28 73 71 6c 69 74 65 33 2a  e_vtab)(sqlite3*
31561 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20  ,const char*);. 
31562 20 69 6e 74 20 20 28 2a 65 6e 61 62 6c 65 5f 73   int  (*enable_s
31563 68 61 72 65 64 5f 63 61 63 68 65 29 28 69 6e 74  hared_cache)(int
31564 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 72 72 63  );.  int  (*errc
31565 6f 64 65 29 28 73 71 6c 69 74 65 33 2a 64 62 29  ode)(sqlite3*db)
31566 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
31567 20 28 2a 65 72 72 6d 73 67 29 28 73 71 6c 69 74   (*errmsg)(sqlit
31568 65 33 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  e3*);.  const vo
31569 69 64 20 2a 20 28 2a 65 72 72 6d 73 67 31 36 29  id * (*errmsg16)
3156a 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e  (sqlite3*);.  in
3156b 74 20 20 28 2a 65 78 65 63 29 28 73 71 6c 69 74  t  (*exec)(sqlit
3156c 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
3156d 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b  sqlite3_callback
3156e 2c 76 6f 69 64 2a 2c 63 68 61 72 2a 2a 29 3b 0a  ,void*,char**);.
3156f 20 20 69 6e 74 20 20 28 2a 65 78 70 69 72 65 64    int  (*expired
31570 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  )(sqlite3_stmt*)
31571 3b 0a 20 20 69 6e 74 20 20 28 2a 66 69 6e 61 6c  ;.  int  (*final
31572 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ize)(sqlite3_stm
31573 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69 64  t*pStmt);.  void
31574 20 20 28 2a 66 72 65 65 29 28 76 6f 69 64 2a 29    (*free)(void*)
31575 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66 72 65 65  ;.  void  (*free
31576 5f 74 61 62 6c 65 29 28 63 68 61 72 2a 2a 72 65  _table)(char**re
31577 73 75 6c 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a  sult);.  int  (*
31578 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 29 28  get_autocommit)(
31579 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76 6f 69  sqlite3*);.  voi
3157a 64 20 2a 20 28 2a 67 65 74 5f 61 75 78 64 61 74  d * (*get_auxdat
3157b 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  a)(sqlite3_conte
3157c 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  xt*,int);.  int 
3157d 20 28 2a 67 65 74 5f 74 61 62 6c 65 29 28 73 71   (*get_table)(sq
3157e 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
3157f 72 2a 2c 63 68 61 72 2a 2a 2a 2c 69 6e 74 2a 2c  r*,char***,int*,
31580 69 6e 74 2a 2c 63 68 61 72 2a 2a 29 3b 0a 20 20  int*,char**);.  
31581 69 6e 74 20 20 28 2a 67 6c 6f 62 61 6c 5f 72 65  int  (*global_re
31582 63 6f 76 65 72 29 28 76 6f 69 64 29 3b 0a 20 20  cover)(void);.  
31583 76 6f 69 64 20 20 28 2a 69 6e 74 65 72 72 75 70  void  (*interrup
31584 74 78 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  tx)(sqlite3*);. 
31585 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 20 28   sqlite_int64  (
31586 2a 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  *last_insert_row
31587 69 64 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  id)(sqlite3*);. 
31588 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a   const char * (*
31589 6c 69 62 76 65 72 73 69 6f 6e 29 28 76 6f 69 64  libversion)(void
3158a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6c 69 62 76  );.  int  (*libv
3158b 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 29 28 76  ersion_number)(v
3158c 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a  oid);.  void *(*
3158d 6d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20  malloc)(int);.  
3158e 63 68 61 72 20 2a 20 28 2a 6d 70 72 69 6e 74 66  char * (*mprintf
3158f 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e  )(const char*,..
31590 2e 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6f 70 65  .);.  int  (*ope
31591 6e 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  n)(const char*,s
31592 71 6c 69 74 65 33 2a 2a 29 3b 0a 20 20 69 6e 74  qlite3**);.  int
31593 20 20 28 2a 6f 70 65 6e 31 36 29 28 63 6f 6e 73    (*open16)(cons
31594 74 20 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  t void*,sqlite3*
31595 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 70 72 65  *);.  int  (*pre
31596 70 61 72 65 29 28 73 71 6c 69 74 65 33 2a 2c 63  pare)(sqlite3*,c
31597 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 73  onst char*,int,s
31598 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f  qlite3_stmt**,co
31599 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69  nst char**);.  i
3159a 6e 74 20 20 28 2a 70 72 65 70 61 72 65 31 36 29  nt  (*prepare16)
3159b 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
3159c 76 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  void*,int,sqlite
3159d 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76  3_stmt**,const v
3159e 6f 69 64 2a 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  oid**);.  void *
3159f 20 28 2a 70 72 6f 66 69 6c 65 29 28 73 71 6c 69   (*profile)(sqli
315a0 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69  te3*,void(*)(voi
315a1 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  d*,const char*,s
315a2 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 2c 76 6f  qlite_uint64),vo
315a3 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  id*);.  void  (*
315a4 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72  progress_handler
315a5 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 69  )(sqlite3*,int,i
315a6 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69  nt(*)(void*),voi
315a7 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 72  d*);.  void *(*r
315a8 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e  ealloc)(void*,in
315a9 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 72 65 73  t);.  int  (*res
315aa 65 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  et)(sqlite3_stmt
315ab 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69 64 20  *pStmt);.  void 
315ac 20 28 2a 72 65 73 75 6c 74 5f 62 6c 6f 62 29 28   (*result_blob)(
315ad 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
315ae 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
315af 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
315b0 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
315b1 6c 74 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74  lt_double)(sqlit
315b2 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 64 6f 75 62  e3_context*,doub
315b3 6c 65 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  le);.  void  (*r
315b4 65 73 75 6c 74 5f 65 72 72 6f 72 29 28 73 71 6c  esult_error)(sql
315b5 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f  ite3_context*,co
315b6 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a  nst char*,int);.
315b7 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
315b8 5f 65 72 72 6f 72 31 36 29 28 73 71 6c 69 74 65  _error16)(sqlite
315b9 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74  3_context*,const
315ba 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a 20 20 76   void*,int);.  v
315bb 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e  oid  (*result_in
315bc 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  t)(sqlite3_conte
315bd 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64  xt*,int);.  void
315be 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e 74 36 34    (*result_int64
315bf 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
315c0 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29  t*,sqlite_int64)
315c1 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
315c2 6c 74 5f 6e 75 6c 6c 29 28 73 71 6c 69 74 65 33  lt_null)(sqlite3
315c3 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f  _context*);.  vo
315c4 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65 78  id  (*result_tex
315c5 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  t)(sqlite3_conte
315c6 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  xt*,const char*,
315c7 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
315c8 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  *));.  void  (*r
315c9 65 73 75 6c 74 5f 74 65 78 74 31 36 29 28 73 71  esult_text16)(sq
315ca 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63  lite3_context*,c
315cb 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76  onst void*,int,v
315cc 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
315cd 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
315ce 5f 74 65 78 74 31 36 62 65 29 28 73 71 6c 69 74  _text16be)(sqlit
315cf 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73  e3_context*,cons
315d0 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64  t void*,int,void
315d1 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76  (*)(void*));.  v
315d2 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65  oid  (*result_te
315d3 78 74 31 36 6c 65 29 28 73 71 6c 69 74 65 33 5f  xt16le)(sqlite3_
315d4 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76  context*,const v
315d5 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29  oid*,int,void(*)
315d6 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64  (void*));.  void
315d7 20 20 28 2a 72 65 73 75 6c 74 5f 76 61 6c 75 65    (*result_value
315d8 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
315d9 74 2a 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t*,sqlite3_value
315da 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 72  *);.  void * (*r
315db 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 29 28 73 71  ollback_hook)(sq
315dc 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76  lite3*,void(*)(v
315dd 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  oid*),void*);.  
315de 69 6e 74 20 20 28 2a 73 65 74 5f 61 75 74 68 6f  int  (*set_autho
315df 72 69 7a 65 72 29 28 73 71 6c 69 74 65 33 2a 2c  rizer)(sqlite3*,
315e0 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74  int(*)(void*,int
315e1 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
315e2 73 74 20 63 68 61 72 2a 2c 0a 20 20 20 20 20 20  st char*,.      
315e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
315e4 20 20 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63     const char*,c
315e5 6f 6e 73 74 20 63 68 61 72 2a 29 2c 76 6f 69 64  onst char*),void
315e6 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 73 65  *);.  void  (*se
315e7 74 5f 61 75 78 64 61 74 61 29 28 73 71 6c 69 74  t_auxdata)(sqlit
315e8 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
315e9 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 29 28 76  void*,void (*)(v
315ea 6f 69 64 2a 29 29 3b 0a 20 20 63 68 61 72 20 2a  oid*));.  char *
315eb 20 28 2a 73 6e 70 72 69 6e 74 66 29 28 69 6e 74   (*snprintf)(int
315ec 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61  ,char*,const cha
315ed 72 2a 2c 2e 2e 2e 29 3b 0a 20 20 69 6e 74 20 20  r*,...);.  int  
315ee 28 2a 73 74 65 70 29 28 73 71 6c 69 74 65 33 5f  (*step)(sqlite3_
315ef 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28  stmt*);.  int  (
315f0 2a 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65  *table_column_me
315f1 74 61 64 61 74 61 29 28 73 71 6c 69 74 65 33 2a  tadata)(sqlite3*
315f2 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
315f3 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
315f4 68 61 72 2a 2c 0a 20 20 20 20 20 20 20 20 20 20  har*,.          
315f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
315f6 20 20 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74        char const
315f7 2a 2a 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2a 2c  **,char const**,
315f8 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b  int*,int*,int*);
315f9 0a 20 20 76 6f 69 64 20 20 28 2a 74 68 72 65 61  .  void  (*threa
315fa 64 5f 63 6c 65 61 6e 75 70 29 28 76 6f 69 64 29  d_cleanup)(void)
315fb 3b 0a 20 20 69 6e 74 20 20 28 2a 74 6f 74 61 6c  ;.  int  (*total
315fc 5f 63 68 61 6e 67 65 73 29 28 73 71 6c 69 74 65  _changes)(sqlite
315fd 33 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a  3*);.  void * (*
315fe 74 72 61 63 65 29 28 73 71 6c 69 74 65 33 2a 2c  trace)(sqlite3*,
315ff 76 6f 69 64 28 2a 78 54 72 61 63 65 29 28 76 6f  void(*xTrace)(vo
31600 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  id*,const char*)
31601 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20  ,void*);.  int  
31602 28 2a 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69  (*transfer_bindi
31603 6e 67 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ngs)(sqlite3_stm
31604 74 2a 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  t*,sqlite3_stmt*
31605 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 75 70  );.  void * (*up
31606 64 61 74 65 5f 68 6f 6f 6b 29 28 73 71 6c 69 74  date_hook)(sqlit
31607 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  e3*,void(*)(void
31608 2a 2c 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73  *,int ,char cons
31609 74 2a 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2c 0a  t*,char const*,.
3160a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3160b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3160c 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f           sqlite_
3160d 69 6e 74 36 34 29 2c 76 6f 69 64 2a 29 3b 0a 20  int64),void*);. 
3160e 20 76 6f 69 64 20 2a 20 28 2a 75 73 65 72 5f 64   void * (*user_d
3160f 61 74 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ata)(sqlite3_con
31610 74 65 78 74 2a 29 3b 0a 20 20 63 6f 6e 73 74 20  text*);.  const 
31611 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 62  void * (*value_b
31612 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 76 61 6c  lob)(sqlite3_val
31613 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76  ue*);.  int  (*v
31614 61 6c 75 65 5f 62 79 74 65 73 29 28 73 71 6c 69  alue_bytes)(sqli
31615 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69  te3_value*);.  i
31616 6e 74 20 20 28 2a 76 61 6c 75 65 5f 62 79 74 65  nt  (*value_byte
31617 73 31 36 29 28 73 71 6c 69 74 65 33 5f 76 61 6c  s16)(sqlite3_val
31618 75 65 2a 29 3b 0a 20 20 64 6f 75 62 6c 65 20 20  ue*);.  double  
31619 28 2a 76 61 6c 75 65 5f 64 6f 75 62 6c 65 29 28  (*value_double)(
3161a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
3161b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f  .  int  (*value_
3161c 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 76 61 6c  int)(sqlite3_val
3161d 75 65 2a 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69  ue*);.  sqlite_i
3161e 6e 74 36 34 20 20 28 2a 76 61 6c 75 65 5f 69 6e  nt64  (*value_in
3161f 74 36 34 29 28 73 71 6c 69 74 65 33 5f 76 61 6c  t64)(sqlite3_val
31620 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76  ue*);.  int  (*v
31621 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70  alue_numeric_typ
31622 65 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e)(sqlite3_value
31623 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  *);.  const unsi
31624 67 6e 65 64 20 63 68 61 72 20 2a 20 28 2a 76 61  gned char * (*va
31625 6c 75 65 5f 74 65 78 74 29 28 73 71 6c 69 74 65  lue_text)(sqlite
31626 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e  3_value*);.  con
31627 73 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75  st void * (*valu
31628 65 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74 65  e_text16)(sqlite
31629 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e  3_value*);.  con
3162a 73 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75  st void * (*valu
3162b 65 5f 74 65 78 74 31 36 62 65 29 28 73 71 6c 69  e_text16be)(sqli
3162c 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63  te3_value*);.  c
3162d 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 76 61  onst void * (*va
3162e 6c 75 65 5f 74 65 78 74 31 36 6c 65 29 28 73 71  lue_text16le)(sq
3162f 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
31630 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 74 79   int  (*value_ty
31631 70 65 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  pe)(sqlite3_valu
31632 65 2a 29 3b 0a 20 20 63 68 61 72 20 2a 28 2a 76  e*);.  char *(*v
31633 6d 70 72 69 6e 74 66 29 28 63 6f 6e 73 74 20 63  mprintf)(const c
31634 68 61 72 2a 2c 76 61 5f 6c 69 73 74 29 3b 0a 20  har*,va_list);. 
31635 20 2f 2a 20 41 64 64 65 64 20 3f 3f 3f 20 2a 2f   /* Added ??? */
31636 0a 20 20 69 6e 74 20 28 2a 6f 76 65 72 6c 6f 61  .  int (*overloa
31637 64 5f 66 75 6e 63 74 69 6f 6e 29 28 73 71 6c 69  d_function)(sqli
31638 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  te3*, const char
31639 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74   *zFuncName, int
3163a 20 6e 41 72 67 29 3b 0a 20 20 2f 2a 20 41 64 64   nArg);.  /* Add
3163b 65 64 20 62 79 20 33 2e 33 2e 31 33 20 2a 2f 0a  ed by 3.3.13 */.
3163c 20 20 69 6e 74 20 28 2a 70 72 65 70 61 72 65 5f    int (*prepare_
3163d 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  v2)(sqlite3*,con
3163e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 73 71 6c  st char*,int,sql
3163f 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73  ite3_stmt**,cons
31640 74 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74  t char**);.  int
31641 20 28 2a 70 72 65 70 61 72 65 31 36 5f 76 32 29   (*prepare16_v2)
31642 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
31643 76 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  void*,int,sqlite
31644 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76  3_stmt**,const v
31645 6f 69 64 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  oid**);.  int (*
31646 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 29 28  clear_bindings)(
31647 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
31648 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20 33 2e    /* Added by 3.
31649 34 2e 31 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 63  4.1 */.  int (*c
3164a 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 29  reate_module_v2)
3164b 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
3164c 63 68 61 72 2a 2c 63 6f 6e 73 74 20 73 71 6c 69  char*,const sqli
3164d 74 65 33 5f 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64  te3_module*,void
3164e 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  *,.             
3164f 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69               voi
31650 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f  d (*xDestroy)(vo
31651 69 64 20 2a 29 29 3b 0a 20 20 2f 2a 20 41 64 64  id *));.  /* Add
31652 65 64 20 62 79 20 33 2e 35 2e 30 20 2a 2f 0a 20  ed by 3.5.0 */. 
31653 20 69 6e 74 20 28 2a 62 69 6e 64 5f 7a 65 72 6f   int (*bind_zero
31654 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73 74  blob)(sqlite3_st
31655 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20  mt*,int,int);.  
31656 69 6e 74 20 28 2a 62 6c 6f 62 5f 62 79 74 65 73  int (*blob_bytes
31657 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 29  )(sqlite3_blob*)
31658 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 63  ;.  int (*blob_c
31659 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 62 6c  lose)(sqlite3_bl
3165a 6f 62 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c  ob*);.  int (*bl
3165b 6f 62 5f 6f 70 65 6e 29 28 73 71 6c 69 74 65 33  ob_open)(sqlite3
3165c 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
3165d 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
3165e 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 69 6e  char*,sqlite3_in
3165f 74 36 34 2c 0a 20 20 20 20 20 20 20 20 20 20 20  t64,.           
31660 20 20 20 20 20 20 20 20 69 6e 74 2c 73 71 6c 69          int,sqli
31661 74 65 33 5f 62 6c 6f 62 2a 2a 29 3b 0a 20 20 69  te3_blob**);.  i
31662 6e 74 20 28 2a 62 6c 6f 62 5f 72 65 61 64 29 28  nt (*blob_read)(
31663 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c 76 6f  sqlite3_blob*,vo
31664 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20  id*,int,int);.  
31665 69 6e 74 20 28 2a 62 6c 6f 62 5f 77 72 69 74 65  int (*blob_write
31666 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c  )(sqlite3_blob*,
31667 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
31668 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72  int);.  int (*cr
31669 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
3166a 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  2)(sqlite3*,cons
3166b 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64  t char*,int,void
3166c 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  *,.             
3166d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3166e 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74  int(*)(void*,int
3166f 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
31670 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20  ,const void*),. 
31671 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31672 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64              void
31673 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69  (*)(void*));.  i
31674 6e 74 20 28 2a 66 69 6c 65 5f 63 6f 6e 74 72 6f  nt (*file_contro
31675 6c 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  l)(sqlite3*,cons
31676 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64  t char*,int,void
31677 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  *);.  sqlite3_in
31678 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f 68 69 67  t64 (*memory_hig
31679 68 77 61 74 65 72 29 28 69 6e 74 29 3b 0a 20 20  hwater)(int);.  
3167a 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 28 2a  sqlite3_int64 (*
3167b 6d 65 6d 6f 72 79 5f 75 73 65 64 29 28 76 6f 69  memory_used)(voi
3167c 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  d);.  sqlite3_mu
3167d 74 65 78 20 2a 28 2a 6d 75 74 65 78 5f 61 6c 6c  tex *(*mutex_all
3167e 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64  oc)(int);.  void
3167f 20 28 2a 6d 75 74 65 78 5f 65 6e 74 65 72 29 28   (*mutex_enter)(
31680 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b  sqlite3_mutex*);
31681 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74 65 78 5f  .  void (*mutex_
31682 66 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75  free)(sqlite3_mu
31683 74 65 78 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a  tex*);.  void (*
31684 6d 75 74 65 78 5f 6c 65 61 76 65 29 28 73 71 6c  mutex_leave)(sql
31685 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20  ite3_mutex*);.  
31686 69 6e 74 20 28 2a 6d 75 74 65 78 5f 74 72 79 29  int (*mutex_try)
31687 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
31688 3b 0a 20 20 69 6e 74 20 28 2a 6f 70 65 6e 5f 76  ;.  int (*open_v
31689 32 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  2)(const char*,s
3168a 71 6c 69 74 65 33 2a 2a 2c 69 6e 74 2c 63 6f 6e  qlite3**,int,con
3168b 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74  st char*);.  int
3168c 20 28 2a 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72   (*release_memor
3168d 79 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20  y)(int);.  void 
3168e 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e  (*result_error_n
3168f 6f 6d 65 6d 29 28 73 71 6c 69 74 65 33 5f 63 6f  omem)(sqlite3_co
31690 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f 69 64 20  ntext*);.  void 
31691 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  (*result_error_t
31692 6f 6f 62 69 67 29 28 73 71 6c 69 74 65 33 5f 63  oobig)(sqlite3_c
31693 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e 74 20  ontext*);.  int 
31694 28 2a 73 6c 65 65 70 29 28 69 6e 74 29 3b 0a 20  (*sleep)(int);. 
31695 20 76 6f 69 64 20 28 2a 73 6f 66 74 5f 68 65 61   void (*soft_hea
31696 70 5f 6c 69 6d 69 74 29 28 69 6e 74 29 3b 0a 20  p_limit)(int);. 
31697 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 28 2a   sqlite3_vfs *(*
31698 76 66 73 5f 66 69 6e 64 29 28 63 6f 6e 73 74 20  vfs_find)(const 
31699 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  char*);.  int (*
3169a 76 66 73 5f 72 65 67 69 73 74 65 72 29 28 73 71  vfs_register)(sq
3169b 6c 69 74 65 33 5f 76 66 73 2a 2c 69 6e 74 29 3b  lite3_vfs*,int);
3169c 0a 20 20 69 6e 74 20 28 2a 76 66 73 5f 75 6e 72  .  int (*vfs_unr
3169d 65 67 69 73 74 65 72 29 28 73 71 6c 69 74 65 33  egister)(sqlite3
3169e 5f 76 66 73 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  _vfs*);.  int (*
3169f 78 74 68 72 65 61 64 73 61 66 65 29 28 76 6f 69  xthreadsafe)(voi
316a0 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73  d);.  void (*res
316a1 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 29 28 73 71  ult_zeroblob)(sq
316a2 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
316a3 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65  nt);.  void (*re
316a4 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 29  sult_error_code)
316a5 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
316a6 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a  *,int);.  int (*
316a7 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 29 28 69 6e  test_control)(in
316a8 74 2c 20 2e 2e 2e 29 3b 0a 20 20 76 6f 69 64 20  t, ...);.  void 
316a9 28 2a 72 61 6e 64 6f 6d 6e 65 73 73 29 28 69 6e  (*randomness)(in
316aa 74 2c 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69  t,void*);.  sqli
316ab 74 65 33 20 2a 28 2a 63 6f 6e 74 65 78 74 5f 64  te3 *(*context_d
316ac 62 5f 68 61 6e 64 6c 65 29 28 73 71 6c 69 74 65  b_handle)(sqlite
316ad 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69  3_context*);.  i
316ae 6e 74 20 28 2a 65 78 74 65 6e 64 65 64 5f 72 65  nt (*extended_re
316af 73 75 6c 74 5f 63 6f 64 65 73 29 28 73 71 6c 69  sult_codes)(sqli
316b0 74 65 33 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  te3*,int);.  int
316b1 20 28 2a 6c 69 6d 69 74 29 28 73 71 6c 69 74 65   (*limit)(sqlite
316b2 33 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 73  3*,int,int);.  s
316b3 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 28 2a 6e  qlite3_stmt *(*n
316b4 65 78 74 5f 73 74 6d 74 29 28 73 71 6c 69 74 65  ext_stmt)(sqlite
316b5 33 2a 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  3*,sqlite3_stmt*
316b6 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
316b7 2a 28 2a 73 71 6c 29 28 73 71 6c 69 74 65 33 5f  *(*sql)(sqlite3_
316b8 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  stmt*);.  int (*
316b9 73 74 61 74 75 73 29 28 69 6e 74 2c 69 6e 74 2a  status)(int,int*
316ba 2c 69 6e 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e  ,int*,int);.  in
316bb 74 20 28 2a 62 61 63 6b 75 70 5f 66 69 6e 69 73  t (*backup_finis
316bc 68 29 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  h)(sqlite3_backu
316bd 70 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62  p*);.  sqlite3_b
316be 61 63 6b 75 70 20 2a 28 2a 62 61 63 6b 75 70 5f  ackup *(*backup_
316bf 69 6e 69 74 29 28 73 71 6c 69 74 65 33 2a 2c 63  init)(sqlite3*,c
316c0 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74  onst char*,sqlit
316c1 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  e3*,const char*)
316c2 3b 0a 20 20 69 6e 74 20 28 2a 62 61 63 6b 75 70  ;.  int (*backup
316c3 5f 70 61 67 65 63 6f 75 6e 74 29 28 73 71 6c 69  _pagecount)(sqli
316c4 74 65 33 5f 62 61 63 6b 75 70 2a 29 3b 0a 20 20  te3_backup*);.  
316c5 69 6e 74 20 28 2a 62 61 63 6b 75 70 5f 72 65 6d  int (*backup_rem
316c6 61 69 6e 69 6e 67 29 28 73 71 6c 69 74 65 33 5f  aining)(sqlite3_
316c7 62 61 63 6b 75 70 2a 29 3b 0a 20 20 69 6e 74 20  backup*);.  int 
316c8 28 2a 62 61 63 6b 75 70 5f 73 74 65 70 29 28 73  (*backup_step)(s
316c9 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2a 2c 69  qlite3_backup*,i
316ca 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  nt);.  const cha
316cb 72 20 2a 28 2a 63 6f 6d 70 69 6c 65 6f 70 74 69  r *(*compileopti
316cc 6f 6e 5f 67 65 74 29 28 69 6e 74 29 3b 0a 20 20  on_get)(int);.  
316cd 69 6e 74 20 28 2a 63 6f 6d 70 69 6c 65 6f 70 74  int (*compileopt
316ce 69 6f 6e 5f 75 73 65 64 29 28 63 6f 6e 73 74 20  ion_used)(const 
316cf 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  char*);.  int (*
316d0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 5f  create_function_
316d1 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  v2)(sqlite3*,con
316d2 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74  st char*,int,int
316d3 2c 76 6f 69 64 2a 2c 0a 20 20 20 20 20 20 20 20  ,void*,.        
316d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
316d5 20 20 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63      void (*xFunc
316d6 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
316d7 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
316d8 61 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20  alue**),.       
316d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
316da 20 20 20 20 20 76 6f 69 64 20 28 2a 78 53 74 65       void (*xSte
316db 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  p)(sqlite3_conte
316dc 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
316dd 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20  value**),.      
316de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
316df 20 20 20 20 20 20 76 6f 69 64 20 28 2a 78 46 69        void (*xFi
316e0 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  nal)(sqlite3_con
316e1 74 65 78 74 2a 29 2c 0a 20 20 20 20 20 20 20 20  text*),.        
316e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
316e3 20 20 20 20 76 6f 69 64 28 2a 78 44 65 73 74 72      void(*xDestr
316e4 6f 79 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69  oy)(void*));.  i
316e5 6e 74 20 28 2a 64 62 5f 63 6f 6e 66 69 67 29 28  nt (*db_config)(
316e6 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 2e 2e 2e  sqlite3*,int,...
316e7 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
316e8 65 78 20 2a 28 2a 64 62 5f 6d 75 74 65 78 29 28  ex *(*db_mutex)(
316e9 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e 74  sqlite3*);.  int
316ea 20 28 2a 64 62 5f 73 74 61 74 75 73 29 28 73 71   (*db_status)(sq
316eb 6c 69 74 65 33 2a 2c 69 6e 74 2c 69 6e 74 2a 2c  lite3*,int,int*,
316ec 69 6e 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  int*,int);.  int
316ed 20 28 2a 65 78 74 65 6e 64 65 64 5f 65 72 72 63   (*extended_errc
316ee 6f 64 65 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a  ode)(sqlite3*);.
316ef 20 20 76 6f 69 64 20 28 2a 6c 6f 67 29 28 69 6e    void (*log)(in
316f0 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e  t,const char*,..
316f1 2e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  .);.  sqlite3_in
316f2 74 36 34 20 28 2a 73 6f 66 74 5f 68 65 61 70 5f  t64 (*soft_heap_
316f3 6c 69 6d 69 74 36 34 29 28 73 71 6c 69 74 65 33  limit64)(sqlite3
316f4 5f 69 6e 74 36 34 29 3b 0a 20 20 63 6f 6e 73 74  _int64);.  const
316f5 20 63 68 61 72 20 2a 28 2a 73 6f 75 72 63 65 69   char *(*sourcei
316f6 64 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20  d)(void);.  int 
316f7 28 2a 73 74 6d 74 5f 73 74 61 74 75 73 29 28 73  (*stmt_status)(s
316f8 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
316f9 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 73  ,int);.  int (*s
316fa 74 72 6e 69 63 6d 70 29 28 63 6f 6e 73 74 20 63  trnicmp)(const c
316fb 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
316fc 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 75  ,int);.  int (*u
316fd 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 29 28 73 71  nlock_notify)(sq
316fe 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76  lite3*,void(*)(v
316ff 6f 69 64 2a 2a 2c 69 6e 74 29 2c 76 6f 69 64 2a  oid**,int),void*
31700 29 3b 0a 20 20 69 6e 74 20 28 2a 77 61 6c 5f 61  );.  int (*wal_a
31701 75 74 6f 63 68 65 63 6b 70 6f 69 6e 74 29 28 73  utocheckpoint)(s
31702 71 6c 69 74 65 33 2a 2c 69 6e 74 29 3b 0a 20 20  qlite3*,int);.  
31703 69 6e 74 20 28 2a 77 61 6c 5f 63 68 65 63 6b 70  int (*wal_checkp
31704 6f 69 6e 74 29 28 73 71 6c 69 74 65 33 2a 2c 63  oint)(sqlite3*,c
31705 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 76  onst char*);.  v
31706 6f 69 64 20 2a 28 2a 77 61 6c 5f 68 6f 6f 6b 29  oid *(*wal_hook)
31707 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a 29  (sqlite3*,int(*)
31708 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c  (void*,sqlite3*,
31709 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29  const char*,int)
3170a 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 28  ,void*);.  int (
3170b 2a 62 6c 6f 62 5f 72 65 6f 70 65 6e 29 28 73 71  *blob_reopen)(sq
3170c 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c 73 71 6c 69  lite3_blob*,sqli
3170d 74 65 33 5f 69 6e 74 36 34 29 3b 0a 20 20 69 6e  te3_int64);.  in
3170e 74 20 28 2a 76 74 61 62 5f 63 6f 6e 66 69 67 29  t (*vtab_config)
3170f 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 6f 70  (sqlite3*,int op
31710 2c 2e 2e 2e 29 3b 0a 20 20 69 6e 74 20 28 2a 76  ,...);.  int (*v
31711 74 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63 74 29  tab_on_conflict)
31712 28 73 71 6c 69 74 65 33 2a 29 3b 0a 7d 3b 0a 0a  (sqlite3*);.};..
31713 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
31714 69 6e 67 20 6d 61 63 72 6f 73 20 72 65 64 65 66  ing macros redef
31715 69 6e 65 20 74 68 65 20 41 50 49 20 72 6f 75 74  ine the API rout
31716 69 6e 65 73 20 73 6f 20 74 68 61 74 20 74 68 65  ines so that the
31717 79 20 61 72 65 0a 2a 2a 20 72 65 64 69 72 65 63  y are.** redirec
31718 74 65 64 20 74 68 72 6f 75 67 68 74 20 74 68 65  ted throught the
31719 20 67 6c 6f 62 61 6c 20 73 71 6c 69 74 65 33 5f   global sqlite3_
3171a 61 70 69 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  api structure..*
3171b 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
3171c 20 66 69 6c 65 20 69 73 20 61 6c 73 6f 20 75 73   file is also us
3171d 65 64 20 62 79 20 74 68 65 20 6c 6f 61 64 65 78  ed by the loadex
3171e 74 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 0a  t.c source file.
3171f 2a 2a 20 28 70 61 72 74 20 6f 66 20 74 68 65 20  ** (part of the 
31720 6d 61 69 6e 20 53 51 4c 69 74 65 20 6c 69 62 72  main SQLite libr
31721 61 72 79 20 2d 20 6e 6f 74 20 61 6e 20 65 78 74  ary - not an ext
31722 65 6e 73 69 6f 6e 29 20 73 6f 20 74 68 61 74 0a  ension) so that.
31723 2a 2a 20 69 74 20 63 61 6e 20 67 65 74 20 61 63  ** it can get ac
31724 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69  cess to the sqli
31725 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
31726 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 65   structure.** de
31727 66 69 6e 69 74 69 6f 6e 2e 20 20 42 75 74 20 74  finition.  But t
31728 68 65 20 6d 61 69 6e 20 6c 69 62 72 61 72 79 20  he main library 
31729 64 6f 65 73 20 6e 6f 74 20 77 61 6e 74 20 74 6f  does not want to
3172a 20 72 65 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65   redefine.** the
3172b 20 41 50 49 2e 20 20 53 6f 20 74 68 65 20 72 65   API.  So the re
3172c 64 65 66 69 6e 69 74 69 6f 6e 20 6d 61 63 72 6f  definition macro
3172d 73 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69 64  s are only valid
3172e 20 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54   if the.** SQLIT
3172f 45 5f 43 4f 52 45 20 6d 61 63 72 6f 73 20 69 73  E_CORE macros is
31730 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23   undefined..*/.#
31731 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f  ifndef SQLITE_CO
31732 52 45 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  RE.#define sqlit
31733 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
31734 74 65 78 74 20 20 20 20 20 20 73 71 6c 69 74 65  text      sqlite
31735 33 5f 61 70 69 2d 3e 61 67 67 72 65 67 61 74 65  3_api->aggregate
31736 5f 63 6f 6e 74 65 78 74 0a 23 69 66 6e 64 65 66  _context.#ifndef
31737 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50   SQLITE_OMIT_DEP
31738 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20  RECATED.#define 
31739 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
3173a 65 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20 73  e_count        s
3173b 71 6c 69 74 65 33 5f 61 70 69 2d 3e 61 67 67 72  qlite3_api->aggr
3173c 65 67 61 74 65 5f 63 6f 75 6e 74 0a 23 65 6e 64  egate_count.#end
3173d 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  if.#define sqlit
3173e 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 20 20 20  e3_bind_blob    
3173f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
31740 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 62 6c 6f 62  3_api->bind_blob
31741 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
31742 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 20 20 20 20  _bind_double    
31743 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
31744 61 70 69 2d 3e 62 69 6e 64 5f 64 6f 75 62 6c 65  api->bind_double
31745 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
31746 5f 62 69 6e 64 5f 69 6e 74 20 20 20 20 20 20 20  _bind_int       
31747 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
31748 61 70 69 2d 3e 62 69 6e 64 5f 69 6e 74 0a 23 64  api->bind_int.#d
31749 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
3174a 6e 64 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20  nd_int64        
3174b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
3174c 2d 3e 62 69 6e 64 5f 69 6e 74 36 34 0a 23 64 65  ->bind_int64.#de
3174d 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
3174e 64 5f 6e 75 6c 6c 20 20 20 20 20 20 20 20 20 20  d_null          
3174f 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
31750 3e 62 69 6e 64 5f 6e 75 6c 6c 0a 23 64 65 66 69  >bind_null.#defi
31751 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
31752 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 20  parameter_count 
31753 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
31754 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
31755 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  unt.#define sqli
31756 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
31757 65 72 5f 69 6e 64 65 78 20 20 20 73 71 6c 69 74  er_index   sqlit
31758 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72  e3_api->bind_par
31759 61 6d 65 74 65 72 5f 69 6e 64 65 78 0a 23 64 65  ameter_index.#de
3175a 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
3175b 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65  d_parameter_name
3175c 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
3175d 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  >bind_parameter_
3175e 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  name.#define sql
3175f 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20 20  ite3_bind_text  
31760 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
31761 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65  te3_api->bind_te
31762 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  xt.#define sqlit
31763 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 20 20  e3_bind_text16  
31764 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
31765 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65 78 74  3_api->bind_text
31766 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
31767 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 20 20 20  e3_bind_value   
31768 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
31769 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 76 61 6c 75  3_api->bind_valu
3176a 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
3176b 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 20 20  3_busy_handler  
3176c 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
3176d 5f 61 70 69 2d 3e 62 75 73 79 5f 68 61 6e 64 6c  _api->busy_handl
3176e 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  er.#define sqlit
3176f 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 20  e3_busy_timeout 
31770 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
31771 33 5f 61 70 69 2d 3e 62 75 73 79 5f 74 69 6d 65  3_api->busy_time
31772 6f 75 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  out.#define sqli
31773 74 65 33 5f 63 68 61 6e 67 65 73 20 20 20 20 20  te3_changes     
31774 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
31775 65 33 5f 61 70 69 2d 3e 63 68 61 6e 67 65 73 0a  e3_api->changes.
31776 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
31777 63 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20  close           
31778 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
31779 70 69 2d 3e 63 6c 6f 73 65 0a 23 64 65 66 69 6e  pi->close.#defin
3177a 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  e sqlite3_collat
3177b 69 6f 6e 5f 6e 65 65 64 65 64 20 20 20 20 20 20  ion_needed      
3177c 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
3177d 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 0a 23  llation_needed.#
3177e 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
3177f 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
31780 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  6     sqlite3_ap
31781 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  i->collation_nee
31782 64 65 64 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ded16.#define sq
31783 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
31784 62 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  b            sql
31785 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
31786 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71  _blob.#define sq
31787 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
31788 65 73 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  es           sql
31789 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
3178a 5f 62 79 74 65 73 0a 23 64 65 66 69 6e 65 20 73  _bytes.#define s
3178b 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
3178c 74 65 73 31 36 20 20 20 20 20 20 20 20 20 73 71  tes16         sq
3178d 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
3178e 6e 5f 62 79 74 65 73 31 36 0a 23 64 65 66 69 6e  n_bytes16.#defin
3178f 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
31790 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20  _count          
31791 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
31792 6c 75 6d 6e 5f 63 6f 75 6e 74 0a 23 64 65 66 69  lumn_count.#defi
31793 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
31794 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20  n_database_name 
31795 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
31796 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
31797 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
31798 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
31799 61 73 65 5f 6e 61 6d 65 31 36 20 73 71 6c 69 74  ase_name16 sqlit
3179a 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64  e3_api->column_d
3179b 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 0a 23  atabase_name16.#
3179c 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
3179d 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 20 20  olumn_decltype  
3179e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
3179f 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  i->column_declty
317a0 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  pe.#define sqlit
317a1 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
317a2 70 65 31 36 20 20 20 20 20 20 73 71 6c 69 74 65  pe16      sqlite
317a3 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65  3_api->column_de
317a4 63 6c 74 79 70 65 31 36 0a 23 64 65 66 69 6e 65  cltype16.#define
317a5 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
317a6 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20 20  double          
317a7 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c  sqlite3_api->col
317a8 75 6d 6e 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69  umn_double.#defi
317a9 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
317aa 6e 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20  n_int           
317ab 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
317ac 6f 6c 75 6d 6e 5f 69 6e 74 0a 23 64 65 66 69 6e  olumn_int.#defin
317ad 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
317ae 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20 20 20  _int64          
317af 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
317b0 6c 75 6d 6e 5f 69 6e 74 36 34 0a 23 64 65 66 69  lumn_int64.#defi
317b1 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
317b2 6e 5f 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20  n_name          
317b3 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
317b4 6f 6c 75 6d 6e 5f 6e 61 6d 65 0a 23 64 65 66 69  olumn_name.#defi
317b5 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
317b6 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20 20 20 20  n_name16        
317b7 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
317b8 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 0a 23 64 65  olumn_name16.#de
317b9 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
317ba 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 20  umn_origin_name 
317bb 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
317bc 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  >column_origin_n
317bd 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
317be 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  te3_column_origi
317bf 6e 5f 6e 61 6d 65 31 36 20 20 20 73 71 6c 69 74  n_name16   sqlit
317c0 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6f  e3_api->column_o
317c1 72 69 67 69 6e 5f 6e 61 6d 65 31 36 0a 23 64 65  rigin_name16.#de
317c2 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
317c3 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 20  umn_table_name  
317c4 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
317c5 3e 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  >column_table_na
317c6 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  me.#define sqlit
317c7 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
317c8 6e 61 6d 65 31 36 20 20 20 20 73 71 6c 69 74 65  name16    sqlite
317c9 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 61  3_api->column_ta
317ca 62 6c 65 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69  ble_name16.#defi
317cb 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
317cc 6e 5f 74 65 78 74 20 20 20 20 20 20 20 20 20 20  n_text          
317cd 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
317ce 6f 6c 75 6d 6e 5f 74 65 78 74 0a 23 64 65 66 69  olumn_text.#defi
317cf 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
317d0 6e 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20  n_text16        
317d1 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
317d2 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 0a 23 64 65  olumn_text16.#de
317d3 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
317d4 75 6d 6e 5f 74 79 70 65 20 20 20 20 20 20 20 20  umn_type        
317d5 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
317d6 3e 63 6f 6c 75 6d 6e 5f 74 79 70 65 0a 23 64 65  >column_type.#de
317d7 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
317d8 75 6d 6e 5f 76 61 6c 75 65 20 20 20 20 20 20 20  umn_value       
317d9 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
317da 3e 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 0a 23 64  >column_value.#d
317db 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
317dc 6d 6d 69 74 5f 68 6f 6f 6b 20 20 20 20 20 20 20  mmit_hook       
317dd 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
317de 2d 3e 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 0a 23 64  ->commit_hook.#d
317df 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
317e0 6d 70 6c 65 74 65 20 20 20 20 20 20 20 20 20 20  mplete          
317e1 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
317e2 2d 3e 63 6f 6d 70 6c 65 74 65 0a 23 64 65 66 69  ->complete.#defi
317e3 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  ne sqlite3_compl
317e4 65 74 65 31 36 20 20 20 20 20 20 20 20 20 20 20  ete16           
317e5 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
317e6 6f 6d 70 6c 65 74 65 31 36 0a 23 64 65 66 69 6e  omplete16.#defin
317e7 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
317e8 5f 63 6f 6c 6c 61 74 69 6f 6e 20 20 20 20 20 20  _collation      
317e9 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
317ea 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 0a 23  eate_collation.#
317eb 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
317ec 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
317ed 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  6     sqlite3_ap
317ee 69 2d 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  i->create_collat
317ef 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ion16.#define sq
317f0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
317f1 63 74 69 6f 6e 20 20 20 20 20 20 20 20 73 71 6c  ction        sql
317f2 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65  ite3_api->create
317f3 5f 66 75 6e 63 74 69 6f 6e 0a 23 64 65 66 69 6e  _function.#defin
317f4 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
317f5 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20 20 20 20  _function16     
317f6 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
317f7 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 0a  eate_function16.
317f8 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
317f9 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 20 20 20  create_module   
317fa 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
317fb 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64 75 6c  pi->create_modul
317fc 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
317fd 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
317fe 76 32 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  v2       sqlite3
317ff 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64  _api->create_mod
31800 75 6c 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73  ule_v2.#define s
31801 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e  qlite3_data_coun
31802 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  t             sq
31803 6c 69 74 65 33 5f 61 70 69 2d 3e 64 61 74 61 5f  lite3_api->data_
31804 63 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71  count.#define sq
31805 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20  lite3_db_handle 
31806 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
31807 69 74 65 33 5f 61 70 69 2d 3e 64 62 5f 68 61 6e  ite3_api->db_han
31808 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  dle.#define sqli
31809 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
3180a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
3180b 65 33 5f 61 70 69 2d 3e 64 65 63 6c 61 72 65 5f  e3_api->declare_
3180c 76 74 61 62 0a 23 64 65 66 69 6e 65 20 73 71 6c  vtab.#define sql
3180d 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
3180e 65 64 5f 63 61 63 68 65 20 20 20 20 73 71 6c 69  ed_cache    sqli
3180f 74 65 33 5f 61 70 69 2d 3e 65 6e 61 62 6c 65 5f  te3_api->enable_
31810 73 68 61 72 65 64 5f 63 61 63 68 65 0a 23 64 65  shared_cache.#de
31811 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72  fine sqlite3_err
31812 63 6f 64 65 20 20 20 20 20 20 20 20 20 20 20 20  code            
31813 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
31814 3e 65 72 72 63 6f 64 65 0a 23 64 65 66 69 6e 65  >errcode.#define
31815 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 20   sqlite3_errmsg 
31816 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31817 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 72 72  sqlite3_api->err
31818 6d 73 67 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  msg.#define sqli
31819 74 65 33 5f 65 72 72 6d 73 67 31 36 20 20 20 20  te3_errmsg16    
3181a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
3181b 65 33 5f 61 70 69 2d 3e 65 72 72 6d 73 67 31 36  e3_api->errmsg16
3181c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
3181d 5f 65 78 65 63 20 20 20 20 20 20 20 20 20 20 20  _exec           
3181e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
3181f 61 70 69 2d 3e 65 78 65 63 0a 23 69 66 6e 64 65  api->exec.#ifnde
31820 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
31821 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65  PRECATED.#define
31822 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64   sqlite3_expired
31823 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31824 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78 70  sqlite3_api->exp
31825 69 72 65 64 0a 23 65 6e 64 69 66 0a 23 64 65 66  ired.#endif.#def
31826 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  ine sqlite3_fina
31827 6c 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20  lize            
31828 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
31829 66 69 6e 61 6c 69 7a 65 0a 23 64 65 66 69 6e 65  finalize.#define
3182a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 20 20   sqlite3_free   
3182b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3182c 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66 72 65  sqlite3_api->fre
3182d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
3182e 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 20 20 20  3_free_table    
3182f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
31830 5f 61 70 69 2d 3e 66 72 65 65 5f 74 61 62 6c 65  _api->free_table
31831 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
31832 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 20  _get_autocommit 
31833 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
31834 61 70 69 2d 3e 67 65 74 5f 61 75 74 6f 63 6f 6d  api->get_autocom
31835 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  mit.#define sqli
31836 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 20  te3_get_auxdata 
31837 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
31838 65 33 5f 61 70 69 2d 3e 67 65 74 5f 61 75 78 64  e3_api->get_auxd
31839 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ata.#define sqli
3183a 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 20 20  te3_get_table   
3183b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
3183c 65 33 5f 61 70 69 2d 3e 67 65 74 5f 74 61 62 6c  e3_api->get_tabl
3183d 65 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  e.#ifndef SQLITE
3183e 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
3183f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
31840 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 20  _global_recover 
31841 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
31842 61 70 69 2d 3e 67 6c 6f 62 61 6c 5f 72 65 63 6f  api->global_reco
31843 76 65 72 0a 23 65 6e 64 69 66 0a 23 64 65 66 69  ver.#endif.#defi
31844 6e 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  ne sqlite3_inter
31845 72 75 70 74 20 20 20 20 20 20 20 20 20 20 20 20  rupt            
31846 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 69    sqlite3_api->i
31847 6e 74 65 72 72 75 70 74 78 0a 23 64 65 66 69 6e  nterruptx.#defin
31848 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  e sqlite3_last_i
31849 6e 73 65 72 74 5f 72 6f 77 69 64 20 20 20 20 20  nsert_rowid     
3184a 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 61   sqlite3_api->la
3184b 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 0a  st_insert_rowid.
3184c 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
3184d 6c 69 62 76 65 72 73 69 6f 6e 20 20 20 20 20 20  libversion      
3184e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
3184f 70 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 0a 23  pi->libversion.#
31850 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c  define sqlite3_l
31851 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
31852 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
31853 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  i->libversion_nu
31854 6d 62 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c  mber.#define sql
31855 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 20 20 20 20  ite3_malloc     
31856 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
31857 74 65 33 5f 61 70 69 2d 3e 6d 61 6c 6c 6f 63 0a  te3_api->malloc.
31858 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
31859 6d 70 72 69 6e 74 66 20 20 20 20 20 20 20 20 20  mprintf         
3185a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
3185b 70 69 2d 3e 6d 70 72 69 6e 74 66 0a 23 64 65 66  pi->mprintf.#def
3185c 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ine sqlite3_open
3185d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3185e 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
3185f 6f 70 65 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c  open.#define sql
31860 69 74 65 33 5f 6f 70 65 6e 31 36 20 20 20 20 20  ite3_open16     
31861 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
31862 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 31 36 0a  te3_api->open16.
31863 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
31864 70 72 65 70 61 72 65 20 20 20 20 20 20 20 20 20  prepare         
31865 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
31866 70 69 2d 3e 70 72 65 70 61 72 65 0a 23 64 65 66  pi->prepare.#def
31867 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ine sqlite3_prep
31868 61 72 65 31 36 20 20 20 20 20 20 20 20 20 20 20  are16           
31869 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
3186a 70 72 65 70 61 72 65 31 36 0a 23 64 65 66 69 6e  prepare16.#defin
3186b 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
3186c 65 5f 76 32 20 20 20 20 20 20 20 20 20 20 20 20  e_v2            
3186d 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72   sqlite3_api->pr
3186e 65 70 61 72 65 5f 76 32 0a 23 64 65 66 69 6e 65  epare_v2.#define
3186f 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
31870 31 36 5f 76 32 20 20 20 20 20 20 20 20 20 20 20  16_v2           
31871 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65  sqlite3_api->pre
31872 70 61 72 65 31 36 5f 76 32 0a 23 64 65 66 69 6e  pare16_v2.#defin
31873 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c  e sqlite3_profil
31874 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
31875 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72   sqlite3_api->pr
31876 6f 66 69 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  ofile.#define sq
31877 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68  lite3_progress_h
31878 61 6e 64 6c 65 72 20 20 20 20 20 20 20 73 71 6c  andler       sql
31879 69 74 65 33 5f 61 70 69 2d 3e 70 72 6f 67 72 65  ite3_api->progre
3187a 73 73 5f 68 61 6e 64 6c 65 72 0a 23 64 65 66 69  ss_handler.#defi
3187b 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  ne sqlite3_reall
3187c 6f 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  oc              
3187d 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
3187e 65 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73  ealloc.#define s
3187f 71 6c 69 74 65 33 5f 72 65 73 65 74 20 20 20 20  qlite3_reset    
31880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
31881 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 65 74  lite3_api->reset
31882 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
31883 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 20 20 20  _result_blob    
31884 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
31885 61 70 69 2d 3e 72 65 73 75 6c 74 5f 62 6c 6f 62  api->result_blob
31886 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
31887 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20 20  _result_double  
31888 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
31889 61 70 69 2d 3e 72 65 73 75 6c 74 5f 64 6f 75 62  api->result_doub
3188a 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
3188b 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 20  e3_result_error 
3188c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3188d 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72  3_api->result_er
3188e 72 6f 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ror.#define sqli
3188f 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
31890 31 36 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  16         sqlit
31891 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65  e3_api->result_e
31892 72 72 6f 72 31 36 0a 23 64 65 66 69 6e 65 20 73  rror16.#define s
31893 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
31894 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  t             sq
31895 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
31896 74 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73 71  t_int.#define sq
31897 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
31898 36 34 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  64           sql
31899 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
3189a 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73  _int64.#define s
3189b 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
3189c 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ll            sq
3189d 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
3189e 74 5f 6e 75 6c 6c 0a 23 64 65 66 69 6e 65 20 73  t_null.#define s
3189f 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
318a0 78 74 20 20 20 20 20 20 20 20 20 20 20 20 73 71  xt            sq
318a1 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
318a2 74 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73  t_text.#define s
318a3 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
318a4 78 74 31 36 20 20 20 20 20 20 20 20 20 20 73 71  xt16          sq
318a5 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
318a6 74 5f 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65  t_text16.#define
318a7 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
318a8 74 65 78 74 31 36 62 65 20 20 20 20 20 20 20 20  text16be        
318a9 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
318aa 75 6c 74 5f 74 65 78 74 31 36 62 65 0a 23 64 65  ult_text16be.#de
318ab 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
318ac 75 6c 74 5f 74 65 78 74 31 36 6c 65 20 20 20 20  ult_text16le    
318ad 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
318ae 3e 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  >result_text16le
318af 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
318b0 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 20 20 20  _result_value   
318b1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
318b2 61 70 69 2d 3e 72 65 73 75 6c 74 5f 76 61 6c 75  api->result_valu
318b3 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
318b4 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 20  3_rollback_hook 
318b5 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
318b6 5f 61 70 69 2d 3e 72 6f 6c 6c 62 61 63 6b 5f 68  _api->rollback_h
318b7 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ook.#define sqli
318b8 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
318b9 65 72 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  er         sqlit
318ba 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61 75 74 68  e3_api->set_auth
318bb 6f 72 69 7a 65 72 0a 23 64 65 66 69 6e 65 20 73  orizer.#define s
318bc 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
318bd 74 61 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ta            sq
318be 6c 69 74 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61  lite3_api->set_a
318bf 75 78 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73  uxdata.#define s
318c0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 20  qlite3_snprintf 
318c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
318c2 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6e 70 72 69  lite3_api->snpri
318c3 6e 74 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ntf.#define sqli
318c4 74 65 33 5f 73 74 65 70 20 20 20 20 20 20 20 20  te3_step        
318c5 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
318c6 65 33 5f 61 70 69 2d 3e 73 74 65 70 0a 23 64 65  e3_api->step.#de
318c7 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 61 62  fine sqlite3_tab
318c8 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61  le_column_metada
318c9 74 61 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  ta  sqlite3_api-
318ca 3e 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65  >table_column_me
318cb 74 61 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73  tadata.#define s
318cc 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c  qlite3_thread_cl
318cd 65 61 6e 75 70 20 20 20 20 20 20 20 20 20 73 71  eanup         sq
318ce 6c 69 74 65 33 5f 61 70 69 2d 3e 74 68 72 65 61  lite3_api->threa
318cf 64 5f 63 6c 65 61 6e 75 70 0a 23 64 65 66 69 6e  d_cleanup.#defin
318d0 65 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f  e sqlite3_total_
318d1 63 68 61 6e 67 65 73 20 20 20 20 20 20 20 20 20  changes         
318d2 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 6f   sqlite3_api->to
318d3 74 61 6c 5f 63 68 61 6e 67 65 73 0a 23 64 65 66  tal_changes.#def
318d4 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63  ine sqlite3_trac
318d5 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
318d6 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
318d7 74 72 61 63 65 0a 23 69 66 6e 64 65 66 20 53 51  trace.#ifndef SQ
318d8 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
318d9 41 54 45 44 0a 23 64 65 66 69 6e 65 20 73 71 6c  ATED.#define sql
318da 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69  ite3_transfer_bi
318db 6e 64 69 6e 67 73 20 20 20 20 20 20 73 71 6c 69  ndings      sqli
318dc 74 65 33 5f 61 70 69 2d 3e 74 72 61 6e 73 66 65  te3_api->transfe
318dd 72 5f 62 69 6e 64 69 6e 67 73 0a 23 65 6e 64 69  r_bindings.#endi
318de 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  f.#define sqlite
318df 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 20 20 20  3_update_hook   
318e0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
318e1 5f 61 70 69 2d 3e 75 70 64 61 74 65 5f 68 6f 6f  _api->update_hoo
318e2 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  k.#define sqlite
318e3 33 5f 75 73 65 72 5f 64 61 74 61 20 20 20 20 20  3_user_data     
318e4 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
318e5 5f 61 70 69 2d 3e 75 73 65 72 5f 64 61 74 61 0a  _api->user_data.
318e6 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
318e7 76 61 6c 75 65 5f 62 6c 6f 62 20 20 20 20 20 20  value_blob      
318e8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
318e9 70 69 2d 3e 76 61 6c 75 65 5f 62 6c 6f 62 0a 23  pi->value_blob.#
318ea 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
318eb 61 6c 75 65 5f 62 79 74 65 73 20 20 20 20 20 20  alue_bytes      
318ec 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
318ed 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 0a 23  i->value_bytes.#
318ee 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
318ef 61 6c 75 65 5f 62 79 74 65 73 31 36 20 20 20 20  alue_bytes16    
318f0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
318f1 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 31 36  i->value_bytes16
318f2 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
318f3 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 20 20  _value_double   
318f4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
318f5 61 70 69 2d 3e 76 61 6c 75 65 5f 64 6f 75 62 6c  api->value_doubl
318f6 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
318f7 33 5f 76 61 6c 75 65 5f 69 6e 74 20 20 20 20 20  3_value_int     
318f8 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
318f9 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 0a  _api->value_int.
318fa 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
318fb 76 61 6c 75 65 5f 69 6e 74 36 34 20 20 20 20 20  value_int64     
318fc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
318fd 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 36 34 0a  pi->value_int64.
318fe 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
318ff 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79  value_numeric_ty
31900 70 65 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  pe     sqlite3_a
31901 70 69 2d 3e 76 61 6c 75 65 5f 6e 75 6d 65 72 69  pi->value_numeri
31902 63 5f 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73  c_type.#define s
31903 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
31904 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  t             sq
31905 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
31906 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71  _text.#define sq
31907 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
31908 31 36 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  16           sql
31909 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
3190a 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73  text16.#define s
3190b 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
3190c 74 31 36 62 65 20 20 20 20 20 20 20 20 20 73 71  t16be         sq
3190d 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
3190e 5f 74 65 78 74 31 36 62 65 0a 23 64 65 66 69 6e  _text16be.#defin
3190f 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
31910 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20  text16le        
31911 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61   sqlite3_api->va
31912 6c 75 65 5f 74 65 78 74 31 36 6c 65 0a 23 64 65  lue_text16le.#de
31913 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
31914 75 65 5f 74 79 70 65 20 20 20 20 20 20 20 20 20  ue_type         
31915 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
31916 3e 76 61 6c 75 65 5f 74 79 70 65 0a 23 64 65 66  >value_type.#def
31917 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 6d 70 72  ine sqlite3_vmpr
31918 69 6e 74 66 20 20 20 20 20 20 20 20 20 20 20 20  intf            
31919 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
3191a 76 6d 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65  vmprintf.#define
3191b 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61   sqlite3_overloa
3191c 64 5f 66 75 6e 63 74 69 6f 6e 20 20 20 20 20 20  d_function      
3191d 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 76 65  sqlite3_api->ove
3191e 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 0a 23  rload_function.#
3191f 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
31920 72 65 70 61 72 65 5f 76 32 20 20 20 20 20 20 20  repare_v2       
31921 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
31922 69 2d 3e 70 72 65 70 61 72 65 5f 76 32 0a 23 64  i->prepare_v2.#d
31923 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
31924 65 70 61 72 65 31 36 5f 76 32 20 20 20 20 20 20  epare16_v2      
31925 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
31926 2d 3e 70 72 65 70 61 72 65 31 36 5f 76 32 0a 23  ->prepare16_v2.#
31927 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
31928 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 20 20 20  lear_bindings   
31929 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
3192a 69 2d 3e 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67  i->clear_binding
3192b 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  s.#define sqlite
3192c 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 20  3_bind_zeroblob 
3192d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
3192e 5f 61 70 69 2d 3e 62 69 6e 64 5f 7a 65 72 6f 62  _api->bind_zerob
3192f 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  lob.#define sqli
31930 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20 20  te3_blob_bytes  
31931 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
31932 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 62 79 74  e3_api->blob_byt
31933 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  es.#define sqlit
31934 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20 20  e3_blob_close   
31935 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
31936 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 63 6c 6f 73  3_api->blob_clos
31937 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
31938 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20 20 20 20  3_blob_open     
31939 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
3193a 5f 61 70 69 2d 3e 62 6c 6f 62 5f 6f 70 65 6e 0a  _api->blob_open.
3193b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
3193c 62 6c 6f 62 5f 72 65 61 64 20 20 20 20 20 20 20  blob_read       
3193d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
3193e 70 69 2d 3e 62 6c 6f 62 5f 72 65 61 64 0a 23 64  pi->blob_read.#d
3193f 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c  efine sqlite3_bl
31940 6f 62 5f 77 72 69 74 65 20 20 20 20 20 20 20 20  ob_write        
31941 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
31942 2d 3e 62 6c 6f 62 5f 77 72 69 74 65 0a 23 64 65  ->blob_write.#de
31943 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65  fine sqlite3_cre
31944 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
31945 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
31946 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  >create_collatio
31947 6e 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c  n_v2.#define sql
31948 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
31949 6c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  l           sqli
3194a 74 65 33 5f 61 70 69 2d 3e 66 69 6c 65 5f 63 6f  te3_api->file_co
3194b 6e 74 72 6f 6c 0a 23 64 65 66 69 6e 65 20 73 71  ntrol.#define sq
3194c 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67  lite3_memory_hig
3194d 68 77 61 74 65 72 20 20 20 20 20 20 20 73 71 6c  hwater       sql
3194e 69 74 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79  ite3_api->memory
3194f 5f 68 69 67 68 77 61 74 65 72 0a 23 64 65 66 69  _highwater.#defi
31950 6e 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  ne sqlite3_memor
31951 79 5f 75 73 65 64 20 20 20 20 20 20 20 20 20 20  y_used          
31952 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
31953 65 6d 6f 72 79 5f 75 73 65 64 0a 23 64 65 66 69  emory_used.#defi
31954 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
31955 5f 61 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20  _alloc          
31956 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
31957 75 74 65 78 5f 61 6c 6c 6f 63 0a 23 64 65 66 69  utex_alloc.#defi
31958 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
31959 5f 65 6e 74 65 72 20 20 20 20 20 20 20 20 20 20  _enter          
3195a 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
3195b 75 74 65 78 5f 65 6e 74 65 72 0a 23 64 65 66 69  utex_enter.#defi
3195c 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
3195d 5f 66 72 65 65 20 20 20 20 20 20 20 20 20 20 20  _free           
3195e 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
3195f 75 74 65 78 5f 66 72 65 65 0a 23 64 65 66 69 6e  utex_free.#defin
31960 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
31961 6c 65 61 76 65 20 20 20 20 20 20 20 20 20 20 20  leave           
31962 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75   sqlite3_api->mu
31963 74 65 78 5f 6c 65 61 76 65 0a 23 64 65 66 69 6e  tex_leave.#defin
31964 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
31965 74 72 79 20 20 20 20 20 20 20 20 20 20 20 20 20  try             
31966 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75   sqlite3_api->mu
31967 74 65 78 5f 74 72 79 0a 23 64 65 66 69 6e 65 20  tex_try.#define 
31968 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 20  sqlite3_open_v2 
31969 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
3196a 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e  qlite3_api->open
3196b 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  _v2.#define sqli
3196c 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
3196d 72 79 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  ry         sqlit
3196e 65 33 5f 61 70 69 2d 3e 72 65 6c 65 61 73 65 5f  e3_api->release_
3196f 6d 65 6d 6f 72 79 0a 23 64 65 66 69 6e 65 20 73  memory.#define s
31970 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
31971 72 6f 72 5f 6e 6f 6d 65 6d 20 20 20 20 20 73 71  ror_nomem     sq
31972 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
31973 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 0a 23 64  t_error_nomem.#d
31974 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
31975 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69  sult_error_toobi
31976 67 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  g    sqlite3_api
31977 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  ->result_error_t
31978 6f 6f 62 69 67 0a 23 64 65 66 69 6e 65 20 73 71  oobig.#define sq
31979 6c 69 74 65 33 5f 73 6c 65 65 70 20 20 20 20 20  lite3_sleep     
3197a 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
3197b 69 74 65 33 5f 61 70 69 2d 3e 73 6c 65 65 70 0a  ite3_api->sleep.
3197c 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
3197d 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 20  soft_heap_limit 
3197e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
3197f 70 69 2d 3e 73 6f 66 74 5f 68 65 61 70 5f 6c 69  pi->soft_heap_li
31980 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  mit.#define sqli
31981 74 65 33 5f 76 66 73 5f 66 69 6e 64 20 20 20 20  te3_vfs_find    
31982 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
31983 65 33 5f 61 70 69 2d 3e 76 66 73 5f 66 69 6e 64  e3_api->vfs_find
31984 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
31985 5f 76 66 73 5f 72 65 67 69 73 74 65 72 20 20 20  _vfs_register   
31986 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
31987 61 70 69 2d 3e 76 66 73 5f 72 65 67 69 73 74 65  api->vfs_registe
31988 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  r.#define sqlite
31989 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  3_vfs_unregister
3198a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
3198b 5f 61 70 69 2d 3e 76 66 73 5f 75 6e 72 65 67 69  _api->vfs_unregi
3198c 73 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c  ster.#define sql
3198d 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 20  ite3_threadsafe 
3198e 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
3198f 74 65 33 5f 61 70 69 2d 3e 78 74 68 72 65 61 64  te3_api->xthread
31990 73 61 66 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  safe.#define sql
31991 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f  ite3_result_zero
31992 62 6c 6f 62 20 20 20 20 20 20 20 20 73 71 6c 69  blob        sqli
31993 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
31994 7a 65 72 6f 62 6c 6f 62 0a 23 64 65 66 69 6e 65  zeroblob.#define
31995 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
31996 65 72 72 6f 72 5f 63 6f 64 65 20 20 20 20 20 20  error_code      
31997 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
31998 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 0a 23  ult_error_code.#
31999 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74  define sqlite3_t
3199a 65 73 74 5f 63 6f 6e 74 72 6f 6c 20 20 20 20 20  est_control     
3199b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
3199c 69 2d 3e 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 0a  i->test_control.
3199d 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
3199e 72 61 6e 64 6f 6d 6e 65 73 73 20 20 20 20 20 20  randomness      
3199f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
319a0 70 69 2d 3e 72 61 6e 64 6f 6d 6e 65 73 73 0a 23  pi->randomness.#
319a1 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
319a2 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
319a3 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
319a4 69 2d 3e 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  i->context_db_ha
319a5 6e 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndle.#define sql
319a6 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65  ite3_extended_re
319a7 73 75 6c 74 5f 63 6f 64 65 73 20 20 73 71 6c 69  sult_codes  sqli
319a8 74 65 33 5f 61 70 69 2d 3e 65 78 74 65 6e 64 65  te3_api->extende
319a9 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 0a 23  d_result_codes.#
319aa 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c  define sqlite3_l
319ab 69 6d 69 74 20 20 20 20 20 20 20 20 20 20 20 20  imit            
319ac 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
319ad 69 2d 3e 6c 69 6d 69 74 0a 23 64 65 66 69 6e 65  i->limit.#define
319ae 20 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74   sqlite3_next_st
319af 6d 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  mt              
319b0 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6e 65 78  sqlite3_api->nex
319b1 74 5f 73 74 6d 74 0a 23 64 65 66 69 6e 65 20 73  t_stmt.#define s
319b2 71 6c 69 74 65 33 5f 73 71 6c 20 20 20 20 20 20  qlite3_sql      
319b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
319b4 6c 69 74 65 33 5f 61 70 69 2d 3e 73 71 6c 0a 23  lite3_api->sql.#
319b5 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
319b6 74 61 74 75 73 20 20 20 20 20 20 20 20 20 20 20  tatus           
319b7 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
319b8 69 2d 3e 73 74 61 74 75 73 0a 23 64 65 66 69 6e  i->status.#defin
319b9 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  e sqlite3_backup
319ba 5f 66 69 6e 69 73 68 20 20 20 20 20 20 20 20 20  _finish         
319bb 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 61   sqlite3_api->ba
319bc 63 6b 75 70 5f 66 69 6e 69 73 68 0a 23 64 65 66  ckup_finish.#def
319bd 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  ine sqlite3_back
319be 75 70 5f 69 6e 69 74 20 20 20 20 20 20 20 20 20  up_init         
319bf 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
319c0 62 61 63 6b 75 70 5f 69 6e 69 74 0a 23 64 65 66  backup_init.#def
319c1 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  ine sqlite3_back
319c2 75 70 5f 70 61 67 65 63 6f 75 6e 74 20 20 20 20  up_pagecount    
319c3 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
319c4 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74  backup_pagecount
319c5 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
319c6 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e  _backup_remainin
319c7 67 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  g       sqlite3_
319c8 61 70 69 2d 3e 62 61 63 6b 75 70 5f 72 65 6d 61  api->backup_rema
319c9 69 6e 69 6e 67 0a 23 64 65 66 69 6e 65 20 73 71  ining.#define sq
319ca 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
319cb 70 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  p            sql
319cc 69 74 65 33 5f 61 70 69 2d 3e 62 61 63 6b 75 70  ite3_api->backup
319cd 5f 73 74 65 70 0a 23 64 65 66 69 6e 65 20 73 71  _step.#define sq
319ce 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74  lite3_compileopt
319cf 69 6f 6e 5f 67 65 74 20 20 20 20 20 20 73 71 6c  ion_get      sql
319d0 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 70 69 6c  ite3_api->compil
319d1 65 6f 70 74 69 6f 6e 5f 67 65 74 0a 23 64 65 66  eoption_get.#def
319d2 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  ine sqlite3_comp
319d3 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64 20 20  ileoption_used  
319d4 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
319d5 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73  compileoption_us
319d6 65 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ed.#define sqlit
319d7 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
319d8 6f 6e 5f 76 32 20 20 20 20 20 73 71 6c 69 74 65  on_v2     sqlite
319d9 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 66 75  3_api->create_fu
319da 6e 63 74 69 6f 6e 5f 76 32 0a 23 64 65 66 69 6e  nction_v2.#defin
319db 65 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e  e sqlite3_db_con
319dc 66 69 67 20 20 20 20 20 20 20 20 20 20 20 20 20  fig             
319dd 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 64 62   sqlite3_api->db
319de 5f 63 6f 6e 66 69 67 0a 23 64 65 66 69 6e 65 20  _config.#define 
319df 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78  sqlite3_db_mutex
319e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
319e1 71 6c 69 74 65 33 5f 61 70 69 2d 3e 64 62 5f 6d  qlite3_api->db_m
319e2 75 74 65 78 0a 23 64 65 66 69 6e 65 20 73 71 6c  utex.#define sql
319e3 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 20 20  ite3_db_status  
319e4 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
319e5 74 65 33 5f 61 70 69 2d 3e 64 62 5f 73 74 61 74  te3_api->db_stat
319e6 75 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  us.#define sqlit
319e7 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63  e3_extended_errc
319e8 6f 64 65 20 20 20 20 20 20 20 73 71 6c 69 74 65  ode       sqlite
319e9 33 5f 61 70 69 2d 3e 65 78 74 65 6e 64 65 64 5f  3_api->extended_
319ea 65 72 72 63 6f 64 65 0a 23 64 65 66 69 6e 65 20  errcode.#define 
319eb 73 71 6c 69 74 65 33 5f 6c 6f 67 20 20 20 20 20  sqlite3_log     
319ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
319ed 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 6f 67 0a  qlite3_api->log.
319ee 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
319ef 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 36  soft_heap_limit6
319f0 34 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  4      sqlite3_a
319f1 70 69 2d 3e 73 6f 66 74 5f 68 65 61 70 5f 6c 69  pi->soft_heap_li
319f2 6d 69 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71  mit64.#define sq
319f3 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 20 20  lite3_sourceid  
319f4 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
319f5 69 74 65 33 5f 61 70 69 2d 3e 73 6f 75 72 63 65  ite3_api->source
319f6 69 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  id.#define sqlit
319f7 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 20 20  e3_stmt_status  
319f8 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
319f9 33 5f 61 70 69 2d 3e 73 74 6d 74 5f 73 74 61 74  3_api->stmt_stat
319fa 75 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  us.#define sqlit
319fb 65 33 5f 73 74 72 6e 69 63 6d 70 20 20 20 20 20  e3_strnicmp     
319fc 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
319fd 33 5f 61 70 69 2d 3e 73 74 72 6e 69 63 6d 70 0a  3_api->strnicmp.
319fe 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
319ff 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 20 20 20  unlock_notify   
31a00 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
31a01 70 69 2d 3e 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  pi->unlock_notif
31a02 79 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  y.#define sqlite
31a03 33 5f 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70  3_wal_autocheckp
31a04 6f 69 6e 74 20 20 20 20 20 73 71 6c 69 74 65 33  oint     sqlite3
31a05 5f 61 70 69 2d 3e 77 61 6c 5f 61 75 74 6f 63 68  _api->wal_autoch
31a06 65 63 6b 70 6f 69 6e 74 0a 23 64 65 66 69 6e 65  eckpoint.#define
31a07 20 73 71 6c 69 74 65 33 5f 77 61 6c 5f 63 68 65   sqlite3_wal_che
31a08 63 6b 70 6f 69 6e 74 20 20 20 20 20 20 20 20 20  ckpoint         
31a09 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 77 61 6c  sqlite3_api->wal
31a0a 5f 63 68 65 63 6b 70 6f 69 6e 74 0a 23 64 65 66  _checkpoint.#def
31a0b 69 6e 65 20 73 71 6c 69 74 65 33 5f 77 61 6c 5f  ine sqlite3_wal_
31a0c 68 6f 6f 6b 20 20 20 20 20 20 20 20 20 20 20 20  hook            
31a0d 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
31a0e 77 61 6c 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65  wal_hook.#define
31a0f 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65   sqlite3_blob_re
31a10 6f 70 65 6e 20 20 20 20 20 20 20 20 20 20 20 20  open            
31a11 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f  sqlite3_api->blo
31a12 62 5f 72 65 6f 70 65 6e 0a 23 64 65 66 69 6e 65  b_reopen.#define
31a13 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 6f   sqlite3_vtab_co
31a14 6e 66 69 67 20 20 20 20 20 20 20 20 20 20 20 20  nfig            
31a15 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 74 61  sqlite3_api->vta
31a16 62 5f 63 6f 6e 66 69 67 0a 23 64 65 66 69 6e 65  b_config.#define
31a17 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 6f 6e   sqlite3_vtab_on
31a18 5f 63 6f 6e 66 6c 69 63 74 20 20 20 20 20 20 20  _conflict       
31a19 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 74 61  sqlite3_api->vta
31a1a 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63 74 0a 23 65  b_on_conflict.#e
31a1b 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 43  ndif /* SQLITE_C
31a1c 4f 52 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20  ORE */..#define 
31a1d 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e  SQLITE_EXTENSION
31a1e 5f 49 4e 49 54 31 20 20 20 20 20 63 6f 6e 73 74  _INIT1     const
31a1f 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
31a20 74 69 6e 65 73 20 2a 73 71 6c 69 74 65 33 5f 61  tines *sqlite3_a
31a21 70 69 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20  pi = 0;.#define 
31a22 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e  SQLITE_EXTENSION
31a23 5f 49 4e 49 54 32 28 76 29 20 20 73 71 6c 69 74  _INIT2(v)  sqlit
31a24 65 33 5f 61 70 69 20 3d 20 76 3b 0a 0a 23 65 6e  e3_api = v;..#en
31a25 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 33 45  dif /* _SQLITE3E
31a26 58 54 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  XT_H_ */../*****
31a27 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
31a28 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20 2a 2a   sqlite3ext.h **
31a29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31a2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31a2b 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
31a2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
31a2d 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
31a2e 66 74 20 6f 66 66 20 69 6e 20 6c 6f 61 64 65 78  ft off in loadex
31a2f 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
31a30 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 23 69 6e  ********/./* #in
31a31 63 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e  clude <string.h>
31a32 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
31a33 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
31a34 54 45 4e 53 49 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53  TENSION../*.** S
31a35 6f 6d 65 20 41 50 49 20 72 6f 75 74 69 6e 65 73  ome API routines
31a36 20 61 72 65 20 6f 6d 69 74 74 65 64 20 77 68 65   are omitted whe
31a37 6e 20 76 61 72 69 6f 75 73 20 66 65 61 74 75 72  n various featur
31a38 65 73 20 61 72 65 0a 2a 2a 20 65 78 63 6c 75 64  es are.** exclud
31a39 65 64 20 66 72 6f 6d 20 61 20 62 75 69 6c 64 20  ed from a build 
31a3a 6f 66 20 53 51 4c 69 74 65 2e 20 20 53 75 62 73  of SQLite.  Subs
31a3b 74 69 74 75 74 65 20 61 20 4e 55 4c 4c 20 70 6f  titute a NULL po
31a3c 69 6e 74 65 72 0a 2a 2a 20 66 6f 72 20 61 6e 79  inter.** for any
31a3d 20 6d 69 73 73 69 6e 67 20 41 50 49 73 2e 0a 2a   missing APIs..*
31a3e 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
31a3f 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d  _ENABLE_COLUMN_M
31a40 45 54 41 44 41 54 41 0a 23 20 64 65 66 69 6e 65  ETADATA.# define
31a41 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
31a42 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20 20 20  database_name   
31a43 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
31a44 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
31a45 73 65 5f 6e 61 6d 65 31 36 20 30 0a 23 20 64 65  se_name16 0.# de
31a46 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
31a47 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 20  umn_table_name  
31a48 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
31a49 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
31a4a 62 6c 65 5f 6e 61 6d 65 31 36 20 20 20 20 30 0a  ble_name16    0.
31a4b 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
31a4c 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  _column_origin_n
31a4d 61 6d 65 20 20 20 20 20 30 0a 23 20 64 65 66 69  ame     0.# defi
31a4e 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
31a4f 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 20  n_origin_name16 
31a50 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
31a51 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d  ite3_table_colum
31a52 6e 5f 6d 65 74 61 64 61 74 61 20 20 30 0a 23 65  n_metadata  0.#e
31a53 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
31a54 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
31a55 5a 41 54 49 4f 4e 0a 23 20 64 65 66 69 6e 65 20  ZATION.# define 
31a56 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
31a57 6f 72 69 7a 65 72 20 20 20 20 20 20 20 20 20 30  orizer         0
31a58 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
31a59 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
31a5a 36 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  6.# define sqlit
31a5b 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 20 20  e3_bind_text16  
31a5c 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
31a5d 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
31a5e 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 20  lation_needed16 
31a5f 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
31a60 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65  qlite3_column_de
31a61 63 6c 74 79 70 65 31 36 20 20 20 20 20 20 30 0a  cltype16      0.
31a62 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
31a63 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 20 20  _column_name16  
31a64 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
31a65 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
31a66 6e 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20  n_text16        
31a67 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
31a68 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20  ite3_complete16 
31a69 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20              0.# 
31a6a 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
31a6b 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
31a6c 36 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65  6     0.# define
31a6d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
31a6e 66 75 6e 63 74 69 6f 6e 31 36 20 20 20 20 20 20  function16      
31a6f 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
31a70 65 33 5f 65 72 72 6d 73 67 31 36 20 20 20 20 20  e3_errmsg16     
31a71 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
31a72 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70 65  fine sqlite3_ope
31a73 6e 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20  n16             
31a74 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
31a75 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
31a76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a                0.
31a77 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
31a78 5f 70 72 65 70 61 72 65 31 36 5f 76 32 20 20 20  _prepare16_v2   
31a79 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
31a7a 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
31a7b 74 5f 65 72 72 6f 72 31 36 20 20 20 20 20 20 20  t_error16       
31a7c 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
31a7d 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
31a7e 31 36 20 20 20 20 20 20 20 20 20 20 30 0a 23 20  16          0.# 
31a7f 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
31a80 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 20 20  esult_text16be  
31a81 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
31a82 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
31a83 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20  text16le        
31a84 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
31a85 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 20  e3_value_text16 
31a86 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
31a87 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
31a88 75 65 5f 74 65 78 74 31 36 62 65 20 20 20 20 20  ue_text16be     
31a89 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
31a8a 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
31a8b 74 31 36 6c 65 20 20 20 20 20 20 20 20 20 30 0a  t16le         0.
31a8c 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
31a8d 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
31a8e 5f 6e 61 6d 65 31 36 20 30 0a 23 20 64 65 66 69  _name16 0.# defi
31a8f 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
31a90 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20 20  n_table_name16  
31a91 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
31a92 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
31a93 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30 0a 23 65  in_name16   0.#e
31a94 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
31a95 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54  ITE_OMIT_COMPLET
31a96 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  E.# define sqlit
31a97 65 33 5f 63 6f 6d 70 6c 65 74 65 20 30 0a 23 20  e3_complete 0.# 
31a98 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
31a99 6f 6d 70 6c 65 74 65 31 36 20 30 0a 23 65 6e 64  omplete16 0.#end
31a9a 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
31a9b 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 0a  E_OMIT_DECLTYPE.
31a9c 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
31a9d 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
31a9e 31 36 20 20 20 20 20 20 30 0a 23 20 64 65 66 69  16      0.# defi
31a9f 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
31aa0 6e 5f 64 65 63 6c 74 79 70 65 20 20 20 20 20 20  n_decltype      
31aa1 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    0.#endif..#ifd
31aa2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ef SQLITE_OMIT_P
31aa3 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b  ROGRESS_CALLBACK
31aa4 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
31aa5 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c  3_progress_handl
31aa6 65 72 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66  er 0.#endif..#if
31aa7 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
31aa8 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 64  VIRTUALTABLE.# d
31aa9 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72  efine sqlite3_cr
31aaa 65 61 74 65 5f 6d 6f 64 75 6c 65 20 30 0a 23 20  eate_module 0.# 
31aab 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
31aac 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 20  reate_module_v2 
31aad 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
31aae 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 20  e3_declare_vtab 
31aaf 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
31ab0 65 33 5f 76 74 61 62 5f 63 6f 6e 66 69 67 20 30  e3_vtab_config 0
31ab1 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
31ab2 33 5f 76 74 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69  3_vtab_on_confli
31ab3 63 74 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66  ct 0.#endif..#if
31ab4 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
31ab5 53 48 41 52 45 44 5f 43 41 43 48 45 0a 23 20 64  SHARED_CACHE.# d
31ab6 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e  efine sqlite3_en
31ab7 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
31ab8 65 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  e 0.#endif..#ifd
31ab9 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
31aba 52 41 43 45 0a 23 20 64 65 66 69 6e 65 20 73 71  RACE.# define sq
31abb 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 20 20 20  lite3_profile   
31abc 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
31abd 71 6c 69 74 65 33 5f 74 72 61 63 65 20 20 20 20  qlite3_trace    
31abe 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23       0.#endif..#
31abf 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
31ac0 54 5f 47 45 54 5f 54 41 42 4c 45 0a 23 20 64 65  T_GET_TABLE.# de
31ac1 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65  fine sqlite3_fre
31ac2 65 5f 74 61 62 6c 65 20 20 20 20 30 0a 23 20 64  e_table    0.# d
31ac3 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65  efine sqlite3_ge
31ac4 74 5f 74 61 62 6c 65 20 20 20 20 20 30 0a 23 65  t_table     0.#e
31ac5 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
31ac6 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
31ac7 42 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  B.#define sqlite
31ac8 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 20  3_bind_zeroblob 
31ac9 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74   0.#define sqlit
31aca 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20 20 20  e3_blob_bytes   
31acb 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69    0.#define sqli
31acc 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20  te3_blob_close  
31acd 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c     0.#define sql
31ace 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20  ite3_blob_open  
31acf 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71      0.#define sq
31ad0 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 20  lite3_blob_read 
31ad1 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73       0.#define s
31ad2 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
31ad3 65 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20  e     0.#define 
31ad4 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 6f  sqlite3_blob_reo
31ad5 70 65 6e 20 20 20 20 30 0a 23 65 6e 64 69 66 0a  pen    0.#endif.
31ad6 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
31ad7 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63  wing structure c
31ad8 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73  ontains pointers
31ad9 20 74 6f 20 61 6c 6c 20 53 51 4c 69 74 65 20 41   to all SQLite A
31ada 50 49 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20  PI routines..** 
31adb 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69  A pointer to thi
31adc 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70  s structure is p
31add 61 73 73 65 64 20 69 6e 74 6f 20 65 78 74 65 6e  assed into exten
31ade 73 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 79 20  sions when they 
31adf 61 72 65 0a 2a 2a 20 6c 6f 61 64 65 64 20 73 6f  are.** loaded so
31ae0 20 74 68 61 74 20 74 68 65 20 65 78 74 65 6e 73   that the extens
31ae1 69 6f 6e 20 63 61 6e 20 6d 61 6b 65 20 63 61 6c  ion can make cal
31ae2 6c 73 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65  ls back into the
31ae3 20 53 51 4c 69 74 65 0a 2a 2a 20 6c 69 62 72 61   SQLite.** libra
31ae4 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61  ry..**.** When a
31ae5 64 64 69 6e 67 20 6e 65 77 20 41 50 49 73 2c 20  dding new APIs, 
31ae6 61 64 64 20 74 68 65 6d 20 74 6f 20 74 68 65 20  add them to the 
31ae7 62 6f 74 74 6f 6d 20 6f 66 20 74 68 69 73 20 73  bottom of this s
31ae8 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 6e 20 6f  tructure.** in o
31ae9 72 64 65 72 20 74 6f 20 70 72 65 73 65 72 76 65  rder to preserve
31aea 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61   backwards compa
31aeb 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20  tibility..**.** 
31aec 45 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 74 20  Extensions that 
31aed 75 73 65 20 6e 65 77 65 72 20 41 50 49 73 20 73  use newer APIs s
31aee 68 6f 75 6c 64 20 66 69 72 73 74 20 63 61 6c 6c  hould first call
31aef 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   the.** sqlite3_
31af0 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65  libversion_numbe
31af1 72 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  r() to make sure
31af2 20 74 68 61 74 20 74 68 65 20 41 50 49 20 74 68   that the API th
31af3 65 79 0a 2a 2a 20 69 6e 74 65 6e 64 20 74 6f 20  ey.** intend to 
31af4 75 73 65 20 69 73 20 73 75 70 70 6f 72 74 65 64  use is supported
31af5 20 62 79 20 74 68 65 20 6c 69 62 72 61 72 79 2e   by the library.
31af6 20 20 45 78 74 65 6e 73 69 6f 6e 73 20 73 68 6f    Extensions sho
31af7 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20 63 68 65 63  uld.** also chec
31af8 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  k to make sure t
31af9 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20  hat the pointer 
31afa 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
31afb 69 73 0a 2a 2a 20 6e 6f 74 20 4e 55 4c 4c 20 62  is.** not NULL b
31afc 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 69 74  efore calling it
31afd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
31afe 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f  t sqlite3_api_ro
31aff 75 74 69 6e 65 73 20 73 71 6c 69 74 65 33 41 70  utines sqlite3Ap
31b00 69 73 20 3d 20 7b 0a 20 20 73 71 6c 69 74 65 33  is = {.  sqlite3
31b01 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
31b02 78 74 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  xt,.#ifndef SQLI
31b03 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
31b04 45 44 0a 20 20 73 71 6c 69 74 65 33 5f 61 67 67  ED.  sqlite3_agg
31b05 72 65 67 61 74 65 5f 63 6f 75 6e 74 2c 0a 23 65  regate_count,.#e
31b06 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a  lse.  0,.#endif.
31b07 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62    sqlite3_bind_b
31b08 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  lob,.  sqlite3_b
31b09 69 6e 64 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71  ind_double,.  sq
31b0a 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 2c 0a  lite3_bind_int,.
31b0b 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
31b0c 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f  nt64,.  sqlite3_
31b0d 62 69 6e 64 5f 6e 75 6c 6c 2c 0a 20 20 73 71 6c  bind_null,.  sql
31b0e 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
31b0f 74 65 72 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c  ter_count,.  sql
31b10 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
31b11 74 65 72 5f 69 6e 64 65 78 2c 0a 20 20 73 71 6c  ter_index,.  sql
31b12 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
31b13 74 65 72 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69  ter_name,.  sqli
31b14 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 2c 0a 20  te3_bind_text,. 
31b15 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
31b16 78 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  xt16,.  sqlite3_
31b17 62 69 6e 64 5f 76 61 6c 75 65 2c 0a 20 20 73 71  bind_value,.  sq
31b18 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c  lite3_busy_handl
31b19 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 75  er,.  sqlite3_bu
31b1a 73 79 5f 74 69 6d 65 6f 75 74 2c 0a 20 20 73 71  sy_timeout,.  sq
31b1b 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 2c 0a 20  lite3_changes,. 
31b1c 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 2c 0a   sqlite3_close,.
31b1d 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74    sqlite3_collat
31b1e 69 6f 6e 5f 6e 65 65 64 65 64 2c 0a 20 20 73 71  ion_needed,.  sq
31b1f 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  lite3_collation_
31b20 6e 65 65 64 65 64 31 36 2c 0a 20 20 73 71 6c 69  needed16,.  sqli
31b21 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 2c  te3_column_blob,
31b22 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
31b23 6e 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c 69 74  n_bytes,.  sqlit
31b24 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31  e3_column_bytes1
31b25 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  6,.  sqlite3_col
31b26 75 6d 6e 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c  umn_count,.  sql
31b27 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
31b28 62 61 73 65 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c  base_name,.  sql
31b29 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
31b2a 62 61 73 65 5f 6e 61 6d 65 31 36 2c 0a 20 20 73  base_name16,.  s
31b2b 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65  qlite3_column_de
31b2c 63 6c 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65  cltype,.  sqlite
31b2d 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  3_column_decltyp
31b2e 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  e16,.  sqlite3_c
31b2f 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 2c 0a 20 20  olumn_double,.  
31b30 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
31b31 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nt,.  sqlite3_co
31b32 6c 75 6d 6e 5f 69 6e 74 36 34 2c 0a 20 20 73 71  lumn_int64,.  sq
31b33 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
31b34 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  e,.  sqlite3_col
31b35 75 6d 6e 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71  umn_name16,.  sq
31b36 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
31b37 67 69 6e 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69  gin_name,.  sqli
31b38 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  te3_column_origi
31b39 6e 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69  n_name16,.  sqli
31b3a 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  te3_column_table
31b3b 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33  _name,.  sqlite3
31b3c 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
31b3d 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  me16,.  sqlite3_
31b3e 63 6f 6c 75 6d 6e 5f 74 65 78 74 2c 0a 20 20 73  column_text,.  s
31b3f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
31b40 78 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  xt16,.  sqlite3_
31b41 63 6f 6c 75 6d 6e 5f 74 79 70 65 2c 0a 20 20 73  column_type,.  s
31b42 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61  qlite3_column_va
31b43 6c 75 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  lue,.  sqlite3_c
31b44 6f 6d 6d 69 74 5f 68 6f 6f 6b 2c 0a 20 20 73 71  ommit_hook,.  sq
31b45 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 2c 0a  lite3_complete,.
31b46 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65    sqlite3_comple
31b47 74 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  te16,.  sqlite3_
31b48 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
31b49 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ,.  sqlite3_crea
31b4a 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 2c 0a  te_collation16,.
31b4b 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
31b4c 5f 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 73 71 6c  _function,.  sql
31b4d 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
31b4e 74 69 6f 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65  tion16,.  sqlite
31b4f 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 2c  3_create_module,
31b50 0a 20 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f  .  sqlite3_data_
31b51 63 6f 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33  count,.  sqlite3
31b52 5f 64 62 5f 68 61 6e 64 6c 65 2c 0a 20 20 73 71  _db_handle,.  sq
31b53 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
31b54 61 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 6e  ab,.  sqlite3_en
31b55 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
31b56 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 72 72  e,.  sqlite3_err
31b57 63 6f 64 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  code,.  sqlite3_
31b58 65 72 72 6d 73 67 2c 0a 20 20 73 71 6c 69 74 65  errmsg,.  sqlite
31b59 33 5f 65 72 72 6d 73 67 31 36 2c 0a 20 20 73 71  3_errmsg16,.  sq
31b5a 6c 69 74 65 33 5f 65 78 65 63 2c 0a 23 69 66 6e  lite3_exec,.#ifn
31b5b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
31b5c 44 45 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c  DEPRECATED.  sql
31b5d 69 74 65 33 5f 65 78 70 69 72 65 64 2c 0a 23 65  ite3_expired,.#e
31b5e 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a  lse.  0,.#endif.
31b5f 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
31b60 7a 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  ze,.  sqlite3_fr
31b61 65 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  ee,.  sqlite3_fr
31b62 65 65 5f 74 61 62 6c 65 2c 0a 20 20 73 71 6c 69  ee_table,.  sqli
31b63 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d  te3_get_autocomm
31b64 69 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65  it,.  sqlite3_ge
31b65 74 5f 61 75 78 64 61 74 61 2c 0a 20 20 73 71 6c  t_auxdata,.  sql
31b66 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 2c 0a  ite3_get_table,.
31b67 20 20 30 2c 20 20 20 20 20 2f 2a 20 57 61 73 20    0,     /* Was 
31b68 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72  sqlite3_global_r
31b69 65 63 6f 76 65 72 28 29 2c 20 62 75 74 20 74 68  ecover(), but th
31b6a 61 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64  at function is d
31b6b 65 70 72 65 63 61 74 65 64 20 2a 2f 0a 20 20 73  eprecated */.  s
31b6c 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
31b6d 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 61 73 74  ,.  sqlite3_last
31b6e 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c 0a 20  _insert_rowid,. 
31b6f 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73   sqlite3_libvers
31b70 69 6f 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c  ion,.  sqlite3_l
31b71 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
31b72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  ,.  sqlite3_mall
31b73 6f 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 70  oc,.  sqlite3_mp
31b74 72 69 6e 74 66 2c 0a 20 20 73 71 6c 69 74 65 33  rintf,.  sqlite3
31b75 5f 6f 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65 33  _open,.  sqlite3
31b76 5f 6f 70 65 6e 31 36 2c 0a 20 20 73 71 6c 69 74  _open16,.  sqlit
31b77 65 33 5f 70 72 65 70 61 72 65 2c 0a 20 20 73 71  e3_prepare,.  sq
31b78 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 2c  lite3_prepare16,
31b79 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69  .  sqlite3_profi
31b7a 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72  le,.  sqlite3_pr
31b7b 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 2c 0a  ogress_handler,.
31b7c 20 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f    sqlite3_reallo
31b7d 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  c,.  sqlite3_res
31b7e 65 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  et,.  sqlite3_re
31b7f 73 75 6c 74 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c  sult_blob,.  sql
31b80 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
31b81 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  le,.  sqlite3_re
31b82 73 75 6c 74 5f 65 72 72 6f 72 2c 0a 20 20 73 71  sult_error,.  sq
31b83 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
31b84 6f 72 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  or16,.  sqlite3_
31b85 72 65 73 75 6c 74 5f 69 6e 74 2c 0a 20 20 73 71  result_int,.  sq
31b86 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
31b87 36 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  64,.  sqlite3_re
31b88 73 75 6c 74 5f 6e 75 6c 6c 2c 0a 20 20 73 71 6c  sult_null,.  sql
31b89 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
31b8a 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
31b8b 6c 74 5f 74 65 78 74 31 36 2c 0a 20 20 73 71 6c  lt_text16,.  sql
31b8c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
31b8d 31 36 62 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  16be,.  sqlite3_
31b8e 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 2c  result_text16le,
31b8f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
31b90 74 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c 69 74  t_value,.  sqlit
31b91 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b  e3_rollback_hook
31b92 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  ,.  sqlite3_set_
31b93 61 75 74 68 6f 72 69 7a 65 72 2c 0a 20 20 73 71  authorizer,.  sq
31b94 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74  lite3_set_auxdat
31b95 61 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  a,.  sqlite3_snp
31b96 72 69 6e 74 66 2c 0a 20 20 73 71 6c 69 74 65 33  rintf,.  sqlite3
31b97 5f 73 74 65 70 2c 0a 20 20 73 71 6c 69 74 65 33  _step,.  sqlite3
31b98 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65  _table_column_me
31b99 74 61 64 61 74 61 2c 0a 23 69 66 6e 64 65 66 20  tadata,.#ifndef 
31b9a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52  SQLITE_OMIT_DEPR
31b9b 45 43 41 54 45 44 0a 20 20 73 71 6c 69 74 65 33  ECATED.  sqlite3
31b9c 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 2c  _thread_cleanup,
31b9d 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64  .#else.  0,.#end
31b9e 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 74  if.  sqlite3_tot
31b9f 61 6c 5f 63 68 61 6e 67 65 73 2c 0a 20 20 73 71  al_changes,.  sq
31ba0 6c 69 74 65 33 5f 74 72 61 63 65 2c 0a 23 69 66  lite3_trace,.#if
31ba1 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
31ba2 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20 73 71  _DEPRECATED.  sq
31ba3 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62  lite3_transfer_b
31ba4 69 6e 64 69 6e 67 73 2c 0a 23 65 6c 73 65 0a 20  indings,.#else. 
31ba5 20 30 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c   0,.#endif.  sql
31ba6 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b  ite3_update_hook
31ba7 2c 0a 20 20 73 71 6c 69 74 65 33 5f 75 73 65 72  ,.  sqlite3_user
31ba8 5f 64 61 74 61 2c 0a 20 20 73 71 6c 69 74 65 33  _data,.  sqlite3
31ba9 5f 76 61 6c 75 65 5f 62 6c 6f 62 2c 0a 20 20 73  _value_blob,.  s
31baa 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
31bab 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  es,.  sqlite3_va
31bac 6c 75 65 5f 62 79 74 65 73 31 36 2c 0a 20 20 73  lue_bytes16,.  s
31bad 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
31bae 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  ble,.  sqlite3_v
31baf 61 6c 75 65 5f 69 6e 74 2c 0a 20 20 73 71 6c 69  alue_int,.  sqli
31bb0 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 2c  te3_value_int64,
31bb1 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
31bb2 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 2c 0a 20  _numeric_type,. 
31bb3 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
31bb4 65 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  ext,.  sqlite3_v
31bb5 61 6c 75 65 5f 74 65 78 74 31 36 2c 0a 20 20 73  alue_text16,.  s
31bb6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
31bb7 74 31 36 62 65 2c 0a 20 20 73 71 6c 69 74 65 33  t16be,.  sqlite3
31bb8 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 2c  _value_text16le,
31bb9 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
31bba 5f 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33  _type,.  sqlite3
31bbb 5f 76 6d 70 72 69 6e 74 66 2c 0a 20 20 2f 2a 0a  _vmprintf,.  /*.
31bbc 20 20 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61    ** The origina
31bbd 6c 20 41 50 49 20 73 65 74 20 65 6e 64 73 20 68  l API set ends h
31bbe 65 72 65 2e 20 20 41 6c 6c 20 65 78 74 65 6e 73  ere.  All extens
31bbf 69 6f 6e 73 20 63 61 6e 20 63 61 6c 6c 20 61 6e  ions can call an
31bc0 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 41 50  y.  ** of the AP
31bc1 49 73 20 61 62 6f 76 65 20 70 72 6f 76 69 64 65  Is above provide
31bc2 64 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74  d that the point
31bc3 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20  er is not NULL. 
31bc4 20 42 75 74 0a 20 20 2a 2a 20 62 65 66 6f 72 65   But.  ** before
31bc5 20 63 61 6c 6c 69 6e 67 20 41 50 49 73 20 74 68   calling APIs th
31bc6 61 74 20 66 6f 6c 6c 6f 77 2c 20 65 78 74 65 6e  at follow, exten
31bc7 73 69 6f 6e 20 73 68 6f 75 6c 64 20 63 68 65 63  sion should chec
31bc8 6b 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74  k the.  ** sqlit
31bc9 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  e3_libversion_nu
31bca 6d 62 65 72 28 29 20 74 6f 20 6d 61 6b 65 20 73  mber() to make s
31bcb 75 72 65 20 74 68 65 79 20 61 72 65 20 64 65 61  ure they are dea
31bcc 6c 69 6e 67 20 77 69 74 68 0a 20 20 2a 2a 20 61  ling with.  ** a
31bcd 20 6c 69 62 72 61 72 79 20 74 68 61 74 20 69 73   library that is
31bce 20 6e 65 77 20 65 6e 6f 75 67 68 20 74 6f 20 73   new enough to s
31bcf 75 70 70 6f 72 74 20 74 68 61 74 20 41 50 49 2e  upport that API.
31bd0 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .  *************
31bd1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31bd2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31bd3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31bd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a  ************.  *
31bd5 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6f 76 65 72  /.  sqlite3_over
31bd6 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 2c 0a 0a  load_function,..
31bd7 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20    /*.  ** Added 
31bd8 61 66 74 65 72 20 33 2e 33 2e 31 33 0a 20 20 2a  after 3.3.13.  *
31bd9 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70  /.  sqlite3_prep
31bda 61 72 65 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65  are_v2,.  sqlite
31bdb 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 2c 0a  3_prepare16_v2,.
31bdc 20 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f    sqlite3_clear_
31bdd 62 69 6e 64 69 6e 67 73 2c 0a 0a 20 20 2f 2a 0a  bindings,..  /*.
31bde 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33    ** Added for 3
31bdf 2e 34 2e 31 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  .4.1.  */.  sqli
31be0 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
31be1 65 5f 76 32 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  e_v2,..  /*.  **
31be2 20 41 64 64 65 64 20 66 6f 72 20 33 2e 35 2e 30   Added for 3.5.0
31be3 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
31be4 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 2c 0a 20  bind_zeroblob,. 
31be5 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79   sqlite3_blob_by
31be6 74 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  tes,.  sqlite3_b
31be7 6c 6f 62 5f 63 6c 6f 73 65 2c 0a 20 20 73 71 6c  lob_close,.  sql
31be8 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 2c 0a  ite3_blob_open,.
31be9 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72    sqlite3_blob_r
31bea 65 61 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  ead,.  sqlite3_b
31beb 6c 6f 62 5f 77 72 69 74 65 2c 0a 20 20 73 71 6c  lob_write,.  sql
31bec 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
31bed 61 74 69 6f 6e 5f 76 32 2c 0a 20 20 73 71 6c 69  ation_v2,.  sqli
31bee 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c  te3_file_control
31bef 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  ,.  sqlite3_memo
31bf0 72 79 5f 68 69 67 68 77 61 74 65 72 2c 0a 20 20  ry_highwater,.  
31bf1 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75  sqlite3_memory_u
31bf2 73 65 64 2c 0a 23 69 66 64 65 66 20 53 51 4c 49  sed,.#ifdef SQLI
31bf3 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 20 20  TE_MUTEX_OMIT.  
31bf4 30 2c 20 0a 20 20 30 2c 20 0a 20 20 30 2c 0a 20  0, .  0, .  0,. 
31bf5 20 30 2c 0a 20 20 30 2c 0a 23 65 6c 73 65 0a 20   0,.  0,.#else. 
31bf6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
31bf7 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  lloc,.  sqlite3_
31bf8 6d 75 74 65 78 5f 65 6e 74 65 72 2c 0a 20 20 73  mutex_enter,.  s
31bf9 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65  qlite3_mutex_fre
31bfa 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  e,.  sqlite3_mut
31bfb 65 78 5f 6c 65 61 76 65 2c 0a 20 20 73 71 6c 69  ex_leave,.  sqli
31bfc 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 2c 0a 23  te3_mutex_try,.#
31bfd 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
31bfe 6f 70 65 6e 5f 76 32 2c 0a 20 20 73 71 6c 69 74  open_v2,.  sqlit
31bff 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
31c00 79 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  y,.  sqlite3_res
31c01 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 2c  ult_error_nomem,
31c02 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
31c03 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 2c 0a  t_error_toobig,.
31c04 20 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 2c    sqlite3_sleep,
31c05 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f  .  sqlite3_soft_
31c06 68 65 61 70 5f 6c 69 6d 69 74 2c 0a 20 20 73 71  heap_limit,.  sq
31c07 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 2c 0a  lite3_vfs_find,.
31c08 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65    sqlite3_vfs_re
31c09 67 69 73 74 65 72 2c 0a 20 20 73 71 6c 69 74 65  gister,.  sqlite
31c0a 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  3_vfs_unregister
31c0b 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64  ,..  /*.  ** Add
31c0c 65 64 20 66 6f 72 20 33 2e 35 2e 38 0a 20 20 2a  ed for 3.5.8.  *
31c0d 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 68 72 65  /.  sqlite3_thre
31c0e 61 64 73 61 66 65 2c 0a 20 20 73 71 6c 69 74 65  adsafe,.  sqlite
31c0f 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f  3_result_zeroblo
31c10 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  b,.  sqlite3_res
31c11 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 2c 0a  ult_error_code,.
31c12 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63    sqlite3_test_c
31c13 6f 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c 69 74 65  ontrol,.  sqlite
31c14 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 2c 0a 20 20  3_randomness,.  
31c15 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
31c16 64 62 5f 68 61 6e 64 6c 65 2c 0a 0a 20 20 2f 2a  db_handle,..  /*
31c17 0a 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20  .  ** Added for 
31c18 33 2e 36 2e 30 0a 20 20 2a 2f 0a 20 20 73 71 6c  3.6.0.  */.  sql
31c19 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65  ite3_extended_re
31c1a 73 75 6c 74 5f 63 6f 64 65 73 2c 0a 20 20 73 71  sult_codes,.  sq
31c1b 6c 69 74 65 33 5f 6c 69 6d 69 74 2c 0a 20 20 73  lite3_limit,.  s
31c1c 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74  qlite3_next_stmt
31c1d 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 71 6c 2c  ,.  sqlite3_sql,
31c1e 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75  .  sqlite3_statu
31c1f 73 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64  s,..  /*.  ** Ad
31c20 64 65 64 20 66 6f 72 20 33 2e 37 2e 34 0a 20 20  ded for 3.7.4.  
31c21 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63  */.  sqlite3_bac
31c22 6b 75 70 5f 66 69 6e 69 73 68 2c 0a 20 20 73 71  kup_finish,.  sq
31c23 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69  lite3_backup_ini
31c24 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63  t,.  sqlite3_bac
31c25 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 2c 0a 20  kup_pagecount,. 
31c26 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
31c27 72 65 6d 61 69 6e 69 6e 67 2c 0a 20 20 73 71 6c  remaining,.  sql
31c28 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
31c29 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ,.#ifndef SQLITE
31c2a 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54  _OMIT_COMPILEOPT
31c2b 49 4f 4e 5f 44 49 41 47 53 0a 20 20 73 71 6c 69  ION_DIAGS.  sqli
31c2c 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f  te3_compileoptio
31c2d 6e 5f 67 65 74 2c 0a 20 20 73 71 6c 69 74 65 33  n_get,.  sqlite3
31c2e 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75  _compileoption_u
31c2f 73 65 64 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a  sed,.#else.  0,.
31c30 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71    0,.#endif.  sq
31c31 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
31c32 63 74 69 6f 6e 5f 76 32 2c 0a 20 20 73 71 6c 69  ction_v2,.  sqli
31c33 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 2c 0a 20  te3_db_config,. 
31c34 20 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65   sqlite3_db_mute
31c35 78 2c 0a 20 20 73 71 6c 69 74 65 33 5f 64 62 5f  x,.  sqlite3_db_
31c36 73 74 61 74 75 73 2c 0a 20 20 73 71 6c 69 74 65  status,.  sqlite
31c37 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f  3_extended_errco
31c38 64 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 6f  de,.  sqlite3_lo
31c39 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 66  g,.  sqlite3_sof
31c3a 74 5f 68 65 61 70 5f 6c 69 6d 69 74 36 34 2c 0a  t_heap_limit64,.
31c3b 20 20 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65    sqlite3_source
31c3c 69 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  id,.  sqlite3_st
31c3d 6d 74 5f 73 74 61 74 75 73 2c 0a 20 20 73 71 6c  mt_status,.  sql
31c3e 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 2c 0a 23  ite3_strnicmp,.#
31c3f 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
31c40 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46  BLE_UNLOCK_NOTIF
31c41 59 0a 20 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f  Y.  sqlite3_unlo
31c42 63 6b 5f 6e 6f 74 69 66 79 2c 0a 23 65 6c 73 65  ck_notify,.#else
31c43 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a 23 69 66  .  0,.#endif.#if
31c44 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
31c45 5f 57 41 4c 0a 20 20 73 71 6c 69 74 65 33 5f 77  _WAL.  sqlite3_w
31c46 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69 6e  al_autocheckpoin
31c47 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 77 61 6c  t,.  sqlite3_wal
31c48 5f 63 68 65 63 6b 70 6f 69 6e 74 2c 0a 20 20 73  _checkpoint,.  s
31c49 71 6c 69 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b 2c  qlite3_wal_hook,
31c4a 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 20 20 30 2c  .#else.  0,.  0,
31c4b 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20 20 73  .  0,.#endif.  s
31c4c 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 6f 70  qlite3_blob_reop
31c4d 65 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  en,.  sqlite3_vt
31c4e 61 62 5f 63 6f 6e 66 69 67 2c 0a 20 20 73 71 6c  ab_config,.  sql
31c4f 69 74 65 33 5f 76 74 61 62 5f 6f 6e 5f 63 6f 6e  ite3_vtab_on_con
31c50 66 6c 69 63 74 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  flict,.};../*.**
31c51 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64   Attempt to load
31c52 20 61 6e 20 53 51 4c 69 74 65 20 65 78 74 65 6e   an SQLite exten
31c53 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 63 6f 6e  sion library con
31c54 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 69  tained in the fi
31c55 6c 65 0a 2a 2a 20 7a 46 69 6c 65 2e 20 20 54 68  le.** zFile.  Th
31c56 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73  e entry point is
31c57 20 7a 50 72 6f 63 2e 20 20 7a 50 72 6f 63 20 6d   zProc.  zProc m
31c58 61 79 20 62 65 20 30 20 69 6e 20 77 68 69 63 68  ay be 0 in which
31c59 20 63 61 73 65 20 61 0a 2a 2a 20 64 65 66 61 75   case a.** defau
31c5a 6c 74 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 6e  lt entry point n
31c5b 61 6d 65 20 28 73 71 6c 69 74 65 33 5f 65 78 74  ame (sqlite3_ext
31c5c 65 6e 73 69 6f 6e 5f 69 6e 69 74 29 20 69 73 20  ension_init) is 
31c5d 75 73 65 64 2e 20 20 55 73 65 0a 2a 2a 20 6f 66  used.  Use.** of
31c5e 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e 61 6d   the default nam
31c5f 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64  e is recommended
31c60 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
31c61 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
31c62 65 73 73 20 61 6e 64 20 53 51 4c 49 54 45 5f 45  ess and SQLITE_E
31c63 52 52 4f 52 20 69 66 20 73 6f 6d 65 74 68 69 6e  RROR if somethin
31c64 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a  g goes wrong..**
31c65 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
31c66 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45 72 72  occurs and pzErr
31c67 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 68  Msg is not 0, th
31c68 65 6e 20 66 69 6c 6c 20 2a 70 7a 45 72 72 4d 73  en fill *pzErrMs
31c69 67 20 77 69 74 68 20 0a 2a 2a 20 65 72 72 6f 72  g with .** error
31c6a 20 6d 65 73 73 61 67 65 20 74 65 78 74 2e 20 20   message text.  
31c6b 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  The calling func
31c6c 74 69 6f 6e 20 73 68 6f 75 6c 64 20 66 72 65 65  tion should free
31c6d 20 74 68 69 73 20 6d 65 6d 6f 72 79 0a 2a 2a 20   this memory.** 
31c6e 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  by calling sqlit
31c6f 65 33 44 62 46 72 65 65 28 64 62 2c 20 29 2e 0a  e3DbFree(db, )..
31c70 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
31c71 6c 69 74 65 33 4c 6f 61 64 45 78 74 65 6e 73 69  lite3LoadExtensi
31c72 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  on(.  sqlite3 *d
31c73 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  b,          /* L
31c74 6f 61 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f  oad the extensio
31c75 6e 20 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61  n into this data
31c76 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
31c77 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
31c78 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61  *zFile,    /* Na
31c79 6d 65 20 6f 66 20 74 68 65 20 73 68 61 72 65 64  me of the shared
31c7a 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e   library contain
31c7b 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f  ing extension */
31c7c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
31c7d 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72  Proc,    /* Entr
31c7e 79 20 70 6f 69 6e 74 2e 20 20 55 73 65 20 22 73  y point.  Use "s
31c7f 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e  qlite3_extension
31c80 5f 69 6e 69 74 22 20 69 66 20 30 20 2a 2f 0a 20  _init" if 0 */. 
31c81 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
31c82 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20 65 72         /* Put er
31c83 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72 65  ror message here
31c84 20 69 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 7b 0a   if not 0 */.){.
31c85 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
31c86 56 66 73 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a  Vfs = db->pVfs;.
31c87 20 20 76 6f 69 64 20 2a 68 61 6e 64 6c 65 3b 0a    void *handle;.
31c88 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 73    int (*xInit)(s
31c89 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63  qlite3*,char**,c
31c8a 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69  onst sqlite3_api
31c8b 5f 72 6f 75 74 69 6e 65 73 2a 29 3b 0a 20 20 63  _routines*);.  c
31c8c 68 61 72 20 2a 7a 45 72 72 6d 73 67 20 3d 20 30  har *zErrmsg = 0
31c8d 3b 0a 20 20 76 6f 69 64 20 2a 2a 61 48 61 6e 64  ;.  void **aHand
31c8e 6c 65 3b 0a 20 20 69 6e 74 20 6e 4d 73 67 20 3d  le;.  int nMsg =
31c8f 20 33 30 30 20 2b 20 73 71 6c 69 74 65 33 53 74   300 + sqlite3St
31c90 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29 3b 0a 0a  rlen30(zFile);..
31c91 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29    if( pzErrMsg )
31c92 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a   *pzErrMsg = 0;.
31c93 0a 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 31 38  .  /* Ticket #18
31c94 36 33 2e 20 20 54 6f 20 61 76 6f 69 64 20 61 20  63.  To avoid a 
31c95 63 72 65 61 74 69 6e 67 20 73 65 63 75 72 69 74  creating securit
31c96 79 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f  y problems for o
31c97 6c 64 65 72 0a 20 20 2a 2a 20 61 70 70 6c 69 63  lder.  ** applic
31c98 61 74 69 6f 6e 73 20 74 68 61 74 20 72 65 6c 69  ations that reli
31c99 6e 6b 20 61 67 61 69 6e 73 74 20 6e 65 77 65 72  nk against newer
31c9a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
31c9b 69 74 65 2c 20 74 68 65 0a 20 20 2a 2a 20 61 62  ite, the.  ** ab
31c9c 69 6c 69 74 79 20 74 6f 20 72 75 6e 20 6c 6f 61  ility to run loa
31c9d 64 5f 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 74  d_extension is t
31c9e 75 72 6e 65 64 20 6f 66 66 20 62 79 20 64 65 66  urned off by def
31c9f 61 75 6c 74 2e 20 20 4f 6e 65 0a 20 20 2a 2a 20  ault.  One.  ** 
31ca0 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65  must call sqlite
31ca1 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78  3_enable_load_ex
31ca2 74 65 6e 73 69 6f 6e 28 29 20 74 6f 20 74 75 72  tension() to tur
31ca3 6e 20 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 20  n on extension. 
31ca4 20 2a 2a 20 6c 6f 61 64 69 6e 67 2e 20 20 4f 74   ** loading.  Ot
31ca5 68 65 72 77 69 73 65 20 79 6f 75 20 67 65 74 20  herwise you get 
31ca6 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72  the following er
31ca7 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ror..  */.  if( 
31ca8 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  (db->flags & SQL
31ca9 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ITE_LoadExtensio
31caa 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  n)==0 ){.    if(
31cab 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20   pzErrMsg ){.   
31cac 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73     *pzErrMsg = s
31cad 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
31cae 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22 29  not authorized")
31caf 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
31cb0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
31cb1 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 50 72 6f  .  }..  if( zPro
31cb2 63 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 50 72 6f  c==0 ){.    zPro
31cb3 63 20 3d 20 22 73 71 6c 69 74 65 33 5f 65 78 74  c = "sqlite3_ext
31cb4 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 3b 0a 20 20  ension_init";.  
31cb5 7d 0a 0a 20 20 68 61 6e 64 6c 65 20 3d 20 73 71  }..  handle = sq
31cb6 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 70 56  lite3OsDlOpen(pV
31cb7 66 73 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 69 66  fs, zFile);.  if
31cb8 28 20 68 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20  ( handle==0 ){. 
31cb9 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20     if( pzErrMsg 
31cba 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 4d  ){.      *pzErrM
31cbb 73 67 20 3d 20 7a 45 72 72 6d 73 67 20 3d 20 73  sg = zErrmsg = s
31cbc 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4d  qlite3_malloc(nM
31cbd 73 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  sg);.      if( z
31cbe 45 72 72 6d 73 67 20 29 7b 0a 20 20 20 20 20 20  Errmsg ){.      
31cbf 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
31cc0 74 66 28 6e 4d 73 67 2c 20 7a 45 72 72 6d 73 67  tf(nMsg, zErrmsg
31cc1 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22  , .            "
31cc2 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 73  unable to open s
31cc3 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 5b 25  hared library [%
31cc4 73 5d 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20  s]", zFile);.   
31cc5 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c       sqlite3OsDl
31cc6 45 72 72 6f 72 28 70 56 66 73 2c 20 6e 4d 73 67  Error(pVfs, nMsg
31cc7 2d 31 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20  -1, zErrmsg);.  
31cc8 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
31cc9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
31cca 52 4f 52 3b 0a 20 20 7d 0a 20 20 78 49 6e 69 74  ROR;.  }.  xInit
31ccb 20 3d 20 28 69 6e 74 28 2a 29 28 73 71 6c 69 74   = (int(*)(sqlit
31ccc 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74  e3*,char**,const
31ccd 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
31cce 74 69 6e 65 73 2a 29 29 0a 20 20 20 20 20 20 20  tines*)).       
31ccf 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
31cd0 74 65 33 4f 73 44 6c 53 79 6d 28 70 56 66 73 2c  te3OsDlSym(pVfs,
31cd1 20 68 61 6e 64 6c 65 2c 20 7a 50 72 6f 63 29 3b   handle, zProc);
31cd2 0a 20 20 69 66 28 20 78 49 6e 69 74 3d 3d 30 20  .  if( xInit==0 
31cd3 29 7b 0a 20 20 20 20 69 66 28 20 70 7a 45 72 72  ){.    if( pzErr
31cd4 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 6e 4d 73  Msg ){.      nMs
31cd5 67 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  g += sqlite3Strl
31cd6 65 6e 33 30 28 7a 50 72 6f 63 29 3b 0a 20 20 20  en30(zProc);.   
31cd7 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 7a     *pzErrMsg = z
31cd8 45 72 72 6d 73 67 20 3d 20 73 71 6c 69 74 65 33  Errmsg = sqlite3
31cd9 5f 6d 61 6c 6c 6f 63 28 6e 4d 73 67 29 3b 0a 20  _malloc(nMsg);. 
31cda 20 20 20 20 20 69 66 28 20 7a 45 72 72 6d 73 67       if( zErrmsg
31cdb 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
31cdc 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4d 73  te3_snprintf(nMs
31cdd 67 2c 20 7a 45 72 72 6d 73 67 2c 0a 20 20 20 20  g, zErrmsg,.    
31cde 20 20 20 20 20 20 20 20 22 6e 6f 20 65 6e 74 72          "no entr
31cdf 79 20 70 6f 69 6e 74 20 5b 25 73 5d 20 69 6e 20  y point [%s] in 
31ce0 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 5b  shared library [
31ce1 25 73 5d 22 2c 20 7a 50 72 6f 63 2c 7a 46 69 6c  %s]", zProc,zFil
31ce2 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e);.        sqli
31ce3 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 70 56 66  te3OsDlError(pVf
31ce4 73 2c 20 6e 4d 73 67 2d 31 2c 20 7a 45 72 72 6d  s, nMsg-1, zErrm
31ce5 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  sg);.      }.   
31ce6 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c     sqlite3OsDlCl
31ce7 6f 73 65 28 70 56 66 73 2c 20 68 61 6e 64 6c 65  ose(pVfs, handle
31ce8 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
31ce9 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
31cea 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 49  ;.  }else if( xI
31ceb 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 6d 73 67  nit(db, &zErrmsg
31cec 2c 20 26 73 71 6c 69 74 65 33 41 70 69 73 29 20  , &sqlite3Apis) 
31ced 29 7b 0a 20 20 20 20 69 66 28 20 70 7a 45 72 72  ){.    if( pzErr
31cee 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a  Msg ){.      *pz
31cef 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
31cf0 5f 6d 70 72 69 6e 74 66 28 22 65 72 72 6f 72 20  _mprintf("error 
31cf1 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a  during initializ
31cf2 61 74 69 6f 6e 3a 20 25 73 22 2c 20 7a 45 72 72  ation: %s", zErr
31cf3 6d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  msg);.    }.    
31cf4 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72  sqlite3_free(zEr
31cf5 72 6d 73 67 29 3b 0a 20 20 20 20 73 71 6c 69 74  rmsg);.    sqlit
31cf6 65 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66 73  e3OsDlClose(pVfs
31cf7 2c 20 68 61 6e 64 6c 65 29 3b 0a 20 20 20 20 72  , handle);.    r
31cf8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
31cf9 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 70  OR;.  }..  /* Ap
31cfa 70 65 6e 64 20 74 68 65 20 6e 65 77 20 73 68 61  pend the new sha
31cfb 72 65 64 20 6c 69 62 72 61 72 79 20 68 61 6e 64  red library hand
31cfc 6c 65 20 74 6f 20 74 68 65 20 64 62 2d 3e 61 45  le to the db->aE
31cfd 78 74 65 6e 73 69 6f 6e 20 61 72 72 61 79 2e 20  xtension array. 
31cfe 2a 2f 0a 20 20 61 48 61 6e 64 6c 65 20 3d 20 73  */.  aHandle = s
31cff 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
31d00 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 68 61  ro(db, sizeof(ha
31d01 6e 64 6c 65 29 2a 28 64 62 2d 3e 6e 45 78 74 65  ndle)*(db->nExte
31d02 6e 73 69 6f 6e 2b 31 29 29 3b 0a 20 20 69 66 28  nsion+1));.  if(
31d03 20 61 48 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20   aHandle==0 ){. 
31d04 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
31d05 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66  _NOMEM;.  }.  if
31d06 28 20 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e  ( db->nExtension
31d07 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79  >0 ){.    memcpy
31d08 28 61 48 61 6e 64 6c 65 2c 20 64 62 2d 3e 61 45  (aHandle, db->aE
31d09 78 74 65 6e 73 69 6f 6e 2c 20 73 69 7a 65 6f 66  xtension, sizeof
31d0a 28 68 61 6e 64 6c 65 29 2a 64 62 2d 3e 6e 45 78  (handle)*db->nEx
31d0b 74 65 6e 73 69 6f 6e 29 3b 0a 20 20 7d 0a 20 20  tension);.  }.  
31d0c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
31d0d 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e  , db->aExtension
31d0e 29 3b 0a 20 20 64 62 2d 3e 61 45 78 74 65 6e 73  );.  db->aExtens
31d0f 69 6f 6e 20 3d 20 61 48 61 6e 64 6c 65 3b 0a 0a  ion = aHandle;..
31d10 20 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e    db->aExtension
31d11 5b 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 2b  [db->nExtension+
31d12 2b 5d 20 3d 20 68 61 6e 64 6c 65 3b 0a 20 20 72  +] = handle;.  r
31d13 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
31d14 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
31d15 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65  t sqlite3_load_e
31d16 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69  xtension(.  sqli
31d17 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
31d18 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78    /* Load the ex
31d19 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69  tension into thi
31d1a 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  s database conne
31d1b 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ction */.  const
31d1c 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20   char *zFile,   
31d1d 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
31d1e 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 63  shared library c
31d1f 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73  ontaining extens
31d20 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ion */.  const c
31d21 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f  har *zProc,    /
31d22 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20  * Entry point.  
31d23 55 73 65 20 22 73 71 6c 69 74 65 33 5f 65 78 74  Use "sqlite3_ext
31d24 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 20 69 66 20  ension_init" if 
31d25 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  0 */.  char **pz
31d26 45 72 72 4d 73 67 20 20 20 20 20 20 20 2f 2a 20  ErrMsg       /* 
31d27 50 75 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  Put error messag
31d28 65 20 68 65 72 65 20 69 66 20 6e 6f 74 20 30 20  e here if not 0 
31d29 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
31d2a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
31d2b 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
31d2c 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
31d2d 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 28 64 62  LoadExtension(db
31d2e 2c 20 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20  , zFile, zProc, 
31d2f 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 72 63 20  pzErrMsg);.  rc 
31d30 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
31d31 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69  (db, rc);.  sqli
31d32 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
31d33 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
31d34 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
31d35 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  * Call this rout
31d36 69 6e 65 20 77 68 65 6e 20 74 68 65 20 64 61 74  ine when the dat
31d37 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
31d38 20 69 73 20 63 6c 6f 73 69 6e 67 20 69 6e 20 6f   is closing in o
31d39 72 64 65 72 0a 2a 2a 20 74 6f 20 63 6c 65 61 6e  rder.** to clean
31d3a 20 75 70 20 6c 6f 61 64 65 64 20 65 78 74 65 6e   up loaded exten
31d3b 73 69 6f 6e 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sions.*/.SQLITE_
31d3c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
31d3d 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69  ite3CloseExtensi
31d3e 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ons(sqlite3 *db)
31d3f 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73  {.  int i;.  ass
31d40 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
31d41 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
31d42 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  x) );.  for(i=0;
31d43 20 69 3c 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f   i<db->nExtensio
31d44 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  n; i++){.    sql
31d45 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 64 62  ite3OsDlClose(db
31d46 2d 3e 70 56 66 73 2c 20 64 62 2d 3e 61 45 78 74  ->pVfs, db->aExt
31d47 65 6e 73 69 6f 6e 5b 69 5d 29 3b 0a 20 20 7d 0a  ension[i]);.  }.
31d48 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
31d49 64 62 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73 69  db, db->aExtensi
31d4a 6f 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e  on);.}../*.** En
31d4b 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20  able or disable 
31d4c 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  extension loadin
31d4d 67 2e 20 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f  g.  Extension lo
31d4e 61 64 69 6e 67 20 69 73 20 64 69 73 61 62 6c 65  ading is disable
31d4f 64 20 62 79 0a 2a 2a 20 64 65 66 61 75 6c 74 20  d by.** default 
31d50 73 6f 20 61 73 20 6e 6f 74 20 74 6f 20 6f 70 65  so as not to ope
31d51 6e 20 73 65 63 75 72 69 74 79 20 68 6f 6c 65 73  n security holes
31d52 20 69 6e 20 6f 6c 64 65 72 20 61 70 70 6c 69 63   in older applic
31d53 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ations..*/.SQLIT
31d54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
31d55 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78  3_enable_load_ex
31d56 74 65 6e 73 69 6f 6e 28 73 71 6c 69 74 65 33 20  tension(sqlite3 
31d57 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 7b  *db, int onoff){
31d58 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
31d59 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
31d5a 29 3b 0a 20 20 69 66 28 20 6f 6e 6f 66 66 20 29  );.  if( onoff )
31d5b 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20  {.    db->flags 
31d5c 7c 3d 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78  |= SQLITE_LoadEx
31d5d 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d 65 6c 73 65  tension;.  }else
31d5e 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20  {.    db->flags 
31d5f 26 3d 20 7e 53 51 4c 49 54 45 5f 4c 6f 61 64 45  &= ~SQLITE_LoadE
31d60 78 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d 0a 20 20  xtension;.  }.  
31d61 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
31d62 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
31d63 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
31d64 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  OK;.}..#endif /*
31d65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
31d66 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 0a  D_EXTENSION */..
31d67 2f 2a 0a 2a 2a 20 54 68 65 20 61 75 74 6f 2d 65  /*.** The auto-e
31d68 78 74 65 6e 73 69 6f 6e 20 63 6f 64 65 20 61 64  xtension code ad
31d69 64 65 64 20 72 65 67 61 72 64 6c 65 73 73 20 6f  ded regardless o
31d6a 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  f whether or not
31d6b 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 6c 6f   extension.** lo
31d6c 61 64 69 6e 67 20 69 73 20 73 75 70 70 6f 72 74  ading is support
31d6d 65 64 2e 20 20 57 65 20 6e 65 65 64 20 61 20 64  ed.  We need a d
31d6e 75 6d 6d 79 20 73 71 6c 69 74 65 33 41 70 69 73  ummy sqlite3Apis
31d6f 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 61   pointer for tha
31d70 74 0a 2a 2a 20 63 6f 64 65 20 69 66 20 72 65 67  t.** code if reg
31d71 75 6c 61 72 20 65 78 74 65 6e 73 69 6f 6e 20 6c  ular extension l
31d72 6f 61 64 69 6e 67 20 69 73 20 6e 6f 74 20 61 76  oading is not av
31d73 61 69 6c 61 62 6c 65 2e 20 20 54 68 69 73 20 69  ailable.  This i
31d74 73 20 74 68 61 74 0a 2a 2a 20 64 75 6d 6d 79 20  s that.** dummy 
31d75 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 23 69 66 64  pointer..*/.#ifd
31d76 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
31d77 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 73 74  OAD_EXTENSION.st
31d78 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
31d79 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20  e3_api_routines 
31d7a 73 71 6c 69 74 65 33 41 70 69 73 20 3d 20 7b 20  sqlite3Apis = { 
31d7b 30 20 7d 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  0 };.#endif.../*
31d7c 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
31d7d 67 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73 20 74  g object holds t
31d7e 68 65 20 6c 69 73 74 20 6f 66 20 61 75 74 6f 6d  he list of autom
31d7f 61 74 69 63 61 6c 6c 79 20 6c 6f 61 64 65 64 0a  atically loaded.
31d80 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a  ** extensions..*
31d81 2a 0a 2a 2a 20 54 68 69 73 20 6c 69 73 74 20 69  *.** This list i
31d82 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20  s shared across 
31d83 74 68 72 65 61 64 73 2e 20 20 54 68 65 20 53 51  threads.  The SQ
31d84 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
31d85 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 6d 75 74 65  C_MASTER.** mute
31d86 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77  x must be held w
31d87 68 69 6c 65 20 61 63 63 65 73 73 69 6e 67 20 74  hile accessing t
31d88 68 69 73 20 6c 69 73 74 2e 0a 2a 2f 0a 74 79 70  his list..*/.typ
31d89 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
31d8a 74 65 33 41 75 74 6f 45 78 74 4c 69 73 74 20 73  te3AutoExtList s
31d8b 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69 73  qlite3AutoExtLis
31d8c 74 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45  t;.static SQLITE
31d8d 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69  _WSD struct sqli
31d8e 74 65 33 41 75 74 6f 45 78 74 4c 69 73 74 20 7b  te3AutoExtList {
31d8f 0a 20 20 69 6e 74 20 6e 45 78 74 3b 20 20 20 20  .  int nExt;    
31d90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
31d91 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
31d92 6e 20 61 45 78 74 5b 5d 20 2a 2f 20 20 20 20 20  n aExt[] */     
31d93 20 20 20 20 20 0a 20 20 76 6f 69 64 20 28 2a 2a       .  void (**
31d94 61 45 78 74 29 28 76 6f 69 64 29 3b 20 20 20 2f  aExt)(void);   /
31d95 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  * Pointers to th
31d96 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 69 74  e extension init
31d97 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 20   functions */.} 
31d98 73 71 6c 69 74 65 33 41 75 74 6f 65 78 74 20 3d  sqlite3Autoext =
31d99 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54   { 0, 0 };../* T
31d9a 68 65 20 22 77 73 64 41 75 74 6f 65 78 74 22 20  he "wsdAutoext" 
31d9b 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c  macro will resol
31d9c 76 65 20 74 6f 20 74 68 65 20 61 75 74 6f 65 78  ve to the autoex
31d9d 74 65 6e 73 69 6f 6e 0a 2a 2a 20 73 74 61 74 65  tension.** state
31d9e 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69   vector.  If wri
31d9f 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74  table static dat
31da0 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64  a is unsupported
31da1 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a   on the target,.
31da2 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f  ** we have to lo
31da3 63 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76  cate the state v
31da4 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d  ector at run-tim
31da5 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20  e.  In the more 
31da6 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77  common.** case w
31da7 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74  here writable st
31da8 61 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70  atic data is sup
31da9 70 6f 72 74 65 64 2c 20 77 73 64 53 74 61 74 20  ported, wsdStat 
31daa 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74  can refer direct
31dab 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71  ly.** to the "sq
31dac 6c 69 74 65 33 41 75 74 6f 65 78 74 22 20 73 74  lite3Autoext" st
31dad 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61  ate vector decla
31dae 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69  red above..*/.#i
31daf 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
31db0 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73  _WSD.# define ws
31db1 64 41 75 74 6f 65 78 74 49 6e 69 74 20 5c 0a 20  dAutoextInit \. 
31db2 20 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c   sqlite3AutoExtL
31db3 69 73 74 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c  ist *x = &GLOBAL
31db4 28 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c  (sqlite3AutoExtL
31db5 69 73 74 2c 73 71 6c 69 74 65 33 41 75 74 6f 65  ist,sqlite3Autoe
31db6 78 74 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64  xt).# define wsd
31db7 41 75 74 6f 65 78 74 20 78 5b 30 5d 0a 23 65 6c  Autoext x[0].#el
31db8 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 41  se.# define wsdA
31db9 75 74 6f 65 78 74 49 6e 69 74 0a 23 20 64 65 66  utoextInit.# def
31dba 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74 20 73  ine wsdAutoext s
31dbb 71 6c 69 74 65 33 41 75 74 6f 65 78 74 0a 23 65  qlite3Autoext.#e
31dbc 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 67  ndif.../*.** Reg
31dbd 69 73 74 65 72 20 61 20 73 74 61 74 69 63 61 6c  ister a statical
31dbe 6c 79 20 6c 69 6e 6b 65 64 20 65 78 74 65 6e 73  ly linked extens
31dbf 69 6f 6e 20 74 68 61 74 20 69 73 20 61 75 74 6f  ion that is auto
31dc0 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 6c 6f 61  matically.** loa
31dc1 64 65 64 20 62 79 20 65 76 65 72 79 20 6e 65 77  ded by every new
31dc2 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
31dc3 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
31dc4 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
31dc5 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76  auto_extension(v
31dc6 6f 69 64 20 28 2a 78 49 6e 69 74 29 28 76 6f 69  oid (*xInit)(voi
31dc7 64 29 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  d)){.  int rc = 
31dc8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 6e 64  SQLITE_OK;.#ifnd
31dc9 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
31dca 55 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20 73  UTOINIT.  rc = s
31dcb 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
31dcc 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  e();.  if( rc ){
31dcd 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
31dce 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
31dcf 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 23 69   {.    int i;.#i
31dd0 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
31dd1 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  AFE.    sqlite3_
31dd2 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73  mutex *mutex = s
31dd3 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
31dd4 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
31dd5 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65  ATIC_MASTER);.#e
31dd6 6e 64 69 66 0a 20 20 20 20 77 73 64 41 75 74 6f  ndif.    wsdAuto
31dd7 65 78 74 49 6e 69 74 3b 0a 20 20 20 20 73 71 6c  extInit;.    sql
31dd8 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
31dd9 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 66 6f 72  (mutex);.    for
31dda 28 69 3d 30 3b 20 69 3c 77 73 64 41 75 74 6f 65  (i=0; i<wsdAutoe
31ddb 78 74 2e 6e 45 78 74 3b 20 69 2b 2b 29 7b 0a 20  xt.nExt; i++){. 
31ddc 20 20 20 20 20 69 66 28 20 77 73 64 41 75 74 6f       if( wsdAuto
31ddd 65 78 74 2e 61 45 78 74 5b 69 5d 3d 3d 78 49 6e  ext.aExt[i]==xIn
31dde 69 74 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  it ) break;.    
31ddf 7d 0a 20 20 20 20 69 66 28 20 69 3d 3d 77 73 64  }.    if( i==wsd
31de0 41 75 74 6f 65 78 74 2e 6e 45 78 74 20 29 7b 0a  Autoext.nExt ){.
31de1 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20        int nByte 
31de2 3d 20 28 77 73 64 41 75 74 6f 65 78 74 2e 6e 45  = (wsdAutoext.nE
31de3 78 74 2b 31 29 2a 73 69 7a 65 6f 66 28 77 73 64  xt+1)*sizeof(wsd
31de4 41 75 74 6f 65 78 74 2e 61 45 78 74 5b 30 5d 29  Autoext.aExt[0])
31de5 3b 0a 20 20 20 20 20 20 76 6f 69 64 20 28 2a 2a  ;.      void (**
31de6 61 4e 65 77 29 28 76 6f 69 64 29 3b 0a 20 20 20  aNew)(void);.   
31de7 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65     aNew = sqlite
31de8 33 5f 72 65 61 6c 6c 6f 63 28 77 73 64 41 75 74  3_realloc(wsdAut
31de9 6f 65 78 74 2e 61 45 78 74 2c 20 6e 42 79 74 65  oext.aExt, nByte
31dea 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61 4e 65  );.      if( aNe
31deb 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  w==0 ){.        
31dec 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
31ded 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  M;.      }else{.
31dee 20 20 20 20 20 20 20 20 77 73 64 41 75 74 6f 65          wsdAutoe
31def 78 74 2e 61 45 78 74 20 3d 20 61 4e 65 77 3b 0a  xt.aExt = aNew;.
31df0 20 20 20 20 20 20 20 20 77 73 64 41 75 74 6f 65          wsdAutoe
31df1 78 74 2e 61 45 78 74 5b 77 73 64 41 75 74 6f 65  xt.aExt[wsdAutoe
31df2 78 74 2e 6e 45 78 74 5d 20 3d 20 78 49 6e 69 74  xt.nExt] = xInit
31df3 3b 0a 20 20 20 20 20 20 20 20 77 73 64 41 75 74  ;.        wsdAut
31df4 6f 65 78 74 2e 6e 45 78 74 2b 2b 3b 0a 20 20 20  oext.nExt++;.   
31df5 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
31df6 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
31df7 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 61  ve(mutex);.    a
31df8 73 73 65 72 74 28 20 28 72 63 26 30 78 66 66 29  ssert( (rc&0xff)
31df9 3d 3d 72 63 20 29 3b 0a 20 20 20 20 72 65 74 75  ==rc );.    retu
31dfa 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  rn rc;.  }.}../*
31dfb 0a 2a 2a 20 52 65 73 65 74 20 74 68 65 20 61 75  .** Reset the au
31dfc 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f  tomatic extensio
31dfd 6e 20 6c 6f 61 64 69 6e 67 20 6d 65 63 68 61 6e  n loading mechan
31dfe 69 73 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ism..*/.SQLITE_A
31dff 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
31e00 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e  reset_auto_exten
31e01 73 69 6f 6e 28 76 6f 69 64 29 7b 0a 23 69 66 6e  sion(void){.#ifn
31e02 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
31e03 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73  AUTOINIT.  if( s
31e04 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
31e05 65 28 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  e()==SQLITE_OK )
31e06 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 23 69 66 20  .#endif.  {.#if 
31e07 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
31e08 45 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  E.    sqlite3_mu
31e09 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c  tex *mutex = sql
31e0a 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
31e0b 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
31e0c 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64  IC_MASTER);.#end
31e0d 69 66 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78  if.    wsdAutoex
31e0e 74 49 6e 69 74 3b 0a 20 20 20 20 73 71 6c 69 74  tInit;.    sqlit
31e0f 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
31e10 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74  utex);.    sqlit
31e11 65 33 5f 66 72 65 65 28 77 73 64 41 75 74 6f 65  e3_free(wsdAutoe
31e12 78 74 2e 61 45 78 74 29 3b 0a 20 20 20 20 77 73  xt.aExt);.    ws
31e13 64 41 75 74 6f 65 78 74 2e 61 45 78 74 20 3d 20  dAutoext.aExt = 
31e14 30 3b 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78  0;.    wsdAutoex
31e15 74 2e 6e 45 78 74 20 3d 20 30 3b 0a 20 20 20 20  t.nExt = 0;.    
31e16 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
31e17 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  ave(mutex);.  }.
31e18 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 61 6c  }../*.** Load al
31e19 6c 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65  l automatic exte
31e1a 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  nsions..**.** If
31e1b 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77   anything goes w
31e1c 72 6f 6e 67 2c 20 73 65 74 20 61 6e 20 65 72 72  rong, set an err
31e1d 6f 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  or in the databa
31e1e 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
31e1f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
31e20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
31e21 6f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28  oLoadExtensions(
31e22 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
31e23 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 67 6f 20  int i;.  int go 
31e24 3d 20 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  = 1;.  int rc;. 
31e25 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 73 71   int (*xInit)(sq
31e26 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f  lite3*,char**,co
31e27 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nst sqlite3_api_
31e28 72 6f 75 74 69 6e 65 73 2a 29 3b 0a 0a 20 20 77  routines*);..  w
31e29 73 64 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20  sdAutoextInit;. 
31e2a 20 69 66 28 20 77 73 64 41 75 74 6f 65 78 74 2e   if( wsdAutoext.
31e2b 6e 45 78 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  nExt==0 ){.    /
31e2c 2a 20 43 6f 6d 6d 6f 6e 20 63 61 73 65 3a 20 65  * Common case: e
31e2d 61 72 6c 79 20 6f 75 74 20 77 69 74 68 6f 75 74  arly out without
31e2e 20 65 76 65 72 79 20 68 61 76 69 6e 67 20 74 6f   every having to
31e2f 20 61 63 71 75 69 72 65 20 61 20 6d 75 74 65 78   acquire a mutex
31e30 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   */.    return;.
31e31 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 67    }.  for(i=0; g
31e32 6f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61  o; i++){.    cha
31e33 72 20 2a 7a 45 72 72 6d 73 67 3b 0a 23 69 66 20  r *zErrmsg;.#if 
31e34 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
31e35 45 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  E.    sqlite3_mu
31e36 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c  tex *mutex = sql
31e37 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
31e38 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
31e39 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64  IC_MASTER);.#end
31e3a 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  if.    sqlite3_m
31e3b 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
31e3c 29 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d 77 73  );.    if( i>=ws
31e3d 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 20 29 7b  dAutoext.nExt ){
31e3e 0a 20 20 20 20 20 20 78 49 6e 69 74 20 3d 20 30  .      xInit = 0
31e3f 3b 0a 20 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a  ;.      go = 0;.
31e40 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
31e41 20 78 49 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29   xInit = (int(*)
31e42 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a  (sqlite3*,char**
31e43 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61  ,const sqlite3_a
31e44 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 29 0a 20  pi_routines*)). 
31e45 20 20 20 20 20 20 20 20 20 20 20 20 20 77 73 64               wsd
31e46 41 75 74 6f 65 78 74 2e 61 45 78 74 5b 69 5d 3b  Autoext.aExt[i];
31e47 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
31e48 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
31e49 75 74 65 78 29 3b 0a 20 20 20 20 7a 45 72 72 6d  utex);.    zErrm
31e4a 73 67 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  sg = 0;.    if( 
31e4b 78 49 6e 69 74 20 26 26 20 28 72 63 20 3d 20 78  xInit && (rc = x
31e4c 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 6d 73  Init(db, &zErrms
31e4d 67 2c 20 26 73 71 6c 69 74 65 33 41 70 69 73 29  g, &sqlite3Apis)
31e4e 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )!=0 ){.      sq
31e4f 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72  lite3Error(db, r
31e50 63 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 22  c,.            "
31e51 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73  automatic extens
31e52 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 66 61 69 6c  ion loading fail
31e53 65 64 3a 20 25 73 22 2c 20 7a 45 72 72 6d 73 67  ed: %s", zErrmsg
31e54 29 3b 0a 20 20 20 20 20 20 67 6f 20 3d 20 30 3b  );.      go = 0;
31e55 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
31e56 65 33 5f 66 72 65 65 28 7a 45 72 72 6d 73 67 29  e3_free(zErrmsg)
31e57 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  ;.  }.}../******
31e58 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
31e59 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a  loadext.c ******
31e5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31e5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31e5c 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
31e5d 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
31e5e 69 6c 65 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a  ile pragma.c ***
31e5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31e61 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
31e62 30 30 33 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a  003 April 6.**.*
31e63 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
31e64 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
31e65 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
31e66 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
31e67 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
31e68 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
31e69 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
31e6a 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
31e6b 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
31e6c 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
31e6d 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
31e6e 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
31e6f 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
31e70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
31e71 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
31e72 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
31e73 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
31e74 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
31e75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31e76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31e77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31e78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
31e79 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
31e7a 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d   code used to im
31e7b 70 6c 65 6d 65 6e 74 20 74 68 65 20 50 52 41 47  plement the PRAG
31e7c 4d 41 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 0a  MA command..*/..
31e7d 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  /*.** Interpret 
31e7e 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67  the given string
31e7f 20 61 73 20 61 20 73 61 66 65 74 79 20 6c 65 76   as a safety lev
31e80 65 6c 2e 20 20 52 65 74 75 72 6e 20 30 20 66 6f  el.  Return 0 fo
31e81 72 20 4f 46 46 2c 0a 2a 2a 20 31 20 66 6f 72 20  r OFF,.** 1 for 
31e82 4f 4e 20 6f 72 20 4e 4f 52 4d 41 4c 20 61 6e 64  ON or NORMAL and
31e83 20 32 20 66 6f 72 20 46 55 4c 4c 2e 20 20 52 65   2 for FULL.  Re
31e84 74 75 72 6e 20 31 20 66 6f 72 20 61 6e 20 65 6d  turn 1 for an em
31e85 70 74 79 20 6f 72 20 0a 2a 2a 20 75 6e 72 65 63  pty or .** unrec
31e86 6f 67 6e 69 7a 65 64 20 73 74 72 69 6e 67 20 61  ognized string a
31e87 72 67 75 6d 65 6e 74 2e 20 20 54 68 65 20 46 55  rgument.  The FU
31e88 4c 4c 20 6f 70 74 69 6f 6e 20 69 73 20 64 69 73  LL option is dis
31e89 61 6c 6c 6f 77 65 64 0a 2a 2a 20 69 66 20 74 68  allowed.** if th
31e8a 65 20 6f 6d 69 74 46 75 6c 6c 20 70 61 72 61 6d  e omitFull param
31e8b 65 74 65 72 20 69 74 20 31 2e 0a 2a 2a 0a 2a 2a  eter it 1..**.**
31e8c 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 76   Note that the v
31e8d 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 61  alues returned a
31e8e 72 65 20 6f 6e 65 20 6c 65 73 73 20 74 68 61 74  re one less that
31e8f 20 74 68 65 20 76 61 6c 75 65 73 20 74 68 61 74   the values that
31e90 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 70 61  .** should be pa
31e91 73 73 65 64 20 69 6e 74 6f 20 73 71 6c 69 74 65  ssed into sqlite
31e92 33 42 74 72 65 65 53 65 74 53 61 66 65 74 79 4c  3BtreeSetSafetyL
31e93 65 76 65 6c 28 29 2e 20 20 54 68 65 20 69 73 20  evel().  The is 
31e94 64 6f 6e 65 0a 2a 2a 20 74 6f 20 73 75 70 70 6f  done.** to suppo
31e95 72 74 20 6c 65 67 61 63 79 20 53 51 4c 20 63 6f  rt legacy SQL co
31e96 64 65 2e 20 20 54 68 65 20 73 61 66 65 74 79 20  de.  The safety 
31e97 6c 65 76 65 6c 20 75 73 65 64 20 74 6f 20 62 65  level used to be
31e98 20 62 6f 6f 6c 65 61 6e 0a 2a 2a 20 61 6e 64 20   boolean.** and 
31e99 6f 6c 64 65 72 20 73 63 72 69 70 74 73 20 6d 61  older scripts ma
31e9a 79 20 68 61 76 65 20 75 73 65 64 20 6e 75 6d 62  y have used numb
31e9b 65 72 73 20 30 20 66 6f 72 20 4f 46 46 20 61 6e  ers 0 for OFF an
31e9c 64 20 31 20 66 6f 72 20 4f 4e 2e 0a 2a 2f 0a 73  d 1 for ON..*/.s
31e9d 74 61 74 69 63 20 75 38 20 67 65 74 53 61 66 65  tatic u8 getSafe
31e9e 74 79 4c 65 76 65 6c 28 63 6f 6e 73 74 20 63 68  tyLevel(const ch
31e9f 61 72 20 2a 7a 2c 20 69 6e 74 20 6f 6d 69 74 46  ar *z, int omitF
31ea0 75 6c 6c 2c 20 69 6e 74 20 64 66 6c 74 29 7b 0a  ull, int dflt){.
31ea1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31ea2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31ea3 31 32 33 34 35 36 37 38 39 20 31 32 33 34 35 36  123456789 123456
31ea4 37 38 39 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  789 */.  static 
31ea5 63 6f 6e 73 74 20 63 68 61 72 20 7a 54 65 78 74  const char zText
31ea6 5b 5d 20 3d 20 22 6f 6e 6f 66 66 61 6c 73 65 79  [] = "onoffalsey
31ea7 65 73 74 72 75 65 66 75 6c 6c 22 3b 0a 20 20 73  estruefull";.  s
31ea8 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 69  tatic const u8 i
31ea9 4f 66 66 73 65 74 5b 5d 20 3d 20 7b 30 2c 20 31  Offset[] = {0, 1
31eaa 2c 20 32 2c 20 34 2c 20 39 2c 20 31 32 2c 20 31  , 2, 4, 9, 12, 1
31eab 36 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  6};.  static con
31eac 73 74 20 75 38 20 69 4c 65 6e 67 74 68 5b 5d 20  st u8 iLength[] 
31ead 3d 20 7b 32 2c 20 32 2c 20 33 2c 20 35 2c 20 33  = {2, 2, 3, 5, 3
31eae 2c 20 34 2c 20 34 7d 3b 0a 20 20 73 74 61 74 69  , 4, 4};.  stati
31eaf 63 20 63 6f 6e 73 74 20 75 38 20 69 56 61 6c 75  c const u8 iValu
31eb0 65 5b 5d 20 3d 20 20 7b 31 2c 20 30 2c 20 30 2c  e[] =  {1, 0, 0,
31eb1 20 30 2c 20 31 2c 20 31 2c 20 32 7d 3b 0a 20 20   0, 1, 1, 2};.  
31eb2 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20  int i, n;.  if( 
31eb3 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a  sqlite3Isdigit(*
31eb4 7a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  z) ){.    return
31eb5 20 28 75 38 29 73 71 6c 69 74 65 33 41 74 6f 69   (u8)sqlite3Atoi
31eb6 28 7a 29 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73  (z);.  }.  n = s
31eb7 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
31eb8 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
31eb9 41 72 72 61 79 53 69 7a 65 28 69 4c 65 6e 67 74  ArraySize(iLengt
31eba 68 29 2d 6f 6d 69 74 46 75 6c 6c 3b 20 69 2b 2b  h)-omitFull; i++
31ebb 29 7b 0a 20 20 20 20 69 66 28 20 69 4c 65 6e 67  ){.    if( iLeng
31ebc 74 68 5b 69 5d 3d 3d 6e 20 26 26 20 73 71 6c 69  th[i]==n && sqli
31ebd 74 65 33 53 74 72 4e 49 43 6d 70 28 26 7a 54 65  te3StrNICmp(&zTe
31ebe 78 74 5b 69 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a  xt[iOffset[i]],z
31ebf 2c 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ,n)==0 ){.      
31ec0 72 65 74 75 72 6e 20 69 56 61 6c 75 65 5b 69 5d  return iValue[i]
31ec1 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
31ec2 74 75 72 6e 20 64 66 6c 74 3b 0a 7d 0a 0a 2f 2a  turn dflt;.}../*
31ec3 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68  .** Interpret th
31ec4 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 61  e given string a
31ec5 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c 75  s a boolean valu
31ec6 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
31ec7 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 47  VATE u8 sqlite3G
31ec8 65 74 42 6f 6f 6c 65 61 6e 28 63 6f 6e 73 74 20  etBoolean(const 
31ec9 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 64 66 6c  char *z, int dfl
31eca 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 67 65 74  t){.  return get
31ecb 53 61 66 65 74 79 4c 65 76 65 6c 28 7a 2c 31 2c  SafetyLevel(z,1,
31ecc 64 66 6c 74 29 21 3d 30 3b 0a 7d 0a 0a 2f 2a 20  dflt)!=0;.}../* 
31ecd 54 68 65 20 73 71 6c 69 74 65 33 47 65 74 42 6f  The sqlite3GetBo
31ece 6f 6c 65 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e  olean() function
31ecf 20 69 73 20 75 73 65 64 20 62 79 20 6f 74 68 65   is used by othe
31ed0 72 20 6d 6f 64 75 6c 65 73 20 62 75 74 20 74 68  r modules but th
31ed1 65 0a 2a 2a 20 72 65 6d 61 69 6e 64 65 72 20 6f  e.** remainder o
31ed2 66 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 73  f this file is s
31ed3 70 65 63 69 66 69 63 20 74 6f 20 50 52 41 47 4d  pecific to PRAGM
31ed4 41 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 53  A processing.  S
31ed5 6f 20 6f 6d 69 74 0a 2a 2a 20 74 68 65 20 72 65  o omit.** the re
31ed6 73 74 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  st of the file i
31ed7 66 20 50 52 41 47 4d 41 73 20 61 72 65 20 6f 6d  f PRAGMAs are om
31ed8 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 62  itted from the b
31ed9 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  uild..*/.#if !de
31eda 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
31edb 54 5f 50 52 41 47 4d 41 29 0a 0a 2f 2a 0a 2a 2a  T_PRAGMA)../*.**
31edc 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67   Interpret the g
31edd 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61  iven string as a
31ede 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 76 61   locking mode va
31edf 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lue..*/.static i
31ee0 6e 74 20 67 65 74 4c 6f 63 6b 69 6e 67 4d 6f 64  nt getLockingMod
31ee1 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  e(const char *z)
31ee2 7b 0a 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20  {.  if( z ){.   
31ee3 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53   if( 0==sqlite3S
31ee4 74 72 49 43 6d 70 28 7a 2c 20 22 65 78 63 6c 75  trICmp(z, "exclu
31ee5 73 69 76 65 22 29 20 29 20 72 65 74 75 72 6e 20  sive") ) return 
31ee6 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
31ee7 45 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 20  E_EXCLUSIVE;.   
31ee8 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53   if( 0==sqlite3S
31ee9 74 72 49 43 6d 70 28 7a 2c 20 22 6e 6f 72 6d 61  trICmp(z, "norma
31eea 6c 22 29 20 29 20 72 65 74 75 72 6e 20 50 41 47  l") ) return PAG
31eeb 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e  ER_LOCKINGMODE_N
31eec 4f 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 72 65 74  ORMAL;.  }.  ret
31eed 75 72 6e 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  urn PAGER_LOCKIN
31eee 47 4d 4f 44 45 5f 51 55 45 52 59 3b 0a 7d 0a 0a  GMODE_QUERY;.}..
31eef 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
31ef0 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f  MIT_AUTOVACUUM./
31ef1 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74  *.** Interpret t
31ef2 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20  he given string 
31ef3 61 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75  as an auto-vacuu
31ef4 6d 20 6d 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2a  m mode value..**
31ef5 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
31ef6 67 20 73 74 72 69 6e 67 73 2c 20 22 6e 6f 6e 65  g strings, "none
31ef7 22 2c 20 22 66 75 6c 6c 22 20 61 6e 64 20 22 69  ", "full" and "i
31ef8 6e 63 72 65 6d 65 6e 74 61 6c 22 20 61 72 65 20  ncremental" are 
31ef9 0a 2a 2a 20 61 63 63 65 70 74 61 62 6c 65 2c 20  .** acceptable, 
31efa 61 73 20 61 72 65 20 74 68 65 69 72 20 6e 75 6d  as are their num
31efb 65 72 69 63 20 65 71 75 69 76 61 6c 65 6e 74 73  eric equivalents
31efc 3a 20 30 2c 20 31 20 61 6e 64 20 32 20 72 65 73  : 0, 1 and 2 res
31efd 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a 73 74  pectively..*/.st
31efe 61 74 69 63 20 69 6e 74 20 67 65 74 41 75 74 6f  atic int getAuto
31eff 56 61 63 75 75 6d 28 63 6f 6e 73 74 20 63 68 61  Vacuum(const cha
31f00 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  r *z){.  int i;.
31f01 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33    if( 0==sqlite3
31f02 53 74 72 49 43 6d 70 28 7a 2c 20 22 6e 6f 6e 65  StrICmp(z, "none
31f03 22 29 20 29 20 72 65 74 75 72 6e 20 42 54 52 45  ") ) return BTRE
31f04 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e  E_AUTOVACUUM_NON
31f05 45 3b 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69  E;.  if( 0==sqli
31f06 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 66  te3StrICmp(z, "f
31f07 75 6c 6c 22 29 20 29 20 72 65 74 75 72 6e 20 42  ull") ) return B
31f08 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
31f09 46 55 4c 4c 3b 0a 20 20 69 66 28 20 30 3d 3d 73  FULL;.  if( 0==s
31f0a 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c  qlite3StrICmp(z,
31f0b 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22 29 20   "incremental") 
31f0c 29 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41  ) return BTREE_A
31f0d 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 3b 0a  UTOVACUUM_INCR;.
31f0e 20 20 69 20 3d 20 73 71 6c 69 74 65 33 41 74 6f    i = sqlite3Ato
31f0f 69 28 7a 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  i(z);.  return (
31f10 75 38 29 28 28 69 3e 3d 30 26 26 69 3c 3d 32 29  u8)((i>=0&&i<=2)
31f11 3f 69 3a 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  ?i:0);.}.#endif 
31f12 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /* ifndef SQLITE
31f13 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
31f14 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
31f15 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
31f16 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 6e 74  RAGMAS./*.** Int
31f17 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e  erpret the given
31f18 20 73 74 72 69 6e 67 20 61 73 20 61 20 74 65 6d   string as a tem
31f19 70 20 64 62 20 6c 6f 63 61 74 69 6f 6e 2e 20 52  p db location. R
31f1a 65 74 75 72 6e 20 31 20 66 6f 72 20 66 69 6c 65  eturn 1 for file
31f1b 0a 2a 2a 20 62 61 63 6b 65 64 20 74 65 6d 70 6f  .** backed tempo
31f1c 72 61 72 79 20 64 61 74 61 62 61 73 65 73 2c 20  rary databases, 
31f1d 32 20 66 6f 72 20 74 68 65 20 52 65 64 2d 42 6c  2 for the Red-Bl
31f1e 61 63 6b 20 74 72 65 65 20 69 6e 20 6d 65 6d 6f  ack tree in memo
31f1f 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 61  ry database.** a
31f20 6e 64 20 30 20 74 6f 20 75 73 65 20 74 68 65 20  nd 0 to use the 
31f21 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 64 65 66  compile-time def
31f22 61 75 6c 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ault..*/.static 
31f23 69 6e 74 20 67 65 74 54 65 6d 70 53 74 6f 72 65  int getTempStore
31f24 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
31f25 0a 20 20 69 66 28 20 7a 5b 30 5d 3e 3d 27 30 27  .  if( z[0]>='0'
31f26 20 26 26 20 7a 5b 30 5d 3c 3d 27 32 27 20 29 7b   && z[0]<='2' ){
31f27 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 5b 30 5d  .    return z[0]
31f28 20 2d 20 27 30 27 3b 0a 20 20 7d 65 6c 73 65 20   - '0';.  }else 
31f29 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
31f2a 6d 70 28 7a 2c 20 22 66 69 6c 65 22 29 3d 3d 30  mp(z, "file")==0
31f2b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   ){.    return 1
31f2c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
31f2d 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
31f2e 22 6d 65 6d 6f 72 79 22 29 3d 3d 30 20 29 7b 0a  "memory")==0 ){.
31f2f 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
31f30 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
31f31 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  n 0;.  }.}.#endi
31f32 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47 45  f /* SQLITE_PAGE
31f33 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69  R_PRAGMAS */..#i
31f34 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
31f35 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a  T_PAGER_PRAGMAS.
31f36 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65  /*.** Invalidate
31f37 20 74 65 6d 70 20 73 74 6f 72 61 67 65 2c 20 65   temp storage, e
31f38 69 74 68 65 72 20 77 68 65 6e 20 74 68 65 20 74  ither when the t
31f39 65 6d 70 20 73 74 6f 72 61 67 65 20 69 73 20 63  emp storage is c
31f3a 68 61 6e 67 65 64 0a 2a 2a 20 66 72 6f 6d 20 64  hanged.** from d
31f3b 65 66 61 75 6c 74 2c 20 6f 72 20 77 68 65 6e 20  efault, or when 
31f3c 27 66 69 6c 65 27 20 61 6e 64 20 74 68 65 20 74  'file' and the t
31f3d 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74  emp_store_direct
31f3e 6f 72 79 20 68 61 73 20 63 68 61 6e 67 65 64 0a  ory has changed.
31f3f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e  */.static int in
31f40 76 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72  validateTempStor
31f41 61 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  age(Parse *pPars
31f42 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  e){.  sqlite3 *d
31f43 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
31f44 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d    if( db->aDb[1]
31f45 2e 70 42 74 21 3d 30 20 29 7b 0a 20 20 20 20 69  .pBt!=0 ){.    i
31f46 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  f( !db->autoComm
31f47 69 74 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72  it || sqlite3Btr
31f48 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28  eeIsInReadTrans(
31f49 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 20  db->aDb[1].pBt) 
31f4a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
31f4b 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
31f4c 20 22 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72   "temporary stor
31f4d 61 67 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 68  age cannot be ch
31f4e 61 6e 67 65 64 20 22 0a 20 20 20 20 20 20 20 20  anged ".        
31f4f 22 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 74  "from within a t
31f50 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20  ransaction");.  
31f51 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
31f52 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
31f53 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
31f54 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b 31 5d 2e  lose(db->aDb[1].
31f55 70 42 74 29 3b 0a 20 20 20 20 64 62 2d 3e 61 44  pBt);.    db->aD
31f56 62 5b 31 5d 2e 70 42 74 20 3d 20 30 3b 0a 20 20  b[1].pBt = 0;.  
31f57 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 41 6c    sqlite3ResetAl
31f58 6c 53 63 68 65 6d 61 73 4f 66 43 6f 6e 6e 65 63  lSchemasOfConnec
31f59 74 69 6f 6e 28 64 62 29 3b 0a 20 20 7d 0a 20 20  tion(db);.  }.  
31f5a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
31f5b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
31f5c 4c 49 54 45 5f 50 41 47 45 52 5f 50 52 41 47 4d  LITE_PAGER_PRAGM
31f5d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  AS */..#ifndef S
31f5e 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
31f5f 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49  _PRAGMAS./*.** I
31f60 66 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62  f the TEMP datab
31f61 61 73 65 20 69 73 20 6f 70 65 6e 2c 20 63 6c 6f  ase is open, clo
31f62 73 65 20 69 74 20 61 6e 64 20 6d 61 72 6b 20 74  se it and mark t
31f63 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
31f64 6d 61 0a 2a 2a 20 61 73 20 6e 65 65 64 69 6e 67  ma.** as needing
31f65 20 72 65 6c 6f 61 64 69 6e 67 2e 20 20 54 68 69   reloading.  Thi
31f66 73 20 6d 75 73 74 20 62 65 20 64 6f 6e 65 20 77  s must be done w
31f67 68 65 6e 20 75 73 69 6e 67 20 74 68 65 20 53 51  hen using the SQ
31f68 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 0a  LITE_TEMP_STORE.
31f69 2a 2a 20 6f 72 20 44 45 46 41 55 4c 54 5f 54 45  ** or DEFAULT_TE
31f6a 4d 50 5f 53 54 4f 52 45 20 70 72 61 67 6d 61 73  MP_STORE pragmas
31f6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
31f6c 63 68 61 6e 67 65 54 65 6d 70 53 74 6f 72 61 67  changeTempStorag
31f6d 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
31f6e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74   const char *zSt
31f6f 6f 72 61 67 65 54 79 70 65 29 7b 0a 20 20 69 6e  orageType){.  in
31f70 74 20 74 73 20 3d 20 67 65 74 54 65 6d 70 53 74  t ts = getTempSt
31f71 6f 72 65 28 7a 53 74 6f 72 61 67 65 54 79 70 65  ore(zStorageType
31f72 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  );.  sqlite3 *db
31f73 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
31f74 20 69 66 28 20 64 62 2d 3e 74 65 6d 70 5f 73 74   if( db->temp_st
31f75 6f 72 65 3d 3d 74 73 20 29 20 72 65 74 75 72 6e  ore==ts ) return
31f76 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
31f77 28 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70  ( invalidateTemp
31f78 53 74 6f 72 61 67 65 28 20 70 50 61 72 73 65 20  Storage( pParse 
31f79 29 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29  ) != SQLITE_OK )
31f7a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
31f7b 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
31f7c 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20   db->temp_store 
31f7d 3d 20 28 75 38 29 74 73 3b 0a 20 20 72 65 74 75  = (u8)ts;.  retu
31f7e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
31f7f 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
31f80 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a  _PAGER_PRAGMAS *
31f81 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  /../*.** Generat
31f82 65 20 63 6f 64 65 20 74 6f 20 72 65 74 75 72 6e  e code to return
31f83 20 61 20 73 69 6e 67 6c 65 20 69 6e 74 65 67 65   a single intege
31f84 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74  r value..*/.stat
31f85 69 63 20 76 6f 69 64 20 72 65 74 75 72 6e 53 69  ic void returnSi
31f86 6e 67 6c 65 49 6e 74 28 50 61 72 73 65 20 2a 70  ngleInt(Parse *p
31f87 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
31f88 72 20 2a 7a 4c 61 62 65 6c 2c 20 69 36 34 20 76  r *zLabel, i64 v
31f89 61 6c 75 65 29 7b 0a 20 20 56 64 62 65 20 2a 76  alue){.  Vdbe *v
31f8a 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
31f8b 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74  e(pParse);.  int
31f8c 20 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d   mem = ++pParse-
31f8d 3e 6e 4d 65 6d 3b 0a 20 20 69 36 34 20 2a 70 49  >nMem;.  i64 *pI
31f8e 36 34 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  64 = sqlite3DbMa
31f8f 6c 6c 6f 63 52 61 77 28 70 50 61 72 73 65 2d 3e  llocRaw(pParse->
31f90 64 62 2c 20 73 69 7a 65 6f 66 28 76 61 6c 75 65  db, sizeof(value
31f91 29 29 3b 0a 20 20 69 66 28 20 70 49 36 34 20 29  ));.  if( pI64 )
31f92 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 49 36  {.    memcpy(pI6
31f93 34 2c 20 26 76 61 6c 75 65 2c 20 73 69 7a 65 6f  4, &value, sizeo
31f94 66 28 76 61 6c 75 65 29 29 3b 0a 20 20 7d 0a 20  f(value));.  }. 
31f95 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
31f96 70 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c 20  p4(v, OP_Int64, 
31f97 30 2c 20 6d 65 6d 2c 20 30 2c 20 28 63 68 61 72  0, mem, 0, (char
31f98 2a 29 70 49 36 34 2c 20 50 34 5f 49 4e 54 36 34  *)pI64, P4_INT64
31f99 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
31f9a 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
31f9b 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53  ;.  sqlite3VdbeS
31f9c 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
31f9d 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c  COLNAME_NAME, zL
31f9e 61 62 65 6c 2c 20 53 51 4c 49 54 45 5f 53 54 41  abel, SQLITE_STA
31f9f 54 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  TIC);.  sqlite3V
31fa0 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
31fa1 52 65 73 75 6c 74 52 6f 77 2c 20 6d 65 6d 2c 20  ResultRow, mem, 
31fa2 31 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  1);.}..#ifndef S
31fa3 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f  QLITE_OMIT_FLAG_
31fa4 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 43 68  PRAGMAS./*.** Ch
31fa5 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 7a 52  eck to see if zR
31fa6 69 67 68 74 20 61 6e 64 20 7a 4c 65 66 74 20 72  ight and zLeft r
31fa7 65 66 65 72 20 74 6f 20 61 20 70 72 61 67 6d 61  efer to a pragma
31fa8 20 74 68 61 74 20 71 75 65 72 69 65 73 0a 2a 2a   that queries.**
31fa9 20 6f 72 20 63 68 61 6e 67 65 73 20 6f 6e 65 20   or changes one 
31faa 6f 66 20 74 68 65 20 66 6c 61 67 73 20 69 6e 20  of the flags in 
31fab 64 62 2d 3e 66 6c 61 67 73 2e 20 20 52 65 74 75  db->flags.  Retu
31fac 72 6e 20 31 20 69 66 20 73 6f 20 61 6e 64 20 30  rn 1 if so and 0
31fad 20 69 66 20 6e 6f 74 2e 0a 2a 2a 20 41 6c 73 6f   if not..** Also
31fae 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  , implement the 
31faf 70 72 61 67 6d 61 2e 0a 2a 2f 0a 73 74 61 74 69  pragma..*/.stati
31fb0 63 20 69 6e 74 20 66 6c 61 67 50 72 61 67 6d 61  c int flagPragma
31fb1 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
31fb2 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66  const char *zLef
31fb3 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  t, const char *z
31fb4 52 69 67 68 74 29 7b 0a 20 20 73 74 61 74 69 63  Right){.  static
31fb5 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 50   const struct sP
31fb6 72 61 67 6d 61 54 79 70 65 20 7b 0a 20 20 20 20  ragmaType {.    
31fb7 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
31fb8 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  e;  /* Name of t
31fb9 68 65 20 70 72 61 67 6d 61 20 2a 2f 0a 20 20 20  he pragma */.   
31fba 20 69 6e 74 20 6d 61 73 6b 3b 20 20 20 20 20 20   int mask;      
31fbb 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f 72       /* Mask for
31fbc 20 74 68 65 20 64 62 2d 3e 66 6c 61 67 73 20 76   the db->flags v
31fbd 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 61 50 72 61  alue */.  } aPra
31fbe 67 6d 61 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20  gma[] = {.    { 
31fbf 22 66 75 6c 6c 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  "full_column_nam
31fc0 65 73 22 2c 20 20 20 20 20 20 20 20 53 51 4c 49  es",        SQLI
31fc1 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20  TE_FullColNames 
31fc2 20 7d 2c 0a 20 20 20 20 7b 20 22 73 68 6f 72 74   },.    { "short
31fc3 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20  _column_names", 
31fc4 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 68 6f        SQLITE_Sho
31fc5 72 74 43 6f 6c 4e 61 6d 65 73 20 7d 2c 0a 20 20  rtColNames },.  
31fc6 20 20 7b 20 22 63 6f 75 6e 74 5f 63 68 61 6e 67    { "count_chang
31fc7 65 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  es",            
31fc8 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
31fc9 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 65       },.    { "e
31fca 6d 70 74 79 5f 72 65 73 75 6c 74 5f 63 61 6c 6c  mpty_result_call
31fcb 62 61 63 6b 73 22 2c 20 20 20 53 51 4c 49 54 45  backs",   SQLITE
31fcc 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 7d  _NullCallback  }
31fcd 2c 0a 20 20 20 20 7b 20 22 6c 65 67 61 63 79 5f  ,.    { "legacy_
31fce 66 69 6c 65 5f 66 6f 72 6d 61 74 22 2c 20 20 20  file_format",   
31fcf 20 20 20 20 53 51 4c 49 54 45 5f 4c 65 67 61 63      SQLITE_Legac
31fd0 79 46 69 6c 65 46 6d 74 20 7d 2c 0a 20 20 20 20  yFileFmt },.    
31fd1 7b 20 22 66 75 6c 6c 66 73 79 6e 63 22 2c 20 20  { "fullfsync",  
31fd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
31fd3 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 20 20  LITE_FullFSync  
31fd4 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 63 68 65     },.    { "che
31fd5 63 6b 70 6f 69 6e 74 5f 66 75 6c 6c 66 73 79 6e  ckpoint_fullfsyn
31fd6 63 22 2c 20 20 20 20 20 53 51 4c 49 54 45 5f 43  c",     SQLITE_C
31fd7 6b 70 74 46 75 6c 6c 46 53 79 6e 63 20 7d 2c 0a  kptFullFSync },.
31fd8 20 20 20 20 7b 20 22 72 65 76 65 72 73 65 5f 75      { "reverse_u
31fd9 6e 6f 72 64 65 72 65 64 5f 73 65 6c 65 63 74 73  nordered_selects
31fda 22 2c 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73  ", SQLITE_Revers
31fdb 65 4f 72 64 65 72 20 20 7d 2c 0a 23 69 66 6e 64  eOrder  },.#ifnd
31fdc 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
31fdd 55 54 4f 4d 41 54 49 43 5f 49 4e 44 45 58 0a 20  UTOMATIC_INDEX. 
31fde 20 20 20 7b 20 22 61 75 74 6f 6d 61 74 69 63 5f     { "automatic_
31fdf 69 6e 64 65 78 22 2c 20 20 20 20 20 20 20 20 20  index",         
31fe0 20 53 51 4c 49 54 45 5f 41 75 74 6f 49 6e 64 65   SQLITE_AutoInde
31fe1 78 20 20 20 20 20 7d 2c 0a 23 65 6e 64 69 66 0a  x     },.#endif.
31fe2 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
31fe3 42 55 47 0a 20 20 20 20 7b 20 22 73 71 6c 5f 74  BUG.    { "sql_t
31fe4 72 61 63 65 22 2c 20 20 20 20 20 20 20 20 20 20  race",          
31fe5 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 71 6c        SQLITE_Sql
31fe6 54 72 61 63 65 20 20 20 20 20 20 7d 2c 0a 20 20  Trace      },.  
31fe7 20 20 7b 20 22 76 64 62 65 5f 6c 69 73 74 69 6e    { "vdbe_listin
31fe8 67 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  g",             
31fe9 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69  SQLITE_VdbeListi
31fea 6e 67 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76  ng   },.    { "v
31feb 64 62 65 5f 74 72 61 63 65 22 2c 20 20 20 20 20  dbe_trace",     
31fec 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
31fed 5f 56 64 62 65 54 72 61 63 65 20 20 20 20 20 7d  _VdbeTrace     }
31fee 2c 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  ,.#endif.#ifndef
31fef 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
31ff0 43 4b 0a 20 20 20 20 7b 20 22 69 67 6e 6f 72 65  CK.    { "ignore
31ff1 5f 63 68 65 63 6b 5f 63 6f 6e 73 74 72 61 69 6e  _check_constrain
31ff2 74 73 22 2c 20 53 51 4c 49 54 45 5f 49 67 6e 6f  ts", SQLITE_Igno
31ff3 72 65 43 68 65 63 6b 73 20 20 7d 2c 0a 23 65 6e  reChecks  },.#en
31ff4 64 69 66 0a 20 20 20 20 2f 2a 20 54 68 65 20 66  dif.    /* The f
31ff5 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 56 45 52 59  ollowing is VERY
31ff6 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 2a 2f   experimental */
31ff7 0a 20 20 20 20 7b 20 22 77 72 69 74 61 62 6c 65  .    { "writable
31ff8 5f 73 63 68 65 6d 61 22 2c 20 20 20 20 20 20 20  _schema",       
31ff9 20 20 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53     SQLITE_WriteS
31ffa 63 68 65 6d 61 7c 53 51 4c 49 54 45 5f 52 65 63  chema|SQLITE_Rec
31ffb 6f 76 65 72 79 4d 6f 64 65 20 7d 2c 0a 0a 20 20  overyMode },..  
31ffc 20 20 2f 2a 20 54 4f 44 4f 3a 20 4d 61 79 62 65    /* TODO: Maybe
31ffd 20 69 74 20 73 68 6f 75 6c 64 6e 27 74 20 62 65   it shouldn't be
31ffe 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 68 61   possible to cha
31fff 6e 67 65 20 74 68 65 20 52 65 61 64 55 6e 63 6f  nge the ReadUnco
32000 6d 6d 69 74 74 65 64 0a 20 20 20 20 2a 2a 20 66  mmitted.    ** f
32001 6c 61 67 20 69 66 20 74 68 65 72 65 20 61 72 65  lag if there are
32002 20 61 6e 79 20 61 63 74 69 76 65 20 73 74 61 74   any active stat
32003 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 20 20 7b  ements. */.    {
32004 20 22 72 65 61 64 5f 75 6e 63 6f 6d 6d 69 74 74   "read_uncommitt
32005 65 64 22 2c 20 20 20 20 20 20 20 20 20 53 51 4c  ed",         SQL
32006 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74  ITE_ReadUncommit
32007 74 65 64 20 7d 2c 0a 20 20 20 20 7b 20 22 72 65  ted },.    { "re
32008 63 75 72 73 69 76 65 5f 74 72 69 67 67 65 72 73  cursive_triggers
32009 22 2c 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  ",       SQLITE_
3200a 52 65 63 54 72 69 67 67 65 72 73 20 7d 2c 0a 0a  RecTriggers },..
3200b 20 20 20 20 2f 2a 20 54 68 69 73 20 66 6c 61 67      /* This flag
3200c 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 73 65 74   may only be set
3200d 20 69 66 20 62 6f 74 68 20 66 6f 72 65 69 67 6e   if both foreign
3200e 2d 6b 65 79 20 61 6e 64 20 74 72 69 67 67 65 72  -key and trigger
3200f 20 73 75 70 70 6f 72 74 0a 20 20 20 20 2a 2a 20   support.    ** 
32010 61 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74  are present in t
32011 68 65 20 62 75 69 6c 64 2e 20 20 2a 2f 0a 23 69  he build.  */.#i
32012 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
32013 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
32014 45 59 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  EY) && !defined(
32015 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
32016 47 45 52 29 0a 20 20 20 20 7b 20 22 66 6f 72 65  GER).    { "fore
32017 69 67 6e 5f 6b 65 79 73 22 2c 20 20 20 20 20 20  ign_keys",      
32018 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46 6f         SQLITE_Fo
32019 72 65 69 67 6e 4b 65 79 73 20 7d 2c 0a 23 65 6e  reignKeys },.#en
3201a 64 69 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69  dif.  };.  int i
3201b 3b 0a 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74  ;.  const struct
3201c 20 73 50 72 61 67 6d 61 54 79 70 65 20 2a 70 3b   sPragmaType *p;
3201d 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 61 50  .  for(i=0, p=aP
3201e 72 61 67 6d 61 3b 20 69 3c 41 72 72 61 79 53 69  ragma; i<ArraySi
3201f 7a 65 28 61 50 72 61 67 6d 61 29 3b 20 69 2b 2b  ze(aPragma); i++
32020 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  , p++){.    if( 
32021 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
32022 4c 65 66 74 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3d  Left, p->zName)=
32023 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
32024 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
32025 2d 3e 64 62 3b 0a 20 20 20 20 20 20 56 64 62 65  ->db;.      Vdbe
32026 20 2a 76 3b 0a 20 20 20 20 20 20 76 20 3d 20 73   *v;.      v = s
32027 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
32028 61 72 73 65 29 3b 0a 20 20 20 20 20 20 61 73 73  arse);.      ass
32029 65 72 74 28 20 76 21 3d 30 20 29 3b 20 20 2f 2a  ert( v!=0 );  /*
3202a 20 41 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74   Already allocat
3202b 65 64 20 62 79 20 73 71 6c 69 74 65 33 50 72 61  ed by sqlite3Pra
3202c 67 6d 61 28 29 20 2a 2f 0a 20 20 20 20 20 20 69  gma() */.      i
3202d 66 28 20 41 4c 57 41 59 53 28 76 29 20 29 7b 0a  f( ALWAYS(v) ){.
3202e 20 20 20 20 20 20 20 20 69 66 28 20 7a 52 69 67          if( zRig
3202f 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ht==0 ){.       
32030 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49     returnSingleI
32031 6e 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 7a 4e  nt(pParse, p->zN
32032 61 6d 65 2c 20 28 64 62 2d 3e 66 6c 61 67 73 20  ame, (db->flags 
32033 26 20 70 2d 3e 6d 61 73 6b 29 21 3d 30 20 29 3b  & p->mask)!=0 );
32034 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
32035 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6d 61            int ma
32036 73 6b 20 3d 20 70 2d 3e 6d 61 73 6b 3b 20 20 20  sk = p->mask;   
32037 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
32038 66 20 62 69 74 73 20 74 6f 20 73 65 74 20 6f 72  f bits to set or
32039 20 63 6c 65 61 72 2e 20 2a 2f 0a 20 20 20 20 20   clear. */.     
3203a 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 75 74       if( db->aut
3203b 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 7b 0a 20 20  oCommit==0 ){.  
3203c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
3203d 65 69 67 6e 20 6b 65 79 20 73 75 70 70 6f 72 74  eign key support
3203e 20 6d 61 79 20 6e 6f 74 20 62 65 20 65 6e 61 62   may not be enab
3203f 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 20  led or disabled 
32040 77 68 69 6c 65 20 6e 6f 74 0a 20 20 20 20 20 20  while not.      
32041 20 20 20 20 20 20 2a 2a 20 69 6e 20 61 75 74 6f        ** in auto
32042 2d 63 6f 6d 6d 69 74 20 6d 6f 64 65 2e 20 20 2a  -commit mode.  *
32043 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 61  /.            ma
32044 73 6b 20 26 3d 20 7e 28 53 51 4c 49 54 45 5f 46  sk &= ~(SQLITE_F
32045 6f 72 65 69 67 6e 4b 65 79 73 29 3b 0a 20 20 20  oreignKeys);.   
32046 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
32047 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47      if( sqlite3G
32048 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74  etBoolean(zRight
32049 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  , 0) ){.        
3204a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d      db->flags |=
3204b 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20 20   mask;.         
3204c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
3204d 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d      db->flags &=
3204e 20 7e 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20   ~mask;.        
3204f 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f    }..          /
32050 2a 20 4d 61 6e 79 20 6f 66 20 74 68 65 20 66 6c  * Many of the fl
32051 61 67 2d 70 72 61 67 6d 61 73 20 6d 6f 64 69 66  ag-pragmas modif
32052 79 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  y the code gener
32053 61 74 65 64 20 62 79 20 74 68 65 20 53 51 4c 20  ated by the SQL 
32054 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  .          ** co
32055 6d 70 69 6c 65 72 20 28 65 67 2e 20 63 6f 75 6e  mpiler (eg. coun
32056 74 5f 63 68 61 6e 67 65 73 29 2e 20 53 6f 20 61  t_changes). So a
32057 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 6f 20  dd an opcode to 
32058 65 78 70 69 72 65 20 61 6c 6c 0a 20 20 20 20 20  expire all.     
32059 20 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64       ** compiled
3205a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
3205b 61 66 74 65 72 20 6d 6f 64 69 66 79 69 6e 67 20  after modifying 
3205c 61 20 70 72 61 67 6d 61 20 76 61 6c 75 65 2e 0a  a pragma value..
3205d 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
3205e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
3205f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45  beAddOp2(v, OP_E
32060 78 70 69 72 65 2c 20 30 2c 20 30 29 3b 0a 20 20  xpire, 0, 0);.  
32061 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
32062 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
32063 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
32064 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20  urn 0;.}.#endif 
32065 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  /* SQLITE_OMIT_F
32066 4c 41 47 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a  LAG_PRAGMAS */..
32067 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 68  /*.** Return a h
32068 75 6d 61 6e 2d 72 65 61 64 61 62 6c 65 20 6e 61  uman-readable na
32069 6d 65 20 66 6f 72 20 61 20 63 6f 6e 73 74 72 61  me for a constra
3206a 69 6e 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61  int resolution a
3206b 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ction..*/.#ifnde
3206c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
3206d 52 45 49 47 4e 5f 4b 45 59 0a 73 74 61 74 69 63  REIGN_KEY.static
3206e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 63 74   const char *act
3206f 69 6f 6e 4e 61 6d 65 28 75 38 20 61 63 74 69 6f  ionName(u8 actio
32070 6e 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  n){.  const char
32071 20 2a 7a 4e 61 6d 65 3b 0a 20 20 73 77 69 74 63   *zName;.  switc
32072 68 28 20 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20  h( action ){.   
32073 20 63 61 73 65 20 4f 45 5f 53 65 74 4e 75 6c 6c   case OE_SetNull
32074 3a 20 20 7a 4e 61 6d 65 20 3d 20 22 53 45 54 20  :  zName = "SET 
32075 4e 55 4c 4c 22 3b 20 20 20 20 20 20 20 20 62 72  NULL";        br
32076 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 45  eak;.    case OE
32077 5f 53 65 74 44 66 6c 74 3a 20 20 7a 4e 61 6d 65  _SetDflt:  zName
32078 20 3d 20 22 53 45 54 20 44 45 46 41 55 4c 54 22   = "SET DEFAULT"
32079 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ;     break;.   
3207a 20 63 61 73 65 20 4f 45 5f 43 61 73 63 61 64 65   case OE_Cascade
3207b 3a 20 20 7a 4e 61 6d 65 20 3d 20 22 43 41 53 43  :  zName = "CASC
3207c 41 44 45 22 3b 20 20 20 20 20 20 20 20 20 62 72  ADE";         br
3207d 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 45  eak;.    case OE
3207e 5f 52 65 73 74 72 69 63 74 3a 20 7a 4e 61 6d 65  _Restrict: zName
3207f 20 3d 20 22 52 45 53 54 52 49 43 54 22 3b 20 20   = "RESTRICT";  
32080 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32081 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20   default:       
32082 20 20 20 7a 4e 61 6d 65 20 3d 20 22 4e 4f 20 41     zName = "NO A
32083 43 54 49 4f 4e 22 3b 20 20 0a 20 20 20 20 20 20  CTION";  .      
32084 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32085 61 73 73 65 72 74 28 20 61 63 74 69 6f 6e 3d 3d  assert( action==
32086 4f 45 5f 4e 6f 6e 65 20 29 3b 20 62 72 65 61 6b  OE_None ); break
32087 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
32088 4e 61 6d 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  Name;.}.#endif..
32089 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72  ./*.** Parameter
3208a 20 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 6f   eMode must be o
3208b 6e 65 20 6f 66 20 74 68 65 20 50 41 47 45 52 5f  ne of the PAGER_
3208c 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 58 58 58 20  JOURNALMODE_XXX 
3208d 63 6f 6e 73 74 61 6e 74 73 0a 2a 2a 20 64 65 66  constants.** def
3208e 69 6e 65 64 20 69 6e 20 70 61 67 65 72 2e 68 2e  ined in pager.h.
3208f 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
32090 65 74 75 72 6e 73 20 74 68 65 20 61 73 73 6f 63  eturns the assoc
32091 69 61 74 65 64 20 6c 6f 77 65 72 63 61 73 65 0a  iated lowercase.
32092 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20  ** journal-mode 
32093 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  name..*/.SQLITE_
32094 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
32095 61 72 20 2a 73 71 6c 69 74 65 33 4a 6f 75 72 6e  ar *sqlite3Journ
32096 61 6c 4d 6f 64 65 6e 61 6d 65 28 69 6e 74 20 65  alModename(int e
32097 4d 6f 64 65 29 7b 0a 20 20 73 74 61 74 69 63 20  Mode){.  static 
32098 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a 4d  char * const azM
32099 6f 64 65 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 20  odeName[] = {.  
3209a 20 20 22 64 65 6c 65 74 65 22 2c 20 22 70 65 72    "delete", "per
3209b 73 69 73 74 22 2c 20 22 6f 66 66 22 2c 20 22 74  sist", "off", "t
3209c 72 75 6e 63 61 74 65 22 2c 20 22 6d 65 6d 6f 72  runcate", "memor
3209d 79 22 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  y".#ifndef SQLIT
3209e 45 5f 4f 4d 49 54 5f 57 41 4c 0a 20 20 20 20 20  E_OMIT_WAL.     
3209f 2c 20 22 77 61 6c 22 0a 23 65 6e 64 69 66 0a 20  , "wal".#endif. 
320a0 20 7d 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41   };.  assert( PA
320a1 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
320a2 44 45 4c 45 54 45 3d 3d 30 20 29 3b 0a 20 20 61  DELETE==0 );.  a
320a3 73 73 65 72 74 28 20 50 41 47 45 52 5f 4a 4f 55  ssert( PAGER_JOU
320a4 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54  RNALMODE_PERSIST
320a5 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==1 );.  assert(
320a6 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
320a7 44 45 5f 4f 46 46 3d 3d 32 20 29 3b 0a 20 20 61  DE_OFF==2 );.  a
320a8 73 73 65 72 74 28 20 50 41 47 45 52 5f 4a 4f 55  ssert( PAGER_JOU
320a9 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54  RNALMODE_TRUNCAT
320aa 45 3d 3d 33 20 29 3b 0a 20 20 61 73 73 65 72 74  E==3 );.  assert
320ab 28 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ( PAGER_JOURNALM
320ac 4f 44 45 5f 4d 45 4d 4f 52 59 3d 3d 34 20 29 3b  ODE_MEMORY==4 );
320ad 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52  .  assert( PAGER
320ae 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 57 41 4c  _JOURNALMODE_WAL
320af 3d 3d 35 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==5 );.  assert(
320b0 20 65 4d 6f 64 65 3e 3d 30 20 26 26 20 65 4d 6f   eMode>=0 && eMo
320b1 64 65 3c 3d 41 72 72 61 79 53 69 7a 65 28 61 7a  de<=ArraySize(az
320b2 4d 6f 64 65 4e 61 6d 65 29 20 29 3b 0a 0a 20 20  ModeName) );..  
320b3 69 66 28 20 65 4d 6f 64 65 3d 3d 41 72 72 61 79  if( eMode==Array
320b4 53 69 7a 65 28 61 7a 4d 6f 64 65 4e 61 6d 65 29  Size(azModeName)
320b5 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72   ) return 0;.  r
320b6 65 74 75 72 6e 20 61 7a 4d 6f 64 65 4e 61 6d 65  eturn azModeName
320b7 5b 65 4d 6f 64 65 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  [eMode];.}../*.*
320b8 2a 20 50 72 6f 63 65 73 73 20 61 20 70 72 61 67  * Process a prag
320b9 6d 61 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 0a  ma statement.  .
320ba 2a 2a 0a 2a 2a 20 50 72 61 67 6d 61 73 20 61 72  **.** Pragmas ar
320bb 65 20 6f 66 20 74 68 69 73 20 66 6f 72 6d 3a 0a  e of this form:.
320bc 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 52 41 47 4d  **.**      PRAGM
320bd 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 69 64 20  A [database.]id 
320be 5b 3d 20 76 61 6c 75 65 5d 0a 2a 2a 0a 2a 2a 20  [= value].**.** 
320bf 54 68 65 20 69 64 65 6e 74 69 66 69 65 72 20 6d  The identifier m
320c0 69 67 68 74 20 61 6c 73 6f 20 62 65 20 61 20 73  ight also be a s
320c1 74 72 69 6e 67 2e 20 20 54 68 65 20 76 61 6c 75  tring.  The valu
320c2 65 20 69 73 20 61 20 73 74 72 69 6e 67 2c 20 61  e is a string, a
320c3 6e 64 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65 72  nd.** identifier
320c4 2c 20 6f 72 20 61 20 6e 75 6d 62 65 72 2e 20 20  , or a number.  
320c5 49 66 20 6d 69 6e 75 73 46 6c 61 67 20 69 73 20  If minusFlag is 
320c6 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76  true, then the v
320c7 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20 6e 75 6d  alue is.** a num
320c8 62 65 72 20 74 68 61 74 20 77 61 73 20 70 72 65  ber that was pre
320c9 63 65 64 65 64 20 62 79 20 61 20 6d 69 6e 75 73  ceded by a minus
320ca 20 73 69 67 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   sign..**.** If 
320cb 74 68 65 20 6c 65 66 74 20 73 69 64 65 20 69 73  the left side is
320cc 20 22 64 61 74 61 62 61 73 65 2e 69 64 22 20 74   "database.id" t
320cd 68 65 6e 20 70 49 64 31 20 69 73 20 74 68 65 20  hen pId1 is the 
320ce 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a  database name.**
320cf 20 61 6e 64 20 70 49 64 32 20 69 73 20 74 68 65   and pId2 is the
320d0 20 69 64 2e 20 20 49 66 20 74 68 65 20 6c 65 66   id.  If the lef
320d1 74 20 73 69 64 65 20 69 73 20 6a 75 73 74 20 22  t side is just "
320d2 69 64 22 20 74 68 65 6e 20 70 49 64 31 20 69 73  id" then pId1 is
320d3 20 74 68 65 0a 2a 2a 20 69 64 20 61 6e 64 20 70   the.** id and p
320d4 49 64 32 20 69 73 20 61 6e 79 20 65 6d 70 74 79  Id2 is any empty
320d5 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49   string..*/.SQLI
320d6 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
320d7 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 0a 20  sqlite3Pragma(. 
320d8 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
320d9 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 31 2c 20  .  Token *pId1, 
320da 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
320db 70 61 72 74 20 6f 66 20 5b 64 61 74 61 62 61 73  part of [databas
320dc 65 2e 5d 69 64 20 66 69 65 6c 64 20 2a 2f 0a 20  e.]id field */. 
320dd 20 54 6f 6b 65 6e 20 2a 70 49 64 32 2c 20 20 20   Token *pId2,   
320de 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70       /* Second p
320df 61 72 74 20 6f 66 20 5b 64 61 74 61 62 61 73 65  art of [database
320e0 2e 5d 69 64 20 66 69 65 6c 64 2c 20 6f 72 20 4e  .]id field, or N
320e1 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ULL */.  Token *
320e2 70 56 61 6c 75 65 2c 20 20 20 20 20 20 2f 2a 20  pValue,      /* 
320e3 54 6f 6b 65 6e 20 66 6f 72 20 3c 76 61 6c 75 65  Token for <value
320e4 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20  >, or NULL */.  
320e5 69 6e 74 20 6d 69 6e 75 73 46 6c 61 67 20 20 20  int minusFlag   
320e6 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
320e7 20 27 2d 27 20 73 69 67 6e 20 70 72 65 63 65 64   '-' sign preced
320e8 65 64 20 3c 76 61 6c 75 65 3e 20 2a 2f 0a 29 7b  ed <value> */.){
320e9 0a 20 20 63 68 61 72 20 2a 7a 4c 65 66 74 20 3d  .  char *zLeft =
320ea 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6c   0;       /* Nul
320eb 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d  -terminated UTF-
320ec 38 20 73 74 72 69 6e 67 20 3c 69 64 3e 20 2a 2f  8 string <id> */
320ed 0a 20 20 63 68 61 72 20 2a 7a 52 69 67 68 74 20  .  char *zRight 
320ee 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6c  = 0;      /* Nul
320ef 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d  -terminated UTF-
320f0 38 20 73 74 72 69 6e 67 20 3c 76 61 6c 75 65 3e  8 string <value>
320f1 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63  , or NULL */.  c
320f2 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d  onst char *zDb =
320f3 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74   0;   /* The dat
320f4 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  abase name */.  
320f5 54 6f 6b 65 6e 20 2a 70 49 64 3b 20 20 20 20 20  Token *pId;     
320f6 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
320f7 72 20 74 6f 20 3c 69 64 3e 20 74 6f 6b 65 6e 20  r to <id> token 
320f8 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
320f9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
320fa 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 66 6f  atabase index fo
320fb 72 20 3c 64 61 74 61 62 61 73 65 3e 20 2a 2f 0a  r <database> */.
320fc 20 20 63 68 61 72 20 2a 61 46 63 6e 74 6c 5b 34    char *aFcntl[4
320fd 5d 3b 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75  ];       /* Argu
320fe 6d 65 6e 74 20 74 6f 20 53 51 4c 49 54 45 5f 46  ment to SQLITE_F
320ff 43 4e 54 4c 5f 50 52 41 47 4d 41 20 2a 2f 0a 20  CNTL_PRAGMA */. 
32100 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
32101 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
32102 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 6f   return value fo
32103 72 6d 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  rm SQLITE_FCNTL_
32104 50 52 41 47 4d 41 20 2a 2f 0a 20 20 73 71 6c 69  PRAGMA */.  sqli
32105 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
32106 2d 3e 64 62 3b 20 20 20 20 2f 2a 20 54 68 65 20  ->db;    /* The 
32107 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
32108 69 6f 6e 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62  ion */.  Db *pDb
32109 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3210a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65        /* The spe
3210b 63 69 66 69 63 20 64 61 74 61 62 61 73 65 20 62  cific database b
3210c 65 69 6e 67 20 70 72 61 67 6d 61 65 64 20 2a 2f  eing pragmaed */
3210d 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
3210e 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 73 71 6c  rse->pVdbe = sql
3210f 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28 64  ite3VdbeCreate(d
32110 62 29 3b 20 20 2f 2a 20 50 72 65 70 61 72 65 64  b);  /* Prepared
32111 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 0a 20   statement */.. 
32112 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
32113 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  rn;.  sqlite3Vdb
32114 65 52 75 6e 4f 6e 6c 79 4f 6e 63 65 28 76 29 3b  eRunOnlyOnce(v);
32115 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20  .  pParse->nMem 
32116 3d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72  = 2;..  /* Inter
32117 70 72 65 74 20 74 68 65 20 5b 64 61 74 61 62 61  pret the [databa
32118 73 65 2e 5d 20 70 61 72 74 20 6f 66 20 74 68 65  se.] part of the
32119 20 70 72 61 67 6d 61 20 73 74 61 74 65 6d 65 6e   pragma statemen
3211a 74 2e 20 69 44 62 20 69 73 20 74 68 65 0a 20 20  t. iDb is the.  
3211b 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  ** index of the 
3211c 64 61 74 61 62 61 73 65 20 74 68 69 73 20 70 72  database this pr
3211d 61 67 6d 61 20 69 73 20 62 65 69 6e 67 20 61 70  agma is being ap
3211e 70 6c 69 65 64 20 74 6f 20 69 6e 20 64 62 2e 61  plied to in db.a
3211f 44 62 5b 5d 2e 20 2a 2f 0a 20 20 69 44 62 20 3d  Db[]. */.  iDb =
32120 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e   sqlite3TwoPartN
32121 61 6d 65 28 70 50 61 72 73 65 2c 20 70 49 64 31  ame(pParse, pId1
32122 2c 20 70 49 64 32 2c 20 26 70 49 64 29 3b 0a 20  , pId2, &pId);. 
32123 20 69 66 28 20 69 44 62 3c 30 20 29 20 72 65 74   if( iDb<0 ) ret
32124 75 72 6e 3b 0a 20 20 70 44 62 20 3d 20 26 64 62  urn;.  pDb = &db
32125 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 2f  ->aDb[iDb];..  /
32126 2a 20 49 66 20 74 68 65 20 74 65 6d 70 20 64 61  * If the temp da
32127 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20  tabase has been 
32128 65 78 70 6c 69 63 69 74 6c 79 20 6e 61 6d 65 64  explicitly named
32129 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
3212a 0a 20 20 2a 2a 20 70 72 61 67 6d 61 2c 20 6d 61  .  ** pragma, ma
3212b 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 6f 70  ke sure it is op
3212c 65 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  en. .  */.  if( 
3212d 69 44 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65  iDb==1 && sqlite
3212e 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73  3OpenTempDatabas
3212f 65 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20  e(pParse) ){.   
32130 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
32131 7a 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 4e  zLeft = sqlite3N
32132 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
32133 20 70 49 64 29 3b 0a 20 20 69 66 28 20 21 7a 4c   pId);.  if( !zL
32134 65 66 74 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  eft ) return;.  
32135 69 66 28 20 6d 69 6e 75 73 46 6c 61 67 20 29 7b  if( minusFlag ){
32136 0a 20 20 20 20 7a 52 69 67 68 74 20 3d 20 73 71  .    zRight = sq
32137 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
32138 20 22 2d 25 54 22 2c 20 70 56 61 6c 75 65 29 3b   "-%T", pValue);
32139 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 52  .  }else{.    zR
3213a 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 4e 61  ight = sqlite3Na
3213b 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
3213c 70 56 61 6c 75 65 29 3b 0a 20 20 7d 0a 0a 20 20  pValue);.  }..  
3213d 61 73 73 65 72 74 28 20 70 49 64 32 20 29 3b 0a  assert( pId2 );.
3213e 20 20 7a 44 62 20 3d 20 70 49 64 32 2d 3e 6e 3e    zDb = pId2->n>
3213f 30 20 3f 20 70 44 62 2d 3e 7a 4e 61 6d 65 20 3a  0 ? pDb->zName :
32140 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   0;.  if( sqlite
32141 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
32142 65 2c 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41  e, SQLITE_PRAGMA
32143 2c 20 7a 4c 65 66 74 2c 20 7a 52 69 67 68 74 2c  , zLeft, zRight,
32144 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 67 6f 74   zDb) ){.    got
32145 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
32146 7d 0a 0a 20 20 2f 2a 20 53 65 6e 64 20 61 6e 20  }..  /* Send an 
32147 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 52 41  SQLITE_FCNTL_PRA
32148 47 4d 41 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c  GMA file-control
32149 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c 79 69   to the underlyi
3214a 6e 67 20 56 46 53 0a 20 20 2a 2a 20 63 6f 6e 6e  ng VFS.  ** conn
3214b 65 63 74 69 6f 6e 2e 20 20 49 66 20 69 74 20 72  ection.  If it r
3214c 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
3214d 2c 20 74 68 65 6e 20 61 73 73 75 6d 65 20 74 68  , then assume th
3214e 61 74 20 74 68 65 20 56 46 53 0a 20 20 2a 2a 20  at the VFS.  ** 
3214f 68 61 6e 64 6c 65 64 20 74 68 65 20 70 72 61 67  handled the prag
32150 6d 61 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20  ma and generate 
32151 61 20 6e 6f 2d 6f 70 20 70 72 65 70 61 72 65 64  a no-op prepared
32152 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f   statement..  */
32153 0a 20 20 61 46 63 6e 74 6c 5b 30 5d 20 3d 20 30  .  aFcntl[0] = 0
32154 3b 0a 20 20 61 46 63 6e 74 6c 5b 31 5d 20 3d 20  ;.  aFcntl[1] = 
32155 7a 4c 65 66 74 3b 0a 20 20 61 46 63 6e 74 6c 5b  zLeft;.  aFcntl[
32156 32 5d 20 3d 20 7a 52 69 67 68 74 3b 0a 20 20 61  2] = zRight;.  a
32157 46 63 6e 74 6c 5b 33 5d 20 3d 20 30 3b 0a 20 20  Fcntl[3] = 0;.  
32158 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e  db->busyHandler.
32159 6e 42 75 73 79 20 3d 20 30 3b 0a 20 20 72 63 20  nBusy = 0;.  rc 
3215a 3d 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63  = sqlite3_file_c
3215b 6f 6e 74 72 6f 6c 28 64 62 2c 20 7a 44 62 2c 20  ontrol(db, zDb, 
3215c 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 50 52 41  SQLITE_FCNTL_PRA
3215d 47 4d 41 2c 20 28 76 6f 69 64 2a 29 61 46 63 6e  GMA, (void*)aFcn
3215e 74 6c 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  tl);.  if( rc==S
3215f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
32160 69 66 28 20 61 46 63 6e 74 6c 5b 30 5d 20 29 7b  if( aFcntl[0] ){
32161 0a 20 20 20 20 20 20 69 6e 74 20 6d 65 6d 20 3d  .      int mem =
32162 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
32163 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
32164 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
32165 74 72 69 6e 67 38 2c 20 30 2c 20 6d 65 6d 2c 20  tring8, 0, mem, 
32166 30 2c 20 61 46 63 6e 74 6c 5b 30 5d 2c 20 30 29  0, aFcntl[0], 0)
32167 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
32168 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
32169 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
3216a 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
3216b 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
3216c 41 4d 45 2c 20 22 72 65 73 75 6c 74 22 2c 20 53  AME, "result", S
3216d 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
3216e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3216f 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
32170 75 6c 74 52 6f 77 2c 20 6d 65 6d 2c 20 31 29 3b  ultRow, mem, 1);
32171 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
32172 72 65 65 28 61 46 63 6e 74 6c 5b 30 5d 29 3b 0a  ree(aFcntl[0]);.
32173 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
32174 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4e 4f 54  ( rc!=SQLITE_NOT
32175 46 4f 55 4e 44 20 29 7b 0a 20 20 20 20 69 66 28  FOUND ){.    if(
32176 20 61 46 63 6e 74 6c 5b 30 5d 20 29 7b 0a 20 20   aFcntl[0] ){.  
32177 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
32178 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 22  Msg(pParse, "%s"
32179 2c 20 61 46 63 6e 74 6c 5b 30 5d 29 3b 0a 20 20  , aFcntl[0]);.  
3217a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
3217b 28 61 46 63 6e 74 6c 5b 30 5d 29 3b 0a 20 20 20  (aFcntl[0]);.   
3217c 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e   }.    pParse->n
3217d 45 72 72 2b 2b 3b 0a 20 20 20 20 70 50 61 72 73  Err++;.    pPars
3217e 65 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 7d 65  e->rc = rc;.  }e
3217f 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  lse.            
32180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32181 0a 20 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  . .#if !defined(
32182 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45  SQLITE_OMIT_PAGE
32183 52 5f 50 52 41 47 4d 41 53 29 20 26 26 20 21 64  R_PRAGMAS) && !d
32184 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
32185 49 54 5f 44 45 50 52 45 43 41 54 45 44 29 0a 20  IT_DEPRECATED). 
32186 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41   /*.  **  PRAGMA
32187 20 5b 64 61 74 61 62 61 73 65 2e 5d 64 65 66 61   [database.]defa
32188 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65 0a 20  ult_cache_size. 
32189 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
3218a 61 62 61 73 65 2e 5d 64 65 66 61 75 6c 74 5f 63  abase.]default_c
3218b 61 63 68 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a  ache_size=N.  **
3218c 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20  .  ** The first 
3218d 66 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65  form reports the
3218e 20 63 75 72 72 65 6e 74 20 70 65 72 73 69 73 74   current persist
3218f 65 6e 74 20 73 65 74 74 69 6e 67 20 66 6f 72 20  ent setting for 
32190 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 61  the.  ** page ca
32191 63 68 65 20 73 69 7a 65 2e 20 20 54 68 65 20 76  che size.  The v
32192 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
32193 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   the maximum num
32194 62 65 72 20 6f 66 0a 20 20 2a 2a 20 70 61 67 65  ber of.  ** page
32195 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61  s in the page ca
32196 63 68 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  che.  The second
32197 20 66 6f 72 6d 20 73 65 74 73 20 62 6f 74 68 20   form sets both 
32198 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a  the current.  **
32199 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65   page cache size
3219a 20 76 61 6c 75 65 20 61 6e 64 20 74 68 65 20 70   value and the p
3219b 65 72 73 69 73 74 65 6e 74 20 70 61 67 65 20 63  ersistent page c
3219c 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75 65 0a  ache size value.
3219d 20 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74    ** stored in t
3219e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
3219f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 6c 64 65  ..  **.  ** Olde
321a0 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  r versions of SQ
321a1 4c 69 74 65 20 77 6f 75 6c 64 20 73 65 74 20 74  Lite would set t
321a2 68 65 20 64 65 66 61 75 6c 74 20 63 61 63 68 65  he default cache
321a3 20 73 69 7a 65 20 74 6f 20 61 0a 20 20 2a 2a 20   size to a.  ** 
321a4 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20  negative number 
321a5 74 6f 20 69 6e 64 69 63 61 74 65 20 73 79 6e 63  to indicate sync
321a6 68 72 6f 6e 6f 75 73 3d 4f 46 46 2e 20 20 54 68  hronous=OFF.  Th
321a7 65 73 65 20 64 61 79 73 2c 20 73 79 6e 63 68 72  ese days, synchr
321a8 6f 6e 6f 75 73 0a 20 20 2a 2a 20 69 73 20 61 6c  onous.  ** is al
321a9 77 61 79 73 20 6f 6e 20 62 79 20 64 65 66 61 75  ways on by defau
321aa 6c 74 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  lt regardless of
321ab 20 74 68 65 20 73 69 67 6e 20 6f 66 20 74 68 65   the sign of the
321ac 20 64 65 66 61 75 6c 74 20 63 61 63 68 65 0a 20   default cache. 
321ad 20 2a 2a 20 73 69 7a 65 2e 20 20 42 75 74 20 63   ** size.  But c
321ae 6f 6e 74 69 6e 75 65 20 74 6f 20 74 61 6b 65 20  ontinue to take 
321af 74 68 65 20 61 62 73 6f 6c 75 74 65 20 76 61 6c  the absolute val
321b0 75 65 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c  ue of the defaul
321b1 74 20 63 61 63 68 65 0a 20 20 2a 2a 20 73 69 7a  t cache.  ** siz
321b2 65 20 6f 66 20 68 69 73 74 6f 72 69 63 61 6c 20  e of historical 
321b3 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 20  compatibility.. 
321b4 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
321b5 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
321b6 64 65 66 61 75 6c 74 5f 63 61 63 68 65 5f 73 69  default_cache_si
321b7 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73  ze")==0 ){.    s
321b8 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65  tatic const Vdbe
321b9 4f 70 4c 69 73 74 20 67 65 74 43 61 63 68 65 53  OpList getCacheS
321ba 69 7a 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20  ize[] = {.      
321bb 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e  { OP_Transaction
321bc 2c 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 30  , 0, 0,        0
321bd 7d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  },              
321be 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20             /* 0 
321bf 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52 65  */.      { OP_Re
321c0 61 64 43 6f 6f 6b 69 65 2c 20 20 30 2c 20 31 2c  adCookie,  0, 1,
321c1 20 20 20 20 20 20 20 20 42 54 52 45 45 5f 44 45          BTREE_DE
321c2 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
321c3 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20  },  /* 1 */.    
321c4 20 20 7b 20 4f 50 5f 49 66 50 6f 73 2c 20 20 20    { OP_IfPos,   
321c5 20 20 20 20 31 2c 20 37 2c 20 20 20 20 20 20 20      1, 7,       
321c6 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f   0},.      { OP_
321c7 49 6e 74 65 67 65 72 2c 20 20 20 20 20 30 2c 20  Integer,     0, 
321c8 32 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20  2,        0},.  
321c9 20 20 20 20 7b 20 4f 50 5f 53 75 62 74 72 61 63      { OP_Subtrac
321ca 74 2c 20 20 20 20 31 2c 20 32 2c 20 20 20 20 20  t,    1, 2,     
321cb 20 20 20 31 7d 2c 0a 20 20 20 20 20 20 7b 20 4f     1},.      { O
321cc 50 5f 49 66 50 6f 73 2c 20 20 20 20 20 20 20 31  P_IfPos,       1
321cd 2c 20 37 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a  , 7,        0},.
321ce 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67        { OP_Integ
321cf 65 72 2c 20 20 20 20 20 30 2c 20 31 2c 20 20 20  er,     0, 1,   
321d0 20 20 20 20 20 30 7d 2c 20 20 20 20 20 20 20 20       0},        
321d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321d2 20 2f 2a 20 36 20 2a 2f 0a 20 20 20 20 20 20 7b   /* 6 */.      {
321d3 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20   OP_ResultRow,  
321d4 20 31 2c 20 31 2c 20 20 20 20 20 20 20 20 30 7d   1, 1,        0}
321d5 2c 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74  ,.    };.    int
321d6 20 61 64 64 72 3b 0a 20 20 20 20 69 66 28 20 73   addr;.    if( s
321d7 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
321d8 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20  (pParse) ) goto 
321d9 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20  pragma_out;.    
321da 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
321db 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20  tree(v, iDb);.  
321dc 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b    if( !zRight ){
321dd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
321de 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
321df 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
321e0 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
321e1 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
321e2 4d 45 2c 20 22 63 61 63 68 65 5f 73 69 7a 65 22  ME, "cache_size"
321e3 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
321e4 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
321e5 6e 4d 65 6d 20 2b 3d 20 32 3b 0a 20 20 20 20 20  nMem += 2;.     
321e6 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
321e7 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20  dbeAddOpList(v, 
321e8 41 72 72 61 79 53 69 7a 65 28 67 65 74 43 61 63  ArraySize(getCac
321e9 68 65 53 69 7a 65 29 2c 20 67 65 74 43 61 63 68  heSize), getCach
321ea 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 73 71  eSize);.      sq
321eb 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
321ec 31 28 76 2c 20 61 64 64 72 2c 20 69 44 62 29 3b  1(v, addr, iDb);
321ed 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
321ee 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64  beChangeP1(v, ad
321ef 64 72 2b 31 2c 20 69 44 62 29 3b 0a 20 20 20 20  dr+1, iDb);.    
321f0 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
321f1 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b 36 2c  ngeP1(v, addr+6,
321f2 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
321f3 43 41 43 48 45 5f 53 49 5a 45 29 3b 0a 20 20 20  CACHE_SIZE);.   
321f4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e   }else{.      in
321f5 74 20 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33  t size = sqlite3
321f6 41 62 73 49 6e 74 33 32 28 73 71 6c 69 74 65 33  AbsInt32(sqlite3
321f7 41 74 6f 69 28 7a 52 69 67 68 74 29 29 3b 0a 20  Atoi(zRight));. 
321f8 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69       sqlite3Begi
321f9 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
321fa 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b  pParse, 0, iDb);
321fb 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
321fc 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
321fd 6e 74 65 67 65 72 2c 20 73 69 7a 65 2c 20 31 29  nteger, size, 1)
321fe 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
321ff 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
32200 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  SetCookie, iDb, 
32201 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41  BTREE_DEFAULT_CA
32202 43 48 45 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20  CHE_SIZE, 1);.  
32203 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
32204 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65  te3SchemaMutexHe
32205 6c 64 28 64 62 2c 20 69 44 62 2c 20 30 29 20 29  ld(db, iDb, 0) )
32206 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 53 63  ;.      pDb->pSc
32207 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65  hema->cache_size
32208 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 73   = size;.      s
32209 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
3220a 63 68 65 53 69 7a 65 28 70 44 62 2d 3e 70 42 74  cheSize(pDb->pBt
3220b 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  , pDb->pSchema->
3220c 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 20 20 20  cache_size);.   
3220d 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69   }.  }else.#endi
3220e 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49  f /* !SQLITE_OMI
3220f 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20  T_PAGER_PRAGMAS 
32210 26 26 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  && !SQLITE_OMIT_
32211 44 45 50 52 45 43 41 54 45 44 20 2a 2f 0a 0a 23  DEPRECATED */..#
32212 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
32213 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
32214 41 47 4d 41 53 29 0a 20 20 2f 2a 0a 20 20 2a 2a  AGMAS).  /*.  **
32215 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
32216 73 65 2e 5d 70 61 67 65 5f 73 69 7a 65 0a 20 20  se.]page_size.  
32217 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
32218 62 61 73 65 2e 5d 70 61 67 65 5f 73 69 7a 65 3d  base.]page_size=
32219 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  N.  **.  ** The 
3221a 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72  first form repor
3221b 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  ts the current s
3221c 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20  etting for the. 
3221d 20 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67   ** database pag
3221e 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2e  e size in bytes.
3221f 20 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f 72    The second for
32220 6d 20 73 65 74 73 20 74 68 65 0a 20 20 2a 2a 20  m sets the.  ** 
32221 64 61 74 61 62 61 73 65 20 70 61 67 65 20 73 69  database page si
32222 7a 65 20 76 61 6c 75 65 2e 20 20 54 68 65 20 76  ze value.  The v
32223 61 6c 75 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65  alue can only be
32224 20 73 65 74 20 69 66 0a 20 20 2a 2a 20 74 68 65   set if.  ** the
32225 20 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f   database has no
32226 74 20 79 65 74 20 62 65 65 6e 20 63 72 65 61 74  t yet been creat
32227 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ed..  */.  if( s
32228 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
32229 65 66 74 2c 22 70 61 67 65 5f 73 69 7a 65 22 29  eft,"page_size")
3222a 3d 3d 30 20 29 7b 0a 20 20 20 20 42 74 72 65 65  ==0 ){.    Btree
3222b 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e 70 42 74   *pBt = pDb->pBt
3222c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  ;.    assert( pB
3222d 74 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  t!=0 );.    if( 
3222e 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  !zRight ){.     
3222f 20 69 6e 74 20 73 69 7a 65 20 3d 20 41 4c 57 41   int size = ALWA
32230 59 53 28 70 42 74 29 20 3f 20 73 71 6c 69 74 65  YS(pBt) ? sqlite
32231 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
32232 65 28 70 42 74 29 20 3a 20 30 3b 0a 20 20 20 20  e(pBt) : 0;.    
32233 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e    returnSingleIn
32234 74 28 70 50 61 72 73 65 2c 20 22 70 61 67 65 5f  t(pParse, "page_
32235 73 69 7a 65 22 2c 20 73 69 7a 65 29 3b 0a 20 20  size", size);.  
32236 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
32237 2a 20 4d 61 6c 6c 6f 63 20 6d 61 79 20 66 61 69  * Malloc may fai
32238 6c 20 77 68 65 6e 20 73 65 74 74 69 6e 67 20 74  l when setting t
32239 68 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61 73  he page-size, as
3223a 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6e 74   there is an int
3223b 65 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 62  ernal.      ** b
3223c 75 66 66 65 72 20 74 68 61 74 20 74 68 65 20 70  uffer that the p
3223d 61 67 65 72 20 6d 6f 64 75 6c 65 20 72 65 73 69  ager module resi
3223e 7a 65 73 20 75 73 69 6e 67 20 73 71 6c 69 74 65  zes using sqlite
3223f 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 20 20 20  3_realloc()..   
32240 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e     */.      db->
32241 6e 65 78 74 50 61 67 65 73 69 7a 65 20 3d 20 73  nextPagesize = s
32242 71 6c 69 74 65 33 41 74 6f 69 28 7a 52 69 67 68  qlite3Atoi(zRigh
32243 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51  t);.      if( SQ
32244 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69  LITE_NOMEM==sqli
32245 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53  te3BtreeSetPageS
32246 69 7a 65 28 70 42 74 2c 20 64 62 2d 3e 6e 65 78  ize(pBt, db->nex
32247 74 50 61 67 65 73 69 7a 65 2c 2d 31 2c 30 29 20  tPagesize,-1,0) 
32248 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d  ){.        db->m
32249 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
3224a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
3224b 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a   }else..  /*.  *
3224c 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
3224d 61 73 65 2e 5d 73 65 63 75 72 65 5f 64 65 6c 65  ase.]secure_dele
3224e 74 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  te.  **  PRAGMA 
3224f 5b 64 61 74 61 62 61 73 65 2e 5d 73 65 63 75 72  [database.]secur
32250 65 5f 64 65 6c 65 74 65 3d 4f 4e 2f 4f 46 46 0a  e_delete=ON/OFF.
32251 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69    **.  ** The fi
32252 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73  rst form reports
32253 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74   the current set
32254 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a  ting for the.  *
32255 2a 20 73 65 63 75 72 65 5f 64 65 6c 65 74 65 20  * secure_delete 
32256 66 6c 61 67 2e 20 20 54 68 65 20 73 65 63 6f 6e  flag.  The secon
32257 64 20 66 6f 72 6d 20 63 68 61 6e 67 65 73 20 74  d form changes t
32258 68 65 20 73 65 63 75 72 65 5f 64 65 6c 65 74 65  he secure_delete
32259 0a 20 20 2a 2a 20 66 6c 61 67 20 73 65 74 74 69  .  ** flag setti
3225a 6e 67 20 61 6e 64 20 72 65 70 6f 72 74 73 20 74  ng and reports t
3225b 68 65 6e 65 77 20 76 61 6c 75 65 2e 0a 20 20 2a  henew value..  *
3225c 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
3225d 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 73 65  trICmp(zLeft,"se
3225e 63 75 72 65 5f 64 65 6c 65 74 65 22 29 3d 3d 30  cure_delete")==0
3225f 20 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70   ){.    Btree *p
32260 42 74 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20  Bt = pDb->pBt;. 
32261 20 20 20 69 6e 74 20 62 20 3d 20 2d 31 3b 0a 20     int b = -1;. 
32262 20 20 20 61 73 73 65 72 74 28 20 70 42 74 21 3d     assert( pBt!=
32263 30 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 52 69  0 );.    if( zRi
32264 67 68 74 20 29 7b 0a 20 20 20 20 20 20 62 20 3d  ght ){.      b =
32265 20 73 71 6c 69 74 65 33 47 65 74 42 6f 6f 6c 65   sqlite3GetBoole
32266 61 6e 28 7a 52 69 67 68 74 2c 20 30 29 3b 0a 20  an(zRight, 0);. 
32267 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 49 64     }.    if( pId
32268 32 2d 3e 6e 3d 3d 30 20 26 26 20 62 3e 3d 30 20  2->n==0 && b>=0 
32269 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 69 3b  ){.      int ii;
3226a 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b  .      for(ii=0;
3226b 20 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b   ii<db->nDb; ii+
3226c 2b 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  +){.        sqli
3226d 74 65 33 42 74 72 65 65 53 65 63 75 72 65 44 65  te3BtreeSecureDe
3226e 6c 65 74 65 28 64 62 2d 3e 61 44 62 5b 69 69 5d  lete(db->aDb[ii]
3226f 2e 70 42 74 2c 20 62 29 3b 0a 20 20 20 20 20 20  .pBt, b);.      
32270 7d 0a 20 20 20 20 7d 0a 20 20 20 20 62 20 3d 20  }.    }.    b = 
32271 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 63 75  sqlite3BtreeSecu
32272 72 65 44 65 6c 65 74 65 28 70 42 74 2c 20 62 29  reDelete(pBt, b)
32273 3b 0a 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67  ;.    returnSing
32274 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 73  leInt(pParse, "s
32275 65 63 75 72 65 5f 64 65 6c 65 74 65 22 2c 20 62  ecure_delete", b
32276 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a  );.  }else..  /*
32277 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
32278 61 74 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61 67  atabase.]max_pag
32279 65 5f 63 6f 75 6e 74 0a 20 20 2a 2a 20 20 50 52  e_count.  **  PR
3227a 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
3227b 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 3d 4e  max_page_count=N
3227c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66  .  **.  ** The f
3227d 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74  irst form report
3227e 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65  s the current se
3227f 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20  tting for the.  
32280 2a 2a 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  ** maximum numbe
32281 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
32282 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
32283 20 20 54 68 65 20 0a 20 20 2a 2a 20 73 65 63 6f    The .  ** seco
32284 6e 64 20 66 6f 72 6d 20 61 74 74 65 6d 70 74 73  nd form attempts
32285 20 74 6f 20 63 68 61 6e 67 65 20 74 68 69 73 20   to change this 
32286 73 65 74 74 69 6e 67 2e 20 20 42 6f 74 68 0a 20  setting.  Both. 
32287 20 2a 2a 20 66 6f 72 6d 73 20 72 65 74 75 72 6e   ** forms return
32288 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74   the current set
32289 74 69 6e 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ting..  **.  ** 
3228a 54 68 65 20 61 62 73 6f 6c 75 74 65 20 76 61 6c  The absolute val
3228b 75 65 20 6f 66 20 4e 20 69 73 20 75 73 65 64 2e  ue of N is used.
3228c 20 20 54 68 69 73 20 69 73 20 75 6e 64 6f 63 75    This is undocu
3228d 6d 65 6e 74 65 64 20 61 6e 64 20 6d 69 67 68 74  mented and might
3228e 0a 20 20 2a 2a 20 63 68 61 6e 67 65 2e 20 20 54  .  ** change.  T
3228f 68 65 20 6f 6e 6c 79 20 70 75 72 70 6f 73 65 20  he only purpose 
32290 69 73 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e  is to provide an
32291 20 65 61 73 79 20 77 61 79 20 74 6f 20 74 65 73   easy way to tes
32292 74 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74  t.  ** the sqlit
32293 65 33 41 62 73 49 6e 74 33 32 28 29 20 66 75 6e  e3AbsInt32() fun
32294 63 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  ction..  **.  **
32295 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
32296 73 65 2e 5d 70 61 67 65 5f 63 6f 75 6e 74 0a 20  se.]page_count. 
32297 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20   **.  ** Return 
32298 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
32299 67 65 73 20 69 6e 20 74 68 65 20 73 70 65 63 69  ges in the speci
3229a 66 69 65 64 20 64 61 74 61 62 61 73 65 2e 0a 20  fied database.. 
3229b 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
3229c 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
3229d 70 61 67 65 5f 63 6f 75 6e 74 22 29 3d 3d 30 0a  page_count")==0.
3229e 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72     || sqlite3Str
3229f 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6d 61 78 5f  ICmp(zLeft,"max_
322a0 70 61 67 65 5f 63 6f 75 6e 74 22 29 3d 3d 30 0a  page_count")==0.
322a1 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 52 65    ){.    int iRe
322a2 67 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  g;.    if( sqlit
322a3 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
322a4 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67  rse) ) goto prag
322a5 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69  ma_out;.    sqli
322a6 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
322a7 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29  ema(pParse, iDb)
322a8 3b 0a 20 20 20 20 69 52 65 67 20 3d 20 2b 2b 70  ;.    iReg = ++p
322a9 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
322aa 20 69 66 28 20 73 71 6c 69 74 65 33 54 6f 6c 6f   if( sqlite3Tolo
322ab 77 65 72 28 7a 4c 65 66 74 5b 30 5d 29 3d 3d 27  wer(zLeft[0])=='
322ac 70 27 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  p' ){.      sqli
322ad 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
322ae 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 2c 20 69   OP_Pagecount, i
322af 44 62 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 7d  Db, iReg);.    }
322b0 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
322b1 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
322b2 20 4f 50 5f 4d 61 78 50 67 63 6e 74 2c 20 69 44   OP_MaxPgcnt, iD
322b3 62 2c 20 69 52 65 67 2c 20 0a 20 20 20 20 20 20  b, iReg, .      
322b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
322b5 20 20 73 71 6c 69 74 65 33 41 62 73 49 6e 74 33    sqlite3AbsInt3
322b6 32 28 73 71 6c 69 74 65 33 41 74 6f 69 28 7a 52  2(sqlite3Atoi(zR
322b7 69 67 68 74 29 29 29 3b 0a 20 20 20 20 7d 0a 20  ight)));.    }. 
322b8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
322b9 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
322ba 74 52 6f 77 2c 20 69 52 65 67 2c 20 31 29 3b 0a  tRow, iReg, 1);.
322bb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
322bc 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
322bd 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
322be 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
322bf 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a   COLNAME_NAME, z
322c0 4c 65 66 74 2c 20 53 51 4c 49 54 45 5f 54 52 41  Left, SQLITE_TRA
322c1 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65  NSIENT);.  }else
322c2 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41  ..  /*.  **  PRA
322c3 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c  GMA [database.]l
322c4 6f 63 6b 69 6e 67 5f 6d 6f 64 65 0a 20 20 2a 2a  ocking_mode.  **
322c5 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
322c6 73 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65  se.]locking_mode
322c7 20 3d 20 28 6e 6f 72 6d 61 6c 7c 65 78 63 6c 75   = (normal|exclu
322c8 73 69 76 65 29 0a 20 20 2a 2f 0a 20 20 69 66 28  sive).  */.  if(
322c9 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
322ca 7a 4c 65 66 74 2c 22 6c 6f 63 6b 69 6e 67 5f 6d  zLeft,"locking_m
322cb 6f 64 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ode")==0 ){.    
322cc 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 74  const char *zRet
322cd 20 3d 20 22 6e 6f 72 6d 61 6c 22 3b 0a 20 20 20   = "normal";.   
322ce 20 69 6e 74 20 65 4d 6f 64 65 20 3d 20 67 65 74   int eMode = get
322cf 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 7a 52 69 67  LockingMode(zRig
322d0 68 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 49  ht);..    if( pI
322d1 64 32 2d 3e 6e 3d 3d 30 20 26 26 20 65 4d 6f 64  d2->n==0 && eMod
322d2 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
322d3 4d 4f 44 45 5f 51 55 45 52 59 20 29 7b 0a 20 20  MODE_QUERY ){.  
322d4 20 20 20 20 2f 2a 20 53 69 6d 70 6c 65 20 22 50      /* Simple "P
322d5 52 41 47 4d 41 20 6c 6f 63 6b 69 6e 67 5f 6d 6f  RAGMA locking_mo
322d6 64 65 3b 22 20 73 74 61 74 65 6d 65 6e 74 2e 20  de;" statement. 
322d7 54 68 69 73 20 69 73 20 61 20 71 75 65 72 79 20  This is a query 
322d8 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  for.      ** the
322d9 20 63 75 72 72 65 6e 74 20 64 65 66 61 75 6c 74   current default
322da 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 28 77   locking mode (w
322db 68 69 63 68 20 6d 61 79 20 62 65 20 64 69 66 66  hich may be diff
322dc 65 72 65 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a  erent to.      *
322dd 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f  * the locking-mo
322de 64 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64  de of the main d
322df 61 74 61 62 61 73 65 29 2e 0a 20 20 20 20 20 20  atabase)..      
322e0 2a 2f 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d  */.      eMode =
322e1 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64   db->dfltLockMod
322e2 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  e;.    }else{.  
322e3 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65      Pager *pPage
322e4 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64  r;.      if( pId
322e5 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  2->n==0 ){.     
322e6 20 20 20 2f 2a 20 54 68 69 73 20 69 6e 64 69 63     /* This indic
322e7 61 74 65 73 20 74 68 61 74 20 6e 6f 20 64 61 74  ates that no dat
322e8 61 62 61 73 65 20 6e 61 6d 65 20 77 61 73 20 73  abase name was s
322e9 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72 74  pecified as part
322ea 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74  .        ** of t
322eb 68 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e  he PRAGMA comman
322ec 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  d. In this case 
322ed 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65  the locking-mode
322ee 20 6d 75 73 74 20 62 65 0a 20 20 20 20 20 20 20   must be.       
322ef 20 2a 2a 20 73 65 74 20 6f 6e 20 61 6c 6c 20 61   ** set on all a
322f0 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
322f1 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 74 68  s, as well as th
322f2 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65 2e 0a  e main db file..
322f3 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
322f4 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68 65 20     ** Also, the 
322f5 73 71 6c 69 74 65 33 2e 64 66 6c 74 4c 6f 63 6b  sqlite3.dfltLock
322f6 4d 6f 64 65 20 76 61 72 69 61 62 6c 65 20 69 73  Mode variable is
322f7 20 73 65 74 20 73 6f 20 74 68 61 74 0a 20 20 20   set so that.   
322f8 20 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 62 73       ** any subs
322f9 65 71 75 65 6e 74 6c 79 20 61 74 74 61 63 68 65  equently attache
322fa 64 20 64 61 74 61 62 61 73 65 73 20 61 6c 73 6f  d databases also
322fb 20 75 73 65 20 74 68 65 20 73 70 65 63 69 66 69   use the specifi
322fc 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c 6f  ed.        ** lo
322fd 63 6b 69 6e 67 20 6d 6f 64 65 2e 0a 20 20 20 20  cking mode..    
322fe 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
322ff 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20 61  nt ii;.        a
32300 73 73 65 72 74 28 70 44 62 3d 3d 26 64 62 2d 3e  ssert(pDb==&db->
32301 61 44 62 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20  aDb[0]);.       
32302 20 66 6f 72 28 69 69 3d 32 3b 20 69 69 3c 64 62   for(ii=2; ii<db
32303 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 20  ->nDb; ii++){.  
32304 20 20 20 20 20 20 20 20 70 50 61 67 65 72 20 3d          pPager =
32305 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
32306 65 72 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70  er(db->aDb[ii].p
32307 42 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  Bt);.          s
32308 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69  qlite3PagerLocki
32309 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 65  ngMode(pPager, e
3230a 4d 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20 7d  Mode);.        }
3230b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 64 66 6c  .        db->dfl
3230c 74 4c 6f 63 6b 4d 6f 64 65 20 3d 20 28 75 38 29  tLockMode = (u8)
3230d 65 4d 6f 64 65 3b 0a 20 20 20 20 20 20 7d 0a 20  eMode;.      }. 
3230e 20 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71       pPager = sq
3230f 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
32310 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20  pDb->pBt);.     
32311 20 65 4d 6f 64 65 20 3d 20 73 71 6c 69 74 65 33   eMode = sqlite3
32312 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65  PagerLockingMode
32313 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b  (pPager, eMode);
32314 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65  .    }..    asse
32315 72 74 28 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rt(eMode==PAGER_
32316 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d  LOCKINGMODE_NORM
32317 41 4c 7c 7c 65 4d 6f 64 65 3d 3d 50 41 47 45 52  AL||eMode==PAGER
32318 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43  _LOCKINGMODE_EXC
32319 4c 55 53 49 56 45 29 3b 0a 20 20 20 20 69 66 28  LUSIVE);.    if(
3231a 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f   eMode==PAGER_LO
3231b 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53  CKINGMODE_EXCLUS
3231c 49 56 45 20 29 7b 0a 20 20 20 20 20 20 7a 52 65  IVE ){.      zRe
3231d 74 20 3d 20 22 65 78 63 6c 75 73 69 76 65 22 3b  t = "exclusive";
3231e 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
3231f 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
32320 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  (v, 1);.    sqli
32321 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
32322 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
32323 4e 41 4d 45 2c 20 22 6c 6f 63 6b 69 6e 67 5f 6d  NAME, "locking_m
32324 6f 64 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ode", SQLITE_STA
32325 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  TIC);.    sqlite
32326 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
32327 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31 2c  P_String8, 0, 1,
32328 20 30 2c 20 7a 52 65 74 2c 20 30 29 3b 0a 20 20   0, zRet, 0);.  
32329 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3232a 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
3232b 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 65  Row, 1, 1);.  }e
3232c 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  lse..  /*.  **  
3232d 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
3232e 2e 5d 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 0a 20  .]journal_mode. 
3232f 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
32330 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 6d  abase.]journal_m
32331 6f 64 65 20 3d 0a 20 20 2a 2a 20 20 20 20 20 20  ode =.  **      
32332 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32333 28 64 65 6c 65 74 65 7c 70 65 72 73 69 73 74 7c  (delete|persist|
32334 6f 66 66 7c 74 72 75 6e 63 61 74 65 7c 6d 65 6d  off|truncate|mem
32335 6f 72 79 7c 77 61 6c 7c 6f 66 66 29 0a 20 20 2a  ory|wal|off).  *
32336 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
32337 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6a 6f  trICmp(zLeft,"jo
32338 75 72 6e 61 6c 5f 6d 6f 64 65 22 29 3d 3d 30 20  urnal_mode")==0 
32339 29 7b 0a 20 20 20 20 69 6e 74 20 65 4d 6f 64 65  ){.    int eMode
3233a 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20  ;        /* One 
3233b 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4a 4f 55  of the PAGER_JOU
3233c 52 4e 41 4c 4d 4f 44 45 5f 58 58 58 20 73 79 6d  RNALMODE_XXX sym
3233d 62 6f 6c 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20  bols */.    int 
3233e 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ii;           /*
3233f 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
32340 0a 0a 20 20 20 20 2f 2a 20 46 6f 72 63 65 20 74  ..    /* Force t
32341 68 65 20 73 63 68 65 6d 61 20 74 6f 20 62 65 20  he schema to be 
32342 6c 6f 61 64 65 64 20 6f 6e 20 61 6c 6c 20 64 61  loaded on all da
32343 74 61 62 61 73 65 73 2e 20 20 54 68 69 73 20 63  tabases.  This c
32344 61 75 73 65 73 20 61 6c 6c 0a 20 20 20 20 2a 2a  auses all.    **
32345 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
32346 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 61 6e 64  to be opened and
32347 20 74 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64   the journal_mod
32348 65 73 20 73 65 74 2e 20 20 54 68 69 73 20 69 73  es set.  This is
32349 0a 20 20 20 20 2a 2a 20 6e 65 63 65 73 73 61 72  .    ** necessar
3234a 79 20 62 65 63 61 75 73 65 20 73 75 62 73 65 71  y because subseq
3234b 75 65 6e 74 20 70 72 6f 63 65 73 73 69 6e 67 20  uent processing 
3234c 6d 75 73 74 20 6b 6e 6f 77 20 69 66 20 74 68 65  must know if the
3234d 20 64 61 74 61 62 61 73 65 73 0a 20 20 20 20 2a   databases.    *
3234e 2a 20 61 72 65 20 69 6e 20 57 41 4c 20 6d 6f 64  * are in WAL mod
3234f 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71  e. */.    if( sq
32350 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
32351 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20  pParse) ){.     
32352 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
32353 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c  ;.    }..    sql
32354 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
32355 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71  ls(v, 1);.    sq
32356 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
32357 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
32358 45 5f 4e 41 4d 45 2c 20 22 6a 6f 75 72 6e 61 6c  E_NAME, "journal
32359 5f 6d 6f 64 65 22 2c 20 53 51 4c 49 54 45 5f 53  _mode", SQLITE_S
3235a 54 41 54 49 43 29 3b 0a 0a 20 20 20 20 69 66 28  TATIC);..    if(
3235b 20 7a 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20   zRight==0 ){.  
3235c 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
3235d 69 73 20 6e 6f 20 22 3d 4d 4f 44 45 22 20 70 61  is no "=MODE" pa
3235e 72 74 20 6f 66 20 74 68 65 20 70 72 61 67 6d 61  rt of the pragma
3235f 2c 20 64 6f 20 61 20 71 75 65 72 79 20 66 6f 72  , do a query for
32360 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 63 75   the.      ** cu
32361 72 72 65 6e 74 20 6d 6f 64 65 20 2a 2f 0a 20 20  rrent mode */.  
32362 20 20 20 20 65 4d 6f 64 65 20 3d 20 50 41 47 45      eMode = PAGE
32363 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
32364 45 52 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ERY;.    }else{.
32365 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
32366 20 2a 7a 4d 6f 64 65 3b 0a 20 20 20 20 20 20 69   *zMode;.      i
32367 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  nt n = sqlite3St
32368 72 6c 65 6e 33 30 28 7a 52 69 67 68 74 29 3b 0a  rlen30(zRight);.
32369 20 20 20 20 20 20 66 6f 72 28 65 4d 6f 64 65 3d        for(eMode=
3236a 30 3b 20 28 7a 4d 6f 64 65 20 3d 20 73 71 6c 69  0; (zMode = sqli
3236b 74 65 33 4a 6f 75 72 6e 61 6c 4d 6f 64 65 6e 61  te3JournalModena
3236c 6d 65 28 65 4d 6f 64 65 29 29 21 3d 30 3b 20 65  me(eMode))!=0; e
3236d 4d 6f 64 65 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Mode++){.       
3236e 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e   if( sqlite3StrN
3236f 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 7a 4d 6f  ICmp(zRight, zMo
32370 64 65 2c 20 6e 29 3d 3d 30 20 29 20 62 72 65 61  de, n)==0 ) brea
32371 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
32372 20 69 66 28 20 21 7a 4d 6f 64 65 20 29 7b 0a 20   if( !zMode ){. 
32373 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65         /* If the
32374 20 22 3d 4d 4f 44 45 22 20 70 61 72 74 20 64 6f   "=MODE" part do
32375 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79  es not match any
32376 20 6b 6e 6f 77 6e 20 6a 6f 75 72 6e 61 6c 20 6d   known journal m
32377 6f 64 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20  ode,.        ** 
32378 74 68 65 6e 20 64 6f 20 61 20 71 75 65 72 79 20  then do a query 
32379 2a 2f 0a 20 20 20 20 20 20 20 20 65 4d 6f 64 65  */.        eMode
3237a 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c   = PAGER_JOURNAL
3237b 4d 4f 44 45 5f 51 55 45 52 59 3b 0a 20 20 20 20  MODE_QUERY;.    
3237c 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
3237d 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  ( eMode==PAGER_J
3237e 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59  OURNALMODE_QUERY
3237f 20 26 26 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 29   && pId2->n==0 )
32380 7b 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65  {.      /* Conve
32381 72 74 20 22 50 52 41 47 4d 41 20 6a 6f 75 72 6e  rt "PRAGMA journ
32382 61 6c 5f 6d 6f 64 65 22 20 69 6e 74 6f 20 22 50  al_mode" into "P
32383 52 41 47 4d 41 20 6d 61 69 6e 2e 6a 6f 75 72 6e  RAGMA main.journ
32384 61 6c 5f 6d 6f 64 65 22 20 2a 2f 0a 20 20 20 20  al_mode" */.    
32385 20 20 69 44 62 20 3d 20 30 3b 0a 20 20 20 20 20    iDb = 0;.     
32386 20 70 49 64 32 2d 3e 6e 20 3d 20 31 3b 0a 20 20   pId2->n = 1;.  
32387 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d 64    }.    for(ii=d
32388 62 2d 3e 6e 44 62 2d 31 3b 20 69 69 3e 3d 30 3b  b->nDb-1; ii>=0;
32389 20 69 69 2d 2d 29 7b 0a 20 20 20 20 20 20 69 66   ii--){.      if
3238a 28 20 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42  ( db->aDb[ii].pB
3238b 74 20 26 26 20 28 69 69 3d 3d 69 44 62 20 7c 7c  t && (ii==iDb ||
3238c 20 70 49 64 32 2d 3e 6e 3d 3d 30 29 20 29 7b 0a   pId2->n==0) ){.
3238d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
3238e 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20  dbeUsesBtree(v, 
3238f 69 69 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ii);.        sql
32390 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
32391 2c 20 4f 50 5f 4a 6f 75 72 6e 61 6c 4d 6f 64 65  , OP_JournalMode
32392 2c 20 69 69 2c 20 31 2c 20 65 4d 6f 64 65 29 3b  , ii, 1, eMode);
32393 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
32394 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
32395 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
32396 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 7d  tRow, 1, 1);.  }
32397 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  else..  /*.  ** 
32398 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
32399 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f  e.]journal_size_
3239a 6c 69 6d 69 74 0a 20 20 2a 2a 20 20 50 52 41 47  limit.  **  PRAG
3239b 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f  MA [database.]jo
3239c 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74  urnal_size_limit
3239d 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 47 65 74  =N.  **.  ** Get
3239e 20 6f 72 20 73 65 74 20 74 68 65 20 73 69 7a 65   or set the size
3239f 20 6c 69 6d 69 74 20 6f 6e 20 72 6f 6c 6c 62 61   limit on rollba
323a0 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73  ck journal files
323a1 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
323a2 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
323a3 74 2c 22 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f  t,"journal_size_
323a4 6c 69 6d 69 74 22 29 3d 3d 30 20 29 7b 0a 20 20  limit")==0 ){.  
323a5 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20    Pager *pPager 
323a6 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  = sqlite3BtreePa
323a7 67 65 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20  ger(pDb->pBt);. 
323a8 20 20 20 69 36 34 20 69 4c 69 6d 69 74 20 3d 20     i64 iLimit = 
323a9 2d 32 3b 0a 20 20 20 20 69 66 28 20 7a 52 69 67  -2;.    if( zRig
323aa 68 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ht ){.      sqli
323ab 74 65 33 41 74 6f 69 36 34 28 7a 52 69 67 68 74  te3Atoi64(zRight
323ac 2c 20 26 69 4c 69 6d 69 74 2c 20 31 30 30 30 30  , &iLimit, 10000
323ad 30 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29  00, SQLITE_UTF8)
323ae 3b 0a 20 20 20 20 20 20 69 66 28 20 69 4c 69 6d  ;.      if( iLim
323af 69 74 3c 2d 31 20 29 20 69 4c 69 6d 69 74 20 3d  it<-1 ) iLimit =
323b0 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   -1;.    }.    i
323b1 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 50  Limit = sqlite3P
323b2 61 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c  agerJournalSizeL
323b3 69 6d 69 74 28 70 50 61 67 65 72 2c 20 69 4c 69  imit(pPager, iLi
323b4 6d 69 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  mit);.    return
323b5 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65  SingleInt(pParse
323b6 2c 20 22 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f  , "journal_size_
323b7 6c 69 6d 69 74 22 2c 20 69 4c 69 6d 69 74 29 3b  limit", iLimit);
323b8 0a 20 20 7d 65 6c 73 65 0a 0a 23 65 6e 64 69 66  .  }else..#endif
323b9 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
323ba 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
323bb 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41  ..  /*.  **  PRA
323bc 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 61  GMA [database.]a
323bd 75 74 6f 5f 76 61 63 75 75 6d 0a 20 20 2a 2a 20  uto_vacuum.  ** 
323be 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
323bf 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75 6d 3d 4e  e.]auto_vacuum=N
323c0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 47 65 74 20 6f  .  **.  ** Get o
323c1 72 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  r set the value 
323c2 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
323c3 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 61  'auto-vacuum' pa
323c4 72 61 6d 65 74 65 72 2e 0a 20 20 2a 2a 20 54 68  rameter..  ** Th
323c5 65 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 20 6f  e value is one o
323c6 66 3a 20 20 30 20 4e 4f 4e 45 20 31 20 46 55 4c  f:  0 NONE 1 FUL
323c7 4c 20 32 20 49 4e 43 52 45 4d 45 4e 54 41 4c 0a  L 2 INCREMENTAL.
323c8 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
323c9 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
323ca 55 55 4d 0a 20 20 69 66 28 20 73 71 6c 69 74 65  UUM.  if( sqlite
323cb 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
323cc 61 75 74 6f 5f 76 61 63 75 75 6d 22 29 3d 3d 30  auto_vacuum")==0
323cd 20 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70   ){.    Btree *p
323ce 42 74 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20  Bt = pDb->pBt;. 
323cf 20 20 20 61 73 73 65 72 74 28 20 70 42 74 21 3d     assert( pBt!=
323d0 30 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  0 );.    if( sql
323d1 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
323d2 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20  Parse) ){.      
323d3 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b  goto pragma_out;
323d4 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
323d5 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
323d6 69 6e 74 20 61 75 74 6f 5f 76 61 63 75 75 6d 3b  int auto_vacuum;
323d7 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59  .      if( ALWAY
323d8 53 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  S(pBt) ){.      
323d9 20 20 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d     auto_vacuum =
323da 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
323db 41 75 74 6f 56 61 63 75 75 6d 28 70 42 74 29 3b  AutoVacuum(pBt);
323dc 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
323dd 20 20 20 20 20 20 20 61 75 74 6f 5f 76 61 63 75         auto_vacu
323de 75 6d 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41  um = SQLITE_DEFA
323df 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 3b 0a  ULT_AUTOVACUUM;.
323e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
323e1 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50  turnSingleInt(pP
323e2 61 72 73 65 2c 20 22 61 75 74 6f 5f 76 61 63 75  arse, "auto_vacu
323e3 75 6d 22 2c 20 61 75 74 6f 5f 76 61 63 75 75 6d  um", auto_vacuum
323e4 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
323e5 20 20 20 20 69 6e 74 20 65 41 75 74 6f 20 3d 20      int eAuto = 
323e6 67 65 74 41 75 74 6f 56 61 63 75 75 6d 28 7a 52  getAutoVacuum(zR
323e7 69 67 68 74 29 3b 0a 20 20 20 20 20 20 61 73 73  ight);.      ass
323e8 65 72 74 28 20 65 41 75 74 6f 3e 3d 30 20 26 26  ert( eAuto>=0 &&
323e9 20 65 41 75 74 6f 3c 3d 32 20 29 3b 0a 20 20 20   eAuto<=2 );.   
323ea 20 20 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76     db->nextAutov
323eb 61 63 20 3d 20 28 75 38 29 65 41 75 74 6f 3b 0a  ac = (u8)eAuto;.
323ec 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53        if( ALWAYS
323ed 28 65 41 75 74 6f 3e 3d 30 29 20 29 7b 0a 20 20  (eAuto>=0) ){.  
323ee 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 53 65        /* Call Se
323ef 74 41 75 74 6f 56 61 63 75 75 6d 28 29 20 74 6f  tAutoVacuum() to
323f0 20 73 65 74 20 69 6e 69 74 69 61 6c 69 7a 65 20   set initialize 
323f1 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 61 75 74  the internal aut
323f2 6f 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a  o and.        **
323f3 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 66 6c 61   incr-vacuum fla
323f4 67 73 2e 20 54 68 69 73 20 69 73 20 72 65 71 75  gs. This is requ
323f5 69 72 65 64 20 69 6e 20 63 61 73 65 20 74 68 69  ired in case thi
323f6 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20  s connection.   
323f7 20 20 20 20 20 2a 2a 20 63 72 65 61 74 65 73 20       ** creates 
323f8 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
323f9 65 2e 20 49 74 20 69 73 20 69 6d 70 6f 72 74 61  e. It is importa
323fa 6e 74 20 74 68 61 74 20 69 74 20 69 73 20 63 72  nt that it is cr
323fb 65 61 74 65 64 0a 20 20 20 20 20 20 20 20 2a 2a  eated.        **
323fc 20 61 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75   as an auto-vacu
323fd 75 6d 20 63 61 70 61 62 6c 65 20 64 62 2e 0a 20  um capable db.. 
323fe 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
323ff 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
32400 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d  reeSetAutoVacuum
32401 28 70 42 74 2c 20 65 41 75 74 6f 29 3b 0a 20 20  (pBt, eAuto);.  
32402 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
32403 4c 49 54 45 5f 4f 4b 20 26 26 20 28 65 41 75 74  LITE_OK && (eAut
32404 6f 3d 3d 31 20 7c 7c 20 65 41 75 74 6f 3d 3d 32  o==1 || eAuto==2
32405 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  ) ){.          /
32406 2a 20 57 68 65 6e 20 73 65 74 74 69 6e 67 20 74  * When setting t
32407 68 65 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 6d  he auto_vacuum m
32408 6f 64 65 20 74 6f 20 65 69 74 68 65 72 20 22 66  ode to either "f
32409 75 6c 6c 22 20 6f 72 20 0a 20 20 20 20 20 20 20  ull" or .       
3240a 20 20 20 2a 2a 20 22 69 6e 63 72 65 6d 65 6e 74     ** "increment
3240b 61 6c 22 2c 20 77 72 69 74 65 20 74 68 65 20 76  al", write the v
3240c 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b 36 5d 20  alue of meta[6] 
3240d 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  in the database.
3240e 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c            ** fil
3240f 65 2e 20 42 65 66 6f 72 65 20 77 72 69 74 69 6e  e. Before writin
32410 67 20 74 6f 20 6d 65 74 61 5b 36 5d 2c 20 63 68  g to meta[6], ch
32411 65 63 6b 20 74 68 61 74 20 6d 65 74 61 5b 33 5d  eck that meta[3]
32412 20 69 6e 64 69 63 61 74 65 73 0a 20 20 20 20 20   indicates.     
32413 20 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 69       ** that thi
32414 73 20 72 65 61 6c 6c 79 20 69 73 20 61 6e 20 61  s really is an a
32415 75 74 6f 2d 76 61 63 75 75 6d 20 63 61 70 61 62  uto-vacuum capab
32416 6c 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20  le database..   
32417 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
32418 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
32419 20 56 64 62 65 4f 70 4c 69 73 74 20 73 65 74 4d   VdbeOpList setM
3241a 65 74 61 36 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  eta6[] = {.     
3241b 20 20 20 20 20 20 20 7b 20 4f 50 5f 54 72 61 6e         { OP_Tran
3241c 73 61 63 74 69 6f 6e 2c 20 20 20 20 30 2c 20 20  saction,    0,  
3241d 20 20 20 20 20 20 20 31 2c 20 20 20 20 20 20 20         1,       
3241e 20 20 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20            0},   
3241f 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 20   /* 0 */.       
32420 20 20 20 20 20 7b 20 4f 50 5f 52 65 61 64 43 6f       { OP_ReadCo
32421 6f 6b 69 65 2c 20 20 20 20 20 30 2c 20 20 20 20  okie,     0,    
32422 20 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20       1,         
32423 42 54 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f  BTREE_LARGEST_RO
32424 4f 54 5f 50 41 47 45 7d 2c 0a 20 20 20 20 20 20  OT_PAGE},.      
32425 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 2c 20 20        { OP_If,  
32426 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 20 20             1,   
32427 20 20 20 20 20 20 30 2c 20 20 20 20 20 20 20 20        0,        
32428 20 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20           0},    
32429 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 2 */.        
3242a 20 20 20 20 7b 20 4f 50 5f 48 61 6c 74 2c 20 20      { OP_Halt,  
3242b 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
3242c 4f 4b 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 20 20  OK, OE_Abort,   
3242d 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a         0},    /*
3242e 20 33 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20   3 */.          
3242f 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20    { OP_Integer, 
32430 20 20 20 20 20 20 20 30 2c 20 20 20 20 20 20 20         0,       
32431 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20    1,            
32432 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 34       0},    /* 4
32433 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
32434 7b 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20  { OP_SetCookie, 
32435 20 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20       0,         
32436 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 43 55 55  BTREE_INCR_VACUU
32437 4d 2c 20 31 7d 2c 20 20 20 20 2f 2a 20 35 20 2a  M, 1},    /* 5 *
32438 2f 0a 20 20 20 20 20 20 20 20 20 20 7d 3b 0a 20  /.          };. 
32439 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 41 64           int iAd
3243a 64 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69 41  dr;.          iA
3243b 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
3243c 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72  eAddOpList(v, Ar
3243d 72 61 79 53 69 7a 65 28 73 65 74 4d 65 74 61 36  raySize(setMeta6
3243e 29 2c 20 73 65 74 4d 65 74 61 36 29 3b 0a 20 20  ), setMeta6);.  
3243f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
32440 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69  dbeChangeP1(v, i
32441 41 64 64 72 2c 20 69 44 62 29 3b 0a 20 20 20 20  Addr, iDb);.    
32442 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
32443 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64  eChangeP1(v, iAd
32444 64 72 2b 31 2c 20 69 44 62 29 3b 0a 20 20 20 20  dr+1, iDb);.    
32445 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
32446 65 43 68 61 6e 67 65 50 32 28 76 2c 20 69 41 64  eChangeP2(v, iAd
32447 64 72 2b 32 2c 20 69 41 64 64 72 2b 34 29 3b 0a  dr+2, iAddr+4);.
32448 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
32449 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
3244a 20 69 41 64 64 72 2b 34 2c 20 65 41 75 74 6f 2d   iAddr+4, eAuto-
3244b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  1);.          sq
3244c 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
3244d 31 28 76 2c 20 69 41 64 64 72 2b 35 2c 20 69 44  1(v, iAddr+5, iD
3244e 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  b);.          sq
3244f 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72  lite3VdbeUsesBtr
32450 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20  ee(v, iDb);.    
32451 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
32452 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64    }.  }else.#end
32453 69 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50  if..  /*.  **  P
32454 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
32455 5d 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 76 61 63  ]incremental_vac
32456 75 75 6d 28 4e 29 0a 20 20 2a 2a 0a 20 20 2a 2a  uum(N).  **.  **
32457 20 44 6f 20 4e 20 73 74 65 70 73 20 6f 66 20 69   Do N steps of i
32458 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75  ncremental vacuu
32459 6d 69 6e 67 20 6f 6e 20 61 20 64 61 74 61 62 61  ming on a databa
3245a 73 65 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  se..  */.#ifndef
3245b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
3245c 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 20 73 71  OVACUUM.  if( sq
3245d 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
3245e 66 74 2c 22 69 6e 63 72 65 6d 65 6e 74 61 6c 5f  ft,"incremental_
3245f 76 61 63 75 75 6d 22 29 3d 3d 30 20 29 7b 0a 20  vacuum")==0 ){. 
32460 20 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20 61     int iLimit, a
32461 64 64 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  ddr;.    if( sql
32462 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
32463 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20  Parse) ){.      
32464 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b  goto pragma_out;
32465 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a  .    }.    if( z
32466 52 69 67 68 74 3d 3d 30 20 7c 7c 20 21 73 71 6c  Right==0 || !sql
32467 69 74 65 33 47 65 74 49 6e 74 33 32 28 7a 52 69  ite3GetInt32(zRi
32468 67 68 74 2c 20 26 69 4c 69 6d 69 74 29 20 7c 7c  ght, &iLimit) ||
32469 20 69 4c 69 6d 69 74 3c 3d 30 20 29 7b 0a 20 20   iLimit<=0 ){.  
3246a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 30 78 37      iLimit = 0x7
3246b 66 66 66 66 66 66 66 3b 0a 20 20 20 20 7d 0a 20  fffffff;.    }. 
3246c 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57     sqlite3BeginW
3246d 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
3246e 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20  arse, 0, iDb);. 
3246f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
32470 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
32471 65 72 2c 20 69 4c 69 6d 69 74 2c 20 31 29 3b 0a  er, iLimit, 1);.
32472 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
32473 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
32474 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 2c 20 69  OP_IncrVacuum, i
32475 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
32476 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
32477 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 29 3b 0a  _ResultRow, 1);.
32478 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
32479 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
3247a 6d 6d 2c 20 31 2c 20 2d 31 29 3b 0a 20 20 20 20  mm, 1, -1);.    
3247b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
3247c 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31  2(v, OP_IfPos, 1
3247d 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 73 71 6c  , addr);.    sql
3247e 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
3247f 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d 65 6c  (v, addr);.  }el
32480 73 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  se.#endif..#ifnd
32481 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ef SQLITE_OMIT_P
32482 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 20 20 2f  AGER_PRAGMAS.  /
32483 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  *.  **  PRAGMA [
32484 64 61 74 61 62 61 73 65 2e 5d 63 61 63 68 65 5f  database.]cache_
32485 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d  size.  **  PRAGM
32486 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 63 61 63  A [database.]cac
32487 68 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20  he_size=N.  **. 
32488 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f   ** The first fo
32489 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20 63  rm reports the c
3248a 75 72 72 65 6e 74 20 6c 6f 63 61 6c 20 73 65 74  urrent local set
3248b 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a  ting for the.  *
3248c 2a 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a  * page cache siz
3248d 65 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f  e. The second fo
3248e 72 6d 20 73 65 74 73 20 74 68 65 20 6c 6f 63 61  rm sets the loca
3248f 6c 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68  l.  ** page cach
32490 65 20 73 69 7a 65 20 76 61 6c 75 65 2e 20 20 49  e size value.  I
32491 66 20 4e 20 69 73 20 70 6f 73 69 74 69 76 65 20  f N is positive 
32492 74 68 65 6e 20 74 68 61 74 20 69 73 20 74 68 65  then that is the
32493 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  .  ** number of 
32494 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63  pages in the cac
32495 68 65 2e 20 20 49 66 20 4e 20 69 73 20 6e 65 67  he.  If N is neg
32496 61 74 69 76 65 2c 20 74 68 65 6e 20 74 68 65 0a  ative, then the.
32497 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70    ** number of p
32498 61 67 65 73 20 69 73 20 61 64 6a 75 73 74 65 64  ages is adjusted
32499 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 61 63   so that the cac
3249a 68 65 20 75 73 65 73 20 2d 4e 20 6b 69 62 69 62  he uses -N kibib
3249b 79 74 65 73 0a 20 20 2a 2a 20 6f 66 20 6d 65 6d  ytes.  ** of mem
3249c 6f 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ory..  */.  if( 
3249d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
3249e 4c 65 66 74 2c 22 63 61 63 68 65 5f 73 69 7a 65  Left,"cache_size
3249f 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  ")==0 ){.    if(
324a0 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
324a1 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
324a2 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
324a3 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
324a4 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64  3SchemaMutexHeld
324a5 28 64 62 2c 20 69 44 62 2c 20 30 29 20 29 3b 0a  (db, iDb, 0) );.
324a6 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20      if( !zRight 
324a7 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53  ){.      returnS
324a8 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c  ingleInt(pParse,
324a9 20 22 63 61 63 68 65 5f 73 69 7a 65 22 2c 20 70   "cache_size", p
324aa 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63  Db->pSchema->cac
324ab 68 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d 65  he_size);.    }e
324ac 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 73  lse{.      int s
324ad 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 41 74 6f  ize = sqlite3Ato
324ae 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  i(zRight);.     
324af 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63   pDb->pSchema->c
324b0 61 63 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65  ache_size = size
324b1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ;.      sqlite3B
324b2 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65  treeSetCacheSize
324b3 28 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e  (pDb->pBt, pDb->
324b4 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73  pSchema->cache_s
324b5 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  ize);.    }.  }e
324b6 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  lse..  /*.  **  
324b7 20 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f   PRAGMA temp_sto
324b8 72 65 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  re.  **   PRAGMA
324b9 20 74 65 6d 70 5f 73 74 6f 72 65 20 3d 20 22 64   temp_store = "d
324ba 65 66 61 75 6c 74 22 7c 22 6d 65 6d 6f 72 79 22  efault"|"memory"
324bb 7c 22 66 69 6c 65 22 0a 20 20 2a 2a 0a 20 20 2a  |"file".  **.  *
324bc 2a 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74 20  * Return or set 
324bd 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20  the local value 
324be 6f 66 20 74 68 65 20 74 65 6d 70 5f 73 74 6f 72  of the temp_stor
324bf 65 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e  e flag.  Changin
324c0 67 0a 20 20 2a 2a 20 74 68 65 20 6c 6f 63 61 6c  g.  ** the local
324c1 20 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74 20   value does not 
324c2 6d 61 6b 65 20 63 68 61 6e 67 65 73 20 74 6f 20  make changes to 
324c3 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 61 6e  the disk file an
324c4 64 20 74 68 65 20 64 65 66 61 75 6c 74 0a 20 20  d the default.  
324c5 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 65  ** value will be
324c6 20 72 65 73 74 6f 72 65 64 20 74 68 65 20 6e 65   restored the ne
324c7 78 74 20 74 69 6d 65 20 74 68 65 20 64 61 74 61  xt time the data
324c8 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 2e 0a  base is opened..
324c9 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74    **.  ** Note t
324ca 68 61 74 20 69 74 20 69 73 20 70 6f 73 73 69 62  hat it is possib
324cb 6c 65 20 66 6f 72 20 74 68 65 20 6c 69 62 72 61  le for the libra
324cc 72 79 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  ry compile-time 
324cd 6f 70 74 69 6f 6e 73 20 74 6f 0a 20 20 2a 2a 20  options to.  ** 
324ce 6f 76 65 72 72 69 64 65 20 74 68 69 73 20 73 65  override this se
324cf 74 74 69 6e 67 0a 20 20 2a 2f 0a 20 20 69 66 28  tting.  */.  if(
324d0 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
324d1 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f 73 74 6f  zLeft, "temp_sto
324d2 72 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  re")==0 ){.    i
324d3 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20  f( !zRight ){.  
324d4 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65      returnSingle
324d5 49 6e 74 28 70 50 61 72 73 65 2c 20 22 74 65 6d  Int(pParse, "tem
324d6 70 5f 73 74 6f 72 65 22 2c 20 64 62 2d 3e 74 65  p_store", db->te
324d7 6d 70 5f 73 74 6f 72 65 29 3b 0a 20 20 20 20 7d  mp_store);.    }
324d8 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 61 6e  else{.      chan
324d9 67 65 54 65 6d 70 53 74 6f 72 61 67 65 28 70 50  geTempStorage(pP
324da 61 72 73 65 2c 20 7a 52 69 67 68 74 29 3b 0a 20  arse, zRight);. 
324db 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20     }.  }else..  
324dc 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  /*.  **   PRAGMA
324dd 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65   temp_store_dire
324de 63 74 6f 72 79 0a 20 20 2a 2a 20 20 20 50 52 41  ctory.  **   PRA
324df 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64  GMA temp_store_d
324e0 69 72 65 63 74 6f 72 79 20 3d 20 22 22 7c 22 64  irectory = ""|"d
324e1 69 72 65 63 74 6f 72 79 5f 6e 61 6d 65 22 0a 20  irectory_name". 
324e2 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20   **.  ** Return 
324e3 6f 72 20 73 65 74 20 74 68 65 20 6c 6f 63 61 6c  or set the local
324e4 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74 65   value of the te
324e5 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f  mp_store_directo
324e6 72 79 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69  ry flag.  Changi
324e7 6e 67 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c 75  ng.  ** the valu
324e8 65 20 73 65 74 73 20 61 20 73 70 65 63 69 66 69  e sets a specifi
324e9 63 20 64 69 72 65 63 74 6f 72 79 20 74 6f 20 62  c directory to b
324ea 65 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70 6f  e used for tempo
324eb 72 61 72 79 20 66 69 6c 65 73 2e 0a 20 20 2a 2a  rary files..  **
324ec 20 53 65 74 74 69 6e 67 20 74 6f 20 61 20 6e 75   Setting to a nu
324ed 6c 6c 20 73 74 72 69 6e 67 20 72 65 76 65 72 74  ll string revert
324ee 73 20 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74  s to the default
324ef 20 74 65 6d 70 6f 72 61 72 79 20 64 69 72 65 63   temporary direc
324f0 74 6f 72 79 20 73 65 61 72 63 68 2e 0a 20 20 2a  tory search..  *
324f1 2a 20 49 66 20 74 65 6d 70 6f 72 61 72 79 20 64  * If temporary d
324f2 69 72 65 63 74 6f 72 79 20 69 73 20 63 68 61 6e  irectory is chan
324f3 67 65 64 2c 20 74 68 65 6e 20 69 6e 76 61 6c 69  ged, then invali
324f4 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65 2e  dateTempStorage.
324f5 0a 20 20 2a 2a 0a 20 20 2a 2f 0a 20 20 69 66 28  .  **.  */.  if(
324f6 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
324f7 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f 73 74 6f  zLeft, "temp_sto
324f8 72 65 5f 64 69 72 65 63 74 6f 72 79 22 29 3d 3d  re_directory")==
324f9 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 7a 52  0 ){.    if( !zR
324fa 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69 66  ight ){.      if
324fb 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64  ( sqlite3_temp_d
324fc 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20  irectory ){.    
324fd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
324fe 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
324ff 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
32500 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
32501 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
32502 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  E, .            
32503 22 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65  "temp_store_dire
32504 63 74 6f 72 79 22 2c 20 53 51 4c 49 54 45 5f 53  ctory", SQLITE_S
32505 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
32506 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
32507 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
32508 20 30 2c 20 31 2c 20 30 2c 20 73 71 6c 69 74 65   0, 1, 0, sqlite
32509 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
3250a 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
3250b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
3250c 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
3250d 20 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a   1, 1);.      }.
3250e 20 20 20 20 7d 65 6c 73 65 7b 0a 23 69 66 6e 64      }else{.#ifnd
3250f 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57  ef SQLITE_OMIT_W
32510 53 44 0a 20 20 20 20 20 20 69 66 28 20 7a 52 69  SD.      if( zRi
32511 67 68 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20  ght[0] ){.      
32512 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20    int res;.     
32513 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
32514 73 41 63 63 65 73 73 28 64 62 2d 3e 70 56 66 73  sAccess(db->pVfs
32515 2c 20 7a 52 69 67 68 74 2c 20 53 51 4c 49 54 45  , zRight, SQLITE
32516 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54  _ACCESS_READWRIT
32517 45 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20  E, &res);.      
32518 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
32519 5f 4f 4b 20 7c 7c 20 72 65 73 3d 3d 30 20 29 7b  _OK || res==0 ){
3251a 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
3251b 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
3251c 65 2c 20 22 6e 6f 74 20 61 20 77 72 69 74 61 62  e, "not a writab
3251d 6c 65 20 64 69 72 65 63 74 6f 72 79 22 29 3b 0a  le directory");.
3251e 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 70            goto p
3251f 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 20  ragma_out;.     
32520 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
32521 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54 45     if( SQLITE_TE
32522 4d 50 5f 53 54 4f 52 45 3d 3d 30 0a 20 20 20 20  MP_STORE==0.    
32523 20 20 20 7c 7c 20 28 53 51 4c 49 54 45 5f 54 45     || (SQLITE_TE
32524 4d 50 5f 53 54 4f 52 45 3d 3d 31 20 26 26 20 64  MP_STORE==1 && d
32525 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3c 3d 31  b->temp_store<=1
32526 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 53 51 4c  ).       || (SQL
32527 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d  ITE_TEMP_STORE==
32528 32 20 26 26 20 64 62 2d 3e 74 65 6d 70 5f 73 74  2 && db->temp_st
32529 6f 72 65 3d 3d 31 29 0a 20 20 20 20 20 20 29 7b  ore==1).      ){
3252a 0a 20 20 20 20 20 20 20 20 69 6e 76 61 6c 69 64  .        invalid
3252b 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28 70  ateTempStorage(p
3252c 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a  Parse);.      }.
3252d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
3252e 65 65 28 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f  ee(sqlite3_temp_
3252f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 20 20  directory);.    
32530 20 20 69 66 28 20 7a 52 69 67 68 74 5b 30 5d 20    if( zRight[0] 
32531 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
32532 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72  e3_temp_director
32533 79 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  y = sqlite3_mpri
32534 6e 74 66 28 22 25 73 22 2c 20 7a 52 69 67 68 74  ntf("%s", zRight
32535 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
32536 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
32537 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d  temp_directory =
32538 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64   0;.      }.#end
32539 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
3253a 54 5f 57 53 44 20 2a 2f 0a 20 20 20 20 7d 0a 20  T_WSD */.    }. 
3253b 20 7d 65 6c 73 65 0a 0a 23 69 66 20 53 51 4c 49   }else..#if SQLI
3253c 54 45 5f 4f 53 5f 57 49 4e 0a 20 20 2f 2a 0a 20  TE_OS_WIN.  /*. 
3253d 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 64 61 74   **   PRAGMA dat
3253e 61 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72  a_store_director
3253f 79 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  y.  **   PRAGMA 
32540 64 61 74 61 5f 73 74 6f 72 65 5f 64 69 72 65 63  data_store_direc
32541 74 6f 72 79 20 3d 20 22 22 7c 22 64 69 72 65 63  tory = ""|"direc
32542 74 6f 72 79 5f 6e 61 6d 65 22 0a 20 20 2a 2a 0a  tory_name".  **.
32543 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73    ** Return or s
32544 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c  et the local val
32545 75 65 20 6f 66 20 74 68 65 20 64 61 74 61 5f 73  ue of the data_s
32546 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 66  tore_directory f
32547 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20  lag.  Changing. 
32548 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 73 65   ** the value se
32549 74 73 20 61 20 73 70 65 63 69 66 69 63 20 64 69  ts a specific di
3254a 72 65 63 74 6f 72 79 20 74 6f 20 62 65 20 75 73  rectory to be us
3254b 65 64 20 66 6f 72 20 64 61 74 61 62 61 73 65 20  ed for database 
3254c 66 69 6c 65 73 20 74 68 61 74 0a 20 20 2a 2a 20  files that.  ** 
3254d 77 65 72 65 20 73 70 65 63 69 66 69 65 64 20 77  were specified w
3254e 69 74 68 20 61 20 72 65 6c 61 74 69 76 65 20 70  ith a relative p
3254f 61 74 68 6e 61 6d 65 2e 20 20 53 65 74 74 69 6e  athname.  Settin
32550 67 20 74 6f 20 61 20 6e 75 6c 6c 20 73 74 72 69  g to a null stri
32551 6e 67 20 72 65 76 65 72 74 73 0a 20 20 2a 2a 20  ng reverts.  ** 
32552 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 64  to the default d
32553 61 74 61 62 61 73 65 20 64 69 72 65 63 74 6f 72  atabase director
32554 79 2c 20 77 68 69 63 68 20 66 6f 72 20 64 61 74  y, which for dat
32555 61 62 61 73 65 20 66 69 6c 65 73 20 73 70 65 63  abase files spec
32556 69 66 69 65 64 20 77 69 74 68 0a 20 20 2a 2a 20  ified with.  ** 
32557 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 20  a relative path 
32558 77 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 62 65  will probably be
32559 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 63 75   based on the cu
3255a 72 72 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20  rrent directory 
3255b 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 70 72 6f  for the.  ** pro
3255c 63 65 73 73 2e 20 20 44 61 74 61 62 61 73 65 20  cess.  Database 
3255d 66 69 6c 65 20 73 70 65 63 69 66 69 65 64 20 77  file specified w
3255e 69 74 68 20 61 6e 20 61 62 73 6f 6c 75 74 65 20  ith an absolute 
3255f 70 61 74 68 20 61 72 65 20 6e 6f 74 20 69 6d 70  path are not imp
32560 61 63 74 65 64 0a 20 20 2a 2a 20 62 79 20 74 68  acted.  ** by th
32561 69 73 20 73 65 74 74 69 6e 67 2c 20 72 65 67 61  is setting, rega
32562 72 64 6c 65 73 73 20 6f 66 20 69 74 73 20 76 61  rdless of its va
32563 6c 75 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2f 0a 20  lue..  **.  */. 
32564 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
32565 43 6d 70 28 7a 4c 65 66 74 2c 20 22 64 61 74 61  Cmp(zLeft, "data
32566 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79  _store_directory
32567 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  ")==0 ){.    if(
32568 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20   !zRight ){.    
32569 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 61    if( sqlite3_da
3256a 74 61 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a  ta_directory ){.
3256b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
3256c 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
3256d 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   1);.        sql
3256e 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
3256f 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
32570 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 20 20  _NAME, .        
32571 20 20 20 20 22 64 61 74 61 5f 73 74 6f 72 65 5f      "data_store_
32572 64 69 72 65 63 74 6f 72 79 22 2c 20 53 51 4c 49  directory", SQLI
32573 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
32574 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
32575 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
32576 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 73 71  ng8, 0, 1, 0, sq
32577 6c 69 74 65 33 5f 64 61 74 61 5f 64 69 72 65 63  lite3_data_direc
32578 74 6f 72 79 2c 20 30 29 3b 0a 20 20 20 20 20 20  tory, 0);.      
32579 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3257a 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
3257b 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20  Row, 1, 1);.    
3257c 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 23    }.    }else{.#
3257d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3257e 49 54 5f 57 53 44 0a 20 20 20 20 20 20 69 66 28  IT_WSD.      if(
3257f 20 7a 52 69 67 68 74 5b 30 5d 20 29 7b 0a 20 20   zRight[0] ){.  
32580 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
32581 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
32582 74 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d 3e  te3OsAccess(db->
32583 70 56 66 73 2c 20 7a 52 69 67 68 74 2c 20 53 51  pVfs, zRight, SQ
32584 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
32585 57 52 49 54 45 2c 20 26 72 65 73 29 3b 0a 20 20  WRITE, &res);.  
32586 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
32587 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 65 73 3d 3d  LITE_OK || res==
32588 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  0 ){.          s
32589 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
3258a 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 20 77 72  Parse, "not a wr
3258b 69 74 61 62 6c 65 20 64 69 72 65 63 74 6f 72 79  itable directory
3258c 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  ");.          go
3258d 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
3258e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
3258f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
32590 72 65 65 28 73 71 6c 69 74 65 33 5f 64 61 74 61  ree(sqlite3_data
32591 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 20  _directory);.   
32592 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b 30 5d     if( zRight[0]
32593 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
32594 74 65 33 5f 64 61 74 61 5f 64 69 72 65 63 74 6f  te3_data_directo
32595 72 79 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ry = sqlite3_mpr
32596 69 6e 74 66 28 22 25 73 22 2c 20 7a 52 69 67 68  intf("%s", zRigh
32597 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  t);.      }else{
32598 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
32599 5f 64 61 74 61 5f 64 69 72 65 63 74 6f 72 79 20  _data_directory 
3259a 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  = 0;.      }.#en
3259b 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
3259c 49 54 5f 57 53 44 20 2a 2f 0a 20 20 20 20 7d 0a  IT_WSD */.    }.
3259d 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a    }else.#endif..
3259e 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
3259f 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
325a0 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 69 66 20  NG_STYLE).#  if 
325a1 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
325a2 5f 29 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53  _).#    define S
325a3 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
325a4 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a 23 20 20  KING_STYLE 1.#  
325a5 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65  else.#    define
325a6 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
325a7 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30 0a 23  OCKING_STYLE 0.#
325a8 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23    endif.#endif.#
325a9 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  if SQLITE_ENABLE
325aa 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20  _LOCKING_STYLE. 
325ab 20 2f 2a 0a 20 20 20 2a 2a 20 20 20 50 52 41 47   /*.   **   PRAG
325ac 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f  MA [database.]lo
325ad 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 20 20  ck_proxy_file.  
325ae 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61   **   PRAGMA [da
325af 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f  tabase.]lock_pro
325b0 78 79 5f 66 69 6c 65 20 3d 20 22 3a 61 75 74 6f  xy_file = ":auto
325b1 3a 22 7c 22 6c 6f 63 6b 5f 66 69 6c 65 5f 70 61  :"|"lock_file_pa
325b2 74 68 22 0a 20 20 20 2a 2a 0a 20 20 20 2a 2a 20  th".   **.   ** 
325b3 52 65 74 75 72 6e 20 6f 72 20 73 65 74 20 74 68  Return or set th
325b4 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6c  e value of the l
325b5 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 20 66  ock_proxy_file f
325b6 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20  lag.  Changing. 
325b7 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 73    ** the value s
325b8 65 74 73 20 61 20 73 70 65 63 69 66 69 63 20 66  ets a specific f
325b9 69 6c 65 20 74 6f 20 62 65 20 75 73 65 64 20 66  ile to be used f
325ba 6f 72 20 64 61 74 61 62 61 73 65 20 61 63 63 65  or database acce
325bb 73 73 20 6c 6f 63 6b 73 2e 0a 20 20 20 2a 2a 0a  ss locks..   **.
325bc 20 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69     */.  if( sqli
325bd 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
325be 2c 20 22 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69  , "lock_proxy_fi
325bf 6c 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  le")==0 ){.    i
325c0 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20  f( !zRight ){.  
325c1 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65      Pager *pPage
325c2 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  r = sqlite3Btree
325c3 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29 3b  Pager(pDb->pBt);
325c4 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 72 6f  .      char *pro
325c5 78 79 5f 66 69 6c 65 5f 70 61 74 68 20 3d 20 4e  xy_file_path = N
325c6 55 4c 4c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ULL;.      sqlit
325c7 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 20 3d  e3_file *pFile =
325c8 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c   sqlite3PagerFil
325c9 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  e(pPager);.     
325ca 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f   sqlite3OsFileCo
325cb 6e 74 72 6f 6c 48 69 6e 74 28 70 46 69 6c 65 2c  ntrolHint(pFile,
325cc 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b   SQLITE_GET_LOCK
325cd 50 52 4f 58 59 46 49 4c 45 2c 20 0a 20 20 20 20  PROXYFILE, .    
325ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
325cf 20 20 20 20 20 20 20 26 70 72 6f 78 79 5f 66 69         &proxy_fi
325d0 6c 65 5f 70 61 74 68 29 3b 0a 20 20 20 20 20 20  le_path);.      
325d1 0a 20 20 20 20 20 20 69 66 28 20 70 72 6f 78 79  .      if( proxy
325d2 5f 66 69 6c 65 5f 70 61 74 68 20 29 7b 0a 20 20  _file_path ){.  
325d3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
325d4 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
325d5 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
325d6 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
325d7 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
325d8 41 4d 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20  AME, .          
325d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
325da 20 20 20 20 22 6c 6f 63 6b 5f 70 72 6f 78 79 5f      "lock_proxy_
325db 66 69 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  file", SQLITE_ST
325dc 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
325dd 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
325de 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
325df 30 2c 20 31 2c 20 30 2c 20 70 72 6f 78 79 5f 66  0, 1, 0, proxy_f
325e0 69 6c 65 5f 70 61 74 68 2c 20 30 29 3b 0a 20 20  ile_path, 0);.  
325e1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
325e2 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
325e3 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a  sultRow, 1, 1);.
325e4 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
325e5 65 7b 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a  e{.      Pager *
325e6 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
325e7 42 74 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e  BtreePager(pDb->
325e8 70 42 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  pBt);.      sqli
325e9 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 20  te3_file *pFile 
325ea 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69  = sqlite3PagerFi
325eb 6c 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  le(pPager);.    
325ec 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20    int res;.     
325ed 20 69 66 28 20 7a 52 69 67 68 74 5b 30 5d 20 29   if( zRight[0] )
325ee 7b 0a 20 20 20 20 20 20 20 20 72 65 73 3d 73 71  {.        res=sq
325ef 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
325f0 6f 6c 28 70 46 69 6c 65 2c 20 53 51 4c 49 54 45  ol(pFile, SQLITE
325f1 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _SET_LOCKPROXYFI
325f2 4c 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  LE, .           
325f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
325f4 20 20 20 20 20 20 20 20 20 20 7a 52 69 67 68 74            zRight
325f5 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20  );.      } else 
325f6 7b 0a 20 20 20 20 20 20 20 20 72 65 73 3d 73 71  {.        res=sq
325f7 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
325f8 6f 6c 28 70 46 69 6c 65 2c 20 53 51 4c 49 54 45  ol(pFile, SQLITE
325f9 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _SET_LOCKPROXYFI
325fa 4c 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  LE, .           
325fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
325fc 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 29 3b            NULL);
325fd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
325fe 66 28 20 72 65 73 21 3d 53 51 4c 49 54 45 5f 4f  f( res!=SQLITE_O
325ff 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  K ){.        sql
32600 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
32601 72 73 65 2c 20 22 66 61 69 6c 65 64 20 74 6f 20  rse, "failed to 
32602 73 65 74 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66  set lock proxy f
32603 69 6c 65 22 29 3b 0a 20 20 20 20 20 20 20 20 67  ile");.        g
32604 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
32605 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
32606 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
32607 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
32608 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 20 20  CKING_STYLE */  
32609 20 20 20 20 0a 20 20 20 20 0a 20 20 2f 2a 0a 20      .    .  /*. 
3260a 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61   **   PRAGMA [da
3260b 74 61 62 61 73 65 2e 5d 73 79 6e 63 68 72 6f 6e  tabase.]synchron
3260c 6f 75 73 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d  ous.  **   PRAGM
3260d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 79 6e  A [database.]syn
3260e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 7c 4f 4e 7c  chronous=OFF|ON|
3260f 4e 4f 52 4d 41 4c 7c 46 55 4c 4c 0a 20 20 2a 2a  NORMAL|FULL.  **
32610 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20  .  ** Return or 
32611 73 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76 61  set the local va
32612 6c 75 65 20 6f 66 20 74 68 65 20 73 79 6e 63 68  lue of the synch
32613 72 6f 6e 6f 75 73 20 66 6c 61 67 2e 20 20 43 68  ronous flag.  Ch
32614 61 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20  anging.  ** the 
32615 6c 6f 63 61 6c 20 76 61 6c 75 65 20 64 6f 65 73  local value does
32616 20 6e 6f 74 20 6d 61 6b 65 20 63 68 61 6e 67 65   not make change
32617 73 20 74 6f 20 74 68 65 20 64 69 73 6b 20 66 69  s to the disk fi
32618 6c 65 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20  le and the.  ** 
32619 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 77 69  default value wi
3261a 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 74  ll be restored t
3261b 68 65 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65  he next time the
3261c 20 64 61 74 61 62 61 73 65 20 69 73 0a 20 20 2a   database is.  *
3261d 2a 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20  * opened..  */. 
3261e 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
3261f 43 6d 70 28 7a 4c 65 66 74 2c 22 73 79 6e 63 68  Cmp(zLeft,"synch
32620 72 6f 6e 6f 75 73 22 29 3d 3d 30 20 29 7b 0a 20  ronous")==0 ){. 
32621 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
32622 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
32623 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
32624 75 74 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69  ut;.    if( !zRi
32625 67 68 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ght ){.      ret
32626 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61  urnSingleInt(pPa
32627 72 73 65 2c 20 22 73 79 6e 63 68 72 6f 6e 6f 75  rse, "synchronou
32628 73 22 2c 20 70 44 62 2d 3e 73 61 66 65 74 79 5f  s", pDb->safety_
32629 6c 65 76 65 6c 2d 31 29 3b 0a 20 20 20 20 7d 65  level-1);.    }e
3262a 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21  lse{.      if( !
3262b 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
3262c 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
3262d 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
3262e 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22  , .            "
3262f 53 61 66 65 74 79 20 6c 65 76 65 6c 20 6d 61 79  Safety level may
32630 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20   not be changed 
32631 69 6e 73 69 64 65 20 61 20 74 72 61 6e 73 61 63  inside a transac
32632 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 7d 65  tion");.      }e
32633 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 44 62  lse{.        pDb
32634 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d  ->safety_level =
32635 20 67 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28   getSafetyLevel(
32636 7a 52 69 67 68 74 2c 30 2c 31 29 2b 31 3b 0a 20  zRight,0,1)+1;. 
32637 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
32638 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53  else.#endif /* S
32639 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
3263a 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66  _PRAGMAS */..#if
3263b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
3263c 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 0a 20 20  _FLAG_PRAGMAS.  
3263d 69 66 28 20 66 6c 61 67 50 72 61 67 6d 61 28 70  if( flagPragma(p
3263e 50 61 72 73 65 2c 20 7a 4c 65 66 74 2c 20 7a 52  Parse, zLeft, zR
3263f 69 67 68 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20  ight) ){.    /* 
32640 54 68 65 20 66 6c 61 67 50 72 61 67 6d 61 28 29  The flagPragma()
32641 20 73 75 62 72 6f 75 74 69 6e 65 20 61 6c 73 6f   subroutine also
32642 20 67 65 6e 65 72 61 74 65 73 20 61 6e 79 20 6e   generates any n
32643 65 63 65 73 73 61 72 79 20 63 6f 64 65 0a 20 20  ecessary code.  
32644 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f    ** there is no
32645 74 68 69 6e 67 20 6d 6f 72 65 20 74 6f 20 64 6f  thing more to do
32646 20 68 65 72 65 20 2a 2f 0a 20 20 7d 65 6c 73 65   here */.  }else
32647 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
32648 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47  E_OMIT_FLAG_PRAG
32649 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  MAS */..#ifndef 
3264a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45  SQLITE_OMIT_SCHE
3264b 4d 41 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a  MA_PRAGMAS.  /*.
3264c 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 61    **   PRAGMA ta
3264d 62 6c 65 5f 69 6e 66 6f 28 3c 74 61 62 6c 65 3e  ble_info(<table>
3264e 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75  ).  **.  ** Retu
3264f 72 6e 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  rn a single row 
32650 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  for each column 
32651 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 74 61 62  of the named tab
32652 6c 65 2e 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20  le. The columns 
32653 6f 66 0a 20 20 2a 2a 20 74 68 65 20 72 65 74 75  of.  ** the retu
32654 72 6e 65 64 20 64 61 74 61 20 73 65 74 20 61 72  rned data set ar
32655 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 63 69 64  e:.  **.  ** cid
32656 3a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  :        Column 
32657 69 64 20 28 6e 75 6d 62 65 72 65 64 20 66 72 6f  id (numbered fro
32658 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2c  m left to right,
32659 20 73 74 61 72 74 69 6e 67 20 61 74 20 30 29 0a   starting at 0).
3265a 20 20 2a 2a 20 6e 61 6d 65 3a 20 20 20 20 20 20    ** name:      
3265b 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 0a 20 20 2a   Column name.  *
3265c 2a 20 74 79 70 65 3a 20 20 20 20 20 20 20 43 6f  * type:       Co
3265d 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e  lumn declaration
3265e 20 74 79 70 65 2e 0a 20 20 2a 2a 20 6e 6f 74 6e   type..  ** notn
3265f 75 6c 6c 3a 20 20 20 20 54 72 75 65 20 69 66 20  ull:    True if 
32660 27 4e 4f 54 20 4e 55 4c 4c 27 20 69 73 20 70 61  'NOT NULL' is pa
32661 72 74 20 6f 66 20 63 6f 6c 75 6d 6e 20 64 65 63  rt of column dec
32662 6c 61 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 66  laration.  ** df
32663 6c 74 5f 76 61 6c 75 65 3a 20 54 68 65 20 64 65  lt_value: The de
32664 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20  fault value for 
32665 74 68 65 20 63 6f 6c 75 6d 6e 2c 20 69 66 20 61  the column, if a
32666 6e 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ny..  */.  if( s
32667 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
32668 65 66 74 2c 20 22 74 61 62 6c 65 5f 69 6e 66 6f  eft, "table_info
32669 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20  ")==0 && zRight 
3266a 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ){.    Table *pT
3266b 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ab;.    if( sqli
3266c 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
3266d 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
3266e 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61  gma_out;.    pTa
3266f 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  b = sqlite3FindT
32670 61 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c  able(db, zRight,
32671 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70   zDb);.    if( p
32672 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  Tab ){.      int
32673 20 69 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 48   i;.      int nH
32674 69 64 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20  idden = 0;.     
32675 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20   Column *pCol;. 
32676 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
32677 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 36 29  SetNumCols(v, 6)
32678 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
32679 6e 4d 65 6d 20 3d 20 36 3b 0a 20 20 20 20 20 20  nMem = 6;.      
3267a 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
3267b 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
3267c 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 69 64 22 2c  AME_NAME, "cid",
3267d 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
3267e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
3267f 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
32680 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
32681 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f   "name", SQLITE_
32682 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73  STATIC);.      s
32683 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
32684 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41  Name(v, 2, COLNA
32685 4d 45 5f 4e 41 4d 45 2c 20 22 74 79 70 65 22 2c  ME_NAME, "type",
32686 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
32687 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
32688 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
32689 33 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  3, COLNAME_NAME,
3268a 20 22 6e 6f 74 6e 75 6c 6c 22 2c 20 53 51 4c 49   "notnull", SQLI
3268b 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
3268c 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
3268d 43 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c 20 43 4f  ColName(v, 4, CO
3268e 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 64 66 6c  LNAME_NAME, "dfl
3268f 74 5f 76 61 6c 75 65 22 2c 20 53 51 4c 49 54 45  t_value", SQLITE
32690 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
32691 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
32692 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c 4e  lName(v, 5, COLN
32693 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 6b 22 2c 20  AME_NAME, "pk", 
32694 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
32695 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 69 65        sqlite3Vie
32696 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
32697 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20  pParse, pTab);. 
32698 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 43       for(i=0, pC
32699 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 69  ol=pTab->aCol; i
3269a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  <pTab->nCol; i++
3269b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  , pCol++){.     
3269c 20 20 20 69 66 28 20 49 73 48 69 64 64 65 6e 43     if( IsHiddenC
3269d 6f 6c 75 6d 6e 28 70 43 6f 6c 29 20 29 7b 0a 20  olumn(pCol) ){. 
3269e 20 20 20 20 20 20 20 20 20 6e 48 69 64 64 65 6e           nHidden
3269f 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f  ++;.          co
326a0 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
326a1 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
326a2 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
326a3 50 5f 49 6e 74 65 67 65 72 2c 20 69 2d 6e 48 69  P_Integer, i-nHi
326a4 64 64 65 6e 2c 20 31 29 3b 0a 20 20 20 20 20 20  dden, 1);.      
326a5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
326a6 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
326a7 38 2c 20 30 2c 20 32 2c 20 30 2c 20 70 43 6f 6c  8, 0, 2, 0, pCol
326a8 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
326a9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
326aa 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
326ab 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20  ing8, 0, 3, 0,. 
326ac 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
326ad 7a 54 79 70 65 20 3f 20 70 43 6f 6c 2d 3e 7a 54  zType ? pCol->zT
326ae 79 70 65 20 3a 20 22 22 2c 20 30 29 3b 0a 20 20  ype : "", 0);.  
326af 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
326b0 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
326b1 74 65 67 65 72 2c 20 28 70 43 6f 6c 2d 3e 6e 6f  teger, (pCol->no
326b2 74 4e 75 6c 6c 20 3f 20 31 20 3a 20 30 29 2c 20  tNull ? 1 : 0), 
326b3 34 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  4);.        if( 
326b4 70 43 6f 6c 2d 3e 7a 44 66 6c 74 20 29 7b 0a 20  pCol->zDflt ){. 
326b5 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
326b6 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
326b7 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 35 2c 20  _String8, 0, 5, 
326b8 30 2c 20 28 63 68 61 72 2a 29 70 43 6f 6c 2d 3e  0, (char*)pCol->
326b9 7a 44 66 6c 74 2c 20 30 29 3b 0a 20 20 20 20 20  zDflt, 0);.     
326ba 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
326bb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
326bc 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
326bd 2c 20 30 2c 20 35 29 3b 0a 20 20 20 20 20 20 20  , 0, 5);.       
326be 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
326bf 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
326c0 4f 50 5f 49 6e 74 65 67 65 72 2c 0a 20 20 20 20  OP_Integer,.    
326c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
326c2 20 20 20 20 20 20 20 20 28 70 43 6f 6c 2d 3e 63          (pCol->c
326c3 6f 6c 46 6c 61 67 73 26 43 4f 4c 46 4c 41 47 5f  olFlags&COLFLAG_
326c4 50 52 49 4d 4b 45 59 29 21 3d 30 2c 20 36 29 3b  PRIMKEY)!=0, 6);
326c5 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
326c6 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
326c7 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 36  _ResultRow, 1, 6
326c8 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
326c9 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20  .  }else..  if( 
326ca 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
326cb 4c 65 66 74 2c 20 22 69 6e 64 65 78 5f 69 6e 66  Left, "index_inf
326cc 6f 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74  o")==0 && zRight
326cd 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70   ){.    Index *p
326ce 49 64 78 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a  Idx;.    Table *
326cf 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71  pTab;.    if( sq
326d0 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
326d1 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70  pParse) ) goto p
326d2 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70  ragma_out;.    p
326d3 49 64 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Idx = sqlite3Fin
326d4 64 49 6e 64 65 78 28 64 62 2c 20 7a 52 69 67 68  dIndex(db, zRigh
326d5 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28  t, zDb);.    if(
326d6 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 69   pIdx ){.      i
326d7 6e 74 20 69 3b 0a 20 20 20 20 20 20 70 54 61 62  nt i;.      pTab
326d8 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b   = pIdx->pTable;
326d9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
326da 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
326db 33 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  3);.      pParse
326dc 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20  ->nMem = 3;.    
326dd 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
326de 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
326df 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71  LNAME_NAME, "seq
326e0 6e 6f 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  no", SQLITE_STAT
326e1 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
326e2 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
326e3 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 1, COLNAME_N
326e4 41 4d 45 2c 20 22 63 69 64 22 2c 20 53 51 4c 49  AME, "cid", SQLI
326e5 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
326e6 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
326e7 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f  ColName(v, 2, CO
326e8 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d  LNAME_NAME, "nam
326e9 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
326ea 43 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  C);.      for(i=
326eb 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75  0; i<pIdx->nColu
326ec 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
326ed 20 20 69 6e 74 20 63 6e 75 6d 20 3d 20 70 49 64    int cnum = pId
326ee 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a  x->aiColumn[i];.
326ef 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
326f0 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
326f1 49 6e 74 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a  Integer, i, 1);.
326f2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
326f3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
326f4 49 6e 74 65 67 65 72 2c 20 63 6e 75 6d 2c 20 32  Integer, cnum, 2
326f5 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
326f6 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 63 6e  t( pTab->nCol>cn
326f7 75 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  um );.        sq
326f8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
326f9 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
326fa 2c 20 33 2c 20 30 2c 20 70 54 61 62 2d 3e 61 43  , 3, 0, pTab->aC
326fb 6f 6c 5b 63 6e 75 6d 5d 2e 7a 4e 61 6d 65 2c 20  ol[cnum].zName, 
326fc 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
326fd 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
326fe 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31   OP_ResultRow, 1
326ff 2c 20 33 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 3);.      }.  
32700 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69    }.  }else..  i
32701 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
32702 70 28 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78 5f  p(zLeft, "index_
32703 6c 69 73 74 22 29 3d 3d 30 20 26 26 20 7a 52 69  list")==0 && zRi
32704 67 68 74 20 29 7b 0a 20 20 20 20 49 6e 64 65 78  ght ){.    Index
32705 20 2a 70 49 64 78 3b 0a 20 20 20 20 54 61 62 6c   *pIdx;.    Tabl
32706 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28  e *pTab;.    if(
32707 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
32708 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
32709 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
3270a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
3270b 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52  FindTable(db, zR
3270c 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20  ight, zDb);.    
3270d 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
3270e 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
3270f 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
32710 20 20 20 20 70 49 64 78 20 3d 20 70 54 61 62 2d      pIdx = pTab-
32711 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 69  >pIndex;.      i
32712 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20  f( pIdx ){.     
32713 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20     int i = 0; . 
32714 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
32715 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
32716 33 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72  3);.        pPar
32717 73 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20  se->nMem = 3;.  
32718 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
32719 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
3271a 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
3271b 22 73 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54  "seq", SQLITE_ST
3271c 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
3271d 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
3271e 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41  Name(v, 1, COLNA
3271f 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c  ME_NAME, "name",
32720 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
32721 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
32722 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
32723 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 2, COLNAME_NAM
32724 45 2c 20 22 75 6e 69 71 75 65 22 2c 20 53 51 4c  E, "unique", SQL
32725 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
32726 20 20 20 20 20 77 68 69 6c 65 28 70 49 64 78 29       while(pIdx)
32727 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
32728 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
32729 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20   OP_Integer, i, 
3272a 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  1);.          sq
3272b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
3272c 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
3272d 2c 20 32 2c 20 30 2c 20 70 49 64 78 2d 3e 7a 4e  , 2, 0, pIdx->zN
3272e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ame, 0);.       
3272f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
32730 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
32731 65 72 2c 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f  er, pIdx->onErro
32732 72 21 3d 4f 45 5f 4e 6f 6e 65 2c 20 33 29 3b 0a  r!=OE_None, 3);.
32733 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
32734 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
32735 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
32736 33 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2b 2b  3);.          ++
32737 69 3b 0a 20 20 20 20 20 20 20 20 20 20 70 49 64  i;.          pId
32738 78 20 3d 20 70 49 64 78 2d 3e 70 4e 65 78 74 3b  x = pIdx->pNext;
32739 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3273a 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
3273b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
3273c 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 64  trICmp(zLeft, "d
3273d 61 74 61 62 61 73 65 5f 6c 69 73 74 22 29 3d 3d  atabase_list")==
3273e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  0 ){.    int i;.
3273f 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
32740 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
32741 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f  ) ) goto pragma_
32742 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  out;.    sqlite3
32743 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
32744 2c 20 33 29 3b 0a 20 20 20 20 70 50 61 72 73 65  , 3);.    pParse
32745 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20  ->nMem = 3;.    
32746 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
32747 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
32748 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c  AME_NAME, "seq",
32749 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
3274a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
3274b 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c  SetColName(v, 1,
3274c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
3274d 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  name", SQLITE_ST
3274e 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74  ATIC);.    sqlit
3274f 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
32750 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 2, COLNAME_N
32751 41 4d 45 2c 20 22 66 69 6c 65 22 2c 20 53 51 4c  AME, "file", SQL
32752 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
32753 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
32754 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
32755 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e   if( db->aDb[i].
32756 70 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  pBt==0 ) continu
32757 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e;.      assert(
32758 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d   db->aDb[i].zNam
32759 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  e!=0 );.      sq
3275a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
3275b 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69  v, OP_Integer, i
3275c 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
3275d 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
3275e 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
3275f 32 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69 5d  2, 0, db->aDb[i]
32760 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  .zName, 0);.    
32761 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
32762 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
32763 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20  8, 0, 3, 0,.    
32764 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
32765 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64  reeGetFilename(d
32766 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 2c 20  b->aDb[i].pBt), 
32767 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
32768 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
32769 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
3276a 33 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  3);.    }.  }els
3276b 65 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  e..  if( sqlite3
3276c 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
3276d 63 6f 6c 6c 61 74 69 6f 6e 5f 6c 69 73 74 22 29  collation_list")
3276e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ==0 ){.    int i
3276f 20 3d 20 30 3b 0a 20 20 20 20 48 61 73 68 45 6c   = 0;.    HashEl
32770 65 6d 20 2a 70 3b 0a 20 20 20 20 73 71 6c 69 74  em *p;.    sqlit
32771 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
32772 28 76 2c 20 32 29 3b 0a 20 20 20 20 70 50 61 72  (v, 2);.    pPar
32773 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 20 20  se->nMem = 2;.  
32774 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
32775 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
32776 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71  LNAME_NAME, "seq
32777 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
32778 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
32779 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
3277a 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
3277b 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f   "name", SQLITE_
3277c 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66 6f 72  STATIC);.    for
3277d 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (p=sqliteHashFir
3277e 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71  st(&db->aCollSeq
3277f 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61  ); p; p=sqliteHa
32780 73 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20 20  shNext(p)){.    
32781 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
32782 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73 71   = (CollSeq *)sq
32783 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 29 3b  liteHashData(p);
32784 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
32785 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
32786 6e 74 65 67 65 72 2c 20 69 2b 2b 2c 20 31 29 3b  nteger, i++, 1);
32787 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
32788 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
32789 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c  tring8, 0, 2, 0,
3278a 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 30   pColl->zName, 0
3278b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
3278c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
3278d 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 32  _ResultRow, 1, 2
3278e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
3278f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
32790 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50 52  E_OMIT_SCHEMA_PR
32791 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65  AGMAS */..#ifnde
32792 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
32793 52 45 49 47 4e 5f 4b 45 59 0a 20 20 69 66 28 20  REIGN_KEY.  if( 
32794 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
32795 4c 65 66 74 2c 20 22 66 6f 72 65 69 67 6e 5f 6b  Left, "foreign_k
32796 65 79 5f 6c 69 73 74 22 29 3d 3d 30 20 26 26 20  ey_list")==0 && 
32797 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 46 4b  zRight ){.    FK
32798 65 79 20 2a 70 46 4b 3b 0a 20 20 20 20 54 61 62  ey *pFK;.    Tab
32799 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66  le *pTab;.    if
3279a 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
3279b 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f  ema(pParse) ) go
3279c 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
3279d 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
3279e 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
3279f 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20  Right, zDb);.   
327a0 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20   if( pTab ){.   
327a1 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65     v = sqlite3Ge
327a2 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
327a3 20 20 20 20 20 70 46 4b 20 3d 20 70 54 61 62 2d       pFK = pTab-
327a4 3e 70 46 4b 65 79 3b 0a 20 20 20 20 20 20 69 66  >pFKey;.      if
327a5 28 20 70 46 4b 20 29 7b 0a 20 20 20 20 20 20 20  ( pFK ){.       
327a6 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20 20 20   int i = 0; .   
327a7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
327a8 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 38 29  SetNumCols(v, 8)
327a9 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  ;.        pParse
327aa 2d 3e 6e 4d 65 6d 20 3d 20 38 3b 0a 20 20 20 20  ->nMem = 8;.    
327ab 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
327ac 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
327ad 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 69  COLNAME_NAME, "i
327ae 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  d", SQLITE_STATI
327af 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  C);.        sqli
327b0 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
327b1 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 1, COLNAME_
327b2 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c  NAME, "seq", SQL
327b3 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
327b4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
327b5 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c  SetColName(v, 2,
327b6 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
327b7 74 61 62 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53  table", SQLITE_S
327b8 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
327b9 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
327ba 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c 4e  lName(v, 3, COLN
327bb 41 4d 45 5f 4e 41 4d 45 2c 20 22 66 72 6f 6d 22  AME_NAME, "from"
327bc 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
327bd 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
327be 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
327bf 76 2c 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 4, COLNAME_NA
327c0 4d 45 2c 20 22 74 6f 22 2c 20 53 51 4c 49 54 45  ME, "to", SQLITE
327c1 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
327c2 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
327c3 43 6f 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f  ColName(v, 5, CO
327c4 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f  LNAME_NAME, "on_
327c5 75 70 64 61 74 65 22 2c 20 53 51 4c 49 54 45 5f  update", SQLITE_
327c6 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
327c7 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
327c8 6f 6c 4e 61 6d 65 28 76 2c 20 36 2c 20 43 4f 4c  olName(v, 6, COL
327c9 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 64  NAME_NAME, "on_d
327ca 65 6c 65 74 65 22 2c 20 53 51 4c 49 54 45 5f 53  elete", SQLITE_S
327cb 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
327cc 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
327cd 6c 4e 61 6d 65 28 76 2c 20 37 2c 20 43 4f 4c 4e  lName(v, 7, COLN
327ce 41 4d 45 5f 4e 41 4d 45 2c 20 22 6d 61 74 63 68  AME_NAME, "match
327cf 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
327d0 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  );.        while
327d1 28 70 46 4b 29 7b 0a 20 20 20 20 20 20 20 20 20  (pFK){.         
327d2 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20   int j;.        
327d3 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 4b    for(j=0; j<pFK
327d4 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  ->nCol; j++){.  
327d5 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
327d6 7a 43 6f 6c 20 3d 20 70 46 4b 2d 3e 61 43 6f 6c  zCol = pFK->aCol
327d7 5b 6a 5d 2e 7a 43 6f 6c 3b 0a 20 20 20 20 20 20  [j].zCol;.      
327d8 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4f 6e 44        char *zOnD
327d9 65 6c 65 74 65 20 3d 20 28 63 68 61 72 20 2a 29  elete = (char *)
327da 61 63 74 69 6f 6e 4e 61 6d 65 28 70 46 4b 2d 3e  actionName(pFK->
327db 61 41 63 74 69 6f 6e 5b 30 5d 29 3b 0a 20 20 20  aAction[0]);.   
327dc 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
327dd 4f 6e 55 70 64 61 74 65 20 3d 20 28 63 68 61 72  OnUpdate = (char
327de 20 2a 29 61 63 74 69 6f 6e 4e 61 6d 65 28 70 46   *)actionName(pF
327df 4b 2d 3e 61 41 63 74 69 6f 6e 5b 31 5d 29 3b 0a  K->aAction[1]);.
327e0 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
327e1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
327e2 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20   OP_Integer, i, 
327e3 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  1);.            
327e4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
327e5 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
327e6 20 6a 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20   j, 2);.        
327e7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
327e8 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
327e9 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 20 70 46  ng8, 0, 3, 0, pF
327ea 4b 2d 3e 7a 54 6f 2c 20 30 29 3b 0a 20 20 20 20  K->zTo, 0);.    
327eb 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
327ec 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
327ed 53 74 72 69 6e 67 38 2c 20 30 2c 20 34 2c 20 30  String8, 0, 4, 0
327ee 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
327ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
327f0 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 46 4b 2d 3e  pTab->aCol[pFK->
327f1 61 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 5d 2e 7a  aCol[j].iFrom].z
327f2 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Name, 0);.      
327f3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
327f4 65 41 64 64 4f 70 34 28 76 2c 20 7a 43 6f 6c 20  eAddOp4(v, zCol 
327f5 3f 20 4f 50 5f 53 74 72 69 6e 67 38 20 3a 20 4f  ? OP_String8 : O
327f6 50 5f 4e 75 6c 6c 2c 20 30 2c 20 35 2c 20 30 2c  P_Null, 0, 5, 0,
327f7 20 7a 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20 20   zCol, 0);.     
327f8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
327f9 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
327fa 74 72 69 6e 67 38 2c 20 30 2c 20 36 2c 20 30 2c  tring8, 0, 6, 0,
327fb 20 7a 4f 6e 55 70 64 61 74 65 2c 20 30 29 3b 0a   zOnUpdate, 0);.
327fc 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
327fd 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
327fe 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
327ff 37 2c 20 30 2c 20 7a 4f 6e 44 65 6c 65 74 65 2c  7, 0, zOnDelete,
32800 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
32801 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
32802 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
32803 2c 20 30 2c 20 38 2c 20 30 2c 20 22 4e 4f 4e 45  , 0, 8, 0, "NONE
32804 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  ", 0);.         
32805 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
32806 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
32807 74 52 6f 77 2c 20 31 2c 20 38 29 3b 0a 20 20 20  tRow, 1, 8);.   
32808 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
32809 20 20 20 2b 2b 69 3b 0a 20 20 20 20 20 20 20 20     ++i;.        
3280a 20 20 70 46 4b 20 3d 20 70 46 4b 2d 3e 70 4e 65    pFK = pFK->pNe
3280b 78 74 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20 20  xtFrom;.        
3280c 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
3280d 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
3280e 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
3280f 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
32810 45 59 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  EY) */..#ifndef 
32811 4e 44 45 42 55 47 0a 20 20 69 66 28 20 73 71 6c  NDEBUG.  if( sql
32812 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
32813 74 2c 20 22 70 61 72 73 65 72 5f 74 72 61 63 65  t, "parser_trace
32814 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  ")==0 ){.    if(
32815 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20   zRight ){.     
32816 20 69 66 28 20 73 71 6c 69 74 65 33 47 65 74 42   if( sqlite3GetB
32817 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74 2c 20 30  oolean(zRight, 0
32818 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ) ){.        sql
32819 69 74 65 33 50 61 72 73 65 72 54 72 61 63 65 28  ite3ParserTrace(
3281a 73 74 64 65 72 72 2c 20 22 70 61 72 73 65 72 3a  stderr, "parser:
3281b 20 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65   ");.      }else
3281c 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
3281d 33 50 61 72 73 65 72 54 72 61 63 65 28 30 2c 20  3ParserTrace(0, 
3281e 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
3281f 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  }.  }else.#endif
32820 0a 0a 20 20 2f 2a 20 52 65 69 6e 73 74 61 6c 6c  ..  /* Reinstall
32821 20 74 68 65 20 4c 49 4b 45 20 61 6e 64 20 47 4c   the LIKE and GL
32822 4f 42 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54  OB functions.  T
32823 68 65 20 76 61 72 69 61 6e 74 20 6f 66 20 4c 49  he variant of LI
32824 4b 45 0a 20 20 2a 2a 20 75 73 65 64 20 77 69 6c  KE.  ** used wil
32825 6c 20 62 65 20 63 61 73 65 20 73 65 6e 73 69 74  l be case sensit
32826 69 76 65 20 6f 72 20 6e 6f 74 20 64 65 70 65 6e  ive or not depen
32827 64 69 6e 67 20 6f 6e 20 74 68 65 20 52 48 53 2e  ding on the RHS.
32828 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
32829 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
3282a 2c 20 22 63 61 73 65 5f 73 65 6e 73 69 74 69 76  , "case_sensitiv
3282b 65 5f 6c 69 6b 65 22 29 3d 3d 30 20 29 7b 0a 20  e_like")==0 ){. 
3282c 20 20 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b     if( zRight ){
3282d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
3282e 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 69  gisterLikeFuncti
3282f 6f 6e 73 28 64 62 2c 20 73 71 6c 69 74 65 33 47  ons(db, sqlite3G
32830 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74  etBoolean(zRight
32831 2c 20 30 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  , 0));.    }.  }
32832 65 6c 73 65 0a 0a 23 69 66 6e 64 65 66 20 53 51  else..#ifndef SQ
32833 4c 49 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43  LITE_INTEGRITY_C
32834 48 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 0a 23  HECK_ERROR_MAX.#
32835 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49   define SQLITE_I
32836 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45  NTEGRITY_CHECK_E
32837 52 52 4f 52 5f 4d 41 58 20 31 30 30 0a 23 65 6e  RROR_MAX 100.#en
32838 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
32839 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
3283a 54 59 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 50 72  TY_CHECK.  /* Pr
3283b 61 67 6d 61 20 22 71 75 69 63 6b 5f 63 68 65 63  agma "quick_chec
3283c 6b 22 20 69 73 20 61 6e 20 65 78 70 65 72 69 6d  k" is an experim
3283d 65 6e 74 61 6c 20 72 65 64 75 63 65 64 20 76 65  ental reduced ve
3283e 72 73 69 6f 6e 20 6f 66 20 0a 20 20 2a 2a 20 69  rsion of .  ** i
3283f 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 20 64  ntegrity_check d
32840 65 73 69 67 6e 65 64 20 74 6f 20 64 65 74 65 63  esigned to detec
32841 74 20 6d 6f 73 74 20 64 61 74 61 62 61 73 65 20  t most database 
32842 63 6f 72 72 75 70 74 69 6f 6e 0a 20 20 2a 2a 20  corruption.  ** 
32843 77 69 74 68 6f 75 74 20 6d 6f 73 74 20 6f 66 20  without most of 
32844 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f 66 20  the overhead of 
32845 61 20 66 75 6c 6c 20 69 6e 74 65 67 72 69 74 79  a full integrity
32846 2d 63 68 65 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69  -check..  */.  i
32847 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
32848 70 28 7a 4c 65 66 74 2c 20 22 69 6e 74 65 67 72  p(zLeft, "integr
32849 69 74 79 5f 63 68 65 63 6b 22 29 3d 3d 30 0a 20  ity_check")==0. 
3284a 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72 49    || sqlite3StrI
3284b 43 6d 70 28 7a 4c 65 66 74 2c 20 22 71 75 69 63  Cmp(zLeft, "quic
3284c 6b 5f 63 68 65 63 6b 22 29 3d 3d 30 20 0a 20 20  k_check")==0 .  
3284d 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c  ){.    int i, j,
3284e 20 61 64 64 72 2c 20 6d 78 45 72 72 3b 0a 0a 20   addr, mxErr;.. 
3284f 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 61 74 20     /* Code that 
32850 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 65  appears at the e
32851 6e 64 20 6f 66 20 74 68 65 20 69 6e 74 65 67 72  nd of the integr
32852 69 74 79 20 63 68 65 63 6b 2e 20 20 49 66 20 6e  ity check.  If n
32853 6f 20 65 72 72 6f 72 0a 20 20 20 20 2a 2a 20 6d  o error.    ** m
32854 65 73 73 61 67 65 73 20 68 61 76 65 20 62 65 65  essages have bee
32855 6e 20 67 65 6e 65 72 61 74 65 64 2c 20 6f 75 74  n generated, out
32856 70 75 74 20 4f 4b 2e 20 20 4f 74 68 65 72 77 69  put OK.  Otherwi
32857 73 65 20 6f 75 74 70 75 74 20 74 68 65 0a 20 20  se output the.  
32858 20 20 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61    ** error messa
32859 67 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 74  ge.    */.    st
3285a 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
3285b 70 4c 69 73 74 20 65 6e 64 43 6f 64 65 5b 5d 20  pList endCode[] 
3285c 3d 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50 5f 41  = {.      { OP_A
3285d 64 64 49 6d 6d 2c 20 20 20 20 20 20 31 2c 20 30  ddImm,      1, 0
3285e 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20  ,        0},    
3285f 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 7b 20  /* 0 */.      { 
32860 4f 50 5f 49 66 4e 65 67 2c 20 20 20 20 20 20 20  OP_IfNeg,       
32861 31 2c 20 30 2c 20 20 20 20 20 20 20 20 30 7d 2c  1, 0,        0},
32862 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20      /* 1 */.    
32863 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20    { OP_String8, 
32864 20 20 20 20 30 2c 20 33 2c 20 20 20 20 20 20 20      0, 3,       
32865 20 30 7d 2c 20 20 20 20 2f 2a 20 32 20 2a 2f 0a   0},    /* 2 */.
32866 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c        { OP_Resul
32867 74 52 6f 77 2c 20 20 20 33 2c 20 31 2c 20 20 20  tRow,   3, 1,   
32868 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a       0},.    };.
32869 0a 20 20 20 20 69 6e 74 20 69 73 51 75 69 63 6b  .    int isQuick
3286a 20 3d 20 28 73 71 6c 69 74 65 33 54 6f 6c 6f 77   = (sqlite3Tolow
3286b 65 72 28 7a 4c 65 66 74 5b 30 5d 29 3d 3d 27 71  er(zLeft[0])=='q
3286c 27 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74  ');..    /* If t
3286d 68 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e  he PRAGMA comman
3286e 64 20 77 61 73 20 6f 66 20 74 68 65 20 66 6f 72  d was of the for
3286f 6d 20 22 50 52 41 47 4d 41 20 3c 64 62 3e 2e 69  m "PRAGMA <db>.i
32870 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 22 2c  ntegrity_check",
32871 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 69 44 62  .    ** then iDb
32872 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 69   is set to the i
32873 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
32874 62 61 73 65 20 69 64 65 6e 74 69 66 69 65 64 20  base identified 
32875 62 79 20 3c 64 62 3e 2e 0a 20 20 20 20 2a 2a 20  by <db>..    ** 
32876 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 68  In this case, th
32877 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 64  e integrity of d
32878 61 74 61 62 61 73 65 20 69 44 62 20 6f 6e 6c 79  atabase iDb only
32879 20 69 73 20 76 65 72 69 66 69 65 64 20 62 79 0a   is verified by.
3287a 20 20 20 20 2a 2a 20 74 68 65 20 56 44 42 45 20      ** the VDBE 
3287b 63 72 65 61 74 65 64 20 62 65 6c 6f 77 2e 0a 20  created below.. 
3287c 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 74 68     **.    ** Oth
3287d 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 63  erwise, if the c
3287e 6f 6d 6d 61 6e 64 20 77 61 73 20 73 69 6d 70 6c  ommand was simpl
3287f 79 20 22 50 52 41 47 4d 41 20 69 6e 74 65 67 72  y "PRAGMA integr
32880 69 74 79 5f 63 68 65 63 6b 22 20 28 6f 72 0a 20  ity_check" (or. 
32881 20 20 20 2a 2a 20 22 50 52 41 47 4d 41 20 71 75     ** "PRAGMA qu
32882 69 63 6b 5f 63 68 65 63 6b 22 29 2c 20 74 68 65  ick_check"), the
32883 6e 20 69 44 62 20 69 73 20 73 65 74 20 74 6f 20  n iDb is set to 
32884 30 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  0. In this case,
32885 20 73 65 74 20 69 44 62 0a 20 20 20 20 2a 2a 20   set iDb.    ** 
32886 74 6f 20 2d 31 20 68 65 72 65 2c 20 74 6f 20 69  to -1 here, to i
32887 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65  ndicate that the
32888 20 56 44 42 45 20 73 68 6f 75 6c 64 20 76 65 72   VDBE should ver
32889 69 66 79 20 74 68 65 20 69 6e 74 65 67 72 69 74  ify the integrit
3288a 79 0a 20 20 20 20 2a 2a 20 6f 66 20 61 6c 6c 20  y.    ** of all 
3288b 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
3288c 65 73 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  es.  */.    asse
3288d 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a 20 20  rt( iDb>=0 );.  
3288e 20 20 61 73 73 65 72 74 28 20 69 44 62 3d 3d 30    assert( iDb==0
3288f 20 7c 7c 20 70 49 64 32 2d 3e 7a 20 29 3b 0a 20   || pId2->z );. 
32890 20 20 20 69 66 28 20 70 49 64 32 2d 3e 7a 3d 3d     if( pId2->z==
32891 30 20 29 20 69 44 62 20 3d 20 2d 31 3b 0a 0a 20  0 ) iDb = -1;.. 
32892 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65     /* Initialize
32893 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61   the VDBE progra
32894 6d 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c  m */.    if( sql
32895 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
32896 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
32897 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 50  agma_out;.    pP
32898 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 36 3b 0a  arse->nMem = 6;.
32899 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
3289a 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
3289b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
3289c 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
3289d 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
3289e 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 22  integrity_check"
3289f 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
328a0 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68  ;..    /* Set th
328a1 65 20 6d 61 78 69 6d 75 6d 20 65 72 72 6f 72 20  e maximum error 
328a2 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 6d 78 45  count */.    mxE
328a3 72 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  rr = SQLITE_INTE
328a4 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f  GRITY_CHECK_ERRO
328a5 52 5f 4d 41 58 3b 0a 20 20 20 20 69 66 28 20 7a  R_MAX;.    if( z
328a6 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73  Right ){.      s
328a7 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 7a  qlite3GetInt32(z
328a8 52 69 67 68 74 2c 20 26 6d 78 45 72 72 29 3b 0a  Right, &mxErr);.
328a9 20 20 20 20 20 20 69 66 28 20 6d 78 45 72 72 3c        if( mxErr<
328aa 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 78  =0 ){.        mx
328ab 45 72 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  Err = SQLITE_INT
328ac 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52  EGRITY_CHECK_ERR
328ad 4f 52 5f 4d 41 58 3b 0a 20 20 20 20 20 20 7d 0a  OR_MAX;.      }.
328ae 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
328af 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
328b0 50 5f 49 6e 74 65 67 65 72 2c 20 6d 78 45 72 72  P_Integer, mxErr
328b1 2c 20 31 29 3b 20 20 2f 2a 20 72 65 67 5b 31 5d  , 1);  /* reg[1]
328b2 20 68 6f 6c 64 73 20 65 72 72 6f 72 73 20 6c 65   holds errors le
328b3 66 74 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 44 6f  ft */..    /* Do
328b4 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68   an integrity ch
328b5 65 63 6b 20 6f 6e 20 65 61 63 68 20 64 61 74 61  eck on each data
328b6 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20  base file */.   
328b7 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
328b8 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
328b9 20 48 61 73 68 45 6c 65 6d 20 2a 78 3b 0a 20 20   HashElem *x;.  
328ba 20 20 20 20 48 61 73 68 20 2a 70 54 62 6c 73 3b      Hash *pTbls;
328bb 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74 20 3d  .      int cnt =
328bc 20 30 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 4f   0;..      if( O
328bd 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 3d  MIT_TEMPDB && i=
328be 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =1 ) continue;. 
328bf 20 20 20 20 20 69 66 28 20 69 44 62 3e 3d 30 20       if( iDb>=0 
328c0 26 26 20 69 21 3d 69 44 62 20 29 20 63 6f 6e 74  && i!=iDb ) cont
328c1 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20 73 71 6c  inue;..      sql
328c2 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63  ite3CodeVerifySc
328c3 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 29 3b  hema(pParse, i);
328c4 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71  .      addr = sq
328c5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
328c6 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29 3b  v, OP_IfPos, 1);
328c7 20 2f 2a 20 48 61 6c 74 20 69 66 20 6f 75 74 20   /* Halt if out 
328c8 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20  of errors */.   
328c9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
328ca 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c  dOp2(v, OP_Halt,
328cb 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71   0, 0);.      sq
328cc 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
328cd 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 20  e(v, addr);..   
328ce 20 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65     /* Do an inte
328cf 67 72 69 74 79 20 63 68 65 63 6b 20 6f 66 20 74  grity check of t
328d0 68 65 20 42 2d 54 72 65 65 0a 20 20 20 20 20 20  he B-Tree.      
328d1 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 42 65 67 69  **.      ** Begi
328d2 6e 20 62 79 20 66 69 6c 6c 69 6e 67 20 72 65 67  n by filling reg
328d3 69 73 74 65 72 73 20 32 2c 20 33 2c 20 2e 2e 2e  isters 2, 3, ...
328d4 20 77 69 74 68 20 74 68 65 20 72 6f 6f 74 20 70   with the root p
328d5 61 67 65 73 20 6e 75 6d 62 65 72 73 0a 20 20 20  ages numbers.   
328d6 20 20 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74 61     ** for all ta
328d7 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73  bles and indices
328d8 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
328d9 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
328da 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
328db 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28  SchemaMutexHeld(
328dc 64 62 2c 20 69 2c 20 30 29 20 29 3b 0a 20 20 20  db, i, 0) );.   
328dd 20 20 20 70 54 62 6c 73 20 3d 20 26 64 62 2d 3e     pTbls = &db->
328de 61 44 62 5b 69 5d 2e 70 53 63 68 65 6d 61 2d 3e  aDb[i].pSchema->
328df 74 62 6c 48 61 73 68 3b 0a 20 20 20 20 20 20 66  tblHash;.      f
328e0 6f 72 28 78 3d 73 71 6c 69 74 65 48 61 73 68 46  or(x=sqliteHashF
328e1 69 72 73 74 28 70 54 62 6c 73 29 3b 20 78 3b 20  irst(pTbls); x; 
328e2 78 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  x=sqliteHashNext
328e3 28 78 29 29 7b 0a 20 20 20 20 20 20 20 20 54 61  (x)){.        Ta
328e4 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69  ble *pTab = sqli
328e5 74 65 48 61 73 68 44 61 74 61 28 78 29 3b 0a 20  teHashData(x);. 
328e6 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49         Index *pI
328e7 64 78 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  dx;.        sqli
328e8 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
328e9 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 54 61   OP_Integer, pTa
328ea 62 2d 3e 74 6e 75 6d 2c 20 32 2b 63 6e 74 29 3b  b->tnum, 2+cnt);
328eb 0a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a  .        cnt++;.
328ec 20 20 20 20 20 20 20 20 66 6f 72 28 70 49 64 78          for(pIdx
328ed 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
328ee 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
328ef 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20  pNext){.        
328f0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
328f1 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
328f2 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 32  r, pIdx->tnum, 2
328f3 2b 63 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20  +cnt);.         
328f4 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20   cnt++;.        
328f5 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  }.      }..     
328f6 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 73 75   /* Make sure su
328f7 66 66 69 63 69 65 6e 74 20 6e 75 6d 62 65 72 20  fficient number 
328f8 6f 66 20 72 65 67 69 73 74 65 72 73 20 68 61 76  of registers hav
328f9 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  e been allocated
328fa 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50   */.      if( pP
328fb 61 72 73 65 2d 3e 6e 4d 65 6d 20 3c 20 63 6e 74  arse->nMem < cnt
328fc 2b 34 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50  +4 ){.        pP
328fd 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 63 6e 74  arse->nMem = cnt
328fe 2b 34 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  +4;.      }..   
328ff 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 62 2d 74     /* Do the b-t
32900 72 65 65 20 69 6e 74 65 67 72 69 74 79 20 63 68  ree integrity ch
32901 65 63 6b 73 20 2a 2f 0a 20 20 20 20 20 20 73 71  ecks */.      sq
32902 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
32903 76 2c 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43  v, OP_IntegrityC
32904 6b 2c 20 32 2c 20 63 6e 74 2c 20 31 29 3b 0a 20  k, 2, cnt, 1);. 
32905 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
32906 43 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29  ChangeP5(v, (u8)
32907 69 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d  i);.      addr =
32908 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
32909 70 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c  p1(v, OP_IsNull,
3290a 20 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   2);.      sqlit
3290b 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
3290c 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 33  OP_String8, 0, 3
3290d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 73 71  , 0,.         sq
3290e 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
3290f 20 22 2a 2a 2a 20 69 6e 20 64 61 74 61 62 61 73   "*** in databas
32910 65 20 25 73 20 2a 2a 2a 5c 6e 22 2c 20 64 62 2d  e %s ***\n", db-
32911 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 29 2c 0a  >aDb[i].zName),.
32912 20 20 20 20 20 20 20 20 20 50 34 5f 44 59 4e 41           P4_DYNA
32913 4d 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  MIC);.      sqli
32914 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
32915 20 4f 50 5f 4d 6f 76 65 2c 20 32 2c 20 34 29 3b   OP_Move, 2, 4);
32916 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
32917 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
32918 6f 6e 63 61 74 2c 20 34 2c 20 33 2c 20 32 29 3b  oncat, 4, 3, 2);
32919 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
3291a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
3291b 65 73 75 6c 74 52 6f 77 2c 20 32 2c 20 31 29 3b  esultRow, 2, 1);
3291c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
3291d 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
3291e 64 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d  dr);..      /* M
3291f 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65  ake sure all the
32920 20 69 6e 64 69 63 65 73 20 61 72 65 20 63 6f 6e   indices are con
32921 73 74 72 75 63 74 65 64 20 63 6f 72 72 65 63 74  structed correct
32922 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ly..      */.   
32923 20 20 20 66 6f 72 28 78 3d 73 71 6c 69 74 65 48     for(x=sqliteH
32924 61 73 68 46 69 72 73 74 28 70 54 62 6c 73 29 3b  ashFirst(pTbls);
32925 20 78 20 26 26 20 21 69 73 51 75 69 63 6b 3b 20   x && !isQuick; 
32926 78 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  x=sqliteHashNext
32927 28 78 29 29 7b 0a 20 20 20 20 20 20 20 20 54 61  (x)){.        Ta
32928 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69  ble *pTab = sqli
32929 74 65 48 61 73 68 44 61 74 61 28 78 29 3b 0a 20  teHashData(x);. 
3292a 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49         Index *pI
3292b 64 78 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  dx;.        int 
3292c 6c 6f 6f 70 54 6f 70 3b 0a 0a 20 20 20 20 20 20  loopTop;..      
3292d 20 20 69 66 28 20 70 54 61 62 2d 3e 70 49 6e 64    if( pTab->pInd
3292e 65 78 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  ex==0 ) continue
3292f 3b 0a 20 20 20 20 20 20 20 20 61 64 64 72 20 3d  ;.        addr =
32930 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
32931 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20  p1(v, OP_IfPos, 
32932 31 29 3b 20 20 2f 2a 20 53 74 6f 70 20 69 66 20  1);  /* Stop if 
32933 6f 75 74 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f  out of errors */
32934 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
32935 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
32936 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20 20  _Halt, 0, 0);.  
32937 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
32938 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
32939 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  r);.        sqli
3293a 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49  te3OpenTableAndI
3293b 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20 70  ndices(pParse, p
3293c 54 61 62 2c 20 31 2c 20 4f 50 5f 4f 70 65 6e 52  Tab, 1, OP_OpenR
3293d 65 61 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ead);.        sq
3293e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
3293f 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
32940 2c 20 32 29 3b 20 20 2f 2a 20 72 65 67 28 32 29  , 2);  /* reg(2)
32941 20 77 69 6c 6c 20 63 6f 75 6e 74 20 65 6e 74 72   will count entr
32942 69 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c  ies */.        l
32943 6f 6f 70 54 6f 70 20 3d 20 73 71 6c 69 74 65 33  oopTop = sqlite3
32944 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
32945 5f 52 65 77 69 6e 64 2c 20 31 2c 20 30 29 3b 0a  _Rewind, 1, 0);.
32946 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
32947 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
32948 41 64 64 49 6d 6d 2c 20 32 2c 20 31 29 3b 20 20  AddImm, 2, 1);  
32949 20 2f 2a 20 69 6e 63 72 65 6d 65 6e 74 20 65 6e   /* increment en
3294a 74 72 79 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 20  try count */.   
3294b 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49       for(j=0, pI
3294c 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
3294d 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
3294e 2d 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20  ->pNext, j++){. 
3294f 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a 6d 70           int jmp
32950 32 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  2;.          int
32951 20 72 31 3b 0a 20 20 20 20 20 20 20 20 20 20 73   r1;.          s
32952 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65  tatic const Vdbe
32953 4f 70 4c 69 73 74 20 69 64 78 45 72 72 5b 5d 20  OpList idxErr[] 
32954 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  = {.            
32955 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20  { OP_AddImm,    
32956 20 20 31 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20 20    1, -1,  0},.  
32957 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53            { OP_S
32958 74 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20  tring8,     0,  
32959 33 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20  3,  0},    /* 1 
3295a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  */.            {
3295b 20 4f 50 5f 52 6f 77 69 64 2c 20 20 20 20 20 20   OP_Rowid,      
3295c 20 31 2c 20 20 34 2c 20 20 30 7d 2c 0a 20 20 20   1,  4,  0},.   
3295d 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74           { OP_St
3295e 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20 35  ring8,     0,  5
3295f 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a  ,  0},    /* 3 *
32960 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  /.            { 
32961 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20  OP_String8,     
32962 30 2c 20 20 36 2c 20 20 30 7d 2c 20 20 20 20 2f  0,  6,  0},    /
32963 2a 20 34 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 4 */.         
32964 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20     { OP_Concat, 
32965 20 20 20 20 20 34 2c 20 20 33 2c 20 20 33 7d 2c       4,  3,  3},
32966 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
32967 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20 20 35  P_Concat,      5
32968 2c 20 20 33 2c 20 20 33 7d 2c 0a 20 20 20 20 20  ,  3,  3},.     
32969 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63         { OP_Conc
3296a 61 74 2c 20 20 20 20 20 20 36 2c 20 20 33 2c 20  at,      6,  3, 
3296b 20 33 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20   3},.           
3296c 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c   { OP_ResultRow,
3296d 20 20 20 33 2c 20 20 31 2c 20 20 30 7d 2c 0a 20     3,  1,  0},. 
3296e 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
3296f 49 66 50 6f 73 2c 20 20 20 20 20 20 20 31 2c 20  IfPos,       1, 
32970 20 30 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 39   0,  0},    /* 9
32971 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
32972 7b 20 4f 50 5f 48 61 6c 74 2c 20 20 20 20 20 20  { OP_Halt,      
32973 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 0a 20 20    0,  0,  0},.  
32974 20 20 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20          };.     
32975 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
32976 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65  3GenerateIndexKe
32977 79 28 70 50 61 72 73 65 2c 20 70 49 64 78 2c 20  y(pParse, pIdx, 
32978 31 2c 20 33 2c 20 30 29 3b 0a 20 20 20 20 20 20  1, 3, 0);.      
32979 20 20 20 20 6a 6d 70 32 20 3d 20 73 71 6c 69 74      jmp2 = sqlit
3297a 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28  e3VdbeAddOp4Int(
3297b 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 6a 2b 32  v, OP_Found, j+2
3297c 2c 20 30 2c 20 72 31 2c 20 70 49 64 78 2d 3e 6e  , 0, r1, pIdx->n
3297d 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 20 20 20  Column+1);.     
3297e 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
3297f 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
32980 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 69 64  (v, ArraySize(id
32981 78 45 72 72 29 2c 20 69 64 78 45 72 72 29 3b 0a  xErr), idxErr);.
32982 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
32983 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
32984 20 61 64 64 72 2b 31 2c 20 22 72 6f 77 69 64 20   addr+1, "rowid 
32985 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
32986 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
32987 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
32988 61 64 64 72 2b 33 2c 20 22 20 6d 69 73 73 69 6e  addr+3, " missin
32989 67 20 66 72 6f 6d 20 69 6e 64 65 78 20 22 2c 20  g from index ", 
3298a 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
3298b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
3298c 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
3298d 72 2b 34 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65  r+4, pIdx->zName
3298e 2c 20 50 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b  , P4_TRANSIENT);
3298f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
32990 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
32991 2c 20 61 64 64 72 2b 39 29 3b 0a 20 20 20 20 20  , addr+9);.     
32992 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
32993 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 6d 70 32  JumpHere(v, jmp2
32994 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
32995 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
32996 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
32997 74 2c 20 31 2c 20 6c 6f 6f 70 54 6f 70 2b 31 29  t, 1, loopTop+1)
32998 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
32999 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
3299a 20 6c 6f 6f 70 54 6f 70 29 3b 0a 20 20 20 20 20   loopTop);.     
3299b 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78     for(j=0, pIdx
3299c 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
3299d 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
3299e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20  pNext, j++){.   
3299f 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f         static co
329a0 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 63  nst VdbeOpList c
329a1 6e 74 49 64 78 5b 5d 20 3d 20 7b 0a 20 20 20 20  ntIdx[] = {.    
329a2 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e           { OP_In
329a3 74 65 67 65 72 2c 20 20 20 20 20 20 30 2c 20 20  teger,      0,  
329a4 33 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20  3,  0},.        
329a5 20 20 20 20 20 7b 20 4f 50 5f 52 65 77 69 6e 64       { OP_Rewind
329a6 2c 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20  ,       0,  0,  
329a7 30 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20  0},  /* 1 */.   
329a8 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 41            { OP_A
329a9 64 64 49 6d 6d 2c 20 20 20 20 20 20 20 33 2c 20  ddImm,       3, 
329aa 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20   1,  0},.       
329ab 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74 2c        { OP_Next,
329ac 20 20 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20           0,  0, 
329ad 20 30 7d 2c 20 20 2f 2a 20 33 20 2a 2f 0a 20 20   0},  /* 3 */.  
329ae 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
329af 45 71 2c 20 20 20 20 20 20 20 20 20 20 20 32 2c  Eq,           2,
329b0 20 20 30 2c 20 20 33 7d 2c 20 20 2f 2a 20 34 20    0,  3},  /* 4 
329b1 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
329b2 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20  { OP_AddImm,    
329b3 20 20 20 31 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20     1, -1,  0},. 
329b4 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
329b5 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 20 30  _String8,      0
329b6 2c 20 20 32 2c 20 20 30 7d 2c 20 20 2f 2a 20 36  ,  2,  0},  /* 6
329b7 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
329b8 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20   { OP_String8,  
329b9 20 20 20 20 30 2c 20 20 33 2c 20 20 30 7d 2c 20      0,  3,  0}, 
329ba 20 2f 2a 20 37 20 2a 2f 0a 20 20 20 20 20 20 20   /* 7 */.       
329bb 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61        { OP_Conca
329bc 74 2c 20 20 20 20 20 20 20 33 2c 20 20 32 2c 20  t,       3,  2, 
329bd 20 32 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20   2},.           
329be 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77    { OP_ResultRow
329bf 2c 20 20 20 20 32 2c 20 20 31 2c 20 20 30 7d 2c  ,    2,  1,  0},
329c0 0a 20 20 20 20 20 20 20 20 20 20 7d 3b 0a 20 20  .          };.  
329c1 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20 73          addr = s
329c2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
329c3 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29  (v, OP_IfPos, 1)
329c4 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
329c5 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
329c6 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b   OP_Halt, 0, 0);
329c7 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
329c8 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
329c9 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 20  , addr);.       
329ca 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
329cb 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
329cc 2c 20 41 72 72 61 79 53 69 7a 65 28 63 6e 74 49  , ArraySize(cntI
329cd 64 78 29 2c 20 63 6e 74 49 64 78 29 3b 0a 20 20  dx), cntIdx);.  
329ce 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
329cf 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61  dbeChangeP1(v, a
329d0 64 64 72 2b 31 2c 20 6a 2b 32 29 3b 0a 20 20 20  ddr+1, j+2);.   
329d1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
329d2 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64  beChangeP2(v, ad
329d3 64 72 2b 31 2c 20 61 64 64 72 2b 34 29 3b 0a 20  dr+1, addr+4);. 
329d4 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
329d5 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
329d6 61 64 64 72 2b 33 2c 20 6a 2b 32 29 3b 0a 20 20  addr+3, j+2);.  
329d7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
329d8 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61  dbeChangeP2(v, a
329d9 64 64 72 2b 33 2c 20 61 64 64 72 2b 32 29 3b 0a  ddr+3, addr+2);.
329da 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
329db 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
329dc 20 61 64 64 72 2b 34 29 3b 0a 20 20 20 20 20 20   addr+4);.      
329dd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
329de 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b  hangeP4(v, addr+
329df 36 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  6, .            
329e0 20 20 20 20 20 20 20 20 20 22 77 72 6f 6e 67 20           "wrong 
329e1 23 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  # of entries in 
329e2 69 6e 64 65 78 20 22 2c 20 50 34 5f 53 54 41 54  index ", P4_STAT
329e3 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  IC);.          s
329e4 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
329e5 50 34 28 76 2c 20 61 64 64 72 2b 37 2c 20 70 49  P4(v, addr+7, pI
329e6 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 54 52  dx->zName, P4_TR
329e7 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20  ANSIENT);.      
329e8 20 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20    }.      } .   
329e9 20 7d 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71   }.    addr = sq
329ea 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
329eb 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28  st(v, ArraySize(
329ec 65 6e 64 43 6f 64 65 29 2c 20 65 6e 64 43 6f 64  endCode), endCod
329ed 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  e);.    sqlite3V
329ee 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61  dbeChangeP2(v, a
329ef 64 64 72 2c 20 2d 6d 78 45 72 72 29 3b 0a 20 20  ddr, -mxErr);.  
329f0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
329f1 70 48 65 72 65 28 76 2c 20 61 64 64 72 2b 31 29  pHere(v, addr+1)
329f2 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
329f3 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
329f4 72 2b 32 2c 20 22 6f 6b 22 2c 20 50 34 5f 53 54  r+2, "ok", P4_ST
329f5 41 54 49 43 29 3b 0a 20 20 7d 65 6c 73 65 0a 23  ATIC);.  }else.#
329f6 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
329f7 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43  OMIT_INTEGRITY_C
329f8 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  HECK */..#ifndef
329f9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
329fa 31 36 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50  16.  /*.  **   P
329fb 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 0a 20  RAGMA encoding. 
329fc 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 65 6e 63   **   PRAGMA enc
329fd 6f 64 69 6e 67 20 3d 20 22 75 74 66 2d 38 22 7c  oding = "utf-8"|
329fe 22 75 74 66 2d 31 36 22 7c 22 75 74 66 2d 31 36  "utf-16"|"utf-16
329ff 6c 65 22 7c 22 75 74 66 2d 31 36 62 65 22 0a 20  le"|"utf-16be". 
32a00 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 69 74 73 20   **.  ** In its 
32a01 66 69 72 73 74 20 66 6f 72 6d 2c 20 74 68 69 73  first form, this
32a02 20 70 72 61 67 6d 61 20 72 65 74 75 72 6e 73 20   pragma returns 
32a03 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  the encoding of 
32a04 74 68 65 20 6d 61 69 6e 0a 20 20 2a 2a 20 64 61  the main.  ** da
32a05 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 64  tabase. If the d
32a06 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 69  atabase is not i
32a07 6e 69 74 69 61 6c 69 7a 65 64 2c 20 69 74 20 69  nitialized, it i
32a08 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 6e 6f  s initialized no
32a09 77 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  w..  **.  ** The
32a0a 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 6f 66 20   second form of 
32a0b 74 68 69 73 20 70 72 61 67 6d 61 20 69 73 20 61  this pragma is a
32a0c 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 6d 61   no-op if the ma
32a0d 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
32a0e 0a 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 61 6c  .  ** has not al
32a0f 72 65 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69  ready been initi
32a10 61 6c 69 7a 65 64 2e 20 49 6e 20 74 68 69 73 20  alized. In this 
32a11 63 61 73 65 20 69 74 20 73 65 74 73 20 74 68 65  case it sets the
32a12 20 64 65 66 61 75 6c 74 0a 20 20 2a 2a 20 65 6e   default.  ** en
32a13 63 6f 64 69 6e 67 20 74 68 61 74 20 77 69 6c 6c  coding that will
32a14 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65   be used for the
32a15 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
32a16 69 6c 65 20 69 66 20 61 20 6e 65 77 20 66 69 6c  ile if a new fil
32a17 65 0a 20 20 2a 2a 20 69 73 20 63 72 65 61 74 65  e.  ** is create
32a18 64 2e 20 49 66 20 61 6e 20 65 78 69 73 74 69 6e  d. If an existin
32a19 67 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  g main database 
32a1a 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 2c 20  file is opened, 
32a1b 74 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 64 65  then the.  ** de
32a1c 66 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64  fault text encod
32a1d 69 6e 67 20 66 6f 72 20 74 68 65 20 65 78 69 73  ing for the exis
32a1e 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 69 73  ting database is
32a1f 20 75 73 65 64 2e 0a 20 20 2a 2a 20 0a 20 20 2a   used..  ** .  *
32a20 2a 20 49 6e 20 61 6c 6c 20 63 61 73 65 73 20 6e  * In all cases n
32a21 65 77 20 64 61 74 61 62 61 73 65 73 20 63 72 65  ew databases cre
32a22 61 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 41  ated using the A
32a23 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64 20 61 72  TTACH command ar
32a24 65 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 74  e.  ** created t
32a25 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 64  o use the same d
32a26 65 66 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f  efault text enco
32a27 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61 69 6e  ding as the main
32a28 20 64 61 74 61 62 61 73 65 2e 20 49 66 0a 20 20   database. If.  
32a29 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  ** the main data
32a2a 62 61 73 65 20 68 61 73 20 6e 6f 74 20 62 65 65  base has not bee
32a2b 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 6e  n initialized an
32a2c 64 2f 6f 72 20 63 72 65 61 74 65 64 20 77 68 65  d/or created whe
32a2d 6e 20 41 54 54 41 43 48 0a 20 20 2a 2a 20 69 73  n ATTACH.  ** is
32a2e 20 65 78 65 63 75 74 65 64 2c 20 74 68 69 73 20   executed, this 
32a2f 69 73 20 64 6f 6e 65 20 62 65 66 6f 72 65 20 74  is done before t
32a30 68 65 20 41 54 54 41 43 48 20 6f 70 65 72 61 74  he ATTACH operat
32a31 69 6f 6e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ion..  **.  ** I
32a32 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 66 6f 72  n the second for
32a33 6d 20 74 68 69 73 20 70 72 61 67 6d 61 20 73 65  m this pragma se
32a34 74 73 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f  ts the text enco
32a35 64 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20  ding to be used 
32a36 69 6e 0a 20 20 2a 2a 20 6e 65 77 20 64 61 74 61  in.  ** new data
32a37 62 61 73 65 20 66 69 6c 65 73 20 63 72 65 61 74  base files creat
32a38 65 64 20 75 73 69 6e 67 20 74 68 69 73 20 64 61  ed using this da
32a39 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 49  tabase handle. I
32a3a 74 20 69 73 20 6f 6e 6c 79 0a 20 20 2a 2a 20 75  t is only.  ** u
32a3b 73 65 66 75 6c 20 69 66 20 69 6e 76 6f 6b 65 64  seful if invoked
32a3c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74   immediately aft
32a3d 65 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  er the main data
32a3e 62 61 73 65 20 69 0a 20 20 2a 2f 0a 20 20 69 66  base i.  */.  if
32a3f 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
32a40 28 7a 4c 65 66 74 2c 20 22 65 6e 63 6f 64 69 6e  (zLeft, "encodin
32a41 67 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74  g")==0 ){.    st
32a42 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63  atic const struc
32a43 74 20 45 6e 63 4e 61 6d 65 20 7b 0a 20 20 20 20  t EncName {.    
32a44 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20    char *zName;. 
32a45 20 20 20 20 20 75 38 20 65 6e 63 3b 0a 20 20 20       u8 enc;.   
32a46 20 7d 20 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20   } encnames[] = 
32a47 7b 0a 20 20 20 20 20 20 7b 20 22 55 54 46 38 22  {.      { "UTF8"
32a48 2c 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46  ,     SQLITE_UTF
32a49 38 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20  8        },.    
32a4a 20 20 7b 20 22 55 54 46 2d 38 22 2c 20 20 20 20    { "UTF-8",    
32a4b 53 51 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20  SQLITE_UTF8     
32a4c 20 20 20 7d 2c 20 20 2f 2a 20 4d 75 73 74 20 62     },  /* Must b
32a4d 65 20 65 6c 65 6d 65 6e 74 20 5b 31 5d 20 2a 2f  e element [1] */
32a4e 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d 31 36  .      { "UTF-16
32a4f 6c 65 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  le", SQLITE_UTF1
32a50 36 4c 45 20 20 20 20 20 7d 2c 20 20 2f 2a 20 4d  6LE     },  /* M
32a51 75 73 74 20 62 65 20 65 6c 65 6d 65 6e 74 20 5b  ust be element [
32a52 32 5d 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55  2] */.      { "U
32a53 54 46 2d 31 36 62 65 22 2c 20 53 51 4c 49 54 45  TF-16be", SQLITE
32a54 5f 55 54 46 31 36 42 45 20 20 20 20 20 7d 2c 20  _UTF16BE     }, 
32a55 20 2f 2a 20 4d 75 73 74 20 62 65 20 65 6c 65 6d   /* Must be elem
32a56 65 6e 74 20 5b 33 5d 20 2a 2f 0a 20 20 20 20 20  ent [3] */.     
32a57 20 7b 20 22 55 54 46 31 36 6c 65 22 2c 20 20 53   { "UTF16le",  S
32a58 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20  QLITE_UTF16LE   
32a59 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54    },.      { "UT
32a5a 46 31 36 62 65 22 2c 20 20 53 51 4c 49 54 45 5f  F16be",  SQLITE_
32a5b 55 54 46 31 36 42 45 20 20 20 20 20 7d 2c 0a 20  UTF16BE     },. 
32a5c 20 20 20 20 20 7b 20 22 55 54 46 2d 31 36 22 2c       { "UTF-16",
32a5d 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
32a5e 20 20 20 20 20 20 7d 2c 20 2f 2a 20 53 51 4c 49        }, /* SQLI
32a5f 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 2a  TE_UTF16NATIVE *
32a60 2f 0a 20 20 20 20 20 20 7b 20 22 55 54 46 31 36  /.      { "UTF16
32a61 22 2c 20 20 20 20 30 20 20 20 20 20 20 20 20 20  ",    0         
32a62 20 20 20 20 20 20 20 20 20 7d 2c 20 2f 2a 20 53           }, /* S
32a63 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
32a64 45 20 2a 2f 0a 20 20 20 20 20 20 7b 20 30 2c 20  E */.      { 0, 
32a65 30 20 7d 0a 20 20 20 20 7d 3b 0a 20 20 20 20 63  0 }.    };.    c
32a66 6f 6e 73 74 20 73 74 72 75 63 74 20 45 6e 63 4e  onst struct EncN
32a67 61 6d 65 20 2a 70 45 6e 63 3b 0a 20 20 20 20 69  ame *pEnc;.    i
32a68 66 28 20 21 7a 52 69 67 68 74 20 29 7b 20 20 20  f( !zRight ){   
32a69 20 2f 2a 20 22 50 52 41 47 4d 41 20 65 6e 63 6f   /* "PRAGMA enco
32a6a 64 69 6e 67 22 20 2a 2f 0a 20 20 20 20 20 20 69  ding" */.      i
32a6b 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
32a6c 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67  hema(pParse) ) g
32a6d 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
32a6e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
32a6f 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
32a70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
32a71 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
32a72 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
32a73 45 2c 20 22 65 6e 63 6f 64 69 6e 67 22 2c 20 53  E, "encoding", S
32a74 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
32a75 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
32a76 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 74 72  AddOp2(v, OP_Str
32a77 69 6e 67 38 2c 20 30 2c 20 31 29 3b 0a 20 20 20  ing8, 0, 1);.   
32a78 20 20 20 61 73 73 65 72 74 28 20 65 6e 63 6e 61     assert( encna
32a79 6d 65 73 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d  mes[SQLITE_UTF8]
32a7a 2e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  .enc==SQLITE_UTF
32a7b 38 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  8 );.      asser
32a7c 74 28 20 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49  t( encnames[SQLI
32a7d 54 45 5f 55 54 46 31 36 4c 45 5d 2e 65 6e 63 3d  TE_UTF16LE].enc=
32a7e 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20  =SQLITE_UTF16LE 
32a7f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
32a80 20 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49 54 45   encnames[SQLITE
32a81 5f 55 54 46 31 36 42 45 5d 2e 65 6e 63 3d 3d 53  _UTF16BE].enc==S
32a82 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b  QLITE_UTF16BE );
32a83 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
32a84 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
32a85 2c 20 65 6e 63 6e 61 6d 65 73 5b 45 4e 43 28 70  , encnames[ENC(p
32a86 50 61 72 73 65 2d 3e 64 62 29 5d 2e 7a 4e 61 6d  Parse->db)].zNam
32a87 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  e, P4_STATIC);. 
32a88 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
32a89 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
32a8a 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20  ultRow, 1, 1);. 
32a8b 20 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20     }else{       
32a8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32a8d 20 2f 2a 20 22 50 52 41 47 4d 41 20 65 6e 63 6f   /* "PRAGMA enco
32a8e 64 69 6e 67 20 3d 20 58 58 58 22 20 2a 2f 0a 20  ding = XXX" */. 
32a8f 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 63 68 61       /* Only cha
32a90 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  nge the value of
32a91 20 73 71 6c 69 74 65 2e 65 6e 63 20 69 66 20 74   sqlite.enc if t
32a92 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
32a93 6c 65 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20  le is not.      
32a94 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20  ** initialized. 
32a95 49 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  If the main data
32a96 62 61 73 65 20 65 78 69 73 74 73 2c 20 74 68 65  base exists, the
32a97 20 6e 65 77 20 73 71 6c 69 74 65 2e 65 6e 63 20   new sqlite.enc 
32a98 76 61 6c 75 65 0a 20 20 20 20 20 20 2a 2a 20 77  value.      ** w
32a99 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74  ill be overwritt
32a9a 65 6e 20 77 68 65 6e 20 74 68 65 20 73 63 68 65  en when the sche
32a9b 6d 61 20 69 73 20 6e 65 78 74 20 6c 6f 61 64 65  ma is next loade
32a9c 64 2e 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f  d. If it does no
32a9d 74 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 61  t.      ** alrea
32a9e 64 79 20 65 78 69 73 74 73 2c 20 69 74 20 77 69  dy exists, it wi
32a9f 6c 6c 20 62 65 20 63 72 65 61 74 65 64 20 74 6f  ll be created to
32aa0 20 75 73 65 20 74 68 65 20 6e 65 77 20 65 6e 63   use the new enc
32aa1 6f 64 69 6e 67 20 76 61 6c 75 65 2e 0a 20 20 20  oding value..   
32aa2 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
32aa3 0a 20 20 20 20 20 20 20 20 21 28 44 62 48 61 73  .        !(DbHas
32aa4 50 72 6f 70 65 72 74 79 28 64 62 2c 20 30 2c 20  Property(db, 0, 
32aa5 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29  DB_SchemaLoaded)
32aa6 29 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 44 62  ) || .        Db
32aa7 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20  HasProperty(db, 
32aa8 30 2c 20 44 42 5f 45 6d 70 74 79 29 20 0a 20 20  0, DB_Empty) .  
32aa9 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 66      ){.        f
32aaa 6f 72 28 70 45 6e 63 3d 26 65 6e 63 6e 61 6d 65  or(pEnc=&encname
32aab 73 5b 30 5d 3b 20 70 45 6e 63 2d 3e 7a 4e 61 6d  s[0]; pEnc->zNam
32aac 65 3b 20 70 45 6e 63 2b 2b 29 7b 0a 20 20 20 20  e; pEnc++){.    
32aad 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c        if( 0==sql
32aae 69 74 65 33 53 74 72 49 43 6d 70 28 7a 52 69 67  ite3StrICmp(zRig
32aaf 68 74 2c 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 29  ht, pEnc->zName)
32ab0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
32ab1 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 20  ENC(pParse->db) 
32ab2 3d 20 70 45 6e 63 2d 3e 65 6e 63 20 3f 20 70 45  = pEnc->enc ? pE
32ab3 6e 63 2d 3e 65 6e 63 20 3a 20 53 51 4c 49 54 45  nc->enc : SQLITE
32ab4 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20  _UTF16NATIVE;.  
32ab5 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
32ab6 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
32ab7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
32ab8 66 28 20 21 70 45 6e 63 2d 3e 7a 4e 61 6d 65 20  f( !pEnc->zName 
32ab9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
32aba 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
32abb 72 73 65 2c 20 22 75 6e 73 75 70 70 6f 72 74 65  rse, "unsupporte
32abc 64 20 65 6e 63 6f 64 69 6e 67 3a 20 25 73 22 2c  d encoding: %s",
32abd 20 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20   zRight);.      
32abe 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
32abf 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  }.  }else.#endif
32ac0 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
32ac1 55 54 46 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65  UTF16 */..#ifnde
32ac2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43  f SQLITE_OMIT_SC
32ac3 48 45 4d 41 5f 56 45 52 53 49 4f 4e 5f 50 52 41  HEMA_VERSION_PRA
32ac4 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  GMAS.  /*.  **  
32ac5 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
32ac6 65 2e 5d 73 63 68 65 6d 61 5f 76 65 72 73 69 6f  e.]schema_versio
32ac7 6e 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  n.  **   PRAGMA 
32ac8 5b 64 61 74 61 62 61 73 65 2e 5d 73 63 68 65 6d  [database.]schem
32ac9 61 5f 76 65 72 73 69 6f 6e 20 3d 20 3c 69 6e 74  a_version = <int
32aca 65 67 65 72 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  eger>.  **.  ** 
32acb 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
32acc 73 65 2e 5d 75 73 65 72 5f 76 65 72 73 69 6f 6e  se.]user_version
32acd 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b  .  **   PRAGMA [
32ace 64 61 74 61 62 61 73 65 2e 5d 75 73 65 72 5f 76  database.]user_v
32acf 65 72 73 69 6f 6e 20 3d 20 3c 69 6e 74 65 67 65  ersion = <intege
32ad0 72 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  r>.  **.  ** The
32ad1 20 70 72 61 67 6d 61 27 73 20 73 63 68 65 6d 61   pragma's schema
32ad2 5f 76 65 72 73 69 6f 6e 20 61 6e 64 20 75 73 65  _version and use
32ad3 72 5f 76 65 72 73 69 6f 6e 20 61 72 65 20 75 73  r_version are us
32ad4 65 64 20 74 6f 20 73 65 74 20 6f 72 20 67 65 74  ed to set or get
32ad5 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  .  ** the value 
32ad6 6f 66 20 74 68 65 20 73 63 68 65 6d 61 2d 76 65  of the schema-ve
32ad7 72 73 69 6f 6e 20 61 6e 64 20 75 73 65 72 2d 76  rsion and user-v
32ad8 65 72 73 69 6f 6e 2c 20 72 65 73 70 65 63 74 69  ersion, respecti
32ad9 76 65 6c 79 2e 20 42 6f 74 68 0a 20 20 2a 2a 20  vely. Both.  ** 
32ada 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69  the schema-versi
32adb 6f 6e 20 61 6e 64 20 74 68 65 20 75 73 65 72 2d  on and the user-
32adc 76 65 72 73 69 6f 6e 20 61 72 65 20 33 32 2d 62  version are 32-b
32add 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  it signed intege
32ade 72 73 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 69  rs.  ** stored i
32adf 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  n the database h
32ae0 65 61 64 65 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  eader..  **.  **
32ae1 20 54 68 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b   The schema-cook
32ae2 69 65 20 69 73 20 75 73 75 61 6c 6c 79 20 6f 6e  ie is usually on
32ae3 6c 79 20 6d 61 6e 69 70 75 6c 61 74 65 64 20 69  ly manipulated i
32ae4 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c  nternally by SQL
32ae5 69 74 65 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20  ite. It.  ** is 
32ae6 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 53  incremented by S
32ae7 51 4c 69 74 65 20 77 68 65 6e 65 76 65 72 20 74  QLite whenever t
32ae8 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
32ae9 6d 61 20 69 73 20 6d 6f 64 69 66 69 65 64 20 28  ma is modified (
32aea 62 79 0a 20 20 2a 2a 20 63 72 65 61 74 69 6e 67  by.  ** creating
32aeb 20 6f 72 20 64 72 6f 70 70 69 6e 67 20 61 20 74   or dropping a t
32aec 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 29 2e 20  able or index). 
32aed 54 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 69  The schema versi
32aee 6f 6e 20 69 73 20 75 73 65 64 20 62 79 0a 20 20  on is used by.  
32aef 2a 2a 20 53 51 4c 69 74 65 20 65 61 63 68 20 74  ** SQLite each t
32af0 69 6d 65 20 61 20 71 75 65 72 79 20 69 73 20 65  ime a query is e
32af1 78 65 63 75 74 65 64 20 74 6f 20 65 6e 73 75 72  xecuted to ensur
32af2 65 20 74 68 61 74 20 74 68 65 20 69 6e 74 65 72  e that the inter
32af3 6e 61 6c 20 63 61 63 68 65 0a 20 20 2a 2a 20 6f  nal cache.  ** o
32af4 66 20 74 68 65 20 73 63 68 65 6d 61 20 75 73 65  f the schema use
32af5 64 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67  d when compiling
32af6 20 74 68 65 20 53 51 4c 20 71 75 65 72 79 20 6d   the SQL query m
32af7 61 74 63 68 65 73 20 74 68 65 20 73 63 68 65 6d  atches the schem
32af8 61 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61  a of.  ** the da
32af9 74 61 62 61 73 65 20 61 67 61 69 6e 73 74 20 77  tabase against w
32afa 68 69 63 68 20 74 68 65 20 63 6f 6d 70 69 6c 65  hich the compile
32afb 64 20 71 75 65 72 79 20 69 73 20 61 63 74 75 61  d query is actua
32afc 6c 6c 79 20 65 78 65 63 75 74 65 64 2e 0a 20 20  lly executed..  
32afd 2a 2a 20 53 75 62 76 65 72 74 69 6e 67 20 74 68  ** Subverting th
32afe 69 73 20 6d 65 63 68 61 6e 69 73 6d 20 62 79 20  is mechanism by 
32aff 75 73 69 6e 67 20 22 50 52 41 47 4d 41 20 73 63  using "PRAGMA sc
32b00 68 65 6d 61 5f 76 65 72 73 69 6f 6e 22 20 74 6f  hema_version" to
32b01 20 6d 6f 64 69 66 79 0a 20 20 2a 2a 20 74 68 65   modify.  ** the
32b02 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20   schema-version 
32b03 69 73 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 64  is potentially d
32b04 61 6e 67 65 72 6f 75 73 20 61 6e 64 20 6d 61 79  angerous and may
32b05 20 6c 65 61 64 20 74 6f 20 70 72 6f 67 72 61 6d   lead to program
32b06 0a 20 20 2a 2a 20 63 72 61 73 68 65 73 20 6f 72  .  ** crashes or
32b07 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
32b08 74 69 6f 6e 2e 20 55 73 65 20 77 69 74 68 20 63  tion. Use with c
32b09 61 75 74 69 6f 6e 21 0a 20 20 2a 2a 0a 20 20 2a  aution!.  **.  *
32b0a 2a 20 54 68 65 20 75 73 65 72 2d 76 65 72 73 69  * The user-versi
32b0b 6f 6e 20 69 73 20 6e 6f 74 20 75 73 65 64 20 69  on is not used i
32b0c 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c  nternally by SQL
32b0d 69 74 65 2e 20 49 74 20 6d 61 79 20 62 65 20 75  ite. It may be u
32b0e 73 65 64 20 62 79 0a 20 20 2a 2a 20 61 70 70 6c  sed by.  ** appl
32b0f 69 63 61 74 69 6f 6e 73 20 66 6f 72 20 61 6e 79  ications for any
32b10 20 70 75 72 70 6f 73 65 2e 0a 20 20 2a 2f 0a 20   purpose..  */. 
32b11 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
32b12 43 6d 70 28 7a 4c 65 66 74 2c 20 22 73 63 68 65  Cmp(zLeft, "sche
32b13 6d 61 5f 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20  ma_version")==0 
32b14 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74  .   || sqlite3St
32b15 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 75 73  rICmp(zLeft, "us
32b16 65 72 5f 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20  er_version")==0 
32b17 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74  .   || sqlite3St
32b18 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 66 72  rICmp(zLeft, "fr
32b19 65 65 6c 69 73 74 5f 63 6f 75 6e 74 22 29 3d 3d  eelist_count")==
32b1a 30 20 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20  0 .  ){.    int 
32b1b 69 43 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 43 6f  iCookie;   /* Co
32b1c 6f 6b 69 65 20 69 6e 64 65 78 2e 20 31 20 66 6f  okie index. 1 fo
32b1d 72 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 2c  r schema-cookie,
32b1e 20 36 20 66 6f 72 20 75 73 65 72 2d 63 6f 6f 6b   6 for user-cook
32b1f 69 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  ie. */.    sqlit
32b20 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28  e3VdbeUsesBtree(
32b21 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 77 69  v, iDb);.    swi
32b22 74 63 68 28 20 7a 4c 65 66 74 5b 30 5d 20 29 7b  tch( zLeft[0] ){
32b23 0a 20 20 20 20 20 20 63 61 73 65 20 27 66 27 3a  .      case 'f':
32b24 20 63 61 73 65 20 27 46 27 3a 0a 20 20 20 20 20   case 'F':.     
32b25 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20 42 54 52     iCookie = BTR
32b26 45 45 5f 46 52 45 45 5f 50 41 47 45 5f 43 4f 55  EE_FREE_PAGE_COU
32b27 4e 54 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  NT;.        brea
32b28 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 27 73  k;.      case 's
32b29 27 3a 20 63 61 73 65 20 27 53 27 3a 0a 20 20 20  ': case 'S':.   
32b2a 20 20 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20 42       iCookie = B
32b2b 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53  TREE_SCHEMA_VERS
32b2c 49 4f 4e 3b 0a 20 20 20 20 20 20 20 20 62 72 65  ION;.        bre
32b2d 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c  ak;.      defaul
32b2e 74 3a 0a 20 20 20 20 20 20 20 20 69 43 6f 6f 6b  t:.        iCook
32b2f 69 65 20 3d 20 42 54 52 45 45 5f 55 53 45 52 5f  ie = BTREE_USER_
32b30 56 45 52 53 49 4f 4e 3b 0a 20 20 20 20 20 20 20  VERSION;.       
32b31 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
32b32 20 20 20 69 66 28 20 7a 52 69 67 68 74 20 26 26     if( zRight &&
32b33 20 69 43 6f 6f 6b 69 65 21 3d 42 54 52 45 45 5f   iCookie!=BTREE_
32b34 46 52 45 45 5f 50 41 47 45 5f 43 4f 55 4e 54 20  FREE_PAGE_COUNT 
32b35 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57 72 69 74  ){.      /* Writ
32b36 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  e the specified 
32b37 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20 2a 2f 0a  cookie value */.
32b38 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
32b39 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 73 65  st VdbeOpList se
32b3a 74 43 6f 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20 20  tCookie[] = {.  
32b3b 20 20 20 20 20 20 7b 20 4f 50 5f 54 72 61 6e 73        { OP_Trans
32b3c 61 63 74 69 6f 6e 2c 20 20 20 20 30 2c 20 20 31  action,    0,  1
32b3d 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a  ,  0},    /* 0 *
32b3e 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49  /.        { OP_I
32b3f 6e 74 65 67 65 72 2c 20 20 20 20 20 20 20 20 30  nteger,        0
32b40 2c 20 20 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a  ,  1,  0},    /*
32b41 20 31 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20   1 */.        { 
32b42 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 20 20  OP_SetCookie,   
32b43 20 20 20 30 2c 20 20 30 2c 20 20 31 7d 2c 20 20     0,  0,  1},  
32b44 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20    /* 2 */.      
32b45 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  };.      int add
32b46 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
32b47 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61  ddOpList(v, Arra
32b48 79 53 69 7a 65 28 73 65 74 43 6f 6f 6b 69 65 29  ySize(setCookie)
32b49 2c 20 73 65 74 43 6f 6f 6b 69 65 29 3b 0a 20 20  , setCookie);.  
32b4a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
32b4b 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c  hangeP1(v, addr,
32b4c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c   iDb);.      sql
32b4d 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
32b4e 28 76 2c 20 61 64 64 72 2b 31 2c 20 73 71 6c 69  (v, addr+1, sqli
32b4f 74 65 33 41 74 6f 69 28 7a 52 69 67 68 74 29 29  te3Atoi(zRight))
32b50 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
32b51 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61  dbeChangeP1(v, a
32b52 64 64 72 2b 32 2c 20 69 44 62 29 3b 0a 20 20 20  ddr+2, iDb);.   
32b53 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
32b54 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2b 32  angeP2(v, addr+2
32b55 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a 20 20 20 20  , iCookie);.    
32b56 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
32b57 52 65 61 64 20 74 68 65 20 73 70 65 63 69 66 69  Read the specifi
32b58 65 64 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20  ed cookie value 
32b59 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 20  */.      static 
32b5a 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74  const VdbeOpList
32b5b 20 72 65 61 64 43 6f 6f 6b 69 65 5b 5d 20 3d 20   readCookie[] = 
32b5c 7b 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 54  {.        { OP_T
32b5d 72 61 6e 73 61 63 74 69 6f 6e 2c 20 20 20 20 20  ransaction,     
32b5e 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20 20 20 2f  0,  0,  0},    /
32b5f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b  * 0 */.        {
32b60 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20   OP_ReadCookie, 
32b61 20 20 20 20 20 30 2c 20 20 31 2c 20 20 30 7d 2c       0,  1,  0},
32b62 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20      /* 1 */.    
32b63 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52      { OP_ResultR
32b64 6f 77 2c 20 20 20 20 20 20 20 31 2c 20 20 31 2c  ow,       1,  1,
32b65 20 20 30 7d 0a 20 20 20 20 20 20 7d 3b 0a 20 20    0}.      };.  
32b66 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73      int addr = s
32b67 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
32b68 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65  ist(v, ArraySize
32b69 28 72 65 61 64 43 6f 6f 6b 69 65 29 2c 20 72 65  (readCookie), re
32b6a 61 64 43 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 20  adCookie);.     
32b6b 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
32b6c 67 65 50 31 28 76 2c 20 61 64 64 72 2c 20 69 44  geP1(v, addr, iD
32b6d 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
32b6e 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
32b6f 20 61 64 64 72 2b 31 2c 20 69 44 62 29 3b 0a 20   addr+1, iDb);. 
32b70 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
32b71 43 68 61 6e 67 65 50 33 28 76 2c 20 61 64 64 72  ChangeP3(v, addr
32b72 2b 31 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a 20 20  +1, iCookie);.  
32b73 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
32b74 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
32b75 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
32b76 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
32b77 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
32b78 20 7a 4c 65 66 74 2c 20 53 51 4c 49 54 45 5f 54   zLeft, SQLITE_T
32b79 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 7d  RANSIENT);.    }
32b7a 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
32b7b 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  /* SQLITE_OMIT_S
32b7c 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 5f 50 52  CHEMA_VERSION_PR
32b7d 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65  AGMAS */..#ifnde
32b7e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f  f SQLITE_OMIT_CO
32b7f 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47  MPILEOPTION_DIAG
32b80 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52  S.  /*.  **   PR
32b81 41 47 4d 41 20 63 6f 6d 70 69 6c 65 5f 6f 70 74  AGMA compile_opt
32b82 69 6f 6e 73 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52  ions.  **.  ** R
32b83 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 73 20  eturn the names 
32b84 6f 66 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 2d 74  of all compile-t
32b85 69 6d 65 20 6f 70 74 69 6f 6e 73 20 75 73 65 64  ime options used
32b86 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c 0a   in this build,.
32b87 20 20 2a 2a 20 6f 6e 65 20 6f 70 74 69 6f 6e 20    ** one option 
32b88 70 65 72 20 72 6f 77 2e 0a 20 20 2a 2f 0a 20 20  per row..  */.  
32b89 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
32b8a 6d 70 28 7a 4c 65 66 74 2c 20 22 63 6f 6d 70 69  mp(zLeft, "compi
32b8b 6c 65 5f 6f 70 74 69 6f 6e 73 22 29 3d 3d 30 20  le_options")==0 
32b8c 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 30  ){.    int i = 0
32b8d 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
32b8e 20 2a 7a 4f 70 74 3b 0a 20 20 20 20 73 71 6c 69   *zOpt;.    sqli
32b8f 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
32b90 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 70 50 61  s(v, 1);.    pPa
32b91 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 31 3b 0a 20  rse->nMem = 1;. 
32b92 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
32b93 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
32b94 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 6f  OLNAME_NAME, "co
32b95 6d 70 69 6c 65 5f 6f 70 74 69 6f 6e 22 2c 20 53  mpile_option", S
32b96 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
32b97 20 20 20 77 68 69 6c 65 28 20 28 7a 4f 70 74 20     while( (zOpt 
32b98 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c  = sqlite3_compil
32b99 65 6f 70 74 69 6f 6e 5f 67 65 74 28 69 2b 2b 29  eoption_get(i++)
32b9a 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )!=0 ){.      sq
32b9b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
32b9c 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
32b9d 2c 20 31 2c 20 30 2c 20 7a 4f 70 74 2c 20 30 29  , 1, 0, zOpt, 0)
32b9e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
32b9f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
32ba0 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29  ResultRow, 1, 1)
32ba1 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
32ba2 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
32ba3 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54  _OMIT_COMPILEOPT
32ba4 49 4f 4e 5f 44 49 41 47 53 20 2a 2f 0a 0a 23 69  ION_DIAGS */..#i
32ba5 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
32ba6 54 5f 57 41 4c 0a 20 20 2f 2a 0a 20 20 2a 2a 20  T_WAL.  /*.  ** 
32ba7 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
32ba8 73 65 2e 5d 77 61 6c 5f 63 68 65 63 6b 70 6f 69  se.]wal_checkpoi
32ba9 6e 74 20 3d 20 70 61 73 73 69 76 65 7c 66 75 6c  nt = passive|ful
32baa 6c 7c 72 65 73 74 61 72 74 0a 20 20 2a 2a 0a 20  l|restart.  **. 
32bab 20 2a 2a 20 43 68 65 63 6b 70 6f 69 6e 74 20 74   ** Checkpoint t
32bac 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a  he database..  *
32bad 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
32bae 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 77  trICmp(zLeft, "w
32baf 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 22 29 3d  al_checkpoint")=
32bb0 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42  =0 ){.    int iB
32bb1 74 20 3d 20 28 70 49 64 32 2d 3e 7a 3f 69 44 62  t = (pId2->z?iDb
32bb2 3a 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41  :SQLITE_MAX_ATTA
32bb3 43 48 45 44 29 3b 0a 20 20 20 20 69 6e 74 20 65  CHED);.    int e
32bb4 4d 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 48  Mode = SQLITE_CH
32bb5 45 43 4b 50 4f 49 4e 54 5f 50 41 53 53 49 56 45  ECKPOINT_PASSIVE
32bb6 3b 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74  ;.    if( zRight
32bb7 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71   ){.      if( sq
32bb8 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 52 69  lite3StrICmp(zRi
32bb9 67 68 74 2c 20 22 66 75 6c 6c 22 29 3d 3d 30 20  ght, "full")==0 
32bba 29 7b 0a 20 20 20 20 20 20 20 20 65 4d 6f 64 65  ){.        eMode
32bbb 20 3d 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 50   = SQLITE_CHECKP
32bbc 4f 49 4e 54 5f 46 55 4c 4c 3b 0a 20 20 20 20 20  OINT_FULL;.     
32bbd 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74   }else if( sqlit
32bbe 65 33 53 74 72 49 43 6d 70 28 7a 52 69 67 68 74  e3StrICmp(zRight
32bbf 2c 20 22 72 65 73 74 61 72 74 22 29 3d 3d 30 20  , "restart")==0 
32bc0 29 7b 0a 20 20 20 20 20 20 20 20 65 4d 6f 64 65  ){.        eMode
32bc1 20 3d 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 50   = SQLITE_CHECKP
32bc2 4f 49 4e 54 5f 52 45 53 54 41 52 54 3b 0a 20 20  OINT_RESTART;.  
32bc3 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
32bc4 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53  if( sqlite3ReadS
32bc5 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20  chema(pParse) ) 
32bc6 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b  goto pragma_out;
32bc7 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
32bc8 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29  SetNumCols(v, 3)
32bc9 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d  ;.    pParse->nM
32bca 65 6d 20 3d 20 33 3b 0a 20 20 20 20 73 71 6c 69  em = 3;.    sqli
32bcb 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
32bcc 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
32bcd 4e 41 4d 45 2c 20 22 62 75 73 79 22 2c 20 53 51  NAME, "busy", SQ
32bce 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
32bcf 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
32bd0 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f  ColName(v, 1, CO
32bd1 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6c 6f 67  LNAME_NAME, "log
32bd2 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
32bd3 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
32bd4 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
32bd5 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  2, COLNAME_NAME,
32bd6 20 22 63 68 65 63 6b 70 6f 69 6e 74 65 64 22 2c   "checkpointed",
32bd7 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
32bd8 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
32bd9 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 68  eAddOp3(v, OP_Ch
32bda 65 63 6b 70 6f 69 6e 74 2c 20 69 42 74 2c 20 65  eckpoint, iBt, e
32bdb 4d 6f 64 65 2c 20 31 29 3b 0a 20 20 20 20 73 71  Mode, 1);.    sq
32bdc 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
32bdd 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
32bde 20 31 2c 20 33 29 3b 0a 20 20 7d 65 6c 73 65 0a   1, 3);.  }else.
32bdf 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41  .  /*.  **   PRA
32be0 47 4d 41 20 77 61 6c 5f 61 75 74 6f 63 68 65 63  GMA wal_autochec
32be1 6b 70 6f 69 6e 74 0a 20 20 2a 2a 20 20 20 50 52  kpoint.  **   PR
32be2 41 47 4d 41 20 77 61 6c 5f 61 75 74 6f 63 68 65  AGMA wal_autoche
32be3 63 6b 70 6f 69 6e 74 20 3d 20 4e 0a 20 20 2a 2a  ckpoint = N.  **
32be4 0a 20 20 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20  .  ** Configure 
32be5 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  a database conne
32be6 63 74 69 6f 6e 20 74 6f 20 61 75 74 6f 6d 61 74  ction to automat
32be7 69 63 61 6c 6c 79 20 63 68 65 63 6b 70 6f 69 6e  ically checkpoin
32be8 74 20 61 20 64 61 74 61 62 61 73 65 0a 20 20 2a  t a database.  *
32be9 2a 20 61 66 74 65 72 20 61 63 63 75 6d 75 6c 61  * after accumula
32bea 74 69 6e 67 20 4e 20 66 72 61 6d 65 73 20 69 6e  ting N frames in
32beb 20 74 68 65 20 6c 6f 67 2e 20 4f 72 20 71 75 65   the log. Or que
32bec 72 79 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  ry for the curre
32bed 6e 74 20 76 61 6c 75 65 0a 20 20 2a 2a 20 6f 66  nt value.  ** of
32bee 20 4e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73   N..  */.  if( s
32bef 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
32bf0 65 66 74 2c 20 22 77 61 6c 5f 61 75 74 6f 63 68  eft, "wal_autoch
32bf1 65 63 6b 70 6f 69 6e 74 22 29 3d 3d 30 20 29 7b  eckpoint")==0 ){
32bf2 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20  .    if( zRight 
32bf3 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
32bf4 5f 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f  _wal_autocheckpo
32bf5 69 6e 74 28 64 62 2c 20 73 71 6c 69 74 65 33 41  int(db, sqlite3A
32bf6 74 6f 69 28 7a 52 69 67 68 74 29 29 3b 0a 20 20  toi(zRight));.  
32bf7 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 53 69    }.    returnSi
32bf8 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20  ngleInt(pParse, 
32bf9 22 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f  "wal_autocheckpo
32bfa 69 6e 74 22 2c 20 0a 20 20 20 20 20 20 20 64 62  int", .       db
32bfb 2d 3e 78 57 61 6c 43 61 6c 6c 62 61 63 6b 3d 3d  ->xWalCallback==
32bfc 73 71 6c 69 74 65 33 57 61 6c 44 65 66 61 75 6c  sqlite3WalDefaul
32bfd 74 48 6f 6f 6b 20 3f 20 0a 20 20 20 20 20 20 20  tHook ? .       
32bfe 20 20 20 20 53 51 4c 49 54 45 5f 50 54 52 5f 54      SQLITE_PTR_T
32bff 4f 5f 49 4e 54 28 64 62 2d 3e 70 57 61 6c 41 72  O_INT(db->pWalAr
32c00 67 29 20 3a 20 30 29 3b 0a 20 20 7d 65 6c 73 65  g) : 0);.  }else
32c01 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a 20 20  .#endif..  /*.  
32c02 2a 2a 20 20 50 52 41 47 4d 41 20 73 68 72 69 6e  **  PRAGMA shrin
32c03 6b 5f 6d 65 6d 6f 72 79 0a 20 20 2a 2a 0a 20 20  k_memory.  **.  
32c04 2a 2a 20 54 68 69 73 20 70 72 61 67 6d 61 20 61  ** This pragma a
32c05 74 74 65 6d 70 74 73 20 74 6f 20 66 72 65 65 20  ttempts to free 
32c06 61 73 20 6d 75 63 68 20 6d 65 6d 6f 72 79 20 61  as much memory a
32c07 73 20 70 6f 73 73 69 62 6c 65 20 66 72 6f 6d 20  s possible from 
32c08 74 68 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74  the.  ** current
32c09 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
32c0a 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  tion..  */.  if(
32c0b 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
32c0c 7a 4c 65 66 74 2c 20 22 73 68 72 69 6e 6b 5f 6d  zLeft, "shrink_m
32c0d 65 6d 6f 72 79 22 29 3d 3d 30 20 29 7b 0a 20 20  emory")==0 ){.  
32c0e 20 20 73 71 6c 69 74 65 33 5f 64 62 5f 72 65 6c    sqlite3_db_rel
32c0f 65 61 73 65 5f 6d 65 6d 6f 72 79 28 64 62 29 3b  ease_memory(db);
32c10 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20  .  }else..  /*. 
32c11 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 62 75 73   **   PRAGMA bus
32c12 79 5f 74 69 6d 65 6f 75 74 0a 20 20 2a 2a 20 20  y_timeout.  **  
32c13 20 50 52 41 47 4d 41 20 62 75 73 79 5f 74 69 6d   PRAGMA busy_tim
32c14 65 6f 75 74 20 3d 20 4e 0a 20 20 2a 2a 0a 20 20  eout = N.  **.  
32c15 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 5f  ** Call sqlite3_
32c16 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 64 62 2c  busy_timeout(db,
32c17 20 4e 29 2e 20 20 52 65 74 75 72 6e 20 74 68 65   N).  Return the
32c18 20 63 75 72 72 65 6e 74 20 74 69 6d 65 6f 75 74   current timeout
32c19 20 76 61 6c 75 65 0a 20 20 2a 2a 20 69 66 20 6f   value.  ** if o
32c1a 6e 65 20 69 73 20 73 65 74 2e 20 20 49 66 20 6e  ne is set.  If n
32c1b 6f 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 6f  o busy handler o
32c1c 72 20 61 20 64 69 66 66 65 72 65 6e 74 20 62 75  r a different bu
32c1d 73 79 20 68 61 6e 64 6c 65 72 20 69 73 20 73 65  sy handler is se
32c1e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 30 20 69 73  t.  ** then 0 is
32c1f 20 72 65 74 75 72 6e 65 64 2e 20 20 53 65 74 74   returned.  Sett
32c20 69 6e 67 20 74 68 65 20 62 75 73 79 5f 74 69 6d  ing the busy_tim
32c21 65 6f 75 74 20 74 6f 20 30 20 6f 72 20 6e 65 67  eout to 0 or neg
32c22 61 74 69 76 65 0a 20 20 2a 2a 20 64 69 73 61 62  ative.  ** disab
32c23 6c 65 73 20 74 68 65 20 74 69 6d 65 6f 75 74 2e  les the timeout.
32c24 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
32c25 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
32c26 2c 20 22 62 75 73 79 5f 74 69 6d 65 6f 75 74 22  , "busy_timeout"
32c27 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )==0 ){.    if( 
32c28 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
32c29 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d  sqlite3_busy_tim
32c2a 65 6f 75 74 28 64 62 2c 20 73 71 6c 69 74 65 33  eout(db, sqlite3
32c2b 41 74 6f 69 28 7a 52 69 67 68 74 29 29 3b 0a 20  Atoi(zRight));. 
32c2c 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 53     }.    returnS
32c2d 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c  ingleInt(pParse,
32c2e 20 22 74 69 6d 65 6f 75 74 22 2c 20 20 64 62 2d   "timeout",  db-
32c2f 3e 62 75 73 79 54 69 6d 65 6f 75 74 29 3b 0a 20  >busyTimeout);. 
32c30 20 7d 65 6c 73 65 0a 0a 23 69 66 20 64 65 66 69   }else..#if defi
32c31 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
32c32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
32c33 49 54 45 5f 54 45 53 54 29 0a 20 20 2f 2a 0a 20  ITE_TEST).  /*. 
32c34 20 2a 2a 20 52 65 70 6f 72 74 20 74 68 65 20 63   ** Report the c
32c35 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20  urrent state of 
32c36 66 69 6c 65 20 6c 6f 67 73 20 66 6f 72 20 61 6c  file logs for al
32c37 6c 20 64 61 74 61 62 61 73 65 73 0a 20 20 2a 2f  l databases.  */
32c38 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
32c39 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 6c 6f  rICmp(zLeft, "lo
32c3a 63 6b 5f 73 74 61 74 75 73 22 29 3d 3d 30 20 29  ck_status")==0 )
32c3b 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  {.    static con
32c3c 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 61  st char *const a
32c3d 7a 4c 6f 63 6b 4e 61 6d 65 5b 5d 20 3d 20 7b 0a  zLockName[] = {.
32c3e 20 20 20 20 20 20 22 75 6e 6c 6f 63 6b 65 64 22        "unlocked"
32c3f 2c 20 22 73 68 61 72 65 64 22 2c 20 22 72 65 73  , "shared", "res
32c40 65 72 76 65 64 22 2c 20 22 70 65 6e 64 69 6e 67  erved", "pending
32c41 22 2c 20 22 65 78 63 6c 75 73 69 76 65 22 0a 20  ", "exclusive". 
32c42 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 69 3b     };.    int i;
32c43 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
32c44 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 32 29  SetNumCols(v, 2)
32c45 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d  ;.    pParse->nM
32c46 65 6d 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69  em = 2;.    sqli
32c47 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
32c48 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
32c49 4e 41 4d 45 2c 20 22 64 61 74 61 62 61 73 65 22  NAME, "database"
32c4a 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
32c4b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
32c4c 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31  eSetColName(v, 1
32c4d 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
32c4e 22 73 74 61 74 75 73 22 2c 20 53 51 4c 49 54 45  "status", SQLITE
32c4f 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66 6f  _STATIC);.    fo
32c50 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62  r(i=0; i<db->nDb
32c51 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74  ; i++){.      Bt
32c52 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 20 20  ree *pBt;.      
32c53 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61  const char *zSta
32c54 74 65 20 3d 20 22 75 6e 6b 6e 6f 77 6e 22 3b 0a  te = "unknown";.
32c55 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
32c56 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69     if( db->aDb[i
32c57 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 29 20 63 6f 6e  ].zName==0 ) con
32c58 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 73 71 6c  tinue;.      sql
32c59 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
32c5a 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
32c5b 20 31 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69   1, 0, db->aDb[i
32c5c 5d 2e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54  ].zName, P4_STAT
32c5d 49 43 29 3b 0a 20 20 20 20 20 20 70 42 74 20 3d  IC);.      pBt =
32c5e 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
32c5f 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 3d 3d  .      if( pBt==
32c60 30 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72 65  0 || sqlite3Btre
32c61 65 50 61 67 65 72 28 70 42 74 29 3d 3d 30 20 29  ePager(pBt)==0 )
32c62 7b 0a 20 20 20 20 20 20 20 20 7a 53 74 61 74 65  {.        zState
32c63 20 3d 20 22 63 6c 6f 73 65 64 22 3b 0a 20 20 20   = "closed";.   
32c64 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c     }else if( sql
32c65 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
32c66 6c 28 64 62 2c 20 69 20 3f 20 64 62 2d 3e 61 44  l(db, i ? db->aD
32c67 62 5b 69 5d 2e 7a 4e 61 6d 65 20 3a 20 30 2c 20  b[i].zName : 0, 
32c68 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32c69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c6a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46 43 4e        SQLITE_FCN
32c6b 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 2c 20 26 6a  TL_LOCKSTATE, &j
32c6c 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  )==SQLITE_OK ){.
32c6d 20 20 20 20 20 20 20 20 20 7a 53 74 61 74 65 20           zState 
32c6e 3d 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 6a 5d 3b  = azLockName[j];
32c6f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
32c70 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
32c71 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
32c72 30 2c 20 32 2c 20 30 2c 20 7a 53 74 61 74 65 2c  0, 2, 0, zState,
32c73 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
32c74 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
32c75 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
32c76 74 52 6f 77 2c 20 31 2c 20 32 29 3b 0a 20 20 20  tRow, 1, 2);.   
32c77 20 7d 0a 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64   }..  }else.#end
32c78 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
32c79 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66  E_HAS_CODEC.  if
32c7a 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
32c7b 28 7a 4c 65 66 74 2c 20 22 6b 65 79 22 29 3d 3d  (zLeft, "key")==
32c7c 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20  0 && zRight ){. 
32c7d 20 20 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 64     sqlite3_key(d
32c7e 62 2c 20 7a 52 69 67 68 74 2c 20 73 71 6c 69 74  b, zRight, sqlit
32c7f 65 33 53 74 72 6c 65 6e 33 30 28 7a 52 69 67 68  e3Strlen30(zRigh
32c80 74 29 29 3b 0a 20 20 7d 65 6c 73 65 0a 20 20 69  t));.  }else.  i
32c81 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
32c82 70 28 7a 4c 65 66 74 2c 20 22 72 65 6b 65 79 22  p(zLeft, "rekey"
32c83 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29  )==0 && zRight )
32c84 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
32c85 6b 65 79 28 64 62 2c 20 7a 52 69 67 68 74 2c 20  key(db, zRight, 
32c86 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
32c87 7a 52 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c 73  zRight));.  }els
32c88 65 0a 20 20 69 66 28 20 7a 52 69 67 68 74 20 26  e.  if( zRight &
32c89 26 20 28 73 71 6c 69 74 65 33 53 74 72 49 43 6d  & (sqlite3StrICm
32c8a 70 28 7a 4c 65 66 74 2c 20 22 68 65 78 6b 65 79  p(zLeft, "hexkey
32c8b 22 29 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20 20  ")==0 ||.       
32c8c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
32c8d 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
32c8e 22 68 65 78 72 65 6b 65 79 22 29 3d 3d 30 29 20  "hexrekey")==0) 
32c8f 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 68 31  ){.    int i, h1
32c90 2c 20 68 32 3b 0a 20 20 20 20 63 68 61 72 20 7a  , h2;.    char z
32c91 4b 65 79 5b 34 30 5d 3b 0a 20 20 20 20 66 6f 72  Key[40];.    for
32c92 28 69 3d 30 3b 20 28 68 31 20 3d 20 7a 52 69 67  (i=0; (h1 = zRig
32c93 68 74 5b 69 5d 29 21 3d 30 20 26 26 20 28 68 32  ht[i])!=0 && (h2
32c94 20 3d 20 7a 52 69 67 68 74 5b 69 2b 31 5d 29 21   = zRight[i+1])!
32c95 3d 30 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20  =0; i+=2){.     
32c96 20 68 31 20 2b 3d 20 39 2a 28 31 26 28 68 31 3e   h1 += 9*(1&(h1>
32c97 3e 36 29 29 3b 0a 20 20 20 20 20 20 68 32 20 2b  >6));.      h2 +
32c98 3d 20 39 2a 28 31 26 28 68 32 3e 3e 36 29 29 3b  = 9*(1&(h2>>6));
32c99 0a 20 20 20 20 20 20 7a 4b 65 79 5b 69 2f 32 5d  .      zKey[i/2]
32c9a 20 3d 20 28 68 32 20 26 20 30 78 30 66 29 20 7c   = (h2 & 0x0f) |
32c9b 20 28 28 68 31 20 26 20 30 78 66 29 3c 3c 34 29   ((h1 & 0xf)<<4)
32c9c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
32c9d 28 7a 4c 65 66 74 5b 33 5d 20 26 20 30 78 66 29  (zLeft[3] & 0xf)
32c9e 3d 3d 30 78 62 20 29 7b 0a 20 20 20 20 20 20 73  ==0xb ){.      s
32c9f 71 6c 69 74 65 33 5f 6b 65 79 28 64 62 2c 20 7a  qlite3_key(db, z
32ca0 4b 65 79 2c 20 69 2f 32 29 3b 0a 20 20 20 20 7d  Key, i/2);.    }
32ca1 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
32ca2 74 65 33 5f 72 65 6b 65 79 28 64 62 2c 20 7a 4b  te3_rekey(db, zK
32ca3 65 79 2c 20 69 2f 32 29 3b 0a 20 20 20 20 7d 0a  ey, i/2);.    }.
32ca4 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 23    }else.#endif.#
32ca5 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
32ca6 45 5f 48 41 53 5f 43 4f 44 45 43 29 20 7c 7c 20  E_HAS_CODEC) || 
32ca7 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
32ca8 4e 41 42 4c 45 5f 43 45 52 4f 44 29 0a 20 20 69  NABLE_CEROD).  i
32ca9 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
32caa 70 28 7a 4c 65 66 74 2c 20 22 61 63 74 69 76 61  p(zLeft, "activa
32cab 74 65 5f 65 78 74 65 6e 73 69 6f 6e 73 22 29 3d  te_extensions")=
32cac 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  =0 ){.#ifdef SQL
32cad 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20  ITE_HAS_CODEC.  
32cae 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
32caf 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 22 73  NICmp(zRight, "s
32cb0 65 65 2d 22 2c 20 34 29 3d 3d 30 20 29 7b 0a 20  ee-", 4)==0 ){. 
32cb1 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 63 74       sqlite3_act
32cb2 69 76 61 74 65 5f 73 65 65 28 26 7a 52 69 67 68  ivate_see(&zRigh
32cb3 74 5b 34 5d 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  t[4]);.    }.#en
32cb4 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
32cb5 45 5f 45 4e 41 42 4c 45 5f 43 45 52 4f 44 0a 20  E_ENABLE_CEROD. 
32cb6 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
32cb7 72 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 22  rNICmp(zRight, "
32cb8 63 65 72 6f 64 2d 22 2c 20 36 29 3d 3d 30 20 29  cerod-", 6)==0 )
32cb9 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
32cba 61 63 74 69 76 61 74 65 5f 63 65 72 6f 64 28 26  activate_cerod(&
32cbb 7a 52 69 67 68 74 5b 36 5d 29 3b 0a 20 20 20 20  zRight[6]);.    
32cbc 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65  }.#endif.  }else
32cbd 0a 23 65 6e 64 69 66 0a 0a 20 0a 20 20 7b 2f 2a  .#endif.. .  {/*
32cbe 20 45 6d 70 74 79 20 45 4c 53 45 20 63 6c 61 75   Empty ELSE clau
32cbf 73 65 20 2a 2f 7d 0a 0a 20 20 2f 2a 0a 20 20 2a  se */}..  /*.  *
32cc0 2a 20 52 65 73 65 74 20 74 68 65 20 73 61 66 65  * Reset the safe
32cc1 74 79 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73  ty level, in cas
32cc2 65 20 74 68 65 20 66 75 6c 6c 66 73 79 6e 63 20  e the fullfsync 
32cc3 66 6c 61 67 20 6f 72 20 73 79 6e 63 68 72 6f 6e  flag or synchron
32cc4 6f 75 73 0a 20 20 2a 2a 20 73 65 74 74 69 6e 67  ous.  ** setting
32cc5 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 23   changed..  */.#
32cc6 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
32cc7 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
32cc8 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43  .  if( db->autoC
32cc9 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c  ommit ){.    sql
32cca 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65  ite3BtreeSetSafe
32ccb 74 79 4c 65 76 65 6c 28 70 44 62 2d 3e 70 42 74  tyLevel(pDb->pBt
32ccc 2c 20 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65  , pDb->safety_le
32ccd 76 65 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20  vel,.           
32cce 20 20 20 20 28 64 62 2d 3e 66 6c 61 67 73 26 53      (db->flags&S
32ccf 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 29  QLITE_FullFSync)
32cd0 21 3d 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  !=0,.           
32cd1 20 20 20 20 28 64 62 2d 3e 66 6c 61 67 73 26 53      (db->flags&S
32cd2 51 4c 49 54 45 5f 43 6b 70 74 46 75 6c 6c 46 53  QLITE_CkptFullFS
32cd3 79 6e 63 29 21 3d 30 29 3b 0a 20 20 7d 0a 23 65  ync)!=0);.  }.#e
32cd4 6e 64 69 66 0a 70 72 61 67 6d 61 5f 6f 75 74 3a  ndif.pragma_out:
32cd5 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
32cd6 28 64 62 2c 20 7a 4c 65 66 74 29 3b 0a 20 20 73  (db, zLeft);.  s
32cd7 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
32cd8 20 7a 52 69 67 68 74 29 3b 0a 7d 0a 0a 23 65 6e   zRight);.}..#en
32cd9 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
32cda 49 54 5f 50 52 41 47 4d 41 20 2a 2f 0a 0a 2f 2a  IT_PRAGMA */../*
32cdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
32cdc 64 20 6f 66 20 70 72 61 67 6d 61 2e 63 20 2a 2a  d of pragma.c **
32cdd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32cde 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32cdf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
32ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
32ce1 67 69 6e 20 66 69 6c 65 20 70 72 65 70 61 72 65  gin file prepare
32ce2 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
32ce3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32ce4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
32ce5 0a 2a 2a 20 32 30 30 35 20 4d 61 79 20 32 35 0a  .** 2005 May 25.
32ce6 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
32ce7 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
32ce8 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
32ce9 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
32cea 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
32ceb 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
32cec 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
32ced 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
32cee 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
32cef 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
32cf0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
32cf1 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
32cf2 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
32cf3 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
32cf4 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
32cf5 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
32cf6 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
32cf7 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
32cf8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32cf9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32cfa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32cfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
32cfc 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
32cfd 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  ains the impleme
32cfe 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
32cff 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
32d00 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c 20 61  .** interface, a
32d01 6e 64 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  nd routines that
32d02 20 63 6f 6e 74 72 69 62 75 74 65 20 74 6f 20 6c   contribute to l
32d03 6f 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62  oading the datab
32d04 61 73 65 20 73 63 68 65 6d 61 0a 2a 2a 20 66 72  ase schema.** fr
32d05 6f 6d 20 64 69 73 6b 2e 0a 2a 2f 0a 0a 2f 2a 0a  om disk..*/../*.
32d06 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e 69 74  ** Fill the Init
32d07 44 61 74 61 20 73 74 72 75 63 74 75 72 65 20 77  Data structure w
32d08 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ith an error mes
32d09 73 61 67 65 20 74 68 61 74 20 69 6e 64 69 63 61  sage that indica
32d0a 74 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  tes.** that the 
32d0b 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 72  database is corr
32d0c 75 70 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  upt..*/.static v
32d0d 6f 69 64 20 63 6f 72 72 75 70 74 53 63 68 65 6d  oid corruptSchem
32d0e 61 28 0a 20 20 49 6e 69 74 44 61 74 61 20 2a 70  a(.  InitData *p
32d0f 44 61 74 61 2c 20 20 20 20 20 2f 2a 20 49 6e 69  Data,     /* Ini
32d10 74 69 61 6c 69 7a 61 74 69 6f 6e 20 63 6f 6e 74  tialization cont
32d11 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ext */.  const c
32d12 68 61 72 20 2a 7a 4f 62 6a 2c 20 20 20 20 2f 2a  har *zObj,    /*
32d13 20 4f 62 6a 65 63 74 20 62 65 69 6e 67 20 70 61   Object being pa
32d14 72 73 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e  rsed at the poin
32d15 74 20 6f 66 20 65 72 72 6f 72 20 2a 2f 0a 20 20  t of error */.  
32d16 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 78 74  const char *zExt
32d17 72 61 20 20 20 2f 2a 20 45 72 72 6f 72 20 69 6e  ra   /* Error in
32d18 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 29 7b 0a  formation */.){.
32d19 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
32d1a 70 44 61 74 61 2d 3e 64 62 3b 0a 20 20 69 66 28  pData->db;.  if(
32d1b 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
32d1c 65 64 20 26 26 20 28 64 62 2d 3e 66 6c 61 67 73  ed && (db->flags
32d1d 20 26 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 65   & SQLITE_Recove
32d1e 72 79 4d 6f 64 65 29 3d 3d 30 20 29 7b 0a 20 20  ryMode)==0 ){.  
32d1f 20 20 69 66 28 20 7a 4f 62 6a 3d 3d 30 20 29 20    if( zObj==0 ) 
32d20 7a 4f 62 6a 20 3d 20 22 3f 22 3b 0a 20 20 20 20  zObj = "?";.    
32d21 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
32d22 28 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67  (pData->pzErrMsg
32d23 2c 20 64 62 2c 0a 20 20 20 20 20 20 22 6d 61 6c  , db,.      "mal
32d24 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 20  formed database 
32d25 73 63 68 65 6d 61 20 28 25 73 29 22 2c 20 7a 4f  schema (%s)", zO
32d26 62 6a 29 3b 0a 20 20 20 20 69 66 28 20 7a 45 78  bj);.    if( zEx
32d27 74 72 61 20 29 7b 0a 20 20 20 20 20 20 2a 70 44  tra ){.      *pD
32d28 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 20 3d 20  ata->pzErrMsg = 
32d29 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28  sqlite3MAppendf(
32d2a 64 62 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72  db, *pData->pzEr
32d2b 72 4d 73 67 2c 20 0a 20 20 20 20 20 20 20 20 20  rMsg, .         
32d2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32d2d 20 20 20 20 20 20 20 20 22 25 73 20 2d 20 25 73          "%s - %s
32d2e 22 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72 72  ", *pData->pzErr
32d2f 4d 73 67 2c 20 7a 45 78 74 72 61 29 3b 0a 20 20  Msg, zExtra);.  
32d30 20 20 7d 0a 20 20 7d 0a 20 20 70 44 61 74 61 2d    }.  }.  pData-
32d31 3e 72 63 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63  >rc = db->malloc
32d32 46 61 69 6c 65 64 20 3f 20 53 51 4c 49 54 45 5f  Failed ? SQLITE_
32d33 4e 4f 4d 45 4d 20 3a 20 53 51 4c 49 54 45 5f 43  NOMEM : SQLITE_C
32d34 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 7d 0a 0a  ORRUPT_BKPT;.}..
32d35 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  /*.** This is th
32d36 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69  e callback routi
32d37 6e 65 20 66 6f 72 20 74 68 65 20 63 6f 64 65 20  ne for the code 
32d38 74 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65 73  that initializes
32d39 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
32d3a 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33 49 6e  .  See sqlite3In
32d3b 69 74 28 29 20 62 65 6c 6f 77 20 66 6f 72 20 61  it() below for a
32d3c 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
32d3d 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72  ation..** This r
32d3e 6f 75 74 69 6e 65 20 69 73 20 61 6c 73 6f 20 63  outine is also c
32d3f 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 4f  alled from the O
32d40 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20 6f 70  P_ParseSchema op
32d41 63 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45  code of the VDBE
32d42 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c  ..**.** Each cal
32d43 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74  lback contains t
32d44 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66  he following inf
32d45 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20  ormation:.**.** 
32d46 20 20 20 20 61 72 67 76 5b 30 5d 20 3d 20 6e 61      argv[0] = na
32d47 6d 65 20 6f 66 20 74 68 69 6e 67 20 62 65 69 6e  me of thing bein
32d48 67 20 63 72 65 61 74 65 64 0a 2a 2a 20 20 20 20  g created.**    
32d49 20 61 72 67 76 5b 31 5d 20 3d 20 72 6f 6f 74 20   argv[1] = root 
32d4a 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20  page number for 
32d4b 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20  table or index. 
32d4c 30 20 66 6f 72 20 74 72 69 67 67 65 72 20 6f 72  0 for trigger or
32d4d 20 76 69 65 77 2e 0a 2a 2a 20 20 20 20 20 61 72   view..**     ar
32d4e 67 76 5b 32 5d 20 3d 20 53 51 4c 20 74 65 78 74  gv[2] = SQL text
32d4f 20 66 6f 72 20 74 68 65 20 43 52 45 41 54 45 20   for the CREATE 
32d50 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2f  statement..**.*/
32d51 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
32d52 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 43  int sqlite3InitC
32d53 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 2a 70 49  allback(void *pI
32d54 6e 69 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 63  nit, int argc, c
32d55 68 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72  har **argv, char
32d56 20 2a 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 49   **NotUsed){.  I
32d57 6e 69 74 44 61 74 61 20 2a 70 44 61 74 61 20 3d  nitData *pData =
32d58 20 28 49 6e 69 74 44 61 74 61 2a 29 70 49 6e 69   (InitData*)pIni
32d59 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t;.  sqlite3 *db
32d5a 20 3d 20 70 44 61 74 61 2d 3e 64 62 3b 0a 20 20   = pData->db;.  
32d5b 69 6e 74 20 69 44 62 20 3d 20 70 44 61 74 61 2d  int iDb = pData-
32d5c 3e 69 44 62 3b 0a 0a 20 20 61 73 73 65 72 74 28  >iDb;..  assert(
32d5d 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20 55 4e   argc==3 );.  UN
32d5e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
32d5f 4e 6f 74 55 73 65 64 2c 20 61 72 67 63 29 3b 0a  NotUsed, argc);.
32d60 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
32d61 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
32d62 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 44 62 43  >mutex) );.  DbC
32d63 6c 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c  learProperty(db,
32d64 20 69 44 62 2c 20 44 42 5f 45 6d 70 74 79 29 3b   iDb, DB_Empty);
32d65 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
32d66 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 63  cFailed ){.    c
32d67 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70 44 61  orruptSchema(pDa
32d68 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20 30 29 3b  ta, argv[0], 0);
32d69 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
32d6a 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44   }..  assert( iD
32d6b 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e  b>=0 && iDb<db->
32d6c 6e 44 62 20 29 3b 0a 20 20 69 66 28 20 61 72 67  nDb );.  if( arg
32d6d 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  v==0 ) return 0;
32d6e 20 20 20 2f 2a 20 4d 69 67 68 74 20 68 61 70 70     /* Might happ
32d6f 65 6e 20 69 66 20 45 4d 50 54 59 5f 52 45 53 55  en if EMPTY_RESU
32d70 4c 54 5f 43 41 4c 4c 42 41 43 4b 53 20 61 72 65  LT_CALLBACKS are
32d71 20 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 61 72 67   on */.  if( arg
32d72 76 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 63  v[1]==0 ){.    c
32d73 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70 44 61  orruptSchema(pDa
32d74 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20 30 29 3b  ta, argv[0], 0);
32d75 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 61 72 67  .  }else if( arg
32d76 76 5b 32 5d 20 26 26 20 61 72 67 76 5b 32 5d 5b  v[2] && argv[2][
32d77 30 5d 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 6c  0] ){.    /* Cal
32d78 6c 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20  l the parser to 
32d79 70 72 6f 63 65 73 73 20 61 20 43 52 45 41 54 45  process a CREATE
32d7a 20 54 41 42 4c 45 2c 20 49 4e 44 45 58 20 6f 72   TABLE, INDEX or
32d7b 20 56 49 45 57 2e 0a 20 20 20 20 2a 2a 20 42 75   VIEW..    ** Bu
32d7c 74 20 62 65 63 61 75 73 65 20 64 62 2d 3e 69 6e  t because db->in
32d7d 69 74 2e 62 75 73 79 20 69 73 20 73 65 74 20 74  it.busy is set t
32d7e 6f 20 31 2c 20 6e 6f 20 56 44 42 45 20 63 6f 64  o 1, no VDBE cod
32d7f 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 0a 20  e is generated. 
32d80 20 20 20 2a 2a 20 6f 72 20 65 78 65 63 75 74 65     ** or execute
32d81 64 2e 20 20 41 6c 6c 20 74 68 65 20 70 61 72 73  d.  All the pars
32d82 65 72 20 64 6f 65 73 20 69 73 20 62 75 69 6c 64  er does is build
32d83 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 64 61   the internal da
32d84 74 61 0a 20 20 20 20 2a 2a 20 73 74 72 75 63 74  ta.    ** struct
32d85 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72 69  ures that descri
32d86 62 65 20 74 68 65 20 74 61 62 6c 65 2c 20 69 6e  be the table, in
32d87 64 65 78 2c 20 6f 72 20 76 69 65 77 2e 0a 20 20  dex, or view..  
32d88 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 3b    */.    int rc;
32d89 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  .    sqlite3_stm
32d8a 74 20 2a 70 53 74 6d 74 3b 0a 20 20 20 20 54 45  t *pStmt;.    TE
32d8b 53 54 4f 4e 4c 59 28 69 6e 74 20 72 63 70 29 3b  STONLY(int rcp);
32d8c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
32d8d 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
32d8e 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
32d8f 29 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74  ) */..    assert
32d90 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20  ( db->init.busy 
32d91 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e  );.    db->init.
32d92 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 64  iDb = iDb;.    d
32d93 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 20  b->init.newTnum 
32d94 3d 20 73 71 6c 69 74 65 33 41 74 6f 69 28 61 72  = sqlite3Atoi(ar
32d95 67 76 5b 31 5d 29 3b 0a 20 20 20 20 64 62 2d 3e  gv[1]);.    db->
32d96 69 6e 69 74 2e 6f 72 70 68 61 6e 54 72 69 67 67  init.orphanTrigg
32d97 65 72 20 3d 20 30 3b 0a 20 20 20 20 54 45 53 54  er = 0;.    TEST
32d98 4f 4e 4c 59 28 72 63 70 20 3d 20 29 20 73 71 6c  ONLY(rcp = ) sql
32d99 69 74 65 33 5f 70 72 65 70 61 72 65 28 64 62 2c  ite3_prepare(db,
32d9a 20 61 72 67 76 5b 32 5d 2c 20 2d 31 2c 20 26 70   argv[2], -1, &p
32d9b 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20 72 63  Stmt, 0);.    rc
32d9c 20 3d 20 64 62 2d 3e 65 72 72 43 6f 64 65 3b 0a   = db->errCode;.
32d9d 20 20 20 20 61 73 73 65 72 74 28 20 28 72 63 26      assert( (rc&
32d9e 30 78 46 46 29 3d 3d 28 72 63 70 26 30 78 46 46  0xFF)==(rcp&0xFF
32d9f 29 20 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69  ) );.    db->ini
32da0 74 2e 69 44 62 20 3d 20 30 3b 0a 20 20 20 20 69  t.iDb = 0;.    i
32da1 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63  f( SQLITE_OK!=rc
32da2 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62   ){.      if( db
32da3 2d 3e 69 6e 69 74 2e 6f 72 70 68 61 6e 54 72 69  ->init.orphanTri
32da4 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20  gger ){.        
32da5 61 73 73 65 72 74 28 20 69 44 62 3d 3d 31 20 29  assert( iDb==1 )
32da6 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
32da7 20 20 20 20 20 20 20 70 44 61 74 61 2d 3e 72 63         pData->rc
32da8 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 20 20 69   = rc;.        i
32da9 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
32daa 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  MEM ){.         
32dab 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
32dac 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  d = 1;.        }
32dad 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c  else if( rc!=SQL
32dae 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 26 26  ITE_INTERRUPT &&
32daf 20 28 72 63 26 30 78 46 46 29 21 3d 53 51 4c 49   (rc&0xFF)!=SQLI
32db0 54 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 20  TE_LOCKED ){.   
32db1 20 20 20 20 20 20 20 63 6f 72 72 75 70 74 53 63         corruptSc
32db2 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67 76  hema(pData, argv
32db3 5b 30 5d 2c 20 73 71 6c 69 74 65 33 5f 65 72 72  [0], sqlite3_err
32db4 6d 73 67 28 64 62 29 29 3b 0a 20 20 20 20 20 20  msg(db));.      
32db5 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
32db6 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  }.    sqlite3_fi
32db7 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20  nalize(pStmt);. 
32db8 20 7d 65 6c 73 65 20 69 66 28 20 61 72 67 76 5b   }else if( argv[
32db9 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 72  0]==0 ){.    cor
32dba 72 75 70 74 53 63 68 65 6d 61 28 70 44 61 74 61  ruptSchema(pData
32dbb 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  , 0, 0);.  }else
32dbc 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  {.    /* If the 
32dbd 53 51 4c 20 63 6f 6c 75 6d 6e 20 69 73 20 62 6c  SQL column is bl
32dbe 61 6e 6b 20 69 74 20 6d 65 61 6e 73 20 74 68 69  ank it means thi
32dbf 73 20 69 73 20 61 6e 20 69 6e 64 65 78 20 74 68  s is an index th
32dc0 61 74 0a 20 20 20 20 2a 2a 20 77 61 73 20 63 72  at.    ** was cr
32dc1 65 61 74 65 64 20 74 6f 20 62 65 20 74 68 65 20  eated to be the 
32dc2 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 74  PRIMARY KEY or t
32dc3 6f 20 66 75 6c 66 69 6c 6c 20 61 20 55 4e 49 51  o fulfill a UNIQ
32dc4 55 45 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72  UE.    ** constr
32dc5 61 69 6e 74 20 66 6f 72 20 61 20 43 52 45 41 54  aint for a CREAT
32dc6 45 20 54 41 42 4c 45 2e 20 20 54 68 65 20 69 6e  E TABLE.  The in
32dc7 64 65 78 20 73 68 6f 75 6c 64 20 68 61 76 65 20  dex should have 
32dc8 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 62  already.    ** b
32dc9 65 65 6e 20 63 72 65 61 74 65 64 20 77 68 65 6e  een created when
32dca 20 77 65 20 70 72 6f 63 65 73 73 65 64 20 74 68   we processed th
32dcb 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 2e 20  e CREATE TABLE. 
32dcc 20 41 6c 6c 20 77 65 20 68 61 76 65 0a 20 20 20   All we have.   
32dcd 20 2a 2a 20 74 6f 20 64 6f 20 68 65 72 65 20 69   ** to do here i
32dce 73 20 72 65 63 6f 72 64 20 74 68 65 20 72 6f 6f  s record the roo
32dcf 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  t page number fo
32dd0 72 20 74 68 61 74 20 69 6e 64 65 78 2e 0a 20 20  r that index..  
32dd1 20 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a    */.    Index *
32dd2 70 49 6e 64 65 78 3b 0a 20 20 20 20 70 49 6e 64  pIndex;.    pInd
32dd3 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ex = sqlite3Find
32dd4 49 6e 64 65 78 28 64 62 2c 20 61 72 67 76 5b 30  Index(db, argv[0
32dd5 5d 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  ], db->aDb[iDb].
32dd6 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  zName);.    if( 
32dd7 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20  pIndex==0 ){.   
32dd8 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6e 20 6f     /* This can o
32dd9 63 63 75 72 20 69 66 20 74 68 65 72 65 20 65 78  ccur if there ex
32dda 69 73 74 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e  ists an index on
32ddb 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 77 68   a TEMP table wh
32ddc 69 63 68 0a 20 20 20 20 20 20 2a 2a 20 68 61 73  ich.      ** has
32ddd 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 61   the same name a
32dde 73 20 61 6e 6f 74 68 65 72 20 69 6e 64 65 78 20  s another index 
32ddf 6f 6e 20 61 20 70 65 72 6d 61 6e 65 6e 74 20 69  on a permanent i
32de0 6e 64 65 78 2e 20 20 53 69 6e 63 65 0a 20 20 20  ndex.  Since.   
32de1 20 20 20 2a 2a 20 74 68 65 20 70 65 72 6d 61 6e     ** the perman
32de2 65 6e 74 20 74 61 62 6c 65 20 69 73 20 68 69 64  ent table is hid
32de3 64 65 6e 20 62 79 20 74 68 65 20 54 45 4d 50 20  den by the TEMP 
32de4 74 61 62 6c 65 2c 20 77 65 20 63 61 6e 20 61 6c  table, we can al
32de5 73 6f 0a 20 20 20 20 20 20 2a 2a 20 73 61 66 65  so.      ** safe
32de6 6c 79 20 69 67 6e 6f 72 65 20 74 68 65 20 69 6e  ly ignore the in
32de7 64 65 78 20 6f 6e 20 74 68 65 20 70 65 72 6d 61  dex on the perma
32de8 6e 65 6e 74 20 74 61 62 6c 65 2e 0a 20 20 20 20  nent table..    
32de9 20 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 44 6f    */.      /* Do
32dea 20 4e 6f 74 68 69 6e 67 20 2a 2f 3b 0a 20 20 20   Nothing */;.   
32deb 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74   }else if( sqlit
32dec 65 33 47 65 74 49 6e 74 33 32 28 61 72 67 76 5b  e3GetInt32(argv[
32ded 31 5d 2c 20 26 70 49 6e 64 65 78 2d 3e 74 6e 75  1], &pIndex->tnu
32dee 6d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 63  m)==0 ){.      c
32def 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70 44 61  orruptSchema(pDa
32df0 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20 22 69 6e  ta, argv[0], "in
32df1 76 61 6c 69 64 20 72 6f 6f 74 70 61 67 65 22 29  valid rootpage")
32df2 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
32df3 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
32df4 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64   Attempt to read
32df5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
32df6 68 65 6d 61 20 61 6e 64 20 69 6e 69 74 69 61 6c  hema and initial
32df7 69 7a 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20  ize internal.** 
32df8 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
32df9 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74  for a single dat
32dfa 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65  abase file.  The
32dfb 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a   index of the.**
32dfc 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
32dfd 73 20 67 69 76 65 6e 20 62 79 20 69 44 62 2e 20  s given by iDb. 
32dfe 20 69 44 62 3d 3d 30 20 69 73 20 75 73 65 64 20   iDb==0 is used 
32dff 66 6f 72 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20  for the main.** 
32e00 64 61 74 61 62 61 73 65 2e 20 20 69 44 62 3d 3d  database.  iDb==
32e01 31 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  1 should never b
32e02 65 20 75 73 65 64 2e 20 20 69 44 62 3e 3d 32 20  e used.  iDb>=2 
32e03 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 61  is used for.** a
32e04 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
32e05 65 73 2e 20 20 52 65 74 75 72 6e 20 6f 6e 65 20  es.  Return one 
32e06 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 20 65  of the SQLITE_ e
32e07 72 72 6f 72 20 63 6f 64 65 73 20 74 6f 0a 2a 2a  rror codes to.**
32e08 20 69 6e 64 69 63 61 74 65 20 73 75 63 63 65 73   indicate succes
32e09 73 20 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a 2f  s or failure..*/
32e0a 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
32e0b 74 65 33 49 6e 69 74 4f 6e 65 28 73 71 6c 69 74  te3InitOne(sqlit
32e0c 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c  e3 *db, int iDb,
32e0d 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
32e0e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  ){.  int rc;.  i
32e0f 6e 74 20 69 3b 0a 23 69 66 6e 64 65 66 20 53 51  nt i;.#ifndef SQ
32e10 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
32e11 41 54 45 44 0a 20 20 69 6e 74 20 73 69 7a 65 3b  ATED.  int size;
32e12 0a 23 65 6e 64 69 66 0a 20 20 54 61 62 6c 65 20  .#endif.  Table 
32e13 2a 70 54 61 62 3b 0a 20 20 44 62 20 2a 70 44 62  *pTab;.  Db *pDb
32e14 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ;.  char const *
32e15 61 7a 41 72 67 5b 34 5d 3b 0a 20 20 69 6e 74 20  azArg[4];.  int 
32e16 6d 65 74 61 5b 35 5d 3b 0a 20 20 49 6e 69 74 44  meta[5];.  InitD
32e17 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 20 20  ata initData;.  
32e18 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 73  char const *zMas
32e19 74 65 72 53 63 68 65 6d 61 3b 0a 20 20 63 68 61  terSchema;.  cha
32e1a 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65 72  r const *zMaster
32e1b 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6f 70 65 6e  Name;.  int open
32e1c 65 64 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20  edTransaction = 
32e1d 30 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68  0;..  /*.  ** Th
32e1e 65 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73  e master databas
32e1f 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 73 74  e table has a st
32e20 72 75 63 74 75 72 65 20 6c 69 6b 65 20 74 68 69  ructure like thi
32e21 73 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  s.  */.  static 
32e22 63 6f 6e 73 74 20 63 68 61 72 20 6d 61 73 74 65  const char maste
32e23 72 5f 73 63 68 65 6d 61 5b 5d 20 3d 20 0a 20 20  r_schema[] = .  
32e24 20 20 20 22 43 52 45 41 54 45 20 54 41 42 4c 45     "CREATE TABLE
32e25 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 28 5c   sqlite_master(\
32e26 6e 22 0a 20 20 20 20 20 22 20 20 74 79 70 65 20  n".     "  type 
32e27 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20  text,\n".     " 
32e28 20 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20   name text,\n". 
32e29 20 20 20 20 22 20 20 74 62 6c 5f 6e 61 6d 65 20      "  tbl_name 
32e2a 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20  text,\n".     " 
32e2b 20 72 6f 6f 74 70 61 67 65 20 69 6e 74 65 67 65   rootpage intege
32e2c 72 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 73 71  r,\n".     "  sq
32e2d 6c 20 74 65 78 74 5c 6e 22 0a 20 20 20 20 20 22  l text\n".     "
32e2e 29 22 0a 20 20 3b 0a 23 69 66 6e 64 65 66 20 53  )".  ;.#ifndef S
32e2f 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44  QLITE_OMIT_TEMPD
32e30 42 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  B.  static const
32e31 20 63 68 61 72 20 74 65 6d 70 5f 6d 61 73 74 65   char temp_maste
32e32 72 5f 73 63 68 65 6d 61 5b 5d 20 3d 20 0a 20 20  r_schema[] = .  
32e33 20 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 20     "CREATE TEMP 
32e34 54 41 42 4c 45 20 73 71 6c 69 74 65 5f 74 65 6d  TABLE sqlite_tem
32e35 70 5f 6d 61 73 74 65 72 28 5c 6e 22 0a 20 20 20  p_master(\n".   
32e36 20 20 22 20 20 74 79 70 65 20 74 65 78 74 2c 5c    "  type text,\
32e37 6e 22 0a 20 20 20 20 20 22 20 20 6e 61 6d 65 20  n".     "  name 
32e38 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20  text,\n".     " 
32e39 20 74 62 6c 5f 6e 61 6d 65 20 74 65 78 74 2c 5c   tbl_name text,\
32e3a 6e 22 0a 20 20 20 20 20 22 20 20 72 6f 6f 74 70  n".     "  rootp
32e3b 61 67 65 20 69 6e 74 65 67 65 72 2c 5c 6e 22 0a  age integer,\n".
32e3c 20 20 20 20 20 22 20 20 73 71 6c 20 74 65 78 74       "  sql text
32e3d 5c 6e 22 0a 20 20 20 20 20 22 29 22 0a 20 20 3b  \n".     ")".  ;
32e3e 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
32e3f 20 74 65 6d 70 5f 6d 61 73 74 65 72 5f 73 63 68   temp_master_sch
32e40 65 6d 61 20 30 0a 23 65 6e 64 69 66 0a 0a 20 20  ema 0.#endif..  
32e41 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26  assert( iDb>=0 &
32e42 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b  & iDb<db->nDb );
32e43 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61  .  assert( db->a
32e44 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 20  Db[iDb].pSchema 
32e45 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
32e46 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
32e47 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
32e48 61 73 73 65 72 74 28 20 69 44 62 3d 3d 31 20 7c  assert( iDb==1 |
32e49 7c 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  | sqlite3BtreeHo
32e4a 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62  ldsMutex(db->aDb
32e4b 5b 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 0a 20  [iDb].pBt) );.. 
32e4c 20 2f 2a 20 7a 4d 61 73 74 65 72 53 63 68 65 6d   /* zMasterSchem
32e4d 61 20 61 6e 64 20 7a 49 6e 69 74 53 63 72 69 70  a and zInitScrip
32e4e 74 20 61 72 65 20 73 65 74 20 74 6f 20 70 6f 69  t are set to poi
32e4f 6e 74 20 61 74 20 74 68 65 20 6d 61 73 74 65 72  nt at the master
32e50 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20 61 6e 64   schema.  ** and
32e51 20 69 6e 69 74 69 61 6c 69 73 61 74 69 6f 6e 20   initialisation 
32e52 73 63 72 69 70 74 20 61 70 70 72 6f 70 72 69 61  script appropria
32e53 74 65 20 66 6f 72 20 74 68 65 20 64 61 74 61 62  te for the datab
32e54 61 73 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 69  ase being.  ** i
32e55 6e 69 74 69 61 6c 69 73 65 64 2e 20 7a 4d 61 73  nitialised. zMas
32e56 74 65 72 4e 61 6d 65 20 69 73 20 74 68 65 20 6e  terName is the n
32e57 61 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65  ame of the maste
32e58 72 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  r table..  */.  
32e59 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42  if( !OMIT_TEMPDB
32e5a 20 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20   && iDb==1 ){.  
32e5b 20 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 20    zMasterSchema 
32e5c 3d 20 74 65 6d 70 5f 6d 61 73 74 65 72 5f 73 63  = temp_master_sc
32e5d 68 65 6d 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  hema;.  }else{. 
32e5e 20 20 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61     zMasterSchema
32e5f 20 3d 20 6d 61 73 74 65 72 5f 73 63 68 65 6d 61   = master_schema
32e60 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 4e  ;.  }.  zMasterN
32e61 61 6d 65 20 3d 20 53 43 48 45 4d 41 5f 54 41 42  ame = SCHEMA_TAB
32e62 4c 45 28 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 43  LE(iDb);..  /* C
32e63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 73 63 68  onstruct the sch
32e64 65 6d 61 20 74 61 62 6c 65 73 2e 20 20 2a 2f 0a  ema tables.  */.
32e65 20 20 61 7a 41 72 67 5b 30 5d 20 3d 20 7a 4d 61    azArg[0] = zMa
32e66 73 74 65 72 4e 61 6d 65 3b 0a 20 20 61 7a 41 72  sterName;.  azAr
32e67 67 5b 31 5d 20 3d 20 22 31 22 3b 0a 20 20 61 7a  g[1] = "1";.  az
32e68 41 72 67 5b 32 5d 20 3d 20 7a 4d 61 73 74 65 72  Arg[2] = zMaster
32e69 53 63 68 65 6d 61 3b 0a 20 20 61 7a 41 72 67 5b  Schema;.  azArg[
32e6a 33 5d 20 3d 20 30 3b 0a 20 20 69 6e 69 74 44 61  3] = 0;.  initDa
32e6b 74 61 2e 64 62 20 3d 20 64 62 3b 0a 20 20 69 6e  ta.db = db;.  in
32e6c 69 74 44 61 74 61 2e 69 44 62 20 3d 20 69 44 62  itData.iDb = iDb
32e6d 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 72 63 20  ;.  initData.rc 
32e6e 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
32e6f 6e 69 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67  nitData.pzErrMsg
32e70 20 3d 20 70 7a 45 72 72 4d 73 67 3b 0a 20 20 73   = pzErrMsg;.  s
32e71 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61  qlite3InitCallba
32e72 63 6b 28 26 69 6e 69 74 44 61 74 61 2c 20 33 2c  ck(&initData, 3,
32e73 20 28 63 68 61 72 20 2a 2a 29 61 7a 41 72 67 2c   (char **)azArg,
32e74 20 30 29 3b 0a 20 20 69 66 28 20 69 6e 69 74 44   0);.  if( initD
32e75 61 74 61 2e 72 63 20 29 7b 0a 20 20 20 20 72 63  ata.rc ){.    rc
32e76 20 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a   = initData.rc;.
32e77 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f      goto error_o
32e78 75 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d  ut;.  }.  pTab =
32e79 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
32e7a 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 4e 61 6d  e(db, zMasterNam
32e7b 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  e, db->aDb[iDb].
32e7c 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 41 4c  zName);.  if( AL
32e7d 57 41 59 53 28 70 54 61 62 29 20 29 7b 0a 20 20  WAYS(pTab) ){.  
32e7e 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73    pTab->tabFlags
32e7f 20 7c 3d 20 54 46 5f 52 65 61 64 6f 6e 6c 79 3b   |= TF_Readonly;
32e80 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74  .  }..  /* Creat
32e81 65 20 61 20 63 75 72 73 6f 72 20 74 6f 20 68 6f  e a cursor to ho
32e82 6c 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ld the database 
32e83 6f 70 65 6e 0a 20 20 2a 2f 0a 20 20 70 44 62 20  open.  */.  pDb 
32e84 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b  = &db->aDb[iDb];
32e85 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d  .  if( pDb->pBt=
32e86 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 4f  =0 ){.    if( !O
32e87 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 41 4c  MIT_TEMPDB && AL
32e88 57 41 59 53 28 69 44 62 3d 3d 31 29 20 29 7b 0a  WAYS(iDb==1) ){.
32e89 20 20 20 20 20 20 44 62 53 65 74 50 72 6f 70 65        DbSetPrope
32e8a 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f 53 63  rty(db, 1, DB_Sc
32e8b 68 65 6d 61 4c 6f 61 64 65 64 29 3b 0a 20 20 20  hemaLoaded);.   
32e8c 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51   }.    return SQ
32e8d 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
32e8e 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  /* If there is n
32e8f 6f 74 20 61 6c 72 65 61 64 79 20 61 20 72 65 61  ot already a rea
32e90 64 2d 6f 6e 6c 79 20 28 6f 72 20 72 65 61 64 2d  d-only (or read-
32e91 77 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69  write) transacti
32e92 6f 6e 20 6f 70 65 6e 65 64 0a 20 20 2a 2a 20 6f  on opened.  ** o
32e93 6e 20 74 68 65 20 62 2d 74 72 65 65 20 64 61 74  n the b-tree dat
32e94 61 62 61 73 65 2c 20 6f 70 65 6e 20 6f 6e 65 20  abase, open one 
32e95 6e 6f 77 2e 20 49 66 20 61 20 74 72 61 6e 73 61  now. If a transa
32e96 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2c  ction is opened,
32e97 20 69 74 20 0a 20 20 2a 2a 20 77 69 6c 6c 20 62   it .  ** will b
32e98 65 20 63 6c 6f 73 65 64 20 62 65 66 6f 72 65 20  e closed before 
32e99 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  this function re
32e9a 74 75 72 6e 73 2e 20 20 2a 2f 0a 20 20 73 71 6c  turns.  */.  sql
32e9b 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
32e9c 44 62 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20  Db->pBt);.  if( 
32e9d 21 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49  !sqlite3BtreeIsI
32e9e 6e 52 65 61 64 54 72 61 6e 73 28 70 44 62 2d 3e  nReadTrans(pDb->
32e9f 70 42 74 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  pBt) ){.    rc =
32ea0 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
32ea1 69 6e 54 72 61 6e 73 28 70 44 62 2d 3e 70 42 74  inTrans(pDb->pBt
32ea2 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
32ea3 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
32ea4 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53       sqlite3SetS
32ea5 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20  tring(pzErrMsg, 
32ea6 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
32ea7 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20  3ErrStr(rc));.  
32ea8 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65      goto initone
32ea9 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20  _error_out;.    
32eaa 7d 0a 20 20 20 20 6f 70 65 6e 65 64 54 72 61 6e  }.    openedTran
32eab 73 61 63 74 69 6f 6e 20 3d 20 31 3b 0a 20 20 7d  saction = 1;.  }
32eac 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 64  ..  /* Get the d
32ead 61 74 61 62 61 73 65 20 6d 65 74 61 20 69 6e 66  atabase meta inf
32eae 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20  ormation..  **. 
32eaf 20 2a 2a 20 4d 65 74 61 20 76 61 6c 75 65 73 20   ** Meta values 
32eb0 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  are as follows:.
32eb1 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 30 5d 20    **    meta[0] 
32eb2 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e    Schema cookie.
32eb3 20 20 43 68 61 6e 67 65 73 20 77 69 74 68 20 65    Changes with e
32eb4 61 63 68 20 73 63 68 65 6d 61 20 63 68 61 6e 67  ach schema chang
32eb5 65 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  e..  **    meta[
32eb6 31 5d 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74  1]   File format
32eb7 20 6f 66 20 73 63 68 65 6d 61 20 6c 61 79 65 72   of schema layer
32eb8 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 32  ..  **    meta[2
32eb9 5d 20 20 20 53 69 7a 65 20 6f 66 20 74 68 65 20  ]   Size of the 
32eba 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20 2a 2a  page cache..  **
32ebb 20 20 20 20 6d 65 74 61 5b 33 5d 20 20 20 4c 61      meta[3]   La
32ebc 72 67 65 73 74 20 72 6f 6f 74 70 61 67 65 20 28  rgest rootpage (
32ebd 61 75 74 6f 2f 69 6e 63 72 5f 76 61 63 75 75 6d  auto/incr_vacuum
32ebe 20 6d 6f 64 65 29 0a 20 20 2a 2a 20 20 20 20 6d   mode).  **    m
32ebf 65 74 61 5b 34 5d 20 20 20 44 62 20 74 65 78 74  eta[4]   Db text
32ec0 20 65 6e 63 6f 64 69 6e 67 2e 20 31 3a 55 54 46   encoding. 1:UTF
32ec1 2d 38 20 32 3a 55 54 46 2d 31 36 4c 45 20 33 3a  -8 2:UTF-16LE 3:
32ec2 55 54 46 2d 31 36 42 45 0a 20 20 2a 2a 20 20 20  UTF-16BE.  **   
32ec3 20 6d 65 74 61 5b 35 5d 20 20 20 55 73 65 72 20   meta[5]   User 
32ec4 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20 20 20  version.  **    
32ec5 6d 65 74 61 5b 36 5d 20 20 20 49 6e 63 72 65 6d  meta[6]   Increm
32ec6 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 6d 6f 64  ental vacuum mod
32ec7 65 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 37  e.  **    meta[7
32ec8 5d 20 20 20 75 6e 75 73 65 64 0a 20 20 2a 2a 20  ]   unused.  ** 
32ec9 20 20 20 6d 65 74 61 5b 38 5d 20 20 20 75 6e 75     meta[8]   unu
32eca 73 65 64 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  sed.  **    meta
32ecb 5b 39 5d 20 20 20 75 6e 75 73 65 64 0a 20 20 2a  [9]   unused.  *
32ecc 2a 0a 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 65  *.  ** Note: The
32ecd 20 23 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45   #defined SQLITE
32ece 5f 55 54 46 2a 20 73 79 6d 62 6f 6c 73 20 69 6e  _UTF* symbols in
32ecf 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 63 6f 72   sqliteInt.h cor
32ed0 72 65 73 70 6f 6e 64 20 74 6f 0a 20 20 2a 2a 20  respond to.  ** 
32ed1 74 68 65 20 70 6f 73 73 69 62 6c 65 20 76 61 6c  the possible val
32ed2 75 65 73 20 6f 66 20 6d 65 74 61 5b 34 5d 2e 0a  ues of meta[4]..
32ed3 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
32ed4 69 3c 41 72 72 61 79 53 69 7a 65 28 6d 65 74 61  i<ArraySize(meta
32ed5 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  ); i++){.    sql
32ed6 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
32ed7 28 70 44 62 2d 3e 70 42 74 2c 20 69 2b 31 2c 20  (pDb->pBt, i+1, 
32ed8 28 75 33 32 20 2a 29 26 6d 65 74 61 5b 69 5d 29  (u32 *)&meta[i])
32ed9 3b 0a 20 20 7d 0a 20 20 70 44 62 2d 3e 70 53 63  ;.  }.  pDb->pSc
32eda 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
32edb 6b 69 65 20 3d 20 6d 65 74 61 5b 42 54 52 45 45  kie = meta[BTREE
32edc 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2d  _SCHEMA_VERSION-
32edd 31 5d 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 70 65  1];..  /* If ope
32ede 6e 69 6e 67 20 61 20 6e 6f 6e 2d 65 6d 70 74 79  ning a non-empty
32edf 20 64 61 74 61 62 61 73 65 2c 20 63 68 65 63 6b   database, check
32ee0 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69   the text encodi
32ee1 6e 67 2e 20 46 6f 72 20 74 68 65 0a 20 20 2a 2a  ng. For the.  **
32ee2 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20   main database, 
32ee3 73 65 74 20 73 71 6c 69 74 65 33 2e 65 6e 63 20  set sqlite3.enc 
32ee4 74 6f 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  to the encoding 
32ee5 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  of the main data
32ee6 62 61 73 65 2e 0a 20 20 2a 2a 20 46 6f 72 20 61  base..  ** For a
32ee7 6e 20 61 74 74 61 63 68 65 64 20 64 62 2c 20 69  n attached db, i
32ee8 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66  t is an error if
32ee9 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73   the encoding is
32eea 20 6e 6f 74 20 74 68 65 20 73 61 6d 65 0a 20 20   not the same.  
32eeb 2a 2a 20 61 73 20 73 71 6c 69 74 65 33 2e 65 6e  ** as sqlite3.en
32eec 63 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65  c..  */.  if( me
32eed 74 61 5b 42 54 52 45 45 5f 54 45 58 54 5f 45 4e  ta[BTREE_TEXT_EN
32eee 43 4f 44 49 4e 47 2d 31 5d 20 29 7b 20 20 2f 2a  CODING-1] ){  /*
32eef 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a   text encoding *
32ef0 2f 0a 20 20 20 20 69 66 28 20 69 44 62 3d 3d 30  /.    if( iDb==0
32ef1 20 29 7b 0a 20 20 20 20 20 20 75 38 20 65 6e 63   ){.      u8 enc
32ef2 6f 64 69 6e 67 3b 0a 20 20 20 20 20 20 2f 2a 20  oding;.      /* 
32ef3 49 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 6d  If opening the m
32ef4 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 73 65  ain database, se
32ef5 74 20 45 4e 43 28 64 62 29 2e 20 2a 2f 0a 20 20  t ENC(db). */.  
32ef6 20 20 20 20 65 6e 63 6f 64 69 6e 67 20 3d 20 28      encoding = (
32ef7 75 38 29 6d 65 74 61 5b 42 54 52 45 45 5f 54 45  u8)meta[BTREE_TE
32ef8 58 54 5f 45 4e 43 4f 44 49 4e 47 2d 31 5d 20 26  XT_ENCODING-1] &
32ef9 20 33 3b 0a 20 20 20 20 20 20 69 66 28 20 65 6e   3;.      if( en
32efa 63 6f 64 69 6e 67 3d 3d 30 20 29 20 65 6e 63 6f  coding==0 ) enco
32efb 64 69 6e 67 20 3d 20 53 51 4c 49 54 45 5f 55 54  ding = SQLITE_UT
32efc 46 38 3b 0a 20 20 20 20 20 20 45 4e 43 28 64 62  F8;.      ENC(db
32efd 29 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20  ) = encoding;.  
32efe 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
32eff 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20 61 6e 20  * If opening an 
32f00 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
32f01 65 2c 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  e, the encoding 
32f02 6d 75 63 68 20 6d 61 74 63 68 20 45 4e 43 28 64  much match ENC(d
32f03 62 29 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  b) */.      if( 
32f04 6d 65 74 61 5b 42 54 52 45 45 5f 54 45 58 54 5f  meta[BTREE_TEXT_
32f05 45 4e 43 4f 44 49 4e 47 2d 31 5d 21 3d 45 4e 43  ENCODING-1]!=ENC
32f06 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (db) ){.        
32f07 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
32f08 28 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  (pzErrMsg, db, "
32f09 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
32f0a 65 73 20 6d 75 73 74 20 75 73 65 20 74 68 65 20  es must use the 
32f0b 73 61 6d 65 22 0a 20 20 20 20 20 20 20 20 20 20  same".          
32f0c 20 20 22 20 74 65 78 74 20 65 6e 63 6f 64 69 6e    " text encodin
32f0d 67 20 61 73 20 6d 61 69 6e 20 64 61 74 61 62 61  g as main databa
32f0e 73 65 22 29 3b 0a 20 20 20 20 20 20 20 20 72 63  se");.        rc
32f0f 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
32f10 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69 6e  .        goto in
32f11 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3b  itone_error_out;
32f12 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
32f13 20 7d 65 6c 73 65 7b 0a 20 20 20 20 44 62 53 65   }else{.    DbSe
32f14 74 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44  tProperty(db, iD
32f15 62 2c 20 44 42 5f 45 6d 70 74 79 29 3b 0a 20 20  b, DB_Empty);.  
32f16 7d 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  }.  pDb->pSchema
32f17 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b  ->enc = ENC(db);
32f18 0a 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63  ..  if( pDb->pSc
32f19 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65  hema->cache_size
32f1a 3d 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 53  ==0 ){.#ifndef S
32f1b 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
32f1c 43 41 54 45 44 0a 20 20 20 20 73 69 7a 65 20 3d  CATED.    size =
32f1d 20 73 71 6c 69 74 65 33 41 62 73 49 6e 74 33 32   sqlite3AbsInt32
32f1e 28 6d 65 74 61 5b 42 54 52 45 45 5f 44 45 46 41  (meta[BTREE_DEFA
32f1f 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2d 31  ULT_CACHE_SIZE-1
32f20 5d 29 3b 0a 20 20 20 20 69 66 28 20 73 69 7a 65  ]);.    if( size
32f21 3d 3d 30 20 29 7b 20 73 69 7a 65 20 3d 20 53 51  ==0 ){ size = SQ
32f22 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  LITE_DEFAULT_CAC
32f23 48 45 5f 53 49 5a 45 3b 20 7d 0a 20 20 20 20 70  HE_SIZE; }.    p
32f24 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63  Db->pSchema->cac
32f25 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b 0a  he_size = size;.
32f26 23 65 6c 73 65 0a 20 20 20 20 70 44 62 2d 3e 70  #else.    pDb->p
32f27 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69  Schema->cache_si
32f28 7a 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41  ze = SQLITE_DEFA
32f29 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 3b 0a  ULT_CACHE_SIZE;.
32f2a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74  #endif.    sqlit
32f2b 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53  e3BtreeSetCacheS
32f2c 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44  ize(pDb->pBt, pD
32f2d 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68  b->pSchema->cach
32f2e 65 5f 73 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20  e_size);.  }..  
32f2f 2f 2a 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72  /*.  ** file_for
32f30 6d 61 74 3d 3d 31 20 20 20 20 56 65 72 73 69 6f  mat==1    Versio
32f31 6e 20 33 2e 30 2e 30 2e 0a 20 20 2a 2a 20 66 69  n 3.0.0..  ** fi
32f32 6c 65 5f 66 6f 72 6d 61 74 3d 3d 32 20 20 20 20  le_format==2    
32f33 56 65 72 73 69 6f 6e 20 33 2e 31 2e 33 2e 20 20  Version 3.1.3.  
32f34 2f 2f 20 41 4c 54 45 52 20 54 41 42 4c 45 20 41  // ALTER TABLE A
32f35 44 44 20 43 4f 4c 55 4d 4e 0a 20 20 2a 2a 20 66  DD COLUMN.  ** f
32f36 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 33 20 20 20  ile_format==3   
32f37 20 56 65 72 73 69 6f 6e 20 33 2e 31 2e 34 2e 20   Version 3.1.4. 
32f38 20 2f 2f 20 64 69 74 74 6f 20 62 75 74 20 77 69   // ditto but wi
32f39 74 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65 66 61  th non-NULL defa
32f3a 75 6c 74 73 0a 20 20 2a 2a 20 66 69 6c 65 5f 66  ults.  ** file_f
32f3b 6f 72 6d 61 74 3d 3d 34 20 20 20 20 56 65 72 73  ormat==4    Vers
32f3c 69 6f 6e 20 33 2e 33 2e 30 2e 20 20 2f 2f 20 44  ion 3.3.0.  // D
32f3d 45 53 43 20 69 6e 64 69 63 65 73 2e 20 20 42 6f  ESC indices.  Bo
32f3e 6f 6c 65 61 6e 20 63 6f 6e 73 74 61 6e 74 73 0a  olean constants.
32f3f 20 20 2a 2f 0a 20 20 70 44 62 2d 3e 70 53 63 68    */.  pDb->pSch
32f40 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74  ema->file_format
32f41 20 3d 20 28 75 38 29 6d 65 74 61 5b 42 54 52 45   = (u8)meta[BTRE
32f42 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2d 31 5d  E_FILE_FORMAT-1]
32f43 3b 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63  ;.  if( pDb->pSc
32f44 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
32f45 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 62 2d  t==0 ){.    pDb-
32f46 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66  >pSchema->file_f
32f47 6f 72 6d 61 74 20 3d 20 31 3b 0a 20 20 7d 0a 20  ormat = 1;.  }. 
32f48 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d   if( pDb->pSchem
32f49 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 53  a->file_format>S
32f4a 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46  QLITE_MAX_FILE_F
32f4b 4f 52 4d 41 54 20 29 7b 0a 20 20 20 20 73 71 6c  ORMAT ){.    sql
32f4c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a  ite3SetString(pz
32f4d 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 75 6e 73  ErrMsg, db, "uns
32f4e 75 70 70 6f 72 74 65 64 20 66 69 6c 65 20 66 6f  upported file fo
32f4f 72 6d 61 74 22 29 3b 0a 20 20 20 20 72 63 20 3d  rmat");.    rc =
32f50 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
32f51 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f     goto initone_
32f52 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  error_out;.  }..
32f53 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 32 38 30    /* Ticket #280
32f54 34 3a 20 20 57 68 65 6e 20 77 65 20 6f 70 65 6e  4:  When we open
32f55 20 61 20 64 61 74 61 62 61 73 65 20 69 6e 20 74   a database in t
32f56 68 65 20 6e 65 77 65 72 20 66 69 6c 65 20 66 6f  he newer file fo
32f57 72 6d 61 74 2c 0a 20 20 2a 2a 20 63 6c 65 61 72  rmat,.  ** clear
32f58 20 74 68 65 20 6c 65 67 61 63 79 5f 66 69 6c 65   the legacy_file
32f59 5f 66 6f 72 6d 61 74 20 70 72 61 67 6d 61 20 66  _format pragma f
32f5a 6c 61 67 20 73 6f 20 74 68 61 74 20 61 20 56 41  lag so that a VA
32f5b 43 55 55 4d 20 77 69 6c 6c 0a 20 20 2a 2a 20 6e  CUUM will.  ** n
32f5c 6f 74 20 64 6f 77 6e 67 72 61 64 65 20 74 68 65  ot downgrade the
32f5d 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68   database and th
32f5e 75 73 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e  us invalidate an
32f5f 79 20 64 65 73 63 65 6e 64 69 6e 67 0a 20 20 2a  y descending.  *
32f60 2a 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 74  * indices that t
32f61 68 65 20 75 73 65 72 20 6d 69 67 68 74 20 68 61  he user might ha
32f62 76 65 20 63 72 65 61 74 65 64 2e 0a 20 20 2a 2f  ve created..  */
32f63 0a 20 20 69 66 28 20 69 44 62 3d 3d 30 20 26 26  .  if( iDb==0 &&
32f64 20 6d 65 74 61 5b 42 54 52 45 45 5f 46 49 4c 45   meta[BTREE_FILE
32f65 5f 46 4f 52 4d 41 54 2d 31 5d 3e 3d 34 20 29 7b  _FORMAT-1]>=4 ){
32f66 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26  .    db->flags &
32f67 3d 20 7e 53 51 4c 49 54 45 5f 4c 65 67 61 63 79  = ~SQLITE_Legacy
32f68 46 69 6c 65 46 6d 74 3b 0a 20 20 7d 0a 0a 20 20  FileFmt;.  }..  
32f69 2f 2a 20 52 65 61 64 20 74 68 65 20 73 63 68 65  /* Read the sche
32f6a 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  ma information o
32f6b 75 74 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61  ut of the schema
32f6c 20 74 61 62 6c 65 73 0a 20 20 2a 2f 0a 20 20 61   tables.  */.  a
32f6d 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e  ssert( db->init.
32f6e 62 75 73 79 20 29 3b 0a 20 20 7b 0a 20 20 20 20  busy );.  {.    
32f6f 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20  char *zSql;.    
32f70 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50  zSql = sqlite3MP
32f71 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20  rintf(db, .     
32f72 20 20 20 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c     "SELECT name,
32f73 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c 20 46   rootpage, sql F
32f74 52 4f 4d 20 27 25 71 27 2e 25 73 20 4f 52 44 45  ROM '%q'.%s ORDE
32f75 52 20 42 59 20 72 6f 77 69 64 22 2c 0a 20 20 20  R BY rowid",.   
32f76 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
32f77 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61 73 74 65 72  ].zName, zMaster
32f78 4e 61 6d 65 29 3b 0a 23 69 66 6e 64 65 66 20 53  Name);.#ifndef S
32f79 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
32f7a 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 7b 0a 20  RIZATION.    {. 
32f7b 20 20 20 20 20 69 6e 74 20 28 2a 78 41 75 74 68       int (*xAuth
32f7c 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
32f7d 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
32f7e 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
32f7f 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20  const char*);.  
32f80 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e      xAuth = db->
32f81 78 41 75 74 68 3b 0a 20 20 20 20 20 20 64 62 2d  xAuth;.      db-
32f82 3e 78 41 75 74 68 20 3d 20 30 3b 0a 23 65 6e 64  >xAuth = 0;.#end
32f83 69 66 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  if.      rc = sq
32f84 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a  lite3_exec(db, z
32f85 53 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69 74  Sql, sqlite3Init
32f86 43 61 6c 6c 62 61 63 6b 2c 20 26 69 6e 69 74 44  Callback, &initD
32f87 61 74 61 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66  ata, 0);.#ifndef
32f88 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
32f89 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20  HORIZATION.     
32f8a 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 41 75   db->xAuth = xAu
32f8b 74 68 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  th;.    }.#endif
32f8c 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
32f8d 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 69 6e  ITE_OK ) rc = in
32f8e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 20 73  itData.rc;.    s
32f8f 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
32f90 20 7a 53 71 6c 29 3b 0a 23 69 66 6e 64 65 66 20   zSql);.#ifndef 
32f91 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c  SQLITE_OMIT_ANAL
32f92 59 5a 45 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  YZE.    if( rc==
32f93 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
32f94 20 20 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73     sqlite3Analys
32f95 69 73 4c 6f 61 64 28 64 62 2c 20 69 44 62 29 3b  isLoad(db, iDb);
32f96 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
32f97 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  }.  if( db->mall
32f98 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
32f99 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
32f9a 4d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  M;.    sqlite3Re
32f9b 73 65 74 41 6c 6c 53 63 68 65 6d 61 73 4f 66 43  setAllSchemasOfC
32f9c 6f 6e 6e 65 63 74 69 6f 6e 28 64 62 29 3b 0a 20  onnection(db);. 
32f9d 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
32f9e 49 54 45 5f 4f 4b 20 7c 7c 20 28 64 62 2d 3e 66  ITE_OK || (db->f
32f9f 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 63 6f  lags&SQLITE_Reco
32fa0 76 65 72 79 4d 6f 64 65 29 29 7b 0a 20 20 20 20  veryMode)){.    
32fa1 2f 2a 20 42 6c 61 63 6b 20 6d 61 67 69 63 3a 20  /* Black magic: 
32fa2 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 52 65  If the SQLITE_Re
32fa3 63 6f 76 65 72 79 4d 6f 64 65 20 66 6c 61 67 20  coveryMode flag 
32fa4 69 73 20 73 65 74 2c 20 74 68 65 6e 20 63 6f 6e  is set, then con
32fa5 73 69 64 65 72 0a 20 20 20 20 2a 2a 20 74 68 65  sider.    ** the
32fa6 20 73 63 68 65 6d 61 20 6c 6f 61 64 65 64 2c 20   schema loaded, 
32fa7 65 76 65 6e 20 69 66 20 65 72 72 6f 72 73 20 6f  even if errors o
32fa8 63 63 75 72 72 65 64 2e 20 49 6e 20 74 68 69 73  ccurred. In this
32fa9 20 73 69 74 75 61 74 69 6f 6e 20 74 68 65 20 0a   situation the .
32faa 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 73      ** current s
32fab 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
32fac 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20   operation will 
32fad 66 61 69 6c 2c 20 62 75 74 20 74 68 65 20 66 6f  fail, but the fo
32fae 6c 6c 6f 77 69 6e 67 20 6f 6e 65 0a 20 20 20 20  llowing one.    
32faf 2a 2a 20 77 69 6c 6c 20 61 74 74 65 6d 70 74 20  ** will attempt 
32fb0 74 6f 20 63 6f 6d 70 69 6c 65 20 74 68 65 20 73  to compile the s
32fb1 75 70 70 6c 69 65 64 20 73 74 61 74 65 6d 65 6e  upplied statemen
32fb2 74 20 61 67 61 69 6e 73 74 20 77 68 61 74 65 76  t against whatev
32fb3 65 72 20 73 75 62 73 65 74 0a 20 20 20 20 2a 2a  er subset.    **
32fb4 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 77   of the schema w
32fb5 61 73 20 6c 6f 61 64 65 64 20 62 65 66 6f 72 65  as loaded before
32fb6 20 74 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72   the error occur
32fb7 72 65 64 2e 20 54 68 65 20 70 72 69 6d 61 72 79  red. The primary
32fb8 0a 20 20 20 20 2a 2a 20 70 75 72 70 6f 73 65 20  .    ** purpose 
32fb9 6f 66 20 74 68 69 73 20 69 73 20 74 6f 20 61 6c  of this is to al
32fba 6c 6f 77 20 61 63 63 65 73 73 20 74 6f 20 74 68  low access to th
32fbb 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
32fbc 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 65 76 65  table.    ** eve
32fbd 6e 20 77 68 65 6e 20 69 74 73 20 63 6f 6e 74 65  n when its conte
32fbe 6e 74 73 20 68 61 76 65 20 62 65 65 6e 20 63 6f  nts have been co
32fbf 72 72 75 70 74 65 64 2e 0a 20 20 20 20 2a 2f 0a  rrupted..    */.
32fc0 20 20 20 20 44 62 53 65 74 50 72 6f 70 65 72 74      DbSetPropert
32fc1 79 28 64 62 2c 20 69 44 62 2c 20 44 42 5f 53 63  y(db, iDb, DB_Sc
32fc2 68 65 6d 61 4c 6f 61 64 65 64 29 3b 0a 20 20 20  hemaLoaded);.   
32fc3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
32fc4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20  .  }..  /* Jump 
32fc5 68 65 72 65 20 66 6f 72 20 61 6e 20 65 72 72 6f  here for an erro
32fc6 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 66  r that occurs af
32fc7 74 65 72 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ter successfully
32fc8 20 61 6c 6c 6f 63 61 74 69 6e 67 0a 20 20 2a 2a   allocating.  **
32fc9 20 63 75 72 4d 61 69 6e 20 61 6e 64 20 63 61 6c   curMain and cal
32fca 6c 69 6e 67 20 73 71 6c 69 74 65 33 42 74 72 65  ling sqlite3Btre
32fcb 65 45 6e 74 65 72 28 29 2e 20 46 6f 72 20 61 6e  eEnter(). For an
32fcc 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75   error that occu
32fcd 72 73 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 74  rs.  ** before t
32fce 68 61 74 20 70 6f 69 6e 74 2c 20 6a 75 6d 70 20  hat point, jump 
32fcf 74 6f 20 65 72 72 6f 72 5f 6f 75 74 2e 0a 20 20  to error_out..  
32fd0 2a 2f 0a 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72  */.initone_error
32fd1 5f 6f 75 74 3a 0a 20 20 69 66 28 20 6f 70 65 6e  _out:.  if( open
32fd2 65 64 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b  edTransaction ){
32fd3 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
32fd4 65 43 6f 6d 6d 69 74 28 70 44 62 2d 3e 70 42 74  eCommit(pDb->pBt
32fd5 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
32fd6 42 74 72 65 65 4c 65 61 76 65 28 70 44 62 2d 3e  BtreeLeave(pDb->
32fd7 70 42 74 29 3b 0a 0a 65 72 72 6f 72 5f 6f 75 74  pBt);..error_out
32fd8 3a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  :.  if( rc==SQLI
32fd9 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d  TE_NOMEM || rc==
32fda 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
32fdb 45 4d 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61  EM ){.    db->ma
32fdc 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
32fdd 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
32fde 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
32fdf 6c 69 7a 65 20 61 6c 6c 20 64 61 74 61 62 61 73  lize all databas
32fe0 65 20 66 69 6c 65 73 20 2d 20 74 68 65 20 6d 61  e files - the ma
32fe1 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
32fe2 2c 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 75 73  , the file.** us
32fe3 65 64 20 74 6f 20 73 74 6f 72 65 20 74 65 6d 70  ed to store temp
32fe4 6f 72 61 72 79 20 74 61 62 6c 65 73 2c 20 61 6e  orary tables, an
32fe5 64 20 61 6e 79 20 61 64 64 69 74 69 6f 6e 61 6c  d any additional
32fe6 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 0a   database files.
32fe7 2a 2a 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  ** created using
32fe8 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e   ATTACH statemen
32fe9 74 73 2e 20 20 52 65 74 75 72 6e 20 61 20 73 75  ts.  Return a su
32fea 63 63 65 73 73 20 63 6f 64 65 2e 20 20 49 66 20  ccess code.  If 
32feb 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 63 75  an.** error occu
32fec 72 73 2c 20 77 72 69 74 65 20 61 6e 20 65 72 72  rs, write an err
32fed 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20  or message into 
32fee 2a 70 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a  *pzErrMsg..**.**
32fef 20 41 66 74 65 72 20 61 20 64 61 74 61 62 61 73   After a databas
32ff0 65 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  e is initialized
32ff1 2c 20 74 68 65 20 44 42 5f 53 63 68 65 6d 61 4c  , the DB_SchemaL
32ff2 6f 61 64 65 64 20 62 69 74 20 69 73 20 73 65 74  oaded bit is set
32ff3 0a 2a 2a 20 62 69 74 20 69 73 20 73 65 74 20 69  .** bit is set i
32ff4 6e 20 74 68 65 20 66 6c 61 67 73 20 66 69 65 6c  n the flags fiel
32ff5 64 20 6f 66 20 74 68 65 20 44 62 20 73 74 72 75  d of the Db stru
32ff6 63 74 75 72 65 2e 20 49 66 20 74 68 65 20 64 61  cture. If the da
32ff7 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 77  tabase.** file w
32ff8 61 73 20 6f 66 20 7a 65 72 6f 2d 6c 65 6e 67 74  as of zero-lengt
32ff9 68 2c 20 74 68 65 6e 20 74 68 65 20 44 42 5f 45  h, then the DB_E
32ffa 6d 70 74 79 20 66 6c 61 67 20 69 73 20 61 6c 73  mpty flag is als
32ffb 6f 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  o set..*/.SQLITE
32ffc 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
32ffd 69 74 65 33 49 6e 69 74 28 73 71 6c 69 74 65 33  ite3Init(sqlite3
32ffe 20 2a 64 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45   *db, char **pzE
32fff 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20 69 2c  rrMsg){.  int i,
33000 20 72 63 3b 0a 20 20 69 6e 74 20 63 6f 6d 6d 69   rc;.  int commi
33001 74 5f 69 6e 74 65 72 6e 61 6c 20 3d 20 21 28 64  t_internal = !(d
33002 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f  b->flags&SQLITE_
33003 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 29 3b 0a  InternChanges);.
33004 20 20 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c    .  assert( sql
33005 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
33006 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
33007 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
33008 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20    db->init.busy 
33009 3d 20 31 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  = 1;.  for(i=0; 
3300a 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
3300b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
3300c 7b 0a 20 20 20 20 69 66 28 20 44 62 48 61 73 50  {.    if( DbHasP
3300d 72 6f 70 65 72 74 79 28 64 62 2c 20 69 2c 20 44  roperty(db, i, D
3300e 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20  B_SchemaLoaded) 
3300f 7c 7c 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e  || i==1 ) contin
33010 75 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ue;.    rc = sql
33011 69 74 65 33 49 6e 69 74 4f 6e 65 28 64 62 2c 20  ite3InitOne(db, 
33012 69 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20  i, pzErrMsg);.  
33013 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
33014 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 4f 6e    sqlite3ResetOn
33015 65 53 63 68 65 6d 61 28 64 62 2c 20 69 29 3b 0a  eSchema(db, i);.
33016 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
33017 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 6f 74 68  Once all the oth
33018 65 72 20 64 61 74 61 62 61 73 65 73 20 68 61 76  er databases hav
33019 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 73  e been initialis
3301a 65 64 2c 20 6c 6f 61 64 20 74 68 65 20 73 63 68  ed, load the sch
3301b 65 6d 61 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65  ema.  ** for the
3301c 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 2e 20   TEMP database. 
3301d 54 68 69 73 20 69 73 20 6c 6f 61 64 65 64 20 6c  This is loaded l
3301e 61 73 74 2c 20 61 73 20 74 68 65 20 54 45 4d 50  ast, as the TEMP
3301f 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 73   database.  ** s
33020 63 68 65 6d 61 20 6d 61 79 20 63 6f 6e 74 61 69  chema may contai
33021 6e 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  n references to 
33022 6f 62 6a 65 63 74 73 20 69 6e 20 6f 74 68 65 72  objects in other
33023 20 64 61 74 61 62 61 73 65 73 2e 0a 20 20 2a 2f   databases..  */
33024 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
33025 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 69 66  OMIT_TEMPDB.  if
33026 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
33027 26 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 6e 44  && ALWAYS(db->nD
33028 62 3e 31 29 0a 20 20 20 20 20 20 20 20 20 20 20  b>1).           
33029 20 20 20 20 20 20 20 20 20 26 26 20 21 44 62 48           && !DbH
3302a 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 31  asProperty(db, 1
3302b 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  , DB_SchemaLoade
3302c 64 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  d) ){.    rc = s
3302d 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28 64 62  qlite3InitOne(db
3302e 2c 20 31 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a  , 1, pzErrMsg);.
3302f 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
33030 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
33031 4f 6e 65 53 63 68 65 6d 61 28 64 62 2c 20 31 29  OneSchema(db, 1)
33032 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
33033 69 66 0a 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62  if..  db->init.b
33034 75 73 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 72  usy = 0;.  if( r
33035 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
33036 63 6f 6d 6d 69 74 5f 69 6e 74 65 72 6e 61 6c 20  commit_internal 
33037 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f  ){.    sqlite3Co
33038 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e  mmitInternalChan
33039 67 65 73 28 64 62 29 3b 0a 20 20 7d 0a 0a 20 20  ges(db);.  }..  
3303a 72 65 74 75 72 6e 20 72 63 3b 20 0a 7d 0a 0a 2f  return rc; .}../
3303b 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
3303c 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20  e is a no-op if 
3303d 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
3303e 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20 69  ema is already i
3303f 6e 69 74 69 61 6c 69 73 65 64 2e 0a 2a 2a 20 4f  nitialised..** O
33040 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 73 63  therwise, the sc
33041 68 65 6d 61 20 69 73 20 6c 6f 61 64 65 64 2e 20  hema is loaded. 
33042 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  An error code is
33043 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51   returned..*/.SQ
33044 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
33045 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
33046 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ma(Parse *pParse
33047 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
33048 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74  LITE_OK;.  sqlit
33049 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
3304a 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 73  >db;.  assert( s
3304b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
3304c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
3304d 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
3304e 62 75 73 79 20 29 7b 0a 20 20 20 20 72 63 20 3d  busy ){.    rc =
3304f 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64 62 2c   sqlite3Init(db,
33050 20 26 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73   &pParse->zErrMs
33051 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  g);.  }.  if( rc
33052 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
33053 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
33054 72 63 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  rc;.    pParse->
33055 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65  nErr++;.  }.  re
33056 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
33057 2a 2a 20 43 68 65 63 6b 20 73 63 68 65 6d 61 20  ** Check schema 
33058 63 6f 6f 6b 69 65 73 20 69 6e 20 61 6c 6c 20 64  cookies in all d
33059 61 74 61 62 61 73 65 73 2e 20 20 49 66 20 61 6e  atabases.  If an
3305a 79 20 63 6f 6f 6b 69 65 20 69 73 20 6f 75 74 0a  y cookie is out.
3305b 2a 2a 20 6f 66 20 64 61 74 65 20 73 65 74 20 70  ** of date set p
3305c 50 61 72 73 65 2d 3e 72 63 20 74 6f 20 53 51 4c  Parse->rc to SQL
3305d 49 54 45 5f 53 43 48 45 4d 41 2e 20 20 49 66 20  ITE_SCHEMA.  If 
3305e 61 6c 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  all schema cooki
3305f 65 73 0a 2a 2a 20 6d 61 6b 65 20 6e 6f 20 63 68  es.** make no ch
33060 61 6e 67 65 73 20 74 6f 20 70 50 61 72 73 65 2d  anges to pParse-
33061 3e 72 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  >rc..*/.static v
33062 6f 69 64 20 73 63 68 65 6d 61 49 73 56 61 6c 69  oid schemaIsVali
33063 64 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  d(Parse *pParse)
33064 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
33065 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
33066 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 72  int iDb;.  int r
33067 63 3b 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65 3b  c;.  int cookie;
33068 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ..  assert( pPar
33069 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20  se->checkSchema 
3306a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
3306b 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
3306c 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
3306d 66 6f 72 28 69 44 62 3d 30 3b 20 69 44 62 3c 64  for(iDb=0; iDb<d
3306e 62 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b 0a  b->nDb; iDb++){.
3306f 20 20 20 20 69 6e 74 20 6f 70 65 6e 65 64 54 72      int openedTr
33070 61 6e 73 61 63 74 69 6f 6e 20 3d 20 30 3b 20 20  ansaction = 0;  
33071 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
33072 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
33073 69 73 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 20  is opened */.   
33074 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
33075 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 20  ->aDb[iDb].pBt; 
33076 20 20 20 20 2f 2a 20 42 74 72 65 65 20 64 61 74      /* Btree dat
33077 61 62 61 73 65 20 74 6f 20 72 65 61 64 20 63 6f  abase to read co
33078 6f 6b 69 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 20  okie from */.   
33079 20 69 66 28 20 70 42 74 3d 3d 30 20 29 20 63 6f   if( pBt==0 ) co
3307a 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 2f 2a 20  ntinue;..    /* 
3307b 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20  If there is not 
3307c 61 6c 72 65 61 64 79 20 61 20 72 65 61 64 2d 6f  already a read-o
3307d 6e 6c 79 20 28 6f 72 20 72 65 61 64 2d 77 72 69  nly (or read-wri
3307e 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  te) transaction 
3307f 6f 70 65 6e 65 64 0a 20 20 20 20 2a 2a 20 6f 6e  opened.    ** on
33080 20 74 68 65 20 62 2d 74 72 65 65 20 64 61 74 61   the b-tree data
33081 62 61 73 65 2c 20 6f 70 65 6e 20 6f 6e 65 20 6e  base, open one n
33082 6f 77 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63  ow. If a transac
33083 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2c 20  tion is opened, 
33084 69 74 20 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20  it .    ** will 
33085 62 65 20 63 6c 6f 73 65 64 20 69 6d 6d 65 64 69  be closed immedi
33086 61 74 65 6c 79 20 61 66 74 65 72 20 72 65 61 64  ately after read
33087 69 6e 67 20 74 68 65 20 6d 65 74 61 2d 76 61 6c  ing the meta-val
33088 75 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21  ue. */.    if( !
33089 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
3308a 52 65 61 64 54 72 61 6e 73 28 70 42 74 29 20 29  ReadTrans(pBt) )
3308b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
3308c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72  ite3BtreeBeginTr
3308d 61 6e 73 28 70 42 74 2c 20 30 29 3b 0a 20 20 20  ans(pBt, 0);.   
3308e 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
3308f 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53  E_NOMEM || rc==S
33090 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45  QLITE_IOERR_NOME
33091 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d  M ){.        db-
33092 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
33093 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
33094 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
33095 4f 4b 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  OK ) return;.   
33096 20 20 20 6f 70 65 6e 65 64 54 72 61 6e 73 61 63     openedTransac
33097 74 69 6f 6e 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  tion = 1;.    }.
33098 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65  .    /* Read the
33099 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 66   schema cookie f
3309a 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
3309b 2e 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74  . If it does not
3309c 20 6d 61 74 63 68 20 74 68 65 20 0a 20 20 20 20   match the .    
3309d 2a 2a 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  ** value stored 
3309e 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 69  as part of the i
3309f 6e 2d 6d 65 6d 6f 72 79 20 73 63 68 65 6d 61 20  n-memory schema 
330a0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 0a  representation,.
330a1 20 20 20 20 2a 2a 20 73 65 74 20 50 61 72 73 65      ** set Parse
330a2 2e 72 63 20 74 6f 20 53 51 4c 49 54 45 5f 53 43  .rc to SQLITE_SC
330a3 48 45 4d 41 2e 20 2a 2f 0a 20 20 20 20 73 71 6c  HEMA. */.    sql
330a4 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
330a5 28 70 42 74 2c 20 42 54 52 45 45 5f 53 43 48 45  (pBt, BTREE_SCHE
330a6 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 28 75 33 32  MA_VERSION, (u32
330a7 20 2a 29 26 63 6f 6f 6b 69 65 29 3b 0a 20 20 20   *)&cookie);.   
330a8 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
330a9 53 63 68 65 6d 61 4d 75 74 65 78 48 65 6c 64 28  SchemaMutexHeld(
330aa 64 62 2c 20 69 44 62 2c 20 30 29 20 29 3b 0a 20  db, iDb, 0) );. 
330ab 20 20 20 69 66 28 20 63 6f 6f 6b 69 65 21 3d 64     if( cookie!=d
330ac 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
330ad 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b  ema->schema_cook
330ae 69 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ie ){.      sqli
330af 74 65 33 52 65 73 65 74 4f 6e 65 53 63 68 65 6d  te3ResetOneSchem
330b0 61 28 64 62 2c 20 69 44 62 29 3b 0a 20 20 20 20  a(db, iDb);.    
330b1 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
330b2 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20  QLITE_SCHEMA;.  
330b3 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6c 6f 73    }..    /* Clos
330b4 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  e the transactio
330b5 6e 2c 20 69 66 20 6f 6e 65 20 77 61 73 20 6f 70  n, if one was op
330b6 65 6e 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ened. */.    if(
330b7 20 6f 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69   openedTransacti
330b8 6f 6e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  on ){.      sqli
330b9 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 70  te3BtreeCommit(p
330ba 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  Bt);.    }.  }.}
330bb 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
330bc 61 20 73 63 68 65 6d 61 20 70 6f 69 6e 74 65 72  a schema pointer
330bd 20 69 6e 74 6f 20 74 68 65 20 69 44 62 20 69 6e   into the iDb in
330be 64 65 78 20 74 68 61 74 20 69 6e 64 69 63 61 74  dex that indicat
330bf 65 73 0a 2a 2a 20 77 68 69 63 68 20 64 61 74 61  es.** which data
330c0 62 61 73 65 20 66 69 6c 65 20 69 6e 20 64 62 2d  base file in db-
330c1 3e 61 44 62 5b 5d 20 74 68 65 20 73 63 68 65 6d  >aDb[] the schem
330c2 61 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a  a refers to..**.
330c3 2a 2a 20 49 66 20 74 68 65 20 73 61 6d 65 20 64  ** If the same d
330c4 61 74 61 62 61 73 65 20 69 73 20 61 74 74 61 63  atabase is attac
330c5 68 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  hed more than on
330c6 63 65 2c 20 74 68 65 20 66 69 72 73 74 0a 2a 2a  ce, the first.**
330c7 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
330c8 73 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  se is returned..
330c9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
330ca 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 63 68  E int sqlite3Sch
330cb 65 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c 69 74  emaToIndex(sqlit
330cc 65 33 20 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a  e3 *db, Schema *
330cd 70 53 63 68 65 6d 61 29 7b 0a 20 20 69 6e 74 20  pSchema){.  int 
330ce 69 20 3d 20 2d 31 30 30 30 30 30 30 3b 0a 0a 20  i = -1000000;.. 
330cf 20 2f 2a 20 49 66 20 70 53 63 68 65 6d 61 20 69   /* If pSchema i
330d0 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 72 65 74  s NULL, then ret
330d1 75 72 6e 20 2d 31 30 30 30 30 30 30 2e 20 54 68  urn -1000000. Th
330d2 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20  is happens when 
330d3 63 6f 64 65 20 69 6e 20 0a 20 20 2a 2a 20 65 78  code in .  ** ex
330d4 70 72 2e 63 20 69 73 20 74 72 79 69 6e 67 20 74  pr.c is trying t
330d5 6f 20 72 65 73 6f 6c 76 65 20 61 20 72 65 66 65  o resolve a refe
330d6 72 65 6e 63 65 20 74 6f 20 61 20 74 72 61 6e 73  rence to a trans
330d7 69 65 6e 74 20 74 61 62 6c 65 20 28 69 2e 65 2e  ient table (i.e.
330d8 20 6f 6e 65 0a 20 20 2a 2a 20 63 72 65 61 74 65   one.  ** create
330d9 64 20 62 79 20 61 20 73 75 62 2d 73 65 6c 65 63  d by a sub-selec
330da 74 29 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  t). In this case
330db 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
330dc 65 20 6f 66 20 74 68 69 73 20 0a 20 20 2a 2a 20  e of this .  ** 
330dd 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
330de 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e 0a 20  never be used.. 
330df 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 72 65 74 75   **.  ** We retu
330e0 72 6e 20 2d 31 30 30 30 30 30 30 20 69 6e 73 74  rn -1000000 inst
330e1 65 61 64 20 6f 66 20 74 68 65 20 6d 6f 72 65 20  ead of the more 
330e2 75 73 75 61 6c 20 2d 31 20 73 69 6d 70 6c 79 20  usual -1 simply 
330e3 62 65 63 61 75 73 65 20 75 73 69 6e 67 0a 20 20  because using.  
330e4 2a 2a 20 2d 31 30 30 30 30 30 30 20 61 73 20 74  ** -1000000 as t
330e5 68 65 20 69 6e 63 6f 72 72 65 63 74 20 69 6e 64  he incorrect ind
330e6 65 78 20 69 6e 74 6f 20 64 62 2d 3e 61 44 62 5b  ex into db->aDb[
330e7 5d 20 69 73 20 6d 75 63 68 20 0a 20 20 2a 2a 20  ] is much .  ** 
330e8 6d 6f 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 63  more likely to c
330e9 61 75 73 65 20 61 20 73 65 67 66 61 75 6c 74 20  ause a segfault 
330ea 74 68 61 6e 20 2d 31 20 28 6f 66 20 63 6f 75 72  than -1 (of cour
330eb 73 65 20 74 68 65 72 65 20 61 72 65 20 61 73 73  se there are ass
330ec 65 72 74 28 29 0a 20 20 2a 2a 20 73 74 61 74 65  ert().  ** state
330ed 6d 65 6e 74 73 20 74 6f 6f 2c 20 62 75 74 20 69  ments too, but i
330ee 74 20 6e 65 76 65 72 20 68 75 72 74 73 20 74 6f  t never hurts to
330ef 20 70 6c 61 79 20 74 68 65 20 6f 64 64 73 29 2e   play the odds).
330f0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
330f1 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
330f2 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
330f3 0a 20 20 69 66 28 20 70 53 63 68 65 6d 61 20 29  .  if( pSchema )
330f4 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 41  {.    for(i=0; A
330f5 4c 57 41 59 53 28 69 3c 64 62 2d 3e 6e 44 62 29  LWAYS(i<db->nDb)
330f6 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
330f7 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 63  ( db->aDb[i].pSc
330f8 68 65 6d 61 3d 3d 70 53 63 68 65 6d 61 20 29 7b  hema==pSchema ){
330f9 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
330fa 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
330fb 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
330fc 26 20 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  & i<db->nDb );. 
330fd 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d   }.  return i;.}
330fe 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65 20  ../*.** Compile 
330ff 74 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 65  the UTF-8 encode
33100 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  d SQL statement 
33101 7a 53 71 6c 20 69 6e 74 6f 20 61 20 73 74 61 74  zSql into a stat
33102 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f  ement handle..*/
33103 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
33104 74 65 33 50 72 65 70 61 72 65 28 0a 20 20 73 71  te3Prepare(.  sq
33105 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
33106 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
33107 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20  ase handle. */. 
33108 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
33109 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54  l,         /* UT
3310a 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20  F-8 encoded SQL 
3310b 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20  statement. */.  
3310c 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20  int nBytes,     
3310d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
3310e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62  gth of zSql in b
3310f 79 74 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73  ytes. */.  int s
33110 61 76 65 53 71 6c 46 6c 61 67 2c 20 20 20 20 20  aveSqlFlag,     
33111 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
33112 63 6f 70 79 20 53 51 4c 20 74 65 78 74 20 69 6e  copy SQL text in
33113 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  to the sqlite3_s
33114 74 6d 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70  tmt */.  Vdbe *p
33115 52 65 70 72 65 70 61 72 65 2c 20 20 20 20 20 20  Reprepare,      
33116 20 20 20 2f 2a 20 56 4d 20 62 65 69 6e 67 20 72     /* VM being r
33117 65 70 72 65 70 61 72 65 64 20 2a 2f 0a 20 20 73  eprepared */.  s
33118 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
33119 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a  Stmt,    /* OUT:
3311a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
3311b 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
3311c 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ment */.  const 
3311d 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20  char **pzTail   
3311e 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20      /* OUT: End 
3311f 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67  of parsed string
33120 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 2a   */.){.  Parse *
33121 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20  pParse;         
33122 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
33123 6e 74 65 78 74 20 2a 2f 0a 20 20 63 68 61 72 20  ntext */.  char 
33124 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 20 20 20  *zErrMsg = 0;   
33125 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65       /* Error me
33126 73 73 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72  ssage */.  int r
33127 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
33128 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63       /* Result c
33129 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  ode */.  int i; 
3312a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3312b 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
3312c 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  er */..  /* Allo
3312d 63 61 74 65 20 74 68 65 20 70 61 72 73 69 6e 67  cate the parsing
3312e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 70 50   context */.  pP
3312f 61 72 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74  arse = sqlite3St
33130 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  ackAllocZero(db,
33131 20 73 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 29   sizeof(*pParse)
33132 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d  );.  if( pParse=
33133 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  =0 ){.    rc = S
33134 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
33135 20 67 6f 74 6f 20 65 6e 64 5f 70 72 65 70 61 72   goto end_prepar
33136 65 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d  e;.  }.  pParse-
33137 3e 70 52 65 70 72 65 70 61 72 65 20 3d 20 70 52  >pReprepare = pR
33138 65 70 72 65 70 61 72 65 3b 0a 20 20 61 73 73 65  eprepare;.  asse
33139 72 74 28 20 70 70 53 74 6d 74 20 26 26 20 2a 70  rt( ppStmt && *p
3313a 70 53 74 6d 74 3d 3d 30 20 29 3b 0a 20 20 61 73  pStmt==0 );.  as
3313b 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
3313c 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73  cFailed );.  ass
3313d 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
3313e 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
3313f 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  x) );..  /* Chec
33140 6b 20 74 6f 20 76 65 72 69 66 79 20 74 68 61 74  k to verify that
33141 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   it is possible 
33142 74 6f 20 67 65 74 20 61 20 72 65 61 64 20 6c 6f  to get a read lo
33143 63 6b 20 6f 6e 20 61 6c 6c 0a 20 20 2a 2a 20 64  ck on all.  ** d
33144 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 73 2e  atabase schemas.
33145 20 20 54 68 65 20 69 6e 61 62 69 6c 69 74 79 20    The inability 
33146 74 6f 20 67 65 74 20 61 20 72 65 61 64 20 6c 6f  to get a read lo
33147 63 6b 20 69 6e 64 69 63 61 74 65 73 20 74 68 61  ck indicates tha
33148 74 0a 20 20 2a 2a 20 73 6f 6d 65 20 6f 74 68 65  t.  ** some othe
33149 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
3314a 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67  ction is holding
3314b 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 77   a write-lock, w
3314c 68 69 63 68 20 69 6e 0a 20 20 2a 2a 20 74 75 72  hich in.  ** tur
3314d 6e 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  n means that the
3314e 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f   other connectio
3314f 6e 20 68 61 73 20 6d 61 64 65 20 75 6e 63 6f 6d  n has made uncom
33150 6d 69 74 74 65 64 20 63 68 61 6e 67 65 73 0a 20  mitted changes. 
33151 20 2a 2a 20 74 6f 20 74 68 65 20 73 63 68 65 6d   ** to the schem
33152 61 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 72  a..  **.  ** Wer
33153 65 20 77 65 20 74 6f 20 70 72 6f 63 65 65 64 20  e we to proceed 
33154 61 6e 64 20 70 72 65 70 61 72 65 20 74 68 65 20  and prepare the 
33155 73 74 61 74 65 6d 65 6e 74 20 61 67 61 69 6e 73  statement agains
33156 74 20 74 68 65 20 75 6e 63 6f 6d 6d 69 74 74 65  t the uncommitte
33157 64 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 63 68  d.  ** schema ch
33158 61 6e 67 65 73 20 61 6e 64 20 69 66 20 74 68 6f  anges and if tho
33159 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  se schema change
3315a 73 20 61 72 65 20 73 75 62 73 65 71 75 65 6e 74  s are subsequent
3315b 6c 79 20 72 6f 6c 6c 65 64 0a 20 20 2a 2a 20 62  ly rolled.  ** b
3315c 61 63 6b 20 61 6e 64 20 64 69 66 66 65 72 65 6e  ack and differen
3315d 74 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61  t changes are ma
3315e 64 65 20 69 6e 20 74 68 65 69 72 20 70 6c 61 63  de in their plac
3315f 65 2c 20 74 68 65 6e 20 77 68 65 6e 20 74 68 69  e, then when thi
33160 73 0a 20 20 2a 2a 20 70 72 65 70 61 72 65 64 20  s.  ** prepared 
33161 73 74 61 74 65 6d 65 6e 74 20 67 6f 65 73 20 74  statement goes t
33162 6f 20 72 75 6e 20 74 68 65 20 73 63 68 65 6d 61  o run the schema
33163 20 63 6f 6f 6b 69 65 20 77 6f 75 6c 64 20 66 61   cookie would fa
33164 69 6c 20 74 6f 20 64 65 74 65 63 74 0a 20 20 2a  il to detect.  *
33165 2a 20 74 68 65 20 73 63 68 65 6d 61 20 63 68 61  * the schema cha
33166 6e 67 65 2e 20 20 44 69 73 61 73 74 65 72 20 77  nge.  Disaster w
33167 6f 75 6c 64 20 66 6f 6c 6c 6f 77 2e 0a 20 20 2a  ould follow..  *
33168 2a 0a 20 20 2a 2a 20 54 68 69 73 20 74 68 72 65  *.  ** This thre
33169 61 64 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  ad is currently 
3316a 68 6f 6c 64 69 6e 67 20 6d 75 74 65 78 65 73 20  holding mutexes 
3316b 6f 6e 20 61 6c 6c 20 42 74 72 65 65 73 20 28 62  on all Btrees (b
3316c 65 63 61 75 73 65 0a 20 20 2a 2a 20 6f 66 20 74  ecause.  ** of t
3316d 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  he sqlite3BtreeE
3316e 6e 74 65 72 41 6c 6c 28 29 20 69 6e 20 73 71 6c  nterAll() in sql
3316f 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61  ite3LockAndPrepa
33170 72 65 28 29 29 20 73 6f 20 69 74 0a 20 20 2a 2a  re()) so it.  **
33171 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
33172 20 66 6f 72 20 61 6e 6f 74 68 65 72 20 74 68 72   for another thr
33173 65 61 64 20 74 6f 20 73 74 61 72 74 20 61 20 6e  ead to start a n
33174 65 77 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  ew schema change
33175 0a 20 20 2a 2a 20 77 68 69 6c 65 20 74 68 69 73  .  ** while this
33176 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e 6e   routine is runn
33177 69 6e 67 2e 20 20 48 65 6e 63 65 2c 20 77 65 20  ing.  Hence, we 
33178 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 68  do not need to h
33179 6f 6c 64 20 0a 20 20 2a 2a 20 6c 6f 63 6b 73 20  old .  ** locks 
3317a 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 2c 20 77  on the schema, w
3317b 65 20 6a 75 73 74 20 6e 65 65 64 20 74 6f 20 6d  e just need to m
3317c 61 6b 65 20 73 75 72 65 20 6e 6f 62 6f 64 79 20  ake sure nobody 
3317d 65 6c 73 65 20 69 73 20 0a 20 20 2a 2a 20 68 6f  else is .  ** ho
3317e 6c 64 69 6e 67 20 74 68 65 6d 2e 0a 20 20 2a 2a  lding them..  **
3317f 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  .  ** Note that 
33180 73 65 74 74 69 6e 67 20 52 45 41 44 5f 55 4e 43  setting READ_UNC
33181 4f 4d 4d 49 54 54 45 44 20 6f 76 65 72 72 69 64  OMMITTED overrid
33182 65 73 20 6d 6f 73 74 20 6c 6f 63 6b 20 64 65 74  es most lock det
33183 65 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 62 75 74  ection,.  ** but
33184 20 69 74 20 64 6f 65 73 20 2a 6e 6f 74 2a 20 6f   it does *not* o
33185 76 65 72 72 69 64 65 20 73 63 68 65 6d 61 20 6c  verride schema l
33186 6f 63 6b 20 64 65 74 65 63 74 69 6f 6e 2c 20 73  ock detection, s
33187 6f 20 74 68 69 73 20 61 6c 6c 20 73 74 69 6c 6c  o this all still
33188 0a 20 20 2a 2a 20 77 6f 72 6b 73 20 65 76 65 6e  .  ** works even
33189 20 69 66 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49   if READ_UNCOMMI
3318a 54 54 45 44 20 69 73 20 73 65 74 2e 0a 20 20 2a  TTED is set..  *
3318b 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64  /.  for(i=0; i<d
3318c 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 20 7b 0a 20  b->nDb; i++) {. 
3318d 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
3318e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
3318f 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20      if( pBt ){. 
33190 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c       assert( sql
33191 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
33192 74 65 78 28 70 42 74 29 20 29 3b 0a 20 20 20 20  tex(pBt) );.    
33193 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
33194 72 65 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28  reeSchemaLocked(
33195 70 42 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  pBt);.      if( 
33196 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  rc ){.        co
33197 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  nst char *zDb = 
33198 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65  db->aDb[i].zName
33199 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
3319a 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 22  3Error(db, rc, "
3319b 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
3319c 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c 20  is locked: %s", 
3319d 7a 44 62 29 3b 0a 20 20 20 20 20 20 20 20 74 65  zDb);.        te
3319e 73 74 63 61 73 65 28 20 64 62 2d 3e 66 6c 61 67  stcase( db->flag
3319f 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55  s & SQLITE_ReadU
331a0 6e 63 6f 6d 6d 69 74 74 65 64 20 29 3b 0a 20 20  ncommitted );.  
331a1 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70        goto end_p
331a2 72 65 70 61 72 65 3b 0a 20 20 20 20 20 20 7d 0a  repare;.      }.
331a3 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c      }.  }..  sql
331a4 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69  ite3VtabUnlockLi
331a5 73 74 28 64 62 29 3b 0a 0a 20 20 70 50 61 72 73  st(db);..  pPars
331a6 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70 50  e->db = db;.  pP
331a7 61 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f 6f 70  arse->nQueryLoop
331a8 20 3d 20 28 64 6f 75 62 6c 65 29 31 3b 0a 20 20   = (double)1;.  
331a9 69 66 28 20 6e 42 79 74 65 73 3e 3d 30 20 26 26  if( nBytes>=0 &&
331aa 20 28 6e 42 79 74 65 73 3d 3d 30 20 7c 7c 20 7a   (nBytes==0 || z
331ab 53 71 6c 5b 6e 42 79 74 65 73 2d 31 5d 21 3d 30  Sql[nBytes-1]!=0
331ac 29 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  ) ){.    char *z
331ad 53 71 6c 43 6f 70 79 3b 0a 20 20 20 20 69 6e 74  SqlCopy;.    int
331ae 20 6d 78 4c 65 6e 20 3d 20 64 62 2d 3e 61 4c 69   mxLen = db->aLi
331af 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
331b0 5f 53 51 4c 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20  _SQL_LENGTH];.  
331b1 20 20 74 65 73 74 63 61 73 65 28 20 6e 42 79 74    testcase( nByt
331b2 65 73 3d 3d 6d 78 4c 65 6e 20 29 3b 0a 20 20 20  es==mxLen );.   
331b3 20 74 65 73 74 63 61 73 65 28 20 6e 42 79 74 65   testcase( nByte
331b4 73 3d 3d 6d 78 4c 65 6e 2b 31 20 29 3b 0a 20 20  s==mxLen+1 );.  
331b5 20 20 69 66 28 20 6e 42 79 74 65 73 3e 6d 78 4c    if( nBytes>mxL
331b6 65 6e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  en ){.      sqli
331b7 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
331b8 49 54 45 5f 54 4f 4f 42 49 47 2c 20 22 73 74 61  ITE_TOOBIG, "sta
331b9 74 65 6d 65 6e 74 20 74 6f 6f 20 6c 6f 6e 67 22  tement too long"
331ba 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
331bb 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
331bc 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 29 3b   SQLITE_TOOBIG);
331bd 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  .      goto end_
331be 70 72 65 70 61 72 65 3b 0a 20 20 20 20 7d 0a 20  prepare;.    }. 
331bf 20 20 20 7a 53 71 6c 43 6f 70 79 20 3d 20 73 71     zSqlCopy = sq
331c0 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64  lite3DbStrNDup(d
331c1 62 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 29  b, zSql, nBytes)
331c2 3b 0a 20 20 20 20 69 66 28 20 7a 53 71 6c 43 6f  ;.    if( zSqlCo
331c3 70 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  py ){.      sqli
331c4 74 65 33 52 75 6e 50 61 72 73 65 72 28 70 50 61  te3RunParser(pPa
331c5 72 73 65 2c 20 7a 53 71 6c 43 6f 70 79 2c 20 26  rse, zSqlCopy, &
331c6 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20  zErrMsg);.      
331c7 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
331c8 2c 20 7a 53 71 6c 43 6f 70 79 29 3b 0a 20 20 20  , zSqlCopy);.   
331c9 20 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c     pParse->zTail
331ca 20 3d 20 26 7a 53 71 6c 5b 70 50 61 72 73 65 2d   = &zSql[pParse-
331cb 3e 7a 54 61 69 6c 2d 7a 53 71 6c 43 6f 70 79 5d  >zTail-zSqlCopy]
331cc 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
331cd 20 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c     pParse->zTail
331ce 20 3d 20 26 7a 53 71 6c 5b 6e 42 79 74 65 73 5d   = &zSql[nBytes]
331cf 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
331d0 0a 20 20 20 20 73 71 6c 69 74 65 33 52 75 6e 50  .    sqlite3RunP
331d1 61 72 73 65 72 28 70 50 61 72 73 65 2c 20 7a 53  arser(pParse, zS
331d2 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20  ql, &zErrMsg);. 
331d3 20 7d 0a 20 20 61 73 73 65 72 74 28 20 31 3d 3d   }.  assert( 1==
331d4 28 69 6e 74 29 70 50 61 72 73 65 2d 3e 6e 51 75  (int)pParse->nQu
331d5 65 72 79 4c 6f 6f 70 20 29 3b 0a 0a 20 20 69 66  eryLoop );..  if
331d6 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
331d7 65 64 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65  ed ){.    pParse
331d8 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ->rc = SQLITE_NO
331d9 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  MEM;.  }.  if( p
331da 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54  Parse->rc==SQLIT
331db 45 5f 44 4f 4e 45 20 29 20 70 50 61 72 73 65 2d  E_DONE ) pParse-
331dc 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  >rc = SQLITE_OK;
331dd 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 63  .  if( pParse->c
331de 68 65 63 6b 53 63 68 65 6d 61 20 29 7b 0a 20 20  heckSchema ){.  
331df 20 20 73 63 68 65 6d 61 49 73 56 61 6c 69 64 28    schemaIsValid(
331e0 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 69  pParse);.  }.  i
331e1 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
331e2 6c 65 64 20 29 7b 0a 20 20 20 20 70 50 61 72 73  led ){.    pPars
331e3 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  e->rc = SQLITE_N
331e4 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OMEM;.  }.  if( 
331e5 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20 20 2a 70  pzTail ){.    *p
331e6 7a 54 61 69 6c 20 3d 20 70 50 61 72 73 65 2d 3e  zTail = pParse->
331e7 7a 54 61 69 6c 3b 0a 20 20 7d 0a 20 20 72 63 20  zTail;.  }.  rc 
331e8 3d 20 70 50 61 72 73 65 2d 3e 72 63 3b 0a 0a 23  = pParse->rc;..#
331e9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
331ea 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69 66 28  IT_EXPLAIN.  if(
331eb 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
331ec 26 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20  & pParse->pVdbe 
331ed 26 26 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61  && pParse->expla
331ee 69 6e 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63  in ){.    static
331ef 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f   const char * co
331f0 6e 73 74 20 61 7a 43 6f 6c 4e 61 6d 65 5b 5d 20  nst azColName[] 
331f1 3d 20 7b 0a 20 20 20 20 20 20 20 22 61 64 64 72  = {.       "addr
331f2 22 2c 20 22 6f 70 63 6f 64 65 22 2c 20 22 70 31  ", "opcode", "p1
331f3 22 2c 20 22 70 32 22 2c 20 22 70 33 22 2c 20 22  ", "p2", "p3", "
331f4 70 34 22 2c 20 22 70 35 22 2c 20 22 63 6f 6d 6d  p4", "p5", "comm
331f5 65 6e 74 22 2c 0a 20 20 20 20 20 20 20 22 73 65  ent",.       "se
331f6 6c 65 63 74 69 64 22 2c 20 22 6f 72 64 65 72 22  lectid", "order"
331f7 2c 20 22 66 72 6f 6d 22 2c 20 22 64 65 74 61 69  , "from", "detai
331f8 6c 22 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e  l".    };.    in
331f9 74 20 69 46 69 72 73 74 2c 20 6d 78 3b 0a 20 20  t iFirst, mx;.  
331fa 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78    if( pParse->ex
331fb 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a 20 20 20 20  plain==2 ){.    
331fc 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
331fd 4e 75 6d 43 6f 6c 73 28 70 50 61 72 73 65 2d 3e  NumCols(pParse->
331fe 70 56 64 62 65 2c 20 34 29 3b 0a 20 20 20 20 20  pVdbe, 4);.     
331ff 20 69 46 69 72 73 74 20 3d 20 38 3b 0a 20 20 20   iFirst = 8;.   
33200 20 20 20 6d 78 20 3d 20 31 32 3b 0a 20 20 20 20     mx = 12;.    
33201 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
33202 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
33203 6c 73 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65  ls(pParse->pVdbe
33204 2c 20 38 29 3b 0a 20 20 20 20 20 20 69 46 69 72  , 8);.      iFir
33205 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 6d 78  st = 0;.      mx
33206 20 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 8;.    }.    
33207 66 6f 72 28 69 3d 69 46 69 72 73 74 3b 20 69 3c  for(i=iFirst; i<
33208 6d 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mx; i++){.      
33209 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
3320a 6c 4e 61 6d 65 28 70 50 61 72 73 65 2d 3e 70 56  lName(pParse->pV
3320b 64 62 65 2c 20 69 2d 69 46 69 72 73 74 2c 20 43  dbe, i-iFirst, C
3320c 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 0a 20 20 20  OLNAME_NAME,.   
3320d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3320e 20 20 20 20 20 20 20 20 20 61 7a 43 6f 6c 4e 61           azColNa
3320f 6d 65 5b 69 5d 2c 20 53 51 4c 49 54 45 5f 53 54  me[i], SQLITE_ST
33210 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ATIC);.    }.  }
33211 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72  .#endif..  asser
33212 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  t( db->init.busy
33213 3d 3d 30 20 7c 7c 20 73 61 76 65 53 71 6c 46 6c  ==0 || saveSqlFl
33214 61 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 64  ag==0 );.  if( d
33215 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20  b->init.busy==0 
33216 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 70 56 64  ){.    Vdbe *pVd
33217 62 65 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  be = pParse->pVd
33218 62 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  be;.    sqlite3V
33219 64 62 65 53 65 74 53 71 6c 28 70 56 64 62 65 2c  dbeSetSql(pVdbe,
3321a 20 7a 53 71 6c 2c 20 28 69 6e 74 29 28 70 50 61   zSql, (int)(pPa
3321b 72 73 65 2d 3e 7a 54 61 69 6c 2d 7a 53 71 6c 29  rse->zTail-zSql)
3321c 2c 20 73 61 76 65 53 71 6c 46 6c 61 67 29 3b 0a  , saveSqlFlag);.
3321d 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65    }.  if( pParse
3321e 2d 3e 70 56 64 62 65 20 26 26 20 28 72 63 21 3d  ->pVdbe && (rc!=
3321f 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d  SQLITE_OK || db-
33220 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 29  >mallocFailed) )
33221 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
33222 65 46 69 6e 61 6c 69 7a 65 28 70 50 61 72 73 65  eFinalize(pParse
33223 2d 3e 70 56 64 62 65 29 3b 0a 20 20 20 20 61 73  ->pVdbe);.    as
33224 73 65 72 74 28 21 28 2a 70 70 53 74 6d 74 29 29  sert(!(*ppStmt))
33225 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a  ;.  }else{.    *
33226 70 70 53 74 6d 74 20 3d 20 28 73 71 6c 69 74 65  ppStmt = (sqlite
33227 33 5f 73 74 6d 74 2a 29 70 50 61 72 73 65 2d 3e  3_stmt*)pParse->
33228 70 56 64 62 65 3b 0a 20 20 7d 0a 0a 20 20 69 66  pVdbe;.  }..  if
33229 28 20 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20  ( zErrMsg ){.   
3322a 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
3322b 2c 20 72 63 2c 20 22 25 73 22 2c 20 7a 45 72 72  , rc, "%s", zErr
3322c 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Msg);.    sqlite
3322d 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
3322e 4d 73 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Msg);.  }else{. 
3322f 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
33230 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 7d 0a  db, rc, 0);.  }.
33231 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6e 79  .  /* Delete any
33232 20 54 72 69 67 67 65 72 50 72 67 20 73 74 72 75   TriggerPrg stru
33233 63 74 75 72 65 73 20 61 6c 6c 6f 63 61 74 65 64  ctures allocated
33234 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 20 74   while parsing t
33235 68 69 73 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a  his statement. *
33236 2f 0a 20 20 77 68 69 6c 65 28 20 70 50 61 72 73  /.  while( pPars
33237 65 2d 3e 70 54 72 69 67 67 65 72 50 72 67 20 29  e->pTriggerPrg )
33238 7b 0a 20 20 20 20 54 72 69 67 67 65 72 50 72 67  {.    TriggerPrg
33239 20 2a 70 54 20 3d 20 70 50 61 72 73 65 2d 3e 70   *pT = pParse->p
3323a 54 72 69 67 67 65 72 50 72 67 3b 0a 20 20 20 20  TriggerPrg;.    
3323b 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  pParse->pTrigger
3323c 50 72 67 20 3d 20 70 54 2d 3e 70 4e 65 78 74 3b  Prg = pT->pNext;
3323d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
3323e 65 65 28 64 62 2c 20 70 54 29 3b 0a 20 20 7d 0a  ee(db, pT);.  }.
3323f 0a 65 6e 64 5f 70 72 65 70 61 72 65 3a 0a 0a 20  .end_prepare:.. 
33240 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
33241 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20  e(db, pParse);. 
33242 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
33243 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
33244 61 73 73 65 72 74 28 20 28 72 63 26 64 62 2d 3e  assert( (rc&db->
33245 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a  errMask)==rc );.
33246 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 73    return rc;.}.s
33247 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
33248 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28  3LockAndPrepare(
33249 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
3324a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3324b 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  Database handle.
3324c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
3324d 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20   *zSql,         
3324e 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  /* UTF-8 encoded
3324f 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20   SQL statement. 
33250 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c  */.  int nBytes,
33251 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
33252 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c  * Length of zSql
33253 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20   in bytes. */.  
33254 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67 2c  int saveSqlFlag,
33255 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
33256 65 20 74 6f 20 63 6f 70 79 20 53 51 4c 20 74 65  e to copy SQL te
33257 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  xt into the sqli
33258 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 56 64  te3_stmt */.  Vd
33259 62 65 20 2a 70 4f 6c 64 2c 20 20 20 20 20 20 20  be *pOld,       
3325a 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20 62 65          /* VM be
3325b 69 6e 67 20 72 65 70 72 65 70 61 72 65 64 20 2a  ing reprepared *
3325c 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
3325d 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
3325e 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
3325f 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
33260 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
33261 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61  onst char **pzTa
33262 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
33263 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
33264 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e  tring */.){.  in
33265 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
33266 70 70 53 74 6d 74 21 3d 30 20 29 3b 0a 20 20 2a  ppStmt!=0 );.  *
33267 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 69 66  ppStmt = 0;.  if
33268 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79  ( !sqlite3Safety
33269 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b 0a 20  CheckOk(db) ){. 
3326a 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3326b 5f 4d 49 53 55 53 45 5f 42 4b 50 54 3b 0a 20 20  _MISUSE_BKPT;.  
3326c 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
3326d 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
3326e 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  x);.  sqlite3Btr
3326f 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a  eeEnterAll(db);.
33270 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 72    rc = sqlite3Pr
33271 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20  epare(db, zSql, 
33272 6e 42 79 74 65 73 2c 20 73 61 76 65 53 71 6c 46  nBytes, saveSqlF
33273 6c 61 67 2c 20 70 4f 6c 64 2c 20 70 70 53 74 6d  lag, pOld, ppStm
33274 74 2c 20 70 7a 54 61 69 6c 29 3b 0a 20 20 69 66  t, pzTail);.  if
33275 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48  ( rc==SQLITE_SCH
33276 45 4d 41 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  EMA ){.    sqlit
33277 65 33 5f 66 69 6e 61 6c 69 7a 65 28 2a 70 70 53  e3_finalize(*ppS
33278 74 6d 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  tmt);.    rc = s
33279 71 6c 69 74 65 33 50 72 65 70 61 72 65 28 64 62  qlite3Prepare(db
3327a 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 2c 20  , zSql, nBytes, 
3327b 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 70 4f 6c  saveSqlFlag, pOl
3327c 64 2c 20 70 70 53 74 6d 74 2c 20 70 7a 54 61 69  d, ppStmt, pzTai
3327d 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  l);.  }.  sqlite
3327e 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64  3BtreeLeaveAll(d
3327f 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  b);.  sqlite3_mu
33280 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
33281 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20  tex);.  assert( 
33282 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
33283 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 0a 20   *ppStmt==0 );. 
33284 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
33285 2a 0a 2a 2a 20 52 65 72 75 6e 20 74 68 65 20 63  *.** Rerun the c
33286 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 61 20  ompilation of a 
33287 73 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20  statement after 
33288 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e  a schema change.
33289 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 74  .**.** If the st
3328a 61 74 65 6d 65 6e 74 20 69 73 20 73 75 63 63 65  atement is succe
3328b 73 73 66 75 6c 6c 79 20 72 65 63 6f 6d 70 69 6c  ssfully recompil
3328c 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ed, return SQLIT
3328d 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c  E_OK. Otherwise,
3328e 0a 2a 2a 20 69 66 20 74 68 65 20 73 74 61 74 65  .** if the state
3328f 6d 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 72  ment cannot be r
33290 65 63 6f 6d 70 69 6c 65 64 20 62 65 63 61 75 73  ecompiled becaus
33291 65 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63  e another connec
33292 74 69 6f 6e 20 68 61 73 0a 2a 2a 20 6c 6f 63 6b  tion has.** lock
33293 65 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d  ed the sqlite3_m
33294 61 73 74 65 72 20 74 61 62 6c 65 2c 20 72 65 74  aster table, ret
33295 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
33296 44 2e 20 49 66 20 61 6e 79 20 6f 74 68 65 72 20  D. If any other 
33297 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2c  error.** occurs,
33298 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 53   return SQLITE_S
33299 43 48 45 4d 41 2e 0a 2a 2f 0a 53 51 4c 49 54 45  CHEMA..*/.SQLITE
3329a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
3329b 69 74 65 33 52 65 70 72 65 70 61 72 65 28 56 64  ite3Reprepare(Vd
3329c 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  be *p){.  int rc
3329d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ;.  sqlite3_stmt
3329e 20 2a 70 4e 65 77 3b 0a 20 20 63 6f 6e 73 74 20   *pNew;.  const 
3329f 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 73 71  char *zSql;.  sq
332a0 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 61 73  lite3 *db;..  as
332a1 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
332a2 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33  tex_held(sqlite3
332a3 56 64 62 65 44 62 28 70 29 2d 3e 6d 75 74 65 78  VdbeDb(p)->mutex
332a4 29 20 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71  ) );.  zSql = sq
332a5 6c 69 74 65 33 5f 73 71 6c 28 28 73 71 6c 69 74  lite3_sql((sqlit
332a6 65 33 5f 73 74 6d 74 20 2a 29 70 29 3b 0a 20 20  e3_stmt *)p);.  
332a7 61 73 73 65 72 74 28 20 7a 53 71 6c 21 3d 30 20  assert( zSql!=0 
332a8 29 3b 20 20 2f 2a 20 52 65 70 72 65 70 61 72 65  );  /* Reprepare
332a9 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 6f 72   only called for
332aa 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 73 74   prepare_v2() st
332ab 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 64 62  atements */.  db
332ac 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 44 62   = sqlite3VdbeDb
332ad 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  (p);.  assert( s
332ae 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
332af 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
332b0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f    rc = sqlite3Lo
332b1 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c  ckAndPrepare(db,
332b2 20 7a 53 71 6c 2c 20 2d 31 2c 20 30 2c 20 70 2c   zSql, -1, 0, p,
332b3 20 26 70 4e 65 77 2c 20 30 29 3b 0a 20 20 69 66   &pNew, 0);.  if
332b4 28 20 72 63 20 29 7b 0a 20 20 20 20 69 66 28 20  ( rc ){.    if( 
332b5 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
332b6 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61   ){.      db->ma
332b7 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
332b8 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
332b9 28 20 70 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20  ( pNew==0 );.   
332ba 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 65   return rc;.  }e
332bb 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
332bc 20 70 4e 65 77 21 3d 30 20 29 3b 0a 20 20 7d 0a   pNew!=0 );.  }.
332bd 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 77 61    sqlite3VdbeSwa
332be 70 28 28 56 64 62 65 2a 29 70 4e 65 77 2c 20 70  p((Vdbe*)pNew, p
332bf 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 72 61 6e  );.  sqlite3Tran
332c0 73 66 65 72 42 69 6e 64 69 6e 67 73 28 70 4e 65  sferBindings(pNe
332c1 77 2c 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  w, (sqlite3_stmt
332c2 2a 29 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  *)p);.  sqlite3V
332c3 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73 75  dbeResetStepResu
332c4 6c 74 28 28 56 64 62 65 2a 29 70 4e 65 77 29 3b  lt((Vdbe*)pNew);
332c5 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69  .  sqlite3VdbeFi
332c6 6e 61 6c 69 7a 65 28 28 56 64 62 65 2a 29 70 4e  nalize((Vdbe*)pN
332c7 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ew);.  return SQ
332c8 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
332c9 2a 2a 20 54 77 6f 20 76 65 72 73 69 6f 6e 73 20  ** Two versions 
332ca 6f 66 20 74 68 65 20 6f 66 66 69 63 69 61 6c 20  of the official 
332cb 41 50 49 2e 20 20 4c 65 67 61 63 79 20 61 6e 64  API.  Legacy and
332cc 20 6e 65 77 20 75 73 65 2e 20 20 49 6e 20 74 68   new use.  In th
332cd 65 20 6c 65 67 61 63 79 0a 2a 2a 20 76 65 72 73  e legacy.** vers
332ce 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61  ion, the origina
332cf 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20 6e 6f  l SQL text is no
332d0 74 20 73 61 76 65 64 20 69 6e 20 74 68 65 20 70  t saved in the p
332d1 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
332d2 74 0a 2a 2a 20 61 6e 64 20 73 6f 20 69 66 20 61  t.** and so if a
332d3 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 6f   schema change o
332d4 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f 53 43  ccurs, SQLITE_SC
332d5 48 45 4d 41 20 69 73 20 72 65 74 75 72 6e 65 64  HEMA is returned
332d6 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73   by.** sqlite3_s
332d7 74 65 70 28 29 2e 20 20 49 6e 20 74 68 65 20 6e  tep().  In the n
332d8 65 77 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20  ew version, the 
332d9 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78  original SQL tex
332da 74 20 69 73 20 72 65 74 61 69 6e 65 64 0a 2a 2a  t is retained.**
332db 20 61 6e 64 20 74 68 65 20 73 74 61 74 65 6d 65   and the stateme
332dc 6e 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  nt is automatica
332dd 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 69  lly recompiled i
332de 66 20 61 6e 20 73 63 68 65 6d 61 20 63 68 61 6e  f an schema chan
332df 67 65 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2f  ge.** occurs..*/
332e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
332e1 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
332e2 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
332e3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
332e4 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  Database handle.
332e5 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
332e6 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20   *zSql,         
332e7 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  /* UTF-8 encoded
332e8 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20   SQL statement. 
332e9 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c  */.  int nBytes,
332ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
332eb 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c  * Length of zSql
332ec 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20   in bytes. */.  
332ed 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70  sqlite3_stmt **p
332ee 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54  pStmt,    /* OUT
332ef 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  : A pointer to t
332f0 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
332f1 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ement */.  const
332f2 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20   char **pzTail  
332f3 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64       /* OUT: End
332f4 20 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e   of parsed strin
332f5 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  g */.){.  int rc
332f6 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
332f7 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64  LockAndPrepare(d
332f8 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 30 2c  b,zSql,nBytes,0,
332f9 30 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29  0,ppStmt,pzTail)
332fa 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
332fb 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53  SQLITE_OK || ppS
332fc 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d  tmt==0 || *ppStm
332fd 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52 49  t==0 );  /* VERI
332fe 46 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20 20  FY: F13021 */.  
332ff 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
33300 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
33301 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 0a  te3_prepare_v2(.
33302 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
33303 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
33304 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20  atabase handle. 
33305 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
33306 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f  *zSql,         /
33307 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  * UTF-8 encoded 
33308 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a  SQL statement. *
33309 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20  /.  int nBytes, 
3330a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3330b 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20   Length of zSql 
3330c 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73  in bytes. */.  s
3330d 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
3330e 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a  Stmt,    /* OUT:
3330f 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
33310 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
33311 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ment */.  const 
33312 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20  char **pzTail   
33313 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20      /* OUT: End 
33314 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67  of parsed string
33315 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
33316 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c  .  rc = sqlite3L
33317 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64 62  ockAndPrepare(db
33318 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 31 2c 30  ,zSql,nBytes,1,0
33319 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b  ,ppStmt,pzTail);
3331a 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
3331b 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74  QLITE_OK || ppSt
3331c 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74  mt==0 || *ppStmt
3331d 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52 49 46  ==0 );  /* VERIF
3331e 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20 20 72  Y: F13021 */.  r
3331f 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69  eturn rc;.}...#i
33320 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
33321 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 43 6f  T_UTF16./*.** Co
33322 6d 70 69 6c 65 20 74 68 65 20 55 54 46 2d 31 36  mpile the UTF-16
33323 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
33324 74 65 6d 65 6e 74 20 7a 53 71 6c 20 69 6e 74 6f  tement zSql into
33325 20 61 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e   a statement han
33326 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  dle..*/.static i
33327 6e 74 20 73 71 6c 69 74 65 33 50 72 65 70 61 72  nt sqlite3Prepar
33328 65 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  e16(.  sqlite3 *
33329 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
3332a 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
3332b 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73 74  dle. */ .  const
3332c 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20   void *zSql,    
3332d 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 65       /* UTF-16 e
3332e 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65  ncoded SQL state
3332f 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ment. */.  int n
33330 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20  Bytes,          
33331 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
33332 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
33333 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 53 71   */.  int saveSq
33334 6c 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20  lFlag,          
33335 2f 2a 20 54 72 75 65 20 74 6f 20 73 61 76 65 20  /* True to save 
33336 53 51 4c 20 74 65 78 74 20 69 6e 74 6f 20 74 68  SQL text into th
33337 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e sqlite3_stmt *
33338 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
33339 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
3333a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
3333b 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
3333c 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
3333d 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61  onst void **pzTa
3333e 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
3333f 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
33340 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 2f 2a  tring */.){.  /*
33341 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63   This function c
33342 75 72 72 65 6e 74 6c 79 20 77 6f 72 6b 73 20 62  urrently works b
33343 79 20 66 69 72 73 74 20 74 72 61 6e 73 66 6f 72  y first transfor
33344 6d 69 6e 67 20 74 68 65 20 55 54 46 2d 31 36 0a  ming the UTF-16.
33345 20 20 2a 2a 20 65 6e 63 6f 64 65 64 20 73 74 72    ** encoded str
33346 69 6e 67 20 74 6f 20 55 54 46 2d 38 2c 20 74 68  ing to UTF-8, th
33347 65 6e 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69  en invoking sqli
33348 74 65 33 5f 70 72 65 70 61 72 65 28 29 2e 20 54  te3_prepare(). T
33349 68 65 0a 20 20 2a 2a 20 74 72 69 63 6b 79 20 62  he.  ** tricky b
3334a 69 74 20 69 73 20 66 69 67 75 72 69 6e 67 20 6f  it is figuring o
3334b 75 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  ut the pointer t
3334c 6f 20 72 65 74 75 72 6e 20 69 6e 20 2a 70 7a 54  o return in *pzT
3334d 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72  ail..  */.  char
3334e 20 2a 7a 53 71 6c 38 3b 0a 20 20 63 6f 6e 73 74   *zSql8;.  const
3334f 20 63 68 61 72 20 2a 7a 54 61 69 6c 38 20 3d 20   char *zTail8 = 
33350 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  0;.  int rc = SQ
33351 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65  LITE_OK;..  asse
33352 72 74 28 20 70 70 53 74 6d 74 20 29 3b 0a 20 20  rt( ppStmt );.  
33353 2a 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 69  *ppStmt = 0;.  i
33354 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  f( !sqlite3Safet
33355 79 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b 0a  yCheckOk(db) ){.
33356 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
33357 45 5f 4d 49 53 55 53 45 5f 42 4b 50 54 3b 0a 20  E_MISUSE_BKPT;. 
33358 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
33359 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
3335a 65 78 29 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73  ex);.  zSql8 = s
3335b 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 64  qlite3Utf16to8(d
3335c 62 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 2c  b, zSql, nBytes,
3335d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
3335e 49 56 45 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c  IVE);.  if( zSql
3335f 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  8 ){.    rc = sq
33360 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70  lite3LockAndPrep
33361 61 72 65 28 64 62 2c 20 7a 53 71 6c 38 2c 20 2d  are(db, zSql8, -
33362 31 2c 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20  1, saveSqlFlag, 
33363 30 2c 20 70 70 53 74 6d 74 2c 20 26 7a 54 61 69  0, ppStmt, &zTai
33364 6c 38 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  l8);.  }..  if( 
33365 7a 54 61 69 6c 38 20 26 26 20 70 7a 54 61 69 6c  zTail8 && pzTail
33366 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 73 71   ){.    /* If sq
33367 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 72 65  lite3_prepare re
33368 74 75 72 6e 73 20 61 20 74 61 69 6c 20 70 6f 69  turns a tail poi
33369 6e 74 65 72 2c 20 77 65 20 63 61 6c 63 75 6c 61  nter, we calcula
3336a 74 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 71  te the.    ** eq
3336b 75 69 76 61 6c 65 6e 74 20 70 6f 69 6e 74 65 72  uivalent pointer
3336c 20 69 6e 74 6f 20 74 68 65 20 55 54 46 2d 31 36   into the UTF-16
3336d 20 73 74 72 69 6e 67 20 62 79 20 63 6f 75 6e 74   string by count
3336e 69 6e 67 20 74 68 65 20 75 6e 69 63 6f 64 65 0a  ing the unicode.
3336f 20 20 20 20 2a 2a 20 63 68 61 72 61 63 74 65 72      ** character
33370 73 20 62 65 74 77 65 65 6e 20 7a 53 71 6c 38 20  s between zSql8 
33371 61 6e 64 20 7a 54 61 69 6c 38 2c 20 61 6e 64 20  and zTail8, and 
33372 74 68 65 6e 20 72 65 74 75 72 6e 69 6e 67 20 61  then returning a
33373 20 70 6f 69 6e 74 65 72 0a 20 20 20 20 2a 2a 20   pointer.    ** 
33374 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20  the same number 
33375 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  of characters in
33376 74 6f 20 74 68 65 20 55 54 46 2d 31 36 20 73 74  to the UTF-16 st
33377 72 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ring..    */.   
33378 20 69 6e 74 20 63 68 61 72 73 5f 70 61 72 73 65   int chars_parse
33379 64 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 43  d = sqlite3Utf8C
3337a 68 61 72 4c 65 6e 28 7a 53 71 6c 38 2c 20 28 69  harLen(zSql8, (i
3337b 6e 74 29 28 7a 54 61 69 6c 38 2d 7a 53 71 6c 38  nt)(zTail8-zSql8
3337c 29 29 3b 0a 20 20 20 20 2a 70 7a 54 61 69 6c 20  ));.    *pzTail 
3337d 3d 20 28 75 38 20 2a 29 7a 53 71 6c 20 2b 20 73  = (u8 *)zSql + s
3337e 71 6c 69 74 65 33 55 74 66 31 36 42 79 74 65 4c  qlite3Utf16ByteL
3337f 65 6e 28 7a 53 71 6c 2c 20 63 68 61 72 73 5f 70  en(zSql, chars_p
33380 61 72 73 65 64 29 3b 0a 20 20 7d 0a 20 20 73 71  arsed);.  }.  sq
33381 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
33382 7a 53 71 6c 38 29 3b 20 0a 20 20 72 63 20 3d 20  zSql8); .  rc = 
33383 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
33384 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65  b, rc);.  sqlite
33385 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
33386 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
33387 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
33388 54 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  Two versions of 
33389 74 68 65 20 6f 66 66 69 63 69 61 6c 20 41 50 49  the official API
3338a 2e 20 20 4c 65 67 61 63 79 20 61 6e 64 20 6e 65  .  Legacy and ne
3338b 77 20 75 73 65 2e 20 20 49 6e 20 74 68 65 20 6c  w use.  In the l
3338c 65 67 61 63 79 0a 2a 2a 20 76 65 72 73 69 6f 6e  egacy.** version
3338d 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53  , the original S
3338e 51 4c 20 74 65 78 74 20 69 73 20 6e 6f 74 20 73  QL text is not s
3338f 61 76 65 64 20 69 6e 20 74 68 65 20 70 72 65 70  aved in the prep
33390 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a  ared statement.*
33391 2a 20 61 6e 64 20 73 6f 20 69 66 20 61 20 73 63  * and so if a sc
33392 68 65 6d 61 20 63 68 61 6e 67 65 20 6f 63 63 75  hema change occu
33393 72 73 2c 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  rs, SQLITE_SCHEM
33394 41 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79  A is returned by
33395 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70  .** sqlite3_step
33396 28 29 2e 20 20 49 6e 20 74 68 65 20 6e 65 77 20  ().  In the new 
33397 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69  version, the ori
33398 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69  ginal SQL text i
33399 73 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20 61 6e  s retained.** an
3339a 64 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  d the statement 
3339b 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  is automatically
3339c 20 72 65 63 6f 6d 70 69 6c 65 64 20 69 66 20 61   recompiled if a
3339d 6e 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 0a  n schema change.
3339e 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51  ** occurs..*/.SQ
3339f 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
333a0 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 0a  ite3_prepare16(.
333a1 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
333a2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
333a3 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20  atabase handle. 
333a4 2a 2f 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  */ .  const void
333a5 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20   *zSql,         
333a6 2f 2a 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65  /* UTF-16 encode
333a7 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
333a8 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   */.  int nBytes
333a9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
333aa 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71  /* Length of zSq
333ab 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
333ac 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
333ad 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55  ppStmt,    /* OU
333ae 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  T: A pointer to 
333af 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61  the prepared sta
333b0 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73  tement */.  cons
333b1 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20  t void **pzTail 
333b2 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e        /* OUT: En
333b3 64 20 6f 66 20 70 61 72 73 65 64 20 73 74 72 69  d of parsed stri
333b4 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  ng */.){.  int r
333b5 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  c;.  rc = sqlite
333b6 33 50 72 65 70 61 72 65 31 36 28 64 62 2c 7a 53  3Prepare16(db,zS
333b7 71 6c 2c 6e 42 79 74 65 73 2c 30 2c 70 70 53 74  ql,nBytes,0,ppSt
333b8 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73  mt,pzTail);.  as
333b9 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
333ba 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30  _OK || ppStmt==0
333bb 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29   || *ppStmt==0 )
333bc 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20 46 31  ;  /* VERIFY: F1
333bd 33 30 32 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e  3021 */.  return
333be 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50   rc;.}.SQLITE_AP
333bf 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72  I int sqlite3_pr
333c0 65 70 61 72 65 31 36 5f 76 32 28 0a 20 20 73 71  epare16_v2(.  sq
333c1 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
333c2 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
333c3 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a  ase handle. */ .
333c4 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53    const void *zS
333c5 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55  ql,         /* U
333c6 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 53 51  TF-16 encoded SQ
333c7 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a  L statement. */.
333c8 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20    int nBytes,   
333c9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
333ca 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e  ength of zSql in
333cb 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c   bytes. */.  sql
333cc 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
333cd 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41  mt,    /* OUT: A
333ce 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
333cf 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
333d0 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  nt */.  const vo
333d1 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  id **pzTail     
333d2 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66    /* OUT: End of
333d3 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a   parsed string *
333d4 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
333d5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 72 65   rc = sqlite3Pre
333d6 70 61 72 65 31 36 28 64 62 2c 7a 53 71 6c 2c 6e  pare16(db,zSql,n
333d7 42 79 74 65 73 2c 31 2c 70 70 53 74 6d 74 2c 70  Bytes,1,ppStmt,p
333d8 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74  zTail);.  assert
333d9 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
333da 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20  || ppStmt==0 || 
333db 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f  *ppStmt==0 );  /
333dc 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30 32 31  * VERIFY: F13021
333dd 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   */.  return rc;
333de 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
333df 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
333e0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
333e1 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 65 70 61  *** End of prepa
333e2 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  re.c ***********
333e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333e5 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
333e6 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73  *** Begin file s
333e7 65 6c 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  elect.c ********
333e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333ea 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
333eb 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
333ec 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
333ed 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
333ee 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
333ef 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
333f0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
333f1 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
333f2 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
333f3 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
333f4 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
333f5 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
333f6 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
333f7 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
333f8 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
333f9 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
333fa 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
333fb 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
333fc 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
333fd 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
333fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33401 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
33402 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
33403 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73   C code routines
33404 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64   that are called
33405 20 62 79 20 74 68 65 20 70 61 72 73 65 72 0a 2a   by the parser.*
33406 2a 20 74 6f 20 68 61 6e 64 6c 65 20 53 45 4c 45  * to handle SELE
33407 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  CT statements in
33408 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 0a 2f 2a   SQLite..*/.../*
33409 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 74  .** Delete all t
3340a 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20  he content of a 
3340b 53 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 65  Select structure
3340c 20 62 75 74 20 64 6f 20 6e 6f 74 20 64 65 61 6c   but do not deal
3340d 6c 6f 63 61 74 65 0a 2a 2a 20 74 68 65 20 73 65  locate.** the se
3340e 6c 65 63 74 20 73 74 72 75 63 74 75 72 65 20 69  lect structure i
3340f 74 73 65 6c 66 2e 0a 2a 2f 0a 73 74 61 74 69 63  tself..*/.static
33410 20 76 6f 69 64 20 63 6c 65 61 72 53 65 6c 65 63   void clearSelec
33411 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53  t(sqlite3 *db, S
33412 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 73 71 6c  elect *p){.  sql
33413 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
33414 74 65 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74  te(db, p->pEList
33415 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c  );.  sqlite3SrcL
33416 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  istDelete(db, p-
33417 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c 69 74 65  >pSrc);.  sqlite
33418 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
33419 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 73 71  p->pWhere);.  sq
3341a 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
3341b 65 74 65 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75  ete(db, p->pGrou
3341c 70 42 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  pBy);.  sqlite3E
3341d 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  xprDelete(db, p-
3341e 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20 73 71 6c  >pHaving);.  sql
3341f 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
33420 74 65 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72  te(db, p->pOrder
33421 42 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  By);.  sqlite3Se
33422 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
33423 2d 3e 70 50 72 69 6f 72 29 3b 0a 20 20 73 71 6c  ->pPrior);.  sql
33424 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
33425 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20  b, p->pLimit);. 
33426 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
33427 74 65 28 64 62 2c 20 70 2d 3e 70 4f 66 66 73 65  te(db, p->pOffse
33428 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  t);.}../*.** Ini
33429 74 69 61 6c 69 7a 65 20 61 20 53 65 6c 65 63 74  tialize a Select
3342a 44 65 73 74 20 73 74 72 75 63 74 75 72 65 2e 0a  Dest structure..
3342b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
3342c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
3342d 6c 65 63 74 44 65 73 74 49 6e 69 74 28 53 65 6c  lectDestInit(Sel
3342e 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 2c 20  ectDest *pDest, 
3342f 69 6e 74 20 65 44 65 73 74 2c 20 69 6e 74 20 69  int eDest, int i
33430 50 61 72 6d 29 7b 0a 20 20 70 44 65 73 74 2d 3e  Parm){.  pDest->
33431 65 44 65 73 74 20 3d 20 28 75 38 29 65 44 65 73  eDest = (u8)eDes
33432 74 3b 0a 20 20 70 44 65 73 74 2d 3e 69 53 44 50  t;.  pDest->iSDP
33433 61 72 6d 20 3d 20 69 50 61 72 6d 3b 0a 20 20 70  arm = iParm;.  p
33434 44 65 73 74 2d 3e 61 66 66 53 64 73 74 20 3d 20  Dest->affSdst = 
33435 30 3b 0a 20 20 70 44 65 73 74 2d 3e 69 53 64 73  0;.  pDest->iSds
33436 74 20 3d 20 30 3b 0a 20 20 70 44 65 73 74 2d 3e  t = 0;.  pDest->
33437 6e 53 64 73 74 20 3d 20 30 3b 0a 7d 0a 0a 0a 2f  nSdst = 0;.}.../
33438 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20  *.** Allocate a 
33439 6e 65 77 20 53 65 6c 65 63 74 20 73 74 72 75 63  new Select struc
3343a 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20  ture and return 
3343b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61  a pointer to tha
3343c 74 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a  t.** structure..
3343d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
3343e 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65  E Select *sqlite
3343f 33 53 65 6c 65 63 74 4e 65 77 28 0a 20 20 50 61  3SelectNew(.  Pa
33440 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
33441 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
33442 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
33443 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20  List *pEList,   
33444 20 20 2f 2a 20 77 68 69 63 68 20 63 6f 6c 75 6d    /* which colum
33445 6e 73 20 74 6f 20 69 6e 63 6c 75 64 65 20 69 6e  ns to include in
33446 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20   the result */. 
33447 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20   SrcList *pSrc, 
33448 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52         /* the FR
33449 4f 4d 20 63 6c 61 75 73 65 20 2d 2d 20 77 68 69  OM clause -- whi
3344a 63 68 20 74 61 62 6c 65 73 20 74 6f 20 73 63 61  ch tables to sca
3344b 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  n */.  Expr *pWh
3344c 65 72 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ere,         /* 
3344d 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
3344e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
3344f 70 47 72 6f 75 70 42 79 2c 20 20 20 2f 2a 20 74  pGroupBy,   /* t
33450 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  he GROUP BY clau
33451 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48  se */.  Expr *pH
33452 61 76 69 6e 67 2c 20 20 20 20 20 20 20 20 2f 2a  aving,        /*
33453 20 74 68 65 20 48 41 56 49 4e 47 20 63 6c 61 75   the HAVING clau
33454 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  se */.  ExprList
33455 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a   *pOrderBy,   /*
33456 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
33457 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73  ause */.  int is
33458 44 69 73 74 69 6e 63 74 2c 20 20 20 20 20 20 20  Distinct,       
33459 2f 2a 20 74 72 75 65 20 69 66 20 74 68 65 20 44  /* true if the D
3345a 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20  ISTINCT keyword 
3345b 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
3345c 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 20 20  Expr *pLimit,   
3345d 20 20 20 20 20 20 2f 2a 20 4c 49 4d 49 54 20 76        /* LIMIT v
3345e 61 6c 75 65 2e 20 20 4e 55 4c 4c 20 6d 65 61 6e  alue.  NULL mean
3345f 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20  s not used */.  
33460 45 78 70 72 20 2a 70 4f 66 66 73 65 74 20 20 20  Expr *pOffset   
33461 20 20 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20        /* OFFSET 
33462 76 61 6c 75 65 2e 20 20 4e 55 4c 4c 20 6d 65 61  value.  NULL mea
33463 6e 73 20 6e 6f 20 6f 66 66 73 65 74 20 2a 2f 0a  ns no offset */.
33464 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65  ){.  Select *pNe
33465 77 3b 0a 20 20 53 65 6c 65 63 74 20 73 74 61 6e  w;.  Select stan
33466 64 69 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  din;.  sqlite3 *
33467 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
33468 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
33469 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
3346a 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20  , sizeof(*pNew) 
3346b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  );.  assert( db-
3346c 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c  >mallocFailed ||
3346d 20 21 70 4f 66 66 73 65 74 20 7c 7c 20 70 4c 69   !pOffset || pLi
3346e 6d 69 74 20 29 3b 20 2f 2a 20 4f 46 46 53 45 54  mit ); /* OFFSET
3346f 20 69 6d 70 6c 69 65 73 20 4c 49 4d 49 54 20 2a   implies LIMIT *
33470 2f 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  /.  if( pNew==0 
33471 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  ){.    assert( d
33472 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
33473 29 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 26 73  );.    pNew = &s
33474 74 61 6e 64 69 6e 3b 0a 20 20 20 20 6d 65 6d 73  tandin;.    mems
33475 65 74 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65  et(pNew, 0, size
33476 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 7d 0a  of(*pNew));.  }.
33477 20 20 69 66 28 20 70 45 4c 69 73 74 3d 3d 30 20    if( pEList==0 
33478 29 7b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20  ){.    pEList = 
33479 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
3347a 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c  ppend(pParse, 0,
3347b 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c   sqlite3Expr(db,
3347c 54 4b 5f 41 4c 4c 2c 30 29 29 3b 0a 20 20 7d 0a  TK_ALL,0));.  }.
3347d 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d    pNew->pEList =
3347e 20 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 20 70   pEList;.  if( p
3347f 53 72 63 3d 3d 30 20 29 20 70 53 72 63 20 3d 20  Src==0 ) pSrc = 
33480 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
33481 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  ero(db, sizeof(*
33482 70 53 72 63 29 29 3b 0a 20 20 70 4e 65 77 2d 3e  pSrc));.  pNew->
33483 70 53 72 63 20 3d 20 70 53 72 63 3b 0a 20 20 70  pSrc = pSrc;.  p
33484 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d 20 70 57  New->pWhere = pW
33485 68 65 72 65 3b 0a 20 20 70 4e 65 77 2d 3e 70 47  here;.  pNew->pG
33486 72 6f 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42  roupBy = pGroupB
33487 79 3b 0a 20 20 70 4e 65 77 2d 3e 70 48 61 76 69  y;.  pNew->pHavi
33488 6e 67 20 3d 20 70 48 61 76 69 6e 67 3b 0a 20 20  ng = pHaving;.  
33489 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42 79 20 3d  pNew->pOrderBy =
3348a 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 70 4e 65   pOrderBy;.  pNe
3348b 77 2d 3e 73 65 6c 46 6c 61 67 73 20 3d 20 69 73  w->selFlags = is
3348c 44 69 73 74 69 6e 63 74 20 3f 20 53 46 5f 44 69  Distinct ? SF_Di
3348d 73 74 69 6e 63 74 20 3a 20 30 3b 0a 20 20 70 4e  stinct : 0;.  pN
3348e 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c 45  ew->op = TK_SELE
3348f 43 54 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 6d  CT;.  pNew->pLim
33490 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 70  it = pLimit;.  p
33491 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70  New->pOffset = p
33492 4f 66 66 73 65 74 3b 0a 20 20 61 73 73 65 72 74  Offset;.  assert
33493 28 20 70 4f 66 66 73 65 74 3d 3d 30 20 7c 7c 20  ( pOffset==0 || 
33494 70 4c 69 6d 69 74 21 3d 30 20 29 3b 0a 20 20 70  pLimit!=0 );.  p
33495 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  New->addrOpenEph
33496 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65  m[0] = -1;.  pNe
33497 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  w->addrOpenEphm[
33498 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d  1] = -1;.  pNew-
33499 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d  >addrOpenEphm[2]
3349a 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 64 62 2d   = -1;.  if( db-
3349b 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
3349c 7b 0a 20 20 20 20 63 6c 65 61 72 53 65 6c 65 63  {.    clearSelec
3349d 74 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20  t(db, pNew);.   
3349e 20 69 66 28 20 70 4e 65 77 21 3d 26 73 74 61 6e   if( pNew!=&stan
3349f 64 69 6e 20 29 20 73 71 6c 69 74 65 33 44 62 46  din ) sqlite3DbF
334a0 72 65 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20  ree(db, pNew);. 
334a1 20 20 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20 7d     pNew = 0;.  }
334a2 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
334a3 28 20 70 4e 65 77 2d 3e 70 53 72 63 21 3d 30 20  ( pNew->pSrc!=0 
334a4 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e  || pParse->nErr>
334a5 30 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  0 );.  }.  asser
334a6 74 28 20 70 4e 65 77 21 3d 26 73 74 61 6e 64 69  t( pNew!=&standi
334a7 6e 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e  n );.  return pN
334a8 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  ew;.}../*.** Del
334a9 65 74 65 20 74 68 65 20 67 69 76 65 6e 20 53 65  ete the given Se
334aa 6c 65 63 74 20 73 74 72 75 63 74 75 72 65 20 61  lect structure a
334ab 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 73 75  nd all of its su
334ac 62 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a  bstructures..*/.
334ad 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
334ae 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63  oid sqlite3Selec
334af 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20  tDelete(sqlite3 
334b0 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b  *db, Select *p){
334b1 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
334b2 63 6c 65 61 72 53 65 6c 65 63 74 28 64 62 2c 20  clearSelect(db, 
334b3 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  p);.    sqlite3D
334b4 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20  bFree(db, p);.  
334b5 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  }.}../*.** Given
334b6 20 31 20 74 6f 20 33 20 69 64 65 6e 74 69 66 69   1 to 3 identifi
334b7 65 72 73 20 70 72 65 63 65 65 64 69 6e 67 20 74  ers preceeding t
334b8 68 65 20 4a 4f 49 4e 20 6b 65 79 77 6f 72 64 2c  he JOIN keyword,
334b9 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 0a 2a   determine the.*
334ba 2a 20 74 79 70 65 20 6f 66 20 6a 6f 69 6e 2e 20  * type of join. 
334bb 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67   Return an integ
334bc 65 72 20 63 6f 6e 73 74 61 6e 74 20 74 68 61 74  er constant that
334bd 20 65 78 70 72 65 73 73 65 73 20 74 68 61 74 20   expresses that 
334be 74 79 70 65 0a 2a 2a 20 69 6e 20 74 65 72 6d 73  type.** in terms
334bf 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
334c0 67 20 62 69 74 20 76 61 6c 75 65 73 3a 0a 2a 2a  g bit values:.**
334c1 0a 2a 2a 20 20 20 20 20 4a 54 5f 49 4e 4e 45 52  .**     JT_INNER
334c2 0a 2a 2a 20 20 20 20 20 4a 54 5f 43 52 4f 53 53  .**     JT_CROSS
334c3 0a 2a 2a 20 20 20 20 20 4a 54 5f 4f 55 54 45 52  .**     JT_OUTER
334c4 0a 2a 2a 20 20 20 20 20 4a 54 5f 4e 41 54 55 52  .**     JT_NATUR
334c5 41 4c 0a 2a 2a 20 20 20 20 20 4a 54 5f 4c 45 46  AL.**     JT_LEF
334c6 54 0a 2a 2a 20 20 20 20 20 4a 54 5f 52 49 47 48  T.**     JT_RIGH
334c7 54 0a 2a 2a 0a 2a 2a 20 41 20 66 75 6c 6c 20 6f  T.**.** A full o
334c8 75 74 65 72 20 6a 6f 69 6e 20 69 73 20 74 68 65  uter join is the
334c9 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20   combination of 
334ca 4a 54 5f 4c 45 46 54 20 61 6e 64 20 4a 54 5f 52  JT_LEFT and JT_R
334cb 49 47 48 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  IGHT..**.** If a
334cc 6e 20 69 6c 6c 65 67 61 6c 20 6f 72 20 75 6e 73  n illegal or uns
334cd 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79  upported join ty
334ce 70 65 20 69 73 20 73 65 65 6e 2c 20 74 68 65 6e  pe is seen, then
334cf 20 73 74 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a   still return.**
334d0 20 61 20 6a 6f 69 6e 20 74 79 70 65 2c 20 62 75   a join type, bu
334d1 74 20 70 75 74 20 61 6e 20 65 72 72 6f 72 20 69  t put an error i
334d2 6e 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72  n the pParse str
334d3 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ucture..*/.SQLIT
334d4 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
334d5 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 50 61  lite3JoinType(Pa
334d6 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b  rse *pParse, Tok
334d7 65 6e 20 2a 70 41 2c 20 54 6f 6b 65 6e 20 2a 70  en *pA, Token *p
334d8 42 2c 20 54 6f 6b 65 6e 20 2a 70 43 29 7b 0a 20  B, Token *pC){. 
334d9 20 69 6e 74 20 6a 6f 69 6e 74 79 70 65 20 3d 20   int jointype = 
334da 30 3b 0a 20 20 54 6f 6b 65 6e 20 2a 61 70 41 6c  0;.  Token *apAl
334db 6c 5b 33 5d 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70  l[3];.  Token *p
334dc 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
334dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
334de 2a 20 20 20 30 31 32 33 34 35 36 37 38 39 20 31  *   0123456789 1
334df 32 33 34 35 36 37 38 39 20 31 32 33 34 35 36 37  23456789 1234567
334e0 38 39 20 31 32 33 20 2a 2f 0a 20 20 73 74 61 74  89 123 */.  stat
334e1 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4b  ic const char zK
334e2 65 79 54 65 78 74 5b 5d 20 3d 20 22 6e 61 74 75  eyText[] = "natu
334e3 72 61 6c 65 66 74 6f 75 74 65 72 69 67 68 74 66  raleftouterightf
334e4 75 6c 6c 69 6e 6e 65 72 63 72 6f 73 73 22 3b 0a  ullinnercross";.
334e5 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
334e6 74 72 75 63 74 20 7b 0a 20 20 20 20 75 38 20 69  truct {.    u8 i
334e7 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69  ;        /* Begi
334e8 6e 6e 69 6e 67 20 6f 66 20 6b 65 79 77 6f 72 64  nning of keyword
334e9 20 74 65 78 74 20 69 6e 20 7a 4b 65 79 54 65 78   text in zKeyTex
334ea 74 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 6e 43  t[] */.    u8 nC
334eb 68 61 72 3b 20 20 20 20 2f 2a 20 4c 65 6e 67 74  har;    /* Lengt
334ec 68 20 6f 66 20 74 68 65 20 6b 65 79 77 6f 72 64  h of the keyword
334ed 20 69 6e 20 63 68 61 72 61 63 74 65 72 73 20 2a   in characters *
334ee 2f 0a 20 20 20 20 75 38 20 63 6f 64 65 3b 20 20  /.    u8 code;  
334ef 20 20 20 2f 2a 20 4a 6f 69 6e 20 74 79 70 65 20     /* Join type 
334f0 6d 61 73 6b 20 2a 2f 0a 20 20 7d 20 61 4b 65 79  mask */.  } aKey
334f1 77 6f 72 64 5b 5d 20 3d 20 7b 0a 20 20 20 20 2f  word[] = {.    /
334f2 2a 20 6e 61 74 75 72 61 6c 20 2a 2f 20 7b 20 30  * natural */ { 0
334f3 2c 20 20 37 2c 20 4a 54 5f 4e 41 54 55 52 41 4c  ,  7, JT_NATURAL
334f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
334f5 7d 2c 0a 20 20 20 20 2f 2a 20 6c 65 66 74 20 20  },.    /* left  
334f6 20 20 2a 2f 20 7b 20 36 2c 20 20 34 2c 20 4a 54    */ { 6,  4, JT
334f7 5f 4c 45 46 54 7c 4a 54 5f 4f 55 54 45 52 20 20  _LEFT|JT_OUTER  
334f8 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f          },.    /
334f9 2a 20 6f 75 74 65 72 20 20 20 2a 2f 20 7b 20 31  * outer   */ { 1
334fa 30 2c 20 35 2c 20 4a 54 5f 4f 55 54 45 52 20 20  0, 5, JT_OUTER  
334fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
334fc 7d 2c 0a 20 20 20 20 2f 2a 20 72 69 67 68 74 20  },.    /* right 
334fd 20 20 2a 2f 20 7b 20 31 34 2c 20 35 2c 20 4a 54    */ { 14, 5, JT
334fe 5f 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20  _RIGHT|JT_OUTER 
334ff 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f          },.    /
33500 2a 20 66 75 6c 6c 20 20 20 20 2a 2f 20 7b 20 31  * full    */ { 1
33501 39 2c 20 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a 54  9, 4, JT_LEFT|JT
33502 5f 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20  _RIGHT|JT_OUTER 
33503 7d 2c 0a 20 20 20 20 2f 2a 20 69 6e 6e 65 72 20  },.    /* inner 
33504 20 20 2a 2f 20 7b 20 32 33 2c 20 35 2c 20 4a 54    */ { 23, 5, JT
33505 5f 49 4e 4e 45 52 20 20 20 20 20 20 20 20 20 20  _INNER          
33506 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f          },.    /
33507 2a 20 63 72 6f 73 73 20 20 20 2a 2f 20 7b 20 32  * cross   */ { 2
33508 38 2c 20 35 2c 20 4a 54 5f 49 4e 4e 45 52 7c 4a  8, 5, JT_INNER|J
33509 54 5f 43 52 4f 53 53 20 20 20 20 20 20 20 20 20  T_CROSS         
3350a 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 2c  },.  };.  int i,
3350b 20 6a 3b 0a 20 20 61 70 41 6c 6c 5b 30 5d 20 3d   j;.  apAll[0] =
3350c 20 70 41 3b 0a 20 20 61 70 41 6c 6c 5b 31 5d 20   pA;.  apAll[1] 
3350d 3d 20 70 42 3b 0a 20 20 61 70 41 6c 6c 5b 32 5d  = pB;.  apAll[2]
3350e 20 3d 20 70 43 3b 0a 20 20 66 6f 72 28 69 3d 30   = pC;.  for(i=0
3350f 3b 20 69 3c 33 20 26 26 20 61 70 41 6c 6c 5b 69  ; i<3 && apAll[i
33510 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d  ]; i++){.    p =
33511 20 61 70 41 6c 6c 5b 69 5d 3b 0a 20 20 20 20 66   apAll[i];.    f
33512 6f 72 28 6a 3d 30 3b 20 6a 3c 41 72 72 61 79 53  or(j=0; j<ArrayS
33513 69 7a 65 28 61 4b 65 79 77 6f 72 64 29 3b 20 6a  ize(aKeyword); j
33514 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
33515 2d 3e 6e 3d 3d 61 4b 65 79 77 6f 72 64 5b 6a 5d  ->n==aKeyword[j]
33516 2e 6e 43 68 61 72 20 0a 20 20 20 20 20 20 20 20  .nChar .        
33517 20 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e    && sqlite3StrN
33518 49 43 6d 70 28 28 63 68 61 72 2a 29 70 2d 3e 7a  ICmp((char*)p->z
33519 2c 20 26 7a 4b 65 79 54 65 78 74 5b 61 4b 65 79  , &zKeyText[aKey
3351a 77 6f 72 64 5b 6a 5d 2e 69 5d 2c 20 70 2d 3e 6e  word[j].i], p->n
3351b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
3351c 6a 6f 69 6e 74 79 70 65 20 7c 3d 20 61 4b 65 79  jointype |= aKey
3351d 77 6f 72 64 5b 6a 5d 2e 63 6f 64 65 3b 0a 20 20  word[j].code;.  
3351e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3351f 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 74     }.    }.    t
33520 65 73 74 63 61 73 65 28 20 6a 3d 3d 30 20 7c 7c  estcase( j==0 ||
33521 20 6a 3d 3d 31 20 7c 7c 20 6a 3d 3d 32 20 7c 7c   j==1 || j==2 ||
33522 20 6a 3d 3d 33 20 7c 7c 20 6a 3d 3d 34 20 7c 7c   j==3 || j==4 ||
33523 20 6a 3d 3d 35 20 7c 7c 20 6a 3d 3d 36 20 29 3b   j==5 || j==6 );
33524 0a 20 20 20 20 69 66 28 20 6a 3e 3d 41 72 72 61  .    if( j>=Arra
33525 79 53 69 7a 65 28 61 4b 65 79 77 6f 72 64 29 20  ySize(aKeyword) 
33526 29 7b 0a 20 20 20 20 20 20 6a 6f 69 6e 74 79 70  ){.      jointyp
33527 65 20 7c 3d 20 4a 54 5f 45 52 52 4f 52 3b 0a 20  e |= JT_ERROR;. 
33528 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
33529 7d 0a 20 20 7d 0a 20 20 69 66 28 0a 20 20 20 20  }.  }.  if(.    
3352a 20 28 6a 6f 69 6e 74 79 70 65 20 26 20 28 4a 54   (jointype & (JT
3352b 5f 49 4e 4e 45 52 7c 4a 54 5f 4f 55 54 45 52 29  _INNER|JT_OUTER)
3352c 29 3d 3d 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f  )==(JT_INNER|JT_
3352d 4f 55 54 45 52 29 20 7c 7c 0a 20 20 20 20 20 28  OUTER) ||.     (
3352e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 45 52  jointype & JT_ER
3352f 52 4f 52 29 21 3d 30 0a 20 20 29 7b 0a 20 20 20  ROR)!=0.  ){.   
33530 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 70   const char *zSp
33531 20 3d 20 22 20 22 3b 0a 20 20 20 20 61 73 73 65   = " ";.    asse
33532 72 74 28 20 70 42 21 3d 30 20 29 3b 0a 20 20 20  rt( pB!=0 );.   
33533 20 69 66 28 20 70 43 3d 3d 30 20 29 7b 20 7a 53   if( pC==0 ){ zS
33534 70 2b 2b 3b 20 7d 0a 20 20 20 20 73 71 6c 69 74  p++; }.    sqlit
33535 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
33536 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75  e, "unknown or u
33537 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20  nsupported join 
33538 74 79 70 65 3a 20 22 0a 20 20 20 20 20 20 20 22  type: ".       "
33539 25 54 20 25 54 25 73 25 54 22 2c 20 70 41 2c 20  %T %T%s%T", pA, 
3353a 70 42 2c 20 7a 53 70 2c 20 70 43 29 3b 0a 20 20  pB, zSp, pC);.  
3353b 20 20 6a 6f 69 6e 74 79 70 65 20 3d 20 4a 54 5f    jointype = JT_
3353c 49 4e 4e 45 52 3b 0a 20 20 7d 65 6c 73 65 20 69  INNER;.  }else i
3353d 66 28 20 28 6a 6f 69 6e 74 79 70 65 20 26 20 4a  f( (jointype & J
3353e 54 5f 4f 55 54 45 52 29 21 3d 30 20 0a 20 20 20  T_OUTER)!=0 .   
3353f 20 20 20 20 20 20 26 26 20 28 6a 6f 69 6e 74 79        && (jointy
33540 70 65 20 26 20 28 4a 54 5f 4c 45 46 54 7c 4a 54  pe & (JT_LEFT|JT
33541 5f 52 49 47 48 54 29 29 21 3d 4a 54 5f 4c 45 46  _RIGHT))!=JT_LEF
33542 54 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  T ){.    sqlite3
33543 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
33544 20 0a 20 20 20 20 20 20 22 52 49 47 48 54 20 61   .      "RIGHT a
33545 6e 64 20 46 55 4c 4c 20 4f 55 54 45 52 20 4a 4f  nd FULL OUTER JO
33546 49 4e 73 20 61 72 65 20 6e 6f 74 20 63 75 72 72  INs are not curr
33547 65 6e 74 6c 79 20 73 75 70 70 6f 72 74 65 64 22  ently supported"
33548 29 3b 0a 20 20 20 20 6a 6f 69 6e 74 79 70 65 20  );.    jointype 
33549 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d 0a  = JT_INNER;.  }.
3354a 20 20 72 65 74 75 72 6e 20 6a 6f 69 6e 74 79 70    return jointyp
3354b 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  e;.}../*.** Retu
3354c 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  rn the index of 
3354d 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 74 61  a column in a ta
3354e 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 2d 31 20  ble.  Return -1 
3354f 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a  if the column.**
33550 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 65   is not containe
33551 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a  d in the table..
33552 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
33553 6c 75 6d 6e 49 6e 64 65 78 28 54 61 62 6c 65 20  lumnIndex(Table 
33554 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68 61  *pTab, const cha
33555 72 20 2a 7a 43 6f 6c 29 7b 0a 20 20 69 6e 74 20  r *zCol){.  int 
33556 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
33557 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
33558 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
33559 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61  3StrICmp(pTab->a
3355a 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 43  Col[i].zName, zC
3355b 6f 6c 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ol)==0 ) return 
3355c 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  i;.  }.  return 
3355d 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61  -1;.}../*.** Sea
3355e 72 63 68 20 74 68 65 20 66 69 72 73 74 20 4e 20  rch the first N 
3355f 74 61 62 6c 65 73 20 69 6e 20 70 53 72 63 2c 20  tables in pSrc, 
33560 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67  from left to rig
33561 68 74 2c 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  ht, looking for 
33562 61 0a 2a 2a 20 74 61 62 6c 65 20 74 68 61 74 20  a.** table that 
33563 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d  has a column nam
33564 65 64 20 7a 43 6f 6c 2e 20 20 0a 2a 2a 0a 2a 2a  ed zCol.  .**.**
33565 20 57 68 65 6e 20 66 6f 75 6e 64 2c 20 73 65 74   When found, set
33566 20 2a 70 69 54 61 62 20 61 6e 64 20 2a 70 69 43   *piTab and *piC
33567 6f 6c 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ol to the table 
33568 69 6e 64 65 78 20 61 6e 64 20 63 6f 6c 75 6d 6e  index and column
33569 20 69 6e 64 65 78 0a 2a 2a 20 6f 66 20 74 68 65   index.** of the
3356a 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e   matching column
3356b 20 61 6e 64 20 72 65 74 75 72 6e 20 54 52 55 45   and return TRUE
3356c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 74 20 66  ..**.** If not f
3356d 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 46 41 4c  ound, return FAL
3356e 53 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  SE..*/.static in
3356f 74 20 74 61 62 6c 65 41 6e 64 43 6f 6c 75 6d 6e  t tableAndColumn
33570 49 6e 64 65 78 28 0a 20 20 53 72 63 4c 69 73 74  Index(.  SrcList
33571 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 2f 2a   *pSrc,       /*
33572 20 41 72 72 61 79 20 6f 66 20 74 61 62 6c 65 73   Array of tables
33573 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20 20   to search */.  
33574 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20 20  int N,          
33575 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
33576 66 20 74 61 62 6c 65 73 20 69 6e 20 70 53 72 63  f tables in pSrc
33577 2d 3e 61 5b 5d 20 74 6f 20 73 65 61 72 63 68 20  ->a[] to search 
33578 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
33579 2a 7a 43 6f 6c 2c 20 20 20 20 2f 2a 20 4e 61 6d  *zCol,    /* Nam
3357a 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  e of the column 
3357b 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66  we are looking f
3357c 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 54  or */.  int *piT
3357d 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ab,          /* 
3357e 57 72 69 74 65 20 69 6e 64 65 78 20 6f 66 20 70  Write index of p
3357f 53 72 63 2d 3e 61 5b 5d 20 68 65 72 65 20 2a 2f  Src->a[] here */
33580 0a 20 20 69 6e 74 20 2a 70 69 43 6f 6c 20 20 20  .  int *piCol   
33581 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
33582 20 69 6e 64 65 78 20 6f 66 20 70 53 72 63 2d 3e   index of pSrc->
33583 61 5b 2a 70 69 54 61 62 5d 2e 70 54 61 62 2d 3e  a[*piTab].pTab->
33584 61 43 6f 6c 5b 5d 20 68 65 72 65 20 2a 2f 0a 29  aCol[] here */.)
33585 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
33586 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
33587 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 74 61 62  looping over tab
33588 6c 65 73 20 69 6e 20 70 53 72 63 20 2a 2f 0a 20  les in pSrc */. 
33589 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20   int iCol;      
3358a 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
3358b 66 20 63 6f 6c 75 6d 6e 20 6d 61 74 63 68 69 6e  f column matchin
3358c 67 20 7a 43 6f 6c 20 2a 2f 0a 0a 20 20 61 73 73  g zCol */..  ass
3358d 65 72 74 28 20 28 70 69 54 61 62 3d 3d 30 29 3d  ert( (piTab==0)=
3358e 3d 28 70 69 43 6f 6c 3d 3d 30 29 20 29 3b 20 20  =(piCol==0) );  
3358f 2f 2a 20 42 6f 74 68 20 6f 72 20 6e 65 69 74 68  /* Both or neith
33590 65 72 20 61 72 65 20 4e 55 4c 4c 20 2a 2f 0a 20  er are NULL */. 
33591 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 3b 20 69   for(i=0; i<N; i
33592 2b 2b 29 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20  ++){.    iCol = 
33593 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 53 72 63  columnIndex(pSrc
33594 2d 3e 61 5b 69 5d 2e 70 54 61 62 2c 20 7a 43 6f  ->a[i].pTab, zCo
33595 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c  l);.    if( iCol
33596 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  >=0 ){.      if(
33597 20 70 69 54 61 62 20 29 7b 0a 20 20 20 20 20 20   piTab ){.      
33598 20 20 2a 70 69 54 61 62 20 3d 20 69 3b 0a 20 20    *piTab = i;.  
33599 20 20 20 20 20 20 2a 70 69 43 6f 6c 20 3d 20 69        *piCol = i
3359a 43 6f 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Col;.      }.   
3359b 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
3359c 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
3359d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  0;.}../*.** This
3359e 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
3359f 64 20 74 6f 20 61 64 64 20 74 65 72 6d 73 20 69  d to add terms i
335a0 6d 70 6c 69 65 64 20 62 79 20 4a 4f 49 4e 20 73  mplied by JOIN s
335a1 79 6e 74 61 78 20 74 6f 20 74 68 65 0a 2a 2a 20  yntax to the.** 
335a2 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78 70  WHERE clause exp
335a3 72 65 73 73 69 6f 6e 20 6f 66 20 61 20 53 45 4c  ression of a SEL
335a4 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  ECT statement. T
335a5 68 65 20 6e 65 77 20 74 65 72 6d 2c 20 77 68 69  he new term, whi
335a6 63 68 0a 2a 2a 20 69 73 20 41 4e 44 65 64 20 77  ch.** is ANDed w
335a7 69 74 68 20 74 68 65 20 65 78 69 73 74 69 6e 67  ith the existing
335a8 20 57 48 45 52 45 20 63 6c 61 75 73 65 2c 20 69   WHERE clause, i
335a9 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a  s of the form:.*
335aa 2a 0a 2a 2a 20 20 20 20 28 74 61 62 31 2e 63 6f  *.**    (tab1.co
335ab 6c 31 20 3d 20 74 61 62 32 2e 63 6f 6c 32 29 0a  l1 = tab2.col2).
335ac 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 74 61 62 31  **.** where tab1
335ad 20 69 73 20 74 68 65 20 69 53 72 63 27 74 68 20   is the iSrc'th 
335ae 74 61 62 6c 65 20 69 6e 20 53 72 63 4c 69 73 74  table in SrcList
335af 20 70 53 72 63 20 61 6e 64 20 74 61 62 32 20 69   pSrc and tab2 i
335b0 73 20 74 68 65 20 0a 2a 2a 20 28 69 53 72 63 2b  s the .** (iSrc+
335b1 31 29 27 74 68 2e 20 43 6f 6c 75 6d 6e 20 63 6f  1)'th. Column co
335b2 6c 31 20 69 73 20 63 6f 6c 75 6d 6e 20 69 43 6f  l1 is column iCo
335b3 6c 4c 65 66 74 20 6f 66 20 74 61 62 31 2c 20 61  lLeft of tab1, a
335b4 6e 64 20 63 6f 6c 32 20 69 73 0a 2a 2a 20 63 6f  nd col2 is.** co
335b5 6c 75 6d 6e 20 69 43 6f 6c 52 69 67 68 74 20 6f  lumn iColRight o
335b6 66 20 74 61 62 32 2e 0a 2a 2f 0a 73 74 61 74 69  f tab2..*/.stati
335b7 63 20 76 6f 69 64 20 61 64 64 57 68 65 72 65 54  c void addWhereT
335b8 65 72 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50  erm(.  Parse *pP
335b9 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
335ba 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
335bb 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  g context */.  S
335bc 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20  rcList *pSrc,   
335bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
335be 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73  * List of tables
335bf 20 69 6e 20 46 52 4f 4d 20 63 6c 61 75 73 65 20   in FROM clause 
335c0 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66 74 2c 20  */.  int iLeft, 
335c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
335c2 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
335c3 20 66 69 72 73 74 20 74 61 62 6c 65 20 74 6f 20   first table to 
335c4 6a 6f 69 6e 20 69 6e 20 70 53 72 63 20 2a 2f 0a  join in pSrc */.
335c5 20 20 69 6e 74 20 69 43 6f 6c 4c 65 66 74 2c 20    int iColLeft, 
335c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
335c7 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f    /* Index of co
335c8 6c 75 6d 6e 20 69 6e 20 66 69 72 73 74 20 74 61  lumn in first ta
335c9 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 69  ble */.  int iRi
335ca 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ght,            
335cb 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
335cc 78 20 6f 66 20 73 65 63 6f 6e 64 20 74 61 62 6c  x of second tabl
335cd 65 20 69 6e 20 70 53 72 63 20 2a 2f 0a 20 20 69  e in pSrc */.  i
335ce 6e 74 20 69 43 6f 6c 52 69 67 68 74 2c 20 20 20  nt iColRight,   
335cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
335d0 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d  * Index of colum
335d1 6e 20 69 6e 20 73 65 63 6f 6e 64 20 74 61 62 6c  n in second tabl
335d2 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 75 74  e */.  int isOut
335d3 65 72 4a 6f 69 6e 2c 20 20 20 20 20 20 20 20 20  erJoin,         
335d4 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
335d5 66 20 74 68 69 73 20 69 73 20 61 6e 20 4f 55 54  f this is an OUT
335d6 45 52 20 6a 6f 69 6e 20 2a 2f 0a 20 20 45 78 70  ER join */.  Exp
335d7 72 20 2a 2a 70 70 57 68 65 72 65 20 20 20 20 20  r **ppWhere     
335d8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
335d9 49 4e 2f 4f 55 54 3a 20 54 68 65 20 57 48 45 52  IN/OUT: The WHER
335da 45 20 63 6c 61 75 73 65 20 74 6f 20 61 64 64 20  E clause to add 
335db 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  to */.){.  sqlit
335dc 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
335dd 3e 64 62 3b 0a 20 20 45 78 70 72 20 2a 70 45 31  >db;.  Expr *pE1
335de 3b 0a 20 20 45 78 70 72 20 2a 70 45 32 3b 0a 20  ;.  Expr *pE2;. 
335df 20 45 78 70 72 20 2a 70 45 71 3b 0a 0a 20 20 61   Expr *pEq;..  a
335e0 73 73 65 72 74 28 20 69 4c 65 66 74 3c 69 52 69  ssert( iLeft<iRi
335e1 67 68 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ght );.  assert(
335e2 20 70 53 72 63 2d 3e 6e 53 72 63 3e 69 52 69 67   pSrc->nSrc>iRig
335e3 68 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ht );.  assert( 
335e4 70 53 72 63 2d 3e 61 5b 69 4c 65 66 74 5d 2e 70  pSrc->a[iLeft].p
335e5 54 61 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Tab );.  assert(
335e6 20 70 53 72 63 2d 3e 61 5b 69 52 69 67 68 74 5d   pSrc->a[iRight]
335e7 2e 70 54 61 62 20 29 3b 0a 0a 20 20 70 45 31 20  .pTab );..  pE1 
335e8 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 43  = sqlite3CreateC
335e9 6f 6c 75 6d 6e 45 78 70 72 28 64 62 2c 20 70 53  olumnExpr(db, pS
335ea 72 63 2c 20 69 4c 65 66 74 2c 20 69 43 6f 6c 4c  rc, iLeft, iColL
335eb 65 66 74 29 3b 0a 20 20 70 45 32 20 3d 20 73 71  eft);.  pE2 = sq
335ec 6c 69 74 65 33 43 72 65 61 74 65 43 6f 6c 75 6d  lite3CreateColum
335ed 6e 45 78 70 72 28 64 62 2c 20 70 53 72 63 2c 20  nExpr(db, pSrc, 
335ee 69 52 69 67 68 74 2c 20 69 43 6f 6c 52 69 67 68  iRight, iColRigh
335ef 74 29 3b 0a 0a 20 20 70 45 71 20 3d 20 73 71 6c  t);..  pEq = sql
335f0 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
335f1 2c 20 54 4b 5f 45 51 2c 20 70 45 31 2c 20 70 45  , TK_EQ, pE1, pE
335f2 32 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 45 71  2, 0);.  if( pEq
335f3 20 26 26 20 69 73 4f 75 74 65 72 4a 6f 69 6e 20   && isOuterJoin 
335f4 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72  ){.    ExprSetPr
335f5 6f 70 65 72 74 79 28 70 45 71 2c 20 45 50 5f 46  operty(pEq, EP_F
335f6 72 6f 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20 61 73  romJoin);.    as
335f7 73 65 72 74 28 20 21 45 78 70 72 48 61 73 41 6e  sert( !ExprHasAn
335f8 79 50 72 6f 70 65 72 74 79 28 70 45 71 2c 20 45  yProperty(pEq, E
335f9 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52  P_TokenOnly|EP_R
335fa 65 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20 45  educed) );.    E
335fb 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 6c  xprSetIrreducibl
335fc 65 28 70 45 71 29 3b 0a 20 20 20 20 70 45 71 2d  e(pEq);.    pEq-
335fd 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65  >iRightJoinTable
335fe 20 3d 20 28 69 31 36 29 70 45 32 2d 3e 69 54 61   = (i16)pE2->iTa
335ff 62 6c 65 3b 0a 20 20 7d 0a 20 20 2a 70 70 57 68  ble;.  }.  *ppWh
33600 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ere = sqlite3Exp
33601 72 41 6e 64 28 64 62 2c 20 2a 70 70 57 68 65 72  rAnd(db, *ppWher
33602 65 2c 20 70 45 71 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  e, pEq);.}../*.*
33603 2a 20 53 65 74 20 74 68 65 20 45 50 5f 46 72 6f  * Set the EP_Fro
33604 6d 4a 6f 69 6e 20 70 72 6f 70 65 72 74 79 20 6f  mJoin property o
33605 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74  n all terms of t
33606 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73  he given express
33607 69 6f 6e 2e 0a 2a 2a 20 41 6e 64 20 73 65 74 20  ion..** And set 
33608 74 68 65 20 45 78 70 72 2e 69 52 69 67 68 74 4a  the Expr.iRightJ
33609 6f 69 6e 54 61 62 6c 65 20 74 6f 20 69 54 61 62  oinTable to iTab
3360a 6c 65 20 66 6f 72 20 65 76 65 72 79 20 74 65 72  le for every ter
3360b 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 65 78 70 72  m in the.** expr
3360c 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ession..**.** Th
3360d 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72  e EP_FromJoin pr
3360e 6f 70 65 72 74 79 20 69 73 20 75 73 65 64 20 6f  operty is used o
3360f 6e 20 74 65 72 6d 73 20 6f 66 20 61 6e 20 65 78  n terms of an ex
33610 70 72 65 73 73 69 6f 6e 20 74 6f 20 74 65 6c 6c  pression to tell
33611 0a 2a 2a 20 74 68 65 20 4c 45 46 54 20 4f 55 54  .** the LEFT OUT
33612 45 52 20 4a 4f 49 4e 20 70 72 6f 63 65 73 73 69  ER JOIN processi
33613 6e 67 20 6c 6f 67 69 63 20 74 68 61 74 20 74 68  ng logic that th
33614 69 73 20 74 65 72 6d 20 69 73 20 70 61 72 74 20  is term is part 
33615 6f 66 20 74 68 65 0a 2a 2a 20 6a 6f 69 6e 20 72  of the.** join r
33616 65 73 74 72 69 63 74 69 6f 6e 20 73 70 65 63 69  estriction speci
33617 66 69 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f  fied in the ON o
33618 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 61  r USING clause a
33619 6e 64 20 6e 6f 74 20 61 20 70 61 72 74 0a 2a 2a  nd not a part.**
3361a 20 6f 66 20 74 68 65 20 6d 6f 72 65 20 67 65 6e   of the more gen
3361b 65 72 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73  eral WHERE claus
3361c 65 2e 20 20 54 68 65 73 65 20 74 65 72 6d 73 20  e.  These terms 
3361d 61 72 65 20 6d 6f 76 65 64 20 6f 76 65 72 20 74  are moved over t
3361e 6f 20 74 68 65 0a 2a 2a 20 57 48 45 52 45 20 63  o the.** WHERE c
3361f 6c 61 75 73 65 20 64 75 72 69 6e 67 20 6a 6f 69  lause during joi
33620 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 62 75 74  n processing but
33621 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 6d 65   we need to reme
33622 6d 62 65 72 20 74 68 61 74 20 74 68 65 79 0a 2a  mber that they.*
33623 2a 20 6f 72 69 67 69 6e 61 74 65 64 20 69 6e 20  * originated in 
33624 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20  the ON or USING 
33625 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  clause..**.** Th
33626 65 20 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69  e Expr.iRightJoi
33627 6e 54 61 62 6c 65 20 74 65 6c 6c 73 20 74 68 65  nTable tells the
33628 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72   WHERE clause pr
33629 6f 63 65 73 73 69 6e 67 20 74 68 61 74 20 74 68  ocessing that th
3362a 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  e.** expression 
3362b 64 65 70 65 6e 64 73 20 6f 6e 20 74 61 62 6c 65  depends on table
3362c 20 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65   iRightJoinTable
3362d 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 74 61   even if that ta
3362e 62 6c 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 78  ble is not.** ex
3362f 70 6c 69 63 69 74 6c 79 20 6d 65 6e 74 69 6f 6e  plicitly mention
33630 65 64 20 69 6e 20 74 68 65 20 65 78 70 72 65 73  ed in the expres
33631 73 69 6f 6e 2e 20 20 54 68 61 74 20 69 6e 66 6f  sion.  That info
33632 72 6d 61 74 69 6f 6e 20 69 73 20 6e 65 65 64 65  rmation is neede
33633 64 0a 2a 2a 20 66 6f 72 20 63 61 73 65 73 20 6c  d.** for cases l
33634 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
33635 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
33636 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32   t1 LEFT JOIN t2
33637 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 62 20 41 4e   ON t1.a=t2.b AN
33638 44 20 74 31 2e 78 3d 35 0a 2a 2a 0a 2a 2a 20 54  D t1.x=5.**.** T
33639 68 65 20 77 68 65 72 65 20 63 6c 61 75 73 65 20  he where clause 
3363a 6e 65 65 64 73 20 74 6f 20 64 65 66 65 72 20 74  needs to defer t
3363b 68 65 20 68 61 6e 64 6c 69 6e 67 20 6f 66 20 74  he handling of t
3363c 68 65 20 74 31 2e 78 3d 35 0a 2a 2a 20 74 65 72  he t1.x=5.** ter
3363d 6d 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68  m until after th
3363e 65 20 74 32 20 6c 6f 6f 70 20 6f 66 20 74 68 65  e t2 loop of the
3363f 20 6a 6f 69 6e 2e 20 20 49 6e 20 74 68 61 74 20   join.  In that 
33640 77 61 79 2c 20 61 0a 2a 2a 20 4e 55 4c 4c 20 74  way, a.** NULL t
33641 32 20 72 6f 77 20 77 69 6c 6c 20 62 65 20 69 6e  2 row will be in
33642 73 65 72 74 65 64 20 77 68 65 6e 65 76 65 72 20  serted whenever 
33643 74 31 2e 78 21 3d 35 2e 20 20 49 66 20 77 65 20  t1.x!=5.  If we 
33644 64 6f 20 6e 6f 74 0a 2a 2a 20 64 65 66 65 72 20  do not.** defer 
33645 74 68 65 20 68 61 6e 64 6c 69 6e 67 20 6f 66 20  the handling of 
33646 74 31 2e 78 3d 35 2c 20 69 74 20 77 69 6c 6c 20  t1.x=5, it will 
33647 62 65 20 70 72 6f 63 65 73 73 65 64 20 69 6d 6d  be processed imm
33648 65 64 69 61 74 65 6c 79 0a 2a 2a 20 61 66 74 65  ediately.** afte
33649 72 20 74 68 65 20 74 31 20 6c 6f 6f 70 20 61 6e  r the t1 loop an
3364a 64 20 72 6f 77 73 20 77 69 74 68 20 74 31 2e 78  d rows with t1.x
3364b 21 3d 35 20 77 69 6c 6c 20 6e 65 76 65 72 20 61  !=5 will never a
3364c 70 70 65 61 72 20 69 6e 0a 2a 2a 20 74 68 65 20  ppear in.** the 
3364d 6f 75 74 70 75 74 2c 20 77 68 69 63 68 20 69 73  output, which is
3364e 20 69 6e 63 6f 72 72 65 63 74 2e 0a 2a 2f 0a 73   incorrect..*/.s
3364f 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 4a 6f  tatic void setJo
33650 69 6e 45 78 70 72 28 45 78 70 72 20 2a 70 2c 20  inExpr(Expr *p, 
33651 69 6e 74 20 69 54 61 62 6c 65 29 7b 0a 20 20 77  int iTable){.  w
33652 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 45  hile( p ){.    E
33653 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70  xprSetProperty(p
33654 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a  , EP_FromJoin);.
33655 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
33656 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
33657 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c  p, EP_TokenOnly|
33658 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20  EP_Reduced) );. 
33659 20 20 20 45 78 70 72 53 65 74 49 72 72 65 64 75     ExprSetIrredu
3365a 63 69 62 6c 65 28 70 29 3b 0a 20 20 20 20 70 2d  cible(p);.    p-
3365b 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65  >iRightJoinTable
3365c 20 3d 20 28 69 31 36 29 69 54 61 62 6c 65 3b 0a   = (i16)iTable;.
3365d 20 20 20 20 73 65 74 4a 6f 69 6e 45 78 70 72 28      setJoinExpr(
3365e 70 2d 3e 70 4c 65 66 74 2c 20 69 54 61 62 6c 65  p->pLeft, iTable
3365f 29 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 52  );.    p = p->pR
33660 69 67 68 74 3b 0a 20 20 7d 20 0a 7d 0a 0a 2f 2a  ight;.  } .}../*
33661 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
33662 20 70 72 6f 63 65 73 73 65 73 20 74 68 65 20 6a   processes the j
33663 6f 69 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  oin information 
33664 66 6f 72 20 61 20 53 45 4c 45 43 54 20 73 74 61  for a SELECT sta
33665 74 65 6d 65 6e 74 2e 0a 2a 2a 20 4f 4e 20 61 6e  tement..** ON an
33666 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20  d USING clauses 
33667 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e  are converted in
33668 74 6f 20 65 78 74 72 61 20 74 65 72 6d 73 20 6f  to extra terms o
33669 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
3366a 73 65 2e 0a 2a 2a 20 4e 41 54 55 52 41 4c 20 6a  se..** NATURAL j
3366b 6f 69 6e 73 20 61 6c 73 6f 20 63 72 65 61 74 65  oins also create
3366c 20 65 78 74 72 61 20 57 48 45 52 45 20 63 6c 61   extra WHERE cla
3366d 75 73 65 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a  use terms..**.**
3366e 20 54 68 65 20 74 65 72 6d 73 20 6f 66 20 61 20   The terms of a 
3366f 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 72 65 20  FROM clause are 
33670 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65  contained in the
33671 20 53 65 6c 65 63 74 2e 70 53 72 63 20 73 74 72   Select.pSrc str
33672 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 6c  ucture..** The l
33673 65 66 74 20 6d 6f 73 74 20 74 61 62 6c 65 20 69  eft most table i
33674 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72  s the first entr
33675 79 20 69 6e 20 53 65 6c 65 63 74 2e 70 53 72 63  y in Select.pSrc
33676 2e 20 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73  .  The right-mos
33677 74 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 74 68  t.** table is th
33678 65 20 6c 61 73 74 20 65 6e 74 72 79 2e 20 20 54  e last entry.  T
33679 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72  he join operator
3367a 20 69 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20   is held in the 
3367b 65 6e 74 72 79 20 74 6f 0a 2a 2a 20 74 68 65 20  entry to.** the 
3367c 6c 65 66 74 2e 20 20 54 68 75 73 20 65 6e 74 72  left.  Thus entr
3367d 79 20 30 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  y 0 contains the
3367e 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20 66   join operator f
3367f 6f 72 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77  or the join betw
33680 65 65 6e 0a 2a 2a 20 65 6e 74 72 69 65 73 20 30  een.** entries 0
33681 20 61 6e 64 20 31 2e 20 20 41 6e 79 20 4f 4e 20   and 1.  Any ON 
33682 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73  or USING clauses
33683 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
33684 20 74 68 65 20 6a 6f 69 6e 20 61 72 65 0a 2a 2a   the join are.**
33685 20 61 6c 73 6f 20 61 74 74 61 63 68 65 64 20 74   also attached t
33686 6f 20 74 68 65 20 6c 65 66 74 20 65 6e 74 72 79  o the left entry
33687 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
33688 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
33689 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   number of error
3368a 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a  s encountered..*
3368b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
3368c 69 74 65 50 72 6f 63 65 73 73 4a 6f 69 6e 28 50  iteProcessJoin(P
3368d 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65  arse *pParse, Se
3368e 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c  lect *p){.  SrcL
3368f 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20 20  ist *pSrc;      
33690 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
33691 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  ll tables in the
33692 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
33693 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20    int i, j;     
33694 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33695 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
33696 72 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53  rs */.  struct S
33697 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 65  rcList_item *pLe
33698 66 74 3b 20 20 20 20 20 2f 2a 20 4c 65 66 74 20  ft;     /* Left 
33699 74 61 62 6c 65 20 62 65 69 6e 67 20 6a 6f 69 6e  table being join
3369a 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53  ed */.  struct S
3369b 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 52 69  rcList_item *pRi
3369c 67 68 74 3b 20 20 20 20 2f 2a 20 52 69 67 68 74  ght;    /* Right
3369d 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6a 6f 69   table being joi
3369e 6e 65 64 20 2a 2f 0a 0a 20 20 70 53 72 63 20 3d  ned */..  pSrc =
3369f 20 70 2d 3e 70 53 72 63 3b 0a 20 20 70 4c 65 66   p->pSrc;.  pLef
336a0 74 20 3d 20 26 70 53 72 63 2d 3e 61 5b 30 5d 3b  t = &pSrc->a[0];
336a1 0a 20 20 70 52 69 67 68 74 20 3d 20 26 70 4c 65  .  pRight = &pLe
336a2 66 74 5b 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30  ft[1];.  for(i=0
336a3 3b 20 69 3c 70 53 72 63 2d 3e 6e 53 72 63 2d 31  ; i<pSrc->nSrc-1
336a4 3b 20 69 2b 2b 2c 20 70 52 69 67 68 74 2b 2b 2c  ; i++, pRight++,
336a5 20 70 4c 65 66 74 2b 2b 29 7b 0a 20 20 20 20 54   pLeft++){.    T
336a6 61 62 6c 65 20 2a 70 4c 65 66 74 54 61 62 20 3d  able *pLeftTab =
336a7 20 70 4c 65 66 74 2d 3e 70 54 61 62 3b 0a 20 20   pLeft->pTab;.  
336a8 20 20 54 61 62 6c 65 20 2a 70 52 69 67 68 74 54    Table *pRightT
336a9 61 62 20 3d 20 70 52 69 67 68 74 2d 3e 70 54 61  ab = pRight->pTa
336aa 62 3b 0a 20 20 20 20 69 6e 74 20 69 73 4f 75 74  b;.    int isOut
336ab 65 72 3b 0a 0a 20 20 20 20 69 66 28 20 4e 45 56  er;..    if( NEV
336ac 45 52 28 70 4c 65 66 74 54 61 62 3d 3d 30 20 7c  ER(pLeftTab==0 |
336ad 7c 20 70 52 69 67 68 74 54 61 62 3d 3d 30 29 20  | pRightTab==0) 
336ae 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
336af 69 73 4f 75 74 65 72 20 3d 20 28 70 52 69 67 68  isOuter = (pRigh
336b0 74 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54  t->jointype & JT
336b1 5f 4f 55 54 45 52 29 21 3d 30 3b 0a 0a 20 20 20  _OUTER)!=0;..   
336b2 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 4e 41 54   /* When the NAT
336b3 55 52 41 4c 20 6b 65 79 77 6f 72 64 20 69 73 20  URAL keyword is 
336b4 70 72 65 73 65 6e 74 2c 20 61 64 64 20 57 48 45  present, add WHE
336b5 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20  RE clause terms 
336b6 66 6f 72 0a 20 20 20 20 2a 2a 20 65 76 65 72 79  for.    ** every
336b7 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65   column that the
336b8 20 74 77 6f 20 74 61 62 6c 65 73 20 68 61 76 65   two tables have
336b9 20 69 6e 20 63 6f 6d 6d 6f 6e 2e 0a 20 20 20 20   in common..    
336ba 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69 67 68  */.    if( pRigh
336bb 74 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54  t->jointype & JT
336bc 5f 4e 41 54 55 52 41 4c 20 29 7b 0a 20 20 20 20  _NATURAL ){.    
336bd 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70 4f    if( pRight->pO
336be 6e 20 7c 7c 20 70 52 69 67 68 74 2d 3e 70 55 73  n || pRight->pUs
336bf 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ing ){.        s
336c0 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
336c1 50 61 72 73 65 2c 20 22 61 20 4e 41 54 55 52 41  Parse, "a NATURA
336c2 4c 20 6a 6f 69 6e 20 6d 61 79 20 6e 6f 74 20 68  L join may not h
336c3 61 76 65 20 22 0a 20 20 20 20 20 20 20 20 20 20  ave ".          
336c4 20 22 61 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47   "an ON or USING
336c5 20 63 6c 61 75 73 65 22 2c 20 30 29 3b 0a 20 20   clause", 0);.  
336c6 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
336c7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f        }.      fo
336c8 72 28 6a 3d 30 3b 20 6a 3c 70 52 69 67 68 74 54  r(j=0; j<pRightT
336c9 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a  ab->nCol; j++){.
336ca 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e          char *zN
336cb 61 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  ame;   /* Name o
336cc 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  f column in the 
336cd 72 69 67 68 74 20 74 61 62 6c 65 20 2a 2f 0a 20  right table */. 
336ce 20 20 20 20 20 20 20 69 6e 74 20 69 4c 65 66 74         int iLeft
336cf 3b 20 20 20 20 20 2f 2a 20 4d 61 74 63 68 69 6e  ;     /* Matchin
336d0 67 20 6c 65 66 74 20 74 61 62 6c 65 20 2a 2f 0a  g left table */.
336d1 20 20 20 20 20 20 20 20 69 6e 74 20 69 4c 65 66          int iLef
336d2 74 43 6f 6c 3b 20 20 2f 2a 20 4d 61 74 63 68 69  tCol;  /* Matchi
336d3 6e 67 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  ng column in the
336d4 20 6c 65 66 74 20 74 61 62 6c 65 20 2a 2f 0a 0a   left table */..
336d5 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20          zName = 
336d6 70 52 69 67 68 74 54 61 62 2d 3e 61 43 6f 6c 5b  pRightTab->aCol[
336d7 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  j].zName;.      
336d8 20 20 69 66 28 20 74 61 62 6c 65 41 6e 64 43 6f    if( tableAndCo
336d9 6c 75 6d 6e 49 6e 64 65 78 28 70 53 72 63 2c 20  lumnIndex(pSrc, 
336da 69 2b 31 2c 20 7a 4e 61 6d 65 2c 20 26 69 4c 65  i+1, zName, &iLe
336db 66 74 2c 20 26 69 4c 65 66 74 43 6f 6c 29 20 29  ft, &iLeftCol) )
336dc 7b 0a 20 20 20 20 20 20 20 20 20 20 61 64 64 57  {.          addW
336dd 68 65 72 65 54 65 72 6d 28 70 50 61 72 73 65 2c  hereTerm(pParse,
336de 20 70 53 72 63 2c 20 69 4c 65 66 74 2c 20 69 4c   pSrc, iLeft, iL
336df 65 66 74 43 6f 6c 2c 20 69 2b 31 2c 20 6a 2c 0a  eftCol, i+1, j,.
336e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
336e1 20 20 20 20 20 20 20 69 73 4f 75 74 65 72 2c 20         isOuter, 
336e2 26 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20  &p->pWhere);.   
336e3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
336e4 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 73     }..    /* Dis
336e5 61 6c 6c 6f 77 20 62 6f 74 68 20 4f 4e 20 61 6e  allow both ON an
336e6 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20  d USING clauses 
336e7 69 6e 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e  in the same join
336e8 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
336e9 70 52 69 67 68 74 2d 3e 70 4f 6e 20 26 26 20 70  pRight->pOn && p
336ea 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b  Right->pUsing ){
336eb 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
336ec 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
336ed 63 61 6e 6e 6f 74 20 68 61 76 65 20 62 6f 74 68  cannot have both
336ee 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 22 0a   ON and USING ".
336ef 20 20 20 20 20 20 20 20 22 63 6c 61 75 73 65 73          "clauses
336f0 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6a 6f 69   in the same joi
336f1 6e 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  n");.      retur
336f2 6e 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  n 1;.    }..    
336f3 2f 2a 20 41 64 64 20 74 68 65 20 4f 4e 20 63 6c  /* Add the ON cl
336f4 61 75 73 65 20 74 6f 20 74 68 65 20 65 6e 64 20  ause to the end 
336f5 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
336f6 75 73 65 2c 20 63 6f 6e 6e 65 63 74 65 64 20 62  use, connected b
336f7 79 0a 20 20 20 20 2a 2a 20 61 6e 20 41 4e 44 20  y.    ** an AND 
336f8 6f 70 65 72 61 74 6f 72 2e 0a 20 20 20 20 2a 2f  operator..    */
336f9 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d  .    if( pRight-
336fa 3e 70 4f 6e 20 29 7b 0a 20 20 20 20 20 20 69 66  >pOn ){.      if
336fb 28 20 69 73 4f 75 74 65 72 20 29 20 73 65 74 4a  ( isOuter ) setJ
336fc 6f 69 6e 45 78 70 72 28 70 52 69 67 68 74 2d 3e  oinExpr(pRight->
336fd 70 4f 6e 2c 20 70 52 69 67 68 74 2d 3e 69 43 75  pOn, pRight->iCu
336fe 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 70 2d 3e  rsor);.      p->
336ff 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33  pWhere = sqlite3
33700 45 78 70 72 41 6e 64 28 70 50 61 72 73 65 2d 3e  ExprAnd(pParse->
33701 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 70  db, p->pWhere, p
33702 52 69 67 68 74 2d 3e 70 4f 6e 29 3b 0a 20 20 20  Right->pOn);.   
33703 20 20 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20 3d     pRight->pOn =
33704 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f   0;.    }..    /
33705 2a 20 43 72 65 61 74 65 20 65 78 74 72 61 20 74  * Create extra t
33706 65 72 6d 73 20 6f 6e 20 74 68 65 20 57 48 45 52  erms on the WHER
33707 45 20 63 6c 61 75 73 65 20 66 6f 72 20 65 61 63  E clause for eac
33708 68 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 0a 20  h column named. 
33709 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 55 53 49     ** in the USI
3370a 4e 47 20 63 6c 61 75 73 65 2e 20 20 45 78 61 6d  NG clause.  Exam
3370b 70 6c 65 3a 20 49 66 20 74 68 65 20 74 77 6f 20  ple: If the two 
3370c 74 61 62 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69  tables to be joi
3370d 6e 65 64 20 61 72 65 20 0a 20 20 20 20 2a 2a 20  ned are .    ** 
3370e 41 20 61 6e 64 20 42 20 61 6e 64 20 74 68 65 20  A and B and the 
3370f 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6e 61 6d  USING clause nam
33710 65 73 20 58 2c 20 59 2c 20 61 6e 64 20 5a 2c 20  es X, Y, and Z, 
33711 74 68 65 6e 20 61 64 64 20 74 68 69 73 0a 20 20  then add this.  
33712 20 20 2a 2a 20 74 6f 20 74 68 65 20 57 48 45 52    ** to the WHER
33713 45 20 63 6c 61 75 73 65 3a 20 20 20 20 41 2e 58  E clause:    A.X
33714 3d 42 2e 58 20 41 4e 44 20 41 2e 59 3d 42 2e 59  =B.X AND A.Y=B.Y
33715 20 41 4e 44 20 41 2e 5a 3d 42 2e 5a 0a 20 20 20   AND A.Z=B.Z.   
33716 20 2a 2a 20 52 65 70 6f 72 74 20 61 6e 20 65 72   ** Report an er
33717 72 6f 72 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d  ror if any colum
33718 6e 20 6d 65 6e 74 69 6f 6e 65 64 20 69 6e 20 74  n mentioned in t
33719 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20  he USING clause 
3371a 69 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 63 6f  is.    ** not co
3371b 6e 74 61 69 6e 65 64 20 69 6e 20 62 6f 74 68 20  ntained in both 
3371c 74 61 62 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69  tables to be joi
3371d 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ned..    */.    
3371e 69 66 28 20 70 52 69 67 68 74 2d 3e 70 55 73 69  if( pRight->pUsi
3371f 6e 67 20 29 7b 0a 20 20 20 20 20 20 49 64 4c 69  ng ){.      IdLi
33720 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 52 69 67  st *pList = pRig
33721 68 74 2d 3e 70 55 73 69 6e 67 3b 0a 20 20 20 20  ht->pUsing;.    
33722 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 69    for(j=0; j<pLi
33723 73 74 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20  st->nId; j++){. 
33724 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61         char *zNa
33725 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  me;     /* Name 
33726 6f 66 20 74 68 65 20 74 65 72 6d 20 69 6e 20 74  of the term in t
33727 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20  he USING clause 
33728 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  */.        int i
33729 4c 65 66 74 3b 20 20 20 20 20 20 20 2f 2a 20 54  Left;       /* T
3372a 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 65 66 74  able on the left
3372b 20 77 69 74 68 20 6d 61 74 63 68 69 6e 67 20 63   with matching c
3372c 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  olumn name */.  
3372d 20 20 20 20 20 20 69 6e 74 20 69 4c 65 66 74 43        int iLeftC
3372e 6f 6c 3b 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e  ol;    /* Column
3372f 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68   number of match
33730 69 6e 67 20 63 6f 6c 75 6d 6e 20 6f 6e 20 74 68  ing column on th
33731 65 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20 20 20  e left */.      
33732 20 20 69 6e 74 20 69 52 69 67 68 74 43 6f 6c 3b    int iRightCol;
33733 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d     /* Column num
33734 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 20  ber of matching 
33735 63 6f 6c 75 6d 6e 20 6f 6e 20 74 68 65 20 72 69  column on the ri
33736 67 68 74 20 2a 2f 0a 0a 20 20 20 20 20 20 20 20  ght */..        
33737 7a 4e 61 6d 65 20 3d 20 70 4c 69 73 74 2d 3e 61  zName = pList->a
33738 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  [j].zName;.     
33739 20 20 20 69 52 69 67 68 74 43 6f 6c 20 3d 20 63     iRightCol = c
3373a 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 52 69 67 68  olumnIndex(pRigh
3373b 74 54 61 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  tTab, zName);.  
3373c 20 20 20 20 20 20 69 66 28 20 69 52 69 67 68 74        if( iRight
3373d 43 6f 6c 3c 30 0a 20 20 20 20 20 20 20 20 20 7c  Col<0.         |
3373e 7c 20 21 74 61 62 6c 65 41 6e 64 43 6f 6c 75 6d  | !tableAndColum
3373f 6e 49 6e 64 65 78 28 70 53 72 63 2c 20 69 2b 31  nIndex(pSrc, i+1
33740 2c 20 7a 4e 61 6d 65 2c 20 26 69 4c 65 66 74 2c  , zName, &iLeft,
33741 20 26 69 4c 65 66 74 43 6f 6c 29 0a 20 20 20 20   &iLeftCol).    
33742 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20      ){.         
33743 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
33744 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74  (pParse, "cannot
33745 20 6a 6f 69 6e 20 75 73 69 6e 67 20 63 6f 6c 75   join using colu
33746 6d 6e 20 25 73 20 2d 20 63 6f 6c 75 6d 6e 20 22  mn %s - column "
33747 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 6e 6f  .            "no
33748 74 20 70 72 65 73 65 6e 74 20 69 6e 20 62 6f 74  t present in bot
33749 68 20 74 61 62 6c 65 73 22 2c 20 7a 4e 61 6d 65  h tables", zName
3374a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
3374b 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  urn 1;.        }
3374c 0a 20 20 20 20 20 20 20 20 61 64 64 57 68 65 72  .        addWher
3374d 65 54 65 72 6d 28 70 50 61 72 73 65 2c 20 70 53  eTerm(pParse, pS
3374e 72 63 2c 20 69 4c 65 66 74 2c 20 69 4c 65 66 74  rc, iLeft, iLeft
3374f 43 6f 6c 2c 20 69 2b 31 2c 20 69 52 69 67 68 74  Col, i+1, iRight
33750 43 6f 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Col,.           
33751 20 20 20 20 20 20 20 20 20 20 69 73 4f 75 74 65            isOute
33752 72 2c 20 26 70 2d 3e 70 57 68 65 72 65 29 3b 0a  r, &p->pWhere);.
33753 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
33754 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
33755 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 63 6f  ./*.** Insert co
33756 64 65 20 69 6e 74 6f 20 22 76 22 20 74 68 61 74  de into "v" that
33757 20 77 69 6c 6c 20 70 75 73 68 20 74 68 65 20 72   will push the r
33758 65 63 6f 72 64 20 6f 6e 20 74 68 65 20 74 6f 70  ecord on the top
33759 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b   of the.** stack
3375a 20 69 6e 74 6f 20 74 68 65 20 73 6f 72 74 65 72   into the sorter
3375b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
3375c 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28   pushOntoSorter(
3375d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
3375e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  ,         /* Par
3375f 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
33760 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
33761 72 42 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 4f  rBy,    /* The O
33762 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
33763 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
33764 65 63 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  ect,       /* Th
33765 65 20 77 68 6f 6c 65 20 53 45 4c 45 43 54 20 73  e whole SELECT s
33766 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e  tatement */.  in
33767 74 20 72 65 67 44 61 74 61 20 20 20 20 20 20 20  t regData       
33768 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
33769 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 74 6f   holding data to
3376a 20 62 65 20 73 6f 72 74 65 64 20 2a 2f 0a 29 7b   be sorted */.){
3376b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
3376c 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e  rse->pVdbe;.  in
3376d 74 20 6e 45 78 70 72 20 3d 20 70 4f 72 64 65 72  t nExpr = pOrder
3376e 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 69 6e 74  By->nExpr;.  int
3376f 20 72 65 67 42 61 73 65 20 3d 20 73 71 6c 69 74   regBase = sqlit
33770 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
33771 50 61 72 73 65 2c 20 6e 45 78 70 72 2b 32 29 3b  Parse, nExpr+2);
33772 0a 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64  .  int regRecord
33773 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
33774 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
33775 69 6e 74 20 6f 70 3b 0a 20 20 73 71 6c 69 74 65  int op;.  sqlite
33776 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28  3ExprCacheClear(
33777 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74  pParse);.  sqlit
33778 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69  e3ExprCodeExprLi
33779 73 74 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65  st(pParse, pOrde
3377a 72 42 79 2c 20 72 65 67 42 61 73 65 2c 20 30 29  rBy, regBase, 0)
3377b 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
3377c 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 71 75  ddOp2(v, OP_Sequ
3377d 65 6e 63 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e  ence, pOrderBy->
3377e 69 45 43 75 72 73 6f 72 2c 20 72 65 67 42 61 73  iECursor, regBas
3377f 65 2b 6e 45 78 70 72 29 3b 0a 20 20 73 71 6c 69  e+nExpr);.  sqli
33780 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28  te3ExprCodeMove(
33781 70 50 61 72 73 65 2c 20 72 65 67 44 61 74 61 2c  pParse, regData,
33782 20 72 65 67 42 61 73 65 2b 6e 45 78 70 72 2b 31   regBase+nExpr+1
33783 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  , 1);.  sqlite3V
33784 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
33785 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42  MakeRecord, regB
33786 61 73 65 2c 20 6e 45 78 70 72 20 2b 20 32 2c 20  ase, nExpr + 2, 
33787 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 69 66  regRecord);.  if
33788 28 20 70 53 65 6c 65 63 74 2d 3e 73 65 6c 46 6c  ( pSelect->selFl
33789 61 67 73 20 26 20 53 46 5f 55 73 65 53 6f 72 74  ags & SF_UseSort
3378a 65 72 20 29 7b 0a 20 20 20 20 6f 70 20 3d 20 4f  er ){.    op = O
3378b 50 5f 53 6f 72 74 65 72 49 6e 73 65 72 74 3b 0a  P_SorterInsert;.
3378c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 70 20    }else{.    op 
3378d 3d 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 3b 0a  = OP_IdxInsert;.
3378e 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
3378f 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 70  eAddOp2(v, op, p
33790 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f  OrderBy->iECurso
33791 72 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20  r, regRecord);. 
33792 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
33793 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
33794 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c  egRecord);.  sql
33795 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
33796 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
33797 42 61 73 65 2c 20 6e 45 78 70 72 2b 32 29 3b 0a  Base, nExpr+2);.
33798 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 69    if( pSelect->i
33799 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 69 6e 74  Limit ){.    int
3379a 20 61 64 64 72 31 2c 20 61 64 64 72 32 3b 0a 20   addr1, addr2;. 
3379b 20 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 0a 20     int iLimit;. 
3379c 20 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e     if( pSelect->
3379d 69 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20  iOffset ){.     
3379e 20 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65 63   iLimit = pSelec
3379f 74 2d 3e 69 4f 66 66 73 65 74 2b 31 3b 0a 20 20  t->iOffset+1;.  
337a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
337a1 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65 63 74 2d  Limit = pSelect-
337a2 3e 69 4c 69 6d 69 74 3b 0a 20 20 20 20 7d 0a 20  >iLimit;.    }. 
337a3 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74     addr1 = sqlit
337a4 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
337a5 4f 50 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d 69  OP_IfZero, iLimi
337a6 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
337a7 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
337a8 41 64 64 49 6d 6d 2c 20 69 4c 69 6d 69 74 2c 20  AddImm, iLimit, 
337a9 2d 31 29 3b 0a 20 20 20 20 61 64 64 72 32 20 3d  -1);.    addr2 =
337aa 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
337ab 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a  p0(v, OP_Goto);.
337ac 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
337ad 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 31  umpHere(v, addr1
337ae 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
337af 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4c  beAddOp1(v, OP_L
337b0 61 73 74 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69  ast, pOrderBy->i
337b1 45 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 71  ECursor);.    sq
337b2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
337b3 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 70 4f  v, OP_Delete, pO
337b4 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72  rderBy->iECursor
337b5 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
337b6 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
337b7 64 72 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  dr2);.  }.}../*.
337b8 2a 2a 20 41 64 64 20 63 6f 64 65 20 74 6f 20 69  ** Add code to i
337b9 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 4f 46 46  mplement the OFF
337ba 53 45 54 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  SET.*/.static vo
337bb 69 64 20 63 6f 64 65 4f 66 66 73 65 74 28 0a 20  id codeOffset(. 
337bc 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20   Vdbe *v,       
337bd 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
337be 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56 4d  ode into this VM
337bf 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
337c0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53          /* The S
337c1 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
337c2 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20  being coded */. 
337c3 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 20 20   int iContinue  
337c4 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20     /* Jump here 
337c5 74 6f 20 73 6b 69 70 20 74 68 65 20 63 75 72 72  to skip the curr
337c6 65 6e 74 20 72 65 63 6f 72 64 20 2a 2f 0a 29 7b  ent record */.){
337c7 0a 20 20 69 66 28 20 70 2d 3e 69 4f 66 66 73 65  .  if( p->iOffse
337c8 74 20 26 26 20 69 43 6f 6e 74 69 6e 75 65 21 3d  t && iContinue!=
337c9 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 64 64  0 ){.    int add
337ca 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  r;.    sqlite3Vd
337cb 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
337cc 64 64 49 6d 6d 2c 20 70 2d 3e 69 4f 66 66 73 65  ddImm, p->iOffse
337cd 74 2c 20 2d 31 29 3b 0a 20 20 20 20 61 64 64 72  t, -1);.    addr
337ce 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
337cf 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 4e 65 67  dOp1(v, OP_IfNeg
337d0 2c 20 70 2d 3e 69 4f 66 66 73 65 74 29 3b 0a 20  , p->iOffset);. 
337d1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
337d2 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
337d3 20 30 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a   0, iContinue);.
337d4 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
337d5 28 76 2c 20 22 73 6b 69 70 20 4f 46 46 53 45 54  (v, "skip OFFSET
337d6 20 72 65 63 6f 72 64 73 22 29 29 3b 0a 20 20 20   records"));.   
337d7 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
337d8 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20  Here(v, addr);. 
337d9 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20   }.}../*.** Add 
337da 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 63  code that will c
337db 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72  heck to make sur
337dc 65 20 74 68 65 20 4e 20 72 65 67 69 73 74 65 72  e the N register
337dd 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 69 4d  s starting at iM
337de 65 6d 0a 2a 2a 20 66 6f 72 6d 20 61 20 64 69 73  em.** form a dis
337df 74 69 6e 63 74 20 65 6e 74 72 79 2e 20 20 69 54  tinct entry.  iT
337e0 61 62 20 69 73 20 61 20 73 6f 72 74 69 6e 67 20  ab is a sorting 
337e1 69 6e 64 65 78 20 74 68 61 74 20 68 6f 6c 64 73  index that holds
337e2 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 73   previously.** s
337e3 65 65 6e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73  een combinations
337e4 20 6f 66 20 74 68 65 20 4e 20 76 61 6c 75 65 73   of the N values
337e5 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69  .  A new entry i
337e6 73 20 6d 61 64 65 20 69 6e 20 69 54 61 62 0a 2a  s made in iTab.*
337e7 2a 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74  * if the current
337e8 20 4e 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65   N values are ne
337e9 77 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75 6d 70 20  w..**.** A jump 
337ea 74 6f 20 61 64 64 72 52 65 70 65 61 74 20 69 73  to addrRepeat is
337eb 20 6d 61 64 65 20 61 6e 64 20 74 68 65 20 4e 2b   made and the N+
337ec 31 20 76 61 6c 75 65 73 20 61 72 65 20 70 6f 70  1 values are pop
337ed 70 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20  ped from the.** 
337ee 73 74 61 63 6b 20 69 66 20 74 68 65 20 74 6f 70  stack if the top
337ef 20 4e 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20   N elements are 
337f0 6e 6f 74 20 64 69 73 74 69 6e 63 74 2e 0a 2a 2f  not distinct..*/
337f1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
337f2 65 44 69 73 74 69 6e 63 74 28 0a 20 20 50 61 72  eDistinct(.  Par
337f3 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
337f4 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63  /* Parsing and c
337f5 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63  ode generating c
337f6 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
337f7 69 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f  iTab,          /
337f8 2a 20 41 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  * A sorting inde
337f9 78 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 66  x used to test f
337fa 6f 72 20 64 69 73 74 69 6e 63 74 6e 65 73 73 20  or distinctness 
337fb 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 52 65 70  */.  int addrRep
337fc 65 61 74 2c 20 20 20 20 2f 2a 20 4a 75 6d 70 20  eat,    /* Jump 
337fd 74 6f 20 68 65 72 65 20 69 66 20 6e 6f 74 20 64  to here if not d
337fe 69 73 74 69 6e 63 74 20 2a 2f 0a 20 20 69 6e 74  istinct */.  int
337ff 20 4e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   N,             
33800 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65  /* Number of ele
33801 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 69  ments */.  int i
33802 4d 65 6d 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Mem           /*
33803 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 2a   First element *
33804 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  /.){.  Vdbe *v;.
33805 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 76 20 3d    int r1;..  v =
33806 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
33807 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65    r1 = sqlite3Ge
33808 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
33809 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
3380a 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50 5f 46  ddOp4Int(v, OP_F
3380b 6f 75 6e 64 2c 20 69 54 61 62 2c 20 61 64 64 72  ound, iTab, addr
3380c 52 65 70 65 61 74 2c 20 69 4d 65 6d 2c 20 4e 29  Repeat, iMem, N)
3380d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
3380e 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
3380f 52 65 63 6f 72 64 2c 20 69 4d 65 6d 2c 20 4e 2c  Record, iMem, N,
33810 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56   r1);.  sqlite3V
33811 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
33812 49 64 78 49 6e 73 65 72 74 2c 20 69 54 61 62 2c  IdxInsert, iTab,
33813 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52   r1);.  sqlite3R
33814 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
33815 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 23 69  arse, r1);.}..#i
33816 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
33817 54 5f 53 55 42 51 55 45 52 59 0a 2f 2a 0a 2a 2a  T_SUBQUERY./*.**
33818 20 47 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72   Generate an err
33819 6f 72 20 6d 65 73 73 61 67 65 20 77 68 65 6e 20  or message when 
3381a 61 20 53 45 4c 45 43 54 20 69 73 20 75 73 65 64  a SELECT is used
3381b 20 77 69 74 68 69 6e 20 61 20 73 75 62 65 78 70   within a subexp
3381c 72 65 73 73 69 6f 6e 0a 2a 2a 20 28 65 78 61 6d  ression.** (exam
3381d 70 6c 65 3a 20 20 22 61 20 49 4e 20 28 53 45 4c  ple:  "a IN (SEL
3381e 45 43 54 20 2a 20 46 52 4f 4d 20 74 61 62 6c 65  ECT * FROM table
3381f 29 22 29 20 62 75 74 20 69 74 20 68 61 73 20 6d  )") but it has m
33820 6f 72 65 20 74 68 61 6e 20 31 20 72 65 73 75 6c  ore than 1 resul
33821 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 20 20 57 65  t.** column.  We
33822 20 64 6f 20 74 68 69 73 20 69 6e 20 61 20 73 75   do this in a su
33823 62 72 6f 75 74 69 6e 65 20 62 65 63 61 75 73 65  broutine because
33824 20 74 68 65 20 65 72 72 6f 72 20 75 73 65 64 20   the error used 
33825 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 6d  to occur.** in m
33826 75 6c 74 69 70 6c 65 20 70 6c 61 63 65 73 2e 20  ultiple places. 
33827 20 28 54 68 65 20 65 72 72 6f 72 20 6f 6e 6c 79   (The error only
33828 20 6f 63 63 75 72 73 20 69 6e 20 6f 6e 65 20 70   occurs in one p
33829 6c 61 63 65 20 6e 6f 77 2c 20 62 75 74 20 77 65  lace now, but we
3382a 0a 2a 2a 20 72 65 74 61 69 6e 20 74 68 65 20 73  .** retain the s
3382b 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 6d 69 6e  ubroutine to min
3382c 69 6d 69 7a 65 20 63 6f 64 65 20 64 69 73 72 75  imize code disru
3382d 70 74 69 6f 6e 2e 29 0a 2a 2f 0a 73 74 61 74 69  ption.).*/.stati
3382e 63 20 69 6e 74 20 63 68 65 63 6b 46 6f 72 4d 75  c int checkForMu
3382f 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45  ltiColumnSelectE
33830 72 72 6f 72 28 0a 20 20 50 61 72 73 65 20 2a 70  rror(.  Parse *p
33831 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  Parse,       /* 
33832 50 61 72 73 65 20 63 6f 6e 74 65 78 74 2e 20 2a  Parse context. *
33833 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a  /.  SelectDest *
33834 70 44 65 73 74 2c 20 20 20 2f 2a 20 44 65 73 74  pDest,   /* Dest
33835 69 6e 61 74 69 6f 6e 20 6f 66 20 53 45 4c 45 43  ination of SELEC
33836 54 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69  T results */.  i
33837 6e 74 20 6e 45 78 70 72 20 20 20 20 20 20 20 20  nt nExpr        
33838 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
33839 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20   result columns 
3383a 72 65 74 75 72 6e 65 64 20 62 79 20 53 45 4c 45  returned by SELE
3383b 43 54 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 65  CT */.){.  int e
3383c 44 65 73 74 20 3d 20 70 44 65 73 74 2d 3e 65 44  Dest = pDest->eD
3383d 65 73 74 3b 0a 20 20 69 66 28 20 6e 45 78 70 72  est;.  if( nExpr
3383e 3e 31 20 26 26 20 28 65 44 65 73 74 3d 3d 53 52  >1 && (eDest==SR
3383f 54 5f 4d 65 6d 20 7c 7c 20 65 44 65 73 74 3d 3d  T_Mem || eDest==
33840 53 52 54 5f 53 65 74 29 20 29 7b 0a 20 20 20 20  SRT_Set) ){.    
33841 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
33842 70 50 61 72 73 65 2c 20 22 6f 6e 6c 79 20 61 20  pParse, "only a 
33843 73 69 6e 67 6c 65 20 72 65 73 75 6c 74 20 61 6c  single result al
33844 6c 6f 77 65 64 20 66 6f 72 20 22 0a 20 20 20 20  lowed for ".    
33845 20 20 20 22 61 20 53 45 4c 45 43 54 20 74 68 61     "a SELECT tha
33846 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20  t is part of an 
33847 65 78 70 72 65 73 73 69 6f 6e 22 29 3b 0a 20 20  expression");.  
33848 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65    return 1;.  }e
33849 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
3384a 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a  0;.  }.}.#endif.
3384b 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
3384c 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
3384d 69 6e 67 20 6f 62 6a 65 63 74 20 69 73 20 75 73  ing object is us
3384e 65 64 20 74 6f 20 72 65 63 6f 72 64 20 69 6e 66  ed to record inf
3384f 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 0a 2a  ormation about.*
33850 2a 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 73 73  * how to process
33851 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65   the DISTINCT ke
33852 79 77 6f 72 64 2c 20 74 6f 20 73 69 6d 70 6c 69  yword, to simpli
33853 66 79 20 70 61 73 73 69 6e 67 20 74 68 61 74 20  fy passing that 
33854 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 69  information.** i
33855 6e 74 6f 20 74 68 65 20 73 65 6c 65 63 74 49 6e  nto the selectIn
33856 6e 65 72 4c 6f 6f 70 28 29 20 72 6f 75 74 69 6e  nerLoop() routin
33857 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  e..*/.typedef st
33858 72 75 63 74 20 44 69 73 74 69 6e 63 74 43 74 78  ruct DistinctCtx
33859 20 44 69 73 74 69 6e 63 74 43 74 78 3b 0a 73 74   DistinctCtx;.st
3385a 72 75 63 74 20 44 69 73 74 69 6e 63 74 43 74 78  ruct DistinctCtx
3385b 20 7b 0a 20 20 75 38 20 69 73 54 6e 63 74 3b 20   {.  u8 isTnct; 
3385c 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
3385d 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79  the DISTINCT key
3385e 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20  word is present 
3385f 2a 2f 0a 20 20 75 38 20 65 54 6e 63 74 54 79 70  */.  u8 eTnctTyp
33860 65 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74  e;   /* One of t
33861 68 65 20 57 48 45 52 45 5f 44 49 53 54 49 4e 43  he WHERE_DISTINC
33862 54 5f 2a 20 6f 70 65 72 61 74 6f 72 73 20 2a 2f  T_* operators */
33863 0a 20 20 69 6e 74 20 74 61 62 54 6e 63 74 3b 20  .  int tabTnct; 
33864 20 20 20 2f 2a 20 45 70 68 65 6d 65 72 61 6c 20     /* Ephemeral 
33865 74 61 62 6c 65 20 75 73 65 64 20 66 6f 72 20 44  table used for D
33866 49 53 54 49 4e 43 54 20 70 72 6f 63 65 73 73 69  ISTINCT processi
33867 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ng */.  int addr
33868 54 6e 63 74 3b 20 20 20 2f 2a 20 41 64 64 72 65  Tnct;   /* Addre
33869 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68  ss of OP_OpenEph
3386a 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 66 6f  emeral opcode fo
3386b 72 20 74 61 62 54 6e 63 74 20 2a 2f 0a 7d 3b 0a  r tabTnct */.};.
3386c 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
3386d 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 74 68  ine generates th
3386e 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 69  e code for the i
3386f 6e 73 69 64 65 20 6f 66 20 74 68 65 20 69 6e 6e  nside of the inn
33870 65 72 20 6c 6f 6f 70 0a 2a 2a 20 6f 66 20 61 20  er loop.** of a 
33871 53 45 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 49 66  SELECT..**.** If
33872 20 73 72 63 54 61 62 20 61 6e 64 20 6e 43 6f 6c   srcTab and nCol
33873 75 6d 6e 20 61 72 65 20 62 6f 74 68 20 7a 65 72  umn are both zer
33874 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 45 4c 69  o, then the pELi
33875 73 74 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a  st expressions.*
33876 2a 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20  * are evaluated 
33877 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 74 20  in order to get 
33878 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 69  the data for thi
33879 73 20 72 6f 77 2e 20 20 49 66 20 6e 43 6f 6c 75  s row.  If nColu
3387a 6d 6e 3e 30 0a 2a 2a 20 74 68 65 6e 20 64 61 74  mn>0.** then dat
3387b 61 20 69 73 20 70 75 6c 6c 65 64 20 66 72 6f 6d  a is pulled from
3387c 20 73 72 63 54 61 62 20 61 6e 64 20 70 45 4c 69   srcTab and pELi
3387d 73 74 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20  st is used only 
3387e 74 6f 20 67 65 74 20 74 68 65 0a 2a 2a 20 64 61  to get the.** da
3387f 74 61 74 79 70 65 73 20 66 6f 72 20 65 61 63 68  tatypes for each
33880 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74   column..*/.stat
33881 69 63 20 76 6f 69 64 20 73 65 6c 65 63 74 49 6e  ic void selectIn
33882 6e 65 72 4c 6f 6f 70 28 0a 20 20 50 61 72 73 65  nerLoop(.  Parse
33883 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
33884 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
33885 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65   context */.  Se
33886 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
33887 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d        /* The com
33888 70 6c 65 74 65 20 73 65 6c 65 63 74 20 73 74 61  plete select sta
33889 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64  tement being cod
3388a 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ed */.  ExprList
3388b 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20   *pEList,       
3388c 2f 2a 20 4c 69 73 74 20 6f 66 20 76 61 6c 75 65  /* List of value
3388d 73 20 62 65 69 6e 67 20 65 78 74 72 61 63 74 65  s being extracte
3388e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 72 63 54 61  d */.  int srcTa
3388f 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b,             /
33890 2a 20 50 75 6c 6c 20 64 61 74 61 20 66 72 6f 6d  * Pull data from
33891 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20   this table */. 
33892 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20   int nColumn,   
33893 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
33894 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
33895 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c   the source tabl
33896 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
33897 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f  *pOrderBy,     /
33898 2a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73  * If not NULL, s
33899 6f 72 74 20 72 65 73 75 6c 74 73 20 75 73 69 6e  ort results usin
3389a 67 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20  g this key */.  
3389b 44 69 73 74 69 6e 63 74 43 74 78 20 2a 70 44 69  DistinctCtx *pDi
3389c 73 74 69 6e 63 74 2c 20 2f 2a 20 49 66 20 6e 6f  stinct, /* If no
3389d 74 20 4e 55 4c 4c 2c 20 69 6e 66 6f 20 6f 6e 20  t NULL, info on 
3389e 68 6f 77 20 74 6f 20 70 72 6f 63 65 73 73 20 44  how to process D
3389f 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 53 65 6c  ISTINCT */.  Sel
338a0 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 2c 20  ectDest *pDest, 
338a1 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64       /* How to d
338a2 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20 72 65  ispose of the re
338a3 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 20 69  sults */.  int i
338a4 43 6f 6e 74 69 6e 75 65 2c 20 20 20 20 20 20 20  Continue,       
338a5 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20     /* Jump here 
338a6 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68  to continue with
338a7 20 6e 65 78 74 20 72 6f 77 20 2a 2f 0a 20 20 69   next row */.  i
338a8 6e 74 20 69 42 72 65 61 6b 20 20 20 20 20 20 20  nt iBreak       
338a9 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68         /* Jump h
338aa 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74  ere to break out
338ab 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f   of the inner lo
338ac 6f 70 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20  op */.){.  Vdbe 
338ad 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
338ae 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  be;.  int i;.  i
338af 6e 74 20 68 61 73 44 69 73 74 69 6e 63 74 3b 20  nt hasDistinct; 
338b0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
338b1 66 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b  f the DISTINCT k
338b2 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e  eyword is presen
338b3 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65  t */.  int regRe
338b4 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20  sult;           
338b5 20 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 6d     /* Start of m
338b6 65 6d 6f 72 79 20 68 6f 6c 64 69 6e 67 20 72 65  emory holding re
338b7 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 6e  sult set */.  in
338b8 74 20 65 44 65 73 74 20 3d 20 70 44 65 73 74 2d  t eDest = pDest-
338b9 3e 65 44 65 73 74 3b 20 20 20 2f 2a 20 48 6f 77  >eDest;   /* How
338ba 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 72   to dispose of r
338bb 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 20  esults */.  int 
338bc 69 50 61 72 6d 20 3d 20 70 44 65 73 74 2d 3e 69  iParm = pDest->i
338bd 53 44 50 61 72 6d 3b 20 2f 2a 20 46 69 72 73 74  SDParm; /* First
338be 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 64 69 73   argument to dis
338bf 70 6f 73 61 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a  posal method */.
338c0 20 20 69 6e 74 20 6e 52 65 73 75 6c 74 43 6f 6c    int nResultCol
338c1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
338c2 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c   Number of resul
338c3 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 0a 20 20  t columns */..  
338c4 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20 69  assert( v );.  i
338c5 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29  f( NEVER(v==0) )
338c6 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
338c7 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a  t( pEList!=0 );.
338c8 20 20 68 61 73 44 69 73 74 69 6e 63 74 20 3d 20    hasDistinct = 
338c9 70 44 69 73 74 69 6e 63 74 20 3f 20 70 44 69 73  pDistinct ? pDis
338ca 74 69 6e 63 74 2d 3e 65 54 6e 63 74 54 79 70 65  tinct->eTnctType
338cb 20 3a 20 57 48 45 52 45 5f 44 49 53 54 49 4e 43   : WHERE_DISTINC
338cc 54 5f 4e 4f 4f 50 3b 0a 20 20 69 66 28 20 70 4f  T_NOOP;.  if( pO
338cd 72 64 65 72 42 79 3d 3d 30 20 26 26 20 21 68 61  rderBy==0 && !ha
338ce 73 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20  sDistinct ){.   
338cf 20 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70   codeOffset(v, p
338d0 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20  , iContinue);.  
338d1 7d 0a 0a 20 20 2f 2a 20 50 75 6c 6c 20 74 68 65  }..  /* Pull the
338d2 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 75 6d   requested colum
338d3 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e  ns..  */.  if( n
338d4 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20  Column>0 ){.    
338d5 6e 52 65 73 75 6c 74 43 6f 6c 20 3d 20 6e 43 6f  nResultCol = nCo
338d6 6c 75 6d 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  lumn;.  }else{. 
338d7 20 20 20 6e 52 65 73 75 6c 74 43 6f 6c 20 3d 20     nResultCol = 
338d8 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20  pEList->nExpr;. 
338d9 20 7d 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e   }.  if( pDest->
338da 69 53 64 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  iSdst==0 ){.    
338db 70 44 65 73 74 2d 3e 69 53 64 73 74 20 3d 20 70  pDest->iSdst = p
338dc 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20  Parse->nMem+1;. 
338dd 20 20 20 70 44 65 73 74 2d 3e 6e 53 64 73 74 20     pDest->nSdst 
338de 3d 20 6e 52 65 73 75 6c 74 43 6f 6c 3b 0a 20 20  = nResultCol;.  
338df 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
338e0 3d 20 6e 52 65 73 75 6c 74 43 6f 6c 3b 0a 20 20  = nResultCol;.  
338e1 7d 65 6c 73 65 7b 20 0a 20 20 20 20 61 73 73 65  }else{ .    asse
338e2 72 74 28 20 70 44 65 73 74 2d 3e 6e 53 64 73 74  rt( pDest->nSdst
338e3 3d 3d 6e 52 65 73 75 6c 74 43 6f 6c 20 29 3b 0a  ==nResultCol );.
338e4 20 20 7d 0a 20 20 72 65 67 52 65 73 75 6c 74 20    }.  regResult 
338e5 3d 20 70 44 65 73 74 2d 3e 69 53 64 73 74 3b 0a  = pDest->iSdst;.
338e6 20 20 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20    if( nColumn>0 
338e7 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
338e8 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  i<nColumn; i++){
338e9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
338ea 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
338eb 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 69  olumn, srcTab, i
338ec 2c 20 72 65 67 52 65 73 75 6c 74 2b 69 29 3b 0a  , regResult+i);.
338ed 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
338ee 28 20 65 44 65 73 74 21 3d 53 52 54 5f 45 78 69  ( eDest!=SRT_Exi
338ef 73 74 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  sts ){.    /* If
338f0 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
338f1 20 69 73 20 61 6e 20 45 58 49 53 54 53 28 2e 2e   is an EXISTS(..
338f2 2e 29 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74  .) expression, t
338f3 68 65 20 61 63 74 75 61 6c 0a 20 20 20 20 2a 2a  he actual.    **
338f4 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64   values returned
338f5 20 62 79 20 74 68 65 20 53 45 4c 45 43 54 20 61   by the SELECT a
338f6 72 65 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e  re not required.
338f7 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
338f8 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61  te3ExprCacheClea
338f9 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 73  r(pParse);.    s
338fa 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78  qlite3ExprCodeEx
338fb 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  prList(pParse, p
338fc 45 4c 69 73 74 2c 20 72 65 67 52 65 73 75 6c 74  EList, regResult
338fd 2c 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74  , eDest==SRT_Out
338fe 70 75 74 29 3b 0a 20 20 7d 0a 20 20 6e 43 6f 6c  put);.  }.  nCol
338ff 75 6d 6e 20 3d 20 6e 52 65 73 75 6c 74 43 6f 6c  umn = nResultCol
33900 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 44  ;..  /* If the D
33901 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20  ISTINCT keyword 
33902 77 61 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 74  was present on t
33903 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
33904 65 6e 74 0a 20 20 2a 2a 20 61 6e 64 20 74 68 69  ent.  ** and thi
33905 73 20 72 6f 77 20 68 61 73 20 62 65 65 6e 20 73  s row has been s
33906 65 65 6e 20 62 65 66 6f 72 65 2c 20 74 68 65 6e  een before, then
33907 20 64 6f 20 6e 6f 74 20 6d 61 6b 65 20 74 68 69   do not make thi
33908 73 20 72 6f 77 0a 20 20 2a 2a 20 70 61 72 74 20  s row.  ** part 
33909 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 20  of the result.. 
3390a 20 2a 2f 0a 20 20 69 66 28 20 68 61 73 44 69 73   */.  if( hasDis
3390b 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 61 73 73  tinct ){.    ass
3390c 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29  ert( pEList!=0 )
3390d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45  ;.    assert( pE
3390e 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6e 43 6f  List->nExpr==nCo
3390f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 73 77 69 74  lumn );.    swit
33910 63 68 28 20 70 44 69 73 74 69 6e 63 74 2d 3e 65  ch( pDistinct->e
33911 54 6e 63 74 54 79 70 65 20 29 7b 0a 20 20 20 20  TnctType ){.    
33912 20 20 63 61 73 65 20 57 48 45 52 45 5f 44 49 53    case WHERE_DIS
33913 54 49 4e 43 54 5f 4f 52 44 45 52 45 44 3a 20 7b  TINCT_ORDERED: {
33914 0a 20 20 20 20 20 20 20 20 56 64 62 65 4f 70 20  .        VdbeOp 
33915 2a 70 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20  *pOp;           
33916 20 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 72 65   /* No longer re
33917 71 75 69 72 65 64 20 4f 70 65 6e 45 70 68 65 6d  quired OpenEphem
33918 65 72 61 6c 20 69 6e 73 74 72 2e 20 2a 2f 0a 20  eral instr. */. 
33919 20 20 20 20 20 20 20 69 6e 74 20 69 4a 75 6d 70         int iJump
3391a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
3391b 2a 20 4a 75 6d 70 20 64 65 73 74 69 6e 61 74 69  * Jump destinati
3391c 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e  on */.        in
3391d 74 20 72 65 67 50 72 65 76 3b 20 20 20 20 20 20  t regPrev;      
3391e 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75        /* Previou
3391f 73 20 72 6f 77 20 63 6f 6e 74 65 6e 74 20 2a 2f  s row content */
33920 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c  ..        /* All
33921 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20  ocate space for 
33922 74 68 65 20 70 72 65 76 69 6f 75 73 20 72 6f 77  the previous row
33923 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 67 50   */.        regP
33924 72 65 76 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  rev = pParse->nM
33925 65 6d 2b 31 3b 0a 20 20 20 20 20 20 20 20 70 50  em+1;.        pP
33926 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 43  arse->nMem += nC
33927 6f 6c 75 6d 6e 3b 0a 0a 20 20 20 20 20 20 20 20  olumn;..        
33928 2f 2a 20 43 68 61 6e 67 65 20 74 68 65 20 4f 50  /* Change the OP
33929 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 63  _OpenEphemeral c
3392a 6f 64 65 64 20 65 61 72 6c 69 65 72 20 74 6f 20  oded earlier to 
3392b 61 6e 20 4f 50 5f 4e 75 6c 6c 0a 20 20 20 20 20  an OP_Null.     
3392c 20 20 20 2a 2a 20 73 65 74 73 20 74 68 65 20 4d     ** sets the M
3392d 45 4d 5f 43 6c 65 61 72 65 64 20 62 69 74 20 6f  EM_Cleared bit o
3392e 6e 20 74 68 65 20 66 69 72 73 74 20 72 65 67 69  n the first regi
3392f 73 74 65 72 20 6f 66 20 74 68 65 0a 20 20 20 20  ster of the.    
33930 20 20 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 20      ** previous 
33931 76 61 6c 75 65 2e 20 20 54 68 69 73 20 77 69 6c  value.  This wil
33932 6c 20 63 61 75 73 65 20 74 68 65 20 4f 50 5f 4e  l cause the OP_N
33933 65 20 62 65 6c 6f 77 20 74 6f 20 61 6c 77 61 79  e below to alway
33934 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 61 69  s.        ** fai
33935 6c 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 69  l on the first i
33936 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  teration of the 
33937 6c 6f 6f 70 20 65 76 65 6e 20 69 66 20 74 68 65  loop even if the
33938 20 66 69 72 73 74 0a 20 20 20 20 20 20 20 20 2a   first.        *
33939 2a 20 72 6f 77 20 69 73 20 61 6c 6c 20 4e 55 4c  * row is all NUL
3393a 4c 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  Ls..        */. 
3393b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
3393c 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76  beChangeToNoop(v
3393d 2c 20 70 44 69 73 74 69 6e 63 74 2d 3e 61 64 64  , pDistinct->add
3393e 72 54 6e 63 74 29 3b 0a 20 20 20 20 20 20 20 20  rTnct);.        
3393f 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  pOp = sqlite3Vdb
33940 65 47 65 74 4f 70 28 76 2c 20 70 44 69 73 74 69  eGetOp(v, pDisti
33941 6e 63 74 2d 3e 61 64 64 72 54 6e 63 74 29 3b 0a  nct->addrTnct);.
33942 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63          pOp->opc
33943 6f 64 65 20 3d 20 4f 50 5f 4e 75 6c 6c 3b 0a 20  ode = OP_Null;. 
33944 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d         pOp->p1 =
33945 20 31 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 2d   1;.        pOp-
33946 3e 70 32 20 3d 20 72 65 67 50 72 65 76 3b 0a 0a  >p2 = regPrev;..
33947 20 20 20 20 20 20 20 20 69 4a 75 6d 70 20 3d 20          iJump = 
33948 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
33949 6e 74 41 64 64 72 28 76 29 20 2b 20 6e 43 6f 6c  ntAddr(v) + nCol
3394a 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  umn;.        for
3394b 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b  (i=0; i<nColumn;
3394c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
3394d 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
3394e 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c  = sqlite3ExprCol
3394f 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 4c  lSeq(pParse, pEL
33950 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ist->a[i].pExpr)
33951 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
33952 69 3c 6e 43 6f 6c 75 6d 6e 2d 31 20 29 7b 0a 20  i<nColumn-1 ){. 
33953 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
33954 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
33955 4f 50 5f 4e 65 2c 20 72 65 67 52 65 73 75 6c 74  OP_Ne, regResult
33956 2b 69 2c 20 69 4a 75 6d 70 2c 20 72 65 67 50 72  +i, iJump, regPr
33957 65 76 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 20  ev+i);.         
33958 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
33959 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
3395a 64 64 4f 70 33 28 76 2c 20 4f 50 5f 45 71 2c 20  ddOp3(v, OP_Eq, 
3395b 72 65 67 52 65 73 75 6c 74 2b 69 2c 20 69 43 6f  regResult+i, iCo
3395c 6e 74 69 6e 75 65 2c 20 72 65 67 50 72 65 76 2b  ntinue, regPrev+
3395d 69 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  i);.          }.
3395e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3395f 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
33960 20 2d 31 2c 20 28 63 6f 6e 73 74 20 63 68 61 72   -1, (const char
33961 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c   *)pColl, P4_COL
33962 4c 53 45 51 29 3b 0a 20 20 20 20 20 20 20 20 20  LSEQ);.         
33963 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
33964 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f 4e  geP5(v, SQLITE_N
33965 55 4c 4c 45 51 29 3b 0a 20 20 20 20 20 20 20 20  ULLEQ);.        
33966 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  }.        assert
33967 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  ( sqlite3VdbeCur
33968 72 65 6e 74 41 64 64 72 28 76 29 3d 3d 69 4a 75  rentAddr(v)==iJu
33969 6d 70 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  mp );.        sq
3396a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
3396b 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 65 67 52  v, OP_Copy, regR
3396c 65 73 75 6c 74 2c 20 72 65 67 50 72 65 76 2c 20  esult, regPrev, 
3396d 6e 43 6f 6c 75 6d 6e 2d 31 29 3b 0a 20 20 20 20  nColumn-1);.    
3396e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3396f 20 7d 0a 0a 20 20 20 20 20 20 63 61 73 65 20 57   }..      case W
33970 48 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 55 4e  HERE_DISTINCT_UN
33971 49 51 55 45 3a 20 7b 0a 20 20 20 20 20 20 20 20  IQUE: {.        
33972 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
33973 65 54 6f 4e 6f 6f 70 28 76 2c 20 70 44 69 73 74  eToNoop(v, pDist
33974 69 6e 63 74 2d 3e 61 64 64 72 54 6e 63 74 29 3b  inct->addrTnct);
33975 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
33976 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 64        }..      d
33977 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
33978 20 20 61 73 73 65 72 74 28 20 70 44 69 73 74 69    assert( pDisti
33979 6e 63 74 2d 3e 65 54 6e 63 74 54 79 70 65 3d 3d  nct->eTnctType==
3397a 57 48 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 55  WHERE_DISTINCT_U
3397b 4e 4f 52 44 45 52 45 44 20 29 3b 0a 20 20 20 20  NORDERED );.    
3397c 20 20 20 20 63 6f 64 65 44 69 73 74 69 6e 63 74      codeDistinct
3397d 28 70 50 61 72 73 65 2c 20 70 44 69 73 74 69 6e  (pParse, pDistin
3397e 63 74 2d 3e 74 61 62 54 6e 63 74 2c 20 69 43 6f  ct->tabTnct, iCo
3397f 6e 74 69 6e 75 65 2c 20 6e 43 6f 6c 75 6d 6e 2c  ntinue, nColumn,
33980 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20 20 20   regResult);.   
33981 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
33982 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
33983 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 7b  ( pOrderBy==0 ){
33984 0a 20 20 20 20 20 20 63 6f 64 65 4f 66 66 73 65  .      codeOffse
33985 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e 75  t(v, p, iContinu
33986 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  e);.    }.  }.. 
33987 20 73 77 69 74 63 68 28 20 65 44 65 73 74 20 29   switch( eDest )
33988 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73  {.    /* In this
33989 20 6d 6f 64 65 2c 20 77 72 69 74 65 20 65 61 63   mode, write eac
3398a 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 20 74  h query result t
3398b 6f 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65  o the key of the
3398c 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 2a   temporary.    *
3398d 2a 20 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20  * table iParm.. 
3398e 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51     */.#ifndef SQ
3398f 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55  LITE_OMIT_COMPOU
33990 4e 44 5f 53 45 4c 45 43 54 0a 20 20 20 20 63 61  ND_SELECT.    ca
33991 73 65 20 53 52 54 5f 55 6e 69 6f 6e 3a 20 7b 0a  se SRT_Union: {.
33992 20 20 20 20 20 20 69 6e 74 20 72 31 3b 0a 20 20        int r1;.  
33993 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
33994 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
33995 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
33996 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
33997 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
33998 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e  gResult, nColumn
33999 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r1);.      sql
3399a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
3399b 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
3399c 69 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20  iParm, r1);.    
3399d 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
3399e 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
3399f 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  r1);.      break
339a0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
339a1 43 6f 6e 73 74 72 75 63 74 20 61 20 72 65 63 6f  Construct a reco
339a2 72 64 20 66 72 6f 6d 20 74 68 65 20 71 75 65 72  rd from the quer
339a3 79 20 72 65 73 75 6c 74 2c 20 62 75 74 20 69 6e  y result, but in
339a4 73 74 65 61 64 20 6f 66 0a 20 20 20 20 2a 2a 20  stead of.    ** 
339a5 73 61 76 69 6e 67 20 74 68 61 74 20 72 65 63 6f  saving that reco
339a6 72 64 2c 20 75 73 65 20 69 74 20 61 73 20 61 20  rd, use it as a 
339a7 6b 65 79 20 74 6f 20 64 65 6c 65 74 65 20 65 6c  key to delete el
339a8 65 6d 65 6e 74 73 20 66 72 6f 6d 0a 20 20 20 20  ements from.    
339a9 2a 2a 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  ** the temporary
339aa 20 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20 20   table iParm..  
339ab 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
339ac 54 5f 45 78 63 65 70 74 3a 20 7b 0a 20 20 20 20  T_Except: {.    
339ad 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
339ae 4f 70 33 28 76 2c 20 4f 50 5f 49 64 78 44 65 6c  Op3(v, OP_IdxDel
339af 65 74 65 2c 20 69 50 61 72 6d 2c 20 72 65 67 52  ete, iParm, regR
339b0 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b  esult, nColumn);
339b1 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
339b2 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
339b3 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65 73  /* Store the res
339b4 75 6c 74 20 61 73 20 64 61 74 61 20 75 73 69 6e  ult as data usin
339b5 67 20 61 20 75 6e 69 71 75 65 20 6b 65 79 2e 0a  g a unique key..
339b6 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
339b7 53 52 54 5f 54 61 62 6c 65 3a 0a 20 20 20 20 63  SRT_Table:.    c
339b8 61 73 65 20 53 52 54 5f 45 70 68 65 6d 54 61 62  ase SRT_EphemTab
339b9 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 31  : {.      int r1
339ba 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
339bb 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
339bc 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44      testcase( eD
339bd 65 73 74 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29  est==SRT_Table )
339be 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
339bf 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68  ( eDest==SRT_Eph
339c0 65 6d 54 61 62 20 29 3b 0a 20 20 20 20 20 20 73  emTab );.      s
339c1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
339c2 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
339c3 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43  d, regResult, nC
339c4 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a 20 20 20 20  olumn, r1);.    
339c5 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29    if( pOrderBy )
339c6 7b 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e  {.        pushOn
339c7 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c  toSorter(pParse,
339c8 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 31   pOrderBy, p, r1
339c9 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
339ca 20 20 20 20 20 20 20 20 69 6e 74 20 72 32 20 3d          int r2 =
339cb 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
339cc 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
339cd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
339ce 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
339cf 6f 77 69 64 2c 20 69 50 61 72 6d 2c 20 72 32 29  owid, iParm, r2)
339d0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
339d1 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
339d2 50 5f 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c  P_Insert, iParm,
339d3 20 72 31 2c 20 72 32 29 3b 0a 20 20 20 20 20 20   r1, r2);.      
339d4 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
339d5 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f  ngeP5(v, OPFLAG_
339d6 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 20  APPEND);.       
339d7 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
339d8 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
339d9 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  2);.      }.    
339da 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
339db 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
339dc 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  r1);.      break
339dd 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66  ;.    }..#ifndef
339de 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
339df 51 55 45 52 59 0a 20 20 20 20 2f 2a 20 49 66 20  QUERY.    /* If 
339e0 77 65 20 61 72 65 20 63 72 65 61 74 69 6e 67 20  we are creating 
339e1 61 20 73 65 74 20 66 6f 72 20 61 6e 20 22 65 78  a set for an "ex
339e2 70 72 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e  pr IN (SELECT ..
339e3 2e 29 22 20 63 6f 6e 73 74 72 75 63 74 2c 0a 20  .)" construct,. 
339e4 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 72 65     ** then there
339e5 20 73 68 6f 75 6c 64 20 62 65 20 61 20 73 69 6e   should be a sin
339e6 67 6c 65 20 69 74 65 6d 20 6f 6e 20 74 68 65 20  gle item on the 
339e7 73 74 61 63 6b 2e 20 20 57 72 69 74 65 20 74 68  stack.  Write th
339e8 69 73 0a 20 20 20 20 2a 2a 20 69 74 65 6d 20 69  is.    ** item i
339e9 6e 74 6f 20 74 68 65 20 73 65 74 20 74 61 62 6c  nto the set tabl
339ea 65 20 77 69 74 68 20 62 6f 67 75 73 20 64 61 74  e with bogus dat
339eb 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  a..    */.    ca
339ec 73 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20 20  se SRT_Set: {.  
339ed 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c      assert( nCol
339ee 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  umn==1 );.      
339ef 70 44 65 73 74 2d 3e 61 66 66 53 64 73 74 20 3d  pDest->affSdst =
339f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
339f1 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72     sqlite3Compar
339f2 65 41 66 66 69 6e 69 74 79 28 70 45 4c 69 73 74  eAffinity(pEList
339f3 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 70 44  ->a[0].pExpr, pD
339f4 65 73 74 2d 3e 61 66 66 53 64 73 74 29 3b 0a 20  est->affSdst);. 
339f5 20 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42       if( pOrderB
339f6 79 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  y ){.        /* 
339f7 41 74 20 66 69 72 73 74 20 67 6c 61 6e 63 65 20  At first glance 
339f8 79 6f 75 20 77 6f 75 6c 64 20 74 68 69 6e 6b 20  you would think 
339f9 77 65 20 63 6f 75 6c 64 20 6f 70 74 69 6d 69 7a  we could optimiz
339fa 65 20 6f 75 74 20 74 68 65 0a 20 20 20 20 20 20  e out the.      
339fb 20 20 2a 2a 20 4f 52 44 45 52 20 42 59 20 69 6e    ** ORDER BY in
339fc 20 74 68 69 73 20 63 61 73 65 20 73 69 6e 63 65   this case since
339fd 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 65 6e   the order of en
339fe 74 72 69 65 73 20 69 6e 20 74 68 65 20 73 65 74  tries in the set
339ff 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 6f 65 73  .        ** does
33a00 20 6e 6f 74 20 6d 61 74 74 65 72 2e 20 20 42 75   not matter.  Bu
33a01 74 20 74 68 65 72 65 20 6d 69 67 68 74 20 62 65  t there might be
33a02 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2c   a LIMIT clause,
33a03 20 69 6e 20 77 68 69 63 68 0a 20 20 20 20 20 20   in which.      
33a04 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20 6f 72    ** case the or
33a05 64 65 72 20 64 6f 65 73 20 6d 61 74 74 65 72 20  der does matter 
33a06 2a 2f 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f  */.        pushO
33a07 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65  ntoSorter(pParse
33a08 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72  , pOrderBy, p, r
33a09 65 67 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 20  egResult);.     
33a0a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
33a0b 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33  int r1 = sqlite3
33a0c 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
33a0d 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e);.        sqli
33a0e 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
33a0f 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
33a10 72 65 67 52 65 73 75 6c 74 2c 31 2c 72 31 2c 20  regResult,1,r1, 
33a11 26 70 44 65 73 74 2d 3e 61 66 66 53 64 73 74 2c  &pDest->affSdst,
33a12 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   1);.        sql
33a13 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
33a14 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
33a15 73 65 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 31  se, regResult, 1
33a16 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
33a17 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
33a18 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50  OP_IdxInsert, iP
33a19 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  arm, r1);.      
33a1a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
33a1b 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
33a1c 72 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  r1);.      }.   
33a1d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
33a1e 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 79 20 72  .    /* If any r
33a1f 6f 77 20 65 78 69 73 74 20 69 6e 20 74 68 65 20  ow exist in the 
33a20 72 65 73 75 6c 74 20 73 65 74 2c 20 72 65 63 6f  result set, reco
33a21 72 64 20 74 68 61 74 20 66 61 63 74 20 61 6e 64  rd that fact and
33a22 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a 2f 0a 20   abort..    */. 
33a23 20 20 20 63 61 73 65 20 53 52 54 5f 45 78 69 73     case SRT_Exis
33a24 74 73 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  ts: {.      sqli
33a25 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
33a26 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
33a27 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 2f 2a  iParm);.      /*
33a28 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73   The LIMIT claus
33a29 65 20 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65  e will terminate
33a2a 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73   the loop for us
33a2b 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   */.      break;
33a2c 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
33a2d 66 20 74 68 69 73 20 69 73 20 61 20 73 63 61 6c  f this is a scal
33a2e 61 72 20 73 65 6c 65 63 74 20 74 68 61 74 20 69  ar select that i
33a2f 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70  s part of an exp
33a30 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20 20  ression, then.  
33a31 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72    ** store the r
33a32 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 61 70  esults in the ap
33a33 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72 79  propriate memory
33a34 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b 20   cell and break 
33a35 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  out.    ** of th
33a36 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20  e scan loop..   
33a37 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54   */.    case SRT
33a38 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73  _Mem: {.      as
33a39 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31  sert( nColumn==1
33a3a 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f   );.      if( pO
33a3b 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20  rderBy ){.      
33a3c 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72    pushOntoSorter
33a3d 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42  (pParse, pOrderB
33a3e 79 2c 20 70 2c 20 72 65 67 52 65 73 75 6c 74 29  y, p, regResult)
33a3f 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
33a40 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
33a41 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73  prCodeMove(pPars
33a42 65 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 69 50  e, regResult, iP
33a43 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  arm, 1);.       
33a44 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c   /* The LIMIT cl
33a45 61 75 73 65 20 77 69 6c 6c 20 6a 75 6d 70 20 6f  ause will jump o
33a46 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 66  ut of the loop f
33a47 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 7d  or us */.      }
33a48 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
33a49 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69    }.#endif /* #i
33a4a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
33a4b 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 20  T_SUBQUERY */.. 
33a4c 20 20 20 2f 2a 20 53 65 6e 64 20 74 68 65 20 64     /* Send the d
33a4d 61 74 61 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  ata to the callb
33a4e 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20  ack function or 
33a4f 74 6f 20 61 20 73 75 62 72 6f 75 74 69 6e 65 2e  to a subroutine.
33a50 20 20 49 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20    In the.    ** 
33a51 63 61 73 65 20 6f 66 20 61 20 73 75 62 72 6f 75  case of a subrou
33a52 74 69 6e 65 2c 20 74 68 65 20 73 75 62 72 6f 75  tine, the subrou
33a53 74 69 6e 65 20 69 74 73 65 6c 66 20 69 73 20 72  tine itself is r
33a54 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 20  esponsible for. 
33a55 20 20 20 2a 2a 20 70 6f 70 70 69 6e 67 20 74 68     ** popping th
33a56 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20  e data from the 
33a57 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20  stack..    */.  
33a58 20 20 63 61 73 65 20 53 52 54 5f 43 6f 72 6f 75    case SRT_Corou
33a59 74 69 6e 65 3a 0a 20 20 20 20 63 61 73 65 20 53  tine:.    case S
33a5a 52 54 5f 4f 75 74 70 75 74 3a 20 7b 0a 20 20 20  RT_Output: {.   
33a5b 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65     testcase( eDe
33a5c 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e  st==SRT_Coroutin
33a5d 65 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  e );.      testc
33a5e 61 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f  ase( eDest==SRT_
33a5f 4f 75 74 70 75 74 20 29 3b 0a 20 20 20 20 20 20  Output );.      
33a60 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a  if( pOrderBy ){.
33a61 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d          int r1 =
33a62 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
33a63 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
33a64 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
33a65 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
33a66 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73 75 6c  Record, regResul
33a67 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b  t, nColumn, r1);
33a68 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74  .        pushOnt
33a69 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20  oSorter(pParse, 
33a6a 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 31 29  pOrderBy, p, r1)
33a6b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
33a6c 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
33a6d 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20  pParse, r1);.   
33a6e 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 44 65     }else if( eDe
33a6f 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e  st==SRT_Coroutin
33a70 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e ){.        sql
33a71 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
33a72 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65 73  , OP_Yield, pDes
33a73 74 2d 3e 69 53 44 50 61 72 6d 29 3b 0a 20 20 20  t->iSDParm);.   
33a74 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
33a75 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
33a76 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
33a77 52 6f 77 2c 20 72 65 67 52 65 73 75 6c 74 2c 20  Row, regResult, 
33a78 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20  nColumn);.      
33a79 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
33a7a 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
33a7b 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 73 75  (pParse, regResu
33a7c 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  lt, nColumn);.  
33a7d 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
33a7e 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20 21 64  k;.    }..#if !d
33a7f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
33a80 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20  IT_TRIGGER).    
33a81 2f 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 72  /* Discard the r
33a82 65 73 75 6c 74 73 2e 20 20 54 68 69 73 20 69 73  esults.  This is
33a83 20 75 73 65 64 20 66 6f 72 20 53 45 4c 45 43 54   used for SELECT
33a84 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69   statements insi
33a85 64 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 62 6f  de.    ** the bo
33a86 64 79 20 6f 66 20 61 20 54 52 49 47 47 45 52 2e  dy of a TRIGGER.
33a87 20 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66    The purpose of
33a88 20 73 75 63 68 20 73 65 6c 65 63 74 73 20 69 73   such selects is
33a89 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20   to call.    ** 
33a8a 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e  user-defined fun
33a8b 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65  ctions that have
33a8c 20 73 69 64 65 20 65 66 66 65 63 74 73 2e 20 20   side effects.  
33a8d 57 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 0a 20  We do not care. 
33a8e 20 20 20 2a 2a 20 61 62 6f 75 74 20 74 68 65 20     ** about the 
33a8f 61 63 74 75 61 6c 20 72 65 73 75 6c 74 73 20 6f  actual results o
33a90 66 20 74 68 65 20 73 65 6c 65 63 74 2e 0a 20 20  f the select..  
33a91 20 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74    */.    default
33a92 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
33a93 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 44 69 73  ( eDest==SRT_Dis
33a94 63 61 72 64 20 29 3b 0a 20 20 20 20 20 20 62 72  card );.      br
33a95 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
33a96 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70  f.  }..  /* Jump
33a97 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74   to the end of t
33a98 68 65 20 6c 6f 6f 70 20 69 66 20 74 68 65 20 4c  he loop if the L
33a99 49 4d 49 54 20 69 73 20 72 65 61 63 68 65 64 2e  IMIT is reached.
33a9a 20 20 45 78 63 65 70 74 2c 20 69 66 0a 20 20 2a    Except, if.  *
33a9b 2a 20 74 68 65 72 65 20 69 73 20 61 20 73 6f 72  * there is a sor
33a9c 74 65 72 2c 20 69 6e 20 77 68 69 63 68 20 63 61  ter, in which ca
33a9d 73 65 20 74 68 65 20 73 6f 72 74 65 72 20 68 61  se the sorter ha
33a9e 73 20 61 6c 72 65 61 64 79 20 6c 69 6d 69 74 65  s already limite
33a9f 64 0a 20 20 2a 2a 20 74 68 65 20 6f 75 74 70 75  d.  ** the outpu
33aa0 74 20 66 6f 72 20 75 73 2e 0a 20 20 2a 2f 0a 20  t for us..  */. 
33aa1 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30   if( pOrderBy==0
33aa2 20 26 26 20 70 2d 3e 69 4c 69 6d 69 74 20 29 7b   && p->iLimit ){
33aa3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
33aa4 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 66 5a  AddOp3(v, OP_IfZ
33aa5 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20  ero, p->iLimit, 
33aa6 69 42 72 65 61 6b 2c 20 2d 31 29 3b 0a 20 20 7d  iBreak, -1);.  }
33aa7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  .}../*.** Given 
33aa8 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  an expression li
33aa9 73 74 2c 20 67 65 6e 65 72 61 74 65 20 61 20 4b  st, generate a K
33aaa 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
33aab 20 74 68 61 74 20 72 65 63 6f 72 64 73 0a 2a 2a   that records.**
33aac 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
33aad 65 71 75 65 6e 63 65 20 66 6f 72 20 65 61 63 68  equence for each
33aae 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
33aaf 68 61 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  hat expression l
33ab0 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ist..**.** If th
33ab1 65 20 45 78 70 72 4c 69 73 74 20 69 73 20 61 6e  e ExprList is an
33ab2 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f   ORDER BY or GRO
33ab3 55 50 20 42 59 20 63 6c 61 75 73 65 20 74 68 65  UP BY clause the
33ab4 6e 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 0a  n the resulting.
33ab5 2a 2a 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63  ** KeyInfo struc
33ab6 74 75 72 65 20 69 73 20 61 70 70 72 6f 70 72 69  ture is appropri
33ab7 61 74 65 20 66 6f 72 20 69 6e 69 74 69 61 6c 69  ate for initiali
33ab8 7a 69 6e 67 20 61 20 76 69 72 74 75 61 6c 20 69  zing a virtual i
33ab9 6e 64 65 78 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65  ndex to.** imple
33aba 6d 65 6e 74 20 74 68 61 74 20 63 6c 61 75 73 65  ment that clause
33abb 2e 20 20 49 66 20 74 68 65 20 45 78 70 72 4c 69  .  If the ExprLi
33abc 73 74 20 69 73 20 74 68 65 20 72 65 73 75 6c 74  st is the result
33abd 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54   set of a SELECT
33abe 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 4b 65 79  .** then the Key
33abf 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69  Info structure i
33ac0 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  s appropriate fo
33ac1 72 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61  r initializing a
33ac2 20 76 69 72 74 75 61 6c 0a 2a 2a 20 69 6e 64 65   virtual.** inde
33ac3 78 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61  x to implement a
33ac4 20 44 49 53 54 49 4e 43 54 20 74 65 73 74 2e 0a   DISTINCT test..
33ac5 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68  **.** Space to h
33ac6 6f 6c 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20  old the KeyInfo 
33ac7 73 74 72 75 63 74 75 72 65 20 69 73 20 6f 62 74  structure is obt
33ac8 61 69 6e 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e  ain from malloc.
33ac9 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a    The calling.**
33aca 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73   function is res
33acb 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 65  ponsible for see
33acc 69 6e 67 20 74 68 61 74 20 74 68 69 73 20 73 74  ing that this st
33acd 72 75 63 74 75 72 65 20 69 73 20 65 76 65 6e 74  ructure is event
33ace 75 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 2e 20  ually.** freed. 
33acf 20 41 64 64 20 74 68 65 20 4b 65 79 49 6e 66 6f   Add the KeyInfo
33ad0 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 68   structure to th
33ad1 65 20 50 34 20 66 69 65 6c 64 20 6f 66 20 61 6e  e P4 field of an
33ad2 20 6f 70 63 6f 64 65 20 75 73 69 6e 67 0a 2a 2a   opcode using.**
33ad3 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
33ad4 4f 46 46 20 69 73 20 74 68 65 20 75 73 75 61 6c  OFF is the usual
33ad5 20 77 61 79 20 6f 66 20 64 65 61 6c 69 6e 67 20   way of dealing 
33ad6 77 69 74 68 20 74 68 69 73 2e 0a 2a 2f 0a 73 74  with this..*/.st
33ad7 61 74 69 63 20 4b 65 79 49 6e 66 6f 20 2a 6b 65  atic KeyInfo *ke
33ad8 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73  yInfoFromExprLis
33ad9 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
33ada 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
33adb 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
33adc 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
33add 20 69 6e 74 20 6e 45 78 70 72 3b 0a 20 20 4b 65   int nExpr;.  Ke
33ade 79 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20  yInfo *pInfo;.  
33adf 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
33ae0 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69  item *pItem;.  i
33ae1 6e 74 20 69 3b 0a 0a 20 20 6e 45 78 70 72 20 3d  nt i;..  nExpr =
33ae2 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20   pList->nExpr;. 
33ae3 20 70 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33   pInfo = sqlite3
33ae4 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
33ae5 20 73 69 7a 65 6f 66 28 2a 70 49 6e 66 6f 29 20   sizeof(*pInfo) 
33ae6 2b 20 6e 45 78 70 72 2a 28 73 69 7a 65 6f 66 28  + nExpr*(sizeof(
33ae7 43 6f 6c 6c 53 65 71 2a 29 2b 31 29 20 29 3b 0a  CollSeq*)+1) );.
33ae8 20 20 69 66 28 20 70 49 6e 66 6f 20 29 7b 0a 20    if( pInfo ){. 
33ae9 20 20 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f     pInfo->aSortO
33aea 72 64 65 72 20 3d 20 28 75 38 2a 29 26 70 49 6e  rder = (u8*)&pIn
33aeb 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70 72 5d  fo->aColl[nExpr]
33aec 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 46 69  ;.    pInfo->nFi
33aed 65 6c 64 20 3d 20 28 75 31 36 29 6e 45 78 70 72  eld = (u16)nExpr
33aee 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 65 6e 63  ;.    pInfo->enc
33aef 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20   = ENC(db);.    
33af0 70 49 6e 66 6f 2d 3e 64 62 20 3d 20 64 62 3b 0a  pInfo->db = db;.
33af1 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74      for(i=0, pIt
33af2 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 6e  em=pList->a; i<n
33af3 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
33af4 2b 2b 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53  ++){.      CollS
33af5 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  eq *pColl;.     
33af6 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
33af7 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
33af8 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  se, pItem->pExpr
33af9 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43  );.      if( !pC
33afa 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70  oll ){.        p
33afb 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74  Coll = db->pDflt
33afc 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Coll;.      }.  
33afd 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 6c      pInfo->aColl
33afe 5b 69 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20  [i] = pColl;.   
33aff 20 20 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f     pInfo->aSortO
33b00 72 64 65 72 5b 69 5d 20 3d 20 70 49 74 65 6d 2d  rder[i] = pItem-
33b01 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20  >sortOrder;.    
33b02 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
33b03 49 6e 66 6f 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  Info;.}..#ifndef
33b04 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
33b05 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a  POUND_SELECT./*.
33b06 2a 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  ** Name of the c
33b07 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 72 61 74  onnection operat
33b08 6f 72 2c 20 75 73 65 64 20 66 6f 72 20 65 72 72  or, used for err
33b09 6f 72 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2f 0a  or messages..*/.
33b0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
33b0b 72 20 2a 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28  r *selectOpName(
33b0c 69 6e 74 20 69 64 29 7b 0a 20 20 63 68 61 72 20  int id){.  char 
33b0d 2a 7a 3b 0a 20 20 73 77 69 74 63 68 28 20 69 64  *z;.  switch( id
33b0e 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   ){.    case TK_
33b0f 41 4c 4c 3a 20 20 20 20 20 20 20 7a 20 3d 20 22  ALL:       z = "
33b10 55 4e 49 4f 4e 20 41 4c 4c 22 3b 20 20 20 62 72  UNION ALL";   br
33b11 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 54 4b  eak;.    case TK
33b12 5f 49 4e 54 45 52 53 45 43 54 3a 20 7a 20 3d 20  _INTERSECT: z = 
33b13 22 49 4e 54 45 52 53 45 43 54 22 3b 20 20 20 62  "INTERSECT";   b
33b14 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 54  reak;.    case T
33b15 4b 5f 45 58 43 45 50 54 3a 20 20 20 20 7a 20 3d  K_EXCEPT:    z =
33b16 20 22 45 58 43 45 50 54 22 3b 20 20 20 20 20 20   "EXCEPT";      
33b17 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75  break;.    defau
33b18 6c 74 3a 20 20 20 20 20 20 20 20 20 20 20 7a 20  lt:           z 
33b19 3d 20 22 55 4e 49 4f 4e 22 3b 20 20 20 20 20 20  = "UNION";      
33b1a 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65   break;.  }.  re
33b1b 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65 6e 64 69 66  turn z;.}.#endif
33b1c 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
33b1d 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20  COMPOUND_SELECT 
33b1e 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
33b1f 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a  TE_OMIT_EXPLAIN.
33b20 2f 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 61 6e 20  /*.** Unless an 
33b21 22 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50  "EXPLAIN QUERY P
33b22 4c 41 4e 22 20 63 6f 6d 6d 61 6e 64 20 69 73 20  LAN" command is 
33b23 62 65 69 6e 67 20 70 72 6f 63 65 73 73 65 64 2c  being processed,
33b24 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a   this function.*
33b25 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 4f 74  * is a no-op. Ot
33b26 68 65 72 77 69 73 65 2c 20 69 74 20 61 64 64 73  herwise, it adds
33b27 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66   a single row of
33b28 20 6f 75 74 70 75 74 20 74 6f 20 74 68 65 20 45   output to the E
33b29 51 50 20 72 65 73 75 6c 74 2c 0a 2a 2a 20 77 68  QP result,.** wh
33b2a 65 72 65 20 74 68 65 20 63 61 70 74 69 6f 6e 20  ere the caption 
33b2b 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a  is of the form:.
33b2c 2a 2a 0a 2a 2a 20 20 20 22 55 53 45 20 54 45 4d  **.**   "USE TEM
33b2d 50 20 42 2d 54 52 45 45 20 46 4f 52 20 78 78 78  P B-TREE FOR xxx
33b2e 22 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 78 78  ".**.** where xx
33b2f 78 20 69 73 20 6f 6e 65 20 6f 66 20 22 44 49 53  x is one of "DIS
33b30 54 49 4e 43 54 22 2c 20 22 4f 52 44 45 52 20 42  TINCT", "ORDER B
33b31 59 22 20 6f 72 20 22 47 52 4f 55 50 20 42 59 22  Y" or "GROUP BY"
33b32 2e 20 45 78 61 63 74 6c 79 20 77 68 69 63 68 0a  . Exactly which.
33b33 2a 2a 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  ** is determined
33b34 20 62 79 20 74 68 65 20 7a 55 73 61 67 65 20 61   by the zUsage a
33b35 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74  rgument..*/.stat
33b36 69 63 20 76 6f 69 64 20 65 78 70 6c 61 69 6e 54  ic void explainT
33b37 65 6d 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a  empTable(Parse *
33b38 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
33b39 61 72 20 2a 7a 55 73 61 67 65 29 7b 0a 20 20 69  ar *zUsage){.  i
33b3a 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61  f( pParse->expla
33b3b 69 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 56 64 62  in==2 ){.    Vdb
33b3c 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
33b3d 56 64 62 65 3b 0a 20 20 20 20 63 68 61 72 20 2a  Vdbe;.    char *
33b3e 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50  zMsg = sqlite3MP
33b3f 72 69 6e 74 66 28 70 50 61 72 73 65 2d 3e 64 62  rintf(pParse->db
33b40 2c 20 22 55 53 45 20 54 45 4d 50 20 42 2d 54 52  , "USE TEMP B-TR
33b41 45 45 20 46 4f 52 20 25 73 22 2c 20 7a 55 73 61  EE FOR %s", zUsa
33b42 67 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ge);.    sqlite3
33b43 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
33b44 5f 45 78 70 6c 61 69 6e 2c 20 70 50 61 72 73 65  _Explain, pParse
33b45 2d 3e 69 53 65 6c 65 63 74 49 64 2c 20 30 2c 20  ->iSelectId, 0, 
33b46 30 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41  0, zMsg, P4_DYNA
33b47 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  MIC);.  }.}../*.
33b48 2a 2a 20 41 73 73 69 67 6e 20 65 78 70 72 65 73  ** Assign expres
33b49 73 69 6f 6e 20 62 20 74 6f 20 6c 76 61 6c 75 65  sion b to lvalue
33b4a 20 61 2e 20 41 20 73 65 63 6f 6e 64 2c 20 6e 6f   a. A second, no
33b4b 2d 6f 70 2c 20 76 65 72 73 69 6f 6e 20 6f 66 20  -op, version of 
33b4c 74 68 69 73 20 6d 61 63 72 6f 0a 2a 2a 20 69 73  this macro.** is
33b4d 20 70 72 6f 76 69 64 65 64 20 77 68 65 6e 20 53   provided when S
33b4e 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
33b4f 49 4e 20 69 73 20 64 65 66 69 6e 65 64 2e 20 54  IN is defined. T
33b50 68 69 73 20 61 6c 6c 6f 77 73 20 74 68 65 20 63  his allows the c
33b51 6f 64 65 0a 2a 2a 20 69 6e 20 73 71 6c 69 74 65  ode.** in sqlite
33b52 33 53 65 6c 65 63 74 28 29 20 74 6f 20 61 73 73  3Select() to ass
33b53 69 67 6e 20 76 61 6c 75 65 73 20 74 6f 20 73 74  ign values to st
33b54 72 75 63 74 75 72 65 20 6d 65 6d 62 65 72 20 76  ructure member v
33b55 61 72 69 61 62 6c 65 73 20 74 68 61 74 0a 2a 2a  ariables that.**
33b56 20 6f 6e 6c 79 20 65 78 69 73 74 20 69 66 20 53   only exist if S
33b57 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
33b58 49 4e 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65  IN is not define
33b59 64 20 77 69 74 68 6f 75 74 20 70 6f 6c 6c 75 74  d without pollut
33b5a 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20  ing the.** code 
33b5b 77 69 74 68 20 23 69 66 6e 64 65 66 20 64 69 72  with #ifndef dir
33b5c 65 63 74 69 76 65 73 2e 0a 2a 2f 0a 23 20 64 65  ectives..*/.# de
33b5d 66 69 6e 65 20 65 78 70 6c 61 69 6e 53 65 74 49  fine explainSetI
33b5e 6e 74 65 67 65 72 28 61 2c 20 62 29 20 61 20 3d  nteger(a, b) a =
33b5f 20 62 0a 0a 23 65 6c 73 65 0a 2f 2a 20 4e 6f 2d   b..#else./* No-
33b60 6f 70 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74  op versions of t
33b61 68 65 20 65 78 70 6c 61 69 6e 58 58 58 28 29 20  he explainXXX() 
33b62 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 6d 61  functions and ma
33b63 63 72 6f 73 2e 20 2a 2f 0a 23 20 64 65 66 69 6e  cros. */.# defin
33b64 65 20 65 78 70 6c 61 69 6e 54 65 6d 70 54 61 62  e explainTempTab
33b65 6c 65 28 79 2c 7a 29 0a 23 20 64 65 66 69 6e 65  le(y,z).# define
33b66 20 65 78 70 6c 61 69 6e 53 65 74 49 6e 74 65 67   explainSetInteg
33b67 65 72 28 79 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a  er(y,z).#endif..
33b68 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
33b69 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
33b6a 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
33b6b 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55  LITE_OMIT_COMPOU
33b6c 4e 44 5f 53 45 4c 45 43 54 29 0a 2f 2a 0a 2a 2a  ND_SELECT)./*.**
33b6d 20 55 6e 6c 65 73 73 20 61 6e 20 22 45 58 50 4c   Unless an "EXPL
33b6e 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 22 20  AIN QUERY PLAN" 
33b6f 63 6f 6d 6d 61 6e 64 20 69 73 20 62 65 69 6e 67  command is being
33b70 20 70 72 6f 63 65 73 73 65 64 2c 20 74 68 69 73   processed, this
33b71 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20   function.** is 
33b72 61 20 6e 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69  a no-op. Otherwi
33b73 73 65 2c 20 69 74 20 61 64 64 73 20 61 20 73 69  se, it adds a si
33b74 6e 67 6c 65 20 72 6f 77 20 6f 66 20 6f 75 74 70  ngle row of outp
33b75 75 74 20 74 6f 20 74 68 65 20 45 51 50 20 72 65  ut to the EQP re
33b76 73 75 6c 74 2c 0a 2a 2a 20 77 68 65 72 65 20 74  sult,.** where t
33b77 68 65 20 63 61 70 74 69 6f 6e 20 69 73 20 6f 66  he caption is of
33b78 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 77 6f 20   one of the two 
33b79 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 22  forms:.**.**   "
33b7a 43 4f 4d 50 4f 53 49 54 45 20 53 55 42 51 55 45  COMPOSITE SUBQUE
33b7b 52 49 45 53 20 69 53 75 62 31 20 61 6e 64 20 69  RIES iSub1 and i
33b7c 53 75 62 32 20 28 6f 70 29 22 0a 2a 2a 20 20 20  Sub2 (op)".**   
33b7d 22 43 4f 4d 50 4f 53 49 54 45 20 53 55 42 51 55  "COMPOSITE SUBQU
33b7e 45 52 49 45 53 20 69 53 75 62 31 20 61 6e 64 20  ERIES iSub1 and 
33b7f 69 53 75 62 32 20 55 53 49 4e 47 20 54 45 4d 50  iSub2 USING TEMP
33b80 20 42 2d 54 52 45 45 20 28 6f 70 29 22 0a 2a 2a   B-TREE (op)".**
33b81 0a 2a 2a 20 77 68 65 72 65 20 69 53 75 62 31 20  .** where iSub1 
33b82 61 6e 64 20 69 53 75 62 32 20 61 72 65 20 74 68  and iSub2 are th
33b83 65 20 69 6e 74 65 67 65 72 73 20 70 61 73 73 65  e integers passe
33b84 64 20 61 73 20 74 68 65 20 63 6f 72 72 65 73 70  d as the corresp
33b85 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69  onding.** functi
33b86 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 2c 20 61  on parameters, a
33b87 6e 64 20 6f 70 20 69 73 20 74 68 65 20 74 65 78  nd op is the tex
33b88 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  t representation
33b89 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   of the paramete
33b8a 72 0a 2a 2a 20 6f 66 20 74 68 65 20 73 61 6d 65  r.** of the same
33b8b 20 6e 61 6d 65 2e 20 54 68 65 20 70 61 72 61 6d   name. The param
33b8c 65 74 65 72 20 22 6f 70 22 20 6d 75 73 74 20 62  eter "op" must b
33b8d 65 20 6f 6e 65 20 6f 66 20 54 4b 5f 55 4e 49 4f  e one of TK_UNIO
33b8e 4e 2c 20 54 4b 5f 45 58 43 45 50 54 2c 0a 2a 2a  N, TK_EXCEPT,.**
33b8f 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 6f 72   TK_INTERSECT or
33b90 20 54 4b 5f 41 4c 4c 2e 20 54 68 65 20 66 69 72   TK_ALL. The fir
33b91 73 74 20 66 6f 72 6d 20 69 73 20 75 73 65 64 20  st form is used 
33b92 69 66 20 61 72 67 75 6d 65 6e 74 20 62 55 73 65  if argument bUse
33b93 54 6d 70 20 69 73 20 0a 2a 2a 20 66 61 6c 73 65  Tmp is .** false
33b94 2c 20 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20  , or the second 
33b95 66 6f 72 6d 20 69 66 20 69 74 20 69 73 20 74 72  form if it is tr
33b96 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ue..*/.static vo
33b97 69 64 20 65 78 70 6c 61 69 6e 43 6f 6d 70 6f 73  id explainCompos
33b98 69 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ite(.  Parse *pP
33b99 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
33b9a 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20         /* Parse 
33b9b 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
33b9c 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   op,            
33b9d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
33b9e 4f 6e 65 20 6f 66 20 54 4b 5f 55 4e 49 4f 4e 2c  One of TK_UNION,
33b9f 20 54 4b 5f 45 58 43 45 50 54 20 65 74 63 2e 20   TK_EXCEPT etc. 
33ba0 2a 2f 0a 20 20 69 6e 74 20 69 53 75 62 31 2c 20  */.  int iSub1, 
33ba1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ba2 20 20 20 20 20 2f 2a 20 53 75 62 71 75 65 72 79       /* Subquery
33ba3 20 69 64 20 31 20 2a 2f 0a 20 20 69 6e 74 20 69   id 1 */.  int i
33ba4 53 75 62 32 2c 20 20 20 20 20 20 20 20 20 20 20  Sub2,           
33ba5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75             /* Su
33ba6 62 71 75 65 72 79 20 69 64 20 32 20 2a 2f 0a 20  bquery id 2 */. 
33ba7 20 69 6e 74 20 62 55 73 65 54 6d 70 20 20 20 20   int bUseTmp    
33ba8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ba9 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 74 65   /* True if a te
33baa 6d 70 20 74 61 62 6c 65 20 77 61 73 20 75 73 65  mp table was use
33bab 64 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74  d */.){.  assert
33bac 28 20 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 7c  ( op==TK_UNION |
33bad 7c 20 6f 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20  | op==TK_EXCEPT 
33bae 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53  || op==TK_INTERS
33baf 45 43 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 41 4c  ECT || op==TK_AL
33bb0 4c 20 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73  L );.  if( pPars
33bb1 65 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b  e->explain==2 ){
33bb2 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 70  .    Vdbe *v = p
33bb3 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
33bb4 20 20 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 73    char *zMsg = s
33bb5 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 0a 20  qlite3MPrintf(. 
33bb6 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64         pParse->d
33bb7 62 2c 20 22 43 4f 4d 50 4f 55 4e 44 20 53 55 42  b, "COMPOUND SUB
33bb8 51 55 45 52 49 45 53 20 25 64 20 41 4e 44 20 25  QUERIES %d AND %
33bb9 64 20 25 73 28 25 73 29 22 2c 20 69 53 75 62 31  d %s(%s)", iSub1
33bba 2c 20 69 53 75 62 32 2c 0a 20 20 20 20 20 20 20  , iSub2,.       
33bbb 20 62 55 73 65 54 6d 70 3f 22 55 53 49 4e 47 20   bUseTmp?"USING 
33bbc 54 45 4d 50 20 42 2d 54 52 45 45 20 22 3a 22 22  TEMP B-TREE ":""
33bbd 2c 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 6f  , selectOpName(o
33bbe 70 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71  p).    );.    sq
33bbf 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
33bc0 76 2c 20 4f 50 5f 45 78 70 6c 61 69 6e 2c 20 70  v, OP_Explain, p
33bc1 50 61 72 73 65 2d 3e 69 53 65 6c 65 63 74 49 64  Parse->iSelectId
33bc2 2c 20 30 2c 20 30 2c 20 7a 4d 73 67 2c 20 50 34  , 0, 0, zMsg, P4
33bc3 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d  _DYNAMIC);.  }.}
33bc4 0a 23 65 6c 73 65 0a 2f 2a 20 4e 6f 2d 6f 70 20  .#else./* No-op 
33bc5 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  versions of the 
33bc6 65 78 70 6c 61 69 6e 58 58 58 28 29 20 66 75 6e  explainXXX() fun
33bc7 63 74 69 6f 6e 73 20 61 6e 64 20 6d 61 63 72 6f  ctions and macro
33bc8 73 2e 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 65  s. */.# define e
33bc9 78 70 6c 61 69 6e 43 6f 6d 70 6f 73 69 74 65 28  xplainComposite(
33bca 76 2c 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e 64 69  v,w,x,y,z).#endi
33bcb 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  f../*.** If the 
33bcc 69 6e 6e 65 72 20 6c 6f 6f 70 20 77 61 73 20 67  inner loop was g
33bcd 65 6e 65 72 61 74 65 64 20 75 73 69 6e 67 20 61  enerated using a
33bce 20 6e 6f 6e 2d 6e 75 6c 6c 20 70 4f 72 64 65 72   non-null pOrder
33bcf 42 79 20 61 72 67 75 6d 65 6e 74 2c 0a 2a 2a 20  By argument,.** 
33bd0 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73  then the results
33bd1 20 77 65 72 65 20 70 6c 61 63 65 64 20 69 6e 20   were placed in 
33bd2 61 20 73 6f 72 74 65 72 2e 20 20 41 66 74 65 72  a sorter.  After
33bd3 20 74 68 65 20 6c 6f 6f 70 20 69 73 20 74 65 72   the loop is ter
33bd4 6d 69 6e 61 74 65 64 0a 2a 2a 20 77 65 20 6e 65  minated.** we ne
33bd5 65 64 20 74 6f 20 72 75 6e 20 74 68 65 20 73 6f  ed to run the so
33bd6 72 74 65 72 20 61 6e 64 20 6f 75 74 70 75 74 20  rter and output 
33bd7 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20 54 68  the results.  Th
33bd8 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 72  e following.** r
33bd9 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
33bda 20 74 68 65 20 63 6f 64 65 20 6e 65 65 64 65 64   the code needed
33bdb 20 74 6f 20 64 6f 20 74 68 61 74 2e 0a 2a 2f 0a   to do that..*/.
33bdc 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 6e 65  static void gene
33bdd 72 61 74 65 53 6f 72 74 54 61 69 6c 28 0a 20 20  rateSortTail(.  
33bde 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
33bdf 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
33be0 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74  text */.  Select
33be1 20 2a 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54   *p,        /* T
33be2 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
33be3 65 6e 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76  ent */.  Vdbe *v
33be4 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65  ,          /* Ge
33be5 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f  nerate code into
33be6 20 74 68 69 73 20 56 44 42 45 20 2a 2f 0a 20 20   this VDBE */.  
33be7 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20  int nColumn,    
33be8 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
33be9 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61 20 2a  olumns of data *
33bea 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a  /.  SelectDest *
33beb 70 44 65 73 74 20 2f 2a 20 57 72 69 74 65 20 74  pDest /* Write t
33bec 68 65 20 73 6f 72 74 65 64 20 72 65 73 75 6c 74  he sorted result
33bed 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  s here */.){.  i
33bee 6e 74 20 61 64 64 72 42 72 65 61 6b 20 3d 20 73  nt addrBreak = s
33bef 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
33bf0 62 65 6c 28 76 29 3b 20 20 20 20 20 2f 2a 20 4a  bel(v);     /* J
33bf1 75 6d 70 20 68 65 72 65 20 74 6f 20 65 78 69 74  ump here to exit
33bf2 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61   loop */.  int a
33bf3 64 64 72 43 6f 6e 74 69 6e 75 65 20 3d 20 73 71  ddrContinue = sq
33bf4 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
33bf5 65 6c 28 76 29 3b 20 20 2f 2a 20 4a 75 6d 70 20  el(v);  /* Jump 
33bf6 68 65 72 65 20 66 6f 72 20 6e 65 78 74 20 63 79  here for next cy
33bf7 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  cle */.  int add
33bf8 72 3b 0a 20 20 69 6e 74 20 69 54 61 62 3b 0a 20  r;.  int iTab;. 
33bf9 20 69 6e 74 20 70 73 65 75 64 6f 54 61 62 20 3d   int pseudoTab =
33bfa 20 30 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   0;.  ExprList *
33bfb 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f  pOrderBy = p->pO
33bfc 72 64 65 72 42 79 3b 0a 0a 20 20 69 6e 74 20 65  rderBy;..  int e
33bfd 44 65 73 74 20 3d 20 70 44 65 73 74 2d 3e 65 44  Dest = pDest->eD
33bfe 65 73 74 3b 0a 20 20 69 6e 74 20 69 50 61 72 6d  est;.  int iParm
33bff 20 3d 20 70 44 65 73 74 2d 3e 69 53 44 50 61 72   = pDest->iSDPar
33c00 6d 3b 0a 0a 20 20 69 6e 74 20 72 65 67 52 6f 77  m;..  int regRow
33c01 3b 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64  ;.  int regRowid
33c02 3b 0a 0a 20 20 69 54 61 62 20 3d 20 70 4f 72 64  ;..  iTab = pOrd
33c03 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 3b 0a  erBy->iECursor;.
33c04 20 20 72 65 67 52 6f 77 20 3d 20 73 71 6c 69 74    regRow = sqlit
33c05 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
33c06 72 73 65 29 3b 0a 20 20 69 66 28 20 65 44 65 73  rse);.  if( eDes
33c07 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c  t==SRT_Output ||
33c08 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f   eDest==SRT_Coro
33c09 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 70 73 65  utine ){.    pse
33c0a 75 64 6f 54 61 62 20 3d 20 70 50 61 72 73 65 2d  udoTab = pParse-
33c0b 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 73 71 6c  >nTab++;.    sql
33c0c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
33c0d 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c  , OP_OpenPseudo,
33c0e 20 70 73 65 75 64 6f 54 61 62 2c 20 72 65 67 52   pseudoTab, regR
33c0f 6f 77 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  ow, nColumn);.  
33c10 20 20 72 65 67 52 6f 77 69 64 20 3d 20 30 3b 0a    regRowid = 0;.
33c11 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 67    }else{.    reg
33c12 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47  Rowid = sqlite3G
33c13 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
33c14 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  );.  }.  if( p->
33c15 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 55 73  selFlags & SF_Us
33c16 65 53 6f 72 74 65 72 20 29 7b 0a 20 20 20 20 69  eSorter ){.    i
33c17 6e 74 20 72 65 67 53 6f 72 74 4f 75 74 20 3d 20  nt regSortOut = 
33c18 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
33c19 20 20 20 20 69 6e 74 20 70 74 61 62 32 20 3d 20      int ptab2 = 
33c1a 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
33c1b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
33c1c 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp3(v, OP_Open
33c1d 50 73 65 75 64 6f 2c 20 70 74 61 62 32 2c 20 72  Pseudo, ptab2, r
33c1e 65 67 53 6f 72 74 4f 75 74 2c 20 70 4f 72 64 65  egSortOut, pOrde
33c1f 72 42 79 2d 3e 6e 45 78 70 72 2b 32 29 3b 0a 20  rBy->nExpr+2);. 
33c20 20 20 20 61 64 64 72 20 3d 20 31 20 2b 20 73 71     addr = 1 + sq
33c21 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
33c22 76 2c 20 4f 50 5f 53 6f 72 74 65 72 53 6f 72 74  v, OP_SorterSort
33c23 2c 20 69 54 61 62 2c 20 61 64 64 72 42 72 65 61  , iTab, addrBrea
33c24 6b 29 3b 0a 20 20 20 20 63 6f 64 65 4f 66 66 73  k);.    codeOffs
33c25 65 74 28 76 2c 20 70 2c 20 61 64 64 72 43 6f 6e  et(v, p, addrCon
33c26 74 69 6e 75 65 29 3b 0a 20 20 20 20 73 71 6c 69  tinue);.    sqli
33c27 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
33c28 20 4f 50 5f 53 6f 72 74 65 72 44 61 74 61 2c 20   OP_SorterData, 
33c29 69 54 61 62 2c 20 72 65 67 53 6f 72 74 4f 75 74  iTab, regSortOut
33c2a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
33c2b 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
33c2c 6f 6c 75 6d 6e 2c 20 70 74 61 62 32 2c 20 70 4f  olumn, ptab2, pO
33c2d 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 2b 31 2c  rderBy->nExpr+1,
33c2e 20 72 65 67 52 6f 77 29 3b 0a 20 20 20 20 73 71   regRow);.    sq
33c2f 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
33c30 35 28 76 2c 20 4f 50 46 4c 41 47 5f 43 4c 45 41  5(v, OPFLAG_CLEA
33c31 52 43 41 43 48 45 29 3b 0a 20 20 7d 65 6c 73 65  RCACHE);.  }else
33c32 7b 0a 20 20 20 20 61 64 64 72 20 3d 20 31 20 2b  {.    addr = 1 +
33c33 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
33c34 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74 2c 20 69  p2(v, OP_Sort, i
33c35 54 61 62 2c 20 61 64 64 72 42 72 65 61 6b 29 3b  Tab, addrBreak);
33c36 0a 20 20 20 20 63 6f 64 65 4f 66 66 73 65 74 28  .    codeOffset(
33c37 76 2c 20 70 2c 20 61 64 64 72 43 6f 6e 74 69 6e  v, p, addrContin
33c38 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ue);.    sqlite3
33c39 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
33c3a 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61 62 2c 20 70  _Column, iTab, p
33c3b 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 2b 31  OrderBy->nExpr+1
33c3c 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20 7d 0a 20  , regRow);.  }. 
33c3d 20 73 77 69 74 63 68 28 20 65 44 65 73 74 20 29   switch( eDest )
33c3e 7b 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 54  {.    case SRT_T
33c3f 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20 53  able:.    case S
33c40 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a 20  RT_EphemTab: {. 
33c41 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65       testcase( e
33c42 44 65 73 74 3d 3d 53 52 54 5f 54 61 62 6c 65 20  Dest==SRT_Table 
33c43 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
33c44 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 45 70  e( eDest==SRT_Ep
33c45 68 65 6d 54 61 62 20 29 3b 0a 20 20 20 20 20 20  hemTab );.      
33c46 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
33c47 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64  2(v, OP_NewRowid
33c48 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 69  , iParm, regRowi
33c49 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  d);.      sqlite
33c4a 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
33c4b 50 5f 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c  P_Insert, iParm,
33c4c 20 72 65 67 52 6f 77 2c 20 72 65 67 52 6f 77 69   regRow, regRowi
33c4d 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  d);.      sqlite
33c4e 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
33c4f 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b   OPFLAG_APPEND);
33c50 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
33c51 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
33c52 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
33c53 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53 65  .    case SRT_Se
33c54 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  t: {.      asser
33c55 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b  t( nColumn==1 );
33c56 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
33c57 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d  beAddOp4(v, OP_M
33c58 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52 6f  akeRecord, regRo
33c59 77 2c 20 31 2c 20 72 65 67 52 6f 77 69 64 2c 0a  w, 1, regRowid,.
33c5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33c5b 20 20 20 20 20 20 20 20 26 70 44 65 73 74 2d 3e          &pDest->
33c5c 61 66 66 53 64 73 74 2c 20 31 29 3b 0a 20 20 20  affSdst, 1);.   
33c5d 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
33c5e 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
33c5f 65 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77  e(pParse, regRow
33c60 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
33c61 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
33c62 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69   OP_IdxInsert, i
33c63 50 61 72 6d 2c 20 72 65 67 52 6f 77 69 64 29 3b  Parm, regRowid);
33c64 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
33c65 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 52 54    }.    case SRT
33c66 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73  _Mem: {.      as
33c67 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31  sert( nColumn==1
33c68 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
33c69 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50  3ExprCodeMove(pP
33c6a 61 72 73 65 2c 20 72 65 67 52 6f 77 2c 20 69 50  arse, regRow, iP
33c6b 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20 20 20 2f  arm, 1);.      /
33c6c 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75  * The LIMIT clau
33c6d 73 65 20 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74  se will terminat
33c6e 65 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75  e the loop for u
33c6f 73 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b  s */.      break
33c70 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
33c71 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
33c72 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
33c73 20 61 73 73 65 72 74 28 20 65 44 65 73 74 3d 3d   assert( eDest==
33c74 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c 20 65 44  SRT_Output || eD
33c75 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69  est==SRT_Corouti
33c76 6e 65 20 29 3b 20 0a 20 20 20 20 20 20 74 65 73  ne ); .      tes
33c77 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53 52  tcase( eDest==SR
33c78 54 5f 4f 75 74 70 75 74 20 29 3b 0a 20 20 20 20  T_Output );.    
33c79 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65 73    testcase( eDes
33c7a 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  t==SRT_Coroutine
33c7b 20 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d   );.      for(i=
33c7c 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  0; i<nColumn; i+
33c7d 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  +){.        asse
33c7e 72 74 28 20 72 65 67 52 6f 77 21 3d 70 44 65 73  rt( regRow!=pDes
33c7f 74 2d 3e 69 53 64 73 74 2b 69 20 29 3b 0a 20 20  t->iSdst+i );.  
33c80 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
33c81 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
33c82 6c 75 6d 6e 2c 20 70 73 65 75 64 6f 54 61 62 2c  lumn, pseudoTab,
33c83 20 69 2c 20 70 44 65 73 74 2d 3e 69 53 64 73 74   i, pDest->iSdst
33c84 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  +i);.        if(
33c85 20 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20   i==0 ){.       
33c86 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
33c87 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
33c88 5f 43 4c 45 41 52 43 41 43 48 45 29 3b 0a 20 20  _CLEARCACHE);.  
33c89 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
33c8a 20 20 20 20 20 20 69 66 28 20 65 44 65 73 74 3d        if( eDest=
33c8b 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20  =SRT_Output ){. 
33c8c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
33c8d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
33c8e 65 73 75 6c 74 52 6f 77 2c 20 70 44 65 73 74 2d  esultRow, pDest-
33c8f 3e 69 53 64 73 74 2c 20 6e 43 6f 6c 75 6d 6e 29  >iSdst, nColumn)
33c90 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
33c91 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
33c92 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
33c93 20 70 44 65 73 74 2d 3e 69 53 64 73 74 2c 20 6e   pDest->iSdst, n
33c94 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 7d  Column);.      }
33c95 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
33c96 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
33c97 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65  v, OP_Yield, pDe
33c98 73 74 2d 3e 69 53 44 50 61 72 6d 29 3b 0a 20 20  st->iSDParm);.  
33c99 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
33c9a 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  k;.    }.  }.  s
33c9b 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
33c9c 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
33c9d 52 6f 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 52  Row);.  sqlite3R
33c9e 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
33c9f 61 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29 3b  arse, regRowid);
33ca0 0a 0a 20 20 2f 2a 20 54 68 65 20 62 6f 74 74 6f  ..  /* The botto
33ca1 6d 20 6f 66 20 74 68 65 20 6c 6f 6f 70 0a 20 20  m of the loop.  
33ca2 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
33ca3 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
33ca4 61 64 64 72 43 6f 6e 74 69 6e 75 65 29 3b 0a 20  addrContinue);. 
33ca5 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73   if( p->selFlags
33ca6 20 26 20 53 46 5f 55 73 65 53 6f 72 74 65 72 20   & SF_UseSorter 
33ca7 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
33ca8 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
33ca9 6f 72 74 65 72 4e 65 78 74 2c 20 69 54 61 62 2c  orterNext, iTab,
33caa 20 61 64 64 72 29 3b 0a 20 20 7d 65 6c 73 65 7b   addr);.  }else{
33cab 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
33cac 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
33cad 74 2c 20 69 54 61 62 2c 20 61 64 64 72 29 3b 0a  t, iTab, addr);.
33cae 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
33caf 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
33cb0 20 61 64 64 72 42 72 65 61 6b 29 3b 0a 20 20 69   addrBreak);.  i
33cb1 66 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75  f( eDest==SRT_Ou
33cb2 74 70 75 74 20 7c 7c 20 65 44 65 73 74 3d 3d 53  tput || eDest==S
33cb3 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 7b 0a  RT_Coroutine ){.
33cb4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
33cb5 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
33cb6 65 2c 20 70 73 65 75 64 6f 54 61 62 2c 20 30 29  e, pseudoTab, 0)
33cb7 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
33cb8 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
33cb9 74 6f 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74  to a string cont
33cba 61 69 6e 69 6e 67 20 74 68 65 20 27 64 65 63 6c  aining the 'decl
33cbb 61 72 61 74 69 6f 6e 20 74 79 70 65 27 20 6f 66  aration type' of
33cbc 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69   the.** expressi
33cbd 6f 6e 20 70 45 78 70 72 2e 20 54 68 65 20 73 74  on pExpr. The st
33cbe 72 69 6e 67 20 6d 61 79 20 62 65 20 74 72 65 61  ring may be trea
33cbf 74 65 64 20 61 73 20 73 74 61 74 69 63 20 62 79  ted as static by
33cc0 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a   the caller..**.
33cc1 2a 2a 20 54 68 65 20 64 65 63 6c 61 72 61 74 69  ** The declarati
33cc2 6f 6e 20 74 79 70 65 20 69 73 20 74 68 65 20 65  on type is the e
33cc3 78 61 63 74 20 64 61 74 61 74 79 70 65 20 64 65  xact datatype de
33cc4 66 69 6e 69 74 69 6f 6e 20 65 78 74 72 61 63 74  finition extract
33cc5 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 6f  ed from the.** o
33cc6 72 69 67 69 6e 61 6c 20 43 52 45 41 54 45 20 54  riginal CREATE T
33cc7 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69  ABLE statement i
33cc8 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
33cc9 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 54 68   is a column. Th
33cca 65 0a 2a 2a 20 64 65 63 6c 61 72 61 74 69 6f 6e  e.** declaration
33ccb 20 74 79 70 65 20 66 6f 72 20 61 20 52 4f 57 49   type for a ROWI
33ccc 44 20 66 69 65 6c 64 20 69 73 20 49 4e 54 45 47  D field is INTEG
33ccd 45 52 2e 20 45 78 61 63 74 6c 79 20 77 68 65 6e  ER. Exactly when
33cce 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a   an expression.*
33ccf 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  * is considered 
33cd0 61 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20  a column can be 
33cd1 63 6f 6d 70 6c 65 78 20 69 6e 20 74 68 65 20 70  complex in the p
33cd2 72 65 73 65 6e 63 65 20 6f 66 20 73 75 62 71 75  resence of subqu
33cd3 65 72 69 65 73 2e 20 54 68 65 0a 2a 2a 20 72 65  eries. The.** re
33cd4 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73  sult-set express
33cd5 69 6f 6e 20 69 6e 20 61 6c 6c 20 6f 66 20 74 68  ion in all of th
33cd6 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 45 4c 45  e following SELE
33cd7 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73  CT statements is
33cd8 20 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 20   .** considered 
33cd9 61 20 63 6f 6c 75 6d 6e 20 62 79 20 74 68 69 73  a column by this
33cda 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
33cdb 20 20 20 53 45 4c 45 43 54 20 63 6f 6c 20 46 52     SELECT col FR
33cdc 4f 4d 20 74 62 6c 3b 0a 2a 2a 20 20 20 53 45 4c  OM tbl;.**   SEL
33cdd 45 43 54 20 28 53 45 4c 45 43 54 20 63 6f 6c 20  ECT (SELECT col 
33cde 46 52 4f 4d 20 74 62 6c 3b 0a 2a 2a 20 20 20 53  FROM tbl;.**   S
33cdf 45 4c 45 43 54 20 28 53 45 4c 45 43 54 20 63 6f  ELECT (SELECT co
33ce0 6c 20 46 52 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20  l FROM tbl);.** 
33ce1 20 20 53 45 4c 45 43 54 20 61 62 63 20 46 52 4f    SELECT abc FRO
33ce2 4d 20 28 53 45 4c 45 43 54 20 63 6f 6c 20 41 53  M (SELECT col AS
33ce3 20 61 62 63 20 46 52 4f 4d 20 74 62 6c 29 3b 0a   abc FROM tbl);.
33ce4 2a 2a 20 0a 2a 2a 20 54 68 65 20 64 65 63 6c 61  ** .** The decla
33ce5 72 61 74 69 6f 6e 20 74 79 70 65 20 66 6f 72 20  ration type for 
33ce6 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 6f  any expression o
33ce7 74 68 65 72 20 74 68 61 6e 20 61 20 63 6f 6c 75  ther than a colu
33ce8 6d 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73  mn is NULL..*/.s
33ce9 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
33cea 20 2a 63 6f 6c 75 6d 6e 54 79 70 65 28 0a 20 20   *columnType(.  
33ceb 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43  NameContext *pNC
33cec 2c 20 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  , .  Expr *pExpr
33ced 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
33cee 2a 70 7a 4f 72 69 67 69 6e 44 62 2c 0a 20 20 63  *pzOriginDb,.  c
33cef 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72  onst char **pzOr
33cf0 69 67 69 6e 54 61 62 2c 0a 20 20 63 6f 6e 73 74  iginTab,.  const
33cf1 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e   char **pzOrigin
33cf2 43 6f 6c 0a 29 7b 0a 20 20 63 68 61 72 20 63 6f  Col.){.  char co
33cf3 6e 73 74 20 2a 7a 54 79 70 65 20 3d 20 30 3b 0a  nst *zType = 0;.
33cf4 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4f    char const *zO
33cf5 72 69 67 69 6e 44 62 20 3d 20 30 3b 0a 20 20 63  riginDb = 0;.  c
33cf6 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4f 72 69 67  har const *zOrig
33cf7 69 6e 54 61 62 20 3d 20 30 3b 0a 20 20 63 68 61  inTab = 0;.  cha
33cf8 72 20 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e  r const *zOrigin
33cf9 43 6f 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6a  Col = 0;.  int j
33cfa 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 45  ;.  if( NEVER(pE
33cfb 78 70 72 3d 3d 30 29 20 7c 7c 20 70 4e 43 2d 3e  xpr==0) || pNC->
33cfc 70 53 72 63 4c 69 73 74 3d 3d 30 20 29 20 72 65  pSrcList==0 ) re
33cfd 74 75 72 6e 20 30 3b 0a 0a 20 20 73 77 69 74 63  turn 0;..  switc
33cfe 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a  h( pExpr->op ){.
33cff 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f      case TK_AGG_
33d00 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65  COLUMN:.    case
33d01 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20   TK_COLUMN: {.  
33d02 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65      /* The expre
33d03 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d  ssion is a colum
33d04 6e 2e 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61  n. Locate the ta
33d05 62 6c 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  ble the column i
33d06 73 20 62 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a  s being.      **
33d07 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20   extracted from 
33d08 69 6e 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70  in NameContext.p
33d09 53 72 63 4c 69 73 74 2e 20 54 68 69 73 20 74 61  SrcList. This ta
33d0a 62 6c 65 20 6d 61 79 20 62 65 20 72 65 61 6c 0a  ble may be real.
33d0b 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73        ** databas
33d0c 65 20 74 61 62 6c 65 20 6f 72 20 61 20 73 75 62  e table or a sub
33d0d 71 75 65 72 79 2e 0a 20 20 20 20 20 20 2a 2f 0a  query..      */.
33d0e 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
33d0f 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  b = 0;          
33d10 20 20 2f 2a 20 54 61 62 6c 65 20 73 74 72 75 63    /* Table struc
33d11 74 75 72 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65  ture column is e
33d12 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f  xtracted from */
33d13 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70  .      Select *p
33d14 53 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  S = 0;          
33d15 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 74 68 65     /* Select the
33d16 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78 74 72 61   column is extra
33d17 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 20  cted from */.   
33d18 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 45     int iCol = pE
33d19 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20 2f  xpr->iColumn;  /
33d1a 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d  * Index of colum
33d1b 6e 20 69 6e 20 70 54 61 62 20 2a 2f 0a 20 20 20  n in pTab */.   
33d1c 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78     testcase( pEx
33d1d 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43  pr->op==TK_AGG_C
33d1e 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74  OLUMN );.      t
33d1f 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e  estcase( pExpr->
33d20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b  op==TK_COLUMN );
33d21 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 4e  .      while( pN
33d22 43 20 26 26 20 21 70 54 61 62 20 29 7b 0a 20 20  C && !pTab ){.  
33d23 20 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70        SrcList *p
33d24 54 61 62 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70  TabList = pNC->p
33d25 53 72 63 4c 69 73 74 3b 0a 20 20 20 20 20 20 20  SrcList;.       
33d26 20 66 6f 72 28 6a 3d 30 3b 6a 3c 70 54 61 62 4c   for(j=0;j<pTabL
33d27 69 73 74 2d 3e 6e 53 72 63 20 26 26 20 70 54 61  ist->nSrc && pTa
33d28 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72  bList->a[j].iCur
33d29 73 6f 72 21 3d 70 45 78 70 72 2d 3e 69 54 61 62  sor!=pExpr->iTab
33d2a 6c 65 3b 6a 2b 2b 29 3b 0a 20 20 20 20 20 20 20  le;j++);.       
33d2b 20 69 66 28 20 6a 3c 70 54 61 62 4c 69 73 74 2d   if( j<pTabList-
33d2c 3e 6e 53 72 63 20 29 7b 0a 20 20 20 20 20 20 20  >nSrc ){.       
33d2d 20 20 20 70 54 61 62 20 3d 20 70 54 61 62 4c 69     pTab = pTabLi
33d2e 73 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a 20  st->a[j].pTab;. 
33d2f 20 20 20 20 20 20 20 20 20 70 53 20 3d 20 70 54           pS = pT
33d30 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 53 65  abList->a[j].pSe
33d31 6c 65 63 74 3b 0a 20 20 20 20 20 20 20 20 7d 65  lect;.        }e
33d32 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70  lse{.          p
33d33 4e 43 20 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b  NC = pNC->pNext;
33d34 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
33d35 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 70 54   }..      if( pT
33d36 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ab==0 ){.       
33d37 20 2f 2a 20 41 74 20 6f 6e 65 20 74 69 6d 65 2c   /* At one time,
33d38 20 63 6f 64 65 20 73 75 63 68 20 61 73 20 22 53   code such as "S
33d39 45 4c 45 43 54 20 6e 65 77 2e 78 22 20 77 69 74  ELECT new.x" wit
33d3a 68 69 6e 20 61 20 74 72 69 67 67 65 72 20 77 6f  hin a trigger wo
33d3b 75 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 63  uld.        ** c
33d3c 61 75 73 65 20 74 68 69 73 20 63 6f 6e 64 69 74  ause this condit
33d3d 69 6f 6e 20 74 6f 20 72 75 6e 2e 20 20 53 69 6e  ion to run.  Sin
33d3e 63 65 20 74 68 65 6e 2c 20 77 65 20 68 61 76 65  ce then, we have
33d3f 20 72 65 73 74 72 75 63 74 75 72 65 64 20 68 6f   restructured ho
33d40 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 69  w.        ** tri
33d41 67 67 65 72 20 63 6f 64 65 20 69 73 20 67 65 6e  gger code is gen
33d42 65 72 61 74 65 64 20 61 6e 64 20 73 6f 20 74 68  erated and so th
33d43 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20  is condition is 
33d44 6e 6f 20 6c 6f 6e 67 65 72 20 0a 20 20 20 20 20  no longer .     
33d45 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c 65 2e 20     ** possible. 
33d46 48 6f 77 65 76 65 72 2c 20 69 74 20 63 61 6e 20  However, it can 
33d47 73 74 69 6c 6c 20 62 65 20 74 72 75 65 20 66 6f  still be true fo
33d48 72 20 73 74 61 74 65 6d 65 6e 74 73 20 6c 69 6b  r statements lik
33d49 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  e.        ** the
33d4a 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 20 20   following:.    
33d4b 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
33d4c 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45  *   CREATE TABLE
33d4d 20 74 31 28 63 6f 6c 20 49 4e 54 45 47 45 52 29   t1(col INTEGER)
33d4e 3b 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 53  ;.        **   S
33d4f 45 4c 45 43 54 20 28 53 45 4c 45 43 54 20 74 31  ELECT (SELECT t1
33d50 2e 63 6f 6c 29 20 46 52 4f 4d 20 46 52 4f 4d 20  .col) FROM FROM 
33d51 74 31 3b 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  t1;.        **. 
33d52 20 20 20 20 20 20 20 2a 2a 20 77 68 65 6e 20 63         ** when c
33d53 6f 6c 75 6d 6e 54 79 70 65 28 29 20 69 73 20 63  olumnType() is c
33d54 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 65 78 70  alled on the exp
33d55 72 65 73 73 69 6f 6e 20 22 74 31 2e 63 6f 6c 22  ression "t1.col"
33d56 20 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 20   in the .       
33d57 20 2a 2a 20 73 75 62 2d 73 65 6c 65 63 74 2e 20   ** sub-select. 
33d58 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 73 65  In this case, se
33d59 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 79 70  t the column typ
33d5a 65 20 74 6f 20 4e 55 4c 4c 2c 20 65 76 65 6e 0a  e to NULL, even.
33d5b 20 20 20 20 20 20 20 20 2a 2a 20 74 68 6f 75 67          ** thoug
33d5c 68 20 69 74 20 73 68 6f 75 6c 64 20 72 65 61 6c  h it should real
33d5d 6c 79 20 62 65 20 22 49 4e 54 45 47 45 52 22 2e  ly be "INTEGER".
33d5e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
33d5f 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 6e      ** This is n
33d60 6f 74 20 61 20 70 72 6f 62 6c 65 6d 2c 20 61 73  ot a problem, as
33d61 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 79 70 65   the column type
33d62 20 6f 66 20 22 74 31 2e 63 6f 6c 22 20 69 73 20   of "t1.col" is 
33d63 6e 65 76 65 72 0a 20 20 20 20 20 20 20 20 2a 2a  never.        **
33d64 20 75 73 65 64 2e 20 57 68 65 6e 20 63 6f 6c 75   used. When colu
33d65 6d 6e 54 79 70 65 28 29 20 69 73 20 63 61 6c 6c  mnType() is call
33d66 65 64 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73  ed on the expres
33d67 73 69 6f 6e 20 0a 20 20 20 20 20 20 20 20 2a 2a  sion .        **
33d68 20 22 28 53 45 4c 45 43 54 20 74 31 2e 63 6f 6c   "(SELECT t1.col
33d69 29 22 2c 20 74 68 65 20 63 6f 72 72 65 63 74 20  )", the correct 
33d6a 74 79 70 65 20 69 73 20 72 65 74 75 72 6e 65 64  type is returned
33d6b 20 28 73 65 65 20 74 68 65 20 54 4b 5f 53 45 4c   (see the TK_SEL
33d6c 45 43 54 0a 20 20 20 20 20 20 20 20 2a 2a 20 62  ECT.        ** b
33d6d 72 61 6e 63 68 20 62 65 6c 6f 77 2e 20 20 2a 2f  ranch below.  */
33d6e 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
33d6f 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61        }..      a
33d70 73 73 65 72 74 28 20 70 54 61 62 20 26 26 20 70  ssert( pTab && p
33d71 45 78 70 72 2d 3e 70 54 61 62 3d 3d 70 54 61 62  Expr->pTab==pTab
33d72 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 53   );.      if( pS
33d73 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   ){.        /* T
33d74 68 65 20 22 74 61 62 6c 65 22 20 69 73 20 61 63  he "table" is ac
33d75 74 75 61 6c 6c 79 20 61 20 73 75 62 2d 73 65 6c  tually a sub-sel
33d76 65 63 74 20 6f 72 20 61 20 76 69 65 77 20 69 6e  ect or a view in
33d77 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
33d78 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74  .        ** of t
33d79 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
33d7a 65 6e 74 2e 20 52 65 74 75 72 6e 20 74 68 65 20  ent. Return the 
33d7b 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
33d7c 20 61 6e 64 20 6f 72 69 67 69 6e 0a 20 20 20 20   and origin.    
33d7d 20 20 20 20 2a 2a 20 64 61 74 61 20 66 6f 72 20      ** data for 
33d7e 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 63  the result-set c
33d7f 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 73 75 62  olumn of the sub
33d80 2d 73 65 6c 65 63 74 2e 0a 20 20 20 20 20 20 20  -select..       
33d81 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
33d82 69 43 6f 6c 3e 3d 30 20 26 26 20 41 4c 57 41 59  iCol>=0 && ALWAY
33d83 53 28 69 43 6f 6c 3c 70 53 2d 3e 70 45 4c 69 73  S(iCol<pS->pELis
33d84 74 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20 20 20  t->nExpr) ){.   
33d85 20 20 20 20 20 20 20 2f 2a 20 49 66 20 69 43 6f         /* If iCo
33d86 6c 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a  l is less than z
33d87 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 65 78  ero, then the ex
33d88 70 72 65 73 73 69 6f 6e 20 72 65 71 75 65 73 74  pression request
33d89 73 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  s the.          
33d8a 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  ** rowid of the 
33d8b 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 76 69  sub-select or vi
33d8c 65 77 2e 20 54 68 69 73 20 65 78 70 72 65 73 73  ew. This express
33d8d 69 6f 6e 20 69 73 20 6c 65 67 61 6c 20 28 73 65  ion is legal (se
33d8e 65 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  e .          ** 
33d8f 74 65 73 74 20 63 61 73 65 20 6d 69 73 63 32 2e  test case misc2.
33d90 32 2e 32 29 20 2d 20 69 74 20 61 6c 77 61 79 73  2.2) - it always
33d91 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55   evaluates to NU
33d92 4c 4c 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  LL..          */
33d93 0a 20 20 20 20 20 20 20 20 20 20 4e 61 6d 65 43  .          NameC
33d94 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20 20  ontext sNC;.    
33d95 20 20 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20        Expr *p = 
33d96 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 69 43  pS->pEList->a[iC
33d97 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  ol].pExpr;.     
33d98 20 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 73       sNC.pSrcLis
33d99 74 20 3d 20 70 53 2d 3e 70 53 72 63 3b 0a 20 20  t = pS->pSrc;.  
33d9a 20 20 20 20 20 20 20 20 73 4e 43 2e 70 4e 65 78          sNC.pNex
33d9b 74 20 3d 20 70 4e 43 3b 0a 20 20 20 20 20 20 20  t = pNC;.       
33d9c 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20     sNC.pParse = 
33d9d 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20  pNC->pParse;.   
33d9e 20 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 63         zType = c
33d9f 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20  olumnType(&sNC, 
33da0 70 2c 20 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26  p, &zOriginDb, &
33da1 7a 4f 72 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72  zOriginTab, &zOr
33da2 69 67 69 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20  iginCol); .     
33da3 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
33da4 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 61 62   if( ALWAYS(pTab
33da5 2d 3e 70 53 63 68 65 6d 61 29 20 29 7b 0a 20 20  ->pSchema) ){.  
33da6 20 20 20 20 20 20 2f 2a 20 41 20 72 65 61 6c 20        /* A real 
33da7 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20  table */.       
33da8 20 61 73 73 65 72 74 28 20 21 70 53 20 29 3b 0a   assert( !pS );.
33da9 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c          if( iCol
33daa 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62  <0 ) iCol = pTab
33dab 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 20  ->iPKey;.       
33dac 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3d 3d 2d   assert( iCol==-
33dad 31 20 7c 7c 20 28 69 43 6f 6c 3e 3d 30 20 26 26  1 || (iCol>=0 &&
33dae 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c   iCol<pTab->nCol
33daf 29 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ) );.        if(
33db0 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20   iCol<0 ){.     
33db1 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 49 4e       zType = "IN
33db2 54 45 47 45 52 22 3b 0a 20 20 20 20 20 20 20 20  TEGER";.        
33db3 20 20 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 22    zOriginCol = "
33db4 72 6f 77 69 64 22 3b 0a 20 20 20 20 20 20 20 20  rowid";.        
33db5 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
33db6 20 7a 54 79 70 65 20 3d 20 70 54 61 62 2d 3e 61   zType = pTab->a
33db7 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b  Col[iCol].zType;
33db8 0a 20 20 20 20 20 20 20 20 20 20 7a 4f 72 69 67  .          zOrig
33db9 69 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43  inCol = pTab->aC
33dba 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a  ol[iCol].zName;.
33dbb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
33dbc 20 20 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20 70    zOriginTab = p
33dbd 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  Tab->zName;.    
33dbe 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 70 50 61      if( pNC->pPa
33dbf 72 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  rse ){.         
33dc0 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74   int iDb = sqlit
33dc1 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
33dc2 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2c  pNC->pParse->db,
33dc3 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
33dc4 0a 20 20 20 20 20 20 20 20 20 20 7a 4f 72 69 67  .          zOrig
33dc5 69 6e 44 62 20 3d 20 70 4e 43 2d 3e 70 50 61 72  inDb = pNC->pPar
33dc6 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d  se->db->aDb[iDb]
33dc7 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  .zName;.        
33dc8 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
33dc9 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66  break;.    }.#if
33dca 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
33dcb 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61  _SUBQUERY.    ca
33dcc 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a  se TK_SELECT: {.
33dcd 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
33dce 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73 75 62  ression is a sub
33dcf 2d 73 65 6c 65 63 74 2e 20 52 65 74 75 72 6e 20  -select. Return 
33dd0 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  the declaration 
33dd1 74 79 70 65 20 61 6e 64 0a 20 20 20 20 20 20 2a  type and.      *
33dd2 2a 20 6f 72 69 67 69 6e 20 69 6e 66 6f 20 66 6f  * origin info fo
33dd3 72 20 74 68 65 20 73 69 6e 67 6c 65 20 63 6f 6c  r the single col
33dd4 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c  umn in the resul
33dd5 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c  t set of the SEL
33dd6 45 43 54 0a 20 20 20 20 20 20 2a 2a 20 73 74 61  ECT.      ** sta
33dd7 74 65 6d 65 6e 74 2e 0a 20 20 20 20 20 20 2a 2f  tement..      */
33dd8 0a 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65  .      NameConte
33dd9 78 74 20 73 4e 43 3b 0a 20 20 20 20 20 20 53 65  xt sNC;.      Se
33dda 6c 65 63 74 20 2a 70 53 20 3d 20 70 45 78 70 72  lect *pS = pExpr
33ddb 2d 3e 78 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20  ->x.pSelect;.   
33ddc 20 20 20 45 78 70 72 20 2a 70 20 3d 20 70 53 2d     Expr *p = pS-
33ddd 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
33dde 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72  xpr;.      asser
33ddf 74 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  t( ExprHasProper
33de0 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
33de1 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20  Select) );.     
33de2 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20   sNC.pSrcList = 
33de3 70 53 2d 3e 70 53 72 63 3b 0a 20 20 20 20 20 20  pS->pSrc;.      
33de4 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70 4e 43 3b  sNC.pNext = pNC;
33de5 0a 20 20 20 20 20 20 73 4e 43 2e 70 50 61 72 73  .      sNC.pPars
33de6 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b  e = pNC->pParse;
33de7 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 63  .      zType = c
33de8 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20  olumnType(&sNC, 
33de9 70 2c 20 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26  p, &zOriginDb, &
33dea 7a 4f 72 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72  zOriginTab, &zOr
33deb 69 67 69 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20  iginCol); .     
33dec 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
33ded 6e 64 69 66 0a 20 20 7d 0a 20 20 0a 20 20 69 66  ndif.  }.  .  if
33dee 28 20 70 7a 4f 72 69 67 69 6e 44 62 20 29 7b 0a  ( pzOriginDb ){.
33def 20 20 20 20 61 73 73 65 72 74 28 20 70 7a 4f 72      assert( pzOr
33df0 69 67 69 6e 54 61 62 20 26 26 20 70 7a 4f 72 69  iginTab && pzOri
33df1 67 69 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 2a 70  ginCol );.    *p
33df2 7a 4f 72 69 67 69 6e 44 62 20 3d 20 7a 4f 72 69  zOriginDb = zOri
33df3 67 69 6e 44 62 3b 0a 20 20 20 20 2a 70 7a 4f 72  ginDb;.    *pzOr
33df4 69 67 69 6e 54 61 62 20 3d 20 7a 4f 72 69 67 69  iginTab = zOrigi
33df5 6e 54 61 62 3b 0a 20 20 20 20 2a 70 7a 4f 72 69  nTab;.    *pzOri
33df6 67 69 6e 43 6f 6c 20 3d 20 7a 4f 72 69 67 69 6e  ginCol = zOrigin
33df7 43 6f 6c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  Col;.  }.  retur
33df8 6e 20 7a 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  n zType;.}../*.*
33df9 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
33dfa 74 68 61 74 20 77 69 6c 6c 20 74 65 6c 6c 20 74  that will tell t
33dfb 68 65 20 56 44 42 45 20 74 68 65 20 64 65 63 6c  he VDBE the decl
33dfc 61 72 61 74 69 6f 6e 20 74 79 70 65 73 20 6f 66  aration types of
33dfd 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74   columns.** in t
33dfe 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a  he result set..*
33dff 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 65  /.static void ge
33e00 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65  nerateColumnType
33e01 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  s(.  Parse *pPar
33e02 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73  se,      /* Pars
33e03 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
33e04 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
33e05 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74  t,  /* List of t
33e06 61 62 6c 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c  ables */.  ExprL
33e07 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f  ist *pEList    /
33e08 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 20 64 65  * Expressions de
33e09 66 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c  fining the resul
33e0a 74 20 73 65 74 20 2a 2f 0a 29 7b 0a 23 69 66 6e  t set */.){.#ifn
33e0b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
33e0c 44 45 43 4c 54 59 50 45 0a 20 20 56 64 62 65 20  DECLTYPE.  Vdbe 
33e0d 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
33e0e 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e  be;.  int i;.  N
33e0f 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a  ameContext sNC;.
33e10 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d    sNC.pSrcList =
33e11 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 73 4e 43   pTabList;.  sNC
33e12 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
33e13 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
33e14 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  EList->nExpr; i+
33e15 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 20  +){.    Expr *p 
33e16 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70  = pEList->a[i].p
33e17 45 78 70 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20  Expr;.    const 
33e18 63 68 61 72 20 2a 7a 54 79 70 65 3b 0a 23 69 66  char *zType;.#if
33e19 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
33e1a 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54  E_COLUMN_METADAT
33e1b 41 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  A.    const char
33e1c 20 2a 7a 4f 72 69 67 44 62 20 3d 20 30 3b 0a 20   *zOrigDb = 0;. 
33e1d 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
33e1e 4f 72 69 67 54 61 62 20 3d 20 30 3b 0a 20 20 20  OrigTab = 0;.   
33e1f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72   const char *zOr
33e20 69 67 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 7a  igCol = 0;.    z
33e21 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70  Type = columnTyp
33e22 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69  e(&sNC, p, &zOri
33e23 67 44 62 2c 20 26 7a 4f 72 69 67 54 61 62 2c 20  gDb, &zOrigTab, 
33e24 26 7a 4f 72 69 67 43 6f 6c 29 3b 0a 0a 20 20 20  &zOrigCol);..   
33e25 20 2f 2a 20 54 68 65 20 76 64 62 65 20 6d 75 73   /* The vdbe mus
33e26 74 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 63  t make its own c
33e27 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  opy of the colum
33e28 6e 2d 74 79 70 65 20 61 6e 64 20 6f 74 68 65 72  n-type and other
33e29 20 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20   .    ** column 
33e2a 73 70 65 63 69 66 69 63 20 73 74 72 69 6e 67 73  specific strings
33e2b 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20 73 63  , in case the sc
33e2c 68 65 6d 61 20 69 73 20 72 65 73 65 74 20 62 65  hema is reset be
33e2d 66 6f 72 65 20 74 68 69 73 0a 20 20 20 20 2a 2a  fore this.    **
33e2e 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
33e2f 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 20 20 20   is deleted..   
33e30 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
33e31 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
33e32 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41   i, COLNAME_DATA
33e33 42 41 53 45 2c 20 7a 4f 72 69 67 44 62 2c 20 53  BASE, zOrigDb, S
33e34 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
33e35 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
33e36 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69  eSetColName(v, i
33e37 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 2c  , COLNAME_TABLE,
33e38 20 7a 4f 72 69 67 54 61 62 2c 20 53 51 4c 49 54   zOrigTab, SQLIT
33e39 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
33e3a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
33e3b 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f  ColName(v, i, CO
33e3c 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 2c 20 7a 4f  LNAME_COLUMN, zO
33e3d 72 69 67 43 6f 6c 2c 20 53 51 4c 49 54 45 5f 54  rigCol, SQLITE_T
33e3e 52 41 4e 53 49 45 4e 54 29 3b 0a 23 65 6c 73 65  RANSIENT);.#else
33e3f 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c  .    zType = col
33e40 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c  umnType(&sNC, p,
33e41 20 30 2c 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69   0, 0, 0);.#endi
33e42 66 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  f.    sqlite3Vdb
33e43 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69  eSetColName(v, i
33e44 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59  , COLNAME_DECLTY
33e45 50 45 2c 20 7a 54 79 70 65 2c 20 53 51 4c 49 54  PE, zType, SQLIT
33e46 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
33e47 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
33e48 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45  TE_OMIT_DECLTYPE
33e49 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e   */.}../*.** Gen
33e4a 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
33e4b 77 69 6c 6c 20 74 65 6c 6c 20 74 68 65 20 56 44  will tell the VD
33e4c 42 45 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20  BE the names of 
33e4d 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68  columns.** in th
33e4e 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54  e result set.  T
33e4f 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
33e50 69 73 20 75 73 65 64 20 74 6f 20 70 72 6f 76 69  is used to provi
33e51 64 65 20 74 68 65 0a 2a 2a 20 61 7a 43 6f 6c 5b  de the.** azCol[
33e52 5d 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20  ] values in the 
33e53 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61  callback..*/.sta
33e54 74 69 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74  tic void generat
33e55 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 0a 20 20  eColumnNames(.  
33e56 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
33e57 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f      /* Parser co
33e58 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
33e59 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 2f  st *pTabList,  /
33e5a 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73  * List of tables
33e5b 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
33e5c 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 45 78 70  pEList    /* Exp
33e5d 72 65 73 73 69 6f 6e 73 20 64 65 66 69 6e 69 6e  ressions definin
33e5e 67 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  g the result set
33e5f 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76   */.){.  Vdbe *v
33e60 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
33e61 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20  ;.  int i, j;.  
33e62 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
33e63 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
33e64 66 75 6c 6c 4e 61 6d 65 73 2c 20 73 68 6f 72 74  fullNames, short
33e65 4e 61 6d 65 73 3b 0a 0a 23 69 66 6e 64 65 66 20  Names;..#ifndef 
33e66 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
33e67 41 49 4e 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  AIN.  /* If this
33e68 20 69 73 20 61 6e 20 45 58 50 4c 41 49 4e 2c 20   is an EXPLAIN, 
33e69 73 6b 69 70 20 74 68 69 73 20 73 74 65 70 20 2a  skip this step *
33e6a 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  /.  if( pParse->
33e6b 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72  explain ){.    r
33e6c 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69  eturn;.  }.#endi
33e6d 66 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  f..  if( pParse-
33e6e 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 7c 7c 20  >colNamesSet || 
33e6f 4e 45 56 45 52 28 76 3d 3d 30 29 20 7c 7c 20 64  NEVER(v==0) || d
33e70 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
33e71 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 50 61 72  ) return;.  pPar
33e72 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20  se->colNamesSet 
33e73 3d 20 31 3b 0a 20 20 66 75 6c 6c 4e 61 6d 65 73  = 1;.  fullNames
33e74 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20   = (db->flags & 
33e75 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61  SQLITE_FullColNa
33e76 6d 65 73 29 21 3d 30 3b 0a 20 20 73 68 6f 72 74  mes)!=0;.  short
33e77 4e 61 6d 65 73 20 3d 20 28 64 62 2d 3e 66 6c 61  Names = (db->fla
33e78 67 73 20 26 20 53 51 4c 49 54 45 5f 53 68 6f 72  gs & SQLITE_Shor
33e79 74 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b 0a 20  tColNames)!=0;. 
33e7a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
33e7b 75 6d 43 6f 6c 73 28 76 2c 20 70 45 4c 69 73 74  umCols(v, pEList
33e7c 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 66 6f 72 28  ->nExpr);.  for(
33e7d 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e  i=0; i<pEList->n
33e7e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
33e7f 45 78 70 72 20 2a 70 3b 0a 20 20 20 20 70 20 3d  Expr *p;.    p =
33e80 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45   pEList->a[i].pE
33e81 78 70 72 3b 0a 20 20 20 20 69 66 28 20 4e 45 56  xpr;.    if( NEV
33e82 45 52 28 70 3d 3d 30 29 20 29 20 63 6f 6e 74 69  ER(p==0) ) conti
33e83 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 45 4c  nue;.    if( pEL
33e84 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20  ist->a[i].zName 
33e85 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
33e86 4e 61 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61  Name = pEList->a
33e87 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  [i].zName;.     
33e88 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
33e89 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
33e8a 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65  NAME_NAME, zName
33e8b 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
33e8c 4e 54 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  NT);.    }else i
33e8d 66 28 20 28 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  f( (p->op==TK_CO
33e8e 4c 55 4d 4e 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 54  LUMN || p->op==T
33e8f 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 29 20 26 26  K_AGG_COLUMN) &&
33e90 20 70 54 61 62 4c 69 73 74 20 29 7b 0a 20 20 20   pTabList ){.   
33e91 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
33e92 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c        char *zCol
33e93 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  ;.      int iCol
33e94 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20   = p->iColumn;. 
33e95 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 41 4c       for(j=0; AL
33e96 57 41 59 53 28 6a 3c 70 54 61 62 4c 69 73 74 2d  WAYS(j<pTabList-
33e97 3e 6e 53 72 63 29 3b 20 6a 2b 2b 29 7b 0a 20 20  >nSrc); j++){.  
33e98 20 20 20 20 20 20 69 66 28 20 70 54 61 62 4c 69        if( pTabLi
33e99 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f 72  st->a[j].iCursor
33e9a 3d 3d 70 2d 3e 69 54 61 62 6c 65 20 29 20 62 72  ==p->iTable ) br
33e9b 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
33e9c 20 20 20 61 73 73 65 72 74 28 20 6a 3c 70 54 61     assert( j<pTa
33e9d 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 3b 0a 20  bList->nSrc );. 
33e9e 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62       pTab = pTab
33e9f 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b  List->a[j].pTab;
33ea0 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c  .      if( iCol<
33ea1 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d  0 ) iCol = pTab-
33ea2 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 61 73  >iPKey;.      as
33ea3 73 65 72 74 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c  sert( iCol==-1 |
33ea4 7c 20 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43  | (iCol>=0 && iC
33ea5 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29 20 29  ol<pTab->nCol) )
33ea6 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c  ;.      if( iCol
33ea7 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 43  <0 ){.        zC
33ea8 6f 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20  ol = "rowid";.  
33ea9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
33eaa 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e     zCol = pTab->
33eab 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65  aCol[iCol].zName
33eac 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
33ead 69 66 28 20 21 73 68 6f 72 74 4e 61 6d 65 73 20  if( !shortNames 
33eae 26 26 20 21 66 75 6c 6c 4e 61 6d 65 73 20 29 7b  && !fullNames ){
33eaf 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
33eb0 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
33eb1 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , i, COLNAME_NAM
33eb2 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  E, .            
33eb3 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
33eb4 64 62 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d  db, pEList->a[i]
33eb5 2e 7a 53 70 61 6e 29 2c 20 53 51 4c 49 54 45 5f  .zSpan), SQLITE_
33eb6 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20  DYNAMIC);.      
33eb7 7d 65 6c 73 65 20 69 66 28 20 66 75 6c 6c 4e 61  }else if( fullNa
33eb8 6d 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 63  mes ){.        c
33eb9 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 0a  har *zName = 0;.
33eba 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20          zName = 
33ebb 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
33ebc 62 2c 20 22 25 73 2e 25 73 22 2c 20 70 54 61 62  b, "%s.%s", pTab
33ebd 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3b 0a  ->zName, zCol);.
33ebe 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
33ebf 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
33ec0 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   i, COLNAME_NAME
33ec1 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f  , zName, SQLITE_
33ec2 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20  DYNAMIC);.      
33ec3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
33ec4 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
33ec5 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41  Name(v, i, COLNA
33ec6 4d 45 5f 4e 41 4d 45 2c 20 7a 43 6f 6c 2c 20 53  ME_NAME, zCol, S
33ec7 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
33ec8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
33ec9 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
33eca 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
33ecb 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, i, COLNAME_N
33ecc 41 4d 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20  AME, .          
33ecd 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
33ece 64 62 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d  db, pEList->a[i]
33ecf 2e 7a 53 70 61 6e 29 2c 20 53 51 4c 49 54 45 5f  .zSpan), SQLITE_
33ed0 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 0a  DYNAMIC);.    }.
33ed1 20 20 7d 0a 20 20 67 65 6e 65 72 61 74 65 43 6f    }.  generateCo
33ed2 6c 75 6d 6e 54 79 70 65 73 28 70 50 61 72 73 65  lumnTypes(pParse
33ed3 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 45 4c 69  , pTabList, pELi
33ed4 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69  st);.}../*.** Gi
33ed5 76 65 6e 20 61 20 61 6e 20 65 78 70 72 65 73 73  ven a an express
33ed6 69 6f 6e 20 6c 69 73 74 20 28 77 68 69 63 68 20  ion list (which 
33ed7 69 73 20 72 65 61 6c 6c 79 20 74 68 65 20 6c 69  is really the li
33ed8 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  st of expression
33ed9 73 0a 2a 2a 20 74 68 61 74 20 66 6f 72 6d 20 74  s.** that form t
33eda 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
33edb 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
33edc 65 6e 74 29 20 63 6f 6d 70 75 74 65 20 61 70 70  ent) compute app
33edd 72 6f 70 72 69 61 74 65 0a 2a 2a 20 63 6f 6c 75  ropriate.** colu
33ede 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 20 61 20 74  mn names for a t
33edf 61 62 6c 65 20 74 68 61 74 20 77 6f 75 6c 64 20  able that would 
33ee0 68 6f 6c 64 20 74 68 65 20 65 78 70 72 65 73 73  hold the express
33ee1 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ion list..**.** 
33ee2 41 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  All column names
33ee3 20 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 2e   will be unique.
33ee4 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20  .**.** Only the 
33ee5 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 72 65  column names are
33ee6 20 63 6f 6d 70 75 74 65 64 2e 20 20 43 6f 6c 75   computed.  Colu
33ee7 6d 6e 2e 7a 54 79 70 65 2c 20 43 6f 6c 75 6d 6e  mn.zType, Column
33ee8 2e 7a 43 6f 6c 6c 2c 0a 2a 2a 20 61 6e 64 20 6f  .zColl,.** and o
33ee9 74 68 65 72 20 66 69 65 6c 64 73 20 6f 66 20 43  ther fields of C
33eea 6f 6c 75 6d 6e 20 61 72 65 20 7a 65 72 6f 65 64  olumn are zeroed
33eeb 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
33eec 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
33eed 65 73 73 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72  ess.  If a memor
33eee 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
33eef 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 73 74  or occurs,.** st
33ef0 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 2a 70 61 43  ore NULL in *paC
33ef1 6f 6c 20 61 6e 64 20 30 20 69 6e 20 2a 70 6e 43  ol and 0 in *pnC
33ef2 6f 6c 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51  ol and return SQ
33ef3 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 73  LITE_NOMEM..*/.s
33ef4 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74  tatic int select
33ef5 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c  ColumnsFromExprL
33ef6 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ist(.  Parse *pP
33ef7 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
33ef8 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
33ef9 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
33efa 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20 2f  *pEList,       /
33efb 2a 20 45 78 70 72 20 6c 69 73 74 20 66 72 6f 6d  * Expr list from
33efc 20 77 68 69 63 68 20 74 6f 20 64 65 72 69 76 65   which to derive
33efd 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f   column names */
33efe 0a 20 20 69 31 36 20 2a 70 6e 43 6f 6c 2c 20 20  .  i16 *pnCol,  
33eff 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
33f00 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ite the number o
33f01 66 20 63 6f 6c 75 6d 6e 73 20 68 65 72 65 20 2a  f columns here *
33f02 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 2a 70 61 43  /.  Column **paC
33f03 6f 6c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  ol          /* W
33f04 72 69 74 65 20 74 68 65 20 6e 65 77 20 63 6f 6c  rite the new col
33f05 75 6d 6e 20 6c 69 73 74 20 68 65 72 65 20 2a 2f  umn list here */
33f06 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
33f07 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
33f08 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
33f09 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  nnection */.  in
33f0a 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  t i, j;         
33f0b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
33f0c 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  p counters */.  
33f0d 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20  int cnt;        
33f0e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
33f0f 6e 64 65 78 20 61 64 64 65 64 20 74 6f 20 6d 61  ndex added to ma
33f10 6b 65 20 74 68 65 20 6e 61 6d 65 20 75 6e 69 71  ke the name uniq
33f11 75 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a  ue */.  Column *
33f12 61 43 6f 6c 2c 20 2a 70 43 6f 6c 3b 20 20 20 20  aCol, *pCol;    
33f13 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
33f14 6e 67 20 6f 76 65 72 20 72 65 73 75 6c 74 20 63  ng over result c
33f15 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  olumns */.  int 
33f16 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  nCol;           
33f17 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
33f18 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
33f19 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
33f1a 2f 0a 20 20 45 78 70 72 20 2a 70 3b 20 20 20 20  /.  Expr *p;    
33f1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f1c 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 66 6f  /* Expression fo
33f1d 72 20 61 20 73 69 6e 67 6c 65 20 72 65 73 75 6c  r a single resul
33f1e 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68  t column */.  ch
33f1f 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
33f20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
33f21 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e  umn name */.  in
33f22 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20  t nName;        
33f23 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
33f24 65 20 6f 66 20 6e 61 6d 65 20 69 6e 20 7a 4e 61  e of name in zNa
33f25 6d 65 5b 5d 20 2a 2f 0a 0a 20 20 69 66 28 20 70  me[] */..  if( p
33f26 45 4c 69 73 74 20 29 7b 0a 20 20 20 20 6e 43 6f  EList ){.    nCo
33f27 6c 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  l = pEList->nExp
33f28 72 3b 0a 20 20 20 20 61 43 6f 6c 20 3d 20 73 71  r;.    aCol = sq
33f29 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
33f2a 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 61 43 6f  o(db, sizeof(aCo
33f2b 6c 5b 30 5d 29 2a 6e 43 6f 6c 29 3b 0a 20 20 20  l[0])*nCol);.   
33f2c 20 74 65 73 74 63 61 73 65 28 20 61 43 6f 6c 3d   testcase( aCol=
33f2d 3d 30 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  =0 );.  }else{. 
33f2e 20 20 20 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20     nCol = 0;.   
33f2f 20 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20   aCol = 0;.  }. 
33f30 20 2a 70 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a   *pnCol = nCol;.
33f31 20 20 2a 70 61 43 6f 6c 20 3d 20 61 43 6f 6c 3b    *paCol = aCol;
33f32 0a 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f  ..  for(i=0, pCo
33f33 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43 6f 6c 3b 20  l=aCol; i<nCol; 
33f34 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20  i++, pCol++){.  
33f35 20 20 2f 2a 20 47 65 74 20 61 6e 20 61 70 70 72    /* Get an appr
33f36 6f 70 72 69 61 74 65 20 6e 61 6d 65 20 66 6f 72  opriate name for
33f37 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20   the column.    
33f38 2a 2f 0a 20 20 20 20 70 20 3d 20 70 45 4c 69 73  */.    p = pELis
33f39 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  t->a[i].pExpr;. 
33f3a 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52     assert( p->pR
33f3b 69 67 68 74 3d 3d 30 20 7c 7c 20 45 78 70 72 48  ight==0 || ExprH
33f3c 61 73 50 72 6f 70 65 72 74 79 28 70 2d 3e 70 52  asProperty(p->pR
33f3d 69 67 68 74 2c 20 45 50 5f 49 6e 74 56 61 6c 75  ight, EP_IntValu
33f3e 65 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e).             
33f3f 20 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74 2d 3e    || p->pRight->
33f40 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 20 7c 7c 20 70  u.zToken==0 || p
33f41 2d 3e 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b  ->pRight->u.zTok
33f42 65 6e 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 20 20  en[0]!=0 );.    
33f43 69 66 28 20 28 7a 4e 61 6d 65 20 3d 20 70 45 4c  if( (zName = pEL
33f44 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  ist->a[i].zName)
33f45 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  !=0 ){.      /* 
33f46 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 6f  If the column co
33f47 6e 74 61 69 6e 73 20 61 6e 20 22 41 53 20 3c 6e  ntains an "AS <n
33f48 61 6d 65 3e 22 20 70 68 72 61 73 65 2c 20 75 73  ame>" phrase, us
33f49 65 20 3c 6e 61 6d 65 3e 20 61 73 20 74 68 65 20  e <name> as the 
33f4a 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 7a 4e  name */.      zN
33f4b 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ame = sqlite3DbS
33f4c 74 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 29  trDup(db, zName)
33f4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
33f4e 20 20 20 45 78 70 72 20 2a 70 43 6f 6c 45 78 70     Expr *pColExp
33f4f 72 20 3d 20 70 3b 20 20 2f 2a 20 54 68 65 20 65  r = p;  /* The e
33f50 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69  xpression that i
33f51 73 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c  s the result col
33f52 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  umn name */.    
33f53 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
33f54 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
33f55 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
33f56 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20  this expression 
33f57 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  */.      while( 
33f58 70 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  pColExpr->op==TK
33f59 5f 44 4f 54 20 29 7b 0a 20 20 20 20 20 20 20 20  _DOT ){.        
33f5a 70 43 6f 6c 45 78 70 72 20 3d 20 70 43 6f 6c 45  pColExpr = pColE
33f5b 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20  xpr->pRight;.   
33f5c 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f       assert( pCo
33f5d 6c 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 20  lExpr!=0 );.    
33f5e 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43    }.      if( pC
33f5f 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43  olExpr->op==TK_C
33f60 4f 4c 55 4d 4e 20 26 26 20 41 4c 57 41 59 53 28  OLUMN && ALWAYS(
33f61 70 43 6f 6c 45 78 70 72 2d 3e 70 54 61 62 21 3d  pColExpr->pTab!=
33f62 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  0) ){.        /*
33f63 20 46 6f 72 20 63 6f 6c 75 6d 6e 73 20 75 73 65   For columns use
33f64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
33f65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20   name */.       
33f66 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 43 6f 6c   int iCol = pCol
33f67 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20  Expr->iColumn;. 
33f68 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 43         pTab = pC
33f69 6f 6c 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20  olExpr->pTab;.  
33f6a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30        if( iCol<0
33f6b 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e   ) iCol = pTab->
33f6c 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 7a  iPKey;.        z
33f6d 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  Name = sqlite3MP
33f6e 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 0a  rintf(db, "%s",.
33f6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f70 20 69 43 6f 6c 3e 3d 30 20 3f 20 70 54 61 62 2d   iCol>=0 ? pTab-
33f71 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d  >aCol[iCol].zNam
33f72 65 20 3a 20 22 72 6f 77 69 64 22 29 3b 0a 20 20  e : "rowid");.  
33f73 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43      }else if( pC
33f74 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49  olExpr->op==TK_I
33f75 44 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  D ){.        ass
33f76 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
33f77 70 65 72 74 79 28 70 43 6f 6c 45 78 70 72 2c 20  perty(pColExpr, 
33f78 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a  EP_IntValue) );.
33f79 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20          zName = 
33f7a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
33f7b 62 2c 20 22 25 73 22 2c 20 70 43 6f 6c 45 78 70  b, "%s", pColExp
33f7c 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20  r->u.zToken);.  
33f7d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
33f7e 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 6f 72     /* Use the or
33f7f 69 67 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 74  iginal text of t
33f80 68 65 20 63 6f 6c 75 6d 6e 20 65 78 70 72 65 73  he column expres
33f81 73 69 6f 6e 20 61 73 20 69 74 73 20 6e 61 6d 65  sion as its name
33f82 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d   */.        zNam
33f83 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  e = sqlite3MPrin
33f84 74 66 28 64 62 2c 20 22 25 73 22 2c 20 70 45 4c  tf(db, "%s", pEL
33f85 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29  ist->a[i].zSpan)
33f86 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
33f87 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
33f88 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
33f89 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
33f8a 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  db, zName);.    
33f8b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
33f8c 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65      /* Make sure
33f8d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
33f8e 20 69 73 20 75 6e 69 71 75 65 2e 20 20 49 66 20   is unique.  If 
33f8f 74 68 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20  the name is not 
33f90 75 6e 69 71 75 65 2c 0a 20 20 20 20 2a 2a 20 61  unique,.    ** a
33f91 70 70 65 6e 64 20 61 20 69 6e 74 65 67 65 72 20  ppend a integer 
33f92 74 6f 20 74 68 65 20 6e 61 6d 65 20 73 6f 20 74  to the name so t
33f93 68 61 74 20 69 74 20 62 65 63 6f 6d 65 73 20 75  hat it becomes u
33f94 6e 69 71 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  nique..    */.  
33f95 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    nName = sqlite
33f96 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
33f97 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 63 6e 74 3d  ;.    for(j=cnt=
33f98 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20  0; j<i; j++){.  
33f99 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
33f9a 74 72 49 43 6d 70 28 61 43 6f 6c 5b 6a 5d 2e 7a  trICmp(aCol[j].z
33f9b 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20  Name, zName)==0 
33f9c 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  ){.        char 
33f9d 2a 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20 20 20 20  *zNewName;.     
33f9e 20 20 20 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20     zName[nName] 
33f9f 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 4e 65  = 0;.        zNe
33fa0 77 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d  wName = sqlite3M
33fa1 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 3a 25  Printf(db, "%s:%
33fa2 64 22 2c 20 7a 4e 61 6d 65 2c 20 2b 2b 63 6e 74  d", zName, ++cnt
33fa3 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
33fa4 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61  e3DbFree(db, zNa
33fa5 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61  me);.        zNa
33fa6 6d 65 20 3d 20 7a 4e 65 77 4e 61 6d 65 3b 0a 20  me = zNewName;. 
33fa7 20 20 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0a 20         j = -1;. 
33fa8 20 20 20 20 20 20 20 69 66 28 20 7a 4e 61 6d 65         if( zName
33fa9 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
33faa 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
33fab 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61  Col->zName = zNa
33fac 6d 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62  me;.  }.  if( db
33fad 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
33fae 7b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  {.    for(j=0; j
33faf 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  <i; j++){.      
33fb0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
33fb1 2c 20 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29  , aCol[j].zName)
33fb2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
33fb3 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 43  te3DbFree(db, aC
33fb4 6f 6c 29 3b 0a 20 20 20 20 2a 70 61 43 6f 6c 20  ol);.    *paCol 
33fb5 3d 20 30 3b 0a 20 20 20 20 2a 70 6e 43 6f 6c 20  = 0;.    *pnCol 
33fb6 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 0;.    return 
33fb7 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
33fb8 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
33fb9 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  E_OK;.}../*.** A
33fba 64 64 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c  dd type and coll
33fbb 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f  ation informatio
33fbc 6e 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 6c 69  n to a column li
33fbd 73 74 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 61  st based on.** a
33fbe 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
33fbf 74 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 63 6f  t..** .** The co
33fc0 6c 75 6d 6e 20 6c 69 73 74 20 70 72 65 73 75 6d  lumn list presum
33fc1 61 62 6c 79 20 63 61 6d 65 20 66 72 6f 6d 20 73  ably came from s
33fc2 65 6c 65 63 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  electColumnNames
33fc3 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 29 2e 0a  FromExprList()..
33fc4 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6c 69  ** The column li
33fc5 73 74 20 68 61 73 20 6f 6e 6c 79 20 6e 61 6d 65  st has only name
33fc6 73 2c 20 6e 6f 74 20 74 79 70 65 73 20 6f 72 20  s, not types or 
33fc7 63 6f 6c 6c 61 74 69 6f 6e 73 2e 20 20 54 68 69  collations.  Thi
33fc8 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67 6f 65  s.** routine goe
33fc9 73 20 74 68 72 6f 75 67 68 20 61 6e 64 20 61 64  s through and ad
33fca 64 73 20 74 68 65 20 74 79 70 65 73 20 61 6e 64  ds the types and
33fcb 20 63 6f 6c 6c 61 74 69 6f 6e 73 2e 0a 2a 2a 0a   collations..**.
33fcc 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
33fcd 72 65 71 75 69 72 65 73 20 74 68 61 74 20 61 6c  requires that al
33fce 6c 20 69 64 65 6e 74 69 66 69 65 72 73 20 69 6e  l identifiers in
33fcf 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73   the SELECT.** s
33fd0 74 61 74 65 6d 65 6e 74 20 62 65 20 72 65 73 6f  tatement be reso
33fd1 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lved..*/.static 
33fd2 76 6f 69 64 20 73 65 6c 65 63 74 41 64 64 43 6f  void selectAddCo
33fd3 6c 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c 6c 61  lumnTypeAndColla
33fd4 74 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70  tion(.  Parse *p
33fd5 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Parse,        /*
33fd6 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
33fd7 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c  s */.  int nCol,
33fd8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
33fd9 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
33fda 73 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61  s */.  Column *a
33fdb 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Col,         /* 
33fdc 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  List of columns 
33fdd 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
33fde 6c 65 63 74 20 20 20 20 20 20 20 2f 2a 20 53 45  lect       /* SE
33fdf 4c 45 43 54 20 75 73 65 64 20 74 6f 20 64 65 74  LECT used to det
33fe0 65 72 6d 69 6e 65 20 74 79 70 65 73 20 61 6e 64  ermine types and
33fe1 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 2a 2f 0a 29   collations */.)
33fe2 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
33fe3 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
33fe4 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b  NameContext sNC;
33fe5 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b  .  Column *pCol;
33fe6 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
33fe7 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45 78  l;.  int i;.  Ex
33fe8 70 72 20 2a 70 3b 0a 20 20 73 74 72 75 63 74 20  pr *p;.  struct 
33fe9 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61  ExprList_item *a
33fea 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 53 65  ;..  assert( pSe
33feb 6c 65 63 74 21 3d 30 20 29 3b 0a 20 20 61 73 73  lect!=0 );.  ass
33fec 65 72 74 28 20 28 70 53 65 6c 65 63 74 2d 3e 73  ert( (pSelect->s
33fed 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73  elFlags & SF_Res
33fee 6f 6c 76 65 64 29 21 3d 30 20 29 3b 0a 20 20 61  olved)!=0 );.  a
33fef 73 73 65 72 74 28 20 6e 43 6f 6c 3d 3d 70 53 65  ssert( nCol==pSe
33ff0 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  lect->pEList->nE
33ff1 78 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  xpr || db->mallo
33ff2 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28  cFailed );.  if(
33ff3 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
33ff4 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65  d ) return;.  me
33ff5 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69  mset(&sNC, 0, si
33ff6 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e  zeof(sNC));.  sN
33ff7 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53 65  C.pSrcList = pSe
33ff8 6c 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20 61 20  lect->pSrc;.  a 
33ff9 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  = pSelect->pELis
33ffa 74 2d 3e 61 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  t->a;.  for(i=0,
33ffb 20 70 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43   pCol=aCol; i<nC
33ffc 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29  ol; i++, pCol++)
33ffd 7b 0a 20 20 20 20 70 20 3d 20 61 5b 69 5d 2e 70  {.    p = a[i].p
33ffe 45 78 70 72 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e  Expr;.    pCol->
33fff 7a 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 44  zType = sqlite3D
34000 62 53 74 72 44 75 70 28 64 62 2c 20 63 6f 6c 75  bStrDup(db, colu
34001 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20  mnType(&sNC, p, 
34002 30 2c 20 30 2c 20 30 29 29 3b 0a 20 20 20 20 70  0, 0, 0));.    p
34003 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20  Col->affinity = 
34004 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e  sqlite3ExprAffin
34005 69 74 79 28 70 29 3b 0a 20 20 20 20 69 66 28 20  ity(p);.    if( 
34006 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d  pCol->affinity==
34007 30 20 29 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  0 ) pCol->affini
34008 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ty = SQLITE_AFF_
34009 4e 4f 4e 45 3b 0a 20 20 20 20 70 43 6f 6c 6c 20  NONE;.    pColl 
3400a 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c  = sqlite3ExprCol
3400b 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 29 3b  lSeq(pParse, p);
3400c 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29  .    if( pColl )
3400d 7b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 43  {.      pCol->zC
3400e 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53  oll = sqlite3DbS
3400f 74 72 44 75 70 28 64 62 2c 20 70 43 6f 6c 6c 2d  trDup(db, pColl-
34010 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20  >zName);.    }. 
34011 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65   }.}../*.** Give
34012 6e 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  n a SELECT state
34013 6d 65 6e 74 2c 20 67 65 6e 65 72 61 74 65 20 61  ment, generate a
34014 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
34015 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 0a   that describes.
34016 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ** the result se
34017 74 20 6f 66 20 74 68 61 74 20 53 45 4c 45 43 54  t of that SELECT
34018 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
34019 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74  ATE Table *sqlit
3401a 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
3401b 65 63 74 28 50 61 72 73 65 20 2a 70 50 61 72 73  ect(Parse *pPars
3401c 65 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  e, Select *pSele
3401d 63 74 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54  ct){.  Table *pT
3401e 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ab;.  sqlite3 *d
3401f 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
34020 20 20 69 6e 74 20 73 61 76 65 64 46 6c 61 67 73    int savedFlags
34021 3b 0a 0a 20 20 73 61 76 65 64 46 6c 61 67 73 20  ;..  savedFlags 
34022 3d 20 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 64  = db->flags;.  d
34023 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c  b->flags &= ~SQL
34024 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73  ITE_FullColNames
34025 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d  ;.  db->flags |=
34026 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c   SQLITE_ShortCol
34027 4e 61 6d 65 73 3b 0a 20 20 73 71 6c 69 74 65 33  Names;.  sqlite3
34028 53 65 6c 65 63 74 50 72 65 70 28 70 50 61 72 73  SelectPrep(pPars
34029 65 2c 20 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a  e, pSelect, 0);.
3402a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
3402b 72 72 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  rr ) return 0;. 
3402c 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 2d   while( pSelect-
3402d 3e 70 50 72 69 6f 72 20 29 20 70 53 65 6c 65 63  >pPrior ) pSelec
3402e 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72  t = pSelect->pPr
3402f 69 6f 72 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73  ior;.  db->flags
34030 20 3d 20 73 61 76 65 64 46 6c 61 67 73 3b 0a 20   = savedFlags;. 
34031 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 44   pTab = sqlite3D
34032 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
34033 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 20 29 3b  sizeof(Table) );
34034 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29  .  if( pTab==0 )
34035 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
34036 20 20 7d 0a 20 20 2f 2a 20 54 68 65 20 73 71 6c    }.  /* The sql
34037 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53  ite3ResultSetOfS
34038 65 6c 65 63 74 28 29 20 69 73 20 6f 6e 6c 79 20  elect() is only 
34039 75 73 65 64 20 6e 20 63 6f 6e 74 65 78 74 73 20  used n contexts 
3403a 77 68 65 72 65 20 6c 6f 6f 6b 61 73 69 64 65 0a  where lookaside.
3403b 20 20 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64    ** is disabled
3403c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62   */.  assert( db
3403d 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
3403e 62 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 70 54 61  bled==0 );.  pTa
3403f 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70  b->nRef = 1;.  p
34040 54 61 62 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a  Tab->zName = 0;.
34041 20 20 70 54 61 62 2d 3e 6e 52 6f 77 45 73 74 20    pTab->nRowEst 
34042 3d 20 31 30 30 30 30 30 30 3b 0a 20 20 73 65 6c  = 1000000;.  sel
34043 65 63 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78  ectColumnsFromEx
34044 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  prList(pParse, p
34045 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c 20  Select->pEList, 
34046 26 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 26 70 54  &pTab->nCol, &pT
34047 61 62 2d 3e 61 43 6f 6c 29 3b 0a 20 20 73 65 6c  ab->aCol);.  sel
34048 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65  ectAddColumnType
34049 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61  AndCollation(pPa
3404a 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c  rse, pTab->nCol,
3404b 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65   pTab->aCol, pSe
3404c 6c 65 63 74 29 3b 0a 20 20 70 54 61 62 2d 3e 69  lect);.  pTab->i
3404d 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 69 66 28  PKey = -1;.  if(
3404e 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
3404f 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
34050 44 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 20  DeleteTable(db, 
34051 70 54 61 62 29 3b 0a 20 20 20 20 72 65 74 75 72  pTab);.    retur
34052 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 0;.  }.  retur
34053 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pTab;.}../*.**
34054 20 47 65 74 20 61 20 56 44 42 45 20 66 6f 72 20   Get a VDBE for 
34055 74 68 65 20 67 69 76 65 6e 20 70 61 72 73 65 72  the given parser
34056 20 63 6f 6e 74 65 78 74 2e 20 20 43 72 65 61 74   context.  Creat
34057 65 20 61 20 6e 65 77 20 6f 6e 65 20 69 66 20 6e  e a new one if n
34058 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 49 66 20  ecessary..** If 
34059 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
3405a 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 61 6e 64   return NULL and
3405b 20 6c 65 61 76 65 20 61 20 6d 65 73 73 61 67 65   leave a message
3405c 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53   in pParse..*/.S
3405d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
3405e 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64  be *sqlite3GetVd
3405f 62 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  be(Parse *pParse
34060 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
34061 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
34062 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20  if( v==0 ){.    
34063 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
34064 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  e = sqlite3VdbeC
34065 72 65 61 74 65 28 70 50 61 72 73 65 2d 3e 64 62  reate(pParse->db
34066 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
34067 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 20  E_OMIT_TRACE.   
34068 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20 20   if( v ){.      
34069 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
3406a 30 28 76 2c 20 4f 50 5f 54 72 61 63 65 29 3b 0a  0(v, OP_Trace);.
3406b 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
3406c 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a 0a  .  return v;.}..
3406d 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  ./*.** Compute t
3406e 68 65 20 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f  he iLimit and iO
3406f 66 66 73 65 74 20 66 69 65 6c 64 73 20 6f 66 20  ffset fields of 
34070 74 68 65 20 53 45 4c 45 43 54 20 62 61 73 65 64  the SELECT based
34071 20 6f 6e 20 74 68 65 0a 2a 2a 20 70 4c 69 6d 69   on the.** pLimi
34072 74 20 61 6e 64 20 70 4f 66 66 73 65 74 20 65 78  t and pOffset ex
34073 70 72 65 73 73 69 6f 6e 73 2e 20 20 70 4c 69 6d  pressions.  pLim
34074 69 74 20 61 6e 64 20 70 4f 66 66 73 65 74 20 68  it and pOffset h
34075 6f 6c 64 20 74 68 65 20 65 78 70 72 65 73 73 69  old the expressi
34076 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 70 70 65  ons.** that appe
34077 61 72 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e  ar in the origin
34078 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  al SQL statement
34079 20 61 66 74 65 72 20 74 68 65 20 4c 49 4d 49 54   after the LIMIT
3407a 20 61 6e 64 20 4f 46 46 53 45 54 0a 2a 2a 20 6b   and OFFSET.** k
3407b 65 79 77 6f 72 64 73 2e 20 20 4f 72 20 4e 55 4c  eywords.  Or NUL
3407c 4c 20 69 66 20 74 68 6f 73 65 20 6b 65 79 77 6f  L if those keywo
3407d 72 64 73 20 61 72 65 20 6f 6d 69 74 74 65 64 2e  rds are omitted.
3407e 20 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66   iLimit and iOff
3407f 73 65 74 20 0a 2a 2a 20 61 72 65 20 74 68 65 20  set .** are the 
34080 69 6e 74 65 67 65 72 20 6d 65 6d 6f 72 79 20 72  integer memory r
34081 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73 20  egister numbers 
34082 66 6f 72 20 63 6f 75 6e 74 65 72 73 20 75 73 65  for counters use
34083 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 0a 2a 2a  d to compute .**
34084 20 74 68 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f   the limit and o
34085 66 66 73 65 74 2e 20 20 49 66 20 74 68 65 72 65  ffset.  If there
34086 20 69 73 20 6e 6f 20 6c 69 6d 69 74 20 61 6e 64   is no limit and
34087 2f 6f 72 20 6f 66 66 73 65 74 2c 20 74 68 65 6e  /or offset, then
34088 20 0a 2a 2a 20 69 4c 69 6d 69 74 20 61 6e 64 20   .** iLimit and 
34089 69 4f 66 66 73 65 74 20 61 72 65 20 6e 65 67 61  iOffset are nega
3408a 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tive..**.** This
3408b 20 72 6f 75 74 69 6e 65 20 63 68 61 6e 67 65 73   routine changes
3408c 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 69   the values of i
3408d 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65  Limit and iOffse
3408e 74 20 6f 6e 6c 79 20 69 66 0a 2a 2a 20 61 20 6c  t only if.** a l
3408f 69 6d 69 74 20 6f 72 20 6f 66 66 73 65 74 20 69  imit or offset i
34090 73 20 64 65 66 69 6e 65 64 20 62 79 20 70 4c 69  s defined by pLi
34091 6d 69 74 20 61 6e 64 20 70 4f 66 66 73 65 74 2e  mit and pOffset.
34092 20 20 69 4c 69 6d 69 74 20 61 6e 64 0a 2a 2a 20    iLimit and.** 
34093 69 4f 66 66 73 65 74 20 73 68 6f 75 6c 64 20 68  iOffset should h
34094 61 76 65 20 62 65 65 6e 20 70 72 65 73 65 74 20  ave been preset 
34095 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65 20 64  to appropriate d
34096 65 66 61 75 6c 74 20 76 61 6c 75 65 73 0a 2a 2a  efault values.**
34097 20 28 75 73 75 61 6c 6c 79 20 62 75 74 20 6e 6f   (usually but no
34098 74 20 61 6c 77 61 79 73 20 2d 31 29 20 70 72 69  t always -1) pri
34099 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68  or to calling th
3409a 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 4f  is routine..** O
3409b 6e 6c 79 20 69 66 20 70 4c 69 6d 69 74 21 3d 30  nly if pLimit!=0
3409c 20 6f 72 20 70 4f 66 66 73 65 74 21 3d 30 20 64   or pOffset!=0 d
3409d 6f 20 74 68 65 20 6c 69 6d 69 74 20 72 65 67 69  o the limit regi
3409e 73 74 65 72 73 20 67 65 74 0a 2a 2a 20 72 65 64  sters get.** red
3409f 65 66 69 6e 65 64 2e 20 20 54 68 65 20 55 4e 49  efined.  The UNI
340a0 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 20  ON ALL operator 
340a1 75 73 65 73 20 74 68 69 73 20 70 72 6f 70 65 72  uses this proper
340a2 74 79 20 74 6f 20 66 6f 72 63 65 0a 2a 2a 20 74  ty to force.** t
340a3 68 65 20 72 65 75 73 65 20 6f 66 20 74 68 65 20  he reuse of the 
340a4 73 61 6d 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f  same limit and o
340a5 66 66 73 65 74 20 72 65 67 69 73 74 65 72 73 20  ffset registers 
340a6 61 63 72 6f 73 73 20 6d 75 6c 74 69 70 6c 65 0a  across multiple.
340a7 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ** SELECT statem
340a8 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ents..*/.static 
340a9 76 6f 69 64 20 63 6f 6d 70 75 74 65 4c 69 6d 69  void computeLimi
340aa 74 52 65 67 69 73 74 65 72 73 28 50 61 72 73 65  tRegisters(Parse
340ab 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74   *pParse, Select
340ac 20 2a 70 2c 20 69 6e 74 20 69 42 72 65 61 6b 29   *p, int iBreak)
340ad 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30 3b  {.  Vdbe *v = 0;
340ae 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 20 3d 20  .  int iLimit = 
340af 30 3b 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74  0;.  int iOffset
340b0 3b 0a 20 20 69 6e 74 20 61 64 64 72 31 2c 20 6e  ;.  int addr1, n
340b1 3b 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69  ;.  if( p->iLimi
340b2 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f  t ) return;..  /
340b3 2a 20 0a 20 20 2a 2a 20 22 4c 49 4d 49 54 20 2d  * .  ** "LIMIT -
340b4 31 22 20 61 6c 77 61 79 73 20 73 68 6f 77 73 20  1" always shows 
340b5 61 6c 6c 20 72 6f 77 73 2e 20 20 54 68 65 72 65  all rows.  There
340b6 20 69 73 20 73 6f 6d 65 0a 20 20 2a 2a 20 63 6f   is some.  ** co
340b7 6e 74 72 61 76 65 72 73 79 20 61 62 6f 75 74 20  ntraversy about 
340b8 77 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74  what the correct
340b9 20 62 65 68 61 76 69 6f 72 20 73 68 6f 75 6c 64   behavior should
340ba 20 62 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 75   be..  ** The cu
340bb 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61  rrent implementa
340bc 74 69 6f 6e 20 69 6e 74 65 72 70 72 65 74 73 20  tion interprets 
340bd 22 4c 49 4d 49 54 20 30 22 20 74 6f 20 6d 65 61  "LIMIT 0" to mea
340be 6e 0a 20 20 2a 2a 20 6e 6f 20 72 6f 77 73 2e 0a  n.  ** no rows..
340bf 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78    */.  sqlite3Ex
340c0 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61  prCacheClear(pPa
340c1 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20  rse);.  assert( 
340c2 70 2d 3e 70 4f 66 66 73 65 74 3d 3d 30 20 7c 7c  p->pOffset==0 ||
340c3 20 70 2d 3e 70 4c 69 6d 69 74 21 3d 30 20 29 3b   p->pLimit!=0 );
340c4 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74  .  if( p->pLimit
340c5 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4c 69 6d 69   ){.    p->iLimi
340c6 74 20 3d 20 69 4c 69 6d 69 74 20 3d 20 2b 2b 70  t = iLimit = ++p
340c7 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
340c8 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
340c9 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20  dbe(pParse);.   
340ca 20 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29   if( NEVER(v==0)
340cb 20 29 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 56   ) return;  /* V
340cc 44 42 45 20 73 68 6f 75 6c 64 20 68 61 76 65 20  DBE should have 
340cd 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c  already been all
340ce 6f 63 61 74 65 64 20 2a 2f 0a 20 20 20 20 69 66  ocated */.    if
340cf 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49  ( sqlite3ExprIsI
340d0 6e 74 65 67 65 72 28 70 2d 3e 70 4c 69 6d 69 74  nteger(p->pLimit
340d1 2c 20 26 6e 29 20 29 7b 0a 20 20 20 20 20 20 73  , &n) ){.      s
340d2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
340d3 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
340d4 6e 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20  n, iLimit);.    
340d5 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
340d6 2c 20 22 4c 49 4d 49 54 20 63 6f 75 6e 74 65 72  , "LIMIT counter
340d7 22 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  "));.      if( n
340d8 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==0 ){.        s
340d9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
340da 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
340db 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 7d  iBreak);.      }
340dc 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66  else{.        if
340dd 28 20 70 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 20  ( p->nSelectRow 
340de 3e 20 28 64 6f 75 62 6c 65 29 6e 20 29 20 70 2d  > (double)n ) p-
340df 3e 6e 53 65 6c 65 63 74 52 6f 77 20 3d 20 28 64  >nSelectRow = (d
340e0 6f 75 62 6c 65 29 6e 3b 0a 20 20 20 20 20 20 7d  ouble)n;.      }
340e1 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
340e2 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
340e3 65 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 4c 69  e(pParse, p->pLi
340e4 6d 69 74 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20  mit, iLimit);.  
340e5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
340e6 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74  ddOp1(v, OP_Must
340e7 42 65 49 6e 74 2c 20 69 4c 69 6d 69 74 29 3b 0a  BeInt, iLimit);.
340e8 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
340e9 74 28 28 76 2c 20 22 4c 49 4d 49 54 20 63 6f 75  t((v, "LIMIT cou
340ea 6e 74 65 72 22 29 29 3b 0a 20 20 20 20 20 20 73  nter"));.      s
340eb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
340ec 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 69  (v, OP_IfZero, i
340ed 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b 29 3b 0a  Limit, iBreak);.
340ee 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d      }.    if( p-
340ef 3e 70 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20  >pOffset ){.    
340f0 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 69    p->iOffset = i
340f1 4f 66 66 73 65 74 20 3d 20 2b 2b 70 50 61 72 73  Offset = ++pPars
340f2 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 70  e->nMem;.      p
340f3 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 20 20  Parse->nMem++;  
340f4 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 20   /* Allocate an 
340f5 65 78 74 72 61 20 72 65 67 69 73 74 65 72 20 66  extra register f
340f6 6f 72 20 6c 69 6d 69 74 2b 6f 66 66 73 65 74 20  or limit+offset 
340f7 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
340f8 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
340f9 20 70 2d 3e 70 4f 66 66 73 65 74 2c 20 69 4f 66   p->pOffset, iOf
340fa 66 73 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  fset);.      sql
340fb 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
340fc 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20  , OP_MustBeInt, 
340fd 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20  iOffset);.      
340fe 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
340ff 22 4f 46 46 53 45 54 20 63 6f 75 6e 74 65 72 22  "OFFSET counter"
34100 29 29 3b 0a 20 20 20 20 20 20 61 64 64 72 31 20  ));.      addr1 
34101 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
34102 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c  Op1(v, OP_IfPos,
34103 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20   iOffset);.     
34104 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
34105 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
34106 2c 20 30 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20  , 0, iOffset);. 
34107 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
34108 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
34109 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
3410a 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
3410b 50 5f 41 64 64 2c 20 69 4c 69 6d 69 74 2c 20 69  P_Add, iLimit, i
3410c 4f 66 66 73 65 74 2c 20 69 4f 66 66 73 65 74 2b  Offset, iOffset+
3410d 31 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  1);.      VdbeCo
3410e 6d 6d 65 6e 74 28 28 76 2c 20 22 4c 49 4d 49 54  mment((v, "LIMIT
3410f 2b 4f 46 46 53 45 54 22 29 29 3b 0a 20 20 20 20  +OFFSET"));.    
34110 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65    addr1 = sqlite
34111 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
34112 50 5f 49 66 50 6f 73 2c 20 69 4c 69 6d 69 74 29  P_IfPos, iLimit)
34113 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
34114 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
34115 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 69 4f 66  Integer, -1, iOf
34116 66 73 65 74 2b 31 29 3b 0a 20 20 20 20 20 20 73  fset+1);.      s
34117 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
34118 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20  re(v, addr1);.  
34119 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64    }.  }.}..#ifnd
3411a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
3411b 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 2f  OMPOUND_SELECT./
3411c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
3411d 61 70 70 72 6f 70 72 69 61 74 65 20 63 6f 6c 6c  appropriate coll
3411e 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66  ating sequence f
3411f 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 63  or the iCol-th c
34120 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74 68 65 20  olumn of.** the 
34121 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20 74  result set for t
34122 68 65 20 63 6f 6d 70 6f 75 6e 64 2d 73 65 6c 65  he compound-sele
34123 63 74 20 73 74 61 74 65 6d 65 6e 74 20 22 70 22  ct statement "p"
34124 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69  .  Return NULL i
34125 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20  f.** the column 
34126 68 61 73 20 6e 6f 20 64 65 66 61 75 6c 74 20 63  has no default c
34127 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
34128 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c  e..**.** The col
34129 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
3412a 66 6f 72 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64  for the compound
3412b 20 73 65 6c 65 63 74 20 69 73 20 74 61 6b 65 6e   select is taken
3412c 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 6c 65 66   from the.** lef
3412d 74 2d 6d 6f 73 74 20 74 65 72 6d 20 6f 66 20 74  t-most term of t
3412e 68 65 20 73 65 6c 65 63 74 20 74 68 61 74 20 68  he select that h
3412f 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73  as a collating s
34130 65 71 75 65 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74  equence..*/.stat
34131 69 63 20 43 6f 6c 6c 53 65 71 20 2a 6d 75 6c 74  ic CollSeq *mult
34132 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 50  iSelectCollSeq(P
34133 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65  arse *pParse, Se
34134 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69 43 6f  lect *p, int iCo
34135 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  l){.  CollSeq *p
34136 52 65 74 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50  Ret;.  if( p->pP
34137 72 69 6f 72 20 29 7b 0a 20 20 20 20 70 52 65 74  rior ){.    pRet
34138 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f   = multiSelectCo
34139 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 2d  llSeq(pParse, p-
3413a 3e 70 50 72 69 6f 72 2c 20 69 43 6f 6c 29 3b 0a  >pPrior, iCol);.
3413b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 52 65    }else{.    pRe
3413c 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73  t = 0;.  }.  ass
3413d 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20 29 3b 0a  ert( iCol>=0 );.
3413e 20 20 69 66 28 20 70 52 65 74 3d 3d 30 20 26 26    if( pRet==0 &&
3413f 20 69 43 6f 6c 3c 70 2d 3e 70 45 4c 69 73 74 2d   iCol<p->pEList-
34140 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 70 52  >nExpr ){.    pR
34141 65 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  et = sqlite3Expr
34142 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
34143 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f  p->pEList->a[iCo
34144 6c 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a 20  l].pExpr);.  }. 
34145 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a   return pRet;.}.
34146 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
34147 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  _OMIT_COMPOUND_S
34148 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 20 46 6f 72  ELECT */../* For
34149 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a  ward reference *
3414a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 75 6c  /.static int mul
3414b 74 69 53 65 6c 65 63 74 4f 72 64 65 72 42 79 28  tiSelectOrderBy(
3414c 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
3414d 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  ,        /* Pars
3414e 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
3414f 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20   Select *p,     
34150 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69         /* The ri
34151 67 68 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45  ght-most of SELE
34152 43 54 73 20 74 6f 20 62 65 20 63 6f 64 65 64 20  CTs to be coded 
34153 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  */.  SelectDest 
34154 2a 70 44 65 73 74 20 20 20 20 20 2f 2a 20 57 68  *pDest     /* Wh
34155 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 71 75  at to do with qu
34156 65 72 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29  ery results */.)
34157 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;...#ifndef SQLI
34158 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44  TE_OMIT_COMPOUND
34159 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 54 68  _SELECT./*.** Th
3415a 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
3415b 6c 6c 65 64 20 74 6f 20 70 72 6f 63 65 73 73 20  lled to process 
3415c 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79  a compound query
3415d 20 66 6f 72 6d 20 66 72 6f 6d 0a 2a 2a 20 74 77   form from.** tw
3415e 6f 20 6f 72 20 6d 6f 72 65 20 73 65 70 61 72 61  o or more separa
3415f 74 65 20 71 75 65 72 69 65 73 20 75 73 69 6e 67  te queries using
34160 20 55 4e 49 4f 4e 2c 20 55 4e 49 4f 4e 20 41 4c   UNION, UNION AL
34161 4c 2c 20 45 58 43 45 50 54 2c 20 6f 72 0a 2a 2a  L, EXCEPT, or.**
34162 20 49 4e 54 45 52 53 45 43 54 0a 2a 2a 0a 2a 2a   INTERSECT.**.**
34163 20 22 70 22 20 70 6f 69 6e 74 73 20 74 6f 20 74   "p" points to t
34164 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66  he right-most of
34165 20 74 68 65 20 74 77 6f 20 71 75 65 72 69 65 73   the two queries
34166 2e 20 20 74 68 65 20 71 75 65 72 79 20 6f 6e 20  .  the query on 
34167 74 68 65 0a 2a 2a 20 6c 65 66 74 20 69 73 20 70  the.** left is p
34168 2d 3e 70 50 72 69 6f 72 2e 20 20 54 68 65 20 6c  ->pPrior.  The l
34169 65 66 74 20 71 75 65 72 79 20 63 6f 75 6c 64 20  eft query could 
3416a 61 6c 73 6f 20 62 65 20 61 20 63 6f 6d 70 6f 75  also be a compou
3416b 6e 64 20 71 75 65 72 79 0a 2a 2a 20 69 6e 20 77  nd query.** in w
3416c 68 69 63 68 20 63 61 73 65 20 74 68 69 73 20 72  hich case this r
3416d 6f 75 74 69 6e 65 20 77 69 6c 6c 20 62 65 20 63  outine will be c
3416e 61 6c 6c 65 64 20 72 65 63 75 72 73 69 76 65 6c  alled recursivel
3416f 79 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  y. .**.** The re
34170 73 75 6c 74 73 20 6f 66 20 74 68 65 20 74 6f 74  sults of the tot
34171 61 6c 20 71 75 65 72 79 20 61 72 65 20 74 6f 20  al query are to 
34172 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  be written into 
34173 61 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a 2a 2a  a destination.**
34174 20 6f 66 20 74 79 70 65 20 65 44 65 73 74 20 77   of type eDest w
34175 69 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 50  ith parameter iP
34176 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70  arm..**.** Examp
34177 6c 65 20 31 3a 20 20 43 6f 6e 73 69 64 65 72 20  le 1:  Consider 
34178 61 20 74 68 72 65 65 2d 77 61 79 20 63 6f 6d 70  a three-way comp
34179 6f 75 6e 64 20 53 51 4c 20 73 74 61 74 65 6d 65  ound SQL stateme
3417a 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  nt..**.**     SE
3417b 4c 45 43 54 20 61 20 46 52 4f 4d 20 74 31 20 55  LECT a FROM t1 U
3417c 4e 49 4f 4e 20 53 45 4c 45 43 54 20 62 20 46 52  NION SELECT b FR
3417d 4f 4d 20 74 32 20 55 4e 49 4f 4e 20 53 45 4c 45  OM t2 UNION SELE
3417e 43 54 20 63 20 46 52 4f 4d 20 74 33 0a 2a 2a 0a  CT c FROM t3.**.
3417f 2a 2a 20 54 68 69 73 20 73 74 61 74 65 6d 65 6e  ** This statemen
34180 74 20 69 73 20 70 61 72 73 65 64 20 75 70 20 61  t is parsed up a
34181 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
34182 20 20 20 20 20 53 45 4c 45 43 54 20 63 20 46 52       SELECT c FR
34183 4f 4d 20 74 33 0a 2a 2a 20 20 20 20 20 20 7c 0a  OM t3.**      |.
34184 2a 2a 20 20 20 20 20 20 60 2d 2d 2d 2d 2d 3e 20  **      `-----> 
34185 20 53 45 4c 45 43 54 20 62 20 46 52 4f 4d 20 74   SELECT b FROM t
34186 32 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  2.**            
34187 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 20 20      |.**        
34188 20 20 20 20 20 20 20 20 60 2d 2d 2d 2d 2d 2d 3e          `------>
34189 20 20 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20    SELECT a FROM 
3418a 74 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 72  t1.**.** The arr
3418b 6f 77 73 20 69 6e 20 74 68 65 20 64 69 61 67 72  ows in the diagr
3418c 61 6d 20 61 62 6f 76 65 20 72 65 70 72 65 73 65  am above represe
3418d 6e 74 20 74 68 65 20 53 65 6c 65 63 74 2e 70 50  nt the Select.pP
3418e 72 69 6f 72 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  rior pointer..**
3418f 20 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74   So if this rout
34190 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 69  ine is called wi
34191 74 68 20 70 20 65 71 75 61 6c 20 74 6f 20 74 68  th p equal to th
34192 65 20 74 33 20 71 75 65 72 79 2c 20 74 68 65 6e  e t3 query, then
34193 0a 2a 2a 20 70 50 72 69 6f 72 20 77 69 6c 6c 20  .** pPrior will 
34194 62 65 20 74 68 65 20 74 32 20 71 75 65 72 79 2e  be the t2 query.
34195 20 20 70 2d 3e 6f 70 20 77 69 6c 6c 20 62 65 20    p->op will be 
34196 54 4b 5f 55 4e 49 4f 4e 20 69 6e 20 74 68 69 73  TK_UNION in this
34197 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74   case..**.** Not
34198 69 63 65 20 74 68 61 74 20 62 65 63 61 75 73 65  ice that because
34199 20 6f 66 20 74 68 65 20 77 61 79 20 53 51 4c 69   of the way SQLi
3419a 74 65 20 70 61 72 73 65 73 20 63 6f 6d 70 6f 75  te parses compou
3419b 6e 64 20 53 45 4c 45 43 54 73 2c 20 74 68 65 0a  nd SELECTs, the.
3419c 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 73 65  ** individual se
3419d 6c 65 63 74 73 20 61 6c 77 61 79 73 20 67 72 6f  lects always gro
3419e 75 70 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20  up from left to 
3419f 72 69 67 68 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  right..*/.static
341a0 20 69 6e 74 20 6d 75 6c 74 69 53 65 6c 65 63 74   int multiSelect
341a1 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
341a2 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
341a3 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
341a4 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20    Select *p,    
341a5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
341a6 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c  ight-most of SEL
341a7 45 43 54 73 20 74 6f 20 62 65 20 63 6f 64 65 64  ECTs to be coded
341a8 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
341a9 20 2a 70 44 65 73 74 20 20 20 20 20 2f 2a 20 57   *pDest     /* W
341aa 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 71  hat to do with q
341ab 75 65 72 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a  uery results */.
341ac 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
341ad 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 53 75  LITE_OK;   /* Su
341ae 63 63 65 73 73 20 63 6f 64 65 20 66 72 6f 6d 20  ccess code from 
341af 61 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  a subroutine */.
341b0 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72    Select *pPrior
341b1 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68  ;       /* Anoth
341b2 65 72 20 53 45 4c 45 43 54 20 69 6d 6d 65 64 69  er SELECT immedi
341b3 61 74 65 6c 79 20 74 6f 20 6f 75 72 20 6c 65 66  ately to our lef
341b4 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  t */.  Vdbe *v; 
341b5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
341b6 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
341b7 20 74 68 69 73 20 56 44 42 45 20 2a 2f 0a 20 20   this VDBE */.  
341b8 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b  SelectDest dest;
341b9 20 20 20 20 20 20 2f 2a 20 41 6c 74 65 72 6e 61        /* Alterna
341ba 74 69 76 65 20 64 61 74 61 20 64 65 73 74 69 6e  tive data destin
341bb 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63  ation */.  Selec
341bc 74 20 2a 70 44 65 6c 65 74 65 20 3d 20 30 3b 20  t *pDelete = 0; 
341bd 20 2f 2a 20 43 68 61 69 6e 20 6f 66 20 73 69 6d   /* Chain of sim
341be 70 6c 65 20 73 65 6c 65 63 74 73 20 74 6f 20 64  ple selects to d
341bf 65 6c 65 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74  elete */.  sqlit
341c0 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
341c1 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
341c2 6e 65 63 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64  nection */.#ifnd
341c3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45  ef SQLITE_OMIT_E
341c4 58 50 4c 41 49 4e 0a 20 20 69 6e 74 20 69 53 75  XPLAIN.  int iSu
341c5 62 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  b1;            /
341c6 2a 20 45 51 50 20 69 64 20 6f 66 20 6c 65 66 74  * EQP id of left
341c7 2d 68 61 6e 64 20 71 75 65 72 79 20 2a 2f 0a 20  -hand query */. 
341c8 20 69 6e 74 20 69 53 75 62 32 3b 20 20 20 20 20   int iSub2;     
341c9 20 20 20 20 20 20 20 2f 2a 20 45 51 50 20 69 64         /* EQP id
341ca 20 6f 66 20 72 69 67 68 74 2d 68 61 6e 64 20 71   of right-hand q
341cb 75 65 72 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a  uery */.#endif..
341cc 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
341cd 68 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52  here is no ORDER
341ce 20 42 59 20 6f 72 20 4c 49 4d 49 54 20 63 6c 61   BY or LIMIT cla
341cf 75 73 65 20 6f 6e 20 70 72 69 6f 72 20 53 45 4c  use on prior SEL
341d0 45 43 54 73 2e 20 20 4f 6e 6c 79 0a 20 20 2a 2a  ECTs.  Only.  **
341d1 20 74 68 65 20 6c 61 73 74 20 28 72 69 67 68 74   the last (right
341d2 2d 6d 6f 73 74 29 20 53 45 4c 45 43 54 20 69 6e  -most) SELECT in
341d3 20 74 68 65 20 73 65 72 69 65 73 20 6d 61 79 20   the series may 
341d4 68 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59  have an ORDER BY
341d5 20 6f 72 20 4c 49 4d 49 54 2e 0a 20 20 2a 2f 0a   or LIMIT..  */.
341d6 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70    assert( p && p
341d7 2d 3e 70 50 72 69 6f 72 20 29 3b 20 20 2f 2a 20  ->pPrior );  /* 
341d8 43 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  Calling function
341d9 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 69 73   guarantees this
341da 20 6d 75 63 68 20 2a 2f 0a 20 20 64 62 20 3d 20   much */.  db = 
341db 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50  pParse->db;.  pP
341dc 72 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72  rior = p->pPrior
341dd 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69  ;.  assert( pPri
341de 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 21 3d  or->pRightmost!=
341df 70 50 72 69 6f 72 20 29 3b 0a 20 20 61 73 73 65  pPrior );.  asse
341e0 72 74 28 20 70 50 72 69 6f 72 2d 3e 70 52 69 67  rt( pPrior->pRig
341e1 68 74 6d 6f 73 74 3d 3d 70 2d 3e 70 52 69 67 68  htmost==p->pRigh
341e2 74 6d 6f 73 74 20 29 3b 0a 20 20 64 65 73 74 20  tmost );.  dest 
341e3 3d 20 2a 70 44 65 73 74 3b 0a 20 20 69 66 28 20  = *pDest;.  if( 
341e4 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79  pPrior->pOrderBy
341e5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
341e6 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22  rrorMsg(pParse,"
341e7 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
341e8 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74 65  should come afte
341e9 72 20 25 73 20 6e 6f 74 20 62 65 66 6f 72 65 22  r %s not before"
341ea 2c 0a 20 20 20 20 20 20 73 65 6c 65 63 74 4f 70  ,.      selectOp
341eb 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20  Name(p->op));.  
341ec 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f    rc = 1;.    go
341ed 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f  to multi_select_
341ee 65 6e 64 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  end;.  }.  if( p
341ef 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 29 7b  Prior->pLimit ){
341f0 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
341f1 72 4d 73 67 28 70 50 61 72 73 65 2c 22 4c 49 4d  rMsg(pParse,"LIM
341f2 49 54 20 63 6c 61 75 73 65 20 73 68 6f 75 6c 64  IT clause should
341f3 20 63 6f 6d 65 20 61 66 74 65 72 20 25 73 20 6e   come after %s n
341f4 6f 74 20 62 65 66 6f 72 65 22 2c 0a 20 20 20 20  ot before",.    
341f5 20 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70    selectOpName(p
341f6 2d 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d  ->op));.    rc =
341f7 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c   1;.    goto mul
341f8 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  ti_select_end;. 
341f9 20 7d 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   }..  v = sqlite
341fa 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
341fb 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30  ;.  assert( v!=0
341fc 20 29 3b 20 20 2f 2a 20 54 68 65 20 56 44 42 45   );  /* The VDBE
341fd 20 61 6c 72 65 61 64 79 20 63 72 65 61 74 65 64   already created
341fe 20 62 79 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63   by calling func
341ff 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 43 72  tion */..  /* Cr
34200 65 61 74 65 20 74 68 65 20 64 65 73 74 69 6e 61  eate the destina
34201 74 69 6f 6e 20 74 65 6d 70 6f 72 61 72 79 20 74  tion temporary t
34202 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72  able if necessar
34203 79 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 65 73  y.  */.  if( des
34204 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68  t.eDest==SRT_Eph
34205 65 6d 54 61 62 20 29 7b 0a 20 20 20 20 61 73 73  emTab ){.    ass
34206 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29  ert( p->pEList )
34207 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
34208 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70  eAddOp2(v, OP_Op
34209 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 64 65 73  enEphemeral, des
3420a 74 2e 69 53 44 50 61 72 6d 2c 20 70 2d 3e 70 45  t.iSDParm, p->pE
3420b 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20  List->nExpr);.  
3420c 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
3420d 6e 67 65 50 35 28 76 2c 20 42 54 52 45 45 5f 55  ngeP5(v, BTREE_U
3420e 4e 4f 52 44 45 52 45 44 29 3b 0a 20 20 20 20 64  NORDERED);.    d
3420f 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f  est.eDest = SRT_
34210 54 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Table;.  }..  /*
34211 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 53   Make sure all S
34212 45 4c 45 43 54 73 20 69 6e 20 74 68 65 20 73 74  ELECTs in the st
34213 61 74 65 6d 65 6e 74 20 68 61 76 65 20 74 68 65  atement have the
34214 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20   same number of 
34215 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a 20 69 6e  elements.  ** in
34216 20 74 68 65 69 72 20 72 65 73 75 6c 74 20 73 65   their result se
34217 74 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ts..  */.  asser
34218 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 26 26 20  t( p->pEList && 
34219 70 50 72 69 6f 72 2d 3e 70 45 4c 69 73 74 20 29  pPrior->pEList )
3421a 3b 0a 20 20 69 66 28 20 70 2d 3e 70 45 4c 69 73  ;.  if( p->pELis
3421b 74 2d 3e 6e 45 78 70 72 21 3d 70 50 72 69 6f 72  t->nExpr!=pPrior
3421c 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  ->pEList->nExpr 
3421d 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 65  ){.    if( p->se
3421e 6c 46 6c 61 67 73 20 26 20 53 46 5f 56 61 6c 75  lFlags & SF_Valu
3421f 65 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  es ){.      sqli
34220 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
34221 73 65 2c 20 22 61 6c 6c 20 56 41 4c 55 45 53 20  se, "all VALUES 
34222 6d 75 73 74 20 68 61 76 65 20 74 68 65 20 73 61  must have the sa
34223 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72  me number of ter
34224 6d 73 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ms");.    }else{
34225 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
34226 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
34227 53 45 4c 45 43 54 73 20 74 6f 20 74 68 65 20 6c  SELECTs to the l
34228 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 6f 66  eft and right of
34229 20 25 73 22 0a 20 20 20 20 20 20 20 20 22 20 64   %s".        " d
3422a 6f 20 6e 6f 74 20 68 61 76 65 20 74 68 65 20 73  o not have the s
3422b 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  ame number of re
3422c 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 22 2c 20 73  sult columns", s
3422d 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f  electOpName(p->o
3422e 70 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  p));.    }.    r
3422f 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20  c = 1;.    goto 
34230 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
34231 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70  ;.  }..  /* Comp
34232 6f 75 6e 64 20 53 45 4c 45 43 54 73 20 74 68 61  ound SELECTs tha
34233 74 20 68 61 76 65 20 61 6e 20 4f 52 44 45 52 20  t have an ORDER 
34234 42 59 20 63 6c 61 75 73 65 20 61 72 65 20 68 61  BY clause are ha
34235 6e 64 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79  ndled separately
34236 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
34237 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
34238 72 65 74 75 72 6e 20 6d 75 6c 74 69 53 65 6c 65  return multiSele
34239 63 74 4f 72 64 65 72 42 79 28 70 50 61 72 73 65  ctOrderBy(pParse
3423a 2c 20 70 2c 20 70 44 65 73 74 29 3b 0a 20 20 7d  , p, pDest);.  }
3423b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
3423c 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65 66  code for the lef
3423d 74 20 61 6e 64 20 72 69 67 68 74 20 53 45 4c 45  t and right SELE
3423e 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20  CT statements.. 
3423f 20 2a 2f 0a 20 20 73 77 69 74 63 68 28 20 70 2d   */.  switch( p-
34240 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20  >op ){.    case 
34241 54 4b 5f 41 4c 4c 3a 20 7b 0a 20 20 20 20 20 20  TK_ALL: {.      
34242 69 6e 74 20 61 64 64 72 20 3d 20 30 3b 0a 20 20  int addr = 0;.  
34243 20 20 20 20 69 6e 74 20 6e 4c 69 6d 69 74 3b 0a      int nLimit;.
34244 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
34245 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 29 3b  Prior->pLimit );
34246 0a 20 20 20 20 20 20 70 50 72 69 6f 72 2d 3e 70  .      pPrior->p
34247 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69  Limit = p->pLimi
34248 74 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72 2d  t;.      pPrior-
34249 3e 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f  >pOffset = p->pO
3424a 66 66 73 65 74 3b 0a 20 20 20 20 20 20 65 78 70  ffset;.      exp
3424b 6c 61 69 6e 53 65 74 49 6e 74 65 67 65 72 28 69  lainSetInteger(i
3424c 53 75 62 31 2c 20 70 50 61 72 73 65 2d 3e 69 4e  Sub1, pParse->iN
3424d 65 78 74 53 65 6c 65 63 74 49 64 29 3b 0a 20 20  extSelectId);.  
3424e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
3424f 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
34250 50 72 69 6f 72 2c 20 26 64 65 73 74 29 3b 0a 20  Prior, &dest);. 
34251 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d       p->pLimit =
34252 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66   0;.      p->pOf
34253 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  fset = 0;.      
34254 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
34255 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c    goto multi_sel
34256 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d  ect_end;.      }
34257 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72  .      p->pPrior
34258 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 69   = 0;.      p->i
34259 4c 69 6d 69 74 20 3d 20 70 50 72 69 6f 72 2d 3e  Limit = pPrior->
3425a 69 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d  iLimit;.      p-
3425b 3e 69 4f 66 66 73 65 74 20 3d 20 70 50 72 69 6f  >iOffset = pPrio
3425c 72 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20  r->iOffset;.    
3425d 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20    if( p->iLimit 
3425e 29 7b 0a 20 20 20 20 20 20 20 20 61 64 64 72 20  ){.        addr 
3425f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
34260 4f 70 31 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f  Op1(v, OP_IfZero
34261 2c 20 70 2d 3e 69 4c 69 6d 69 74 29 3b 0a 20 20  , p->iLimit);.  
34262 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
34263 74 28 28 76 2c 20 22 4a 75 6d 70 20 61 68 65 61  t((v, "Jump ahea
34264 64 20 69 66 20 4c 49 4d 49 54 20 72 65 61 63 68  d if LIMIT reach
34265 65 64 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ed"));.      }. 
34266 20 20 20 20 20 65 78 70 6c 61 69 6e 53 65 74 49       explainSetI
34267 6e 74 65 67 65 72 28 69 53 75 62 32 2c 20 70 50  nteger(iSub2, pP
34268 61 72 73 65 2d 3e 69 4e 65 78 74 53 65 6c 65 63  arse->iNextSelec
34269 74 49 64 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  tId);.      rc =
3426a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
3426b 50 61 72 73 65 2c 20 70 2c 20 26 64 65 73 74 29  Parse, p, &dest)
3426c 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
3426d 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3426e 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74 65  );.      pDelete
3426f 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20   = p->pPrior;.  
34270 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20      p->pPrior = 
34271 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d  pPrior;.      p-
34272 3e 6e 53 65 6c 65 63 74 52 6f 77 20 2b 3d 20 70  >nSelectRow += p
34273 50 72 69 6f 72 2d 3e 6e 53 65 6c 65 63 74 52 6f  Prior->nSelectRo
34274 77 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 72  w;.      if( pPr
34275 69 6f 72 2d 3e 70 4c 69 6d 69 74 0a 20 20 20 20  ior->pLimit.    
34276 20 20 20 26 26 20 73 71 6c 69 74 65 33 45 78 70     && sqlite3Exp
34277 72 49 73 49 6e 74 65 67 65 72 28 70 50 72 69 6f  rIsInteger(pPrio
34278 72 2d 3e 70 4c 69 6d 69 74 2c 20 26 6e 4c 69 6d  r->pLimit, &nLim
34279 69 74 29 0a 20 20 20 20 20 20 20 26 26 20 70 2d  it).       && p-
3427a 3e 6e 53 65 6c 65 63 74 52 6f 77 20 3e 20 28 64  >nSelectRow > (d
3427b 6f 75 62 6c 65 29 6e 4c 69 6d 69 74 20 0a 20 20  ouble)nLimit .  
3427c 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 70      ){.        p
3427d 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 20 3d 20 28  ->nSelectRow = (
3427e 64 6f 75 62 6c 65 29 6e 4c 69 6d 69 74 3b 0a 20  double)nLimit;. 
3427f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
34280 20 61 64 64 72 20 29 7b 0a 20 20 20 20 20 20 20   addr ){.       
34281 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
34282 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20  Here(v, addr);. 
34283 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
34284 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
34285 73 65 20 54 4b 5f 45 58 43 45 50 54 3a 0a 20 20  se TK_EXCEPT:.  
34286 20 20 63 61 73 65 20 54 4b 5f 55 4e 49 4f 4e 3a    case TK_UNION:
34287 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 75 6e 69   {.      int uni
34288 6f 6e 54 61 62 3b 20 20 20 20 2f 2a 20 43 75 72  onTab;    /* Cur
34289 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  sor number of th
3428a 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
3428b 65 20 68 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74  e holding result
3428c 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 6f 70 20   */.      u8 op 
3428d 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6e  = 0;       /* On
3428e 65 20 6f 66 20 74 68 65 20 53 52 54 5f 20 6f 70  e of the SRT_ op
3428f 65 72 61 74 69 6f 6e 73 20 74 6f 20 61 70 70 6c  erations to appl
34290 79 20 74 6f 20 73 65 6c 66 20 2a 2f 0a 20 20 20  y to self */.   
34291 20 20 20 69 6e 74 20 70 72 69 6f 72 4f 70 3b 20     int priorOp; 
34292 20 20 20 20 2f 2a 20 54 68 65 20 53 52 54 5f 20      /* The SRT_ 
34293 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 70 70  operation to app
34294 6c 79 20 74 6f 20 70 72 69 6f 72 20 73 65 6c 65  ly to prior sele
34295 63 74 73 20 2a 2f 0a 20 20 20 20 20 20 45 78 70  cts */.      Exp
34296 72 20 2a 70 4c 69 6d 69 74 2c 20 2a 70 4f 66 66  r *pLimit, *pOff
34297 73 65 74 3b 20 2f 2a 20 53 61 76 65 64 20 76 61  set; /* Saved va
34298 6c 75 65 73 20 6f 66 20 70 2d 3e 6e 4c 69 6d 69  lues of p->nLimi
34299 74 20 61 6e 64 20 70 2d 3e 6e 4f 66 66 73 65 74  t and p->nOffset
3429a 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64   */.      int ad
3429b 64 72 3b 0a 20 20 20 20 20 20 53 65 6c 65 63 74  dr;.      Select
3429c 44 65 73 74 20 75 6e 69 6f 6e 64 65 73 74 3b 0a  Dest uniondest;.
3429d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3429e 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 43 45 50   p->op==TK_EXCEP
3429f 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
342a0 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55  ase( p->op==TK_U
342a1 4e 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 70 72  NION );.      pr
342a2 69 6f 72 4f 70 20 3d 20 53 52 54 5f 55 6e 69 6f  iorOp = SRT_Unio
342a3 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 64 65 73  n;.      if( des
342a4 74 2e 65 44 65 73 74 3d 3d 70 72 69 6f 72 4f 70  t.eDest==priorOp
342a5 20 26 26 20 41 4c 57 41 59 53 28 21 70 2d 3e 70   && ALWAYS(!p->p
342a6 4c 69 6d 69 74 20 26 26 21 70 2d 3e 70 4f 66 66  Limit &&!p->pOff
342a7 73 65 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  set) ){.        
342a8 2f 2a 20 57 65 20 63 61 6e 20 72 65 75 73 65 20  /* We can reuse 
342a9 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  a temporary tabl
342aa 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61  e generated by a
342ab 20 53 45 4c 45 43 54 20 74 6f 20 6f 75 72 0a 20   SELECT to our. 
342ac 20 20 20 20 20 20 20 2a 2a 20 72 69 67 68 74 2e         ** right.
342ad 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
342ae 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
342af 52 69 67 68 74 6d 6f 73 74 21 3d 70 20 29 3b 20  Rightmost!=p ); 
342b0 20 2f 2a 20 43 61 6e 20 6f 6e 6c 79 20 68 61 70   /* Can only hap
342b1 70 65 6e 20 66 6f 72 20 6c 65 66 74 77 61 72 64  pen for leftward
342b2 20 65 6c 65 6d 65 6e 74 73 0a 20 20 20 20 20 20   elements.      
342b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
342b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
342b5 2a 20 6f 66 20 61 20 33 2d 77 61 79 20 6f 72 20  * of a 3-way or 
342b6 6d 6f 72 65 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f  more compound */
342b7 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
342b8 20 70 2d 3e 70 4c 69 6d 69 74 3d 3d 30 20 29 3b   p->pLimit==0 );
342b9 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c        /* Not all
342ba 6f 77 65 64 20 6f 6e 20 6c 65 66 74 77 61 72 64  owed on leftward
342bb 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20   elements */.   
342bc 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
342bd 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20 20  pOffset==0 );   
342be 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65 64    /* Not allowed
342bf 20 6f 6e 20 6c 65 66 74 77 61 72 64 20 65 6c 65   on leftward ele
342c0 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20  ments */.       
342c1 20 75 6e 69 6f 6e 54 61 62 20 3d 20 64 65 73 74   unionTab = dest
342c2 2e 69 53 44 50 61 72 6d 3b 0a 20 20 20 20 20 20  .iSDParm;.      
342c3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
342c4 2a 20 57 65 20 77 69 6c 6c 20 6e 65 65 64 20 74  * We will need t
342c5 6f 20 63 72 65 61 74 65 20 6f 75 72 20 6f 77 6e  o create our own
342c6 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
342c7 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 20 20 20   to hold the.   
342c8 20 20 20 20 20 2a 2a 20 69 6e 74 65 72 6d 65 64       ** intermed
342c9 69 61 74 65 20 72 65 73 75 6c 74 73 2e 0a 20 20  iate results..  
342ca 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
342cb 20 75 6e 69 6f 6e 54 61 62 20 3d 20 70 50 61 72   unionTab = pPar
342cc 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20  se->nTab++;.    
342cd 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
342ce 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20  OrderBy==0 );.  
342cf 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
342d0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
342d1 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
342d2 61 6c 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30 29  al, unionTab, 0)
342d3 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
342d4 28 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  ( p->addrOpenEph
342d5 6d 5b 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20  m[0] == -1 );.  
342d6 20 20 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65        p->addrOpe
342d7 6e 45 70 68 6d 5b 30 5d 20 3d 20 61 64 64 72 3b  nEphm[0] = addr;
342d8 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 52 69 67  .        p->pRig
342d9 68 74 6d 6f 73 74 2d 3e 73 65 6c 46 6c 61 67 73  htmost->selFlags
342da 20 7c 3d 20 53 46 5f 55 73 65 73 45 70 68 65 6d   |= SF_UsesEphem
342db 65 72 61 6c 3b 0a 20 20 20 20 20 20 20 20 61 73  eral;.        as
342dc 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20  sert( p->pEList 
342dd 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
342de 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53 45    /* Code the SE
342df 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
342e0 74 6f 20 6f 75 72 20 6c 65 66 74 0a 20 20 20 20  to our left.    
342e1 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72    */.      asser
342e2 74 28 20 21 70 50 72 69 6f 72 2d 3e 70 4f 72 64  t( !pPrior->pOrd
342e3 65 72 42 79 20 29 3b 0a 20 20 20 20 20 20 73 71  erBy );.      sq
342e4 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
342e5 6e 69 74 28 26 75 6e 69 6f 6e 64 65 73 74 2c 20  nit(&uniondest, 
342e6 70 72 69 6f 72 4f 70 2c 20 75 6e 69 6f 6e 54 61  priorOp, unionTa
342e7 62 29 3b 0a 20 20 20 20 20 20 65 78 70 6c 61 69  b);.      explai
342e8 6e 53 65 74 49 6e 74 65 67 65 72 28 69 53 75 62  nSetInteger(iSub
342e9 31 2c 20 70 50 61 72 73 65 2d 3e 69 4e 65 78 74  1, pParse->iNext
342ea 53 65 6c 65 63 74 49 64 29 3b 0a 20 20 20 20 20  SelectId);.     
342eb 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c   rc = sqlite3Sel
342ec 65 63 74 28 70 50 61 72 73 65 2c 20 70 50 72 69  ect(pParse, pPri
342ed 6f 72 2c 20 26 75 6e 69 6f 6e 64 65 73 74 29 3b  or, &uniondest);
342ee 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
342ef 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75  .        goto mu
342f0 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
342f1 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
342f2 2a 20 43 6f 64 65 20 74 68 65 20 63 75 72 72 65  * Code the curre
342f3 6e 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  nt SELECT statem
342f4 65 6e 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ent.      */.   
342f5 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b     if( p->op==TK
342f6 5f 45 58 43 45 50 54 20 29 7b 0a 20 20 20 20 20  _EXCEPT ){.     
342f7 20 20 20 6f 70 20 3d 20 53 52 54 5f 45 78 63 65     op = SRT_Exce
342f8 70 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  pt;.      }else{
342f9 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
342fa 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e   p->op==TK_UNION
342fb 20 29 3b 0a 20 20 20 20 20 20 20 20 6f 70 20 3d   );.        op =
342fc 20 53 52 54 5f 55 6e 69 6f 6e 3b 0a 20 20 20 20   SRT_Union;.    
342fd 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 70 50 72    }.      p->pPr
342fe 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ior = 0;.      p
342ff 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69  Limit = p->pLimi
34300 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d  t;.      p->pLim
34301 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f  it = 0;.      pO
34302 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73  ffset = p->pOffs
34303 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66  et;.      p->pOf
34304 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  fset = 0;.      
34305 75 6e 69 6f 6e 64 65 73 74 2e 65 44 65 73 74 20  uniondest.eDest 
34306 3d 20 6f 70 3b 0a 20 20 20 20 20 20 65 78 70 6c  = op;.      expl
34307 61 69 6e 53 65 74 49 6e 74 65 67 65 72 28 69 53  ainSetInteger(iS
34308 75 62 32 2c 20 70 50 61 72 73 65 2d 3e 69 4e 65  ub2, pParse->iNe
34309 78 74 53 65 6c 65 63 74 49 64 29 3b 0a 20 20 20  xtSelectId);.   
3430a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53     rc = sqlite3S
3430b 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c  elect(pParse, p,
3430c 20 26 75 6e 69 6f 6e 64 65 73 74 29 3b 0a 20 20   &uniondest);.  
3430d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63      testcase( rc
3430e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  !=SQLITE_OK );. 
3430f 20 20 20 20 20 2f 2a 20 51 75 65 72 79 20 66 6c       /* Query fl
34310 61 74 74 65 6e 69 6e 67 20 69 6e 20 73 71 6c 69  attening in sqli
34311 74 65 33 53 65 6c 65 63 74 28 29 20 6d 69 67 68  te3Select() migh
34312 74 20 72 65 66 69 6c 6c 20 70 2d 3e 70 4f 72 64  t refill p->pOrd
34313 65 72 42 79 2e 0a 20 20 20 20 20 20 2a 2a 20 42  erBy..      ** B
34314 65 20 73 75 72 65 20 74 6f 20 64 65 6c 65 74 65  e sure to delete
34315 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 74 68   p->pOrderBy, th
34316 65 72 65 66 6f 72 65 2c 20 74 6f 20 61 76 6f 69  erefore, to avoi
34317 64 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 2e  d a memory leak.
34318 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
34319 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
3431a 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29  db, p->pOrderBy)
3431b 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74 65 20  ;.      pDelete 
3431c 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20  = p->pPrior;.   
3431d 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70     p->pPrior = p
3431e 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e  Prior;.      p->
3431f 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20  pOrderBy = 0;.  
34320 20 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54      if( p->op==T
34321 4b 5f 55 4e 49 4f 4e 20 29 20 70 2d 3e 6e 53 65  K_UNION ) p->nSe
34322 6c 65 63 74 52 6f 77 20 2b 3d 20 70 50 72 69 6f  lectRow += pPrio
34323 72 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 3b 0a 20  r->nSelectRow;. 
34324 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
34325 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c  Delete(db, p->pL
34326 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e  imit);.      p->
34327 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b  pLimit = pLimit;
34328 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66 73 65  .      p->pOffse
34329 74 20 3d 20 70 4f 66 66 73 65 74 3b 0a 20 20 20  t = pOffset;.   
3432a 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 30     p->iLimit = 0
3432b 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66 66 73  ;.      p->iOffs
3432c 65 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f  et = 0;..      /
3432d 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 64 61  * Convert the da
3432e 74 61 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72  ta in the tempor
3432f 61 72 79 20 74 61 62 6c 65 20 69 6e 74 6f 20 77  ary table into w
34330 68 61 74 65 76 65 72 20 66 6f 72 6d 0a 20 20 20  hatever form.   
34331 20 20 20 2a 2a 20 69 74 20 69 73 20 74 68 61 74     ** it is that
34332 20 77 65 20 63 75 72 72 65 6e 74 6c 79 20 6e 65   we currently ne
34333 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ed..      */.   
34334 20 20 20 61 73 73 65 72 74 28 20 75 6e 69 6f 6e     assert( union
34335 54 61 62 3d 3d 64 65 73 74 2e 69 53 44 50 61 72  Tab==dest.iSDPar
34336 6d 20 7c 7c 20 64 65 73 74 2e 65 44 65 73 74 21  m || dest.eDest!
34337 3d 70 72 69 6f 72 4f 70 20 29 3b 0a 20 20 20 20  =priorOp );.    
34338 20 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74    if( dest.eDest
34339 21 3d 70 72 69 6f 72 4f 70 20 29 7b 0a 20 20 20  !=priorOp ){.   
3433a 20 20 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20       int iCont, 
3433b 69 42 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a  iBreak, iStart;.
3433c 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
3433d 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20  p->pEList );.   
3433e 20 20 20 20 20 69 66 28 20 64 65 73 74 2e 65 44       if( dest.eD
3433f 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20  est==SRT_Output 
34340 29 7b 0a 20 20 20 20 20 20 20 20 20 20 53 65 6c  ){.          Sel
34341 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20 70 3b  ect *pFirst = p;
34342 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
34343 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  ( pFirst->pPrior
34344 20 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72   ) pFirst = pFir
34345 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20  st->pPrior;.    
34346 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f        generateCo
34347 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
34348 2c 20 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c  , 0, pFirst->pEL
34349 69 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ist);.        }.
3434a 20 20 20 20 20 20 20 20 69 42 72 65 61 6b 20 3d          iBreak =
3434b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
3434c 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20  Label(v);.      
3434d 20 20 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65    iCont = sqlite
3434e 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
3434f 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75  );.        compu
34350 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73  teLimitRegisters
34351 28 70 50 61 72 73 65 2c 20 70 2c 20 69 42 72 65  (pParse, p, iBre
34352 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ak);.        sql
34353 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
34354 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 75 6e 69  , OP_Rewind, uni
34355 6f 6e 54 61 62 2c 20 69 42 72 65 61 6b 29 3b 0a  onTab, iBreak);.
34356 20 20 20 20 20 20 20 20 69 53 74 61 72 74 20 3d          iStart =
34357 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
34358 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
34359 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c      selectInnerL
3435a 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70  oop(pParse, p, p
3435b 2d 3e 70 45 4c 69 73 74 2c 20 75 6e 69 6f 6e 54  ->pEList, unionT
3435c 61 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e  ab, p->pEList->n
3435d 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20  Expr,.          
3435e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
3435f 20 30 2c 20 26 64 65 73 74 2c 20 69 43 6f 6e 74   0, &dest, iCont
34360 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20  , iBreak);.     
34361 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
34362 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43  solveLabel(v, iC
34363 6f 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ont);.        sq
34364 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
34365 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 75 6e 69 6f  v, OP_Next, unio
34366 6e 54 61 62 2c 20 69 53 74 61 72 74 29 3b 0a 20  nTab, iStart);. 
34367 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
34368 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
34369 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20  , iBreak);.     
3436a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
3436b 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
3436c 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a  , unionTab, 0);.
3436d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
3436e 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
3436f 65 66 61 75 6c 74 3a 20 61 73 73 65 72 74 28 20  efault: assert( 
34370 70 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53  p->op==TK_INTERS
34371 45 43 54 20 29 3b 20 7b 0a 20 20 20 20 20 20 69  ECT ); {.      i
34372 6e 74 20 74 61 62 31 2c 20 74 61 62 32 3b 0a 20  nt tab1, tab2;. 
34373 20 20 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20       int iCont, 
34374 69 42 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a  iBreak, iStart;.
34375 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d        Expr *pLim
34376 69 74 2c 20 2a 70 4f 66 66 73 65 74 3b 0a 20 20  it, *pOffset;.  
34377 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20      int addr;.  
34378 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 69      SelectDest i
34379 6e 74 65 72 73 65 63 74 64 65 73 74 3b 0a 20 20  ntersectdest;.  
3437a 20 20 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 20      int r1;..   
3437b 20 20 20 2f 2a 20 49 4e 54 45 52 53 45 43 54 20     /* INTERSECT 
3437c 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  is different fro
3437d 6d 20 74 68 65 20 6f 74 68 65 72 73 20 73 69 6e  m the others sin
3437e 63 65 20 69 74 20 72 65 71 75 69 72 65 73 0a 20  ce it requires. 
3437f 20 20 20 20 20 2a 2a 20 74 77 6f 20 74 65 6d 70       ** two temp
34380 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 48  orary tables.  H
34381 65 6e 63 65 20 69 74 20 68 61 73 20 69 74 73 20  ence it has its 
34382 6f 77 6e 20 63 61 73 65 2e 20 20 42 65 67 69 6e  own case.  Begin
34383 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 61 6c 6c  .      ** by all
34384 6f 63 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c  ocating the tabl
34385 65 73 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 2e  es we will need.
34386 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
34387 74 61 62 31 20 3d 20 70 50 61 72 73 65 2d 3e 6e  tab1 = pParse->n
34388 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 74 61 62  Tab++;.      tab
34389 32 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  2 = pParse->nTab
3438a 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ++;.      assert
3438b 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30  ( p->pOrderBy==0
3438c 20 29 3b 0a 0a 20 20 20 20 20 20 61 64 64 72 20   );..      addr 
3438d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
3438e 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70  Op2(v, OP_OpenEp
3438f 68 65 6d 65 72 61 6c 2c 20 74 61 62 31 2c 20 30  hemeral, tab1, 0
34390 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
34391 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
34392 5b 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20  [0] == -1 );.   
34393 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70     p->addrOpenEp
34394 68 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20  hm[0] = addr;.  
34395 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73      p->pRightmos
34396 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53  t->selFlags |= S
34397 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b  F_UsesEphemeral;
34398 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
34399 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 0a 20 20 20  ->pEList );..   
3439a 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53     /* Code the S
3439b 45 4c 45 43 54 73 20 74 6f 20 6f 75 72 20 6c 65  ELECTs to our le
3439c 66 74 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72  ft into temporar
3439d 79 20 74 61 62 6c 65 20 22 74 61 62 31 22 2e 0a  y table "tab1"..
3439e 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
3439f 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
343a0 49 6e 69 74 28 26 69 6e 74 65 72 73 65 63 74 64  Init(&intersectd
343a1 65 73 74 2c 20 53 52 54 5f 55 6e 69 6f 6e 2c 20  est, SRT_Union, 
343a2 74 61 62 31 29 3b 0a 20 20 20 20 20 20 65 78 70  tab1);.      exp
343a3 6c 61 69 6e 53 65 74 49 6e 74 65 67 65 72 28 69  lainSetInteger(i
343a4 53 75 62 31 2c 20 70 50 61 72 73 65 2d 3e 69 4e  Sub1, pParse->iN
343a5 65 78 74 53 65 6c 65 63 74 49 64 29 3b 0a 20 20  extSelectId);.  
343a6 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
343a7 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
343a8 50 72 69 6f 72 2c 20 26 69 6e 74 65 72 73 65 63  Prior, &intersec
343a9 74 64 65 73 74 29 3b 0a 20 20 20 20 20 20 69 66  tdest);.      if
343aa 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
343ab 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63  goto multi_selec
343ac 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a  t_end;.      }..
343ad 20 20 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68        /* Code th
343ae 65 20 63 75 72 72 65 6e 74 20 53 45 4c 45 43 54  e current SELECT
343af 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72 79 20   into temporary 
343b0 74 61 62 6c 65 20 22 74 61 62 32 22 0a 20 20 20  table "tab2".   
343b1 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 64 64 72     */.      addr
343b2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
343b3 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp2(v, OP_OpenE
343b4 70 68 65 6d 65 72 61 6c 2c 20 74 61 62 32 2c 20  phemeral, tab2, 
343b5 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  0);.      assert
343b6 28 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  ( p->addrOpenEph
343b7 6d 5b 31 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20  m[1] == -1 );.  
343b8 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45      p->addrOpenE
343b9 70 68 6d 5b 31 5d 20 3d 20 61 64 64 72 3b 0a 20  phm[1] = addr;. 
343ba 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d       p->pPrior =
343bb 20 30 3b 0a 20 20 20 20 20 20 70 4c 69 6d 69 74   0;.      pLimit
343bc 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20   = p->pLimit;.  
343bd 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20      p->pLimit = 
343be 30 3b 0a 20 20 20 20 20 20 70 4f 66 66 73 65 74  0;.      pOffset
343bf 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20   = p->pOffset;. 
343c0 20 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20       p->pOffset 
343c1 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 65 72  = 0;.      inter
343c2 73 65 63 74 64 65 73 74 2e 69 53 44 50 61 72 6d  sectdest.iSDParm
343c3 20 3d 20 74 61 62 32 3b 0a 20 20 20 20 20 20 65   = tab2;.      e
343c4 78 70 6c 61 69 6e 53 65 74 49 6e 74 65 67 65 72  xplainSetInteger
343c5 28 69 53 75 62 32 2c 20 70 50 61 72 73 65 2d 3e  (iSub2, pParse->
343c6 69 4e 65 78 74 53 65 6c 65 63 74 49 64 29 3b 0a  iNextSelectId);.
343c7 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
343c8 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
343c9 20 70 2c 20 26 69 6e 74 65 72 73 65 63 74 64 65   p, &intersectde
343ca 73 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  st);.      testc
343cb 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ase( rc!=SQLITE_
343cc 4f 4b 20 29 3b 0a 20 20 20 20 20 20 70 44 65 6c  OK );.      pDel
343cd 65 74 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b  ete = p->pPrior;
343ce 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72  .      p->pPrior
343cf 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20   = pPrior;.     
343d0 20 69 66 28 20 70 2d 3e 6e 53 65 6c 65 63 74 52   if( p->nSelectR
343d1 6f 77 3e 70 50 72 69 6f 72 2d 3e 6e 53 65 6c 65  ow>pPrior->nSele
343d2 63 74 52 6f 77 20 29 20 70 2d 3e 6e 53 65 6c 65  ctRow ) p->nSele
343d3 63 74 52 6f 77 20 3d 20 70 50 72 69 6f 72 2d 3e  ctRow = pPrior->
343d4 6e 53 65 6c 65 63 74 52 6f 77 3b 0a 20 20 20 20  nSelectRow;.    
343d5 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
343d6 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69  ete(db, p->pLimi
343d7 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69  t);.      p->pLi
343d8 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20  mit = pLimit;.  
343d9 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d      p->pOffset =
343da 20 70 4f 66 66 73 65 74 3b 0a 0a 20 20 20 20 20   pOffset;..     
343db 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
343dc 65 20 74 6f 20 74 61 6b 65 20 74 68 65 20 69 6e  e to take the in
343dd 74 65 72 73 65 63 74 69 6f 6e 20 6f 66 20 74 68  tersection of th
343de 65 20 74 77 6f 20 74 65 6d 70 6f 72 61 72 79 0a  e two temporary.
343df 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 2e        ** tables.
343e0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
343e1 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73  assert( p->pELis
343e2 74 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64  t );.      if( d
343e3 65 73 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f  est.eDest==SRT_O
343e4 75 74 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20  utput ){.       
343e5 20 53 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20   Select *pFirst 
343e6 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 77 68 69  = p;.        whi
343e7 6c 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69  le( pFirst->pPri
343e8 6f 72 20 29 20 70 46 69 72 73 74 20 3d 20 70 46  or ) pFirst = pF
343e9 69 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20  irst->pPrior;.  
343ea 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f        generateCo
343eb 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
343ec 2c 20 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c  , 0, pFirst->pEL
343ed 69 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ist);.      }.  
343ee 20 20 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c      iBreak = sql
343ef 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
343f0 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 43 6f 6e  l(v);.      iCon
343f1 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  t = sqlite3VdbeM
343f2 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
343f3 20 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52     computeLimitR
343f4 65 67 69 73 74 65 72 73 28 70 50 61 72 73 65 2c  egisters(pParse,
343f5 20 70 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20   p, iBreak);.   
343f6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
343f7 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
343f8 64 2c 20 74 61 62 31 2c 20 69 42 72 65 61 6b 29  d, tab1, iBreak)
343f9 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c  ;.      r1 = sql
343fa 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
343fb 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 53  Parse);.      iS
343fc 74 61 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64  tart = sqlite3Vd
343fd 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
343fe 6f 77 4b 65 79 2c 20 74 61 62 31 2c 20 72 31 29  owKey, tab1, r1)
343ff 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
34400 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20  dbeAddOp4Int(v, 
34401 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 74 61 62  OP_NotFound, tab
34402 32 2c 20 69 43 6f 6e 74 2c 20 72 31 2c 20 30 29  2, iCont, r1, 0)
34403 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
34404 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
34405 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20  arse, r1);.     
34406 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70   selectInnerLoop
34407 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70  (pParse, p, p->p
34408 45 4c 69 73 74 2c 20 74 61 62 31 2c 20 70 2d 3e  EList, tab1, p->
34409 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 0a 20  pEList->nExpr,. 
3440a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3440b 20 20 20 20 20 30 2c 20 30 2c 20 26 64 65 73 74       0, 0, &dest
3440c 2c 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29  , iCont, iBreak)
3440d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
3440e 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
3440f 76 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20  v, iCont);.     
34410 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
34411 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 74  p2(v, OP_Next, t
34412 61 62 31 2c 20 69 53 74 61 72 74 29 3b 0a 20 20  ab1, iStart);.  
34413 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
34414 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69  esolveLabel(v, i
34415 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 73 71  Break);.      sq
34416 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
34417 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62  v, OP_Close, tab
34418 32 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  2, 0);.      sql
34419 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
3441a 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 31  , OP_Close, tab1
3441b 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61  , 0);.      brea
3441c 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  k;.    }.  }..  
3441d 65 78 70 6c 61 69 6e 43 6f 6d 70 6f 73 69 74 65  explainComposite
3441e 28 70 50 61 72 73 65 2c 20 70 2d 3e 6f 70 2c 20  (pParse, p->op, 
3441f 69 53 75 62 31 2c 20 69 53 75 62 32 2c 20 70 2d  iSub1, iSub2, p-
34420 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29 3b 0a 0a 20  >op!=TK_ALL);.. 
34421 20 2f 2a 20 43 6f 6d 70 75 74 65 20 63 6f 6c 6c   /* Compute coll
34422 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20  ating sequences 
34423 75 73 65 64 20 62 79 20 0a 20 20 2a 2a 20 74 65  used by .  ** te
34424 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 6e  mporary tables n
34425 65 65 64 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65  eeded to impleme
34426 6e 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  nt the compound 
34427 73 65 6c 65 63 74 2e 0a 20 20 2a 2a 20 41 74 74  select..  ** Att
34428 61 63 68 20 74 68 65 20 4b 65 79 49 6e 66 6f 20  ach the KeyInfo 
34429 73 74 72 75 63 74 75 72 65 20 74 6f 20 61 6c 6c  structure to all
3442a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
3442b 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  s..  **.  ** Thi
3442c 73 20 73 65 63 74 69 6f 6e 20 69 73 20 72 75 6e  s section is run
3442d 20 62 79 20 74 68 65 20 72 69 67 68 74 2d 6d 6f   by the right-mo
3442e 73 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  st SELECT statem
3442f 65 6e 74 20 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53  ent only..  ** S
34430 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
34431 20 74 6f 20 74 68 65 20 6c 65 66 74 20 61 6c 77   to the left alw
34432 61 79 73 20 73 6b 69 70 20 74 68 69 73 20 70 61  ays skip this pa
34433 72 74 2e 20 20 54 68 65 20 72 69 67 68 74 2d 6d  rt.  The right-m
34434 6f 73 74 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20  ost.  ** SELECT 
34435 6d 69 67 68 74 20 61 6c 73 6f 20 73 6b 69 70 20  might also skip 
34436 74 68 69 73 20 70 61 72 74 20 69 66 20 69 74 20  this part if it 
34437 68 61 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20  has no ORDER BY 
34438 63 6c 61 75 73 65 20 61 6e 64 0a 20 20 2a 2a 20  clause and.  ** 
34439 6e 6f 20 74 65 6d 70 20 74 61 62 6c 65 73 20 61  no temp tables a
3443a 72 65 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a  re required..  *
3443b 2f 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c  /.  if( p->selFl
3443c 61 67 73 20 26 20 53 46 5f 55 73 65 73 45 70 68  ags & SF_UsesEph
3443d 65 6d 65 72 61 6c 20 29 7b 0a 20 20 20 20 69 6e  emeral ){.    in
3443e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
3443f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
34440 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
34441 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
34442 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20  Info;           
34443 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65   /* Collating se
34444 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 72  quence for the r
34445 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20  esult set */.   
34446 20 53 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 3b 20   Select *pLoop; 
34447 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
34448 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68  * For looping th
34449 72 6f 75 67 68 20 53 45 4c 45 43 54 20 73 74 61  rough SELECT sta
3444a 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 43  tements */.    C
3444b 6f 6c 6c 53 65 71 20 2a 2a 61 70 43 6f 6c 6c 3b  ollSeq **apColl;
3444c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3444d 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f  For looping thro
3444e 75 67 68 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43  ugh pKeyInfo->aC
3444f 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 20 20 69 6e 74  oll[] */.    int
34450 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   nCol;          
34451 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
34452 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
34453 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  in result set */
34454 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ..    assert( p-
34455 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d 70 20 29  >pRightmost==p )
34456 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 70 2d 3e  ;.    nCol = p->
34457 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20  pEList->nExpr;. 
34458 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71     pKeyInfo = sq
34459 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
3445a 6f 28 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20  o(db,.          
3445b 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a               siz
3445c 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 2b 6e  eof(*pKeyInfo)+n
3445d 43 6f 6c 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c  Col*(sizeof(Coll
3445e 53 65 71 2a 29 20 2b 20 31 29 29 3b 0a 20 20 20  Seq*) + 1));.   
3445f 20 69 66 28 20 21 70 4b 65 79 49 6e 66 6f 20 29   if( !pKeyInfo )
34460 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
34461 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
34462 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
34463 63 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20  ct_end;.    }.. 
34464 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63     pKeyInfo->enc
34465 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20   = ENC(db);.    
34466 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
34467 20 3d 20 28 75 31 36 29 6e 43 6f 6c 3b 0a 0a 20   = (u16)nCol;.. 
34468 20 20 20 66 6f 72 28 69 3d 30 2c 20 61 70 43 6f     for(i=0, apCo
34469 6c 6c 3d 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f  ll=pKeyInfo->aCo
3446a 6c 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c  ll; i<nCol; i++,
3446b 20 61 70 43 6f 6c 6c 2b 2b 29 7b 0a 20 20 20 20   apColl++){.    
3446c 20 20 2a 61 70 43 6f 6c 6c 20 3d 20 6d 75 6c 74    *apColl = mult
3446d 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70  iSelectCollSeq(p
3446e 50 61 72 73 65 2c 20 70 2c 20 69 29 3b 0a 20 20  Parse, p, i);.  
3446f 20 20 20 20 69 66 28 20 30 3d 3d 2a 61 70 43 6f      if( 0==*apCo
34470 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 61  ll ){.        *a
34471 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c  pColl = db->pDfl
34472 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  tColl;.      }. 
34473 20 20 20 7d 0a 20 20 20 20 70 4b 65 79 49 6e 66     }.    pKeyInf
34474 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20  o->aSortOrder = 
34475 28 75 38 2a 29 61 70 43 6f 6c 6c 3b 0a 0a 20 20  (u8*)apColl;..  
34476 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b 20 70    for(pLoop=p; p
34477 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f  Loop; pLoop=pLoo
34478 70 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20 20  p->pPrior){.    
34479 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 3b 20    for(i=0; i<2; 
3447a 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  i++){.        in
3447b 74 20 61 64 64 72 20 3d 20 70 4c 6f 6f 70 2d 3e  t addr = pLoop->
3447c 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 3b  addrOpenEphm[i];
3447d 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 64 64  .        if( add
3447e 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  r<0 ){.         
3447f 20 2f 2a 20 49 66 20 5b 30 5d 20 69 73 20 75 6e   /* If [0] is un
34480 75 73 65 64 20 74 68 65 6e 20 5b 31 5d 20 69 73  used then [1] is
34481 20 61 6c 73 6f 20 75 6e 75 73 65 64 2e 20 20 53   also unused.  S
34482 6f 20 77 65 20 63 61 6e 0a 20 20 20 20 20 20 20  o we can.       
34483 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 73 61 66     ** always saf
34484 65 6c 79 20 61 62 6f 72 74 20 61 73 20 73 6f 6f  ely abort as soo
34485 6e 20 61 73 20 74 68 65 20 66 69 72 73 74 20 75  n as the first u
34486 6e 75 73 65 64 20 73 6c 6f 74 20 69 73 20 66 6f  nused slot is fo
34487 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  und */.         
34488 20 61 73 73 65 72 74 28 20 70 4c 6f 6f 70 2d 3e   assert( pLoop->
34489 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 3c  addrOpenEphm[1]<
3448a 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62  0 );.          b
3448b 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
3448c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
3448d 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61  dbeChangeP2(v, a
3448e 64 64 72 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20  ddr, nCol);.    
3448f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
34490 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c  hangeP4(v, addr,
34491 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f   (char*)pKeyInfo
34492 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20  , P4_KEYINFO);. 
34493 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 61 64         pLoop->ad
34494 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 20 3d 20  drOpenEphm[i] = 
34495 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  -1;.      }.    
34496 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
34497 72 65 65 28 64 62 2c 20 70 4b 65 79 49 6e 66 6f  ree(db, pKeyInfo
34498 29 3b 0a 20 20 7d 0a 0a 6d 75 6c 74 69 5f 73 65  );.  }..multi_se
34499 6c 65 63 74 5f 65 6e 64 3a 0a 20 20 70 44 65 73  lect_end:.  pDes
3449a 74 2d 3e 69 53 64 73 74 20 3d 20 64 65 73 74 2e  t->iSdst = dest.
3449b 69 53 64 73 74 3b 0a 20 20 70 44 65 73 74 2d 3e  iSdst;.  pDest->
3449c 6e 53 64 73 74 20 3d 20 64 65 73 74 2e 6e 53 64  nSdst = dest.nSd
3449d 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  st;.  sqlite3Sel
3449e 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44  ectDelete(db, pD
3449f 65 6c 65 74 65 29 3b 0a 20 20 72 65 74 75 72 6e  elete);.  return
344a0 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a   rc;.}.#endif /*
344a1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
344a2 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a  POUND_SELECT */.
344a3 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 6f  ./*.** Code an o
344a4 75 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e 65  utput subroutine
344a5 20 66 6f 72 20 61 20 63 6f 72 6f 75 74 69 6e 65   for a coroutine
344a6 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
344a7 6f 66 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20 73  of a.** SELECT s
344a8 74 61 74 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  tatment..**.** T
344a9 68 65 20 64 61 74 61 20 74 6f 20 62 65 20 6f 75  he data to be ou
344aa 74 70 75 74 20 69 73 20 63 6f 6e 74 61 69 6e 65  tput is containe
344ab 64 20 69 6e 20 70 49 6e 2d 3e 69 53 64 73 74 2e  d in pIn->iSdst.
344ac 20 20 54 68 65 72 65 20 61 72 65 0a 2a 2a 20 70    There are.** p
344ad 49 6e 2d 3e 6e 53 64 73 74 20 63 6f 6c 75 6d 6e  In->nSdst column
344ae 73 20 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 20  s to be output. 
344af 20 70 44 65 73 74 20 69 73 20 77 68 65 72 65 20   pDest is where 
344b0 74 68 65 20 6f 75 74 70 75 74 20 73 68 6f 75 6c  the output shoul
344b1 64 0a 2a 2a 20 62 65 20 73 65 6e 74 2e 0a 2a 2a  d.** be sent..**
344b2 0a 2a 2a 20 72 65 67 52 65 74 75 72 6e 20 69 73  .** regReturn is
344b3 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
344b4 68 65 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64  he register hold
344b5 69 6e 67 20 74 68 65 20 73 75 62 72 6f 75 74 69  ing the subrouti
344b6 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 61 64 64  ne.** return add
344b7 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72  ress..**.** If r
344b8 65 67 50 72 65 76 3e 30 20 74 68 65 6e 20 69 74  egPrev>0 then it
344b9 20 69 73 20 74 68 65 20 66 69 72 73 74 20 72 65   is the first re
344ba 67 69 73 74 65 72 20 69 6e 20 61 20 76 65 63 74  gister in a vect
344bb 6f 72 20 74 68 61 74 0a 2a 2a 20 72 65 63 6f 72  or that.** recor
344bc 64 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  ds the previous 
344bd 6f 75 74 70 75 74 2e 20 20 6d 65 6d 5b 72 65 67  output.  mem[reg
344be 50 72 65 76 5d 20 69 73 20 61 20 66 6c 61 67 20  Prev] is a flag 
344bf 74 68 61 74 20 69 73 20 66 61 6c 73 65 0a 2a 2a  that is false.**
344c0 20 69 66 20 74 68 65 72 65 20 68 61 73 20 62 65   if there has be
344c1 65 6e 20 6e 6f 20 70 72 65 76 69 6f 75 73 20 6f  en no previous o
344c2 75 74 70 75 74 2e 20 20 49 66 20 72 65 67 50 72  utput.  If regPr
344c3 65 76 3e 30 20 74 68 65 6e 20 63 6f 64 65 20 69  ev>0 then code i
344c4 73 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 74  s.** generated t
344c5 6f 20 73 75 70 70 72 65 73 73 20 64 75 70 6c 69  o suppress dupli
344c6 63 61 74 65 73 2e 20 20 70 4b 65 79 49 6e 66 6f  cates.  pKeyInfo
344c7 20 69 73 20 75 73 65 64 20 66 6f 72 20 63 6f 6d   is used for com
344c8 70 61 72 69 6e 67 0a 2a 2a 20 6b 65 79 73 2e 0a  paring.** keys..
344c9 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4c 49 4d  **.** If the LIM
344ca 49 54 20 66 6f 75 6e 64 20 69 6e 20 70 2d 3e 69  IT found in p->i
344cb 4c 69 6d 69 74 20 69 73 20 72 65 61 63 68 65 64  Limit is reached
344cc 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65  , jump immediate
344cd 6c 79 20 74 6f 0a 2a 2a 20 69 42 72 65 61 6b 2e  ly to.** iBreak.
344ce 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
344cf 65 6e 65 72 61 74 65 4f 75 74 70 75 74 53 75 62  enerateOutputSub
344d0 72 6f 75 74 69 6e 65 28 0a 20 20 50 61 72 73 65  routine(.  Parse
344d1 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
344d2 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
344d3 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63  ntext */.  Selec
344d4 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  t *p,           
344d5 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54     /* The SELECT
344d6 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
344d7 53 65 6c 65 63 74 44 65 73 74 20 2a 70 49 6e 2c  SelectDest *pIn,
344d8 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 72 6f 75          /* Corou
344d9 74 69 6e 65 20 73 75 70 70 6c 79 69 6e 67 20 64  tine supplying d
344da 61 74 61 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  ata */.  SelectD
344db 65 73 74 20 2a 70 44 65 73 74 2c 20 20 20 20 20  est *pDest,     
344dc 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 73 65 6e   /* Where to sen
344dd 64 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20  d the data */.  
344de 69 6e 74 20 72 65 67 52 65 74 75 72 6e 2c 20 20  int regReturn,  
344df 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
344e0 65 74 75 72 6e 20 61 64 64 72 65 73 73 20 72 65  eturn address re
344e1 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  gister */.  int 
344e2 72 65 67 50 72 65 76 2c 20 20 20 20 20 20 20 20  regPrev,        
344e3 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20      /* Previous 
344e4 72 65 73 75 6c 74 20 72 65 67 69 73 74 65 72 2e  result register.
344e5 20 20 4e 6f 20 75 6e 69 71 75 65 6e 65 73 73 20    No uniqueness 
344e6 69 66 20 30 20 2a 2f 0a 20 20 4b 65 79 49 6e 66  if 0 */.  KeyInf
344e7 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20  o *pKeyInfo,    
344e8 20 20 2f 2a 20 46 6f 72 20 63 6f 6d 70 61 72 69    /* For compari
344e9 6e 67 20 77 69 74 68 20 70 72 65 76 69 6f 75 73  ng with previous
344ea 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20   entry */.  int 
344eb 70 34 74 79 70 65 2c 20 20 20 20 20 20 20 20 20  p4type,         
344ec 20 20 20 20 2f 2a 20 54 68 65 20 70 34 20 74 79      /* The p4 ty
344ed 70 65 20 66 6f 72 20 70 4b 65 79 49 6e 66 6f 20  pe for pKeyInfo 
344ee 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 20  */.  int iBreak 
344ef 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
344f0 4a 75 6d 70 20 68 65 72 65 20 69 66 20 77 65 20  Jump here if we 
344f1 68 69 74 20 74 68 65 20 4c 49 4d 49 54 20 2a 2f  hit the LIMIT */
344f2 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  .){.  Vdbe *v = 
344f3 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
344f4 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 3b 0a   int iContinue;.
344f5 20 20 69 6e 74 20 61 64 64 72 3b 0a 0a 20 20 61    int addr;..  a
344f6 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
344f7 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
344f8 0a 20 20 69 43 6f 6e 74 69 6e 75 65 20 3d 20 73  .  iContinue = s
344f9 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
344fa 62 65 6c 28 76 29 3b 0a 0a 20 20 2f 2a 20 53 75  bel(v);..  /* Su
344fb 70 70 72 65 73 73 20 64 75 70 6c 69 63 61 74 65  ppress duplicate
344fc 73 20 66 6f 72 20 55 4e 49 4f 4e 2c 20 45 58 43  s for UNION, EXC
344fd 45 50 54 2c 20 61 6e 64 20 49 4e 54 45 52 53 45  EPT, and INTERSE
344fe 43 54 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72  CT .  */.  if( r
344ff 65 67 50 72 65 76 20 29 7b 0a 20 20 20 20 69 6e  egPrev ){.    in
34500 74 20 6a 31 2c 20 6a 32 3b 0a 20 20 20 20 6a 31  t j1, j2;.    j1
34501 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
34502 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 4e 6f 74  dOp1(v, OP_IfNot
34503 2c 20 72 65 67 50 72 65 76 29 3b 0a 20 20 20 20  , regPrev);.    
34504 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j2 = sqlite3Vdbe
34505 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d  AddOp4(v, OP_Com
34506 70 61 72 65 2c 20 70 49 6e 2d 3e 69 53 64 73 74  pare, pIn->iSdst
34507 2c 20 72 65 67 50 72 65 76 2b 31 2c 20 70 49 6e  , regPrev+1, pIn
34508 2d 3e 6e 53 64 73 74 2c 0a 20 20 20 20 20 20 20  ->nSdst,.       
34509 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3450a 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
3450b 65 79 49 6e 66 6f 2c 20 70 34 74 79 70 65 29 3b  eyInfo, p4type);
3450c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
3450d 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4a 75 6d  AddOp3(v, OP_Jum
3450e 70 2c 20 6a 32 2b 32 2c 20 69 43 6f 6e 74 69 6e  p, j2+2, iContin
3450f 75 65 2c 20 6a 32 2b 32 29 3b 0a 20 20 20 20 73  ue, j2+2);.    s
34510 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
34511 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 73  re(v, j1);.    s
34512 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
34513 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 70 49 6e  (v, OP_Copy, pIn
34514 2d 3e 69 53 64 73 74 2c 20 72 65 67 50 72 65 76  ->iSdst, regPrev
34515 2b 31 2c 20 70 49 6e 2d 3e 6e 53 64 73 74 2d 31  +1, pIn->nSdst-1
34516 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
34517 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
34518 6e 74 65 67 65 72 2c 20 31 2c 20 72 65 67 50 72  nteger, 1, regPr
34519 65 76 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ev);.  }.  if( p
3451a 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
3451b 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
3451c 20 30 3b 0a 0a 20 20 2f 2a 20 53 75 70 70 72 65   0;..  /* Suppre
3451d 73 73 20 74 68 65 20 66 69 72 73 74 20 4f 46 46  ss the first OFF
3451e 53 45 54 20 65 6e 74 72 69 65 73 20 69 66 20 74  SET entries if t
3451f 68 65 72 65 20 69 73 20 61 6e 20 4f 46 46 53 45  here is an OFFSE
34520 54 20 63 6c 61 75 73 65 0a 20 20 2a 2f 0a 20 20  T clause.  */.  
34521 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c  codeOffset(v, p,
34522 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 0a 20 20   iContinue);..  
34523 73 77 69 74 63 68 28 20 70 44 65 73 74 2d 3e 65  switch( pDest->e
34524 44 65 73 74 20 29 7b 0a 20 20 20 20 2f 2a 20 53  Dest ){.    /* S
34525 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
34526 61 73 20 64 61 74 61 20 75 73 69 6e 67 20 61 20  as data using a 
34527 75 6e 69 71 75 65 20 6b 65 79 2e 0a 20 20 20 20  unique key..    
34528 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
34529 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20  Table:.    case 
3452a 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a  SRT_EphemTab: {.
3452b 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73        int r1 = s
3452c 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
3452d 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
3452e 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33  int r2 = sqlite3
3452f 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
34530 65 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  e);.      testca
34531 73 65 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74  se( pDest->eDest
34532 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29 3b 0a 20  ==SRT_Table );. 
34533 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
34534 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54  Dest->eDest==SRT
34535 5f 45 70 68 65 6d 54 61 62 20 29 3b 0a 20 20 20  _EphemTab );.   
34536 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
34537 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  dOp3(v, OP_MakeR
34538 65 63 6f 72 64 2c 20 70 49 6e 2d 3e 69 53 64 73  ecord, pIn->iSds
34539 74 2c 20 70 49 6e 2d 3e 6e 53 64 73 74 2c 20 72  t, pIn->nSdst, r
3453a 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
3453b 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
3453c 50 5f 4e 65 77 52 6f 77 69 64 2c 20 70 44 65 73  P_NewRowid, pDes
3453d 74 2d 3e 69 53 44 50 61 72 6d 2c 20 72 32 29 3b  t->iSDParm, r2);
3453e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
3453f 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
34540 6e 73 65 72 74 2c 20 70 44 65 73 74 2d 3e 69 53  nsert, pDest->iS
34541 44 50 61 72 6d 2c 20 72 31 2c 20 72 32 29 3b 0a  DParm, r1, r2);.
34542 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
34543 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46  eChangeP5(v, OPF
34544 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20  LAG_APPEND);.   
34545 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
34546 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
34547 20 72 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   r2);.      sqli
34548 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
34549 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20  g(pParse, r1);. 
3454a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3454b 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
3454c 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
3454d 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65      /* If we are
3454e 20 63 72 65 61 74 69 6e 67 20 61 20 73 65 74 20   creating a set 
3454f 66 6f 72 20 61 6e 20 22 65 78 70 72 20 49 4e 20  for an "expr IN 
34550 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 63 6f  (SELECT ...)" co
34551 6e 73 74 72 75 63 74 2c 0a 20 20 20 20 2a 2a 20  nstruct,.    ** 
34552 74 68 65 6e 20 74 68 65 72 65 20 73 68 6f 75 6c  then there shoul
34553 64 20 62 65 20 61 20 73 69 6e 67 6c 65 20 69 74  d be a single it
34554 65 6d 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e  em on the stack.
34555 20 20 57 72 69 74 65 20 74 68 69 73 0a 20 20 20    Write this.   
34556 20 2a 2a 20 69 74 65 6d 20 69 6e 74 6f 20 74 68   ** item into th
34557 65 20 73 65 74 20 74 61 62 6c 65 20 77 69 74 68  e set table with
34558 20 62 6f 67 75 73 20 64 61 74 61 2e 0a 20 20 20   bogus data..   
34559 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54   */.    case SRT
3455a 5f 53 65 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e  _Set: {.      in
3455b 74 20 72 31 3b 0a 20 20 20 20 20 20 61 73 73 65  t r1;.      asse
3455c 72 74 28 20 70 49 6e 2d 3e 6e 53 64 73 74 3d 3d  rt( pIn->nSdst==
3455d 31 20 29 3b 0a 20 20 20 20 20 20 70 44 65 73 74  1 );.      pDest
3455e 2d 3e 61 66 66 53 64 73 74 20 3d 20 0a 20 20 20  ->affSdst = .   
3455f 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d        sqlite3Com
34560 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 2d 3e  pareAffinity(p->
34561 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
34562 70 72 2c 20 70 44 65 73 74 2d 3e 61 66 66 53 64  pr, pDest->affSd
34563 73 74 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20  st);.      r1 = 
34564 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
34565 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
34566 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
34567 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p4(v, OP_MakeRec
34568 6f 72 64 2c 20 70 49 6e 2d 3e 69 53 64 73 74 2c  ord, pIn->iSdst,
34569 20 31 2c 20 72 31 2c 20 26 70 44 65 73 74 2d 3e   1, r1, &pDest->
3456a 61 66 66 53 64 73 74 2c 31 29 3b 0a 20 20 20 20  affSdst,1);.    
3456b 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
3456c 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
3456d 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 53  (pParse, pIn->iS
3456e 64 73 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  dst, 1);.      s
3456f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
34570 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
34571 2c 20 70 44 65 73 74 2d 3e 69 53 44 50 61 72 6d  , pDest->iSDParm
34572 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r1);.      sql
34573 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
34574 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a  eg(pParse, r1);.
34575 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
34576 20 7d 0a 0a 23 69 66 20 30 20 20 2f 2a 20 4e 65   }..#if 0  /* Ne
34577 76 65 72 20 6f 63 63 75 72 73 20 6f 6e 20 61 6e  ver occurs on an
34578 20 4f 52 44 45 52 20 42 59 20 71 75 65 72 79 20   ORDER BY query 
34579 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 79  */.    /* If any
3457a 20 72 6f 77 20 65 78 69 73 74 20 69 6e 20 74 68   row exist in th
3457b 65 20 72 65 73 75 6c 74 20 73 65 74 2c 20 72 65  e result set, re
3457c 63 6f 72 64 20 74 68 61 74 20 66 61 63 74 20 61  cord that fact a
3457d 6e 64 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a 2f  nd abort..    */
3457e 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 78  .    case SRT_Ex
3457f 69 73 74 73 3a 20 7b 0a 20 20 20 20 20 20 73 71  ists: {.      sq
34580 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
34581 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31  v, OP_Integer, 1
34582 2c 20 70 44 65 73 74 2d 3e 69 53 44 50 61 72 6d  , pDest->iSDParm
34583 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  );.      /* The 
34584 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c  LIMIT clause wil
34585 6c 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20  l terminate the 
34586 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20  loop for us */. 
34587 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
34588 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  }.#endif..    /*
34589 20 49 66 20 74 68 69 73 20 69 73 20 61 20 73 63   If this is a sc
3458a 61 6c 61 72 20 73 65 6c 65 63 74 20 74 68 61 74  alar select that
3458b 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65   is part of an e
3458c 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a  xpression, then.
3458d 20 20 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65      ** store the
3458e 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20   results in the 
3458f 61 70 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f  appropriate memo
34590 72 79 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61  ry cell and brea
34591 6b 20 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20  k out.    ** of 
34592 74 68 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20  the scan loop.. 
34593 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
34594 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20  RT_Mem: {.      
34595 61 73 73 65 72 74 28 20 70 49 6e 2d 3e 6e 53 64  assert( pIn->nSd
34596 73 74 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73  st==1 );.      s
34597 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f  qlite3ExprCodeMo
34598 76 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e  ve(pParse, pIn->
34599 69 53 64 73 74 2c 20 70 44 65 73 74 2d 3e 69 53  iSdst, pDest->iS
3459a 44 50 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20 20  DParm, 1);.     
3459b 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c   /* The LIMIT cl
3459c 61 75 73 65 20 77 69 6c 6c 20 6a 75 6d 70 20 6f  ause will jump o
3459d 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 66  ut of the loop f
3459e 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62  or us */.      b
3459f 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
345a0 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51  if /* #ifndef SQ
345a1 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
345a2 52 59 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 54 68  RY */..    /* Th
345a3 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 73 74  e results are st
345a4 6f 72 65 64 20 69 6e 20 61 20 73 65 71 75 65 6e  ored in a sequen
345a5 63 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 0a  ce of registers.
345a6 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20      ** starting 
345a7 61 74 20 70 44 65 73 74 2d 3e 69 53 64 73 74 2e  at pDest->iSdst.
345a8 20 20 54 68 65 6e 20 74 68 65 20 63 6f 2d 72 6f    Then the co-ro
345a9 75 74 69 6e 65 20 79 69 65 6c 64 73 2e 0a 20 20  utine yields..  
345aa 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
345ab 54 5f 43 6f 72 6f 75 74 69 6e 65 3a 20 7b 0a 20  T_Coroutine: {. 
345ac 20 20 20 20 20 69 66 28 20 70 44 65 73 74 2d 3e       if( pDest->
345ad 69 53 64 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  iSdst==0 ){.    
345ae 20 20 20 20 70 44 65 73 74 2d 3e 69 53 64 73 74      pDest->iSdst
345af 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
345b0 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70  pRange(pParse, p
345b1 49 6e 2d 3e 6e 53 64 73 74 29 3b 0a 20 20 20 20  In->nSdst);.    
345b2 20 20 20 20 70 44 65 73 74 2d 3e 6e 53 64 73 74      pDest->nSdst
345b3 20 3d 20 70 49 6e 2d 3e 6e 53 64 73 74 3b 0a 20   = pIn->nSdst;. 
345b4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
345b5 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
345b6 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 53  (pParse, pIn->iS
345b7 64 73 74 2c 20 70 44 65 73 74 2d 3e 69 53 64 73  dst, pDest->iSds
345b8 74 2c 20 70 44 65 73 74 2d 3e 6e 53 64 73 74 29  t, pDest->nSdst)
345b9 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
345ba 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
345bb 59 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e 69 53  Yield, pDest->iS
345bc 44 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 62 72  DParm);.      br
345bd 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
345be 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68  /* If none of th
345bf 65 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 74 68  e above, then th
345c0 65 20 72 65 73 75 6c 74 20 64 65 73 74 69 6e 61  e result destina
345c1 74 69 6f 6e 20 6d 75 73 74 20 62 65 0a 20 20 20  tion must be.   
345c2 20 2a 2a 20 53 52 54 5f 4f 75 74 70 75 74 2e 20   ** SRT_Output. 
345c3 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
345c4 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 77 69   never called wi
345c5 74 68 20 61 6e 79 20 6f 74 68 65 72 0a 20 20 20  th any other.   
345c6 20 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20   ** destination 
345c7 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f  other than the o
345c8 6e 65 73 20 68 61 6e 64 6c 65 64 20 61 62 6f 76  nes handled abov
345c9 65 20 6f 72 20 53 52 54 5f 4f 75 74 70 75 74 2e  e or SRT_Output.
345ca 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46  .    **.    ** F
345cb 6f 72 20 53 52 54 5f 4f 75 74 70 75 74 2c 20 72  or SRT_Output, r
345cc 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65  esults are store
345cd 64 20 69 6e 20 61 20 73 65 71 75 65 6e 63 65 20  d in a sequence 
345ce 6f 66 20 72 65 67 69 73 74 65 72 73 2e 20 20 0a  of registers.  .
345cf 20 20 20 20 2a 2a 20 54 68 65 6e 20 74 68 65 20      ** Then the 
345d0 4f 50 5f 52 65 73 75 6c 74 52 6f 77 20 6f 70 63  OP_ResultRow opc
345d1 6f 64 65 20 69 73 20 75 73 65 64 20 74 6f 20 63  ode is used to c
345d2 61 75 73 65 20 73 71 6c 69 74 65 33 5f 73 74 65  ause sqlite3_ste
345d3 70 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65  p() to.    ** re
345d4 74 75 72 6e 20 74 68 65 20 6e 65 78 74 20 72 6f  turn the next ro
345d5 77 20 6f 66 20 72 65 73 75 6c 74 2e 0a 20 20 20  w of result..   
345d6 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a   */.    default:
345d7 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
345d8 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53   pDest->eDest==S
345d9 52 54 5f 4f 75 74 70 75 74 20 29 3b 0a 20 20 20  RT_Output );.   
345da 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
345db 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
345dc 74 52 6f 77 2c 20 70 49 6e 2d 3e 69 53 64 73 74  tRow, pIn->iSdst
345dd 2c 20 70 49 6e 2d 3e 6e 53 64 73 74 29 3b 0a 20  , pIn->nSdst);. 
345de 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
345df 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
345e0 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d  nge(pParse, pIn-
345e1 3e 69 53 64 73 74 2c 20 70 49 6e 2d 3e 6e 53 64  >iSdst, pIn->nSd
345e2 73 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  st);.      break
345e3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
345e4 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e  * Jump to the en
345e5 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 66  d of the loop if
345e6 20 74 68 65 20 4c 49 4d 49 54 20 69 73 20 72 65   the LIMIT is re
345e7 61 63 68 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  ached..  */.  if
345e8 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a 20  ( p->iLimit ){. 
345e9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
345ea 64 4f 70 33 28 76 2c 20 4f 50 5f 49 66 5a 65 72  dOp3(v, OP_IfZer
345eb 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 69 42  o, p->iLimit, iB
345ec 72 65 61 6b 2c 20 2d 31 29 3b 0a 20 20 7d 0a 0a  reak, -1);.  }..
345ed 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68    /* Generate th
345ee 65 20 73 75 62 72 6f 75 74 69 6e 65 20 72 65 74  e subroutine ret
345ef 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  urn.  */.  sqlit
345f0 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
345f1 65 6c 28 76 2c 20 69 43 6f 6e 74 69 6e 75 65 29  el(v, iContinue)
345f2 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
345f3 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75  ddOp1(v, OP_Retu
345f4 72 6e 2c 20 72 65 67 52 65 74 75 72 6e 29 3b 0a  rn, regReturn);.
345f5 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a  .  return addr;.
345f6 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 74 65 72 6e 61  }../*.** Alterna
345f7 74 69 76 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65  tive compound se
345f8 6c 65 63 74 20 63 6f 64 65 20 67 65 6e 65 72 61  lect code genera
345f9 74 6f 72 20 66 6f 72 20 63 61 73 65 73 20 77 68  tor for cases wh
345fa 65 6e 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 61  en there.** is a
345fb 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
345fc 65 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 73 73 75  e..**.** We assu
345fd 6d 65 20 61 20 71 75 65 72 79 20 6f 66 20 74 68  me a query of th
345fe 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d  e following form
345ff 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 3c 73 65  :.**.**      <se
34600 6c 65 63 74 41 3e 20 20 3c 6f 70 65 72 61 74 6f  lectA>  <operato
34601 72 3e 20 20 3c 73 65 6c 65 63 74 42 3e 20 20 4f  r>  <selectB>  O
34602 52 44 45 52 20 42 59 20 3c 6f 72 64 65 72 62 79  RDER BY <orderby
34603 6c 69 73 74 3e 0a 2a 2a 0a 2a 2a 20 3c 6f 70 65  list>.**.** <ope
34604 72 61 74 6f 72 3e 20 69 73 20 6f 6e 65 20 6f 66  rator> is one of
34605 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20 55 4e 49 4f   UNION ALL, UNIO
34606 4e 2c 20 45 58 43 45 50 54 2c 20 6f 72 20 49 4e  N, EXCEPT, or IN
34607 54 45 52 53 45 43 54 2e 20 20 54 68 65 20 69 64  TERSECT.  The id
34608 65 61 0a 2a 2a 20 69 73 20 74 6f 20 63 6f 64 65  ea.** is to code
34609 20 62 6f 74 68 20 3c 73 65 6c 65 63 74 41 3e 20   both <selectA> 
3460a 61 6e 64 20 3c 73 65 6c 65 63 74 42 3e 20 77 69  and <selectB> wi
3460b 74 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  th the ORDER BY 
3460c 63 6c 61 75 73 65 20 61 73 0a 2a 2a 20 63 6f 2d  clause as.** co-
3460d 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 6e 20  routines.  Then 
3460e 72 75 6e 20 74 68 65 20 63 6f 2d 72 6f 75 74 69  run the co-routi
3460f 6e 65 73 20 69 6e 20 70 61 72 61 6c 6c 65 6c 20  nes in parallel 
34610 61 6e 64 20 6d 65 72 67 65 20 74 68 65 20 72 65  and merge the re
34611 73 75 6c 74 73 0a 2a 2a 20 69 6e 74 6f 20 74 68  sults.** into th
34612 65 20 6f 75 74 70 75 74 2e 20 20 49 6e 20 61 64  e output.  In ad
34613 64 69 74 69 6f 6e 20 74 6f 20 74 68 65 20 74 77  dition to the tw
34614 6f 20 63 6f 72 6f 75 74 69 6e 65 73 20 28 63 61  o coroutines (ca
34615 6c 6c 65 64 20 73 65 6c 65 63 74 41 20 61 6e 64  lled selectA and
34616 0a 2a 2a 20 73 65 6c 65 63 74 42 29 20 74 68 65  .** selectB) the
34617 72 65 20 61 72 65 20 37 20 73 75 62 72 6f 75 74  re are 7 subrout
34618 69 6e 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 6f  ines:.**.**    o
34619 75 74 41 3a 20 20 20 20 4d 6f 76 65 20 74 68 65  utA:    Move the
3461a 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65 20 73   output of the s
3461b 65 6c 65 63 74 41 20 63 6f 72 6f 75 74 69 6e 65  electA coroutine
3461c 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74   into the output
3461d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3461e 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  of the compound 
3461f 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  query..**.**    
34620 6f 75 74 42 3a 20 20 20 20 4d 6f 76 65 20 74 68  outB:    Move th
34621 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65 20  e output of the 
34622 73 65 6c 65 63 74 42 20 63 6f 72 6f 75 74 69 6e  selectB coroutin
34623 65 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75  e into the outpu
34624 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
34625 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   of the compound
34626 20 71 75 65 72 79 2e 20 20 28 4f 6e 6c 79 20 67   query.  (Only g
34627 65 6e 65 72 61 74 65 64 20 66 6f 72 20 55 4e 49  enerated for UNI
34628 4f 4e 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20  ON and.**       
34629 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 2e        UNION ALL.
3462a 20 20 45 58 43 45 50 54 20 61 6e 64 20 49 4e 53    EXCEPT and INS
3462b 45 52 54 53 45 43 54 20 6e 65 76 65 72 20 6f 75  ERTSECT never ou
3462c 74 70 75 74 20 61 20 72 6f 77 20 74 68 61 74 0a  tput a row that.
3462d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 61  **             a
3462e 70 70 65 61 72 73 20 6f 6e 6c 79 20 69 6e 20 42  ppears only in B
3462f 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 20 41 6c 74 42  .).**.**    AltB
34630 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e  :    Called when
34631 20 74 68 65 72 65 20 69 73 20 64 61 74 61 20 66   there is data f
34632 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74 69  rom both corouti
34633 6e 65 73 20 61 6e 64 20 41 3c 42 2e 0a 2a 2a 0a  nes and A<B..**.
34634 2a 2a 20 20 20 20 41 65 71 42 3a 20 20 20 20 43  **    AeqB:    C
34635 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72 65  alled when there
34636 20 69 73 20 64 61 74 61 20 66 72 6f 6d 20 62 6f   is data from bo
34637 74 68 20 63 6f 72 6f 75 74 69 6e 65 73 20 61 6e  th coroutines an
34638 64 20 41 3d 3d 42 2e 0a 2a 2a 0a 2a 2a 20 20 20  d A==B..**.**   
34639 20 41 67 74 42 3a 20 20 20 20 43 61 6c 6c 65 64   AgtB:    Called
3463a 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 64   when there is d
3463b 61 74 61 20 66 72 6f 6d 20 62 6f 74 68 20 63 6f  ata from both co
3463c 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 41 3e 42  routines and A>B
3463d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45 6f 66 41 3a  ..**.**    EofA:
3463e 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e 20      Called when 
3463f 64 61 74 61 20 69 73 20 65 78 68 61 75 73 74 65  data is exhauste
34640 64 20 66 72 6f 6d 20 73 65 6c 65 63 74 41 2e 0a  d from selectA..
34641 2a 2a 0a 2a 2a 20 20 20 20 45 6f 66 42 3a 20 20  **.**    EofB:  
34642 20 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 64 61    Called when da
34643 74 61 20 69 73 20 65 78 68 61 75 73 74 65 64 20  ta is exhausted 
34644 66 72 6f 6d 20 73 65 6c 65 63 74 42 2e 0a 2a 2a  from selectB..**
34645 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e  .** The implemen
34646 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61  tation of the la
34647 74 74 65 72 20 66 69 76 65 20 73 75 62 72 6f 75  tter five subrou
34648 74 69 6e 65 73 20 64 65 70 65 6e 64 20 6f 6e 20  tines depend on 
34649 77 68 69 63 68 20 0a 2a 2a 20 3c 6f 70 65 72 61  which .** <opera
3464a 74 6f 72 3e 20 69 73 20 75 73 65 64 3a 0a 2a 2a  tor> is used:.**
3464b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
3464c 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 20 20 20 20     UNION ALL    
3464d 20 20 20 20 20 55 4e 49 4f 4e 20 20 20 20 20 20       UNION      
3464e 20 20 20 20 20 20 45 58 43 45 50 54 20 20 20 20        EXCEPT    
3464f 20 20 20 20 20 20 49 4e 54 45 52 53 45 43 54 0a        INTERSECT.
34650 2a 2a 20 20 20 20 20 20 20 20 20 20 2d 2d 2d 2d  **          ----
34651 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d  ---------  -----
34652 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d  ------------  --
34653 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d  ------------  --
34654 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
34655 2a 2a 20 20 20 41 6c 74 42 3a 20 20 20 6f 75 74  **   AltB:   out
34656 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20 6f 75  A, nextA      ou
34657 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20  tA, nextA       
34658 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20  outA, nextA     
34659 20 20 20 20 6e 65 78 74 41 0a 2a 2a 0a 2a 2a 20      nextA.**.** 
3465a 20 20 41 65 71 42 3a 20 20 20 6f 75 74 41 2c 20    AeqB:   outA, 
3465b 6e 65 78 74 41 20 20 20 20 20 20 20 20 20 6e 65  nextA         ne
3465c 78 74 41 20 20 20 20 20 20 20 20 20 20 20 20 20  xtA             
3465d 6e 65 78 74 41 20 20 20 20 20 20 20 20 20 6f 75  nextA         ou
3465e 74 41 2c 20 6e 65 78 74 41 0a 2a 2a 0a 2a 2a 20  tA, nextA.**.** 
3465f 20 20 41 67 74 42 3a 20 20 20 6f 75 74 42 2c 20    AgtB:   outB, 
34660 6e 65 78 74 42 20 20 20 20 20 20 6f 75 74 42 2c  nextB      outB,
34661 20 6e 65 78 74 42 20 20 20 20 20 20 20 20 20 20   nextB          
34662 6e 65 78 74 42 20 20 20 20 20 20 20 20 20 20 20  nextB           
34663 20 6e 65 78 74 42 0a 2a 2a 0a 2a 2a 20 20 20 45   nextB.**.**   E
34664 6f 66 41 3a 20 20 20 6f 75 74 42 2c 20 6e 65 78  ofA:   outB, nex
34665 74 42 20 20 20 20 20 20 6f 75 74 42 2c 20 6e 65  tB      outB, ne
34666 78 74 42 20 20 20 20 20 20 20 20 20 20 68 61 6c  xtB          hal
34667 74 20 20 20 20 20 20 20 20 20 20 20 20 20 68 61  t             ha
34668 6c 74 0a 2a 2a 0a 2a 2a 20 20 20 45 6f 66 42 3a  lt.**.**   EofB:
34669 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20     outA, nextA  
3466a 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20      outA, nextA 
3466b 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74        outA, next
3466c 41 20 20 20 20 20 20 20 20 20 68 61 6c 74 0a 2a  A         halt.*
3466d 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 41 6c 74 42  *.** In the AltB
3466e 2c 20 41 65 71 42 2c 20 61 6e 64 20 41 67 74 42  , AeqB, and AgtB
3466f 20 73 75 62 72 6f 75 74 69 6e 65 73 2c 20 61 6e   subroutines, an
34670 20 45 4f 46 20 6f 6e 20 41 20 66 6f 6c 6c 6f 77   EOF on A follow
34671 69 6e 67 20 6e 65 78 74 41 0a 2a 2a 20 63 61 75  ing nextA.** cau
34672 73 65 73 20 61 6e 20 69 6d 6d 65 64 69 61 74 65  ses an immediate
34673 20 6a 75 6d 70 20 74 6f 20 45 6f 66 41 20 61 6e   jump to EofA an
34674 64 20 61 6e 20 45 4f 46 20 6f 6e 20 42 20 66 6f  d an EOF on B fo
34675 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 42 20 63 61  llowing nextB ca
34676 75 73 65 73 0a 2a 2a 20 61 6e 20 69 6d 6d 65 64  uses.** an immed
34677 69 61 74 65 20 6a 75 6d 70 20 74 6f 20 45 6f 66  iate jump to Eof
34678 42 2e 20 20 57 69 74 68 69 6e 20 45 6f 66 41 20  B.  Within EofA 
34679 61 6e 64 20 45 6f 66 42 2c 20 61 6e 64 20 45 4f  and EofB, and EO
3467a 46 20 6f 6e 20 65 6e 74 72 79 20 6f 72 0a 2a 2a  F on entry or.**
3467b 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 58   following nextX
3467c 20 63 61 75 73 65 73 20 61 20 6a 75 6d 70 20 74   causes a jump t
3467d 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
3467e 20 73 65 6c 65 63 74 20 70 72 6f 63 65 73 73 69   select processi
3467f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 44 75 70 6c 69 63  ng..**.** Duplic
34680 61 74 65 20 72 65 6d 6f 76 61 6c 20 69 6e 20 74  ate removal in t
34681 68 65 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54  he UNION, EXCEPT
34682 2c 20 61 6e 64 20 49 4e 54 45 52 53 45 43 54 20  , and INTERSECT 
34683 63 61 73 65 73 20 69 73 20 68 61 6e 64 6c 65 64  cases is handled
34684 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 6f  .** within the o
34685 75 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e 65  utput subroutine
34686 2e 20 20 54 68 65 20 72 65 67 50 72 65 76 20 72  .  The regPrev r
34687 65 67 69 73 74 65 72 20 73 65 74 20 68 6f 6c 64  egister set hold
34688 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79  s the previously
34689 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61 6c 75 65  .** output value
3468a 2e 20 20 41 20 63 6f 6d 70 61 72 69 73 6f 6e 20  .  A comparison 
3468b 69 73 20 6d 61 64 65 20 61 67 61 69 6e 73 74 20  is made against 
3468c 74 68 69 73 20 76 61 6c 75 65 20 61 6e 64 20 74  this value and t
3468d 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 69 73 20  he output.** is 
3468e 73 6b 69 70 70 65 64 20 69 66 20 74 68 65 20 6e  skipped if the n
3468f 65 78 74 20 72 65 73 75 6c 74 73 20 77 6f 75 6c  ext results woul
34690 64 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73  d be the same as
34691 20 74 68 65 20 70 72 65 76 69 6f 75 73 2e 0a 2a   the previous..*
34692 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65  *.** The impleme
34693 6e 74 61 74 69 6f 6e 20 70 6c 61 6e 20 69 73 20  ntation plan is 
34694 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
34695 20 74 77 6f 20 63 6f 72 6f 75 74 69 6e 65 73 20   two coroutines 
34696 61 6e 64 20 73 65 76 65 6e 0a 2a 2a 20 73 75 62  and seven.** sub
34697 72 6f 75 74 69 6e 65 73 20 66 69 72 73 74 2c 20  routines first, 
34698 74 68 65 6e 20 70 75 74 20 74 68 65 20 63 6f 6e  then put the con
34699 74 72 6f 6c 20 6c 6f 67 69 63 20 61 74 20 74 68  trol logic at th
3469a 65 20 62 6f 74 74 6f 6d 2e 20 20 4c 69 6b 65 20  e bottom.  Like 
3469b 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
3469c 20 20 20 20 20 67 6f 74 6f 20 49 6e 69 74 0a 2a       goto Init.*
3469d 2a 20 20 20 20 20 63 6f 41 3a 20 63 6f 72 6f 75  *     coA: corou
3469e 74 69 6e 65 20 66 6f 72 20 6c 65 66 74 20 71 75  tine for left qu
3469f 65 72 79 20 28 41 29 0a 2a 2a 20 20 20 20 20 63  ery (A).**     c
346a0 6f 42 3a 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f  oB: coroutine fo
346a1 72 20 72 69 67 68 74 20 71 75 65 72 79 20 28 42  r right query (B
346a2 29 0a 2a 2a 20 20 20 20 6f 75 74 41 3a 20 6f 75  ).**    outA: ou
346a3 74 70 75 74 20 6f 6e 65 20 72 6f 77 20 6f 66 20  tput one row of 
346a4 41 0a 2a 2a 20 20 20 20 6f 75 74 42 3a 20 6f 75  A.**    outB: ou
346a5 74 70 75 74 20 6f 6e 65 20 72 6f 77 20 6f 66 20  tput one row of 
346a6 42 20 28 55 4e 49 4f 4e 20 61 6e 64 20 55 4e 49  B (UNION and UNI
346a7 4f 4e 20 41 4c 4c 20 6f 6e 6c 79 29 0a 2a 2a 20  ON ALL only).** 
346a8 20 20 20 45 6f 66 41 3a 20 2e 2e 2e 0a 2a 2a 20     EofA: ....** 
346a9 20 20 20 45 6f 66 42 3a 20 2e 2e 2e 0a 2a 2a 20     EofB: ....** 
346aa 20 20 20 41 6c 74 42 3a 20 2e 2e 2e 0a 2a 2a 20     AltB: ....** 
346ab 20 20 20 41 65 71 42 3a 20 2e 2e 2e 0a 2a 2a 20     AeqB: ....** 
346ac 20 20 20 41 67 74 42 3a 20 2e 2e 2e 0a 2a 2a 20     AgtB: ....** 
346ad 20 20 20 49 6e 69 74 3a 20 69 6e 69 74 69 61 6c     Init: initial
346ae 69 7a 65 20 63 6f 72 6f 75 74 69 6e 65 20 72 65  ize coroutine re
346af 67 69 73 74 65 72 73 0a 2a 2a 20 20 20 20 20 20  gisters.**      
346b0 20 20 20 20 79 69 65 6c 64 20 63 6f 41 0a 2a 2a      yield coA.**
346b1 20 20 20 20 20 20 20 20 20 20 69 66 20 65 6f 66            if eof
346b2 28 41 29 20 67 6f 74 6f 20 45 6f 66 41 0a 2a 2a  (A) goto EofA.**
346b3 20 20 20 20 20 20 20 20 20 20 79 69 65 6c 64 20            yield 
346b4 63 6f 42 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  coB.**          
346b5 69 66 20 65 6f 66 28 42 29 20 67 6f 74 6f 20 45  if eof(B) goto E
346b6 6f 66 42 0a 2a 2a 20 20 20 20 43 6d 70 72 3a 20  ofB.**    Cmpr: 
346b7 43 6f 6d 70 61 72 65 20 41 2c 20 42 0a 2a 2a 20  Compare A, B.** 
346b8 20 20 20 20 20 20 20 20 20 4a 75 6d 70 20 41 6c           Jump Al
346b9 74 42 2c 20 41 65 71 42 2c 20 41 67 74 42 0a 2a  tB, AeqB, AgtB.*
346ba 2a 20 20 20 20 20 45 6e 64 3a 20 2e 2e 2e 0a 2a  *     End: ....*
346bb 2a 0a 2a 2a 20 57 65 20 63 61 6c 6c 20 41 6c 74  *.** We call Alt
346bc 42 2c 20 41 65 71 42 2c 20 41 67 74 42 2c 20 45  B, AeqB, AgtB, E
346bd 6f 66 41 2c 20 61 6e 64 20 45 6f 66 42 20 22 73  ofA, and EofB "s
346be 75 62 72 6f 75 74 69 6e 65 73 22 20 62 75 74 20  ubroutines" but 
346bf 74 68 65 79 20 61 72 65 20 6e 6f 74 0a 2a 2a 20  they are not.** 
346c0 61 63 74 75 61 6c 6c 79 20 63 61 6c 6c 65 64 20  actually called 
346c1 75 73 69 6e 67 20 47 6f 73 75 62 20 61 6e 64 20  using Gosub and 
346c2 74 68 65 79 20 64 6f 20 6e 6f 74 20 52 65 74 75  they do not Retu
346c3 72 6e 2e 20 20 45 6f 66 41 20 61 6e 64 20 45 6f  rn.  EofA and Eo
346c4 66 42 20 6c 6f 6f 70 0a 2a 2a 20 75 6e 74 69 6c  fB loop.** until
346c5 20 61 6c 6c 20 64 61 74 61 20 69 73 20 65 78 68   all data is exh
346c6 61 75 73 74 65 64 20 74 68 65 6e 20 6a 75 6d 70  austed then jump
346c7 20 74 6f 20 74 68 65 20 22 65 6e 64 22 20 6c 61   to the "end" la
346c8 62 65 2e 20 20 41 6c 74 42 2c 20 41 65 71 42 2c  be.  AltB, AeqB,
346c9 0a 2a 2a 20 61 6e 64 20 41 67 74 42 20 6a 75 6d  .** and AgtB jum
346ca 70 20 74 6f 20 65 69 74 68 65 72 20 4c 32 20 6f  p to either L2 o
346cb 72 20 74 6f 20 6f 6e 65 20 6f 66 20 45 6f 66 41  r to one of EofA
346cc 20 6f 72 20 45 6f 66 42 2e 0a 2a 2f 0a 23 69 66   or EofB..*/.#if
346cd 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
346ce 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
346cf 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 75 6c 74  .static int mult
346d0 69 53 65 6c 65 63 74 4f 72 64 65 72 42 79 28 0a  iSelectOrderBy(.
346d1 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
346d2 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
346d3 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
346d4 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
346d5 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67        /* The rig
346d6 68 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43  ht-most of SELEC
346d7 54 73 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a  Ts to be coded *
346d8 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a  /.  SelectDest *
346d9 70 44 65 73 74 20 20 20 20 20 2f 2a 20 57 68 61  pDest     /* Wha
346da 74 20 74 6f 20 64 6f 20 77 69 74 68 20 71 75 65  t to do with que
346db 72 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b  ry results */.){
346dc 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20  .  int i, j;    
346dd 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
346de 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 53   counters */.  S
346df 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20 20  elect *pPrior;  
346e0 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72 20       /* Another 
346e1 53 45 4c 45 43 54 20 69 6d 6d 65 64 69 61 74 65  SELECT immediate
346e2 6c 79 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 2a  ly to our left *
346e3 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
346e4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e            /* Gen
346e5 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 74 68  erate code to th
346e6 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 53 65 6c  is VDBE */.  Sel
346e7 65 63 74 44 65 73 74 20 64 65 73 74 41 3b 20 20  ectDest destA;  
346e8 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f     /* Destinatio
346e9 6e 20 66 6f 72 20 63 6f 72 6f 75 74 69 6e 65 20  n for coroutine 
346ea 41 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73  A */.  SelectDes
346eb 74 20 64 65 73 74 42 3b 20 20 20 20 20 2f 2a 20  t destB;     /* 
346ec 44 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72 20  Destination for 
346ed 63 6f 72 6f 75 74 69 6e 65 20 42 20 2a 2f 0a 20  coroutine B */. 
346ee 20 69 6e 74 20 72 65 67 41 64 64 72 41 3b 20 20   int regAddrA;  
346ef 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
346f0 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 73  s register for s
346f1 65 6c 65 63 74 2d 41 20 63 6f 72 6f 75 74 69 6e  elect-A coroutin
346f2 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45 6f  e */.  int regEo
346f3 66 41 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  fA;          /* 
346f4 46 6c 61 67 20 74 6f 20 69 6e 64 69 63 61 74 65  Flag to indicate
346f5 20 77 68 65 6e 20 73 65 6c 65 63 74 2d 41 20 69   when select-A i
346f6 73 20 63 6f 6d 70 6c 65 74 65 20 2a 2f 0a 20 20  s complete */.  
346f7 69 6e 74 20 72 65 67 41 64 64 72 42 3b 20 20 20  int regAddrB;   
346f8 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
346f9 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 73 65   register for se
346fa 6c 65 63 74 2d 42 20 63 6f 72 6f 75 74 69 6e 65  lect-B coroutine
346fb 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45 6f 66   */.  int regEof
346fc 42 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  B;          /* F
346fd 6c 61 67 20 74 6f 20 69 6e 64 69 63 61 74 65 20  lag to indicate 
346fe 77 68 65 6e 20 73 65 6c 65 63 74 2d 42 20 69 73  when select-B is
346ff 20 63 6f 6d 70 6c 65 74 65 20 2a 2f 0a 20 20 69   complete */.  i
34700 6e 74 20 61 64 64 72 53 65 6c 65 63 74 41 3b 20  nt addrSelectA; 
34701 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
34702 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2d 41 20  of the select-A 
34703 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  coroutine */.  i
34704 6e 74 20 61 64 64 72 53 65 6c 65 63 74 42 3b 20  nt addrSelectB; 
34705 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
34706 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2d 42 20  of the select-B 
34707 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  coroutine */.  i
34708 6e 74 20 72 65 67 4f 75 74 41 3b 20 20 20 20 20  nt regOutA;     
34709 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
3470a 72 65 67 69 73 74 65 72 20 66 6f 72 20 74 68 65  register for the
3470b 20 6f 75 74 70 75 74 2d 41 20 73 75 62 72 6f 75   output-A subrou
3470c 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65  tine */.  int re
3470d 67 4f 75 74 42 3b 20 20 20 20 20 20 20 20 20 20  gOutB;          
3470e 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67 69 73  /* Address regis
3470f 74 65 72 20 66 6f 72 20 74 68 65 20 6f 75 74 70  ter for the outp
34710 75 74 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 20  ut-B subroutine 
34711 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 75 74  */.  int addrOut
34712 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64  A;         /* Ad
34713 64 72 65 73 73 20 6f 66 20 74 68 65 20 6f 75 74  dress of the out
34714 70 75 74 2d 41 20 73 75 62 72 6f 75 74 69 6e 65  put-A subroutine
34715 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 75   */.  int addrOu
34716 74 42 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 41  tB = 0;     /* A
34717 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6f 75  ddress of the ou
34718 74 70 75 74 2d 42 20 73 75 62 72 6f 75 74 69 6e  tput-B subroutin
34719 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 45  e */.  int addrE
3471a 6f 66 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ofA;         /* 
3471b 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 73  Address of the s
3471c 65 6c 65 63 74 2d 41 2d 65 78 68 61 75 73 74 65  elect-A-exhauste
3471d 64 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  d subroutine */.
3471e 20 20 69 6e 74 20 61 64 64 72 45 6f 66 42 3b 20    int addrEofB; 
3471f 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
34720 73 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  ss of the select
34721 2d 42 2d 65 78 68 61 75 73 74 65 64 20 73 75 62  -B-exhausted sub
34722 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
34723 20 61 64 64 72 41 6c 74 42 3b 20 20 20 20 20 20   addrAltB;      
34724 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
34725 20 74 68 65 20 41 3c 42 20 73 75 62 72 6f 75 74   the A<B subrout
34726 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ine */.  int add
34727 72 41 65 71 42 3b 20 20 20 20 20 20 20 20 20 2f  rAeqB;         /
34728 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65  * Address of the
34729 20 41 3d 3d 42 20 73 75 62 72 6f 75 74 69 6e 65   A==B subroutine
3472a 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 41 67   */.  int addrAg
3472b 74 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  tB;         /* A
3472c 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 41 3e  ddress of the A>
3472d 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  B subroutine */.
3472e 20 20 69 6e 74 20 72 65 67 4c 69 6d 69 74 41 3b    int regLimitA;
3472f 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6d 69 74          /* Limit
34730 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 73 65   register for se
34731 6c 65 63 74 2d 41 20 2a 2f 0a 20 20 69 6e 74 20  lect-A */.  int 
34732 72 65 67 4c 69 6d 69 74 42 3b 20 20 20 20 20 20  regLimitB;      
34733 20 20 2f 2a 20 4c 69 6d 69 74 20 72 65 67 69 73    /* Limit regis
34734 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74 2d 41  ter for select-A
34735 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 50 72 65   */.  int regPre
34736 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  v;          /* A
34737 20 72 61 6e 67 65 20 6f 66 20 72 65 67 69 73 74   range of regist
34738 65 72 73 20 74 6f 20 68 6f 6c 64 20 70 72 65 76  ers to hold prev
34739 69 6f 75 73 20 6f 75 74 70 75 74 20 2a 2f 0a 20  ious output */. 
3473a 20 69 6e 74 20 73 61 76 65 64 4c 69 6d 69 74 3b   int savedLimit;
3473b 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20         /* Saved 
3473c 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4c 69 6d  value of p->iLim
3473d 69 74 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65  it */.  int save
3473e 64 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a  dOffset;      /*
3473f 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20   Saved value of 
34740 70 2d 3e 69 4f 66 66 73 65 74 20 2a 2f 0a 20 20  p->iOffset */.  
34741 69 6e 74 20 6c 61 62 65 6c 43 6d 70 72 3b 20 20  int labelCmpr;  
34742 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66        /* Label f
34743 6f 72 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  or the start of 
34744 74 68 65 20 6d 65 72 67 65 20 61 6c 67 6f 72 69  the merge algori
34745 74 68 6d 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 62  thm */.  int lab
34746 65 6c 45 6e 64 3b 20 20 20 20 20 20 20 20 20 2f  elEnd;         /
34747 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74 68 65 20  * Label for the 
34748 65 6e 64 20 6f 66 20 74 68 65 20 6f 76 65 72 61  end of the overa
34749 6c 6c 20 53 45 4c 45 43 54 20 73 74 6d 74 20 2a  ll SELECT stmt *
3474a 2f 0a 20 20 69 6e 74 20 6a 31 3b 20 20 20 20 20  /.  int j1;     
3474b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d            /* Jum
3474c 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74  p instructions t
3474d 68 61 74 20 67 65 74 20 72 65 74 61 72 67 65 74  hat get retarget
3474e 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b  ted */.  int op;
3474f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
34750 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 41 4c 4c 2c  * One of TK_ALL,
34751 20 54 4b 5f 55 4e 49 4f 4e 2c 20 54 4b 5f 45 58   TK_UNION, TK_EX
34752 43 45 50 54 2c 20 54 4b 5f 49 4e 54 45 52 53 45  CEPT, TK_INTERSE
34753 43 54 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20  CT */.  KeyInfo 
34754 2a 70 4b 65 79 44 75 70 20 3d 20 30 3b 20 2f 2a  *pKeyDup = 0; /*
34755 20 43 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 66 6f   Comparison info
34756 72 6d 61 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c  rmation for dupl
34757 69 63 61 74 65 20 72 65 6d 6f 76 61 6c 20 2a 2f  icate removal */
34758 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  .  KeyInfo *pKey
34759 4d 65 72 67 65 3b 20 20 20 2f 2a 20 43 6f 6d 70  Merge;   /* Comp
3475a 61 72 69 73 6f 6e 20 69 6e 66 6f 72 6d 61 74 69  arison informati
3475b 6f 6e 20 66 6f 72 20 6d 65 72 67 69 6e 67 20 72  on for merging r
3475c 6f 77 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ows */.  sqlite3
3475d 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f   *db;          /
3475e 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
3475f 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c  ction */.  ExprL
34760 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 20 20  ist *pOrderBy;  
34761 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
34762 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
34763 20 6e 4f 72 64 65 72 42 79 3b 20 20 20 20 20 20   nOrderBy;      
34764 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
34765 74 65 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 44  terms in the ORD
34766 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
34767 20 20 69 6e 74 20 2a 61 50 65 72 6d 75 74 65 3b    int *aPermute;
34768 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 70 69          /* Mappi
34769 6e 67 20 66 72 6f 6d 20 4f 52 44 45 52 20 42 59  ng from ORDER BY
3476a 20 74 65 72 6d 73 20 74 6f 20 72 65 73 75 6c 74   terms to result
3476b 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a   set columns */.
3476c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
3476d 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69 6e  MIT_EXPLAIN.  in
3476e 74 20 69 53 75 62 31 3b 20 20 20 20 20 20 20 20  t iSub1;        
3476f 20 20 20 20 2f 2a 20 45 51 50 20 69 64 20 6f 66      /* EQP id of
34770 20 6c 65 66 74 2d 68 61 6e 64 20 71 75 65 72 79   left-hand query
34771 20 2a 2f 0a 20 20 69 6e 74 20 69 53 75 62 32 3b   */.  int iSub2;
34772 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
34773 51 50 20 69 64 20 6f 66 20 72 69 67 68 74 2d 68  QP id of right-h
34774 61 6e 64 20 71 75 65 72 79 20 2a 2f 0a 23 65 6e  and query */.#en
34775 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70  dif..  assert( p
34776 2d 3e 70 4f 72 64 65 72 42 79 21 3d 30 20 29 3b  ->pOrderBy!=0 );
34777 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 44  .  assert( pKeyD
34778 75 70 3d 3d 30 20 29 3b 20 2f 2a 20 22 4d 61 6e  up==0 ); /* "Man
34779 61 67 65 64 22 20 63 6f 64 65 20 6e 65 65 64 73  aged" code needs
3477a 20 74 68 69 73 2e 20 20 54 69 63 6b 65 74 20 23   this.  Ticket #
3477b 33 33 38 32 2e 20 2a 2f 0a 20 20 64 62 20 3d 20  3382. */.  db = 
3477c 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 76 20  pParse->db;.  v 
3477d 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
3477e 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20  .  assert( v!=0 
3477f 29 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c 72 65  );       /* Alre
34780 61 64 79 20 74 68 72 6f 77 6e 20 74 68 65 20 65  ady thrown the e
34781 72 72 6f 72 20 69 66 20 56 44 42 45 20 61 6c 6c  rror if VDBE all
34782 6f 63 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 6c  oc failed */.  l
34783 61 62 65 6c 45 6e 64 20 3d 20 73 71 6c 69 74 65  abelEnd = sqlite
34784 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
34785 29 3b 0a 20 20 6c 61 62 65 6c 43 6d 70 72 20 3d  );.  labelCmpr =
34786 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
34787 4c 61 62 65 6c 28 76 29 3b 0a 0a 0a 20 20 2f 2a  Label(v);...  /*
34788 20 50 61 74 63 68 20 75 70 20 74 68 65 20 4f 52   Patch up the OR
34789 44 45 52 20 42 59 20 63 6c 61 75 73 65 0a 20 20  DER BY clause.  
3478a 2a 2f 0a 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b  */.  op = p->op;
3478b 20 20 0a 20 20 70 50 72 69 6f 72 20 3d 20 70 2d    .  pPrior = p-
3478c 3e 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72  >pPrior;.  asser
3478d 74 28 20 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65  t( pPrior->pOrde
3478e 72 42 79 3d 3d 30 20 29 3b 0a 20 20 70 4f 72 64  rBy==0 );.  pOrd
3478f 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72  erBy = p->pOrder
34790 42 79 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  By;.  assert( pO
34791 72 64 65 72 42 79 20 29 3b 0a 20 20 6e 4f 72 64  rderBy );.  nOrd
34792 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 2d  erBy = pOrderBy-
34793 3e 6e 45 78 70 72 3b 0a 0a 20 20 2f 2a 20 46 6f  >nExpr;..  /* Fo
34794 72 20 6f 70 65 72 61 74 6f 72 73 20 6f 74 68 65  r operators othe
34795 72 20 74 68 61 6e 20 55 4e 49 4f 4e 20 41 4c 4c  r than UNION ALL
34796 20 77 65 20 68 61 76 65 20 74 6f 20 6d 61 6b 65   we have to make
34797 20 73 75 72 65 20 74 68 61 74 0a 20 20 2a 2a 20   sure that.  ** 
34798 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
34799 75 73 65 20 63 6f 76 65 72 73 20 65 76 65 72 79  use covers every
3479a 20 74 65 72 6d 20 6f 66 20 74 68 65 20 72 65 73   term of the res
3479b 75 6c 74 20 73 65 74 2e 20 20 41 64 64 0a 20 20  ult set.  Add.  
3479c 2a 2a 20 74 65 72 6d 73 20 74 6f 20 74 68 65 20  ** terms to the 
3479d 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
3479e 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20  as necessary..  
3479f 2a 2f 0a 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f  */.  if( op!=TK_
347a0 41 4c 4c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  ALL ){.    for(i
347a1 3d 31 3b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  =1; db->mallocFa
347a2 69 6c 65 64 3d 3d 30 20 26 26 20 69 3c 3d 70 2d  iled==0 && i<=p-
347a3 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  >pEList->nExpr; 
347a4 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 72 75  i++){.      stru
347a5 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
347a6 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 66   *pItem;.      f
347a7 6f 72 28 6a 3d 30 2c 20 70 49 74 65 6d 3d 70 4f  or(j=0, pItem=pO
347a8 72 64 65 72 42 79 2d 3e 61 3b 20 6a 3c 6e 4f 72  rderBy->a; j<nOr
347a9 64 65 72 42 79 3b 20 6a 2b 2b 2c 20 70 49 74 65  derBy; j++, pIte
347aa 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73  m++){.        as
347ab 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 69 4f 72  sert( pItem->iOr
347ac 64 65 72 42 79 43 6f 6c 3e 30 20 29 3b 0a 20 20  derByCol>0 );.  
347ad 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d        if( pItem-
347ae 3e 69 4f 72 64 65 72 42 79 43 6f 6c 3d 3d 69 20  >iOrderByCol==i 
347af 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d  ) break;.      }
347b0 0a 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 6e 4f  .      if( j==nO
347b1 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20  rderBy ){.      
347b2 20 20 45 78 70 72 20 2a 70 4e 65 77 20 3d 20 73    Expr *pNew = s
347b3 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54  qlite3Expr(db, T
347b4 4b 5f 49 4e 54 45 47 45 52 2c 20 30 29 3b 0a 20  K_INTEGER, 0);. 
347b5 20 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 3d         if( pNew=
347b6 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
347b7 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
347b8 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d    pNew->flags |=
347b9 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 20   EP_IntValue;.  
347ba 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 69 56        pNew->u.iV
347bb 61 6c 75 65 20 3d 20 69 3b 0a 20 20 20 20 20 20  alue = i;.      
347bc 20 20 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c    pOrderBy = sql
347bd 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
347be 6e 64 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65  nd(pParse, pOrde
347bf 72 42 79 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20  rBy, pNew);.    
347c0 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79      if( pOrderBy
347c1 20 29 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 6e   ) pOrderBy->a[n
347c2 4f 72 64 65 72 42 79 2b 2b 5d 2e 69 4f 72 64 65  OrderBy++].iOrde
347c3 72 42 79 43 6f 6c 20 3d 20 28 75 31 36 29 69 3b  rByCol = (u16)i;
347c4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
347c5 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65   }..  /* Compute
347c6 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
347c7 70 65 72 6d 75 74 61 74 69 6f 6e 20 61 6e 64 20  permutation and 
347c8 6b 65 79 69 6e 66 6f 20 74 68 61 74 20 69 73 20  keyinfo that is 
347c9 75 73 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 74  used with.  ** t
347ca 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75  he permutation u
347cb 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
347cc 20 69 66 20 74 68 65 20 6e 65 78 74 0a 20 20 2a   if the next.  *
347cd 2a 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 73  * row of results
347ce 20 63 6f 6d 65 73 20 66 72 6f 6d 20 73 65 6c 65   comes from sele
347cf 63 74 41 20 6f 72 20 73 65 6c 65 63 74 42 2e 20  ctA or selectB. 
347d0 20 41 6c 73 6f 20 61 64 64 20 65 78 70 6c 69 63   Also add explic
347d1 69 74 0a 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f  it.  ** collatio
347d2 6e 73 20 74 6f 20 74 68 65 20 4f 52 44 45 52 20  ns to the ORDER 
347d3 42 59 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20  BY clause terms 
347d4 73 6f 20 74 68 61 74 20 77 68 65 6e 20 74 68 65  so that when the
347d5 20 73 75 62 71 75 65 72 69 65 73 0a 20 20 2a 2a   subqueries.  **
347d6 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 61 6e   to the right an
347d7 64 20 74 68 65 20 6c 65 66 74 20 61 72 65 20 65  d the left are e
347d8 76 61 6c 75 61 74 65 64 2c 20 74 68 65 79 20 75  valuated, they u
347d9 73 65 20 74 68 65 20 63 6f 72 72 65 63 74 0a 20  se the correct. 
347da 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 20   ** collation.. 
347db 20 2a 2f 0a 20 20 61 50 65 72 6d 75 74 65 20 3d   */.  aPermute =
347dc 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
347dd 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69  Raw(db, sizeof(i
347de 6e 74 29 2a 6e 4f 72 64 65 72 42 79 29 3b 0a 20  nt)*nOrderBy);. 
347df 20 69 66 28 20 61 50 65 72 6d 75 74 65 20 29 7b   if( aPermute ){
347e0 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72  .    struct Expr
347e1 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
347e2 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70  ;.    for(i=0, p
347e3 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61  Item=pOrderBy->a
347e4 3b 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69 2b  ; i<nOrderBy; i+
347e5 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
347e6 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d     assert( pItem
347e7 2d 3e 69 4f 72 64 65 72 42 79 43 6f 6c 3e 30 20  ->iOrderByCol>0 
347e8 20 26 26 20 70 49 74 65 6d 2d 3e 69 4f 72 64 65   && pItem->iOrde
347e9 72 42 79 43 6f 6c 3c 3d 70 2d 3e 70 45 4c 69 73  rByCol<=p->pELis
347ea 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 20 20  t->nExpr );.    
347eb 20 20 61 50 65 72 6d 75 74 65 5b 69 5d 20 3d 20    aPermute[i] = 
347ec 70 49 74 65 6d 2d 3e 69 4f 72 64 65 72 42 79 43  pItem->iOrderByC
347ed 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ol - 1;.    }.  
347ee 20 20 70 4b 65 79 4d 65 72 67 65 20 3d 0a 20 20    pKeyMerge =.  
347ef 20 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c      sqlite3DbMal
347f0 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
347f1 66 28 2a 70 4b 65 79 4d 65 72 67 65 29 2b 6e 4f  f(*pKeyMerge)+nO
347f2 72 64 65 72 42 79 2a 28 73 69 7a 65 6f 66 28 43  rderBy*(sizeof(C
347f3 6f 6c 6c 53 65 71 2a 29 2b 31 29 29 3b 0a 20 20  ollSeq*)+1));.  
347f4 20 20 69 66 28 20 70 4b 65 79 4d 65 72 67 65 20    if( pKeyMerge 
347f5 29 7b 0a 20 20 20 20 20 20 70 4b 65 79 4d 65 72  ){.      pKeyMer
347f6 67 65 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d  ge->aSortOrder =
347f7 20 28 75 38 2a 29 26 70 4b 65 79 4d 65 72 67 65   (u8*)&pKeyMerge
347f8 2d 3e 61 43 6f 6c 6c 5b 6e 4f 72 64 65 72 42 79  ->aColl[nOrderBy
347f9 5d 3b 0a 20 20 20 20 20 20 70 4b 65 79 4d 65 72  ];.      pKeyMer
347fa 67 65 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31  ge->nField = (u1
347fb 36 29 6e 4f 72 64 65 72 42 79 3b 0a 20 20 20 20  6)nOrderBy;.    
347fc 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 65 6e 63    pKeyMerge->enc
347fd 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20   = ENC(db);.    
347fe 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72    for(i=0; i<nOr
347ff 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20  derBy; i++){.   
34800 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43       CollSeq *pC
34801 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 45 78 70  oll;.        Exp
34802 72 20 2a 70 54 65 72 6d 20 3d 20 70 4f 72 64 65  r *pTerm = pOrde
34803 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  rBy->a[i].pExpr;
34804 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 65  .        if( pTe
34805 72 6d 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45  rm->flags & EP_E
34806 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20  xpCollate ){.   
34807 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70         pColl = p
34808 54 65 72 6d 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20  Term->pColl;.   
34809 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
3480a 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 6d 75        pColl = mu
3480b 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71  ltiSelectCollSeq
3480c 28 70 50 61 72 73 65 2c 20 70 2c 20 61 50 65 72  (pParse, p, aPer
3480d 6d 75 74 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20  mute[i]);.      
3480e 20 20 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73      pTerm->flags
3480f 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74   |= EP_ExpCollat
34810 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70 54 65  e;.          pTe
34811 72 6d 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c  rm->pColl = pCol
34812 6c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  l;.        }.   
34813 20 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e       pKeyMerge->
34814 61 43 6f 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c  aColl[i] = pColl
34815 3b 0a 20 20 20 20 20 20 20 20 70 4b 65 79 4d 65  ;.        pKeyMe
34816 72 67 65 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  rge->aSortOrder[
34817 69 5d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61  i] = pOrderBy->a
34818 5b 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20  [i].sortOrder;. 
34819 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
3481a 65 6c 73 65 7b 0a 20 20 20 20 70 4b 65 79 4d 65  else{.    pKeyMe
3481b 72 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  rge = 0;.  }..  
3481c 2f 2a 20 52 65 61 74 74 61 63 68 20 74 68 65 20  /* Reattach the 
3481d 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
3481e 74 6f 20 74 68 65 20 71 75 65 72 79 2e 0a 20 20  to the query..  
3481f 2a 2f 0a 20 20 70 2d 3e 70 4f 72 64 65 72 42 79  */.  p->pOrderBy
34820 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 70   = pOrderBy;.  p
34821 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20  Prior->pOrderBy 
34822 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
34823 74 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c  tDup(pParse->db,
34824 20 70 4f 72 64 65 72 42 79 2c 20 30 29 3b 0a 0a   pOrderBy, 0);..
34825 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20    /* Allocate a 
34826 72 61 6e 67 65 20 6f 66 20 74 65 6d 70 6f 72 61  range of tempora
34827 72 79 20 72 65 67 69 73 74 65 72 73 20 61 6e 64  ry registers and
34828 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 6e 65 65   the KeyInfo nee
34829 64 65 64 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65  ded.  ** for the
3482a 20 6c 6f 67 69 63 20 74 68 61 74 20 72 65 6d 6f   logic that remo
3482b 76 65 73 20 64 75 70 6c 69 63 61 74 65 20 72 65  ves duplicate re
3482c 73 75 6c 74 20 72 6f 77 73 20 77 68 65 6e 20 74  sult rows when t
3482d 68 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72  he.  ** operator
3482e 20 69 73 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50   is UNION, EXCEP
3482f 54 2c 20 6f 72 20 49 4e 54 45 52 53 45 43 54 20  T, or INTERSECT 
34830 28 62 75 74 20 6e 6f 74 20 55 4e 49 4f 4e 20 41  (but not UNION A
34831 4c 4c 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  LL)..  */.  if( 
34832 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20  op==TK_ALL ){.  
34833 20 20 72 65 67 50 72 65 76 20 3d 20 30 3b 0a 20    regPrev = 0;. 
34834 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
34835 6e 45 78 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73  nExpr = p->pELis
34836 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 61 73  t->nExpr;.    as
34837 73 65 72 74 28 20 6e 4f 72 64 65 72 42 79 3e 3d  sert( nOrderBy>=
34838 6e 45 78 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  nExpr || db->mal
34839 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
3483a 20 72 65 67 50 72 65 76 20 3d 20 73 71 6c 69 74   regPrev = sqlit
3483b 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
3483c 50 61 72 73 65 2c 20 6e 45 78 70 72 2b 31 29 3b  Parse, nExpr+1);
3483d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
3483e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
3483f 65 67 65 72 2c 20 30 2c 20 72 65 67 50 72 65 76  eger, 0, regPrev
34840 29 3b 0a 20 20 20 20 70 4b 65 79 44 75 70 20 3d  );.    pKeyDup =
34841 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
34842 5a 65 72 6f 28 64 62 2c 0a 20 20 20 20 20 20 20  Zero(db,.       
34843 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f             sizeo
34844 66 28 2a 70 4b 65 79 44 75 70 29 20 2b 20 6e 45  f(*pKeyDup) + nE
34845 78 70 72 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c  xpr*(sizeof(Coll
34846 53 65 71 2a 29 2b 31 29 20 29 3b 0a 20 20 20 20  Seq*)+1) );.    
34847 69 66 28 20 70 4b 65 79 44 75 70 20 29 7b 0a 20  if( pKeyDup ){. 
34848 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 61 53       pKeyDup->aS
34849 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29  ortOrder = (u8*)
3484a 26 70 4b 65 79 44 75 70 2d 3e 61 43 6f 6c 6c 5b  &pKeyDup->aColl[
3484b 6e 45 78 70 72 5d 3b 0a 20 20 20 20 20 20 70 4b  nExpr];.      pK
3484c 65 79 44 75 70 2d 3e 6e 46 69 65 6c 64 20 3d 20  eyDup->nField = 
3484d 28 75 31 36 29 6e 45 78 70 72 3b 0a 20 20 20 20  (u16)nExpr;.    
3484e 20 20 70 4b 65 79 44 75 70 2d 3e 65 6e 63 20 3d    pKeyDup->enc =
3484f 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 20 20   ENC(db);.      
34850 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72  for(i=0; i<nExpr
34851 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
34852 70 4b 65 79 44 75 70 2d 3e 61 43 6f 6c 6c 5b 69  pKeyDup->aColl[i
34853 5d 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74 43  ] = multiSelectC
34854 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
34855 2c 20 69 29 3b 0a 20 20 20 20 20 20 20 20 70 4b  , i);.        pK
34856 65 79 44 75 70 2d 3e 61 53 6f 72 74 4f 72 64 65  eyDup->aSortOrde
34857 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20  r[i] = 0;.      
34858 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 0a 20 20  }.    }.  }. .  
34859 2f 2a 20 53 65 70 61 72 61 74 65 20 74 68 65 20  /* Separate the 
3485a 6c 65 66 74 20 61 6e 64 20 74 68 65 20 72 69 67  left and the rig
3485b 68 74 20 71 75 65 72 79 20 66 72 6f 6d 20 6f 6e  ht query from on
3485c 65 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2f 0a 20  e another.  */. 
3485d 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a   p->pPrior = 0;.
3485e 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65    sqlite3Resolve
3485f 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70 50 61  OrderGroupBy(pPa
34860 72 73 65 2c 20 70 2c 20 70 2d 3e 70 4f 72 64 65  rse, p, p->pOrde
34861 72 42 79 2c 20 22 4f 52 44 45 52 22 29 3b 0a 20  rBy, "ORDER");. 
34862 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 50 72   if( pPrior->pPr
34863 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ior==0 ){.    sq
34864 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65  lite3ResolveOrde
34865 72 47 72 6f 75 70 42 79 28 70 50 61 72 73 65 2c  rGroupBy(pParse,
34866 20 70 50 72 69 6f 72 2c 20 70 50 72 69 6f 72 2d   pPrior, pPrior-
34867 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f 52 44 45  >pOrderBy, "ORDE
34868 52 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  R");.  }..  /* C
34869 6f 6d 70 75 74 65 20 74 68 65 20 6c 69 6d 69 74  ompute the limit
3486a 20 72 65 67 69 73 74 65 72 73 20 2a 2f 0a 20 20   registers */.  
3486b 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67 69  computeLimitRegi
3486c 73 74 65 72 73 28 70 50 61 72 73 65 2c 20 70 2c  sters(pParse, p,
3486d 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 69 66   labelEnd);.  if
3486e 28 20 70 2d 3e 69 4c 69 6d 69 74 20 26 26 20 6f  ( p->iLimit && o
3486f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20  p==TK_ALL ){.   
34870 20 72 65 67 4c 69 6d 69 74 41 20 3d 20 2b 2b 70   regLimitA = ++p
34871 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
34872 20 72 65 67 4c 69 6d 69 74 42 20 3d 20 2b 2b 70   regLimitB = ++p
34873 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
34874 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
34875 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 70  p2(v, OP_Copy, p
34876 2d 3e 69 4f 66 66 73 65 74 20 3f 20 70 2d 3e 69  ->iOffset ? p->i
34877 4f 66 66 73 65 74 2b 31 20 3a 20 70 2d 3e 69 4c  Offset+1 : p->iL
34878 69 6d 69 74 2c 0a 20 20 20 20 20 20 20 20 20 20  imit,.          
34879 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3487a 20 20 20 20 20 20 20 20 72 65 67 4c 69 6d 69 74          regLimit
3487b 41 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  A);.    sqlite3V
3487c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
3487d 43 6f 70 79 2c 20 72 65 67 4c 69 6d 69 74 41 2c  Copy, regLimitA,
3487e 20 72 65 67 4c 69 6d 69 74 42 29 3b 0a 20 20 7d   regLimitB);.  }
3487f 65 6c 73 65 7b 0a 20 20 20 20 72 65 67 4c 69 6d  else{.    regLim
34880 69 74 41 20 3d 20 72 65 67 4c 69 6d 69 74 42 20  itA = regLimitB 
34881 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 0;.  }.  sqlit
34882 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
34883 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 70   p->pLimit);.  p
34884 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->pLimit = 0;.  
34885 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
34886 65 28 64 62 2c 20 70 2d 3e 70 4f 66 66 73 65 74  e(db, p->pOffset
34887 29 3b 0a 20 20 70 2d 3e 70 4f 66 66 73 65 74 20  );.  p->pOffset 
34888 3d 20 30 3b 0a 0a 20 20 72 65 67 41 64 64 72 41  = 0;..  regAddrA
34889 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
3488a 6d 3b 0a 20 20 72 65 67 45 6f 66 41 20 3d 20 2b  m;.  regEofA = +
3488b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
3488c 20 72 65 67 41 64 64 72 42 20 3d 20 2b 2b 70 50   regAddrB = ++pP
3488d 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65  arse->nMem;.  re
3488e 67 45 6f 66 42 20 3d 20 2b 2b 70 50 61 72 73 65  gEofB = ++pParse
3488f 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f 75 74  ->nMem;.  regOut
34890 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  A = ++pParse->nM
34891 65 6d 3b 0a 20 20 72 65 67 4f 75 74 42 20 3d 20  em;.  regOutB = 
34892 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
34893 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
34894 65 73 74 49 6e 69 74 28 26 64 65 73 74 41 2c 20  estInit(&destA, 
34895 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 72  SRT_Coroutine, r
34896 65 67 41 64 64 72 41 29 3b 0a 20 20 73 71 6c 69  egAddrA);.  sqli
34897 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69  te3SelectDestIni
34898 74 28 26 64 65 73 74 42 2c 20 53 52 54 5f 43 6f  t(&destB, SRT_Co
34899 72 6f 75 74 69 6e 65 2c 20 72 65 67 41 64 64 72  routine, regAddr
3489a 42 29 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 70  B);..  /* Jump p
3489b 61 73 74 20 74 68 65 20 76 61 72 69 6f 75 73 20  ast the various 
3489c 73 75 62 72 6f 75 74 69 6e 65 73 20 61 6e 64 20  subroutines and 
3489d 63 6f 72 6f 75 74 69 6e 65 73 20 74 6f 20 74 68  coroutines to th
3489e 65 20 6d 61 69 6e 0a 20 20 2a 2a 20 6d 65 72 67  e main.  ** merg
3489f 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20 20 6a 31  e loop.  */.  j1
348a0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
348a1 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29  dOp0(v, OP_Goto)
348a2 3b 0a 20 20 61 64 64 72 53 65 6c 65 63 74 41 20  ;.  addrSelectA 
348a3 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
348a4 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 0a 0a 20  rentAddr(v);... 
348a5 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 63   /* Generate a c
348a6 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65 76 61 6c  oroutine to eval
348a7 75 61 74 65 20 74 68 65 20 53 45 4c 45 43 54 20  uate the SELECT 
348a8 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 74 68 65  statement to the
348a9 0a 20 20 2a 2a 20 6c 65 66 74 20 6f 66 20 74 68  .  ** left of th
348aa 65 20 63 6f 6d 70 6f 75 6e 64 20 6f 70 65 72 61  e compound opera
348ab 74 6f 72 20 2d 20 74 68 65 20 22 41 22 20 73 65  tor - the "A" se
348ac 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 56 64 62  lect..  */.  Vdb
348ad 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
348ae 20 22 42 65 67 69 6e 20 63 6f 72 6f 75 74 69 6e   "Begin coroutin
348af 65 20 66 6f 72 20 6c 65 66 74 20 53 45 4c 45 43  e for left SELEC
348b0 54 22 29 29 3b 0a 20 20 70 50 72 69 6f 72 2d 3e  T"));.  pPrior->
348b1 69 4c 69 6d 69 74 20 3d 20 72 65 67 4c 69 6d 69  iLimit = regLimi
348b2 74 41 3b 0a 20 20 65 78 70 6c 61 69 6e 53 65 74  tA;.  explainSet
348b3 49 6e 74 65 67 65 72 28 69 53 75 62 31 2c 20 70  Integer(iSub1, p
348b4 50 61 72 73 65 2d 3e 69 4e 65 78 74 53 65 6c 65  Parse->iNextSele
348b5 63 74 49 64 29 3b 0a 20 20 73 71 6c 69 74 65 33  ctId);.  sqlite3
348b6 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
348b7 50 72 69 6f 72 2c 20 26 64 65 73 74 41 29 3b 0a  Prior, &destA);.
348b8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
348b9 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
348ba 72 2c 20 31 2c 20 72 65 67 45 6f 66 41 29 3b 0a  r, 1, regEofA);.
348bb 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
348bc 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
348bd 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 56 64   regAddrA);.  Vd
348be 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
348bf 2c 20 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65  , "End coroutine
348c0 20 66 6f 72 20 6c 65 66 74 20 53 45 4c 45 43 54   for left SELECT
348c1 22 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  "));..  /* Gener
348c2 61 74 65 20 61 20 63 6f 72 6f 75 74 69 6e 65 20  ate a coroutine 
348c3 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20  to evaluate the 
348c4 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
348c5 20 6f 6e 20 0a 20 20 2a 2a 20 74 68 65 20 72 69   on .  ** the ri
348c6 67 68 74 20 2d 20 74 68 65 20 22 42 22 20 73 65  ght - the "B" se
348c7 6c 65 63 74 0a 20 20 2a 2f 0a 20 20 61 64 64 72  lect.  */.  addr
348c8 53 65 6c 65 63 74 42 20 3d 20 73 71 6c 69 74 65  SelectB = sqlite
348c9 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
348ca 28 76 29 3b 0a 20 20 56 64 62 65 4e 6f 6f 70 43  (v);.  VdbeNoopC
348cb 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 42 65 67 69  omment((v, "Begi
348cc 6e 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20  n coroutine for 
348cd 72 69 67 68 74 20 53 45 4c 45 43 54 22 29 29 3b  right SELECT"));
348ce 0a 20 20 73 61 76 65 64 4c 69 6d 69 74 20 3d 20  .  savedLimit = 
348cf 70 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 73 61 76  p->iLimit;.  sav
348d0 65 64 4f 66 66 73 65 74 20 3d 20 70 2d 3e 69 4f  edOffset = p->iO
348d1 66 66 73 65 74 3b 0a 20 20 70 2d 3e 69 4c 69 6d  ffset;.  p->iLim
348d2 69 74 20 3d 20 72 65 67 4c 69 6d 69 74 42 3b 0a  it = regLimitB;.
348d3 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30    p->iOffset = 0
348d4 3b 20 20 0a 20 20 65 78 70 6c 61 69 6e 53 65 74  ;  .  explainSet
348d5 49 6e 74 65 67 65 72 28 69 53 75 62 32 2c 20 70  Integer(iSub2, p
348d6 50 61 72 73 65 2d 3e 69 4e 65 78 74 53 65 6c 65  Parse->iNextSele
348d7 63 74 49 64 29 3b 0a 20 20 73 71 6c 69 74 65 33  ctId);.  sqlite3
348d8 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
348d9 2c 20 26 64 65 73 74 42 29 3b 0a 20 20 70 2d 3e  , &destB);.  p->
348da 69 4c 69 6d 69 74 20 3d 20 73 61 76 65 64 4c 69  iLimit = savedLi
348db 6d 69 74 3b 0a 20 20 70 2d 3e 69 4f 66 66 73 65  mit;.  p->iOffse
348dc 74 20 3d 20 73 61 76 65 64 4f 66 66 73 65 74 3b  t = savedOffset;
348dd 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
348de 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
348df 65 72 2c 20 31 2c 20 72 65 67 45 6f 66 42 29 3b  er, 1, regEofB);
348e0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
348e1 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
348e2 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 20 20 56  , regAddrB);.  V
348e3 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
348e4 76 2c 20 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e  v, "End coroutin
348e5 65 20 66 6f 72 20 72 69 67 68 74 20 53 45 4c 45  e for right SELE
348e6 43 54 22 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e  CT"));..  /* Gen
348e7 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69  erate a subrouti
348e8 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74 73 20  ne that outputs 
348e9 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
348ea 6f 66 20 74 68 65 20 41 0a 20 20 2a 2a 20 73 65  of the A.  ** se
348eb 6c 65 63 74 20 61 73 20 74 68 65 20 6e 65 78 74  lect as the next
348ec 20 6f 75 74 70 75 74 20 72 6f 77 20 6f 66 20 74   output row of t
348ed 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65  he compound sele
348ee 63 74 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e  ct..  */.  VdbeN
348ef 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
348f0 4f 75 74 70 75 74 20 72 6f 75 74 69 6e 65 20 66  Output routine f
348f1 6f 72 20 41 22 29 29 3b 0a 20 20 61 64 64 72 4f  or A"));.  addrO
348f2 75 74 41 20 3d 20 67 65 6e 65 72 61 74 65 4f 75  utA = generateOu
348f3 74 70 75 74 53 75 62 72 6f 75 74 69 6e 65 28 70  tputSubroutine(p
348f4 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20  Parse,.         
348f5 20 20 20 20 20 20 20 20 70 2c 20 26 64 65 73 74          p, &dest
348f6 41 2c 20 70 44 65 73 74 2c 20 72 65 67 4f 75 74  A, pDest, regOut
348f7 41 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  A,.             
348f8 20 20 20 20 72 65 67 50 72 65 76 2c 20 70 4b 65      regPrev, pKe
348f9 79 44 75 70 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  yDup, P4_KEYINFO
348fa 5f 48 41 4e 44 4f 46 46 2c 20 6c 61 62 65 6c 45  _HANDOFF, labelE
348fb 6e 64 29 3b 0a 20 20 0a 20 20 2f 2a 20 47 65 6e  nd);.  .  /* Gen
348fc 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69  erate a subrouti
348fd 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74 73 20  ne that outputs 
348fe 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
348ff 6f 66 20 74 68 65 20 42 0a 20 20 2a 2a 20 73 65  of the B.  ** se
34900 6c 65 63 74 20 61 73 20 74 68 65 20 6e 65 78 74  lect as the next
34901 20 6f 75 74 70 75 74 20 72 6f 77 20 6f 66 20 74   output row of t
34902 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65  he compound sele
34903 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f  ct..  */.  if( o
34904 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20 6f 70 3d  p==TK_ALL || op=
34905 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b 0a 20 20 20  =TK_UNION ){.   
34906 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
34907 28 28 76 2c 20 22 4f 75 74 70 75 74 20 72 6f 75  ((v, "Output rou
34908 74 69 6e 65 20 66 6f 72 20 42 22 29 29 3b 0a 20  tine for B"));. 
34909 20 20 20 61 64 64 72 4f 75 74 42 20 3d 20 67 65     addrOutB = ge
3490a 6e 65 72 61 74 65 4f 75 74 70 75 74 53 75 62 72  nerateOutputSubr
3490b 6f 75 74 69 6e 65 28 70 50 61 72 73 65 2c 0a 20  outine(pParse,. 
3490c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3490d 70 2c 20 26 64 65 73 74 42 2c 20 70 44 65 73 74  p, &destB, pDest
3490e 2c 20 72 65 67 4f 75 74 42 2c 0a 20 20 20 20 20  , regOutB,.     
3490f 20 20 20 20 20 20 20 20 20 20 20 20 72 65 67 50              regP
34910 72 65 76 2c 20 70 4b 65 79 44 75 70 2c 20 50 34  rev, pKeyDup, P4
34911 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54 49 43 2c  _KEYINFO_STATIC,
34912 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 0a   labelEnd);.  }.
34913 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  .  /* Generate a
34914 20 73 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 72   subroutine to r
34915 75 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73 75  un when the resu
34916 6c 74 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 20  lts from select 
34917 41 0a 20 20 2a 2a 20 61 72 65 20 65 78 68 61 75  A.  ** are exhau
34918 73 74 65 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61  sted and only da
34919 74 61 20 69 6e 20 73 65 6c 65 63 74 20 42 20 72  ta in select B r
3491a 65 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 56  emains..  */.  V
3491b 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
3491c 76 2c 20 22 65 6f 66 2d 41 20 73 75 62 72 6f 75  v, "eof-A subrou
3491d 74 69 6e 65 22 29 29 3b 0a 20 20 69 66 28 20 6f  tine"));.  if( o
3491e 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20 7c 7c 20  p==TK_EXCEPT || 
3491f 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54  op==TK_INTERSECT
34920 20 29 7b 0a 20 20 20 20 61 64 64 72 45 6f 66 41   ){.    addrEofA
34921 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
34922 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
34923 20 30 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20   0, labelEnd);. 
34924 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 61 64   }else{  .    ad
34925 64 72 45 6f 66 41 20 3d 20 73 71 6c 69 74 65 33  drEofA = sqlite3
34926 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
34927 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c 20 6c 61  _If, regEofB, la
34928 62 65 6c 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c  belEnd);.    sql
34929 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
3492a 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f  , OP_Gosub, regO
3492b 75 74 42 2c 20 61 64 64 72 4f 75 74 42 29 3b 0a  utB, addrOutB);.
3492c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
3492d 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
3492e 64 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 20 20  d, regAddrB);.  
3492f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
34930 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
34931 30 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20  0, addrEofA);.  
34932 20 20 70 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 20    p->nSelectRow 
34933 2b 3d 20 70 50 72 69 6f 72 2d 3e 6e 53 65 6c 65  += pPrior->nSele
34934 63 74 52 6f 77 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ctRow;.  }..  /*
34935 20 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72   Generate a subr
34936 6f 75 74 69 6e 65 20 74 6f 20 72 75 6e 20 77 68  outine to run wh
34937 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 66  en the results f
34938 72 6f 6d 20 73 65 6c 65 63 74 20 42 0a 20 20 2a  rom select B.  *
34939 2a 20 61 72 65 20 65 78 68 61 75 73 74 65 64 20  * are exhausted 
3493a 61 6e 64 20 6f 6e 6c 79 20 64 61 74 61 20 69 6e  and only data in
3493b 20 73 65 6c 65 63 74 20 41 20 72 65 6d 61 69 6e   select A remain
3493c 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70  s..  */.  if( op
3493d 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29  ==TK_INTERSECT )
3493e 7b 0a 20 20 20 20 61 64 64 72 45 6f 66 42 20 3d  {.    addrEofB =
3493f 20 61 64 64 72 45 6f 66 41 3b 0a 20 20 20 20 69   addrEofA;.    i
34940 66 28 20 70 2d 3e 6e 53 65 6c 65 63 74 52 6f 77  f( p->nSelectRow
34941 20 3e 20 70 50 72 69 6f 72 2d 3e 6e 53 65 6c 65   > pPrior->nSele
34942 63 74 52 6f 77 20 29 20 70 2d 3e 6e 53 65 6c 65  ctRow ) p->nSele
34943 63 74 52 6f 77 20 3d 20 70 50 72 69 6f 72 2d 3e  ctRow = pPrior->
34944 6e 53 65 6c 65 63 74 52 6f 77 3b 0a 20 20 7d 65  nSelectRow;.  }e
34945 6c 73 65 7b 20 20 0a 20 20 20 20 56 64 62 65 4e  lse{  .    VdbeN
34946 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
34947 65 6f 66 2d 42 20 73 75 62 72 6f 75 74 69 6e 65  eof-B subroutine
34948 22 29 29 3b 0a 20 20 20 20 61 64 64 72 45 6f 66  "));.    addrEof
34949 42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  B = sqlite3VdbeA
3494a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp2(v, OP_If, 
3494b 72 65 67 45 6f 66 41 2c 20 6c 61 62 65 6c 45 6e  regEofA, labelEn
3494c 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  d);.    sqlite3V
3494d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
3494e 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 41 2c 20  Gosub, regOutA, 
3494f 61 64 64 72 4f 75 74 41 29 3b 0a 20 20 20 20 73  addrOutA);.    s
34950 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
34951 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65  (v, OP_Yield, re
34952 67 41 64 64 72 41 29 3b 0a 20 20 20 20 73 71 6c  gAddrA);.    sql
34953 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
34954 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64  , OP_Goto, 0, ad
34955 64 72 45 6f 66 42 29 3b 0a 20 20 7d 0a 0a 20 20  drEofB);.  }..  
34956 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
34957 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 63   to handle the c
34958 61 73 65 20 6f 66 20 41 3c 42 0a 20 20 2a 2f 0a  ase of A<B.  */.
34959 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e    VdbeNoopCommen
3495a 74 28 28 76 2c 20 22 41 2d 6c 74 2d 42 20 73 75  t((v, "A-lt-B su
3495b 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 61  broutine"));.  a
3495c 64 64 72 41 6c 74 42 20 3d 20 73 71 6c 69 74 65  ddrAltB = sqlite
3495d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
3495e 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 41  P_Gosub, regOutA
3495f 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a 20 20 73  , addrOutA);.  s
34960 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
34961 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65  (v, OP_Yield, re
34962 67 41 64 64 72 41 29 3b 0a 20 20 73 71 6c 69 74  gAddrA);.  sqlit
34963 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
34964 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 41 2c 20  OP_If, regEofA, 
34965 61 64 64 72 45 6f 66 41 29 3b 0a 20 20 73 71 6c  addrEofA);.  sql
34966 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
34967 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61  , OP_Goto, 0, la
34968 62 65 6c 43 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20  belCmpr);..  /* 
34969 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
3496a 20 68 61 6e 64 6c 65 20 74 68 65 20 63 61 73 65   handle the case
3496b 20 6f 66 20 41 3d 3d 42 0a 20 20 2a 2f 0a 20 20   of A==B.  */.  
3496c 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29  if( op==TK_ALL )
3496d 7b 0a 20 20 20 20 61 64 64 72 41 65 71 42 20 3d  {.    addrAeqB =
3496e 20 61 64 64 72 41 6c 74 42 3b 0a 20 20 7d 65 6c   addrAltB;.  }el
3496f 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e  se if( op==TK_IN
34970 54 45 52 53 45 43 54 20 29 7b 0a 20 20 20 20 61  TERSECT ){.    a
34971 64 64 72 41 65 71 42 20 3d 20 61 64 64 72 41 6c  ddrAeqB = addrAl
34972 74 42 3b 0a 20 20 20 20 61 64 64 72 41 6c 74 42  tB;.    addrAltB
34973 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ++;.  }else{.   
34974 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
34975 28 28 76 2c 20 22 41 2d 65 71 2d 42 20 73 75 62  ((v, "A-eq-B sub
34976 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 20 20  routine"));.    
34977 61 64 64 72 41 65 71 42 20 3d 0a 20 20 20 20 73  addrAeqB =.    s
34978 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
34979 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65  (v, OP_Yield, re
3497a 67 41 64 64 72 41 29 3b 0a 20 20 20 20 73 71 6c  gAddrA);.    sql
3497b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
3497c 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 41  , OP_If, regEofA
3497d 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20 20  , addrEofA);.   
3497e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
3497f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
34980 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 20 20  , labelCmpr);.  
34981 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  }..  /* Generate
34982 20 63 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65 20   code to handle 
34983 74 68 65 20 63 61 73 65 20 6f 66 20 41 3e 42 0a  the case of A>B.
34984 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43    */.  VdbeNoopC
34985 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41 2d 67 74  omment((v, "A-gt
34986 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29  -B subroutine"))
34987 3b 0a 20 20 61 64 64 72 41 67 74 42 20 3d 20 73  ;.  addrAgtB = s
34988 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
34989 74 41 64 64 72 28 76 29 3b 0a 20 20 69 66 28 20  tAddr(v);.  if( 
3498a 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20 6f 70  op==TK_ALL || op
3498b 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b 0a 20 20  ==TK_UNION ){.  
3498c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3498d 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c  Op2(v, OP_Gosub,
3498e 20 72 65 67 4f 75 74 42 2c 20 61 64 64 72 4f 75   regOutB, addrOu
3498f 74 42 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  tB);.  }.  sqlit
34990 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
34991 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67 41 64 64  OP_Yield, regAdd
34992 72 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  rB);.  sqlite3Vd
34993 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
34994 66 2c 20 72 65 67 45 6f 66 42 2c 20 61 64 64 72  f, regEofB, addr
34995 45 6f 66 42 29 3b 0a 20 20 73 71 6c 69 74 65 33  EofB);.  sqlite3
34996 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
34997 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61 62 65 6c 43  _Goto, 0, labelC
34998 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  mpr);..  /* This
34999 20 63 6f 64 65 20 72 75 6e 73 20 6f 6e 63 65 20   code runs once 
3499a 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 65 76  to initialize ev
3499b 65 72 79 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20  erything..  */. 
3499c 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
3499d 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 73  Here(v, j1);.  s
3499e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
3499f 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
349a0 30 2c 20 72 65 67 45 6f 66 41 29 3b 0a 20 20 73  0, regEofA);.  s
349a1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
349a2 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
349a3 30 2c 20 72 65 67 45 6f 66 42 29 3b 0a 20 20 73  0, regEofB);.  s
349a4 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
349a5 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65  (v, OP_Gosub, re
349a6 67 41 64 64 72 41 2c 20 61 64 64 72 53 65 6c 65  gAddrA, addrSele
349a7 63 74 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ctA);.  sqlite3V
349a8 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
349a9 47 6f 73 75 62 2c 20 72 65 67 41 64 64 72 42 2c  Gosub, regAddrB,
349aa 20 61 64 64 72 53 65 6c 65 63 74 42 29 3b 0a 20   addrSelectB);. 
349ab 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
349ac 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67  p2(v, OP_If, reg
349ad 45 6f 66 41 2c 20 61 64 64 72 45 6f 66 41 29 3b  EofA, addrEofA);
349ae 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
349af 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72  dOp2(v, OP_If, r
349b0 65 67 45 6f 66 42 2c 20 61 64 64 72 45 6f 66 42  egEofB, addrEofB
349b1 29 3b 0a 0a 20 20 2f 2a 20 49 6d 70 6c 65 6d 65  );..  /* Impleme
349b2 6e 74 20 74 68 65 20 6d 61 69 6e 20 6d 65 72 67  nt the main merg
349b3 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20 20 73 71  e loop.  */.  sq
349b4 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
349b5 4c 61 62 65 6c 28 76 2c 20 6c 61 62 65 6c 43 6d  Label(v, labelCm
349b6 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  pr);.  sqlite3Vd
349b7 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50  beAddOp4(v, OP_P
349b8 65 72 6d 75 74 61 74 69 6f 6e 2c 20 30 2c 20 30  ermutation, 0, 0
349b9 2c 20 30 2c 20 28 63 68 61 72 2a 29 61 50 65 72  , 0, (char*)aPer
349ba 6d 75 74 65 2c 20 50 34 5f 49 4e 54 41 52 52 41  mute, P4_INTARRA
349bb 59 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  Y);.  sqlite3Vdb
349bc 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f  eAddOp4(v, OP_Co
349bd 6d 70 61 72 65 2c 20 64 65 73 74 41 2e 69 53 64  mpare, destA.iSd
349be 73 74 2c 20 64 65 73 74 42 2e 69 53 64 73 74 2c  st, destB.iSdst,
349bf 20 6e 4f 72 64 65 72 42 79 2c 0a 20 20 20 20 20   nOrderBy,.     
349c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
349c1 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 4d      (char*)pKeyM
349c2 65 72 67 65 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  erge, P4_KEYINFO
349c3 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 73 71 6c  _HANDOFF);.  sql
349c4 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
349c5 2c 20 4f 50 5f 4a 75 6d 70 2c 20 61 64 64 72 41  , OP_Jump, addrA
349c6 6c 74 42 2c 20 61 64 64 72 41 65 71 42 2c 20 61  ltB, addrAeqB, a
349c7 64 64 72 41 67 74 42 29 3b 0a 0a 20 20 2f 2a 20  ddrAgtB);..  /* 
349c8 52 65 6c 65 61 73 65 20 74 65 6d 70 6f 72 61 72  Release temporar
349c9 79 20 72 65 67 69 73 74 65 72 73 0a 20 20 2a 2f  y registers.  */
349ca 0a 20 20 69 66 28 20 72 65 67 50 72 65 76 20 29  .  if( regPrev )
349cb 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c  {.    sqlite3Rel
349cc 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
349cd 61 72 73 65 2c 20 72 65 67 50 72 65 76 2c 20 6e  arse, regPrev, n
349ce 4f 72 64 65 72 42 79 2b 31 29 3b 0a 20 20 7d 0a  OrderBy+1);.  }.
349cf 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68  .  /* Jump to th
349d0 65 20 74 68 69 73 20 70 6f 69 6e 74 20 69 6e 20  e this point in 
349d1 6f 72 64 65 72 20 74 6f 20 74 65 72 6d 69 6e 61  order to termina
349d2 74 65 20 74 68 65 20 71 75 65 72 79 2e 0a 20 20  te the query..  
349d3 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
349d4 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
349d5 6c 61 62 65 6c 45 6e 64 29 3b 0a 0a 20 20 2f 2a  labelEnd);..  /*
349d6 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20   Set the number 
349d7 6f 66 20 6f 75 74 70 75 74 20 63 6f 6c 75 6d 6e  of output column
349d8 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 65  s.  */.  if( pDe
349d9 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 4f  st->eDest==SRT_O
349da 75 74 70 75 74 20 29 7b 0a 20 20 20 20 53 65 6c  utput ){.    Sel
349db 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20 70 50  ect *pFirst = pP
349dc 72 69 6f 72 3b 0a 20 20 20 20 77 68 69 6c 65 28  rior;.    while(
349dd 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 20   pFirst->pPrior 
349de 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72 73  ) pFirst = pFirs
349df 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 67  t->pPrior;.    g
349e0 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d  enerateColumnNam
349e1 65 73 28 70 50 61 72 73 65 2c 20 30 2c 20 70 46  es(pParse, 0, pF
349e2 69 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  irst->pEList);. 
349e3 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 73 73 65 6d   }..  /* Reassem
349e4 62 6c 79 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64  bly the compound
349e5 20 71 75 65 72 79 20 73 6f 20 74 68 61 74 20 69   query so that i
349e6 74 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20  t will be freed 
349e7 63 6f 72 72 65 63 74 6c 79 0a 20 20 2a 2a 20 62  correctly.  ** b
349e8 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75  y the calling fu
349e9 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20  nction */.  if( 
349ea 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20  p->pPrior ){.   
349eb 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
349ec 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 50 72 69  lete(db, p->pPri
349ed 6f 72 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 50  or);.  }.  p->pP
349ee 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a 0a  rior = pPrior;..
349ef 20 20 2f 2a 2a 2a 20 54 42 44 3a 20 20 49 6e 73    /*** TBD:  Ins
349f0 65 72 74 20 73 75 62 72 6f 75 74 69 6e 65 20 63  ert subroutine c
349f1 61 6c 6c 73 20 74 6f 20 63 6c 6f 73 65 20 63 75  alls to close cu
349f2 72 73 6f 72 73 20 6f 6e 20 69 6e 63 6f 6d 70 6c  rsors on incompl
349f3 65 74 65 0a 20 20 2a 2a 2a 2a 20 73 75 62 71 75  ete.  **** subqu
349f4 65 72 69 65 73 20 2a 2a 2a 2a 2f 0a 20 20 65 78  eries ****/.  ex
349f5 70 6c 61 69 6e 43 6f 6d 70 6f 73 69 74 65 28 70  plainComposite(p
349f6 50 61 72 73 65 2c 20 70 2d 3e 6f 70 2c 20 69 53  Parse, p->op, iS
349f7 75 62 31 2c 20 69 53 75 62 32 2c 20 30 29 3b 0a  ub1, iSub2, 0);.
349f8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
349f9 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  OK;.}.#endif..#i
349fa 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
349fb 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
349fc 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
349fd 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f  ITE_OMIT_VIEW)./
349fe 2a 20 46 6f 72 77 61 72 64 20 44 65 63 6c 61 72  * Forward Declar
349ff 61 74 69 6f 6e 73 20 2a 2f 0a 73 74 61 74 69 63  ations */.static
34a00 20 76 6f 69 64 20 73 75 62 73 74 45 78 70 72 4c   void substExprL
34a01 69 73 74 28 73 71 6c 69 74 65 33 2a 2c 20 45 78  ist(sqlite3*, Ex
34a02 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 45 78  prList*, int, Ex
34a03 70 72 4c 69 73 74 2a 29 3b 0a 73 74 61 74 69 63  prList*);.static
34a04 20 76 6f 69 64 20 73 75 62 73 74 53 65 6c 65 63   void substSelec
34a05 74 28 73 71 6c 69 74 65 33 2a 2c 20 53 65 6c 65  t(sqlite3*, Sele
34a06 63 74 20 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c  ct *, int, ExprL
34a07 69 73 74 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53  ist *);../*.** S
34a08 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20  can through the 
34a09 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72  expression pExpr
34a0a 2e 20 20 52 65 70 6c 61 63 65 20 65 76 65 72 79  .  Replace every
34a0b 20 72 65 66 65 72 65 6e 63 65 20 74 6f 0a 2a 2a   reference to.**
34a0c 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62   a column in tab
34a0d 6c 65 20 6e 75 6d 62 65 72 20 69 54 61 62 6c 65  le number iTable
34a0e 20 77 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20   with a copy of 
34a0f 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 0a 2a  the iColumn-th.*
34a10 2a 20 65 6e 74 72 79 20 69 6e 20 70 45 4c 69 73  * entry in pELis
34a11 74 2e 20 20 28 42 75 74 20 6c 65 61 76 65 20 72  t.  (But leave r
34a12 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65  eferences to the
34a13 20 52 4f 57 49 44 20 63 6f 6c 75 6d 6e 20 0a 2a   ROWID column .*
34a14 2a 20 75 6e 63 68 61 6e 67 65 64 2e 29 0a 2a 2a  * unchanged.).**
34a15 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
34a16 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   is part of the 
34a17 66 6c 61 74 74 65 6e 69 6e 67 20 70 72 6f 63 65  flattening proce
34a18 64 75 72 65 2e 20 20 41 20 73 75 62 71 75 65 72  dure.  A subquer
34a19 79 0a 2a 2a 20 77 68 6f 73 65 20 72 65 73 75 6c  y.** whose resul
34a1a 74 20 73 65 74 20 69 73 20 64 65 66 69 6e 65 64  t set is defined
34a1b 20 62 79 20 70 45 4c 69 73 74 20 61 70 70 65 61   by pEList appea
34a1c 72 73 20 61 73 20 65 6e 74 72 79 20 69 6e 20 74  rs as entry in t
34a1d 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73  he.** FROM claus
34a1e 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 75  e of a SELECT su
34a1f 63 68 20 74 68 61 74 20 74 68 65 20 56 44 42 45  ch that the VDBE
34a20 20 63 75 72 73 6f 72 20 61 73 73 69 67 6e 65 64   cursor assigned
34a21 20 74 6f 20 74 68 61 74 0a 2a 2a 20 46 4f 52 4d   to that.** FORM
34a22 20 63 6c 61 75 73 65 20 65 6e 74 72 79 20 69 73   clause entry is
34a23 20 69 54 61 62 6c 65 2e 20 20 54 68 69 73 20 72   iTable.  This r
34a24 6f 75 74 69 6e 65 20 6d 61 6b 65 20 74 68 65 20  outine make the 
34a25 6e 65 63 65 73 73 61 72 79 20 0a 2a 2a 20 63 68  necessary .** ch
34a26 61 6e 67 65 73 20 74 6f 20 70 45 78 70 72 20 73  anges to pExpr s
34a27 6f 20 74 68 61 74 20 69 74 20 72 65 66 65 72 73  o that it refers
34a28 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68 65   directly to the
34a29 20 73 6f 75 72 63 65 20 74 61 62 6c 65 0a 2a 2a   source table.**
34a2a 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79   of the subquery
34a2b 20 72 61 74 68 65 72 20 74 68 65 20 72 65 73 75   rather the resu
34a2c 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 73 75  lt set of the su
34a2d 62 71 75 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  bquery..*/.stati
34a2e 63 20 45 78 70 72 20 2a 73 75 62 73 74 45 78 70  c Expr *substExp
34a2f 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  r(.  sqlite3 *db
34a30 2c 20 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6f  ,        /* Repo
34a31 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73  rt malloc errors
34a32 20 74 6f 20 74 68 69 73 20 63 6f 6e 6e 65 63 74   to this connect
34a33 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ion */.  Expr *p
34a34 45 78 70 72 2c 20 20 20 20 20 20 20 20 2f 2a 20  Expr,        /* 
34a35 45 78 70 72 20 69 6e 20 77 68 69 63 68 20 73 75  Expr in which su
34a36 62 73 74 69 74 75 74 69 6f 6e 20 6f 63 63 75 72  bstitution occur
34a37 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c  s */.  int iTabl
34a38 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61  e,         /* Ta
34a39 62 6c 65 20 74 6f 20 62 65 20 73 75 62 73 74 69  ble to be substi
34a3a 74 75 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c  tuted */.  ExprL
34a3b 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f  ist *pEList    /
34a3c 2a 20 53 75 62 73 74 69 74 75 74 65 20 65 78 70  * Substitute exp
34a3d 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20  ressions */.){. 
34a3e 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20   if( pExpr==0 ) 
34a3f 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
34a40 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  pExpr->op==TK_CO
34a41 4c 55 4d 4e 20 26 26 20 70 45 78 70 72 2d 3e 69  LUMN && pExpr->i
34a42 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 29 7b  Table==iTable ){
34a43 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e  .    if( pExpr->
34a44 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20  iColumn<0 ){.   
34a45 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
34a46 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20 7d 65 6c 73  K_NULL;.    }els
34a47 65 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  e{.      Expr *p
34a48 4e 65 77 3b 0a 20 20 20 20 20 20 61 73 73 65 72  New;.      asser
34a49 74 28 20 70 45 4c 69 73 74 21 3d 30 20 26 26 20  t( pEList!=0 && 
34a4a 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70  pExpr->iColumn<p
34a4b 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a  EList->nExpr );.
34a4c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
34a4d 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26  xpr->pLeft==0 &&
34a4e 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d   pExpr->pRight==
34a4f 30 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 20  0 );.      pNew 
34a50 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
34a51 28 64 62 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 70  (db, pEList->a[p
34a52 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 70  Expr->iColumn].p
34a53 45 78 70 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  Expr, 0);.      
34a54 69 66 28 20 70 4e 65 77 20 26 26 20 70 45 78 70  if( pNew && pExp
34a55 72 2d 3e 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  r->pColl ){.    
34a56 20 20 20 20 70 4e 65 77 2d 3e 70 43 6f 6c 6c 20      pNew->pColl 
34a57 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a  = pExpr->pColl;.
34a58 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
34a59 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
34a5a 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20  db, pExpr);.    
34a5b 20 20 70 45 78 70 72 20 3d 20 70 4e 65 77 3b 0a    pExpr = pNew;.
34a5c 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
34a5d 20 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20     pExpr->pLeft 
34a5e 3d 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20  = substExpr(db, 
34a5f 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 69 54  pExpr->pLeft, iT
34a60 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20  able, pEList);. 
34a61 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74     pExpr->pRight
34a62 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62 2c   = substExpr(db,
34a63 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
34a64 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
34a65 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73  .    if( ExprHas
34a66 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
34a67 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
34a68 0a 20 20 20 20 20 20 73 75 62 73 74 53 65 6c 65  .      substSele
34a69 63 74 28 64 62 2c 20 70 45 78 70 72 2d 3e 78 2e  ct(db, pExpr->x.
34a6a 70 53 65 6c 65 63 74 2c 20 69 54 61 62 6c 65 2c  pSelect, iTable,
34a6b 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d 65   pEList);.    }e
34a6c 6c 73 65 7b 0a 20 20 20 20 20 20 73 75 62 73 74  lse{.      subst
34a6d 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 45 78  ExprList(db, pEx
34a6e 70 72 2d 3e 78 2e 70 4c 69 73 74 2c 20 69 54 61  pr->x.pList, iTa
34a6f 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
34a70 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
34a71 20 70 45 78 70 72 3b 0a 7d 0a 73 74 61 74 69 63   pExpr;.}.static
34a72 20 76 6f 69 64 20 73 75 62 73 74 45 78 70 72 4c   void substExprL
34a73 69 73 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ist(.  sqlite3 *
34a74 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52  db,         /* R
34a75 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72  eport malloc err
34a76 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20 45 78  ors here */.  Ex
34a77 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20  prList *pList,  
34a78 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 73 63     /* List to sc
34a79 61 6e 20 61 6e 64 20 69 6e 20 77 68 69 63 68 20  an and in which 
34a7a 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69 74 75  to make substitu
34a7b 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  tes */.  int iTa
34a7c 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ble,          /*
34a7d 20 54 61 62 6c 65 20 74 6f 20 62 65 20 73 75 62   Table to be sub
34a7e 73 74 69 74 75 74 65 64 20 2a 2f 0a 20 20 45 78  stituted */.  Ex
34a7f 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20  prList *pEList  
34a80 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65     /* Substitute
34a81 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20 20   values */.){.  
34a82 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69  int i;.  if( pLi
34a83 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  st==0 ) return;.
34a84 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69    for(i=0; i<pLi
34a85 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
34a86 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d  .    pList->a[i]
34a87 2e 70 45 78 70 72 20 3d 20 73 75 62 73 74 45 78  .pExpr = substEx
34a88 70 72 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b  pr(db, pList->a[
34a89 69 5d 2e 70 45 78 70 72 2c 20 69 54 61 62 6c 65  i].pExpr, iTable
34a8a 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 7d  , pEList);.  }.}
34a8b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 62  .static void sub
34a8c 73 74 53 65 6c 65 63 74 28 0a 20 20 73 71 6c 69  stSelect(.  sqli
34a8d 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
34a8e 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f   /* Report mallo
34a8f 63 20 65 72 72 6f 72 73 20 68 65 72 65 20 2a 2f  c errors here */
34a90 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
34a91 20 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43          /* SELEC
34a92 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 77  T statement in w
34a93 68 69 63 68 20 74 6f 20 6d 61 6b 65 20 73 75 62  hich to make sub
34a94 73 74 69 74 75 74 69 6f 6e 73 20 2a 2f 0a 20 20  stitutions */.  
34a95 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20  int iTable,     
34a96 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f       /* Table to
34a97 20 62 65 20 72 65 70 6c 61 63 65 64 20 2a 2f 0a   be replaced */.
34a98 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
34a99 73 74 20 20 20 20 20 2f 2a 20 53 75 62 73 74 69  st     /* Substi
34a9a 74 75 74 65 20 76 61 6c 75 65 73 20 2a 2f 0a 29  tute values */.)
34a9b 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  {.  SrcList *pSr
34a9c 63 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  c;.  struct SrcL
34a9d 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
34a9e 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
34a9f 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  !p ) return;.  s
34aa0 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c  ubstExprList(db,
34aa1 20 70 2d 3e 70 45 4c 69 73 74 2c 20 69 54 61 62   p->pEList, iTab
34aa2 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73  le, pEList);.  s
34aa3 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c  ubstExprList(db,
34aa4 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 69 54   p->pGroupBy, iT
34aa5 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20  able, pEList);. 
34aa6 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64   substExprList(d
34aa7 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20  b, p->pOrderBy, 
34aa8 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
34aa9 0a 20 20 70 2d 3e 70 48 61 76 69 6e 67 20 3d 20  .  p->pHaving = 
34aaa 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 2d  substExpr(db, p-
34aab 3e 70 48 61 76 69 6e 67 2c 20 69 54 61 62 6c 65  >pHaving, iTable
34aac 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 70 2d 3e  , pEList);.  p->
34aad 70 57 68 65 72 65 20 3d 20 73 75 62 73 74 45 78  pWhere = substEx
34aae 70 72 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65  pr(db, p->pWhere
34aaf 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74  , iTable, pEList
34ab0 29 3b 0a 20 20 73 75 62 73 74 53 65 6c 65 63 74  );.  substSelect
34ab1 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20  (db, p->pPrior, 
34ab2 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
34ab3 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72  .  pSrc = p->pSr
34ab4 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 72  c;.  assert( pSr
34ab5 63 20 29 3b 20 20 2f 2a 20 45 76 65 6e 20 66 6f  c );  /* Even fo
34ab6 72 20 28 53 45 4c 45 43 54 20 31 29 20 77 65 20  r (SELECT 1) we 
34ab7 68 61 76 65 3a 20 70 53 72 63 21 3d 30 20 62 75  have: pSrc!=0 bu
34ab8 74 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 30 20  t pSrc->nSrc==0 
34ab9 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  */.  if( ALWAYS(
34aba 70 53 72 63 29 20 29 7b 0a 20 20 20 20 66 6f 72  pSrc) ){.    for
34abb 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70  (i=pSrc->nSrc, p
34abc 49 74 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e  Item=pSrc->a; i>
34abd 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29  0; i--, pItem++)
34abe 7b 0a 20 20 20 20 20 20 73 75 62 73 74 53 65 6c  {.      substSel
34abf 65 63 74 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70  ect(db, pItem->p
34ac0 53 65 6c 65 63 74 2c 20 69 54 61 62 6c 65 2c 20  Select, iTable, 
34ac1 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20  pEList);.    }. 
34ac2 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21   }.}.#endif /* !
34ac3 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
34ac4 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c  MIT_SUBQUERY) ||
34ac5 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
34ac6 5f 4f 4d 49 54 5f 56 49 45 57 29 20 2a 2f 0a 0a  _OMIT_VIEW) */..
34ac7 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
34ac8 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
34ac9 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  Y) || !defined(S
34aca 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
34acb 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
34acc 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ine attempts to 
34acd 66 6c 61 74 74 65 6e 20 73 75 62 71 75 65 72 69  flatten subqueri
34ace 65 73 20 61 73 20 61 20 70 65 72 66 6f 72 6d 61  es as a performa
34acf 6e 63 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  nce optimization
34ad0 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
34ad1 65 20 72 65 74 75 72 6e 73 20 31 20 69 66 20 69  e returns 1 if i
34ad2 74 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20  t makes changes 
34ad3 61 6e 64 20 30 20 69 66 20 6e 6f 20 66 6c 61 74  and 0 if no flat
34ad4 74 65 6e 69 6e 67 20 6f 63 63 75 72 73 2e 0a 2a  tening occurs..*
34ad5 2a 0a 2a 2a 20 54 6f 20 75 6e 64 65 72 73 74 61  *.** To understa
34ad6 6e 64 20 74 68 65 20 63 6f 6e 63 65 70 74 20 6f  nd the concept o
34ad7 66 20 66 6c 61 74 74 65 6e 69 6e 67 2c 20 63 6f  f flattening, co
34ad8 6e 73 69 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f  nsider the follo
34ad9 77 69 6e 67 0a 2a 2a 20 71 75 65 72 79 3a 0a 2a  wing.** query:.*
34ada 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
34adb 61 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 78  a FROM (SELECT x
34adc 2b 79 20 41 53 20 61 20 46 52 4f 4d 20 74 31 20  +y AS a FROM t1 
34add 57 48 45 52 45 20 7a 3c 31 30 30 29 20 57 48 45  WHERE z<100) WHE
34ade 52 45 20 61 3e 35 0a 2a 2a 0a 2a 2a 20 54 68 65  RE a>5.**.** The
34adf 20 64 65 66 61 75 6c 74 20 77 61 79 20 6f 66 20   default way of 
34ae0 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 69  implementing thi
34ae1 73 20 71 75 65 72 79 20 69 73 20 74 6f 20 65 78  s query is to ex
34ae2 65 63 75 74 65 20 74 68 65 0a 2a 2a 20 73 75 62  ecute the.** sub
34ae3 71 75 65 72 79 20 66 69 72 73 74 20 61 6e 64 20  query first and 
34ae4 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
34ae5 73 20 69 6e 20 61 20 74 65 6d 70 6f 72 61 72 79  s in a temporary
34ae6 20 74 61 62 6c 65 2c 20 74 68 65 6e 0a 2a 2a 20   table, then.** 
34ae7 72 75 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75  run the outer qu
34ae8 65 72 79 20 6f 6e 20 74 68 61 74 20 74 65 6d 70  ery on that temp
34ae9 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 20 54 68  orary table.  Th
34aea 69 73 20 72 65 71 75 69 72 65 73 20 74 77 6f 0a  is requires two.
34aeb 2a 2a 20 70 61 73 73 65 73 20 6f 76 65 72 20 74  ** passes over t
34aec 68 65 20 64 61 74 61 2e 20 20 46 75 72 74 68 65  he data.  Furthe
34aed 72 6d 6f 72 65 2c 20 62 65 63 61 75 73 65 20 74  rmore, because t
34aee 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  he temporary tab
34aef 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f 20 69 6e 64  le.** has no ind
34af0 69 63 65 73 2c 20 74 68 65 20 57 48 45 52 45 20  ices, the WHERE 
34af1 63 6c 61 75 73 65 20 6f 6e 20 74 68 65 20 6f 75  clause on the ou
34af2 74 65 72 20 71 75 65 72 79 20 63 61 6e 6e 6f 74  ter query cannot
34af3 20 62 65 0a 2a 2a 20 6f 70 74 69 6d 69 7a 65 64   be.** optimized
34af4 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
34af5 74 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f  tine attempts to
34af6 20 72 65 77 72 69 74 65 20 71 75 65 72 69 65 73   rewrite queries
34af7 20 73 75 63 68 20 61 73 20 74 68 65 20 61 62 6f   such as the abo
34af8 76 65 20 69 6e 74 6f 0a 2a 2a 20 61 20 73 69 6e  ve into.** a sin
34af9 67 6c 65 20 66 6c 61 74 20 73 65 6c 65 63 74 2c  gle flat select,
34afa 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
34afb 2a 20 20 20 20 20 53 45 4c 45 43 54 20 78 2b 79  *     SELECT x+y
34afc 20 41 53 20 61 20 46 52 4f 4d 20 74 31 20 57 48   AS a FROM t1 WH
34afd 45 52 45 20 7a 3c 31 30 30 20 41 4e 44 20 61 3e  ERE z<100 AND a>
34afe 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65  5.**.** The code
34aff 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 74   generated for t
34b00 68 69 73 20 73 69 6d 70 69 66 69 63 61 74 69 6f  his simpificatio
34b01 6e 20 67 69 76 65 73 20 74 68 65 20 73 61 6d 65  n gives the same
34b02 20 72 65 73 75 6c 74 0a 2a 2a 20 62 75 74 20 6f   result.** but o
34b03 6e 6c 79 20 68 61 73 20 74 6f 20 73 63 61 6e 20  nly has to scan 
34b04 74 68 65 20 64 61 74 61 20 6f 6e 63 65 2e 20 20  the data once.  
34b05 41 6e 64 20 62 65 63 61 75 73 65 20 69 6e 64 69  And because indi
34b06 63 65 73 20 6d 69 67 68 74 20 0a 2a 2a 20 65 78  ces might .** ex
34b07 69 73 74 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ist on the table
34b08 20 74 31 2c 20 61 20 63 6f 6d 70 6c 65 74 65 20   t1, a complete 
34b09 73 63 61 6e 20 6f 66 20 74 68 65 20 64 61 74 61  scan of the data
34b0a 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 61 76 6f   might be.** avo
34b0b 69 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6c 61 74  ided..**.** Flat
34b0c 74 65 6e 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61  tening is only a
34b0d 74 74 65 6d 70 74 65 64 20 69 66 20 61 6c 6c 20  ttempted if all 
34b0e 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
34b0f 20 61 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a   are true:.**.**
34b10 20 20 20 28 31 29 20 20 54 68 65 20 73 75 62 71     (1)  The subq
34b11 75 65 72 79 20 61 6e 64 20 74 68 65 20 6f 75 74  uery and the out
34b12 65 72 20 71 75 65 72 79 20 64 6f 20 6e 6f 74 20  er query do not 
34b13 62 6f 74 68 20 75 73 65 20 61 67 67 72 65 67 61  both use aggrega
34b14 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 32 29  tes..**.**   (2)
34b15 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69    The subquery i
34b16 73 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 61  s not an aggrega
34b17 74 65 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20  te or the outer 
34b18 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 20 6a  query is not a j
34b19 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29  oin..**.**   (3)
34b1a 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69    The subquery i
34b1b 73 20 6e 6f 74 20 74 68 65 20 72 69 67 68 74 20  s not the right 
34b1c 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 6c 65 66  operand of a lef
34b1d 74 20 6f 75 74 65 72 20 6a 6f 69 6e 0a 2a 2a 20  t outer join.** 
34b1e 20 20 20 20 20 20 20 28 4f 72 69 67 69 6e 61 6c         (Original
34b1f 6c 79 20 74 69 63 6b 65 74 20 23 33 30 36 2e 20  ly ticket #306. 
34b20 20 53 74 72 65 6e 67 74 68 65 6e 65 64 20 62 79   Strengthened by
34b21 20 74 69 63 6b 65 74 20 23 33 33 30 30 29 0a 2a   ticket #3300).*
34b22 2a 0a 2a 2a 20 20 20 28 34 29 20 20 54 68 65 20  *.**   (4)  The 
34b23 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20  subquery is not 
34b24 44 49 53 54 49 4e 43 54 2e 0a 2a 2a 0a 2a 2a 20  DISTINCT..**.** 
34b25 20 28 2a 2a 29 20 20 41 74 20 6f 6e 65 20 70 6f   (**)  At one po
34b26 69 6e 74 20 72 65 73 74 72 69 63 74 69 6f 6e 73  int restrictions
34b27 20 28 34 29 20 61 6e 64 20 28 35 29 20 64 65 66   (4) and (5) def
34b28 69 6e 65 64 20 61 20 73 75 62 73 65 74 20 6f 66  ined a subset of
34b29 20 44 49 53 54 49 4e 43 54 0a 2a 2a 20 20 20 20   DISTINCT.**    
34b2a 20 20 20 20 73 75 62 2d 71 75 65 72 69 65 73 20      sub-queries 
34b2b 74 68 61 74 20 77 65 72 65 20 65 78 63 6c 75 64  that were exclud
34b2c 65 64 20 66 72 6f 6d 20 74 68 69 73 20 6f 70 74  ed from this opt
34b2d 69 6d 69 7a 61 74 69 6f 6e 2e 20 52 65 73 74 72  imization. Restr
34b2e 69 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 20 20 20  iction .**      
34b2f 20 20 28 34 29 20 68 61 73 20 73 69 6e 63 65 20    (4) has since 
34b30 62 65 65 6e 20 65 78 70 61 6e 64 65 64 20 74 6f  been expanded to
34b31 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 44 49 53   exclude all DIS
34b32 54 49 4e 43 54 20 73 75 62 71 75 65 72 69 65 73  TINCT subqueries
34b33 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36 29 20 20 54  ..**.**   (6)  T
34b34 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73  he subquery does
34b35 20 6e 6f 74 20 75 73 65 20 61 67 67 72 65 67 61   not use aggrega
34b36 74 65 73 20 6f 72 20 74 68 65 20 6f 75 74 65 72  tes or the outer
34b37 20 71 75 65 72 79 20 69 73 20 6e 6f 74 0a 2a 2a   query is not.**
34b38 20 20 20 20 20 20 20 20 44 49 53 54 49 4e 43 54          DISTINCT
34b39 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 37 29 20 20 54  ..**.**   (7)  T
34b3a 68 65 20 73 75 62 71 75 65 72 79 20 68 61 73 20  he subquery has 
34b3b 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20  a FROM clause.  
34b3c 54 4f 44 4f 3a 20 20 46 6f 72 20 73 75 62 71 75  TODO:  For subqu
34b3d 65 72 69 65 73 20 77 69 74 68 6f 75 74 0a 2a 2a  eries without.**
34b3e 20 20 20 20 20 20 20 20 41 20 46 52 4f 4d 20 63          A FROM c
34b3f 6c 61 75 73 65 2c 20 63 6f 6e 73 69 64 65 72 20  lause, consider 
34b40 61 64 64 69 6e 67 20 61 20 46 52 4f 4d 20 63 6c  adding a FROM cl
34b41 6f 73 65 20 77 69 74 68 20 74 68 65 20 73 70 65  ose with the spe
34b42 63 69 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 74  cial.**        t
34b43 61 62 6c 65 20 73 71 6c 69 74 65 5f 6f 6e 63 65  able sqlite_once
34b44 20 74 68 61 74 20 63 6f 6e 73 69 73 74 73 20 6f   that consists o
34b45 66 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 63  f a single row c
34b46 6f 6e 74 61 69 6e 69 6e 67 20 61 0a 2a 2a 20 20  ontaining a.**  
34b47 20 20 20 20 20 20 73 69 6e 67 6c 65 20 4e 55 4c        single NUL
34b48 4c 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 38 29 20 20  L..**.**   (8)  
34b49 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65  The subquery doe
34b4a 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54 20  s not use LIMIT 
34b4b 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  or the outer que
34b4c 72 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e  ry is not a join
34b4d 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 39 29 20 20 54  ..**.**   (9)  T
34b4e 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73  he subquery does
34b4f 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54 20 6f   not use LIMIT o
34b50 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  r the outer quer
34b51 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 0a 2a  y does not use.*
34b52 2a 20 20 20 20 20 20 20 20 61 67 67 72 65 67 61  *        aggrega
34b53 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 30 29  tes..**.**  (10)
34b54 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64    The subquery d
34b55 6f 65 73 20 6e 6f 74 20 75 73 65 20 61 67 67 72  oes not use aggr
34b56 65 67 61 74 65 73 20 6f 72 20 74 68 65 20 6f 75  egates or the ou
34b57 74 65 72 20 71 75 65 72 79 20 64 6f 65 73 20 6e  ter query does n
34b58 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 75 73 65  ot.**        use
34b59 20 4c 49 4d 49 54 2e 0a 2a 2a 0a 2a 2a 20 20 28   LIMIT..**.**  (
34b5a 31 31 29 20 20 54 68 65 20 73 75 62 71 75 65 72  11)  The subquer
34b5b 79 20 61 6e 64 20 74 68 65 20 6f 75 74 65 72 20  y and the outer 
34b5c 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74  query do not bot
34b5d 68 20 68 61 76 65 20 4f 52 44 45 52 20 42 59 20  h have ORDER BY 
34b5e 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  clauses..**.**  
34b5f 28 2a 2a 29 20 20 4e 6f 74 20 69 6d 70 6c 65 6d  (**)  Not implem
34b60 65 6e 74 65 64 2e 20 20 53 75 62 73 75 6d 65 64  ented.  Subsumed
34b61 20 69 6e 74 6f 20 72 65 73 74 72 69 63 74 69 6f   into restrictio
34b62 6e 20 28 33 29 2e 20 20 57 61 73 20 70 72 65 76  n (3).  Was prev
34b63 69 6f 75 73 6c 79 0a 2a 2a 20 20 20 20 20 20 20  iously.**       
34b64 20 61 20 73 65 70 61 72 61 74 65 20 72 65 73 74   a separate rest
34b65 72 69 63 74 69 6f 6e 20 64 65 72 69 76 69 6e 67  riction deriving
34b66 20 66 72 6f 6d 20 74 69 63 6b 65 74 20 23 33 35   from ticket #35
34b67 30 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 33 29 20 20  0..**.**  (13)  
34b68 54 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64  The subquery and
34b69 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 20   outer query do 
34b6a 6e 6f 74 20 62 6f 74 68 20 75 73 65 20 4c 49 4d  not both use LIM
34b6b 49 54 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 34 29 20  IT..**.**  (14) 
34b6c 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f   The subquery do
34b6d 65 73 20 6e 6f 74 20 75 73 65 20 4f 46 46 53 45  es not use OFFSE
34b6e 54 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 35 29 20 20  T..**.**  (15)  
34b6f 54 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  The outer query 
34b70 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61  is not part of a
34b71 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
34b72 20 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   or the.**      
34b73 20 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20    subquery does 
34b74 6e 6f 74 20 68 61 76 65 20 61 20 4c 49 4d 49 54  not have a LIMIT
34b75 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 20 20 20 20   clause..**     
34b76 20 20 20 28 53 65 65 20 74 69 63 6b 65 74 20 23     (See ticket #
34b77 32 33 33 39 20 61 6e 64 20 74 69 63 6b 65 74 20  2339 and ticket 
34b78 5b 30 32 61 38 65 38 31 64 34 34 5d 29 2e 0a 2a  [02a8e81d44])..*
34b79 2a 0a 2a 2a 20 20 28 31 36 29 20 20 54 68 65 20  *.**  (16)  The 
34b7a 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e  outer query is n
34b7b 6f 74 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  ot an aggregate 
34b7c 6f 72 20 74 68 65 20 73 75 62 71 75 65 72 79 20  or the subquery 
34b7d 64 6f 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 6e  does.**        n
34b7e 6f 74 20 63 6f 6e 74 61 69 6e 20 4f 52 44 45 52  ot contain ORDER
34b7f 20 42 59 2e 20 20 28 54 69 63 6b 65 74 20 23 32   BY.  (Ticket #2
34b80 39 34 32 29 20 20 54 68 69 73 20 75 73 65 64 20  942)  This used 
34b81 74 6f 20 6e 6f 74 20 6d 61 74 74 65 72 0a 2a 2a  to not matter.**
34b82 20 20 20 20 20 20 20 20 75 6e 74 69 6c 20 77 65          until we
34b83 20 69 6e 74 72 6f 64 75 63 65 64 20 74 68 65 20   introduced the 
34b84 67 72 6f 75 70 5f 63 6f 6e 63 61 74 28 29 20 66  group_concat() f
34b85 75 6e 63 74 69 6f 6e 2e 20 20 0a 2a 2a 0a 2a 2a  unction.  .**.**
34b86 20 20 28 31 37 29 20 20 54 68 65 20 73 75 62 2d    (17)  The sub-
34b87 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 20 63  query is not a c
34b88 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c 20  ompound select, 
34b89 6f 72 20 69 74 20 69 73 20 61 20 55 4e 49 4f 4e  or it is a UNION
34b8a 20 41 4c 4c 20 0a 2a 2a 20 20 20 20 20 20 20 20   ALL .**        
34b8b 63 6f 6d 70 6f 75 6e 64 20 63 6c 61 75 73 65 20  compound clause 
34b8c 6d 61 64 65 20 75 70 20 65 6e 74 69 72 65 6c 79  made up entirely
34b8d 20 6f 66 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74   of non-aggregat
34b8e 65 20 71 75 65 72 69 65 73 2c 20 61 6e 64 20 0a  e queries, and .
34b8f 2a 2a 20 20 20 20 20 20 20 20 74 68 65 20 70 61  **        the pa
34b90 72 65 6e 74 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a  rent query:.**.*
34b91 2a 20 20 20 20 20 20 20 20 20 20 2a 20 69 73 20  *          * is 
34b92 6e 6f 74 20 69 74 73 65 6c 66 20 70 61 72 74 20  not itself part 
34b93 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65  of a compound se
34b94 6c 65 63 74 2c 0a 2a 2a 20 20 20 20 20 20 20 20  lect,.**        
34b95 20 20 2a 20 69 73 20 6e 6f 74 20 61 6e 20 61 67    * is not an ag
34b96 67 72 65 67 61 74 65 20 6f 72 20 44 49 53 54 49  gregate or DISTI
34b97 4e 43 54 20 71 75 65 72 79 2c 20 61 6e 64 0a 2a  NCT query, and.*
34b98 2a 20 20 20 20 20 20 20 20 20 20 2a 20 69 73 20  *          * is 
34b99 6e 6f 74 20 61 20 6a 6f 69 6e 0a 2a 2a 0a 2a 2a  not a join.**.**
34b9a 20 20 20 20 20 20 20 20 54 68 65 20 70 61 72 65          The pare
34b9b 6e 74 20 61 6e 64 20 73 75 62 2d 71 75 65 72 79  nt and sub-query
34b9c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 57 48 45   may contain WHE
34b9d 52 45 20 63 6c 61 75 73 65 73 2e 20 53 75 62 6a  RE clauses. Subj
34b9e 65 63 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  ect to.**       
34b9f 20 72 75 6c 65 73 20 28 31 31 29 2c 20 28 31 33   rules (11), (13
34ba0 29 20 61 6e 64 20 28 31 34 29 2c 20 74 68 65 79  ) and (14), they
34ba1 20 6d 61 79 20 61 6c 73 6f 20 63 6f 6e 74 61 69   may also contai
34ba2 6e 20 4f 52 44 45 52 20 42 59 2c 0a 2a 2a 20 20  n ORDER BY,.**  
34ba3 20 20 20 20 20 20 4c 49 4d 49 54 20 61 6e 64 20        LIMIT and 
34ba4 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 2e 20  OFFSET clauses. 
34ba5 20 54 68 65 20 73 75 62 71 75 65 72 79 20 63 61   The subquery ca
34ba6 6e 6e 6f 74 20 75 73 65 20 61 6e 79 20 63 6f 6d  nnot use any com
34ba7 70 6f 75 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  pound.**        
34ba8 6f 70 65 72 61 74 6f 72 20 6f 74 68 65 72 20 74  operator other t
34ba9 68 61 6e 20 55 4e 49 4f 4e 20 41 4c 4c 20 62 65  han UNION ALL be
34baa 63 61 75 73 65 20 61 6c 6c 20 74 68 65 20 6f 74  cause all the ot
34bab 68 65 72 20 63 6f 6d 70 6f 75 6e 64 0a 2a 2a 20  her compound.** 
34bac 20 20 20 20 20 20 20 6f 70 65 72 61 74 6f 72 73         operators
34bad 20 68 61 76 65 20 61 6e 20 69 6d 70 6c 69 65 64   have an implied
34bae 20 44 49 53 54 49 4e 43 54 20 77 68 69 63 68 20   DISTINCT which 
34baf 69 73 20 64 69 73 61 6c 6c 6f 77 65 64 20 62 79  is disallowed by
34bb0 0a 2a 2a 20 20 20 20 20 20 20 20 72 65 73 74 72  .**        restr
34bb1 69 63 74 69 6f 6e 20 28 34 29 2e 0a 2a 2a 0a 2a  iction (4)..**.*
34bb2 2a 20 20 20 20 20 20 20 20 41 6c 73 6f 2c 20 65  *        Also, e
34bb3 61 63 68 20 63 6f 6d 70 6f 6e 65 6e 74 20 6f 66  ach component of
34bb4 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20 6d   the sub-query m
34bb5 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 73  ust return the s
34bb6 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20  ame number.**   
34bb7 20 20 20 20 20 6f 66 20 72 65 73 75 6c 74 20 63       of result c
34bb8 6f 6c 75 6d 6e 73 2e 20 54 68 69 73 20 69 73 20  olumns. This is 
34bb9 61 63 74 75 61 6c 6c 79 20 61 20 72 65 71 75 69  actually a requi
34bba 72 65 6d 65 6e 74 20 66 6f 72 20 61 6e 79 20 63  rement for any c
34bbb 6f 6d 70 6f 75 6e 64 0a 2a 2a 20 20 20 20 20 20  ompound.**      
34bbc 20 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65    SELECT stateme
34bbd 6e 74 2c 20 62 75 74 20 61 6c 6c 20 74 68 65 20  nt, but all the 
34bbe 63 6f 64 65 20 68 65 72 65 20 64 6f 65 73 20 69  code here does i
34bbf 73 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  s make sure that
34bc0 20 6e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 73 75   no.**        su
34bc1 63 68 20 28 69 6c 6c 65 67 61 6c 29 20 73 75 62  ch (illegal) sub
34bc2 2d 71 75 65 72 79 20 69 73 20 66 6c 61 74 74 65  -query is flatte
34bc3 6e 65 64 2e 20 54 68 65 20 63 61 6c 6c 65 72 20  ned. The caller 
34bc4 77 69 6c 6c 20 64 65 74 65 63 74 20 74 68 65 0a  will detect the.
34bc5 2a 2a 20 20 20 20 20 20 20 20 73 79 6e 74 61 78  **        syntax
34bc6 20 65 72 72 6f 72 20 61 6e 64 20 72 65 74 75 72   error and retur
34bc7 6e 20 61 20 64 65 74 61 69 6c 65 64 20 6d 65 73  n a detailed mes
34bc8 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 38  sage..**.**  (18
34bc9 29 20 20 49 66 20 74 68 65 20 73 75 62 2d 71 75  )  If the sub-qu
34bca 65 72 79 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e  ery is a compoun
34bcb 64 20 73 65 6c 65 63 74 2c 20 74 68 65 6e 20 61  d select, then a
34bcc 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65 0a  ll terms of the.
34bcd 2a 2a 20 20 20 20 20 20 20 20 4f 52 44 45 52 20  **        ORDER 
34bce 62 79 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  by clause of the
34bcf 20 70 61 72 65 6e 74 20 6d 75 73 74 20 62 65 20   parent must be 
34bd0 73 69 6d 70 6c 65 20 72 65 66 65 72 65 6e 63 65  simple reference
34bd1 73 20 74 6f 20 0a 2a 2a 20 20 20 20 20 20 20 20  s to .**        
34bd2 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 73  columns of the s
34bd3 75 62 2d 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20  ub-query..**.** 
34bd4 20 28 31 39 29 20 20 54 68 65 20 73 75 62 71 75   (19)  The subqu
34bd5 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65  ery does not use
34bd6 20 4c 49 4d 49 54 20 6f 72 20 74 68 65 20 6f 75   LIMIT or the ou
34bd7 74 65 72 20 71 75 65 72 79 20 64 6f 65 73 20 6e  ter query does n
34bd8 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 68 61 76  ot.**        hav
34bd9 65 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65  e a WHERE clause
34bda 2e 0a 2a 2a 0a 2a 2a 20 20 28 32 30 29 20 20 49  ..**.**  (20)  I
34bdb 66 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20  f the sub-query 
34bdc 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65  is a compound se
34bdd 6c 65 63 74 2c 20 74 68 65 6e 20 69 74 20 6d 75  lect, then it mu
34bde 73 74 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20  st not use.**   
34bdf 20 20 20 20 20 61 6e 20 4f 52 44 45 52 20 42 59       an ORDER BY
34be0 20 63 6c 61 75 73 65 2e 20 20 54 69 63 6b 65 74   clause.  Ticket
34be1 20 23 33 37 37 33 2e 20 20 57 65 20 63 6f 75 6c   #3773.  We coul
34be2 64 20 72 65 6c 61 78 20 74 68 69 73 20 63 6f 6e  d relax this con
34be3 73 74 72 61 69 6e 74 0a 2a 2a 20 20 20 20 20 20  straint.**      
34be4 20 20 73 6f 6d 65 77 68 61 74 20 62 79 20 73 61    somewhat by sa
34be5 79 69 6e 67 20 74 68 61 74 20 74 68 65 20 74 65  ying that the te
34be6 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52  rms of the ORDER
34be7 20 42 59 20 63 6c 61 75 73 65 20 6d 75 73 74 0a   BY clause must.
34be8 2a 2a 20 20 20 20 20 20 20 20 61 70 70 65 61 72  **        appear
34be9 20 61 73 20 75 6e 6d 6f 64 69 66 69 65 64 20 72   as unmodified r
34bea 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 69 6e  esult columns in
34beb 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
34bec 2e 20 20 42 75 74 20 77 65 0a 2a 2a 20 20 20 20  .  But we.**    
34bed 20 20 20 20 68 61 76 65 20 6f 74 68 65 72 20 6f      have other o
34bee 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 69 6e 20  ptimizations in 
34bef 6d 69 6e 64 20 74 6f 20 64 65 61 6c 20 77 69 74  mind to deal wit
34bf0 68 20 74 68 61 74 20 63 61 73 65 2e 0a 2a 2a 0a  h that case..**.
34bf1 2a 2a 20 20 28 32 31 29 20 20 54 68 65 20 73 75  **  (21)  The su
34bf2 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20  bquery does not 
34bf3 75 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65  use LIMIT or the
34bf4 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20   outer query is 
34bf5 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 44 49  not.**        DI
34bf6 53 54 49 4e 43 54 2e 20 20 28 53 65 65 20 74 69  STINCT.  (See ti
34bf7 63 6b 65 74 20 5b 37 35 32 65 31 36 34 36 66 63  cket [752e1646fc
34bf8 5d 29 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69  ])..**.** In thi
34bf9 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 20 22  s routine, the "
34bfa 70 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  p" parameter is 
34bfb 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
34bfc 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a 2a 2a   outer query..**
34bfd 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73   The subquery is
34bfe 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 46 72 6f   p->pSrc->a[iFro
34bff 6d 5d 2e 20 20 69 73 41 67 67 20 69 73 20 74 72  m].  isAgg is tr
34c00 75 65 20 69 66 20 74 68 65 20 6f 75 74 65 72 20  ue if the outer 
34c01 71 75 65 72 79 0a 2a 2a 20 75 73 65 73 20 61 67  query.** uses ag
34c02 67 72 65 67 61 74 65 73 20 61 6e 64 20 73 75 62  gregates and sub
34c03 71 75 65 72 79 49 73 41 67 67 20 69 73 20 74 72  queryIsAgg is tr
34c04 75 65 20 69 66 20 74 68 65 20 73 75 62 71 75 65  ue if the subque
34c05 72 79 20 75 73 65 73 20 61 67 67 72 65 67 61 74  ry uses aggregat
34c06 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 66 6c 61  es..**.** If fla
34c07 74 74 65 6e 69 6e 67 20 69 73 20 6e 6f 74 20 61  ttening is not a
34c08 74 74 65 6d 70 74 65 64 2c 20 74 68 69 73 20 72  ttempted, this r
34c09 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
34c0a 70 20 61 6e 64 20 72 65 74 75 72 6e 73 20 30 2e  p and returns 0.
34c0b 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65 6e 69 6e  .** If flattenin
34c0c 67 20 69 73 20 61 74 74 65 6d 70 74 65 64 20 74  g is attempted t
34c0d 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
34c0e 72 6e 73 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c  rns 1..**.** All
34c0f 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
34c10 6f 6e 20 61 6e 61 6c 79 73 69 73 20 6d 75 73 74  on analysis must
34c11 20 6f 63 63 75 72 20 6f 6e 20 62 6f 74 68 20 74   occur on both t
34c12 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 61  he outer query a
34c13 6e 64 0a 2a 2a 20 74 68 65 20 73 75 62 71 75 65  nd.** the subque
34c14 72 79 20 62 65 66 6f 72 65 20 74 68 69 73 20 72  ry before this r
34c15 6f 75 74 69 6e 65 20 72 75 6e 73 2e 0a 2a 2f 0a  outine runs..*/.
34c16 73 74 61 74 69 63 20 69 6e 74 20 66 6c 61 74 74  static int flatt
34c17 65 6e 53 75 62 71 75 65 72 79 28 0a 20 20 50 61  enSubquery(.  Pa
34c18 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
34c19 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
34c1a 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63  ntext */.  Selec
34c1b 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  t *p,           
34c1c 2f 2a 20 54 68 65 20 70 61 72 65 6e 74 20 6f 72  /* The parent or
34c1d 20 6f 75 74 65 72 20 53 45 4c 45 43 54 20 73 74   outer SELECT st
34c1e 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  atement */.  int
34c1f 20 69 46 72 6f 6d 2c 20 20 20 20 20 20 20 20 20   iFrom,         
34c20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 2d    /* Index in p-
34c21 3e 70 53 72 63 2d 3e 61 5b 5d 20 6f 66 20 74 68  >pSrc->a[] of th
34c22 65 20 69 6e 6e 65 72 20 73 75 62 71 75 65 72 79  e inner subquery
34c23 20 2a 2f 0a 20 20 69 6e 74 20 69 73 41 67 67 2c   */.  int isAgg,
34c24 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
34c25 75 65 20 69 66 20 6f 75 74 65 72 20 53 45 4c 45  ue if outer SELE
34c26 43 54 20 75 73 65 73 20 61 67 67 72 65 67 61 74  CT uses aggregat
34c27 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20  e functions */. 
34c28 20 69 6e 74 20 73 75 62 71 75 65 72 79 49 73 41   int subqueryIsA
34c29 67 67 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  gg    /* True if
34c2a 20 74 68 65 20 73 75 62 71 75 65 72 79 20 75 73   the subquery us
34c2b 65 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  es aggregate fun
34c2c 63 74 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 63  ctions */.){.  c
34c2d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 61 76 65  onst char *zSave
34c2e 64 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70  dAuthContext = p
34c2f 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
34c30 65 78 74 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70  ext;.  Select *p
34c31 50 61 72 65 6e 74 3b 0a 20 20 53 65 6c 65 63 74  Parent;.  Select
34c32 20 2a 70 53 75 62 3b 20 20 20 20 20 20 20 2f 2a   *pSub;       /*
34c33 20 54 68 65 20 69 6e 6e 65 72 20 71 75 65 72 79   The inner query
34c34 20 6f 72 20 22 73 75 62 71 75 65 72 79 22 20 2a   or "subquery" *
34c35 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75 62  /.  Select *pSub
34c36 31 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  1;      /* Point
34c37 65 72 20 74 6f 20 74 68 65 20 72 69 67 68 74 6d  er to the rightm
34c38 6f 73 74 20 73 65 6c 65 63 74 20 69 6e 20 73 75  ost select in su
34c39 62 2d 71 75 65 72 79 20 2a 2f 0a 20 20 53 72 63  b-query */.  Src
34c3a 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20  List *pSrc;     
34c3b 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61   /* The FROM cla
34c3c 75 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72  use of the outer
34c3d 20 71 75 65 72 79 20 2a 2f 0a 20 20 53 72 63 4c   query */.  SrcL
34c3e 69 73 74 20 2a 70 53 75 62 53 72 63 3b 20 20 20  ist *pSubSrc;   
34c3f 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* The FROM clau
34c40 73 65 20 6f 66 20 74 68 65 20 73 75 62 71 75 65  se of the subque
34c41 72 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ry */.  ExprList
34c42 20 2a 70 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54   *pList;    /* T
34c43 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
34c44 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
34c45 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 6e   */.  int iParen
34c46 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42  t;        /* VDB
34c47 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  E cursor number 
34c48 6f 66 20 74 68 65 20 70 53 75 62 20 72 65 73 75  of the pSub resu
34c49 6c 74 20 73 65 74 20 74 65 6d 70 20 74 61 62 6c  lt set temp tabl
34c4a 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  e */.  int i;   
34c4b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
34c4c 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
34c4d 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20  Expr *pWhere;   
34c4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34c4f 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
34c50 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  ause */.  struct
34c51 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
34c52 53 75 62 69 74 65 6d 3b 20 20 20 2f 2a 20 54 68  Subitem;   /* Th
34c53 65 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20  e subquery */.  
34c54 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
34c55 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20  arse->db;..  /* 
34c56 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
34c57 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 70 65  flattening is pe
34c58 72 6d 69 74 74 65 64 2e 20 20 52 65 74 75 72 6e  rmitted.  Return
34c59 20 30 20 69 66 20 6e 6f 74 2e 0a 20 20 2a 2f 0a   0 if not..  */.
34c5a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29    assert( p!=0 )
34c5b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70  ;.  assert( p->p
34c5c 50 72 69 6f 72 3d 3d 30 20 29 3b 20 20 2f 2a 20  Prior==0 );  /* 
34c5d 55 6e 61 62 6c 65 20 74 6f 20 66 6c 61 74 74 65  Unable to flatte
34c5e 6e 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 69  n compound queri
34c5f 65 73 20 2a 2f 0a 20 20 69 66 28 20 4f 70 74 69  es */.  if( Opti
34c60 6d 69 7a 61 74 69 6f 6e 44 69 73 61 62 6c 65 64  mizationDisabled
34c61 28 64 62 2c 20 53 51 4c 49 54 45 5f 51 75 65 72  (db, SQLITE_Quer
34c62 79 46 6c 61 74 74 65 6e 65 72 29 20 29 20 72 65  yFlattener) ) re
34c63 74 75 72 6e 20 30 3b 0a 20 20 70 53 72 63 20 3d  turn 0;.  pSrc =
34c64 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73 65   p->pSrc;.  asse
34c65 72 74 28 20 70 53 72 63 20 26 26 20 69 46 72 6f  rt( pSrc && iFro
34c66 6d 3e 3d 30 20 26 26 20 69 46 72 6f 6d 3c 70 53  m>=0 && iFrom<pS
34c67 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 70 53  rc->nSrc );.  pS
34c68 75 62 69 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e  ubitem = &pSrc->
34c69 61 5b 69 46 72 6f 6d 5d 3b 0a 20 20 69 50 61 72  a[iFrom];.  iPar
34c6a 65 6e 74 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e  ent = pSubitem->
34c6b 69 43 75 72 73 6f 72 3b 0a 20 20 70 53 75 62 20  iCursor;.  pSub 
34c6c 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c  = pSubitem->pSel
34c6d 65 63 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ect;.  assert( p
34c6e 53 75 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  Sub!=0 );.  if( 
34c6f 69 73 41 67 67 20 26 26 20 73 75 62 71 75 65 72  isAgg && subquer
34c70 79 49 73 41 67 67 20 29 20 72 65 74 75 72 6e 20  yIsAgg ) return 
34c71 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
34c72 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
34c73 6e 20 28 31 29 20 20 2a 2f 0a 20 20 69 66 28 20  n (1)  */.  if( 
34c74 73 75 62 71 75 65 72 79 49 73 41 67 67 20 26 26  subqueryIsAgg &&
34c75 20 70 53 72 63 2d 3e 6e 53 72 63 3e 31 20 29 20   pSrc->nSrc>1 ) 
34c76 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
34c77 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
34c78 6e 20 28 32 29 20 20 2a 2f 0a 20 20 70 53 75 62  n (2)  */.  pSub
34c79 53 72 63 20 3d 20 70 53 75 62 2d 3e 70 53 72 63  Src = pSub->pSrc
34c7a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 75 62  ;.  assert( pSub
34c7b 53 72 63 20 29 3b 0a 20 20 2f 2a 20 50 72 69 6f  Src );.  /* Prio
34c7c 72 20 74 6f 20 76 65 72 73 69 6f 6e 20 33 2e 31  r to version 3.1
34c7d 2e 32 2c 20 77 68 65 6e 20 4c 49 4d 49 54 20 61  .2, when LIMIT a
34c7e 6e 64 20 4f 46 46 53 45 54 20 68 61 64 20 74 6f  nd OFFSET had to
34c7f 20 62 65 20 73 69 6d 70 6c 65 20 63 6f 6e 73 74   be simple const
34c80 61 6e 74 73 2c 0a 20 20 2a 2a 20 6e 6f 74 20 61  ants,.  ** not a
34c81 72 62 69 74 72 61 72 79 20 65 78 70 72 65 73 73  rbitrary express
34c82 73 69 6f 6e 73 2c 20 77 65 20 61 6c 6c 6f 77 65  sions, we allowe
34c83 64 20 73 6f 6d 65 20 63 6f 6d 62 69 6e 69 6e 67  d some combining
34c84 20 6f 66 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46   of LIMIT and OF
34c85 46 53 45 54 0a 20 20 2a 2a 20 62 65 63 61 75 73  FSET.  ** becaus
34c86 65 20 74 68 65 79 20 63 6f 75 6c 64 20 62 65 20  e they could be 
34c87 63 6f 6d 70 75 74 65 64 20 61 74 20 63 6f 6d 70  computed at comp
34c88 69 6c 65 2d 74 69 6d 65 2e 20 20 42 75 74 20 77  ile-time.  But w
34c89 68 65 6e 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46  hen LIMIT and OF
34c8a 46 53 45 54 0a 20 20 2a 2a 20 62 65 63 61 6d 65  FSET.  ** became
34c8b 20 61 72 62 69 74 72 61 72 79 20 65 78 70 72 65   arbitrary expre
34c8c 73 73 69 6f 6e 73 2c 20 77 65 20 77 65 72 65 20  ssions, we were 
34c8d 66 6f 72 63 65 64 20 74 6f 20 61 64 64 20 72 65  forced to add re
34c8e 73 74 72 69 63 74 69 6f 6e 73 20 28 31 33 29 0a  strictions (13).
34c8f 20 20 2a 2a 20 61 6e 64 20 28 31 34 29 2e 20 2a    ** and (14). *
34c90 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4c  /.  if( pSub->pL
34c91 69 6d 69 74 20 26 26 20 70 2d 3e 70 4c 69 6d 69  imit && p->pLimi
34c92 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  t ) return 0;   
34c93 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
34c94 73 74 72 69 63 74 69 6f 6e 20 28 31 33 29 20 2a  striction (13) *
34c95 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4f  /.  if( pSub->pO
34c96 66 66 73 65 74 20 29 20 72 65 74 75 72 6e 20 30  ffset ) return 0
34c97 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
34c98 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
34c99 73 74 72 69 63 74 69 6f 6e 20 28 31 34 29 20 2a  striction (14) *
34c9a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68  /.  if( p->pRigh
34c9b 74 6d 6f 73 74 20 26 26 20 70 53 75 62 2d 3e 70  tmost && pSub->p
34c9c 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 72 65 74  Limit ){.    ret
34c9d 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
34c9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34c9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34ca0 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
34ca1 20 28 31 35 29 20 2a 2f 0a 20 20 7d 0a 20 20 69   (15) */.  }.  i
34ca2 66 28 20 70 53 75 62 53 72 63 2d 3e 6e 53 72 63  f( pSubSrc->nSrc
34ca3 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 20  ==0 ) return 0; 
34ca4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34ca5 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
34ca6 74 69 6f 6e 20 28 37 29 20 20 2a 2f 0a 20 20 69  tion (7)  */.  i
34ca7 66 28 20 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67  f( pSub->selFlag
34ca8 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 20  s & SF_Distinct 
34ca9 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
34caa 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
34cab 74 69 6f 6e 20 28 35 29 20 20 2a 2f 0a 20 20 69  tion (5)  */.  i
34cac 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20  f( pSub->pLimit 
34cad 26 26 20 28 70 53 72 63 2d 3e 6e 53 72 63 3e 31  && (pSrc->nSrc>1
34cae 20 7c 7c 20 69 73 41 67 67 29 20 29 7b 0a 20 20   || isAgg) ){.  
34caf 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20     return 0;    
34cb0 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74       /* Restrict
34cb1 69 6f 6e 73 20 28 38 29 28 39 29 20 2a 2f 0a 20  ions (8)(9) */. 
34cb2 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c   }.  if( (p->sel
34cb3 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74 69  Flags & SF_Disti
34cb4 6e 63 74 29 21 3d 30 20 26 26 20 73 75 62 71 75  nct)!=0 && subqu
34cb5 65 72 79 49 73 41 67 67 20 29 7b 0a 20 20 20 20  eryIsAgg ){.    
34cb6 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
34cb7 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
34cb8 6e 20 28 36 29 20 20 2a 2f 0a 20 20 7d 0a 20 20  n (6)  */.  }.  
34cb9 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20  if( p->pOrderBy 
34cba 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42  && pSub->pOrderB
34cbb 79 20 29 7b 0a 20 20 20 20 20 72 65 74 75 72 6e  y ){.     return
34cbc 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
34cbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34cbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34cbf 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 31   Restriction (11
34cc0 29 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 69  ) */.  }.  if( i
34cc1 73 41 67 67 20 26 26 20 70 53 75 62 2d 3e 70 4f  sAgg && pSub->pO
34cc2 72 64 65 72 42 79 20 29 20 72 65 74 75 72 6e 20  rderBy ) return 
34cc3 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
34cc4 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
34cc5 20 28 31 36 29 20 2a 2f 0a 20 20 69 66 28 20 70   (16) */.  if( p
34cc6 53 75 62 2d 3e 70 4c 69 6d 69 74 20 26 26 20 70  Sub->pLimit && p
34cc7 2d 3e 70 57 68 65 72 65 20 29 20 72 65 74 75 72  ->pWhere ) retur
34cc8 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
34cc9 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
34cca 20 28 31 39 29 20 2a 2f 0a 20 20 69 66 28 20 70   (19) */.  if( p
34ccb 53 75 62 2d 3e 70 4c 69 6d 69 74 20 26 26 20 28  Sub->pLimit && (
34ccc 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
34ccd 5f 44 69 73 74 69 6e 63 74 29 21 3d 30 20 29 7b  _Distinct)!=0 ){
34cce 0a 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  .     return 0; 
34ccf 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
34cd0 69 63 74 69 6f 6e 20 28 32 31 29 20 2a 2f 0a 20  iction (21) */. 
34cd1 20 7d 0a 0a 20 20 2f 2a 20 4f 42 53 4f 4c 45 54   }..  /* OBSOLET
34cd2 45 20 43 4f 4d 4d 45 4e 54 20 31 3a 0a 20 20 2a  E COMMENT 1:.  *
34cd3 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 33 3a  * Restriction 3:
34cd4 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65 72    If the subquer
34cd5 79 20 69 73 20 61 20 6a 6f 69 6e 2c 20 6d 61 6b  y is a join, mak
34cd6 65 20 73 75 72 65 20 74 68 65 20 73 75 62 71 75  e sure the subqu
34cd7 65 72 79 20 69 73 20 0a 20 20 2a 2a 20 6e 6f 74  ery is .  ** not
34cd8 20 75 73 65 64 20 61 73 20 74 68 65 20 72 69 67   used as the rig
34cd9 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e  ht operand of an
34cda 20 6f 75 74 65 72 20 6a 6f 69 6e 2e 20 20 45 78   outer join.  Ex
34cdb 61 6d 70 6c 65 73 20 6f 66 20 77 68 79 20 74 68  amples of why th
34cdc 69 73 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61  is.  ** is not a
34cdd 6c 6c 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a  llowed:.  **.  *
34cde 2a 20 20 20 20 20 20 20 20 20 74 31 20 4c 45 46  *         t1 LEF
34cdf 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 28 74 32  T OUTER JOIN (t2
34ce0 20 4a 4f 49 4e 20 74 33 29 0a 20 20 2a 2a 0a 20   JOIN t3).  **. 
34ce1 20 2a 2a 20 49 66 20 77 65 20 66 6c 61 74 74 65   ** If we flatte
34ce2 6e 20 74 68 65 20 61 62 6f 76 65 2c 20 77 65 20  n the above, we 
34ce3 77 6f 75 6c 64 20 67 65 74 0a 20 20 2a 2a 0a 20  would get.  **. 
34ce4 20 2a 2a 20 20 20 20 20 20 20 20 20 28 74 31 20   **         (t1 
34ce5 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20  LEFT OUTER JOIN 
34ce6 74 32 29 20 4a 4f 49 4e 20 74 33 0a 20 20 2a 2a  t2) JOIN t3.  **
34ce7 0a 20 20 2a 2a 20 77 68 69 63 68 20 69 73 20 6e  .  ** which is n
34ce8 6f 74 20 61 74 20 61 6c 6c 20 74 68 65 20 73 61  ot at all the sa
34ce9 6d 65 20 74 68 69 6e 67 2e 0a 20 20 2a 2a 0a 20  me thing..  **. 
34cea 20 2a 2a 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d   ** OBSOLETE COM
34ceb 4d 45 4e 54 20 32 3a 0a 20 20 2a 2a 20 52 65 73  MENT 2:.  ** Res
34cec 74 72 69 63 74 69 6f 6e 20 31 32 3a 20 20 49 66  triction 12:  If
34ced 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 73   the subquery is
34cee 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61   the right opera
34cef 6e 64 20 6f 66 20 61 20 6c 65 66 74 20 6f 75 74  nd of a left out
34cf0 65 72 0a 20 20 2a 2a 20 6a 6f 69 6e 2c 20 6d 61  er.  ** join, ma
34cf1 6b 65 20 73 75 72 65 20 74 68 65 20 73 75 62 71  ke sure the subq
34cf2 75 65 72 79 20 68 61 73 20 6e 6f 20 57 48 45 52  uery has no WHER
34cf3 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 20 41  E clause..  ** A
34cf4 6e 20 65 78 61 6d 70 6c 65 73 20 6f 66 20 77 68  n examples of wh
34cf5 79 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 6c  y this is not al
34cf6 6c 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  lowed:.  **.  **
34cf7 20 20 20 20 20 20 20 20 20 74 31 20 4c 45 46 54           t1 LEFT
34cf8 20 4f 55 54 45 52 20 4a 4f 49 4e 20 28 53 45 4c   OUTER JOIN (SEL
34cf9 45 43 54 20 2a 20 46 52 4f 4d 20 74 32 20 57 48  ECT * FROM t2 WH
34cfa 45 52 45 20 74 32 2e 78 3e 30 29 0a 20 20 2a 2a  ERE t2.x>0).  **
34cfb 0a 20 20 2a 2a 20 49 66 20 77 65 20 66 6c 61 74  .  ** If we flat
34cfc 74 65 6e 20 74 68 65 20 61 62 6f 76 65 2c 20 77  ten the above, w
34cfd 65 20 77 6f 75 6c 64 20 67 65 74 0a 20 20 2a 2a  e would get.  **
34cfe 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 28 74  .  **         (t
34cff 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  1 LEFT OUTER JOI
34d00 4e 20 74 32 29 20 57 48 45 52 45 20 74 32 2e 78  N t2) WHERE t2.x
34d01 3e 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 75 74  >0.  **.  ** But
34d02 20 74 68 65 20 74 32 2e 78 3e 30 20 74 65 73 74   the t2.x>0 test
34d03 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 66 61 69   will always fai
34d04 6c 20 6f 6e 20 61 20 4e 55 4c 4c 20 72 6f 77 20  l on a NULL row 
34d05 6f 66 20 74 32 2c 20 77 68 69 63 68 0a 20 20 2a  of t2, which.  *
34d06 2a 20 65 66 66 65 63 74 69 76 65 6c 79 20 63 6f  * effectively co
34d07 6e 76 65 72 74 73 20 74 68 65 20 4f 55 54 45 52  nverts the OUTER
34d08 20 4a 4f 49 4e 20 69 6e 74 6f 20 61 6e 20 49 4e   JOIN into an IN
34d09 4e 45 52 20 4a 4f 49 4e 2e 0a 20 20 2a 2a 0a 20  NER JOIN..  **. 
34d0a 20 2a 2a 20 54 48 49 53 20 4f 56 45 52 52 49 44   ** THIS OVERRID
34d0b 45 53 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d  ES OBSOLETE COMM
34d0c 45 4e 54 53 20 31 20 41 4e 44 20 32 20 41 42 4f  ENTS 1 AND 2 ABO
34d0d 56 45 3a 0a 20 20 2a 2a 20 54 69 63 6b 65 74 20  VE:.  ** Ticket 
34d0e 23 33 33 30 30 20 73 68 6f 77 73 20 74 68 61 74  #3300 shows that
34d0f 20 66 6c 61 74 74 65 6e 69 6e 67 20 74 68 65 20   flattening the 
34d10 72 69 67 68 74 20 74 65 72 6d 20 6f 66 20 61 20  right term of a 
34d11 4c 45 46 54 20 4a 4f 49 4e 0a 20 20 2a 2a 20 69  LEFT JOIN.  ** i
34d12 73 20 66 72 61 75 67 68 74 20 77 69 74 68 20 64  s fraught with d
34d13 61 6e 67 65 72 2e 20 20 42 65 73 74 20 74 6f 20  anger.  Best to 
34d14 61 76 6f 69 64 20 74 68 65 20 77 68 6f 6c 65 20  avoid the whole 
34d15 74 68 69 6e 67 2e 20 20 49 66 20 74 68 65 0a 20  thing.  If the. 
34d16 20 2a 2a 20 73 75 62 71 75 65 72 79 20 69 73 20   ** subquery is 
34d17 74 68 65 20 72 69 67 68 74 20 74 65 72 6d 20 6f  the right term o
34d18 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 2c 20 74  f a LEFT JOIN, t
34d19 68 65 6e 20 64 6f 20 6e 6f 74 20 66 6c 61 74 74  hen do not flatt
34d1a 65 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  en..  */.  if( (
34d1b 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e 74 79  pSubitem->jointy
34d1c 70 65 20 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d  pe & JT_OUTER)!=
34d1d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
34d1e 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73  0;.  }..  /* Res
34d1f 74 72 69 63 74 69 6f 6e 20 31 37 3a 20 49 66 20  triction 17: If 
34d20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20 69 73  the sub-query is
34d21 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45   a compound SELE
34d22 43 54 2c 20 74 68 65 6e 20 69 74 20 6d 75 73 74  CT, then it must
34d23 0a 20 20 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74  .  ** use only t
34d24 68 65 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65  he UNION ALL ope
34d25 72 61 74 6f 72 2e 20 41 6e 64 20 6e 6f 6e 65 20  rator. And none 
34d26 6f 66 20 74 68 65 20 73 69 6d 70 6c 65 20 73 65  of the simple se
34d27 6c 65 63 74 20 71 75 65 72 69 65 73 0a 20 20 2a  lect queries.  *
34d28 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74  * that make up t
34d29 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45  he compound SELE
34d2a 43 54 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74  CT are allowed t
34d2b 6f 20 62 65 20 61 67 67 72 65 67 61 74 65 20 6f  o be aggregate o
34d2c 72 20 64 69 73 74 69 6e 63 74 0a 20 20 2a 2a 20  r distinct.  ** 
34d2d 71 75 65 72 69 65 73 2e 0a 20 20 2a 2f 0a 20 20  queries..  */.  
34d2e 69 66 28 20 70 53 75 62 2d 3e 70 50 72 69 6f 72  if( pSub->pPrior
34d2f 20 29 7b 0a 20 20 20 20 69 66 28 20 70 53 75 62   ){.    if( pSub
34d30 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
34d31 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 2f      return 0;  /
34d32 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 32 30  * Restriction 20
34d33 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   */.    }.    if
34d34 28 20 69 73 41 67 67 20 7c 7c 20 28 70 2d 3e 73  ( isAgg || (p->s
34d35 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73  elFlags & SF_Dis
34d36 74 69 6e 63 74 29 21 3d 30 20 7c 7c 20 70 53 72  tinct)!=0 || pSr
34d37 63 2d 3e 6e 53 72 63 21 3d 31 20 29 7b 0a 20 20  c->nSrc!=1 ){.  
34d38 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
34d39 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 53 75 62    }.    for(pSub
34d3a 31 3d 70 53 75 62 3b 20 70 53 75 62 31 3b 20 70  1=pSub; pSub1; p
34d3b 53 75 62 31 3d 70 53 75 62 31 2d 3e 70 50 72 69  Sub1=pSub1->pPri
34d3c 6f 72 29 7b 0a 20 20 20 20 20 20 74 65 73 74 63  or){.      testc
34d3d 61 73 65 28 20 28 70 53 75 62 31 2d 3e 73 65 6c  ase( (pSub1->sel
34d3e 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74  Flags & (SF_Dist
34d3f 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74  inct|SF_Aggregat
34d40 65 29 29 3d 3d 53 46 5f 44 69 73 74 69 6e 63 74  e))==SF_Distinct
34d41 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
34d42 73 65 28 20 28 70 53 75 62 31 2d 3e 73 65 6c 46  se( (pSub1->selF
34d43 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69  lags & (SF_Disti
34d44 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65  nct|SF_Aggregate
34d45 29 29 3d 3d 53 46 5f 41 67 67 72 65 67 61 74 65  ))==SF_Aggregate
34d46 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
34d47 28 20 70 53 75 62 2d 3e 70 53 72 63 21 3d 30 20  ( pSub->pSrc!=0 
34d48 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 53  );.      if( (pS
34d49 75 62 31 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20  ub1->selFlags & 
34d4a 28 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f  (SF_Distinct|SF_
34d4b 41 67 67 72 65 67 61 74 65 29 29 21 3d 30 0a 20  Aggregate))!=0. 
34d4c 20 20 20 20 20 20 7c 7c 20 28 70 53 75 62 31 2d        || (pSub1-
34d4d 3e 70 50 72 69 6f 72 20 26 26 20 70 53 75 62 31  >pPrior && pSub1
34d4e 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29 20 0a 20  ->op!=TK_ALL) . 
34d4f 20 20 20 20 20 20 7c 7c 20 70 53 75 62 31 2d 3e        || pSub1->
34d50 70 53 72 63 2d 3e 6e 53 72 63 3c 31 0a 20 20 20  pSrc->nSrc<1.   
34d51 20 20 20 20 7c 7c 20 70 53 75 62 2d 3e 70 45 4c      || pSub->pEL
34d52 69 73 74 2d 3e 6e 45 78 70 72 21 3d 70 53 75 62  ist->nExpr!=pSub
34d53 31 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  1->pEList->nExpr
34d54 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
34d55 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
34d56 20 20 7d 0a 20 20 20 20 20 20 74 65 73 74 63 61    }.      testca
34d57 73 65 28 20 70 53 75 62 31 2d 3e 70 53 72 63 2d  se( pSub1->pSrc-
34d58 3e 6e 53 72 63 3e 31 20 29 3b 0a 20 20 20 20 7d  >nSrc>1 );.    }
34d59 0a 0a 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63  ..    /* Restric
34d5a 74 69 6f 6e 20 31 38 2e 20 2a 2f 0a 20 20 20 20  tion 18. */.    
34d5b 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20  if( p->pOrderBy 
34d5c 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 69 3b  ){.      int ii;
34d5d 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b  .      for(ii=0;
34d5e 20 69 69 3c 70 2d 3e 70 4f 72 64 65 72 42 79 2d   ii<p->pOrderBy-
34d5f 3e 6e 45 78 70 72 3b 20 69 69 2b 2b 29 7b 0a 20  >nExpr; ii++){. 
34d60 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4f         if( p->pO
34d61 72 64 65 72 42 79 2d 3e 61 5b 69 69 5d 2e 69 4f  rderBy->a[ii].iO
34d62 72 64 65 72 42 79 43 6f 6c 3d 3d 30 20 29 20 72  rderByCol==0 ) r
34d63 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
34d64 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
34d65 2a 2a 2a 2a 20 49 66 20 77 65 20 72 65 61 63 68  **** If we reach
34d66 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 66 6c 61   this point, fla
34d67 74 74 65 6e 69 6e 67 20 69 73 20 70 65 72 6d 69  ttening is permi
34d68 74 74 65 64 2e 20 2a 2a 2a 2a 2a 2f 0a 0a 20 20  tted. *****/..  
34d69 2f 2a 20 41 75 74 68 6f 72 69 7a 65 20 74 68 65  /* Authorize the
34d6a 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 70   subquery */.  p
34d6b 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
34d6c 65 78 74 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e  ext = pSubitem->
34d6d 7a 4e 61 6d 65 3b 0a 20 20 54 45 53 54 4f 4e 4c  zName;.  TESTONL
34d6e 59 28 69 20 3d 29 20 73 71 6c 69 74 65 33 41 75  Y(i =) sqlite3Au
34d6f 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
34d70 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 2c 20 30  SQLITE_SELECT, 0
34d71 2c 20 30 2c 20 30 29 3b 0a 20 20 74 65 73 74 63  , 0, 0);.  testc
34d72 61 73 65 28 20 69 3d 3d 53 51 4c 49 54 45 5f 44  ase( i==SQLITE_D
34d73 45 4e 59 20 29 3b 0a 20 20 70 50 61 72 73 65 2d  ENY );.  pParse-
34d74 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20  >zAuthContext = 
34d75 7a 53 61 76 65 64 41 75 74 68 43 6f 6e 74 65 78  zSavedAuthContex
34d76 74 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  t;..  /* If the 
34d77 73 75 62 2d 71 75 65 72 79 20 69 73 20 61 20 63  sub-query is a c
34d78 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73  ompound SELECT s
34d79 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 28  tatement, then (
34d7a 62 79 20 72 65 73 74 72 69 63 74 69 6f 6e 73 0a  by restrictions.
34d7b 20 20 2a 2a 20 31 37 20 61 6e 64 20 31 38 20 61    ** 17 and 18 a
34d7c 62 6f 76 65 29 20 69 74 20 6d 75 73 74 20 62 65  bove) it must be
34d7d 20 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 61 6e 64   a UNION ALL and
34d7e 20 74 68 65 20 70 61 72 65 6e 74 20 71 75 65 72   the parent quer
34d7f 79 20 6d 75 73 74 20 0a 20 20 2a 2a 20 62 65 20  y must .  ** be 
34d80 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20 20 2a  of the form:.  *
34d81 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43  *.  **     SELEC
34d82 54 20 3c 65 78 70 72 2d 6c 69 73 74 3e 20 46 52  T <expr-list> FR
34d83 4f 4d 20 28 3c 73 75 62 2d 71 75 65 72 79 3e 29  OM (<sub-query>)
34d84 20 3c 77 68 65 72 65 2d 63 6c 61 75 73 65 3e 20   <where-clause> 
34d85 0a 20 20 2a 2a 0a 20 20 2a 2a 20 66 6f 6c 6c 6f  .  **.  ** follo
34d86 77 65 64 20 62 79 20 61 6e 79 20 4f 52 44 45 52  wed by any ORDER
34d87 20 42 59 2c 20 4c 49 4d 49 54 20 61 6e 64 2f 6f   BY, LIMIT and/o
34d88 72 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73  r OFFSET clauses
34d89 2e 20 54 68 69 73 20 62 6c 6f 63 6b 0a 20 20 2a  . This block.  *
34d8a 2a 20 63 72 65 61 74 65 73 20 4e 2d 31 20 63 6f  * creates N-1 co
34d8b 70 69 65 73 20 6f 66 20 74 68 65 20 70 61 72 65  pies of the pare
34d8c 6e 74 20 71 75 65 72 79 20 77 69 74 68 6f 75 74  nt query without
34d8d 20 61 6e 79 20 4f 52 44 45 52 20 42 59 2c 20 4c   any ORDER BY, L
34d8e 49 4d 49 54 20 6f 72 20 0a 20 20 2a 2a 20 4f 46  IMIT or .  ** OF
34d8f 46 53 45 54 20 63 6c 61 75 73 65 73 20 61 6e 64  FSET clauses and
34d90 20 6a 6f 69 6e 73 20 74 68 65 6d 20 74 6f 20 74   joins them to t
34d91 68 65 20 6c 65 66 74 2d 68 61 6e 64 2d 73 69 64  he left-hand-sid
34d92 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61  e of the origina
34d93 6c 0a 20 20 2a 2a 20 75 73 69 6e 67 20 55 4e 49  l.  ** using UNI
34d94 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 73  ON ALL operators
34d95 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 4e  . In this case N
34d96 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
34d97 66 20 73 69 6d 70 6c 65 0a 20 20 2a 2a 20 73 65  f simple.  ** se
34d98 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 73 20  lect statements 
34d99 69 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  in the compound 
34d9a 73 75 62 2d 71 75 65 72 79 2e 0a 20 20 2a 2a 0a  sub-query..  **.
34d9b 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20    ** Example:.  
34d9c 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45  **.  **     SELE
34d9d 43 54 20 61 2b 31 20 46 52 4f 4d 20 28 0a 20 20  CT a+1 FROM (.  
34d9e 2a 2a 20 20 20 20 20 20 20 20 53 45 4c 45 43 54  **        SELECT
34d9f 20 78 20 46 52 4f 4d 20 74 61 62 0a 20 20 2a 2a   x FROM tab.  **
34da0 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c          UNION AL
34da1 4c 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 53 45  L.  **        SE
34da2 4c 45 43 54 20 79 20 46 52 4f 4d 20 74 61 62 0a  LECT y FROM tab.
34da3 20 20 2a 2a 20 20 20 20 20 20 20 20 55 4e 49 4f    **        UNIO
34da4 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 20  N ALL.  **      
34da5 20 20 53 45 4c 45 43 54 20 61 62 73 28 7a 2a 32    SELECT abs(z*2
34da6 29 20 46 52 4f 4d 20 74 61 62 32 0a 20 20 2a 2a  ) FROM tab2.  **
34da7 20 20 20 20 20 29 20 57 48 45 52 45 20 61 21 3d       ) WHERE a!=
34da8 35 20 4f 52 44 45 52 20 42 59 20 31 0a 20 20 2a  5 ORDER BY 1.  *
34da9 2a 0a 20 20 2a 2a 20 54 72 61 6e 73 66 6f 72 6d  *.  ** Transform
34daa 65 64 20 69 6e 74 6f 3a 0a 20 20 2a 2a 0a 20 20  ed into:.  **.  
34dab 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 78 2b  **     SELECT x+
34dac 31 20 46 52 4f 4d 20 74 61 62 20 57 48 45 52 45  1 FROM tab WHERE
34dad 20 78 2b 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20   x+1!=5.  **    
34dae 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20   UNION ALL.  ** 
34daf 20 20 20 20 53 45 4c 45 43 54 20 79 2b 31 20 46      SELECT y+1 F
34db0 52 4f 4d 20 74 61 62 20 57 48 45 52 45 20 79 2b  ROM tab WHERE y+
34db1 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20 55 4e  1!=5.  **     UN
34db2 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20  ION ALL.  **    
34db3 20 53 45 4c 45 43 54 20 61 62 73 28 7a 2a 32 29   SELECT abs(z*2)
34db4 2b 31 20 46 52 4f 4d 20 74 61 62 32 20 57 48 45  +1 FROM tab2 WHE
34db5 52 45 20 61 62 73 28 7a 2a 32 29 2b 31 21 3d 35  RE abs(z*2)+1!=5
34db6 0a 20 20 2a 2a 20 20 20 20 20 4f 52 44 45 52 20  .  **     ORDER 
34db7 42 59 20 31 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  BY 1.  **.  ** W
34db8 65 20 63 61 6c 6c 20 74 68 69 73 20 74 68 65 20  e call this the 
34db9 22 63 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65  "compound-subque
34dba 72 79 20 66 6c 61 74 74 65 6e 69 6e 67 22 2e 0a  ry flattening"..
34dbb 20 20 2a 2f 0a 20 20 66 6f 72 28 70 53 75 62 3d    */.  for(pSub=
34dbc 70 53 75 62 2d 3e 70 50 72 69 6f 72 3b 20 70 53  pSub->pPrior; pS
34dbd 75 62 3b 20 70 53 75 62 3d 70 53 75 62 2d 3e 70  ub; pSub=pSub->p
34dbe 50 72 69 6f 72 29 7b 0a 20 20 20 20 53 65 6c 65  Prior){.    Sele
34dbf 63 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 45 78  ct *pNew;.    Ex
34dc0 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
34dc1 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a   = p->pOrderBy;.
34dc2 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74      Expr *pLimit
34dc3 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20   = p->pLimit;.  
34dc4 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72    Select *pPrior
34dc5 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20   = p->pPrior;.  
34dc6 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20    p->pOrderBy = 
34dc7 30 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d  0;.    p->pSrc =
34dc8 20 30 3b 0a 20 20 20 20 70 2d 3e 70 50 72 69 6f   0;.    p->pPrio
34dc9 72 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 4c  r = 0;.    p->pL
34dca 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 70 4e  imit = 0;.    pN
34dcb 65 77 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  ew = sqlite3Sele
34dcc 63 74 44 75 70 28 64 62 2c 20 70 2c 20 30 29 3b  ctDup(db, p, 0);
34dcd 0a 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d  .    p->pLimit =
34dce 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 70 2d 3e   pLimit;.    p->
34dcf 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65  pOrderBy = pOrde
34dd0 72 42 79 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63  rBy;.    p->pSrc
34dd1 20 3d 20 70 53 72 63 3b 0a 20 20 20 20 70 2d 3e   = pSrc;.    p->
34dd2 6f 70 20 3d 20 54 4b 5f 41 4c 4c 3b 0a 20 20 20  op = TK_ALL;.   
34dd3 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d   p->pRightmost =
34dd4 20 30 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77   0;.    if( pNew
34dd5 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 65  ==0 ){.      pNe
34dd6 77 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20  w = pPrior;.    
34dd7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4e 65  }else{.      pNe
34dd8 77 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69  w->pPrior = pPri
34dd9 6f 72 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  or;.      pNew->
34dda 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a  pRightmost = 0;.
34ddb 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 50 72      }.    p->pPr
34ddc 69 6f 72 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  ior = pNew;.    
34ddd 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
34dde 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 31 3b  iled ) return 1;
34ddf 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e  .  }..  /* Begin
34de0 20 66 6c 61 74 74 65 6e 69 6e 67 20 74 68 65 20   flattening the 
34de1 69 46 72 6f 6d 2d 74 68 20 65 6e 74 72 79 20 6f  iFrom-th entry o
34de2 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
34de3 65 20 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 6f  e .  ** in the o
34de4 75 74 65 72 20 71 75 65 72 79 2e 0a 20 20 2a 2f  uter query..  */
34de5 0a 20 20 70 53 75 62 20 3d 20 70 53 75 62 31 20  .  pSub = pSub1 
34de6 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c  = pSubitem->pSel
34de7 65 63 74 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74  ect;..  /* Delet
34de8 65 20 74 68 65 20 74 72 61 6e 73 69 65 6e 74 20  e the transient 
34de9 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  table structure 
34dea 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
34deb 74 68 65 0a 20 20 2a 2a 20 73 75 62 71 75 65 72  the.  ** subquer
34dec 79 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  y.  */.  sqlite3
34ded 44 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69  DbFree(db, pSubi
34dee 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b  tem->zDatabase);
34def 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
34df0 28 64 62 2c 20 70 53 75 62 69 74 65 6d 2d 3e 7a  (db, pSubitem->z
34df1 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
34df2 44 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69  DbFree(db, pSubi
34df3 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20  tem->zAlias);.  
34df4 70 53 75 62 69 74 65 6d 2d 3e 7a 44 61 74 61 62  pSubitem->zDatab
34df5 61 73 65 20 3d 20 30 3b 0a 20 20 70 53 75 62 69  ase = 0;.  pSubi
34df6 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a  tem->zName = 0;.
34df7 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a 41 6c 69    pSubitem->zAli
34df8 61 73 20 3d 20 30 3b 0a 20 20 70 53 75 62 69 74  as = 0;.  pSubit
34df9 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 30 3b  em->pSelect = 0;
34dfa 0a 0a 20 20 2f 2a 20 44 65 66 65 72 20 64 65 6c  ..  /* Defer del
34dfb 65 74 69 6e 67 20 74 68 65 20 54 61 62 6c 65 20  eting the Table 
34dfc 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65  object associate
34dfd 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20  d with the.  ** 
34dfe 73 75 62 71 75 65 72 79 20 75 6e 74 69 6c 20 63  subquery until c
34dff 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 69  ode generation i
34e00 73 0a 20 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 2c  s.  ** complete,
34e01 20 73 69 6e 63 65 20 74 68 65 72 65 20 6d 61 79   since there may
34e02 20 73 74 69 6c 6c 20 65 78 69 73 74 20 45 78 70   still exist Exp
34e03 72 2e 70 54 61 62 20 65 6e 74 72 69 65 73 20 74  r.pTab entries t
34e04 68 61 74 0a 20 20 2a 2a 20 72 65 66 65 72 20 74  hat.  ** refer t
34e05 6f 20 74 68 65 20 73 75 62 71 75 65 72 79 20 65  o the subquery e
34e06 76 65 6e 20 61 66 74 65 72 20 66 6c 61 74 74 65  ven after flatte
34e07 6e 69 6e 67 2e 20 20 54 69 63 6b 65 74 20 23 33  ning.  Ticket #3
34e08 33 34 36 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 70  346..  **.  ** p
34e09 53 75 62 69 74 65 6d 2d 3e 70 54 61 62 20 69 73  Subitem->pTab is
34e0a 20 61 6c 77 61 79 73 20 6e 6f 6e 2d 4e 55 4c 4c   always non-NULL
34e0b 20 62 79 20 74 65 73 74 20 72 65 73 74 72 69 63   by test restric
34e0c 74 69 6f 6e 73 20 61 6e 64 20 74 65 73 74 73 20  tions and tests 
34e0d 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  above..  */.  if
34e0e 28 20 41 4c 57 41 59 53 28 70 53 75 62 69 74 65  ( ALWAYS(pSubite
34e0f 6d 2d 3e 70 54 61 62 21 3d 30 29 20 29 7b 0a 20  m->pTab!=0) ){. 
34e10 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 54 6f     Table *pTabTo
34e11 44 65 6c 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e  Del = pSubitem->
34e12 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 70 54  pTab;.    if( pT
34e13 61 62 54 6f 44 65 6c 2d 3e 6e 52 65 66 3d 3d 31  abToDel->nRef==1
34e14 20 29 7b 0a 20 20 20 20 20 20 50 61 72 73 65 20   ){.      Parse 
34e15 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c  *pToplevel = sql
34e16 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65  ite3ParseTopleve
34e17 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  l(pParse);.     
34e18 20 70 54 61 62 54 6f 44 65 6c 2d 3e 70 4e 65 78   pTabToDel->pNex
34e19 74 5a 6f 6d 62 69 65 20 3d 20 70 54 6f 70 6c 65  tZombie = pTople
34e1a 76 65 6c 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 3b  vel->pZombieTab;
34e1b 0a 20 20 20 20 20 20 70 54 6f 70 6c 65 76 65 6c  .      pToplevel
34e1c 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 3d 20 70  ->pZombieTab = p
34e1d 54 61 62 54 6f 44 65 6c 3b 0a 20 20 20 20 7d 65  TabToDel;.    }e
34e1e 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 61 62 54  lse{.      pTabT
34e1f 6f 44 65 6c 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20  oDel->nRef--;.  
34e20 20 20 7d 0a 20 20 20 20 70 53 75 62 69 74 65 6d    }.    pSubitem
34e21 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 7d 0a  ->pTab = 0;.  }.
34e22 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  /* The follow
34e23 69 6e 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e  ing loop runs on
34e24 63 65 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d  ce for each term
34e25 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 2d 73   in a compound-s
34e26 75 62 71 75 65 72 79 0a 20 20 2a 2a 20 66 6c 61  ubquery.  ** fla
34e27 74 74 65 6e 69 6e 67 20 28 61 73 20 64 65 73 63  ttening (as desc
34e28 72 69 62 65 64 20 61 62 6f 76 65 29 2e 20 20 49  ribed above).  I
34e29 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20 61  f we are doing a
34e2a 20 64 69 66 66 65 72 65 6e 74 20 6b 69 6e 64 0a   different kind.
34e2b 20 20 2a 2a 20 6f 66 20 66 6c 61 74 74 65 6e 69    ** of flatteni
34e2c 6e 67 20 2d 20 61 20 66 6c 61 74 74 65 6e 69 6e  ng - a flattenin
34e2d 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20 63  g other than a c
34e2e 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72 79  ompound-subquery
34e2f 20 66 6c 61 74 74 65 6e 69 6e 67 20 2d 0a 20 20   flattening -.  
34e30 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 6c 6f 6f  ** then this loo
34e31 70 20 6f 6e 6c 79 20 72 75 6e 73 20 6f 6e 63 65  p only runs once
34e32 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73  ..  **.  ** This
34e33 20 6c 6f 6f 70 20 6d 6f 76 65 73 20 61 6c 6c 20   loop moves all 
34e34 6f 66 20 74 68 65 20 46 52 4f 4d 20 65 6c 65 6d  of the FROM elem
34e35 65 6e 74 73 20 6f 66 20 74 68 65 20 73 75 62 71  ents of the subq
34e36 75 65 72 79 20 69 6e 74 6f 20 74 68 65 0a 20 20  uery into the.  
34e37 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  ** the FROM clau
34e38 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20  se of the outer 
34e39 71 75 65 72 79 2e 20 20 42 65 66 6f 72 65 20 64  query.  Before d
34e3a 6f 69 6e 67 20 74 68 69 73 2c 20 72 65 6d 65 6d  oing this, remem
34e3b 62 65 72 0a 20 20 2a 2a 20 74 68 65 20 63 75 72  ber.  ** the cur
34e3c 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  sor number for t
34e3d 68 65 20 6f 72 69 67 69 6e 61 6c 20 6f 75 74 65  he original oute
34e3e 72 20 71 75 65 72 79 20 46 52 4f 4d 20 65 6c 65  r query FROM ele
34e3f 6d 65 6e 74 20 69 6e 0a 20 20 2a 2a 20 69 50 61  ment in.  ** iPa
34e40 72 65 6e 74 2e 20 20 54 68 65 20 69 50 61 72 65  rent.  The iPare
34e41 6e 74 20 63 75 72 73 6f 72 20 77 69 6c 6c 20 6e  nt cursor will n
34e42 65 76 65 72 20 62 65 20 75 73 65 64 2e 20 20 53  ever be used.  S
34e43 75 62 73 65 71 75 65 6e 74 20 63 6f 64 65 0a 20  ubsequent code. 
34e44 20 2a 2a 20 77 69 6c 6c 20 73 63 61 6e 20 65 78   ** will scan ex
34e45 70 72 65 73 73 69 6f 6e 73 20 6c 6f 6f 6b 69 6e  pressions lookin
34e46 67 20 66 6f 72 20 69 50 61 72 65 6e 74 20 72 65  g for iParent re
34e47 66 65 72 65 6e 63 65 73 20 61 6e 64 20 72 65 70  ferences and rep
34e48 6c 61 63 65 0a 20 20 2a 2a 20 74 68 6f 73 65 20  lace.  ** those 
34e49 72 65 66 65 72 65 6e 63 65 73 20 77 69 74 68 20  references with 
34e4a 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74  expressions that
34e4b 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20   resolve to the 
34e4c 73 75 62 71 75 65 72 79 20 46 52 4f 4d 0a 20 20  subquery FROM.  
34e4d 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 77 65 20 61  ** elements we a
34e4e 72 65 20 6e 6f 77 20 63 6f 70 79 69 6e 67 20 69  re now copying i
34e4f 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 50  n..  */.  for(pP
34e50 61 72 65 6e 74 3d 70 3b 20 70 50 61 72 65 6e 74  arent=p; pParent
34e51 3b 20 70 50 61 72 65 6e 74 3d 70 50 61 72 65 6e  ; pParent=pParen
34e52 74 2d 3e 70 50 72 69 6f 72 2c 20 70 53 75 62 3d  t->pPrior, pSub=
34e53 70 53 75 62 2d 3e 70 50 72 69 6f 72 29 7b 0a 20  pSub->pPrior){. 
34e54 20 20 20 69 6e 74 20 6e 53 75 62 53 72 63 3b 0a     int nSubSrc;.
34e55 20 20 20 20 75 38 20 6a 6f 69 6e 74 79 70 65 20      u8 jointype 
34e56 3d 20 30 3b 0a 20 20 20 20 70 53 75 62 53 72 63  = 0;.    pSubSrc
34e57 20 3d 20 70 53 75 62 2d 3e 70 53 72 63 3b 20 20   = pSub->pSrc;  
34e58 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73     /* FROM claus
34e59 65 20 6f 66 20 73 75 62 71 75 65 72 79 20 2a 2f  e of subquery */
34e5a 0a 20 20 20 20 6e 53 75 62 53 72 63 20 3d 20 70  .    nSubSrc = p
34e5b 53 75 62 53 72 63 2d 3e 6e 53 72 63 3b 20 20 2f  SubSrc->nSrc;  /
34e5c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
34e5d 73 20 69 6e 20 73 75 62 71 75 65 72 79 20 46 52  s in subquery FR
34e5e 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20  OM clause */.   
34e5f 20 70 53 72 63 20 3d 20 70 50 61 72 65 6e 74 2d   pSrc = pParent-
34e60 3e 70 53 72 63 3b 20 20 20 20 20 2f 2a 20 46 52  >pSrc;     /* FR
34e61 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  OM clause of the
34e62 20 6f 75 74 65 72 20 71 75 65 72 79 20 2a 2f 0a   outer query */.
34e63 0a 20 20 20 20 69 66 28 20 70 53 72 63 20 29 7b  .    if( pSrc ){
34e64 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
34e65 50 61 72 65 6e 74 3d 3d 70 20 29 3b 20 20 2f 2a  Parent==p );  /*
34e66 20 46 69 72 73 74 20 74 69 6d 65 20 74 68 72 6f   First time thro
34e67 75 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a  ugh the loop */.
34e68 20 20 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d        jointype =
34e69 20 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e 74   pSubitem->joint
34e6a 79 70 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ype;.    }else{.
34e6b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
34e6c 61 72 65 6e 74 21 3d 70 20 29 3b 20 20 2f 2a 20  arent!=p );  /* 
34e6d 32 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65  2nd and subseque
34e6e 6e 74 20 74 69 6d 65 73 20 74 68 72 6f 75 67 68  nt times through
34e6f 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20   the loop */.   
34e70 20 20 20 70 53 72 63 20 3d 20 70 50 61 72 65 6e     pSrc = pParen
34e71 74 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 74 65  t->pSrc = sqlite
34e72 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64  3SrcListAppend(d
34e73 62 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20  b, 0, 0, 0);.   
34e74 20 20 20 69 66 28 20 70 53 72 63 3d 3d 30 20 29     if( pSrc==0 )
34e75 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
34e76 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
34e77 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 62 72  ed );.        br
34e78 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
34e79 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 73   }..    /* The s
34e7a 75 62 71 75 65 72 79 20 75 73 65 73 20 61 20 73  ubquery uses a s
34e7b 69 6e 67 6c 65 20 73 6c 6f 74 20 6f 66 20 74 68  ingle slot of th
34e7c 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
34e7d 20 74 68 65 20 6f 75 74 65 72 0a 20 20 20 20 2a   the outer.    *
34e7e 2a 20 71 75 65 72 79 2e 20 20 49 66 20 74 68 65  * query.  If the
34e7f 20 73 75 62 71 75 65 72 79 20 68 61 73 20 6d 6f   subquery has mo
34e80 72 65 20 74 68 61 6e 20 6f 6e 65 20 65 6c 65 6d  re than one elem
34e81 65 6e 74 20 69 6e 20 69 74 73 20 46 52 4f 4d 20  ent in its FROM 
34e82 63 6c 61 75 73 65 2c 0a 20 20 20 20 2a 2a 20 74  clause,.    ** t
34e83 68 65 6e 20 65 78 70 61 6e 64 20 74 68 65 20 6f  hen expand the o
34e84 75 74 65 72 20 71 75 65 72 79 20 74 6f 20 6d 61  uter query to ma
34e85 6b 65 20 73 70 61 63 65 20 66 6f 72 20 69 74 20  ke space for it 
34e86 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 65 6c 65 6d  to hold all elem
34e87 65 6e 74 73 0a 20 20 20 20 2a 2a 20 6f 66 20 74  ents.    ** of t
34e88 68 65 20 73 75 62 71 75 65 72 79 2e 0a 20 20 20  he subquery..   
34e89 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70   **.    ** Examp
34e8a 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  le:.    **.    *
34e8b 2a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  *    SELECT * FR
34e8c 4f 4d 20 74 61 62 41 2c 20 28 53 45 4c 45 43 54  OM tabA, (SELECT
34e8d 20 2a 20 46 52 4f 4d 20 73 75 62 31 2c 20 73 75   * FROM sub1, su
34e8e 62 32 29 2c 20 74 61 62 42 3b 0a 20 20 20 20 2a  b2), tabB;.    *
34e8f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f 75 74  *.    ** The out
34e90 65 72 20 71 75 65 72 79 20 68 61 73 20 33 20 73  er query has 3 s
34e91 6c 6f 74 73 20 69 6e 20 69 74 73 20 46 52 4f 4d  lots in its FROM
34e92 20 63 6c 61 75 73 65 2e 20 20 4f 6e 65 20 73 6c   clause.  One sl
34e93 6f 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a  ot of the.    **
34e94 20 6f 75 74 65 72 20 71 75 65 72 79 20 28 74 68   outer query (th
34e95 65 20 6d 69 64 64 6c 65 20 73 6c 6f 74 29 20 69  e middle slot) i
34e96 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73 75  s used by the su
34e97 62 71 75 65 72 79 2e 20 20 54 68 65 20 6e 65 78  bquery.  The nex
34e98 74 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 6f  t.    ** block o
34e99 66 20 63 6f 64 65 20 77 69 6c 6c 20 65 78 70 61  f code will expa
34e9a 6e 64 20 74 68 65 20 6f 75 74 20 71 75 65 72 79  nd the out query
34e9b 20 74 6f 20 34 20 73 6c 6f 74 73 2e 20 20 54 68   to 4 slots.  Th
34e9c 65 20 6d 69 64 64 6c 65 0a 20 20 20 20 2a 2a 20  e middle.    ** 
34e9d 73 6c 6f 74 20 69 73 20 65 78 70 61 6e 64 65 64  slot is expanded
34e9e 20 74 6f 20 74 77 6f 20 73 6c 6f 74 73 20 69 6e   to two slots in
34e9f 20 6f 72 64 65 72 20 74 6f 20 6d 61 6b 65 20 73   order to make s
34ea0 70 61 63 65 20 66 6f 72 20 74 68 65 0a 20 20 20  pace for the.   
34ea1 20 2a 2a 20 74 77 6f 20 65 6c 65 6d 65 6e 74 73   ** two elements
34ea2 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
34ea3 75 73 65 20 6f 66 20 74 68 65 20 73 75 62 71 75  use of the subqu
34ea4 65 72 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ery..    */.    
34ea5 69 66 28 20 6e 53 75 62 53 72 63 3e 31 20 29 7b  if( nSubSrc>1 ){
34ea6 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  .      pParent->
34ea7 70 53 72 63 20 3d 20 70 53 72 63 20 3d 20 73 71  pSrc = pSrc = sq
34ea8 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61  lite3SrcListEnla
34ea9 72 67 65 28 64 62 2c 20 70 53 72 63 2c 20 6e 53  rge(db, pSrc, nS
34eaa 75 62 53 72 63 2d 31 2c 69 46 72 6f 6d 2b 31 29  ubSrc-1,iFrom+1)
34eab 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e  ;.      if( db->
34eac 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
34ead 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
34eae 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
34eaf 20 20 2f 2a 20 54 72 61 6e 73 66 65 72 20 74 68    /* Transfer th
34eb0 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65  e FROM clause te
34eb1 72 6d 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62  rms from the sub
34eb2 71 75 65 72 79 20 69 6e 74 6f 20 74 68 65 0a 20  query into the. 
34eb3 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72     ** outer quer
34eb4 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  y..    */.    fo
34eb5 72 28 69 3d 30 3b 20 69 3c 6e 53 75 62 53 72 63  r(i=0; i<nSubSrc
34eb6 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
34eb7 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
34eb8 65 28 64 62 2c 20 70 53 72 63 2d 3e 61 5b 69 2b  e(db, pSrc->a[i+
34eb9 69 46 72 6f 6d 5d 2e 70 55 73 69 6e 67 29 3b 0a  iFrom].pUsing);.
34eba 20 20 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 2b        pSrc->a[i+
34ebb 69 46 72 6f 6d 5d 20 3d 20 70 53 75 62 53 72 63  iFrom] = pSubSrc
34ebc 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 6d 65  ->a[i];.      me
34ebd 6d 73 65 74 28 26 70 53 75 62 53 72 63 2d 3e 61  mset(&pSubSrc->a
34ebe 5b 69 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70  [i], 0, sizeof(p
34ebf 53 75 62 53 72 63 2d 3e 61 5b 69 5d 29 29 3b 0a  SubSrc->a[i]));.
34ec0 20 20 20 20 7d 0a 20 20 20 20 70 53 72 63 2d 3e      }.    pSrc->
34ec1 61 5b 69 46 72 6f 6d 5d 2e 6a 6f 69 6e 74 79 70  a[iFrom].jointyp
34ec2 65 20 3d 20 6a 6f 69 6e 74 79 70 65 3b 0a 20 20  e = jointype;.  
34ec3 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 62 65 67 69  .    /* Now begi
34ec4 6e 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 73  n substituting s
34ec5 75 62 71 75 65 72 79 20 72 65 73 75 6c 74 20 73  ubquery result s
34ec6 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 66  et expressions f
34ec7 6f 72 20 0a 20 20 20 20 2a 2a 20 72 65 66 65 72  or .    ** refer
34ec8 65 6e 63 65 73 20 74 6f 20 74 68 65 20 69 50 61  ences to the iPa
34ec9 72 65 6e 74 20 69 6e 20 74 68 65 20 6f 75 74 65  rent in the oute
34eca 72 20 71 75 65 72 79 2e 0a 20 20 20 20 2a 2a 20  r query..    ** 
34ecb 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a  .    ** Example:
34ecc 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
34ecd 20 53 45 4c 45 43 54 20 61 2b 35 2c 20 62 2a 31   SELECT a+5, b*1
34ece 30 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 78  0 FROM (SELECT x
34ecf 2a 33 20 41 53 20 61 2c 20 79 2b 31 30 20 41 53  *3 AS a, y+10 AS
34ed0 20 62 20 46 52 4f 4d 20 74 31 29 20 57 48 45 52   b FROM t1) WHER
34ed1 45 20 61 3e 62 3b 0a 20 20 20 20 2a 2a 20 20 20  E a>b;.    **   
34ed2 5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \               
34ed3 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f        \_________
34ed4 5f 5f 5f 5f 20 73 75 62 71 75 65 72 79 20 5f 5f  ____ subquery __
34ed5 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20  ________/       
34ed6 20 20 20 2f 0a 20 20 20 20 2a 2a 20 20 20 20 5c     /.    **    \
34ed7 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
34ed8 5f 5f 5f 5f 5f 20 6f 75 74 65 72 20 71 75 65 72  _____ outer quer
34ed9 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  y ______________
34eda 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
34edb 2f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  /.    **.    ** 
34edc 57 65 20 6c 6f 6f 6b 20 61 74 20 65 76 65 72 79  We look at every
34edd 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
34ede 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 61  he outer query a
34edf 6e 64 20 65 76 65 72 79 20 70 6c 61 63 65 20 77  nd every place w
34ee0 65 20 73 65 65 0a 20 20 20 20 2a 2a 20 22 61 22  e see.    ** "a"
34ee1 20 77 65 20 73 75 62 73 74 69 74 75 74 65 20 22   we substitute "
34ee2 78 2a 33 22 20 61 6e 64 20 65 76 65 72 79 20 70  x*3" and every p
34ee3 6c 61 63 65 20 77 65 20 73 65 65 20 22 62 22 20  lace we see "b" 
34ee4 77 65 20 73 75 62 73 74 69 74 75 74 65 20 22 79  we substitute "y
34ee5 2b 31 30 22 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  +10"..    */.   
34ee6 20 70 4c 69 73 74 20 3d 20 70 50 61 72 65 6e 74   pList = pParent
34ee7 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 66 6f  ->pEList;.    fo
34ee8 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e  r(i=0; i<pList->
34ee9 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
34eea 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 61 5b     if( pList->a[
34eeb 69 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20  i].zName==0 ){. 
34eec 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61         char *zNa
34eed 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  me = sqlite3DbSt
34eee 72 44 75 70 28 64 62 2c 20 70 4c 69 73 74 2d 3e  rDup(db, pList->
34eef 61 5b 69 5d 2e 7a 53 70 61 6e 29 3b 0a 20 20 20  a[i].zSpan);.   
34ef0 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 71 75       sqlite3Dequ
34ef1 6f 74 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ote(zName);.    
34ef2 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e      pList->a[i].
34ef3 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20  zName = zName;. 
34ef4 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
34ef5 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64   substExprList(d
34ef6 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 45 4c 69  b, pParent->pELi
34ef7 73 74 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75  st, iParent, pSu
34ef8 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  b->pEList);.    
34ef9 69 66 28 20 69 73 41 67 67 20 29 7b 0a 20 20 20  if( isAgg ){.   
34efa 20 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74     substExprList
34efb 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 47  (db, pParent->pG
34efc 72 6f 75 70 42 79 2c 20 69 50 61 72 65 6e 74 2c  roupBy, iParent,
34efd 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a   pSub->pEList);.
34efe 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70        pParent->p
34eff 48 61 76 69 6e 67 20 3d 20 73 75 62 73 74 45 78  Having = substEx
34f00 70 72 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e  pr(db, pParent->
34f01 70 48 61 76 69 6e 67 2c 20 69 50 61 72 65 6e 74  pHaving, iParent
34f02 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b  , pSub->pEList);
34f03 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
34f04 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b  Sub->pOrderBy ){
34f05 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
34f06 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79  Parent->pOrderBy
34f07 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61  ==0 );.      pPa
34f08 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79 20 3d  rent->pOrderBy =
34f09 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 3b   pSub->pOrderBy;
34f0a 0a 20 20 20 20 20 20 70 53 75 62 2d 3e 70 4f 72  .      pSub->pOr
34f0b 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 7d  derBy = 0;.    }
34f0c 65 6c 73 65 20 69 66 28 20 70 50 61 72 65 6e 74  else if( pParent
34f0d 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
34f0e 20 20 20 20 73 75 62 73 74 45 78 70 72 4c 69 73      substExprLis
34f0f 74 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70  t(db, pParent->p
34f10 4f 72 64 65 72 42 79 2c 20 69 50 61 72 65 6e 74  OrderBy, iParent
34f11 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b  , pSub->pEList);
34f12 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
34f13 53 75 62 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20  Sub->pWhere ){. 
34f14 20 20 20 20 20 70 57 68 65 72 65 20 3d 20 73 71       pWhere = sq
34f15 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
34f16 20 70 53 75 62 2d 3e 70 57 68 65 72 65 2c 20 30   pSub->pWhere, 0
34f17 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
34f18 20 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b 0a      pWhere = 0;.
34f19 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 75      }.    if( su
34f1a 62 71 75 65 72 79 49 73 41 67 67 20 29 7b 0a 20  bqueryIsAgg ){. 
34f1b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
34f1c 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 3d 3d 30  rent->pHaving==0
34f1d 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e   );.      pParen
34f1e 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 70 50 61  t->pHaving = pPa
34f1f 72 65 6e 74 2d 3e 70 57 68 65 72 65 3b 0a 20 20  rent->pWhere;.  
34f20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 57 68      pParent->pWh
34f21 65 72 65 20 3d 20 70 57 68 65 72 65 3b 0a 20 20  ere = pWhere;.  
34f22 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48 61      pParent->pHa
34f23 76 69 6e 67 20 3d 20 73 75 62 73 74 45 78 70 72  ving = substExpr
34f24 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48  (db, pParent->pH
34f25 61 76 69 6e 67 2c 20 69 50 61 72 65 6e 74 2c 20  aving, iParent, 
34f26 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  pSub->pEList);. 
34f27 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48       pParent->pH
34f28 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 45  aving = sqlite3E
34f29 78 70 72 41 6e 64 28 64 62 2c 20 70 50 61 72 65  xprAnd(db, pPare
34f2a 6e 74 2d 3e 70 48 61 76 69 6e 67 2c 20 0a 20 20  nt->pHaving, .  
34f2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34f2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34f2d 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
34f2e 62 2c 20 70 53 75 62 2d 3e 70 48 61 76 69 6e 67  b, pSub->pHaving
34f2f 2c 20 30 29 29 3b 0a 20 20 20 20 20 20 61 73 73  , 0));.      ass
34f30 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 70 47  ert( pParent->pG
34f31 72 6f 75 70 42 79 3d 3d 30 20 29 3b 0a 20 20 20  roupBy==0 );.   
34f32 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 47 72 6f     pParent->pGro
34f33 75 70 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78  upBy = sqlite3Ex
34f34 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53  prListDup(db, pS
34f35 75 62 2d 3e 70 47 72 6f 75 70 42 79 2c 20 30 29  ub->pGroupBy, 0)
34f36 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
34f37 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65     pParent->pWhe
34f38 72 65 20 3d 20 73 75 62 73 74 45 78 70 72 28 64  re = substExpr(d
34f39 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65  b, pParent->pWhe
34f3a 72 65 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75  re, iParent, pSu
34f3b 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  b->pEList);.    
34f3c 20 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72    pParent->pWher
34f3d 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  e = sqlite3ExprA
34f3e 6e 64 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e  nd(db, pParent->
34f3f 70 57 68 65 72 65 2c 20 70 57 68 65 72 65 29 3b  pWhere, pWhere);
34f40 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
34f41 20 54 68 65 20 66 6c 61 74 74 65 6e 65 64 20 71   The flattened q
34f42 75 65 72 79 20 69 73 20 64 69 73 74 69 6e 63 74  uery is distinct
34f43 20 69 66 20 65 69 74 68 65 72 20 74 68 65 20 69   if either the i
34f44 6e 6e 65 72 20 6f 72 20 74 68 65 0a 20 20 20 20  nner or the.    
34f45 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 79 20 69  ** outer query i
34f46 73 20 64 69 73 74 69 6e 63 74 2e 20 0a 20 20 20  s distinct. .   
34f47 20 2a 2f 0a 20 20 20 20 70 50 61 72 65 6e 74 2d   */.    pParent-
34f48 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 70 53 75  >selFlags |= pSu
34f49 62 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  b->selFlags & SF
34f4a 5f 44 69 73 74 69 6e 63 74 3b 0a 20 20 0a 20 20  _Distinct;.  .  
34f4b 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 53 45 4c 45    /*.    ** SELE
34f4c 43 54 20 2e 2e 2e 20 46 52 4f 4d 20 28 53 45 4c  CT ... FROM (SEL
34f4d 45 43 54 20 2e 2e 2e 20 4c 49 4d 49 54 20 61 20  ECT ... LIMIT a 
34f4e 4f 46 46 53 45 54 20 62 29 20 4c 49 4d 49 54 20  OFFSET b) LIMIT 
34f4f 78 20 4f 46 46 53 45 54 20 79 3b 0a 20 20 20 20  x OFFSET y;.    
34f50 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e 65 20 69 73  **.    ** One is
34f51 20 74 65 6d 70 74 65 64 20 74 6f 20 74 72 79 20   tempted to try 
34f52 74 6f 20 61 64 64 20 61 20 61 6e 64 20 62 20 74  to add a and b t
34f53 6f 20 63 6f 6d 62 69 6e 65 20 74 68 65 20 6c 69  o combine the li
34f54 6d 69 74 73 2e 20 20 42 75 74 20 74 68 69 73 0a  mits.  But this.
34f55 20 20 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20      ** does not 
34f56 77 6f 72 6b 20 69 66 20 65 69 74 68 65 72 20 6c  work if either l
34f57 69 6d 69 74 20 69 73 20 6e 65 67 61 74 69 76 65  imit is negative
34f58 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
34f59 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 29 7b   pSub->pLimit ){
34f5a 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  .      pParent->
34f5b 70 4c 69 6d 69 74 20 3d 20 70 53 75 62 2d 3e 70  pLimit = pSub->p
34f5c 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 53 75  Limit;.      pSu
34f5d 62 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20  b->pLimit = 0;. 
34f5e 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46     }.  }..  /* F
34f5f 69 6e 69 61 6c 6c 79 2c 20 64 65 6c 65 74 65 20  inially, delete 
34f60 77 68 61 74 20 69 73 20 6c 65 66 74 20 6f 66 20  what is left of 
34f61 74 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64  the subquery and
34f62 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 73 75 63   return.  ** suc
34f63 63 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  cess..  */.  sql
34f64 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
34f65 28 64 62 2c 20 70 53 75 62 31 29 3b 0a 0a 20 20  (db, pSub1);..  
34f66 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64  return 1;.}.#end
34f67 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
34f68 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
34f69 45 52 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  ERY) || !defined
34f6a 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
34f6b 57 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61  W) */../*.** Ana
34f6c 6c 79 7a 65 20 74 68 65 20 53 45 4c 45 43 54 20  lyze the SELECT 
34f6d 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64  statement passed
34f6e 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   as an argument 
34f6f 74 6f 20 73 65 65 20 69 66 20 69 74 0a 2a 2a 20  to see if it.** 
34f70 69 73 20 61 20 6d 69 6e 28 29 20 6f 72 20 6d 61  is a min() or ma
34f71 78 28 29 20 71 75 65 72 79 2e 20 52 65 74 75 72  x() query. Retur
34f72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f  n WHERE_ORDERBY_
34f73 4d 49 4e 20 6f 72 20 57 48 45 52 45 5f 4f 52 44  MIN or WHERE_ORD
34f74 45 52 42 59 5f 4d 41 58 20 69 66 20 0a 2a 2a 20  ERBY_MAX if .** 
34f75 69 74 20 69 73 2c 20 6f 72 20 30 20 6f 74 68 65  it is, or 0 othe
34f76 72 77 69 73 65 2e 20 41 74 20 70 72 65 73 65 6e  rwise. At presen
34f77 74 2c 20 61 20 71 75 65 72 79 20 69 73 20 63 6f  t, a query is co
34f78 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 0a 2a  nsidered to be.*
34f79 2a 20 61 20 6d 69 6e 28 29 2f 6d 61 78 28 29 20  * a min()/max() 
34f7a 71 75 65 72 79 20 69 66 3a 0a 2a 2a 0a 2a 2a 20  query if:.**.** 
34f7b 20 20 31 2e 20 54 68 65 72 65 20 69 73 20 61 20    1. There is a 
34f7c 73 69 6e 67 6c 65 20 6f 62 6a 65 63 74 20 69 6e  single object in
34f7d 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
34f7e 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 54 68 65  ..**.**   2. The
34f7f 72 65 20 69 73 20 61 20 73 69 6e 67 6c 65 20 65  re is a single e
34f80 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65  xpression in the
34f81 20 72 65 73 75 6c 74 20 73 65 74 2c 20 61 6e 64   result set, and
34f82 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 65   it is.**      e
34f83 69 74 68 65 72 20 6d 69 6e 28 78 29 20 6f 72 20  ither min(x) or 
34f84 6d 61 78 28 78 29 2c 20 77 68 65 72 65 20 78 20  max(x), where x 
34f85 69 73 20 61 20 63 6f 6c 75 6d 6e 20 72 65 66 65  is a column refe
34f86 72 65 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  rence..*/.static
34f87 20 75 38 20 6d 69 6e 4d 61 78 51 75 65 72 79 28   u8 minMaxQuery(
34f88 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 45 78  Select *p){.  Ex
34f89 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 45 78 70  pr *pExpr;.  Exp
34f8a 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20  rList *pEList = 
34f8b 70 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 69 66  p->pEList;..  if
34f8c 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21  ( pEList->nExpr!
34f8d 3d 31 20 29 20 72 65 74 75 72 6e 20 57 48 45 52  =1 ) return WHER
34f8e 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c  E_ORDERBY_NORMAL
34f8f 3b 0a 20 20 70 45 78 70 72 20 3d 20 70 45 4c 69  ;.  pExpr = pELi
34f90 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a  st->a[0].pExpr;.
34f91 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21    if( pExpr->op!
34f92 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e  =TK_AGG_FUNCTION
34f93 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
34f94 66 28 20 4e 45 56 45 52 28 45 78 70 72 48 61 73  f( NEVER(ExprHas
34f95 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
34f96 45 50 5f 78 49 73 53 65 6c 65 63 74 29 29 20 29  EP_xIsSelect)) )
34f97 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 45 4c   return 0;.  pEL
34f98 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70  ist = pExpr->x.p
34f99 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45 4c 69  List;.  if( pELi
34f9a 73 74 3d 3d 30 20 7c 7c 20 70 45 4c 69 73 74 2d  st==0 || pEList-
34f9b 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74 75  >nExpr!=1 ) retu
34f9c 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 4c 69  rn 0;.  if( pELi
34f9d 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e  st->a[0].pExpr->
34f9e 6f 70 21 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  op!=TK_AGG_COLUM
34f9f 4e 20 29 20 72 65 74 75 72 6e 20 57 48 45 52 45  N ) return WHERE
34fa0 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b  _ORDERBY_NORMAL;
34fa1 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72  .  assert( !Expr
34fa2 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
34fa3 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20  r, EP_IntValue) 
34fa4 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
34fa5 53 74 72 49 43 6d 70 28 70 45 78 70 72 2d 3e 75  StrICmp(pExpr->u
34fa6 2e 7a 54 6f 6b 65 6e 2c 22 6d 69 6e 22 29 3d 3d  .zToken,"min")==
34fa7 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
34fa8 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49  WHERE_ORDERBY_MI
34fa9 4e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  N;.  }else if( s
34faa 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 45  qlite3StrICmp(pE
34fab 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 22 6d  xpr->u.zToken,"m
34fac 61 78 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  ax")==0 ){.    r
34fad 65 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45  eturn WHERE_ORDE
34fae 52 42 59 5f 4d 41 58 3b 0a 20 20 7d 0a 20 20 72  RBY_MAX;.  }.  r
34faf 65 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45  eturn WHERE_ORDE
34fb0 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a 7d 0a 0a 2f  RBY_NORMAL;.}../
34fb1 2a 0a 2a 2a 20 54 68 65 20 73 65 6c 65 63 74 20  *.** The select 
34fb2 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64  statement passed
34fb3 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
34fb4 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 61 67 67  gument is an agg
34fb5 72 65 67 61 74 65 20 71 75 65 72 79 2e 0a 2a 2a  regate query..**
34fb6 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 6d   The second argm
34fb7 65 6e 74 20 69 73 20 74 68 65 20 61 73 73 6f 63  ent is the assoc
34fb8 69 61 74 65 64 20 61 67 67 72 65 67 61 74 65 2d  iated aggregate-
34fb9 69 6e 66 6f 20 6f 62 6a 65 63 74 2e 20 54 68 69  info object. Thi
34fba 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74  s .** function t
34fbb 65 73 74 73 20 69 66 20 74 68 65 20 53 45 4c 45  ests if the SELE
34fbc 43 54 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  CT is of the for
34fbd 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43  m:.**.**   SELEC
34fbe 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20  T count(*) FROM 
34fbf 3c 74 62 6c 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72  <tbl>.**.** wher
34fc0 65 20 74 61 62 6c 65 20 69 73 20 61 20 64 61 74  e table is a dat
34fc1 61 62 61 73 65 20 74 61 62 6c 65 2c 20 6e 6f 74  abase table, not
34fc2 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20 6f 72   a sub-select or
34fc3 20 76 69 65 77 2e 20 49 66 20 74 68 65 20 71 75   view. If the qu
34fc4 65 72 79 0a 2a 2a 20 64 6f 65 73 20 6d 61 74 63  ery.** does matc
34fc5 68 20 74 68 69 73 20 70 61 74 74 65 72 6e 2c 20  h this pattern, 
34fc6 74 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  then a pointer t
34fc7 6f 20 74 68 65 20 54 61 62 6c 65 20 6f 62 6a 65  o the Table obje
34fc8 63 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 0a  ct representing.
34fc9 2a 2a 20 3c 74 62 6c 3e 20 69 73 20 72 65 74 75  ** <tbl> is retu
34fca 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rned. Otherwise,
34fcb 20 30 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a   0 is returned..
34fcc 2a 2f 0a 73 74 61 74 69 63 20 54 61 62 6c 65 20  */.static Table 
34fcd 2a 69 73 53 69 6d 70 6c 65 43 6f 75 6e 74 28 53  *isSimpleCount(S
34fce 65 6c 65 63 74 20 2a 70 2c 20 41 67 67 49 6e 66  elect *p, AggInf
34fcf 6f 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20  o *pAggInfo){.  
34fd0 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 45  Table *pTab;.  E
34fd1 78 70 72 20 2a 70 45 78 70 72 3b 0a 0a 20 20 61  xpr *pExpr;..  a
34fd2 73 73 65 72 74 28 20 21 70 2d 3e 70 47 72 6f 75  ssert( !p->pGrou
34fd3 70 42 79 20 29 3b 0a 0a 20 20 69 66 28 20 70 2d  pBy );..  if( p-
34fd4 3e 70 57 68 65 72 65 20 7c 7c 20 70 2d 3e 70 45  >pWhere || p->pE
34fd5 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 0a  List->nExpr!=1 .
34fd6 20 20 20 7c 7c 20 70 2d 3e 70 53 72 63 2d 3e 6e     || p->pSrc->n
34fd7 53 72 63 21 3d 31 20 7c 7c 20 70 2d 3e 70 53 72  Src!=1 || p->pSr
34fd8 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63 74 0a  c->a[0].pSelect.
34fd9 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20    ){.    return 
34fda 30 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20  0;.  }.  pTab = 
34fdb 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54  p->pSrc->a[0].pT
34fdc 61 62 3b 0a 20 20 70 45 78 70 72 20 3d 20 70 2d  ab;.  pExpr = p-
34fdd 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
34fde 78 70 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70  xpr;.  assert( p
34fdf 54 61 62 20 26 26 20 21 70 54 61 62 2d 3e 70 53  Tab && !pTab->pS
34fe0 65 6c 65 63 74 20 26 26 20 70 45 78 70 72 20 29  elect && pExpr )
34fe1 3b 0a 0a 20 20 69 66 28 20 49 73 56 69 72 74 75  ;..  if( IsVirtu
34fe2 61 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72  al(pTab) ) retur
34fe3 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 78 70 72  n 0;.  if( pExpr
34fe4 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 46 55 4e  ->op!=TK_AGG_FUN
34fe5 43 54 49 4f 4e 20 29 20 72 65 74 75 72 6e 20 30  CTION ) return 0
34fe6 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 41  ;.  if( NEVER(pA
34fe7 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3d 3d 30  ggInfo->nFunc==0
34fe8 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
34fe9 69 66 28 20 28 70 41 67 67 49 6e 66 6f 2d 3e 61  if( (pAggInfo->a
34fea 46 75 6e 63 5b 30 5d 2e 70 46 75 6e 63 2d 3e 66  Func[0].pFunc->f
34feb 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 55 4e 43  lags&SQLITE_FUNC
34fec 5f 43 4f 55 4e 54 29 3d 3d 30 20 29 20 72 65 74  _COUNT)==0 ) ret
34fed 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 78  urn 0;.  if( pEx
34fee 70 72 2d 3e 66 6c 61 67 73 26 45 50 5f 44 69 73  pr->flags&EP_Dis
34fef 74 69 6e 63 74 20 29 20 72 65 74 75 72 6e 20 30  tinct ) return 0
34ff0 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 54 61 62  ;..  return pTab
34ff1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ;.}../*.** If th
34ff2 65 20 73 6f 75 72 63 65 2d 6c 69 73 74 20 69 74  e source-list it
34ff3 65 6d 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  em passed as an 
34ff4 61 72 67 75 6d 65 6e 74 20 77 61 73 20 61 75 67  argument was aug
34ff5 6d 65 6e 74 65 64 20 77 69 74 68 20 61 6e 0a 2a  mented with an.*
34ff6 2a 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61  * INDEXED BY cla
34ff7 75 73 65 2c 20 74 68 65 6e 20 74 72 79 20 74 6f  use, then try to
34ff8 20 6c 6f 63 61 74 65 20 74 68 65 20 73 70 65 63   locate the spec
34ff9 69 66 69 65 64 20 69 6e 64 65 78 2e 20 49 66 20  ified index. If 
34ffa 74 68 65 72 65 0a 2a 2a 20 77 61 73 20 73 75 63  there.** was suc
34ffb 68 20 61 20 63 6c 61 75 73 65 20 61 6e 64 20 74  h a clause and t
34ffc 68 65 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 63  he named index c
34ffd 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20  annot be found, 
34ffe 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54  return .** SQLIT
34fff 45 5f 45 52 52 4f 52 20 61 6e 64 20 6c 65 61 76  E_ERROR and leav
35000 65 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 70 50  e an error in pP
35001 61 72 73 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  arse. Otherwise,
35002 20 70 6f 70 75 6c 61 74 65 20 0a 2a 2a 20 70 46   populate .** pF
35003 72 6f 6d 2d 3e 70 49 6e 64 65 78 20 61 6e 64 20  rom->pIndex and 
35004 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
35005 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
35006 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
35007 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28 50  ndexedByLookup(P
35008 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 73 74  arse *pParse, st
35009 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
3500a 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 66 28  m *pFrom){.  if(
3500b 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20 26 26 20   pFrom->pTab && 
3500c 70 46 72 6f 6d 2d 3e 7a 49 6e 64 65 78 20 29 7b  pFrom->zIndex ){
3500d 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
3500e 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3b 0a   = pFrom->pTab;.
3500f 20 20 20 20 63 68 61 72 20 2a 7a 49 6e 64 65 78      char *zIndex
35010 20 3d 20 70 46 72 6f 6d 2d 3e 7a 49 6e 64 65 78   = pFrom->zIndex
35011 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  ;.    Index *pId
35012 78 3b 0a 20 20 20 20 66 6f 72 28 70 49 64 78 3d  x;.    for(pIdx=
35013 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 0a 20  pTab->pIndex; . 
35014 20 20 20 20 20 20 20 70 49 64 78 20 26 26 20 73         pIdx && s
35015 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 49  qlite3StrICmp(pI
35016 64 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 49 6e 64 65  dx->zName, zInde
35017 78 29 3b 20 0a 20 20 20 20 20 20 20 20 70 49 64  x); .        pId
35018 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 0a 20 20  x=pIdx->pNext.  
35019 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 70 49    );.    if( !pI
3501a 64 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  dx ){.      sqli
3501b 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
3501c 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69 6e 64  se, "no such ind
3501d 65 78 3a 20 25 73 22 2c 20 7a 49 6e 64 65 78 2c  ex: %s", zIndex,
3501e 20 30 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73   0);.      pPars
3501f 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d  e->checkSchema =
35020 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   1;.      return
35021 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
35022 20 20 20 7d 0a 20 20 20 20 70 46 72 6f 6d 2d 3e     }.    pFrom->
35023 70 49 6e 64 65 78 20 3d 20 70 49 64 78 3b 0a 20  pIndex = pIdx;. 
35024 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
35025 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
35026 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
35027 61 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 61 63  a Walker callbac
35028 6b 20 66 6f 72 20 22 65 78 70 61 6e 64 69 6e 67  k for "expanding
35029 22 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  " a SELECT state
3502a 6d 65 6e 74 2e 0a 2a 2a 20 22 45 78 70 61 6e 64  ment..** "Expand
3502b 69 6e 67 22 20 6d 65 61 6e 73 20 74 6f 20 64 6f  ing" means to do
3502c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
3502d 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 4d 61  **.**    (1)  Ma
3502e 6b 65 20 73 75 72 65 20 56 44 42 45 20 63 75 72  ke sure VDBE cur
3502f 73 6f 72 20 6e 75 6d 62 65 72 73 20 68 61 76 65  sor numbers have
35030 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64 20 74   been assigned t
35031 6f 20 65 76 65 72 79 0a 2a 2a 20 20 20 20 20 20  o every.**      
35032 20 20 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68     element of th
35033 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a  e FROM clause..*
35034 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 46 69 6c  *.**    (2)  Fil
35035 6c 20 69 6e 20 74 68 65 20 70 54 61 62 4c 69 73  l in the pTabLis
35036 74 2d 3e 61 5b 5d 2e 70 54 61 62 20 66 69 65 6c  t->a[].pTab fiel
35037 64 73 20 69 6e 20 74 68 65 20 53 72 63 4c 69 73  ds in the SrcLis
35038 74 20 74 68 61 74 20 0a 2a 2a 20 20 20 20 20 20  t that .**      
35039 20 20 20 64 65 66 69 6e 65 73 20 46 52 4f 4d 20     defines FROM 
3503a 63 6c 61 75 73 65 2e 20 20 57 68 65 6e 20 76 69  clause.  When vi
3503b 65 77 73 20 61 70 70 65 61 72 20 69 6e 20 74 68  ews appear in th
3503c 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2c 0a 2a  e FROM clause,.*
3503d 2a 20 20 20 20 20 20 20 20 20 66 69 6c 6c 20 70  *         fill p
3503e 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e 70 53 65  TabList->a[].pSe
3503f 6c 65 63 74 20 77 69 74 68 20 61 20 63 6f 70 79  lect with a copy
35040 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73   of the SELECT s
35041 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 20  tatement.**     
35042 20 20 20 20 74 68 61 74 20 69 6d 70 6c 65 6d 65      that impleme
35043 6e 74 73 20 74 68 65 20 76 69 65 77 2e 20 20 41  nts the view.  A
35044 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20 6f 66   copy is made of
35045 20 74 68 65 20 76 69 65 77 27 73 20 53 45 4c 45   the view's SELE
35046 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 74  CT.**         st
35047 61 74 65 6d 65 6e 74 20 73 6f 20 74 68 61 74 20  atement so that 
35048 77 65 20 63 61 6e 20 66 72 65 65 6c 79 20 6d 6f  we can freely mo
35049 64 69 66 79 20 6f 72 20 64 65 6c 65 74 65 20 74  dify or delete t
3504a 68 61 74 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  hat statement.**
3504b 20 20 20 20 20 20 20 20 20 77 69 74 68 6f 75 74           without
3504c 20 77 6f 72 72 79 69 6e 67 20 61 62 6f 75 74 20   worrying about 
3504d 6d 65 73 73 69 6e 67 20 75 70 20 74 68 65 20 70  messing up the p
3504e 72 65 73 69 73 74 65 6e 74 20 72 65 70 72 65 73  resistent repres
3504f 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 20  entation.**     
35050 20 20 20 20 6f 66 20 74 68 65 20 76 69 65 77 2e      of the view.
35051 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29 20 20 41  .**.**    (3)  A
35052 64 64 20 74 65 72 6d 73 20 74 6f 20 74 68 65 20  dd terms to the 
35053 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20  WHERE clause to 
35054 61 63 63 6f 6d 6f 64 61 74 65 20 74 68 65 20 4e  accomodate the N
35055 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64 0a 2a  ATURAL keyword.*
35056 2a 20 20 20 20 20 20 20 20 20 6f 6e 20 6a 6f 69  *         on joi
35057 6e 73 20 61 6e 64 20 74 68 65 20 4f 4e 20 61 6e  ns and the ON an
35058 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f  d USING clause o
35059 66 20 6a 6f 69 6e 73 2e 0a 2a 2a 0a 2a 2a 20 20  f joins..**.**  
3505a 20 20 28 34 29 20 20 53 63 61 6e 20 74 68 65 20    (4)  Scan the 
3505b 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  list of columns 
3505c 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
3505d 74 20 28 70 45 4c 69 73 74 29 20 6c 6f 6f 6b 69  t (pEList) looki
3505e 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 6f  ng.**         fo
3505f 72 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74  r instances of t
35060 68 65 20 22 2a 22 20 6f 70 65 72 61 74 6f 72 20  he "*" operator 
35061 6f 72 20 74 68 65 20 54 41 42 4c 45 2e 2a 20 6f  or the TABLE.* o
35062 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 20 20 20 20  perator..**     
35063 20 20 20 20 49 66 20 66 6f 75 6e 64 2c 20 65 78      If found, ex
35064 70 61 6e 64 20 65 61 63 68 20 22 2a 22 20 74 6f  pand each "*" to
35065 20 62 65 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e   be every column
35066 20 69 6e 20 65 76 65 72 79 20 74 61 62 6c 65 0a   in every table.
35067 2a 2a 20 20 20 20 20 20 20 20 20 61 6e 64 20 54  **         and T
35068 41 42 4c 45 2e 2a 20 74 6f 20 62 65 20 65 76 65  ABLE.* to be eve
35069 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 54 41 42  ry column in TAB
3506a 4c 45 2e 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63  LE..**.*/.static
3506b 20 69 6e 74 20 73 65 6c 65 63 74 45 78 70 61 6e   int selectExpan
3506c 64 65 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c  der(Walker *pWal
3506d 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b  ker, Select *p){
3506e 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
3506f 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72   = pWalker->pPar
35070 73 65 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20  se;.  int i, j, 
35071 6b 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  k;.  SrcList *pT
35072 61 62 4c 69 73 74 3b 0a 20 20 45 78 70 72 4c 69  abList;.  ExprLi
35073 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74  st *pEList;.  st
35074 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
35075 6d 20 2a 70 46 72 6f 6d 3b 0a 20 20 73 71 6c 69  m *pFrom;.  sqli
35076 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
35077 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 64 62 2d  ->db;..  if( db-
35078 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 20 29  >mallocFailed  )
35079 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43  {.    return WRC
3507a 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 20 20 69 66  _Abort;.  }.  if
3507b 28 20 4e 45 56 45 52 28 70 2d 3e 70 53 72 63 3d  ( NEVER(p->pSrc=
3507c 3d 30 29 20 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c  =0) || (p->selFl
3507d 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64 65  ags & SF_Expande
3507e 64 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  d)!=0 ){.    ret
3507f 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
35080 20 7d 0a 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73   }.  p->selFlags
35081 20 7c 3d 20 53 46 5f 45 78 70 61 6e 64 65 64 3b   |= SF_Expanded;
35082 0a 20 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d  .  pTabList = p-
35083 3e 70 53 72 63 3b 0a 20 20 70 45 4c 69 73 74 20  >pSrc;.  pEList 
35084 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20 20  = p->pEList;..  
35085 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 63 75 72  /* Make sure cur
35086 73 6f 72 20 6e 75 6d 62 65 72 73 20 68 61 76 65  sor numbers have
35087 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64 20 74   been assigned t
35088 6f 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e  o all entries in
35089 0a 20 20 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63  .  ** the FROM c
3508a 6c 61 75 73 65 20 6f 66 20 74 68 65 20 53 45 4c  lause of the SEL
3508b 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20  ECT statement.. 
3508c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 72 63   */.  sqlite3Src
3508d 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72  ListAssignCursor
3508e 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  s(pParse, pTabLi
3508f 73 74 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20  st);..  /* Look 
35090 75 70 20 65 76 65 72 79 20 74 61 62 6c 65 20 6e  up every table n
35091 61 6d 65 64 20 69 6e 20 74 68 65 20 46 52 4f 4d  amed in the FROM
35092 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 73   clause of the s
35093 65 6c 65 63 74 2e 20 20 49 66 0a 20 20 2a 2a 20  elect.  If.  ** 
35094 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20  an entry of the 
35095 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61  FROM clause is a
35096 20 73 75 62 71 75 65 72 79 20 69 6e 73 74 65 61   subquery instea
35097 64 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 20  d of a table or 
35098 76 69 65 77 2c 0a 20 20 2a 2a 20 74 68 65 6e 20  view,.  ** then 
35099 63 72 65 61 74 65 20 61 20 74 72 61 6e 73 69 65  create a transie
3509a 6e 74 20 74 61 62 6c 65 20 73 74 72 75 63 74 75  nt table structu
3509b 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20 74  re to describe t
3509c 68 65 20 73 75 62 71 75 65 72 79 2e 0a 20 20 2a  he subquery..  *
3509d 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 72  /.  for(i=0, pFr
3509e 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20  om=pTabList->a; 
3509f 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
350a0 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b  ; i++, pFrom++){
350a1 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
350a2 3b 0a 20 20 20 20 69 66 28 20 70 46 72 6f 6d 2d  ;.    if( pFrom-
350a3 3e 70 54 61 62 21 3d 30 20 29 7b 0a 20 20 20 20  >pTab!=0 ){.    
350a4 20 20 2f 2a 20 54 68 69 73 20 73 74 61 74 65 6d    /* This statem
350a5 65 6e 74 20 68 61 73 20 61 6c 72 65 61 64 79 20  ent has already 
350a6 62 65 65 6e 20 70 72 65 70 61 72 65 64 2e 20 20  been prepared.  
350a7 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  There is no need
350a8 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 67 6f 20  .      ** to go 
350a9 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20 20 20  further. */.    
350aa 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20 29    assert( i==0 )
350ab 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57  ;.      return W
350ac 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a  RC_Prune;.    }.
350ad 20 20 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 7a      if( pFrom->z
350ae 4e 61 6d 65 3d 3d 30 20 29 7b 0a 23 69 66 6e 64  Name==0 ){.#ifnd
350af 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
350b0 55 42 51 55 45 52 59 0a 20 20 20 20 20 20 53 65  UBQUERY.      Se
350b1 6c 65 63 74 20 2a 70 53 65 6c 20 3d 20 70 46 72  lect *pSel = pFr
350b2 6f 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20  om->pSelect;.   
350b3 20 20 20 2f 2a 20 41 20 73 75 62 2d 71 75 65 72     /* A sub-quer
350b4 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  y in the FROM cl
350b5 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54  ause of a SELECT
350b6 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
350b7 28 20 70 53 65 6c 21 3d 30 20 29 3b 0a 20 20 20  ( pSel!=0 );.   
350b8 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d     assert( pFrom
350b9 2d 3e 70 54 61 62 3d 3d 30 20 29 3b 0a 20 20 20  ->pTab==0 );.   
350ba 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65     sqlite3WalkSe
350bb 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 53  lect(pWalker, pS
350bc 65 6c 29 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d  el);.      pFrom
350bd 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 20 3d 20  ->pTab = pTab = 
350be 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
350bf 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54  ero(db, sizeof(T
350c0 61 62 6c 65 29 29 3b 0a 20 20 20 20 20 20 69 66  able));.      if
350c1 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65 74 75  ( pTab==0 ) retu
350c2 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
350c3 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d      pTab->nRef =
350c4 20 31 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e   1;.      pTab->
350c5 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d  zName = sqlite3M
350c6 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69  Printf(db, "sqli
350c7 74 65 5f 73 75 62 71 75 65 72 79 5f 25 70 5f 22  te_subquery_%p_"
350c8 2c 20 28 76 6f 69 64 2a 29 70 54 61 62 29 3b 0a  , (void*)pTab);.
350c9 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 65        while( pSe
350ca 6c 2d 3e 70 50 72 69 6f 72 20 29 7b 20 70 53 65  l->pPrior ){ pSe
350cb 6c 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72  l = pSel->pPrior
350cc 3b 20 7d 0a 20 20 20 20 20 20 73 65 6c 65 63 74  ; }.      select
350cd 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c  ColumnsFromExprL
350ce 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ist(pParse, pSel
350cf 2d 3e 70 45 4c 69 73 74 2c 20 26 70 54 61 62 2d  ->pEList, &pTab-
350d0 3e 6e 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61 43  >nCol, &pTab->aC
350d1 6f 6c 29 3b 0a 20 20 20 20 20 20 70 54 61 62 2d  ol);.      pTab-
350d2 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 20  >iPKey = -1;.   
350d3 20 20 20 70 54 61 62 2d 3e 6e 52 6f 77 45 73 74     pTab->nRowEst
350d4 20 3d 20 31 30 30 30 30 30 30 3b 0a 20 20 20 20   = 1000000;.    
350d5 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73    pTab->tabFlags
350d6 20 7c 3d 20 54 46 5f 45 70 68 65 6d 65 72 61 6c   |= TF_Ephemeral
350d7 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c  ;.#endif.    }el
350d8 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20  se{.      /* An 
350d9 6f 72 64 69 6e 61 72 79 20 74 61 62 6c 65 20 6f  ordinary table o
350da 72 20 76 69 65 77 20 6e 61 6d 65 20 69 6e 20 74  r view name in t
350db 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
350dc 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
350dd 70 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20 29  pFrom->pTab==0 )
350de 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70  ;.      pFrom->p
350df 54 61 62 20 3d 20 70 54 61 62 20 3d 20 73 71 6c  Tab = pTab = sql
350e0 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 49  ite3LocateTableI
350e1 74 65 6d 28 70 50 61 72 73 65 2c 20 30 2c 20 70  tem(pParse, 0, p
350e2 46 72 6f 6d 29 3b 0a 20 20 20 20 20 20 69 66 28  From);.      if(
350e3 20 70 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72   pTab==0 ) retur
350e4 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20  n WRC_Abort;.   
350e5 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b     pTab->nRef++;
350e6 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
350e7 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
350e8 7c 7c 20 21 64 65 66 69 6e 65 64 20 28 53 51 4c  || !defined (SQL
350e9 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
350ea 54 41 42 4c 45 29 0a 20 20 20 20 20 20 69 66 28  TABLE).      if(
350eb 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 7c   pTab->pSelect |
350ec 7c 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  | IsVirtual(pTab
350ed 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
350ee 57 65 20 72 65 61 63 68 20 68 65 72 65 20 69 66  We reach here if
350ef 20 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65   the named table
350f0 20 69 73 20 61 20 72 65 61 6c 6c 79 20 61 20 76   is a really a v
350f1 69 65 77 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  iew */.        i
350f2 66 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65  f( sqlite3ViewGe
350f3 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  tColumnNames(pPa
350f4 72 73 65 2c 20 70 54 61 62 29 20 29 20 72 65 74  rse, pTab) ) ret
350f5 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
350f6 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
350f7 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 3d 3d 30  From->pSelect==0
350f8 20 29 3b 0a 20 20 20 20 20 20 20 20 70 46 72 6f   );.        pFro
350f9 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c  m->pSelect = sql
350fa 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62  ite3SelectDup(db
350fb 2c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 2c  , pTab->pSelect,
350fc 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
350fd 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70  ite3WalkSelect(p
350fe 57 61 6c 6b 65 72 2c 20 70 46 72 6f 6d 2d 3e 70  Walker, pFrom->p
350ff 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 7d  Select);.      }
35100 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 0a 20  .#endif.    }.. 
35101 20 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65     /* Locate the
35102 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 62 79 20   index named by 
35103 74 68 65 20 49 4e 44 45 58 45 44 20 42 59 20 63  the INDEXED BY c
35104 6c 61 75 73 65 2c 20 69 66 20 61 6e 79 2e 20 2a  lause, if any. *
35105 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  /.    if( sqlite
35106 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70  3IndexedByLookup
35107 28 70 50 61 72 73 65 2c 20 70 46 72 6f 6d 29 20  (pParse, pFrom) 
35108 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
35109 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d  WRC_Abort;.    }
3510a 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 72 6f 63 65  .  }..  /* Proce
3510b 73 73 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f  ss NATURAL keywo
3510c 72 64 73 2c 20 61 6e 64 20 4f 4e 20 61 6e 64 20  rds, and ON and 
3510d 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66  USING clauses of
3510e 20 6a 6f 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69   joins..  */.  i
3510f 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
35110 6c 65 64 20 7c 7c 20 73 71 6c 69 74 65 50 72 6f  led || sqlitePro
35111 63 65 73 73 4a 6f 69 6e 28 70 50 61 72 73 65 2c  cessJoin(pParse,
35112 20 70 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   p) ){.    retur
35113 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d  n WRC_Abort;.  }
35114 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 76 65 72 79  ..  /* For every
35115 20 22 2a 22 20 74 68 61 74 20 6f 63 63 75 72 73   "*" that occurs
35116 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6c   in the column l
35117 69 73 74 2c 20 69 6e 73 65 72 74 20 74 68 65 20  ist, insert the 
35118 6e 61 6d 65 73 20 6f 66 0a 20 20 2a 2a 20 61 6c  names of.  ** al
35119 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6c 6c  l columns in all
3511a 20 74 61 62 6c 65 73 2e 20 20 41 6e 64 20 66 6f   tables.  And fo
3511b 72 20 65 76 65 72 79 20 54 41 42 4c 45 2e 2a 20  r every TABLE.* 
3511c 69 6e 73 65 72 74 20 74 68 65 20 6e 61 6d 65 73  insert the names
3511d 0a 20 20 2a 2a 20 6f 66 20 61 6c 6c 20 63 6f 6c  .  ** of all col
3511e 75 6d 6e 73 20 69 6e 20 54 41 42 4c 45 2e 20 20  umns in TABLE.  
3511f 54 68 65 20 70 61 72 73 65 72 20 69 6e 73 65 72  The parser inser
35120 74 65 64 20 61 20 73 70 65 63 69 61 6c 20 65 78  ted a special ex
35121 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 77 69  pression.  ** wi
35122 74 68 20 74 68 65 20 54 4b 5f 41 4c 4c 20 6f 70  th the TK_ALL op
35123 65 72 61 74 6f 72 20 66 6f 72 20 65 61 63 68 20  erator for each 
35124 22 2a 22 20 74 68 61 74 20 69 74 20 66 6f 75 6e  "*" that it foun
35125 64 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20  d in the column 
35126 6c 69 73 74 2e 0a 20 20 2a 2a 20 54 68 65 20 66  list..  ** The f
35127 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 6a 75  ollowing code ju
35128 73 74 20 68 61 73 20 74 6f 20 6c 6f 63 61 74 65  st has to locate
35129 20 74 68 65 20 54 4b 5f 41 4c 4c 20 65 78 70 72   the TK_ALL expr
3512a 65 73 73 69 6f 6e 73 20 61 6e 64 20 65 78 70 61  essions and expa
3512b 6e 64 0a 20 20 2a 2a 20 65 61 63 68 20 6f 6e 65  nd.  ** each one
3512c 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20   to the list of 
3512d 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61  all columns in a
3512e 6c 6c 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2a 0a  ll tables..  **.
3512f 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 6c    ** The first l
35130 6f 6f 70 20 6a 75 73 74 20 63 68 65 63 6b 73 20  oop just checks 
35131 74 6f 20 73 65 65 20 69 66 20 74 68 65 72 65 20  to see if there 
35132 61 72 65 20 61 6e 79 20 22 2a 22 20 6f 70 65 72  are any "*" oper
35133 61 74 6f 72 73 0a 20 20 2a 2a 20 74 68 61 74 20  ators.  ** that 
35134 6e 65 65 64 20 65 78 70 61 6e 64 69 6e 67 2e 0a  need expanding..
35135 20 20 2a 2f 0a 20 20 66 6f 72 28 6b 3d 30 3b 20    */.  for(k=0; 
35136 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b  k<pEList->nExpr;
35137 20 6b 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20   k++){.    Expr 
35138 2a 70 45 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b  *pE = pEList->a[
35139 6b 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 66  k].pExpr;.    if
3513a 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c  ( pE->op==TK_ALL
3513b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 61 73   ) break;.    as
3513c 73 65 72 74 28 20 70 45 2d 3e 6f 70 21 3d 54 4b  sert( pE->op!=TK
3513d 5f 44 4f 54 20 7c 7c 20 70 45 2d 3e 70 52 69 67  _DOT || pE->pRig
3513e 68 74 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  ht!=0 );.    ass
3513f 65 72 74 28 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f  ert( pE->op!=TK_
35140 44 4f 54 20 7c 7c 20 28 70 45 2d 3e 70 4c 65 66  DOT || (pE->pLef
35141 74 21 3d 30 20 26 26 20 70 45 2d 3e 70 4c 65 66  t!=0 && pE->pLef
35142 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 29 20 29 3b  t->op==TK_ID) );
35143 0a 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d  .    if( pE->op=
35144 3d 54 4b 5f 44 4f 54 20 26 26 20 70 45 2d 3e 70  =TK_DOT && pE->p
35145 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c  Right->op==TK_AL
35146 4c 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20  L ) break;.  }. 
35147 20 69 66 28 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e   if( k<pEList->n
35148 45 78 70 72 20 29 7b 0a 20 20 20 20 2f 2a 0a 20  Expr ){.    /*. 
35149 20 20 20 2a 2a 20 49 66 20 77 65 20 67 65 74 20     ** If we get 
3514a 68 65 72 65 20 69 74 20 6d 65 61 6e 73 20 74 68  here it means th
3514b 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6e  e result set con
3514c 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72  tains one or mor
3514d 65 20 22 2a 22 0a 20 20 20 20 2a 2a 20 6f 70 65  e "*".    ** ope
3514e 72 61 74 6f 72 73 20 74 68 61 74 20 6e 65 65 64  rators that need
3514f 20 74 6f 20 62 65 20 65 78 70 61 6e 64 65 64 2e   to be expanded.
35150 20 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 65    Loop through e
35151 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 0a 20  ach expression. 
35152 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 65 73     ** in the res
35153 75 6c 74 20 73 65 74 20 61 6e 64 20 65 78 70 61  ult set and expa
35154 6e 64 20 74 68 65 6d 20 6f 6e 65 20 62 79 20 6f  nd them one by o
35155 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ne..    */.    s
35156 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
35157 74 65 6d 20 2a 61 20 3d 20 70 45 4c 69 73 74 2d  tem *a = pEList-
35158 3e 61 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74  >a;.    ExprList
35159 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20   *pNew = 0;.    
3515a 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 50 61 72  int flags = pPar
3515b 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 3b 0a 20  se->db->flags;. 
3515c 20 20 20 69 6e 74 20 6c 6f 6e 67 4e 61 6d 65 73     int longNames
3515d 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49   = (flags & SQLI
3515e 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29  TE_FullColNames)
3515f 21 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 20  !=0.            
35160 20 20 20 20 20 20 20 20 20 20 26 26 20 28 66 6c            && (fl
35161 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 68 6f  ags & SQLITE_Sho
35162 72 74 43 6f 6c 4e 61 6d 65 73 29 3d 3d 30 3b 0a  rtColNames)==0;.
35163 0a 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c  .    for(k=0; k<
35164 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6b  pEList->nExpr; k
35165 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  ++){.      Expr 
35166 2a 70 45 20 3d 20 61 5b 6b 5d 2e 70 45 78 70 72  *pE = a[k].pExpr
35167 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
35168 70 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c  pE->op!=TK_DOT |
35169 7c 20 70 45 2d 3e 70 52 69 67 68 74 21 3d 30 20  | pE->pRight!=0 
3516a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 2d  );.      if( pE-
3516b 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 20 26 26 20 28  >op!=TK_ALL && (
3516c 70 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c  pE->op!=TK_DOT |
3516d 7c 20 70 45 2d 3e 70 52 69 67 68 74 2d 3e 6f 70  | pE->pRight->op
3516e 21 3d 54 4b 5f 41 4c 4c 29 20 29 7b 0a 20 20 20  !=TK_ALL) ){.   
3516f 20 20 20 20 20 2f 2a 20 54 68 69 73 20 70 61 72       /* This par
35170 74 69 63 75 6c 61 72 20 65 78 70 72 65 73 73 69  ticular expressi
35171 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  on does not need
35172 20 74 6f 20 62 65 20 65 78 70 61 6e 64 65 64 2e   to be expanded.
35173 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
35174 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
35175 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
35176 28 70 50 61 72 73 65 2c 20 70 4e 65 77 2c 20 61  (pParse, pNew, a
35177 5b 6b 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20  [k].pExpr);.    
35178 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a      if( pNew ){.
35179 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e            pNew->
3517a 61 5b 70 4e 65 77 2d 3e 6e 45 78 70 72 2d 31 5d  a[pNew->nExpr-1]
3517b 2e 7a 4e 61 6d 65 20 3d 20 61 5b 6b 5d 2e 7a 4e  .zName = a[k].zN
3517c 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70  ame;.          p
3517d 4e 65 77 2d 3e 61 5b 70 4e 65 77 2d 3e 6e 45 78  New->a[pNew->nEx
3517e 70 72 2d 31 5d 2e 7a 53 70 61 6e 20 3d 20 61 5b  pr-1].zSpan = a[
3517f 6b 5d 2e 7a 53 70 61 6e 3b 0a 20 20 20 20 20 20  k].zSpan;.      
35180 20 20 20 20 61 5b 6b 5d 2e 7a 4e 61 6d 65 20 3d      a[k].zName =
35181 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 5b   0;.          a[
35182 6b 5d 2e 7a 53 70 61 6e 20 3d 20 30 3b 0a 20 20  k].zSpan = 0;.  
35183 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
35184 61 5b 6b 5d 2e 70 45 78 70 72 20 3d 20 30 3b 0a  a[k].pExpr = 0;.
35185 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
35186 20 20 20 20 20 2f 2a 20 54 68 69 73 20 65 78 70       /* This exp
35187 72 65 73 73 69 6f 6e 20 69 73 20 61 20 22 2a 22  ression is a "*"
35188 20 6f 72 20 61 20 22 54 41 42 4c 45 2e 2a 22 20   or a "TABLE.*" 
35189 61 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65 0a  and needs to be.
3518a 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 61 6e          ** expan
3518b 64 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ded. */.        
3518c 69 6e 74 20 74 61 62 6c 65 53 65 65 6e 20 3d 20  int tableSeen = 
3518d 30 3b 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74  0;      /* Set t
3518e 6f 20 31 20 77 68 65 6e 20 54 41 42 4c 45 20 6d  o 1 when TABLE m
3518f 61 74 63 68 65 73 20 2a 2f 0a 20 20 20 20 20 20  atches */.      
35190 20 20 63 68 61 72 20 2a 7a 54 4e 61 6d 65 3b 20    char *zTName; 
35191 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 65             /* te
35192 78 74 20 6f 66 20 6e 61 6d 65 20 6f 66 20 54 41  xt of name of TA
35193 42 4c 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  BLE */.        i
35194 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f  f( pE->op==TK_DO
35195 54 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  T ){.          a
35196 73 73 65 72 74 28 20 70 45 2d 3e 70 4c 65 66 74  ssert( pE->pLeft
35197 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  !=0 );.         
35198 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
35199 73 50 72 6f 70 65 72 74 79 28 70 45 2d 3e 70 4c  sProperty(pE->pL
3519a 65 66 74 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  eft, EP_IntValue
3519b 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7a  ) );.          z
3519c 54 4e 61 6d 65 20 3d 20 70 45 2d 3e 70 4c 65 66  TName = pE->pLef
3519d 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20  t->u.zToken;.   
3519e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
3519f 20 20 20 20 20 20 7a 54 4e 61 6d 65 20 3d 20 30        zTName = 0
351a0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
351a1 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 72      for(i=0, pFr
351a2 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20  om=pTabList->a; 
351a3 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
351a4 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b  ; i++, pFrom++){
351a5 0a 20 20 20 20 20 20 20 20 20 20 54 61 62 6c 65  .          Table
351a6 20 2a 70 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e   *pTab = pFrom->
351a7 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20  pTab;.          
351a8 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d  char *zTabName =
351a9 20 70 46 72 6f 6d 2d 3e 7a 41 6c 69 61 73 3b 0a   pFrom->zAlias;.
351aa 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54            if( zT
351ab 61 62 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20  abName==0 ){.   
351ac 20 20 20 20 20 20 20 20 20 7a 54 61 62 4e 61 6d           zTabNam
351ad 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b  e = pTab->zName;
351ae 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
351af 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d         if( db->m
351b0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 62 72  allocFailed ) br
351b1 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 69  eak;.          i
351b2 66 28 20 7a 54 4e 61 6d 65 20 26 26 20 73 71 6c  f( zTName && sql
351b3 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 4e 61  ite3StrICmp(zTNa
351b4 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 29 21 3d 30  me, zTabName)!=0
351b5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
351b6 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
351b7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
351b8 74 61 62 6c 65 53 65 65 6e 20 3d 20 31 3b 0a 20  tableSeen = 1;. 
351b9 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30           for(j=0
351ba 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; j<pTab->nCol; 
351bb 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
351bc 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 2a    Expr *pExpr, *
351bd 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20  pRight;.        
351be 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20      char *zName 
351bf 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e  = pTab->aCol[j].
351c0 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20  zName;.         
351c1 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6e 61 6d     char *zColnam
351c2 65 3b 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 75  e;  /* The compu
351c3 74 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20  ted column name 
351c4 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  */.            c
351c5 68 61 72 20 2a 7a 54 6f 46 72 65 65 3b 20 20 20  har *zToFree;   
351c6 2f 2a 20 4d 61 6c 6c 6f 63 65 64 20 73 74 72 69  /* Malloced stri
351c7 6e 67 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f  ng that needs to
351c8 20 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20   be freed */.   
351c9 20 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 20 73           Token s
351ca 43 6f 6c 6e 61 6d 65 3b 20 20 2f 2a 20 43 6f 6d  Colname;  /* Com
351cb 70 75 74 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d  puted column nam
351cc 65 20 61 73 20 61 20 74 6f 6b 65 6e 20 2a 2f 0a  e as a token */.
351cd 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
351ce 49 66 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 6d  If a column is m
351cf 61 72 6b 65 64 20 61 73 20 27 68 69 64 64 65 6e  arked as 'hidden
351d0 27 20 28 63 75 72 72 65 6e 74 6c 79 20 6f 6e 6c  ' (currently onl
351d1 79 20 70 6f 73 73 69 62 6c 65 0a 20 20 20 20 20  y possible.     
351d2 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 76 69         ** for vi
351d3 72 74 75 61 6c 20 74 61 62 6c 65 73 29 2c 20 64  rtual tables), d
351d4 6f 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 69 74  o not include it
351d5 20 69 6e 20 74 68 65 20 65 78 70 61 6e 64 65 64   in the expanded
351d6 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
351d7 72 65 73 75 6c 74 2d 73 65 74 20 6c 69 73 74 2e  result-set list.
351d8 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  .            */.
351d9 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
351da 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26  IsHiddenColumn(&
351db 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 29 20 29  pTab->aCol[j]) )
351dc 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
351dd 61 73 73 65 72 74 28 49 73 56 69 72 74 75 61 6c  assert(IsVirtual
351de 28 70 54 61 62 29 29 3b 0a 20 20 20 20 20 20 20  (pTab));.       
351df 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
351e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 0a  .            }..
351e1 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
351e2 69 3e 30 20 26 26 20 7a 54 4e 61 6d 65 3d 3d 30  i>0 && zTName==0
351e3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
351e4 20 20 69 66 28 20 28 70 46 72 6f 6d 2d 3e 6a 6f    if( (pFrom->jo
351e5 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 55  intype & JT_NATU
351e6 52 41 4c 29 21 3d 30 0a 20 20 20 20 20 20 20 20  RAL)!=0.        
351e7 20 20 20 20 20 20 20 20 26 26 20 74 61 62 6c 65          && table
351e8 41 6e 64 43 6f 6c 75 6d 6e 49 6e 64 65 78 28 70  AndColumnIndex(p
351e9 54 61 62 4c 69 73 74 2c 20 69 2c 20 7a 4e 61 6d  TabList, i, zNam
351ea 65 2c 20 30 2c 20 30 29 0a 20 20 20 20 20 20 20  e, 0, 0).       
351eb 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20         ){.      
351ec 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20            /* In 
351ed 61 20 4e 41 54 55 52 41 4c 20 6a 6f 69 6e 2c 20  a NATURAL join, 
351ee 6f 6d 69 74 20 74 68 65 20 6a 6f 69 6e 20 63 6f  omit the join co
351ef 6c 75 6d 6e 73 20 66 72 6f 6d 20 74 68 65 20 0a  lumns from the .
351f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
351f1 2a 2a 20 74 61 62 6c 65 20 74 6f 20 74 68 65 20  ** table to the 
351f2 72 69 67 68 74 20 6f 66 20 74 68 65 20 6a 6f 69  right of the joi
351f3 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  n */.           
351f4 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
351f5 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
351f6 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
351f7 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e   sqlite3IdListIn
351f8 64 65 78 28 70 46 72 6f 6d 2d 3e 70 55 73 69 6e  dex(pFrom->pUsin
351f9 67 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a  g, zName)>=0 ){.
351fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
351fb 2f 2a 20 49 6e 20 61 20 6a 6f 69 6e 20 77 69 74  /* In a join wit
351fc 68 20 61 20 55 53 49 4e 47 20 63 6c 61 75 73 65  h a USING clause
351fd 2c 20 6f 6d 69 74 20 63 6f 6c 75 6d 6e 73 20 69  , omit columns i
351fe 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  n the.          
351ff 20 20 20 20 20 20 2a 2a 20 75 73 69 6e 67 20 63        ** using c
35200 6c 61 75 73 65 20 66 72 6f 6d 20 74 68 65 20 74  lause from the t
35201 61 62 6c 65 20 6f 6e 20 74 68 65 20 72 69 67 68  able on the righ
35202 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  t. */.          
35203 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
35204 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
35205 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
35206 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74            pRight
35207 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
35208 62 2c 20 54 4b 5f 49 44 2c 20 7a 4e 61 6d 65 29  b, TK_ID, zName)
35209 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 43  ;.            zC
3520a 6f 6c 6e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a  olname = zName;.
3520b 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 6f 46              zToF
3520c 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ree = 0;.       
3520d 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 4e 61 6d       if( longNam
3520e 65 73 20 7c 7c 20 70 54 61 62 4c 69 73 74 2d 3e  es || pTabList->
3520f 6e 53 72 63 3e 31 20 29 7b 0a 20 20 20 20 20 20  nSrc>1 ){.      
35210 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c          Expr *pL
35211 65 66 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eft;.           
35212 20 20 20 70 4c 65 66 74 20 3d 20 73 71 6c 69 74     pLeft = sqlit
35213 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 44  e3Expr(db, TK_ID
35214 2c 20 7a 54 61 62 4e 61 6d 65 29 3b 0a 20 20 20  , zTabName);.   
35215 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
35216 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
35217 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20  pParse, TK_DOT, 
35218 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30  pLeft, pRight, 0
35219 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
3521a 20 69 66 28 20 6c 6f 6e 67 4e 61 6d 65 73 20 29   if( longNames )
3521b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
3521c 20 20 7a 43 6f 6c 6e 61 6d 65 20 3d 20 73 71 6c    zColname = sql
3521d 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
3521e 22 25 73 2e 25 73 22 2c 20 7a 54 61 62 4e 61 6d  "%s.%s", zTabNam
3521f 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  e, zName);.     
35220 20 20 20 20 20 20 20 20 20 20 20 7a 54 6f 46 72             zToFr
35221 65 65 20 3d 20 7a 43 6f 6c 6e 61 6d 65 3b 0a 20  ee = zColname;. 
35222 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
35223 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
35224 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
35225 70 45 78 70 72 20 3d 20 70 52 69 67 68 74 3b 0a  pExpr = pRight;.
35226 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
35227 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d            pNew =
35228 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
35229 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70  Append(pParse, p
3522a 4e 65 77 2c 20 70 45 78 70 72 29 3b 0a 20 20 20  New, pExpr);.   
3522b 20 20 20 20 20 20 20 20 20 73 43 6f 6c 6e 61 6d           sColnam
3522c 65 2e 7a 20 3d 20 7a 43 6f 6c 6e 61 6d 65 3b 0a  e.z = zColname;.
3522d 20 20 20 20 20 20 20 20 20 20 20 20 73 43 6f 6c              sCol
3522e 6e 61 6d 65 2e 6e 20 3d 20 73 71 6c 69 74 65 33  name.n = sqlite3
3522f 53 74 72 6c 65 6e 33 30 28 7a 43 6f 6c 6e 61 6d  Strlen30(zColnam
35230 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e);.            
35231 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53  sqlite3ExprListS
35232 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  etName(pParse, p
35233 4e 65 77 2c 20 26 73 43 6f 6c 6e 61 6d 65 2c 20  New, &sColname, 
35234 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
35235 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
35236 2c 20 7a 54 6f 46 72 65 65 29 3b 0a 20 20 20 20  , zToFree);.    
35237 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
35238 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 74  }.        if( !t
35239 61 62 6c 65 53 65 65 6e 20 29 7b 0a 20 20 20 20  ableSeen ){.    
3523a 20 20 20 20 20 20 69 66 28 20 7a 54 4e 61 6d 65        if( zTName
3523b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
3523c 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
3523d 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
3523e 20 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a 54 4e   table: %s", zTN
3523f 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ame);.          
35240 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
35241 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
35242 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 74  sg(pParse, "no t
35243 61 62 6c 65 73 20 73 70 65 63 69 66 69 65 64 22  ables specified"
35244 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
35245 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
35246 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
35247 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
35248 28 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  (db, pEList);.  
35249 20 20 70 2d 3e 70 45 4c 69 73 74 20 3d 20 70 4e    p->pEList = pN
3524a 65 77 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49  ew;.  }.#if SQLI
3524b 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20  TE_MAX_COLUMN.  
3524c 69 66 28 20 70 2d 3e 70 45 4c 69 73 74 20 26 26  if( p->pEList &&
3524d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70   p->pEList->nExp
3524e 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  r>db->aLimit[SQL
3524f 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e  ITE_LIMIT_COLUMN
35250 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
35251 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
35252 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d   "too many colum
35253 6e 73 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74  ns in result set
35254 22 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ");.  }.#endif. 
35255 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
35256 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4e  inue;.}../*.** N
35257 6f 2d 6f 70 20 72 6f 75 74 69 6e 65 20 66 6f 72  o-op routine for
35258 20 74 68 65 20 70 61 72 73 65 2d 74 72 65 65 20   the parse-tree 
35259 77 61 6c 6b 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 68  walker..**.** Wh
3525a 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
3525b 69 73 20 74 68 65 20 57 61 6c 6b 65 72 2e 78 45  is the Walker.xE
3525c 78 70 72 43 61 6c 6c 62 61 63 6b 20 74 68 65 6e  xprCallback then
3525d 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
3525e 73 0a 2a 2a 20 61 72 65 20 77 61 6c 6b 65 64 20  s.** are walked 
3525f 77 69 74 68 6f 75 74 20 61 6e 79 20 61 63 74 69  without any acti
35260 6f 6e 73 20 62 65 69 6e 67 20 74 61 6b 65 6e 20  ons being taken 
35261 61 74 20 65 61 63 68 20 6e 6f 64 65 2e 20 20 50  at each node.  P
35262 72 65 73 75 6d 61 62 6c 79 2c 0a 2a 2a 20 77 68  resumably,.** wh
35263 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
35264 69 73 20 75 73 65 64 20 66 6f 72 20 57 61 6c 6b  is used for Walk
35265 65 72 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b  er.xExprCallback
35266 20 74 68 65 6e 20 0a 2a 2a 20 57 61 6c 6b 65 72   then .** Walker
35267 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  .xSelectCallback
35268 20 69 73 20 73 65 74 20 74 6f 20 64 6f 20 73 6f   is set to do so
35269 6d 65 74 68 69 6e 67 20 75 73 65 66 75 6c 20 66  mething useful f
3526a 6f 72 20 65 76 65 72 79 20 0a 2a 2a 20 73 75 62  or every .** sub
3526b 71 75 65 72 79 20 69 6e 20 74 68 65 20 70 61 72  query in the par
3526c 73 65 72 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61  ser tree..*/.sta
3526d 74 69 63 20 69 6e 74 20 65 78 70 72 57 61 6c 6b  tic int exprWalk
3526e 4e 6f 6f 70 28 57 61 6c 6b 65 72 20 2a 4e 6f 74  Noop(Walker *Not
3526f 55 73 65 64 2c 20 45 78 70 72 20 2a 4e 6f 74 55  Used, Expr *NotU
35270 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f  sed2){.  UNUSED_
35271 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
35272 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20  ed, NotUsed2);. 
35273 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
35274 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  inue;.}../*.** T
35275 68 69 73 20 72 6f 75 74 69 6e 65 20 22 65 78 70  his routine "exp
35276 61 6e 64 73 22 20 61 20 53 45 4c 45 43 54 20 73  ands" a SELECT s
35277 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 61 6c 6c  tatement and all
35278 20 6f 66 20 69 74 73 20 73 75 62 71 75 65 72 69   of its subqueri
35279 65 73 2e 0a 2a 2a 20 46 6f 72 20 61 64 64 69 74  es..** For addit
3527a 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
3527b 6e 20 6f 6e 20 77 68 61 74 20 69 74 20 6d 65 61  n on what it mea
3527c 6e 73 20 74 6f 20 22 65 78 70 61 6e 64 22 20 61  ns to "expand" a
3527d 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65   SELECT.** state
3527e 6d 65 6e 74 2c 20 73 65 65 20 74 68 65 20 63 6f  ment, see the co
3527f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20 73 65 6c  mment on the sel
35280 65 63 74 45 78 70 61 6e 64 20 77 6f 72 6b 65 72  ectExpand worker
35281 20 63 61 6c 6c 62 61 63 6b 20 61 62 6f 76 65 2e   callback above.
35282 0a 2a 2a 0a 2a 2a 20 45 78 70 61 6e 64 69 6e 67  .**.** Expanding
35283 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
35284 65 6e 74 20 69 73 20 74 68 65 20 66 69 72 73 74  ent is the first
35285 20 73 74 65 70 20 69 6e 20 70 72 6f 63 65 73 73   step in process
35286 69 6e 67 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20  ing a.** SELECT 
35287 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20  statement.  The 
35288 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
35289 20 6d 75 73 74 20 62 65 20 65 78 70 61 6e 64 65   must be expande
3528a 64 20 62 65 66 6f 72 65 0a 2a 2a 20 6e 61 6d 65  d before.** name
3528b 20 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 70   resolution is p
3528c 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20  erformed..**.** 
3528d 49 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73  If anything goes
3528e 20 77 72 6f 6e 67 2c 20 61 6e 20 65 72 72 6f 72   wrong, an error
3528f 20 6d 65 73 73 61 67 65 20 69 73 20 77 72 69 74   message is writ
35290 74 65 6e 20 69 6e 74 6f 20 70 50 61 72 73 65 2e  ten into pParse.
35291 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  .** The calling 
35292 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 64 65 74  function can det
35293 65 63 74 20 74 68 65 20 70 72 6f 62 6c 65 6d 20  ect the problem 
35294 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 70 50  by looking at pP
35295 61 72 73 65 2d 3e 6e 45 72 72 0a 2a 2a 20 61 6e  arse->nErr.** an
35296 64 2f 6f 72 20 70 50 61 72 73 65 2d 3e 64 62 2d  d/or pParse->db-
35297 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 2e 0a 2a  >mallocFailed..*
35298 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
35299 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e  lite3SelectExpan
3529a 64 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  d(Parse *pParse,
3529b 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
3529c 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20  ){.  Walker w;. 
3529d 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61   w.xSelectCallba
3529e 63 6b 20 3d 20 73 65 6c 65 63 74 45 78 70 61 6e  ck = selectExpan
3529f 64 65 72 3b 0a 20 20 77 2e 78 45 78 70 72 43 61  der;.  w.xExprCa
352a0 6c 6c 62 61 63 6b 20 3d 20 65 78 70 72 57 61 6c  llback = exprWal
352a1 6b 4e 6f 6f 70 3b 0a 20 20 77 2e 70 50 61 72 73  kNoop;.  w.pPars
352a2 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71  e = pParse;.  sq
352a3 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
352a4 26 77 2c 20 70 53 65 6c 65 63 74 29 3b 0a 7d 0a  &w, pSelect);.}.
352a5 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
352a6 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 2f  _OMIT_SUBQUERY./
352a7 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 57  *.** This is a W
352a8 61 6c 6b 65 72 2e 78 53 65 6c 65 63 74 43 61 6c  alker.xSelectCal
352a9 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 66  lback callback f
352aa 6f 72 20 74 68 65 20 73 71 6c 69 74 65 33 53 65  or the sqlite3Se
352ab 6c 65 63 74 54 79 70 65 49 6e 66 6f 28 29 0a 2a  lectTypeInfo().*
352ac 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a  * interface..**.
352ad 2a 2a 20 46 6f 72 20 65 61 63 68 20 46 52 4f 4d  ** For each FROM
352ae 2d 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79  -clause subquery
352af 2c 20 61 64 64 20 43 6f 6c 75 6d 6e 2e 7a 54 79  , add Column.zTy
352b0 70 65 20 61 6e 64 20 43 6f 6c 75 6d 6e 2e 7a 43  pe and Column.zC
352b1 6f 6c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  oll.** informati
352b2 6f 6e 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20  on to the Table 
352b3 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 72  structure that r
352b4 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 72 65  epresents the re
352b5 73 75 6c 74 20 73 65 74 0a 2a 2a 20 6f 66 20 74  sult set.** of t
352b6 68 61 74 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a  hat subquery..**
352b7 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74  .** The Table st
352b8 72 75 63 74 75 72 65 20 74 68 61 74 20 72 65 70  ructure that rep
352b9 72 65 73 65 6e 74 73 20 74 68 65 20 72 65 73 75  resents the resu
352ba 6c 74 20 73 65 74 20 77 61 73 20 63 6f 6e 73 74  lt set was const
352bb 72 75 63 74 65 64 0a 2a 2a 20 62 79 20 73 65 6c  ructed.** by sel
352bc 65 63 74 45 78 70 61 6e 64 65 72 28 29 20 62 75  ectExpander() bu
352bd 74 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 63  t the type and c
352be 6f 6c 6c 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61  ollation informa
352bf 74 69 6f 6e 20 77 61 73 20 6f 6d 69 74 74 65 64  tion was omitted
352c0 0a 2a 2a 20 61 74 20 74 68 61 74 20 70 6f 69 6e  .** at that poin
352c1 74 20 62 65 63 61 75 73 65 20 69 64 65 6e 74 69  t because identi
352c2 66 69 65 72 73 20 68 61 64 20 6e 6f 74 20 79 65  fiers had not ye
352c3 74 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e  t been resolved.
352c4 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e    This.** routin
352c5 65 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  e is called afte
352c6 72 20 69 64 65 6e 74 69 66 69 65 72 20 72 65 73  r identifier res
352c7 6f 6c 75 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  olution..*/.stat
352c8 69 63 20 69 6e 74 20 73 65 6c 65 63 74 41 64 64  ic int selectAdd
352c9 53 75 62 71 75 65 72 79 54 79 70 65 49 6e 66 6f  SubqueryTypeInfo
352ca 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
352cb 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
352cc 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 20  Parse *pParse;. 
352cd 20 69 6e 74 20 69 3b 0a 20 20 53 72 63 4c 69 73   int i;.  SrcLis
352ce 74 20 2a 70 54 61 62 4c 69 73 74 3b 0a 20 20 73  t *pTabList;.  s
352cf 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
352d0 65 6d 20 2a 70 46 72 6f 6d 3b 0a 0a 20 20 61 73  em *pFrom;..  as
352d1 73 65 72 74 28 20 70 2d 3e 73 65 6c 46 6c 61 67  sert( p->selFlag
352d2 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 20  s & SF_Resolved 
352d3 29 3b 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c  );.  if( (p->sel
352d4 46 6c 61 67 73 20 26 20 53 46 5f 48 61 73 54 79  Flags & SF_HasTy
352d5 70 65 49 6e 66 6f 29 3d 3d 30 20 29 7b 0a 20 20  peInfo)==0 ){.  
352d6 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d    p->selFlags |=
352d7 20 53 46 5f 48 61 73 54 79 70 65 49 6e 66 6f 3b   SF_HasTypeInfo;
352d8 0a 20 20 20 20 70 50 61 72 73 65 20 3d 20 70 57  .    pParse = pW
352d9 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20  alker->pParse;. 
352da 20 20 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d     pTabList = p-
352db 3e 70 53 72 63 3b 0a 20 20 20 20 66 6f 72 28 69  >pSrc;.    for(i
352dc 3d 30 2c 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69  =0, pFrom=pTabLi
352dd 73 74 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73  st->a; i<pTabLis
352de 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46  t->nSrc; i++, pF
352df 72 6f 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 54 61  rom++){.      Ta
352e0 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 46 72 6f  ble *pTab = pFro
352e1 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 69  m->pTab;.      i
352e2 66 28 20 41 4c 57 41 59 53 28 70 54 61 62 21 3d  f( ALWAYS(pTab!=
352e3 30 29 20 26 26 20 28 70 54 61 62 2d 3e 74 61 62  0) && (pTab->tab
352e4 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68 65 6d  Flags & TF_Ephem
352e5 65 72 61 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20  eral)!=0 ){.    
352e6 20 20 20 20 2f 2a 20 41 20 73 75 62 2d 71 75 65      /* A sub-que
352e7 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ry in the FROM c
352e8 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43  lause of a SELEC
352e9 54 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 65 6c  T */.        Sel
352ea 65 63 74 20 2a 70 53 65 6c 20 3d 20 70 46 72 6f  ect *pSel = pFro
352eb 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20  m->pSelect;.    
352ec 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c      assert( pSel
352ed 20 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c   );.        whil
352ee 65 28 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 20  e( pSel->pPrior 
352ef 29 20 70 53 65 6c 20 3d 20 70 53 65 6c 2d 3e 70  ) pSel = pSel->p
352f0 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 20 20 73  Prior;.        s
352f1 65 6c 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79  electAddColumnTy
352f2 70 65 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70  peAndCollation(p
352f3 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f  Parse, pTab->nCo
352f4 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 70  l, pTab->aCol, p
352f5 53 65 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Sel);.      }.  
352f6 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
352f7 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
352f8 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
352f9 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 64 64  This routine add
352fa 73 20 64 61 74 61 74 79 70 65 20 61 6e 64 20 63  s datatype and c
352fb 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
352fc 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  e information to
352fd 0a 2a 2a 20 74 68 65 20 54 61 62 6c 65 20 73 74  .** the Table st
352fe 72 75 63 74 75 72 65 73 20 6f 66 20 61 6c 6c 20  ructures of all 
352ff 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75 62 71  FROM-clause subq
35300 75 65 72 69 65 73 20 69 6e 20 61 0a 2a 2a 20 53  ueries in a.** S
35301 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
35302 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 69 73 20  .**.** Use this 
35303 72 6f 75 74 69 6e 65 20 61 66 74 65 72 20 6e 61  routine after na
35304 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 2e 0a 2a  me resolution..*
35305 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
35306 6c 69 74 65 33 53 65 6c 65 63 74 41 64 64 54 79  lite3SelectAddTy
35307 70 65 49 6e 66 6f 28 50 61 72 73 65 20 2a 70 50  peInfo(Parse *pP
35308 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 53  arse, Select *pS
35309 65 6c 65 63 74 29 7b 0a 23 69 66 6e 64 65 66 20  elect){.#ifndef 
3530a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
3530b 55 45 52 59 0a 20 20 57 61 6c 6b 65 72 20 77 3b  UERY.  Walker w;
3530c 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c  .  w.xSelectCall
3530d 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 41 64 64  back = selectAdd
3530e 53 75 62 71 75 65 72 79 54 79 70 65 49 6e 66 6f  SubqueryTypeInfo
3530f 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62  ;.  w.xExprCallb
35310 61 63 6b 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f  ack = exprWalkNo
35311 6f 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d  op;.  w.pParse =
35312 20 70 50 61 72 73 65 3b 0a 20 20 73 71 6c 69 74   pParse;.  sqlit
35313 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c  e3WalkSelect(&w,
35314 20 70 53 65 6c 65 63 74 29 3b 0a 23 65 6e 64 69   pSelect);.#endi
35315 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  f.}.../*.** This
35316 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 75 70   routine sets up
35317 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
35318 65 6e 74 20 66 6f 72 20 70 72 6f 63 65 73 73 69  ent for processi
35319 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c  ng.  The.** foll
3531a 6f 77 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c  owing is accompl
3531b 69 73 68 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ished:.**.**    
3531c 20 2a 20 20 56 44 42 45 20 43 75 72 73 6f 72 20   *  VDBE Cursor 
3531d 6e 75 6d 62 65 72 73 20 61 72 65 20 61 73 73 69  numbers are assi
3531e 67 6e 65 64 20 74 6f 20 61 6c 6c 20 46 52 4f 4d  gned to all FROM
3531f 2d 63 6c 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a  -clause terms..*
35320 2a 20 20 20 20 20 2a 20 20 45 70 68 65 6d 65 72  *     *  Ephemer
35321 61 6c 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 73  al Table objects
35322 20 61 72 65 20 63 72 65 61 74 65 64 20 66 6f 72   are created for
35323 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73 65   all FROM-clause
35324 20 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20   subqueries..** 
35325 20 20 20 20 2a 20 20 4f 4e 20 61 6e 64 20 55 53      *  ON and US
35326 49 4e 47 20 63 6c 61 75 73 65 73 20 61 72 65 20  ING clauses are 
35327 73 68 69 66 74 65 64 20 69 6e 74 6f 20 57 48 45  shifted into WHE
35328 52 45 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a  RE statements.**
35329 20 20 20 20 20 2a 20 20 57 69 6c 64 63 61 72 64       *  Wildcard
3532a 73 20 22 2a 22 20 61 6e 64 20 22 54 41 42 4c 45  s "*" and "TABLE
3532b 2e 2a 22 20 69 6e 20 72 65 73 75 6c 74 20 73 65  .*" in result se
3532c 74 73 20 61 72 65 20 65 78 70 61 6e 64 65 64 2e  ts are expanded.
3532d 0a 2a 2a 20 20 20 20 20 2a 20 20 49 64 65 6e 74  .**     *  Ident
3532e 69 66 69 65 72 73 20 69 6e 20 65 78 70 72 65 73  ifiers in expres
3532f 73 69 6f 6e 20 61 72 65 20 6d 61 74 63 68 65 64  sion are matched
35330 20 74 6f 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a   to tables..**.*
35331 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
35332 63 74 73 20 72 65 63 75 72 73 69 76 65 6c 79 20  cts recursively 
35333 6f 6e 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65  on all subquerie
35334 73 20 77 69 74 68 69 6e 20 74 68 65 20 53 45 4c  s within the SEL
35335 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ECT..*/.SQLITE_P
35336 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
35337 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 0a 20  te3SelectPrep(. 
35338 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
35339 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
3533a 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
3533b 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
3533c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3533d 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
3533e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a  t being coded. *
3533f 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  /.  NameContext 
35340 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a 20 4e 61  *pOuterNC  /* Na
35341 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 63  me context for c
35342 6f 6e 74 61 69 6e 65 72 20 2a 2f 0a 29 7b 0a 20  ontainer */.){. 
35343 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
35344 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20  if( NEVER(p==0) 
35345 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d  ) return;.  db =
35346 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
35347 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  f( p->selFlags &
35348 20 53 46 5f 48 61 73 54 79 70 65 49 6e 66 6f 20   SF_HasTypeInfo 
35349 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69  ) return;.  sqli
3534a 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28  te3SelectExpand(
3534b 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 69 66  pParse, p);.  if
3534c 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
3534d 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
3534e 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  ed ) return;.  s
3534f 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c  qlite3ResolveSel
35350 65 63 74 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  ectNames(pParse,
35351 20 70 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20   p, pOuterNC);. 
35352 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
35353 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
35354 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
35355 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 41    sqlite3SelectA
35356 64 64 54 79 70 65 49 6e 66 6f 28 70 50 61 72 73  ddTypeInfo(pPars
35357 65 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  e, p);.}../*.** 
35358 52 65 73 65 74 20 74 68 65 20 61 67 67 72 65 67  Reset the aggreg
35359 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e  ate accumulator.
3535a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 67 67 72 65  .**.** The aggre
3535b 67 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72  gate accumulator
3535c 20 69 73 20 61 20 73 65 74 20 6f 66 20 6d 65 6d   is a set of mem
3535d 6f 72 79 20 63 65 6c 6c 73 20 74 68 61 74 20 68  ory cells that h
3535e 6f 6c 64 0a 2a 2a 20 69 6e 74 65 72 6d 65 64 69  old.** intermedi
3535f 61 74 65 20 72 65 73 75 6c 74 73 20 77 68 69 6c  ate results whil
35360 65 20 63 61 6c 63 75 6c 61 74 69 6e 67 20 61 6e  e calculating an
35361 20 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 69   aggregate.  Thi
35362 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67 65 6e  s.** routine gen
35363 65 72 61 74 65 73 20 63 6f 64 65 20 74 68 61 74  erates code that
35364 20 73 74 6f 72 65 73 20 4e 55 4c 4c 73 20 69 6e   stores NULLs in
35365 20 61 6c 6c 20 6f 66 20 74 68 6f 73 65 20 6d 65   all of those me
35366 6d 6f 72 79 0a 2a 2a 20 63 65 6c 6c 73 2e 0a 2a  mory.** cells..*
35367 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
35368 73 65 74 41 63 63 75 6d 75 6c 61 74 6f 72 28 50  setAccumulator(P
35369 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 41 67  arse *pParse, Ag
3536a 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 29  gInfo *pAggInfo)
3536b 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
3536c 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
3536d 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 41  nt i;.  struct A
3536e 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 46 75  ggInfo_func *pFu
3536f 6e 63 3b 0a 20 20 69 66 28 20 70 41 67 67 49 6e  nc;.  if( pAggIn
35370 66 6f 2d 3e 6e 46 75 6e 63 2b 70 41 67 67 49 6e  fo->nFunc+pAggIn
35371 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 29  fo->nColumn==0 )
35372 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
35373 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  }.  for(i=0; i<p
35374 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e  AggInfo->nColumn
35375 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
35376 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
35377 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 70 41 67   OP_Null, 0, pAg
35378 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 69 5d 2e 69  gInfo->aCol[i].i
35379 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  Mem);.  }.  for(
3537a 70 46 75 6e 63 3d 70 41 67 67 49 6e 66 6f 2d 3e  pFunc=pAggInfo->
3537b 61 46 75 6e 63 2c 20 69 3d 30 3b 20 69 3c 70 41  aFunc, i=0; i<pA
3537c 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69  ggInfo->nFunc; i
3537d 2b 2b 2c 20 70 46 75 6e 63 2b 2b 29 7b 0a 20 20  ++, pFunc++){.  
3537e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3537f 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
35380 30 2c 20 70 46 75 6e 63 2d 3e 69 4d 65 6d 29 3b  0, pFunc->iMem);
35381 0a 20 20 20 20 69 66 28 20 70 46 75 6e 63 2d 3e  .    if( pFunc->
35382 69 44 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0a  iDistinct>=0 ){.
35383 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d        Expr *pE =
35384 20 70 46 75 6e 63 2d 3e 70 45 78 70 72 3b 0a 20   pFunc->pExpr;. 
35385 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
35386 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
35387 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
35388 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 2d  );.      if( pE-
35389 3e 78 2e 70 4c 69 73 74 3d 3d 30 20 7c 7c 20 70  >x.pList==0 || p
3538a 45 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70  E->x.pList->nExp
3538b 72 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  r!=1 ){.        
3538c 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
3538d 70 50 61 72 73 65 2c 20 22 44 49 53 54 49 4e 43  pParse, "DISTINC
3538e 54 20 61 67 67 72 65 67 61 74 65 73 20 6d 75 73  T aggregates mus
3538f 74 20 68 61 76 65 20 65 78 61 63 74 6c 79 20 6f  t have exactly o
35390 6e 65 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  ne ".           
35391 22 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 20  "argument");.   
35392 20 20 20 20 20 70 46 75 6e 63 2d 3e 69 44 69 73       pFunc->iDis
35393 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 20 20  tinct = -1;.    
35394 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
35395 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
35396 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d  fo = keyInfoFrom
35397 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ExprList(pParse,
35398 20 70 45 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20   pE->x.pList);. 
35399 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
3539a 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
3539b 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 46  penEphemeral, pF
3539c 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74 2c 20  unc->iDistinct, 
3539d 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  0, 0,.          
3539e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3539f 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c  (char*)pKeyInfo,
353a0 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
353a1 4f 46 46 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OFF);.      }.  
353a2 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
353a3 20 49 6e 76 6f 6b 65 20 74 68 65 20 4f 50 5f 41   Invoke the OP_A
353a4 67 67 46 69 6e 61 6c 69 7a 65 20 6f 70 63 6f 64  ggFinalize opcod
353a5 65 20 66 6f 72 20 65 76 65 72 79 20 61 67 67 72  e for every aggr
353a6 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a 2a  egate function.*
353a7 2a 20 69 6e 20 74 68 65 20 41 67 67 49 6e 66 6f  * in the AggInfo
353a8 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
353a9 74 61 74 69 63 20 76 6f 69 64 20 66 69 6e 61 6c  tatic void final
353aa 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73 28  izeAggFunctions(
353ab 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 41  Parse *pParse, A
353ac 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f  ggInfo *pAggInfo
353ad 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
353ae 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
353af 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
353b0 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 46  AggInfo_func *pF
353b1 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d  ;.  for(i=0, pF=
353b2 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b  pAggInfo->aFunc;
353b3 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75   i<pAggInfo->nFu
353b4 6e 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a  nc; i++, pF++){.
353b5 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
353b6 69 73 74 20 3d 20 70 46 2d 3e 70 45 78 70 72 2d  ist = pF->pExpr-
353b7 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 61 73  >x.pList;.    as
353b8 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
353b9 6f 70 65 72 74 79 28 70 46 2d 3e 70 45 78 70 72  operty(pF->pExpr
353ba 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
353bb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
353bc 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 41  beAddOp4(v, OP_A
353bd 67 67 46 69 6e 61 6c 2c 20 70 46 2d 3e 69 4d 65  ggFinal, pF->iMe
353be 6d 2c 20 70 4c 69 73 74 20 3f 20 70 4c 69 73 74  m, pList ? pList
353bf 2d 3e 6e 45 78 70 72 20 3a 20 30 2c 20 30 2c 0a  ->nExpr : 0, 0,.
353c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
353c1 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 46 2d        (void*)pF-
353c2 3e 70 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43 44  >pFunc, P4_FUNCD
353c3 45 46 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  EF);.  }.}../*.*
353c4 2a 20 55 70 64 61 74 65 20 74 68 65 20 61 63 63  * Update the acc
353c5 75 6d 75 6c 61 74 6f 72 20 6d 65 6d 6f 72 79 20  umulator memory 
353c6 63 65 6c 6c 73 20 66 6f 72 20 61 6e 20 61 67 67  cells for an agg
353c7 72 65 67 61 74 65 20 62 61 73 65 64 20 6f 6e 0a  regate based on.
353c8 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  ** the current c
353c9 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e 0a  ursor position..
353ca 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  */.static void u
353cb 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f 72  pdateAccumulator
353cc 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
353cd 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66  AggInfo *pAggInf
353ce 6f 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  o){.  Vdbe *v = 
353cf 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
353d0 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 65   int i;.  int re
353d1 67 48 69 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20  gHit = 0;.  int 
353d2 61 64 64 72 48 69 74 54 65 73 74 20 3d 20 30 3b  addrHitTest = 0;
353d3 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66  .  struct AggInf
353d4 6f 5f 66 75 6e 63 20 2a 70 46 3b 0a 20 20 73 74  o_func *pF;.  st
353d5 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c  ruct AggInfo_col
353d6 20 2a 70 43 3b 0a 0a 20 20 70 41 67 67 49 6e 66   *pC;..  pAggInf
353d7 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 3d 20  o->directMode = 
353d8 31 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  1;.  sqlite3Expr
353d9 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73  CacheClear(pPars
353da 65 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  e);.  for(i=0, p
353db 46 3d 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e  F=pAggInfo->aFun
353dc 63 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e  c; i<pAggInfo->n
353dd 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29  Func; i++, pF++)
353de 7b 0a 20 20 20 20 69 6e 74 20 6e 41 72 67 3b 0a  {.    int nArg;.
353df 20 20 20 20 69 6e 74 20 61 64 64 72 4e 65 78 74      int addrNext
353e0 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 72 65   = 0;.    int re
353e1 67 41 67 67 3b 0a 20 20 20 20 45 78 70 72 4c 69  gAgg;.    ExprLi
353e2 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e  st *pList = pF->
353e3 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a  pExpr->x.pList;.
353e4 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
353e5 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 46 2d  rHasProperty(pF-
353e6 3e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  >pExpr, EP_xIsSe
353e7 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 69 66 28  lect) );.    if(
353e8 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20   pList ){.      
353e9 6e 41 72 67 20 3d 20 70 4c 69 73 74 2d 3e 6e 45  nArg = pList->nE
353ea 78 70 72 3b 0a 20 20 20 20 20 20 72 65 67 41 67  xpr;.      regAg
353eb 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  g = sqlite3GetTe
353ec 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
353ed 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 73 71 6c  nArg);.      sql
353ee 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72  ite3ExprCodeExpr
353ef 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 4c 69  List(pParse, pLi
353f0 73 74 2c 20 72 65 67 41 67 67 2c 20 31 29 3b 0a  st, regAgg, 1);.
353f1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
353f2 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 20   nArg = 0;.     
353f3 20 72 65 67 41 67 67 20 3d 20 30 3b 0a 20 20 20   regAgg = 0;.   
353f4 20 7d 0a 20 20 20 20 69 66 28 20 70 46 2d 3e 69   }.    if( pF->i
353f5 44 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0a 20  Distinct>=0 ){. 
353f6 20 20 20 20 20 61 64 64 72 4e 65 78 74 20 3d 20       addrNext = 
353f7 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
353f8 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 61  abel(v);.      a
353f9 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 31 20 29  ssert( nArg==1 )
353fa 3b 0a 20 20 20 20 20 20 63 6f 64 65 44 69 73 74  ;.      codeDist
353fb 69 6e 63 74 28 70 50 61 72 73 65 2c 20 70 46 2d  inct(pParse, pF-
353fc 3e 69 44 69 73 74 69 6e 63 74 2c 20 61 64 64 72  >iDistinct, addr
353fd 4e 65 78 74 2c 20 31 2c 20 72 65 67 41 67 67 29  Next, 1, regAgg)
353fe 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
353ff 70 46 2d 3e 70 46 75 6e 63 2d 3e 66 6c 61 67 73  pF->pFunc->flags
35400 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e   & SQLITE_FUNC_N
35401 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20  EEDCOLL ){.     
35402 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
35403 3d 20 30 3b 0a 20 20 20 20 20 20 73 74 72 75 63  = 0;.      struc
35404 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
35405 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 69 6e  *pItem;.      in
35406 74 20 6a 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t j;.      asser
35407 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b 20 20  t( pList!=0 );  
35408 2f 2a 20 70 4c 69 73 74 21 3d 30 20 69 66 20 70  /* pList!=0 if p
35409 46 2d 3e 70 46 75 6e 63 20 68 61 73 20 4e 45 45  F->pFunc has NEE
3540a 44 43 4f 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 66  DCOLL */.      f
3540b 6f 72 28 6a 3d 30 2c 20 70 49 74 65 6d 3d 70 4c  or(j=0, pItem=pL
3540c 69 73 74 2d 3e 61 3b 20 21 70 43 6f 6c 6c 20 26  ist->a; !pColl &
3540d 26 20 6a 3c 6e 41 72 67 3b 20 6a 2b 2b 2c 20 70  & j<nArg; j++, p
3540e 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
3540f 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
35410 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
35411 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  se, pItem->pExpr
35412 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
35413 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20   if( !pColl ){. 
35414 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70         pColl = p
35415 50 61 72 73 65 2d 3e 64 62 2d 3e 70 44 66 6c 74  Parse->db->pDflt
35416 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Coll;.      }.  
35417 20 20 20 20 69 66 28 20 72 65 67 48 69 74 3d 3d      if( regHit==
35418 30 20 26 26 20 70 41 67 67 49 6e 66 6f 2d 3e 6e  0 && pAggInfo->n
35419 41 63 63 75 6d 75 6c 61 74 6f 72 20 29 20 72 65  Accumulator ) re
3541a 67 48 69 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d  gHit = ++pParse-
3541b 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c  >nMem;.      sql
3541c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
3541d 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 72 65  , OP_CollSeq, re
3541e 67 48 69 74 2c 20 30 2c 20 30 2c 20 28 63 68 61  gHit, 0, 0, (cha
3541f 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f  r *)pColl, P4_CO
35420 4c 4c 53 45 51 29 3b 0a 20 20 20 20 7d 0a 20 20  LLSEQ);.    }.  
35421 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
35422 4f 70 34 28 76 2c 20 4f 50 5f 41 67 67 53 74 65  Op4(v, OP_AggSte
35423 70 2c 20 30 2c 20 72 65 67 41 67 67 2c 20 70 46  p, 0, regAgg, pF
35424 2d 3e 69 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20  ->iMem,.        
35425 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 76                (v
35426 6f 69 64 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20  oid*)pF->pFunc, 
35427 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20  P4_FUNCDEF);.   
35428 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
35429 67 65 50 35 28 76 2c 20 28 75 38 29 6e 41 72 67  geP5(v, (u8)nArg
3542a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
3542b 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
3542c 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  hange(pParse, re
3542d 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20 20 20  gAgg, nArg);.   
3542e 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
3542f 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
35430 20 72 65 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a   regAgg, nArg);.
35431 20 20 20 20 69 66 28 20 61 64 64 72 4e 65 78 74      if( addrNext
35432 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
35433 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
35434 6c 28 76 2c 20 61 64 64 72 4e 65 78 74 29 3b 0a  l(v, addrNext);.
35435 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
35436 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72  rCacheClear(pPar
35437 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  se);.    }.  }..
35438 20 20 2f 2a 20 42 65 66 6f 72 65 20 70 6f 70 75    /* Before popu
35439 6c 61 74 69 6e 67 20 74 68 65 20 61 63 63 75 6d  lating the accum
3543a 75 6c 61 74 6f 72 20 72 65 67 69 73 74 65 72 73  ulator registers
3543b 2c 20 63 6c 65 61 72 20 74 68 65 20 63 6f 6c 75  , clear the colu
3543c 6d 6e 20 63 61 63 68 65 2e 0a 20 20 2a 2a 20 4f  mn cache..  ** O
3543d 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e 79  therwise, if any
3543e 20 6f 66 20 74 68 65 20 72 65 71 75 69 72 65 64   of the required
3543f 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 73 20 61   column values a
35440 72 65 20 61 6c 72 65 61 64 79 20 70 72 65 73 65  re already prese
35441 6e 74 20 0a 20 20 2a 2a 20 69 6e 20 72 65 67 69  nt .  ** in regi
35442 73 74 65 72 73 2c 20 73 71 6c 69 74 65 33 45 78  sters, sqlite3Ex
35443 70 72 43 6f 64 65 28 29 20 6d 61 79 20 75 73 65  prCode() may use
35444 20 4f 50 5f 53 43 6f 70 79 20 74 6f 20 63 6f 70   OP_SCopy to cop
35445 79 20 74 68 65 20 76 61 6c 75 65 0a 20 20 2a 2a  y the value.  **
35446 20 74 6f 20 70 43 2d 3e 69 4d 65 6d 2e 20 42 75   to pC->iMem. Bu
35447 74 20 62 79 20 74 68 65 20 74 69 6d 65 20 74 68  t by the time th
35448 65 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 2c  e value is used,
35449 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 72 65   the original re
3544a 67 69 73 74 65 72 0a 20 20 2a 2a 20 6d 61 79 20  gister.  ** may 
3544b 68 61 76 65 20 62 65 65 6e 20 75 73 65 64 2c 20  have been used, 
3544c 69 6e 76 61 6c 69 64 61 74 69 6e 67 20 74 68 65  invalidating the
3544d 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 75 66 66   underlying buff
3544e 65 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 20  er holding the. 
3544f 20 2a 2a 20 74 65 78 74 20 6f 72 20 62 6c 6f 62   ** text or blob
35450 20 76 61 6c 75 65 2e 20 53 65 65 20 74 69 63 6b   value. See tick
35451 65 74 20 5b 38 38 33 30 33 34 64 63 62 35 5d 2e  et [883034dcb5].
35452 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 6f 74 68  .  **.  ** Anoth
35453 65 72 20 73 6f 6c 75 74 69 6f 6e 20 77 6f 75 6c  er solution woul
35454 64 20 62 65 20 74 6f 20 63 68 61 6e 67 65 20 74  d be to change t
35455 68 65 20 4f 50 5f 53 43 6f 70 79 20 75 73 65 64  he OP_SCopy used
35456 20 74 6f 20 63 6f 70 79 20 63 61 63 68 65 64 0a   to copy cached.
35457 20 20 2a 2a 20 76 61 6c 75 65 73 20 74 6f 20 61    ** values to a
35458 6e 20 4f 50 5f 43 6f 70 79 2e 0a 20 20 2a 2f 0a  n OP_Copy..  */.
35459 20 20 69 66 28 20 72 65 67 48 69 74 20 29 7b 0a    if( regHit ){.
3545a 20 20 20 20 61 64 64 72 48 69 74 54 65 73 74 20      addrHitTest 
3545b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
3545c 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65  Op1(v, OP_If, re
3545d 67 48 69 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  gHit);.  }.  sql
3545e 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65  ite3ExprCacheCle
3545f 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 66 6f  ar(pParse);.  fo
35460 72 28 69 3d 30 2c 20 70 43 3d 70 41 67 67 49 6e  r(i=0, pC=pAggIn
35461 66 6f 2d 3e 61 43 6f 6c 3b 20 69 3c 70 41 67 67  fo->aCol; i<pAgg
35462 49 6e 66 6f 2d 3e 6e 41 63 63 75 6d 75 6c 61 74  Info->nAccumulat
35463 6f 72 3b 20 69 2b 2b 2c 20 70 43 2b 2b 29 7b 0a  or; i++, pC++){.
35464 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
35465 6f 64 65 28 70 50 61 72 73 65 2c 20 70 43 2d 3e  ode(pParse, pC->
35466 70 45 78 70 72 2c 20 70 43 2d 3e 69 4d 65 6d 29  pExpr, pC->iMem)
35467 3b 0a 20 20 7d 0a 20 20 70 41 67 67 49 6e 66 6f  ;.  }.  pAggInfo
35468 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 3d 20 30  ->directMode = 0
35469 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43  ;.  sqlite3ExprC
3546a 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73 65  acheClear(pParse
3546b 29 3b 0a 20 20 69 66 28 20 61 64 64 72 48 69 74  );.  if( addrHit
3546c 54 65 73 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  Test ){.    sqli
3546d 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
3546e 76 2c 20 61 64 64 72 48 69 74 54 65 73 74 29 3b  v, addrHitTest);
3546f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  .  }.}../*.** Ad
35470 64 20 61 20 73 69 6e 67 6c 65 20 4f 50 5f 45 78  d a single OP_Ex
35471 70 6c 61 69 6e 20 69 6e 73 74 72 75 63 74 69 6f  plain instructio
35472 6e 20 74 6f 20 74 68 65 20 56 44 42 45 20 74 6f  n to the VDBE to
35473 20 65 78 70 6c 61 69 6e 20 61 20 73 69 6d 70 6c   explain a simpl
35474 65 0a 2a 2a 20 63 6f 75 6e 74 28 2a 29 20 71 75  e.** count(*) qu
35475 65 72 79 20 28 22 53 45 4c 45 43 54 20 63 6f 75  ery ("SELECT cou
35476 6e 74 28 2a 29 20 46 52 4f 4d 20 70 54 61 62 22  nt(*) FROM pTab"
35477 29 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  )..*/.#ifndef SQ
35478 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
35479 4e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78  N.static void ex
3547a 70 6c 61 69 6e 53 69 6d 70 6c 65 43 6f 75 6e 74  plainSimpleCount
3547b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
3547c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
3547d 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e      /* Parse con
3547e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20  text */.  Table 
3547f 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 20  *pTab,          
35480 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62            /* Tab
35481 6c 65 20 62 65 69 6e 67 20 71 75 65 72 69 65 64  le being queried
35482 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
35483 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x               
35484 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 75        /* Index u
35485 73 65 64 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20  sed to optimize 
35486 73 63 61 6e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f  scan, or NULL */
35487 0a 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73 65  .){.  if( pParse
35488 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a  ->explain==2 ){.
35489 20 20 20 20 63 68 61 72 20 2a 7a 45 71 70 20 3d      char *zEqp =
3548a 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
3548b 70 50 61 72 73 65 2d 3e 64 62 2c 20 22 53 43 41  pParse->db, "SCA
3548c 4e 20 54 41 42 4c 45 20 25 73 20 25 73 25 73 28  N TABLE %s %s%s(
3548d 7e 25 64 20 72 6f 77 73 29 22 2c 0a 20 20 20 20  ~%d rows)",.    
3548e 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c      pTab->zName,
3548f 20 0a 20 20 20 20 20 20 20 20 70 49 64 78 20 3f   .        pIdx ?
35490 20 22 55 53 49 4e 47 20 43 4f 56 45 52 49 4e 47   "USING COVERING
35491 20 49 4e 44 45 58 20 22 20 3a 20 22 22 2c 0a 20   INDEX " : "",. 
35492 20 20 20 20 20 20 20 70 49 64 78 20 3f 20 70 49         pIdx ? pI
35493 64 78 2d 3e 7a 4e 61 6d 65 20 3a 20 22 22 2c 0a  dx->zName : "",.
35494 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 52          pTab->nR
35495 6f 77 45 73 74 0a 20 20 20 20 29 3b 0a 20 20 20  owEst.    );.   
35496 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
35497 70 34 28 0a 20 20 20 20 20 20 20 20 70 50 61 72  p4(.        pPar
35498 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 45 78  se->pVdbe, OP_Ex
35499 70 6c 61 69 6e 2c 20 70 50 61 72 73 65 2d 3e 69  plain, pParse->i
3549a 53 65 6c 65 63 74 49 64 2c 20 30 2c 20 30 2c 20  SelectId, 0, 0, 
3549b 7a 45 71 70 2c 20 50 34 5f 44 59 4e 41 4d 49 43  zEqp, P4_DYNAMIC
3549c 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65  .    );.  }.}.#e
3549d 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 65 78 70  lse.# define exp
3549e 6c 61 69 6e 53 69 6d 70 6c 65 43 6f 75 6e 74 28  lainSimpleCount(
3549f 61 2c 62 2c 63 29 0a 23 65 6e 64 69 66 0a 0a 2f  a,b,c).#endif../
354a0 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
354a1 64 65 20 66 6f 72 20 74 68 65 20 53 45 4c 45 43  de for the SELEC
354a2 54 20 73 74 61 74 65 6d 65 6e 74 20 67 69 76 65  T statement give
354a3 6e 20 69 6e 20 74 68 65 20 70 20 61 72 67 75 6d  n in the p argum
354a4 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ent.  .**.** The
354a5 20 72 65 73 75 6c 74 73 20 61 72 65 20 64 69 73   results are dis
354a6 74 72 69 62 75 74 65 64 20 69 6e 20 76 61 72 69  tributed in vari
354a7 6f 75 73 20 77 61 79 73 20 64 65 70 65 6e 64 69  ous ways dependi
354a8 6e 67 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 6f 6e  ng on the.** con
354a9 74 65 6e 74 73 20 6f 66 20 74 68 65 20 53 65 6c  tents of the Sel
354aa 65 63 74 44 65 73 74 20 73 74 72 75 63 74 75 72  ectDest structur
354ab 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  e pointed to by 
354ac 61 72 67 75 6d 65 6e 74 20 70 44 65 73 74 0a 2a  argument pDest.*
354ad 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  * as follows:.**
354ae 0a 2a 2a 20 20 20 20 20 70 44 65 73 74 2d 3e 65  .**     pDest->e
354af 44 65 73 74 20 20 20 20 52 65 73 75 6c 74 0a 2a  Dest    Result.*
354b0 2a 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *     ----------
354b1 2d 2d 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  --    ----------
354b2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
354b3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
354b4 2d 0a 2a 2a 20 20 20 20 20 53 52 54 5f 4f 75 74  -.**     SRT_Out
354b5 70 75 74 20 20 20 20 20 20 47 65 6e 65 72 61 74  put      Generat
354b6 65 20 61 20 72 6f 77 20 6f 66 20 6f 75 74 70 75  e a row of outpu
354b7 74 20 28 75 73 69 6e 67 20 74 68 65 20 4f 50 5f  t (using the OP_
354b8 52 65 73 75 6c 74 52 6f 77 0a 2a 2a 20 20 20 20  ResultRow.**    
354b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
354ba 20 6f 70 63 6f 64 65 29 20 66 6f 72 20 65 61 63   opcode) for eac
354bb 68 20 72 6f 77 20 69 6e 20 74 68 65 20 72 65 73  h row in the res
354bc 75 6c 74 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 20  ult set..**.**  
354bd 20 20 20 53 52 54 5f 4d 65 6d 20 20 20 20 20 20     SRT_Mem      
354be 20 20 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66     Only valid if
354bf 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   the result is a
354c0 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 2e 0a   single column..
354c1 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
354c2 20 20 20 20 20 20 20 53 74 6f 72 65 20 74 68 65         Store the
354c3 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66   first column of
354c4 20 74 68 65 20 66 69 72 73 74 20 72 65 73 75 6c   the first resul
354c5 74 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20  t row.**        
354c6 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 20               in 
354c7 72 65 67 69 73 74 65 72 20 70 44 65 73 74 2d 3e  register pDest->
354c8 69 53 44 50 61 72 6d 20 74 68 65 6e 20 61 62 61  iSDParm then aba
354c9 6e 64 6f 6e 20 74 68 65 20 72 65 73 74 0a 2a 2a  ndon the rest.**
354ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
354cb 20 20 20 20 20 6f 66 20 74 68 65 20 71 75 65 72       of the quer
354cc 79 2e 20 20 54 68 69 73 20 64 65 73 74 69 6e 61  y.  This destina
354cd 74 69 6f 6e 20 69 6d 70 6c 69 65 73 20 22 4c 49  tion implies "LI
354ce 4d 49 54 20 31 22 2e 0a 2a 2a 0a 2a 2a 20 20 20  MIT 1"..**.**   
354cf 20 20 53 52 54 5f 53 65 74 20 20 20 20 20 20 20    SRT_Set       
354d0 20 20 54 68 65 20 72 65 73 75 6c 74 20 6d 75 73    The result mus
354d1 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20 63 6f  t be a single co
354d2 6c 75 6d 6e 2e 20 20 53 74 6f 72 65 20 65 61 63  lumn.  Store eac
354d3 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  h.**            
354d4 20 20 20 20 20 20 20 20 20 72 6f 77 20 6f 66 20           row of 
354d5 72 65 73 75 6c 74 20 61 73 20 74 68 65 20 6b 65  result as the ke
354d6 79 20 69 6e 20 74 61 62 6c 65 20 70 44 65 73 74  y in table pDest
354d7 2d 3e 69 53 44 50 61 72 6d 2e 20 0a 2a 2a 20 20  ->iSDParm. .**  
354d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
354d9 20 20 20 41 70 70 6c 79 20 74 68 65 20 61 66 66     Apply the aff
354da 69 6e 69 74 79 20 70 44 65 73 74 2d 3e 61 66 66  inity pDest->aff
354db 53 64 73 74 20 62 65 66 6f 72 65 20 73 74 6f 72  Sdst before stor
354dc 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ing.**          
354dd 20 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c             resul
354de 74 73 2e 20 20 55 73 65 64 20 74 6f 20 69 6d 70  ts.  Used to imp
354df 6c 65 6d 65 6e 74 20 22 49 4e 20 28 53 45 4c 45  lement "IN (SELE
354e0 43 54 20 2e 2e 2e 29 22 2e 0a 2a 2a 0a 2a 2a 20  CT ...)"..**.** 
354e1 20 20 20 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20      SRT_Union   
354e2 20 20 20 20 53 74 6f 72 65 20 72 65 73 75 6c 74      Store result
354e3 73 20 61 73 20 61 20 6b 65 79 20 69 6e 20 61 20  s as a key in a 
354e4 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
354e5 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
354e6 20 20 20 20 20 20 20 20 69 64 65 6e 74 69 66 69          identifi
354e7 65 64 20 62 79 20 70 44 65 73 74 2d 3e 69 53 44  ed by pDest->iSD
354e8 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  Parm..**.**     
354e9 53 52 54 5f 45 78 63 65 70 74 20 20 20 20 20 20  SRT_Except      
354ea 52 65 6d 6f 76 65 20 72 65 73 75 6c 74 73 20 66  Remove results f
354eb 72 6f 6d 20 74 68 65 20 74 65 6d 70 6f 72 61 72  rom the temporar
354ec 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69  y table pDest->i
354ed 53 44 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20  SDParm..**.**   
354ee 20 20 53 52 54 5f 54 61 62 6c 65 20 20 20 20 20    SRT_Table     
354ef 20 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20    Store results 
354f0 69 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  in temporary tab
354f1 6c 65 20 70 44 65 73 74 2d 3e 69 53 44 50 61 72  le pDest->iSDPar
354f2 6d 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  m..**           
354f3 20 20 20 20 20 20 20 20 20 20 54 68 69 73 20 69            This i
354f4 73 20 6c 69 6b 65 20 53 52 54 5f 45 70 68 65 6d  s like SRT_Ephem
354f5 54 61 62 20 65 78 63 65 70 74 20 74 68 61 74 20  Tab except that 
354f6 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20  the table.**    
354f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
354f8 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 61   is assumed to a
354f9 6c 72 65 61 64 79 20 62 65 20 6f 70 65 6e 2e 0a  lready be open..
354fa 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 45 70  **.**     SRT_Ep
354fb 68 65 6d 54 61 62 20 20 20 20 43 72 65 61 74 65  hemTab    Create
354fc 20 61 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61   an temporary ta
354fd 62 6c 65 20 70 44 65 73 74 2d 3e 69 53 44 50 61  ble pDest->iSDPa
354fe 72 6d 20 61 6e 64 20 73 74 6f 72 65 0a 2a 2a 20  rm and store.** 
354ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35500 20 20 20 20 74 68 65 20 72 65 73 75 6c 74 20 74      the result t
35501 68 65 72 65 2e 20 54 68 65 20 63 75 72 73 6f 72  here. The cursor
35502 20 69 73 20 6c 65 66 74 20 6f 70 65 6e 20 61 66   is left open af
35503 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ter.**          
35504 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
35505 6e 69 6e 67 2e 20 20 54 68 69 73 20 69 73 20 6c  ning.  This is l
35506 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 65 78  ike SRT_Table ex
35507 63 65 70 74 20 74 68 61 74 0a 2a 2a 20 20 20 20  cept that.**    
35508 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35509 20 74 68 69 73 20 64 65 73 74 69 6e 61 74 69 6f   this destinatio
3550a 6e 20 75 73 65 73 20 4f 50 5f 4f 70 65 6e 45 70  n uses OP_OpenEp
3550b 68 65 6d 65 72 61 6c 20 74 6f 20 63 72 65 61 74  hemeral to creat
3550c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
3550d 20 20 20 20 20 20 20 20 20 74 68 65 20 74 61 62           the tab
3550e 6c 65 20 66 69 72 73 74 2e 0a 2a 2a 0a 2a 2a 20  le first..**.** 
3550f 20 20 20 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e      SRT_Coroutin
35510 65 20 20 20 47 65 6e 65 72 61 74 65 20 61 20 63  e   Generate a c
35511 6f 2d 72 6f 75 74 69 6e 65 20 74 68 61 74 20 72  o-routine that r
35512 65 74 75 72 6e 73 20 61 20 6e 65 77 20 72 6f 77  eturns a new row
35513 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
35514 20 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c             resul
35515 74 73 20 65 61 63 68 20 74 69 6d 65 20 69 74 20  ts each time it 
35516 69 73 20 69 6e 76 6f 6b 65 64 2e 20 20 54 68 65  is invoked.  The
35517 20 65 6e 74 72 79 20 70 6f 69 6e 74 0a 2a 2a 20   entry point.** 
35518 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35519 20 20 20 20 6f 66 20 74 68 65 20 63 6f 2d 72 6f      of the co-ro
3551a 75 74 69 6e 65 20 69 73 20 73 74 6f 72 65 64 20  utine is stored 
3551b 69 6e 20 72 65 67 69 73 74 65 72 20 70 44 65 73  in register pDes
3551c 74 2d 3e 69 53 44 50 61 72 6d 2e 0a 2a 2a 0a 2a  t->iSDParm..**.*
3551d 2a 20 20 20 20 20 53 52 54 5f 45 78 69 73 74 73  *     SRT_Exists
3551e 20 20 20 20 20 20 53 74 6f 72 65 20 61 20 31 20        Store a 1 
3551f 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 70  in memory cell p
35520 44 65 73 74 2d 3e 69 53 44 50 61 72 6d 20 69 66  Dest->iSDParm if
35521 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 20   the result.**  
35522 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35523 20 20 20 73 65 74 20 69 73 20 6e 6f 74 20 65 6d     set is not em
35524 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  pty..**.**     S
35525 52 54 5f 44 69 73 63 61 72 64 20 20 20 20 20 54  RT_Discard     T
35526 68 72 6f 77 20 74 68 65 20 72 65 73 75 6c 74 73  hrow the results
35527 20 61 77 61 79 2e 20 20 54 68 69 73 20 69 73 20   away.  This is 
35528 75 73 65 64 20 62 79 20 53 45 4c 45 43 54 0a 2a  used by SELECT.*
35529 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
3552a 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74 73        statements
3552b 20 77 69 74 68 69 6e 20 74 72 69 67 67 65 72 73   within triggers
3552c 20 77 68 6f 73 65 20 6f 6e 6c 79 20 70 75 72 70   whose only purp
3552d 6f 73 65 20 69 73 0a 2a 2a 20 20 20 20 20 20 20  ose is.**       
3552e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
3552f 65 20 73 69 64 65 2d 65 66 66 65 63 74 73 20 6f  e side-effects o
35530 66 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a  f functions..**.
35531 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
35532 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
35533 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49  er of errors.  I
35534 66 20 61 6e 79 20 65 72 72 6f 72 73 20 61 72 65  f any errors are
35535 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c  .** encountered,
35536 20 74 68 65 6e 20 61 6e 20 61 70 70 72 6f 70 72   then an appropr
35537 69 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61  iate error messa
35538 67 65 20 69 73 20 6c 65 66 74 20 69 6e 0a 2a 2a  ge is left in.**
35539 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
3553a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
3553b 74 69 6e 65 20 64 6f 65 73 20 4e 4f 54 20 66 72  tine does NOT fr
3553c 65 65 20 74 68 65 20 53 65 6c 65 63 74 20 73 74  ee the Select st
3553d 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20 69  ructure passed i
3553e 6e 2e 20 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 69  n.  The.** calli
3553f 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6e 65 65 64  ng function need
35540 73 20 74 6f 20 64 6f 20 74 68 61 74 2e 0a 2a 2f  s to do that..*/
35541 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
35542 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63  int sqlite3Selec
35543 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
35544 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  se,         /* T
35545 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78  he parser contex
35546 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  t */.  Select *p
35547 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
35548 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   The SELECT stat
35549 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65  ement being code
3554a 64 2e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65  d. */.  SelectDe
3554b 73 74 20 2a 70 44 65 73 74 20 20 20 20 20 20 2f  st *pDest      /
3554c 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74  * What to do wit
3554d 68 20 74 68 65 20 71 75 65 72 79 20 72 65 73 75  h the query resu
3554e 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  lts */.){.  int 
3554f 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20  i, j;           
35550 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
35551 65 72 73 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e  ers */.  WhereIn
35552 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20  fo *pWInfo;     
35553 2f 2a 20 52 65 74 75 72 6e 20 66 72 6f 6d 20 73  /* Return from s
35554 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
35555 28 29 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b  () */.  Vdbe *v;
35556 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35557 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 6d 61  * The virtual ma
35558 63 68 69 6e 65 20 75 6e 64 65 72 20 63 6f 6e 73  chine under cons
35559 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  truction */.  in
3555a 74 20 69 73 41 67 67 3b 20 20 20 20 20 20 20 20  t isAgg;        
3555b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72       /* True for
3555c 20 73 65 6c 65 63 74 20 6c 69 73 74 73 20 6c 69   select lists li
3555d 6b 65 20 22 63 6f 75 6e 74 28 2a 29 22 20 2a 2f  ke "count(*)" */
3555e 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
3555f 69 73 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73  ist;      /* Lis
35560 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20  t of columns to 
35561 65 78 74 72 61 63 74 2e 20 2a 2f 0a 20 20 53 72  extract. */.  Sr
35562 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 3b  cList *pTabList;
35563 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
35564 74 61 62 6c 65 73 20 74 6f 20 73 65 6c 65 63 74  tables to select
35565 20 66 72 6f 6d 20 2a 2f 0a 20 20 45 78 70 72 20   from */.  Expr 
35566 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 20  *pWhere;        
35567 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
35568 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e  lause.  May be N
35569 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ULL */.  ExprLis
3556a 74 20 2a 70 4f 72 64 65 72 42 79 3b 20 20 20 20  t *pOrderBy;    
3556b 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
3556c 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20  clause.  May be 
3556d 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 4c 69  NULL */.  ExprLi
3556e 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20  st *pGroupBy;   
3556f 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59   /* The GROUP BY
35570 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65   clause.  May be
35571 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 20   NULL */.  Expr 
35572 2a 70 48 61 76 69 6e 67 3b 20 20 20 20 20 20 20  *pHaving;       
35573 20 20 2f 2a 20 54 68 65 20 48 41 56 49 4e 47 20    /* The HAVING 
35574 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20  clause.  May be 
35575 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 72 63  NULL */.  int rc
35576 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20   = 1;           
35577 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74   /* Value to ret
35578 75 72 6e 20 66 72 6f 6d 20 74 68 69 73 20 66 75  urn from this fu
35579 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nction */.  int 
3557a 61 64 64 72 53 6f 72 74 49 6e 64 65 78 3b 20 20  addrSortIndex;  
3557b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
3557c 20 61 6e 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d   an OP_OpenEphem
3557d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e  eral instruction
3557e 20 2a 2f 0a 20 20 44 69 73 74 69 6e 63 74 43 74   */.  DistinctCt
3557f 78 20 73 44 69 73 74 69 6e 63 74 3b 20 2f 2a 20  x sDistinct; /* 
35580 49 6e 66 6f 20 6f 6e 20 68 6f 77 20 74 6f 20 63  Info on how to c
35581 6f 64 65 20 74 68 65 20 44 49 53 54 49 4e 43 54  ode the DISTINCT
35582 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 20 20 41 67   keyword */.  Ag
35583 67 49 6e 66 6f 20 73 41 67 67 49 6e 66 6f 3b 20  gInfo sAggInfo; 
35584 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74       /* Informat
35585 69 6f 6e 20 75 73 65 64 20 62 79 20 61 67 67 72  ion used by aggr
35586 65 67 61 74 65 20 71 75 65 72 69 65 73 20 2a 2f  egate queries */
35587 0a 20 20 69 6e 74 20 69 45 6e 64 3b 20 20 20 20  .  int iEnd;    
35588 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
35589 72 65 73 73 20 6f 66 20 74 68 65 20 65 6e 64 20  ress of the end 
3558a 6f 66 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a  of the query */.
3558b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
3558c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3558d 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3558e 69 6f 6e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ion */..#ifndef 
3558f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
35590 41 49 4e 0a 20 20 69 6e 74 20 69 52 65 73 74 6f  AIN.  int iResto
35591 72 65 53 65 6c 65 63 74 49 64 20 3d 20 70 50 61  reSelectId = pPa
35592 72 73 65 2d 3e 69 53 65 6c 65 63 74 49 64 3b 0a  rse->iSelectId;.
35593 20 20 70 50 61 72 73 65 2d 3e 69 53 65 6c 65 63    pParse->iSelec
35594 74 49 64 20 3d 20 70 50 61 72 73 65 2d 3e 69 4e  tId = pParse->iN
35595 65 78 74 53 65 6c 65 63 74 49 64 2b 2b 3b 0a 23  extSelectId++;.#
35596 65 6e 64 69 66 0a 0a 20 20 64 62 20 3d 20 70 50  endif..  db = pP
35597 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
35598 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  p==0 || db->mall
35599 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 72  ocFailed || pPar
3559a 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20  se->nErr ){.    
3559b 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
3559c 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
3559d 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
3559e 49 54 45 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30  ITE_SELECT, 0, 0
3559f 2c 20 30 29 20 29 20 72 65 74 75 72 6e 20 31 3b  , 0) ) return 1;
355a0 0a 20 20 6d 65 6d 73 65 74 28 26 73 41 67 67 49  .  memset(&sAggI
355a1 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  nfo, 0, sizeof(s
355a2 41 67 67 49 6e 66 6f 29 29 3b 0a 0a 20 20 69 66  AggInfo));..  if
355a3 28 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72  ( IgnorableOrder
355a4 62 79 28 70 44 65 73 74 29 20 29 7b 0a 20 20 20  by(pDest) ){.   
355a5 20 61 73 73 65 72 74 28 70 44 65 73 74 2d 3e 65   assert(pDest->e
355a6 44 65 73 74 3d 3d 53 52 54 5f 45 78 69 73 74 73  Dest==SRT_Exists
355a7 20 7c 7c 20 70 44 65 73 74 2d 3e 65 44 65 73 74   || pDest->eDest
355a8 3d 3d 53 52 54 5f 55 6e 69 6f 6e 20 7c 7c 20 0a  ==SRT_Union || .
355a9 20 20 20 20 20 20 20 20 20 20 20 70 44 65 73 74             pDest
355aa 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 78 63  ->eDest==SRT_Exc
355ab 65 70 74 20 7c 7c 20 70 44 65 73 74 2d 3e 65 44  ept || pDest->eD
355ac 65 73 74 3d 3d 53 52 54 5f 44 69 73 63 61 72 64  est==SRT_Discard
355ad 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 4f 52 44  );.    /* If ORD
355ae 45 52 20 42 59 20 6d 61 6b 65 73 20 6e 6f 20 64  ER BY makes no d
355af 69 66 66 65 72 65 6e 63 65 20 69 6e 20 74 68 65  ifference in the
355b0 20 6f 75 74 70 75 74 20 74 68 65 6e 20 6e 65 69   output then nei
355b1 74 68 65 72 20 64 6f 65 73 0a 20 20 20 20 2a 2a  ther does.    **
355b2 20 44 49 53 54 49 4e 43 54 20 73 6f 20 69 74 20   DISTINCT so it 
355b3 63 61 6e 20 62 65 20 72 65 6d 6f 76 65 64 20 74  can be removed t
355b4 6f 6f 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  oo. */.    sqlit
355b5 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
355b6 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79  (db, p->pOrderBy
355b7 29 3b 0a 20 20 20 20 70 2d 3e 70 4f 72 64 65 72  );.    p->pOrder
355b8 42 79 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 73  By = 0;.    p->s
355b9 65 6c 46 6c 61 67 73 20 26 3d 20 7e 53 46 5f 44  elFlags &= ~SF_D
355ba 69 73 74 69 6e 63 74 3b 0a 20 20 7d 0a 20 20 73  istinct;.  }.  s
355bb 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70  qlite3SelectPrep
355bc 28 70 50 61 72 73 65 2c 20 70 2c 20 30 29 3b 0a  (pParse, p, 0);.
355bd 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e    pOrderBy = p->
355be 70 4f 72 64 65 72 42 79 3b 0a 20 20 70 54 61 62  pOrderBy;.  pTab
355bf 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a  List = p->pSrc;.
355c0 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45    pEList = p->pE
355c1 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 50 61 72  List;.  if( pPar
355c2 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
355c3 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
355c4 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f      goto select_
355c5 65 6e 64 3b 0a 20 20 7d 0a 20 20 69 73 41 67 67  end;.  }.  isAgg
355c6 20 3d 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20   = (p->selFlags 
355c7 26 20 53 46 5f 41 67 67 72 65 67 61 74 65 29 21  & SF_Aggregate)!
355c8 3d 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45  =0;.  assert( pE
355c9 4c 69 73 74 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a  List!=0 );..  /*
355ca 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e   Begin generatin
355cb 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76  g code..  */.  v
355cc 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
355cd 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
355ce 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c   v==0 ) goto sel
355cf 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20 49  ect_end;..  /* I
355d0 66 20 77 72 69 74 69 6e 67 20 74 6f 20 6d 65 6d  f writing to mem
355d1 6f 72 79 20 6f 72 20 67 65 6e 65 72 61 74 69 6e  ory or generatin
355d2 67 20 61 20 73 65 74 0a 20 20 2a 2a 20 6f 6e 6c  g a set.  ** onl
355d3 79 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d  y a single colum
355d4 6e 20 6d 61 79 20 62 65 20 6f 75 74 70 75 74 2e  n may be output.
355d5 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
355d6 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
355d7 52 59 0a 20 20 69 66 28 20 63 68 65 63 6b 46 6f  RY.  if( checkFo
355d8 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65  rMultiColumnSele
355d9 63 74 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20  ctError(pParse, 
355da 70 44 65 73 74 2c 20 70 45 4c 69 73 74 2d 3e 6e  pDest, pEList->n
355db 45 78 70 72 29 20 29 7b 0a 20 20 20 20 67 6f 74  Expr) ){.    got
355dc 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  o select_end;.  
355dd 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47  }.#endif..  /* G
355de 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
355df 20 61 6c 6c 20 73 75 62 2d 71 75 65 72 69 65 73   all sub-queries
355e0 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
355e1 75 73 65 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65  use.  */.#if !de
355e2 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
355e3 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21  T_SUBQUERY) || !
355e4 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
355e5 4d 49 54 5f 56 49 45 57 29 0a 20 20 66 6f 72 28  MIT_VIEW).  for(
355e6 69 3d 30 3b 20 21 70 2d 3e 70 50 72 69 6f 72 20  i=0; !p->pPrior 
355e7 26 26 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  && i<pTabList->n
355e8 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  Src; i++){.    s
355e9 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
355ea 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 54 61  em *pItem = &pTa
355eb 62 4c 69 73 74 2d 3e 61 5b 69 5d 3b 0a 20 20 20  bList->a[i];.   
355ec 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
355ed 3b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53  ;.    Select *pS
355ee 75 62 20 3d 20 70 49 74 65 6d 2d 3e 70 53 65 6c  ub = pItem->pSel
355ef 65 63 74 3b 0a 20 20 20 20 69 6e 74 20 69 73 41  ect;.    int isA
355f0 67 67 53 75 62 3b 0a 0a 20 20 20 20 69 66 28 20  ggSub;..    if( 
355f1 70 53 75 62 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  pSub==0 ) contin
355f2 75 65 3b 0a 0a 20 20 20 20 2f 2a 20 53 6f 6d 65  ue;..    /* Some
355f3 74 69 6d 65 73 20 74 68 65 20 63 6f 64 65 20 66  times the code f
355f4 6f 72 20 61 20 73 75 62 71 75 65 72 79 20 77 69  or a subquery wi
355f5 6c 6c 20 62 65 20 67 65 6e 65 72 61 74 65 64 20  ll be generated 
355f6 6d 6f 72 65 20 74 68 61 6e 0a 20 20 20 20 2a 2a  more than.    **
355f7 20 6f 6e 63 65 2c 20 69 66 20 74 68 65 20 73 75   once, if the su
355f8 62 71 75 65 72 79 20 69 73 20 70 61 72 74 20 6f  bquery is part o
355f9 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
355fa 73 65 20 69 6e 20 61 20 4c 45 46 54 20 4a 4f 49  se in a LEFT JOI
355fb 4e 2c 0a 20 20 20 20 2a 2a 20 66 6f 72 20 65 78  N,.    ** for ex
355fc 61 6d 70 6c 65 2e 20 20 49 6e 20 74 68 61 74 20  ample.  In that 
355fd 63 61 73 65 2c 20 64 6f 20 6e 6f 74 20 72 65 67  case, do not reg
355fe 65 6e 65 72 61 74 65 20 74 68 65 20 63 6f 64 65  enerate the code
355ff 20 74 6f 20 6d 61 6e 69 66 65 73 74 0a 20 20 20   to manifest.   
35600 20 2a 2a 20 61 20 76 69 65 77 20 6f 72 20 74 68   ** a view or th
35601 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20 74 6f 20  e co-routine to 
35602 69 6d 70 6c 65 6d 65 6e 74 20 61 20 76 69 65 77  implement a view
35603 2e 20 20 54 68 65 20 66 69 72 73 74 20 69 6e 73  .  The first ins
35604 74 61 6e 63 65 0a 20 20 20 20 2a 2a 20 69 73 20  tance.    ** is 
35605 73 75 66 66 69 63 69 65 6e 74 2c 20 74 68 6f 75  sufficient, thou
35606 67 68 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e  gh the subroutin
35607 65 20 74 6f 20 6d 61 6e 69 66 65 73 74 20 74 68  e to manifest th
35608 65 20 76 69 65 77 20 64 6f 65 73 20 6e 65 65 64  e view does need
35609 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e  .    ** to be in
3560a 76 6f 6b 65 64 20 61 67 61 69 6e 2e 20 2a 2f 0a  voked again. */.
3560b 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 61      if( pItem->a
3560c 64 64 72 46 69 6c 6c 53 75 62 20 29 7b 0a 20 20  ddrFillSub ){.  
3560d 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 76      if( pItem->v
3560e 69 61 43 6f 72 6f 75 74 69 6e 65 3d 3d 30 20 29  iaCoroutine==0 )
3560f 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
35610 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
35611 50 5f 47 6f 73 75 62 2c 20 70 49 74 65 6d 2d 3e  P_Gosub, pItem->
35612 72 65 67 52 65 74 75 72 6e 2c 20 70 49 74 65 6d  regReturn, pItem
35613 2d 3e 61 64 64 72 46 69 6c 6c 53 75 62 29 3b 0a  ->addrFillSub);.
35614 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 6f        }.      co
35615 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20  ntinue;.    }.. 
35616 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20     /* Increment 
35617 50 61 72 73 65 2e 6e 48 65 69 67 68 74 20 62 79  Parse.nHeight by
35618 20 74 68 65 20 68 65 69 67 68 74 20 6f 66 20 74   the height of t
35619 68 65 20 6c 61 72 67 65 73 74 20 65 78 70 72 65  he largest expre
3561a 73 73 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 72 65  ssion.    ** tre
3561b 65 20 72 65 66 65 72 65 64 20 74 6f 20 62 79 20  e refered to by 
3561c 74 68 69 73 2c 20 74 68 65 20 70 61 72 65 6e 74  this, the parent
3561d 20 73 65 6c 65 63 74 2e 20 54 68 65 20 63 68 69   select. The chi
3561e 6c 64 20 73 65 6c 65 63 74 0a 20 20 20 20 2a 2a  ld select.    **
3561f 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 65 78 70   may contain exp
35620 72 65 73 73 69 6f 6e 20 74 72 65 65 73 20 6f 66  ression trees of
35621 20 61 74 20 6d 6f 73 74 0a 20 20 20 20 2a 2a 20   at most.    ** 
35622 28 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52  (SQLITE_MAX_EXPR
35623 5f 44 45 50 54 48 2d 50 61 72 73 65 2e 6e 48 65  _DEPTH-Parse.nHe
35624 69 67 68 74 29 20 68 65 69 67 68 74 2e 20 54 68  ight) height. Th
35625 69 73 20 69 73 20 61 20 62 69 74 0a 20 20 20 20  is is a bit.    
35626 2a 2a 20 6d 6f 72 65 20 63 6f 6e 73 65 72 76 61  ** more conserva
35627 74 69 76 65 20 74 68 61 6e 20 6e 65 63 65 73 73  tive than necess
35628 61 72 79 2c 20 62 75 74 20 6d 75 63 68 20 65 61  ary, but much ea
35629 73 69 65 72 20 74 68 61 6e 20 65 6e 66 6f 72 63  sier than enforc
3562a 69 6e 67 0a 20 20 20 20 2a 2a 20 61 6e 20 65 78  ing.    ** an ex
3562b 61 63 74 20 6c 69 6d 69 74 2e 0a 20 20 20 20 2a  act limit..    *
3562c 2f 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48  /.    pParse->nH
3562d 65 69 67 68 74 20 2b 3d 20 73 71 6c 69 74 65 33  eight += sqlite3
3562e 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 68 74  SelectExprHeight
3562f 28 70 29 3b 0a 0a 20 20 20 20 69 73 41 67 67 53  (p);..    isAggS
35630 75 62 20 3d 20 28 70 53 75 62 2d 3e 73 65 6c 46  ub = (pSub->selF
35631 6c 61 67 73 20 26 20 53 46 5f 41 67 67 72 65 67  lags & SF_Aggreg
35632 61 74 65 29 21 3d 30 3b 0a 20 20 20 20 69 66 28  ate)!=0;.    if(
35633 20 66 6c 61 74 74 65 6e 53 75 62 71 75 65 72 79   flattenSubquery
35634 28 70 50 61 72 73 65 2c 20 70 2c 20 69 2c 20 69  (pParse, p, i, i
35635 73 41 67 67 2c 20 69 73 41 67 67 53 75 62 29 20  sAgg, isAggSub) 
35636 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73  ){.      /* This
35637 20 73 75 62 71 75 65 72 79 20 63 61 6e 20 62 65   subquery can be
35638 20 61 62 73 6f 72 62 65 64 20 69 6e 74 6f 20 69   absorbed into i
35639 74 73 20 70 61 72 65 6e 74 2e 20 2a 2f 0a 20 20  ts parent. */.  
3563a 20 20 20 20 69 66 28 20 69 73 41 67 67 53 75 62      if( isAggSub
3563b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 73 41 67   ){.        isAg
3563c 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70  g = 1;.        p
3563d 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46  ->selFlags |= SF
3563e 5f 41 67 67 72 65 67 61 74 65 3b 0a 20 20 20 20  _Aggregate;.    
3563f 20 20 7d 0a 20 20 20 20 20 20 69 20 3d 20 2d 31    }.      i = -1
35640 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
35641 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d  pTabList->nSrc==
35642 31 20 26 26 20 28 70 2d 3e 73 65 6c 46 6c 61 67  1 && (p->selFlag
35643 73 20 26 20 53 46 5f 4d 61 74 65 72 69 61 6c 69  s & SF_Materiali
35644 7a 65 29 3d 3d 30 0a 20 20 20 20 20 20 26 26 20  ze)==0.      && 
35645 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 45 6e 61 62  OptimizationEnab
35646 6c 65 64 28 64 62 2c 20 53 51 4c 49 54 45 5f 53  led(db, SQLITE_S
35647 75 62 71 43 6f 72 6f 75 74 69 6e 65 29 0a 20 20  ubqCoroutine).  
35648 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6d    ){.      /* Im
35649 70 6c 65 6d 65 6e 74 20 61 20 63 6f 2d 72 6f 75  plement a co-rou
3564a 74 69 6e 65 20 74 68 61 74 20 77 69 6c 6c 20 72  tine that will r
3564b 65 74 75 72 6e 20 61 20 73 69 6e 67 6c 65 20 72  eturn a single r
3564c 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ow of the result
3564d 0a 20 20 20 20 20 20 2a 2a 20 73 65 74 20 6f 6e  .      ** set on
3564e 20 65 61 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e   each invocation
3564f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
35650 20 69 6e 74 20 61 64 64 72 54 6f 70 3b 0a 20 20   int addrTop;.  
35651 20 20 20 20 69 6e 74 20 61 64 64 72 45 6f 66 3b      int addrEof;
35652 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 72 65  .      pItem->re
35653 67 52 65 74 75 72 6e 20 3d 20 2b 2b 70 50 61 72  gReturn = ++pPar
35654 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20  se->nMem;.      
35655 61 64 64 72 45 6f 66 20 3d 20 2b 2b 70 50 61 72  addrEof = ++pPar
35656 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20  se->nMem;.      
35657 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
35658 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20  0(v, OP_Goto);. 
35659 20 20 20 20 20 61 64 64 72 54 6f 70 20 3d 20 73       addrTop = s
3565a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
3565b 28 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64  (v, OP_OpenPseud
3565c 6f 2c 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f  o, pItem->iCurso
3565d 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  r);.      sqlite
3565e 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
3565f 20 31 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43   1);.      VdbeC
35660 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 63 6f 72 6f  omment((v, "coro
35661 75 74 69 6e 65 20 66 6f 72 20 25 73 22 2c 20 70  utine for %s", p
35662 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d  Item->pTab->zNam
35663 65 29 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d  e));.      pItem
35664 2d 3e 61 64 64 72 46 69 6c 6c 53 75 62 20 3d 20  ->addrFillSub = 
35665 61 64 64 72 54 6f 70 3b 0a 20 20 20 20 20 20 73  addrTop;.      s
35666 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
35667 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
35668 30 2c 20 61 64 64 72 45 6f 66 29 3b 0a 20 20 20  0, addrEof);.   
35669 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
3566a 61 6e 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20  angeP5(v, 1);.  
3566b 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
3566c 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c  tDestInit(&dest,
3566d 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 2c 20   SRT_Coroutine, 
3566e 70 49 74 65 6d 2d 3e 72 65 67 52 65 74 75 72 6e  pItem->regReturn
3566f 29 3b 0a 20 20 20 20 20 20 65 78 70 6c 61 69 6e  );.      explain
35670 53 65 74 49 6e 74 65 67 65 72 28 70 49 74 65 6d  SetInteger(pItem
35671 2d 3e 69 53 65 6c 65 63 74 49 64 2c 20 28 75 38  ->iSelectId, (u8
35672 29 70 50 61 72 73 65 2d 3e 69 4e 65 78 74 53 65  )pParse->iNextSe
35673 6c 65 63 74 49 64 29 3b 0a 20 20 20 20 20 20 73  lectId);.      s
35674 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
35675 72 73 65 2c 20 70 53 75 62 2c 20 26 64 65 73 74  rse, pSub, &dest
35676 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e  );.      pItem->
35677 70 54 61 62 2d 3e 6e 52 6f 77 45 73 74 20 3d 20  pTab->nRowEst = 
35678 28 75 6e 73 69 67 6e 65 64 29 70 53 75 62 2d 3e  (unsigned)pSub->
35679 6e 53 65 6c 65 63 74 52 6f 77 3b 0a 20 20 20 20  nSelectRow;.    
3567a 20 20 70 49 74 65 6d 2d 3e 76 69 61 43 6f 72 6f    pItem->viaCoro
3567b 75 74 69 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20  utine = 1;.     
3567c 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
3567d 67 65 50 32 28 76 2c 20 61 64 64 72 54 6f 70 2c  geP2(v, addrTop,
3567e 20 64 65 73 74 2e 69 53 64 73 74 29 3b 0a 20 20   dest.iSdst);.  
3567f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
35680 68 61 6e 67 65 50 33 28 76 2c 20 61 64 64 72 54  hangeP3(v, addrT
35681 6f 70 2c 20 64 65 73 74 2e 6e 53 64 73 74 29 3b  op, dest.nSdst);
35682 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
35683 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
35684 6e 74 65 67 65 72 2c 20 31 2c 20 61 64 64 72 45  nteger, 1, addrE
35685 6f 66 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  of);.      sqlit
35686 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
35687 4f 50 5f 59 69 65 6c 64 2c 20 70 49 74 65 6d 2d  OP_Yield, pItem-
35688 3e 72 65 67 52 65 74 75 72 6e 29 3b 0a 20 20 20  >regReturn);.   
35689 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
3568a 76 2c 20 22 65 6e 64 20 25 73 22 2c 20 70 49 74  v, "end %s", pIt
3568b 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  em->pTab->zName)
3568c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
3568d 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
3568e 61 64 64 72 54 6f 70 2d 31 29 3b 0a 20 20 20 20  addrTop-1);.    
3568f 20 20 73 71 6c 69 74 65 33 43 6c 65 61 72 54 65    sqlite3ClearTe
35690 6d 70 52 65 67 43 61 63 68 65 28 70 50 61 72 73  mpRegCache(pPars
35691 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
35692 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
35693 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68   a subroutine th
35694 61 74 20 77 69 6c 6c 20 66 69 6c 6c 20 61 6e 20  at will fill an 
35695 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20  ephemeral table 
35696 77 69 74 68 0a 20 20 20 20 20 20 2a 2a 20 74 68  with.      ** th
35697 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69  e content of thi
35698 73 20 73 75 62 71 75 65 72 79 2e 20 20 70 49 74  s subquery.  pIt
35699 65 6d 2d 3e 61 64 64 72 46 69 6c 6c 53 75 62 20  em->addrFillSub 
3569a 77 69 6c 6c 20 70 6f 69 6e 74 0a 20 20 20 20 20  will point.     
3569b 20 2a 2a 20 74 6f 20 74 68 65 20 61 64 64 72 65   ** to the addre
3569c 73 73 20 6f 66 20 74 68 65 20 67 65 6e 65 72 61  ss of the genera
3569d 74 65 64 20 73 75 62 72 6f 75 74 69 6e 65 2e 20  ted subroutine. 
3569e 20 70 49 74 65 6d 2d 3e 72 65 67 52 65 74 75 72   pItem->regRetur
3569f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 61 20  n.      ** is a 
356a0 72 65 67 69 73 74 65 72 20 61 6c 6c 6f 63 61 74  register allocat
356a1 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 73  ed to hold the s
356a2 75 62 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  ubroutine return
356a3 20 61 64 64 72 65 73 73 0a 20 20 20 20 20 20 2a   address.      *
356a4 2f 0a 20 20 20 20 20 20 69 6e 74 20 74 6f 70 41  /.      int topA
356a5 64 64 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 6f  ddr;.      int o
356a6 6e 63 65 41 64 64 72 20 3d 20 30 3b 0a 20 20 20  nceAddr = 0;.   
356a7 20 20 20 69 6e 74 20 72 65 74 41 64 64 72 3b 0a     int retAddr;.
356a8 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
356a9 74 65 6d 2d 3e 61 64 64 72 46 69 6c 6c 53 75 62  tem->addrFillSub
356aa 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 49 74  ==0 );.      pIt
356ab 65 6d 2d 3e 72 65 67 52 65 74 75 72 6e 20 3d 20  em->regReturn = 
356ac 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
356ad 20 20 20 20 20 20 74 6f 70 41 64 64 72 20 3d 20        topAddr = 
356ae 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
356af 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
356b0 20 30 2c 20 70 49 74 65 6d 2d 3e 72 65 67 52 65   0, pItem->regRe
356b1 74 75 72 6e 29 3b 0a 20 20 20 20 20 20 70 49 74  turn);.      pIt
356b2 65 6d 2d 3e 61 64 64 72 46 69 6c 6c 53 75 62 20  em->addrFillSub 
356b3 3d 20 74 6f 70 41 64 64 72 2b 31 3b 0a 20 20 20  = topAddr+1;.   
356b4 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65     VdbeNoopComme
356b5 6e 74 28 28 76 2c 20 22 6d 61 74 65 72 69 61 6c  nt((v, "material
356b6 69 7a 65 20 25 73 22 2c 20 70 49 74 65 6d 2d 3e  ize %s", pItem->
356b7 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20  pTab->zName));. 
356b8 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e       if( pItem->
356b9 69 73 43 6f 72 72 65 6c 61 74 65 64 3d 3d 30 20  isCorrelated==0 
356ba 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ){.        /* If
356bb 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 73   the subquery is
356bc 20 6e 6f 20 63 6f 72 72 65 6c 61 74 65 64 20 61   no correlated a
356bd 6e 64 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74  nd if we are not
356be 20 69 6e 73 69 64 65 20 6f 66 0a 20 20 20 20 20   inside of.     
356bf 20 20 20 2a 2a 20 61 20 74 72 69 67 67 65 72 2c     ** a trigger,
356c0 20 74 68 65 6e 20 77 65 20 6f 6e 6c 79 20 6e 65   then we only ne
356c1 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68  ed to compute th
356c2 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73  e value of the s
356c3 75 62 71 75 65 72 79 0a 20 20 20 20 20 20 20 20  ubquery.        
356c4 2a 2a 20 6f 6e 63 65 2e 20 2a 2f 0a 20 20 20 20  ** once. */.    
356c5 20 20 20 20 6f 6e 63 65 41 64 64 72 20 3d 20 73      onceAddr = s
356c6 71 6c 69 74 65 33 43 6f 64 65 4f 6e 63 65 28 70  qlite3CodeOnce(p
356c7 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a  Parse);.      }.
356c8 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
356c9 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73  ectDestInit(&des
356ca 74 2c 20 53 52 54 5f 45 70 68 65 6d 54 61 62 2c  t, SRT_EphemTab,
356cb 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 29   pItem->iCursor)
356cc 3b 0a 20 20 20 20 20 20 65 78 70 6c 61 69 6e 53  ;.      explainS
356cd 65 74 49 6e 74 65 67 65 72 28 70 49 74 65 6d 2d  etInteger(pItem-
356ce 3e 69 53 65 6c 65 63 74 49 64 2c 20 28 75 38 29  >iSelectId, (u8)
356cf 70 50 61 72 73 65 2d 3e 69 4e 65 78 74 53 65 6c  pParse->iNextSel
356d0 65 63 74 49 64 29 3b 0a 20 20 20 20 20 20 73 71  ectId);.      sq
356d1 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
356d2 73 65 2c 20 70 53 75 62 2c 20 26 64 65 73 74 29  se, pSub, &dest)
356d3 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70  ;.      pItem->p
356d4 54 61 62 2d 3e 6e 52 6f 77 45 73 74 20 3d 20 28  Tab->nRowEst = (
356d5 75 6e 73 69 67 6e 65 64 29 70 53 75 62 2d 3e 6e  unsigned)pSub->n
356d6 53 65 6c 65 63 74 52 6f 77 3b 0a 20 20 20 20 20  SelectRow;.     
356d7 20 69 66 28 20 6f 6e 63 65 41 64 64 72 20 29 20   if( onceAddr ) 
356d8 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
356d9 65 72 65 28 76 2c 20 6f 6e 63 65 41 64 64 72 29  ere(v, onceAddr)
356da 3b 0a 20 20 20 20 20 20 72 65 74 41 64 64 72 20  ;.      retAddr 
356db 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
356dc 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e  Op1(v, OP_Return
356dd 2c 20 70 49 74 65 6d 2d 3e 72 65 67 52 65 74 75  , pItem->regRetu
356de 72 6e 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  rn);.      VdbeC
356df 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64 20  omment((v, "end 
356e0 25 73 22 2c 20 70 49 74 65 6d 2d 3e 70 54 61 62  %s", pItem->pTab
356e1 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20  ->zName));.     
356e2 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
356e3 67 65 50 31 28 76 2c 20 74 6f 70 41 64 64 72 2c  geP1(v, topAddr,
356e4 20 72 65 74 41 64 64 72 29 3b 0a 20 20 20 20 20   retAddr);.     
356e5 20 73 71 6c 69 74 65 33 43 6c 65 61 72 54 65 6d   sqlite3ClearTem
356e6 70 52 65 67 43 61 63 68 65 28 70 50 61 72 73 65  pRegCache(pParse
356e7 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
356e8 20 2f 2a 70 50 61 72 73 65 2d 3e 6e 45 72 72 20   /*pParse->nErr 
356e9 7c 7c 2a 2f 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ||*/ db->mallocF
356ea 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 67  ailed ){.      g
356eb 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  oto select_end;.
356ec 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65      }.    pParse
356ed 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 73 71 6c  ->nHeight -= sql
356ee 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65  ite3SelectExprHe
356ef 69 67 68 74 28 70 29 3b 0a 20 20 20 20 70 54 61  ight(p);.    pTa
356f0 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b  bList = p->pSrc;
356f1 0a 20 20 20 20 69 66 28 20 21 49 67 6e 6f 72 61  .    if( !Ignora
356f2 62 6c 65 4f 72 64 65 72 62 79 28 70 44 65 73 74  bleOrderby(pDest
356f3 29 20 29 7b 0a 20 20 20 20 20 20 70 4f 72 64 65  ) ){.      pOrde
356f4 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42  rBy = p->pOrderB
356f5 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  y;.    }.  }.  p
356f6 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73  EList = p->pELis
356f7 74 3b 0a 23 65 6e 64 69 66 0a 20 20 70 57 68 65  t;.#endif.  pWhe
356f8 72 65 20 3d 20 70 2d 3e 70 57 68 65 72 65 3b 0a  re = p->pWhere;.
356f9 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e    pGroupBy = p->
356fa 70 47 72 6f 75 70 42 79 3b 0a 20 20 70 48 61 76  pGroupBy;.  pHav
356fb 69 6e 67 20 3d 20 70 2d 3e 70 48 61 76 69 6e 67  ing = p->pHaving
356fc 3b 0a 20 20 73 44 69 73 74 69 6e 63 74 2e 69 73  ;.  sDistinct.is
356fd 54 6e 63 74 20 3d 20 28 70 2d 3e 73 65 6c 46 6c  Tnct = (p->selFl
356fe 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63  ags & SF_Distinc
356ff 74 29 21 3d 30 3b 0a 0a 23 69 66 6e 64 65 66 20  t)!=0;..#ifndef 
35700 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50  SQLITE_OMIT_COMP
35701 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 20 20 2f 2a  OUND_SELECT.  /*
35702 20 49 66 20 74 68 65 72 65 20 69 73 20 61 72 65   If there is are
35703 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 71   a sequence of q
35704 75 65 72 69 65 73 2c 20 64 6f 20 74 68 65 20 65  ueries, do the e
35705 61 72 6c 69 65 72 20 6f 6e 65 73 20 66 69 72 73  arlier ones firs
35706 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  t..  */.  if( p-
35707 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 69  >pPrior ){.    i
35708 66 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74  f( p->pRightmost
35709 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 53 65 6c  ==0 ){.      Sel
3570a 65 63 74 20 2a 70 4c 6f 6f 70 2c 20 2a 70 52 69  ect *pLoop, *pRi
3570b 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  ght = 0;.      i
3570c 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20  nt cnt = 0;.    
3570d 20 20 69 6e 74 20 6d 78 53 65 6c 65 63 74 3b 0a    int mxSelect;.
3570e 20 20 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d        for(pLoop=
3570f 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d  p; pLoop; pLoop=
35710 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72 2c 20 63  pLoop->pPrior, c
35711 6e 74 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70  nt++){.        p
35712 4c 6f 6f 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74  Loop->pRightmost
35713 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 70 4c   = p;.        pL
35714 6f 6f 70 2d 3e 70 4e 65 78 74 20 3d 20 70 52 69  oop->pNext = pRi
35715 67 68 74 3b 0a 20 20 20 20 20 20 20 20 70 52 69  ght;.        pRi
35716 67 68 74 20 3d 20 70 4c 6f 6f 70 3b 0a 20 20 20  ght = pLoop;.   
35717 20 20 20 7d 0a 20 20 20 20 20 20 6d 78 53 65 6c     }.      mxSel
35718 65 63 74 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74  ect = db->aLimit
35719 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f  [SQLITE_LIMIT_CO
3571a 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 5d 3b 0a  MPOUND_SELECT];.
3571b 20 20 20 20 20 20 69 66 28 20 6d 78 53 65 6c 65        if( mxSele
3571c 63 74 20 26 26 20 63 6e 74 3e 6d 78 53 65 6c 65  ct && cnt>mxSele
3571d 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ct ){.        sq
3571e 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
3571f 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20  arse, "too many 
35720 74 65 72 6d 73 20 69 6e 20 63 6f 6d 70 6f 75 6e  terms in compoun
35721 64 20 53 45 4c 45 43 54 22 29 3b 0a 20 20 20 20  d SELECT");.    
35722 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f      goto select_
35723 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  end;.      }.   
35724 20 7d 0a 20 20 20 20 72 63 20 3d 20 6d 75 6c 74   }.    rc = mult
35725 69 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  iSelect(pParse, 
35726 70 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20 65  p, pDest);.    e
35727 78 70 6c 61 69 6e 53 65 74 49 6e 74 65 67 65 72  xplainSetInteger
35728 28 70 50 61 72 73 65 2d 3e 69 53 65 6c 65 63 74  (pParse->iSelect
35729 49 64 2c 20 69 52 65 73 74 6f 72 65 53 65 6c 65  Id, iRestoreSele
3572a 63 74 49 64 29 3b 0a 20 20 20 20 72 65 74 75 72  ctId);.    retur
3572b 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  n rc;.  }.#endif
3572c 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
3572d 69 73 20 62 6f 74 68 20 61 20 47 52 4f 55 50 20  is both a GROUP 
3572e 42 59 20 61 6e 64 20 61 6e 20 4f 52 44 45 52 20  BY and an ORDER 
3572f 42 59 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68  BY clause and th
35730 65 79 20 61 72 65 0a 20 20 2a 2a 20 69 64 65 6e  ey are.  ** iden
35731 74 69 63 61 6c 2c 20 74 68 65 6e 20 64 69 73 61  tical, then disa
35732 62 6c 65 20 74 68 65 20 4f 52 44 45 52 20 42 59  ble the ORDER BY
35733 20 63 6c 61 75 73 65 20 73 69 6e 63 65 20 74 68   clause since th
35734 65 20 47 52 4f 55 50 20 42 59 0a 20 20 2a 2a 20  e GROUP BY.  ** 
35735 77 69 6c 6c 20 63 61 75 73 65 20 65 6c 65 6d 65  will cause eleme
35736 6e 74 73 20 74 6f 20 63 6f 6d 65 20 6f 75 74 20  nts to come out 
35737 69 6e 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f  in the correct o
35738 72 64 65 72 2e 20 20 54 68 69 73 20 69 73 0a 20  rder.  This is. 
35739 20 2a 2a 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74   ** an optimizat
3573a 69 6f 6e 20 2d 20 74 68 65 20 63 6f 72 72 65 63  ion - the correc
3573b 74 20 61 6e 73 77 65 72 20 73 68 6f 75 6c 64 20  t answer should 
3573c 72 65 73 75 6c 74 20 72 65 67 61 72 64 6c 65 73  result regardles
3573d 73 2e 0a 20 20 2a 2a 20 55 73 65 20 74 68 65 20  s..  ** Use the 
3573e 53 51 4c 49 54 45 5f 47 72 6f 75 70 42 79 4f 72  SQLITE_GroupByOr
3573f 64 65 72 20 66 6c 61 67 20 77 69 74 68 20 53 51  der flag with SQ
35740 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 4f 50  LITE_TESTCTRL_OP
35741 54 49 4d 49 5a 45 52 0a 20 20 2a 2a 20 74 6f 20  TIMIZER.  ** to 
35742 64 69 73 61 62 6c 65 20 74 68 69 73 20 6f 70 74  disable this opt
35743 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20 74 65  imization for te
35744 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 2e 0a  sting purposes..
35745 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
35746 65 33 45 78 70 72 4c 69 73 74 43 6f 6d 70 61 72  e3ExprListCompar
35747 65 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70  e(p->pGroupBy, p
35748 4f 72 64 65 72 42 79 29 3d 3d 30 0a 20 20 20 20  OrderBy)==0.    
35749 20 20 20 20 20 26 26 20 4f 70 74 69 6d 69 7a 61       && Optimiza
3574a 74 69 6f 6e 45 6e 61 62 6c 65 64 28 64 62 2c 20  tionEnabled(db, 
3574b 53 51 4c 49 54 45 5f 47 72 6f 75 70 42 79 4f 72  SQLITE_GroupByOr
3574c 64 65 72 29 20 29 7b 0a 20 20 20 20 70 4f 72 64  der) ){.    pOrd
3574d 65 72 42 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  erBy = 0;.  }.. 
3574e 20 2f 2a 20 49 66 20 74 68 65 20 71 75 65 72 79   /* If the query
3574f 20 69 73 20 44 49 53 54 49 4e 43 54 20 77 69 74   is DISTINCT wit
35750 68 20 61 6e 20 4f 52 44 45 52 20 42 59 20 62 75  h an ORDER BY bu
35751 74 20 69 73 20 6e 6f 74 20 61 6e 20 61 67 67 72  t is not an aggr
35752 65 67 61 74 65 2c 20 61 6e 64 20 0a 20 20 2a 2a  egate, and .  **
35753 20 69 66 20 74 68 65 20 73 65 6c 65 63 74 2d 6c   if the select-l
35754 69 73 74 20 69 73 20 74 68 65 20 73 61 6d 65 20  ist is the same 
35755 61 73 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  as the ORDER BY 
35756 6c 69 73 74 2c 20 74 68 65 6e 20 74 68 69 73 20  list, then this 
35757 71 75 65 72 79 0a 20 20 2a 2a 20 63 61 6e 20 62  query.  ** can b
35758 65 20 72 65 77 72 69 74 74 65 6e 20 61 73 20 61  e rewritten as a
35759 20 47 52 4f 55 50 20 42 59 2e 20 49 6e 20 6f 74   GROUP BY. In ot
3575a 68 65 72 20 77 6f 72 64 73 2c 20 74 68 69 73 3a  her words, this:
3575b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53  .  **.  **     S
3575c 45 4c 45 43 54 20 44 49 53 54 49 4e 43 54 20 78  ELECT DISTINCT x
3575d 79 7a 20 46 52 4f 4d 20 2e 2e 2e 20 4f 52 44 45  yz FROM ... ORDE
3575e 52 20 42 59 20 78 79 7a 0a 20 20 2a 2a 0a 20 20  R BY xyz.  **.  
3575f 2a 2a 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65  ** is transforme
35760 64 20 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  d to:.  **.  ** 
35761 20 20 20 20 53 45 4c 45 43 54 20 78 79 7a 20 46      SELECT xyz F
35762 52 4f 4d 20 2e 2e 2e 20 47 52 4f 55 50 20 42 59  ROM ... GROUP BY
35763 20 78 79 7a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54   xyz.  **.  ** T
35764 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 69  he second form i
35765 73 20 70 72 65 66 65 72 72 65 64 20 61 73 20 61  s preferred as a
35766 20 73 69 6e 67 6c 65 20 69 6e 64 65 78 20 28 6f   single index (o
35767 72 20 74 65 6d 70 2d 74 61 62 6c 65 29 20 6d 61  r temp-table) ma
35768 79 20 62 65 20 0a 20 20 2a 2a 20 75 73 65 64 20  y be .  ** used 
35769 66 6f 72 20 62 6f 74 68 20 74 68 65 20 4f 52 44  for both the ORD
3576a 45 52 20 42 59 20 61 6e 64 20 44 49 53 54 49 4e  ER BY and DISTIN
3576b 43 54 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 41  CT processing. A
3576c 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 0a 20 20  s originally .  
3576d 2a 2a 20 77 72 69 74 74 65 6e 20 74 68 65 20 71  ** written the q
3576e 75 65 72 79 20 6d 75 73 74 20 75 73 65 20 61 20  uery must use a 
3576f 74 65 6d 70 2d 74 61 62 6c 65 20 66 6f 72 20 61  temp-table for a
35770 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 66 20 74  t least one of t
35771 68 65 20 4f 52 44 45 52 20 0a 20 20 2a 2a 20 42  he ORDER .  ** B
35772 59 20 61 6e 64 20 44 49 53 54 49 4e 43 54 2c 20  Y and DISTINCT, 
35773 61 6e 64 20 61 6e 20 69 6e 64 65 78 20 6f 72 20  and an index or 
35774 73 65 70 61 72 61 74 65 20 74 65 6d 70 2d 74 61  separate temp-ta
35775 62 6c 65 20 66 6f 72 20 74 68 65 20 6f 74 68 65  ble for the othe
35776 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70  r..  */.  if( (p
35777 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46  ->selFlags & (SF
35778 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67  _Distinct|SF_Agg
35779 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 69 73  regate))==SF_Dis
3577a 74 69 6e 63 74 20 0a 20 20 20 26 26 20 73 71 6c  tinct .   && sql
3577b 69 74 65 33 45 78 70 72 4c 69 73 74 43 6f 6d 70  ite3ExprListComp
3577c 61 72 65 28 70 4f 72 64 65 72 42 79 2c 20 70 2d  are(pOrderBy, p-
3577d 3e 70 45 4c 69 73 74 29 3d 3d 30 0a 20 20 29 7b  >pEList)==0.  ){
3577e 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73  .    p->selFlags
3577f 20 26 3d 20 7e 53 46 5f 44 69 73 74 69 6e 63 74   &= ~SF_Distinct
35780 3b 0a 20 20 20 20 70 2d 3e 70 47 72 6f 75 70 42  ;.    p->pGroupB
35781 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  y = sqlite3ExprL
35782 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45  istDup(db, p->pE
35783 4c 69 73 74 2c 20 30 29 3b 0a 20 20 20 20 70 47  List, 0);.    pG
35784 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f  roupBy = p->pGro
35785 75 70 42 79 3b 0a 20 20 20 20 70 4f 72 64 65 72  upBy;.    pOrder
35786 42 79 20 3d 20 30 3b 0a 20 20 20 20 2f 2a 20 4e  By = 0;.    /* N
35787 6f 74 69 63 65 20 74 68 61 74 20 65 76 65 6e 20  otice that even 
35788 74 68 6f 75 67 68 74 20 53 46 5f 44 69 73 74 69  thought SF_Disti
35789 6e 63 74 20 68 61 73 20 62 65 65 6e 20 63 6c 65  nct has been cle
3578a 61 72 65 64 20 66 72 6f 6d 20 70 2d 3e 73 65 6c  ared from p->sel
3578b 46 6c 61 67 73 2c 0a 20 20 20 20 2a 2a 20 74 68  Flags,.    ** th
3578c 65 20 73 44 69 73 74 69 6e 63 74 2e 69 73 54 6e  e sDistinct.isTn
3578d 63 74 20 69 73 20 73 74 69 6c 6c 20 73 65 74 2e  ct is still set.
3578e 20 20 48 65 6e 63 65 2c 20 69 73 54 6e 63 74 20    Hence, isTnct 
3578f 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 0a 20  represents the. 
35790 20 20 20 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 73     ** original s
35791 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20 53 46  etting of the SF
35792 5f 44 69 73 74 69 6e 63 74 20 66 6c 61 67 2c 20  _Distinct flag, 
35793 6e 6f 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  not the current 
35794 73 65 74 74 69 6e 67 20 2a 2f 0a 20 20 20 20 61  setting */.    a
35795 73 73 65 72 74 28 20 73 44 69 73 74 69 6e 63 74  ssert( sDistinct
35796 2e 69 73 54 6e 63 74 20 29 3b 0a 20 20 7d 0a 0a  .isTnct );.  }..
35797 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
35798 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   an ORDER BY cla
35799 75 73 65 2c 20 74 68 65 6e 20 74 68 69 73 20 73  use, then this s
3579a 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 65  orting.  ** inde
3579b 78 20 6d 69 67 68 74 20 65 6e 64 20 75 70 20 62  x might end up b
3579c 65 69 6e 67 20 75 6e 75 73 65 64 20 69 66 20 74  eing unused if t
3579d 68 65 20 64 61 74 61 20 63 61 6e 20 62 65 20 0a  he data can be .
3579e 20 20 2a 2a 20 65 78 74 72 61 63 74 65 64 20 69    ** extracted i
3579f 6e 20 70 72 65 2d 73 6f 72 74 65 64 20 6f 72 64  n pre-sorted ord
357a0 65 72 2e 20 20 49 66 20 74 68 61 74 20 69 73 20  er.  If that is 
357a1 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74  the case, then t
357a2 68 65 0a 20 20 2a 2a 20 4f 50 5f 4f 70 65 6e 45  he.  ** OP_OpenE
357a3 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63  phemeral instruc
357a4 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 68 61  tion will be cha
357a5 6e 67 65 64 20 74 6f 20 61 6e 20 4f 50 5f 4e 6f  nged to an OP_No
357a6 6f 70 20 6f 6e 63 65 0a 20 20 2a 2a 20 77 65 20  op once.  ** we 
357a7 66 69 67 75 72 65 20 6f 75 74 20 74 68 61 74 20  figure out that 
357a8 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  the sorting inde
357a9 78 20 69 73 20 6e 6f 74 20 6e 65 65 64 65 64 2e  x is not needed.
357aa 20 20 54 68 65 20 61 64 64 72 53 6f 72 74 49 6e    The addrSortIn
357ab 64 65 78 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c  dex.  ** variabl
357ac 65 20 69 73 20 75 73 65 64 20 74 6f 20 66 61 63  e is used to fac
357ad 69 6c 69 74 61 74 65 20 74 68 61 74 20 63 68 61  ilitate that cha
357ae 6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  nge..  */.  if( 
357af 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
357b0 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
357b1 6f 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20  o;.    pKeyInfo 
357b2 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70  = keyInfoFromExp
357b3 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 4f  rList(pParse, pO
357b4 72 64 65 72 42 79 29 3b 0a 20 20 20 20 70 4f 72  rderBy);.    pOr
357b5 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 20  derBy->iECursor 
357b6 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
357b7 3b 0a 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65  ;.    p->addrOpe
357b8 6e 45 70 68 6d 5b 32 5d 20 3d 20 61 64 64 72 53  nEphm[2] = addrS
357b9 6f 72 74 49 6e 64 65 78 20 3d 0a 20 20 20 20 20  ortIndex =.     
357ba 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
357bb 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p4(v, OP_OpenEph
357bc 65 6d 65 72 61 6c 2c 0a 20 20 20 20 20 20 20 20  emeral,.        
357bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357be 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43     pOrderBy->iEC
357bf 75 72 73 6f 72 2c 20 70 4f 72 64 65 72 42 79 2d  ursor, pOrderBy-
357c0 3e 6e 45 78 70 72 2b 32 2c 20 30 2c 0a 20 20 20  >nExpr+2, 0,.   
357c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357c2 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
357c3 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49  KeyInfo, P4_KEYI
357c4 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
357c5 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 53  }else{.    addrS
357c6 6f 72 74 49 6e 64 65 78 20 3d 20 2d 31 3b 0a 20  ortIndex = -1;. 
357c7 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
357c8 6f 75 74 70 75 74 20 69 73 20 64 65 73 74 69 6e  output is destin
357c9 65 64 20 66 6f 72 20 61 20 74 65 6d 70 6f 72 61  ed for a tempora
357ca 72 79 20 74 61 62 6c 65 2c 20 6f 70 65 6e 20 74  ry table, open t
357cb 68 61 74 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  hat table..  */.
357cc 20 20 69 66 28 20 70 44 65 73 74 2d 3e 65 44 65    if( pDest->eDe
357cd 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62  st==SRT_EphemTab
357ce 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
357cf 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
357d0 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70  OpenEphemeral, p
357d1 44 65 73 74 2d 3e 69 53 44 50 61 72 6d 2c 20 70  Dest->iSDParm, p
357d2 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20  EList->nExpr);. 
357d3 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65   }..  /* Set the
357d4 20 6c 69 6d 69 74 65 72 2e 0a 20 20 2a 2f 0a 20   limiter..  */. 
357d5 20 69 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56   iEnd = sqlite3V
357d6 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
357d7 0a 20 20 70 2d 3e 6e 53 65 6c 65 63 74 52 6f 77  .  p->nSelectRow
357d8 20 3d 20 28 64 6f 75 62 6c 65 29 4c 41 52 47 45   = (double)LARGE
357d9 53 54 5f 49 4e 54 36 34 3b 0a 20 20 63 6f 6d 70  ST_INT64;.  comp
357da 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72  uteLimitRegister
357db 73 28 70 50 61 72 73 65 2c 20 70 2c 20 69 45 6e  s(pParse, p, iEn
357dc 64 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69  d);.  if( p->iLi
357dd 6d 69 74 3d 3d 30 20 26 26 20 61 64 64 72 53 6f  mit==0 && addrSo
357de 72 74 49 6e 64 65 78 3e 3d 30 20 29 7b 0a 20 20  rtIndex>=0 ){.  
357df 20 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 74    sqlite3VdbeGet
357e0 4f 70 28 76 2c 20 61 64 64 72 53 6f 72 74 49 6e  Op(v, addrSortIn
357e1 64 65 78 29 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f  dex)->opcode = O
357e2 50 5f 53 6f 72 74 65 72 4f 70 65 6e 3b 0a 20 20  P_SorterOpen;.  
357e3 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d    p->selFlags |=
357e4 20 53 46 5f 55 73 65 53 6f 72 74 65 72 3b 0a 20   SF_UseSorter;. 
357e5 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 61 20   }..  /* Open a 
357e6 76 69 72 74 75 61 6c 20 69 6e 64 65 78 20 74 6f  virtual index to
357e7 20 75 73 65 20 66 6f 72 20 74 68 65 20 64 69 73   use for the dis
357e8 74 69 6e 63 74 20 73 65 74 2e 0a 20 20 2a 2f 0a  tinct set..  */.
357e9 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67    if( p->selFlag
357ea 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 20  s & SF_Distinct 
357eb 29 7b 0a 20 20 20 20 73 44 69 73 74 69 6e 63 74  ){.    sDistinct
357ec 2e 74 61 62 54 6e 63 74 20 3d 20 70 50 61 72 73  .tabTnct = pPars
357ed 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 73  e->nTab++;.    s
357ee 44 69 73 74 69 6e 63 74 2e 61 64 64 72 54 6e 63  Distinct.addrTnc
357ef 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  t = sqlite3VdbeA
357f0 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp4(v, OP_Open
357f1 45 70 68 65 6d 65 72 61 6c 2c 0a 20 20 20 20 20  Ephemeral,.     
357f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357f3 20 20 20 20 20 20 20 20 20 20 20 73 44 69 73 74             sDist
357f4 69 6e 63 74 2e 74 61 62 54 6e 63 74 2c 20 30 2c  inct.tabTnct, 0,
357f5 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
357f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357f7 20 20 20 20 28 63 68 61 72 2a 29 6b 65 79 49 6e      (char*)keyIn
357f8 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70  foFromExprList(p
357f9 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c 69 73 74  Parse, p->pEList
357fa 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
357fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357fc 20 20 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41     P4_KEYINFO_HA
357fd 4e 44 4f 46 46 29 3b 0a 20 20 20 20 73 71 6c 69  NDOFF);.    sqli
357fe 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
357ff 76 2c 20 42 54 52 45 45 5f 55 4e 4f 52 44 45 52  v, BTREE_UNORDER
35800 45 44 29 3b 0a 20 20 20 20 73 44 69 73 74 69 6e  ED);.    sDistin
35801 63 74 2e 65 54 6e 63 74 54 79 70 65 20 3d 20 57  ct.eTnctType = W
35802 48 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 55 4e  HERE_DISTINCT_UN
35803 4f 52 44 45 52 45 44 3b 0a 20 20 7d 65 6c 73 65  ORDERED;.  }else
35804 7b 0a 20 20 20 20 73 44 69 73 74 69 6e 63 74 2e  {.    sDistinct.
35805 65 54 6e 63 74 54 79 70 65 20 3d 20 57 48 45 52  eTnctType = WHER
35806 45 5f 44 49 53 54 49 4e 43 54 5f 4e 4f 4f 50 3b  E_DISTINCT_NOOP;
35807 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 69 73 41  .  }..  if( !isA
35808 67 67 20 26 26 20 70 47 72 6f 75 70 42 79 3d 3d  gg && pGroupBy==
35809 30 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 61  0 ){.    /* No a
3580a 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
3580b 6e 73 20 61 6e 64 20 6e 6f 20 47 52 4f 55 50 20  ns and no GROUP 
3580c 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20  BY clause */.   
3580d 20 45 78 70 72 4c 69 73 74 20 2a 70 44 69 73 74   ExprList *pDist
3580e 20 3d 20 28 73 44 69 73 74 69 6e 63 74 2e 69 73   = (sDistinct.is
3580f 54 6e 63 74 20 3f 20 70 2d 3e 70 45 4c 69 73 74  Tnct ? p->pEList
35810 20 3a 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 42   : 0);..    /* B
35811 65 67 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  egin the databas
35812 65 20 73 63 61 6e 2e 20 2a 2f 0a 20 20 20 20 70  e scan. */.    p
35813 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57  WInfo = sqlite3W
35814 68 65 72 65 42 65 67 69 6e 28 70 50 61 72 73 65  hereBegin(pParse
35815 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65  , pTabList, pWhe
35816 72 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 44  re, pOrderBy, pD
35817 69 73 74 2c 20 30 2c 30 29 3b 0a 20 20 20 20 69  ist, 0,0);.    i
35818 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67  f( pWInfo==0 ) g
35819 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  oto select_end;.
3581a 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 2d 3e      if( pWInfo->
3581b 6e 52 6f 77 4f 75 74 20 3c 20 70 2d 3e 6e 53 65  nRowOut < p->nSe
3581c 6c 65 63 74 52 6f 77 20 29 20 70 2d 3e 6e 53 65  lectRow ) p->nSe
3581d 6c 65 63 74 52 6f 77 20 3d 20 70 57 49 6e 66 6f  lectRow = pWInfo
3581e 2d 3e 6e 52 6f 77 4f 75 74 3b 0a 20 20 20 20 69  ->nRowOut;.    i
3581f 66 28 20 70 57 49 6e 66 6f 2d 3e 65 44 69 73 74  f( pWInfo->eDist
35820 69 6e 63 74 20 29 20 73 44 69 73 74 69 6e 63 74  inct ) sDistinct
35821 2e 65 54 6e 63 74 54 79 70 65 20 3d 20 70 57 49  .eTnctType = pWI
35822 6e 66 6f 2d 3e 65 44 69 73 74 69 6e 63 74 3b 0a  nfo->eDistinct;.
35823 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79      if( pOrderBy
35824 20 26 26 20 70 57 49 6e 66 6f 2d 3e 6e 4f 42 53   && pWInfo->nOBS
35825 61 74 3d 3d 70 4f 72 64 65 72 42 79 2d 3e 6e 45  at==pOrderBy->nE
35826 78 70 72 20 29 20 70 4f 72 64 65 72 42 79 20 3d  xpr ) pOrderBy =
35827 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 73   0;..    /* If s
35828 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74 68 61  orting index tha
35829 74 20 77 61 73 20 63 72 65 61 74 65 64 20 62 79  t was created by
3582a 20 61 20 70 72 69 6f 72 20 4f 50 5f 4f 70 65 6e   a prior OP_Open
3582b 45 70 68 65 6d 65 72 61 6c 20 0a 20 20 20 20 2a  Ephemeral .    *
3582c 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 6e  * instruction en
3582d 64 65 64 20 75 70 20 6e 6f 74 20 62 65 69 6e 67  ded up not being
3582e 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 63 68   needed, then ch
3582f 61 6e 67 65 20 74 68 65 20 4f 50 5f 4f 70 65 6e  ange the OP_Open
35830 45 70 68 65 6d 65 72 61 6c 0a 20 20 20 20 2a 2a  Ephemeral.    **
35831 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70   into an OP_Noop
35832 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
35833 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 3e 3d   addrSortIndex>=
35834 30 20 26 26 20 70 4f 72 64 65 72 42 79 3d 3d 30  0 && pOrderBy==0
35835 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
35836 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f  3VdbeChangeToNoo
35837 70 28 76 2c 20 61 64 64 72 53 6f 72 74 49 6e 64  p(v, addrSortInd
35838 65 78 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64  ex);.      p->ad
35839 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20  drOpenEphm[2] = 
3583a 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  -1;.    }..    /
3583b 2a 20 55 73 65 20 74 68 65 20 73 74 61 6e 64 61  * Use the standa
3583c 72 64 20 69 6e 6e 65 72 20 6c 6f 6f 70 2e 20 2a  rd inner loop. *
3583d 2f 0a 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65  /.    selectInne
3583e 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c  rLoop(pParse, p,
3583f 20 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20 70   pEList, 0, 0, p
35840 4f 72 64 65 72 42 79 2c 20 26 73 44 69 73 74 69  OrderBy, &sDisti
35841 6e 63 74 2c 20 70 44 65 73 74 2c 0a 20 20 20 20  nct, pDest,.    
35842 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35843 70 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e 75  pWInfo->iContinu
35844 65 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61  e, pWInfo->iBrea
35845 6b 29 3b 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20  k);..    /* End 
35846 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 61  the database sca
35847 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20  n loop..    */. 
35848 20 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45     sqlite3WhereE
35849 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 65  nd(pWInfo);.  }e
3584a 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73  lse{.    /* This
3584b 20 63 61 73 65 20 77 68 65 6e 20 74 68 65 72 65   case when there
3584c 20 65 78 69 73 74 20 61 67 67 72 65 67 61 74 65   exist aggregate
3584d 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 20   functions or a 
3584e 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 0a  GROUP BY clause.
3584f 20 20 20 20 2a 2a 20 6f 72 20 62 6f 74 68 20 2a      ** or both *
35850 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78  /.    NameContex
35851 74 20 73 4e 43 3b 20 20 20 20 2f 2a 20 4e 61 6d  t sNC;    /* Nam
35852 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 72  e context for pr
35853 6f 63 65 73 73 69 6e 67 20 61 67 67 72 65 67 61  ocessing aggrega
35854 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a  te information *
35855 2f 0a 20 20 20 20 69 6e 74 20 69 41 4d 65 6d 3b  /.    int iAMem;
35856 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
35857 73 74 20 4d 65 6d 20 61 64 64 72 65 73 73 20 66  st Mem address f
35858 6f 72 20 73 74 6f 72 69 6e 67 20 63 75 72 72 65  or storing curre
35859 6e 74 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a 20  nt GROUP BY */. 
3585a 20 20 20 69 6e 74 20 69 42 4d 65 6d 3b 20 20 20     int iBMem;   
3585b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
3585c 4d 65 6d 20 61 64 64 72 65 73 73 20 66 6f 72 20  Mem address for 
3585d 70 72 65 76 69 6f 75 73 20 47 52 4f 55 50 20 42  previous GROUP B
3585e 59 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 55 73  Y */.    int iUs
3585f 65 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20  eFlag;       /* 
35860 4d 65 6d 20 61 64 64 72 65 73 73 20 68 6f 6c 64  Mem address hold
35861 69 6e 67 20 66 6c 61 67 20 69 6e 64 69 63 61 74  ing flag indicat
35862 69 6e 67 20 74 68 61 74 20 61 74 20 6c 65 61 73  ing that at leas
35863 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t.              
35864 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 65            ** one
35865 20 72 6f 77 20 6f 66 20 74 68 65 20 69 6e 70 75   row of the inpu
35866 74 20 74 6f 20 74 68 65 20 61 67 67 72 65 67 61  t to the aggrega
35867 74 6f 72 20 68 61 73 20 62 65 65 6e 0a 20 20 20  tor has been.   
35868 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35869 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 65       ** processe
3586a 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 41 62  d */.    int iAb
3586b 6f 72 74 46 6c 61 67 3b 20 20 20 20 20 2f 2a 20  ortFlag;     /* 
3586c 4d 65 6d 20 61 64 64 72 65 73 73 20 77 68 69 63  Mem address whic
3586d 68 20 63 61 75 73 65 73 20 71 75 65 72 79 20 61  h causes query a
3586e 62 6f 72 74 20 69 66 20 70 6f 73 69 74 69 76 65  bort if positive
3586f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 67 72 6f 75   */.    int grou
35870 70 42 79 53 6f 72 74 3b 20 20 20 20 2f 2a 20 52  pBySort;    /* R
35871 6f 77 73 20 63 6f 6d 65 20 66 72 6f 6d 20 73 6f  ows come from so
35872 75 72 63 65 20 69 6e 20 47 52 4f 55 50 20 42 59  urce in GROUP BY
35873 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20 69 6e   order */.    in
35874 74 20 61 64 64 72 45 6e 64 3b 20 20 20 20 20 20  t addrEnd;      
35875 20 20 2f 2a 20 45 6e 64 20 6f 66 20 70 72 6f 63    /* End of proc
35876 65 73 73 69 6e 67 20 66 6f 72 20 74 68 69 73 20  essing for this 
35877 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20 20 69 6e  SELECT */.    in
35878 74 20 73 6f 72 74 50 54 61 62 20 3d 20 30 3b 20  t sortPTab = 0; 
35879 20 20 2f 2a 20 50 73 65 75 64 6f 74 61 62 6c 65    /* Pseudotable
3587a 20 75 73 65 64 20 74 6f 20 64 65 63 6f 64 65 20   used to decode 
3587b 73 6f 72 74 69 6e 67 20 72 65 73 75 6c 74 73 20  sorting results 
3587c 2a 2f 0a 20 20 20 20 69 6e 74 20 73 6f 72 74 4f  */.    int sortO
3587d 75 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4f 75  ut = 0;    /* Ou
3587e 74 70 75 74 20 72 65 67 69 73 74 65 72 20 66 72  tput register fr
3587f 6f 6d 20 74 68 65 20 73 6f 72 74 65 72 20 2a 2f  om the sorter */
35880 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20  ..    /* Remove 
35881 61 6e 79 20 61 6e 64 20 61 6c 6c 20 61 6c 69 61  any and all alia
35882 73 65 73 20 62 65 74 77 65 65 6e 20 74 68 65 20  ses between the 
35883 72 65 73 75 6c 74 20 73 65 74 20 61 6e 64 20 74  result set and t
35884 68 65 0a 20 20 20 20 2a 2a 20 47 52 4f 55 50 20  he.    ** GROUP 
35885 42 59 20 63 6c 61 75 73 65 2e 0a 20 20 20 20 2a  BY clause..    *
35886 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70  /.    if( pGroup
35887 42 79 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  By ){.      int 
35888 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
35889 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
3588a 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20  p counter */.   
3588b 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
3588c 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20  st_item *pItem; 
3588d 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20   /* For looping 
3588e 6f 76 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20  over expression 
3588f 69 6e 20 61 20 6c 69 73 74 20 2a 2f 0a 0a 20 20  in a list */..  
35890 20 20 20 20 66 6f 72 28 6b 3d 70 2d 3e 70 45 4c      for(k=p->pEL
35891 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65  ist->nExpr, pIte
35892 6d 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 3b 20  m=p->pEList->a; 
35893 6b 3e 30 3b 20 6b 2d 2d 2c 20 70 49 74 65 6d 2b  k>0; k--, pItem+
35894 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 49 74 65  +){.        pIte
35895 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0a 20  m->iAlias = 0;. 
35896 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72       }.      for
35897 28 6b 3d 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78  (k=pGroupBy->nEx
35898 70 72 2c 20 70 49 74 65 6d 3d 70 47 72 6f 75 70  pr, pItem=pGroup
35899 42 79 2d 3e 61 3b 20 6b 3e 30 3b 20 6b 2d 2d 2c  By->a; k>0; k--,
3589a 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
3589b 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73     pItem->iAlias
3589c 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
3589d 20 20 20 20 69 66 28 20 70 2d 3e 6e 53 65 6c 65      if( p->nSele
3589e 63 74 52 6f 77 3e 28 64 6f 75 62 6c 65 29 31 30  ctRow>(double)10
3589f 30 20 29 20 70 2d 3e 6e 53 65 6c 65 63 74 52 6f  0 ) p->nSelectRo
358a0 77 20 3d 20 28 64 6f 75 62 6c 65 29 31 30 30 3b  w = (double)100;
358a1 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
358a2 20 20 70 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 20    p->nSelectRow 
358a3 3d 20 28 64 6f 75 62 6c 65 29 31 3b 0a 20 20 20  = (double)1;.   
358a4 20 7d 0a 0a 20 0a 20 20 20 20 2f 2a 20 43 72 65   }.. .    /* Cre
358a5 61 74 65 20 61 20 6c 61 62 65 6c 20 74 6f 20 6a  ate a label to j
358a6 75 6d 70 20 74 6f 20 77 68 65 6e 20 77 65 20 77  ump to when we w
358a7 61 6e 74 20 74 6f 20 61 62 6f 72 74 20 74 68 65  ant to abort the
358a8 20 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 61 64   query */.    ad
358a9 64 72 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56  drEnd = sqlite3V
358aa 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
358ab 0a 0a 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74  ..    /* Convert
358ac 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6e 6f 64 65 73   TK_COLUMN nodes
358ad 20 69 6e 74 6f 20 54 4b 5f 41 47 47 5f 43 4f 4c   into TK_AGG_COL
358ae 55 4d 4e 20 61 6e 64 20 6d 61 6b 65 20 65 6e 74  UMN and make ent
358af 72 69 65 73 20 69 6e 0a 20 20 20 20 2a 2a 20 73  ries in.    ** s
358b0 41 67 67 49 6e 66 6f 20 66 6f 72 20 61 6c 6c 20  AggInfo for all 
358b1 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20  TK_AGG_FUNCTION 
358b2 6e 6f 64 65 73 20 69 6e 20 65 78 70 72 65 73 73  nodes in express
358b3 69 6f 6e 73 20 6f 66 20 74 68 65 0a 20 20 20 20  ions of the.    
358b4 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ** SELECT statem
358b5 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ent..    */.    
358b6 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20  memset(&sNC, 0, 
358b7 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20  sizeof(sNC));.  
358b8 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70    sNC.pParse = p
358b9 50 61 72 73 65 3b 0a 20 20 20 20 73 4e 43 2e 70  Parse;.    sNC.p
358ba 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62 4c 69  SrcList = pTabLi
358bb 73 74 3b 0a 20 20 20 20 73 4e 43 2e 70 41 67 67  st;.    sNC.pAgg
358bc 49 6e 66 6f 20 3d 20 26 73 41 67 67 49 6e 66 6f  Info = &sAggInfo
358bd 3b 0a 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 6e  ;.    sAggInfo.n
358be 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 20 3d 20  SortingColumn = 
358bf 70 47 72 6f 75 70 42 79 20 3f 20 70 47 72 6f 75  pGroupBy ? pGrou
358c0 70 42 79 2d 3e 6e 45 78 70 72 2b 31 20 3a 20 30  pBy->nExpr+1 : 0
358c1 3b 0a 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 70  ;.    sAggInfo.p
358c2 47 72 6f 75 70 42 79 20 3d 20 70 47 72 6f 75 70  GroupBy = pGroup
358c3 42 79 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  By;.    sqlite3E
358c4 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73  xprAnalyzeAggLis
358c5 74 28 26 73 4e 43 2c 20 70 45 4c 69 73 74 29 3b  t(&sNC, pEList);
358c6 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
358c7 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 26  AnalyzeAggList(&
358c8 73 4e 43 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a  sNC, pOrderBy);.
358c9 20 20 20 20 69 66 28 20 70 48 61 76 69 6e 67 20      if( pHaving 
358ca 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
358cb 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65  ExprAnalyzeAggre
358cc 67 61 74 65 73 28 26 73 4e 43 2c 20 70 48 61 76  gates(&sNC, pHav
358cd 69 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ing);.    }.    
358ce 73 41 67 67 49 6e 66 6f 2e 6e 41 63 63 75 6d 75  sAggInfo.nAccumu
358cf 6c 61 74 6f 72 20 3d 20 73 41 67 67 49 6e 66 6f  lator = sAggInfo
358d0 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 66 6f  .nColumn;.    fo
358d1 72 28 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66  r(i=0; i<sAggInf
358d2 6f 2e 6e 46 75 6e 63 3b 20 69 2b 2b 29 7b 0a 20  o.nFunc; i++){. 
358d3 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
358d4 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 73 41  prHasProperty(sA
358d5 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 69 5d 2e  ggInfo.aFunc[i].
358d6 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
358d7 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 73 4e  ect) );.      sN
358d8 43 2e 6e 63 46 6c 61 67 73 20 7c 3d 20 4e 43 5f  C.ncFlags |= NC_
358d9 49 6e 41 67 67 46 75 6e 63 3b 0a 20 20 20 20 20  InAggFunc;.     
358da 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
358db 79 7a 65 41 67 67 4c 69 73 74 28 26 73 4e 43 2c  yzeAggList(&sNC,
358dc 20 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b   sAggInfo.aFunc[
358dd 69 5d 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  i].pExpr->x.pLis
358de 74 29 3b 0a 20 20 20 20 20 20 73 4e 43 2e 6e 63  t);.      sNC.nc
358df 46 6c 61 67 73 20 26 3d 20 7e 4e 43 5f 49 6e 41  Flags &= ~NC_InA
358e0 67 67 46 75 6e 63 3b 0a 20 20 20 20 7d 0a 20 20  ggFunc;.    }.  
358e1 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
358e2 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 73 65  Failed ) goto se
358e3 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 20 20 2f  lect_end;..    /
358e4 2a 20 50 72 6f 63 65 73 73 69 6e 67 20 66 6f 72  * Processing for
358e5 20 61 67 67 72 65 67 61 74 65 73 20 77 69 74 68   aggregates with
358e6 20 47 52 4f 55 50 20 42 59 20 69 73 20 76 65 72   GROUP BY is ver
358e7 79 20 64 69 66 66 65 72 65 6e 74 20 61 6e 64 0a  y different and.
358e8 20 20 20 20 2a 2a 20 6d 75 63 68 20 6d 6f 72 65      ** much more
358e9 20 63 6f 6d 70 6c 65 78 20 74 68 61 6e 20 61 67   complex than ag
358ea 67 72 65 67 61 74 65 73 20 77 69 74 68 6f 75 74  gregates without
358eb 20 61 20 47 52 4f 55 50 20 42 59 2e 0a 20 20 20   a GROUP BY..   
358ec 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f   */.    if( pGro
358ed 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 4b 65  upBy ){.      Ke
358ee 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b  yInfo *pKeyInfo;
358ef 20 20 2f 2a 20 4b 65 79 69 6e 67 20 69 6e 66 6f    /* Keying info
358f0 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  rmation for the 
358f1 67 72 6f 75 70 20 62 79 20 63 6c 61 75 73 65 20  group by clause 
358f2 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 3b  */.      int j1;
358f3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
358f4 41 2d 76 73 2d 42 20 63 6f 6d 70 61 72 69 73 69  A-vs-B comparisi
358f5 6f 6e 20 6a 75 6d 70 20 2a 2f 0a 20 20 20 20 20  on jump */.     
358f6 20 69 6e 74 20 61 64 64 72 4f 75 74 70 75 74 52   int addrOutputR
358f7 6f 77 3b 20 20 2f 2a 20 53 74 61 72 74 20 6f 66  ow;  /* Start of
358f8 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74   subroutine that
358f9 20 6f 75 74 70 75 74 73 20 61 20 72 65 73 75 6c   outputs a resul
358fa 74 20 72 6f 77 20 2a 2f 0a 20 20 20 20 20 20 69  t row */.      i
358fb 6e 74 20 72 65 67 4f 75 74 70 75 74 52 6f 77 3b  nt regOutputRow;
358fc 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 64 64     /* Return add
358fd 72 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f  ress register fo
358fe 72 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74  r output subrout
358ff 69 6e 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ine */.      int
35900 20 61 64 64 72 53 65 74 41 62 6f 72 74 3b 20 20   addrSetAbort;  
35901 20 2f 2a 20 53 65 74 20 74 68 65 20 61 62 6f 72   /* Set the abor
35902 74 20 66 6c 61 67 20 61 6e 64 20 72 65 74 75 72  t flag and retur
35903 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61  n */.      int a
35904 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 3b 20 20 2f  ddrTopOfLoop;  /
35905 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 69 6e 70  * Top of the inp
35906 75 74 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20  ut loop */.     
35907 20 69 6e 74 20 61 64 64 72 53 6f 72 74 69 6e 67   int addrSorting
35908 49 64 78 3b 20 2f 2a 20 54 68 65 20 4f 50 5f 4f  Idx; /* The OP_O
35909 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 66 6f 72  penEphemeral for
3590a 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64   the sorting ind
3590b 65 78 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ex */.      int 
3590c 61 64 64 72 52 65 73 65 74 3b 20 20 20 20 20 20  addrReset;      
3590d 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 66 6f  /* Subroutine fo
3590e 72 20 72 65 73 65 74 74 69 6e 67 20 74 68 65 20  r resetting the 
3590f 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20  accumulator */. 
35910 20 20 20 20 20 69 6e 74 20 72 65 67 52 65 73 65       int regRese
35911 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75  t;       /* Retu
35912 72 6e 20 61 64 64 72 65 73 73 20 72 65 67 69 73  rn address regis
35913 74 65 72 20 66 6f 72 20 72 65 73 65 74 20 73 75  ter for reset su
35914 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 0a 20 20 20  broutine */..   
35915 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69     /* If there i
35916 73 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61  s a GROUP BY cla
35917 75 73 65 20 77 65 20 6d 69 67 68 74 20 6e 65 65  use we might nee
35918 64 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  d a sorting inde
35919 78 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 69 6d  x to.      ** im
3591a 70 6c 65 6d 65 6e 74 20 69 74 2e 20 20 41 6c 6c  plement it.  All
3591b 6f 63 61 74 65 20 74 68 61 74 20 73 6f 72 74 69  ocate that sorti
3591c 6e 67 20 69 6e 64 65 78 20 6e 6f 77 2e 20 20 49  ng index now.  I
3591d 66 20 69 74 20 74 75 72 6e 73 20 6f 75 74 0a 20  f it turns out. 
3591e 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65 20       ** that we 
3591f 64 6f 20 6e 6f 74 20 6e 65 65 64 20 69 74 20 61  do not need it a
35920 66 74 65 72 20 61 6c 6c 2c 20 74 68 65 20 4f 50  fter all, the OP
35921 5f 53 6f 72 74 65 72 4f 70 65 6e 20 69 6e 73 74  _SorterOpen inst
35922 72 75 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a  ruction.      **
35923 20 77 69 6c 6c 20 62 65 20 63 6f 6e 76 65 72 74   will be convert
35924 65 64 20 69 6e 74 6f 20 61 20 4e 6f 6f 70 2e 20  ed into a Noop. 
35925 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20   .      */.     
35926 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
35927 67 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e  gIdx = pParse->n
35928 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 70 4b 65  Tab++;.      pKe
35929 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46  yInfo = keyInfoF
3592a 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72  romExprList(pPar
3592b 73 65 2c 20 70 47 72 6f 75 70 42 79 29 3b 0a 20  se, pGroupBy);. 
3592c 20 20 20 20 20 61 64 64 72 53 6f 72 74 69 6e 67       addrSorting
3592d 49 64 78 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Idx = sqlite3Vdb
3592e 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 6f  eAddOp4(v, OP_So
3592f 72 74 65 72 4f 70 65 6e 2c 20 0a 20 20 20 20 20  rterOpen, .     
35930 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 73 6f       sAggInfo.so
35931 72 74 69 6e 67 49 64 78 2c 20 73 41 67 67 49 6e  rtingIdx, sAggIn
35932 66 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d  fo.nSortingColum
35933 6e 2c 20 0a 20 20 20 20 20 20 20 20 20 20 30 2c  n, .          0,
35934 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f   (char*)pKeyInfo
35935 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
35936 44 4f 46 46 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  DOFF);..      /*
35937 20 49 6e 69 74 69 61 6c 69 7a 65 20 6d 65 6d 6f   Initialize memo
35938 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 75 73 65  ry locations use
35939 64 20 62 79 20 47 52 4f 55 50 20 42 59 20 61 67  d by GROUP BY ag
3593a 67 72 65 67 61 74 65 20 70 72 6f 63 65 73 73 69  gregate processi
3593b 6e 67 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ng.      */.    
3593c 20 20 69 55 73 65 46 6c 61 67 20 3d 20 2b 2b 70    iUseFlag = ++p
3593d 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
3593e 20 20 20 69 41 62 6f 72 74 46 6c 61 67 20 3d 20     iAbortFlag = 
3593f 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
35940 20 20 20 20 20 20 72 65 67 4f 75 74 70 75 74 52        regOutputR
35941 6f 77 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  ow = ++pParse->n
35942 4d 65 6d 3b 0a 20 20 20 20 20 20 61 64 64 72 4f  Mem;.      addrO
35943 75 74 70 75 74 52 6f 77 20 3d 20 73 71 6c 69 74  utputRow = sqlit
35944 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
35945 76 29 3b 0a 20 20 20 20 20 20 72 65 67 52 65 73  v);.      regRes
35946 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  et = ++pParse->n
35947 4d 65 6d 3b 0a 20 20 20 20 20 20 61 64 64 72 52  Mem;.      addrR
35948 65 73 65 74 20 3d 20 73 71 6c 69 74 65 33 56 64  eset = sqlite3Vd
35949 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
3594a 20 20 20 20 20 20 69 41 4d 65 6d 20 3d 20 70 50        iAMem = pP
3594b 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a  arse->nMem + 1;.
3594c 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
3594d 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42 79 2d 3e  em += pGroupBy->
3594e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 69 42 4d  nExpr;.      iBM
3594f 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  em = pParse->nMe
35950 6d 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 50 61  m + 1;.      pPa
35951 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 47 72  rse->nMem += pGr
35952 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20  oupBy->nExpr;.  
35953 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
35954 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
35955 67 65 72 2c 20 30 2c 20 69 41 62 6f 72 74 46 6c  ger, 0, iAbortFl
35956 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  ag);.      VdbeC
35957 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 63 6c 65 61  omment((v, "clea
35958 72 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29 3b  r abort flag"));
35959 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
3595a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
3595b 6e 74 65 67 65 72 2c 20 30 2c 20 69 55 73 65 46  nteger, 0, iUseF
3595c 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65  lag);.      Vdbe
3595d 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e 64  Comment((v, "ind
3595e 69 63 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f  icate accumulato
3595f 72 20 65 6d 70 74 79 22 29 29 3b 0a 20 20 20 20  r empty"));.    
35960 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
35961 4f 70 33 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op3(v, OP_Null, 
35962 30 2c 20 69 41 4d 65 6d 2c 20 69 41 4d 65 6d 2b  0, iAMem, iAMem+
35963 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2d  pGroupBy->nExpr-
35964 31 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 42 65  1);..      /* Be
35965 67 69 6e 20 61 20 6c 6f 6f 70 20 74 68 61 74 20  gin a loop that 
35966 77 69 6c 6c 20 65 78 74 72 61 63 74 20 61 6c 6c  will extract all
35967 20 73 6f 75 72 63 65 20 72 6f 77 73 20 69 6e 20   source rows in 
35968 47 52 4f 55 50 20 42 59 20 6f 72 64 65 72 2e 0a  GROUP BY order..
35969 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 6d 69        ** This mi
3596a 67 68 74 20 69 6e 76 6f 6c 76 65 20 74 77 6f 20  ght involve two 
3596b 73 65 70 61 72 61 74 65 20 6c 6f 6f 70 73 20 77  separate loops w
3596c 69 74 68 20 61 6e 20 4f 50 5f 53 6f 72 74 20 69  ith an OP_Sort i
3596d 6e 20 62 65 74 77 65 65 6e 2c 20 6f 72 0a 20 20  n between, or.  
3596e 20 20 20 20 2a 2a 20 69 74 20 6d 69 67 68 74 20      ** it might 
3596f 62 65 20 61 20 73 69 6e 67 6c 65 20 6c 6f 6f 70  be a single loop
35970 20 74 68 61 74 20 75 73 65 73 20 61 6e 20 69 6e   that uses an in
35971 64 65 78 20 74 6f 20 65 78 74 72 61 63 74 20 69  dex to extract i
35972 6e 66 6f 72 6d 61 74 69 6f 6e 0a 20 20 20 20 20  nformation.     
35973 20 2a 2a 20 69 6e 20 74 68 65 20 72 69 67 68 74   ** in the right
35974 20 6f 72 64 65 72 20 74 6f 20 62 65 67 69 6e 20   order to begin 
35975 77 69 74 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  with..      */. 
35976 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
35977 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73  AddOp2(v, OP_Gos
35978 75 62 2c 20 72 65 67 52 65 73 65 74 2c 20 61 64  ub, regReset, ad
35979 64 72 52 65 73 65 74 29 3b 0a 20 20 20 20 20 20  drReset);.      
3597a 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33  pWInfo = sqlite3
3597b 57 68 65 72 65 42 65 67 69 6e 28 70 50 61 72 73  WhereBegin(pPars
3597c 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68  e, pTabList, pWh
3597d 65 72 65 2c 20 70 47 72 6f 75 70 42 79 2c 20 30  ere, pGroupBy, 0
3597e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  , 0, 0);.      i
3597f 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67  f( pWInfo==0 ) g
35980 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  oto select_end;.
35981 20 20 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f        if( pWInfo
35982 2d 3e 6e 4f 42 53 61 74 3d 3d 70 47 72 6f 75 70  ->nOBSat==pGroup
35983 42 79 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  By->nExpr ){.   
35984 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 74 69       /* The opti
35985 6d 69 7a 65 72 20 69 73 20 61 62 6c 65 20 74 6f  mizer is able to
35986 20 64 65 6c 69 76 65 72 20 72 6f 77 73 20 69 6e   deliver rows in
35987 20 67 72 6f 75 70 20 62 79 20 6f 72 64 65 72 20   group by order 
35988 73 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65  so.        ** we
35989 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20   do not have to 
3598a 73 6f 72 74 2e 20 20 54 68 65 20 4f 50 5f 4f 70  sort.  The OP_Op
3598b 65 6e 45 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  enEphemeral tabl
3598c 65 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20  e will be.      
3598d 20 20 2a 2a 20 63 61 6e 63 65 6c 6c 65 64 20 6c    ** cancelled l
3598e 61 74 65 72 20 62 65 63 61 75 73 65 20 77 65 20  ater because we 
3598f 73 74 69 6c 6c 20 6e 65 65 64 20 74 6f 20 75 73  still need to us
35990 65 20 74 68 65 20 70 4b 65 79 49 6e 66 6f 0a 20  e the pKeyInfo. 
35991 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
35992 20 20 67 72 6f 75 70 42 79 53 6f 72 74 20 3d 20    groupBySort = 
35993 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  0;.      }else{.
35994 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 73 20          /* Rows 
35995 61 72 65 20 63 6f 6d 69 6e 67 20 6f 75 74 20 69  are coming out i
35996 6e 20 75 6e 64 65 74 65 72 6d 69 6e 65 64 20 6f  n undetermined o
35997 72 64 65 72 2e 20 20 57 65 20 68 61 76 65 20 74  rder.  We have t
35998 6f 20 70 75 73 68 0a 20 20 20 20 20 20 20 20 2a  o push.        *
35999 2a 20 65 61 63 68 20 72 6f 77 20 69 6e 74 6f 20  * each row into 
3599a 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c  a sorting index,
3599b 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 66   terminate the f
3599c 69 72 73 74 20 6c 6f 6f 70 2c 0a 20 20 20 20 20  irst loop,.     
3599d 20 20 20 2a 2a 20 74 68 65 6e 20 6c 6f 6f 70 20     ** then loop 
3599e 6f 76 65 72 20 74 68 65 20 73 6f 72 74 69 6e 67  over the sorting
3599f 20 69 6e 64 65 78 20 69 6e 20 6f 72 64 65 72 20   index in order 
359a0 74 6f 20 67 65 74 20 74 68 65 20 6f 75 74 70 75  to get the outpu
359a1 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20  t.        ** in 
359a2 73 6f 72 74 65 64 20 6f 72 64 65 72 0a 20 20 20  sorted order.   
359a3 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
359a4 69 6e 74 20 72 65 67 42 61 73 65 3b 0a 20 20 20  int regBase;.   
359a5 20 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 6f       int regReco
359a6 72 64 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  rd;.        int 
359a7 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e  nCol;.        in
359a8 74 20 6e 47 72 6f 75 70 42 79 3b 0a 0a 20 20 20  t nGroupBy;..   
359a9 20 20 20 20 20 65 78 70 6c 61 69 6e 54 65 6d 70       explainTemp
359aa 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 0a 20  Table(pParse, . 
359ab 20 20 20 20 20 20 20 20 20 20 20 28 73 44 69 73             (sDis
359ac 74 69 6e 63 74 2e 69 73 54 6e 63 74 20 26 26 20  tinct.isTnct && 
359ad 28 70 2d 3e 73 65 6c 46 6c 61 67 73 26 53 46 5f  (p->selFlags&SF_
359ae 44 69 73 74 69 6e 63 74 29 3d 3d 30 29 20 3f 0a  Distinct)==0) ?.
359af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359b0 20 20 20 20 22 44 49 53 54 49 4e 43 54 22 20 3a      "DISTINCT" :
359b1 20 22 47 52 4f 55 50 20 42 59 22 29 3b 0a 0a 20   "GROUP BY");.. 
359b2 20 20 20 20 20 20 20 67 72 6f 75 70 42 79 53 6f         groupBySo
359b3 72 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  rt = 1;.        
359b4 6e 47 72 6f 75 70 42 79 20 3d 20 70 47 72 6f 75  nGroupBy = pGrou
359b5 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  pBy->nExpr;.    
359b6 20 20 20 20 6e 43 6f 6c 20 3d 20 6e 47 72 6f 75      nCol = nGrou
359b7 70 42 79 20 2b 20 31 3b 0a 20 20 20 20 20 20 20  pBy + 1;.       
359b8 20 6a 20 3d 20 6e 47 72 6f 75 70 42 79 2b 31 3b   j = nGroupBy+1;
359b9 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
359ba 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 43 6f  ; i<sAggInfo.nCo
359bb 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  lumn; i++){.    
359bc 20 20 20 20 20 20 69 66 28 20 73 41 67 67 49 6e        if( sAggIn
359bd 66 6f 2e 61 43 6f 6c 5b 69 5d 2e 69 53 6f 72 74  fo.aCol[i].iSort
359be 65 72 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20  erColumn>=j ){. 
359bf 20 20 20 20 20 20 20 20 20 20 20 6e 43 6f 6c 2b             nCol+
359c0 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a  +;.            j
359c1 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ++;.          }.
359c2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
359c3 20 20 72 65 67 42 61 73 65 20 3d 20 73 71 6c 69    regBase = sqli
359c4 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
359c5 70 50 61 72 73 65 2c 20 6e 43 6f 6c 29 3b 0a 20  pParse, nCol);. 
359c6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
359c7 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61  prCacheClear(pPa
359c8 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  rse);.        sq
359c9 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70  lite3ExprCodeExp
359ca 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 47  rList(pParse, pG
359cb 72 6f 75 70 42 79 2c 20 72 65 67 42 61 73 65 2c  roupBy, regBase,
359cc 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
359cd 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
359ce 2c 20 4f 50 5f 53 65 71 75 65 6e 63 65 2c 20 73  , OP_Sequence, s
359cf 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49  AggInfo.sortingI
359d0 64 78 2c 72 65 67 42 61 73 65 2b 6e 47 72 6f 75  dx,regBase+nGrou
359d1 70 42 79 29 3b 0a 20 20 20 20 20 20 20 20 6a 20  pBy);.        j 
359d2 3d 20 6e 47 72 6f 75 70 42 79 2b 31 3b 0a 20 20  = nGroupBy+1;.  
359d3 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
359d4 3c 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d  <sAggInfo.nColum
359d5 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; i++){.       
359d6 20 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66     struct AggInf
359d7 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26 73  o_col *pCol = &s
359d8 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 69 5d 3b  AggInfo.aCol[i];
359d9 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
359da 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75  Col->iSorterColu
359db 6d 6e 3e 3d 6a 20 29 7b 0a 20 20 20 20 20 20 20  mn>=j ){.       
359dc 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 6a 20       int r1 = j 
359dd 2b 20 72 65 67 42 61 73 65 3b 0a 20 20 20 20 20  + regBase;.     
359de 20 20 20 20 20 20 20 69 6e 74 20 72 32 3b 0a 0a         int r2;..
359df 20 20 20 20 20 20 20 20 20 20 20 20 72 32 20 3d              r2 =
359e0 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
359e1 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65  GetColumn(pParse
359e2 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
359e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359e4 20 20 70 43 6f 6c 2d 3e 70 54 61 62 2c 20 70 43    pCol->pTab, pC
359e5 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 43 6f  ol->iColumn, pCo
359e6 6c 2d 3e 69 54 61 62 6c 65 2c 20 72 31 2c 20 30  l->iTable, r1, 0
359e7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
359e8 66 28 20 72 31 21 3d 72 32 20 29 7b 0a 20 20 20  f( r1!=r2 ){.   
359e9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
359ea 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
359eb 4f 50 5f 53 43 6f 70 79 2c 20 72 32 2c 20 72 31  OP_SCopy, r2, r1
359ec 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  );.            }
359ed 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 2b 2b  .            j++
359ee 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
359ef 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
359f0 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69  regRecord = sqli
359f1 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
359f2 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73  arse);.        s
359f3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
359f4 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
359f5 64 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c  d, regBase, nCol
359f6 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20  , regRecord);.  
359f7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
359f8 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f  eAddOp2(v, OP_So
359f9 72 74 65 72 49 6e 73 65 72 74 2c 20 73 41 67 67  rterInsert, sAgg
359fa 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c  Info.sortingIdx,
359fb 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 20   regRecord);.   
359fc 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
359fd 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
359fe 65 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20  e, regRecord);. 
359ff 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
35a00 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70  leaseTempRange(p
35a01 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20  Parse, regBase, 
35a02 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 73  nCol);.        s
35a03 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70  qlite3WhereEnd(p
35a04 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20  WInfo);.        
35a05 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67  sAggInfo.sorting
35a06 49 64 78 50 54 61 62 20 3d 20 73 6f 72 74 50 54  IdxPTab = sortPT
35a07 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  ab = pParse->nTa
35a08 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 73 6f 72  b++;.        sor
35a09 74 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 47 65  tOut = sqlite3Ge
35a0a 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
35a0b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
35a0c 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
35a0d 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 73 6f  P_OpenPseudo, so
35a0e 72 74 50 54 61 62 2c 20 73 6f 72 74 4f 75 74 2c  rtPTab, sortOut,
35a0f 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20   nCol);.        
35a10 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
35a11 32 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 53 6f  2(v, OP_SorterSo
35a12 72 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72  rt, sAggInfo.sor
35a13 74 69 6e 67 49 64 78 2c 20 61 64 64 72 45 6e 64  tingIdx, addrEnd
35a14 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43  );.        VdbeC
35a15 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 47 52 4f 55  omment((v, "GROU
35a16 50 20 42 59 20 73 6f 72 74 22 29 29 3b 0a 20 20  P BY sort"));.  
35a17 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 75        sAggInfo.u
35a18 73 65 53 6f 72 74 69 6e 67 49 64 78 20 3d 20 31  seSortingIdx = 1
35a19 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
35a1a 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28  3ExprCacheClear(
35a1b 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 7d  pParse);.      }
35a1c 0a 0a 20 20 20 20 20 20 2f 2a 20 45 76 61 6c 75  ..      /* Evalu
35a1d 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ate the current 
35a1e 47 52 4f 55 50 20 42 59 20 74 65 72 6d 73 20 61  GROUP BY terms a
35a1f 6e 64 20 73 74 6f 72 65 20 69 6e 20 62 30 2c 20  nd store in b0, 
35a20 62 31 2c 20 62 32 2e 2e 2e 0a 20 20 20 20 20 20  b1, b2....      
35a21 2a 2a 20 28 62 30 20 69 73 20 6d 65 6d 6f 72 79  ** (b0 is memory
35a22 20 6c 6f 63 61 74 69 6f 6e 20 69 42 4d 65 6d 2b   location iBMem+
35a23 30 2c 20 62 31 20 69 73 20 69 42 4d 65 6d 2b 31  0, b1 is iBMem+1
35a24 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a  , and so forth).
35a25 20 20 20 20 20 20 2a 2a 20 54 68 65 6e 20 63 6f        ** Then co
35a26 6d 70 61 72 65 20 74 68 65 20 63 75 72 72 65 6e  mpare the curren
35a27 74 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 73  t GROUP BY terms
35a28 20 61 67 61 69 6e 73 74 20 74 68 65 20 47 52 4f   against the GRO
35a29 55 50 20 42 59 20 74 65 72 6d 73 0a 20 20 20 20  UP BY terms.    
35a2a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 70 72    ** from the pr
35a2b 65 76 69 6f 75 73 20 72 6f 77 20 63 75 72 72 65  evious row curre
35a2c 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 61  ntly stored in a
35a2d 30 2c 20 61 31 2c 20 61 32 2e 2e 2e 0a 20 20 20  0, a1, a2....   
35a2e 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 64 64 72     */.      addr
35a2f 54 6f 70 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69  TopOfLoop = sqli
35a30 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
35a31 64 72 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c  dr(v);.      sql
35a32 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65  ite3ExprCacheCle
35a33 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  ar(pParse);.    
35a34 20 20 69 66 28 20 67 72 6f 75 70 42 79 53 6f 72    if( groupBySor
35a35 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
35a36 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
35a37 2c 20 4f 50 5f 53 6f 72 74 65 72 44 61 74 61 2c  , OP_SorterData,
35a38 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
35a39 67 49 64 78 2c 20 73 6f 72 74 4f 75 74 29 3b 0a  gIdx, sortOut);.
35a3a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f        }.      fo
35a3b 72 28 6a 3d 30 3b 20 6a 3c 70 47 72 6f 75 70 42  r(j=0; j<pGroupB
35a3c 79 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b 0a  y->nExpr; j++){.
35a3d 20 20 20 20 20 20 20 20 69 66 28 20 67 72 6f 75          if( grou
35a3e 70 42 79 53 6f 72 74 20 29 7b 0a 20 20 20 20 20  pBySort ){.     
35a3f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
35a40 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
35a41 75 6d 6e 2c 20 73 6f 72 74 50 54 61 62 2c 20 6a  umn, sortPTab, j
35a42 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20  , iBMem+j);.    
35a43 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 30 20 29        if( j==0 )
35a44 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
35a45 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 43  geP5(v, OPFLAG_C
35a46 4c 45 41 52 43 41 43 48 45 29 3b 0a 20 20 20 20  LEARCACHE);.    
35a47 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
35a48 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 64 69       sAggInfo.di
35a49 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20 20  rectMode = 1;.  
35a4a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
35a4b 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
35a4c 70 47 72 6f 75 70 42 79 2d 3e 61 5b 6a 5d 2e 70  pGroupBy->a[j].p
35a4d 45 78 70 72 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a  Expr, iBMem+j);.
35a4e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
35a4f 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  }.      sqlite3V
35a50 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
35a51 43 6f 6d 70 61 72 65 2c 20 69 41 4d 65 6d 2c 20  Compare, iAMem, 
35a52 69 42 4d 65 6d 2c 20 70 47 72 6f 75 70 42 79 2d  iBMem, pGroupBy-
35a53 3e 6e 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20  >nExpr,.        
35a54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a55 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66    (char*)pKeyInf
35a56 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a  o, P4_KEYINFO);.
35a57 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74        j1 = sqlit
35a58 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
35a59 72 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  r(v);.      sqli
35a5a 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
35a5b 20 4f 50 5f 4a 75 6d 70 2c 20 6a 31 2b 31 2c 20   OP_Jump, j1+1, 
35a5c 30 2c 20 6a 31 2b 31 29 3b 0a 0a 20 20 20 20 20  0, j1+1);..     
35a5d 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
35a5e 65 20 74 68 61 74 20 72 75 6e 73 20 77 68 65 6e  e that runs when
35a5f 65 76 65 72 20 74 68 65 20 47 52 4f 55 50 20 42  ever the GROUP B
35a60 59 20 63 68 61 6e 67 65 73 2e 0a 20 20 20 20 20  Y changes..     
35a61 20 2a 2a 20 43 68 61 6e 67 65 73 20 69 6e 20 74   ** Changes in t
35a62 68 65 20 47 52 4f 55 50 20 42 59 20 61 72 65 20  he GROUP BY are 
35a63 64 65 74 65 63 74 65 64 20 62 79 20 74 68 65 20  detected by the 
35a64 70 72 65 76 69 6f 75 73 20 63 6f 64 65 0a 20 20  previous code.  
35a65 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49      ** block.  I
35a66 66 20 74 68 65 72 65 20 77 65 72 65 20 6e 6f 20  f there were no 
35a67 63 68 61 6e 67 65 73 2c 20 74 68 69 73 20 62 6c  changes, this bl
35a68 6f 63 6b 20 69 73 20 73 6b 69 70 70 65 64 2e 0a  ock is skipped..
35a69 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
35a6a 2a 20 54 68 69 73 20 63 6f 64 65 20 63 6f 70 69  * This code copi
35a6b 65 73 20 63 75 72 72 65 6e 74 20 67 72 6f 75 70  es current group
35a6c 20 62 79 20 74 65 72 6d 73 20 69 6e 20 62 30 2c   by terms in b0,
35a6d 62 31 2c 62 32 2c 2e 2e 2e 0a 20 20 20 20 20 20  b1,b2,....      
35a6e 2a 2a 20 6f 76 65 72 20 74 6f 20 61 30 2c 61 31  ** over to a0,a1
35a6f 2c 61 32 2e 20 20 49 74 20 74 68 65 6e 20 63 61  ,a2.  It then ca
35a70 6c 6c 73 20 74 68 65 20 6f 75 74 70 75 74 20 73  lls the output s
35a71 75 62 72 6f 75 74 69 6e 65 0a 20 20 20 20 20 20  ubroutine.      
35a72 2a 2a 20 61 6e 64 20 72 65 73 65 74 73 20 74 68  ** and resets th
35a73 65 20 61 67 67 72 65 67 61 74 65 20 61 63 63 75  e aggregate accu
35a74 6d 75 6c 61 74 6f 72 20 72 65 67 69 73 74 65 72  mulator register
35a75 73 20 69 6e 20 70 72 65 70 61 72 61 74 69 6f 6e  s in preparation
35a76 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68  .      ** for th
35a77 65 20 6e 65 78 74 20 47 52 4f 55 50 20 42 59 20  e next GROUP BY 
35a78 62 61 74 63 68 2e 0a 20 20 20 20 20 20 2a 2f 0a  batch..      */.
35a79 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
35a7a 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65  rCodeMove(pParse
35a7b 2c 20 69 42 4d 65 6d 2c 20 69 41 4d 65 6d 2c 20  , iBMem, iAMem, 
35a7c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 29  pGroupBy->nExpr)
35a7d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
35a7e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
35a7f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 70 75 74  Gosub, regOutput
35a80 52 6f 77 2c 20 61 64 64 72 4f 75 74 70 75 74 52  Row, addrOutputR
35a81 6f 77 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  ow);.      VdbeC
35a82 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 6f 75 74 70  omment((v, "outp
35a83 75 74 20 6f 6e 65 20 72 6f 77 22 29 29 3b 0a 20  ut one row"));. 
35a84 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
35a85 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 50  AddOp2(v, OP_IfP
35a86 6f 73 2c 20 69 41 62 6f 72 74 46 6c 61 67 2c 20  os, iAbortFlag, 
35a87 61 64 64 72 45 6e 64 29 3b 0a 20 20 20 20 20 20  addrEnd);.      
35a88 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
35a89 22 63 68 65 63 6b 20 61 62 6f 72 74 20 66 6c 61  "check abort fla
35a8a 67 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  g"));.      sqli
35a8b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
35a8c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 52 65   OP_Gosub, regRe
35a8d 73 65 74 2c 20 61 64 64 72 52 65 73 65 74 29 3b  set, addrReset);
35a8e 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
35a8f 6e 74 28 28 76 2c 20 22 72 65 73 65 74 20 61 63  nt((v, "reset ac
35a90 63 75 6d 75 6c 61 74 6f 72 22 29 29 3b 0a 0a 20  cumulator"));.. 
35a91 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74       /* Update t
35a92 68 65 20 61 67 67 72 65 67 61 74 65 20 61 63 63  he aggregate acc
35a93 75 6d 75 6c 61 74 6f 72 73 20 62 61 73 65 64 20  umulators based 
35a94 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  on the content o
35a95 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63  f.      ** the c
35a96 75 72 72 65 6e 74 20 72 6f 77 0a 20 20 20 20 20  urrent row.     
35a97 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
35a98 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
35a99 20 6a 31 29 3b 0a 20 20 20 20 20 20 75 70 64 61   j1);.      upda
35a9a 74 65 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50  teAccumulator(pP
35a9b 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29  arse, &sAggInfo)
35a9c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
35a9d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
35a9e 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 55 73 65  Integer, 1, iUse
35a9f 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62  Flag);.      Vdb
35aa0 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e  eComment((v, "in
35aa1 64 69 63 61 74 65 20 64 61 74 61 20 69 6e 20 61  dicate data in a
35aa2 63 63 75 6d 75 6c 61 74 6f 72 22 29 29 3b 0a 0a  ccumulator"));..
35aa3 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20        /* End of 
35aa4 74 68 65 20 6c 6f 6f 70 0a 20 20 20 20 20 20 2a  the loop.      *
35aa5 2f 0a 20 20 20 20 20 20 69 66 28 20 67 72 6f 75  /.      if( grou
35aa6 70 42 79 53 6f 72 74 20 29 7b 0a 20 20 20 20 20  pBySort ){.     
35aa7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
35aa8 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74 65  dOp2(v, OP_Sorte
35aa9 72 4e 65 78 74 2c 20 73 41 67 67 49 6e 66 6f 2e  rNext, sAggInfo.
35aaa 73 6f 72 74 69 6e 67 49 64 78 2c 20 61 64 64 72  sortingIdx, addr
35aab 54 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20  TopOfLoop);.    
35aac 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
35aad 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
35aae 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  (pWInfo);.      
35aaf 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
35ab0 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 61 64 64  ngeToNoop(v, add
35ab1 72 53 6f 72 74 69 6e 67 49 64 78 29 3b 0a 20 20  rSortingIdx);.  
35ab2 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
35ab3 4f 75 74 70 75 74 20 74 68 65 20 66 69 6e 61 6c  Output the final
35ab4 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 0a 20   row of result. 
35ab5 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
35ab6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
35ab7 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67  v, OP_Gosub, reg
35ab8 4f 75 74 70 75 74 52 6f 77 2c 20 61 64 64 72 4f  OutputRow, addrO
35ab9 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20  utputRow);.     
35aba 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
35abb 20 22 6f 75 74 70 75 74 20 66 69 6e 61 6c 20 72   "output final r
35abc 6f 77 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  ow"));..      /*
35abd 20 4a 75 6d 70 20 6f 76 65 72 20 74 68 65 20 73   Jump over the s
35abe 75 62 72 6f 75 74 69 6e 65 73 0a 20 20 20 20 20  ubroutines.     
35abf 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
35ac0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
35ac1 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 45  P_Goto, 0, addrE
35ac2 6e 64 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47  nd);..      /* G
35ac3 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f 75  enerate a subrou
35ac4 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74  tine that output
35ac5 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f  s a single row o
35ac6 66 20 74 68 65 20 72 65 73 75 6c 74 0a 20 20 20  f the result.   
35ac7 20 20 20 2a 2a 20 73 65 74 2e 20 20 54 68 69 73     ** set.  This
35ac8 20 73 75 62 72 6f 75 74 69 6e 65 20 66 69 72 73   subroutine firs
35ac9 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 69  t looks at the i
35aca 55 73 65 46 6c 61 67 2e 20 20 49 66 20 69 55 73  UseFlag.  If iUs
35acb 65 46 6c 61 67 0a 20 20 20 20 20 20 2a 2a 20 69  eFlag.      ** i
35acc 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65  s less than or e
35acd 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2c 20 74 68  qual to zero, th
35ace 65 20 73 75 62 72 6f 75 74 69 6e 65 20 69 73 20  e subroutine is 
35acf 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 0a 20 20 20  a no-op.  If.   
35ad0 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 73     ** the proces
35ad1 73 69 6e 67 20 63 61 6c 6c 73 20 66 6f 72 20 74  sing calls for t
35ad2 68 65 20 71 75 65 72 79 20 74 6f 20 61 62 6f 72  he query to abor
35ad3 74 2c 20 74 68 69 73 20 73 75 62 72 6f 75 74 69  t, this subrouti
35ad4 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e 63 72  ne.      ** incr
35ad5 65 6d 65 6e 74 73 20 74 68 65 20 69 41 62 6f 72  ements the iAbor
35ad6 74 46 6c 61 67 20 6d 65 6d 6f 72 79 20 6c 6f 63  tFlag memory loc
35ad7 61 74 69 6f 6e 20 62 65 66 6f 72 65 20 72 65 74  ation before ret
35ad8 75 72 6e 69 6e 67 20 69 6e 0a 20 20 20 20 20 20  urning in.      
35ad9 2a 2a 20 6f 72 64 65 72 20 74 6f 20 73 69 67 6e  ** order to sign
35ada 61 6c 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  al the caller to
35adb 20 61 62 6f 72 74 2e 0a 20 20 20 20 20 20 2a 2f   abort..      */
35adc 0a 20 20 20 20 20 20 61 64 64 72 53 65 74 41 62  .      addrSetAb
35add 6f 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ort = sqlite3Vdb
35ade 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
35adf 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
35ae0 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
35ae1 6e 74 65 67 65 72 2c 20 31 2c 20 69 41 62 6f 72  nteger, 1, iAbor
35ae2 74 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 64  tFlag);.      Vd
35ae3 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 73  beComment((v, "s
35ae4 65 74 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29  et abort flag"))
35ae5 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
35ae6 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
35ae7 52 65 74 75 72 6e 2c 20 72 65 67 4f 75 74 70 75  Return, regOutpu
35ae8 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 73 71 6c  tRow);.      sql
35ae9 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
35aea 61 62 65 6c 28 76 2c 20 61 64 64 72 4f 75 74 70  abel(v, addrOutp
35aeb 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 61 64  utRow);.      ad
35aec 64 72 4f 75 74 70 75 74 52 6f 77 20 3d 20 73 71  drOutputRow = sq
35aed 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
35aee 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 73  Addr(v);.      s
35aef 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
35af0 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 55  (v, OP_IfPos, iU
35af1 73 65 46 6c 61 67 2c 20 61 64 64 72 4f 75 74 70  seFlag, addrOutp
35af2 75 74 52 6f 77 2b 32 29 3b 0a 20 20 20 20 20 20  utRow+2);.      
35af3 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
35af4 22 47 72 6f 75 70 62 79 20 72 65 73 75 6c 74 20  "Groupby result 
35af5 67 65 6e 65 72 61 74 6f 72 20 65 6e 74 72 79 20  generator entry 
35af6 70 6f 69 6e 74 22 29 29 3b 0a 20 20 20 20 20 20  point"));.      
35af7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
35af8 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20  1(v, OP_Return, 
35af9 72 65 67 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20  regOutputRow);. 
35afa 20 20 20 20 20 66 69 6e 61 6c 69 7a 65 41 67 67       finalizeAgg
35afb 46 75 6e 63 74 69 6f 6e 73 28 70 50 61 72 73 65  Functions(pParse
35afc 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20  , &sAggInfo);.  
35afd 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
35afe 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70  fFalse(pParse, p
35aff 48 61 76 69 6e 67 2c 20 61 64 64 72 4f 75 74 70  Having, addrOutp
35b00 75 74 52 6f 77 2b 31 2c 20 53 51 4c 49 54 45 5f  utRow+1, SQLITE_
35b01 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20  JUMPIFNULL);.   
35b02 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f     selectInnerLo
35b03 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d  op(pParse, p, p-
35b04 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20 70  >pEList, 0, 0, p
35b05 4f 72 64 65 72 42 79 2c 0a 20 20 20 20 20 20 20  OrderBy,.       
35b06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
35b07 73 44 69 73 74 69 6e 63 74 2c 20 70 44 65 73 74  sDistinct, pDest
35b08 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
35b09 20 20 20 20 20 20 20 20 61 64 64 72 4f 75 74 70          addrOutp
35b0a 75 74 52 6f 77 2b 31 2c 20 61 64 64 72 53 65 74  utRow+1, addrSet
35b0b 41 62 6f 72 74 29 3b 0a 20 20 20 20 20 20 73 71  Abort);.      sq
35b0c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
35b0d 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65  v, OP_Return, re
35b0e 67 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20  gOutputRow);.   
35b0f 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
35b10 76 2c 20 22 65 6e 64 20 67 72 6f 75 70 62 79 20  v, "end groupby 
35b11 72 65 73 75 6c 74 20 67 65 6e 65 72 61 74 6f 72  result generator
35b12 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47  "));..      /* G
35b13 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f 75  enerate a subrou
35b14 74 69 6e 65 20 74 68 61 74 20 77 69 6c 6c 20 72  tine that will r
35b15 65 73 65 74 20 74 68 65 20 67 72 6f 75 70 2d 62  eset the group-b
35b16 79 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a 20 20  y accumulator.  
35b17 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
35b18 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
35b19 61 62 65 6c 28 76 2c 20 61 64 64 72 52 65 73 65  abel(v, addrRese
35b1a 74 29 3b 0a 20 20 20 20 20 20 72 65 73 65 74 41  t);.      resetA
35b1b 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73  ccumulator(pPars
35b1c 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20  e, &sAggInfo);. 
35b1d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
35b1e 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74  AddOp1(v, OP_Ret
35b1f 75 72 6e 2c 20 72 65 67 52 65 73 65 74 29 3b 0a  urn, regReset);.
35b20 20 20 20 20 20 0a 20 20 20 20 7d 20 2f 2a 20 65       .    } /* e
35b21 6e 64 69 66 20 70 47 72 6f 75 70 42 79 2e 20 20  ndif pGroupBy.  
35b22 42 65 67 69 6e 20 61 67 67 72 65 67 61 74 65 20  Begin aggregate 
35b23 71 75 65 72 69 65 73 20 77 69 74 68 6f 75 74 20  queries without 
35b24 47 52 4f 55 50 20 42 59 3a 20 2a 2f 0a 20 20 20  GROUP BY: */.   
35b25 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 45 78   else {.      Ex
35b26 70 72 4c 69 73 74 20 2a 70 44 65 6c 20 3d 20 30  prList *pDel = 0
35b27 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
35b28 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54  _OMIT_BTREECOUNT
35b29 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54  .      Table *pT
35b2a 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70  ab;.      if( (p
35b2b 54 61 62 20 3d 20 69 73 53 69 6d 70 6c 65 43 6f  Tab = isSimpleCo
35b2c 75 6e 74 28 70 2c 20 26 73 41 67 67 49 6e 66 6f  unt(p, &sAggInfo
35b2d 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ))!=0 ){.       
35b2e 20 2f 2a 20 49 66 20 69 73 53 69 6d 70 6c 65 43   /* If isSimpleC
35b2f 6f 75 6e 74 28 29 20 72 65 74 75 72 6e 73 20 61  ount() returns a
35b30 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 61   pointer to a Ta
35b31 62 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 74  ble structure, t
35b32 68 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  hen.        ** t
35b33 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
35b34 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a   is of the form:
35b35 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
35b36 20 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20      **   SELECT 
35b37 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 3c 74  count(*) FROM <t
35b38 62 6c 3e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  bl>.        **. 
35b39 20 20 20 20 20 20 20 2a 2a 20 77 68 65 72 65 20         ** where 
35b3a 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
35b3b 75 72 65 20 72 65 74 75 72 6e 65 64 20 72 65 70  ure returned rep
35b3c 72 65 73 65 6e 74 73 20 74 61 62 6c 65 20 3c 74  resents table <t
35b3d 62 6c 3e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a  bl>..        **.
35b3e 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20          ** This 
35b3f 73 74 61 74 65 6d 65 6e 74 20 69 73 20 73 6f 20  statement is so 
35b40 63 6f 6d 6d 6f 6e 20 74 68 61 74 20 69 74 20 69  common that it i
35b41 73 20 6f 70 74 69 6d 69 7a 65 64 20 73 70 65 63  s optimized spec
35b42 69 61 6c 6c 79 2e 20 54 68 65 0a 20 20 20 20 20  ially. The.     
35b43 20 20 20 2a 2a 20 4f 50 5f 43 6f 75 6e 74 20 69     ** OP_Count i
35b44 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 65 78  nstruction is ex
35b45 65 63 75 74 65 64 20 65 69 74 68 65 72 20 6f 6e  ecuted either on
35b46 20 74 68 65 20 69 6e 74 6b 65 79 20 74 61 62 6c   the intkey tabl
35b47 65 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a  e that.        *
35b48 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64  * contains the d
35b49 61 74 61 20 66 6f 72 20 74 61 62 6c 65 20 3c 74  ata for table <t
35b4a 62 6c 3e 20 6f 72 20 6f 6e 20 6f 6e 65 20 6f 66  bl> or on one of
35b4b 20 69 74 73 20 69 6e 64 65 78 65 73 2e 20 49 74   its indexes. It
35b4c 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 62  .        ** is b
35b4d 65 74 74 65 72 20 74 6f 20 65 78 65 63 75 74 65  etter to execute
35b4e 20 74 68 65 20 6f 70 20 6f 6e 20 61 6e 20 69 6e   the op on an in
35b4f 64 65 78 2c 20 61 73 20 69 6e 64 65 78 65 73 20  dex, as indexes 
35b50 61 72 65 20 61 6c 6d 6f 73 74 0a 20 20 20 20 20  are almost.     
35b51 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 73 70 72     ** always spr
35b52 65 61 64 20 61 63 72 6f 73 73 20 6c 65 73 73 20  ead across less 
35b53 70 61 67 65 73 20 74 68 61 6e 20 74 68 65 69 72  pages than their
35b54 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
35b55 61 62 6c 65 73 2e 0a 20 20 20 20 20 20 20 20 2a  ables..        *
35b56 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  /.        const 
35b57 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65  int iDb = sqlite
35b58 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
35b59 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d  Parse->db, pTab-
35b5a 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20  >pSchema);.     
35b5b 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 43 73     const int iCs
35b5c 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
35b5d 2b 2b 3b 20 20 20 20 20 2f 2a 20 43 75 72 73 6f  ++;     /* Curso
35b5e 72 20 74 6f 20 73 63 61 6e 20 62 2d 74 72 65 65  r to scan b-tree
35b5f 20 2a 2f 0a 20 20 20 20 20 20 20 20 49 6e 64 65   */.        Inde
35b60 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20  x *pIdx;        
35b61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35b62 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
35b63 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20  iable */.       
35b64 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
35b65 66 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  fo = 0;         
35b66 20 20 20 20 20 20 2f 2a 20 4b 65 79 69 6e 66 6f        /* Keyinfo
35b67 20 66 6f 72 20 73 63 61 6e 6e 65 64 20 69 6e 64   for scanned ind
35b68 65 78 20 2a 2f 0a 20 20 20 20 20 20 20 20 49 6e  ex */.        In
35b69 64 65 78 20 2a 70 42 65 73 74 20 3d 20 30 3b 20  dex *pBest = 0; 
35b6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35b6b 20 20 20 2f 2a 20 42 65 73 74 20 69 6e 64 65 78     /* Best index
35b6c 20 66 6f 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f   found so far */
35b6d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 52 6f  .        int iRo
35b6e 6f 74 20 3d 20 70 54 61 62 2d 3e 74 6e 75 6d 3b  ot = pTab->tnum;
35b6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35b70 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 73 63   Root page of sc
35b71 61 6e 6e 65 64 20 62 2d 74 72 65 65 20 2a 2f 0a  anned b-tree */.
35b72 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
35b73 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61  CodeVerifySchema
35b74 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20  (pParse, iDb);. 
35b75 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 61         sqlite3Ta
35b76 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20  bleLock(pParse, 
35b77 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  iDb, pTab->tnum,
35b78 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29   0, pTab->zName)
35b79 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 65  ;..        /* Se
35b7a 61 72 63 68 20 66 6f 72 20 74 68 65 20 69 6e 64  arch for the ind
35b7b 65 78 20 74 68 61 74 20 68 61 73 20 74 68 65 20  ex that has the 
35b7c 6c 65 61 73 74 20 61 6d 6f 75 6e 74 20 6f 66 20  least amount of 
35b7d 63 6f 6c 75 6d 6e 73 2e 20 49 66 0a 20 20 20 20  columns. If.    
35b7e 20 20 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20      ** there is 
35b7f 73 75 63 68 20 61 6e 20 69 6e 64 65 78 2c 20 61  such an index, a
35b80 6e 64 20 69 74 20 68 61 73 20 6c 65 73 73 20 63  nd it has less c
35b81 6f 6c 75 6d 6e 73 20 74 68 61 6e 20 74 68 65 20  olumns than the 
35b82 74 61 62 6c 65 0a 20 20 20 20 20 20 20 20 2a 2a  table.        **
35b83 20 64 6f 65 73 2c 20 74 68 65 6e 20 77 65 20 63   does, then we c
35b84 61 6e 20 61 73 73 75 6d 65 20 74 68 61 74 20 69  an assume that i
35b85 74 20 63 6f 6e 73 75 6d 65 73 20 6c 65 73 73 20  t consumes less 
35b86 73 70 61 63 65 20 6f 6e 20 64 69 73 6b 20 61 6e  space on disk an
35b87 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c  d.        ** wil
35b88 6c 20 74 68 65 72 65 66 6f 72 65 20 62 65 20 63  l therefore be c
35b89 68 65 61 70 65 72 20 74 6f 20 73 63 61 6e 20 74  heaper to scan t
35b8a 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
35b8b 71 75 65 72 79 20 72 65 73 75 6c 74 2e 0a 20 20  query result..  
35b8c 20 20 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73        ** In this
35b8d 20 63 61 73 65 20 73 65 74 20 69 52 6f 6f 74 20   case set iRoot 
35b8e 74 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  to the root page
35b8f 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69   number of the i
35b90 6e 64 65 78 20 62 2d 74 72 65 65 0a 20 20 20 20  ndex b-tree.    
35b91 20 20 20 20 2a 2a 20 61 6e 64 20 70 4b 65 79 49      ** and pKeyI
35b92 6e 66 6f 20 74 6f 20 74 68 65 20 4b 65 79 49 6e  nfo to the KeyIn
35b93 66 6f 20 73 74 72 75 63 74 75 72 65 20 72 65 71  fo structure req
35b94 75 69 72 65 64 20 74 6f 20 6e 61 76 69 67 61 74  uired to navigat
35b95 65 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  e the.        **
35b96 20 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 20 20   index..        
35b97 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 28 32  **.        ** (2
35b98 30 31 31 2d 30 34 2d 31 35 29 20 44 6f 20 6e 6f  011-04-15) Do no
35b99 74 20 64 6f 20 61 20 66 75 6c 6c 20 73 63 61 6e  t do a full scan
35b9a 20 6f 66 20 61 6e 20 75 6e 6f 72 64 65 72 65 64   of an unordered
35b9b 20 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 20 20   index..        
35b9c 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 6e  **.        ** In
35b9d 20 70 72 61 63 74 69 63 65 20 74 68 65 20 4b 65   practice the Ke
35b9e 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
35b9f 77 69 6c 6c 20 6e 6f 74 20 62 65 20 75 73 65 64  will not be used
35ba0 2e 20 49 74 20 69 73 20 6f 6e 6c 79 20 0a 20 20  . It is only .  
35ba1 20 20 20 20 20 20 2a 2a 20 70 61 73 73 65 64 20        ** passed 
35ba2 74 6f 20 6b 65 65 70 20 4f 50 5f 4f 70 65 6e 52  to keep OP_OpenR
35ba3 65 61 64 20 68 61 70 70 79 2e 0a 20 20 20 20 20  ead happy..     
35ba4 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f     */.        fo
35ba5 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
35ba6 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
35ba7 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
35ba8 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78          if( pIdx
35ba9 2d 3e 62 55 6e 6f 72 64 65 72 65 64 3d 3d 30 20  ->bUnordered==0 
35baa 26 26 20 28 21 70 42 65 73 74 20 7c 7c 20 70 49  && (!pBest || pI
35bab 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3c 70 42 65 73  dx->nColumn<pBes
35bac 74 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 29 7b 0a 20  t->nColumn) ){. 
35bad 20 20 20 20 20 20 20 20 20 20 20 70 42 65 73 74             pBest
35bae 20 3d 20 70 49 64 78 3b 0a 20 20 20 20 20 20 20   = pIdx;.       
35baf 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
35bb0 20 20 20 20 20 20 20 69 66 28 20 70 42 65 73 74         if( pBest
35bb1 20 26 26 20 70 42 65 73 74 2d 3e 6e 43 6f 6c 75   && pBest->nColu
35bb2 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b  mn<pTab->nCol ){
35bb3 0a 20 20 20 20 20 20 20 20 20 20 69 52 6f 6f 74  .          iRoot
35bb4 20 3d 20 70 42 65 73 74 2d 3e 74 6e 75 6d 3b 0a   = pBest->tnum;.
35bb5 20 20 20 20 20 20 20 20 20 20 70 4b 65 79 49 6e            pKeyIn
35bb6 66 6f 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  fo = sqlite3Inde
35bb7 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
35bb8 20 70 42 65 73 74 29 3b 0a 20 20 20 20 20 20 20   pBest);.       
35bb9 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f   }..        /* O
35bba 70 65 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20  pen a read-only 
35bbb 63 75 72 73 6f 72 2c 20 65 78 65 63 75 74 65 20  cursor, execute 
35bbc 74 68 65 20 4f 50 5f 43 6f 75 6e 74 2c 20 63 6c  the OP_Count, cl
35bbd 6f 73 65 20 74 68 65 20 63 75 72 73 6f 72 2e 20  ose the cursor. 
35bbe 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
35bbf 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
35bc0 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 43 73  OP_OpenRead, iCs
35bc1 72 2c 20 69 52 6f 6f 74 2c 20 69 44 62 29 3b 0a  r, iRoot, iDb);.
35bc2 20 20 20 20 20 20 20 20 69 66 28 20 70 4b 65 79          if( pKey
35bc3 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20  Info ){.        
35bc4 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
35bc5 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68  ngeP4(v, -1, (ch
35bc6 61 72 20 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50  ar *)pKeyInfo, P
35bc7 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
35bc8 46 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  F);.        }.  
35bc9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
35bca 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f  eAddOp2(v, OP_Co
35bcb 75 6e 74 2c 20 69 43 73 72 2c 20 73 41 67 67 49  unt, iCsr, sAggI
35bcc 6e 66 6f 2e 61 46 75 6e 63 5b 30 5d 2e 69 4d 65  nfo.aFunc[0].iMe
35bcd 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  m);.        sqli
35bce 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
35bcf 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 73 72 29   OP_Close, iCsr)
35bd0 3b 0a 20 20 20 20 20 20 20 20 65 78 70 6c 61 69  ;.        explai
35bd1 6e 53 69 6d 70 6c 65 43 6f 75 6e 74 28 70 50 61  nSimpleCount(pPa
35bd2 72 73 65 2c 20 70 54 61 62 2c 20 70 42 65 73 74  rse, pTab, pBest
35bd3 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23  );.      }else.#
35bd4 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
35bd5 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 20  OMIT_BTREECOUNT 
35bd6 2a 2f 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20  */.      {.     
35bd7 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74     /* Check if t
35bd8 68 65 20 71 75 65 72 79 20 69 73 20 6f 66 20 6f  he query is of o
35bd9 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
35bda 69 6e 67 20 66 6f 72 6d 73 3a 0a 20 20 20 20 20  ing forms:.     
35bdb 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
35bdc 20 20 20 53 45 4c 45 43 54 20 6d 69 6e 28 78 29     SELECT min(x)
35bdd 20 46 52 4f 4d 20 2e 2e 2e 0a 20 20 20 20 20 20   FROM ....      
35bde 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 6d 61    **   SELECT ma
35bdf 78 28 78 29 20 46 52 4f 4d 20 2e 2e 2e 0a 20 20  x(x) FROM ....  
35be0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
35be1 20 2a 2a 20 49 66 20 69 74 20 69 73 2c 20 74 68   ** If it is, th
35be2 65 6e 20 61 73 6b 20 74 68 65 20 63 6f 64 65 20  en ask the code 
35be3 69 6e 20 77 68 65 72 65 2e 63 20 74 6f 20 61 74  in where.c to at
35be4 74 65 6d 70 74 20 74 6f 20 73 6f 72 74 20 72 65  tempt to sort re
35be5 73 75 6c 74 73 0a 20 20 20 20 20 20 20 20 2a 2a  sults.        **
35be6 20 61 73 20 69 66 20 74 68 65 72 65 20 77 61 73   as if there was
35be7 20 61 6e 20 22 4f 52 44 45 52 20 4f 4e 20 78 22   an "ORDER ON x"
35be8 20 6f 72 20 22 4f 52 44 45 52 20 4f 4e 20 78 20   or "ORDER ON x 
35be9 44 45 53 43 22 20 63 6c 61 75 73 65 2e 20 0a 20  DESC" clause. . 
35bea 20 20 20 20 20 20 20 2a 2a 20 49 66 20 77 68 65         ** If whe
35beb 72 65 2e 63 20 69 73 20 61 62 6c 65 20 74 6f 20  re.c is able to 
35bec 70 72 6f 64 75 63 65 20 72 65 73 75 6c 74 73 20  produce results 
35bed 73 6f 72 74 65 64 20 69 6e 20 74 68 69 73 20 6f  sorted in this o
35bee 72 64 65 72 2c 20 74 68 65 6e 0a 20 20 20 20 20  rder, then.     
35bef 20 20 20 2a 2a 20 61 64 64 20 76 64 62 65 20 63     ** add vdbe c
35bf0 6f 64 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74  ode to break out
35bf1 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 69   of the processi
35bf2 6e 67 20 6c 6f 6f 70 20 61 66 74 65 72 20 74 68  ng loop after th
35bf3 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69  e .        ** fi
35bf4 72 73 74 20 69 74 65 72 61 74 69 6f 6e 20 28 73  rst iteration (s
35bf5 69 6e 63 65 20 74 68 65 20 66 69 72 73 74 20 69  ince the first i
35bf6 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  teration of the 
35bf7 6c 6f 6f 70 20 69 73 20 0a 20 20 20 20 20 20 20  loop is .       
35bf8 20 2a 2a 20 67 75 61 72 61 6e 74 65 65 64 20 74   ** guaranteed t
35bf9 6f 20 6f 70 65 72 61 74 65 20 6f 6e 20 74 68 65  o operate on the
35bfa 20 72 6f 77 20 77 69 74 68 20 74 68 65 20 6d 69   row with the mi
35bfb 6e 69 6d 75 6d 20 6f 72 20 6d 61 78 69 6d 75 6d  nimum or maximum
35bfc 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 76 61 6c   .        ** val
35bfd 75 65 20 6f 66 20 78 2c 20 74 68 65 20 6f 6e 6c  ue of x, the onl
35bfe 79 20 72 6f 77 20 72 65 71 75 69 72 65 64 29 2e  y row required).
35bff 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
35c00 20 20 20 20 2a 2a 20 41 20 73 70 65 63 69 61 6c      ** A special
35c01 20 66 6c 61 67 20 6d 75 73 74 20 62 65 20 70 61   flag must be pa
35c02 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 57  ssed to sqlite3W
35c03 68 65 72 65 42 65 67 69 6e 28 29 20 74 6f 20 73  hereBegin() to s
35c04 6c 69 67 68 74 6c 79 0a 20 20 20 20 20 20 20 20  lightly.        
35c05 2a 2a 20 6d 6f 64 69 66 79 20 62 65 68 61 76 69  ** modify behavi
35c06 6f 75 72 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  our as follows:.
35c07 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
35c08 20 20 20 2a 2a 20 20 20 2b 20 49 66 20 74 68 65     **   + If the
35c09 20 71 75 65 72 79 20 69 73 20 61 20 22 53 45 4c   query is a "SEL
35c0a 45 43 54 20 6d 69 6e 28 78 29 22 2c 20 74 68 65  ECT min(x)", the
35c0b 6e 20 74 68 65 20 6c 6f 6f 70 20 63 6f 64 65 64  n the loop coded
35c0c 20 62 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 20   by.        **  
35c0d 20 20 20 77 68 65 72 65 2e 63 20 73 68 6f 75 6c     where.c shoul
35c0e 64 20 6e 6f 74 20 69 74 65 72 61 74 65 20 6f 76  d not iterate ov
35c0f 65 72 20 61 6e 79 20 76 61 6c 75 65 73 20 77 69  er any values wi
35c10 74 68 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 0a  th a NULL value.
35c11 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 66          **     f
35c12 6f 72 20 78 2e 0a 20 20 20 20 20 20 20 20 2a 2a  or x..        **
35c13 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2b 20  .        **   + 
35c14 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 63 6f  The optimizer co
35c15 64 65 20 69 6e 20 77 68 65 72 65 2e 63 20 28 74  de in where.c (t
35c16 68 65 20 74 68 69 6e 67 20 74 68 61 74 20 64 65  he thing that de
35c17 63 69 64 65 73 20 77 68 69 63 68 0a 20 20 20 20  cides which.    
35c18 20 20 20 20 2a 2a 20 20 20 20 20 69 6e 64 65 78      **     index
35c19 20 6f 72 20 69 6e 64 69 63 65 73 20 74 6f 20 75   or indices to u
35c1a 73 65 29 20 73 68 6f 75 6c 64 20 70 6c 61 63 65  se) should place
35c1b 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 72 69   a different pri
35c1c 6f 72 69 74 79 20 6f 6e 20 0a 20 20 20 20 20 20  ority on .      
35c1d 20 20 2a 2a 20 20 20 20 20 73 61 74 69 73 66 79    **     satisfy
35c1e 69 6e 67 20 74 68 65 20 27 4f 52 44 45 52 20 42  ing the 'ORDER B
35c1f 59 27 20 63 6c 61 75 73 65 20 74 68 61 6e 20 69  Y' clause than i
35c20 74 20 64 6f 65 73 20 69 6e 20 6f 74 68 65 72 20  t does in other 
35c21 63 61 73 65 73 2e 0a 20 20 20 20 20 20 20 20 2a  cases..        *
35c22 2a 20 20 20 20 20 52 65 66 65 72 20 74 6f 20 63  *     Refer to c
35c23 6f 64 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73  ode and comments
35c24 20 69 6e 20 77 68 65 72 65 2e 63 20 66 6f 72 20   in where.c for 
35c25 64 65 74 61 69 6c 73 2e 0a 20 20 20 20 20 20 20  details..       
35c26 20 2a 2f 0a 20 20 20 20 20 20 20 20 45 78 70 72   */.        Expr
35c27 4c 69 73 74 20 2a 70 4d 69 6e 4d 61 78 20 3d 20  List *pMinMax = 
35c28 30 3b 0a 20 20 20 20 20 20 20 20 75 38 20 66 6c  0;.        u8 fl
35c29 61 67 20 3d 20 6d 69 6e 4d 61 78 51 75 65 72 79  ag = minMaxQuery
35c2a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  (p);.        if(
35c2b 20 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20   flag ){.       
35c2c 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
35c2d 48 61 73 50 72 6f 70 65 72 74 79 28 70 2d 3e 70  HasProperty(p->p
35c2e 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  EList->a[0].pExp
35c2f 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
35c30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73   );.          as
35c31 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 2d  sert( p->pEList-
35c32 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 78 2e 70  >a[0].pExpr->x.p
35c33 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 31 20 29  List->nExpr==1 )
35c34 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d 69 6e  ;.          pMin
35c35 4d 61 78 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Max = sqlite3Exp
35c36 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e  rListDup(db, p->
35c37 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
35c38 70 72 2d 3e 78 2e 70 4c 69 73 74 2c 30 29 3b 0a  pr->x.pList,0);.
35c39 20 20 20 20 20 20 20 20 20 20 70 44 65 6c 20 3d            pDel =
35c3a 20 70 4d 69 6e 4d 61 78 3b 0a 20 20 20 20 20 20   pMinMax;.      
35c3b 20 20 20 20 69 66 28 20 70 4d 69 6e 4d 61 78 20      if( pMinMax 
35c3c 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  && !db->mallocFa
35c3d 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  iled ){.        
35c3e 20 20 20 20 70 4d 69 6e 4d 61 78 2d 3e 61 5b 30      pMinMax->a[0
35c3f 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 66 6c  ].sortOrder = fl
35c40 61 67 21 3d 57 48 45 52 45 5f 4f 52 44 45 52 42  ag!=WHERE_ORDERB
35c41 59 5f 4d 49 4e 20 3f 31 3a 30 3b 0a 20 20 20 20  Y_MIN ?1:0;.    
35c42 20 20 20 20 20 20 20 20 70 4d 69 6e 4d 61 78 2d          pMinMax-
35c43 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 20  >a[0].pExpr->op 
35c44 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20  = TK_COLUMN;.   
35c45 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
35c46 20 7d 0a 20 20 0a 20 20 20 20 20 20 20 20 2f 2a   }.  .        /*
35c47 20 54 68 69 73 20 63 61 73 65 20 72 75 6e 73 20   This case runs 
35c48 69 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65  if the aggregate
35c49 20 68 61 73 20 6e 6f 20 47 52 4f 55 50 20 42 59   has no GROUP BY
35c4a 20 63 6c 61 75 73 65 2e 20 20 54 68 65 0a 20 20   clause.  The.  
35c4b 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73        ** process
35c4c 69 6e 67 20 69 73 20 6d 75 63 68 20 73 69 6d 70  ing is much simp
35c4d 6c 65 72 20 73 69 6e 63 65 20 74 68 65 72 65 20  ler since there 
35c4e 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65  is only a single
35c4f 20 72 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a 20   row.        ** 
35c50 6f 66 20 6f 75 74 70 75 74 2e 0a 20 20 20 20 20  of output..     
35c51 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65     */.        re
35c52 73 65 74 41 63 63 75 6d 75 6c 61 74 6f 72 28 70  setAccumulator(p
35c53 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f  Parse, &sAggInfo
35c54 29 3b 0a 20 20 20 20 20 20 20 20 70 57 49 6e 66  );.        pWInf
35c55 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65  o = sqlite3Where
35c56 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54  Begin(pParse, pT
35c57 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20  abList, pWhere, 
35c58 70 4d 69 6e 4d 61 78 2c 30 2c 66 6c 61 67 2c 30  pMinMax,0,flag,0
35c59 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
35c5a 57 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20  WInfo==0 ){.    
35c5b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
35c5c 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
35c5d 70 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 20  pDel);.         
35c5e 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64   goto select_end
35c5f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
35c60 20 20 20 20 75 70 64 61 74 65 41 63 63 75 6d 75      updateAccumu
35c61 6c 61 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73  lator(pParse, &s
35c62 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  AggInfo);.      
35c63 20 20 61 73 73 65 72 74 28 20 70 4d 69 6e 4d 61    assert( pMinMa
35c64 78 3d 3d 30 20 7c 7c 20 70 4d 69 6e 4d 61 78 2d  x==0 || pMinMax-
35c65 3e 6e 45 78 70 72 3d 3d 31 20 29 3b 0a 20 20 20  >nExpr==1 );.   
35c66 20 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 2d       if( pWInfo-
35c67 3e 6e 4f 42 53 61 74 3e 30 20 29 7b 0a 20 20 20  >nOBSat>0 ){.   
35c68 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
35c69 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
35c6a 6f 74 6f 2c 20 30 2c 20 70 57 49 6e 66 6f 2d 3e  oto, 0, pWInfo->
35c6b 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 20  iBreak);.       
35c6c 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
35c6d 76 2c 20 22 25 73 28 29 20 62 79 20 69 6e 64 65  v, "%s() by inde
35c6e 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  x",.            
35c6f 20 20 20 20 28 66 6c 61 67 3d 3d 57 48 45 52 45      (flag==WHERE
35c70 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 3f 22 6d 69  _ORDERBY_MIN?"mi
35c71 6e 22 3a 22 6d 61 78 22 29 29 29 3b 0a 20 20 20  n":"max")));.   
35c72 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73       }.        s
35c73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70  qlite3WhereEnd(p
35c74 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20  WInfo);.        
35c75 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74  finalizeAggFunct
35c76 69 6f 6e 73 28 70 50 61 72 73 65 2c 20 26 73 41  ions(pParse, &sA
35c77 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 7d  ggInfo);.      }
35c78 0a 0a 20 20 20 20 20 20 70 4f 72 64 65 72 42 79  ..      pOrderBy
35c79 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 0;.      sqli
35c7a 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
35c7b 50 61 72 73 65 2c 20 70 48 61 76 69 6e 67 2c 20  Parse, pHaving, 
35c7c 61 64 64 72 45 6e 64 2c 20 53 51 4c 49 54 45 5f  addrEnd, SQLITE_
35c7d 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20  JUMPIFNULL);.   
35c7e 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f     selectInnerLo
35c7f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d  op(pParse, p, p-
35c80 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20 30  >pEList, 0, 0, 0
35c81 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20  , 0, .          
35c82 20 20 20 20 20 20 20 20 20 20 20 20 70 44 65 73              pDes
35c83 74 2c 20 61 64 64 72 45 6e 64 2c 20 61 64 64 72  t, addrEnd, addr
35c84 45 6e 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  End);.      sqli
35c85 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
35c86 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20  e(db, pDel);.   
35c87 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
35c88 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
35c89 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20 20 20 20  , addrEnd);.    
35c8a 0a 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 61 67  .  } /* endif ag
35c8b 67 72 65 67 61 74 65 20 71 75 65 72 79 20 2a 2f  gregate query */
35c8c 0a 0a 20 20 69 66 28 20 73 44 69 73 74 69 6e 63  ..  if( sDistinc
35c8d 74 2e 65 54 6e 63 74 54 79 70 65 3d 3d 57 48 45  t.eTnctType==WHE
35c8e 52 45 5f 44 49 53 54 49 4e 43 54 5f 55 4e 4f 52  RE_DISTINCT_UNOR
35c8f 44 45 52 45 44 20 29 7b 0a 20 20 20 20 65 78 70  DERED ){.    exp
35c90 6c 61 69 6e 54 65 6d 70 54 61 62 6c 65 28 70 50  lainTempTable(pP
35c91 61 72 73 65 2c 20 22 44 49 53 54 49 4e 43 54 22  arse, "DISTINCT"
35c92 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
35c93 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45  there is an ORDE
35c94 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65  R BY clause, the
35c95 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 73 6f 72  n we need to sor
35c96 74 20 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20  t the results.  
35c97 2a 2a 20 61 6e 64 20 73 65 6e 64 20 74 68 65 6d  ** and send them
35c98 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   to the callback
35c99 20 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a 20 20 2a   one by one..  *
35c9a 2f 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79  /.  if( pOrderBy
35c9b 20 29 7b 0a 20 20 20 20 65 78 70 6c 61 69 6e 54   ){.    explainT
35c9c 65 6d 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c  empTable(pParse,
35c9d 20 22 4f 52 44 45 52 20 42 59 22 29 3b 0a 20 20   "ORDER BY");.  
35c9e 20 20 67 65 6e 65 72 61 74 65 53 6f 72 74 54 61    generateSortTa
35c9f 69 6c 28 70 50 61 72 73 65 2c 20 70 2c 20 76 2c  il(pParse, p, v,
35ca0 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20   pEList->nExpr, 
35ca1 70 44 65 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f  pDest);.  }..  /
35ca2 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 73  * Jump here to s
35ca3 6b 69 70 20 74 68 69 73 20 71 75 65 72 79 0a 20  kip this query. 
35ca4 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
35ca5 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
35ca6 20 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a 20 54 68   iEnd);..  /* Th
35ca7 65 20 53 45 4c 45 43 54 20 77 61 73 20 73 75 63  e SELECT was suc
35ca8 63 65 73 73 66 75 6c 6c 79 20 63 6f 64 65 64 2e  cessfully coded.
35ca9 20 20 20 53 65 74 20 74 68 65 20 72 65 74 75 72     Set the retur
35caa 6e 20 63 6f 64 65 20 74 6f 20 30 0a 20 20 2a 2a  n code to 0.  **
35cab 20 74 6f 20 69 6e 64 69 63 61 74 65 20 6e 6f 20   to indicate no 
35cac 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 72  errors..  */.  r
35cad 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6e  c = 0;..  /* Con
35cae 74 72 6f 6c 20 6a 75 6d 70 73 20 74 6f 20 68 65  trol jumps to he
35caf 72 65 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69  re if an error i
35cb0 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 61 62  s encountered ab
35cb1 6f 76 65 2c 20 6f 72 20 75 70 6f 6e 0a 20 20 2a  ove, or upon.  *
35cb2 2a 20 73 75 63 63 65 73 73 66 75 6c 20 63 6f 64  * successful cod
35cb3 69 6e 67 20 6f 66 20 74 68 65 20 53 45 4c 45 43  ing of the SELEC
35cb4 54 2e 0a 20 20 2a 2f 0a 73 65 6c 65 63 74 5f 65  T..  */.select_e
35cb5 6e 64 3a 0a 20 20 65 78 70 6c 61 69 6e 53 65 74  nd:.  explainSet
35cb6 49 6e 74 65 67 65 72 28 70 50 61 72 73 65 2d 3e  Integer(pParse->
35cb7 69 53 65 6c 65 63 74 49 64 2c 20 69 52 65 73 74  iSelectId, iRest
35cb8 6f 72 65 53 65 6c 65 63 74 49 64 29 3b 0a 0a 20  oreSelectId);.. 
35cb9 20 2f 2a 20 49 64 65 6e 74 69 66 79 20 63 6f 6c   /* Identify col
35cba 75 6d 6e 20 6e 61 6d 65 73 20 69 66 20 72 65 73  umn names if res
35cbb 75 6c 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45  ults of the SELE
35cbc 43 54 20 61 72 65 20 74 6f 20 62 65 20 6f 75 74  CT are to be out
35cbd 70 75 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  put..  */.  if( 
35cbe 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
35cbf 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53   pDest->eDest==S
35cc0 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 20  RT_Output ){.   
35cc1 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e   generateColumnN
35cc2 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ames(pParse, pTa
35cc3 62 4c 69 73 74 2c 20 70 45 4c 69 73 74 29 3b 0a  bList, pEList);.
35cc4 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 44 62    }..  sqlite3Db
35cc5 46 72 65 65 28 64 62 2c 20 73 41 67 67 49 6e 66  Free(db, sAggInf
35cc6 6f 2e 61 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74  o.aCol);.  sqlit
35cc7 65 33 44 62 46 72 65 65 28 64 62 2c 20 73 41 67  e3DbFree(db, sAg
35cc8 67 49 6e 66 6f 2e 61 46 75 6e 63 29 3b 0a 20 20  gInfo.aFunc);.  
35cc9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69  return rc;.}..#i
35cca 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
35ccb 5f 45 4e 41 42 4c 45 5f 54 52 45 45 5f 45 58 50  _ENABLE_TREE_EXP
35ccc 4c 41 49 4e 29 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  LAIN)./*.** Gene
35ccd 72 61 74 65 20 61 20 68 75 6d 61 6e 2d 72 65 61  rate a human-rea
35cce 64 61 62 6c 65 20 64 65 73 63 72 69 70 74 69 6f  dable descriptio
35ccf 6e 20 6f 66 20 61 20 74 68 65 20 53 65 6c 65 63  n of a the Selec
35cd0 74 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61  t object..*/.sta
35cd1 74 69 63 20 76 6f 69 64 20 65 78 70 6c 61 69 6e  tic void explain
35cd2 4f 6e 65 53 65 6c 65 63 74 28 56 64 62 65 20 2a  OneSelect(Vdbe *
35cd3 70 56 64 62 65 2c 20 53 65 6c 65 63 74 20 2a 70  pVdbe, Select *p
35cd4 29 7b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 6c  ){.  sqlite3Expl
35cd5 61 69 6e 50 72 69 6e 74 66 28 70 56 64 62 65 2c  ainPrintf(pVdbe,
35cd6 20 22 53 45 4c 45 43 54 20 22 29 3b 0a 20 20 69   "SELECT ");.  i
35cd7 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  f( p->selFlags &
35cd8 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46   (SF_Distinct|SF
35cd9 5f 41 67 67 72 65 67 61 74 65 29 20 29 7b 0a 20  _Aggregate) ){. 
35cda 20 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61     if( p->selFla
35cdb 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74  gs & SF_Distinct
35cdc 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
35cdd 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70  3ExplainPrintf(p
35cde 56 64 62 65 2c 20 22 44 49 53 54 49 4e 43 54 20  Vdbe, "DISTINCT 
35cdf 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ");.    }.    if
35ce0 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20  ( p->selFlags & 
35ce1 53 46 5f 41 67 67 72 65 67 61 74 65 20 29 7b 0a  SF_Aggregate ){.
35ce2 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
35ce3 6c 61 69 6e 50 72 69 6e 74 66 28 70 56 64 62 65  lainPrintf(pVdbe
35ce4 2c 20 22 61 67 67 5f 66 6c 61 67 20 22 29 3b 0a  , "agg_flag ");.
35ce5 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
35ce6 33 45 78 70 6c 61 69 6e 4e 4c 28 70 56 64 62 65  3ExplainNL(pVdbe
35ce7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
35ce8 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 56 64 62  plainPrintf(pVdb
35ce9 65 2c 20 22 20 20 20 22 29 3b 0a 20 20 7d 0a 20  e, "   ");.  }. 
35cea 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45   sqlite3ExplainE
35ceb 78 70 72 4c 69 73 74 28 70 56 64 62 65 2c 20 70  xprList(pVdbe, p
35cec 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c  ->pEList);.  sql
35ced 69 74 65 33 45 78 70 6c 61 69 6e 4e 4c 28 70 56  ite3ExplainNL(pV
35cee 64 62 65 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  dbe);.  if( p->p
35cef 53 72 63 20 26 26 20 70 2d 3e 70 53 72 63 2d 3e  Src && p->pSrc->
35cf0 6e 53 72 63 20 29 7b 0a 20 20 20 20 69 6e 74 20  nSrc ){.    int 
35cf1 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  i;.    sqlite3Ex
35cf2 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 56 64 62  plainPrintf(pVdb
35cf3 65 2c 20 22 46 52 4f 4d 20 22 29 3b 0a 20 20 20  e, "FROM ");.   
35cf4 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50   sqlite3ExplainP
35cf5 75 73 68 28 70 56 64 62 65 29 3b 0a 20 20 20 20  ush(pVdbe);.    
35cf6 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53  for(i=0; i<p->pS
35cf7 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a  rc->nSrc; i++){.
35cf8 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63        struct Src
35cf9 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
35cfa 20 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69   = &p->pSrc->a[i
35cfb 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ];.      sqlite3
35cfc 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 56  ExplainPrintf(pV
35cfd 64 62 65 2c 20 22 7b 25 64 2c 2a 7d 20 3d 20 22  dbe, "{%d,*} = "
35cfe 2c 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  , pItem->iCursor
35cff 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74  );.      if( pIt
35d00 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  em->pSelect ){. 
35d01 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
35d02 70 6c 61 69 6e 53 65 6c 65 63 74 28 70 56 64 62  plainSelect(pVdb
35d03 65 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  e, pItem->pSelec
35d04 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t);.        if( 
35d05 70 49 74 65 6d 2d 3e 70 54 61 62 20 29 7b 0a 20  pItem->pTab ){. 
35d06 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
35d07 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 56  ExplainPrintf(pV
35d08 64 62 65 2c 20 22 20 28 74 61 62 6e 61 6d 65 3d  dbe, " (tabname=
35d09 25 73 29 22 2c 20 70 49 74 65 6d 2d 3e 70 54 61  %s)", pItem->pTa
35d0a 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  b->zName);.     
35d0b 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
35d0c 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d   if( pItem->zNam
35d0d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e ){.        sql
35d0e 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74  ite3ExplainPrint
35d0f 66 28 70 56 64 62 65 2c 20 22 25 73 22 2c 20 70  f(pVdbe, "%s", p
35d10 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Item->zName);.  
35d11 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
35d12 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b  pItem->zAlias ){
35d13 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
35d14 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 56  ExplainPrintf(pV
35d15 64 62 65 2c 20 22 20 28 41 53 20 25 73 29 22 2c  dbe, " (AS %s)",
35d16 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b   pItem->zAlias);
35d17 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
35d18 66 28 20 70 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79  f( pItem->jointy
35d19 70 65 20 26 20 4a 54 5f 4c 45 46 54 20 29 7b 0a  pe & JT_LEFT ){.
35d1a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
35d1b 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 56 64  xplainPrintf(pVd
35d1c 62 65 2c 20 22 20 4c 45 46 54 2d 4a 4f 49 4e 22  be, " LEFT-JOIN"
35d1d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
35d1e 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 4e   sqlite3ExplainN
35d1f 4c 28 70 56 64 62 65 29 3b 0a 20 20 20 20 7d 0a  L(pVdbe);.    }.
35d20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61      sqlite3Expla
35d21 69 6e 50 6f 70 28 70 56 64 62 65 29 3b 0a 20 20  inPop(pVdbe);.  
35d22 7d 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65 72  }.  if( p->pWher
35d23 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
35d24 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 56  ExplainPrintf(pV
35d25 64 62 65 2c 20 22 57 48 45 52 45 20 22 29 3b 0a  dbe, "WHERE ");.
35d26 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61      sqlite3Expla
35d27 69 6e 45 78 70 72 28 70 56 64 62 65 2c 20 70 2d  inExpr(pVdbe, p-
35d28 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 73 71  >pWhere);.    sq
35d29 6c 69 74 65 33 45 78 70 6c 61 69 6e 4e 4c 28 70  lite3ExplainNL(p
35d2a 56 64 62 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28  Vdbe);.  }.  if(
35d2b 20 70 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a   p->pGroupBy ){.
35d2c 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61      sqlite3Expla
35d2d 69 6e 50 72 69 6e 74 66 28 70 56 64 62 65 2c 20  inPrintf(pVdbe, 
35d2e 22 47 52 4f 55 50 42 59 20 22 29 3b 0a 20 20 20  "GROUPBY ");.   
35d2f 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45   sqlite3ExplainE
35d30 78 70 72 4c 69 73 74 28 70 56 64 62 65 2c 20 70  xprList(pVdbe, p
35d31 2d 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 20  ->pGroupBy);.   
35d32 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 4e   sqlite3ExplainN
35d33 4c 28 70 56 64 62 65 29 3b 0a 20 20 7d 0a 20 20  L(pVdbe);.  }.  
35d34 69 66 28 20 70 2d 3e 70 48 61 76 69 6e 67 20 29  if( p->pHaving )
35d35 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
35d36 6c 61 69 6e 50 72 69 6e 74 66 28 70 56 64 62 65  lainPrintf(pVdbe
35d37 2c 20 22 48 41 56 49 4e 47 20 22 29 3b 0a 20 20  , "HAVING ");.  
35d38 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e    sqlite3Explain
35d39 45 78 70 72 28 70 56 64 62 65 2c 20 70 2d 3e 70  Expr(pVdbe, p->p
35d3a 48 61 76 69 6e 67 29 3b 0a 20 20 20 20 73 71 6c  Having);.    sql
35d3b 69 74 65 33 45 78 70 6c 61 69 6e 4e 4c 28 70 56  ite3ExplainNL(pV
35d3c 64 62 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  dbe);.  }.  if( 
35d3d 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  p->pOrderBy ){. 
35d3e 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69     sqlite3Explai
35d3f 6e 50 72 69 6e 74 66 28 70 56 64 62 65 2c 20 22  nPrintf(pVdbe, "
35d40 4f 52 44 45 52 42 59 20 22 29 3b 0a 20 20 20 20  ORDERBY ");.    
35d41 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78  sqlite3ExplainEx
35d42 70 72 4c 69 73 74 28 70 56 64 62 65 2c 20 70 2d  prList(pVdbe, p-
35d43 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20  >pOrderBy);.    
35d44 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 4e 4c  sqlite3ExplainNL
35d45 28 70 56 64 62 65 29 3b 0a 20 20 7d 0a 20 20 69  (pVdbe);.  }.  i
35d46 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a  f( p->pLimit ){.
35d47 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61      sqlite3Expla
35d48 69 6e 50 72 69 6e 74 66 28 70 56 64 62 65 2c 20  inPrintf(pVdbe, 
35d49 22 4c 49 4d 49 54 20 22 29 3b 0a 20 20 20 20 73  "LIMIT ");.    s
35d4a 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 45 78 70  qlite3ExplainExp
35d4b 72 28 70 56 64 62 65 2c 20 70 2d 3e 70 4c 69 6d  r(pVdbe, p->pLim
35d4c 69 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  it);.    sqlite3
35d4d 45 78 70 6c 61 69 6e 4e 4c 28 70 56 64 62 65 29  ExplainNL(pVdbe)
35d4e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
35d4f 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 73 71  Offset ){.    sq
35d50 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e  lite3ExplainPrin
35d51 74 66 28 70 56 64 62 65 2c 20 22 4f 46 46 53 45  tf(pVdbe, "OFFSE
35d52 54 20 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  T ");.    sqlite
35d53 33 45 78 70 6c 61 69 6e 45 78 70 72 28 70 56 64  3ExplainExpr(pVd
35d54 62 65 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 3b  be, p->pOffset);
35d55 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c  .    sqlite3Expl
35d56 61 69 6e 4e 4c 28 70 56 64 62 65 29 3b 0a 20 20  ainNL(pVdbe);.  
35d57 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  }.}.SQLITE_PRIVA
35d58 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
35d59 78 70 6c 61 69 6e 53 65 6c 65 63 74 28 56 64 62  xplainSelect(Vdb
35d5a 65 20 2a 70 56 64 62 65 2c 20 53 65 6c 65 63 74  e *pVdbe, Select
35d5b 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30   *p){.  if( p==0
35d5c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
35d5d 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 70 56 64  xplainPrintf(pVd
35d5e 62 65 2c 20 22 28 6e 75 6c 6c 2d 73 65 6c 65 63  be, "(null-selec
35d5f 74 29 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  t)");.    return
35d60 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70  ;.  }.  while( p
35d61 2d 3e 70 50 72 69 6f 72 20 29 20 70 20 3d 20 70  ->pPrior ) p = p
35d62 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 73 71 6c 69  ->pPrior;.  sqli
35d63 74 65 33 45 78 70 6c 61 69 6e 50 75 73 68 28 70  te3ExplainPush(p
35d64 56 64 62 65 29 3b 0a 20 20 77 68 69 6c 65 28 20  Vdbe);.  while( 
35d65 70 20 29 7b 0a 20 20 20 20 65 78 70 6c 61 69 6e  p ){.    explain
35d66 4f 6e 65 53 65 6c 65 63 74 28 70 56 64 62 65 2c  OneSelect(pVdbe,
35d67 20 70 29 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e   p);.    p = p->
35d68 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70  pNext;.    if( p
35d69 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
35d6a 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 4e   sqlite3ExplainN
35d6b 4c 28 70 56 64 62 65 29 3b 0a 20 20 20 20 73 71  L(pVdbe);.    sq
35d6c 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e  lite3ExplainPrin
35d6d 74 66 28 70 56 64 62 65 2c 20 22 25 73 5c 6e 22  tf(pVdbe, "%s\n"
35d6e 2c 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70  , selectOpName(p
35d6f 2d 3e 6f 70 29 29 3b 0a 20 20 7d 0a 20 20 73 71  ->op));.  }.  sq
35d70 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e  lite3ExplainPrin
35d71 74 66 28 70 56 64 62 65 2c 20 22 45 4e 44 22 29  tf(pVdbe, "END")
35d72 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61  ;.  sqlite3Expla
35d73 69 6e 50 6f 70 28 70 56 64 62 65 29 3b 0a 7d 0a  inPop(pVdbe);.}.
35d74 0a 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 73  ./* End of the s
35d75 74 72 75 63 74 75 72 65 20 64 65 62 75 67 20 70  tructure debug p
35d76 72 69 6e 74 69 6e 67 20 63 6f 64 65 0a 2a 2a 2a  rinting code.***
35d77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35d78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35d79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35d7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35d7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64  **********/.#end
35d7c 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51  if /* defined(SQ
35d7d 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 54 52 45 45  LITE_ENABLE_TREE
35d7e 5f 45 58 50 4c 41 49 4e 29 20 2a 2f 0a 0a 2f 2a  _EXPLAIN) */../*
35d7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
35d80 64 20 6f 66 20 73 65 6c 65 63 74 2e 63 20 2a 2a  d of select.c **
35d81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35d82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35d83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
35d84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
35d85 67 69 6e 20 66 69 6c 65 20 74 61 62 6c 65 2e 63  gin file table.c
35d86 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
35d87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35d88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
35d89 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
35d8a 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
35d8b 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
35d8c 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
35d8d 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
35d8e 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
35d8f 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
35d90 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
35d91 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
35d92 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
35d93 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
35d94 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
35d95 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
35d96 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
35d97 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
35d98 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
35d99 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
35d9a 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
35d9b 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
35d9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35d9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35d9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35d9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35da0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
35da1 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73  e contains the s
35da2 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
35da3 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 66  () and sqlite3_f
35da4 72 65 65 5f 74 61 62 6c 65 28 29 0a 2a 2a 20 69  ree_table().** i
35da5 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65  nterface routine
35da6 73 2e 20 20 54 68 65 73 65 20 61 72 65 20 6a 75  s.  These are ju
35da7 73 74 20 77 72 61 70 70 65 72 73 20 61 72 6f 75  st wrappers arou
35da8 6e 64 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 69  nd the main.** i
35da9 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65  nterface routine
35daa 20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63   of sqlite3_exec
35dab 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ()..**.** These 
35dac 72 6f 75 74 69 6e 65 73 20 61 72 65 20 69 6e 20  routines are in 
35dad 61 20 73 65 70 61 72 61 74 65 20 66 69 6c 65 73  a separate files
35dae 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 77 69   so that they wi
35daf 6c 6c 20 6e 6f 74 20 62 65 20 6c 69 6e 6b 65 64  ll not be linked
35db0 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72 65 20  .** if they are 
35db1 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 2f 2a 20  not used..*/./* 
35db2 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62  #include <stdlib
35db3 2e 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75  .h> */./* #inclu
35db4 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 20 2a 2f  de <string.h> */
35db5 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
35db6 5f 4f 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45 0a  _OMIT_GET_TABLE.
35db7 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75  ./*.** This stru
35db8 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f  cture is used to
35db9 20 70 61 73 73 20 64 61 74 61 20 66 72 6f 6d 20   pass data from 
35dba 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
35dbb 65 28 29 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74  e() through.** t
35dbc 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  o the callback f
35dbd 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 73 20  unction is uses 
35dbe 74 6f 20 62 75 69 6c 64 20 74 68 65 20 72 65 73  to build the res
35dbf 75 6c 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ult..*/.typedef 
35dc0 73 74 72 75 63 74 20 54 61 62 52 65 73 75 6c 74  struct TabResult
35dc1 20 7b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 52 65   {.  char **azRe
35dc2 73 75 6c 74 3b 20 20 20 2f 2a 20 41 63 63 75 6d  sult;   /* Accum
35dc3 75 6c 61 74 65 64 20 6f 75 74 70 75 74 20 2a 2f  ulated output */
35dc4 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
35dc5 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d  ;     /* Error m
35dc6 65 73 73 61 67 65 20 74 65 78 74 2c 20 69 66 20  essage text, if 
35dc7 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
35dc8 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b  */.  int nAlloc;
35dc9 20 20 20 20 20 20 20 20 2f 2a 20 53 6c 6f 74 73          /* Slots
35dca 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61   allocated for a
35dcb 7a 52 65 73 75 6c 74 5b 5d 20 2a 2f 0a 20 20 69  zResult[] */.  i
35dcc 6e 74 20 6e 52 6f 77 3b 20 20 20 20 20 20 20 20  nt nRow;        
35dcd 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
35dce 6f 77 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  ows in the resul
35dcf 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  t */.  int nColu
35dd0 6d 6e 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  mn;       /* Num
35dd1 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
35dd2 6e 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a  n the result */.
35dd3 20 20 69 6e 74 20 6e 44 61 74 61 3b 20 20 20 20    int nData;    
35dd4 20 20 20 20 20 2f 2a 20 53 6c 6f 74 73 20 75 73       /* Slots us
35dd5 65 64 20 69 6e 20 61 7a 52 65 73 75 6c 74 5b 5d  ed in azResult[]
35dd6 2e 20 20 28 6e 52 6f 77 2b 31 29 2a 6e 43 6f 6c  .  (nRow+1)*nCol
35dd7 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  umn */.  int rc;
35dd8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
35dd9 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
35dda 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 2a  sqlite3_exec() *
35ddb 2f 0a 7d 20 54 61 62 52 65 73 75 6c 74 3b 0a 0a  /.} TabResult;..
35ddc 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
35ddd 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63  ne is called onc
35dde 65 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69  e for each row i
35ddf 6e 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62  n the result tab
35de0 6c 65 2e 20 20 49 74 73 20 6a 6f 62 0a 2a 2a 20  le.  Its job.** 
35de1 69 73 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68  is to fill in th
35de2 65 20 54 61 62 52 65 73 75 6c 74 20 73 74 72 75  e TabResult stru
35de3 63 74 75 72 65 20 61 70 70 72 6f 70 72 69 61 74  cture appropriat
35de4 65 6c 79 2c 20 61 6c 6c 6f 63 61 74 69 6e 67 20  ely, allocating 
35de5 6e 65 77 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 73  new.** memory as
35de6 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73   necessary..*/.s
35de7 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
35de8 33 5f 67 65 74 5f 74 61 62 6c 65 5f 63 62 28 76  3_get_table_cb(v
35de9 6f 69 64 20 2a 70 41 72 67 2c 20 69 6e 74 20 6e  oid *pArg, int n
35dea 43 6f 6c 2c 20 63 68 61 72 20 2a 2a 61 72 67 76  Col, char **argv
35deb 2c 20 63 68 61 72 20 2a 2a 63 6f 6c 76 29 7b 0a  , char **colv){.
35dec 20 20 54 61 62 52 65 73 75 6c 74 20 2a 70 20 3d    TabResult *p =
35ded 20 28 54 61 62 52 65 73 75 6c 74 2a 29 70 41 72   (TabResult*)pAr
35dee 67 3b 20 20 2f 2a 20 52 65 73 75 6c 74 20 61 63  g;  /* Result ac
35def 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 69  cumulator */.  i
35df0 6e 74 20 6e 65 65 64 3b 20 20 20 20 20 20 20 20  nt need;        
35df1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35df2 20 2f 2a 20 53 6c 6f 74 73 20 6e 65 65 64 65 64   /* Slots needed
35df3 20 69 6e 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b   in p->azResult[
35df4 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  ] */.  int i;   
35df5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35df6 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
35df7 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63 68   counter */.  ch
35df8 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20  ar *z;          
35df9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35dfa 2f 2a 20 41 20 73 69 6e 67 6c 65 20 63 6f 6c 75  /* A single colu
35dfb 6d 6e 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a  mn of result */.
35dfc 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
35dfd 74 68 65 72 65 20 69 73 20 65 6e 6f 75 67 68 20  there is enough 
35dfe 73 70 61 63 65 20 69 6e 20 70 2d 3e 61 7a 52 65  space in p->azRe
35dff 73 75 6c 74 20 74 6f 20 68 6f 6c 64 20 65 76 65  sult to hold eve
35e00 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 65 20  rything.  ** we 
35e01 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72  need to remember
35e02 20 66 72 6f 6d 20 74 68 69 73 20 69 6e 76 6f 63   from this invoc
35e03 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 61 6c  ation of the cal
35e04 6c 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66  lback..  */.  if
35e05 28 20 70 2d 3e 6e 52 6f 77 3d 3d 30 20 26 26 20  ( p->nRow==0 && 
35e06 61 72 67 76 21 3d 30 20 29 7b 0a 20 20 20 20 6e  argv!=0 ){.    n
35e07 65 65 64 20 3d 20 6e 43 6f 6c 2a 32 3b 0a 20 20  eed = nCol*2;.  
35e08 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 65 64 20  }else{.    need 
35e09 3d 20 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66  = nCol;.  }.  if
35e0a 28 20 70 2d 3e 6e 44 61 74 61 20 2b 20 6e 65 65  ( p->nData + nee
35e0b 64 20 3e 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b  d > p->nAlloc ){
35e0c 0a 20 20 20 20 63 68 61 72 20 2a 2a 61 7a 4e 65  .    char **azNe
35e0d 77 3b 0a 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63  w;.    p->nAlloc
35e0e 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b   = p->nAlloc*2 +
35e0f 20 6e 65 65 64 3b 0a 20 20 20 20 61 7a 4e 65 77   need;.    azNew
35e10 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
35e11 6f 63 28 20 70 2d 3e 61 7a 52 65 73 75 6c 74 2c  oc( p->azResult,
35e12 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 70   sizeof(char*)*p
35e13 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 20 20  ->nAlloc );.    
35e14 69 66 28 20 61 7a 4e 65 77 3d 3d 30 20 29 20 67  if( azNew==0 ) g
35e15 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65  oto malloc_faile
35e16 64 3b 0a 20 20 20 20 70 2d 3e 61 7a 52 65 73 75  d;.    p->azResu
35e17 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d 0a  lt = azNew;.  }.
35e18 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
35e19 20 74 68 65 20 66 69 72 73 74 20 72 6f 77 2c 20   the first row, 
35e1a 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20 61 6e  then generate an
35e1b 20 65 78 74 72 61 20 72 6f 77 20 63 6f 6e 74 61   extra row conta
35e1c 69 6e 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6e  ining.  ** the n
35e1d 61 6d 65 73 20 6f 66 20 61 6c 6c 20 63 6f 6c 75  ames of all colu
35e1e 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  mns..  */.  if( 
35e1f 70 2d 3e 6e 52 6f 77 3d 3d 30 20 29 7b 0a 20 20  p->nRow==0 ){.  
35e20 20 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 6e    p->nColumn = n
35e21 43 6f 6c 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  Col;.    for(i=0
35e22 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ; i<nCol; i++){.
35e23 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65        z = sqlite
35e24 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20  3_mprintf("%s", 
35e25 63 6f 6c 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20  colv[i]);.      
35e26 69 66 28 20 7a 3d 3d 30 20 29 20 67 6f 74 6f 20  if( z==0 ) goto 
35e27 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20  malloc_failed;. 
35e28 20 20 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74       p->azResult
35e29 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20 7a  [p->nData++] = z
35e2a 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
35e2b 69 66 28 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d  if( p->nColumn!=
35e2c 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69  nCol ){.    sqli
35e2d 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 45 72 72  te3_free(p->zErr
35e2e 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72  Msg);.    p->zEr
35e2f 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d  rMsg = sqlite3_m
35e30 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20 20 22  printf(.       "
35e31 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
35e32 65 28 29 20 63 61 6c 6c 65 64 20 77 69 74 68 20  e() called with 
35e33 74 77 6f 20 6f 72 20 6d 6f 72 65 20 69 6e 63 6f  two or more inco
35e34 6d 70 61 74 69 62 6c 65 20 71 75 65 72 69 65 73  mpatible queries
35e35 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 70 2d 3e  ".    );.    p->
35e36 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
35e37 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  R;.    return 1;
35e38 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20  .  }..  /* Copy 
35e39 6f 76 65 72 20 74 68 65 20 72 6f 77 20 64 61 74  over the row dat
35e3a 61 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 72 67  a.  */.  if( arg
35e3b 76 21 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72 28  v!=0 ){.    for(
35e3c 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
35e3d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 72 67  ){.      if( arg
35e3e 76 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  v[i]==0 ){.     
35e3f 20 20 20 7a 20 3d 20 30 3b 0a 20 20 20 20 20 20     z = 0;.      
35e40 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69  }else{.        i
35e41 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  nt n = sqlite3St
35e42 72 6c 65 6e 33 30 28 61 72 67 76 5b 69 5d 29 2b  rlen30(argv[i])+
35e43 31 3b 0a 20 20 20 20 20 20 20 20 7a 20 3d 20 73  1;.        z = s
35e44 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e  qlite3_malloc( n
35e45 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
35e46 7a 3d 3d 30 20 29 20 67 6f 74 6f 20 6d 61 6c 6c  z==0 ) goto mall
35e47 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 20  oc_failed;.     
35e48 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 61 72 67     memcpy(z, arg
35e49 76 5b 69 5d 2c 20 6e 29 3b 0a 20 20 20 20 20 20  v[i], n);.      
35e4a 7d 0a 20 20 20 20 20 20 70 2d 3e 61 7a 52 65 73  }.      p->azRes
35e4b 75 6c 74 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20  ult[p->nData++] 
35e4c 3d 20 7a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  = z;.    }.    p
35e4d 2d 3e 6e 52 6f 77 2b 2b 3b 0a 20 20 7d 0a 20 20  ->nRow++;.  }.  
35e4e 72 65 74 75 72 6e 20 30 3b 0a 0a 6d 61 6c 6c 6f  return 0;..mallo
35e4f 63 5f 66 61 69 6c 65 64 3a 0a 20 20 70 2d 3e 72  c_failed:.  p->r
35e50 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
35e51 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  ;.  return 1;.}.
35e52 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65  ./*.** Query the
35e53 20 64 61 74 61 62 61 73 65 2e 20 20 42 75 74 20   database.  But 
35e54 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b  instead of invok
35e55 69 6e 67 20 61 20 63 61 6c 6c 62 61 63 6b 20 66  ing a callback f
35e56 6f 72 20 65 61 63 68 20 72 6f 77 2c 0a 2a 2a 20  or each row,.** 
35e57 6d 61 6c 6c 6f 63 28 29 20 66 6f 72 20 73 70 61  malloc() for spa
35e58 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
35e59 65 73 75 6c 74 20 61 6e 64 20 72 65 74 75 72 6e  esult and return
35e5a 20 74 68 65 20 65 6e 74 69 72 65 20 72 65 73 75   the entire resu
35e5b 6c 74 73 0a 2a 2a 20 61 74 20 74 68 65 20 63 6f  lts.** at the co
35e5c 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 74 68 65 20  nclusion of the 
35e5d 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  call..**.** The 
35e5e 72 65 73 75 6c 74 20 74 68 61 74 20 69 73 20 77  result that is w
35e5f 72 69 74 74 65 6e 20 74 6f 20 2a 2a 2a 70 61 7a  ritten to ***paz
35e60 52 65 73 75 6c 74 20 69 73 20 68 65 6c 64 20 69  Result is held i
35e61 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  n memory obtaine
35e62 64 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  d.** from malloc
35e63 28 29 2e 20 20 42 75 74 20 74 68 65 20 63 61 6c  ().  But the cal
35e64 6c 65 72 20 63 61 6e 6e 6f 74 20 66 72 65 65 20  ler cannot free 
35e65 74 68 69 73 20 6d 65 6d 6f 72 79 20 64 69 72 65  this memory dire
35e66 63 74 6c 79 2e 20 20 0a 2a 2a 20 49 6e 73 74 65  ctly.  .** Inste
35e67 61 64 2c 20 74 68 65 20 65 6e 74 69 72 65 20 74  ad, the entire t
35e68 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 70  able should be p
35e69 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33  assed to sqlite3
35e6a 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 20 77 68  _free_table() wh
35e6b 65 6e 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e  en.** the callin
35e6c 67 20 70 72 6f 63 65 64 75 72 65 20 69 73 20 66  g procedure is f
35e6d 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 69 74  inished using it
35e6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
35e6f 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  int sqlite3_get_
35e70 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  table(.  sqlite3
35e71 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
35e72 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
35e73 62 61 73 65 20 6f 6e 20 77 68 69 63 68 20 74 68  base on which th
35e74 65 20 53 51 4c 20 65 78 65 63 75 74 65 73 20 2a  e SQL executes *
35e75 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
35e76 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20  zSql,           
35e77 2f 2a 20 54 68 65 20 53 51 4c 20 74 6f 20 62 65  /* The SQL to be
35e78 20 65 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 63   executed */.  c
35e79 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74  har ***pazResult
35e7a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72  ,          /* Wr
35e7b 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 74  ite the result t
35e7c 61 62 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69  able here */.  i
35e7d 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20  nt *pnRow,      
35e7e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
35e7f 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ite the number o
35e80 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 72 65  f rows in the re
35e81 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a 20 20 69  sult here */.  i
35e82 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20  nt *pnColumn,   
35e83 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
35e84 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ite the number o
35e85 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 72 65 73  f columns of res
35e86 75 6c 74 20 68 65 72 65 20 2a 2f 0a 20 20 63 68  ult here */.  ch
35e87 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20  ar **pzErrMsg   
35e88 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
35e89 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
35e8a 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  s here */.){.  i
35e8b 6e 74 20 72 63 3b 0a 20 20 54 61 62 52 65 73 75  nt rc;.  TabResu
35e8c 6c 74 20 72 65 73 3b 0a 0a 20 20 2a 70 61 7a 52  lt res;..  *pazR
35e8d 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20 69 66 28  esult = 0;.  if(
35e8e 20 70 6e 43 6f 6c 75 6d 6e 20 29 20 2a 70 6e 43   pnColumn ) *pnC
35e8f 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 69 66 28  olumn = 0;.  if(
35e90 20 70 6e 52 6f 77 20 29 20 2a 70 6e 52 6f 77 20   pnRow ) *pnRow 
35e91 3d 20 30 3b 0a 20 20 69 66 28 20 70 7a 45 72 72  = 0;.  if( pzErr
35e92 4d 73 67 20 29 20 2a 70 7a 45 72 72 4d 73 67 20  Msg ) *pzErrMsg 
35e93 3d 20 30 3b 0a 20 20 72 65 73 2e 7a 45 72 72 4d  = 0;.  res.zErrM
35e94 73 67 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 52  sg = 0;.  res.nR
35e95 6f 77 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 43  ow = 0;.  res.nC
35e96 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 72 65 73  olumn = 0;.  res
35e97 2e 6e 44 61 74 61 20 3d 20 31 3b 0a 20 20 72 65  .nData = 1;.  re
35e98 73 2e 6e 41 6c 6c 6f 63 20 3d 20 32 30 3b 0a 20  s.nAlloc = 20;. 
35e99 20 72 65 73 2e 72 63 20 3d 20 53 51 4c 49 54 45   res.rc = SQLITE
35e9a 5f 4f 4b 3b 0a 20 20 72 65 73 2e 61 7a 52 65 73  _OK;.  res.azRes
35e9b 75 6c 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  ult = sqlite3_ma
35e9c 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 63 68 61 72  lloc(sizeof(char
35e9d 2a 29 2a 72 65 73 2e 6e 41 6c 6c 6f 63 20 29 3b  *)*res.nAlloc );
35e9e 0a 20 20 69 66 28 20 72 65 73 2e 61 7a 52 65 73  .  if( res.azRes
35e9f 75 6c 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 64  ult==0 ){.     d
35ea0 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c  b->errCode = SQL
35ea1 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
35ea2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
35ea3 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 73 2e 61  MEM;.  }.  res.a
35ea4 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 30 3b 0a  zResult[0] = 0;.
35ea5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65    rc = sqlite3_e
35ea6 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 73 71  xec(db, zSql, sq
35ea7 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 5f  lite3_get_table_
35ea8 63 62 2c 20 26 72 65 73 2c 20 70 7a 45 72 72 4d  cb, &res, pzErrM
35ea9 73 67 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  sg);.  assert( s
35eaa 69 7a 65 6f 66 28 72 65 73 2e 61 7a 52 65 73 75  izeof(res.azResu
35eab 6c 74 5b 30 5d 29 3e 3d 20 73 69 7a 65 6f 66 28  lt[0])>= sizeof(
35eac 72 65 73 2e 6e 44 61 74 61 29 20 29 3b 0a 20 20  res.nData) );.  
35ead 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d 20  res.azResult[0] 
35eae 3d 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  = SQLITE_INT_TO_
35eaf 50 54 52 28 72 65 73 2e 6e 44 61 74 61 29 3b 0a  PTR(res.nData);.
35eb0 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29 3d    if( (rc&0xff)=
35eb1 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b  =SQLITE_ABORT ){
35eb2 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
35eb3 65 5f 74 61 62 6c 65 28 26 72 65 73 2e 61 7a 52  e_table(&res.azR
35eb4 65 73 75 6c 74 5b 31 5d 29 3b 0a 20 20 20 20 69  esult[1]);.    i
35eb5 66 28 20 72 65 73 2e 7a 45 72 72 4d 73 67 20 29  f( res.zErrMsg )
35eb6 7b 0a 20 20 20 20 20 20 69 66 28 20 70 7a 45 72  {.      if( pzEr
35eb7 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20  rMsg ){.        
35eb8 73 71 6c 69 74 65 33 5f 66 72 65 65 28 2a 70 7a  sqlite3_free(*pz
35eb9 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20  ErrMsg);.       
35eba 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c   *pzErrMsg = sql
35ebb 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73  ite3_mprintf("%s
35ebc 22 2c 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a  ",res.zErrMsg);.
35ebd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
35ebe 6c 69 74 65 33 5f 66 72 65 65 28 72 65 73 2e 7a  lite3_free(res.z
35ebf 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20  ErrMsg);.    }. 
35ec0 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d     db->errCode =
35ec1 20 72 65 73 2e 72 63 3b 20 20 2f 2a 20 41 73 73   res.rc;  /* Ass
35ec2 75 6d 65 20 33 32 2d 62 69 74 20 61 73 73 69 67  ume 32-bit assig
35ec3 6e 6d 65 6e 74 20 69 73 20 61 74 6f 6d 69 63 20  nment is atomic 
35ec4 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 72 65  */.    return re
35ec5 73 2e 72 63 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  s.rc;.  }.  sqli
35ec6 74 65 33 5f 66 72 65 65 28 72 65 73 2e 7a 45 72  te3_free(res.zEr
35ec7 72 4d 73 67 29 3b 0a 20 20 69 66 28 20 72 63 21  rMsg);.  if( rc!
35ec8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
35ec9 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74    sqlite3_free_t
35eca 61 62 6c 65 28 26 72 65 73 2e 61 7a 52 65 73 75  able(&res.azResu
35ecb 6c 74 5b 31 5d 29 3b 0a 20 20 20 20 72 65 74 75  lt[1]);.    retu
35ecc 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28  rn rc;.  }.  if(
35ecd 20 72 65 73 2e 6e 41 6c 6c 6f 63 3e 72 65 73 2e   res.nAlloc>res.
35ece 6e 44 61 74 61 20 29 7b 0a 20 20 20 20 63 68 61  nData ){.    cha
35ecf 72 20 2a 2a 61 7a 4e 65 77 3b 0a 20 20 20 20 61  r **azNew;.    a
35ed0 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72  zNew = sqlite3_r
35ed1 65 61 6c 6c 6f 63 28 20 72 65 73 2e 61 7a 52 65  ealloc( res.azRe
35ed2 73 75 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68 61  sult, sizeof(cha
35ed3 72 2a 29 2a 72 65 73 2e 6e 44 61 74 61 20 29 3b  r*)*res.nData );
35ed4 0a 20 20 20 20 69 66 28 20 61 7a 4e 65 77 3d 3d  .    if( azNew==
35ed5 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
35ed6 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72  e3_free_table(&r
35ed7 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b  es.azResult[1]);
35ed8 0a 20 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f  .      db->errCo
35ed9 64 65 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  de = SQLITE_NOME
35eda 4d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  M;.      return 
35edb 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
35edc 20 20 7d 0a 20 20 20 20 72 65 73 2e 61 7a 52 65    }.    res.azRe
35edd 73 75 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20 20  sult = azNew;.  
35ede 7d 0a 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d  }.  *pazResult =
35edf 20 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31   &res.azResult[1
35ee0 5d 3b 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75 6d  ];.  if( pnColum
35ee1 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20  n ) *pnColumn = 
35ee2 72 65 73 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69  res.nColumn;.  i
35ee3 66 28 20 70 6e 52 6f 77 20 29 20 2a 70 6e 52 6f  f( pnRow ) *pnRo
35ee4 77 20 3d 20 72 65 73 2e 6e 52 6f 77 3b 0a 20 20  w = res.nRow;.  
35ee5 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
35ee6 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
35ee7 20 66 72 65 65 73 20 74 68 65 20 73 70 61 63 65   frees the space
35ee8 20 74 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74   the sqlite3_get
35ee9 5f 74 61 62 6c 65 28 29 20 6d 61 6c 6c 6f 63 65  _table() malloce
35eea 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
35eeb 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72   void sqlite3_fr
35eec 65 65 5f 74 61 62 6c 65 28 0a 20 20 63 68 61 72  ee_table(.  char
35eed 20 2a 2a 61 7a 52 65 73 75 6c 74 20 20 20 20 20   **azResult     
35eee 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
35eef 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 66   returned from f
35ef0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  rom sqlite3_get_
35ef1 74 61 62 6c 65 28 29 20 2a 2f 0a 29 7b 0a 20 20  table() */.){.  
35ef2 69 66 28 20 61 7a 52 65 73 75 6c 74 20 29 7b 0a  if( azResult ){.
35ef3 20 20 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20      int i, n;.  
35ef4 20 20 61 7a 52 65 73 75 6c 74 2d 2d 3b 0a 20 20    azResult--;.  
35ef5 20 20 61 73 73 65 72 74 28 20 61 7a 52 65 73 75    assert( azResu
35ef6 6c 74 21 3d 30 20 29 3b 0a 20 20 20 20 6e 20 3d  lt!=0 );.    n =
35ef7 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49   SQLITE_PTR_TO_I
35ef8 4e 54 28 61 7a 52 65 73 75 6c 74 5b 30 5d 29 3b  NT(azResult[0]);
35ef9 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  .    for(i=1; i<
35efa 6e 3b 20 69 2b 2b 29 7b 20 69 66 28 20 61 7a 52  n; i++){ if( azR
35efb 65 73 75 6c 74 5b 69 5d 20 29 20 73 71 6c 69 74  esult[i] ) sqlit
35efc 65 33 5f 66 72 65 65 28 61 7a 52 65 73 75 6c 74  e3_free(azResult
35efd 5b 69 5d 29 3b 20 7d 0a 20 20 20 20 73 71 6c 69  [i]); }.    sqli
35efe 74 65 33 5f 66 72 65 65 28 61 7a 52 65 73 75 6c  te3_free(azResul
35eff 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69  t);.  }.}..#endi
35f00 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
35f01 5f 47 45 54 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f  _GET_TABLE */../
35f02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
35f03 6e 64 20 6f 66 20 74 61 62 6c 65 2e 63 20 2a 2a  nd of table.c **
35f04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35f05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35f06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
35f07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
35f08 65 67 69 6e 20 66 69 6c 65 20 74 72 69 67 67 65  egin file trigge
35f09 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
35f0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35f0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
35f0c 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  *.**.** The auth
35f0d 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
35f0e 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
35f0f 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
35f10 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
35f11 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
35f12 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
35f13 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
35f14 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
35f15 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
35f16 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
35f17 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
35f18 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
35f19 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
35f1a 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
35f1b 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
35f1c 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
35f1d 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
35f1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35f1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35f21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35f22 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
35f23 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65  ntains the imple
35f24 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 54 52  mentation for TR
35f25 49 47 47 45 52 73 0a 2a 2f 0a 0a 23 69 66 6e 64  IGGERs.*/..#ifnd
35f26 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
35f27 52 49 47 47 45 52 0a 2f 2a 0a 2a 2a 20 44 65 6c  RIGGER./*.** Del
35f28 65 74 65 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  ete a linked lis
35f29 74 20 6f 66 20 54 72 69 67 67 65 72 53 74 65 70  t of TriggerStep
35f2a 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a   structures..*/.
35f2b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
35f2c 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74  oid sqlite3Delet
35f2d 65 54 72 69 67 67 65 72 53 74 65 70 28 73 71 6c  eTriggerStep(sql
35f2e 69 74 65 33 20 2a 64 62 2c 20 54 72 69 67 67 65  ite3 *db, Trigge
35f2f 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72 53  rStep *pTriggerS
35f30 74 65 70 29 7b 0a 20 20 77 68 69 6c 65 28 20 70  tep){.  while( p
35f31 54 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20  TriggerStep ){. 
35f32 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a     TriggerStep *
35f33 20 70 54 6d 70 20 3d 20 70 54 72 69 67 67 65 72   pTmp = pTrigger
35f34 53 74 65 70 3b 0a 20 20 20 20 70 54 72 69 67 67  Step;.    pTrigg
35f35 65 72 53 74 65 70 20 3d 20 70 54 72 69 67 67 65  erStep = pTrigge
35f36 72 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a 0a 20  rStep->pNext;.. 
35f37 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
35f38 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70  lete(db, pTmp->p
35f39 57 68 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69  Where);.    sqli
35f3a 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
35f3b 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70 45 78 70  e(db, pTmp->pExp
35f3c 72 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  rList);.    sqli
35f3d 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
35f3e 64 62 2c 20 70 54 6d 70 2d 3e 70 53 65 6c 65 63  db, pTmp->pSelec
35f3f 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49  t);.    sqlite3I
35f40 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  dListDelete(db, 
35f41 70 54 6d 70 2d 3e 70 49 64 4c 69 73 74 29 3b 0a  pTmp->pIdList);.
35f42 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
35f43 65 65 28 64 62 2c 20 70 54 6d 70 29 3b 0a 20 20  ee(db, pTmp);.  
35f44 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  }.}../*.** Given
35f45 20 74 61 62 6c 65 20 70 54 61 62 2c 20 72 65 74   table pTab, ret
35f46 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c  urn a list of al
35f47 6c 20 74 68 65 20 74 72 69 67 67 65 72 73 20 61  l the triggers a
35f48 74 74 61 63 68 65 64 20 74 6f 20 0a 2a 2a 20 74  ttached to .** t
35f49 68 65 20 74 61 62 6c 65 2e 20 54 68 65 20 6c 69  he table. The li
35f4a 73 74 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20  st is connected 
35f4b 62 79 20 54 72 69 67 67 65 72 2e 70 4e 65 78 74  by Trigger.pNext
35f4c 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a   pointers..**.**
35f4d 20 41 6c 6c 20 6f 66 20 74 68 65 20 74 72 69 67   All of the trig
35f4e 67 65 72 73 20 6f 6e 20 70 54 61 62 20 74 68 61  gers on pTab tha
35f4f 74 20 61 72 65 20 69 6e 20 74 68 65 20 73 61 6d  t are in the sam
35f50 65 20 64 61 74 61 62 61 73 65 20 61 73 20 70 54  e database as pT
35f51 61 62 0a 2a 2a 20 61 72 65 20 61 6c 72 65 61 64  ab.** are alread
35f52 79 20 61 74 74 61 63 68 65 64 20 74 6f 20 70 54  y attached to pT
35f53 61 62 2d 3e 70 54 72 69 67 67 65 72 2e 20 20 42  ab->pTrigger.  B
35f54 75 74 20 74 68 65 72 65 20 6d 69 67 68 74 20 62  ut there might b
35f55 65 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  e additional.** 
35f56 74 72 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62  triggers on pTab
35f57 20 69 6e 20 74 68 65 20 54 45 4d 50 20 73 63 68   in the TEMP sch
35f58 65 6d 61 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ema.  This routi
35f59 6e 65 20 70 72 65 70 65 6e 64 73 20 61 6c 6c 0a  ne prepends all.
35f5a 2a 2a 20 54 45 4d 50 20 74 72 69 67 67 65 72 73  ** TEMP triggers
35f5b 20 6f 6e 20 70 54 61 62 20 74 6f 20 74 68 65 20   on pTab to the 
35f5c 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
35f5d 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 20   pTab->pTrigger 
35f5e 6c 69 73 74 0a 2a 2a 20 61 6e 64 20 72 65 74 75  list.** and retu
35f5f 72 6e 73 20 74 68 65 20 63 6f 6d 62 69 6e 65 64  rns the combined
35f60 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20   list..**.** To 
35f61 73 74 61 74 65 20 69 74 20 61 6e 6f 74 68 65 72  state it another
35f62 20 77 61 79 3a 20 20 54 68 69 73 20 72 6f 75 74   way:  This rout
35f63 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 6c 69  ine returns a li
35f64 73 74 20 6f 66 20 61 6c 6c 20 74 72 69 67 67 65  st of all trigge
35f65 72 73 0a 2a 2a 20 74 68 61 74 20 66 69 72 65 20  rs.** that fire 
35f66 6f 66 66 20 6f 66 20 70 54 61 62 2e 20 20 54 68  off of pTab.  Th
35f67 65 20 6c 69 73 74 20 77 69 6c 6c 20 69 6e 63 6c  e list will incl
35f68 75 64 65 20 61 6e 79 20 54 45 4d 50 20 74 72 69  ude any TEMP tri
35f69 67 67 65 72 73 20 6f 6e 0a 2a 2a 20 70 54 61 62  ggers on.** pTab
35f6a 20 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20   as well as the 
35f6b 74 72 69 67 67 65 72 73 20 6c 69 73 65 64 20 69  triggers lised i
35f6c 6e 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72  n pTab->pTrigger
35f6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
35f6e 41 54 45 20 54 72 69 67 67 65 72 20 2a 73 71 6c  ATE Trigger *sql
35f6f 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28  ite3TriggerList(
35f70 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
35f71 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 53  able *pTab){.  S
35f72 63 68 65 6d 61 20 2a 20 63 6f 6e 73 74 20 70 54  chema * const pT
35f73 6d 70 53 63 68 65 6d 61 20 3d 20 70 50 61 72 73  mpSchema = pPars
35f74 65 2d 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53  e->db->aDb[1].pS
35f75 63 68 65 6d 61 3b 0a 20 20 54 72 69 67 67 65 72  chema;.  Trigger
35f76 20 2a 70 4c 69 73 74 20 3d 20 30 3b 20 20 20 20   *pList = 0;    
35f77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35f78 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72   List of trigger
35f79 73 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a  s to return */..
35f7a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 69    if( pParse->di
35f7b 73 61 62 6c 65 54 72 69 67 67 65 72 73 20 29 7b  sableTriggers ){
35f7c 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
35f7d 20 7d 0a 0a 20 20 69 66 28 20 70 54 6d 70 53 63   }..  if( pTmpSc
35f7e 68 65 6d 61 21 3d 70 54 61 62 2d 3e 70 53 63 68  hema!=pTab->pSch
35f7f 65 6d 61 20 29 7b 0a 20 20 20 20 48 61 73 68 45  ema ){.    HashE
35f80 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 61 73 73 65  lem *p;.    asse
35f81 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d  rt( sqlite3Schem
35f82 61 4d 75 74 65 78 48 65 6c 64 28 70 50 61 72 73  aMutexHeld(pPars
35f83 65 2d 3e 64 62 2c 20 30 2c 20 70 54 6d 70 53 63  e->db, 0, pTmpSc
35f84 68 65 6d 61 29 20 29 3b 0a 20 20 20 20 66 6f 72  hema) );.    for
35f85 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (p=sqliteHashFir
35f86 73 74 28 26 70 54 6d 70 53 63 68 65 6d 61 2d 3e  st(&pTmpSchema->
35f87 74 72 69 67 48 61 73 68 29 3b 20 70 3b 20 70 3d  trigHash); p; p=
35f88 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
35f89 29 29 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65  )){.      Trigge
35f8a 72 20 2a 70 54 72 69 67 20 3d 20 28 54 72 69 67  r *pTrig = (Trig
35f8b 67 65 72 20 2a 29 73 71 6c 69 74 65 48 61 73 68  ger *)sqliteHash
35f8c 44 61 74 61 28 70 29 3b 0a 20 20 20 20 20 20 69  Data(p);.      i
35f8d 66 28 20 70 54 72 69 67 2d 3e 70 54 61 62 53 63  f( pTrig->pTabSc
35f8e 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68  hema==pTab->pSch
35f8f 65 6d 61 0a 20 20 20 20 20 20 20 26 26 20 30 3d  ema.       && 0=
35f90 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28  =sqlite3StrICmp(
35f91 70 54 72 69 67 2d 3e 74 61 62 6c 65 2c 20 70 54  pTrig->table, pT
35f92 61 62 2d 3e 7a 4e 61 6d 65 29 20 0a 20 20 20 20  ab->zName) .    
35f93 20 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 72    ){.        pTr
35f94 69 67 2d 3e 70 4e 65 78 74 20 3d 20 28 70 4c 69  ig->pNext = (pLi
35f95 73 74 20 3f 20 70 4c 69 73 74 20 3a 20 70 54 61  st ? pList : pTa
35f96 62 2d 3e 70 54 72 69 67 67 65 72 29 3b 0a 20 20  b->pTrigger);.  
35f97 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20 70 54        pList = pT
35f98 72 69 67 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rig;.      }.   
35f99 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
35f9a 20 28 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 20   (pList ? pList 
35f9b 3a 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72  : pTab->pTrigger
35f9c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
35f9d 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
35f9e 65 20 70 61 72 73 65 72 20 77 68 65 6e 20 69 74  e parser when it
35f9f 20 73 65 65 73 20 61 20 43 52 45 41 54 45 20 54   sees a CREATE T
35fa0 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74  RIGGER statement
35fa1 0a 2a 2a 20 75 70 20 74 6f 20 74 68 65 20 70 6f  .** up to the po
35fa2 69 6e 74 20 6f 66 20 74 68 65 20 42 45 47 49 4e  int of the BEGIN
35fa3 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 67   before the trig
35fa4 67 65 72 20 61 63 74 69 6f 6e 73 2e 20 20 41 20  ger actions.  A 
35fa5 54 72 69 67 67 65 72 0a 2a 2a 20 73 74 72 75 63  Trigger.** struc
35fa6 74 75 72 65 20 69 73 20 67 65 6e 65 72 61 74 65  ture is generate
35fa7 64 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 69  d based on the i
35fa8 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 76 61 69 6c  nformation avail
35fa9 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 64 0a  able and stored.
35faa 2a 2a 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e  ** in pParse->pN
35fab 65 77 54 72 69 67 67 65 72 2e 20 20 41 66 74 65  ewTrigger.  Afte
35fac 72 20 74 68 65 20 74 72 69 67 67 65 72 20 61 63  r the trigger ac
35fad 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20  tions have been 
35fae 70 61 72 73 65 64 2c 20 74 68 65 0a 2a 2a 20 73  parsed, the.** s
35faf 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67  qlite3FinishTrig
35fb0 67 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ger() function i
35fb1 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70  s called to comp
35fb2 6c 65 74 65 20 74 68 65 20 74 72 69 67 67 65 72  lete the trigger
35fb3 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  .** construction
35fb4 20 70 72 6f 63 65 73 73 2e 0a 2a 2f 0a 53 51 4c   process..*/.SQL
35fb5 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
35fb6 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69   sqlite3BeginTri
35fb7 67 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70  gger(.  Parse *p
35fb8 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54  Parse,      /* T
35fb9 68 65 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74  he parse context
35fba 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54   of the CREATE T
35fbb 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74  RIGGER statement
35fbc 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
35fbd 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  me1,      /* The
35fbe 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69   name of the tri
35fbf 67 67 65 72 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  gger */.  Token 
35fc0 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a  *pName2,      /*
35fc1 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   The name of the
35fc2 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 69 6e   trigger */.  in
35fc3 74 20 74 72 5f 74 6d 2c 20 20 20 20 20 20 20 20  t tr_tm,        
35fc4 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 42    /* One of TK_B
35fc5 45 46 4f 52 45 2c 20 54 4b 5f 41 46 54 45 52 2c  EFORE, TK_AFTER,
35fc6 20 54 4b 5f 49 4e 53 54 45 41 44 20 2a 2f 0a 20   TK_INSTEAD */. 
35fc7 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20   int op,        
35fc8 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54       /* One of T
35fc9 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 55 50 44  K_INSERT, TK_UPD
35fca 41 54 45 2c 20 54 4b 5f 44 45 4c 45 54 45 20 2a  ATE, TK_DELETE *
35fcb 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c  /.  IdList *pCol
35fcc 75 6d 6e 73 2c 20 20 20 2f 2a 20 63 6f 6c 75 6d  umns,   /* colum
35fcd 6e 20 6c 69 73 74 20 69 66 20 74 68 69 73 20 69  n list if this i
35fce 73 20 61 6e 20 55 50 44 41 54 45 20 4f 46 20 74  s an UPDATE OF t
35fcf 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 72 63 4c  rigger */.  SrcL
35fd0 69 73 74 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c  ist *pTableName,
35fd1 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  /* The name of t
35fd2 68 65 20 74 61 62 6c 65 2f 76 69 65 77 20 74 68  he table/view th
35fd3 65 20 74 72 69 67 67 65 72 20 61 70 70 6c 69 65  e trigger applie
35fd4 73 20 74 6f 20 2a 2f 0a 20 20 45 78 70 72 20 2a  s to */.  Expr *
35fd5 70 57 68 65 6e 2c 20 20 20 20 20 20 20 20 2f 2a  pWhen,        /*
35fd6 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a   WHEN clause */.
35fd7 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 20 20    int isTemp,   
35fd8 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
35fd9 20 74 68 65 20 54 45 4d 50 4f 52 41 52 59 20 6b   the TEMPORARY k
35fda 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e  eyword is presen
35fdb 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72  t */.  int noErr
35fdc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75             /* Su
35fdd 70 70 72 65 73 73 20 65 72 72 6f 72 73 20 69 66  ppress errors if
35fde 20 74 68 65 20 74 72 69 67 67 65 72 20 61 6c 72   the trigger alr
35fdf 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29  eady exists */.)
35fe0 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  {.  Trigger *pTr
35fe1 69 67 67 65 72 20 3d 20 30 3b 20 20 2f 2a 20 54  igger = 0;  /* T
35fe2 68 65 20 6e 65 77 20 74 72 69 67 67 65 72 20 2a  he new trigger *
35fe3 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  /.  Table *pTab;
35fe4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
35fe5 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 74 72  able that the tr
35fe6 69 67 67 65 72 20 66 69 72 65 73 20 6f 66 66 20  igger fires off 
35fe7 6f 66 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e  of */.  char *zN
35fe8 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ame = 0;        
35fe9 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74  /* Name of the t
35fea 72 69 67 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69  rigger */.  sqli
35feb 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
35fec 2d 3e 64 62 3b 20 20 2f 2a 20 54 68 65 20 64 61  ->db;  /* The da
35fed 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
35fee 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  n */.  int iDb; 
35fef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35ff0 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 74  * The database t
35ff1 6f 20 73 74 6f 72 65 20 74 68 65 20 74 72 69 67  o store the trig
35ff2 67 65 72 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65  ger in */.  Toke
35ff3 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20 20 20 20  n *pName;       
35ff4 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 71 75 61      /* The unqua
35ff5 6c 69 66 69 65 64 20 64 62 20 6e 61 6d 65 20 2a  lified db name *
35ff6 2f 0a 20 20 44 62 46 69 78 65 72 20 73 46 69 78  /.  DbFixer sFix
35ff7 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ;           /* S
35ff8 74 61 74 65 20 76 65 63 74 6f 72 20 66 6f 72 20  tate vector for 
35ff9 74 68 65 20 44 42 20 66 69 78 65 72 20 2a 2f 0a  the DB fixer */.
35ffa 20 20 69 6e 74 20 69 54 61 62 44 62 3b 20 20 20    int iTabDb;   
35ffb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
35ffc 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
35ffd 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20  se holding pTab 
35ffe 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e  */..  assert( pN
35fff 61 6d 65 31 21 3d 30 20 29 3b 20 20 20 2f 2a 20  ame1!=0 );   /* 
36000 70 4e 61 6d 65 31 2d 3e 7a 20 6d 69 67 68 74 20  pName1->z might 
36001 62 65 20 4e 55 4c 4c 2c 20 62 75 74 20 6e 6f 74  be NULL, but not
36002 20 70 4e 61 6d 65 31 20 69 74 73 65 6c 66 20 2a   pName1 itself *
36003 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d  /.  assert( pNam
36004 65 32 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  e2!=0 );.  asser
36005 74 28 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54  t( op==TK_INSERT
36006 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 50 44 41 54   || op==TK_UPDAT
36007 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 44 45 4c 45  E || op==TK_DELE
36008 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  TE );.  assert( 
36009 6f 70 3e 30 20 26 26 20 6f 70 3c 30 78 66 66 20  op>0 && op<0xff 
3600a 29 3b 0a 20 20 69 66 28 20 69 73 54 65 6d 70 20  );.  if( isTemp 
3600b 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 54 45 4d  ){.    /* If TEM
3600c 50 20 77 61 73 20 73 70 65 63 69 66 69 65 64 2c  P was specified,
3600d 20 74 68 65 6e 20 74 68 65 20 74 72 69 67 67 65   then the trigge
3600e 72 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 20 62  r name may not b
3600f 65 20 71 75 61 6c 69 66 69 65 64 2e 20 2a 2f 0a  e qualified. */.
36010 20 20 20 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e      if( pName2->
36011 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  n>0 ){.      sql
36012 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
36013 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20  rse, "temporary 
36014 74 72 69 67 67 65 72 20 6d 61 79 20 6e 6f 74 20  trigger may not 
36015 68 61 76 65 20 71 75 61 6c 69 66 69 65 64 20 6e  have qualified n
36016 61 6d 65 22 29 3b 0a 20 20 20 20 20 20 67 6f 74  ame");.      got
36017 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
36018 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 44 62  p;.    }.    iDb
36019 20 3d 20 31 3b 0a 20 20 20 20 70 4e 61 6d 65 20   = 1;.    pName 
3601a 3d 20 70 4e 61 6d 65 31 3b 0a 20 20 7d 65 6c 73  = pName1;.  }els
3601b 65 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65  e{.    /* Figure
3601c 20 6f 75 74 20 74 68 65 20 64 62 20 74 68 61 74   out the db that
3601d 20 74 68 65 20 74 72 69 67 67 65 72 20 77 69 6c   the trigger wil
3601e 6c 20 62 65 20 63 72 65 61 74 65 64 20 69 6e 20  l be created in 
3601f 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c  */.    iDb = sql
36020 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28  ite3TwoPartName(
36021 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20  pParse, pName1, 
36022 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b  pName2, &pName);
36023 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30 20 29  .    if( iDb<0 )
36024 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69  {.      goto tri
36025 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
36026 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 21 70    }.  }.  if( !p
36027 54 61 62 6c 65 4e 61 6d 65 20 7c 7c 20 64 62 2d  TableName || db-
36028 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
36029 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
3602a 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  r_cleanup;.  }..
3602b 20 20 2f 2a 20 41 20 6c 6f 6e 67 2d 73 74 61 6e    /* A long-stan
3602c 64 69 6e 67 20 70 61 72 73 65 72 20 62 75 67 20  ding parser bug 
3602d 69 73 20 74 68 61 74 20 74 68 69 73 20 73 79 6e  is that this syn
3602e 74 61 78 20 77 61 73 20 61 6c 6c 6f 77 65 64 3a  tax was allowed:
3602f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 43 52  .  **.  **    CR
36030 45 41 54 45 20 54 52 49 47 47 45 52 20 61 74 74  EATE TRIGGER att
36031 61 63 68 65 64 2e 64 65 6d 6f 20 41 46 54 45 52  ached.demo AFTER
36032 20 49 4e 53 45 52 54 20 4f 4e 20 61 74 74 61 63   INSERT ON attac
36033 68 65 64 2e 74 61 62 20 2e 2e 2e 2e 0a 20 20 2a  hed.tab .....  *
36034 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
36035 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36036 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36037 20 20 5e 5e 5e 5e 5e 5e 5e 5e 0a 20 20 2a 2a 0a    ^^^^^^^^.  **.
36038 20 20 2a 2a 20 54 6f 20 6d 61 69 6e 74 61 69 6e    ** To maintain
36039 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61   backwards compa
3603a 74 69 62 69 6c 69 74 79 2c 20 69 67 6e 6f 72 65  tibility, ignore
3603b 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20   the database.  
3603c 2a 2a 20 6e 61 6d 65 20 6f 6e 20 70 54 61 62 6c  ** name on pTabl
3603d 65 4e 61 6d 65 20 69 66 20 77 65 20 61 72 65 20  eName if we are 
3603e 72 65 70 61 72 73 69 6e 67 20 6f 75 72 20 6f 66  reparsing our of
3603f 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 2e 0a   SQLITE_MASTER..
36040 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69    */.  if( db->i
36041 6e 69 74 2e 62 75 73 79 20 26 26 20 69 44 62 21  nit.busy && iDb!
36042 3d 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =1 ){.    sqlite
36043 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62  3DbFree(db, pTab
36044 6c 65 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61  leName->a[0].zDa
36045 74 61 62 61 73 65 29 3b 0a 20 20 20 20 70 54 61  tabase);.    pTa
36046 62 6c 65 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44  bleName->a[0].zD
36047 61 74 61 62 61 73 65 20 3d 20 30 3b 0a 20 20 7d  atabase = 0;.  }
36048 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72  ..  /* If the tr
36049 69 67 67 65 72 20 6e 61 6d 65 20 77 61 73 20 75  igger name was u
3604a 6e 71 75 61 6c 69 66 69 65 64 2c 20 61 6e 64 20  nqualified, and 
3604b 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 74  the table is a t
3604c 65 6d 70 20 74 61 62 6c 65 2c 0a 20 20 2a 2a 20  emp table,.  ** 
3604d 74 68 65 6e 20 73 65 74 20 69 44 62 20 74 6f 20  then set iDb to 
3604e 31 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  1 to create the 
3604f 74 72 69 67 67 65 72 20 69 6e 20 74 68 65 20 74  trigger in the t
36050 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
36051 65 2e 0a 20 20 2a 2a 20 49 66 20 73 71 6c 69 74  e..  ** If sqlit
36052 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28  e3SrcListLookup(
36053 29 20 72 65 74 75 72 6e 73 20 30 2c 20 69 6e 64  ) returns 0, ind
36054 69 63 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c  icating the tabl
36055 65 20 64 6f 65 73 20 6e 6f 74 0a 20 20 2a 2a 20  e does not.  ** 
36056 65 78 69 73 74 2c 20 74 68 65 20 65 72 72 6f 72  exist, the error
36057 20 69 73 20 63 61 75 67 68 74 20 62 79 20 74 68   is caught by th
36058 65 20 62 6c 6f 63 6b 20 62 65 6c 6f 77 2e 0a 20  e block below.. 
36059 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c   */.  pTab = sql
3605a 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
3605b 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65  p(pParse, pTable
3605c 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 64 62 2d  Name);.  if( db-
3605d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 26 26  >init.busy==0 &&
3605e 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 26 26   pName2->n==0 &&
3605f 20 70 54 61 62 0a 20 20 20 20 20 20 20 20 26 26   pTab.        &&
36060 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3d 3d   pTab->pSchema==
36061 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65  db->aDb[1].pSche
36062 6d 61 20 29 7b 0a 20 20 20 20 69 44 62 20 3d 20  ma ){.    iDb = 
36063 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73  1;.  }..  /* Ens
36064 75 72 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61  ure the table na
36065 6d 65 20 6d 61 74 63 68 65 73 20 64 61 74 61 62  me matches datab
36066 61 73 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 61  ase name and tha
36067 74 20 74 68 65 20 74 61 62 6c 65 20 65 78 69 73  t the table exis
36068 74 73 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  ts */.  if( db->
36069 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67  mallocFailed ) g
3606a 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
3606b 6e 75 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70  nup;.  assert( p
3606c 54 61 62 6c 65 4e 61 6d 65 2d 3e 6e 53 72 63 3d  TableName->nSrc=
3606d 3d 31 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  =1 );.  if( sqli
3606e 74 65 33 46 69 78 49 6e 69 74 28 26 73 46 69 78  te3FixInit(&sFix
3606f 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22  , pParse, iDb, "
36070 74 72 69 67 67 65 72 22 2c 20 70 4e 61 6d 65 29  trigger", pName)
36071 20 26 26 20 0a 20 20 20 20 20 20 73 71 6c 69 74   && .      sqlit
36072 65 33 46 69 78 53 72 63 4c 69 73 74 28 26 73 46  e3FixSrcList(&sF
36073 69 78 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 20  ix, pTableName) 
36074 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  ){.    goto trig
36075 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
36076 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
36077 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70  3SrcListLookup(p
36078 50 61 72 73 65 2c 20 70 54 61 62 6c 65 4e 61 6d  Parse, pTableNam
36079 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20  e);.  if( !pTab 
3607a 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 61  ){.    /* The ta
3607b 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ble does not exi
3607c 73 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 64  st. */.    if( d
3607d 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d 31 20 29  b->init.iDb==1 )
3607e 7b 0a 20 20 20 20 20 20 2f 2a 20 54 69 63 6b 65  {.      /* Ticke
3607f 74 20 23 33 38 31 30 2e 0a 20 20 20 20 20 20 2a  t #3810..      *
36080 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 77 68 65 6e  * Normally, when
36081 65 76 65 72 20 61 20 74 61 62 6c 65 20 69 73 20  ever a table is 
36082 64 72 6f 70 70 65 64 2c 20 61 6c 6c 20 61 73 73  dropped, all ass
36083 6f 63 69 61 74 65 64 20 74 72 69 67 67 65 72 73  ociated triggers
36084 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 64 72   are.      ** dr
36085 6f 70 70 65 64 20 74 6f 6f 2e 20 20 42 75 74 20  opped too.  But 
36086 69 66 20 61 20 54 45 4d 50 20 74 72 69 67 67 65  if a TEMP trigge
36087 72 20 69 73 20 63 72 65 61 74 65 64 20 6f 6e 20  r is created on 
36088 61 20 6e 6f 6e 2d 54 45 4d 50 20 74 61 62 6c 65  a non-TEMP table
36089 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 74 68  .      ** and th
3608a 65 20 74 61 62 6c 65 20 69 73 20 64 72 6f 70 70  e table is dropp
3608b 65 64 20 62 79 20 61 20 64 69 66 66 65 72 65 6e  ed by a differen
3608c 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  t database conne
3608d 63 74 69 6f 6e 2c 20 74 68 65 0a 20 20 20 20 20  ction, the.     
3608e 20 2a 2a 20 74 72 69 67 67 65 72 20 69 73 20 6e   ** trigger is n
3608f 6f 74 20 76 69 73 69 62 6c 65 20 74 6f 20 74 68  ot visible to th
36090 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
36091 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20  ction that does 
36092 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 72 6f  the.      ** dro
36093 70 20 73 6f 20 74 68 65 20 74 72 69 67 67 65 72  p so the trigger
36094 20 63 61 6e 6e 6f 74 20 62 65 20 64 72 6f 70 70   cannot be dropp
36095 65 64 2e 20 20 54 68 69 73 20 72 65 73 75 6c 74  ed.  This result
36096 73 20 69 6e 20 61 6e 0a 20 20 20 20 20 20 2a 2a  s in an.      **
36097 20 22 6f 72 70 68 61 6e 65 64 20 74 72 69 67 67   "orphaned trigg
36098 65 72 22 20 2d 20 61 20 74 72 69 67 67 65 72 20  er" - a trigger 
36099 77 68 6f 73 65 20 61 73 73 6f 63 69 61 74 65 64  whose associated
3609a 20 74 61 62 6c 65 20 69 73 20 6d 69 73 73 69 6e   table is missin
3609b 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  g..      */.    
3609c 20 20 64 62 2d 3e 69 6e 69 74 2e 6f 72 70 68 61    db->init.orpha
3609d 6e 54 72 69 67 67 65 72 20 3d 20 31 3b 0a 20 20  nTrigger = 1;.  
3609e 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 74 72 69    }.    goto tri
3609f 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
360a0 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  }.  if( IsVirtua
360a1 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73  l(pTab) ){.    s
360a2 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
360a3 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63  Parse, "cannot c
360a4 72 65 61 74 65 20 74 72 69 67 67 65 72 73 20 6f  reate triggers o
360a5 6e 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  n virtual tables
360a6 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69  ");.    goto tri
360a7 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
360a8 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  }..  /* Check th
360a9 61 74 20 74 68 65 20 74 72 69 67 67 65 72 20 6e  at the trigger n
360aa 61 6d 65 20 69 73 20 6e 6f 74 20 72 65 73 65 72  ame is not reser
360ab 76 65 64 20 61 6e 64 20 74 68 61 74 20 6e 6f 20  ved and that no 
360ac 74 72 69 67 67 65 72 20 6f 66 20 74 68 65 0a 20  trigger of the. 
360ad 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 6e 61   ** specified na
360ae 6d 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 7a  me exists */.  z
360af 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61  Name = sqlite3Na
360b0 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
360b1 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a  pName);.  if( !z
360b2 4e 61 6d 65 20 7c 7c 20 53 51 4c 49 54 45 5f 4f  Name || SQLITE_O
360b3 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f  K!=sqlite3CheckO
360b4 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65  bjectName(pParse
360b5 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  , zName) ){.    
360b6 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
360b7 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65  anup;.  }.  asse
360b8 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65 6d  rt( sqlite3Schem
360b9 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20 69  aMutexHeld(db, i
360ba 44 62 2c 20 30 29 20 29 3b 0a 20 20 69 66 28 20  Db, 0) );.  if( 
360bb 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28  sqlite3HashFind(
360bc 26 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  &(db->aDb[iDb].p
360bd 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68  Schema->trigHash
360be 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
360bf 20 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20           zName, 
360c0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
360c1 7a 4e 61 6d 65 29 29 20 29 7b 0a 20 20 20 20 69  zName)) ){.    i
360c2 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20  f( !noErr ){.   
360c3 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
360c4 73 67 28 70 50 61 72 73 65 2c 20 22 74 72 69 67  sg(pParse, "trig
360c5 67 65 72 20 25 54 20 61 6c 72 65 61 64 79 20 65  ger %T already e
360c6 78 69 73 74 73 22 2c 20 70 4e 61 6d 65 29 3b 0a  xists", pName);.
360c7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
360c8 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 69 6e   assert( !db->in
360c9 69 74 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 20  it.busy );.     
360ca 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69   sqlite3CodeVeri
360cb 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c  fySchema(pParse,
360cc 20 69 44 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20   iDb);.    }.   
360cd 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
360ce 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eanup;.  }..  /*
360cf 20 44 6f 20 6e 6f 74 20 63 72 65 61 74 65 20 61   Do not create a
360d0 20 74 72 69 67 67 65 72 20 6f 6e 20 61 20 73 79   trigger on a sy
360d1 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f 0a 20 20  stem table */.  
360d2 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49  if( sqlite3StrNI
360d3 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  Cmp(pTab->zName,
360d4 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 3d 3d   "sqlite_", 7)==
360d5 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
360d6 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
360d7 20 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20   "cannot create 
360d8 74 72 69 67 67 65 72 20 6f 6e 20 73 79 73 74 65  trigger on syste
360d9 6d 20 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 70  m table");.    p
360da 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  Parse->nErr++;. 
360db 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
360dc 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
360dd 2f 2a 20 49 4e 53 54 45 41 44 20 6f 66 20 74 72  /* INSTEAD of tr
360de 69 67 67 65 72 73 20 61 72 65 20 6f 6e 6c 79 20  iggers are only 
360df 66 6f 72 20 76 69 65 77 73 20 61 6e 64 20 76 69  for views and vi
360e0 65 77 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74  ews only support
360e1 20 49 4e 53 54 45 41 44 0a 20 20 2a 2a 20 6f 66   INSTEAD.  ** of
360e2 20 74 72 69 67 67 65 72 73 2e 0a 20 20 2a 2f 0a   triggers..  */.
360e3 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c    if( pTab->pSel
360e4 65 63 74 20 26 26 20 74 72 5f 74 6d 21 3d 54 4b  ect && tr_tm!=TK
360e5 5f 49 4e 53 54 45 41 44 20 29 7b 0a 20 20 20 20  _INSTEAD ){.    
360e6 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
360e7 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20  pParse, "cannot 
360e8 63 72 65 61 74 65 20 25 73 20 74 72 69 67 67 65  create %s trigge
360e9 72 20 6f 6e 20 76 69 65 77 3a 20 25 53 22 2c 20  r on view: %S", 
360ea 0a 20 20 20 20 20 20 20 20 28 74 72 5f 74 6d 20  .        (tr_tm 
360eb 3d 3d 20 54 4b 5f 42 45 46 4f 52 45 29 3f 22 42  == TK_BEFORE)?"B
360ec 45 46 4f 52 45 22 3a 22 41 46 54 45 52 22 2c 20  EFORE":"AFTER", 
360ed 70 54 61 62 6c 65 4e 61 6d 65 2c 20 30 29 3b 0a  pTableName, 0);.
360ee 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
360ef 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
360f0 69 66 28 20 21 70 54 61 62 2d 3e 70 53 65 6c 65  if( !pTab->pSele
360f1 63 74 20 26 26 20 74 72 5f 74 6d 3d 3d 54 4b 5f  ct && tr_tm==TK_
360f2 49 4e 53 54 45 41 44 20 29 7b 0a 20 20 20 20 73  INSTEAD ){.    s
360f3 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
360f4 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63  Parse, "cannot c
360f5 72 65 61 74 65 20 49 4e 53 54 45 41 44 20 4f 46  reate INSTEAD OF
360f6 22 0a 20 20 20 20 20 20 20 20 22 20 74 72 69 67  ".        " trig
360f7 67 65 72 20 6f 6e 20 74 61 62 6c 65 3a 20 25 53  ger on table: %S
360f8 22 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2c 20 30  ", pTableName, 0
360f9 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  );.    goto trig
360fa 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
360fb 0a 20 20 69 54 61 62 44 62 20 3d 20 73 71 6c 69  .  iTabDb = sqli
360fc 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
360fd 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  (db, pTab->pSche
360fe 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  ma);..#ifndef SQ
360ff 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
36100 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20  IZATION.  {.    
36101 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 54  int code = SQLIT
36102 45 5f 43 52 45 41 54 45 5f 54 52 49 47 47 45 52  E_CREATE_TRIGGER
36103 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
36104 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b   *zDb = db->aDb[
36105 69 54 61 62 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  iTabDb].zName;. 
36106 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
36107 44 62 54 72 69 67 20 3d 20 69 73 54 65 6d 70 20  DbTrig = isTemp 
36108 3f 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61  ? db->aDb[1].zNa
36109 6d 65 20 3a 20 7a 44 62 3b 0a 20 20 20 20 69 66  me : zDb;.    if
3610a 28 20 69 54 61 62 44 62 3d 3d 31 20 7c 7c 20 69  ( iTabDb==1 || i
3610b 73 54 65 6d 70 20 29 20 63 6f 64 65 20 3d 20 53  sTemp ) code = S
3610c 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
3610d 50 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20 69  P_TRIGGER;.    i
3610e 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
3610f 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65  eck(pParse, code
36110 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a  , zName, pTab->z
36111 4e 61 6d 65 2c 20 7a 44 62 54 72 69 67 29 20 29  Name, zDbTrig) )
36112 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69  {.      goto tri
36113 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
36114 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
36115 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
36116 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45  rse, SQLITE_INSE
36117 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  RT, SCHEMA_TABLE
36118 28 69 54 61 62 44 62 29 2c 30 2c 7a 44 62 29 29  (iTabDb),0,zDb))
36119 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69  {.      goto tri
3611a 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
3611b 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a    }.  }.#endif..
3611c 20 20 2f 2a 20 49 4e 53 54 45 41 44 20 4f 46 20    /* INSTEAD OF 
3611d 74 72 69 67 67 65 72 73 20 63 61 6e 20 6f 6e 6c  triggers can onl
3611e 79 20 61 70 70 65 61 72 20 6f 6e 20 76 69 65 77  y appear on view
3611f 73 20 61 6e 64 20 42 45 46 4f 52 45 20 74 72 69  s and BEFORE tri
36120 67 67 65 72 73 0a 20 20 2a 2a 20 63 61 6e 6e 6f  ggers.  ** canno
36121 74 20 61 70 70 65 61 72 20 6f 6e 20 76 69 65 77  t appear on view
36122 73 2e 20 20 53 6f 20 77 65 20 6d 69 67 68 74 20  s.  So we might 
36123 61 73 20 77 65 6c 6c 20 74 72 61 6e 73 6c 61 74  as well translat
36124 65 20 65 76 65 72 79 0a 20 20 2a 2a 20 49 4e 53  e every.  ** INS
36125 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 20  TEAD OF trigger 
36126 69 6e 74 6f 20 61 20 42 45 46 4f 52 45 20 74 72  into a BEFORE tr
36127 69 67 67 65 72 2e 20 20 49 74 20 73 69 6d 70 6c  igger.  It simpl
36128 69 66 69 65 73 20 63 6f 64 65 0a 20 20 2a 2a 20  ifies code.  ** 
36129 65 6c 73 65 77 68 65 72 65 2e 0a 20 20 2a 2f 0a  elsewhere..  */.
3612a 20 20 69 66 20 28 74 72 5f 74 6d 20 3d 3d 20 54    if (tr_tm == T
3612b 4b 5f 49 4e 53 54 45 41 44 29 7b 0a 20 20 20 20  K_INSTEAD){.    
3612c 74 72 5f 74 6d 20 3d 20 54 4b 5f 42 45 46 4f 52  tr_tm = TK_BEFOR
3612d 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 75 69  E;.  }..  /* Bui
3612e 6c 64 20 74 68 65 20 54 72 69 67 67 65 72 20 6f  ld the Trigger o
3612f 62 6a 65 63 74 20 2a 2f 0a 20 20 70 54 72 69 67  bject */.  pTrig
36130 67 65 72 20 3d 20 28 54 72 69 67 67 65 72 2a 29  ger = (Trigger*)
36131 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
36132 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54  ero(db, sizeof(T
36133 72 69 67 67 65 72 29 29 3b 0a 20 20 69 66 28 20  rigger));.  if( 
36134 70 54 72 69 67 67 65 72 3d 3d 30 20 29 20 67 6f  pTrigger==0 ) go
36135 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  to trigger_clean
36136 75 70 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e  up;.  pTrigger->
36137 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20  zName = zName;. 
36138 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 70 54   zName = 0;.  pT
36139 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 20 3d 20  rigger->table = 
3613a 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
3613b 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2d 3e  db, pTableName->
3613c 61 5b 30 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 70  a[0].zName);.  p
3613d 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61  Trigger->pSchema
3613e 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
3613f 70 53 63 68 65 6d 61 3b 0a 20 20 70 54 72 69 67  pSchema;.  pTrig
36140 67 65 72 2d 3e 70 54 61 62 53 63 68 65 6d 61 20  ger->pTabSchema 
36141 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b  = pTab->pSchema;
36142 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 6f 70 20  .  pTrigger->op 
36143 3d 20 28 75 38 29 6f 70 3b 0a 20 20 70 54 72 69  = (u8)op;.  pTri
36144 67 67 65 72 2d 3e 74 72 5f 74 6d 20 3d 20 74 72  gger->tr_tm = tr
36145 5f 74 6d 3d 3d 54 4b 5f 42 45 46 4f 52 45 20 3f  _tm==TK_BEFORE ?
36146 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20   TRIGGER_BEFORE 
36147 3a 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 3b  : TRIGGER_AFTER;
36148 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68  .  pTrigger->pWh
36149 65 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  en = sqlite3Expr
3614a 44 75 70 28 64 62 2c 20 70 57 68 65 6e 2c 20 45  Dup(db, pWhen, E
3614b 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a  XPRDUP_REDUCE);.
3614c 20 20 70 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c    pTrigger->pCol
3614d 75 6d 6e 73 20 3d 20 73 71 6c 69 74 65 33 49 64  umns = sqlite3Id
3614e 4c 69 73 74 44 75 70 28 64 62 2c 20 70 43 6f 6c  ListDup(db, pCol
3614f 75 6d 6e 73 29 3b 0a 20 20 61 73 73 65 72 74 28  umns);.  assert(
36150 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69   pParse->pNewTri
36151 67 67 65 72 3d 3d 30 20 29 3b 0a 20 20 70 50 61  gger==0 );.  pPa
36152 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72  rse->pNewTrigger
36153 20 3d 20 70 54 72 69 67 67 65 72 3b 0a 0a 74 72   = pTrigger;..tr
36154 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3a 0a 20  igger_cleanup:. 
36155 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
36156 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  b, zName);.  sql
36157 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
36158 65 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65  e(db, pTableName
36159 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69  );.  sqlite3IdLi
3615a 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f  stDelete(db, pCo
3615b 6c 75 6d 6e 73 29 3b 0a 20 20 73 71 6c 69 74 65  lumns);.  sqlite
3615c 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
3615d 70 57 68 65 6e 29 3b 0a 20 20 69 66 28 20 21 70  pWhen);.  if( !p
3615e 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
3615f 65 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  er ){.    sqlite
36160 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 64  3DeleteTrigger(d
36161 62 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20  b, pTrigger);.  
36162 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
36163 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  t( pParse->pNewT
36164 72 69 67 67 65 72 3d 3d 70 54 72 69 67 67 65 72  rigger==pTrigger
36165 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   );.  }.}../*.**
36166 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
36167 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 6c   called after al
36168 6c 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72  l of the trigger
36169 20 61 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65   actions have be
3616a 65 6e 20 70 61 72 73 65 64 0a 2a 2a 20 69 6e 20  en parsed.** in 
3616b 6f 72 64 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74  order to complet
3616c 65 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66  e the process of
3616d 20 62 75 69 6c 64 69 6e 67 20 74 68 65 20 74 72   building the tr
3616e 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  igger..*/.SQLITE
3616f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
36170 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 67  lite3FinishTrigg
36171 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  er(.  Parse *pPa
36172 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  rse,          /*
36173 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   Parser context 
36174 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  */.  TriggerStep
36175 20 2a 70 53 74 65 70 4c 69 73 74 2c 20 2f 2a 20   *pStepList, /* 
36176 54 68 65 20 74 72 69 67 67 65 72 65 64 20 70 72  The triggered pr
36177 6f 67 72 61 6d 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ogram */.  Token
36178 20 2a 70 41 6c 6c 20 20 20 20 20 20 20 20 20 20   *pAll          
36179 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 74 68 61 74     /* Token that
3617a 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 63   describes the c
3617b 6f 6d 70 6c 65 74 65 20 43 52 45 41 54 45 20 54  omplete CREATE T
3617c 52 49 47 47 45 52 20 2a 2f 0a 29 7b 0a 20 20 54  RIGGER */.){.  T
3617d 72 69 67 67 65 72 20 2a 70 54 72 69 67 20 3d 20  rigger *pTrig = 
3617e 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67  pParse->pNewTrig
3617f 67 65 72 3b 20 20 20 2f 2a 20 54 72 69 67 67 65  ger;   /* Trigge
36180 72 20 62 65 69 6e 67 20 66 69 6e 69 73 68 65 64  r being finished
36181 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d   */.  char *zNam
36182 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
36183 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36184 20 4e 61 6d 65 20 6f 66 20 74 72 69 67 67 65 72   Name of trigger
36185 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
36186 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
36187 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36188 20 54 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   The database */
36189 0a 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b  .  DbFixer sFix;
3618a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3618b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
3618c 78 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  xer object */.  
3618d 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
3618e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3618f 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
36190 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ase containing t
36191 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20  he trigger */.  
36192 54 6f 6b 65 6e 20 6e 61 6d 65 54 6f 6b 65 6e 3b  Token nameToken;
36193 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36194 20 20 20 20 20 20 20 20 2f 2a 20 54 72 69 67 67          /* Trigg
36195 65 72 20 6e 61 6d 65 20 66 6f 72 20 65 72 72 6f  er name for erro
36196 72 20 72 65 70 6f 72 74 69 6e 67 20 2a 2f 0a 0a  r reporting */..
36197 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72    pParse->pNewTr
36198 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 69 66 28  igger = 0;.  if(
36199 20 4e 45 56 45 52 28 70 50 61 72 73 65 2d 3e 6e   NEVER(pParse->n
3619a 45 72 72 29 20 7c 7c 20 21 70 54 72 69 67 20 29  Err) || !pTrig )
3619b 20 67 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e   goto triggerfin
3619c 69 73 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7a  ish_cleanup;.  z
3619d 4e 61 6d 65 20 3d 20 70 54 72 69 67 2d 3e 7a 4e  Name = pTrig->zN
3619e 61 6d 65 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c  ame;.  iDb = sql
3619f 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
361a0 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
361a1 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  rig->pSchema);. 
361a2 20 70 54 72 69 67 2d 3e 73 74 65 70 5f 6c 69 73   pTrig->step_lis
361a3 74 20 3d 20 70 53 74 65 70 4c 69 73 74 3b 0a 20  t = pStepList;. 
361a4 20 77 68 69 6c 65 28 20 70 53 74 65 70 4c 69 73   while( pStepLis
361a5 74 20 29 7b 0a 20 20 20 20 70 53 74 65 70 4c 69  t ){.    pStepLi
361a6 73 74 2d 3e 70 54 72 69 67 20 3d 20 70 54 72 69  st->pTrig = pTri
361a7 67 3b 0a 20 20 20 20 70 53 74 65 70 4c 69 73 74  g;.    pStepList
361a8 20 3d 20 70 53 74 65 70 4c 69 73 74 2d 3e 70 4e   = pStepList->pN
361a9 65 78 74 3b 0a 20 20 7d 0a 20 20 6e 61 6d 65 54  ext;.  }.  nameT
361aa 6f 6b 65 6e 2e 7a 20 3d 20 70 54 72 69 67 2d 3e  oken.z = pTrig->
361ab 7a 4e 61 6d 65 3b 0a 20 20 6e 61 6d 65 54 6f 6b  zName;.  nameTok
361ac 65 6e 2e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  en.n = sqlite3St
361ad 72 6c 65 6e 33 30 28 6e 61 6d 65 54 6f 6b 65 6e  rlen30(nameToken
361ae 2e 7a 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  .z);.  if( sqlit
361af 65 33 46 69 78 49 6e 69 74 28 26 73 46 69 78 2c  e3FixInit(&sFix,
361b0 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 74   pParse, iDb, "t
361b1 72 69 67 67 65 72 22 2c 20 26 6e 61 6d 65 54 6f  rigger", &nameTo
361b2 6b 65 6e 29 20 0a 20 20 20 20 20 20 20 20 20 20  ken) .          
361b3 26 26 20 73 71 6c 69 74 65 33 46 69 78 54 72 69  && sqlite3FixTri
361b4 67 67 65 72 53 74 65 70 28 26 73 46 69 78 2c 20  ggerStep(&sFix, 
361b5 70 54 72 69 67 2d 3e 73 74 65 70 5f 6c 69 73 74  pTrig->step_list
361b6 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72  ) ){.    goto tr
361b7 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61  iggerfinish_clea
361b8 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69  nup;.  }..  /* i
361b9 66 20 77 65 20 61 72 65 20 6e 6f 74 20 69 6e 69  f we are not ini
361ba 74 69 61 6c 69 7a 69 6e 67 2c 0a 20 20 2a 2a 20  tializing,.  ** 
361bb 62 75 69 6c 64 20 74 68 65 20 73 71 6c 69 74 65  build the sqlite
361bc 5f 6d 61 73 74 65 72 20 65 6e 74 72 79 0a 20 20  _master entry.  
361bd 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e  */.  if( !db->in
361be 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 56  it.busy ){.    V
361bf 64 62 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72  dbe *v;.    char
361c0 20 2a 7a 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b   *z;..    /* Mak
361c1 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68  e an entry in th
361c2 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
361c3 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 76 20 3d  table */.    v =
361c4 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
361c5 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28  pParse);.    if(
361c6 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 74 72 69   v==0 ) goto tri
361c7 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e  ggerfinish_clean
361c8 75 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  up;.    sqlite3B
361c9 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
361ca 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
361cb 62 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69  b);.    z = sqli
361cc 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c  te3DbStrNDup(db,
361cd 20 28 63 68 61 72 2a 29 70 41 6c 6c 2d 3e 7a 2c   (char*)pAll->z,
361ce 20 70 41 6c 6c 2d 3e 6e 29 3b 0a 20 20 20 20 73   pAll->n);.    s
361cf 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
361d0 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
361d1 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51   "INSERT INTO %Q
361d2 2e 25 73 20 56 41 4c 55 45 53 28 27 74 72 69 67  .%s VALUES('trig
361d3 67 65 72 27 2c 25 51 2c 25 51 2c 30 2c 27 43 52  ger',%Q,%Q,0,'CR
361d4 45 41 54 45 20 54 52 49 47 47 45 52 20 25 71 27  EATE TRIGGER %q'
361d5 29 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61  )",.       db->a
361d6 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53  Db[iDb].zName, S
361d7 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
361d8 2c 20 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  , zName,.       
361d9 70 54 72 69 67 2d 3e 74 61 62 6c 65 2c 20 7a 29  pTrig->table, z)
361da 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
361db 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20  ree(db, z);.    
361dc 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f  sqlite3ChangeCoo
361dd 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29  kie(pParse, iDb)
361de 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
361df 65 41 64 64 50 61 72 73 65 53 63 68 65 6d 61 4f  eAddParseSchemaO
361e0 70 28 76 2c 20 69 44 62 2c 0a 20 20 20 20 20 20  p(v, iDb,.      
361e1 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66    sqlite3MPrintf
361e2 28 64 62 2c 20 22 74 79 70 65 3d 27 74 72 69 67  (db, "type='trig
361e3 67 65 72 27 20 41 4e 44 20 6e 61 6d 65 3d 27 25  ger' AND name='%
361e4 71 27 22 2c 20 7a 4e 61 6d 65 29 29 3b 0a 20 20  q'", zName));.  
361e5 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69  }..  if( db->ini
361e6 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 54 72  t.busy ){.    Tr
361e7 69 67 67 65 72 20 2a 70 4c 69 6e 6b 20 3d 20 70  igger *pLink = p
361e8 54 72 69 67 3b 0a 20 20 20 20 48 61 73 68 20 2a  Trig;.    Hash *
361e9 70 48 61 73 68 20 3d 20 26 64 62 2d 3e 61 44 62  pHash = &db->aDb
361ea 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74  [iDb].pSchema->t
361eb 72 69 67 48 61 73 68 3b 0a 20 20 20 20 61 73 73  rigHash;.    ass
361ec 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65  ert( sqlite3Sche
361ed 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20  maMutexHeld(db, 
361ee 69 44 62 2c 20 30 29 20 29 3b 0a 20 20 20 20 70  iDb, 0) );.    p
361ef 54 72 69 67 20 3d 20 73 71 6c 69 74 65 33 48 61  Trig = sqlite3Ha
361f0 73 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20  shInsert(pHash, 
361f1 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74  zName, sqlite3St
361f2 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 20 70  rlen30(zName), p
361f3 54 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20 70  Trig);.    if( p
361f4 54 72 69 67 20 29 7b 0a 20 20 20 20 20 20 64 62  Trig ){.      db
361f5 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
361f6 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   1;.    }else if
361f7 28 20 70 4c 69 6e 6b 2d 3e 70 53 63 68 65 6d 61  ( pLink->pSchema
361f8 3d 3d 70 4c 69 6e 6b 2d 3e 70 54 61 62 53 63 68  ==pLink->pTabSch
361f9 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 54 61 62  ema ){.      Tab
361fa 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 20  le *pTab;.      
361fb 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53  int n = sqlite3S
361fc 74 72 6c 65 6e 33 30 28 70 4c 69 6e 6b 2d 3e 74  trlen30(pLink->t
361fd 61 62 6c 65 29 3b 0a 20 20 20 20 20 20 70 54 61  able);.      pTa
361fe 62 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  b = sqlite3HashF
361ff 69 6e 64 28 26 70 4c 69 6e 6b 2d 3e 70 54 61 62  ind(&pLink->pTab
36200 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c  Schema->tblHash,
36201 20 70 4c 69 6e 6b 2d 3e 74 61 62 6c 65 2c 20 6e   pLink->table, n
36202 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
36203 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 20 20   pTab!=0 );.    
36204 20 20 70 4c 69 6e 6b 2d 3e 70 4e 65 78 74 20 3d    pLink->pNext =
36205 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b   pTab->pTrigger;
36206 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 54 72  .      pTab->pTr
36207 69 67 67 65 72 20 3d 20 70 4c 69 6e 6b 3b 0a 20  igger = pLink;. 
36208 20 20 20 7d 0a 20 20 7d 0a 0a 74 72 69 67 67 65     }.  }..trigge
36209 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3a  rfinish_cleanup:
3620a 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  .  sqlite3Delete
3620b 54 72 69 67 67 65 72 28 64 62 2c 20 70 54 72 69  Trigger(db, pTri
3620c 67 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70  g);.  assert( !p
3620d 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
3620e 65 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  er );.  sqlite3D
3620f 65 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70  eleteTriggerStep
36210 28 64 62 2c 20 70 53 74 65 70 4c 69 73 74 29 3b  (db, pStepList);
36211 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61  .}../*.** Turn a
36212 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
36213 74 20 28 74 68 61 74 20 74 68 65 20 70 53 65 6c  t (that the pSel
36214 65 63 74 20 70 61 72 61 6d 65 74 65 72 20 70 6f  ect parameter po
36215 69 6e 74 73 20 74 6f 29 20 69 6e 74 6f 0a 2a 2a  ints to) into.**
36216 20 61 20 74 72 69 67 67 65 72 20 73 74 65 70 2e   a trigger step.
36217 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
36218 65 72 20 74 6f 20 61 20 54 72 69 67 67 65 72 53  er to a TriggerS
36219 74 65 70 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  tep structure..*
3621a 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20  *.** The parser 
3621b 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
3621c 6e 65 20 77 68 65 6e 20 69 74 20 66 69 6e 64 73  ne when it finds
3621d 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
3621e 65 6e 74 20 69 6e 0a 2a 2a 20 62 6f 64 79 20 6f  ent in.** body o
3621f 66 20 61 20 54 52 49 47 47 45 52 2e 20 20 0a 2a  f a TRIGGER.  .*
36220 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
36221 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71   TriggerStep *sq
36222 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c 65  lite3TriggerSele
36223 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 20 2a  ctStep(sqlite3 *
36224 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  db, Select *pSel
36225 65 63 74 29 7b 0a 20 20 54 72 69 67 67 65 72 53  ect){.  TriggerS
36226 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65  tep *pTriggerSte
36227 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  p = sqlite3DbMal
36228 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
36229 6f 66 28 54 72 69 67 67 65 72 53 74 65 70 29 29  of(TriggerStep))
3622a 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  ;.  if( pTrigger
3622b 53 74 65 70 3d 3d 30 20 29 20 7b 0a 20 20 20 20  Step==0 ) {.    
3622c 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
3622d 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29  ete(db, pSelect)
3622e 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
3622f 20 20 7d 0a 20 20 70 54 72 69 67 67 65 72 53 74    }.  pTriggerSt
36230 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c 45  ep->op = TK_SELE
36231 43 54 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74  CT;.  pTriggerSt
36232 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 53  ep->pSelect = pS
36233 65 6c 65 63 74 3b 0a 20 20 70 54 72 69 67 67 65  elect;.  pTrigge
36234 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20  rStep->orconf = 
36235 4f 45 5f 44 65 66 61 75 6c 74 3b 0a 20 20 72 65  OE_Default;.  re
36236 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65  turn pTriggerSte
36237 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  p;.}../*.** Allo
36238 63 61 74 65 20 73 70 61 63 65 20 74 6f 20 68 6f  cate space to ho
36239 6c 64 20 61 20 6e 65 77 20 74 72 69 67 67 65 72  ld a new trigger
3623a 20 73 74 65 70 2e 20 20 54 68 65 20 61 6c 6c 6f   step.  The allo
3623b 63 61 74 65 64 20 73 70 61 63 65 0a 2a 2a 20 68  cated space.** h
3623c 6f 6c 64 73 20 62 6f 74 68 20 74 68 65 20 54 72  olds both the Tr
3623d 69 67 67 65 72 53 74 65 70 20 6f 62 6a 65 63 74  iggerStep object
3623e 20 61 6e 64 20 74 68 65 20 54 72 69 67 67 65 72   and the Trigger
3623f 53 74 65 70 2e 74 61 72 67 65 74 2e 7a 20 73 74  Step.target.z st
36240 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  ring..**.** If a
36241 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75  n OOM error occu
36242 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  rs, NULL is retu
36243 72 6e 65 64 20 61 6e 64 20 64 62 2d 3e 6d 61 6c  rned and db->mal
36244 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 73 65 74  locFailed is set
36245 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 72 69 67  ..*/.static Trig
36246 67 65 72 53 74 65 70 20 2a 74 72 69 67 67 65 72  gerStep *trigger
36247 53 74 65 70 41 6c 6c 6f 63 61 74 65 28 0a 20 20  StepAllocate(.  
36248 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
36249 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
3624a 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3624b 6f 6e 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 20  on */.  u8 op,  
3624c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3624d 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 6f      /* Trigger o
3624e 70 63 6f 64 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e  pcode */.  Token
3624f 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20 20 20   *pName         
36250 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
36251 72 67 65 74 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a  rget name */.){.
36252 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70    TriggerStep *p
36253 54 72 69 67 67 65 72 53 74 65 70 3b 0a 0a 20 20  TriggerStep;..  
36254 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 73  pTriggerStep = s
36255 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
36256 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72  ro(db, sizeof(Tr
36257 69 67 67 65 72 53 74 65 70 29 20 2b 20 70 4e 61  iggerStep) + pNa
36258 6d 65 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 70 54  me->n);.  if( pT
36259 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20  riggerStep ){.  
3625a 20 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 68 61    char *z = (cha
3625b 72 2a 29 26 70 54 72 69 67 67 65 72 53 74 65 70  r*)&pTriggerStep
3625c 5b 31 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  [1];.    memcpy(
3625d 7a 2c 20 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61  z, pName->z, pNa
3625e 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 70 54 72 69  me->n);.    pTri
3625f 67 67 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74  ggerStep->target
36260 2e 7a 20 3d 20 7a 3b 0a 20 20 20 20 70 54 72 69  .z = z;.    pTri
36261 67 67 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74  ggerStep->target
36262 2e 6e 20 3d 20 70 4e 61 6d 65 2d 3e 6e 3b 0a 20  .n = pName->n;. 
36263 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
36264 3e 6f 70 20 3d 20 6f 70 3b 0a 20 20 7d 0a 20 20  >op = op;.  }.  
36265 72 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53  return pTriggerS
36266 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75  tep;.}../*.** Bu
36267 69 6c 64 20 61 20 74 72 69 67 67 65 72 20 73 74  ild a trigger st
36268 65 70 20 6f 75 74 20 6f 66 20 61 6e 20 49 4e 53  ep out of an INS
36269 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ERT statement.  
3626a 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
3626b 0a 2a 2a 20 74 6f 20 74 68 65 20 6e 65 77 20 74  .** to the new t
3626c 72 69 67 67 65 72 20 73 74 65 70 2e 0a 2a 2a 0a  rigger step..**.
3626d 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61  ** The parser ca
3626e 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
3626f 20 77 68 65 6e 20 69 74 20 73 65 65 73 20 61 6e   when it sees an
36270 20 49 4e 53 45 52 54 20 69 6e 73 69 64 65 20 74   INSERT inside t
36271 68 65 0a 2a 2a 20 62 6f 64 79 20 6f 66 20 61 20  he.** body of a 
36272 74 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49  trigger..*/.SQLI
36273 54 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67  TE_PRIVATE Trigg
36274 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
36275 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70  riggerInsertStep
36276 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
36277 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
36278 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
36279 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  on */.  Token *p
3627a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 2f 2a 20 4e  TableName,  /* N
3627b 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
3627c 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 69   into which we i
3627d 6e 73 65 72 74 20 2a 2f 0a 20 20 49 64 4c 69 73  nsert */.  IdLis
3627e 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20 20 2f  t *pColumn,    /
3627f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e  * List of column
36280 73 20 69 6e 20 70 54 61 62 6c 65 4e 61 6d 65 20  s in pTableName 
36281 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 2a  to insert into *
36282 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
36283 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68 65 20 56  List,   /* The V
36284 41 4c 55 45 20 63 6c 61 75 73 65 3a 20 61 20 6c  ALUE clause: a l
36285 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20 74 6f  ist of values to
36286 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
36287 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
36288 74 2c 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43  t,    /* A SELEC
36289 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  T statement that
3628a 20 73 75 70 70 6c 69 65 73 20 76 61 6c 75 65 73   supplies values
3628b 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f 6e 66 20   */.  u8 orconf 
3628c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3628d 20 63 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69   conflict algori
3628e 74 68 6d 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f  thm (OE_Abort, O
3628f 45 5f 52 65 70 6c 61 63 65 2c 20 65 74 63 2e 29  E_Replace, etc.)
36290 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72   */.){.  Trigger
36291 53 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74  Step *pTriggerSt
36292 65 70 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 45  ep;..  assert(pE
36293 4c 69 73 74 20 3d 3d 20 30 20 7c 7c 20 70 53 65  List == 0 || pSe
36294 6c 65 63 74 20 3d 3d 20 30 29 3b 0a 20 20 61 73  lect == 0);.  as
36295 73 65 72 74 28 70 45 4c 69 73 74 20 21 3d 20 30  sert(pEList != 0
36296 20 7c 7c 20 70 53 65 6c 65 63 74 20 21 3d 20 30   || pSelect != 0
36297 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
36298 69 6c 65 64 29 3b 0a 0a 20 20 70 54 72 69 67 67  iled);..  pTrigg
36299 65 72 53 74 65 70 20 3d 20 74 72 69 67 67 65 72  erStep = trigger
3629a 53 74 65 70 41 6c 6c 6f 63 61 74 65 28 64 62 2c  StepAllocate(db,
3629b 20 54 4b 5f 49 4e 53 45 52 54 2c 20 70 54 61 62   TK_INSERT, pTab
3629c 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70  leName);.  if( p
3629d 54 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20  TriggerStep ){. 
3629e 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
3629f 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74  >pSelect = sqlit
362a0 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20  e3SelectDup(db, 
362a1 70 53 65 6c 65 63 74 2c 20 45 58 50 52 44 55 50  pSelect, EXPRDUP
362a2 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20 70 54  _REDUCE);.    pT
362a3 72 69 67 67 65 72 53 74 65 70 2d 3e 70 49 64 4c  riggerStep->pIdL
362a4 69 73 74 20 3d 20 70 43 6f 6c 75 6d 6e 3b 0a 20  ist = pColumn;. 
362a5 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
362a6 3e 70 45 78 70 72 4c 69 73 74 20 3d 20 73 71 6c  >pExprList = sql
362a7 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
362a8 64 62 2c 20 70 45 4c 69 73 74 2c 20 45 58 50 52  db, pEList, EXPR
362a9 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20  DUP_REDUCE);.   
362aa 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f   pTriggerStep->o
362ab 72 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a  rconf = orconf;.
362ac 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
362ad 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
362ae 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0a 20  (db, pColumn);. 
362af 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72   }.  sqlite3Expr
362b0 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
362b1 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  EList);.  sqlite
362b2 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
362b3 2c 20 70 53 65 6c 65 63 74 29 3b 0a 0a 20 20 72  , pSelect);..  r
362b4 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74  eturn pTriggerSt
362b5 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  ep;.}../*.** Con
362b6 73 74 72 75 63 74 20 61 20 74 72 69 67 67 65 72  struct a trigger
362b7 20 73 74 65 70 20 74 68 61 74 20 69 6d 70 6c 65   step that imple
362b8 6d 65 6e 74 73 20 61 6e 20 55 50 44 41 54 45 20  ments an UPDATE 
362b9 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65  statement and re
362ba 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  turn.** a pointe
362bb 72 20 74 6f 20 74 68 61 74 20 74 72 69 67 67 65  r to that trigge
362bc 72 20 73 74 65 70 2e 20 20 54 68 65 20 70 61 72  r step.  The par
362bd 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
362be 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a  outine when it.*
362bf 2a 20 73 65 65 73 20 61 6e 20 55 50 44 41 54 45  * sees an UPDATE
362c0 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 73 69 64   statement insid
362c1 65 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20  e the body of a 
362c2 43 52 45 41 54 45 20 54 52 49 47 47 45 52 2e 0a  CREATE TRIGGER..
362c3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
362c4 45 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73  E TriggerStep *s
362c5 71 6c 69 74 65 33 54 72 69 67 67 65 72 55 70 64  qlite3TriggerUpd
362c6 61 74 65 53 74 65 70 28 0a 20 20 73 71 6c 69 74  ateStep(.  sqlit
362c7 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
362c8 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
362c9 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
362ca 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d  Token *pTableNam
362cb 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  e,   /* Name of 
362cc 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  the table to be 
362cd 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 45 78 70  updated */.  Exp
362ce 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20  rList *pEList,  
362cf 20 20 2f 2a 20 54 68 65 20 53 45 54 20 63 6c 61    /* The SET cla
362d0 75 73 65 3a 20 6c 69 73 74 20 6f 66 20 63 6f 6c  use: list of col
362d1 75 6d 6e 20 61 6e 64 20 6e 65 77 20 76 61 6c 75  umn and new valu
362d2 65 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57  es */.  Expr *pW
362d3 68 65 72 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  here,        /* 
362d4 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
362d5 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f 6e 66 20   */.  u8 orconf 
362d6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
362d7 65 20 63 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72  e conflict algor
362d8 69 74 68 6d 2e 20 28 4f 45 5f 41 62 6f 72 74 2c  ithm. (OE_Abort,
362d9 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 65 74 63 29   OE_Ignore, etc)
362da 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72   */.){.  Trigger
362db 53 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74  Step *pTriggerSt
362dc 65 70 3b 0a 0a 20 20 70 54 72 69 67 67 65 72 53  ep;..  pTriggerS
362dd 74 65 70 20 3d 20 74 72 69 67 67 65 72 53 74 65  tep = triggerSte
362de 70 41 6c 6c 6f 63 61 74 65 28 64 62 2c 20 54 4b  pAllocate(db, TK
362df 5f 55 50 44 41 54 45 2c 20 70 54 61 62 6c 65 4e  _UPDATE, pTableN
362e0 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 54 72 69  ame);.  if( pTri
362e1 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20  ggerStep ){.    
362e2 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 45  pTriggerStep->pE
362e3 78 70 72 4c 69 73 74 20 3d 20 73 71 6c 69 74 65  xprList = sqlite
362e4 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
362e5 20 70 45 4c 69 73 74 2c 20 45 58 50 52 44 55 50   pEList, EXPRDUP
362e6 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20 70 54  _REDUCE);.    pT
362e7 72 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65  riggerStep->pWhe
362e8 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  re = sqlite3Expr
362e9 44 75 70 28 64 62 2c 20 70 57 68 65 72 65 2c 20  Dup(db, pWhere, 
362ea 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b  EXPRDUP_REDUCE);
362eb 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65  .    pTriggerSte
362ec 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63 6f  p->orconf = orco
362ed 6e 66 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  nf;.  }.  sqlite
362ee 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
362ef 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73  db, pEList);.  s
362f0 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
362f1 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  (db, pWhere);.  
362f2 72 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53  return pTriggerS
362f3 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  tep;.}../*.** Co
362f4 6e 73 74 72 75 63 74 20 61 20 74 72 69 67 67 65  nstruct a trigge
362f5 72 20 73 74 65 70 20 74 68 61 74 20 69 6d 70 6c  r step that impl
362f6 65 6d 65 6e 74 73 20 61 20 44 45 4c 45 54 45 20  ements a DELETE 
362f7 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65  statement and re
362f8 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  turn.** a pointe
362f9 72 20 74 6f 20 74 68 61 74 20 74 72 69 67 67 65  r to that trigge
362fa 72 20 73 74 65 70 2e 20 20 54 68 65 20 70 61 72  r step.  The par
362fb 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
362fc 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a  outine when it.*
362fd 2a 20 73 65 65 73 20 61 20 44 45 4c 45 54 45 20  * sees a DELETE 
362fe 73 74 61 74 65 6d 65 6e 74 20 69 6e 73 69 64 65  statement inside
362ff 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 43   the body of a C
36300 52 45 41 54 45 20 54 52 49 47 47 45 52 2e 0a 2a  REATE TRIGGER..*
36301 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
36302 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71   TriggerStep *sq
36303 6c 69 74 65 33 54 72 69 67 67 65 72 44 65 6c 65  lite3TriggerDele
36304 74 65 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65  teStep(.  sqlite
36305 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
36306 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
36307 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f  nnection */.  To
36308 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c  ken *pTableName,
36309 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
3630a 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 72 6f  le from which ro
3630b 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 20 2a  ws are deleted *
3630c 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
3630d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
3630e 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
3630f 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53  */.){.  TriggerS
36310 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65  tep *pTriggerSte
36311 70 3b 0a 0a 20 20 70 54 72 69 67 67 65 72 53 74  p;..  pTriggerSt
36312 65 70 20 3d 20 74 72 69 67 67 65 72 53 74 65 70  ep = triggerStep
36313 41 6c 6c 6f 63 61 74 65 28 64 62 2c 20 54 4b 5f  Allocate(db, TK_
36314 44 45 4c 45 54 45 2c 20 70 54 61 62 6c 65 4e 61  DELETE, pTableNa
36315 6d 65 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67  me);.  if( pTrig
36316 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20 70  gerStep ){.    p
36317 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68  TriggerStep->pWh
36318 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ere = sqlite3Exp
36319 72 44 75 70 28 64 62 2c 20 70 57 68 65 72 65 2c  rDup(db, pWhere,
3631a 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29   EXPRDUP_REDUCE)
3631b 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74  ;.    pTriggerSt
3631c 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f  ep->orconf = OE_
3631d 44 65 66 61 75 6c 74 3b 0a 20 20 7d 0a 20 20 73  Default;.  }.  s
3631e 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
3631f 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  (db, pWhere);.  
36320 72 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53  return pTriggerS
36321 74 65 70 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52  tep;.}../* .** R
36322 65 63 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74  ecursively delet
36323 65 20 61 20 54 72 69 67 67 65 72 20 73 74 72 75  e a Trigger stru
36324 63 74 75 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f  cture.*/.SQLITE_
36325 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
36326 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65  ite3DeleteTrigge
36327 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54  r(sqlite3 *db, T
36328 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
36329 29 7b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65  ){.  if( pTrigge
3632a 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  r==0 ) return;. 
3632b 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
3632c 69 67 67 65 72 53 74 65 70 28 64 62 2c 20 70 54  iggerStep(db, pT
3632d 72 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73  rigger->step_lis
3632e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  t);.  sqlite3DbF
3632f 72 65 65 28 64 62 2c 20 70 54 72 69 67 67 65 72  ree(db, pTrigger
36330 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69  ->zName);.  sqli
36331 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54  te3DbFree(db, pT
36332 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 29 3b 0a  rigger->table);.
36333 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
36334 65 74 65 28 64 62 2c 20 70 54 72 69 67 67 65 72  ete(db, pTrigger
36335 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 73 71 6c 69  ->pWhen);.  sqli
36336 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28  te3IdListDelete(
36337 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 43  db, pTrigger->pC
36338 6f 6c 75 6d 6e 73 29 3b 0a 20 20 73 71 6c 69 74  olumns);.  sqlit
36339 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 72  e3DbFree(db, pTr
3633a 69 67 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  igger);.}../*.**
3633b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
3633c 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 72 6f 70  s called to drop
3633d 20 61 20 74 72 69 67 67 65 72 20 66 72 6f 6d 20   a trigger from 
3633e 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
3633f 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ema. .**.** This
36340 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 64   may be called d
36341 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 74 68 65  irectly from the
36342 20 70 61 72 73 65 72 20 61 6e 64 20 74 68 65 72   parser and ther
36343 65 66 6f 72 65 20 69 64 65 6e 74 69 66 69 65 73  efore identifies
36344 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 20  .** the trigger 
36345 62 79 20 6e 61 6d 65 2e 20 20 54 68 65 20 73 71  by name.  The sq
36346 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72  lite3DropTrigger
36347 50 74 72 28 29 20 72 6f 75 74 69 6e 65 20 64 6f  Ptr() routine do
36348 65 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6a  es the.** same j
36349 6f 62 20 61 73 20 74 68 69 73 20 72 6f 75 74 69  ob as this routi
3634a 6e 65 20 65 78 63 65 70 74 20 69 74 20 74 61 6b  ne except it tak
3634b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  es a pointer to 
3634c 74 68 65 20 74 72 69 67 67 65 72 0a 2a 2a 20 69  the trigger.** i
3634d 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 74 72  nstead of the tr
3634e 69 67 67 65 72 20 6e 61 6d 65 2e 0a 2a 2a 2f 0a  igger name..**/.
3634f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
36350 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54  oid sqlite3DropT
36351 72 69 67 67 65 72 28 50 61 72 73 65 20 2a 70 50  rigger(Parse *pP
36352 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70  arse, SrcList *p
36353 4e 61 6d 65 2c 20 69 6e 74 20 6e 6f 45 72 72 29  Name, int noErr)
36354 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  {.  Trigger *pTr
36355 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74  igger = 0;.  int
36356 20 69 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   i;.  const char
36357 20 2a 7a 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63   *zDb;.  const c
36358 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e  har *zName;.  in
36359 74 20 6e 4e 61 6d 65 3b 0a 20 20 73 71 6c 69 74  t nName;.  sqlit
3635a 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
3635b 3e 64 62 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e  >db;..  if( db->
3635c 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67  mallocFailed ) g
3635d 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67 67 65 72  oto drop_trigger
3635e 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20  _cleanup;.  if( 
3635f 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
36360 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
36361 72 73 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  rse) ){.    goto
36362 20 64 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c   drop_trigger_cl
36363 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 61 73  eanup;.  }..  as
36364 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72  sert( pName->nSr
36365 63 3d 3d 31 20 29 3b 0a 20 20 7a 44 62 20 3d 20  c==1 );.  zDb = 
36366 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74  pName->a[0].zDat
36367 61 62 61 73 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d  abase;.  zName =
36368 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61   pName->a[0].zNa
36369 6d 65 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71  me;.  nName = sq
3636a 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
3636b 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ame);.  assert( 
3636c 7a 44 62 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65  zDb!=0 || sqlite
3636d 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
3636e 74 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 66  texes(db) );.  f
3636f 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42  or(i=OMIT_TEMPDB
36370 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
36371 29 7b 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28  ){.    int j = (
36372 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20  i<2) ? i^1 : i; 
36373 20 2f 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20   /* Search TEMP 
36374 62 65 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20  before MAIN */. 
36375 20 20 20 69 66 28 20 7a 44 62 20 26 26 20 73 71     if( zDb && sq
36376 6c 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d  lite3StrICmp(db-
36377 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 7a  >aDb[j].zName, z
36378 44 62 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  Db) ) continue;.
36379 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
3637a 74 65 33 53 63 68 65 6d 61 4d 75 74 65 78 48 65  te3SchemaMutexHe
3637b 6c 64 28 64 62 2c 20 6a 2c 20 30 29 20 29 3b 0a  ld(db, j, 0) );.
3637c 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20 73      pTrigger = s
3637d 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
3637e 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68  (db->aDb[j].pSch
3637f 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 29 2c 20  ema->trigHash), 
36380 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20  zName, nName);. 
36381 20 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20     if( pTrigger 
36382 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69  ) break;.  }.  i
36383 66 28 20 21 70 54 72 69 67 67 65 72 20 29 7b 0a  f( !pTrigger ){.
36384 20 20 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29      if( !noErr )
36385 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
36386 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
36387 22 6e 6f 20 73 75 63 68 20 74 72 69 67 67 65 72  "no such trigger
36388 3a 20 25 53 22 2c 20 70 4e 61 6d 65 2c 20 30 29  : %S", pName, 0)
36389 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
3638a 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65     sqlite3CodeVe
3638b 72 69 66 79 4e 61 6d 65 64 53 63 68 65 6d 61 28  rifyNamedSchema(
3638c 70 50 61 72 73 65 2c 20 7a 44 62 29 3b 0a 20 20  pParse, zDb);.  
3638d 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e    }.    pParse->
3638e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b  checkSchema = 1;
3638f 0a 20 20 20 20 67 6f 74 6f 20 64 72 6f 70 5f 74  .    goto drop_t
36390 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
36391 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 72 6f    }.  sqlite3Dro
36392 70 54 72 69 67 67 65 72 50 74 72 28 70 50 61 72  pTriggerPtr(pPar
36393 73 65 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 0a  se, pTrigger);..
36394 64 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65  drop_trigger_cle
36395 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53  anup:.  sqlite3S
36396 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
36397 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   pName);.}../*.*
36398 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
36399 65 72 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20  er to the Table 
3639a 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 68  structure for th
3639b 65 20 74 61 62 6c 65 20 74 68 61 74 20 61 20 74  e table that a t
3639c 72 69 67 67 65 72 0a 2a 2a 20 69 73 20 73 65 74  rigger.** is set
3639d 20 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54   on..*/.static T
3639e 61 62 6c 65 20 2a 74 61 62 6c 65 4f 66 54 72 69  able *tableOfTri
3639f 67 67 65 72 28 54 72 69 67 67 65 72 20 2a 70 54  gger(Trigger *pT
363a0 72 69 67 67 65 72 29 7b 0a 20 20 69 6e 74 20 6e  rigger){.  int n
363a1 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
363a2 33 30 28 70 54 72 69 67 67 65 72 2d 3e 74 61 62  30(pTrigger->tab
363a3 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  le);.  return sq
363a4 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 70  lite3HashFind(&p
363a5 54 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68  Trigger->pTabSch
363a6 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 54  ema->tblHash, pT
363a7 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 2c 20 6e  rigger->table, n
363a8 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f  );.}.../*.** Dro
363a9 70 20 61 20 74 72 69 67 67 65 72 20 67 69 76 65  p a trigger give
363aa 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
363ab 68 61 74 20 74 72 69 67 67 65 72 2e 20 0a 2a 2f  hat trigger. .*/
363ac 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
363ad 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70  void sqlite3Drop
363ae 54 72 69 67 67 65 72 50 74 72 28 50 61 72 73 65  TriggerPtr(Parse
363af 20 2a 70 50 61 72 73 65 2c 20 54 72 69 67 67 65   *pParse, Trigge
363b0 72 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20  r *pTrigger){.  
363b1 54 61 62 6c 65 20 20 20 2a 70 54 61 62 6c 65 3b  Table   *pTable;
363b2 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71  .  Vdbe *v;.  sq
363b3 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
363b4 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44  se->db;.  int iD
363b5 62 3b 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c 69  b;..  iDb = sqli
363b6 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
363b7 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 72  (pParse->db, pTr
363b8 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 29 3b  igger->pSchema);
363b9 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
363ba 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62  0 && iDb<db->nDb
363bb 20 29 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20 74   );.  pTable = t
363bc 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70 54  ableOfTrigger(pT
363bd 72 69 67 67 65 72 29 3b 0a 20 20 61 73 73 65 72  rigger);.  asser
363be 74 28 20 70 54 61 62 6c 65 20 29 3b 0a 20 20 61  t( pTable );.  a
363bf 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 70  ssert( pTable->p
363c0 53 63 68 65 6d 61 3d 3d 70 54 72 69 67 67 65 72  Schema==pTrigger
363c1 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20 69 44 62  ->pSchema || iDb
363c2 3d 3d 31 20 29 3b 0a 23 69 66 6e 64 65 66 20 53  ==1 );.#ifndef S
363c3 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
363c4 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20  RIZATION.  {.   
363c5 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49   int code = SQLI
363c6 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52 3b  TE_DROP_TRIGGER;
363c7 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
363c8 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *zDb = db->aDb[i
363c9 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63  Db].zName;.    c
363ca 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20  onst char *zTab 
363cb 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  = SCHEMA_TABLE(i
363cc 44 62 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62  Db);.    if( iDb
363cd 3d 3d 31 20 29 20 63 6f 64 65 20 3d 20 53 51 4c  ==1 ) code = SQL
363ce 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52  ITE_DROP_TEMP_TR
363cf 49 47 47 45 52 3b 0a 20 20 20 20 69 66 28 20 73  IGGER;.    if( s
363d0 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
363d1 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 54  pParse, code, pT
363d2 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20 70  rigger->zName, p
363d3 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 44  Table->zName, zD
363d4 62 29 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69  b) ||.      sqli
363d5 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
363d6 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45  rse, SQLITE_DELE
363d7 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62  TE, zTab, 0, zDb
363d8 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
363d9 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  n;.    }.  }.#en
363da 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  dif..  /* Genera
363db 74 65 20 63 6f 64 65 20 74 6f 20 64 65 73 74 72  te code to destr
363dc 6f 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20  oy the database 
363dd 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 74 72  record of the tr
363de 69 67 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73  igger..  */.  as
363df 73 65 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20  sert( pTable!=0 
363e0 29 3b 0a 20 20 69 66 28 20 28 76 20 3d 20 73 71  );.  if( (v = sq
363e1 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
363e2 72 73 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  rse))!=0 ){.    
363e3 69 6e 74 20 62 61 73 65 3b 0a 20 20 20 20 73 74  int base;.    st
363e4 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
363e5 70 4c 69 73 74 20 64 72 6f 70 54 72 69 67 67 65  pList dropTrigge
363e6 72 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20  r[] = {.      { 
363e7 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 30  OP_Rewind,     0
363e8 2c 20 41 44 44 52 28 39 29 2c 20 20 30 7d 2c 0a  , ADDR(9),  0},.
363e9 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
363ea 67 38 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20  g8,    0, 1,    
363eb 20 20 20 20 30 7d 2c 20 2f 2a 20 31 20 2a 2f 0a      0}, /* 1 */.
363ec 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d        { OP_Colum
363ed 6e 2c 20 20 20 20 20 30 2c 20 31 2c 20 20 20 20  n,     0, 1,    
363ee 20 20 20 20 32 7d 2c 0a 20 20 20 20 20 20 7b 20      2},.      { 
363ef 4f 50 5f 4e 65 2c 20 20 20 20 20 20 20 20 20 32  OP_Ne,         2
363f0 2c 20 41 44 44 52 28 38 29 2c 20 20 31 7d 2c 0a  , ADDR(8),  1},.
363f1 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
363f2 67 38 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20  g8,    0, 1,    
363f3 20 20 20 20 30 7d 2c 20 2f 2a 20 34 3a 20 22 74      0}, /* 4: "t
363f4 72 69 67 67 65 72 22 20 2a 2f 0a 20 20 20 20 20  rigger" */.     
363f5 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20   { OP_Column,   
363f6 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 32    0, 0,        2
363f7 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65  },.      { OP_Ne
363f8 2c 20 20 20 20 20 20 20 20 20 32 2c 20 41 44 44  ,         2, ADD
363f9 52 28 38 29 2c 20 20 31 7d 2c 0a 20 20 20 20 20  R(8),  1},.     
363fa 20 7b 20 4f 50 5f 44 65 6c 65 74 65 2c 20 20 20   { OP_Delete,   
363fb 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 30    0, 0,        0
363fc 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65  },.      { OP_Ne
363fd 78 74 2c 20 20 20 20 20 20 20 30 2c 20 41 44 44  xt,       0, ADD
363fe 52 28 31 29 2c 20 20 30 7d 2c 20 2f 2a 20 38 20  R(1),  0}, /* 8 
363ff 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 73  */.    };..    s
36400 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
36401 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
36402 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  , 0, iDb);.    s
36403 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72  qlite3OpenMaster
36404 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
36405 62 29 3b 0a 20 20 20 20 62 61 73 65 20 3d 20 73  b);.    base = s
36406 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
36407 69 73 74 28 76 2c 20 20 41 72 72 61 79 53 69 7a  ist(v,  ArraySiz
36408 65 28 64 72 6f 70 54 72 69 67 67 65 72 29 2c 20  e(dropTrigger), 
36409 64 72 6f 70 54 72 69 67 67 65 72 29 3b 0a 20 20  dropTrigger);.  
3640a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
3640b 6e 67 65 50 34 28 76 2c 20 62 61 73 65 2b 31 2c  ngeP4(v, base+1,
3640c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65   pTrigger->zName
3640d 2c 20 50 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b  , P4_TRANSIENT);
3640e 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
3640f 43 68 61 6e 67 65 50 34 28 76 2c 20 62 61 73 65  ChangeP4(v, base
36410 2b 34 2c 20 22 74 72 69 67 67 65 72 22 2c 20 50  +4, "trigger", P
36411 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  4_STATIC);.    s
36412 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
36413 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ie(pParse, iDb);
36414 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
36415 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
36416 73 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  se, 0, 0);.    s
36417 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
36418 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67 67  (v, OP_DropTrigg
36419 65 72 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70  er, iDb, 0, 0, p
3641a 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20  Trigger->zName, 
3641b 30 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72  0);.    if( pPar
3641c 73 65 2d 3e 6e 4d 65 6d 3c 33 20 29 7b 0a 20 20  se->nMem<3 ){.  
3641d 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
3641e 20 3d 20 33 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 3;.    }.  }.
3641f 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  }../*.** Remove 
36420 61 20 74 72 69 67 67 65 72 20 66 72 6f 6d 20 74  a trigger from t
36421 68 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 6f  he hash tables o
36422 66 20 74 68 65 20 73 71 6c 69 74 65 2a 20 70 6f  f the sqlite* po
36423 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inter..*/.SQLITE
36424 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
36425 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
36426 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c 69  leteTrigger(sqli
36427 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62  te3 *db, int iDb
36428 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
36429 61 6d 65 29 7b 0a 20 20 54 72 69 67 67 65 72 20  ame){.  Trigger 
3642a 2a 70 54 72 69 67 67 65 72 3b 0a 20 20 48 61 73  *pTrigger;.  Has
3642b 68 20 2a 70 48 61 73 68 3b 0a 0a 20 20 61 73 73  h *pHash;..  ass
3642c 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65  ert( sqlite3Sche
3642d 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20  maMutexHeld(db, 
3642e 69 44 62 2c 20 30 29 20 29 3b 0a 20 20 70 48 61  iDb, 0) );.  pHa
3642f 73 68 20 3d 20 26 28 64 62 2d 3e 61 44 62 5b 69  sh = &(db->aDb[i
36430 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69  Db].pSchema->tri
36431 67 48 61 73 68 29 3b 0a 20 20 70 54 72 69 67 67  gHash);.  pTrigg
36432 65 72 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  er = sqlite3Hash
36433 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a 4e  Insert(pHash, zN
36434 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  ame, sqlite3Strl
36435 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 20 30 29 3b  en30(zName), 0);
36436 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 54  .  if( ALWAYS(pT
36437 72 69 67 67 65 72 29 20 29 7b 0a 20 20 20 20 69  rigger) ){.    i
36438 66 28 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63  f( pTrigger->pSc
36439 68 65 6d 61 3d 3d 70 54 72 69 67 67 65 72 2d 3e  hema==pTrigger->
3643a 70 54 61 62 53 63 68 65 6d 61 20 29 7b 0a 20 20  pTabSchema ){.  
3643b 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
3643c 3d 20 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72  = tableOfTrigger
3643d 28 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20  (pTrigger);.    
3643e 20 20 54 72 69 67 67 65 72 20 2a 2a 70 70 3b 0a    Trigger **pp;.
3643f 20 20 20 20 20 20 66 6f 72 28 70 70 3d 26 70 54        for(pp=&pT
36440 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 20 2a 70  ab->pTrigger; *p
36441 70 21 3d 70 54 72 69 67 67 65 72 3b 20 70 70 3d  p!=pTrigger; pp=
36442 26 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29  &((*pp)->pNext))
36443 3b 0a 20 20 20 20 20 20 2a 70 70 20 3d 20 28 2a  ;.      *pp = (*
36444 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  pp)->pNext;.    
36445 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c  }.    sqlite3Del
36446 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70  eteTrigger(db, p
36447 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 64 62  Trigger);.    db
36448 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54  ->flags |= SQLIT
36449 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b  E_InternChanges;
3644a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45  .  }.}../*.** pE
3644b 4c 69 73 74 20 69 73 20 74 68 65 20 53 45 54 20  List is the SET 
3644c 63 6c 61 75 73 65 20 6f 66 20 61 6e 20 55 50 44  clause of an UPD
3644d 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ATE statement.  
3644e 45 61 63 68 20 65 6e 74 72 79 0a 2a 2a 20 69 6e  Each entry.** in
3644f 20 70 45 4c 69 73 74 20 69 73 20 6f 66 20 74 68   pEList is of th
36450 65 20 66 6f 72 6d 61 74 20 3c 69 64 3e 3d 3c 65  e format <id>=<e
36451 78 70 72 3e 2e 20 20 49 66 20 61 6e 79 20 6f 66  xpr>.  If any of
36452 20 74 68 65 20 65 6e 74 72 69 65 73 0a 2a 2a 20   the entries.** 
36453 69 6e 20 70 45 4c 69 73 74 20 68 61 76 65 20 61  in pEList have a
36454 6e 20 3c 69 64 3e 20 77 68 69 63 68 20 6d 61 74  n <id> which mat
36455 63 68 65 73 20 61 6e 20 69 64 65 6e 74 69 66 69  ches an identifi
36456 65 72 20 69 6e 20 70 49 64 4c 69 73 74 2c 0a 2a  er in pIdList,.*
36457 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20 54 52  * then return TR
36458 55 45 2e 20 20 49 66 20 70 49 64 4c 69 73 74 3d  UE.  If pIdList=
36459 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69  =NULL, then it i
3645a 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 0a 2a  s considered a.*
3645b 2a 20 77 69 6c 64 63 61 72 64 20 74 68 61 74 20  * wildcard that 
3645c 6d 61 74 63 68 65 73 20 61 6e 79 74 68 69 6e 67  matches anything
3645d 2e 20 20 4c 69 6b 65 77 69 73 65 20 69 66 20 70  .  Likewise if p
3645e 45 4c 69 73 74 3d 3d 4e 55 4c 4c 20 74 68 65 6e  EList==NULL then
3645f 0a 2a 2a 20 69 74 20 6d 61 74 63 68 65 73 20 61  .** it matches a
36460 6e 79 74 68 69 6e 67 20 73 6f 20 61 6c 77 61 79  nything so alway
36461 73 20 72 65 74 75 72 6e 20 74 72 75 65 2e 20 20  s return true.  
36462 52 65 74 75 72 6e 20 66 61 6c 73 65 20 6f 6e 6c  Return false onl
36463 79 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73  y.** if there is
36464 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74   no match..*/.st
36465 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 43 6f  atic int checkCo
36466 6c 75 6d 6e 4f 76 65 72 6c 61 70 28 49 64 4c 69  lumnOverlap(IdLi
36467 73 74 20 2a 70 49 64 4c 69 73 74 2c 20 45 78 70  st *pIdList, Exp
36468 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 29 7b 0a  rList *pEList){.
36469 20 20 69 6e 74 20 65 3b 0a 20 20 69 66 28 20 70    int e;.  if( p
3646a 49 64 4c 69 73 74 3d 3d 30 20 7c 7c 20 4e 45 56  IdList==0 || NEV
3646b 45 52 28 70 45 4c 69 73 74 3d 3d 30 29 20 29 20  ER(pEList==0) ) 
3646c 72 65 74 75 72 6e 20 31 3b 0a 20 20 66 6f 72 28  return 1;.  for(
3646d 65 3d 30 3b 20 65 3c 70 45 4c 69 73 74 2d 3e 6e  e=0; e<pEList->n
3646e 45 78 70 72 3b 20 65 2b 2b 29 7b 0a 20 20 20 20  Expr; e++){.    
3646f 69 66 28 20 73 71 6c 69 74 65 33 49 64 4c 69 73  if( sqlite3IdLis
36470 74 49 6e 64 65 78 28 70 49 64 4c 69 73 74 2c 20  tIndex(pIdList, 
36471 70 45 4c 69 73 74 2d 3e 61 5b 65 5d 2e 7a 4e 61  pEList->a[e].zNa
36472 6d 65 29 3e 3d 30 20 29 20 72 65 74 75 72 6e 20  me)>=0 ) return 
36473 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
36474 30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  0; .}../*.** Ret
36475 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c  urn a list of al
36476 6c 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61  l triggers on ta
36477 62 6c 65 20 70 54 61 62 20 69 66 20 74 68 65 72  ble pTab if ther
36478 65 20 65 78 69 73 74 73 20 61 74 20 6c 65 61 73  e exists at leas
36479 74 0a 2a 2a 20 6f 6e 65 20 74 72 69 67 67 65 72  t.** one trigger
3647a 20 74 68 61 74 20 6d 75 73 74 20 62 65 20 66 69   that must be fi
3647b 72 65 64 20 77 68 65 6e 20 61 6e 20 6f 70 65 72  red when an oper
3647c 61 74 69 6f 6e 20 6f 66 20 74 79 70 65 20 27 6f  ation of type 'o
3647d 70 27 20 69 73 20 0a 2a 2a 20 70 65 72 66 6f 72  p' is .** perfor
3647e 6d 65 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  med on the table
3647f 2c 20 61 6e 64 2c 20 69 66 20 74 68 61 74 20 6f  , and, if that o
36480 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 55  peration is an U
36481 50 44 41 54 45 2c 20 69 66 20 61 74 0a 2a 2a 20  PDATE, if at.** 
36482 6c 65 61 73 74 20 6f 6e 65 20 6f 66 20 74 68 65  least one of the
36483 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 43 68 61   columns in pCha
36484 6e 67 65 73 20 69 73 20 62 65 69 6e 67 20 6d 6f  nges is being mo
36485 64 69 66 69 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  dified..*/.SQLIT
36486 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67 65  E_PRIVATE Trigge
36487 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65  r *sqlite3Trigge
36488 72 73 45 78 69 73 74 28 0a 20 20 50 61 72 73 65  rsExist(.  Parse
36489 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
3648a 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
3648b 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
3648c 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20  pTab,           
3648d 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68   /* The table th
3648e 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74  e contains the t
3648f 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74  riggers */.  int
36490 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   op,            
36491 20 20 20 20 20 2f 2a 20 6f 6e 65 20 6f 66 20 54       /* one of T
36492 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 49 4e 53  K_DELETE, TK_INS
36493 45 52 54 2c 20 54 4b 5f 55 50 44 41 54 45 20 2a  ERT, TK_UPDATE *
36494 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43  /.  ExprList *pC
36495 68 61 6e 67 65 73 2c 20 20 20 20 20 2f 2a 20 43  hanges,     /* C
36496 6f 6c 75 6d 6e 73 20 74 68 61 74 20 63 68 61 6e  olumns that chan
36497 67 65 20 69 6e 20 61 6e 20 55 50 44 41 54 45 20  ge in an UPDATE 
36498 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69  statement */.  i
36499 6e 74 20 2a 70 4d 61 73 6b 20 20 20 20 20 20 20  nt *pMask       
3649a 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d         /* OUT: M
3649b 61 73 6b 20 6f 66 20 54 52 49 47 47 45 52 5f 42  ask of TRIGGER_B
3649c 45 46 4f 52 45 7c 54 52 49 47 47 45 52 5f 41 46  EFORE|TRIGGER_AF
3649d 54 45 52 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  TER */.){.  int 
3649e 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 54 72 69 67  mask = 0;.  Trig
3649f 67 65 72 20 2a 70 4c 69 73 74 20 3d 20 30 3b 0a  ger *pList = 0;.
364a0 20 20 54 72 69 67 67 65 72 20 2a 70 3b 0a 0a 20    Trigger *p;.. 
364a1 20 69 66 28 20 28 70 50 61 72 73 65 2d 3e 64 62   if( (pParse->db
364a2 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
364a3 5f 45 6e 61 62 6c 65 54 72 69 67 67 65 72 29 21  _EnableTrigger)!
364a4 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20  =0 ){.    pList 
364a5 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
364a6 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61  List(pParse, pTa
364a7 62 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  b);.  }.  assert
364a8 28 20 70 4c 69 73 74 3d 3d 30 20 7c 7c 20 49 73  ( pList==0 || Is
364a9 56 69 72 74 75 61 6c 28 70 54 61 62 29 3d 3d 30  Virtual(pTab)==0
364aa 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 4c 69 73   );.  for(p=pLis
364ab 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  t; p; p=p->pNext
364ac 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6f 70  ){.    if( p->op
364ad 3d 3d 6f 70 20 26 26 20 63 68 65 63 6b 43 6f 6c  ==op && checkCol
364ae 75 6d 6e 4f 76 65 72 6c 61 70 28 70 2d 3e 70 43  umnOverlap(p->pC
364af 6f 6c 75 6d 6e 73 2c 20 70 43 68 61 6e 67 65 73  olumns, pChanges
364b0 29 20 29 7b 0a 20 20 20 20 20 20 6d 61 73 6b 20  ) ){.      mask 
364b1 7c 3d 20 70 2d 3e 74 72 5f 74 6d 3b 0a 20 20 20  |= p->tr_tm;.   
364b2 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4d 61   }.  }.  if( pMa
364b3 73 6b 20 29 7b 0a 20 20 20 20 2a 70 4d 61 73 6b  sk ){.    *pMask
364b4 20 3d 20 6d 61 73 6b 3b 0a 20 20 7d 0a 20 20 72   = mask;.  }.  r
364b5 65 74 75 72 6e 20 28 6d 61 73 6b 20 3f 20 70 4c  eturn (mask ? pL
364b6 69 73 74 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  ist : 0);.}../*.
364b7 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 70  ** Convert the p
364b8 53 74 65 70 2d 3e 74 61 72 67 65 74 20 74 6f 6b  Step->target tok
364b9 65 6e 20 69 6e 74 6f 20 61 20 53 72 63 4c 69 73  en into a SrcLis
364ba 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70  t and return a p
364bb 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 61  ointer.** to tha
364bc 74 20 53 72 63 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a  t SrcList..**.**
364bd 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 64   This routine ad
364be 64 73 20 61 20 73 70 65 63 69 66 69 63 20 64 61  ds a specific da
364bf 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 69 66 20  tabase name, if 
364c0 6e 65 65 64 65 64 2c 20 74 6f 20 74 68 65 20 74  needed, to the t
364c1 61 72 67 65 74 20 77 68 65 6e 0a 2a 2a 20 66 6f  arget when.** fo
364c2 72 6d 69 6e 67 20 74 68 65 20 53 72 63 4c 69 73  rming the SrcLis
364c3 74 2e 20 20 54 68 69 73 20 70 72 65 76 65 6e 74  t.  This prevent
364c4 73 20 61 20 74 72 69 67 67 65 72 20 69 6e 20 6f  s a trigger in o
364c5 6e 65 20 64 61 74 61 62 61 73 65 20 66 72 6f 6d  ne database from
364c6 0a 2a 2a 20 72 65 66 65 72 72 69 6e 67 20 74 6f  .** referring to
364c7 20 61 20 74 61 72 67 65 74 20 69 6e 20 61 6e 6f   a target in ano
364c8 74 68 65 72 20 64 61 74 61 62 61 73 65 2e 20 20  ther database.  
364c9 41 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20  An exception is 
364ca 77 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 69 67  when the.** trig
364cb 67 65 72 20 69 73 20 69 6e 20 54 45 4d 50 20 69  ger is in TEMP i
364cc 6e 20 77 68 69 63 68 20 63 61 73 65 20 69 74 20  n which case it 
364cd 63 61 6e 20 72 65 66 65 72 20 74 6f 20 61 6e 79  can refer to any
364ce 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20   other database 
364cf 69 74 0a 2a 2a 20 77 61 6e 74 73 2e 0a 2a 2f 0a  it.** wants..*/.
364d0 73 74 61 74 69 63 20 53 72 63 4c 69 73 74 20 2a  static SrcList *
364d1 74 61 72 67 65 74 53 72 63 4c 69 73 74 28 0a 20  targetSrcList(. 
364d2 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
364d3 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
364d4 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
364d5 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70    TriggerStep *p
364d6 53 74 65 70 20 20 20 2f 2a 20 54 68 65 20 74 72  Step   /* The tr
364d7 69 67 67 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  igger containing
364d8 20 74 68 65 20 74 61 72 67 65 74 20 74 6f 6b 65   the target toke
364d9 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44  n */.){.  int iD
364da 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b;             /
364db 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64  * Index of the d
364dc 61 74 61 62 61 73 65 20 74 6f 20 75 73 65 20 2a  atabase to use *
364dd 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
364de 63 3b 20 20 20 20 20 20 20 2f 2a 20 53 72 63 4c  c;       /* SrcL
364df 69 73 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e  ist to be return
364e0 65 64 20 2a 2f 0a 0a 20 20 70 53 72 63 20 3d 20  ed */..  pSrc = 
364e1 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
364e2 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c  pend(pParse->db,
364e3 20 30 2c 20 26 70 53 74 65 70 2d 3e 74 61 72 67   0, &pStep->targ
364e4 65 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53  et, 0);.  if( pS
364e5 72 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  rc ){.    assert
364e6 28 20 70 53 72 63 2d 3e 6e 53 72 63 3e 30 20 29  ( pSrc->nSrc>0 )
364e7 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53  ;.    assert( pS
364e8 72 63 2d 3e 61 21 3d 30 20 29 3b 0a 20 20 20 20  rc->a!=0 );.    
364e9 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
364ea 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
364eb 65 2d 3e 64 62 2c 20 70 53 74 65 70 2d 3e 70 54  e->db, pStep->pT
364ec 72 69 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  rig->pSchema);. 
364ed 20 20 20 69 66 28 20 69 44 62 3d 3d 30 20 7c 7c     if( iDb==0 ||
364ee 20 69 44 62 3e 3d 32 20 29 7b 0a 20 20 20 20 20   iDb>=2 ){.     
364ef 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
364f0 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20  Parse->db;.     
364f1 20 61 73 73 65 72 74 28 20 69 44 62 3c 70 50 61   assert( iDb<pPa
364f2 72 73 65 2d 3e 64 62 2d 3e 6e 44 62 20 29 3b 0a  rse->db->nDb );.
364f3 20 20 20 20 20 20 70 53 72 63 2d 3e 61 5b 70 53        pSrc->a[pS
364f4 72 63 2d 3e 6e 53 72 63 2d 31 5d 2e 7a 44 61 74  rc->nSrc-1].zDat
364f5 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44  abase = sqlite3D
364f6 62 53 74 72 44 75 70 28 64 62 2c 20 64 62 2d 3e  bStrDup(db, db->
364f7 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b  aDb[iDb].zName);
364f8 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
364f9 75 72 6e 20 70 53 72 63 3b 0a 7d 0a 0a 2f 2a 0a  urn pSrc;.}../*.
364fa 2a 2a 20 47 65 6e 65 72 61 74 65 20 56 44 42 45  ** Generate VDBE
364fb 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 73 74   code for the st
364fc 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65 20  atements inside 
364fd 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 73 69  the body of a si
364fe 6e 67 6c 65 20 0a 2a 2a 20 74 72 69 67 67 65 72  ngle .** trigger
364ff 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
36500 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67 72  codeTriggerProgr
36501 61 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  am(.  Parse *pPa
36502 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
36503 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
36504 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67  ntext */.  Trigg
36505 65 72 53 74 65 70 20 2a 70 53 74 65 70 4c 69 73  erStep *pStepLis
36506 74 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  t,   /* List of 
36507 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64  statements insid
36508 65 20 74 68 65 20 74 72 69 67 67 65 72 20 62 6f  e the trigger bo
36509 64 79 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f  dy */.  int orco
3650a 6e 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nf              
3650b 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 61 6c    /* Conflict al
3650c 67 6f 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62 6f  gorithm. (OE_Abo
3650d 72 74 2c 20 65 74 63 29 20 2a 2f 20 20 0a 29 7b  rt, etc) */  .){
3650e 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
3650f 70 53 74 65 70 3b 0a 20 20 56 64 62 65 20 2a 76  pStep;.  Vdbe *v
36510 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
36511 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
36512 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
36513 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
36514 3e 70 54 72 69 67 67 65 72 54 61 62 20 26 26 20  >pTriggerTab && 
36515 70 50 61 72 73 65 2d 3e 70 54 6f 70 6c 65 76 65  pParse->pTopleve
36516 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  l );.  assert( p
36517 53 74 65 70 4c 69 73 74 20 29 3b 0a 20 20 61 73  StepList );.  as
36518 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20  sert( v!=0 );.  
36519 66 6f 72 28 70 53 74 65 70 3d 70 53 74 65 70 4c  for(pStep=pStepL
3651a 69 73 74 3b 20 70 53 74 65 70 3b 20 70 53 74 65  ist; pStep; pSte
3651b 70 3d 70 53 74 65 70 2d 3e 70 4e 65 78 74 29 7b  p=pStep->pNext){
3651c 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  .    /* Figure o
3651d 75 74 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49  ut the ON CONFLI
3651e 43 54 20 70 6f 6c 69 63 79 20 74 68 61 74 20 77  CT policy that w
3651f 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20  ill be used for 
36520 74 68 69 73 20 73 74 65 70 0a 20 20 20 20 2a 2a  this step.    **
36521 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20   of the trigger 
36522 70 72 6f 67 72 61 6d 2e 20 49 66 20 74 68 65 20  program. If the 
36523 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63  statement that c
36524 61 75 73 65 64 20 74 68 69 73 20 74 72 69 67 67  aused this trigg
36525 65 72 0a 20 20 20 20 2a 2a 20 74 6f 20 66 69 72  er.    ** to fir
36526 65 20 68 61 64 20 61 6e 20 65 78 70 6c 69 63 69  e had an explici
36527 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 2c 20 74  t ON CONFLICT, t
36528 68 65 6e 20 75 73 65 20 69 74 2e 20 4f 74 68 65  hen use it. Othe
36529 72 77 69 73 65 2c 20 75 73 65 0a 20 20 20 20 2a  rwise, use.    *
3652a 2a 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43  * the ON CONFLIC
3652b 54 20 70 6f 6c 69 63 79 20 74 68 61 74 20 77 61  T policy that wa
3652c 73 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70  s specified as p
3652d 61 72 74 20 6f 66 20 74 68 65 20 74 72 69 67 67  art of the trigg
3652e 65 72 0a 20 20 20 20 2a 2a 20 73 74 65 70 20 73  er.    ** step s
3652f 74 61 74 65 6d 65 6e 74 2e 20 45 78 61 6d 70 6c  tatement. Exampl
36530 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e:.    **.    **
36531 20 20 20 43 52 45 41 54 45 20 54 52 49 47 47 45     CREATE TRIGGE
36532 52 20 41 46 54 45 52 20 49 4e 53 45 52 54 20 4f  R AFTER INSERT O
36533 4e 20 74 31 20 42 45 47 49 4e 3b 0a 20 20 20 20  N t1 BEGIN;.    
36534 2a 2a 20 20 20 20 20 49 4e 53 45 52 54 20 4f 52  **     INSERT OR
36535 20 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 74 32   REPLACE INTO t2
36536 20 56 41 4c 55 45 53 28 6e 65 77 2e 61 2c 20 6e   VALUES(new.a, n
36537 65 77 2e 62 29 3b 0a 20 20 20 20 2a 2a 20 20 20  ew.b);.    **   
36538 45 4e 44 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20  END;.    **.    
36539 2a 2a 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  **   INSERT INTO
3653a 20 74 31 20 2e 2e 2e 20 3b 20 20 20 20 20 20 20   t1 ... ;       
3653b 20 20 20 20 20 2d 2d 20 69 6e 73 65 72 74 20 69       -- insert i
3653c 6e 74 6f 20 74 32 20 75 73 65 73 20 52 45 50 4c  nto t2 uses REPL
3653d 41 43 45 20 70 6f 6c 69 63 79 0a 20 20 20 20 2a  ACE policy.    *
3653e 2a 20 20 20 49 4e 53 45 52 54 20 4f 52 20 49 47  *   INSERT OR IG
3653f 4e 4f 52 45 20 49 4e 54 4f 20 74 31 20 2e 2e 2e  NORE INTO t1 ...
36540 20 3b 20 20 2d 2d 20 69 6e 73 65 72 74 20 69 6e   ;  -- insert in
36541 74 6f 20 74 32 20 75 73 65 73 20 49 47 4e 4f 52  to t2 uses IGNOR
36542 45 20 70 6f 6c 69 63 79 0a 20 20 20 20 2a 2f 0a  E policy.    */.
36543 20 20 20 20 70 50 61 72 73 65 2d 3e 65 4f 72 63      pParse->eOrc
36544 6f 6e 66 20 3d 20 28 6f 72 63 6f 6e 66 3d 3d 4f  onf = (orconf==O
36545 45 5f 44 65 66 61 75 6c 74 29 3f 70 53 74 65 70  E_Default)?pStep
36546 2d 3e 6f 72 63 6f 6e 66 3a 28 75 38 29 6f 72 63  ->orconf:(u8)orc
36547 6f 6e 66 3b 0a 0a 20 20 20 20 73 77 69 74 63 68  onf;..    switch
36548 28 20 70 53 74 65 70 2d 3e 6f 70 20 29 7b 0a 20  ( pStep->op ){. 
36549 20 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50 44       case TK_UPD
3654a 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 73  ATE: {.        s
3654b 71 6c 69 74 65 33 55 70 64 61 74 65 28 70 50 61  qlite3Update(pPa
3654c 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  rse, .          
3654d 74 61 72 67 65 74 53 72 63 4c 69 73 74 28 70 50  targetSrcList(pP
3654e 61 72 73 65 2c 20 70 53 74 65 70 29 2c 0a 20 20  arse, pStep),.  
3654f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
36550 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
36551 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 2c  Step->pExprList,
36552 20 30 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20   0), .          
36553 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
36554 62 2c 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65  b, pStep->pWhere
36555 2c 20 30 29 2c 20 0a 20 20 20 20 20 20 20 20 20  , 0), .         
36556 20 70 50 61 72 73 65 2d 3e 65 4f 72 63 6f 6e 66   pParse->eOrconf
36557 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .        );.    
36558 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
36559 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b   }.      case TK
3655a 5f 49 4e 53 45 52 54 3a 20 7b 0a 20 20 20 20 20  _INSERT: {.     
3655b 20 20 20 73 71 6c 69 74 65 33 49 6e 73 65 72 74     sqlite3Insert
3655c 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
3655d 20 20 20 20 74 61 72 67 65 74 53 72 63 4c 69 73      targetSrcLis
3655e 74 28 70 50 61 72 73 65 2c 20 70 53 74 65 70 29  t(pParse, pStep)
3655f 2c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ,.          sqli
36560 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
36561 62 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c  b, pStep->pExprL
36562 69 73 74 2c 20 30 29 2c 20 0a 20 20 20 20 20 20  ist, 0), .      
36563 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
36564 74 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d 3e  tDup(db, pStep->
36565 70 53 65 6c 65 63 74 2c 20 30 29 2c 20 0a 20 20  pSelect, 0), .  
36566 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 49          sqlite3I
36567 64 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53 74  dListDup(db, pSt
36568 65 70 2d 3e 70 49 64 4c 69 73 74 29 2c 20 0a 20  ep->pIdList), . 
36569 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d           pParse-
3656a 3e 65 4f 72 63 6f 6e 66 0a 20 20 20 20 20 20 20  >eOrconf.       
3656b 20 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   );.        brea
3656c 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
3656d 20 63 61 73 65 20 54 4b 5f 44 45 4c 45 54 45 3a   case TK_DELETE:
3656e 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
3656f 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 70 50 61  e3DeleteFrom(pPa
36570 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  rse, .          
36571 74 61 72 67 65 74 53 72 63 4c 69 73 74 28 70 50  targetSrcList(pP
36572 61 72 73 65 2c 20 70 53 74 65 70 29 2c 0a 20 20  arse, pStep),.  
36573 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
36574 78 70 72 44 75 70 28 64 62 2c 20 70 53 74 65 70  xprDup(db, pStep
36575 2d 3e 70 57 68 65 72 65 2c 20 30 29 0a 20 20 20  ->pWhere, 0).   
36576 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20       );.        
36577 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
36578 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 61 73       default: as
36579 73 65 72 74 28 20 70 53 74 65 70 2d 3e 6f 70 3d  sert( pStep->op=
3657a 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 20 7b 0a  =TK_SELECT ); {.
3657b 20 20 20 20 20 20 20 20 53 65 6c 65 63 74 44 65          SelectDe
3657c 73 74 20 73 44 65 73 74 3b 0a 20 20 20 20 20 20  st sDest;.      
3657d 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
3657e 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  t = sqlite3Selec
3657f 74 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d 3e  tDup(db, pStep->
36580 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 20  pSelect, 0);.   
36581 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
36582 63 74 44 65 73 74 49 6e 69 74 28 26 73 44 65 73  ctDestInit(&sDes
36583 74 2c 20 53 52 54 5f 44 69 73 63 61 72 64 2c 20  t, SRT_Discard, 
36584 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
36585 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
36586 2c 20 70 53 65 6c 65 63 74 2c 20 26 73 44 65 73  , pSelect, &sDes
36587 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
36588 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
36589 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20  db, pSelect);.  
3658a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3658b 20 20 20 7d 0a 20 20 20 20 7d 20 0a 20 20 20 20     }.    } .    
3658c 69 66 28 20 70 53 74 65 70 2d 3e 6f 70 21 3d 54  if( pStep->op!=T
3658d 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20  K_SELECT ){.    
3658e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3658f 4f 70 30 28 76 2c 20 4f 50 5f 52 65 73 65 74 43  Op0(v, OP_ResetC
36590 6f 75 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ount);.    }.  }
36591 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ..  return 0;.}.
36592 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
36593 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  EBUG./*.** This 
36594 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
36595 20 74 6f 20 61 64 64 20 56 64 62 65 43 6f 6d 6d   to add VdbeComm
36596 65 6e 74 28 29 20 61 6e 6e 6f 74 61 74 69 6f 6e  ent() annotation
36597 73 20 74 6f 20 61 20 56 44 42 45 0a 2a 2a 20 70  s to a VDBE.** p
36598 72 6f 67 72 61 6d 2e 20 49 74 20 69 73 20 6e 6f  rogram. It is no
36599 74 20 75 73 65 64 20 69 6e 20 70 72 6f 64 75 63  t used in produc
3659a 74 69 6f 6e 20 63 6f 64 65 2c 20 6f 6e 6c 79 20  tion code, only 
3659b 66 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a  for debugging..*
3659c 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
3659d 68 61 72 20 2a 6f 6e 45 72 72 6f 72 54 65 78 74  har *onErrorText
3659e 28 69 6e 74 20 6f 6e 45 72 72 6f 72 29 7b 0a 20  (int onError){. 
3659f 20 73 77 69 74 63 68 28 20 6f 6e 45 72 72 6f 72   switch( onError
365a0 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f   ){.    case OE_
365a1 41 62 6f 72 74 3a 20 20 20 20 72 65 74 75 72 6e  Abort:    return
365a2 20 22 61 62 6f 72 74 22 3b 0a 20 20 20 20 63 61   "abort";.    ca
365a3 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 20  se OE_Rollback: 
365a4 72 65 74 75 72 6e 20 22 72 6f 6c 6c 62 61 63 6b  return "rollback
365a5 22 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 46  ";.    case OE_F
365a6 61 69 6c 3a 20 20 20 20 20 72 65 74 75 72 6e 20  ail:     return 
365a7 22 66 61 69 6c 22 3b 0a 20 20 20 20 63 61 73 65  "fail";.    case
365a8 20 4f 45 5f 52 65 70 6c 61 63 65 3a 20 20 72 65   OE_Replace:  re
365a9 74 75 72 6e 20 22 72 65 70 6c 61 63 65 22 3b 0a  turn "replace";.
365aa 20 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f      case OE_Igno
365ab 72 65 3a 20 20 20 72 65 74 75 72 6e 20 22 69 67  re:   return "ig
365ac 6e 6f 72 65 22 3b 0a 20 20 20 20 63 61 73 65 20  nore";.    case 
365ad 4f 45 5f 44 65 66 61 75 6c 74 3a 20 20 72 65 74  OE_Default:  ret
365ae 75 72 6e 20 22 64 65 66 61 75 6c 74 22 3b 0a 20  urn "default";. 
365af 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 6e 2f 61   }.  return "n/a
365b0 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  ";.}.#endif../*.
365b1 2a 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74  ** Parse context
365b2 20 73 74 72 75 63 74 75 72 65 20 70 46 72 6f 6d   structure pFrom
365b3 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 75   has just been u
365b4 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  sed to create a 
365b5 73 75 62 2d 76 64 62 65 0a 2a 2a 20 28 74 72 69  sub-vdbe.** (tri
365b6 67 67 65 72 20 70 72 6f 67 72 61 6d 29 2e 20 49  gger program). I
365b7 66 20 61 6e 20 65 72 72 6f 72 20 68 61 73 20 6f  f an error has o
365b8 63 63 75 72 72 65 64 2c 20 74 72 61 6e 73 66 65  ccurred, transfe
365b9 72 20 65 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74  r error informat
365ba 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 70 46 72 6f  ion.** from pFro
365bb 6d 20 74 6f 20 70 54 6f 2e 0a 2a 2f 0a 73 74 61  m to pTo..*/.sta
365bc 74 69 63 20 76 6f 69 64 20 74 72 61 6e 73 66 65  tic void transfe
365bd 72 50 61 72 73 65 45 72 72 6f 72 28 50 61 72 73  rParseError(Pars
365be 65 20 2a 70 54 6f 2c 20 50 61 72 73 65 20 2a 70  e *pTo, Parse *p
365bf 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28  From){.  assert(
365c0 20 70 46 72 6f 6d 2d 3e 7a 45 72 72 4d 73 67 3d   pFrom->zErrMsg=
365c1 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 6e 45 72  =0 || pFrom->nEr
365c2 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
365c3 54 6f 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 7c  To->zErrMsg==0 |
365c4 7c 20 70 54 6f 2d 3e 6e 45 72 72 20 29 3b 0a 20  | pTo->nErr );. 
365c5 20 69 66 28 20 70 54 6f 2d 3e 6e 45 72 72 3d 3d   if( pTo->nErr==
365c6 30 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 7a 45  0 ){.    pTo->zE
365c7 72 72 4d 73 67 20 3d 20 70 46 72 6f 6d 2d 3e 7a  rrMsg = pFrom->z
365c8 45 72 72 4d 73 67 3b 0a 20 20 20 20 70 54 6f 2d  ErrMsg;.    pTo-
365c9 3e 6e 45 72 72 20 3d 20 70 46 72 6f 6d 2d 3e 6e  >nErr = pFrom->n
365ca 45 72 72 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Err;.  }else{.  
365cb 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
365cc 70 46 72 6f 6d 2d 3e 64 62 2c 20 70 46 72 6f 6d  pFrom->db, pFrom
365cd 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a  ->zErrMsg);.  }.
365ce 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
365cf 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 61 20 6e  and populate a n
365d0 65 77 20 54 72 69 67 67 65 72 50 72 67 20 6f 62  ew TriggerPrg ob
365d1 6a 65 63 74 20 77 69 74 68 20 61 20 73 75 62 2d  ject with a sub-
365d2 70 72 6f 67 72 61 6d 20 0a 2a 2a 20 69 6d 70 6c  program .** impl
365d3 65 6d 65 6e 74 69 6e 67 20 74 72 69 67 67 65 72  ementing trigger
365d4 20 70 54 72 69 67 67 65 72 20 77 69 74 68 20 4f   pTrigger with O
365d5 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63  N CONFLICT polic
365d6 79 20 6f 72 63 6f 6e 66 2e 0a 2a 2f 0a 73 74 61  y orconf..*/.sta
365d7 74 69 63 20 54 72 69 67 67 65 72 50 72 67 20 2a  tic TriggerPrg *
365d8 63 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 0a  codeRowTrigger(.
365d9 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
365da 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
365db 74 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20  t parse context 
365dc 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  */.  Trigger *pT
365dd 72 69 67 67 65 72 2c 20 20 20 2f 2a 20 54 72 69  rigger,   /* Tri
365de 67 67 65 72 20 74 6f 20 63 6f 64 65 20 2a 2f 0a  gger to code */.
365df 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
365e0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
365e1 62 6c 65 20 70 54 72 69 67 67 65 72 20 69 73 20  ble pTrigger is 
365e2 61 74 74 61 63 68 65 64 20 74 6f 20 2a 2f 0a 20  attached to */. 
365e3 20 69 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20 20   int orconf     
365e4 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46        /* ON CONF
365e5 4c 49 43 54 20 70 6f 6c 69 63 79 20 74 6f 20 63  LICT policy to c
365e6 6f 64 65 20 74 72 69 67 67 65 72 20 70 72 6f 67  ode trigger prog
365e7 72 61 6d 20 77 69 74 68 20 2a 2f 0a 29 7b 0a 20  ram with */.){. 
365e8 20 50 61 72 73 65 20 2a 70 54 6f 70 20 3d 20 73   Parse *pTop = s
365e9 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65  qlite3ParseTople
365ea 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 73  vel(pParse);.  s
365eb 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
365ec 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 44 61  rse->db;   /* Da
365ed 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f  tabase handle */
365ee 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70  .  TriggerPrg *p
365ef 50 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Prg;           /
365f0 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
365f1 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  n */.  Expr *pWh
365f2 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  en = 0;         
365f3 20 20 20 2f 2a 20 44 75 70 6c 69 63 61 74 65 20     /* Duplicate 
365f4 6f 66 20 74 72 69 67 67 65 72 20 57 48 45 4e 20  of trigger WHEN 
365f5 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
365f6 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
365f7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
365f8 65 6d 70 6f 72 61 72 79 20 56 4d 20 2a 2f 0a 20  emporary VM */. 
365f9 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43   NameContext sNC
365fa 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
365fb 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72  Name context for
365fc 20 73 75 62 2d 76 64 62 65 20 2a 2f 0a 20 20 53   sub-vdbe */.  S
365fd 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67  ubProgram *pProg
365fe 72 61 6d 20 3d 20 30 3b 20 20 20 2f 2a 20 53 75  ram = 0;   /* Su
365ff 62 2d 76 64 62 65 20 66 6f 72 20 74 72 69 67 67  b-vdbe for trigg
36600 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20  er program */.  
36601 50 61 72 73 65 20 2a 70 53 75 62 50 61 72 73 65  Parse *pSubParse
36602 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ;           /* P
36603 61 72 73 65 20 63 6f 6e 74 65 78 74 20 66 6f 72  arse context for
36604 20 73 75 62 2d 76 64 62 65 20 2a 2f 0a 20 20 69   sub-vdbe */.  i
36605 6e 74 20 69 45 6e 64 54 72 69 67 67 65 72 20 3d  nt iEndTrigger =
36606 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61   0;        /* La
36607 62 65 6c 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69  bel to jump to i
36608 66 20 57 48 45 4e 20 69 73 20 66 61 6c 73 65 20  f WHEN is false 
36609 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54  */..  assert( pT
3660a 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 3d 3d 30  rigger->zName==0
3660b 20 7c 7c 20 70 54 61 62 3d 3d 74 61 62 6c 65 4f   || pTab==tableO
3660c 66 54 72 69 67 67 65 72 28 70 54 72 69 67 67 65  fTrigger(pTrigge
3660d 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r) );.  assert( 
3660e 70 54 6f 70 2d 3e 70 56 64 62 65 20 29 3b 0a 0a  pTop->pVdbe );..
3660f 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68    /* Allocate th
36610 65 20 54 72 69 67 67 65 72 50 72 67 20 61 6e 64  e TriggerPrg and
36611 20 53 75 62 50 72 6f 67 72 61 6d 20 6f 62 6a 65   SubProgram obje
36612 63 74 73 2e 20 54 6f 20 65 6e 73 75 72 65 20 74  cts. To ensure t
36613 68 61 74 20 74 68 65 79 0a 20 20 2a 2a 20 61 72  hat they.  ** ar
36614 65 20 66 72 65 65 64 20 69 66 20 61 6e 20 65 72  e freed if an er
36615 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 69 6e 6b  ror occurs, link
36616 20 74 68 65 6d 20 69 6e 74 6f 20 74 68 65 20 50   them into the P
36617 61 72 73 65 2e 70 54 72 69 67 67 65 72 50 72 67  arse.pTriggerPrg
36618 20 0a 20 20 2a 2a 20 6c 69 73 74 20 6f 66 20 74   .  ** list of t
36619 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 50 61 72  he top-level Par
3661a 73 65 20 6f 62 6a 65 63 74 20 73 6f 6f 6e 65 72  se object sooner
3661b 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c 61 74   rather than lat
3661c 65 72 2e 20 20 2a 2f 0a 20 20 70 50 72 67 20 3d  er.  */.  pPrg =
3661d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
3661e 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
3661f 54 72 69 67 67 65 72 50 72 67 29 29 3b 0a 20 20  TriggerPrg));.  
36620 69 66 28 20 21 70 50 72 67 20 29 20 72 65 74 75  if( !pPrg ) retu
36621 72 6e 20 30 3b 0a 20 20 70 50 72 67 2d 3e 70 4e  rn 0;.  pPrg->pN
36622 65 78 74 20 3d 20 70 54 6f 70 2d 3e 70 54 72 69  ext = pTop->pTri
36623 67 67 65 72 50 72 67 3b 0a 20 20 70 54 6f 70 2d  ggerPrg;.  pTop-
36624 3e 70 54 72 69 67 67 65 72 50 72 67 20 3d 20 70  >pTriggerPrg = p
36625 50 72 67 3b 0a 20 20 70 50 72 67 2d 3e 70 50 72  Prg;.  pPrg->pPr
36626 6f 67 72 61 6d 20 3d 20 70 50 72 6f 67 72 61 6d  ogram = pProgram
36627 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
36628 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
36629 66 28 53 75 62 50 72 6f 67 72 61 6d 29 29 3b 0a  f(SubProgram));.
3662a 20 20 69 66 28 20 21 70 50 72 6f 67 72 61 6d 20    if( !pProgram 
3662b 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 73 71  ) return 0;.  sq
3662c 6c 69 74 65 33 56 64 62 65 4c 69 6e 6b 53 75 62  lite3VdbeLinkSub
3662d 50 72 6f 67 72 61 6d 28 70 54 6f 70 2d 3e 70 56  Program(pTop->pV
3662e 64 62 65 2c 20 70 50 72 6f 67 72 61 6d 29 3b 0a  dbe, pProgram);.
3662f 20 20 70 50 72 67 2d 3e 70 54 72 69 67 67 65 72    pPrg->pTrigger
36630 20 3d 20 70 54 72 69 67 67 65 72 3b 0a 20 20 70   = pTrigger;.  p
36631 50 72 67 2d 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72  Prg->orconf = or
36632 63 6f 6e 66 3b 0a 20 20 70 50 72 67 2d 3e 61 43  conf;.  pPrg->aC
36633 6f 6c 6d 61 73 6b 5b 30 5d 20 3d 20 30 78 66 66  olmask[0] = 0xff
36634 66 66 66 66 66 66 3b 0a 20 20 70 50 72 67 2d 3e  ffffff;.  pPrg->
36635 61 43 6f 6c 6d 61 73 6b 5b 31 5d 20 3d 20 30 78  aColmask[1] = 0x
36636 66 66 66 66 66 66 66 66 3b 0a 0a 20 20 2f 2a 20  ffffffff;..  /* 
36637 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 70 6f 70  Allocate and pop
36638 75 6c 61 74 65 20 61 20 6e 65 77 20 50 61 72 73  ulate a new Pars
36639 65 20 63 6f 6e 74 65 78 74 20 74 6f 20 75 73 65  e context to use
3663a 20 66 6f 72 20 63 6f 64 69 6e 67 20 74 68 65 20   for coding the 
3663b 0a 20 20 2a 2a 20 74 72 69 67 67 65 72 20 73 75  .  ** trigger su
3663c 62 2d 70 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20  b-program.  */. 
3663d 20 70 53 75 62 50 61 72 73 65 20 3d 20 73 71 6c   pSubParse = sql
3663e 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65  ite3StackAllocZe
3663f 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 50 61  ro(db, sizeof(Pa
36640 72 73 65 29 29 3b 0a 20 20 69 66 28 20 21 70 53  rse));.  if( !pS
36641 75 62 50 61 72 73 65 20 29 20 72 65 74 75 72 6e  ubParse ) return
36642 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e   0;.  memset(&sN
36643 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
36644 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65  ));.  sNC.pParse
36645 20 3d 20 70 53 75 62 50 61 72 73 65 3b 0a 20 20   = pSubParse;.  
36646 70 53 75 62 50 61 72 73 65 2d 3e 64 62 20 3d 20  pSubParse->db = 
36647 64 62 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d  db;.  pSubParse-
36648 3e 70 54 72 69 67 67 65 72 54 61 62 20 3d 20 70  >pTriggerTab = p
36649 54 61 62 3b 0a 20 20 70 53 75 62 50 61 72 73 65  Tab;.  pSubParse
3664a 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3d 20 70 54  ->pToplevel = pT
3664b 6f 70 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d  op;.  pSubParse-
3664c 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20  >zAuthContext = 
3664d 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 3b  pTrigger->zName;
3664e 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e 65 54  .  pSubParse->eT
3664f 72 69 67 67 65 72 4f 70 20 3d 20 70 54 72 69 67  riggerOp = pTrig
36650 67 65 72 2d 3e 6f 70 3b 0a 20 20 70 53 75 62 50  ger->op;.  pSubP
36651 61 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f 6f 70  arse->nQueryLoop
36652 20 3d 20 70 50 61 72 73 65 2d 3e 6e 51 75 65 72   = pParse->nQuer
36653 79 4c 6f 6f 70 3b 0a 0a 20 20 76 20 3d 20 73 71  yLoop;..  v = sq
36654 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 53 75  lite3GetVdbe(pSu
36655 62 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  bParse);.  if( v
36656 20 29 7b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d   ){.    VdbeComm
36657 65 6e 74 28 28 76 2c 20 22 53 74 61 72 74 3a 20  ent((v, "Start: 
36658 25 73 2e 25 73 20 28 25 73 20 25 73 25 73 25 73  %s.%s (%s %s%s%s
36659 20 4f 4e 20 25 73 29 22 2c 20 0a 20 20 20 20 20   ON %s)", .     
3665a 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65   pTrigger->zName
3665b 2c 20 6f 6e 45 72 72 6f 72 54 65 78 74 28 6f 72  , onErrorText(or
3665c 63 6f 6e 66 29 2c 0a 20 20 20 20 20 20 28 70 54  conf),.      (pT
3665d 72 69 67 67 65 72 2d 3e 74 72 5f 74 6d 3d 3d 54  rigger->tr_tm==T
3665e 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 3f 20  RIGGER_BEFORE ? 
3665f 22 42 45 46 4f 52 45 22 20 3a 20 22 41 46 54 45  "BEFORE" : "AFTE
36660 52 22 29 2c 0a 20 20 20 20 20 20 20 20 28 70 54  R"),.        (pT
36661 72 69 67 67 65 72 2d 3e 6f 70 3d 3d 54 4b 5f 55  rigger->op==TK_U
36662 50 44 41 54 45 20 3f 20 22 55 50 44 41 54 45 22  PDATE ? "UPDATE"
36663 20 3a 20 22 22 29 2c 0a 20 20 20 20 20 20 20 20   : ""),.        
36664 28 70 54 72 69 67 67 65 72 2d 3e 6f 70 3d 3d 54  (pTrigger->op==T
36665 4b 5f 49 4e 53 45 52 54 20 3f 20 22 49 4e 53 45  K_INSERT ? "INSE
36666 52 54 22 20 3a 20 22 22 29 2c 0a 20 20 20 20 20  RT" : ""),.     
36667 20 20 20 28 70 54 72 69 67 67 65 72 2d 3e 6f 70     (pTrigger->op
36668 3d 3d 54 4b 5f 44 45 4c 45 54 45 20 3f 20 22 44  ==TK_DELETE ? "D
36669 45 4c 45 54 45 22 20 3a 20 22 22 29 2c 0a 20 20  ELETE" : ""),.  
3666a 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a      pTab->zName.
3666b 20 20 20 20 29 29 3b 0a 23 69 66 6e 64 65 66 20      ));.#ifndef 
3666c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
3666d 45 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  E.    sqlite3Vdb
3666e 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
3666f 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d   .      sqlite3M
36670 50 72 69 6e 74 66 28 64 62 2c 20 22 2d 2d 20 54  Printf(db, "-- T
36671 52 49 47 47 45 52 20 25 73 22 2c 20 70 54 72 69  RIGGER %s", pTri
36672 67 67 65 72 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34  gger->zName), P4
36673 5f 44 59 4e 41 4d 49 43 0a 20 20 20 20 29 3b 0a  _DYNAMIC.    );.
36674 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49  #endif..    /* I
36675 66 20 6f 6e 65 20 77 61 73 20 73 70 65 63 69 66  f one was specif
36676 69 65 64 2c 20 63 6f 64 65 20 74 68 65 20 57 48  ied, code the WH
36677 45 4e 20 63 6c 61 75 73 65 2e 20 49 66 20 69 74  EN clause. If it
36678 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 66 61   evaluates to fa
36679 6c 73 65 0a 20 20 20 20 2a 2a 20 28 6f 72 20 4e  lse.    ** (or N
3667a 55 4c 4c 29 20 74 68 65 20 73 75 62 2d 76 64 62  ULL) the sub-vdb
3667b 65 20 69 73 20 69 6d 6d 65 64 69 61 74 65 6c 79  e is immediately
3667c 20 68 61 6c 74 65 64 20 62 79 20 6a 75 6d 70 69   halted by jumpi
3667d 6e 67 20 74 6f 20 74 68 65 20 0a 20 20 20 20 2a  ng to the .    *
3667e 2a 20 4f 50 5f 48 61 6c 74 20 69 6e 73 65 72 74  * OP_Halt insert
3667f 65 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  ed at the end of
36680 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 2a   the program.  *
36681 2f 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 67  /.    if( pTrigg
36682 65 72 2d 3e 70 57 68 65 6e 20 29 7b 0a 20 20 20  er->pWhen ){.   
36683 20 20 20 70 57 68 65 6e 20 3d 20 73 71 6c 69 74     pWhen = sqlit
36684 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 54  e3ExprDup(db, pT
36685 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 2c 20 30  rigger->pWhen, 0
36686 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c  );.      if( SQL
36687 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 52  ITE_OK==sqlite3R
36688 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
36689 26 73 4e 43 2c 20 70 57 68 65 6e 29 20 0a 20 20  &sNC, pWhen) .  
3668a 20 20 20 20 20 26 26 20 64 62 2d 3e 6d 61 6c 6c       && db->mall
3668b 6f 63 46 61 69 6c 65 64 3d 3d 30 20 0a 20 20 20  ocFailed==0 .   
3668c 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 45     ){.        iE
3668d 6e 64 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69  ndTrigger = sqli
3668e 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
3668f 28 76 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  (v);.        sql
36690 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
36691 70 53 75 62 50 61 72 73 65 2c 20 70 57 68 65 6e  pSubParse, pWhen
36692 2c 20 69 45 6e 64 54 72 69 67 67 65 72 2c 20 53  , iEndTrigger, S
36693 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
36694 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
36695 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
36696 74 65 28 64 62 2c 20 70 57 68 65 6e 29 3b 0a 20  te(db, pWhen);. 
36697 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 64     }..    /* Cod
36698 65 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72  e the trigger pr
36699 6f 67 72 61 6d 20 69 6e 74 6f 20 74 68 65 20 73  ogram into the s
3669a 75 62 2d 76 64 62 65 2e 20 2a 2f 0a 20 20 20 20  ub-vdbe. */.    
3669b 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67 72  codeTriggerProgr
3669c 61 6d 28 70 53 75 62 50 61 72 73 65 2c 20 70 54  am(pSubParse, pT
3669d 72 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73  rigger->step_lis
3669e 74 2c 20 6f 72 63 6f 6e 66 29 3b 0a 0a 20 20 20  t, orconf);..   
3669f 20 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 4f 50   /* Insert an OP
366a0 5f 48 61 6c 74 20 61 74 20 74 68 65 20 65 6e 64  _Halt at the end
366a1 20 6f 66 20 74 68 65 20 73 75 62 2d 70 72 6f 67   of the sub-prog
366a2 72 61 6d 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ram. */.    if( 
366a3 69 45 6e 64 54 72 69 67 67 65 72 20 29 7b 0a 20  iEndTrigger ){. 
366a4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
366a5 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
366a6 69 45 6e 64 54 72 69 67 67 65 72 29 3b 0a 20 20  iEndTrigger);.  
366a7 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
366a8 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f  dbeAddOp0(v, OP_
366a9 48 61 6c 74 29 3b 0a 20 20 20 20 56 64 62 65 43  Halt);.    VdbeC
366aa 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 45 6e 64 3a  omment((v, "End:
366ab 20 25 73 2e 25 73 22 2c 20 70 54 72 69 67 67 65   %s.%s", pTrigge
366ac 72 2d 3e 7a 4e 61 6d 65 2c 20 6f 6e 45 72 72 6f  r->zName, onErro
366ad 72 54 65 78 74 28 6f 72 63 6f 6e 66 29 29 29 3b  rText(orconf)));
366ae 0a 0a 20 20 20 20 74 72 61 6e 73 66 65 72 50 61  ..    transferPa
366af 72 73 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c  rseError(pParse,
366b0 20 70 53 75 62 50 61 72 73 65 29 3b 0a 20 20 20   pSubParse);.   
366b1 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
366b2 61 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20  ailed==0 ){.    
366b3 20 20 70 50 72 6f 67 72 61 6d 2d 3e 61 4f 70 20    pProgram->aOp 
366b4 3d 20 73 71 6c 69 74 65 33 56 64 62 65 54 61 6b  = sqlite3VdbeTak
366b5 65 4f 70 41 72 72 61 79 28 76 2c 20 26 70 50 72  eOpArray(v, &pPr
366b6 6f 67 72 61 6d 2d 3e 6e 4f 70 2c 20 26 70 54 6f  ogram->nOp, &pTo
366b7 70 2d 3e 6e 4d 61 78 41 72 67 29 3b 0a 20 20 20  p->nMaxArg);.   
366b8 20 7d 0a 20 20 20 20 70 50 72 6f 67 72 61 6d 2d   }.    pProgram-
366b9 3e 6e 4d 65 6d 20 3d 20 70 53 75 62 50 61 72 73  >nMem = pSubPars
366ba 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 70 50 72  e->nMem;.    pPr
366bb 6f 67 72 61 6d 2d 3e 6e 43 73 72 20 3d 20 70 53  ogram->nCsr = pS
366bc 75 62 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20  ubParse->nTab;. 
366bd 20 20 20 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 6e     pProgram->nOn
366be 63 65 20 3d 20 70 53 75 62 50 61 72 73 65 2d 3e  ce = pSubParse->
366bf 6e 4f 6e 63 65 3b 0a 20 20 20 20 70 50 72 6f 67  nOnce;.    pProg
366c0 72 61 6d 2d 3e 74 6f 6b 65 6e 20 3d 20 28 76 6f  ram->token = (vo
366c1 69 64 20 2a 29 70 54 72 69 67 67 65 72 3b 0a 20  id *)pTrigger;. 
366c2 20 20 20 70 50 72 67 2d 3e 61 43 6f 6c 6d 61 73     pPrg->aColmas
366c3 6b 5b 30 5d 20 3d 20 70 53 75 62 50 61 72 73 65  k[0] = pSubParse
366c4 2d 3e 6f 6c 64 6d 61 73 6b 3b 0a 20 20 20 20 70  ->oldmask;.    p
366c5 50 72 67 2d 3e 61 43 6f 6c 6d 61 73 6b 5b 31 5d  Prg->aColmask[1]
366c6 20 3d 20 70 53 75 62 50 61 72 73 65 2d 3e 6e 65   = pSubParse->ne
366c7 77 6d 61 73 6b 3b 0a 20 20 20 20 73 71 6c 69 74  wmask;.    sqlit
366c8 65 33 56 64 62 65 44 65 6c 65 74 65 28 76 29 3b  e3VdbeDelete(v);
366c9 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
366ca 21 70 53 75 62 50 61 72 73 65 2d 3e 70 41 69 6e  !pSubParse->pAin
366cb 63 20 20 20 20 20 20 20 26 26 20 21 70 53 75 62  c       && !pSub
366cc 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61  Parse->pZombieTa
366cd 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  b );.  assert( !
366ce 70 53 75 62 50 61 72 73 65 2d 3e 70 54 72 69 67  pSubParse->pTrig
366cf 67 65 72 50 72 67 20 26 26 20 21 70 53 75 62 50  gerPrg && !pSubP
366d0 61 72 73 65 2d 3e 6e 4d 61 78 41 72 67 20 29 3b  arse->nMaxArg );
366d1 0a 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46  .  sqlite3StackF
366d2 72 65 65 28 64 62 2c 20 70 53 75 62 50 61 72 73  ree(db, pSubPars
366d3 65 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 50  e);..  return pP
366d4 72 67 3b 0a 7d 0a 20 20 20 20 0a 2f 2a 0a 2a 2a  rg;.}.    ./*.**
366d5 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
366d6 72 20 74 6f 20 61 20 54 72 69 67 67 65 72 50 72  r to a TriggerPr
366d7 67 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e  g object contain
366d8 69 6e 67 20 74 68 65 20 73 75 62 2d 70 72 6f 67  ing the sub-prog
366d9 72 61 6d 20 66 6f 72 0a 2a 2a 20 74 72 69 67 67  ram for.** trigg
366da 65 72 20 70 54 72 69 67 67 65 72 20 77 69 74 68  er pTrigger with
366db 20 64 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46   default ON CONF
366dc 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d 20 6f  LICT algorithm o
366dd 72 63 6f 6e 66 2e 20 49 66 20 6e 6f 20 73 75 63  rconf. If no suc
366de 68 0a 2a 2a 20 54 72 69 67 67 65 72 50 72 67 20  h.** TriggerPrg 
366df 6f 62 6a 65 63 74 20 65 78 69 73 74 73 2c 20 61  object exists, a
366e0 20 6e 65 77 20 6f 62 6a 65 63 74 20 69 73 20 61   new object is a
366e1 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70  llocated and pop
366e2 75 6c 61 74 65 64 20 62 65 66 6f 72 65 0a 2a 2a  ulated before.**
366e3 20 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 2e   being returned.
366e4 0a 2a 2f 0a 73 74 61 74 69 63 20 54 72 69 67 67  .*/.static Trigg
366e5 65 72 50 72 67 20 2a 67 65 74 52 6f 77 54 72 69  erPrg *getRowTri
366e6 67 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70  gger(.  Parse *p
366e7 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  Parse,       /* 
366e8 43 75 72 72 65 6e 74 20 70 61 72 73 65 20 63 6f  Current parse co
366e9 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67  ntext */.  Trigg
366ea 65 72 20 2a 70 54 72 69 67 67 65 72 2c 20 20 20  er *pTrigger,   
366eb 2f 2a 20 54 72 69 67 67 65 72 20 74 6f 20 63 6f  /* Trigger to co
366ec 64 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  de */.  Table *p
366ed 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Tab,         /* 
366ee 54 68 65 20 74 61 62 6c 65 20 74 72 69 67 67 65  The table trigge
366ef 72 20 70 54 72 69 67 67 65 72 20 69 73 20 61 74  r pTrigger is at
366f0 74 61 63 68 65 64 20 74 6f 20 2a 2f 0a 20 20 69  tached to */.  i
366f1 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20 20 20 20  nt orconf       
366f2 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49      /* ON CONFLI
366f3 43 54 20 61 6c 67 6f 72 69 74 68 6d 2e 20 2a 2f  CT algorithm. */
366f4 0a 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 52 6f  .){.  Parse *pRo
366f5 6f 74 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73  ot = sqlite3Pars
366f6 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65  eToplevel(pParse
366f7 29 3b 0a 20 20 54 72 69 67 67 65 72 50 72 67 20  );.  TriggerPrg 
366f8 2a 70 50 72 67 3b 0a 0a 20 20 61 73 73 65 72 74  *pPrg;..  assert
366f9 28 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d  ( pTrigger->zNam
366fa 65 3d 3d 30 20 7c 7c 20 70 54 61 62 3d 3d 74 61  e==0 || pTab==ta
366fb 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70 54 72  bleOfTrigger(pTr
366fc 69 67 67 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20  igger) );..  /* 
366fd 49 74 20 6d 61 79 20 62 65 20 74 68 61 74 20 74  It may be that t
366fe 68 69 73 20 74 72 69 67 67 65 72 20 68 61 73 20  his trigger has 
366ff 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 64  already been cod
36700 65 64 20 28 6f 72 20 69 73 20 69 6e 20 74 68 65  ed (or is in the
36701 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f 66  .  ** process of
36702 20 62 65 69 6e 67 20 63 6f 64 65 64 29 2e 20 49   being coded). I
36703 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61  f this is the ca
36704 73 65 2c 20 74 68 65 6e 20 61 6e 20 65 6e 74 72  se, then an entr
36705 79 20 77 69 74 68 0a 20 20 2a 2a 20 61 20 6d 61  y with.  ** a ma
36706 74 63 68 69 6e 67 20 54 72 69 67 67 65 72 50 72  tching TriggerPr
36707 67 2e 70 54 72 69 67 67 65 72 20 66 69 65 6c 64  g.pTrigger field
36708 20 77 69 6c 6c 20 62 65 20 70 72 65 73 65 6e 74   will be present
36709 20 73 6f 6d 65 77 68 65 72 65 0a 20 20 2a 2a 20   somewhere.  ** 
3670a 69 6e 20 74 68 65 20 50 61 72 73 65 2e 70 54 72  in the Parse.pTr
3670b 69 67 67 65 72 50 72 67 20 6c 69 73 74 2e 20 53  iggerPrg list. S
3670c 65 61 72 63 68 20 66 6f 72 20 73 75 63 68 20 61  earch for such a
3670d 6e 20 65 6e 74 72 79 2e 20 20 2a 2f 0a 20 20 66  n entry.  */.  f
3670e 6f 72 28 70 50 72 67 3d 70 52 6f 6f 74 2d 3e 70  or(pPrg=pRoot->p
3670f 54 72 69 67 67 65 72 50 72 67 3b 20 0a 20 20 20  TriggerPrg; .   
36710 20 20 20 70 50 72 67 20 26 26 20 28 70 50 72 67     pPrg && (pPrg
36711 2d 3e 70 54 72 69 67 67 65 72 21 3d 70 54 72 69  ->pTrigger!=pTri
36712 67 67 65 72 20 7c 7c 20 70 50 72 67 2d 3e 6f 72  gger || pPrg->or
36713 63 6f 6e 66 21 3d 6f 72 63 6f 6e 66 29 3b 20 0a  conf!=orconf); .
36714 20 20 20 20 20 20 70 50 72 67 3d 70 50 72 67 2d        pPrg=pPrg-
36715 3e 70 4e 65 78 74 0a 20 20 29 3b 0a 0a 20 20 2f  >pNext.  );..  /
36716 2a 20 49 66 20 61 6e 20 65 78 69 73 74 69 6e 67  * If an existing
36717 20 54 72 69 67 67 65 72 50 72 67 20 63 6f 75 6c   TriggerPrg coul
36718 64 20 6e 6f 74 20 62 65 20 6c 6f 63 61 74 65 64  d not be located
36719 2c 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 6f  , create a new o
3671a 6e 65 2e 20 2a 2f 0a 20 20 69 66 28 20 21 70 50  ne. */.  if( !pP
3671b 72 67 20 29 7b 0a 20 20 20 20 70 50 72 67 20 3d  rg ){.    pPrg =
3671c 20 63 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28   codeRowTrigger(
3671d 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
3671e 2c 20 70 54 61 62 2c 20 6f 72 63 6f 6e 66 29 3b  , pTab, orconf);
3671f 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
36720 50 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  Prg;.}../*.** Ge
36721 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
36722 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67  the trigger prog
36723 72 61 6d 20 61 73 73 6f 63 69 61 74 65 64 20 77  ram associated w
36724 69 74 68 20 74 72 69 67 67 65 72 20 70 20 6f 6e  ith trigger p on
36725 20 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 2e   .** table pTab.
36726 20 54 68 65 20 72 65 67 2c 20 6f 72 63 6f 6e 66   The reg, orconf
36727 20 61 6e 64 20 69 67 6e 6f 72 65 4a 75 6d 70 20   and ignoreJump 
36728 70 61 72 61 6d 65 74 65 72 73 20 70 61 73 73 65  parameters passe
36729 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e  d to this.** fun
3672a 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 73 61  ction are the sa
3672b 6d 65 20 61 73 20 74 68 6f 73 65 20 64 65 73 63  me as those desc
3672c 72 69 62 65 64 20 69 6e 20 74 68 65 20 68 65 61  ribed in the hea
3672d 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  der function for
3672e 0a 2a 2a 20 73 71 6c 69 74 65 33 43 6f 64 65 52  .** sqlite3CodeR
3672f 6f 77 54 72 69 67 67 65 72 28 29 0a 2a 2f 0a 53  owTrigger().*/.S
36730 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
36731 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f  id sqlite3CodeRo
36732 77 54 72 69 67 67 65 72 44 69 72 65 63 74 28 0a  wTriggerDirect(.
36733 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
36734 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20         /* Parse 
36735 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69  context */.  Tri
36736 67 67 65 72 20 2a 70 2c 20 20 20 20 20 20 20 20  gger *p,        
36737 20 20 2f 2a 20 54 72 69 67 67 65 72 20 74 6f 20    /* Trigger to 
36738 63 6f 64 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20  code */.  Table 
36739 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f  *pTab,         /
3673a 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 63  * The table to c
3673b 6f 64 65 20 74 72 69 67 67 65 72 73 20 66 72 6f  ode triggers fro
3673c 6d 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 2c 20  m */.  int reg, 
3673d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
3673e 65 67 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e  eg array contain
3673f 69 6e 67 20 4f 4c 44 2e 2a 20 61 6e 64 20 4e 45  ing OLD.* and NE
36740 57 2e 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20  W.* values */.  
36741 69 6e 74 20 6f 72 63 6f 6e 66 2c 20 20 20 20 20  int orconf,     
36742 20 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c       /* ON CONFL
36743 49 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20  ICT policy */.  
36744 69 6e 74 20 69 67 6e 6f 72 65 4a 75 6d 70 20 20  int ignoreJump  
36745 20 20 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74       /* Instruct
36746 69 6f 6e 20 74 6f 20 6a 75 6d 70 20 74 6f 20 66  ion to jump to f
36747 6f 72 20 52 41 49 53 45 28 49 47 4e 4f 52 45 29  or RAISE(IGNORE)
36748 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76   */.){.  Vdbe *v
36749 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
3674a 65 28 70 50 61 72 73 65 29 3b 20 2f 2a 20 4d 61  e(pParse); /* Ma
3674b 69 6e 20 56 4d 20 2a 2f 0a 20 20 54 72 69 67 67  in VM */.  Trigg
3674c 65 72 50 72 67 20 2a 70 50 72 67 3b 0a 20 20 70  erPrg *pPrg;.  p
3674d 50 72 67 20 3d 20 67 65 74 52 6f 77 54 72 69 67  Prg = getRowTrig
3674e 67 65 72 28 70 50 61 72 73 65 2c 20 70 2c 20 70  ger(pParse, p, p
3674f 54 61 62 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20 20  Tab, orconf);.  
36750 61 73 73 65 72 74 28 20 70 50 72 67 20 7c 7c 20  assert( pPrg || 
36751 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
36752 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
36753 6f 63 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 2f  ocFailed );..  /
36754 2a 20 43 6f 64 65 20 74 68 65 20 4f 50 5f 50 72  * Code the OP_Pr
36755 6f 67 72 61 6d 20 6f 70 63 6f 64 65 20 69 6e 20  ogram opcode in 
36756 74 68 65 20 70 61 72 65 6e 74 20 56 44 42 45 2e  the parent VDBE.
36757 20 50 34 20 6f 66 20 74 68 65 20 4f 50 5f 50 72   P4 of the OP_Pr
36758 6f 67 72 61 6d 20 0a 20 20 2a 2a 20 69 73 20 61  ogram .  ** is a
36759 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
3675a 73 75 62 2d 76 64 62 65 20 63 6f 6e 74 61 69 6e  sub-vdbe contain
3675b 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20  ing the trigger 
3675c 70 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 69  program.  */.  i
3675d 66 28 20 70 50 72 67 20 29 7b 0a 20 20 20 20 69  f( pPrg ){.    i
3675e 6e 74 20 62 52 65 63 75 72 73 69 76 65 20 3d 20  nt bRecursive = 
3675f 28 70 2d 3e 7a 4e 61 6d 65 20 26 26 20 30 3d 3d  (p->zName && 0==
36760 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61  (pParse->db->fla
36761 67 73 26 53 51 4c 49 54 45 5f 52 65 63 54 72 69  gs&SQLITE_RecTri
36762 67 67 65 72 73 29 29 3b 0a 0a 20 20 20 20 73 71  ggers));..    sq
36763 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
36764 76 2c 20 4f 50 5f 50 72 6f 67 72 61 6d 2c 20 72  v, OP_Program, r
36765 65 67 2c 20 69 67 6e 6f 72 65 4a 75 6d 70 2c 20  eg, ignoreJump, 
36766 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29 3b  ++pParse->nMem);
36767 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
36768 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20  ChangeP4(v, -1, 
36769 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 50  (const char *)pP
3676a 72 67 2d 3e 70 50 72 6f 67 72 61 6d 2c 20 50 34  rg->pProgram, P4
3676b 5f 53 55 42 50 52 4f 47 52 41 4d 29 3b 0a 20 20  _SUBPROGRAM);.  
3676c 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 0a 20    VdbeComment(. 
3676d 20 20 20 20 20 20 20 28 76 2c 20 22 43 61 6c 6c         (v, "Call
3676e 3a 20 25 73 2e 25 73 22 2c 20 28 70 2d 3e 7a 4e  : %s.%s", (p->zN
3676f 61 6d 65 3f 70 2d 3e 7a 4e 61 6d 65 3a 22 66 6b  ame?p->zName:"fk
36770 65 79 22 29 2c 20 6f 6e 45 72 72 6f 72 54 65 78  ey"), onErrorTex
36771 74 28 6f 72 63 6f 6e 66 29 29 29 3b 0a 0a 20 20  t(orconf)));..  
36772 20 20 2f 2a 20 53 65 74 20 74 68 65 20 50 35 20    /* Set the P5 
36773 6f 70 65 72 61 6e 64 20 6f 66 20 74 68 65 20 4f  operand of the O
36774 50 5f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 75  P_Program instru
36775 63 74 69 6f 6e 20 74 6f 20 6e 6f 6e 2d 7a 65 72  ction to non-zer
36776 6f 20 69 66 0a 20 20 20 20 2a 2a 20 72 65 63 75  o if.    ** recu
36777 72 73 69 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e  rsive invocation
36778 20 6f 66 20 74 68 69 73 20 74 72 69 67 67 65 72   of this trigger
36779 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73 61   program is disa
3677a 6c 6c 6f 77 65 64 2e 20 52 65 63 75 72 73 69 76  llowed. Recursiv
3677b 65 0a 20 20 20 20 2a 2a 20 69 6e 76 6f 63 61 74  e.    ** invocat
3677c 69 6f 6e 20 69 73 20 64 69 73 61 6c 6c 6f 77 65  ion is disallowe
3677d 64 20 69 66 20 28 61 29 20 74 68 65 20 73 75 62  d if (a) the sub
3677e 2d 70 72 6f 67 72 61 6d 20 69 73 20 72 65 61 6c  -program is real
3677f 6c 79 20 61 20 74 72 69 67 67 65 72 2c 0a 20 20  ly a trigger,.  
36780 20 20 2a 2a 20 6e 6f 74 20 61 20 66 6f 72 65 69    ** not a forei
36781 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 2c 20 61  gn key action, a
36782 6e 64 20 28 62 29 20 74 68 65 20 66 6c 61 67 20  nd (b) the flag 
36783 74 6f 20 65 6e 61 62 6c 65 20 72 65 63 75 72 73  to enable recurs
36784 69 76 65 20 74 72 69 67 67 65 72 73 0a 20 20 20  ive triggers.   
36785 20 2a 2a 20 69 73 20 63 6c 65 61 72 2e 20 20 2a   ** is clear.  *
36786 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
36787 65 43 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38  eChangeP5(v, (u8
36788 29 62 52 65 63 75 72 73 69 76 65 29 3b 0a 20 20  )bRecursive);.  
36789 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
3678a 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 64  is called to cod
3678b 65 20 74 68 65 20 72 65 71 75 69 72 65 64 20 46  e the required F
3678c 4f 52 20 45 41 43 48 20 52 4f 57 20 74 72 69 67  OR EACH ROW trig
3678d 67 65 72 73 20 66 6f 72 20 61 6e 20 6f 70 65 72  gers for an oper
3678e 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c  ation.** on tabl
3678f 65 20 70 54 61 62 2e 20 54 68 65 20 6f 70 65 72  e pTab. The oper
36790 61 74 69 6f 6e 20 74 6f 20 63 6f 64 65 20 74 72  ation to code tr
36791 69 67 67 65 72 73 20 66 6f 72 20 28 49 4e 53 45  iggers for (INSE
36792 52 54 2c 20 55 50 44 41 54 45 20 6f 72 20 44 45  RT, UPDATE or DE
36793 4c 45 54 45 29 0a 2a 2a 20 69 73 20 67 69 76 65  LETE).** is give
36794 6e 20 62 79 20 74 68 65 20 6f 70 20 70 61 72 61  n by the op para
36795 6d 61 74 65 72 2e 20 54 68 65 20 74 72 5f 74 6d  mater. The tr_tm
36796 20 70 61 72 61 6d 65 74 65 72 20 64 65 74 65 72   parameter deter
36797 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 74 68  mines whether th
36798 65 0a 2a 2a 20 42 45 46 4f 52 45 20 6f 72 20 41  e.** BEFORE or A
36799 46 54 45 52 20 74 72 69 67 67 65 72 73 20 61 72  FTER triggers ar
3679a 65 20 63 6f 64 65 64 2e 20 49 66 20 74 68 65 20  e coded. If the 
3679b 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e 20  operation is an 
3679c 55 50 44 41 54 45 2c 20 74 68 65 6e 0a 2a 2a 20  UPDATE, then.** 
3679d 70 61 72 61 6d 65 74 65 72 20 70 43 68 61 6e 67  parameter pChang
3679e 65 73 20 69 73 20 70 61 73 73 65 64 20 74 68 65  es is passed the
3679f 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   list of columns
367a0 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e   being modified.
367a1 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
367a2 61 72 65 20 6e 6f 20 74 72 69 67 67 65 72 73 20  are no triggers 
367a3 74 68 61 74 20 66 69 72 65 20 61 74 20 74 68 65  that fire at the
367a4 20 73 70 65 63 69 66 69 65 64 20 74 69 6d 65 20   specified time 
367a5 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65  for the specifie
367a6 64 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 6f  d.** operation o
367a7 6e 20 70 54 61 62 2c 20 74 68 69 73 20 66 75 6e  n pTab, this fun
367a8 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
367a9 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 20  ..**.** The reg 
367aa 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
367ab 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 66  address of the f
367ac 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
367ad 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 0a 2a   of registers .*
367ae 2a 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 74  * that contain t
367af 68 65 20 76 61 6c 75 65 73 20 73 75 62 73 74 69  he values substi
367b0 74 75 74 65 64 20 66 6f 72 20 74 68 65 20 6e 65  tuted for the ne
367b1 77 2e 2a 20 61 6e 64 20 6f 6c 64 2e 2a 20 72 65  w.* and old.* re
367b2 66 65 72 65 6e 63 65 73 0a 2a 2a 20 69 6e 20 74  ferences.** in t
367b3 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  he trigger progr
367b4 61 6d 2e 20 49 66 20 4e 20 69 73 20 74 68 65 20  am. If N is the 
367b5 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
367b6 73 20 69 6e 20 74 61 62 6c 65 20 70 54 61 62 0a  s in table pTab.
367b7 2a 2a 20 28 61 20 63 6f 70 79 20 6f 66 20 70 54  ** (a copy of pT
367b8 61 62 2d 3e 6e 43 6f 6c 29 2c 20 74 68 65 6e 20  ab->nCol), then 
367b9 72 65 67 69 73 74 65 72 73 20 61 72 65 20 70 6f  registers are po
367ba 70 75 6c 61 74 65 64 20 61 73 20 66 6f 6c 6c 6f  pulated as follo
367bb 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 52 65 67 69  ws:.**.**   Regi
367bc 73 74 65 72 20 20 20 20 20 20 20 43 6f 6e 74 61  ster       Conta
367bd 69 6e 73 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  ins.**   -------
367be 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
367bf 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
367c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
367c1 2a 2a 20 20 20 72 65 67 2b 30 20 20 20 20 20 20  **   reg+0      
367c2 20 20 20 20 4f 4c 44 2e 72 6f 77 69 64 0a 2a 2a      OLD.rowid.**
367c3 20 20 20 72 65 67 2b 31 20 20 20 20 20 20 20 20     reg+1        
367c4 20 20 4f 4c 44 2e 2a 20 76 61 6c 75 65 20 6f 66    OLD.* value of
367c5 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d   left-most colum
367c6 6e 20 6f 66 20 70 54 61 62 0a 2a 2a 20 20 20 2e  n of pTab.**   .
367c7 2e 2e 20 20 20 20 20 20 20 20 20 20 20 20 2e 2e  ..            ..
367c8 2e 0a 2a 2a 20 20 20 72 65 67 2b 4e 20 20 20 20  ..**   reg+N    
367c9 20 20 20 20 20 20 4f 4c 44 2e 2a 20 76 61 6c 75        OLD.* valu
367ca 65 20 6f 66 20 72 69 67 68 74 2d 6d 6f 73 74 20  e of right-most 
367cb 63 6f 6c 75 6d 6e 20 6f 66 20 70 54 61 62 0a 2a  column of pTab.*
367cc 2a 20 20 20 72 65 67 2b 4e 2b 31 20 20 20 20 20  *   reg+N+1     
367cd 20 20 20 4e 45 57 2e 72 6f 77 69 64 0a 2a 2a 20     NEW.rowid.** 
367ce 20 20 72 65 67 2b 4e 2b 32 20 20 20 20 20 20 20    reg+N+2       
367cf 20 4f 4c 44 2e 2a 20 76 61 6c 75 65 20 6f 66 20   OLD.* value of 
367d0 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e  left-most column
367d1 20 6f 66 20 70 54 61 62 0a 2a 2a 20 20 20 2e 2e   of pTab.**   ..
367d2 2e 20 20 20 20 20 20 20 20 20 20 20 20 2e 2e 2e  .            ...
367d3 0a 2a 2a 20 20 20 72 65 67 2b 4e 2b 4e 2b 31 20  .**   reg+N+N+1 
367d4 20 20 20 20 20 4e 45 57 2e 2a 20 76 61 6c 75 65       NEW.* value
367d5 20 6f 66 20 72 69 67 68 74 2d 6d 6f 73 74 20 63   of right-most c
367d6 6f 6c 75 6d 6e 20 6f 66 20 70 54 61 62 0a 2a 2a  olumn of pTab.**
367d7 0a 2a 2a 20 46 6f 72 20 4f 4e 20 44 45 4c 45 54  .** For ON DELET
367d8 45 20 74 72 69 67 67 65 72 73 2c 20 74 68 65 20  E triggers, the 
367d9 72 65 67 69 73 74 65 72 73 20 63 6f 6e 74 61 69  registers contai
367da 6e 69 6e 67 20 74 68 65 20 4e 45 57 2e 2a 20 76  ning the NEW.* v
367db 61 6c 75 65 73 20 77 69 6c 6c 0a 2a 2a 20 6e 65  alues will.** ne
367dc 76 65 72 20 62 65 20 61 63 63 65 73 73 65 64 20  ver be accessed 
367dd 62 79 20 74 68 65 20 74 72 69 67 67 65 72 20 70  by the trigger p
367de 72 6f 67 72 61 6d 2c 20 73 6f 20 74 68 65 79 20  rogram, so they 
367df 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65  are not allocate
367e0 64 20 6f 72 20 0a 2a 2a 20 70 6f 70 75 6c 61 74  d or .** populat
367e1 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ed by the caller
367e2 20 28 74 68 65 72 65 20 69 73 20 6e 6f 20 64 61   (there is no da
367e3 74 61 20 74 6f 20 70 6f 70 75 6c 61 74 65 20 74  ta to populate t
367e4 68 65 6d 20 77 69 74 68 20 61 6e 79 77 61 79 29  hem with anyway)
367e5 2e 20 0a 2a 2a 20 53 69 6d 69 6c 61 72 6c 79 2c  . .** Similarly,
367e6 20 66 6f 72 20 4f 4e 20 49 4e 53 45 52 54 20 74   for ON INSERT t
367e7 72 69 67 67 65 72 73 20 74 68 65 20 76 61 6c 75  riggers the valu
367e8 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  es stored in the
367e9 20 4f 4c 44 2e 2a 20 72 65 67 69 73 74 65 72 73   OLD.* registers
367ea 0a 2a 2a 20 61 72 65 20 6e 65 76 65 72 20 61 63  .** are never ac
367eb 63 65 73 73 65 64 2c 20 61 6e 64 20 73 6f 20 61  cessed, and so a
367ec 72 65 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64  re not allocated
367ed 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   by the caller. 
367ee 53 6f 2c 20 66 6f 72 20 61 6e 0a 2a 2a 20 4f 4e  So, for an.** ON
367ef 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72 2c   INSERT trigger,
367f0 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65   the value passe
367f1 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  d to this functi
367f2 6f 6e 20 61 73 20 70 61 72 61 6d 65 74 65 72 20  on as parameter 
367f3 72 65 67 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 20  reg.** is not a 
367f4 72 65 61 64 61 62 6c 65 20 72 65 67 69 73 74 65  readable registe
367f5 72 2c 20 61 6c 74 68 6f 75 67 68 20 72 65 67 69  r, although regi
367f6 73 74 65 72 73 20 28 72 65 67 2b 4e 29 20 74 68  sters (reg+N) th
367f7 72 6f 75 67 68 20 0a 2a 2a 20 28 72 65 67 2b 4e  rough .** (reg+N
367f8 2b 4e 2b 31 29 20 61 72 65 2e 0a 2a 2a 0a 2a 2a  +N+1) are..**.**
367f9 20 50 61 72 61 6d 65 74 65 72 20 6f 72 63 6f 6e   Parameter orcon
367fa 66 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74  f is the default
367fb 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75   conflict resolu
367fc 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 20 66  tion algorithm f
367fd 6f 72 20 74 68 65 0a 2a 2a 20 74 72 69 67 67 65  or the.** trigge
367fe 72 20 70 72 6f 67 72 61 6d 20 74 6f 20 75 73 65  r program to use
367ff 20 28 52 45 50 4c 41 43 45 2c 20 49 47 4e 4f 52   (REPLACE, IGNOR
36800 45 20 65 74 63 2e 29 2e 20 50 61 72 61 6d 65 74  E etc.). Paramet
36801 65 72 20 69 67 6e 6f 72 65 4a 75 6d 70 0a 2a 2a  er ignoreJump.**
36802 20 69 73 20 74 68 65 20 69 6e 73 74 72 75 63 74   is the instruct
36803 69 6f 6e 20 74 68 61 74 20 63 6f 6e 74 72 6f 6c  ion that control
36804 20 73 68 6f 75 6c 64 20 6a 75 6d 70 20 74 6f 20   should jump to 
36805 69 66 20 61 20 74 72 69 67 67 65 72 20 70 72 6f  if a trigger pro
36806 67 72 61 6d 0a 2a 2a 20 72 61 69 73 65 73 20 61  gram.** raises a
36807 6e 20 49 47 4e 4f 52 45 20 65 78 63 65 70 74 69  n IGNORE excepti
36808 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
36809 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
3680a 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
3680b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
3680c 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  e,       /* Pars
3680d 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  e context */.  T
3680e 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
3680f 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74  ,   /* List of t
36810 72 69 67 67 65 72 73 20 6f 6e 20 74 61 62 6c 65  riggers on table
36811 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 6f   pTab */.  int o
36812 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
36813 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 55 50 44  /* One of TK_UPD
36814 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20  ATE, TK_INSERT, 
36815 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20 45  TK_DELETE */.  E
36816 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65  xprList *pChange
36817 73 2c 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 6c  s,  /* Changes l
36818 69 73 74 20 66 6f 72 20 61 6e 79 20 55 50 44 41  ist for any UPDA
36819 54 45 20 4f 46 20 74 72 69 67 67 65 72 73 20 2a  TE OF triggers *
3681a 2f 0a 20 20 69 6e 74 20 74 72 5f 74 6d 2c 20 20  /.  int tr_tm,  
3681b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
3681c 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52  of TRIGGER_BEFOR
3681d 45 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45 52  E, TRIGGER_AFTER
3681e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
3681f 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  b,         /* Th
36820 65 20 74 61 62 6c 65 20 74 6f 20 63 6f 64 65 20  e table to code 
36821 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20 2a 2f  triggers from */
36822 0a 20 20 69 6e 74 20 72 65 67 2c 20 20 20 20 20  .  int reg,     
36823 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
36824 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
36825 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 28 73   of registers (s
36826 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 69  ee above) */.  i
36827 6e 74 20 6f 72 63 6f 6e 66 2c 20 20 20 20 20 20  nt orconf,      
36828 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49      /* ON CONFLI
36829 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 69  CT policy */.  i
3682a 6e 74 20 69 67 6e 6f 72 65 4a 75 6d 70 20 20 20  nt ignoreJump   
3682b 20 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69      /* Instructi
3682c 6f 6e 20 74 6f 20 6a 75 6d 70 20 74 6f 20 66 6f  on to jump to fo
3682d 72 20 52 41 49 53 45 28 49 47 4e 4f 52 45 29 20  r RAISE(IGNORE) 
3682e 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 20  */.){.  Trigger 
3682f 2a 70 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  *p;          /* 
36830 55 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 20  Used to iterate 
36831 74 68 72 6f 75 67 68 20 70 54 72 69 67 67 65 72  through pTrigger
36832 20 6c 69 73 74 20 2a 2f 0a 0a 20 20 61 73 73 65   list */..  asse
36833 72 74 28 20 6f 70 3d 3d 54 4b 5f 55 50 44 41 54  rt( op==TK_UPDAT
36834 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45  E || op==TK_INSE
36835 52 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 44 45 4c  RT || op==TK_DEL
36836 45 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ETE );.  assert(
36837 20 74 72 5f 74 6d 3d 3d 54 52 49 47 47 45 52 5f   tr_tm==TRIGGER_
36838 42 45 46 4f 52 45 20 7c 7c 20 74 72 5f 74 6d 3d  BEFORE || tr_tm=
36839 3d 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 29  =TRIGGER_AFTER )
3683a 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6f 70 3d  ;.  assert( (op=
3683b 3d 54 4b 5f 55 50 44 41 54 45 29 3d 3d 28 70 43  =TK_UPDATE)==(pC
3683c 68 61 6e 67 65 73 21 3d 30 29 20 29 3b 0a 0a 20  hanges!=0) );.. 
3683d 20 66 6f 72 28 70 3d 70 54 72 69 67 67 65 72 3b   for(p=pTrigger;
3683e 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
3683f 0a 0a 20 20 20 20 2f 2a 20 53 61 6e 69 74 79 20  ..    /* Sanity 
36840 63 68 65 63 6b 69 6e 67 3a 20 20 54 68 65 20 73  checking:  The s
36841 63 68 65 6d 61 20 66 6f 72 20 74 68 65 20 74 72  chema for the tr
36842 69 67 67 65 72 20 61 6e 64 20 66 6f 72 20 74 68  igger and for th
36843 65 20 74 61 62 6c 65 20 61 72 65 0a 20 20 20 20  e table are.    
36844 2a 2a 20 61 6c 77 61 79 73 20 64 65 66 69 6e 65  ** always define
36845 64 2e 20 20 54 68 65 20 74 72 69 67 67 65 72 20  d.  The trigger 
36846 6d 75 73 74 20 62 65 20 69 6e 20 74 68 65 20 73  must be in the s
36847 61 6d 65 20 73 63 68 65 6d 61 20 61 73 20 74 68  ame schema as th
36848 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 6f  e table.    ** o
36849 72 20 65 6c 73 65 20 69 74 20 6d 75 73 74 20 62  r else it must b
3684a 65 20 61 20 54 45 4d 50 20 74 72 69 67 67 65 72  e a TEMP trigger
3684b 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  . */.    assert(
3684c 20 70 2d 3e 70 53 63 68 65 6d 61 21 3d 30 20 29   p->pSchema!=0 )
3684d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ;.    assert( p-
3684e 3e 70 54 61 62 53 63 68 65 6d 61 21 3d 30 20 29  >pTabSchema!=0 )
3684f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ;.    assert( p-
36850 3e 70 53 63 68 65 6d 61 3d 3d 70 2d 3e 70 54 61  >pSchema==p->pTa
36851 62 53 63 68 65 6d 61 20 0a 20 20 20 20 20 20 20  bSchema .       
36852 20 20 7c 7c 20 70 2d 3e 70 53 63 68 65 6d 61 3d    || p->pSchema=
36853 3d 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62  =pParse->db->aDb
36854 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a 0a  [1].pSchema );..
36855 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65      /* Determine
36856 20 77 68 65 74 68 65 72 20 77 65 20 73 68 6f 75   whether we shou
36857 6c 64 20 63 6f 64 65 20 74 68 69 73 20 74 72 69  ld code this tri
36858 67 67 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20  gger */.    if( 
36859 70 2d 3e 6f 70 3d 3d 6f 70 20 0a 20 20 20 20 20  p->op==op .     
3685a 26 26 20 70 2d 3e 74 72 5f 74 6d 3d 3d 74 72 5f  && p->tr_tm==tr_
3685b 74 6d 20 0a 20 20 20 20 20 26 26 20 63 68 65 63  tm .     && chec
3685c 6b 43 6f 6c 75 6d 6e 4f 76 65 72 6c 61 70 28 70  kColumnOverlap(p
3685d 2d 3e 70 43 6f 6c 75 6d 6e 73 2c 20 70 43 68 61  ->pColumns, pCha
3685e 6e 67 65 73 29 0a 20 20 20 20 29 7b 0a 20 20 20  nges).    ){.   
3685f 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f     sqlite3CodeRo
36860 77 54 72 69 67 67 65 72 44 69 72 65 63 74 28 70  wTriggerDirect(p
36861 50 61 72 73 65 2c 20 70 2c 20 70 54 61 62 2c 20  Parse, p, pTab, 
36862 72 65 67 2c 20 6f 72 63 6f 6e 66 2c 20 69 67 6e  reg, orconf, ign
36863 6f 72 65 4a 75 6d 70 29 3b 0a 20 20 20 20 7d 0a  oreJump);.    }.
36864 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 69    }.}../*.** Tri
36865 67 67 65 72 73 20 6d 61 79 20 61 63 63 65 73 73  ggers may access
36866 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69   values stored i
36867 6e 20 74 68 65 20 6f 6c 64 2e 2a 20 6f 72 20 6e  n the old.* or n
36868 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c  ew.* pseudo-tabl
36869 65 2e 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  e. .** This func
3686a 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 33  tion returns a 3
3686b 32 2d 62 69 74 20 62 69 74 6d 61 73 6b 20 69 6e  2-bit bitmask in
3686c 64 69 63 61 74 69 6e 67 20 77 68 69 63 68 20 63  dicating which c
3686d 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 0a 2a  olumns of the .*
3686e 2a 20 6f 6c 64 2e 2a 20 6f 72 20 6e 65 77 2e 2a  * old.* or new.*
3686f 20 74 61 62 6c 65 73 20 61 63 74 75 61 6c 6c 79   tables actually
36870 20 61 72 65 20 75 73 65 64 20 62 79 20 74 72 69   are used by tri
36871 67 67 65 72 73 2e 20 54 68 69 73 20 69 6e 66 6f  ggers. This info
36872 72 6d 61 74 69 6f 6e 20 0a 2a 2a 20 6d 61 79 20  rmation .** may 
36873 62 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63  be used by the c
36874 61 6c 6c 65 72 2c 20 66 6f 72 20 65 78 61 6d 70  aller, for examp
36875 6c 65 2c 20 74 6f 20 61 76 6f 69 64 20 68 61 76  le, to avoid hav
36876 69 6e 67 20 74 6f 20 6c 6f 61 64 20 74 68 65 20  ing to load the 
36877 65 6e 74 69 72 65 0a 2a 2a 20 6f 6c 64 2e 2a 20  entire.** old.* 
36878 72 65 63 6f 72 64 20 69 6e 74 6f 20 6d 65 6d 6f  record into memo
36879 72 79 20 77 68 65 6e 20 65 78 65 63 75 74 69 6e  ry when executin
3687a 67 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44  g an UPDATE or D
3687b 45 4c 45 54 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  ELETE command..*
3687c 2a 0a 2a 2a 20 42 69 74 20 30 20 6f 66 20 74 68  *.** Bit 0 of th
3687d 65 20 72 65 74 75 72 6e 65 64 20 6d 61 73 6b 20  e returned mask 
3687e 69 73 20 73 65 74 20 69 66 20 74 68 65 20 6c 65  is set if the le
3687f 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f  ft-most column o
36880 66 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 6d  f the.** table m
36881 61 79 20 62 65 20 61 63 63 65 73 73 65 64 20 75  ay be accessed u
36882 73 69 6e 67 20 61 6e 20 5b 6f 6c 64 7c 6e 65 77  sing an [old|new
36883 5d 2e 3c 63 6f 6c 3e 20 72 65 66 65 72 65 6e 63  ].<col> referenc
36884 65 2e 20 42 69 74 20 31 20 69 73 20 73 65 74 20  e. Bit 1 is set 
36885 69 66 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64  if.** the second
36886 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e   leftmost column
36887 20 76 61 6c 75 65 20 69 73 20 72 65 71 75 69 72   value is requir
36888 65 64 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 20 49  ed, and so on. I
36889 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6d  f there.** are m
3688a 6f 72 65 20 74 68 61 6e 20 33 32 20 63 6f 6c 75  ore than 32 colu
3688b 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  mns in the table
3688c 2c 20 61 6e 64 20 61 74 20 6c 65 61 73 74 20 6f  , and at least o
3688d 6e 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ne of the column
3688e 73 0a 2a 2a 20 77 69 74 68 20 61 6e 20 69 6e 64  s.** with an ind
3688f 65 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  ex greater than 
36890 33 32 20 6d 61 79 20 62 65 20 61 63 63 65 73 73  32 may be access
36891 65 64 2c 20 30 78 66 66 66 66 66 66 66 66 20 69  ed, 0xffffffff i
36892 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
36893 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  * It is not poss
36894 69 62 6c 65 20 74 6f 20 64 65 74 65 72 6d 69 6e  ible to determin
36895 65 20 69 66 20 74 68 65 20 6f 6c 64 2e 72 6f 77  e if the old.row
36896 69 64 20 6f 72 20 6e 65 77 2e 72 6f 77 69 64 20  id or new.rowid 
36897 63 6f 6c 75 6d 6e 20 69 73 20 0a 2a 2a 20 61 63  column is .** ac
36898 63 65 73 73 65 64 20 62 79 20 74 72 69 67 67 65  cessed by trigge
36899 72 73 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  rs. The caller m
3689a 75 73 74 20 61 6c 77 61 79 73 20 61 73 73 75 6d  ust always assum
3689b 65 20 74 68 61 74 20 69 74 20 69 73 2e 0a 2a 2a  e that it is..**
3689c 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 69 73  .** Parameter is
3689d 4e 65 77 20 6d 75 73 74 20 62 65 20 65 69 74 68  New must be eith
3689e 65 72 20 31 20 6f 72 20 30 2e 20 49 66 20 69 74  er 1 or 0. If it
3689f 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20   is 0, then the 
368a0 6d 61 73 6b 20 72 65 74 75 72 6e 65 64 0a 2a 2a  mask returned.**
368a1 20 61 70 70 6c 69 65 73 20 74 6f 20 74 68 65 20   applies to the 
368a2 6f 6c 64 2e 2a 20 74 61 62 6c 65 2e 20 49 66 20  old.* table. If 
368a3 31 2c 20 74 68 65 20 6e 65 77 2e 2a 20 74 61 62  1, the new.* tab
368a4 6c 65 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65  le..**.** Parame
368a5 74 65 72 20 74 72 5f 74 6d 20 6d 75 73 74 20 62  ter tr_tm must b
368a6 65 20 61 20 6d 61 73 6b 20 77 69 74 68 20 6f 6e  e a mask with on
368a7 65 20 6f 72 20 62 6f 74 68 20 6f 66 20 74 68 65  e or both of the
368a8 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 0a   TRIGGER_BEFORE.
368a9 2a 2a 20 61 6e 64 20 54 52 49 47 47 45 52 5f 41  ** and TRIGGER_A
368aa 46 54 45 52 20 62 69 74 73 20 73 65 74 2e 20 56  FTER bits set. V
368ab 61 6c 75 65 73 20 61 63 63 65 73 73 65 64 20 62  alues accessed b
368ac 79 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72  y BEFORE trigger
368ad 73 20 61 72 65 20 6f 6e 6c 79 0a 2a 2a 20 69 6e  s are only.** in
368ae 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 72 65  cluded in the re
368af 74 75 72 6e 65 64 20 6d 61 73 6b 20 69 66 20 74  turned mask if t
368b0 68 65 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52  he TRIGGER_BEFOR
368b1 45 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20  E bit is set in 
368b2 74 68 65 0a 2a 2a 20 74 72 5f 74 6d 20 70 61 72  the.** tr_tm par
368b3 61 6d 65 74 65 72 2e 20 53 69 6d 69 6c 61 72 6c  ameter. Similarl
368b4 79 2c 20 76 61 6c 75 65 73 20 61 63 63 65 73 73  y, values access
368b5 65 64 20 62 79 20 41 46 54 45 52 20 74 72 69 67  ed by AFTER trig
368b6 67 65 72 73 20 61 72 65 20 6f 6e 6c 79 0a 2a 2a  gers are only.**
368b7 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65   included in the
368b8 20 72 65 74 75 72 6e 65 64 20 6d 61 73 6b 20 69   returned mask i
368b9 66 20 74 68 65 20 54 52 49 47 47 45 52 5f 41 46  f the TRIGGER_AF
368ba 54 45 52 20 62 69 74 20 69 73 20 73 65 74 20 69  TER bit is set i
368bb 6e 20 74 72 5f 74 6d 2e 0a 2a 2f 0a 53 51 4c 49  n tr_tm..*/.SQLI
368bc 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73  TE_PRIVATE u32 s
368bd 71 6c 69 74 65 33 54 72 69 67 67 65 72 43 6f 6c  qlite3TriggerCol
368be 6d 61 73 6b 28 0a 20 20 50 61 72 73 65 20 2a 70  mask(.  Parse *p
368bf 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  Parse,       /* 
368c0 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  Parse context */
368c1 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
368c2 67 67 65 72 2c 20 20 20 2f 2a 20 4c 69 73 74 20  gger,   /* List 
368c3 6f 66 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74  of triggers on t
368c4 61 62 6c 65 20 70 54 61 62 20 2a 2f 0a 20 20 45  able pTab */.  E
368c5 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65  xprList *pChange
368c6 73 2c 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 6c  s,  /* Changes l
368c7 69 73 74 20 66 6f 72 20 61 6e 79 20 55 50 44 41  ist for any UPDA
368c8 54 45 20 4f 46 20 74 72 69 67 67 65 72 73 20 2a  TE OF triggers *
368c9 2f 0a 20 20 69 6e 74 20 69 73 4e 65 77 2c 20 20  /.  int isNew,  
368ca 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f           /* 1 fo
368cb 72 20 6e 65 77 2e 2a 20 72 65 66 20 6d 61 73 6b  r new.* ref mask
368cc 2c 20 30 20 66 6f 72 20 6f 6c 64 2e 2a 20 72 65  , 0 for old.* re
368cd 66 20 6d 61 73 6b 20 2a 2f 0a 20 20 69 6e 74 20  f mask */.  int 
368ce 74 72 5f 74 6d 2c 20 20 20 20 20 20 20 20 20 20  tr_tm,          
368cf 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 54 52 49 47   /* Mask of TRIG
368d0 47 45 52 5f 42 45 46 4f 52 45 7c 54 52 49 47 47  GER_BEFORE|TRIGG
368d1 45 52 5f 41 46 54 45 52 20 2a 2f 0a 20 20 54 61  ER_AFTER */.  Ta
368d2 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
368d3 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
368d4 74 6f 20 63 6f 64 65 20 74 72 69 67 67 65 72 73  to code triggers
368d5 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 6f   from */.  int o
368d6 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20 20  rconf           
368d7 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 43 4f  /* Default ON CO
368d8 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 66 6f  NFLICT policy fo
368d9 72 20 74 72 69 67 67 65 72 20 73 74 65 70 73 20  r trigger steps 
368da 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 69 6e  */.){.  const in
368db 74 20 6f 70 20 3d 20 70 43 68 61 6e 67 65 73 20  t op = pChanges 
368dc 3f 20 54 4b 5f 55 50 44 41 54 45 20 3a 20 54 4b  ? TK_UPDATE : TK
368dd 5f 44 45 4c 45 54 45 3b 0a 20 20 75 33 32 20 6d  _DELETE;.  u32 m
368de 61 73 6b 20 3d 20 30 3b 0a 20 20 54 72 69 67 67  ask = 0;.  Trigg
368df 65 72 20 2a 70 3b 0a 0a 20 20 61 73 73 65 72 74  er *p;..  assert
368e0 28 20 69 73 4e 65 77 3d 3d 31 20 7c 7c 20 69 73  ( isNew==1 || is
368e1 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28  New==0 );.  for(
368e2 70 3d 70 54 72 69 67 67 65 72 3b 20 70 3b 20 70  p=pTrigger; p; p
368e3 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  =p->pNext){.    
368e4 69 66 28 20 70 2d 3e 6f 70 3d 3d 6f 70 20 26 26  if( p->op==op &&
368e5 20 28 74 72 5f 74 6d 26 70 2d 3e 74 72 5f 74 6d   (tr_tm&p->tr_tm
368e6 29 0a 20 20 20 20 20 26 26 20 63 68 65 63 6b 43  ).     && checkC
368e7 6f 6c 75 6d 6e 4f 76 65 72 6c 61 70 28 70 2d 3e  olumnOverlap(p->
368e8 70 43 6f 6c 75 6d 6e 73 2c 70 43 68 61 6e 67 65  pColumns,pChange
368e9 73 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  s).    ){.      
368ea 54 72 69 67 67 65 72 50 72 67 20 2a 70 50 72 67  TriggerPrg *pPrg
368eb 3b 0a 20 20 20 20 20 20 70 50 72 67 20 3d 20 67  ;.      pPrg = g
368ec 65 74 52 6f 77 54 72 69 67 67 65 72 28 70 50 61  etRowTrigger(pPa
368ed 72 73 65 2c 20 70 2c 20 70 54 61 62 2c 20 6f 72  rse, p, pTab, or
368ee 63 6f 6e 66 29 3b 0a 20 20 20 20 20 20 69 66 28  conf);.      if(
368ef 20 70 50 72 67 20 29 7b 0a 20 20 20 20 20 20 20   pPrg ){.       
368f0 20 6d 61 73 6b 20 7c 3d 20 70 50 72 67 2d 3e 61   mask |= pPrg->a
368f1 43 6f 6c 6d 61 73 6b 5b 69 73 4e 65 77 5d 3b 0a  Colmask[isNew];.
368f2 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
368f3 7d 0a 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b  }..  return mask
368f4 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  ;.}..#endif /* !
368f5 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
368f6 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a  MIT_TRIGGER) */.
368f7 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
368f8 20 45 6e 64 20 6f 66 20 74 72 69 67 67 65 72 2e   End of trigger.
368f9 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
368fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
368fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
368fc 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
368fd 20 42 65 67 69 6e 20 66 69 6c 65 20 75 70 64 61   Begin file upda
368fe 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  te.c ***********
368ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
36901 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
36902 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
36903 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
36904 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
36905 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
36906 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
36907 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
36908 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
36909 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
3690a 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
3690b 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
3690c 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
3690d 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
3690e 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
3690f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
36910 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
36911 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
36912 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
36913 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
36914 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36915 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36916 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36917 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36918 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
36919 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20  file contains C 
3691a 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68  code routines th
3691b 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79  at are called by
3691c 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 74   the parser.** t
3691d 6f 20 68 61 6e 64 6c 65 20 55 50 44 41 54 45 20  o handle UPDATE 
3691e 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 0a  statements..*/..
3691f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
36920 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
36921 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  ./* Forward decl
36922 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69  aration */.stati
36923 63 20 76 6f 69 64 20 75 70 64 61 74 65 56 69 72  c void updateVir
36924 74 75 61 6c 54 61 62 6c 65 28 0a 20 20 50 61 72  tualTable(.  Par
36925 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
36926 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
36927 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
36928 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20  cList *pSrc,    
36929 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
3692a 6c 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6d 6f  l table to be mo
3692b 64 69 66 69 65 64 20 2a 2f 0a 20 20 54 61 62 6c  dified */.  Tabl
3692c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20  e *pTab,        
3692d 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20   /* The virtual 
3692e 74 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c  table */.  ExprL
3692f 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20  ist *pChanges,  
36930 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 74  /* The columns t
36931 6f 20 63 68 61 6e 67 65 20 69 6e 20 74 68 65 20  o change in the 
36932 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
36933 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f 77   */.  Expr *pRow
36934 69 64 45 78 70 72 2c 20 20 20 20 2f 2a 20 45 78  idExpr,    /* Ex
36935 70 72 65 73 73 69 6f 6e 20 75 73 65 64 20 74 6f  pression used to
36936 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65 20 72   recompute the r
36937 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  owid */.  int *a
36938 58 52 65 66 2c 20 20 20 20 20 20 20 20 20 20 2f  XRef,          /
36939 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63  * Mapping from c
3693a 6f 6c 75 6d 6e 73 20 6f 66 20 70 54 61 62 20 74  olumns of pTab t
3693b 6f 20 65 6e 74 72 69 65 73 20 69 6e 20 70 43 68  o entries in pCh
3693c 61 6e 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 20  anges */.  Expr 
3693d 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20  *pWhere,        
3693e 2f 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  /* WHERE clause 
3693f 6f 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74  of the UPDATE st
36940 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  atement */.  int
36941 20 6f 6e 45 72 72 6f 72 20 20 20 20 20 20 20 20   onError        
36942 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54    /* ON CONFLICT
36943 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 29 3b 0a   strategy */.);.
36944 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
36945 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
36946 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  LE */../*.** The
36947 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63   most recently c
36948 6f 64 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e  oded instruction
36949 20 77 61 73 20 61 6e 20 4f 50 5f 43 6f 6c 75 6d   was an OP_Colum
3694a 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68  n to retrieve th
3694b 65 0a 2a 2a 20 69 2d 74 68 20 63 6f 6c 75 6d 6e  e.** i-th column
3694c 20 6f 66 20 74 61 62 6c 65 20 70 54 61 62 2e 20   of table pTab. 
3694d 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  This routine set
3694e 73 20 74 68 65 20 50 34 20 70 61 72 61 6d 65 74  s the P4 paramet
3694f 65 72 20 6f 66 20 74 68 65 20 0a 2a 2a 20 4f 50  er of the .** OP
36950 5f 43 6f 6c 75 6d 6e 20 74 6f 20 74 68 65 20 64  _Column to the d
36951 65 66 61 75 6c 74 20 76 61 6c 75 65 2c 20 69 66  efault value, if
36952 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   any..**.** The 
36953 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66  default value of
36954 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 73 70 65   a column is spe
36955 63 69 66 69 65 64 20 62 79 20 61 20 44 45 46 41  cified by a DEFA
36956 55 4c 54 20 63 6c 61 75 73 65 20 69 6e 20 74 68  ULT clause in th
36957 65 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64 65 66  e .** column def
36958 69 6e 69 74 69 6f 6e 2e 20 54 68 69 73 20 77 61  inition. This wa
36959 73 20 65 69 74 68 65 72 20 73 75 70 70 6c 69 65  s either supplie
3695a 64 20 62 79 20 74 68 65 20 75 73 65 72 20 77 68  d by the user wh
3695b 65 6e 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20  en the table.** 
3695c 77 61 73 20 63 72 65 61 74 65 64 2c 20 6f 72 20  was created, or 
3695d 61 64 64 65 64 20 6c 61 74 65 72 20 74 6f 20 74  added later to t
3695e 68 65 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74  he table definit
3695f 69 6f 6e 20 62 79 20 61 6e 20 41 4c 54 45 52 20  ion by an ALTER 
36960 54 41 42 4c 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64  TABLE.** command
36961 2e 20 49 66 20 74 68 65 20 6c 61 74 74 65 72 2c  . If the latter,
36962 20 74 68 65 6e 20 74 68 65 20 72 6f 77 2d 72 65   then the row-re
36963 63 6f 72 64 73 20 69 6e 20 74 68 65 20 74 61 62  cords in the tab
36964 6c 65 20 62 74 72 65 65 20 6f 6e 20 64 69 73 6b  le btree on disk
36965 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 63 6f 6e 74  .** may not cont
36966 61 69 6e 20 61 20 76 61 6c 75 65 20 66 6f 72 20  ain a value for 
36967 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 74  the column and t
36968 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
36969 2c 20 74 61 6b 65 6e 0a 2a 2a 20 66 72 6f 6d 20  , taken.** from 
3696a 74 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 72  the P4 parameter
3696b 20 6f 66 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d   of the OP_Colum
3696c 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c 20 69  n instruction, i
3696d 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65  s returned inste
3696e 61 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f  ad..** If the fo
3696f 72 6d 65 72 2c 20 74 68 65 6e 20 61 6c 6c 20 72  rmer, then all r
36970 6f 77 2d 72 65 63 6f 72 64 73 20 61 72 65 20 67  ow-records are g
36971 75 61 72 61 6e 74 65 65 64 20 74 6f 20 69 6e 63  uaranteed to inc
36972 6c 75 64 65 20 61 20 76 61 6c 75 65 0a 2a 2a 20  lude a value.** 
36973 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61  for the column a
36974 6e 64 20 74 68 65 20 50 34 20 76 61 6c 75 65 20  nd the P4 value 
36975 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e  is not required.
36976 0a 2a 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 64 65  .**.** Column de
36977 66 69 6e 69 74 69 6f 6e 73 20 63 72 65 61 74 65  finitions create
36978 64 20 62 79 20 61 6e 20 41 4c 54 45 52 20 54 41  d by an ALTER TA
36979 42 4c 45 20 63 6f 6d 6d 61 6e 64 20 6d 61 79 20  BLE command may 
3697a 6f 6e 6c 79 20 68 61 76 65 20 0a 2a 2a 20 6c 69  only have .** li
3697b 74 65 72 61 6c 20 64 65 66 61 75 6c 74 20 76 61  teral default va
3697c 6c 75 65 73 20 73 70 65 63 69 66 69 65 64 3a 20  lues specified: 
3697d 61 20 6e 75 6d 62 65 72 2c 20 6e 75 6c 6c 20 6f  a number, null o
3697e 72 20 61 20 73 74 72 69 6e 67 2e 20 28 49 66 20  r a string. (If 
3697f 61 20 6d 6f 72 65 0a 2a 2a 20 63 6f 6d 70 6c 69  a more.** compli
36980 63 61 74 65 64 20 64 65 66 61 75 6c 74 20 65 78  cated default ex
36981 70 72 65 73 73 69 6f 6e 20 76 61 6c 75 65 20 77  pression value w
36982 61 73 20 70 72 6f 76 69 64 65 64 2c 20 69 74 20  as provided, it 
36983 69 73 20 65 76 61 6c 75 61 74 65 64 20 0a 2a 2a  is evaluated .**
36984 20 77 68 65 6e 20 74 68 65 20 41 4c 54 45 52 20   when the ALTER 
36985 54 41 42 4c 45 20 69 73 20 65 78 65 63 75 74 65  TABLE is execute
36986 64 20 61 6e 64 20 6f 6e 65 20 6f 66 20 74 68 65  d and one of the
36987 20 6c 69 74 65 72 61 6c 20 76 61 6c 75 65 73 20   literal values 
36988 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20  written.** into 
36989 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  the sqlite_maste
3698a 72 20 74 61 62 6c 65 2e 29 0a 2a 2a 0a 2a 2a 20  r table.).**.** 
3698b 54 68 65 72 65 66 6f 72 65 2c 20 74 68 65 20 50  Therefore, the P
3698c 34 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6f  4 parameter is o
3698d 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 20  nly required if 
3698e 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
3698f 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 63 6f 6c  e for.** the col
36990 75 6d 6e 20 69 73 20 61 20 6c 69 74 65 72 61 6c  umn is a literal
36991 20 6e 75 6d 62 65 72 2c 20 73 74 72 69 6e 67 20   number, string 
36992 6f 72 20 6e 75 6c 6c 2e 20 54 68 65 20 73 71 6c  or null. The sql
36993 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70  ite3ValueFromExp
36994 72 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  r().** function 
36995 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20 74 72  is capable of tr
36996 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68 65 73 65  ansforming these
36997 20 74 79 70 65 73 20 6f 66 20 65 78 70 72 65 73   types of expres
36998 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20 73 71  sions into.** sq
36999 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
3699a 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61  cts..**.** If pa
3699b 72 61 6d 65 74 65 72 20 69 52 65 67 20 69 73 20  rameter iReg is 
3699c 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 63 6f  not negative, co
3699d 64 65 20 61 6e 20 4f 50 5f 52 65 61 6c 41 66 66  de an OP_RealAff
3699e 69 6e 69 74 79 20 69 6e 73 74 72 75 63 74 69 6f  inity instructio
3699f 6e 0a 2a 2a 20 6f 6e 20 72 65 67 69 73 74 65 72  n.** on register
369a0 20 69 52 65 67 2e 20 54 68 69 73 20 69 73 20 75   iReg. This is u
369a1 73 65 64 20 77 68 65 6e 20 61 6e 20 65 71 75 69  sed when an equi
369a2 76 61 6c 65 6e 74 20 69 6e 74 65 67 65 72 20 76  valent integer v
369a3 61 6c 75 65 20 69 73 20 0a 2a 2a 20 73 74 6f 72  alue is .** stor
369a4 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61  ed in place of a
369a5 6e 20 38 2d 62 79 74 65 20 66 6c 6f 61 74 69 6e  n 8-byte floatin
369a6 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 6e  g point value in
369a7 20 6f 72 64 65 72 20 74 6f 20 73 61 76 65 20 0a   order to save .
369a8 2a 2a 20 73 70 61 63 65 2e 0a 2a 2f 0a 53 51 4c  ** space..*/.SQL
369a9 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
369aa 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65   sqlite3ColumnDe
369ab 66 61 75 6c 74 28 56 64 62 65 20 2a 76 2c 20 54  fault(Vdbe *v, T
369ac 61 62 6c 65 20 2a 70 54 61 62 2c 20 69 6e 74 20  able *pTab, int 
369ad 69 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20 20  i, int iReg){.  
369ae 61 73 73 65 72 74 28 20 70 54 61 62 21 3d 30 20  assert( pTab!=0 
369af 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e  );.  if( !pTab->
369b0 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73  pSelect ){.    s
369b1 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
369b2 61 6c 75 65 3b 0a 20 20 20 20 75 38 20 65 6e 63  alue;.    u8 enc
369b3 20 3d 20 45 4e 43 28 73 71 6c 69 74 65 33 56 64   = ENC(sqlite3Vd
369b4 62 65 44 62 28 76 29 29 3b 0a 20 20 20 20 43 6f  beDb(v));.    Co
369b5 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54  lumn *pCol = &pT
369b6 61 62 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20  ab->aCol[i];.   
369b7 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
369b8 20 22 25 73 2e 25 73 22 2c 20 70 54 61 62 2d 3e   "%s.%s", pTab->
369b9 7a 4e 61 6d 65 2c 20 70 43 6f 6c 2d 3e 7a 4e 61  zName, pCol->zNa
369ba 6d 65 29 29 3b 0a 20 20 20 20 61 73 73 65 72 74  me));.    assert
369bb 28 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29  ( i<pTab->nCol )
369bc 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c  ;.    sqlite3Val
369bd 75 65 46 72 6f 6d 45 78 70 72 28 73 71 6c 69 74  ueFromExpr(sqlit
369be 65 33 56 64 62 65 44 62 28 76 29 2c 20 70 43 6f  e3VdbeDb(v), pCo
369bf 6c 2d 3e 70 44 66 6c 74 2c 20 65 6e 63 2c 20 0a  l->pDflt, enc, .
369c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
369c1 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 61           pCol->a
369c2 66 66 69 6e 69 74 79 2c 20 26 70 56 61 6c 75 65  ffinity, &pValue
369c3 29 3b 0a 20 20 20 20 69 66 28 20 70 56 61 6c 75  );.    if( pValu
369c4 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  e ){.      sqlit
369c5 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
369c6 2c 20 2d 31 2c 20 28 63 6f 6e 73 74 20 63 68 61  , -1, (const cha
369c7 72 20 2a 29 70 56 61 6c 75 65 2c 20 50 34 5f 4d  r *)pValue, P4_M
369c8 45 4d 29 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  EM);.    }.#ifnd
369c9 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
369ca 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
369cb 20 20 69 66 28 20 69 52 65 67 3e 3d 30 20 26 26    if( iReg>=0 &&
369cc 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 61   pTab->aCol[i].a
369cd 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
369ce 41 46 46 5f 52 45 41 4c 20 29 7b 0a 20 20 20 20  AFF_REAL ){.    
369cf 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
369d0 4f 70 31 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66  Op1(v, OP_RealAf
369d1 66 69 6e 69 74 79 2c 20 69 52 65 67 29 3b 0a 20  finity, iReg);. 
369d2 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
369d3 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73  }../*.** Process
369d4 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65   an UPDATE state
369d5 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 55 50  ment..**.**   UP
369d6 44 41 54 45 20 4f 52 20 49 47 4e 4f 52 45 20 74  DATE OR IGNORE t
369d7 61 62 6c 65 5f 77 78 79 7a 20 53 45 54 20 61 3d  able_wxyz SET a=
369d8 62 2c 20 63 3d 64 20 57 48 45 52 45 20 65 3c 35  b, c=d WHERE e<5
369d9 20 41 4e 44 20 66 20 4e 4f 54 20 4e 55 4c 4c 3b   AND f NOT NULL;
369da 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5c 5f 5f  .**          \__
369db 5f 5f 5f 5f 5f 2f 20 5c 5f 5f 5f 5f 5f 5f 5f 5f  _____/ \________
369dc 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 2f 20 20  /     \______/  
369dd 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f       \__________
369de 5f 5f 5f 5f 5f 5f 2f 0a 2a 20 20 20 20 20 20 20  ______/.*       
369df 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 20 20 70       onError   p
369e0 54 61 62 4c 69 73 74 20 20 20 20 20 20 70 43 68  TabList      pCh
369e1 61 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 20  anges           
369e2 20 20 70 57 68 65 72 65 0a 2a 2f 0a 53 51 4c 49    pWhere.*/.SQLI
369e3 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
369e4 73 71 6c 69 74 65 33 55 70 64 61 74 65 28 0a 20  sqlite3Update(. 
369e5 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
369e6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
369e7 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
369e8 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
369e9 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 54 68 65  List,     /* The
369ea 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68 20   table in which 
369eb 77 65 20 73 68 6f 75 6c 64 20 63 68 61 6e 67 65  we should change
369ec 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20 45 78 70   things */.  Exp
369ed 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c  rList *pChanges,
369ee 20 20 20 20 2f 2a 20 54 68 69 6e 67 73 20 74 6f      /* Things to
369ef 20 62 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20   be changed */. 
369f0 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20   Expr *pWhere,  
369f1 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
369f2 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61  HERE clause.  Ma
369f3 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 69  y be null */.  i
369f4 6e 74 20 6f 6e 45 72 72 6f 72 20 20 20 20 20 20  nt onError      
369f5 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20        /* How to 
369f6 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e  handle constrain
369f7 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29 7b 0a 20  t errors */.){. 
369f8 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20   int i, j;      
369f9 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
369fa 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 54 61  counters */.  Ta
369fb 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
369fc 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
369fd 65 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20  e to be updated 
369fe 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20  */.  int addr = 
369ff 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56  0;          /* V
36a00 44 42 45 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  DBE instruction 
36a01 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 73  address of the s
36a02 74 61 72 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70  tart of the loop
36a03 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20   */.  WhereInfo 
36a04 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20  *pWInfo;     /* 
36a05 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
36a06 74 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  t the WHERE clau
36a07 73 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b  se */.  Vdbe *v;
36a08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36a09 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 64 61  * The virtual da
36a0a 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 2a 2f  tabase engine */
36a0b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20  .  Index *pIdx; 
36a0c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
36a0d 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e   looping over in
36a0e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  dices */.  int n
36a0f 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Idx;            
36a10 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69    /* Number of i
36a11 6e 64 69 63 65 73 20 74 68 61 74 20 6e 65 65 64  ndices that need
36a12 20 75 70 64 61 74 69 6e 67 20 2a 2f 0a 20 20 69   updating */.  i
36a13 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 20  nt iCur;        
36a14 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75        /* VDBE Cu
36a15 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 70  rsor number of p
36a16 54 61 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  Tab */.  sqlite3
36a17 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20   *db;           
36a18 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
36a19 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69  structure */.  i
36a1a 6e 74 20 2a 61 52 65 67 49 64 78 20 3d 20 30 3b  nt *aRegIdx = 0;
36a1b 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 72 65 67        /* One reg
36a1c 69 73 74 65 72 20 61 73 73 69 67 6e 65 64 20 74  ister assigned t
36a1d 6f 20 65 61 63 68 20 69 6e 64 65 78 20 74 6f 20  o each index to 
36a1e 62 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20  be updated */.  
36a1f 69 6e 74 20 2a 61 58 52 65 66 20 3d 20 30 3b 20  int *aXRef = 0; 
36a20 20 20 20 20 20 20 20 2f 2a 20 61 58 52 65 66 5b         /* aXRef[
36a21 69 5d 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  i] is the index 
36a22 69 6e 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 5d  in pChanges->a[]
36a23 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20   of the.        
36a24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36a25 20 2a 2a 20 61 6e 20 65 78 70 72 65 73 73 69 6f   ** an expressio
36a26 6e 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63  n for the i-th c
36a27 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62  olumn of the tab
36a28 6c 65 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  le..            
36a29 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
36a2a 61 58 52 65 66 5b 69 5d 3d 3d 2d 31 20 69 66 20  aXRef[i]==-1 if 
36a2b 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20  the i-th column 
36a2c 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2e 20  is not changed. 
36a2d 2a 2f 0a 20 20 69 6e 74 20 63 68 6e 67 52 6f 77  */.  int chngRow
36a2e 69 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  id;         /* T
36a2f 72 75 65 20 69 66 20 74 68 65 20 72 65 63 6f 72  rue if the recor
36a30 64 20 6e 75 6d 62 65 72 20 69 73 20 62 65 69 6e  d number is bein
36a31 67 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 45  g changed */.  E
36a32 78 70 72 20 2a 70 52 6f 77 69 64 45 78 70 72 20  xpr *pRowidExpr 
36a33 3d 20 30 3b 20 20 2f 2a 20 45 78 70 72 65 73 73  = 0;  /* Express
36a34 69 6f 6e 20 64 65 66 69 6e 69 6e 67 20 74 68 65  ion defining the
36a35 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62   new record numb
36a36 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e  er */.  int open
36a37 41 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  All = 0;       /
36a38 2a 20 54 72 75 65 20 69 66 20 61 6c 6c 20 69 6e  * True if all in
36a39 64 69 63 65 73 20 6e 65 65 64 20 74 6f 20 62 65  dices need to be
36a3a 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 41 75 74   opened */.  Aut
36a3b 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78  hContext sContex
36a3c 74 3b 20 20 2f 2a 20 54 68 65 20 61 75 74 68 6f  t;  /* The autho
36a3d 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74  rization context
36a3e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   */.  NameContex
36a3f 74 20 73 4e 43 3b 20 20 20 20 20 20 20 2f 2a 20  t sNC;       /* 
36a40 54 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74  The name-context
36a41 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72   to resolve expr
36a42 65 73 73 69 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20  essions in */.  
36a43 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
36a44 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
36a45 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  se containing th
36a46 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 75 70  e table being up
36a47 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f  dated */.  int o
36a48 6b 4f 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20  kOnePass;       
36a49 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6f 6e    /* True for on
36a4a 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d  e-pass algorithm
36a4b 20 77 69 74 68 6f 75 74 20 74 68 65 20 46 49 46   without the FIF
36a4c 4f 20 2a 2f 0a 20 20 69 6e 74 20 68 61 73 46 4b  O */.  int hasFK
36a4d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
36a4e 20 54 72 75 65 20 69 66 20 66 6f 72 65 69 67 6e   True if foreign
36a4f 20 6b 65 79 20 70 72 6f 63 65 73 73 69 6e 67 20   key processing 
36a50 69 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 0a  is required */..
36a51 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
36a52 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 69 6e  MIT_TRIGGER.  in
36a53 74 20 69 73 56 69 65 77 3b 20 20 20 20 20 20 20  t isView;       
36a54 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 68 65       /* True whe
36a55 6e 20 75 70 64 61 74 69 6e 67 20 61 20 76 69 65  n updating a vie
36a56 77 20 28 49 4e 53 54 45 41 44 20 4f 46 20 74 72  w (INSTEAD OF tr
36a57 69 67 67 65 72 29 20 2a 2f 0a 20 20 54 72 69 67  igger) */.  Trig
36a58 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20  ger *pTrigger;  
36a59 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72     /* List of tr
36a5a 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62 2c 20  iggers on pTab, 
36a5b 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20  if required */. 
36a5c 20 69 6e 74 20 74 6d 61 73 6b 3b 20 20 20 20 20   int tmask;     
36a5d 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
36a5e 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52  of TRIGGER_BEFOR
36a5f 45 7c 54 52 49 47 47 45 52 5f 41 46 54 45 52 20  E|TRIGGER_AFTER 
36a60 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20  */.#endif.  int 
36a61 6e 65 77 6d 61 73 6b 3b 20 20 20 20 20 20 20 20  newmask;        
36a62 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 4e 45     /* Mask of NE
36a63 57 2e 2a 20 63 6f 6c 75 6d 6e 73 20 61 63 63 65  W.* columns acce
36a64 73 73 65 64 20 62 79 20 42 45 46 4f 52 45 20 74  ssed by BEFORE t
36a65 72 69 67 67 65 72 73 20 2a 2f 0a 0a 20 20 2f 2a  riggers */..  /*
36a66 20 52 65 67 69 73 74 65 72 20 41 6c 6c 6f 63 61   Register Alloca
36a67 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 72  tions */.  int r
36a68 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 30 3b 20  egRowCount = 0; 
36a69 20 20 2f 2a 20 41 20 63 6f 75 6e 74 20 6f 66 20    /* A count of 
36a6a 72 6f 77 73 20 63 68 61 6e 67 65 64 20 2a 2f 0a  rows changed */.
36a6b 20 20 69 6e 74 20 72 65 67 4f 6c 64 52 6f 77 69    int regOldRowi
36a6c 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  d;       /* The 
36a6d 6f 6c 64 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69  old rowid */.  i
36a6e 6e 74 20 72 65 67 4e 65 77 52 6f 77 69 64 3b 20  nt regNewRowid; 
36a6f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77        /* The new
36a70 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20   rowid */.  int 
36a71 72 65 67 4e 65 77 3b 20 20 20 20 20 20 20 20 20  regNew;         
36a72 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66     /* Content of
36a73 20 74 68 65 20 4e 45 57 2e 2a 20 74 61 62 6c 65   the NEW.* table
36a74 20 69 6e 20 74 72 69 67 67 65 72 73 20 2a 2f 0a   in triggers */.
36a75 20 20 69 6e 74 20 72 65 67 4f 6c 64 20 3d 20 30    int regOld = 0
36a76 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74  ;        /* Cont
36a77 65 6e 74 20 6f 66 20 4f 4c 44 2e 2a 20 74 61 62  ent of OLD.* tab
36a78 6c 65 20 69 6e 20 74 72 69 67 67 65 72 73 20 2a  le in triggers *
36a79 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 53 65  /.  int regRowSe
36a7a 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 52 6f  t = 0;     /* Ro
36a7b 77 73 65 74 20 6f 66 20 72 6f 77 73 20 74 6f 20  wset of rows to 
36a7c 62 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 0a 20  be updated */.. 
36a7d 20 6d 65 6d 73 65 74 28 26 73 43 6f 6e 74 65 78   memset(&sContex
36a7e 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 43 6f  t, 0, sizeof(sCo
36a7f 6e 74 65 78 74 29 29 3b 0a 20 20 64 62 20 3d 20  ntext));.  db = 
36a80 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66  pParse->db;.  if
36a81 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
36a82 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
36a83 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75  ed ){.    goto u
36a84 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
36a85 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61   }.  assert( pTa
36a86 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29  bList->nSrc==1 )
36a87 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74  ;..  /* Locate t
36a88 68 65 20 74 61 62 6c 65 20 77 68 69 63 68 20 77  he table which w
36a89 65 20 77 61 6e 74 20 74 6f 20 75 70 64 61 74 65  e want to update
36a8a 2e 20 0a 20 20 2a 2f 0a 20 20 70 54 61 62 20 3d  . .  */.  pTab =
36a8b 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c   sqlite3SrcListL
36a8c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54  ookup(pParse, pT
36a8d 61 62 4c 69 73 74 29 3b 0a 20 20 69 66 28 20 70  abList);.  if( p
36a8e 54 61 62 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70  Tab==0 ) goto up
36a8f 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  date_cleanup;.  
36a90 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
36a91 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
36a92 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
36a93 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a 20 46 69 67  hema);..  /* Fig
36a94 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 68 61  ure out if we ha
36a95 76 65 20 61 6e 79 20 74 72 69 67 67 65 72 73 20  ve any triggers 
36a96 61 6e 64 20 69 66 20 74 68 65 20 74 61 62 6c 65  and if the table
36a97 20 62 65 69 6e 67 0a 20 20 2a 2a 20 75 70 64 61   being.  ** upda
36a98 74 65 64 20 69 73 20 61 20 76 69 65 77 2e 0a 20  ted is a view.. 
36a99 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
36a9a 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
36a9b 20 20 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c    pTrigger = sql
36a9c 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73  ite3TriggersExis
36a9d 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  t(pParse, pTab, 
36a9e 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68 61 6e  TK_UPDATE, pChan
36a9f 67 65 73 2c 20 26 74 6d 61 73 6b 29 3b 0a 20 20  ges, &tmask);.  
36aa0 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e 70  isView = pTab->p
36aa1 53 65 6c 65 63 74 21 3d 30 3b 0a 20 20 61 73 73  Select!=0;.  ass
36aa2 65 72 74 28 20 70 54 72 69 67 67 65 72 20 7c 7c  ert( pTrigger ||
36aa3 20 74 6d 61 73 6b 3d 3d 30 20 29 3b 0a 23 65 6c   tmask==0 );.#el
36aa4 73 65 0a 23 20 64 65 66 69 6e 65 20 70 54 72 69  se.# define pTri
36aa5 67 67 65 72 20 30 0a 23 20 64 65 66 69 6e 65 20  gger 0.# define 
36aa6 69 73 56 69 65 77 20 30 0a 23 20 64 65 66 69 6e  isView 0.# defin
36aa7 65 20 74 6d 61 73 6b 20 30 0a 23 65 6e 64 69 66  e tmask 0.#endif
36aa8 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
36aa9 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64 65 66  MIT_VIEW.# undef
36aaa 20 69 73 56 69 65 77 0a 23 20 64 65 66 69 6e 65   isView.# define
36aab 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66   isView 0.#endif
36aac 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  ..  if( sqlite3V
36aad 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
36aae 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20  s(pParse, pTab) 
36aaf 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61  ){.    goto upda
36ab0 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  te_cleanup;.  }.
36ab1 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52    if( sqlite3IsR
36ab2 65 61 64 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20  eadOnly(pParse, 
36ab3 70 54 61 62 2c 20 74 6d 61 73 6b 29 20 29 7b 0a  pTab, tmask) ){.
36ab4 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
36ab5 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61  cleanup;.  }.  a
36ab6 58 52 65 66 20 3d 20 73 71 6c 69 74 65 33 44 62  XRef = sqlite3Db
36ab7 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69  MallocRaw(db, si
36ab8 7a 65 6f 66 28 69 6e 74 29 20 2a 20 70 54 61 62  zeof(int) * pTab
36ab9 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 69 66 28 20  ->nCol );.  if( 
36aba 61 58 52 65 66 3d 3d 30 20 29 20 67 6f 74 6f 20  aXRef==0 ) goto 
36abb 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
36abc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
36abd 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 20 61 58  b->nCol; i++) aX
36abe 52 65 66 5b 69 5d 20 3d 20 2d 31 3b 0a 0a 20 20  Ref[i] = -1;..  
36abf 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 63 75  /* Allocate a cu
36ac0 72 73 6f 72 73 20 66 6f 72 20 74 68 65 20 6d 61  rsors for the ma
36ac1 69 6e 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  in database tabl
36ac2 65 20 61 6e 64 20 66 6f 72 20 61 6c 6c 20 69 6e  e and for all in
36ac3 64 69 63 65 73 2e 0a 20 20 2a 2a 20 54 68 65 20  dices..  ** The 
36ac4 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 6d 69  index cursors mi
36ac5 67 68 74 20 6e 6f 74 20 62 65 20 75 73 65 64 2c  ght not be used,
36ac6 20 62 75 74 20 69 66 20 74 68 65 79 20 61 72 65   but if they are
36ac7 20 75 73 65 64 20 74 68 65 79 0a 20 20 2a 2a 20   used they.  ** 
36ac8 6e 65 65 64 20 74 6f 20 6f 63 63 75 72 20 72 69  need to occur ri
36ac9 67 68 74 20 61 66 74 65 72 20 74 68 65 20 64 61  ght after the da
36aca 74 61 62 61 73 65 20 63 75 72 73 6f 72 2e 20 20  tabase cursor.  
36acb 53 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 0a  So go ahead and.
36acc 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 65 6e    ** allocate en
36acd 6f 75 67 68 20 73 70 61 63 65 2c 20 6a 75 73 74  ough space, just
36ace 20 69 6e 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20   in case..  */. 
36acf 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e   pTabList->a[0].
36ad0 69 43 75 72 73 6f 72 20 3d 20 69 43 75 72 20 3d  iCursor = iCur =
36ad1 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
36ad2 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62  .  for(pIdx=pTab
36ad3 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
36ad4 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
36ad5 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  ){.    pParse->n
36ad6 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Tab++;.  }..  /*
36ad7 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
36ad8 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 2a 2f 0a  name-context */.
36ad9 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30    memset(&sNC, 0
36ada 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a  , sizeof(sNC));.
36adb 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70    sNC.pParse = p
36adc 50 61 72 73 65 3b 0a 20 20 73 4e 43 2e 70 53 72  Parse;.  sNC.pSr
36add 63 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74  cList = pTabList
36ade 3b 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20  ;..  /* Resolve 
36adf 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
36ae0 20 69 6e 20 61 6c 6c 20 74 68 65 20 65 78 70 72   in all the expr
36ae1 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 0a 20  essions of the. 
36ae2 20 2a 2a 20 6f 66 20 74 68 65 20 55 50 44 41 54   ** of the UPDAT
36ae3 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 6c  E statement.  Al
36ae4 73 6f 20 66 69 6e 64 20 74 68 65 20 63 6f 6c 75  so find the colu
36ae5 6d 6e 20 69 6e 64 65 78 0a 20 20 2a 2a 20 66 6f  mn index.  ** fo
36ae6 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 74 6f  r each column to
36ae7 20 62 65 20 75 70 64 61 74 65 64 20 69 6e 20 74   be updated in t
36ae8 68 65 20 70 43 68 61 6e 67 65 73 20 61 72 72 61  he pChanges arra
36ae9 79 2e 20 20 46 6f 72 20 65 61 63 68 0a 20 20 2a  y.  For each.  *
36aea 2a 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 75  * column to be u
36aeb 70 64 61 74 65 64 2c 20 6d 61 6b 65 20 73 75 72  pdated, make sur
36aec 65 20 77 65 20 68 61 76 65 20 61 75 74 68 6f 72  e we have author
36aed 69 7a 61 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67  ization to chang
36aee 65 0a 20 20 2a 2a 20 74 68 61 74 20 63 6f 6c 75  e.  ** that colu
36aef 6d 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 6e 67 52  mn..  */.  chngR
36af0 6f 77 69 64 20 3d 20 30 3b 0a 20 20 66 6f 72 28  owid = 0;.  for(
36af1 69 3d 30 3b 20 69 3c 70 43 68 61 6e 67 65 73 2d  i=0; i<pChanges-
36af2 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
36af3 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73    if( sqlite3Res
36af4 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73  olveExprNames(&s
36af5 4e 43 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b  NC, pChanges->a[
36af6 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20  i].pExpr) ){.   
36af7 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63     goto update_c
36af8 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20  leanup;.    }.  
36af9 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54 61    for(j=0; j<pTa
36afa 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20  b->nCol; j++){. 
36afb 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
36afc 53 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43  StrICmp(pTab->aC
36afd 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70 43 68  ol[j].zName, pCh
36afe 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  anges->a[i].zNam
36aff 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  e)==0 ){.       
36b00 20 69 66 28 20 6a 3d 3d 70 54 61 62 2d 3e 69 50   if( j==pTab->iP
36b01 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Key ){.         
36b02 20 63 68 6e 67 52 6f 77 69 64 20 3d 20 31 3b 0a   chngRowid = 1;.
36b03 20 20 20 20 20 20 20 20 20 20 70 52 6f 77 69 64            pRowid
36b04 45 78 70 72 20 3d 20 70 43 68 61 6e 67 65 73 2d  Expr = pChanges-
36b05 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[i].pExpr;.   
36b06 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61       }.        a
36b07 58 52 65 66 5b 6a 5d 20 3d 20 69 3b 0a 20 20 20  XRef[j] = i;.   
36b08 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
36b09 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
36b0a 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20  ( j>=pTab->nCol 
36b0b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
36b0c 69 74 65 33 49 73 52 6f 77 69 64 28 70 43 68 61  ite3IsRowid(pCha
36b0d 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  nges->a[i].zName
36b0e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 6e  ) ){.        chn
36b0f 67 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20  gRowid = 1;.    
36b10 20 20 20 20 70 52 6f 77 69 64 45 78 70 72 20 3d      pRowidExpr =
36b11 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e   pChanges->a[i].
36b12 70 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 65 6c  pExpr;.      }el
36b13 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
36b14 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
36b15 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c  se, "no such col
36b16 75 6d 6e 3a 20 25 73 22 2c 20 70 43 68 61 6e 67  umn: %s", pChang
36b17 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b  es->a[i].zName);
36b18 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
36b19 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31  >checkSchema = 1
36b1a 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 75  ;.        goto u
36b1b 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
36b1c 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66       }.    }.#if
36b1d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
36b1e 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20  _AUTHORIZATION. 
36b1f 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72     {.      int r
36b20 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  c;.      rc = sq
36b21 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
36b22 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 55 50  Parse, SQLITE_UP
36b23 44 41 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  DATE, pTab->zNam
36b24 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
36b25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54                pT
36b26 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d  ab->aCol[j].zNam
36b27 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  e, db->aDb[iDb].
36b28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66  zName);.      if
36b29 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e  ( rc==SQLITE_DEN
36b2a 59 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  Y ){.        got
36b2b 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70  o update_cleanup
36b2c 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
36b2d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e  ( rc==SQLITE_IGN
36b2e 4f 52 45 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ORE ){.        a
36b2f 58 52 65 66 5b 6a 5d 20 3d 20 2d 31 3b 0a 20 20  XRef[j] = -1;.  
36b30 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64      }.    }.#end
36b31 69 66 0a 20 20 7d 0a 0a 20 20 68 61 73 46 4b 20  if.  }..  hasFK 
36b32 3d 20 73 71 6c 69 74 65 33 46 6b 52 65 71 75 69  = sqlite3FkRequi
36b33 72 65 64 28 70 50 61 72 73 65 2c 20 70 54 61 62  red(pParse, pTab
36b34 2c 20 61 58 52 65 66 2c 20 63 68 6e 67 52 6f 77  , aXRef, chngRow
36b35 69 64 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  id);..  /* Alloc
36b36 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74  ate memory for t
36b37 68 65 20 61 72 72 61 79 20 61 52 65 67 49 64 78  he array aRegIdx
36b38 5b 5d 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e  [].  There is on
36b39 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 0a 20  e entry in the. 
36b3a 20 2a 2a 20 61 72 72 61 79 20 66 6f 72 20 65 61   ** array for ea
36b3b 63 68 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61  ch index associa
36b3c 74 65 64 20 77 69 74 68 20 74 61 62 6c 65 20 62  ted with table b
36b3d 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20 20 46  eing updated.  F
36b3e 69 6c 6c 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20  ill in.  ** the 
36b3f 76 61 6c 75 65 20 77 69 74 68 20 61 20 72 65 67  value with a reg
36b40 69 73 74 65 72 20 6e 75 6d 62 65 72 20 66 6f 72  ister number for
36b41 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 61 72   indices that ar
36b42 65 20 74 6f 20 62 65 20 75 73 65 64 0a 20 20 2a  e to be used.  *
36b43 2a 20 61 6e 64 20 77 69 74 68 20 7a 65 72 6f 20  * and with zero 
36b44 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64 69 63  for unused indic
36b45 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 6e  es..  */.  for(n
36b46 49 64 78 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  Idx=0, pIdx=pTab
36b47 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
36b48 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
36b49 2c 20 6e 49 64 78 2b 2b 29 7b 7d 0a 20 20 69 66  , nIdx++){}.  if
36b4a 28 20 6e 49 64 78 3e 30 20 29 7b 0a 20 20 20 20  ( nIdx>0 ){.    
36b4b 61 52 65 67 49 64 78 20 3d 20 73 71 6c 69 74 65  aRegIdx = sqlite
36b4c 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
36b4d 20 73 69 7a 65 6f 66 28 49 6e 64 65 78 2a 29 20   sizeof(Index*) 
36b4e 2a 20 6e 49 64 78 20 29 3b 0a 20 20 20 20 69 66  * nIdx );.    if
36b4f 28 20 61 52 65 67 49 64 78 3d 3d 30 20 29 20 67  ( aRegIdx==0 ) g
36b50 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
36b51 75 70 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 6a 3d  up;.  }.  for(j=
36b52 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
36b53 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
36b54 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b  =pIdx->pNext, j+
36b55 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 67 3b  +){.    int reg;
36b56 0a 20 20 20 20 69 66 28 20 68 61 73 46 4b 20 7c  .    if( hasFK |
36b57 7c 20 63 68 6e 67 52 6f 77 69 64 20 29 7b 0a 20  | chngRowid ){. 
36b58 20 20 20 20 20 72 65 67 20 3d 20 2b 2b 70 50 61       reg = ++pPa
36b59 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d  rse->nMem;.    }
36b5a 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 67 20  else{.      reg 
36b5b 3d 20 30 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  = 0;.      for(i
36b5c 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c  =0; i<pIdx->nCol
36b5d 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  umn; i++){.     
36b5e 20 20 20 69 66 28 20 61 58 52 65 66 5b 70 49 64     if( aXRef[pId
36b5f 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 3e  x->aiColumn[i]]>
36b60 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
36b61 72 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  reg = ++pParse->
36b62 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20  nMem;.          
36b63 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
36b64 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
36b65 20 20 20 61 52 65 67 49 64 78 5b 6a 5d 20 3d 20     aRegIdx[j] = 
36b66 72 65 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42  reg;.  }..  /* B
36b67 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20  egin generating 
36b68 63 6f 64 65 2e 20 2a 2f 0a 20 20 76 20 3d 20 73  code. */.  v = s
36b69 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
36b6a 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
36b6b 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f  0 ) goto update_
36b6c 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 70  cleanup;.  if( p
36b6d 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
36b6e 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f   ) sqlite3VdbeCo
36b6f 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b 0a 20  untChanges(v);. 
36b70 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
36b71 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
36b72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a 23 69  se, 1, iDb);..#i
36b73 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
36b74 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
36b75 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c   /* Virtual tabl
36b76 65 73 20 6d 75 73 74 20 62 65 20 68 61 6e 64 6c  es must be handl
36b77 65 64 20 73 65 70 61 72 61 74 65 6c 79 20 2a 2f  ed separately */
36b78 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
36b79 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 75 70  (pTab) ){.    up
36b7a 64 61 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65  dateVirtualTable
36b7b 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73  (pParse, pTabLis
36b7c 74 2c 20 70 54 61 62 2c 20 70 43 68 61 6e 67 65  t, pTab, pChange
36b7d 73 2c 20 70 52 6f 77 69 64 45 78 70 72 2c 20 61  s, pRowidExpr, a
36b7e 58 52 65 66 2c 0a 20 20 20 20 20 20 20 20 20 20  XRef,.          
36b7f 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57 68               pWh
36b80 65 72 65 2c 20 6f 6e 45 72 72 6f 72 29 3b 0a 20  ere, onError);. 
36b81 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20     pWhere = 0;. 
36b82 20 20 20 70 54 61 62 4c 69 73 74 20 3d 20 30 3b     pTabList = 0;
36b83 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65  .    goto update
36b84 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 23 65  _cleanup;.  }.#e
36b85 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  ndif..  /* Alloc
36b86 61 74 65 20 72 65 71 75 69 72 65 64 20 72 65 67  ate required reg
36b87 69 73 74 65 72 73 2e 20 2a 2f 0a 20 20 72 65 67  isters. */.  reg
36b88 52 6f 77 53 65 74 20 3d 20 2b 2b 70 50 61 72 73  RowSet = ++pPars
36b89 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f 6c  e->nMem;.  regOl
36b8a 64 52 6f 77 69 64 20 3d 20 72 65 67 4e 65 77 52  dRowid = regNewR
36b8b 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d  owid = ++pParse-
36b8c 3e 6e 4d 65 6d 3b 0a 20 20 69 66 28 20 70 54 72  >nMem;.  if( pTr
36b8d 69 67 67 65 72 20 7c 7c 20 68 61 73 46 4b 20 29  igger || hasFK )
36b8e 7b 0a 20 20 20 20 72 65 67 4f 6c 64 20 3d 20 70  {.    regOld = p
36b8f 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b  Parse->nMem + 1;
36b90 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
36b91 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b  m += pTab->nCol;
36b92 0a 20 20 7d 0a 20 20 69 66 28 20 63 68 6e 67 52  .  }.  if( chngR
36b93 6f 77 69 64 20 7c 7c 20 70 54 72 69 67 67 65 72  owid || pTrigger
36b94 20 7c 7c 20 68 61 73 46 4b 20 29 7b 0a 20 20 20   || hasFK ){.   
36b95 20 72 65 67 4e 65 77 52 6f 77 69 64 20 3d 20 2b   regNewRowid = +
36b96 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
36b97 20 7d 0a 20 20 72 65 67 4e 65 77 20 3d 20 70 50   }.  regNew = pP
36b98 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a  arse->nMem + 1;.
36b99 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
36b9a 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 0a 20  = pTab->nCol;.. 
36b9b 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20 76 69   /* Start the vi
36b9c 65 77 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20  ew context. */. 
36b9d 20 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20   if( isView ){. 
36b9e 20 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f     sqlite3AuthCo
36b9f 6e 74 65 78 74 50 75 73 68 28 70 50 61 72 73 65  ntextPush(pParse
36ba0 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20 70 54 61  , &sContext, pTa
36ba1 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 0a  b->zName);.  }..
36ba2 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 74    /* If we are t
36ba3 72 79 69 6e 67 20 74 6f 20 75 70 64 61 74 65 20  rying to update 
36ba4 61 20 76 69 65 77 2c 20 72 65 61 6c 69 7a 65 20  a view, realize 
36ba5 74 68 61 74 20 76 69 65 77 20 69 6e 74 6f 0a 20  that view into. 
36ba6 20 2a 2a 20 61 20 65 70 68 65 6d 65 72 61 6c 20   ** a ephemeral 
36ba7 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66 20  table..  */.#if 
36ba8 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
36ba9 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21 64  OMIT_VIEW) && !d
36baa 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
36bab 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 69 66  IT_TRIGGER).  if
36bac 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20  ( isView ){.    
36bad 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61 6c 69  sqlite3Materiali
36bae 7a 65 56 69 65 77 28 70 50 61 72 73 65 2c 20 70  zeView(pParse, p
36baf 54 61 62 2c 20 70 57 68 65 72 65 2c 20 69 43 75  Tab, pWhere, iCu
36bb0 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  r);.  }.#endif..
36bb1 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65    /* Resolve the
36bb2 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e   column names in
36bb3 20 61 6c 6c 20 74 68 65 20 65 78 70 72 65 73 73   all the express
36bb4 69 6f 6e 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a  ions in the.  **
36bb5 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 20   WHERE clause.. 
36bb6 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
36bb7 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
36bb8 73 28 26 73 4e 43 2c 20 70 57 68 65 72 65 29 20  s(&sNC, pWhere) 
36bb9 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61  ){.    goto upda
36bba 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  te_cleanup;.  }.
36bbb 0a 20 20 2f 2a 20 42 65 67 69 6e 20 74 68 65 20  .  /* Begin the 
36bbc 64 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 20  database scan.  
36bbd 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
36bbe 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp3(v, OP_Nul
36bbf 6c 2c 20 30 2c 20 72 65 67 52 6f 77 53 65 74 2c  l, 0, regRowSet,
36bc0 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20   regOldRowid);. 
36bc1 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65   pWInfo = sqlite
36bc2 33 57 68 65 72 65 42 65 67 69 6e 28 0a 20 20 20  3WhereBegin(.   
36bc3 20 20 20 70 50 61 72 73 65 2c 20 70 54 61 62 4c     pParse, pTabL
36bc4 69 73 74 2c 20 70 57 68 65 72 65 2c 20 30 2c 20  ist, pWhere, 0, 
36bc5 30 2c 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53  0, WHERE_ONEPASS
36bc6 5f 44 45 53 49 52 45 44 2c 20 30 0a 20 20 29 3b  _DESIRED, 0.  );
36bc7 0a 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30  .  if( pWInfo==0
36bc8 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63   ) goto update_c
36bc9 6c 65 61 6e 75 70 3b 0a 20 20 6f 6b 4f 6e 65 50  leanup;.  okOneP
36bca 61 73 73 20 3d 20 70 57 49 6e 66 6f 2d 3e 6f 6b  ass = pWInfo->ok
36bcb 4f 6e 65 50 61 73 73 3b 0a 0a 20 20 2f 2a 20 52  OnePass;..  /* R
36bcc 65 6d 65 6d 62 65 72 20 74 68 65 20 72 6f 77 69  emember the rowi
36bcd 64 20 6f 66 20 65 76 65 72 79 20 69 74 65 6d 20  d of every item 
36bce 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20  to be updated.. 
36bcf 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
36bd0 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
36bd1 77 69 64 2c 20 69 43 75 72 2c 20 72 65 67 4f 6c  wid, iCur, regOl
36bd2 64 52 6f 77 69 64 29 3b 0a 20 20 69 66 28 20 21  dRowid);.  if( !
36bd3 6f 6b 4f 6e 65 50 61 73 73 20 29 7b 0a 20 20 20  okOnePass ){.   
36bd4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
36bd5 70 32 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 41  p2(v, OP_RowSetA
36bd6 64 64 2c 20 72 65 67 52 6f 77 53 65 74 2c 20 72  dd, regRowSet, r
36bd7 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 7d  egOldRowid);.  }
36bd8 0a 0a 20 20 2f 2a 20 45 6e 64 20 74 68 65 20 64  ..  /* End the d
36bd9 61 74 61 62 61 73 65 20 73 63 61 6e 20 6c 6f 6f  atabase scan loo
36bda 70 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  p..  */.  sqlite
36bdb 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f  3WhereEnd(pWInfo
36bdc 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c  );..  /* Initial
36bdd 69 7a 65 20 74 68 65 20 63 6f 75 6e 74 20 6f 66  ize the count of
36bde 20 75 70 64 61 74 65 64 20 72 6f 77 73 0a 20 20   updated rows.  
36bdf 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c  */.  if( (db->fl
36be0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75  ags & SQLITE_Cou
36be1 6e 74 52 6f 77 73 29 20 26 26 20 21 70 50 61 72  ntRows) && !pPar
36be2 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20  se->pTriggerTab 
36be3 29 7b 0a 20 20 20 20 72 65 67 52 6f 77 43 6f 75  ){.    regRowCou
36be4 6e 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  nt = ++pParse->n
36be5 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
36be6 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
36be7 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67  _Integer, 0, reg
36be8 52 6f 77 43 6f 75 6e 74 29 3b 0a 20 20 7d 0a 0a  RowCount);.  }..
36be9 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b    if( !isView ){
36bea 0a 20 20 20 20 2f 2a 20 0a 20 20 20 20 2a 2a 20  .    /* .    ** 
36beb 4f 70 65 6e 20 65 76 65 72 79 20 69 6e 64 65 78  Open every index
36bec 20 74 68 61 74 20 6e 65 65 64 73 20 75 70 64 61   that needs upda
36bed 74 69 6e 67 2e 20 20 4e 6f 74 65 20 74 68 61 74  ting.  Note that
36bee 20 69 66 20 61 6e 79 0a 20 20 20 20 2a 2a 20 69   if any.    ** i
36bef 6e 64 65 78 20 63 6f 75 6c 64 20 70 6f 74 65 6e  ndex could poten
36bf0 74 69 61 6c 6c 79 20 69 6e 76 6f 6b 65 20 61 20  tially invoke a 
36bf1 52 45 50 4c 41 43 45 20 63 6f 6e 66 6c 69 63 74  REPLACE conflict
36bf2 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 20 20 20   resolution .   
36bf3 20 2a 2a 20 61 63 74 69 6f 6e 2c 20 74 68 65 6e   ** action, then
36bf4 20 77 65 20 6e 65 65 64 20 74 6f 20 6f 70 65 6e   we need to open
36bf5 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 62 65 63   all indices bec
36bf6 61 75 73 65 20 77 65 20 6d 69 67 68 74 20 6e 65  ause we might ne
36bf7 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20  ed.    ** to be 
36bf8 64 65 6c 65 74 69 6e 67 20 73 6f 6d 65 20 72 65  deleting some re
36bf9 63 6f 72 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  cords..    */.  
36bfa 20 20 69 66 28 20 21 6f 6b 4f 6e 65 50 61 73 73    if( !okOnePass
36bfb 20 29 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61   ) sqlite3OpenTa
36bfc 62 6c 65 28 70 50 61 72 73 65 2c 20 69 43 75 72  ble(pParse, iCur
36bfd 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f  , iDb, pTab, OP_
36bfe 4f 70 65 6e 57 72 69 74 65 29 3b 20 0a 20 20 20  OpenWrite); .   
36bff 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45   if( onError==OE
36c00 5f 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20  _Replace ){.    
36c01 20 20 6f 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a 20    openAll = 1;. 
36c02 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
36c03 6f 70 65 6e 41 6c 6c 20 3d 20 30 3b 0a 20 20 20  openAll = 0;.   
36c04 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62     for(pIdx=pTab
36c05 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
36c06 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
36c07 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
36c08 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45  Idx->onError==OE
36c09 5f 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20  _Replace ){.    
36c0a 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20        openAll = 
36c0b 31 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  1;.          bre
36c0c 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
36c0d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
36c0e 66 6f 72 28 69 3d 30 2c 20 70 49 64 78 3d 70 54  for(i=0, pIdx=pT
36c0f 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
36c10 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  ; pIdx=pIdx->pNe
36c11 78 74 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  xt, i++){.      
36c12 61 73 73 65 72 74 28 20 61 52 65 67 49 64 78 20  assert( aRegIdx 
36c13 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 65  );.      if( ope
36c14 6e 41 6c 6c 20 7c 7c 20 61 52 65 67 49 64 78 5b  nAll || aRegIdx[
36c15 69 5d 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  i]>0 ){.        
36c16 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20  KeyInfo *pKey = 
36c17 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
36c18 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78  nfo(pParse, pIdx
36c19 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
36c1a 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
36c1b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 43  OP_OpenWrite, iC
36c1c 75 72 2b 69 2b 31 2c 20 70 49 64 78 2d 3e 74 6e  ur+i+1, pIdx->tn
36c1d 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20  um, iDb,.       
36c1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c1f 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f  (char*)pKey, P4_
36c20 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
36c21 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
36c22 28 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3e 69  ( pParse->nTab>i
36c23 43 75 72 2b 69 2b 31 20 29 3b 0a 20 20 20 20 20  Cur+i+1 );.     
36c24 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
36c25 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 75 70  /* Top of the up
36c26 64 61 74 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69  date loop */.  i
36c27 66 28 20 6f 6b 4f 6e 65 50 61 73 73 20 29 7b 0a  f( okOnePass ){.
36c28 20 20 20 20 69 6e 74 20 61 31 20 3d 20 73 71 6c      int a1 = sql
36c29 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
36c2a 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65  , OP_NotNull, re
36c2b 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20 20  gOldRowid);.    
36c2c 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
36c2d 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47  beAddOp0(v, OP_G
36c2e 6f 74 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  oto);.    sqlite
36c2f 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
36c30 20 61 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   a1);.  }else{. 
36c31 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
36c32 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
36c33 50 5f 52 6f 77 53 65 74 52 65 61 64 2c 20 72 65  P_RowSetRead, re
36c34 67 52 6f 77 53 65 74 2c 20 30 2c 20 72 65 67 4f  gRowSet, 0, regO
36c35 6c 64 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 0a 20  ldRowid);.  }.. 
36c36 20 2f 2a 20 4d 61 6b 65 20 63 75 72 73 6f 72 20   /* Make cursor 
36c37 69 43 75 72 20 70 6f 69 6e 74 20 74 6f 20 74 68  iCur point to th
36c38 65 20 72 65 63 6f 72 64 20 74 68 61 74 20 69 73  e record that is
36c39 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20   being updated. 
36c3a 49 66 0a 20 20 2a 2a 20 74 68 69 73 20 72 65 63  If.  ** this rec
36c3b 6f 72 64 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ord does not exi
36c3c 73 74 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73  st for some reas
36c3d 6f 6e 20 28 64 65 6c 65 74 65 64 20 62 79 20 61  on (deleted by a
36c3e 20 74 72 69 67 67 65 72 2c 0a 20 20 2a 2a 20 66   trigger,.  ** f
36c3f 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 6e  or example, then
36c40 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 6e 65 78   jump to the nex
36c41 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74  t iteration of t
36c42 68 65 20 52 6f 77 53 65 74 20 6c 6f 6f 70 2e 20  he RowSet loop. 
36c43 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
36c44 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
36c45 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 61  tExists, iCur, a
36c46 64 64 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  ddr, regOldRowid
36c47 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
36c48 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69  record number wi
36c49 6c 6c 20 63 68 61 6e 67 65 2c 20 73 65 74 20 72  ll change, set r
36c4a 65 67 69 73 74 65 72 20 72 65 67 4e 65 77 52 6f  egister regNewRo
36c4b 77 69 64 20 74 6f 0a 20 20 2a 2a 20 63 6f 6e 74  wid to.  ** cont
36c4c 61 69 6e 20 74 68 65 20 6e 65 77 20 76 61 6c 75  ain the new valu
36c4d 65 2e 20 49 66 20 74 68 65 20 72 65 63 6f 72 64  e. If the record
36c4e 20 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 62   number is not b
36c4f 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2c 0a 20  eing modified,. 
36c50 20 2a 2a 20 74 68 65 6e 20 72 65 67 4e 65 77 52   ** then regNewR
36c51 6f 77 69 64 20 69 73 20 74 68 65 20 73 61 6d 65  owid is the same
36c52 20 72 65 67 69 73 74 65 72 20 61 73 20 72 65 67   register as reg
36c53 4f 6c 64 52 6f 77 69 64 2c 20 77 68 69 63 68 20  OldRowid, which 
36c54 69 73 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20  is.  ** already 
36c55 70 6f 70 75 6c 61 74 65 64 2e 20 20 2a 2f 0a 20  populated.  */. 
36c56 20 61 73 73 65 72 74 28 20 63 68 6e 67 52 6f 77   assert( chngRow
36c57 69 64 20 7c 7c 20 70 54 72 69 67 67 65 72 20 7c  id || pTrigger |
36c58 7c 20 68 61 73 46 4b 20 7c 7c 20 72 65 67 4f 6c  | hasFK || regOl
36c59 64 52 6f 77 69 64 3d 3d 72 65 67 4e 65 77 52 6f  dRowid==regNewRo
36c5a 77 69 64 20 29 3b 0a 20 20 69 66 28 20 63 68 6e  wid );.  if( chn
36c5b 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 73 71  gRowid ){.    sq
36c5c 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
36c5d 61 72 73 65 2c 20 70 52 6f 77 69 64 45 78 70 72  arse, pRowidExpr
36c5e 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b 0a  , regNewRowid);.
36c5f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
36c60 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74  ddOp1(v, OP_Must
36c61 42 65 49 6e 74 2c 20 72 65 67 4e 65 77 52 6f 77  BeInt, regNewRow
36c62 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  id);.  }..  /* I
36c63 66 20 74 68 65 72 65 20 61 72 65 20 74 72 69 67  f there are trig
36c64 67 65 72 73 20 6f 6e 20 74 68 69 73 20 74 61 62  gers on this tab
36c65 6c 65 2c 20 70 6f 70 75 6c 61 74 65 20 61 6e 20  le, populate an 
36c66 61 72 72 61 79 20 6f 66 20 72 65 67 69 73 74 65  array of registe
36c67 72 73 20 0a 20 20 2a 2a 20 77 69 74 68 20 74 68  rs .  ** with th
36c68 65 20 72 65 71 75 69 72 65 64 20 6f 6c 64 2e 2a  e required old.*
36c69 20 63 6f 6c 75 6d 6e 20 64 61 74 61 2e 20 20 2a   column data.  *
36c6a 2f 0a 20 20 69 66 28 20 68 61 73 46 4b 20 7c 7c  /.  if( hasFK ||
36c6b 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
36c6c 20 75 33 32 20 6f 6c 64 6d 61 73 6b 20 3d 20 28   u32 oldmask = (
36c6d 68 61 73 46 4b 20 3f 20 73 71 6c 69 74 65 33 46  hasFK ? sqlite3F
36c6e 6b 4f 6c 64 6d 61 73 6b 28 70 50 61 72 73 65 2c  kOldmask(pParse,
36c6f 20 70 54 61 62 29 20 3a 20 30 29 3b 0a 20 20 20   pTab) : 0);.   
36c70 20 6f 6c 64 6d 61 73 6b 20 7c 3d 20 73 71 6c 69   oldmask |= sqli
36c71 74 65 33 54 72 69 67 67 65 72 43 6f 6c 6d 61 73  te3TriggerColmas
36c72 6b 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  k(pParse, .     
36c73 20 20 20 70 54 72 69 67 67 65 72 2c 20 70 43 68     pTrigger, pCh
36c74 61 6e 67 65 73 2c 20 30 2c 20 54 52 49 47 47 45  anges, 0, TRIGGE
36c75 52 5f 42 45 46 4f 52 45 7c 54 52 49 47 47 45 52  R_BEFORE|TRIGGER
36c76 5f 41 46 54 45 52 2c 20 70 54 61 62 2c 20 6f 6e  _AFTER, pTab, on
36c77 45 72 72 6f 72 0a 20 20 20 20 29 3b 0a 20 20 20  Error.    );.   
36c78 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
36c79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
36c7a 20 20 20 20 69 66 28 20 61 58 52 65 66 5b 69 5d      if( aXRef[i]
36c7b 3c 30 20 7c 7c 20 6f 6c 64 6d 61 73 6b 3d 3d 30  <0 || oldmask==0
36c7c 78 66 66 66 66 66 66 66 66 20 7c 7c 20 28 69 3c  xffffffff || (i<
36c7d 33 32 20 26 26 20 28 6f 6c 64 6d 61 73 6b 20 26  32 && (oldmask &
36c7e 20 28 31 3c 3c 69 29 29 29 20 29 7b 0a 20 20 20   (1<<i))) ){.   
36c7f 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
36c80 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 4f 66 54  CodeGetColumnOfT
36c81 61 62 6c 65 28 76 2c 20 70 54 61 62 2c 20 69 43  able(v, pTab, iC
36c82 75 72 2c 20 69 2c 20 72 65 67 4f 6c 64 2b 69 29  ur, i, regOld+i)
36c83 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
36c84 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
36c85 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
36c86 75 6c 6c 2c 20 30 2c 20 72 65 67 4f 6c 64 2b 69  ull, 0, regOld+i
36c87 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
36c88 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77  .    if( chngRow
36c89 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  id==0 ){.      s
36c8a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
36c8b 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 65 67  (v, OP_Copy, reg
36c8c 4f 6c 64 52 6f 77 69 64 2c 20 72 65 67 4e 65 77  OldRowid, regNew
36c8d 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20  Rowid);.    }.  
36c8e 7d 0a 0a 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65  }..  /* Populate
36c8f 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 72 65   the array of re
36c90 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e  gisters beginnin
36c91 67 20 61 74 20 72 65 67 4e 65 77 20 77 69 74 68  g at regNew with
36c92 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 72 6f   the new.  ** ro
36c93 77 20 64 61 74 61 2e 20 54 68 69 73 20 61 72 72  w data. This arr
36c94 61 79 20 69 73 20 75 73 65 64 20 74 6f 20 63 68  ay is used to ch
36c95 65 63 6b 20 63 6f 6e 73 74 61 69 6e 74 73 2c 20  eck constaints, 
36c96 63 72 65 61 74 65 20 74 68 65 20 6e 65 77 0a 20  create the new. 
36c97 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69 6e   ** table and in
36c98 64 65 78 20 72 65 63 6f 72 64 73 2c 20 61 6e 64  dex records, and
36c99 20 61 73 20 74 68 65 20 76 61 6c 75 65 73 20 66   as the values f
36c9a 6f 72 20 61 6e 79 20 6e 65 77 2e 2a 20 72 65 66  or any new.* ref
36c9b 65 72 65 6e 63 65 73 0a 20 20 2a 2a 20 6d 61 64  erences.  ** mad
36c9c 65 20 62 79 20 74 72 69 67 67 65 72 73 2e 0a 20  e by triggers.. 
36c9d 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 72   **.  ** If ther
36c9e 65 20 61 72 65 20 6f 6e 65 20 6f 72 20 6d 6f 72  e are one or mor
36c9f 65 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72  e BEFORE trigger
36ca0 73 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 70  s, then do not p
36ca1 6f 70 75 6c 61 74 65 20 74 68 65 0a 20 20 2a 2a  opulate the.  **
36ca2 20 72 65 67 69 73 74 65 72 73 20 61 73 73 6f 63   registers assoc
36ca3 69 61 74 65 64 20 77 69 74 68 20 63 6f 6c 75 6d  iated with colum
36ca4 6e 73 20 74 68 61 74 20 61 72 65 20 28 61 29 20  ns that are (a) 
36ca5 6e 6f 74 20 6d 6f 64 69 66 69 65 64 20 62 79 0a  not modified by.
36ca6 20 20 2a 2a 20 74 68 69 73 20 55 50 44 41 54 45    ** this UPDATE
36ca7 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 28   statement and (
36ca8 62 29 20 6e 6f 74 20 61 63 63 65 73 73 65 64 20  b) not accessed 
36ca9 62 79 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e  by new.* referen
36caa 63 65 73 2e 20 54 68 65 0a 20 20 2a 2a 20 76 61  ces. The.  ** va
36cab 6c 75 65 73 20 66 6f 72 20 72 65 67 69 73 74 65  lues for registe
36cac 72 73 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64 20  rs not modified 
36cad 62 79 20 74 68 65 20 55 50 44 41 54 45 20 6d 75  by the UPDATE mu
36cae 73 74 20 62 65 20 72 65 6c 6f 61 64 65 64 20 66  st be reloaded f
36caf 72 6f 6d 20 0a 20 20 2a 2a 20 74 68 65 20 64 61  rom .  ** the da
36cb0 74 61 62 61 73 65 20 61 66 74 65 72 20 74 68 65  tabase after the
36cb1 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72 73   BEFORE triggers
36cb2 20 61 72 65 20 66 69 72 65 64 20 61 6e 79 77 61   are fired anywa
36cb3 79 20 28 61 73 20 74 68 65 20 74 72 69 67 67 65  y (as the trigge
36cb4 72 20 0a 20 20 2a 2a 20 6d 61 79 20 68 61 76 65  r .  ** may have
36cb5 20 6d 6f 64 69 66 69 65 64 20 74 68 65 6d 29 2e   modified them).
36cb6 20 53 6f 20 6e 6f 74 20 6c 6f 61 64 69 6e 67 20   So not loading 
36cb7 74 68 6f 73 65 20 74 68 61 74 20 61 72 65 20 6e  those that are n
36cb8 6f 74 20 67 6f 69 6e 67 20 74 6f 0a 20 20 2a 2a  ot going to.  **
36cb9 20 62 65 20 75 73 65 64 20 65 6c 69 6d 69 6e 61   be used elimina
36cba 74 65 73 20 73 6f 6d 65 20 72 65 64 75 6e 64 61  tes some redunda
36cbb 6e 74 20 6f 70 63 6f 64 65 73 2e 0a 20 20 2a 2f  nt opcodes..  */
36cbc 0a 20 20 6e 65 77 6d 61 73 6b 20 3d 20 73 71 6c  .  newmask = sql
36cbd 69 74 65 33 54 72 69 67 67 65 72 43 6f 6c 6d 61  ite3TriggerColma
36cbe 73 6b 28 0a 20 20 20 20 20 20 70 50 61 72 73 65  sk(.      pParse
36cbf 2c 20 70 54 72 69 67 67 65 72 2c 20 70 43 68 61  , pTrigger, pCha
36cc0 6e 67 65 73 2c 20 31 2c 20 54 52 49 47 47 45 52  nges, 1, TRIGGER
36cc1 5f 42 45 46 4f 52 45 2c 20 70 54 61 62 2c 20 6f  _BEFORE, pTab, o
36cc2 6e 45 72 72 6f 72 0a 20 20 29 3b 0a 20 20 73 71  nError.  );.  sq
36cc3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
36cc4 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72  v, OP_Null, 0, r
36cc5 65 67 4e 65 77 2c 20 72 65 67 4e 65 77 2b 70 54  egNew, regNew+pT
36cc6 61 62 2d 3e 6e 43 6f 6c 2d 31 29 3b 0a 20 20 66  ab->nCol-1);.  f
36cc7 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e  or(i=0; i<pTab->
36cc8 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
36cc9 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b  if( i==pTab->iPK
36cca 65 79 20 29 7b 0a 20 20 20 20 20 20 2f 2a 73 71  ey ){.      /*sq
36ccb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
36ccc 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72  v, OP_Null, 0, r
36ccd 65 67 4e 65 77 2b 69 29 3b 2a 2f 0a 20 20 20 20  egNew+i);*/.    
36cce 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6a 20 3d  }else{.      j =
36ccf 20 61 58 52 65 66 5b 69 5d 3b 0a 20 20 20 20 20   aXRef[i];.     
36cd0 20 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20   if( j>=0 ){.   
36cd1 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
36cd2 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 43 68  Code(pParse, pCh
36cd3 61 6e 67 65 73 2d 3e 61 5b 6a 5d 2e 70 45 78 70  anges->a[j].pExp
36cd4 72 2c 20 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20  r, regNew+i);.  
36cd5 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 30 3d      }else if( 0=
36cd6 3d 28 74 6d 61 73 6b 26 54 52 49 47 47 45 52 5f  =(tmask&TRIGGER_
36cd7 42 45 46 4f 52 45 29 20 7c 7c 20 69 3e 33 31 20  BEFORE) || i>31 
36cd8 7c 7c 20 28 6e 65 77 6d 61 73 6b 26 28 31 3c 3c  || (newmask&(1<<
36cd9 69 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  i)) ){.        /
36cda 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 6c 6f  * This branch lo
36cdb 61 64 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ads the value of
36cdc 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 77   a column that w
36cdd 69 6c 6c 20 6e 6f 74 20 62 65 20 63 68 61 6e 67  ill not be chang
36cde 65 64 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  ed .        ** i
36cdf 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20  nto a register. 
36ce0 54 68 69 73 20 69 73 20 64 6f 6e 65 20 69 66 20  This is done if 
36ce1 74 68 65 72 65 20 61 72 65 20 6e 6f 20 42 45 46  there are no BEF
36ce2 4f 52 45 20 74 72 69 67 67 65 72 73 2c 20 6f 72  ORE triggers, or
36ce3 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20 74  .        ** if t
36ce4 68 65 72 65 20 61 72 65 20 6f 6e 65 20 6f 72 20  here are one or 
36ce5 6d 6f 72 65 20 42 45 46 4f 52 45 20 74 72 69 67  more BEFORE trig
36ce6 67 65 72 73 20 74 68 61 74 20 75 73 65 20 74 68  gers that use th
36ce7 69 73 20 76 61 6c 75 65 20 76 69 61 0a 20 20 20  is value via.   
36ce8 20 20 20 20 20 2a 2a 20 61 20 6e 65 77 2e 2a 20       ** a new.* 
36ce9 72 65 66 65 72 65 6e 63 65 20 69 6e 20 61 20 74  reference in a t
36cea 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a  rigger program..
36ceb 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
36cec 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
36ced 33 31 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65  31 );.        te
36cee 73 74 63 61 73 65 28 20 69 3d 3d 33 32 20 29 3b  stcase( i==32 );
36cef 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
36cf0 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
36cf1 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69  _Column, iCur, i
36cf2 2c 20 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20  , regNew+i);.   
36cf3 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75       sqlite3Colu
36cf4 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61  mnDefault(v, pTa
36cf5 62 2c 20 69 2c 20 72 65 67 4e 65 77 2b 69 29 3b  b, i, regNew+i);
36cf6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
36cf7 20 7d 0a 0a 20 20 2f 2a 20 46 69 72 65 20 61 6e   }..  /* Fire an
36cf8 79 20 42 45 46 4f 52 45 20 55 50 44 41 54 45 20  y BEFORE UPDATE 
36cf9 74 72 69 67 67 65 72 73 2e 20 54 68 69 73 20 68  triggers. This h
36cfa 61 70 70 65 6e 73 20 62 65 66 6f 72 65 20 63 6f  appens before co
36cfb 6e 73 74 72 61 69 6e 74 73 20 61 72 65 0a 20 20  nstraints are.  
36cfc 2a 2a 20 76 65 72 69 66 69 65 64 2e 20 4f 6e 65  ** verified. One
36cfd 20 63 6f 75 6c 64 20 61 72 67 75 65 20 74 68 61   could argue tha
36cfe 74 20 74 68 69 73 20 69 73 20 77 72 6f 6e 67 2e  t this is wrong.
36cff 0a 20 20 2a 2f 0a 20 20 69 66 28 20 74 6d 61 73  .  */.  if( tmas
36d00 6b 26 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45  k&TRIGGER_BEFORE
36d01 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
36d02 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
36d03 41 66 66 69 6e 69 74 79 2c 20 72 65 67 4e 65 77  Affinity, regNew
36d04 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20  , pTab->nCol);. 
36d05 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41     sqlite3TableA
36d06 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 54  ffinityStr(v, pT
36d07 61 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ab);.    sqlite3
36d08 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70  CodeRowTrigger(p
36d09 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c  Parse, pTrigger,
36d0a 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68 61   TK_UPDATE, pCha
36d0b 6e 67 65 73 2c 20 0a 20 20 20 20 20 20 20 20 54  nges, .        T
36d0c 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 70  RIGGER_BEFORE, p
36d0d 54 61 62 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  Tab, regOldRowid
36d0e 2c 20 6f 6e 45 72 72 6f 72 2c 20 61 64 64 72 29  , onError, addr)
36d0f 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f  ;..    /* The ro
36d10 77 2d 74 72 69 67 67 65 72 20 6d 61 79 20 68 61  w-trigger may ha
36d11 76 65 20 64 65 6c 65 74 65 64 20 74 68 65 20 72  ve deleted the r
36d12 6f 77 20 62 65 69 6e 67 20 75 70 64 61 74 65 64  ow being updated
36d13 2e 20 49 6e 20 74 68 69 73 0a 20 20 20 20 2a 2a  . In this.    **
36d14 20 63 61 73 65 2c 20 6a 75 6d 70 20 74 6f 20 74   case, jump to t
36d15 68 65 20 6e 65 78 74 20 72 6f 77 2e 20 4e 6f 20  he next row. No 
36d16 75 70 64 61 74 65 73 20 6f 72 20 41 46 54 45 52  updates or AFTER
36d17 20 74 72 69 67 67 65 72 73 20 61 72 65 20 0a 20   triggers are . 
36d18 20 20 20 2a 2a 20 72 65 71 75 69 72 65 64 2e 20     ** required. 
36d19 54 68 69 73 20 62 65 68 61 76 69 6f 75 72 20 2d  This behaviour -
36d1a 20 77 68 61 74 20 68 61 70 70 65 6e 73 20 77 68   what happens wh
36d1b 65 6e 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67  en the row being
36d1c 20 75 70 64 61 74 65 64 0a 20 20 20 20 2a 2a 20   updated.    ** 
36d1d 69 73 20 64 65 6c 65 74 65 64 20 6f 72 20 72 65  is deleted or re
36d1e 6e 61 6d 65 64 20 62 79 20 61 20 42 45 46 4f 52  named by a BEFOR
36d1f 45 20 74 72 69 67 67 65 72 20 2d 20 69 73 20 6c  E trigger - is l
36d20 65 66 74 20 75 6e 64 65 66 69 6e 65 64 20 69 6e  eft undefined in
36d21 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 6f 63 75   the.    ** docu
36d22 6d 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a  mentation..    *
36d23 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
36d24 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
36d25 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 61  tExists, iCur, a
36d26 64 64 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  ddr, regOldRowid
36d27 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 69 74  );..    /* If it
36d28 20 64 69 64 20 6e 6f 74 20 64 65 6c 65 74 65 20   did not delete 
36d29 69 74 2c 20 74 68 65 20 72 6f 77 2d 74 72 69 67  it, the row-trig
36d2a 67 65 72 20 6d 61 79 20 73 74 69 6c 6c 20 68 61  ger may still ha
36d2b 76 65 20 6d 6f 64 69 66 69 65 64 20 0a 20 20 20  ve modified .   
36d2c 20 2a 2a 20 73 6f 6d 65 20 6f 66 20 74 68 65 20   ** some of the 
36d2d 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 72  columns of the r
36d2e 6f 77 20 62 65 69 6e 67 20 75 70 64 61 74 65 64  ow being updated
36d2f 2e 20 4c 6f 61 64 20 74 68 65 20 76 61 6c 75 65  . Load the value
36d30 73 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 61 6c  s for .    ** al
36d31 6c 20 63 6f 6c 75 6d 6e 73 20 6e 6f 74 20 6d 6f  l columns not mo
36d32 64 69 66 69 65 64 20 62 79 20 74 68 65 20 75 70  dified by the up
36d33 64 61 74 65 20 73 74 61 74 65 6d 65 6e 74 20 69  date statement i
36d34 6e 74 6f 20 74 68 65 69 72 20 0a 20 20 20 20 2a  nto their .    *
36d35 2a 20 72 65 67 69 73 74 65 72 73 20 69 6e 20 63  * registers in c
36d36 61 73 65 20 74 68 69 73 20 68 61 73 20 68 61 70  ase this has hap
36d37 70 65 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  pened..    */.  
36d38 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
36d39 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  b->nCol; i++){. 
36d3a 20 20 20 20 20 69 66 28 20 61 58 52 65 66 5b 69       if( aXRef[i
36d3b 5d 3c 30 20 26 26 20 69 21 3d 70 54 61 62 2d 3e  ]<0 && i!=pTab->
36d3c 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20  iPKey ){.       
36d3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
36d3e 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
36d3f 20 69 43 75 72 2c 20 69 2c 20 72 65 67 4e 65 77   iCur, i, regNew
36d40 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  +i);.        sql
36d41 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c  ite3ColumnDefaul
36d42 74 28 76 2c 20 70 54 61 62 2c 20 69 2c 20 72 65  t(v, pTab, i, re
36d43 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 20 20 7d  gNew+i);.      }
36d44 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
36d45 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20  ( !isView ){.   
36d46 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20   int j1;        
36d47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36d48 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6a 75 6d  * Address of jum
36d49 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f  p instruction */
36d4a 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6e 73  ..    /* Do cons
36d4b 74 72 61 69 6e 74 20 63 68 65 63 6b 73 2e 20 2a  traint checks. *
36d4c 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e  /.    sqlite3Gen
36d4d 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
36d4e 68 65 63 6b 73 28 70 50 61 72 73 65 2c 20 70 54  hecks(pParse, pT
36d4f 61 62 2c 20 69 43 75 72 2c 20 72 65 67 4e 65 77  ab, iCur, regNew
36d50 52 6f 77 69 64 2c 0a 20 20 20 20 20 20 20 20 61  Rowid,.        a
36d51 52 65 67 49 64 78 2c 20 28 63 68 6e 67 52 6f 77  RegIdx, (chngRow
36d52 69 64 3f 72 65 67 4f 6c 64 52 6f 77 69 64 3a 30  id?regOldRowid:0
36d53 29 2c 20 31 2c 20 6f 6e 45 72 72 6f 72 2c 20 61  ), 1, onError, a
36d54 64 64 72 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a  ddr, 0);..    /*
36d55 20 44 6f 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e   Do FK constrain
36d56 74 20 63 68 65 63 6b 73 2e 20 2a 2f 0a 20 20 20  t checks. */.   
36d57 20 69 66 28 20 68 61 73 46 4b 20 29 7b 0a 20 20   if( hasFK ){.  
36d58 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43 68 65      sqlite3FkChe
36d59 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ck(pParse, pTab,
36d5a 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 30 29   regOldRowid, 0)
36d5b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
36d5c 44 65 6c 65 74 65 20 74 68 65 20 69 6e 64 65 78  Delete the index
36d5d 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61   entries associa
36d5e 74 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72  ted with the cur
36d5f 72 65 6e 74 20 72 65 63 6f 72 64 2e 20 20 2a 2f  rent record.  */
36d60 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65  .    j1 = sqlite
36d61 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
36d62 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75  P_NotExists, iCu
36d63 72 2c 20 30 2c 20 72 65 67 4f 6c 64 52 6f 77 69  r, 0, regOldRowi
36d64 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47  d);.    sqlite3G
36d65 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44  enerateRowIndexD
36d66 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70 54  elete(pParse, pT
36d67 61 62 2c 20 69 43 75 72 2c 20 61 52 65 67 49 64  ab, iCur, aRegId
36d68 78 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66  x);.  .    /* If
36d69 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 72 65   changing the re
36d6a 63 6f 72 64 20 6e 75 6d 62 65 72 2c 20 64 65 6c  cord number, del
36d6b 65 74 65 20 74 68 65 20 6f 6c 64 20 72 65 63 6f  ete the old reco
36d6c 72 64 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  rd.  */.    if( 
36d6d 68 61 73 46 4b 20 7c 7c 20 63 68 6e 67 52 6f 77  hasFK || chngRow
36d6e 69 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  id ){.      sqli
36d6f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
36d70 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69 43 75 72   OP_Delete, iCur
36d71 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
36d72 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
36d73 65 72 65 28 76 2c 20 6a 31 29 3b 0a 0a 20 20 20  ere(v, j1);..   
36d74 20 69 66 28 20 68 61 73 46 4b 20 29 7b 0a 20 20   if( hasFK ){.  
36d75 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43 68 65      sqlite3FkChe
36d76 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ck(pParse, pTab,
36d77 20 30 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29   0, regNewRowid)
36d78 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  ;.    }.  .    /
36d79 2a 20 49 6e 73 65 72 74 20 74 68 65 20 6e 65 77  * Insert the new
36d7a 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 61   index entries a
36d7b 6e 64 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72  nd the new recor
36d7c 64 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  d. */.    sqlite
36d7d 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69  3CompleteInserti
36d7e 6f 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  on(pParse, pTab,
36d7f 20 69 43 75 72 2c 20 72 65 67 4e 65 77 52 6f 77   iCur, regNewRow
36d80 69 64 2c 20 61 52 65 67 49 64 78 2c 20 31 2c 20  id, aRegIdx, 1, 
36d81 30 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 44  0, 0);..    /* D
36d82 6f 20 61 6e 79 20 4f 4e 20 43 41 53 43 41 44 45  o any ON CASCADE
36d83 2c 20 53 45 54 20 4e 55 4c 4c 20 6f 72 20 53 45  , SET NULL or SE
36d84 54 20 44 45 46 41 55 4c 54 20 6f 70 65 72 61 74  T DEFAULT operat
36d85 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 74 6f  ions required to
36d86 0a 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 20 72  .    ** handle r
36d87 6f 77 73 20 28 70 6f 73 73 69 62 6c 79 20 69 6e  ows (possibly in
36d88 20 6f 74 68 65 72 20 74 61 62 6c 65 73 29 20 74   other tables) t
36d89 68 61 74 20 72 65 66 65 72 20 76 69 61 20 61 20  hat refer via a 
36d8a 66 6f 72 65 69 67 6e 20 6b 65 79 0a 20 20 20 20  foreign key.    
36d8b 2a 2a 20 74 6f 20 74 68 65 20 72 6f 77 20 6a 75  ** to the row ju
36d8c 73 74 20 75 70 64 61 74 65 64 2e 20 2a 2f 20 0a  st updated. */ .
36d8d 20 20 20 20 69 66 28 20 68 61 73 46 4b 20 29 7b      if( hasFK ){
36d8e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 6b  .      sqlite3Fk
36d8f 41 63 74 69 6f 6e 73 28 70 50 61 72 73 65 2c 20  Actions(pParse, 
36d90 70 54 61 62 2c 20 70 43 68 61 6e 67 65 73 2c 20  pTab, pChanges, 
36d91 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20  regOldRowid);.  
36d92 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e    }.  }..  /* In
36d93 63 72 65 6d 65 6e 74 20 74 68 65 20 72 6f 77 20  crement the row 
36d94 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2f 0a 20 20  counter .  */.  
36d95 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26  if( (db->flags &
36d96 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77   SQLITE_CountRow
36d97 73 29 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70  s) && !pParse->p
36d98 54 72 69 67 67 65 72 54 61 62 29 7b 0a 20 20 20  TriggerTab){.   
36d99 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
36d9a 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
36d9b 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29   regRowCount, 1)
36d9c 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
36d9d 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70  CodeRowTrigger(p
36d9e 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c  Parse, pTrigger,
36d9f 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68 61   TK_UPDATE, pCha
36da0 6e 67 65 73 2c 20 0a 20 20 20 20 20 20 54 52 49  nges, .      TRI
36da1 47 47 45 52 5f 41 46 54 45 52 2c 20 70 54 61 62  GGER_AFTER, pTab
36da2 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 6f  , regOldRowid, o
36da3 6e 45 72 72 6f 72 2c 20 61 64 64 72 29 3b 0a 0a  nError, addr);..
36da4 20 20 2f 2a 20 52 65 70 65 61 74 20 74 68 65 20    /* Repeat the 
36da5 61 62 6f 76 65 20 77 69 74 68 20 74 68 65 20 6e  above with the n
36da6 65 78 74 20 72 65 63 6f 72 64 20 74 6f 20 62 65  ext record to be
36da7 20 75 70 64 61 74 65 64 2c 20 75 6e 74 69 6c 0a   updated, until.
36da8 20 20 2a 2a 20 61 6c 6c 20 72 65 63 6f 72 64 20    ** all record 
36da9 73 65 6c 65 63 74 65 64 20 62 79 20 74 68 65 20  selected by the 
36daa 57 48 45 52 45 20 63 6c 61 75 73 65 20 68 61 76  WHERE clause hav
36dab 65 20 62 65 65 6e 20 75 70 64 61 74 65 64 2e 0a  e been updated..
36dac 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
36dad 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
36dae 6f 74 6f 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20  oto, 0, addr);. 
36daf 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
36db0 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a  Here(v, addr);..
36db1 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74    /* Close all t
36db2 61 62 6c 65 73 20 2a 2f 0a 20 20 66 6f 72 28 69  ables */.  for(i
36db3 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70  =0, pIdx=pTab->p
36db4 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
36db5 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69  x=pIdx->pNext, i
36db6 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ++){.    assert(
36db7 20 61 52 65 67 49 64 78 20 29 3b 0a 20 20 20 20   aRegIdx );.    
36db8 69 66 28 20 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61  if( openAll || a
36db9 52 65 67 49 64 78 5b 69 5d 3e 30 20 29 7b 0a 20  RegIdx[i]>0 ){. 
36dba 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
36dbb 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
36dbc 73 65 2c 20 69 43 75 72 2b 69 2b 31 2c 20 30 29  se, iCur+i+1, 0)
36dbd 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
36dbe 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
36dbf 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75  v, OP_Close, iCu
36dc0 72 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 55 70 64  r, 0);..  /* Upd
36dc1 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73  ate the sqlite_s
36dc2 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 62 79  equence table by
36dc3 20 73 74 6f 72 69 6e 67 20 74 68 65 20 63 6f 6e   storing the con
36dc4 74 65 6e 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a  tent of the.  **
36dc5 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 63   maximum rowid c
36dc6 6f 75 6e 74 65 72 20 76 61 6c 75 65 73 20 72 65  ounter values re
36dc7 63 6f 72 64 65 64 20 77 68 69 6c 65 20 69 6e 73  corded while ins
36dc8 65 72 74 69 6e 67 20 69 6e 74 6f 0a 20 20 2a 2a  erting into.  **
36dc9 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 74   autoincrement t
36dca 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  ables..  */.  if
36dcb 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  ( pParse->nested
36dcc 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 70  ==0 && pParse->p
36dcd 54 72 69 67 67 65 72 54 61 62 3d 3d 30 20 29 7b  TriggerTab==0 ){
36dce 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 6f  .    sqlite3Auto
36dcf 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 70 50 61  incrementEnd(pPa
36dd0 72 73 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a  rse);.  }..  /*.
36dd1 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20    ** Return the 
36dd2 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74  number of rows t
36dd3 68 61 74 20 77 65 72 65 20 63 68 61 6e 67 65 64  hat were changed
36dd4 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e  . If this routin
36dd5 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72  e is .  ** gener
36dd6 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63 61 75  ating code becau
36dd7 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20  se of a call to 
36dd8 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
36dd9 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a  se(), do not.  *
36dda 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c  * invoke the cal
36ddb 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a  lback function..
36ddc 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e    */.  if( (db->
36ddd 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75  flags&SQLITE_Cou
36dde 6e 74 52 6f 77 73 29 20 26 26 20 21 70 50 61 72  ntRows) && !pPar
36ddf 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20  se->pTriggerTab 
36de0 26 26 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74  && !pParse->nest
36de1 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ed ){.    sqlite
36de2 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
36de3 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72 65 67  P_ResultRow, reg
36de4 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20  RowCount, 1);.  
36de5 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
36de6 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20  NumCols(v, 1);. 
36de7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
36de8 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
36de9 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f  OLNAME_NAME, "ro
36dea 77 73 20 75 70 64 61 74 65 64 22 2c 20 53 51 4c  ws updated", SQL
36deb 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d  ITE_STATIC);.  }
36dec 0a 0a 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70  ..update_cleanup
36ded 3a 0a 20 20 73 71 6c 69 74 65 33 41 75 74 68 43  :.  sqlite3AuthC
36dee 6f 6e 74 65 78 74 50 6f 70 28 26 73 43 6f 6e 74  ontextPop(&sCont
36def 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ext);.  sqlite3D
36df0 62 46 72 65 65 28 64 62 2c 20 61 52 65 67 49 64  bFree(db, aRegId
36df1 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  x);.  sqlite3DbF
36df2 72 65 65 28 64 62 2c 20 61 58 52 65 66 29 3b 0a  ree(db, aXRef);.
36df3 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
36df4 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 4c  Delete(db, pTabL
36df5 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ist);.  sqlite3E
36df6 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
36df7 2c 20 70 43 68 61 6e 67 65 73 29 3b 0a 20 20 73  , pChanges);.  s
36df8 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
36df9 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  (db, pWhere);.  
36dfa 72 65 74 75 72 6e 3b 0a 7d 0a 2f 2a 20 4d 61 6b  return;.}./* Mak
36dfb 65 20 73 75 72 65 20 22 69 73 56 69 65 77 22 20  e sure "isView" 
36dfc 61 6e 64 20 6f 74 68 65 72 20 6d 61 63 72 6f 73  and other macros
36dfd 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 20 61   defined above a
36dfe 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 20 4f 74  re undefined. Ot
36dff 68 65 72 77 69 73 65 0a 2a 2a 20 74 68 65 6c 79  herwise.** thely
36e00 20 6d 61 79 20 69 6e 74 65 72 66 65 72 65 20 77   may interfere w
36e01 69 74 68 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20  ith compilation 
36e02 6f 66 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f  of other functio
36e03 6e 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 0a  ns in this file.
36e04 2a 2a 20 28 6f 72 20 69 6e 20 61 6e 6f 74 68 65  ** (or in anothe
36e05 72 20 66 69 6c 65 2c 20 69 66 20 74 68 69 73 20  r file, if this 
36e06 66 69 6c 65 20 62 65 63 6f 6d 65 73 20 70 61 72  file becomes par
36e07 74 20 6f 66 20 74 68 65 20 61 6d 61 6c 67 61 6d  t of the amalgam
36e08 61 74 69 6f 6e 29 2e 20 20 2a 2f 0a 23 69 66 64  ation).  */.#ifd
36e09 65 66 20 69 73 56 69 65 77 0a 20 23 75 6e 64 65  ef isView. #unde
36e0a 66 20 69 73 56 69 65 77 0a 23 65 6e 64 69 66 0a  f isView.#endif.
36e0b 23 69 66 64 65 66 20 70 54 72 69 67 67 65 72 0a  #ifdef pTrigger.
36e0c 20 23 75 6e 64 65 66 20 70 54 72 69 67 67 65 72   #undef pTrigger
36e0d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
36e0e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
36e0f 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20  TUALTABLE./*.** 
36e10 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
36e11 72 20 61 6e 20 55 50 44 41 54 45 20 6f 66 20 61  r an UPDATE of a
36e12 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a   virtual table..
36e13 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 61 74 65  **.** The strate
36e14 67 79 20 69 73 20 74 68 61 74 20 77 65 20 63 72  gy is that we cr
36e15 65 61 74 65 20 61 6e 20 65 70 68 65 6d 65 72 69  eate an ephemeri
36e16 61 6c 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f  al table that co
36e17 6e 74 61 69 6e 73 0a 2a 2a 20 66 6f 72 20 65 61  ntains.** for ea
36e18 63 68 20 72 6f 77 20 74 6f 20 62 65 20 63 68 61  ch row to be cha
36e19 6e 67 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 41  nged:.**.**   (A
36e1a 29 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20  )  The original 
36e1b 72 6f 77 69 64 20 6f 66 20 74 68 61 74 20 72 6f  rowid of that ro
36e1c 77 2e 0a 2a 2a 20 20 20 28 42 29 20 20 54 68 65  w..**   (B)  The
36e1d 20 72 65 76 69 73 65 64 20 72 6f 77 69 64 20 66   revised rowid f
36e1e 6f 72 20 74 68 65 20 72 6f 77 2e 20 28 6e 6f 74  or the row. (not
36e1f 65 31 29 0a 2a 2a 20 20 20 28 43 29 20 20 54 68  e1).**   (C)  Th
36e20 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 65 76 65  e content of eve
36e21 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  ry column in the
36e22 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 6e   row..**.** Then
36e23 20 77 65 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68   we loop over th
36e24 69 73 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  is ephemeral tab
36e25 6c 65 20 61 6e 64 20 66 6f 72 20 65 61 63 68 20  le and for each 
36e26 72 6f 77 20 69 6e 0a 2a 2a 20 74 68 65 20 65 70  row in.** the ep
36e27 68 65 72 6d 65 72 61 6c 20 74 61 62 6c 65 20 63  hermeral table c
36e28 61 6c 6c 20 56 55 70 64 61 74 65 2e 0a 2a 2a 0a  all VUpdate..**.
36e29 2a 2a 20 57 68 65 6e 20 66 69 6e 69 73 68 65 64  ** When finished
36e2a 2c 20 64 72 6f 70 20 74 68 65 20 65 70 68 65 6d  , drop the ephem
36e2b 65 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  eral table..**.*
36e2c 2a 20 28 6e 6f 74 65 31 29 20 41 63 74 75 61 6c  * (note1) Actual
36e2d 6c 79 2c 20 69 66 20 77 65 20 6b 6e 6f 77 20 69  ly, if we know i
36e2e 6e 20 61 64 76 61 6e 63 65 20 74 68 61 74 20 28  n advance that (
36e2f 41 29 20 69 73 20 61 6c 77 61 79 73 20 74 68 65  A) is always the
36e30 20 73 61 6d 65 0a 2a 2a 20 61 73 20 28 42 29 20   same.** as (B) 
36e31 77 65 20 6f 6e 6c 79 20 73 74 6f 72 65 20 28 41  we only store (A
36e32 29 2c 20 74 68 65 6e 20 64 75 70 6c 69 63 61 74  ), then duplicat
36e33 65 20 28 41 29 20 77 68 65 6e 20 70 75 6c 6c 69  e (A) when pulli
36e34 6e 67 0a 2a 2a 20 69 74 20 6f 75 74 20 6f 66 20  ng.** it out of 
36e35 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61  the ephemeral ta
36e36 62 6c 65 20 62 65 66 6f 72 65 20 63 61 6c 6c 69  ble before calli
36e37 6e 67 20 56 55 70 64 61 74 65 2e 0a 2a 2f 0a 73  ng VUpdate..*/.s
36e38 74 61 74 69 63 20 76 6f 69 64 20 75 70 64 61 74  tatic void updat
36e39 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28 0a 20  eVirtualTable(. 
36e3a 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
36e3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
36e3c 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
36e3d 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c    SrcList *pSrc,
36e3e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
36e3f 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f 20 62  rtual table to b
36e40 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 20 20  e modified */.  
36e41 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
36e42 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74       /* The virt
36e43 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45  ual table */.  E
36e44 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65  xprList *pChange
36e45 73 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d  s,  /* The colum
36e46 6e 73 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20  ns to change in 
36e47 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65  the UPDATE state
36e48 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a  ment */.  Expr *
36e49 70 52 6f 77 69 64 2c 20 20 20 20 20 20 20 20 2f  pRowid,        /
36e4a 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 75 73 65  * Expression use
36e4b 64 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20 74  d to recompute t
36e4c 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e  he rowid */.  in
36e4d 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20 20 20  t *aXRef,       
36e4e 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72     /* Mapping fr
36e4f 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70 54  om columns of pT
36e50 61 62 20 74 6f 20 65 6e 74 72 69 65 73 20 69 6e  ab to entries in
36e51 20 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20 20 45   pChanges */.  E
36e52 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20  xpr *pWhere,    
36e53 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61      /* WHERE cla
36e54 75 73 65 20 6f 66 20 74 68 65 20 55 50 44 41 54  use of the UPDAT
36e55 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  E statement */. 
36e56 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20 20 20   int onError    
36e57 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46        /* ON CONF
36e58 4c 49 43 54 20 73 74 72 61 74 65 67 79 20 2a 2f  LICT strategy */
36e59 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  .){.  Vdbe *v = 
36e5a 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20  pParse->pVdbe;  
36e5b 2f 2a 20 56 69 72 74 75 61 6c 20 6d 61 63 68 69  /* Virtual machi
36e5c 6e 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ne under constru
36e5d 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c  ction */.  ExprL
36e5e 69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 30 3b  ist *pEList = 0;
36e5f 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75       /* The resu
36e60 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45  lt set of the SE
36e61 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a  LECT statement *
36e62 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
36e63 65 63 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  ect = 0;      /*
36e64 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   The SELECT stat
36e65 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20  ement */.  Expr 
36e66 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
36e67 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72       /* Temporar
36e68 79 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  y expression */.
36e69 20 20 69 6e 74 20 65 70 68 65 6d 54 61 62 3b 20    int ephemTab; 
36e6a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
36e6b 61 62 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 65  able holding the
36e6c 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 53   result of the S
36e6d 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69  ELECT */.  int i
36e6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
36e6f 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
36e70 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 64  nter */.  int ad
36e71 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
36e72 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
36e73 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 2a 2f  f top of loop */
36e74 0a 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 20  .  int iReg;    
36e75 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36e76 46 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69  First register i
36e77 6e 20 73 65 74 20 70 61 73 73 65 64 20 74 6f 20  n set passed to 
36e78 4f 50 5f 56 55 70 64 61 74 65 20 2a 2f 0a 20 20  OP_VUpdate */.  
36e79 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
36e7a 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 44 61 74  arse->db; /* Dat
36e7b 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
36e7c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
36e7d 20 2a 70 56 54 61 62 20 3d 20 28 63 6f 6e 73 74   *pVTab = (const
36e7e 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 65   char*)sqlite3Ge
36e7f 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62  tVTable(db, pTab
36e80 29 3b 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  );.  SelectDest 
36e81 64 65 73 74 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73  dest;..  /* Cons
36e82 74 72 75 63 74 20 74 68 65 20 53 45 4c 45 43 54  truct the SELECT
36e83 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
36e84 77 69 6c 6c 20 66 69 6e 64 20 74 68 65 20 6e 65  will find the ne
36e85 77 20 76 61 6c 75 65 73 20 66 6f 72 0a 20 20 2a  w values for.  *
36e86 2a 20 61 6c 6c 20 75 70 64 61 74 65 64 20 72 6f  * all updated ro
36e87 77 73 2e 20 0a 20 20 2a 2f 0a 20 20 70 45 4c 69  ws. .  */.  pELi
36e88 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
36e89 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
36e8a 65 2c 20 30 2c 20 73 71 6c 69 74 65 33 45 78 70  e, 0, sqlite3Exp
36e8b 72 28 64 62 2c 20 54 4b 5f 49 44 2c 20 22 5f 72  r(db, TK_ID, "_r
36e8c 6f 77 69 64 5f 22 29 29 3b 0a 20 20 69 66 28 20  owid_"));.  if( 
36e8d 70 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 70 45  pRowid ){.    pE
36e8e 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
36e8f 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
36e90 72 73 65 2c 20 70 45 4c 69 73 74 2c 0a 20 20 20  rse, pEList,.   
36e91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36e92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36e93 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
36e94 62 2c 20 70 52 6f 77 69 64 2c 20 30 29 29 3b 0a  b, pRowid, 0));.
36e95 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54    }.  assert( pT
36e96 61 62 2d 3e 69 50 4b 65 79 3c 30 20 29 3b 0a 20  ab->iPKey<0 );. 
36e97 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
36e98 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
36e99 20 20 69 66 28 20 61 58 52 65 66 5b 69 5d 3e 3d    if( aXRef[i]>=
36e9a 30 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72  0 ){.      pExpr
36e9b 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
36e9c 70 28 64 62 2c 20 70 43 68 61 6e 67 65 73 2d 3e  p(db, pChanges->
36e9d 61 5b 61 58 52 65 66 5b 69 5d 5d 2e 70 45 78 70  a[aXRef[i]].pExp
36e9e 72 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  r, 0);.    }else
36e9f 7b 0a 20 20 20 20 20 20 70 45 78 70 72 20 3d 20  {.      pExpr = 
36ea0 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20  sqlite3Expr(db, 
36ea1 54 4b 5f 49 44 2c 20 70 54 61 62 2d 3e 61 43 6f  TK_ID, pTab->aCo
36ea2 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20  l[i].zName);.   
36ea3 20 7d 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20   }.    pEList = 
36ea4 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
36ea5 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 45  ppend(pParse, pE
36ea6 4c 69 73 74 2c 20 70 45 78 70 72 29 3b 0a 20 20  List, pExpr);.  
36ea7 7d 0a 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71  }.  pSelect = sq
36ea8 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70  lite3SelectNew(p
36ea9 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70  Parse, pEList, p
36eaa 53 72 63 2c 20 70 57 68 65 72 65 2c 20 30 2c 20  Src, pWhere, 0, 
36eab 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  0, 0, 0, 0, 0);.
36eac 20 20 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74    .  /* Create t
36ead 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  he ephemeral tab
36eae 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 68  le into which th
36eaf 65 20 75 70 64 61 74 65 20 72 65 73 75 6c 74 73  e update results
36eb0 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 73 74   will.  ** be st
36eb1 6f 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  ored..  */.  ass
36eb2 65 72 74 28 20 76 20 29 3b 0a 20 20 65 70 68 65  ert( v );.  ephe
36eb3 6d 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e  mTab = pParse->n
36eb4 54 61 62 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33  Tab++;.  sqlite3
36eb5 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
36eb6 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20  _OpenEphemeral, 
36eb7 65 70 68 65 6d 54 61 62 2c 20 70 54 61 62 2d 3e  ephemTab, pTab->
36eb8 6e 43 6f 6c 2b 31 2b 28 70 52 6f 77 69 64 21 3d  nCol+1+(pRowid!=
36eb9 30 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  0));.  sqlite3Vd
36eba 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 42 54  beChangeP5(v, BT
36ebb 52 45 45 5f 55 4e 4f 52 44 45 52 45 44 29 3b 0a  REE_UNORDERED);.
36ebc 0a 20 20 2f 2a 20 66 69 6c 6c 20 74 68 65 20 65  .  /* fill the e
36ebd 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 0a  phemeral table .
36ebe 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 65    */.  sqlite3Se
36ebf 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65  lectDestInit(&de
36ec0 73 74 2c 20 53 52 54 5f 54 61 62 6c 65 2c 20 65  st, SRT_Table, e
36ec1 70 68 65 6d 54 61 62 29 3b 0a 20 20 73 71 6c 69  phemTab);.  sqli
36ec2 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
36ec3 2c 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74  , pSelect, &dest
36ec4 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  );..  /* Generat
36ec5 65 20 63 6f 64 65 20 74 6f 20 73 63 61 6e 20 74  e code to scan t
36ec6 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  he ephemeral tab
36ec7 6c 65 20 61 6e 64 20 63 61 6c 6c 20 56 55 70 64  le and call VUpd
36ec8 61 74 65 2e 20 2a 2f 0a 20 20 69 52 65 67 20 3d  ate. */.  iReg =
36ec9 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
36eca 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20  .  pParse->nMem 
36ecb 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 3b  += pTab->nCol+1;
36ecc 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65  .  addr = sqlite
36ecd 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
36ece 50 5f 52 65 77 69 6e 64 2c 20 65 70 68 65 6d 54  P_Rewind, ephemT
36ecf 61 62 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  ab, 0);.  sqlite
36ed0 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
36ed1 50 5f 43 6f 6c 75 6d 6e 2c 20 20 65 70 68 65 6d  P_Column,  ephem
36ed2 54 61 62 2c 20 30 2c 20 69 52 65 67 29 3b 0a 20  Tab, 0, iReg);. 
36ed3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
36ed4 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
36ed5 20 65 70 68 65 6d 54 61 62 2c 20 28 70 52 6f 77   ephemTab, (pRow
36ed6 69 64 3f 31 3a 30 29 2c 20 69 52 65 67 2b 31 29  id?1:0), iReg+1)
36ed7 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
36ed8 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Tab->nCol; i++){
36ed9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
36eda 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
36edb 75 6d 6e 2c 20 65 70 68 65 6d 54 61 62 2c 20 69  umn, ephemTab, i
36edc 2b 31 2b 28 70 52 6f 77 69 64 21 3d 30 29 2c 20  +1+(pRowid!=0), 
36edd 69 52 65 67 2b 32 2b 69 29 3b 0a 20 20 7d 0a 20  iReg+2+i);.  }. 
36ede 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65   sqlite3VtabMake
36edf 57 72 69 74 61 62 6c 65 28 70 50 61 72 73 65 2c  Writable(pParse,
36ee0 20 70 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65   pTab);.  sqlite
36ee1 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
36ee2 50 5f 56 55 70 64 61 74 65 2c 20 30 2c 20 70 54  P_VUpdate, 0, pT
36ee3 61 62 2d 3e 6e 43 6f 6c 2b 32 2c 20 69 52 65 67  ab->nCol+2, iReg
36ee4 2c 20 70 56 54 61 62 2c 20 50 34 5f 56 54 41 42  , pVTab, P4_VTAB
36ee5 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
36ee6 43 68 61 6e 67 65 50 35 28 76 2c 20 6f 6e 45 72  ChangeP5(v, onEr
36ee7 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror==OE_Default 
36ee8 3f 20 4f 45 5f 41 62 6f 72 74 20 3a 20 6f 6e 45  ? OE_Abort : onE
36ee9 72 72 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  rror);.  sqlite3
36eea 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 29  MayAbort(pParse)
36eeb 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
36eec 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74  ddOp2(v, OP_Next
36eed 2c 20 65 70 68 65 6d 54 61 62 2c 20 61 64 64 72  , ephemTab, addr
36eee 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  +1);.  sqlite3Vd
36eef 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
36ef0 64 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  dr);.  sqlite3Vd
36ef1 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
36ef2 6c 6f 73 65 2c 20 65 70 68 65 6d 54 61 62 2c 20  lose, ephemTab, 
36ef3 30 29 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 75  0);..  /* Cleanu
36ef4 70 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 65  p */.  sqlite3Se
36ef5 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
36ef6 53 65 6c 65 63 74 29 3b 20 20 0a 7d 0a 23 65 6e  Select);  .}.#en
36ef7 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
36ef8 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
36ef9 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
36efa 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 70 64 61 74  *** End of updat
36efb 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
36efc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36efd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36efe 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
36eff 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76  *** Begin file v
36f00 61 63 75 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  acuum.c ********
36f01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36f02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36f03 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41  **/./*.** 2003 A
36f04 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65  pril 6.**.** The
36f05 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
36f06 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
36f07 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
36f08 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
36f09 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
36f0a 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
36f0b 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
36f0c 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
36f0d 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
36f0e 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
36f0f 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
36f10 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
36f11 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
36f12 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
36f13 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
36f14 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
36f15 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
36f16 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36f17 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36f18 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36f19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36f1a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
36f1b 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
36f1c 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
36f1d 6e 74 20 74 68 65 20 56 41 43 55 55 4d 20 63 6f  nt the VACUUM co
36f1e 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73  mmand..**.** Mos
36f1f 74 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e  t of the code in
36f20 20 74 68 69 73 20 66 69 6c 65 20 6d 61 79 20 62   this file may b
36f21 65 20 6f 6d 69 74 74 65 64 20 62 79 20 64 65 66  e omitted by def
36f22 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c  ining the.** SQL
36f23 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20  ITE_OMIT_VACUUM 
36f24 6d 61 63 72 6f 2e 0a 2a 2f 0a 0a 23 69 66 20 21  macro..*/..#if !
36f25 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
36f26 4d 49 54 5f 56 41 43 55 55 4d 29 20 26 26 20 21  MIT_VACUUM) && !
36f27 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
36f28 4d 49 54 5f 41 54 54 41 43 48 29 0a 2f 2a 0a 2a  MIT_ATTACH)./*.*
36f29 2a 20 46 69 6e 61 6c 69 7a 65 20 61 20 70 72 65  * Finalize a pre
36f2a 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e  pared statement.
36f2b 20 20 49 66 20 74 68 65 72 65 20 77 61 73 20 61    If there was a
36f2c 6e 20 65 72 72 6f 72 2c 20 73 74 6f 72 65 20 74  n error, store t
36f2d 68 65 0a 2a 2a 20 74 65 78 74 20 6f 66 20 74 68  he.** text of th
36f2e 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
36f2f 69 6e 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20 52  in *pzErrMsg.  R
36f30 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74  eturn the result
36f31 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   code..*/.static
36f32 20 69 6e 74 20 76 61 63 75 75 6d 46 69 6e 61 6c   int vacuumFinal
36f33 69 7a 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ize(sqlite3 *db,
36f34 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
36f35 53 74 6d 74 2c 20 63 68 61 72 20 2a 2a 70 7a 45  Stmt, char **pzE
36f36 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20 72 63  rrMsg){.  int rc
36f37 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
36f38 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64  VdbeFinalize((Vd
36f39 62 65 2a 29 70 53 74 6d 74 29 3b 0a 20 20 69 66  be*)pStmt);.  if
36f3a 28 20 72 63 20 29 7b 0a 20 20 20 20 73 71 6c 69  ( rc ){.    sqli
36f3b 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45  te3SetString(pzE
36f3c 72 72 4d 73 67 2c 20 64 62 2c 20 73 71 6c 69 74  rrMsg, db, sqlit
36f3d 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a  e3_errmsg(db));.
36f3e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
36f3f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74  .}../*.** Execut
36f40 65 20 7a 53 71 6c 20 6f 6e 20 64 61 74 61 62 61  e zSql on databa
36f41 73 65 20 64 62 2e 20 52 65 74 75 72 6e 20 61 6e  se db. Return an
36f42 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a   error code..*/.
36f43 73 74 61 74 69 63 20 69 6e 74 20 65 78 65 63 53  static int execS
36f44 71 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ql(sqlite3 *db, 
36f45 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c  char **pzErrMsg,
36f46 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
36f47 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  l){.  sqlite3_st
36f48 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 56 56 41  mt *pStmt;.  VVA
36f49 5f 4f 4e 4c 59 28 20 69 6e 74 20 72 63 3b 20 29  _ONLY( int rc; )
36f4a 0a 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a  .  if( !zSql ){.
36f4b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
36f4c 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69  E_NOMEM;.  }.  i
36f4d 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
36f4e 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 64 62  lite3_prepare(db
36f4f 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
36f50 6d 74 2c 20 30 29 20 29 7b 0a 20 20 20 20 73 71  mt, 0) ){.    sq
36f51 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70  lite3SetString(p
36f52 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 73 71 6c  zErrMsg, db, sql
36f53 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29  ite3_errmsg(db))
36f54 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  ;.    return sql
36f55 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64 62 29  ite3_errcode(db)
36f56 3b 0a 20 20 7d 0a 20 20 56 56 41 5f 4f 4e 4c 59  ;.  }.  VVA_ONLY
36f57 28 20 72 63 20 3d 20 29 20 73 71 6c 69 74 65 33  ( rc = ) sqlite3
36f58 5f 73 74 65 70 28 70 53 74 6d 74 29 3b 0a 20 20  _step(pStmt);.  
36f59 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
36f5a 54 45 5f 52 4f 57 20 7c 7c 20 28 64 62 2d 3e 66  TE_ROW || (db->f
36f5b 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e  lags&SQLITE_Coun
36f5c 74 52 6f 77 73 29 20 29 3b 0a 20 20 72 65 74 75  tRows) );.  retu
36f5d 72 6e 20 76 61 63 75 75 6d 46 69 6e 61 6c 69 7a  rn vacuumFinaliz
36f5e 65 28 64 62 2c 20 70 53 74 6d 74 2c 20 70 7a 45  e(db, pStmt, pzE
36f5f 72 72 4d 73 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rrMsg);.}../*.**
36f60 20 45 78 65 63 75 74 65 20 7a 53 71 6c 20 6f 6e   Execute zSql on
36f61 20 64 61 74 61 62 61 73 65 20 64 62 2e 20 54 68   database db. Th
36f62 65 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74 75  e statement retu
36f63 72 6e 73 20 65 78 61 63 74 6c 79 0a 2a 2a 20 6f  rns exactly.** o
36f64 6e 65 20 63 6f 6c 75 6d 6e 2e 20 45 78 65 63 75  ne column. Execu
36f65 74 65 20 74 68 69 73 20 61 73 20 53 51 4c 20 6f  te this as SQL o
36f66 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  n the same datab
36f67 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ase..*/.static i
36f68 6e 74 20 65 78 65 63 45 78 65 63 53 71 6c 28 73  nt execExecSql(s
36f69 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72  qlite3 *db, char
36f6a 20 2a 2a 70 7a 45 72 72 4d 73 67 2c 20 63 6f 6e   **pzErrMsg, con
36f6b 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a  st char *zSql){.
36f6c 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
36f6d 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 72 63 3b  pStmt;.  int rc;
36f6e 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
36f6f 5f 70 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71  _prepare(db, zSq
36f70 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30  l, -1, &pStmt, 0
36f71 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
36f72 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
36f73 72 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 53 51  rc;..  while( SQ
36f74 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65  LITE_ROW==sqlite
36f75 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b  3_step(pStmt) ){
36f76 0a 20 20 20 20 72 63 20 3d 20 65 78 65 63 53 71  .    rc = execSq
36f77 6c 28 64 62 2c 20 70 7a 45 72 72 4d 73 67 2c 20  l(db, pzErrMsg, 
36f78 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63  (char*)sqlite3_c
36f79 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74  olumn_text(pStmt
36f7a 2c 20 30 29 29 3b 0a 20 20 20 20 69 66 28 20 72  , 0));.    if( r
36f7b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
36f7c 20 20 20 20 20 20 76 61 63 75 75 6d 46 69 6e 61        vacuumFina
36f7d 6c 69 7a 65 28 64 62 2c 20 70 53 74 6d 74 2c 20  lize(db, pStmt, 
36f7e 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20  pzErrMsg);.     
36f7f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
36f80 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
36f81 76 61 63 75 75 6d 46 69 6e 61 6c 69 7a 65 28 64  vacuumFinalize(d
36f82 62 2c 20 70 53 74 6d 74 2c 20 70 7a 45 72 72 4d  b, pStmt, pzErrM
36f83 73 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  sg);.}../*.** Th
36f84 65 20 6e 6f 6e 2d 73 74 61 6e 64 61 72 64 20 56  e non-standard V
36f85 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20 69 73  ACUUM command is
36f86 20 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20 75   used to clean u
36f87 70 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 0a  p the database,.
36f88 2a 2a 20 63 6f 6c 6c 61 70 73 65 20 66 72 65 65  ** collapse free
36f89 20 73 70 61 63 65 2c 20 65 74 63 2e 20 20 49 74   space, etc.  It
36f8a 20 69 73 20 6d 6f 64 65 6c 6c 65 64 20 61 66 74   is modelled aft
36f8b 65 72 20 74 68 65 20 56 41 43 55 55 4d 20 63 6f  er the VACUUM co
36f8c 6d 6d 61 6e 64 0a 2a 2a 20 69 6e 20 50 6f 73 74  mmand.** in Post
36f8d 67 72 65 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 6e  greSQL..**.** In
36f8e 20 76 65 72 73 69 6f 6e 20 31 2e 30 2e 78 20 6f   version 1.0.x o
36f8f 66 20 53 51 4c 69 74 65 2c 20 74 68 65 20 56 41  f SQLite, the VA
36f90 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20 77 6f 75  CUUM command wou
36f91 6c 64 20 63 61 6c 6c 0a 2a 2a 20 67 64 62 6d 5f  ld call.** gdbm_
36f92 72 65 6f 72 67 61 6e 69 7a 65 28 29 20 6f 6e 20  reorganize() on 
36f93 61 6c 6c 20 74 68 65 20 64 61 74 61 62 61 73 65  all the database
36f94 20 74 61 62 6c 65 73 2e 20 20 42 75 74 20 62 65   tables.  But be
36f95 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74 68 20  ginning.** with 
36f96 32 2e 30 2e 30 2c 20 53 51 4c 69 74 65 20 6e 6f  2.0.0, SQLite no
36f97 20 6c 6f 6e 67 65 72 20 75 73 65 73 20 47 44 42   longer uses GDB
36f98 4d 20 73 6f 20 74 68 69 73 20 63 6f 6d 6d 61 6e  M so this comman
36f99 64 20 68 61 73 0a 2a 2a 20 62 65 63 6f 6d 65 20  d has.** become 
36f9a 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49  a no-op..*/.SQLI
36f9b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
36f9c 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61  sqlite3Vacuum(Pa
36f9d 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
36f9e 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
36f9f 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
36fa0 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20  ;.  if( v ){.   
36fa1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
36fa2 70 32 28 76 2c 20 4f 50 5f 56 61 63 75 75 6d 2c  p2(v, OP_Vacuum,
36fa3 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
36fa4 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65  te3VdbeUsesBtree
36fa5 28 76 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65  (v, 0);.  }.  re
36fa6 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  turn;.}../*.** T
36fa7 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c  his routine impl
36fa8 65 6d 65 6e 74 73 20 74 68 65 20 4f 50 5f 56 61  ements the OP_Va
36fa9 63 75 75 6d 20 6f 70 63 6f 64 65 20 6f 66 20 74  cuum opcode of t
36faa 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49  he VDBE..*/.SQLI
36fab 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
36fac 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75 6d 28  qlite3RunVacuum(
36fad 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c  char **pzErrMsg,
36fae 20 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20   sqlite3 *db){. 
36faf 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
36fb0 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 52 65 74 75  _OK;     /* Retu
36fb1 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 65 72  rn code from ser
36fb2 76 69 63 65 20 72 6f 75 74 69 6e 65 73 20 2a 2f  vice routines */
36fb3 0a 20 20 42 74 72 65 65 20 2a 70 4d 61 69 6e 3b  .  Btree *pMain;
36fb4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
36fb5 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67  e database being
36fb6 20 76 61 63 75 75 6d 65 64 20 2a 2f 0a 20 20 42   vacuumed */.  B
36fb7 74 72 65 65 20 2a 70 54 65 6d 70 3b 20 20 20 20  tree *pTemp;    
36fb8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 65         /* The te
36fb9 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
36fba 20 77 65 20 76 61 63 75 75 6d 20 69 6e 74 6f 20   we vacuum into 
36fbb 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 20  */.  char *zSql 
36fbc 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
36fbd 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 2a  SQL statements *
36fbe 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 5f 66 6c  /.  int saved_fl
36fbf 61 67 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 53  ags;        /* S
36fc0 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68  aved value of th
36fc1 65 20 64 62 2d 3e 66 6c 61 67 73 20 2a 2f 0a 20  e db->flags */. 
36fc2 20 69 6e 74 20 73 61 76 65 64 5f 6e 43 68 61 6e   int saved_nChan
36fc3 67 65 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 65  ge;      /* Save
36fc4 64 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 6e  d value of db->n
36fc5 43 68 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20  Change */.  int 
36fc6 73 61 76 65 64 5f 6e 54 6f 74 61 6c 43 68 61 6e  saved_nTotalChan
36fc7 67 65 3b 20 2f 2a 20 53 61 76 65 64 20 76 61 6c  ge; /* Saved val
36fc8 75 65 20 6f 66 20 64 62 2d 3e 6e 54 6f 74 61 6c  ue of db->nTotal
36fc9 43 68 61 6e 67 65 20 2a 2f 0a 20 20 76 6f 69 64  Change */.  void
36fca 20 28 2a 73 61 76 65 64 5f 78 54 72 61 63 65 29   (*saved_xTrace)
36fcb 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
36fcc 72 2a 29 3b 20 20 2f 2a 20 53 61 76 65 64 20 64  r*);  /* Saved d
36fcd 62 2d 3e 78 54 72 61 63 65 20 2a 2f 0a 20 20 44  b->xTrace */.  D
36fce 62 20 2a 70 44 62 20 3d 20 30 3b 20 20 20 20 20  b *pDb = 0;     
36fcf 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
36fd0 73 65 20 74 6f 20 64 65 74 61 63 68 20 61 74 20  se to detach at 
36fd1 65 6e 64 20 6f 66 20 76 61 63 75 75 6d 20 2a 2f  end of vacuum */
36fd2 0a 20 20 69 6e 74 20 69 73 4d 65 6d 44 62 3b 20  .  int isMemDb; 
36fd3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
36fd4 75 65 20 69 66 20 76 61 63 75 75 6d 69 6e 67 20  ue if vacuuming 
36fd5 61 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61 62  a :memory: datab
36fd6 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  ase */.  int nRe
36fd7 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
36fd8 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 72 65 73   /* Bytes of res
36fd9 65 72 76 65 64 20 73 70 61 63 65 20 61 74 20 74  erved space at t
36fda 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70  he end of each p
36fdb 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62  age */.  int nDb
36fdc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
36fdd 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 74   /* Number of at
36fde 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
36fdf 20 2a 2f 0a 0a 20 20 69 66 28 20 21 64 62 2d 3e   */..  if( !db->
36fe0 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20  autoCommit ){.  
36fe1 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
36fe2 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c  ng(pzErrMsg, db,
36fe3 20 22 63 61 6e 6e 6f 74 20 56 41 43 55 55 4d 20   "cannot VACUUM 
36fe4 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 74 72  from within a tr
36fe5 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20  ansaction");.   
36fe6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
36fe7 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20  RROR;.  }.  if( 
36fe8 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
36fe9 74 3e 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  t>1 ){.    sqlit
36fea 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72  e3SetString(pzEr
36feb 72 4d 73 67 2c 20 64 62 2c 22 63 61 6e 6e 6f 74  rMsg, db,"cannot
36fec 20 56 41 43 55 55 4d 20 2d 20 53 51 4c 20 73 74   VACUUM - SQL st
36fed 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67  atements in prog
36fee 72 65 73 73 22 29 3b 0a 20 20 20 20 72 65 74 75  ress");.    retu
36fef 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
36ff0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76 65 20  .  }..  /* Save 
36ff1 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  the current valu
36ff2 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
36ff3 65 20 66 6c 61 67 73 20 73 6f 20 74 68 61 74 20  e flags so that 
36ff4 69 74 20 63 61 6e 20 62 65 20 0a 20 20 2a 2a 20  it can be .  ** 
36ff5 72 65 73 74 6f 72 65 64 20 62 65 66 6f 72 65 20  restored before 
36ff6 72 65 74 75 72 6e 69 6e 67 2e 20 54 68 65 6e 20  returning. Then 
36ff7 73 65 74 20 74 68 65 20 77 72 69 74 61 62 6c 65  set the writable
36ff8 2d 73 63 68 65 6d 61 20 66 6c 61 67 2c 20 61 6e  -schema flag, an
36ff9 64 0a 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 43  d.  ** disable C
36ffa 48 45 43 4b 20 61 6e 64 20 66 6f 72 65 69 67 6e  HECK and foreign
36ffb 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73   key constraints
36ffc 2e 20 20 2a 2f 0a 20 20 73 61 76 65 64 5f 66 6c  .  */.  saved_fl
36ffd 61 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67 73 3b  ags = db->flags;
36ffe 0a 20 20 73 61 76 65 64 5f 6e 43 68 61 6e 67 65  .  saved_nChange
36fff 20 3d 20 64 62 2d 3e 6e 43 68 61 6e 67 65 3b 0a   = db->nChange;.
37000 20 20 73 61 76 65 64 5f 6e 54 6f 74 61 6c 43 68    saved_nTotalCh
37001 61 6e 67 65 20 3d 20 64 62 2d 3e 6e 54 6f 74 61  ange = db->nTota
37002 6c 43 68 61 6e 67 65 3b 0a 20 20 73 61 76 65 64  lChange;.  saved
37003 5f 78 54 72 61 63 65 20 3d 20 64 62 2d 3e 78 54  _xTrace = db->xT
37004 72 61 63 65 3b 0a 20 20 64 62 2d 3e 66 6c 61 67  race;.  db->flag
37005 73 20 7c 3d 20 53 51 4c 49 54 45 5f 57 72 69 74  s |= SQLITE_Writ
37006 65 53 63 68 65 6d 61 20 7c 20 53 51 4c 49 54 45  eSchema | SQLITE
37007 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 20 7c 20  _IgnoreChecks | 
37008 53 51 4c 49 54 45 5f 50 72 65 66 65 72 42 75 69  SQLITE_PreferBui
37009 6c 74 69 6e 3b 0a 20 20 64 62 2d 3e 66 6c 61 67  ltin;.  db->flag
3700a 73 20 26 3d 20 7e 28 53 51 4c 49 54 45 5f 46 6f  s &= ~(SQLITE_Fo
3700b 72 65 69 67 6e 4b 65 79 73 20 7c 20 53 51 4c 49  reignKeys | SQLI
3700c 54 45 5f 52 65 76 65 72 73 65 4f 72 64 65 72 29  TE_ReverseOrder)
3700d 3b 0a 20 20 64 62 2d 3e 78 54 72 61 63 65 20 3d  ;.  db->xTrace =
3700e 20 30 3b 0a 0a 20 20 70 4d 61 69 6e 20 3d 20 64   0;..  pMain = d
3700f 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3b 0a 20  b->aDb[0].pBt;. 
37010 20 69 73 4d 65 6d 44 62 20 3d 20 73 71 6c 69 74   isMemDb = sqlit
37011 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 73  e3PagerIsMemdb(s
37012 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
37013 28 70 4d 61 69 6e 29 29 3b 0a 0a 20 20 2f 2a 20  (pMain));..  /* 
37014 41 74 74 61 63 68 20 74 68 65 20 74 65 6d 70 6f  Attach the tempo
37015 72 61 72 79 20 64 61 74 61 62 61 73 65 20 61 73  rary database as
37016 20 27 76 61 63 75 75 6d 5f 64 62 27 2e 20 54 68   'vacuum_db'. Th
37017 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 70 72  e synchronous pr
37018 61 67 6d 61 0a 20 20 2a 2a 20 63 61 6e 20 62 65  agma.  ** can be
37019 20 73 65 74 20 74 6f 20 27 6f 66 66 27 20 66 6f   set to 'off' fo
3701a 72 20 74 68 69 73 20 66 69 6c 65 2c 20 61 73 20  r this file, as 
3701b 69 74 20 69 73 20 6e 6f 74 20 72 65 63 6f 76 65  it is not recove
3701c 72 65 64 20 69 66 20 61 20 63 72 61 73 68 0a 20  red if a crash. 
3701d 20 2a 2a 20 6f 63 63 75 72 73 20 61 6e 79 77 61   ** occurs anywa
3701e 79 2e 20 54 68 65 20 69 6e 74 65 67 72 69 74 79  y. The integrity
3701f 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
37020 20 69 73 20 6d 61 69 6e 74 61 69 6e 65 64 20 62   is maintained b
37021 79 20 61 0a 20 20 2a 2a 20 28 70 6f 73 73 69 62  y a.  ** (possib
37022 6c 79 20 73 79 6e 63 68 72 6f 6e 6f 75 73 29 20  ly synchronous) 
37023 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e  transaction open
37024 65 64 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64  ed on the main d
37025 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a 20  atabase before. 
37026 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65   ** sqlite3Btree
37027 43 6f 70 79 46 69 6c 65 28 29 20 69 73 20 63 61  CopyFile() is ca
37028 6c 6c 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  lled..  **.  ** 
37029 41 6e 20 6f 70 74 69 6d 69 73 61 74 69 6f 6e 20  An optimisation 
3702a 77 6f 75 6c 64 20 62 65 20 74 6f 20 75 73 65 20  would be to use 
3702b 61 20 6e 6f 6e 2d 6a 6f 75 72 6e 61 6c 65 64 20  a non-journaled 
3702c 70 61 67 65 72 2e 0a 20 20 2a 2a 20 28 4c 61 74  pager..  ** (Lat
3702d 65 72 3a 29 20 49 20 74 72 69 65 64 20 73 65 74  er:) I tried set
3702e 74 69 6e 67 20 22 50 52 41 47 4d 41 20 76 61 63  ting "PRAGMA vac
3702f 75 75 6d 5f 64 62 2e 6a 6f 75 72 6e 61 6c 5f 6d  uum_db.journal_m
37030 6f 64 65 3d 4f 46 46 22 20 62 75 74 0a 20 20 2a  ode=OFF" but.  *
37031 2a 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79 20  * that actually 
37032 6d 61 64 65 20 74 68 65 20 56 41 43 55 55 4d 20  made the VACUUM 
37033 72 75 6e 20 73 6c 6f 77 65 72 2e 20 20 56 65 72  run slower.  Ver
37034 79 20 6c 69 74 74 6c 65 20 6a 6f 75 72 6e 61 6c  y little journal
37035 6c 69 6e 67 0a 20 20 2a 2a 20 61 63 74 75 61 6c  ling.  ** actual
37036 6c 79 20 6f 63 63 75 72 73 20 77 68 65 6e 20 64  ly occurs when d
37037 6f 69 6e 67 20 61 20 76 61 63 75 75 6d 20 73 69  oing a vacuum si
37038 6e 63 65 20 74 68 65 20 76 61 63 75 75 6d 5f 64  nce the vacuum_d
37039 62 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 0a 20  b is initially. 
3703a 20 2a 2a 20 65 6d 70 74 79 2e 20 20 4f 6e 6c 79   ** empty.  Only
3703b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   the journal hea
3703c 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 2e 20  der is written. 
3703d 20 41 70 70 61 72 65 6e 74 6c 79 20 69 74 20 74   Apparently it t
3703e 61 6b 65 73 20 6d 6f 72 65 0a 20 20 2a 2a 20 74  akes more.  ** t
3703f 69 6d 65 20 74 6f 20 70 61 72 73 65 20 61 6e 64  ime to parse and
37040 20 72 75 6e 20 74 68 65 20 50 52 41 47 4d 41 20   run the PRAGMA 
37041 74 6f 20 74 75 72 6e 20 6a 6f 75 72 6e 61 6c 6c  to turn journall
37042 69 6e 67 20 6f 66 66 20 74 68 61 6e 20 69 74 20  ing off than it 
37043 64 6f 65 73 0a 20 20 2a 2a 20 74 6f 20 77 72 69  does.  ** to wri
37044 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  te the journal h
37045 65 61 64 65 72 20 66 69 6c 65 2e 0a 20 20 2a 2f  eader file..  */
37046 0a 20 20 6e 44 62 20 3d 20 64 62 2d 3e 6e 44 62  .  nDb = db->nDb
37047 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 54  ;.  if( sqlite3T
37048 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 64 62 29 20  empInMemory(db) 
37049 29 7b 0a 20 20 20 20 7a 53 71 6c 20 3d 20 22 41  ){.    zSql = "A
3704a 54 54 41 43 48 20 27 3a 6d 65 6d 6f 72 79 3a 27  TTACH ':memory:'
3704b 20 41 53 20 76 61 63 75 75 6d 5f 64 62 3b 22 3b   AS vacuum_db;";
3704c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 53  .  }else{.    zS
3704d 71 6c 20 3d 20 22 41 54 54 41 43 48 20 27 27 20  ql = "ATTACH '' 
3704e 41 53 20 76 61 63 75 75 6d 5f 64 62 3b 22 3b 0a  AS vacuum_db;";.
3704f 20 20 7d 0a 20 20 72 63 20 3d 20 65 78 65 63 53    }.  rc = execS
37050 71 6c 28 64 62 2c 20 70 7a 45 72 72 4d 73 67 2c  ql(db, pzErrMsg,
37051 20 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 64 62   zSql);.  if( db
37052 2d 3e 6e 44 62 3e 6e 44 62 20 29 7b 0a 20 20 20  ->nDb>nDb ){.   
37053 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b   pDb = &db->aDb[
37054 64 62 2d 3e 6e 44 62 2d 31 5d 3b 0a 20 20 20 20  db->nDb-1];.    
37055 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28 70  assert( strcmp(p
37056 44 62 2d 3e 7a 4e 61 6d 65 2c 22 76 61 63 75 75  Db->zName,"vacuu
37057 6d 5f 64 62 22 29 3d 3d 30 20 29 3b 0a 20 20 7d  m_db")==0 );.  }
37058 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
37059 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f  E_OK ) goto end_
3705a 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 70 54 65  of_vacuum;.  pTe
3705b 6d 70 20 3d 20 64 62 2d 3e 61 44 62 5b 64 62 2d  mp = db->aDb[db-
3705c 3e 6e 44 62 2d 31 5d 2e 70 42 74 3b 0a 0a 20 20  >nDb-1].pBt;..  
3705d 2f 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 65  /* The call to e
3705e 78 65 63 53 71 6c 28 29 20 74 6f 20 61 74 74 61  xecSql() to atta
3705f 63 68 20 74 68 65 20 74 65 6d 70 20 64 61 74 61  ch the temp data
37060 62 61 73 65 20 68 61 73 20 6c 65 66 74 20 74 68  base has left th
37061 65 20 66 69 6c 65 0a 20 20 2a 2a 20 6c 6f 63 6b  e file.  ** lock
37062 65 64 20 28 61 73 20 74 68 65 72 65 20 77 61 73  ed (as there was
37063 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 61   more than one a
37064 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 20  ctive statement 
37065 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63  when the transac
37066 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 72 65 61  tion.  ** to rea
37067 64 20 74 68 65 20 73 63 68 65 6d 61 20 77 61 73  d the schema was
37068 20 63 6f 6e 63 6c 75 64 65 64 2e 20 55 6e 6c 6f   concluded. Unlo
37069 63 6b 20 69 74 20 68 65 72 65 20 73 6f 20 74 68  ck it here so th
3706a 61 74 20 74 68 69 73 20 64 6f 65 73 6e 27 74 0a  at this doesn't.
3706b 20 20 2a 2a 20 63 61 75 73 65 20 70 72 6f 62 6c    ** cause probl
3706c 65 6d 73 20 66 6f 72 20 74 68 65 20 63 61 6c 6c  ems for the call
3706d 20 74 6f 20 42 74 72 65 65 53 65 74 50 61 67 65   to BtreeSetPage
3706e 53 69 7a 65 28 29 20 62 65 6c 6f 77 2e 20 20 2a  Size() below.  *
3706f 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  /.  sqlite3Btree
37070 43 6f 6d 6d 69 74 28 70 54 65 6d 70 29 3b 0a 0a  Commit(pTemp);..
37071 20 20 6e 52 65 73 20 3d 20 73 71 6c 69 74 65 33    nRes = sqlite3
37072 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28  BtreeGetReserve(
37073 70 4d 61 69 6e 29 3b 0a 0a 20 20 2f 2a 20 41 20  pMain);..  /* A 
37074 56 41 43 55 55 4d 20 63 61 6e 6e 6f 74 20 63 68  VACUUM cannot ch
37075 61 6e 67 65 20 74 68 65 20 70 61 67 65 73 69 7a  ange the pagesiz
37076 65 20 6f 66 20 61 6e 20 65 6e 63 72 79 70 74 65  e of an encrypte
37077 64 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a 23  d database. */.#
37078 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53  ifdef SQLITE_HAS
37079 5f 43 4f 44 45 43 0a 20 20 69 66 28 20 64 62 2d  _CODEC.  if( db-
3707a 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 20 29 7b  >nextPagesize ){
3707b 0a 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64  .    extern void
3707c 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74   sqlite3CodecGet
3707d 4b 65 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e  Key(sqlite3*, in
3707e 74 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e 74 2a 29  t, void**, int*)
3707f 3b 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a  ;.    int nKey;.
37080 20 20 20 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a      char *zKey;.
37081 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 63      sqlite3Codec
37082 47 65 74 4b 65 79 28 64 62 2c 20 30 2c 20 28 76  GetKey(db, 0, (v
37083 6f 69 64 2a 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b  oid**)&zKey, &nK
37084 65 79 29 3b 0a 20 20 20 20 69 66 28 20 6e 4b 65  ey);.    if( nKe
37085 79 20 29 20 64 62 2d 3e 6e 65 78 74 50 61 67 65  y ) db->nextPage
37086 73 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a 23 65  size = 0;.  }.#e
37087 6e 64 69 66 0a 0a 20 20 72 63 20 3d 20 65 78 65  ndif..  rc = exe
37088 63 53 71 6c 28 64 62 2c 20 70 7a 45 72 72 4d 73  cSql(db, pzErrMs
37089 67 2c 20 22 50 52 41 47 4d 41 20 76 61 63 75 75  g, "PRAGMA vacuu
3708a 6d 5f 64 62 2e 73 79 6e 63 68 72 6f 6e 6f 75 73  m_db.synchronous
3708b 3d 4f 46 46 22 29 3b 0a 20 20 69 66 28 20 72 63  =OFF");.  if( rc
3708c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
3708d 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
3708e 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20  ;..  /* Begin a 
3708f 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20  transaction and 
37090 74 61 6b 65 20 61 6e 20 65 78 63 6c 75 73 69 76  take an exclusiv
37091 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d 61  e lock on the ma
37092 69 6e 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  in database.  **
37093 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 64   file. This is d
37094 6f 6e 65 20 62 65 66 6f 72 65 20 74 68 65 20 73  one before the s
37095 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
37096 67 65 53 69 7a 65 28 70 4d 61 69 6e 29 20 63 61  geSize(pMain) ca
37097 6c 6c 20 62 65 6c 6f 77 2c 0a 20 20 2a 2a 20 74  ll below,.  ** t
37098 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 77 65  o ensure that we
37099 20 64 6f 20 6e 6f 74 20 74 72 79 20 74 6f 20 63   do not try to c
3709a 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 2d 73  hange the page-s
3709b 69 7a 65 20 6f 6e 20 61 20 57 41 4c 20 64 61 74  ize on a WAL dat
3709c 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 72 63  abase..  */.  rc
3709d 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 20 70   = execSql(db, p
3709e 7a 45 72 72 4d 73 67 2c 20 22 42 45 47 49 4e 3b  zErrMsg, "BEGIN;
3709f 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ");.  if( rc!=SQ
370a0 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
370a1 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20  nd_of_vacuum;.  
370a2 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
370a3 65 42 65 67 69 6e 54 72 61 6e 73 28 70 4d 61 69  eBeginTrans(pMai
370a4 6e 2c 20 32 29 3b 0a 20 20 69 66 28 20 72 63 21  n, 2);.  if( rc!
370a5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
370a6 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
370a7 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 74  ..  /* Do not at
370a8 74 65 6d 70 74 20 74 6f 20 63 68 61 6e 67 65 20  tempt to change 
370a9 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 66 6f  the page size fo
370aa 72 20 61 20 57 41 4c 20 64 61 74 61 62 61 73 65  r a WAL database
370ab 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
370ac 33 50 61 67 65 72 47 65 74 4a 6f 75 72 6e 61 6c  3PagerGetJournal
370ad 4d 6f 64 65 28 73 71 6c 69 74 65 33 42 74 72 65  Mode(sqlite3Btre
370ae 65 50 61 67 65 72 28 70 4d 61 69 6e 29 29 0a 20  ePager(pMain)). 
370af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3d 3d                ==
370b2 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
370b3 45 5f 57 41 4c 20 29 7b 0a 20 20 20 20 64 62 2d  E_WAL ){.    db-
370b4 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 20 3d 20  >nextPagesize = 
370b5 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71  0;.  }..  if( sq
370b6 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67  lite3BtreeSetPag
370b7 65 53 69 7a 65 28 70 54 65 6d 70 2c 20 73 71 6c  eSize(pTemp, sql
370b8 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
370b9 53 69 7a 65 28 70 4d 61 69 6e 29 2c 20 6e 52 65  Size(pMain), nRe
370ba 73 2c 20 30 29 0a 20 20 20 7c 7c 20 28 21 69 73  s, 0).   || (!is
370bb 4d 65 6d 44 62 20 26 26 20 73 71 6c 69 74 65 33  MemDb && sqlite3
370bc 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65  BtreeSetPageSize
370bd 28 70 54 65 6d 70 2c 20 64 62 2d 3e 6e 65 78 74  (pTemp, db->next
370be 50 61 67 65 73 69 7a 65 2c 20 6e 52 65 73 2c 20  Pagesize, nRes, 
370bf 30 29 29 0a 20 20 20 7c 7c 20 4e 45 56 45 52 28  0)).   || NEVER(
370c0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
370c1 29 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ).  ){.    rc = 
370c2 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
370c3 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61    goto end_of_va
370c4 63 75 75 6d 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64  cuum;.  }..#ifnd
370c5 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
370c6 55 54 4f 56 41 43 55 55 4d 0a 20 20 73 71 6c 69  UTOVACUUM.  sqli
370c7 74 65 33 42 74 72 65 65 53 65 74 41 75 74 6f 56  te3BtreeSetAutoV
370c8 61 63 75 75 6d 28 70 54 65 6d 70 2c 20 64 62 2d  acuum(pTemp, db-
370c9 3e 6e 65 78 74 41 75 74 6f 76 61 63 3e 3d 30 20  >nextAutovac>=0 
370ca 3f 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61  ? db->nextAutova
370cb 63 20 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20  c :.            
370cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
370ce 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75  qlite3BtreeGetAu
370cf 74 6f 56 61 63 75 75 6d 28 70 4d 61 69 6e 29 29  toVacuum(pMain))
370d0 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 51  ;.#endif..  /* Q
370d1 75 65 72 79 20 74 68 65 20 73 63 68 65 6d 61 20  uery the schema 
370d2 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  of the main data
370d3 62 61 73 65 2e 20 43 72 65 61 74 65 20 61 20 6d  base. Create a m
370d4 69 72 72 6f 72 20 73 63 68 65 6d 61 0a 20 20 2a  irror schema.  *
370d5 2a 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61  * in the tempora
370d6 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a  ry database..  *
370d7 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65  /.  rc = execExe
370d8 63 53 71 6c 28 64 62 2c 20 70 7a 45 72 72 4d 73  cSql(db, pzErrMs
370d9 67 2c 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54  g,.      "SELECT
370da 20 27 43 52 45 41 54 45 20 54 41 42 4c 45 20 76   'CREATE TABLE v
370db 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73 75  acuum_db.' || su
370dc 62 73 74 72 28 73 71 6c 2c 31 34 29 20 22 0a 20  bstr(sql,14) ". 
370dd 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73 71 6c       "  FROM sql
370de 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45  ite_master WHERE
370df 20 74 79 70 65 3d 27 74 61 62 6c 65 27 20 41 4e   type='table' AN
370e0 44 20 6e 61 6d 65 21 3d 27 73 71 6c 69 74 65 5f  D name!='sqlite_
370e1 73 65 71 75 65 6e 63 65 27 22 0a 20 20 20 20 20  sequence'".     
370e2 20 22 20 20 20 41 4e 44 20 72 6f 6f 74 70 61 67   "   AND rootpag
370e3 65 3e 30 22 0a 20 20 29 3b 0a 20 20 69 66 28 20  e>0".  );.  if( 
370e4 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
370e5 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75  goto end_of_vacu
370e6 75 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 45  um;.  rc = execE
370e7 78 65 63 53 71 6c 28 64 62 2c 20 70 7a 45 72 72  xecSql(db, pzErr
370e8 4d 73 67 2c 0a 20 20 20 20 20 20 22 53 45 4c 45  Msg,.      "SELE
370e9 43 54 20 27 43 52 45 41 54 45 20 49 4e 44 45 58  CT 'CREATE INDEX
370ea 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20   vacuum_db.' || 
370eb 73 75 62 73 74 72 28 73 71 6c 2c 31 34 29 22 0a  substr(sql,14)".
370ec 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73 71        "  FROM sq
370ed 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52  lite_master WHER
370ee 45 20 73 71 6c 20 4c 49 4b 45 20 27 43 52 45 41  E sql LIKE 'CREA
370ef 54 45 20 49 4e 44 45 58 20 25 27 20 22 29 3b 0a  TE INDEX %' ");.
370f0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
370f1 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
370f2 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63 20 3d  f_vacuum;.  rc =
370f3 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c   execExecSql(db,
370f4 20 70 7a 45 72 72 4d 73 67 2c 0a 20 20 20 20 20   pzErrMsg,.     
370f5 20 22 53 45 4c 45 43 54 20 27 43 52 45 41 54 45   "SELECT 'CREATE
370f6 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 76 61   UNIQUE INDEX va
370f7 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73 75 62  cuum_db.' || sub
370f8 73 74 72 28 73 71 6c 2c 32 31 29 20 22 0a 20 20  str(sql,21) ".  
370f9 20 20 20 20 22 20 20 46 52 4f 4d 20 73 71 6c 69      "  FROM sqli
370fa 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20  te_master WHERE 
370fb 73 71 6c 20 4c 49 4b 45 20 27 43 52 45 41 54 45  sql LIKE 'CREATE
370fc 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 25 27   UNIQUE INDEX %'
370fd 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ");.  if( rc!=SQ
370fe 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
370ff 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20  nd_of_vacuum;.. 
37100 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
37101 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74   the tables in t
37102 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
37103 2e 20 46 6f 72 20 65 61 63 68 2c 20 64 6f 0a 20  . For each, do. 
37104 20 2a 2a 20 61 6e 20 22 49 4e 53 45 52 54 20 49   ** an "INSERT I
37105 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 78 78  NTO vacuum_db.xx
37106 78 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  x SELECT * FROM 
37107 6d 61 69 6e 2e 78 78 78 3b 22 20 74 6f 20 63 6f  main.xxx;" to co
37108 70 79 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74  py.  ** the cont
37109 65 6e 74 73 20 74 6f 20 74 68 65 20 74 65 6d 70  ents to the temp
3710a 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a  orary database..
3710b 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63    */.  rc = exec
3710c 45 78 65 63 53 71 6c 28 64 62 2c 20 70 7a 45 72  ExecSql(db, pzEr
3710d 72 4d 73 67 2c 0a 20 20 20 20 20 20 22 53 45 4c  rMsg,.      "SEL
3710e 45 43 54 20 27 49 4e 53 45 52 54 20 49 4e 54 4f  ECT 'INSERT INTO
3710f 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20   vacuum_db.' || 
37110 71 75 6f 74 65 28 6e 61 6d 65 29 20 22 0a 20 20  quote(name) ".  
37111 20 20 20 20 22 7c 7c 20 27 20 53 45 4c 45 43 54      "|| ' SELECT
37112 20 2a 20 46 52 4f 4d 20 6d 61 69 6e 2e 27 20 7c   * FROM main.' |
37113 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c  | quote(name) ||
37114 20 27 3b 27 22 0a 20 20 20 20 20 20 22 46 52 4f   ';'".      "FRO
37115 4d 20 6d 61 69 6e 2e 73 71 6c 69 74 65 5f 6d 61  M main.sqlite_ma
37116 73 74 65 72 20 22 0a 20 20 20 20 20 20 22 57 48  ster ".      "WH
37117 45 52 45 20 74 79 70 65 20 3d 20 27 74 61 62 6c  ERE type = 'tabl
37118 65 27 20 41 4e 44 20 6e 61 6d 65 21 3d 27 73 71  e' AND name!='sq
37119 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27 20 22  lite_sequence' "
3711a 0a 20 20 20 20 20 20 22 20 20 41 4e 44 20 72 6f  .      "  AND ro
3711b 6f 74 70 61 67 65 3e 30 22 0a 20 20 29 3b 0a 20  otpage>0".  );. 
3711c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3711d 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
3711e 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 43  _vacuum;..  /* C
3711f 6f 70 79 20 6f 76 65 72 20 74 68 65 20 73 65 71  opy over the seq
37120 75 65 6e 63 65 20 74 61 62 6c 65 0a 20 20 2a 2f  uence table.  */
37121 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65 63  .  rc = execExec
37122 53 71 6c 28 64 62 2c 20 70 7a 45 72 72 4d 73 67  Sql(db, pzErrMsg
37123 2c 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20  ,.      "SELECT 
37124 27 44 45 4c 45 54 45 20 46 52 4f 4d 20 76 61 63  'DELETE FROM vac
37125 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74  uum_db.' || quot
37126 65 28 6e 61 6d 65 29 20 7c 7c 20 27 3b 27 20 22  e(name) || ';' "
37127 0a 20 20 20 20 20 20 22 46 52 4f 4d 20 76 61 63  .      "FROM vac
37128 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61  uum_db.sqlite_ma
37129 73 74 65 72 20 57 48 45 52 45 20 6e 61 6d 65 3d  ster WHERE name=
3712a 27 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65  'sqlite_sequence
3712b 27 20 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72  ' ".  );.  if( r
3712c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
3712d 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
3712e 6d 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78  m;.  rc = execEx
3712f 65 63 53 71 6c 28 64 62 2c 20 70 7a 45 72 72 4d  ecSql(db, pzErrM
37130 73 67 2c 0a 20 20 20 20 20 20 22 53 45 4c 45 43  sg,.      "SELEC
37131 54 20 27 49 4e 53 45 52 54 20 49 4e 54 4f 20 76  T 'INSERT INTO v
37132 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71 75  acuum_db.' || qu
37133 6f 74 65 28 6e 61 6d 65 29 20 22 0a 20 20 20 20  ote(name) ".    
37134 20 20 22 7c 7c 20 27 20 53 45 4c 45 43 54 20 2a    "|| ' SELECT *
37135 20 46 52 4f 4d 20 6d 61 69 6e 2e 27 20 7c 7c 20   FROM main.' || 
37136 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c 20 27  quote(name) || '
37137 3b 27 20 22 0a 20 20 20 20 20 20 22 46 52 4f 4d  ;' ".      "FROM
37138 20 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69 74   vacuum_db.sqlit
37139 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 6e  e_master WHERE n
3713a 61 6d 65 3d 3d 27 73 71 6c 69 74 65 5f 73 65 71  ame=='sqlite_seq
3713b 75 65 6e 63 65 27 3b 22 0a 20 20 29 3b 0a 20 20  uence';".  );.  
3713c 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3713d 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f  K ) goto end_of_
3713e 76 61 63 75 75 6d 3b 0a 0a 0a 20 20 2f 2a 20 43  vacuum;...  /* C
3713f 6f 70 79 20 74 68 65 20 74 72 69 67 67 65 72 73  opy the triggers
37140 2c 20 76 69 65 77 73 2c 20 61 6e 64 20 76 69 72  , views, and vir
37141 74 75 61 6c 20 74 61 62 6c 65 73 20 66 72 6f 6d  tual tables from
37142 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
37143 73 65 0a 20 20 2a 2a 20 6f 76 65 72 20 74 6f 20  se.  ** over to 
37144 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  the temporary da
37145 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66  tabase.  None of
37146 20 74 68 65 73 65 20 6f 62 6a 65 63 74 73 20 68   these objects h
37147 61 73 20 61 6e 79 0a 20 20 2a 2a 20 61 73 73 6f  as any.  ** asso
37148 63 69 61 74 65 64 20 73 74 6f 72 61 67 65 2c 20  ciated storage, 
37149 73 6f 20 61 6c 6c 20 77 65 20 68 61 76 65 20 74  so all we have t
3714a 6f 20 64 6f 20 69 73 20 63 6f 70 79 20 74 68 65  o do is copy the
3714b 69 72 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20  ir entries.  ** 
3714c 66 72 6f 6d 20 74 68 65 20 53 51 4c 49 54 45 5f  from the SQLITE_
3714d 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 0a 20 20  MASTER table..  
3714e 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71  */.  rc = execSq
3714f 6c 28 64 62 2c 20 70 7a 45 72 72 4d 73 67 2c 0a  l(db, pzErrMsg,.
37150 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e        "INSERT IN
37151 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c  TO vacuum_db.sql
37152 69 74 65 5f 6d 61 73 74 65 72 20 22 0a 20 20 20  ite_master ".   
37153 20 20 20 22 20 20 53 45 4c 45 43 54 20 74 79 70     "  SELECT typ
37154 65 2c 20 6e 61 6d 65 2c 20 74 62 6c 5f 6e 61 6d  e, name, tbl_nam
37155 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c  e, rootpage, sql
37156 22 0a 20 20 20 20 20 20 22 20 20 20 20 46 52 4f  ".      "    FRO
37157 4d 20 6d 61 69 6e 2e 73 71 6c 69 74 65 5f 6d 61  M main.sqlite_ma
37158 73 74 65 72 22 0a 20 20 20 20 20 20 22 20 20 20  ster".      "   
37159 57 48 45 52 45 20 74 79 70 65 3d 27 76 69 65 77  WHERE type='view
3715a 27 20 4f 52 20 74 79 70 65 3d 27 74 72 69 67 67  ' OR type='trigg
3715b 65 72 27 22 0a 20 20 20 20 20 20 22 20 20 20 20  er'".      "    
3715c 20 20 4f 52 20 28 74 79 70 65 3d 27 74 61 62 6c    OR (type='tabl
3715d 65 27 20 41 4e 44 20 72 6f 6f 74 70 61 67 65 3d  e' AND rootpage=
3715e 30 29 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72  0)".  );.  if( r
3715f 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f  c ) goto end_of_
37160 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 41 74  vacuum;..  /* At
37161 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65   this point, the
37162 72 65 20 69 73 20 61 20 77 72 69 74 65 20 74 72  re is a write tr
37163 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f  ansaction open o
37164 6e 20 62 6f 74 68 20 74 68 65 20 0a 20 20 2a 2a  n both the .  **
37165 20 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65   vacuum database
37166 20 61 6e 64 20 74 68 65 20 6d 61 69 6e 20 64 61   and the main da
37167 74 61 62 61 73 65 2e 20 41 73 73 75 6d 69 6e 67  tabase. Assuming
37168 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   no error occurs
37169 2c 0a 20 20 2a 2a 20 62 6f 74 68 20 74 72 61 6e  ,.  ** both tran
3716a 73 61 63 74 69 6f 6e 73 20 61 72 65 20 63 6c 6f  sactions are clo
3716b 73 65 64 20 62 79 20 74 68 69 73 20 62 6c 6f 63  sed by this bloc
3716c 6b 20 2d 20 74 68 65 20 6d 61 69 6e 20 64 61 74  k - the main dat
3716d 61 62 61 73 65 0a 20 20 2a 2a 20 74 72 61 6e 73  abase.  ** trans
3716e 61 63 74 69 6f 6e 20 62 79 20 73 71 6c 69 74 65  action by sqlite
3716f 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28 29  3BtreeCopyFile()
37170 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20 62   and the other b
37171 79 20 61 6e 20 65 78 70 6c 69 63 69 74 0a 20 20  y an explicit.  
37172 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  ** call to sqlit
37173 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 29 2e  e3BtreeCommit().
37174 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 33  .  */.  {.    u3
37175 32 20 6d 65 74 61 3b 0a 20 20 20 20 69 6e 74 20  2 meta;.    int 
37176 69 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  i;..    /* This 
37177 61 72 72 61 79 20 64 65 74 65 72 6d 69 6e 65 73  array determines
37178 20 77 68 69 63 68 20 6d 65 74 61 20 6d 65 74 61   which meta meta
37179 20 76 61 6c 75 65 73 20 61 72 65 20 70 72 65 73   values are pres
3717a 65 72 76 65 64 20 69 6e 20 74 68 65 0a 20 20 20  erved in the.   
3717b 20 2a 2a 20 76 61 63 75 75 6d 2e 20 20 45 76 65   ** vacuum.  Eve
3717c 6e 20 65 6e 74 72 69 65 73 20 61 72 65 20 74 68  n entries are th
3717d 65 20 6d 65 74 61 20 76 61 6c 75 65 20 6e 75 6d  e meta value num
3717e 62 65 72 20 61 6e 64 20 6f 64 64 20 65 6e 74 72  ber and odd entr
3717f 69 65 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 61  ies.    ** are a
37180 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20 61  n increment to a
37181 70 70 6c 79 20 74 6f 20 74 68 65 20 6d 65 74 61  pply to the meta
37182 20 76 61 6c 75 65 20 61 66 74 65 72 20 74 68 65   value after the
37183 20 76 61 63 75 75 6d 2e 0a 20 20 20 20 2a 2a 20   vacuum..    ** 
37184 54 68 65 20 69 6e 63 72 65 6d 65 6e 74 20 69 73  The increment is
37185 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 61 73   used to increas
37186 65 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  e the schema coo
37187 6b 69 65 20 73 6f 20 74 68 61 74 20 6f 74 68 65  kie so that othe
37188 72 0a 20 20 20 20 2a 2a 20 63 6f 6e 6e 65 63 74  r.    ** connect
37189 69 6f 6e 73 20 74 6f 20 74 68 65 20 73 61 6d 65  ions to the same
3718a 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 6b   database will k
3718b 6e 6f 77 20 74 6f 20 72 65 72 65 61 64 20 74 68  now to reread th
3718c 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a 2f  e schema..    */
3718d 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
3718e 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
3718f 61 43 6f 70 79 5b 5d 20 3d 20 7b 0a 20 20 20 20  aCopy[] = {.    
37190 20 20 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f     BTREE_SCHEMA_
37191 56 45 52 53 49 4f 4e 2c 20 20 20 20 20 31 2c 20  VERSION,     1, 
37192 20 2f 2a 20 41 64 64 20 6f 6e 65 20 74 6f 20 74   /* Add one to t
37193 68 65 20 6f 6c 64 20 73 63 68 65 6d 61 20 63 6f  he old schema co
37194 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20 20 20 42  okie */.       B
37195 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  TREE_DEFAULT_CAC
37196 48 45 5f 53 49 5a 45 2c 20 30 2c 20 20 2f 2a 20  HE_SIZE, 0,  /* 
37197 50 72 65 73 65 72 76 65 20 74 68 65 20 64 65 66  Preserve the def
37198 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 20  ault page cache 
37199 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20 20 42  size */.       B
3719a 54 52 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49  TREE_TEXT_ENCODI
3719b 4e 47 2c 20 20 20 20 20 20 30 2c 20 20 2f 2a 20  NG,      0,  /* 
3719c 50 72 65 73 65 72 76 65 20 74 68 65 20 74 65 78  Preserve the tex
3719d 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20  t encoding */.  
3719e 20 20 20 20 20 42 54 52 45 45 5f 55 53 45 52 5f       BTREE_USER_
3719f 56 45 52 53 49 4f 4e 2c 20 20 20 20 20 20 20 30  VERSION,       0
371a0 2c 20 20 2f 2a 20 50 72 65 73 65 72 76 65 20 74  ,  /* Preserve t
371a1 68 65 20 75 73 65 72 20 76 65 72 73 69 6f 6e 20  he user version 
371a2 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 61  */.    };..    a
371a3 73 73 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65  ssert( 1==sqlite
371a4 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28  3BtreeIsInTrans(
371a5 70 54 65 6d 70 29 20 29 3b 0a 20 20 20 20 61 73  pTemp) );.    as
371a6 73 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65 33  sert( 1==sqlite3
371a7 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70  BtreeIsInTrans(p
371a8 4d 61 69 6e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a  Main) );..    /*
371a9 20 43 6f 70 79 20 42 74 72 65 65 20 6d 65 74 61   Copy Btree meta
371aa 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 20 20 66   values */.    f
371ab 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53  or(i=0; i<ArrayS
371ac 69 7a 65 28 61 43 6f 70 79 29 3b 20 69 2b 3d 32  ize(aCopy); i+=2
371ad 29 7b 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 4d  ){.      /* GetM
371ae 65 74 61 28 29 20 61 6e 64 20 55 70 64 61 74 65  eta() and Update
371af 4d 65 74 61 28 29 20 63 61 6e 6e 6f 74 20 66 61  Meta() cannot fa
371b0 69 6c 20 69 6e 20 74 68 69 73 20 63 6f 6e 74 65  il in this conte
371b1 78 74 20 62 65 63 61 75 73 65 0a 20 20 20 20 20  xt because.     
371b2 20 2a 2a 20 77 65 20 61 6c 72 65 61 64 79 20 68   ** we already h
371b3 61 76 65 20 70 61 67 65 20 31 20 6c 6f 61 64 65  ave page 1 loade
371b4 64 20 69 6e 74 6f 20 63 61 63 68 65 20 61 6e 64  d into cache and
371b5 20 6d 61 72 6b 65 64 20 64 69 72 74 79 2e 20 2a   marked dirty. *
371b6 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  /.      sqlite3B
371b7 74 72 65 65 47 65 74 4d 65 74 61 28 70 4d 61 69  treeGetMeta(pMai
371b8 6e 2c 20 61 43 6f 70 79 5b 69 5d 2c 20 26 6d 65  n, aCopy[i], &me
371b9 74 61 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  ta);.      rc = 
371ba 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61  sqlite3BtreeUpda
371bb 74 65 4d 65 74 61 28 70 54 65 6d 70 2c 20 61 43  teMeta(pTemp, aC
371bc 6f 70 79 5b 69 5d 2c 20 6d 65 74 61 2b 61 43 6f  opy[i], meta+aCo
371bd 70 79 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 20 20  py[i+1]);.      
371be 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51  if( NEVER(rc!=SQ
371bf 4c 49 54 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20  LITE_OK) ) goto 
371c0 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
371c1 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 73     }..    rc = s
371c2 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46  qlite3BtreeCopyF
371c3 69 6c 65 28 70 4d 61 69 6e 2c 20 70 54 65 6d 70  ile(pMain, pTemp
371c4 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
371c5 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
371c6 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
371c7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
371c8 74 72 65 65 43 6f 6d 6d 69 74 28 70 54 65 6d 70  treeCommit(pTemp
371c9 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
371ca 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
371cb 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 23  end_of_vacuum;.#
371cc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
371cd 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
371ce 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65    sqlite3BtreeSe
371cf 74 41 75 74 6f 56 61 63 75 75 6d 28 70 4d 61 69  tAutoVacuum(pMai
371d0 6e 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  n, sqlite3BtreeG
371d1 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 54 65  etAutoVacuum(pTe
371d2 6d 70 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  mp));.#endif.  }
371d3 0a 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ..  assert( rc==
371d4 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72  SQLITE_OK );.  r
371d5 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
371d6 53 65 74 50 61 67 65 53 69 7a 65 28 70 4d 61 69  SetPageSize(pMai
371d7 6e 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  n, sqlite3BtreeG
371d8 65 74 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70  etPageSize(pTemp
371d9 29 2c 20 6e 52 65 73 2c 31 29 3b 0a 0a 65 6e 64  ), nRes,1);..end
371da 5f 6f 66 5f 76 61 63 75 75 6d 3a 0a 20 20 2f 2a  _of_vacuum:.  /*
371db 20 52 65 73 74 6f 72 65 20 74 68 65 20 6f 72 69   Restore the ori
371dc 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 64  ginal value of d
371dd 62 2d 3e 66 6c 61 67 73 20 2a 2f 0a 20 20 64 62  b->flags */.  db
371de 2d 3e 66 6c 61 67 73 20 3d 20 73 61 76 65 64 5f  ->flags = saved_
371df 66 6c 61 67 73 3b 0a 20 20 64 62 2d 3e 6e 43 68  flags;.  db->nCh
371e0 61 6e 67 65 20 3d 20 73 61 76 65 64 5f 6e 43 68  ange = saved_nCh
371e1 61 6e 67 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74  ange;.  db->nTot
371e2 61 6c 43 68 61 6e 67 65 20 3d 20 73 61 76 65 64  alChange = saved
371e3 5f 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a 20  _nTotalChange;. 
371e4 20 64 62 2d 3e 78 54 72 61 63 65 20 3d 20 73 61   db->xTrace = sa
371e5 76 65 64 5f 78 54 72 61 63 65 3b 0a 20 20 73 71  ved_xTrace;.  sq
371e6 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67  lite3BtreeSetPag
371e7 65 53 69 7a 65 28 70 4d 61 69 6e 2c 20 2d 31 2c  eSize(pMain, -1,
371e8 20 2d 31 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 43   -1, 1);..  /* C
371e9 75 72 72 65 6e 74 6c 79 20 74 68 65 72 65 20 69  urrently there i
371ea 73 20 61 6e 20 53 51 4c 20 6c 65 76 65 6c 20 74  s an SQL level t
371eb 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20  ransaction open 
371ec 6f 6e 20 74 68 65 20 76 61 63 75 75 6d 0a 20 20  on the vacuum.  
371ed 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 4e 6f 20  ** database. No 
371ee 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 20 6f  locks are held o
371ef 6e 20 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65  n any other file
371f0 73 20 28 73 69 6e 63 65 20 74 68 65 20 6d 61 69  s (since the mai
371f1 6e 20 66 69 6c 65 0a 20 20 2a 2a 20 77 61 73 20  n file.  ** was 
371f2 63 6f 6d 6d 69 74 74 65 64 20 61 74 20 74 68 65  committed at the
371f3 20 62 74 72 65 65 20 6c 65 76 65 6c 29 2e 20 53   btree level). S
371f4 6f 20 69 74 20 73 61 66 65 20 74 6f 20 65 6e 64  o it safe to end
371f5 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
371f6 0a 20 20 2a 2a 20 62 79 20 6d 61 6e 75 61 6c 6c  .  ** by manuall
371f7 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 61 75  y setting the au
371f8 74 6f 43 6f 6d 6d 69 74 20 66 6c 61 67 20 74 6f  toCommit flag to
371f9 20 74 72 75 65 20 61 6e 64 20 64 65 74 61 63 68   true and detach
371fa 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 76 61 63  ing the.  ** vac
371fb 75 75 6d 20 64 61 74 61 62 61 73 65 2e 20 54 68  uum database. Th
371fc 65 20 76 61 63 75 75 6d 5f 64 62 20 6a 6f 75 72  e vacuum_db jour
371fd 6e 61 6c 20 66 69 6c 65 20 69 73 20 64 65 6c 65  nal file is dele
371fe 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 67  ted when the pag
371ff 65 72 0a 20 20 2a 2a 20 69 73 20 63 6c 6f 73 65  er.  ** is close
37200 64 20 62 79 20 74 68 65 20 44 45 54 41 43 48 2e  d by the DETACH.
37201 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 61 75 74 6f  .  */.  db->auto
37202 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 69  Commit = 1;..  i
37203 66 28 20 70 44 62 20 29 7b 0a 20 20 20 20 73 71  f( pDb ){.    sq
37204 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
37205 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 70  pDb->pBt);.    p
37206 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20 20  Db->pBt = 0;.   
37207 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20   pDb->pSchema = 
37208 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69  0;.  }..  /* Thi
37209 73 20 62 6f 74 68 20 63 6c 65 61 72 73 20 74 68  s both clears th
3720a 65 20 73 63 68 65 6d 61 73 20 61 6e 64 20 72 65  e schemas and re
3720b 64 75 63 65 73 20 74 68 65 20 73 69 7a 65 20 6f  duces the size o
3720c 66 20 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d 0a  f the db->aDb[].
3720d 20 20 2a 2a 20 61 72 72 61 79 2e 20 2a 2f 20 0a    ** array. */ .
3720e 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 41 6c    sqlite3ResetAl
3720f 6c 53 63 68 65 6d 61 73 4f 66 43 6f 6e 6e 65 63  lSchemasOfConnec
37210 74 69 6f 6e 28 64 62 29 3b 0a 0a 20 20 72 65 74  tion(db);..  ret
37211 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69  urn rc;.}..#endi
37212 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  f  /* SQLITE_OMI
37213 54 5f 56 41 43 55 55 4d 20 26 26 20 53 51 4c 49  T_VACUUM && SQLI
37214 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 20 2a  TE_OMIT_ATTACH *
37215 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
37216 2a 2a 20 45 6e 64 20 6f 66 20 76 61 63 75 75 6d  ** End of vacuum
37217 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
37218 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37219 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3721a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
3721b 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 74  ** Begin file vt
3721c 61 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ab.c ***********
3721d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3721e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3721f 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75  */./*.** 2006 Ju
37220 6e 65 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ne 10.**.** The 
37221 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
37222 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
37223 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
37224 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
37225 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
37226 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
37227 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
37228 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
37229 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
3722a 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
3722b 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
3722c 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
3722d 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
3722e 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
3722f 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
37230 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
37231 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
37232 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37233 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37234 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37235 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37236 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
37237 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
37238 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69 6d 70  used to help imp
37239 6c 65 6d 65 6e 74 20 76 69 72 74 75 61 6c 20 74  lement virtual t
3723a 61 62 6c 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ables..*/.#ifnde
3723b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
3723c 52 54 55 41 4c 54 41 42 4c 45 0a 0a 2f 2a 0a 2a  RTUALTABLE../*.*
3723d 2a 20 42 65 66 6f 72 65 20 61 20 76 69 72 74 75  * Before a virtu
3723e 61 6c 20 74 61 62 6c 65 20 78 43 72 65 61 74 65  al table xCreate
3723f 28 29 20 6f 72 20 78 43 6f 6e 6e 65 63 74 28 29  () or xConnect()
37240 20 6d 65 74 68 6f 64 20 69 73 20 69 6e 76 6f 6b   method is invok
37241 65 64 2c 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74  ed, the.** sqlit
37242 65 33 2e 70 56 74 61 62 43 74 78 20 6d 65 6d 62  e3.pVtabCtx memb
37243 65 72 20 76 61 72 69 61 62 6c 65 20 69 73 20 73  er variable is s
37244 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61  et to point to a
37245 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a  n instance of.**
37246 20 74 68 69 73 20 73 74 72 75 63 74 20 61 6c 6c   this struct all
37247 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20 73 74  ocated on the st
37248 61 63 6b 2e 20 49 74 20 69 73 20 75 73 65 64 20  ack. It is used 
37249 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  by the implement
3724a 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a 20 74 68 65  ation of .** the
3724b 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
3724c 5f 76 74 61 62 28 29 20 61 6e 64 20 73 71 6c 69  _vtab() and sqli
3724d 74 65 33 5f 76 74 61 62 5f 63 6f 6e 66 69 67 28  te3_vtab_config(
3724e 29 20 41 50 49 73 2c 20 62 6f 74 68 20 6f 66 20  ) APIs, both of 
3724f 77 68 69 63 68 0a 2a 2a 20 61 72 65 20 69 6e 76  which.** are inv
37250 6f 6b 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20 77  oked only from w
37251 69 74 68 69 6e 20 78 43 72 65 61 74 65 20 61 6e  ithin xCreate an
37252 64 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74 68 6f  d xConnect metho
37253 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 74  ds..*/.struct Vt
37254 61 62 43 74 78 20 7b 0a 20 20 56 54 61 62 6c 65  abCtx {.  VTable
37255 20 2a 70 56 54 61 62 6c 65 3b 20 20 20 20 2f 2a   *pVTable;    /*
37256 20 54 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   The virtual tab
37257 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75  le being constru
37258 63 74 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65 20  cted */.  Table 
37259 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 2f 2a  *pTab;        /*
3725a 20 54 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63   The Table objec
3725b 74 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 76  t to which the v
3725c 69 72 74 75 61 6c 20 74 61 62 6c 65 20 62 65 6c  irtual table bel
3725d 6f 6e 67 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ongs */.};../*.*
3725e 2a 20 54 68 65 20 61 63 74 75 61 6c 20 66 75 6e  * The actual fun
3725f 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20  ction that does 
37260 74 68 65 20 77 6f 72 6b 20 6f 66 20 63 72 65 61  the work of crea
37261 74 69 6e 67 20 61 20 6e 65 77 20 6d 6f 64 75 6c  ting a new modul
37262 65 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  e..** This funct
37263 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ion implements t
37264 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  he sqlite3_creat
37265 65 5f 6d 6f 64 75 6c 65 28 29 20 61 6e 64 0a 2a  e_module() and.*
37266 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  * sqlite3_create
37267 5f 6d 6f 64 75 6c 65 5f 76 32 28 29 20 69 6e 74  _module_v2() int
37268 65 72 66 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74  erfaces..*/.stat
37269 69 63 20 69 6e 74 20 63 72 65 61 74 65 4d 6f 64  ic int createMod
3726a 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ule(.  sqlite3 *
3726b 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
3726c 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
3726d 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f 64 75  se in which modu
3726e 6c 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64  le is registered
3726f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
37270 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
37271 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73        /* Name as
37272 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73 20 6d  signed to this m
37273 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  odule */.  const
37274 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
37275 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68  *pModule,  /* Th
37276 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  e definition of 
37277 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  the module */.  
37278 76 6f 69 64 20 2a 70 41 75 78 2c 20 20 20 20 20  void *pAux,     
37279 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3727a 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74  /* Context point
3727b 65 72 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78  er for xCreate/x
3727c 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69  Connect */.  voi
3727d 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f  d (*xDestroy)(vo
3727e 69 64 20 2a 29 20 20 20 20 20 20 20 20 2f 2a 20  id *)        /* 
3727f 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f  Module destructo
37280 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  r function */.){
37281 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
37282 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 4e 61  TE_OK;.  int nNa
37283 6d 65 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d  me;..  sqlite3_m
37284 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
37285 75 74 65 78 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d  utex);.  nName =
37286 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
37287 28 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 73  (zName);.  if( s
37288 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
37289 64 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20 7a 4e 61  db->aModule, zNa
3728a 6d 65 2c 20 6e 4e 61 6d 65 29 20 29 7b 0a 20 20  me, nName) ){.  
3728b 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49    rc = SQLITE_MI
3728c 53 55 53 45 5f 42 4b 50 54 3b 0a 20 20 7d 65 6c  SUSE_BKPT;.  }el
3728d 73 65 7b 0a 20 20 20 20 4d 6f 64 75 6c 65 20 2a  se{.    Module *
3728e 70 4d 6f 64 3b 0a 20 20 20 20 70 4d 6f 64 20 3d  pMod;.    pMod =
3728f 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74   (Module *)sqlit
37290 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
37291 2c 20 73 69 7a 65 6f 66 28 4d 6f 64 75 6c 65 29  , sizeof(Module)
37292 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 29 3b 0a 20   + nName + 1);. 
37293 20 20 20 69 66 28 20 70 4d 6f 64 20 29 7b 0a 20     if( pMod ){. 
37294 20 20 20 20 20 4d 6f 64 75 6c 65 20 2a 70 44 65       Module *pDe
37295 6c 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  l;.      char *z
37296 43 6f 70 79 20 3d 20 28 63 68 61 72 20 2a 29 28  Copy = (char *)(
37297 26 70 4d 6f 64 5b 31 5d 29 3b 0a 20 20 20 20 20  &pMod[1]);.     
37298 20 6d 65 6d 63 70 79 28 7a 43 6f 70 79 2c 20 7a   memcpy(zCopy, z
37299 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a  Name, nName+1);.
3729a 20 20 20 20 20 20 70 4d 6f 64 2d 3e 7a 4e 61 6d        pMod->zNam
3729b 65 20 3d 20 7a 43 6f 70 79 3b 0a 20 20 20 20 20  e = zCopy;.     
3729c 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 20 3d   pMod->pModule =
3729d 20 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20   pModule;.      
3729e 70 4d 6f 64 2d 3e 70 41 75 78 20 3d 20 70 41 75  pMod->pAux = pAu
3729f 78 3b 0a 20 20 20 20 20 20 70 4d 6f 64 2d 3e 78  x;.      pMod->x
372a0 44 65 73 74 72 6f 79 20 3d 20 78 44 65 73 74 72  Destroy = xDestr
372a1 6f 79 3b 0a 20 20 20 20 20 20 70 44 65 6c 20 3d  oy;.      pDel =
372a2 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74   (Module *)sqlit
372a3 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 64 62  e3HashInsert(&db
372a4 2d 3e 61 4d 6f 64 75 6c 65 2c 7a 43 6f 70 79 2c  ->aModule,zCopy,
372a5 6e 4e 61 6d 65 2c 28 76 6f 69 64 2a 29 70 4d 6f  nName,(void*)pMo
372a6 64 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  d);.      assert
372a7 28 20 70 44 65 6c 3d 3d 30 20 7c 7c 20 70 44 65  ( pDel==0 || pDe
372a8 6c 3d 3d 70 4d 6f 64 20 29 3b 0a 20 20 20 20 20  l==pMod );.     
372a9 20 69 66 28 20 70 44 65 6c 20 29 7b 0a 20 20 20   if( pDel ){.   
372aa 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
372ab 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
372ac 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
372ad 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20  (db, pDel);.    
372ae 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
372af 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
372b0 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 69  xit(db, rc);.  i
372b1 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
372b2 20 26 26 20 78 44 65 73 74 72 6f 79 20 29 20 78   && xDestroy ) x
372b3 44 65 73 74 72 6f 79 28 70 41 75 78 29 3b 0a 0a  Destroy(pAux);..
372b4 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
372b5 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
372b6 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
372b7 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e 61  .../*.** Externa
372b8 6c 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 75  l API function u
372b9 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  sed to create a 
372ba 6e 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  new virtual-tabl
372bb 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c  e module..*/.SQL
372bc 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
372bd 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
372be 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
372bf 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
372c0 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
372c1 20 69 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c 65   in which module
372c2 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 2a   is registered *
372c3 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
372c4 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20  zName,          
372c5 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69      /* Name assi
372c6 67 6e 65 64 20 74 6f 20 74 68 69 73 20 6d 6f 64  gned to this mod
372c7 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ule */.  const s
372c8 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
372c9 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20  Module,  /* The 
372ca 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68  definition of th
372cb 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f  e module */.  vo
372cc 69 64 20 2a 70 41 75 78 20 20 20 20 20 20 20 20  id *pAux        
372cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
372ce 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72   Context pointer
372cf 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f   for xCreate/xCo
372d0 6e 6e 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 72 65  nnect */.){.  re
372d1 74 75 72 6e 20 63 72 65 61 74 65 4d 6f 64 75 6c  turn createModul
372d2 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 4d 6f  e(db, zName, pMo
372d3 64 75 6c 65 2c 20 70 41 75 78 2c 20 30 29 3b 0a  dule, pAux, 0);.
372d4 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e 61  }../*.** Externa
372d5 6c 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 75  l API function u
372d6 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  sed to create a 
372d7 6e 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  new virtual-tabl
372d8 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c  e module..*/.SQL
372d9 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
372da 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
372db 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20  e_v2(.  sqlite3 
372dc 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
372dd 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
372de 61 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f 64  ase in which mod
372df 75 6c 65 20 69 73 20 72 65 67 69 73 74 65 72 65  ule is registere
372e0 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
372e1 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
372e2 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61         /* Name a
372e3 73 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73 20  ssigned to this 
372e4 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  module */.  cons
372e5 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
372e6 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54   *pModule,  /* T
372e7 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66  he definition of
372e8 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20   the module */. 
372e9 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 20 20 20   void *pAux,    
372ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
372eb 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e   /* Context poin
372ec 74 65 72 20 66 6f 72 20 78 43 72 65 61 74 65 2f  ter for xCreate/
372ed 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f  xConnect */.  vo
372ee 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76  id (*xDestroy)(v
372ef 6f 69 64 20 2a 29 20 20 20 20 20 20 20 20 2f 2a  oid *)        /*
372f0 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74   Module destruct
372f1 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29  or function */.)
372f2 7b 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74  {.  return creat
372f3 65 4d 6f 64 75 6c 65 28 64 62 2c 20 7a 4e 61 6d  eModule(db, zNam
372f4 65 2c 20 70 4d 6f 64 75 6c 65 2c 20 70 41 75 78  e, pModule, pAux
372f5 2c 20 78 44 65 73 74 72 6f 79 29 3b 0a 7d 0a 0a  , xDestroy);.}..
372f6 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 76  /*.** Lock the v
372f7 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 6f 20  irtual table so 
372f8 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62  that it cannot b
372f9 65 20 64 69 73 63 6f 6e 6e 65 63 74 65 64 2e 0a  e disconnected..
372fa 2a 2a 20 4c 6f 63 6b 73 20 6e 65 73 74 2e 20 20  ** Locks nest.  
372fb 45 76 65 72 79 20 6c 6f 63 6b 20 73 68 6f 75 6c  Every lock shoul
372fc 64 20 68 61 76 65 20 61 20 63 6f 72 72 65 73 70  d have a corresp
372fd 6f 6e 64 69 6e 67 20 75 6e 6c 6f 63 6b 2e 0a 2a  onding unlock..*
372fe 2a 20 49 66 20 61 6e 20 75 6e 6c 6f 63 6b 20 69  * If an unlock i
372ff 73 20 6f 6d 69 74 74 65 64 2c 20 72 65 73 6f 75  s omitted, resou
37300 72 63 65 73 20 6c 65 61 6b 73 20 77 69 6c 6c 20  rces leaks will 
37301 6f 63 63 75 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 49  occur.  .**.** I
37302 66 20 61 20 64 69 73 63 6f 6e 6e 65 63 74 20 69  f a disconnect i
37303 73 20 61 74 74 65 6d 70 74 65 64 20 77 68 69 6c  s attempted whil
37304 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  e a virtual tabl
37305 65 20 69 73 20 6c 6f 63 6b 65 64 2c 0a 2a 2a 20  e is locked,.** 
37306 74 68 65 20 64 69 73 63 6f 6e 6e 65 63 74 20 69  the disconnect i
37307 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c  s deferred until
37308 20 61 6c 6c 20 6c 6f 63 6b 73 20 68 61 76 65 20   all locks have 
37309 62 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f  been removed..*/
3730a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
3730b 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
3730c 4c 6f 63 6b 28 56 54 61 62 6c 65 20 2a 70 56 54  Lock(VTable *pVT
3730d 61 62 29 7b 0a 20 20 70 56 54 61 62 2d 3e 6e 52  ab){.  pVTab->nR
3730e 65 66 2b 2b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  ef++;.}.../*.** 
3730f 70 54 61 62 20 69 73 20 61 20 70 6f 69 6e 74 65  pTab is a pointe
37310 72 20 74 6f 20 61 20 54 61 62 6c 65 20 73 74 72  r to a Table str
37311 75 63 74 75 72 65 20 72 65 70 72 65 73 65 6e 74  ucture represent
37312 69 6e 67 20 61 20 76 69 72 74 75 61 6c 2d 74 61  ing a virtual-ta
37313 62 6c 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ble..** Return a
37314 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
37315 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 75 73  VTable object us
37316 65 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e  ed by connection
37317 20 64 62 20 74 6f 20 61 63 63 65 73 73 20 0a 2a   db to access .*
37318 2a 20 74 68 69 73 20 76 69 72 74 75 61 6c 2d 74  * this virtual-t
37319 61 62 6c 65 2c 20 69 66 20 6f 6e 65 20 68 61 73  able, if one has
3731a 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20 6f   been created, o
3731b 72 20 4e 55 4c 4c 20 6f 74 68 65 72 77 69 73 65  r NULL otherwise
3731c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
3731d 41 54 45 20 56 54 61 62 6c 65 20 2a 73 71 6c 69  ATE VTable *sqli
3731e 74 65 33 47 65 74 56 54 61 62 6c 65 28 73 71 6c  te3GetVTable(sql
3731f 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20  ite3 *db, Table 
37320 2a 70 54 61 62 29 7b 0a 20 20 56 54 61 62 6c 65  *pTab){.  VTable
37321 20 2a 70 56 74 61 62 3b 0a 20 20 61 73 73 65 72   *pVtab;.  asser
37322 74 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  t( IsVirtual(pTa
37323 62 29 20 29 3b 0a 20 20 66 6f 72 28 70 56 74 61  b) );.  for(pVta
37324 62 3d 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 3b  b=pTab->pVTable;
37325 20 70 56 74 61 62 20 26 26 20 70 56 74 61 62 2d   pVtab && pVtab-
37326 3e 64 62 21 3d 64 62 3b 20 70 56 74 61 62 3d 70  >db!=db; pVtab=p
37327 56 74 61 62 2d 3e 70 4e 65 78 74 29 3b 0a 20 20  Vtab->pNext);.  
37328 72 65 74 75 72 6e 20 70 56 74 61 62 3b 0a 7d 0a  return pVtab;.}.
37329 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74  ./*.** Decrement
3732a 20 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 6f   the ref-count o
3732b 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  n a virtual tabl
3732c 65 20 6f 62 6a 65 63 74 2e 20 57 68 65 6e 20 74  e object. When t
3732d 68 65 20 72 65 66 2d 63 6f 75 6e 74 0a 2a 2a 20  he ref-count.** 
3732e 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 63 61  reaches zero, ca
3732f 6c 6c 20 74 68 65 20 78 44 69 73 63 6f 6e 6e 65  ll the xDisconne
37330 63 74 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 64  ct() method to d
37331 65 6c 65 74 65 20 74 68 65 20 6f 62 6a 65 63 74  elete the object
37332 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
37333 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
37334 56 74 61 62 55 6e 6c 6f 63 6b 28 56 54 61 62 6c  VtabUnlock(VTabl
37335 65 20 2a 70 56 54 61 62 29 7b 0a 20 20 73 71 6c  e *pVTab){.  sql
37336 69 74 65 33 20 2a 64 62 20 3d 20 70 56 54 61 62  ite3 *db = pVTab
37337 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28  ->db;..  assert(
37338 20 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28   db );.  assert(
37339 20 70 56 54 61 62 2d 3e 6e 52 65 66 3e 30 20 29   pVTab->nRef>0 )
3733a 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ;.  assert( db->
3733b 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41  magic==SQLITE_MA
3733c 47 49 43 5f 4f 50 45 4e 20 7c 7c 20 64 62 2d 3e  GIC_OPEN || db->
3733d 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41  magic==SQLITE_MA
3733e 47 49 43 5f 5a 4f 4d 42 49 45 20 29 3b 0a 0a 20  GIC_ZOMBIE );.. 
3733f 20 70 56 54 61 62 2d 3e 6e 52 65 66 2d 2d 3b 0a   pVTab->nRef--;.
37340 20 20 69 66 28 20 70 56 54 61 62 2d 3e 6e 52 65    if( pVTab->nRe
37341 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  f==0 ){.    sqli
37342 74 65 33 5f 76 74 61 62 20 2a 70 20 3d 20 70 56  te3_vtab *p = pV
37343 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20  Tab->pVtab;.    
37344 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 70  if( p ){.      p
37345 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 69 73 63  ->pModule->xDisc
37346 6f 6e 6e 65 63 74 28 70 29 3b 0a 20 20 20 20 7d  onnect(p);.    }
37347 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
37348 65 65 28 64 62 2c 20 70 56 54 61 62 29 3b 0a 20  ee(db, pVTab);. 
37349 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c   }.}../*.** Tabl
3734a 65 20 70 20 69 73 20 61 20 76 69 72 74 75 61 6c  e p is a virtual
3734b 20 74 61 62 6c 65 2e 20 54 68 69 73 20 66 75 6e   table. This fun
3734c 63 74 69 6f 6e 20 6d 6f 76 65 73 20 61 6c 6c 20  ction moves all 
3734d 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 0a  elements in the.
3734e 2a 2a 20 70 2d 3e 70 56 54 61 62 6c 65 20 6c 69  ** p->pVTable li
3734f 73 74 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  st to the sqlite
37350 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69  3.pDisconnect li
37351 73 74 73 20 6f 66 20 74 68 65 69 72 20 61 73 73  sts of their ass
37352 6f 63 69 61 74 65 64 0a 2a 2a 20 64 61 74 61 62  ociated.** datab
37353 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ase connections 
37354 74 6f 20 62 65 20 64 69 73 63 6f 6e 6e 65 63 74  to be disconnect
37355 65 64 20 61 74 20 74 68 65 20 6e 65 78 74 20 6f  ed at the next o
37356 70 70 6f 72 74 75 6e 69 74 79 2e 20 0a 2a 2a 20  pportunity. .** 
37357 45 78 63 65 70 74 2c 20 69 66 20 61 72 67 75 6d  Except, if argum
37358 65 6e 74 20 64 62 20 69 73 20 6e 6f 74 20 4e 55  ent db is not NU
37359 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 74  LL, then the ent
3735a 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ry associated wi
3735b 74 68 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  th.** connection
3735c 20 64 62 20 69 73 20 6c 65 66 74 20 69 6e 20 74   db is left in t
3735d 68 65 20 70 2d 3e 70 56 54 61 62 6c 65 20 6c 69  he p->pVTable li
3735e 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 54  st..*/.static VT
3735f 61 62 6c 65 20 2a 76 74 61 62 44 69 73 63 6f 6e  able *vtabDiscon
37360 6e 65 63 74 41 6c 6c 28 73 71 6c 69 74 65 33 20  nectAll(sqlite3 
37361 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70 29 7b 0a  *db, Table *p){.
37362 20 20 56 54 61 62 6c 65 20 2a 70 52 65 74 20 3d    VTable *pRet =
37363 20 30 3b 0a 20 20 56 54 61 62 6c 65 20 2a 70 56   0;.  VTable *pV
37364 54 61 62 6c 65 20 3d 20 70 2d 3e 70 56 54 61 62  Table = p->pVTab
37365 6c 65 3b 0a 20 20 70 2d 3e 70 56 54 61 62 6c 65  le;.  p->pVTable
37366 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 41 73 73 65   = 0;..  /* Asse
37367 72 74 20 74 68 61 74 20 74 68 65 20 6d 75 74 65  rt that the mute
37368 78 20 28 69 66 20 61 6e 79 29 20 61 73 73 6f 63  x (if any) assoc
37369 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 42  iated with the B
3736a 74 53 68 61 72 65 64 20 64 61 74 61 62 61 73 65  tShared database
3736b 20 0a 20 20 2a 2a 20 74 68 61 74 20 63 6f 6e 74   .  ** that cont
3736c 61 69 6e 73 20 74 61 62 6c 65 20 70 20 69 73 20  ains table p is 
3736d 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c  held by the call
3736e 65 72 2e 20 53 65 65 20 68 65 61 64 65 72 20 63  er. See header c
3736f 6f 6d 6d 65 6e 74 73 20 0a 20 20 2a 2a 20 61 62  omments .  ** ab
37370 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c  ove function sql
37371 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69  ite3VtabUnlockLi
37372 73 74 28 29 20 66 6f 72 20 61 6e 20 65 78 70 6c  st() for an expl
37373 61 6e 61 74 69 6f 6e 20 6f 66 20 77 68 79 0a 20  anation of why. 
37374 20 2a 2a 20 74 68 69 73 20 6d 61 6b 65 73 20 69   ** this makes i
37375 74 20 73 61 66 65 20 74 6f 20 61 63 63 65 73 73  t safe to access
37376 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69   the sqlite3.pDi
37377 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 20 6f 66  sconnect list of
37378 20 61 6e 79 0a 20 20 2a 2a 20 64 61 74 61 62 61   any.  ** databa
37379 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68  se connection th
3737a 61 74 20 6d 61 79 20 68 61 76 65 20 61 6e 20 65  at may have an e
3737b 6e 74 72 79 20 69 6e 20 74 68 65 20 70 2d 3e 70  ntry in the p->p
3737c 56 54 61 62 6c 65 20 6c 69 73 74 2e 0a 20 20 2a  VTable list..  *
3737d 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d  /.  assert( db==
3737e 30 20 7c 7c 20 73 71 6c 69 74 65 33 53 63 68 65  0 || sqlite3Sche
3737f 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20  maMutexHeld(db, 
37380 30 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 20 29  0, p->pSchema) )
37381 3b 0a 0a 20 20 77 68 69 6c 65 28 20 70 56 54 61  ;..  while( pVTa
37382 62 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ble ){.    sqlit
37383 65 33 20 2a 64 62 32 20 3d 20 70 56 54 61 62 6c  e3 *db2 = pVTabl
37384 65 2d 3e 64 62 3b 0a 20 20 20 20 56 54 61 62 6c  e->db;.    VTabl
37385 65 20 2a 70 4e 65 78 74 20 3d 20 70 56 54 61 62  e *pNext = pVTab
37386 6c 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 61  le->pNext;.    a
37387 73 73 65 72 74 28 20 64 62 32 20 29 3b 0a 20 20  ssert( db2 );.  
37388 20 20 69 66 28 20 64 62 32 3d 3d 64 62 20 29 7b    if( db2==db ){
37389 0a 20 20 20 20 20 20 70 52 65 74 20 3d 20 70 56  .      pRet = pV
3738a 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 2d 3e  Table;.      p->
3738b 70 56 54 61 62 6c 65 20 3d 20 70 52 65 74 3b 0a  pVTable = pRet;.
3738c 20 20 20 20 20 20 70 52 65 74 2d 3e 70 4e 65 78        pRet->pNex
3738d 74 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  t = 0;.    }else
3738e 7b 0a 20 20 20 20 20 20 70 56 54 61 62 6c 65 2d  {.      pVTable-
3738f 3e 70 4e 65 78 74 20 3d 20 64 62 32 2d 3e 70 44  >pNext = db2->pD
37390 69 73 63 6f 6e 6e 65 63 74 3b 0a 20 20 20 20 20  isconnect;.     
37391 20 64 62 32 2d 3e 70 44 69 73 63 6f 6e 6e 65 63   db2->pDisconnec
37392 74 20 3d 20 70 56 54 61 62 6c 65 3b 0a 20 20 20  t = pVTable;.   
37393 20 7d 0a 20 20 20 20 70 56 54 61 62 6c 65 20 3d   }.    pVTable =
37394 20 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 20 61   pNext;.  }..  a
37395 73 73 65 72 74 28 20 21 64 62 20 7c 7c 20 70 52  ssert( !db || pR
37396 65 74 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  et );.  return p
37397 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61  Ret;.}../*.** Ta
37398 62 6c 65 20 2a 70 20 69 73 20 61 20 76 69 72 74  ble *p is a virt
37399 75 61 6c 20 74 61 62 6c 65 2e 20 54 68 69 73 20  ual table. This 
3739a 66 75 6e 63 74 69 6f 6e 20 72 65 6d 6f 76 65 73  function removes
3739b 20 74 68 65 20 56 54 61 62 6c 65 20 6f 62 6a 65   the VTable obje
3739c 63 74 0a 2a 2a 20 66 6f 72 20 74 61 62 6c 65 20  ct.** for table 
3739d 2a 70 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  *p associated wi
3739e 74 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  th database conn
3739f 65 63 74 69 6f 6e 20 64 62 20 66 72 6f 6d 20 74  ection db from t
373a0 68 65 20 6c 69 6e 6b 65 64 0a 2a 2a 20 6c 69 73  he linked.** lis
373a1 74 20 69 6e 20 70 2d 3e 70 56 54 61 62 2e 20 49  t in p->pVTab. I
373a2 74 20 61 6c 73 6f 20 64 65 63 72 65 6d 65 6e 74  t also decrement
373a3 73 20 74 68 65 20 56 54 61 62 6c 65 20 72 65 66  s the VTable ref
373a4 20 63 6f 75 6e 74 2e 20 54 68 69 73 20 69 73 0a   count. This is.
373a5 2a 2a 20 75 73 65 64 20 77 68 65 6e 20 63 6c 6f  ** used when clo
373a6 73 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f  sing database co
373a7 6e 6e 65 63 74 69 6f 6e 20 64 62 20 74 6f 20 66  nnection db to f
373a8 72 65 65 20 61 6c 6c 20 6f 66 20 69 74 73 20 56  ree all of its V
373a9 54 61 62 6c 65 0a 2a 2a 20 6f 62 6a 65 63 74 73  Table.** objects
373aa 20 77 69 74 68 6f 75 74 20 64 69 73 74 75 72 62   without disturb
373ab 69 6e 67 20 74 68 65 20 72 65 73 74 20 6f 66 20  ing the rest of 
373ac 74 68 65 20 53 63 68 65 6d 61 20 6f 62 6a 65 63  the Schema objec
373ad 74 20 28 77 68 69 63 68 20 6d 61 79 0a 2a 2a 20  t (which may.** 
373ae 62 65 20 62 65 69 6e 67 20 75 73 65 64 20 62 79  be being used by
373af 20 6f 74 68 65 72 20 73 68 61 72 65 64 2d 63 61   other shared-ca
373b0 63 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 29  che connections)
373b1 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
373b2 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
373b3 56 74 61 62 44 69 73 63 6f 6e 6e 65 63 74 28 73  VtabDisconnect(s
373b4 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c  qlite3 *db, Tabl
373b5 65 20 2a 70 29 7b 0a 20 20 56 54 61 62 6c 65 20  e *p){.  VTable 
373b6 2a 2a 70 70 56 54 61 62 3b 0a 0a 20 20 61 73 73  **ppVTab;..  ass
373b7 65 72 74 28 20 49 73 56 69 72 74 75 61 6c 28 70  ert( IsVirtual(p
373b8 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
373b9 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
373ba 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29  AllMutexes(db) )
373bb 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
373bc 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
373bd 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  b->mutex) );..  
373be 66 6f 72 28 70 70 56 54 61 62 3d 26 70 2d 3e 70  for(ppVTab=&p->p
373bf 56 54 61 62 6c 65 3b 20 2a 70 70 56 54 61 62 3b  VTable; *ppVTab;
373c0 20 70 70 56 54 61 62 3d 26 28 2a 70 70 56 54 61   ppVTab=&(*ppVTa
373c1 62 29 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  b)->pNext){.    
373c2 69 66 28 20 28 2a 70 70 56 54 61 62 29 2d 3e 64  if( (*ppVTab)->d
373c3 62 3d 3d 64 62 20 20 29 7b 0a 20 20 20 20 20 20  b==db  ){.      
373c4 56 54 61 62 6c 65 20 2a 70 56 54 61 62 20 3d 20  VTable *pVTab = 
373c5 2a 70 70 56 54 61 62 3b 0a 20 20 20 20 20 20 2a  *ppVTab;.      *
373c6 70 70 56 54 61 62 20 3d 20 70 56 54 61 62 2d 3e  ppVTab = pVTab->
373c7 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c  pNext;.      sql
373c8 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 70  ite3VtabUnlock(p
373c9 56 54 61 62 29 3b 0a 20 20 20 20 20 20 62 72 65  VTab);.      bre
373ca 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  ak;.    }.  }.}.
373cb 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 63 6f 6e 6e 65  ../*.** Disconne
373cc 63 74 20 61 6c 6c 20 74 68 65 20 76 69 72 74 75  ct all the virtu
373cd 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 73  al table objects
373ce 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e   in the sqlite3.
373cf 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74  pDisconnect list
373d0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
373d1 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62  ction may only b
373d2 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  e called when th
373d3 65 20 6d 75 74 65 78 65 73 20 61 73 73 6f 63 69  e mutexes associ
373d4 61 74 65 64 20 77 69 74 68 20 61 6c 6c 0a 2a 2a  ated with all.**
373d5 20 73 68 61 72 65 64 20 62 2d 74 72 65 65 20 64   shared b-tree d
373d6 61 74 61 62 61 73 65 73 20 6f 70 65 6e 65 64 20  atabases opened 
373d7 75 73 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e  using connection
373d8 20 64 62 20 61 72 65 20 68 65 6c 64 20 62 79 20   db are held by 
373d9 74 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20  the .** caller. 
373da 54 68 69 73 20 69 73 20 64 6f 6e 65 20 74 6f 20  This is done to 
373db 70 72 6f 74 65 63 74 20 74 68 65 20 73 71 6c 69  protect the sqli
373dc 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20  te3.pDisconnect 
373dd 6c 69 73 74 2e 20 54 68 65 0a 2a 2a 20 73 71 6c  list. The.** sql
373de 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74  ite3.pDisconnect
373df 20 6c 69 73 74 20 69 73 20 61 63 63 65 73 73 65   list is accesse
373e0 64 20 6f 6e 6c 79 20 61 73 20 66 6f 6c 6c 6f 77  d only as follow
373e1 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 42 79  s:.**.**   1) By
373e2 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20   this function. 
373e3 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 61 6c  In this case, al
373e4 6c 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78  l BtShared mutex
373e5 65 73 20 61 6e 64 20 74 68 65 20 6d 75 74 65 78  es and the mutex
373e6 0a 2a 2a 20 20 20 20 20 20 61 73 73 6f 63 69 61  .**      associa
373e7 74 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74  ted with the dat
373e8 61 62 61 73 65 20 68 61 6e 64 6c 65 20 69 74 73  abase handle its
373e9 65 6c 66 20 6d 75 73 74 20 62 65 20 68 65 6c 64  elf must be held
373ea 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 42 79 20  ..**.**   2) By 
373eb 66 75 6e 63 74 69 6f 6e 20 76 74 61 62 44 69 73  function vtabDis
373ec 63 6f 6e 6e 65 63 74 41 6c 6c 28 29 2c 20 77 68  connectAll(), wh
373ed 65 6e 20 69 74 20 61 64 64 73 20 61 20 56 54 61  en it adds a VTa
373ee 62 6c 65 20 65 6e 74 72 79 20 74 6f 0a 2a 2a 20  ble entry to.** 
373ef 20 20 20 20 20 74 68 65 20 73 71 6c 69 74 65 33       the sqlite3
373f0 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73  .pDisconnect lis
373f1 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  t. In this case 
373f2 65 69 74 68 65 72 20 74 68 65 20 42 74 53 68 61  either the BtSha
373f3 72 65 64 20 6d 75 74 65 78 0a 2a 2a 20 20 20 20  red mutex.**    
373f4 20 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74    associated wit
373f5 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  h the database t
373f6 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
373f7 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 69 73   is stored in is
373f8 20 68 65 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 72   held.**      or
373f9 2c 20 69 66 20 74 68 65 20 76 69 72 74 75 61 6c  , if the virtual
373fa 20 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64   table is stored
373fb 20 69 6e 20 61 20 6e 6f 6e 2d 73 68 61 72 61 62   in a non-sharab
373fc 6c 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  le database, the
373fd 6e 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 64 61  n.**      the da
373fe 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6d 75  tabase handle mu
373ff 74 65 78 20 69 73 20 68 65 6c 64 2e 0a 2a 2a 0a  tex is held..**.
37400 2a 2a 20 41 73 20 61 20 72 65 73 75 6c 74 2c 20  ** As a result, 
37401 61 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f  a sqlite3.pDisco
37402 6e 6e 65 63 74 20 63 61 6e 6e 6f 74 20 62 65 20  nnect cannot be 
37403 61 63 63 65 73 73 65 64 20 73 69 6d 75 6c 74 61  accessed simulta
37404 6e 65 6f 75 73 6c 79 20 0a 2a 2a 20 62 79 20 6d  neously .** by m
37405 75 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73 2e  ultiple threads.
37406 20 49 74 20 69 73 20 74 68 72 65 61 64 2d 73 61   It is thread-sa
37407 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  fe..*/.SQLITE_PR
37408 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
37409 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74  e3VtabUnlockList
3740a 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
3740b 20 56 54 61 62 6c 65 20 2a 70 20 3d 20 64 62 2d   VTable *p = db-
3740c 3e 70 44 69 73 63 6f 6e 6e 65 63 74 3b 0a 20 20  >pDisconnect;.  
3740d 64 62 2d 3e 70 44 69 73 63 6f 6e 6e 65 63 74 20  db->pDisconnect 
3740e 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
3740f 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
37410 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20  sAllMutexes(db) 
37411 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
37412 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
37413 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  db->mutex) );.. 
37414 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71   if( p ){.    sq
37415 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
37416 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62  redStatements(db
37417 29 3b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  );.    do {.    
37418 20 20 56 54 61 62 6c 65 20 2a 70 4e 65 78 74 20    VTable *pNext 
37419 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  = p->pNext;.    
3741a 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c    sqlite3VtabUnl
3741b 6f 63 6b 28 70 29 3b 0a 20 20 20 20 20 20 70 20  ock(p);.      p 
3741c 3d 20 70 4e 65 78 74 3b 0a 20 20 20 20 7d 77 68  = pNext;.    }wh
3741d 69 6c 65 28 20 70 20 29 3b 0a 20 20 7d 0a 7d 0a  ile( p );.  }.}.
3741e 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 79  ./*.** Clear any
3741f 20 61 6e 64 20 61 6c 6c 20 76 69 72 74 75 61 6c   and all virtual
37420 2d 74 61 62 6c 65 20 69 6e 66 6f 72 6d 61 74 69  -table informati
37421 6f 6e 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c  on from the Tabl
37422 65 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 54 68 69  e record..** Thi
37423 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
37424 6c 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  led, for example
37425 2c 20 6a 75 73 74 20 62 65 66 6f 72 65 20 64 65  , just before de
37426 6c 65 74 69 6e 67 20 74 68 65 20 54 61 62 6c 65  leting the Table
37427 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a 2a 0a 2a  .** record..**.*
37428 2a 20 53 69 6e 63 65 20 69 74 20 69 73 20 61 20  * Since it is a 
37429 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 2c 20 74  virtual-table, t
3742a 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
3742b 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f  re contains a po
3742c 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20  inter.** to the 
3742d 68 65 61 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64  head of a linked
3742e 20 6c 69 73 74 20 6f 66 20 56 54 61 62 6c 65 20   list of VTable 
3742f 73 74 72 75 63 74 75 72 65 73 2e 20 45 61 63 68  structures. Each
37430 20 56 54 61 62 6c 65 20 0a 2a 2a 20 73 74 72 75   VTable .** stru
37431 63 74 75 72 65 20 69 73 20 61 73 73 6f 63 69 61  cture is associa
37432 74 65 64 20 77 69 74 68 20 61 20 73 69 6e 67 6c  ted with a singl
37433 65 20 73 71 6c 69 74 65 33 2a 20 75 73 65 72 20  e sqlite3* user 
37434 6f 66 20 74 68 65 20 73 63 68 65 6d 61 2e 0a 2a  of the schema..*
37435 2a 20 54 68 65 20 72 65 66 65 72 65 6e 63 65 20  * The reference 
37436 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 56 54 61  count of the VTa
37437 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 61 73  ble structure as
37438 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 64 61  sociated with da
37439 74 61 62 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65  tabase .** conne
3743a 63 74 69 6f 6e 20 64 62 20 69 73 20 64 65 63 72  ction db is decr
3743b 65 6d 65 6e 74 65 64 20 69 6d 6d 65 64 69 61 74  emented immediat
3743c 65 6c 79 20 28 77 68 69 63 68 20 6d 61 79 20 6c  ely (which may l
3743d 65 61 64 20 74 6f 20 74 68 65 20 0a 2a 2a 20 73  ead to the .** s
3743e 74 72 75 63 74 75 72 65 20 62 65 69 6e 67 20 78  tructure being x
3743f 44 69 73 63 6f 6e 6e 65 63 74 65 64 20 61 6e 64  Disconnected and
37440 20 66 72 65 65 29 2e 20 41 6e 79 20 6f 74 68 65   free). Any othe
37441 72 20 56 54 61 62 6c 65 20 73 74 72 75 63 74 75  r VTable structu
37442 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 6c 69  res.** in the li
37443 73 74 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20  st are moved to 
37444 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69 73  the sqlite3.pDis
37445 63 6f 6e 6e 65 63 74 20 6c 69 73 74 20 6f 66 20  connect list of 
37446 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a  the associated .
37447 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ** database conn
37448 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ection..*/.SQLIT
37449 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
3744a 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28  qlite3VtabClear(
3744b 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62  sqlite3 *db, Tab
3744c 6c 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 64  le *p){.  if( !d
3744d 62 20 7c 7c 20 64 62 2d 3e 70 6e 42 79 74 65 73  b || db->pnBytes
3744e 46 72 65 65 64 3d 3d 30 20 29 20 76 74 61 62 44  Freed==0 ) vtabD
3744f 69 73 63 6f 6e 6e 65 63 74 41 6c 6c 28 30 2c 20  isconnectAll(0, 
37450 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 7a 4d  p);.  if( p->azM
37451 6f 64 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20  oduleArg ){.    
37452 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
37453 3d 30 3b 20 69 3c 70 2d 3e 6e 4d 6f 64 75 6c 65  =0; i<p->nModule
37454 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Arg; i++){.     
37455 20 69 66 28 20 69 21 3d 31 20 29 20 73 71 6c 69   if( i!=1 ) sqli
37456 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
37457 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69 5d 29  >azModuleArg[i])
37458 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
37459 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
3745a 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20  >azModuleArg);. 
3745b 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20   }.}../*.** Add 
3745c 61 20 6e 65 77 20 6d 6f 64 75 6c 65 20 61 72 67  a new module arg
3745d 75 6d 65 6e 74 20 74 6f 20 70 54 61 62 6c 65 2d  ument to pTable-
3745e 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 5d 2e 0a  >azModuleArg[]..
3745f 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 69 73  ** The string is
37460 20 6e 6f 74 20 63 6f 70 69 65 64 20 2d 20 74 68   not copied - th
37461 65 20 70 6f 69 6e 74 65 72 20 69 73 20 73 74 6f  e pointer is sto
37462 72 65 64 2e 20 20 54 68 65 0a 2a 2a 20 73 74 72  red.  The.** str
37463 69 6e 67 20 77 69 6c 6c 20 62 65 20 66 72 65 65  ing will be free
37464 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  d automatically 
37465 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 69  when the table i
37466 73 0a 2a 2a 20 64 65 6c 65 74 65 64 2e 0a 2a 2f  s.** deleted..*/
37467 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 64 64  .static void add
37468 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 73  ModuleArgument(s
37469 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c  qlite3 *db, Tabl
3746a 65 20 2a 70 54 61 62 6c 65 2c 20 63 68 61 72 20  e *pTable, char 
3746b 2a 7a 41 72 67 29 7b 0a 20 20 69 6e 74 20 69 20  *zArg){.  int i 
3746c 3d 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c  = pTable->nModul
3746d 65 41 72 67 2b 2b 3b 0a 20 20 69 6e 74 20 6e 42  eArg++;.  int nB
3746e 79 74 65 73 20 3d 20 73 69 7a 65 6f 66 28 63 68  ytes = sizeof(ch
3746f 61 72 20 2a 29 2a 28 31 2b 70 54 61 62 6c 65 2d  ar *)*(1+pTable-
37470 3e 6e 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20  >nModuleArg);.  
37471 63 68 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41  char **azModuleA
37472 72 67 3b 0a 20 20 61 7a 4d 6f 64 75 6c 65 41 72  rg;.  azModuleAr
37473 67 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  g = sqlite3DbRea
37474 6c 6c 6f 63 28 64 62 2c 20 70 54 61 62 6c 65 2d  lloc(db, pTable-
37475 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 2c 20 6e 42  >azModuleArg, nB
37476 79 74 65 73 29 3b 0a 20 20 69 66 28 20 61 7a 4d  ytes);.  if( azM
37477 6f 64 75 6c 65 41 72 67 3d 3d 30 20 29 7b 0a 20  oduleArg==0 ){. 
37478 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 66 6f     int j;.    fo
37479 72 28 6a 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29  r(j=0; j<i; j++)
3747a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
3747b 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65  bFree(db, pTable
3747c 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 6a 5d  ->azModuleArg[j]
3747d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
3747e 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
3747f 41 72 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Arg);.    sqlite
37480 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62  3DbFree(db, pTab
37481 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 29  le->azModuleArg)
37482 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 4d  ;.    pTable->nM
37483 6f 64 75 6c 65 41 72 67 20 3d 20 30 3b 0a 20 20  oduleArg = 0;.  
37484 7d 65 6c 73 65 7b 0a 20 20 20 20 61 7a 4d 6f 64  }else{.    azMod
37485 75 6c 65 41 72 67 5b 69 5d 20 3d 20 7a 41 72 67  uleArg[i] = zArg
37486 3b 0a 20 20 20 20 61 7a 4d 6f 64 75 6c 65 41 72  ;.    azModuleAr
37487 67 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20 20 7d 0a  g[i+1] = 0;.  }.
37488 20 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75    pTable->azModu
37489 6c 65 41 72 67 20 3d 20 61 7a 4d 6f 64 75 6c 65  leArg = azModule
3748a 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  Arg;.}../*.** Th
3748b 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74  e parser calls t
3748c 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e  his routine when
3748d 20 69 74 20 66 69 72 73 74 20 73 65 65 73 20 61   it first sees a
3748e 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
3748f 54 41 42 4c 45 0a 2a 2a 20 73 74 61 74 65 6d 65  TABLE.** stateme
37490 6e 74 2e 20 20 54 68 65 20 6d 6f 64 75 6c 65 20  nt.  The module 
37491 6e 61 6d 65 20 68 61 73 20 62 65 65 6e 20 70 61  name has been pa
37492 72 73 65 64 2c 20 62 75 74 20 74 68 65 20 6f 70  rsed, but the op
37493 74 69 6f 6e 61 6c 20 6c 69 73 74 0a 2a 2a 20 6f  tional list.** o
37494 66 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61  f parameters tha
37495 74 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6d 6f 64  t follow the mod
37496 75 6c 65 20 6e 61 6d 65 20 61 72 65 20 73 74 69  ule name are sti
37497 6c 6c 20 70 65 6e 64 69 6e 67 2e 0a 2a 2f 0a 53  ll pending..*/.S
37498 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
37499 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 42 65  id sqlite3VtabBe
3749a 67 69 6e 50 61 72 73 65 28 0a 20 20 50 61 72 73  ginParse(.  Pars
3749b 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
3749c 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
3749d 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  text */.  Token 
3749e 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 20 20  *pName1,        
3749f 2f 2a 20 4e 61 6d 65 20 6f 66 20 6e 65 77 20 74  /* Name of new t
374a0 61 62 6c 65 2c 20 6f 72 20 64 61 74 61 62 61 73  able, or databas
374a1 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65  e name */.  Toke
374a2 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20  n *pName2,      
374a3 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6e 65 77    /* Name of new
374a4 20 74 61 62 6c 65 20 6f 72 20 4e 55 4c 4c 20 2a   table or NULL *
374a5 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4d 6f 64 75  /.  Token *pModu
374a6 6c 65 4e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d  leName,   /* Nam
374a7 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20  e of the module 
374a8 66 6f 72 20 74 68 65 20 76 69 72 74 75 61 6c 20  for the virtual 
374a9 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
374aa 66 4e 6f 74 45 78 69 73 74 73 20 20 20 20 20 20  fNotExists      
374ab 20 2f 2a 20 4e 6f 20 65 72 72 6f 72 20 69 66 20   /* No error if 
374ac 74 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64  the table alread
374ad 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20  y exists */.){. 
374ae 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
374af 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
374b0 74 61 62 61 73 65 20 74 68 65 20 74 61 62 6c 65  tabase the table
374b1 20 69 73 20 62 65 69 6e 67 20 63 72 65 61 74 65   is being create
374b2 64 20 69 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20  d in */.  Table 
374b3 2a 70 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20  *pTable;        
374b4 2f 2a 20 54 68 65 20 6e 65 77 20 76 69 72 74 75  /* The new virtu
374b5 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71  al table */.  sq
374b6 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
374b7 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
374b8 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20  connection */.. 
374b9 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62   sqlite3StartTab
374ba 6c 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65  le(pParse, pName
374bb 31 2c 20 70 4e 61 6d 65 32 2c 20 30 2c 20 30 2c  1, pName2, 0, 0,
374bc 20 31 2c 20 69 66 4e 6f 74 45 78 69 73 74 73 29   1, ifNotExists)
374bd 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20 70 50 61  ;.  pTable = pPa
374be 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
374bf 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30 20    if( pTable==0 
374c0 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
374c1 72 74 28 20 30 3d 3d 70 54 61 62 6c 65 2d 3e 70  rt( 0==pTable->p
374c2 49 6e 64 65 78 20 29 3b 0a 0a 20 20 64 62 20 3d  Index );..  db =
374c3 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
374c4 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
374c5 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
374c6 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  able->pSchema);.
374c7 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
374c8 20 29 3b 0a 0a 20 20 70 54 61 62 6c 65 2d 3e 74   );..  pTable->t
374c9 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 56 69  abFlags |= TF_Vi
374ca 72 74 75 61 6c 3b 0a 20 20 70 54 61 62 6c 65 2d  rtual;.  pTable-
374cb 3e 6e 4d 6f 64 75 6c 65 41 72 67 20 3d 20 30 3b  >nModuleArg = 0;
374cc 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41 72 67 75  .  addModuleArgu
374cd 6d 65 6e 74 28 64 62 2c 20 70 54 61 62 6c 65 2c  ment(db, pTable,
374ce 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
374cf 54 6f 6b 65 6e 28 64 62 2c 20 70 4d 6f 64 75 6c  Token(db, pModul
374d0 65 4e 61 6d 65 29 29 3b 0a 20 20 61 64 64 4d 6f  eName));.  addMo
374d1 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 64 62 2c  duleArgument(db,
374d2 20 70 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 61   pTable, 0);.  a
374d3 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74  ddModuleArgument
374d4 28 64 62 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c  (db, pTable, sql
374d5 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
374d6 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 29   pTable->zName))
374d7 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d  ;.  pParse->sNam
374d8 65 54 6f 6b 65 6e 2e 6e 20 3d 20 28 69 6e 74 29  eToken.n = (int)
374d9 28 26 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e 7a  (&pModuleName->z
374da 5b 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e 6e 5d  [pModuleName->n]
374db 20 2d 20 70 4e 61 6d 65 31 2d 3e 7a 29 3b 0a 0a   - pName1->z);..
374dc 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
374dd 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
374de 4e 0a 20 20 2f 2a 20 43 72 65 61 74 69 6e 67 20  N.  /* Creating 
374df 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
374e0 69 6e 76 6f 6b 65 73 20 74 68 65 20 61 75 74 68  invokes the auth
374e1 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61  orization callba
374e2 63 6b 20 74 77 69 63 65 2e 0a 20 20 2a 2a 20 54  ck twice..  ** T
374e3 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61 74  he first invocat
374e4 69 6f 6e 2c 20 74 6f 20 6f 62 74 61 69 6e 20 70  ion, to obtain p
374e5 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20 49 4e 53  ermission to INS
374e6 45 52 54 20 61 20 72 6f 77 20 69 6e 74 6f 20 74  ERT a row into t
374e7 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d  he.  ** sqlite_m
374e8 61 73 74 65 72 20 74 61 62 6c 65 2c 20 68 61 73  aster table, has
374e9 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d 61   already been ma
374ea 64 65 20 62 79 20 73 71 6c 69 74 65 33 53 74 61  de by sqlite3Sta
374eb 72 74 54 61 62 6c 65 28 29 2e 0a 20 20 2a 2a 20  rtTable()..  ** 
374ec 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c 2c  The second call,
374ed 20 74 6f 20 6f 62 74 61 69 6e 20 70 65 72 6d 69   to obtain permi
374ee 73 73 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20  ssion to create 
374ef 74 68 65 20 74 61 62 6c 65 2c 20 69 73 20 6d 61  the table, is ma
374f0 64 65 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69  de now..  */.  i
374f1 66 28 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64  f( pTable->azMod
374f2 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 73 71  uleArg ){.    sq
374f3 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
374f4 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 43 52  Parse, SQLITE_CR
374f5 45 41 54 45 5f 56 54 41 42 4c 45 2c 20 70 54 61  EATE_VTABLE, pTa
374f6 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20 20  ble->zName, .   
374f7 20 20 20 20 20 20 20 20 20 70 54 61 62 6c 65 2d           pTable-
374f8 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 2c  >azModuleArg[0],
374f9 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62   pParse->db->aDb
374fa 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  [iDb].zName);.  
374fb 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
374fc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74  * This routine t
374fd 61 6b 65 73 20 74 68 65 20 6d 6f 64 75 6c 65 20  akes the module 
374fe 61 72 67 75 6d 65 6e 74 20 74 68 61 74 20 68 61  argument that ha
374ff 73 20 62 65 65 6e 20 61 63 63 75 6d 75 6c 61 74  s been accumulat
37500 69 6e 67 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65  ing.** in pParse
37501 2d 3e 7a 41 72 67 5b 5d 20 61 6e 64 20 61 70 70  ->zArg[] and app
37502 65 6e 64 73 20 69 74 20 74 6f 20 74 68 65 20 6c  ends it to the l
37503 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ist of arguments
37504 20 6f 6e 20 74 68 65 0a 2a 2a 20 76 69 72 74 75   on the.** virtu
37505 61 6c 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74  al table current
37506 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ly under constru
37507 63 74 69 6f 6e 20 69 6e 20 70 50 61 72 73 65 2d  ction in pParse-
37508 3e 70 54 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  >pTable..*/.stat
37509 69 63 20 76 6f 69 64 20 61 64 64 41 72 67 75 6d  ic void addArgum
3750a 65 6e 74 54 6f 56 74 61 62 28 50 61 72 73 65 20  entToVtab(Parse 
3750b 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 66 28 20  *pParse){.  if( 
3750c 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20 26  pParse->sArg.z &
3750d 26 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  & pParse->pNewTa
3750e 62 6c 65 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  ble ){.    const
3750f 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73   char *z = (cons
37510 74 20 63 68 61 72 2a 29 70 50 61 72 73 65 2d 3e  t char*)pParse->
37511 73 41 72 67 2e 7a 3b 0a 20 20 20 20 69 6e 74 20  sArg.z;.    int 
37512 6e 20 3d 20 70 50 61 72 73 65 2d 3e 73 41 72 67  n = pParse->sArg
37513 2e 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  .n;.    sqlite3 
37514 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
37515 3b 0a 20 20 20 20 61 64 64 4d 6f 64 75 6c 65 41  ;.    addModuleA
37516 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 50 61 72  rgument(db, pPar
37517 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2c 20 73  se->pNewTable, s
37518 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
37519 64 62 2c 20 7a 2c 20 6e 29 29 3b 0a 20 20 7d 0a  db, z, n));.  }.
3751a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72  }../*.** The par
3751b 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
3751c 6f 75 74 69 6e 65 20 61 66 74 65 72 20 74 68 65  outine after the
3751d 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
3751e 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 0a  TABLE statement.
3751f 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 6d 70  ** has been comp
37520 6c 65 74 65 6c 79 20 70 61 72 73 65 64 2e 0a 2a  letely parsed..*
37521 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
37522 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
37523 62 46 69 6e 69 73 68 50 61 72 73 65 28 50 61 72  bFinishParse(Par
37524 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
37525 6e 20 2a 70 45 6e 64 29 7b 0a 20 20 54 61 62 6c  n *pEnd){.  Tabl
37526 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72 73 65  e *pTab = pParse
37527 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 20 20 2f 2a  ->pNewTable;  /*
37528 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   The table being
37529 20 63 6f 6e 73 74 72 75 63 74 65 64 20 2a 2f 0a   constructed */.
3752a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
3752b 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
3752c 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
3752d 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
3752e 2f 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30  /..  if( pTab==0
3752f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 64 64   ) return;.  add
37530 41 72 67 75 6d 65 6e 74 54 6f 56 74 61 62 28 70  ArgumentToVtab(p
37531 50 61 72 73 65 29 3b 0a 20 20 70 50 61 72 73 65  Parse);.  pParse
37532 2d 3e 73 41 72 67 2e 7a 20 3d 20 30 3b 0a 20 20  ->sArg.z = 0;.  
37533 69 66 28 20 70 54 61 62 2d 3e 6e 4d 6f 64 75 6c  if( pTab->nModul
37534 65 41 72 67 3c 31 20 29 20 72 65 74 75 72 6e 3b  eArg<1 ) return;
37535 0a 20 20 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  .  .  /* If the 
37536 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
37537 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69  ABLE statement i
37538 73 20 62 65 69 6e 67 20 65 6e 74 65 72 65 64 20  s being entered 
37539 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 66 69 72  for the.  ** fir
3753a 73 74 20 74 69 6d 65 20 28 69 6e 20 6f 74 68 65  st time (in othe
3753b 72 20 77 6f 72 64 73 20 69 66 20 74 68 65 20 76  r words if the v
3753c 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20  irtual table is 
3753d 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 0a 20  actually being. 
3753e 20 2a 2a 20 63 72 65 61 74 65 64 20 6e 6f 77 20   ** created now 
3753f 69 6e 73 74 65 61 64 20 6f 66 20 6a 75 73 74 20  instead of just 
37540 62 65 69 6e 67 20 72 65 61 64 20 6f 75 74 20 6f  being read out o
37541 66 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 29  f sqlite_master)
37542 20 74 68 65 6e 0a 20 20 2a 2a 20 64 6f 20 61 64   then.  ** do ad
37543 64 69 74 69 6f 6e 61 6c 20 69 6e 69 74 69 61 6c  ditional initial
37544 69 7a 61 74 69 6f 6e 20 77 6f 72 6b 20 61 6e 64  ization work and
37545 20 73 74 6f 72 65 20 74 68 65 20 73 74 61 74 65   store the state
37546 6d 65 6e 74 20 74 65 78 74 0a 20 20 2a 2a 20 69  ment text.  ** i
37547 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  n the sqlite_mas
37548 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  ter table..  */.
37549 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
3754a 62 75 73 79 20 29 7b 0a 20 20 20 20 63 68 61 72  busy ){.    char
3754b 20 2a 7a 53 74 6d 74 3b 0a 20 20 20 20 63 68 61   *zStmt;.    cha
3754c 72 20 2a 7a 57 68 65 72 65 3b 0a 20 20 20 20 69  r *zWhere;.    i
3754d 6e 74 20 69 44 62 3b 0a 20 20 20 20 56 64 62 65  nt iDb;.    Vdbe
3754e 20 2a 76 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d   *v;..    /* Com
3754f 70 75 74 65 20 74 68 65 20 63 6f 6d 70 6c 65 74  pute the complet
37550 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43 52  e text of the CR
37551 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42  EATE VIRTUAL TAB
37552 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  LE statement */.
37553 20 20 20 20 69 66 28 20 70 45 6e 64 20 29 7b 0a      if( pEnd ){.
37554 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 73 4e        pParse->sN
37555 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 28 69 6e  ameToken.n = (in
37556 74 29 28 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61  t)(pEnd->z - pPa
37557 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e  rse->sNameToken.
37558 7a 29 20 2b 20 70 45 6e 64 2d 3e 6e 3b 0a 20 20  z) + pEnd->n;.  
37559 20 20 7d 0a 20 20 20 20 7a 53 74 6d 74 20 3d 20    }.    zStmt = 
3755a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
3755b 62 2c 20 22 43 52 45 41 54 45 20 56 49 52 54 55  b, "CREATE VIRTU
3755c 41 4c 20 54 41 42 4c 45 20 25 54 22 2c 20 26 70  AL TABLE %T", &p
3755d 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65  Parse->sNameToke
3755e 6e 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c  n);..    /* A sl
3755f 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72  ot for the recor
37560 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  d has already be
37561 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  en allocated in 
37562 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49  the .    ** SQLI
37563 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e  TE_MASTER table.
37564 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74    We just need t
37565 6f 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c  o update that sl
37566 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20  ot with all.    
37567 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69  ** the informati
37568 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74  on we've collect
37569 65 64 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20  ed.  .    **.   
3756a 20 2a 2a 20 54 68 65 20 56 4d 20 72 65 67 69 73   ** The VM regis
3756b 74 65 72 20 6e 75 6d 62 65 72 20 70 50 61 72 73  ter number pPars
3756c 65 2d 3e 72 65 67 52 6f 77 69 64 20 68 6f 6c 64  e->regRowid hold
3756d 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61  s the rowid of a
3756e 6e 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 20 69  n.    ** entry i
3756f 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  n the sqlite_mas
37570 74 65 72 20 74 61 62 6c 65 20 74 68 74 20 77 61  ter table tht wa
37571 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 74 68  s created for th
37572 69 73 20 76 74 61 62 0a 20 20 20 20 2a 2a 20 62  is vtab.    ** b
37573 79 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61  y sqlite3StartTa
37574 62 6c 65 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  ble()..    */.  
37575 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
37576 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
37577 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
37578 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
37579 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
3757a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51        "UPDATE %Q
3757b 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20 20 22  .%s ".         "
3757c 53 45 54 20 74 79 70 65 3d 27 74 61 62 6c 65 27  SET type='table'
3757d 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e  , name=%Q, tbl_n
3757e 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65  ame=%Q, rootpage
3757f 3d 30 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20 20  =0, sql=%Q ".   
37580 20 20 20 20 22 57 48 45 52 45 20 72 6f 77 69 64      "WHERE rowid
37581 3d 23 25 64 22 2c 0a 20 20 20 20 20 20 64 62 2d  =#%d",.      db-
37582 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
37583 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
37584 62 29 2c 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  b),.      pTab->
37585 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 54 61  zName,.      pTa
37586 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  b->zName,.      
37587 7a 53 74 6d 74 2c 0a 20 20 20 20 20 20 70 50 61  zStmt,.      pPa
37588 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a 20 20  rse->regRowid.  
37589 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33    );.    sqlite3
3758a 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74  DbFree(db, zStmt
3758b 29 3b 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74  );.    v = sqlit
3758c 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
3758d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
3758e 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
3758f 65 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 73 71  e, iDb);..    sq
37590 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
37591 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c  v, OP_Expire, 0,
37592 20 30 29 3b 0a 20 20 20 20 7a 57 68 65 72 65 20   0);.    zWhere 
37593 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
37594 28 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 20  (db, "name='%q' 
37595 41 4e 44 20 74 79 70 65 3d 27 74 61 62 6c 65 27  AND type='table'
37596 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  ", pTab->zName);
37597 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
37598 41 64 64 50 61 72 73 65 53 63 68 65 6d 61 4f 70  AddParseSchemaOp
37599 28 76 2c 20 69 44 62 2c 20 7a 57 68 65 72 65 29  (v, iDb, zWhere)
3759a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
3759b 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 43  eAddOp4(v, OP_VC
3759c 72 65 61 74 65 2c 20 69 44 62 2c 20 30 2c 20 30  reate, iDb, 0, 0
3759d 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
3759e 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62              pTab
3759f 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33  ->zName, sqlite3
375a0 53 74 72 6c 65 6e 33 30 28 70 54 61 62 2d 3e 7a  Strlen30(pTab->z
375a1 4e 61 6d 65 29 20 2b 20 31 29 3b 0a 20 20 7d 0a  Name) + 1);.  }.
375a2 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20  .  /* If we are 
375a3 72 65 72 65 61 64 69 6e 67 20 74 68 65 20 73 71  rereading the sq
375a4 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
375a5 65 20 63 72 65 61 74 65 20 74 68 65 20 69 6e 2d  e create the in-
375a6 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 72 65 63 6f  memory.  ** reco
375a7 72 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e  rd of the table.
375a8 20 54 68 65 20 78 43 6f 6e 6e 65 63 74 28 29 20   The xConnect() 
375a9 6d 65 74 68 6f 64 20 69 73 20 6e 6f 74 20 63 61  method is not ca
375aa 6c 6c 65 64 20 75 6e 74 69 6c 0a 20 20 2a 2a 20  lled until.  ** 
375ab 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74  the first time t
375ac 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
375ad 20 69 73 20 75 73 65 64 20 69 6e 20 61 6e 20 53   is used in an S
375ae 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68  QL statement. Th
375af 69 73 0a 20 20 2a 2a 20 61 6c 6c 6f 77 73 20 61  is.  ** allows a
375b0 20 73 63 68 65 6d 61 20 74 68 61 74 20 63 6f 6e   schema that con
375b1 74 61 69 6e 73 20 76 69 72 74 75 61 6c 20 74 61  tains virtual ta
375b2 62 6c 65 73 20 74 6f 20 62 65 20 6c 6f 61 64 65  bles to be loade
375b3 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 74 68  d before.  ** th
375b4 65 20 72 65 71 75 69 72 65 64 20 76 69 72 74 75  e required virtu
375b5 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65  al table impleme
375b6 6e 74 61 74 69 6f 6e 73 20 61 72 65 20 72 65 67  ntations are reg
375b7 69 73 74 65 72 65 64 2e 20 20 2a 2f 0a 20 20 65  istered.  */.  e
375b8 6c 73 65 20 7b 0a 20 20 20 20 54 61 62 6c 65 20  lse {.    Table 
375b9 2a 70 4f 6c 64 3b 0a 20 20 20 20 53 63 68 65 6d  *pOld;.    Schem
375ba 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 70 54 61  a *pSchema = pTa
375bb 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20  b->pSchema;.    
375bc 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
375bd 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b  e = pTab->zName;
375be 0a 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d  .    int nName =
375bf 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
375c0 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 61 73 73  (zName);.    ass
375c1 65 72 74 28 20 73 71 6c 69 74 65 33 53 63 68 65  ert( sqlite3Sche
375c2 6d 61 4d 75 74 65 78 48 65 6c 64 28 64 62 2c 20  maMutexHeld(db, 
375c3 30 2c 20 70 53 63 68 65 6d 61 29 20 29 3b 0a 20  0, pSchema) );. 
375c4 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65     pOld = sqlite
375c5 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 53 63  3HashInsert(&pSc
375c6 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a  hema->tblHash, z
375c7 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 54 61  Name, nName, pTa
375c8 62 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64  b);.    if( pOld
375c9 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61   ){.      db->ma
375ca 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
375cb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
375cc 61 62 3d 3d 70 4f 6c 64 20 29 3b 20 20 2f 2a 20  ab==pOld );  /* 
375cd 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65  Malloc must have
375ce 20 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 48   failed inside H
375cf 61 73 68 49 6e 73 65 72 74 28 29 20 2a 2f 0a 20  ashInsert() */. 
375d0 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
375d1 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70   }.    pParse->p
375d2 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20  NewTable = 0;.  
375d3 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  }.}../*.** The p
375d4 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73  arser calls this
375d5 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74   routine when it
375d6 20 73 65 65 73 20 74 68 65 20 66 69 72 73 74 20   sees the first 
375d7 74 6f 6b 65 6e 0a 2a 2a 20 6f 66 20 61 6e 20 61  token.** of an a
375d8 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 6d  rgument to the m
375d9 6f 64 75 6c 65 20 6e 61 6d 65 20 69 6e 20 61 20  odule name in a 
375da 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
375db 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a  ABLE statement..
375dc 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
375dd 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
375de 61 62 41 72 67 49 6e 69 74 28 50 61 72 73 65 20  abArgInit(Parse 
375df 2a 70 50 61 72 73 65 29 7b 0a 20 20 61 64 64 41  *pParse){.  addA
375e0 72 67 75 6d 65 6e 74 54 6f 56 74 61 62 28 70 50  rgumentToVtab(pP
375e1 61 72 73 65 29 3b 0a 20 20 70 50 61 72 73 65 2d  arse);.  pParse-
375e2 3e 73 41 72 67 2e 7a 20 3d 20 30 3b 0a 20 20 70  >sArg.z = 0;.  p
375e3 50 61 72 73 65 2d 3e 73 41 72 67 2e 6e 20 3d 20  Parse->sArg.n = 
375e4 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  0;.}../*.** The 
375e5 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
375e6 73 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 65 61  s routine for ea
375e7 63 68 20 74 6f 6b 65 6e 20 61 66 74 65 72 20 74  ch token after t
375e8 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 0a 2a  he first token.*
375e9 2a 20 69 6e 20 61 6e 20 61 72 67 75 6d 65 6e 74  * in an argument
375ea 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e   to the module n
375eb 61 6d 65 20 69 6e 20 61 20 43 52 45 41 54 45 20  ame in a CREATE 
375ec 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74  VIRTUAL TABLE st
375ed 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  atement..*/.SQLI
375ee 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
375ef 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 45 78  sqlite3VtabArgEx
375f0 74 65 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72  tend(Parse *pPar
375f1 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 29 7b 0a 20  se, Token *p){. 
375f2 20 54 6f 6b 65 6e 20 2a 70 41 72 67 20 3d 20 26   Token *pArg = &
375f3 70 50 61 72 73 65 2d 3e 73 41 72 67 3b 0a 20 20  pParse->sArg;.  
375f4 69 66 28 20 70 41 72 67 2d 3e 7a 3d 3d 30 20 29  if( pArg->z==0 )
375f5 7b 0a 20 20 20 20 70 41 72 67 2d 3e 7a 20 3d 20  {.    pArg->z = 
375f6 70 2d 3e 7a 3b 0a 20 20 20 20 70 41 72 67 2d 3e  p->z;.    pArg->
375f7 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 65 6c 73  n = p->n;.  }els
375f8 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 70 41  e{.    assert(pA
375f9 72 67 2d 3e 7a 20 3c 20 70 2d 3e 7a 29 3b 0a 20  rg->z < p->z);. 
375fa 20 20 20 70 41 72 67 2d 3e 6e 20 3d 20 28 69 6e     pArg->n = (in
375fb 74 29 28 26 70 2d 3e 7a 5b 70 2d 3e 6e 5d 20 2d  t)(&p->z[p->n] -
375fc 20 70 41 72 67 2d 3e 7a 29 3b 0a 20 20 7d 0a 7d   pArg->z);.  }.}
375fd 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61  ../*.** Invoke a
375fe 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63   virtual table c
375ff 6f 6e 73 74 72 75 63 74 6f 72 20 28 65 69 74 68  onstructor (eith
37600 65 72 20 78 43 72 65 61 74 65 20 6f 72 20 78 43  er xCreate or xC
37601 6f 6e 6e 65 63 74 29 2e 20 54 68 65 0a 2a 2a 20  onnect). The.** 
37602 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66  pointer to the f
37603 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b  unction to invok
37604 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  e is passed as t
37605 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65  he fourth parame
37606 74 65 72 0a 2a 2a 20 74 6f 20 74 68 69 73 20 70  ter.** to this p
37607 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61  rocedure..*/.sta
37608 74 69 63 20 69 6e 74 20 76 74 61 62 43 61 6c 6c  tic int vtabCall
37609 43 6f 6e 73 74 72 75 63 74 6f 72 28 0a 20 20 73  Constructor(.  s
3760a 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 54  qlite3 *db, .  T
3760b 61 62 6c 65 20 2a 70 54 61 62 2c 0a 20 20 4d 6f  able *pTab,.  Mo
3760c 64 75 6c 65 20 2a 70 4d 6f 64 2c 0a 20 20 69 6e  dule *pMod,.  in
3760d 74 20 28 2a 78 43 6f 6e 73 74 72 75 63 74 29 28  t (*xConstruct)(
3760e 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69  sqlite3*,void*,i
3760f 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 63 6f  nt,const char*co
37610 6e 73 74 2a 2c 73 71 6c 69 74 65 33 5f 76 74 61  nst*,sqlite3_vta
37611 62 2a 2a 2c 63 68 61 72 2a 2a 29 2c 0a 20 20 63  b**,char**),.  c
37612 68 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20  har **pzErr.){. 
37613 20 56 74 61 62 43 74 78 20 73 43 74 78 2c 20 2a   VtabCtx sCtx, *
37614 70 50 72 69 6f 72 43 74 78 3b 0a 20 20 56 54 61  pPriorCtx;.  VTa
37615 62 6c 65 20 2a 70 56 54 61 62 6c 65 3b 0a 20 20  ble *pVTable;.  
37616 69 6e 74 20 72 63 3b 0a 20 20 63 6f 6e 73 74 20  int rc;.  const 
37617 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 7a 41 72  char *const*azAr
37618 67 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  g = (const char 
37619 2a 63 6f 6e 73 74 2a 29 70 54 61 62 2d 3e 61 7a  *const*)pTab->az
3761a 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20 69 6e 74  ModuleArg;.  int
3761b 20 6e 41 72 67 20 3d 20 70 54 61 62 2d 3e 6e 4d   nArg = pTab->nM
3761c 6f 64 75 6c 65 41 72 67 3b 0a 20 20 63 68 61 72  oduleArg;.  char
3761d 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20 63 68   *zErr = 0;.  ch
3761e 61 72 20 2a 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20  ar *zModuleName 
3761f 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
37620 28 64 62 2c 20 22 25 73 22 2c 20 70 54 61 62 2d  (db, "%s", pTab-
37621 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 69  >zName);.  int i
37622 44 62 3b 0a 0a 20 20 69 66 28 20 21 7a 4d 6f 64  Db;..  if( !zMod
37623 75 6c 65 4e 61 6d 65 20 29 7b 0a 20 20 20 20 72  uleName ){.    r
37624 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
37625 45 4d 3b 0a 20 20 7d 0a 0a 20 20 70 56 54 61 62  EM;.  }..  pVTab
37626 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  le = sqlite3DbMa
37627 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
37628 65 6f 66 28 56 54 61 62 6c 65 29 29 3b 0a 20 20  eof(VTable));.  
37629 69 66 28 20 21 70 56 54 61 62 6c 65 20 29 7b 0a  if( !pVTable ){.
3762a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
3762b 65 28 64 62 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d  e(db, zModuleNam
3762c 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  e);.    return S
3762d 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
3762e 0a 20 20 70 56 54 61 62 6c 65 2d 3e 64 62 20 3d  .  pVTable->db =
3762f 20 64 62 3b 0a 20 20 70 56 54 61 62 6c 65 2d 3e   db;.  pVTable->
37630 70 4d 6f 64 20 3d 20 70 4d 6f 64 3b 0a 0a 20 20  pMod = pMod;..  
37631 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
37632 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
37633 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
37634 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41   pTab->azModuleA
37635 72 67 5b 31 5d 20 3d 20 64 62 2d 3e 61 44 62 5b  rg[1] = db->aDb[
37636 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 2f  iDb].zName;..  /
37637 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 76 69 72  * Invoke the vir
37638 74 75 61 6c 20 74 61 62 6c 65 20 63 6f 6e 73 74  tual table const
37639 72 75 63 74 6f 72 20 2a 2f 0a 20 20 61 73 73 65  ructor */.  asse
3763a 72 74 28 20 26 64 62 2d 3e 70 56 74 61 62 43 74  rt( &db->pVtabCt
3763b 78 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78  x );.  assert( x
3763c 43 6f 6e 73 74 72 75 63 74 20 29 3b 0a 20 20 73  Construct );.  s
3763d 43 74 78 2e 70 54 61 62 20 3d 20 70 54 61 62 3b  Ctx.pTab = pTab;
3763e 0a 20 20 73 43 74 78 2e 70 56 54 61 62 6c 65 20  .  sCtx.pVTable 
3763f 3d 20 70 56 54 61 62 6c 65 3b 0a 20 20 70 50 72  = pVTable;.  pPr
37640 69 6f 72 43 74 78 20 3d 20 64 62 2d 3e 70 56 74  iorCtx = db->pVt
37641 61 62 43 74 78 3b 0a 20 20 64 62 2d 3e 70 56 74  abCtx;.  db->pVt
37642 61 62 43 74 78 20 3d 20 26 73 43 74 78 3b 0a 20  abCtx = &sCtx;. 
37643 20 72 63 20 3d 20 78 43 6f 6e 73 74 72 75 63 74   rc = xConstruct
37644 28 64 62 2c 20 70 4d 6f 64 2d 3e 70 41 75 78 2c  (db, pMod->pAux,
37645 20 6e 41 72 67 2c 20 61 7a 41 72 67 2c 20 26 70   nArg, azArg, &p
37646 56 54 61 62 6c 65 2d 3e 70 56 74 61 62 2c 20 26  VTable->pVtab, &
37647 7a 45 72 72 29 3b 0a 20 20 64 62 2d 3e 70 56 74  zErr);.  db->pVt
37648 61 62 43 74 78 20 3d 20 70 50 72 69 6f 72 43 74  abCtx = pPriorCt
37649 78 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  x;.  if( rc==SQL
3764a 49 54 45 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d 3e  ITE_NOMEM ) db->
3764b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
3764c 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  ;..  if( SQLITE_
3764d 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 69 66  OK!=rc ){.    if
3764e 28 20 7a 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20  ( zErr==0 ){.   
3764f 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69     *pzErr = sqli
37650 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
37651 76 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63 74  vtable construct
37652 6f 72 20 66 61 69 6c 65 64 3a 20 25 73 22 2c 20  or failed: %s", 
37653 7a 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a 20 20  zModuleName);.  
37654 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20 20 20    }else {.      
37655 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33  *pzErr = sqlite3
37656 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22  MPrintf(db, "%s"
37657 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 73  , zErr);.      s
37658 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72  qlite3_free(zErr
37659 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
3765a 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
3765b 56 54 61 62 6c 65 29 3b 0a 20 20 7d 65 6c 73 65  VTable);.  }else
3765c 20 69 66 28 20 41 4c 57 41 59 53 28 70 56 54 61   if( ALWAYS(pVTa
3765d 62 6c 65 2d 3e 70 56 74 61 62 29 20 29 7b 0a 20  ble->pVtab) ){. 
3765e 20 20 20 2f 2a 20 4a 75 73 74 69 66 69 63 61 74     /* Justificat
3765f 69 6f 6e 20 6f 66 20 41 4c 57 41 59 53 28 29 3a  ion of ALWAYS():
37660 20 20 41 20 63 6f 72 72 65 63 74 20 76 74 61 62    A correct vtab
37661 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 6d 75 73   constructor mus
37662 74 20 61 6c 6c 6f 63 61 74 65 0a 20 20 20 20 2a  t allocate.    *
37663 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74  * the sqlite3_vt
37664 61 62 20 6f 62 6a 65 63 74 20 69 66 20 73 75 63  ab object if suc
37665 63 65 73 73 66 75 6c 2e 20 20 2a 2f 0a 20 20 20  cessful.  */.   
37666 20 70 56 54 61 62 6c 65 2d 3e 70 56 74 61 62 2d   pVTable->pVtab-
37667 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 2d  >pModule = pMod-
37668 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70 56  >pModule;.    pV
37669 54 61 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b  Table->nRef = 1;
3766a 0a 20 20 20 20 69 66 28 20 73 43 74 78 2e 70 54  .    if( sCtx.pT
3766b 61 62 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73  ab ){.      cons
3766c 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20  t char *zFormat 
3766d 3d 20 22 76 74 61 62 6c 65 20 63 6f 6e 73 74 72  = "vtable constr
3766e 75 63 74 6f 72 20 64 69 64 20 6e 6f 74 20 64 65  uctor did not de
3766f 63 6c 61 72 65 20 73 63 68 65 6d 61 3a 20 25 73  clare schema: %s
37670 22 3b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20  ";.      *pzErr 
37671 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
37672 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 70 54  (db, zFormat, pT
37673 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
37674 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c    sqlite3VtabUnl
37675 6f 63 6b 28 70 56 54 61 62 6c 65 29 3b 0a 20 20  ock(pVTable);.  
37676 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
37677 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65  ERROR;.    }else
37678 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  {.      int iCol
37679 3b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 65 76  ;.      /* If ev
3767a 65 72 79 74 68 69 6e 67 20 77 65 6e 74 20 61 63  erything went ac
3767b 63 6f 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e 2c  cording to plan,
3767c 20 6c 69 6e 6b 20 74 68 65 20 6e 65 77 20 56 54   link the new VT
3767d 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 0a 20  able structure. 
3767e 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65       ** into the
3767f 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61   linked list hea
37680 64 65 64 20 62 79 20 70 54 61 62 2d 3e 70 56 54  ded by pTab->pVT
37681 61 62 6c 65 2e 20 54 68 65 6e 20 6c 6f 6f 70 20  able. Then loop 
37682 74 68 72 6f 75 67 68 20 74 68 65 20 0a 20 20 20  through the .   
37683 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66     ** columns of
37684 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 73 65   the table to se
37685 65 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 6d  e if any of them
37686 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 74 6f 6b   contain the tok
37687 65 6e 20 22 68 69 64 64 65 6e 22 2e 0a 20 20 20  en "hidden"..   
37688 20 20 20 2a 2a 20 49 66 20 73 6f 2c 20 73 65 74     ** If so, set
37689 20 74 68 65 20 43 6f 6c 75 6d 6e 20 43 4f 4c 46   the Column COLF
3768a 4c 41 47 5f 48 49 44 44 45 4e 20 66 6c 61 67 20  LAG_HIDDEN flag 
3768b 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65 20 74  and remove the t
3768c 6f 6b 65 6e 20 66 72 6f 6d 0a 20 20 20 20 20 20  oken from.      
3768d 2a 2a 20 74 68 65 20 74 79 70 65 20 73 74 72 69  ** the type stri
3768e 6e 67 2e 20 20 2a 2f 0a 20 20 20 20 20 20 70 56  ng.  */.      pV
3768f 54 61 62 6c 65 2d 3e 70 4e 65 78 74 20 3d 20 70  Table->pNext = p
37690 54 61 62 2d 3e 70 56 54 61 62 6c 65 3b 0a 20 20  Tab->pVTable;.  
37691 20 20 20 20 70 54 61 62 2d 3e 70 56 54 61 62 6c      pTab->pVTabl
37692 65 20 3d 20 70 56 54 61 62 6c 65 3b 0a 0a 20 20  e = pVTable;..  
37693 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20      for(iCol=0; 
37694 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  iCol<pTab->nCol;
37695 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20   iCol++){.      
37696 20 20 63 68 61 72 20 2a 7a 54 79 70 65 20 3d 20    char *zType = 
37697 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  pTab->aCol[iCol]
37698 2e 7a 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20  .zType;.        
37699 69 6e 74 20 6e 54 79 70 65 3b 0a 20 20 20 20 20  int nType;.     
3769a 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20     int i = 0;.  
3769b 20 20 20 20 20 20 69 66 28 20 21 7a 54 79 70 65        if( !zType
3769c 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
3769d 20 20 20 20 20 6e 54 79 70 65 20 3d 20 73 71 6c       nType = sql
3769e 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 79  ite3Strlen30(zTy
3769f 70 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  pe);.        if(
376a0 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
376a1 28 22 68 69 64 64 65 6e 22 2c 20 7a 54 79 70 65  ("hidden", zType
376a2 2c 20 36 29 7c 7c 28 7a 54 79 70 65 5b 36 5d 20  , 6)||(zType[6] 
376a3 26 26 20 7a 54 79 70 65 5b 36 5d 21 3d 27 20 27  && zType[6]!=' '
376a4 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  ) ){.          f
376a5 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 79 70 65 3b  or(i=0; i<nType;
376a6 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
376a7 20 20 20 69 66 28 20 28 30 3d 3d 73 71 6c 69 74     if( (0==sqlit
376a8 65 33 53 74 72 4e 49 43 6d 70 28 22 20 68 69 64  e3StrNICmp(" hid
376a9 64 65 6e 22 2c 20 26 7a 54 79 70 65 5b 69 5d 2c  den", &zType[i],
376aa 20 37 29 29 0a 20 20 20 20 20 20 20 20 20 20 20   7)).           
376ab 20 20 26 26 20 28 7a 54 79 70 65 5b 69 2b 37 5d    && (zType[i+7]
376ac 3d 3d 27 5c 30 27 20 7c 7c 20 7a 54 79 70 65 5b  =='\0' || zType[
376ad 69 2b 37 5d 3d 3d 27 20 27 29 0a 20 20 20 20 20  i+7]==' ').     
376ae 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20         ){.      
376af 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
376b0 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
376b1 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
376b2 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
376b3 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
376b4 28 20 69 3c 6e 54 79 70 65 20 29 7b 0a 20 20 20  ( i<nType ){.   
376b5 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20         int j;.  
376b6 20 20 20 20 20 20 20 20 69 6e 74 20 6e 44 65 6c          int nDel
376b7 20 3d 20 36 20 2b 20 28 7a 54 79 70 65 5b 69 2b   = 6 + (zType[i+
376b8 36 5d 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20  6] ? 1 : 0);.   
376b9 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 69 3b 20         for(j=i; 
376ba 28 6a 2b 6e 44 65 6c 29 3c 3d 6e 54 79 70 65 3b  (j+nDel)<=nType;
376bb 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   j++){.         
376bc 20 20 20 7a 54 79 70 65 5b 6a 5d 20 3d 20 7a 54     zType[j] = zT
376bd 79 70 65 5b 6a 2b 6e 44 65 6c 5d 3b 0a 20 20 20  ype[j+nDel];.   
376be 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
376bf 20 20 20 69 66 28 20 7a 54 79 70 65 5b 69 5d 3d     if( zType[i]=
376c0 3d 27 5c 30 27 20 26 26 20 69 3e 30 20 29 7b 0a  ='\0' && i>0 ){.
376c1 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
376c2 72 74 28 7a 54 79 70 65 5b 69 2d 31 5d 3d 3d 27  rt(zType[i-1]=='
376c3 20 27 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   ');.           
376c4 20 7a 54 79 70 65 5b 69 2d 31 5d 20 3d 20 27 5c   zType[i-1] = '\
376c5 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  0';.          }.
376c6 20 20 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e            pTab->
376c7 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 63 6f 6c 46 6c  aCol[iCol].colFl
376c8 61 67 73 20 7c 3d 20 43 4f 4c 46 4c 41 47 5f 48  ags |= COLFLAG_H
376c9 49 44 44 45 4e 3b 0a 20 20 20 20 20 20 20 20 7d  IDDEN;.        }
376ca 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
376cb 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 44 62 46   }..  sqlite3DbF
376cc 72 65 65 28 64 62 2c 20 7a 4d 6f 64 75 6c 65 4e  ree(db, zModuleN
376cd 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ame);.  return r
376ce 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
376cf 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76   function is inv
376d0 6f 6b 65 64 20 62 79 20 74 68 65 20 70 61 72 73  oked by the pars
376d1 65 72 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78  er to call the x
376d2 43 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64  Connect() method
376d3 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69 72 74 75  .** of the virtu
376d4 61 6c 20 74 61 62 6c 65 20 70 54 61 62 2e 20 49  al table pTab. I
376d5 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
376d6 73 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  s, an error code
376d7 20 69 73 20 72 65 74 75 72 6e 65 64 20 0a 2a 2a   is returned .**
376d8 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6c 65   and an error le
376d9 66 74 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2a  ft in pParse..**
376da 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73  .** This call is
376db 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 61 62 6c   a no-op if tabl
376dc 65 20 70 54 61 62 20 69 73 20 6e 6f 74 20 61 20  e pTab is not a 
376dd 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
376de 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
376df 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
376e0 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73  CallConnect(Pars
376e1 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65  e *pParse, Table
376e2 20 2a 70 54 61 62 29 7b 0a 20 20 73 71 6c 69 74   *pTab){.  sqlit
376e3 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
376e4 3e 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  >db;.  const cha
376e5 72 20 2a 7a 4d 6f 64 3b 0a 20 20 4d 6f 64 75 6c  r *zMod;.  Modul
376e6 65 20 2a 70 4d 6f 64 3b 0a 20 20 69 6e 74 20 72  e *pMod;.  int r
376e7 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54  c;..  assert( pT
376e8 61 62 20 29 3b 0a 20 20 69 66 28 20 28 70 54 61  ab );.  if( (pTa
376e9 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
376ea 5f 56 69 72 74 75 61 6c 29 3d 3d 30 20 7c 7c 20  _Virtual)==0 || 
376eb 73 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65  sqlite3GetVTable
376ec 28 64 62 2c 20 70 54 61 62 29 20 29 7b 0a 20 20  (db, pTab) ){.  
376ed 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
376ee 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f  OK;.  }..  /* Lo
376ef 63 61 74 65 20 74 68 65 20 72 65 71 75 69 72 65  cate the require
376f0 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  d virtual table 
376f1 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 7a 4d 6f 64  module */.  zMod
376f2 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c   = pTab->azModul
376f3 65 41 72 67 5b 30 5d 3b 0a 20 20 70 4d 6f 64 20  eArg[0];.  pMod 
376f4 3d 20 28 4d 6f 64 75 6c 65 2a 29 73 71 6c 69 74  = (Module*)sqlit
376f5 65 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e  e3HashFind(&db->
376f6 61 4d 6f 64 75 6c 65 2c 20 7a 4d 6f 64 2c 20 73  aModule, zMod, s
376f7 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
376f8 4d 6f 64 29 29 3b 0a 0a 20 20 69 66 28 20 21 70  Mod));..  if( !p
376f9 4d 6f 64 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  Mod ){.    const
376fa 20 63 68 61 72 20 2a 7a 4d 6f 64 75 6c 65 20 3d   char *zModule =
376fb 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41   pTab->azModuleA
376fc 72 67 5b 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74  rg[0];.    sqlit
376fd 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
376fe 65 2c 20 22 6e 6f 20 73 75 63 68 20 6d 6f 64 75  e, "no such modu
376ff 6c 65 3a 20 25 73 22 2c 20 7a 4d 6f 64 75 6c 65  le: %s", zModule
37700 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
37701 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73  TE_ERROR;.  }els
37702 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72  e{.    char *zEr
37703 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  r = 0;.    rc = 
37704 76 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63  vtabCallConstruc
37705 74 6f 72 28 64 62 2c 20 70 54 61 62 2c 20 70 4d  tor(db, pTab, pM
37706 6f 64 2c 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c  od, pMod->pModul
37707 65 2d 3e 78 43 6f 6e 6e 65 63 74 2c 20 26 7a 45  e->xConnect, &zE
37708 72 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  rr);.    if( rc!
37709 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
3770a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
3770b 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 22  Msg(pParse, "%s"
3770c 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20  , zErr);.    }. 
3770d 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
3770e 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a  (db, zErr);.  }.
3770f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
37710 2f 2a 0a 2a 2a 20 47 72 6f 77 20 74 68 65 20 64  /*.** Grow the d
37711 62 2d 3e 61 56 54 72 61 6e 73 5b 5d 20 61 72 72  b->aVTrans[] arr
37712 61 79 20 73 6f 20 74 68 61 74 20 74 68 65 72 65  ay so that there
37713 20 69 73 20 72 6f 6f 6d 20 66 6f 72 20 61 74 20   is room for at 
37714 6c 65 61 73 74 20 6f 6e 65 0a 2a 2a 20 6d 6f 72  least one.** mor
37715 65 20 76 2d 74 61 62 6c 65 2e 20 52 65 74 75 72  e v-table. Retur
37716 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  n SQLITE_NOMEM i
37717 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73  f a malloc fails
37718 2c 20 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  , or SQLITE_OK o
37719 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61  therwise..*/.sta
3771a 74 69 63 20 69 6e 74 20 67 72 6f 77 56 54 72 61  tic int growVTra
3771b 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ns(sqlite3 *db){
3771c 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 41 52 52  .  const int ARR
3771d 41 59 5f 49 4e 43 52 20 3d 20 35 3b 0a 0a 20 20  AY_INCR = 5;..  
3771e 2f 2a 20 47 72 6f 77 20 74 68 65 20 73 71 6c 69  /* Grow the sqli
3771f 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61  te3.aVTrans arra
37720 79 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f  y if required */
37721 0a 20 20 69 66 28 20 28 64 62 2d 3e 6e 56 54 72  .  if( (db->nVTr
37722 61 6e 73 25 41 52 52 41 59 5f 49 4e 43 52 29 3d  ans%ARRAY_INCR)=
37723 3d 30 20 29 7b 0a 20 20 20 20 56 54 61 62 6c 65  =0 ){.    VTable
37724 20 2a 2a 61 56 54 72 61 6e 73 3b 0a 20 20 20 20   **aVTrans;.    
37725 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69 7a  int nBytes = siz
37726 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62  eof(sqlite3_vtab
37727 20 2a 29 20 2a 20 28 64 62 2d 3e 6e 56 54 72 61   *) * (db->nVTra
37728 6e 73 20 2b 20 41 52 52 41 59 5f 49 4e 43 52 29  ns + ARRAY_INCR)
37729 3b 0a 20 20 20 20 61 56 54 72 61 6e 73 20 3d 20  ;.    aVTrans = 
3772a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
3772b 28 64 62 2c 20 28 76 6f 69 64 20 2a 29 64 62 2d  (db, (void *)db-
3772c 3e 61 56 54 72 61 6e 73 2c 20 6e 42 79 74 65 73  >aVTrans, nBytes
3772d 29 3b 0a 20 20 20 20 69 66 28 20 21 61 56 54 72  );.    if( !aVTr
3772e 61 6e 73 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ans ){.      ret
3772f 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
37730 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73  ;.    }.    mems
37731 65 74 28 26 61 56 54 72 61 6e 73 5b 64 62 2d 3e  et(&aVTrans[db->
37732 6e 56 54 72 61 6e 73 5d 2c 20 30 2c 20 73 69 7a  nVTrans], 0, siz
37733 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62  eof(sqlite3_vtab
37734 20 2a 29 2a 41 52 52 41 59 5f 49 4e 43 52 29 3b   *)*ARRAY_INCR);
37735 0a 20 20 20 20 64 62 2d 3e 61 56 54 72 61 6e 73  .    db->aVTrans
37736 20 3d 20 61 56 54 72 61 6e 73 3b 0a 20 20 7d 0a   = aVTrans;.  }.
37737 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
37738 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  _OK;.}../*.** Ad
37739 64 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  d the virtual ta
3773a 62 6c 65 20 70 56 54 61 62 20 74 6f 20 74 68 65  ble pVTab to the
3773b 20 61 72 72 61 79 20 73 71 6c 69 74 65 33 2e 61   array sqlite3.a
3773c 56 54 72 61 6e 73 5b 5d 2e 20 53 70 61 63 65 20  VTrans[]. Space 
3773d 73 68 6f 75 6c 64 0a 2a 2a 20 68 61 76 65 20 61  should.** have a
3773e 6c 72 65 61 64 79 20 62 65 65 6e 20 72 65 73 65  lready been rese
3773f 72 76 65 64 20 75 73 69 6e 67 20 67 72 6f 77 56  rved using growV
37740 54 72 61 6e 73 28 29 2e 0a 2a 2f 0a 73 74 61 74  Trans()..*/.stat
37741 69 63 20 76 6f 69 64 20 61 64 64 54 6f 56 54 72  ic void addToVTr
37742 61 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ans(sqlite3 *db,
37743 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 29 7b   VTable *pVTab){
37744 0a 20 20 2f 2a 20 41 64 64 20 70 56 74 61 62 20  .  /* Add pVtab 
37745 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73 71  to the end of sq
37746 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 2a 2f  lite3.aVTrans */
37747 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 64  .  db->aVTrans[d
37748 62 2d 3e 6e 56 54 72 61 6e 73 2b 2b 5d 20 3d 20  b->nVTrans++] = 
37749 70 56 54 61 62 3b 0a 20 20 73 71 6c 69 74 65 33  pVTab;.  sqlite3
3774a 56 74 61 62 4c 6f 63 6b 28 70 56 54 61 62 29 3b  VtabLock(pVTab);
3774b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
3774c 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b  unction is invok
3774d 65 64 20 62 79 20 74 68 65 20 76 64 62 65 20 74  ed by the vdbe t
3774e 6f 20 63 61 6c 6c 20 74 68 65 20 78 43 72 65 61  o call the xCrea
3774f 74 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20  te method.** of 
37750 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
37751 65 20 6e 61 6d 65 64 20 7a 54 61 62 20 69 6e 20  e named zTab in 
37752 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 0a 2a  database iDb. .*
37753 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
37754 20 6f 63 63 75 72 73 2c 20 2a 70 7a 45 72 72 20   occurs, *pzErr 
37755 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
37756 61 6e 20 61 6e 20 45 6e 67 6c 69 73 68 20 6c 61  an an English la
37757 6e 67 75 61 67 65 0a 2a 2a 20 64 65 73 63 72 69  nguage.** descri
37758 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 65 72 72  ption of the err
37759 6f 72 20 61 6e 64 20 61 6e 20 53 51 4c 49 54 45  or and an SQLITE
3775a 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65 20  _XXX error code 
3775b 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
3775c 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
3775d 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c   caller must cal
3775e 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  l sqlite3DbFree(
3775f 64 62 2c 20 29 20 6f 6e 20 2a 70 7a 45 72 72 2e  db, ) on *pzErr.
37760 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
37761 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74  TE int sqlite3Vt
37762 61 62 43 61 6c 6c 43 72 65 61 74 65 28 73 71 6c  abCallCreate(sql
37763 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44  ite3 *db, int iD
37764 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
37765 54 61 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72  Tab, char **pzEr
37766 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  r){.  int rc = S
37767 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c  QLITE_OK;.  Tabl
37768 65 20 2a 70 54 61 62 3b 0a 20 20 4d 6f 64 75 6c  e *pTab;.  Modul
37769 65 20 2a 70 4d 6f 64 3b 0a 20 20 63 6f 6e 73 74  e *pMod;.  const
3776a 20 63 68 61 72 20 2a 7a 4d 6f 64 3b 0a 0a 20 20   char *zMod;..  
3776b 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69  pTab = sqlite3Fi
3776c 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62  ndTable(db, zTab
3776d 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  , db->aDb[iDb].z
3776e 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 28  Name);.  assert(
3776f 20 70 54 61 62 20 26 26 20 28 70 54 61 62 2d 3e   pTab && (pTab->
37770 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69  tabFlags & TF_Vi
37771 72 74 75 61 6c 29 21 3d 30 20 26 26 20 21 70 54  rtual)!=0 && !pT
37772 61 62 2d 3e 70 56 54 61 62 6c 65 20 29 3b 0a 0a  ab->pVTable );..
37773 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20    /* Locate the 
37774 72 65 71 75 69 72 65 64 20 76 69 72 74 75 61 6c  required virtual
37775 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 2a 2f   table module */
37776 0a 20 20 7a 4d 6f 64 20 3d 20 70 54 61 62 2d 3e  .  zMod = pTab->
37777 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0a  azModuleArg[0];.
37778 20 20 70 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65    pMod = (Module
37779 2a 29 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  *)sqlite3HashFin
3777a 64 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20  d(&db->aModule, 
3777b 7a 4d 6f 64 2c 20 73 71 6c 69 74 65 33 53 74 72  zMod, sqlite3Str
3777c 6c 65 6e 33 30 28 7a 4d 6f 64 29 29 3b 0a 0a 20  len30(zMod));.. 
3777d 20 2f 2a 20 49 66 20 74 68 65 20 6d 6f 64 75 6c   /* If the modul
3777e 65 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 73  e has been regis
3777f 74 65 72 65 64 20 61 6e 64 20 69 6e 63 6c 75 64  tered and includ
37780 65 73 20 61 20 43 72 65 61 74 65 20 6d 65 74 68  es a Create meth
37781 6f 64 2c 20 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65  od, .  ** invoke
37782 20 69 74 20 6e 6f 77 2e 20 49 66 20 74 68 65 20   it now. If the 
37783 6d 6f 64 75 6c 65 20 68 61 73 20 6e 6f 74 20 62  module has not b
37784 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 2c 20  een registered, 
37785 72 65 74 75 72 6e 20 61 6e 20 0a 20 20 2a 2a 20  return an .  ** 
37786 65 72 72 6f 72 2e 20 4f 74 68 65 72 77 69 73 65  error. Otherwise
37787 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a 20 20  , do nothing..  
37788 2a 2f 0a 20 20 69 66 28 20 21 70 4d 6f 64 20 29  */.  if( !pMod )
37789 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73  {.    *pzErr = s
3778a 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
3778b 2c 20 22 6e 6f 20 73 75 63 68 20 6d 6f 64 75 6c  , "no such modul
3778c 65 3a 20 25 73 22 2c 20 7a 4d 6f 64 29 3b 0a 20  e: %s", zMod);. 
3778d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
3778e 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  RROR;.  }else{. 
3778f 20 20 20 72 63 20 3d 20 76 74 61 62 43 61 6c 6c     rc = vtabCall
37790 43 6f 6e 73 74 72 75 63 74 6f 72 28 64 62 2c 20  Constructor(db, 
37791 70 54 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f 64  pTab, pMod, pMod
37792 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65 61  ->pModule->xCrea
37793 74 65 2c 20 70 7a 45 72 72 29 3b 0a 20 20 7d 0a  te, pzErr);.  }.
37794 0a 20 20 2f 2a 20 4a 75 73 74 69 66 69 63 61 74  .  /* Justificat
37795 69 6f 6e 20 6f 66 20 41 4c 57 41 59 53 28 29 3a  ion of ALWAYS():
37796 20 20 54 68 65 20 78 43 6f 6e 73 74 72 75 63 74    The xConstruct
37797 6f 72 20 6d 65 74 68 6f 64 20 69 73 20 72 65 71  or method is req
37798 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72  uired to.  ** cr
37799 65 61 74 65 20 61 20 76 61 6c 69 64 20 73 71 6c  eate a valid sql
3779a 69 74 65 33 5f 76 74 61 62 20 69 66 20 69 74 20  ite3_vtab if it 
3779b 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
3779c 4b 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  K. */.  if( rc==
3779d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 41 4c 57  SQLITE_OK && ALW
3779e 41 59 53 28 73 71 6c 69 74 65 33 47 65 74 56 54  AYS(sqlite3GetVT
3779f 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 29 20  able(db, pTab)) 
377a0 29 7b 0a 20 20 20 20 72 63 20 3d 20 67 72 6f 77  ){.    rc = grow
377a1 56 54 72 61 6e 73 28 64 62 29 3b 0a 20 20 20 20  VTrans(db);.    
377a2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
377a3 4b 20 29 7b 0a 20 20 20 20 20 20 61 64 64 54 6f  K ){.      addTo
377a4 56 54 72 61 6e 73 28 64 62 2c 20 73 71 6c 69 74  VTrans(db, sqlit
377a5 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20  e3GetVTable(db, 
377a6 70 54 61 62 29 29 3b 0a 20 20 20 20 7d 0a 20 20  pTab));.    }.  
377a7 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
377a8 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
377a9 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
377aa 6f 20 73 65 74 20 74 68 65 20 73 63 68 65 6d 61  o set the schema
377ab 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 61   of a virtual ta
377ac 62 6c 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79  ble.  It is only
377ad 0a 2a 2a 20 76 61 6c 69 64 20 74 6f 20 63 61 6c  .** valid to cal
377ae 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  l this function 
377af 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20  from within the 
377b0 78 43 72 65 61 74 65 28 29 20 6f 72 20 78 43 6f  xCreate() or xCo
377b1 6e 6e 65 63 74 28 29 20 6f 66 20 61 0a 2a 2a 20  nnect() of a.** 
377b2 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
377b3 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dule..*/.SQLITE_
377b4 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
377b5 64 65 63 6c 61 72 65 5f 76 74 61 62 28 73 71 6c  declare_vtab(sql
377b6 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
377b7 63 68 61 72 20 2a 7a 43 72 65 61 74 65 54 61 62  char *zCreateTab
377b8 6c 65 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  le){.  Parse *pP
377b9 61 72 73 65 3b 0a 0a 20 20 69 6e 74 20 72 63 20  arse;..  int rc 
377ba 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54  = SQLITE_OK;.  T
377bb 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 63 68  able *pTab;.  ch
377bc 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 0a 20  ar *zErr = 0;.. 
377bd 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
377be 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
377bf 0a 20 20 69 66 28 20 21 64 62 2d 3e 70 56 74 61  .  if( !db->pVta
377c0 62 43 74 78 20 7c 7c 20 21 28 70 54 61 62 20 3d  bCtx || !(pTab =
377c1 20 64 62 2d 3e 70 56 74 61 62 43 74 78 2d 3e 70   db->pVtabCtx->p
377c2 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  Tab) ){.    sqli
377c3 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
377c4 49 54 45 5f 4d 49 53 55 53 45 2c 20 30 29 3b 0a  ITE_MISUSE, 0);.
377c5 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
377c6 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
377c7 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  x);.    return S
377c8 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b 50  QLITE_MISUSE_BKP
377c9 54 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  T;.  }.  assert(
377ca 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73   (pTab->tabFlags
377cb 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d   & TF_Virtual)!=
377cc 30 20 29 3b 0a 0a 20 20 70 50 61 72 73 65 20 3d  0 );..  pParse =
377cd 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c   sqlite3StackAll
377ce 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
377cf 66 28 2a 70 50 61 72 73 65 29 29 3b 0a 20 20 69  f(*pParse));.  i
377d0 66 28 20 70 50 61 72 73 65 3d 3d 30 20 29 7b 0a  f( pParse==0 ){.
377d1 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
377d2 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  NOMEM;.  }else{.
377d3 20 20 20 20 70 50 61 72 73 65 2d 3e 64 65 63 6c      pParse->decl
377d4 61 72 65 56 74 61 62 20 3d 20 31 3b 0a 20 20 20  areVtab = 1;.   
377d5 20 70 50 61 72 73 65 2d 3e 64 62 20 3d 20 64 62   pParse->db = db
377d6 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 51  ;.    pParse->nQ
377d7 75 65 72 79 4c 6f 6f 70 20 3d 20 31 3b 0a 20 20  ueryLoop = 1;.  
377d8 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
377d9 4f 4b 3d 3d 73 71 6c 69 74 65 33 52 75 6e 50 61  OK==sqlite3RunPa
377da 72 73 65 72 28 70 50 61 72 73 65 2c 20 7a 43 72  rser(pParse, zCr
377db 65 61 74 65 54 61 62 6c 65 2c 20 26 7a 45 72 72  eateTable, &zErr
377dc 29 20 0a 20 20 20 20 20 26 26 20 70 50 61 72 73  ) .     && pPars
377dd 65 2d 3e 70 4e 65 77 54 61 62 6c 65 0a 20 20 20  e->pNewTable.   
377de 20 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63    && !db->malloc
377df 46 61 69 6c 65 64 0a 20 20 20 20 20 26 26 20 21  Failed.     && !
377e0 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
377e1 65 2d 3e 70 53 65 6c 65 63 74 0a 20 20 20 20 20  e->pSelect.     
377e2 26 26 20 28 70 50 61 72 73 65 2d 3e 70 4e 65 77  && (pParse->pNew
377e3 54 61 62 6c 65 2d 3e 74 61 62 46 6c 61 67 73 20  Table->tabFlags 
377e4 26 20 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30  & TF_Virtual)==0
377e5 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66  .    ){.      if
377e6 28 20 21 70 54 61 62 2d 3e 61 43 6f 6c 20 29 7b  ( !pTab->aCol ){
377e7 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61  .        pTab->a
377e8 43 6f 6c 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  Col = pParse->pN
377e9 65 77 54 61 62 6c 65 2d 3e 61 43 6f 6c 3b 0a 20  ewTable->aCol;. 
377ea 20 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 43 6f         pTab->nCo
377eb 6c 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  l = pParse->pNew
377ec 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 0a 20 20 20  Table->nCol;.   
377ed 20 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65       pParse->pNe
377ee 77 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30  wTable->nCol = 0
377ef 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  ;.        pParse
377f0 2d 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 61 43 6f  ->pNewTable->aCo
377f1 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  l = 0;.      }. 
377f2 20 20 20 20 20 64 62 2d 3e 70 56 74 61 62 43 74       db->pVtabCt
377f3 78 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 20  x->pTab = 0;.   
377f4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
377f5 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
377f6 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 28 7a 45  QLITE_ERROR, (zE
377f7 72 72 20 3f 20 22 25 73 22 20 3a 20 30 29 2c 20  rr ? "%s" : 0), 
377f8 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 73 71 6c  zErr);.      sql
377f9 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
377fa 45 72 72 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  Err);.      rc =
377fb 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
377fc 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d     }.    pParse-
377fd 3e 64 65 63 6c 61 72 65 56 74 61 62 20 3d 20 30  >declareVtab = 0
377fe 3b 0a 20 20 0a 20 20 20 20 69 66 28 20 70 50 61  ;.  .    if( pPa
377ff 72 73 65 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20  rse->pVdbe ){.  
37800 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46      sqlite3VdbeF
37801 69 6e 61 6c 69 7a 65 28 70 50 61 72 73 65 2d 3e  inalize(pParse->
37802 70 56 64 62 65 29 3b 0a 20 20 20 20 7d 0a 20 20  pVdbe);.    }.  
37803 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
37804 61 62 6c 65 28 64 62 2c 20 70 50 61 72 73 65 2d  able(db, pParse-
37805 3e 70 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20 20  >pNewTable);.   
37806 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
37807 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20  e(db, pParse);. 
37808 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 28 72   }..  assert( (r
37809 63 26 30 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20  c&0xff)==rc );. 
3780a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
3780b 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
3780c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
3780d 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
3780e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
3780f 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
37810 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 62  ion is invoked b
37811 79 20 74 68 65 20 76 64 62 65 20 74 6f 20 63 61  y the vdbe to ca
37812 6c 6c 20 74 68 65 20 78 44 65 73 74 72 6f 79 20  ll the xDestroy 
37813 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65  method.** of the
37814 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e   virtual table n
37815 61 6d 65 64 20 7a 54 61 62 20 69 6e 20 64 61 74  amed zTab in dat
37816 61 62 61 73 65 20 69 44 62 2e 20 54 68 69 73 20  abase iDb. This 
37817 6f 63 63 75 72 73 0a 2a 2a 20 77 68 65 6e 20 61  occurs.** when a
37818 20 44 52 4f 50 20 54 41 42 4c 45 20 69 73 20 6d   DROP TABLE is m
37819 65 6e 74 69 6f 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  entioned..**.** 
3781a 54 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e  This call is a n
3781b 6f 2d 6f 70 20 69 66 20 7a 54 61 62 20 69 73 20  o-op if zTab is 
3781c 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74 61  not a virtual ta
3781d 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
3781e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
3781f 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f  e3VtabCallDestro
37820 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  y(sqlite3 *db, i
37821 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68  nt iDb, const ch
37822 61 72 20 2a 7a 54 61 62 29 7b 0a 20 20 69 6e 74  ar *zTab){.  int
37823 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
37824 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a  .  Table *pTab;.
37825 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
37826 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
37827 54 61 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  Tab, db->aDb[iDb
37828 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  ].zName);.  if( 
37829 41 4c 57 41 59 53 28 70 54 61 62 21 3d 30 20 26  ALWAYS(pTab!=0 &
3782a 26 20 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 21  & pTab->pVTable!
3782b 3d 30 29 20 29 7b 0a 20 20 20 20 56 54 61 62 6c  =0) ){.    VTabl
3782c 65 20 2a 70 20 3d 20 76 74 61 62 44 69 73 63 6f  e *p = vtabDisco
3782d 6e 6e 65 63 74 41 6c 6c 28 64 62 2c 20 70 54 61  nnectAll(db, pTa
3782e 62 29 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28  b);..    assert(
3782f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
37830 3b 0a 20 20 20 20 72 63 20 3d 20 70 2d 3e 70 4d  ;.    rc = p->pM
37831 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 65  od->pModule->xDe
37832 73 74 72 6f 79 28 70 2d 3e 70 56 74 61 62 29 3b  stroy(p->pVtab);
37833 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20  ..    /* Remove 
37834 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62  the sqlite3_vtab
37835 2a 20 66 72 6f 6d 20 74 68 65 20 61 56 54 72 61  * from the aVTra
37836 6e 73 5b 5d 20 61 72 72 61 79 2c 20 69 66 20 61  ns[] array, if a
37837 70 70 6c 69 63 61 62 6c 65 20 2a 2f 0a 20 20 20  pplicable */.   
37838 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
37839 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  OK ){.      asse
3783a 72 74 28 20 70 54 61 62 2d 3e 70 56 54 61 62 6c  rt( pTab->pVTabl
3783b 65 3d 3d 70 20 26 26 20 70 2d 3e 70 4e 65 78 74  e==p && p->pNext
3783c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e  ==0 );.      p->
3783d 70 56 74 61 62 20 3d 20 30 3b 0a 20 20 20 20 20  pVtab = 0;.     
3783e 20 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 20 3d   pTab->pVTable =
3783f 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   0;.      sqlite
37840 33 56 74 61 62 55 6e 6c 6f 63 6b 28 70 29 3b 0a  3VtabUnlock(p);.
37841 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
37842 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
37843 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
37844 6e 76 6f 6b 65 73 20 65 69 74 68 65 72 20 74 68  nvokes either th
37845 65 20 78 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 78  e xRollback or x
37846 43 6f 6d 6d 69 74 20 6d 65 74 68 6f 64 0a 2a 2a  Commit method.**
37847 20 6f 66 20 65 61 63 68 20 6f 66 20 74 68 65 20   of each of the 
37848 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69  virtual tables i
37849 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56  n the sqlite3.aV
3784a 54 72 61 6e 73 20 61 72 72 61 79 2e 20 54 68 65  Trans array. The
3784b 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 61 6c 6c 65   method.** calle
3784c 64 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20  d is identified 
3784d 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  by the second ar
3784e 67 75 6d 65 6e 74 2c 20 22 6f 66 66 73 65 74 22  gument, "offset"
3784f 2c 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68  , which is.** th
37850 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20  e offset of the 
37851 6d 65 74 68 6f 64 20 74 6f 20 63 61 6c 6c 20 69  method to call i
37852 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 6f  n the sqlite3_mo
37853 64 75 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a  dule structure..
37854 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 72 61 79 20  **.** The array 
37855 69 73 20 63 6c 65 61 72 65 64 20 61 66 74 65 72  is cleared after
37856 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 61   invoking the ca
37857 6c 6c 62 61 63 6b 73 2e 20 0a 2a 2f 0a 73 74 61  llbacks. .*/.sta
37858 74 69 63 20 76 6f 69 64 20 63 61 6c 6c 46 69 6e  tic void callFin
37859 61 6c 69 73 65 72 28 73 71 6c 69 74 65 33 20 2a  aliser(sqlite3 *
3785a 64 62 2c 20 69 6e 74 20 6f 66 66 73 65 74 29 7b  db, int offset){
3785b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
3785c 64 62 2d 3e 61 56 54 72 61 6e 73 20 29 7b 0a 20  db->aVTrans ){. 
3785d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
3785e 2d 3e 6e 56 54 72 61 6e 73 3b 20 69 2b 2b 29 7b  ->nVTrans; i++){
3785f 0a 20 20 20 20 20 20 56 54 61 62 6c 65 20 2a 70  .      VTable *p
37860 56 54 61 62 20 3d 20 64 62 2d 3e 61 56 54 72 61  VTab = db->aVTra
37861 6e 73 5b 69 5d 3b 0a 20 20 20 20 20 20 73 71 6c  ns[i];.      sql
37862 69 74 65 33 5f 76 74 61 62 20 2a 70 20 3d 20 70  ite3_vtab *p = p
37863 56 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20  VTab->pVtab;.   
37864 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20     if( p ){.    
37865 20 20 20 20 69 6e 74 20 28 2a 78 29 28 73 71 6c      int (*x)(sql
37866 69 74 65 33 5f 76 74 61 62 20 2a 29 3b 0a 20 20  ite3_vtab *);.  
37867 20 20 20 20 20 20 78 20 3d 20 2a 28 69 6e 74 20        x = *(int 
37868 28 2a 2a 29 28 73 71 6c 69 74 65 33 5f 76 74 61  (**)(sqlite3_vta
37869 62 20 2a 29 29 28 28 63 68 61 72 20 2a 29 70 2d  b *))((char *)p-
3786a 3e 70 4d 6f 64 75 6c 65 20 2b 20 6f 66 66 73 65  >pModule + offse
3786b 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t);.        if( 
3786c 78 20 29 20 78 28 70 29 3b 0a 20 20 20 20 20 20  x ) x(p);.      
3786d 7d 0a 20 20 20 20 20 20 70 56 54 61 62 2d 3e 69  }.      pVTab->i
3786e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20  Savepoint = 0;. 
3786f 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62       sqlite3Vtab
37870 55 6e 6c 6f 63 6b 28 70 56 54 61 62 29 3b 0a 20  Unlock(pVTab);. 
37871 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
37872 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61  DbFree(db, db->a
37873 56 54 72 61 6e 73 29 3b 0a 20 20 20 20 64 62 2d  VTrans);.    db-
37874 3e 6e 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20  >nVTrans = 0;.  
37875 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20    db->aVTrans = 
37876 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
37877 49 6e 76 6f 6b 65 20 74 68 65 20 78 53 79 6e 63  Invoke the xSync
37878 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20 76   method of all v
37879 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e  irtual tables in
3787a 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54   the sqlite3.aVT
3787b 72 61 6e 73 0a 2a 2a 20 61 72 72 61 79 2e 20 52  rans.** array. R
3787c 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20  eturn the error 
3787d 63 6f 64 65 20 66 6f 72 20 74 68 65 20 66 69 72  code for the fir
3787e 73 74 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63  st error that oc
3787f 63 75 72 73 2c 20 6f 72 0a 2a 2a 20 53 51 4c 49  curs, or.** SQLI
37880 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 20 78 53 79  TE_OK if all xSy
37881 6e 63 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72  nc operations ar
37882 65 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a  e successful..**
37883 0a 2a 2a 20 53 65 74 20 2a 70 7a 45 72 72 6d 73  .** Set *pzErrms
37884 67 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20  g to point to a 
37885 62 75 66 66 65 72 20 74 68 61 74 20 73 68 6f 75  buffer that shou
37886 6c 64 20 62 65 20 72 65 6c 65 61 73 65 64 20 75  ld be released u
37887 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33  sing .** sqlite3
37888 44 62 46 72 65 65 28 29 20 63 6f 6e 74 61 69 6e  DbFree() contain
37889 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ing an error mes
3788a 73 61 67 65 2c 20 69 66 20 6f 6e 65 20 69 73 20  sage, if one is 
3788b 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 53 51  available..*/.SQ
3788c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
3788d 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63   sqlite3VtabSync
3788e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 68  (sqlite3 *db, ch
3788f 61 72 20 2a 2a 70 7a 45 72 72 6d 73 67 29 7b 0a  ar **pzErrmsg){.
37890 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72    int i;.  int r
37891 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
37892 20 56 54 61 62 6c 65 20 2a 2a 61 56 54 72 61 6e   VTable **aVTran
37893 73 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73 3b  s = db->aVTrans;
37894 0a 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20  ..  db->aVTrans 
37895 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  = 0;.  for(i=0; 
37896 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
37897 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 3b 20   i<db->nVTrans; 
37898 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 28 2a  i++){.    int (*
37899 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  x)(sqlite3_vtab 
3789a 2a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  *);.    sqlite3_
3789b 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 61 56  vtab *pVtab = aV
3789c 54 72 61 6e 73 5b 69 5d 2d 3e 70 56 74 61 62 3b  Trans[i]->pVtab;
3789d 0a 20 20 20 20 69 66 28 20 70 56 74 61 62 20 26  .    if( pVtab &
3789e 26 20 28 78 20 3d 20 70 56 74 61 62 2d 3e 70 4d  & (x = pVtab->pM
3789f 6f 64 75 6c 65 2d 3e 78 53 79 6e 63 29 21 3d 30  odule->xSync)!=0
378a0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 78   ){.      rc = x
378a1 28 70 56 74 61 62 29 3b 0a 20 20 20 20 20 20 73  (pVtab);.      s
378a2 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
378a3 20 2a 70 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20   *pzErrmsg);.   
378a4 20 20 20 2a 70 7a 45 72 72 6d 73 67 20 3d 20 73     *pzErrmsg = s
378a5 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
378a6 62 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73  b, pVtab->zErrMs
378a7 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  g);.      sqlite
378a8 33 5f 66 72 65 65 28 70 56 74 61 62 2d 3e 7a 45  3_free(pVtab->zE
378a9 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20  rrMsg);.    }.  
378aa 7d 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20  }.  db->aVTrans 
378ab 3d 20 61 56 54 72 61 6e 73 3b 0a 20 20 72 65 74  = aVTrans;.  ret
378ac 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
378ad 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 52 6f 6c   Invoke the xRol
378ae 6c 62 61 63 6b 20 6d 65 74 68 6f 64 20 6f 66 20  lback method of 
378af 61 6c 6c 20 76 69 72 74 75 61 6c 20 74 61 62 6c  all virtual tabl
378b0 65 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 73 71  es in the .** sq
378b1 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72  lite3.aVTrans ar
378b2 72 61 79 2e 20 54 68 65 6e 20 63 6c 65 61 72 20  ray. Then clear 
378b3 74 68 65 20 61 72 72 61 79 20 69 74 73 65 6c 66  the array itself
378b4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
378b5 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
378b6 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69  tabRollback(sqli
378b7 74 65 33 20 2a 64 62 29 7b 0a 20 20 63 61 6c 6c  te3 *db){.  call
378b8 46 69 6e 61 6c 69 73 65 72 28 64 62 2c 20 6f 66  Finaliser(db, of
378b9 66 73 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d  fsetof(sqlite3_m
378ba 6f 64 75 6c 65 2c 78 52 6f 6c 6c 62 61 63 6b 29  odule,xRollback)
378bb 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
378bc 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
378bd 49 6e 76 6f 6b 65 20 74 68 65 20 78 43 6f 6d 6d  Invoke the xComm
378be 69 74 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c  it method of all
378bf 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
378c0 69 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74  in the .** sqlit
378c1 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79  e3.aVTrans array
378c2 2e 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65  . Then clear the
378c3 20 61 72 72 61 79 20 69 74 73 65 6c 66 2e 0a 2a   array itself..*
378c4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
378c5 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
378c6 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a  Commit(sqlite3 *
378c7 64 62 29 7b 0a 20 20 63 61 6c 6c 46 69 6e 61 6c  db){.  callFinal
378c8 69 73 65 72 28 64 62 2c 20 6f 66 66 73 65 74 6f  iser(db, offseto
378c9 66 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  f(sqlite3_module
378ca 2c 78 43 6f 6d 6d 69 74 29 29 3b 0a 20 20 72 65  ,xCommit));.  re
378cb 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
378cc 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
378cd 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70 56  virtual table pV
378ce 74 61 62 20 73 75 70 70 6f 72 74 73 20 74 68 65  tab supports the
378cf 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 74   transaction int
378d0 65 72 66 61 63 65 0a 2a 2a 20 28 78 42 65 67 69  erface.** (xBegi
378d1 6e 2f 78 52 6f 6c 6c 62 61 63 6b 2f 78 43 6f 6d  n/xRollback/xCom
378d2 6d 69 74 20 61 6e 64 20 6f 70 74 69 6f 6e 61 6c  mit and optional
378d3 6c 79 20 78 53 79 6e 63 29 20 61 6e 64 20 61 20  ly xSync) and a 
378d4 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a  transaction is.*
378d5 2a 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  * not currently 
378d6 6f 70 65 6e 2c 20 69 6e 76 6f 6b 65 20 74 68 65  open, invoke the
378d7 20 78 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 6e   xBegin method n
378d8 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ow..**.** If the
378d9 20 78 42 65 67 69 6e 20 63 61 6c 6c 20 69 73 20   xBegin call is 
378da 73 75 63 63 65 73 73 66 75 6c 2c 20 70 6c 61 63  successful, plac
378db 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74  e the sqlite3_vt
378dc 61 62 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 69 6e  ab pointer.** in
378dd 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54   the sqlite3.aVT
378de 72 61 6e 73 20 61 72 72 61 79 2e 0a 2a 2f 0a 53  rans array..*/.S
378df 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
378e0 74 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67  t sqlite3VtabBeg
378e1 69 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  in(sqlite3 *db, 
378e2 56 54 61 62 6c 65 20 2a 70 56 54 61 62 29 7b 0a  VTable *pVTab){.
378e3 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
378e4 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74 20 73 71  E_OK;.  const sq
378e5 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
378e6 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 53 70 65  odule;..  /* Spe
378e7 63 69 61 6c 20 63 61 73 65 3a 20 49 66 20 64 62  cial case: If db
378e8 2d 3e 61 56 54 72 61 6e 73 20 69 73 20 4e 55 4c  ->aVTrans is NUL
378e9 4c 20 61 6e 64 20 64 62 2d 3e 6e 56 54 72 61 6e  L and db->nVTran
378ea 73 20 69 73 20 67 72 65 61 74 65 72 0a 20 20 2a  s is greater.  *
378eb 2a 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65  * than zero, the
378ec 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
378ed 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20  is being called 
378ee 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 0a 20 20  from within a.  
378ef 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c  ** virtual modul
378f0 65 20 78 53 79 6e 63 28 29 20 63 61 6c 6c 62 61  e xSync() callba
378f1 63 6b 2e 20 49 74 20 69 73 20 69 6c 6c 65 67 61  ck. It is illega
378f2 6c 20 74 6f 20 77 72 69 74 65 20 74 6f 20 0a 20  l to write to . 
378f3 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75   ** virtual modu
378f4 6c 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 69  le tables in thi
378f5 73 20 63 61 73 65 2c 20 73 6f 20 72 65 74 75 72  s case, so retur
378f6 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e  n SQLITE_LOCKED.
378f7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
378f8 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62  te3VtabInSync(db
378f9 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
378fa 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20  SQLITE_LOCKED;. 
378fb 20 7d 0a 20 20 69 66 28 20 21 70 56 54 61 62 20   }.  if( !pVTab 
378fc 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
378fd 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 0a 20 20  LITE_OK;.  } .  
378fe 70 4d 6f 64 75 6c 65 20 3d 20 70 56 54 61 62 2d  pModule = pVTab-
378ff 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b  >pVtab->pModule;
37900 0a 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d  ..  if( pModule-
37901 3e 78 42 65 67 69 6e 20 29 7b 0a 20 20 20 20 69  >xBegin ){.    i
37902 6e 74 20 69 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  nt i;..    /* If
37903 20 70 56 74 61 62 20 69 73 20 61 6c 72 65 61 64   pVtab is alread
37904 79 20 69 6e 20 74 68 65 20 61 56 54 72 61 6e 73  y in the aVTrans
37905 20 61 72 72 61 79 2c 20 72 65 74 75 72 6e 20 65   array, return e
37906 61 72 6c 79 20 2a 2f 0a 20 20 20 20 66 6f 72 28  arly */.    for(
37907 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61  i=0; i<db->nVTra
37908 6e 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ns; i++){.      
37909 69 66 28 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b  if( db->aVTrans[
3790a 69 5d 3d 3d 70 56 54 61 62 20 29 7b 0a 20 20 20  i]==pVTab ){.   
3790b 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
3790c 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20  TE_OK;.      }. 
3790d 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 76     }..    /* Inv
3790e 6f 6b 65 20 74 68 65 20 78 42 65 67 69 6e 20 6d  oke the xBegin m
3790f 65 74 68 6f 64 2e 20 49 66 20 73 75 63 63 65 73  ethod. If succes
37910 73 66 75 6c 2c 20 61 64 64 20 74 68 65 20 76 74  sful, add the vt
37911 61 62 20 74 6f 20 74 68 65 20 0a 20 20 20 20 2a  ab to the .    *
37912 2a 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e  * sqlite3.aVTran
37913 73 5b 5d 20 61 72 72 61 79 2e 20 2a 2f 0a 20 20  s[] array. */.  
37914 20 20 72 63 20 3d 20 67 72 6f 77 56 54 72 61 6e    rc = growVTran
37915 73 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72  s(db);.    if( r
37916 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
37917 20 20 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75        rc = pModu
37918 6c 65 2d 3e 78 42 65 67 69 6e 28 70 56 54 61 62  le->xBegin(pVTab
37919 2d 3e 70 56 74 61 62 29 3b 0a 20 20 20 20 20 20  ->pVtab);.      
3791a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3791b 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 64 64  K ){.        add
3791c 54 6f 56 54 72 61 6e 73 28 64 62 2c 20 70 56 54  ToVTrans(db, pVT
3791d 61 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ab);.      }.   
3791e 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
3791f 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76  rc;.}../*.** Inv
37920 6f 6b 65 20 65 69 74 68 65 72 20 74 68 65 20 78  oke either the x
37921 53 61 76 65 70 6f 69 6e 74 2c 20 78 52 6f 6c 6c  Savepoint, xRoll
37922 62 61 63 6b 54 6f 20 6f 72 20 78 52 65 6c 65 61  backTo or xRelea
37923 73 65 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c  se method of all
37924 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c  .** virtual tabl
37925 65 73 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c  es that currentl
37926 79 20 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 74  y have an open t
37927 72 61 6e 73 61 63 74 69 6f 6e 2e 20 50 61 73 73  ransaction. Pass
37928 20 69 53 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 61   iSavepoint.** a
37929 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
3792a 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 76 69 72  ument to the vir
3792b 74 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f  tual table metho
3792c 64 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a  d invoked..**.**
3792d 20 49 66 20 6f 70 20 69 73 20 53 41 56 45 50 4f   If op is SAVEPO
3792e 49 4e 54 5f 42 45 47 49 4e 2c 20 74 68 65 20 78  INT_BEGIN, the x
3792f 53 61 76 65 70 6f 69 6e 74 20 6d 65 74 68 6f 64  Savepoint method
37930 20 69 73 20 69 6e 76 6f 6b 65 64 2e 20 49 66 20   is invoked. If 
37931 69 74 20 69 73 0a 2a 2a 20 53 41 56 45 50 4f 49  it is.** SAVEPOI
37932 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65  NT_ROLLBACK, the
37933 20 78 52 6f 6c 6c 62 61 63 6b 54 6f 20 6d 65 74   xRollbackTo met
37934 68 6f 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  hod. Otherwise, 
37935 69 66 20 6f 70 20 69 73 20 0a 2a 2a 20 53 41 56  if op is .** SAV
37936 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20  EPOINT_RELEASE, 
37937 74 68 65 6e 20 74 68 65 20 78 52 65 6c 65 61 73  then the xReleas
37938 65 20 6d 65 74 68 6f 64 20 6f 66 20 65 61 63 68  e method of each
37939 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 77   virtual table w
3793a 69 74 68 0a 2a 2a 20 61 6e 20 6f 70 65 6e 20 74  ith.** an open t
3793b 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 69 6e  ransaction is in
3793c 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  voked..**.** If 
3793d 61 6e 79 20 76 69 72 74 75 61 6c 20 74 61 62 6c  any virtual tabl
3793e 65 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73  e method returns
3793f 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f   an error code o
37940 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45  ther than SQLITE
37941 5f 4f 4b 2c 20 0a 2a 2a 20 70 72 6f 63 65 73 73  _OK, .** process
37942 69 6e 67 20 69 73 20 61 62 61 6e 64 6f 6e 65 64  ing is abandoned
37943 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 72   and the error r
37944 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63  eturned to the c
37945 61 6c 6c 65 72 20 6f 66 20 74 68 69 73 0a 2a 2a  aller of this.**
37946 20 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65 64 69   function immedi
37947 61 74 65 6c 79 2e 20 49 66 20 61 6c 6c 20 63 61  ately. If all ca
37948 6c 6c 73 20 74 6f 20 76 69 72 74 75 61 6c 20 74  lls to virtual t
37949 61 62 6c 65 20 6d 65 74 68 6f 64 73 20 61 72 65  able methods are
3794a 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20   successful,.** 
3794b 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
3794c 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
3794d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
3794e 69 74 65 33 56 74 61 62 53 61 76 65 70 6f 69 6e  ite3VtabSavepoin
3794f 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  t(sqlite3 *db, i
37950 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65  nt op, int iSave
37951 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63  point){.  int rc
37952 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
37953 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56   assert( op==SAV
37954 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 7c 7c  EPOINT_RELEASE||
37955 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f  op==SAVEPOINT_RO
37956 4c 4c 42 41 43 4b 7c 7c 6f 70 3d 3d 53 41 56 45  LLBACK||op==SAVE
37957 50 4f 49 4e 54 5f 42 45 47 49 4e 20 29 3b 0a 20  POINT_BEGIN );. 
37958 20 61 73 73 65 72 74 28 20 69 53 61 76 65 70 6f   assert( iSavepo
37959 69 6e 74 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20  int>=0 );.  if( 
3795a 64 62 2d 3e 61 56 54 72 61 6e 73 20 29 7b 0a 20  db->aVTrans ){. 
3795b 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
3795c 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=0; rc==SQLIT
3795d 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 56  E_OK && i<db->nV
3795e 54 72 61 6e 73 3b 20 69 2b 2b 29 7b 0a 20 20 20  Trans; i++){.   
3795f 20 20 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62     VTable *pVTab
37960 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69   = db->aVTrans[i
37961 5d 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 73  ];.      const s
37962 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
37963 4d 6f 64 20 3d 20 70 56 54 61 62 2d 3e 70 4d 6f  Mod = pVTab->pMo
37964 64 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20  d->pModule;.    
37965 20 20 69 66 28 20 70 56 54 61 62 2d 3e 70 56 74    if( pVTab->pVt
37966 61 62 20 26 26 20 70 4d 6f 64 2d 3e 69 56 65 72  ab && pMod->iVer
37967 73 69 6f 6e 3e 3d 32 20 29 7b 0a 20 20 20 20 20  sion>=2 ){.     
37968 20 20 20 69 6e 74 20 28 2a 78 4d 65 74 68 6f 64     int (*xMethod
37969 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
3796a 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  , int);.        
3796b 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20  switch( op ){.  
3796c 20 20 20 20 20 20 20 20 63 61 73 65 20 53 41 56          case SAV
3796d 45 50 4f 49 4e 54 5f 42 45 47 49 4e 3a 0a 20 20  EPOINT_BEGIN:.  
3796e 20 20 20 20 20 20 20 20 20 20 78 4d 65 74 68 6f            xMetho
3796f 64 20 3d 20 70 4d 6f 64 2d 3e 78 53 61 76 65 70  d = pMod->xSavep
37970 6f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 20 20  oint;.          
37971 20 20 70 56 54 61 62 2d 3e 69 53 61 76 65 70 6f    pVTab->iSavepo
37972 69 6e 74 20 3d 20 69 53 61 76 65 70 6f 69 6e 74  int = iSavepoint
37973 2b 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +1;.            
37974 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
37975 20 63 61 73 65 20 53 41 56 45 50 4f 49 4e 54 5f   case SAVEPOINT_
37976 52 4f 4c 4c 42 41 43 4b 3a 0a 20 20 20 20 20 20  ROLLBACK:.      
37977 20 20 20 20 20 20 78 4d 65 74 68 6f 64 20 3d 20        xMethod = 
37978 70 4d 6f 64 2d 3e 78 52 6f 6c 6c 62 61 63 6b 54  pMod->xRollbackT
37979 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  o;.            b
3797a 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
3797b 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20  default:.       
3797c 20 20 20 20 20 78 4d 65 74 68 6f 64 20 3d 20 70       xMethod = p
3797d 4d 6f 64 2d 3e 78 52 65 6c 65 61 73 65 3b 0a 20  Mod->xRelease;. 
3797e 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
3797f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
37980 20 20 20 20 69 66 28 20 78 4d 65 74 68 6f 64 20      if( xMethod 
37981 26 26 20 70 56 54 61 62 2d 3e 69 53 61 76 65 70  && pVTab->iSavep
37982 6f 69 6e 74 3e 69 53 61 76 65 70 6f 69 6e 74 20  oint>iSavepoint 
37983 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
37984 3d 20 78 4d 65 74 68 6f 64 28 70 56 54 61 62 2d  = xMethod(pVTab-
37985 3e 70 56 74 61 62 2c 20 69 53 61 76 65 70 6f 69  >pVtab, iSavepoi
37986 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  nt);.        }. 
37987 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
37988 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
37989 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  ./*.** The first
3798a 20 70 61 72 61 6d 65 74 65 72 20 28 70 44 65 66   parameter (pDef
3798b 29 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 20  ) is a function 
3798c 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20  implementation. 
3798d 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70   The.** second p
3798e 61 72 61 6d 65 74 65 72 20 28 70 45 78 70 72 29  arameter (pExpr)
3798f 20 69 73 20 74 68 65 20 66 69 72 73 74 20 61 72   is the first ar
37990 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66  gument to this f
37991 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 66 20 70  unction..** If p
37992 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e  Expr is a column
37993 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61   in a virtual ta
37994 62 6c 65 2c 20 74 68 65 6e 20 6c 65 74 20 74 68  ble, then let th
37995 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62  e virtual.** tab
37996 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
37997 6e 20 68 61 76 65 20 61 6e 20 6f 70 70 6f 72 74  n have an opport
37998 75 6e 69 74 79 20 74 6f 20 6f 76 65 72 6c 6f 61  unity to overloa
37999 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a  d the function..
3799a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
3799b 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 61 6c  ne is used to al
3799c 6c 6f 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c  low virtual tabl
3799d 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
3799e 73 20 74 6f 0a 2a 2a 20 6f 76 65 72 6c 6f 61 64  s to.** overload
3799f 20 4d 41 54 43 48 2c 20 4c 49 4b 45 2c 20 47 4c   MATCH, LIKE, GL
379a0 4f 42 2c 20 61 6e 64 20 52 45 47 45 58 50 20 6f  OB, and REGEXP o
379a1 70 65 72 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20  perators..**.** 
379a2 52 65 74 75 72 6e 20 65 69 74 68 65 72 20 74 68  Return either th
379a3 65 20 70 44 65 66 20 61 72 67 75 6d 65 6e 74 20  e pDef argument 
379a4 28 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 63  (indicating no c
379a5 68 61 6e 67 65 29 20 6f 72 20 61 20 0a 2a 2a 20  hange) or a .** 
379a6 6e 65 77 20 46 75 6e 63 44 65 66 20 73 74 72 75  new FuncDef stru
379a7 63 74 75 72 65 20 74 68 61 74 20 69 73 20 6d 61  cture that is ma
379a8 72 6b 65 64 20 61 73 20 65 70 68 65 6d 65 72 61  rked as ephemera
379a9 6c 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 53  l using the.** S
379aa 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d  QLITE_FUNC_EPHEM
379ab 20 66 6c 61 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45   flag..*/.SQLITE
379ac 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 66  _PRIVATE FuncDef
379ad 20 2a 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65   *sqlite3VtabOve
379ae 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 0a 20  rloadFunction(. 
379af 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
379b0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
379b1 6e 65 63 74 69 6f 6e 20 66 6f 72 20 72 65 70 6f  nection for repo
379b2 72 74 69 6e 67 20 6d 61 6c 6c 6f 63 20 70 72 6f  rting malloc pro
379b3 62 6c 65 6d 73 20 2a 2f 0a 20 20 46 75 6e 63 44  blems */.  FuncD
379b4 65 66 20 2a 70 44 65 66 2c 20 20 2f 2a 20 46 75  ef *pDef,  /* Fu
379b5 6e 63 74 69 6f 6e 20 74 6f 20 70 6f 73 73 69 62  nction to possib
379b6 6c 79 20 6f 76 65 72 6c 6f 61 64 20 2a 2f 0a 20  ly overload */. 
379b7 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 20   int nArg,      
379b8 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
379b9 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66  guments to the f
379ba 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70  unction */.  Exp
379bb 72 20 2a 70 45 78 70 72 20 20 20 20 20 2f 2a 20  r *pExpr     /* 
379bc 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  First argument t
379bd 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  o the function *
379be 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54  /.){.  Table *pT
379bf 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  ab;.  sqlite3_vt
379c0 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 73 71 6c  ab *pVtab;.  sql
379c1 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
379c2 64 3b 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e  d;.  void (*xFun
379c3 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
379c4 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
379c5 76 61 6c 75 65 2a 2a 29 20 3d 20 30 3b 0a 20 20  value**) = 0;.  
379c6 76 6f 69 64 20 2a 70 41 72 67 20 3d 20 30 3b 0a  void *pArg = 0;.
379c7 20 20 46 75 6e 63 44 65 66 20 2a 70 4e 65 77 3b    FuncDef *pNew;
379c8 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20  .  int rc = 0;. 
379c9 20 63 68 61 72 20 2a 7a 4c 6f 77 65 72 4e 61 6d   char *zLowerNam
379ca 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  e;.  unsigned ch
379cb 61 72 20 2a 7a 3b 0a 0a 0a 20 20 2f 2a 20 43 68  ar *z;...  /* Ch
379cc 65 63 6b 20 74 6f 20 73 65 65 20 74 68 65 20 6c  eck to see the l
379cd 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73 20 61  eft operand is a
379ce 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72   column in a vir
379cf 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
379d0 69 66 28 20 4e 45 56 45 52 28 70 45 78 70 72 3d  if( NEVER(pExpr=
379d1 3d 30 29 20 29 20 72 65 74 75 72 6e 20 70 44 65  =0) ) return pDe
379d2 66 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  f;.  if( pExpr->
379d3 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20  op!=TK_COLUMN ) 
379d4 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 70  return pDef;.  p
379d5 54 61 62 20 3d 20 70 45 78 70 72 2d 3e 70 54 61  Tab = pExpr->pTa
379d6 62 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  b;.  if( NEVER(p
379d7 54 61 62 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  Tab==0) ) return
379d8 20 70 44 65 66 3b 0a 20 20 69 66 28 20 28 70 54   pDef;.  if( (pT
379d9 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
379da 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30 20 29 20  F_Virtual)==0 ) 
379db 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 70  return pDef;.  p
379dc 56 74 61 62 20 3d 20 73 71 6c 69 74 65 33 47 65  Vtab = sqlite3Ge
379dd 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62  tVTable(db, pTab
379de 29 2d 3e 70 56 74 61 62 3b 0a 20 20 61 73 73 65  )->pVtab;.  asse
379df 72 74 28 20 70 56 74 61 62 21 3d 30 20 29 3b 0a  rt( pVtab!=0 );.
379e0 20 20 61 73 73 65 72 74 28 20 70 56 74 61 62 2d    assert( pVtab-
379e1 3e 70 4d 6f 64 75 6c 65 21 3d 30 20 29 3b 0a 20  >pModule!=0 );. 
379e2 20 70 4d 6f 64 20 3d 20 28 73 71 6c 69 74 65 33   pMod = (sqlite3
379e3 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61 62 2d  _module *)pVtab-
379e4 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 66 28 20  >pModule;.  if( 
379e5 70 4d 6f 64 2d 3e 78 46 69 6e 64 46 75 6e 63 74  pMod->xFindFunct
379e6 69 6f 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ion==0 ) return 
379e7 70 44 65 66 3b 0a 20 0a 20 20 2f 2a 20 43 61 6c  pDef;. .  /* Cal
379e8 6c 20 74 68 65 20 78 46 69 6e 64 46 75 6e 63 74  l the xFindFunct
379e9 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68  ion method on th
379ea 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
379eb 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 20  implementation. 
379ec 20 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74 68   ** to see if th
379ed 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
379ee 20 77 61 6e 74 73 20 74 6f 20 6f 76 65 72 6c 6f   wants to overlo
379ef 61 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ad this function
379f0 20 0a 20 20 2a 2f 0a 20 20 7a 4c 6f 77 65 72 4e   .  */.  zLowerN
379f1 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ame = sqlite3DbS
379f2 74 72 44 75 70 28 64 62 2c 20 70 44 65 66 2d 3e  trDup(db, pDef->
379f3 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4c  zName);.  if( zL
379f4 6f 77 65 72 4e 61 6d 65 20 29 7b 0a 20 20 20 20  owerName ){.    
379f5 66 6f 72 28 7a 3d 28 75 6e 73 69 67 6e 65 64 20  for(z=(unsigned 
379f6 63 68 61 72 2a 29 7a 4c 6f 77 65 72 4e 61 6d 65  char*)zLowerName
379f7 3b 20 2a 7a 3b 20 7a 2b 2b 29 7b 0a 20 20 20 20  ; *z; z++){.    
379f8 20 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 55 70    *z = sqlite3Up
379f9 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 7a 5d 3b 0a  perToLower[*z];.
379fa 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 70      }.    rc = p
379fb 4d 6f 64 2d 3e 78 46 69 6e 64 46 75 6e 63 74 69  Mod->xFindFuncti
379fc 6f 6e 28 70 56 74 61 62 2c 20 6e 41 72 67 2c 20  on(pVtab, nArg, 
379fd 7a 4c 6f 77 65 72 4e 61 6d 65 2c 20 26 78 46 75  zLowerName, &xFu
379fe 6e 63 2c 20 26 70 41 72 67 29 3b 0a 20 20 20 20  nc, &pArg);.    
379ff 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
37a00 2c 20 7a 4c 6f 77 65 72 4e 61 6d 65 29 3b 0a 20  , zLowerName);. 
37a01 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29   }.  if( rc==0 )
37a02 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 44 65  {.    return pDe
37a03 66 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65  f;.  }..  /* Cre
37a04 61 74 65 20 61 20 6e 65 77 20 65 70 68 65 6d 65  ate a new epheme
37a05 72 61 6c 20 66 75 6e 63 74 69 6f 6e 20 64 65 66  ral function def
37a06 69 6e 69 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  inition for the 
37a07 6f 76 65 72 6c 6f 61 64 65 64 0a 20 20 2a 2a 20  overloaded.  ** 
37a08 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 70 4e  function */.  pN
37a09 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
37a0a 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
37a0b 65 6f 66 28 2a 70 4e 65 77 29 0a 20 20 20 20 20  eof(*pNew).     
37a0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37a0d 20 20 20 20 20 20 20 20 2b 20 73 71 6c 69 74 65          + sqlite
37a0e 33 53 74 72 6c 65 6e 33 30 28 70 44 65 66 2d 3e  3Strlen30(pDef->
37a0f 7a 4e 61 6d 65 29 20 2b 20 31 29 3b 0a 20 20 69  zName) + 1);.  i
37a10 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20  f( pNew==0 ){.  
37a11 20 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20    return pDef;. 
37a12 20 7d 0a 20 20 2a 70 4e 65 77 20 3d 20 2a 70 44   }.  *pNew = *pD
37a13 65 66 3b 0a 20 20 70 4e 65 77 2d 3e 7a 4e 61 6d  ef;.  pNew->zNam
37a14 65 20 3d 20 28 63 68 61 72 20 2a 29 26 70 4e 65  e = (char *)&pNe
37a15 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70 79 28 70  w[1];.  memcpy(p
37a16 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66  New->zName, pDef
37a17 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33  ->zName, sqlite3
37a18 53 74 72 6c 65 6e 33 30 28 70 44 65 66 2d 3e 7a  Strlen30(pDef->z
37a19 4e 61 6d 65 29 2b 31 29 3b 0a 20 20 70 4e 65 77  Name)+1);.  pNew
37a1a 2d 3e 78 46 75 6e 63 20 3d 20 78 46 75 6e 63 3b  ->xFunc = xFunc;
37a1b 0a 20 20 70 4e 65 77 2d 3e 70 55 73 65 72 44 61  .  pNew->pUserDa
37a1c 74 61 20 3d 20 70 41 72 67 3b 0a 20 20 70 4e 65  ta = pArg;.  pNe
37a1d 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  w->flags |= SQLI
37a1e 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 3b 0a 20  TE_FUNC_EPHEM;. 
37a1f 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
37a20 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
37a21 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70   virtual table p
37a22 54 61 62 20 69 73 20 63 6f 6e 74 61 69 6e 65 64  Tab is contained
37a23 20 69 6e 20 74 68 65 20 70 50 61 72 73 65 2d 3e   in the pParse->
37a24 61 70 56 69 72 74 75 61 6c 4c 6f 63 6b 5b 5d 0a  apVirtualLock[].
37a25 2a 2a 20 61 72 72 61 79 20 73 6f 20 74 68 61 74  ** array so that
37a26 20 61 6e 20 4f 50 5f 56 42 65 67 69 6e 20 77 69   an OP_VBegin wi
37a27 6c 6c 20 67 65 74 20 67 65 6e 65 72 61 74 65 64  ll get generated
37a28 20 66 6f 72 20 69 74 2e 20 20 41 64 64 20 70 54   for it.  Add pT
37a29 61 62 20 74 6f 20 74 68 65 0a 2a 2a 20 61 72 72  ab to the.** arr
37a2a 61 79 20 69 66 20 69 74 20 69 73 20 6d 69 73 73  ay if it is miss
37a2b 69 6e 67 2e 20 20 49 66 20 70 54 61 62 20 69 73  ing.  If pTab is
37a2c 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
37a2d 61 72 72 61 79 2c 20 74 68 69 73 20 72 6f 75 74  array, this rout
37a2e 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f  ine.** is a no-o
37a2f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  p..*/.SQLITE_PRI
37a30 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
37a31 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c  3VtabMakeWritabl
37a32 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
37a33 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20   Table *pTab){. 
37a34 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65   Parse *pTopleve
37a35 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65  l = sqlite3Parse
37a36 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29  Toplevel(pParse)
37a37 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  ;.  int i, n;.  
37a38 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 62 4c 6f  Table **apVtabLo
37a39 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 49  ck;..  assert( I
37a3a 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
37a3b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
37a3c 54 6f 70 6c 65 76 65 6c 2d 3e 6e 56 74 61 62 4c  Toplevel->nVtabL
37a3d 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  ock; i++){.    i
37a3e 66 28 20 70 54 61 62 3d 3d 70 54 6f 70 6c 65 76  f( pTab==pToplev
37a3f 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 69  el->apVtabLock[i
37a40 5d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a  ] ) return;.  }.
37a41 20 20 6e 20 3d 20 28 70 54 6f 70 6c 65 76 65 6c    n = (pToplevel
37a42 2d 3e 6e 56 74 61 62 4c 6f 63 6b 2b 31 29 2a 73  ->nVtabLock+1)*s
37a43 69 7a 65 6f 66 28 70 54 6f 70 6c 65 76 65 6c 2d  izeof(pToplevel-
37a44 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 30 5d 29 3b  >apVtabLock[0]);
37a45 0a 20 20 61 70 56 74 61 62 4c 6f 63 6b 20 3d 20  .  apVtabLock = 
37a46 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
37a47 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70 56 74 61  pToplevel->apVta
37a48 62 4c 6f 63 6b 2c 20 6e 29 3b 0a 20 20 69 66 28  bLock, n);.  if(
37a49 20 61 70 56 74 61 62 4c 6f 63 6b 20 29 7b 0a 20   apVtabLock ){. 
37a4a 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70     pToplevel->ap
37a4b 56 74 61 62 4c 6f 63 6b 20 3d 20 61 70 56 74 61  VtabLock = apVta
37a4c 62 4c 6f 63 6b 3b 0a 20 20 20 20 70 54 6f 70 6c  bLock;.    pTopl
37a4d 65 76 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b  evel->apVtabLock
37a4e 5b 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 56 74 61  [pToplevel->nVta
37a4f 62 4c 6f 63 6b 2b 2b 5d 20 3d 20 70 54 61 62 3b  bLock++] = pTab;
37a50 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54  .  }else{.    pT
37a51 6f 70 6c 65 76 65 6c 2d 3e 64 62 2d 3e 6d 61 6c  oplevel->db->mal
37a52 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
37a53 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
37a54 72 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49  rn the ON CONFLI
37a55 43 54 20 72 65 73 6f 6c 75 74 69 6f 6e 20 6d 6f  CT resolution mo
37a56 64 65 20 69 6e 20 65 66 66 65 63 74 20 66 6f 72  de in effect for
37a57 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20   the virtual.** 
37a58 74 61 62 6c 65 20 75 70 64 61 74 65 20 6f 70 65  table update ope
37a59 72 61 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79  ration currently
37a5a 20 69 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a   in progress..**
37a5b 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20  .** The results 
37a5c 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  of this routine 
37a5d 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 75 6e  are undefined un
37a5e 6c 65 73 73 20 69 74 20 69 73 20 63 61 6c 6c 65  less it is calle
37a5f 64 20 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69 6e  d from.** within
37a60 20 61 6e 20 78 55 70 64 61 74 65 20 6d 65 74 68   an xUpdate meth
37a61 6f 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  od..*/.SQLITE_AP
37a62 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 74  I int sqlite3_vt
37a63 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63 74 28 73  ab_on_conflict(s
37a64 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73  qlite3 *db){.  s
37a65 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
37a66 67 6e 65 64 20 63 68 61 72 20 61 4d 61 70 5b 5d  gned char aMap[]
37a67 20 3d 20 7b 20 0a 20 20 20 20 53 51 4c 49 54 45   = { .    SQLITE
37a68 5f 52 4f 4c 4c 42 41 43 4b 2c 20 53 51 4c 49 54  _ROLLBACK, SQLIT
37a69 45 5f 41 42 4f 52 54 2c 20 53 51 4c 49 54 45 5f  E_ABORT, SQLITE_
37a6a 46 41 49 4c 2c 20 53 51 4c 49 54 45 5f 49 47 4e  FAIL, SQLITE_IGN
37a6b 4f 52 45 2c 20 53 51 4c 49 54 45 5f 52 45 50 4c  ORE, SQLITE_REPL
37a6c 41 43 45 20 0a 20 20 7d 3b 0a 20 20 61 73 73 65  ACE .  };.  asse
37a6d 72 74 28 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3d  rt( OE_Rollback=
37a6e 3d 31 20 26 26 20 4f 45 5f 41 62 6f 72 74 3d 3d  =1 && OE_Abort==
37a6f 32 20 26 26 20 4f 45 5f 46 61 69 6c 3d 3d 33 20  2 && OE_Fail==3 
37a70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 4f 45 5f  );.  assert( OE_
37a71 49 67 6e 6f 72 65 3d 3d 34 20 26 26 20 4f 45 5f  Ignore==4 && OE_
37a72 52 65 70 6c 61 63 65 3d 3d 35 20 29 3b 0a 20 20  Replace==5 );.  
37a73 61 73 73 65 72 74 28 20 64 62 2d 3e 76 74 61 62  assert( db->vtab
37a74 4f 6e 43 6f 6e 66 6c 69 63 74 3e 3d 31 20 26 26  OnConflict>=1 &&
37a75 20 64 62 2d 3e 76 74 61 62 4f 6e 43 6f 6e 66 6c   db->vtabOnConfl
37a76 69 63 74 3c 3d 35 20 29 3b 0a 20 20 72 65 74 75  ict<=5 );.  retu
37a77 72 6e 20 28 69 6e 74 29 61 4d 61 70 5b 64 62 2d  rn (int)aMap[db-
37a78 3e 76 74 61 62 4f 6e 43 6f 6e 66 6c 69 63 74 2d  >vtabOnConflict-
37a79 31 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c  1];.}../*.** Cal
37a7a 6c 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68  l from within th
37a7b 65 20 78 43 72 65 61 74 65 28 29 20 6f 72 20 78  e xCreate() or x
37a7c 43 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64  Connect() method
37a7d 73 20 74 6f 20 70 72 6f 76 69 64 65 20 0a 2a 2a  s to provide .**
37a7e 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65   the SQLite core
37a7f 20 77 69 74 68 20 61 64 64 69 74 69 6f 6e 61 6c   with additional
37a80 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
37a81 75 74 20 74 68 65 20 62 65 68 61 76 69 6f 72 0a  ut the behavior.
37a82 2a 2a 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  ** of the virtua
37a83 6c 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69 6d  l table being im
37a84 70 6c 65 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 53 51  plemented..*/.SQ
37a85 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
37a86 69 74 65 33 5f 76 74 61 62 5f 63 6f 6e 66 69 67  ite3_vtab_config
37a87 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
37a88 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  t op, ...){.  va
37a89 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20  _list ap;.  int 
37a8a 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
37a8b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
37a8c 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
37a8d 29 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74 28 61  );..  va_start(a
37a8e 70 2c 20 6f 70 29 3b 0a 20 20 73 77 69 74 63 68  p, op);.  switch
37a8f 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ( op ){.    case
37a90 20 53 51 4c 49 54 45 5f 56 54 41 42 5f 43 4f 4e   SQLITE_VTAB_CON
37a91 53 54 52 41 49 4e 54 5f 53 55 50 50 4f 52 54 3a  STRAINT_SUPPORT:
37a92 20 7b 0a 20 20 20 20 20 20 56 74 61 62 43 74 78   {.      VtabCtx
37a93 20 2a 70 20 3d 20 64 62 2d 3e 70 56 74 61 62 43   *p = db->pVtabC
37a94 74 78 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  tx;.      if( !p
37a95 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
37a96 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42   SQLITE_MISUSE_B
37a97 4b 50 54 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  KPT;.      }else
37a98 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
37a99 28 20 70 2d 3e 70 54 61 62 3d 3d 30 20 7c 7c 20  ( p->pTab==0 || 
37a9a 28 70 2d 3e 70 54 61 62 2d 3e 74 61 62 46 6c 61  (p->pTab->tabFla
37a9b 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29  gs & TF_Virtual)
37a9c 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70  !=0 );.        p
37a9d 2d 3e 70 56 54 61 62 6c 65 2d 3e 62 43 6f 6e 73  ->pVTable->bCons
37a9e 74 72 61 69 6e 74 20 3d 20 28 75 38 29 76 61 5f  traint = (u8)va_
37a9f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
37aa0 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
37aa1 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
37aa2 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 63 20 3d  ault:.      rc =
37aa3 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42   SQLITE_MISUSE_B
37aa4 4b 50 54 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  KPT;.      break
37aa5 3b 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61  ;.  }.  va_end(a
37aa6 70 29 3b 0a 0a 20 20 69 66 28 20 72 63 21 3d 53  p);..  if( rc!=S
37aa7 51 4c 49 54 45 5f 4f 4b 20 29 20 73 71 6c 69 74  QLITE_OK ) sqlit
37aa8 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20  e3Error(db, rc, 
37aa9 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  0);.  sqlite3_mu
37aaa 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
37aab 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
37aac 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  c;.}..#endif /* 
37aad 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
37aae 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a  UALTABLE */../**
37aaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
37ab0 20 6f 66 20 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a   of vtab.c *****
37ab1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37ab2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37ab3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
37ab4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
37ab5 69 6e 20 66 69 6c 65 20 77 68 65 72 65 2e 63 20  in file where.c 
37ab6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37ab7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37ab8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
37ab9 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
37aba 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
37abb 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
37abc 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
37abd 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
37abe 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
37abf 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
37ac0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
37ac1 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
37ac2 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
37ac3 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
37ac4 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
37ac5 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
37ac6 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
37ac7 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
37ac8 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
37ac9 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
37aca 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
37acb 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
37acc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37acd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37ace 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37acf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37ad0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75  ***.** This modu
37ad1 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
37ad2 64 65 20 74 68 61 74 20 67 65 6e 65 72 61 74 65  de that generate
37ad3 73 20 56 44 42 45 20 63 6f 64 65 20 75 73 65 64  s VDBE code used
37ad4 20 74 6f 20 70 72 6f 63 65 73 73 0a 2a 2a 20 74   to process.** t
37ad5 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
37ad6 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  of SQL statement
37ad7 73 2e 20 20 54 68 69 73 20 6d 6f 64 75 6c 65 20  s.  This module 
37ad8 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
37ad9 6f 72 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67  or.** generating
37ada 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 6c   the code that l
37adb 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 61 20 74  oops through a t
37adc 61 62 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  able looking for
37add 20 61 70 70 6c 69 63 61 62 6c 65 0a 2a 2a 20 72   applicable.** r
37ade 6f 77 73 2e 20 20 49 6e 64 69 63 65 73 20 61 72  ows.  Indices ar
37adf 65 20 73 65 6c 65 63 74 65 64 20 61 6e 64 20 75  e selected and u
37ae0 73 65 64 20 74 6f 20 73 70 65 65 64 20 74 68 65  sed to speed the
37ae1 20 73 65 61 72 63 68 20 77 68 65 6e 20 64 6f 69   search when doi
37ae2 6e 67 0a 2a 2a 20 73 6f 20 69 73 20 61 70 70 6c  ng.** so is appl
37ae3 69 63 61 62 6c 65 2e 20 20 42 65 63 61 75 73 65  icable.  Because
37ae4 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20   this module is 
37ae5 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
37ae6 73 65 6c 65 63 74 69 6e 67 0a 2a 2a 20 69 6e 64  selecting.** ind
37ae7 69 63 65 73 2c 20 79 6f 75 20 6d 69 67 68 74 20  ices, you might 
37ae8 61 6c 73 6f 20 74 68 69 6e 6b 20 6f 66 20 74 68  also think of th
37ae9 69 73 20 6d 6f 64 75 6c 65 20 61 73 20 74 68 65  is module as the
37aea 20 22 71 75 65 72 79 20 6f 70 74 69 6d 69 7a 65   "query optimize
37aeb 72 22 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54  r"..*/.../*.** T
37aec 72 61 63 65 20 6f 75 74 70 75 74 20 6d 61 63 72  race output macr
37aed 6f 73 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  os.*/.#if define
37aee 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c  d(SQLITE_TEST) |
37aef 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
37af0 5f 44 45 42 55 47 29 0a 2f 2a 2a 2a 2f 20 69 6e  _DEBUG)./***/ in
37af1 74 20 73 71 6c 69 74 65 33 57 68 65 72 65 54 72  t sqlite3WhereTr
37af2 61 63 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  ace = 0;.#endif.
37af3 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
37af4 54 45 5f 44 45 42 55 47 29 20 5c 0a 20 20 20 20  TE_DEBUG) \.    
37af5 26 26 20 28 64 65 66 69 6e 65 64 28 53 51 4c 49  && (defined(SQLI
37af6 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69  TE_TEST) || defi
37af7 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
37af8 45 5f 57 48 45 52 45 54 52 41 43 45 29 29 0a 23  E_WHERETRACE)).#
37af9 20 64 65 66 69 6e 65 20 57 48 45 52 45 54 52 41   define WHERETRA
37afa 43 45 28 58 29 20 20 69 66 28 73 71 6c 69 74 65  CE(X)  if(sqlite
37afb 33 57 68 65 72 65 54 72 61 63 65 29 20 73 71 6c  3WhereTrace) sql
37afc 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 20  ite3DebugPrintf 
37afd 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  X.#else.# define
37afe 20 57 48 45 52 45 54 52 41 43 45 28 58 29 0a 23   WHERETRACE(X).#
37aff 65 6e 64 69 66 0a 0a 2f 2a 20 46 6f 72 77 61 72  endif../* Forwar
37b00 64 20 72 65 66 65 72 65 6e 63 65 0a 2a 2f 0a 74  d reference.*/.t
37b01 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68  ypedef struct Wh
37b02 65 72 65 43 6c 61 75 73 65 20 57 68 65 72 65 43  ereClause WhereC
37b03 6c 61 75 73 65 3b 0a 74 79 70 65 64 65 66 20 73  lause;.typedef s
37b04 74 72 75 63 74 20 57 68 65 72 65 4d 61 73 6b 53  truct WhereMaskS
37b05 65 74 20 57 68 65 72 65 4d 61 73 6b 53 65 74 3b  et WhereMaskSet;
37b06 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
37b07 57 68 65 72 65 4f 72 49 6e 66 6f 20 57 68 65 72  WhereOrInfo Wher
37b08 65 4f 72 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66  eOrInfo;.typedef
37b09 20 73 74 72 75 63 74 20 57 68 65 72 65 41 6e 64   struct WhereAnd
37b0a 49 6e 66 6f 20 57 68 65 72 65 41 6e 64 49 6e 66  Info WhereAndInf
37b0b 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  o;.typedef struc
37b0c 74 20 57 68 65 72 65 43 6f 73 74 20 57 68 65 72  t WhereCost Wher
37b0d 65 43 6f 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  eCost;../*.** Th
37b0e 65 20 71 75 65 72 79 20 67 65 6e 65 72 61 74 6f  e query generato
37b0f 72 20 75 73 65 73 20 61 6e 20 61 72 72 61 79 20  r uses an array 
37b10 6f 66 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20  of instances of 
37b11 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 74  this structure t
37b12 6f 0a 2a 2a 20 68 65 6c 70 20 69 74 20 61 6e 61  o.** help it ana
37b13 6c 79 7a 65 20 74 68 65 20 73 75 62 65 78 70 72  lyze the subexpr
37b14 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 57  essions of the W
37b15 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 45 61  HERE clause.  Ea
37b16 63 68 20 57 48 45 52 45 0a 2a 2a 20 63 6c 61 75  ch WHERE.** clau
37b17 73 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  se subexpression
37b18 20 69 73 20 73 65 70 61 72 61 74 65 64 20 66 72   is separated fr
37b19 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 20 62 79  om the others by
37b1a 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 73 2c 0a   AND operators,.
37b1b 2a 2a 20 75 73 75 61 6c 6c 79 2c 20 6f 72 20 73  ** usually, or s
37b1c 6f 6d 65 74 69 6d 65 73 20 73 75 62 65 78 70 72  ometimes subexpr
37b1d 65 73 73 69 6f 6e 73 20 73 65 70 61 72 61 74 65  essions separate
37b1e 64 20 62 79 20 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41  d by OR..**.** A
37b1f 6c 6c 20 57 68 65 72 65 54 65 72 6d 73 20 61 72  ll WhereTerms ar
37b20 65 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f  e collected into
37b21 20 61 20 73 69 6e 67 6c 65 20 57 68 65 72 65 43   a single WhereC
37b22 6c 61 75 73 65 20 73 74 72 75 63 74 75 72 65 2e  lause structure.
37b23 20 20 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77    .** The follow
37b24 69 6e 67 20 69 64 65 6e 74 69 74 79 20 68 6f 6c  ing identity hol
37b25 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  ds:.**.**       
37b26 20 57 68 65 72 65 54 65 72 6d 2e 70 57 43 2d 3e   WhereTerm.pWC->
37b27 61 5b 57 68 65 72 65 54 65 72 6d 2e 69 64 78 5d  a[WhereTerm.idx]
37b28 20 3d 3d 20 57 68 65 72 65 54 65 72 6d 0a 2a 2a   == WhereTerm.**
37b29 0a 2a 2a 20 57 68 65 6e 20 61 20 74 65 72 6d 20  .** When a term 
37b2a 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a  is of the form:.
37b2b 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  **.**           
37b2c 20 20 20 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e     X <op> <expr>
37b2d 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 58 20 69  .**.** where X i
37b2e 73 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20  s a column name 
37b2f 61 6e 64 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20  and <op> is one 
37b30 6f 66 20 63 65 72 74 61 69 6e 20 6f 70 65 72 61  of certain opera
37b31 74 6f 72 73 2c 0a 2a 2a 20 74 68 65 6e 20 57 68  tors,.** then Wh
37b32 65 72 65 54 65 72 6d 2e 6c 65 66 74 43 75 72 73  ereTerm.leftCurs
37b33 6f 72 20 61 6e 64 20 57 68 65 72 65 54 65 72 6d  or and WhereTerm
37b34 2e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 72 65  .u.leftColumn re
37b35 63 6f 72 64 20 74 68 65 0a 2a 2a 20 63 75 72 73  cord the.** curs
37b36 6f 72 20 6e 75 6d 62 65 72 20 61 6e 64 20 63 6f  or number and co
37b37 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20  lumn number for 
37b38 58 2e 20 20 57 68 65 72 65 54 65 72 6d 2e 65 4f  X.  WhereTerm.eO
37b39 70 65 72 61 74 6f 72 20 72 65 63 6f 72 64 73 0a  perator records.
37b3a 2a 2a 20 74 68 65 20 3c 6f 70 3e 20 75 73 69 6e  ** the <op> usin
37b3b 67 20 61 20 62 69 74 6d 61 73 6b 20 65 6e 63 6f  g a bitmask enco
37b3c 64 69 6e 67 20 64 65 66 69 6e 65 64 20 62 79 20  ding defined by 
37b3d 57 4f 5f 78 78 78 20 62 65 6c 6f 77 2e 20 20 54  WO_xxx below.  T
37b3e 68 65 0a 2a 2a 20 75 73 65 20 6f 66 20 61 20 62  he.** use of a b
37b3f 69 74 6d 61 73 6b 20 65 6e 63 6f 64 69 6e 67 20  itmask encoding 
37b40 66 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f 72  for the operator
37b41 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 73 65   allows us to se
37b42 61 72 63 68 0a 2a 2a 20 71 75 69 63 6b 6c 79 20  arch.** quickly 
37b43 66 6f 72 20 74 65 72 6d 73 20 74 68 61 74 20 6d  for terms that m
37b44 61 74 63 68 20 61 6e 79 20 6f 66 20 73 65 76 65  atch any of seve
37b45 72 61 6c 20 64 69 66 66 65 72 65 6e 74 20 6f 70  ral different op
37b46 65 72 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 41  erators..**.** A
37b47 20 57 68 65 72 65 54 65 72 6d 20 6d 69 67 68 74   WhereTerm might
37b48 20 61 6c 73 6f 20 62 65 20 74 77 6f 20 6f 72 20   also be two or 
37b49 6d 6f 72 65 20 73 75 62 74 65 72 6d 73 20 63 6f  more subterms co
37b4a 6e 6e 65 63 74 65 64 20 62 79 20 4f 52 3a 0a 2a  nnected by OR:.*
37b4b 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 28 74 31  *.**         (t1
37b4c 2e 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 29 20  .X <op> <expr>) 
37b4d 4f 52 20 28 74 31 2e 59 20 3c 6f 70 3e 20 3c 65  OR (t1.Y <op> <e
37b4e 78 70 72 3e 29 20 4f 52 20 2e 2e 2e 2e 0a 2a 2a  xpr>) OR .....**
37b4f 0a 2a 2a 20 49 6e 20 74 68 69 73 20 73 65 63 6f  .** In this seco
37b50 6e 64 20 63 61 73 65 2c 20 77 74 46 6c 61 67 20  nd case, wtFlag 
37b51 61 73 20 74 68 65 20 54 45 52 4d 5f 4f 52 49 4e  as the TERM_ORIN
37b52 46 4f 20 73 65 74 20 61 6e 64 20 65 4f 70 65 72  FO set and eOper
37b53 61 74 6f 72 3d 3d 57 4f 5f 4f 52 0a 2a 2a 20 61  ator==WO_OR.** a
37b54 6e 64 20 74 68 65 20 57 68 65 72 65 54 65 72 6d  nd the WhereTerm
37b55 2e 75 2e 70 4f 72 49 6e 66 6f 20 66 69 65 6c 64  .u.pOrInfo field
37b56 20 70 6f 69 6e 74 73 20 74 6f 20 61 75 78 69 6c   points to auxil
37b57 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  iary information
37b58 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 6f 6c 6c   that.** is coll
37b59 65 63 74 65 64 20 61 62 6f 75 74 20 74 68 65 0a  ected about the.
37b5a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 65 72 6d 20  **.** If a term 
37b5b 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  in the WHERE cla
37b5c 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74  use does not mat
37b5d 63 68 20 65 69 74 68 65 72 20 6f 66 20 74 68 65  ch either of the
37b5e 20 74 77 6f 20 70 72 65 76 69 6f 75 73 0a 2a 2a   two previous.**
37b5f 20 63 61 74 65 67 6f 72 69 65 73 2c 20 74 68 65   categories, the
37b60 6e 20 65 4f 70 65 72 61 74 6f 72 3d 3d 30 2e 20  n eOperator==0. 
37b61 20 54 68 65 20 57 68 65 72 65 54 65 72 6d 2e 70   The WhereTerm.p
37b62 45 78 70 72 20 66 69 65 6c 64 20 69 73 20 73 74  Expr field is st
37b63 69 6c 6c 20 73 65 74 0a 2a 2a 20 74 6f 20 74 68  ill set.** to th
37b64 65 20 6f 72 69 67 69 6e 61 6c 20 73 75 62 65 78  e original subex
37b65 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74 65 6e 74  pression content
37b66 20 61 6e 64 20 77 74 46 6c 61 67 73 20 69 73 20   and wtFlags is 
37b67 73 65 74 20 75 70 20 61 70 70 72 6f 70 72 69 61  set up appropria
37b68 74 65 6c 79 0a 2a 2a 20 62 75 74 20 6e 6f 20 6f  tely.** but no o
37b69 74 68 65 72 20 66 69 65 6c 64 73 20 69 6e 20 74  ther fields in t
37b6a 68 65 20 57 68 65 72 65 54 65 72 6d 20 6f 62 6a  he WhereTerm obj
37b6b 65 63 74 20 61 72 65 20 6d 65 61 6e 69 6e 67 66  ect are meaningf
37b6c 75 6c 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 65  ul..**.** When e
37b6d 4f 70 65 72 61 74 6f 72 21 3d 30 2c 20 70 72 65  Operator!=0, pre
37b6e 72 65 71 52 69 67 68 74 20 61 6e 64 20 70 72 65  reqRight and pre
37b6f 72 65 71 41 6c 6c 20 72 65 63 6f 72 64 20 73 65  reqAll record se
37b70 74 73 20 6f 66 20 63 75 72 73 6f 72 20 6e 75 6d  ts of cursor num
37b71 62 65 72 73 2c 0a 2a 2a 20 62 75 74 20 74 68 65  bers,.** but the
37b72 79 20 64 6f 20 73 6f 20 69 6e 64 69 72 65 63 74  y do so indirect
37b73 6c 79 2e 20 20 41 20 73 69 6e 67 6c 65 20 57 68  ly.  A single Wh
37b74 65 72 65 4d 61 73 6b 53 65 74 20 73 74 72 75 63  ereMaskSet struc
37b75 74 75 72 65 20 74 72 61 6e 73 6c 61 74 65 73 0a  ture translates.
37b76 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  ** cursor number
37b77 20 69 6e 74 6f 20 62 69 74 73 20 61 6e 64 20 74   into bits and t
37b78 68 65 20 74 72 61 6e 73 6c 61 74 65 64 20 62 69  he translated bi
37b79 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74  t is stored in t
37b7a 68 65 20 70 72 65 72 65 71 0a 2a 2a 20 66 69 65  he prereq.** fie
37b7b 6c 64 73 2e 20 20 54 68 65 20 74 72 61 6e 73 6c  lds.  The transl
37b7c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e  ation is used in
37b7d 20 6f 72 64 65 72 20 74 6f 20 6d 61 78 69 6d 69   order to maximi
37b7e 7a 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ze the number of
37b7f 0a 2a 2a 20 62 69 74 73 20 74 68 61 74 20 77 69  .** bits that wi
37b80 6c 6c 20 66 69 74 20 69 6e 20 61 20 42 69 74 6d  ll fit in a Bitm
37b81 61 73 6b 2e 20 20 54 68 65 20 56 44 42 45 20 63  ask.  The VDBE c
37b82 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 6d 69  ursor numbers mi
37b83 67 68 74 20 62 65 0a 2a 2a 20 73 70 72 65 61 64  ght be.** spread
37b84 20 6f 75 74 20 6f 76 65 72 20 74 68 65 20 6e 6f   out over the no
37b85 6e 2d 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67  n-negative integ
37b86 65 72 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  ers.  For exampl
37b87 65 2c 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a  e, the cursor.**
37b88 20 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20 62   numbers might b
37b89 65 20 33 2c 20 38 2c 20 39 2c 20 31 30 2c 20 32  e 3, 8, 9, 10, 2
37b8a 30 2c 20 32 33 2c 20 34 31 2c 20 61 6e 64 20 34  0, 23, 41, and 4
37b8b 35 2e 20 20 54 68 65 20 57 68 65 72 65 4d 61 73  5.  The WhereMas
37b8c 6b 53 65 74 0a 2a 2a 20 74 72 61 6e 73 6c 61 74  kSet.** translat
37b8d 65 73 20 74 68 65 73 65 20 73 70 61 72 73 65 20  es these sparse 
37b8e 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69  cursor numbers i
37b8f 6e 74 6f 20 63 6f 6e 73 65 63 75 74 69 76 65 20  nto consecutive 
37b90 69 6e 74 65 67 65 72 73 0a 2a 2a 20 62 65 67 69  integers.** begi
37b91 6e 6e 69 6e 67 20 77 69 74 68 20 30 20 69 6e 20  nning with 0 in 
37b92 6f 72 64 65 72 20 74 6f 20 6d 61 6b 65 20 74 68  order to make th
37b93 65 20 62 65 73 74 20 70 6f 73 73 69 62 6c 65 20  e best possible 
37b94 75 73 65 20 6f 66 20 74 68 65 20 61 76 61 69 6c  use of the avail
37b95 61 62 6c 65 0a 2a 2a 20 62 69 74 73 20 69 6e 20  able.** bits in 
37b96 74 68 65 20 42 69 74 6d 61 73 6b 2e 20 20 53 6f  the Bitmask.  So
37b97 2c 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65  , in the example
37b98 20 61 62 6f 76 65 2c 20 74 68 65 20 63 75 72 73   above, the curs
37b99 6f 72 20 6e 75 6d 62 65 72 73 0a 2a 2a 20 77 6f  or numbers.** wo
37b9a 75 6c 64 20 62 65 20 6d 61 70 70 65 64 20 69 6e  uld be mapped in
37b9b 74 6f 20 69 6e 74 65 67 65 72 73 20 30 20 74 68  to integers 0 th
37b9c 72 6f 75 67 68 20 37 2e 0a 2a 2a 0a 2a 2a 20 54  rough 7..**.** T
37b9d 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72  he number of ter
37b9e 6d 73 20 69 6e 20 61 20 6a 6f 69 6e 20 69 73 20  ms in a join is 
37b9f 6c 69 6d 69 74 65 64 20 62 79 20 74 68 65 20 6e  limited by the n
37ba0 75 6d 62 65 72 20 6f 66 20 62 69 74 73 0a 2a 2a  umber of bits.**
37ba1 20 69 6e 20 70 72 65 72 65 71 52 69 67 68 74 20   in prereqRight 
37ba2 61 6e 64 20 70 72 65 72 65 71 41 6c 6c 2e 20 20  and prereqAll.  
37ba3 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 36  The default is 6
37ba4 34 20 62 69 74 73 2c 20 68 65 6e 63 65 20 53 51  4 bits, hence SQ
37ba5 4c 69 74 65 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20  Lite.** is only 
37ba6 61 62 6c 65 20 74 6f 20 70 72 6f 63 65 73 73 20  able to process 
37ba7 6a 6f 69 6e 73 20 77 69 74 68 20 36 34 20 6f 72  joins with 64 or
37ba8 20 66 65 77 65 72 20 74 61 62 6c 65 73 2e 0a 2a   fewer tables..*
37ba9 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
37baa 20 57 68 65 72 65 54 65 72 6d 20 57 68 65 72 65   WhereTerm Where
37bab 54 65 72 6d 3b 0a 73 74 72 75 63 74 20 57 68 65  Term;.struct Whe
37bac 72 65 54 65 72 6d 20 7b 0a 20 20 45 78 70 72 20  reTerm {.  Expr 
37bad 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
37bae 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
37baf 20 74 68 65 20 73 75 62 65 78 70 72 65 73 73 69   the subexpressi
37bb0 6f 6e 20 74 68 61 74 20 69 73 20 74 68 69 73 20  on that is this 
37bb1 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 50  term */.  int iP
37bb2 61 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20  arent;          
37bb3 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 70 57 43    /* Disable pWC
37bb4 2d 3e 61 5b 69 50 61 72 65 6e 74 5d 20 77 68 65  ->a[iParent] whe
37bb5 6e 20 74 68 69 73 20 74 65 72 6d 20 64 69 73 61  n this term disa
37bb6 62 6c 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65  bled */.  int le
37bb7 66 74 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20  ftCursor;       
37bb8 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62    /* Cursor numb
37bb9 65 72 20 6f 66 20 58 20 69 6e 20 22 58 20 3c 6f  er of X in "X <o
37bba 70 3e 20 3c 65 78 70 72 3e 22 20 2a 2f 0a 20 20  p> <expr>" */.  
37bbb 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 6e 74 20  union {.    int 
37bbc 6c 65 66 74 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  leftColumn;     
37bbd 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75      /* Column nu
37bbe 6d 62 65 72 20 6f 66 20 58 20 69 6e 20 22 58 20  mber of X in "X 
37bbf 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 20 2a 2f 0a  <op> <expr>" */.
37bc0 20 20 20 20 57 68 65 72 65 4f 72 49 6e 66 6f 20      WhereOrInfo 
37bc1 2a 70 4f 72 49 6e 66 6f 3b 20 20 20 2f 2a 20 45  *pOrInfo;   /* E
37bc2 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  xtra information
37bc3 20 69 66 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57   if eOperator==W
37bc4 4f 5f 4f 52 20 2a 2f 0a 20 20 20 20 57 68 65 72  O_OR */.    Wher
37bc5 65 41 6e 64 49 6e 66 6f 20 2a 70 41 6e 64 49 6e  eAndInfo *pAndIn
37bc6 66 6f 3b 20 2f 2a 20 45 78 74 72 61 20 69 6e 66  fo; /* Extra inf
37bc7 6f 72 6d 61 74 69 6f 6e 20 69 66 20 65 4f 70 65  ormation if eOpe
37bc8 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 2a 2f  rator==WO_AND */
37bc9 0a 20 20 7d 20 75 3b 0a 20 20 75 31 36 20 65 4f  .  } u;.  u16 eO
37bca 70 65 72 61 74 6f 72 3b 20 20 20 20 20 20 20 20  perator;        
37bcb 20 20 2f 2a 20 41 20 57 4f 5f 78 78 20 76 61 6c    /* A WO_xx val
37bcc 75 65 20 64 65 73 63 72 69 62 69 6e 67 20 3c 6f  ue describing <o
37bcd 70 3e 20 2a 2f 0a 20 20 75 38 20 77 74 46 6c 61  p> */.  u8 wtFla
37bce 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
37bcf 2f 2a 20 54 45 52 4d 5f 78 78 78 20 62 69 74 20  /* TERM_xxx bit 
37bd0 66 6c 61 67 73 2e 20 20 53 65 65 20 62 65 6c 6f  flags.  See belo
37bd1 77 20 2a 2f 0a 20 20 75 38 20 6e 43 68 69 6c 64  w */.  u8 nChild
37bd2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
37bd3 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 69 6c  * Number of chil
37bd4 64 72 65 6e 20 74 68 61 74 20 6d 75 73 74 20 64  dren that must d
37bd5 69 73 61 62 6c 65 20 75 73 20 2a 2f 0a 20 20 57  isable us */.  W
37bd6 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b  hereClause *pWC;
37bd7 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6c         /* The cl
37bd8 61 75 73 65 20 74 68 69 73 20 74 65 72 6d 20 69  ause this term i
37bd9 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 42  s part of */.  B
37bda 69 74 6d 61 73 6b 20 70 72 65 72 65 71 52 69 67  itmask prereqRig
37bdb 68 74 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61 73  ht;    /* Bitmas
37bdc 6b 20 6f 66 20 74 61 62 6c 65 73 20 75 73 65 64  k of tables used
37bdd 20 62 79 20 70 45 78 70 72 2d 3e 70 52 69 67 68   by pExpr->pRigh
37bde 74 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70  t */.  Bitmask p
37bdf 72 65 72 65 71 41 6c 6c 3b 20 20 20 20 20 20 2f  rereqAll;      /
37be0 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 74 61 62  * Bitmask of tab
37be1 6c 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 62  les referenced b
37be2 79 20 70 45 78 70 72 20 2a 2f 0a 7d 3b 0a 0a 2f  y pExpr */.};../
37be3 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  *.** Allowed val
37be4 75 65 73 20 6f 66 20 57 68 65 72 65 54 65 72 6d  ues of WhereTerm
37be5 2e 77 74 46 6c 61 67 73 0a 2a 2f 0a 23 64 65 66  .wtFlags.*/.#def
37be6 69 6e 65 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43  ine TERM_DYNAMIC
37be7 20 20 20 20 30 78 30 31 20 20 20 2f 2a 20 4e 65      0x01   /* Ne
37be8 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74  ed to call sqlit
37be9 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
37bea 20 70 45 78 70 72 29 20 2a 2f 0a 23 64 65 66 69   pExpr) */.#defi
37beb 6e 65 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20  ne TERM_VIRTUAL 
37bec 20 20 20 30 78 30 32 20 20 20 2f 2a 20 41 64 64     0x02   /* Add
37bed 65 64 20 62 79 20 74 68 65 20 6f 70 74 69 6d 69  ed by the optimi
37bee 7a 65 72 2e 20 20 44 6f 20 6e 6f 74 20 63 6f 64  zer.  Do not cod
37bef 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52  e */.#define TER
37bf0 4d 5f 43 4f 44 45 44 20 20 20 20 20 20 30 78 30  M_CODED      0x0
37bf1 34 20 20 20 2f 2a 20 54 68 69 73 20 74 65 72 6d  4   /* This term
37bf2 20 69 73 20 61 6c 72 65 61 64 79 20 63 6f 64 65   is already code
37bf3 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52  d */.#define TER
37bf4 4d 5f 43 4f 50 49 45 44 20 20 20 20 20 30 78 30  M_COPIED     0x0
37bf5 38 20 20 20 2f 2a 20 48 61 73 20 61 20 63 68 69  8   /* Has a chi
37bf6 6c 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45  ld */.#define TE
37bf7 52 4d 5f 4f 52 49 4e 46 4f 20 20 20 20 20 30 78  RM_ORINFO     0x
37bf8 31 30 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20  10   /* Need to 
37bf9 66 72 65 65 20 74 68 65 20 57 68 65 72 65 54 65  free the WhereTe
37bfa 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 6f 62 6a  rm.u.pOrInfo obj
37bfb 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54  ect */.#define T
37bfc 45 52 4d 5f 41 4e 44 49 4e 46 4f 20 20 20 20 30  ERM_ANDINFO    0
37bfd 78 32 30 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f  x20   /* Need to
37bfe 20 66 72 65 65 20 74 68 65 20 57 68 65 72 65 54   free the WhereT
37bff 65 72 6d 2e 75 2e 70 41 6e 64 49 6e 66 6f 20 6f  erm.u.pAndInfo o
37c00 62 6a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45  bj */.#define TE
37c01 52 4d 5f 4f 52 5f 4f 4b 20 20 20 20 20 20 30 78  RM_OR_OK      0x
37c02 34 30 20 20 20 2f 2a 20 55 73 65 64 20 64 75 72  40   /* Used dur
37c03 69 6e 67 20 4f 52 2d 63 6c 61 75 73 65 20 70 72  ing OR-clause pr
37c04 6f 63 65 73 73 69 6e 67 20 2a 2f 0a 23 69 66 64  ocessing */.#ifd
37c05 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
37c06 5f 53 54 41 54 33 0a 23 20 20 64 65 66 69 6e 65  _STAT3.#  define
37c07 20 54 45 52 4d 5f 56 4e 55 4c 4c 20 20 20 20 30   TERM_VNULL    0
37c08 78 38 30 20 20 20 2f 2a 20 4d 61 6e 75 66 61 63  x80   /* Manufac
37c09 74 75 72 65 64 20 78 3e 4e 55 4c 4c 20 6f 72 20  tured x>NULL or 
37c0a 78 3c 3d 4e 55 4c 4c 20 74 65 72 6d 20 2a 2f 0a  x<=NULL term */.
37c0b 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20  #else.#  define 
37c0c 54 45 52 4d 5f 56 4e 55 4c 4c 20 20 20 20 30 78  TERM_VNULL    0x
37c0d 30 30 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 64  00   /* Disabled
37c0e 20 69 66 20 6e 6f 74 20 75 73 69 6e 67 20 73 74   if not using st
37c0f 61 74 33 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f  at3 */.#endif../
37c10 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
37c11 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
37c12 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64  g structure hold
37c13 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  s all informatio
37c14 6e 20 61 62 6f 75 74 20 61 0a 2a 2a 20 57 48 45  n about a.** WHE
37c15 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 6f 73 74  RE clause.  Most
37c16 6c 79 20 74 68 69 73 20 69 73 20 61 20 63 6f 6e  ly this is a con
37c17 74 61 69 6e 65 72 20 66 6f 72 20 6f 6e 65 20 6f  tainer for one o
37c18 72 20 6d 6f 72 65 20 57 68 65 72 65 54 65 72 6d  r more WhereTerm
37c19 73 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 6c 61 6e 61  s..**.** Explana
37c1a 74 69 6f 6e 20 6f 66 20 70 4f 75 74 65 72 3a 20  tion of pOuter: 
37c1b 20 46 6f 72 20 61 20 57 48 45 52 45 20 63 6c 61   For a WHERE cla
37c1c 75 73 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a  use of the form.
37c1d 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  **.**           
37c1e 61 20 41 4e 44 20 28 28 62 20 41 4e 44 20 63 29  a AND ((b AND c)
37c1f 20 4f 52 20 28 64 20 41 4e 44 20 65 29 29 20 41   OR (d AND e)) A
37c20 4e 44 20 66 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  ND f.**.** There
37c21 20 61 72 65 20 73 65 70 61 72 61 74 65 20 57 68   are separate Wh
37c22 65 72 65 43 6c 61 75 73 65 20 6f 62 6a 65 63 74  ereClause object
37c23 73 20 66 6f 72 20 74 68 65 20 77 68 6f 6c 65 20  s for the whole 
37c24 63 6c 61 75 73 65 20 61 6e 64 20 66 6f 72 0a 2a  clause and for.*
37c25 2a 20 74 68 65 20 73 75 62 63 6c 61 75 73 65 73  * the subclauses
37c26 20 22 28 62 20 41 4e 44 20 63 29 22 20 61 6e 64   "(b AND c)" and
37c27 20 22 28 64 20 41 4e 44 20 65 29 22 2e 20 20 54   "(d AND e)".  T
37c28 68 65 20 70 4f 75 74 65 72 20 66 69 65 6c 64 20  he pOuter field 
37c29 6f 66 20 74 68 65 0a 2a 2a 20 73 75 62 63 6c 61  of the.** subcla
37c2a 75 73 65 73 20 70 6f 69 6e 74 73 20 74 6f 20 74  uses points to t
37c2b 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 6f  he WhereClause o
37c2c 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 77 68  bject for the wh
37c2d 6f 6c 65 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73  ole clause..*/.s
37c2e 74 72 75 63 74 20 57 68 65 72 65 43 6c 61 75 73  truct WhereClaus
37c2f 65 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  e {.  Parse *pPa
37c30 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  rse;           /
37c31 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
37c32 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 4d  text */.  WhereM
37c33 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74  askSet *pMaskSet
37c34 3b 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66  ;  /* Mapping of
37c35 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75   table cursor nu
37c36 6d 62 65 72 73 20 74 6f 20 62 69 74 6d 61 73 6b  mbers to bitmask
37c37 73 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 76  s */.  Bitmask v
37c38 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20  mask;           
37c39 2f 2a 20 42 69 74 6d 61 73 6b 20 69 64 65 6e 74  /* Bitmask ident
37c3a 69 66 79 69 6e 67 20 76 69 72 74 75 61 6c 20 74  ifying virtual t
37c3b 61 62 6c 65 20 63 75 72 73 6f 72 73 20 2a 2f 0a  able cursors */.
37c3c 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
37c3d 4f 75 74 65 72 3b 20 20 20 20 20 2f 2a 20 4f 75  Outer;     /* Ou
37c3e 74 65 72 20 63 6f 6e 6a 75 6e 63 74 69 6f 6e 20  ter conjunction 
37c3f 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20  */.  u8 op;     
37c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
37c41 20 53 70 6c 69 74 20 6f 70 65 72 61 74 6f 72 2e   Split operator.
37c42 20 20 54 4b 5f 41 4e 44 20 6f 72 20 54 4b 5f 4f    TK_AND or TK_O
37c43 52 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72 6c  R */.  u16 wctrl
37c44 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  Flags;          
37c45 2f 2a 20 4d 69 67 68 74 20 69 6e 63 6c 75 64 65  /* Might include
37c46 20 57 48 45 52 45 5f 41 4e 44 5f 4f 4e 4c 59 20   WHERE_AND_ONLY 
37c47 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20  */.  int nTerm; 
37c48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
37c49 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   Number of terms
37c4a 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6c 6f 74 3b   */.  int nSlot;
37c4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37c4c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
37c4d 69 65 73 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20 20  ies in a[] */.  
37c4e 57 68 65 72 65 54 65 72 6d 20 2a 61 3b 20 20 20  WhereTerm *a;   
37c4f 20 20 20 20 20 20 20 20 20 2f 2a 20 45 61 63 68           /* Each
37c50 20 61 5b 5d 20 64 65 73 63 72 69 62 65 73 20 61   a[] describes a
37c51 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45   term of the WHE
37c52 52 45 20 63 6c 75 61 73 65 20 2a 2f 0a 23 69 66  RE cluase */.#if
37c53 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
37c54 53 4d 41 4c 4c 5f 53 54 41 43 4b 29 0a 20 20 57  SMALL_STACK).  W
37c55 68 65 72 65 54 65 72 6d 20 61 53 74 61 74 69 63  hereTerm aStatic
37c56 5b 31 5d 3b 20 20 20 20 2f 2a 20 49 6e 69 74 69  [1];    /* Initi
37c57 61 6c 20 73 74 61 74 69 63 20 73 70 61 63 65 20  al static space 
37c58 66 6f 72 20 61 5b 5d 20 2a 2f 0a 23 65 6c 73 65  for a[] */.#else
37c59 0a 20 20 57 68 65 72 65 54 65 72 6d 20 61 53 74  .  WhereTerm aSt
37c5a 61 74 69 63 5b 38 5d 3b 20 20 20 20 2f 2a 20 49  atic[8];    /* I
37c5b 6e 69 74 69 61 6c 20 73 74 61 74 69 63 20 73 70  nitial static sp
37c5c 61 63 65 20 66 6f 72 20 61 5b 5d 20 2a 2f 0a 23  ace for a[] */.#
37c5d 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
37c5e 41 20 57 68 65 72 65 54 65 72 6d 20 77 69 74 68  A WhereTerm with
37c5f 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f   eOperator==WO_O
37c60 52 20 68 61 73 20 69 74 73 20 75 2e 70 4f 72 49  R has its u.pOrI
37c61 6e 66 6f 20 70 6f 69 6e 74 65 72 20 73 65 74 20  nfo pointer set 
37c62 74 6f 0a 2a 2a 20 61 20 64 79 6e 61 6d 69 63 61  to.** a dynamica
37c63 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e  lly allocated in
37c64 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
37c65 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
37c66 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65  e..*/.struct Whe
37c67 72 65 4f 72 49 6e 66 6f 20 7b 0a 20 20 57 68 65  reOrInfo {.  Whe
37c68 72 65 43 6c 61 75 73 65 20 77 63 3b 20 20 20 20  reClause wc;    
37c69 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f        /* Decompo
37c6a 73 69 74 69 6f 6e 20 69 6e 74 6f 20 73 75 62 74  sition into subt
37c6b 65 72 6d 73 20 2a 2f 0a 20 20 42 69 74 6d 61 73  erms */.  Bitmas
37c6c 6b 20 69 6e 64 65 78 61 62 6c 65 3b 20 20 20 20  k indexable;    
37c6d 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66     /* Bitmask of
37c6e 20 61 6c 6c 20 69 6e 64 65 78 61 62 6c 65 20 74   all indexable t
37c6f 61 62 6c 65 73 20 69 6e 20 74 68 65 20 63 6c 61  ables in the cla
37c70 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  use */.};../*.**
37c71 20 41 20 57 68 65 72 65 54 65 72 6d 20 77 69 74   A WhereTerm wit
37c72 68 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  h eOperator==WO_
37c73 41 4e 44 20 68 61 73 20 69 74 73 20 75 2e 70 41  AND has its u.pA
37c74 6e 64 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 73  ndInfo pointer s
37c75 65 74 20 74 6f 0a 2a 2a 20 61 20 64 79 6e 61 6d  et to.** a dynam
37c76 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
37c77 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
37c78 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
37c79 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ture..*/.struct 
37c7a 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 7b 0a 20  WhereAndInfo {. 
37c7b 20 57 68 65 72 65 43 6c 61 75 73 65 20 77 63 3b   WhereClause wc;
37c7c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
37c7d 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 62   subexpression b
37c7e 72 6f 6b 65 6e 20 6f 75 74 20 2a 2f 0a 7d 3b 0a  roken out */.};.
37c7f 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
37c80 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
37c81 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65  ing structure ke
37c82 65 70 73 20 74 72 61 63 6b 20 6f 66 20 61 20 6d  eps track of a m
37c83 61 70 70 69 6e 67 0a 2a 2a 20 62 65 74 77 65 65  apping.** betwee
37c84 6e 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  n VDBE cursor nu
37c85 6d 62 65 72 73 20 61 6e 64 20 62 69 74 73 20 6f  mbers and bits o
37c86 66 20 74 68 65 20 62 69 74 6d 61 73 6b 73 20 69  f the bitmasks i
37c87 6e 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a 2a 0a  n WhereTerm..**.
37c88 2a 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73  ** The VDBE curs
37c89 6f 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 73  or numbers are s
37c8a 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 20 63 6f  mall integers co
37c8b 6e 74 61 69 6e 65 64 20 69 6e 20 0a 2a 2a 20 53  ntained in .** S
37c8c 72 63 4c 69 73 74 5f 69 74 65 6d 2e 69 43 75 72  rcList_item.iCur
37c8d 73 6f 72 20 61 6e 64 20 45 78 70 72 2e 69 54 61  sor and Expr.iTa
37c8e 62 6c 65 20 66 69 65 6c 64 73 2e 20 20 46 6f 72  ble fields.  For
37c8f 20 61 6e 79 20 67 69 76 65 6e 20 57 48 45 52 45   any given WHERE
37c90 20 0a 2a 2a 20 63 6c 61 75 73 65 2c 20 74 68 65   .** clause, the
37c91 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20   cursor numbers 
37c92 6d 69 67 68 74 20 6e 6f 74 20 62 65 67 69 6e 20  might not begin 
37c93 77 69 74 68 20 30 20 61 6e 64 20 74 68 65 79 20  with 0 and they 
37c94 6d 69 67 68 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e  might.** contain
37c95 20 67 61 70 73 20 69 6e 20 74 68 65 20 6e 75 6d   gaps in the num
37c96 62 65 72 69 6e 67 20 73 65 71 75 65 6e 63 65 2e  bering sequence.
37c97 20 20 42 75 74 20 77 65 20 77 61 6e 74 20 74 6f    But we want to
37c98 20 6d 61 6b 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a   make maximum.**
37c99 20 75 73 65 20 6f 66 20 74 68 65 20 62 69 74 73   use of the bits
37c9a 20 69 6e 20 6f 75 72 20 62 69 74 6d 61 73 6b 73   in our bitmasks
37c9b 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72  .  This structur
37c9c 65 20 70 72 6f 76 69 64 65 73 20 61 20 6d 61 70  e provides a map
37c9d 70 69 6e 67 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  ping.** from the
37c9e 20 73 70 61 72 73 65 20 63 75 72 73 6f 72 20 6e   sparse cursor n
37c9f 75 6d 62 65 72 73 20 69 6e 74 6f 20 63 6f 6e 73  umbers into cons
37ca0 65 63 75 74 69 76 65 20 69 6e 74 65 67 65 72 73  ecutive integers
37ca1 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69   beginning.** wi
37ca2 74 68 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 57  th 0..**.** If W
37ca3 68 65 72 65 4d 61 73 6b 53 65 74 2e 69 78 5b 41  hereMaskSet.ix[A
37ca4 5d 3d 3d 42 20 69 74 20 6d 65 61 6e 73 20 74 68  ]==B it means th
37ca5 61 74 20 54 68 65 20 41 2d 74 68 20 62 69 74 20  at The A-th bit 
37ca6 6f 66 20 61 20 42 69 74 6d 61 73 6b 0a 2a 2a 20  of a Bitmask.** 
37ca7 63 6f 72 72 65 73 70 6f 6e 64 73 20 56 44 42 45  corresponds VDBE
37ca8 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 42   cursor number B
37ca9 2e 20 20 54 68 65 20 41 2d 74 68 20 62 69 74 20  .  The A-th bit 
37caa 6f 66 20 61 20 62 69 74 6d 61 73 6b 20 69 73 20  of a bitmask is 
37cab 31 3c 3c 41 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  1<<A..**.** For 
37cac 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20  example, if the 
37cad 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78 70  WHERE clause exp
37cae 72 65 73 73 69 6f 6e 20 75 73 65 64 20 74 68 65  ression used the
37caf 73 65 20 56 44 42 45 0a 2a 2a 20 63 75 72 73 6f  se VDBE.** curso
37cb0 72 73 3a 20 20 34 2c 20 35 2c 20 38 2c 20 32 39  rs:  4, 5, 8, 29
37cb1 2c 20 35 37 2c 20 37 33 2e 20 20 54 68 65 6e 20  , 57, 73.  Then 
37cb2 74 68 65 20 20 57 68 65 72 65 4d 61 73 6b 53 65  the  WhereMaskSe
37cb3 74 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 77  t structure.** w
37cb4 6f 75 6c 64 20 6d 61 70 20 74 68 6f 73 65 20 63  ould map those c
37cb5 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e  ursor numbers in
37cb6 74 6f 20 62 69 74 73 20 30 20 74 68 72 6f 75 67  to bits 0 throug
37cb7 68 20 35 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  h 5..**.** Note 
37cb8 74 68 61 74 20 74 68 65 20 6d 61 70 70 69 6e 67  that the mapping
37cb9 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   is not necessar
37cba 69 6c 79 20 6f 72 64 65 72 65 64 2e 20 20 49 6e  ily ordered.  In
37cbb 20 74 68 65 20 65 78 61 6d 70 6c 65 0a 2a 2a 20   the example.** 
37cbc 61 62 6f 76 65 2c 20 74 68 65 20 6d 61 70 70 69  above, the mappi
37cbd 6e 67 20 6d 69 67 68 74 20 67 6f 20 6c 69 6b 65  ng might go like
37cbe 20 74 68 69 73 3a 20 20 34 2d 3e 33 2c 20 35 2d   this:  4->3, 5-
37cbf 3e 31 2c 20 38 2d 3e 32 2c 20 32 39 2d 3e 30 2c  >1, 8->2, 29->0,
37cc0 0a 2a 2a 20 35 37 2d 3e 35 2c 20 37 33 2d 3e 34  .** 57->5, 73->4
37cc1 2e 20 20 4f 72 20 6f 6e 65 20 6f 66 20 37 31 39  .  Or one of 719
37cc2 20 6f 74 68 65 72 20 63 6f 6d 62 69 6e 61 74 69   other combinati
37cc3 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 75 73 65  ons might be use
37cc4 64 2e 20 49 74 0a 2a 2a 20 64 6f 65 73 20 6e 6f  d. It.** does no
37cc5 74 20 72 65 61 6c 6c 79 20 6d 61 74 74 65 72 2e  t really matter.
37cc6 20 20 57 68 61 74 20 69 73 20 69 6d 70 6f 72 74    What is import
37cc7 61 6e 74 20 69 73 20 74 68 61 74 20 73 70 61 72  ant is that spar
37cc8 73 65 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d  se cursor.** num
37cc9 62 65 72 73 20 61 6c 6c 20 67 65 74 20 6d 61 70  bers all get map
37cca 70 65 64 20 69 6e 74 6f 20 62 69 74 20 6e 75 6d  ped into bit num
37ccb 62 65 72 73 20 74 68 61 74 20 62 65 67 69 6e 20  bers that begin 
37ccc 77 69 74 68 20 30 20 61 6e 64 20 63 6f 6e 74 61  with 0 and conta
37ccd 69 6e 0a 2a 2a 20 6e 6f 20 67 61 70 73 2e 0a 2a  in.** no gaps..*
37cce 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 4d 61  /.struct WhereMa
37ccf 73 6b 53 65 74 20 7b 0a 20 20 69 6e 74 20 6e 3b  skSet {.  int n;
37cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37cd1 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
37cd2 72 20 6f 66 20 61 73 73 69 67 6e 65 64 20 63 75  r of assigned cu
37cd3 72 73 6f 72 20 76 61 6c 75 65 73 20 2a 2f 0a 20  rsor values */. 
37cd4 20 69 6e 74 20 69 78 5b 42 4d 53 5d 3b 20 20 20   int ix[BMS];   
37cd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37cd6 2a 20 43 75 72 73 6f 72 20 61 73 73 69 67 6e 65  * Cursor assigne
37cd7 64 20 74 6f 20 65 61 63 68 20 62 69 74 20 2a 2f  d to each bit */
37cd8 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65  .};../*.** A Whe
37cd9 72 65 43 6f 73 74 20 6f 62 6a 65 63 74 20 72 65  reCost object re
37cda 63 6f 72 64 73 20 61 20 6c 6f 6f 6b 75 70 20 73  cords a lookup s
37cdb 74 72 61 74 65 67 79 20 61 6e 64 20 74 68 65 20  trategy and the 
37cdc 65 73 74 69 6d 61 74 65 64 0a 2a 2a 20 63 6f 73  estimated.** cos
37cdd 74 20 6f 66 20 70 75 72 73 75 69 6e 67 20 74 68  t of pursuing th
37cde 61 74 20 73 74 72 61 74 65 67 79 2e 0a 2a 2f 0a  at strategy..*/.
37cdf 73 74 72 75 63 74 20 57 68 65 72 65 43 6f 73 74  struct WhereCost
37ce0 20 7b 0a 20 20 57 68 65 72 65 50 6c 61 6e 20 70   {.  WherePlan p
37ce1 6c 61 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 6c  lan;    /* The l
37ce2 6f 6f 6b 75 70 20 73 74 72 61 74 65 67 79 20 2a  ookup strategy *
37ce3 2f 0a 20 20 64 6f 75 62 6c 65 20 72 43 6f 73 74  /.  double rCost
37ce4 3b 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 61 6c  ;      /* Overal
37ce5 6c 20 63 6f 73 74 20 6f 66 20 70 75 72 73 75 69  l cost of pursui
37ce6 6e 67 20 74 68 69 73 20 73 65 61 72 63 68 20 73  ng this search s
37ce7 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 42 69 74  trategy */.  Bit
37ce8 6d 61 73 6b 20 75 73 65 64 3b 20 20 20 20 20 20  mask used;      
37ce9 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 63 75  /* Bitmask of cu
37cea 72 73 6f 72 73 20 75 73 65 64 20 62 79 20 74 68  rsors used by th
37ceb 69 73 20 70 6c 61 6e 20 2a 2f 0a 7d 3b 0a 0a 2f  is plan */.};../
37cec 2a 0a 2a 2a 20 42 69 74 6d 61 73 6b 73 20 66 6f  *.** Bitmasks fo
37ced 72 20 74 68 65 20 6f 70 65 72 61 74 6f 72 73 20  r the operators 
37cee 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 72 65  that indices are
37cef 20 61 62 6c 65 20 74 6f 20 65 78 70 6c 6f 69 74   able to exploit
37cf0 2e 20 20 41 6e 0a 2a 2a 20 4f 52 2d 65 64 20 63  .  An.** OR-ed c
37cf1 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68  ombination of th
37cf2 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20 62  ese values can b
37cf3 65 20 75 73 65 64 20 77 68 65 6e 20 73 65 61 72  e used when sear
37cf4 63 68 69 6e 67 20 66 6f 72 0a 2a 2a 20 74 65 72  ching for.** ter
37cf5 6d 73 20 69 6e 20 74 68 65 20 77 68 65 72 65 20  ms in the where 
37cf6 63 6c 61 75 73 65 2e 0a 2a 2f 0a 23 64 65 66 69  clause..*/.#defi
37cf7 6e 65 20 57 4f 5f 49 4e 20 20 20 20 20 30 78 30  ne WO_IN     0x0
37cf8 30 31 0a 23 64 65 66 69 6e 65 20 57 4f 5f 45 51  01.#define WO_EQ
37cf9 20 20 20 20 20 30 78 30 30 32 0a 23 64 65 66 69       0x002.#defi
37cfa 6e 65 20 57 4f 5f 4c 54 20 20 20 20 20 28 57 4f  ne WO_LT     (WO
37cfb 5f 45 51 3c 3c 28 54 4b 5f 4c 54 2d 54 4b 5f 45  _EQ<<(TK_LT-TK_E
37cfc 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4c  Q)).#define WO_L
37cfd 45 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54  E     (WO_EQ<<(T
37cfe 4b 5f 4c 45 2d 54 4b 5f 45 51 29 29 0a 23 64 65  K_LE-TK_EQ)).#de
37cff 66 69 6e 65 20 57 4f 5f 47 54 20 20 20 20 20 28  fine WO_GT     (
37d00 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47 54 2d 54 4b  WO_EQ<<(TK_GT-TK
37d01 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f  _EQ)).#define WO
37d02 5f 47 45 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c  _GE     (WO_EQ<<
37d03 28 54 4b 5f 47 45 2d 54 4b 5f 45 51 29 29 0a 23  (TK_GE-TK_EQ)).#
37d04 64 65 66 69 6e 65 20 57 4f 5f 4d 41 54 43 48 20  define WO_MATCH 
37d05 20 30 78 30 34 30 0a 23 64 65 66 69 6e 65 20 57   0x040.#define W
37d06 4f 5f 49 53 4e 55 4c 4c 20 30 78 30 38 30 0a 23  O_ISNULL 0x080.#
37d07 64 65 66 69 6e 65 20 57 4f 5f 4f 52 20 20 20 20  define WO_OR    
37d08 20 30 78 31 30 30 20 20 20 20 20 20 20 2f 2a 20   0x100       /* 
37d09 54 77 6f 20 6f 72 20 6d 6f 72 65 20 4f 52 2d 63  Two or more OR-c
37d0a 6f 6e 6e 65 63 74 65 64 20 74 65 72 6d 73 20 2a  onnected terms *
37d0b 2f 0a 23 64 65 66 69 6e 65 20 57 4f 5f 41 4e 44  /.#define WO_AND
37d0c 20 20 20 20 30 78 32 30 30 20 20 20 20 20 20 20      0x200       
37d0d 2f 2a 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20 41  /* Two or more A
37d0e 4e 44 2d 63 6f 6e 6e 65 63 74 65 64 20 74 65 72  ND-connected ter
37d0f 6d 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 4f  ms */.#define WO
37d10 5f 4e 4f 4f 50 20 20 20 30 78 38 30 30 20 20 20  _NOOP   0x800   
37d11 20 20 20 20 2f 2a 20 54 68 69 73 20 74 65 72 6d      /* This term
37d12 20 64 6f 65 73 20 6e 6f 74 20 72 65 73 74 72 69   does not restri
37d13 63 74 20 73 65 61 72 63 68 20 73 70 61 63 65 20  ct search space 
37d14 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 57 4f 5f 41  */..#define WO_A
37d15 4c 4c 20 20 20 20 30 78 66 66 66 20 20 20 20 20  LL    0xfff     
37d16 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 61 6c 6c    /* Mask of all
37d17 20 70 6f 73 73 69 62 6c 65 20 57 4f 5f 2a 20 76   possible WO_* v
37d18 61 6c 75 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65  alues */.#define
37d19 20 57 4f 5f 53 49 4e 47 4c 45 20 30 78 30 66 66   WO_SINGLE 0x0ff
37d1a 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
37d1b 66 20 61 6c 6c 20 6e 6f 6e 2d 63 6f 6d 70 6f 75  f all non-compou
37d1c 6e 64 20 57 4f 5f 2a 20 76 61 6c 75 65 73 20 2a  nd WO_* values *
37d1d 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 75 65 20 66  /../*.** Value f
37d1e 6f 72 20 77 73 46 6c 61 67 73 20 72 65 74 75 72  or wsFlags retur
37d1f 6e 65 64 20 62 79 20 62 65 73 74 49 6e 64 65 78  ned by bestIndex
37d20 28 29 20 61 6e 64 20 73 74 6f 72 65 64 20 69 6e  () and stored in
37d21 0a 2a 2a 20 57 68 65 72 65 4c 65 76 65 6c 2e 77  .** WhereLevel.w
37d22 73 46 6c 61 67 73 2e 20 20 54 68 65 73 65 20 66  sFlags.  These f
37d23 6c 61 67 73 20 64 65 74 65 72 6d 69 6e 65 20 77  lags determine w
37d24 68 69 63 68 20 73 65 61 72 63 68 0a 2a 2a 20 73  hich search.** s
37d25 74 72 61 74 65 67 69 65 73 20 61 72 65 20 61 70  trategies are ap
37d26 70 72 6f 70 72 69 61 74 65 2e 0a 2a 2a 0a 2a 2a  propriate..**.**
37d27 20 54 68 65 20 6c 65 61 73 74 20 73 69 67 6e 69   The least signi
37d28 66 69 63 61 6e 74 20 31 32 20 62 69 74 73 20 69  ficant 12 bits i
37d29 73 20 72 65 73 65 72 76 65 64 20 61 73 20 61 20  s reserved as a 
37d2a 6d 61 73 6b 20 66 6f 72 20 57 4f 5f 20 76 61 6c  mask for WO_ val
37d2b 75 65 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 54 68  ues above..** Th
37d2c 65 20 57 68 65 72 65 4c 65 76 65 6c 2e 77 73 46  e WhereLevel.wsF
37d2d 6c 61 67 73 20 66 69 65 6c 64 20 69 73 20 75 73  lags field is us
37d2e 75 61 6c 6c 79 20 73 65 74 20 74 6f 20 57 4f 5f  ually set to WO_
37d2f 49 4e 7c 57 4f 5f 45 51 7c 57 4f 5f 49 53 4e 55  IN|WO_EQ|WO_ISNU
37d30 4c 4c 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68  LL..** But if th
37d31 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 72  e table is the r
37d32 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20  ight table of a 
37d33 6c 65 66 74 20 6a 6f 69 6e 2c 20 57 68 65 72 65  left join, Where
37d34 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73 0a 2a 2a  Level.wsFlags.**
37d35 20 69 73 20 73 65 74 20 74 6f 20 57 4f 5f 49 4e   is set to WO_IN
37d36 7c 57 4f 5f 45 51 2e 20 20 54 68 65 20 57 68 65  |WO_EQ.  The Whe
37d37 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73 20  reLevel.wsFlags 
37d38 66 69 65 6c 64 20 63 61 6e 20 74 68 65 6e 20 62  field can then b
37d39 65 20 75 73 65 64 20 61 73 0a 2a 2a 20 74 68 65  e used as.** the
37d3a 20 22 6f 70 22 20 70 61 72 61 6d 65 74 65 72 20   "op" parameter 
37d3b 74 6f 20 66 69 6e 64 54 65 72 6d 20 77 68 65 6e  to findTerm when
37d3c 20 77 65 20 61 72 65 20 72 65 73 6f 6c 76 69 6e   we are resolvin
37d3d 67 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  g equality const
37d3e 72 61 69 6e 74 73 2e 0a 2a 2a 20 49 53 4e 55 4c  raints..** ISNUL
37d3f 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 69  L constraints wi
37d40 6c 6c 20 74 68 65 6e 20 6e 6f 74 20 62 65 20 75  ll then not be u
37d41 73 65 64 20 6f 6e 20 74 68 65 20 72 69 67 68 74  sed on the right
37d42 20 74 61 62 6c 65 20 6f 66 20 61 20 6c 65 66 74   table of a left
37d43 0a 2a 2a 20 6a 6f 69 6e 2e 20 20 54 69 63 6b 65  .** join.  Ticke
37d44 74 73 20 23 32 31 37 37 20 61 6e 64 20 23 32 31  ts #2177 and #21
37d45 38 39 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57  89..*/.#define W
37d46 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 20 20  HERE_ROWID_EQ   
37d47 20 20 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a    0x00001000  /*
37d48 20 72 6f 77 69 64 3d 45 58 50 52 20 6f 72 20 72   rowid=EXPR or r
37d49 6f 77 69 64 20 49 4e 20 28 2e 2e 2e 29 20 2a 2f  owid IN (...) */
37d4a 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 52  .#define WHERE_R
37d4b 4f 57 49 44 5f 52 41 4e 47 45 20 20 30 78 30 30  OWID_RANGE  0x00
37d4c 30 30 32 30 30 30 20 20 2f 2a 20 72 6f 77 69 64  002000  /* rowid
37d4d 3c 45 58 50 52 20 61 6e 64 2f 6f 72 20 72 6f 77  <EXPR and/or row
37d4e 69 64 3e 45 58 50 52 20 2a 2f 0a 23 64 65 66 69  id>EXPR */.#defi
37d4f 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  ne WHERE_COLUMN_
37d50 45 51 20 20 20 20 30 78 30 30 30 31 30 30 30 30  EQ    0x00010000
37d51 20 20 2f 2a 20 78 3d 45 58 50 52 20 6f 72 20 78    /* x=EXPR or x
37d52 20 49 4e 20 28 2e 2e 2e 29 20 6f 72 20 78 20 49   IN (...) or x I
37d53 53 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e  S NULL */.#defin
37d54 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52  e WHERE_COLUMN_R
37d55 41 4e 47 45 20 30 78 30 30 30 32 30 30 30 30 20  ANGE 0x00020000 
37d56 20 2f 2a 20 78 3c 45 58 50 52 20 61 6e 64 2f 6f   /* x<EXPR and/o
37d57 72 20 78 3e 45 58 50 52 20 2a 2f 0a 23 64 65 66  r x>EXPR */.#def
37d58 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  ine WHERE_COLUMN
37d59 5f 49 4e 20 20 20 20 30 78 30 30 30 34 30 30 30  _IN    0x0004000
37d5a 30 20 20 2f 2a 20 78 20 49 4e 20 28 2e 2e 2e 29  0  /* x IN (...)
37d5b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
37d5c 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 20 20 30  E_COLUMN_NULL  0
37d5d 78 30 30 30 38 30 30 30 30 20 20 2f 2a 20 78 20  x00080000  /* x 
37d5e 49 53 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69  IS NULL */.#defi
37d5f 6e 65 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44  ne WHERE_INDEXED
37d60 20 20 20 20 20 20 30 78 30 30 30 66 30 30 30 30        0x000f0000
37d61 20 20 2f 2a 20 41 6e 79 74 68 69 6e 67 20 74 68    /* Anything th
37d62 61 74 20 75 73 65 73 20 61 6e 20 69 6e 64 65 78  at uses an index
37d63 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
37d64 45 5f 4e 4f 54 5f 46 55 4c 4c 53 43 41 4e 20 30  E_NOT_FULLSCAN 0
37d65 78 31 30 30 66 33 30 30 30 20 20 2f 2a 20 44 6f  x100f3000  /* Do
37d66 65 73 20 6e 6f 74 20 64 6f 20 61 20 66 75 6c 6c  es not do a full
37d67 20 74 61 62 6c 65 20 73 63 61 6e 20 2a 2f 0a 23   table scan */.#
37d68 64 65 66 69 6e 65 20 57 48 45 52 45 5f 49 4e 5f  define WHERE_IN_
37d69 41 42 4c 45 20 20 20 20 20 20 30 78 30 30 30 66  ABLE      0x000f
37d6a 31 30 30 30 20 20 2f 2a 20 41 62 6c 65 20 74 6f  1000  /* Able to
37d6b 20 73 75 70 70 6f 72 74 20 61 6e 20 49 4e 20 6f   support an IN o
37d6c 70 65 72 61 74 6f 72 20 2a 2f 0a 23 64 65 66 69  perator */.#defi
37d6d 6e 65 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d  ne WHERE_TOP_LIM
37d6e 49 54 20 20 20 20 30 78 30 30 31 30 30 30 30 30  IT    0x00100000
37d6f 20 20 2f 2a 20 78 3c 45 58 50 52 20 6f 72 20 78    /* x<EXPR or x
37d70 3c 3d 45 58 50 52 20 63 6f 6e 73 74 72 61 69 6e  <=EXPR constrain
37d71 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  t */.#define WHE
37d72 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 20 20 20 20  RE_BTM_LIMIT    
37d73 30 78 30 30 32 30 30 30 30 30 20 20 2f 2a 20 78  0x00200000  /* x
37d74 3e 45 58 50 52 20 6f 72 20 78 3e 3d 45 58 50 52  >EXPR or x>=EXPR
37d75 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 23   constraint */.#
37d76 64 65 66 69 6e 65 20 57 48 45 52 45 5f 42 4f 54  define WHERE_BOT
37d77 48 5f 4c 49 4d 49 54 20 20 20 30 78 30 30 33 30  H_LIMIT   0x0030
37d78 30 30 30 30 20 20 2f 2a 20 42 6f 74 68 20 78 3e  0000  /* Both x>
37d79 45 58 50 52 20 61 6e 64 20 78 3c 45 58 50 52 20  EXPR and x<EXPR 
37d7a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
37d7b 5f 49 44 58 5f 4f 4e 4c 59 20 20 20 20 20 30 78  _IDX_ONLY     0x
37d7c 30 30 34 30 30 30 30 30 20 20 2f 2a 20 55 73 65  00400000  /* Use
37d7d 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2d 20 6f 6d   index only - om
37d7e 69 74 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66  it table */.#def
37d7f 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 45  ine WHERE_ORDERE
37d80 44 20 20 20 20 20 20 30 78 30 30 38 30 30 30 30  D      0x0080000
37d81 30 20 20 2f 2a 20 4f 75 74 70 75 74 20 77 69 6c  0  /* Output wil
37d82 6c 20 61 70 70 65 61 72 20 69 6e 20 63 6f 72 72  l appear in corr
37d83 65 63 74 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65  ect order */.#de
37d84 66 69 6e 65 20 57 48 45 52 45 5f 52 45 56 45 52  fine WHERE_REVER
37d85 53 45 20 20 20 20 20 20 30 78 30 31 30 30 30 30  SE      0x010000
37d86 30 30 20 20 2f 2a 20 53 63 61 6e 20 69 6e 20 72  00  /* Scan in r
37d87 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a  everse order */.
37d88 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 55 4e  #define WHERE_UN
37d89 49 51 55 45 20 20 20 20 20 20 20 30 78 30 32 30  IQUE       0x020
37d8a 30 30 30 30 30 20 20 2f 2a 20 53 65 6c 65 63 74  00000  /* Select
37d8b 73 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f  s no more than o
37d8c 6e 65 20 72 6f 77 20 2a 2f 0a 23 64 65 66 69 6e  ne row */.#defin
37d8d 65 20 57 48 45 52 45 5f 41 4c 4c 5f 55 4e 49 51  e WHERE_ALL_UNIQ
37d8e 55 45 20 20 20 30 78 30 34 30 30 30 30 30 30 20  UE   0x04000000 
37d8f 20 2f 2a 20 54 68 69 73 20 61 6e 64 20 61 6c 6c   /* This and all
37d90 20 70 72 69 6f 72 20 68 61 76 65 20 6f 6e 65 20   prior have one 
37d91 72 6f 77 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  row */.#define W
37d92 48 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c  HERE_VIRTUALTABL
37d93 45 20 30 78 30 38 30 30 30 30 30 30 20 20 2f 2a  E 0x08000000  /*
37d94 20 55 73 65 20 76 69 72 74 75 61 6c 2d 74 61 62   Use virtual-tab
37d95 6c 65 20 70 72 6f 63 65 73 73 69 6e 67 20 2a 2f  le processing */
37d96 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4d  .#define WHERE_M
37d97 55 4c 54 49 5f 4f 52 20 20 20 20 20 30 78 31 30  ULTI_OR     0x10
37d98 30 30 30 30 30 30 20 20 2f 2a 20 4f 52 20 75 73  000000  /* OR us
37d99 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20 69 6e 64  ing multiple ind
37d9a 69 63 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ices */.#define 
37d9b 57 48 45 52 45 5f 54 45 4d 50 5f 49 4e 44 45 58  WHERE_TEMP_INDEX
37d9c 20 20 20 30 78 32 30 30 30 30 30 30 30 20 20 2f     0x20000000  /
37d9d 2a 20 55 73 65 73 20 61 6e 20 65 70 68 65 6d 65  * Uses an epheme
37d9e 72 61 6c 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65  ral index */.#de
37d9f 66 69 6e 65 20 57 48 45 52 45 5f 44 49 53 54 49  fine WHERE_DISTI
37da0 4e 43 54 20 20 20 20 20 30 78 34 30 30 30 30 30  NCT     0x400000
37da1 30 30 20 20 2f 2a 20 43 6f 72 72 65 63 74 20 6f  00  /* Correct o
37da2 72 64 65 72 20 66 6f 72 20 44 49 53 54 49 4e 43  rder for DISTINC
37da3 54 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  T */.#define WHE
37da4 52 45 5f 43 4f 56 45 52 5f 53 43 41 4e 20 20 20  RE_COVER_SCAN   
37da5 30 78 38 30 30 30 30 30 30 30 20 20 2f 2a 20 46  0x80000000  /* F
37da6 75 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20 63 6f  ull scan of a co
37da7 76 65 72 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a  vering index */.
37da8 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75  ./*.** This modu
37da9 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 6e 79  le contains many
37daa 20 73 65 70 61 72 61 74 65 20 73 75 62 72 6f 75   separate subrou
37dab 74 69 6e 65 73 20 74 68 61 74 20 77 6f 72 6b 20  tines that work 
37dac 74 6f 67 65 74 68 65 72 20 74 6f 0a 2a 2a 20 66  together to.** f
37dad 69 6e 64 20 74 68 65 20 62 65 73 74 20 69 6e 64  ind the best ind
37dae 69 63 65 73 20 74 6f 20 75 73 65 20 66 6f 72 20  ices to use for 
37daf 61 63 63 65 73 73 69 6e 67 20 61 20 70 61 72 74  accessing a part
37db0 69 63 75 6c 61 72 20 74 61 62 6c 65 20 69 6e 20  icular table in 
37db1 61 20 71 75 65 72 79 2e 0a 2a 2a 20 41 6e 20 69  a query..** An i
37db2 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
37db3 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
37db4 72 65 20 68 6f 6c 64 73 20 63 6f 6e 74 65 78 74  re holds context
37db5 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
37db6 75 74 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20  ut the.** index 
37db7 73 65 61 72 63 68 20 73 6f 20 74 68 61 74 20 69  search so that i
37db8 74 20 63 61 6e 20 62 65 20 6d 6f 72 65 20 65 61  t can be more ea
37db9 73 69 6c 79 20 70 61 73 73 65 64 20 62 65 74 77  sily passed betw
37dba 65 65 6e 20 74 68 65 20 76 61 72 69 6f 75 73 0a  een the various.
37dbb 2a 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a  ** routines..*/.
37dbc 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57  typedef struct W
37dbd 68 65 72 65 42 65 73 74 49 64 78 20 57 68 65 72  hereBestIdx Wher
37dbe 65 42 65 73 74 49 64 78 3b 0a 73 74 72 75 63 74  eBestIdx;.struct
37dbf 20 57 68 65 72 65 42 65 73 74 49 64 78 20 7b 0a   WhereBestIdx {.
37dc0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b    Parse *pParse;
37dc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37dc2 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
37dc3 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c  ext */.  WhereCl
37dc4 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20  ause *pWC;      
37dc5 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
37dc6 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
37dc7 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
37dc8 5f 69 74 65 6d 20 2a 70 53 72 63 3b 20 20 20 20  _item *pSrc;    
37dc9 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c    /* The FROM cl
37dca 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61  ause term to sea
37dcb 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  rch */.  Bitmask
37dcc 20 6e 6f 74 52 65 61 64 79 3b 20 20 20 20 20 20   notReady;      
37dcd 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
37dce 20 6f 66 20 63 75 72 73 6f 72 73 20 6e 6f 74 20   of cursors not 
37dcf 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 42  available */.  B
37dd0 69 74 6d 61 73 6b 20 6e 6f 74 56 61 6c 69 64 3b  itmask notValid;
37dd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37dd2 2a 20 43 75 72 73 6f 72 73 20 6e 6f 74 20 61 76  * Cursors not av
37dd3 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6e 79 20  ailable for any 
37dd4 70 75 72 70 6f 73 65 20 2a 2f 0a 20 20 45 78 70  purpose */.  Exp
37dd5 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b  rList *pOrderBy;
37dd6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37dd7 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
37dd8 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  use */.  ExprLis
37dd9 74 20 2a 70 44 69 73 74 69 6e 63 74 3b 20 20 20  t *pDistinct;   
37dda 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
37ddb 73 65 6c 65 63 74 2d 6c 69 73 74 20 69 66 20 71  select-list if q
37ddc 75 65 72 79 20 69 73 20 44 49 53 54 49 4e 43 54  uery is DISTINCT
37ddd 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
37dde 64 65 78 5f 69 6e 66 6f 20 2a 2a 70 70 49 64 78  dex_info **ppIdx
37ddf 49 6e 66 6f 3b 20 2f 2a 20 49 6e 64 65 78 20 69  Info; /* Index i
37de0 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65  nformation passe
37de1 64 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 20  d to xBestIndex 
37de2 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 20 20  */.  int i, n;  
37de3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37de4 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 6c 6f       /* Which lo
37de5 6f 70 20 69 73 20 62 65 69 6e 67 20 63 6f 64 65  op is being code
37de6 64 3b 20 23 20 6f 66 20 6c 6f 6f 70 73 20 2a 2f  d; # of loops */
37de7 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 61  .  WhereLevel *a
37de8 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20  Level;          
37de9 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74     /* Info about
37dea 20 6f 75 74 65 72 20 6c 6f 6f 70 73 20 2a 2f 0a   outer loops */.
37deb 20 20 57 68 65 72 65 43 6f 73 74 20 63 6f 73 74    WhereCost cost
37dec 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
37ded 20 20 2f 2a 20 4c 6f 77 65 73 74 20 63 6f 73 74    /* Lowest cost
37dee 20 71 75 65 72 79 20 70 6c 61 6e 20 2a 2f 0a 7d   query plan */.}
37def 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  ;../*.** Return 
37df0 54 52 55 45 20 69 66 20 74 68 65 20 70 72 6f 62  TRUE if the prob
37df1 65 20 63 6f 73 74 20 69 73 20 6c 65 73 73 20 74  e cost is less t
37df2 68 61 6e 20 74 68 65 20 62 61 73 65 6c 69 6e 65  han the baseline
37df3 20 63 6f 73 74 0a 2a 2f 0a 73 74 61 74 69 63 20   cost.*/.static 
37df4 69 6e 74 20 63 6f 6d 70 61 72 65 43 6f 73 74 28  int compareCost(
37df5 63 6f 6e 73 74 20 57 68 65 72 65 43 6f 73 74 20  const WhereCost 
37df6 2a 70 50 72 6f 62 65 2c 20 63 6f 6e 73 74 20 57  *pProbe, const W
37df7 68 65 72 65 43 6f 73 74 20 2a 70 42 61 73 65 6c  hereCost *pBasel
37df8 69 6e 65 29 7b 0a 20 20 69 66 28 20 70 50 72 6f  ine){.  if( pPro
37df9 62 65 2d 3e 72 43 6f 73 74 3c 70 42 61 73 65 6c  be->rCost<pBasel
37dfa 69 6e 65 2d 3e 72 43 6f 73 74 20 29 20 72 65 74  ine->rCost ) ret
37dfb 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 70 50 72  urn 1;.  if( pPr
37dfc 6f 62 65 2d 3e 72 43 6f 73 74 3e 70 42 61 73 65  obe->rCost>pBase
37dfd 6c 69 6e 65 2d 3e 72 43 6f 73 74 20 29 20 72 65  line->rCost ) re
37dfe 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 50  turn 0;.  if( pP
37dff 72 6f 62 65 2d 3e 70 6c 61 6e 2e 6e 4f 42 53 61  robe->plan.nOBSa
37e00 74 3e 70 42 61 73 65 6c 69 6e 65 2d 3e 70 6c 61  t>pBaseline->pla
37e01 6e 2e 6e 4f 42 53 61 74 20 29 20 72 65 74 75 72  n.nOBSat ) retur
37e02 6e 20 31 3b 0a 20 20 69 66 28 20 70 50 72 6f 62  n 1;.  if( pProb
37e03 65 2d 3e 70 6c 61 6e 2e 6e 52 6f 77 3c 70 42 61  e->plan.nRow<pBa
37e04 73 65 6c 69 6e 65 2d 3e 70 6c 61 6e 2e 6e 52 6f  seline->plan.nRo
37e05 77 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  w ) return 1;.  
37e06 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
37e07 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20  ** Initialize a 
37e08 70 72 65 61 6c 6c 6f 63 61 74 65 64 20 57 68 65  preallocated Whe
37e09 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75  reClause structu
37e0a 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  re..*/.static vo
37e0b 69 64 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e  id whereClauseIn
37e0c 69 74 28 0a 20 20 57 68 65 72 65 43 6c 61 75 73  it(.  WhereClaus
37e0d 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 2f  e *pWC,        /
37e0e 2a 20 54 68 65 20 57 68 65 72 65 43 6c 61 75 73  * The WhereClaus
37e0f 65 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69  e to be initiali
37e10 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  zed */.  Parse *
37e11 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
37e12 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
37e13 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68   context */.  Wh
37e14 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
37e15 6b 53 65 74 2c 20 20 2f 2a 20 4d 61 70 70 69 6e  kSet,  /* Mappin
37e16 67 20 66 72 6f 6d 20 74 61 62 6c 65 20 63 75 72  g from table cur
37e17 73 6f 72 20 6e 75 6d 62 65 72 73 20 74 6f 20 62  sor numbers to b
37e18 69 74 6d 61 73 6b 73 20 2a 2f 0a 20 20 75 31 36  itmasks */.  u16
37e19 20 77 63 74 72 6c 46 6c 61 67 73 20 20 20 20 20   wctrlFlags     
37e1a 20 20 20 20 20 20 2f 2a 20 4d 69 67 68 74 20 69        /* Might i
37e1b 6e 63 6c 75 64 65 20 57 48 45 52 45 5f 41 4e 44  nclude WHERE_AND
37e1c 5f 4f 4e 4c 59 20 2a 2f 0a 29 7b 0a 20 20 70 57  _ONLY */.){.  pW
37e1d 43 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72  C->pParse = pPar
37e1e 73 65 3b 0a 20 20 70 57 43 2d 3e 70 4d 61 73 6b  se;.  pWC->pMask
37e1f 53 65 74 20 3d 20 70 4d 61 73 6b 53 65 74 3b 0a  Set = pMaskSet;.
37e20 20 20 70 57 43 2d 3e 70 4f 75 74 65 72 20 3d 20    pWC->pOuter = 
37e21 30 3b 0a 20 20 70 57 43 2d 3e 6e 54 65 72 6d 20  0;.  pWC->nTerm 
37e22 3d 20 30 3b 0a 20 20 70 57 43 2d 3e 6e 53 6c 6f  = 0;.  pWC->nSlo
37e23 74 20 3d 20 41 72 72 61 79 53 69 7a 65 28 70 57  t = ArraySize(pW
37e24 43 2d 3e 61 53 74 61 74 69 63 29 3b 0a 20 20 70  C->aStatic);.  p
37e25 57 43 2d 3e 61 20 3d 20 70 57 43 2d 3e 61 53 74  WC->a = pWC->aSt
37e26 61 74 69 63 3b 0a 20 20 70 57 43 2d 3e 76 6d 61  atic;.  pWC->vma
37e27 73 6b 20 3d 20 30 3b 0a 20 20 70 57 43 2d 3e 77  sk = 0;.  pWC->w
37e28 63 74 72 6c 46 6c 61 67 73 20 3d 20 77 63 74 72  ctrlFlags = wctr
37e29 6c 46 6c 61 67 73 3b 0a 7d 0a 0a 2f 2a 20 46 6f  lFlags;.}../* Fo
37e2a 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 20  rward reference 
37e2b 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
37e2c 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28  hereClauseClear(
37e2d 57 68 65 72 65 43 6c 61 75 73 65 2a 29 3b 0a 0a  WhereClause*);..
37e2e 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65  /*.** Deallocate
37e2f 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 73 73 6f   all memory asso
37e30 63 69 61 74 65 64 20 77 69 74 68 20 61 20 57 68  ciated with a Wh
37e31 65 72 65 4f 72 49 6e 66 6f 20 6f 62 6a 65 63 74  ereOrInfo object
37e32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
37e33 20 77 68 65 72 65 4f 72 49 6e 66 6f 44 65 6c 65   whereOrInfoDele
37e34 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  te(sqlite3 *db, 
37e35 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a 70 29 7b  WhereOrInfo *p){
37e36 0a 20 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c  .  whereClauseCl
37e37 65 61 72 28 26 70 2d 3e 77 63 29 3b 0a 20 20 73  ear(&p->wc);.  s
37e38 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
37e39 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65   p);.}../*.** De
37e3a 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 6d 65 6d  allocate all mem
37e3b 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  ory associated w
37e3c 69 74 68 20 61 20 57 68 65 72 65 41 6e 64 49 6e  ith a WhereAndIn
37e3d 66 6f 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74  fo object..*/.st
37e3e 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 41  atic void whereA
37e3f 6e 64 49 6e 66 6f 44 65 6c 65 74 65 28 73 71 6c  ndInfoDelete(sql
37e40 69 74 65 33 20 2a 64 62 2c 20 57 68 65 72 65 41  ite3 *db, WhereA
37e41 6e 64 49 6e 66 6f 20 2a 70 29 7b 0a 20 20 77 68  ndInfo *p){.  wh
37e42 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28 26  ereClauseClear(&
37e43 70 2d 3e 77 63 29 3b 0a 20 20 73 71 6c 69 74 65  p->wc);.  sqlite
37e44 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
37e45 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63  }../*.** Dealloc
37e46 61 74 65 20 61 20 57 68 65 72 65 43 6c 61 75 73  ate a WhereClaus
37e47 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  e structure.  Th
37e48 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 73 74  e WhereClause st
37e49 72 75 63 74 75 72 65 0a 2a 2a 20 69 74 73 65 6c  ructure.** itsel
37e4a 66 20 69 73 20 6e 6f 74 20 66 72 65 65 64 2e 20  f is not freed. 
37e4b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
37e4c 20 74 68 65 20 69 6e 76 65 72 73 65 20 6f 66 20   the inverse of 
37e4d 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28  whereClauseInit(
37e4e 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
37e4f 64 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65  d whereClauseCle
37e50 61 72 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a  ar(WhereClause *
37e51 70 57 43 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  pWC){.  int i;. 
37e52 20 57 68 65 72 65 54 65 72 6d 20 2a 61 3b 0a 20   WhereTerm *a;. 
37e53 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
37e54 57 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62 3b 0a  WC->pParse->db;.
37e55 20 20 66 6f 72 28 69 3d 70 57 43 2d 3e 6e 54 65    for(i=pWC->nTe
37e56 72 6d 2d 31 2c 20 61 3d 70 57 43 2d 3e 61 3b 20  rm-1, a=pWC->a; 
37e57 69 3e 3d 30 3b 20 69 2d 2d 2c 20 61 2b 2b 29 7b  i>=0; i--, a++){
37e58 0a 20 20 20 20 69 66 28 20 61 2d 3e 77 74 46 6c  .    if( a->wtFl
37e59 61 67 73 20 26 20 54 45 52 4d 5f 44 59 4e 41 4d  ags & TERM_DYNAM
37e5a 49 43 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  IC ){.      sqli
37e5b 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
37e5c 2c 20 61 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20  , a->pExpr);.   
37e5d 20 7d 0a 20 20 20 20 69 66 28 20 61 2d 3e 77 74   }.    if( a->wt
37e5e 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 49  Flags & TERM_ORI
37e5f 4e 46 4f 20 29 7b 0a 20 20 20 20 20 20 77 68 65  NFO ){.      whe
37e60 72 65 4f 72 49 6e 66 6f 44 65 6c 65 74 65 28 64  reOrInfoDelete(d
37e61 62 2c 20 61 2d 3e 75 2e 70 4f 72 49 6e 66 6f 29  b, a->u.pOrInfo)
37e62 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
37e63 61 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52  a->wtFlags & TER
37e64 4d 5f 41 4e 44 49 4e 46 4f 20 29 7b 0a 20 20 20  M_ANDINFO ){.   
37e65 20 20 20 77 68 65 72 65 41 6e 64 49 6e 66 6f 44     whereAndInfoD
37e66 65 6c 65 74 65 28 64 62 2c 20 61 2d 3e 75 2e 70  elete(db, a->u.p
37e67 41 6e 64 49 6e 66 6f 29 3b 0a 20 20 20 20 7d 0a  AndInfo);.    }.
37e68 20 20 7d 0a 20 20 69 66 28 20 70 57 43 2d 3e 61    }.  if( pWC->a
37e69 21 3d 70 57 43 2d 3e 61 53 74 61 74 69 63 20 29  !=pWC->aStatic )
37e6a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
37e6b 72 65 65 28 64 62 2c 20 70 57 43 2d 3e 61 29 3b  ree(db, pWC->a);
37e6c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  .  }.}../*.** Ad
37e6d 64 20 61 20 73 69 6e 67 6c 65 20 6e 65 77 20 57  d a single new W
37e6e 68 65 72 65 54 65 72 6d 20 65 6e 74 72 79 20 74  hereTerm entry t
37e6f 6f 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73  o the WhereClaus
37e70 65 20 6f 62 6a 65 63 74 20 70 57 43 2e 0a 2a 2a  e object pWC..**
37e71 20 54 68 65 20 6e 65 77 20 57 68 65 72 65 54 65   The new WhereTe
37e72 72 6d 20 6f 62 6a 65 63 74 20 69 73 20 63 6f 6e  rm object is con
37e73 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 45 78  structed from Ex
37e74 70 72 20 70 20 61 6e 64 20 77 69 74 68 20 77 74  pr p and with wt
37e75 46 6c 61 67 73 2e 0a 2a 2a 20 54 68 65 20 69 6e  Flags..** The in
37e76 64 65 78 20 69 6e 20 70 57 43 2d 3e 61 5b 5d 20  dex in pWC->a[] 
37e77 6f 66 20 74 68 65 20 6e 65 77 20 57 68 65 72 65  of the new Where
37e78 54 65 72 6d 20 69 73 20 72 65 74 75 72 6e 65 64  Term is returned
37e79 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20   on success..** 
37e7a 30 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  0 is returned if
37e7b 20 74 68 65 20 6e 65 77 20 57 68 65 72 65 54 65   the new WhereTe
37e7c 72 6d 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  rm could not be 
37e7d 61 64 64 65 64 20 64 75 65 20 74 6f 20 61 20 6d  added due to a m
37e7e 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  emory.** allocat
37e7f 69 6f 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20  ion error.  The 
37e80 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
37e81 6e 20 66 61 69 6c 75 72 65 20 77 69 6c 6c 20 62  n failure will b
37e82 65 20 72 65 63 6f 72 64 65 64 20 69 6e 0a 2a 2a  e recorded in.**
37e83 20 74 68 65 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   the db->mallocF
37e84 61 69 6c 65 64 20 66 6c 61 67 20 73 6f 20 74 68  ailed flag so th
37e85 61 74 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20  at higher-level 
37e86 66 75 6e 63 74 69 6f 6e 73 20 63 61 6e 20 64 65  functions can de
37e87 74 65 63 74 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54  tect it..**.** T
37e88 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
37e89 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 73 69   increase the si
37e8a 7a 65 20 6f 66 20 74 68 65 20 70 57 43 2d 3e 61  ze of the pWC->a
37e8b 5b 5d 20 61 72 72 61 79 20 61 73 20 6e 65 63 65  [] array as nece
37e8c 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ssary..**.** If 
37e8d 74 68 65 20 77 74 46 6c 61 67 73 20 61 72 67 75  the wtFlags argu
37e8e 6d 65 6e 74 20 69 6e 63 6c 75 64 65 73 20 54 45  ment includes TE
37e8f 52 4d 5f 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e  RM_DYNAMIC, then
37e90 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 0a   responsibility.
37e91 2a 2a 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74  ** for freeing t
37e92 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20  he expression p 
37e93 69 73 20 61 73 73 75 6d 65 64 20 62 79 20 74 68  is assumed by th
37e94 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 6f 62  e WhereClause ob
37e95 6a 65 63 74 20 70 57 43 2e 0a 2a 2a 20 54 68 69  ject pWC..** Thi
37e96 73 20 69 73 20 74 72 75 65 20 65 76 65 6e 20 69  s is true even i
37e97 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  f this routine f
37e98 61 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65  ails to allocate
37e99 20 61 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d   a new WhereTerm
37e9a 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a  ..**.** WARNING:
37e9b 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d    This routine m
37e9c 69 67 68 74 20 72 65 61 6c 6c 6f 63 61 74 65 20  ight reallocate 
37e9d 74 68 65 20 73 70 61 63 65 20 75 73 65 64 20 74  the space used t
37e9e 6f 20 73 74 6f 72 65 0a 2a 2a 20 57 68 65 72 65  o store.** Where
37e9f 54 65 72 6d 73 2e 20 20 41 6c 6c 20 70 6f 69 6e  Terms.  All poin
37ea0 74 65 72 73 20 74 6f 20 57 68 65 72 65 54 65 72  ters to WhereTer
37ea1 6d 73 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 76  ms should be inv
37ea2 61 6c 69 64 61 74 65 64 20 61 66 74 65 72 0a 2a  alidated after.*
37ea3 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72  * calling this r
37ea4 6f 75 74 69 6e 65 2e 20 20 53 75 63 68 20 70 6f  outine.  Such po
37ea5 69 6e 74 65 72 73 20 6d 61 79 20 62 65 20 72 65  inters may be re
37ea6 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 79 20 72  initialized by r
37ea7 65 66 65 72 65 6e 63 69 6e 67 0a 2a 2a 20 74 68  eferencing.** th
37ea8 65 20 70 57 43 2d 3e 61 5b 5d 20 61 72 72 61 79  e pWC->a[] array
37ea9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
37eaa 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
37eab 74 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70  t(WhereClause *p
37eac 57 43 2c 20 45 78 70 72 20 2a 70 2c 20 75 38 20  WC, Expr *p, u8 
37ead 77 74 46 6c 61 67 73 29 7b 0a 20 20 57 68 65 72  wtFlags){.  Wher
37eae 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20  eTerm *pTerm;.  
37eaf 69 6e 74 20 69 64 78 3b 0a 20 20 74 65 73 74 63  int idx;.  testc
37eb0 61 73 65 28 20 77 74 46 6c 61 67 73 20 26 20 54  ase( wtFlags & T
37eb1 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 20 20  ERM_VIRTUAL );  
37eb2 2f 2a 20 45 56 3a 20 52 2d 30 30 32 31 31 2d 31  /* EV: R-00211-1
37eb3 35 31 30 30 20 2a 2f 0a 20 20 69 66 28 20 70 57  5100 */.  if( pW
37eb4 43 2d 3e 6e 54 65 72 6d 3e 3d 70 57 43 2d 3e 6e  C->nTerm>=pWC->n
37eb5 53 6c 6f 74 20 29 7b 0a 20 20 20 20 57 68 65 72  Slot ){.    Wher
37eb6 65 54 65 72 6d 20 2a 70 4f 6c 64 20 3d 20 70 57  eTerm *pOld = pW
37eb7 43 2d 3e 61 3b 0a 20 20 20 20 73 71 6c 69 74 65  C->a;.    sqlite
37eb8 33 20 2a 64 62 20 3d 20 70 57 43 2d 3e 70 50 61  3 *db = pWC->pPa
37eb9 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70 57 43  rse->db;.    pWC
37eba 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  ->a = sqlite3DbM
37ebb 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a  allocRaw(db, siz
37ebc 65 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29 2a 70  eof(pWC->a[0])*p
37ebd 57 43 2d 3e 6e 53 6c 6f 74 2a 32 20 29 3b 0a 20  WC->nSlot*2 );. 
37ebe 20 20 20 69 66 28 20 70 57 43 2d 3e 61 3d 3d 30     if( pWC->a==0
37ebf 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 77 74   ){.      if( wt
37ec0 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 44 59 4e  Flags & TERM_DYN
37ec1 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20  AMIC ){.        
37ec2 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
37ec3 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20  e(db, p);.      
37ec4 7d 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 20 3d  }.      pWC->a =
37ec5 20 70 4f 6c 64 3b 0a 20 20 20 20 20 20 72 65 74   pOld;.      ret
37ec6 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 0;.    }.   
37ec7 20 6d 65 6d 63 70 79 28 70 57 43 2d 3e 61 2c 20   memcpy(pWC->a, 
37ec8 70 4f 6c 64 2c 20 73 69 7a 65 6f 66 28 70 57 43  pOld, sizeof(pWC
37ec9 2d 3e 61 5b 30 5d 29 2a 70 57 43 2d 3e 6e 54 65  ->a[0])*pWC->nTe
37eca 72 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c  rm);.    if( pOl
37ecb 64 21 3d 70 57 43 2d 3e 61 53 74 61 74 69 63 20  d!=pWC->aStatic 
37ecc 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
37ecd 44 62 46 72 65 65 28 64 62 2c 20 70 4f 6c 64 29  DbFree(db, pOld)
37ece 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 57 43 2d  ;.    }.    pWC-
37ecf 3e 6e 53 6c 6f 74 20 3d 20 73 71 6c 69 74 65 33  >nSlot = sqlite3
37ed0 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c  DbMallocSize(db,
37ed1 20 70 57 43 2d 3e 61 29 2f 73 69 7a 65 6f 66 28   pWC->a)/sizeof(
37ed2 70 57 43 2d 3e 61 5b 30 5d 29 3b 0a 20 20 7d 0a  pWC->a[0]);.  }.
37ed3 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e    pTerm = &pWC->
37ed4 61 5b 69 64 78 20 3d 20 70 57 43 2d 3e 6e 54 65  a[idx = pWC->nTe
37ed5 72 6d 2b 2b 5d 3b 0a 20 20 70 54 65 72 6d 2d 3e  rm++];.  pTerm->
37ed6 70 45 78 70 72 20 3d 20 70 3b 0a 20 20 70 54 65  pExpr = p;.  pTe
37ed7 72 6d 2d 3e 77 74 46 6c 61 67 73 20 3d 20 77 74  rm->wtFlags = wt
37ed8 46 6c 61 67 73 3b 0a 20 20 70 54 65 72 6d 2d 3e  Flags;.  pTerm->
37ed9 70 57 43 20 3d 20 70 57 43 3b 0a 20 20 70 54 65  pWC = pWC;.  pTe
37eda 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31  rm->iParent = -1
37edb 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 78 3b 0a  ;.  return idx;.
37edc 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
37edd 75 74 69 6e 65 20 69 64 65 6e 74 69 66 69 65 73  utine identifies
37ede 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20   subexpressions 
37edf 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  in the WHERE cla
37ee0 75 73 65 20 77 68 65 72 65 0a 2a 2a 20 65 61 63  use where.** eac
37ee1 68 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20  h subexpression 
37ee2 69 73 20 73 65 70 61 72 61 74 65 64 20 62 79 20  is separated by 
37ee3 74 68 65 20 41 4e 44 20 6f 70 65 72 61 74 6f 72  the AND operator
37ee4 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a   or some other.*
37ee5 2a 20 6f 70 65 72 61 74 6f 72 20 73 70 65 63 69  * operator speci
37ee6 66 69 65 64 20 69 6e 20 74 68 65 20 6f 70 20 70  fied in the op p
37ee7 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20 57  arameter.  The W
37ee8 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63  hereClause struc
37ee9 74 75 72 65 0a 2a 2a 20 69 73 20 66 69 6c 6c 65  ture.** is fille
37eea 64 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20  d with pointers 
37eeb 74 6f 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  to subexpression
37eec 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a  s.  For example:
37eed 0a 2a 2a 0a 2a 2a 20 20 20 20 57 48 45 52 45 20  .**.**    WHERE 
37eee 20 61 3d 3d 27 68 65 6c 6c 6f 27 20 41 4e 44 20   a=='hello' AND 
37eef 63 6f 61 6c 65 73 63 65 28 62 2c 31 31 29 3c 31  coalesce(b,11)<1
37ef0 30 20 41 4e 44 20 28 63 2b 31 32 21 3d 64 20 4f  0 AND (c+12!=d O
37ef1 52 20 63 3d 3d 32 32 29 0a 2a 2a 20 20 20 20 20  R c==22).**     
37ef2 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f        \________/
37ef3 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f       \__________
37ef4 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f  _____/     \____
37ef5 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a  ____________/.**
37ef6 20 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74              slot
37ef7 5b 30 5d 20 20 20 20 20 20 20 20 20 20 20 20 73  [0]            s
37ef8 6c 6f 74 5b 31 5d 20 20 20 20 20 20 20 20 20 20  lot[1]          
37ef9 20 20 20 20 20 73 6c 6f 74 5b 32 5d 0a 2a 2a 0a       slot[2].**.
37efa 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20  ** The original 
37efb 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20  WHERE clause in 
37efc 70 45 78 70 72 20 69 73 20 75 6e 61 6c 74 65 72  pExpr is unalter
37efd 65 64 2e 20 20 41 6c 6c 20 74 68 69 73 20 72 6f  ed.  All this ro
37efe 75 74 69 6e 65 0a 2a 2a 20 64 6f 65 73 20 69 73  utine.** does is
37eff 20 6d 61 6b 65 20 73 6c 6f 74 5b 5d 20 65 6e 74   make slot[] ent
37f00 72 69 65 73 20 70 6f 69 6e 74 20 74 6f 20 73 75  ries point to su
37f01 62 73 74 72 75 63 74 75 72 65 20 77 69 74 68 69  bstructure withi
37f02 6e 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 49  n pExpr..**.** I
37f03 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73  n the previous s
37f04 65 6e 74 65 6e 63 65 20 61 6e 64 20 69 6e 20 74  entence and in t
37f05 68 65 20 64 69 61 67 72 61 6d 2c 20 22 73 6c 6f  he diagram, "slo
37f06 74 5b 5d 22 20 72 65 66 65 72 73 20 74 6f 0a 2a  t[]" refers to.*
37f07 2a 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73  * the WhereClaus
37f08 65 2e 61 5b 5d 20 61 72 72 61 79 2e 20 20 54 68  e.a[] array.  Th
37f09 65 20 73 6c 6f 74 5b 5d 20 61 72 72 61 79 20 67  e slot[] array g
37f0a 72 6f 77 73 20 61 73 20 6e 65 65 64 65 64 20 74  rows as needed t
37f0b 6f 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 6c 6c  o contain.** all
37f0c 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48   terms of the WH
37f0d 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73  ERE clause..*/.s
37f0e 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65  tatic void where
37f0f 53 70 6c 69 74 28 57 68 65 72 65 43 6c 61 75 73  Split(WhereClaus
37f10 65 20 2a 70 57 43 2c 20 45 78 70 72 20 2a 70 45  e *pWC, Expr *pE
37f11 78 70 72 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20  xpr, int op){.  
37f12 70 57 43 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70  pWC->op = (u8)op
37f13 3b 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30  ;.  if( pExpr==0
37f14 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
37f15 20 70 45 78 70 72 2d 3e 6f 70 21 3d 6f 70 20 29   pExpr->op!=op )
37f16 7b 0a 20 20 20 20 77 68 65 72 65 43 6c 61 75 73  {.    whereClaus
37f17 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70 45 78  eInsert(pWC, pEx
37f18 70 72 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b  pr, 0);.  }else{
37f19 0a 20 20 20 20 77 68 65 72 65 53 70 6c 69 74 28  .    whereSplit(
37f1a 70 57 43 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  pWC, pExpr->pLef
37f1b 74 2c 20 6f 70 29 3b 0a 20 20 20 20 77 68 65 72  t, op);.    wher
37f1c 65 53 70 6c 69 74 28 70 57 43 2c 20 70 45 78 70  eSplit(pWC, pExp
37f1d 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 29 3b 0a  r->pRight, op);.
37f1e 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69    }.}../*.** Ini
37f1f 74 69 61 6c 69 7a 65 20 61 6e 20 65 78 70 72 65  tialize an expre
37f20 73 73 69 6f 6e 20 6d 61 73 6b 20 73 65 74 20 28  ssion mask set (
37f21 61 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 6f  a WhereMaskSet o
37f22 62 6a 65 63 74 29 0a 2a 2f 0a 23 64 65 66 69 6e  bject).*/.#defin
37f23 65 20 69 6e 69 74 4d 61 73 6b 53 65 74 28 50 29  e initMaskSet(P)
37f24 20 20 6d 65 6d 73 65 74 28 50 2c 20 30 2c 20 73    memset(P, 0, s
37f25 69 7a 65 6f 66 28 2a 50 29 29 0a 0a 2f 2a 0a 2a  izeof(*P))../*.*
37f26 2a 20 52 65 74 75 72 6e 20 74 68 65 20 62 69 74  * Return the bit
37f27 6d 61 73 6b 20 66 6f 72 20 74 68 65 20 67 69 76  mask for the giv
37f28 65 6e 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  en cursor number
37f29 2e 20 20 52 65 74 75 72 6e 20 30 20 69 66 0a 2a  .  Return 0 if.*
37f2a 2a 20 69 43 75 72 73 6f 72 20 69 73 20 6e 6f 74  * iCursor is not
37f2b 20 69 6e 20 74 68 65 20 73 65 74 2e 0a 2a 2f 0a   in the set..*/.
37f2c 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 67  static Bitmask g
37f2d 65 74 4d 61 73 6b 28 57 68 65 72 65 4d 61 73 6b  etMask(WhereMask
37f2e 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 69  Set *pMaskSet, i
37f2f 6e 74 20 69 43 75 72 73 6f 72 29 7b 0a 20 20 69  nt iCursor){.  i
37f30 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20  nt i;.  assert( 
37f31 70 4d 61 73 6b 53 65 74 2d 3e 6e 3c 3d 28 69 6e  pMaskSet->n<=(in
37f32 74 29 73 69 7a 65 6f 66 28 42 69 74 6d 61 73 6b  t)sizeof(Bitmask
37f33 29 2a 38 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30  )*8 );.  for(i=0
37f34 3b 20 69 3c 70 4d 61 73 6b 53 65 74 2d 3e 6e 3b  ; i<pMaskSet->n;
37f35 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70   i++){.    if( p
37f36 4d 61 73 6b 53 65 74 2d 3e 69 78 5b 69 5d 3d 3d  MaskSet->ix[i]==
37f37 69 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20  iCursor ){.     
37f38 20 72 65 74 75 72 6e 20 28 28 42 69 74 6d 61 73   return ((Bitmas
37f39 6b 29 31 29 3c 3c 69 3b 0a 20 20 20 20 7d 0a 20  k)1)<<i;.    }. 
37f3a 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
37f3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
37f3c 20 6e 65 77 20 6d 61 73 6b 20 66 6f 72 20 63 75   new mask for cu
37f3d 72 73 6f 72 20 69 43 75 72 73 6f 72 2e 0a 2a 2a  rsor iCursor..**
37f3e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 65  .** There is one
37f3f 20 63 75 72 73 6f 72 20 70 65 72 20 74 61 62 6c   cursor per tabl
37f40 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  e in the FROM cl
37f41 61 75 73 65 2e 20 20 54 68 65 20 6e 75 6d 62 65  ause.  The numbe
37f42 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20 69  r of.** tables i
37f43 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
37f44 65 20 69 73 20 6c 69 6d 69 74 65 64 20 62 79 20  e is limited by 
37f45 61 20 74 65 73 74 20 65 61 72 6c 79 20 69 6e 20  a test early in 
37f46 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 57 68  the.** sqlite3Wh
37f47 65 72 65 42 65 67 69 6e 28 29 20 72 6f 75 74 69  ereBegin() routi
37f48 6e 65 2e 20 20 53 6f 20 77 65 20 6b 6e 6f 77 20  ne.  So we know 
37f49 74 68 61 74 20 74 68 65 20 70 4d 61 73 6b 53 65  that the pMaskSe
37f4a 74 2d 3e 69 78 5b 5d 0a 2a 2a 20 61 72 72 61 79  t->ix[].** array
37f4b 20 77 69 6c 6c 20 6e 65 76 65 72 20 6f 76 65 72   will never over
37f4c 66 6c 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  flow..*/.static 
37f4d 76 6f 69 64 20 63 72 65 61 74 65 4d 61 73 6b 28  void createMask(
37f4e 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d  WhereMaskSet *pM
37f4f 61 73 6b 53 65 74 2c 20 69 6e 74 20 69 43 75 72  askSet, int iCur
37f50 73 6f 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  sor){.  assert( 
37f51 70 4d 61 73 6b 53 65 74 2d 3e 6e 20 3c 20 41 72  pMaskSet->n < Ar
37f52 72 61 79 53 69 7a 65 28 70 4d 61 73 6b 53 65 74  raySize(pMaskSet
37f53 2d 3e 69 78 29 20 29 3b 0a 20 20 70 4d 61 73 6b  ->ix) );.  pMask
37f54 53 65 74 2d 3e 69 78 5b 70 4d 61 73 6b 53 65 74  Set->ix[pMaskSet
37f55 2d 3e 6e 2b 2b 5d 20 3d 20 69 43 75 72 73 6f 72  ->n++] = iCursor
37f56 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
37f57 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20 28 72  routine walks (r
37f58 65 63 75 72 73 69 76 65 6c 79 29 20 61 6e 20 65  ecursively) an e
37f59 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61  xpression tree a
37f5a 6e 64 20 67 65 6e 65 72 61 74 65 73 0a 2a 2a 20  nd generates.** 
37f5b 61 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61  a bitmask indica
37f5c 74 69 6e 67 20 77 68 69 63 68 20 74 61 62 6c 65  ting which table
37f5d 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 68  s are used in th
37f5e 61 74 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  at expression.**
37f5f 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20   tree..**.** In 
37f60 6f 72 64 65 72 20 66 6f 72 20 74 68 69 73 20 72  order for this r
37f61 6f 75 74 69 6e 65 20 74 6f 20 77 6f 72 6b 2c 20  outine to work, 
37f62 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  the calling func
37f63 74 69 6f 6e 20 6d 75 73 74 20 68 61 76 65 0a 2a  tion must have.*
37f64 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 76  * previously inv
37f65 6f 6b 65 64 20 73 71 6c 69 74 65 33 52 65 73 6f  oked sqlite3Reso
37f66 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 6f  lveExprNames() o
37f67 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
37f68 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 20 68 65  .  See.** the he
37f69 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20  ader comment on 
37f6a 74 68 61 74 20 72 6f 75 74 69 6e 65 20 66 6f 72  that routine for
37f6b 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
37f6c 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20  rmation..** The 
37f6d 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
37f6e 70 72 4e 61 6d 65 73 28 29 20 72 6f 75 74 69 6e  prNames() routin
37f6f 65 73 20 6c 6f 6f 6b 73 20 66 6f 72 20 63 6f 6c  es looks for col
37f70 75 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 0a 2a 2a  umn names and.**
37f71 20 73 65 74 73 20 74 68 65 69 72 20 6f 70 63 6f   sets their opco
37f72 64 65 73 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e  des to TK_COLUMN
37f73 20 61 6e 64 20 74 68 65 69 72 20 45 78 70 72 2e   and their Expr.
37f74 69 54 61 62 6c 65 20 66 69 65 6c 64 73 20 74 6f  iTable fields to
37f75 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 63 75 72  .** the VDBE cur
37f76 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  sor number of th
37f77 65 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 72  e table.  This r
37f78 6f 75 74 69 6e 65 20 6a 75 73 74 20 68 61 73 20  outine just has 
37f79 74 6f 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 65 20  to.** translate 
37f7a 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  the cursor numbe
37f7b 72 73 20 69 6e 74 6f 20 62 69 74 6d 61 73 6b 20  rs into bitmask 
37f7c 76 61 6c 75 65 73 20 61 6e 64 20 4f 52 20 61 6c  values and OR al
37f7d 6c 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 73 6b  l.** the bitmask
37f7e 73 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 73  s together..*/.s
37f7f 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78  tatic Bitmask ex
37f80 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65  prListTableUsage
37f81 28 57 68 65 72 65 4d 61 73 6b 53 65 74 2a 2c 20  (WhereMaskSet*, 
37f82 45 78 70 72 4c 69 73 74 2a 29 3b 0a 73 74 61 74  ExprList*);.stat
37f83 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 53  ic Bitmask exprS
37f84 65 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65 28  electTableUsage(
37f85 57 68 65 72 65 4d 61 73 6b 53 65 74 2a 2c 20 53  WhereMaskSet*, S
37f86 65 6c 65 63 74 2a 29 3b 0a 73 74 61 74 69 63 20  elect*);.static 
37f87 42 69 74 6d 61 73 6b 20 65 78 70 72 54 61 62 6c  Bitmask exprTabl
37f88 65 55 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b  eUsage(WhereMask
37f89 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 45  Set *pMaskSet, E
37f8a 78 70 72 20 2a 70 29 7b 0a 20 20 42 69 74 6d 61  xpr *p){.  Bitma
37f8b 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 69  sk mask = 0;.  i
37f8c 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
37f8d 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e 6f 70 3d   0;.  if( p->op=
37f8e 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20  =TK_COLUMN ){.  
37f8f 20 20 6d 61 73 6b 20 3d 20 67 65 74 4d 61 73 6b    mask = getMask
37f90 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 69 54  (pMaskSet, p->iT
37f91 61 62 6c 65 29 3b 0a 20 20 20 20 72 65 74 75 72  able);.    retur
37f92 6e 20 6d 61 73 6b 3b 0a 20 20 7d 0a 20 20 6d 61  n mask;.  }.  ma
37f93 73 6b 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73  sk = exprTableUs
37f94 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d  age(pMaskSet, p-
37f95 3e 70 52 69 67 68 74 29 3b 0a 20 20 6d 61 73 6b  >pRight);.  mask
37f96 20 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61   |= exprTableUsa
37f97 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e  ge(pMaskSet, p->
37f98 70 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 45 78  pLeft);.  if( Ex
37f99 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
37f9a 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
37f9b 7b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78  {.    mask |= ex
37f9c 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61  prSelectTableUsa
37f9d 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e  ge(pMaskSet, p->
37f9e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 7d 65  x.pSelect);.  }e
37f9f 6c 73 65 7b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d  lse{.    mask |=
37fa0 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73   exprListTableUs
37fa1 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d  age(pMaskSet, p-
37fa2 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20 7d 0a 20  >x.pList);.  }. 
37fa3 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a   return mask;.}.
37fa4 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65  static Bitmask e
37fa5 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67  xprListTableUsag
37fa6 65 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a  e(WhereMaskSet *
37fa7 70 4d 61 73 6b 53 65 74 2c 20 45 78 70 72 4c 69  pMaskSet, ExprLi
37fa8 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e  st *pList){.  in
37fa9 74 20 69 3b 0a 20 20 42 69 74 6d 61 73 6b 20 6d  t i;.  Bitmask m
37faa 61 73 6b 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  ask = 0;.  if( p
37fab 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 28  List ){.    for(
37fac 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45  i=0; i<pList->nE
37fad 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
37fae 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62   mask |= exprTab
37faf 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
37fb0 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  , pList->a[i].pE
37fb1 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  xpr);.    }.  }.
37fb2 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d    return mask;.}
37fb3 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20  .static Bitmask 
37fb4 65 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55  exprSelectTableU
37fb5 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b 53 65  sage(WhereMaskSe
37fb6 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 53 65 6c  t *pMaskSet, Sel
37fb7 65 63 74 20 2a 70 53 29 7b 0a 20 20 42 69 74 6d  ect *pS){.  Bitm
37fb8 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20  ask mask = 0;.  
37fb9 77 68 69 6c 65 28 20 70 53 20 29 7b 0a 20 20 20  while( pS ){.   
37fba 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 20 3d   SrcList *pSrc =
37fbb 20 70 53 2d 3e 70 53 72 63 3b 0a 20 20 20 20 6d   pS->pSrc;.    m
37fbc 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54  ask |= exprListT
37fbd 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
37fbe 65 74 2c 20 70 53 2d 3e 70 45 4c 69 73 74 29 3b  et, pS->pEList);
37fbf 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
37fc0 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
37fc1 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 47  pMaskSet, pS->pG
37fc2 72 6f 75 70 42 79 29 3b 0a 20 20 20 20 6d 61 73  roupBy);.    mas
37fc3 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62  k |= exprListTab
37fc4 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
37fc5 2c 20 70 53 2d 3e 70 4f 72 64 65 72 42 79 29 3b  , pS->pOrderBy);
37fc6 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
37fc7 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  rTableUsage(pMas
37fc8 6b 53 65 74 2c 20 70 53 2d 3e 70 57 68 65 72 65  kSet, pS->pWhere
37fc9 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65  );.    mask |= e
37fca 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
37fcb 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 48 61 76  askSet, pS->pHav
37fcc 69 6e 67 29 3b 0a 20 20 20 20 69 66 28 20 41 4c  ing);.    if( AL
37fcd 57 41 59 53 28 70 53 72 63 21 3d 30 29 20 29 7b  WAYS(pSrc!=0) ){
37fce 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
37fcf 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
37fd0 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b  Src->nSrc; i++){
37fd1 0a 20 20 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d  .        mask |=
37fd2 20 65 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65   exprSelectTable
37fd3 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
37fd4 70 53 72 63 2d 3e 61 5b 69 5d 2e 70 53 65 6c 65  pSrc->a[i].pSele
37fd5 63 74 29 3b 0a 20 20 20 20 20 20 20 20 6d 61 73  ct);.        mas
37fd6 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73  k |= exprTableUs
37fd7 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53  age(pMaskSet, pS
37fd8 72 63 2d 3e 61 5b 69 5d 2e 70 4f 6e 29 3b 0a 20  rc->a[i].pOn);. 
37fd9 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
37fda 20 70 53 20 3d 20 70 53 2d 3e 70 50 72 69 6f 72   pS = pS->pPrior
37fdb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d  ;.  }.  return m
37fdc 61 73 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ask;.}../*.** Re
37fdd 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
37fde 20 67 69 76 65 6e 20 6f 70 65 72 61 74 6f 72 20   given operator 
37fdf 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 6f 70  is one of the op
37fe0 65 72 61 74 6f 72 73 20 74 68 61 74 20 69 73 0a  erators that is.
37fe1 2a 2a 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 61  ** allowed for a
37fe2 6e 20 69 6e 64 65 78 61 62 6c 65 20 57 48 45 52  n indexable WHER
37fe3 45 20 63 6c 61 75 73 65 20 74 65 72 6d 2e 20 20  E clause term.  
37fe4 54 68 65 20 61 6c 6c 6f 77 65 64 20 6f 70 65 72  The allowed oper
37fe5 61 74 6f 72 73 20 61 72 65 0a 2a 2a 20 22 3d 22  ators are.** "="
37fe6 2c 20 22 3c 22 2c 20 22 3e 22 2c 20 22 3c 3d 22  , "<", ">", "<="
37fe7 2c 20 22 3e 3d 22 2c 20 61 6e 64 20 22 49 4e 22  , ">=", and "IN"
37fe8 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 4c 45 4d 45 4e  ..**.** IMPLEMEN
37fe9 54 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d 35 39 39  TATION-OF: R-599
37fea 32 36 2d 32 36 33 39 33 20 54 6f 20 62 65 20 75  26-26393 To be u
37feb 73 61 62 6c 65 20 62 79 20 61 6e 20 69 6e 64 65  sable by an inde
37fec 78 20 61 20 74 65 72 6d 20 6d 75 73 74 20 62 65  x a term must be
37fed 0a 2a 2a 20 6f 66 20 6f 6e 65 20 6f 66 20 74 68  .** of one of th
37fee 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d  e following form
37fef 73 3a 20 63 6f 6c 75 6d 6e 20 3d 20 65 78 70 72  s: column = expr
37ff0 65 73 73 69 6f 6e 20 63 6f 6c 75 6d 6e 20 3e 20  ession column > 
37ff1 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 63 6f  expression.** co
37ff2 6c 75 6d 6e 20 3e 3d 20 65 78 70 72 65 73 73 69  lumn >= expressi
37ff3 6f 6e 20 63 6f 6c 75 6d 6e 20 3c 20 65 78 70 72  on column < expr
37ff4 65 73 73 69 6f 6e 20 63 6f 6c 75 6d 6e 20 3c 3d  ession column <=
37ff5 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 65   expression.** e
37ff6 78 70 72 65 73 73 69 6f 6e 20 3d 20 63 6f 6c 75  xpression = colu
37ff7 6d 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 3e 20  mn expression > 
37ff8 63 6f 6c 75 6d 6e 20 65 78 70 72 65 73 73 69 6f  column expressio
37ff9 6e 20 3e 3d 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 65  n >= column.** e
37ffa 78 70 72 65 73 73 69 6f 6e 20 3c 20 63 6f 6c 75  xpression < colu
37ffb 6d 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 3c 3d  mn expression <=
37ffc 20 63 6f 6c 75 6d 6e 20 63 6f 6c 75 6d 6e 20 49   column column I
37ffd 4e 0a 2a 2a 20 28 65 78 70 72 65 73 73 69 6f 6e  N.** (expression
37ffe 2d 6c 69 73 74 29 20 63 6f 6c 75 6d 6e 20 49 4e  -list) column IN
37fff 20 28 73 75 62 71 75 65 72 79 29 20 63 6f 6c 75   (subquery) colu
38000 6d 6e 20 49 53 20 4e 55 4c 4c 0a 2a 2f 0a 73 74  mn IS NULL.*/.st
38001 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 77 65 64  atic int allowed
38002 4f 70 28 69 6e 74 20 6f 70 29 7b 0a 20 20 61 73  Op(int op){.  as
38003 73 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f 45  sert( TK_GT>TK_E
38004 51 20 26 26 20 54 4b 5f 47 54 3c 54 4b 5f 47 45  Q && TK_GT<TK_GE
38005 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b   );.  assert( TK
38006 5f 4c 54 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f  _LT>TK_EQ && TK_
38007 4c 54 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73  LT<TK_GE );.  as
38008 73 65 72 74 28 20 54 4b 5f 4c 45 3e 54 4b 5f 45  sert( TK_LE>TK_E
38009 51 20 26 26 20 54 4b 5f 4c 45 3c 54 4b 5f 47 45  Q && TK_LE<TK_GE
3800a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b   );.  assert( TK
3800b 5f 47 45 3d 3d 54 4b 5f 45 51 2b 34 20 29 3b 0a  _GE==TK_EQ+4 );.
3800c 20 20 72 65 74 75 72 6e 20 6f 70 3d 3d 54 4b 5f    return op==TK_
3800d 49 4e 20 7c 7c 20 28 6f 70 3e 3d 54 4b 5f 45 51  IN || (op>=TK_EQ
3800e 20 26 26 20 6f 70 3c 3d 54 4b 5f 47 45 29 20 7c   && op<=TK_GE) |
3800f 7c 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 3b  | op==TK_ISNULL;
38010 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 20 74  .}../*.** Swap t
38011 77 6f 20 6f 62 6a 65 63 74 73 20 6f 66 20 74 79  wo objects of ty
38012 70 65 20 54 59 50 45 2e 0a 2a 2f 0a 23 64 65 66  pe TYPE..*/.#def
38013 69 6e 65 20 53 57 41 50 28 54 59 50 45 2c 41 2c  ine SWAP(TYPE,A,
38014 42 29 20 7b 54 59 50 45 20 74 3d 41 3b 20 41 3d  B) {TYPE t=A; A=
38015 42 3b 20 42 3d 74 3b 7d 0a 0a 2f 2a 0a 2a 2a 20  B; B=t;}../*.** 
38016 43 6f 6d 6d 75 74 65 20 61 20 63 6f 6d 70 61 72  Commute a compar
38017 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20  ison operator.  
38018 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74  Expressions of t
38019 68 65 20 66 6f 72 6d 20 22 58 20 6f 70 20 59 22  he form "X op Y"
3801a 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65  .** are converte
3801b 64 20 69 6e 74 6f 20 22 59 20 6f 70 20 58 22 2e  d into "Y op X".
3801c 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 63 6f 6c 6c  .**.** If a coll
3801d 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69  ation sequence i
3801e 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
3801f 68 20 65 69 74 68 65 72 20 74 68 65 20 6c 65 66  h either the lef
38020 74 20 6f 72 20 72 69 67 68 74 0a 2a 2a 20 73 69  t or right.** si
38021 64 65 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72  de of the compar
38022 69 73 6f 6e 2c 20 69 74 20 72 65 6d 61 69 6e 73  ison, it remains
38023 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
38024 20 74 68 65 20 73 61 6d 65 20 73 69 64 65 20 61   the same side a
38025 66 74 65 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d  fter.** the comm
38026 75 74 61 74 69 6f 6e 2e 20 53 6f 20 22 59 20 63  utation. So "Y c
38027 6f 6c 6c 61 74 65 20 4e 4f 43 41 53 45 20 6f 70  ollate NOCASE op
38028 20 58 22 20 62 65 63 6f 6d 65 73 20 0a 2a 2a 20   X" becomes .** 
38029 22 58 20 63 6f 6c 6c 61 74 65 20 4e 4f 43 41 53  "X collate NOCAS
3802a 45 20 6f 70 20 59 22 2e 20 54 68 69 73 20 69 73  E op Y". This is
3802b 20 62 65 63 61 75 73 65 20 61 6e 79 20 63 6f 6c   because any col
3802c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
3802d 6f 6e 0a 2a 2a 20 74 68 65 20 6c 65 66 74 20 68  on.** the left h
3802e 61 6e 64 20 73 69 64 65 20 6f 66 20 61 20 63 6f  and side of a co
3802f 6d 70 61 72 69 73 6f 6e 20 6f 76 65 72 72 69 64  mparison overrid
38030 65 73 20 61 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e  es any collation
38031 20 73 65 71 75 65 6e 63 65 20 0a 2a 2a 20 61 74   sequence .** at
38032 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 72 69  tached to the ri
38033 67 68 74 2e 20 46 6f 72 20 74 68 65 20 73 61 6d  ght. For the sam
38034 65 20 72 65 61 73 6f 6e 20 74 68 65 20 45 50 5f  e reason the EP_
38035 45 78 70 43 6f 6c 6c 61 74 65 20 66 6c 61 67 0a  ExpCollate flag.
38036 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6d 6d 75 74  ** is not commut
38037 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
38038 69 64 20 65 78 70 72 43 6f 6d 6d 75 74 65 28 50  id exprCommute(P
38039 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
3803a 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 75 31  pr *pExpr){.  u1
3803b 36 20 65 78 70 52 69 67 68 74 20 3d 20 28 70 45  6 expRight = (pE
3803c 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61  xpr->pRight->fla
3803d 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
3803e 74 65 29 3b 0a 20 20 75 31 36 20 65 78 70 4c 65  te);.  u16 expLe
3803f 66 74 20 3d 20 28 70 45 78 70 72 2d 3e 70 4c 65  ft = (pExpr->pLe
38040 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45  ft->flags & EP_E
38041 78 70 43 6f 6c 6c 61 74 65 29 3b 0a 20 20 61 73  xpCollate);.  as
38042 73 65 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28  sert( allowedOp(
38043 70 45 78 70 72 2d 3e 6f 70 29 20 26 26 20 70 45  pExpr->op) && pE
38044 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 4e 20 29  xpr->op!=TK_IN )
38045 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68  ;.  pExpr->pRigh
38046 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  t->pColl = sqlit
38047 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50  e3ExprCollSeq(pP
38048 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69  arse, pExpr->pRi
38049 67 68 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70  ght);.  pExpr->p
3804a 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71  Left->pColl = sq
3804b 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
3804c 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
3804d 70 4c 65 66 74 29 3b 0a 20 20 53 57 41 50 28 43  pLeft);.  SWAP(C
3804e 6f 6c 6c 53 65 71 2a 2c 70 45 78 70 72 2d 3e 70  ollSeq*,pExpr->p
3804f 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 2c 70 45 78  Right->pColl,pEx
38050 70 72 2d 3e 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c  pr->pLeft->pColl
38051 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67  );.  pExpr->pRig
38052 68 74 2d 3e 66 6c 61 67 73 20 3d 20 28 70 45 78  ht->flags = (pEx
38053 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67  pr->pRight->flag
38054 73 20 26 20 7e 45 50 5f 45 78 70 43 6f 6c 6c 61  s & ~EP_ExpColla
38055 74 65 29 20 7c 20 65 78 70 4c 65 66 74 3b 0a 20  te) | expLeft;. 
38056 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 66   pExpr->pLeft->f
38057 6c 61 67 73 20 3d 20 28 70 45 78 70 72 2d 3e 70  lags = (pExpr->p
38058 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 7e 45  Left->flags & ~E
38059 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 20 7c 20  P_ExpCollate) | 
3805a 65 78 70 52 69 67 68 74 3b 0a 20 20 53 57 41 50  expRight;.  SWAP
3805b 28 45 78 70 72 2a 2c 70 45 78 70 72 2d 3e 70 52  (Expr*,pExpr->pR
3805c 69 67 68 74 2c 70 45 78 70 72 2d 3e 70 4c 65 66  ight,pExpr->pLef
3805d 74 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d  t);.  if( pExpr-
3805e 3e 6f 70 3e 3d 54 4b 5f 47 54 20 29 7b 0a 20 20  >op>=TK_GT ){.  
3805f 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d    assert( TK_LT=
38060 3d 54 4b 5f 47 54 2b 32 20 29 3b 0a 20 20 20 20  =TK_GT+2 );.    
38061 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54  assert( TK_GE==T
38062 4b 5f 4c 45 2b 32 20 29 3b 0a 20 20 20 20 61 73  K_LE+2 );.    as
38063 73 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f 45  sert( TK_GT>TK_E
38064 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Q );.    assert(
38065 20 54 4b 5f 47 54 3c 54 4b 5f 4c 45 20 29 3b 0a   TK_GT<TK_LE );.
38066 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
38067 72 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20 26 26 20  r->op>=TK_GT && 
38068 70 45 78 70 72 2d 3e 6f 70 3c 3d 54 4b 5f 47 45  pExpr->op<=TK_GE
38069 20 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f   );.    pExpr->o
3806a 70 20 3d 20 28 28 70 45 78 70 72 2d 3e 6f 70 2d  p = ((pExpr->op-
3806b 54 4b 5f 47 54 29 5e 32 29 2b 54 4b 5f 47 54 3b  TK_GT)^2)+TK_GT;
3806c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72  .  }.}../*.** Tr
3806d 61 6e 73 6c 61 74 65 20 66 72 6f 6d 20 54 4b 5f  anslate from TK_
3806e 78 78 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 57  xx operator to W
3806f 4f 5f 78 78 20 62 69 74 6d 61 73 6b 2e 0a 2a 2f  O_xx bitmask..*/
38070 0a 73 74 61 74 69 63 20 75 31 36 20 6f 70 65 72  .static u16 oper
38071 61 74 6f 72 4d 61 73 6b 28 69 6e 74 20 6f 70 29  atorMask(int op)
38072 7b 0a 20 20 75 31 36 20 63 3b 0a 20 20 61 73 73  {.  u16 c;.  ass
38073 65 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28 6f  ert( allowedOp(o
38074 70 29 20 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d  p) );.  if( op==
38075 54 4b 5f 49 4e 20 29 7b 0a 20 20 20 20 63 20 3d  TK_IN ){.    c =
38076 20 57 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65 20   WO_IN;.  }else 
38077 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c  if( op==TK_ISNUL
38078 4c 20 29 7b 0a 20 20 20 20 63 20 3d 20 57 4f 5f  L ){.    c = WO_
38079 49 53 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b  ISNULL;.  }else{
3807a 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 57 4f  .    assert( (WO
3807b 5f 45 51 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29 29  _EQ<<(op-TK_EQ))
3807c 20 3c 20 30 78 37 66 66 66 20 29 3b 0a 20 20 20   < 0x7fff );.   
3807d 20 63 20 3d 20 28 75 31 36 29 28 57 4f 5f 45 51   c = (u16)(WO_EQ
3807e 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29 29 3b 0a 20  <<(op-TK_EQ));. 
3807f 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21   }.  assert( op!
38080 3d 54 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 63 3d  =TK_ISNULL || c=
38081 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20  =WO_ISNULL );.  
38082 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 49  assert( op!=TK_I
38083 4e 20 7c 7c 20 63 3d 3d 57 4f 5f 49 4e 20 29 3b  N || c==WO_IN );
38084 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54  .  assert( op!=T
38085 4b 5f 45 51 20 7c 7c 20 63 3d 3d 57 4f 5f 45 51  K_EQ || c==WO_EQ
38086 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70   );.  assert( op
38087 21 3d 54 4b 5f 4c 54 20 7c 7c 20 63 3d 3d 57 4f  !=TK_LT || c==WO
38088 5f 4c 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _LT );.  assert(
38089 20 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 63 3d   op!=TK_LE || c=
3808a 3d 57 4f 5f 4c 45 20 29 3b 0a 20 20 61 73 73 65  =WO_LE );.  asse
3808b 72 74 28 20 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c  rt( op!=TK_GT ||
3808c 20 63 3d 3d 57 4f 5f 47 54 20 29 3b 0a 20 20 61   c==WO_GT );.  a
3808d 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 47 45  ssert( op!=TK_GE
3808e 20 7c 7c 20 63 3d 3d 57 4f 5f 47 45 20 29 3b 0a   || c==WO_GE );.
3808f 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 2f    return c;.}../
38090 2a 0a 2a 2a 20 53 65 61 72 63 68 20 66 6f 72 20  *.** Search for 
38091 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48  a term in the WH
38092 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 20  ERE clause that 
38093 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  is of the form "
38094 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 0a 2a  X <op> <expr>".*
38095 2a 20 77 68 65 72 65 20 58 20 69 73 20 61 20 72  * where X is a r
38096 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
38097 69 43 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65  iColumn of table
38098 20 69 43 75 72 20 61 6e 64 20 3c 6f 70 3e 20 69   iCur and <op> i
38099 73 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20  s one of.** the 
3809a 57 4f 5f 78 78 20 6f 70 65 72 61 74 6f 72 20 63  WO_xx operator c
3809b 6f 64 65 73 20 73 70 65 63 69 66 69 65 64 20 62  odes specified b
3809c 79 20 74 68 65 20 6f 70 20 70 61 72 61 6d 65 74  y the op paramet
3809d 65 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  er..** Return a 
3809e 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 74  pointer to the t
3809f 65 72 6d 2e 20 20 52 65 74 75 72 6e 20 30 20 69  erm.  Return 0 i
380a0 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a  f not found..*/.
380a1 73 74 61 74 69 63 20 57 68 65 72 65 54 65 72 6d  static WhereTerm
380a2 20 2a 66 69 6e 64 54 65 72 6d 28 0a 20 20 57 68   *findTerm(.  Wh
380a3 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
380a4 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
380a5 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 73 65   clause to be se
380a6 61 72 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20  arched */.  int 
380a7 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20  iCur,           
380a8 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62    /* Cursor numb
380a9 65 72 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20 69  er of LHS */.  i
380aa 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20  nt iColumn,     
380ab 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e       /* Column n
380ac 75 6d 62 65 72 20 6f 66 20 4c 48 53 20 2a 2f 0a  umber of LHS */.
380ad 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61    Bitmask notRea
380ae 64 79 2c 20 20 20 20 20 2f 2a 20 52 48 53 20 6d  dy,     /* RHS m
380af 75 73 74 20 6e 6f 74 20 6f 76 65 72 6c 61 70 20  ust not overlap 
380b0 77 69 74 68 20 74 68 69 73 20 6d 61 73 6b 20 2a  with this mask *
380b1 2f 0a 20 20 75 33 32 20 6f 70 2c 20 20 20 20 20  /.  u32 op,     
380b2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73            /* Mas
380b3 6b 20 6f 66 20 57 4f 5f 78 78 20 76 61 6c 75 65  k of WO_xx value
380b4 73 20 64 65 73 63 72 69 62 69 6e 67 20 6f 70 65  s describing ope
380b5 72 61 74 6f 72 20 2a 2f 0a 20 20 49 6e 64 65 78  rator */.  Index
380b6 20 2a 70 49 64 78 20 20 20 20 20 20 20 20 20 20   *pIdx          
380b7 20 2f 2a 20 4d 75 73 74 20 62 65 20 63 6f 6d 70   /* Must be comp
380b8 61 74 69 62 6c 65 20 77 69 74 68 20 74 68 69 73  atible with this
380b9 20 69 6e 64 65 78 2c 20 69 66 20 6e 6f 74 20 4e   index, if not N
380ba 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72  ULL */.){.  Wher
380bb 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20  eTerm *pTerm;.  
380bc 69 6e 74 20 6b 3b 0a 20 20 61 73 73 65 72 74 28  int k;.  assert(
380bd 20 69 43 75 72 3e 3d 30 20 29 3b 0a 20 20 6f 70   iCur>=0 );.  op
380be 20 26 3d 20 57 4f 5f 41 4c 4c 3b 0a 20 20 66 6f   &= WO_ALL;.  fo
380bf 72 28 3b 20 70 57 43 3b 20 70 57 43 3d 70 57 43  r(; pWC; pWC=pWC
380c0 2d 3e 70 4f 75 74 65 72 29 7b 0a 20 20 20 20 66  ->pOuter){.    f
380c1 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c  or(pTerm=pWC->a,
380c2 20 6b 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6b   k=pWC->nTerm; k
380c3 3b 20 6b 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b  ; k--, pTerm++){
380c4 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d  .      if( pTerm
380c5 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43  ->leftCursor==iC
380c6 75 72 0a 20 20 20 20 20 20 20 20 20 26 26 20 28  ur.         && (
380c7 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67  pTerm->prereqRig
380c8 68 74 20 26 20 6e 6f 74 52 65 61 64 79 29 3d 3d  ht & notReady)==
380c9 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 54  0.         && pT
380ca 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d  erm->u.leftColum
380cb 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20  n==iColumn.     
380cc 20 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e 65      && (pTerm->e
380cd 4f 70 65 72 61 74 6f 72 20 26 20 6f 70 29 21 3d  Operator & op)!=
380ce 30 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20  0.      ){.     
380cf 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3e 3d     if( iColumn>=
380d0 30 20 26 26 20 70 49 64 78 20 26 26 20 70 54 65  0 && pIdx && pTe
380d1 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21 3d 57  rm->eOperator!=W
380d2 4f 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  O_ISNULL ){.    
380d3 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 20 3d        Expr *pX =
380d4 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20   pTerm->pExpr;. 
380d5 20 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71           CollSeq
380d6 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20   *pColl;.       
380d7 20 20 20 63 68 61 72 20 69 64 78 61 66 66 3b 0a     char idxaff;.
380d8 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b            int j;
380d9 0a 20 20 20 20 20 20 20 20 20 20 50 61 72 73 65  .          Parse
380da 20 2a 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e   *pParse = pWC->
380db 70 50 61 72 73 65 3b 0a 20 20 0a 20 20 20 20 20  pParse;.  .     
380dc 20 20 20 20 20 69 64 78 61 66 66 20 3d 20 70 49       idxaff = pI
380dd 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 61 43 6f 6c  dx->pTable->aCol
380de 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69  [iColumn].affini
380df 74 79 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ty;.          if
380e0 28 20 21 73 71 6c 69 74 65 33 49 6e 64 65 78 41  ( !sqlite3IndexA
380e1 66 66 69 6e 69 74 79 4f 6b 28 70 58 2c 20 69 64  ffinityOk(pX, id
380e2 78 61 66 66 29 20 29 20 63 6f 6e 74 69 6e 75 65  xaff) ) continue
380e3 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 20 20 2f  ;.  .          /
380e4 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65  * Figure out the
380e5 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
380e6 6e 63 65 20 72 65 71 75 69 72 65 64 20 66 72 6f  nce required fro
380e7 6d 20 61 6e 20 69 6e 64 65 78 20 66 6f 72 0a 20  m an index for. 
380e8 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 74           ** it t
380e9 6f 20 62 65 20 75 73 65 66 75 6c 20 66 6f 72 20  o be useful for 
380ea 6f 70 74 69 6d 69 73 69 6e 67 20 65 78 70 72 65  optimising expre
380eb 73 73 69 6f 6e 20 70 58 2e 20 53 74 6f 72 65 20  ssion pX. Store 
380ec 74 68 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a  this.          *
380ed 2a 20 76 61 6c 75 65 20 69 6e 20 76 61 72 69 61  * value in varia
380ee 62 6c 65 20 70 43 6f 6c 6c 2e 0a 20 20 20 20 20  ble pColl..     
380ef 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
380f0 20 20 61 73 73 65 72 74 28 70 58 2d 3e 70 4c 65    assert(pX->pLe
380f1 66 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  ft);.          p
380f2 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 42 69  Coll = sqlite3Bi
380f3 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53  naryCompareCollS
380f4 65 71 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70  eq(pParse, pX->p
380f5 4c 65 66 74 2c 20 70 58 2d 3e 70 52 69 67 68 74  Left, pX->pRight
380f6 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
380f7 65 72 74 28 70 43 6f 6c 6c 20 7c 7c 20 70 50 61  ert(pColl || pPa
380f8 72 73 65 2d 3e 6e 45 72 72 29 3b 0a 20 20 0a 20  rse->nErr);.  . 
380f9 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30           for(j=0
380fa 3b 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ; pIdx->aiColumn
380fb 5b 6a 5d 21 3d 69 43 6f 6c 75 6d 6e 3b 20 6a 2b  [j]!=iColumn; j+
380fc 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +){.            
380fd 69 66 28 20 4e 45 56 45 52 28 6a 3e 3d 70 49 64  if( NEVER(j>=pId
380fe 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 29 20 72 65  x->nColumn) ) re
380ff 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20  turn 0;.        
38100 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66    }.          if
38101 28 20 70 43 6f 6c 6c 20 26 26 20 73 71 6c 69 74  ( pColl && sqlit
38102 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 6c 2d  e3StrICmp(pColl-
38103 3e 7a 4e 61 6d 65 2c 20 70 49 64 78 2d 3e 61 7a  >zName, pIdx->az
38104 43 6f 6c 6c 5b 6a 5d 29 20 29 20 63 6f 6e 74 69  Coll[j]) ) conti
38105 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  nue;.        }. 
38106 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 54         return pT
38107 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  erm;.      }.   
38108 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
38109 30 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  0;.}../* Forward
3810a 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74   reference */.st
3810b 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e  atic void exprAn
3810c 61 6c 79 7a 65 28 53 72 63 4c 69 73 74 2a 2c 20  alyze(SrcList*, 
3810d 57 68 65 72 65 43 6c 61 75 73 65 2a 2c 20 69 6e  WhereClause*, in
3810e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  t);../*.** Call 
3810f 65 78 70 72 41 6e 61 6c 79 7a 65 20 6f 6e 20 61  exprAnalyze on a
38110 6c 6c 20 74 65 72 6d 73 20 69 6e 20 61 20 57 48  ll terms in a WH
38111 45 52 45 20 63 6c 61 75 73 65 2e 20 20 0a 2a 2a  ERE clause.  .**
38112 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  .**.*/.static vo
38113 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c  id exprAnalyzeAl
38114 6c 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  l(.  SrcList *pT
38115 61 62 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a  abList,       /*
38116 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
38117 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73   */.  WhereClaus
38118 65 20 2a 70 57 43 20 20 20 20 20 20 20 20 20 2f  e *pWC         /
38119 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  * the WHERE clau
3811a 73 65 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  se to be analyze
3811b 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  d */.){.  int i;
3811c 0a 20 20 66 6f 72 28 69 3d 70 57 43 2d 3e 6e 54  .  for(i=pWC->nT
3811d 65 72 6d 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d  erm-1; i>=0; i--
3811e 29 7b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79  ){.    exprAnaly
3811f 7a 65 28 70 54 61 62 4c 69 73 74 2c 20 70 57 43  ze(pTabList, pWC
38120 2c 20 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66  , i);.  }.}..#if
38121 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
38122 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49  _LIKE_OPTIMIZATI
38123 4f 4e 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  ON./*.** Check t
38124 6f 20 73 65 65 20 69 66 20 74 68 65 20 67 69 76  o see if the giv
38125 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  en expression is
38126 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20   a LIKE or GLOB 
38127 6f 70 65 72 61 74 6f 72 20 74 68 61 74 0a 2a 2a  operator that.**
38128 20 63 61 6e 20 62 65 20 6f 70 74 69 6d 69 7a 65   can be optimize
38129 64 20 75 73 69 6e 67 20 69 6e 65 71 75 61 6c 69  d using inequali
3812a 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20  ty constraints. 
3812b 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
3812c 69 74 20 69 73 0a 2a 2a 20 73 6f 20 61 6e 64 20  it is.** so and 
3812d 66 61 6c 73 65 20 69 66 20 6e 6f 74 2e 0a 2a 2a  false if not..**
3812e 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 66 6f 72  .** In order for
3812f 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f   the operator to
38130 20 62 65 20 6f 70 74 69 6d 69 7a 69 62 6c 65 2c   be optimizible,
38131 20 74 68 65 20 52 48 53 20 6d 75 73 74 20 62 65   the RHS must be
38132 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 69 74   a string.** lit
38133 65 72 61 6c 20 74 68 61 74 20 64 6f 65 73 20 6e  eral that does n
38134 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20  ot begin with a 
38135 77 69 6c 64 63 61 72 64 2e 20 20 0a 2a 2f 0a 73  wildcard.  .*/.s
38136 74 61 74 69 63 20 69 6e 74 20 69 73 4c 69 6b 65  tatic int isLike
38137 4f 72 47 6c 6f 62 28 0a 20 20 50 61 72 73 65 20  OrGlob(.  Parse 
38138 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50  *pParse,    /* P
38139 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20  arsing and code 
3813a 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65  generating conte
3813b 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  xt */.  Expr *pE
3813c 78 70 72 2c 20 20 20 20 20 20 2f 2a 20 54 65 73  xpr,      /* Tes
3813d 74 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f  t this expressio
3813e 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 2a 70 70  n */.  Expr **pp
3813f 50 72 65 66 69 78 2c 20 20 2f 2a 20 50 6f 69 6e  Prefix,  /* Poin
38140 74 65 72 20 74 6f 20 54 4b 5f 53 54 52 49 4e 47  ter to TK_STRING
38141 20 65 78 70 72 65 73 73 69 6f 6e 20 77 69 74 68   expression with
38142 20 70 61 74 74 65 72 6e 20 70 72 65 66 69 78 20   pattern prefix 
38143 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 73 43 6f 6d  */.  int *pisCom
38144 70 6c 65 74 65 2c 20 2f 2a 20 54 72 75 65 20 69  plete, /* True i
38145 66 20 74 68 65 20 6f 6e 6c 79 20 77 69 6c 64 63  f the only wildc
38146 61 72 64 20 69 73 20 25 20 69 6e 20 74 68 65 20  ard is % in the 
38147 6c 61 73 74 20 63 68 61 72 61 63 74 65 72 20 2a  last character *
38148 2f 0a 20 20 69 6e 74 20 2a 70 6e 6f 43 61 73 65  /.  int *pnoCase
38149 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
3814a 20 75 70 70 65 72 63 61 73 65 20 69 73 20 65 71   uppercase is eq
3814b 75 69 76 61 6c 65 6e 74 20 74 6f 20 6c 6f 77 65  uivalent to lowe
3814c 72 63 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f  rcase */.){.  co
3814d 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b  nst char *z = 0;
3814e 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69           /* Stri
3814f 6e 67 20 6f 6e 20 52 48 53 20 6f 66 20 4c 49 4b  ng on RHS of LIK
38150 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  E operator */.  
38151 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 2a 70  Expr *pRight, *p
38152 4c 65 66 74 3b 20 20 20 20 20 20 2f 2a 20 52 69  Left;      /* Ri
38153 67 68 74 20 61 6e 64 20 6c 65 66 74 20 73 69 7a  ght and left siz
38154 65 20 6f 66 20 4c 49 4b 45 20 6f 70 65 72 61 74  e of LIKE operat
38155 6f 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  or */.  ExprList
38156 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 20 20 20   *pList;        
38157 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f 70     /* List of op
38158 65 72 61 6e 64 73 20 74 6f 20 74 68 65 20 4c 49  erands to the LI
38159 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  KE operator */. 
3815a 20 69 6e 74 20 63 3b 20 20 20 20 20 20 20 20 20   int c;         
3815b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
3815c 6e 65 20 63 68 61 72 61 63 74 65 72 20 69 6e 20  ne character in 
3815d 7a 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74  z[] */.  int cnt
3815e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3815f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
38160 20 6e 6f 6e 2d 77 69 6c 64 63 61 72 64 20 70 72   non-wildcard pr
38161 65 66 69 78 20 63 68 61 72 61 63 74 65 72 73 20  efix characters 
38162 2a 2f 0a 20 20 63 68 61 72 20 77 63 5b 33 5d 3b  */.  char wc[3];
38163 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38164 2f 2a 20 57 69 6c 64 63 61 72 64 20 63 68 61 72  /* Wildcard char
38165 61 63 74 65 72 73 20 2a 2f 0a 20 20 73 71 6c 69  acters */.  sqli
38166 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
38167 2d 3e 64 62 3b 20 20 2f 2a 20 44 61 74 61 62 61  ->db;  /* Databa
38168 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
38169 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
3816a 20 2a 70 56 61 6c 20 3d 20 30 3b 0a 20 20 69 6e   *pVal = 0;.  in
3816b 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  t op;           
3816c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f           /* Opco
3816d 64 65 20 6f 66 20 70 52 69 67 68 74 20 2a 2f 0a  de of pRight */.
3816e 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49  .  if( !sqlite3I
3816f 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 64 62  sLikeFunction(db
38170 2c 20 70 45 78 70 72 2c 20 70 6e 6f 43 61 73 65  , pExpr, pnoCase
38171 2c 20 77 63 29 20 29 7b 0a 20 20 20 20 72 65 74  , wc) ){.    ret
38172 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 69 66 64 65  urn 0;.  }.#ifde
38173 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a  f SQLITE_EBCDIC.
38174 20 20 69 66 28 20 2a 70 6e 6f 43 61 73 65 20 29    if( *pnoCase )
38175 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69   return 0;.#endi
38176 66 0a 20 20 70 4c 69 73 74 20 3d 20 70 45 78 70  f.  pList = pExp
38177 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 70 4c  r->x.pList;.  pL
38178 65 66 74 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 31  eft = pList->a[1
38179 5d 2e 70 45 78 70 72 3b 0a 20 20 69 66 28 20 70  ].pExpr;.  if( p
3817a 4c 65 66 74 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c  Left->op!=TK_COL
3817b 55 4d 4e 20 0a 20 20 20 7c 7c 20 73 71 6c 69 74  UMN .   || sqlit
3817c 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70  e3ExprAffinity(p
3817d 4c 65 66 74 29 21 3d 53 51 4c 49 54 45 5f 41 46  Left)!=SQLITE_AF
3817e 46 5f 54 45 58 54 20 0a 20 20 20 7c 7c 20 49 73  F_TEXT .   || Is
3817f 56 69 72 74 75 61 6c 28 70 4c 65 66 74 2d 3e 70  Virtual(pLeft->p
38180 54 61 62 29 0a 20 20 29 7b 0a 20 20 20 20 2f 2a  Tab).  ){.    /*
38181 20 49 4d 50 3a 20 52 2d 30 32 30 36 35 2d 34 39   IMP: R-02065-49
38182 34 36 35 20 54 68 65 20 6c 65 66 74 2d 68 61 6e  465 The left-han
38183 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 4c 49  d side of the LI
38184 4b 45 20 6f 72 20 47 4c 4f 42 20 6f 70 65 72 61  KE or GLOB opera
38185 74 6f 72 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20  tor must.    ** 
38186 62 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  be the name of a
38187 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e  n indexed column
38188 20 77 69 74 68 20 54 45 58 54 20 61 66 66 69 6e   with TEXT affin
38189 69 74 79 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  ity. */.    retu
3818a 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65  rn 0;.  }.  asse
3818b 72 74 28 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75  rt( pLeft->iColu
3818c 6d 6e 21 3d 28 2d 31 29 20 29 3b 20 2f 2a 20 42  mn!=(-1) ); /* B
3818d 65 63 61 75 73 65 20 49 50 4b 20 6e 65 76 65 72  ecause IPK never
3818e 20 68 61 73 20 41 46 46 5f 54 45 58 54 20 2a 2f   has AFF_TEXT */
3818f 0a 0a 20 20 70 52 69 67 68 74 20 3d 20 70 4c 69  ..  pRight = pLi
38190 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a  st->a[0].pExpr;.
38191 20 20 6f 70 20 3d 20 70 52 69 67 68 74 2d 3e 6f    op = pRight->o
38192 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  p;.  if( op==TK_
38193 52 45 47 49 53 54 45 52 20 29 7b 0a 20 20 20 20  REGISTER ){.    
38194 6f 70 20 3d 20 70 52 69 67 68 74 2d 3e 6f 70 32  op = pRight->op2
38195 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6f 70 3d 3d  ;.  }.  if( op==
38196 54 4b 5f 56 41 52 49 41 42 4c 45 20 29 7b 0a 20  TK_VARIABLE ){. 
38197 20 20 20 56 64 62 65 20 2a 70 52 65 70 72 65 70     Vdbe *pReprep
38198 61 72 65 20 3d 20 70 50 61 72 73 65 2d 3e 70 52  are = pParse->pR
38199 65 70 72 65 70 61 72 65 3b 0a 20 20 20 20 69 6e  eprepare;.    in
3819a 74 20 69 43 6f 6c 20 3d 20 70 52 69 67 68 74 2d  t iCol = pRight-
3819b 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 56  >iColumn;.    pV
3819c 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  al = sqlite3Vdbe
3819d 47 65 74 56 61 6c 75 65 28 70 52 65 70 72 65 70  GetValue(pReprep
3819e 61 72 65 2c 20 69 43 6f 6c 2c 20 53 51 4c 49 54  are, iCol, SQLIT
3819f 45 5f 41 46 46 5f 4e 4f 4e 45 29 3b 0a 20 20 20  E_AFF_NONE);.   
381a0 20 69 66 28 20 70 56 61 6c 20 26 26 20 73 71 6c   if( pVal && sql
381a1 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
381a2 70 56 61 6c 29 3d 3d 53 51 4c 49 54 45 5f 54 45  pVal)==SQLITE_TE
381a3 58 54 20 29 7b 0a 20 20 20 20 20 20 7a 20 3d 20  XT ){.      z = 
381a4 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
381a5 76 61 6c 75 65 5f 74 65 78 74 28 70 56 61 6c 29  value_text(pVal)
381a6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
381a7 74 65 33 56 64 62 65 53 65 74 56 61 72 6d 61 73  te3VdbeSetVarmas
381a8 6b 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  k(pParse->pVdbe,
381a9 20 69 43 6f 6c 29 3b 0a 20 20 20 20 61 73 73 65   iCol);.    asse
381aa 72 74 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d  rt( pRight->op==
381ab 54 4b 5f 56 41 52 49 41 42 4c 45 20 7c 7c 20 70  TK_VARIABLE || p
381ac 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 52 45  Right->op==TK_RE
381ad 47 49 53 54 45 52 20 29 3b 0a 20 20 7d 65 6c 73  GISTER );.  }els
381ae 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 54 52  e if( op==TK_STR
381af 49 4e 47 20 29 7b 0a 20 20 20 20 7a 20 3d 20 70  ING ){.    z = p
381b0 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b  Right->u.zToken;
381b1 0a 20 20 7d 0a 20 20 69 66 28 20 7a 20 29 7b 0a  .  }.  if( z ){.
381b2 20 20 20 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20      cnt = 0;.   
381b3 20 77 68 69 6c 65 28 20 28 63 3d 7a 5b 63 6e 74   while( (c=z[cnt
381b4 5d 29 21 3d 30 20 26 26 20 63 21 3d 77 63 5b 30  ])!=0 && c!=wc[0
381b5 5d 20 26 26 20 63 21 3d 77 63 5b 31 5d 20 26 26  ] && c!=wc[1] &&
381b6 20 63 21 3d 77 63 5b 32 5d 20 29 7b 0a 20 20 20   c!=wc[2] ){.   
381b7 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a     cnt++;.    }.
381b8 20 20 20 20 69 66 28 20 63 6e 74 21 3d 30 20 26      if( cnt!=0 &
381b9 26 20 32 35 35 21 3d 28 75 38 29 7a 5b 63 6e 74  & 255!=(u8)z[cnt
381ba 2d 31 5d 20 29 7b 0a 20 20 20 20 20 20 45 78 70  -1] ){.      Exp
381bb 72 20 2a 70 50 72 65 66 69 78 3b 0a 20 20 20 20  r *pPrefix;.    
381bc 20 20 2a 70 69 73 43 6f 6d 70 6c 65 74 65 20 3d    *pisComplete =
381bd 20 63 3d 3d 77 63 5b 30 5d 20 26 26 20 7a 5b 63   c==wc[0] && z[c
381be 6e 74 2b 31 5d 3d 3d 30 3b 0a 20 20 20 20 20 20  nt+1]==0;.      
381bf 70 50 72 65 66 69 78 20 3d 20 73 71 6c 69 74 65  pPrefix = sqlite
381c0 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 53 54 52  3Expr(db, TK_STR
381c1 49 4e 47 2c 20 7a 29 3b 0a 20 20 20 20 20 20 69  ING, z);.      i
381c2 66 28 20 70 50 72 65 66 69 78 20 29 20 70 50 72  f( pPrefix ) pPr
381c3 65 66 69 78 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 63  efix->u.zToken[c
381c4 6e 74 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 2a  nt] = 0;.      *
381c5 70 70 50 72 65 66 69 78 20 3d 20 70 50 72 65 66  ppPrefix = pPref
381c6 69 78 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70  ix;.      if( op
381c7 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45 20 29 7b  ==TK_VARIABLE ){
381c8 0a 20 20 20 20 20 20 20 20 56 64 62 65 20 2a 76  .        Vdbe *v
381c9 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
381ca 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
381cb 33 56 64 62 65 53 65 74 56 61 72 6d 61 73 6b 28  3VdbeSetVarmask(
381cc 76 2c 20 70 52 69 67 68 74 2d 3e 69 43 6f 6c 75  v, pRight->iColu
381cd 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  mn);.        if(
381ce 20 2a 70 69 73 43 6f 6d 70 6c 65 74 65 20 26 26   *pisComplete &&
381cf 20 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65   pRight->u.zToke
381d0 6e 5b 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20  n[1] ){.        
381d1 20 20 2f 2a 20 49 66 20 74 68 65 20 72 68 73 20    /* If the rhs 
381d2 6f 66 20 74 68 65 20 4c 49 4b 45 20 65 78 70 72  of the LIKE expr
381d3 65 73 73 69 6f 6e 20 69 73 20 61 20 76 61 72 69  ession is a vari
381d4 61 62 6c 65 2c 20 61 6e 64 20 74 68 65 20 63 75  able, and the cu
381d5 72 72 65 6e 74 0a 20 20 20 20 20 20 20 20 20 20  rrent.          
381d6 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ** value of the 
381d7 76 61 72 69 61 62 6c 65 20 6d 65 61 6e 73 20 74  variable means t
381d8 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
381d9 74 6f 20 69 6e 76 6f 6b 65 20 74 68 65 20 4c 49  to invoke the LI
381da 4b 45 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  KE.          ** 
381db 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 6e  function, then n
381dc 6f 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 77 69  o OP_Variable wi
381dd 6c 6c 20 62 65 20 61 64 64 65 64 20 74 6f 20 74  ll be added to t
381de 68 65 20 70 72 6f 67 72 61 6d 2e 0a 20 20 20 20  he program..    
381df 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 63 61        ** This ca
381e0 75 73 65 73 20 70 72 6f 62 6c 65 6d 73 20 66 6f  uses problems fo
381e1 72 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69  r the sqlite3_bi
381e2 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
381e3 65 28 29 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  e().          **
381e4 20 41 50 49 2e 20 54 6f 20 77 6f 72 6b 61 72 6f   API. To workaro
381e5 75 6e 64 20 74 68 65 6d 2c 20 61 64 64 20 61 20  und them, add a 
381e6 64 75 6d 6d 79 20 4f 50 5f 56 61 72 69 61 62 6c  dummy OP_Variabl
381e7 65 20 68 65 72 65 2e 0a 20 20 20 20 20 20 20 20  e here..        
381e8 20 20 2a 2f 20 0a 20 20 20 20 20 20 20 20 20 20    */ .          
381e9 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33  int r1 = sqlite3
381ea 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
381eb 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  e);.          sq
381ec 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72  lite3ExprCodeTar
381ed 67 65 74 28 70 50 61 72 73 65 2c 20 70 52 69 67  get(pParse, pRig
381ee 68 74 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20  ht, r1);.       
381ef 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
381f0 61 6e 67 65 50 33 28 76 2c 20 73 71 6c 69 74 65  angeP3(v, sqlite
381f1 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
381f2 28 76 29 2d 31 2c 20 30 29 3b 0a 20 20 20 20 20  (v)-1, 0);.     
381f3 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
381f4 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
381f5 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20  e, r1);.        
381f6 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  }.      }.    }e
381f7 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20 30  lse{.      z = 0
381f8 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73  ;.    }.  }..  s
381f9 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
381fa 70 56 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20  pVal);.  return 
381fb 28 7a 21 3d 30 29 3b 0a 7d 0a 23 65 6e 64 69 66  (z!=0);.}.#endif
381fc 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
381fd 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  LIKE_OPTIMIZATIO
381fe 4e 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53  N */...#ifndef S
381ff 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
38200 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 43 68  ALTABLE./*.** Ch
38201 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
38202 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69  e given expressi
38203 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  on is of the for
38204 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  m.**.**         
38205 63 6f 6c 75 6d 6e 20 4d 41 54 43 48 20 65 78 70  column MATCH exp
38206 72 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73  r.**.** If it is
38207 20 74 68 65 6e 20 72 65 74 75 72 6e 20 54 52 55   then return TRU
38208 45 2e 20 20 49 66 20 6e 6f 74 2c 20 72 65 74 75  E.  If not, retu
38209 72 6e 20 46 41 4c 53 45 2e 0a 2a 2f 0a 73 74 61  rn FALSE..*/.sta
3820a 74 69 63 20 69 6e 74 20 69 73 4d 61 74 63 68 4f  tic int isMatchO
3820b 66 43 6f 6c 75 6d 6e 28 0a 20 20 45 78 70 72 20  fColumn(.  Expr 
3820c 2a 70 45 78 70 72 20 20 20 20 20 20 2f 2a 20 54  *pExpr      /* T
3820d 65 73 74 20 74 68 69 73 20 65 78 70 72 65 73 73  est this express
3820e 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72  ion */.){.  Expr
3820f 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a 0a 20 20  List *pList;..  
38210 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  if( pExpr->op!=T
38211 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 7b 0a 20 20  K_FUNCTION ){.  
38212 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
38213 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
38214 49 43 6d 70 28 70 45 78 70 72 2d 3e 75 2e 7a 54  ICmp(pExpr->u.zT
38215 6f 6b 65 6e 2c 22 6d 61 74 63 68 22 29 21 3d 30  oken,"match")!=0
38216 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
38217 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 20 3d 20  ;.  }.  pList = 
38218 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a  pExpr->x.pList;.
38219 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78    if( pList->nEx
3821a 70 72 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74  pr!=2 ){.    ret
3821b 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 0;.  }.  if(
3821c 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78   pList->a[1].pEx
3821d 70 72 2d 3e 6f 70 20 21 3d 20 54 4b 5f 43 4f 4c  pr->op != TK_COL
3821e 55 4d 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72  UMN ){.    retur
3821f 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 0;.  }.  retur
38220 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  n 1;.}.#endif /*
38221 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
38222 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a  TUALTABLE */../*
38223 0a 2a 2a 20 49 66 20 74 68 65 20 70 42 61 73 65  .** If the pBase
38224 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 69 67   expression orig
38225 69 6e 61 74 65 64 20 69 6e 20 74 68 65 20 4f 4e  inated in the ON
38226 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65   or USING clause
38227 20 6f 66 0a 2a 2a 20 61 20 6a 6f 69 6e 2c 20 74   of.** a join, t
38228 68 65 6e 20 74 72 61 6e 73 66 65 72 20 74 68 65  hen transfer the
38229 20 61 70 70 72 6f 70 72 69 61 74 65 20 6d 61 72   appropriate mar
3822a 6b 69 6e 67 73 20 6f 76 65 72 20 74 6f 20 64 65  kings over to de
3822b 72 69 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  rived..*/.static
3822c 20 76 6f 69 64 20 74 72 61 6e 73 66 65 72 4a 6f   void transferJo
3822d 69 6e 4d 61 72 6b 69 6e 67 73 28 45 78 70 72 20  inMarkings(Expr 
3822e 2a 70 44 65 72 69 76 65 64 2c 20 45 78 70 72 20  *pDerived, Expr 
3822f 2a 70 42 61 73 65 29 7b 0a 20 20 70 44 65 72 69  *pBase){.  pDeri
38230 76 65 64 2d 3e 66 6c 61 67 73 20 7c 3d 20 70 42  ved->flags |= pB
38231 61 73 65 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  ase->flags & EP_
38232 46 72 6f 6d 4a 6f 69 6e 3b 0a 20 20 70 44 65 72  FromJoin;.  pDer
38233 69 76 65 64 2d 3e 69 52 69 67 68 74 4a 6f 69 6e  ived->iRightJoin
38234 54 61 62 6c 65 20 3d 20 70 42 61 73 65 2d 3e 69  Table = pBase->i
38235 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b 0a  RightJoinTable;.
38236 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  }..#if !defined(
38237 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f  SQLITE_OMIT_OR_O
38238 50 54 49 4d 49 5a 41 54 49 4f 4e 29 20 26 26 20  PTIMIZATION) && 
38239 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
3823a 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 2f  OMIT_SUBQUERY)./
3823b 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 61 20 74  *.** Analyze a t
3823c 65 72 6d 20 74 68 61 74 20 63 6f 6e 73 69 73 74  erm that consist
3823d 73 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65  s of two or more
3823e 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 64 0a 2a 2a   OR-connected.**
3823f 20 73 75 62 74 65 72 6d 73 2e 20 20 53 6f 20 69   subterms.  So i
38240 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2e 2e 2e  n:.**.**     ...
38241 20 57 48 45 52 45 20 20 28 61 3d 35 29 20 41 4e   WHERE  (a=5) AN
38242 44 20 28 62 3d 37 20 4f 52 20 63 3d 39 20 4f 52  D (b=7 OR c=9 OR
38243 20 64 3d 31 33 29 20 41 4e 44 20 28 64 3d 31 33   d=13) AND (d=13
38244 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ).**            
38245 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5e 5e                ^^
38246 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e  ^^^^^^^^^^^^^^^^
38247 5e 5e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  ^^.**.** This ro
38248 75 74 69 6e 65 20 61 6e 61 6c 79 7a 65 73 20 74  utine analyzes t
38249 65 72 6d 73 20 73 75 63 68 20 61 73 20 74 68 65  erms such as the
3824a 20 6d 69 64 64 6c 65 20 74 65 72 6d 20 69 6e 20   middle term in 
3824b 74 68 65 20 61 62 6f 76 65 20 65 78 61 6d 70 6c  the above exampl
3824c 65 2e 0a 2a 2a 20 41 20 57 68 65 72 65 4f 72 54  e..** A WhereOrT
3824d 65 72 6d 20 6f 62 6a 65 63 74 20 69 73 20 63 6f  erm object is co
3824e 6d 70 75 74 65 64 20 61 6e 64 20 61 74 74 61 63  mputed and attac
3824f 68 65 64 20 74 6f 20 74 68 65 20 74 65 72 6d 20  hed to the term 
38250 75 6e 64 65 72 0a 2a 2a 20 61 6e 61 6c 79 73 69  under.** analysi
38251 73 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  s, regardless of
38252 20 74 68 65 20 6f 75 74 63 6f 6d 65 20 6f 66 20   the outcome of 
38253 74 68 65 20 61 6e 61 6c 79 73 69 73 2e 20 20 48  the analysis.  H
38254 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ence:.**.**     
38255 57 68 65 72 65 54 65 72 6d 2e 77 74 46 6c 61 67  WhereTerm.wtFlag
38256 73 20 20 20 7c 3d 20 20 54 45 52 4d 5f 4f 52 49  s   |=  TERM_ORI
38257 4e 46 4f 0a 2a 2a 20 20 20 20 20 57 68 65 72 65  NFO.**     Where
38258 54 65 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 20  Term.u.pOrInfo  
38259 3d 20 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79  =  a dynamically
3825a 20 61 6c 6c 6f 63 61 74 65 64 20 57 68 65 72 65   allocated Where
3825b 4f 72 54 65 72 6d 20 6f 62 6a 65 63 74 0a 2a 2a  OrTerm object.**
3825c 0a 2a 2a 20 54 68 65 20 74 65 72 6d 20 62 65 69  .** The term bei
3825d 6e 67 20 61 6e 61 6c 79 7a 65 64 20 6d 75 73 74  ng analyzed must
3825e 20 68 61 76 65 20 74 77 6f 20 6f 72 20 6d 6f 72   have two or mor
3825f 65 20 6f 66 20 4f 52 2d 63 6f 6e 6e 65 63 74 65  e of OR-connecte
38260 64 20 73 75 62 74 65 72 6d 73 2e 0a 2a 2a 20 41  d subterms..** A
38261 20 73 69 6e 67 6c 65 20 73 75 62 74 65 72 6d 20   single subterm 
38262 6d 69 67 68 74 20 62 65 20 61 20 73 65 74 20 6f  might be a set o
38263 66 20 41 4e 44 2d 63 6f 6e 6e 65 63 74 65 64 20  f AND-connected 
38264 73 75 62 2d 73 75 62 74 65 72 6d 73 2e 0a 2a 2a  sub-subterms..**
38265 20 45 78 61 6d 70 6c 65 73 20 6f 66 20 74 65 72   Examples of ter
38266 6d 73 20 75 6e 64 65 72 20 61 6e 61 6c 79 73 69  ms under analysi
38267 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 41 29  s:.**.**     (A)
38268 20 20 20 20 20 74 31 2e 78 3d 74 32 2e 79 20 4f       t1.x=t2.y O
38269 52 20 74 31 2e 78 3d 74 32 2e 7a 20 4f 52 20 74  R t1.x=t2.z OR t
3826a 31 2e 79 3d 31 35 20 4f 52 20 74 31 2e 7a 3d 74  1.y=15 OR t1.z=t
3826b 33 2e 61 2b 35 0a 2a 2a 20 20 20 20 20 28 42 29  3.a+5.**     (B)
3826c 20 20 20 20 20 78 3d 65 78 70 72 31 20 4f 52 20       x=expr1 OR 
3826d 65 78 70 72 32 3d 78 20 4f 52 20 78 3d 65 78 70  expr2=x OR x=exp
3826e 72 33 0a 2a 2a 20 20 20 20 20 28 43 29 20 20 20  r3.**     (C)   
3826f 20 20 74 31 2e 78 3d 74 32 2e 79 20 4f 52 20 28    t1.x=t2.y OR (
38270 74 31 2e 78 3d 74 32 2e 7a 20 41 4e 44 20 74 31  t1.x=t2.z AND t1
38271 2e 79 3d 31 35 29 0a 2a 2a 20 20 20 20 20 28 44  .y=15).**     (D
38272 29 20 20 20 20 20 78 3d 65 78 70 72 31 20 4f 52  )     x=expr1 OR
38273 20 28 79 3e 31 31 20 41 4e 44 20 79 3c 32 32 20   (y>11 AND y<22 
38274 41 4e 44 20 7a 20 4c 49 4b 45 20 27 2a 68 65 6c  AND z LIKE '*hel
38275 6c 6f 2a 27 29 0a 2a 2a 20 20 20 20 20 28 45 29  lo*').**     (E)
38276 20 20 20 20 20 28 70 2e 61 3d 31 20 41 4e 44 20       (p.a=1 AND 
38277 71 2e 62 3d 32 20 41 4e 44 20 72 2e 63 3d 33 29  q.b=2 AND r.c=3)
38278 20 4f 52 20 28 70 2e 78 3d 34 20 41 4e 44 20 71   OR (p.x=4 AND q
38279 2e 79 3d 35 20 41 4e 44 20 72 2e 7a 3d 36 29 0a  .y=5 AND r.z=6).
3827a 2a 2a 0a 2a 2a 20 43 41 53 45 20 31 3a 0a 2a 2a  **.** CASE 1:.**
3827b 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 75 62 74 65  .** If all subte
3827c 72 6d 73 20 61 72 65 20 6f 66 20 74 68 65 20 66  rms are of the f
3827d 6f 72 6d 20 54 2e 43 3d 65 78 70 72 20 66 6f 72  orm T.C=expr for
3827e 20 73 6f 6d 65 20 73 69 6e 67 6c 65 20 63 6f 6c   some single col
3827f 75 6d 6e 20 6f 66 20 43 0a 2a 2a 20 61 20 73 69  umn of C.** a si
38280 6e 67 6c 65 20 74 61 62 6c 65 20 54 20 28 61 73  ngle table T (as
38281 20 73 68 6f 77 6e 20 69 6e 20 65 78 61 6d 70 6c   shown in exampl
38282 65 20 42 20 61 62 6f 76 65 29 20 74 68 65 6e 20  e B above) then 
38283 63 72 65 61 74 65 20 61 20 6e 65 77 20 76 69 72  create a new vir
38284 74 75 61 6c 0a 2a 2a 20 74 65 72 6d 20 74 68 61  tual.** term tha
38285 74 20 69 73 20 61 6e 20 65 71 75 69 76 61 6c 65  t is an equivale
38286 6e 74 20 49 4e 20 65 78 70 72 65 73 73 69 6f 6e  nt IN expression
38287 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
38288 73 2c 20 69 66 20 74 68 65 20 74 65 72 6d 0a 2a  s, if the term.*
38289 2a 20 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64  * being analyzed
3828a 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20   is:.**.**      
3828b 78 20 3d 20 65 78 70 72 31 20 20 4f 52 20 20 65  x = expr1  OR  e
3828c 78 70 72 32 20 3d 20 78 20 20 4f 52 20 20 78 20  xpr2 = x  OR  x 
3828d 3d 20 65 78 70 72 33 0a 2a 2a 0a 2a 2a 20 74 68  = expr3.**.** th
3828e 65 6e 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  en create a new 
3828f 76 69 72 74 75 61 6c 20 74 65 72 6d 20 6c 69 6b  virtual term lik
38290 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
38291 20 20 20 78 20 49 4e 20 28 65 78 70 72 31 2c 65     x IN (expr1,e
38292 78 70 72 32 2c 65 78 70 72 33 29 0a 2a 2a 0a 2a  xpr2,expr3).**.*
38293 2a 20 43 41 53 45 20 32 3a 0a 2a 2a 0a 2a 2a 20  * CASE 2:.**.** 
38294 49 66 20 61 6c 6c 20 73 75 62 74 65 72 6d 73 20  If all subterms 
38295 61 72 65 20 69 6e 64 65 78 61 62 6c 65 20 62 79  are indexable by
38296 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20   a single table 
38297 54 2c 20 74 68 65 6e 20 73 65 74 0a 2a 2a 0a 2a  T, then set.**.*
38298 2a 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e  *     WhereTerm.
38299 65 4f 70 65 72 61 74 6f 72 20 20 20 20 20 20 20  eOperator       
3829a 20 20 20 20 20 20 20 3d 20 20 57 4f 5f 4f 52 0a         =  WO_OR.
3829b 2a 2a 20 20 20 20 20 57 68 65 72 65 54 65 72 6d  **     WhereTerm
3829c 2e 75 2e 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65  .u.pOrInfo->inde
3829d 78 61 62 6c 65 20 20 7c 3d 20 20 74 68 65 20 63  xable  |=  the c
3829e 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72  ursor number for
3829f 20 74 61 62 6c 65 20 54 0a 2a 2a 0a 2a 2a 20 41   table T.**.** A
382a0 20 73 75 62 74 65 72 6d 20 69 73 20 22 69 6e 64   subterm is "ind
382a1 65 78 61 62 6c 65 22 20 69 66 20 69 74 20 69 73  exable" if it is
382a2 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 20   of the form.** 
382a3 22 54 2e 43 20 3c 6f 70 3e 20 3c 65 78 70 72 3e  "T.C <op> <expr>
382a4 22 20 77 68 65 72 65 20 43 20 69 73 20 61 6e 79  " where C is any
382a5 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65   column of table
382a6 20 54 20 61 6e 64 20 0a 2a 2a 20 3c 6f 70 3e 20   T and .** <op> 
382a7 69 73 20 6f 6e 65 20 6f 66 20 22 3d 22 2c 20 22  is one of "=", "
382a8 3c 22 2c 20 22 3c 3d 22 2c 20 22 3e 22 2c 20 22  <", "<=", ">", "
382a9 3e 3d 22 2c 20 22 49 53 20 4e 55 4c 4c 22 2c 20  >=", "IS NULL", 
382aa 6f 72 20 22 49 4e 22 2e 0a 2a 2a 20 41 20 73 75  or "IN"..** A su
382ab 62 74 65 72 6d 20 69 73 20 61 6c 73 6f 20 69 6e  bterm is also in
382ac 64 65 78 61 62 6c 65 20 69 66 20 69 74 20 69 73  dexable if it is
382ad 20 61 6e 20 41 4e 44 20 6f 66 20 74 77 6f 20 6f   an AND of two o
382ae 72 20 6d 6f 72 65 0a 2a 2a 20 73 75 62 73 75 62  r more.** subsub
382af 74 65 72 6d 73 20 61 74 20 6c 65 61 73 74 20 6f  terms at least o
382b0 6e 65 20 6f 66 20 77 68 69 63 68 20 69 73 20 69  ne of which is i
382b1 6e 64 65 78 61 62 6c 65 2e 20 20 49 6e 64 65 78  ndexable.  Index
382b2 61 62 6c 65 20 41 4e 44 20 0a 2a 2a 20 73 75 62  able AND .** sub
382b3 74 65 72 6d 73 20 68 61 76 65 20 74 68 65 69 72  terms have their
382b4 20 65 4f 70 65 72 61 74 6f 72 20 73 65 74 20 74   eOperator set t
382b5 6f 20 57 4f 5f 41 4e 44 20 61 6e 64 20 74 68 65  o WO_AND and the
382b6 79 20 68 61 76 65 0a 2a 2a 20 75 2e 70 41 6e 64  y have.** u.pAnd
382b7 49 6e 66 6f 20 73 65 74 20 74 6f 20 61 20 64 79  Info set to a dy
382b8 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
382b9 74 65 64 20 57 68 65 72 65 41 6e 64 54 65 72 6d  ted WhereAndTerm
382ba 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 46   object..**.** F
382bb 72 6f 6d 20 61 6e 6f 74 68 65 72 20 70 6f 69 6e  rom another poin
382bc 74 20 6f 66 20 76 69 65 77 2c 20 22 69 6e 64 65  t of view, "inde
382bd 78 61 62 6c 65 22 20 6d 65 61 6e 73 20 74 68 61  xable" means tha
382be 74 20 74 68 65 20 73 75 62 74 65 72 6d 20 63 6f  t the subterm co
382bf 75 6c 64 0a 2a 2a 20 70 6f 74 65 6e 74 69 61 6c  uld.** potential
382c0 6c 79 20 62 65 20 75 73 65 64 20 77 69 74 68 20  ly be used with 
382c1 61 6e 20 69 6e 64 65 78 20 69 66 20 61 6e 20 61  an index if an a
382c2 70 70 72 6f 70 72 69 61 74 65 20 69 6e 64 65 78  ppropriate index
382c3 20 65 78 69 73 74 73 2e 0a 2a 2a 20 54 68 69 73   exists..** This
382c4 20 61 6e 61 6c 79 73 69 73 20 64 6f 65 73 20 6e   analysis does n
382c5 6f 74 20 63 6f 6e 73 69 64 65 72 20 77 68 65 74  ot consider whet
382c6 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69  her or not the i
382c7 6e 64 65 78 20 65 78 69 73 74 73 3b 20 74 68 61  ndex exists; tha
382c8 74 0a 2a 2a 20 69 73 20 73 6f 6d 65 74 68 69 6e  t.** is somethin
382c9 67 20 74 68 65 20 62 65 73 74 49 6e 64 65 78 28  g the bestIndex(
382ca 29 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64  ) routine will d
382cb 65 74 65 72 6d 69 6e 65 2e 20 20 54 68 69 73 20  etermine.  This 
382cc 61 6e 61 6c 79 73 69 73 0a 2a 2a 20 6f 6e 6c 79  analysis.** only
382cd 20 6c 6f 6f 6b 73 20 61 74 20 77 68 65 74 68 65   looks at whethe
382ce 72 20 73 75 62 74 65 72 6d 73 20 61 70 70 72 6f  r subterms appro
382cf 70 72 69 61 74 65 20 66 6f 72 20 69 6e 64 65 78  priate for index
382d0 69 6e 67 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a  ing exist..**.**
382d1 20 41 6c 6c 20 65 78 61 6d 70 6c 65 73 20 41 20   All examples A 
382d2 74 68 72 6f 75 67 68 20 45 20 61 62 6f 76 65 20  through E above 
382d3 61 6c 6c 20 73 61 74 69 73 66 79 20 63 61 73 65  all satisfy case
382d4 20 32 2e 20 20 42 75 74 20 69 66 20 61 20 74 65   2.  But if a te
382d5 72 6d 0a 2a 2a 20 61 6c 73 6f 20 73 74 61 74 69  rm.** also stati
382d6 73 66 69 65 73 20 63 61 73 65 20 31 20 28 73 75  sfies case 1 (su
382d7 63 68 20 61 73 20 42 29 20 77 65 20 6b 6e 6f 77  ch as B) we know
382d8 20 74 68 61 74 20 74 68 65 20 6f 70 74 69 6d 69   that the optimi
382d9 7a 65 72 20 77 69 6c 6c 0a 2a 2a 20 61 6c 77 61  zer will.** alwa
382da 79 73 20 70 72 65 66 65 72 20 63 61 73 65 20 31  ys prefer case 1
382db 2c 20 73 6f 20 69 6e 20 74 68 61 74 20 63 61 73  , so in that cas
382dc 65 20 77 65 20 70 72 65 74 65 6e 64 20 74 68 61  e we pretend tha
382dd 74 20 63 61 73 65 20 32 20 69 73 20 6e 6f 74 0a  t case 2 is not.
382de 2a 2a 20 73 61 74 69 73 66 69 65 64 2e 0a 2a 2a  ** satisfied..**
382df 0a 2a 2a 20 49 74 20 6d 69 67 68 74 20 62 65 20  .** It might be 
382e0 74 68 65 20 63 61 73 65 20 74 68 61 74 20 6d 75  the case that mu
382e1 6c 74 69 70 6c 65 20 74 61 62 6c 65 73 20 61 72  ltiple tables ar
382e2 65 20 69 6e 64 65 78 61 62 6c 65 2e 20 20 46 6f  e indexable.  Fo
382e3 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 28 45  r example,.** (E
382e4 29 20 61 62 6f 76 65 20 69 73 20 69 6e 64 65 78  ) above is index
382e5 61 62 6c 65 20 6f 6e 20 74 61 62 6c 65 73 20 50  able on tables P
382e6 2c 20 51 2c 20 61 6e 64 20 52 2e 0a 2a 2a 0a 2a  , Q, and R..**.*
382e7 2a 20 54 65 72 6d 73 20 74 68 61 74 20 73 61 74  * Terms that sat
382e8 69 73 66 79 20 63 61 73 65 20 32 20 61 72 65 20  isfy case 2 are 
382e9 63 61 6e 64 69 64 61 74 65 73 20 66 6f 72 20 6c  candidates for l
382ea 6f 6f 6b 75 70 20 62 79 20 75 73 69 6e 67 0a 2a  ookup by using.*
382eb 2a 20 73 65 70 61 72 61 74 65 20 69 6e 64 69 63  * separate indic
382ec 65 73 20 74 6f 20 66 69 6e 64 20 72 6f 77 69 64  es to find rowid
382ed 73 20 66 6f 72 20 65 61 63 68 20 73 75 62 74 65  s for each subte
382ee 72 6d 20 61 6e 64 20 63 6f 6d 70 6f 73 69 6e 67  rm and composing
382ef 0a 2a 2a 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66  .** the union of
382f0 20 61 6c 6c 20 72 6f 77 69 64 73 20 75 73 69 6e   all rowids usin
382f1 67 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63  g a RowSet objec
382f2 74 2e 20 20 54 68 69 73 20 69 73 20 73 69 6d 69  t.  This is simi
382f3 6c 61 72 0a 2a 2a 20 74 6f 20 22 62 69 74 6d 61  lar.** to "bitma
382f4 70 20 69 6e 64 69 63 65 73 22 20 69 6e 20 6f 74  p indices" in ot
382f5 68 65 72 20 64 61 74 61 62 61 73 65 20 65 6e 67  her database eng
382f6 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 54 48 45  ines..**.** OTHE
382f7 52 57 49 53 45 3a 0a 2a 2a 0a 2a 2a 20 49 66 20  RWISE:.**.** If 
382f8 6e 65 69 74 68 65 72 20 63 61 73 65 20 31 20 6e  neither case 1 n
382f9 6f 72 20 63 61 73 65 20 32 20 61 70 70 6c 79 2c  or case 2 apply,
382fa 20 74 68 65 6e 20 6c 65 61 76 65 20 74 68 65 20   then leave the 
382fb 65 4f 70 65 72 61 74 6f 72 20 73 65 74 20 74 6f  eOperator set to
382fc 0a 2a 2a 20 7a 65 72 6f 2e 20 20 54 68 69 73 20  .** zero.  This 
382fd 74 65 72 6d 20 69 73 20 6e 6f 74 20 75 73 65 66  term is not usef
382fe 75 6c 20 66 6f 72 20 73 65 61 72 63 68 2e 0a 2a  ul for search..*
382ff 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78  /.static void ex
38300 70 72 41 6e 61 6c 79 7a 65 4f 72 54 65 72 6d 28  prAnalyzeOrTerm(
38301 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
38302 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
38303 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
38304 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65  */.  WhereClause
38305 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 2f   *pWC,         /
38306 2a 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 57  * the complete W
38307 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
38308 20 69 6e 74 20 69 64 78 54 65 72 6d 20 20 20 20   int idxTerm    
38309 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
3830a 64 65 78 20 6f 66 20 74 68 65 20 4f 52 2d 74 65  dex of the OR-te
3830b 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  rm to be analyze
3830c 64 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20  d */.){.  Parse 
3830d 2a 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e 70  *pParse = pWC->p
3830e 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 20  Parse;          
3830f 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
38310 65 78 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ext */.  sqlite3
38311 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
38312 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
38313 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
38314 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 57 68 65  nection */.  Whe
38315 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 20 3d 20  reTerm *pTerm = 
38316 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d  &pWC->a[idxTerm]
38317 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 65 72 6d  ;    /* The term
38318 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20   to be analyzed 
38319 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
3831a 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b   = pTerm->pExpr;
3831b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3831c 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6f  The expression o
3831d 66 20 74 68 65 20 74 65 72 6d 20 2a 2f 0a 20 20  f the term */.  
3831e 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d  WhereMaskSet *pM
3831f 61 73 6b 53 65 74 20 3d 20 70 57 43 2d 3e 70 4d  askSet = pWC->pM
38320 61 73 6b 53 65 74 3b 20 2f 2a 20 54 61 62 6c 65  askSet; /* Table
38321 20 75 73 65 20 6d 61 73 6b 73 20 2a 2f 0a 20 20   use masks */.  
38322 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
38323 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38324 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
38325 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 57 68  counters */.  Wh
38326 65 72 65 43 6c 61 75 73 65 20 2a 70 4f 72 57 63  ereClause *pOrWc
38327 3b 20 20 20 20 20 20 20 2f 2a 20 42 72 65 61 6b  ;       /* Break
38328 75 70 20 6f 66 20 70 54 65 72 6d 20 69 6e 74 6f  up of pTerm into
38329 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20 57   subterms */.  W
3832a 68 65 72 65 54 65 72 6d 20 2a 70 4f 72 54 65 72  hereTerm *pOrTer
3832b 6d 3b 20 20 20 20 20 20 20 2f 2a 20 41 20 53 75  m;       /* A Su
3832c 62 2d 74 65 72 6d 20 77 69 74 68 69 6e 20 74 68  b-term within th
3832d 65 20 70 4f 72 57 63 20 2a 2f 0a 20 20 57 68 65  e pOrWc */.  Whe
3832e 72 65 4f 72 49 6e 66 6f 20 2a 70 4f 72 49 6e 66  reOrInfo *pOrInf
3832f 6f 3b 20 20 20 20 20 2f 2a 20 41 64 64 69 74 69  o;     /* Additi
38330 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
38331 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
38332 20 70 54 65 72 6d 20 2a 2f 0a 20 20 42 69 74 6d   pTerm */.  Bitm
38333 61 73 6b 20 63 68 6e 67 54 6f 49 4e 3b 20 20 20  ask chngToIN;   
38334 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 73 20        /* Tables 
38335 74 68 61 74 20 6d 69 67 68 74 20 73 61 74 69 73  that might satis
38336 66 79 20 63 61 73 65 20 31 20 2a 2f 0a 20 20 42  fy case 1 */.  B
38337 69 74 6d 61 73 6b 20 69 6e 64 65 78 61 62 6c 65  itmask indexable
38338 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c  ;        /* Tabl
38339 65 73 20 74 68 61 74 20 61 72 65 20 69 6e 64 65  es that are inde
3833a 78 61 62 6c 65 2c 20 73 61 74 69 73 66 79 69 6e  xable, satisfyin
3833b 67 20 63 61 73 65 20 32 20 2a 2f 0a 0a 20 20 2f  g case 2 */..  /
3833c 2a 0a 20 20 2a 2a 20 42 72 65 61 6b 20 74 68 65  *.  ** Break the
3833d 20 4f 52 20 63 6c 61 75 73 65 20 69 6e 74 6f 20   OR clause into 
3833e 69 74 73 20 73 65 70 61 72 61 74 65 20 73 75 62  its separate sub
3833f 74 65 72 6d 73 2e 20 20 54 68 65 20 73 75 62 74  terms.  The subt
38340 65 72 6d 73 20 61 72 65 0a 20 20 2a 2a 20 73 74  erms are.  ** st
38341 6f 72 65 64 20 69 6e 20 61 20 57 68 65 72 65 43  ored in a WhereC
38342 6c 61 75 73 65 20 73 74 72 75 63 74 75 72 65 20  lause structure 
38343 63 6f 6e 74 61 69 6e 69 6e 67 20 77 69 74 68 69  containing withi
38344 6e 20 74 68 65 20 57 68 65 72 65 4f 72 49 6e 66  n the WhereOrInf
38345 6f 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 20 74 68  o.  ** object th
38346 61 74 20 69 73 20 61 74 74 61 63 68 65 64 20 74  at is attached t
38347 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 4f  o the original O
38348 52 20 63 6c 61 75 73 65 20 74 65 72 6d 2e 0a 20  R clause term.. 
38349 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70   */.  assert( (p
3834a 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
3834b 28 54 45 52 4d 5f 44 59 4e 41 4d 49 43 7c 54 45  (TERM_DYNAMIC|TE
3834c 52 4d 5f 4f 52 49 4e 46 4f 7c 54 45 52 4d 5f 41  RM_ORINFO|TERM_A
3834d 4e 44 49 4e 46 4f 29 29 3d 3d 30 20 29 3b 0a 20  NDINFO))==0 );. 
3834e 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
3834f 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 70  op==TK_OR );.  p
38350 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 20  Term->u.pOrInfo 
38351 3d 20 70 4f 72 49 6e 66 6f 20 3d 20 73 71 6c 69  = pOrInfo = sqli
38352 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
38353 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4f 72 49  db, sizeof(*pOrI
38354 6e 66 6f 29 29 3b 0a 20 20 69 66 28 20 70 4f 72  nfo));.  if( pOr
38355 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e  Info==0 ) return
38356 3b 0a 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61  ;.  pTerm->wtFla
38357 67 73 20 7c 3d 20 54 45 52 4d 5f 4f 52 49 4e 46  gs |= TERM_ORINF
38358 4f 3b 0a 20 20 70 4f 72 57 63 20 3d 20 26 70 4f  O;.  pOrWc = &pO
38359 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 77 68 65  rInfo->wc;.  whe
3835a 72 65 43 6c 61 75 73 65 49 6e 69 74 28 70 4f 72  reClauseInit(pOr
3835b 57 63 2c 20 70 57 43 2d 3e 70 50 61 72 73 65 2c  Wc, pWC->pParse,
3835c 20 70 4d 61 73 6b 53 65 74 2c 20 70 57 43 2d 3e   pMaskSet, pWC->
3835d 77 63 74 72 6c 46 6c 61 67 73 29 3b 0a 20 20 77  wctrlFlags);.  w
3835e 68 65 72 65 53 70 6c 69 74 28 70 4f 72 57 63 2c  hereSplit(pOrWc,
3835f 20 70 45 78 70 72 2c 20 54 4b 5f 4f 52 29 3b 0a   pExpr, TK_OR);.
38360 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c    exprAnalyzeAll
38361 28 70 53 72 63 2c 20 70 4f 72 57 63 29 3b 0a 20  (pSrc, pOrWc);. 
38362 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
38363 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
38364 20 20 61 73 73 65 72 74 28 20 70 4f 72 57 63 2d    assert( pOrWc-
38365 3e 6e 54 65 72 6d 3e 3d 32 20 29 3b 0a 0a 20 20  >nTerm>=2 );..  
38366 2f 2a 0a 20 20 2a 2a 20 43 6f 6d 70 75 74 65 20  /*.  ** Compute 
38367 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c 65  the set of table
38368 73 20 74 68 61 74 20 6d 69 67 68 74 20 73 61 74  s that might sat
38369 69 73 66 79 20 63 61 73 65 73 20 31 20 6f 72 20  isfy cases 1 or 
3836a 32 2e 0a 20 20 2a 2f 0a 20 20 69 6e 64 65 78 61  2..  */.  indexa
3836b 62 6c 65 20 3d 20 7e 28 42 69 74 6d 61 73 6b 29  ble = ~(Bitmask)
3836c 30 3b 0a 20 20 63 68 6e 67 54 6f 49 4e 20 3d 20  0;.  chngToIN = 
3836d 7e 28 70 57 43 2d 3e 76 6d 61 73 6b 29 3b 0a 20  ~(pWC->vmask);. 
3836e 20 66 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54   for(i=pOrWc->nT
3836f 65 72 6d 2d 31 2c 20 70 4f 72 54 65 72 6d 3d 70  erm-1, pOrTerm=p
38370 4f 72 57 63 2d 3e 61 3b 20 69 3e 3d 30 20 26 26  OrWc->a; i>=0 &&
38371 20 69 6e 64 65 78 61 62 6c 65 3b 20 69 2d 2d 2c   indexable; i--,
38372 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20   pOrTerm++){.   
38373 20 69 66 28 20 28 70 4f 72 54 65 72 6d 2d 3e 65   if( (pOrTerm->e
38374 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 53 49  Operator & WO_SI
38375 4e 47 4c 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20  NGLE)==0 ){.    
38376 20 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 2a    WhereAndInfo *
38377 70 41 6e 64 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pAndInfo;.      
38378 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d  assert( pOrTerm-
38379 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 30 20 29 3b  >eOperator==0 );
3837a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
3837b 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73  pOrTerm->wtFlags
3837c 20 26 20 28 54 45 52 4d 5f 41 4e 44 49 4e 46 4f   & (TERM_ANDINFO
3837d 7c 54 45 52 4d 5f 4f 52 49 4e 46 4f 29 29 3d 3d  |TERM_ORINFO))==
3837e 30 20 29 3b 0a 20 20 20 20 20 20 63 68 6e 67 54  0 );.      chngT
3837f 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  oIN = 0;.      p
38380 41 6e 64 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65  AndInfo = sqlite
38381 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
38382 20 73 69 7a 65 6f 66 28 2a 70 41 6e 64 49 6e 66   sizeof(*pAndInf
38383 6f 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  o));.      if( p
38384 41 6e 64 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20  AndInfo ){.     
38385 20 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a     WhereClause *
38386 70 41 6e 64 57 43 3b 0a 20 20 20 20 20 20 20 20  pAndWC;.        
38387 57 68 65 72 65 54 65 72 6d 20 2a 70 41 6e 64 54  WhereTerm *pAndT
38388 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  erm;.        int
38389 20 6a 3b 0a 20 20 20 20 20 20 20 20 42 69 74 6d   j;.        Bitm
3838a 61 73 6b 20 62 20 3d 20 30 3b 0a 20 20 20 20 20  ask b = 0;.     
3838b 20 20 20 70 4f 72 54 65 72 6d 2d 3e 75 2e 70 41     pOrTerm->u.pA
3838c 6e 64 49 6e 66 6f 20 3d 20 70 41 6e 64 49 6e 66  ndInfo = pAndInf
3838d 6f 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 54 65  o;.        pOrTe
3838e 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54  rm->wtFlags |= T
3838f 45 52 4d 5f 41 4e 44 49 4e 46 4f 3b 0a 20 20 20  ERM_ANDINFO;.   
38390 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e 65 4f       pOrTerm->eO
38391 70 65 72 61 74 6f 72 20 3d 20 57 4f 5f 41 4e 44  perator = WO_AND
38392 3b 0a 20 20 20 20 20 20 20 20 70 41 6e 64 57 43  ;.        pAndWC
38393 20 3d 20 26 70 41 6e 64 49 6e 66 6f 2d 3e 77 63   = &pAndInfo->wc
38394 3b 0a 20 20 20 20 20 20 20 20 77 68 65 72 65 43  ;.        whereC
38395 6c 61 75 73 65 49 6e 69 74 28 70 41 6e 64 57 43  lauseInit(pAndWC
38396 2c 20 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70  , pWC->pParse, p
38397 4d 61 73 6b 53 65 74 2c 20 70 57 43 2d 3e 77 63  MaskSet, pWC->wc
38398 74 72 6c 46 6c 61 67 73 29 3b 0a 20 20 20 20 20  trlFlags);.     
38399 20 20 20 77 68 65 72 65 53 70 6c 69 74 28 70 41     whereSplit(pA
3839a 6e 64 57 43 2c 20 70 4f 72 54 65 72 6d 2d 3e 70  ndWC, pOrTerm->p
3839b 45 78 70 72 2c 20 54 4b 5f 41 4e 44 29 3b 0a 20  Expr, TK_AND);. 
3839c 20 20 20 20 20 20 20 65 78 70 72 41 6e 61 6c 79         exprAnaly
3839d 7a 65 41 6c 6c 28 70 53 72 63 2c 20 70 41 6e 64  zeAll(pSrc, pAnd
3839e 57 43 29 3b 0a 20 20 20 20 20 20 20 20 70 41 6e  WC);.        pAn
3839f 64 57 43 2d 3e 70 4f 75 74 65 72 20 3d 20 70 57  dWC->pOuter = pW
383a0 43 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  C;.        testc
383a1 61 73 65 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ase( db->mallocF
383a2 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 20  ailed );.       
383a3 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
383a4 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
383a5 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 41 6e      for(j=0, pAn
383a6 64 54 65 72 6d 3d 70 41 6e 64 57 43 2d 3e 61 3b  dTerm=pAndWC->a;
383a7 20 6a 3c 70 41 6e 64 57 43 2d 3e 6e 54 65 72 6d   j<pAndWC->nTerm
383a8 3b 20 6a 2b 2b 2c 20 70 41 6e 64 54 65 72 6d 2b  ; j++, pAndTerm+
383a9 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +){.            
383aa 61 73 73 65 72 74 28 20 70 41 6e 64 54 65 72 6d  assert( pAndTerm
383ab 2d 3e 70 45 78 70 72 20 29 3b 0a 20 20 20 20 20  ->pExpr );.     
383ac 20 20 20 20 20 20 20 69 66 28 20 61 6c 6c 6f 77         if( allow
383ad 65 64 4f 70 28 70 41 6e 64 54 65 72 6d 2d 3e 70  edOp(pAndTerm->p
383ae 45 78 70 72 2d 3e 6f 70 29 20 29 7b 0a 20 20 20  Expr->op) ){.   
383af 20 20 20 20 20 20 20 20 20 20 20 62 20 7c 3d 20             b |= 
383b0 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74  getMask(pMaskSet
383b1 2c 20 70 41 6e 64 54 65 72 6d 2d 3e 6c 65 66 74  , pAndTerm->left
383b2 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 20  Cursor);.       
383b3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
383b4 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
383b5 20 20 20 20 20 69 6e 64 65 78 61 62 6c 65 20 26       indexable &
383b6 3d 20 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = b;.      }.   
383b7 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 72 54 65   }else if( pOrTe
383b8 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
383b9 52 4d 5f 43 4f 50 49 45 44 20 29 7b 0a 20 20 20  RM_COPIED ){.   
383ba 20 20 20 2f 2a 20 53 6b 69 70 20 74 68 69 73 20     /* Skip this 
383bb 74 65 72 6d 20 66 6f 72 20 6e 6f 77 2e 20 20 57  term for now.  W
383bc 65 20 72 65 76 69 73 69 74 20 69 74 20 77 68 65  e revisit it whe
383bd 6e 20 77 65 20 70 72 6f 63 65 73 73 20 74 68 65  n we process the
383be 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 65 73  .      ** corres
383bf 70 6f 6e 64 69 6e 67 20 54 45 52 4d 5f 56 49 52  ponding TERM_VIR
383c0 54 55 41 4c 20 74 65 72 6d 20 2a 2f 0a 20 20 20  TUAL term */.   
383c1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 42 69   }else{.      Bi
383c2 74 6d 61 73 6b 20 62 3b 0a 20 20 20 20 20 20 62  tmask b;.      b
383c3 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b   = getMask(pMask
383c4 53 65 74 2c 20 70 4f 72 54 65 72 6d 2d 3e 6c 65  Set, pOrTerm->le
383c5 66 74 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  ftCursor);.     
383c6 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74   if( pOrTerm->wt
383c7 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52  Flags & TERM_VIR
383c8 54 55 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20  TUAL ){.        
383c9 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 74 68 65  WhereTerm *pOthe
383ca 72 20 3d 20 26 70 4f 72 57 63 2d 3e 61 5b 70 4f  r = &pOrWc->a[pO
383cb 72 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 5d 3b  rTerm->iParent];
383cc 0a 20 20 20 20 20 20 20 20 62 20 7c 3d 20 67 65  .        b |= ge
383cd 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20  tMask(pMaskSet, 
383ce 70 4f 74 68 65 72 2d 3e 6c 65 66 74 43 75 72 73  pOther->leftCurs
383cf 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  or);.      }.   
383d0 20 20 20 69 6e 64 65 78 61 62 6c 65 20 26 3d 20     indexable &= 
383d1 62 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72  b;.      if( pOr
383d2 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21  Term->eOperator!
383d3 3d 57 4f 5f 45 51 20 29 7b 0a 20 20 20 20 20 20  =WO_EQ ){.      
383d4 20 20 63 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a    chngToIN = 0;.
383d5 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
383d6 20 20 20 20 20 63 68 6e 67 54 6f 49 4e 20 26 3d       chngToIN &=
383d7 20 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   b;.      }.    
383d8 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  }.  }..  /*.  **
383d9 20 52 65 63 6f 72 64 20 74 68 65 20 73 65 74 20   Record the set 
383da 6f 66 20 74 61 62 6c 65 73 20 74 68 61 74 20 73  of tables that s
383db 61 74 69 73 66 79 20 63 61 73 65 20 32 2e 20 20  atisfy case 2.  
383dc 54 68 65 20 73 65 74 20 6d 69 67 68 74 20 62 65  The set might be
383dd 0a 20 20 2a 2a 20 65 6d 70 74 79 2e 0a 20 20 2a  .  ** empty..  *
383de 2f 0a 20 20 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64  /.  pOrInfo->ind
383df 65 78 61 62 6c 65 20 3d 20 69 6e 64 65 78 61 62  exable = indexab
383e0 6c 65 3b 0a 20 20 70 54 65 72 6d 2d 3e 65 4f 70  le;.  pTerm->eOp
383e1 65 72 61 74 6f 72 20 3d 20 69 6e 64 65 78 61 62  erator = indexab
383e2 6c 65 3d 3d 30 20 3f 20 30 20 3a 20 57 4f 5f 4f  le==0 ? 0 : WO_O
383e3 52 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63 68  R;..  /*.  ** ch
383e4 6e 67 54 6f 49 4e 20 68 6f 6c 64 73 20 61 20 73  ngToIN holds a s
383e5 65 74 20 6f 66 20 74 61 62 6c 65 73 20 74 68 61  et of tables tha
383e6 74 20 2a 6d 69 67 68 74 2a 20 73 61 74 69 73 66  t *might* satisf
383e7 79 20 63 61 73 65 20 31 2e 20 20 42 75 74 0a 20  y case 1.  But. 
383e8 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 64   ** we have to d
383e9 6f 20 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61  o some additiona
383ea 6c 20 63 68 65 63 6b 69 6e 67 20 74 6f 20 73 65  l checking to se
383eb 65 20 69 66 20 63 61 73 65 20 31 20 72 65 61 6c  e if case 1 real
383ec 6c 79 0a 20 20 2a 2a 20 69 73 20 73 61 74 69 73  ly.  ** is satis
383ed 66 69 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  fied..  **.  ** 
383ee 63 68 6e 67 54 6f 49 4e 20 77 69 6c 6c 20 68 6f  chngToIN will ho
383ef 6c 64 20 65 69 74 68 65 72 20 30 2c 20 31 2c 20  ld either 0, 1, 
383f0 6f 72 20 32 20 62 69 74 73 2e 20 20 54 68 65 20  or 2 bits.  The 
383f1 30 2d 62 69 74 20 63 61 73 65 20 6d 65 61 6e 73  0-bit case means
383f2 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 72 65  .  ** that there
383f3 20 69 73 20 6e 6f 20 70 6f 73 73 69 62 69 6c 69   is no possibili
383f4 74 79 20 6f 66 20 74 72 61 6e 73 66 6f 72 6d 69  ty of transformi
383f5 6e 67 20 74 68 65 20 4f 52 20 63 6c 61 75 73 65  ng the OR clause
383f6 20 69 6e 74 6f 20 61 6e 0a 20 20 2a 2a 20 49 4e   into an.  ** IN
383f7 20 6f 70 65 72 61 74 6f 72 20 62 65 63 61 75 73   operator becaus
383f8 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 65  e one or more te
383f9 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 20 63 6c  rms in the OR cl
383fa 61 75 73 65 20 63 6f 6e 74 61 69 6e 0a 20 20 2a  ause contain.  *
383fb 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65  * something othe
383fc 72 20 74 68 61 6e 20 3d 3d 20 6f 6e 20 61 20 63  r than == on a c
383fd 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 73 69 6e  olumn in the sin
383fe 67 6c 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20  gle table.  The 
383ff 31 2d 62 69 74 0a 20 20 2a 2a 20 63 61 73 65 20  1-bit.  ** case 
38400 6d 65 61 6e 73 20 74 68 61 74 20 65 76 65 72 79  means that every
38401 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 20   term of the OR 
38402 63 6c 61 75 73 65 20 69 73 20 6f 66 20 74 68 65  clause is of the
38403 20 66 6f 72 6d 0a 20 20 2a 2a 20 22 74 61 62 6c   form.  ** "tabl
38404 65 2e 63 6f 6c 75 6d 6e 3d 65 78 70 72 22 20 66  e.column=expr" f
38405 6f 72 20 73 6f 6d 65 20 73 69 6e 67 6c 65 20 74  or some single t
38406 61 62 6c 65 2e 20 20 54 68 65 20 6f 6e 65 20 62  able.  The one b
38407 69 74 20 74 68 61 74 20 69 73 20 73 65 74 0a 20  it that is set. 
38408 20 2a 2a 20 77 69 6c 6c 20 63 6f 72 72 65 73 70   ** will corresp
38409 6f 6e 64 20 74 6f 20 74 68 65 20 63 6f 6d 6d 6f  ond to the commo
3840a 6e 20 74 61 62 6c 65 2e 20 20 57 65 20 73 74 69  n table.  We sti
3840b 6c 6c 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b  ll need to check
3840c 20 74 6f 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75   to make.  ** su
3840d 72 65 20 74 68 65 20 73 61 6d 65 20 63 6f 6c 75  re the same colu
3840e 6d 6e 20 69 73 20 75 73 65 64 20 6f 6e 20 61 6c  mn is used on al
3840f 6c 20 74 65 72 6d 73 2e 20 20 54 68 65 20 32 2d  l terms.  The 2-
38410 62 69 74 20 63 61 73 65 20 69 73 20 77 68 65 6e  bit case is when
38411 0a 20 20 2a 2a 20 74 68 65 20 61 6c 6c 20 74 65  .  ** the all te
38412 72 6d 73 20 61 72 65 20 6f 66 20 74 68 65 20 66  rms are of the f
38413 6f 72 6d 20 22 74 61 62 6c 65 31 2e 63 6f 6c 75  orm "table1.colu
38414 6d 6e 3d 74 61 62 6c 65 32 2e 63 6f 6c 75 6d 6e  mn=table2.column
38415 22 2e 20 20 49 74 0a 20 20 2a 2a 20 6d 69 67 68  ".  It.  ** migh
38416 74 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f  t be possible to
38417 20 66 6f 72 6d 20 61 6e 20 49 4e 20 6f 70 65 72   form an IN oper
38418 61 74 6f 72 20 77 69 74 68 20 65 69 74 68 65 72  ator with either
38419 20 74 61 62 6c 65 31 2e 63 6f 6c 75 6d 6e 0a 20   table1.column. 
3841a 20 2a 2a 20 6f 72 20 74 61 62 6c 65 32 2e 63 6f   ** or table2.co
3841b 6c 75 6d 6e 20 61 73 20 74 68 65 20 4c 48 53 20  lumn as the LHS 
3841c 69 66 20 65 69 74 68 65 72 20 69 73 20 63 6f 6d  if either is com
3841d 6d 6f 6e 20 74 6f 20 65 76 65 72 79 20 74 65 72  mon to every ter
3841e 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 4f 52  m of.  ** the OR
3841f 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a 20 20   clause..  **.  
38420 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 65 72  ** Note that ter
38421 6d 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  ms of the form "
38422 74 61 62 6c 65 2e 63 6f 6c 75 6d 6e 31 3d 74 61  table.column1=ta
38423 62 6c 65 2e 63 6f 6c 75 6d 6e 32 22 20 28 74 68  ble.column2" (th
38424 65 0a 20 20 2a 2a 20 73 61 6d 65 20 74 61 62 6c  e.  ** same tabl
38425 65 20 6f 6e 20 62 6f 74 68 20 73 69 7a 65 73 20  e on both sizes 
38426 6f 66 20 74 68 65 20 3d 3d 29 20 63 61 6e 6e 6f  of the ==) canno
38427 74 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 2e 0a  t be optimized..
38428 20 20 2a 2f 0a 20 20 69 66 28 20 63 68 6e 67 54    */.  if( chngT
38429 6f 49 4e 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f  oIN ){.    int o
3842a 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 30 3b  kToChngToIN = 0;
3842b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
3842c 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74  the conversion t
3842d 6f 20 49 4e 20 69 73 20 76 61 6c 69 64 20 2a 2f  o IN is valid */
3842e 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  .    int iColumn
3842f 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 2f   = -1;         /
38430 2a 20 43 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 6f  * Column index o
38431 6e 20 6c 68 73 20 6f 66 20 49 4e 20 6f 70 65 72  n lhs of IN oper
38432 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ator */.    int 
38433 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 20 20 20  iCursor = -1;   
38434 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63        /* Table c
38435 75 72 73 6f 72 20 63 6f 6d 6d 6f 6e 20 74 6f 20  ursor common to 
38436 61 6c 6c 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20  all terms */.   
38437 20 69 6e 74 20 6a 20 3d 20 30 3b 20 20 20 20 20   int j = 0;     
38438 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
38439 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20  op counter */.. 
3843a 20 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72     /* Search for
3843b 20 61 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c   a table and col
3843c 75 6d 6e 20 74 68 61 74 20 61 70 70 65 61 72 73  umn that appears
3843d 20 6f 6e 20 6f 6e 65 20 73 69 64 65 20 6f 72 20   on one side or 
3843e 74 68 65 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72  the.    ** other
3843f 20 6f 66 20 74 68 65 20 3d 3d 20 6f 70 65 72 61   of the == opera
38440 74 6f 72 20 69 6e 20 65 76 65 72 79 20 73 75 62  tor in every sub
38441 74 65 72 6d 2e 20 20 54 68 61 74 20 74 61 62 6c  term.  That tabl
38442 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 20 20 20  e and column.   
38443 20 2a 2a 20 77 69 6c 6c 20 62 65 20 72 65 63 6f   ** will be reco
38444 72 64 65 64 20 69 6e 20 69 43 75 72 73 6f 72 20  rded in iCursor 
38445 61 6e 64 20 69 43 6f 6c 75 6d 6e 2e 20 20 54 68  and iColumn.  Th
38446 65 72 65 20 6d 69 67 68 74 20 6e 6f 74 20 62 65  ere might not be
38447 20 61 6e 79 0a 20 20 20 20 2a 2a 20 73 75 63 68   any.    ** such
38448 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d   table and colum
38449 6e 2e 20 20 53 65 74 20 6f 6b 54 6f 43 68 6e 67  n.  Set okToChng
3844a 54 6f 49 4e 20 69 66 20 61 6e 20 61 70 70 72 6f  ToIN if an appro
3844b 70 72 69 61 74 65 20 74 61 62 6c 65 0a 20 20 20  priate table.   
3844c 20 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 69   ** and column i
3844d 73 20 66 6f 75 6e 64 20 62 75 74 20 6c 65 61 76  s found but leav
3844e 65 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 66  e okToChngToIN f
3844f 61 6c 73 65 20 69 66 20 6e 6f 74 20 66 6f 75 6e  alse if not foun
38450 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  d..    */.    fo
38451 72 28 6a 3d 30 3b 20 6a 3c 32 20 26 26 20 21 6f  r(j=0; j<2 && !o
38452 6b 54 6f 43 68 6e 67 54 6f 49 4e 3b 20 6a 2b 2b  kToChngToIN; j++
38453 29 7b 0a 20 20 20 20 20 20 70 4f 72 54 65 72 6d  ){.      pOrTerm
38454 20 3d 20 70 4f 72 57 63 2d 3e 61 3b 0a 20 20 20   = pOrWc->a;.   
38455 20 20 20 66 6f 72 28 69 3d 70 4f 72 57 63 2d 3e     for(i=pOrWc->
38456 6e 54 65 72 6d 2d 31 3b 20 69 3e 3d 30 3b 20 69  nTerm-1; i>=0; i
38457 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a  --, pOrTerm++){.
38458 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
38459 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  pOrTerm->eOperat
3845a 6f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 20  or==WO_EQ );.   
3845b 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74       pOrTerm->wt
3845c 46 6c 61 67 73 20 26 3d 20 7e 54 45 52 4d 5f 4f  Flags &= ~TERM_O
3845d 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 69 66  R_OK;.        if
3845e 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43  ( pOrTerm->leftC
3845f 75 72 73 6f 72 3d 3d 69 43 75 72 73 6f 72 20 29  ursor==iCursor )
38460 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
38461 68 69 73 20 69 73 20 74 68 65 20 32 2d 62 69 74  his is the 2-bit
38462 20 63 61 73 65 20 61 6e 64 20 77 65 20 61 72 65   case and we are
38463 20 6f 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 69   on the second i
38464 74 65 72 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20  teration and.   
38465 20 20 20 20 20 20 20 2a 2a 20 63 75 72 72 65 6e         ** curren
38466 74 20 74 65 72 6d 20 69 73 20 66 72 6f 6d 20 74  t term is from t
38467 68 65 20 66 69 72 73 74 20 69 74 65 72 61 74 69  he first iterati
38468 6f 6e 2e 20 20 53 6f 20 73 6b 69 70 20 74 68 69  on.  So skip thi
38469 73 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 20  s term. */.     
3846a 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 3d 3d       assert( j==
3846b 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63  1 );.          c
3846c 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
3846d 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 28   }.        if( (
3846e 63 68 6e 67 54 6f 49 4e 20 26 20 67 65 74 4d 61  chngToIN & getMa
3846f 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 4f 72  sk(pMaskSet, pOr
38470 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
38471 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ))==0 ){.       
38472 20 20 20 2f 2a 20 54 68 69 73 20 74 65 72 6d 20     /* This term 
38473 6d 75 73 74 20 62 65 20 6f 66 20 74 68 65 20 66  must be of the f
38474 6f 72 6d 20 74 31 2e 61 3d 3d 74 32 2e 62 20 77  orm t1.a==t2.b w
38475 68 65 72 65 20 74 32 20 69 73 20 69 6e 20 74 68  here t2 is in th
38476 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63  e.          ** c
38477 68 6e 67 54 6f 49 4e 20 73 65 74 20 62 75 74 20  hngToIN set but 
38478 74 31 20 69 73 20 6e 6f 74 2e 20 20 54 68 69 73  t1 is not.  This
38479 20 74 65 72 6d 20 77 69 6c 6c 20 62 65 20 65 69   term will be ei
3847a 74 68 65 72 20 70 72 65 63 65 65 64 65 64 0a 20  ther preceeded. 
3847b 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 66           ** or f
3847c 6f 6c 6c 77 65 64 20 62 79 20 61 6e 20 69 6e 76  ollwed by an inv
3847d 65 72 74 65 64 20 63 6f 70 79 20 28 74 32 2e 62  erted copy (t2.b
3847e 3d 3d 74 31 2e 61 29 2e 20 20 53 6b 69 70 20 74  ==t1.a).  Skip t
3847f 68 69 73 20 74 65 72 6d 20 0a 20 20 20 20 20 20  his term .      
38480 20 20 20 20 2a 2a 20 61 6e 64 20 75 73 65 20 69      ** and use i
38481 74 73 20 69 6e 76 65 72 73 69 6f 6e 2e 20 2a 2f  ts inversion. */
38482 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74 63  .          testc
38483 61 73 65 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74  ase( pOrTerm->wt
38484 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 50  Flags & TERM_COP
38485 49 45 44 20 29 3b 0a 20 20 20 20 20 20 20 20 20  IED );.         
38486 20 74 65 73 74 63 61 73 65 28 20 70 4f 72 54 65   testcase( pOrTe
38487 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
38488 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 0a 20 20  RM_VIRTUAL );.  
38489 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
3848a 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73  pOrTerm->wtFlags
3848b 20 26 20 28 54 45 52 4d 5f 43 4f 50 49 45 44 7c   & (TERM_COPIED|
3848c 54 45 52 4d 5f 56 49 52 54 55 41 4c 29 20 29 3b  TERM_VIRTUAL) );
3848d 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69  .          conti
3848e 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  nue;.        }. 
3848f 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d         iColumn =
38490 20 70 4f 72 54 65 72 6d 2d 3e 75 2e 6c 65 66 74   pOrTerm->u.left
38491 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20  Column;.        
38492 69 43 75 72 73 6f 72 20 3d 20 70 4f 72 54 65 72  iCursor = pOrTer
38493 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3b 0a 20  m->leftCursor;. 
38494 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
38495 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
38496 69 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  i<0 ){.        /
38497 2a 20 4e 6f 20 63 61 6e 64 69 64 61 74 65 20 74  * No candidate t
38498 61 62 6c 65 2b 63 6f 6c 75 6d 6e 20 77 61 73 20  able+column was 
38499 66 6f 75 6e 64 2e 20 20 54 68 69 73 20 63 61 6e  found.  This can
3849a 20 6f 6e 6c 79 20 6f 63 63 75 72 0a 20 20 20 20   only occur.    
3849b 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 73 65      ** on the se
3849c 63 6f 6e 64 20 69 74 65 72 61 74 69 6f 6e 20 2a  cond iteration *
3849d 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
3849e 28 20 6a 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  ( j==1 );.      
3849f 20 20 61 73 73 65 72 74 28 20 28 63 68 6e 67 54    assert( (chngT
384a0 6f 49 4e 26 28 63 68 6e 67 54 6f 49 4e 2d 31 29  oIN&(chngToIN-1)
384a1 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  )==0 );.        
384a2 61 73 73 65 72 74 28 20 63 68 6e 67 54 6f 49 4e  assert( chngToIN
384a3 3d 3d 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53  ==getMask(pMaskS
384a4 65 74 2c 20 69 43 75 72 73 6f 72 29 20 29 3b 0a  et, iCursor) );.
384a5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
384a6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 65 73       }.      tes
384a7 74 63 61 73 65 28 20 6a 3d 3d 31 20 29 3b 0a 0a  tcase( j==1 );..
384a8 20 20 20 20 20 20 2f 2a 20 57 65 20 68 61 76 65        /* We have
384a9 20 66 6f 75 6e 64 20 61 20 63 61 6e 64 69 64 61   found a candida
384aa 74 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c  te table and col
384ab 75 6d 6e 2e 20 20 43 68 65 63 6b 20 74 6f 20 73  umn.  Check to s
384ac 65 65 20 69 66 20 74 68 61 74 0a 20 20 20 20 20  ee if that.     
384ad 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 63 6f   ** table and co
384ae 6c 75 6d 6e 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  lumn is common t
384af 6f 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e 20  o every term in 
384b0 74 68 65 20 4f 52 20 63 6c 61 75 73 65 20 2a 2f  the OR clause */
384b1 0a 20 20 20 20 20 20 6f 6b 54 6f 43 68 6e 67 54  .      okToChngT
384b2 6f 49 4e 20 3d 20 31 3b 0a 20 20 20 20 20 20 66  oIN = 1;.      f
384b3 6f 72 28 3b 20 69 3e 3d 30 20 26 26 20 6f 6b 54  or(; i>=0 && okT
384b4 6f 43 68 6e 67 54 6f 49 4e 3b 20 69 2d 2d 2c 20  oChngToIN; i--, 
384b5 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20  pOrTerm++){.    
384b6 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54      assert( pOrT
384b7 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d  erm->eOperator==
384b8 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 20  WO_EQ );.       
384b9 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65   if( pOrTerm->le
384ba 66 74 43 75 72 73 6f 72 21 3d 69 43 75 72 73 6f  ftCursor!=iCurso
384bb 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  r ){.          p
384bc 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20  OrTerm->wtFlags 
384bd 26 3d 20 7e 54 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a  &= ~TERM_OR_OK;.
384be 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
384bf 28 20 70 4f 72 54 65 72 6d 2d 3e 75 2e 6c 65 66  ( pOrTerm->u.lef
384c0 74 43 6f 6c 75 6d 6e 21 3d 69 43 6f 6c 75 6d 6e  tColumn!=iColumn
384c1 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 6b   ){.          ok
384c2 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a  ToChngToIN = 0;.
384c3 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
384c4 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 66 66           int aff
384c5 4c 65 66 74 2c 20 61 66 66 52 69 67 68 74 3b 0a  Left, affRight;.
384c6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
384c7 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73  the right-hand s
384c8 69 64 65 20 69 73 20 61 6c 73 6f 20 61 20 63 6f  ide is also a co
384c9 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20 61  lumn, then the a
384ca 66 66 69 6e 69 74 69 65 73 0a 20 20 20 20 20 20  ffinities.      
384cb 20 20 20 20 2a 2a 20 6f 66 20 62 6f 74 68 20 72      ** of both r
384cc 69 67 68 74 20 61 6e 64 20 6c 65 66 74 20 73 69  ight and left si
384cd 64 65 73 20 6d 75 73 74 20 62 65 20 73 75 63 68  des must be such
384ce 20 74 68 61 74 20 6e 6f 20 74 79 70 65 0a 20 20   that no type.  
384cf 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 76 65          ** conve
384d0 72 73 69 6f 6e 73 20 61 72 65 20 72 65 71 75 69  rsions are requi
384d1 72 65 64 20 6f 6e 20 74 68 65 20 72 69 67 68 74  red on the right
384d2 2e 20 20 28 54 69 63 6b 65 74 20 23 32 32 34 39  .  (Ticket #2249
384d3 29 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ).          */. 
384d4 20 20 20 20 20 20 20 20 20 61 66 66 52 69 67 68           affRigh
384d5 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  t = sqlite3ExprA
384d6 66 66 69 6e 69 74 79 28 70 4f 72 54 65 72 6d 2d  ffinity(pOrTerm-
384d7 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b  >pExpr->pRight);
384d8 0a 20 20 20 20 20 20 20 20 20 20 61 66 66 4c 65  .          affLe
384d9 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ft = sqlite3Expr
384da 41 66 66 69 6e 69 74 79 28 70 4f 72 54 65 72 6d  Affinity(pOrTerm
384db 2d 3e 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b  ->pExpr->pLeft);
384dc 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61  .          if( a
384dd 66 66 52 69 67 68 74 21 3d 30 20 26 26 20 61 66  ffRight!=0 && af
384de 66 52 69 67 68 74 21 3d 61 66 66 4c 65 66 74 20  fRight!=affLeft 
384df 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6f  ){.            o
384e0 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 30 3b  kToChngToIN = 0;
384e1 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
384e2 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4f  {.            pO
384e3 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c  rTerm->wtFlags |
384e4 3d 20 54 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20  = TERM_OR_OK;.  
384e5 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
384e6 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
384e7 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 69  }..    /* At thi
384e8 73 20 70 6f 69 6e 74 2c 20 6f 6b 54 6f 43 68 6e  s point, okToChn
384e9 67 54 6f 49 4e 20 69 73 20 74 72 75 65 20 69 66  gToIN is true if
384ea 20 6f 72 69 67 69 6e 61 6c 20 70 54 65 72 6d 20   original pTerm 
384eb 73 61 74 69 73 66 69 65 73 0a 20 20 20 20 2a 2a  satisfies.    **
384ec 20 63 61 73 65 20 31 2e 20 20 49 6e 20 74 68 61   case 1.  In tha
384ed 74 20 63 61 73 65 2c 20 63 6f 6e 73 74 72 75 63  t case, construc
384ee 74 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20  t a new virtual 
384ef 74 65 72 6d 20 74 68 61 74 20 69 73 20 0a 20 20  term that is .  
384f0 20 20 2a 2a 20 70 54 65 72 6d 20 63 6f 6e 76 65    ** pTerm conve
384f1 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 49 4e 20  rted into an IN 
384f2 6f 70 65 72 61 74 6f 72 2e 0a 20 20 20 20 2a 2a  operator..    **
384f3 0a 20 20 20 20 2a 2a 20 45 56 3a 20 52 2d 30 30  .    ** EV: R-00
384f4 32 31 31 2d 31 35 31 30 30 0a 20 20 20 20 2a 2f  211-15100.    */
384f5 0a 20 20 20 20 69 66 28 20 6f 6b 54 6f 43 68 6e  .    if( okToChn
384f6 67 54 6f 49 4e 20 29 7b 0a 20 20 20 20 20 20 45  gToIN ){.      E
384f7 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20 20 20  xpr *pDup;      
384f8 20 20 20 20 20 20 2f 2a 20 41 20 74 72 61 6e 73        /* A trans
384f9 69 65 6e 74 20 64 75 70 6c 69 63 61 74 65 20 65  ient duplicate e
384fa 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20  xpression */.   
384fb 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69     ExprList *pLi
384fc 73 74 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65  st = 0;   /* The
384fd 20 52 48 53 20 6f 66 20 74 68 65 20 49 4e 20 6f   RHS of the IN o
384fe 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20  perator */.     
384ff 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 30   Expr *pLeft = 0
38500 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c  ;       /* The L
38501 48 53 20 6f 66 20 74 68 65 20 49 4e 20 6f 70 65  HS of the IN ope
38502 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 45  rator */.      E
38503 78 70 72 20 2a 70 4e 65 77 3b 20 20 20 20 20 20  xpr *pNew;      
38504 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d        /* The com
38505 70 6c 65 74 65 20 49 4e 20 6f 70 65 72 61 74 6f  plete IN operato
38506 72 20 2a 2f 0a 0a 20 20 20 20 20 20 66 6f 72 28  r */..      for(
38507 69 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31  i=pOrWc->nTerm-1
38508 2c 20 70 4f 72 54 65 72 6d 3d 70 4f 72 57 63 2d  , pOrTerm=pOrWc-
38509 3e 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70  >a; i>=0; i--, p
3850a 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20  OrTerm++){.     
3850b 20 20 20 69 66 28 20 28 70 4f 72 54 65 72 6d 2d     if( (pOrTerm-
3850c 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
3850d 4f 52 5f 4f 4b 29 3d 3d 30 20 29 20 63 6f 6e 74  OR_OK)==0 ) cont
3850e 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 61 73  inue;.        as
3850f 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65  sert( pOrTerm->e
38510 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 45 51 20  Operator==WO_EQ 
38511 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
38512 74 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74  t( pOrTerm->left
38513 43 75 72 73 6f 72 3d 3d 69 43 75 72 73 6f 72 20  Cursor==iCursor 
38514 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
38515 74 28 20 70 4f 72 54 65 72 6d 2d 3e 75 2e 6c 65  t( pOrTerm->u.le
38516 66 74 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d  ftColumn==iColum
38517 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 70 44 75  n );.        pDu
38518 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  p = sqlite3ExprD
38519 75 70 28 64 62 2c 20 70 4f 72 54 65 72 6d 2d 3e  up(db, pOrTerm->
3851a 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 30  pExpr->pRight, 0
3851b 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74  );.        pList
3851c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
3851d 73 74 41 70 70 65 6e 64 28 70 57 43 2d 3e 70 50  stAppend(pWC->pP
3851e 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 70 44 75  arse, pList, pDu
3851f 70 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 65 66  p);.        pLef
38520 74 20 3d 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78  t = pOrTerm->pEx
38521 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20  pr->pLeft;.     
38522 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
38523 20 70 4c 65 66 74 21 3d 30 20 29 3b 0a 20 20 20   pLeft!=0 );.   
38524 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65     pDup = sqlite
38525 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4c 65  3ExprDup(db, pLe
38526 66 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4e  ft, 0);.      pN
38527 65 77 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  ew = sqlite3PExp
38528 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c  r(pParse, TK_IN,
38529 20 70 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20   pDup, 0, 0);.  
3852a 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a      if( pNew ){.
3852b 20 20 20 20 20 20 20 20 69 6e 74 20 69 64 78 4e          int idxN
3852c 65 77 3b 0a 20 20 20 20 20 20 20 20 74 72 61 6e  ew;.        tran
3852d 73 66 65 72 4a 6f 69 6e 4d 61 72 6b 69 6e 67 73  sferJoinMarkings
3852e 28 70 4e 65 77 2c 20 70 45 78 70 72 29 3b 0a 20  (pNew, pExpr);. 
3852f 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21         assert( !
38530 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
38531 70 4e 65 77 2c 20 45 50 5f 78 49 73 53 65 6c 65  pNew, EP_xIsSele
38532 63 74 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70  ct) );.        p
38533 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70  New->x.pList = p
38534 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 69 64  List;.        id
38535 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61 75  xNew = whereClau
38536 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70 4e  seInsert(pWC, pN
38537 65 77 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  ew, TERM_VIRTUAL
38538 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a  |TERM_DYNAMIC);.
38539 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
3853a 28 20 69 64 78 4e 65 77 3d 3d 30 20 29 3b 0a 20  ( idxNew==0 );. 
3853b 20 20 20 20 20 20 20 65 78 70 72 41 6e 61 6c 79         exprAnaly
3853c 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64  ze(pSrc, pWC, id
3853d 78 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 20 70  xNew);.        p
3853e 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69  Term = &pWC->a[i
3853f 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20 20  dxTerm];.       
38540 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 2e   pWC->a[idxNew].
38541 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72  iParent = idxTer
38542 6d 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d  m;.        pTerm
38543 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a 20 20  ->nChild = 1;.  
38544 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
38545 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
38546 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69  stDelete(db, pLi
38547 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  st);.      }.   
38548 20 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61     pTerm->eOpera
38549 74 6f 72 20 3d 20 57 4f 5f 4e 4f 4f 50 3b 20 20  tor = WO_NOOP;  
3854a 2f 2a 20 63 61 73 65 20 31 20 74 72 75 6d 70 73  /* case 1 trumps
3854b 20 63 61 73 65 20 32 20 2a 2f 0a 20 20 20 20 7d   case 2 */.    }
3854c 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  .  }.}.#endif /*
3854d 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52   !SQLITE_OMIT_OR
3854e 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 26 26  _OPTIMIZATION &&
3854f 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55   !SQLITE_OMIT_SU
38550 42 51 55 45 52 59 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  BQUERY */.../*.*
38551 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20 74  * The input to t
38552 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
38553 6e 20 57 68 65 72 65 54 65 72 6d 20 73 74 72 75  n WhereTerm stru
38554 63 74 75 72 65 20 77 69 74 68 20 6f 6e 6c 79 20  cture with only 
38555 74 68 65 0a 2a 2a 20 22 70 45 78 70 72 22 20 66  the.** "pExpr" f
38556 69 65 6c 64 20 66 69 6c 6c 65 64 20 69 6e 2e 20  ield filled in. 
38557 20 54 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73   The job of this
38558 20 72 6f 75 74 69 6e 65 20 69 73 20 74 6f 20 61   routine is to a
38559 6e 61 6c 79 7a 65 20 74 68 65 0a 2a 2a 20 73 75  nalyze the.** su
3855a 62 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20  bexpression and 
3855b 70 6f 70 75 6c 61 74 65 20 61 6c 6c 20 74 68 65  populate all the
3855c 20 6f 74 68 65 72 20 66 69 65 6c 64 73 20 6f 66   other fields of
3855d 20 74 68 65 20 57 68 65 72 65 54 65 72 6d 0a 2a   the WhereTerm.*
3855e 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  * structure..**.
3855f 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  ** If the expres
38560 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66  sion is of the f
38561 6f 72 6d 20 22 3c 65 78 70 72 3e 20 3c 6f 70 3e  orm "<expr> <op>
38562 20 58 22 20 69 74 20 67 65 74 73 20 63 6f 6d 6d   X" it gets comm
38563 75 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 73  uted.** to the s
38564 74 61 6e 64 61 72 64 20 66 6f 72 6d 20 6f 66 20  tandard form of 
38565 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 2e  "X <op> <expr>".
38566 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78  .**.** If the ex
38567 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 74  pression is of t
38568 68 65 20 66 6f 72 6d 20 22 58 20 3c 6f 70 3e 20  he form "X <op> 
38569 59 22 20 77 68 65 72 65 20 62 6f 74 68 20 58 20  Y" where both X 
3856a 61 6e 64 20 59 20 61 72 65 0a 2a 2a 20 63 6f 6c  and Y are.** col
3856b 75 6d 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 6f  umns, then the o
3856c 72 69 67 69 6e 61 6c 20 65 78 70 72 65 73 73 69  riginal expressi
3856d 6f 6e 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20  on is unchanged 
3856e 61 6e 64 20 61 20 6e 65 77 20 76 69 72 74 75 61  and a new virtua
3856f 6c 0a 2a 2a 20 74 65 72 6d 20 6f 66 20 74 68 65  l.** term of the
38570 20 66 6f 72 6d 20 22 59 20 3c 6f 70 3e 20 58 22   form "Y <op> X"
38571 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65   is added to the
38572 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 61 6e   WHERE clause an
38573 64 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64 20 73 65  d.** analyzed se
38574 70 61 72 61 74 65 6c 79 2e 20 20 54 68 65 20 6f  parately.  The o
38575 72 69 67 69 6e 61 6c 20 74 65 72 6d 20 69 73 20  riginal term is 
38576 6d 61 72 6b 65 64 20 77 69 74 68 20 54 45 52 4d  marked with TERM
38577 5f 43 4f 50 49 45 44 0a 2a 2a 20 61 6e 64 20 74  _COPIED.** and t
38578 68 65 20 6e 65 77 20 74 65 72 6d 20 69 73 20 6d  he new term is m
38579 61 72 6b 65 64 20 77 69 74 68 20 54 45 52 4d 5f  arked with TERM_
3857a 44 59 4e 41 4d 49 43 20 28 62 65 63 61 75 73 65  DYNAMIC (because
3857b 20 69 74 27 73 20 70 45 78 70 72 0a 2a 2a 20 6e   it's pExpr.** n
3857c 65 65 64 73 20 74 6f 20 62 65 20 66 72 65 65 64  eeds to be freed
3857d 20 77 69 74 68 20 74 68 65 20 57 68 65 72 65 43   with the WhereC
3857e 6c 61 75 73 65 29 20 61 6e 64 20 54 45 52 4d 5f  lause) and TERM_
3857f 56 49 52 54 55 41 4c 20 28 62 65 63 61 75 73 65  VIRTUAL (because
38580 20 69 74 0a 2a 2a 20 69 73 20 61 20 63 6f 6d 6d   it.** is a comm
38581 75 74 65 64 20 63 6f 70 79 20 6f 66 20 61 20 70  uted copy of a p
38582 72 69 6f 72 20 74 65 72 6d 2e 29 20 20 54 68 65  rior term.)  The
38583 20 6f 72 69 67 69 6e 61 6c 20 74 65 72 6d 20 68   original term h
38584 61 73 20 6e 43 68 69 6c 64 3d 31 0a 2a 2a 20 61  as nChild=1.** a
38585 6e 64 20 74 68 65 20 63 6f 70 79 20 68 61 73 20  nd the copy has 
38586 69 64 78 50 61 72 65 6e 74 20 73 65 74 20 74 6f  idxParent set to
38587 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
38588 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72 6d 2e  e original term.
38589 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3858a 65 78 70 72 41 6e 61 6c 79 7a 65 28 0a 20 20 53  exprAnalyze(.  S
3858b 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20  rcList *pSrc,   
3858c 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20           /* the 
3858d 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
3858e 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
3858f 43 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68  C,         /* th
38590 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
38591 2f 0a 20 20 69 6e 74 20 69 64 78 54 65 72 6d 20  /.  int idxTerm 
38592 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38593 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 74 65   Index of the te
38594 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  rm to be analyze
38595 64 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54  d */.){.  WhereT
38596 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20  erm *pTerm;     
38597 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
38598 65 20 74 65 72 6d 20 74 6f 20 62 65 20 61 6e 61  e term to be ana
38599 6c 79 7a 65 64 20 2a 2f 0a 20 20 57 68 65 72 65  lyzed */.  Where
3859a 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
3859b 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  t;          /* S
3859c 65 74 20 6f 66 20 74 61 62 6c 65 20 69 6e 64 65  et of table inde
3859d 78 20 6d 61 73 6b 73 20 2a 2f 0a 20 20 45 78 70  x masks */.  Exp
3859e 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
3859f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
385a0 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
385a1 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a  to be analyzed *
385a2 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72  /.  Bitmask prer
385a3 65 71 4c 65 66 74 3b 20 20 20 20 20 20 20 20 20  eqLeft;         
385a4 20 20 20 20 20 2f 2a 20 50 72 65 72 65 71 75 65       /* Prereque
385a5 73 69 74 65 73 20 6f 66 20 74 68 65 20 70 45 78  sites of the pEx
385a6 70 72 2d 3e 70 4c 65 66 74 20 2a 2f 0a 20 20 42  pr->pLeft */.  B
385a7 69 74 6d 61 73 6b 20 70 72 65 72 65 71 41 6c 6c  itmask prereqAll
385a8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
385a9 2f 2a 20 50 72 65 72 65 71 75 65 73 69 74 65 73  /* Prerequesites
385aa 20 6f 66 20 70 45 78 70 72 20 2a 2f 0a 20 20 42   of pExpr */.  B
385ab 69 74 6d 61 73 6b 20 65 78 74 72 61 52 69 67 68  itmask extraRigh
385ac 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
385ad 2f 2a 20 45 78 74 72 61 20 64 65 70 65 6e 64 65  /* Extra depende
385ae 6e 63 69 65 73 20 6f 6e 20 4c 45 46 54 20 4a 4f  ncies on LEFT JO
385af 49 4e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 53  IN */.  Expr *pS
385b0 74 72 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20  tr1 = 0;        
385b1 20 20 20 20 20 20 20 20 20 2f 2a 20 52 48 53 20           /* RHS 
385b2 6f 66 20 4c 49 4b 45 2f 47 4c 4f 42 20 6f 70 65  of LIKE/GLOB ope
385b3 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69  rator */.  int i
385b4 73 43 6f 6d 70 6c 65 74 65 20 3d 20 30 3b 20 20  sComplete = 0;  
385b5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
385b6 48 53 20 6f 66 20 4c 49 4b 45 2f 47 4c 4f 42 20  HS of LIKE/GLOB 
385b7 65 6e 64 73 20 77 69 74 68 20 77 69 6c 64 63 61  ends with wildca
385b8 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 61  rd */.  int noCa
385b9 73 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  se = 0;         
385ba 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4b 45           /* LIKE
385bb 2f 47 4c 4f 42 20 64 69 73 74 69 6e 67 75 69 73  /GLOB distinguis
385bc 68 65 73 20 63 61 73 65 20 2a 2f 0a 20 20 69 6e  hes case */.  in
385bd 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  t op;           
385be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
385bf 2a 20 54 6f 70 2d 6c 65 76 65 6c 20 6f 70 65 72  * Top-level oper
385c0 61 74 6f 72 2e 20 20 70 45 78 70 72 2d 3e 6f 70  ator.  pExpr->op
385c1 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61   */.  Parse *pPa
385c2 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61 72 73  rse = pWC->pPars
385c3 65 3b 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  e;     /* Parsin
385c4 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 73  g context */.  s
385c5 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
385c6 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20  rse->db;        
385c7 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
385c8 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28  ection */..  if(
385c9 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
385ca 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  d ){.    return;
385cb 0a 20 20 7d 0a 20 20 70 54 65 72 6d 20 3d 20 26  .  }.  pTerm = &
385cc 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b  pWC->a[idxTerm];
385cd 0a 20 20 70 4d 61 73 6b 53 65 74 20 3d 20 70 57  .  pMaskSet = pW
385ce 43 2d 3e 70 4d 61 73 6b 53 65 74 3b 0a 20 20 70  C->pMaskSet;.  p
385cf 45 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45  Expr = pTerm->pE
385d0 78 70 72 3b 0a 20 20 70 72 65 72 65 71 4c 65 66  xpr;.  prereqLef
385d1 74 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61  t = exprTableUsa
385d2 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78  ge(pMaskSet, pEx
385d3 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 6f 70  pr->pLeft);.  op
385d4 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20   = pExpr->op;.  
385d5 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e 20 29 7b  if( op==TK_IN ){
385d6 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
385d7 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b  pr->pRight==0 );
385d8 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73  .    if( ExprHas
385d9 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
385da 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
385db 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 70 72  .      pTerm->pr
385dc 65 72 65 71 52 69 67 68 74 20 3d 20 65 78 70 72  ereqRight = expr
385dd 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65  SelectTableUsage
385de 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72  (pMaskSet, pExpr
385df 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20  ->x.pSelect);.  
385e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
385e1 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68  Term->prereqRigh
385e2 74 20 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c  t = exprListTabl
385e3 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c  eUsage(pMaskSet,
385e4 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29   pExpr->x.pList)
385e5 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
385e6 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c  if( op==TK_ISNUL
385e7 4c 20 29 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e  L ){.    pTerm->
385e8 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 30 3b  prereqRight = 0;
385e9 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54  .  }else{.    pT
385ea 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74  erm->prereqRight
385eb 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67   = exprTableUsag
385ec 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70  e(pMaskSet, pExp
385ed 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 7d 0a  r->pRight);.  }.
385ee 20 20 70 72 65 72 65 71 41 6c 6c 20 3d 20 65 78    prereqAll = ex
385ef 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
385f0 73 6b 53 65 74 2c 20 70 45 78 70 72 29 3b 0a 20  skSet, pExpr);. 
385f1 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
385f2 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 46  erty(pExpr, EP_F
385f3 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20  romJoin) ){.    
385f4 42 69 74 6d 61 73 6b 20 78 20 3d 20 67 65 74 4d  Bitmask x = getM
385f5 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 45  ask(pMaskSet, pE
385f6 78 70 72 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54  xpr->iRightJoinT
385f7 61 62 6c 65 29 3b 0a 20 20 20 20 70 72 65 72 65  able);.    prere
385f8 71 41 6c 6c 20 7c 3d 20 78 3b 0a 20 20 20 20 65  qAll |= x;.    e
385f9 78 74 72 61 52 69 67 68 74 20 3d 20 78 2d 31 3b  xtraRight = x-1;
385fa 20 20 2f 2a 20 4f 4e 20 63 6c 61 75 73 65 20 74    /* ON clause t
385fb 65 72 6d 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  erms may not be 
385fc 75 73 65 64 20 77 69 74 68 20 61 6e 20 69 6e 64  used with an ind
385fd 65 78 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ex.             
385fe 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20            ** on 
385ff 6c 65 66 74 20 74 61 62 6c 65 20 6f 66 20 61 20  left table of a 
38600 4c 45 46 54 20 4a 4f 49 4e 2e 20 20 54 69 63 6b  LEFT JOIN.  Tick
38601 65 74 20 23 33 30 31 35 20 2a 2f 0a 20 20 7d 0a  et #3015 */.  }.
38602 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41    pTerm->prereqA
38603 6c 6c 20 3d 20 70 72 65 72 65 71 41 6c 6c 3b 0a  ll = prereqAll;.
38604 20 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72    pTerm->leftCur
38605 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 70 54 65 72  sor = -1;.  pTer
38606 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b  m->iParent = -1;
38607 0a 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  .  pTerm->eOpera
38608 74 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 61  tor = 0;.  if( a
38609 6c 6c 6f 77 65 64 4f 70 28 6f 70 29 20 26 26 20  llowedOp(op) && 
3860a 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69  (pTerm->prereqRi
3860b 67 68 74 20 26 20 70 72 65 72 65 71 4c 65 66 74  ght & prereqLeft
3860c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72  )==0 ){.    Expr
3860d 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d   *pLeft = pExpr-
3860e 3e 70 4c 65 66 74 3b 0a 20 20 20 20 45 78 70 72  >pLeft;.    Expr
3860f 20 2a 70 52 69 67 68 74 20 3d 20 70 45 78 70 72   *pRight = pExpr
38610 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 69 66  ->pRight;.    if
38611 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  ( pLeft->op==TK_
38612 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20  COLUMN ){.      
38613 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  pTerm->leftCurso
38614 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c  r = pLeft->iTabl
38615 65 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  e;.      pTerm->
38616 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70  u.leftColumn = p
38617 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20  Left->iColumn;. 
38618 20 20 20 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65       pTerm->eOpe
38619 72 61 74 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72  rator = operator
3861a 4d 61 73 6b 28 6f 70 29 3b 0a 20 20 20 20 7d 0a  Mask(op);.    }.
3861b 20 20 20 20 69 66 28 20 70 52 69 67 68 74 20 26      if( pRight &
3861c 26 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b  & pRight->op==TK
3861d 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20  _COLUMN ){.     
3861e 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4e 65 77   WhereTerm *pNew
3861f 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 44  ;.      Expr *pD
38620 75 70 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  up;.      if( pT
38621 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3e  erm->leftCursor>
38622 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  =0 ){.        in
38623 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 20 20  t idxNew;.      
38624 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33    pDup = sqlite3
38625 45 78 70 72 44 75 70 28 64 62 2c 20 70 45 78 70  ExprDup(db, pExp
38626 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  r, 0);.        i
38627 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
38628 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  led ){.         
38629 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
3862a 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 20 20  te(db, pDup);.  
3862b 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
3862c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3862d 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65    idxNew = where
3862e 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
3862f 2c 20 70 44 75 70 2c 20 54 45 52 4d 5f 56 49 52  , pDup, TERM_VIR
38630 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49  TUAL|TERM_DYNAMI
38631 43 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  C);.        if( 
38632 69 64 78 4e 65 77 3d 3d 30 20 29 20 72 65 74 75  idxNew==0 ) retu
38633 72 6e 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77  rn;.        pNew
38634 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 4e 65   = &pWC->a[idxNe
38635 77 5d 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77  w];.        pNew
38636 2d 3e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54  ->iParent = idxT
38637 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 54 65  erm;.        pTe
38638 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78  rm = &pWC->a[idx
38639 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20 20 20 70  Term];.        p
3863a 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31  Term->nChild = 1
3863b 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d  ;.        pTerm-
3863c 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d  >wtFlags |= TERM
3863d 5f 43 4f 50 49 45 44 3b 0a 20 20 20 20 20 20 7d  _COPIED;.      }
3863e 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 44  else{.        pD
3863f 75 70 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20  up = pExpr;.    
38640 20 20 20 20 70 4e 65 77 20 3d 20 70 54 65 72 6d      pNew = pTerm
38641 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
38642 65 78 70 72 43 6f 6d 6d 75 74 65 28 70 50 61 72  exprCommute(pPar
38643 73 65 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20  se, pDup);.     
38644 20 70 4c 65 66 74 20 3d 20 70 44 75 70 2d 3e 70   pLeft = pDup->p
38645 4c 65 66 74 3b 0a 20 20 20 20 20 20 70 4e 65 77  Left;.      pNew
38646 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 70  ->leftCursor = p
38647 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 20  Left->iTable;.  
38648 20 20 20 20 70 4e 65 77 2d 3e 75 2e 6c 65 66 74      pNew->u.left
38649 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e  Column = pLeft->
3864a 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 74  iColumn;.      t
3864b 65 73 74 63 61 73 65 28 20 28 70 72 65 72 65 71  estcase( (prereq
3864c 4c 65 66 74 20 7c 20 65 78 74 72 61 52 69 67 68  Left | extraRigh
3864d 74 29 20 21 3d 20 70 72 65 72 65 71 4c 65 66 74  t) != prereqLeft
3864e 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e   );.      pNew->
3864f 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 70 72  prereqRight = pr
38650 65 72 65 71 4c 65 66 74 20 7c 20 65 78 74 72 61  ereqLeft | extra
38651 52 69 67 68 74 3b 0a 20 20 20 20 20 20 70 4e 65  Right;.      pNe
38652 77 2d 3e 70 72 65 72 65 71 41 6c 6c 20 3d 20 70  w->prereqAll = p
38653 72 65 72 65 71 41 6c 6c 3b 0a 20 20 20 20 20 20  rereqAll;.      
38654 70 4e 65 77 2d 3e 65 4f 70 65 72 61 74 6f 72 20  pNew->eOperator 
38655 3d 20 6f 70 65 72 61 74 6f 72 4d 61 73 6b 28 70  = operatorMask(p
38656 44 75 70 2d 3e 6f 70 29 3b 0a 20 20 20 20 7d 0a  Dup->op);.    }.
38657 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
38658 49 54 45 5f 4f 4d 49 54 5f 42 45 54 57 45 45 4e  ITE_OMIT_BETWEEN
38659 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20  _OPTIMIZATION.  
3865a 2f 2a 20 49 66 20 61 20 74 65 72 6d 20 69 73 20  /* If a term is 
3865b 74 68 65 20 42 45 54 57 45 45 4e 20 6f 70 65 72  the BETWEEN oper
3865c 61 74 6f 72 2c 20 63 72 65 61 74 65 20 74 77 6f  ator, create two
3865d 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 65 72   new virtual ter
3865e 6d 73 0a 20 20 2a 2a 20 74 68 61 74 20 64 65 66  ms.  ** that def
3865f 69 6e 65 20 74 68 65 20 72 61 6e 67 65 20 74 68  ine the range th
38660 61 74 20 74 68 65 20 42 45 54 57 45 45 4e 20 69  at the BETWEEN i
38661 6d 70 6c 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20  mplements.  For 
38662 65 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20  example:.  **.  
38663 2a 2a 20 20 20 20 20 20 61 20 42 45 54 57 45 45  **      a BETWEE
38664 4e 20 62 20 41 4e 44 20 63 0a 20 20 2a 2a 0a 20  N b AND c.  **. 
38665 20 2a 2a 20 69 73 20 63 6f 6e 76 65 72 74 65 64   ** is converted
38666 20 69 6e 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a   into:.  **.  **
38667 20 20 20 20 20 20 28 61 20 42 45 54 57 45 45 4e        (a BETWEEN
38668 20 62 20 41 4e 44 20 63 29 20 41 4e 44 20 28 61   b AND c) AND (a
38669 3e 3d 62 29 20 41 4e 44 20 28 61 3c 3d 63 29 0a  >=b) AND (a<=c).
3866a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 74 77    **.  ** The tw
3866b 6f 20 6e 65 77 20 74 65 72 6d 73 20 61 72 65 20  o new terms are 
3866c 61 64 64 65 64 20 6f 6e 74 6f 20 74 68 65 20 65  added onto the e
3866d 6e 64 20 6f 66 20 74 68 65 20 57 68 65 72 65 43  nd of the WhereC
3866e 6c 61 75 73 65 20 6f 62 6a 65 63 74 2e 0a 20 20  lause object..  
3866f 2a 2a 20 54 68 65 20 6e 65 77 20 74 65 72 6d 73  ** The new terms
38670 20 61 72 65 20 22 64 79 6e 61 6d 69 63 22 20 61   are "dynamic" a
38671 6e 64 20 61 72 65 20 63 68 69 6c 64 72 65 6e 20  nd are children 
38672 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  of the original 
38673 42 45 54 57 45 45 4e 0a 20 20 2a 2a 20 74 65 72  BETWEEN.  ** ter
38674 6d 2e 20 20 54 68 61 74 20 6d 65 61 6e 73 20 74  m.  That means t
38675 68 61 74 20 69 66 20 74 68 65 20 42 45 54 57 45  hat if the BETWE
38676 45 4e 20 74 65 72 6d 20 69 73 20 63 6f 64 65 64  EN term is coded
38677 2c 20 74 68 65 20 63 68 69 6c 64 72 65 6e 20 61  , the children a
38678 72 65 0a 20 20 2a 2a 20 73 6b 69 70 70 65 64 2e  re.  ** skipped.
38679 20 20 4f 72 2c 20 69 66 20 74 68 65 20 63 68 69    Or, if the chi
3867a 6c 64 72 65 6e 20 61 72 65 20 73 61 74 69 73 66  ldren are satisf
3867b 69 65 64 20 62 79 20 61 6e 20 69 6e 64 65 78 2c  ied by an index,
3867c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 20 20   the original.  
3867d 2a 2a 20 42 45 54 57 45 45 4e 20 74 65 72 6d 20  ** BETWEEN term 
3867e 69 73 20 73 6b 69 70 70 65 64 2e 0a 20 20 2a 2f  is skipped..  */
3867f 0a 20 20 65 6c 73 65 20 69 66 28 20 70 45 78 70  .  else if( pExp
38680 72 2d 3e 6f 70 3d 3d 54 4b 5f 42 45 54 57 45 45  r->op==TK_BETWEE
38681 4e 20 26 26 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b  N && pWC->op==TK
38682 5f 41 4e 44 20 29 7b 0a 20 20 20 20 45 78 70 72  _AND ){.    Expr
38683 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45  List *pList = pE
38684 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20  xpr->x.pList;.  
38685 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 74 61    int i;.    sta
38686 74 69 63 20 63 6f 6e 73 74 20 75 38 20 6f 70 73  tic const u8 ops
38687 5b 5d 20 3d 20 7b 54 4b 5f 47 45 2c 20 54 4b 5f  [] = {TK_GE, TK_
38688 4c 45 7d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  LE};.    assert(
38689 20 70 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20   pList!=0 );.   
3868a 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e   assert( pList->
3868b 6e 45 78 70 72 3d 3d 32 20 29 3b 0a 20 20 20 20  nExpr==2 );.    
3868c 66 6f 72 28 69 3d 30 3b 20 69 3c 32 3b 20 69 2b  for(i=0; i<2; i+
3868d 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a  +){.      Expr *
3868e 70 4e 65 77 45 78 70 72 3b 0a 20 20 20 20 20 20  pNewExpr;.      
3868f 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20  int idxNew;.    
38690 20 20 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c    pNewExpr = sql
38691 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
38692 2c 20 6f 70 73 5b 69 5d 2c 20 0a 20 20 20 20 20  , ops[i], .     
38693 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38694 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
38695 78 70 72 44 75 70 28 64 62 2c 20 70 45 78 70 72  xprDup(db, pExpr
38696 2d 3e 70 4c 65 66 74 2c 20 30 29 2c 0a 20 20 20  ->pLeft, 0),.   
38697 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38698 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
38699 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4c 69  3ExprDup(db, pLi
3869a 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20  st->a[i].pExpr, 
3869b 30 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 64  0), 0);.      id
3869c 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61 75  xNew = whereClau
3869d 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70 4e  seInsert(pWC, pN
3869e 65 77 45 78 70 72 2c 20 54 45 52 4d 5f 56 49 52  ewExpr, TERM_VIR
3869f 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49  TUAL|TERM_DYNAMI
386a0 43 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  C);.      testca
386a1 73 65 28 20 69 64 78 4e 65 77 3d 3d 30 20 29 3b  se( idxNew==0 );
386a2 0a 20 20 20 20 20 20 65 78 70 72 41 6e 61 6c 79  .      exprAnaly
386a3 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64  ze(pSrc, pWC, id
386a4 78 4e 65 77 29 3b 0a 20 20 20 20 20 20 70 54 65  xNew);.      pTe
386a5 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78  rm = &pWC->a[idx
386a6 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20 70 57 43  Term];.      pWC
386a7 2d 3e 61 5b 69 64 78 4e 65 77 5d 2e 69 50 61 72  ->a[idxNew].iPar
386a8 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20  ent = idxTerm;. 
386a9 20 20 20 7d 0a 20 20 20 20 70 54 65 72 6d 2d 3e     }.    pTerm->
386aa 6e 43 68 69 6c 64 20 3d 20 32 3b 0a 20 20 7d 0a  nChild = 2;.  }.
386ab 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
386ac 5f 4f 4d 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50  _OMIT_BETWEEN_OP
386ad 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23  TIMIZATION */..#
386ae 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
386af 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d  TE_OMIT_OR_OPTIM
386b0 49 5a 41 54 49 4f 4e 29 20 26 26 20 21 64 65 66  IZATION) && !def
386b1 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
386b2 5f 53 55 42 51 55 45 52 59 29 0a 20 20 2f 2a 20  _SUBQUERY).  /* 
386b3 41 6e 61 6c 79 7a 65 20 61 20 74 65 72 6d 20 74  Analyze a term t
386b4 68 61 74 20 69 73 20 63 6f 6d 70 6f 73 65 64 20  hat is composed 
386b5 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73  of two or more s
386b6 75 62 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65  ubterms connecte
386b7 64 20 62 79 0a 20 20 2a 2a 20 61 6e 20 4f 52 20  d by.  ** an OR 
386b8 6f 70 65 72 61 74 6f 72 2e 0a 20 20 2a 2f 0a 20  operator..  */. 
386b9 20 65 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d   else if( pExpr-
386ba 3e 6f 70 3d 3d 54 4b 5f 4f 52 20 29 7b 0a 20 20  >op==TK_OR ){.  
386bb 20 20 61 73 73 65 72 74 28 20 70 57 43 2d 3e 6f    assert( pWC->o
386bc 70 3d 3d 54 4b 5f 41 4e 44 20 29 3b 0a 20 20 20  p==TK_AND );.   
386bd 20 65 78 70 72 41 6e 61 6c 79 7a 65 4f 72 54 65   exprAnalyzeOrTe
386be 72 6d 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64  rm(pSrc, pWC, id
386bf 78 54 65 72 6d 29 3b 0a 20 20 20 20 70 54 65 72  xTerm);.    pTer
386c0 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54  m = &pWC->a[idxT
386c1 65 72 6d 5d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  erm];.  }.#endif
386c2 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
386c3 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20  OR_OPTIMIZATION 
386c4 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
386c5 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54  TE_OMIT_LIKE_OPT
386c6 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 41  IMIZATION.  /* A
386c7 64 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74  dd constraints t
386c8 6f 20 72 65 64 75 63 65 20 74 68 65 20 73 65 61  o reduce the sea
386c9 72 63 68 20 73 70 61 63 65 20 6f 6e 20 61 20 4c  rch space on a L
386ca 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 20 20 2a 2a  IKE or GLOB.  **
386cb 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 2a 2a 0a   operator..  **.
386cc 20 20 2a 2a 20 41 20 6c 69 6b 65 20 70 61 74 74    ** A like patt
386cd 65 72 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ern of the form 
386ce 22 78 20 4c 49 4b 45 20 27 61 62 63 25 27 22 20  "x LIKE 'abc%'" 
386cf 69 73 20 63 68 61 6e 67 65 64 20 69 6e 74 6f 20  is changed into 
386d0 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a  constraints.  **
386d1 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 78  .  **          x
386d2 3e 3d 27 61 62 63 27 20 41 4e 44 20 78 3c 27 61  >='abc' AND x<'a
386d3 62 64 27 20 41 4e 44 20 78 20 4c 49 4b 45 20 27  bd' AND x LIKE '
386d4 61 62 63 25 27 0a 20 20 2a 2a 0a 20 20 2a 2a 20  abc%'.  **.  ** 
386d5 54 68 65 20 6c 61 73 74 20 63 68 61 72 61 63 74  The last charact
386d6 65 72 20 6f 66 20 74 68 65 20 70 72 65 66 69 78  er of the prefix
386d7 20 22 61 62 63 22 20 69 73 20 69 6e 63 72 65 6d   "abc" is increm
386d8 65 6e 74 65 64 20 74 6f 20 66 6f 72 6d 20 74 68  ented to form th
386d9 65 0a 20 20 2a 2a 20 74 65 72 6d 69 6e 61 74 69  e.  ** terminati
386da 6f 6e 20 63 6f 6e 64 69 74 69 6f 6e 20 22 61 62  on condition "ab
386db 64 22 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  d"..  */.  if( p
386dc 57 43 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e 44 20 0a  WC->op==TK_AND .
386dd 20 20 20 26 26 20 69 73 4c 69 6b 65 4f 72 47 6c     && isLikeOrGl
386de 6f 62 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ob(pParse, pExpr
386df 2c 20 26 70 53 74 72 31 2c 20 26 69 73 43 6f 6d  , &pStr1, &isCom
386e0 70 6c 65 74 65 2c 20 26 6e 6f 43 61 73 65 29 0a  plete, &noCase).
386e1 20 20 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70    ){.    Expr *p
386e2 4c 65 66 74 3b 20 20 20 20 20 20 20 2f 2a 20 4c  Left;       /* L
386e3 48 53 20 6f 66 20 4c 49 4b 45 2f 47 4c 4f 42 20  HS of LIKE/GLOB 
386e4 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20  operator */.    
386e5 45 78 70 72 20 2a 70 53 74 72 32 3b 20 20 20 20  Expr *pStr2;    
386e6 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 53     /* Copy of pS
386e7 74 72 31 20 2d 20 52 48 53 20 6f 66 20 4c 49 4b  tr1 - RHS of LIK
386e8 45 2f 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20  E/GLOB operator 
386e9 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65  */.    Expr *pNe
386ea 77 45 78 70 72 31 3b 0a 20 20 20 20 45 78 70 72  wExpr1;.    Expr
386eb 20 2a 70 4e 65 77 45 78 70 72 32 3b 0a 20 20 20   *pNewExpr2;.   
386ec 20 69 6e 74 20 69 64 78 4e 65 77 31 3b 0a 20 20   int idxNew1;.  
386ed 20 20 69 6e 74 20 69 64 78 4e 65 77 32 3b 0a 20    int idxNew2;. 
386ee 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
386ef 6c 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69  l;    /* Collati
386f0 6e 67 20 73 65 71 75 65 6e 63 65 20 74 6f 20 75  ng sequence to u
386f1 73 65 20 2a 2f 0a 0a 20 20 20 20 70 4c 65 66 74  se */..    pLeft
386f2 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73   = pExpr->x.pLis
386f3 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20  t->a[1].pExpr;. 
386f4 20 20 20 70 53 74 72 32 20 3d 20 73 71 6c 69 74     pStr2 = sqlit
386f5 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 53  e3ExprDup(db, pS
386f6 74 72 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  tr1, 0);.    if(
386f7 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
386f8 65 64 20 29 7b 0a 20 20 20 20 20 20 75 38 20 63  ed ){.      u8 c
386f9 2c 20 2a 70 43 3b 20 20 20 20 20 20 20 2f 2a 20  , *pC;       /* 
386fa 4c 61 73 74 20 63 68 61 72 61 63 74 65 72 20 62  Last character b
386fb 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 20  efore the first 
386fc 77 69 6c 64 63 61 72 64 20 2a 2f 0a 20 20 20 20  wildcard */.    
386fd 20 20 70 43 20 3d 20 28 75 38 2a 29 26 70 53 74    pC = (u8*)&pSt
386fe 72 32 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 73 71 6c  r2->u.zToken[sql
386ff 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 53 74  ite3Strlen30(pSt
38700 72 32 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2d 31 5d  r2->u.zToken)-1]
38701 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a 70 43 3b  ;.      c = *pC;
38702 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61 73  .      if( noCas
38703 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  e ){.        /* 
38704 54 68 65 20 70 6f 69 6e 74 20 69 73 20 74 6f 20  The point is to 
38705 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 6c 61  increment the la
38706 73 74 20 63 68 61 72 61 63 74 65 72 20 62 65 66  st character bef
38707 6f 72 65 20 74 68 65 20 66 69 72 73 74 0a 20 20  ore the first.  
38708 20 20 20 20 20 20 2a 2a 20 77 69 6c 64 63 61 72        ** wildcar
38709 64 2e 20 20 42 75 74 20 69 66 20 77 65 20 69 6e  d.  But if we in
3870a 63 72 65 6d 65 6e 74 20 27 40 27 2c 20 74 68 61  crement '@', tha
3870b 74 20 77 69 6c 6c 20 70 75 73 68 20 69 74 20 69  t will push it i
3870c 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20  nto the.        
3870d 2a 2a 20 61 6c 70 68 61 62 65 74 69 63 20 72 61  ** alphabetic ra
3870e 6e 67 65 20 77 68 65 72 65 20 63 61 73 65 20 63  nge where case c
3870f 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c 20  onversions will 
38710 6d 65 73 73 20 75 70 20 74 68 65 20 0a 20 20 20  mess up the .   
38711 20 20 20 20 20 2a 2a 20 69 6e 65 71 75 61 6c 69       ** inequali
38712 74 79 2e 20 20 54 6f 20 61 76 6f 69 64 20 74 68  ty.  To avoid th
38713 69 73 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 6f  is, make sure to
38714 20 61 6c 73 6f 20 72 75 6e 20 74 68 65 20 66 75   also run the fu
38715 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 4c 49  ll.        ** LI
38716 4b 45 20 6f 6e 20 61 6c 6c 20 63 61 6e 64 69 64  KE on all candid
38717 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ate expressions 
38718 62 79 20 63 6c 65 61 72 69 6e 67 20 74 68 65 20  by clearing the 
38719 69 73 43 6f 6d 70 6c 65 74 65 20 66 6c 61 67 0a  isComplete flag.
3871a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
3871b 20 20 20 69 66 28 20 63 3d 3d 27 41 27 2d 31 20     if( c=='A'-1 
3871c 29 20 69 73 43 6f 6d 70 6c 65 74 65 20 3d 20 30  ) isComplete = 0
3871d 3b 20 20 20 2f 2a 20 45 56 3a 20 52 2d 36 34 33  ;   /* EV: R-643
3871e 33 39 2d 30 38 32 30 37 20 2a 2f 0a 0a 0a 20 20  39-08207 */...  
3871f 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65        c = sqlite
38720 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 63 5d  3UpperToLower[c]
38721 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
38722 2a 70 43 20 3d 20 63 20 2b 20 31 3b 0a 20 20 20  *pC = c + 1;.   
38723 20 7d 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73   }.    pColl = s
38724 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
38725 71 28 64 62 2c 20 53 51 4c 49 54 45 5f 55 54 46  q(db, SQLITE_UTF
38726 38 2c 20 6e 6f 43 61 73 65 20 3f 20 22 4e 4f 43  8, noCase ? "NOC
38727 41 53 45 22 20 3a 20 22 42 49 4e 41 52 59 22 2c  ASE" : "BINARY",
38728 30 29 3b 0a 20 20 20 20 70 4e 65 77 45 78 70 72  0);.    pNewExpr
38729 31 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  1 = sqlite3PExpr
3872a 28 70 50 61 72 73 65 2c 20 54 4b 5f 47 45 2c 20  (pParse, TK_GE, 
3872b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3872c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
3872d 72 53 65 74 43 6f 6c 6c 28 73 71 6c 69 74 65 33  rSetColl(sqlite3
3872e 45 78 70 72 44 75 70 28 64 62 2c 70 4c 65 66 74  ExprDup(db,pLeft
3872f 2c 30 29 2c 20 70 43 6f 6c 6c 29 2c 0a 20 20 20  ,0), pColl),.   
38730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38731 20 20 70 53 74 72 31 2c 20 30 29 3b 0a 20 20 20    pStr1, 0);.   
38732 20 69 64 78 4e 65 77 31 20 3d 20 77 68 65 72 65   idxNew1 = where
38733 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
38734 2c 20 70 4e 65 77 45 78 70 72 31 2c 20 54 45 52  , pNewExpr1, TER
38735 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
38736 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 74 65 73  YNAMIC);.    tes
38737 74 63 61 73 65 28 20 69 64 78 4e 65 77 31 3d 3d  tcase( idxNew1==
38738 30 20 29 3b 0a 20 20 20 20 65 78 70 72 41 6e 61  0 );.    exprAna
38739 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20  lyze(pSrc, pWC, 
3873a 69 64 78 4e 65 77 31 29 3b 0a 20 20 20 20 70 4e  idxNew1);.    pN
3873b 65 77 45 78 70 72 32 20 3d 20 73 71 6c 69 74 65  ewExpr2 = sqlite
3873c 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
3873d 4b 5f 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20  K_LT,.          
3873e 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
3873f 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 73 71  e3ExprSetColl(sq
38740 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
38741 70 4c 65 66 74 2c 30 29 2c 20 70 43 6f 6c 6c 29  pLeft,0), pColl)
38742 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
38743 20 20 20 20 20 20 20 70 53 74 72 32 2c 20 30 29         pStr2, 0)
38744 3b 0a 20 20 20 20 69 64 78 4e 65 77 32 20 3d 20  ;.    idxNew2 = 
38745 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
38746 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72 32  t(pWC, pNewExpr2
38747 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54  , TERM_VIRTUAL|T
38748 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20  ERM_DYNAMIC);.  
38749 20 20 74 65 73 74 63 61 73 65 28 20 69 64 78 4e    testcase( idxN
3874a 65 77 32 3d 3d 30 20 29 3b 0a 20 20 20 20 65 78  ew2==0 );.    ex
3874b 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20  prAnalyze(pSrc, 
3874c 70 57 43 2c 20 69 64 78 4e 65 77 32 29 3b 0a 20  pWC, idxNew2);. 
3874d 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d     pTerm = &pWC-
3874e 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20  >a[idxTerm];.   
3874f 20 69 66 28 20 69 73 43 6f 6d 70 6c 65 74 65 20   if( isComplete 
38750 29 7b 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 5b  ){.      pWC->a[
38751 69 64 78 4e 65 77 31 5d 2e 69 50 61 72 65 6e 74  idxNew1].iParent
38752 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20   = idxTerm;.    
38753 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 32    pWC->a[idxNew2
38754 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54  ].iParent = idxT
38755 65 72 6d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d  erm;.      pTerm
38756 2d 3e 6e 43 68 69 6c 64 20 3d 20 32 3b 0a 20 20  ->nChild = 2;.  
38757 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f    }.  }.#endif /
38758 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49  * SQLITE_OMIT_LI
38759 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20  KE_OPTIMIZATION 
3875a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
3875b 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
3875c 41 42 4c 45 0a 20 20 2f 2a 20 41 64 64 20 61 20  ABLE.  /* Add a 
3875d 57 4f 5f 4d 41 54 43 48 20 61 75 78 69 6c 69 61  WO_MATCH auxilia
3875e 72 79 20 74 65 72 6d 20 74 6f 20 74 68 65 20 63  ry term to the c
3875f 6f 6e 73 74 72 61 69 6e 74 20 73 65 74 20 69 66  onstraint set if
38760 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e   the.  ** curren
38761 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  t expression is 
38762 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20 20 63 6f  of the form:  co
38763 6c 75 6d 6e 20 4d 41 54 43 48 20 65 78 70 72 2e  lumn MATCH expr.
38764 0a 20 20 2a 2a 20 54 68 69 73 20 69 6e 66 6f 72  .  ** This infor
38765 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62  mation is used b
38766 79 20 74 68 65 20 78 42 65 73 74 49 6e 64 65 78  y the xBestIndex
38767 20 6d 65 74 68 6f 64 73 20 6f 66 0a 20 20 2a 2a   methods of.  **
38768 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e   virtual tables.
38769 20 20 54 68 65 20 6e 61 74 69 76 65 20 71 75 65    The native que
3876a 72 79 20 6f 70 74 69 6d 69 7a 65 72 20 64 6f 65  ry optimizer doe
3876b 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 0a 20 20  s not attempt.  
3876c 2a 2a 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e  ** to do anythin
3876d 67 20 77 69 74 68 20 4d 41 54 43 48 20 66 75 6e  g with MATCH fun
3876e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69  ctions..  */.  i
3876f 66 28 20 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75  f( isMatchOfColu
38770 6d 6e 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20  mn(pExpr) ){.   
38771 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20   int idxNew;.   
38772 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 2a   Expr *pRight, *
38773 70 4c 65 66 74 3b 0a 20 20 20 20 57 68 65 72 65  pLeft;.    Where
38774 54 65 72 6d 20 2a 70 4e 65 77 54 65 72 6d 3b 0a  Term *pNewTerm;.
38775 20 20 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72      Bitmask prer
38776 65 71 43 6f 6c 75 6d 6e 2c 20 70 72 65 72 65 71  eqColumn, prereq
38777 45 78 70 72 3b 0a 0a 20 20 20 20 70 52 69 67 68  Expr;..    pRigh
38778 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
38779 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a  st->a[0].pExpr;.
3877a 20 20 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70      pLeft = pExp
3877b 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d  r->x.pList->a[1]
3877c 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 72 65 72  .pExpr;.    prer
3877d 65 71 45 78 70 72 20 3d 20 65 78 70 72 54 61 62  eqExpr = exprTab
3877e 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
3877f 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20 70  , pRight);.    p
38780 72 65 72 65 71 43 6f 6c 75 6d 6e 20 3d 20 65 78  rereqColumn = ex
38781 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
38782 73 6b 53 65 74 2c 20 70 4c 65 66 74 29 3b 0a 20  skSet, pLeft);. 
38783 20 20 20 69 66 28 20 28 70 72 65 72 65 71 45 78     if( (prereqEx
38784 70 72 20 26 20 70 72 65 72 65 71 43 6f 6c 75 6d  pr & prereqColum
38785 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 45  n)==0 ){.      E
38786 78 70 72 20 2a 70 4e 65 77 45 78 70 72 3b 0a 20  xpr *pNewExpr;. 
38787 20 20 20 20 20 70 4e 65 77 45 78 70 72 20 3d 20       pNewExpr = 
38788 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
38789 72 73 65 2c 20 54 4b 5f 4d 41 54 43 48 2c 20 0a  rse, TK_MATCH, .
3878a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3878b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
3878c 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
3878d 64 62 2c 20 70 52 69 67 68 74 2c 20 30 29 2c 20  db, pRight, 0), 
3878e 30 29 3b 0a 20 20 20 20 20 20 69 64 78 4e 65 77  0);.      idxNew
3878f 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e   = whereClauseIn
38790 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45 78  sert(pWC, pNewEx
38791 70 72 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  pr, TERM_VIRTUAL
38792 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a  |TERM_DYNAMIC);.
38793 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
38794 69 64 78 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20  idxNew==0 );.   
38795 20 20 20 70 4e 65 77 54 65 72 6d 20 3d 20 26 70     pNewTerm = &p
38796 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b 0a 20  WC->a[idxNew];. 
38797 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 70       pNewTerm->p
38798 72 65 72 65 71 52 69 67 68 74 20 3d 20 70 72 65  rereqRight = pre
38799 72 65 71 45 78 70 72 3b 0a 20 20 20 20 20 20 70  reqExpr;.      p
3879a 4e 65 77 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  NewTerm->leftCur
3879b 73 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61  sor = pLeft->iTa
3879c 62 6c 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 54  ble;.      pNewT
3879d 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d  erm->u.leftColum
3879e 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75  n = pLeft->iColu
3879f 6d 6e 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65  mn;.      pNewTe
387a0 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20  rm->eOperator = 
387a1 57 4f 5f 4d 41 54 43 48 3b 0a 20 20 20 20 20 20  WO_MATCH;.      
387a2 70 4e 65 77 54 65 72 6d 2d 3e 69 50 61 72 65 6e  pNewTerm->iParen
387a3 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20  t = idxTerm;.   
387a4 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d     pTerm = &pWC-
387a5 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20  >a[idxTerm];.   
387a6 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64     pTerm->nChild
387a7 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 54 65 72   = 1;.      pTer
387a8 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45  m->wtFlags |= TE
387a9 52 4d 5f 43 4f 50 49 45 44 3b 0a 20 20 20 20 20  RM_COPIED;.     
387aa 20 70 4e 65 77 54 65 72 6d 2d 3e 70 72 65 72 65   pNewTerm->prere
387ab 71 41 6c 6c 20 3d 20 70 54 65 72 6d 2d 3e 70 72  qAll = pTerm->pr
387ac 65 72 65 71 41 6c 6c 3b 0a 20 20 20 20 7d 0a 20  ereqAll;.    }. 
387ad 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
387ae 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
387af 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 64 65 66  TABLE */..#ifdef
387b0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
387b1 54 41 54 33 0a 20 20 2f 2a 20 57 68 65 6e 20 73  TAT3.  /* When s
387b2 71 6c 69 74 65 5f 73 74 61 74 33 20 68 69 73 74  qlite_stat3 hist
387b3 6f 67 72 61 6d 20 64 61 74 61 20 69 73 20 61 76  ogram data is av
387b4 61 69 6c 61 62 6c 65 20 61 6e 20 6f 70 65 72 61  ailable an opera
387b5 74 6f 72 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  tor of the.  ** 
387b6 66 6f 72 6d 20 22 78 20 49 53 20 4e 4f 54 20 4e  form "x IS NOT N
387b7 55 4c 4c 22 20 63 61 6e 20 73 6f 6d 65 74 69 6d  ULL" can sometim
387b8 65 73 20 62 65 20 65 76 61 6c 75 61 74 65 64 20  es be evaluated 
387b9 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 6c 79  more efficiently
387ba 0a 20 20 2a 2a 20 61 73 20 22 78 3e 4e 55 4c 4c  .  ** as "x>NULL
387bb 22 20 69 66 20 78 20 69 73 20 6e 6f 74 20 61 6e  " if x is not an
387bc 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
387bd 20 4b 45 59 2e 20 20 53 6f 20 63 6f 6e 73 74 72   KEY.  So constr
387be 75 63 74 20 61 0a 20 20 2a 2a 20 76 69 72 74 75  uct a.  ** virtu
387bf 61 6c 20 74 65 72 6d 20 6f 66 20 74 68 61 74 20  al term of that 
387c0 66 6f 72 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  form..  **.  ** 
387c1 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 76 69  Note that the vi
387c2 72 74 75 61 6c 20 74 65 72 6d 20 6d 75 73 74 20  rtual term must 
387c3 62 65 20 74 61 67 67 65 64 20 77 69 74 68 20 54  be tagged with T
387c4 45 52 4d 5f 56 4e 55 4c 4c 2e 20 20 54 68 69 73  ERM_VNULL.  This
387c5 0a 20 20 2a 2a 20 54 45 52 4d 5f 56 4e 55 4c 4c  .  ** TERM_VNULL
387c6 20 74 61 67 20 77 69 6c 6c 20 73 75 70 70 72 65   tag will suppre
387c7 73 73 20 74 68 65 20 6e 6f 74 2d 6e 75 6c 6c 20  ss the not-null 
387c8 63 68 65 63 6b 20 61 74 20 74 68 65 20 62 65 67  check at the beg
387c9 69 6e 6e 69 6e 67 0a 20 20 2a 2a 20 6f 66 20 74  inning.  ** of t
387ca 68 65 20 6c 6f 6f 70 2e 20 20 57 69 74 68 6f 75  he loop.  Withou
387cb 74 20 74 68 65 20 54 45 52 4d 5f 56 4e 55 4c 4c  t the TERM_VNULL
387cc 20 66 6c 61 67 2c 20 74 68 65 20 6e 6f 74 2d 6e   flag, the not-n
387cd 75 6c 6c 20 63 68 65 63 6b 20 61 74 0a 20 20 2a  ull check at.  *
387ce 2a 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  * the start of t
387cf 68 65 20 6c 6f 6f 70 20 77 69 6c 6c 20 70 72 65  he loop will pre
387d0 76 65 6e 74 20 61 6e 79 20 72 65 73 75 6c 74 73  vent any results
387d1 20 66 72 6f 6d 20 62 65 69 6e 67 20 72 65 74 75   from being retu
387d2 72 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rned..  */.  if(
387d3 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4e   pExpr->op==TK_N
387d4 4f 54 4e 55 4c 4c 0a 20 20 20 26 26 20 70 45 78  OTNULL.   && pEx
387d5 70 72 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54  pr->pLeft->op==T
387d6 4b 5f 43 4f 4c 55 4d 4e 0a 20 20 20 26 26 20 70  K_COLUMN.   && p
387d7 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 69 43 6f  Expr->pLeft->iCo
387d8 6c 75 6d 6e 3e 3d 30 0a 20 20 29 7b 0a 20 20 20  lumn>=0.  ){.   
387d9 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 3b   Expr *pNewExpr;
387da 0a 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74  .    Expr *pLeft
387db 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b   = pExpr->pLeft;
387dc 0a 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b  .    int idxNew;
387dd 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a  .    WhereTerm *
387de 70 4e 65 77 54 65 72 6d 3b 0a 0a 20 20 20 20 70  pNewTerm;..    p
387df 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65  NewExpr = sqlite
387e0 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
387e1 4b 5f 47 54 2c 0a 20 20 20 20 20 20 20 20 20 20  K_GT,.          
387e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
387e3 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70    sqlite3ExprDup
387e4 28 64 62 2c 20 70 4c 65 66 74 2c 20 30 29 2c 0a  (db, pLeft, 0),.
387e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
387e6 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
387e7 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
387e8 20 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20   TK_NULL, 0, 0, 
387e9 30 29 2c 20 30 29 3b 0a 0a 20 20 20 20 69 64 78  0), 0);..    idx
387ea 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61 75 73  New = whereClaus
387eb 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65  eInsert(pWC, pNe
387ec 77 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20  wExpr,.         
387ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
387ee 20 20 20 20 20 54 45 52 4d 5f 56 49 52 54 55 41       TERM_VIRTUA
387ef 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 7c 54  L|TERM_DYNAMIC|T
387f0 45 52 4d 5f 56 4e 55 4c 4c 29 3b 0a 20 20 20 20  ERM_VNULL);.    
387f1 69 66 28 20 69 64 78 4e 65 77 20 29 7b 0a 20 20  if( idxNew ){.  
387f2 20 20 20 20 70 4e 65 77 54 65 72 6d 20 3d 20 26      pNewTerm = &
387f3 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b 0a  pWC->a[idxNew];.
387f4 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e        pNewTerm->
387f5 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 30 3b  prereqRight = 0;
387f6 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d  .      pNewTerm-
387f7 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 70 4c  >leftCursor = pL
387f8 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20  eft->iTable;.   
387f9 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 75 2e 6c     pNewTerm->u.l
387fa 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66  eftColumn = pLef
387fb 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  t->iColumn;.    
387fc 20 20 70 4e 65 77 54 65 72 6d 2d 3e 65 4f 70 65    pNewTerm->eOpe
387fd 72 61 74 6f 72 20 3d 20 57 4f 5f 47 54 3b 0a 20  rator = WO_GT;. 
387fe 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 69       pNewTerm->i
387ff 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d  Parent = idxTerm
38800 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20  ;.      pTerm = 
38801 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d  &pWC->a[idxTerm]
38802 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e  ;.      pTerm->n
38803 43 68 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20  Child = 1;.     
38804 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
38805 7c 3d 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b 0a  |= TERM_COPIED;.
38806 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e        pNewTerm->
38807 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 54 65 72  prereqAll = pTer
38808 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 3b 0a 20 20  m->prereqAll;.  
38809 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f    }.  }.#endif /
3880a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
3880b 53 54 41 54 20 2a 2f 0a 0a 20 20 2f 2a 20 50 72  STAT */..  /* Pr
3880c 65 76 65 6e 74 20 4f 4e 20 63 6c 61 75 73 65 20  event ON clause 
3880d 74 65 72 6d 73 20 6f 66 20 61 20 4c 45 46 54 20  terms of a LEFT 
3880e 4a 4f 49 4e 20 66 72 6f 6d 20 62 65 69 6e 67 20  JOIN from being 
3880f 75 73 65 64 20 74 6f 20 64 72 69 76 65 0a 20 20  used to drive.  
38810 2a 2a 20 61 6e 20 69 6e 64 65 78 20 66 6f 72 20  ** an index for 
38811 74 61 62 6c 65 73 20 74 6f 20 74 68 65 20 6c 65  tables to the le
38812 66 74 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 0a  ft of the join..
38813 20 20 2a 2f 0a 20 20 70 54 65 72 6d 2d 3e 70 72    */.  pTerm->pr
38814 65 72 65 71 52 69 67 68 74 20 7c 3d 20 65 78 74  ereqRight |= ext
38815 72 61 52 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  raRight;.}../*.*
38816 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
38817 73 65 61 72 63 68 65 73 20 74 68 65 20 65 78 70  searches the exp
38818 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 70 61 73  ression list pas
38819 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
3881a 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 66 6f  d argument.** fo
3881b 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  r an expression 
3881c 6f 66 20 74 79 70 65 20 54 4b 5f 43 4f 4c 55 4d  of type TK_COLUM
3881d 4e 20 74 68 61 74 20 72 65 66 65 72 73 20 74 6f  N that refers to
3881e 20 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e   the same column
3881f 20 61 6e 64 0a 2a 2a 20 75 73 65 73 20 74 68 65   and.** uses the
38820 20 73 61 6d 65 20 63 6f 6c 6c 61 74 69 6f 6e 20   same collation 
38821 73 65 71 75 65 6e 63 65 20 61 73 20 74 68 65 20  sequence as the 
38822 69 43 6f 6c 27 74 68 20 63 6f 6c 75 6d 6e 20 6f  iCol'th column o
38823 66 20 69 6e 64 65 78 20 70 49 64 78 2e 0a 2a 2a  f index pIdx..**
38824 20 41 72 67 75 6d 65 6e 74 20 69 42 61 73 65 20   Argument iBase 
38825 69 73 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75  is the cursor nu
38826 6d 62 65 72 20 75 73 65 64 20 66 6f 72 20 74 68  mber used for th
38827 65 20 74 61 62 6c 65 20 74 68 61 74 20 70 49 64  e table that pId
38828 78 20 72 65 66 65 72 73 0a 2a 2a 20 74 6f 2e 0a  x refers.** to..
38829 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 68 20 61 6e  **.** If such an
3882a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 66   expression is f
3882b 6f 75 6e 64 2c 20 69 74 73 20 69 6e 64 65 78 20  ound, its index 
3882c 69 6e 20 70 4c 69 73 74 2d 3e 61 5b 5d 20 69 73  in pList->a[] is
3882d 20 72 65 74 75 72 6e 65 64 2e 20 49 66 0a 2a 2a   returned. If.**
3882e 20 6e 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 69   no expression i
3882f 73 20 66 6f 75 6e 64 2c 20 2d 31 20 69 73 20 72  s found, -1 is r
38830 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
38831 69 63 20 69 6e 74 20 66 69 6e 64 49 6e 64 65 78  ic int findIndex
38832 43 6f 6c 28 0a 20 20 50 61 72 73 65 20 2a 70 50  Col(.  Parse *pP
38833 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
38834 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20         /* Parse 
38835 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
38836 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20  rList *pList,   
38837 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38838 45 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20  Expression list 
38839 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 69  to search */.  i
3883a 6e 74 20 69 42 61 73 65 2c 20 20 20 20 20 20 20  nt iBase,       
3883b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3883c 2a 20 43 75 72 73 6f 72 20 66 6f 72 20 74 61 62  * Cursor for tab
3883d 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  le associated wi
3883e 74 68 20 70 49 64 78 20 2a 2f 0a 20 20 49 6e 64  th pIdx */.  Ind
3883f 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20  ex *pIdx,       
38840 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38841 49 6e 64 65 78 20 74 6f 20 6d 61 74 63 68 20 63  Index to match c
38842 6f 6c 75 6d 6e 20 6f 66 20 2a 2f 0a 20 20 69 6e  olumn of */.  in
38843 74 20 69 43 6f 6c 20 20 20 20 20 20 20 20 20 20  t iCol          
38844 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38845 20 43 6f 6c 75 6d 6e 20 6f 66 20 69 6e 64 65 78   Column of index
38846 20 74 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 7b 0a   to match */.){.
38847 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74    int i;.  const
38848 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 70   char *zColl = p
38849 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 43 6f 6c  Idx->azColl[iCol
3884a 5d 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  ];..  for(i=0; i
3884b 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  <pList->nExpr; i
3884c 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  ++){.    Expr *p
3884d 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70   = pList->a[i].p
3884e 45 78 70 72 3b 0a 20 20 20 20 69 66 28 20 70 2d  Expr;.    if( p-
3884f 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 0a 20  >op==TK_COLUMN. 
38850 20 20 20 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d      && p->iColum
38851 6e 3d 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d  n==pIdx->aiColum
38852 6e 5b 69 43 6f 6c 5d 0a 20 20 20 20 20 26 26 20  n[iCol].     && 
38853 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 42 61 73 65  p->iTable==iBase
38854 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 43 6f  .    ){.      Co
38855 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73  llSeq *pColl = s
38856 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
38857 71 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20  q(pParse, p);.  
38858 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70      if( ALWAYS(p
38859 43 6f 6c 6c 29 20 26 26 20 30 3d 3d 73 71 6c 69  Coll) && 0==sqli
3885a 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 6c  te3StrICmp(pColl
3885b 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 6c 29 20  ->zName, zColl) 
3885c 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
3885d 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n i;.      }.   
3885e 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
3885f 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   -1;.}../*.** Th
38860 69 73 20 72 6f 75 74 69 6e 65 20 64 65 74 65 72  is routine deter
38861 6d 69 6e 65 73 20 69 66 20 70 49 64 78 20 63 61  mines if pIdx ca
38862 6e 20 62 65 20 75 73 65 64 20 74 6f 20 61 73 73  n be used to ass
38863 69 73 74 20 69 6e 20 70 72 6f 63 65 73 73 69 6e  ist in processin
38864 67 20 61 0a 2a 2a 20 44 49 53 54 49 4e 43 54 20  g a.** DISTINCT 
38865 71 75 61 6c 69 66 69 65 72 2e 20 49 6e 20 6f 74  qualifier. In ot
38866 68 65 72 20 77 6f 72 64 73 2c 20 69 74 20 74 65  her words, it te
38867 73 74 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e  sts whether or n
38868 6f 74 20 75 73 69 6e 67 20 74 68 69 73 0a 2a 2a  ot using this.**
38869 20 69 6e 64 65 78 20 66 6f 72 20 74 68 65 20 6f   index for the o
3886a 75 74 65 72 20 6c 6f 6f 70 20 67 75 61 72 61 6e  uter loop guaran
3886b 74 65 65 73 20 74 68 61 74 20 72 6f 77 73 20 77  tees that rows w
3886c 69 74 68 20 65 71 75 61 6c 20 76 61 6c 75 65 73  ith equal values
3886d 20 66 6f 72 0a 2a 2a 20 61 6c 6c 20 65 78 70 72   for.** all expr
3886e 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 70  essions in the p
3886f 44 69 73 74 69 6e 63 74 20 6c 69 73 74 20 61 72  Distinct list ar
38870 65 20 64 65 6c 69 76 65 72 65 64 20 67 72 6f 75  e delivered grou
38871 70 65 64 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2a  ped together..**
38872 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  .** For example,
38873 20 74 68 65 20 71 75 65 72 79 20 0a 2a 2a 0a 2a   the query .**.*
38874 2a 20 20 20 53 45 4c 45 43 54 20 44 49 53 54 49  *   SELECT DISTI
38875 4e 43 54 20 61 2c 20 62 2c 20 63 20 46 52 4f 4d  NCT a, b, c FROM
38876 20 74 62 6c 20 57 48 45 52 45 20 61 20 3d 20 3f   tbl WHERE a = ?
38877 0a 2a 2a 0a 2a 2a 20 63 61 6e 20 62 65 6e 65 66  .**.** can benef
38878 69 74 20 66 72 6f 6d 20 61 6e 79 20 69 6e 64 65  it from any inde
38879 78 20 6f 6e 20 63 6f 6c 75 6d 6e 73 20 22 62 22  x on columns "b"
3887a 20 61 6e 64 20 22 63 22 2e 0a 2a 2f 0a 73 74 61   and "c"..*/.sta
3887b 74 69 63 20 69 6e 74 20 69 73 44 69 73 74 69 6e  tic int isDistin
3887c 63 74 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65  ctIndex(.  Parse
3887d 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
3887e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
3887f 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
38880 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
38881 70 57 43 2c 20 20 20 20 20 20 20 20 20 20 20 20  pWC,            
38882 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
38883 63 6c 61 75 73 65 20 2a 2f 0a 20 20 49 6e 64 65  clause */.  Inde
38884 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20 20  x *pIdx,        
38885 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
38886 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 63  he index being c
38887 6f 6e 73 69 64 65 72 65 64 20 2a 2f 0a 20 20 69  onsidered */.  i
38888 6e 74 20 62 61 73 65 2c 20 20 20 20 20 20 20 20  nt base,        
38889 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3888a 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * Cursor number 
3888b 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 70 49  for the table pI
3888c 64 78 20 69 73 20 6f 6e 20 2a 2f 0a 20 20 45 78  dx is on */.  Ex
3888d 70 72 4c 69 73 74 20 2a 70 44 69 73 74 69 6e 63  prList *pDistinc
3888e 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t,            /*
3888f 20 54 68 65 20 44 49 53 54 49 4e 43 54 20 65 78   The DISTINCT ex
38890 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69  pressions */.  i
38891 6e 74 20 6e 45 71 43 6f 6c 20 20 20 20 20 20 20  nt nEqCol       
38892 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38893 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e 64 65  * Number of inde
38894 78 20 63 6f 6c 75 6d 6e 73 20 77 69 74 68 20 3d  x columns with =
38895 3d 20 2a 2f 0a 29 7b 0a 20 20 42 69 74 6d 61 73  = */.){.  Bitmas
38896 6b 20 6d 61 73 6b 20 3d 20 30 3b 20 20 20 20 20  k mask = 0;     
38897 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73            /* Mas
38898 6b 20 6f 66 20 75 6e 61 63 63 6f 75 6e 74 65 64  k of unaccounted
38899 20 66 6f 72 20 70 44 69 73 74 69 6e 63 74 20 65   for pDistinct e
3889a 78 70 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  xprs */.  int i;
3889b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3889c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
3889d 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
3889e 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 44 69  /..  assert( pDi
3889f 73 74 69 6e 63 74 21 3d 30 20 29 3b 0a 20 20 69  stinct!=0 );.  i
388a0 66 28 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 3d 3d  f( pIdx->zName==
388a1 30 20 7c 7c 20 70 44 69 73 74 69 6e 63 74 2d 3e  0 || pDistinct->
388a2 6e 45 78 70 72 3e 3d 42 4d 53 20 29 20 72 65 74  nExpr>=BMS ) ret
388a3 75 72 6e 20 30 3b 0a 20 20 74 65 73 74 63 61 73  urn 0;.  testcas
388a4 65 28 20 70 44 69 73 74 69 6e 63 74 2d 3e 6e 45  e( pDistinct->nE
388a5 78 70 72 3d 3d 42 4d 53 2d 31 20 29 3b 0a 0a 20  xpr==BMS-1 );.. 
388a6 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
388a7 20 61 6c 6c 20 74 68 65 20 65 78 70 72 65 73 73   all the express
388a8 69 6f 6e 73 20 69 6e 20 74 68 65 20 64 69 73 74  ions in the dist
388a9 69 6e 63 74 20 6c 69 73 74 2e 20 49 66 20 61 6e  inct list. If an
388aa 79 20 6f 66 20 74 68 65 6d 0a 20 20 2a 2a 20 61  y of them.  ** a
388ab 72 65 20 6e 6f 74 20 73 69 6d 70 6c 65 20 63 6f  re not simple co
388ac 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 73 2c  lumn references,
388ad 20 72 65 74 75 72 6e 20 65 61 72 6c 79 2e 20 4f   return early. O
388ae 74 68 65 72 77 69 73 65 2c 20 74 65 73 74 20 69  therwise, test i
388af 66 20 74 68 65 0a 20 20 2a 2a 20 57 48 45 52 45  f the.  ** WHERE
388b0 20 63 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e 73   clause contains
388b1 20 61 20 22 63 6f 6c 3d 58 22 20 63 6c 61 75 73   a "col=X" claus
388b2 65 2e 20 49 66 20 69 74 20 64 6f 65 73 2c 20 74  e. If it does, t
388b3 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20  he expression.  
388b4 2a 2a 20 63 61 6e 20 62 65 20 69 67 6e 6f 72 65  ** can be ignore
388b5 64 2e 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f  d. If it does no
388b6 74 2c 20 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d  t, and the colum
388b7 6e 20 64 6f 65 73 20 6e 6f 74 20 62 65 6c 6f 6e  n does not belon
388b8 67 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 73 61  g to the.  ** sa
388b9 6d 65 20 74 61 62 6c 65 20 61 73 20 69 6e 64 65  me table as inde
388ba 78 20 70 49 64 78 2c 20 72 65 74 75 72 6e 20 65  x pIdx, return e
388bb 61 72 6c 79 2e 20 46 69 6e 61 6c 6c 79 2c 20 69  arly. Finally, i
388bc 66 20 74 68 65 72 65 20 69 73 20 6e 6f 0a 20 20  f there is no.  
388bd 2a 2a 20 6d 61 74 63 68 69 6e 67 20 22 63 6f 6c  ** matching "col
388be 3d 58 22 20 65 78 70 72 65 73 73 69 6f 6e 20 61  =X" expression a
388bf 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73  nd the column is
388c0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62   on the same tab
388c1 6c 65 20 61 73 20 70 49 64 78 2c 0a 20 20 2a 2a  le as pIdx,.  **
388c2 20 73 65 74 20 74 68 65 20 63 6f 72 72 65 73 70   set the corresp
388c3 6f 6e 64 69 6e 67 20 62 69 74 20 69 6e 20 76 61  onding bit in va
388c4 72 69 61 62 6c 65 20 6d 61 73 6b 2e 0a 20 20 2a  riable mask..  *
388c5 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  /.  for(i=0; i<p
388c6 44 69 73 74 69 6e 63 74 2d 3e 6e 45 78 70 72 3b  Distinct->nExpr;
388c7 20 69 2b 2b 29 7b 0a 20 20 20 20 57 68 65 72 65   i++){.    Where
388c8 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 20  Term *pTerm;.   
388c9 20 45 78 70 72 20 2a 70 20 3d 20 70 44 69 73 74   Expr *p = pDist
388ca 69 6e 63 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  inct->a[i].pExpr
388cb 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6f 70 21  ;.    if( p->op!
388cc 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74  =TK_COLUMN ) ret
388cd 75 72 6e 20 30 3b 0a 20 20 20 20 70 54 65 72 6d  urn 0;.    pTerm
388ce 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c   = findTerm(pWC,
388cf 20 70 2d 3e 69 54 61 62 6c 65 2c 20 70 2d 3e 69   p->iTable, p->i
388d0 43 6f 6c 75 6d 6e 2c 20 7e 28 42 69 74 6d 61 73  Column, ~(Bitmas
388d1 6b 29 30 2c 20 57 4f 5f 45 51 2c 20 30 29 3b 0a  k)0, WO_EQ, 0);.
388d2 20 20 20 20 69 66 28 20 70 54 65 72 6d 20 29 7b      if( pTerm ){
388d3 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 20  .      Expr *pX 
388d4 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a  = pTerm->pExpr;.
388d5 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
388d6 31 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72  1 = sqlite3Binar
388d7 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28  yCompareCollSeq(
388d8 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c 65 66  pParse, pX->pLef
388d9 74 2c 20 70 58 2d 3e 70 52 69 67 68 74 29 3b 0a  t, pX->pRight);.
388da 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
388db 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  2 = sqlite3ExprC
388dc 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
388dd 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 31 3d  );.      if( p1=
388de 3d 70 32 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  =p2 ) continue;.
388df 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d      }.    if( p-
388e0 3e 69 54 61 62 6c 65 21 3d 62 61 73 65 20 29 20  >iTable!=base ) 
388e1 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 6d 61  return 0;.    ma
388e2 73 6b 20 7c 3d 20 28 28 28 42 69 74 6d 61 73 6b  sk |= (((Bitmask
388e3 29 31 29 20 3c 3c 20 69 29 3b 0a 20 20 7d 0a 0a  )1) << i);.  }..
388e4 20 20 66 6f 72 28 69 3d 6e 45 71 43 6f 6c 3b 20    for(i=nEqCol; 
388e5 6d 61 73 6b 20 26 26 20 69 3c 70 49 64 78 2d 3e  mask && i<pIdx->
388e6 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
388e7 20 20 20 69 6e 74 20 69 45 78 70 72 20 3d 20 66     int iExpr = f
388e8 69 6e 64 49 6e 64 65 78 43 6f 6c 28 70 50 61 72  indIndexCol(pPar
388e9 73 65 2c 20 70 44 69 73 74 69 6e 63 74 2c 20 62  se, pDistinct, b
388ea 61 73 65 2c 20 70 49 64 78 2c 20 69 29 3b 0a 20  ase, pIdx, i);. 
388eb 20 20 20 69 66 28 20 69 45 78 70 72 3c 30 20 29     if( iExpr<0 )
388ec 20 62 72 65 61 6b 3b 0a 20 20 20 20 6d 61 73 6b   break;.    mask
388ed 20 26 3d 20 7e 28 28 28 42 69 74 6d 61 73 6b 29   &= ~(((Bitmask)
388ee 31 29 20 3c 3c 20 69 45 78 70 72 29 3b 0a 20 20  1) << iExpr);.  
388ef 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 6d 61 73  }..  return (mas
388f0 6b 3d 3d 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  k==0);.}.../*.**
388f1 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
388f2 74 68 65 20 44 49 53 54 49 4e 43 54 20 65 78 70  the DISTINCT exp
388f3 72 65 73 73 69 6f 6e 2d 6c 69 73 74 20 70 61 73  ression-list pas
388f4 73 65 64 20 61 73 20 74 68 65 20 74 68 69 72 64  sed as the third
388f5 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20   argument.** is 
388f6 72 65 64 75 6e 64 61 6e 74 2e 20 41 20 44 49 53  redundant. A DIS
388f7 54 49 4e 43 54 20 6c 69 73 74 20 69 73 20 72 65  TINCT list is re
388f8 64 75 6e 64 61 6e 74 20 69 66 20 74 68 65 20 64  dundant if the d
388f9 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 73  atabase contains
388fa 20 61 0a 2a 2a 20 55 4e 49 51 55 45 20 69 6e 64   a.** UNIQUE ind
388fb 65 78 20 74 68 61 74 20 67 75 61 72 61 6e 74 65  ex that guarante
388fc 65 73 20 74 68 61 74 20 74 68 65 20 72 65 73 75  es that the resu
388fd 6c 74 20 6f 66 20 74 68 65 20 71 75 65 72 79 20  lt of the query 
388fe 77 69 6c 6c 20 62 65 20 64 69 73 74 69 6e 63 74  will be distinct
388ff 0a 2a 2a 20 61 6e 79 77 61 79 2e 0a 2a 2f 0a 73  .** anyway..*/.s
38900 74 61 74 69 63 20 69 6e 74 20 69 73 44 69 73 74  tatic int isDist
38901 69 6e 63 74 52 65 64 75 6e 64 61 6e 74 28 0a 20  inctRedundant(. 
38902 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a   Parse *pParse,.
38903 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
38904 69 73 74 2c 0a 20 20 57 68 65 72 65 43 6c 61 75  ist,.  WhereClau
38905 73 65 20 2a 70 57 43 2c 0a 20 20 45 78 70 72 4c  se *pWC,.  ExprL
38906 69 73 74 20 2a 70 44 69 73 74 69 6e 63 74 0a 29  ist *pDistinct.)
38907 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  {.  Table *pTab;
38908 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a  .  Index *pIdx;.
38909 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
3890a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3890b 20 20 0a 20 20 69 6e 74 20 69 42 61 73 65 3b 0a    .  int iBase;.
3890c 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69  .  /* If there i
3890d 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20  s more than one 
3890e 74 61 62 6c 65 20 6f 72 20 73 75 62 2d 73 65 6c  table or sub-sel
3890f 65 63 74 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ect in the FROM 
38910 63 6c 61 75 73 65 20 6f 66 0a 20 20 2a 2a 20 74  clause of.  ** t
38911 68 69 73 20 71 75 65 72 79 2c 20 74 68 65 6e 20  his query, then 
38912 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 70  it will not be p
38913 6f 73 73 69 62 6c 65 20 74 6f 20 73 68 6f 77 20  ossible to show 
38914 74 68 61 74 20 74 68 65 20 44 49 53 54 49 4e 43  that the DISTINC
38915 54 20 0a 20 20 2a 2a 20 63 6c 61 75 73 65 20 69  T .  ** clause i
38916 73 20 72 65 64 75 6e 64 61 6e 74 2e 20 2a 2f 0a  s redundant. */.
38917 20 20 69 66 28 20 70 54 61 62 4c 69 73 74 2d 3e    if( pTabList->
38918 6e 53 72 63 21 3d 31 20 29 20 72 65 74 75 72 6e  nSrc!=1 ) return
38919 20 30 3b 0a 20 20 69 42 61 73 65 20 3d 20 70 54   0;.  iBase = pT
3891a 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75  abList->a[0].iCu
3891b 72 73 6f 72 3b 0a 20 20 70 54 61 62 20 3d 20 70  rsor;.  pTab = p
3891c 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 54  TabList->a[0].pT
3891d 61 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79  ab;..  /* If any
3891e 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
3891f 6f 6e 73 20 69 73 20 61 6e 20 49 50 4b 20 63 6f  ons is an IPK co
38920 6c 75 6d 6e 20 6f 6e 20 74 61 62 6c 65 20 69 42  lumn on table iB
38921 61 73 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e  ase, then return
38922 20 0a 20 20 2a 2a 20 74 72 75 65 2e 20 4e 6f 74   .  ** true. Not
38923 65 3a 20 54 68 65 20 28 70 2d 3e 69 54 61 62 6c  e: The (p->iTabl
38924 65 3d 3d 69 42 61 73 65 29 20 70 61 72 74 20 6f  e==iBase) part o
38925 66 20 74 68 69 73 20 74 65 73 74 20 6d 61 79 20  f this test may 
38926 62 65 20 66 61 6c 73 65 20 69 66 20 74 68 65 0a  be false if the.
38927 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 53 45 4c    ** current SEL
38928 45 43 54 20 69 73 20 61 20 63 6f 72 72 65 6c 61  ECT is a correla
38929 74 65 64 20 73 75 62 2d 71 75 65 72 79 2e 0a 20  ted sub-query.. 
3892a 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   */.  for(i=0; i
3892b 3c 70 44 69 73 74 69 6e 63 74 2d 3e 6e 45 78 70  <pDistinct->nExp
3892c 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70  r; i++){.    Exp
3892d 72 20 2a 70 20 3d 20 70 44 69 73 74 69 6e 63 74  r *p = pDistinct
3892e 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
3892f 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f    if( p->op==TK_
38930 43 4f 4c 55 4d 4e 20 26 26 20 70 2d 3e 69 54 61  COLUMN && p->iTa
38931 62 6c 65 3d 3d 69 42 61 73 65 20 26 26 20 70 2d  ble==iBase && p-
38932 3e 69 43 6f 6c 75 6d 6e 3c 30 20 29 20 72 65 74  >iColumn<0 ) ret
38933 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 1;.  }..  /*
38934 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 61 6c   Loop through al
38935 6c 20 69 6e 64 69 63 65 73 20 6f 6e 20 74 68 65  l indices on the
38936 20 74 61 62 6c 65 2c 20 63 68 65 63 6b 69 6e 67   table, checking
38937 20 65 61 63 68 20 74 6f 20 73 65 65 20 69 66 20   each to see if 
38938 69 74 20 6d 61 6b 65 73 0a 20 20 2a 2a 20 74 68  it makes.  ** th
38939 65 20 44 49 53 54 49 4e 43 54 20 71 75 61 6c 69  e DISTINCT quali
3893a 66 69 65 72 20 72 65 64 75 6e 64 61 6e 74 2e 20  fier redundant. 
3893b 49 74 20 64 6f 65 73 20 73 6f 20 69 66 3a 0a 20  It does so if:. 
3893c 20 2a 2a 0a 20 20 2a 2a 20 20 20 31 2e 20 54 68   **.  **   1. Th
3893d 65 20 69 6e 64 65 78 20 69 73 20 69 74 73 65 6c  e index is itsel
3893e 66 20 55 4e 49 51 55 45 2c 20 61 6e 64 0a 20 20  f UNIQUE, and.  
3893f 2a 2a 0a 20 20 2a 2a 20 20 20 32 2e 20 41 6c 6c  **.  **   2. All
38940 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20   of the columns 
38941 69 6e 20 74 68 65 20 69 6e 64 65 78 20 61 72 65  in the index are
38942 20 65 69 74 68 65 72 20 70 61 72 74 20 6f 66 20   either part of 
38943 74 68 65 20 70 44 69 73 74 69 6e 63 74 0a 20 20  the pDistinct.  
38944 2a 2a 20 20 20 20 20 20 6c 69 73 74 2c 20 6f 72  **      list, or
38945 20 65 6c 73 65 20 74 68 65 20 57 48 45 52 45 20   else the WHERE 
38946 63 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e 73 20  clause contains 
38947 61 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f  a term of the fo
38948 72 6d 20 22 63 6f 6c 3d 58 22 2c 0a 20 20 2a 2a  rm "col=X",.  **
38949 20 20 20 20 20 20 77 68 65 72 65 20 58 20 69 73        where X is
3894a 20 61 20 63 6f 6e 73 74 61 6e 74 20 76 61 6c 75   a constant valu
3894b 65 2e 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  e. The collation
3894c 20 73 65 71 75 65 6e 63 65 73 20 6f 66 20 74 68   sequences of th
3894d 65 0a 20 20 2a 2a 20 20 20 20 20 20 63 6f 6d 70  e.  **      comp
3894e 61 72 69 73 6f 6e 20 61 6e 64 20 73 65 6c 65 63  arison and selec
3894f 74 2d 6c 69 73 74 20 65 78 70 72 65 73 73 69 6f  t-list expressio
38950 6e 73 20 6d 75 73 74 20 6d 61 74 63 68 20 74 68  ns must match th
38951 6f 73 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ose of the index
38952 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 33 2e  ..  **.  **   3.
38953 20 41 6c 6c 20 6f 66 20 74 68 6f 73 65 20 69 6e   All of those in
38954 64 65 78 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20  dex columns for 
38955 77 68 69 63 68 20 74 68 65 20 57 48 45 52 45 20  which the WHERE 
38956 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 0a  clause does not.
38957 20 20 2a 2a 20 20 20 20 20 20 63 6f 6e 74 61 69    **      contai
38958 6e 20 61 20 22 63 6f 6c 3d 58 22 20 74 65 72 6d  n a "col=X" term
38959 20 61 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20   are subject to 
3895a 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74  a NOT NULL const
3895b 72 61 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f  raint..  */.  fo
3895c 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
3895d 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
3895e 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
3895f 20 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72    if( pIdx->onEr
38960 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 29 20 63  ror==OE_None ) c
38961 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 6f 72  ontinue;.    for
38962 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43  (i=0; i<pIdx->nC
38963 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  olumn; i++){.   
38964 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 49     int iCol = pI
38965 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b  dx->aiColumn[i];
38966 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 66 69  .      if( 0==fi
38967 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 42 61 73  ndTerm(pWC, iBas
38968 65 2c 20 69 43 6f 6c 2c 20 7e 28 42 69 74 6d 61  e, iCol, ~(Bitma
38969 73 6b 29 30 2c 20 57 4f 5f 45 51 2c 20 70 49 64  sk)0, WO_EQ, pId
3896a 78 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  x) ){.        in
3896b 74 20 69 49 64 78 43 6f 6c 20 3d 20 66 69 6e 64  t iIdxCol = find
3896c 49 6e 64 65 78 43 6f 6c 28 70 50 61 72 73 65 2c  IndexCol(pParse,
3896d 20 70 44 69 73 74 69 6e 63 74 2c 20 69 42 61 73   pDistinct, iBas
3896e 65 2c 20 70 49 64 78 2c 20 69 29 3b 0a 20 20 20  e, pIdx, i);.   
3896f 20 20 20 20 20 69 66 28 20 69 49 64 78 43 6f 6c       if( iIdxCol
38970 3c 30 20 7c 7c 20 70 54 61 62 2d 3e 61 43 6f 6c  <0 || pTab->aCol
38971 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  [pIdx->aiColumn[
38972 69 5d 5d 2e 6e 6f 74 4e 75 6c 6c 3d 3d 30 20 29  i]].notNull==0 )
38973 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  {.          brea
38974 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
38975 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
38976 66 28 20 69 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c  f( i==pIdx->nCol
38977 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  umn ){.      /* 
38978 54 68 69 73 20 69 6e 64 65 78 20 69 6d 70 6c 69  This index impli
38979 65 73 20 74 68 61 74 20 74 68 65 20 44 49 53 54  es that the DIST
3897a 49 4e 43 54 20 71 75 61 6c 69 66 69 65 72 20 69  INCT qualifier i
3897b 73 20 72 65 64 75 6e 64 61 6e 74 2e 20 2a 2f 0a  s redundant. */.
3897c 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
3897d 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
3897e 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
3897f 50 72 65 70 61 72 65 20 61 20 63 72 75 64 65 20  Prepare a crude 
38980 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20  estimate of the 
38981 6c 6f 67 61 72 69 74 68 6d 20 6f 66 20 74 68 65  logarithm of the
38982 20 69 6e 70 75 74 20 76 61 6c 75 65 2e 0a 2a 2a   input value..**
38983 20 54 68 65 20 72 65 73 75 6c 74 73 20 6e 65 65   The results nee
38984 64 20 6e 6f 74 20 62 65 20 65 78 61 63 74 2e 20  d not be exact. 
38985 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73   This is only us
38986 65 64 20 66 6f 72 20 65 73 74 69 6d 61 74 69 6e  ed for estimatin
38987 67 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 63  g.** the total c
38988 6f 73 74 20 6f 66 20 70 65 72 66 6f 72 6d 69 6e  ost of performin
38989 67 20 6f 70 65 72 61 74 69 6f 6e 73 20 77 69 74  g operations wit
3898a 68 20 4f 28 6c 6f 67 4e 29 20 6f 72 20 4f 28 4e  h O(logN) or O(N
3898b 6c 6f 67 4e 29 0a 2a 2a 20 63 6f 6d 70 6c 65 78  logN).** complex
3898c 69 74 79 2e 20 20 42 65 63 61 75 73 65 20 4e 20  ity.  Because N 
3898d 69 73 20 6a 75 73 74 20 61 20 67 75 65 73 73 2c  is just a guess,
3898e 20 69 74 20 69 73 20 6e 6f 20 67 72 65 61 74 20   it is no great 
3898f 74 72 61 67 65 64 79 20 69 66 0a 2a 2a 20 6c 6f  tragedy if.** lo
38990 67 4e 20 69 73 20 61 20 6c 69 74 74 6c 65 20 6f  gN is a little o
38991 66 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 64 6f  ff..*/.static do
38992 75 62 6c 65 20 65 73 74 4c 6f 67 28 64 6f 75 62  uble estLog(doub
38993 6c 65 20 4e 29 7b 0a 20 20 64 6f 75 62 6c 65 20  le N){.  double 
38994 6c 6f 67 4e 20 3d 20 31 3b 0a 20 20 64 6f 75 62  logN = 1;.  doub
38995 6c 65 20 78 20 3d 20 31 30 3b 0a 20 20 77 68 69  le x = 10;.  whi
38996 6c 65 28 20 4e 3e 78 20 29 7b 0a 20 20 20 20 6c  le( N>x ){.    l
38997 6f 67 4e 20 2b 3d 20 31 3b 0a 20 20 20 20 78 20  ogN += 1;.    x 
38998 2a 3d 20 31 30 3b 0a 20 20 7d 0a 20 20 72 65 74  *= 10;.  }.  ret
38999 75 72 6e 20 6c 6f 67 4e 3b 0a 7d 0a 0a 2f 2a 0a  urn logN;.}../*.
3899a 2a 2a 20 54 77 6f 20 72 6f 75 74 69 6e 65 73 20  ** Two routines 
3899b 66 6f 72 20 70 72 69 6e 74 69 6e 67 20 74 68 65  for printing the
3899c 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 73   content of an s
3899d 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
3899e 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20  o.** structure. 
3899f 20 55 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   Used for testin
389a0 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20  g and debugging 
389a1 6f 6e 6c 79 2e 20 20 49 66 20 6e 65 69 74 68 65  only.  If neithe
389a2 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45 53 54  r.** SQLITE_TEST
389a3 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 42 55 47   or SQLITE_DEBUG
389a4 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68   are defined, th
389a5 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  en these routine
389a6 73 0a 2a 2a 20 61 72 65 20 6e 6f 2d 6f 70 73 2e  s.** are no-ops.
389a7 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
389a8 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52  (SQLITE_OMIT_VIR
389a9 54 55 41 4c 54 41 42 4c 45 29 20 26 26 20 64 65  TUALTABLE) && de
389aa 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
389ab 55 47 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20  UG).static void 
389ac 54 52 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53  TRACE_IDX_INPUTS
389ad 28 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69  (sqlite3_index_i
389ae 6e 66 6f 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  nfo *p){.  int i
389af 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ;.  if( !sqlite3
389b0 57 68 65 72 65 54 72 61 63 65 20 29 20 72 65 74  WhereTrace ) ret
389b1 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  urn;.  for(i=0; 
389b2 69 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74  i<p->nConstraint
389b3 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
389b4 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
389b5 20 20 63 6f 6e 73 74 72 61 69 6e 74 5b 25 64 5d    constraint[%d]
389b6 3a 20 63 6f 6c 3d 25 64 20 74 65 72 6d 69 64 3d  : col=%d termid=
389b7 25 64 20 6f 70 3d 25 64 20 75 73 61 62 6c 65 64  %d op=%d usabled
389b8 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 69  =%d\n",.       i
389b9 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e  ,.       p->aCon
389ba 73 74 72 61 69 6e 74 5b 69 5d 2e 69 43 6f 6c 75  straint[i].iColu
389bb 6d 6e 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43  mn,.       p->aC
389bc 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 69 54 65  onstraint[i].iTe
389bd 72 6d 4f 66 66 73 65 74 2c 0a 20 20 20 20 20 20  rmOffset,.      
389be 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b   p->aConstraint[
389bf 69 5d 2e 6f 70 2c 0a 20 20 20 20 20 20 20 70 2d  i].op,.       p-
389c0 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e  >aConstraint[i].
389c1 75 73 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 66  usable);.  }.  f
389c2 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 72  or(i=0; i<p->nOr
389c3 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20  derBy; i++){.   
389c4 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
389c5 6e 74 66 28 22 20 20 6f 72 64 65 72 62 79 5b 25  ntf("  orderby[%
389c6 64 5d 3a 20 63 6f 6c 3d 25 64 20 64 65 73 63 3d  d]: col=%d desc=
389c7 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 69 2c  %d\n",.       i,
389c8 0a 20 20 20 20 20 20 20 70 2d 3e 61 4f 72 64 65  .       p->aOrde
389c9 72 42 79 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a  rBy[i].iColumn,.
389ca 20 20 20 20 20 20 20 70 2d 3e 61 4f 72 64 65 72         p->aOrder
389cb 42 79 5b 69 5d 2e 64 65 73 63 29 3b 0a 20 20 7d  By[i].desc);.  }
389cc 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 54  .}.static void T
389cd 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54 53  RACE_IDX_OUTPUTS
389ce 28 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69  (sqlite3_index_i
389cf 6e 66 6f 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  nfo *p){.  int i
389d0 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ;.  if( !sqlite3
389d1 57 68 65 72 65 54 72 61 63 65 20 29 20 72 65 74  WhereTrace ) ret
389d2 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  urn;.  for(i=0; 
389d3 69 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74  i<p->nConstraint
389d4 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
389d5 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
389d6 20 20 75 73 61 67 65 5b 25 64 5d 3a 20 61 72 67    usage[%d]: arg
389d7 76 49 64 78 3d 25 64 20 6f 6d 69 74 3d 25 64 5c  vIdx=%d omit=%d\
389d8 6e 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20  n",.       i,.  
389d9 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61       p->aConstra
389da 69 6e 74 55 73 61 67 65 5b 69 5d 2e 61 72 67 76  intUsage[i].argv
389db 49 6e 64 65 78 2c 0a 20 20 20 20 20 20 20 70 2d  Index,.       p-
389dc 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67  >aConstraintUsag
389dd 65 5b 69 5d 2e 6f 6d 69 74 29 3b 0a 20 20 7d 0a  e[i].omit);.  }.
389de 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
389df 69 6e 74 66 28 22 20 20 69 64 78 4e 75 6d 3d 25  intf("  idxNum=%
389e0 64 5c 6e 22 2c 20 70 2d 3e 69 64 78 4e 75 6d 29  d\n", p->idxNum)
389e1 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67  ;.  sqlite3Debug
389e2 50 72 69 6e 74 66 28 22 20 20 69 64 78 53 74 72  Printf("  idxStr
389e3 3d 25 73 5c 6e 22 2c 20 70 2d 3e 69 64 78 53 74  =%s\n", p->idxSt
389e4 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62  r);.  sqlite3Deb
389e5 75 67 50 72 69 6e 74 66 28 22 20 20 6f 72 64 65  ugPrintf("  orde
389e6 72 42 79 43 6f 6e 73 75 6d 65 64 3d 25 64 5c 6e  rByConsumed=%d\n
389e7 22 2c 20 70 2d 3e 6f 72 64 65 72 42 79 43 6f 6e  ", p->orderByCon
389e8 73 75 6d 65 64 29 3b 0a 20 20 73 71 6c 69 74 65  sumed);.  sqlite
389e9 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20  3DebugPrintf("  
389ea 65 73 74 69 6d 61 74 65 64 43 6f 73 74 3d 25 67  estimatedCost=%g
389eb 5c 6e 22 2c 20 70 2d 3e 65 73 74 69 6d 61 74 65  \n", p->estimate
389ec 64 43 6f 73 74 29 3b 0a 7d 0a 23 65 6c 73 65 0a  dCost);.}.#else.
389ed 23 64 65 66 69 6e 65 20 54 52 41 43 45 5f 49 44  #define TRACE_ID
389ee 58 5f 49 4e 50 55 54 53 28 41 29 0a 23 64 65 66  X_INPUTS(A).#def
389ef 69 6e 65 20 54 52 41 43 45 5f 49 44 58 5f 4f 55  ine TRACE_IDX_OU
389f0 54 50 55 54 53 28 41 29 0a 23 65 6e 64 69 66 0a  TPUTS(A).#endif.
389f1 0a 2f 2a 20 0a 2a 2a 20 52 65 71 75 69 72 65 64  ./* .** Required
389f2 20 62 65 63 61 75 73 65 20 62 65 73 74 49 6e 64   because bestInd
389f3 65 78 28 29 20 69 73 20 63 61 6c 6c 65 64 20 62  ex() is called b
389f4 79 20 62 65 73 74 4f 72 43 6c 61 75 73 65 49 6e  y bestOrClauseIn
389f5 64 65 78 28 29 20 0a 2a 2f 0a 73 74 61 74 69 63  dex() .*/.static
389f6 20 76 6f 69 64 20 62 65 73 74 49 6e 64 65 78 28   void bestIndex(
389f7 57 68 65 72 65 42 65 73 74 49 64 78 2a 29 3b 0a  WhereBestIdx*);.
389f8 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
389f9 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ine attempts to 
389fa 66 69 6e 64 20 61 6e 20 73 63 61 6e 6e 69 6e 67  find an scanning
389fb 20 73 74 72 61 74 65 67 79 20 74 68 61 74 20 63   strategy that c
389fc 61 6e 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 74  an be used .** t
389fd 6f 20 6f 70 74 69 6d 69 7a 65 20 61 6e 20 27 4f  o optimize an 'O
389fe 52 27 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  R' expression th
389ff 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 20  at is part of a 
38a00 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 0a 2a  WHERE clause. .*
38a01 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 61  *.** The table a
38a02 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 46  ssociated with F
38a03 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20  ROM clause term 
38a04 70 53 72 63 20 6d 61 79 20 62 65 20 65 69 74 68  pSrc may be eith
38a05 65 72 20 61 0a 2a 2a 20 72 65 67 75 6c 61 72 20  er a.** regular 
38a06 42 2d 54 72 65 65 20 74 61 62 6c 65 20 6f 72 20  B-Tree table or 
38a07 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
38a08 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
38a09 62 65 73 74 4f 72 43 6c 61 75 73 65 49 6e 64 65  bestOrClauseInde
38a0a 78 28 57 68 65 72 65 42 65 73 74 49 64 78 20 2a  x(WhereBestIdx *
38a0b 70 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  p){.#ifndef SQLI
38a0c 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d  TE_OMIT_OR_OPTIM
38a0d 49 5a 41 54 49 4f 4e 0a 20 20 57 68 65 72 65 43  IZATION.  WhereC
38a0e 6c 61 75 73 65 20 2a 70 57 43 20 3d 20 70 2d 3e  lause *pWC = p->
38a0f 70 57 43 3b 20 20 20 20 20 20 20 20 20 20 20 2f  pWC;           /
38a10 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
38a11 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53  se */.  struct S
38a12 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72  rcList_item *pSr
38a13 63 20 3d 20 70 2d 3e 70 53 72 63 3b 20 2f 2a 20  c = p->pSrc; /* 
38a14 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  The FROM clause 
38a15 74 65 72 6d 20 74 6f 20 73 65 61 72 63 68 20 2a  term to search *
38a16 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 43  /.  const int iC
38a17 75 72 20 3d 20 70 53 72 63 2d 3e 69 43 75 72 73  ur = pSrc->iCurs
38a18 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  or;      /* The 
38a19 63 75 72 73 6f 72 20 6f 66 20 74 68 65 20 74 61  cursor of the ta
38a1a 62 6c 65 20 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ble  */.  const 
38a1b 42 69 74 6d 61 73 6b 20 6d 61 73 6b 53 72 63 20  Bitmask maskSrc 
38a1c 3d 20 67 65 74 4d 61 73 6b 28 70 57 43 2d 3e 70  = getMask(pWC->p
38a1d 4d 61 73 6b 53 65 74 2c 20 69 43 75 72 29 3b 20  MaskSet, iCur); 
38a1e 20 2f 2a 20 42 69 74 6d 61 73 6b 20 66 6f 72 20   /* Bitmask for 
38a1f 70 53 72 63 20 2a 2f 0a 20 20 57 68 65 72 65 54  pSrc */.  WhereT
38a20 65 72 6d 20 2a 20 63 6f 6e 73 74 20 70 57 43 45  erm * const pWCE
38a21 6e 64 20 3d 20 26 70 57 43 2d 3e 61 5b 70 57 43  nd = &pWC->a[pWC
38a22 2d 3e 6e 54 65 72 6d 5d 3b 20 20 20 20 20 20 20  ->nTerm];       
38a23 20 2f 2a 20 45 6e 64 20 6f 66 20 70 57 43 2d 3e   /* End of pWC->
38a24 61 5b 5d 20 2a 2f 0a 20 20 57 68 65 72 65 54 65  a[] */.  WhereTe
38a25 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20  rm *pTerm;      
38a26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38a27 20 41 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 6f   A single term o
38a28 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
38a29 73 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20  se */..  /* The 
38a2a 4f 52 2d 63 6c 61 75 73 65 20 6f 70 74 69 6d 69  OR-clause optimi
38a2b 7a 61 74 69 6f 6e 20 69 73 20 64 69 73 61 6c 6c  zation is disall
38a2c 6f 77 65 64 20 69 66 20 74 68 65 20 49 4e 44 45  owed if the INDE
38a2d 58 45 44 20 42 59 20 6f 72 0a 20 20 2a 2a 20 4e  XED BY or.  ** N
38a2e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75 73  OT INDEXED claus
38a2f 65 73 20 61 72 65 20 75 73 65 64 20 6f 72 20 69  es are used or i
38a30 66 20 74 68 65 20 57 48 45 52 45 5f 41 4e 44 5f  f the WHERE_AND_
38a31 4f 4e 4c 59 20 62 69 74 20 69 73 20 73 65 74 2e  ONLY bit is set.
38a32 20 2a 2f 0a 20 20 69 66 28 20 70 53 72 63 2d 3e   */.  if( pSrc->
38a33 6e 6f 74 49 6e 64 65 78 65 64 20 7c 7c 20 70 53  notIndexed || pS
38a34 72 63 2d 3e 70 49 6e 64 65 78 21 3d 30 20 29 7b  rc->pIndex!=0 ){
38a35 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
38a36 0a 20 20 69 66 28 20 70 57 43 2d 3e 77 63 74 72  .  if( pWC->wctr
38a37 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 41  lFlags & WHERE_A
38a38 4e 44 5f 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 72  ND_ONLY ){.    r
38a39 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eturn;.  }..  /*
38a3a 20 53 65 61 72 63 68 20 74 68 65 20 57 48 45 52   Search the WHER
38a3b 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 66  E clause terms f
38a3c 6f 72 20 61 20 75 73 61 62 6c 65 20 57 4f 5f 4f  or a usable WO_O
38a3d 52 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 66 6f 72  R term. */.  for
38a3e 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 70  (pTerm=pWC->a; p
38a3f 54 65 72 6d 3c 70 57 43 45 6e 64 3b 20 70 54 65  Term<pWCEnd; pTe
38a40 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70  rm++){.    if( p
38a41 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
38a42 3d 57 4f 5f 4f 52 20 0a 20 20 20 20 20 26 26 20  =WO_OR .     && 
38a43 28 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41  ((pTerm->prereqA
38a44 6c 6c 20 26 20 7e 6d 61 73 6b 53 72 63 29 20 26  ll & ~maskSrc) &
38a45 20 70 2d 3e 6e 6f 74 52 65 61 64 79 29 3d 3d 30   p->notReady)==0
38a46 0a 20 20 20 20 20 26 26 20 28 70 54 65 72 6d 2d  .     && (pTerm-
38a47 3e 75 2e 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65  >u.pOrInfo->inde
38a48 78 61 62 6c 65 20 26 20 6d 61 73 6b 53 72 63 29  xable & maskSrc)
38a49 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 20  !=0 .    ){.    
38a4a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 20    WhereClause * 
38a4b 63 6f 6e 73 74 20 70 4f 72 57 43 20 3d 20 26 70  const pOrWC = &p
38a4c 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 2d  Term->u.pOrInfo-
38a4d 3e 77 63 3b 0a 20 20 20 20 20 20 57 68 65 72 65  >wc;.      Where
38a4e 54 65 72 6d 20 2a 20 63 6f 6e 73 74 20 70 4f 72  Term * const pOr
38a4f 57 43 45 6e 64 20 3d 20 26 70 4f 72 57 43 2d 3e  WCEnd = &pOrWC->
38a50 61 5b 70 4f 72 57 43 2d 3e 6e 54 65 72 6d 5d 3b  a[pOrWC->nTerm];
38a51 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d  .      WhereTerm
38a52 20 2a 70 4f 72 54 65 72 6d 3b 0a 20 20 20 20 20   *pOrTerm;.     
38a53 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 57 48 45   int flags = WHE
38a54 52 45 5f 4d 55 4c 54 49 5f 4f 52 3b 0a 20 20 20  RE_MULTI_OR;.   
38a55 20 20 20 64 6f 75 62 6c 65 20 72 54 6f 74 61 6c     double rTotal
38a56 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f 75 62   = 0;.      doub
38a57 6c 65 20 6e 52 6f 77 20 3d 20 30 3b 0a 20 20 20  le nRow = 0;.   
38a58 20 20 20 42 69 74 6d 61 73 6b 20 75 73 65 64 20     Bitmask used 
38a59 3d 20 30 3b 0a 20 20 20 20 20 20 57 68 65 72 65  = 0;.      Where
38a5a 42 65 73 74 49 64 78 20 73 42 4f 49 3b 0a 0a 20  BestIdx sBOI;.. 
38a5b 20 20 20 20 20 73 42 4f 49 20 3d 20 2a 70 3b 0a       sBOI = *p;.
38a5c 20 20 20 20 20 20 73 42 4f 49 2e 70 4f 72 64 65        sBOI.pOrde
38a5d 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 73  rBy = 0;.      s
38a5e 42 4f 49 2e 70 44 69 73 74 69 6e 63 74 20 3d 20  BOI.pDistinct = 
38a5f 30 3b 0a 20 20 20 20 20 20 73 42 4f 49 2e 70 70  0;.      sBOI.pp
38a60 49 64 78 49 6e 66 6f 20 3d 20 30 3b 0a 20 20 20  IdxInfo = 0;.   
38a61 20 20 20 66 6f 72 28 70 4f 72 54 65 72 6d 3d 70     for(pOrTerm=p
38a62 4f 72 57 43 2d 3e 61 3b 20 70 4f 72 54 65 72 6d  OrWC->a; pOrTerm
38a63 3c 70 4f 72 57 43 45 6e 64 3b 20 70 4f 72 54 65  <pOrWCEnd; pOrTe
38a64 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 57  rm++){.        W
38a65 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20  HERETRACE(("... 
38a66 4d 75 6c 74 69 2d 69 6e 64 65 78 20 4f 52 20 74  Multi-index OR t
38a67 65 73 74 69 6e 67 20 66 6f 72 20 74 65 72 6d 20  esting for term 
38a68 25 64 20 6f 66 20 25 64 2e 2e 2e 2e 5c 6e 22 2c  %d of %d....\n",
38a69 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 4f 72   .          (pOr
38a6a 54 65 72 6d 20 2d 20 70 4f 72 57 43 2d 3e 61 29  Term - pOrWC->a)
38a6b 2c 20 28 70 54 65 72 6d 20 2d 20 70 57 43 2d 3e  , (pTerm - pWC->
38a6c 61 29 0a 20 20 20 20 20 20 20 20 29 29 3b 0a 20  a).        ));. 
38a6d 20 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65         if( pOrTe
38a6e 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
38a6f 4f 5f 41 4e 44 20 29 7b 0a 20 20 20 20 20 20 20  O_AND ){.       
38a70 20 20 20 73 42 4f 49 2e 70 57 43 20 3d 20 26 70     sBOI.pWC = &p
38a71 4f 72 54 65 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e  OrTerm->u.pAndIn
38a72 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20 20 20  fo->wc;.        
38a73 20 20 62 65 73 74 49 6e 64 65 78 28 26 73 42 4f    bestIndex(&sBO
38a74 49 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  I);.        }els
38a75 65 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c  e if( pOrTerm->l
38a76 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20  eftCursor==iCur 
38a77 29 7b 0a 20 20 20 20 20 20 20 20 20 20 57 68 65  ){.          Whe
38a78 72 65 43 6c 61 75 73 65 20 74 65 6d 70 57 43 3b  reClause tempWC;
38a79 0a 20 20 20 20 20 20 20 20 20 20 74 65 6d 70 57  .          tempW
38a7a 43 2e 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e  C.pParse = pWC->
38a7b 70 50 61 72 73 65 3b 0a 20 20 20 20 20 20 20 20  pParse;.        
38a7c 20 20 74 65 6d 70 57 43 2e 70 4d 61 73 6b 53 65    tempWC.pMaskSe
38a7d 74 20 3d 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65  t = pWC->pMaskSe
38a7e 74 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 6d  t;.          tem
38a7f 70 57 43 2e 70 4f 75 74 65 72 20 3d 20 70 57 43  pWC.pOuter = pWC
38a80 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 6d 70  ;.          temp
38a81 57 43 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a  WC.op = TK_AND;.
38a82 20 20 20 20 20 20 20 20 20 20 74 65 6d 70 57 43            tempWC
38a83 2e 61 20 3d 20 70 4f 72 54 65 72 6d 3b 0a 20 20  .a = pOrTerm;.  
38a84 20 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e 77          tempWC.w
38a85 63 74 72 6c 46 6c 61 67 73 20 3d 20 30 3b 0a 20  ctrlFlags = 0;. 
38a86 20 20 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e           tempWC.
38a87 6e 54 65 72 6d 20 3d 20 31 3b 0a 20 20 20 20 20  nTerm = 1;.     
38a88 20 20 20 20 20 73 42 4f 49 2e 70 57 43 20 3d 20       sBOI.pWC = 
38a89 26 74 65 6d 70 57 43 3b 0a 20 20 20 20 20 20 20  &tempWC;.       
38a8a 20 20 20 62 65 73 74 49 6e 64 65 78 28 26 73 42     bestIndex(&sB
38a8b 4f 49 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  OI);.        }el
38a8c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f  se{.          co
38a8d 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
38a8e 7d 0a 20 20 20 20 20 20 20 20 72 54 6f 74 61 6c  }.        rTotal
38a8f 20 2b 3d 20 73 42 4f 49 2e 63 6f 73 74 2e 72 43   += sBOI.cost.rC
38a90 6f 73 74 3b 0a 20 20 20 20 20 20 20 20 6e 52 6f  ost;.        nRo
38a91 77 20 2b 3d 20 73 42 4f 49 2e 63 6f 73 74 2e 70  w += sBOI.cost.p
38a92 6c 61 6e 2e 6e 52 6f 77 3b 0a 20 20 20 20 20 20  lan.nRow;.      
38a93 20 20 75 73 65 64 20 7c 3d 20 73 42 4f 49 2e 63    used |= sBOI.c
38a94 6f 73 74 2e 75 73 65 64 3b 0a 20 20 20 20 20 20  ost.used;.      
38a95 20 20 69 66 28 20 72 54 6f 74 61 6c 3e 3d 70 2d    if( rTotal>=p-
38a96 3e 63 6f 73 74 2e 72 43 6f 73 74 20 29 20 62 72  >cost.rCost ) br
38a97 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  eak;.      }..  
38a98 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
38a99 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  is an ORDER BY c
38a9a 6c 61 75 73 65 2c 20 69 6e 63 72 65 61 73 65 20  lause, increase 
38a9b 74 68 65 20 73 63 61 6e 20 63 6f 73 74 20 74 6f  the scan cost to
38a9c 20 61 63 63 6f 75 6e 74 20 0a 20 20 20 20 20 20   account .      
38a9d 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 73 74 20  ** for the cost 
38a9e 6f 66 20 74 68 65 20 73 6f 72 74 2e 20 2a 2f 0a  of the sort. */.
38a9f 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4f 72        if( p->pOr
38aa0 64 65 72 42 79 21 3d 30 20 29 7b 0a 20 20 20 20  derBy!=0 ){.    
38aa1 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28      WHERETRACE((
38aa2 22 2e 2e 2e 20 73 6f 72 74 69 6e 67 20 69 6e 63  "... sorting inc
38aa3 72 65 61 73 65 73 20 4f 52 20 63 6f 73 74 20 25  reases OR cost %
38aa4 2e 39 67 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 0a  .9g to %.9g\n",.
38aa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38aa6 20 20 20 20 72 54 6f 74 61 6c 2c 20 72 54 6f 74      rTotal, rTot
38aa7 61 6c 2b 6e 52 6f 77 2a 65 73 74 4c 6f 67 28 6e  al+nRow*estLog(n
38aa8 52 6f 77 29 29 29 3b 0a 20 20 20 20 20 20 20 20  Row)));.        
38aa9 72 54 6f 74 61 6c 20 2b 3d 20 6e 52 6f 77 2a 65  rTotal += nRow*e
38aaa 73 74 4c 6f 67 28 6e 52 6f 77 29 3b 0a 20 20 20  stLog(nRow);.   
38aab 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49     }..      /* I
38aac 66 20 74 68 65 20 63 6f 73 74 20 6f 66 20 73 63  f the cost of sc
38aad 61 6e 6e 69 6e 67 20 75 73 69 6e 67 20 74 68 69  anning using thi
38aae 73 20 4f 52 20 74 65 72 6d 20 66 6f 72 20 6f 70  s OR term for op
38aaf 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 0a 20 20  timization is.  
38ab0 20 20 20 20 2a 2a 20 6c 65 73 73 20 74 68 61 6e      ** less than
38ab1 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 73   the current cos
38ab2 74 20 73 74 6f 72 65 64 20 69 6e 20 70 43 6f 73  t stored in pCos
38ab3 74 2c 20 72 65 70 6c 61 63 65 20 74 68 65 20 63  t, replace the c
38ab4 6f 6e 74 65 6e 74 73 0a 20 20 20 20 20 20 2a 2a  ontents.      **
38ab5 20 6f 66 20 70 43 6f 73 74 2e 20 2a 2f 0a 20 20   of pCost. */.  
38ab6 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28      WHERETRACE((
38ab7 22 2e 2e 2e 20 6d 75 6c 74 69 2d 69 6e 64 65 78  "... multi-index
38ab8 20 4f 52 20 63 6f 73 74 3d 25 2e 39 67 20 6e 72   OR cost=%.9g nr
38ab9 6f 77 3d 25 2e 39 67 5c 6e 22 2c 20 72 54 6f 74  ow=%.9g\n", rTot
38aba 61 6c 2c 20 6e 52 6f 77 29 29 3b 0a 20 20 20 20  al, nRow));.    
38abb 20 20 69 66 28 20 72 54 6f 74 61 6c 3c 70 2d 3e    if( rTotal<p->
38abc 63 6f 73 74 2e 72 43 6f 73 74 20 29 7b 0a 20 20  cost.rCost ){.  
38abd 20 20 20 20 20 20 70 2d 3e 63 6f 73 74 2e 72 43        p->cost.rC
38abe 6f 73 74 20 3d 20 72 54 6f 74 61 6c 3b 0a 20 20  ost = rTotal;.  
38abf 20 20 20 20 20 20 70 2d 3e 63 6f 73 74 2e 75 73        p->cost.us
38ac0 65 64 20 3d 20 75 73 65 64 3b 0a 20 20 20 20 20  ed = used;.     
38ac1 20 20 20 70 2d 3e 63 6f 73 74 2e 70 6c 61 6e 2e     p->cost.plan.
38ac2 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b 0a 20 20 20  nRow = nRow;.   
38ac3 20 20 20 20 20 70 2d 3e 63 6f 73 74 2e 70 6c 61       p->cost.pla
38ac4 6e 2e 6e 4f 42 53 61 74 20 3d 20 70 2d 3e 69 20  n.nOBSat = p->i 
38ac5 3f 20 70 2d 3e 61 4c 65 76 65 6c 5b 70 2d 3e 69  ? p->aLevel[p->i
38ac6 2d 31 5d 2e 70 6c 61 6e 2e 6e 4f 42 53 61 74 20  -1].plan.nOBSat 
38ac7 3a 20 30 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  : 0;.        p->
38ac8 63 6f 73 74 2e 70 6c 61 6e 2e 77 73 46 6c 61 67  cost.plan.wsFlag
38ac9 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 20 20 20  s = flags;.     
38aca 20 20 20 70 2d 3e 63 6f 73 74 2e 70 6c 61 6e 2e     p->cost.plan.
38acb 75 2e 70 54 65 72 6d 20 3d 20 70 54 65 72 6d 3b  u.pTerm = pTerm;
38acc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
38acd 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
38ace 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49  ITE_OMIT_OR_OPTI
38acf 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 7d 0a 0a 23  MIZATION */.}..#
38ad0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
38ad1 49 54 5f 41 55 54 4f 4d 41 54 49 43 5f 49 4e 44  IT_AUTOMATIC_IND
38ad2 45 58 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  EX./*.** Return 
38ad3 54 52 55 45 20 69 66 20 74 68 65 20 57 48 45 52  TRUE if the WHER
38ad4 45 20 63 6c 61 75 73 65 20 74 65 72 6d 20 70 54  E clause term pT
38ad5 65 72 6d 20 69 73 20 6f 66 20 61 20 66 6f 72 6d  erm is of a form
38ad6 20 77 68 65 72 65 20 69 74 0a 2a 2a 20 63 6f 75   where it.** cou
38ad7 6c 64 20 62 65 20 75 73 65 64 20 77 69 74 68 20  ld be used with 
38ad8 61 6e 20 69 6e 64 65 78 20 74 6f 20 61 63 63 65  an index to acce
38ad9 73 73 20 70 53 72 63 2c 20 61 73 73 75 6d 69 6e  ss pSrc, assumin
38ada 67 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65  g an appropriate
38adb 0a 2a 2a 20 69 6e 64 65 78 20 65 78 69 73 74 65  .** index existe
38adc 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
38add 20 74 65 72 6d 43 61 6e 44 72 69 76 65 49 6e 64   termCanDriveInd
38ade 65 78 28 0a 20 20 57 68 65 72 65 54 65 72 6d 20  ex(.  WhereTerm 
38adf 2a 70 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20  *pTerm,         
38ae0 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c       /* WHERE cl
38ae1 61 75 73 65 20 74 65 72 6d 20 74 6f 20 63 68 65  ause term to che
38ae2 63 6b 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53  ck */.  struct S
38ae3 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72  rcList_item *pSr
38ae4 63 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20  c,     /* Table 
38ae5 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f  we are trying to
38ae6 20 61 63 63 65 73 73 20 2a 2f 0a 20 20 42 69 74   access */.  Bit
38ae7 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 20 20 20  mask notReady   
38ae8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
38ae9 61 62 6c 65 73 20 69 6e 20 6f 75 74 65 72 20 6c  ables in outer l
38aea 6f 6f 70 73 20 6f 66 20 74 68 65 20 6a 6f 69 6e  oops of the join
38aeb 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 61 66   */.){.  char af
38aec 66 3b 0a 20 20 69 66 28 20 70 54 65 72 6d 2d 3e  f;.  if( pTerm->
38aed 6c 65 66 74 43 75 72 73 6f 72 21 3d 70 53 72 63  leftCursor!=pSrc
38aee 2d 3e 69 43 75 72 73 6f 72 20 29 20 72 65 74 75  ->iCursor ) retu
38aef 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 54 65 72  rn 0;.  if( pTer
38af0 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21 3d 57 4f  m->eOperator!=WO
38af1 5f 45 51 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  _EQ ) return 0;.
38af2 20 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 70 72    if( (pTerm->pr
38af3 65 72 65 71 52 69 67 68 74 20 26 20 6e 6f 74 52  ereqRight & notR
38af4 65 61 64 79 29 21 3d 30 20 29 20 72 65 74 75 72  eady)!=0 ) retur
38af5 6e 20 30 3b 0a 20 20 61 66 66 20 3d 20 70 53 72  n 0;.  aff = pSr
38af6 63 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54  c->pTab->aCol[pT
38af7 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d  erm->u.leftColum
38af8 6e 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 69  n].affinity;.  i
38af9 66 28 20 21 73 71 6c 69 74 65 33 49 6e 64 65 78  f( !sqlite3Index
38afa 41 66 66 69 6e 69 74 79 4f 6b 28 70 54 65 72 6d  AffinityOk(pTerm
38afb 2d 3e 70 45 78 70 72 2c 20 61 66 66 29 20 29 20  ->pExpr, aff) ) 
38afc 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75  return 0;.  retu
38afd 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  rn 1;.}.#endif..
38afe 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
38aff 4d 49 54 5f 41 55 54 4f 4d 41 54 49 43 5f 49 4e  MIT_AUTOMATIC_IN
38b00 44 45 58 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  DEX./*.** If the
38b01 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72 20   query plan for 
38b02 70 53 72 63 20 73 70 65 63 69 66 69 65 64 20 69  pSrc specified i
38b03 6e 20 70 43 6f 73 74 20 69 73 20 61 20 66 75 6c  n pCost is a ful
38b04 6c 20 74 61 62 6c 65 20 73 63 61 6e 0a 2a 2a 20  l table scan.** 
38b05 61 6e 64 20 69 6e 64 65 78 69 6e 67 20 69 73 20  and indexing is 
38b06 61 6c 6c 6f 77 73 20 28 69 66 20 74 68 65 72 65  allows (if there
38b07 20 69 73 20 6e 6f 20 4e 4f 54 20 49 4e 44 45 58   is no NOT INDEX
38b08 45 44 20 63 6c 61 75 73 65 29 20 61 6e 64 20 69  ED clause) and i
38b09 74 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 20 74 6f  t.** possible to
38b0a 20 63 6f 6e 73 74 72 75 63 74 20 61 20 74 72 61   construct a tra
38b0b 6e 73 69 65 6e 74 20 69 6e 64 65 78 20 74 68 61  nsient index tha
38b0c 74 20 77 6f 75 6c 64 20 70 65 72 66 6f 72 6d 20  t would perform 
38b0d 62 65 74 74 65 72 0a 2a 2a 20 74 68 61 6e 20 61  better.** than a
38b0e 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e   full table scan
38b0f 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 20 63   even when the c
38b10 6f 73 74 20 6f 66 20 63 6f 6e 73 74 72 75 63 74  ost of construct
38b11 69 6e 67 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a  ing the index.**
38b12 20 69 73 20 74 61 6b 65 6e 20 69 6e 74 6f 20 61   is taken into a
38b13 63 63 6f 75 6e 74 2c 20 74 68 65 6e 20 61 6c 74  ccount, then alt
38b14 65 72 20 74 68 65 20 71 75 65 72 79 20 70 6c 61  er the query pla
38b15 6e 20 74 6f 20 75 73 65 20 74 68 65 0a 2a 2a 20  n to use the.** 
38b16 74 72 61 6e 73 69 65 6e 74 20 69 6e 64 65 78 2e  transient index.
38b17 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
38b18 62 65 73 74 41 75 74 6f 6d 61 74 69 63 49 6e 64  bestAutomaticInd
38b19 65 78 28 57 68 65 72 65 42 65 73 74 49 64 78 20  ex(WhereBestIdx 
38b1a 2a 70 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  *p){.  Parse *pP
38b1b 61 72 73 65 20 3d 20 70 2d 3e 70 50 61 72 73 65  arse = p->pParse
38b1c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
38b1d 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
38b1e 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c  ext */.  WhereCl
38b1f 61 75 73 65 20 2a 70 57 43 20 3d 20 70 2d 3e 70  ause *pWC = p->p
38b20 57 43 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  WC;            /
38b21 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
38b22 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53  se */.  struct S
38b23 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72  rcList_item *pSr
38b24 63 20 3d 20 70 2d 3e 70 53 72 63 3b 20 20 2f 2a  c = p->pSrc;  /*
38b25 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   The FROM clause
38b26 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63 68 20   term to search 
38b27 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 6e 54 61 62  */.  double nTab
38b28 6c 65 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20  leRow;          
38b29 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
38b2a 77 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20  ws in the input 
38b2b 74 61 62 6c 65 20 2a 2f 0a 20 20 64 6f 75 62 6c  table */.  doubl
38b2c 65 20 6c 6f 67 4e 3b 20 20 20 20 20 20 20 20 20  e logN;         
38b2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38b2e 20 2f 2a 20 6c 6f 67 28 6e 54 61 62 6c 65 52 6f   /* log(nTableRo
38b2f 77 29 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 63  w) */.  double c
38b30 6f 73 74 54 65 6d 70 49 64 78 3b 20 20 20 20 20  ostTempIdx;     
38b31 20 20 20 20 2f 2a 20 70 65 72 2d 71 75 65 72 79      /* per-query
38b32 20 63 6f 73 74 20 6f 66 20 74 68 65 20 74 72 61   cost of the tra
38b33 6e 73 69 65 6e 74 20 69 6e 64 65 78 20 2a 2f 0a  nsient index */.
38b34 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65    WhereTerm *pTe
38b35 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  rm;           /*
38b36 20 41 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 6f   A single term o
38b37 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
38b38 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72  se */.  WhereTer
38b39 6d 20 2a 70 57 43 45 6e 64 3b 20 20 20 20 20 20  m *pWCEnd;      
38b3a 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 70 57      /* End of pW
38b3b 43 2d 3e 61 5b 5d 20 2a 2f 0a 20 20 54 61 62 6c  C->a[] */.  Tabl
38b3c 65 20 2a 70 54 61 62 6c 65 3b 20 20 20 20 20 20  e *pTable;      
38b3d 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
38b3e 20 74 68 74 20 6d 69 67 68 74 20 62 65 20 69 6e   tht might be in
38b3f 64 65 78 65 64 20 2a 2f 0a 0a 20 20 69 66 28 20  dexed */..  if( 
38b40 70 50 61 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f  pParse->nQueryLo
38b41 6f 70 3c 3d 28 64 6f 75 62 6c 65 29 31 20 29 7b  op<=(double)1 ){
38b42 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73  .    /* There is
38b43 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 62 75 69   no point in bui
38b44 6c 64 69 6e 67 20 61 6e 20 61 75 74 6f 6d 61 74  lding an automat
38b45 69 63 20 69 6e 64 65 78 20 66 6f 72 20 61 20 73  ic index for a s
38b46 69 6e 67 6c 65 20 73 63 61 6e 20 2a 2f 0a 20 20  ingle scan */.  
38b47 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
38b48 69 66 28 20 28 70 50 61 72 73 65 2d 3e 64 62 2d  if( (pParse->db-
38b49 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
38b4a 41 75 74 6f 49 6e 64 65 78 29 3d 3d 30 20 29 7b  AutoIndex)==0 ){
38b4b 0a 20 20 20 20 2f 2a 20 41 75 74 6f 6d 61 74 69  .    /* Automati
38b4c 63 20 69 6e 64 69 63 65 73 20 61 72 65 20 64 69  c indices are di
38b4d 73 61 62 6c 65 64 20 61 74 20 72 75 6e 2d 74 69  sabled at run-ti
38b4e 6d 65 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  me */.    return
38b4f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e  ;.  }.  if( (p->
38b50 63 6f 73 74 2e 70 6c 61 6e 2e 77 73 46 6c 61 67  cost.plan.wsFlag
38b51 73 20 26 20 57 48 45 52 45 5f 4e 4f 54 5f 46 55  s & WHERE_NOT_FU
38b52 4c 4c 53 43 41 4e 29 21 3d 30 0a 20 20 20 26 26  LLSCAN)!=0.   &&
38b53 20 28 70 2d 3e 63 6f 73 74 2e 70 6c 61 6e 2e 77   (p->cost.plan.w
38b54 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 43  sFlags & WHERE_C
38b55 4f 56 45 52 5f 53 43 41 4e 29 3d 3d 30 0a 20 20  OVER_SCAN)==0.  
38b56 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 6c 72  ){.    /* We alr
38b57 65 61 64 79 20 68 61 76 65 20 73 6f 6d 65 20 6b  eady have some k
38b58 69 6e 64 20 6f 66 20 69 6e 64 65 78 20 69 6e 20  ind of index in 
38b59 75 73 65 20 66 6f 72 20 74 68 69 73 20 71 75 65  use for this que
38b5a 72 79 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ry. */.    retur
38b5b 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 72  n;.  }.  if( pSr
38b5c 63 2d 3e 76 69 61 43 6f 72 6f 75 74 69 6e 65 20  c->viaCoroutine 
38b5d 29 7b 0a 20 20 20 20 2f 2a 20 43 61 6e 6e 6f 74  ){.    /* Cannot
38b5e 20 69 6e 64 65 78 20 61 20 63 6f 2d 72 6f 75 74   index a co-rout
38b5f 69 6e 65 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ine */.    retur
38b60 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 72  n;.  }.  if( pSr
38b61 63 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 29 7b  c->notIndexed ){
38b62 0a 20 20 20 20 2f 2a 20 54 68 65 20 4e 4f 54 20  .    /* The NOT 
38b63 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20 61  INDEXED clause a
38b64 70 70 65 61 72 73 20 69 6e 20 74 68 65 20 53 51  ppears in the SQ
38b65 4c 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  L. */.    return
38b66 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 72 63  ;.  }.  if( pSrc
38b67 2d 3e 69 73 43 6f 72 72 65 6c 61 74 65 64 20 29  ->isCorrelated )
38b68 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 6f 75  {.    /* The sou
38b69 72 63 65 20 69 73 20 61 20 63 6f 72 72 65 6c 61  rce is a correla
38b6a 74 65 64 20 73 75 62 2d 71 75 65 72 79 2e 20 4e  ted sub-query. N
38b6b 6f 20 70 6f 69 6e 74 20 69 6e 20 69 6e 64 65 78  o point in index
38b6c 69 6e 67 20 69 74 2e 20 2a 2f 0a 20 20 20 20 72  ing it. */.    r
38b6d 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73  eturn;.  }..  as
38b6e 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 51  sert( pParse->nQ
38b6f 75 65 72 79 4c 6f 6f 70 20 3e 3d 20 28 64 6f 75  ueryLoop >= (dou
38b70 62 6c 65 29 31 20 29 3b 0a 20 20 70 54 61 62 6c  ble)1 );.  pTabl
38b71 65 20 3d 20 70 53 72 63 2d 3e 70 54 61 62 3b 0a  e = pSrc->pTab;.
38b72 20 20 6e 54 61 62 6c 65 52 6f 77 20 3d 20 70 54    nTableRow = pT
38b73 61 62 6c 65 2d 3e 6e 52 6f 77 45 73 74 3b 0a 20  able->nRowEst;. 
38b74 20 6c 6f 67 4e 20 3d 20 65 73 74 4c 6f 67 28 6e   logN = estLog(n
38b75 54 61 62 6c 65 52 6f 77 29 3b 0a 20 20 63 6f 73  TableRow);.  cos
38b76 74 54 65 6d 70 49 64 78 20 3d 20 32 2a 6c 6f 67  tTempIdx = 2*log
38b77 4e 2a 28 6e 54 61 62 6c 65 52 6f 77 2f 70 50 61  N*(nTableRow/pPa
38b78 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f 6f 70 20  rse->nQueryLoop 
38b79 2b 20 31 29 3b 0a 20 20 69 66 28 20 63 6f 73 74  + 1);.  if( cost
38b7a 54 65 6d 70 49 64 78 3e 3d 70 2d 3e 63 6f 73 74  TempIdx>=p->cost
38b7b 2e 72 43 6f 73 74 20 29 7b 0a 20 20 20 20 2f 2a  .rCost ){.    /*
38b7c 20 54 68 65 20 63 6f 73 74 20 6f 66 20 63 72 65   The cost of cre
38b7d 61 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73 69  ating the transi
38b7e 65 6e 74 20 74 61 62 6c 65 20 77 6f 75 6c 64 20  ent table would 
38b7f 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a  be greater than.
38b80 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68 65      ** doing the
38b81 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e   full table scan
38b82 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   */.    return;.
38b83 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68    }..  /* Search
38b84 20 66 6f 72 20 61 6e 79 20 65 71 75 61 6c 69 74   for any equalit
38b85 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 74 65 72  y comparison ter
38b86 6d 20 2a 2f 0a 20 20 70 57 43 45 6e 64 20 3d 20  m */.  pWCEnd = 
38b87 26 70 57 43 2d 3e 61 5b 70 57 43 2d 3e 6e 54 65  &pWC->a[pWC->nTe
38b88 72 6d 5d 3b 0a 20 20 66 6f 72 28 70 54 65 72 6d  rm];.  for(pTerm
38b89 3d 70 57 43 2d 3e 61 3b 20 70 54 65 72 6d 3c 70  =pWC->a; pTerm<p
38b8a 57 43 45 6e 64 3b 20 70 54 65 72 6d 2b 2b 29 7b  WCEnd; pTerm++){
38b8b 0a 20 20 20 20 69 66 28 20 74 65 72 6d 43 61 6e  .    if( termCan
38b8c 44 72 69 76 65 49 6e 64 65 78 28 70 54 65 72 6d  DriveIndex(pTerm
38b8d 2c 20 70 53 72 63 2c 20 70 2d 3e 6e 6f 74 52 65  , pSrc, p->notRe
38b8e 61 64 79 29 20 29 7b 0a 20 20 20 20 20 20 57 48  ady) ){.      WH
38b8f 45 52 45 54 52 41 43 45 28 28 22 61 75 74 6f 2d  ERETRACE(("auto-
38b90 69 6e 64 65 78 20 72 65 64 75 63 65 73 20 63 6f  index reduces co
38b91 73 74 20 66 72 6f 6d 20 25 2e 31 66 20 74 6f 20  st from %.1f to 
38b92 25 2e 31 66 5c 6e 22 2c 0a 20 20 20 20 20 20 20  %.1f\n",.       
38b93 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e               p->
38b94 63 6f 73 74 2e 72 43 6f 73 74 2c 20 63 6f 73 74  cost.rCost, cost
38b95 54 65 6d 70 49 64 78 29 29 3b 0a 20 20 20 20 20  TempIdx));.     
38b96 20 70 2d 3e 63 6f 73 74 2e 72 43 6f 73 74 20 3d   p->cost.rCost =
38b97 20 63 6f 73 74 54 65 6d 70 49 64 78 3b 0a 20 20   costTempIdx;.  
38b98 20 20 20 20 70 2d 3e 63 6f 73 74 2e 70 6c 61 6e      p->cost.plan
38b99 2e 6e 52 6f 77 20 3d 20 6c 6f 67 4e 20 2b 20 31  .nRow = logN + 1
38b9a 3b 0a 20 20 20 20 20 20 70 2d 3e 63 6f 73 74 2e  ;.      p->cost.
38b9b 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 57  plan.wsFlags = W
38b9c 48 45 52 45 5f 54 45 4d 50 5f 49 4e 44 45 58 3b  HERE_TEMP_INDEX;
38b9d 0a 20 20 20 20 20 20 70 2d 3e 63 6f 73 74 2e 75  .      p->cost.u
38b9e 73 65 64 20 3d 20 70 54 65 72 6d 2d 3e 70 72 65  sed = pTerm->pre
38b9f 72 65 71 52 69 67 68 74 3b 0a 20 20 20 20 20 20  reqRight;.      
38ba0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
38ba1 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  .}.#else.# defin
38ba2 65 20 62 65 73 74 41 75 74 6f 6d 61 74 69 63 49  e bestAutomaticI
38ba3 6e 64 65 78 28 41 29 20 20 2f 2a 20 6e 6f 2d 6f  ndex(A)  /* no-o
38ba4 70 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53  p */.#endif /* S
38ba5 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 4d  QLITE_OMIT_AUTOM
38ba6 41 54 49 43 5f 49 4e 44 45 58 20 2a 2f 0a 0a 0a  ATIC_INDEX */...
38ba7 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
38ba8 4d 49 54 5f 41 55 54 4f 4d 41 54 49 43 5f 49 4e  MIT_AUTOMATIC_IN
38ba9 44 45 58 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  DEX./*.** Genera
38baa 74 65 20 63 6f 64 65 20 74 6f 20 63 6f 6e 73 74  te code to const
38bab 72 75 63 74 20 74 68 65 20 49 6e 64 65 78 20 6f  ruct the Index o
38bac 62 6a 65 63 74 20 66 6f 72 20 61 6e 20 61 75 74  bject for an aut
38bad 6f 6d 61 74 69 63 20 69 6e 64 65 78 0a 2a 2a 20  omatic index.** 
38bae 61 6e 64 20 74 6f 20 73 65 74 20 75 70 20 74 68  and to set up th
38baf 65 20 57 68 65 72 65 4c 65 76 65 6c 20 6f 62 6a  e WhereLevel obj
38bb0 65 63 74 20 70 4c 65 76 65 6c 20 73 6f 20 74 68  ect pLevel so th
38bb1 61 74 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65  at the code gene
38bb2 72 61 74 6f 72 0a 2a 2a 20 6d 61 6b 65 73 20 75  rator.** makes u
38bb3 73 65 20 6f 66 20 74 68 65 20 61 75 74 6f 6d 61  se of the automa
38bb4 74 69 63 20 69 6e 64 65 78 2e 0a 2a 2f 0a 73 74  tic index..*/.st
38bb5 61 74 69 63 20 76 6f 69 64 20 63 6f 6e 73 74 72  atic void constr
38bb6 75 63 74 41 75 74 6f 6d 61 74 69 63 49 6e 64 65  uctAutomaticInde
38bb7 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  x(.  Parse *pPar
38bb8 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
38bb9 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
38bba 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65  context */.  Whe
38bbb 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20  reClause *pWC,  
38bbc 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
38bbd 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
38bbe 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
38bbf 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 2f 2a  _item *pSrc,  /*
38bc0 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   The FROM clause
38bc1 20 74 65 72 6d 20 74 6f 20 67 65 74 20 74 68 65   term to get the
38bc2 20 6e 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20   next index */. 
38bc3 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64   Bitmask notRead
38bc4 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y,           /* 
38bc5 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20  Mask of cursors 
38bc6 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61  that are not ava
38bc7 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 57 68 65 72  ilable */.  Wher
38bc8 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 20 20  eLevel *pLevel  
38bc9 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
38bca 20 6e 65 77 20 69 6e 64 65 78 20 68 65 72 65 20   new index here 
38bcb 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 43 6f 6c  */.){.  int nCol
38bcc 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  umn;            
38bcd 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
38bce 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
38bcf 63 6f 6e 73 74 72 75 63 74 65 64 20 69 6e 64 65  constructed inde
38bd0 78 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d  x */.  WhereTerm
38bd1 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20   *pTerm;        
38bd2 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 74     /* A single t
38bd3 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45  erm of the WHERE
38bd4 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65   clause */.  Whe
38bd5 72 65 54 65 72 6d 20 2a 70 57 43 45 6e 64 3b 20  reTerm *pWCEnd; 
38bd6 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20           /* End 
38bd7 6f 66 20 70 57 43 2d 3e 61 5b 5d 20 2a 2f 0a 20  of pWC->a[] */. 
38bd8 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20   int nByte;     
38bd9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38bda 42 79 74 65 20 6f 66 20 6d 65 6d 6f 72 79 20 6e  Byte of memory n
38bdb 65 65 64 65 64 20 66 6f 72 20 70 49 64 78 20 2a  eeded for pIdx *
38bdc 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  /.  Index *pIdx;
38bdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38bde 2f 2a 20 4f 62 6a 65 63 74 20 64 65 73 63 72 69  /* Object descri
38bdf 62 69 6e 67 20 74 68 65 20 74 72 61 6e 73 69 65  bing the transie
38be0 6e 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 56 64  nt index */.  Vd
38be1 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
38be2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
38be3 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
38be4 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
38be5 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  on */.  int addr
38be6 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20  Init;           
38be7 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
38be8 66 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61  f the initializa
38be9 74 69 6f 6e 20 62 79 70 61 73 73 20 6a 75 6d 70  tion bypass jump
38bea 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
38beb 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ble;            
38bec 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62    /* The table b
38bed 65 69 6e 67 20 69 6e 64 65 78 65 64 20 2a 2f 0a  eing indexed */.
38bee 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 69    KeyInfo *pKeyi
38bef 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  nfo;          /*
38bf0 20 4b 65 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   Key information
38bf1 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 20 2a   for the index *
38bf2 2f 20 20 20 0a 20 20 69 6e 74 20 61 64 64 72 54  /   .  int addrT
38bf3 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
38bf4 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65     /* Top of the
38bf5 20 69 6e 64 65 78 20 66 69 6c 6c 20 6c 6f 6f 70   index fill loop
38bf6 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 63   */.  int regRec
38bf7 6f 72 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ord;            
38bf8 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f    /* Register ho
38bf9 6c 64 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 72  lding an index r
38bfa 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ecord */.  int n
38bfb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
38bfc 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e         /* Column
38bfd 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
38bfe 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
38bff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
38c00 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
38c01 6e 74 20 6d 78 42 69 74 43 6f 6c 3b 20 20 20 20  nt mxBitCol;    
38c02 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
38c03 78 69 6d 75 6d 20 63 6f 6c 75 6d 6e 20 69 6e 20  ximum column in 
38c04 70 53 72 63 2d 3e 63 6f 6c 55 73 65 64 20 2a 2f  pSrc->colUsed */
38c05 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
38c06 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l;             /
38c07 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * Collating sequ
38c08 65 6e 63 65 20 74 6f 20 6f 6e 20 61 20 63 6f 6c  ence to on a col
38c09 75 6d 6e 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  umn */.  Bitmask
38c0a 20 69 64 78 43 6f 6c 73 3b 20 20 20 20 20 20 20   idxCols;       
38c0b 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 70 20 6f       /* Bitmap o
38c0c 66 20 63 6f 6c 75 6d 6e 73 20 75 73 65 64 20 66  f columns used f
38c0d 6f 72 20 69 6e 64 65 78 69 6e 67 20 2a 2f 0a 20  or indexing */. 
38c0e 20 42 69 74 6d 61 73 6b 20 65 78 74 72 61 43 6f   Bitmask extraCo
38c0f 6c 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ls;          /* 
38c10 42 69 74 6d 61 70 20 6f 66 20 61 64 64 69 74 69  Bitmap of additi
38c11 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a  onal columns */.
38c12 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63  .  /* Generate c
38c13 6f 64 65 20 74 6f 20 73 6b 69 70 20 6f 76 65 72  ode to skip over
38c14 20 74 68 65 20 63 72 65 61 74 69 6f 6e 20 61 6e   the creation an
38c15 64 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  d initialization
38c16 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 74 72 61   of the.  ** tra
38c17 6e 73 69 65 6e 74 20 69 6e 64 65 78 20 6f 6e 20  nsient index on 
38c18 32 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65  2nd and subseque
38c19 6e 74 20 69 74 65 72 61 74 69 6f 6e 73 20 6f 66  nt iterations of
38c1a 20 74 68 65 20 6c 6f 6f 70 2e 20 2a 2f 0a 20 20   the loop. */.  
38c1b 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
38c1c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d  e;.  assert( v!=
38c1d 30 20 29 3b 0a 20 20 61 64 64 72 49 6e 69 74 20  0 );.  addrInit 
38c1e 3d 20 73 71 6c 69 74 65 33 43 6f 64 65 4f 6e 63  = sqlite3CodeOnc
38c1f 65 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 2f 2a  e(pParse);..  /*
38c20 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65   Count the numbe
38c21 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 68 61  r of columns tha
38c22 74 20 77 69 6c 6c 20 62 65 20 61 64 64 65 64 20  t will be added 
38c23 74 6f 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a  to the index.  *
38c24 2a 20 61 6e 64 20 75 73 65 64 20 74 6f 20 6d 61  * and used to ma
38c25 74 63 68 20 57 48 45 52 45 20 63 6c 61 75 73 65  tch WHERE clause
38c26 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
38c27 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20    nColumn = 0;. 
38c28 20 70 54 61 62 6c 65 20 3d 20 70 53 72 63 2d 3e   pTable = pSrc->
38c29 70 54 61 62 3b 0a 20 20 70 57 43 45 6e 64 20 3d  pTab;.  pWCEnd =
38c2a 20 26 70 57 43 2d 3e 61 5b 70 57 43 2d 3e 6e 54   &pWC->a[pWC->nT
38c2b 65 72 6d 5d 3b 0a 20 20 69 64 78 43 6f 6c 73 20  erm];.  idxCols 
38c2c 3d 20 30 3b 0a 20 20 66 6f 72 28 70 54 65 72 6d  = 0;.  for(pTerm
38c2d 3d 70 57 43 2d 3e 61 3b 20 70 54 65 72 6d 3c 70  =pWC->a; pTerm<p
38c2e 57 43 45 6e 64 3b 20 70 54 65 72 6d 2b 2b 29 7b  WCEnd; pTerm++){
38c2f 0a 20 20 20 20 69 66 28 20 74 65 72 6d 43 61 6e  .    if( termCan
38c30 44 72 69 76 65 49 6e 64 65 78 28 70 54 65 72 6d  DriveIndex(pTerm
38c31 2c 20 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79  , pSrc, notReady
38c32 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  ) ){.      int i
38c33 43 6f 6c 20 3d 20 70 54 65 72 6d 2d 3e 75 2e 6c  Col = pTerm->u.l
38c34 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  eftColumn;.     
38c35 20 42 69 74 6d 61 73 6b 20 63 4d 61 73 6b 20 3d   Bitmask cMask =
38c36 20 69 43 6f 6c 3e 3d 42 4d 53 20 3f 20 28 28 42   iCol>=BMS ? ((B
38c37 69 74 6d 61 73 6b 29 31 29 3c 3c 28 42 4d 53 2d  itmask)1)<<(BMS-
38c38 31 29 20 3a 20 28 28 42 69 74 6d 61 73 6b 29 31  1) : ((Bitmask)1
38c39 29 3c 3c 69 43 6f 6c 3b 0a 20 20 20 20 20 20 74  )<<iCol;.      t
38c3a 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d 42  estcase( iCol==B
38c3b 4d 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  MS );.      test
38c3c 63 61 73 65 28 20 69 43 6f 6c 3d 3d 42 4d 53 2d  case( iCol==BMS-
38c3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28  1 );.      if( (
38c3e 69 64 78 43 6f 6c 73 20 26 20 63 4d 61 73 6b 29  idxCols & cMask)
38c3f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6e  ==0 ){.        n
38c40 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 20 20  Column++;.      
38c41 20 20 69 64 78 43 6f 6c 73 20 7c 3d 20 63 4d 61    idxCols |= cMa
38c42 73 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  sk;.      }.    
38c43 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  }.  }.  assert( 
38c44 6e 43 6f 6c 75 6d 6e 3e 30 20 29 3b 0a 20 20 70  nColumn>0 );.  p
38c45 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 45 71 20  Level->plan.nEq 
38c46 3d 20 6e 43 6f 6c 75 6d 6e 3b 0a 0a 20 20 2f 2a  = nColumn;..  /*
38c47 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65   Count the numbe
38c48 72 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20  r of additional 
38c49 63 6f 6c 75 6d 6e 73 20 6e 65 65 64 65 64 20 74  columns needed t
38c4a 6f 20 63 72 65 61 74 65 20 61 0a 20 20 2a 2a 20  o create a.  ** 
38c4b 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 78 2e 20  covering index. 
38c4c 20 41 20 22 63 6f 76 65 72 69 6e 67 20 69 6e 64   A "covering ind
38c4d 65 78 22 20 69 73 20 61 6e 20 69 6e 64 65 78 20  ex" is an index 
38c4e 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 6c  that contains al
38c4f 6c 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 73 20 74  l.  ** columns t
38c50 68 61 74 20 61 72 65 20 6e 65 65 64 65 64 20 62  hat are needed b
38c51 79 20 74 68 65 20 71 75 65 72 79 2e 20 20 57 69  y the query.  Wi
38c52 74 68 20 61 20 63 6f 76 65 72 69 6e 67 20 69 6e  th a covering in
38c53 64 65 78 2c 20 74 68 65 0a 20 20 2a 2a 20 6f 72  dex, the.  ** or
38c54 69 67 69 6e 61 6c 20 74 61 62 6c 65 20 6e 65 76  iginal table nev
38c55 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61  er needs to be a
38c56 63 63 65 73 73 65 64 2e 20 20 41 75 74 6f 6d 61  ccessed.  Automa
38c57 74 69 63 20 69 6e 64 69 63 65 73 20 6d 75 73 74  tic indices must
38c58 0a 20 20 2a 2a 20 62 65 20 61 20 63 6f 76 65 72  .  ** be a cover
38c59 69 6e 67 20 69 6e 64 65 78 20 62 65 63 61 75 73  ing index becaus
38c5a 65 20 74 68 65 20 69 6e 64 65 78 20 77 69 6c 6c  e the index will
38c5b 20 6e 6f 74 20 62 65 20 75 70 64 61 74 65 64 20   not be updated 
38c5c 69 66 20 74 68 65 0a 20 20 2a 2a 20 6f 72 69 67  if the.  ** orig
38c5d 69 6e 61 6c 20 74 61 62 6c 65 20 63 68 61 6e 67  inal table chang
38c5e 65 73 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78  es and the index
38c5f 20 61 6e 64 20 74 61 62 6c 65 20 63 61 6e 6e 6f   and table canno
38c60 74 20 62 6f 74 68 20 62 65 20 75 73 65 64 0a 20  t both be used. 
38c61 20 2a 2a 20 69 66 20 74 68 65 79 20 67 6f 20 6f   ** if they go o
38c62 75 74 20 6f 66 20 73 79 6e 63 2e 0a 20 20 2a 2f  ut of sync..  */
38c63 0a 20 20 65 78 74 72 61 43 6f 6c 73 20 3d 20 70  .  extraCols = p
38c64 53 72 63 2d 3e 63 6f 6c 55 73 65 64 20 26 20 28  Src->colUsed & (
38c65 7e 69 64 78 43 6f 6c 73 20 7c 20 28 28 28 42 69  ~idxCols | (((Bi
38c66 74 6d 61 73 6b 29 31 29 3c 3c 28 42 4d 53 2d 31  tmask)1)<<(BMS-1
38c67 29 29 29 3b 0a 20 20 6d 78 42 69 74 43 6f 6c 20  )));.  mxBitCol 
38c68 3d 20 28 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20  = (pTable->nCol 
38c69 3e 3d 20 42 4d 53 2d 31 29 20 3f 20 42 4d 53 2d  >= BMS-1) ? BMS-
38c6a 31 20 3a 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c  1 : pTable->nCol
38c6b 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 54  ;.  testcase( pT
38c6c 61 62 6c 65 2d 3e 6e 43 6f 6c 3d 3d 42 4d 53 2d  able->nCol==BMS-
38c6d 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  1 );.  testcase(
38c6e 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3d 3d 42   pTable->nCol==B
38c6f 4d 53 2d 32 20 29 3b 0a 20 20 66 6f 72 28 69 3d  MS-2 );.  for(i=
38c70 30 3b 20 69 3c 6d 78 42 69 74 43 6f 6c 3b 20 69  0; i<mxBitCol; i
38c71 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 65 78 74  ++){.    if( ext
38c72 72 61 43 6f 6c 73 20 26 20 28 28 28 42 69 74 6d  raCols & (((Bitm
38c73 61 73 6b 29 31 29 3c 3c 69 29 20 29 20 6e 43 6f  ask)1)<<i) ) nCo
38c74 6c 75 6d 6e 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66  lumn++;.  }.  if
38c75 28 20 70 53 72 63 2d 3e 63 6f 6c 55 73 65 64 20  ( pSrc->colUsed 
38c76 26 20 28 28 28 42 69 74 6d 61 73 6b 29 31 29 3c  & (((Bitmask)1)<
38c77 3c 28 42 4d 53 2d 31 29 29 20 29 7b 0a 20 20 20  <(BMS-1)) ){.   
38c78 20 6e 43 6f 6c 75 6d 6e 20 2b 3d 20 70 54 61 62   nColumn += pTab
38c79 6c 65 2d 3e 6e 43 6f 6c 20 2d 20 42 4d 53 20 2b  le->nCol - BMS +
38c7a 20 31 3b 0a 20 20 7d 0a 20 20 70 4c 65 76 65 6c   1;.  }.  pLevel
38c7b 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 7c  ->plan.wsFlags |
38c7c 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45  = WHERE_COLUMN_E
38c7d 51 20 7c 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e  Q | WHERE_IDX_ON
38c7e 4c 59 20 7c 20 57 4f 5f 45 51 3b 0a 0a 20 20 2f  LY | WO_EQ;..  /
38c7f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20  * Construct the 
38c80 49 6e 64 65 78 20 6f 62 6a 65 63 74 20 74 6f 20  Index object to 
38c81 64 65 73 63 72 69 62 65 20 74 68 69 73 20 69 6e  describe this in
38c82 64 65 78 20 2a 2f 0a 20 20 6e 42 79 74 65 20 3d  dex */.  nByte =
38c83 20 73 69 7a 65 6f 66 28 49 6e 64 65 78 29 3b 0a   sizeof(Index);.
38c84 20 20 6e 42 79 74 65 20 2b 3d 20 6e 43 6f 6c 75    nByte += nColu
38c85 6d 6e 2a 73 69 7a 65 6f 66 28 69 6e 74 29 3b 20  mn*sizeof(int); 
38c86 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 69 43      /* Index.aiC
38c87 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 6e 42 79 74 65  olumn */.  nByte
38c88 20 2b 3d 20 6e 43 6f 6c 75 6d 6e 2a 73 69 7a 65   += nColumn*size
38c89 6f 66 28 63 68 61 72 2a 29 3b 20 20 20 2f 2a 20  of(char*);   /* 
38c8a 49 6e 64 65 78 2e 61 7a 43 6f 6c 6c 20 2a 2f 0a  Index.azColl */.
38c8b 20 20 6e 42 79 74 65 20 2b 3d 20 6e 43 6f 6c 75    nByte += nColu
38c8c 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
38c8d 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 53 6f      /* Index.aSo
38c8e 72 74 4f 72 64 65 72 20 2a 2f 0a 20 20 70 49 64  rtOrder */.  pId
38c8f 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  x = sqlite3DbMal
38c90 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e  locZero(pParse->
38c91 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 69 66  db, nByte);.  if
38c92 28 20 70 49 64 78 3d 3d 30 20 29 20 72 65 74 75  ( pIdx==0 ) retu
38c93 72 6e 3b 0a 20 20 70 4c 65 76 65 6c 2d 3e 70 6c  rn;.  pLevel->pl
38c94 61 6e 2e 75 2e 70 49 64 78 20 3d 20 70 49 64 78  an.u.pIdx = pIdx
38c95 3b 0a 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c  ;.  pIdx->azColl
38c96 20 3d 20 28 63 68 61 72 2a 2a 29 26 70 49 64 78   = (char**)&pIdx
38c97 5b 31 5d 3b 0a 20 20 70 49 64 78 2d 3e 61 69 43  [1];.  pIdx->aiC
38c98 6f 6c 75 6d 6e 20 3d 20 28 69 6e 74 2a 29 26 70  olumn = (int*)&p
38c99 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e 43 6f 6c  Idx->azColl[nCol
38c9a 75 6d 6e 5d 3b 0a 20 20 70 49 64 78 2d 3e 61 53  umn];.  pIdx->aS
38c9b 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29  ortOrder = (u8*)
38c9c 26 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  &pIdx->aiColumn[
38c9d 6e 43 6f 6c 75 6d 6e 5d 3b 0a 20 20 70 49 64 78  nColumn];.  pIdx
38c9e 2d 3e 7a 4e 61 6d 65 20 3d 20 22 61 75 74 6f 2d  ->zName = "auto-
38c9f 69 6e 64 65 78 22 3b 0a 20 20 70 49 64 78 2d 3e  index";.  pIdx->
38ca0 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 43 6f 6c 75 6d  nColumn = nColum
38ca1 6e 3b 0a 20 20 70 49 64 78 2d 3e 70 54 61 62 6c  n;.  pIdx->pTabl
38ca2 65 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 6e 20  e = pTable;.  n 
38ca3 3d 20 30 3b 0a 20 20 69 64 78 43 6f 6c 73 20 3d  = 0;.  idxCols =
38ca4 20 30 3b 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d   0;.  for(pTerm=
38ca5 70 57 43 2d 3e 61 3b 20 70 54 65 72 6d 3c 70 57  pWC->a; pTerm<pW
38ca6 43 45 6e 64 3b 20 70 54 65 72 6d 2b 2b 29 7b 0a  CEnd; pTerm++){.
38ca7 20 20 20 20 69 66 28 20 74 65 72 6d 43 61 6e 44      if( termCanD
38ca8 72 69 76 65 49 6e 64 65 78 28 70 54 65 72 6d 2c  riveIndex(pTerm,
38ca9 20 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 29   pSrc, notReady)
38caa 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43   ){.      int iC
38cab 6f 6c 20 3d 20 70 54 65 72 6d 2d 3e 75 2e 6c 65  ol = pTerm->u.le
38cac 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20  ftColumn;.      
38cad 42 69 74 6d 61 73 6b 20 63 4d 61 73 6b 20 3d 20  Bitmask cMask = 
38cae 69 43 6f 6c 3e 3d 42 4d 53 20 3f 20 28 28 42 69  iCol>=BMS ? ((Bi
38caf 74 6d 61 73 6b 29 31 29 3c 3c 28 42 4d 53 2d 31  tmask)1)<<(BMS-1
38cb0 29 20 3a 20 28 28 42 69 74 6d 61 73 6b 29 31 29  ) : ((Bitmask)1)
38cb1 3c 3c 69 43 6f 6c 3b 0a 20 20 20 20 20 20 69 66  <<iCol;.      if
38cb2 28 20 28 69 64 78 43 6f 6c 73 20 26 20 63 4d 61  ( (idxCols & cMa
38cb3 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  sk)==0 ){.      
38cb4 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70 54 65    Expr *pX = pTe
38cb5 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20  rm->pExpr;.     
38cb6 20 20 20 69 64 78 43 6f 6c 73 20 7c 3d 20 63 4d     idxCols |= cM
38cb7 61 73 6b 3b 0a 20 20 20 20 20 20 20 20 70 49 64  ask;.        pId
38cb8 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 5d 20 3d  x->aiColumn[n] =
38cb9 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f   pTerm->u.leftCo
38cba 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 70 43  lumn;.        pC
38cbb 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 6e  oll = sqlite3Bin
38cbc 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65  aryCompareCollSe
38cbd 71 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c  q(pParse, pX->pL
38cbe 65 66 74 2c 20 70 58 2d 3e 70 52 69 67 68 74 29  eft, pX->pRight)
38cbf 3b 0a 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e  ;.        pIdx->
38cc0 61 7a 43 6f 6c 6c 5b 6e 5d 20 3d 20 41 4c 57 41  azColl[n] = ALWA
38cc1 59 53 28 70 43 6f 6c 6c 29 20 3f 20 70 43 6f 6c  YS(pColl) ? pCol
38cc2 6c 2d 3e 7a 4e 61 6d 65 20 3a 20 22 42 49 4e 41  l->zName : "BINA
38cc3 52 59 22 3b 0a 20 20 20 20 20 20 20 20 6e 2b 2b  RY";.        n++
38cc4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
38cc5 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 75    }.  assert( (u
38cc6 33 32 29 6e 3d 3d 70 4c 65 76 65 6c 2d 3e 70 6c  32)n==pLevel->pl
38cc7 61 6e 2e 6e 45 71 20 29 3b 0a 0a 20 20 2f 2a 20  an.nEq );..  /* 
38cc8 41 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 63  Add additional c
38cc9 6f 6c 75 6d 6e 73 20 6e 65 65 64 65 64 20 74 6f  olumns needed to
38cca 20 6d 61 6b 65 20 74 68 65 20 61 75 74 6f 6d 61   make the automa
38ccb 74 69 63 20 69 6e 64 65 78 20 69 6e 74 6f 0a 20  tic index into. 
38ccc 20 2a 2a 20 61 20 63 6f 76 65 72 69 6e 67 20 69   ** a covering i
38ccd 6e 64 65 78 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  ndex */.  for(i=
38cce 30 3b 20 69 3c 6d 78 42 69 74 43 6f 6c 3b 20 69  0; i<mxBitCol; i
38ccf 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 65 78 74  ++){.    if( ext
38cd0 72 61 43 6f 6c 73 20 26 20 28 28 28 42 69 74 6d  raCols & (((Bitm
38cd1 61 73 6b 29 31 29 3c 3c 69 29 20 29 7b 0a 20 20  ask)1)<<i) ){.  
38cd2 20 20 20 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75      pIdx->aiColu
38cd3 6d 6e 5b 6e 5d 20 3d 20 69 3b 0a 20 20 20 20 20  mn[n] = i;.     
38cd4 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e 5d   pIdx->azColl[n]
38cd5 20 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20 20 20   = "BINARY";.   
38cd6 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20     n++;.    }.  
38cd7 7d 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 63 6f  }.  if( pSrc->co
38cd8 6c 55 73 65 64 20 26 20 28 28 28 42 69 74 6d 61  lUsed & (((Bitma
38cd9 73 6b 29 31 29 3c 3c 28 42 4d 53 2d 31 29 29 20  sk)1)<<(BMS-1)) 
38cda 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 42 4d 53  ){.    for(i=BMS
38cdb 2d 31 3b 20 69 3c 70 54 61 62 6c 65 2d 3e 6e 43  -1; i<pTable->nC
38cdc 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
38cdd 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e  pIdx->aiColumn[n
38cde 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 70 49 64  ] = i;.      pId
38cdf 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e 5d 20 3d 20 22  x->azColl[n] = "
38ce0 42 49 4e 41 52 59 22 3b 0a 20 20 20 20 20 20 6e  BINARY";.      n
38ce1 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ++;.    }.  }.  
38ce2 61 73 73 65 72 74 28 20 6e 3d 3d 6e 43 6f 6c 75  assert( n==nColu
38ce3 6d 6e 20 29 3b 0a 0a 20 20 2f 2a 20 43 72 65 61  mn );..  /* Crea
38ce4 74 65 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63  te the automatic
38ce5 20 69 6e 64 65 78 20 2a 2f 0a 20 20 70 4b 65 79   index */.  pKey
38ce6 69 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 49 6e  info = sqlite3In
38ce7 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73  dexKeyinfo(pPars
38ce8 65 2c 20 70 49 64 78 29 3b 0a 20 20 61 73 73 65  e, pIdx);.  asse
38ce9 72 74 28 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  rt( pLevel->iIdx
38cea 43 75 72 3e 3d 30 20 29 3b 0a 20 20 73 71 6c 69  Cur>=0 );.  sqli
38ceb 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
38cec 20 4f 50 5f 4f 70 65 6e 41 75 74 6f 69 6e 64 65   OP_OpenAutoinde
38ced 78 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43  x, pLevel->iIdxC
38cee 75 72 2c 20 6e 43 6f 6c 75 6d 6e 2b 31 2c 20 30  ur, nColumn+1, 0
38cef 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
38cf0 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
38cf1 79 69 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46  yinfo, P4_KEYINF
38cf2 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 56 64  O_HANDOFF);.  Vd
38cf3 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 66  beComment((v, "f
38cf4 6f 72 20 25 73 22 2c 20 70 54 61 62 6c 65 2d 3e  or %s", pTable->
38cf5 7a 4e 61 6d 65 29 29 3b 0a 0a 20 20 2f 2a 20 46  zName));..  /* F
38cf6 69 6c 6c 20 74 68 65 20 61 75 74 6f 6d 61 74 69  ill the automati
38cf7 63 20 69 6e 64 65 78 20 77 69 74 68 20 63 6f 6e  c index with con
38cf8 74 65 6e 74 20 2a 2f 0a 20 20 61 64 64 72 54 6f  tent */.  addrTo
38cf9 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  p = sqlite3VdbeA
38cfa 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 77 69  ddOp1(v, OP_Rewi
38cfb 6e 64 2c 20 70 4c 65 76 65 6c 2d 3e 69 54 61 62  nd, pLevel->iTab
38cfc 43 75 72 29 3b 0a 20 20 72 65 67 52 65 63 6f 72  Cur);.  regRecor
38cfd 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  d = sqlite3GetTe
38cfe 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
38cff 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
38d00 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73 65 2c  IndexKey(pParse,
38d01 20 70 49 64 78 2c 20 70 4c 65 76 65 6c 2d 3e 69   pIdx, pLevel->i
38d02 54 61 62 43 75 72 2c 20 72 65 67 52 65 63 6f 72  TabCur, regRecor
38d03 64 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  d, 1);.  sqlite3
38d04 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
38d05 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 4c 65 76  _IdxInsert, pLev
38d06 65 6c 2d 3e 69 49 64 78 43 75 72 2c 20 72 65 67  el->iIdxCur, reg
38d07 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74  Record);.  sqlit
38d08 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
38d09 2c 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b  , OPFLAG_USESEEK
38d0a 52 45 53 55 4c 54 29 3b 0a 20 20 73 71 6c 69 74  RESULT);.  sqlit
38d0b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
38d0c 4f 50 5f 4e 65 78 74 2c 20 70 4c 65 76 65 6c 2d  OP_Next, pLevel-
38d0d 3e 69 54 61 62 43 75 72 2c 20 61 64 64 72 54 6f  >iTabCur, addrTo
38d0e 70 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  p+1);.  sqlite3V
38d0f 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53  dbeChangeP5(v, S
38d10 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
38d11 5f 41 55 54 4f 49 4e 44 45 58 29 3b 0a 20 20 73  _AUTOINDEX);.  s
38d12 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
38d13 72 65 28 76 2c 20 61 64 64 72 54 6f 70 29 3b 0a  re(v, addrTop);.
38d14 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
38d15 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
38d16 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 0a 20  regRecord);.  . 
38d17 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 77 68   /* Jump here wh
38d18 65 6e 20 73 6b 69 70 70 69 6e 67 20 74 68 65 20  en skipping the 
38d19 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a  initialization *
38d1a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a  /.  sqlite3VdbeJ
38d1b 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 49  umpHere(v, addrI
38d1c 6e 69 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  nit);.}.#endif /
38d1d 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  * SQLITE_OMIT_AU
38d1e 54 4f 4d 41 54 49 43 5f 49 4e 44 45 58 20 2a 2f  TOMATIC_INDEX */
38d1f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
38d20 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
38d21 4c 45 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  LE./*.** Allocat
38d22 65 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 61  e and populate a
38d23 6e 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  n sqlite3_index_
38d24 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20  info structure. 
38d25 49 74 20 69 73 20 74 68 65 20 0a 2a 2a 20 72 65  It is the .** re
38d26 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20  sponsibility of 
38d27 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 65 76  the caller to ev
38d28 65 6e 74 75 61 6c 6c 79 20 72 65 6c 65 61 73 65  entually release
38d29 20 74 68 65 20 73 74 72 75 63 74 75 72 65 0a 2a   the structure.*
38d2a 2a 20 62 79 20 70 61 73 73 69 6e 67 20 74 68 65  * by passing the
38d2b 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65   pointer returne
38d2c 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
38d2d 6f 6e 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72  on to sqlite3_fr
38d2e 65 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ee()..*/.static 
38d2f 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
38d30 66 6f 20 2a 61 6c 6c 6f 63 61 74 65 49 6e 64 65  fo *allocateInde
38d31 78 49 6e 66 6f 28 57 68 65 72 65 42 65 73 74 49  xInfo(WhereBestI
38d32 64 78 20 2a 70 29 7b 0a 20 20 50 61 72 73 65 20  dx *p){.  Parse 
38d33 2a 70 50 61 72 73 65 20 3d 20 70 2d 3e 70 50 61  *pParse = p->pPa
38d34 72 73 65 3b 20 0a 20 20 57 68 65 72 65 43 6c 61  rse; .  WhereCla
38d35 75 73 65 20 2a 70 57 43 20 3d 20 70 2d 3e 70 57  use *pWC = p->pW
38d36 43 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  C;.  struct SrcL
38d37 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 20 3d  ist_item *pSrc =
38d38 20 70 2d 3e 70 53 72 63 3b 0a 20 20 45 78 70 72   p->pSrc;.  Expr
38d39 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 20 3d  List *pOrderBy =
38d3a 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20   p->pOrderBy;.  
38d3b 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 6e 74 20  int i, j;.  int 
38d3c 6e 54 65 72 6d 3b 0a 20 20 73 74 72 75 63 74 20  nTerm;.  struct 
38d3d 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
38d3e 6e 73 74 72 61 69 6e 74 20 2a 70 49 64 78 43 6f  nstraint *pIdxCo
38d3f 6e 73 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c  ns;.  struct sql
38d40 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72  ite3_index_order
38d41 62 79 20 2a 70 49 64 78 4f 72 64 65 72 42 79 3b  by *pIdxOrderBy;
38d42 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  .  struct sqlite
38d43 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
38d44 6e 74 5f 75 73 61 67 65 20 2a 70 55 73 61 67 65  nt_usage *pUsage
38d45 3b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  ;.  WhereTerm *p
38d46 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6e 4f 72 64  Term;.  int nOrd
38d47 65 72 42 79 3b 0a 20 20 73 71 6c 69 74 65 33 5f  erBy;.  sqlite3_
38d48 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78  index_info *pIdx
38d49 49 6e 66 6f 3b 0a 0a 20 20 57 48 45 52 45 54 52  Info;..  WHERETR
38d4a 41 43 45 28 28 22 52 65 63 6f 6d 70 75 74 69 6e  ACE(("Recomputin
38d4b 67 20 69 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72  g index info for
38d4c 20 25 73 2e 2e 2e 5c 6e 22 2c 20 70 53 72 63 2d   %s...\n", pSrc-
38d4d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a  >pTab->zName));.
38d4e 0a 20 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20  .  /* Count the 
38d4f 6e 75 6d 62 65 72 20 6f 66 20 70 6f 73 73 69 62  number of possib
38d50 6c 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  le WHERE clause 
38d51 63 6f 6e 73 74 72 61 69 6e 74 73 20 72 65 66 65  constraints refe
38d52 72 72 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 74 68  rring.  ** to th
38d53 69 73 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  is virtual table
38d54 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6e 54 65 72   */.  for(i=nTer
38d55 6d 3d 30 2c 20 70 54 65 72 6d 3d 70 57 43 2d 3e  m=0, pTerm=pWC->
38d56 61 3b 20 69 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b  a; i<pWC->nTerm;
38d57 20 69 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a   i++, pTerm++){.
38d58 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c      if( pTerm->l
38d59 65 66 74 43 75 72 73 6f 72 20 21 3d 20 70 53 72  eftCursor != pSr
38d5a 63 2d 3e 69 43 75 72 73 6f 72 20 29 20 63 6f 6e  c->iCursor ) con
38d5b 74 69 6e 75 65 3b 0a 20 20 20 20 61 73 73 65 72  tinue;.    asser
38d5c 74 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72  t( (pTerm->eOper
38d5d 61 74 6f 72 26 28 70 54 65 72 6d 2d 3e 65 4f 70  ator&(pTerm->eOp
38d5e 65 72 61 74 6f 72 2d 31 29 29 3d 3d 30 20 29 3b  erator-1))==0 );
38d5f 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
38d60 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
38d61 3d 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20 74 65  =WO_IN );.    te
38d62 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65  stcase( pTerm->e
38d63 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e  Operator==WO_ISN
38d64 55 4c 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ULL );.    if( p
38d65 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
38d66 26 20 28 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55  & (WO_IN|WO_ISNU
38d67 4c 4c 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  LL) ) continue;.
38d68 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 77      if( pTerm->w
38d69 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 4e  tFlags & TERM_VN
38d6a 55 4c 4c 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ULL ) continue;.
38d6b 20 20 20 20 6e 54 65 72 6d 2b 2b 3b 0a 20 20 7d      nTerm++;.  }
38d6c 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 4f 52  ..  /* If the OR
38d6d 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 63 6f  DER BY clause co
38d6e 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 63 6f 6c 75  ntains only colu
38d6f 6d 6e 73 20 69 6e 20 74 68 65 20 63 75 72 72 65  mns in the curre
38d70 6e 74 20 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c  nt .  ** virtual
38d71 20 74 61 62 6c 65 20 74 68 65 6e 20 61 6c 6c 6f   table then allo
38d72 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 74  cate space for t
38d73 68 65 20 61 4f 72 64 65 72 42 79 20 70 61 72 74  he aOrderBy part
38d74 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c   of.  ** the sql
38d75 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
38d76 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2f 0a  structure..  */.
38d77 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a    nOrderBy = 0;.
38d78 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29    if( pOrderBy )
38d79 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 4f  {.    int n = pO
38d7a 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20  rderBy->nExpr;. 
38d7b 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b     for(i=0; i<n;
38d7c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70   i++){.      Exp
38d7d 72 20 2a 70 45 78 70 72 20 3d 20 70 4f 72 64 65  r *pExpr = pOrde
38d7e 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  rBy->a[i].pExpr;
38d7f 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72  .      if( pExpr
38d80 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op!=TK_COLUMN 
38d81 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  || pExpr->iTable
38d82 21 3d 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 20  !=pSrc->iCursor 
38d83 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20  ) break;.    }. 
38d84 20 20 20 69 66 28 20 69 3d 3d 6e 29 7b 0a 20 20     if( i==n){.  
38d85 20 20 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 6e      nOrderBy = n
38d86 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
38d87 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 73  * Allocate the s
38d88 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
38d89 6f 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2f  o structure.  */
38d8a 0a 20 20 70 49 64 78 49 6e 66 6f 20 3d 20 73 71  .  pIdxInfo = sq
38d8b 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
38d8c 6f 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 73 69  o(pParse->db, si
38d8d 7a 65 6f 66 28 2a 70 49 64 78 49 6e 66 6f 29 0a  zeof(*pIdxInfo).
38d8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38d8f 20 20 20 20 20 20 20 20 20 20 20 2b 20 28 73 69             + (si
38d90 7a 65 6f 66 28 2a 70 49 64 78 43 6f 6e 73 29 20  zeof(*pIdxCons) 
38d91 2b 20 73 69 7a 65 6f 66 28 2a 70 55 73 61 67 65  + sizeof(*pUsage
38d92 29 29 2a 6e 54 65 72 6d 0a 20 20 20 20 20 20 20  ))*nTerm.       
38d93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38d94 20 20 20 20 2b 20 73 69 7a 65 6f 66 28 2a 70 49      + sizeof(*pI
38d95 64 78 4f 72 64 65 72 42 79 29 2a 6e 4f 72 64 65  dxOrderBy)*nOrde
38d96 72 42 79 20 29 3b 0a 20 20 69 66 28 20 70 49 64  rBy );.  if( pId
38d97 78 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20  xInfo==0 ){.    
38d98 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
38d99 70 50 61 72 73 65 2c 20 22 6f 75 74 20 6f 66 20  pParse, "out of 
38d9a 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 20 20 2f 2a  memory");.    /*
38d9b 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61   (double)0 In ca
38d9c 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49  se of SQLITE_OMI
38d9d 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
38d9e 2e 2e 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ... */.    retur
38d9f 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  n 0;.  }..  /* I
38da0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 73 74  nitialize the st
38da1 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 73 71  ructure.  The sq
38da2 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
38da3 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
38da4 69 6e 73 0a 20 20 2a 2a 20 6d 61 6e 79 20 66 69  ins.  ** many fi
38da5 65 6c 64 73 20 74 68 61 74 20 61 72 65 20 64 65  elds that are de
38da6 63 6c 61 72 65 64 20 22 63 6f 6e 73 74 22 20 74  clared "const" t
38da7 6f 20 70 72 65 76 65 6e 74 20 78 42 65 73 74 49  o prevent xBestI
38da8 6e 64 65 78 20 66 72 6f 6d 0a 20 20 2a 2a 20 63  ndex from.  ** c
38da9 68 61 6e 67 69 6e 67 20 74 68 65 6d 2e 20 20 57  hanging them.  W
38daa 65 20 68 61 76 65 20 74 6f 20 64 6f 20 73 6f 6d  e have to do som
38dab 65 20 66 75 6e 6b 79 20 63 61 73 74 69 6e 67 20  e funky casting 
38dac 69 6e 20 6f 72 64 65 72 20 74 6f 0a 20 20 2a 2a  in order to.  **
38dad 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 6f 73   initialize thos
38dae 65 20 66 69 65 6c 64 73 2e 0a 20 20 2a 2f 0a 20  e fields..  */. 
38daf 20 70 49 64 78 43 6f 6e 73 20 3d 20 28 73 74 72   pIdxCons = (str
38db0 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
38db1 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 29 26 70  x_constraint*)&p
38db2 49 64 78 49 6e 66 6f 5b 31 5d 3b 0a 20 20 70 49  IdxInfo[1];.  pI
38db3 64 78 4f 72 64 65 72 42 79 20 3d 20 28 73 74 72  dxOrderBy = (str
38db4 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
38db5 78 5f 6f 72 64 65 72 62 79 2a 29 26 70 49 64 78  x_orderby*)&pIdx
38db6 43 6f 6e 73 5b 6e 54 65 72 6d 5d 3b 0a 20 20 70  Cons[nTerm];.  p
38db7 55 73 61 67 65 20 3d 20 28 73 74 72 75 63 74 20  Usage = (struct 
38db8 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
38db9 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65 2a 29  nstraint_usage*)
38dba 26 70 49 64 78 4f 72 64 65 72 42 79 5b 6e 4f 72  &pIdxOrderBy[nOr
38dbb 64 65 72 42 79 5d 3b 0a 20 20 2a 28 69 6e 74 2a  derBy];.  *(int*
38dbc 29 26 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e  )&pIdxInfo->nCon
38dbd 73 74 72 61 69 6e 74 20 3d 20 6e 54 65 72 6d 3b  straint = nTerm;
38dbe 0a 20 20 2a 28 69 6e 74 2a 29 26 70 49 64 78 49  .  *(int*)&pIdxI
38dbf 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20  nfo->nOrderBy = 
38dc0 6e 4f 72 64 65 72 42 79 3b 0a 20 20 2a 28 73 74  nOrderBy;.  *(st
38dc1 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
38dc2 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a 29  ex_constraint**)
38dc3 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73  &pIdxInfo->aCons
38dc4 74 72 61 69 6e 74 20 3d 20 70 49 64 78 43 6f 6e  traint = pIdxCon
38dc5 73 3b 0a 20 20 2a 28 73 74 72 75 63 74 20 73 71  s;.  *(struct sq
38dc6 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65  lite3_index_orde
38dc7 72 62 79 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d  rby**)&pIdxInfo-
38dc8 3e 61 4f 72 64 65 72 42 79 20 3d 20 70 49 64 78  >aOrderBy = pIdx
38dc9 4f 72 64 65 72 42 79 3b 0a 20 20 2a 28 73 74 72  OrderBy;.  *(str
38dca 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
38dcb 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61  x_constraint_usa
38dcc 67 65 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e  ge**)&pIdxInfo->
38dcd 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
38dce 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   =.             
38dcf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38dd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38dd2 20 20 20 20 20 20 70 55 73 61 67 65 3b 0a 0a 20        pUsage;.. 
38dd3 20 66 6f 72 28 69 3d 6a 3d 30 2c 20 70 54 65 72   for(i=j=0, pTer
38dd4 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c 70 57 43 2d  m=pWC->a; i<pWC-
38dd5 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 54 65  >nTerm; i++, pTe
38dd6 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70  rm++){.    if( p
38dd7 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
38dd8 20 21 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f   != pSrc->iCurso
38dd9 72 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  r ) continue;.  
38dda 20 20 61 73 73 65 72 74 28 20 28 70 54 65 72 6d    assert( (pTerm
38ddb 2d 3e 65 4f 70 65 72 61 74 6f 72 26 28 70 54 65  ->eOperator&(pTe
38ddc 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 2d 31 29  rm->eOperator-1)
38ddd 29 3d 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74  )==0 );.    test
38dde 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70  case( pTerm->eOp
38ddf 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20 29 3b  erator==WO_IN );
38de0 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
38de1 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
38de2 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20  =WO_ISNULL );.  
38de3 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70    if( pTerm->eOp
38de4 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 4e 7c  erator & (WO_IN|
38de5 57 4f 5f 49 53 4e 55 4c 4c 29 20 29 20 63 6f 6e  WO_ISNULL) ) con
38de6 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70  tinue;.    if( p
38de7 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
38de8 54 45 52 4d 5f 56 4e 55 4c 4c 20 29 20 63 6f 6e  TERM_VNULL ) con
38de9 74 69 6e 75 65 3b 0a 20 20 20 20 70 49 64 78 43  tinue;.    pIdxC
38dea 6f 6e 73 5b 6a 5d 2e 69 43 6f 6c 75 6d 6e 20 3d  ons[j].iColumn =
38deb 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f   pTerm->u.leftCo
38dec 6c 75 6d 6e 3b 0a 20 20 20 20 70 49 64 78 43 6f  lumn;.    pIdxCo
38ded 6e 73 5b 6a 5d 2e 69 54 65 72 6d 4f 66 66 73 65  ns[j].iTermOffse
38dee 74 20 3d 20 69 3b 0a 20 20 20 20 70 49 64 78 43  t = i;.    pIdxC
38def 6f 6e 73 5b 6a 5d 2e 6f 70 20 3d 20 28 75 38 29  ons[j].op = (u8)
38df0 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
38df1 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72  ;.    /* The dir
38df2 65 63 74 20 61 73 73 69 67 6e 6d 65 6e 74 20 69  ect assignment i
38df3 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6c  n the previous l
38df4 69 6e 65 20 69 73 20 70 6f 73 73 69 62 6c 65 20  ine is possible 
38df5 6f 6e 6c 79 20 62 65 63 61 75 73 65 0a 20 20 20  only because.   
38df6 20 2a 2a 20 74 68 65 20 57 4f 5f 20 61 6e 64 20   ** the WO_ and 
38df7 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
38df8 53 54 52 41 49 4e 54 5f 20 63 6f 64 65 73 20 61  STRAINT_ codes a
38df9 72 65 20 69 64 65 6e 74 69 63 61 6c 2e 20 20 54  re identical.  T
38dfa 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77  he.    ** follow
38dfb 69 6e 67 20 61 73 73 65 72 74 73 20 76 65 72 69  ing asserts veri
38dfc 66 79 20 74 68 69 73 20 66 61 63 74 2e 20 2a 2f  fy this fact. */
38dfd 0a 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f  .    assert( WO_
38dfe 45 51 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58  EQ==SQLITE_INDEX
38dff 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 29  _CONSTRAINT_EQ )
38e00 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 57 4f  ;.    assert( WO
38e01 5f 4c 54 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45  _LT==SQLITE_INDE
38e02 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20  X_CONSTRAINT_LT 
38e03 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 57  );.    assert( W
38e04 4f 5f 4c 45 3d 3d 53 51 4c 49 54 45 5f 49 4e 44  O_LE==SQLITE_IND
38e05 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45  EX_CONSTRAINT_LE
38e06 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
38e07 57 4f 5f 47 54 3d 3d 53 51 4c 49 54 45 5f 49 4e  WO_GT==SQLITE_IN
38e08 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47  DEX_CONSTRAINT_G
38e09 54 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  T );.    assert(
38e0a 20 57 4f 5f 47 45 3d 3d 53 51 4c 49 54 45 5f 49   WO_GE==SQLITE_I
38e0b 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
38e0c 47 45 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  GE );.    assert
38e0d 28 20 57 4f 5f 4d 41 54 43 48 3d 3d 53 51 4c 49  ( WO_MATCH==SQLI
38e0e 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
38e0f 49 4e 54 5f 4d 41 54 43 48 20 29 3b 0a 20 20 20  INT_MATCH );.   
38e10 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e   assert( pTerm->
38e11 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f  eOperator & (WO_
38e12 45 51 7c 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57  EQ|WO_LT|WO_LE|W
38e13 4f 5f 47 54 7c 57 4f 5f 47 45 7c 57 4f 5f 4d 41  O_GT|WO_GE|WO_MA
38e14 54 43 48 29 20 29 3b 0a 20 20 20 20 6a 2b 2b 3b  TCH) );.    j++;
38e15 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
38e16 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b 29  i<nOrderBy; i++)
38e17 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70  {.    Expr *pExp
38e18 72 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  r = pOrderBy->a[
38e19 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 49  i].pExpr;.    pI
38e1a 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e 69 43 6f  dxOrderBy[i].iCo
38e1b 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43  lumn = pExpr->iC
38e1c 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 49 64 78 4f  olumn;.    pIdxO
38e1d 72 64 65 72 42 79 5b 69 5d 2e 64 65 73 63 20 3d  rderBy[i].desc =
38e1e 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e   pOrderBy->a[i].
38e1f 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 7d 0a 0a  sortOrder;.  }..
38e20 20 20 72 65 74 75 72 6e 20 70 49 64 78 49 6e 66    return pIdxInf
38e21 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  o;.}../*.** The 
38e22 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 72 65 66  table object ref
38e23 65 72 65 6e 63 65 20 70 61 73 73 65 64 20 61 73  erence passed as
38e24 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
38e25 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
38e26 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 72 65  ction.** must re
38e27 70 72 65 73 65 6e 74 20 61 20 76 69 72 74 75 61  present a virtua
38e28 6c 20 74 61 62 6c 65 2e 20 54 68 69 73 20 66 75  l table. This fu
38e29 6e 63 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 74  nction invokes t
38e2a 68 65 20 78 42 65 73 74 49 6e 64 65 78 28 29 0a  he xBestIndex().
38e2b 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  ** method of the
38e2c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 77   virtual table w
38e2d 69 74 68 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ith the sqlite3_
38e2e 69 6e 64 65 78 5f 69 6e 66 6f 20 70 6f 69 6e 74  index_info point
38e2f 65 72 20 70 61 73 73 65 64 0a 2a 2a 20 61 73 20  er passed.** as 
38e30 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  the argument..**
38e31 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
38e32 6f 63 63 75 72 73 2c 20 70 50 61 72 73 65 20 69  occurs, pParse i
38e33 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68  s populated with
38e34 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
38e35 65 20 61 6e 64 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a  e and a.** non-z
38e36 65 72 6f 20 76 61 6c 75 65 20 69 73 20 72 65 74  ero value is ret
38e37 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  urned. Otherwise
38e38 2c 20 30 20 69 73 20 72 65 74 75 72 6e 65 64 20  , 0 is returned 
38e39 61 6e 64 20 74 68 65 20 6f 75 74 70 75 74 0a 2a  and the output.*
38e3a 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 73 71  * part of the sq
38e3b 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
38e3c 20 73 74 72 75 63 74 75 72 65 20 69 73 20 6c 65   structure is le
38e3d 66 74 20 70 6f 70 75 6c 61 74 65 64 2e 0a 2a 2a  ft populated..**
38e3e 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e  .** Whether or n
38e3f 6f 74 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72  ot an error is r
38e40 65 74 75 72 6e 65 64 2c 20 69 74 20 69 73 20 74  eturned, it is t
38e41 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  he responsibilit
38e42 79 20 6f 66 20 74 68 65 0a 2a 2a 20 63 61 6c 6c  y of the.** call
38e43 65 72 20 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79  er to eventually
38e44 20 66 72 65 65 20 70 2d 3e 69 64 78 53 74 72 20   free p->idxStr 
38e45 69 66 20 70 2d 3e 6e 65 65 64 54 6f 46 72 65 65  if p->needToFree
38e46 49 64 78 53 74 72 20 69 6e 64 69 63 61 74 65 73  IdxStr indicates
38e47 0a 2a 2a 20 74 68 61 74 20 74 68 69 73 20 69 73  .** that this is
38e48 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74   required..*/.st
38e49 61 74 69 63 20 69 6e 74 20 76 74 61 62 42 65 73  atic int vtabBes
38e4a 74 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50  tIndex(Parse *pP
38e4b 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  arse, Table *pTa
38e4c 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  b, sqlite3_index
38e4d 5f 69 6e 66 6f 20 2a 70 29 7b 0a 20 20 73 71 6c  _info *p){.  sql
38e4e 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
38e4f 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 54 61   = sqlite3GetVTa
38e50 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ble(pParse->db, 
38e51 70 54 61 62 29 2d 3e 70 56 74 61 62 3b 0a 20 20  pTab)->pVtab;.  
38e52 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 3b  int i;.  int rc;
38e53 0a 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28  ..  WHERETRACE((
38e54 22 78 42 65 73 74 49 6e 64 65 78 20 66 6f 72 20  "xBestIndex for 
38e55 25 73 5c 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  %s\n", pTab->zNa
38e56 6d 65 29 29 3b 0a 20 20 54 52 41 43 45 5f 49 44  me));.  TRACE_ID
38e57 58 5f 49 4e 50 55 54 53 28 70 29 3b 0a 20 20 72  X_INPUTS(p);.  r
38e58 63 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75  c = pVtab->pModu
38e59 6c 65 2d 3e 78 42 65 73 74 49 6e 64 65 78 28 70  le->xBestIndex(p
38e5a 56 74 61 62 2c 20 70 29 3b 0a 20 20 54 52 41 43  Vtab, p);.  TRAC
38e5b 45 5f 49 44 58 5f 4f 55 54 50 55 54 53 28 70 29  E_IDX_OUTPUTS(p)
38e5c 3b 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  ;..  if( rc!=SQL
38e5d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
38e5e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
38e5f 45 4d 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  EM ){.      pPar
38e60 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  se->db->mallocFa
38e61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 65  iled = 1;.    }e
38e62 6c 73 65 20 69 66 28 20 21 70 56 74 61 62 2d 3e  lse if( !pVtab->
38e63 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20  zErrMsg ){.     
38e64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
38e65 28 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20 73  (pParse, "%s", s
38e66 71 6c 69 74 65 33 45 72 72 53 74 72 28 72 63 29  qlite3ErrStr(rc)
38e67 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
38e68 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
38e69 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 22  Msg(pParse, "%s"
38e6a 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  , pVtab->zErrMsg
38e6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  );.    }.  }.  s
38e6c 71 6c 69 74 65 33 5f 66 72 65 65 28 70 56 74 61  qlite3_free(pVta
38e6d 62 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70  b->zErrMsg);.  p
38e6e 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  Vtab->zErrMsg = 
38e6f 30 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  0;..  for(i=0; i
38e70 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  <p->nConstraint;
38e71 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 21   i++){.    if( !
38e72 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69  p->aConstraint[i
38e73 5d 2e 75 73 61 62 6c 65 20 26 26 20 70 2d 3e 61  ].usable && p->a
38e74 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
38e75 69 5d 2e 61 72 67 76 49 6e 64 65 78 3e 30 20 29  i].argvIndex>0 )
38e76 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
38e77 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
38e78 0a 20 20 20 20 20 20 20 20 20 20 22 74 61 62 6c  .          "tabl
38e79 65 20 25 73 3a 20 78 42 65 73 74 49 6e 64 65 78  e %s: xBestIndex
38e7a 20 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6e 76   returned an inv
38e7b 61 6c 69 64 20 70 6c 61 6e 22 2c 20 70 54 61 62  alid plan", pTab
38e7c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  ->zName);.    }.
38e7d 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 50    }..  return pP
38e7e 61 72 73 65 2d 3e 6e 45 72 72 3b 0a 7d 0a 0a 0a  arse->nErr;.}...
38e7f 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
38e80 65 20 62 65 73 74 20 69 6e 64 65 78 20 66 6f 72  e best index for
38e81 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
38e82 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 65 73 74  ..**.** The best
38e83 20 69 6e 64 65 78 20 69 73 20 63 6f 6d 70 75 74   index is comput
38e84 65 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49  ed by the xBestI
38e85 6e 64 65 78 20 6d 65 74 68 6f 64 20 6f 66 20 74  ndex method of t
38e86 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61  he virtual.** ta
38e87 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 69  ble module.  Thi
38e88 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65 61  s routine is rea
38e89 6c 6c 79 20 6a 75 73 74 20 61 20 77 72 61 70 70  lly just a wrapp
38e8a 65 72 20 74 68 61 74 20 73 65 74 73 20 75 70 0a  er that sets up.
38e8b 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  ** the sqlite3_i
38e8c 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74  ndex_info struct
38e8d 75 72 65 20 74 68 61 74 20 69 73 20 75 73 65 64  ure that is used
38e8e 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20   to communicate 
38e8f 77 69 74 68 0a 2a 2a 20 78 42 65 73 74 49 6e 64  with.** xBestInd
38e90 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 20 6a  ex..**.** In a j
38e91 6f 69 6e 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  oin, this routin
38e92 65 20 6d 69 67 68 74 20 62 65 20 63 61 6c 6c 65  e might be calle
38e93 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73  d multiple times
38e94 20 66 6f 72 20 74 68 65 0a 2a 2a 20 73 61 6d 65   for the.** same
38e95 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
38e96 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   The sqlite3_ind
38e97 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
38e98 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20  e is created.** 
38e99 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  and initialized 
38e9a 6f 6e 20 74 68 65 20 66 69 72 73 74 20 69 6e 76  on the first inv
38e9b 6f 63 61 74 69 6f 6e 20 61 6e 64 20 72 65 75 73  ocation and reus
38e9c 65 64 20 6f 6e 20 61 6c 6c 20 73 75 62 73 65 71  ed on all subseq
38e9d 75 65 6e 74 0a 2a 2a 20 69 6e 76 6f 63 61 74 69  uent.** invocati
38e9e 6f 6e 73 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ons.  The sqlite
38e9f 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
38ea0 75 63 74 75 72 65 20 69 73 20 61 6c 73 6f 20 75  ucture is also u
38ea1 73 65 64 20 77 68 65 6e 0a 2a 2a 20 63 6f 64 65  sed when.** code
38ea2 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f   is generated to
38ea3 20 61 63 63 65 73 73 20 74 68 65 20 76 69 72 74   access the virt
38ea4 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20  ual table.  The 
38ea5 77 68 65 72 65 49 6e 66 6f 44 65 6c 65 74 65 28  whereInfoDelete(
38ea6 29 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 61  ) .** routine ta
38ea7 6b 65 73 20 63 61 72 65 20 6f 66 20 66 72 65 65  kes care of free
38ea8 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ing the sqlite3_
38ea9 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
38eaa 74 75 72 65 20 61 66 74 65 72 0a 2a 2a 20 65 76  ture after.** ev
38eab 65 72 79 62 6f 64 79 20 68 61 73 20 66 69 6e 69  erybody has fini
38eac 73 68 65 64 20 77 69 74 68 20 69 74 2e 0a 2a 2f  shed with it..*/
38ead 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 65 73  .static void bes
38eae 74 56 69 72 74 75 61 6c 49 6e 64 65 78 28 57 68  tVirtualIndex(Wh
38eaf 65 72 65 42 65 73 74 49 64 78 20 2a 70 29 7b 0a  ereBestIdx *p){.
38eb0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20    Parse *pParse 
38eb1 3d 20 70 2d 3e 70 50 61 72 73 65 3b 20 20 20 20  = p->pParse;    
38eb2 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
38eb3 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68   context */.  Wh
38eb4 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 20 3d  ereClause *pWC =
38eb5 20 70 2d 3e 70 57 43 3b 20 20 20 20 20 20 2f 2a   p->pWC;      /*
38eb6 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
38eb7 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  e */.  struct Sr
38eb8 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63  cList_item *pSrc
38eb9 20 3d 20 70 2d 3e 70 53 72 63 3b 20 2f 2a 20 54   = p->pSrc; /* T
38eba 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74  he FROM clause t
38ebb 65 72 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f  erm to search */
38ebc 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d  .  Table *pTab =
38ebd 20 70 53 72 63 2d 3e 70 54 61 62 3b 0a 20 20 73   pSrc->pTab;.  s
38ebe 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
38ebf 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 0a 20 20 73  o *pIdxInfo;.  s
38ec0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
38ec1 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a  dex_constraint *
38ec2 70 49 64 78 43 6f 6e 73 3b 0a 20 20 73 74 72 75  pIdxCons;.  stru
38ec3 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
38ec4 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67  _constraint_usag
38ec5 65 20 2a 70 55 73 61 67 65 3b 0a 20 20 57 68 65  e *pUsage;.  Whe
38ec6 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20  reTerm *pTerm;. 
38ec7 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 6e 74   int i, j;.  int
38ec8 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20 64 6f 75   nOrderBy;.  dou
38ec9 62 6c 65 20 72 43 6f 73 74 3b 0a 0a 20 20 2f 2a  ble rCost;..  /*
38eca 20 4d 61 6b 65 20 73 75 72 65 20 77 73 46 6c 61   Make sure wsFla
38ecb 67 73 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  gs is initialize
38ecc 64 20 74 6f 20 73 6f 6d 65 20 73 61 6e 65 20 76  d to some sane v
38ecd 61 6c 75 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  alue. Otherwise,
38ece 20 69 66 20 74 68 65 20 0a 20 20 2a 2a 20 6d 61   if the .  ** ma
38ecf 6c 6c 6f 63 20 69 6e 20 61 6c 6c 6f 63 61 74 65  lloc in allocate
38ed0 49 6e 64 65 78 49 6e 66 6f 28 29 20 66 61 69 6c  IndexInfo() fail
38ed1 73 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63 74  s and this funct
38ed2 69 6f 6e 20 72 65 74 75 72 6e 73 20 6c 65 61 76  ion returns leav
38ed3 69 6e 67 0a 20 20 2a 2a 20 77 73 46 6c 61 67 73  ing.  ** wsFlags
38ed4 20 69 6e 20 61 6e 20 75 6e 69 6e 69 74 69 61 6c   in an uninitial
38ed5 69 7a 65 64 20 73 74 61 74 65 2c 20 74 68 65 20  ized state, the 
38ed6 63 61 6c 6c 65 72 20 6d 61 79 20 62 65 68 61 76  caller may behav
38ed7 65 20 75 6e 70 72 65 64 69 63 74 61 62 6c 79 2e  e unpredictably.
38ed8 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26  .  */.  memset(&
38ed9 70 2d 3e 63 6f 73 74 2c 20 30 2c 20 73 69 7a 65  p->cost, 0, size
38eda 6f 66 28 70 2d 3e 63 6f 73 74 29 29 3b 0a 20 20  of(p->cost));.  
38edb 70 2d 3e 63 6f 73 74 2e 70 6c 61 6e 2e 77 73 46  p->cost.plan.wsF
38edc 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 56 49 52  lags = WHERE_VIR
38edd 54 55 41 4c 54 41 42 4c 45 3b 0a 0a 20 20 2f 2a  TUALTABLE;..  /*
38ede 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   If the sqlite3_
38edf 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
38ee0 74 75 72 65 20 68 61 73 20 6e 6f 74 20 62 65 65  ture has not bee
38ee1 6e 20 70 72 65 76 69 6f 75 73 6c 79 0a 20 20 2a  n previously.  *
38ee2 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  * allocated and 
38ee3 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65  initialized, the
38ee4 6e 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69  n allocate and i
38ee5 6e 69 74 69 61 6c 69 7a 65 20 69 74 20 6e 6f 77  nitialize it now
38ee6 2e 0a 20 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66  ..  */.  pIdxInf
38ee7 6f 20 3d 20 2a 70 2d 3e 70 70 49 64 78 49 6e 66  o = *p->ppIdxInf
38ee8 6f 3b 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66  o;.  if( pIdxInf
38ee9 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 2d 3e  o==0 ){.    *p->
38eea 70 70 49 64 78 49 6e 66 6f 20 3d 20 70 49 64 78  ppIdxInfo = pIdx
38eeb 49 6e 66 6f 20 3d 20 61 6c 6c 6f 63 61 74 65 49  Info = allocateI
38eec 6e 64 65 78 49 6e 66 6f 28 70 29 3b 0a 20 20 7d  ndexInfo(p);.  }
38eed 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f 3d  .  if( pIdxInfo=
38eee 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
38eef 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74  ;.  }..  /* At t
38ef0 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 20 73  his point, the s
38ef1 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
38ef2 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  o structure that
38ef3 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 73   pIdxInfo points
38ef4 0a 20 20 2a 2a 20 74 6f 20 77 69 6c 6c 20 68 61  .  ** to will ha
38ef5 76 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  ve been initiali
38ef6 7a 65 64 2c 20 65 69 74 68 65 72 20 64 75 72 69  zed, either duri
38ef7 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 69  ng the current i
38ef8 6e 76 6f 63 61 74 69 6f 6e 20 6f 72 0a 20 20 2a  nvocation or.  *
38ef9 2a 20 64 75 72 69 6e 67 20 73 6f 6d 65 20 70 72  * during some pr
38efa 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20  ior invocation. 
38efb 20 4e 6f 77 20 77 65 20 6a 75 73 74 20 68 61 76   Now we just hav
38efc 65 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74  e to customize t
38efd 68 65 0a 20 20 2a 2a 20 64 65 74 61 69 6c 73 20  he.  ** details 
38efe 6f 66 20 70 49 64 78 49 6e 66 6f 20 66 6f 72 20  of pIdxInfo for 
38eff 74 68 65 20 63 75 72 72 65 6e 74 20 69 6e 76 6f  the current invo
38f00 63 61 74 69 6f 6e 20 61 6e 64 20 70 61 73 73 20  cation and pass 
38f01 69 74 20 74 6f 0a 20 20 2a 2a 20 78 42 65 73 74  it to.  ** xBest
38f02 49 6e 64 65 78 2e 0a 20 20 2a 2f 0a 0a 20 20 2f  Index..  */..  /
38f03 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d  * The module nam
38f04 65 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65  e must be define
38f05 64 2e 20 41 6c 73 6f 2c 20 62 79 20 74 68 69 73  d. Also, by this
38f06 20 70 6f 69 6e 74 20 74 68 65 72 65 20 6d 75 73   point there mus
38f07 74 0a 20 20 2a 2a 20 62 65 20 61 20 70 6f 69 6e  t.  ** be a poin
38f08 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65  ter to an sqlite
38f09 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65  3_vtab structure
38f0a 2e 20 4f 74 68 65 72 77 69 73 65 0a 20 20 2a 2a  . Otherwise.  **
38f0b 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
38f0c 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 77 6f 75  olumnNames() wou
38f0d 6c 64 20 68 61 76 65 20 70 69 63 6b 65 64 20 75  ld have picked u
38f0e 70 20 74 68 65 20 65 72 72 6f 72 2e 20 0a 20 20  p the error. .  
38f0f 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  */.  assert( pTa
38f10 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20 26  b->azModuleArg &
38f11 26 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65  & pTab->azModule
38f12 41 72 67 5b 30 5d 20 29 3b 0a 20 20 61 73 73 65  Arg[0] );.  asse
38f13 72 74 28 20 73 71 6c 69 74 65 33 47 65 74 56 54  rt( sqlite3GetVT
38f14 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  able(pParse->db,
38f15 20 70 54 61 62 29 20 29 3b 0a 0a 20 20 2f 2a 20   pTab) );..  /* 
38f16 53 65 74 20 74 68 65 20 61 43 6f 6e 73 74 72 61  Set the aConstra
38f17 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 66 69 65  int[].usable fie
38f18 6c 64 73 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  lds and initiali
38f19 7a 65 20 61 6c 6c 20 0a 20 20 2a 2a 20 6f 75 74  ze all .  ** out
38f1a 70 75 74 20 76 61 72 69 61 62 6c 65 73 20 74 6f  put variables to
38f1b 20 7a 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   zero..  **.  **
38f1c 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75   aConstraint[].u
38f1d 73 61 62 6c 65 20 69 73 20 74 72 75 65 20 66 6f  sable is true fo
38f1e 72 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 68  r constraints wh
38f1f 65 72 65 20 74 68 65 20 72 69 67 68 74 2d 68 61  ere the right-ha
38f20 6e 64 0a 20 20 2a 2a 20 73 69 64 65 20 63 6f 6e  nd.  ** side con
38f21 74 61 69 6e 73 20 6f 6e 6c 79 20 72 65 66 65 72  tains only refer
38f22 65 6e 63 65 73 20 74 6f 20 74 61 62 6c 65 73 20  ences to tables 
38f23 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74  to the left of t
38f24 68 65 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20  he current.  ** 
38f25 74 61 62 6c 65 2e 20 20 49 6e 20 6f 74 68 65 72  table.  In other
38f26 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 20 63   words, if the c
38f27 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 6f 66 20  onstraint is of 
38f28 74 68 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20  the form:.  **. 
38f29 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 63 6f   **           co
38f2a 6c 75 6d 6e 20 3d 20 65 78 70 72 0a 20 20 2a 2a  lumn = expr.  **
38f2b 0a 20 20 2a 2a 20 61 6e 64 20 77 65 20 61 72 65  .  ** and we are
38f2c 20 65 76 61 6c 75 61 74 69 6e 67 20 61 20 6a 6f   evaluating a jo
38f2d 69 6e 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e  in, then the con
38f2e 73 74 72 61 69 6e 74 20 6f 6e 20 63 6f 6c 75 6d  straint on colum
38f2f 6e 20 69 73 20 0a 20 20 2a 2a 20 6f 6e 6c 79 20  n is .  ** only 
38f30 76 61 6c 69 64 20 69 66 20 61 6c 6c 20 74 61 62  valid if all tab
38f31 6c 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 69  les referenced i
38f32 6e 20 65 78 70 72 20 6f 63 63 75 72 20 74 6f 20  n expr occur to 
38f33 74 68 65 20 6c 65 66 74 0a 20 20 2a 2a 20 6f 66  the left.  ** of
38f34 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61   the table conta
38f35 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2e 0a 20 20  ining column..  
38f36 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 61 43 6f 6e  **.  ** The aCon
38f37 73 74 72 61 69 6e 74 73 5b 5d 20 61 72 72 61 79  straints[] array
38f38 20 63 6f 6e 74 61 69 6e 73 20 65 6e 74 72 69 65   contains entrie
38f39 73 20 66 6f 72 20 61 6c 6c 20 63 6f 6e 73 74 72  s for all constr
38f3a 61 69 6e 74 73 0a 20 20 2a 2a 20 6f 6e 20 74 68  aints.  ** on th
38f3b 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 2e  e current table.
38f3c 20 20 54 68 61 74 20 77 61 79 20 77 65 20 6f 6e    That way we on
38f3d 6c 79 20 68 61 76 65 20 74 6f 20 63 6f 6d 70 75  ly have to compu
38f3e 74 65 20 69 74 20 6f 6e 63 65 0a 20 20 2a 2a 20  te it once.  ** 
38f3f 65 76 65 6e 20 74 68 6f 75 67 68 20 77 65 20 6d  even though we m
38f40 69 67 68 74 20 74 72 79 20 74 6f 20 70 69 63 6b  ight try to pick
38f41 20 74 68 65 20 62 65 73 74 20 69 6e 64 65 78 20   the best index 
38f42 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 0a  multiple times..
38f43 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20 61 74    ** For each at
38f44 74 65 6d 70 74 20 61 74 20 70 69 63 6b 69 6e 67  tempt at picking
38f45 20 61 6e 20 69 6e 64 65 78 2c 20 74 68 65 20 6f   an index, the o
38f46 72 64 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69  rder of tables i
38f47 6e 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 69 6e 20  n the.  ** join 
38f48 6d 69 67 68 74 20 62 65 20 64 69 66 66 65 72 65  might be differe
38f49 6e 74 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f  nt so we have to
38f4a 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65 20 75   recompute the u
38f4b 73 61 62 6c 65 20 66 6c 61 67 0a 20 20 2a 2a 20  sable flag.  ** 
38f4c 65 61 63 68 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a  each time..  */.
38f4d 20 20 70 49 64 78 43 6f 6e 73 20 3d 20 2a 28 73    pIdxCons = *(s
38f4e 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
38f4f 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a  dex_constraint**
38f50 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e  )&pIdxInfo->aCon
38f51 73 74 72 61 69 6e 74 3b 0a 20 20 70 55 73 61 67  straint;.  pUsag
38f52 65 20 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43  e = pIdxInfo->aC
38f53 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 3b 0a  onstraintUsage;.
38f54 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64    for(i=0; i<pId
38f55 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69  xInfo->nConstrai
38f56 6e 74 3b 20 69 2b 2b 2c 20 70 49 64 78 43 6f 6e  nt; i++, pIdxCon
38f57 73 2b 2b 29 7b 0a 20 20 20 20 6a 20 3d 20 70 49  s++){.    j = pI
38f58 64 78 43 6f 6e 73 2d 3e 69 54 65 72 6d 4f 66 66  dxCons->iTermOff
38f59 73 65 74 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d  set;.    pTerm =
38f5a 20 26 70 57 43 2d 3e 61 5b 6a 5d 3b 0a 20 20 20   &pWC->a[j];.   
38f5b 20 70 49 64 78 43 6f 6e 73 2d 3e 75 73 61 62 6c   pIdxCons->usabl
38f5c 65 20 3d 20 28 70 54 65 72 6d 2d 3e 70 72 65 72  e = (pTerm->prer
38f5d 65 71 52 69 67 68 74 26 70 2d 3e 6e 6f 74 52 65  eqRight&p->notRe
38f5e 61 64 79 29 20 3f 20 30 20 3a 20 31 3b 0a 20 20  ady) ? 0 : 1;.  
38f5f 7d 0a 20 20 6d 65 6d 73 65 74 28 70 55 73 61 67  }.  memset(pUsag
38f60 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 55 73  e, 0, sizeof(pUs
38f61 61 67 65 5b 30 5d 29 2a 70 49 64 78 49 6e 66 6f  age[0])*pIdxInfo
38f62 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a  ->nConstraint);.
38f63 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f 2d 3e    if( pIdxInfo->
38f64 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72  needToFreeIdxStr
38f65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
38f66 66 72 65 65 28 70 49 64 78 49 6e 66 6f 2d 3e 69  free(pIdxInfo->i
38f67 64 78 53 74 72 29 3b 0a 20 20 7d 0a 20 20 70 49  dxStr);.  }.  pI
38f68 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 20 3d  dxInfo->idxStr =
38f69 20 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e   0;.  pIdxInfo->
38f6a 69 64 78 4e 75 6d 20 3d 20 30 3b 0a 20 20 70 49  idxNum = 0;.  pI
38f6b 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72  dxInfo->needToFr
38f6c 65 65 49 64 78 53 74 72 20 3d 20 30 3b 0a 20 20  eeIdxStr = 0;.  
38f6d 70 49 64 78 49 6e 66 6f 2d 3e 6f 72 64 65 72 42  pIdxInfo->orderB
38f6e 79 43 6f 6e 73 75 6d 65 64 20 3d 20 30 3b 0a 20  yConsumed = 0;. 
38f6f 20 2f 2a 20 28 28 64 6f 75 62 6c 65 29 32 29 20   /* ((double)2) 
38f70 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54  In case of SQLIT
38f71 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
38f72 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 70 49  POINT... */.  pI
38f73 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65  dxInfo->estimate
38f74 64 43 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42  dCost = SQLITE_B
38f75 49 47 5f 44 42 4c 20 2f 20 28 28 64 6f 75 62 6c  IG_DBL / ((doubl
38f76 65 29 32 29 3b 0a 20 20 6e 4f 72 64 65 72 42 79  e)2);.  nOrderBy
38f77 20 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72   = pIdxInfo->nOr
38f78 64 65 72 42 79 3b 0a 20 20 69 66 28 20 21 70 2d  derBy;.  if( !p-
38f79 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20  >pOrderBy ){.   
38f7a 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65   pIdxInfo->nOrde
38f7b 72 42 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  rBy = 0;.  }..  
38f7c 69 66 28 20 76 74 61 62 42 65 73 74 49 6e 64 65  if( vtabBestInde
38f7d 78 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  x(pParse, pTab, 
38f7e 70 49 64 78 49 6e 66 6f 29 20 29 7b 0a 20 20 20  pIdxInfo) ){.   
38f7f 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
38f80 70 49 64 78 43 6f 6e 73 20 3d 20 2a 28 73 74 72  pIdxCons = *(str
38f81 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
38f82 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26  x_constraint**)&
38f83 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  pIdxInfo->aConst
38f84 72 61 69 6e 74 3b 0a 20 20 66 6f 72 28 69 3d 30  raint;.  for(i=0
38f85 3b 20 69 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e 43  ; i<pIdxInfo->nC
38f86 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b  onstraint; i++){
38f87 0a 20 20 20 20 69 66 28 20 70 55 73 61 67 65 5b  .    if( pUsage[
38f88 69 5d 2e 61 72 67 76 49 6e 64 65 78 3e 30 20 29  i].argvIndex>0 )
38f89 7b 0a 20 20 20 20 20 20 70 2d 3e 63 6f 73 74 2e  {.      p->cost.
38f8a 75 73 65 64 20 7c 3d 20 70 57 43 2d 3e 61 5b 70  used |= pWC->a[p
38f8b 49 64 78 43 6f 6e 73 5b 69 5d 2e 69 54 65 72 6d  IdxCons[i].iTerm
38f8c 4f 66 66 73 65 74 5d 2e 70 72 65 72 65 71 52 69  Offset].prereqRi
38f8d 67 68 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ght;.    }.  }..
38f8e 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
38f8f 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   an ORDER BY cla
38f90 75 73 65 2c 20 61 6e 64 20 74 68 65 20 73 65 6c  use, and the sel
38f91 65 63 74 65 64 20 76 69 72 74 75 61 6c 20 74 61  ected virtual ta
38f92 62 6c 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20 64  ble index.  ** d
38f93 6f 65 73 20 6e 6f 74 20 73 61 74 69 73 66 79 20  oes not satisfy 
38f94 69 74 2c 20 69 6e 63 72 65 61 73 65 20 74 68 65  it, increase the
38f95 20 63 6f 73 74 20 6f 66 20 74 68 65 20 73 63 61   cost of the sca
38f96 6e 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 20 54  n accordingly. T
38f97 68 69 73 0a 20 20 2a 2a 20 6d 61 74 63 68 65 73  his.  ** matches
38f98 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20   the processing 
38f99 66 6f 72 20 6e 6f 6e 2d 76 69 72 74 75 61 6c 20  for non-virtual 
38f9a 74 61 62 6c 65 73 20 69 6e 20 62 65 73 74 42 74  tables in bestBt
38f9b 72 65 65 49 6e 64 65 78 28 29 2e 0a 20 20 2a 2f  reeIndex()..  */
38f9c 0a 20 20 72 43 6f 73 74 20 3d 20 70 49 64 78 49  .  rCost = pIdxI
38f9d 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f  nfo->estimatedCo
38f9e 73 74 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72  st;.  if( p->pOr
38f9f 64 65 72 42 79 20 26 26 20 70 49 64 78 49 6e 66  derBy && pIdxInf
38fa0 6f 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d  o->orderByConsum
38fa1 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 43 6f  ed==0 ){.    rCo
38fa2 73 74 20 2b 3d 20 65 73 74 4c 6f 67 28 72 43 6f  st += estLog(rCo
38fa3 73 74 29 2a 72 43 6f 73 74 3b 0a 20 20 7d 0a 0a  st)*rCost;.  }..
38fa4 20 20 2f 2a 20 54 68 65 20 63 6f 73 74 20 69 73    /* The cost is
38fa5 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20   not allowed to 
38fa6 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 53  be larger than S
38fa7 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 74  QLITE_BIG_DBL (t
38fa8 68 65 0a 20 20 2a 2a 20 69 6e 69 74 61 6c 20 76  he.  ** inital v
38fa9 61 6c 75 65 20 6f 66 20 6c 6f 77 65 73 74 43 6f  alue of lowestCo
38faa 73 74 20 69 6e 20 74 68 69 73 20 6c 6f 6f 70 2e  st in this loop.
38fab 20 49 66 20 69 74 20 69 73 2c 20 74 68 65 6e 20   If it is, then 
38fac 74 68 65 0a 20 20 2a 2a 20 28 63 6f 73 74 3c 6c  the.  ** (cost<l
38fad 6f 77 65 73 74 43 6f 73 74 29 20 74 65 73 74 20  owestCost) test 
38fae 62 65 6c 6f 77 20 77 69 6c 6c 20 6e 65 76 65 72  below will never
38faf 20 62 65 20 74 72 75 65 2e 0a 20 20 2a 2a 20 0a   be true..  ** .
38fb0 20 20 2a 2a 20 55 73 65 20 22 28 64 6f 75 62 6c    ** Use "(doubl
38fb1 65 29 32 22 20 69 6e 73 74 65 61 64 20 6f 66 20  e)2" instead of 
38fb2 22 32 2e 30 22 20 69 6e 20 63 61 73 65 20 4f 4d  "2.0" in case OM
38fb3 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
38fb4 54 20 0a 20 20 2a 2a 20 69 73 20 64 65 66 69 6e  T .  ** is defin
38fb5 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  ed..  */.  if( (
38fb6 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 2f 28  SQLITE_BIG_DBL/(
38fb7 28 64 6f 75 62 6c 65 29 32 29 29 3c 72 43 6f 73  (double)2))<rCos
38fb8 74 20 29 7b 0a 20 20 20 20 70 2d 3e 63 6f 73 74  t ){.    p->cost
38fb9 2e 72 43 6f 73 74 20 3d 20 28 53 51 4c 49 54 45  .rCost = (SQLITE
38fba 5f 42 49 47 5f 44 42 4c 2f 28 28 64 6f 75 62 6c  _BIG_DBL/((doubl
38fbb 65 29 32 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  e)2));.  }else{.
38fbc 20 20 20 20 70 2d 3e 63 6f 73 74 2e 72 43 6f 73      p->cost.rCos
38fbd 74 20 3d 20 72 43 6f 73 74 3b 0a 20 20 7d 0a 20  t = rCost;.  }. 
38fbe 20 70 2d 3e 63 6f 73 74 2e 70 6c 61 6e 2e 75 2e   p->cost.plan.u.
38fbf 70 56 74 61 62 49 64 78 20 3d 20 70 49 64 78 49  pVtabIdx = pIdxI
38fc0 6e 66 6f 3b 0a 20 20 69 66 28 20 70 49 64 78 49  nfo;.  if( pIdxI
38fc1 6e 66 6f 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73  nfo->orderByCons
38fc2 75 6d 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 63  umed ){.    p->c
38fc3 6f 73 74 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  ost.plan.wsFlags
38fc4 20 7c 3d 20 57 48 45 52 45 5f 4f 52 44 45 52 45   |= WHERE_ORDERE
38fc5 44 3b 0a 20 20 20 20 70 2d 3e 63 6f 73 74 2e 70  D;.    p->cost.p
38fc6 6c 61 6e 2e 6e 4f 42 53 61 74 20 3d 20 6e 4f 72  lan.nOBSat = nOr
38fc7 64 65 72 42 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a  derBy;.  }else{.
38fc8 20 20 20 20 70 2d 3e 63 6f 73 74 2e 70 6c 61 6e      p->cost.plan
38fc9 2e 6e 4f 42 53 61 74 20 3d 20 70 2d 3e 69 20 3f  .nOBSat = p->i ?
38fca 20 70 2d 3e 61 4c 65 76 65 6c 5b 70 2d 3e 69 2d   p->aLevel[p->i-
38fcb 31 5d 2e 70 6c 61 6e 2e 6e 4f 42 53 61 74 20 3a  1].plan.nOBSat :
38fcc 20 30 3b 0a 20 20 7d 0a 20 20 70 2d 3e 63 6f 73   0;.  }.  p->cos
38fcd 74 2e 70 6c 61 6e 2e 6e 45 71 20 3d 20 30 3b 0a  t.plan.nEq = 0;.
38fce 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64    pIdxInfo->nOrd
38fcf 65 72 42 79 20 3d 20 6e 4f 72 64 65 72 42 79 3b  erBy = nOrderBy;
38fd0 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69  ..  /* Try to fi
38fd1 6e 64 20 61 20 6d 6f 72 65 20 65 66 66 69 63 69  nd a more effici
38fd2 65 6e 74 20 61 63 63 65 73 73 20 70 61 74 74 65  ent access patte
38fd3 72 6e 20 62 79 20 75 73 69 6e 67 20 6d 75 6c 74  rn by using mult
38fd4 69 70 6c 65 20 69 6e 64 65 78 65 73 0a 20 20 2a  iple indexes.  *
38fd5 2a 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20 61 6e  * to optimize an
38fd6 20 4f 52 20 65 78 70 72 65 73 73 69 6f 6e 20 77   OR expression w
38fd7 69 74 68 69 6e 20 74 68 65 20 57 48 45 52 45 20  ithin the WHERE 
38fd8 63 6c 61 75 73 65 2e 20 0a 20 20 2a 2f 0a 20 20  clause. .  */.  
38fd9 62 65 73 74 4f 72 43 6c 61 75 73 65 49 6e 64 65  bestOrClauseInde
38fda 78 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  x(p);.}.#endif /
38fdb 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
38fdc 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23  RTUALTABLE */..#
38fdd 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
38fde 42 4c 45 5f 53 54 41 54 33 0a 2f 2a 0a 2a 2a 20  BLE_STAT3./*.** 
38fdf 45 73 74 69 6d 61 74 65 20 74 68 65 20 6c 6f 63  Estimate the loc
38fe0 61 74 69 6f 6e 20 6f 66 20 61 20 70 61 72 74 69  ation of a parti
38fe1 63 75 6c 61 72 20 6b 65 79 20 61 6d 6f 6e 67 20  cular key among 
38fe2 61 6c 6c 20 6b 65 79 73 20 69 6e 20 61 6e 0a 2a  all keys in an.*
38fe3 2a 20 69 6e 64 65 78 2e 20 20 53 74 6f 72 65 20  * index.  Store 
38fe4 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 61  the results in a
38fe5 53 74 61 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  Stat as follows:
38fe6 0a 2a 2a 0a 2a 2a 20 20 20 20 61 53 74 61 74 5b  .**.**    aStat[
38fe7 30 5d 20 20 20 20 20 20 45 73 74 2e 20 6e 75 6d  0]      Est. num
38fe8 62 65 72 20 6f 66 20 72 6f 77 73 20 6c 65 73 73  ber of rows less
38fe9 20 74 68 61 6e 20 70 56 61 6c 0a 2a 2a 20 20 20   than pVal.**   
38fea 20 61 53 74 61 74 5b 31 5d 20 20 20 20 20 20 45   aStat[1]      E
38feb 73 74 2e 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  st. number of ro
38fec 77 73 20 65 71 75 61 6c 20 74 6f 20 70 56 61 6c  ws equal to pVal
38fed 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
38fee 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
38fef 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ss..*/.static in
38ff0 74 20 77 68 65 72 65 4b 65 79 53 74 61 74 73 28  t whereKeyStats(
38ff1 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
38ff2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
38ff3 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
38ff4 63 74 69 6f 6e 20 2a 2f 0a 20 20 49 6e 64 65 78  ction */.  Index
38ff5 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20 20 20   *pIdx,         
38ff6 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
38ff7 74 6f 20 63 6f 6e 73 69 64 65 72 20 64 6f 6d 61  to consider doma
38ff8 69 6e 20 6f 66 20 2a 2f 0a 20 20 73 71 6c 69 74  in of */.  sqlit
38ff9 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20  e3_value *pVal, 
38ffa 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
38ffb 74 6f 20 63 6f 6e 73 69 64 65 72 20 2a 2f 0a 20  to consider */. 
38ffc 20 69 6e 74 20 72 6f 75 6e 64 55 70 2c 20 20 20   int roundUp,   
38ffd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38ffe 52 6f 75 6e 64 20 75 70 20 69 66 20 74 72 75 65  Round up if true
38fff 2e 20 20 52 6f 75 6e 64 20 64 6f 77 6e 20 69 66  .  Round down if
39000 20 66 61 6c 73 65 20 2a 2f 0a 20 20 74 52 6f 77   false */.  tRow
39001 63 6e 74 20 2a 61 53 74 61 74 20 20 20 20 20 20  cnt *aStat      
39002 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
39003 73 74 61 74 73 20 77 72 69 74 74 65 6e 20 68 65  stats written he
39004 72 65 20 2a 2f 0a 29 7b 0a 20 20 74 52 6f 77 63  re */.){.  tRowc
39005 6e 74 20 6e 3b 0a 20 20 49 6e 64 65 78 53 61 6d  nt n;.  IndexSam
39006 70 6c 65 20 2a 61 53 61 6d 70 6c 65 3b 0a 20 20  ple *aSample;.  
39007 69 6e 74 20 69 2c 20 65 54 79 70 65 3b 0a 20 20  int i, eType;.  
39008 69 6e 74 20 69 73 45 71 20 3d 20 30 3b 0a 20 20  int isEq = 0;.  
39009 69 36 34 20 76 3b 0a 20 20 64 6f 75 62 6c 65 20  i64 v;.  double 
3900a 72 2c 20 72 53 3b 0a 0a 20 20 61 73 73 65 72 74  r, rS;..  assert
3900b 28 20 72 6f 75 6e 64 55 70 3d 3d 30 20 7c 7c 20  ( roundUp==0 || 
3900c 72 6f 75 6e 64 55 70 3d 3d 31 20 29 3b 0a 20 20  roundUp==1 );.  
3900d 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 53  assert( pIdx->nS
3900e 61 6d 70 6c 65 3e 30 20 29 3b 0a 20 20 69 66 28  ample>0 );.  if(
3900f 20 70 56 61 6c 3d 3d 30 20 29 20 72 65 74 75 72   pVal==0 ) retur
39010 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
39011 20 20 6e 20 3d 20 70 49 64 78 2d 3e 61 69 52 6f    n = pIdx->aiRo
39012 77 45 73 74 5b 30 5d 3b 0a 20 20 61 53 61 6d 70  wEst[0];.  aSamp
39013 6c 65 20 3d 20 70 49 64 78 2d 3e 61 53 61 6d 70  le = pIdx->aSamp
39014 6c 65 3b 0a 20 20 65 54 79 70 65 20 3d 20 73 71  le;.  eType = sq
39015 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
39016 28 70 56 61 6c 29 3b 0a 0a 20 20 69 66 28 20 65  (pVal);..  if( e
39017 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54  Type==SQLITE_INT
39018 45 47 45 52 20 29 7b 0a 20 20 20 20 76 20 3d 20  EGER ){.    v = 
39019 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
3901a 74 36 34 28 70 56 61 6c 29 3b 0a 20 20 20 20 72  t64(pVal);.    r
3901b 20 3d 20 28 69 36 34 29 76 3b 0a 20 20 20 20 66   = (i64)v;.    f
3901c 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e  or(i=0; i<pIdx->
3901d 6e 53 61 6d 70 6c 65 3b 20 69 2b 2b 29 7b 0a 20  nSample; i++){. 
3901e 20 20 20 20 20 69 66 28 20 61 53 61 6d 70 6c 65       if( aSample
3901f 5b 69 5d 2e 65 54 79 70 65 3d 3d 53 51 4c 49 54  [i].eType==SQLIT
39020 45 5f 4e 55 4c 4c 20 29 20 63 6f 6e 74 69 6e 75  E_NULL ) continu
39021 65 3b 0a 20 20 20 20 20 20 69 66 28 20 61 53 61  e;.      if( aSa
39022 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3e 3d 53  mple[i].eType>=S
39023 51 4c 49 54 45 5f 54 45 58 54 20 29 20 62 72 65  QLITE_TEXT ) bre
39024 61 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 61 53  ak;.      if( aS
39025 61 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3d 3d  ample[i].eType==
39026 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 29  SQLITE_INTEGER )
39027 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 53  {.        if( aS
39028 61 6d 70 6c 65 5b 69 5d 2e 75 2e 69 3e 3d 76 20  ample[i].u.i>=v 
39029 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 73 45  ){.          isE
3902a 71 20 3d 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75  q = aSample[i].u
3902b 2e 69 3d 3d 76 3b 0a 20 20 20 20 20 20 20 20 20  .i==v;.         
3902c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
3902d 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
3902e 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61         assert( a
3902f 53 61 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3d  Sample[i].eType=
39030 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 29 3b  =SQLITE_FLOAT );
39031 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 53 61  .        if( aSa
39032 6d 70 6c 65 5b 69 5d 2e 75 2e 72 3e 3d 72 20 29  mple[i].u.r>=r )
39033 7b 0a 20 20 20 20 20 20 20 20 20 20 69 73 45 71  {.          isEq
39034 20 3d 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e   = aSample[i].u.
39035 72 3d 3d 72 3b 0a 20 20 20 20 20 20 20 20 20 20  r==r;.          
39036 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
39037 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
39038 20 7d 65 6c 73 65 20 69 66 28 20 65 54 79 70 65   }else if( eType
39039 3d 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 29  ==SQLITE_FLOAT )
3903a 7b 0a 20 20 20 20 72 20 3d 20 73 71 6c 69 74 65  {.    r = sqlite
3903b 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 70  3_value_double(p
3903c 56 61 6c 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  Val);.    for(i=
3903d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 53 61 6d 70  0; i<pIdx->nSamp
3903e 6c 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  le; i++){.      
3903f 69 66 28 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 65  if( aSample[i].e
39040 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c  Type==SQLITE_NUL
39041 4c 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  L ) continue;.  
39042 20 20 20 20 69 66 28 20 61 53 61 6d 70 6c 65 5b      if( aSample[
39043 69 5d 2e 65 54 79 70 65 3e 3d 53 51 4c 49 54 45  i].eType>=SQLITE
39044 5f 54 45 58 54 20 29 20 62 72 65 61 6b 3b 0a 20  _TEXT ) break;. 
39045 20 20 20 20 20 69 66 28 20 61 53 61 6d 70 6c 65       if( aSample
39046 5b 69 5d 2e 65 54 79 70 65 3d 3d 53 51 4c 49 54  [i].eType==SQLIT
39047 45 5f 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20  E_FLOAT ){.     
39048 20 20 20 72 53 20 3d 20 61 53 61 6d 70 6c 65 5b     rS = aSample[
39049 69 5d 2e 75 2e 72 3b 0a 20 20 20 20 20 20 7d 65  i].u.r;.      }e
3904a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 53 20  lse{.        rS 
3904b 3d 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e 69  = aSample[i].u.i
3904c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3904d 69 66 28 20 72 53 3e 3d 72 20 29 7b 0a 20 20 20  if( rS>=r ){.   
3904e 20 20 20 20 20 69 73 45 71 20 3d 20 72 53 3d 3d       isEq = rS==
3904f 72 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  r;.        break
39050 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
39051 20 20 7d 65 6c 73 65 20 69 66 28 20 65 54 79 70    }else if( eTyp
39052 65 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  e==SQLITE_NULL )
39053 7b 0a 20 20 20 20 69 20 3d 20 30 3b 0a 20 20 20  {.    i = 0;.   
39054 20 69 66 28 20 61 53 61 6d 70 6c 65 5b 30 5d 2e   if( aSample[0].
39055 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4e 55  eType==SQLITE_NU
39056 4c 4c 20 29 20 69 73 45 71 20 3d 20 31 3b 0a 20  LL ) isEq = 1;. 
39057 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
39058 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54  rt( eType==SQLIT
39059 45 5f 54 45 58 54 20 7c 7c 20 65 54 79 70 65 3d  E_TEXT || eType=
3905a 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 3b 0a  =SQLITE_BLOB );.
3905b 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
3905c 49 64 78 2d 3e 6e 53 61 6d 70 6c 65 3b 20 69 2b  Idx->nSample; i+
3905d 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 53  +){.      if( aS
3905e 61 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3d 3d  ample[i].eType==
3905f 53 51 4c 49 54 45 5f 54 45 58 54 20 7c 7c 20 61  SQLITE_TEXT || a
39060 53 61 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3d  Sample[i].eType=
39061 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a  =SQLITE_BLOB ){.
39062 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
39063 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
39064 20 69 66 28 20 69 3c 70 49 64 78 2d 3e 6e 53 61   if( i<pIdx->nSa
39065 6d 70 6c 65 20 29 7b 20 20 20 20 20 20 0a 20 20  mple ){      .  
39066 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
39067 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
39068 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
39069 6c 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  ll;.      const 
3906a 75 38 20 2a 7a 3b 0a 20 20 20 20 20 20 69 66 28  u8 *z;.      if(
3906b 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42   eType==SQLITE_B
3906c 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  LOB ){.        z
3906d 20 3d 20 28 63 6f 6e 73 74 20 75 38 20 2a 29 73   = (const u8 *)s
3906e 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
3906f 62 28 70 56 61 6c 29 3b 0a 20 20 20 20 20 20 20  b(pVal);.       
39070 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66   pColl = db->pDf
39071 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20  ltColl;.        
39072 61 73 73 65 72 74 28 20 70 43 6f 6c 6c 2d 3e 65  assert( pColl->e
39073 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
39074 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
39075 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20          pColl = 
39076 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65  sqlite3GetCollSe
39077 71 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  q(pParse, SQLITE
39078 5f 55 54 46 38 2c 20 30 2c 20 2a 70 49 64 78 2d  _UTF8, 0, *pIdx-
39079 3e 61 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20  >azColl);.      
3907a 20 20 69 66 28 20 70 43 6f 6c 6c 3d 3d 30 20 29    if( pColl==0 )
3907b 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
3907c 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
3907d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3907e 20 20 20 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38     z = (const u8
3907f 20 2a 29 73 71 6c 69 74 65 33 56 61 6c 75 65 54   *)sqlite3ValueT
39080 65 78 74 28 70 56 61 6c 2c 20 70 43 6f 6c 6c 2d  ext(pVal, pColl-
39081 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 69  >enc);.        i
39082 66 28 20 21 7a 20 29 7b 0a 20 20 20 20 20 20 20  f( !z ){.       
39083 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
39084 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
39085 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  }.        assert
39086 28 20 7a 20 26 26 20 70 43 6f 6c 6c 20 26 26 20  ( z && pColl && 
39087 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a 20  pColl->xCmp );. 
39088 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 20 3d       }.      n =
39089 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74   sqlite3ValueByt
3908a 65 73 28 70 56 61 6c 2c 20 70 43 6f 6c 6c 2d 3e  es(pVal, pColl->
3908b 65 6e 63 29 3b 0a 20 20 0a 20 20 20 20 20 20 66  enc);.  .      f
3908c 6f 72 28 3b 20 69 3c 70 49 64 78 2d 3e 6e 53 61  or(; i<pIdx->nSa
3908d 6d 70 6c 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  mple; i++){.    
3908e 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20      int c;.     
3908f 20 20 20 69 6e 74 20 65 53 61 6d 70 6c 65 74 79     int eSamplety
39090 70 65 20 3d 20 61 53 61 6d 70 6c 65 5b 69 5d 2e  pe = aSample[i].
39091 65 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 69  eType;.        i
39092 66 28 20 65 53 61 6d 70 6c 65 74 79 70 65 3c 65  f( eSampletype<e
39093 54 79 70 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b  Type ) continue;
39094 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 53 61  .        if( eSa
39095 6d 70 6c 65 74 79 70 65 21 3d 65 54 79 70 65 20  mpletype!=eType 
39096 29 20 62 72 65 61 6b 3b 0a 23 69 66 6e 64 65 66  ) break;.#ifndef
39097 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
39098 31 36 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  16.        if( p
39099 43 6f 6c 6c 2d 3e 65 6e 63 21 3d 53 51 4c 49 54  Coll->enc!=SQLIT
3909a 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20  E_UTF8 ){.      
3909b 20 20 20 20 69 6e 74 20 6e 53 61 6d 70 6c 65 3b      int nSample;
3909c 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20  .          char 
3909d 2a 7a 53 61 6d 70 6c 65 20 3d 20 73 71 6c 69 74  *zSample = sqlit
3909e 65 33 55 74 66 38 74 6f 31 36 28 0a 20 20 20 20  e3Utf8to16(.    
3909f 20 20 20 20 20 20 20 20 20 20 64 62 2c 20 70 43            db, pC
390a0 6f 6c 6c 2d 3e 65 6e 63 2c 20 61 53 61 6d 70 6c  oll->enc, aSampl
390a1 65 5b 69 5d 2e 75 2e 7a 2c 20 61 53 61 6d 70 6c  e[i].u.z, aSampl
390a2 65 5b 69 5d 2e 6e 42 79 74 65 2c 20 26 6e 53 61  e[i].nByte, &nSa
390a3 6d 70 6c 65 0a 20 20 20 20 20 20 20 20 20 20 29  mple.          )
390a4 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
390a5 21 7a 53 61 6d 70 6c 65 20 29 7b 0a 20 20 20 20  !zSample ){.    
390a6 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
390a7 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
390a8 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
390a9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
390aa 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  MEM;.          }
390ab 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 70  .          c = p
390ac 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c  Coll->xCmp(pColl
390ad 2d 3e 70 55 73 65 72 2c 20 6e 53 61 6d 70 6c 65  ->pUser, nSample
390ae 2c 20 7a 53 61 6d 70 6c 65 2c 20 6e 2c 20 7a 29  , zSample, n, z)
390af 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
390b0 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53  te3DbFree(db, zS
390b1 61 6d 70 6c 65 29 3b 0a 20 20 20 20 20 20 20 20  ample);.        
390b2 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20  }else.#endif.   
390b3 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20       {.         
390b4 20 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70   c = pColl->xCmp
390b5 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 61  (pColl->pUser, a
390b6 53 61 6d 70 6c 65 5b 69 5d 2e 6e 42 79 74 65 2c  Sample[i].nByte,
390b7 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e 7a 2c   aSample[i].u.z,
390b8 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20   n, z);.        
390b9 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3e  }.        if( c>
390ba 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
390bb 69 66 28 20 63 3d 3d 30 20 29 20 69 73 45 71 20  if( c==0 ) isEq 
390bc 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 62  = 1;.          b
390bd 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
390be 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
390bf 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20  }..  /* At this 
390c0 70 6f 69 6e 74 2c 20 61 53 61 6d 70 6c 65 5b 69  point, aSample[i
390c1 5d 20 69 73 20 74 68 65 20 66 69 72 73 74 20 73  ] is the first s
390c2 61 6d 70 6c 65 20 74 68 61 74 20 69 73 20 67 72  ample that is gr
390c3 65 61 74 65 72 20 74 68 61 6e 0a 20 20 2a 2a 20  eater than.  ** 
390c4 6f 72 20 65 71 75 61 6c 20 74 6f 20 70 56 61 6c  or equal to pVal
390c5 2e 20 20 4f 72 20 69 66 20 69 3d 3d 70 49 64 78  .  Or if i==pIdx
390c6 2d 3e 6e 53 61 6d 70 6c 65 2c 20 74 68 65 6e 20  ->nSample, then 
390c7 61 6c 6c 20 73 61 6d 70 6c 65 73 20 61 72 65 20  all samples are 
390c8 6c 65 73 73 0a 20 20 2a 2a 20 74 68 61 6e 20 70  less.  ** than p
390c9 56 61 6c 2e 20 20 49 66 20 61 53 61 6d 70 6c 65  Val.  If aSample
390ca 5b 69 5d 3d 3d 70 56 61 6c 2c 20 74 68 65 6e 20  [i]==pVal, then 
390cb 69 73 45 71 3d 3d 31 2e 0a 20 20 2a 2f 0a 20 20  isEq==1..  */.  
390cc 69 66 28 20 69 73 45 71 20 29 7b 0a 20 20 20 20  if( isEq ){.    
390cd 61 73 73 65 72 74 28 20 69 3c 70 49 64 78 2d 3e  assert( i<pIdx->
390ce 6e 53 61 6d 70 6c 65 20 29 3b 0a 20 20 20 20 61  nSample );.    a
390cf 53 74 61 74 5b 30 5d 20 3d 20 61 53 61 6d 70 6c  Stat[0] = aSampl
390d0 65 5b 69 5d 2e 6e 4c 74 3b 0a 20 20 20 20 61 53  e[i].nLt;.    aS
390d1 74 61 74 5b 31 5d 20 3d 20 61 53 61 6d 70 6c 65  tat[1] = aSample
390d2 5b 69 5d 2e 6e 45 71 3b 0a 20 20 7d 65 6c 73 65  [i].nEq;.  }else
390d3 7b 0a 20 20 20 20 74 52 6f 77 63 6e 74 20 69 4c  {.    tRowcnt iL
390d4 6f 77 65 72 2c 20 69 55 70 70 65 72 2c 20 69 47  ower, iUpper, iG
390d5 61 70 3b 0a 20 20 20 20 69 66 28 20 69 3d 3d 30  ap;.    if( i==0
390d6 20 29 7b 0a 20 20 20 20 20 20 69 4c 6f 77 65 72   ){.      iLower
390d7 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 55 70 70   = 0;.      iUpp
390d8 65 72 20 3d 20 61 53 61 6d 70 6c 65 5b 30 5d 2e  er = aSample[0].
390d9 6e 4c 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  nLt;.    }else{.
390da 20 20 20 20 20 20 69 55 70 70 65 72 20 3d 20 69        iUpper = i
390db 3e 3d 70 49 64 78 2d 3e 6e 53 61 6d 70 6c 65 20  >=pIdx->nSample 
390dc 3f 20 6e 20 3a 20 61 53 61 6d 70 6c 65 5b 69 5d  ? n : aSample[i]
390dd 2e 6e 4c 74 3b 0a 20 20 20 20 20 20 69 4c 6f 77  .nLt;.      iLow
390de 65 72 20 3d 20 61 53 61 6d 70 6c 65 5b 69 2d 31  er = aSample[i-1
390df 5d 2e 6e 45 71 20 2b 20 61 53 61 6d 70 6c 65 5b  ].nEq + aSample[
390e0 69 2d 31 5d 2e 6e 4c 74 3b 0a 20 20 20 20 7d 0a  i-1].nLt;.    }.
390e1 20 20 20 20 61 53 74 61 74 5b 31 5d 20 3d 20 70      aStat[1] = p
390e2 49 64 78 2d 3e 61 76 67 45 71 3b 0a 20 20 20 20  Idx->avgEq;.    
390e3 69 66 28 20 69 4c 6f 77 65 72 3e 3d 69 55 70 70  if( iLower>=iUpp
390e4 65 72 20 29 7b 0a 20 20 20 20 20 20 69 47 61 70  er ){.      iGap
390e5 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
390e6 0a 20 20 20 20 20 20 69 47 61 70 20 3d 20 69 55  .      iGap = iU
390e7 70 70 65 72 20 2d 20 69 4c 6f 77 65 72 3b 0a 20  pper - iLower;. 
390e8 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 6f 75     }.    if( rou
390e9 6e 64 55 70 20 29 7b 0a 20 20 20 20 20 20 69 47  ndUp ){.      iG
390ea 61 70 20 3d 20 28 69 47 61 70 2a 32 29 2f 33 3b  ap = (iGap*2)/3;
390eb 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
390ec 20 20 69 47 61 70 20 3d 20 69 47 61 70 2f 33 3b    iGap = iGap/3;
390ed 0a 20 20 20 20 7d 0a 20 20 20 20 61 53 74 61 74  .    }.    aStat
390ee 5b 30 5d 20 3d 20 69 4c 6f 77 65 72 20 2b 20 69  [0] = iLower + i
390ef 47 61 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  Gap;.  }.  retur
390f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
390f1 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
390f2 45 4e 41 42 4c 45 5f 53 54 41 54 33 20 2a 2f 0a  ENABLE_STAT3 */.
390f3 0a 2f 2a 0a 2a 2a 20 49 66 20 65 78 70 72 65 73  ./*.** If expres
390f4 73 69 6f 6e 20 70 45 78 70 72 20 72 65 70 72 65  sion pExpr repre
390f5 73 65 6e 74 73 20 61 20 6c 69 74 65 72 61 6c 20  sents a literal 
390f6 76 61 6c 75 65 2c 20 73 65 74 20 2a 70 70 20 74  value, set *pp t
390f7 6f 20 70 6f 69 6e 74 20 74 6f 0a 2a 2a 20 61 6e  o point to.** an
390f8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 73   sqlite3_value s
390f9 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
390fa 69 6e 67 20 74 68 65 20 73 61 6d 65 20 76 61 6c  ing the same val
390fb 75 65 2c 20 77 69 74 68 20 61 66 66 69 6e 69 74  ue, with affinit
390fc 79 0a 2a 2a 20 61 66 66 20 61 70 70 6c 69 65 64  y.** aff applied
390fd 20 74 6f 20 69 74 2c 20 62 65 66 6f 72 65 20 72   to it, before r
390fe 65 74 75 72 6e 69 6e 67 2e 20 49 74 20 69 73 20  eturning. It is 
390ff 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  the responsibili
39100 74 79 20 6f 66 20 74 68 65 20 0a 2a 2a 20 63 61  ty of the .** ca
39101 6c 6c 65 72 20 74 6f 20 65 76 65 6e 74 75 61 6c  ller to eventual
39102 6c 79 20 72 65 6c 65 61 73 65 20 74 68 69 73 20  ly release this 
39103 73 74 72 75 63 74 75 72 65 20 62 79 20 70 61 73  structure by pas
39104 73 69 6e 67 20 69 74 20 74 6f 20 0a 2a 2a 20 73  sing it to .** s
39105 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
39106 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  )..**.** If the 
39107 63 75 72 72 65 6e 74 20 70 61 72 73 65 20 69 73  current parse is
39108 20 61 20 72 65 63 6f 6d 70 69 6c 65 20 28 73 71   a recompile (sq
39109 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 29  lite3Reprepare()
3910a 29 20 61 6e 64 20 70 45 78 70 72 0a 2a 2a 20 69  ) and pExpr.** i
3910b 73 20 61 6e 20 53 51 4c 20 76 61 72 69 61 62 6c  s an SQL variabl
3910c 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79  e that currently
3910d 20 68 61 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20   has a non-NULL 
3910e 76 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20 69  value bound to i
3910f 74 2c 0a 2a 2a 20 63 72 65 61 74 65 20 61 6e 20  t,.** create an 
39110 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 73 74  sqlite3_value st
39111 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69  ructure containi
39112 6e 67 20 74 68 69 73 20 76 61 6c 75 65 2c 20 61  ng this value, a
39113 67 61 69 6e 20 77 69 74 68 0a 2a 2a 20 61 66 66  gain with.** aff
39114 69 6e 69 74 79 20 61 66 66 20 61 70 70 6c 69 65  inity aff applie
39115 64 20 74 6f 20 69 74 2c 20 69 6e 73 74 65 61 64  d to it, instead
39116 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 65 69 74 68  ..**.** If neith
39117 65 72 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20  er of the above 
39118 61 70 70 6c 79 2c 20 73 65 74 20 2a 70 70 20 74  apply, set *pp t
39119 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66  o NULL..**.** If
3911a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
3911b 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  , return an erro
3911c 72 20 63 6f 64 65 2e 20 4f 74 68 65 72 77 69 73  r code. Otherwis
3911d 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f  e, SQLITE_OK..*/
3911e 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
3911f 4e 41 42 4c 45 5f 53 54 41 54 33 0a 73 74 61 74  NABLE_STAT3.stat
39120 69 63 20 69 6e 74 20 76 61 6c 75 65 46 72 6f 6d  ic int valueFrom
39121 45 78 70 72 28 0a 20 20 50 61 72 73 65 20 2a 70  Expr(.  Parse *p
39122 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72 20 2a  Parse, .  Expr *
39123 70 45 78 70 72 2c 20 0a 20 20 75 38 20 61 66 66  pExpr, .  u8 aff
39124 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  , .  sqlite3_val
39125 75 65 20 2a 2a 70 70 0a 29 7b 0a 20 20 69 66 28  ue **pp.){.  if(
39126 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 56   pExpr->op==TK_V
39127 41 52 49 41 42 4c 45 0a 20 20 20 7c 7c 20 28 70  ARIABLE.   || (p
39128 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47  Expr->op==TK_REG
39129 49 53 54 45 52 20 26 26 20 70 45 78 70 72 2d 3e  ISTER && pExpr->
3912a 6f 70 32 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45  op2==TK_VARIABLE
3912b 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ).  ){.    int i
3912c 56 61 72 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f  Var = pExpr->iCo
3912d 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65  lumn;.    sqlite
3912e 33 56 64 62 65 53 65 74 56 61 72 6d 61 73 6b 28  3VdbeSetVarmask(
3912f 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 69  pParse->pVdbe, i
39130 56 61 72 29 3b 0a 20 20 20 20 2a 70 70 20 3d 20  Var);.    *pp = 
39131 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 56 61  sqlite3VdbeGetVa
39132 6c 75 65 28 70 50 61 72 73 65 2d 3e 70 52 65 70  lue(pParse->pRep
39133 72 65 70 61 72 65 2c 20 69 56 61 72 2c 20 61 66  repare, iVar, af
39134 66 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  f);.    return S
39135 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
39136 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61  return sqlite3Va
39137 6c 75 65 46 72 6f 6d 45 78 70 72 28 70 50 61 72  lueFromExpr(pPar
39138 73 65 2d 3e 64 62 2c 20 70 45 78 70 72 2c 20 53  se->db, pExpr, S
39139 51 4c 49 54 45 5f 55 54 46 38 2c 20 61 66 66 2c  QLITE_UTF8, aff,
3913a 20 70 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a   pp);.}.#endif..
3913b 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
3913c 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 65  ion is used to e
3913d 73 74 69 6d 61 74 65 20 74 68 65 20 6e 75 6d 62  stimate the numb
3913e 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20  er of rows that 
3913f 77 69 6c 6c 20 62 65 20 76 69 73 69 74 65 64 0a  will be visited.
39140 2a 2a 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 61  ** by scanning a
39141 6e 20 69 6e 64 65 78 20 66 6f 72 20 61 20 72 61  n index for a ra
39142 6e 67 65 20 6f 66 20 76 61 6c 75 65 73 2e 20 54  nge of values. T
39143 68 65 20 72 61 6e 67 65 20 6d 61 79 20 68 61 76  he range may hav
39144 65 20 61 6e 20 75 70 70 65 72 0a 2a 2a 20 62 6f  e an upper.** bo
39145 75 6e 64 2c 20 61 20 6c 6f 77 65 72 20 62 6f 75  und, a lower bou
39146 6e 64 2c 20 6f 72 20 62 6f 74 68 2e 20 54 68 65  nd, or both. The
39147 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
39148 72 6d 73 20 74 68 61 74 20 73 65 74 20 74 68 65  rms that set the
39149 20 75 70 70 65 72 0a 2a 2a 20 61 6e 64 20 6c 6f   upper.** and lo
3914a 77 65 72 20 62 6f 75 6e 64 73 20 61 72 65 20 72  wer bounds are r
3914b 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 70 4c  epresented by pL
3914c 6f 77 65 72 20 61 6e 64 20 70 55 70 70 65 72 20  ower and pUpper 
3914d 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 46 6f  respectively. Fo
3914e 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 73  r.** example, as
3914f 73 75 6d 69 6e 67 20 74 68 61 74 20 69 6e 64 65  suming that inde
39150 78 20 70 20 69 73 20 6f 6e 20 74 31 28 61 29 3a  x p is on t1(a):
39151 0a 2a 2a 0a 2a 2a 20 20 20 2e 2e 2e 20 46 52 4f  .**.**   ... FRO
39152 4d 20 74 31 20 57 48 45 52 45 20 61 20 3e 20 3f  M t1 WHERE a > ?
39153 20 41 4e 44 20 61 20 3c 20 3f 20 2e 2e 2e 0a 2a   AND a < ? ....*
39154 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
39155 20 20 20 20 20 7c 5f 5f 5f 5f 5f 7c 20 20 20 7c       |_____|   |
39156 5f 5f 5f 5f 5f 7c 0a 2a 2a 20 20 20 20 20 20 20  _____|.**       
39157 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39158 7c 20 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20  |         |.**  
39159 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3915a 20 20 20 70 4c 6f 77 65 72 20 20 20 20 70 55 70     pLower    pUp
3915b 70 65 72 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74  per.**.** If eit
3915c 68 65 72 20 6f 66 20 74 68 65 20 75 70 70 65 72  her of the upper
3915d 20 6f 72 20 6c 6f 77 65 72 20 62 6f 75 6e 64 20   or lower bound 
3915e 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 2c 20  is not present, 
3915f 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 70 61 73  then NULL is pas
39160 73 65 64 20 69 6e 0a 2a 2a 20 70 6c 61 63 65 20  sed in.** place 
39161 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  of the correspon
39162 64 69 6e 67 20 57 68 65 72 65 54 65 72 6d 2e 0a  ding WhereTerm..
39163 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45 71 20 70 61  **.** The nEq pa
39164 72 61 6d 65 74 65 72 20 69 73 20 70 61 73 73 65  rameter is passe
39165 64 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  d the index of t
39166 68 65 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 20  he index column 
39167 73 75 62 6a 65 63 74 20 74 6f 20 74 68 65 0a 2a  subject to the.*
39168 2a 20 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69  * range constrai
39169 6e 74 2e 20 4f 72 2c 20 65 71 75 69 76 61 6c 65  nt. Or, equivale
3916a 6e 74 6c 79 2c 20 74 68 65 20 6e 75 6d 62 65 72  ntly, the number
3916b 20 6f 66 20 65 71 75 61 6c 69 74 79 20 63 6f 6e   of equality con
3916c 73 74 72 61 69 6e 74 73 0a 2a 2a 20 6f 70 74 69  straints.** opti
3916d 6d 69 7a 65 64 20 62 79 20 74 68 65 20 70 72 6f  mized by the pro
3916e 70 6f 73 65 64 20 69 6e 64 65 78 20 73 63 61 6e  posed index scan
3916f 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61  . For example, a
39170 73 73 75 6d 69 6e 67 20 69 6e 64 65 78 20 70 20  ssuming index p 
39171 69 73 0a 2a 2a 20 6f 6e 20 74 31 28 61 2c 20 62  is.** on t1(a, b
39172 29 2c 20 61 6e 64 20 74 68 65 20 53 51 4c 20 71  ), and the SQL q
39173 75 65 72 79 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  uery is:.**.**  
39174 20 2e 2e 2e 20 46 52 4f 4d 20 74 31 20 57 48 45   ... FROM t1 WHE
39175 52 45 20 61 20 3d 20 3f 20 41 4e 44 20 62 20 3e  RE a = ? AND b >
39176 20 3f 20 41 4e 44 20 62 20 3c 20 3f 20 2e 2e 2e   ? AND b < ? ...
39177 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 6e 45 71 20  .**.** then nEq 
39178 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
39179 20 74 68 65 20 76 61 6c 75 65 20 31 20 28 61 73   the value 1 (as
3917a 20 74 68 65 20 72 61 6e 67 65 20 72 65 73 74 72   the range restr
3917b 69 63 74 65 64 20 63 6f 6c 75 6d 6e 2c 0a 2a 2a  icted column,.**
3917c 20 62 2c 20 69 73 20 74 68 65 20 73 65 63 6f 6e   b, is the secon
3917d 64 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75  d left-most colu
3917e 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 29  mn of the index)
3917f 2e 20 4f 72 2c 20 69 66 20 74 68 65 20 71 75 65  . Or, if the que
39180 72 79 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2e  ry is:.**.**   .
39181 2e 2e 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45  .. FROM t1 WHERE
39182 20 61 20 3e 20 3f 20 41 4e 44 20 61 20 3c 20 3f   a > ? AND a < ?
39183 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20   ....**.** then 
39184 6e 45 71 20 73 68 6f 75 6c 64 20 62 65 20 70 61  nEq should be pa
39185 73 73 65 64 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68  ssed 0..**.** Th
39186 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65  e returned value
39187 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 64   is an integer d
39188 69 76 69 73 6f 72 20 74 6f 20 72 65 64 75 63 65  ivisor to reduce
39189 20 74 68 65 20 65 73 74 69 6d 61 74 65 64 0a 2a   the estimated.*
3918a 2a 20 73 65 61 72 63 68 20 73 70 61 63 65 2e 20  * search space. 
3918b 20 41 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20   A return value 
3918c 6f 66 20 31 20 6d 65 61 6e 73 20 74 68 61 74 20  of 1 means that 
3918d 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74  range constraint
3918e 73 20 61 72 65 0a 2a 2a 20 6e 6f 20 68 65 6c 70  s are.** no help
3918f 20 61 74 20 61 6c 6c 2e 20 20 41 20 72 65 74 75   at all.  A retu
39190 72 6e 20 76 61 6c 75 65 20 6f 66 20 32 20 6d 65  rn value of 2 me
39191 61 6e 73 20 72 61 6e 67 65 20 63 6f 6e 73 74 72  ans range constr
39192 61 69 6e 74 73 20 61 72 65 0a 2a 2a 20 65 78 70  aints are.** exp
39193 65 63 74 65 64 20 74 6f 20 72 65 64 75 63 65 20  ected to reduce 
39194 74 68 65 20 73 65 61 72 63 68 20 73 70 61 63 65  the search space
39195 20 62 79 20 68 61 6c 66 2e 20 20 41 6e 64 20 73   by half.  And s
39196 6f 20 66 6f 72 74 68 2e 2e 2e 0a 2a 2a 0a 2a 2a  o forth....**.**
39197 20 49 6e 20 74 68 65 20 61 62 73 65 6e 63 65 20   In the absence 
39198 6f 66 20 73 71 6c 69 74 65 5f 73 74 61 74 33 20  of sqlite_stat3 
39199 41 4e 41 4c 59 5a 45 20 64 61 74 61 2c 20 65 61  ANALYZE data, ea
3919a 63 68 20 72 61 6e 67 65 20 69 6e 65 71 75 61 6c  ch range inequal
3919b 69 74 79 0a 2a 2a 20 72 65 64 75 63 65 73 20 74  ity.** reduces t
3919c 68 65 20 73 65 61 72 63 68 20 73 70 61 63 65 20  he search space 
3919d 62 79 20 61 20 66 61 63 74 6f 72 20 6f 66 20 34  by a factor of 4
3919e 2e 20 20 48 65 6e 63 65 20 61 20 73 69 6e 67 6c  .  Hence a singl
3919f 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 28 78 3e  e constraint (x>
391a0 3f 29 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e  ?).** results in
391a1 20 61 20 72 65 74 75 72 6e 20 6f 66 20 34 20 61   a return of 4 a
391a2 6e 64 20 61 20 72 61 6e 67 65 20 63 6f 6e 73 74  nd a range const
391a3 72 61 69 6e 74 20 28 78 3e 3f 20 41 4e 44 20 78  raint (x>? AND x
391a4 3c 3f 29 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69  <?) results.** i
391a5 6e 20 61 20 72 65 74 75 72 6e 20 6f 66 20 31 36  n a return of 16
391a6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
391a7 77 68 65 72 65 52 61 6e 67 65 53 63 61 6e 45 73  whereRangeScanEs
391a8 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
391a9 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72  se,       /* Par
391aa 73 69 6e 67 20 26 20 63 6f 64 65 20 67 65 6e 65  sing & code gene
391ab 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  rating context *
391ac 2f 0a 20 20 49 6e 64 65 78 20 2a 70 2c 20 20 20  /.  Index *p,   
391ad 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
391ae 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67  index containing
391af 20 74 68 65 20 72 61 6e 67 65 2d 63 6f 6d 70 61   the range-compa
391b0 72 65 64 20 63 6f 6c 75 6d 6e 3b 20 22 78 22 20  red column; "x" 
391b1 2a 2f 0a 20 20 69 6e 74 20 6e 45 71 2c 20 20 20  */.  int nEq,   
391b2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 64            /* ind
391b3 65 78 20 69 6e 74 6f 20 70 2d 3e 61 43 6f 6c 5b  ex into p->aCol[
391b4 5d 20 6f 66 20 74 68 65 20 72 61 6e 67 65 2d 63  ] of the range-c
391b5 6f 6d 70 61 72 65 64 20 63 6f 6c 75 6d 6e 20 2a  ompared column *
391b6 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  /.  WhereTerm *p
391b7 4c 6f 77 65 72 2c 20 20 20 2f 2a 20 4c 6f 77 65  Lower,   /* Lowe
391b8 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 72  r bound on the r
391b9 61 6e 67 65 2e 20 65 78 3a 20 22 78 3e 31 32 33  ange. ex: "x>123
391ba 22 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20  " Might be NULL 
391bb 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  */.  WhereTerm *
391bc 70 55 70 70 65 72 2c 20 20 20 2f 2a 20 55 70 70  pUpper,   /* Upp
391bd 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20  er bound on the 
391be 72 61 6e 67 65 2e 20 65 78 3a 20 22 78 3c 34 35  range. ex: "x<45
391bf 35 22 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c  5" Might be NULL
391c0 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 2a 70 52   */.  double *pR
391c1 61 6e 67 65 44 69 76 20 20 20 2f 2a 20 4f 55 54  angeDiv   /* OUT
391c2 3a 20 52 65 64 75 63 65 20 73 65 61 72 63 68 20  : Reduce search 
391c3 73 70 61 63 65 20 62 79 20 74 68 69 73 20 64 69  space by this di
391c4 76 69 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e  visor */.){.  in
391c5 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
391c6 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
391c7 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 0a 0a 20  _ENABLE_STAT3.. 
391c8 20 69 66 28 20 6e 45 71 3d 3d 30 20 26 26 20 70   if( nEq==0 && p
391c9 2d 3e 6e 53 61 6d 70 6c 65 20 29 7b 0a 20 20 20  ->nSample ){.   
391ca 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
391cb 70 52 61 6e 67 65 56 61 6c 3b 0a 20 20 20 20 74  pRangeVal;.    t
391cc 52 6f 77 63 6e 74 20 69 4c 6f 77 65 72 20 3d 20  Rowcnt iLower = 
391cd 30 3b 0a 20 20 20 20 74 52 6f 77 63 6e 74 20 69  0;.    tRowcnt i
391ce 55 70 70 65 72 20 3d 20 70 2d 3e 61 69 52 6f 77  Upper = p->aiRow
391cf 45 73 74 5b 30 5d 3b 0a 20 20 20 20 74 52 6f 77  Est[0];.    tRow
391d0 63 6e 74 20 61 5b 32 5d 3b 0a 20 20 20 20 75 38  cnt a[2];.    u8
391d1 20 61 66 66 20 3d 20 70 2d 3e 70 54 61 62 6c 65   aff = p->pTable
391d2 2d 3e 61 43 6f 6c 5b 70 2d 3e 61 69 43 6f 6c 75  ->aCol[p->aiColu
391d3 6d 6e 5b 30 5d 5d 2e 61 66 66 69 6e 69 74 79 3b  mn[0]].affinity;
391d4 0a 0a 20 20 20 20 69 66 28 20 70 4c 6f 77 65 72  ..    if( pLower
391d5 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a   ){.      Expr *
391d6 70 45 78 70 72 20 3d 20 70 4c 6f 77 65 72 2d 3e  pExpr = pLower->
391d7 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20  pExpr->pRight;. 
391d8 20 20 20 20 20 72 63 20 3d 20 76 61 6c 75 65 46       rc = valueF
391d9 72 6f 6d 45 78 70 72 28 70 50 61 72 73 65 2c 20  romExpr(pParse, 
391da 70 45 78 70 72 2c 20 61 66 66 2c 20 26 70 52 61  pExpr, aff, &pRa
391db 6e 67 65 56 61 6c 29 3b 0a 20 20 20 20 20 20 61  ngeVal);.      a
391dc 73 73 65 72 74 28 20 70 4c 6f 77 65 72 2d 3e 65  ssert( pLower->e
391dd 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 47 54 20  Operator==WO_GT 
391de 7c 7c 20 70 4c 6f 77 65 72 2d 3e 65 4f 70 65 72  || pLower->eOper
391df 61 74 6f 72 3d 3d 57 4f 5f 47 45 20 29 3b 0a 20  ator==WO_GE );. 
391e0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
391e1 49 54 45 5f 4f 4b 0a 20 20 20 20 20 20 20 26 26  ITE_OK.       &&
391e2 20 77 68 65 72 65 4b 65 79 53 74 61 74 73 28 70   whereKeyStats(p
391e3 50 61 72 73 65 2c 20 70 2c 20 70 52 61 6e 67 65  Parse, p, pRange
391e4 56 61 6c 2c 20 30 2c 20 61 29 3d 3d 53 51 4c 49  Val, 0, a)==SQLI
391e5 54 45 5f 4f 4b 0a 20 20 20 20 20 20 29 7b 0a 20  TE_OK.      ){. 
391e6 20 20 20 20 20 20 20 69 4c 6f 77 65 72 20 3d 20         iLower = 
391e7 61 5b 30 5d 3b 0a 20 20 20 20 20 20 20 20 69 66  a[0];.        if
391e8 28 20 70 4c 6f 77 65 72 2d 3e 65 4f 70 65 72 61  ( pLower->eOpera
391e9 74 6f 72 3d 3d 57 4f 5f 47 54 20 29 20 69 4c 6f  tor==WO_GT ) iLo
391ea 77 65 72 20 2b 3d 20 61 5b 31 5d 3b 0a 20 20 20  wer += a[1];.   
391eb 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
391ec 65 33 56 61 6c 75 65 46 72 65 65 28 70 52 61 6e  e3ValueFree(pRan
391ed 67 65 56 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20  geVal);.    }.  
391ee 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
391ef 5f 4f 4b 20 26 26 20 70 55 70 70 65 72 20 29 7b  _OK && pUpper ){
391f0 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78  .      Expr *pEx
391f1 70 72 20 3d 20 70 55 70 70 65 72 2d 3e 70 45 78  pr = pUpper->pEx
391f2 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  pr->pRight;.    
391f3 20 20 72 63 20 3d 20 76 61 6c 75 65 46 72 6f 6d    rc = valueFrom
391f4 45 78 70 72 28 70 50 61 72 73 65 2c 20 70 45 78  Expr(pParse, pEx
391f5 70 72 2c 20 61 66 66 2c 20 26 70 52 61 6e 67 65  pr, aff, &pRange
391f6 56 61 6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Val);.      asse
391f7 72 74 28 20 70 55 70 70 65 72 2d 3e 65 4f 70 65  rt( pUpper->eOpe
391f8 72 61 74 6f 72 3d 3d 57 4f 5f 4c 54 20 7c 7c 20  rator==WO_LT || 
391f9 70 55 70 70 65 72 2d 3e 65 4f 70 65 72 61 74 6f  pUpper->eOperato
391fa 72 3d 3d 57 4f 5f 4c 45 20 29 3b 0a 20 20 20 20  r==WO_LE );.    
391fb 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
391fc 5f 4f 4b 0a 20 20 20 20 20 20 20 26 26 20 77 68  _OK.       && wh
391fd 65 72 65 4b 65 79 53 74 61 74 73 28 70 50 61 72  ereKeyStats(pPar
391fe 73 65 2c 20 70 2c 20 70 52 61 6e 67 65 56 61 6c  se, p, pRangeVal
391ff 2c 20 31 2c 20 61 29 3d 3d 53 51 4c 49 54 45 5f  , 1, a)==SQLITE_
39200 4f 4b 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  OK.      ){.    
39201 20 20 20 20 69 55 70 70 65 72 20 3d 20 61 5b 30      iUpper = a[0
39202 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ];.        if( p
39203 55 70 70 65 72 2d 3e 65 4f 70 65 72 61 74 6f 72  Upper->eOperator
39204 3d 3d 57 4f 5f 4c 45 20 29 20 69 55 70 70 65 72  ==WO_LE ) iUpper
39205 20 2b 3d 20 61 5b 31 5d 3b 0a 20 20 20 20 20 20   += a[1];.      
39206 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  }.      sqlite3V
39207 61 6c 75 65 46 72 65 65 28 70 52 61 6e 67 65 56  alueFree(pRangeV
39208 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  al);.    }.    i
39209 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
3920a 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 55   ){.      if( iU
3920b 70 70 65 72 3c 3d 69 4c 6f 77 65 72 20 29 7b 0a  pper<=iLower ){.
3920c 20 20 20 20 20 20 20 20 2a 70 52 61 6e 67 65 44          *pRangeD
3920d 69 76 20 3d 20 28 64 6f 75 62 6c 65 29 70 2d 3e  iv = (double)p->
3920e 61 69 52 6f 77 45 73 74 5b 30 5d 3b 0a 20 20 20  aiRowEst[0];.   
3920f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
39210 20 20 2a 70 52 61 6e 67 65 44 69 76 20 3d 20 28    *pRangeDiv = (
39211 64 6f 75 62 6c 65 29 70 2d 3e 61 69 52 6f 77 45  double)p->aiRowE
39212 73 74 5b 30 5d 2f 28 64 6f 75 62 6c 65 29 28 69  st[0]/(double)(i
39213 55 70 70 65 72 20 2d 20 69 4c 6f 77 65 72 29 3b  Upper - iLower);
39214 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 57  .      }.      W
39215 48 45 52 45 54 52 41 43 45 28 28 22 72 61 6e 67  HERETRACE(("rang
39216 65 20 73 63 61 6e 20 72 65 67 69 6f 6e 73 3a 20  e scan regions: 
39217 25 75 2e 2e 25 75 20 20 64 69 76 3d 25 67 5c 6e  %u..%u  div=%g\n
39218 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
39219 20 20 20 20 20 28 75 33 32 29 69 4c 6f 77 65 72       (u32)iLower
3921a 2c 20 28 75 33 32 29 69 55 70 70 65 72 2c 20 2a  , (u32)iUpper, *
3921b 70 52 61 6e 67 65 44 69 76 29 29 3b 0a 20 20 20  pRangeDiv));.   
3921c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3921d 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  _OK;.    }.  }.#
3921e 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41  else.  UNUSED_PA
3921f 52 41 4d 45 54 45 52 28 70 50 61 72 73 65 29 3b  RAMETER(pParse);
39220 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
39221 54 45 52 28 70 29 3b 0a 20 20 55 4e 55 53 45 44  TER(p);.  UNUSED
39222 5f 50 41 52 41 4d 45 54 45 52 28 6e 45 71 29 3b  _PARAMETER(nEq);
39223 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74  .#endif.  assert
39224 28 20 70 4c 6f 77 65 72 20 7c 7c 20 70 55 70 70  ( pLower || pUpp
39225 65 72 20 29 3b 0a 20 20 2a 70 52 61 6e 67 65 44  er );.  *pRangeD
39226 69 76 20 3d 20 28 64 6f 75 62 6c 65 29 31 3b 0a  iv = (double)1;.
39227 20 20 69 66 28 20 70 4c 6f 77 65 72 20 26 26 20    if( pLower && 
39228 28 70 4c 6f 77 65 72 2d 3e 77 74 46 6c 61 67 73  (pLower->wtFlags
39229 20 26 20 54 45 52 4d 5f 56 4e 55 4c 4c 29 3d 3d   & TERM_VNULL)==
3922a 30 20 29 20 2a 70 52 61 6e 67 65 44 69 76 20 2a  0 ) *pRangeDiv *
3922b 3d 20 28 64 6f 75 62 6c 65 29 34 3b 0a 20 20 69  = (double)4;.  i
3922c 66 28 20 70 55 70 70 65 72 20 29 20 2a 70 52 61  f( pUpper ) *pRa
3922d 6e 67 65 44 69 76 20 2a 3d 20 28 64 6f 75 62 6c  ngeDiv *= (doubl
3922e 65 29 34 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  e)4;.  return rc
3922f 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
39230 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 0a  TE_ENABLE_STAT3.
39231 2f 2a 0a 2a 2a 20 45 73 74 69 6d 61 74 65 20 74  /*.** Estimate t
39232 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
39233 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 72  s that will be r
39234 65 74 75 72 6e 65 64 20 62 61 73 65 64 20 6f 6e  eturned based on
39235 0a 2a 2a 20 61 6e 20 65 71 75 61 6c 69 74 79 20  .** an equality 
39236 63 6f 6e 73 74 72 61 69 6e 74 20 78 3d 56 41 4c  constraint x=VAL
39237 55 45 20 61 6e 64 20 77 68 65 72 65 20 74 68 61  UE and where tha
39238 74 20 56 41 4c 55 45 20 6f 63 63 75 72 73 20 69  t VALUE occurs i
39239 6e 0a 2a 2a 20 74 68 65 20 68 69 73 74 6f 67 72  n.** the histogr
3923a 61 6d 20 64 61 74 61 2e 20 20 54 68 69 73 20 6f  am data.  This o
3923b 6e 6c 79 20 77 6f 72 6b 73 20 77 68 65 6e 20 78  nly works when x
3923c 20 69 73 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73   is the left-mos
3923d 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20 61  t.** column of a
3923e 6e 20 69 6e 64 65 78 20 61 6e 64 20 73 71 6c 69  n index and sqli
3923f 74 65 5f 73 74 61 74 33 20 68 69 73 74 6f 67 72  te_stat3 histogr
39240 61 6d 20 64 61 74 61 20 69 73 20 61 76 61 69 6c  am data is avail
39241 61 62 6c 65 0a 2a 2a 20 66 6f 72 20 74 68 61 74  able.** for that
39242 20 69 6e 64 65 78 2e 20 20 57 68 65 6e 20 70 45   index.  When pE
39243 78 70 72 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d  xpr==NULL that m
39244 65 61 6e 73 20 74 68 65 20 63 6f 6e 73 74 72 61  eans the constra
39245 69 6e 74 20 69 73 0a 2a 2a 20 22 78 20 49 53 20  int is.** "x IS 
39246 4e 55 4c 4c 22 20 69 6e 73 74 65 61 64 20 6f 66  NULL" instead of
39247 20 22 78 3d 56 41 4c 55 45 22 2e 0a 2a 2a 0a 2a   "x=VALUE"..**.*
39248 2a 20 57 72 69 74 65 20 74 68 65 20 65 73 74 69  * Write the esti
39249 6d 61 74 65 64 20 72 6f 77 20 63 6f 75 6e 74 20  mated row count 
3924a 69 6e 74 6f 20 2a 70 6e 52 6f 77 20 61 6e 64 20  into *pnRow and 
3924b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
3924c 2e 20 0a 2a 2a 20 49 66 20 75 6e 61 62 6c 65 20  . .** If unable 
3924d 74 6f 20 6d 61 6b 65 20 61 6e 20 65 73 74 69 6d  to make an estim
3924e 61 74 65 2c 20 6c 65 61 76 65 20 2a 70 6e 52 6f  ate, leave *pnRo
3924f 77 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20  w unchanged and 
39250 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65  return.** non-ze
39251 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ro..**.** This r
39252 6f 75 74 69 6e 65 20 63 61 6e 20 66 61 69 6c 20  outine can fail 
39253 69 66 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20  if it is unable 
39254 74 6f 20 6c 6f 61 64 20 61 20 63 6f 6c 6c 61 74  to load a collat
39255 69 6e 67 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20  ing sequence.** 
39256 72 65 71 75 69 72 65 64 20 66 6f 72 20 73 74 72  required for str
39257 69 6e 67 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20  ing comparison, 
39258 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 74 6f 20  or if unable to 
39259 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 0a  allocate memory.
3925a 2a 2a 20 66 6f 72 20 61 20 55 54 46 20 63 6f 6e  ** for a UTF con
3925b 76 65 72 73 69 6f 6e 20 72 65 71 75 69 72 65 64  version required
3925c 20 66 6f 72 20 63 6f 6d 70 61 72 69 73 6f 6e 2e   for comparison.
3925d 20 20 54 68 65 20 65 72 72 6f 72 20 69 73 20 73    The error is s
3925e 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20  tored.** in the 
3925f 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65  pParse structure
39260 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
39261 77 68 65 72 65 45 71 75 61 6c 53 63 61 6e 45 73  whereEqualScanEs
39262 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
39263 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72  se,       /* Par
39264 73 69 6e 67 20 26 20 63 6f 64 65 20 67 65 6e 65  sing & code gene
39265 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  rating context *
39266 2f 0a 20 20 49 6e 64 65 78 20 2a 70 2c 20 20 20  /.  Index *p,   
39267 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
39268 69 6e 64 65 78 20 77 68 6f 73 65 20 6c 65 66 74  index whose left
39269 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20  -most column is 
3926a 70 54 65 72 6d 20 2a 2f 0a 20 20 45 78 70 72 20  pTerm */.  Expr 
3926b 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20  *pExpr,         
3926c 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 66 6f  /* Expression fo
3926d 72 20 56 41 4c 55 45 20 69 6e 20 74 68 65 20 78  r VALUE in the x
3926e 3d 56 41 4c 55 45 20 63 6f 6e 73 74 72 61 69 6e  =VALUE constrain
3926f 74 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 2a 70  t */.  double *p
39270 6e 52 6f 77 20 20 20 20 20 20 20 20 2f 2a 20 57  nRow        /* W
39271 72 69 74 65 20 74 68 65 20 72 65 76 69 73 65 64  rite the revised
39272 20 72 6f 77 20 65 73 74 69 6d 61 74 65 20 68 65   row estimate he
39273 72 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  re */.){.  sqlit
39274 65 33 5f 76 61 6c 75 65 20 2a 70 52 68 73 20 3d  e3_value *pRhs =
39275 20 30 3b 20 20 2f 2a 20 56 41 4c 55 45 20 6f 6e   0;  /* VALUE on
39276 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65   right-hand side
39277 20 6f 66 20 70 54 65 72 6d 20 2a 2f 0a 20 20 75   of pTerm */.  u
39278 38 20 61 66 66 3b 20 20 20 20 20 20 20 20 20 20  8 aff;          
39279 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
3927a 6d 6e 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20  mn affinity */. 
3927b 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
3927c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75             /* Su
3927d 62 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e  bfunction return
3927e 20 63 6f 64 65 20 2a 2f 0a 20 20 74 52 6f 77 63   code */.  tRowc
3927f 6e 74 20 61 5b 32 5d 3b 20 20 20 20 20 20 20 20  nt a[2];        
39280 20 20 20 20 20 2f 2a 20 53 74 61 74 69 73 74 69       /* Statisti
39281 63 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  cs */..  assert(
39282 20 70 2d 3e 61 53 61 6d 70 6c 65 21 3d 30 20 29   p->aSample!=0 )
39283 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  ;.  assert( p->n
39284 53 61 6d 70 6c 65 3e 30 20 29 3b 0a 20 20 61 66  Sample>0 );.  af
39285 66 20 3d 20 70 2d 3e 70 54 61 62 6c 65 2d 3e 61  f = p->pTable->a
39286 43 6f 6c 5b 70 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  Col[p->aiColumn[
39287 30 5d 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20  0]].affinity;.  
39288 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20  if( pExpr ){.   
39289 20 72 63 20 3d 20 76 61 6c 75 65 46 72 6f 6d 45   rc = valueFromE
3928a 78 70 72 28 70 50 61 72 73 65 2c 20 70 45 78 70  xpr(pParse, pExp
3928b 72 2c 20 61 66 66 2c 20 26 70 52 68 73 29 3b 0a  r, aff, &pRhs);.
3928c 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74      if( rc ) got
3928d 6f 20 77 68 65 72 65 45 71 75 61 6c 53 63 61 6e  o whereEqualScan
3928e 45 73 74 5f 63 61 6e 63 65 6c 3b 0a 20 20 7d 65  Est_cancel;.  }e
3928f 6c 73 65 7b 0a 20 20 20 20 70 52 68 73 20 3d 20  lse{.    pRhs = 
39290 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28  sqlite3ValueNew(
39291 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 7d  pParse->db);.  }
39292 0a 20 20 69 66 28 20 70 52 68 73 3d 3d 30 20 29  .  if( pRhs==0 )
39293 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
39294 4f 54 46 4f 55 4e 44 3b 0a 20 20 72 63 20 3d 20  OTFOUND;.  rc = 
39295 77 68 65 72 65 4b 65 79 53 74 61 74 73 28 70 50  whereKeyStats(pP
39296 61 72 73 65 2c 20 70 2c 20 70 52 68 73 2c 20 30  arse, p, pRhs, 0
39297 2c 20 61 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , a);.  if( rc==
39298 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
39299 20 57 48 45 52 45 54 52 41 43 45 28 28 22 65 71   WHERETRACE(("eq
3929a 75 61 6c 69 74 79 20 73 63 61 6e 20 72 65 67 69  uality scan regi
3929b 6f 6e 73 3a 20 25 64 5c 6e 22 2c 20 28 69 6e 74  ons: %d\n", (int
3929c 29 61 5b 31 5d 29 29 3b 0a 20 20 20 20 2a 70 6e  )a[1]));.    *pn
3929d 52 6f 77 20 3d 20 61 5b 31 5d 3b 0a 20 20 7d 0a  Row = a[1];.  }.
3929e 77 68 65 72 65 45 71 75 61 6c 53 63 61 6e 45 73  whereEqualScanEs
3929f 74 5f 63 61 6e 63 65 6c 3a 0a 20 20 73 71 6c 69  t_cancel:.  sqli
392a0 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 52 68  te3ValueFree(pRh
392a1 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  s);.  return rc;
392a2 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66  .}.#endif /* def
392a3 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
392a4 4c 45 5f 53 54 41 54 33 29 20 2a 2f 0a 0a 23 69  LE_STAT3) */..#i
392a5 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
392a6 4c 45 5f 53 54 41 54 33 0a 2f 2a 0a 2a 2a 20 45  LE_STAT3./*.** E
392a7 73 74 69 6d 61 74 65 20 74 68 65 20 6e 75 6d 62  stimate the numb
392a8 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20  er of rows that 
392a9 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64  will be returned
392aa 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 61 6e 20   based on.** an 
392ab 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 20 77 68  IN constraint wh
392ac 65 72 65 20 74 68 65 20 72 69 67 68 74 2d 68 61  ere the right-ha
392ad 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 49  nd side of the I
392ae 4e 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 69 73  N operator.** is
392af 20 61 20 6c 69 73 74 20 6f 66 20 76 61 6c 75 65   a list of value
392b0 73 2e 20 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 0a  s.  Example:.**.
392b1 2a 2a 20 20 20 20 20 20 20 20 57 48 45 52 45 20  **        WHERE 
392b2 78 20 49 4e 20 28 31 2c 32 2c 33 2c 34 29 0a 2a  x IN (1,2,3,4).*
392b3 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 65  *.** Write the e
392b4 73 74 69 6d 61 74 65 64 20 72 6f 77 20 63 6f 75  stimated row cou
392b5 6e 74 20 69 6e 74 6f 20 2a 70 6e 52 6f 77 20 61  nt into *pnRow a
392b6 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  nd return SQLITE
392b7 5f 4f 4b 2e 20 0a 2a 2a 20 49 66 20 75 6e 61 62  _OK. .** If unab
392b8 6c 65 20 74 6f 20 6d 61 6b 65 20 61 6e 20 65 73  le to make an es
392b9 74 69 6d 61 74 65 2c 20 6c 65 61 76 65 20 2a 70  timate, leave *p
392ba 6e 52 6f 77 20 75 6e 63 68 61 6e 67 65 64 20 61  nRow unchanged a
392bb 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e  nd return.** non
392bc 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  -zero..**.** Thi
392bd 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 66 61  s routine can fa
392be 69 6c 20 69 66 20 69 74 20 69 73 20 75 6e 61 62  il if it is unab
392bf 6c 65 20 74 6f 20 6c 6f 61 64 20 61 20 63 6f 6c  le to load a col
392c0 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 0a  lating sequence.
392c1 2a 2a 20 72 65 71 75 69 72 65 64 20 66 6f 72 20  ** required for 
392c2 73 74 72 69 6e 67 20 63 6f 6d 70 61 72 69 73 6f  string compariso
392c3 6e 2c 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20  n, or if unable 
392c4 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  to allocate memo
392c5 72 79 0a 2a 2a 20 66 6f 72 20 61 20 55 54 46 20  ry.** for a UTF 
392c6 63 6f 6e 76 65 72 73 69 6f 6e 20 72 65 71 75 69  conversion requi
392c7 72 65 64 20 66 6f 72 20 63 6f 6d 70 61 72 69 73  red for comparis
392c8 6f 6e 2e 20 20 54 68 65 20 65 72 72 6f 72 20 69  on.  The error i
392c9 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 74  s stored.** in t
392ca 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63 74  he pParse struct
392cb 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
392cc 6e 74 20 77 68 65 72 65 49 6e 53 63 61 6e 45 73  nt whereInScanEs
392cd 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
392ce 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72  se,       /* Par
392cf 73 69 6e 67 20 26 20 63 6f 64 65 20 67 65 6e 65  sing & code gene
392d0 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  rating context *
392d1 2f 0a 20 20 49 6e 64 65 78 20 2a 70 2c 20 20 20  /.  Index *p,   
392d2 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
392d3 69 6e 64 65 78 20 77 68 6f 73 65 20 6c 65 66 74  index whose left
392d4 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20  -most column is 
392d5 70 54 65 72 6d 20 2a 2f 0a 20 20 45 78 70 72 4c  pTerm */.  ExprL
392d6 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20  ist *pList,     
392d7 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6c 69 73  /* The value lis
392d8 74 20 6f 6e 20 74 68 65 20 52 48 53 20 6f 66 20  t on the RHS of 
392d9 22 78 20 49 4e 20 28 76 31 2c 76 32 2c 76 33 2c  "x IN (v1,v2,v3,
392da 2e 2e 2e 29 22 20 2a 2f 0a 20 20 64 6f 75 62 6c  ...)" */.  doubl
392db 65 20 2a 70 6e 52 6f 77 20 20 20 20 20 20 20 20  e *pnRow        
392dc 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 76  /* Write the rev
392dd 69 73 65 64 20 72 6f 77 20 65 73 74 69 6d 61 74  ised row estimat
392de 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  e here */.){.  i
392df 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
392e0 4b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75  K;         /* Su
392e1 62 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e  bfunction return
392e2 20 63 6f 64 65 20 2a 2f 0a 20 20 64 6f 75 62 6c   code */.  doubl
392e3 65 20 6e 45 73 74 3b 20 20 20 20 20 20 20 20 20  e nEst;         
392e4 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
392e5 20 6f 66 20 72 6f 77 73 20 66 6f 72 20 61 20 73   of rows for a s
392e6 69 6e 67 6c 65 20 74 65 72 6d 20 2a 2f 0a 20 20  ingle term */.  
392e7 64 6f 75 62 6c 65 20 6e 52 6f 77 45 73 74 20 3d  double nRowEst =
392e8 20 28 64 6f 75 62 6c 65 29 30 3b 20 2f 2a 20 4e   (double)0; /* N
392e9 65 77 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74  ew estimate of t
392ea 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
392eb 73 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  s */.  int i;   
392ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
392ed 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
392ee 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  er */..  assert(
392ef 20 70 2d 3e 61 53 61 6d 70 6c 65 21 3d 30 20 29   p->aSample!=0 )
392f0 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d  ;.  for(i=0; rc=
392f1 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c  =SQLITE_OK && i<
392f2 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  pList->nExpr; i+
392f3 2b 29 7b 0a 20 20 20 20 6e 45 73 74 20 3d 20 70  +){.    nEst = p
392f4 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d 3b 0a 20  ->aiRowEst[0];. 
392f5 20 20 20 72 63 20 3d 20 77 68 65 72 65 45 71 75     rc = whereEqu
392f6 61 6c 53 63 61 6e 45 73 74 28 70 50 61 72 73 65  alScanEst(pParse
392f7 2c 20 70 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d  , p, pList->a[i]
392f8 2e 70 45 78 70 72 2c 20 26 6e 45 73 74 29 3b 0a  .pExpr, &nEst);.
392f9 20 20 20 20 6e 52 6f 77 45 73 74 20 2b 3d 20 6e      nRowEst += n
392fa 45 73 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  Est;.  }.  if( r
392fb 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
392fc 20 20 20 20 69 66 28 20 6e 52 6f 77 45 73 74 20      if( nRowEst 
392fd 3e 20 70 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d  > p->aiRowEst[0]
392fe 20 29 20 6e 52 6f 77 45 73 74 20 3d 20 70 2d 3e   ) nRowEst = p->
392ff 61 69 52 6f 77 45 73 74 5b 30 5d 3b 0a 20 20 20  aiRowEst[0];.   
39300 20 2a 70 6e 52 6f 77 20 3d 20 6e 52 6f 77 45 73   *pnRow = nRowEs
39301 74 3b 0a 20 20 20 20 57 48 45 52 45 54 52 41 43  t;.    WHERETRAC
39302 45 28 28 22 49 4e 20 72 6f 77 20 65 73 74 69 6d  E(("IN row estim
39303 61 74 65 3a 20 65 73 74 3d 25 67 5c 6e 22 2c 20  ate: est=%g\n", 
39304 6e 52 6f 77 45 73 74 29 29 3b 0a 20 20 7d 0a 20  nRowEst));.  }. 
39305 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
39306 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28  ndif /* defined(
39307 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
39308 41 54 33 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  AT3) */../*.** C
39309 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 63  heck to see if c
3930a 6f 6c 75 6d 6e 20 69 43 6f 6c 20 6f 66 20 74 68  olumn iCol of th
3930b 65 20 74 61 62 6c 65 20 77 69 74 68 20 63 75 72  e table with cur
3930c 73 6f 72 20 69 54 61 62 20 77 69 6c 6c 20 61 70  sor iTab will ap
3930d 70 65 61 72 0a 2a 2a 20 69 6e 20 73 6f 72 74 65  pear.** in sorte
3930e 64 20 6f 72 64 65 72 20 61 63 63 6f 72 64 69 6e  d order accordin
3930f 67 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74  g to the current
39310 20 71 75 65 72 79 20 70 6c 61 6e 2e 0a 2a 2a 0a   query plan..**.
39311 2a 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 73  ** Return values
39312 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 20 69  :.**.**    0   i
39313 43 6f 6c 20 69 73 20 6e 6f 74 20 6f 72 64 65 72  Col is not order
39314 65 64 0a 2a 2a 20 20 20 20 31 20 20 20 69 43 6f  ed.**    1   iCo
39315 6c 20 68 61 73 20 6f 6e 6c 79 20 61 20 73 69 6e  l has only a sin
39316 67 6c 65 20 76 61 6c 75 65 0a 2a 2a 20 20 20 20  gle value.**    
39317 32 20 20 20 69 43 6f 6c 20 69 73 20 69 6e 20 41  2   iCol is in A
39318 53 43 20 6f 72 64 65 72 0a 2a 2a 20 20 20 20 33  SC order.**    3
39319 20 20 20 69 43 6f 6c 20 69 73 20 69 6e 20 44 45     iCol is in DE
3931a 53 43 20 6f 72 64 65 72 0a 2a 2f 0a 73 74 61 74  SC order.*/.stat
3931b 69 63 20 69 6e 74 20 69 73 4f 72 64 65 72 65 64  ic int isOrdered
3931c 43 6f 6c 75 6d 6e 28 0a 20 20 57 68 65 72 65 42  Column(.  WhereB
3931d 65 73 74 49 64 78 20 2a 70 2c 0a 20 20 69 6e 74  estIdx *p,.  int
3931e 20 69 54 61 62 2c 0a 20 20 69 6e 74 20 69 43 6f   iTab,.  int iCo
3931f 6c 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  l.){.  int i, j;
39320 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70  .  WhereLevel *p
39321 4c 65 76 65 6c 20 3d 20 26 70 2d 3e 61 4c 65 76  Level = &p->aLev
39322 65 6c 5b 70 2d 3e 69 2d 31 5d 3b 0a 20 20 49 6e  el[p->i-1];.  In
39323 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 75 38 20  dex *pIdx;.  u8 
39324 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 66 6f 72  sortOrder;.  for
39325 28 69 3d 70 2d 3e 69 2d 31 3b 20 69 3e 3d 30 3b  (i=p->i-1; i>=0;
39326 20 69 2d 2d 2c 20 70 4c 65 76 65 6c 2d 2d 29 7b   i--, pLevel--){
39327 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
39328 3e 69 54 61 62 43 75 72 21 3d 69 54 61 62 20 29  >iTabCur!=iTab )
39329 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69   continue;.    i
3932a 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  f( (pLevel->plan
3932b 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
3932c 5f 41 4c 4c 5f 55 4e 49 51 55 45 29 21 3d 30 20  _ALL_UNIQUE)!=0 
3932d 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
3932e 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  1;.    }.    ass
3932f 65 72 74 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c  ert( (pLevel->pl
39330 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
39331 52 45 5f 4f 52 44 45 52 45 44 29 21 3d 30 20 29  RE_ORDERED)!=0 )
39332 3b 0a 20 20 20 20 69 66 28 20 28 70 49 64 78 20  ;.    if( (pIdx 
39333 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75  = pLevel->plan.u
39334 2e 70 49 64 78 29 21 3d 30 20 29 7b 0a 20 20 20  .pIdx)!=0 ){.   
39335 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b     if( iCol<0 ){
39336 0a 20 20 20 20 20 20 20 20 73 6f 72 74 4f 72 64  .        sortOrd
39337 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  er = 0;.        
39338 74 65 73 74 63 61 73 65 28 20 28 70 4c 65 76 65  testcase( (pLeve
39339 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
3933a 26 20 57 48 45 52 45 5f 52 45 56 45 52 53 45 29  & WHERE_REVERSE)
3933b 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d 65 6c  !=0 );.      }el
3933c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  se{.        int 
3933d 6e 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  n = pIdx->nColum
3933e 6e 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a  n;.        for(j
3933f 3d 30 3b 20 6a 3c 6e 3b 20 6a 2b 2b 29 7b 0a 20  =0; j<n; j++){. 
39340 20 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f           if( iCo
39341 6c 3d 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d  l==pIdx->aiColum
39342 6e 5b 6a 5d 20 29 20 62 72 65 61 6b 3b 0a 20 20  n[j] ) break;.  
39343 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
39344 69 66 28 20 6a 3e 3d 6e 20 29 20 72 65 74 75 72  if( j>=n ) retur
39345 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 73 6f 72  n 0;.        sor
39346 74 4f 72 64 65 72 20 3d 20 70 49 64 78 2d 3e 61  tOrder = pIdx->a
39347 53 6f 72 74 4f 72 64 65 72 5b 6a 5d 3b 0a 20 20  SortOrder[j];.  
39348 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
39349 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
3934a 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 45  Flags & WHERE_RE
3934b 56 45 52 53 45 29 21 3d 30 20 29 3b 0a 20 20 20  VERSE)!=0 );.   
3934c 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
3934d 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 21 3d        if( iCol!=
3934e 28 2d 31 29 20 29 20 72 65 74 75 72 6e 20 30 3b  (-1) ) return 0;
3934f 0a 20 20 20 20 20 20 73 6f 72 74 4f 72 64 65 72  .      sortOrder
39350 20 3d 20 30 3b 0a 20 20 20 20 20 20 74 65 73 74   = 0;.      test
39351 63 61 73 65 28 20 28 70 4c 65 76 65 6c 2d 3e 70  case( (pLevel->p
39352 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
39353 45 52 45 5f 52 45 56 45 52 53 45 29 21 3d 30 20  ERE_REVERSE)!=0 
39354 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
39355 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
39356 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52  sFlags & WHERE_R
39357 45 56 45 52 53 45 29 21 3d 30 20 29 7b 0a 20 20  EVERSE)!=0 ){.  
39358 20 20 20 20 61 73 73 65 72 74 28 20 73 6f 72 74      assert( sort
39359 4f 72 64 65 72 3d 3d 30 20 7c 7c 20 73 6f 72 74  Order==0 || sort
3935a 4f 72 64 65 72 3d 3d 31 20 29 3b 0a 20 20 20 20  Order==1 );.    
3935b 20 20 74 65 73 74 63 61 73 65 28 20 73 6f 72 74    testcase( sort
3935c 4f 72 64 65 72 3d 3d 31 20 29 3b 0a 20 20 20 20  Order==1 );.    
3935d 20 20 73 6f 72 74 4f 72 64 65 72 20 3d 20 31 20    sortOrder = 1 
3935e 2d 20 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20  - sortOrder;.   
3935f 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 6f   }.    return so
39360 72 74 4f 72 64 65 72 2b 32 3b 0a 20 20 7d 0a 20  rtOrder+2;.  }. 
39361 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
39362 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
39363 20 64 65 63 69 64 65 73 20 69 66 20 70 49 64 78   decides if pIdx
39364 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
39365 73 61 74 69 73 66 79 20 74 68 65 20 4f 52 44 45  satisfy the ORDE
39366 52 20 42 59 0a 2a 2a 20 63 6c 61 75 73 65 2c 20  R BY.** clause, 
39367 65 69 74 68 65 72 20 69 6e 20 77 68 6f 6c 65 20  either in whole 
39368 6f 72 20 69 6e 20 70 61 72 74 2e 20 20 54 68 65  or in part.  The
39369 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
3936a 20 74 68 65 20 0a 2a 2a 20 63 75 6d 75 6c 61 74   the .** cumulat
3936b 69 76 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65  ive number of te
3936c 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52  rms in the ORDER
3936d 20 42 59 20 63 6c 61 75 73 65 20 74 68 61 74 20   BY clause that 
3936e 61 72 65 20 73 61 74 69 73 66 69 65 64 0a 2a 2a  are satisfied.**
3936f 20 62 79 20 74 68 65 20 69 6e 64 65 78 20 70 49   by the index pI
39370 64 78 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 64  dx and other ind
39371 69 63 65 73 20 69 6e 20 6f 75 74 65 72 20 6c 6f  ices in outer lo
39372 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  ops..**.** The t
39373 61 62 6c 65 20 62 65 69 6e 67 20 71 75 65 72 69  able being queri
39374 65 64 20 68 61 73 20 61 20 63 75 72 73 6f 72 20  ed has a cursor 
39375 6e 75 6d 62 65 72 20 6f 66 20 22 62 61 73 65 22  number of "base"
39376 2e 20 20 70 49 64 78 20 69 73 20 74 68 65 0a 2a  .  pIdx is the.*
39377 2a 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 20  * index that is 
39378 70 6f 73 74 75 6c 61 74 65 64 20 66 6f 72 20 75  postulated for u
39379 73 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  se to access the
3937a 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   table..**.** Th
3937b 65 20 2a 70 62 52 65 76 20 76 61 6c 75 65 20 69  e *pbRev value i
3937c 73 20 73 65 74 20 74 6f 20 30 20 6f 72 64 65 72  s set to 0 order
3937d 20 31 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20   1 depending on 
3937e 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a  whether or not.*
3937f 2a 20 70 49 64 78 20 73 68 6f 75 6c 64 20 62 65  * pIdx should be
39380 20 72 75 6e 20 69 6e 20 74 68 65 20 66 6f 72 77   run in the forw
39381 61 72 64 20 6f 72 64 65 72 20 6f 72 20 69 6e 20  ard order or in 
39382 72 65 76 65 72 73 65 20 6f 72 64 65 72 2e 0a 2a  reverse order..*
39383 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 53  /.static int isS
39384 6f 72 74 69 6e 67 49 6e 64 65 78 28 0a 20 20 57  ortingIndex(.  W
39385 68 65 72 65 42 65 73 74 49 64 78 20 2a 70 2c 20  hereBestIdx *p, 
39386 20 20 20 2f 2a 20 42 65 73 74 20 69 6e 64 65 78     /* Best index
39387 20 73 65 61 72 63 68 20 63 6f 6e 74 65 78 74 20   search context 
39388 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  */.  Index *pIdx
39389 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
3938a 69 6e 64 65 78 20 77 65 20 61 72 65 20 74 65 73  index we are tes
3938b 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 61  ting */.  int ba
3938c 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  se,           /*
3938d 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
3938e 6f 72 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  or the table to 
3938f 62 65 20 73 6f 72 74 65 64 20 2a 2f 0a 20 20 69  be sorted */.  i
39390 6e 74 20 2a 70 62 52 65 76 20 20 20 20 20 20 20  nt *pbRev       
39391 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 31 20 66     /* Set to 1 f
39392 6f 72 20 72 65 76 65 72 73 65 2d 6f 72 64 65 72  or reverse-order
39393 20 73 63 61 6e 20 6f 66 20 70 49 64 78 20 2a 2f   scan of pIdx */
39394 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  .){.  int i;    
39395 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39396 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
39397 20 70 49 64 78 20 74 65 72 6d 73 20 75 73 65 64   pIdx terms used
39398 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20   */.  int j;    
39399 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3939a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3939b 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20   ORDER BY terms 
3939c 73 61 74 69 73 66 69 65 64 20 2a 2f 0a 20 20 69  satisfied */.  i
3939d 6e 74 20 73 6f 72 74 4f 72 64 65 72 20 3d 20 32  nt sortOrder = 2
3939e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
3939f 30 3a 20 66 6f 72 77 61 72 64 2e 20 20 31 3a 20  0: forward.  1: 
393a0 62 61 63 6b 77 61 72 64 2e 20 20 32 3a 20 75 6e  backward.  2: un
393a1 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e  known */.  int n
393a2 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20  Term;           
393a3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
393a4 65 72 20 6f 66 20 4f 52 44 45 52 20 42 59 20 74  er of ORDER BY t
393a5 65 72 6d 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  erms */.  struct
393a6 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
393a7 70 4f 42 49 74 65 6d 3b 2f 2a 20 41 20 74 65 72  pOBItem;/* A ter
393a8 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42  m of the ORDER B
393a9 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 54 61  Y clause */.  Ta
393aa 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 64 78  ble *pTab = pIdx
393ab 2d 3e 70 54 61 62 6c 65 3b 20 20 20 2f 2a 20 54  ->pTable;   /* T
393ac 61 62 6c 65 20 74 68 61 74 20 6f 77 6e 73 20 69  able that owns i
393ad 6e 64 65 78 20 70 49 64 78 20 2a 2f 0a 20 20 45  ndex pIdx */.  E
393ae 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
393af 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y;           /* 
393b0 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
393b1 75 73 65 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  use */.  Parse *
393b2 70 50 61 72 73 65 20 3d 20 70 2d 3e 70 50 61 72  pParse = p->pPar
393b3 73 65 3b 20 20 20 20 2f 2a 20 50 61 72 73 65 72  se;    /* Parser
393b4 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 73 71   context */.  sq
393b5 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
393b6 73 65 2d 3e 64 62 3b 20 20 20 20 20 2f 2a 20 44  se->db;     /* D
393b7 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
393b8 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 69  on */.  int nPri
393b9 6f 72 53 61 74 3b 20 20 20 20 20 20 20 20 20 20  orSat;          
393ba 20 20 20 20 20 20 2f 2a 20 4f 52 44 45 52 20 42        /* ORDER B
393bb 59 20 74 65 72 6d 73 20 73 61 74 69 73 66 69 65  Y terms satisfie
393bc 64 20 62 79 20 6f 75 74 65 72 20 6c 6f 6f 70 73  d by outer loops
393bd 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65 6e 52 6f   */.  int seenRo
393be 77 69 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20  wid = 0;        
393bf 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
393c0 6e 20 4f 52 44 45 52 20 42 59 20 72 6f 77 69 64  n ORDER BY rowid
393c1 20 74 65 72 6d 20 69 73 20 73 65 65 6e 20 2a 2f   term is seen */
393c2 0a 20 20 69 6e 74 20 75 6e 69 71 75 65 4e 6f 74  .  int uniqueNot
393c3 4e 75 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Null;           
393c4 20 2f 2a 20 70 49 64 78 20 69 73 20 55 4e 49 51   /* pIdx is UNIQ
393c5 55 45 20 77 69 74 68 20 61 6c 6c 20 74 65 72 6d  UE with all term
393c6 73 20 61 72 65 20 4e 4f 54 20 4e 55 4c 4c 20 2a  s are NOT NULL *
393c7 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 69 3d 3d 30  /..  if( p->i==0
393c8 20 29 7b 0a 20 20 20 20 6e 50 72 69 6f 72 53 61   ){.    nPriorSa
393c9 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  t = 0;.  }else{.
393ca 20 20 20 20 6e 50 72 69 6f 72 53 61 74 20 3d 20      nPriorSat = 
393cb 70 2d 3e 61 4c 65 76 65 6c 5b 70 2d 3e 69 2d 31  p->aLevel[p->i-1
393cc 5d 2e 70 6c 61 6e 2e 6e 4f 42 53 61 74 3b 0a 20  ].plan.nOBSat;. 
393cd 20 20 20 69 66 28 20 28 70 2d 3e 61 4c 65 76 65     if( (p->aLeve
393ce 6c 5b 70 2d 3e 69 2d 31 5d 2e 70 6c 61 6e 2e 77  l[p->i-1].plan.w
393cf 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f  sFlags & WHERE_O
393d0 52 44 45 52 45 44 29 3d 3d 30 20 29 7b 0a 20 20  RDERED)==0 ){.  
393d1 20 20 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70      /* This loop
393d2 20 63 61 6e 6e 6f 74 20 62 65 20 6f 72 64 65 72   cannot be order
393d3 65 64 20 75 6e 6c 65 73 73 20 74 68 65 20 6e 65  ed unless the ne
393d4 78 74 20 6f 75 74 65 72 20 6c 6f 6f 70 20 69 73  xt outer loop is
393d5 0a 20 20 20 20 20 20 2a 2a 20 61 6c 73 6f 20 6f  .      ** also o
393d6 72 64 65 72 65 64 20 2a 2f 0a 20 20 20 20 20 20  rdered */.      
393d7 72 65 74 75 72 6e 20 6e 50 72 69 6f 72 53 61 74  return nPriorSat
393d8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
393d9 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 44 69 73 61  OptimizationDisa
393da 62 6c 65 64 28 64 62 2c 20 53 51 4c 49 54 45 5f  bled(db, SQLITE_
393db 4f 72 64 65 72 42 79 49 64 78 4a 6f 69 6e 29 20  OrderByIdxJoin) 
393dc 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79  ){.      /* Only
393dd 20 6c 6f 6f 6b 20 61 74 20 74 68 65 20 6f 75 74   look at the out
393de 65 72 2d 6d 6f 73 74 20 6c 6f 6f 70 20 69 66 20  er-most loop if 
393df 74 68 65 20 4f 72 64 65 72 42 79 49 64 78 4a 6f  the OrderByIdxJo
393e0 69 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 70 74 69  in.      ** opti
393e1 6d 69 7a 61 74 69 6f 6e 20 69 73 20 64 69 73 61  mization is disa
393e2 62 6c 65 64 20 2a 2f 0a 20 20 20 20 20 20 72 65  bled */.      re
393e3 74 75 72 6e 20 6e 50 72 69 6f 72 53 61 74 3b 0a  turn nPriorSat;.
393e4 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4f 72 64      }.  }.  pOrd
393e5 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72  erBy = p->pOrder
393e6 42 79 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  By;.  assert( pO
393e7 72 64 65 72 42 79 21 3d 30 20 29 3b 0a 20 20 69  rderBy!=0 );.  i
393e8 66 28 20 70 49 64 78 2d 3e 62 55 6e 6f 72 64 65  f( pIdx->bUnorde
393e9 72 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 48 61  red ){.    /* Ha
393ea 73 68 20 69 6e 64 69 63 65 73 20 28 69 6e 64 69  sh indices (indi
393eb 63 61 74 65 64 20 62 79 20 74 68 65 20 22 75 6e  cated by the "un
393ec 6f 72 64 65 72 65 64 22 20 74 61 67 20 6f 6e 20  ordered" tag on 
393ed 73 71 6c 69 74 65 5f 73 74 61 74 31 29 20 63 61  sqlite_stat1) ca
393ee 6e 6e 6f 74 0a 20 20 20 20 2a 2a 20 62 65 20 75  nnot.    ** be u
393ef 73 65 64 20 66 6f 72 20 73 6f 72 74 69 6e 67 20  sed for sorting 
393f0 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 50  */.    return nP
393f1 72 69 6f 72 53 61 74 3b 0a 20 20 7d 0a 20 20 6e  riorSat;.  }.  n
393f2 54 65 72 6d 20 3d 20 70 4f 72 64 65 72 42 79 2d  Term = pOrderBy-
393f3 3e 6e 45 78 70 72 3b 0a 20 20 75 6e 69 71 75 65  >nExpr;.  unique
393f4 4e 6f 74 4e 75 6c 6c 20 3d 20 70 49 64 78 2d 3e  NotNull = pIdx->
393f5 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
393f6 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65 72  ;.  assert( nTer
393f7 6d 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 72 67  m>0 );..  /* Arg
393f8 75 6d 65 6e 74 20 70 49 64 78 20 6d 75 73 74 20  ument pIdx must 
393f9 65 69 74 68 65 72 20 70 6f 69 6e 74 20 74 6f 20  either point to 
393fa 61 20 27 72 65 61 6c 27 20 6e 61 6d 65 64 20 69  a 'real' named i
393fb 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 2c 20  ndex structure, 
393fc 0a 20 20 2a 2a 20 6f 72 20 61 6e 20 69 6e 64 65  .  ** or an inde
393fd 78 20 73 74 72 75 63 74 75 72 65 20 61 6c 6c 6f  x structure allo
393fe 63 61 74 65 64 20 6f 6e 20 74 68 65 20 73 74 61  cated on the sta
393ff 63 6b 20 62 79 20 62 65 73 74 42 74 72 65 65 49  ck by bestBtreeI
39400 6e 64 65 78 28 29 20 74 6f 0a 20 20 2a 2a 20 72  ndex() to.  ** r
39401 65 70 72 65 73 65 6e 74 20 74 68 65 20 72 6f 77  epresent the row
39402 69 64 20 69 6e 64 65 78 20 74 68 61 74 20 69 73  id index that is
39403 20 70 61 72 74 20 6f 66 20 65 76 65 72 79 20 74   part of every t
39404 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65  able.  */.  asse
39405 72 74 28 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 20  rt( pIdx->zName 
39406 7c 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  || (pIdx->nColum
39407 6e 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e 61 69  n==1 && pIdx->ai
39408 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 2d 31 29 20 29  Column[0]==-1) )
39409 3b 0a 0a 20 20 2f 2a 20 4d 61 74 63 68 20 74 65  ;..  /* Match te
3940a 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52  rms of the ORDER
3940b 20 42 59 20 63 6c 61 75 73 65 20 61 67 61 69 6e   BY clause again
3940c 73 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20  st columns of.  
3940d 2a 2a 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20  ** the index..  
3940e 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61  **.  ** Note tha
3940f 74 20 69 6e 64 69 63 65 73 20 68 61 76 65 20 70  t indices have p
39410 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 72 65 67  Idx->nColumn reg
39411 75 6c 61 72 20 63 6f 6c 75 6d 6e 73 20 70 6c 75  ular columns plu
39412 73 0a 20 20 2a 2a 20 6f 6e 65 20 61 64 64 69 74  s.  ** one addit
39413 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 20 63 6f 6e  ional column con
39414 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 69  taining the rowi
39415 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 63 6f  d.  The rowid co
39416 6c 75 6d 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65  lumn.  ** of the
39417 20 69 6e 64 65 78 20 69 73 20 61 6c 73 6f 20 61   index is also a
39418 6c 6c 6f 77 65 64 20 74 6f 20 6d 61 74 63 68 20  llowed to match 
39419 61 67 61 69 6e 73 74 20 74 68 65 20 4f 52 44 45  against the ORDE
3941a 52 20 42 59 0a 20 20 2a 2a 20 63 6c 61 75 73 65  R BY.  ** clause
3941b 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d 20 6e 50 72  ..  */.  j = nPr
3941c 69 6f 72 53 61 74 3b 0a 20 20 66 6f 72 28 69 3d  iorSat;.  for(i=
3941d 30 2c 70 4f 42 49 74 65 6d 3d 26 70 4f 72 64 65  0,pOBItem=&pOrde
3941e 72 42 79 2d 3e 61 5b 6a 5d 3b 20 6a 3c 6e 54 65  rBy->a[j]; j<nTe
3941f 72 6d 20 26 26 20 69 3c 3d 70 49 64 78 2d 3e 6e  rm && i<=pIdx->n
39420 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
39421 20 20 45 78 70 72 20 2a 70 4f 42 45 78 70 72 3b    Expr *pOBExpr;
39422 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
39423 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74   expression of t
39424 68 65 20 4f 52 44 45 52 20 42 59 20 70 4f 42 49  he ORDER BY pOBI
39425 74 65 6d 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53  tem */.    CollS
39426 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20  eq *pColl;      
39427 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74     /* The collat
39428 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20  ing sequence of 
39429 70 4f 42 45 78 70 72 20 2a 2f 0a 20 20 20 20 69  pOBExpr */.    i
3942a 6e 74 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72  nt termSortOrder
3942b 3b 20 20 20 20 20 20 2f 2a 20 53 6f 72 74 20 6f  ;      /* Sort o
3942c 72 64 65 72 20 66 6f 72 20 74 68 69 73 20 74 65  rder for this te
3942d 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  rm */.    int iC
3942e 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20  olumn;          
3942f 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 20 63 6f    /* The i-th co
39430 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65  lumn of the inde
39431 78 2e 20 20 2d 31 20 66 6f 72 20 72 6f 77 69 64  x.  -1 for rowid
39432 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f 72   */.    int iSor
39433 74 4f 72 64 65 72 3b 20 20 20 20 20 20 20 20 20  tOrder;         
39434 2f 2a 20 31 20 66 6f 72 20 44 45 53 43 2c 20 30  /* 1 for DESC, 0
39435 20 66 6f 72 20 41 53 43 20 6f 6e 20 74 68 65 20   for ASC on the 
39436 69 2d 74 68 20 69 6e 64 65 78 20 74 65 72 6d 20  i-th index term 
39437 2a 2f 0a 20 20 20 20 69 6e 74 20 69 73 45 71 3b  */.    int isEq;
39438 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39439 2a 20 53 75 62 6a 65 63 74 20 74 6f 20 61 6e 20  * Subject to an 
3943a 3d 3d 20 6f 72 20 49 53 20 4e 55 4c 4c 20 63 6f  == or IS NULL co
3943b 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20  nstraint */.    
3943c 69 6e 74 20 69 73 4d 61 74 63 68 3b 20 20 20 20  int isMatch;    
3943d 20 20 20 20 20 20 20 20 2f 2a 20 4f 52 44 45 52          /* ORDER
3943e 20 42 59 20 74 65 72 6d 20 6d 61 74 63 68 65 73   BY term matches
3943f 20 74 68 65 20 69 6e 64 65 78 20 74 65 72 6d 20   the index term 
39440 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  */.    const cha
39441 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 2f  r *zColl;      /
39442 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 74  * Name of collat
39443 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ing sequence for
39444 20 69 2d 74 68 20 69 6e 64 65 78 20 74 65 72 6d   i-th index term
39445 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 54 65 72   */.    WhereTer
39446 6d 20 2a 70 43 6f 6e 73 74 72 61 69 6e 74 3b 20  m *pConstraint; 
39447 2f 2a 20 41 20 63 6f 6e 73 74 72 61 69 6e 74 20  /* A constraint 
39448 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  in the WHERE cla
39449 75 73 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49  use */..    /* I
3944a 66 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20  f the next term 
3944b 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
3944c 63 6c 61 75 73 65 20 72 65 66 65 72 73 20 74 6f  clause refers to
3944d 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 72 20   anything other 
3944e 74 68 61 6e 0a 20 20 20 20 2a 2a 20 61 20 63 6f  than.    ** a co
3944f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 22 62 61 73  lumn in the "bas
39450 65 22 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 74  e" table, then t
39451 68 69 73 20 69 6e 64 65 78 20 77 69 6c 6c 20 6e  his index will n
39452 6f 74 20 62 65 20 6f 66 20 61 6e 79 0a 20 20 20  ot be of any.   
39453 20 2a 2a 20 66 75 72 74 68 65 72 20 75 73 65 20   ** further use 
39454 69 6e 20 68 61 6e 64 6c 69 6e 67 20 74 68 65 20  in handling the 
39455 4f 52 44 45 52 20 42 59 2e 20 2a 2f 0a 20 20 20  ORDER BY. */.   
39456 20 70 4f 42 45 78 70 72 20 3d 20 70 4f 42 49 74   pOBExpr = pOBIt
39457 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69  em->pExpr;.    i
39458 66 28 20 70 4f 42 45 78 70 72 2d 3e 6f 70 21 3d  f( pOBExpr->op!=
39459 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 4f 42  TK_COLUMN || pOB
3945a 45 78 70 72 2d 3e 69 54 61 62 6c 65 21 3d 62 61  Expr->iTable!=ba
3945b 73 65 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61  se ){.      brea
3945c 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
3945d 20 46 69 6e 64 20 63 6f 6c 75 6d 6e 20 6e 75 6d   Find column num
3945e 62 65 72 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e  ber and collatin
3945f 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74  g sequence for t
39460 68 65 20 6e 65 78 74 20 65 6e 74 72 79 0a 20 20  he next entry.  
39461 20 20 2a 2a 20 69 6e 20 74 68 65 20 69 6e 64 65    ** in the inde
39462 78 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 49 64  x */.    if( pId
39463 78 2d 3e 7a 4e 61 6d 65 20 26 26 20 69 3c 70 49  x->zName && i<pI
39464 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20  dx->nColumn ){. 
39465 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70       iColumn = p
39466 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
39467 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c  ;.      if( iCol
39468 75 6d 6e 3d 3d 70 49 64 78 2d 3e 70 54 61 62 6c  umn==pIdx->pTabl
39469 65 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20  e->iPKey ){.    
3946a 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31      iColumn = -1
3946b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3946c 69 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 49 64  iSortOrder = pId
3946d 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  x->aSortOrder[i]
3946e 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20  ;.      zColl = 
3946f 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b  pIdx->azColl[i];
39470 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a  .      assert( z
39471 43 6f 6c 6c 21 3d 30 20 29 3b 0a 20 20 20 20 7d  Coll!=0 );.    }
39472 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 43 6f 6c  else{.      iCol
39473 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  umn = -1;.      
39474 69 53 6f 72 74 4f 72 64 65 72 20 3d 20 30 3b 0a  iSortOrder = 0;.
39475 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 30 3b        zColl = 0;
39476 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
39477 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
39478 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  he column number
39479 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73   and collating s
3947a 65 71 75 65 6e 63 65 20 6f 66 20 74 68 65 0a 20  equence of the. 
3947b 20 20 20 2a 2a 20 69 6e 64 65 78 20 6d 61 74 63     ** index matc
3947c 68 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d  h the column num
3947d 62 65 72 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e  ber and collatin
3947e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68  g sequence of th
3947f 65 20 4f 52 44 45 52 20 42 59 0a 20 20 20 20 2a  e ORDER BY.    *
39480 2a 20 63 6c 61 75 73 65 20 65 6e 74 72 79 2e 20  * clause entry. 
39481 20 53 65 74 20 69 73 4d 61 74 63 68 20 74 6f 20   Set isMatch to 
39482 31 20 69 66 20 74 68 65 79 20 62 6f 74 68 20 6d  1 if they both m
39483 61 74 63 68 2e 20 2a 2f 0a 20 20 20 20 69 66 28  atch. */.    if(
39484 20 70 4f 42 45 78 70 72 2d 3e 69 43 6f 6c 75 6d   pOBExpr->iColum
39485 6e 3d 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  n==iColumn ){.  
39486 20 20 20 20 69 66 28 20 7a 43 6f 6c 6c 20 29 7b      if( zColl ){
39487 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d  .        pColl =
39488 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c   sqlite3ExprColl
39489 53 65 71 28 70 50 61 72 73 65 2c 20 70 4f 42 45  Seq(pParse, pOBE
3948a 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 69 66  xpr);.        if
3948b 28 20 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c  ( !pColl ) pColl
3948c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c   = db->pDfltColl
3948d 3b 0a 20 20 20 20 20 20 20 20 69 73 4d 61 74 63  ;.        isMatc
3948e 68 20 3d 20 73 71 6c 69 74 65 33 53 74 72 49 43  h = sqlite3StrIC
3948f 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c  mp(pColl->zName,
39490 20 7a 43 6f 6c 6c 29 3d 3d 30 3b 0a 20 20 20 20   zColl)==0;.    
39491 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
39492 20 69 73 4d 61 74 63 68 20 3d 20 31 3b 0a 20 20   isMatch = 1;.  
39493 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
39494 0a 20 20 20 20 20 20 69 73 4d 61 74 63 68 20 3d  .      isMatch =
39495 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f   0;.    }..    /
39496 2a 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 20  * termSortOrder 
39497 69 73 20 30 20 6f 72 20 31 20 66 6f 72 20 77 68  is 0 or 1 for wh
39498 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
39499 20 61 63 63 65 73 73 20 6c 6f 6f 70 20 73 68 6f   access loop sho
3949a 75 6c 64 0a 20 20 20 20 2a 2a 20 72 75 6e 20 66  uld.    ** run f
3949b 6f 72 77 61 72 64 20 6f 72 20 62 61 63 6b 77 61  orward or backwa
3949c 72 64 73 20 28 72 65 73 70 65 63 74 69 76 65 6c  rds (respectivel
3949d 79 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73  y) in order to s
3949e 61 74 69 73 66 79 20 74 68 69 73 20 0a 20 20 20  atisfy this .   
3949f 20 2a 2a 20 74 65 72 6d 20 6f 66 20 74 68 65 20   ** term of the 
394a0 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e  ORDER BY clause.
394a1 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
394a2 70 4f 42 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64  pOBItem->sortOrd
394a3 65 72 3d 3d 30 20 7c 7c 20 70 4f 42 49 74 65 6d  er==0 || pOBItem
394a4 2d 3e 73 6f 72 74 4f 72 64 65 72 3d 3d 31 20 29  ->sortOrder==1 )
394a5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 53  ;.    assert( iS
394a6 6f 72 74 4f 72 64 65 72 3d 3d 30 20 7c 7c 20 69  ortOrder==0 || i
394a7 53 6f 72 74 4f 72 64 65 72 3d 3d 31 20 29 3b 0a  SortOrder==1 );.
394a8 20 20 20 20 74 65 72 6d 53 6f 72 74 4f 72 64 65      termSortOrde
394a9 72 20 3d 20 69 53 6f 72 74 4f 72 64 65 72 20 5e  r = iSortOrder ^
394aa 20 70 4f 42 49 74 65 6d 2d 3e 73 6f 72 74 4f 72   pOBItem->sortOr
394ab 64 65 72 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  der;..    /* If 
394ac 58 20 69 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20  X is the column 
394ad 69 6e 20 74 68 65 20 69 6e 64 65 78 20 61 6e 64  in the index and
394ae 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
394af 2c 20 63 68 65 63 6b 20 74 6f 20 73 65 65 0a 20  , check to see. 
394b0 20 20 20 2a 2a 20 69 66 20 74 68 65 72 65 20 61     ** if there a
394b1 72 65 20 61 6e 79 20 58 3d 20 6f 72 20 58 20 49  re any X= or X I
394b2 53 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e  S NULL constrain
394b3 74 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  ts in the WHERE 
394b4 63 6c 61 75 73 65 2e 20 2a 2f 0a 20 20 20 20 70  clause. */.    p
394b5 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 66 69 6e  Constraint = fin
394b6 64 54 65 72 6d 28 70 2d 3e 70 57 43 2c 20 62 61  dTerm(p->pWC, ba
394b7 73 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 70 2d 3e  se, iColumn, p->
394b8 6e 6f 74 52 65 61 64 79 2c 0a 20 20 20 20 20 20  notReady,.      
394b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
394ba 20 20 20 20 20 57 4f 5f 45 51 7c 57 4f 5f 49 53       WO_EQ|WO_IS
394bb 4e 55 4c 4c 7c 57 4f 5f 49 4e 2c 20 70 49 64 78  NULL|WO_IN, pIdx
394bc 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6e 73  );.    if( pCons
394bd 74 72 61 69 6e 74 3d 3d 30 20 29 7b 0a 20 20 20  traint==0 ){.   
394be 20 20 20 69 73 45 71 20 3d 20 30 3b 0a 20 20 20     isEq = 0;.   
394bf 20 7d 65 6c 73 65 20 69 66 28 20 70 43 6f 6e 73   }else if( pCons
394c0 74 72 61 69 6e 74 2d 3e 65 4f 70 65 72 61 74 6f  traint->eOperato
394c1 72 3d 3d 57 4f 5f 49 4e 20 29 7b 0a 20 20 20 20  r==WO_IN ){.    
394c2 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 74 73    /* Constraints
394c3 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20 22 58   of the form: "X
394c4 20 49 4e 20 2e 2e 2e 22 20 63 61 6e 6e 6f 74 20   IN ..." cannot 
394c5 62 65 20 75 73 65 64 20 77 69 74 68 20 61 6e 20  be used with an 
394c6 4f 52 44 45 52 20 42 59 0a 20 20 20 20 20 20 2a  ORDER BY.      *
394c7 2a 20 62 65 63 61 75 73 65 20 77 65 20 64 6f 20  * because we do 
394c8 6e 6f 74 20 6b 6e 6f 77 20 69 6e 20 77 68 61 74  not know in what
394c9 20 6f 72 64 65 72 20 74 68 65 20 76 61 6c 75 65   order the value
394ca 73 20 6f 6e 20 74 68 65 20 52 48 53 20 6f 66 20  s on the RHS of 
394cb 74 68 65 20 49 4e 0a 20 20 20 20 20 20 2a 2a 20  the IN.      ** 
394cc 6f 70 65 72 61 74 6f 72 20 77 69 6c 6c 20 6f 63  operator will oc
394cd 63 75 72 2e 20 2a 2f 0a 20 20 20 20 20 20 62 72  cur. */.      br
394ce 65 61 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  eak;.    }else i
394cf 66 28 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e  f( pConstraint->
394d0 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 53  eOperator==WO_IS
394d1 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 75 6e  NULL ){.      un
394d2 69 71 75 65 4e 6f 74 4e 75 6c 6c 20 3d 20 30 3b  iqueNotNull = 0;
394d3 0a 20 20 20 20 20 20 69 73 45 71 20 3d 20 31 3b  .      isEq = 1;
394d4 20 20 2f 2a 20 22 58 20 49 53 20 4e 55 4c 4c 22    /* "X IS NULL"
394d5 20 6d 65 61 6e 73 20 58 20 68 61 73 20 6f 6e 6c   means X has onl
394d6 79 20 61 20 73 69 6e 67 6c 65 20 76 61 6c 75 65  y a single value
394d7 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   */.    }else if
394d8 28 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 70  ( pConstraint->p
394d9 72 65 72 65 71 52 69 67 68 74 3d 3d 30 20 29 7b  rereqRight==0 ){
394da 0a 20 20 20 20 20 20 69 73 45 71 20 3d 20 31 3b  .      isEq = 1;
394db 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20    /* Constraint 
394dc 22 58 3d 63 6f 6e 73 74 61 6e 74 22 20 6d 65 61  "X=constant" mea
394dd 6e 73 20 58 20 68 61 73 20 6f 6e 6c 79 20 61 20  ns X has only a 
394de 73 69 6e 67 6c 65 20 76 61 6c 75 65 20 2a 2f 0a  single value */.
394df 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
394e0 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20   Expr *pRight = 
394e1 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 70 45 78  pConstraint->pEx
394e2 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  pr->pRight;.    
394e3 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 6f 70    if( pRight->op
394e4 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20  ==TK_COLUMN ){. 
394e5 20 20 20 20 20 20 20 57 48 45 52 45 54 52 41 43         WHERETRAC
394e6 45 28 28 22 20 20 20 20 20 20 20 2e 2e 20 69 73  E(("       .. is
394e7 4f 72 64 65 72 65 64 43 6f 6c 75 6d 6e 28 74 61  OrderedColumn(ta
394e8 62 3d 25 64 2c 63 6f 6c 3d 25 64 29 22 2c 0a 20  b=%d,col=%d)",. 
394e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
394ea 20 20 20 70 52 69 67 68 74 2d 3e 69 54 61 62 6c     pRight->iTabl
394eb 65 2c 20 70 52 69 67 68 74 2d 3e 69 43 6f 6c 75  e, pRight->iColu
394ec 6d 6e 29 29 3b 0a 20 20 20 20 20 20 20 20 69 73  mn));.        is
394ed 45 71 20 3d 20 69 73 4f 72 64 65 72 65 64 43 6f  Eq = isOrderedCo
394ee 6c 75 6d 6e 28 70 2c 20 70 52 69 67 68 74 2d 3e  lumn(p, pRight->
394ef 69 54 61 62 6c 65 2c 20 70 52 69 67 68 74 2d 3e  iTable, pRight->
394f0 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20  iColumn);.      
394f1 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 20    WHERETRACE((" 
394f2 2d 3e 20 69 73 45 71 3d 25 64 5c 6e 22 2c 20 69  -> isEq=%d\n", i
394f3 73 45 71 29 29 3b 0a 0a 20 20 20 20 20 20 20 20  sEq));..        
394f4 2f 2a 20 49 66 20 74 68 65 20 63 6f 6e 73 74 72  /* If the constr
394f5 61 69 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66  aint is of the f
394f6 6f 72 6d 20 58 3d 59 20 77 68 65 72 65 20 59 20  orm X=Y where Y 
394f7 69 73 20 61 6e 20 6f 72 64 65 72 65 64 20 76 61  is an ordered va
394f8 6c 75 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  lue.        ** i
394f9 6e 20 61 6e 20 6f 75 74 65 72 20 6c 6f 6f 70 2c  n an outer loop,
394fa 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20   then make sure 
394fb 74 68 65 20 73 6f 72 74 20 6f 72 64 65 72 20 6f  the sort order o
394fc 66 20 59 20 6d 61 74 63 68 65 73 20 74 68 65 0a  f Y matches the.
394fd 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 72 74 20          ** sort 
394fe 6f 72 64 65 72 20 72 65 71 75 69 72 65 64 20 66  order required f
394ff 6f 72 20 58 2e 20 2a 2f 0a 20 20 20 20 20 20 20  or X. */.       
39500 20 69 66 28 20 69 73 4d 61 74 63 68 20 26 26 20   if( isMatch && 
39501 69 73 45 71 3e 3d 32 20 26 26 20 69 73 45 71 21  isEq>=2 && isEq!
39502 3d 70 4f 42 49 74 65 6d 2d 3e 73 6f 72 74 4f 72  =pOBItem->sortOr
39503 64 65 72 2b 32 20 29 7b 0a 20 20 20 20 20 20 20  der+2 ){.       
39504 20 20 20 74 65 73 74 63 61 73 65 28 20 69 73 45     testcase( isE
39505 71 3d 3d 32 20 29 3b 0a 20 20 20 20 20 20 20 20  q==2 );.        
39506 20 20 74 65 73 74 63 61 73 65 28 20 69 73 45 71    testcase( isEq
39507 3d 3d 33 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ==3 );.         
39508 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
39509 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
3950a 20 20 20 20 20 20 20 69 73 45 71 20 3d 20 30 3b         isEq = 0;
3950b 20 20 2f 2a 20 22 58 3d 65 78 70 72 22 20 70 6c    /* "X=expr" pl
3950c 61 63 65 73 20 6e 6f 20 6f 72 64 65 72 69 6e 67  aces no ordering
3950d 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20   constraints on 
3950e 58 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  X */.      }.   
3950f 20 7d 0a 20 20 20 20 69 66 28 20 21 69 73 4d 61   }.    if( !isMa
39510 74 63 68 20 29 7b 0a 20 20 20 20 20 20 69 66 28  tch ){.      if(
39511 20 69 73 45 71 3d 3d 30 20 29 7b 0a 20 20 20 20   isEq==0 ){.    
39512 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
39513 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
39514 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
39515 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  }.    }else if( 
39516 69 73 45 71 21 3d 31 20 29 7b 0a 20 20 20 20 20  isEq!=1 ){.     
39517 20 69 66 28 20 73 6f 72 74 4f 72 64 65 72 3d 3d   if( sortOrder==
39518 32 20 29 7b 0a 20 20 20 20 20 20 20 20 73 6f 72  2 ){.        sor
39519 74 4f 72 64 65 72 20 3d 20 74 65 72 6d 53 6f 72  tOrder = termSor
3951a 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 7d 65  tOrder;.      }e
3951b 6c 73 65 20 69 66 28 20 74 65 72 6d 53 6f 72 74  lse if( termSort
3951c 4f 72 64 65 72 21 3d 73 6f 72 74 4f 72 64 65 72  Order!=sortOrder
3951d 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
3951e 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
3951f 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 70 4f  .    j++;.    pO
39520 42 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 69 66 28  BItem++;.    if(
39521 20 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20   iColumn<0 ){.  
39522 20 20 20 20 73 65 65 6e 52 6f 77 69 64 20 3d 20      seenRowid = 
39523 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  1;.      break;.
39524 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54      }else if( pT
39525 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 75 6d 6e  ab->aCol[iColumn
39526 5d 2e 6e 6f 74 4e 75 6c 6c 3d 3d 30 20 26 26 20  ].notNull==0 && 
39527 69 73 45 71 21 3d 31 20 29 7b 0a 20 20 20 20 20  isEq!=1 ){.     
39528 20 74 65 73 74 63 61 73 65 28 20 69 73 45 71 3d   testcase( isEq=
39529 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  =0 );.      test
3952a 63 61 73 65 28 20 69 73 45 71 3d 3d 32 20 29 3b  case( isEq==2 );
3952b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3952c 20 69 73 45 71 3d 3d 33 20 29 3b 0a 20 20 20 20   isEq==3 );.    
3952d 20 20 75 6e 69 71 75 65 4e 6f 74 4e 75 6c 6c 20    uniqueNotNull 
3952e 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  = 0;.    }.  }..
3952f 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20    /* If we have 
39530 6e 6f 74 20 66 6f 75 6e 64 20 61 74 20 6c 65 61  not found at lea
39531 73 74 20 6f 6e 65 20 4f 52 44 45 52 20 42 59 20  st one ORDER BY 
39532 74 65 72 6d 20 74 68 61 74 20 6d 61 74 63 68 65  term that matche
39533 73 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78  s the.  ** index
39534 2c 20 74 68 65 6e 20 73 68 6f 77 20 6e 6f 20 70  , then show no p
39535 72 6f 67 72 65 73 73 2e 20 2a 2f 0a 20 20 69 66  rogress. */.  if
39536 28 20 70 4f 42 49 74 65 6d 3d 3d 26 70 4f 72 64  ( pOBItem==&pOrd
39537 65 72 42 79 2d 3e 61 5b 6e 50 72 69 6f 72 53 61  erBy->a[nPriorSa
39538 74 5d 20 29 20 72 65 74 75 72 6e 20 6e 50 72 69  t] ) return nPri
39539 6f 72 53 61 74 3b 0a 0a 20 20 2f 2a 20 52 65 74  orSat;..  /* Ret
3953a 75 72 6e 20 74 68 65 20 6e 65 63 65 73 73 61 72  urn the necessar
3953b 79 20 73 63 61 6e 20 6f 72 64 65 72 20 62 61 63  y scan order bac
3953c 6b 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20  k to the caller 
3953d 2a 2f 0a 20 20 2a 70 62 52 65 76 20 3d 20 73 6f  */.  *pbRev = so
3953e 72 74 4f 72 64 65 72 20 26 20 31 3b 0a 0a 20 20  rtOrder & 1;..  
3953f 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61 73 20  /* If there was 
39540 61 6e 20 22 4f 52 44 45 52 20 42 59 20 72 6f 77  an "ORDER BY row
39541 69 64 22 20 74 65 72 6d 20 74 68 61 74 20 6d 61  id" term that ma
39542 74 63 68 65 64 2c 20 6f 72 20 69 74 20 69 73 20  tched, or it is 
39543 6f 6e 6c 79 0a 20 20 2a 2a 20 70 6f 73 73 69 62  only.  ** possib
39544 6c 65 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20  le for a single 
39545 72 6f 77 20 66 72 6f 6d 20 74 68 69 73 20 74 61  row from this ta
39546 62 6c 65 20 74 6f 20 6d 61 74 63 68 2c 20 74 68  ble to match, th
39547 65 6e 20 73 6b 69 70 20 6f 76 65 72 0a 20 20 2a  en skip over.  *
39548 2a 20 61 6e 79 20 61 64 64 69 74 69 6f 6e 61 6c  * any additional
39549 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20   ORDER BY terms 
3954a 64 65 61 6c 69 6e 67 20 77 69 74 68 20 74 68 69  dealing with thi
3954b 73 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  s table..  */.  
3954c 69 66 28 20 73 65 65 6e 52 6f 77 69 64 20 7c 7c  if( seenRowid ||
3954d 20 28 75 6e 69 71 75 65 4e 6f 74 4e 75 6c 6c 20   (uniqueNotNull 
3954e 26 26 20 69 3e 3d 70 49 64 78 2d 3e 6e 43 6f 6c  && i>=pIdx->nCol
3954f 75 6d 6e 29 20 29 7b 0a 20 20 20 20 2f 2a 20 41  umn) ){.    /* A
39550 64 76 61 6e 63 65 20 6a 20 6f 76 65 72 20 61 64  dvance j over ad
39551 64 69 74 69 6f 6e 61 6c 20 4f 52 44 45 52 20 42  ditional ORDER B
39552 59 20 74 65 72 6d 73 20 61 73 73 6f 63 69 61 74  Y terms associat
39553 65 64 20 77 69 74 68 20 62 61 73 65 20 2a 2f 0a  ed with base */.
39554 20 20 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74      WhereMaskSet
39555 20 2a 70 4d 53 20 3d 20 70 2d 3e 70 57 43 2d 3e   *pMS = p->pWC->
39556 70 4d 61 73 6b 53 65 74 3b 0a 20 20 20 20 42 69  pMaskSet;.    Bi
39557 74 6d 61 73 6b 20 6d 20 3d 20 7e 67 65 74 4d 61  tmask m = ~getMa
39558 73 6b 28 70 4d 53 2c 20 62 61 73 65 29 3b 0a 20  sk(pMS, base);. 
39559 20 20 20 77 68 69 6c 65 28 20 6a 3c 6e 54 65 72     while( j<nTer
3955a 6d 20 26 26 20 28 65 78 70 72 54 61 62 6c 65 55  m && (exprTableU
3955b 73 61 67 65 28 70 4d 53 2c 20 70 4f 72 64 65 72  sage(pMS, pOrder
3955c 42 79 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 29 26  By->a[j].pExpr)&
3955d 6d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6a  m)==0 ){.      j
3955e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ++;.    }.  }.  
3955f 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 0a  return j;.}../*.
39560 2a 2a 20 46 69 6e 64 20 74 68 65 20 62 65 73 74  ** Find the best
39561 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72 20   query plan for 
39562 61 63 63 65 73 73 69 6e 67 20 61 20 70 61 72 74  accessing a part
39563 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 20 57  icular table.  W
39564 72 69 74 65 20 74 68 65 0a 2a 2a 20 62 65 73 74  rite the.** best
39565 20 71 75 65 72 79 20 70 6c 61 6e 20 61 6e 64 20   query plan and 
39566 69 74 73 20 63 6f 73 74 20 69 6e 74 6f 20 74 68  its cost into th
39567 65 20 70 2d 3e 63 6f 73 74 2e 0a 2a 2a 0a 2a 2a  e p->cost..**.**
39568 20 54 68 65 20 6c 6f 77 65 73 74 20 63 6f 73 74   The lowest cost
39569 20 70 6c 61 6e 20 77 69 6e 73 2e 20 20 54 68 65   plan wins.  The
3956a 20 63 6f 73 74 20 69 73 20 61 6e 20 65 73 74 69   cost is an esti
3956b 6d 61 74 65 20 6f 66 20 74 68 65 20 61 6d 6f 75  mate of the amou
3956c 6e 74 20 6f 66 0a 2a 2a 20 43 50 55 20 61 6e 64  nt of.** CPU and
3956d 20 64 69 73 6b 20 49 2f 4f 20 6e 65 65 64 65 64   disk I/O needed
3956e 20 74 6f 20 70 72 6f 63 65 73 73 20 74 68 65 20   to process the 
3956f 72 65 71 75 65 73 74 65 64 20 72 65 73 75 6c 74  requested result
39570 2e 0a 2a 2a 20 46 61 63 74 6f 72 73 20 74 68 61  ..** Factors tha
39571 74 20 69 6e 66 6c 75 65 6e 63 65 20 63 6f 73 74  t influence cost
39572 20 69 6e 63 6c 75 64 65 3a 0a 2a 2a 0a 2a 2a 20   include:.**.** 
39573 20 20 20 2a 20 20 54 68 65 20 65 73 74 69 6d 61     *  The estima
39574 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  ted number of ro
39575 77 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20  ws that will be 
39576 72 65 74 72 69 65 76 65 64 2e 20 20 28 54 68 65  retrieved.  (The
39577 0a 2a 2a 20 20 20 20 20 20 20 66 65 77 65 72 20  .**       fewer 
39578 74 68 65 20 62 65 74 74 65 72 2e 29 0a 2a 2a 0a  the better.).**.
39579 2a 2a 20 20 20 20 2a 20 20 57 68 65 74 68 65 72  **    *  Whether
3957a 20 6f 72 20 6e 6f 74 20 73 6f 72 74 69 6e 67 20   or not sorting 
3957b 6d 75 73 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a  must occur..**.*
3957c 2a 20 20 20 20 2a 20 20 57 68 65 74 68 65 72 20  *    *  Whether 
3957d 6f 72 20 6e 6f 74 20 74 68 65 72 65 20 6d 75 73  or not there mus
3957e 74 20 62 65 20 73 65 70 61 72 61 74 65 20 6c 6f  t be separate lo
3957f 6f 6b 75 70 73 20 69 6e 20 74 68 65 0a 2a 2a 20  okups in the.** 
39580 20 20 20 20 20 20 69 6e 64 65 78 20 61 6e 64 20        index and 
39581 69 6e 20 74 68 65 20 6d 61 69 6e 20 74 61 62 6c  in the main tabl
39582 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  e..**.** If ther
39583 65 20 77 61 73 20 61 6e 20 49 4e 44 45 58 45 44  e was an INDEXED
39584 20 42 59 20 63 6c 61 75 73 65 20 28 70 53 72 63   BY clause (pSrc
39585 2d 3e 70 49 6e 64 65 78 29 20 61 74 74 61 63 68  ->pIndex) attach
39586 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ed to the table 
39587 69 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 20 73 74  in.** the SQL st
39588 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68  atement, then th
39589 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79  is function only
3958a 20 63 6f 6e 73 69 64 65 72 73 20 70 6c 61 6e 73   considers plans
3958b 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 6e   using the .** n
3958c 61 6d 65 64 20 69 6e 64 65 78 2e 20 49 66 20 6e  amed index. If n
3958d 6f 20 73 75 63 68 20 70 6c 61 6e 20 69 73 20 66  o such plan is f
3958e 6f 75 6e 64 2c 20 74 68 65 6e 20 74 68 65 20 72  ound, then the r
3958f 65 74 75 72 6e 65 64 20 63 6f 73 74 20 69 73 0a  eturned cost is.
39590 2a 2a 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42  ** SQLITE_BIG_DB
39591 4c 2e 20 49 66 20 61 20 70 6c 61 6e 20 69 73 20  L. If a plan is 
39592 66 6f 75 6e 64 20 74 68 61 74 20 75 73 65 73 20  found that uses 
39593 74 68 65 20 6e 61 6d 65 64 20 69 6e 64 65 78 2c  the named index,
39594 20 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 6f   .** then the co
39595 73 74 20 69 73 20 63 61 6c 63 75 6c 61 74 65 64  st is calculated
39596 20 69 6e 20 74 68 65 20 75 73 75 61 6c 20 77 61   in the usual wa
39597 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 4e 4f  y..**.** If a NO
39598 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65  T INDEXED clause
39599 20 77 61 73 20 61 74 74 61 63 68 65 64 20 74 6f   was attached to
3959a 20 74 68 65 20 74 61 62 6c 65 20 0a 2a 2a 20 69   the table .** i
3959b 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  n the SELECT sta
3959c 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 6e 6f 20  tement, then no 
3959d 69 6e 64 65 78 65 73 20 61 72 65 20 63 6f 6e 73  indexes are cons
3959e 69 64 65 72 65 64 2e 20 48 6f 77 65 76 65 72 2c  idered. However,
3959f 20 74 68 65 20 0a 2a 2a 20 73 65 6c 65 63 74 65   the .** selecte
395a0 64 20 70 6c 61 6e 20 6d 61 79 20 73 74 69 6c 6c  d plan may still
395a1 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20   take advantage 
395a2 6f 66 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  of the built-in 
395a3 72 6f 77 69 64 20 70 72 69 6d 61 72 79 20 6b 65  rowid primary ke
395a4 79 0a 2a 2a 20 69 6e 64 65 78 2e 0a 2a 2f 0a 73  y.** index..*/.s
395a5 74 61 74 69 63 20 76 6f 69 64 20 62 65 73 74 42  tatic void bestB
395a6 74 72 65 65 49 6e 64 65 78 28 57 68 65 72 65 42  treeIndex(WhereB
395a7 65 73 74 49 64 78 20 2a 70 29 7b 0a 20 20 50 61  estIdx *p){.  Pa
395a8 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 2d  rse *pParse = p-
395a9 3e 70 50 61 72 73 65 3b 20 20 2f 2a 20 54 68 65  >pParse;  /* The
395aa 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   parsing context
395ab 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73   */.  WhereClaus
395ac 65 20 2a 70 57 43 20 3d 20 70 2d 3e 70 57 43 3b  e *pWC = p->pWC;
395ad 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
395ae 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63  lause */.  struc
395af 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
395b0 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 20  pSrc = p->pSrc; 
395b1 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* The FROM clau
395b2 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63  se term to searc
395b3 68 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 20  h */.  int iCur 
395b4 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 3b  = pSrc->iCursor;
395b5 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72     /* The cursor
395b6 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
395b7 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a   be accessed */.
395b8 20 20 49 6e 64 65 78 20 2a 70 50 72 6f 62 65 3b    Index *pProbe;
395b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
395ba 20 41 6e 20 69 6e 64 65 78 20 77 65 20 61 72 65   An index we are
395bb 20 65 76 61 6c 75 61 74 69 6e 67 20 2a 2f 0a 20   evaluating */. 
395bc 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20   Index *pIdx;   
395bd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
395be 43 6f 70 79 20 6f 66 20 70 50 72 6f 62 65 2c 20  Copy of pProbe, 
395bf 6f 72 20 7a 65 72 6f 20 66 6f 72 20 49 50 4b 20  or zero for IPK 
395c0 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 65  index */.  int e
395c1 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20 20 20  qTermMask;      
395c2 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
395c3 74 20 6d 61 73 6b 20 6f 66 20 76 61 6c 69 64 20  t mask of valid 
395c4 65 71 75 61 6c 69 74 79 20 6f 70 65 72 61 74 6f  equality operato
395c5 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 45  rs */.  int idxE
395c6 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20 20 20  qTermMask;      
395c7 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6d 61 73      /* Index mas
395c8 6b 20 6f 66 20 76 61 6c 69 64 20 65 71 75 61 6c  k of valid equal
395c9 69 74 79 20 6f 70 65 72 61 74 6f 72 73 20 2a 2f  ity operators */
395ca 0a 20 20 49 6e 64 65 78 20 73 50 6b 3b 20 20 20  .  Index sPk;   
395cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
395cc 2a 20 41 20 66 61 6b 65 20 69 6e 64 65 78 20 6f  * A fake index o
395cd 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 70 72  bject for the pr
395ce 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20 20 74  imary key */.  t
395cf 52 6f 77 63 6e 74 20 61 69 52 6f 77 45 73 74 50  Rowcnt aiRowEstP
395d0 6b 5b 32 5d 3b 20 20 20 20 20 20 2f 2a 20 54 68  k[2];      /* Th
395d1 65 20 61 69 52 6f 77 45 73 74 5b 5d 20 76 61 6c  e aiRowEst[] val
395d2 75 65 20 66 6f 72 20 74 68 65 20 73 50 6b 20 69  ue for the sPk i
395d3 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 69  ndex */.  int ai
395d4 43 6f 6c 75 6d 6e 50 6b 20 3d 20 2d 31 3b 20 20  ColumnPk = -1;  
395d5 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 43 6f        /* The aCo
395d6 6c 75 6d 6e 5b 5d 20 76 61 6c 75 65 20 66 6f 72  lumn[] value for
395d7 20 74 68 65 20 73 50 6b 20 69 6e 64 65 78 20 2a   the sPk index *
395d8 2f 0a 20 20 69 6e 74 20 77 73 46 6c 61 67 4d 61  /.  int wsFlagMa
395d9 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sk;             
395da 2f 2a 20 41 6c 6c 6f 77 65 64 20 66 6c 61 67 73  /* Allowed flags
395db 20 69 6e 20 70 2d 3e 63 6f 73 74 2e 70 6c 61 6e   in p->cost.plan
395dc 2e 77 73 46 6c 61 67 20 2a 2f 0a 0a 20 20 2f 2a  .wsFlag */..  /*
395dd 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
395de 63 6f 73 74 20 74 6f 20 61 20 77 6f 72 73 74 2d  cost to a worst-
395df 63 61 73 65 20 76 61 6c 75 65 20 2a 2f 0a 20 20  case value */.  
395e0 6d 65 6d 73 65 74 28 26 70 2d 3e 63 6f 73 74 2c  memset(&p->cost,
395e1 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 63 6f   0, sizeof(p->co
395e2 73 74 29 29 3b 0a 20 20 70 2d 3e 63 6f 73 74 2e  st));.  p->cost.
395e3 72 43 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42  rCost = SQLITE_B
395e4 49 47 5f 44 42 4c 3b 0a 0a 20 20 2f 2a 20 49 66  IG_DBL;..  /* If
395e5 20 74 68 65 20 70 53 72 63 20 74 61 62 6c 65 20   the pSrc table 
395e6 69 73 20 74 68 65 20 72 69 67 68 74 20 74 61 62  is the right tab
395e7 6c 65 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49  le of a LEFT JOI
395e8 4e 20 74 68 65 6e 20 77 65 20 6d 61 79 20 6e 6f  N then we may no
395e9 74 0a 20 20 2a 2a 20 75 73 65 20 61 6e 20 69 6e  t.  ** use an in
395ea 64 65 78 20 74 6f 20 73 61 74 69 73 66 79 20 49  dex to satisfy I
395eb 53 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e  S NULL constrain
395ec 74 73 20 6f 6e 20 74 68 61 74 20 74 61 62 6c 65  ts on that table
395ed 2e 20 20 54 68 69 73 20 69 73 0a 20 20 2a 2a 20  .  This is.  ** 
395ee 62 65 63 61 75 73 65 20 63 6f 6c 75 6d 6e 73 20  because columns 
395ef 6d 69 67 68 74 20 65 6e 64 20 75 70 20 62 65 69  might end up bei
395f0 6e 67 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 74  ng NULL if the t
395f1 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61  able does not ma
395f2 74 63 68 20 2d 0a 20 20 2a 2a 20 61 20 63 69 72  tch -.  ** a cir
395f3 63 75 6d 73 74 61 6e 63 65 20 77 68 69 63 68 20  cumstance which 
395f4 74 68 65 20 69 6e 64 65 78 20 63 61 6e 6e 6f 74  the index cannot
395f5 20 68 65 6c 70 20 75 73 20 64 69 73 63 6f 76 65   help us discove
395f6 72 2e 20 20 54 69 63 6b 65 74 20 23 32 31 37 37  r.  Ticket #2177
395f7 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 72  ..  */.  if( pSr
395f8 63 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54  c->jointype & JT
395f9 5f 4c 45 46 54 20 29 7b 0a 20 20 20 20 69 64 78  _LEFT ){.    idx
395fa 45 71 54 65 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f  EqTermMask = WO_
395fb 45 51 7c 57 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73  EQ|WO_IN;.  }els
395fc 65 7b 0a 20 20 20 20 69 64 78 45 71 54 65 72 6d  e{.    idxEqTerm
395fd 4d 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f  Mask = WO_EQ|WO_
395fe 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 3b 0a 20 20  IN|WO_ISNULL;.  
395ff 7d 0a 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 70  }..  if( pSrc->p
39600 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 2f 2a 20  Index ){.    /* 
39601 41 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c  An INDEXED BY cl
39602 61 75 73 65 20 73 70 65 63 69 66 69 65 73 20 61  ause specifies a
39603 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64 65   particular inde
39604 78 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 20 20  x to use */.    
39605 70 49 64 78 20 3d 20 70 50 72 6f 62 65 20 3d 20  pIdx = pProbe = 
39606 70 53 72 63 2d 3e 70 49 6e 64 65 78 3b 0a 20 20  pSrc->pIndex;.  
39607 20 20 77 73 46 6c 61 67 4d 61 73 6b 20 3d 20 7e    wsFlagMask = ~
39608 28 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 7c  (WHERE_ROWID_EQ|
39609 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47  WHERE_ROWID_RANG
3960a 45 29 3b 0a 20 20 20 20 65 71 54 65 72 6d 4d 61  E);.    eqTermMa
3960b 73 6b 20 3d 20 69 64 78 45 71 54 65 72 6d 4d 61  sk = idxEqTermMa
3960c 73 6b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  sk;.  }else{.   
3960d 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20   /* There is no 
3960e 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73  INDEXED BY claus
3960f 65 2e 20 20 43 72 65 61 74 65 20 61 20 66 61 6b  e.  Create a fak
39610 65 20 49 6e 64 65 78 20 6f 62 6a 65 63 74 20 69  e Index object i
39611 6e 20 6c 6f 63 61 6c 0a 20 20 20 20 2a 2a 20 76  n local.    ** v
39612 61 72 69 61 62 6c 65 20 73 50 6b 20 74 6f 20 72  ariable sPk to r
39613 65 70 72 65 73 65 6e 74 20 74 68 65 20 72 6f 77  epresent the row
39614 69 64 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69  id primary key i
39615 6e 64 65 78 2e 20 20 4d 61 6b 65 20 74 68 69 73  ndex.  Make this
39616 0a 20 20 20 20 2a 2a 20 66 61 6b 65 20 69 6e 64  .    ** fake ind
39617 65 78 20 74 68 65 20 66 69 72 73 74 20 69 6e 20  ex the first in 
39618 61 20 63 68 61 69 6e 20 6f 66 20 49 6e 64 65 78  a chain of Index
39619 20 6f 62 6a 65 63 74 73 20 77 69 74 68 20 61 6c   objects with al
3961a 6c 20 6f 66 20 74 68 65 20 72 65 61 6c 0a 20 20  l of the real.  
3961b 20 20 2a 2a 20 69 6e 64 69 63 65 73 20 74 6f 20    ** indices to 
3961c 66 6f 6c 6c 6f 77 20 2a 2f 0a 20 20 20 20 49 6e  follow */.    In
3961d 64 65 78 20 2a 70 46 69 72 73 74 3b 20 20 20 20  dex *pFirst;    
3961e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3961f 20 46 69 72 73 74 20 6f 66 20 72 65 61 6c 20 69   First of real i
39620 6e 64 69 63 65 73 20 6f 6e 20 74 68 65 20 74 61  ndices on the ta
39621 62 6c 65 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65  ble */.    memse
39622 74 28 26 73 50 6b 2c 20 30 2c 20 73 69 7a 65 6f  t(&sPk, 0, sizeo
39623 66 28 49 6e 64 65 78 29 29 3b 0a 20 20 20 20 73  f(Index));.    s
39624 50 6b 2e 6e 43 6f 6c 75 6d 6e 20 3d 20 31 3b 0a  Pk.nColumn = 1;.
39625 20 20 20 20 73 50 6b 2e 61 69 43 6f 6c 75 6d 6e      sPk.aiColumn
39626 20 3d 20 26 61 69 43 6f 6c 75 6d 6e 50 6b 3b 0a   = &aiColumnPk;.
39627 20 20 20 20 73 50 6b 2e 61 69 52 6f 77 45 73 74      sPk.aiRowEst
39628 20 3d 20 61 69 52 6f 77 45 73 74 50 6b 3b 0a 20   = aiRowEstPk;. 
39629 20 20 20 73 50 6b 2e 6f 6e 45 72 72 6f 72 20 3d     sPk.onError =
3962a 20 4f 45 5f 52 65 70 6c 61 63 65 3b 0a 20 20 20   OE_Replace;.   
3962b 20 73 50 6b 2e 70 54 61 62 6c 65 20 3d 20 70 53   sPk.pTable = pS
3962c 72 63 2d 3e 70 54 61 62 3b 0a 20 20 20 20 61 69  rc->pTab;.    ai
3962d 52 6f 77 45 73 74 50 6b 5b 30 5d 20 3d 20 70 53  RowEstPk[0] = pS
3962e 72 63 2d 3e 70 54 61 62 2d 3e 6e 52 6f 77 45 73  rc->pTab->nRowEs
3962f 74 3b 0a 20 20 20 20 61 69 52 6f 77 45 73 74 50  t;.    aiRowEstP
39630 6b 5b 31 5d 20 3d 20 31 3b 0a 20 20 20 20 70 46  k[1] = 1;.    pF
39631 69 72 73 74 20 3d 20 70 53 72 63 2d 3e 70 54 61  irst = pSrc->pTa
39632 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 69  b->pIndex;.    i
39633 66 28 20 70 53 72 63 2d 3e 6e 6f 74 49 6e 64 65  f( pSrc->notInde
39634 78 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  xed==0 ){.      
39635 2f 2a 20 54 68 65 20 72 65 61 6c 20 69 6e 64 69  /* The real indi
39636 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ces of the table
39637 20 61 72 65 20 6f 6e 6c 79 20 63 6f 6e 73 69 64   are only consid
39638 65 72 65 64 20 69 66 20 74 68 65 0a 20 20 20 20  ered if the.    
39639 20 20 2a 2a 20 4e 4f 54 20 49 4e 44 45 58 45 44    ** NOT INDEXED
3963a 20 71 75 61 6c 69 66 69 65 72 20 69 73 20 6f 6d   qualifier is om
3963b 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 46  itted from the F
3963c 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ROM clause */.  
3963d 20 20 20 20 73 50 6b 2e 70 4e 65 78 74 20 3d 20      sPk.pNext = 
3963e 70 46 69 72 73 74 3b 0a 20 20 20 20 7d 0a 20 20  pFirst;.    }.  
3963f 20 20 70 50 72 6f 62 65 20 3d 20 26 73 50 6b 3b    pProbe = &sPk;
39640 0a 20 20 20 20 77 73 46 6c 61 67 4d 61 73 6b 20  .    wsFlagMask 
39641 3d 20 7e 28 0a 20 20 20 20 20 20 20 20 57 48 45  = ~(.        WHE
39642 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 7c 57 48 45  RE_COLUMN_IN|WHE
39643 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 7c 57 48 45  RE_COLUMN_EQ|WHE
39644 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 7c 57  RE_COLUMN_NULL|W
39645 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47  HERE_COLUMN_RANG
39646 45 0a 20 20 20 20 29 3b 0a 20 20 20 20 65 71 54  E.    );.    eqT
39647 65 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51 7c  ermMask = WO_EQ|
39648 57 4f 5f 49 4e 3b 0a 20 20 20 20 70 49 64 78 20  WO_IN;.    pIdx 
39649 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c  = 0;.  }..  /* L
3964a 6f 6f 70 20 6f 76 65 72 20 61 6c 6c 20 69 6e 64  oop over all ind
3964b 69 63 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  ices looking for
3964c 20 74 68 65 20 62 65 73 74 20 6f 6e 65 20 74 6f   the best one to
3964d 20 75 73 65 0a 20 20 2a 2f 0a 20 20 66 6f 72 28   use.  */.  for(
3964e 3b 20 70 50 72 6f 62 65 3b 20 70 49 64 78 3d 70  ; pProbe; pIdx=p
3964f 50 72 6f 62 65 3d 70 50 72 6f 62 65 2d 3e 70 4e  Probe=pProbe->pN
39650 65 78 74 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  ext){.    const 
39651 74 52 6f 77 63 6e 74 20 2a 20 63 6f 6e 73 74 20  tRowcnt * const 
39652 61 69 52 6f 77 45 73 74 20 3d 20 70 50 72 6f 62  aiRowEst = pProb
39653 65 2d 3e 61 69 52 6f 77 45 73 74 3b 0a 20 20 20  e->aiRowEst;.   
39654 20 57 68 65 72 65 43 6f 73 74 20 70 63 3b 20 20   WhereCost pc;  
39655 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39656 43 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 70 50  Cost of using pP
39657 72 6f 62 65 20 2a 2f 0a 20 20 20 20 64 6f 75 62  robe */.    doub
39658 6c 65 20 6c 6f 67 31 30 4e 20 3d 20 28 64 6f 75  le log10N = (dou
39659 62 6c 65 29 31 3b 20 20 2f 2a 20 62 61 73 65 2d  ble)1;  /* base-
3965a 31 30 20 6c 6f 67 61 72 69 74 68 6d 20 6f 66 20  10 logarithm of 
3965b 6e 52 6f 77 20 28 69 6e 65 78 61 63 74 29 20 2a  nRow (inexact) *
3965c 2f 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f  /..    /* The fo
3965d 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65  llowing variable
3965e 73 20 61 72 65 20 70 6f 70 75 6c 61 74 65 64 20  s are populated 
3965f 62 61 73 65 64 20 6f 6e 20 74 68 65 20 70 72 6f  based on the pro
39660 70 65 72 74 69 65 73 20 6f 66 0a 20 20 20 20 2a  perties of.    *
39661 2a 20 69 6e 64 65 78 20 62 65 69 6e 67 20 65 76  * index being ev
39662 61 6c 75 61 74 65 64 2e 20 54 68 65 79 20 61 72  aluated. They ar
39663 65 20 74 68 65 6e 20 75 73 65 64 20 74 6f 20 64  e then used to d
39664 65 74 65 72 6d 69 6e 65 20 74 68 65 20 65 78 70  etermine the exp
39665 65 63 74 65 64 0a 20 20 20 20 2a 2a 20 63 6f 73  ected.    ** cos
39666 74 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f 66 20  t and number of 
39667 72 6f 77 73 20 72 65 74 75 72 6e 65 64 2e 0a 20  rows returned.. 
39668 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 70 63     **.    **  pc
39669 2e 70 6c 61 6e 2e 6e 45 71 3a 20 0a 20 20 20 20  .plan.nEq: .    
3966a 2a 2a 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20  **    Number of 
3966b 65 71 75 61 6c 69 74 79 20 74 65 72 6d 73 20 74  equality terms t
3966c 68 61 74 20 63 61 6e 20 62 65 20 69 6d 70 6c 65  hat can be imple
3966d 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 74 68 65  mented using the
3966e 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2a 20 20   index..    **  
3966f 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
39670 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  , the number of 
39671 69 6e 69 74 69 61 6c 20 66 69 65 6c 64 73 20 69  initial fields i
39672 6e 20 74 68 65 20 69 6e 64 65 78 20 74 68 61 74  n the index that
39673 0a 20 20 20 20 2a 2a 20 20 20 20 61 72 65 20 75  .    **    are u
39674 73 65 64 20 69 6e 20 3d 3d 20 6f 72 20 49 4e 20  sed in == or IN 
39675 6f 72 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73  or NOT NULL cons
39676 74 72 61 69 6e 74 73 20 6f 66 20 74 68 65 20 57  traints of the W
39677 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 20 20 20  HERE clause..   
39678 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 6e 49 6e 4d   **.    **  nInM
39679 75 6c 3a 20 20 0a 20 20 20 20 2a 2a 20 20 20 20  ul:  .    **    
3967a 54 68 65 20 22 69 6e 2d 6d 75 6c 74 69 70 6c 69  The "in-multipli
3967b 65 72 22 2e 20 54 68 69 73 20 69 73 20 61 6e 20  er". This is an 
3967c 65 73 74 69 6d 61 74 65 20 6f 66 20 68 6f 77 20  estimate of how 
3967d 6d 61 6e 79 20 73 65 65 6b 20 6f 70 65 72 61 74  many seek operat
3967e 69 6f 6e 73 20 0a 20 20 20 20 2a 2a 20 20 20 20  ions .    **    
3967f 53 51 4c 69 74 65 20 6d 75 73 74 20 70 65 72 66  SQLite must perf
39680 6f 72 6d 20 6f 6e 20 74 68 65 20 69 6e 64 65 78  orm on the index
39681 20 69 6e 20 71 75 65 73 74 69 6f 6e 2e 20 46 6f   in question. Fo
39682 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68  r example, if th
39683 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 57 48 45  e .    **    WHE
39684 52 45 20 63 6c 61 75 73 65 20 69 73 3a 0a 20 20  RE clause is:.  
39685 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
39686 20 57 48 45 52 45 20 61 20 49 4e 20 28 31 2c 20   WHERE a IN (1, 
39687 32 2c 20 33 29 20 41 4e 44 20 62 20 49 4e 20 28  2, 3) AND b IN (
39688 34 2c 20 35 2c 20 36 29 0a 20 20 20 20 2a 2a 0a  4, 5, 6).    **.
39689 20 20 20 20 2a 2a 20 20 20 20 53 51 4c 69 74 65      **    SQLite
3968a 20 6d 75 73 74 20 70 65 72 66 6f 72 6d 20 39 20   must perform 9 
3968b 6c 6f 6f 6b 75 70 73 20 6f 6e 20 61 6e 20 69 6e  lookups on an in
3968c 64 65 78 20 6f 6e 20 28 61 2c 20 62 29 2c 20 73  dex on (a, b), s
3968d 6f 20 6e 49 6e 4d 75 6c 20 69 73 20 0a 20 20 20  o nInMul is .   
3968e 20 2a 2a 20 20 20 20 73 65 74 20 74 6f 20 39 2e   **    set to 9.
3968f 20 47 69 76 65 6e 20 74 68 65 20 73 61 6d 65 20   Given the same 
39690 73 63 68 65 6d 61 20 61 6e 64 20 65 69 74 68 65  schema and eithe
39691 72 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  r of the followi
39692 6e 67 20 57 48 45 52 45 20 0a 20 20 20 20 2a 2a  ng WHERE .    **
39693 20 20 20 20 63 6c 61 75 73 65 73 3a 0a 20 20 20      clauses:.   
39694 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
39695 57 48 45 52 45 20 61 20 3d 20 20 31 0a 20 20 20  WHERE a =  1.   
39696 20 2a 2a 20 20 20 20 20 20 57 48 45 52 45 20 61   **      WHERE a
39697 20 3e 3d 20 32 0a 20 20 20 20 2a 2a 0a 20 20 20   >= 2.    **.   
39698 20 2a 2a 20 20 20 20 6e 49 6e 4d 75 6c 20 69 73   **    nInMul is
39699 20 73 65 74 20 74 6f 20 31 2e 0a 20 20 20 20 2a   set to 1..    *
3969a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 49 66 20 74  *.    **    If t
3969b 68 65 72 65 20 65 78 69 73 74 73 20 61 20 57 48  here exists a WH
3969c 45 52 45 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ERE term of the 
3969d 66 6f 72 6d 20 22 78 20 49 4e 20 28 53 45 4c 45  form "x IN (SELE
3969e 43 54 20 2e 2e 2e 29 22 2c 20 74 68 65 6e 20 0a  CT ...)", then .
3969f 20 20 20 20 2a 2a 20 20 20 20 74 68 65 20 73 75      **    the su
396a0 62 2d 73 65 6c 65 63 74 20 69 73 20 61 73 73 75  b-select is assu
396a1 6d 65 64 20 74 6f 20 72 65 74 75 72 6e 20 32 35  med to return 25
396a2 20 72 6f 77 73 20 66 6f 72 20 74 68 65 20 70 75   rows for the pu
396a3 72 70 6f 73 65 73 20 6f 66 20 0a 20 20 20 20 2a  rposes of .    *
396a4 2a 20 20 20 20 64 65 74 65 72 6d 69 6e 69 6e 67  *    determining
396a5 20 6e 49 6e 4d 75 6c 2e 0a 20 20 20 20 2a 2a 0a   nInMul..    **.
396a6 20 20 20 20 2a 2a 20 20 62 49 6e 45 73 74 3a 20      **  bInEst: 
396a7 20 0a 20 20 20 20 2a 2a 20 20 20 20 53 65 74 20   .    **    Set 
396a8 74 6f 20 74 72 75 65 20 69 66 20 74 68 65 72 65  to true if there
396a9 20 77 61 73 20 61 74 20 6c 65 61 73 74 20 6f 6e   was at least on
396aa 65 20 22 78 20 49 4e 20 28 53 45 4c 45 43 54 20  e "x IN (SELECT 
396ab 2e 2e 2e 29 22 20 74 65 72 6d 20 75 73 65 64 20  ...)" term used 
396ac 0a 20 20 20 20 2a 2a 20 20 20 20 69 6e 20 64 65  .    **    in de
396ad 74 65 72 6d 69 6e 69 6e 67 20 74 68 65 20 76 61  termining the va
396ae 6c 75 65 20 6f 66 20 6e 49 6e 4d 75 6c 2e 20 20  lue of nInMul.  
396af 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 52 48  Note that the RH
396b0 53 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  S of the.    ** 
396b1 20 20 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 6d     IN operator m
396b2 75 73 74 20 62 65 20 61 20 53 45 4c 45 43 54 2c  ust be a SELECT,
396b3 20 6e 6f 74 20 61 20 76 61 6c 75 65 20 6c 69 73   not a value lis
396b4 74 2c 20 66 6f 72 20 74 68 69 73 20 76 61 72 69  t, for this vari
396b5 61 62 6c 65 0a 20 20 20 20 2a 2a 20 20 20 20 74  able.    **    t
396b6 6f 20 62 65 20 74 72 75 65 2e 0a 20 20 20 20 2a  o be true..    *
396b7 2a 0a 20 20 20 20 2a 2a 20 20 72 61 6e 67 65 44  *.    **  rangeD
396b8 69 76 3a 0a 20 20 20 20 2a 2a 20 20 20 20 41 6e  iv:.    **    An
396b9 20 65 73 74 69 6d 61 74 65 20 6f 66 20 61 20 64   estimate of a d
396ba 69 76 69 73 6f 72 20 62 79 20 77 68 69 63 68 20  ivisor by which 
396bb 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 73 65  to reduce the se
396bc 61 72 63 68 20 73 70 61 63 65 20 64 75 65 0a 20  arch space due. 
396bd 20 20 20 2a 2a 20 20 20 20 74 6f 20 69 6e 65 71     **    to ineq
396be 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
396bf 74 73 2e 20 20 49 6e 20 74 68 65 20 61 62 73 65  ts.  In the abse
396c0 6e 63 65 20 6f 66 20 73 71 6c 69 74 65 5f 73 74  nce of sqlite_st
396c1 61 74 33 20 41 4e 41 4c 59 5a 45 0a 20 20 20 20  at3 ANALYZE.    
396c2 2a 2a 20 20 20 20 64 61 74 61 2c 20 61 20 73 69  **    data, a si
396c3 6e 67 6c 65 20 69 6e 65 71 75 61 6c 69 74 79 20  ngle inequality 
396c4 72 65 64 75 63 65 73 20 74 68 65 20 73 65 61 72  reduces the sear
396c5 63 68 20 73 70 61 63 65 20 74 6f 20 31 2f 34 72  ch space to 1/4r
396c6 64 20 69 74 73 0a 20 20 20 20 2a 2a 20 20 20 20  d its.    **    
396c7 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 20 28 72  original size (r
396c8 61 6e 67 65 44 69 76 3d 3d 34 29 2e 20 20 54 77  angeDiv==4).  Tw
396c9 6f 20 69 6e 65 71 75 61 6c 69 74 69 65 73 20 72  o inequalities r
396ca 65 64 75 63 65 20 74 68 65 20 73 65 61 72 63 68  educe the search
396cb 0a 20 20 20 20 2a 2a 20 20 20 20 73 70 61 63 65  .    **    space
396cc 20 74 6f 20 31 2f 31 36 74 68 20 6f 66 20 69 74   to 1/16th of it
396cd 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 20  s original size 
396ce 28 72 61 6e 67 65 44 69 76 3d 3d 31 36 29 2e 0a  (rangeDiv==16)..
396cf 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 62      **.    **  b
396d0 53 6f 72 74 3a 20 20 20 0a 20 20 20 20 2a 2a 20  Sort:   .    ** 
396d1 20 20 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65     Boolean. True
396d2 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   if there is an 
396d3 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
396d4 74 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69 72  that will requir
396d5 65 20 61 6e 20 0a 20 20 20 20 2a 2a 20 20 20 20  e an .    **    
396d6 65 78 74 65 72 6e 61 6c 20 73 6f 72 74 20 28 69  external sort (i
396d7 2e 65 2e 20 73 63 61 6e 6e 69 6e 67 20 74 68 65  .e. scanning the
396d8 20 69 6e 64 65 78 20 62 65 69 6e 67 20 65 76 61   index being eva
396d9 6c 75 61 74 65 64 20 77 69 6c 6c 20 6e 6f 74 20  luated will not 
396da 0a 20 20 20 20 2a 2a 20 20 20 20 63 6f 72 72 65  .    **    corre
396db 63 74 6c 79 20 6f 72 64 65 72 20 72 65 63 6f 72  ctly order recor
396dc 64 73 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ds)..    **.    
396dd 2a 2a 20 20 62 44 69 73 74 3a 0a 20 20 20 20 2a  **  bDist:.    *
396de 2a 20 20 20 20 42 6f 6f 6c 65 61 6e 2e 20 54 72  *    Boolean. Tr
396df 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ue if there is a
396e0 20 44 49 53 54 49 4e 43 54 20 63 6c 61 75 73 65   DISTINCT clause
396e1 20 74 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69   that will requi
396e2 72 65 20 61 6e 20 0a 20 20 20 20 2a 2a 20 20 20  re an .    **   
396e3 20 65 78 74 65 72 6e 61 6c 20 62 74 72 65 65 2e   external btree.
396e4 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
396e5 62 4c 6f 6f 6b 75 70 3a 20 0a 20 20 20 20 2a 2a  bLookup: .    **
396e6 20 20 20 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75      Boolean. Tru
396e7 65 20 69 66 20 61 20 74 61 62 6c 65 20 6c 6f 6f  e if a table loo
396e8 6b 75 70 20 69 73 20 72 65 71 75 69 72 65 64 20  kup is required 
396e9 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20 65  for each index e
396ea 6e 74 72 79 0a 20 20 20 20 2a 2a 20 20 20 20 76  ntry.    **    v
396eb 69 73 69 74 65 64 2e 20 20 49 6e 20 6f 74 68 65  isited.  In othe
396ec 72 20 77 6f 72 64 73 2c 20 74 72 75 65 20 69 66  r words, true if
396ed 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20 63   this is not a c
396ee 6f 76 65 72 69 6e 67 20 69 6e 64 65 78 2e 0a 20  overing index.. 
396ef 20 20 20 2a 2a 20 20 20 20 54 68 69 73 20 69 73     **    This is
396f0 20 61 6c 77 61 79 73 20 66 61 6c 73 65 20 66 6f   always false fo
396f1 72 20 74 68 65 20 72 6f 77 69 64 20 70 72 69 6d  r the rowid prim
396f2 61 72 79 20 6b 65 79 20 69 6e 64 65 78 20 6f 66  ary key index of
396f3 20 61 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a   a table..    **
396f4 20 20 20 20 46 6f 72 20 6f 74 68 65 72 20 69 6e      For other in
396f5 64 65 78 65 73 2c 20 69 74 20 69 73 20 74 72 75  dexes, it is tru
396f6 65 20 75 6e 6c 65 73 73 20 61 6c 6c 20 74 68 65  e unless all the
396f7 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
396f8 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 20 20 20  table.    **    
396f9 75 73 65 64 20 62 79 20 74 68 65 20 53 45 4c 45  used by the SELE
396fa 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65  CT statement are
396fb 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20   present in the 
396fc 69 6e 64 65 78 20 28 73 75 63 68 20 61 6e 0a 20  index (such an. 
396fd 20 20 20 2a 2a 20 20 20 20 69 6e 64 65 78 20 69     **    index i
396fe 73 20 73 6f 6d 65 74 69 6d 65 73 20 64 65 73 63  s sometimes desc
396ff 72 69 62 65 64 20 61 73 20 61 20 63 6f 76 65 72  ribed as a cover
39700 69 6e 67 20 69 6e 64 65 78 29 2e 0a 20 20 20 20  ing index)..    
39701 2a 2a 20 20 20 20 46 6f 72 20 65 78 61 6d 70 6c  **    For exampl
39702 65 2c 20 67 69 76 65 6e 20 74 68 65 20 69 6e 64  e, given the ind
39703 65 78 20 6f 6e 20 28 61 2c 20 62 29 2c 20 74 68  ex on (a, b), th
39704 65 20 73 65 63 6f 6e 64 20 6f 66 20 74 68 65 20  e second of the 
39705 66 6f 6c 6c 6f 77 69 6e 67 20 0a 20 20 20 20 2a  following .    *
39706 2a 20 20 20 20 74 77 6f 20 71 75 65 72 69 65 73  *    two queries
39707 20 72 65 71 75 69 72 65 73 20 74 61 62 6c 65 20   requires table 
39708 62 2d 74 72 65 65 20 6c 6f 6f 6b 75 70 73 20 69  b-tree lookups i
39709 6e 20 6f 72 64 65 72 20 74 6f 20 66 69 6e 64 20  n order to find 
3970a 74 68 65 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a  the value.    **
3970b 20 20 20 20 6f 66 20 63 6f 6c 75 6d 6e 20 63 2c      of column c,
3970c 20 62 75 74 20 74 68 65 20 66 69 72 73 74 20 64   but the first d
3970d 6f 65 73 20 6e 6f 74 20 62 65 63 61 75 73 65 20  oes not because 
3970e 63 6f 6c 75 6d 6e 73 20 61 20 61 6e 64 20 62 20  columns a and b 
3970f 61 72 65 0a 20 20 20 20 2a 2a 20 20 20 20 62 6f  are.    **    bo
39710 74 68 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  th available in 
39711 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a  the index..    *
39712 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
39713 20 20 20 20 20 53 45 4c 45 43 54 20 61 2c 20 62       SELECT a, b
39714 20 20 20 20 46 52 4f 4d 20 74 62 6c 20 57 48 45      FROM tbl WHE
39715 52 45 20 61 20 3d 20 31 3b 0a 20 20 20 20 2a 2a  RE a = 1;.    **
39716 20 20 20 20 20 20 20 20 20 20 20 20 20 53 45 4c               SEL
39717 45 43 54 20 61 2c 20 62 2c 20 63 20 46 52 4f 4d  ECT a, b, c FROM
39718 20 74 62 6c 20 57 48 45 52 45 20 61 20 3d 20 31   tbl WHERE a = 1
39719 3b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ;.    */.    int
3971a 20 62 49 6e 45 73 74 20 3d 20 30 3b 20 20 20 20   bInEst = 0;    
3971b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
3971c 75 65 20 69 66 20 22 78 20 49 4e 20 28 53 45 4c  ue if "x IN (SEL
3971d 45 43 54 2e 2e 2e 29 22 20 73 65 65 6e 20 2a 2f  ECT...)" seen */
3971e 0a 20 20 20 20 69 6e 74 20 6e 49 6e 4d 75 6c 20  .    int nInMul 
3971f 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 1;            
39720 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
39721 64 69 73 74 69 6e 63 74 20 65 71 75 61 6c 69 74  distinct equalit
39722 69 65 73 20 74 6f 20 6c 6f 6f 6b 75 70 20 2a 2f  ies to lookup */
39723 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 61 6e 67  .    double rang
39724 65 44 69 76 20 3d 20 28 64 6f 75 62 6c 65 29 31  eDiv = (double)1
39725 3b 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20  ;  /* Estimated 
39726 72 65 64 75 63 74 69 6f 6e 20 69 6e 20 73 65 61  reduction in sea
39727 72 63 68 20 73 70 61 63 65 20 2a 2f 0a 20 20 20  rch space */.   
39728 20 69 6e 74 20 6e 42 6f 75 6e 64 20 3d 20 30 3b   int nBound = 0;
39729 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3972a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 61 6e 67  * Number of rang
3972b 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 73 65  e constraints se
3972c 65 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62 53  en */.    int bS
3972d 6f 72 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ort;            
3972e 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
3972f 69 66 20 65 78 74 65 72 6e 61 6c 20 73 6f 72 74  if external sort
39730 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 20   required */.   
39731 20 69 6e 74 20 62 44 69 73 74 3b 20 20 20 20 20   int bDist;     
39732 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39733 2a 20 54 72 75 65 20 69 66 20 69 6e 64 65 78 20  * True if index 
39734 63 61 6e 6e 6f 74 20 68 65 6c 70 20 77 69 74 68  cannot help with
39735 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 20   DISTINCT */.   
39736 20 69 6e 74 20 62 4c 6f 6f 6b 75 70 20 3d 20 30   int bLookup = 0
39737 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
39738 2a 20 54 72 75 65 20 69 66 20 6e 6f 74 20 61 20  * True if not a 
39739 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 78 20 2a  covering index *
3973a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 72 69 6f 72  /.    int nPrior
3973b 53 61 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  Sat;            
3973c 20 20 20 20 2f 2a 20 4f 52 44 45 52 20 42 59 20      /* ORDER BY 
3973d 74 65 72 6d 73 20 73 61 74 69 73 66 69 65 64 20  terms satisfied 
3973e 62 79 20 6f 75 74 65 72 20 6c 6f 6f 70 73 20 2a  by outer loops *
3973f 2f 0a 20 20 20 20 69 6e 74 20 6e 4f 72 64 65 72  /.    int nOrder
39740 42 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  By;             
39741 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
39742 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20   ORDER BY terms 
39743 2a 2f 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d  */.    WhereTerm
39744 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20   *pTerm;        
39745 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65       /* A single
39746 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45   term of the WHE
39747 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 23 69 66  RE clause */.#if
39748 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
39749 45 5f 53 54 41 54 33 0a 20 20 20 20 57 68 65 72  E_STAT3.    Wher
3974a 65 54 65 72 6d 20 2a 70 46 69 72 73 74 54 65 72  eTerm *pFirstTer
3974b 6d 20 3d 20 30 3b 20 20 20 20 2f 2a 20 46 69 72  m = 0;    /* Fir
3974c 73 74 20 74 65 72 6d 20 6d 61 74 63 68 69 6e 67  st term matching
3974d 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 23 65   the index */.#e
3974e 6e 64 69 66 0a 0a 20 20 20 20 57 48 45 52 45 54  ndif..    WHERET
3974f 52 41 43 45 28 28 0a 20 20 20 20 20 20 22 20 20  RACE((.      "  
39750 20 25 73 28 25 73 29 3a 5c 6e 22 2c 0a 20 20 20   %s(%s):\n",.   
39751 20 20 20 70 53 72 63 2d 3e 70 54 61 62 2d 3e 7a     pSrc->pTab->z
39752 4e 61 6d 65 2c 20 28 70 49 64 78 20 3f 20 70 49  Name, (pIdx ? pI
39753 64 78 2d 3e 7a 4e 61 6d 65 20 3a 20 22 69 70 6b  dx->zName : "ipk
39754 22 29 0a 20 20 20 20 29 29 3b 0a 20 20 20 20 6d  ").    ));.    m
39755 65 6d 73 65 74 28 26 70 63 2c 20 30 2c 20 73 69  emset(&pc, 0, si
39756 7a 65 6f 66 28 70 63 29 29 3b 0a 20 20 20 20 6e  zeof(pc));.    n
39757 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72  OrderBy = p->pOr
39758 64 65 72 42 79 20 3f 20 70 2d 3e 70 4f 72 64 65  derBy ? p->pOrde
39759 72 42 79 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 0a  rBy->nExpr : 0;.
3975a 20 20 20 20 69 66 28 20 70 2d 3e 69 20 29 7b 0a      if( p->i ){.
3975b 20 20 20 20 20 20 6e 50 72 69 6f 72 53 61 74 20        nPriorSat 
3975c 3d 20 70 63 2e 70 6c 61 6e 2e 6e 4f 42 53 61 74  = pc.plan.nOBSat
3975d 20 3d 20 70 2d 3e 61 4c 65 76 65 6c 5b 70 2d 3e   = p->aLevel[p->
3975e 69 2d 31 5d 2e 70 6c 61 6e 2e 6e 4f 42 53 61 74  i-1].plan.nOBSat
3975f 3b 0a 20 20 20 20 20 20 62 53 6f 72 74 20 3d 20  ;.      bSort = 
39760 6e 50 72 69 6f 72 53 61 74 3c 6e 4f 72 64 65 72  nPriorSat<nOrder
39761 42 79 3b 0a 20 20 20 20 20 20 62 44 69 73 74 20  By;.      bDist 
39762 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 0;.    }else{.
39763 20 20 20 20 20 20 6e 50 72 69 6f 72 53 61 74 20        nPriorSat 
39764 3d 20 70 63 2e 70 6c 61 6e 2e 6e 4f 42 53 61 74  = pc.plan.nOBSat
39765 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 53 6f 72   = 0;.      bSor
39766 74 20 3d 20 6e 4f 72 64 65 72 42 79 3e 30 3b 0a  t = nOrderBy>0;.
39767 20 20 20 20 20 20 62 44 69 73 74 20 3d 20 70 2d        bDist = p-
39768 3e 70 44 69 73 74 69 6e 63 74 21 3d 30 3b 0a 20  >pDistinct!=0;. 
39769 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 74     }..    /* Det
3976a 65 72 6d 69 6e 65 20 74 68 65 20 76 61 6c 75 65  ermine the value
3976b 73 20 6f 66 20 70 63 2e 70 6c 61 6e 2e 6e 45 71  s of pc.plan.nEq
3976c 20 61 6e 64 20 6e 49 6e 4d 75 6c 20 2a 2f 0a 20   and nInMul */. 
3976d 20 20 20 66 6f 72 28 70 63 2e 70 6c 61 6e 2e 6e     for(pc.plan.n
3976e 45 71 3d 30 3b 20 70 63 2e 70 6c 61 6e 2e 6e 45  Eq=0; pc.plan.nE
3976f 71 3c 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d  q<pProbe->nColum
39770 6e 3b 20 70 63 2e 70 6c 61 6e 2e 6e 45 71 2b 2b  n; pc.plan.nEq++
39771 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d  ){.      int j =
39772 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d   pProbe->aiColum
39773 6e 5b 70 63 2e 70 6c 61 6e 2e 6e 45 71 5d 3b 0a  n[pc.plan.nEq];.
39774 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20 66 69        pTerm = fi
39775 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
39776 2c 20 6a 2c 20 70 2d 3e 6e 6f 74 52 65 61 64 79  , j, p->notReady
39777 2c 20 65 71 54 65 72 6d 4d 61 73 6b 2c 20 70 49  , eqTermMask, pI
39778 64 78 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  dx);.      if( p
39779 54 65 72 6d 3d 3d 30 20 29 20 62 72 65 61 6b 3b  Term==0 ) break;
3977a 0a 20 20 20 20 20 20 70 63 2e 70 6c 61 6e 2e 77  .      pc.plan.w
3977b 73 46 6c 61 67 73 20 7c 3d 20 28 57 48 45 52 45  sFlags |= (WHERE
3977c 5f 43 4f 4c 55 4d 4e 5f 45 51 7c 57 48 45 52 45  _COLUMN_EQ|WHERE
3977d 5f 52 4f 57 49 44 5f 45 51 29 3b 0a 20 20 20 20  _ROWID_EQ);.    
3977e 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
3977f 6d 2d 3e 70 57 43 21 3d 70 57 43 20 29 3b 0a 20  m->pWC!=pWC );. 
39780 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e       if( pTerm->
39781 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49  eOperator & WO_I
39782 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70  N ){.        Exp
39783 72 20 2a 70 45 78 70 72 20 3d 20 70 54 65 72 6d  r *pExpr = pTerm
39784 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ->pExpr;.       
39785 20 70 63 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73   pc.plan.wsFlags
39786 20 7c 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e   |= WHERE_COLUMN
39787 5f 49 4e 3b 0a 20 20 20 20 20 20 20 20 69 66 28  _IN;.        if(
39788 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
39789 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  (pExpr, EP_xIsSe
3978a 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20  lect) ){.       
3978b 20 20 20 2f 2a 20 22 78 20 49 4e 20 28 53 45 4c     /* "x IN (SEL
3978c 45 43 54 20 2e 2e 2e 29 22 3a 20 20 41 73 73 75  ECT ...)":  Assu
3978d 6d 65 20 74 68 65 20 53 45 4c 45 43 54 20 72 65  me the SELECT re
3978e 74 75 72 6e 73 20 32 35 20 72 6f 77 73 20 2a 2f  turns 25 rows */
3978f 0a 20 20 20 20 20 20 20 20 20 20 6e 49 6e 4d 75  .          nInMu
39790 6c 20 2a 3d 20 32 35 3b 0a 20 20 20 20 20 20 20  l *= 25;.       
39791 20 20 20 62 49 6e 45 73 74 20 3d 20 31 3b 0a 20     bInEst = 1;. 
39792 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
39793 20 41 4c 57 41 59 53 28 70 45 78 70 72 2d 3e 78   ALWAYS(pExpr->x
39794 2e 70 4c 69 73 74 20 26 26 20 70 45 78 70 72 2d  .pList && pExpr-
39795 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 29  >x.pList->nExpr)
39796 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
39797 20 22 78 20 49 4e 20 28 76 61 6c 75 65 2c 20 76   "x IN (value, v
39798 61 6c 75 65 2c 20 2e 2e 2e 29 22 20 2a 2f 0a 20  alue, ...)" */. 
39799 20 20 20 20 20 20 20 20 20 6e 49 6e 4d 75 6c 20           nInMul 
3979a 2a 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  *= pExpr->x.pLis
3979b 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  t->nExpr;.      
3979c 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20    }.      }else 
3979d 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  if( pTerm->eOper
3979e 61 74 6f 72 20 26 20 57 4f 5f 49 53 4e 55 4c 4c  ator & WO_ISNULL
3979f 20 29 7b 0a 20 20 20 20 20 20 20 20 70 63 2e 70   ){.        pc.p
397a0 6c 61 6e 2e 77 73 46 6c 61 67 73 20 7c 3d 20 57  lan.wsFlags |= W
397a1 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c  HERE_COLUMN_NULL
397a2 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 64 65 66  ;.      }.#ifdef
397a3 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
397a4 54 41 54 33 0a 20 20 20 20 20 20 69 66 28 20 70  TAT3.      if( p
397a5 63 2e 70 6c 61 6e 2e 6e 45 71 3d 3d 30 20 26 26  c.plan.nEq==0 &&
397a6 20 70 50 72 6f 62 65 2d 3e 61 53 61 6d 70 6c 65   pProbe->aSample
397a7 20 29 20 70 46 69 72 73 74 54 65 72 6d 20 3d 20   ) pFirstTerm = 
397a8 70 54 65 72 6d 3b 0a 23 65 6e 64 69 66 0a 20 20  pTerm;.#endif.  
397a9 20 20 20 20 70 63 2e 75 73 65 64 20 7c 3d 20 70      pc.used |= p
397aa 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68  Term->prereqRigh
397ab 74 3b 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20 2f  t;.    }. .    /
397ac 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 20 62  * If the index b
397ad 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65 64 20  eing considered 
397ae 69 73 20 55 4e 49 51 55 45 2c 20 61 6e 64 20 74  is UNIQUE, and t
397af 68 65 72 65 20 69 73 20 61 6e 20 65 71 75 61 6c  here is an equal
397b0 69 74 79 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 73  ity .    ** cons
397b1 74 72 61 69 6e 74 20 66 6f 72 20 61 6c 6c 20 63  traint for all c
397b2 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e  olumns in the in
397b3 64 65 78 2c 20 74 68 65 6e 20 74 68 69 73 20 73  dex, then this s
397b4 65 61 72 63 68 20 77 69 6c 6c 20 66 69 6e 64 0a  earch will find.
397b5 20 20 20 20 2a 2a 20 61 74 20 6d 6f 73 74 20 61      ** at most a
397b6 20 73 69 6e 67 6c 65 20 72 6f 77 2e 20 49 6e 20   single row. In 
397b7 74 68 69 73 20 63 61 73 65 20 73 65 74 20 74 68  this case set th
397b8 65 20 57 48 45 52 45 5f 55 4e 49 51 55 45 20 66  e WHERE_UNIQUE f
397b9 6c 61 67 20 74 6f 20 0a 20 20 20 20 2a 2a 20 69  lag to .    ** i
397ba 6e 64 69 63 61 74 65 20 74 68 69 73 20 74 6f 20  ndicate this to 
397bb 74 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20  the caller..    
397bc 2a 2a 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77  **.    ** Otherw
397bd 69 73 65 2c 20 69 66 20 74 68 65 20 73 65 61 72  ise, if the sear
397be 63 68 20 6d 61 79 20 66 69 6e 64 20 6d 6f 72 65  ch may find more
397bf 20 74 68 61 6e 20 6f 6e 65 20 72 6f 77 2c 20 74   than one row, t
397c0 65 73 74 20 74 6f 20 73 65 65 20 69 66 0a 20 20  est to see if.  
397c1 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20    ** there is a 
397c2 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74  range constraint
397c3 20 6f 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c 75   on indexed colu
397c4 6d 6e 20 28 70 63 2e 70 6c 61 6e 2e 6e 45 71 2b  mn (pc.plan.nEq+
397c5 31 29 20 74 68 61 74 20 63 61 6e 20 62 65 20 0a  1) that can be .
397c6 20 20 20 20 2a 2a 20 6f 70 74 69 6d 69 7a 65 64      ** optimized
397c7 20 75 73 69 6e 67 20 74 68 65 20 69 6e 64 65 78   using the index
397c8 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  . .    */.    if
397c9 28 20 70 63 2e 70 6c 61 6e 2e 6e 45 71 3d 3d 70  ( pc.plan.nEq==p
397ca 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 20 26  Probe->nColumn &
397cb 26 20 70 50 72 6f 62 65 2d 3e 6f 6e 45 72 72 6f  & pProbe->onErro
397cc 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20  r!=OE_None ){.  
397cd 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 63      testcase( pc
397ce 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  .plan.wsFlags & 
397cf 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 20  WHERE_COLUMN_IN 
397d0 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
397d1 65 28 20 70 63 2e 70 6c 61 6e 2e 77 73 46 6c 61  e( pc.plan.wsFla
397d2 67 73 20 26 20 57 48 45 52 45 5f 43 4f 4c 55 4d  gs & WHERE_COLUM
397d3 4e 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20  N_NULL );.      
397d4 69 66 28 20 28 70 63 2e 70 6c 61 6e 2e 77 73 46  if( (pc.plan.wsF
397d5 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f 43 4f  lags & (WHERE_CO
397d6 4c 55 4d 4e 5f 49 4e 7c 57 48 45 52 45 5f 43 4f  LUMN_IN|WHERE_CO
397d7 4c 55 4d 4e 5f 4e 55 4c 4c 29 29 3d 3d 30 20 29  LUMN_NULL))==0 )
397d8 7b 0a 20 20 20 20 20 20 20 20 70 63 2e 70 6c 61  {.        pc.pla
397d9 6e 2e 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45  n.wsFlags |= WHE
397da 52 45 5f 55 4e 49 51 55 45 3b 0a 20 20 20 20 20  RE_UNIQUE;.     
397db 20 20 20 69 66 28 20 70 2d 3e 69 3d 3d 30 20 7c     if( p->i==0 |
397dc 7c 20 28 70 2d 3e 61 4c 65 76 65 6c 5b 70 2d 3e  | (p->aLevel[p->
397dd 69 2d 31 5d 2e 70 6c 61 6e 2e 77 73 46 6c 61 67  i-1].plan.wsFlag
397de 73 20 26 20 57 48 45 52 45 5f 41 4c 4c 5f 55 4e  s & WHERE_ALL_UN
397df 49 51 55 45 29 21 3d 30 20 29 7b 0a 20 20 20 20  IQUE)!=0 ){.    
397e0 20 20 20 20 20 20 70 63 2e 70 6c 61 6e 2e 77 73        pc.plan.ws
397e1 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 41  Flags |= WHERE_A
397e2 4c 4c 5f 55 4e 49 51 55 45 3b 0a 20 20 20 20 20  LL_UNIQUE;.     
397e3 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
397e4 20 7d 65 6c 73 65 20 69 66 28 20 70 50 72 6f 62   }else if( pProb
397e5 65 2d 3e 62 55 6e 6f 72 64 65 72 65 64 3d 3d 30  e->bUnordered==0
397e6 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b   ){.      int j;
397e7 0a 20 20 20 20 20 20 6a 20 3d 20 28 70 63 2e 70  .      j = (pc.p
397e8 6c 61 6e 2e 6e 45 71 3d 3d 70 50 72 6f 62 65 2d  lan.nEq==pProbe-
397e9 3e 6e 43 6f 6c 75 6d 6e 20 3f 20 2d 31 20 3a 20  >nColumn ? -1 : 
397ea 70 50 72 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e  pProbe->aiColumn
397eb 5b 70 63 2e 70 6c 61 6e 2e 6e 45 71 5d 29 3b 0a  [pc.plan.nEq]);.
397ec 20 20 20 20 20 20 69 66 28 20 66 69 6e 64 54 65        if( findTe
397ed 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c  rm(pWC, iCur, j,
397ee 20 70 2d 3e 6e 6f 74 52 65 61 64 79 2c 20 57 4f   p->notReady, WO
397ef 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c  _LT|WO_LE|WO_GT|
397f0 57 4f 5f 47 45 2c 20 70 49 64 78 29 20 29 7b 0a  WO_GE, pIdx) ){.
397f1 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65 72          WhereTer
397f2 6d 20 2a 70 54 6f 70 2c 20 2a 70 42 74 6d 3b 0a  m *pTop, *pBtm;.
397f3 20 20 20 20 20 20 20 20 70 54 6f 70 20 3d 20 66          pTop = f
397f4 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75  indTerm(pWC, iCu
397f5 72 2c 20 6a 2c 20 70 2d 3e 6e 6f 74 52 65 61 64  r, j, p->notRead
397f6 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20  y, WO_LT|WO_LE, 
397f7 70 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 70  pIdx);.        p
397f8 42 74 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  Btm = findTerm(p
397f9 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 70 2d 3e  WC, iCur, j, p->
397fa 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c  notReady, WO_GT|
397fb 57 4f 5f 47 45 2c 20 70 49 64 78 29 3b 0a 20 20  WO_GE, pIdx);.  
397fc 20 20 20 20 20 20 77 68 65 72 65 52 61 6e 67 65        whereRange
397fd 53 63 61 6e 45 73 74 28 70 50 61 72 73 65 2c 20  ScanEst(pParse, 
397fe 70 50 72 6f 62 65 2c 20 70 63 2e 70 6c 61 6e 2e  pProbe, pc.plan.
397ff 6e 45 71 2c 20 70 42 74 6d 2c 20 70 54 6f 70 2c  nEq, pBtm, pTop,
39800 20 26 72 61 6e 67 65 44 69 76 29 3b 0a 20 20 20   &rangeDiv);.   
39801 20 20 20 20 20 69 66 28 20 70 54 6f 70 20 29 7b       if( pTop ){
39802 0a 20 20 20 20 20 20 20 20 20 20 6e 42 6f 75 6e  .          nBoun
39803 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  d = 1;.         
39804 20 70 63 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73   pc.plan.wsFlags
39805 20 7c 3d 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49   |= WHERE_TOP_LI
39806 4d 49 54 3b 0a 20 20 20 20 20 20 20 20 20 20 70  MIT;.          p
39807 63 2e 75 73 65 64 20 7c 3d 20 70 54 6f 70 2d 3e  c.used |= pTop->
39808 70 72 65 72 65 71 52 69 67 68 74 3b 0a 20 20 20  prereqRight;.   
39809 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
3980a 20 70 54 6f 70 2d 3e 70 57 43 21 3d 70 57 43 20   pTop->pWC!=pWC 
3980b 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
3980c 20 20 20 20 20 69 66 28 20 70 42 74 6d 20 29 7b       if( pBtm ){
3980d 0a 20 20 20 20 20 20 20 20 20 20 6e 42 6f 75 6e  .          nBoun
3980e 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 70  d++;.          p
3980f 63 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 7c  c.plan.wsFlags |
39810 3d 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49  = WHERE_BTM_LIMI
39811 54 3b 0a 20 20 20 20 20 20 20 20 20 20 70 63 2e  T;.          pc.
39812 75 73 65 64 20 7c 3d 20 70 42 74 6d 2d 3e 70 72  used |= pBtm->pr
39813 65 72 65 71 52 69 67 68 74 3b 0a 20 20 20 20 20  ereqRight;.     
39814 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
39815 42 74 6d 2d 3e 70 57 43 21 3d 70 57 43 20 29 3b  Btm->pWC!=pWC );
39816 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
39817 20 20 20 70 63 2e 70 6c 61 6e 2e 77 73 46 6c 61     pc.plan.wsFla
39818 67 73 20 7c 3d 20 28 57 48 45 52 45 5f 43 4f 4c  gs |= (WHERE_COL
39819 55 4d 4e 5f 52 41 4e 47 45 7c 57 48 45 52 45 5f  UMN_RANGE|WHERE_
3981a 52 4f 57 49 44 5f 52 41 4e 47 45 29 3b 0a 20 20  ROWID_RANGE);.  
3981b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
3981c 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
3981d 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  an ORDER BY clau
3981e 73 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78  se and the index
3981f 20 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65   being considere
39820 64 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 6e 61  d will.    ** na
39821 74 75 72 61 6c 6c 79 20 73 63 61 6e 20 72 6f 77  turally scan row
39822 73 20 69 6e 20 74 68 65 20 72 65 71 75 69 72 65  s in the require
39823 64 20 6f 72 64 65 72 2c 20 73 65 74 20 74 68 65  d order, set the
39824 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6c 61   appropriate fla
39825 67 73 0a 20 20 20 20 2a 2a 20 69 6e 20 70 63 2e  gs.    ** in pc.
39826 70 6c 61 6e 2e 77 73 46 6c 61 67 73 2e 20 4f 74  plan.wsFlags. Ot
39827 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 72  herwise, if ther
39828 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59  e is an ORDER BY
39829 20 63 6c 61 75 73 65 20 62 75 74 0a 20 20 20 20   clause but.    
3982a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 77 69 6c  ** the index wil
3982b 6c 20 73 63 61 6e 20 72 6f 77 73 20 69 6e 20 61  l scan rows in a
3982c 20 64 69 66 66 65 72 65 6e 74 20 6f 72 64 65 72   different order
3982d 2c 20 73 65 74 20 74 68 65 20 62 53 6f 72 74 0a  , set the bSort.
3982e 20 20 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 2e      ** variable.
3982f 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 53 6f    */.    if( bSo
39830 72 74 20 26 26 20 28 70 53 72 63 2d 3e 6a 6f 69  rt && (pSrc->joi
39831 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45 46 54 29  ntype & JT_LEFT)
39832 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ==0 ){.      int
39833 20 62 52 65 76 20 3d 20 32 3b 0a 20 20 20 20 20   bRev = 2;.     
39834 20 57 48 45 52 45 54 52 41 43 45 28 28 22 20 20   WHERETRACE(("  
39835 20 20 20 20 2d 2d 3e 20 62 65 66 6f 72 65 20 69      --> before i
39836 73 53 6f 72 74 69 6e 67 49 6e 64 65 78 3a 20 6e  sSortingIndex: n
39837 50 72 69 6f 72 53 61 74 3d 25 64 5c 6e 22 2c 6e  PriorSat=%d\n",n
39838 50 72 69 6f 72 53 61 74 29 29 3b 0a 20 20 20 20  PriorSat));.    
39839 20 20 70 63 2e 70 6c 61 6e 2e 6e 4f 42 53 61 74    pc.plan.nOBSat
3983a 20 3d 20 69 73 53 6f 72 74 69 6e 67 49 6e 64 65   = isSortingInde
3983b 78 28 70 2c 20 70 50 72 6f 62 65 2c 20 69 43 75  x(p, pProbe, iCu
3983c 72 2c 20 26 62 52 65 76 29 3b 0a 20 20 20 20 20  r, &bRev);.     
3983d 20 57 48 45 52 45 54 52 41 43 45 28 28 22 20 20   WHERETRACE(("  
3983e 20 20 20 20 2d 2d 3e 20 61 66 74 65 72 20 20 69      --> after  i
3983f 73 53 6f 72 74 69 6e 67 49 6e 64 65 78 3a 20 62  sSortingIndex: b
39840 52 65 76 3d 25 64 20 6e 4f 42 53 61 74 3d 25 64  Rev=%d nOBSat=%d
39841 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
39842 20 20 20 20 20 20 20 62 52 65 76 2c 20 70 63 2e         bRev, pc.
39843 70 6c 61 6e 2e 6e 4f 42 53 61 74 29 29 3b 0a 20  plan.nOBSat));. 
39844 20 20 20 20 20 69 66 28 20 6e 50 72 69 6f 72 53       if( nPriorS
39845 61 74 3c 70 63 2e 70 6c 61 6e 2e 6e 4f 42 53 61  at<pc.plan.nOBSa
39846 74 20 7c 7c 20 28 70 63 2e 70 6c 61 6e 2e 77 73  t || (pc.plan.ws
39847 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 55 4e  Flags & WHERE_UN
39848 49 51 55 45 29 21 3d 30 20 29 7b 0a 20 20 20 20  IQUE)!=0 ){.    
39849 20 20 20 20 70 63 2e 70 6c 61 6e 2e 77 73 46 6c      pc.plan.wsFl
3984a 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 4f 52 44  ags |= WHERE_ORD
3984b 45 52 45 44 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ERED;.      }.  
3984c 20 20 20 20 69 66 28 20 6e 4f 72 64 65 72 42 79      if( nOrderBy
3984d 3d 3d 70 63 2e 70 6c 61 6e 2e 6e 4f 42 53 61 74  ==pc.plan.nOBSat
3984e 20 29 7b 0a 20 20 20 20 20 20 20 20 62 53 6f 72   ){.        bSor
3984f 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  t = 0;.        p
39850 63 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 7c  c.plan.wsFlags |
39851 3d 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41  = WHERE_ROWID_RA
39852 4e 47 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  NGE|WHERE_COLUMN
39853 5f 52 41 4e 47 45 3b 0a 20 20 20 20 20 20 7d 0a  _RANGE;.      }.
39854 20 20 20 20 20 20 69 66 28 20 62 52 65 76 20 26        if( bRev &
39855 20 31 20 29 20 70 63 2e 70 6c 61 6e 2e 77 73 46   1 ) pc.plan.wsF
39856 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 52 45  lags |= WHERE_RE
39857 56 45 52 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20  VERSE;.    }..  
39858 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
39859 20 61 20 44 49 53 54 49 4e 43 54 20 71 75 61 6c   a DISTINCT qual
3985a 69 66 69 65 72 20 61 6e 64 20 74 68 69 73 20 69  ifier and this i
3985b 6e 64 65 78 20 77 69 6c 6c 20 73 63 61 6e 20 72  ndex will scan r
3985c 6f 77 73 20 69 6e 0a 20 20 20 20 2a 2a 20 6f 72  ows in.    ** or
3985d 64 65 72 20 6f 66 20 74 68 65 20 44 49 53 54 49  der of the DISTI
3985e 4e 43 54 20 65 78 70 72 65 73 73 69 6f 6e 73 2c  NCT expressions,
3985f 20 63 6c 65 61 72 20 62 44 69 73 74 20 61 6e 64   clear bDist and
39860 20 73 65 74 20 74 68 65 20 61 70 70 72 6f 70 72   set the appropr
39861 69 61 74 65 0a 20 20 20 20 2a 2a 20 66 6c 61 67  iate.    ** flag
39862 73 20 69 6e 20 70 63 2e 70 6c 61 6e 2e 77 73 46  s in pc.plan.wsF
39863 6c 61 67 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28  lags. */.    if(
39864 20 62 44 69 73 74 0a 20 20 20 20 20 26 26 20 69   bDist.     && i
39865 73 44 69 73 74 69 6e 63 74 49 6e 64 65 78 28 70  sDistinctIndex(p
39866 50 61 72 73 65 2c 20 70 57 43 2c 20 70 50 72 6f  Parse, pWC, pPro
39867 62 65 2c 20 69 43 75 72 2c 20 70 2d 3e 70 44 69  be, iCur, p->pDi
39868 73 74 69 6e 63 74 2c 20 70 63 2e 70 6c 61 6e 2e  stinct, pc.plan.
39869 6e 45 71 29 0a 20 20 20 20 20 26 26 20 28 70 63  nEq).     && (pc
3986a 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  .plan.wsFlags & 
3986b 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 29  WHERE_COLUMN_IN)
3986c 3d 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  ==0.    ){.     
3986d 20 62 44 69 73 74 20 3d 20 30 3b 0a 20 20 20 20   bDist = 0;.    
3986e 20 20 70 63 2e 70 6c 61 6e 2e 77 73 46 6c 61 67    pc.plan.wsFlag
3986f 73 20 7c 3d 20 57 48 45 52 45 5f 52 4f 57 49 44  s |= WHERE_ROWID
39870 5f 52 41 4e 47 45 7c 57 48 45 52 45 5f 43 4f 4c  _RANGE|WHERE_COL
39871 55 4d 4e 5f 52 41 4e 47 45 7c 57 48 45 52 45 5f  UMN_RANGE|WHERE_
39872 44 49 53 54 49 4e 43 54 3b 0a 20 20 20 20 7d 0a  DISTINCT;.    }.
39873 0a 20 20 20 20 2f 2a 20 49 66 20 63 75 72 72 65  .    /* If curre
39874 6e 74 6c 79 20 63 61 6c 63 75 6c 61 74 69 6e 67  ntly calculating
39875 20 74 68 65 20 63 6f 73 74 20 6f 66 20 75 73 69   the cost of usi
39876 6e 67 20 61 6e 20 69 6e 64 65 78 20 28 6e 6f 74  ng an index (not
39877 20 74 68 65 20 49 50 4b 0a 20 20 20 20 2a 2a 20   the IPK.    ** 
39878 69 6e 64 65 78 29 2c 20 64 65 74 65 72 6d 69 6e  index), determin
39879 65 20 69 66 20 61 6c 6c 20 72 65 71 75 69 72 65  e if all require
3987a 64 20 63 6f 6c 75 6d 6e 20 64 61 74 61 20 6d 61  d column data ma
3987b 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20 77 69  y be obtained wi
3987c 74 68 6f 75 74 20 0a 20 20 20 20 2a 2a 20 75 73  thout .    ** us
3987d 69 6e 67 20 74 68 65 20 6d 61 69 6e 20 74 61 62  ing the main tab
3987e 6c 65 20 28 69 2e 65 2e 20 69 66 20 74 68 65 20  le (i.e. if the 
3987f 69 6e 64 65 78 20 69 73 20 61 20 63 6f 76 65 72  index is a cover
39880 69 6e 67 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78  ing.    ** index
39881 20 66 6f 72 20 74 68 69 73 20 71 75 65 72 79 29   for this query)
39882 2e 20 49 66 20 69 74 20 69 73 2c 20 73 65 74 20  . If it is, set 
39883 74 68 65 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e  the WHERE_IDX_ON
39884 4c 59 20 66 6c 61 67 20 69 6e 0a 20 20 20 20 2a  LY flag in.    *
39885 2a 20 70 63 2e 70 6c 61 6e 2e 77 73 46 6c 61 67  * pc.plan.wsFlag
39886 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 73 65  s. Otherwise, se
39887 74 20 74 68 65 20 62 4c 6f 6f 6b 75 70 20 76 61  t the bLookup va
39888 72 69 61 62 6c 65 20 74 6f 20 74 72 75 65 2e 20  riable to true. 
39889 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 49 64 78   */.    if( pIdx
3988a 20 29 7b 0a 20 20 20 20 20 20 42 69 74 6d 61 73   ){.      Bitmas
3988b 6b 20 6d 20 3d 20 70 53 72 63 2d 3e 63 6f 6c 55  k m = pSrc->colU
3988c 73 65 64 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a  sed;.      int j
3988d 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ;.      for(j=0;
3988e 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   j<pIdx->nColumn
3988f 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
39890 69 6e 74 20 78 20 3d 20 70 49 64 78 2d 3e 61 69  int x = pIdx->ai
39891 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 20  Column[j];.     
39892 20 20 20 69 66 28 20 78 3c 42 4d 53 2d 31 20 29     if( x<BMS-1 )
39893 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 20 26 3d  {.          m &=
39894 20 7e 28 28 28 42 69 74 6d 61 73 6b 29 31 29 3c   ~(((Bitmask)1)<
39895 3c 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  <x);.        }. 
39896 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
39897 20 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20   m==0 ){.       
39898 20 70 63 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73   pc.plan.wsFlags
39899 20 7c 3d 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e   |= WHERE_IDX_ON
3989a 4c 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  LY;.      }else{
3989b 0a 20 20 20 20 20 20 20 20 62 4c 6f 6f 6b 75 70  .        bLookup
3989c 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
3989d 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20    }..    /*.    
3989e 2a 2a 20 45 73 74 69 6d 61 74 65 20 74 68 65 20  ** Estimate the 
3989f 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 6f  number of rows o
398a0 66 20 6f 75 74 70 75 74 2e 20 20 46 6f 72 20 61  f output.  For a
398a1 6e 20 22 78 20 49 4e 20 28 53 45 4c 45 43 54 2e  n "x IN (SELECT.
398a2 2e 2e 29 22 0a 20 20 20 20 2a 2a 20 63 6f 6e 73  ..)".    ** cons
398a3 74 72 61 69 6e 74 2c 20 64 6f 20 6e 6f 74 20 6c  traint, do not l
398a4 65 74 20 74 68 65 20 65 73 74 69 6d 61 74 65 20  et the estimate 
398a5 65 78 63 65 65 64 20 68 61 6c 66 20 74 68 65 20  exceed half the 
398a6 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c  rows in the tabl
398a7 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63  e..    */.    pc
398a8 2e 70 6c 61 6e 2e 6e 52 6f 77 20 3d 20 28 64 6f  .plan.nRow = (do
398a9 75 62 6c 65 29 28 61 69 52 6f 77 45 73 74 5b 70  uble)(aiRowEst[p
398aa 63 2e 70 6c 61 6e 2e 6e 45 71 5d 20 2a 20 6e 49  c.plan.nEq] * nI
398ab 6e 4d 75 6c 29 3b 0a 20 20 20 20 69 66 28 20 62  nMul);.    if( b
398ac 49 6e 45 73 74 20 26 26 20 70 63 2e 70 6c 61 6e  InEst && pc.plan
398ad 2e 6e 52 6f 77 2a 32 3e 61 69 52 6f 77 45 73 74  .nRow*2>aiRowEst
398ae 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 70 63 2e  [0] ){.      pc.
398af 70 6c 61 6e 2e 6e 52 6f 77 20 3d 20 61 69 52 6f  plan.nRow = aiRo
398b0 77 45 73 74 5b 30 5d 2f 32 3b 0a 20 20 20 20 20  wEst[0]/2;.     
398b1 20 6e 49 6e 4d 75 6c 20 3d 20 28 69 6e 74 29 28   nInMul = (int)(
398b2 70 63 2e 70 6c 61 6e 2e 6e 52 6f 77 20 2f 20 61  pc.plan.nRow / a
398b3 69 52 6f 77 45 73 74 5b 70 63 2e 70 6c 61 6e 2e  iRowEst[pc.plan.
398b4 6e 45 71 5d 29 3b 0a 20 20 20 20 7d 0a 0a 23 69  nEq]);.    }..#i
398b5 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
398b6 4c 45 5f 53 54 41 54 33 0a 20 20 20 20 2f 2a 20  LE_STAT3.    /* 
398b7 49 66 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  If the constrain
398b8 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  t is of the form
398b9 20 78 3d 56 41 4c 55 45 20 6f 72 20 78 20 49 4e   x=VALUE or x IN
398ba 20 28 45 31 2c 45 32 2c 2e 2e 2e 29 0a 20 20 20   (E1,E2,...).   
398bb 20 2a 2a 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f   ** and we do no
398bc 74 20 74 68 69 6e 6b 20 74 68 61 74 20 76 61 6c  t think that val
398bd 75 65 73 20 6f 66 20 78 20 61 72 65 20 75 6e 69  ues of x are uni
398be 71 75 65 20 61 6e 64 20 69 66 20 68 69 73 74 6f  que and if histo
398bf 67 72 61 6d 0a 20 20 20 20 2a 2a 20 64 61 74 61  gram.    ** data
398c0 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 66 6f   is available fo
398c1 72 20 63 6f 6c 75 6d 6e 20 78 2c 20 74 68 65 6e  r column x, then
398c2 20 69 74 20 6d 69 67 68 74 20 62 65 20 70 6f 73   it might be pos
398c3 73 69 62 6c 65 0a 20 20 20 20 2a 2a 20 74 6f 20  sible.    ** to 
398c4 67 65 74 20 61 20 62 65 74 74 65 72 20 65 73 74  get a better est
398c5 69 6d 61 74 65 20 6f 6e 20 74 68 65 20 6e 75 6d  imate on the num
398c6 62 65 72 20 6f 66 20 72 6f 77 73 20 62 61 73 65  ber of rows base
398c7 64 20 6f 6e 0a 20 20 20 20 2a 2a 20 56 41 4c 55  d on.    ** VALU
398c8 45 20 61 6e 64 20 68 6f 77 20 63 6f 6d 6d 6f 6e  E and how common
398c9 20 74 68 61 74 20 76 61 6c 75 65 20 69 73 20 61   that value is a
398ca 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20  ccording to the 
398cb 68 69 73 74 6f 67 72 61 6d 2e 0a 20 20 20 20 2a  histogram..    *
398cc 2f 0a 20 20 20 20 69 66 28 20 70 63 2e 70 6c 61  /.    if( pc.pla
398cd 6e 2e 6e 52 6f 77 3e 28 64 6f 75 62 6c 65 29 31  n.nRow>(double)1
398ce 20 26 26 20 70 63 2e 70 6c 61 6e 2e 6e 45 71 3d   && pc.plan.nEq=
398cf 3d 31 0a 20 20 20 20 20 26 26 20 70 46 69 72 73  =1.     && pFirs
398d0 74 54 65 72 6d 21 3d 30 20 26 26 20 61 69 52 6f  tTerm!=0 && aiRo
398d1 77 45 73 74 5b 31 5d 3e 31 20 29 7b 0a 20 20 20  wEst[1]>1 ){.   
398d2 20 20 20 61 73 73 65 72 74 28 20 28 70 46 69 72     assert( (pFir
398d3 73 74 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  stTerm->eOperato
398d4 72 20 26 20 28 57 4f 5f 45 51 7c 57 4f 5f 49 53  r & (WO_EQ|WO_IS
398d5 4e 55 4c 4c 7c 57 4f 5f 49 4e 29 29 21 3d 30 20  NULL|WO_IN))!=0 
398d6 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 46 69  );.      if( pFi
398d7 72 73 74 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  rstTerm->eOperat
398d8 6f 72 20 26 20 28 57 4f 5f 45 51 7c 57 4f 5f 49  or & (WO_EQ|WO_I
398d9 53 4e 55 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20  SNULL) ){.      
398da 20 20 74 65 73 74 63 61 73 65 28 20 70 46 69 72    testcase( pFir
398db 73 74 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  stTerm->eOperato
398dc 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20  r==WO_EQ );.    
398dd 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 46      testcase( pF
398de 69 72 73 74 54 65 72 6d 2d 3e 65 4f 70 65 72 61  irstTerm->eOpera
398df 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29  tor==WO_ISNULL )
398e0 3b 0a 20 20 20 20 20 20 20 20 77 68 65 72 65 45  ;.        whereE
398e1 71 75 61 6c 53 63 61 6e 45 73 74 28 70 50 61 72  qualScanEst(pPar
398e2 73 65 2c 20 70 50 72 6f 62 65 2c 20 70 46 69 72  se, pProbe, pFir
398e3 73 74 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70  stTerm->pExpr->p
398e4 52 69 67 68 74 2c 0a 20 20 20 20 20 20 20 20 20  Right,.         
398e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
398e6 20 26 70 63 2e 70 6c 61 6e 2e 6e 52 6f 77 29 3b   &pc.plan.nRow);
398e7 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
398e8 20 62 49 6e 45 73 74 3d 3d 30 20 29 7b 0a 20 20   bInEst==0 ){.  
398e9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46        assert( pF
398ea 69 72 73 74 54 65 72 6d 2d 3e 65 4f 70 65 72 61  irstTerm->eOpera
398eb 74 6f 72 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20 20  tor==WO_IN );.  
398ec 20 20 20 20 20 20 77 68 65 72 65 49 6e 53 63 61        whereInSca
398ed 6e 45 73 74 28 70 50 61 72 73 65 2c 20 70 50 72  nEst(pParse, pPr
398ee 6f 62 65 2c 20 70 46 69 72 73 74 54 65 72 6d 2d  obe, pFirstTerm-
398ef 3e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2c  >pExpr->x.pList,
398f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
398f1 20 20 20 20 20 20 20 20 26 70 63 2e 70 6c 61 6e          &pc.plan
398f2 2e 6e 52 6f 77 29 3b 0a 20 20 20 20 20 20 7d 0a  .nRow);.      }.
398f3 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
398f4 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
398f5 41 54 33 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41  AT3 */..    /* A
398f6 64 6a 75 73 74 20 74 68 65 20 6e 75 6d 62 65 72  djust the number
398f7 20 6f 66 20 6f 75 74 70 75 74 20 72 6f 77 73 20   of output rows 
398f8 61 6e 64 20 64 6f 77 6e 77 61 72 64 20 74 6f 20  and downward to 
398f9 72 65 66 6c 65 63 74 20 72 6f 77 73 0a 20 20 20  reflect rows.   
398fa 20 2a 2a 20 74 68 61 74 20 61 72 65 20 65 78 63   ** that are exc
398fb 6c 75 64 65 64 20 62 79 20 72 61 6e 67 65 20 63  luded by range c
398fc 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 20 20 20 20  onstraints..    
398fd 2a 2f 0a 20 20 20 20 70 63 2e 70 6c 61 6e 2e 6e  */.    pc.plan.n
398fe 52 6f 77 20 3d 20 70 63 2e 70 6c 61 6e 2e 6e 52  Row = pc.plan.nR
398ff 6f 77 2f 72 61 6e 67 65 44 69 76 3b 0a 20 20 20  ow/rangeDiv;.   
39900 20 69 66 28 20 70 63 2e 70 6c 61 6e 2e 6e 52 6f   if( pc.plan.nRo
39901 77 3c 31 20 29 20 70 63 2e 70 6c 61 6e 2e 6e 52  w<1 ) pc.plan.nR
39902 6f 77 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20  ow = 1;..    /* 
39903 45 78 70 65 72 69 6d 65 6e 74 73 20 72 75 6e 20  Experiments run 
39904 6f 6e 20 72 65 61 6c 20 53 51 4c 69 74 65 20 64  on real SQLite d
39905 61 74 61 62 61 73 65 73 20 73 68 6f 77 20 74 68  atabases show th
39906 61 74 20 74 68 65 20 74 69 6d 65 20 6e 65 65 64  at the time need
39907 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f 20  ed.    ** to do 
39908 61 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 20  a binary search 
39909 74 6f 20 6c 6f 63 61 74 65 20 61 20 72 6f 77 20  to locate a row 
3990a 69 6e 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e  in a table or in
3990b 64 65 78 20 69 73 20 72 6f 75 67 68 6c 79 0a 20  dex is roughly. 
3990c 20 20 20 2a 2a 20 6c 6f 67 31 30 28 4e 29 20 74     ** log10(N) t
3990d 69 6d 65 73 20 74 68 65 20 74 69 6d 65 20 74 6f  imes the time to
3990e 20 6d 6f 76 65 20 66 72 6f 6d 20 6f 6e 65 20 72   move from one r
3990f 6f 77 20 74 6f 20 74 68 65 20 6e 65 78 74 20 72  ow to the next r
39910 6f 77 20 77 69 74 68 69 6e 0a 20 20 20 20 2a 2a  ow within.    **
39911 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65   a table or inde
39912 78 2e 20 20 54 68 65 20 61 63 74 75 61 6c 20 74  x.  The actual t
39913 69 6d 65 73 20 63 61 6e 20 76 61 72 79 2c 20 77  imes can vary, w
39914 69 74 68 20 74 68 65 20 73 69 7a 65 20 6f 66 0a  ith the size of.
39915 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 73 20 62      ** records b
39916 65 69 6e 67 20 61 6e 20 69 6d 70 6f 72 74 61 6e  eing an importan
39917 74 20 66 61 63 74 6f 72 2e 20 20 42 6f 74 68 20  t factor.  Both 
39918 6d 6f 76 65 73 20 61 6e 64 20 73 65 61 72 63 68  moves and search
39919 65 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 73 6c  es are.    ** sl
3991a 6f 77 65 72 20 77 69 74 68 20 6c 61 72 67 65 72  ower with larger
3991b 20 72 65 63 6f 72 64 73 2c 20 70 72 65 73 75 6d   records, presum
3991c 61 62 6c 79 20 62 65 63 61 75 73 65 20 66 65 77  ably because few
3991d 65 72 20 72 65 63 6f 72 64 73 20 66 69 74 0a 20  er records fit. 
3991e 20 20 20 2a 2a 20 6f 6e 20 6f 6e 65 20 70 61 67     ** on one pag
3991f 65 20 61 6e 64 20 68 65 6e 63 65 20 6d 6f 72 65  e and hence more
39920 20 70 61 67 65 73 20 68 61 76 65 20 74 6f 20 62   pages have to b
39921 65 20 66 65 74 63 68 65 64 2e 0a 20 20 20 20 2a  e fetched..    *
39922 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 41 4e 41  *.    ** The ANA
39923 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 20 61 6e 64  LYZE command and
39924 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
39925 31 20 61 6e 64 20 73 71 6c 69 74 65 5f 73 74 61  1 and sqlite_sta
39926 74 33 20 74 61 62 6c 65 73 20 64 6f 0a 20 20 20  t3 tables do.   
39927 20 2a 2a 20 6e 6f 74 20 67 69 76 65 20 75 73 20   ** not give us 
39928 64 61 74 61 20 6f 6e 20 74 68 65 20 72 65 6c 61  data on the rela
39929 74 69 76 65 20 73 69 7a 65 73 20 6f 66 20 74 61  tive sizes of ta
3992a 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 72 65  ble and index re
3992b 63 6f 72 64 73 2e 0a 20 20 20 20 2a 2a 20 53 6f  cords..    ** So
3992c 20 74 68 69 73 20 63 6f 6d 70 75 74 61 74 69 6f   this computatio
3992d 6e 20 61 73 73 75 6d 65 73 20 74 61 62 6c 65 20  n assumes table 
3992e 72 65 63 6f 72 64 73 20 61 72 65 20 61 62 6f 75  records are abou
3992f 74 20 74 77 69 63 65 20 61 73 20 62 69 67 0a 20  t twice as big. 
39930 20 20 20 2a 2a 20 61 73 20 69 6e 64 65 78 20 72     ** as index r
39931 65 63 6f 72 64 73 0a 20 20 20 20 2a 2f 0a 20 20  ecords.    */.  
39932 20 20 69 66 28 20 28 70 63 2e 70 6c 61 6e 2e 77    if( (pc.plan.w
39933 73 46 6c 61 67 73 26 7e 28 57 48 45 52 45 5f 52  sFlags&~(WHERE_R
39934 45 56 45 52 53 45 7c 57 48 45 52 45 5f 4f 52 44  EVERSE|WHERE_ORD
39935 45 52 45 44 29 29 3d 3d 57 48 45 52 45 5f 49 44  ERED))==WHERE_ID
39936 58 5f 4f 4e 4c 59 0a 20 20 20 20 20 26 26 20 28  X_ONLY.     && (
39937 70 57 43 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20  pWC->wctrlFlags 
39938 26 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f  & WHERE_ONEPASS_
39939 44 45 53 49 52 45 44 29 3d 3d 30 0a 20 20 20 20  DESIRED)==0.    
3993a 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   && sqlite3Globa
3993b 6c 43 6f 6e 66 69 67 2e 62 55 73 65 43 69 73 0a  lConfig.bUseCis.
3993c 20 20 20 20 20 26 26 20 4f 70 74 69 6d 69 7a 61       && Optimiza
3993d 74 69 6f 6e 45 6e 61 62 6c 65 64 28 70 50 61 72  tionEnabled(pPar
3993e 73 65 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 43  se->db, SQLITE_C
3993f 6f 76 65 72 49 64 78 53 63 61 6e 29 0a 20 20 20  overIdxScan).   
39940 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   ){.      /* Thi
39941 73 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 75  s index is not u
39942 73 65 66 75 6c 20 66 6f 72 20 69 6e 64 65 78 69  seful for indexi
39943 6e 67 2c 20 62 75 74 20 69 74 20 69 73 20 61 20  ng, but it is a 
39944 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 78 2e 0a  covering index..
39945 20 20 20 20 20 20 2a 2a 20 41 20 66 75 6c 6c 2d        ** A full-
39946 73 63 61 6e 20 6f 66 20 74 68 65 20 69 6e 64 65  scan of the inde
39947 78 20 6d 69 67 68 74 20 62 65 20 61 20 6c 69 74  x might be a lit
39948 74 6c 65 20 66 61 73 74 65 72 20 74 68 61 6e 20  tle faster than 
39949 61 20 66 75 6c 6c 2d 73 63 61 6e 0a 20 20 20 20  a full-scan.    
3994a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c    ** of the tabl
3994b 65 2c 20 73 6f 20 67 69 76 65 20 74 68 69 73 20  e, so give this 
3994c 63 61 73 65 20 61 20 63 6f 73 74 20 73 6c 69 67  case a cost slig
3994d 68 74 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 61  htly less than a
3994e 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20   table.      ** 
3994f 73 63 61 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 70  scan. */.      p
39950 63 2e 72 43 6f 73 74 20 3d 20 61 69 52 6f 77 45  c.rCost = aiRowE
39951 73 74 5b 30 5d 2a 33 20 2b 20 70 50 72 6f 62 65  st[0]*3 + pProbe
39952 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  ->nColumn;.     
39953 20 70 63 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73   pc.plan.wsFlags
39954 20 7c 3d 20 57 48 45 52 45 5f 43 4f 56 45 52 5f   |= WHERE_COVER_
39955 53 43 41 4e 7c 57 48 45 52 45 5f 43 4f 4c 55 4d  SCAN|WHERE_COLUM
39956 4e 5f 52 41 4e 47 45 3b 0a 20 20 20 20 7d 65 6c  N_RANGE;.    }el
39957 73 65 20 69 66 28 20 28 70 63 2e 70 6c 61 6e 2e  se if( (pc.plan.
39958 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
39959 4e 4f 54 5f 46 55 4c 4c 53 43 41 4e 29 3d 3d 30  NOT_FULLSCAN)==0
3995a 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65   ){.      /* The
3995b 20 63 6f 73 74 20 6f 66 20 61 20 66 75 6c 6c 20   cost of a full 
3995c 74 61 62 6c 65 20 73 63 61 6e 20 69 73 20 61 20  table scan is a 
3995d 6e 75 6d 62 65 72 20 6f 66 20 6d 6f 76 65 20 6f  number of move o
3995e 70 65 72 61 74 69 6f 6e 73 20 65 71 75 61 6c 0a  perations equal.
3995f 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20        ** to the 
39960 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69  number of rows i
39961 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20  n the table..   
39962 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 57     **.      ** W
39963 65 20 61 64 64 20 61 6e 20 61 64 64 69 74 69 6f  e add an additio
39964 6e 61 6c 20 34 78 20 70 65 6e 61 6c 74 79 20 74  nal 4x penalty t
39965 6f 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61  o full table sca
39966 6e 73 2e 20 20 54 68 69 73 20 63 61 75 73 65 73  ns.  This causes
39967 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 6f  .      ** the co
39968 73 74 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 65  st function to e
39969 72 72 20 6f 6e 20 74 68 65 20 73 69 64 65 20 6f  rr on the side o
3996a 66 20 63 68 6f 6f 73 69 6e 67 20 61 6e 20 69 6e  f choosing an in
3996b 64 65 78 20 6f 76 65 72 0a 20 20 20 20 20 20 2a  dex over.      *
3996c 2a 20 63 68 6f 6f 73 69 6e 67 20 61 20 66 75 6c  * choosing a ful
3996d 6c 20 73 63 61 6e 2e 20 20 54 68 69 73 20 34 78  l scan.  This 4x
3996e 20 66 75 6c 6c 2d 73 63 61 6e 20 70 65 6e 61 6c   full-scan penal
3996f 74 79 20 69 73 20 61 6e 20 61 72 67 75 61 62 6c  ty is an arguabl
39970 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 63 69 73  e.      ** decis
39971 69 6f 6e 20 61 6e 64 20 6f 6e 65 20 77 68 69 63  ion and one whic
39972 68 20 77 65 20 65 78 70 65 63 74 20 74 6f 20 72  h we expect to r
39973 65 76 69 73 69 74 20 69 6e 20 74 68 65 20 66 75  evisit in the fu
39974 74 75 72 65 2e 20 20 42 75 74 0a 20 20 20 20 20  ture.  But.     
39975 20 2a 2a 20 69 74 20 73 65 65 6d 73 20 74 6f 20   ** it seems to 
39976 62 65 20 77 6f 72 6b 69 6e 67 20 77 65 6c 6c 20  be working well 
39977 65 6e 6f 75 67 68 20 61 74 20 74 68 65 20 6d 6f  enough at the mo
39978 6d 65 6e 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ment..      */. 
39979 20 20 20 20 20 70 63 2e 72 43 6f 73 74 20 3d 20       pc.rCost = 
3997a 61 69 52 6f 77 45 73 74 5b 30 5d 2a 34 3b 0a 20  aiRowEst[0]*4;. 
3997b 20 20 20 20 20 70 63 2e 70 6c 61 6e 2e 77 73 46       pc.plan.wsF
3997c 6c 61 67 73 20 26 3d 20 7e 57 48 45 52 45 5f 49  lags &= ~WHERE_I
3997d 44 58 5f 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 69  DX_ONLY;.      i
3997e 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20  f( pIdx ){.     
3997f 20 20 20 70 63 2e 70 6c 61 6e 2e 77 73 46 6c 61     pc.plan.wsFla
39980 67 73 20 26 3d 20 7e 57 48 45 52 45 5f 4f 52 44  gs &= ~WHERE_ORD
39981 45 52 45 44 3b 0a 20 20 20 20 20 20 20 20 70 63  ERED;.        pc
39982 2e 70 6c 61 6e 2e 6e 4f 42 53 61 74 20 3d 20 6e  .plan.nOBSat = n
39983 50 72 69 6f 72 53 61 74 3b 0a 20 20 20 20 20 20  PriorSat;.      
39984 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
39985 20 20 20 6c 6f 67 31 30 4e 20 3d 20 65 73 74 4c     log10N = estL
39986 6f 67 28 61 69 52 6f 77 45 73 74 5b 30 5d 29 3b  og(aiRowEst[0]);
39987 0a 20 20 20 20 20 20 70 63 2e 72 43 6f 73 74 20  .      pc.rCost 
39988 3d 20 70 63 2e 70 6c 61 6e 2e 6e 52 6f 77 3b 0a  = pc.plan.nRow;.
39989 20 20 20 20 20 20 69 66 28 20 70 49 64 78 20 29        if( pIdx )
3998a 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 4c  {.        if( bL
3998b 6f 6f 6b 75 70 20 29 7b 0a 20 20 20 20 20 20 20  ookup ){.       
3998c 20 20 20 2f 2a 20 46 6f 72 20 61 6e 20 69 6e 64     /* For an ind
3998d 65 78 20 6c 6f 6f 6b 75 70 20 66 6f 6c 6c 6f 77  ex lookup follow
3998e 65 64 20 62 79 20 61 20 74 61 62 6c 65 20 6c 6f  ed by a table lo
3998f 6f 6b 75 70 3a 0a 20 20 20 20 20 20 20 20 20 20  okup:.          
39990 2a 2a 20 20 20 20 6e 49 6e 4d 75 6c 20 69 6e 64  **    nInMul ind
39991 65 78 20 73 65 61 72 63 68 65 73 20 74 6f 20 66  ex searches to f
39992 69 6e 64 20 74 68 65 20 73 74 61 72 74 20 6f 66  ind the start of
39993 20 65 61 63 68 20 69 6e 64 65 78 20 72 61 6e 67   each index rang
39994 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 20  e.          **  
39995 2b 20 6e 52 6f 77 20 73 74 65 70 73 20 74 68 72  + nRow steps thr
39996 6f 75 67 68 20 74 68 65 20 69 6e 64 65 78 0a 20  ough the index. 
39997 20 20 20 20 20 20 20 20 20 2a 2a 20 20 2b 20 6e           **  + n
39998 52 6f 77 20 74 61 62 6c 65 20 73 65 61 72 63 68  Row table search
39999 65 73 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65  es to lookup the
3999a 20 74 61 62 6c 65 20 65 6e 74 72 79 20 75 73 69   table entry usi
3999b 6e 67 20 74 68 65 20 72 6f 77 69 64 0a 20 20 20  ng the rowid.   
3999c 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
3999d 20 20 20 20 70 63 2e 72 43 6f 73 74 20 2b 3d 20      pc.rCost += 
3999e 28 6e 49 6e 4d 75 6c 20 2b 20 70 63 2e 70 6c 61  (nInMul + pc.pla
3999f 6e 2e 6e 52 6f 77 29 2a 6c 6f 67 31 30 4e 3b 0a  n.nRow)*log10N;.
399a0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
399a1 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
399a2 61 20 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 78  a covering index
399a3 3a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 20  :.          **  
399a4 20 20 20 6e 49 6e 4d 75 6c 20 69 6e 64 65 78 20     nInMul index 
399a5 73 65 61 72 63 68 65 73 20 74 6f 20 66 69 6e 64  searches to find
399a6 20 74 68 65 20 69 6e 69 74 69 61 6c 20 65 6e 74   the initial ent
399a7 72 79 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ry .          **
399a8 20 20 20 2b 20 6e 52 6f 77 20 73 74 65 70 73 20     + nRow steps 
399a9 74 68 72 6f 75 67 68 20 74 68 65 20 69 6e 64 65  through the inde
399aa 78 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  x.          */. 
399ab 20 20 20 20 20 20 20 20 20 70 63 2e 72 43 6f 73           pc.rCos
399ac 74 20 2b 3d 20 6e 49 6e 4d 75 6c 2a 6c 6f 67 31  t += nInMul*log1
399ad 30 4e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  0N;.        }.  
399ae 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
399af 20 20 20 2f 2a 20 46 6f 72 20 61 20 72 6f 77 69     /* For a rowi
399b0 64 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6c 6f  d primary key lo
399b1 6f 6b 75 70 3a 0a 20 20 20 20 20 20 20 20 2a 2a  okup:.        **
399b2 20 20 20 20 6e 49 6e 4d 75 6c 74 20 74 61 62 6c      nInMult tabl
399b3 65 20 73 65 61 72 63 68 65 73 20 74 6f 20 66 69  e searches to fi
399b4 6e 64 20 74 68 65 20 69 6e 69 74 69 61 6c 20 65  nd the initial e
399b5 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 72 61  ntry for each ra
399b6 6e 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 20  nge.        **  
399b7 2b 20 6e 52 6f 77 20 73 74 65 70 73 20 74 68 72  + nRow steps thr
399b8 6f 75 67 68 20 74 68 65 20 74 61 62 6c 65 0a 20  ough the table. 
399b9 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
399ba 20 20 70 63 2e 72 43 6f 73 74 20 2b 3d 20 6e 49    pc.rCost += nI
399bb 6e 4d 75 6c 2a 6c 6f 67 31 30 4e 3b 0a 20 20 20  nMul*log10N;.   
399bc 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
399bd 2f 2a 20 41 64 64 20 69 6e 20 74 68 65 20 65 73  /* Add in the es
399be 74 69 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20  timated cost of 
399bf 73 6f 72 74 69 6e 67 20 74 68 65 20 72 65 73 75  sorting the resu
399c0 6c 74 2e 20 20 41 63 74 75 61 6c 20 65 78 70 65  lt.  Actual expe
399c1 72 69 6d 65 6e 74 61 6c 0a 20 20 20 20 2a 2a 20  rimental.    ** 
399c2 6d 65 61 73 75 72 65 6d 65 6e 74 73 20 6f 66 20  measurements of 
399c3 73 6f 72 74 69 6e 67 20 70 65 72 66 6f 72 6d 61  sorting performa
399c4 6e 63 65 20 69 6e 20 53 51 4c 69 74 65 20 73 68  nce in SQLite sh
399c5 6f 77 20 74 68 61 74 20 73 6f 72 74 69 6e 67 20  ow that sorting 
399c6 74 69 6d 65 0a 20 20 20 20 2a 2a 20 61 64 64 73  time.    ** adds
399c7 20 43 2a 4e 2a 6c 6f 67 31 30 28 4e 29 20 74 6f   C*N*log10(N) to
399c8 20 74 68 65 20 63 6f 73 74 2c 20 77 68 65 72 65   the cost, where
399c9 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72   N is the number
399ca 20 6f 66 20 72 6f 77 73 20 74 6f 20 62 65 20 0a   of rows to be .
399cb 20 20 20 20 2a 2a 20 73 6f 72 74 65 64 20 61 6e      ** sorted an
399cc 64 20 43 20 69 73 20 61 20 66 61 63 74 6f 72 20  d C is a factor 
399cd 62 65 74 77 65 65 6e 20 31 2e 39 35 20 61 6e 64  between 1.95 and
399ce 20 34 2e 33 2e 20 20 57 65 20 77 69 6c 6c 20 73   4.3.  We will s
399cf 70 6c 69 74 20 74 68 65 0a 20 20 20 20 2a 2a 20  plit the.    ** 
399d0 64 69 66 66 65 72 65 6e 63 65 20 61 6e 64 20 73  difference and s
399d1 65 6c 65 63 74 20 43 20 6f 66 20 33 2e 30 2e 0a  elect C of 3.0..
399d2 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62      */.    if( b
399d3 53 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 64 6f  Sort ){.      do
399d4 75 62 6c 65 20 6d 20 3d 20 65 73 74 4c 6f 67 28  uble m = estLog(
399d5 70 63 2e 70 6c 61 6e 2e 6e 52 6f 77 2a 28 6e 4f  pc.plan.nRow*(nO
399d6 72 64 65 72 42 79 20 2d 20 70 63 2e 70 6c 61 6e  rderBy - pc.plan
399d7 2e 6e 4f 42 53 61 74 29 2f 6e 4f 72 64 65 72 42  .nOBSat)/nOrderB
399d8 79 29 3b 0a 20 20 20 20 20 20 6d 20 2a 3d 20 28  y);.      m *= (
399d9 64 6f 75 62 6c 65 29 28 70 63 2e 70 6c 61 6e 2e  double)(pc.plan.
399da 6e 4f 42 53 61 74 20 3f 20 32 20 3a 20 33 29 3b  nOBSat ? 2 : 3);
399db 0a 20 20 20 20 20 20 70 63 2e 72 43 6f 73 74 20  .      pc.rCost 
399dc 2b 3d 20 70 63 2e 70 6c 61 6e 2e 6e 52 6f 77 2a  += pc.plan.nRow*
399dd 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  m;.    }.    if(
399de 20 62 44 69 73 74 20 29 7b 0a 20 20 20 20 20 20   bDist ){.      
399df 70 63 2e 72 43 6f 73 74 20 2b 3d 20 70 63 2e 70  pc.rCost += pc.p
399e0 6c 61 6e 2e 6e 52 6f 77 2a 65 73 74 4c 6f 67 28  lan.nRow*estLog(
399e1 70 63 2e 70 6c 61 6e 2e 6e 52 6f 77 29 2a 33 3b  pc.plan.nRow)*3;
399e2 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 2a 2a  .    }..    /***
399e3 2a 20 43 6f 73 74 20 6f 66 20 75 73 69 6e 67 20  * Cost of using 
399e4 74 68 69 73 20 69 6e 64 65 78 20 68 61 73 20 6e  this index has n
399e5 6f 77 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64  ow been computed
399e6 20 2a 2a 2a 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49   ****/..    /* I
399e7 66 20 74 68 65 72 65 20 61 72 65 20 61 64 64 69  f there are addi
399e8 74 69 6f 6e 61 6c 20 63 6f 6e 73 74 72 61 69 6e  tional constrain
399e9 74 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65  ts on this table
399ea 20 74 68 61 74 20 63 61 6e 6e 6f 74 0a 20 20 20   that cannot.   
399eb 20 2a 2a 20 62 65 20 75 73 65 64 20 77 69 74 68   ** be used with
399ec 20 74 68 65 20 63 75 72 72 65 6e 74 20 69 6e 64   the current ind
399ed 65 78 2c 20 62 75 74 20 77 68 69 63 68 20 6d 69  ex, but which mi
399ee 67 68 74 20 6c 6f 77 65 72 20 74 68 65 20 6e 75  ght lower the nu
399ef 6d 62 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 6f  mber.    ** of o
399f0 75 74 70 75 74 20 72 6f 77 73 2c 20 61 64 6a 75  utput rows, adju
399f1 73 74 20 74 68 65 20 6e 52 6f 77 20 76 61 6c 75  st the nRow valu
399f2 65 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 20 20  e accordingly.  
399f3 54 68 69 73 20 6f 6e 6c 79 20 0a 20 20 20 20 2a  This only .    *
399f4 2a 20 6d 61 74 74 65 72 73 20 69 66 20 74 68 65  * matters if the
399f5 20 63 75 72 72 65 6e 74 20 69 6e 64 65 78 20 69   current index i
399f6 73 20 74 68 65 20 6c 65 61 73 74 20 63 6f 73 74  s the least cost
399f7 6c 79 2c 20 73 6f 20 64 6f 20 6e 6f 74 20 62 6f  ly, so do not bo
399f8 74 68 65 72 0a 20 20 20 20 2a 2a 20 77 69 74 68  ther.    ** with
399f9 20 74 68 69 73 20 73 74 65 70 20 69 66 20 77 65   this step if we
399fa 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 68   already know th
399fb 69 73 20 69 6e 64 65 78 20 77 69 6c 6c 20 6e 6f  is index will no
399fc 74 20 62 65 20 63 68 6f 73 65 6e 2e 0a 20 20 20  t be chosen..   
399fd 20 2a 2a 20 41 6c 73 6f 2c 20 6e 65 76 65 72 20   ** Also, never 
399fe 72 65 64 75 63 65 20 74 68 65 20 6f 75 74 70 75  reduce the outpu
399ff 74 20 72 6f 77 20 63 6f 75 6e 74 20 62 65 6c 6f  t row count belo
39a00 77 20 32 20 75 73 69 6e 67 20 74 68 69 73 20 73  w 2 using this s
39a01 74 65 70 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  tep..    **.    
39a02 2a 2a 20 49 74 20 69 73 20 63 72 69 74 69 63 61  ** It is critica
39a03 6c 20 74 68 61 74 20 74 68 65 20 6e 6f 74 56 61  l that the notVa
39a04 6c 69 64 20 6d 61 73 6b 20 62 65 20 75 73 65 64  lid mask be used
39a05 20 68 65 72 65 20 69 6e 73 74 65 61 64 20 6f 66   here instead of
39a06 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e 6f 74 52  .    ** the notR
39a07 65 61 64 79 20 6d 61 73 6b 2e 20 20 57 68 65 6e  eady mask.  When
39a08 20 63 6f 6d 70 75 74 69 6e 67 20 61 6e 20 22 6f   computing an "o
39a09 70 74 69 6d 61 6c 22 20 69 6e 64 65 78 2c 20 74  ptimal" index, t
39a0a 68 65 20 6e 6f 74 52 65 61 64 79 0a 20 20 20 20  he notReady.    
39a0b 2a 2a 20 6d 61 73 6b 20 77 69 6c 6c 20 6f 6e 6c  ** mask will onl
39a0c 79 20 68 61 76 65 20 6f 6e 65 20 62 69 74 20 73  y have one bit s
39a0d 65 74 20 2d 20 74 68 65 20 62 69 74 20 66 6f 72  et - the bit for
39a0e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62   the current tab
39a0f 6c 65 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 6e  le..    ** The n
39a10 6f 74 56 61 6c 69 64 20 6d 61 73 6b 2c 20 6f 6e  otValid mask, on
39a11 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c   the other hand,
39a12 20 61 6c 77 61 79 73 20 68 61 73 20 61 6c 6c 20   always has all 
39a13 62 69 74 73 20 73 65 74 20 66 6f 72 0a 20 20 20  bits set for.   
39a14 20 2a 2a 20 74 61 62 6c 65 73 20 74 68 61 74 20   ** tables that 
39a15 61 72 65 20 6e 6f 74 20 69 6e 20 6f 75 74 65 72  are not in outer
39a16 20 6c 6f 6f 70 73 2e 20 20 49 66 20 6e 6f 74 52   loops.  If notR
39a17 65 61 64 79 20 69 73 20 75 73 65 64 20 68 65 72  eady is used her
39a18 65 20 69 6e 73 74 65 61 64 0a 20 20 20 20 2a 2a  e instead.    **
39a19 20 6f 66 20 6e 6f 74 56 61 6c 69 64 2c 20 74 68   of notValid, th
39a1a 65 6e 20 61 20 6f 70 74 69 6d 61 6c 20 69 6e 64  en a optimal ind
39a1b 65 78 20 74 68 61 74 20 64 65 70 65 6e 64 73 20  ex that depends 
39a1c 6f 6e 20 69 6e 6e 65 72 20 6a 6f 69 6e 73 20 6c  on inner joins l
39a1d 6f 6f 70 73 0a 20 20 20 20 2a 2a 20 6d 69 67 68  oops.    ** migh
39a1e 74 20 62 65 20 73 65 6c 65 63 74 65 64 20 65 76  t be selected ev
39a1f 65 6e 20 77 68 65 6e 20 74 68 65 72 65 20 65 78  en when there ex
39a20 69 73 74 73 20 61 6e 20 6f 70 74 69 6d 61 6c 20  ists an optimal 
39a21 69 6e 64 65 78 20 74 68 61 74 20 68 61 73 0a 20  index that has. 
39a22 20 20 20 2a 2a 20 6e 6f 20 73 75 63 68 20 64 65     ** no such de
39a23 70 65 6e 64 65 6e 63 79 2e 0a 20 20 20 20 2a 2f  pendency..    */
39a24 0a 20 20 20 20 69 66 28 20 70 63 2e 70 6c 61 6e  .    if( pc.plan
39a25 2e 6e 52 6f 77 3e 32 20 26 26 20 70 63 2e 72 43  .nRow>2 && pc.rC
39a26 6f 73 74 3c 3d 70 2d 3e 63 6f 73 74 2e 72 43 6f  ost<=p->cost.rCo
39a27 73 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  st ){.      int 
39a28 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
39a29 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
39a2a 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
39a2b 20 20 69 6e 74 20 6e 53 6b 69 70 45 71 20 3d 20    int nSkipEq = 
39a2c 70 63 2e 70 6c 61 6e 2e 6e 45 71 3b 20 20 20 2f  pc.plan.nEq;   /
39a2d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 63  * Number of == c
39a2e 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 73 6b  onstraints to sk
39a2f 69 70 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ip */.      int 
39a30 6e 53 6b 69 70 52 61 6e 67 65 20 3d 20 6e 42 6f  nSkipRange = nBo
39a31 75 6e 64 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62  und;     /* Numb
39a32 65 72 20 6f 66 20 3c 20 63 6f 6e 73 74 72 61 69  er of < constrai
39a33 6e 74 73 20 74 6f 20 73 6b 69 70 20 2a 2f 0a 20  nts to skip */. 
39a34 20 20 20 20 20 42 69 74 6d 61 73 6b 20 74 68 69       Bitmask thi
39a35 73 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  sTab;           
39a36 20 20 2f 2a 20 42 69 74 6d 61 70 20 66 6f 72 20    /* Bitmap for 
39a37 70 53 72 63 20 2a 2f 0a 0a 20 20 20 20 20 20 74  pSrc */..      t
39a38 68 69 73 54 61 62 20 3d 20 67 65 74 4d 61 73 6b  hisTab = getMask
39a39 28 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20  (pWC->pMaskSet, 
39a3a 69 43 75 72 29 3b 0a 20 20 20 20 20 20 66 6f 72  iCur);.      for
39a3b 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6b  (pTerm=pWC->a, k
39a3c 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 70 63 2e  =pWC->nTerm; pc.
39a3d 70 6c 61 6e 2e 6e 52 6f 77 3e 32 20 26 26 20 6b  plan.nRow>2 && k
39a3e 3b 20 6b 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b  ; k--, pTerm++){
39a3f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 65  .        if( pTe
39a40 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
39a41 52 4d 5f 56 49 52 54 55 41 4c 20 29 20 63 6f 6e  RM_VIRTUAL ) con
39a42 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69  tinue;.        i
39a43 66 28 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65  f( (pTerm->prere
39a44 71 41 6c 6c 20 26 20 70 2d 3e 6e 6f 74 56 61 6c  qAll & p->notVal
39a45 69 64 29 21 3d 74 68 69 73 54 61 62 20 29 20 63  id)!=thisTab ) c
39a46 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
39a47 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65   if( pTerm->eOpe
39a48 72 61 74 6f 72 20 26 20 28 57 4f 5f 45 51 7c 57  rator & (WO_EQ|W
39a49 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 29 20  O_IN|WO_ISNULL) 
39a4a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
39a4b 20 6e 53 6b 69 70 45 71 20 29 7b 0a 20 20 20 20   nSkipEq ){.    
39a4c 20 20 20 20 20 20 20 20 2f 2a 20 49 67 6e 6f 72          /* Ignor
39a4d 65 20 74 68 65 20 66 69 72 73 74 20 70 63 2e 70  e the first pc.p
39a4e 6c 61 6e 2e 6e 45 71 20 65 71 75 61 6c 69 74 79  lan.nEq equality
39a4f 20 6d 61 74 63 68 65 73 20 73 69 6e 63 65 20 74   matches since t
39a50 68 65 20 69 6e 64 65 78 0a 20 20 20 20 20 20 20  he index.       
39a51 20 20 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65       ** has alre
39a52 61 64 79 20 61 63 63 6f 75 6e 74 65 64 20 66 6f  ady accounted fo
39a53 72 20 74 68 65 73 65 20 2a 2f 0a 20 20 20 20 20  r these */.     
39a54 20 20 20 20 20 20 20 6e 53 6b 69 70 45 71 2d 2d         nSkipEq--
39a55 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
39a56 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  e{.            /
39a57 2a 20 41 73 73 75 6d 65 20 65 61 63 68 20 61 64  * Assume each ad
39a58 64 69 74 69 6f 6e 61 6c 20 65 71 75 61 6c 69 74  ditional equalit
39a59 79 20 6d 61 74 63 68 20 72 65 64 75 63 65 73 20  y match reduces 
39a5a 74 68 65 20 72 65 73 75 6c 74 0a 20 20 20 20 20  the result.     
39a5b 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 73 69         ** set si
39a5c 7a 65 20 62 79 20 61 20 66 61 63 74 6f 72 20 6f  ze by a factor o
39a5d 66 20 31 30 20 2a 2f 0a 20 20 20 20 20 20 20 20  f 10 */.        
39a5e 20 20 20 20 70 63 2e 70 6c 61 6e 2e 6e 52 6f 77      pc.plan.nRow
39a5f 20 2f 3d 20 31 30 3b 0a 20 20 20 20 20 20 20 20   /= 10;.        
39a60 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73    }.        }els
39a61 65 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70  e if( pTerm->eOp
39a62 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 4c 54 7c  erator & (WO_LT|
39a63 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47  WO_LE|WO_GT|WO_G
39a64 45 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  E) ){.          
39a65 69 66 28 20 6e 53 6b 69 70 52 61 6e 67 65 20 29  if( nSkipRange )
39a66 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  {.            /*
39a67 20 49 67 6e 6f 72 65 20 74 68 65 20 66 69 72 73   Ignore the firs
39a68 74 20 6e 53 6b 69 70 52 61 6e 67 65 20 72 61 6e  t nSkipRange ran
39a69 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 73  ge constraints s
39a6a 69 6e 63 65 20 74 68 65 20 69 6e 64 65 78 0a 20  ince the index. 
39a6b 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61             ** ha
39a6c 73 20 61 6c 72 65 61 64 79 20 61 63 63 6f 75 6e  s already accoun
39a6d 74 65 64 20 66 6f 72 20 74 68 65 73 65 20 2a 2f  ted for these */
39a6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 53 6b  .            nSk
39a6f 69 70 52 61 6e 67 65 2d 2d 3b 0a 20 20 20 20 20  ipRange--;.     
39a70 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
39a71 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 75 6d          /* Assum
39a72 65 20 65 61 63 68 20 61 64 64 69 74 69 6f 6e 61  e each additiona
39a73 6c 20 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69  l range constrai
39a74 6e 74 20 72 65 64 75 63 65 73 20 74 68 65 20 72  nt reduces the r
39a75 65 73 75 6c 74 0a 20 20 20 20 20 20 20 20 20 20  esult.          
39a76 20 20 2a 2a 20 73 65 74 20 73 69 7a 65 20 62 79    ** set size by
39a77 20 61 20 66 61 63 74 6f 72 20 6f 66 20 33 2e 20   a factor of 3. 
39a78 20 49 6e 64 65 78 65 64 20 72 61 6e 67 65 20 63   Indexed range c
39a79 6f 6e 73 74 72 61 69 6e 74 73 20 72 65 64 75 63  onstraints reduc
39a7a 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  e.            **
39a7b 20 74 68 65 20 73 65 61 72 63 68 20 73 70 61 63   the search spac
39a7c 65 20 62 79 20 61 20 6c 61 72 67 65 72 20 66 61  e by a larger fa
39a7d 63 74 6f 72 3a 20 34 2e 20 20 57 65 20 6d 61 6b  ctor: 4.  We mak
39a7e 65 20 69 6e 64 65 78 65 64 20 72 61 6e 67 65 0a  e indexed range.
39a7f 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6d              ** m
39a80 6f 72 65 20 73 65 6c 65 63 74 69 76 65 20 69 6e  ore selective in
39a81 74 65 6e 74 69 6f 6e 61 6c 6c 79 20 62 65 63 61  tentionally beca
39a82 75 73 65 20 6f 66 20 74 68 65 20 73 75 62 6a 65  use of the subje
39a83 63 74 69 76 65 20 0a 20 20 20 20 20 20 20 20 20  ctive .         
39a84 20 20 20 2a 2a 20 6f 62 73 65 72 76 61 74 69 6f     ** observatio
39a85 6e 20 74 68 61 74 20 69 6e 64 65 78 65 64 20 72  n that indexed r
39a86 61 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 73  ange constraints
39a87 20 72 65 61 6c 6c 79 20 61 72 65 20 6d 6f 72 65   really are more
39a88 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
39a89 73 65 6c 65 63 74 69 76 65 20 69 6e 20 70 72 61  selective in pra
39a8a 63 74 69 63 65 2c 20 6f 6e 20 61 76 65 72 61 67  ctice, on averag
39a8b 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  e. */.          
39a8c 20 20 70 63 2e 70 6c 61 6e 2e 6e 52 6f 77 20 2f    pc.plan.nRow /
39a8d 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 3;.          }
39a8e 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
39a8f 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  f( pTerm->eOpera
39a90 74 6f 72 21 3d 57 4f 5f 4e 4f 4f 50 20 29 7b 0a  tor!=WO_NOOP ){.
39a91 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 79            /* Any
39a92 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f   other expressio
39a93 6e 20 6c 6f 77 65 72 73 20 74 68 65 20 6f 75 74  n lowers the out
39a94 70 75 74 20 72 6f 77 20 63 6f 75 6e 74 20 62 79  put row count by
39a95 20 68 61 6c 66 20 2a 2f 0a 20 20 20 20 20 20 20   half */.       
39a96 20 20 20 70 63 2e 70 6c 61 6e 2e 6e 52 6f 77 20     pc.plan.nRow 
39a97 2f 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a  /= 2;.        }.
39a98 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
39a99 28 20 70 63 2e 70 6c 61 6e 2e 6e 52 6f 77 3c 32  ( pc.plan.nRow<2
39a9a 20 29 20 70 63 2e 70 6c 61 6e 2e 6e 52 6f 77 20   ) pc.plan.nRow 
39a9b 3d 20 32 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20 20  = 2;.    }...   
39a9c 20 57 48 45 52 45 54 52 41 43 45 28 28 0a 20 20   WHERETRACE((.  
39a9d 20 20 20 20 22 20 20 20 20 20 20 6e 45 71 3d 25      "      nEq=%
39a9e 64 20 6e 49 6e 4d 75 6c 3d 25 64 20 72 61 6e 67  d nInMul=%d rang
39a9f 65 44 69 76 3d 25 64 20 62 53 6f 72 74 3d 25 64  eDiv=%d bSort=%d
39aa0 20 62 4c 6f 6f 6b 75 70 3d 25 64 20 77 73 46 6c   bLookup=%d wsFl
39aa1 61 67 73 3d 30 78 25 30 38 78 5c 6e 22 0a 20 20  ags=0x%08x\n".  
39aa2 20 20 20 20 22 20 20 20 20 20 20 6e 6f 74 52 65      "      notRe
39aa3 61 64 79 3d 30 78 25 6c 6c 78 20 6c 6f 67 31 30  ady=0x%llx log10
39aa4 4e 3d 25 2e 31 66 20 6e 52 6f 77 3d 25 2e 31 66  N=%.1f nRow=%.1f
39aa5 20 63 6f 73 74 3d 25 2e 31 66 5c 6e 22 0a 20 20   cost=%.1f\n".  
39aa6 20 20 20 20 22 20 20 20 20 20 20 75 73 65 64 3d      "      used=
39aa7 30 78 25 6c 6c 78 20 6e 4f 42 53 61 74 3d 25 64  0x%llx nOBSat=%d
39aa8 5c 6e 22 2c 0a 20 20 20 20 20 20 70 63 2e 70 6c  \n",.      pc.pl
39aa9 61 6e 2e 6e 45 71 2c 20 6e 49 6e 4d 75 6c 2c 20  an.nEq, nInMul, 
39aaa 28 69 6e 74 29 72 61 6e 67 65 44 69 76 2c 20 62  (int)rangeDiv, b
39aab 53 6f 72 74 2c 20 62 4c 6f 6f 6b 75 70 2c 20 70  Sort, bLookup, p
39aac 63 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 2c 0a  c.plan.wsFlags,.
39aad 20 20 20 20 20 20 70 2d 3e 6e 6f 74 52 65 61 64        p->notRead
39aae 79 2c 20 6c 6f 67 31 30 4e 2c 20 70 63 2e 70 6c  y, log10N, pc.pl
39aaf 61 6e 2e 6e 52 6f 77 2c 20 70 63 2e 72 43 6f 73  an.nRow, pc.rCos
39ab0 74 2c 20 70 63 2e 75 73 65 64 2c 0a 20 20 20 20  t, pc.used,.    
39ab1 20 20 70 63 2e 70 6c 61 6e 2e 6e 4f 42 53 61 74    pc.plan.nOBSat
39ab2 0a 20 20 20 20 29 29 3b 0a 0a 20 20 20 20 2f 2a  .    ));..    /*
39ab3 20 49 66 20 74 68 69 73 20 69 6e 64 65 78 20 69   If this index i
39ab4 73 20 74 68 65 20 62 65 73 74 20 77 65 20 68 61  s the best we ha
39ab5 76 65 20 73 65 65 6e 20 73 6f 20 66 61 72 2c 20  ve seen so far, 
39ab6 74 68 65 6e 20 72 65 63 6f 72 64 20 74 68 69 73  then record this
39ab7 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 61 6e  .    ** index an
39ab8 64 20 69 74 73 20 63 6f 73 74 20 69 6e 20 74 68  d its cost in th
39ab9 65 20 70 2d 3e 63 6f 73 74 20 73 74 72 75 63 74  e p->cost struct
39aba 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ure..    */.    
39abb 69 66 28 20 28 21 70 49 64 78 20 7c 7c 20 70 63  if( (!pIdx || pc
39abc 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 29 20 26  .plan.wsFlags) &
39abd 26 20 63 6f 6d 70 61 72 65 43 6f 73 74 28 26 70  & compareCost(&p
39abe 63 2c 20 26 70 2d 3e 63 6f 73 74 29 20 29 7b 0a  c, &p->cost) ){.
39abf 20 20 20 20 20 20 70 2d 3e 63 6f 73 74 20 3d 20        p->cost = 
39ac0 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e 63 6f 73  pc;.      p->cos
39ac1 74 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  t.plan.wsFlags &
39ac2 3d 20 77 73 46 6c 61 67 4d 61 73 6b 3b 0a 20 20  = wsFlagMask;.  
39ac3 20 20 20 20 70 2d 3e 63 6f 73 74 2e 70 6c 61 6e      p->cost.plan
39ac4 2e 75 2e 70 49 64 78 20 3d 20 70 49 64 78 3b 0a  .u.pIdx = pIdx;.
39ac5 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
39ac6 20 74 68 65 72 65 20 77 61 73 20 61 6e 20 49 4e   there was an IN
39ac7 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 2c  DEXED BY clause,
39ac8 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 61 74 20   then only that 
39ac9 6f 6e 65 20 69 6e 64 65 78 20 69 73 0a 20 20 20  one index is.   
39aca 20 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 2e 20   ** considered. 
39acb 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d  */.    if( pSrc-
39acc 3e 70 49 6e 64 65 78 20 29 20 62 72 65 61 6b 3b  >pIndex ) break;
39acd 0a 0a 20 20 20 20 2f 2a 20 52 65 73 65 74 20 6d  ..    /* Reset m
39ace 61 73 6b 73 20 66 6f 72 20 74 68 65 20 6e 65 78  asks for the nex
39acf 74 20 69 6e 64 65 78 20 69 6e 20 74 68 65 20 6c  t index in the l
39ad0 6f 6f 70 20 2a 2f 0a 20 20 20 20 77 73 46 6c 61  oop */.    wsFla
39ad1 67 4d 61 73 6b 20 3d 20 7e 28 57 48 45 52 45 5f  gMask = ~(WHERE_
39ad2 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52  ROWID_EQ|WHERE_R
39ad3 4f 57 49 44 5f 52 41 4e 47 45 29 3b 0a 20 20 20  OWID_RANGE);.   
39ad4 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20 69 64   eqTermMask = id
39ad5 78 45 71 54 65 72 6d 4d 61 73 6b 3b 0a 20 20 7d  xEqTermMask;.  }
39ad6 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
39ad7 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20 63  is no ORDER BY c
39ad8 6c 61 75 73 65 20 61 6e 64 20 74 68 65 20 53 51  lause and the SQ
39ad9 4c 49 54 45 5f 52 65 76 65 72 73 65 4f 72 64 65  LITE_ReverseOrde
39ada 72 20 66 6c 61 67 0a 20 20 2a 2a 20 69 73 20 73  r flag.  ** is s
39adb 65 74 2c 20 74 68 65 6e 20 72 65 76 65 72 73 65  et, then reverse
39adc 20 74 68 65 20 6f 72 64 65 72 20 74 68 61 74 20   the order that 
39add 74 68 65 20 69 6e 64 65 78 20 77 69 6c 6c 20 62  the index will b
39ade 65 20 73 63 61 6e 6e 65 64 0a 20 20 2a 2a 20 69  e scanned.  ** i
39adf 6e 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  n. This is used 
39ae0 66 6f 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  for application 
39ae1 74 65 73 74 69 6e 67 2c 20 74 6f 20 68 65 6c 70  testing, to help
39ae2 20 66 69 6e 64 20 63 61 73 65 73 0a 20 20 2a 2a   find cases.  **
39ae3 20 77 68 65 72 65 20 61 70 70 6c 69 63 61 74 69   where applicati
39ae4 6f 6e 20 62 65 68 61 76 69 6f 75 72 20 64 65 70  on behaviour dep
39ae5 65 6e 64 73 20 6f 6e 20 74 68 65 20 28 75 6e 64  ends on the (und
39ae6 65 66 69 6e 65 64 29 20 6f 72 64 65 72 20 74 68  efined) order th
39ae7 61 74 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 6f  at.  ** SQLite o
39ae8 75 74 70 75 74 73 20 72 6f 77 73 20 69 6e 20 69  utputs rows in i
39ae9 6e 20 74 68 65 20 61 62 73 65 6e 63 65 20 6f 66  n the absence of
39aea 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   an ORDER BY cla
39aeb 75 73 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 21  use.  */.  if( !
39aec 70 2d 3e 70 4f 72 64 65 72 42 79 20 26 26 20 70  p->pOrderBy && p
39aed 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73  Parse->db->flags
39aee 20 26 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73   & SQLITE_Revers
39aef 65 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 70 2d  eOrder ){.    p-
39af0 3e 63 6f 73 74 2e 70 6c 61 6e 2e 77 73 46 6c 61  >cost.plan.wsFla
39af1 67 73 20 7c 3d 20 57 48 45 52 45 5f 52 45 56 45  gs |= WHERE_REVE
39af2 52 53 45 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  RSE;.  }..  asse
39af3 72 74 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20  rt( p->pOrderBy 
39af4 7c 7c 20 28 70 2d 3e 63 6f 73 74 2e 70 6c 61 6e  || (p->cost.plan
39af5 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 4f  .wsFlags&WHERE_O
39af6 52 44 45 52 45 44 29 3d 3d 30 20 29 3b 0a 20 20  RDERED)==0 );.  
39af7 61 73 73 65 72 74 28 20 70 2d 3e 63 6f 73 74 2e  assert( p->cost.
39af8 70 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d 30 20 7c  plan.u.pIdx==0 |
39af9 7c 20 28 70 2d 3e 63 6f 73 74 2e 70 6c 61 6e 2e  | (p->cost.plan.
39afa 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 52 4f  wsFlags&WHERE_RO
39afb 57 49 44 5f 45 51 29 3d 3d 30 20 29 3b 0a 20 20  WID_EQ)==0 );.  
39afc 61 73 73 65 72 74 28 20 70 53 72 63 2d 3e 70 49  assert( pSrc->pI
39afd 6e 64 65 78 3d 3d 30 20 0a 20 20 20 20 20 20 20  ndex==0 .       
39afe 7c 7c 20 70 2d 3e 63 6f 73 74 2e 70 6c 61 6e 2e  || p->cost.plan.
39aff 75 2e 70 49 64 78 3d 3d 30 20 0a 20 20 20 20 20  u.pIdx==0 .     
39b00 20 20 7c 7c 20 70 2d 3e 63 6f 73 74 2e 70 6c 61    || p->cost.pla
39b01 6e 2e 75 2e 70 49 64 78 3d 3d 70 53 72 63 2d 3e  n.u.pIdx==pSrc->
39b02 70 49 6e 64 65 78 20 0a 20 20 29 3b 0a 0a 20 20  pIndex .  );..  
39b03 57 48 45 52 45 54 52 41 43 45 28 28 22 20 20 20  WHERETRACE(("   
39b04 62 65 73 74 20 69 6e 64 65 78 20 69 73 3a 20 25  best index is: %
39b05 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 70  s\n",.         p
39b06 2d 3e 63 6f 73 74 2e 70 6c 61 6e 2e 75 2e 70 49  ->cost.plan.u.pI
39b07 64 78 20 3f 20 70 2d 3e 63 6f 73 74 2e 70 6c 61  dx ? p->cost.pla
39b08 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 20  n.u.pIdx->zName 
39b09 3a 20 22 69 70 6b 22 29 29 3b 0a 20 20 0a 20 20  : "ipk"));.  .  
39b0a 62 65 73 74 4f 72 43 6c 61 75 73 65 49 6e 64 65  bestOrClauseInde
39b0b 78 28 70 29 3b 0a 20 20 62 65 73 74 41 75 74 6f  x(p);.  bestAuto
39b0c 6d 61 74 69 63 49 6e 64 65 78 28 70 29 3b 0a 20  maticIndex(p);. 
39b0d 20 70 2d 3e 63 6f 73 74 2e 70 6c 61 6e 2e 77 73   p->cost.plan.ws
39b0e 46 6c 61 67 73 20 7c 3d 20 65 71 54 65 72 6d 4d  Flags |= eqTermM
39b0f 61 73 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69  ask;.}../*.** Fi
39b10 6e 64 20 74 68 65 20 71 75 65 72 79 20 70 6c 61  nd the query pla
39b11 6e 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  n for accessing 
39b12 74 61 62 6c 65 20 70 53 72 63 2d 3e 70 54 61 62  table pSrc->pTab
39b13 2e 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 62  . Write the.** b
39b14 65 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20 61  est query plan a
39b15 6e 64 20 69 74 73 20 63 6f 73 74 20 69 6e 74 6f  nd its cost into
39b16 20 74 68 65 20 57 68 65 72 65 43 6f 73 74 20 6f   the WhereCost o
39b17 62 6a 65 63 74 20 73 75 70 70 6c 69 65 64 20 0a  bject supplied .
39b18 2a 2a 20 61 73 20 74 68 65 20 6c 61 73 74 20 70  ** as the last p
39b19 61 72 61 6d 65 74 65 72 2e 20 54 68 69 73 20 66  arameter. This f
39b1a 75 6e 63 74 69 6f 6e 20 6d 61 79 20 63 61 6c 63  unction may calc
39b1b 75 6c 61 74 65 20 74 68 65 20 63 6f 73 74 20 6f  ulate the cost o
39b1c 66 0a 2a 2a 20 62 6f 74 68 20 72 65 61 6c 20 61  f.** both real a
39b1d 6e 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  nd virtual table
39b1e 20 73 63 61 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   scans..**.** Th
39b1f 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  is function does
39b20 20 6e 6f 74 20 74 61 6b 65 20 4f 52 44 45 52 20   not take ORDER 
39b21 42 59 20 6f 72 20 44 49 53 54 49 4e 43 54 20 69  BY or DISTINCT i
39b22 6e 74 6f 20 61 63 63 6f 75 6e 74 2e 20 20 4e 6f  nto account.  No
39b23 72 0a 2a 2a 20 64 6f 65 73 20 69 74 20 72 65 6d  r.** does it rem
39b24 65 6d 62 65 72 20 74 68 65 20 76 69 72 74 75 61  ember the virtua
39b25 6c 20 74 61 62 6c 65 20 71 75 65 72 79 20 70 6c  l table query pl
39b26 61 6e 2e 20 20 41 6c 6c 20 69 74 20 64 6f 65 73  an.  All it does
39b27 20 69 73 20 63 6f 6d 70 75 74 65 0a 2a 2a 20 74   is compute.** t
39b28 68 65 20 63 6f 73 74 20 77 68 69 6c 65 20 64 65  he cost while de
39b29 74 65 72 6d 69 6e 69 6e 67 20 69 66 20 61 6e 20  termining if an 
39b2a 4f 52 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  OR optimization 
39b2b 69 73 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20  is applicable.  
39b2c 54 68 65 0a 2a 2a 20 64 65 74 61 69 6c 73 20 77  The.** details w
39b2d 69 6c 6c 20 62 65 20 72 65 63 6f 6e 73 69 64 65  ill be reconside
39b2e 72 65 64 20 6c 61 74 65 72 20 69 66 20 74 68 65  red later if the
39b2f 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
39b30 20 66 6f 75 6e 64 20 74 6f 20 62 65 0a 2a 2a 20   found to be.** 
39b31 61 70 70 6c 69 63 61 62 6c 65 2e 0a 2a 2f 0a 73  applicable..*/.s
39b32 74 61 74 69 63 20 76 6f 69 64 20 62 65 73 74 49  tatic void bestI
39b33 6e 64 65 78 28 57 68 65 72 65 42 65 73 74 49 64  ndex(WhereBestId
39b34 78 20 2a 70 29 7b 0a 23 69 66 6e 64 65 66 20 53  x *p){.#ifndef S
39b35 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
39b36 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 49 73  ALTABLE.  if( Is
39b37 56 69 72 74 75 61 6c 28 70 2d 3e 70 53 72 63 2d  Virtual(p->pSrc-
39b38 3e 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71  >pTab) ){.    sq
39b39 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
39b3a 20 2a 70 49 64 78 49 6e 66 6f 20 3d 20 30 3b 0a   *pIdxInfo = 0;.
39b3b 20 20 20 20 70 2d 3e 70 70 49 64 78 49 6e 66 6f      p->ppIdxInfo
39b3c 20 3d 20 26 70 49 64 78 49 6e 66 6f 3b 0a 20 20   = &pIdxInfo;.  
39b3d 20 20 62 65 73 74 56 69 72 74 75 61 6c 49 6e 64    bestVirtualInd
39b3e 65 78 28 70 29 3b 0a 20 20 20 20 69 66 28 20 70  ex(p);.    if( p
39b3f 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46  IdxInfo->needToF
39b40 72 65 65 49 64 78 53 74 72 20 29 7b 0a 20 20 20  reeIdxStr ){.   
39b41 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
39b42 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72  pIdxInfo->idxStr
39b43 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
39b44 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 70 50  ite3DbFree(p->pP
39b45 61 72 73 65 2d 3e 64 62 2c 20 70 49 64 78 49 6e  arse->db, pIdxIn
39b46 66 6f 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e  fo);.  }else.#en
39b47 64 69 66 0a 20 20 7b 0a 20 20 20 20 62 65 73 74  dif.  {.    best
39b48 42 74 72 65 65 49 6e 64 65 78 28 70 29 3b 0a 20  BtreeIndex(p);. 
39b49 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 61   }.}../*.** Disa
39b4a 62 6c 65 20 61 20 74 65 72 6d 20 69 6e 20 74 68  ble a term in th
39b4b 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20  e WHERE clause. 
39b4c 20 45 78 63 65 70 74 2c 20 64 6f 20 6e 6f 74 20   Except, do not 
39b4d 64 69 73 61 62 6c 65 20 74 68 65 20 74 65 72 6d  disable the term
39b4e 0a 2a 2a 20 69 66 20 69 74 20 63 6f 6e 74 72 6f  .** if it contro
39b4f 6c 73 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20  ls a LEFT OUTER 
39b50 4a 4f 49 4e 20 61 6e 64 20 69 74 20 64 69 64 20  JOIN and it did 
39b51 6e 6f 74 20 6f 72 69 67 69 6e 61 74 65 20 69 6e  not originate in
39b52 20 74 68 65 20 4f 4e 0a 2a 2a 20 6f 72 20 55 53   the ON.** or US
39b53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ING clause of th
39b54 61 74 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 43  at join..**.** C
39b55 6f 6e 73 69 64 65 72 20 74 68 65 20 74 65 72 6d  onsider the term
39b56 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 6e 20 74 68   t2.z='ok' in th
39b57 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75 65 72  e following quer
39b58 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29  ies:.**.**   (1)
39b59 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
39b5a 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32 20  t1 LEFT JOIN t2 
39b5b 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 57 48 45  ON t1.a=t2.x WHE
39b5c 52 45 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20  RE t2.z='ok'.** 
39b5d 20 20 28 32 29 20 20 53 45 4c 45 43 54 20 2a 20    (2)  SELECT * 
39b5e 46 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49  FROM t1 LEFT JOI
39b5f 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e  N t2 ON t1.a=t2.
39b60 78 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a  x AND t2.z='ok'.
39b61 2a 2a 20 20 20 28 33 29 20 20 53 45 4c 45 43 54  **   (3)  SELECT
39b62 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74 32 20 57   * FROM t1, t2 W
39b63 48 45 52 45 20 74 31 2e 61 3d 74 32 2e 78 20 41  HERE t1.a=t2.x A
39b64 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 0a  ND t2.z='ok'.**.
39b65 2a 2a 20 54 68 65 20 74 32 2e 7a 3d 27 6f 6b 27  ** The t2.z='ok'
39b66 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20   is disabled in 
39b67 74 68 65 20 69 6e 20 28 32 29 20 62 65 63 61 75  the in (2) becau
39b68 73 65 20 69 74 20 6f 72 69 67 69 6e 61 74 65 73  se it originates
39b69 0a 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20 63 6c  .** in the ON cl
39b6a 61 75 73 65 2e 20 20 54 68 65 20 74 65 72 6d 20  ause.  The term 
39b6b 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20 28  is disabled in (
39b6c 33 29 20 62 65 63 61 75 73 65 20 69 74 20 69 73  3) because it is
39b6d 20 6e 6f 74 20 70 61 72 74 0a 2a 2a 20 6f 66 20   not part.** of 
39b6e 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  a LEFT OUTER JOI
39b6f 4e 2e 20 20 49 6e 20 28 31 29 2c 20 74 68 65 20  N.  In (1), the 
39b70 74 65 72 6d 20 69 73 20 6e 6f 74 20 64 69 73 61  term is not disa
39b71 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 4c  bled..**.** IMPL
39b72 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20 52  EMENTATION-OF: R
39b73 2d 32 34 35 39 37 2d 35 38 36 35 35 20 4e 6f 20  -24597-58655 No 
39b74 74 65 73 74 73 20 61 72 65 20 64 6f 6e 65 20 66  tests are done f
39b75 6f 72 20 74 65 72 6d 73 20 74 68 61 74 20 61 72  or terms that ar
39b76 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 6c 79 20  e.** completely 
39b77 73 61 74 69 73 66 69 65 64 20 62 79 20 69 6e 64  satisfied by ind
39b78 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 44 69 73 61  ices..**.** Disa
39b79 62 6c 69 6e 67 20 61 20 74 65 72 6d 20 63 61 75  bling a term cau
39b7a 73 65 73 20 74 68 61 74 20 74 65 72 6d 20 74 6f  ses that term to
39b7b 20 6e 6f 74 20 62 65 20 74 65 73 74 65 64 20 69   not be tested i
39b7c 6e 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70  n the inner loop
39b7d 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e  .** of the join.
39b7e 20 20 44 69 73 61 62 6c 69 6e 67 20 69 73 20 61    Disabling is a
39b7f 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20  n optimization. 
39b80 20 57 68 65 6e 20 74 65 72 6d 73 20 61 72 65 20   When terms are 
39b81 73 61 74 69 73 66 69 65 64 0a 2a 2a 20 62 79 20  satisfied.** by 
39b82 69 6e 64 69 63 65 73 2c 20 77 65 20 64 69 73 61  indices, we disa
39b83 62 6c 65 20 74 68 65 6d 20 74 6f 20 70 72 65 76  ble them to prev
39b84 65 6e 74 20 72 65 64 75 6e 64 61 6e 74 20 74 65  ent redundant te
39b85 73 74 73 20 69 6e 20 74 68 65 20 69 6e 6e 65 72  sts in the inner
39b86 0a 2a 2a 20 6c 6f 6f 70 2e 20 20 57 65 20 77 6f  .** loop.  We wo
39b87 75 6c 64 20 67 65 74 20 74 68 65 20 63 6f 72 72  uld get the corr
39b88 65 63 74 20 72 65 73 75 6c 74 73 20 69 66 20 6e  ect results if n
39b89 6f 74 68 69 6e 67 20 77 65 72 65 20 65 76 65 72  othing were ever
39b8a 20 64 69 73 61 62 6c 65 64 2c 0a 2a 2a 20 62 75   disabled,.** bu
39b8b 74 20 6a 6f 69 6e 73 20 6d 69 67 68 74 20 72 75  t joins might ru
39b8c 6e 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65  n a little slowe
39b8d 72 2e 20 20 54 68 65 20 74 72 69 63 6b 20 69 73  r.  The trick is
39b8e 20 74 6f 20 64 69 73 61 62 6c 65 20 61 73 20 6d   to disable as m
39b8f 75 63 68 0a 2a 2a 20 61 73 20 77 65 20 63 61 6e  uch.** as we can
39b90 20 77 69 74 68 6f 75 74 20 64 69 73 61 62 6c 69   without disabli
39b91 6e 67 20 74 6f 6f 20 6d 75 63 68 2e 20 20 49 66  ng too much.  If
39b92 20 77 65 20 64 69 73 61 62 6c 65 64 20 69 6e 20   we disabled in 
39b93 28 31 29 2c 20 77 65 27 64 20 67 65 74 0a 2a 2a  (1), we'd get.**
39b94 20 74 68 65 20 77 72 6f 6e 67 20 61 6e 73 77 65   the wrong answe
39b95 72 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23  r.  See ticket #
39b96 38 31 33 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  813..*/.static v
39b97 6f 69 64 20 64 69 73 61 62 6c 65 54 65 72 6d 28  oid disableTerm(
39b98 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76  WhereLevel *pLev
39b99 65 6c 2c 20 57 68 65 72 65 54 65 72 6d 20 2a 70  el, WhereTerm *p
39b9a 54 65 72 6d 29 7b 0a 20 20 69 66 28 20 70 54 65  Term){.  if( pTe
39b9b 72 6d 0a 20 20 20 20 20 20 26 26 20 28 70 54 65  rm.      && (pTe
39b9c 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
39b9d 52 4d 5f 43 4f 44 45 44 29 3d 3d 30 0a 20 20 20  RM_CODED)==0.   
39b9e 20 20 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 69     && (pLevel->i
39b9f 4c 65 66 74 4a 6f 69 6e 3d 3d 30 20 7c 7c 20 45  LeftJoin==0 || E
39ba0 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
39ba1 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20 45 50 5f  Term->pExpr, EP_
39ba2 46 72 6f 6d 4a 6f 69 6e 29 29 0a 20 20 29 7b 0a  FromJoin)).  ){.
39ba3 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61      pTerm->wtFla
39ba4 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44  gs |= TERM_CODED
39ba5 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d  ;.    if( pTerm-
39ba6 3e 69 50 61 72 65 6e 74 3e 3d 30 20 29 7b 0a 20  >iParent>=0 ){. 
39ba7 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a       WhereTerm *
39ba8 70 4f 74 68 65 72 20 3d 20 26 70 54 65 72 6d 2d  pOther = &pTerm-
39ba9 3e 70 57 43 2d 3e 61 5b 70 54 65 72 6d 2d 3e 69  >pWC->a[pTerm->i
39baa 50 61 72 65 6e 74 5d 3b 0a 20 20 20 20 20 20 69  Parent];.      i
39bab 66 28 20 28 2d 2d 70 4f 74 68 65 72 2d 3e 6e 43  f( (--pOther->nC
39bac 68 69 6c 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20  hild)==0 ){.    
39bad 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28      disableTerm(
39bae 70 4c 65 76 65 6c 2c 20 70 4f 74 68 65 72 29 3b  pLevel, pOther);
39baf 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
39bb0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65   }.}../*.** Code
39bb1 20 61 6e 20 4f 50 5f 41 66 66 69 6e 69 74 79 20   an OP_Affinity 
39bb2 6f 70 63 6f 64 65 20 74 6f 20 61 70 70 6c 79 20  opcode to apply 
39bb3 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e  the column affin
39bb4 69 74 79 20 73 74 72 69 6e 67 20 7a 41 66 66 0a  ity string zAff.
39bb5 2a 2a 20 74 6f 20 74 68 65 20 6e 20 72 65 67 69  ** to the n regi
39bb6 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 61  sters starting a
39bb7 74 20 62 61 73 65 2e 20 0a 2a 2a 0a 2a 2a 20 41  t base. .**.** A
39bb8 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
39bb9 6e 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  n, SQLITE_AFF_NO
39bba 4e 45 20 65 6e 74 72 69 65 73 20 28 77 68 69 63  NE entries (whic
39bbb 68 20 61 72 65 20 6e 6f 2d 6f 70 73 29 20 61 74  h are no-ops) at
39bbc 20 74 68 65 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e   the.** beginnin
39bbd 67 20 61 6e 64 20 65 6e 64 20 6f 66 20 7a 41 66  g and end of zAf
39bbe 66 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 20 20  f are ignored.  
39bbf 49 66 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69  If all entries i
39bc0 6e 20 7a 41 66 66 20 61 72 65 0a 2a 2a 20 53 51  n zAff are.** SQ
39bc1 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 2c 20 74  LITE_AFF_NONE, t
39bc2 68 65 6e 20 6e 6f 20 63 6f 64 65 20 67 65 74 73  hen no code gets
39bc3 20 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2a 0a 2a   generated..**.*
39bc4 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d  * This routine m
39bc5 61 6b 65 73 20 69 74 73 20 6f 77 6e 20 63 6f 70  akes its own cop
39bc6 79 20 6f 66 20 7a 41 66 66 20 73 6f 20 74 68 61  y of zAff so tha
39bc7 74 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20  t the caller is 
39bc8 66 72 65 65 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66  free.** to modif
39bc9 79 20 7a 41 66 66 20 61 66 74 65 72 20 74 68 69  y zAff after thi
39bca 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
39bcb 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
39bcc 64 20 63 6f 64 65 41 70 70 6c 79 41 66 66 69 6e  d codeApplyAffin
39bcd 69 74 79 28 50 61 72 73 65 20 2a 70 50 61 72 73  ity(Parse *pPars
39bce 65 2c 20 69 6e 74 20 62 61 73 65 2c 20 69 6e 74  e, int base, int
39bcf 20 6e 2c 20 63 68 61 72 20 2a 7a 41 66 66 29 7b   n, char *zAff){
39bd0 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
39bd1 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 66  rse->pVdbe;.  if
39bd2 28 20 7a 41 66 66 3d 3d 30 20 29 7b 0a 20 20 20  ( zAff==0 ){.   
39bd3 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
39bd4 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
39bd5 64 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  d );.    return;
39bd6 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 76  .  }.  assert( v
39bd7 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 64 6a  !=0 );..  /* Adj
39bd8 75 73 74 20 62 61 73 65 20 61 6e 64 20 6e 20 74  ust base and n t
39bd9 6f 20 73 6b 69 70 20 6f 76 65 72 20 53 51 4c 49  o skip over SQLI
39bda 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 65 6e 74 72  TE_AFF_NONE entr
39bdb 69 65 73 20 61 74 20 74 68 65 20 62 65 67 69 6e  ies at the begin
39bdc 6e 69 6e 67 0a 20 20 2a 2a 20 61 6e 64 20 65 6e  ning.  ** and en
39bdd 64 20 6f 66 20 74 68 65 20 61 66 66 69 6e 69 74  d of the affinit
39bde 79 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2f 0a 20  y string..  */. 
39bdf 20 77 68 69 6c 65 28 20 6e 3e 30 20 26 26 20 7a   while( n>0 && z
39be0 41 66 66 5b 30 5d 3d 3d 53 51 4c 49 54 45 5f 41  Aff[0]==SQLITE_A
39be1 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 6e  FF_NONE ){.    n
39be2 2d 2d 3b 0a 20 20 20 20 62 61 73 65 2b 2b 3b 0a  --;.    base++;.
39be3 20 20 20 20 7a 41 66 66 2b 2b 3b 0a 20 20 7d 0a      zAff++;.  }.
39be4 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 20    while( n>1 && 
39be5 7a 41 66 66 5b 6e 2d 31 5d 3d 3d 53 51 4c 49 54  zAff[n-1]==SQLIT
39be6 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20  E_AFF_NONE ){.  
39be7 20 20 6e 2d 2d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a    n--;.  }..  /*
39be8 20 43 6f 64 65 20 74 68 65 20 4f 50 5f 41 66 66   Code the OP_Aff
39be9 69 6e 69 74 79 20 6f 70 63 6f 64 65 20 69 66 20  inity opcode if 
39bea 74 68 65 72 65 20 69 73 20 61 6e 79 74 68 69 6e  there is anythin
39beb 67 20 6c 65 66 74 20 74 6f 20 64 6f 2e 20 2a 2f  g left to do. */
39bec 0a 20 20 69 66 28 20 6e 3e 30 20 29 7b 0a 20 20  .  if( n>0 ){.  
39bed 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
39bee 4f 70 32 28 76 2c 20 4f 50 5f 41 66 66 69 6e 69  Op2(v, OP_Affini
39bef 74 79 2c 20 62 61 73 65 2c 20 6e 29 3b 0a 20 20  ty, base, n);.  
39bf0 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
39bf1 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 7a 41 66  ngeP4(v, -1, zAf
39bf2 66 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74  f, n);.    sqlit
39bf3 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e  e3ExprCacheAffin
39bf4 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65  ityChange(pParse
39bf5 2c 20 62 61 73 65 2c 20 6e 29 3b 0a 20 20 7d 0a  , base, n);.  }.
39bf6 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  }.../*.** Genera
39bf7 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69  te code for a si
39bf8 6e 67 6c 65 20 65 71 75 61 6c 69 74 79 20 74 65  ngle equality te
39bf9 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  rm of the WHERE 
39bfa 63 6c 61 75 73 65 2e 20 20 41 6e 20 65 71 75 61  clause.  An equa
39bfb 6c 69 74 79 0a 2a 2a 20 74 65 72 6d 20 63 61 6e  lity.** term can
39bfc 20 62 65 20 65 69 74 68 65 72 20 58 3d 65 78 70   be either X=exp
39bfd 72 20 6f 72 20 58 20 49 4e 20 28 2e 2e 2e 29 2e  r or X IN (...).
39bfe 20 20 20 70 54 65 72 6d 20 69 73 20 74 68 65 20     pTerm is the 
39bff 74 65 72 6d 20 74 6f 20 62 65 20 0a 2a 2a 20 63  term to be .** c
39c00 6f 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  oded..**.** The 
39c01 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 66 6f  current value fo
39c02 72 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  r the constraint
39c03 20 69 73 20 6c 65 66 74 20 69 6e 20 72 65 67 69   is left in regi
39c04 73 74 65 72 20 69 52 65 67 2e 0a 2a 2a 0a 2a 2a  ster iReg..**.**
39c05 20 46 6f 72 20 61 20 63 6f 6e 73 74 72 61 69 6e   For a constrain
39c06 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 3d  t of the form X=
39c07 65 78 70 72 2c 20 74 68 65 20 65 78 70 72 65 73  expr, the expres
39c08 73 69 6f 6e 20 69 73 20 65 76 61 6c 75 61 74 65  sion is evaluate
39c09 64 20 61 6e 64 20 69 74 73 0a 2a 2a 20 72 65 73  d and its.** res
39c0a 75 6c 74 20 69 73 20 6c 65 66 74 20 6f 6e 20 74  ult is left on t
39c0b 68 65 20 73 74 61 63 6b 2e 20 20 46 6f 72 20 63  he stack.  For c
39c0c 6f 6e 73 74 72 61 69 6e 74 73 20 6f 66 20 74 68  onstraints of th
39c0d 65 20 66 6f 72 6d 20 58 20 49 4e 20 28 2e 2e 2e  e form X IN (...
39c0e 29 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  ).** this routin
39c0f 65 20 73 65 74 73 20 75 70 20 61 20 6c 6f 6f 70  e sets up a loop
39c10 20 74 68 61 74 20 77 69 6c 6c 20 69 74 65 72 61   that will itera
39c11 74 65 20 6f 76 65 72 20 61 6c 6c 20 76 61 6c 75  te over all valu
39c12 65 73 20 6f 66 20 58 2e 0a 2a 2f 0a 73 74 61 74  es of X..*/.stat
39c13 69 63 20 69 6e 74 20 63 6f 64 65 45 71 75 61 6c  ic int codeEqual
39c14 69 74 79 54 65 72 6d 28 0a 20 20 50 61 72 73 65  ityTerm(.  Parse
39c15 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f   *pParse,      /
39c16 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
39c17 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65  ntext */.  Where
39c18 54 65 72 6d 20 2a 70 54 65 72 6d 2c 20 20 20 2f  Term *pTerm,   /
39c19 2a 20 54 68 65 20 74 65 72 6d 20 6f 66 20 74 68  * The term of th
39c1a 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  e WHERE clause t
39c1b 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20  o be coded */.  
39c1c 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76  WhereLevel *pLev
39c1d 65 6c 2c 20 2f 2a 20 57 68 65 6e 20 6c 65 76 65  el, /* When leve
39c1e 6c 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c  l of the FROM cl
39c1f 61 75 73 65 20 77 65 20 61 72 65 20 77 6f 72 6b  ause we are work
39c20 69 6e 67 20 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ing on */.  int 
39c21 69 54 61 72 67 65 74 20 20 20 20 20 20 20 20 20  iTarget         
39c22 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 65  /* Attempt to le
39c23 61 76 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74  ave results in t
39c24 68 69 73 20 72 65 67 69 73 74 65 72 20 2a 2f 0a  his register */.
39c25 29 7b 0a 20 20 45 78 70 72 20 2a 70 58 20 3d 20  ){.  Expr *pX = 
39c26 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
39c27 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
39c28 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69  ->pVdbe;.  int i
39c29 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Reg;            
39c2a 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
39c2b 72 20 68 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74  r holding result
39c2c 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  s */..  assert( 
39c2d 69 54 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 69  iTarget>0 );.  i
39c2e 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 45 51  f( pX->op==TK_EQ
39c2f 20 29 7b 0a 20 20 20 20 69 52 65 67 20 3d 20 73   ){.    iReg = s
39c30 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
39c31 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 58 2d  rget(pParse, pX-
39c32 3e 70 52 69 67 68 74 2c 20 69 54 61 72 67 65 74  >pRight, iTarget
39c33 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  );.  }else if( p
39c34 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c  X->op==TK_ISNULL
39c35 20 29 7b 0a 20 20 20 20 69 52 65 67 20 3d 20 69   ){.    iReg = i
39c36 54 61 72 67 65 74 3b 0a 20 20 20 20 73 71 6c 69  Target;.    sqli
39c37 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
39c38 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52 65   OP_Null, 0, iRe
39c39 67 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  g);.#ifndef SQLI
39c3a 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
39c3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
39c3c 74 20 65 54 79 70 65 3b 0a 20 20 20 20 69 6e 74  t eType;.    int
39c3d 20 69 54 61 62 3b 0a 20 20 20 20 73 74 72 75 63   iTab;.    struc
39c3e 74 20 49 6e 4c 6f 6f 70 20 2a 70 49 6e 3b 0a 0a  t InLoop *pIn;..
39c3f 20 20 20 20 61 73 73 65 72 74 28 20 70 58 2d 3e      assert( pX->
39c40 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 20  op==TK_IN );.   
39c41 20 69 52 65 67 20 3d 20 69 54 61 72 67 65 74 3b   iReg = iTarget;
39c42 0a 20 20 20 20 65 54 79 70 65 20 3d 20 73 71 6c  .    eType = sql
39c43 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28  ite3FindInIndex(
39c44 70 50 61 72 73 65 2c 20 70 58 2c 20 30 29 3b 0a  pParse, pX, 0);.
39c45 20 20 20 20 69 54 61 62 20 3d 20 70 58 2d 3e 69      iTab = pX->i
39c46 54 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74  Table;.    sqlit
39c47 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
39c48 4f 50 5f 52 65 77 69 6e 64 2c 20 69 54 61 62 2c  OP_Rewind, iTab,
39c49 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   0);.    assert(
39c4a 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
39c4b 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e  Flags & WHERE_IN
39c4c 5f 41 42 4c 45 20 29 3b 0a 20 20 20 20 69 66 28  _ABLE );.    if(
39c4d 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49   pLevel->u.in.nI
39c4e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c  n==0 ){.      pL
39c4f 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 20 3d 20  evel->addrNxt = 
39c50 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
39c51 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 7d 0a 20  abel(v);.    }. 
39c52 20 20 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e     pLevel->u.in.
39c53 6e 49 6e 2b 2b 3b 0a 20 20 20 20 70 4c 65 76 65  nIn++;.    pLeve
39c54 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 20  l->u.in.aInLoop 
39c55 3d 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  =.       sqlite3
39c56 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
39c57 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 65 76  pParse->db, pLev
39c58 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70  el->u.in.aInLoop
39c59 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
39c5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39c5b 73 69 7a 65 6f 66 28 70 4c 65 76 65 6c 2d 3e 75  sizeof(pLevel->u
39c5c 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 5b 30 5d 29 2a  .in.aInLoop[0])*
39c5d 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e  pLevel->u.in.nIn
39c5e 29 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 4c 65  );.    pIn = pLe
39c5f 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f  vel->u.in.aInLoo
39c60 70 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 20 29  p;.    if( pIn )
39c61 7b 0a 20 20 20 20 20 20 70 49 6e 20 2b 3d 20 70  {.      pIn += p
39c62 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 20  Level->u.in.nIn 
39c63 2d 20 31 3b 0a 20 20 20 20 20 20 70 49 6e 2d 3e  - 1;.      pIn->
39c64 69 43 75 72 20 3d 20 69 54 61 62 3b 0a 20 20 20  iCur = iTab;.   
39c65 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e     if( eType==IN
39c66 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0a  _INDEX_ROWID ){.
39c67 20 20 20 20 20 20 20 20 70 49 6e 2d 3e 61 64 64          pIn->add
39c68 72 49 6e 54 6f 70 20 3d 20 73 71 6c 69 74 65 33  rInTop = sqlite3
39c69 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
39c6a 5f 52 6f 77 69 64 2c 20 69 54 61 62 2c 20 69 52  _Rowid, iTab, iR
39c6b 65 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  eg);.      }else
39c6c 7b 0a 20 20 20 20 20 20 20 20 70 49 6e 2d 3e 61  {.        pIn->a
39c6d 64 64 72 49 6e 54 6f 70 20 3d 20 73 71 6c 69 74  ddrInTop = sqlit
39c6e 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
39c6f 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61 62 2c  OP_Column, iTab,
39c70 20 30 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 20   0, iReg);.     
39c71 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
39c72 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
39c73 5f 49 73 4e 75 6c 6c 2c 20 69 52 65 67 29 3b 0a  _IsNull, iReg);.
39c74 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
39c75 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49   pLevel->u.in.nI
39c76 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 23 65 6e  n = 0;.    }.#en
39c77 64 69 66 0a 20 20 7d 0a 20 20 64 69 73 61 62 6c  dif.  }.  disabl
39c78 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 54  eTerm(pLevel, pT
39c79 65 72 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  erm);.  return i
39c7a 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  Reg;.}../*.** Ge
39c7b 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
39c7c 20 77 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 61   will evaluate a
39c7d 6c 6c 20 3d 3d 20 61 6e 64 20 49 4e 20 63 6f 6e  ll == and IN con
39c7e 73 74 72 61 69 6e 74 73 20 66 6f 72 20 61 6e 0a  straints for an.
39c7f 2a 2a 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  ** index..**.** 
39c80 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 63 6f 6e  For example, con
39c81 73 69 64 65 72 20 74 61 62 6c 65 20 74 31 28 61  sider table t1(a
39c82 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 77 69 74 68  ,b,c,d,e,f) with
39c83 20 69 6e 64 65 78 20 69 31 28 61 2c 62 2c 63 29   index i1(a,b,c)
39c84 2e 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65  ..** Suppose the
39c85 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 73   WHERE clause is
39c86 20 74 68 69 73 3a 20 20 61 3d 3d 35 20 41 4e 44   this:  a==5 AND
39c87 20 62 20 49 4e 20 28 31 2c 32 2c 33 29 20 41 4e   b IN (1,2,3) AN
39c88 44 20 63 3e 35 20 41 4e 44 20 63 3c 31 30 0a 2a  D c>5 AND c<10.*
39c89 2a 20 54 68 65 20 69 6e 64 65 78 20 68 61 73 20  * The index has 
39c8a 61 73 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65  as many as three
39c8b 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72   equality constr
39c8c 61 69 6e 74 73 2c 20 62 75 74 20 69 6e 20 74 68  aints, but in th
39c8d 69 73 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 74  is.** example, t
39c8e 68 65 20 74 68 69 72 64 20 22 63 22 20 76 61 6c  he third "c" val
39c8f 75 65 20 69 73 20 61 6e 20 69 6e 65 71 75 61 6c  ue is an inequal
39c90 69 74 79 2e 20 20 53 6f 20 6f 6e 6c 79 20 74 77  ity.  So only tw
39c91 6f 20 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74  o .** constraint
39c92 73 20 61 72 65 20 63 6f 64 65 64 2e 20 20 54 68  s are coded.  Th
39c93 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
39c94 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  generate code to
39c95 20 65 76 61 6c 75 61 74 65 0a 2a 2a 20 61 3d 3d   evaluate.** a==
39c96 35 20 61 6e 64 20 62 20 49 4e 20 28 31 2c 32 2c  5 and b IN (1,2,
39c97 33 29 2e 20 20 54 68 65 20 63 75 72 72 65 6e 74  3).  The current
39c98 20 76 61 6c 75 65 73 20 66 6f 72 20 61 20 61 6e   values for a an
39c99 64 20 62 20 77 69 6c 6c 20 62 65 20 73 74 6f 72  d b will be stor
39c9a 65 64 0a 2a 2a 20 69 6e 20 63 6f 6e 73 65 63 75  ed.** in consecu
39c9b 74 69 76 65 20 72 65 67 69 73 74 65 72 73 20 61  tive registers a
39c9c 6e 64 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  nd the index of 
39c9d 74 68 65 20 66 69 72 73 74 20 72 65 67 69 73 74  the first regist
39c9e 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  er is returned..
39c9f 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 61  **.** In the exa
39ca0 6d 70 6c 65 20 61 62 6f 76 65 20 6e 45 71 3d 3d  mple above nEq==
39ca1 32 2e 20 20 42 75 74 20 74 68 69 73 20 73 75 62  2.  But this sub
39ca2 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 66 6f  routine works fo
39ca3 72 20 61 6e 79 20 76 61 6c 75 65 0a 2a 2a 20 6f  r any value.** o
39ca4 66 20 6e 45 71 20 69 6e 63 6c 75 64 69 6e 67 20  f nEq including 
39ca5 30 2e 20 20 49 66 20 6e 45 71 3d 3d 30 2c 20 74  0.  If nEq==0, t
39ca6 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e  his routine is n
39ca7 65 61 72 6c 79 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  early a no-op..*
39ca8 2a 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67  * The only thing
39ca9 20 69 74 20 64 6f 65 73 20 69 73 20 61 6c 6c 6f   it does is allo
39caa 63 61 74 65 20 74 68 65 20 70 4c 65 76 65 6c 2d  cate the pLevel-
39cab 3e 69 4d 65 6d 20 6d 65 6d 6f 72 79 20 63 65 6c  >iMem memory cel
39cac 6c 20 61 6e 64 0a 2a 2a 20 63 6f 6d 70 75 74 65  l and.** compute
39cad 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 73 74   the affinity st
39cae 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ring..**.** This
39caf 20 72 6f 75 74 69 6e 65 20 61 6c 77 61 79 73 20   routine always 
39cb0 61 6c 6c 6f 63 61 74 65 73 20 61 74 20 6c 65 61  allocates at lea
39cb1 73 74 20 6f 6e 65 20 6d 65 6d 6f 72 79 20 63 65  st one memory ce
39cb2 6c 6c 20 61 6e 64 20 72 65 74 75 72 6e 73 0a 2a  ll and returns.*
39cb3 2a 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  * the index of t
39cb4 68 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e  hat memory cell.
39cb5 20 54 68 65 20 63 6f 64 65 20 74 68 61 74 0a 2a   The code that.*
39cb6 2a 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  * calls this rou
39cb7 74 69 6e 65 20 77 69 6c 6c 20 75 73 65 20 74 68  tine will use th
39cb8 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74  at memory cell t
39cb9 6f 20 73 74 6f 72 65 20 74 68 65 20 74 65 72 6d  o store the term
39cba 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6b 65 79 20 76  ination.** key v
39cbb 61 6c 75 65 20 6f 66 20 74 68 65 20 6c 6f 6f 70  alue of the loop
39cbc 2e 20 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72  .  If one or mor
39cbd 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 20 61  e IN operators a
39cbe 70 70 65 61 72 2c 20 74 68 65 6e 0a 2a 2a 20 74  ppear, then.** t
39cbf 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f  his routine allo
39cc0 63 61 74 65 73 20 61 6e 20 61 64 64 69 74 69 6f  cates an additio
39cc1 6e 61 6c 20 6e 45 71 20 6d 65 6d 6f 72 79 20 63  nal nEq memory c
39cc2 65 6c 6c 73 20 66 6f 72 20 69 6e 74 65 72 6e 61  ells for interna
39cc3 6c 0a 2a 2a 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20  l.** use..**.** 
39cc4 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  Before returning
39cc5 2c 20 2a 70 7a 41 66 66 20 69 73 20 73 65 74 20  , *pzAff is set 
39cc6 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75  to point to a bu
39cc7 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ffer containing 
39cc8 61 0a 2a 2a 20 63 6f 70 79 20 6f 66 20 74 68 65  a.** copy of the
39cc9 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
39cca 20 73 74 72 69 6e 67 20 6f 66 20 74 68 65 20 69   string of the i
39ccb 6e 64 65 78 20 61 6c 6c 6f 63 61 74 65 64 20 75  ndex allocated u
39ccc 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 44  sing.** sqlite3D
39ccd 62 4d 61 6c 6c 6f 63 28 29 2e 20 45 78 63 65 70  bMalloc(). Excep
39cce 74 2c 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  t, entries in th
39ccf 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 74  e copy of the st
39cd0 72 69 6e 67 20 61 73 73 6f 63 69 61 74 65 64 0a  ring associated.
39cd1 2a 2a 20 77 69 74 68 20 65 71 75 61 6c 69 74 79  ** with equality
39cd2 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 68 61   constraints tha
39cd3 74 20 75 73 65 20 4e 4f 4e 45 20 61 66 66 69 6e  t use NONE affin
39cd4 69 74 79 20 61 72 65 20 73 65 74 20 74 6f 0a 2a  ity are set to.*
39cd5 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  * SQLITE_AFF_NON
39cd6 45 2e 20 54 68 69 73 20 69 73 20 74 6f 20 64 65  E. This is to de
39cd7 61 6c 20 77 69 74 68 20 53 51 4c 20 73 75 63 68  al with SQL such
39cd8 20 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   as the followin
39cd9 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 43 52 45 41 54  g:.**.**   CREAT
39cda 45 20 54 41 42 4c 45 20 74 31 28 61 20 54 45 58  E TABLE t1(a TEX
39cdb 54 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62  T PRIMARY KEY, b
39cdc 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 2e  );.**   SELECT .
39cdd 2e 2e 20 46 52 4f 4d 20 74 31 20 41 53 20 74 32  .. FROM t1 AS t2
39cde 2c 20 74 31 20 57 48 45 52 45 20 74 31 2e 61 20  , t1 WHERE t1.a 
39cdf 3d 20 74 32 2e 62 3b 0a 2a 2a 0a 2a 2a 20 49 6e  = t2.b;.**.** In
39ce0 20 74 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f   the example abo
39ce1 76 65 2c 20 74 68 65 20 69 6e 64 65 78 20 6f 6e  ve, the index on
39ce2 20 74 31 28 61 29 20 68 61 73 20 54 45 58 54 20   t1(a) has TEXT 
39ce3 61 66 66 69 6e 69 74 79 2e 20 42 75 74 20 73 69  affinity. But si
39ce4 6e 63 65 0a 2a 2a 20 74 68 65 20 72 69 67 68 74  nce.** the right
39ce5 20 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68   hand side of th
39ce6 65 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  e equality const
39ce7 72 61 69 6e 74 20 28 74 32 2e 62 29 20 68 61 73  raint (t2.b) has
39ce8 20 4e 4f 4e 45 20 61 66 66 69 6e 69 74 79 2c 0a   NONE affinity,.
39ce9 2a 2a 20 6e 6f 20 63 6f 6e 76 65 72 73 69 6f 6e  ** no conversion
39cea 20 73 68 6f 75 6c 64 20 62 65 20 61 74 74 65 6d   should be attem
39ceb 70 74 65 64 20 62 65 66 6f 72 65 20 75 73 69 6e  pted before usin
39cec 67 20 61 20 74 32 2e 62 20 76 61 6c 75 65 20 61  g a t2.b value a
39ced 73 20 70 61 72 74 20 6f 66 0a 2a 2a 20 61 20 6b  s part of.** a k
39cee 65 79 20 74 6f 20 73 65 61 72 63 68 20 74 68 65  ey to search the
39cef 20 69 6e 64 65 78 2e 20 48 65 6e 63 65 20 74 68   index. Hence th
39cf0 65 20 66 69 72 73 74 20 62 79 74 65 20 69 6e 20  e first byte in 
39cf1 74 68 65 20 72 65 74 75 72 6e 65 64 20 61 66 66  the returned aff
39cf2 69 6e 69 74 79 0a 2a 2a 20 73 74 72 69 6e 67 20  inity.** string 
39cf3 69 6e 20 74 68 69 73 20 65 78 61 6d 70 6c 65 20  in this example 
39cf4 77 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20  would be set to 
39cf5 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 2e  SQLITE_AFF_NONE.
39cf6 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
39cf7 6f 64 65 41 6c 6c 45 71 75 61 6c 69 74 79 54 65  odeAllEqualityTe
39cf8 72 6d 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50  rms(.  Parse *pP
39cf9 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
39cfa 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
39cfb 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20  */.  WhereLevel 
39cfc 2a 70 4c 65 76 65 6c 2c 20 20 20 2f 2a 20 57 68  *pLevel,   /* Wh
39cfd 69 63 68 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20  ich nested loop 
39cfe 6f 66 20 74 68 65 20 46 52 4f 4d 20 77 65 20 61  of the FROM we a
39cff 72 65 20 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 57  re coding */.  W
39d00 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c  hereClause *pWC,
39d01 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
39d02 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 42 69  E clause */.  Bi
39d03 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20  tmask notReady, 
39d04 20 20 20 20 2f 2a 20 57 68 69 63 68 20 70 61 72      /* Which par
39d05 74 73 20 6f 66 20 46 52 4f 4d 20 68 61 76 65 20  ts of FROM have 
39d06 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 6f 64  not yet been cod
39d07 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74  ed */.  int nExt
39d08 72 61 52 65 67 2c 20 20 20 20 20 20 20 20 2f 2a  raReg,        /*
39d09 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72 61   Number of extra
39d0a 20 72 65 67 69 73 74 65 72 73 20 74 6f 20 61 6c   registers to al
39d0b 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 63 68 61 72  locate */.  char
39d0c 20 2a 2a 70 7a 41 66 66 20 20 20 20 20 20 20 20   **pzAff        
39d0d 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f    /* OUT: Set to
39d0e 20 70 6f 69 6e 74 20 74 6f 20 61 66 66 69 6e 69   point to affini
39d0f 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a  ty string */.){.
39d10 20 20 69 6e 74 20 6e 45 71 20 3d 20 70 4c 65 76    int nEq = pLev
39d11 65 6c 2d 3e 70 6c 61 6e 2e 6e 45 71 3b 20 20 20  el->plan.nEq;   
39d12 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  /* The number of
39d13 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e 73 74 72   == or IN constr
39d14 61 69 6e 74 73 20 74 6f 20 63 6f 64 65 20 2a 2f  aints to code */
39d15 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
39d16 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 20 20  rse->pVdbe;     
39d17 20 2f 2a 20 54 68 65 20 76 6d 20 75 6e 64 65 72   /* The vm under
39d18 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f   construction */
39d19 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20  .  Index *pIdx; 
39d1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39d1b 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 62 65   /* The index be
39d1c 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68 69  ing used for thi
39d1d 73 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20  s loop */.  int 
39d1e 69 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69  iCur = pLevel->i
39d1f 54 61 62 43 75 72 3b 20 20 20 2f 2a 20 54 68 65  TabCur;   /* The
39d20 20 63 75 72 73 6f 72 20 6f 66 20 74 68 65 20 74   cursor of the t
39d21 61 62 6c 65 20 2a 2f 0a 20 20 57 68 65 72 65 54  able */.  WhereT
39d22 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20  erm *pTerm;     
39d23 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e          /* A sin
39d24 67 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 74  gle constraint t
39d25 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20  erm */.  int j; 
39d26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39d27 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
39d28 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
39d29 72 65 67 42 61 73 65 3b 20 20 20 20 20 20 20 20  regBase;        
39d2a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73            /* Bas
39d2b 65 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  e register */.  
39d2c 69 6e 74 20 6e 52 65 67 3b 20 20 20 20 20 20 20  int nReg;       
39d2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39d2e 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73   Number of regis
39d2f 74 65 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65  ters to allocate
39d30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 41 66 66   */.  char *zAff
39d31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
39d32 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20      /* Affinity 
39d33 73 74 72 69 6e 67 20 74 6f 20 72 65 74 75 72 6e  string to return
39d34 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6d   */..  /* This m
39d35 6f 64 75 6c 65 20 69 73 20 6f 6e 6c 79 20 63 61  odule is only ca
39d36 6c 6c 65 64 20 6f 6e 20 71 75 65 72 79 20 70 6c  lled on query pl
39d37 61 6e 73 20 74 68 61 74 20 75 73 65 20 61 6e 20  ans that use an 
39d38 69 6e 64 65 78 2e 20 2a 2f 0a 20 20 61 73 73 65  index. */.  asse
39d39 72 74 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  rt( pLevel->plan
39d3a 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
39d3b 5f 49 4e 44 45 58 45 44 20 29 3b 0a 20 20 70 49  _INDEXED );.  pI
39d3c 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  dx = pLevel->pla
39d3d 6e 2e 75 2e 70 49 64 78 3b 0a 0a 20 20 2f 2a 20  n.u.pIdx;..  /* 
39d3e 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d  Figure out how m
39d3f 61 6e 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73  any memory cells
39d40 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 68   we will need th
39d41 65 6e 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 6d  en allocate them
39d42 2e 0a 20 20 2a 2f 0a 20 20 72 65 67 42 61 73 65  ..  */.  regBase
39d43 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20   = pParse->nMem 
39d44 2b 20 31 3b 0a 20 20 6e 52 65 67 20 3d 20 70 4c  + 1;.  nReg = pL
39d45 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 45 71 20 2b  evel->plan.nEq +
39d46 20 6e 45 78 74 72 61 52 65 67 3b 0a 20 20 70 50   nExtraReg;.  pP
39d47 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52  arse->nMem += nR
39d48 65 67 3b 0a 0a 20 20 7a 41 66 66 20 3d 20 73 71  eg;..  zAff = sq
39d49 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 70 50  lite3DbStrDup(pP
39d4a 61 72 73 65 2d 3e 64 62 2c 20 73 71 6c 69 74 65  arse->db, sqlite
39d4b 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74  3IndexAffinitySt
39d4c 72 28 76 2c 20 70 49 64 78 29 29 3b 0a 20 20 69  r(v, pIdx));.  i
39d4d 66 28 20 21 7a 41 66 66 20 29 7b 0a 20 20 20 20  f( !zAff ){.    
39d4e 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
39d4f 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
39d50 7d 0a 0a 20 20 2f 2a 20 45 76 61 6c 75 61 74 65  }..  /* Evaluate
39d51 20 74 68 65 20 65 71 75 61 6c 69 74 79 20 63 6f   the equality co
39d52 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2f 0a 20  nstraints.  */. 
39d53 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 6e   assert( pIdx->n
39d54 43 6f 6c 75 6d 6e 3e 3d 6e 45 71 20 29 3b 0a 20  Column>=nEq );. 
39d55 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 45 71 3b   for(j=0; j<nEq;
39d56 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72   j++){.    int r
39d57 31 3b 0a 20 20 20 20 69 6e 74 20 6b 20 3d 20 70  1;.    int k = p
39d58 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d  Idx->aiColumn[j]
39d59 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 66 69  ;.    pTerm = fi
39d5a 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
39d5b 2c 20 6b 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70  , k, notReady, p
39d5c 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
39d5d 61 67 73 2c 20 70 49 64 78 29 3b 0a 20 20 20 20  ags, pIdx);.    
39d5e 69 66 28 20 70 54 65 72 6d 3d 3d 30 20 29 20 62  if( pTerm==0 ) b
39d5f 72 65 61 6b 3b 0a 20 20 20 20 2f 2a 20 54 68 65  reak;.    /* The
39d60 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 72 75 65 20   following true 
39d61 66 6f 72 20 69 6e 64 69 63 65 73 20 77 69 74 68  for indices with
39d62 20 72 65 64 75 6e 64 61 6e 74 20 63 6f 6c 75 6d   redundant colum
39d63 6e 73 2e 20 0a 20 20 20 20 2a 2a 20 45 78 3a 20  ns. .    ** Ex: 
39d64 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 31 20  CREATE INDEX i1 
39d65 4f 4e 20 74 31 28 61 2c 62 2c 61 29 3b 20 53 45  ON t1(a,b,a); SE
39d66 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57  LECT * FROM t1 W
39d67 48 45 52 45 20 61 3d 30 20 41 4e 44 20 62 3d 30  HERE a=0 AND b=0
39d68 3b 20 2a 2f 0a 20 20 20 20 74 65 73 74 63 61 73  ; */.    testcas
39d69 65 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61  e( (pTerm->wtFla
39d6a 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 29  gs & TERM_CODED)
39d6b 21 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74 63  !=0 );.    testc
39d6c 61 73 65 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c  ase( pTerm->wtFl
39d6d 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55  ags & TERM_VIRTU
39d6e 41 4c 20 29 3b 20 2f 2a 20 45 56 3a 20 52 2d 33  AL ); /* EV: R-3
39d6f 30 35 37 35 2d 31 31 36 36 32 20 2a 2f 0a 20 20  0575-11662 */.  
39d70 20 20 72 31 20 3d 20 63 6f 64 65 45 71 75 61 6c    r1 = codeEqual
39d71 69 74 79 54 65 72 6d 28 70 50 61 72 73 65 2c 20  ityTerm(pParse, 
39d72 70 54 65 72 6d 2c 20 70 4c 65 76 65 6c 2c 20 72  pTerm, pLevel, r
39d73 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20 69  egBase+j);.    i
39d74 66 28 20 72 31 21 3d 72 65 67 42 61 73 65 2b 6a  f( r1!=regBase+j
39d75 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 52   ){.      if( nR
39d76 65 67 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  eg==1 ){.       
39d77 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
39d78 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
39d79 65 67 42 61 73 65 29 3b 0a 20 20 20 20 20 20 20  egBase);.       
39d7a 20 72 65 67 42 61 73 65 20 3d 20 72 31 3b 0a 20   regBase = r1;. 
39d7b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
39d7c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
39d7d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
39d7e 79 2c 20 72 31 2c 20 72 65 67 42 61 73 65 2b 6a  y, r1, regBase+j
39d7f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
39d80 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
39d81 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
39d82 26 20 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20  & WO_ISNULL );. 
39d83 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65     testcase( pTe
39d84 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
39d85 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20 69 66 28  WO_IN );.    if(
39d86 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74   (pTerm->eOperat
39d87 6f 72 20 26 20 28 57 4f 5f 49 53 4e 55 4c 4c 7c  or & (WO_ISNULL|
39d88 57 4f 5f 49 4e 29 29 3d 3d 30 20 29 7b 0a 20 20  WO_IN))==0 ){.  
39d89 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74      Expr *pRight
39d8a 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 2d   = pTerm->pExpr-
39d8b 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 73  >pRight;.      s
39d8c 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 49 73  qlite3ExprCodeIs
39d8d 4e 75 6c 6c 4a 75 6d 70 28 76 2c 20 70 52 69 67  NullJump(v, pRig
39d8e 68 74 2c 20 72 65 67 42 61 73 65 2b 6a 2c 20 70  ht, regBase+j, p
39d8f 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 29 3b  Level->addrBrk);
39d90 0a 20 20 20 20 20 20 69 66 28 20 7a 41 66 66 20  .      if( zAff 
39d91 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  ){.        if( s
39d92 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
39d93 69 6e 69 74 79 28 70 52 69 67 68 74 2c 20 7a 41  inity(pRight, zA
39d94 66 66 5b 6a 5d 29 3d 3d 53 51 4c 49 54 45 5f 41  ff[j])==SQLITE_A
39d95 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20  FF_NONE ){.     
39d96 20 20 20 20 20 7a 41 66 66 5b 6a 5d 20 3d 20 53       zAff[j] = S
39d97 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a  QLITE_AFF_NONE;.
39d98 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
39d99 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70    if( sqlite3Exp
39d9a 72 4e 65 65 64 73 4e 6f 41 66 66 69 6e 69 74 79  rNeedsNoAffinity
39d9b 43 68 61 6e 67 65 28 70 52 69 67 68 74 2c 20 7a  Change(pRight, z
39d9c 41 66 66 5b 6a 5d 29 20 29 7b 0a 20 20 20 20 20  Aff[j]) ){.     
39d9d 20 20 20 20 20 7a 41 66 66 5b 6a 5d 20 3d 20 53       zAff[j] = S
39d9e 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a  QLITE_AFF_NONE;.
39d9f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
39da0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70  }.    }.  }.  *p
39da1 7a 41 66 66 20 3d 20 7a 41 66 66 3b 0a 20 20 72  zAff = zAff;.  r
39da2 65 74 75 72 6e 20 72 65 67 42 61 73 65 3b 0a 7d  eturn regBase;.}
39da3 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
39da4 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 2f 2a  _OMIT_EXPLAIN./*
39da5 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
39da6 20 69 73 20 61 20 68 65 6c 70 65 72 20 66 6f 72   is a helper for
39da7 20 65 78 70 6c 61 69 6e 49 6e 64 65 78 52 61 6e   explainIndexRan
39da8 67 65 28 29 20 62 65 6c 6f 77 0a 2a 2a 0a 2a 2a  ge() below.**.**
39da9 20 70 53 74 72 20 68 6f 6c 64 73 20 74 68 65 20   pStr holds the 
39daa 74 65 78 74 20 6f 66 20 61 6e 20 65 78 70 72 65  text of an expre
39dab 73 73 69 6f 6e 20 74 68 61 74 20 77 65 20 61 72  ssion that we ar
39dac 65 20 62 75 69 6c 64 69 6e 67 20 75 70 20 6f 6e  e building up on
39dad 65 20 74 65 72 6d 0a 2a 2a 20 61 74 20 61 20 74  e term.** at a t
39dae 69 6d 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ime.  This routi
39daf 6e 65 20 61 64 64 73 20 61 20 6e 65 77 20 74 65  ne adds a new te
39db0 72 6d 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  rm to the end of
39db1 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e   the expression.
39db2 0a 2a 2a 20 54 65 72 6d 73 20 61 72 65 20 73 65  .** Terms are se
39db3 70 61 72 61 74 65 64 20 62 79 20 41 4e 44 20 73  parated by AND s
39db4 6f 20 61 64 64 20 74 68 65 20 22 41 4e 44 22 20  o add the "AND" 
39db5 74 65 78 74 20 66 6f 72 20 73 65 63 6f 6e 64 20  text for second 
39db6 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 0a 2a  and subsequent.*
39db7 2a 20 74 65 72 6d 73 20 6f 6e 6c 79 2e 0a 2a 2f  * terms only..*/
39db8 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70  .static void exp
39db9 6c 61 69 6e 41 70 70 65 6e 64 54 65 72 6d 28 0a  lainAppendTerm(.
39dba 20 20 53 74 72 41 63 63 75 6d 20 2a 70 53 74 72    StrAccum *pStr
39dbb 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
39dbc 20 54 68 65 20 74 65 78 74 20 65 78 70 72 65 73   The text expres
39dbd 73 69 6f 6e 20 62 65 69 6e 67 20 62 75 69 6c 74  sion being built
39dbe 20 2a 2f 0a 20 20 69 6e 74 20 69 54 65 72 6d 2c   */.  int iTerm,
39dbf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39dc0 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
39dc1 69 73 20 74 65 72 6d 2e 20 20 46 69 72 73 74 20  is term.  First 
39dc2 69 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 63 6f 6e  is zero */.  con
39dc3 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e  st char *zColumn
39dc4 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ,        /* Name
39dc5 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a   of the column *
39dc6 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
39dc7 7a 4f 70 20 20 20 20 20 20 20 20 20 20 20 20 20  zOp             
39dc8 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f  /* Name of the o
39dc9 70 65 72 61 74 6f 72 20 2a 2f 0a 29 7b 0a 20 20  perator */.){.  
39dca 69 66 28 20 69 54 65 72 6d 20 29 20 73 71 6c 69  if( iTerm ) sqli
39dcb 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
39dcc 64 28 70 53 74 72 2c 20 22 20 41 4e 44 20 22 2c  d(pStr, " AND ",
39dcd 20 35 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74   5);.  sqlite3St
39dce 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 53 74  rAccumAppend(pSt
39dcf 72 2c 20 7a 43 6f 6c 75 6d 6e 2c 20 2d 31 29 3b  r, zColumn, -1);
39dd0 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  .  sqlite3StrAcc
39dd1 75 6d 41 70 70 65 6e 64 28 70 53 74 72 2c 20 7a  umAppend(pStr, z
39dd2 4f 70 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65  Op, 1);.  sqlite
39dd3 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
39dd4 70 53 74 72 2c 20 22 3f 22 2c 20 31 29 3b 0a 7d  pStr, "?", 1);.}
39dd5 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ../*.** Argument
39dd6 20 70 4c 65 76 65 6c 20 64 65 73 63 72 69 62 65   pLevel describe
39dd7 73 20 61 20 73 74 72 61 74 65 67 79 20 66 6f 72  s a strategy for
39dd8 20 73 63 61 6e 6e 69 6e 67 20 74 61 62 6c 65 20   scanning table 
39dd9 70 54 61 62 2e 20 54 68 69 73 20 0a 2a 2a 20 66  pTab. This .** f
39dda 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
39ddb 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  a pointer to a s
39ddc 74 72 69 6e 67 20 62 75 66 66 65 72 20 63 6f 6e  tring buffer con
39ddd 74 61 69 6e 69 6e 67 20 61 20 64 65 73 63 72 69  taining a descri
39dde 70 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20  ption.** of the 
39ddf 73 75 62 73 65 74 20 6f 66 20 74 61 62 6c 65 20  subset of table 
39de0 72 6f 77 73 20 73 63 61 6e 6e 65 64 20 62 79 20  rows scanned by 
39de1 74 68 65 20 73 74 72 61 74 65 67 79 20 69 6e 20  the strategy in 
39de2 74 68 65 20 66 6f 72 6d 20 6f 66 20 61 6e 0a 2a  the form of an.*
39de3 2a 20 53 51 4c 20 65 78 70 72 65 73 73 69 6f 6e  * SQL expression
39de4 2e 20 4f 72 2c 20 69 66 20 61 6c 6c 20 72 6f 77  . Or, if all row
39de5 73 20 61 72 65 20 73 63 61 6e 6e 65 64 2c 20 4e  s are scanned, N
39de6 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ULL is returned.
39de7 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  .**.** For examp
39de8 6c 65 2c 20 69 66 20 74 68 65 20 71 75 65 72 79  le, if the query
39de9 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54  :.**.**   SELECT
39dea 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45   * FROM t1 WHERE
39deb 20 61 3d 31 20 41 4e 44 20 62 3e 32 3b 0a 2a 2a   a=1 AND b>2;.**
39dec 0a 2a 2a 20 69 73 20 72 75 6e 20 61 6e 64 20 74  .** is run and t
39ded 68 65 72 65 20 69 73 20 61 6e 20 69 6e 64 65 78  here is an index
39dee 20 6f 6e 20 28 61 2c 20 62 29 2c 20 74 68 65 6e   on (a, b), then
39def 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
39df0 65 74 75 72 6e 73 20 61 0a 2a 2a 20 73 74 72 69  eturns a.** stri
39df1 6e 67 20 73 69 6d 69 6c 61 72 20 74 6f 3a 0a 2a  ng similar to:.*
39df2 2a 0a 2a 2a 20 20 20 22 61 3d 3f 20 41 4e 44 20  *.**   "a=? AND 
39df3 62 3e 3f 22 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  b>?".**.** The r
39df4 65 74 75 72 6e 65 64 20 70 6f 69 6e 74 65 72 20  eturned pointer 
39df5 70 6f 69 6e 74 73 20 74 6f 20 6d 65 6d 6f 72 79  points to memory
39df6 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
39df7 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28 29  qlite3DbMalloc()
39df8 2e 0a 2a 2a 20 49 74 20 69 73 20 74 68 65 20 72  ..** It is the r
39df9 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66  esponsibility of
39dfa 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 66   the caller to f
39dfb 72 65 65 20 74 68 65 20 62 75 66 66 65 72 20 77  ree the buffer w
39dfc 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 20  hen it is.** no 
39dfd 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e  longer required.
39dfe 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
39dff 2a 65 78 70 6c 61 69 6e 49 6e 64 65 78 52 61 6e  *explainIndexRan
39e00 67 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ge(sqlite3 *db, 
39e01 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76  WhereLevel *pLev
39e02 65 6c 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29  el, Table *pTab)
39e03 7b 0a 20 20 57 68 65 72 65 50 6c 61 6e 20 2a 70  {.  WherePlan *p
39e04 50 6c 61 6e 20 3d 20 26 70 4c 65 76 65 6c 2d 3e  Plan = &pLevel->
39e05 70 6c 61 6e 3b 0a 20 20 49 6e 64 65 78 20 2a 70  plan;.  Index *p
39e06 49 6e 64 65 78 20 3d 20 70 50 6c 61 6e 2d 3e 75  Index = pPlan->u
39e07 2e 70 49 64 78 3b 0a 20 20 69 6e 74 20 6e 45 71  .pIdx;.  int nEq
39e08 20 3d 20 70 50 6c 61 6e 2d 3e 6e 45 71 3b 0a 20   = pPlan->nEq;. 
39e09 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 43 6f 6c   int i, j;.  Col
39e0a 75 6d 6e 20 2a 61 43 6f 6c 20 3d 20 70 54 61 62  umn *aCol = pTab
39e0b 2d 3e 61 43 6f 6c 3b 0a 20 20 69 6e 74 20 2a 61  ->aCol;.  int *a
39e0c 69 43 6f 6c 75 6d 6e 20 3d 20 70 49 6e 64 65 78  iColumn = pIndex
39e0d 2d 3e 61 69 43 6f 6c 75 6d 6e 3b 0a 20 20 53 74  ->aiColumn;.  St
39e0e 72 41 63 63 75 6d 20 74 78 74 3b 0a 0a 20 20 69  rAccum txt;..  i
39e0f 66 28 20 6e 45 71 3d 3d 30 20 26 26 20 28 70 50  f( nEq==0 && (pP
39e10 6c 61 6e 2d 3e 77 73 46 6c 61 67 73 20 26 20 28  lan->wsFlags & (
39e11 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 7c  WHERE_BTM_LIMIT|
39e12 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 29  WHERE_TOP_LIMIT)
39e13 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
39e14 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  rn 0;.  }.  sqli
39e15 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28  te3StrAccumInit(
39e16 26 74 78 74 2c 20 30 2c 20 30 2c 20 53 51 4c 49  &txt, 0, 0, SQLI
39e17 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 3b 0a  TE_MAX_LENGTH);.
39e18 20 20 74 78 74 2e 64 62 20 3d 20 64 62 3b 0a 20    txt.db = db;. 
39e19 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
39e1a 41 70 70 65 6e 64 28 26 74 78 74 2c 20 22 20 28  Append(&txt, " (
39e1b 22 2c 20 32 29 3b 0a 20 20 66 6f 72 28 69 3d 30  ", 2);.  for(i=0
39e1c 3b 20 69 3c 6e 45 71 3b 20 69 2b 2b 29 7b 0a 20  ; i<nEq; i++){. 
39e1d 20 20 20 65 78 70 6c 61 69 6e 41 70 70 65 6e 64     explainAppend
39e1e 54 65 72 6d 28 26 74 78 74 2c 20 69 2c 20 61 43  Term(&txt, i, aC
39e1f 6f 6c 5b 61 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 2e  ol[aiColumn[i]].
39e20 7a 4e 61 6d 65 2c 20 22 3d 22 29 3b 0a 20 20 7d  zName, "=");.  }
39e21 0a 0a 20 20 6a 20 3d 20 69 3b 0a 20 20 69 66 28  ..  j = i;.  if(
39e22 20 70 50 6c 61 6e 2d 3e 77 73 46 6c 61 67 73 26   pPlan->wsFlags&
39e23 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 20  WHERE_BTM_LIMIT 
39e24 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d  ){.    char *z =
39e25 20 28 6a 3d 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f   (j==pIndex->nCo
39e26 6c 75 6d 6e 20 29 20 3f 20 22 72 6f 77 69 64 22  lumn ) ? "rowid"
39e27 20 3a 20 61 43 6f 6c 5b 61 69 43 6f 6c 75 6d 6e   : aCol[aiColumn
39e28 5b 6a 5d 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  [j]].zName;.    
39e29 65 78 70 6c 61 69 6e 41 70 70 65 6e 64 54 65 72  explainAppendTer
39e2a 6d 28 26 74 78 74 2c 20 69 2b 2b 2c 20 7a 2c 20  m(&txt, i++, z, 
39e2b 22 3e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ">");.  }.  if( 
39e2c 70 50 6c 61 6e 2d 3e 77 73 46 6c 61 67 73 26 57  pPlan->wsFlags&W
39e2d 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 29  HERE_TOP_LIMIT )
39e2e 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d 20  {.    char *z = 
39e2f 28 6a 3d 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c  (j==pIndex->nCol
39e30 75 6d 6e 20 29 20 3f 20 22 72 6f 77 69 64 22 20  umn ) ? "rowid" 
39e31 3a 20 61 43 6f 6c 5b 61 69 43 6f 6c 75 6d 6e 5b  : aCol[aiColumn[
39e32 6a 5d 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 65  j]].zName;.    e
39e33 78 70 6c 61 69 6e 41 70 70 65 6e 64 54 65 72 6d  xplainAppendTerm
39e34 28 26 74 78 74 2c 20 69 2c 20 7a 2c 20 22 3c 22  (&txt, i, z, "<"
39e35 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
39e36 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26  StrAccumAppend(&
39e37 74 78 74 2c 20 22 29 22 2c 20 31 29 3b 0a 20 20  txt, ")", 1);.  
39e38 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74  return sqlite3St
39e39 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 74 78  rAccumFinish(&tx
39e3a 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  t);.}../*.** Thi
39e3b 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
39e3c 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 63 75 72  no-op unless cur
39e3d 72 65 6e 74 6c 79 20 70 72 6f 63 65 73 73 69 6e  rently processin
39e3e 67 20 61 6e 20 45 58 50 4c 41 49 4e 20 51 55 45  g an EXPLAIN QUE
39e3f 52 59 20 50 4c 41 4e 0a 2a 2a 20 63 6f 6d 6d 61  RY PLAN.** comma
39e40 6e 64 2e 20 49 66 20 74 68 65 20 71 75 65 72 79  nd. If the query
39e41 20 62 65 69 6e 67 20 63 6f 6d 70 69 6c 65 64 20   being compiled 
39e42 69 73 20 61 6e 20 45 58 50 4c 41 49 4e 20 51 55  is an EXPLAIN QU
39e43 45 52 59 20 50 4c 41 4e 2c 20 61 20 73 69 6e 67  ERY PLAN, a sing
39e44 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69 73 20  le.** record is 
39e45 61 64 64 65 64 20 74 6f 20 74 68 65 20 6f 75 74  added to the out
39e46 70 75 74 20 74 6f 20 64 65 73 63 72 69 62 65 20  put to describe 
39e47 74 68 65 20 74 61 62 6c 65 20 73 63 61 6e 20 73  the table scan s
39e48 74 72 61 74 65 67 79 20 69 6e 20 0a 2a 2a 20 70  trategy in .** p
39e49 4c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  Level..*/.static
39e4a 20 76 6f 69 64 20 65 78 70 6c 61 69 6e 4f 6e 65   void explainOne
39e4b 53 63 61 6e 28 0a 20 20 50 61 72 73 65 20 2a 70  Scan(.  Parse *p
39e4c 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
39e4d 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
39e4e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
39e4f 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c  cList *pTabList,
39e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39e51 20 54 61 62 6c 65 20 6c 69 73 74 20 74 68 69 73   Table list this
39e52 20 6c 6f 6f 70 20 72 65 66 65 72 73 20 74 6f 20   loop refers to 
39e53 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20  */.  WhereLevel 
39e54 2a 70 4c 65 76 65 6c 2c 20 20 20 20 20 20 20 20  *pLevel,        
39e55 20 20 20 20 20 2f 2a 20 53 63 61 6e 20 74 6f 20       /* Scan to 
39e56 77 72 69 74 65 20 4f 50 5f 45 78 70 6c 61 69 6e  write OP_Explain
39e57 20 6f 70 63 6f 64 65 20 66 6f 72 20 2a 2f 0a 20   opcode for */. 
39e58 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 20 20 20   int iLevel,    
39e59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39e5a 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 6c   /* Value for "l
39e5b 65 76 65 6c 22 20 63 6f 6c 75 6d 6e 20 6f 66 20  evel" column of 
39e5c 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20  output */.  int 
39e5d 69 46 72 6f 6d 2c 20 20 20 20 20 20 20 20 20 20  iFrom,          
39e5e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
39e5f 61 6c 75 65 20 66 6f 72 20 22 66 72 6f 6d 22 20  alue for "from" 
39e60 63 6f 6c 75 6d 6e 20 6f 66 20 6f 75 74 70 75 74  column of output
39e61 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72 6c 46   */.  u16 wctrlF
39e62 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20  lags            
39e63 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70        /* Flags p
39e64 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33  assed to sqlite3
39e65 57 68 65 72 65 42 65 67 69 6e 28 29 20 2a 2f 0a  WhereBegin() */.
39e66 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  ){.  if( pParse-
39e67 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a 20  >explain==2 ){. 
39e68 20 20 20 75 33 32 20 66 6c 61 67 73 20 3d 20 70     u32 flags = p
39e69 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
39e6a 61 67 73 3b 0a 20 20 20 20 73 74 72 75 63 74 20  ags;.    struct 
39e6b 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
39e6c 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d  tem = &pTabList-
39e6d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d  >a[pLevel->iFrom
39e6e 5d 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d  ];.    Vdbe *v =
39e6f 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20   pParse->pVdbe; 
39e70 20 20 20 20 20 2f 2a 20 56 4d 20 62 65 69 6e 67       /* VM being
39e71 20 63 6f 6e 73 74 72 75 63 74 65 64 20 2a 2f 0a   constructed */.
39e72 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
39e73 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
39e74 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
39e75 6e 64 6c 65 20 2a 2f 0a 20 20 20 20 63 68 61 72  ndle */.    char
39e76 20 2a 7a 4d 73 67 3b 20 20 20 20 20 20 20 20 20   *zMsg;         
39e77 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
39e78 74 20 74 6f 20 61 64 64 20 74 6f 20 45 51 50 20  t to add to EQP 
39e79 6f 75 74 70 75 74 20 2a 2f 0a 20 20 20 20 73 71  output */.    sq
39e7a 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 52 6f 77  lite3_int64 nRow
39e7b 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45  ;           /* E
39e7c 78 70 65 63 74 65 64 20 6e 75 6d 62 65 72 20 6f  xpected number o
39e7d 66 20 72 6f 77 73 20 76 69 73 69 74 65 64 20 62  f rows visited b
39e7e 79 20 73 63 61 6e 20 2a 2f 0a 20 20 20 20 69 6e  y scan */.    in
39e7f 74 20 69 49 64 20 3d 20 70 50 61 72 73 65 2d 3e  t iId = pParse->
39e80 69 53 65 6c 65 63 74 49 64 3b 20 20 2f 2a 20 53  iSelectId;  /* S
39e81 65 6c 65 63 74 20 69 64 20 28 6c 65 66 74 2d 6d  elect id (left-m
39e82 6f 73 74 20 6f 75 74 70 75 74 20 63 6f 6c 75 6d  ost output colum
39e83 6e 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 73  n) */.    int is
39e84 53 65 61 72 63 68 3b 20 20 20 20 20 20 20 20 20  Search;         
39e85 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
39e86 66 6f 72 20 61 20 53 45 41 52 43 48 2e 20 46 61  for a SEARCH. Fa
39e87 6c 73 65 20 66 6f 72 20 53 43 41 4e 2e 20 2a 2f  lse for SCAN. */
39e88 0a 0a 20 20 20 20 69 66 28 20 28 66 6c 61 67 73  ..    if( (flags
39e89 26 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 29  &WHERE_MULTI_OR)
39e8a 20 7c 7c 20 28 77 63 74 72 6c 46 6c 61 67 73 26   || (wctrlFlags&
39e8b 57 48 45 52 45 5f 4f 4e 45 54 41 42 4c 45 5f 4f  WHERE_ONETABLE_O
39e8c 4e 4c 59 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a  NLY) ) return;..
39e8d 20 20 20 20 69 73 53 65 61 72 63 68 20 3d 20 28      isSearch = (
39e8e 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 45 71  pLevel->plan.nEq
39e8f 3e 30 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  >0).            
39e90 20 7c 7c 20 28 66 6c 61 67 73 26 28 57 48 45 52   || (flags&(WHER
39e91 45 5f 42 54 4d 5f 4c 49 4d 49 54 7c 57 48 45 52  E_BTM_LIMIT|WHER
39e92 45 5f 54 4f 50 5f 4c 49 4d 49 54 29 29 21 3d 30  E_TOP_LIMIT))!=0
39e93 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  .             ||
39e94 20 28 77 63 74 72 6c 46 6c 61 67 73 26 28 57 48   (wctrlFlags&(WH
39e95 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 7c  ERE_ORDERBY_MIN|
39e96 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 41  WHERE_ORDERBY_MA
39e97 58 29 29 3b 0a 0a 20 20 20 20 7a 4d 73 67 20 3d  X));..    zMsg =
39e98 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
39e99 64 62 2c 20 22 25 73 22 2c 20 69 73 53 65 61 72  db, "%s", isSear
39e9a 63 68 3f 22 53 45 41 52 43 48 22 3a 22 53 43 41  ch?"SEARCH":"SCA
39e9b 4e 22 29 3b 0a 20 20 20 20 69 66 28 20 70 49 74  N");.    if( pIt
39e9c 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  em->pSelect ){. 
39e9d 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69       zMsg = sqli
39e9e 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20  te3MAppendf(db, 
39e9f 7a 4d 73 67 2c 20 22 25 73 20 53 55 42 51 55 45  zMsg, "%s SUBQUE
39ea0 52 59 20 25 64 22 2c 20 7a 4d 73 67 2c 70 49 74  RY %d", zMsg,pIt
39ea1 65 6d 2d 3e 69 53 65 6c 65 63 74 49 64 29 3b 0a  em->iSelectId);.
39ea2 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
39ea3 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d   zMsg = sqlite3M
39ea4 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67  Appendf(db, zMsg
39ea5 2c 20 22 25 73 20 54 41 42 4c 45 20 25 73 22 2c  , "%s TABLE %s",
39ea6 20 7a 4d 73 67 2c 20 70 49 74 65 6d 2d 3e 7a 4e   zMsg, pItem->zN
39ea7 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ame);.    }..   
39ea8 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69   if( pItem->zAli
39ea9 61 73 20 29 7b 0a 20 20 20 20 20 20 7a 4d 73 67  as ){.      zMsg
39eaa 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e   = sqlite3MAppen
39eab 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73  df(db, zMsg, "%s
39eac 20 41 53 20 25 73 22 2c 20 7a 4d 73 67 2c 20 70   AS %s", zMsg, p
39ead 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20  Item->zAlias);. 
39eae 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 6c     }.    if( (fl
39eaf 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45  ags & WHERE_INDE
39eb0 58 45 44 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  XED)!=0 ){.     
39eb1 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20 3d 20   char *zWhere = 
39eb2 65 78 70 6c 61 69 6e 49 6e 64 65 78 52 61 6e 67  explainIndexRang
39eb3 65 28 64 62 2c 20 70 4c 65 76 65 6c 2c 20 70 49  e(db, pLevel, pI
39eb4 74 65 6d 2d 3e 70 54 61 62 29 3b 0a 20 20 20 20  tem->pTab);.    
39eb5 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33    zMsg = sqlite3
39eb6 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73  MAppendf(db, zMs
39eb7 67 2c 20 22 25 73 20 55 53 49 4e 47 20 25 73 25  g, "%s USING %s%
39eb8 73 49 4e 44 45 58 25 73 25 73 25 73 22 2c 20 7a  sINDEX%s%s%s", z
39eb9 4d 73 67 2c 20 0a 20 20 20 20 20 20 20 20 20 20  Msg, .          
39eba 28 28 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f  ((flags & WHERE_
39ebb 54 45 4d 50 5f 49 4e 44 45 58 29 3f 22 41 55 54  TEMP_INDEX)?"AUT
39ebc 4f 4d 41 54 49 43 20 22 3a 22 22 29 2c 0a 20 20  OMATIC ":""),.  
39ebd 20 20 20 20 20 20 20 20 28 28 66 6c 61 67 73 20          ((flags 
39ebe 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  & WHERE_IDX_ONLY
39ebf 29 3f 22 43 4f 56 45 52 49 4e 47 20 22 3a 22 22  )?"COVERING ":""
39ec0 29 2c 0a 20 20 20 20 20 20 20 20 20 20 28 28 66  ),.          ((f
39ec1 6c 61 67 73 20 26 20 57 48 45 52 45 5f 54 45 4d  lags & WHERE_TEM
39ec2 50 5f 49 4e 44 45 58 29 3f 22 22 3a 22 20 22 29  P_INDEX)?"":" ")
39ec3 2c 0a 20 20 20 20 20 20 20 20 20 20 28 28 66 6c  ,.          ((fl
39ec4 61 67 73 20 26 20 57 48 45 52 45 5f 54 45 4d 50  ags & WHERE_TEMP
39ec5 5f 49 4e 44 45 58 29 3f 22 22 3a 20 70 4c 65 76  _INDEX)?"": pLev
39ec6 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d  el->plan.u.pIdx-
39ec7 3e 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 20  >zName),.       
39ec8 20 20 20 7a 57 68 65 72 65 0a 20 20 20 20 20 20     zWhere.      
39ec9 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
39eca 44 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72  DbFree(db, zWher
39ecb 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  e);.    }else if
39ecc 28 20 66 6c 61 67 73 20 26 20 28 57 48 45 52 45  ( flags & (WHERE
39ecd 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f  _ROWID_EQ|WHERE_
39ece 52 4f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a  ROWID_RANGE) ){.
39ecf 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c        zMsg = sql
39ed0 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c  ite3MAppendf(db,
39ed1 20 7a 4d 73 67 2c 20 22 25 73 20 55 53 49 4e 47   zMsg, "%s USING
39ed2 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
39ed3 20 4b 45 59 22 2c 20 7a 4d 73 67 29 3b 0a 0a 20   KEY", zMsg);.. 
39ed4 20 20 20 20 20 69 66 28 20 66 6c 61 67 73 26 57       if( flags&W
39ed5 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 29 7b  HERE_ROWID_EQ ){
39ed6 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20  .        zMsg = 
39ed7 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28  sqlite3MAppendf(
39ed8 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 28 72  db, zMsg, "%s (r
39ed9 6f 77 69 64 3d 3f 29 22 2c 20 7a 4d 73 67 29 3b  owid=?)", zMsg);
39eda 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
39edb 20 28 66 6c 61 67 73 26 57 48 45 52 45 5f 42 4f   (flags&WHERE_BO
39edc 54 48 5f 4c 49 4d 49 54 29 3d 3d 57 48 45 52 45  TH_LIMIT)==WHERE
39edd 5f 42 4f 54 48 5f 4c 49 4d 49 54 20 29 7b 0a 20  _BOTH_LIMIT ){. 
39ede 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71         zMsg = sq
39edf 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62  lite3MAppendf(db
39ee0 2c 20 7a 4d 73 67 2c 20 22 25 73 20 28 72 6f 77  , zMsg, "%s (row
39ee1 69 64 3e 3f 20 41 4e 44 20 72 6f 77 69 64 3c 3f  id>? AND rowid<?
39ee2 29 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20  )", zMsg);.     
39ee3 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73   }else if( flags
39ee4 26 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54  &WHERE_BTM_LIMIT
39ee5 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67   ){.        zMsg
39ee6 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e   = sqlite3MAppen
39ee7 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73  df(db, zMsg, "%s
39ee8 20 28 72 6f 77 69 64 3e 3f 29 22 2c 20 7a 4d 73   (rowid>?)", zMs
39ee9 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  g);.      }else 
39eea 69 66 28 20 66 6c 61 67 73 26 57 48 45 52 45 5f  if( flags&WHERE_
39eeb 54 4f 50 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20  TOP_LIMIT ){.   
39eec 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69       zMsg = sqli
39eed 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20  te3MAppendf(db, 
39eee 7a 4d 73 67 2c 20 22 25 73 20 28 72 6f 77 69 64  zMsg, "%s (rowid
39eef 3c 3f 29 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20  <?)", zMsg);.   
39ef0 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64     }.    }.#ifnd
39ef1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
39ef2 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
39ef3 65 6c 73 65 20 69 66 28 20 28 66 6c 61 67 73 20  else if( (flags 
39ef4 26 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54  & WHERE_VIRTUALT
39ef5 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20 20 20  ABLE)!=0 ){.    
39ef6 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f    sqlite3_index_
39ef7 69 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 20 3d  info *pVtabIdx =
39ef8 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e   pLevel->plan.u.
39ef9 70 56 74 61 62 49 64 78 3b 0a 20 20 20 20 20 20  pVtabIdx;.      
39efa 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41  zMsg = sqlite3MA
39efb 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c  ppendf(db, zMsg,
39efc 20 22 25 73 20 56 49 52 54 55 41 4c 20 54 41 42   "%s VIRTUAL TAB
39efd 4c 45 20 49 4e 44 45 58 20 25 64 3a 25 73 22 2c  LE INDEX %d:%s",
39efe 20 7a 4d 73 67 2c 0a 20 20 20 20 20 20 20 20 20   zMsg,.         
39eff 20 20 20 20 20 20 20 20 20 70 56 74 61 62 49 64           pVtabId
39f00 78 2d 3e 69 64 78 4e 75 6d 2c 20 70 56 74 61 62  x->idxNum, pVtab
39f01 49 64 78 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20  Idx->idxStr);.  
39f02 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69    }.#endif.    i
39f03 66 28 20 77 63 74 72 6c 46 6c 61 67 73 26 28 57  f( wctrlFlags&(W
39f04 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e  HERE_ORDERBY_MIN
39f05 7c 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d  |WHERE_ORDERBY_M
39f06 41 58 29 20 29 7b 0a 20 20 20 20 20 20 74 65 73  AX) ){.      tes
39f07 74 63 61 73 65 28 20 77 63 74 72 6c 46 6c 61 67  tcase( wctrlFlag
39f08 73 20 26 20 57 48 45 52 45 5f 4f 52 44 45 52 42  s & WHERE_ORDERB
39f09 59 5f 4d 49 4e 20 29 3b 0a 20 20 20 20 20 20 6e  Y_MIN );.      n
39f0a 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c  Row = 1;.    }el
39f0b 73 65 7b 0a 20 20 20 20 20 20 6e 52 6f 77 20 3d  se{.      nRow =
39f0c 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
39f0d 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 52 6f  pLevel->plan.nRo
39f0e 77 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4d 73  w;.    }.    zMs
39f0f 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65  g = sqlite3MAppe
39f10 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25  ndf(db, zMsg, "%
39f11 73 20 28 7e 25 6c 6c 64 20 72 6f 77 73 29 22 2c  s (~%lld rows)",
39f12 20 7a 4d 73 67 2c 20 6e 52 6f 77 29 3b 0a 20 20   zMsg, nRow);.  
39f13 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
39f14 4f 70 34 28 76 2c 20 4f 50 5f 45 78 70 6c 61 69  Op4(v, OP_Explai
39f15 6e 2c 20 69 49 64 2c 20 69 4c 65 76 65 6c 2c 20  n, iId, iLevel, 
39f16 69 46 72 6f 6d 2c 20 7a 4d 73 67 2c 20 50 34 5f  iFrom, zMsg, P4_
39f17 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a  DYNAMIC);.  }.}.
39f18 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 65  #else.# define e
39f19 78 70 6c 61 69 6e 4f 6e 65 53 63 61 6e 28 75 2c  xplainOneScan(u,
39f1a 76 2c 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e 64 69  v,w,x,y,z).#endi
39f1b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
39f1c 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 0a 2f 2a  _EXPLAIN */.../*
39f1d 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
39f1e 65 20 66 6f 72 20 74 68 65 20 73 74 61 72 74 20  e for the start 
39f1f 6f 66 20 74 68 65 20 69 4c 65 76 65 6c 2d 74 68  of the iLevel-th
39f20 20 6c 6f 6f 70 20 69 6e 20 74 68 65 20 57 48 45   loop in the WHE
39f21 52 45 20 63 6c 61 75 73 65 0a 2a 2a 20 69 6d 70  RE clause.** imp
39f22 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 65 73 63  lementation desc
39f23 72 69 62 65 64 20 62 79 20 70 57 49 6e 66 6f 2e  ribed by pWInfo.
39f24 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69 74 6d 61  .*/.static Bitma
39f25 73 6b 20 63 6f 64 65 4f 6e 65 4c 6f 6f 70 53 74  sk codeOneLoopSt
39f26 61 72 74 28 0a 20 20 57 68 65 72 65 49 6e 66 6f  art(.  WhereInfo
39f27 20 2a 70 57 49 6e 66 6f 2c 20 20 20 2f 2a 20 43   *pWInfo,   /* C
39f28 6f 6d 70 6c 65 74 65 20 69 6e 66 6f 72 6d 61 74  omplete informat
39f29 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 57 48  ion about the WH
39f2a 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
39f2b 69 6e 74 20 69 4c 65 76 65 6c 2c 20 20 20 20 20  int iLevel,     
39f2c 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 6c 65       /* Which le
39f2d 76 65 6c 20 6f 66 20 70 57 49 6e 66 6f 2d 3e 61  vel of pWInfo->a
39f2e 5b 5d 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 64  [] should be cod
39f2f 65 64 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72  ed */.  u16 wctr
39f30 6c 46 6c 61 67 73 2c 20 20 20 20 20 20 2f 2a 20  lFlags,      /* 
39f31 4f 6e 65 20 6f 66 20 74 68 65 20 57 48 45 52 45  One of the WHERE
39f32 5f 2a 20 66 6c 61 67 73 20 64 65 66 69 6e 65 64  _* flags defined
39f33 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
39f34 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74  */.  Bitmask not
39f35 52 65 61 64 79 20 20 20 20 20 2f 2a 20 57 68 69  Ready     /* Whi
39f36 63 68 20 74 61 62 6c 65 73 20 61 72 65 20 63 75  ch tables are cu
39f37 72 72 65 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c  rrently availabl
39f38 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6a 2c  e */.){.  int j,
39f39 20 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   k;            /
39f3a 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
39f3b 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20  */.  int iCur;  
39f3c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
39f3d 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72   VDBE cursor for
39f3e 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
39f3f 69 6e 74 20 61 64 64 72 4e 78 74 3b 20 20 20 20  int addrNxt;    
39f40 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f       /* Where to
39f41 20 6a 75 6d 70 20 74 6f 20 63 6f 6e 74 69 6e 75   jump to continu
39f42 65 20 77 69 74 68 20 74 68 65 20 6e 65 78 74 20  e with the next 
39f43 49 4e 20 63 61 73 65 20 2a 2f 0a 20 20 69 6e 74  IN case */.  int
39f44 20 6f 6d 69 74 54 61 62 6c 65 3b 20 20 20 20 20   omitTable;     
39f45 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20    /* True if we 
39f46 75 73 65 20 74 68 65 20 69 6e 64 65 78 20 6f 6e  use the index on
39f47 6c 79 20 2a 2f 0a 20 20 69 6e 74 20 62 52 65 76  ly */.  int bRev
39f48 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
39f49 54 72 75 65 20 69 66 20 77 65 20 6e 65 65 64 20  True if we need 
39f4a 74 6f 20 73 63 61 6e 20 69 6e 20 72 65 76 65 72  to scan in rever
39f4b 73 65 20 6f 72 64 65 72 20 2a 2f 0a 20 20 57 68  se order */.  Wh
39f4c 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c  ereLevel *pLevel
39f4d 3b 20 20 2f 2a 20 54 68 65 20 77 68 65 72 65 20  ;  /* The where 
39f4e 6c 65 76 65 6c 20 74 6f 20 62 65 20 63 6f 64 65  level to be code
39f4f 64 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75  d */.  WhereClau
39f50 73 65 20 2a 70 57 43 3b 20 20 20 20 2f 2a 20 44  se *pWC;    /* D
39f51 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20  ecomposition of 
39f52 74 68 65 20 65 6e 74 69 72 65 20 57 48 45 52 45  the entire WHERE
39f53 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65   clause */.  Whe
39f54 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20  reTerm *pTerm;  
39f55 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39f56 41 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  A WHERE clause t
39f57 65 72 6d 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  erm */.  Parse *
39f58 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20  pParse;         
39f59 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
39f5a 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
39f5b 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20   Vdbe *v;       
39f5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39f5d 20 2f 2a 20 54 68 65 20 70 72 65 70 61 72 65 64   /* The prepared
39f5e 20 73 74 6d 74 20 75 6e 64 65 72 20 63 6f 6e 73   stmt under cons
39f5f 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 73  tructions */.  s
39f60 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
39f61 65 6d 20 2a 70 54 61 62 49 74 65 6d 3b 20 20 2f  em *pTabItem;  /
39f62 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65  * FROM clause te
39f63 72 6d 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a  rm being coded *
39f64 2f 0a 20 20 69 6e 74 20 61 64 64 72 42 72 6b 3b  /.  int addrBrk;
39f65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39f66 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
39f67 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66   to break out of
39f68 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69   the loop */.  i
39f69 6e 74 20 61 64 64 72 43 6f 6e 74 3b 20 20 20 20  nt addrCont;    
39f6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39f6b 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63  * Jump here to c
39f6c 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 6e 65 78  ontinue with nex
39f6d 74 20 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74  t cycle */.  int
39f6e 20 69 52 6f 77 69 64 52 65 67 20 3d 20 30 3b 20   iRowidReg = 0; 
39f6f 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20         /* Rowid 
39f70 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 69  is stored in thi
39f71 73 20 72 65 67 69 73 74 65 72 2c 20 69 66 20 6e  s register, if n
39f72 6f 74 20 7a 65 72 6f 20 2a 2f 0a 20 20 69 6e 74  ot zero */.  int
39f73 20 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20 30   iReleaseReg = 0
39f74 3b 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 72  ;      /* Temp r
39f75 65 67 69 73 74 65 72 20 74 6f 20 66 72 65 65 20  egister to free 
39f76 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
39f77 20 2a 2f 0a 0a 20 20 70 50 61 72 73 65 20 3d 20   */..  pParse = 
39f78 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 3b 0a  pWInfo->pParse;.
39f79 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56    v = pParse->pV
39f7a 64 62 65 3b 0a 20 20 70 57 43 20 3d 20 70 57 49  dbe;.  pWC = pWI
39f7b 6e 66 6f 2d 3e 70 57 43 3b 0a 20 20 70 4c 65 76  nfo->pWC;.  pLev
39f7c 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b  el = &pWInfo->a[
39f7d 69 4c 65 76 65 6c 5d 3b 0a 20 20 70 54 61 62 49  iLevel];.  pTabI
39f7e 74 65 6d 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 70  tem = &pWInfo->p
39f7f 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65  TabList->a[pLeve
39f80 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 69 43 75  l->iFrom];.  iCu
39f81 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69 43  r = pTabItem->iC
39f82 75 72 73 6f 72 3b 0a 20 20 62 52 65 76 20 3d 20  ursor;.  bRev = 
39f83 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
39f84 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 45  Flags & WHERE_RE
39f85 56 45 52 53 45 29 21 3d 30 3b 0a 20 20 6f 6d 69  VERSE)!=0;.  omi
39f86 74 54 61 62 6c 65 20 3d 20 28 70 4c 65 76 65 6c  tTable = (pLevel
39f87 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
39f88 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29   WHERE_IDX_ONLY)
39f89 21 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 20  !=0 .           
39f8a 26 26 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26  && (wctrlFlags &
39f8b 20 57 48 45 52 45 5f 46 4f 52 43 45 5f 54 41 42   WHERE_FORCE_TAB
39f8c 4c 45 29 3d 3d 30 3b 0a 0a 20 20 2f 2a 20 43 72  LE)==0;..  /* Cr
39f8d 65 61 74 65 20 6c 61 62 65 6c 73 20 66 6f 72 20  eate labels for 
39f8e 74 68 65 20 22 62 72 65 61 6b 22 20 61 6e 64 20  the "break" and 
39f8f 22 63 6f 6e 74 69 6e 75 65 22 20 69 6e 73 74 72  "continue" instr
39f90 75 63 74 69 6f 6e 73 0a 20 20 2a 2a 20 66 6f 72  uctions.  ** for
39f91 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 6f   the current loo
39f92 70 2e 20 20 4a 75 6d 70 20 74 6f 20 61 64 64 72  p.  Jump to addr
39f93 42 72 6b 20 74 6f 20 62 72 65 61 6b 20 6f 75 74  Brk to break out
39f94 20 6f 66 20 61 20 6c 6f 6f 70 2e 0a 20 20 2a 2a   of a loop..  **
39f95 20 4a 75 6d 70 20 74 6f 20 63 6f 6e 74 20 74 6f   Jump to cont to
39f96 20 67 6f 20 69 6d 6d 65 64 69 61 74 65 6c 79 20   go immediately 
39f97 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74 65 72  to the next iter
39f98 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 2a  ation of the.  *
39f99 2a 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a  * loop..  **.  *
39f9a 2a 20 57 68 65 6e 20 74 68 65 72 65 20 69 73 20  * When there is 
39f9b 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 2c 20  an IN operator, 
39f9c 77 65 20 61 6c 73 6f 20 68 61 76 65 20 61 20 22  we also have a "
39f9d 61 64 64 72 4e 78 74 22 20 6c 61 62 65 6c 20 74  addrNxt" label t
39f9e 68 61 74 0a 20 20 2a 2a 20 6d 65 61 6e 73 20 74  hat.  ** means t
39f9f 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20  o continue with 
39fa0 74 68 65 20 6e 65 78 74 20 49 4e 20 76 61 6c 75  the next IN valu
39fa1 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 2e 20 20  e combination.  
39fa2 57 68 65 6e 0a 20 20 2a 2a 20 74 68 65 72 65 20  When.  ** there 
39fa3 61 72 65 20 6e 6f 20 49 4e 20 6f 70 65 72 61 74  are no IN operat
39fa4 6f 72 73 20 69 6e 20 74 68 65 20 63 6f 6e 73 74  ors in the const
39fa5 72 61 69 6e 74 73 2c 20 74 68 65 20 22 61 64 64  raints, the "add
39fa6 72 4e 78 74 22 20 6c 61 62 65 6c 0a 20 20 2a 2a  rNxt" label.  **
39fa7 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
39fa8 22 61 64 64 72 42 72 6b 22 2e 0a 20 20 2a 2f 0a  "addrBrk"..  */.
39fa9 20 20 61 64 64 72 42 72 6b 20 3d 20 70 4c 65 76    addrBrk = pLev
39faa 65 6c 2d 3e 61 64 64 72 42 72 6b 20 3d 20 70 4c  el->addrBrk = pL
39fab 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 20 3d 20  evel->addrNxt = 
39fac 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
39fad 61 62 65 6c 28 76 29 3b 0a 20 20 61 64 64 72 43  abel(v);.  addrC
39fae 6f 6e 74 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64  ont = pLevel->ad
39faf 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33  drCont = sqlite3
39fb0 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
39fb1 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  ;..  /* If this 
39fb2 69 73 20 74 68 65 20 72 69 67 68 74 20 74 61 62  is the right tab
39fb3 6c 65 20 6f 66 20 61 20 4c 45 46 54 20 4f 55 54  le of a LEFT OUT
39fb4 45 52 20 4a 4f 49 4e 2c 20 61 6c 6c 6f 63 61 74  ER JOIN, allocat
39fb5 65 20 61 6e 64 0a 20 20 2a 2a 20 69 6e 69 74 69  e and.  ** initi
39fb6 61 6c 69 7a 65 20 61 20 6d 65 6d 6f 72 79 20 63  alize a memory c
39fb7 65 6c 6c 20 74 68 61 74 20 72 65 63 6f 72 64 73  ell that records
39fb8 20 69 66 20 74 68 69 73 20 74 61 62 6c 65 20 6d   if this table m
39fb9 61 74 63 68 65 73 20 61 6e 79 0a 20 20 2a 2a 20  atches any.  ** 
39fba 72 6f 77 20 6f 66 20 74 68 65 20 6c 65 66 74 20  row of the left 
39fbb 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69  table of the joi
39fbc 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c  n..  */.  if( pL
39fbd 65 76 65 6c 2d 3e 69 46 72 6f 6d 3e 30 20 26 26  evel->iFrom>0 &&
39fbe 20 28 70 54 61 62 49 74 65 6d 5b 30 5d 2e 6a 6f   (pTabItem[0].jo
39fbf 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45 46 54  intype & JT_LEFT
39fc0 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 4c 65 76  )!=0 ){.    pLev
39fc1 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 3d 20  el->iLeftJoin = 
39fc2 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
39fc3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
39fc4 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
39fc5 67 65 72 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e  ger, 0, pLevel->
39fc6 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20  iLeftJoin);.    
39fc7 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
39fc8 22 69 6e 69 74 20 4c 45 46 54 20 4a 4f 49 4e 20  "init LEFT JOIN 
39fc9 6e 6f 2d 6d 61 74 63 68 20 66 6c 61 67 22 29 29  no-match flag"))
39fca 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 70 65 63  ;.  }..  /* Spec
39fcb 69 61 6c 20 63 61 73 65 20 6f 66 20 61 20 46 52  ial case of a FR
39fcc 4f 4d 20 63 6c 61 75 73 65 20 73 75 62 71 75 65  OM clause subque
39fcd 72 79 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61  ry implemented a
39fce 73 20 61 20 63 6f 2d 72 6f 75 74 69 6e 65 20 2a  s a co-routine *
39fcf 2f 0a 20 20 69 66 28 20 70 54 61 62 49 74 65 6d  /.  if( pTabItem
39fd0 2d 3e 76 69 61 43 6f 72 6f 75 74 69 6e 65 20 29  ->viaCoroutine )
39fd1 7b 0a 20 20 20 20 69 6e 74 20 72 65 67 59 69 65  {.    int regYie
39fd2 6c 64 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 72  ld = pTabItem->r
39fd3 65 67 52 65 74 75 72 6e 3b 0a 20 20 20 20 73 71  egReturn;.    sq
39fd4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
39fd5 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70  v, OP_Integer, p
39fd6 54 61 62 49 74 65 6d 2d 3e 61 64 64 72 46 69 6c  TabItem->addrFil
39fd7 6c 53 75 62 2d 31 2c 20 72 65 67 59 69 65 6c 64  lSub-1, regYield
39fd8 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70  );.    pLevel->p
39fd9 32 20 3d 20 20 73 71 6c 69 74 65 33 56 64 62 65  2 =  sqlite3Vdbe
39fda 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65  AddOp1(v, OP_Yie
39fdb 6c 64 2c 20 72 65 67 59 69 65 6c 64 29 3b 0a 20  ld, regYield);. 
39fdc 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
39fdd 76 2c 20 22 6e 65 78 74 20 72 6f 77 20 6f 66 20  v, "next row of 
39fde 63 6f 2d 72 6f 75 74 69 6e 65 20 25 73 22 2c 20  co-routine %s", 
39fdf 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2d 3e  pTabItem->pTab->
39fe0 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 73 71 6c  zName));.    sql
39fe1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
39fe2 2c 20 4f 50 5f 49 66 2c 20 72 65 67 59 69 65 6c  , OP_If, regYiel
39fe3 64 2b 31 2c 20 61 64 64 72 42 72 6b 29 3b 0a 20  d+1, addrBrk);. 
39fe4 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20     pLevel->op = 
39fe5 4f 50 5f 47 6f 74 6f 3b 0a 20 20 7d 65 6c 73 65  OP_Goto;.  }else
39fe6 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
39fe7 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
39fe8 4c 45 0a 20 20 69 66 28 20 20 28 70 4c 65 76 65  LE.  if(  (pLeve
39fe9 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
39fea 26 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54  & WHERE_VIRTUALT
39feb 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20 20 20  ABLE)!=0 ){.    
39fec 2f 2a 20 43 61 73 65 20 30 3a 20 20 54 68 65 20  /* Case 0:  The 
39fed 74 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 75  table is a virtu
39fee 61 6c 2d 74 61 62 6c 65 2e 20 20 55 73 65 20 74  al-table.  Use t
39fef 68 65 20 56 46 69 6c 74 65 72 20 61 6e 64 20 56  he VFilter and V
39ff0 4e 65 78 74 0a 20 20 20 20 2a 2a 20 20 20 20 20  Next.    **     
39ff1 20 20 20 20 20 74 6f 20 61 63 63 65 73 73 20 74       to access t
39ff2 68 65 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a  he data..    */.
39ff3 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20      int iReg;   
39ff4 2f 2a 20 50 33 20 56 61 6c 75 65 20 66 6f 72 20  /* P3 Value for 
39ff5 4f 50 5f 56 46 69 6c 74 65 72 20 2a 2f 0a 20 20  OP_VFilter */.  
39ff6 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f    sqlite3_index_
39ff7 69 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 20 3d  info *pVtabIdx =
39ff8 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e   pLevel->plan.u.
39ff9 70 56 74 61 62 49 64 78 3b 0a 20 20 20 20 69 6e  pVtabIdx;.    in
39ffa 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20  t nConstraint = 
39ffb 70 56 74 61 62 49 64 78 2d 3e 6e 43 6f 6e 73 74  pVtabIdx->nConst
39ffc 72 61 69 6e 74 3b 0a 20 20 20 20 73 74 72 75 63  raint;.    struc
39ffd 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
39ffe 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65  constraint_usage
39fff 20 2a 61 55 73 61 67 65 20 3d 0a 20 20 20 20 20   *aUsage =.     
3a000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a001 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a002 20 20 20 20 20 20 20 20 20 20 20 70 56 74 61 62             pVtab
3a003 49 64 78 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  Idx->aConstraint
3a004 55 73 61 67 65 3b 0a 20 20 20 20 63 6f 6e 73 74  Usage;.    const
3a005 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
3a006 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
3a007 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 0a   *aConstraint =.
3a008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a009 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a00a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a00b 70 56 74 61 62 49 64 78 2d 3e 61 43 6f 6e 73 74  pVtabIdx->aConst
3a00c 72 61 69 6e 74 3b 0a 0a 20 20 20 20 73 71 6c 69  raint;..    sqli
3a00d 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68  te3ExprCachePush
3a00e 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 52  (pParse);.    iR
3a00f 65 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  eg = sqlite3GetT
3a010 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
3a011 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 32 29 3b   nConstraint+2);
3a012 0a 20 20 20 20 66 6f 72 28 6a 3d 31 3b 20 6a 3c  .    for(j=1; j<
3a013 3d 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6a 2b  =nConstraint; j+
3a014 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d  +){.      for(k=
3a015 30 3b 20 6b 3c 6e 43 6f 6e 73 74 72 61 69 6e 74  0; k<nConstraint
3a016 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; k++){.        
3a017 69 66 28 20 61 55 73 61 67 65 5b 6b 5d 2e 61 72  if( aUsage[k].ar
3a018 67 76 49 6e 64 65 78 3d 3d 6a 20 29 7b 0a 20 20  gvIndex==j ){.  
3a019 20 20 20 20 20 20 20 20 69 6e 74 20 69 54 65 72          int iTer
3a01a 6d 20 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  m = aConstraint[
3a01b 6b 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a  k].iTermOffset;.
3a01c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3a01d 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
3a01e 2c 20 70 57 43 2d 3e 61 5b 69 54 65 72 6d 5d 2e  , pWC->a[iTerm].
3a01f 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 69  pExpr->pRight, i
3a020 52 65 67 2b 6a 2b 31 29 3b 0a 20 20 20 20 20 20  Reg+j+1);.      
3a021 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3a022 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
3a023 20 20 20 69 66 28 20 6b 3d 3d 6e 43 6f 6e 73 74     if( k==nConst
3a024 72 61 69 6e 74 20 29 20 62 72 65 61 6b 3b 0a 20  raint ) break;. 
3a025 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
3a026 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
3a027 5f 49 6e 74 65 67 65 72 2c 20 70 56 74 61 62 49  _Integer, pVtabI
3a028 64 78 2d 3e 69 64 78 4e 75 6d 2c 20 69 52 65 67  dx->idxNum, iReg
3a029 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
3a02a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
3a02b 6e 74 65 67 65 72 2c 20 6a 2d 31 2c 20 69 52 65  nteger, j-1, iRe
3a02c 67 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  g+1);.    sqlite
3a02d 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
3a02e 50 5f 56 46 69 6c 74 65 72 2c 20 69 43 75 72 2c  P_VFilter, iCur,
3a02f 20 61 64 64 72 42 72 6b 2c 20 69 52 65 67 2c 20   addrBrk, iReg, 
3a030 70 56 74 61 62 49 64 78 2d 3e 69 64 78 53 74 72  pVtabIdx->idxStr
3a031 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
3a032 20 20 20 20 20 20 20 20 70 56 74 61 62 49 64 78          pVtabIdx
3a033 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53  ->needToFreeIdxS
3a034 74 72 20 3f 20 50 34 5f 4d 50 52 49 4e 54 46 20  tr ? P4_MPRINTF 
3a035 3a 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  : P4_STATIC);.  
3a036 20 20 70 56 74 61 62 49 64 78 2d 3e 6e 65 65 64    pVtabIdx->need
3a037 54 6f 46 72 65 65 49 64 78 53 74 72 20 3d 20 30  ToFreeIdxStr = 0
3a038 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  ;.    for(j=0; j
3a039 3c 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6a 2b  <nConstraint; j+
3a03a 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 55  +){.      if( aU
3a03b 73 61 67 65 5b 6a 5d 2e 6f 6d 69 74 20 29 7b 0a  sage[j].omit ){.
3a03c 20 20 20 20 20 20 20 20 69 6e 74 20 69 54 65 72          int iTer
3a03d 6d 20 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  m = aConstraint[
3a03e 6a 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a  j].iTermOffset;.
3a03f 20 20 20 20 20 20 20 20 64 69 73 61 62 6c 65 54          disableT
3a040 65 72 6d 28 70 4c 65 76 65 6c 2c 20 26 70 57 43  erm(pLevel, &pWC
3a041 2d 3e 61 5b 69 54 65 72 6d 5d 29 3b 0a 20 20 20  ->a[iTerm]);.   
3a042 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
3a043 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 56  Level->op = OP_V
3a044 4e 65 78 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c  Next;.    pLevel
3a045 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20  ->p1 = iCur;.   
3a046 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 71   pLevel->p2 = sq
3a047 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
3a048 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71 6c  Addr(v);.    sql
3a049 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
3a04a 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 52 65  ange(pParse, iRe
3a04b 67 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 32  g, nConstraint+2
3a04c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
3a04d 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73  prCachePop(pPars
3a04e 65 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 0a 23  e, 1);.  }else.#
3a04f 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
3a050 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
3a051 45 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4c 65 76  E */..  if( pLev
3a052 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
3a053 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45   & WHERE_ROWID_E
3a054 51 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65  Q ){.    /* Case
3a055 20 31 3a 20 20 57 65 20 63 61 6e 20 64 69 72 65   1:  We can dire
3a056 63 74 6c 79 20 72 65 66 65 72 65 6e 63 65 20 61  ctly reference a
3a057 20 73 69 6e 67 6c 65 20 72 6f 77 20 75 73 69 6e   single row usin
3a058 67 20 61 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20  g an.    **     
3a059 20 20 20 20 20 65 71 75 61 6c 69 74 79 20 63 6f       equality co
3a05a 6d 70 61 72 69 73 6f 6e 20 61 67 61 69 6e 73 74  mparison against
3a05b 20 74 68 65 20 52 4f 57 49 44 20 66 69 65 6c 64   the ROWID field
3a05c 2e 20 20 4f 72 0a 20 20 20 20 2a 2a 20 20 20 20  .  Or.    **    
3a05d 20 20 20 20 20 20 77 65 20 72 65 66 65 72 65 6e        we referen
3a05e 63 65 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73  ce multiple rows
3a05f 20 75 73 69 6e 67 20 61 20 22 72 6f 77 69 64 20   using a "rowid 
3a060 49 4e 20 28 2e 2e 2e 29 22 0a 20 20 20 20 2a 2a  IN (...)".    **
3a061 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 72            constr
3a062 75 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  uct..    */.    
3a063 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20 73 71  iReleaseReg = sq
3a064 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
3a065 70 50 61 72 73 65 29 3b 0a 20 20 20 20 70 54 65  pParse);.    pTe
3a066 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57  rm = findTerm(pW
3a067 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74  C, iCur, -1, not
3a068 52 65 61 64 79 2c 20 57 4f 5f 45 51 7c 57 4f 5f  Ready, WO_EQ|WO_
3a069 49 4e 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65  IN, 0);.    asse
3a06a 72 74 28 20 70 54 65 72 6d 21 3d 30 20 29 3b 0a  rt( pTerm!=0 );.
3a06b 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72      assert( pTer
3a06c 6d 2d 3e 70 45 78 70 72 21 3d 30 20 29 3b 0a 20  m->pExpr!=0 );. 
3a06d 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d     assert( pTerm
3a06e 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43  ->leftCursor==iC
3a06f 75 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ur );.    assert
3a070 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29  ( omitTable==0 )
3a071 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
3a072 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  pTerm->wtFlags &
3a073 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b   TERM_VIRTUAL );
3a074 20 2f 2a 20 45 56 3a 20 52 2d 33 30 35 37 35 2d   /* EV: R-30575-
3a075 31 31 36 36 32 20 2a 2f 0a 20 20 20 20 69 52 6f  11662 */.    iRo
3a076 77 69 64 52 65 67 20 3d 20 63 6f 64 65 45 71 75  widReg = codeEqu
3a077 61 6c 69 74 79 54 65 72 6d 28 70 50 61 72 73 65  alityTerm(pParse
3a078 2c 20 70 54 65 72 6d 2c 20 70 4c 65 76 65 6c 2c  , pTerm, pLevel,
3a079 20 69 52 65 6c 65 61 73 65 52 65 67 29 3b 0a 20   iReleaseReg);. 
3a07a 20 20 20 61 64 64 72 4e 78 74 20 3d 20 70 4c 65     addrNxt = pLe
3a07b 76 65 6c 2d 3e 61 64 64 72 4e 78 74 3b 0a 20 20  vel->addrNxt;.  
3a07c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
3a07d 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65  Op2(v, OP_MustBe
3a07e 49 6e 74 2c 20 69 52 6f 77 69 64 52 65 67 2c 20  Int, iRowidReg, 
3a07f 61 64 64 72 4e 78 74 29 3b 0a 20 20 20 20 73 71  addrNxt);.    sq
3a080 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
3a081 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
3a082 20 69 43 75 72 2c 20 61 64 64 72 4e 78 74 2c 20   iCur, addrNxt, 
3a083 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20  iRowidReg);.    
3a084 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
3a085 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20 69 43  Store(pParse, iC
3a086 75 72 2c 20 2d 31 2c 20 69 52 6f 77 69 64 52 65  ur, -1, iRowidRe
3a087 67 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  g);.    VdbeComm
3a088 65 6e 74 28 28 76 2c 20 22 70 6b 22 29 29 3b 0a  ent((v, "pk"));.
3a089 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d      pLevel->op =
3a08a 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 7d 65 6c 73   OP_Noop;.  }els
3a08b 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c  e if( pLevel->pl
3a08c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
3a08d 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20 29  RE_ROWID_RANGE )
3a08e 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 32 3a  {.    /* Case 2:
3a08f 20 20 57 65 20 68 61 76 65 20 61 6e 20 69 6e 65    We have an ine
3a090 71 75 61 6c 69 74 79 20 63 6f 6d 70 61 72 69 73  quality comparis
3a091 6f 6e 20 61 67 61 69 6e 73 74 20 74 68 65 20 52  on against the R
3a092 4f 57 49 44 20 66 69 65 6c 64 2e 0a 20 20 20 20  OWID field..    
3a093 2a 2f 0a 20 20 20 20 69 6e 74 20 74 65 73 74 4f  */.    int testO
3a094 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20  p = OP_Noop;.   
3a095 20 69 6e 74 20 73 74 61 72 74 3b 0a 20 20 20 20   int start;.    
3a096 69 6e 74 20 6d 65 6d 45 6e 64 56 61 6c 75 65 20  int memEndValue 
3a097 3d 20 30 3b 0a 20 20 20 20 57 68 65 72 65 54 65  = 0;.    WhereTe
3a098 72 6d 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e  rm *pStart, *pEn
3a099 64 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  d;..    assert( 
3a09a 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  omitTable==0 );.
3a09b 20 20 20 20 70 53 74 61 72 74 20 3d 20 66 69 6e      pStart = fin
3a09c 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c  dTerm(pWC, iCur,
3a09d 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57   -1, notReady, W
3a09e 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 30 29 3b 0a  O_GT|WO_GE, 0);.
3a09f 20 20 20 20 70 45 6e 64 20 3d 20 66 69 6e 64 54      pEnd = findT
3a0a0 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d  erm(pWC, iCur, -
3a0a1 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f  1, notReady, WO_
3a0a2 4c 54 7c 57 4f 5f 4c 45 2c 20 30 29 3b 0a 20 20  LT|WO_LE, 0);.  
3a0a3 20 20 69 66 28 20 62 52 65 76 20 29 7b 0a 20 20    if( bRev ){.  
3a0a4 20 20 20 20 70 54 65 72 6d 20 3d 20 70 53 74 61      pTerm = pSta
3a0a5 72 74 3b 0a 20 20 20 20 20 20 70 53 74 61 72 74  rt;.      pStart
3a0a6 20 3d 20 70 45 6e 64 3b 0a 20 20 20 20 20 20 70   = pEnd;.      p
3a0a7 45 6e 64 20 3d 20 70 54 65 72 6d 3b 0a 20 20 20  End = pTerm;.   
3a0a8 20 7d 0a 20 20 20 20 69 66 28 20 70 53 74 61 72   }.    if( pStar
3a0a9 74 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  t ){.      Expr 
3a0aa 2a 70 58 3b 20 20 20 20 20 20 20 20 20 20 20 20  *pX;            
3a0ab 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
3a0ac 6f 6e 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  on that defines 
3a0ad 74 68 65 20 73 74 61 72 74 20 62 6f 75 6e 64 20  the start bound 
3a0ae 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 31 2c  */.      int r1,
3a0af 20 72 54 65 6d 70 3b 20 20 20 20 20 20 20 20 2f   rTemp;        /
3a0b0 2a 20 52 65 67 69 73 74 65 72 73 20 66 6f 72 20  * Registers for 
3a0b1 68 6f 6c 64 69 6e 67 20 74 68 65 20 73 74 61 72  holding the star
3a0b2 74 20 62 6f 75 6e 64 61 72 79 20 2a 2f 0a 0a 20  t boundary */.. 
3a0b3 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c       /* The foll
3a0b4 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 6d  owing constant m
3a0b5 61 70 73 20 54 4b 5f 78 78 20 63 6f 64 65 73 20  aps TK_xx codes 
3a0b6 69 6e 74 6f 20 63 6f 72 72 65 73 70 6f 6e 64 69  into correspondi
3a0b7 6e 67 20 0a 20 20 20 20 20 20 2a 2a 20 73 65 65  ng .      ** see
3a0b8 6b 20 6f 70 63 6f 64 65 73 2e 20 20 49 74 20 64  k opcodes.  It d
3a0b9 65 70 65 6e 64 73 20 6f 6e 20 61 20 70 61 72 74  epends on a part
3a0ba 69 63 75 6c 61 72 20 6f 72 64 65 72 69 6e 67 20  icular ordering 
3a0bb 6f 66 20 54 4b 5f 78 78 0a 20 20 20 20 20 20 2a  of TK_xx.      *
3a0bc 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 38  /.      const u8
3a0bd 20 61 4d 6f 76 65 4f 70 5b 5d 20 3d 20 7b 0a 20   aMoveOp[] = {. 
3a0be 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f            /* TK_
3a0bf 47 54 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 47 74  GT */  OP_SeekGt
3a0c0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,.           /* 
3a0c1 54 4b 5f 4c 45 20 2a 2f 20 20 4f 50 5f 53 65 65  TK_LE */  OP_See
3a0c2 6b 4c 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  kLe,.           
3a0c3 2f 2a 20 54 4b 5f 4c 54 20 2a 2f 20 20 4f 50 5f  /* TK_LT */  OP_
3a0c4 53 65 65 6b 4c 74 2c 0a 20 20 20 20 20 20 20 20  SeekLt,.        
3a0c5 20 20 20 2f 2a 20 54 4b 5f 47 45 20 2a 2f 20 20     /* TK_GE */  
3a0c6 4f 50 5f 53 65 65 6b 47 65 0a 20 20 20 20 20 20  OP_SeekGe.      
3a0c7 7d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  };.      assert(
3a0c8 20 54 4b 5f 4c 45 3d 3d 54 4b 5f 47 54 2b 31 20   TK_LE==TK_GT+1 
3a0c9 29 3b 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20  );      /* Make 
3a0ca 73 75 72 65 20 74 68 65 20 6f 72 64 65 72 69 6e  sure the orderin
3a0cb 67 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  g.. */.      ass
3a0cc 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 54 4b 5f 47  ert( TK_LT==TK_G
3a0cd 54 2b 32 20 29 3b 20 20 20 20 20 20 2f 2a 20 20  T+2 );      /*  
3a0ce 2e 2e 2e 20 6f 66 20 74 68 65 20 54 4b 5f 78 78  ... of the TK_xx
3a0cf 20 76 61 6c 75 65 73 2e 2e 2e 20 2a 2f 0a 20 20   values... */.  
3a0d0 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47      assert( TK_G
3a0d1 45 3d 3d 54 4b 5f 47 54 2b 33 20 29 3b 20 20 20  E==TK_GT+3 );   
3a0d2 20 20 20 2f 2a 20 20 2e 2e 2e 20 69 73 20 63 6f     /*  ... is co
3a0d3 72 72 65 63 63 74 2e 20 2a 2f 0a 0a 20 20 20 20  rrecct. */..    
3a0d4 20 20 74 65 73 74 63 61 73 65 28 20 70 53 74 61    testcase( pSta
3a0d5 72 74 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rt->wtFlags & TE
3a0d6 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 20 2f 2a  RM_VIRTUAL ); /*
3a0d7 20 45 56 3a 20 52 2d 33 30 35 37 35 2d 31 31 36   EV: R-30575-116
3a0d8 36 32 20 2a 2f 0a 20 20 20 20 20 20 70 58 20 3d  62 */.      pX =
3a0d9 20 70 53 74 61 72 74 2d 3e 70 45 78 70 72 3b 0a   pStart->pExpr;.
3a0da 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 58        assert( pX
3a0db 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  !=0 );.      ass
3a0dc 65 72 74 28 20 70 53 74 61 72 74 2d 3e 6c 65 66  ert( pStart->lef
3a0dd 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 3b  tCursor==iCur );
3a0de 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69  .      r1 = sqli
3a0df 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
3a0e0 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67  pParse, pX->pRig
3a0e1 68 74 2c 20 26 72 54 65 6d 70 29 3b 0a 20 20 20  ht, &rTemp);.   
3a0e2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
3a0e3 64 4f 70 33 28 76 2c 20 61 4d 6f 76 65 4f 70 5b  dOp3(v, aMoveOp[
3a0e4 70 58 2d 3e 6f 70 2d 54 4b 5f 47 54 5d 2c 20 69  pX->op-TK_GT], i
3a0e5 43 75 72 2c 20 61 64 64 72 42 72 6b 2c 20 72 31  Cur, addrBrk, r1
3a0e6 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
3a0e7 6d 65 6e 74 28 28 76 2c 20 22 70 6b 22 29 29 3b  ment((v, "pk"));
3a0e8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
3a0e9 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
3a0ea 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31  hange(pParse, r1
3a0eb 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
3a0ec 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
3a0ed 67 28 70 50 61 72 73 65 2c 20 72 54 65 6d 70 29  g(pParse, rTemp)
3a0ee 3b 0a 20 20 20 20 20 20 64 69 73 61 62 6c 65 54  ;.      disableT
3a0ef 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 53 74 61  erm(pLevel, pSta
3a0f0 72 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  rt);.    }else{.
3a0f1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
3a0f2 65 41 64 64 4f 70 32 28 76 2c 20 62 52 65 76 20  eAddOp2(v, bRev 
3a0f3 3f 20 4f 50 5f 4c 61 73 74 20 3a 20 4f 50 5f 52  ? OP_Last : OP_R
3a0f4 65 77 69 6e 64 2c 20 69 43 75 72 2c 20 61 64 64  ewind, iCur, add
3a0f5 72 42 72 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rBrk);.    }.   
3a0f6 20 69 66 28 20 70 45 6e 64 20 29 7b 0a 20 20 20   if( pEnd ){.   
3a0f7 20 20 20 45 78 70 72 20 2a 70 58 3b 0a 20 20 20     Expr *pX;.   
3a0f8 20 20 20 70 58 20 3d 20 70 45 6e 64 2d 3e 70 45     pX = pEnd->pE
3a0f9 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72  xpr;.      asser
3a0fa 74 28 20 70 58 21 3d 30 20 29 3b 0a 20 20 20 20  t( pX!=0 );.    
3a0fb 20 20 61 73 73 65 72 74 28 20 70 45 6e 64 2d 3e    assert( pEnd->
3a0fc 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72  leftCursor==iCur
3a0fd 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
3a0fe 73 65 28 20 70 45 6e 64 2d 3e 77 74 46 6c 61 67  se( pEnd->wtFlag
3a0ff 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  s & TERM_VIRTUAL
3a100 20 29 3b 20 2f 2a 20 45 56 3a 20 52 2d 33 30 35   ); /* EV: R-305
3a101 37 35 2d 31 31 36 36 32 20 2a 2f 0a 20 20 20 20  75-11662 */.    
3a102 20 20 6d 65 6d 45 6e 64 56 61 6c 75 65 20 3d 20    memEndValue = 
3a103 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
3a104 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
3a105 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 58  rCode(pParse, pX
3a106 2d 3e 70 52 69 67 68 74 2c 20 6d 65 6d 45 6e 64  ->pRight, memEnd
3a107 56 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 69 66  Value);.      if
3a108 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54 20  ( pX->op==TK_LT 
3a109 7c 7c 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 47 54  || pX->op==TK_GT
3a10a 20 29 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74   ){.        test
3a10b 4f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 4c  Op = bRev ? OP_L
3a10c 65 20 3a 20 4f 50 5f 47 65 3b 0a 20 20 20 20 20  e : OP_Ge;.     
3a10d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
3a10e 74 65 73 74 4f 70 20 3d 20 62 52 65 76 20 3f 20  testOp = bRev ? 
3a10f 4f 50 5f 4c 74 20 3a 20 4f 50 5f 47 74 3b 0a 20  OP_Lt : OP_Gt;. 
3a110 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 69 73       }.      dis
3a111 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c  ableTerm(pLevel,
3a112 20 70 45 6e 64 29 3b 0a 20 20 20 20 7d 0a 20 20   pEnd);.    }.  
3a113 20 20 73 74 61 72 74 20 3d 20 73 71 6c 69 74 65    start = sqlite
3a114 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
3a115 28 76 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  (v);.    pLevel-
3a116 3e 6f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f  >op = bRev ? OP_
3a117 50 72 65 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a  Prev : OP_Next;.
3a118 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d      pLevel->p1 =
3a119 20 69 43 75 72 3b 0a 20 20 20 20 70 4c 65 76 65   iCur;.    pLeve
3a11a 6c 2d 3e 70 32 20 3d 20 73 74 61 72 74 3b 0a 20  l->p2 = start;. 
3a11b 20 20 20 69 66 28 20 70 53 74 61 72 74 3d 3d 30     if( pStart==0
3a11c 20 26 26 20 70 45 6e 64 3d 3d 30 20 29 7b 0a 20   && pEnd==0 ){. 
3a11d 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 35 20       pLevel->p5 
3a11e 3d 20 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41  = SQLITE_STMTSTA
3a11f 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45  TUS_FULLSCAN_STE
3a120 50 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  P;.    }else{.  
3a121 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 76      assert( pLev
3a122 65 6c 2d 3e 70 35 3d 3d 30 20 29 3b 0a 20 20 20  el->p5==0 );.   
3a123 20 7d 0a 20 20 20 20 69 66 28 20 74 65 73 74 4f   }.    if( testO
3a124 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20  p!=OP_Noop ){.  
3a125 20 20 20 20 69 52 6f 77 69 64 52 65 67 20 3d 20      iRowidReg = 
3a126 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20 73 71  iReleaseReg = sq
3a127 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
3a128 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73  pParse);.      s
3a129 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
3a12a 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43  (v, OP_Rowid, iC
3a12b 75 72 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a  ur, iRowidReg);.
3a12c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
3a12d 72 43 61 63 68 65 53 74 6f 72 65 28 70 50 61 72  rCacheStore(pPar
3a12e 73 65 2c 20 69 43 75 72 2c 20 2d 31 2c 20 69 52  se, iCur, -1, iR
3a12f 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20 20 20  owidReg);.      
3a130 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
3a131 33 28 76 2c 20 74 65 73 74 4f 70 2c 20 6d 65 6d  3(v, testOp, mem
3a132 45 6e 64 56 61 6c 75 65 2c 20 61 64 64 72 42 72  EndValue, addrBr
3a133 6b 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20  k, iRowidReg);. 
3a134 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3a135 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49  ChangeP5(v, SQLI
3a136 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c  TE_AFF_NUMERIC |
3a137 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
3a138 4c 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  LL);.    }.  }el
3a139 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70  se if( pLevel->p
3a13a 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 28 57  lan.wsFlags & (W
3a13b 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47  HERE_COLUMN_RANG
3a13c 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45  E|WHERE_COLUMN_E
3a13d 51 29 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73  Q) ){.    /* Cas
3a13e 65 20 33 3a 20 41 20 73 63 61 6e 20 75 73 69 6e  e 3: A scan usin
3a13f 67 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20 20 20  g an index..    
3a140 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  **.    **       
3a141 20 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75    The WHERE clau
3a142 73 65 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 7a  se may contain z
3a143 65 72 6f 20 6f 72 20 6d 6f 72 65 20 65 71 75 61  ero or more equa
3a144 6c 69 74 79 20 0a 20 20 20 20 2a 2a 20 20 20 20  lity .    **    
3a145 20 20 20 20 20 74 65 72 6d 73 20 28 22 3d 3d 22       terms ("=="
3a146 20 6f 72 20 22 49 4e 22 20 6f 70 65 72 61 74 6f   or "IN" operato
3a147 72 73 29 20 74 68 61 74 20 72 65 66 65 72 20 74  rs) that refer t
3a148 6f 20 74 68 65 20 4e 0a 20 20 20 20 2a 2a 20 20  o the N.    **  
3a149 20 20 20 20 20 20 20 6c 65 66 74 2d 6d 6f 73 74         left-most
3a14a 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
3a14b 69 6e 64 65 78 2e 20 49 74 20 6d 61 79 20 61 6c  index. It may al
3a14c 73 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 2a  so contain.    *
3a14d 2a 20 20 20 20 20 20 20 20 20 69 6e 65 71 75 61  *         inequa
3a14e 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
3a14f 20 28 3e 2c 20 3c 2c 20 3e 3d 20 6f 72 20 3c 3d   (>, <, >= or <=
3a150 29 20 6f 6e 20 74 68 65 20 69 6e 64 65 78 65 64  ) on the indexed
3a151 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
3a152 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 6d 6d 65  column that imme
3a153 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20  diately follows 
3a154 74 68 65 20 4e 20 65 71 75 61 6c 69 74 69 65 73  the N equalities
3a155 2e 20 4f 6e 6c 79 20 0a 20 20 20 20 2a 2a 20 20  . Only .    **  
3a156 20 20 20 20 20 20 20 74 68 65 20 72 69 67 68 74         the right
3a157 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 63 61 6e  -most column can
3a158 20 62 65 20 61 6e 20 69 6e 65 71 75 61 6c 69 74   be an inequalit
3a159 79 20 2d 20 74 68 65 20 72 65 73 74 20 6d 75 73  y - the rest mus
3a15a 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  t.    **        
3a15b 20 75 73 65 20 74 68 65 20 22 3d 3d 22 20 61 6e   use the "==" an
3a15c 64 20 22 49 4e 22 20 6f 70 65 72 61 74 6f 72 73  d "IN" operators
3a15d 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  . For example, i
3a15e 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20 20  f the .    **   
3a15f 20 20 20 20 20 20 69 6e 64 65 78 20 69 73 20 6f        index is o
3a160 6e 20 28 78 2c 79 2c 7a 29 2c 20 74 68 65 6e 20  n (x,y,z), then 
3a161 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6c  the following cl
3a162 61 75 73 65 73 20 61 72 65 20 61 6c 6c 20 0a 20  auses are all . 
3a163 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6f 70     **         op
3a164 74 69 6d 69 7a 65 64 3a 0a 20 20 20 20 2a 2a 0a  timized:.    **.
3a165 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
3a166 20 20 78 3d 35 0a 20 20 20 20 2a 2a 20 20 20 20    x=5.    **    
3a167 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20          x=5 AND 
3a168 79 3d 31 30 0a 20 20 20 20 2a 2a 20 20 20 20 20  y=10.    **     
3a169 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79         x=5 AND y
3a16a 3c 31 30 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  <10.    **      
3a16b 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3e        x=5 AND y>
3a16c 35 20 41 4e 44 20 79 3c 31 30 0a 20 20 20 20 2a  5 AND y<10.    *
3a16d 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 3d 35  *            x=5
3a16e 20 41 4e 44 20 79 3d 35 20 41 4e 44 20 7a 3c 3d   AND y=5 AND z<=
3a16f 31 30 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  10.    **.    **
3a170 20 20 20 20 20 20 20 20 20 54 68 65 20 7a 3c 31           The z<1
3a171 30 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f  0 term of the fo
3a172 6c 6c 6f 77 69 6e 67 20 63 61 6e 6e 6f 74 20 62  llowing cannot b
3a173 65 20 75 73 65 64 2c 20 6f 6e 6c 79 0a 20 20 20  e used, only.   
3a174 20 2a 2a 20 20 20 20 20 20 20 20 20 74 68 65 20   **         the 
3a175 78 3d 35 20 74 65 72 6d 3a 0a 20 20 20 20 2a 2a  x=5 term:.    **
3a176 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
3a177 20 20 20 78 3d 35 20 41 4e 44 20 7a 3c 31 30 0a     x=5 AND z<10.
3a178 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
3a179 20 20 20 20 20 20 4e 20 6d 61 79 20 62 65 20 7a        N may be z
3a17a 65 72 6f 20 69 66 20 74 68 65 72 65 20 61 72 65  ero if there are
3a17b 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73   inequality cons
3a17c 74 72 61 69 6e 74 73 2e 0a 20 20 20 20 2a 2a 20  traints..    ** 
3a17d 20 20 20 20 20 20 20 20 49 66 20 74 68 65 72 65          If there
3a17e 20 61 72 65 20 6e 6f 20 69 6e 65 71 75 61 6c 69   are no inequali
3a17f 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20  ty constraints, 
3a180 74 68 65 6e 20 4e 20 69 73 20 61 74 0a 20 20 20  then N is at.   
3a181 20 2a 2a 20 20 20 20 20 20 20 20 20 6c 65 61 73   **         leas
3a182 74 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 20  t one..    **.  
3a183 20 20 2a 2a 20 20 20 20 20 20 20 20 20 54 68 69    **         Thi
3a184 73 20 63 61 73 65 20 69 73 20 61 6c 73 6f 20 75  s case is also u
3a185 73 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 61  sed when there a
3a186 72 65 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75  re no WHERE clau
3a187 73 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  se.    **       
3a188 20 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 75    constraints bu
3a189 74 20 61 6e 20 69 6e 64 65 78 20 69 73 20 73 65  t an index is se
3a18a 6c 65 63 74 65 64 20 61 6e 79 77 61 79 2c 20 69  lected anyway, i
3a18b 6e 20 6f 72 64 65 72 0a 20 20 20 20 2a 2a 20 20  n order.    **  
3a18c 20 20 20 20 20 20 20 74 6f 20 66 6f 72 63 65 20         to force 
3a18d 74 68 65 20 6f 75 74 70 75 74 20 6f 72 64 65 72  the output order
3a18e 20 74 6f 20 63 6f 6e 66 6f 72 6d 20 74 6f 20 61   to conform to a
3a18f 6e 20 4f 52 44 45 52 20 42 59 2e 0a 20 20 20 20  n ORDER BY..    
3a190 2a 2f 20 20 0a 20 20 20 20 73 74 61 74 69 63 20  */  .    static 
3a191 63 6f 6e 73 74 20 75 38 20 61 53 74 61 72 74 4f  const u8 aStartO
3a192 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 30 2c  p[] = {.      0,
3a193 0a 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20  .      0,.      
3a194 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 20  OP_Rewind,      
3a195 20 20 20 20 20 2f 2a 20 32 3a 20 28 21 73 74 61       /* 2: (!sta
3a196 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26  rt_constraints &
3a197 26 20 73 74 61 72 74 45 71 20 26 26 20 20 21 62  & startEq &&  !b
3a198 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50  Rev) */.      OP
3a199 5f 4c 61 73 74 2c 20 20 20 20 20 20 20 20 20 20  _Last,          
3a19a 20 20 20 2f 2a 20 33 3a 20 28 21 73 74 61 72 74     /* 3: (!start
3a19b 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26 20  _constraints && 
3a19c 73 74 61 72 74 45 71 20 26 26 20 20 20 62 52 65  startEq &&   bRe
3a19d 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53  v) */.      OP_S
3a19e 65 65 6b 47 74 2c 20 20 20 20 20 20 20 20 20 20  eekGt,          
3a19f 20 2f 2a 20 34 3a 20 28 73 74 61 72 74 5f 63 6f   /* 4: (start_co
3a1a0 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 21 73  nstraints  && !s
3a1a1 74 61 72 74 45 71 20 26 26 20 21 62 52 65 76 29  tartEq && !bRev)
3a1a2 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65 65   */.      OP_See
3a1a3 6b 4c 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f  kLt,           /
3a1a4 2a 20 35 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73  * 5: (start_cons
3a1a5 74 72 61 69 6e 74 73 20 20 26 26 20 21 73 74 61  traints  && !sta
3a1a6 72 74 45 71 20 26 26 20 20 62 52 65 76 29 20 2a  rtEq &&  bRev) *
3a1a7 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65 65 6b 47  /.      OP_SeekG
3a1a8 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e,           /* 
3a1a9 36 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73 74 72  6: (start_constr
3a1aa 61 69 6e 74 73 20 20 26 26 20 20 73 74 61 72 74  aints  &&  start
3a1ab 45 71 20 26 26 20 21 62 52 65 76 29 20 2a 2f 0a  Eq && !bRev) */.
3a1ac 20 20 20 20 20 20 4f 50 5f 53 65 65 6b 4c 65 20        OP_SeekLe 
3a1ad 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 37 3a             /* 7:
3a1ae 20 28 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69   (start_constrai
3a1af 6e 74 73 20 20 26 26 20 20 73 74 61 72 74 45 71  nts  &&  startEq
3a1b0 20 26 26 20 20 62 52 65 76 29 20 2a 2f 0a 20 20   &&  bRev) */.  
3a1b1 20 20 7d 3b 0a 20 20 20 20 73 74 61 74 69 63 20    };.    static 
3a1b2 63 6f 6e 73 74 20 75 38 20 61 45 6e 64 4f 70 5b  const u8 aEndOp[
3a1b3 5d 20 3d 20 7b 0a 20 20 20 20 20 20 4f 50 5f 4e  ] = {.      OP_N
3a1b4 6f 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20  oop,            
3a1b5 20 2f 2a 20 30 3a 20 28 21 65 6e 64 5f 63 6f 6e   /* 0: (!end_con
3a1b6 73 74 72 61 69 6e 74 73 29 20 2a 2f 0a 20 20 20  straints) */.   
3a1b7 20 20 20 4f 50 5f 49 64 78 47 45 2c 20 20 20 20     OP_IdxGE,    
3a1b8 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 28 65          /* 1: (e
3a1b9 6e 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26  nd_constraints &
3a1ba 26 20 21 62 52 65 76 29 20 2a 2f 0a 20 20 20 20  & !bRev) */.    
3a1bb 20 20 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20    OP_IdxLT      
3a1bc 20 20 20 20 20 20 20 2f 2a 20 32 3a 20 28 65 6e         /* 2: (en
3a1bd 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26  d_constraints &&
3a1be 20 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 7d 3b   bRev) */.    };
3a1bf 0a 20 20 20 20 69 6e 74 20 6e 45 71 20 3d 20 70  .    int nEq = p
3a1c0 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 45 71 3b  Level->plan.nEq;
3a1c1 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d    /* Number of =
3a1c2 3d 20 6f 72 20 49 4e 20 74 65 72 6d 73 20 2a 2f  = or IN terms */
3a1c3 0a 20 20 20 20 69 6e 74 20 69 73 4d 69 6e 51 75  .    int isMinQu
3a1c4 65 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ery = 0;        
3a1c5 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
3a1c6 61 6e 20 6f 70 74 69 6d 69 7a 65 64 20 53 45 4c  an optimized SEL
3a1c7 45 43 54 20 6d 69 6e 28 78 29 2e 2e 20 2a 2f 0a  ECT min(x).. */.
3a1c8 20 20 20 20 69 6e 74 20 72 65 67 42 61 73 65 3b      int regBase;
3a1c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a1ca 20 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65   /* Base registe
3a1cb 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6e 73 74 72  r holding constr
3a1cc 61 69 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0a 20  aint values */. 
3a1cd 20 20 20 69 6e 74 20 72 31 3b 20 20 20 20 20 20     int r1;      
3a1ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a1cf 2f 2a 20 54 65 6d 70 20 72 65 67 69 73 74 65 72  /* Temp register
3a1d0 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 54 65 72   */.    WhereTer
3a1d1 6d 20 2a 70 52 61 6e 67 65 53 74 61 72 74 20 3d  m *pRangeStart =
3a1d2 20 30 3b 20 20 2f 2a 20 49 6e 65 71 75 61 6c 69   0;  /* Inequali
3a1d3 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 74  ty constraint at
3a1d4 20 72 61 6e 67 65 20 73 74 61 72 74 20 2a 2f 0a   range start */.
3a1d5 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
3a1d6 52 61 6e 67 65 45 6e 64 20 3d 20 30 3b 20 20 20  RangeEnd = 0;   
3a1d7 20 2f 2a 20 49 6e 65 71 75 61 6c 69 74 79 20 63   /* Inequality c
3a1d8 6f 6e 73 74 72 61 69 6e 74 20 61 74 20 72 61 6e  onstraint at ran
3a1d9 67 65 20 65 6e 64 20 2a 2f 0a 20 20 20 20 69 6e  ge end */.    in
3a1da 74 20 73 74 61 72 74 45 71 3b 20 20 20 20 20 20  t startEq;      
3a1db 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
3a1dc 75 65 20 69 66 20 72 61 6e 67 65 20 73 74 61 72  ue if range star
3a1dd 74 20 75 73 65 73 20 3d 3d 2c 20 3e 3d 20 6f 72  t uses ==, >= or
3a1de 20 3c 3d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65   <= */.    int e
3a1df 6e 64 45 71 3b 20 20 20 20 20 20 20 20 20 20 20  ndEq;           
3a1e0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
3a1e1 69 66 20 72 61 6e 67 65 20 65 6e 64 20 75 73 65  if range end use
3a1e2 73 20 3d 3d 2c 20 3e 3d 20 6f 72 20 3c 3d 20 2a  s ==, >= or <= *
3a1e3 2f 0a 20 20 20 20 69 6e 74 20 73 74 61 72 74 5f  /.    int start_
3a1e4 63 6f 6e 73 74 72 61 69 6e 74 73 3b 20 20 20 20  constraints;    
3a1e5 20 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 72     /* Start of r
3a1e6 61 6e 67 65 20 69 73 20 63 6f 6e 73 74 72 61 69  ange is constrai
3a1e7 6e 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ned */.    int n
3a1e8 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20  Constraint;     
3a1e9 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
3a1ea 72 20 6f 66 20 63 6f 6e 73 74 72 61 69 6e 74 20  r of constraint 
3a1eb 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 49 6e 64  terms */.    Ind
3a1ec 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
3a1ed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3a1ee 20 69 6e 64 65 78 20 77 65 20 77 69 6c 6c 20 62   index we will b
3a1ef 65 20 75 73 69 6e 67 20 2a 2f 0a 20 20 20 20 69  e using */.    i
3a1f0 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20 20  nt iIdxCur;     
3a1f1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
3a1f2 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 66  he VDBE cursor f
3a1f3 6f 72 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a  or the index */.
3a1f4 20 20 20 20 69 6e 74 20 6e 45 78 74 72 61 52 65      int nExtraRe
3a1f5 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  g = 0;          
3a1f6 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78   /* Number of ex
3a1f7 74 72 61 20 72 65 67 69 73 74 65 72 73 20 6e 65  tra registers ne
3a1f8 65 64 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  eded */.    int 
3a1f9 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
3a1fa 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 74           /* Inst
3a1fb 72 75 63 74 69 6f 6e 20 6f 70 63 6f 64 65 20 2a  ruction opcode *
3a1fc 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74 61  /.    char *zSta
3a1fd 72 74 41 66 66 3b 20 20 20 20 20 20 20 20 20 20  rtAff;          
3a1fe 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 66     /* Affinity f
3a1ff 6f 72 20 73 74 61 72 74 20 6f 66 20 72 61 6e 67  or start of rang
3a200 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a  e constraint */.
3a201 20 20 20 20 63 68 61 72 20 2a 7a 45 6e 64 41 66      char *zEndAf
3a202 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
3a203 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 66 6f 72   /* Affinity for
3a204 20 65 6e 64 20 6f 66 20 72 61 6e 67 65 20 63 6f   end of range co
3a205 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 0a 20 20 20  nstraint */..   
3a206 20 70 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e   pIdx = pLevel->
3a207 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a 20 20 20  plan.u.pIdx;.   
3a208 20 69 49 64 78 43 75 72 20 3d 20 70 4c 65 76 65   iIdxCur = pLeve
3a209 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20  l->iIdxCur;.    
3a20a 6b 20 3d 20 28 6e 45 71 3d 3d 70 49 64 78 2d 3e  k = (nEq==pIdx->
3a20b 6e 43 6f 6c 75 6d 6e 20 3f 20 2d 31 20 3a 20 70  nColumn ? -1 : p
3a20c 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45  Idx->aiColumn[nE
3a20d 71 5d 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  q]);..    /* If 
3a20e 74 68 69 73 20 6c 6f 6f 70 20 73 61 74 69 73 66  this loop satisf
3a20f 69 65 73 20 61 20 73 6f 72 74 20 6f 72 64 65 72  ies a sort order
3a210 20 28 70 4f 72 64 65 72 42 79 29 20 72 65 71 75   (pOrderBy) requ
3a211 65 73 74 20 74 68 61 74 20 0a 20 20 20 20 2a 2a  est that .    **
3a212 20 77 61 73 20 70 61 73 73 65 64 20 74 6f 20 74   was passed to t
3a213 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  his function to 
3a214 69 6d 70 6c 65 6d 65 6e 74 20 61 20 22 53 45 4c  implement a "SEL
3a215 45 43 54 20 6d 69 6e 28 78 29 20 2e 2e 2e 22 20  ECT min(x) ..." 
3a216 0a 20 20 20 20 2a 2a 20 71 75 65 72 79 2c 20 74  .    ** query, t
3a217 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 77  hen the caller w
3a218 69 6c 6c 20 6f 6e 6c 79 20 61 6c 6c 6f 77 20 74  ill only allow t
3a219 68 65 20 6c 6f 6f 70 20 74 6f 20 72 75 6e 20 66  he loop to run f
3a21a 6f 72 0a 20 20 20 20 2a 2a 20 61 20 73 69 6e 67  or.    ** a sing
3a21b 6c 65 20 69 74 65 72 61 74 69 6f 6e 2e 20 54 68  le iteration. Th
3a21c 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  is means that th
3a21d 65 20 66 69 72 73 74 20 72 6f 77 20 72 65 74 75  e first row retu
3a21e 72 6e 65 64 0a 20 20 20 20 2a 2a 20 73 68 6f 75  rned.    ** shou
3a21f 6c 64 20 6e 6f 74 20 68 61 76 65 20 61 20 4e 55  ld not have a NU
3a220 4c 4c 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  LL value stored 
3a221 69 6e 20 27 78 27 2e 20 49 66 20 63 6f 6c 75 6d  in 'x'. If colum
3a222 6e 20 27 78 27 20 69 73 0a 20 20 20 20 2a 2a 20  n 'x' is.    ** 
3a223 74 68 65 20 66 69 72 73 74 20 6f 6e 65 20 61 66  the first one af
3a224 74 65 72 20 74 68 65 20 6e 45 71 20 65 71 75 61  ter the nEq equa
3a225 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
3a226 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2c 0a 20   in the index,. 
3a227 20 20 20 2a 2a 20 74 68 69 73 20 72 65 71 75 69     ** this requi
3a228 72 65 73 20 73 6f 6d 65 20 73 70 65 63 69 61 6c  res some special
3a229 20 68 61 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 2a   handling..    *
3a22a 2f 0a 20 20 20 20 69 66 28 20 28 77 63 74 72 6c  /.    if( (wctrl
3a22b 46 6c 61 67 73 26 57 48 45 52 45 5f 4f 52 44 45  Flags&WHERE_ORDE
3a22c 52 42 59 5f 4d 49 4e 29 21 3d 30 0a 20 20 20 20  RBY_MIN)!=0.    
3a22d 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61   && (pLevel->pla
3a22e 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f  n.wsFlags&WHERE_
3a22f 4f 52 44 45 52 45 44 29 0a 20 20 20 20 20 26 26  ORDERED).     &&
3a230 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e   (pIdx->nColumn>
3a231 6e 45 71 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  nEq).    ){.    
3a232 20 20 2f 2a 20 61 73 73 65 72 74 28 20 70 4f 72    /* assert( pOr
3a233 64 65 72 42 79 2d 3e 6e 45 78 70 72 3d 3d 31 20  derBy->nExpr==1 
3a234 29 3b 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 61  ); */.      /* a
3a235 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 2d  ssert( pOrderBy-
3a236 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 69 43 6f  >a[0].pExpr->iCo
3a237 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e 61 69 43 6f  lumn==pIdx->aiCo
3a238 6c 75 6d 6e 5b 6e 45 71 5d 20 29 3b 20 2a 2f 0a  lumn[nEq] ); */.
3a239 20 20 20 20 20 20 69 73 4d 69 6e 51 75 65 72 79        isMinQuery
3a23a 20 3d 20 31 3b 0a 20 20 20 20 20 20 6e 45 78 74   = 1;.      nExt
3a23b 72 61 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d  raReg = 1;.    }
3a23c 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 6e  ..    /* Find an
3a23d 79 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e  y inequality con
3a23e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20 66 6f  straint terms fo
3a23f 72 20 74 68 65 20 73 74 61 72 74 20 61 6e 64 20  r the start and 
3a240 65 6e 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74  end .    ** of t
3a241 68 65 20 72 61 6e 67 65 2e 20 0a 20 20 20 20 2a  he range. .    *
3a242 2f 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c  /.    if( pLevel
3a243 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
3a244 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54   WHERE_TOP_LIMIT
3a245 20 29 7b 0a 20 20 20 20 20 20 70 52 61 6e 67 65   ){.      pRange
3a246 45 6e 64 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  End = findTerm(p
3a247 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74  WC, iCur, k, not
3a248 52 65 61 64 79 2c 20 28 57 4f 5f 4c 54 7c 57 4f  Ready, (WO_LT|WO
3a249 5f 4c 45 29 2c 20 70 49 64 78 29 3b 0a 20 20 20  _LE), pIdx);.   
3a24a 20 20 20 6e 45 78 74 72 61 52 65 67 20 3d 20 31     nExtraReg = 1
3a24b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
3a24c 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
3a24d 6c 61 67 73 20 26 20 57 48 45 52 45 5f 42 54 4d  lags & WHERE_BTM
3a24e 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 20 20  _LIMIT ){.      
3a24f 70 52 61 6e 67 65 53 74 61 72 74 20 3d 20 66 69  pRangeStart = fi
3a250 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
3a251 2c 20 6b 2c 20 6e 6f 74 52 65 61 64 79 2c 20 28  , k, notReady, (
3a252 57 4f 5f 47 54 7c 57 4f 5f 47 45 29 2c 20 70 49  WO_GT|WO_GE), pI
3a253 64 78 29 3b 0a 20 20 20 20 20 20 6e 45 78 74 72  dx);.      nExtr
3a254 61 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  aReg = 1;.    }.
3a255 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  .    /* Generate
3a256 20 63 6f 64 65 20 74 6f 20 65 76 61 6c 75 61 74   code to evaluat
3a257 65 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74  e all constraint
3a258 20 74 65 72 6d 73 20 75 73 69 6e 67 20 3d 3d 20   terms using == 
3a259 6f 72 20 49 4e 0a 20 20 20 20 2a 2a 20 61 6e 64  or IN.    ** and
3a25a 20 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65   store the value
3a25b 73 20 6f 66 20 74 68 6f 73 65 20 74 65 72 6d 73  s of those terms
3a25c 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20   in an array of 
3a25d 72 65 67 69 73 74 65 72 73 0a 20 20 20 20 2a 2a  registers.    **
3a25e 20 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67   starting at reg
3a25f 42 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  Base..    */.   
3a260 20 72 65 67 42 61 73 65 20 3d 20 63 6f 64 65 41   regBase = codeA
3a261 6c 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28  llEqualityTerms(
3a262 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c  .        pParse,
3a263 20 70 4c 65 76 65 6c 2c 20 70 57 43 2c 20 6e 6f   pLevel, pWC, no
3a264 74 52 65 61 64 79 2c 20 6e 45 78 74 72 61 52 65  tReady, nExtraRe
3a265 67 2c 20 26 7a 53 74 61 72 74 41 66 66 0a 20 20  g, &zStartAff.  
3a266 20 20 29 3b 0a 20 20 20 20 7a 45 6e 64 41 66 66    );.    zEndAff
3a267 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
3a268 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a  up(pParse->db, z
3a269 53 74 61 72 74 41 66 66 29 3b 0a 20 20 20 20 61  StartAff);.    a
3a26a 64 64 72 4e 78 74 20 3d 20 70 4c 65 76 65 6c 2d  ddrNxt = pLevel-
3a26b 3e 61 64 64 72 4e 78 74 3b 0a 0a 20 20 20 20 2f  >addrNxt;..    /
3a26c 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e  * If we are doin
3a26d 67 20 61 20 72 65 76 65 72 73 65 20 6f 72 64 65  g a reverse orde
3a26e 72 20 73 63 61 6e 20 6f 6e 20 61 6e 20 61 73 63  r scan on an asc
3a26f 65 6e 64 69 6e 67 20 69 6e 64 65 78 2c 20 6f 72  ending index, or
3a270 0a 20 20 20 20 2a 2a 20 61 20 66 6f 72 77 61 72  .    ** a forwar
3a271 64 20 6f 72 64 65 72 20 73 63 61 6e 20 6f 6e 20  d order scan on 
3a272 61 20 64 65 73 63 65 6e 64 69 6e 67 20 69 6e 64  a descending ind
3a273 65 78 2c 20 69 6e 74 65 72 63 68 61 6e 67 65 20  ex, interchange 
3a274 74 68 65 20 0a 20 20 20 20 2a 2a 20 73 74 61 72  the .    ** star
3a275 74 20 61 6e 64 20 65 6e 64 20 74 65 72 6d 73 20  t and end terms 
3a276 28 70 52 61 6e 67 65 53 74 61 72 74 20 61 6e 64  (pRangeStart and
3a277 20 70 52 61 6e 67 65 45 6e 64 29 2e 0a 20 20 20   pRangeEnd)..   
3a278 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 6e 45 71   */.    if( (nEq
3a279 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 26  <pIdx->nColumn &
3a27a 26 20 62 52 65 76 3d 3d 28 70 49 64 78 2d 3e 61  & bRev==(pIdx->a
3a27b 53 6f 72 74 4f 72 64 65 72 5b 6e 45 71 5d 3d 3d  SortOrder[nEq]==
3a27c 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 29 29 0a  SQLITE_SO_ASC)).
3a27d 20 20 20 20 20 7c 7c 20 28 62 52 65 76 20 26 26       || (bRev &&
3a27e 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d   pIdx->nColumn==
3a27f 6e 45 71 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  nEq).    ){.    
3a280 20 20 53 57 41 50 28 57 68 65 72 65 54 65 72 6d    SWAP(WhereTerm
3a281 20 2a 2c 20 70 52 61 6e 67 65 45 6e 64 2c 20 70   *, pRangeEnd, p
3a282 52 61 6e 67 65 53 74 61 72 74 29 3b 0a 20 20 20  RangeStart);.   
3a283 20 7d 0a 0a 20 20 20 20 74 65 73 74 63 61 73 65   }..    testcase
3a284 28 20 70 52 61 6e 67 65 53 74 61 72 74 20 26 26  ( pRangeStart &&
3a285 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f   pRangeStart->eO
3a286 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 4c 45 20  perator & WO_LE 
3a287 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
3a288 20 70 52 61 6e 67 65 53 74 61 72 74 20 26 26 20   pRangeStart && 
3a289 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f 70  pRangeStart->eOp
3a28a 65 72 61 74 6f 72 20 26 20 57 4f 5f 47 45 20 29  erator & WO_GE )
3a28b 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
3a28c 70 52 61 6e 67 65 45 6e 64 20 26 26 20 70 52 61  pRangeEnd && pRa
3a28d 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f  ngeEnd->eOperato
3a28e 72 20 26 20 57 4f 5f 4c 45 20 29 3b 0a 20 20 20  r & WO_LE );.   
3a28f 20 74 65 73 74 63 61 73 65 28 20 70 52 61 6e 67   testcase( pRang
3a290 65 45 6e 64 20 26 26 20 70 52 61 6e 67 65 45 6e  eEnd && pRangeEn
3a291 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57  d->eOperator & W
3a292 4f 5f 47 45 20 29 3b 0a 20 20 20 20 73 74 61 72  O_GE );.    star
3a293 74 45 71 20 3d 20 21 70 52 61 6e 67 65 53 74 61  tEq = !pRangeSta
3a294 72 74 20 7c 7c 20 70 52 61 6e 67 65 53 74 61 72  rt || pRangeStar
3a295 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28  t->eOperator & (
3a296 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0a 20 20  WO_LE|WO_GE);.  
3a297 20 20 65 6e 64 45 71 20 3d 20 20 20 21 70 52 61    endEq =   !pRa
3a298 6e 67 65 45 6e 64 20 7c 7c 20 70 52 61 6e 67 65  ngeEnd || pRange
3a299 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  End->eOperator &
3a29a 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0a   (WO_LE|WO_GE);.
3a29b 20 20 20 20 73 74 61 72 74 5f 63 6f 6e 73 74 72      start_constr
3a29c 61 69 6e 74 73 20 3d 20 70 52 61 6e 67 65 53 74  aints = pRangeSt
3a29d 61 72 74 20 7c 7c 20 6e 45 71 3e 30 3b 0a 0a 20  art || nEq>0;.. 
3a29e 20 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 69     /* Seek the i
3a29f 6e 64 65 78 20 63 75 72 73 6f 72 20 74 6f 20 74  ndex cursor to t
3a2a0 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
3a2a1 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 20 20 6e 43  range. */.    nC
3a2a2 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 45 71 3b  onstraint = nEq;
3a2a3 0a 20 20 20 20 69 66 28 20 70 52 61 6e 67 65 53  .    if( pRangeS
3a2a4 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 45 78  tart ){.      Ex
3a2a5 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 52 61  pr *pRight = pRa
3a2a6 6e 67 65 53 74 61 72 74 2d 3e 70 45 78 70 72 2d  ngeStart->pExpr-
3a2a7 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 73  >pRight;.      s
3a2a8 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
3a2a9 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 72  Parse, pRight, r
3a2aa 65 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20  egBase+nEq);.   
3a2ab 20 20 20 69 66 28 20 28 70 52 61 6e 67 65 53 74     if( (pRangeSt
3a2ac 61 72 74 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  art->wtFlags & T
3a2ad 45 52 4d 5f 56 4e 55 4c 4c 29 3d 3d 30 20 29 7b  ERM_VNULL)==0 ){
3a2ae 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
3a2af 45 78 70 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75  ExprCodeIsNullJu
3a2b0 6d 70 28 76 2c 20 70 52 69 67 68 74 2c 20 72 65  mp(v, pRight, re
3a2b1 67 42 61 73 65 2b 6e 45 71 2c 20 61 64 64 72 4e  gBase+nEq, addrN
3a2b2 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  xt);.      }.   
3a2b3 20 20 20 69 66 28 20 7a 53 74 61 72 74 41 66 66     if( zStartAff
3a2b4 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
3a2b5 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66  sqlite3CompareAf
3a2b6 66 69 6e 69 74 79 28 70 52 69 67 68 74 2c 20 7a  finity(pRight, z
3a2b7 53 74 61 72 74 41 66 66 5b 6e 45 71 5d 29 3d 3d  StartAff[nEq])==
3a2b8 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 29  SQLITE_AFF_NONE)
3a2b9 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  {.          /* S
3a2ba 69 6e 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69  ince the compari
3a2bb 73 6f 6e 20 69 73 20 74 6f 20 62 65 20 70 65 72  son is to be per
3a2bc 66 6f 72 6d 65 64 20 77 69 74 68 20 6e 6f 20 63  formed with no c
3a2bd 6f 6e 76 65 72 73 69 6f 6e 73 0a 20 20 20 20 20  onversions.     
3a2be 20 20 20 20 20 2a 2a 20 61 70 70 6c 69 65 64 20       ** applied 
3a2bf 74 6f 20 74 68 65 20 6f 70 65 72 61 6e 64 73 2c  to the operands,
3a2c0 20 73 65 74 20 74 68 65 20 61 66 66 69 6e 69 74   set the affinit
3a2c1 79 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 70 52  y to apply to pR
3a2c2 69 67 68 74 20 74 6f 20 0a 20 20 20 20 20 20 20  ight to .       
3a2c3 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46     ** SQLITE_AFF
3a2c4 5f 4e 4f 4e 45 2e 20 20 2a 2f 0a 20 20 20 20 20  _NONE.  */.     
3a2c5 20 20 20 20 20 7a 53 74 61 72 74 41 66 66 5b 6e       zStartAff[n
3a2c6 45 71 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  Eq] = SQLITE_AFF
3a2c7 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d  _NONE;.        }
3a2c8 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
3a2c9 69 74 65 33 45 78 70 72 4e 65 65 64 73 4e 6f 41  ite3ExprNeedsNoA
3a2ca 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 52  ffinityChange(pR
3a2cb 69 67 68 74 2c 20 7a 53 74 61 72 74 41 66 66 5b  ight, zStartAff[
3a2cc 6e 45 71 5d 29 20 29 7b 0a 20 20 20 20 20 20 20  nEq]) ){.       
3a2cd 20 20 20 7a 53 74 61 72 74 41 66 66 5b 6e 45 71     zStartAff[nEq
3a2ce 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ] = SQLITE_AFF_N
3a2cf 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ONE;.        }. 
3a2d0 20 20 20 20 20 7d 20 20 0a 20 20 20 20 20 20 6e       }  .      n
3a2d1 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20  Constraint++;.  
3a2d2 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 52      testcase( pR
3a2d3 61 6e 67 65 53 74 61 72 74 2d 3e 77 74 46 6c 61  angeStart->wtFla
3a2d4 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41  gs & TERM_VIRTUA
3a2d5 4c 20 29 3b 20 2f 2a 20 45 56 3a 20 52 2d 33 30  L ); /* EV: R-30
3a2d6 35 37 35 2d 31 31 36 36 32 20 2a 2f 0a 20 20 20  575-11662 */.   
3a2d7 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4d 69 6e   }else if( isMin
3a2d8 51 75 65 72 79 20 29 7b 0a 20 20 20 20 20 20 73  Query ){.      s
3a2d9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
3a2da 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
3a2db 72 65 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20  regBase+nEq);.  
3a2dc 20 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b      nConstraint+
3a2dd 2b 3b 0a 20 20 20 20 20 20 73 74 61 72 74 45 71  +;.      startEq
3a2de 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 74 61 72   = 0;.      star
3a2df 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 3d 20  t_constraints = 
3a2e0 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 6f 64  1;.    }.    cod
3a2e1 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70  eApplyAffinity(p
3a2e2 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20  Parse, regBase, 
3a2e3 6e 43 6f 6e 73 74 72 61 69 6e 74 2c 20 7a 53 74  nConstraint, zSt
3a2e4 61 72 74 41 66 66 29 3b 0a 20 20 20 20 6f 70 20  artAff);.    op 
3a2e5 3d 20 61 53 74 61 72 74 4f 70 5b 28 73 74 61 72  = aStartOp[(star
3a2e6 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 3c 3c 32  t_constraints<<2
3a2e7 29 20 2b 20 28 73 74 61 72 74 45 71 3c 3c 31 29  ) + (startEq<<1)
3a2e8 20 2b 20 62 52 65 76 5d 3b 0a 20 20 20 20 61 73   + bRev];.    as
3a2e9 73 65 72 74 28 20 6f 70 21 3d 30 20 29 3b 0a 20  sert( op!=0 );. 
3a2ea 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
3a2eb 3d 4f 50 5f 52 65 77 69 6e 64 20 29 3b 0a 20 20  =OP_Rewind );.  
3a2ec 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
3a2ed 4f 50 5f 4c 61 73 74 20 29 3b 0a 20 20 20 20 74  OP_Last );.    t
3a2ee 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f  estcase( op==OP_
3a2ef 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 74 65  SeekGt );.    te
3a2f0 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53  stcase( op==OP_S
3a2f1 65 65 6b 47 65 20 29 3b 0a 20 20 20 20 74 65 73  eekGe );.    tes
3a2f2 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65  tcase( op==OP_Se
3a2f3 65 6b 4c 65 20 29 3b 0a 20 20 20 20 74 65 73 74  ekLe );.    test
3a2f4 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65 65  case( op==OP_See
3a2f5 6b 4c 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  kLt );.    sqlit
3a2f6 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28  e3VdbeAddOp4Int(
3a2f7 76 2c 20 6f 70 2c 20 69 49 64 78 43 75 72 2c 20  v, op, iIdxCur, 
3a2f8 61 64 64 72 4e 78 74 2c 20 72 65 67 42 61 73 65  addrNxt, regBase
3a2f9 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a  , nConstraint);.
3a2fa 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65  .    /* Load the
3a2fb 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 69   value for the i
3a2fc 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72  nequality constr
3a2fd 61 69 6e 74 20 61 74 20 74 68 65 20 65 6e 64 20  aint at the end 
3a2fe 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 61  of the.    ** ra
3a2ff 6e 67 65 20 28 69 66 20 61 6e 79 29 2e 0a 20 20  nge (if any)..  
3a300 20 20 2a 2f 0a 20 20 20 20 6e 43 6f 6e 73 74 72    */.    nConstr
3a301 61 69 6e 74 20 3d 20 6e 45 71 3b 0a 20 20 20 20  aint = nEq;.    
3a302 69 66 28 20 70 52 61 6e 67 65 45 6e 64 20 29 7b  if( pRangeEnd ){
3a303 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52 69  .      Expr *pRi
3a304 67 68 74 20 3d 20 70 52 61 6e 67 65 45 6e 64 2d  ght = pRangeEnd-
3a305 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a  >pExpr->pRight;.
3a306 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
3a307 72 43 61 63 68 65 52 65 6d 6f 76 65 28 70 50 61  rCacheRemove(pPa
3a308 72 73 65 2c 20 72 65 67 42 61 73 65 2b 6e 45 71  rse, regBase+nEq
3a309 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
3a30a 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
3a30b 73 65 2c 20 70 52 69 67 68 74 2c 20 72 65 67 42  se, pRight, regB
3a30c 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20 20  ase+nEq);.      
3a30d 69 66 28 20 28 70 52 61 6e 67 65 45 6e 64 2d 3e  if( (pRangeEnd->
3a30e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56  wtFlags & TERM_V
3a30f 4e 55 4c 4c 29 3d 3d 30 20 29 7b 0a 20 20 20 20  NULL)==0 ){.    
3a310 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
3a311 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 76 2c  odeIsNullJump(v,
3a312 20 70 52 69 67 68 74 2c 20 72 65 67 42 61 73 65   pRight, regBase
3a313 2b 6e 45 71 2c 20 61 64 64 72 4e 78 74 29 3b 0a  +nEq, addrNxt);.
3a314 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
3a315 28 20 7a 45 6e 64 41 66 66 20 29 7b 0a 20 20 20  ( zEndAff ){.   
3a316 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
3a317 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
3a318 70 52 69 67 68 74 2c 20 7a 45 6e 64 41 66 66 5b  pRight, zEndAff[
3a319 6e 45 71 5d 29 3d 3d 53 51 4c 49 54 45 5f 41 46  nEq])==SQLITE_AF
3a31a 46 5f 4e 4f 4e 45 29 7b 0a 20 20 20 20 20 20 20  F_NONE){.       
3a31b 20 20 20 2f 2a 20 53 69 6e 63 65 20 74 68 65 20     /* Since the 
3a31c 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 6f  comparison is to
3a31d 20 62 65 20 70 65 72 66 6f 72 6d 65 64 20 77 69   be performed wi
3a31e 74 68 20 6e 6f 20 63 6f 6e 76 65 72 73 69 6f 6e  th no conversion
3a31f 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61  s.          ** a
3a320 70 70 6c 69 65 64 20 74 6f 20 74 68 65 20 6f 70  pplied to the op
3a321 65 72 61 6e 64 73 2c 20 73 65 74 20 74 68 65 20  erands, set the 
3a322 61 66 66 69 6e 69 74 79 20 74 6f 20 61 70 70 6c  affinity to appl
3a323 79 20 74 6f 20 70 52 69 67 68 74 20 74 6f 20 0a  y to pRight to .
3a324 20 20 20 20 20 20 20 20 20 20 2a 2a 20 53 51 4c            ** SQL
3a325 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 2e 20 20 2a  ITE_AFF_NONE.  *
3a326 2f 0a 20 20 20 20 20 20 20 20 20 20 7a 45 6e 64  /.          zEnd
3a327 41 66 66 5b 6e 45 71 5d 20 3d 20 53 51 4c 49 54  Aff[nEq] = SQLIT
3a328 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20  E_AFF_NONE;.    
3a329 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
3a32a 28 20 73 71 6c 69 74 65 33 45 78 70 72 4e 65 65  ( sqlite3ExprNee
3a32b 64 73 4e 6f 41 66 66 69 6e 69 74 79 43 68 61 6e  dsNoAffinityChan
3a32c 67 65 28 70 52 69 67 68 74 2c 20 7a 45 6e 64 41  ge(pRight, zEndA
3a32d 66 66 5b 6e 45 71 5d 29 20 29 7b 0a 20 20 20 20  ff[nEq]) ){.    
3a32e 20 20 20 20 20 20 7a 45 6e 64 41 66 66 5b 6e 45        zEndAff[nE
3a32f 71 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  q] = SQLITE_AFF_
3a330 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a  NONE;.        }.
3a331 20 20 20 20 20 20 7d 20 20 0a 20 20 20 20 20 20        }  .      
3a332 63 6f 64 65 41 70 70 6c 79 41 66 66 69 6e 69 74  codeApplyAffinit
3a333 79 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73  y(pParse, regBas
3a334 65 2c 20 6e 45 71 2b 31 2c 20 7a 45 6e 64 41 66  e, nEq+1, zEndAf
3a335 66 29 3b 0a 20 20 20 20 20 20 6e 43 6f 6e 73 74  f);.      nConst
3a336 72 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 74  raint++;.      t
3a337 65 73 74 63 61 73 65 28 20 70 52 61 6e 67 65 45  estcase( pRangeE
3a338 6e 64 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  nd->wtFlags & TE
3a339 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 20 2f 2a  RM_VIRTUAL ); /*
3a33a 20 45 56 3a 20 52 2d 33 30 35 37 35 2d 31 31 36   EV: R-30575-116
3a33b 36 32 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  62 */.    }.    
3a33c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50  sqlite3DbFree(pP
3a33d 61 72 73 65 2d 3e 64 62 2c 20 7a 53 74 61 72 74  arse->db, zStart
3a33e 41 66 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Aff);.    sqlite
3a33f 33 44 62 46 72 65 65 28 70 50 61 72 73 65 2d 3e  3DbFree(pParse->
3a340 64 62 2c 20 7a 45 6e 64 41 66 66 29 3b 0a 0a 20  db, zEndAff);.. 
3a341 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65     /* Top of the
3a342 20 6c 6f 6f 70 20 62 6f 64 79 20 2a 2f 0a 20 20   loop body */.  
3a343 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73    pLevel->p2 = s
3a344 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
3a345 74 41 64 64 72 28 76 29 3b 0a 0a 20 20 20 20 2f  tAddr(v);..    /
3a346 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 69  * Check if the i
3a347 6e 64 65 78 20 63 75 72 73 6f 72 20 69 73 20 70  ndex cursor is p
3a348 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
3a349 68 65 20 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 20  he range. */.   
3a34a 20 6f 70 20 3d 20 61 45 6e 64 4f 70 5b 28 70 52   op = aEndOp[(pR
3a34b 61 6e 67 65 45 6e 64 20 7c 7c 20 6e 45 71 29 20  angeEnd || nEq) 
3a34c 2a 20 28 31 20 2b 20 62 52 65 76 29 5d 3b 0a 20  * (1 + bRev)];. 
3a34d 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
3a34e 3d 4f 50 5f 4e 6f 6f 70 20 29 3b 0a 20 20 20 20  =OP_Noop );.    
3a34f 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50  testcase( op==OP
3a350 5f 49 64 78 47 45 20 29 3b 0a 20 20 20 20 74 65  _IdxGE );.    te
3a351 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 49  stcase( op==OP_I
3a352 64 78 4c 54 20 29 3b 0a 20 20 20 20 69 66 28 20  dxLT );.    if( 
3a353 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20  op!=OP_Noop ){. 
3a354 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3a355 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 6f 70 2c  AddOp4Int(v, op,
3a356 20 69 49 64 78 43 75 72 2c 20 61 64 64 72 4e 78   iIdxCur, addrNx
3a357 74 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6e  t, regBase, nCon
3a358 73 74 72 61 69 6e 74 29 3b 0a 20 20 20 20 20 20  straint);.      
3a359 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
3a35a 65 50 35 28 76 2c 20 65 6e 64 45 71 21 3d 62 52  eP5(v, endEq!=bR
3a35b 65 76 20 3f 31 3a 30 29 3b 0a 20 20 20 20 7d 0a  ev ?1:0);.    }.
3a35c 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65  .    /* If there
3a35d 20 61 72 65 20 69 6e 65 71 75 61 6c 69 74 79 20   are inequality 
3a35e 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 63 68 65  constraints, che
3a35f 63 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75  ck that the valu
3a360 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  e.    ** of the 
3a361 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61  table column tha
3a362 74 20 74 68 65 20 69 6e 65 71 75 61 6c 69 74 79  t the inequality
3a363 20 63 6f 6e 74 72 61 69 6e 73 20 69 73 20 6e 6f   contrains is no
3a364 74 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20 49  t NULL..    ** I
3a365 66 20 69 74 20 69 73 2c 20 6a 75 6d 70 20 74 6f  f it is, jump to
3a366 20 74 68 65 20 6e 65 78 74 20 69 74 65 72 61 74   the next iterat
3a367 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e  ion of the loop.
3a368 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 31 20 3d  .    */.    r1 =
3a369 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
3a36a 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
3a36b 74 65 73 74 63 61 73 65 28 20 70 4c 65 76 65 6c  testcase( pLevel
3a36c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
3a36d 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54   WHERE_BTM_LIMIT
3a36e 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
3a36f 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77  ( pLevel->plan.w
3a370 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 54  sFlags & WHERE_T
3a371 4f 50 5f 4c 49 4d 49 54 20 29 3b 0a 20 20 20 20  OP_LIMIT );.    
3a372 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  if( (pLevel->pla
3a373 6e 2e 77 73 46 6c 61 67 73 20 26 20 28 57 48 45  n.wsFlags & (WHE
3a374 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 7c 57 48 45  RE_BTM_LIMIT|WHE
3a375 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 29 29 21 3d  RE_TOP_LIMIT))!=
3a376 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
3a377 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
3a378 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43  OP_Column, iIdxC
3a379 75 72 2c 20 6e 45 71 2c 20 72 31 29 3b 0a 20 20  ur, nEq, r1);.  
3a37a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
3a37b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75  ddOp2(v, OP_IsNu
3a37c 6c 6c 2c 20 72 31 2c 20 61 64 64 72 43 6f 6e 74  ll, r1, addrCont
3a37d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
3a37e 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
3a37f 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a  eg(pParse, r1);.
3a380 0a 20 20 20 20 2f 2a 20 53 65 65 6b 20 74 68 65  .    /* Seek the
3a381 20 74 61 62 6c 65 20 63 75 72 73 6f 72 2c 20 69   table cursor, i
3a382 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20  f required */.  
3a383 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c    disableTerm(pL
3a384 65 76 65 6c 2c 20 70 52 61 6e 67 65 53 74 61 72  evel, pRangeStar
3a385 74 29 3b 0a 20 20 20 20 64 69 73 61 62 6c 65 54  t);.    disableT
3a386 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 52 61 6e  erm(pLevel, pRan
3a387 67 65 45 6e 64 29 3b 0a 20 20 20 20 69 66 28 20  geEnd);.    if( 
3a388 21 6f 6d 69 74 54 61 62 6c 65 20 29 7b 0a 20 20  !omitTable ){.  
3a389 20 20 20 20 69 52 6f 77 69 64 52 65 67 20 3d 20      iRowidReg = 
3a38a 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20 73 71  iReleaseReg = sq
3a38b 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
3a38c 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73  pParse);.      s
3a38d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
3a38e 28 76 2c 20 4f 50 5f 49 64 78 52 6f 77 69 64 2c  (v, OP_IdxRowid,
3a38f 20 69 49 64 78 43 75 72 2c 20 69 52 6f 77 69 64   iIdxCur, iRowid
3a390 52 65 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Reg);.      sqli
3a391 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f 72  te3ExprCacheStor
3a392 65 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20  e(pParse, iCur, 
3a393 2d 31 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a  -1, iRowidReg);.
3a394 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
3a395 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65  eAddOp2(v, OP_Se
3a396 65 6b 2c 20 69 43 75 72 2c 20 69 52 6f 77 69 64  ek, iCur, iRowid
3a397 52 65 67 29 3b 20 20 2f 2a 20 44 65 66 65 72 72  Reg);  /* Deferr
3a398 65 64 20 73 65 65 6b 20 2a 2f 0a 20 20 20 20 7d  ed seek */.    }
3a399 0a 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 20  ..    /* Record 
3a39a 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  the instruction 
3a39b 75 73 65 64 20 74 6f 20 74 65 72 6d 69 6e 61 74  used to terminat
3a39c 65 20 74 68 65 20 6c 6f 6f 70 2e 20 44 69 73 61  e the loop. Disa
3a39d 62 6c 65 20 0a 20 20 20 20 2a 2a 20 57 48 45 52  ble .    ** WHER
3a39e 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 6d  E clause terms m
3a39f 61 64 65 20 72 65 64 75 6e 64 61 6e 74 20 62 79  ade redundant by
3a3a0 20 74 68 65 20 69 6e 64 65 78 20 72 61 6e 67 65   the index range
3a3a1 20 73 63 61 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20   scan..    */.  
3a3a2 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c    if( pLevel->pl
3a3a3 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
3a3a4 52 45 5f 55 4e 49 51 55 45 20 29 7b 0a 20 20 20  RE_UNIQUE ){.   
3a3a5 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20     pLevel->op = 
3a3a6 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 7d 65 6c  OP_Noop;.    }el
3a3a7 73 65 20 69 66 28 20 62 52 65 76 20 29 7b 0a 20  se if( bRev ){. 
3a3a8 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20       pLevel->op 
3a3a9 3d 20 4f 50 5f 50 72 65 76 3b 0a 20 20 20 20 7d  = OP_Prev;.    }
3a3aa 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 65 76  else{.      pLev
3a3ab 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 4e 65 78 74  el->op = OP_Next
3a3ac 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 65 76  ;.    }.    pLev
3a3ad 65 6c 2d 3e 70 31 20 3d 20 69 49 64 78 43 75 72  el->p1 = iIdxCur
3a3ae 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c  ;.    if( pLevel
3a3af 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
3a3b0 20 57 48 45 52 45 5f 43 4f 56 45 52 5f 53 43 41   WHERE_COVER_SCA
3a3b1 4e 20 29 7b 0a 20 20 20 20 20 20 70 4c 65 76 65  N ){.      pLeve
3a3b2 6c 2d 3e 70 35 20 3d 20 53 51 4c 49 54 45 5f 53  l->p5 = SQLITE_S
3a3b3 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43  TMTSTATUS_FULLSC
3a3b4 41 4e 5f 53 54 45 50 3b 0a 20 20 20 20 7d 65 6c  AN_STEP;.    }el
3a3b5 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
3a3b6 28 20 70 4c 65 76 65 6c 2d 3e 70 35 3d 3d 30 20  ( pLevel->p5==0 
3a3b7 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
3a3b8 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
3a3b9 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a  _OMIT_OR_OPTIMIZ
3a3ba 41 54 49 4f 4e 0a 20 20 69 66 28 20 70 4c 65 76  ATION.  if( pLev
3a3bb 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
3a3bc 20 26 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f   & WHERE_MULTI_O
3a3bd 52 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65  R ){.    /* Case
3a3be 20 34 3a 20 20 54 77 6f 20 6f 72 20 6d 6f 72 65   4:  Two or more
3a3bf 20 73 65 70 61 72 61 74 65 6c 79 20 69 6e 64 65   separately inde
3a3c0 78 65 64 20 74 65 72 6d 73 20 63 6f 6e 6e 65 63  xed terms connec
3a3c1 74 65 64 20 62 79 20 4f 52 0a 20 20 20 20 2a 2a  ted by OR.    **
3a3c2 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a  .    ** Example:
3a3c3 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
3a3c4 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31   CREATE TABLE t1
3a3c5 28 61 2c 62 2c 63 2c 64 29 3b 0a 20 20 20 20 2a  (a,b,c,d);.    *
3a3c6 2a 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58  *   CREATE INDEX
3a3c7 20 69 31 20 4f 4e 20 74 31 28 61 29 3b 0a 20 20   i1 ON t1(a);.  
3a3c8 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 49 4e    **   CREATE IN
3a3c9 44 45 58 20 69 32 20 4f 4e 20 74 31 28 62 29 3b  DEX i2 ON t1(b);
3a3ca 0a 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45  .    **   CREATE
3a3cb 20 49 4e 44 45 58 20 69 33 20 4f 4e 20 74 31 28   INDEX i3 ON t1(
3a3cc 63 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  c);.    **.    *
3a3cd 2a 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  *   SELECT * FRO
3a3ce 4d 20 74 31 20 57 48 45 52 45 20 61 3d 35 20 4f  M t1 WHERE a=5 O
3a3cf 52 20 62 3d 37 20 4f 52 20 28 63 3d 31 31 20 41  R b=7 OR (c=11 A
3a3d0 4e 44 20 64 3d 31 33 29 0a 20 20 20 20 2a 2a 0a  ND d=13).    **.
3a3d1 20 20 20 20 2a 2a 20 49 6e 20 74 68 65 20 65 78      ** In the ex
3a3d2 61 6d 70 6c 65 2c 20 74 68 65 72 65 20 61 72 65  ample, there are
3a3d3 20 74 68 72 65 65 20 69 6e 64 65 78 65 64 20 74   three indexed t
3a3d4 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62  erms connected b
3a3d5 79 20 4f 52 2e 0a 20 20 20 20 2a 2a 20 54 68 65  y OR..    ** The
3a3d6 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70   top of the loop
3a3d7 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73   looks like this
3a3d8 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
3a3d9 20 20 20 20 20 20 20 20 20 4e 75 6c 6c 20 20 20           Null   
3a3da 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20      1           
3a3db 20 20 20 20 20 23 20 5a 65 72 6f 20 74 68 65 20       # Zero the 
3a3dc 72 6f 77 73 65 74 20 69 6e 20 72 65 67 20 31 0a  rowset in reg 1.
3a3dd 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
3a3de 65 6e 2c 20 66 6f 72 20 65 61 63 68 20 69 6e 64  en, for each ind
3a3df 65 78 65 64 20 74 65 72 6d 2c 20 74 68 65 20 66  exed term, the f
3a3e0 6f 6c 6c 6f 77 69 6e 67 2e 20 54 68 65 20 61 72  ollowing. The ar
3a3e1 67 75 6d 65 6e 74 73 20 74 6f 0a 20 20 20 20 2a  guments to.    *
3a3e2 2a 20 52 6f 77 53 65 74 54 65 73 74 20 61 72 65  * RowSetTest are
3a3e3 20 73 75 63 68 20 74 68 61 74 20 74 68 65 20 72   such that the r
3a3e4 6f 77 69 64 20 6f 66 20 74 68 65 20 63 75 72 72  owid of the curr
3a3e5 65 6e 74 20 72 6f 77 20 69 73 20 69 6e 73 65 72  ent row is inser
3a3e6 74 65 64 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20  ted.    ** into 
3a3e7 74 68 65 20 52 6f 77 53 65 74 2e 20 49 66 20 69  the RowSet. If i
3a3e8 74 20 69 73 20 61 6c 72 65 61 64 79 20 70 72 65  t is already pre
3a3e9 73 65 6e 74 2c 20 63 6f 6e 74 72 6f 6c 20 73 6b  sent, control sk
3a3ea 69 70 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 47  ips the.    ** G
3a3eb 6f 73 75 62 20 6f 70 63 6f 64 65 20 61 6e 64 20  osub opcode and 
3a3ec 6a 75 6d 70 73 20 73 74 72 61 69 67 68 74 20 74  jumps straight t
3a3ed 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  o the code gener
3a3ee 61 74 65 64 20 62 79 20 57 68 65 72 65 45 6e 64  ated by WhereEnd
3a3ef 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ()..    **.    *
3a3f0 2a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  *        sqlite3
3a3f1 57 68 65 72 65 42 65 67 69 6e 28 3c 74 65 72 6d  WhereBegin(<term
3a3f2 3e 29 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  >).    **       
3a3f3 20 20 20 52 6f 77 53 65 74 54 65 73 74 20 20 20     RowSetTest   
3a3f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 23                 #
3a3f5 20 49 6e 73 65 72 74 20 72 6f 77 69 64 20 69 6e   Insert rowid in
3a3f6 74 6f 20 72 6f 77 73 65 74 0a 20 20 20 20 2a 2a  to rowset.    **
3a3f7 20 20 20 20 20 20 20 20 20 20 47 6f 73 75 62 20            Gosub 
3a3f8 20 20 20 20 20 32 20 41 0a 20 20 20 20 2a 2a 20       2 A.    ** 
3a3f9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68         sqlite3Wh
3a3fa 65 72 65 45 6e 64 28 29 0a 20 20 20 20 2a 2a 0a  ereEnd().    **.
3a3fb 20 20 20 20 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67      ** Following
3a3fc 20 74 68 65 20 61 62 6f 76 65 2c 20 63 6f 64 65   the above, code
3a3fd 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68   to terminate th
3a3fe 65 20 6c 6f 6f 70 2e 20 4c 61 62 65 6c 20 41 2c  e loop. Label A,
3a3ff 20 74 68 65 20 74 61 72 67 65 74 0a 20 20 20 20   the target.    
3a400 2a 2a 20 6f 66 20 74 68 65 20 47 6f 73 75 62 20  ** of the Gosub 
3a401 61 62 6f 76 65 2c 20 6a 75 6d 70 73 20 74 6f 20  above, jumps to 
3a402 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  the instruction 
3a403 72 69 67 68 74 20 61 66 74 65 72 20 74 68 65 20  right after the 
3a404 47 6f 74 6f 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  Goto..    **.   
3a405 20 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 75 6c   **          Nul
3a406 6c 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20  l       1       
3a407 20 20 20 20 20 20 20 20 20 23 20 5a 65 72 6f 20           # Zero 
3a408 74 68 65 20 72 6f 77 73 65 74 20 69 6e 20 72 65  the rowset in re
3a409 67 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  g 1.    **      
3a40a 20 20 20 20 47 6f 74 6f 20 20 20 20 20 20 20 42      Goto       B
3a40b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a40c 23 20 54 68 65 20 6c 6f 6f 70 20 69 73 20 66 69  # The loop is fi
3a40d 6e 69 73 68 65 64 2e 0a 20 20 20 20 2a 2a 0a 20  nished..    **. 
3a40e 20 20 20 2a 2a 20 20 20 20 20 20 20 41 3a 20 3c     **       A: <
3a40f 6c 6f 6f 70 20 62 6f 64 79 3e 20 20 20 20 20 20  loop body>      
3a410 20 20 20 20 20 20 20 20 20 20 20 23 20 52 65 74             # Ret
3a411 75 72 6e 20 64 61 74 61 2c 20 77 68 61 74 65 76  urn data, whatev
3a412 65 72 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  er..    **.    *
3a413 2a 20 20 20 20 20 20 20 20 20 20 52 65 74 75 72  *          Retur
3a414 6e 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20  n     2         
3a415 20 20 20 20 20 20 20 23 20 4a 75 6d 70 20 62 61         # Jump ba
3a416 63 6b 20 74 6f 20 74 68 65 20 47 6f 73 75 62 0a  ck to the Gosub.
3a417 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
3a418 20 20 20 20 42 3a 20 3c 61 66 74 65 72 20 74 68      B: <after th
3a419 65 20 6c 6f 6f 70 3e 0a 20 20 20 20 2a 2a 0a 20  e loop>.    **. 
3a41a 20 20 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 43     */.    WhereC
3a41b 6c 61 75 73 65 20 2a 70 4f 72 57 63 3b 20 20 20  lause *pOrWc;   
3a41c 20 2f 2a 20 54 68 65 20 4f 52 2d 63 6c 61 75 73   /* The OR-claus
3a41d 65 20 62 72 6f 6b 65 6e 20 6f 75 74 20 69 6e 74  e broken out int
3a41e 6f 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20  o subterms */.  
3a41f 20 20 53 72 63 4c 69 73 74 20 2a 70 4f 72 54 61    SrcList *pOrTa
3a420 62 3b 20 20 20 20 20 20 20 2f 2a 20 53 68 6f 72  b;       /* Shor
3a421 74 65 6e 65 64 20 74 61 62 6c 65 20 6c 69 73 74  tened table list
3a422 20 6f 72 20 4f 52 2d 63 6c 61 75 73 65 20 67 65   or OR-clause ge
3a423 6e 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  neration */.    
3a424 49 6e 64 65 78 20 2a 70 43 6f 76 20 3d 20 30 3b  Index *pCov = 0;
3a425 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a426 50 6f 74 65 6e 74 69 61 6c 20 63 6f 76 65 72 69  Potential coveri
3a427 6e 67 20 69 6e 64 65 78 20 28 6f 72 20 4e 55 4c  ng index (or NUL
3a428 4c 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  L) */.    int iC
3a429 6f 76 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e  ovCur = pParse->
3a42a 6e 54 61 62 2b 2b 3b 20 20 2f 2a 20 43 75 72 73  nTab++;  /* Curs
3a42b 6f 72 20 75 73 65 64 20 66 6f 72 20 69 6e 64 65  or used for inde
3a42c 78 20 73 63 61 6e 73 20 28 69 66 20 61 6e 79 29  x scans (if any)
3a42d 20 2a 2f 0a 0a 20 20 20 20 69 6e 74 20 72 65 67   */..    int reg
3a42e 52 65 74 75 72 6e 20 3d 20 2b 2b 70 50 61 72 73  Return = ++pPars
3a42f 65 2d 3e 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20  e->nMem;        
3a430 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75     /* Register u
3a431 73 65 64 20 77 69 74 68 20 4f 50 5f 47 6f 73 75  sed with OP_Gosu
3a432 62 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67  b */.    int reg
3a433 52 6f 77 73 65 74 20 3d 20 30 3b 20 20 20 20 20  Rowset = 0;     
3a434 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a435 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 66     /* Register f
3a436 6f 72 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74  or RowSet object
3a437 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 52   */.    int regR
3a438 6f 77 69 64 20 3d 20 30 3b 20 20 20 20 20 20 20  owid = 0;       
3a439 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a43a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f    /* Register ho
3a43b 6c 64 69 6e 67 20 72 6f 77 69 64 20 2a 2f 0a 20  lding rowid */. 
3a43c 20 20 20 69 6e 74 20 69 4c 6f 6f 70 42 6f 64 79     int iLoopBody
3a43d 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
3a43e 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20 2f 2a 20  keLabel(v);  /* 
3a43f 53 74 61 72 74 20 6f 66 20 6c 6f 6f 70 20 62 6f  Start of loop bo
3a440 64 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 52  dy */.    int iR
3a441 65 74 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20  etInit;         
3a442 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a443 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
3a444 66 20 72 65 67 52 65 74 75 72 6e 20 69 6e 69 74  f regReturn init
3a445 20 2a 2f 0a 20 20 20 20 69 6e 74 20 75 6e 74 65   */.    int unte
3a446 73 74 65 64 54 65 72 6d 73 20 3d 20 30 3b 20 20  stedTerms = 0;  
3a447 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f             /* So
3a448 6d 65 20 74 65 72 6d 73 20 6e 6f 74 20 63 6f 6d  me terms not com
3a449 70 6c 65 74 65 6c 79 20 74 65 73 74 65 64 20 2a  pletely tested *
3a44a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20  /.    int ii;   
3a44b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a44c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
3a44d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
3a44e 45 78 70 72 20 2a 70 41 6e 64 45 78 70 72 20 3d  Expr *pAndExpr =
3a44f 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
3a450 20 20 20 2f 2a 20 41 6e 20 22 2e 2e 20 41 4e 44     /* An ".. AND
3a451 20 28 2e 2e 2e 29 22 20 65 78 70 72 65 73 73 69   (...)" expressi
3a452 6f 6e 20 2a 2f 0a 20 20 20 0a 20 20 20 20 70 54  on */.   .    pT
3a453 65 72 6d 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c  erm = pLevel->pl
3a454 61 6e 2e 75 2e 70 54 65 72 6d 3b 0a 20 20 20 20  an.u.pTerm;.    
3a455 61 73 73 65 72 74 28 20 70 54 65 72 6d 21 3d 30  assert( pTerm!=0
3a456 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
3a457 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
3a458 3d 3d 57 4f 5f 4f 52 20 29 3b 0a 20 20 20 20 61  ==WO_OR );.    a
3a459 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77  ssert( (pTerm->w
3a45a 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52  tFlags & TERM_OR
3a45b 49 4e 46 4f 29 21 3d 30 20 29 3b 0a 20 20 20 20  INFO)!=0 );.    
3a45c 70 4f 72 57 63 20 3d 20 26 70 54 65 72 6d 2d 3e  pOrWc = &pTerm->
3a45d 75 2e 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20  u.pOrInfo->wc;. 
3a45e 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20     pLevel->op = 
3a45f 4f 50 5f 52 65 74 75 72 6e 3b 0a 20 20 20 20 70  OP_Return;.    p
3a460 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 72 65 67 52  Level->p1 = regR
3a461 65 74 75 72 6e 3b 0a 0a 20 20 20 20 2f 2a 20 53  eturn;..    /* S
3a462 65 74 20 75 70 20 61 20 6e 65 77 20 53 72 63 4c  et up a new SrcL
3a463 69 73 74 20 69 6e 20 70 4f 72 54 61 62 20 63 6f  ist in pOrTab co
3a464 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62  ntaining the tab
3a465 6c 65 20 62 65 69 6e 67 20 73 63 61 6e 6e 65 64  le being scanned
3a466 0a 20 20 20 20 2a 2a 20 62 79 20 74 68 69 73 20  .    ** by this 
3a467 6c 6f 6f 70 20 69 6e 20 74 68 65 20 61 5b 30 5d  loop in the a[0]
3a468 20 73 6c 6f 74 20 61 6e 64 20 61 6c 6c 20 6e 6f   slot and all no
3a469 74 52 65 61 64 79 20 74 61 62 6c 65 73 20 69 6e  tReady tables in
3a46a 20 61 5b 31 2e 2e 5d 20 73 6c 6f 74 73 2e 0a 20   a[1..] slots.. 
3a46b 20 20 20 2a 2a 20 54 68 69 73 20 62 65 63 6f 6d     ** This becom
3a46c 65 73 20 74 68 65 20 53 72 63 4c 69 73 74 20 69  es the SrcList i
3a46d 6e 20 74 68 65 20 72 65 63 75 72 73 69 76 65 20  n the recursive 
3a46e 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 57  call to sqlite3W
3a46f 68 65 72 65 42 65 67 69 6e 28 29 2e 0a 20 20 20  hereBegin()..   
3a470 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 57 49 6e   */.    if( pWIn
3a471 66 6f 2d 3e 6e 4c 65 76 65 6c 3e 31 20 29 7b 0a  fo->nLevel>1 ){.
3a472 20 20 20 20 20 20 69 6e 74 20 6e 4e 6f 74 52 65        int nNotRe
3a473 61 64 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  ady;            
3a474 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62       /* The numb
3a475 65 72 20 6f 66 20 6e 6f 74 52 65 61 64 79 20 74  er of notReady t
3a476 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 20 20 73  ables */.      s
3a477 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
3a478 65 6d 20 2a 6f 72 69 67 53 72 63 3b 20 20 20 20  em *origSrc;    
3a479 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 69 73   /* Original lis
3a47a 74 20 6f 66 20 74 61 62 6c 65 73 20 2a 2f 0a 20  t of tables */. 
3a47b 20 20 20 20 20 6e 4e 6f 74 52 65 61 64 79 20 3d       nNotReady =
3a47c 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 20   pWInfo->nLevel 
3a47d 2d 20 69 4c 65 76 65 6c 20 2d 20 31 3b 0a 20 20  - iLevel - 1;.  
3a47e 20 20 20 20 70 4f 72 54 61 62 20 3d 20 73 71 6c      pOrTab = sql
3a47f 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61  ite3StackAllocRa
3a480 77 28 70 50 61 72 73 65 2d 3e 64 62 2c 0a 20 20  w(pParse->db,.  
3a481 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a482 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
3a483 28 2a 70 4f 72 54 61 62 29 2b 20 6e 4e 6f 74 52  (*pOrTab)+ nNotR
3a484 65 61 64 79 2a 73 69 7a 65 6f 66 28 70 4f 72 54  eady*sizeof(pOrT
3a485 61 62 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 20 20  ab->a[0]));.    
3a486 20 20 69 66 28 20 70 4f 72 54 61 62 3d 3d 30 20    if( pOrTab==0 
3a487 29 20 72 65 74 75 72 6e 20 6e 6f 74 52 65 61 64  ) return notRead
3a488 79 3b 0a 20 20 20 20 20 20 70 4f 72 54 61 62 2d  y;.      pOrTab-
3a489 3e 6e 41 6c 6c 6f 63 20 3d 20 28 69 31 36 29 28  >nAlloc = (i16)(
3a48a 6e 4e 6f 74 52 65 61 64 79 20 2b 20 31 29 3b 0a  nNotReady + 1);.
3a48b 20 20 20 20 20 20 70 4f 72 54 61 62 2d 3e 6e 53        pOrTab->nS
3a48c 72 63 20 3d 20 70 4f 72 54 61 62 2d 3e 6e 41 6c  rc = pOrTab->nAl
3a48d 6c 6f 63 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70  loc;.      memcp
3a48e 79 28 70 4f 72 54 61 62 2d 3e 61 2c 20 70 54 61  y(pOrTab->a, pTa
3a48f 62 49 74 65 6d 2c 20 73 69 7a 65 6f 66 28 2a 70  bItem, sizeof(*p
3a490 54 61 62 49 74 65 6d 29 29 3b 0a 20 20 20 20 20  TabItem));.     
3a491 20 6f 72 69 67 53 72 63 20 3d 20 70 57 49 6e 66   origSrc = pWInf
3a492 6f 2d 3e 70 54 61 62 4c 69 73 74 2d 3e 61 3b 0a  o->pTabList->a;.
3a493 20 20 20 20 20 20 66 6f 72 28 6b 3d 31 3b 20 6b        for(k=1; k
3a494 3c 3d 6e 4e 6f 74 52 65 61 64 79 3b 20 6b 2b 2b  <=nNotReady; k++
3a495 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ){.        memcp
3a496 79 28 26 70 4f 72 54 61 62 2d 3e 61 5b 6b 5d 2c  y(&pOrTab->a[k],
3a497 20 26 6f 72 69 67 53 72 63 5b 70 4c 65 76 65 6c   &origSrc[pLevel
3a498 5b 6b 5d 2e 69 46 72 6f 6d 5d 2c 20 73 69 7a 65  [k].iFrom], size
3a499 6f 66 28 70 4f 72 54 61 62 2d 3e 61 5b 6b 5d 29  of(pOrTab->a[k])
3a49a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
3a49b 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4f 72 54  else{.      pOrT
3a49c 61 62 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 54 61  ab = pWInfo->pTa
3a49d 62 4c 69 73 74 3b 0a 20 20 20 20 7d 0a 0a 20 20  bList;.    }..  
3a49e 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
3a49f 74 68 65 20 72 6f 77 73 65 74 20 72 65 67 69 73  the rowset regis
3a4a0 74 65 72 20 74 6f 20 63 6f 6e 74 61 69 6e 20 4e  ter to contain N
3a4a1 55 4c 4c 2e 20 41 6e 20 53 51 4c 20 4e 55 4c 4c  ULL. An SQL NULL
3a4a2 20 69 73 20 0a 20 20 20 20 2a 2a 20 65 71 75 69   is .    ** equi
3a4a3 76 61 6c 65 6e 74 20 74 6f 20 61 6e 20 65 6d 70  valent to an emp
3a4a4 74 79 20 72 6f 77 73 65 74 2e 0a 20 20 20 20 2a  ty rowset..    *
3a4a5 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 20 69 6e  *.    ** Also in
3a4a6 69 74 69 61 6c 69 7a 65 20 72 65 67 52 65 74 75  itialize regRetu
3a4a7 72 6e 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68  rn to contain th
3a4a8 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  e address of the
3a4a9 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 0a 20 20   instruction .  
3a4aa 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79    ** immediately
3a4ab 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 4f   following the O
3a4ac 50 5f 52 65 74 75 72 6e 20 61 74 20 74 68 65 20  P_Return at the 
3a4ad 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 6c 6f  bottom of the lo
3a4ae 6f 70 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a 20  op. This.    ** 
3a4af 69 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 61  is required in a
3a4b0 20 66 65 77 20 6f 62 73 63 75 72 65 20 4c 45 46   few obscure LEF
3a4b1 54 20 4a 4f 49 4e 20 63 61 73 65 73 20 77 68 65  T JOIN cases whe
3a4b2 72 65 20 63 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73  re control jumps
3a4b3 0a 20 20 20 20 2a 2a 20 6f 76 65 72 20 74 68 65  .    ** over the
3a4b4 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70   top of the loop
3a4b5 20 69 6e 74 6f 20 74 68 65 20 62 6f 64 79 20 6f   into the body o
3a4b6 66 20 69 74 2e 20 49 6e 20 74 68 69 73 20 63 61  f it. In this ca
3a4b7 73 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63  se the .    ** c
3a4b8 6f 72 72 65 63 74 20 72 65 73 70 6f 6e 73 65 20  orrect response 
3a4b9 66 6f 72 20 74 68 65 20 65 6e 64 2d 6f 66 2d 6c  for the end-of-l
3a4ba 6f 6f 70 20 63 6f 64 65 20 28 74 68 65 20 4f 50  oop code (the OP
3a4bb 5f 52 65 74 75 72 6e 29 20 69 73 20 74 6f 20 0a  _Return) is to .
3a4bc 20 20 20 20 2a 2a 20 66 61 6c 6c 20 74 68 72 6f      ** fall thro
3a4bd 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
3a4be 69 6e 73 74 72 75 63 74 69 6f 6e 2c 20 6a 75 73  instruction, jus
3a4bf 74 20 61 73 20 61 6e 20 4f 50 5f 4e 65 78 74 20  t as an OP_Next 
3a4c0 64 6f 65 73 20 69 66 0a 20 20 20 20 2a 2a 20 63  does if.    ** c
3a4c1 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 75 6e 69 6e  alled on an unin
3a4c2 69 74 69 61 6c 69 7a 65 64 20 63 75 72 73 6f 72  itialized cursor
3a4c3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
3a4c4 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57   (wctrlFlags & W
3a4c5 48 45 52 45 5f 44 55 50 4c 49 43 41 54 45 53 5f  HERE_DUPLICATES_
3a4c6 4f 4b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  OK)==0 ){.      
3a4c7 72 65 67 52 6f 77 73 65 74 20 3d 20 2b 2b 70 50  regRowset = ++pP
3a4c8 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
3a4c9 20 20 72 65 67 52 6f 77 69 64 20 3d 20 2b 2b 70    regRowid = ++p
3a4ca 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
3a4cb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
3a4cc 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
3a4cd 20 30 2c 20 72 65 67 52 6f 77 73 65 74 29 3b 0a   0, regRowset);.
3a4ce 20 20 20 20 7d 0a 20 20 20 20 69 52 65 74 49 6e      }.    iRetIn
3a4cf 69 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  it = sqlite3Vdbe
3a4d0 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
3a4d1 65 67 65 72 2c 20 30 2c 20 72 65 67 52 65 74 75  eger, 0, regRetu
3a4d2 72 6e 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  rn);..    /* If 
3a4d3 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 57 48 45  the original WHE
3a4d4 52 45 20 63 6c 61 75 73 65 20 69 73 20 7a 20 6f  RE clause is z o
3a4d5 66 20 74 68 65 20 66 6f 72 6d 3a 20 20 28 78 31  f the form:  (x1
3a4d6 20 4f 52 20 78 32 20 4f 52 20 2e 2e 2e 29 20 41   OR x2 OR ...) A
3a4d7 4e 44 20 79 0a 20 20 20 20 2a 2a 20 54 68 65 6e  ND y.    ** Then
3a4d8 20 66 6f 72 20 65 76 65 72 79 20 74 65 72 6d 20   for every term 
3a4d9 78 4e 2c 20 65 76 61 6c 75 61 74 65 20 61 73 20  xN, evaluate as 
3a4da 74 68 65 20 73 75 62 65 78 70 72 65 73 73 69 6f  the subexpressio
3a4db 6e 3a 20 78 4e 20 41 4e 44 20 7a 0a 20 20 20 20  n: xN AND z.    
3a4dc 2a 2a 20 54 68 61 74 20 77 61 79 2c 20 74 65 72  ** That way, ter
3a4dd 6d 73 20 69 6e 20 79 20 74 68 61 74 20 61 72 65  ms in y that are
3a4de 20 66 61 63 74 6f 72 65 64 20 69 6e 74 6f 20 74   factored into t
3a4df 68 65 20 64 69 73 6a 75 6e 63 74 69 6f 6e 20 77  he disjunction w
3a4e0 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65 20 70 69  ill.    ** be pi
3a4e1 63 6b 65 64 20 75 70 20 62 79 20 74 68 65 20 72  cked up by the r
3a4e2 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20 74  ecursive calls t
3a4e3 6f 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  o sqlite3WhereBe
3a4e4 67 69 6e 28 29 20 62 65 6c 6f 77 2e 0a 20 20 20  gin() below..   
3a4e5 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 63 74 75 61   **.    ** Actua
3a4e6 6c 6c 79 2c 20 65 61 63 68 20 73 75 62 65 78 70  lly, each subexp
3a4e7 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 76 65  ression is conve
3a4e8 72 74 65 64 20 74 6f 20 22 78 4e 20 41 4e 44 20  rted to "xN AND 
3a4e9 77 22 20 77 68 65 72 65 20 77 20 69 73 0a 20 20  w" where w is.  
3a4ea 20 20 2a 2a 20 74 68 65 20 22 69 6e 74 65 72 65    ** the "intere
3a4eb 73 74 69 6e 67 22 20 74 65 72 6d 73 20 6f 66 20  sting" terms of 
3a4ec 7a 20 2d 20 74 65 72 6d 73 20 74 68 61 74 20 64  z - terms that d
3a4ed 69 64 20 6e 6f 74 20 6f 72 69 67 69 6e 61 74 65  id not originate
3a4ee 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 4f   in the.    ** O
3a4ef 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73  N or USING claus
3a4f0 65 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e  e of a LEFT JOIN
3a4f1 2c 20 61 6e 64 20 74 65 72 6d 73 20 74 68 61 74  , and terms that
3a4f2 20 61 72 65 20 75 73 61 62 6c 65 20 61 73 20 0a   are usable as .
3a4f3 20 20 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 0a      ** indices..
3a4f4 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
3a4f5 57 43 2d 3e 6e 54 65 72 6d 3e 31 20 29 7b 0a 20  WC->nTerm>1 ){. 
3a4f6 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d 3b 0a       int iTerm;.
3a4f7 20 20 20 20 20 20 66 6f 72 28 69 54 65 72 6d 3d        for(iTerm=
3a4f8 30 3b 20 69 54 65 72 6d 3c 70 57 43 2d 3e 6e 54  0; iTerm<pWC->nT
3a4f9 65 72 6d 3b 20 69 54 65 72 6d 2b 2b 29 7b 0a 20  erm; iTerm++){. 
3a4fa 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78         Expr *pEx
3a4fb 70 72 20 3d 20 70 57 43 2d 3e 61 5b 69 54 65 72  pr = pWC->a[iTer
3a4fc 6d 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  m].pExpr;.      
3a4fd 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
3a4fe 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
3a4ff 46 72 6f 6d 4a 6f 69 6e 29 20 29 20 63 6f 6e 74  FromJoin) ) cont
3a500 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66  inue;.        if
3a501 28 20 70 57 43 2d 3e 61 5b 69 54 65 72 6d 5d 2e  ( pWC->a[iTerm].
3a502 77 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d 5f  wtFlags & (TERM_
3a503 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 4f 52 49  VIRTUAL|TERM_ORI
3a504 4e 46 4f 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  NFO) ) continue;
3a505 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 70 57  .        if( (pW
3a506 43 2d 3e 61 5b 69 54 65 72 6d 5d 2e 65 4f 70 65  C->a[iTerm].eOpe
3a507 72 61 74 6f 72 20 26 20 57 4f 5f 41 4c 4c 29 3d  rator & WO_ALL)=
3a508 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
3a509 20 20 20 20 20 20 20 70 45 78 70 72 20 3d 20 73         pExpr = s
3a50a 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 70 50  qlite3ExprDup(pP
3a50b 61 72 73 65 2d 3e 64 62 2c 20 70 45 78 70 72 2c  arse->db, pExpr,
3a50c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 41 6e   0);.        pAn
3a50d 64 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45  dExpr = sqlite3E
3a50e 78 70 72 41 6e 64 28 70 50 61 72 73 65 2d 3e 64  xprAnd(pParse->d
3a50f 62 2c 20 70 41 6e 64 45 78 70 72 2c 20 70 45 78  b, pAndExpr, pEx
3a510 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  pr);.      }.   
3a511 20 20 20 69 66 28 20 70 41 6e 64 45 78 70 72 20     if( pAndExpr 
3a512 29 7b 0a 20 20 20 20 20 20 20 20 70 41 6e 64 45  ){.        pAndE
3a513 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78  xpr = sqlite3PEx
3a514 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 41 4e  pr(pParse, TK_AN
3a515 44 2c 20 30 2c 20 70 41 6e 64 45 78 70 72 2c 20  D, 0, pAndExpr, 
3a516 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
3a517 7d 0a 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b  }..    for(ii=0;
3a518 20 69 69 3c 70 4f 72 57 63 2d 3e 6e 54 65 72 6d   ii<pOrWc->nTerm
3a519 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 57  ; ii++){.      W
3a51a 68 65 72 65 54 65 72 6d 20 2a 70 4f 72 54 65 72  hereTerm *pOrTer
3a51b 6d 20 3d 20 26 70 4f 72 57 63 2d 3e 61 5b 69 69  m = &pOrWc->a[ii
3a51c 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72  ];.      if( pOr
3a51d 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
3a51e 3d 3d 69 43 75 72 20 7c 7c 20 70 4f 72 54 65 72  ==iCur || pOrTer
3a51f 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  m->eOperator==WO
3a520 5f 41 4e 44 20 29 7b 0a 20 20 20 20 20 20 20 20  _AND ){.        
3a521 57 68 65 72 65 49 6e 66 6f 20 2a 70 53 75 62 57  WhereInfo *pSubW
3a522 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 2f  Info;          /
3a523 2a 20 49 6e 66 6f 20 66 6f 72 20 73 69 6e 67 6c  * Info for singl
3a524 65 20 4f 52 2d 74 65 72 6d 20 73 63 61 6e 20 2a  e OR-term scan *
3a525 2f 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a  /.        Expr *
3a526 70 4f 72 45 78 70 72 20 3d 20 70 4f 72 54 65 72  pOrExpr = pOrTer
3a527 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  m->pExpr;.      
3a528 20 20 69 66 28 20 70 41 6e 64 45 78 70 72 20 29    if( pAndExpr )
3a529 7b 0a 20 20 20 20 20 20 20 20 20 20 70 41 6e 64  {.          pAnd
3a52a 45 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20 70 4f  Expr->pLeft = pO
3a52b 72 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20  rExpr;.         
3a52c 20 70 4f 72 45 78 70 72 20 3d 20 70 41 6e 64 45   pOrExpr = pAndE
3a52d 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  xpr;.        }. 
3a52e 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74         /* Loop t
3a52f 68 72 6f 75 67 68 20 74 61 62 6c 65 20 65 6e 74  hrough table ent
3a530 72 69 65 73 20 74 68 61 74 20 6d 61 74 63 68 20  ries that match 
3a531 74 65 72 6d 20 70 4f 72 54 65 72 6d 2e 20 2a 2f  term pOrTerm. */
3a532 0a 20 20 20 20 20 20 20 20 70 53 75 62 57 49 6e  .        pSubWIn
3a533 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72  fo = sqlite3Wher
3a534 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70  eBegin(pParse, p
3a535 4f 72 54 61 62 2c 20 70 4f 72 45 78 70 72 2c 20  OrTab, pOrExpr, 
3a536 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  0, 0,.          
3a537 20 20 20 20 20 20 20 20 20 20 20 20 20 20 57 48                WH
3a538 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 5f 43 4c  ERE_OMIT_OPEN_CL
3a539 4f 53 45 20 7c 20 57 48 45 52 45 5f 41 4e 44 5f  OSE | WHERE_AND_
3a53a 4f 4e 4c 59 20 7c 0a 20 20 20 20 20 20 20 20 20  ONLY |.         
3a53b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 57                 W
3a53c 48 45 52 45 5f 46 4f 52 43 45 5f 54 41 42 4c 45  HERE_FORCE_TABLE
3a53d 20 7c 20 57 48 45 52 45 5f 4f 4e 45 54 41 42 4c   | WHERE_ONETABL
3a53e 45 5f 4f 4e 4c 59 2c 20 69 43 6f 76 43 75 72 29  E_ONLY, iCovCur)
3a53f 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
3a540 28 20 70 53 75 62 57 49 6e 66 6f 20 7c 7c 20 70  ( pSubWInfo || p
3a541 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 70  Parse->nErr || p
3a542 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
3a543 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20  cFailed );.     
3a544 20 20 20 69 66 28 20 70 53 75 62 57 49 6e 66 6f     if( pSubWInfo
3a545 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 57 68   ){.          Wh
3a546 65 72 65 4c 65 76 65 6c 20 2a 70 4c 76 6c 3b 0a  ereLevel *pLvl;.
3a547 20 20 20 20 20 20 20 20 20 20 65 78 70 6c 61 69            explai
3a548 6e 4f 6e 65 53 63 61 6e 28 0a 20 20 20 20 20 20  nOneScan(.      
3a549 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20          pParse, 
3a54a 70 4f 72 54 61 62 2c 20 26 70 53 75 62 57 49 6e  pOrTab, &pSubWIn
3a54b 66 6f 2d 3e 61 5b 30 5d 2c 20 69 4c 65 76 65 6c  fo->a[0], iLevel
3a54c 2c 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 2c  , pLevel->iFrom,
3a54d 20 30 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a   0.          );.
3a54e 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28 77            if( (w
3a54f 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52  ctrlFlags & WHER
3a550 45 5f 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b 29  E_DUPLICATES_OK)
3a551 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
3a552 20 20 20 69 6e 74 20 69 53 65 74 20 3d 20 28 28     int iSet = ((
3a553 69 69 3d 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d  ii==pOrWc->nTerm
3a554 2d 31 29 3f 2d 31 3a 69 69 29 3b 0a 20 20 20 20  -1)?-1:ii);.    
3a555 20 20 20 20 20 20 20 20 69 6e 74 20 72 3b 0a 20          int r;. 
3a556 20 20 20 20 20 20 20 20 20 20 20 72 20 3d 20 73             r = s
3a557 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65  qlite3ExprCodeGe
3a558 74 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20  tColumn(pParse, 
3a559 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2c 20  pTabItem->pTab, 
3a55a 2d 31 2c 20 69 43 75 72 2c 20 0a 20 20 20 20 20  -1, iCur, .     
3a55b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a55c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a55d 20 20 20 20 72 65 67 52 6f 77 69 64 2c 20 30 29      regRowid, 0)
3a55e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ;.            sq
3a55f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 49  lite3VdbeAddOp4I
3a560 6e 74 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 54  nt(v, OP_RowSetT
3a561 65 73 74 2c 20 72 65 67 52 6f 77 73 65 74 2c 0a  est, regRowset,.
3a562 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a563 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a564 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
3a565 65 6e 74 41 64 64 72 28 76 29 2b 32 2c 20 72 2c  entAddr(v)+2, r,
3a566 20 69 53 65 74 29 3b 0a 20 20 20 20 20 20 20 20   iSet);.        
3a567 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71    }.          sq
3a568 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
3a569 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67  v, OP_Gosub, reg
3a56a 52 65 74 75 72 6e 2c 20 69 4c 6f 6f 70 42 6f 64  Return, iLoopBod
3a56b 79 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f  y);..          /
3a56c 2a 20 54 68 65 20 70 53 75 62 57 49 6e 66 6f 2d  * The pSubWInfo-
3a56d 3e 75 6e 74 65 73 74 65 64 54 65 72 6d 73 20 66  >untestedTerms f
3a56e 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74  lag means that t
3a56f 68 69 73 20 4f 52 20 74 65 72 6d 0a 20 20 20 20  his OR term.    
3a570 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e        ** contain
3a571 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 41  ed one or more A
3a572 4e 44 20 74 65 72 6d 20 66 72 6f 6d 20 61 20 6e  ND term from a n
3a573 6f 74 52 65 61 64 79 20 74 61 62 6c 65 2e 20 20  otReady table.  
3a574 54 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  The.          **
3a575 20 74 65 72 6d 73 20 66 72 6f 6d 20 74 68 65 20   terms from the 
3a576 6e 6f 74 52 65 61 64 79 20 74 61 62 6c 65 20 63  notReady table c
3a577 6f 75 6c 64 20 6e 6f 74 20 62 65 20 74 65 73 74  ould not be test
3a578 65 64 20 61 6e 64 20 77 69 6c 6c 0a 20 20 20 20  ed and will.    
3a579 20 20 20 20 20 20 2a 2a 20 6e 65 65 64 20 74 6f        ** need to
3a57a 20 62 65 20 74 65 73 74 65 64 20 6c 61 74 65 72   be tested later
3a57b 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
3a57c 20 20 20 20 20 20 20 20 20 69 66 28 20 70 53 75           if( pSu
3a57d 62 57 49 6e 66 6f 2d 3e 75 6e 74 65 73 74 65 64  bWInfo->untested
3a57e 54 65 72 6d 73 20 29 20 75 6e 74 65 73 74 65 64  Terms ) untested
3a57f 54 65 72 6d 73 20 3d 20 31 3b 0a 0a 20 20 20 20  Terms = 1;..    
3a580 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6c 6c 20        /* If all 
3a581 6f 66 20 74 68 65 20 4f 52 2d 63 6f 6e 6e 65 63  of the OR-connec
3a582 74 65 64 20 74 65 72 6d 73 20 61 72 65 20 6f 70  ted terms are op
3a583 74 69 6d 69 7a 65 64 20 75 73 69 6e 67 20 74 68  timized using th
3a584 65 20 73 61 6d 65 0a 20 20 20 20 20 20 20 20 20  e same.         
3a585 20 2a 2a 20 69 6e 64 65 78 2c 20 61 6e 64 20 74   ** index, and t
3a586 68 65 20 69 6e 64 65 78 20 69 73 20 6f 70 65 6e  he index is open
3a587 65 64 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  ed using the sam
3a588 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 0a  e cursor number.
3a589 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 79 20            ** by 
3a58a 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c  each call to sql
3a58b 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
3a58c 20 6d 61 64 65 20 62 79 20 74 68 69 73 20 6c 6f   made by this lo
3a58d 6f 70 2c 20 69 74 20 6d 61 79 0a 20 20 20 20 20  op, it may.     
3a58e 20 20 20 20 20 2a 2a 20 62 65 20 70 6f 73 73 69       ** be possi
3a58f 62 6c 65 20 74 6f 20 75 73 65 20 74 68 61 74 20  ble to use that 
3a590 69 6e 64 65 78 20 61 73 20 61 20 63 6f 76 65 72  index as a cover
3a591 69 6e 67 20 69 6e 64 65 78 2e 0a 20 20 20 20 20  ing index..     
3a592 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
3a593 20 20 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c    ** If the call
3a594 20 74 6f 20 73 71 6c 69 74 65 33 57 68 65 72 65   to sqlite3Where
3a595 42 65 67 69 6e 28 29 20 61 62 6f 76 65 20 72 65  Begin() above re
3a596 73 75 6c 74 65 64 20 69 6e 20 61 20 73 63 61 6e  sulted in a scan
3a597 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 20 20   that.          
3a598 2a 2a 20 75 73 65 73 20 61 6e 20 69 6e 64 65 78  ** uses an index
3a599 2c 20 61 6e 64 20 74 68 69 73 20 69 73 20 65 69  , and this is ei
3a59a 74 68 65 72 20 74 68 65 20 66 69 72 73 74 20 4f  ther the first O
3a59b 52 2d 63 6f 6e 6e 65 63 74 65 64 20 74 65 72 6d  R-connected term
3a59c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 72  .          ** pr
3a59d 6f 63 65 73 73 65 64 20 6f 72 20 74 68 65 20 69  ocessed or the i
3a59e 6e 64 65 78 20 69 73 20 74 68 65 20 73 61 6d 65  ndex is the same
3a59f 20 61 73 20 74 68 61 74 20 75 73 65 64 20 62 79   as that used by
3a5a0 20 61 6c 6c 20 70 72 65 76 69 6f 75 73 0a 20 20   all previous.  
3a5a1 20 20 20 20 20 20 20 20 2a 2a 20 74 65 72 6d 73          ** terms
3a5a2 2c 20 73 65 74 20 70 43 6f 76 20 74 6f 20 74 68  , set pCov to th
3a5a3 65 20 63 61 6e 64 69 64 61 74 65 20 63 6f 76 65  e candidate cove
3a5a4 72 69 6e 67 20 69 6e 64 65 78 2e 20 4f 74 68 65  ring index. Othe
3a5a5 72 77 69 73 65 2c 20 73 65 74 20 0a 20 20 20 20  rwise, set .    
3a5a6 20 20 20 20 20 20 2a 2a 20 70 43 6f 76 20 74 6f        ** pCov to
3a5a7 20 4e 55 4c 4c 20 74 6f 20 69 6e 64 69 63 61 74   NULL to indicat
3a5a8 65 20 74 68 61 74 20 6e 6f 20 63 61 6e 64 69 64  e that no candid
3a5a9 61 74 65 20 63 6f 76 65 72 69 6e 67 20 69 6e 64  ate covering ind
3a5aa 65 78 20 77 69 6c 6c 20 0a 20 20 20 20 20 20 20  ex will .       
3a5ab 20 20 20 2a 2a 20 62 65 20 61 76 61 69 6c 61 62     ** be availab
3a5ac 6c 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  le..          */
3a5ad 0a 20 20 20 20 20 20 20 20 20 20 70 4c 76 6c 20  .          pLvl 
3a5ae 3d 20 26 70 53 75 62 57 49 6e 66 6f 2d 3e 61 5b  = &pSubWInfo->a[
3a5af 30 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  0];.          if
3a5b0 28 20 28 70 4c 76 6c 2d 3e 70 6c 61 6e 2e 77 73  ( (pLvl->plan.ws
3a5b1 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e  Flags & WHERE_IN
3a5b2 44 45 58 45 44 29 21 3d 30 0a 20 20 20 20 20 20  DEXED)!=0.      
3a5b3 20 20 20 20 20 26 26 20 28 70 4c 76 6c 2d 3e 70       && (pLvl->p
3a5b4 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
3a5b5 45 52 45 5f 54 45 4d 50 5f 49 4e 44 45 58 29 3d  ERE_TEMP_INDEX)=
3a5b6 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 26 26  =0.           &&
3a5b7 20 28 69 69 3d 3d 30 20 7c 7c 20 70 4c 76 6c 2d   (ii==0 || pLvl-
3a5b8 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d 70 43  >plan.u.pIdx==pC
3a5b9 6f 76 29 0a 20 20 20 20 20 20 20 20 20 20 29 7b  ov).          ){
3a5ba 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
3a5bb 65 72 74 28 20 70 4c 76 6c 2d 3e 69 49 64 78 43  ert( pLvl->iIdxC
3a5bc 75 72 3d 3d 69 43 6f 76 43 75 72 20 29 3b 0a 20  ur==iCovCur );. 
3a5bd 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 76 20             pCov 
3a5be 3d 20 70 4c 76 6c 2d 3e 70 6c 61 6e 2e 75 2e 70  = pLvl->plan.u.p
3a5bf 49 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  Idx;.          }
3a5c0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
3a5c1 20 20 70 43 6f 76 20 3d 20 30 3b 0a 20 20 20 20    pCov = 0;.    
3a5c2 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
3a5c3 20 20 20 2f 2a 20 46 69 6e 69 73 68 20 74 68 65     /* Finish the
3a5c4 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 61   loop through ta
3a5c5 62 6c 65 20 65 6e 74 72 69 65 73 20 74 68 61 74  ble entries that
3a5c6 20 6d 61 74 63 68 20 74 65 72 6d 20 70 4f 72 54   match term pOrT
3a5c7 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  erm. */.        
3a5c8 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e    sqlite3WhereEn
3a5c9 64 28 70 53 75 62 57 49 6e 66 6f 29 3b 0a 20 20  d(pSubWInfo);.  
3a5ca 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
3a5cb 20 20 20 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c      }.    pLevel
3a5cc 2d 3e 75 2e 70 43 6f 76 69 64 78 20 3d 20 70 43  ->u.pCovidx = pC
3a5cd 6f 76 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 76  ov;.    if( pCov
3a5ce 20 29 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43   ) pLevel->iIdxC
3a5cf 75 72 20 3d 20 69 43 6f 76 43 75 72 3b 0a 20 20  ur = iCovCur;.  
3a5d0 20 20 69 66 28 20 70 41 6e 64 45 78 70 72 20 29    if( pAndExpr )
3a5d1 7b 0a 20 20 20 20 20 20 70 41 6e 64 45 78 70 72  {.      pAndExpr
3a5d2 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20  ->pLeft = 0;.   
3a5d3 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
3a5d4 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
3a5d5 20 70 41 6e 64 45 78 70 72 29 3b 0a 20 20 20 20   pAndExpr);.    
3a5d6 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
3a5d7 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69 52 65  eChangeP1(v, iRe
3a5d8 74 49 6e 69 74 2c 20 73 71 6c 69 74 65 33 56 64  tInit, sqlite3Vd
3a5d9 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
3a5da 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
3a5db 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
3a5dc 6f 74 6f 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e  oto, 0, pLevel->
3a5dd 61 64 64 72 42 72 6b 29 3b 0a 20 20 20 20 73 71  addrBrk);.    sq
3a5de 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
3a5df 4c 61 62 65 6c 28 76 2c 20 69 4c 6f 6f 70 42 6f  Label(v, iLoopBo
3a5e0 64 79 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 57  dy);..    if( pW
3a5e1 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3e 31 20 29  Info->nLevel>1 )
3a5e2 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
3a5e3 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f  e(pParse->db, pO
3a5e4 72 54 61 62 29 3b 0a 20 20 20 20 69 66 28 20 21  rTab);.    if( !
3a5e5 75 6e 74 65 73 74 65 64 54 65 72 6d 73 20 29 20  untestedTerms ) 
3a5e6 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76  disableTerm(pLev
3a5e7 65 6c 2c 20 70 54 65 72 6d 29 3b 0a 20 20 7d 65  el, pTerm);.  }e
3a5e8 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  lse.#endif /* SQ
3a5e9 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54  LITE_OMIT_OR_OPT
3a5ea 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 20 20  IMIZATION */..  
3a5eb 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 35 3a  {.    /* Case 5:
3a5ec 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 75 73    There is no us
3a5ed 61 62 6c 65 20 69 6e 64 65 78 2e 20 20 57 65 20  able index.  We 
3a5ee 6d 75 73 74 20 64 6f 20 61 20 63 6f 6d 70 6c 65  must do a comple
3a5ef 74 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  te.    **       
3a5f0 20 20 20 73 63 61 6e 20 6f 66 20 74 68 65 20 65     scan of the e
3a5f1 6e 74 69 72 65 20 74 61 62 6c 65 2e 0a 20 20 20  ntire table..   
3a5f2 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20 63   */.    static c
3a5f3 6f 6e 73 74 20 75 38 20 61 53 74 65 70 5b 5d 20  onst u8 aStep[] 
3a5f4 3d 20 7b 20 4f 50 5f 4e 65 78 74 2c 20 4f 50 5f  = { OP_Next, OP_
3a5f5 50 72 65 76 20 7d 3b 0a 20 20 20 20 73 74 61 74  Prev };.    stat
3a5f6 69 63 20 63 6f 6e 73 74 20 75 38 20 61 53 74 61  ic const u8 aSta
3a5f7 72 74 5b 5d 20 3d 20 7b 20 4f 50 5f 52 65 77 69  rt[] = { OP_Rewi
3a5f8 6e 64 2c 20 4f 50 5f 4c 61 73 74 20 7d 3b 0a 20  nd, OP_Last };. 
3a5f9 20 20 20 61 73 73 65 72 74 28 20 62 52 65 76 3d     assert( bRev=
3a5fa 3d 30 20 7c 7c 20 62 52 65 76 3d 3d 31 20 29 3b  =0 || bRev==1 );
3a5fb 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6d 69  .    assert( omi
3a5fc 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20  tTable==0 );.   
3a5fd 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 61 53   pLevel->op = aS
3a5fe 74 65 70 5b 62 52 65 76 5d 3b 0a 20 20 20 20 70  tep[bRev];.    p
3a5ff 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72  Level->p1 = iCur
3a600 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32  ;.    pLevel->p2
3a601 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 56 64   = 1 + sqlite3Vd
3a602 62 65 41 64 64 4f 70 32 28 76 2c 20 61 53 74 61  beAddOp2(v, aSta
3a603 72 74 5b 62 52 65 76 5d 2c 20 69 43 75 72 2c 20  rt[bRev], iCur, 
3a604 61 64 64 72 42 72 6b 29 3b 0a 20 20 20 20 70 4c  addrBrk);.    pL
3a605 65 76 65 6c 2d 3e 70 35 20 3d 20 53 51 4c 49 54  evel->p5 = SQLIT
3a606 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55 4c  E_STMTSTATUS_FUL
3a607 4c 53 43 41 4e 5f 53 54 45 50 3b 0a 20 20 7d 0a  LSCAN_STEP;.  }.
3a608 20 20 6e 6f 74 52 65 61 64 79 20 26 3d 20 7e 67    notReady &= ~g
3a609 65 74 4d 61 73 6b 28 70 57 43 2d 3e 70 4d 61 73  etMask(pWC->pMas
3a60a 6b 53 65 74 2c 20 69 43 75 72 29 3b 0a 0a 20 20  kSet, iCur);..  
3a60b 2f 2a 20 49 6e 73 65 72 74 20 63 6f 64 65 20 74  /* Insert code t
3a60c 6f 20 74 65 73 74 20 65 76 65 72 79 20 73 75 62  o test every sub
3a60d 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
3a60e 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c  can be completel
3a60f 79 0a 20 20 2a 2a 20 63 6f 6d 70 75 74 65 64 20  y.  ** computed 
3a610 75 73 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e  using the curren
3a611 74 20 73 65 74 20 6f 66 20 74 61 62 6c 65 73 2e  t set of tables.
3a612 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 4d 50 4c 45  .  **.  ** IMPLE
3a613 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d  MENTATION-OF: R-
3a614 34 39 35 32 35 2d 35 30 39 33 35 20 54 65 72 6d  49525-50935 Term
3a615 73 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 65  s that cannot be
3a616 20 73 61 74 69 73 66 69 65 64 20 74 68 72 6f 75   satisfied throu
3a617 67 68 0a 20 20 2a 2a 20 74 68 65 20 75 73 65 20  gh.  ** the use 
3a618 6f 66 20 69 6e 64 69 63 65 73 20 62 65 63 6f 6d  of indices becom
3a619 65 20 74 65 73 74 73 20 74 68 61 74 20 61 72 65  e tests that are
3a61a 20 65 76 61 6c 75 61 74 65 64 20 61 67 61 69 6e   evaluated again
3a61b 73 74 20 65 61 63 68 20 72 6f 77 20 6f 66 0a 20  st each row of. 
3a61c 20 2a 2a 20 74 68 65 20 72 65 6c 65 76 61 6e 74   ** the relevant
3a61d 20 69 6e 70 75 74 20 74 61 62 6c 65 73 2e 0a 20   input tables.. 
3a61e 20 2a 2f 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d   */.  for(pTerm=
3a61f 70 57 43 2d 3e 61 2c 20 6a 3d 70 57 43 2d 3e 6e  pWC->a, j=pWC->n
3a620 54 65 72 6d 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c 20  Term; j>0; j--, 
3a621 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 45 78  pTerm++){.    Ex
3a622 70 72 20 2a 70 45 3b 0a 20 20 20 20 74 65 73 74  pr *pE;.    test
3a623 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 77 74 46  case( pTerm->wtF
3a624 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54  lags & TERM_VIRT
3a625 55 41 4c 20 29 3b 20 2f 2a 20 49 4d 50 3a 20 52  UAL ); /* IMP: R
3a626 2d 33 30 35 37 35 2d 31 31 36 36 32 20 2a 2f 0a  -30575-11662 */.
3a627 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54      testcase( pT
3a628 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  erm->wtFlags & T
3a629 45 52 4d 5f 43 4f 44 45 44 20 29 3b 0a 20 20 20  ERM_CODED );.   
3a62a 20 69 66 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c   if( pTerm->wtFl
3a62b 61 67 73 20 26 20 28 54 45 52 4d 5f 56 49 52 54  ags & (TERM_VIRT
3a62c 55 41 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29 20  UAL|TERM_CODED) 
3a62d 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
3a62e 69 66 28 20 28 70 54 65 72 6d 2d 3e 70 72 65 72  if( (pTerm->prer
3a62f 65 71 41 6c 6c 20 26 20 6e 6f 74 52 65 61 64 79  eqAll & notReady
3a630 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 74 65  )!=0 ){.      te
3a631 73 74 63 61 73 65 28 20 70 57 49 6e 66 6f 2d 3e  stcase( pWInfo->
3a632 75 6e 74 65 73 74 65 64 54 65 72 6d 73 3d 3d 30  untestedTerms==0
3a633 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3a634 26 26 20 28 70 57 49 6e 66 6f 2d 3e 77 63 74 72  && (pWInfo->wctr
3a635 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f  lFlags & WHERE_O
3a636 4e 45 54 41 42 4c 45 5f 4f 4e 4c 59 29 21 3d 30  NETABLE_ONLY)!=0
3a637 20 29 3b 0a 20 20 20 20 20 20 70 57 49 6e 66 6f   );.      pWInfo
3a638 2d 3e 75 6e 74 65 73 74 65 64 54 65 72 6d 73 20  ->untestedTerms 
3a639 3d 20 31 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69  = 1;.      conti
3a63a 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  nue;.    }.    p
3a63b 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  E = pTerm->pExpr
3a63c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45  ;.    assert( pE
3a63d 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70  !=0 );.    if( p
3a63e 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e  Level->iLeftJoin
3a63f 20 26 26 20 21 45 78 70 72 48 61 73 50 72 6f 70   && !ExprHasProp
3a640 65 72 74 79 28 70 45 2c 20 45 50 5f 46 72 6f 6d  erty(pE, EP_From
3a641 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 20 20 63  Join) ){.      c
3a642 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20  ontinue;.    }. 
3a643 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
3a644 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45  False(pParse, pE
3a645 2c 20 61 64 64 72 43 6f 6e 74 2c 20 53 51 4c 49  , addrCont, SQLI
3a646 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a  TE_JUMPIFNULL);.
3a647 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61      pTerm->wtFla
3a648 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44  gs |= TERM_CODED
3a649 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20  ;.  }..  /* For 
3a64a 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  a LEFT OUTER JOI
3a64b 4e 2c 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65  N, generate code
3a64c 20 74 68 61 74 20 77 69 6c 6c 20 72 65 63 6f 72   that will recor
3a64d 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 0a  d the fact that.
3a64e 20 20 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e    ** at least on
3a64f 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72 69 67  e row of the rig
3a650 68 74 20 74 61 62 6c 65 20 68 61 73 20 6d 61 74  ht table has mat
3a651 63 68 65 64 20 74 68 65 20 6c 65 66 74 20 74 61  ched the left ta
3a652 62 6c 65 2e 20 20 0a 20 20 2a 2f 0a 20 20 69 66  ble.  .  */.  if
3a653 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a  ( pLevel->iLeftJ
3a654 6f 69 6e 20 29 7b 0a 20 20 20 20 70 4c 65 76 65  oin ){.    pLeve
3a655 6c 2d 3e 61 64 64 72 46 69 72 73 74 20 3d 20 73  l->addrFirst = s
3a656 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
3a657 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71  tAddr(v);.    sq
3a658 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
3a659 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31  v, OP_Integer, 1
3a65a 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a  , pLevel->iLeftJ
3a65b 6f 69 6e 29 3b 0a 20 20 20 20 56 64 62 65 43 6f  oin);.    VdbeCo
3a65c 6d 6d 65 6e 74 28 28 76 2c 20 22 72 65 63 6f 72  mment((v, "recor
3a65d 64 20 4c 45 46 54 20 4a 4f 49 4e 20 68 69 74 22  d LEFT JOIN hit"
3a65e 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  ));.    sqlite3E
3a65f 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50  xprCacheClear(pP
3a660 61 72 73 65 29 3b 0a 20 20 20 20 66 6f 72 28 70  arse);.    for(p
3a661 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6a 3d 30  Term=pWC->a, j=0
3a662 3b 20 6a 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b 20  ; j<pWC->nTerm; 
3a663 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20  j++, pTerm++){. 
3a664 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
3a665 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
3a666 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 20  TERM_VIRTUAL ); 
3a667 20 2f 2a 20 49 4d 50 3a 20 52 2d 33 30 35 37 35   /* IMP: R-30575
3a668 2d 31 31 36 36 32 20 2a 2f 0a 20 20 20 20 20 20  -11662 */.      
3a669 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d  testcase( pTerm-
3a66a 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
3a66b 43 4f 44 45 44 20 29 3b 0a 20 20 20 20 20 20 69  CODED );.      i
3a66c 66 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67  f( pTerm->wtFlag
3a66d 73 20 26 20 28 54 45 52 4d 5f 56 49 52 54 55 41  s & (TERM_VIRTUA
3a66e 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29 20 29 20  L|TERM_CODED) ) 
3a66f 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
3a670 69 66 28 20 28 70 54 65 72 6d 2d 3e 70 72 65 72  if( (pTerm->prer
3a671 65 71 41 6c 6c 20 26 20 6e 6f 74 52 65 61 64 79  eqAll & notReady
3a672 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
3a673 61 73 73 65 72 74 28 20 70 57 49 6e 66 6f 2d 3e  assert( pWInfo->
3a674 75 6e 74 65 73 74 65 64 54 65 72 6d 73 20 29 3b  untestedTerms );
3a675 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  .        continu
3a676 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
3a677 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e   assert( pTerm->
3a678 70 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20 73  pExpr );.      s
3a679 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
3a67a 65 28 70 50 61 72 73 65 2c 20 70 54 65 72 6d 2d  e(pParse, pTerm-
3a67b 3e 70 45 78 70 72 2c 20 61 64 64 72 43 6f 6e 74  >pExpr, addrCont
3a67c 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  , SQLITE_JUMPIFN
3a67d 55 4c 4c 29 3b 0a 20 20 20 20 20 20 70 54 65 72  ULL);.      pTer
3a67e 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45  m->wtFlags |= TE
3a67f 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 20 20 7d 0a  RM_CODED;.    }.
3a680 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c    }.  sqlite3Rel
3a681 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
3a682 73 65 2c 20 69 52 65 6c 65 61 73 65 52 65 67 29  se, iReleaseReg)
3a683 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6e 6f 74 52  ;..  return notR
3a684 65 61 64 79 3b 0a 7d 0a 0a 23 69 66 20 64 65 66  eady;.}..#if def
3a685 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
3a686 29 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  )./*.** The foll
3a687 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 20 68  owing variable h
3a688 6f 6c 64 73 20 61 20 74 65 78 74 20 64 65 73 63  olds a text desc
3a689 72 69 70 74 69 6f 6e 20 6f 66 20 71 75 65 72 79  ription of query
3a68a 20 70 6c 61 6e 20 67 65 6e 65 72 61 74 65 64 0a   plan generated.
3a68b 2a 2a 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72  ** by the most r
3a68c 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71  ecent call to sq
3a68d 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
3a68e 29 2e 20 20 45 61 63 68 20 63 61 6c 6c 20 74 6f  ).  Each call to
3a68f 20 57 68 65 72 65 42 65 67 69 6e 0a 2a 2a 20 6f   WhereBegin.** o
3a690 76 65 72 77 72 69 74 65 73 20 74 68 65 20 70 72  verwrites the pr
3a691 65 76 69 6f 75 73 2e 20 20 54 68 69 73 20 69 6e  evious.  This in
3a692 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65  formation is use
3a693 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e  d for testing an
3a694 64 0a 2a 2a 20 61 6e 61 6c 79 73 69 73 20 6f 6e  d.** analysis on
3a695 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ly..*/.SQLITE_AP
3a696 49 20 63 68 61 72 20 73 71 6c 69 74 65 33 5f 71  I char sqlite3_q
3a697 75 65 72 79 5f 70 6c 61 6e 5b 42 4d 53 2a 32 2a  uery_plan[BMS*2*
3a698 34 30 5d 3b 20 20 2f 2a 20 54 65 78 74 20 6f 66  40];  /* Text of
3a699 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 73 74 61   the join */.sta
3a69a 74 69 63 20 69 6e 74 20 6e 51 50 6c 61 6e 20 3d  tic int nQPlan =
3a69b 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
3a69c 20 2f 2a 20 4e 65 78 74 20 66 72 65 65 20 73 6c   /* Next free sl
3a69d 6f 77 20 69 6e 20 5f 71 75 65 72 79 5f 70 6c 61  ow in _query_pla
3a69e 6e 5b 5d 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f  n[] */..#endif /
3a69f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f  * SQLITE_TEST */
3a6a0 0a 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20  .../*.** Free a 
3a6a1 57 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63 74  WhereInfo struct
3a6a2 75 72 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ure.*/.static vo
3a6a3 69 64 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65  id whereInfoFree
3a6a4 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 57 68  (sqlite3 *db, Wh
3a6a5 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 29  ereInfo *pWInfo)
3a6a6 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70  {.  if( ALWAYS(p
3a6a7 57 49 6e 66 6f 29 20 29 7b 0a 20 20 20 20 69 6e  WInfo) ){.    in
3a6a8 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
3a6a9 3b 20 69 3c 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76  ; i<pWInfo->nLev
3a6aa 65 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  el; i++){.      
3a6ab 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
3a6ac 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 57 49 6e  fo *pInfo = pWIn
3a6ad 66 6f 2d 3e 61 5b 69 5d 2e 70 49 64 78 49 6e 66  fo->a[i].pIdxInf
3a6ae 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 6e  o;.      if( pIn
3a6af 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  fo ){.        /*
3a6b0 20 61 73 73 65 72 74 28 20 70 49 6e 66 6f 2d 3e   assert( pInfo->
3a6b1 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72  needToFreeIdxStr
3a6b2 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  ==0 || db->mallo
3a6b3 63 46 61 69 6c 65 64 20 29 3b 20 2a 2f 0a 20 20  cFailed ); */.  
3a6b4 20 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 2d        if( pInfo-
3a6b5 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74  >needToFreeIdxSt
3a6b6 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  r ){.          s
3a6b7 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49 6e 66  qlite3_free(pInf
3a6b8 6f 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 20 20  o->idxStr);.    
3a6b9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
3a6ba 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
3a6bb 70 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 7d 0a  pInfo);.      }.
3a6bc 20 20 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f        if( pWInfo
3a6bd 2d 3e 61 5b 69 5d 2e 70 6c 61 6e 2e 77 73 46 6c  ->a[i].plan.wsFl
3a6be 61 67 73 20 26 20 57 48 45 52 45 5f 54 45 4d 50  ags & WHERE_TEMP
3a6bf 5f 49 4e 44 45 58 20 29 7b 0a 20 20 20 20 20 20  _INDEX ){.      
3a6c0 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20    Index *pIdx = 
3a6c1 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 2e 70 6c 61  pWInfo->a[i].pla
3a6c2 6e 2e 75 2e 70 49 64 78 3b 0a 20 20 20 20 20 20  n.u.pIdx;.      
3a6c3 20 20 69 66 28 20 70 49 64 78 20 29 7b 0a 20 20    if( pIdx ){.  
3a6c4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
3a6c5 62 46 72 65 65 28 64 62 2c 20 70 49 64 78 2d 3e  bFree(db, pIdx->
3a6c6 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 20 20 20 20  zColAff);.      
3a6c7 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
3a6c8 65 28 64 62 2c 20 70 49 64 78 29 3b 0a 20 20 20  e(db, pIdx);.   
3a6c9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
3a6ca 20 20 20 7d 0a 20 20 20 20 77 68 65 72 65 43 6c     }.    whereCl
3a6cb 61 75 73 65 43 6c 65 61 72 28 70 57 49 6e 66 6f  auseClear(pWInfo
3a6cc 2d 3e 70 57 43 29 3b 0a 20 20 20 20 73 71 6c 69  ->pWC);.    sqli
3a6cd 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 57  te3DbFree(db, pW
3a6ce 49 6e 66 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f  Info);.  }.}.../
3a6cf 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68  *.** Generate th
3a6d0 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
3a6d1 68 65 20 6c 6f 6f 70 20 75 73 65 64 20 66 6f 72  he loop used for
3a6d2 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72   WHERE clause pr
3a6d3 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 20 54 68 65  ocessing..** The
3a6d4 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
3a6d5 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
3a6d6 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72   opaque structur
3a6d7 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a  e that contains.
3a6d8 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e  ** information n
3a6d9 65 65 64 65 64 20 74 6f 20 74 65 72 6d 69 6e 61  eeded to termina
3a6da 74 65 20 74 68 65 20 6c 6f 6f 70 2e 20 20 4c 61  te the loop.  La
3a6db 74 65 72 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67  ter, the calling
3a6dc 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 73 68 6f 75   routine.** shou
3a6dd 6c 64 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65  ld invoke sqlite
3a6de 33 57 68 65 72 65 45 6e 64 28 29 20 77 69 74 68  3WhereEnd() with
3a6df 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
3a6e0 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69  e of this functi
3a6e1 6f 6e 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74  on.** in order t
3a6e2 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 57  o complete the W
3a6e3 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63  HERE clause proc
3a6e4 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66  essing..**.** If
3a6e5 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
3a6e6 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  , this routine r
3a6e7 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  eturns NULL..**.
3a6e8 2a 2a 20 54 68 65 20 62 61 73 69 63 20 69 64 65  ** The basic ide
3a6e9 61 20 69 73 20 74 6f 20 64 6f 20 61 20 6e 65 73  a is to do a nes
3a6ea 74 65 64 20 6c 6f 6f 70 2c 20 6f 6e 65 20 6c 6f  ted loop, one lo
3a6eb 6f 70 20 66 6f 72 20 65 61 63 68 20 74 61 62 6c  op for each tabl
3a6ec 65 20 69 6e 0a 2a 2a 20 74 68 65 20 46 52 4f 4d  e in.** the FROM
3a6ed 20 63 6c 61 75 73 65 20 6f 66 20 61 20 73 65 6c   clause of a sel
3a6ee 65 63 74 2e 20 20 28 49 4e 53 45 52 54 20 61 6e  ect.  (INSERT an
3a6ef 64 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  d UPDATE stateme
3a6f0 6e 74 73 20 61 72 65 20 74 68 65 0a 2a 2a 20 73  nts are the.** s
3a6f1 61 6d 65 20 61 73 20 61 20 53 45 4c 45 43 54 20  ame as a SELECT 
3a6f2 77 69 74 68 20 6f 6e 6c 79 20 61 20 73 69 6e 67  with only a sing
3a6f3 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20  le table in the 
3a6f4 46 52 4f 4d 20 63 6c 61 75 73 65 2e 29 20 20 46  FROM clause.)  F
3a6f5 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69  or.** example, i
3a6f6 66 20 74 68 65 20 53 51 4c 20 69 73 20 74 68 69  f the SQL is thi
3a6f7 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53  s:.**.**       S
3a6f8 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c  ELECT * FROM t1,
3a6f9 20 74 32 2c 20 74 33 20 57 48 45 52 45 20 2e 2e   t2, t3 WHERE ..
3a6fa 2e 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 74 68  .;.**.** Then th
3a6fb 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64  e code generated
3a6fc 20 69 73 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79   is conceptually
3a6fd 20 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f 77   like the follow
3a6fe 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ing:.**.**      
3a6ff 66 6f 72 65 61 63 68 20 72 6f 77 31 20 69 6e 20  foreach row1 in 
3a700 74 31 20 64 6f 20 20 20 20 20 20 20 5c 20 20 20  t1 do       \   
3a701 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a   Code generated.
3a702 2a 2a 20 20 20 20 20 20 20 20 66 6f 72 65 61 63  **        foreac
3a703 68 20 72 6f 77 32 20 69 6e 20 74 32 20 64 6f 20  h row2 in t2 do 
3a704 20 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69       |-- by sqli
3a705 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 0a  te3WhereBegin().
3a706 2a 2a 20 20 20 20 20 20 20 20 20 20 66 6f 72 65  **          fore
3a707 61 63 68 20 72 6f 77 33 20 69 6e 20 74 33 20 64  ach row3 in t3 d
3a708 6f 20 20 20 2f 0a 2a 2a 20 20 20 20 20 20 20 20  o   /.**        
3a709 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20 20      ....**      
3a70a 20 20 20 20 65 6e 64 20 20 20 20 20 20 20 20 20      end         
3a70b 20 20 20 20 20 20 20 20 20 20 20 20 5c 20 20 20              \   
3a70c 20 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a   Code generated.
3a70d 2a 2a 20 20 20 20 20 20 20 20 65 6e 64 20 20 20  **        end   
3a70e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a70f 20 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69       |-- by sqli
3a710 74 65 33 57 68 65 72 65 45 6e 64 28 29 0a 2a 2a  te3WhereEnd().**
3a711 20 20 20 20 20 20 65 6e 64 20 20 20 20 20 20 20        end       
3a712 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a713 20 20 2f 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74    /.**.** Note t
3a714 68 61 74 20 74 68 65 20 6c 6f 6f 70 73 20 6d 69  hat the loops mi
3a715 67 68 74 20 6e 6f 74 20 62 65 20 6e 65 73 74 65  ght not be neste
3a716 64 20 69 6e 20 74 68 65 20 6f 72 64 65 72 20 69  d in the order i
3a717 6e 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a 20  n which they.** 
3a718 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 46 52  appear in the FR
3a719 4f 4d 20 63 6c 61 75 73 65 20 69 66 20 61 20 64  OM clause if a d
3a71a 69 66 66 65 72 65 6e 74 20 6f 72 64 65 72 20 69  ifferent order i
3a71b 73 20 62 65 74 74 65 72 20 61 62 6c 65 20 74 6f  s better able to
3a71c 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20   make.** use of 
3a71d 69 6e 64 69 63 65 73 2e 20 20 4e 6f 74 65 20 61  indices.  Note a
3a71e 6c 73 6f 20 74 68 61 74 20 77 68 65 6e 20 74 68  lso that when th
3a71f 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 61 70  e IN operator ap
3a720 70 65 61 72 73 20 69 6e 0a 2a 2a 20 74 68 65 20  pears in.** the 
3a721 57 48 45 52 45 20 63 6c 61 75 73 65 2c 20 69 74  WHERE clause, it
3a722 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e   might result in
3a723 20 61 64 64 69 74 69 6f 6e 61 6c 20 6e 65 73 74   additional nest
3a724 65 64 20 6c 6f 6f 70 73 20 66 6f 72 0a 2a 2a 20  ed loops for.** 
3a725 73 63 61 6e 6e 69 6e 67 20 74 68 72 6f 75 67 68  scanning through
3a726 20 61 6c 6c 20 76 61 6c 75 65 73 20 6f 6e 20 74   all values on t
3a727 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  he right-hand si
3a728 64 65 20 6f 66 20 74 68 65 20 49 4e 2e 0a 2a 2a  de of the IN..**
3a729 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 42 74  .** There are Bt
3a72a 72 65 65 20 63 75 72 73 6f 72 73 20 61 73 73 6f  ree cursors asso
3a72b 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68  ciated with each
3a72c 20 74 61 62 6c 65 2e 20 20 74 31 20 75 73 65 73   table.  t1 uses
3a72d 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65   cursor.** numbe
3a72e 72 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d  r pTabList->a[0]
3a72f 2e 69 43 75 72 73 6f 72 2e 20 20 74 32 20 75 73  .iCursor.  t2 us
3a730 65 73 20 74 68 65 20 63 75 72 73 6f 72 20 70 54  es the cursor pT
3a731 61 62 4c 69 73 74 2d 3e 61 5b 31 5d 2e 69 43 75  abList->a[1].iCu
3a732 72 73 6f 72 2e 0a 2a 2a 20 41 6e 64 20 73 6f 20  rsor..** And so 
3a733 66 6f 72 74 68 2e 20 20 54 68 69 73 20 72 6f 75  forth.  This rou
3a734 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63  tine generates c
3a735 6f 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 6f 73  ode to open thos
3a736 65 20 56 44 42 45 20 63 75 72 73 6f 72 73 0a 2a  e VDBE cursors.*
3a737 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 57 68 65  * and sqlite3Whe
3a738 72 65 45 6e 64 28 29 20 67 65 6e 65 72 61 74 65  reEnd() generate
3a739 73 20 74 68 65 20 63 6f 64 65 20 74 6f 20 63 6c  s the code to cl
3a73a 6f 73 65 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20  ose them..**.** 
3a73b 54 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 71  The code that sq
3a73c 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
3a73d 29 20 67 65 6e 65 72 61 74 65 73 20 6c 65 61 76  ) generates leav
3a73e 65 73 20 74 68 65 20 63 75 72 73 6f 72 73 20 6e  es the cursors n
3a73f 61 6d 65 64 0a 2a 2a 20 69 6e 20 70 54 61 62 4c  amed.** in pTabL
3a740 69 73 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  ist pointing at 
3a741 74 68 65 69 72 20 61 70 70 72 6f 70 72 69 61 74  their appropriat
3a742 65 20 65 6e 74 72 69 65 73 2e 20 20 54 68 65 20  e entries.  The 
3a743 5b 2e 2e 2e 5d 20 63 6f 64 65 0a 2a 2a 20 63 61  [...] code.** ca
3a744 6e 20 75 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20  n use OP_Column 
3a745 61 6e 64 20 4f 50 5f 52 6f 77 69 64 20 6f 70 63  and OP_Rowid opc
3a746 6f 64 65 73 20 6f 6e 20 74 68 65 73 65 20 63 75  odes on these cu
3a747 72 73 6f 72 73 20 74 6f 20 65 78 74 72 61 63 74  rsors to extract
3a748 0a 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20 74 68  .** data from th
3a749 65 20 76 61 72 69 6f 75 73 20 74 61 62 6c 65 73  e various tables
3a74a 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 2a 2a   of the loop..**
3a74b 0a 2a 2a 20 49 66 20 74 68 65 20 57 48 45 52 45  .** If the WHERE
3a74c 20 63 6c 61 75 73 65 20 69 73 20 65 6d 70 74 79   clause is empty
3a74d 2c 20 74 68 65 20 66 6f 72 65 61 63 68 20 6c 6f  , the foreach lo
3a74e 6f 70 73 20 6d 75 73 74 20 65 61 63 68 20 73 63  ops must each sc
3a74f 61 6e 20 74 68 65 69 72 0a 2a 2a 20 65 6e 74 69  an their.** enti
3a750 72 65 20 74 61 62 6c 65 73 2e 20 20 54 68 75 73  re tables.  Thus
3a751 20 61 20 74 68 72 65 65 2d 77 61 79 20 6a 6f 69   a three-way joi
3a752 6e 20 69 73 20 61 6e 20 4f 28 4e 5e 33 29 20 6f  n is an O(N^3) o
3a753 70 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 69  peration.  But i
3a754 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 73 20  f.** the tables 
3a755 68 61 76 65 20 69 6e 64 69 63 65 73 20 61 6e 64  have indices and
3a756 20 74 68 65 72 65 20 61 72 65 20 74 65 72 6d 73   there are terms
3a757 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c   in the WHERE cl
3a758 61 75 73 65 20 74 68 61 74 0a 2a 2a 20 72 65 66  ause that.** ref
3a759 65 72 20 74 6f 20 74 68 6f 73 65 20 69 6e 64 69  er to those indi
3a75a 63 65 73 2c 20 61 20 63 6f 6d 70 6c 65 74 65 20  ces, a complete 
3a75b 74 61 62 6c 65 20 73 63 61 6e 20 63 61 6e 20 62  table scan can b
3a75c 65 20 61 76 6f 69 64 65 64 20 61 6e 64 20 74 68  e avoided and th
3a75d 65 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20 72  e.** code will r
3a75e 75 6e 20 6d 75 63 68 20 66 61 73 74 65 72 2e 20  un much faster. 
3a75f 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72   Most of the wor
3a760 6b 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  k of this routin
3a761 65 20 69 73 20 63 68 65 63 6b 69 6e 67 0a 2a 2a  e is checking.**
3a762 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 72 65   to see if there
3a763 20 61 72 65 20 69 6e 64 69 63 65 73 20 74 68 61   are indices tha
3a764 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  t can be used to
3a765 20 73 70 65 65 64 20 75 70 20 74 68 65 20 6c 6f   speed up the lo
3a766 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 65 72 6d 73 20  op..**.** Terms 
3a767 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
3a768 75 73 65 20 61 72 65 20 61 6c 73 6f 20 75 73 65  use are also use
3a769 64 20 74 6f 20 6c 69 6d 69 74 20 77 68 69 63 68  d to limit which
3a76a 20 72 6f 77 73 20 61 63 74 75 61 6c 6c 79 0a 2a   rows actually.*
3a76b 2a 20 6d 61 6b 65 20 69 74 20 74 6f 20 74 68 65  * make it to the
3a76c 20 22 2e 2e 2e 22 20 69 6e 20 74 68 65 20 6d 69   "..." in the mi
3a76d 64 64 6c 65 20 6f 66 20 74 68 65 20 6c 6f 6f 70  ddle of the loop
3a76e 2e 20 20 41 66 74 65 72 20 65 61 63 68 20 22 66  .  After each "f
3a76f 6f 72 65 61 63 68 22 2c 0a 2a 2a 20 74 65 72 6d  oreach",.** term
3a770 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  s of the WHERE c
3a771 6c 61 75 73 65 20 74 68 61 74 20 75 73 65 20 6f  lause that use o
3a772 6e 6c 79 20 74 65 72 6d 73 20 69 6e 20 74 68 61  nly terms in tha
3a773 74 20 6c 6f 6f 70 20 61 6e 64 20 6f 75 74 65 72  t loop and outer
3a774 0a 2a 2a 20 6c 6f 6f 70 73 20 61 72 65 20 65 76  .** loops are ev
3a775 61 6c 75 61 74 65 64 20 61 6e 64 20 69 66 20 66  aluated and if f
3a776 61 6c 73 65 20 61 20 6a 75 6d 70 20 69 73 20 6d  alse a jump is m
3a777 61 64 65 20 61 72 6f 75 6e 64 20 61 6c 6c 20 73  ade around all s
3a778 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 69 6e 6e  ubsequent.** inn
3a779 65 72 20 6c 6f 6f 70 73 20 28 6f 72 20 61 72 6f  er loops (or aro
3a77a 75 6e 64 20 74 68 65 20 22 2e 2e 2e 22 20 69 66  und the "..." if
3a77b 20 74 68 65 20 74 65 73 74 20 6f 63 63 75 72 73   the test occurs
3a77c 20 77 69 74 68 69 6e 20 74 68 65 20 69 6e 6e 65   within the inne
3a77d 72 2d 0a 2a 2a 20 6d 6f 73 74 20 6c 6f 6f 70 29  r-.** most loop)
3a77e 0a 2a 2a 0a 2a 2a 20 4f 55 54 45 52 20 4a 4f 49  .**.** OUTER JOI
3a77f 4e 53 0a 2a 2a 0a 2a 2a 20 41 6e 20 6f 75 74 65  NS.**.** An oute
3a780 72 20 6a 6f 69 6e 20 6f 66 20 74 61 62 6c 65 73  r join of tables
3a781 20 74 31 20 61 6e 64 20 74 32 20 69 73 20 63 6f   t1 and t2 is co
3a782 6e 63 65 70 74 61 6c 6c 79 20 63 6f 64 65 64 20  nceptally coded 
3a783 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
3a784 2a 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77  *    foreach row
3a785 31 20 69 6e 20 74 31 20 64 6f 0a 2a 2a 20 20 20  1 in t1 do.**   
3a786 20 20 20 66 6c 61 67 20 3d 20 30 0a 2a 2a 20 20     flag = 0.**  
3a787 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 32      foreach row2
3a788 20 69 6e 20 74 32 20 64 6f 0a 2a 2a 20 20 20 20   in t2 do.**    
3a789 20 20 20 20 73 74 61 72 74 3a 0a 2a 2a 20 20 20      start:.**   
3a78a 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20         ....**   
3a78b 20 20 20 20 20 20 20 66 6c 61 67 20 3d 20 31 0a         flag = 1.
3a78c 2a 2a 20 20 20 20 20 20 65 6e 64 0a 2a 2a 20 20  **      end.**  
3a78d 20 20 20 20 69 66 20 66 6c 61 67 3d 3d 30 20 74      if flag==0 t
3a78e 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 6d 6f  hen.**        mo
3a78f 76 65 20 74 68 65 20 72 6f 77 32 20 63 75 72 73  ve the row2 curs
3a790 6f 72 20 74 6f 20 61 20 6e 75 6c 6c 20 72 6f 77  or to a null row
3a791 0a 2a 2a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .**        goto 
3a792 73 74 61 72 74 0a 2a 2a 20 20 20 20 20 20 66 69  start.**      fi
3a793 0a 2a 2a 20 20 20 20 65 6e 64 0a 2a 2a 0a 2a 2a  .**    end.**.**
3a794 20 4f 52 44 45 52 20 42 59 20 43 4c 41 55 53 45   ORDER BY CLAUSE
3a795 20 50 52 4f 43 45 53 53 49 4e 47 0a 2a 2a 0a 2a   PROCESSING.**.*
3a796 2a 20 70 4f 72 64 65 72 42 79 20 69 73 20 61 20  * pOrderBy is a 
3a797 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4f  pointer to the O
3a798 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 6f  RDER BY clause o
3a799 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  f a SELECT state
3a79a 6d 65 6e 74 2c 0a 2a 2a 20 69 66 20 74 68 65 72  ment,.** if ther
3a79b 65 20 69 73 20 6f 6e 65 2e 20 20 49 66 20 74 68  e is one.  If th
3a79c 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20  ere is no ORDER 
3a79d 42 59 20 63 6c 61 75 73 65 20 6f 72 20 69 66 20  BY clause or if 
3a79e 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
3a79f 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61  is called from a
3a7a0 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45  n UPDATE or DELE
3a7a1 54 45 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68  TE statement, th
3a7a2 65 6e 20 70 4f 72 64 65 72 42 79 20 69 73 20 4e  en pOrderBy is N
3a7a3 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ULL..**.** If an
3a7a4 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73   index can be us
3a7a5 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e  ed so that the n
3a7a6 61 74 75 72 61 6c 20 6f 75 74 70 75 74 20 6f 72  atural output or
3a7a7 64 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65  der of the table
3a7a8 0a 2a 2a 20 73 63 61 6e 20 69 73 20 63 6f 72 72  .** scan is corr
3a7a9 65 63 74 20 66 6f 72 20 74 68 65 20 4f 52 44 45  ect for the ORDE
3a7aa 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65  R BY clause, the
3a7ab 6e 20 74 68 61 74 20 69 6e 64 65 78 20 69 73 20  n that index is 
3a7ac 75 73 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20  used and.** the 
3a7ad 72 65 74 75 72 6e 65 64 20 57 68 65 72 65 49 6e  returned WhereIn
3a7ae 66 6f 2e 6e 4f 42 53 61 74 20 66 69 65 6c 64 20  fo.nOBSat field 
3a7af 69 73 20 73 65 74 20 74 6f 20 70 4f 72 64 65 72  is set to pOrder
3a7b0 42 79 2d 3e 6e 45 78 70 72 2e 20 20 54 68 69 73  By->nExpr.  This
3a7b1 0a 2a 2a 20 69 73 20 61 6e 20 6f 70 74 69 6d 69  .** is an optimi
3a7b2 7a 61 74 69 6f 6e 20 74 68 61 74 20 70 72 65 76  zation that prev
3a7b3 65 6e 74 73 20 61 6e 20 75 6e 6e 65 63 65 73 73  ents an unnecess
3a7b4 61 72 79 20 73 6f 72 74 20 6f 66 20 74 68 65 20  ary sort of the 
3a7b5 72 65 73 75 6c 74 20 73 65 74 0a 2a 2a 20 69 66  result set.** if
3a7b6 20 61 6e 20 69 6e 64 65 78 20 61 70 70 72 6f 70   an index approp
3a7b7 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 4f 52  riate for the OR
3a7b8 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 6c  DER BY clause al
3a7b9 72 65 61 64 79 20 65 78 69 73 74 73 2e 0a 2a 2a  ready exists..**
3a7ba 0a 2a 2a 20 49 66 20 74 68 65 20 77 68 65 72 65  .** If the where
3a7bb 20 63 6c 61 75 73 65 20 6c 6f 6f 70 73 20 63 61   clause loops ca
3a7bc 6e 6e 6f 74 20 62 65 20 61 72 72 61 6e 67 65 64  nnot be arranged
3a7bd 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20   to provide the 
3a7be 63 6f 72 72 65 63 74 0a 2a 2a 20 6f 75 74 70 75  correct.** outpu
3a7bf 74 20 6f 72 64 65 72 2c 20 74 68 65 6e 20 57 68  t order, then Wh
3a7c0 65 72 65 49 6e 66 6f 2e 6e 4f 42 53 61 74 20 69  ereInfo.nOBSat i
3a7c1 73 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  s 0..*/.SQLITE_P
3a7c2 52 49 56 41 54 45 20 57 68 65 72 65 49 6e 66 6f  RIVATE WhereInfo
3a7c3 20 2a 73 71 6c 69 74 65 33 57 68 65 72 65 42 65   *sqlite3WhereBe
3a7c4 67 69 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  gin(.  Parse *pP
3a7c5 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
3a7c6 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
3a7c7 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  xt */.  SrcList 
3a7c8 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20 2f 2a  *pTabList,    /*
3a7c9 20 41 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 74   A list of all t
3a7ca 61 62 6c 65 73 20 74 6f 20 62 65 20 73 63 61 6e  ables to be scan
3a7cb 6e 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ned */.  Expr *p
3a7cc 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20 2f  Where,         /
3a7cd 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
3a7ce 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  se */.  ExprList
3a7cf 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a   *pOrderBy,   /*
3a7d0 20 41 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   An ORDER BY cla
3a7d1 75 73 65 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  use, or NULL */.
3a7d2 20 20 45 78 70 72 4c 69 73 74 20 2a 70 44 69 73    ExprList *pDis
3a7d3 74 69 6e 63 74 2c 20 20 2f 2a 20 54 68 65 20 73  tinct,  /* The s
3a7d4 65 6c 65 63 74 2d 6c 69 73 74 20 66 6f 72 20 44  elect-list for D
3a7d5 49 53 54 49 4e 43 54 20 71 75 65 72 69 65 73 20  ISTINCT queries 
3a7d6 2d 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 75  - or NULL */.  u
3a7d7 31 36 20 77 63 74 72 6c 46 6c 61 67 73 2c 20 20  16 wctrlFlags,  
3a7d8 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74       /* One of t
3a7d9 68 65 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73  he WHERE_* flags
3a7da 20 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69   defined in sqli
3a7db 74 65 49 6e 74 2e 68 20 2a 2f 0a 20 20 69 6e 74  teInt.h */.  int
3a7dc 20 69 49 64 78 43 75 72 20 20 20 20 20 20 20 20   iIdxCur        
3a7dd 20 20 20 2f 2a 20 49 66 20 57 48 45 52 45 5f 4f     /* If WHERE_O
3a7de 4e 45 54 41 42 4c 45 5f 4f 4e 4c 59 20 69 73 20  NETABLE_ONLY is 
3a7df 73 65 74 2c 20 69 6e 64 65 78 20 63 75 72 73 6f  set, index curso
3a7e0 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 7b 0a 20  r number */.){. 
3a7e1 20 69 6e 74 20 6e 42 79 74 65 57 49 6e 66 6f 3b   int nByteWInfo;
3a7e2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
3a7e3 75 6d 2e 20 62 79 74 65 73 20 61 6c 6c 6f 63 61  um. bytes alloca
3a7e4 74 65 64 20 66 6f 72 20 57 68 65 72 65 49 6e 66  ted for WhereInf
3a7e5 6f 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 69 6e  o struct */.  in
3a7e6 74 20 6e 54 61 62 4c 69 73 74 3b 20 20 20 20 20  t nTabList;     
3a7e7 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
3a7e8 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69  er of elements i
3a7e9 6e 20 70 54 61 62 4c 69 73 74 20 2a 2f 0a 20 20  n pTabList */.  
3a7ea 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66  WhereInfo *pWInf
3a7eb 6f 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69  o;         /* Wi
3a7ec 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 72 65  ll become the re
3a7ed 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68  turn value of th
3a7ee 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  is function */. 
3a7ef 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
3a7f0 65 2d 3e 70 56 64 62 65 3b 20 20 20 2f 2a 20 54  e->pVdbe;   /* T
3a7f1 68 65 20 76 69 72 74 75 61 6c 20 64 61 74 61 62  he virtual datab
3a7f2 61 73 65 20 65 6e 67 69 6e 65 20 2a 2f 0a 20 20  ase engine */.  
3a7f3 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79  Bitmask notReady
3a7f4 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75  ;          /* Cu
3a7f5 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e  rsors that are n
3a7f6 6f 74 20 79 65 74 20 70 6f 73 69 74 69 6f 6e 65  ot yet positione
3a7f7 64 20 2a 2f 0a 20 20 57 68 65 72 65 42 65 73 74  d */.  WhereBest
3a7f8 49 64 78 20 73 57 42 49 3b 20 20 20 20 20 20 20  Idx sWBI;       
3a7f9 20 20 2f 2a 20 42 65 73 74 20 69 6e 64 65 78 20    /* Best index 
3a7fa 73 65 61 72 63 68 20 63 6f 6e 74 65 78 74 20 2a  search context *
3a7fb 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74  /.  WhereMaskSet
3a7fc 20 2a 70 4d 61 73 6b 53 65 74 3b 20 20 20 20 2f   *pMaskSet;    /
3a7fd 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
3a7fe 20 6d 61 73 6b 20 73 65 74 20 2a 2f 0a 20 20 57   mask set */.  W
3a7ff 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65  hereLevel *pLeve
3a800 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73  l;        /* A s
3a801 69 6e 67 6c 65 20 6c 65 76 65 6c 20 69 6e 20 70  ingle level in p
3a802 57 49 6e 66 6f 2d 3e 61 5b 5d 20 2a 2f 0a 20 20  WInfo->a[] */.  
3a803 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20  int iFrom;      
3a804 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
3a805 72 73 74 20 75 6e 75 73 65 64 20 46 52 4f 4d 20  rst unused FROM 
3a806 63 6c 61 75 73 65 20 65 6c 65 6d 65 6e 74 20 2a  clause element *
3a807 2f 0a 20 20 69 6e 74 20 61 6e 64 46 6c 61 67 73  /.  int andFlags
3a808 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
3a809 2a 20 41 4e 44 2d 65 64 20 63 6f 6d 62 69 6e 61  * AND-ed combina
3a80a 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 70 57 43 2d  tion of all pWC-
3a80b 3e 61 5b 5d 2e 77 74 46 6c 61 67 73 20 2a 2f 0a  >a[].wtFlags */.
3a80c 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20    int ii;       
3a80d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a80e 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
3a80f 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
3a810 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a811 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
3a812 69 6f 6e 20 2a 2f 0a 0a 0a 20 20 2f 2a 20 56 61  ion */...  /* Va
3a813 72 69 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a  riable initializ
3a814 61 74 69 6f 6e 20 2a 2f 0a 20 20 6d 65 6d 73 65  ation */.  memse
3a815 74 28 26 73 57 42 49 2c 20 30 2c 20 73 69 7a 65  t(&sWBI, 0, size
3a816 6f 66 28 73 57 42 49 29 29 3b 0a 20 20 73 57 42  of(sWBI));.  sWB
3a817 49 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73  I.pParse = pPars
3a818 65 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6e 75 6d  e;..  /* The num
3a819 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e  ber of tables in
3a81a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
3a81b 20 69 73 20 6c 69 6d 69 74 65 64 20 62 79 20 74   is limited by t
3a81c 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a  he number of.  *
3a81d 2a 20 62 69 74 73 20 69 6e 20 61 20 42 69 74 6d  * bits in a Bitm
3a81e 61 73 6b 20 0a 20 20 2a 2f 0a 20 20 74 65 73 74  ask .  */.  test
3a81f 63 61 73 65 28 20 70 54 61 62 4c 69 73 74 2d 3e  case( pTabList->
3a820 6e 53 72 63 3d 3d 42 4d 53 20 29 3b 0a 20 20 69  nSrc==BMS );.  i
3a821 66 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72  f( pTabList->nSr
3a822 63 3e 42 4d 53 20 29 7b 0a 20 20 20 20 73 71 6c  c>BMS ){.    sql
3a823 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
3a824 72 73 65 2c 20 22 61 74 20 6d 6f 73 74 20 25 64  rse, "at most %d
3a825 20 74 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f 69   tables in a joi
3a826 6e 22 2c 20 42 4d 53 29 3b 0a 20 20 20 20 72 65  n", BMS);.    re
3a827 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 0;.  }..  /
3a828 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
3a829 6e 6f 72 6d 61 6c 6c 79 20 67 65 6e 65 72 61 74  normally generat
3a82a 65 73 20 61 20 6e 65 73 74 65 64 20 6c 6f 6f 70  es a nested loop
3a82b 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20   for all tables 
3a82c 69 6e 20 0a 20 20 2a 2a 20 70 54 61 62 4c 69 73  in .  ** pTabLis
3a82d 74 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 57  t.  But if the W
3a82e 48 45 52 45 5f 4f 4e 45 54 41 42 4c 45 5f 4f 4e  HERE_ONETABLE_ON
3a82f 4c 59 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  LY flag is set, 
3a830 74 68 65 6e 20 77 65 20 73 68 6f 75 6c 64 0a 20  then we should. 
3a831 20 2a 2a 20 6f 6e 6c 79 20 67 65 6e 65 72 61 74   ** only generat
3a832 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 66  e code for the f
3a833 69 72 73 74 20 74 61 62 6c 65 20 69 6e 20 70 54  irst table in pT
3a834 61 62 4c 69 73 74 20 61 6e 64 20 61 73 73 75 6d  abList and assum
3a835 65 20 74 68 61 74 0a 20 20 2a 2a 20 61 6e 79 20  e that.  ** any 
3a836 63 75 72 73 6f 72 73 20 61 73 73 6f 63 69 61 74  cursors associat
3a837 65 64 20 77 69 74 68 20 73 75 62 73 65 71 75 65  ed with subseque
3a838 6e 74 20 74 61 62 6c 65 73 20 61 72 65 20 75 6e  nt tables are un
3a839 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a  initialized..  *
3a83a 2f 0a 20 20 6e 54 61 62 4c 69 73 74 20 3d 20 28  /.  nTabList = (
3a83b 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45  wctrlFlags & WHE
3a83c 52 45 5f 4f 4e 45 54 41 42 4c 45 5f 4f 4e 4c 59  RE_ONETABLE_ONLY
3a83d 29 20 3f 20 31 20 3a 20 70 54 61 62 4c 69 73 74  ) ? 1 : pTabList
3a83e 2d 3e 6e 53 72 63 3b 0a 0a 20 20 2f 2a 20 41 6c  ->nSrc;..  /* Al
3a83f 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69  locate and initi
3a840 61 6c 69 7a 65 20 74 68 65 20 57 68 65 72 65 49  alize the WhereI
3a841 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68  nfo structure th
3a842 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74  at will become t
3a843 68 65 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 76  he.  ** return v
3a844 61 6c 75 65 2e 20 41 20 73 69 6e 67 6c 65 20 61  alue. A single a
3a845 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 75 73 65  llocation is use
3a846 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 57  d to store the W
3a847 68 65 72 65 49 6e 66 6f 0a 20 20 2a 2a 20 73 74  hereInfo.  ** st
3a848 72 75 63 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e  ruct, the conten
3a849 74 73 20 6f 66 20 57 68 65 72 65 49 6e 66 6f 2e  ts of WhereInfo.
3a84a 61 5b 5d 2c 20 74 68 65 20 57 68 65 72 65 43 6c  a[], the WhereCl
3a84b 61 75 73 65 20 73 74 72 75 63 74 75 72 65 0a 20  ause structure. 
3a84c 20 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72   ** and the Wher
3a84d 65 4d 61 73 6b 53 65 74 20 73 74 72 75 63 74 75  eMaskSet structu
3a84e 72 65 2e 20 53 69 6e 63 65 20 57 68 65 72 65 43  re. Since WhereC
3a84f 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e 73 20 61  lause contains a
3a850 6e 20 38 2d 62 79 74 65 0a 20 20 2a 2a 20 66 69  n 8-byte.  ** fi
3a851 65 6c 64 20 28 74 79 70 65 20 42 69 74 6d 61 73  eld (type Bitmas
3a852 6b 29 20 69 74 20 6d 75 73 74 20 62 65 20 61 6c  k) it must be al
3a853 69 67 6e 65 64 20 6f 6e 20 61 6e 20 38 2d 62 79  igned on an 8-by
3a854 74 65 20 62 6f 75 6e 64 61 72 79 20 6f 6e 0a 20  te boundary on. 
3a855 20 2a 2a 20 73 6f 6d 65 20 61 72 63 68 69 74 65   ** some archite
3a856 63 74 75 72 65 73 2e 20 48 65 6e 63 65 20 74 68  ctures. Hence th
3a857 65 20 52 4f 55 4e 44 38 28 29 20 62 65 6c 6f 77  e ROUND8() below
3a858 2e 0a 20 20 2a 2f 0a 20 20 64 62 20 3d 20 70 50  ..  */.  db = pP
3a859 61 72 73 65 2d 3e 64 62 3b 0a 20 20 6e 42 79 74  arse->db;.  nByt
3a85a 65 57 49 6e 66 6f 20 3d 20 52 4f 55 4e 44 38 28  eWInfo = ROUND8(
3a85b 73 69 7a 65 6f 66 28 57 68 65 72 65 49 6e 66 6f  sizeof(WhereInfo
3a85c 29 2b 28 6e 54 61 62 4c 69 73 74 2d 31 29 2a 73  )+(nTabList-1)*s
3a85d 69 7a 65 6f 66 28 57 68 65 72 65 4c 65 76 65 6c  izeof(WhereLevel
3a85e 29 29 3b 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73  ));.  pWInfo = s
3a85f 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
3a860 72 6f 28 64 62 2c 20 0a 20 20 20 20 20 20 6e 42  ro(db, .      nB
3a861 79 74 65 57 49 6e 66 6f 20 2b 20 0a 20 20 20 20  yteWInfo + .    
3a862 20 20 73 69 7a 65 6f 66 28 57 68 65 72 65 43 6c    sizeof(WhereCl
3a863 61 75 73 65 29 20 2b 0a 20 20 20 20 20 20 73 69  ause) +.      si
3a864 7a 65 6f 66 28 57 68 65 72 65 4d 61 73 6b 53 65  zeof(WhereMaskSe
3a865 74 29 0a 20 20 29 3b 0a 20 20 69 66 28 20 64 62  t).  );.  if( db
3a866 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
3a867 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
3a868 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29 3b  ree(db, pWInfo);
3a869 0a 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 30 3b  .    pWInfo = 0;
3a86a 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65 42  .    goto whereB
3a86b 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a 20  eginError;.  }. 
3a86c 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 20   pWInfo->nLevel 
3a86d 3d 20 6e 54 61 62 4c 69 73 74 3b 0a 20 20 70 57  = nTabList;.  pW
3a86e 49 6e 66 6f 2d 3e 70 50 61 72 73 65 20 3d 20 70  Info->pParse = p
3a86f 50 61 72 73 65 3b 0a 20 20 70 57 49 6e 66 6f 2d  Parse;.  pWInfo-
3a870 3e 70 54 61 62 4c 69 73 74 20 3d 20 70 54 61 62  >pTabList = pTab
3a871 4c 69 73 74 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e  List;.  pWInfo->
3a872 69 42 72 65 61 6b 20 3d 20 73 71 6c 69 74 65 33  iBreak = sqlite3
3a873 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
3a874 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 57 43 20  ;.  pWInfo->pWC 
3a875 3d 20 73 57 42 49 2e 70 57 43 20 3d 20 28 57 68  = sWBI.pWC = (Wh
3a876 65 72 65 43 6c 61 75 73 65 20 2a 29 26 28 28 75  ereClause *)&((u
3a877 38 20 2a 29 70 57 49 6e 66 6f 29 5b 6e 42 79 74  8 *)pWInfo)[nByt
3a878 65 57 49 6e 66 6f 5d 3b 0a 20 20 70 57 49 6e 66  eWInfo];.  pWInf
3a879 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20 3d 20  o->wctrlFlags = 
3a87a 77 63 74 72 6c 46 6c 61 67 73 3b 0a 20 20 70 57  wctrlFlags;.  pW
3a87b 49 6e 66 6f 2d 3e 73 61 76 65 64 4e 51 75 65 72  Info->savedNQuer
3a87c 79 4c 6f 6f 70 20 3d 20 70 50 61 72 73 65 2d 3e  yLoop = pParse->
3a87d 6e 51 75 65 72 79 4c 6f 6f 70 3b 0a 20 20 70 4d  nQueryLoop;.  pM
3a87e 61 73 6b 53 65 74 20 3d 20 28 57 68 65 72 65 4d  askSet = (WhereM
3a87f 61 73 6b 53 65 74 2a 29 26 73 57 42 49 2e 70 57  askSet*)&sWBI.pW
3a880 43 5b 31 5d 3b 0a 20 20 73 57 42 49 2e 61 4c 65  C[1];.  sWBI.aLe
3a881 76 65 6c 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 3b  vel = pWInfo->a;
3a882 0a 0a 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 74  ..  /* Disable t
3a883 68 65 20 44 49 53 54 49 4e 43 54 20 6f 70 74 69  he DISTINCT opti
3a884 6d 69 7a 61 74 69 6f 6e 20 69 66 20 53 51 4c 49  mization if SQLI
3a885 54 45 5f 44 69 73 74 69 6e 63 74 4f 70 74 20 69  TE_DistinctOpt i
3a886 73 20 73 65 74 20 76 69 61 0a 20 20 2a 2a 20 73  s set via.  ** s
3a887 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 74 72 6c  qlite3_test_ctrl
3a888 28 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c  (SQLITE_TESTCTRL
3a889 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 53 2c 2e  _OPTIMIZATIONS,.
3a88a 2e 2e 29 20 2a 2f 0a 20 20 69 66 28 20 4f 70 74  ..) */.  if( Opt
3a88b 69 6d 69 7a 61 74 69 6f 6e 44 69 73 61 62 6c 65  imizationDisable
3a88c 64 28 64 62 2c 20 53 51 4c 49 54 45 5f 44 69 73  d(db, SQLITE_Dis
3a88d 74 69 6e 63 74 4f 70 74 29 20 29 20 70 44 69 73  tinctOpt) ) pDis
3a88e 74 69 6e 63 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a  tinct = 0;..  /*
3a88f 20 53 70 6c 69 74 20 74 68 65 20 57 48 45 52 45   Split the WHERE
3a890 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 73 65 70   clause into sep
3a891 61 72 61 74 65 20 73 75 62 65 78 70 72 65 73 73  arate subexpress
3a892 69 6f 6e 73 20 77 68 65 72 65 20 65 61 63 68 0a  ions where each.
3a893 20 20 2a 2a 20 73 75 62 65 78 70 72 65 73 73 69    ** subexpressi
3a894 6f 6e 20 69 73 20 73 65 70 61 72 61 74 65 64 20  on is separated 
3a895 62 79 20 61 6e 20 41 4e 44 20 6f 70 65 72 61 74  by an AND operat
3a896 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 6e 69 74 4d  or..  */.  initM
3a897 61 73 6b 53 65 74 28 70 4d 61 73 6b 53 65 74 29  askSet(pMaskSet)
3a898 3b 0a 20 20 77 68 65 72 65 43 6c 61 75 73 65 49  ;.  whereClauseI
3a899 6e 69 74 28 73 57 42 49 2e 70 57 43 2c 20 70 50  nit(sWBI.pWC, pP
3a89a 61 72 73 65 2c 20 70 4d 61 73 6b 53 65 74 2c 20  arse, pMaskSet, 
3a89b 77 63 74 72 6c 46 6c 61 67 73 29 3b 0a 20 20 73  wctrlFlags);.  s
3a89c 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f  qlite3ExprCodeCo
3a89d 6e 73 74 61 6e 74 73 28 70 50 61 72 73 65 2c 20  nstants(pParse, 
3a89e 70 57 68 65 72 65 29 3b 0a 20 20 77 68 65 72 65  pWhere);.  where
3a89f 53 70 6c 69 74 28 73 57 42 49 2e 70 57 43 2c 20  Split(sWBI.pWC, 
3a8a0 70 57 68 65 72 65 2c 20 54 4b 5f 41 4e 44 29 3b  pWhere, TK_AND);
3a8a1 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 31 35 38     /* IMP: R-158
3a8a2 34 32 2d 35 33 32 39 36 20 2a 2f 0a 20 20 20 20  42-53296 */.    
3a8a3 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61  .  /* Special ca
3a8a4 73 65 3a 20 61 20 57 48 45 52 45 20 63 6c 61 75  se: a WHERE clau
3a8a5 73 65 20 74 68 61 74 20 69 73 20 63 6f 6e 73 74  se that is const
3a8a6 61 6e 74 2e 20 20 45 76 61 6c 75 61 74 65 20 74  ant.  Evaluate t
3a8a7 68 65 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69  he.  ** expressi
3a8a8 6f 6e 20 61 6e 64 20 65 69 74 68 65 72 20 6a 75  on and either ju
3a8a9 6d 70 20 6f 76 65 72 20 61 6c 6c 20 6f 66 20 74  mp over all of t
3a8aa 68 65 20 63 6f 64 65 20 6f 72 20 66 61 6c 6c 20  he code or fall 
3a8ab 74 68 72 75 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  thru..  */.  if(
3a8ac 20 70 57 68 65 72 65 20 26 26 20 28 6e 54 61 62   pWhere && (nTab
3a8ad 4c 69 73 74 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  List==0 || sqlit
3a8ae 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
3a8af 4e 6f 74 4a 6f 69 6e 28 70 57 68 65 72 65 29 29  NotJoin(pWhere))
3a8b0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
3a8b1 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
3a8b2 65 2c 20 70 57 68 65 72 65 2c 20 70 57 49 6e 66  e, pWhere, pWInf
3a8b3 6f 2d 3e 69 42 72 65 61 6b 2c 20 53 51 4c 49 54  o->iBreak, SQLIT
3a8b4 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
3a8b5 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20     pWhere = 0;. 
3a8b6 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20   }..  /* Assign 
3a8b7 61 20 62 69 74 20 66 72 6f 6d 20 74 68 65 20 62  a bit from the b
3a8b8 69 74 6d 61 73 6b 20 74 6f 20 65 76 65 72 79 20  itmask to every 
3a8b9 74 65 72 6d 20 69 6e 20 74 68 65 20 46 52 4f 4d  term in the FROM
3a8ba 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a 20 20   clause..  **.  
3a8bb 2a 2a 20 57 68 65 6e 20 61 73 73 69 67 6e 69 6e  ** When assignin
3a8bc 67 20 62 69 74 6d 61 73 6b 20 76 61 6c 75 65 73  g bitmask values
3a8bd 20 74 6f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20   to FROM clause 
3a8be 63 75 72 73 6f 72 73 2c 20 69 74 20 6d 75 73 74  cursors, it must
3a8bf 20 62 65 0a 20 20 2a 2a 20 74 68 65 20 63 61 73   be.  ** the cas
3a8c0 65 20 74 68 61 74 20 69 66 20 58 20 69 73 20 74  e that if X is t
3a8c1 68 65 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 74  he bitmask for t
3a8c2 68 65 20 4e 2d 74 68 20 46 52 4f 4d 20 63 6c 61  he N-th FROM cla
3a8c3 75 73 65 20 74 65 72 6d 20 74 68 65 6e 0a 20 20  use term then.  
3a8c4 2a 2a 20 74 68 65 20 62 69 74 6d 61 73 6b 20 66  ** the bitmask f
3a8c5 6f 72 20 61 6c 6c 20 46 52 4f 4d 20 63 6c 61 75  or all FROM clau
3a8c6 73 65 20 74 65 72 6d 73 20 74 6f 20 74 68 65 20  se terms to the 
3a8c7 6c 65 66 74 20 6f 66 20 74 68 65 20 4e 2d 74 68  left of the N-th
3a8c8 20 74 65 72 6d 0a 20 20 2a 2a 20 69 73 20 28 58   term.  ** is (X
3a8c9 2d 31 29 2e 20 20 20 41 6e 20 65 78 70 72 65 73  -1).   An expres
3a8ca 73 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 4f 4e  sion from the ON
3a8cb 20 63 6c 61 75 73 65 20 6f 66 20 61 20 4c 45 46   clause of a LEF
3a8cc 54 20 4a 4f 49 4e 20 63 61 6e 20 75 73 65 0a 20  T JOIN can use. 
3a8cd 20 2a 2a 20 69 74 73 20 45 78 70 72 2e 69 52 69   ** its Expr.iRi
3a8ce 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 76 61 6c  ghtJoinTable val
3a8cf 75 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20 62  ue to find the b
3a8d0 69 74 6d 61 73 6b 20 6f 66 20 74 68 65 20 72 69  itmask of the ri
3a8d1 67 68 74 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f  ght table.  ** o
3a8d2 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 53 75 62  f the join.  Sub
3a8d3 74 72 61 63 74 69 6e 67 20 6f 6e 65 20 66 72 6f  tracting one fro
3a8d4 6d 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c  m the right tabl
3a8d5 65 20 62 69 74 6d 61 73 6b 20 67 69 76 65 73 20  e bitmask gives 
3a8d6 61 0a 20 20 2a 2a 20 62 69 74 6d 61 73 6b 20 66  a.  ** bitmask f
3a8d7 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f  or all tables to
3a8d8 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65   the left of the
3a8d9 20 6a 6f 69 6e 2e 20 20 4b 6e 6f 77 69 6e 67 20   join.  Knowing 
3a8da 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a  the bitmask.  **
3a8db 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20   for all tables 
3a8dc 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 61  to the left of a
3a8dd 20 6c 65 66 74 20 6a 6f 69 6e 20 69 73 20 69 6d   left join is im
3a8de 70 6f 72 74 61 6e 74 2e 20 20 54 69 63 6b 65 74  portant.  Ticket
3a8df 20 23 33 30 31 35 2e 0a 20 20 2a 2a 0a 20 20 2a   #3015..  **.  *
3a8e0 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20  * Configure the 
3a8e1 57 68 65 72 65 43 6c 61 75 73 65 2e 76 6d 61 73  WhereClause.vmas
3a8e2 6b 20 76 61 72 69 61 62 6c 65 20 73 6f 20 74 68  k variable so th
3a8e3 61 74 20 62 69 74 73 20 74 68 61 74 20 63 6f 72  at bits that cor
3a8e4 72 65 73 70 6f 6e 64 0a 20 20 2a 2a 20 74 6f 20  respond.  ** to 
3a8e5 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75  virtual table cu
3a8e6 72 73 6f 72 73 20 61 72 65 20 73 65 74 2e 20 54  rsors are set. T
3a8e7 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 73  his is used to s
3a8e8 65 6c 65 63 74 69 76 65 6c 79 20 64 69 73 61 62  electively disab
3a8e9 6c 65 20 0a 20 20 2a 2a 20 74 68 65 20 4f 52 2d  le .  ** the OR-
3a8ea 74 6f 2d 49 4e 20 74 72 61 6e 73 66 6f 72 6d 61  to-IN transforma
3a8eb 74 69 6f 6e 20 69 6e 20 65 78 70 72 41 6e 61 6c  tion in exprAnal
3a8ec 79 7a 65 4f 72 54 65 72 6d 28 29 2e 20 49 74 20  yzeOrTerm(). It 
3a8ed 69 73 20 6e 6f 74 20 68 65 6c 70 66 75 6c 20 0a  is not helpful .
3a8ee 20 20 2a 2a 20 77 69 74 68 20 76 69 72 74 75 61    ** with virtua
3a8ef 6c 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2a 0a 20  l tables..  **. 
3a8f0 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 62 69   ** Note that bi
3a8f1 74 6d 61 73 6b 73 20 61 72 65 20 63 72 65 61 74  tmasks are creat
3a8f2 65 64 20 66 6f 72 20 61 6c 6c 20 70 54 61 62 4c  ed for all pTabL
3a8f3 69 73 74 2d 3e 6e 53 72 63 20 74 61 62 6c 65 73  ist->nSrc tables
3a8f4 20 69 6e 0a 20 20 2a 2a 20 70 54 61 62 4c 69 73   in.  ** pTabLis
3a8f5 74 2c 20 6e 6f 74 20 6a 75 73 74 20 74 68 65 20  t, not just the 
3a8f6 66 69 72 73 74 20 6e 54 61 62 4c 69 73 74 20 74  first nTabList t
3a8f7 61 62 6c 65 73 2e 20 20 6e 54 61 62 4c 69 73 74  ables.  nTabList
3a8f8 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a 20 20 2a   is normally.  *
3a8f9 2a 20 65 71 75 61 6c 20 74 6f 20 70 54 61 62 4c  * equal to pTabL
3a8fa 69 73 74 2d 3e 6e 53 72 63 20 62 75 74 20 6d 69  ist->nSrc but mi
3a8fb 67 68 74 20 62 65 20 73 68 6f 72 74 65 6e 65 64  ght be shortened
3a8fc 20 74 6f 20 31 20 69 66 20 74 68 65 0a 20 20 2a   to 1 if the.  *
3a8fd 2a 20 57 48 45 52 45 5f 4f 4e 45 54 41 42 4c 45  * WHERE_ONETABLE
3a8fe 5f 4f 4e 4c 59 20 66 6c 61 67 20 69 73 20 73 65  _ONLY flag is se
3a8ff 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  t..  */.  assert
3a900 28 20 73 57 42 49 2e 70 57 43 2d 3e 76 6d 61 73  ( sWBI.pWC->vmas
3a901 6b 3d 3d 30 20 26 26 20 70 4d 61 73 6b 53 65 74  k==0 && pMaskSet
3a902 2d 3e 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28  ->n==0 );.  for(
3a903 69 69 3d 30 3b 20 69 69 3c 70 54 61 62 4c 69 73  ii=0; ii<pTabLis
3a904 74 2d 3e 6e 53 72 63 3b 20 69 69 2b 2b 29 7b 0a  t->nSrc; ii++){.
3a905 20 20 20 20 63 72 65 61 74 65 4d 61 73 6b 28 70      createMask(p
3a906 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73  MaskSet, pTabLis
3a907 74 2d 3e 61 5b 69 69 5d 2e 69 43 75 72 73 6f 72  t->a[ii].iCursor
3a908 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
3a909 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
3a90a 42 4c 45 0a 20 20 20 20 69 66 28 20 41 4c 57 41  BLE.    if( ALWA
3a90b 59 53 28 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69  YS(pTabList->a[i
3a90c 69 5d 2e 70 54 61 62 29 20 26 26 20 49 73 56 69  i].pTab) && IsVi
3a90d 72 74 75 61 6c 28 70 54 61 62 4c 69 73 74 2d 3e  rtual(pTabList->
3a90e 61 5b 69 69 5d 2e 70 54 61 62 29 20 29 7b 0a 20  a[ii].pTab) ){. 
3a90f 20 20 20 20 20 73 57 42 49 2e 70 57 43 2d 3e 76       sWBI.pWC->v
3a910 6d 61 73 6b 20 7c 3d 20 28 28 42 69 74 6d 61 73  mask |= ((Bitmas
3a911 6b 29 31 20 3c 3c 20 69 69 29 3b 0a 20 20 20 20  k)1 << ii);.    
3a912 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 69 66  }.#endif.  }.#if
3a913 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a  ndef NDEBUG.  {.
3a914 20 20 20 20 42 69 74 6d 61 73 6b 20 74 6f 54 68      Bitmask toTh
3a915 65 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 66  eLeft = 0;.    f
3a916 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 54 61 62  or(ii=0; ii<pTab
3a917 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 69 2b 2b  List->nSrc; ii++
3a918 29 7b 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b  ){.      Bitmask
3a919 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61   m = getMask(pMa
3a91a 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d  skSet, pTabList-
3a91b 3e 61 5b 69 69 5d 2e 69 43 75 72 73 6f 72 29 3b  >a[ii].iCursor);
3a91c 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
3a91d 6d 2d 31 29 3d 3d 74 6f 54 68 65 4c 65 66 74 20  m-1)==toTheLeft 
3a91e 29 3b 0a 20 20 20 20 20 20 74 6f 54 68 65 4c 65  );.      toTheLe
3a91f 66 74 20 7c 3d 20 6d 3b 0a 20 20 20 20 7d 0a 20  ft |= m;.    }. 
3a920 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
3a921 41 6e 61 6c 79 7a 65 20 61 6c 6c 20 6f 66 20 74  Analyze all of t
3a922 68 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  he subexpression
3a923 73 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 65 78  s.  Note that ex
3a924 70 72 41 6e 61 6c 79 7a 65 28 29 20 6d 69 67 68  prAnalyze() migh
3a925 74 0a 20 20 2a 2a 20 61 64 64 20 6e 65 77 20 76  t.  ** add new v
3a926 69 72 74 75 61 6c 20 74 65 72 6d 73 20 6f 6e 74  irtual terms ont
3a927 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
3a928 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20   WHERE clause.  
3a929 57 65 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 77  We do not.  ** w
3a92a 61 6e 74 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74  ant to analyze t
3a92b 68 65 73 65 20 76 69 72 74 75 61 6c 20 74 65 72  hese virtual ter
3a92c 6d 73 2c 20 73 6f 20 73 74 61 72 74 20 61 6e 61  ms, so start ana
3a92d 6c 79 7a 69 6e 67 20 61 74 20 74 68 65 20 65 6e  lyzing at the en
3a92e 64 0a 20 20 2a 2a 20 61 6e 64 20 77 6f 72 6b 20  d.  ** and work 
3a92f 66 6f 72 77 61 72 64 20 73 6f 20 74 68 61 74 20  forward so that 
3a930 74 68 65 20 61 64 64 65 64 20 76 69 72 74 75 61  the added virtua
3a931 6c 20 74 65 72 6d 73 20 61 72 65 20 6e 65 76 65  l terms are neve
3a932 72 20 70 72 6f 63 65 73 73 65 64 2e 0a 20 20 2a  r processed..  *
3a933 2f 0a 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41  /.  exprAnalyzeA
3a934 6c 6c 28 70 54 61 62 4c 69 73 74 2c 20 73 57 42  ll(pTabList, sWB
3a935 49 2e 70 57 43 29 3b 0a 20 20 69 66 28 20 64 62  I.pWC);.  if( db
3a936 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
3a937 7b 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65  {.    goto where
3a938 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a  BeginError;.  }.
3a939 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74  .  /* Check if t
3a93a 68 65 20 44 49 53 54 49 4e 43 54 20 71 75 61 6c  he DISTINCT qual
3a93b 69 66 69 65 72 2c 20 69 66 20 74 68 65 72 65 20  ifier, if there 
3a93c 69 73 20 6f 6e 65 2c 20 69 73 20 72 65 64 75 6e  is one, is redun
3a93d 64 61 6e 74 2e 20 0a 20 20 2a 2a 20 49 66 20 69  dant. .  ** If i
3a93e 74 20 69 73 2c 20 74 68 65 6e 20 73 65 74 20 70  t is, then set p
3a93f 44 69 73 74 69 6e 63 74 20 74 6f 20 4e 55 4c 4c  Distinct to NULL
3a940 20 61 6e 64 20 57 68 65 72 65 49 6e 66 6f 2e 65   and WhereInfo.e
3a941 44 69 73 74 69 6e 63 74 20 74 6f 0a 20 20 2a 2a  Distinct to.  **
3a942 20 57 48 45 52 45 5f 44 49 53 54 49 4e 43 54 5f   WHERE_DISTINCT_
3a943 55 4e 49 51 55 45 20 74 6f 20 74 65 6c 6c 20 74  UNIQUE to tell t
3a944 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 69 67 6e  he caller to ign
3a945 6f 72 65 20 74 68 65 20 44 49 53 54 49 4e 43 54  ore the DISTINCT
3a946 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 69  ..  */.  if( pDi
3a947 73 74 69 6e 63 74 20 26 26 20 69 73 44 69 73 74  stinct && isDist
3a948 69 6e 63 74 52 65 64 75 6e 64 61 6e 74 28 70 50  inctRedundant(pP
3a949 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
3a94a 73 57 42 49 2e 70 57 43 2c 20 70 44 69 73 74 69  sWBI.pWC, pDisti
3a94b 6e 63 74 29 20 29 7b 0a 20 20 20 20 70 44 69 73  nct) ){.    pDis
3a94c 74 69 6e 63 74 20 3d 20 30 3b 0a 20 20 20 20 70  tinct = 0;.    p
3a94d 57 49 6e 66 6f 2d 3e 65 44 69 73 74 69 6e 63 74  WInfo->eDistinct
3a94e 20 3d 20 57 48 45 52 45 5f 44 49 53 54 49 4e 43   = WHERE_DISTINC
3a94f 54 5f 55 4e 49 51 55 45 3b 0a 20 20 7d 0a 0a 20  T_UNIQUE;.  }.. 
3a950 20 2f 2a 20 43 68 6f 73 65 20 74 68 65 20 62 65   /* Chose the be
3a951 73 74 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20  st index to use 
3a952 66 6f 72 20 65 61 63 68 20 74 61 62 6c 65 20 69  for each table i
3a953 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
3a954 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  e..  **.  ** Thi
3a955 73 20 6c 6f 6f 70 20 66 69 6c 6c 73 20 69 6e 20  s loop fills in 
3a956 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69  the following fi
3a957 65 6c 64 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  elds:.  **.  ** 
3a958 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 70 49    pWInfo->a[].pI
3a959 64 78 20 20 20 20 20 20 54 68 65 20 69 6e 64 65  dx      The inde
3a95a 78 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69  x to use for thi
3a95b 73 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 6c  s level of the l
3a95c 6f 6f 70 2e 0a 20 20 2a 2a 20 20 20 70 57 49 6e  oop..  **   pWIn
3a95d 66 6f 2d 3e 61 5b 5d 2e 77 73 46 6c 61 67 73 20  fo->a[].wsFlags 
3a95e 20 20 57 48 45 52 45 5f 78 78 78 20 66 6c 61 67    WHERE_xxx flag
3a95f 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
3a960 68 20 70 49 64 78 0a 20 20 2a 2a 20 20 20 70 57  h pIdx.  **   pW
3a961 49 6e 66 6f 2d 3e 61 5b 5d 2e 6e 45 71 20 20 20  Info->a[].nEq   
3a962 20 20 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f      The number o
3a963 66 20 3d 3d 20 61 6e 64 20 49 4e 20 63 6f 6e 73  f == and IN cons
3a964 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20 20 20 70  traints.  **   p
3a965 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 69 46 72 6f 6d  WInfo->a[].iFrom
3a966 20 20 20 20 20 57 68 69 63 68 20 74 65 72 6d 20       Which term 
3a967 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
3a968 73 65 20 69 73 20 62 65 69 6e 67 20 63 6f 64 65  se is being code
3a969 64 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  d.  **   pWInfo-
3a96a 3e 61 5b 5d 2e 69 54 61 62 43 75 72 20 20 20 54  >a[].iTabCur   T
3a96b 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 66  he VDBE cursor f
3a96c 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  or the database 
3a96d 74 61 62 6c 65 0a 20 20 2a 2a 20 20 20 70 57 49  table.  **   pWI
3a96e 6e 66 6f 2d 3e 61 5b 5d 2e 69 49 64 78 43 75 72  nfo->a[].iIdxCur
3a96f 20 20 20 54 68 65 20 56 44 42 45 20 63 75 72 73     The VDBE curs
3a970 6f 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78  or for the index
3a971 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e  .  **   pWInfo->
3a972 61 5b 5d 2e 70 54 65 72 6d 20 20 20 20 20 57 68  a[].pTerm     Wh
3a973 65 6e 20 77 73 46 6c 61 67 73 3d 3d 57 4f 5f 4f  en wsFlags==WO_O
3a974 52 2c 20 74 68 65 20 4f 52 2d 63 6c 61 75 73 65  R, the OR-clause
3a975 20 74 65 72 6d 0a 20 20 2a 2a 0a 20 20 2a 2a 20   term.  **.  ** 
3a976 54 68 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20 66  This loop also f
3a977 69 67 75 72 65 73 20 6f 75 74 20 74 68 65 20 6e  igures out the n
3a978 65 73 74 69 6e 67 20 6f 72 64 65 72 20 6f 66 20  esting order of 
3a979 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46 52  tables in the FR
3a97a 4f 4d 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2e 0a  OM.  ** clause..
3a97b 20 20 2a 2f 0a 20 20 73 57 42 49 2e 6e 6f 74 56    */.  sWBI.notV
3a97c 61 6c 69 64 20 3d 20 7e 28 42 69 74 6d 61 73 6b  alid = ~(Bitmask
3a97d 29 30 3b 0a 20 20 73 57 42 49 2e 70 4f 72 64 65  )0;.  sWBI.pOrde
3a97e 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a  rBy = pOrderBy;.
3a97f 20 20 73 57 42 49 2e 6e 20 3d 20 6e 54 61 62 4c    sWBI.n = nTabL
3a980 69 73 74 3b 0a 20 20 73 57 42 49 2e 70 44 69 73  ist;.  sWBI.pDis
3a981 74 69 6e 63 74 20 3d 20 70 44 69 73 74 69 6e 63  tinct = pDistinc
3a982 74 3b 0a 20 20 61 6e 64 46 6c 61 67 73 20 3d 20  t;.  andFlags = 
3a983 7e 30 3b 0a 20 20 57 48 45 52 45 54 52 41 43 45  ~0;.  WHERETRACE
3a984 28 28 22 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65 72  (("*** Optimizer
3a985 20 53 74 61 72 74 20 2a 2a 2a 5c 6e 22 29 29 3b   Start ***\n"));
3a986 0a 20 20 66 6f 72 28 73 57 42 49 2e 69 3d 69 46  .  for(sWBI.i=iF
3a987 72 6f 6d 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57  rom=0, pLevel=pW
3a988 49 6e 66 6f 2d 3e 61 3b 20 73 57 42 49 2e 69 3c  Info->a; sWBI.i<
3a989 6e 54 61 62 4c 69 73 74 3b 20 73 57 42 49 2e 69  nTabList; sWBI.i
3a98a 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20  ++, pLevel++){. 
3a98b 20 20 20 57 68 65 72 65 43 6f 73 74 20 62 65 73     WhereCost bes
3a98c 74 50 6c 61 6e 3b 20 20 20 20 20 20 20 20 20 2f  tPlan;         /
3a98d 2a 20 4d 6f 73 74 20 65 66 66 69 63 69 65 6e 74  * Most efficient
3a98e 20 70 6c 61 6e 20 73 65 65 6e 20 73 6f 20 66 61   plan seen so fa
3a98f 72 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a  r */.    Index *
3a990 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20  pIdx;           
3a991 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 66 6f       /* Index fo
3a992 72 20 46 52 4f 4d 20 74 61 62 6c 65 20 61 74 20  r FROM table at 
3a993 70 54 61 62 49 74 65 6d 20 2a 2f 0a 20 20 20 20  pTabItem */.    
3a994 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20  int j;          
3a995 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
3a996 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
3a997 46 52 4f 4d 20 74 61 62 6c 65 73 20 2a 2f 0a 20  FROM tables */. 
3a998 20 20 20 69 6e 74 20 62 65 73 74 4a 20 3d 20 2d     int bestJ = -
3a999 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  1;             /
3a99a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 6a  * The value of j
3a99b 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b 20   */.    Bitmask 
3a99c 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
3a99d 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 76      /* Bitmask v
3a99e 61 6c 75 65 20 66 6f 72 20 6a 20 6f 72 20 62 65  alue for j or be
3a99f 73 74 4a 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  stJ */.    int i
3a9a0 73 4f 70 74 69 6d 61 6c 3b 20 20 20 20 20 20 20  sOptimal;       
3a9a1 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
3a9a2 6f 72 20 66 6f 72 20 6f 70 74 69 6d 61 6c 2f 6e  or for optimal/n
3a9a3 6f 6e 2d 6f 70 74 69 6d 61 6c 20 73 65 61 72 63  on-optimal searc
3a9a4 68 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 55 6e  h */.    int nUn
3a9a5 63 6f 6e 73 74 72 61 69 6e 65 64 3b 20 20 20 20  constrained;    
3a9a6 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 74       /* Number t
3a9a7 61 62 6c 65 73 20 77 69 74 68 6f 75 74 20 49 4e  ables without IN
3a9a8 44 45 58 45 44 20 42 59 20 2a 2f 0a 20 20 20 20  DEXED BY */.    
3a9a9 42 69 74 6d 61 73 6b 20 6e 6f 74 49 6e 64 65 78  Bitmask notIndex
3a9aa 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ed;         /* M
3a9ab 61 73 6b 20 6f 66 20 74 61 62 6c 65 73 20 74 68  ask of tables th
3a9ac 61 74 20 63 61 6e 6e 6f 74 20 75 73 65 20 61 6e  at cannot use an
3a9ad 20 69 6e 64 65 78 20 2a 2f 0a 0a 20 20 20 20 6d   index */..    m
3a9ae 65 6d 73 65 74 28 26 62 65 73 74 50 6c 61 6e 2c  emset(&bestPlan,
3a9af 20 30 2c 20 73 69 7a 65 6f 66 28 62 65 73 74 50   0, sizeof(bestP
3a9b0 6c 61 6e 29 29 3b 0a 20 20 20 20 62 65 73 74 50  lan));.    bestP
3a9b1 6c 61 6e 2e 72 43 6f 73 74 20 3d 20 53 51 4c 49  lan.rCost = SQLI
3a9b2 54 45 5f 42 49 47 5f 44 42 4c 3b 0a 20 20 20 20  TE_BIG_DBL;.    
3a9b3 57 48 45 52 45 54 52 41 43 45 28 28 22 2a 2a 2a  WHERETRACE(("***
3a9b4 20 42 65 67 69 6e 20 73 65 61 72 63 68 20 66 6f   Begin search fo
3a9b5 72 20 6c 6f 6f 70 20 25 64 20 2a 2a 2a 5c 6e 22  r loop %d ***\n"
3a9b6 2c 20 73 57 42 49 2e 69 29 29 3b 0a 0a 20 20 20  , sWBI.i));..   
3a9b7 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
3a9b8 20 74 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 65   the remaining e
3a9b9 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 46 52  ntries in the FR
3a9ba 4f 4d 20 63 6c 61 75 73 65 20 74 6f 20 66 69 6e  OM clause to fin
3a9bb 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 65 78  d the.    ** nex
3a9bc 74 20 6e 65 73 74 65 64 20 6c 6f 6f 70 2e 20 54  t nested loop. T
3a9bd 68 65 20 6c 6f 6f 70 20 74 65 73 74 73 20 61 6c  he loop tests al
3a9be 6c 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e  l FROM clause en
3a9bf 74 72 69 65 73 0a 20 20 20 20 2a 2a 20 65 69 74  tries.    ** eit
3a9c0 68 65 72 20 6f 6e 63 65 20 6f 72 20 74 77 69 63  her once or twic
3a9c1 65 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  e. .    **.    *
3a9c2 2a 20 54 68 65 20 66 69 72 73 74 20 74 65 73 74  * The first test
3a9c3 20 69 73 20 61 6c 77 61 79 73 20 70 65 72 66 6f   is always perfo
3a9c4 72 6d 65 64 20 69 66 20 74 68 65 72 65 20 61 72  rmed if there ar
3a9c5 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 65 6e  e two or more en
3a9c6 74 72 69 65 73 0a 20 20 20 20 2a 2a 20 72 65 6d  tries.    ** rem
3a9c7 61 69 6e 69 6e 67 20 61 6e 64 20 6e 65 76 65 72  aining and never
3a9c8 20 70 65 72 66 6f 72 6d 65 64 20 69 66 20 74 68   performed if th
3a9c9 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20  ere is only one 
3a9ca 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74 72  FROM clause entr
3a9cb 79 0a 20 20 20 20 2a 2a 20 74 6f 20 63 68 6f 6f  y.    ** to choo
3a9cc 73 65 20 66 72 6f 6d 2e 20 20 54 68 65 20 66 69  se from.  The fi
3a9cd 72 73 74 20 74 65 73 74 20 6c 6f 6f 6b 73 20 66  rst test looks f
3a9ce 6f 72 20 61 6e 20 22 6f 70 74 69 6d 61 6c 22 20  or an "optimal" 
3a9cf 73 63 61 6e 2e 20 20 49 6e 0a 20 20 20 20 2a 2a  scan.  In.    **
3a9d0 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 61 6e   this context an
3a9d1 20 6f 70 74 69 6d 61 6c 20 73 63 61 6e 20 69 73   optimal scan is
3a9d2 20 6f 6e 65 20 74 68 61 74 20 75 73 65 73 20 74   one that uses t
3a9d3 68 65 20 73 61 6d 65 20 73 74 72 61 74 65 67 79  he same strategy
3a9d4 0a 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20  .    ** for the 
3a9d5 67 69 76 65 6e 20 46 52 4f 4d 20 63 6c 61 75 73  given FROM claus
3a9d6 65 20 65 6e 74 72 79 20 61 73 20 77 6f 75 6c 64  e entry as would
3a9d7 20 62 65 20 73 65 6c 65 63 74 65 64 20 69 66 20   be selected if 
3a9d8 74 68 65 20 65 6e 74 72 79 0a 20 20 20 20 2a 2a  the entry.    **
3a9d9 20 77 65 72 65 20 75 73 65 64 20 61 73 20 74 68   were used as th
3a9da 65 20 69 6e 6e 65 72 6d 6f 73 74 20 6e 65 73 74  e innermost nest
3a9db 65 64 20 6c 6f 6f 70 2e 20 20 49 6e 20 6f 74 68  ed loop.  In oth
3a9dc 65 72 20 77 6f 72 64 73 2c 20 61 20 74 61 62 6c  er words, a tabl
3a9dd 65 0a 20 20 20 20 2a 2a 20 69 73 20 63 68 6f 73  e.    ** is chos
3a9de 65 6e 20 73 75 63 68 20 74 68 61 74 20 74 68 65  en such that the
3a9df 20 63 6f 73 74 20 6f 66 20 72 75 6e 6e 69 6e 67   cost of running
3a9e0 20 74 68 61 74 20 74 61 62 6c 65 20 63 61 6e 6e   that table cann
3a9e1 6f 74 20 62 65 20 72 65 64 75 63 65 64 0a 20 20  ot be reduced.  
3a9e2 20 20 2a 2a 20 62 79 20 77 61 69 74 69 6e 67 20    ** by waiting 
3a9e3 66 6f 72 20 6f 74 68 65 72 20 74 61 62 6c 65 73  for other tables
3a9e4 20 74 6f 20 72 75 6e 20 66 69 72 73 74 2e 20 20   to run first.  
3a9e5 54 68 69 73 20 22 6f 70 74 69 6d 61 6c 22 20 74  This "optimal" t
3a9e6 65 73 74 20 77 6f 72 6b 73 0a 20 20 20 20 2a 2a  est works.    **
3a9e7 20 62 79 20 66 69 72 73 74 20 61 73 73 75 6d 69   by first assumi
3a9e8 6e 67 20 74 68 61 74 20 74 68 65 20 46 52 4f 4d  ng that the FROM
3a9e9 20 63 6c 61 75 73 65 20 69 73 20 6f 6e 20 74 68   clause is on th
3a9ea 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 20 61 6e 64  e inner loop and
3a9eb 20 66 69 6e 64 69 6e 67 0a 20 20 20 20 2a 2a 20   finding.    ** 
3a9ec 69 74 73 20 71 75 65 72 79 20 70 6c 61 6e 2c 20  its query plan, 
3a9ed 74 68 65 6e 20 63 68 65 63 6b 69 6e 67 20 74 6f  then checking to
3a9ee 20 73 65 65 20 69 66 20 74 68 61 74 20 71 75 65   see if that que
3a9ef 72 79 20 70 6c 61 6e 20 75 73 65 73 20 61 6e 79  ry plan uses any
3a9f0 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 46 52  .    ** other FR
3a9f1 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20  OM clause terms 
3a9f2 74 68 61 74 20 61 72 65 20 73 57 42 49 2e 6e 6f  that are sWBI.no
3a9f3 74 56 61 6c 69 64 2e 20 20 49 66 20 6e 6f 20 6e  tValid.  If no n
3a9f4 6f 74 56 61 6c 69 64 20 74 65 72 6d 73 0a 20 20  otValid terms.  
3a9f5 20 20 2a 2a 20 61 72 65 20 75 73 65 64 20 74 68    ** are used th
3a9f6 65 6e 20 74 68 65 20 22 6f 70 74 69 6d 61 6c 22  en the "optimal"
3a9f7 20 71 75 65 72 79 20 70 6c 61 6e 20 77 6f 72 6b   query plan work
3a9f8 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  s..    **.    **
3a9f9 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 57   Note that the W
3a9fa 68 65 72 65 43 6f 73 74 2e 6e 52 6f 77 20 70 61  hereCost.nRow pa
3a9fb 72 61 6d 65 74 65 72 20 66 6f 72 20 61 6e 20 6f  rameter for an o
3a9fc 70 74 69 6d 61 6c 20 73 63 61 6e 20 6d 69 67 68  ptimal scan migh
3a9fd 74 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62 65 20  t.    ** not be 
3a9fe 61 73 20 73 6d 61 6c 6c 20 61 73 20 69 74 20 77  as small as it w
3a9ff 6f 75 6c 64 20 62 65 20 69 66 20 74 68 65 20 74  ould be if the t
3aa00 61 62 6c 65 20 72 65 61 6c 6c 79 20 77 65 72 65  able really were
3aa01 20 74 68 65 20 69 6e 6e 65 72 6d 6f 73 74 0a 20   the innermost. 
3aa02 20 20 20 2a 2a 20 6a 6f 69 6e 2e 20 20 54 68 65     ** join.  The
3aa03 20 6e 52 6f 77 20 76 61 6c 75 65 20 63 61 6e 20   nRow value can 
3aa04 62 65 20 72 65 64 75 63 65 64 20 62 79 20 57 48  be reduced by WH
3aa05 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e 73 74  ERE clause const
3aa06 72 61 69 6e 74 73 0a 20 20 20 20 2a 2a 20 74 68  raints.    ** th
3aa07 61 74 20 64 6f 20 6e 6f 74 20 75 73 65 20 69 6e  at do not use in
3aa08 64 69 63 65 73 2e 20 20 42 75 74 20 74 68 69 73  dices.  But this
3aa09 20 6e 52 6f 77 20 72 65 64 75 63 74 69 6f 6e 20   nRow reduction 
3aa0a 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 66 20  only happens if 
3aa0b 74 68 65 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65  the.    ** table
3aa0c 20 72 65 61 6c 6c 79 20 69 73 20 74 68 65 20 69   really is the i
3aa0d 6e 6e 65 72 6d 6f 73 74 20 6a 6f 69 6e 2e 20 20  nnermost join.  
3aa0e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
3aa0f 68 65 20 73 65 63 6f 6e 64 20 6c 6f 6f 70 20 69  he second loop i
3aa10 74 65 72 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  teration is only
3aa11 20 70 65 72 66 6f 72 6d 65 64 20 69 66 20 6e 6f   performed if no
3aa12 20 6f 70 74 69 6d 61 6c 20 73 63 61 6e 0a 20 20   optimal scan.  
3aa13 20 20 2a 2a 20 73 74 72 61 74 65 67 69 65 73 20    ** strategies 
3aa14 77 65 72 65 20 66 6f 75 6e 64 20 62 79 20 74 68  were found by th
3aa15 65 20 66 69 72 73 74 20 69 74 65 72 61 74 69 6f  e first iteratio
3aa16 6e 2e 20 54 68 69 73 20 73 65 63 6f 6e 64 20 69  n. This second i
3aa17 74 65 72 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20  teration.    ** 
3aa18 69 73 20 75 73 65 64 20 74 6f 20 73 65 61 72 63  is used to searc
3aa19 68 20 66 6f 72 20 74 68 65 20 6c 6f 77 65 73 74  h for the lowest
3aa1a 20 63 6f 73 74 20 73 63 61 6e 20 6f 76 65 72 61   cost scan overa
3aa1b 6c 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ll..    **.    *
3aa1c 2a 20 50 72 65 76 69 6f 75 73 20 76 65 72 73 69  * Previous versi
3aa1d 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 70 65  ons of SQLite pe
3aa1e 72 66 6f 72 6d 65 64 20 6f 6e 6c 79 20 74 68 65  rformed only the
3aa1f 20 73 65 63 6f 6e 64 20 69 74 65 72 61 74 69 6f   second iteratio
3aa20 6e 20 2d 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e  n -.    ** the n
3aa21 65 78 74 20 6f 75 74 65 72 6d 6f 73 74 20 6c 6f  ext outermost lo
3aa22 6f 70 20 77 61 73 20 61 6c 77 61 79 73 20 74 68  op was always th
3aa23 61 74 20 77 69 74 68 20 74 68 65 20 6c 6f 77 65  at with the lowe
3aa24 73 74 20 6f 76 65 72 61 6c 6c 0a 20 20 20 20 2a  st overall.    *
3aa25 2a 20 63 6f 73 74 2e 20 48 6f 77 65 76 65 72 2c  * cost. However,
3aa26 20 74 68 69 73 20 6d 65 61 6e 74 20 74 68 61 74   this meant that
3aa27 20 53 51 4c 69 74 65 20 63 6f 75 6c 64 20 73 65   SQLite could se
3aa28 6c 65 63 74 20 74 68 65 20 77 72 6f 6e 67 20 70  lect the wrong p
3aa29 6c 61 6e 0a 20 20 20 20 2a 2a 20 66 6f 72 20 73  lan.    ** for s
3aa2a 63 72 69 70 74 73 20 73 75 63 68 20 61 73 20 74  cripts such as t
3aa2b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20  he following:.  
3aa2c 20 20 2a 2a 20 20 20 0a 20 20 20 20 2a 2a 20 20    **   .    **  
3aa2d 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31   CREATE TABLE t1
3aa2e 28 61 2c 20 62 29 3b 20 0a 20 20 20 20 2a 2a 20  (a, b); .    ** 
3aa2f 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
3aa30 32 28 63 2c 20 64 29 3b 0a 20 20 20 20 2a 2a 20  2(c, d);.    ** 
3aa31 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
3aa32 74 32 2c 20 74 31 20 57 48 45 52 45 20 74 32 2e  t2, t1 WHERE t2.
3aa33 72 6f 77 69 64 20 3d 20 74 31 2e 61 3b 0a 20 20  rowid = t1.a;.  
3aa34 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
3aa35 62 65 73 74 20 73 74 72 61 74 65 67 79 20 69 73  best strategy is
3aa36 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f   to iterate thro
3aa37 75 67 68 20 74 61 62 6c 65 20 74 31 20 66 69 72  ugh table t1 fir
3aa38 73 74 2e 20 48 6f 77 65 76 65 72 20 69 74 0a 20  st. However it. 
3aa39 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f 73     ** is not pos
3aa3a 73 69 62 6c 65 20 74 6f 20 64 65 74 65 72 6d 69  sible to determi
3aa3b 6e 65 20 74 68 69 73 20 77 69 74 68 20 61 20 73  ne this with a s
3aa3c 69 6d 70 6c 65 20 67 72 65 65 64 79 20 61 6c 67  imple greedy alg
3aa3d 6f 72 69 74 68 6d 2e 0a 20 20 20 20 2a 2a 20 53  orithm..    ** S
3aa3e 69 6e 63 65 20 74 68 65 20 63 6f 73 74 20 6f 66  ince the cost of
3aa3f 20 61 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74   a linear scan t
3aa40 68 72 6f 75 67 68 20 74 61 62 6c 65 20 74 32 20  hrough table t2 
3aa41 69 73 20 74 68 65 20 73 61 6d 65 20 0a 20 20 20  is the same .   
3aa42 20 2a 2a 20 61 73 20 74 68 65 20 63 6f 73 74 20   ** as the cost 
3aa43 6f 66 20 61 20 6c 69 6e 65 61 72 20 73 63 61 6e  of a linear scan
3aa44 20 74 68 72 6f 75 67 68 20 74 61 62 6c 65 20 74   through table t
3aa45 31 2c 20 61 20 73 69 6d 70 6c 65 20 67 72 65 65  1, a simple gree
3aa46 64 79 20 0a 20 20 20 20 2a 2a 20 61 6c 67 6f 72  dy .    ** algor
3aa47 69 74 68 6d 20 6d 61 79 20 63 68 6f 6f 73 65 20  ithm may choose 
3aa48 74 6f 20 75 73 65 20 74 32 20 66 6f 72 20 74 68  to use t2 for th
3aa49 65 20 6f 75 74 65 72 20 6c 6f 6f 70 2c 20 77 68  e outer loop, wh
3aa4a 69 63 68 20 69 73 20 61 20 6d 75 63 68 0a 20 20  ich is a much.  
3aa4b 20 20 2a 2a 20 63 6f 73 74 6c 69 65 72 20 61 70    ** costlier ap
3aa4c 70 72 6f 61 63 68 2e 0a 20 20 20 20 2a 2f 0a 20  proach..    */. 
3aa4d 20 20 20 6e 55 6e 63 6f 6e 73 74 72 61 69 6e 65     nUnconstraine
3aa4e 64 20 3d 20 30 3b 0a 20 20 20 20 6e 6f 74 49 6e  d = 0;.    notIn
3aa4f 64 65 78 65 64 20 3d 20 30 3b 0a 20 20 20 20 66  dexed = 0;.    f
3aa50 6f 72 28 69 73 4f 70 74 69 6d 61 6c 3d 28 69 46  or(isOptimal=(iF
3aa51 72 6f 6d 3c 6e 54 61 62 4c 69 73 74 2d 31 29 3b  rom<nTabList-1);
3aa52 20 69 73 4f 70 74 69 6d 61 6c 3e 3d 30 20 26 26   isOptimal>=0 &&
3aa53 20 62 65 73 74 4a 3c 30 3b 20 69 73 4f 70 74 69   bestJ<0; isOpti
3aa54 6d 61 6c 2d 2d 29 7b 0a 20 20 20 20 20 20 66 6f  mal--){.      fo
3aa55 72 28 6a 3d 69 46 72 6f 6d 2c 20 73 57 42 49 2e  r(j=iFrom, sWBI.
3aa56 70 53 72 63 3d 26 70 54 61 62 4c 69 73 74 2d 3e  pSrc=&pTabList->
3aa57 61 5b 6a 5d 3b 20 6a 3c 6e 54 61 62 4c 69 73 74  a[j]; j<nTabList
3aa58 3b 20 6a 2b 2b 2c 20 73 57 42 49 2e 70 53 72 63  ; j++, sWBI.pSrc
3aa59 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ++){.        int
3aa5a 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 3b 20 20   doNotReorder;  
3aa5b 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
3aa5c 73 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20 6e  s table should n
3aa5d 6f 74 20 62 65 20 72 65 6f 72 64 65 72 65 64 20  ot be reordered 
3aa5e 2a 2f 0a 20 20 0a 20 20 20 20 20 20 20 20 64 6f  */.  .        do
3aa5f 4e 6f 74 52 65 6f 72 64 65 72 20 3d 20 20 28 73  NotReorder =  (s
3aa60 57 42 49 2e 70 53 72 63 2d 3e 6a 6f 69 6e 74 79  WBI.pSrc->jointy
3aa61 70 65 20 26 20 28 4a 54 5f 4c 45 46 54 7c 4a 54  pe & (JT_LEFT|JT
3aa62 5f 43 52 4f 53 53 29 29 21 3d 30 3b 0a 20 20 20  _CROSS))!=0;.   
3aa63 20 20 20 20 20 69 66 28 20 6a 21 3d 69 46 72 6f       if( j!=iFro
3aa64 6d 20 26 26 20 64 6f 4e 6f 74 52 65 6f 72 64 65  m && doNotReorde
3aa65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  r ) break;.     
3aa66 20 20 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28 70     m = getMask(p
3aa67 4d 61 73 6b 53 65 74 2c 20 73 57 42 49 2e 70 53  MaskSet, sWBI.pS
3aa68 72 63 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20  rc->iCursor);.  
3aa69 20 20 20 20 20 20 69 66 28 20 28 6d 20 26 20 73        if( (m & s
3aa6a 57 42 49 2e 6e 6f 74 56 61 6c 69 64 29 3d 3d 30  WBI.notValid)==0
3aa6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
3aa6c 28 20 6a 3d 3d 69 46 72 6f 6d 20 29 20 69 46 72  ( j==iFrom ) iFr
3aa6d 6f 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  om++;.          
3aa6e 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
3aa6f 20 20 7d 0a 20 20 20 20 20 20 20 20 73 57 42 49    }.        sWBI
3aa70 2e 6e 6f 74 52 65 61 64 79 20 3d 20 28 69 73 4f  .notReady = (isO
3aa71 70 74 69 6d 61 6c 20 3f 20 6d 20 3a 20 73 57 42  ptimal ? m : sWB
3aa72 49 2e 6e 6f 74 56 61 6c 69 64 29 3b 0a 20 20 20  I.notValid);.   
3aa73 20 20 20 20 20 69 66 28 20 73 57 42 49 2e 70 53       if( sWBI.pS
3aa74 72 63 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29 20  rc->pIndex==0 ) 
3aa75 6e 55 6e 63 6f 6e 73 74 72 61 69 6e 65 64 2b 2b  nUnconstrained++
3aa76 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 57 48 45  ;.  .        WHE
3aa77 52 45 54 52 41 43 45 28 28 22 20 20 20 3d 3d 3d  RETRACE(("   ===
3aa78 20 74 72 79 69 6e 67 20 74 61 62 6c 65 20 25 64   trying table %d
3aa79 20 28 25 73 29 20 77 69 74 68 20 69 73 4f 70 74   (%s) with isOpt
3aa7a 69 6d 61 6c 3d 25 64 20 3d 3d 3d 5c 6e 22 2c 0a  imal=%d ===\n",.
3aa7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3aa7c 20 20 20 20 6a 2c 20 73 57 42 49 2e 70 53 72 63      j, sWBI.pSrc
3aa7d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 69  ->pTab->zName, i
3aa7e 73 4f 70 74 69 6d 61 6c 29 29 3b 0a 20 20 20 20  sOptimal));.    
3aa7f 20 20 20 20 61 73 73 65 72 74 28 20 73 57 42 49      assert( sWBI
3aa80 2e 70 53 72 63 2d 3e 70 54 61 62 20 29 3b 0a 23  .pSrc->pTab );.#
3aa81 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3aa82 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
3aa83 20 20 20 20 20 20 20 20 69 66 28 20 49 73 56 69          if( IsVi
3aa84 72 74 75 61 6c 28 73 57 42 49 2e 70 53 72 63 2d  rtual(sWBI.pSrc-
3aa85 3e 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  >pTab) ){.      
3aa86 20 20 20 20 73 57 42 49 2e 70 70 49 64 78 49 6e      sWBI.ppIdxIn
3aa87 66 6f 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b  fo = &pWInfo->a[
3aa88 6a 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20 20 20  j].pIdxInfo;.   
3aa89 20 20 20 20 20 20 20 62 65 73 74 56 69 72 74 75         bestVirtu
3aa8a 61 6c 49 6e 64 65 78 28 26 73 57 42 49 29 3b 0a  alIndex(&sWBI);.
3aa8b 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 0a 23          }else .#
3aa8c 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7b 0a  endif.        {.
3aa8d 20 20 20 20 20 20 20 20 20 20 62 65 73 74 42 74            bestBt
3aa8e 72 65 65 49 6e 64 65 78 28 26 73 57 42 49 29 3b  reeIndex(&sWBI);
3aa8f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3aa90 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 74     assert( isOpt
3aa91 69 6d 61 6c 20 7c 7c 20 28 73 57 42 49 2e 63 6f  imal || (sWBI.co
3aa92 73 74 2e 75 73 65 64 26 73 57 42 49 2e 6e 6f 74  st.used&sWBI.not
3aa93 56 61 6c 69 64 29 3d 3d 30 20 29 3b 0a 0a 20 20  Valid)==0 );..  
3aa94 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 49        /* If an I
3aa95 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65  NDEXED BY clause
3aa96 20 69 73 20 70 72 65 73 65 6e 74 2c 20 74 68 65   is present, the
3aa97 6e 20 74 68 65 20 70 6c 61 6e 20 6d 75 73 74 20  n the plan must 
3aa98 75 73 65 20 74 68 61 74 0a 20 20 20 20 20 20 20  use that.       
3aa99 20 2a 2a 20 69 6e 64 65 78 20 69 66 20 69 74 20   ** index if it 
3aa9a 75 73 65 73 20 61 6e 79 20 69 6e 64 65 78 20 61  uses any index a
3aa9b 74 20 61 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20  t all */.       
3aa9c 20 61 73 73 65 72 74 28 20 73 57 42 49 2e 70 53   assert( sWBI.pS
3aa9d 72 63 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 0a 20  rc->pIndex==0 . 
3aa9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3aa9f 20 7c 7c 20 28 73 57 42 49 2e 63 6f 73 74 2e 70   || (sWBI.cost.p
3aaa0 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
3aaa1 45 52 45 5f 4e 4f 54 5f 46 55 4c 4c 53 43 41 4e  ERE_NOT_FULLSCAN
3aaa2 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20  )==0.           
3aaa3 20 20 20 20 20 20 20 7c 7c 20 73 57 42 49 2e 63         || sWBI.c
3aaa4 6f 73 74 2e 70 6c 61 6e 2e 75 2e 70 49 64 78 3d  ost.plan.u.pIdx=
3aaa5 3d 73 57 42 49 2e 70 53 72 63 2d 3e 70 49 6e 64  =sWBI.pSrc->pInd
3aaa6 65 78 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 69  ex );..        i
3aaa7 66 28 20 69 73 4f 70 74 69 6d 61 6c 20 26 26 20  f( isOptimal && 
3aaa8 28 73 57 42 49 2e 63 6f 73 74 2e 70 6c 61 6e 2e  (sWBI.cost.plan.
3aaa9 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
3aaaa 4e 4f 54 5f 46 55 4c 4c 53 43 41 4e 29 3d 3d 30  NOT_FULLSCAN)==0
3aaab 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 6f   ){.          no
3aaac 74 49 6e 64 65 78 65 64 20 7c 3d 20 6d 3b 0a 20  tIndexed |= m;. 
3aaad 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3aaae 20 69 66 28 20 69 73 4f 70 74 69 6d 61 6c 20 29   if( isOptimal )
3aaaf 7b 0a 20 20 20 20 20 20 20 20 20 20 70 57 49 6e  {.          pWIn
3aab0 66 6f 2d 3e 61 5b 6a 5d 2e 72 4f 70 74 43 6f 73  fo->a[j].rOptCos
3aab1 74 20 3d 20 73 57 42 49 2e 63 6f 73 74 2e 72 43  t = sWBI.cost.rC
3aab2 6f 73 74 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ost;.        }el
3aab3 73 65 20 69 66 28 20 69 46 72 6f 6d 3c 6e 54 61  se if( iFrom<nTa
3aab4 62 4c 69 73 74 2d 31 20 29 7b 0a 20 20 20 20 20  bList-1 ){.     
3aab5 20 20 20 20 20 2f 2a 20 49 66 20 74 77 6f 20 6f       /* If two o
3aab6 72 20 6d 6f 72 65 20 74 61 62 6c 65 73 20 68 61  r more tables ha
3aab7 76 65 20 6e 65 61 72 6c 79 20 74 68 65 20 73 61  ve nearly the sa
3aab8 6d 65 20 6f 75 74 65 72 20 6c 6f 6f 70 20 63 6f  me outer loop co
3aab9 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  st,.          **
3aaba 20 76 65 72 79 20 64 69 66 66 65 72 65 6e 74 20   very different 
3aabb 69 6e 6e 65 72 20 6c 6f 6f 70 20 28 6f 70 74 69  inner loop (opti
3aabc 6d 61 6c 29 20 63 6f 73 74 2c 20 77 65 20 77 61  mal) cost, we wa
3aabd 6e 74 20 74 6f 20 63 68 6f 6f 73 65 0a 20 20 20  nt to choose.   
3aabe 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68         ** for th
3aabf 65 20 6f 75 74 65 72 20 6c 6f 6f 70 20 74 68 61  e outer loop tha
3aac0 74 20 74 61 62 6c 65 20 77 68 69 63 68 20 62 65  t table which be
3aac1 6e 65 66 69 74 73 20 74 68 65 20 6c 65 61 73 74  nefits the least
3aac2 20 66 72 6f 6d 0a 20 20 20 20 20 20 20 20 20 20   from.          
3aac3 2a 2a 20 62 65 69 6e 67 20 69 6e 20 74 68 65 20  ** being in the 
3aac4 69 6e 6e 65 72 20 6c 6f 6f 70 2e 20 20 54 68 65  inner loop.  The
3aac5 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20   following code 
3aac6 73 63 61 6c 65 73 20 74 68 65 20 0a 20 20 20 20  scales the .    
3aac7 20 20 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 6c        ** outer l
3aac8 6f 6f 70 20 63 6f 73 74 20 65 73 74 69 6d 61 74  oop cost estimat
3aac9 65 20 74 6f 20 61 63 63 6f 6d 70 6c 69 73 68 20  e to accomplish 
3aaca 74 68 61 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20  that. */.       
3aacb 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
3aacc 20 20 20 73 63 61 6c 69 6e 67 20 63 6f 73 74 20     scaling cost 
3aacd 66 72 6f 6d 20 25 2e 31 66 20 74 6f 20 25 2e 31  from %.1f to %.1
3aace 66 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  f\n",.          
3aacf 20 20 20 20 20 20 20 20 20 20 20 20 73 57 42 49              sWBI
3aad0 2e 63 6f 73 74 2e 72 43 6f 73 74 2c 0a 20 20 20  .cost.rCost,.   
3aad1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3aad2 20 20 20 73 57 42 49 2e 63 6f 73 74 2e 72 43 6f     sWBI.cost.rCo
3aad3 73 74 2f 70 57 49 6e 66 6f 2d 3e 61 5b 6a 5d 2e  st/pWInfo->a[j].
3aad4 72 4f 70 74 43 6f 73 74 29 29 3b 0a 20 20 20 20  rOptCost));.    
3aad5 20 20 20 20 20 20 73 57 42 49 2e 63 6f 73 74 2e        sWBI.cost.
3aad6 72 43 6f 73 74 20 2f 3d 20 70 57 49 6e 66 6f 2d  rCost /= pWInfo-
3aad7 3e 61 5b 6a 5d 2e 72 4f 70 74 43 6f 73 74 3b 0a  >a[j].rOptCost;.
3aad8 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
3aad9 20 20 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e 73     /* Conditions
3aada 20 75 6e 64 65 72 20 77 68 69 63 68 20 74 68 69   under which thi
3aadb 73 20 74 61 62 6c 65 20 62 65 63 6f 6d 65 73 20  s table becomes 
3aadc 74 68 65 20 62 65 73 74 20 73 6f 20 66 61 72 3a  the best so far:
3aadd 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
3aade 20 20 20 20 2a 2a 20 20 20 28 31 29 20 54 68 65      **   (1) The
3aadf 20 74 61 62 6c 65 20 6d 75 73 74 20 6e 6f 74 20   table must not 
3aae0 64 65 70 65 6e 64 20 6f 6e 20 6f 74 68 65 72 20  depend on other 
3aae1 74 61 62 6c 65 73 20 74 68 61 74 20 68 61 76 65  tables that have
3aae2 20 6e 6f 74 0a 20 20 20 20 20 20 20 20 2a 2a 20   not.        ** 
3aae3 20 20 20 20 20 20 79 65 74 20 72 75 6e 2e 20 20        yet run.  
3aae4 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c  (In other words,
3aae5 20 69 74 20 6d 75 73 74 20 6e 6f 74 20 64 65 70   it must not dep
3aae6 65 6e 64 20 6f 6e 20 74 61 62 6c 65 73 0a 20 20  end on tables.  
3aae7 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 69        **       i
3aae8 6e 20 69 6e 6e 65 72 20 6c 6f 6f 70 73 2e 29 0a  n inner loops.).
3aae9 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
3aaea 20 20 20 2a 2a 20 20 20 28 32 29 20 28 54 68 69     **   (2) (Thi
3aaeb 73 20 72 75 6c 65 20 77 61 73 20 72 65 6d 6f 76  s rule was remov
3aaec 65 64 20 6f 6e 20 32 30 31 32 2d 31 31 2d 30 39  ed on 2012-11-09
3aaed 2e 20 20 54 68 65 20 73 63 61 6c 69 6e 67 20 6f  .  The scaling o
3aaee 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  f the.        **
3aaef 20 20 20 20 20 20 20 63 6f 73 74 20 75 73 69 6e         cost usin
3aaf0 67 20 74 68 65 20 6f 70 74 69 6d 61 6c 20 73 63  g the optimal sc
3aaf1 61 6e 20 63 6f 73 74 20 6d 61 64 65 20 74 68 69  an cost made thi
3aaf2 73 20 72 75 6c 65 20 6f 62 73 6f 6c 65 74 65 2e  s rule obsolete.
3aaf3 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ).        **.   
3aaf4 20 20 20 20 20 2a 2a 20 20 20 28 33 29 20 41 6c       **   (3) Al
3aaf5 6c 20 74 61 62 6c 65 73 20 68 61 76 65 20 61 6e  l tables have an
3aaf6 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75   INDEXED BY clau
3aaf7 73 65 20 6f 72 20 74 68 69 73 20 74 61 62 6c 65  se or this table
3aaf8 20 6c 61 63 6b 73 20 61 6e 0a 20 20 20 20 20 20   lacks an.      
3aaf9 20 20 2a 2a 20 20 20 20 20 20 20 49 4e 44 45 58    **       INDEX
3aafa 45 44 20 42 59 20 63 6c 61 75 73 65 20 6f 72 20  ED BY clause or 
3aafb 74 68 69 73 20 74 61 62 6c 65 20 75 73 65 73 20  this table uses 
3aafc 74 68 65 20 73 70 65 63 69 66 69 63 0a 20 20 20  the specific.   
3aafd 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 69 6e       **       in
3aafe 64 65 78 20 73 70 65 63 69 66 69 65 64 20 62 79  dex specified by
3aaff 20 69 74 73 20 49 4e 44 45 58 45 44 20 42 59 20   its INDEXED BY 
3ab00 63 6c 61 75 73 65 2e 20 20 54 68 69 73 20 72 75  clause.  This ru
3ab01 6c 65 20 65 6e 73 75 72 65 73 0a 20 20 20 20 20  le ensures.     
3ab02 20 20 20 2a 2a 20 20 20 20 20 20 20 74 68 61 74     **       that
3ab03 20 61 20 62 65 73 74 2d 73 6f 2d 66 61 72 20 69   a best-so-far i
3ab04 73 20 61 6c 77 61 79 73 20 73 65 6c 65 63 74 65  s always selecte
3ab05 64 20 65 76 65 6e 20 69 66 20 61 6e 20 69 6d 70  d even if an imp
3ab06 6f 73 73 69 62 6c 65 0a 20 20 20 20 20 20 20 20  ossible.        
3ab07 2a 2a 20 20 20 20 20 20 20 63 6f 6d 62 69 6e 61  **       combina
3ab08 74 69 6f 6e 20 6f 66 20 49 4e 44 45 58 45 44 20  tion of INDEXED 
3ab09 42 59 20 63 6c 61 75 73 65 73 20 61 72 65 20 67  BY clauses are g
3ab0a 69 76 65 6e 2e 20 20 54 68 65 20 65 72 72 6f 72  iven.  The error
3ab0b 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20  .        **     
3ab0c 20 20 77 69 6c 6c 20 62 65 20 64 65 74 65 63 74    will be detect
3ab0d 65 64 20 61 6e 64 20 72 65 6c 61 79 65 64 20 62  ed and relayed b
3ab0e 61 63 6b 20 74 6f 20 74 68 65 20 61 70 70 6c 69  ack to the appli
3ab0f 63 61 74 69 6f 6e 20 6c 61 74 65 72 2e 0a 20 20  cation later..  
3ab10 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 54        **       T
3ab11 68 65 20 4e 45 56 45 52 28 29 20 63 6f 6d 65 73  he NEVER() comes
3ab12 20 61 62 6f 75 74 20 62 65 63 61 75 73 65 20 72   about because r
3ab13 75 6c 65 20 28 32 29 20 61 62 6f 76 65 20 70 72  ule (2) above pr
3ab14 65 76 65 6e 74 73 0a 20 20 20 20 20 20 20 20 2a  events.        *
3ab15 2a 20 20 20 20 20 20 20 41 6e 20 69 6e 64 65 78  *       An index
3ab16 61 62 6c 65 20 66 75 6c 6c 2d 74 61 62 6c 65 2d  able full-table-
3ab17 73 63 61 6e 20 66 72 6f 6d 20 72 65 61 63 68 69  scan from reachi
3ab18 6e 67 20 72 75 6c 65 20 28 33 29 2e 0a 20 20 20  ng rule (3)..   
3ab19 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
3ab1a 2a 2a 20 20 20 28 34 29 20 54 68 65 20 70 6c 61  **   (4) The pla
3ab1b 6e 20 63 6f 73 74 20 6d 75 73 74 20 62 65 20 6c  n cost must be l
3ab1c 6f 77 65 72 20 74 68 61 6e 20 70 72 69 6f 72 20  ower than prior 
3ab1d 70 6c 61 6e 73 2c 20 77 68 65 72 65 20 22 63 6f  plans, where "co
3ab1e 73 74 22 0a 20 20 20 20 20 20 20 20 2a 2a 20 20  st".        **  
3ab1f 20 20 20 20 20 69 73 20 64 65 66 69 6e 65 64 20       is defined 
3ab20 62 79 20 74 68 65 20 63 6f 6d 70 61 72 65 43 6f  by the compareCo
3ab21 73 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 62  st() function ab
3ab22 6f 76 65 2e 20 0a 20 20 20 20 20 20 20 20 2a 2f  ove. .        */
3ab23 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 73 57  .        if( (sW
3ab24 42 49 2e 63 6f 73 74 2e 75 73 65 64 26 73 57 42  BI.cost.used&sWB
3ab25 49 2e 6e 6f 74 56 61 6c 69 64 29 3d 3d 30 20 20  I.notValid)==0  
3ab26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab27 20 20 2f 2a 20 28 31 29 20 2a 2f 0a 20 20 20 20    /* (1) */.    
3ab28 20 20 20 20 20 20 20 20 26 26 20 28 6e 55 6e 63          && (nUnc
3ab29 6f 6e 73 74 72 61 69 6e 65 64 3d 3d 30 20 7c 7c  onstrained==0 ||
3ab2a 20 73 57 42 49 2e 70 53 72 63 2d 3e 70 49 6e 64   sWBI.pSrc->pInd
3ab2b 65 78 3d 3d 30 20 20 20 20 20 20 20 20 2f 2a 20  ex==0        /* 
3ab2c 28 33 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  (3) */.         
3ab2d 20 20 20 20 20 20 20 7c 7c 20 4e 45 56 45 52 28         || NEVER(
3ab2e 28 73 57 42 49 2e 63 6f 73 74 2e 70 6c 61 6e 2e  (sWBI.cost.plan.
3ab2f 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
3ab30 4e 4f 54 5f 46 55 4c 4c 53 43 41 4e 29 21 3d 30  NOT_FULLSCAN)!=0
3ab31 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 26  )).            &
3ab32 26 20 28 62 65 73 74 4a 3c 30 20 7c 7c 20 63 6f  & (bestJ<0 || co
3ab33 6d 70 61 72 65 43 6f 73 74 28 26 73 57 42 49 2e  mpareCost(&sWBI.
3ab34 63 6f 73 74 2c 20 26 62 65 73 74 50 6c 61 6e 29  cost, &bestPlan)
3ab35 29 20 20 20 2f 2a 20 28 34 29 20 2a 2f 0a 20 20  )   /* (4) */.  
3ab36 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
3ab37 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
3ab38 20 20 20 3d 3d 3d 20 74 61 62 6c 65 20 25 64 20     === table %d 
3ab39 28 25 73 29 20 69 73 20 62 65 73 74 20 73 6f 20  (%s) is best so 
3ab3a 66 61 72 5c 6e 22 0a 20 20 20 20 20 20 20 20 20  far\n".         
3ab3b 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20               "  
3ab3c 20 20 20 20 20 63 6f 73 74 3d 25 2e 31 66 2c 20       cost=%.1f, 
3ab3d 6e 52 6f 77 3d 25 2e 31 66 2c 20 6e 4f 42 53 61  nRow=%.1f, nOBSa
3ab3e 74 3d 25 64 2c 20 77 73 46 6c 61 67 73 3d 25 30  t=%d, wsFlags=%0
3ab3f 38 78 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  8x\n",.         
3ab40 20 20 20 20 20 20 20 20 20 20 20 20 20 6a 2c 20               j, 
3ab41 73 57 42 49 2e 70 53 72 63 2d 3e 70 54 61 62 2d  sWBI.pSrc->pTab-
3ab42 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  >zName,.        
3ab43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 57                sW
3ab44 42 49 2e 63 6f 73 74 2e 72 43 6f 73 74 2c 20 73  BI.cost.rCost, s
3ab45 57 42 49 2e 63 6f 73 74 2e 70 6c 61 6e 2e 6e 52  WBI.cost.plan.nR
3ab46 6f 77 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ow,.            
3ab47 20 20 20 20 20 20 20 20 20 20 73 57 42 49 2e 63            sWBI.c
3ab48 6f 73 74 2e 70 6c 61 6e 2e 6e 4f 42 53 61 74 2c  ost.plan.nOBSat,
3ab49 20 73 57 42 49 2e 63 6f 73 74 2e 70 6c 61 6e 2e   sWBI.cost.plan.
3ab4a 77 73 46 6c 61 67 73 29 29 3b 0a 20 20 20 20 20  wsFlags));.     
3ab4b 20 20 20 20 20 62 65 73 74 50 6c 61 6e 20 3d 20       bestPlan = 
3ab4c 73 57 42 49 2e 63 6f 73 74 3b 0a 20 20 20 20 20  sWBI.cost;.     
3ab4d 20 20 20 20 20 62 65 73 74 4a 20 3d 20 6a 3b 0a       bestJ = j;.
3ab4e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3ab4f 20 20 69 66 28 20 64 6f 4e 6f 74 52 65 6f 72 64    if( doNotReord
3ab50 65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  er ) break;.    
3ab51 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73    }.    }.    as
3ab52 73 65 72 74 28 20 62 65 73 74 4a 3e 3d 30 20 29  sert( bestJ>=0 )
3ab53 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 57  ;.    assert( sW
3ab54 42 49 2e 6e 6f 74 56 61 6c 69 64 20 26 20 67 65  BI.notValid & ge
3ab55 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20  tMask(pMaskSet, 
3ab56 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74  pTabList->a[best
3ab57 4a 5d 2e 69 43 75 72 73 6f 72 29 20 29 3b 0a 20  J].iCursor) );. 
3ab58 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
3ab59 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 73 65  *** Optimizer se
3ab5a 6c 65 63 74 73 20 74 61 62 6c 65 20 25 64 20 28  lects table %d (
3ab5b 25 73 29 20 66 6f 72 20 6c 6f 6f 70 20 25 64 20  %s) for loop %d 
3ab5c 77 69 74 68 3a 5c 6e 22 0a 20 20 20 20 20 20 20  with:\n".       
3ab5d 20 20 20 20 20 20 20 20 20 22 20 20 20 20 63 6f           "    co
3ab5e 73 74 3d 25 2e 31 66 2c 20 6e 52 6f 77 3d 25 2e  st=%.1f, nRow=%.
3ab5f 31 66 2c 20 6e 4f 42 53 61 74 3d 25 64 2c 20 77  1f, nOBSat=%d, w
3ab60 73 46 6c 61 67 73 3d 30 78 25 30 38 78 5c 6e 22  sFlags=0x%08x\n"
3ab61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
3ab62 20 20 62 65 73 74 4a 2c 20 70 54 61 62 4c 69 73    bestJ, pTabLis
3ab63 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e 70 54 61 62  t->a[bestJ].pTab
3ab64 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  ->zName,.       
3ab65 20 20 20 20 20 20 20 20 20 70 4c 65 76 65 6c 2d           pLevel-
3ab66 70 57 49 6e 66 6f 2d 3e 61 2c 20 62 65 73 74 50  pWInfo->a, bestP
3ab67 6c 61 6e 2e 72 43 6f 73 74 2c 20 62 65 73 74 50  lan.rCost, bestP
3ab68 6c 61 6e 2e 70 6c 61 6e 2e 6e 52 6f 77 2c 0a 20  lan.plan.nRow,. 
3ab69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
3ab6a 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 6e 4f 42  estPlan.plan.nOB
3ab6b 53 61 74 2c 20 62 65 73 74 50 6c 61 6e 2e 70 6c  Sat, bestPlan.pl
3ab6c 61 6e 2e 77 73 46 6c 61 67 73 29 29 3b 0a 20 20  an.wsFlags));.  
3ab6d 20 20 69 66 28 20 28 62 65 73 74 50 6c 61 6e 2e    if( (bestPlan.
3ab6e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
3ab6f 48 45 52 45 5f 44 49 53 54 49 4e 43 54 29 21 3d  HERE_DISTINCT)!=
3ab70 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  0 ){.      asser
3ab71 74 28 20 70 57 49 6e 66 6f 2d 3e 65 44 69 73 74  t( pWInfo->eDist
3ab72 69 6e 63 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20  inct==0 );.     
3ab73 20 70 57 49 6e 66 6f 2d 3e 65 44 69 73 74 69 6e   pWInfo->eDistin
3ab74 63 74 20 3d 20 57 48 45 52 45 5f 44 49 53 54 49  ct = WHERE_DISTI
3ab75 4e 43 54 5f 4f 52 44 45 52 45 44 3b 0a 20 20 20  NCT_ORDERED;.   
3ab76 20 7d 0a 20 20 20 20 61 6e 64 46 6c 61 67 73 20   }.    andFlags 
3ab77 26 3d 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e  &= bestPlan.plan
3ab78 2e 77 73 46 6c 61 67 73 3b 0a 20 20 20 20 70 4c  .wsFlags;.    pL
3ab79 65 76 65 6c 2d 3e 70 6c 61 6e 20 3d 20 62 65 73  evel->plan = bes
3ab7a 74 50 6c 61 6e 2e 70 6c 61 6e 3b 0a 20 20 20 20  tPlan.plan;.    
3ab7b 70 4c 65 76 65 6c 2d 3e 69 54 61 62 43 75 72 20  pLevel->iTabCur 
3ab7c 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65  = pTabList->a[be
3ab7d 73 74 4a 5d 2e 69 43 75 72 73 6f 72 3b 0a 20 20  stJ].iCursor;.  
3ab7e 20 20 74 65 73 74 63 61 73 65 28 20 62 65 73 74    testcase( best
3ab7f 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c 61 67  Plan.plan.wsFlag
3ab80 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45  s & WHERE_INDEXE
3ab81 44 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  D );.    testcas
3ab82 65 28 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e  e( bestPlan.plan
3ab83 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
3ab84 5f 54 45 4d 50 5f 49 4e 44 45 58 20 29 3b 0a 20  _TEMP_INDEX );. 
3ab85 20 20 20 69 66 28 20 62 65 73 74 50 6c 61 6e 2e     if( bestPlan.
3ab86 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 28  plan.wsFlags & (
3ab87 57 48 45 52 45 5f 49 4e 44 45 58 45 44 7c 57 48  WHERE_INDEXED|WH
3ab88 45 52 45 5f 54 45 4d 50 5f 49 4e 44 45 58 29 20  ERE_TEMP_INDEX) 
3ab89 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 77 63  ){.      if( (wc
3ab8a 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45  trlFlags & WHERE
3ab8b 5f 4f 4e 45 54 41 42 4c 45 5f 4f 4e 4c 59 29 20  _ONETABLE_ONLY) 
3ab8c 0a 20 20 20 20 20 20 20 26 26 20 28 62 65 73 74  .       && (best
3ab8d 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c 61 67  Plan.plan.wsFlag
3ab8e 73 20 26 20 57 48 45 52 45 5f 54 45 4d 50 5f 49  s & WHERE_TEMP_I
3ab8f 4e 44 45 58 29 3d 3d 30 20 0a 20 20 20 20 20 20  NDEX)==0 .      
3ab90 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 65 76 65  ){.        pLeve
3ab91 6c 2d 3e 69 49 64 78 43 75 72 20 3d 20 69 49 64  l->iIdxCur = iId
3ab92 78 43 75 72 3b 0a 20 20 20 20 20 20 7d 65 6c 73  xCur;.      }els
3ab93 65 7b 0a 20 20 20 20 20 20 20 20 70 4c 65 76 65  e{.        pLeve
3ab94 6c 2d 3e 69 49 64 78 43 75 72 20 3d 20 70 50 61  l->iIdxCur = pPa
3ab95 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
3ab96 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
3ab97 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 49        pLevel->iI
3ab98 64 78 43 75 72 20 3d 20 2d 31 3b 0a 20 20 20 20  dxCur = -1;.    
3ab99 7d 0a 20 20 20 20 73 57 42 49 2e 6e 6f 74 56 61  }.    sWBI.notVa
3ab9a 6c 69 64 20 26 3d 20 7e 67 65 74 4d 61 73 6b 28  lid &= ~getMask(
3ab9b 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69  pMaskSet, pTabLi
3ab9c 73 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e 69 43 75  st->a[bestJ].iCu
3ab9d 72 73 6f 72 29 3b 0a 20 20 20 20 70 4c 65 76 65  rsor);.    pLeve
3ab9e 6c 2d 3e 69 46 72 6f 6d 20 3d 20 28 75 38 29 62  l->iFrom = (u8)b
3ab9f 65 73 74 4a 3b 0a 20 20 20 20 69 66 28 20 62 65  estJ;.    if( be
3aba0 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 6e 52 6f 77  stPlan.plan.nRow
3aba1 3e 3d 28 64 6f 75 62 6c 65 29 31 20 29 7b 0a 20  >=(double)1 ){. 
3aba2 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 51 75       pParse->nQu
3aba3 65 72 79 4c 6f 6f 70 20 2a 3d 20 62 65 73 74 50  eryLoop *= bestP
3aba4 6c 61 6e 2e 70 6c 61 6e 2e 6e 52 6f 77 3b 0a 20  lan.plan.nRow;. 
3aba5 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65     }..    /* Che
3aba6 63 6b 20 74 68 61 74 20 69 66 20 74 68 65 20 74  ck that if the t
3aba7 61 62 6c 65 20 73 63 61 6e 6e 65 64 20 62 79 20  able scanned by 
3aba8 74 68 69 73 20 6c 6f 6f 70 20 69 74 65 72 61 74  this loop iterat
3aba9 69 6f 6e 20 68 61 64 20 61 6e 0a 20 20 20 20 2a  ion had an.    *
3abaa 2a 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61  * INDEXED BY cla
3abab 75 73 65 20 61 74 74 61 63 68 65 64 20 74 6f 20  use attached to 
3abac 69 74 2c 20 74 68 61 74 20 74 68 65 20 6e 61 6d  it, that the nam
3abad 65 64 20 69 6e 64 65 78 20 69 73 20 62 65 69 6e  ed index is bein
3abae 67 0a 20 20 20 20 2a 2a 20 75 73 65 64 20 66 6f  g.    ** used fo
3abaf 72 20 74 68 65 20 73 63 61 6e 2e 20 49 66 20 6e  r the scan. If n
3abb0 6f 74 2c 20 74 68 65 6e 20 71 75 65 72 79 20 63  ot, then query c
3abb1 6f 6d 70 69 6c 61 74 69 6f 6e 20 68 61 73 20 66  ompilation has f
3abb2 61 69 6c 65 64 2e 0a 20 20 20 20 2a 2a 20 52 65  ailed..    ** Re
3abb3 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20  turn an error.. 
3abb4 20 20 20 2a 2f 0a 20 20 20 20 70 49 64 78 20 3d     */.    pIdx =
3abb5 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73   pTabList->a[bes
3abb6 74 4a 5d 2e 70 49 6e 64 65 78 3b 0a 20 20 20 20  tJ].pIndex;.    
3abb7 69 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20  if( pIdx ){.    
3abb8 20 20 69 66 28 20 28 62 65 73 74 50 6c 61 6e 2e    if( (bestPlan.
3abb9 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
3abba 48 45 52 45 5f 49 4e 44 45 58 45 44 29 3d 3d 30  HERE_INDEXED)==0
3abbb 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
3abbc 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
3abbd 73 65 2c 20 22 63 61 6e 6e 6f 74 20 75 73 65 20  se, "cannot use 
3abbe 69 6e 64 65 78 3a 20 25 73 22 2c 20 70 49 64 78  index: %s", pIdx
3abbf 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
3abc0 20 20 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69    goto whereBegi
3abc1 6e 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 65  nError;.      }e
3abc2 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
3abc3 49 66 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59  If an INDEXED BY
3abc4 20 63 6c 61 75 73 65 20 69 73 20 75 73 65 64 2c   clause is used,
3abc5 20 74 68 65 20 62 65 73 74 49 6e 64 65 78 28 29   the bestIndex()
3abc6 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 20 20 20   function is.   
3abc7 20 20 20 20 20 2a 2a 20 67 75 61 72 61 6e 74 65       ** guarante
3abc8 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 69  ed to find the i
3abc9 6e 64 65 78 20 73 70 65 63 69 66 69 65 64 20 69  ndex specified i
3abca 6e 20 74 68 65 20 49 4e 44 45 58 45 44 20 42 59  n the INDEXED BY
3abcb 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20   clause.        
3abcc 2a 2a 20 69 66 20 69 74 20 66 69 6e 64 20 61 6e  ** if it find an
3abcd 20 69 6e 64 65 78 20 61 74 20 61 6c 6c 2e 20 2a   index at all. *
3abce 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
3abcf 28 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e  ( bestPlan.plan.
3abd0 75 2e 70 49 64 78 3d 3d 70 49 64 78 20 29 3b 0a  u.pIdx==pIdx );.
3abd1 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
3abd2 7d 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28  }.  WHERETRACE((
3abd3 22 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 46  "*** Optimizer F
3abd4 69 6e 69 73 68 65 64 20 2a 2a 2a 5c 6e 22 29 29  inished ***\n"))
3abd5 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
3abd6 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  nErr || db->mall
3abd7 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
3abd8 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45  goto whereBeginE
3abd9 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rror;.  }.  if( 
3abda 6e 54 61 62 4c 69 73 74 20 29 7b 0a 20 20 20 20  nTabList ){.    
3abdb 70 4c 65 76 65 6c 2d 2d 3b 0a 20 20 20 20 70 57  pLevel--;.    pW
3abdc 49 6e 66 6f 2d 3e 6e 4f 42 53 61 74 20 3d 20 70  Info->nOBSat = p
3abdd 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 4f 42 53  Level->plan.nOBS
3abde 61 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  at;.  }else{.   
3abdf 20 70 57 49 6e 66 6f 2d 3e 6e 4f 42 53 61 74 20   pWInfo->nOBSat 
3abe0 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  = 0;.  }..  /* I
3abe1 66 20 74 68 65 20 74 6f 74 61 6c 20 71 75 65 72  f the total quer
3abe2 79 20 6f 6e 6c 79 20 73 65 6c 65 63 74 73 20 61  y only selects a
3abe3 20 73 69 6e 67 6c 65 20 72 6f 77 2c 20 74 68 65   single row, the
3abe4 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 20  n the ORDER BY. 
3abe5 20 2a 2a 20 63 6c 61 75 73 65 20 69 73 20 69 72   ** clause is ir
3abe6 72 65 6c 65 76 61 6e 74 2e 0a 20 20 2a 2f 0a 20  relevant..  */. 
3abe7 20 69 66 28 20 28 61 6e 64 46 6c 61 67 73 20 26   if( (andFlags &
3abe8 20 57 48 45 52 45 5f 55 4e 49 51 55 45 29 21 3d   WHERE_UNIQUE)!=
3abe9 30 20 26 26 20 70 4f 72 64 65 72 42 79 20 29 7b  0 && pOrderBy ){
3abea 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 54 61  .    assert( nTa
3abeb 62 4c 69 73 74 3d 3d 30 20 7c 7c 20 28 70 4c 65  bList==0 || (pLe
3abec 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
3abed 73 20 26 20 57 48 45 52 45 5f 41 4c 4c 5f 55 4e  s & WHERE_ALL_UN
3abee 49 51 55 45 29 21 3d 30 20 29 3b 0a 20 20 20 20  IQUE)!=0 );.    
3abef 70 57 49 6e 66 6f 2d 3e 6e 4f 42 53 61 74 20 3d  pWInfo->nOBSat =
3abf0 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72   pOrderBy->nExpr
3abf1 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
3abf2 68 65 20 63 61 6c 6c 65 72 20 69 73 20 61 6e 20  he caller is an 
3abf3 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45  UPDATE or DELETE
3abf4 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
3abf5 69 73 20 72 65 71 75 65 73 74 69 6e 67 0a 20 20  is requesting.  
3abf6 2a 2a 20 74 6f 20 75 73 65 20 61 20 6f 6e 65 2d  ** to use a one-
3abf7 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 2c 20  pass algorithm, 
3abf8 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 69  determine if thi
3abf9 73 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65  s is appropriate
3abfa 2e 0a 20 20 2a 2a 20 54 68 65 20 6f 6e 65 2d 70  ..  ** The one-p
3abfb 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 6f 6e  ass algorithm on
3abfc 6c 79 20 77 6f 72 6b 73 20 69 66 20 74 68 65 20  ly works if the 
3abfd 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e  WHERE clause con
3abfe 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20 74 68  straints.  ** th
3abff 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75  e statement to u
3ac00 70 64 61 74 65 20 61 20 73 69 6e 67 6c 65 20 72  pdate a single r
3ac01 6f 77 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ow..  */.  asser
3ac02 74 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26  t( (wctrlFlags &
3ac03 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44   WHERE_ONEPASS_D
3ac04 45 53 49 52 45 44 29 3d 3d 30 20 7c 7c 20 70 57  ESIRED)==0 || pW
3ac05 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3d 3d 31 20  Info->nLevel==1 
3ac06 29 3b 0a 20 20 69 66 28 20 28 77 63 74 72 6c 46  );.  if( (wctrlF
3ac07 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f 4e 45  lags & WHERE_ONE
3ac08 50 41 53 53 5f 44 45 53 49 52 45 44 29 21 3d 30  PASS_DESIRED)!=0
3ac09 20 26 26 20 28 61 6e 64 46 6c 61 67 73 20 26 20   && (andFlags & 
3ac0a 57 48 45 52 45 5f 55 4e 49 51 55 45 29 21 3d 30  WHERE_UNIQUE)!=0
3ac0b 20 29 7b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e   ){.    pWInfo->
3ac0c 6f 6b 4f 6e 65 50 61 73 73 20 3d 20 31 3b 0a 20  okOnePass = 1;. 
3ac0d 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 30 5d 2e     pWInfo->a[0].
3ac0e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 3d 20  plan.wsFlags &= 
3ac0f 7e 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 3b  ~WHERE_IDX_ONLY;
3ac10 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20  .  }..  /* Open 
3ac11 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68  all tables in th
3ac12 65 20 70 54 61 62 4c 69 73 74 20 61 6e 64 20 61  e pTabList and a
3ac13 6e 79 20 69 6e 64 69 63 65 73 20 73 65 6c 65 63  ny indices selec
3ac14 74 65 64 20 66 6f 72 0a 20 20 2a 2a 20 73 65 61  ted for.  ** sea
3ac15 72 63 68 69 6e 67 20 74 68 6f 73 65 20 74 61 62  rching those tab
3ac16 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  les..  */.  sqli
3ac17 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
3ac18 65 6d 61 28 70 50 61 72 73 65 2c 20 2d 31 29 3b  ema(pParse, -1);
3ac19 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 65 20 63   /* Insert the c
3ac1a 6f 6f 6b 69 65 20 76 65 72 69 66 69 65 72 20 47  ookie verifier G
3ac1b 6f 74 6f 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64  oto */.  notRead
3ac1c 79 20 3d 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b  y = ~(Bitmask)0;
3ac1d 0a 20 20 70 57 49 6e 66 6f 2d 3e 6e 52 6f 77 4f  .  pWInfo->nRowO
3ac1e 75 74 20 3d 20 28 64 6f 75 62 6c 65 29 31 3b 0a  ut = (double)1;.
3ac1f 20 20 66 6f 72 28 69 69 3d 30 2c 20 70 4c 65 76    for(ii=0, pLev
3ac20 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 69  el=pWInfo->a; ii
3ac21 3c 6e 54 61 62 4c 69 73 74 3b 20 69 69 2b 2b 2c  <nTabList; ii++,
3ac22 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20   pLevel++){.    
3ac23 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
3ac24 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 6f 70 65   /* Table to ope
3ac25 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62  n */.    int iDb
3ac26 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  ;         /* Ind
3ac27 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 63  ex of database c
3ac28 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 2f  ontaining table/
3ac29 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 73 74 72  index */.    str
3ac2a 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
3ac2b 20 2a 70 54 61 62 49 74 65 6d 3b 0a 0a 20 20 20   *pTabItem;..   
3ac2c 20 70 54 61 62 49 74 65 6d 20 3d 20 26 70 54 61   pTabItem = &pTa
3ac2d 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d  bList->a[pLevel-
3ac2e 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 70 54 61  >iFrom];.    pTa
3ac2f 62 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70 54  b = pTabItem->pT
3ac30 61 62 3b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e  ab;.    pWInfo->
3ac31 6e 52 6f 77 4f 75 74 20 2a 3d 20 70 4c 65 76 65  nRowOut *= pLeve
3ac32 6c 2d 3e 70 6c 61 6e 2e 6e 52 6f 77 3b 0a 20 20  l->plan.nRow;.  
3ac33 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
3ac34 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
3ac35 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
3ac36 0a 20 20 20 20 69 66 28 20 28 70 54 61 62 2d 3e  .    if( (pTab->
3ac37 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 45 70  tabFlags & TF_Ep
3ac38 68 65 6d 65 72 61 6c 29 21 3d 30 20 7c 7c 20 70  hemeral)!=0 || p
3ac39 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  Tab->pSelect ){.
3ac3a 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68        /* Do noth
3ac3b 69 6e 67 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65  ing */.    }else
3ac3c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
3ac3d 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
3ac3e 45 0a 20 20 20 20 69 66 28 20 28 70 4c 65 76 65  E.    if( (pLeve
3ac3f 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
3ac40 26 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54  & WHERE_VIRTUALT
3ac41 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20 20 20  ABLE)!=0 ){.    
3ac42 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 56    const char *pV
3ac43 54 61 62 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  Tab = (const cha
3ac44 72 20 2a 29 73 71 6c 69 74 65 33 47 65 74 56 54  r *)sqlite3GetVT
3ac45 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 3b 0a  able(db, pTab);.
3ac46 20 20 20 20 20 20 69 6e 74 20 69 43 75 72 20 3d        int iCur =
3ac47 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73   pTabItem->iCurs
3ac48 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  or;.      sqlite
3ac49 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
3ac4a 50 5f 56 4f 70 65 6e 2c 20 69 43 75 72 2c 20 30  P_VOpen, iCur, 0
3ac4b 2c 20 30 2c 20 70 56 54 61 62 2c 20 50 34 5f 56  , 0, pVTab, P4_V
3ac4c 54 41 42 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  TAB);.    }else 
3ac4d 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
3ac4e 61 62 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ab) ){.      /* 
3ac4f 6e 6f 6f 70 20 2a 2f 0a 20 20 20 20 7d 65 6c 73  noop */.    }els
3ac50 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28  e.#endif.    if(
3ac51 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
3ac52 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
3ac53 44 58 5f 4f 4e 4c 59 29 3d 3d 30 0a 20 20 20 20  DX_ONLY)==0.    
3ac54 20 20 20 20 20 26 26 20 28 77 63 74 72 6c 46 6c       && (wctrlFl
3ac55 61 67 73 20 26 20 57 48 45 52 45 5f 4f 4d 49 54  ags & WHERE_OMIT
3ac56 5f 4f 50 45 4e 5f 43 4c 4f 53 45 29 3d 3d 30 20  _OPEN_CLOSE)==0 
3ac57 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 20  ){.      int op 
3ac58 3d 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50  = pWInfo->okOneP
3ac59 61 73 73 20 3f 20 4f 50 5f 4f 70 65 6e 57 72 69  ass ? OP_OpenWri
3ac5a 74 65 20 3a 20 4f 50 5f 4f 70 65 6e 52 65 61 64  te : OP_OpenRead
3ac5b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  ;.      sqlite3O
3ac5c 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c  penTable(pParse,
3ac5d 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73   pTabItem->iCurs
3ac5e 6f 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 6f  or, iDb, pTab, o
3ac5f 70 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  p);.      testca
3ac60 73 65 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3d 3d  se( pTab->nCol==
3ac61 42 4d 53 2d 31 20 29 3b 0a 20 20 20 20 20 20 74  BMS-1 );.      t
3ac62 65 73 74 63 61 73 65 28 20 70 54 61 62 2d 3e 6e  estcase( pTab->n
3ac63 43 6f 6c 3d 3d 42 4d 53 20 29 3b 0a 20 20 20 20  Col==BMS );.    
3ac64 20 20 69 66 28 20 21 70 57 49 6e 66 6f 2d 3e 6f    if( !pWInfo->o
3ac65 6b 4f 6e 65 50 61 73 73 20 26 26 20 70 54 61 62  kOnePass && pTab
3ac66 2d 3e 6e 43 6f 6c 3c 42 4d 53 20 29 7b 0a 20 20  ->nCol<BMS ){.  
3ac67 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 62 20        Bitmask b 
3ac68 3d 20 70 54 61 62 49 74 65 6d 2d 3e 63 6f 6c 55  = pTabItem->colU
3ac69 73 65 64 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  sed;.        int
3ac6a 20 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20   n = 0;.        
3ac6b 66 6f 72 28 3b 20 62 3b 20 62 3d 62 3e 3e 31 2c  for(; b; b=b>>1,
3ac6c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20   n++){}.        
3ac6d 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
3ac6e 65 50 34 28 76 2c 20 73 71 6c 69 74 65 33 56 64  eP4(v, sqlite3Vd
3ac6f 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
3ac70 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  -1, .           
3ac71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ac72 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50   SQLITE_INT_TO_P
3ac73 54 52 28 6e 29 2c 20 50 34 5f 49 4e 54 33 32 29  TR(n), P4_INT32)
3ac74 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
3ac75 28 20 6e 3c 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20  ( n<=pTab->nCol 
3ac76 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
3ac77 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
3ac78 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61  te3TableLock(pPa
3ac79 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e  rse, iDb, pTab->
3ac7a 74 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a  tnum, 0, pTab->z
3ac7b 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66  Name);.    }.#if
3ac7c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
3ac7d 5f 41 55 54 4f 4d 41 54 49 43 5f 49 4e 44 45 58  _AUTOMATIC_INDEX
3ac7e 0a 20 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c  .    if( (pLevel
3ac7f 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
3ac80 20 57 48 45 52 45 5f 54 45 4d 50 5f 49 4e 44 45   WHERE_TEMP_INDE
3ac81 58 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 63  X)!=0 ){.      c
3ac82 6f 6e 73 74 72 75 63 74 41 75 74 6f 6d 61 74 69  onstructAutomati
3ac83 63 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 73  cIndex(pParse, s
3ac84 57 42 49 2e 70 57 43 2c 20 70 54 61 62 49 74 65  WBI.pWC, pTabIte
3ac85 6d 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70 4c 65  m, notReady, pLe
3ac86 76 65 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a  vel);.    }else.
3ac87 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 28  #endif.    if( (
3ac88 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
3ac89 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44  lags & WHERE_IND
3ac8a 45 58 45 44 29 21 3d 30 20 29 7b 0a 20 20 20 20  EXED)!=0 ){.    
3ac8b 20 20 49 6e 64 65 78 20 2a 70 49 78 20 3d 20 70    Index *pIx = p
3ac8c 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49  Level->plan.u.pI
3ac8d 64 78 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66  dx;.      KeyInf
3ac8e 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65  o *pKey = sqlite
3ac8f 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50  3IndexKeyinfo(pP
3ac90 61 72 73 65 2c 20 70 49 78 29 3b 0a 20 20 20 20  arse, pIx);.    
3ac91 20 20 69 6e 74 20 69 49 6e 64 65 78 43 75 72 20    int iIndexCur 
3ac92 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75  = pLevel->iIdxCu
3ac93 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  r;.      assert(
3ac94 20 70 49 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70   pIx->pSchema==p
3ac95 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a  Tab->pSchema );.
3ac96 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 49        assert( iI
3ac97 6e 64 65 78 43 75 72 3e 3d 30 20 29 3b 0a 20 20  ndexCur>=0 );.  
3ac98 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
3ac99 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp4(v, OP_Open
3ac9a 52 65 61 64 2c 20 69 49 6e 64 65 78 43 75 72 2c  Read, iIndexCur,
3ac9b 20 70 49 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c   pIx->tnum, iDb,
3ac9c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3ac9d 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
3ac9e 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  pKey, P4_KEYINFO
3ac9f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20  _HANDOFF);.     
3aca0 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
3aca1 20 22 25 73 22 2c 20 70 49 78 2d 3e 7a 4e 61 6d   "%s", pIx->zNam
3aca2 65 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  e));.    }.    s
3aca3 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
3aca4 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69  Schema(pParse, i
3aca5 44 62 29 3b 0a 20 20 20 20 6e 6f 74 52 65 61 64  Db);.    notRead
3aca6 79 20 26 3d 20 7e 67 65 74 4d 61 73 6b 28 73 57  y &= ~getMask(sW
3aca7 42 49 2e 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74  BI.pWC->pMaskSet
3aca8 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72  , pTabItem->iCur
3aca9 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 70 57 49 6e  sor);.  }.  pWIn
3acaa 66 6f 2d 3e 69 54 6f 70 20 3d 20 73 71 6c 69 74  fo->iTop = sqlit
3acab 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
3acac 72 28 76 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  r(v);.  if( db->
3acad 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67  mallocFailed ) g
3acae 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72  oto whereBeginEr
3acaf 72 6f 72 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  ror;..  /* Gener
3acb0 61 74 65 20 74 68 65 20 63 6f 64 65 20 74 6f 20  ate the code to 
3acb1 64 6f 20 74 68 65 20 73 65 61 72 63 68 2e 20 20  do the search.  
3acb2 45 61 63 68 20 69 74 65 72 61 74 69 6f 6e 20 6f  Each iteration o
3acb3 66 20 74 68 65 20 66 6f 72 0a 20 20 2a 2a 20 6c  f the for.  ** l
3acb4 6f 6f 70 20 62 65 6c 6f 77 20 67 65 6e 65 72 61  oop below genera
3acb5 74 65 73 20 63 6f 64 65 20 66 6f 72 20 61 20 73  tes code for a s
3acb6 69 6e 67 6c 65 20 6e 65 73 74 65 64 20 6c 6f 6f  ingle nested loo
3acb7 70 20 6f 66 20 74 68 65 20 56 4d 0a 20 20 2a 2a  p of the VM.  **
3acb8 20 70 72 6f 67 72 61 6d 2e 0a 20 20 2a 2f 0a 20   program..  */. 
3acb9 20 6e 6f 74 52 65 61 64 79 20 3d 20 7e 28 42 69   notReady = ~(Bi
3acba 74 6d 61 73 6b 29 30 3b 0a 20 20 66 6f 72 28 69  tmask)0;.  for(i
3acbb 69 3d 30 3b 20 69 69 3c 6e 54 61 62 4c 69 73 74  i=0; ii<nTabList
3acbc 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 70 4c 65  ; ii++){.    pLe
3acbd 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61  vel = &pWInfo->a
3acbe 5b 69 69 5d 3b 0a 20 20 20 20 65 78 70 6c 61 69  [ii];.    explai
3acbf 6e 4f 6e 65 53 63 61 6e 28 70 50 61 72 73 65 2c  nOneScan(pParse,
3acc0 20 70 54 61 62 4c 69 73 74 2c 20 70 4c 65 76 65   pTabList, pLeve
3acc1 6c 2c 20 69 69 2c 20 70 4c 65 76 65 6c 2d 3e 69  l, ii, pLevel->i
3acc2 46 72 6f 6d 2c 20 77 63 74 72 6c 46 6c 61 67 73  From, wctrlFlags
3acc3 29 3b 0a 20 20 20 20 6e 6f 74 52 65 61 64 79 20  );.    notReady 
3acc4 3d 20 63 6f 64 65 4f 6e 65 4c 6f 6f 70 53 74 61  = codeOneLoopSta
3acc5 72 74 28 70 57 49 6e 66 6f 2c 20 69 69 2c 20 77  rt(pWInfo, ii, w
3acc6 63 74 72 6c 46 6c 61 67 73 2c 20 6e 6f 74 52 65  ctrlFlags, notRe
3acc7 61 64 79 29 3b 0a 20 20 20 20 70 57 49 6e 66 6f  ady);.    pWInfo
3acc8 2d 3e 69 43 6f 6e 74 69 6e 75 65 20 3d 20 70 4c  ->iContinue = pL
3acc9 65 76 65 6c 2d 3e 61 64 64 72 43 6f 6e 74 3b 0a  evel->addrCont;.
3acca 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49    }..#ifdef SQLI
3accb 54 45 5f 54 45 53 54 20 20 2f 2a 20 46 6f 72 20  TE_TEST  /* For 
3accc 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75  testing and debu
3accd 67 67 69 6e 67 20 75 73 65 20 6f 6e 6c 79 20 2a  gging use only *
3acce 2f 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20 69 6e  /.  /* Record in
3accf 20 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20   the query plan 
3acd0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
3acd1 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61  t the current ta
3acd2 62 6c 65 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65  ble.  ** and the
3acd3 20 69 6e 64 65 78 20 75 73 65 64 20 74 6f 20 61   index used to a
3acd4 63 63 65 73 73 20 69 74 20 28 69 66 20 61 6e 79  ccess it (if any
3acd5 29 2e 20 20 49 66 20 74 68 65 20 74 61 62 6c 65  ).  If the table
3acd6 20 69 74 73 65 6c 66 0a 20 20 2a 2a 20 69 73 20   itself.  ** is 
3acd7 6e 6f 74 20 75 73 65 64 2c 20 69 74 73 20 6e 61  not used, its na
3acd8 6d 65 20 69 73 20 6a 75 73 74 20 27 7b 7d 27 2e  me is just '{}'.
3acd9 20 20 49 66 20 6e 6f 20 69 6e 64 65 78 20 69 73    If no index is
3acda 20 75 73 65 64 0a 20 20 2a 2a 20 74 68 65 20 69   used.  ** the i
3acdb 6e 64 65 78 20 69 73 20 6c 69 73 74 65 64 20 61  ndex is listed a
3acdc 73 20 22 7b 7d 22 2e 20 20 49 66 20 74 68 65 20  s "{}".  If the 
3acdd 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20 75  primary key is u
3acde 73 65 64 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64  sed the.  ** ind
3acdf 65 78 20 6e 61 6d 65 20 69 73 20 27 2a 27 2e 0a  ex name is '*'..
3ace0 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b    */.  for(ii=0;
3ace1 20 69 69 3c 6e 54 61 62 4c 69 73 74 3b 20 69 69   ii<nTabList; ii
3ace2 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  ++){.    char *z
3ace3 3b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20  ;.    int n;.   
3ace4 20 69 6e 74 20 77 3b 0a 20 20 20 20 73 74 72 75   int w;.    stru
3ace5 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
3ace6 2a 70 54 61 62 49 74 65 6d 3b 0a 0a 20 20 20 20  *pTabItem;..    
3ace7 70 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f  pLevel = &pWInfo
3ace8 2d 3e 61 5b 69 69 5d 3b 0a 20 20 20 20 77 20 3d  ->a[ii];.    w =
3ace9 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
3acea 46 6c 61 67 73 3b 0a 20 20 20 20 70 54 61 62 49  Flags;.    pTabI
3aceb 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d  tem = &pTabList-
3acec 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d  >a[pLevel->iFrom
3aced 5d 3b 0a 20 20 20 20 7a 20 3d 20 70 54 61 62 49  ];.    z = pTabI
3acee 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20  tem->zAlias;.   
3acef 20 69 66 28 20 7a 3d 3d 30 20 29 20 7a 20 3d 20   if( z==0 ) z = 
3acf0 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2d 3e  pTabItem->pTab->
3acf1 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 20 3d 20 73  zName;.    n = s
3acf2 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
3acf3 29 3b 0a 20 20 20 20 69 66 28 20 6e 2b 6e 51 50  );.    if( n+nQP
3acf4 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71 6c  lan < sizeof(sql
3acf5 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 29  ite3_query_plan)
3acf6 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  -10 ){.      if(
3acf7 20 28 77 20 26 20 57 48 45 52 45 5f 49 44 58 5f   (w & WHERE_IDX_
3acf8 4f 4e 4c 59 29 21 3d 30 20 26 26 20 28 77 20 26  ONLY)!=0 && (w &
3acf9 20 57 48 45 52 45 5f 43 4f 56 45 52 5f 53 43 41   WHERE_COVER_SCA
3acfa 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  N)==0 ){.       
3acfb 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33   memcpy(&sqlite3
3acfc 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
3acfd 61 6e 5d 2c 20 22 7b 7d 22 2c 20 32 29 3b 0a 20  an], "{}", 2);. 
3acfe 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d         nQPlan +=
3acff 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   2;.      }else{
3ad00 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
3ad01 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70  &sqlite3_query_p
3ad02 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 7a 2c 20  lan[nQPlan], z, 
3ad03 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 51 50 6c  n);.        nQPl
3ad04 61 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 7d  an += n;.      }
3ad05 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 71  .      sqlite3_q
3ad06 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e  uery_plan[nQPlan
3ad07 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 7d  ++] = ' ';.    }
3ad08 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 77  .    testcase( w
3ad09 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45   & WHERE_ROWID_E
3ad0a 51 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  Q );.    testcas
3ad0b 65 28 20 77 20 26 20 57 48 45 52 45 5f 52 4f 57  e( w & WHERE_ROW
3ad0c 49 44 5f 52 41 4e 47 45 20 29 3b 0a 20 20 20 20  ID_RANGE );.    
3ad0d 69 66 28 20 77 20 26 20 28 57 48 45 52 45 5f 52  if( w & (WHERE_R
3ad0e 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52 4f  OWID_EQ|WHERE_RO
3ad0f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a 20 20  WID_RANGE) ){.  
3ad10 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69      memcpy(&sqli
3ad11 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e  te3_query_plan[n
3ad12 51 50 6c 61 6e 5d 2c 20 22 2a 20 22 2c 20 32 29  QPlan], "* ", 2)
3ad13 3b 0a 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b  ;.      nQPlan +
3ad14 3d 20 32 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  = 2;.    }else i
3ad15 66 28 20 28 77 20 26 20 57 48 45 52 45 5f 49 4e  f( (w & WHERE_IN
3ad16 44 45 58 45 44 29 21 3d 30 20 26 26 20 28 77 20  DEXED)!=0 && (w 
3ad17 26 20 57 48 45 52 45 5f 43 4f 56 45 52 5f 53 43  & WHERE_COVER_SC
3ad18 41 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  AN)==0 ){.      
3ad19 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
3ad1a 6e 33 30 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  n30(pLevel->plan
3ad1b 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b  .u.pIdx->zName);
3ad1c 0a 20 20 20 20 20 20 69 66 28 20 6e 2b 6e 51 50  .      if( n+nQP
3ad1d 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71 6c  lan < sizeof(sql
3ad1e 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 29  ite3_query_plan)
3ad1f 2d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  -2 ){.        me
3ad20 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75  mcpy(&sqlite3_qu
3ad21 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d  ery_plan[nQPlan]
3ad22 2c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75  , pLevel->plan.u
3ad23 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 6e 29  .pIdx->zName, n)
3ad24 3b 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e  ;.        nQPlan
3ad25 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20 73   += n;.        s
3ad26 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
3ad27 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20  n[nQPlan++] = ' 
3ad28 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  ';.      }.    }
3ad29 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63  else{.      memc
3ad2a 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72  py(&sqlite3_quer
3ad2b 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20  y_plan[nQPlan], 
3ad2c 22 7b 7d 20 22 2c 20 33 29 3b 0a 20 20 20 20 20  "{} ", 3);.     
3ad2d 20 6e 51 50 6c 61 6e 20 2b 3d 20 33 3b 0a 20 20   nQPlan += 3;.  
3ad2e 20 20 7d 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28    }.  }.  while(
3ad2f 20 6e 51 50 6c 61 6e 3e 30 20 26 26 20 73 71 6c   nQPlan>0 && sql
3ad30 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
3ad31 6e 51 50 6c 61 6e 2d 31 5d 3d 3d 27 20 27 20 29  nQPlan-1]==' ' )
3ad32 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 71 75  {.    sqlite3_qu
3ad33 65 72 79 5f 70 6c 61 6e 5b 2d 2d 6e 51 50 6c 61  ery_plan[--nQPla
3ad34 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  n] = 0;.  }.  sq
3ad35 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
3ad36 5b 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b 0a 20 20  [nQPlan] = 0;.  
3ad37 6e 51 50 6c 61 6e 20 3d 20 30 3b 0a 23 65 6e 64  nQPlan = 0;.#end
3ad38 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53  if /* SQLITE_TES
3ad39 54 20 2f 2f 20 54 65 73 74 69 6e 67 20 61 6e 64  T // Testing and
3ad3a 20 64 65 62 75 67 67 69 6e 67 20 75 73 65 20 6f   debugging use o
3ad3b 6e 6c 79 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 63  nly */..  /* Rec
3ad3c 6f 72 64 20 74 68 65 20 63 6f 6e 74 69 6e 75 61  ord the continua
3ad3d 74 69 6f 6e 20 61 64 64 72 65 73 73 20 69 6e 20  tion address in 
3ad3e 74 68 65 20 57 68 65 72 65 49 6e 66 6f 20 73 74  the WhereInfo st
3ad3f 72 75 63 74 75 72 65 2e 20 20 54 68 65 6e 0a 20  ructure.  Then. 
3ad40 20 2a 2a 20 63 6c 65 61 6e 20 75 70 20 61 6e 64   ** clean up and
3ad41 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20   return..  */.  
3ad42 72 65 74 75 72 6e 20 70 57 49 6e 66 6f 3b 0a 0a  return pWInfo;..
3ad43 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69    /* Jump here i
3ad44 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 2a  f malloc fails *
3ad45 2f 0a 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f  /.whereBeginErro
3ad46 72 3a 0a 20 20 69 66 28 20 70 57 49 6e 66 6f 20  r:.  if( pWInfo 
3ad47 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  ){.    pParse->n
3ad48 51 75 65 72 79 4c 6f 6f 70 20 3d 20 70 57 49 6e  QueryLoop = pWIn
3ad49 66 6f 2d 3e 73 61 76 65 64 4e 51 75 65 72 79 4c  fo->savedNQueryL
3ad4a 6f 6f 70 3b 0a 20 20 20 20 77 68 65 72 65 49 6e  oop;.    whereIn
3ad4b 66 6f 46 72 65 65 28 64 62 2c 20 70 57 49 6e 66  foFree(db, pWInf
3ad4c 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  o);.  }.  return
3ad4d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e   0;.}../*.** Gen
3ad4e 65 72 61 74 65 20 74 68 65 20 65 6e 64 20 6f 66  erate the end of
3ad4f 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 2e   the WHERE loop.
3ad50 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f    See comments o
3ad51 6e 20 0a 2a 2a 20 73 71 6c 69 74 65 33 57 68 65  n .** sqlite3Whe
3ad52 72 65 42 65 67 69 6e 28 29 20 66 6f 72 20 61 64  reBegin() for ad
3ad53 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
3ad54 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
3ad55 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
3ad56 69 74 65 33 57 68 65 72 65 45 6e 64 28 57 68 65  ite3WhereEnd(Whe
3ad57 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 29 7b  reInfo *pWInfo){
3ad58 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
3ad59 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73   = pWInfo->pPars
3ad5a 65 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  e;.  Vdbe *v = p
3ad5b 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
3ad5c 69 6e 74 20 69 3b 0a 20 20 57 68 65 72 65 4c 65  int i;.  WhereLe
3ad5d 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 0a 20 20 53  vel *pLevel;.  S
3ad5e 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
3ad5f 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c   = pWInfo->pTabL
3ad60 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  ist;.  sqlite3 *
3ad61 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
3ad62 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
3ad63 6c 6f 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e  loop termination
3ad64 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 71   code..  */.  sq
3ad65 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c  lite3ExprCacheCl
3ad66 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 66  ear(pParse);.  f
3ad67 6f 72 28 69 3d 70 57 49 6e 66 6f 2d 3e 6e 4c 65  or(i=pWInfo->nLe
3ad68 76 65 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d  vel-1; i>=0; i--
3ad69 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c 20 3d 20  ){.    pLevel = 
3ad6a 26 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a 20  &pWInfo->a[i];. 
3ad6b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
3ad6c 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c  solveLabel(v, pL
3ad6d 65 76 65 6c 2d 3e 61 64 64 72 43 6f 6e 74 29 3b  evel->addrCont);
3ad6e 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
3ad6f 3e 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a  >op!=OP_Noop ){.
3ad70 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
3ad71 65 41 64 64 4f 70 32 28 76 2c 20 70 4c 65 76 65  eAddOp2(v, pLeve
3ad72 6c 2d 3e 6f 70 2c 20 70 4c 65 76 65 6c 2d 3e 70  l->op, pLevel->p
3ad73 31 2c 20 70 4c 65 76 65 6c 2d 3e 70 32 29 3b 0a  1, pLevel->p2);.
3ad74 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
3ad75 65 43 68 61 6e 67 65 50 35 28 76 2c 20 70 4c 65  eChangeP5(v, pLe
3ad76 76 65 6c 2d 3e 70 35 29 3b 0a 20 20 20 20 7d 0a  vel->p5);.    }.
3ad77 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
3ad78 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
3ad79 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 26 26 20  HERE_IN_ABLE && 
3ad7a 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e  pLevel->u.in.nIn
3ad7b 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75  >0 ){.      stru
3ad7c 63 74 20 49 6e 4c 6f 6f 70 20 2a 70 49 6e 3b 0a  ct InLoop *pIn;.
3ad7d 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
3ad7e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
3ad7f 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c  solveLabel(v, pL
3ad80 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 29 3b 0a  evel->addrNxt);.
3ad81 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 4c 65 76        for(j=pLev
3ad82 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 2c 20 70 49  el->u.in.nIn, pI
3ad83 6e 3d 26 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e  n=&pLevel->u.in.
3ad84 61 49 6e 4c 6f 6f 70 5b 6a 2d 31 5d 3b 20 6a 3e  aInLoop[j-1]; j>
3ad85 30 3b 20 6a 2d 2d 2c 20 70 49 6e 2d 2d 29 7b 0a  0; j--, pIn--){.
3ad86 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
3ad87 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70  dbeJumpHere(v, p
3ad88 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 2b 31 29  In->addrInTop+1)
3ad89 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
3ad8a 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
3ad8b 50 5f 4e 65 78 74 2c 20 70 49 6e 2d 3e 69 43 75  P_Next, pIn->iCu
3ad8c 72 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f  r, pIn->addrInTo
3ad8d 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  p);.        sqli
3ad8e 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
3ad8f 76 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f  v, pIn->addrInTo
3ad90 70 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  p-1);.      }.  
3ad91 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
3ad92 65 28 64 62 2c 20 70 4c 65 76 65 6c 2d 3e 75 2e  e(db, pLevel->u.
3ad93 69 6e 2e 61 49 6e 4c 6f 6f 70 29 3b 0a 20 20 20  in.aInLoop);.   
3ad94 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
3ad95 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
3ad96 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72  , pLevel->addrBr
3ad97 6b 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76  k);.    if( pLev
3ad98 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 29 7b  el->iLeftJoin ){
3ad99 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b  .      int addr;
3ad9a 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71  .      addr = sq
3ad9b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
3ad9c 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 70 4c 65  v, OP_IfPos, pLe
3ad9d 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b  vel->iLeftJoin);
3ad9e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
3ad9f 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
3ada0 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58  lags & WHERE_IDX
3ada1 5f 4f 4e 4c 59 29 3d 3d 30 0a 20 20 20 20 20 20  _ONLY)==0.      
3ada2 20 20 20 20 20 7c 7c 20 28 70 4c 65 76 65 6c 2d       || (pLevel-
3ada3 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
3ada4 57 48 45 52 45 5f 49 4e 44 45 58 45 44 29 21 3d  WHERE_INDEXED)!=
3ada5 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28  0 );.      if( (
3ada6 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
3ada7 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58  lags & WHERE_IDX
3ada8 5f 4f 4e 4c 59 29 3d 3d 30 20 29 7b 0a 20 20 20  _ONLY)==0 ){.   
3ada9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3adaa 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp1(v, OP_Nul
3adab 6c 52 6f 77 2c 20 70 54 61 62 4c 69 73 74 2d 3e  lRow, pTabList->
3adac 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20  a[i].iCursor);. 
3adad 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
3adae 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72   pLevel->iIdxCur
3adaf 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  >=0 ){.        s
3adb0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
3adb1 28 76 2c 20 4f 50 5f 4e 75 6c 6c 52 6f 77 2c 20  (v, OP_NullRow, 
3adb2 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 29  pLevel->iIdxCur)
3adb3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3adb4 69 66 28 20 70 4c 65 76 65 6c 2d 3e 6f 70 3d 3d  if( pLevel->op==
3adb5 4f 50 5f 52 65 74 75 72 6e 20 29 7b 0a 20 20 20  OP_Return ){.   
3adb6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3adb7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73  AddOp2(v, OP_Gos
3adb8 75 62 2c 20 70 4c 65 76 65 6c 2d 3e 70 31 2c 20  ub, pLevel->p1, 
3adb9 70 4c 65 76 65 6c 2d 3e 61 64 64 72 46 69 72 73  pLevel->addrFirs
3adba 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  t);.      }else{
3adbb 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
3adbc 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
3adbd 5f 47 6f 74 6f 2c 20 30 2c 20 70 4c 65 76 65 6c  _Goto, 0, pLevel
3adbe 2d 3e 61 64 64 72 46 69 72 73 74 29 3b 0a 20 20  ->addrFirst);.  
3adbf 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
3adc0 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
3adc1 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 7d 0a  v, addr);.    }.
3adc2 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 22 62    }..  /* The "b
3adc3 72 65 61 6b 22 20 70 6f 69 6e 74 20 69 73 20 68  reak" point is h
3adc4 65 72 65 2c 20 6a 75 73 74 20 70 61 73 74 20 74  ere, just past t
3adc5 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6f 75  he end of the ou
3adc6 74 65 72 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 53  ter loop..  ** S
3adc7 65 74 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 73 71  et it..  */.  sq
3adc8 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
3adc9 4c 61 62 65 6c 28 76 2c 20 70 57 49 6e 66 6f 2d  Label(v, pWInfo-
3adca 3e 69 42 72 65 61 6b 29 3b 0a 0a 20 20 2f 2a 20  >iBreak);..  /* 
3adcb 43 6c 6f 73 65 20 61 6c 6c 20 6f 66 20 74 68 65  Close all of the
3adcc 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 77 65   cursors that we
3adcd 72 65 20 6f 70 65 6e 65 64 20 62 79 20 73 71 6c  re opened by sql
3adce 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 2e 0a  ite3WhereBegin..
3adcf 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
3add0 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3d 3d 31  WInfo->nLevel==1
3add1 20 7c 7c 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76   || pWInfo->nLev
3add2 65 6c 3d 3d 70 54 61 62 4c 69 73 74 2d 3e 6e 53  el==pTabList->nS
3add3 72 63 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  rc );.  for(i=0,
3add4 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e   pLevel=pWInfo->
3add5 61 3b 20 69 3c 70 57 49 6e 66 6f 2d 3e 6e 4c 65  a; i<pWInfo->nLe
3add6 76 65 6c 3b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c  vel; i++, pLevel
3add7 2b 2b 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  ++){.    Index *
3add8 70 49 64 78 20 3d 20 30 3b 0a 20 20 20 20 73 74  pIdx = 0;.    st
3add9 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
3adda 6d 20 2a 70 54 61 62 49 74 65 6d 20 3d 20 26 70  m *pTabItem = &p
3addb 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65  TabList->a[pLeve
3addc 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 54  l->iFrom];.    T
3addd 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 54 61  able *pTab = pTa
3adde 62 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20  bItem->pTab;.   
3addf 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d 30   assert( pTab!=0
3ade0 20 29 3b 0a 20 20 20 20 69 66 28 20 28 70 54 61   );.    if( (pTa
3ade1 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
3ade2 5f 45 70 68 65 6d 65 72 61 6c 29 3d 3d 30 0a 20  _Ephemeral)==0. 
3ade3 20 20 20 20 26 26 20 70 54 61 62 2d 3e 70 53 65      && pTab->pSe
3ade4 6c 65 63 74 3d 3d 30 0a 20 20 20 20 20 26 26 20  lect==0.     && 
3ade5 28 70 57 49 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c  (pWInfo->wctrlFl
3ade6 61 67 73 20 26 20 57 48 45 52 45 5f 4f 4d 49 54  ags & WHERE_OMIT
3ade7 5f 4f 50 45 4e 5f 43 4c 4f 53 45 29 3d 3d 30 0a  _OPEN_CLOSE)==0.
3ade8 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 6e 74      ){.      int
3ade9 20 77 73 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c   ws = pLevel->pl
3adea 61 6e 2e 77 73 46 6c 61 67 73 3b 0a 20 20 20 20  an.wsFlags;.    
3adeb 20 20 69 66 28 20 21 70 57 49 6e 66 6f 2d 3e 6f    if( !pWInfo->o
3adec 6b 4f 6e 65 50 61 73 73 20 26 26 20 28 77 73 20  kOnePass && (ws 
3aded 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  & WHERE_IDX_ONLY
3adee 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
3adef 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
3adf0 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70  1(v, OP_Close, p
3adf1 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  TabItem->iCursor
3adf2 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
3adf3 20 69 66 28 20 28 77 73 20 26 20 57 48 45 52 45   if( (ws & WHERE
3adf4 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 26 26 20  _INDEXED)!=0 && 
3adf5 28 77 73 20 26 20 57 48 45 52 45 5f 54 45 4d 50  (ws & WHERE_TEMP
3adf6 5f 49 4e 44 45 58 29 3d 3d 30 20 29 7b 0a 20 20  _INDEX)==0 ){.  
3adf7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
3adf8 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
3adf9 6f 73 65 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64  ose, pLevel->iId
3adfa 78 43 75 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  xCur);.      }. 
3adfb 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
3adfc 74 68 69 73 20 73 63 61 6e 20 75 73 65 73 20 61  this scan uses a
3adfd 6e 20 69 6e 64 65 78 2c 20 6d 61 6b 65 20 63 6f  n index, make co
3adfe 64 65 20 73 75 62 73 74 69 74 75 74 69 6f 6e 73  de substitutions
3adff 20 74 6f 20 72 65 61 64 20 64 61 74 61 0a 20 20   to read data.  
3ae00 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e    ** from the in
3ae01 64 65 78 20 69 6e 20 70 72 65 66 65 72 65 6e 63  dex in preferenc
3ae02 65 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 20  e to the table. 
3ae03 53 6f 6d 65 74 69 6d 65 73 2c 20 74 68 69 73 20  Sometimes, this 
3ae04 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65  means.    ** the
3ae05 20 74 61 62 6c 65 20 6e 65 65 64 20 6e 65 76 65   table need neve
3ae06 72 20 62 65 20 72 65 61 64 20 66 72 6f 6d 2e 20  r be read from. 
3ae07 54 68 69 73 20 69 73 20 61 20 70 65 72 66 6f 72  This is a perfor
3ae08 6d 61 6e 63 65 20 62 6f 6f 73 74 2c 0a 20 20 20  mance boost,.   
3ae09 20 2a 2a 20 61 73 20 74 68 65 20 76 64 62 65 20   ** as the vdbe 
3ae0a 6c 65 76 65 6c 20 77 61 69 74 73 20 75 6e 74 69  level waits unti
3ae0b 6c 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 72  l the table is r
3ae0c 65 61 64 20 62 65 66 6f 72 65 20 61 63 74 75 61  ead before actua
3ae0d 6c 6c 79 0a 20 20 20 20 2a 2a 20 73 65 65 6b 69  lly.    ** seeki
3ae0e 6e 67 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  ng the table cur
3ae0f 73 6f 72 20 74 6f 20 74 68 65 20 72 65 63 6f 72  sor to the recor
3ae10 64 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  d corresponding 
3ae11 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20  to the current. 
3ae12 20 20 20 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69     ** position i
3ae13 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20  n the index..   
3ae14 20 2a 2a 20 0a 20 20 20 20 2a 2a 20 43 61 6c 6c   ** .    ** Call
3ae15 73 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67 65  s to the code ge
3ae16 6e 65 72 61 74 6f 72 20 69 6e 20 62 65 74 77 65  nerator in betwe
3ae17 65 6e 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  en sqlite3WhereB
3ae18 65 67 69 6e 20 61 6e 64 0a 20 20 20 20 2a 2a 20  egin and.    ** 
3ae19 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 20  sqlite3WhereEnd 
3ae1a 77 69 6c 6c 20 68 61 76 65 20 63 72 65 61 74 65  will have create
3ae1b 64 20 63 6f 64 65 20 74 68 61 74 20 72 65 66 65  d code that refe
3ae1c 72 65 6e 63 65 73 20 74 68 65 20 74 61 62 6c 65  rences the table
3ae1d 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79  .    ** directly
3ae1e 2e 20 20 54 68 69 73 20 6c 6f 6f 70 20 73 63 61  .  This loop sca
3ae1f 6e 73 20 61 6c 6c 20 74 68 61 74 20 63 6f 64 65  ns all that code
3ae20 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70 63   looking for opc
3ae21 6f 64 65 73 0a 20 20 20 20 2a 2a 20 74 68 61 74  odes.    ** that
3ae22 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 74   reference the t
3ae23 61 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72 74  able and convert
3ae24 73 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63 6f  s them into opco
3ae25 64 65 73 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  des that.    ** 
3ae26 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 69 6e  reference the in
3ae27 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  dex..    */.    
3ae28 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  if( pLevel->plan
3ae29 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
3ae2a 5f 49 4e 44 45 58 45 44 20 29 7b 0a 20 20 20 20  _INDEXED ){.    
3ae2b 20 20 70 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d    pIdx = pLevel-
3ae2c 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a 20 20  >plan.u.pIdx;.  
3ae2d 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76    }else if( pLev
3ae2e 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
3ae2f 20 26 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f   & WHERE_MULTI_O
3ae30 52 20 29 7b 0a 20 20 20 20 20 20 70 49 64 78 20  R ){.      pIdx 
3ae31 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e 70 43 6f 76  = pLevel->u.pCov
3ae32 69 64 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  idx;.    }.    i
3ae33 66 28 20 70 49 64 78 20 26 26 20 21 64 62 2d 3e  f( pIdx && !db->
3ae34 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 7b 0a 20  mallocFailed){. 
3ae35 20 20 20 20 20 69 6e 74 20 6b 2c 20 6a 2c 20 6c       int k, j, l
3ae36 61 73 74 3b 0a 20 20 20 20 20 20 56 64 62 65 4f  ast;.      VdbeO
3ae37 70 20 2a 70 4f 70 3b 0a 0a 20 20 20 20 20 20 70  p *pOp;..      p
3ae38 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  Op = sqlite3Vdbe
3ae39 47 65 74 4f 70 28 76 2c 20 70 57 49 6e 66 6f 2d  GetOp(v, pWInfo-
3ae3a 3e 69 54 6f 70 29 3b 0a 20 20 20 20 20 20 6c 61  >iTop);.      la
3ae3b 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  st = sqlite3Vdbe
3ae3c 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
3ae3d 20 20 20 20 20 20 66 6f 72 28 6b 3d 70 57 49 6e        for(k=pWIn
3ae3e 66 6f 2d 3e 69 54 6f 70 3b 20 6b 3c 6c 61 73 74  fo->iTop; k<last
3ae3f 3b 20 6b 2b 2b 2c 20 70 4f 70 2b 2b 29 7b 0a 20  ; k++, pOp++){. 
3ae40 20 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e         if( pOp->
3ae41 70 31 21 3d 70 4c 65 76 65 6c 2d 3e 69 54 61 62  p1!=pLevel->iTab
3ae42 43 75 72 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  Cur ) continue;.
3ae43 20 20 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d          if( pOp-
3ae44 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 75  >opcode==OP_Colu
3ae45 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  mn ){.          
3ae46 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d  for(j=0; j<pIdx-
3ae47 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a  >nColumn; j++){.
3ae48 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
3ae49 70 4f 70 2d 3e 70 32 3d 3d 70 49 64 78 2d 3e 61  pOp->p2==pIdx->a
3ae4a 69 43 6f 6c 75 6d 6e 5b 6a 5d 20 29 7b 0a 20 20  iColumn[j] ){.  
3ae4b 20 20 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d              pOp-
3ae4c 3e 70 32 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20  >p2 = j;.       
3ae4d 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d         pOp->p1 =
3ae4e 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72   pLevel->iIdxCur
3ae4f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
3ae50 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
3ae51 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
3ae52 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
3ae53 74 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  t( (pLevel->plan
3ae54 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
3ae55 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d 30 0a 20 20  _IDX_ONLY)==0.  
3ae56 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
3ae57 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20  j<pIdx->nColumn 
3ae58 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
3ae59 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
3ae5a 3d 3d 4f 50 5f 52 6f 77 69 64 20 29 7b 0a 20 20  ==OP_Rowid ){.  
3ae5b 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 31 20          pOp->p1 
3ae5c 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75  = pLevel->iIdxCu
3ae5d 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70  r;.          pOp
3ae5e 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 49 64  ->opcode = OP_Id
3ae5f 78 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 20 20  xRowid;.        
3ae60 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
3ae61 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c 20    }..  /* Final 
3ae62 63 6c 65 61 6e 75 70 0a 20 20 2a 2f 0a 20 20 70  cleanup.  */.  p
3ae63 50 61 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f 6f  Parse->nQueryLoo
3ae64 70 20 3d 20 70 57 49 6e 66 6f 2d 3e 73 61 76 65  p = pWInfo->save
3ae65 64 4e 51 75 65 72 79 4c 6f 6f 70 3b 0a 20 20 77  dNQueryLoop;.  w
3ae66 68 65 72 65 49 6e 66 6f 46 72 65 65 28 64 62 2c  hereInfoFree(db,
3ae67 20 70 57 49 6e 66 6f 29 3b 0a 20 20 72 65 74 75   pWInfo);.  retu
3ae68 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  rn;.}../********
3ae69 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 77 68  ****** End of wh
3ae6a 65 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ere.c **********
3ae6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ae6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ae6d 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
3ae6e 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
3ae6f 65 20 70 61 72 73 65 2e 63 20 2a 2a 2a 2a 2a 2a  e parse.c ******
3ae70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ae71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ae72 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 72 69 76 65 72  *****/./* Driver
3ae73 20 74 65 6d 70 6c 61 74 65 20 66 6f 72 20 74 68   template for th
3ae74 65 20 4c 45 4d 4f 4e 20 70 61 72 73 65 72 20 67  e LEMON parser g
3ae75 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 65  enerator..** The
3ae76 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
3ae77 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
3ae78 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
3ae79 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  .**.** This vers
3ae7a 69 6f 6e 20 6f 66 20 22 6c 65 6d 70 61 72 2e 63  ion of "lempar.c
3ae7b 22 20 69 73 20 6d 6f 64 69 66 69 65 64 2c 20 73  " is modified, s
3ae7c 6c 69 67 68 74 6c 79 2c 20 66 6f 72 20 75 73 65  lightly, for use
3ae7d 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 54   by SQLite..** T
3ae7e 68 65 20 6f 6e 6c 79 20 6d 6f 64 69 66 69 63 61  he only modifica
3ae7f 74 69 6f 6e 73 20 61 72 65 20 74 68 65 20 61 64  tions are the ad
3ae80 64 69 74 69 6f 6e 20 6f 66 20 61 20 63 6f 75 70  dition of a coup
3ae81 6c 65 20 6f 66 20 4e 45 56 45 52 28 29 0a 2a 2a  le of NEVER().**
3ae82 20 6d 61 63 72 6f 73 20 74 6f 20 64 69 73 61 62   macros to disab
3ae83 6c 65 20 74 65 73 74 73 20 74 68 61 74 20 61 72  le tests that ar
3ae84 65 20 6e 65 65 64 65 64 20 69 6e 20 74 68 65 20  e needed in the 
3ae85 63 61 73 65 20 6f 66 20 61 20 67 65 6e 65 72 61  case of a genera
3ae86 6c 0a 2a 2a 20 4c 41 4c 52 28 31 29 20 67 72 61  l.** LALR(1) gra
3ae87 6d 6d 61 72 20 62 75 74 20 77 68 69 63 68 20 61  mmar but which a
3ae88 72 65 20 61 6c 77 61 79 73 20 66 61 6c 73 65 20  re always false 
3ae89 69 6e 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66  in the.** specif
3ae8a 69 63 20 67 72 61 6d 6d 61 72 20 75 73 65 64 20  ic grammar used 
3ae8b 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 2f 2a  by SQLite..*/./*
3ae8c 20 46 69 72 73 74 20 6f 66 66 2c 20 63 6f 64 65   First off, code
3ae8d 20 69 73 20 69 6e 63 6c 75 64 65 64 20 74 68 61   is included tha
3ae8e 74 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 22 69  t follows the "i
3ae8f 6e 63 6c 75 64 65 22 20 64 65 63 6c 61 72 61 74  nclude" declarat
3ae90 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 69 6e  ion.** in the in
3ae91 70 75 74 20 67 72 61 6d 6d 61 72 20 66 69 6c 65  put grammar file
3ae92 2e 20 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65  . */./* #include
3ae93 20 3c 73 74 64 69 6f 2e 68 3e 20 2a 2f 0a 0a 0a   <stdio.h> */...
3ae94 2f 2a 0a 2a 2a 20 44 69 73 61 62 6c 65 20 61 6c  /*.** Disable al
3ae95 6c 20 65 72 72 6f 72 20 72 65 63 6f 76 65 72 79  l error recovery
3ae96 20 70 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 74   processing in t
3ae97 68 65 20 70 61 72 73 65 72 20 70 75 73 68 2d 64  he parser push-d
3ae98 6f 77 6e 0a 2a 2a 20 61 75 74 6f 6d 61 74 6f 6e  own.** automaton
3ae99 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 59 59 4e  ..*/.#define YYN
3ae9a 4f 45 52 52 4f 52 52 45 43 4f 56 45 52 59 20 31  OERRORRECOVERY 1
3ae9b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 79 79 74  ../*.** Make yyt
3ae9c 65 73 74 63 61 73 65 28 29 20 74 68 65 20 73 61  estcase() the sa
3ae9d 6d 65 20 61 73 20 74 65 73 74 63 61 73 65 28 29  me as testcase()
3ae9e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 79 79 74 65  .*/.#define yyte
3ae9f 73 74 63 61 73 65 28 58 29 20 74 65 73 74 63 61  stcase(X) testca
3aea0 73 65 28 58 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  se(X)../*.** An 
3aea1 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
3aea2 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
3aea3 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
3aea4 75 74 20 74 68 65 0a 2a 2a 20 4c 49 4d 49 54 20  ut the.** LIMIT 
3aea5 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45  clause of a SELE
3aea6 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  CT statement..*/
3aea7 0a 73 74 72 75 63 74 20 4c 69 6d 69 74 56 61 6c  .struct LimitVal
3aea8 20 7b 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69   {.  Expr *pLimi
3aea9 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d  t;    /* The LIM
3aeaa 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20  IT expression.  
3aeab 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73  NULL if there is
3aeac 20 6e 6f 20 6c 69 6d 69 74 20 2a 2f 0a 20 20 45   no limit */.  E
3aead 78 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20  xpr *pOffset;   
3aeae 2f 2a 20 54 68 65 20 4f 46 46 53 45 54 20 65 78  /* The OFFSET ex
3aeaf 70 72 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20  pression.  NULL 
3aeb0 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 6e 65  if there is none
3aeb1 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
3aeb2 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
3aeb3 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75  s structure is u
3aeb4 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  sed to store the
3aeb5 20 4c 49 4b 45 2c 0a 2a 2a 20 47 4c 4f 42 2c 20   LIKE,.** GLOB, 
3aeb6 4e 4f 54 20 4c 49 4b 45 2c 20 61 6e 64 20 4e 4f  NOT LIKE, and NO
3aeb7 54 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73  T GLOB operators
3aeb8 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 69 6b 65  ..*/.struct Like
3aeb9 4f 70 20 7b 0a 20 20 54 6f 6b 65 6e 20 65 4f 70  Op {.  Token eOp
3aeba 65 72 61 74 6f 72 3b 20 20 2f 2a 20 22 6c 69 6b  erator;  /* "lik
3aebb 65 22 20 6f 72 20 22 67 6c 6f 62 22 20 6f 72 20  e" or "glob" or 
3aebc 22 72 65 67 65 78 70 22 20 2a 2f 0a 20 20 69 6e  "regexp" */.  in
3aebd 74 20 62 4e 6f 74 3b 20 20 20 20 20 20 20 20 20  t bNot;         
3aebe 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 4e  /* True if the N
3aebf 4f 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72  OT keyword is pr
3aec0 65 73 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  esent */.};../*.
3aec1 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
3aec2 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
3aec3 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69  structure descri
3aec4 62 65 73 20 74 68 65 20 65 76 65 6e 74 20 6f 66  bes the event of
3aec5 20 61 0a 2a 2a 20 54 52 49 47 47 45 52 2e 20 20   a.** TRIGGER.  
3aec6 22 61 22 20 69 73 20 74 68 65 20 65 76 65 6e 74  "a" is the event
3aec7 20 74 79 70 65 2c 20 6f 6e 65 20 6f 66 20 54 4b   type, one of TK
3aec8 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45  _UPDATE, TK_INSE
3aec9 52 54 2c 0a 2a 2a 20 54 4b 5f 44 45 4c 45 54 45  RT,.** TK_DELETE
3aeca 2c 20 6f 72 20 54 4b 5f 49 4e 53 54 45 41 44 2e  , or TK_INSTEAD.
3aecb 20 20 49 66 20 74 68 65 20 65 76 65 6e 74 20 69    If the event i
3aecc 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a  s of the form.**
3aecd 0a 2a 2a 20 20 20 20 20 20 55 50 44 41 54 45 20  .**      UPDATE 
3aece 4f 4e 20 28 61 2c 62 2c 63 29 0a 2a 2a 0a 2a 2a  ON (a,b,c).**.**
3aecf 20 54 68 65 6e 20 74 68 65 20 22 62 22 20 49 64   Then the "b" Id
3aed0 4c 69 73 74 20 72 65 63 6f 72 64 73 20 74 68 65  List records the
3aed1 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 2e 0a 2a   list "a,b,c"..*
3aed2 2f 0a 73 74 72 75 63 74 20 54 72 69 67 45 76 65  /.struct TrigEve
3aed3 6e 74 20 7b 20 69 6e 74 20 61 3b 20 49 64 4c 69  nt { int a; IdLi
3aed4 73 74 20 2a 20 62 3b 20 7d 3b 0a 0a 2f 2a 0a 2a  st * b; };../*.*
3aed5 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
3aed6 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
3aed7 68 6f 6c 64 73 20 74 68 65 20 41 54 54 41 43 48  holds the ATTACH
3aed8 20 6b 65 79 20 61 6e 64 20 74 68 65 20 6b 65 79   key and the key
3aed9 20 74 79 70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74   type..*/.struct
3aeda 20 41 74 74 61 63 68 4b 65 79 20 7b 20 69 6e 74   AttachKey { int
3aedb 20 74 79 70 65 3b 20 20 54 6f 6b 65 6e 20 6b 65   type;  Token ke
3aedc 79 3b 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 65  y; };../*.** One
3aedd 20 6f 72 20 6d 6f 72 65 20 56 41 4c 55 45 53 20   or more VALUES 
3aede 63 6c 61 75 65 73 0a 2a 2f 0a 73 74 72 75 63 74  claues.*/.struct
3aedf 20 56 61 6c 75 65 4c 69 73 74 20 7b 0a 20 20 45   ValueList {.  E
3aee0 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a  xprList *pList;.
3aee1 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
3aee2 74 3b 0a 7d 3b 0a 0a 0a 20 20 2f 2a 20 54 68 69  t;.};...  /* Thi
3aee3 73 20 69 73 20 61 20 75 74 69 6c 69 74 79 20 72  s is a utility r
3aee4 6f 75 74 69 6e 65 20 75 73 65 64 20 74 6f 20 73  outine used to s
3aee5 65 74 20 74 68 65 20 45 78 70 72 53 70 61 6e 2e  et the ExprSpan.
3aee6 7a 53 74 61 72 74 20 61 6e 64 0a 20 20 2a 2a 20  zStart and.  ** 
3aee7 45 78 70 72 53 70 61 6e 2e 7a 45 6e 64 20 76 61  ExprSpan.zEnd va
3aee8 6c 75 65 73 20 6f 66 20 70 4f 75 74 20 73 6f 20  lues of pOut so 
3aee9 74 68 61 74 20 74 68 65 20 73 70 61 6e 20 63 6f  that the span co
3aeea 76 65 72 73 20 74 68 65 20 63 6f 6d 70 6c 65 74  vers the complet
3aeeb 65 0a 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20  e.  ** range of 
3aeec 74 65 78 74 20 62 65 67 69 6e 6e 69 6e 67 20 77  text beginning w
3aeed 69 74 68 20 70 53 74 61 72 74 20 61 6e 64 20 67  ith pStart and g
3aeee 6f 69 6e 67 20 74 6f 20 74 68 65 20 65 6e 64 20  oing to the end 
3aeef 6f 66 20 70 45 6e 64 2e 0a 20 20 2a 2f 0a 20 20  of pEnd..  */.  
3aef0 73 74 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e  static void span
3aef1 53 65 74 28 45 78 70 72 53 70 61 6e 20 2a 70 4f  Set(ExprSpan *pO
3aef2 75 74 2c 20 54 6f 6b 65 6e 20 2a 70 53 74 61 72  ut, Token *pStar
3aef3 74 2c 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 29 7b  t, Token *pEnd){
3aef4 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72  .    pOut->zStar
3aef5 74 20 3d 20 70 53 74 61 72 74 2d 3e 7a 3b 0a 20  t = pStart->z;. 
3aef6 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20     pOut->zEnd = 
3aef7 26 70 45 6e 64 2d 3e 7a 5b 70 45 6e 64 2d 3e 6e  &pEnd->z[pEnd->n
3aef8 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e  ];.  }..  /* Con
3aef9 73 74 72 75 63 74 20 61 20 6e 65 77 20 45 78 70  struct a new Exp
3aefa 72 20 6f 62 6a 65 63 74 20 66 72 6f 6d 20 61 20  r object from a 
3aefb 73 69 6e 67 6c 65 20 69 64 65 6e 74 69 66 69 65  single identifie
3aefc 72 2e 20 20 55 73 65 20 74 68 65 0a 20 20 2a 2a  r.  Use the.  **
3aefd 20 6e 65 77 20 45 78 70 72 20 74 6f 20 70 6f 70   new Expr to pop
3aefe 75 6c 61 74 65 20 70 4f 75 74 2e 20 20 53 65 74  ulate pOut.  Set
3aeff 20 74 68 65 20 73 70 61 6e 20 6f 66 20 70 4f 75   the span of pOu
3af00 74 20 74 6f 20 62 65 20 74 68 65 20 69 64 65 6e  t to be the iden
3af01 74 69 66 69 65 72 0a 20 20 2a 2a 20 74 68 61 74  tifier.  ** that
3af02 20 63 72 65 61 74 65 64 20 74 68 65 20 65 78 70   created the exp
3af03 72 65 73 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  ression..  */.  
3af04 73 74 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e  static void span
3af05 45 78 70 72 28 45 78 70 72 53 70 61 6e 20 2a 70  Expr(ExprSpan *p
3af06 4f 75 74 2c 20 50 61 72 73 65 20 2a 70 50 61 72  Out, Parse *pPar
3af07 73 65 2c 20 69 6e 74 20 6f 70 2c 20 54 6f 6b 65  se, int op, Toke
3af08 6e 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 20 20  n *pValue){.    
3af09 70 4f 75 74 2d 3e 70 45 78 70 72 20 3d 20 73 71  pOut->pExpr = sq
3af0a 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
3af0b 65 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 70 56 61  e, op, 0, 0, pVa
3af0c 6c 75 65 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e  lue);.    pOut->
3af0d 7a 53 74 61 72 74 20 3d 20 70 56 61 6c 75 65 2d  zStart = pValue-
3af0e 3e 7a 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45  >z;.    pOut->zE
3af0f 6e 64 20 3d 20 26 70 56 61 6c 75 65 2d 3e 7a 5b  nd = &pValue->z[
3af10 70 56 61 6c 75 65 2d 3e 6e 5d 3b 0a 20 20 7d 0a  pValue->n];.  }.
3af11 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69  .  /* This routi
3af12 6e 65 20 63 6f 6e 73 74 72 75 63 74 73 20 61 20  ne constructs a 
3af13 62 69 6e 61 72 79 20 65 78 70 72 65 73 73 69 6f  binary expressio
3af14 6e 20 6e 6f 64 65 20 6f 75 74 20 6f 66 20 74 77  n node out of tw
3af15 6f 20 45 78 70 72 53 70 61 6e 0a 20 20 2a 2a 20  o ExprSpan.  ** 
3af16 6f 62 6a 65 63 74 73 20 61 6e 64 20 75 73 65 73  objects and uses
3af17 20 74 68 65 20 72 65 73 75 6c 74 20 74 6f 20 70   the result to p
3af18 6f 70 75 6c 61 74 65 20 61 20 6e 65 77 20 45 78  opulate a new Ex
3af19 70 72 53 70 61 6e 20 6f 62 6a 65 63 74 2e 0a 20  prSpan object.. 
3af1a 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69   */.  static voi
3af1b 64 20 73 70 61 6e 42 69 6e 61 72 79 45 78 70 72  d spanBinaryExpr
3af1c 28 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a  (.    ExprSpan *
3af1d 70 4f 75 74 2c 20 20 20 20 20 2f 2a 20 57 72 69  pOut,     /* Wri
3af1e 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 68 65  te the result he
3af1f 72 65 20 2a 2f 0a 20 20 20 20 50 61 72 73 65 20  re */.    Parse 
3af20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
3af21 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e   The parsing con
3af22 74 65 78 74 2e 20 20 45 72 72 6f 72 73 20 61 63  text.  Errors ac
3af23 63 75 6d 75 6c 61 74 65 20 68 65 72 65 20 2a 2f  cumulate here */
3af24 0a 20 20 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  .    int op,    
3af25 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3af26 62 69 6e 61 72 79 20 6f 70 65 72 61 74 69 6f 6e  binary operation
3af27 20 2a 2f 0a 20 20 20 20 45 78 70 72 53 70 61 6e   */.    ExprSpan
3af28 20 2a 70 4c 65 66 74 2c 20 20 20 20 2f 2a 20 54   *pLeft,    /* T
3af29 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  he left operand 
3af2a 2a 2f 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20  */.    ExprSpan 
3af2b 2a 70 52 69 67 68 74 20 20 20 20 2f 2a 20 54 68  *pRight    /* Th
3af2c 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  e right operand 
3af2d 2a 2f 0a 20 20 29 7b 0a 20 20 20 20 70 4f 75 74  */.  ){.    pOut
3af2e 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  ->pExpr = sqlite
3af2f 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 6f  3PExpr(pParse, o
3af30 70 2c 20 70 4c 65 66 74 2d 3e 70 45 78 70 72 2c  p, pLeft->pExpr,
3af31 20 70 52 69 67 68 74 2d 3e 70 45 78 70 72 2c 20   pRight->pExpr, 
3af32 30 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53  0);.    pOut->zS
3af33 74 61 72 74 20 3d 20 70 4c 65 66 74 2d 3e 7a 53  tart = pLeft->zS
3af34 74 61 72 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e  tart;.    pOut->
3af35 7a 45 6e 64 20 3d 20 70 52 69 67 68 74 2d 3e 7a  zEnd = pRight->z
3af36 45 6e 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  End;.  }..  /* C
3af37 6f 6e 73 74 72 75 63 74 20 61 6e 20 65 78 70 72  onstruct an expr
3af38 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20  ession node for 
3af39 61 20 75 6e 61 72 79 20 70 6f 73 74 66 69 78 20  a unary postfix 
3af3a 6f 70 65 72 61 74 6f 72 0a 20 20 2a 2f 0a 20 20  operator.  */.  
3af3b 73 74 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e  static void span
3af3c 55 6e 61 72 79 50 6f 73 74 66 69 78 28 0a 20 20  UnaryPostfix(.  
3af3d 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74    ExprSpan *pOut
3af3e 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74  ,        /* Writ
3af3f 65 20 74 68 65 20 6e 65 77 20 65 78 70 72 65 73  e the new expres
3af40 73 69 6f 6e 20 6e 6f 64 65 20 68 65 72 65 20 2a  sion node here *
3af41 2f 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 61  /.    Parse *pPa
3af42 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rse,         /* 
3af43 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
3af44 74 6f 20 72 65 63 6f 72 64 20 65 72 72 6f 72 73  to record errors
3af45 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 70 2c 20   */.    int op, 
3af46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3af47 2a 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20 2a  * The operator *
3af48 2f 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a  /.    ExprSpan *
3af49 70 4f 70 65 72 61 6e 64 2c 20 20 20 20 2f 2a 20  pOperand,    /* 
3af4a 54 68 65 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  The operand */. 
3af4b 20 20 20 54 6f 6b 65 6e 20 2a 70 50 6f 73 74 4f     Token *pPostO
3af4c 70 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  p         /* The
3af4d 20 6f 70 65 72 61 6e 64 20 74 6f 6b 65 6e 20 66   operand token f
3af4e 6f 72 20 73 65 74 74 69 6e 67 20 74 68 65 20 73  or setting the s
3af4f 70 61 6e 20 2a 2f 0a 20 20 29 7b 0a 20 20 20 20  pan */.  ){.    
3af50 70 4f 75 74 2d 3e 70 45 78 70 72 20 3d 20 73 71  pOut->pExpr = sq
3af51 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
3af52 65 2c 20 6f 70 2c 20 70 4f 70 65 72 61 6e 64 2d  e, op, pOperand-
3af53 3e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20  >pExpr, 0, 0);. 
3af54 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20     pOut->zStart 
3af55 3d 20 70 4f 70 65 72 61 6e 64 2d 3e 7a 53 74 61  = pOperand->zSta
3af56 72 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45  rt;.    pOut->zE
3af57 6e 64 20 3d 20 26 70 50 6f 73 74 4f 70 2d 3e 7a  nd = &pPostOp->z
3af58 5b 70 50 6f 73 74 4f 70 2d 3e 6e 5d 3b 0a 20 20  [pPostOp->n];.  
3af59 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }               
3af5a 20 20 20 20 20 20 20 20 20 20 20 20 0a 0a 20 20              ..  
3af5b 2f 2a 20 41 20 72 6f 75 74 69 6e 65 20 74 6f 20  /* A routine to 
3af5c 63 6f 6e 76 65 72 74 20 61 20 62 69 6e 61 72 79  convert a binary
3af5d 20 54 4b 5f 49 53 20 6f 72 20 54 4b 5f 49 53 4e   TK_IS or TK_ISN
3af5e 4f 54 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  OT expression in
3af5f 74 6f 20 61 0a 20 20 2a 2a 20 75 6e 61 72 79 20  to a.  ** unary 
3af60 54 4b 5f 49 53 4e 55 4c 4c 20 6f 72 20 54 4b 5f  TK_ISNULL or TK_
3af61 4e 4f 54 4e 55 4c 4c 20 65 78 70 72 65 73 73 69  NOTNULL expressi
3af62 6f 6e 2e 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  on. */.  static 
3af63 76 6f 69 64 20 62 69 6e 61 72 79 54 6f 55 6e 61  void binaryToUna
3af64 72 79 49 66 4e 75 6c 6c 28 50 61 72 73 65 20 2a  ryIfNull(Parse *
3af65 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 59  pParse, Expr *pY
3af66 2c 20 45 78 70 72 20 2a 70 41 2c 20 69 6e 74 20  , Expr *pA, int 
3af67 6f 70 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  op){.    sqlite3
3af68 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
3af69 62 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  b;.    if( db->m
3af6a 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 26  allocFailed==0 &
3af6b 26 20 70 59 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c  & pY->op==TK_NUL
3af6c 4c 20 29 7b 0a 20 20 20 20 20 20 70 41 2d 3e 6f  L ){.      pA->o
3af6d 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 20 20  p = (u8)op;.    
3af6e 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
3af6f 65 74 65 28 64 62 2c 20 70 41 2d 3e 70 52 69 67  ete(db, pA->pRig
3af70 68 74 29 3b 0a 20 20 20 20 20 20 70 41 2d 3e 70  ht);.      pA->p
3af71 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 7d  Right = 0;.    }
3af72 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74  .  }..  /* Const
3af73 72 75 63 74 20 61 6e 20 65 78 70 72 65 73 73 69  ruct an expressi
3af74 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20 75 6e  on node for a un
3af75 61 72 79 20 70 72 65 66 69 78 20 6f 70 65 72 61  ary prefix opera
3af76 74 6f 72 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69  tor.  */.  stati
3af77 63 20 76 6f 69 64 20 73 70 61 6e 55 6e 61 72 79  c void spanUnary
3af78 50 72 65 66 69 78 28 0a 20 20 20 20 45 78 70 72  Prefix(.    Expr
3af79 53 70 61 6e 20 2a 70 4f 75 74 2c 20 20 20 20 20  Span *pOut,     
3af7a 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
3af7b 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6e  new expression n
3af7c 6f 64 65 20 68 65 72 65 20 2a 2f 0a 20 20 20 20  ode here */.    
3af7d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
3af7e 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
3af7f 67 20 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 63  g context to rec
3af80 6f 72 64 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  ord errors */.  
3af81 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20    int op,       
3af82 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3af83 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20  operator */.    
3af84 45 78 70 72 53 70 61 6e 20 2a 70 4f 70 65 72 61  ExprSpan *pOpera
3af85 6e 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6f 70  nd,    /* The op
3af86 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 54 6f 6b  erand */.    Tok
3af87 65 6e 20 2a 70 50 72 65 4f 70 20 20 20 20 20 20  en *pPreOp      
3af88 20 20 20 2f 2a 20 54 68 65 20 6f 70 65 72 61 6e     /* The operan
3af89 64 20 74 6f 6b 65 6e 20 66 6f 72 20 73 65 74 74  d token for sett
3af8a 69 6e 67 20 74 68 65 20 73 70 61 6e 20 2a 2f 0a  ing the span */.
3af8b 20 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 70    ){.    pOut->p
3af8c 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
3af8d 78 70 72 28 70 50 61 72 73 65 2c 20 6f 70 2c 20  xpr(pParse, op, 
3af8e 70 4f 70 65 72 61 6e 64 2d 3e 70 45 78 70 72 2c  pOperand->pExpr,
3af8f 20 30 2c 20 30 29 3b 0a 20 20 20 20 70 4f 75 74   0, 0);.    pOut
3af90 2d 3e 7a 53 74 61 72 74 20 3d 20 70 50 72 65 4f  ->zStart = pPreO
3af91 70 2d 3e 7a 3b 0a 20 20 20 20 70 4f 75 74 2d 3e  p->z;.    pOut->
3af92 7a 45 6e 64 20 3d 20 70 4f 70 65 72 61 6e 64 2d  zEnd = pOperand-
3af93 3e 7a 45 6e 64 3b 0a 20 20 7d 0a 2f 2a 20 4e 65  >zEnd;.  }./* Ne
3af94 78 74 20 69 73 20 61 6c 6c 20 74 6f 6b 65 6e 20  xt is all token 
3af95 76 61 6c 75 65 73 2c 20 69 6e 20 61 20 66 6f 72  values, in a for
3af96 6d 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75  m suitable for u
3af97 73 65 20 62 79 20 6d 61 6b 65 68 65 61 64 65 72  se by makeheader
3af98 73 2e 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69  s..** This secti
3af99 6f 6e 20 77 69 6c 6c 20 62 65 20 6e 75 6c 6c 20  on will be null 
3af9a 75 6e 6c 65 73 73 20 6c 65 6d 6f 6e 20 69 73 20  unless lemon is 
3af9b 72 75 6e 20 77 69 74 68 20 74 68 65 20 2d 6d 20  run with the -m 
3af9c 73 77 69 74 63 68 2e 0a 2a 2f 0a 2f 2a 20 0a 2a  switch..*/./* .*
3af9d 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74  * These constant
3af9e 73 20 28 61 6c 6c 20 67 65 6e 65 72 61 74 65 64  s (all generated
3af9f 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62   automatically b
3afa0 79 20 74 68 65 20 70 61 72 73 65 72 20 67 65 6e  y the parser gen
3afa1 65 72 61 74 6f 72 29 0a 2a 2a 20 73 70 65 63 69  erator).** speci
3afa2 66 79 20 74 68 65 20 76 61 72 69 6f 75 73 20 6b  fy the various k
3afa3 69 6e 64 73 20 6f 66 20 74 6f 6b 65 6e 73 20 28  inds of tokens (
3afa4 74 65 72 6d 69 6e 61 6c 73 29 20 74 68 61 74 20  terminals) that 
3afa5 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 75 6e  the parser.** un
3afa6 64 65 72 73 74 61 6e 64 73 2e 20 0a 2a 2a 0a 2a  derstands. .**.*
3afa7 2a 20 45 61 63 68 20 73 79 6d 62 6f 6c 20 68 65  * Each symbol he
3afa8 72 65 20 69 73 20 61 20 74 65 72 6d 69 6e 61 6c  re is a terminal
3afa9 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 65 20 67   symbol in the g
3afaa 72 61 6d 6d 61 72 2e 0a 2a 2f 0a 2f 2a 20 4d 61  rammar..*/./* Ma
3afab 6b 65 20 73 75 72 65 20 74 68 65 20 49 4e 54 45  ke sure the INTE
3afac 52 46 41 43 45 20 6d 61 63 72 6f 20 69 73 20 64  RFACE macro is d
3afad 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64  efined..*/.#ifnd
3afae 65 66 20 49 4e 54 45 52 46 41 43 45 0a 23 20 64  ef INTERFACE.# d
3afaf 65 66 69 6e 65 20 49 4e 54 45 52 46 41 43 45 20  efine INTERFACE 
3afb0 31 0a 23 65 6e 64 69 66 0a 2f 2a 20 54 68 65 20  1.#endif./* The 
3afb1 6e 65 78 74 20 74 68 69 6e 67 20 69 6e 63 6c 75  next thing inclu
3afb2 64 65 64 20 69 73 20 73 65 72 69 65 73 20 6f 66  ded is series of
3afb3 20 64 65 66 69 6e 65 73 20 77 68 69 63 68 20 63   defines which c
3afb4 6f 6e 74 72 6f 6c 0a 2a 2a 20 76 61 72 69 6f 75  ontrol.** variou
3afb5 73 20 61 73 70 65 63 74 73 20 6f 66 20 74 68 65  s aspects of the
3afb6 20 67 65 6e 65 72 61 74 65 64 20 70 61 72 73 65   generated parse
3afb7 72 2e 0a 2a 2a 20 20 20 20 59 59 43 4f 44 45 54  r..**    YYCODET
3afb8 59 50 45 20 20 20 20 20 20 20 20 20 69 73 20 74  YPE         is t
3afb9 68 65 20 64 61 74 61 20 74 79 70 65 20 75 73 65  he data type use
3afba 64 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65  d for storing te
3afbb 72 6d 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20  rminal.**       
3afbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3afbd 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20  and nonterminal 
3afbe 6e 75 6d 62 65 72 73 2e 20 20 22 75 6e 73 69 67  numbers.  "unsig
3afbf 6e 65 64 20 63 68 61 72 22 20 69 73 0a 2a 2a 20  ned char" is.** 
3afc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3afc1 20 20 20 20 20 20 75 73 65 64 20 69 66 20 74 68        used if th
3afc2 65 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68  ere are fewer th
3afc3 61 6e 20 32 35 30 20 74 65 72 6d 69 6e 61 6c 73  an 250 terminals
3afc4 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3afc5 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 6e 6f            and no
3afc6 6e 74 65 72 6d 69 6e 61 6c 73 2e 20 20 22 69 6e  nterminals.  "in
3afc7 74 22 20 69 73 20 75 73 65 64 20 6f 74 68 65 72  t" is used other
3afc8 77 69 73 65 2e 0a 2a 2a 20 20 20 20 59 59 4e 4f  wise..**    YYNO
3afc9 43 4f 44 45 20 20 20 20 20 20 20 20 20 20 20 69  CODE           i
3afca 73 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 74 79  s a number of ty
3afcb 70 65 20 59 59 43 4f 44 45 54 59 50 45 20 77 68  pe YYCODETYPE wh
3afcc 69 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 0a  ich corresponds.
3afcd 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
3afce 20 20 20 20 20 20 20 20 20 74 6f 20 6e 6f 20 6c           to no l
3afcf 65 67 61 6c 20 74 65 72 6d 69 6e 61 6c 20 6f 72  egal terminal or
3afd0 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e 75 6d   nonterminal num
3afd1 62 65 72 2e 20 20 54 68 69 73 0a 2a 2a 20 20 20  ber.  This.**   
3afd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3afd3 20 20 20 20 6e 75 6d 62 65 72 20 69 73 20 75 73      number is us
3afd4 65 64 20 74 6f 20 66 69 6c 6c 20 69 6e 20 65 6d  ed to fill in em
3afd5 70 74 79 20 73 6c 6f 74 73 20 6f 66 20 74 68 65  pty slots of the
3afd6 20 68 61 73 68 20 0a 2a 2a 20 20 20 20 20 20 20   hash .**       
3afd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3afd8 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 59 59 46  table..**    YYF
3afd9 41 4c 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20  ALLBACK         
3afda 49 66 20 64 65 66 69 6e 65 64 2c 20 74 68 69 73  If defined, this
3afdb 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
3afdc 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65  one or more toke
3afdd 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ns.**           
3afde 20 20 20 20 20 20 20 20 20 20 20 20 68 61 76 65              have
3afdf 20 66 61 6c 6c 2d 62 61 63 6b 20 76 61 6c 75 65   fall-back value
3afe0 73 20 77 68 69 63 68 20 73 68 6f 75 6c 64 20 62  s which should b
3afe1 65 20 75 73 65 64 20 69 66 20 74 68 65 0a 2a 2a  e used if the.**
3afe2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3afe3 20 20 20 20 20 20 20 6f 72 69 67 69 6e 61 6c 20         original 
3afe4 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b  value of the tok
3afe5 65 6e 20 77 69 6c 6c 20 6e 6f 74 20 70 61 72 73  en will not pars
3afe6 65 2e 0a 2a 2a 20 20 20 20 59 59 41 43 54 49 4f  e..**    YYACTIO
3afe7 4e 54 59 50 45 20 20 20 20 20 20 20 69 73 20 74  NTYPE       is t
3afe8 68 65 20 64 61 74 61 20 74 79 70 65 20 75 73 65  he data type use
3afe9 64 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65  d for storing te
3afea 72 6d 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20  rminal.**       
3afeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3afec 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20  and nonterminal 
3afed 6e 75 6d 62 65 72 73 2e 20 20 22 75 6e 73 69 67  numbers.  "unsig
3afee 6e 65 64 20 63 68 61 72 22 20 69 73 0a 2a 2a 20  ned char" is.** 
3afef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3aff0 20 20 20 20 20 20 75 73 65 64 20 69 66 20 74 68        used if th
3aff1 65 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68  ere are fewer th
3aff2 61 6e 20 32 35 30 20 72 75 6c 65 73 20 61 6e 64  an 250 rules and
3aff3 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3aff4 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 73            states
3aff5 20 63 6f 6d 62 69 6e 65 64 2e 20 20 22 69 6e 74   combined.  "int
3aff6 22 20 69 73 20 75 73 65 64 20 6f 74 68 65 72 77  " is used otherw
3aff7 69 73 65 2e 0a 2a 2a 20 20 20 20 73 71 6c 69 74  ise..**    sqlit
3aff8 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50  e3ParserTOKENTYP
3aff9 45 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74  E     is the dat
3affa 61 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20  a type used for 
3affb 6d 69 6e 6f 72 20 74 6f 6b 65 6e 73 20 67 69 76  minor tokens giv
3affc 65 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  en .**          
3affd 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69 72               dir
3affe 65 63 74 6c 79 20 74 6f 20 74 68 65 20 70 61 72  ectly to the par
3afff 73 65 72 20 66 72 6f 6d 20 74 68 65 20 74 6f 6b  ser from the tok
3b000 65 6e 69 7a 65 72 2e 0a 2a 2a 20 20 20 20 59 59  enizer..**    YY
3b001 4d 49 4e 4f 52 54 59 50 45 20 20 20 20 20 20 20  MINORTYPE       
3b002 20 69 73 20 74 68 65 20 64 61 74 61 20 74 79 70   is the data typ
3b003 65 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 6d  e used for all m
3b004 69 6e 6f 72 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 20  inor tokens..** 
3b005 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b006 20 20 20 20 20 20 54 68 69 73 20 69 73 20 74 79        This is ty
3b007 70 69 63 61 6c 6c 79 20 61 20 75 6e 69 6f 6e 20  pically a union 
3b008 6f 66 20 6d 61 6e 79 20 74 79 70 65 73 2c 20 6f  of many types, o
3b009 6e 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  ne of.**        
3b00a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77                 w
3b00b 68 69 63 68 20 69 73 20 73 71 6c 69 74 65 33 50  hich is sqlite3P
3b00c 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 2e 20  arserTOKENTYPE. 
3b00d 20 54 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68   The entry in th
3b00e 65 20 75 6e 69 6f 6e 0a 2a 2a 20 20 20 20 20 20  e union.**      
3b00f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b010 20 66 6f 72 20 62 61 73 65 20 74 6f 6b 65 6e 73   for base tokens
3b011 20 69 73 20 63 61 6c 6c 65 64 20 22 79 79 30 22   is called "yy0"
3b012 2e 0a 2a 2a 20 20 20 20 59 59 53 54 41 43 4b 44  ..**    YYSTACKD
3b013 45 50 54 48 20 20 20 20 20 20 20 69 73 20 74 68  EPTH       is th
3b014 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20  e maximum depth 
3b015 6f 66 20 74 68 65 20 70 61 72 73 65 72 27 73 20  of the parser's 
3b016 73 74 61 63 6b 2e 20 20 49 66 0a 2a 2a 20 20 20  stack.  If.**   
3b017 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b018 20 20 20 20 7a 65 72 6f 20 74 68 65 20 73 74 61      zero the sta
3b019 63 6b 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c  ck is dynamicall
3b01a 79 20 73 69 7a 65 64 20 75 73 69 6e 67 20 72 65  y sized using re
3b01b 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 73 71  alloc().**    sq
3b01c 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53  lite3ParserARG_S
3b01d 44 45 43 4c 20 20 20 20 20 41 20 73 74 61 74 69  DECL     A stati
3b01e 63 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c 61  c variable decla
3b01f 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 25  ration for the %
3b020 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 0a 2a  extra_argument.*
3b021 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73  *    sqlite3Pars
3b022 65 72 41 52 47 5f 50 44 45 43 4c 20 20 20 20 20  erARG_PDECL     
3b023 41 20 70 61 72 61 6d 65 74 65 72 20 64 65 63 6c  A parameter decl
3b024 61 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  aration for the 
3b025 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 0a  %extra_argument.
3b026 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72  **    sqlite3Par
3b027 73 65 72 41 52 47 5f 53 54 4f 52 45 20 20 20 20  serARG_STORE    
3b028 20 43 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 25   Code to store %
3b029 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 69  extra_argument i
3b02a 6e 74 6f 20 79 79 70 50 61 72 73 65 72 0a 2a 2a  nto yypParser.**
3b02b 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
3b02c 72 41 52 47 5f 46 45 54 43 48 20 20 20 20 20 43  rARG_FETCH     C
3b02d 6f 64 65 20 74 6f 20 65 78 74 72 61 63 74 20 25  ode to extract %
3b02e 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 66  extra_argument f
3b02f 72 6f 6d 20 79 79 70 50 61 72 73 65 72 0a 2a 2a  rom yypParser.**
3b030 20 20 20 20 59 59 4e 53 54 41 54 45 20 20 20 20      YYNSTATE    
3b031 20 20 20 20 20 20 20 74 68 65 20 63 6f 6d 62 69         the combi
3b032 6e 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 73 74  ned number of st
3b033 61 74 65 73 2e 0a 2a 2a 20 20 20 20 59 59 4e 52  ates..**    YYNR
3b034 55 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 74  ULE            t
3b035 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 75 6c  he number of rul
3b036 65 73 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61  es in the gramma
3b037 72 0a 2a 2a 20 20 20 20 59 59 45 52 52 4f 52 53  r.**    YYERRORS
3b038 59 4d 42 4f 4c 20 20 20 20 20 20 69 73 20 74 68  YMBOL      is th
3b039 65 20 63 6f 64 65 20 6e 75 6d 62 65 72 20 6f 66  e code number of
3b03a 20 74 68 65 20 65 72 72 6f 72 20 73 79 6d 62 6f   the error symbo
3b03b 6c 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 20 20  l.  If not.**   
3b03c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b03d 20 20 20 20 64 65 66 69 6e 65 64 2c 20 74 68 65      defined, the
3b03e 6e 20 64 6f 20 6e 6f 20 65 72 72 6f 72 20 70 72  n do no error pr
3b03f 6f 63 65 73 73 69 6e 67 2e 0a 2a 2f 0a 23 64 65  ocessing..*/.#de
3b040 66 69 6e 65 20 59 59 43 4f 44 45 54 59 50 45 20  fine YYCODETYPE 
3b041 75 6e 73 69 67 6e 65 64 20 63 68 61 72 0a 23 64  unsigned char.#d
3b042 65 66 69 6e 65 20 59 59 4e 4f 43 4f 44 45 20 32  efine YYNOCODE 2
3b043 35 31 0a 23 64 65 66 69 6e 65 20 59 59 41 43 54  51.#define YYACT
3b044 49 4f 4e 54 59 50 45 20 75 6e 73 69 67 6e 65 64  IONTYPE unsigned
3b045 20 73 68 6f 72 74 20 69 6e 74 0a 23 64 65 66 69   short int.#defi
3b046 6e 65 20 59 59 57 49 4c 44 43 41 52 44 20 36 37  ne YYWILDCARD 67
3b047 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
3b048 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20  ParserTOKENTYPE 
3b049 54 6f 6b 65 6e 0a 74 79 70 65 64 65 66 20 75 6e  Token.typedef un
3b04a 69 6f 6e 20 7b 0a 20 20 69 6e 74 20 79 79 69 6e  ion {.  int yyin
3b04b 69 74 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 72  it;.  sqlite3Par
3b04c 73 65 72 54 4f 4b 45 4e 54 59 50 45 20 79 79 30  serTOKENTYPE yy0
3b04d 3b 0a 20 20 73 74 72 75 63 74 20 4c 69 6d 69 74  ;.  struct Limit
3b04e 56 61 6c 20 79 79 36 34 3b 0a 20 20 45 78 70 72  Val yy64;.  Expr
3b04f 2a 20 79 79 31 32 32 3b 0a 20 20 53 65 6c 65 63  * yy122;.  Selec
3b050 74 2a 20 79 79 31 35 39 3b 0a 20 20 49 64 4c 69  t* yy159;.  IdLi
3b051 73 74 2a 20 79 79 31 38 30 3b 0a 20 20 73 74 72  st* yy180;.  str
3b052 75 63 74 20 7b 69 6e 74 20 76 61 6c 75 65 3b 20  uct {int value; 
3b053 69 6e 74 20 6d 61 73 6b 3b 7d 20 79 79 32 30 37  int mask;} yy207
3b054 3b 0a 20 20 75 38 20 79 79 32 35 38 3b 0a 20 20  ;.  u8 yy258;.  
3b055 73 74 72 75 63 74 20 4c 69 6b 65 4f 70 20 79 79  struct LikeOp yy
3b056 33 31 38 3b 0a 20 20 54 72 69 67 67 65 72 53 74  318;.  TriggerSt
3b057 65 70 2a 20 79 79 33 32 37 3b 0a 20 20 45 78 70  ep* yy327;.  Exp
3b058 72 53 70 61 6e 20 79 79 33 34 32 3b 0a 20 20 53  rSpan yy342;.  S
3b059 72 63 4c 69 73 74 2a 20 79 79 33 34 37 3b 0a 20  rcList* yy347;. 
3b05a 20 69 6e 74 20 79 79 33 39 32 3b 0a 20 20 73 74   int yy392;.  st
3b05b 72 75 63 74 20 54 72 69 67 45 76 65 6e 74 20 79  ruct TrigEvent y
3b05c 79 34 31 30 3b 0a 20 20 45 78 70 72 4c 69 73 74  y410;.  ExprList
3b05d 2a 20 79 79 34 34 32 3b 0a 20 20 73 74 72 75 63  * yy442;.  struc
3b05e 74 20 56 61 6c 75 65 4c 69 73 74 20 79 79 34 38  t ValueList yy48
3b05f 37 3b 0a 7d 20 59 59 4d 49 4e 4f 52 54 59 50 45  7;.} YYMINORTYPE
3b060 3b 0a 23 69 66 6e 64 65 66 20 59 59 53 54 41 43  ;.#ifndef YYSTAC
3b061 4b 44 45 50 54 48 0a 23 64 65 66 69 6e 65 20 59  KDEPTH.#define Y
3b062 59 53 54 41 43 4b 44 45 50 54 48 20 31 30 30 0a  YSTACKDEPTH 100.
3b063 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 73  #endif.#define s
3b064 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
3b065 53 44 45 43 4c 20 50 61 72 73 65 20 2a 70 50 61  SDECL Parse *pPa
3b066 72 73 65 3b 0a 23 64 65 66 69 6e 65 20 73 71 6c  rse;.#define sql
3b067 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 50 44  ite3ParserARG_PD
3b068 45 43 4c 20 2c 50 61 72 73 65 20 2a 70 50 61 72  ECL ,Parse *pPar
3b069 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  se.#define sqlit
3b06a 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43  e3ParserARG_FETC
3b06b 48 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20  H Parse *pParse 
3b06c 3d 20 79 79 70 50 61 72 73 65 72 2d 3e 70 50 61  = yypParser->pPa
3b06d 72 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  rse.#define sqli
3b06e 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f  te3ParserARG_STO
3b06f 52 45 20 79 79 70 50 61 72 73 65 72 2d 3e 70 50  RE yypParser->pP
3b070 61 72 73 65 20 3d 20 70 50 61 72 73 65 0a 23 64  arse = pParse.#d
3b071 65 66 69 6e 65 20 59 59 4e 53 54 41 54 45 20 36  efine YYNSTATE 6
3b072 32 37 0a 23 64 65 66 69 6e 65 20 59 59 4e 52 55  27.#define YYNRU
3b073 4c 45 20 33 32 37 0a 23 64 65 66 69 6e 65 20 59  LE 327.#define Y
3b074 59 46 41 4c 4c 42 41 43 4b 20 31 0a 23 64 65 66  YFALLBACK 1.#def
3b075 69 6e 65 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e  ine YY_NO_ACTION
3b076 20 20 20 20 20 20 28 59 59 4e 53 54 41 54 45 2b        (YYNSTATE+
3b077 59 59 4e 52 55 4c 45 2b 32 29 0a 23 64 65 66 69  YYNRULE+2).#defi
3b078 6e 65 20 59 59 5f 41 43 43 45 50 54 5f 41 43 54  ne YY_ACCEPT_ACT
3b079 49 4f 4e 20 20 28 59 59 4e 53 54 41 54 45 2b 59  ION  (YYNSTATE+Y
3b07a 59 4e 52 55 4c 45 2b 31 29 0a 23 64 65 66 69 6e  YNRULE+1).#defin
3b07b 65 20 59 59 5f 45 52 52 4f 52 5f 41 43 54 49 4f  e YY_ERROR_ACTIO
3b07c 4e 20 20 20 28 59 59 4e 53 54 41 54 45 2b 59 59  N   (YYNSTATE+YY
3b07d 4e 52 55 4c 45 29 0a 0a 2f 2a 20 54 68 65 20 79  NRULE)../* The y
3b07e 79 7a 65 72 6f 6d 69 6e 6f 72 20 63 6f 6e 73 74  yzerominor const
3b07f 61 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 69  ant is used to i
3b080 6e 69 74 69 61 6c 69 7a 65 20 69 6e 73 74 61 6e  nitialize instan
3b081 63 65 73 20 6f 66 0a 2a 2a 20 59 59 4d 49 4e 4f  ces of.** YYMINO
3b082 52 54 59 50 45 20 6f 62 6a 65 63 74 73 20 74 6f  RTYPE objects to
3b083 20 7a 65 72 6f 2e 20 2a 2f 0a 73 74 61 74 69 63   zero. */.static
3b084 20 63 6f 6e 73 74 20 59 59 4d 49 4e 4f 52 54 59   const YYMINORTY
3b085 50 45 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 20 3d  PE yyzerominor =
3b086 20 7b 20 30 20 7d 3b 0a 0a 2f 2a 20 44 65 66 69   { 0 };../* Defi
3b087 6e 65 20 74 68 65 20 79 79 74 65 73 74 63 61 73  ne the yytestcas
3b088 65 28 29 20 6d 61 63 72 6f 20 74 6f 20 62 65 20  e() macro to be 
3b089 61 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20 6e 6f  a no-op if is no
3b08a 74 20 61 6c 72 65 61 64 79 20 64 65 66 69 6e 65  t already define
3b08b 64 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 2e 0a  d.** otherwise..
3b08c 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f  **.** Applicatio
3b08d 6e 73 20 63 61 6e 20 63 68 6f 6f 73 65 20 74 6f  ns can choose to
3b08e 20 64 65 66 69 6e 65 20 79 79 74 65 73 74 63 61   define yytestca
3b08f 73 65 28 29 20 69 6e 20 74 68 65 20 25 69 6e 63  se() in the %inc
3b090 6c 75 64 65 20 73 65 63 74 69 6f 6e 0a 2a 2a 20  lude section.** 
3b091 74 6f 20 61 20 6d 61 63 72 6f 20 74 68 61 74 20  to a macro that 
3b092 63 61 6e 20 61 73 73 69 73 74 20 69 6e 20 76 65  can assist in ve
3b093 72 69 66 79 69 6e 67 20 63 6f 64 65 20 63 6f 76  rifying code cov
3b094 65 72 61 67 65 2e 20 20 46 6f 72 20 70 72 6f 64  erage.  For prod
3b095 75 63 74 69 6f 6e 0a 2a 2a 20 63 6f 64 65 20 74  uction.** code t
3b096 68 65 20 79 79 74 65 73 74 63 61 73 65 28 29 20  he yytestcase() 
3b097 6d 61 63 72 6f 20 73 68 6f 75 6c 64 20 62 65 20  macro should be 
3b098 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 42 75 74  turned off.  But
3b099 20 69 74 20 69 73 20 75 73 65 66 75 6c 0a 2a 2a   it is useful.**
3b09a 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f   for testing..*/
3b09b 0a 23 69 66 6e 64 65 66 20 79 79 74 65 73 74 63  .#ifndef yytestc
3b09c 61 73 65 0a 23 20 64 65 66 69 6e 65 20 79 79 74  ase.# define yyt
3b09d 65 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69  estcase(X).#endi
3b09e 66 0a 0a 0a 2f 2a 20 4e 65 78 74 20 61 72 65 20  f.../* Next are 
3b09f 74 68 65 20 74 61 62 6c 65 73 20 75 73 65 64 20  the tables used 
3b0a0 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61  to determine wha
3b0a1 74 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65  t action to take
3b0a2 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a   based on the.**
3b0a3 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 61   current state a
3b0a4 6e 64 20 6c 6f 6f 6b 61 68 65 61 64 20 74 6f 6b  nd lookahead tok
3b0a5 65 6e 2e 20 20 54 68 65 73 65 20 74 61 62 6c 65  en.  These table
3b0a6 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d  s are used to im
3b0a7 70 6c 65 6d 65 6e 74 0a 2a 2a 20 66 75 6e 63 74  plement.** funct
3b0a8 69 6f 6e 73 20 74 68 61 74 20 74 61 6b 65 20 61  ions that take a
3b0a9 20 73 74 61 74 65 20 6e 75 6d 62 65 72 20 61 6e   state number an
3b0aa 64 20 6c 6f 6f 6b 61 68 65 61 64 20 76 61 6c 75  d lookahead valu
3b0ab 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 0a  e and return an.
3b0ac 2a 2a 20 61 63 74 69 6f 6e 20 69 6e 74 65 67 65  ** action intege
3b0ad 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f  r.  .**.** Suppo
3b0ae 73 65 20 74 68 65 20 61 63 74 69 6f 6e 20 69 6e  se the action in
3b0af 74 65 67 65 72 20 69 73 20 4e 2e 20 20 54 68 65  teger is N.  The
3b0b0 6e 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20  n the action is 
3b0b1 64 65 74 65 72 6d 69 6e 65 64 20 61 73 0a 2a 2a  determined as.**
3b0b2 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2a 20 20   follows.**.**  
3b0b3 20 30 20 3c 3d 20 4e 20 3c 20 59 59 4e 53 54 41   0 <= N < YYNSTA
3b0b4 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TE              
3b0b5 20 20 20 20 53 68 69 66 74 20 4e 2e 20 20 54 68      Shift N.  Th
3b0b6 61 74 20 69 73 2c 20 70 75 73 68 20 74 68 65 20  at is, push the 
3b0b7 6c 6f 6f 6b 61 68 65 61 64 0a 2a 2a 20 20 20 20  lookahead.**    
3b0b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b0b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b0ba 20 20 74 6f 6b 65 6e 20 6f 6e 74 6f 20 74 68 65    token onto the
3b0bb 20 73 74 61 63 6b 20 61 6e 64 20 67 6f 74 6f 20   stack and goto 
3b0bc 73 74 61 74 65 20 4e 2e 0a 2a 2a 0a 2a 2a 20 20  state N..**.**  
3b0bd 20 59 59 4e 53 54 41 54 45 20 3c 3d 20 4e 20 3c   YYNSTATE <= N <
3b0be 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c   YYNSTATE+YYNRUL
3b0bf 45 20 20 20 52 65 64 75 63 65 20 62 79 20 72 75  E   Reduce by ru
3b0c0 6c 65 20 4e 2d 59 59 4e 53 54 41 54 45 2e 0a 2a  le N-YYNSTATE..*
3b0c1 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53  *.**   N == YYNS
3b0c2 54 41 54 45 2b 59 59 4e 52 55 4c 45 20 20 20 20  TATE+YYNRULE    
3b0c3 20 20 20 20 20 20 20 20 20 20 41 20 73 79 6e 74            A synt
3b0c4 61 78 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  ax error has occ
3b0c5 75 72 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e  urred..**.**   N
3b0c6 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59 59 4e   == YYNSTATE+YYN
3b0c7 52 55 4c 45 2b 31 20 20 20 20 20 20 20 20 20 20  RULE+1          
3b0c8 20 20 54 68 65 20 70 61 72 73 65 72 20 61 63 63    The parser acc
3b0c9 65 70 74 73 20 69 74 73 20 69 6e 70 75 74 2e 0a  epts its input..
3b0ca 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e  **.**   N == YYN
3b0cb 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 32 20  STATE+YYNRULE+2 
3b0cc 20 20 20 20 20 20 20 20 20 20 20 4e 6f 20 73 75             No su
3b0cd 63 68 20 61 63 74 69 6f 6e 2e 20 20 44 65 6e 6f  ch action.  Deno
3b0ce 74 65 73 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20  tes unused.**   
3b0cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b0d1 20 20 20 73 6c 6f 74 73 20 69 6e 20 74 68 65 20     slots in the 
3b0d2 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 74 61 62 6c  yy_action[] tabl
3b0d3 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 74  e..**.** The act
3b0d4 69 6f 6e 20 74 61 62 6c 65 20 69 73 20 63 6f 6e  ion table is con
3b0d5 73 74 72 75 63 74 65 64 20 61 73 20 61 20 73 69  structed as a si
3b0d6 6e 67 6c 65 20 6c 61 72 67 65 20 74 61 62 6c 65  ngle large table
3b0d7 20 6e 61 6d 65 64 20 79 79 5f 61 63 74 69 6f 6e   named yy_action
3b0d8 5b 5d 2e 0a 2a 2a 20 47 69 76 65 6e 20 73 74 61  []..** Given sta
3b0d9 74 65 20 53 20 61 6e 64 20 6c 6f 6f 6b 61 68 65  te S and lookahe
3b0da 61 64 20 58 2c 20 74 68 65 20 61 63 74 69 6f 6e  ad X, the action
3b0db 20 69 73 20 63 6f 6d 70 75 74 65 64 20 61 73 0a   is computed as.
3b0dc 2a 2a 0a 2a 2a 20 20 20 20 20 20 79 79 5f 61 63  **.**      yy_ac
3b0dd 74 69 6f 6e 5b 20 79 79 5f 73 68 69 66 74 5f 6f  tion[ yy_shift_o
3b0de 66 73 74 5b 53 5d 20 2b 20 58 20 5d 0a 2a 2a 0a  fst[S] + X ].**.
3b0df 2a 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 20  ** If the index 
3b0e0 76 61 6c 75 65 20 79 79 5f 73 68 69 66 74 5f 6f  value yy_shift_o
3b0e1 66 73 74 5b 53 5d 2b 58 20 69 73 20 6f 75 74 20  fst[S]+X is out 
3b0e2 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66 20 74  of range or if t
3b0e3 68 65 20 76 61 6c 75 65 0a 2a 2a 20 79 79 5f 6c  he value.** yy_l
3b0e4 6f 6f 6b 61 68 65 61 64 5b 79 79 5f 73 68 69 66  ookahead[yy_shif
3b0e5 74 5f 6f 66 73 74 5b 53 5d 2b 58 5d 20 69 73 20  t_ofst[S]+X] is 
3b0e6 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 58 20 6f  not equal to X o
3b0e7 72 20 69 66 20 79 79 5f 73 68 69 66 74 5f 6f 66  r if yy_shift_of
3b0e8 73 74 5b 53 5d 0a 2a 2a 20 69 73 20 65 71 75 61  st[S].** is equa
3b0e9 6c 20 74 6f 20 59 59 5f 53 48 49 46 54 5f 55 53  l to YY_SHIFT_US
3b0ea 45 5f 44 46 4c 54 2c 20 69 74 20 6d 65 61 6e 73  E_DFLT, it means
3b0eb 20 74 68 61 74 20 74 68 65 20 61 63 74 69 6f 6e   that the action
3b0ec 20 69 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 74   is not in the t
3b0ed 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 61 74  able.** and that
3b0ee 20 79 79 5f 64 65 66 61 75 6c 74 5b 53 5d 20 73   yy_default[S] s
3b0ef 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e  hould be used in
3b0f0 73 74 65 61 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  stead.  .**.** T
3b0f1 68 65 20 66 6f 72 6d 75 6c 61 20 61 62 6f 76 65  he formula above
3b0f2 20 69 73 20 66 6f 72 20 63 6f 6d 70 75 74 69 6e   is for computin
3b0f3 67 20 74 68 65 20 61 63 74 69 6f 6e 20 77 68 65  g the action whe
3b0f4 6e 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 20  n the lookahead 
3b0f5 69 73 0a 2a 2a 20 61 20 74 65 72 6d 69 6e 61 6c  is.** a terminal
3b0f6 20 73 79 6d 62 6f 6c 2e 20 20 49 66 20 74 68 65   symbol.  If the
3b0f7 20 6c 6f 6f 6b 61 68 65 61 64 20 69 73 20 61 20   lookahead is a 
3b0f8 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 20 28 61 73  non-terminal (as
3b0f9 20 6f 63 63 75 72 73 20 61 66 74 65 72 0a 2a 2a   occurs after.**
3b0fa 20 61 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e   a reduce action
3b0fb 29 20 74 68 65 6e 20 74 68 65 20 79 79 5f 72 65  ) then the yy_re
3b0fc 64 75 63 65 5f 6f 66 73 74 5b 5d 20 61 72 72 61  duce_ofst[] arra
3b0fd 79 20 69 73 20 75 73 65 64 20 69 6e 20 70 6c 61  y is used in pla
3b0fe 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 79 79 5f  ce of.** the yy_
3b0ff 73 68 69 66 74 5f 6f 66 73 74 5b 5d 20 61 72 72  shift_ofst[] arr
3b100 61 79 20 61 6e 64 20 59 59 5f 52 45 44 55 43 45  ay and YY_REDUCE
3b101 5f 55 53 45 5f 44 46 4c 54 20 69 73 20 75 73 65  _USE_DFLT is use
3b102 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a  d in place of.**
3b103 20 59 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46   YY_SHIFT_USE_DF
3b104 4c 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  LT..**.** The fo
3b105 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65 20  llowing are the 
3b106 74 61 62 6c 65 73 20 67 65 6e 65 72 61 74 65 64  tables generated
3b107 20 69 6e 20 74 68 69 73 20 73 65 63 74 69 6f 6e   in this section
3b108 3a 0a 2a 2a 0a 2a 2a 20 20 79 79 5f 61 63 74 69  :.**.**  yy_acti
3b109 6f 6e 5b 5d 20 20 20 20 20 20 20 20 41 20 73 69  on[]        A si
3b10a 6e 67 6c 65 20 74 61 62 6c 65 20 63 6f 6e 74 61  ngle table conta
3b10b 69 6e 69 6e 67 20 61 6c 6c 20 61 63 74 69 6f 6e  ining all action
3b10c 73 2e 0a 2a 2a 20 20 79 79 5f 6c 6f 6f 6b 61 68  s..**  yy_lookah
3b10d 65 61 64 5b 5d 20 20 20 20 20 41 20 74 61 62 6c  ead[]     A tabl
3b10e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
3b10f 20 6c 6f 6f 6b 61 68 65 61 64 20 66 6f 72 20 65   lookahead for e
3b110 61 63 68 20 65 6e 74 72 79 20 69 6e 0a 2a 2a 20  ach entry in.** 
3b111 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3b112 20 20 20 20 79 79 5f 61 63 74 69 6f 6e 2e 20 20      yy_action.  
3b113 55 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 68  Used to detect h
3b114 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a  ash collisions..
3b115 2a 2a 20 20 79 79 5f 73 68 69 66 74 5f 6f 66 73  **  yy_shift_ofs
3b116 74 5b 5d 20 20 20 20 46 6f 72 20 65 61 63 68 20  t[]    For each 
3b117 73 74 61 74 65 2c 20 74 68 65 20 6f 66 66 73 65  state, the offse
3b118 74 20 69 6e 74 6f 20 79 79 5f 61 63 74 69 6f 6e  t into yy_action
3b119 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20   for.**         
3b11a 20 20 20 20 20 20 20 20 20 20 20 20 73 68 69 66              shif
3b11b 74 69 6e 67 20 74 65 72 6d 69 6e 61 6c 73 2e 0a  ting terminals..
3b11c 2a 2a 20 20 79 79 5f 72 65 64 75 63 65 5f 6f 66  **  yy_reduce_of
3b11d 73 74 5b 5d 20 20 20 46 6f 72 20 65 61 63 68 20  st[]   For each 
3b11e 73 74 61 74 65 2c 20 74 68 65 20 6f 66 66 73 65  state, the offse
3b11f 74 20 69 6e 74 6f 20 79 79 5f 61 63 74 69 6f 6e  t into yy_action
3b120 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20   for.**         
3b121 20 20 20 20 20 20 20 20 20 20 20 20 73 68 69 66              shif
3b122 74 69 6e 67 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61  ting non-termina
3b123 6c 73 20 61 66 74 65 72 20 61 20 72 65 64 75 63  ls after a reduc
3b124 65 2e 0a 2a 2a 20 20 79 79 5f 64 65 66 61 75 6c  e..**  yy_defaul
3b125 74 5b 5d 20 20 20 20 20 20 20 44 65 66 61 75 6c  t[]       Defaul
3b126 74 20 61 63 74 69 6f 6e 20 66 6f 72 20 65 61 63  t action for eac
3b127 68 20 73 74 61 74 65 2e 0a 2a 2f 0a 23 64 65 66  h state..*/.#def
3b128 69 6e 65 20 59 59 5f 41 43 54 54 41 42 5f 43 4f  ine YY_ACTTAB_CO
3b129 55 4e 54 20 28 31 35 36 34 29 0a 73 74 61 74 69  UNT (1564).stati
3b12a 63 20 63 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e  c const YYACTION
3b12b 54 59 50 45 20 79 79 5f 61 63 74 69 6f 6e 5b 5d  TYPE yy_action[]
3b12c 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a   = {. /*     0 *
3b12d 2f 20 20 20 33 30 39 2c 20 20 39 35 35 2c 20 20  /   309,  955,  
3b12e 31 38 34 2c 20 20 34 31 37 2c 20 20 20 20 32 2c  184,  417,    2,
3b12f 20 20 31 37 31 2c 20 20 36 32 34 2c 20 20 35 39    171,  624,  59
3b130 34 2c 20 20 20 35 36 2c 20 20 20 35 36 2c 0a 20  4,   56,   56,. 
3b131 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 20 35  /*    10 */    5
3b132 36 2c 20 20 20 35 36 2c 20 20 20 34 39 2c 20 20  6,   56,   49,  
3b133 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 34 2c   54,   54,   54,
3b134 20 20 20 35 34 2c 20 20 20 35 33 2c 20 20 20 35     54,   53,   5
3b135 33 2c 20 20 20 35 32 2c 0a 20 2f 2a 20 20 20 20  3,   52,. /*    
3b136 32 30 20 2a 2f 20 20 20 20 35 32 2c 20 20 20 35  20 */    52,   5
3b137 32 2c 20 20 20 35 31 2c 20 20 32 33 33 2c 20 20  2,   51,  233,  
3b138 36 32 30 2c 20 20 36 31 39 2c 20 20 32 39 38 2c  620,  619,  298,
3b139 20 20 36 32 30 2c 20 20 36 31 39 2c 20 20 32 33    620,  619,  23
3b13a 34 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20  4,. /*    30 */ 
3b13b 20 20 35 38 37 2c 20 20 35 38 31 2c 20 20 20 35    587,  581,   5
3b13c 36 2c 20 20 20 35 36 2c 20 20 20 35 36 2c 20 20  6,   56,   56,  
3b13d 20 35 36 2c 20 20 20 31 39 2c 20 20 20 35 34 2c   56,   19,   54,
3b13e 20 20 20 35 34 2c 20 20 20 35 34 2c 0a 20 2f 2a     54,   54,. /*
3b13f 20 20 20 20 34 30 20 2a 2f 20 20 20 20 35 34 2c      40 */    54,
3b140 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
3b141 32 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20  2,   52,   52,  
3b142 20 35 31 2c 20 20 32 33 33 2c 20 20 36 30 35 2c   51,  233,  605,
3b143 20 20 20 35 37 2c 0a 20 2f 2a 20 20 20 20 35 30     57,. /*    50
3b144 20 2a 2f 20 20 20 20 35 38 2c 20 20 20 34 38 2c   */    58,   48,
3b145 20 20 35 37 39 2c 20 20 35 37 38 2c 20 20 35 38    579,  578,  58
3b146 30 2c 20 20 35 38 30 2c 20 20 20 35 35 2c 20 20  0,  580,   55,  
3b147 20 35 35 2c 20 20 20 35 36 2c 20 20 20 35 36 2c   55,   56,   56,
3b148 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20  . /*    60 */   
3b149 20 35 36 2c 20 20 20 35 36 2c 20 20 35 34 31 2c   56,   56,  541,
3b14a 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35     54,   54,   5
3b14b 34 2c 20 20 20 35 34 2c 20 20 20 35 33 2c 20 20  4,   54,   53,  
3b14c 20 35 33 2c 20 20 20 35 32 2c 0a 20 2f 2a 20 20   53,   52,. /*  
3b14d 20 20 37 30 20 2a 2f 20 20 20 20 35 32 2c 20 20    70 */    52,  
3b14e 20 35 32 2c 20 20 20 35 31 2c 20 20 32 33 33 2c   52,   51,  233,
3b14f 20 20 33 30 39 2c 20 20 35 39 34 2c 20 20 33 32    309,  594,  32
3b150 35 2c 20 20 31 39 36 2c 20 20 31 39 35 2c 20 20  5,  196,  195,  
3b151 31 39 34 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a  194,. /*    80 *
3b152 2f 20 20 20 20 33 33 2c 20 20 20 35 34 2c 20 20  /    33,   54,  
3b153 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 34 2c   54,   54,   54,
3b154 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
3b155 32 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 0a 20  2,   52,   52,. 
3b156 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 20 35  /*    90 */    5
3b157 31 2c 20 20 32 33 33 2c 20 20 36 31 37 2c 20 20  1,  233,  617,  
3b158 36 31 36 2c 20 20 31 36 35 2c 20 20 36 31 37 2c  616,  165,  617,
3b159 20 20 36 31 36 2c 20 20 33 38 30 2c 20 20 33 37    616,  380,  37
3b15a 37 2c 20 20 33 37 36 2c 0a 20 2f 2a 20 20 20 31  7,  376,. /*   1
3b15b 30 30 20 2a 2f 20 20 20 34 30 37 2c 20 20 35 33  00 */   407,  53
3b15c 32 2c 20 20 35 37 36 2c 20 20 35 37 36 2c 20 20  2,  576,  576,  
3b15d 35 38 37 2c 20 20 35 38 31 2c 20 20 33 30 33 2c  587,  581,  303,
3b15e 20 20 34 32 32 2c 20 20 33 37 35 2c 20 20 20 35    422,  375,   5
3b15f 39 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20  9,. /*   110 */ 
3b160 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
3b161 32 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20  2,   52,   52,  
3b162 20 35 31 2c 20 20 32 33 33 2c 20 20 20 35 30 2c   51,  233,   50,
3b163 20 20 20 34 37 2c 20 20 31 34 36 2c 0a 20 2f 2a     47,  146,. /*
3b164 20 20 20 31 32 30 20 2a 2f 20 20 20 35 37 34 2c     120 */   574,
3b165 20 20 35 34 35 2c 20 20 20 36 35 2c 20 20 20 35    545,   65,   5
3b166 37 2c 20 20 20 35 38 2c 20 20 20 34 38 2c 20 20  7,   58,   48,  
3b167 35 37 39 2c 20 20 35 37 38 2c 20 20 35 38 30 2c  579,  578,  580,
3b168 20 20 35 38 30 2c 0a 20 2f 2a 20 20 20 31 33 30    580,. /*   130
3b169 20 2a 2f 20 20 20 20 35 35 2c 20 20 20 35 35 2c   */    55,   55,
3b16a 20 20 20 35 36 2c 20 20 20 35 36 2c 20 20 20 35     56,   56,   5
3b16b 36 2c 20 20 20 35 36 2c 20 20 32 31 33 2c 20 20  6,   56,  213,  
3b16c 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 34 2c   54,   54,   54,
3b16d 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20  . /*   140 */   
3b16e 20 35 34 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   54,   53,   53,
3b16f 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35     52,   52,   5
3b170 32 2c 20 20 20 35 31 2c 20 20 32 33 33 2c 20 20  2,   51,  233,  
3b171 33 30 39 2c 20 20 32 32 33 2c 0a 20 2f 2a 20 20  309,  223,. /*  
3b172 20 31 35 30 20 2a 2f 20 20 20 35 33 39 2c 20 20   150 */   539,  
3b173 34 32 30 2c 20 20 31 37 30 2c 20 20 31 37 36 2c  420,  170,  176,
3b174 20 20 31 33 38 2c 20 20 32 38 30 2c 20 20 33 38    138,  280,  38
3b175 33 2c 20 20 32 37 35 2c 20 20 33 38 32 2c 20 20  3,  275,  382,  
3b176 31 36 38 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a  168,. /*   160 *
3b177 2f 20 20 20 34 38 39 2c 20 20 35 35 31 2c 20 20  /   489,  551,  
3b178 34 30 39 2c 20 20 36 36 38 2c 20 20 36 32 30 2c  409,  668,  620,
3b179 20 20 36 31 39 2c 20 20 32 37 31 2c 20 20 34 33    619,  271,  43
3b17a 38 2c 20 20 34 30 39 2c 20 20 34 33 38 2c 0a 20  8,  409,  438,. 
3b17b 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 35 35  /*   170 */   55
3b17c 30 2c 20 20 36 30 34 2c 20 20 20 36 37 2c 20 20  0,  604,   67,  
3b17d 34 38 32 2c 20 20 35 30 37 2c 20 20 36 31 38 2c  482,  507,  618,
3b17e 20 20 35 39 39 2c 20 20 34 31 32 2c 20 20 35 38    599,  412,  58
3b17f 37 2c 20 20 35 38 31 2c 0a 20 2f 2a 20 20 20 31  7,  581,. /*   1
3b180 38 30 20 2a 2f 20 20 20 36 30 30 2c 20 20 34 38  80 */   600,  48
3b181 33 2c 20 20 36 31 38 2c 20 20 34 31 32 2c 20 20  3,  618,  412,  
3b182 36 31 38 2c 20 20 35 39 38 2c 20 20 20 39 31 2c  618,  598,   91,
3b183 20 20 34 33 39 2c 20 20 34 34 30 2c 20 20 34 33    439,  440,  43
3b184 39 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20  9,. /*   190 */ 
3b185 20 20 33 33 35 2c 20 20 35 39 38 2c 20 20 20 37    335,  598,   7
3b186 33 2c 20 20 36 36 39 2c 20 20 32 32 32 2c 20 20  3,  669,  222,  
3b187 32 36 36 2c 20 20 34 38 30 2c 20 20 20 35 37 2c  266,  480,   57,
3b188 20 20 20 35 38 2c 20 20 20 34 38 2c 0a 20 2f 2a     58,   48,. /*
3b189 20 20 20 32 30 30 20 2a 2f 20 20 20 35 37 39 2c     200 */   579,
3b18a 20 20 35 37 38 2c 20 20 35 38 30 2c 20 20 35 38    578,  580,  58
3b18b 30 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20  0,   55,   55,  
3b18c 20 35 36 2c 20 20 20 35 36 2c 20 20 20 35 36 2c   56,   56,   56,
3b18d 20 20 20 35 36 2c 0a 20 2f 2a 20 20 20 32 31 30     56,. /*   210
3b18e 20 2a 2f 20 20 20 36 37 30 2c 20 20 20 35 34 2c   */   670,   54,
3b18f 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35     54,   54,   5
3b190 34 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  4,   53,   53,  
3b191 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35 32 2c   52,   52,   52,
3b192 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20  . /*   220 */   
3b193 20 35 31 2c 20 20 32 33 33 2c 20 20 33 30 39 2c   51,  233,  309,
3b194 20 20 32 37 39 2c 20 20 32 33 32 2c 20 20 32 33    279,  232,  23
3b195 31 2c 20 20 20 20 31 2c 20 20 31 33 32 2c 20 20  1,    1,  132,  
3b196 32 30 30 2c 20 20 33 38 35 2c 0a 20 2f 2a 20 20  200,  385,. /*  
3b197 20 32 33 30 20 2a 2f 20 20 20 36 32 30 2c 20 20   230 */   620,  
3b198 36 31 39 2c 20 20 36 31 37 2c 20 20 36 31 36 2c  619,  617,  616,
3b199 20 20 32 37 38 2c 20 20 34 33 35 2c 20 20 32 38    278,  435,  28
3b19a 39 2c 20 20 35 36 33 2c 20 20 31 37 35 2c 20 20  9,  563,  175,  
3b19b 32 36 32 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a  262,. /*   240 *
3b19c 2f 20 20 20 34 30 39 2c 20 20 32 36 34 2c 20 20  /   409,  264,  
3b19d 34 33 37 2c 20 20 34 39 37 2c 20 20 34 33 36 2c  437,  497,  436,
3b19e 20 20 31 36 36 2c 20 20 34 34 31 2c 20 20 35 36    166,  441,  56
3b19f 38 2c 20 20 33 33 36 2c 20 20 35 36 38 2c 0a 20  8,  336,  568,. 
3b1a0 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20 32 30  /*   250 */   20
3b1a1 31 2c 20 20 35 33 37 2c 20 20 35 38 37 2c 20 20  1,  537,  587,  
3b1a2 35 38 31 2c 20 20 35 39 39 2c 20 20 34 31 32 2c  581,  599,  412,
3b1a3 20 20 31 36 35 2c 20 20 35 39 34 2c 20 20 36 30    165,  594,  60
3b1a4 30 2c 20 20 33 38 30 2c 0a 20 2f 2a 20 20 20 32  0,  380,. /*   2
3b1a5 36 30 20 2a 2f 20 20 20 33 37 37 2c 20 20 33 37  60 */   377,  37
3b1a6 36 2c 20 20 35 39 37 2c 20 20 35 39 38 2c 20 20  6,  597,  598,  
3b1a7 20 39 32 2c 20 20 35 32 33 2c 20 20 36 31 38 2c   92,  523,  618,
3b1a8 20 20 35 36 39 2c 20 20 35 36 39 2c 20 20 35 39    569,  569,  59
3b1a9 32 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20  2,. /*   270 */ 
3b1aa 20 20 33 37 35 2c 20 20 20 35 37 2c 20 20 20 35    375,   57,   5
3b1ab 38 2c 20 20 20 34 38 2c 20 20 35 37 39 2c 20 20  8,   48,  579,  
3b1ac 35 37 38 2c 20 20 35 38 30 2c 20 20 35 38 30 2c  578,  580,  580,
3b1ad 20 20 20 35 35 2c 20 20 20 35 35 2c 0a 20 2f 2a     55,   55,. /*
3b1ae 20 20 20 32 38 30 20 2a 2f 20 20 20 20 35 36 2c     280 */    56,
3b1af 20 20 20 35 36 2c 20 20 20 35 36 2c 20 20 20 35     56,   56,   5
3b1b0 36 2c 20 20 35 39 37 2c 20 20 20 35 34 2c 20 20  6,  597,   54,  
3b1b1 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 34 2c   54,   54,   54,
3b1b2 20 20 20 35 33 2c 0a 20 2f 2a 20 20 20 32 39 30     53,. /*   290
3b1b3 20 2a 2f 20 20 20 20 35 33 2c 20 20 20 35 32 2c   */    53,   52,
3b1b4 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35     52,   52,   5
3b1b5 31 2c 20 20 32 33 33 2c 20 20 33 30 39 2c 20 20  1,  233,  309,  
3b1b6 34 36 33 2c 20 20 36 31 37 2c 20 20 36 31 36 2c  463,  617,  616,
3b1b7 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20  . /*   300 */   
3b1b8 35 39 30 2c 20 20 35 39 30 2c 20 20 35 39 30 2c  590,  590,  590,
3b1b9 20 20 31 37 34 2c 20 20 32 37 32 2c 20 20 33 39    174,  272,  39
3b1ba 36 2c 20 20 34 30 39 2c 20 20 32 37 32 2c 20 20  6,  409,  272,  
3b1bb 34 30 39 2c 20 20 35 34 38 2c 0a 20 2f 2a 20 20  409,  548,. /*  
3b1bc 20 33 31 30 20 2a 2f 20 20 20 33 39 37 2c 20 20   310 */   397,  
3b1bd 36 32 30 2c 20 20 36 31 39 2c 20 20 20 36 38 2c  620,  619,   68,
3b1be 20 20 33 32 36 2c 20 20 36 32 30 2c 20 20 36 31    326,  620,  61
3b1bf 39 2c 20 20 36 32 30 2c 20 20 36 31 39 2c 20 20  9,  620,  619,  
3b1c0 36 31 38 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a  618,. /*   320 *
3b1c1 2f 20 20 20 35 34 36 2c 20 20 34 31 32 2c 20 20  /   546,  412,  
3b1c2 36 31 38 2c 20 20 34 31 32 2c 20 20 34 37 31 2c  618,  412,  471,
3b1c3 20 20 35 39 34 2c 20 20 35 38 37 2c 20 20 35 38    594,  587,  58
3b1c4 31 2c 20 20 34 37 32 2c 20 20 35 39 38 2c 0a 20  1,  472,  598,. 
3b1c5 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20 20 39  /*   330 */    9
3b1c6 32 2c 20 20 35 39 38 2c 20 20 20 39 32 2c 20 20  2,  598,   92,  
3b1c7 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35 32 2c   52,   52,   52,
3b1c8 20 20 20 35 31 2c 20 20 32 33 33 2c 20 20 35 31     51,  233,  51
3b1c9 33 2c 20 20 35 31 32 2c 0a 20 2f 2a 20 20 20 33  3,  512,. /*   3
3b1ca 34 30 20 2a 2f 20 20 20 32 30 36 2c 20 20 33 32  40 */   206,  32
3b1cb 32 2c 20 20 33 36 33 2c 20 20 34 36 34 2c 20 20  2,  363,  464,  
3b1cc 32 32 31 2c 20 20 20 35 37 2c 20 20 20 35 38 2c  221,   57,   58,
3b1cd 20 20 20 34 38 2c 20 20 35 37 39 2c 20 20 35 37     48,  579,  57
3b1ce 38 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20  8,. /*   350 */ 
3b1cf 20 20 35 38 30 2c 20 20 35 38 30 2c 20 20 20 35    580,  580,   5
3b1d0 35 2c 20 20 20 35 35 2c 20 20 20 35 36 2c 20 20  5,   55,   56,  
3b1d1 20 35 36 2c 20 20 20 35 36 2c 20 20 20 35 36 2c   56,   56,   56,
3b1d2 20 20 35 32 39 2c 20 20 20 35 34 2c 0a 20 2f 2a    529,   54,. /*
3b1d3 20 20 20 33 36 30 20 2a 2f 20 20 20 20 35 34 2c     360 */    54,
3b1d4 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35     54,   54,   5
3b1d5 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20  3,   53,   52,  
3b1d6 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c   52,   52,   51,
3b1d7 20 20 32 33 33 2c 0a 20 2f 2a 20 20 20 33 37 30    233,. /*   370
3b1d8 20 2a 2f 20 20 20 33 30 39 2c 20 20 33 39 36 2c   */   309,  396,
3b1d9 20 20 34 30 39 2c 20 20 33 39 36 2c 20 20 35 39    409,  396,  59
3b1da 37 2c 20 20 33 37 32 2c 20 20 33 38 36 2c 20 20  7,  372,  386,  
3b1db 35 33 30 2c 20 20 33 34 37 2c 20 20 36 31 37 2c  530,  347,  617,
3b1dc 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20  . /*   380 */   
3b1dd 36 31 36 2c 20 20 35 37 35 2c 20 20 32 30 32 2c  616,  575,  202,
3b1de 20 20 36 31 37 2c 20 20 36 31 36 2c 20 20 36 31    617,  616,  61
3b1df 37 2c 20 20 36 31 36 2c 20 20 34 31 32 2c 20 20  7,  616,  412,  
3b1e0 36 32 30 2c 20 20 36 31 39 2c 0a 20 2f 2a 20 20  620,  619,. /*  
3b1e1 20 33 39 30 20 2a 2f 20 20 20 31 34 35 2c 20 20   390 */   145,  
3b1e2 32 35 35 2c 20 20 33 34 36 2c 20 20 32 35 34 2c  255,  346,  254,
3b1e3 20 20 35 37 37 2c 20 20 35 39 38 2c 20 20 20 37    577,  598,   7
3b1e4 34 2c 20 20 33 35 31 2c 20 20 20 34 35 2c 20 20  4,  351,   45,  
3b1e5 34 38 39 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a  489,. /*   400 *
3b1e6 2f 20 20 20 35 38 37 2c 20 20 35 38 31 2c 20 20  /   587,  581,  
3b1e7 32 33 35 2c 20 20 31 38 39 2c 20 20 34 36 34 2c  235,  189,  464,
3b1e8 20 20 35 34 34 2c 20 20 31 36 37 2c 20 20 32 39    544,  167,  29
3b1e9 36 2c 20 20 31 38 37 2c 20 20 34 36 39 2c 0a 20  6,  187,  469,. 
3b1ea 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20 34 37  /*   410 */   47
3b1eb 39 2c 20 20 20 36 37 2c 20 20 20 36 32 2c 20 20  9,   67,   62,  
3b1ec 20 33 39 2c 20 20 36 31 38 2c 20 20 35 34 36 2c   39,  618,  546,
3b1ed 20 20 35 39 37 2c 20 20 33 34 35 2c 20 20 35 37    597,  345,  57
3b1ee 33 2c 20 20 20 35 37 2c 0a 20 2f 2a 20 20 20 34  3,   57,. /*   4
3b1ef 32 30 20 2a 2f 20 20 20 20 35 38 2c 20 20 20 34  20 */    58,   4
3b1f0 38 2c 20 20 35 37 39 2c 20 20 35 37 38 2c 20 20  8,  579,  578,  
3b1f1 35 38 30 2c 20 20 35 38 30 2c 20 20 20 35 35 2c  580,  580,   55,
3b1f2 20 20 20 35 35 2c 20 20 20 35 36 2c 20 20 20 35     55,   56,   5
3b1f3 36 2c 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20  6,. /*   430 */ 
3b1f4 20 20 20 35 36 2c 20 20 20 35 36 2c 20 20 20 20     56,   56,    
3b1f5 36 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20  6,   54,   54,  
3b1f6 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 33 2c   54,   54,   53,
3b1f7 20 20 20 35 33 2c 20 20 20 35 32 2c 0a 20 2f 2a     53,   52,. /*
3b1f8 20 20 20 34 34 30 20 2a 2f 20 20 20 20 35 32 2c     440 */    52,
3b1f9 20 20 20 35 32 2c 20 20 20 35 31 2c 20 20 32 33     52,   51,  23
3b1fa 33 2c 20 20 33 30 39 2c 20 20 35 36 32 2c 20 20  3,  309,  562,  
3b1fb 35 35 38 2c 20 20 34 30 37 2c 20 20 35 32 38 2c  558,  407,  528,
3b1fc 20 20 35 37 36 2c 0a 20 2f 2a 20 20 20 34 35 30    576,. /*   450
3b1fd 20 2a 2f 20 20 20 35 37 36 2c 20 20 33 34 34 2c   */   576,  344,
3b1fe 20 20 32 35 35 2c 20 20 33 34 36 2c 20 20 32 35    255,  346,  25
3b1ff 34 2c 20 20 31 38 32 2c 20 20 36 31 37 2c 20 20  4,  182,  617,  
3b200 36 31 36 2c 20 20 35 30 33 2c 20 20 35 30 34 2c  616,  503,  504,
3b201 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20  . /*   460 */   
3b202 33 31 34 2c 20 20 34 30 39 2c 20 20 35 35 37 2c  314,  409,  557,
3b203 20 20 32 33 35 2c 20 20 31 36 36 2c 20 20 32 37    235,  166,  27
3b204 31 2c 20 20 34 30 39 2c 20 20 33 35 32 2c 20 20  1,  409,  352,  
3b205 35 36 34 2c 20 20 31 38 31 2c 0a 20 2f 2a 20 20  564,  181,. /*  
3b206 20 34 37 30 20 2a 2f 20 20 20 34 30 37 2c 20 20   470 */   407,  
3b207 35 34 36 2c 20 20 35 37 36 2c 20 20 35 37 36 2c  546,  576,  576,
3b208 20 20 35 38 37 2c 20 20 35 38 31 2c 20 20 34 31    587,  581,  41
3b209 32 2c 20 20 35 33 37 2c 20 20 35 35 36 2c 20 20  2,  537,  556,  
3b20a 35 36 31 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a  561,. /*   480 *
3b20b 2f 20 20 20 35 31 37 2c 20 20 34 31 32 2c 20 20  /   517,  412,  
3b20c 36 31 38 2c 20 20 32 34 39 2c 20 20 35 39 38 2c  618,  249,  598,
3b20d 20 20 20 31 36 2c 20 20 20 20 37 2c 20 20 20 33     16,    7,   3
3b20e 36 2c 20 20 34 36 37 2c 20 20 35 39 38 2c 0a 20  6,  467,  598,. 
3b20f 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20 20 39  /*   490 */    9
3b210 32 2c 20 20 35 31 36 2c 20 20 36 31 38 2c 20 20  2,  516,  618,  
3b211 20 35 37 2c 20 20 20 35 38 2c 20 20 20 34 38 2c   57,   58,   48,
3b212 20 20 35 37 39 2c 20 20 35 37 38 2c 20 20 35 38    579,  578,  58
3b213 30 2c 20 20 35 38 30 2c 0a 20 2f 2a 20 20 20 35  0,  580,. /*   5
3b214 30 30 20 2a 2f 20 20 20 20 35 35 2c 20 20 20 35  00 */    55,   5
3b215 35 2c 20 20 20 35 36 2c 20 20 20 35 36 2c 20 20  5,   56,   56,  
3b216 20 35 36 2c 20 20 20 35 36 2c 20 20 35 34 31 2c   56,   56,  541,
3b217 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35     54,   54,   5
3b218 34 2c 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20  4,. /*   510 */ 
3b219 20 20 20 35 34 2c 20 20 20 35 33 2c 20 20 20 35     54,   53,   5
3b21a 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20  3,   52,   52,  
3b21b 20 35 32 2c 20 20 20 35 31 2c 20 20 32 33 33 2c   52,   51,  233,
3b21c 20 20 33 30 39 2c 20 20 33 32 37 2c 0a 20 2f 2a    309,  327,. /*
3b21d 20 20 20 35 32 30 20 2a 2f 20 20 20 35 37 32 2c     520 */   572,
3b21e 20 20 35 37 31 2c 20 20 35 32 35 2c 20 20 35 35    571,  525,  55
3b21f 38 2c 20 20 35 36 30 2c 20 20 33 39 34 2c 20 20  8,  560,  394,  
3b220 38 37 31 2c 20 20 32 34 36 2c 20 20 34 30 39 2c  871,  246,  409,
3b221 20 20 32 34 38 2c 0a 20 2f 2a 20 20 20 35 33 30    248,. /*   530
3b222 20 2a 2f 20 20 20 31 37 31 2c 20 20 33 39 32 2c   */   171,  392,
3b223 20 20 35 39 34 2c 20 20 32 31 39 2c 20 20 34 30    594,  219,  40
3b224 37 2c 20 20 34 30 39 2c 20 20 35 37 36 2c 20 20  7,  409,  576,  
3b225 35 37 36 2c 20 20 35 30 32 2c 20 20 35 35 37 2c  576,  502,  557,
3b226 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20 20  . /*   540 */   
3b227 33 36 34 2c 20 20 31 34 35 2c 20 20 35 31 30 2c  364,  145,  510,
3b228 20 20 34 31 32 2c 20 20 34 30 37 2c 20 20 32 32    412,  407,  22
3b229 39 2c 20 20 35 37 36 2c 20 20 35 37 36 2c 20 20  9,  576,  576,  
3b22a 35 38 37 2c 20 20 35 38 31 2c 0a 20 2f 2a 20 20  587,  581,. /*  
3b22b 20 35 35 30 20 2a 2f 20 20 20 34 31 32 2c 20 20   550 */   412,  
3b22c 35 39 38 2c 20 20 20 39 32 2c 20 20 33 38 31 2c  598,   92,  381,
3b22d 20 20 32 36 39 2c 20 20 35 35 36 2c 20 20 31 36    269,  556,  16
3b22e 36 2c 20 20 34 30 30 2c 20 20 35 39 38 2c 20 20  6,  400,  598,  
3b22f 20 36 39 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a   69,. /*   560 *
3b230 2f 20 20 20 35 30 31 2c 20 20 34 31 39 2c 20 20  /   501,  419,  
3b231 39 34 35 2c 20 20 31 39 39 2c 20 20 39 34 35 2c  945,  199,  945,
3b232 20 20 31 39 38 2c 20 20 35 34 36 2c 20 20 20 35    198,  546,   5
3b233 37 2c 20 20 20 35 38 2c 20 20 20 34 38 2c 0a 20  7,   58,   48,. 
3b234 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20 35 37  /*   570 */   57
3b235 39 2c 20 20 35 37 38 2c 20 20 35 38 30 2c 20 20  9,  578,  580,  
3b236 35 38 30 2c 20 20 20 35 35 2c 20 20 20 35 35 2c  580,   55,   55,
3b237 20 20 20 35 36 2c 20 20 20 35 36 2c 20 20 20 35     56,   56,   5
3b238 36 2c 20 20 20 35 36 2c 0a 20 2f 2a 20 20 20 35  6,   56,. /*   5
3b239 38 30 20 2a 2f 20 20 20 35 36 38 2c 20 20 20 35  80 */   568,   5
3b23a 34 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20  4,   54,   54,  
3b23b 20 35 34 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   54,   53,   53,
3b23c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35     52,   52,   5
3b23d 32 2c 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20  2,. /*   590 */ 
3b23e 20 20 20 35 31 2c 20 20 32 33 33 2c 20 20 33 30     51,  233,  30
3b23f 39 2c 20 20 33 31 37 2c 20 20 34 31 39 2c 20 20  9,  317,  419,  
3b240 39 34 34 2c 20 20 35 30 38 2c 20 20 39 34 34 2c  944,  508,  944,
3b241 20 20 33 30 38 2c 20 20 35 39 37 2c 0a 20 2f 2a    308,  597,. /*
3b242 20 20 20 36 30 30 20 2a 2f 20 20 20 35 39 34 2c     600 */   594,
3b243 20 20 35 36 35 2c 20 20 34 39 30 2c 20 20 32 31    565,  490,  21
3b244 32 2c 20 20 31 37 33 2c 20 20 32 34 37 2c 20 20  2,  173,  247,  
3b245 34 32 33 2c 20 20 36 31 35 2c 20 20 36 31 34 2c  423,  615,  614,
3b246 20 20 36 31 33 2c 0a 20 2f 2a 20 20 20 36 31 30    613,. /*   610
3b247 20 2a 2f 20 20 20 33 32 33 2c 20 20 31 39 37 2c   */   323,  197,
3b248 20 20 31 34 33 2c 20 20 34 30 35 2c 20 20 35 37    143,  405,  57
3b249 32 2c 20 20 35 37 31 2c 20 20 34 38 39 2c 20 20  2,  571,  489,  
3b24a 20 36 36 2c 20 20 20 35 30 2c 20 20 20 34 37 2c   66,   50,   47,
3b24b 0a 20 2f 2a 20 20 20 36 32 30 20 2a 2f 20 20 20  . /*   620 */   
3b24c 31 34 36 2c 20 20 35 39 34 2c 20 20 35 38 37 2c  146,  594,  587,
3b24d 20 20 35 38 31 2c 20 20 32 33 32 2c 20 20 32 33    581,  232,  23
3b24e 31 2c 20 20 35 35 39 2c 20 20 34 32 37 2c 20 20  1,  559,  427,  
3b24f 20 36 37 2c 20 20 35 35 35 2c 0a 20 2f 2a 20 20   67,  555,. /*  
3b250 20 36 33 30 20 2a 2f 20 20 20 20 31 35 2c 20 20   630 */    15,  
3b251 36 31 38 2c 20 20 31 38 36 2c 20 20 35 34 33 2c  618,  186,  543,
3b252 20 20 33 30 33 2c 20 20 34 32 31 2c 20 20 20 33    303,  421,   3
3b253 35 2c 20 20 32 30 36 2c 20 20 34 33 32 2c 20 20  5,  206,  432,  
3b254 34 32 33 2c 0a 20 2f 2a 20 20 20 36 34 30 20 2a  423,. /*   640 *
3b255 2f 20 20 20 35 35 32 2c 20 20 20 35 37 2c 20 20  /   552,   57,  
3b256 20 35 38 2c 20 20 20 34 38 2c 20 20 35 37 39 2c   58,   48,  579,
3b257 20 20 35 37 38 2c 20 20 35 38 30 2c 20 20 35 38    578,  580,  58
3b258 30 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 0a 20  0,   55,   55,. 
3b259 2f 2a 20 20 20 36 35 30 20 2a 2f 20 20 20 20 35  /*   650 */    5
3b25a 36 2c 20 20 20 35 36 2c 20 20 20 35 36 2c 20 20  6,   56,   56,  
3b25b 20 35 36 2c 20 20 32 30 35 2c 20 20 20 35 34 2c   56,  205,   54,
3b25c 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35     54,   54,   5
3b25d 34 2c 20 20 20 35 33 2c 0a 20 2f 2a 20 20 20 36  4,   53,. /*   6
3b25e 36 30 20 2a 2f 20 20 20 20 35 33 2c 20 20 20 35  60 */    53,   5
3b25f 32 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20  2,   52,   52,  
3b260 20 35 31 2c 20 20 32 33 33 2c 20 20 33 30 39 2c   51,  233,  309,
3b261 20 20 35 36 39 2c 20 20 35 36 39 2c 20 20 32 36    569,  569,  26
3b262 30 2c 0a 20 2f 2a 20 20 20 36 37 30 20 2a 2f 20  0,. /*   670 */ 
3b263 20 20 32 36 38 2c 20 20 35 39 37 2c 20 20 20 31    268,  597,   1
3b264 32 2c 20 20 33 37 33 2c 20 20 35 36 38 2c 20 20  2,  373,  568,  
3b265 31 36 36 2c 20 20 34 30 39 2c 20 20 33 31 33 2c  166,  409,  313,
3b266 20 20 34 30 39 2c 20 20 34 32 30 2c 0a 20 2f 2a    409,  420,. /*
3b267 20 20 20 36 38 30 20 2a 2f 20 20 20 34 30 39 2c     680 */   409,
3b268 20 20 34 37 33 2c 20 20 34 37 33 2c 20 20 33 36    473,  473,  36
3b269 35 2c 20 20 36 31 38 2c 20 20 20 35 30 2c 20 20  5,  618,   50,  
3b26a 20 34 37 2c 20 20 31 34 36 2c 20 20 35 39 37 2c   47,  146,  597,
3b26b 20 20 35 39 34 2c 0a 20 2f 2a 20 20 20 36 39 30    594,. /*   690
3b26c 20 2a 2f 20 20 20 34 36 38 2c 20 20 34 31 32 2c   */   468,  412,
3b26d 20 20 31 36 36 2c 20 20 34 31 32 2c 20 20 33 35    166,  412,  35
3b26e 31 2c 20 20 34 31 32 2c 20 20 35 38 37 2c 20 20  1,  412,  587,  
3b26f 35 38 31 2c 20 20 20 33 32 2c 20 20 35 39 38 2c  581,   32,  598,
3b270 0a 20 2f 2a 20 20 20 37 30 30 20 2a 2f 20 20 20  . /*   700 */   
3b271 20 39 34 2c 20 20 35 39 38 2c 20 20 20 39 37 2c   94,  598,   97,
3b272 20 20 35 39 38 2c 20 20 20 39 35 2c 20 20 36 32    598,   95,  62
3b273 37 2c 20 20 36 32 35 2c 20 20 33 32 39 2c 20 20  7,  625,  329,  
3b274 31 34 32 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20  142,   50,. /*  
3b275 20 37 31 30 20 2a 2f 20 20 20 20 34 37 2c 20 20   710 */    47,  
3b276 31 34 36 2c 20 20 33 33 33 2c 20 20 33 34 39 2c  146,  333,  349,
3b277 20 20 33 35 38 2c 20 20 20 35 37 2c 20 20 20 35    358,   57,   5
3b278 38 2c 20 20 20 34 38 2c 20 20 35 37 39 2c 20 20  8,   48,  579,  
3b279 35 37 38 2c 0a 20 2f 2a 20 20 20 37 32 30 20 2a  578,. /*   720 *
3b27a 2f 20 20 20 35 38 30 2c 20 20 35 38 30 2c 20 20  /   580,  580,  
3b27b 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 36 2c   55,   55,   56,
3b27c 20 20 20 35 36 2c 20 20 20 35 36 2c 20 20 20 35     56,   56,   5
3b27d 36 2c 20 20 34 30 39 2c 20 20 20 35 34 2c 0a 20  6,  409,   54,. 
3b27e 2f 2a 20 20 20 37 33 30 20 2a 2f 20 20 20 20 35  /*   730 */    5
3b27f 34 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20  4,   54,   54,  
3b280 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c   53,   53,   52,
3b281 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35     52,   52,   5
3b282 31 2c 20 20 32 33 33 2c 0a 20 2f 2a 20 20 20 37  1,  233,. /*   7
3b283 34 30 20 2a 2f 20 20 20 33 30 39 2c 20 20 34 30  40 */   309,  40
3b284 39 2c 20 20 33 38 38 2c 20 20 34 31 32 2c 20 20  9,  388,  412,  
3b285 34 30 39 2c 20 20 20 32 32 2c 20 20 35 36 35 2c  409,   22,  565,
3b286 20 20 34 30 34 2c 20 20 32 31 32 2c 20 20 33 36    404,  212,  36
3b287 32 2c 0a 20 2f 2a 20 20 20 37 35 30 20 2a 2f 20  2,. /*   750 */ 
3b288 20 20 33 38 39 2c 20 20 35 39 38 2c 20 20 31 30    389,  598,  10
3b289 34 2c 20 20 33 35 39 2c 20 20 34 30 39 2c 20 20  4,  359,  409,  
3b28a 31 35 36 2c 20 20 34 31 32 2c 20 20 34 30 39 2c  156,  412,  409,
3b28b 20 20 36 30 33 2c 20 20 34 31 32 2c 0a 20 2f 2a    603,  412,. /*
3b28c 20 20 20 37 36 30 20 2a 2f 20 20 20 35 33 37 2c     760 */   537,
3b28d 20 20 33 33 31 2c 20 20 35 36 39 2c 20 20 35 36    331,  569,  56
3b28e 39 2c 20 20 35 39 38 2c 20 20 31 30 33 2c 20 20  9,  598,  103,  
3b28f 34 39 33 2c 20 20 35 39 38 2c 20 20 31 30 35 2c  493,  598,  105,
3b290 20 20 34 31 32 2c 0a 20 2f 2a 20 20 20 37 37 30    412,. /*   770
3b291 20 2a 2f 20 20 20 35 38 37 2c 20 20 35 38 31 2c   */   587,  581,
3b292 20 20 34 31 32 2c 20 20 32 36 30 2c 20 20 35 34    412,  260,  54
3b293 39 2c 20 20 36 31 38 2c 20 20 20 31 31 2c 20 20  9,  618,   11,  
3b294 35 39 38 2c 20 20 31 30 36 2c 20 20 35 32 31 2c  598,  106,  521,
3b295 0a 20 2f 2a 20 20 20 37 38 30 20 2a 2f 20 20 20  . /*   780 */   
3b296 35 39 38 2c 20 20 31 33 33 2c 20 20 31 36 39 2c  598,  133,  169,
3b297 20 20 34 35 37 2c 20 20 34 35 36 2c 20 20 31 37    457,  456,  17
3b298 30 2c 20 20 20 33 35 2c 20 20 36 30 31 2c 20 20  0,   35,  601,  
3b299 36 31 38 2c 20 20 20 35 37 2c 0a 20 2f 2a 20 20  618,   57,. /*  
3b29a 20 37 39 30 20 2a 2f 20 20 20 20 35 38 2c 20 20   790 */    58,  
3b29b 20 34 38 2c 20 20 35 37 39 2c 20 20 35 37 38 2c   48,  579,  578,
3b29c 20 20 35 38 30 2c 20 20 35 38 30 2c 20 20 20 35    580,  580,   5
3b29d 35 2c 20 20 20 35 35 2c 20 20 20 35 36 2c 20 20  5,   55,   56,  
3b29e 20 35 36 2c 0a 20 2f 2a 20 20 20 38 30 30 20 2a   56,. /*   800 *
3b29f 2f 20 20 20 20 35 36 2c 20 20 20 35 36 2c 20 20  /    56,   56,  
3b2a0 34 30 39 2c 20 20 20 35 34 2c 20 20 20 35 34 2c  409,   54,   54,
3b2a1 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35     54,   54,   5
3b2a2 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c 0a 20  3,   53,   52,. 
3b2a3 2f 2a 20 20 20 38 31 30 20 2a 2f 20 20 20 20 35  /*   810 */    5
3b2a4 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 20 20  2,   52,   51,  
3b2a5 32 33 33 2c 20 20 33 30 39 2c 20 20 34 30 39 2c  233,  309,  409,
3b2a6 20 20 32 35 39 2c 20 20 34 31 32 2c 20 20 34 30    259,  412,  40
3b2a7 39 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20 20 38  9,   50,. /*   8
3b2a8 32 30 20 2a 2f 20 20 20 20 34 37 2c 20 20 31 34  20 */    47,  14
3b2a9 36 2c 20 20 33 35 37 2c 20 20 33 31 38 2c 20 20  6,  357,  318,  
3b2aa 33 35 35 2c 20 20 35 39 38 2c 20 20 31 33 34 2c  355,  598,  134,
3b2ab 20 20 35 32 37 2c 20 20 33 35 32 2c 20 20 33 33    527,  352,  33
3b2ac 37 2c 0a 20 2f 2a 20 20 20 38 33 30 20 2a 2f 20  7,. /*   830 */ 
3b2ad 20 20 34 31 32 2c 20 20 34 30 39 2c 20 20 33 35    412,  409,  35
3b2ae 36 2c 20 20 34 31 32 2c 20 20 33 35 37 2c 20 20  6,  412,  357,  
3b2af 34 30 39 2c 20 20 33 35 37 2c 20 20 36 31 38 2c  409,  357,  618,
3b2b0 20 20 35 39 38 2c 20 20 20 39 38 2c 0a 20 2f 2a    598,   98,. /*
3b2b1 20 20 20 38 34 30 20 2a 2f 20 20 20 31 32 39 2c     840 */   129,
3b2b2 20 20 35 39 38 2c 20 20 31 30 32 2c 20 20 36 31    598,  102,  61
3b2b3 38 2c 20 20 35 38 37 2c 20 20 35 38 31 2c 20 20  8,  587,  581,  
3b2b4 34 31 32 2c 20 20 20 32 31 2c 20 20 32 33 35 2c  412,   21,  235,
3b2b5 20 20 36 31 38 2c 0a 20 2f 2a 20 20 20 38 35 30    618,. /*   850
3b2b6 20 2a 2f 20 20 20 34 31 32 2c 20 20 36 31 38 2c   */   412,  618,
3b2b7 20 20 32 31 31 2c 20 20 31 34 33 2c 20 20 35 39    211,  143,  59
3b2b8 38 2c 20 20 31 30 31 2c 20 20 20 33 30 2c 20 20  8,  101,   30,  
3b2b9 31 36 37 2c 20 20 35 39 38 2c 20 20 20 39 33 2c  167,  598,   93,
3b2ba 0a 20 2f 2a 20 20 20 38 36 30 20 2a 2f 20 20 20  . /*   860 */   
3b2bb 33 35 30 2c 20 20 35 33 35 2c 20 20 32 30 33 2c  350,  535,  203,
3b2bc 20 20 20 35 37 2c 20 20 20 35 38 2c 20 20 20 34     57,   58,   4
3b2bd 38 2c 20 20 35 37 39 2c 20 20 35 37 38 2c 20 20  8,  579,  578,  
3b2be 35 38 30 2c 20 20 35 38 30 2c 0a 20 2f 2a 20 20  580,  580,. /*  
3b2bf 20 38 37 30 20 2a 2f 20 20 20 20 35 35 2c 20 20   870 */    55,  
3b2c0 20 35 35 2c 20 20 20 35 36 2c 20 20 20 35 36 2c   55,   56,   56,
3b2c1 20 20 20 35 36 2c 20 20 20 35 36 2c 20 20 34 30     56,   56,  40
3b2c2 39 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20  9,   54,   54,  
3b2c3 20 35 34 2c 0a 20 2f 2a 20 20 20 38 38 30 20 2a   54,. /*   880 *
3b2c4 2f 20 20 20 20 35 34 2c 20 20 20 35 33 2c 20 20  /    54,   53,  
3b2c5 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c   53,   52,   52,
3b2c6 20 20 20 35 32 2c 20 20 20 35 31 2c 20 20 32 33     52,   51,  23
3b2c7 33 2c 20 20 33 30 39 2c 20 20 34 30 39 2c 0a 20  3,  309,  409,. 
3b2c8 2f 2a 20 20 20 38 39 30 20 2a 2f 20 20 20 35 32  /*   890 */   52
3b2c9 36 2c 20 20 34 31 32 2c 20 20 34 30 39 2c 20 20  6,  412,  409,  
3b2ca 34 32 35 2c 20 20 32 31 35 2c 20 20 33 30 35 2c  425,  215,  305,
3b2cb 20 20 35 39 37 2c 20 20 35 35 31 2c 20 20 31 34    597,  551,  14
3b2cc 31 2c 20 20 35 39 38 2c 0a 20 2f 2a 20 20 20 39  1,  598,. /*   9
3b2cd 30 30 20 2a 2f 20 20 20 31 30 30 2c 20 20 20 34  00 */   100,   4
3b2ce 30 2c 20 20 34 30 39 2c 20 20 20 33 38 2c 20 20  0,  409,   38,  
3b2cf 34 31 32 2c 20 20 34 30 39 2c 20 20 35 35 30 2c  412,  409,  550,
3b2d0 20 20 34 31 32 2c 20 20 34 30 39 2c 20 20 32 32    412,  409,  22
3b2d1 38 2c 0a 20 2f 2a 20 20 20 39 31 30 20 2a 2f 20  8,. /*   910 */ 
3b2d2 20 20 32 32 30 2c 20 20 33 31 34 2c 20 20 35 39    220,  314,  59
3b2d3 38 2c 20 20 20 37 37 2c 20 20 35 30 30 2c 20 20  8,   77,  500,  
3b2d4 35 39 38 2c 20 20 20 39 36 2c 20 20 34 31 32 2c  598,   96,  412,
3b2d5 20 20 35 38 37 2c 20 20 35 38 31 2c 0a 20 2f 2a    587,  581,. /*
3b2d6 20 20 20 39 32 30 20 2a 2f 20 20 20 34 31 32 2c     920 */   412,
3b2d7 20 20 33 33 38 2c 20 20 32 35 33 2c 20 20 34 31    338,  253,  41
3b2d8 32 2c 20 20 32 31 38 2c 20 20 35 39 38 2c 20 20  2,  218,  598,  
3b2d9 31 33 37 2c 20 20 33 37 39 2c 20 20 35 39 38 2c  137,  379,  598,
3b2da 20 20 31 33 36 2c 0a 20 2f 2a 20 20 20 39 33 30    136,. /*   930
3b2db 20 2a 2f 20 20 20 20 32 38 2c 20 20 35 39 38 2c   */    28,  598,
3b2dc 20 20 31 33 35 2c 20 20 32 37 30 2c 20 20 37 31    135,  270,  71
3b2dd 35 2c 20 20 32 31 30 2c 20 20 34 38 31 2c 20 20  5,  210,  481,  
3b2de 20 35 37 2c 20 20 20 35 38 2c 20 20 20 34 38 2c   57,   58,   48,
3b2df 0a 20 2f 2a 20 20 20 39 34 30 20 2a 2f 20 20 20  . /*   940 */   
3b2e0 35 37 39 2c 20 20 35 37 38 2c 20 20 35 38 30 2c  579,  578,  580,
3b2e1 20 20 35 38 30 2c 20 20 20 35 35 2c 20 20 20 35    580,   55,   5
3b2e2 35 2c 20 20 20 35 36 2c 20 20 20 35 36 2c 20 20  5,   56,   56,  
3b2e3 20 35 36 2c 20 20 20 35 36 2c 0a 20 2f 2a 20 20   56,   56,. /*  
3b2e4 20 39 35 30 20 2a 2f 20 20 20 34 30 39 2c 20 20   950 */   409,  
3b2e5 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 34 2c   54,   54,   54,
3b2e6 20 20 20 35 34 2c 20 20 20 35 33 2c 20 20 20 35     54,   53,   5
3b2e7 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20  3,   52,   52,  
3b2e8 20 35 32 2c 0a 20 2f 2a 20 20 20 39 36 30 20 2a   52,. /*   960 *
3b2e9 2f 20 20 20 20 35 31 2c 20 20 32 33 33 2c 20 20  /    51,  233,  
3b2ea 33 30 39 2c 20 20 34 30 39 2c 20 20 32 37 32 2c  309,  409,  272,
3b2eb 20 20 34 31 32 2c 20 20 34 30 39 2c 20 20 33 31    412,  409,  31
3b2ec 35 2c 20 20 31 34 37 2c 20 20 35 39 37 2c 0a 20  5,  147,  597,. 
3b2ed 2f 2a 20 20 20 39 37 30 20 2a 2f 20 20 20 32 37  /*   970 */   27
3b2ee 32 2c 20 20 36 32 36 2c 20 20 20 20 32 2c 20 20  2,  626,    2,  
3b2ef 35 39 38 2c 20 20 20 37 36 2c 20 20 32 30 39 2c  598,   76,  209,
3b2f0 20 20 34 30 39 2c 20 20 31 32 37 2c 20 20 34 31    409,  127,  41
3b2f1 32 2c 20 20 36 31 38 2c 0a 20 2f 2a 20 20 20 39  2,  618,. /*   9
3b2f2 38 30 20 2a 2f 20 20 20 31 32 36 2c 20 20 34 31  80 */   126,  41
3b2f3 32 2c 20 20 34 30 39 2c 20 20 36 32 31 2c 20 20  2,  409,  621,  
3b2f4 32 33 35 2c 20 20 36 31 38 2c 20 20 35 39 38 2c  235,  618,  598,
3b2f5 20 20 20 39 30 2c 20 20 33 37 34 2c 20 20 35 39     90,  374,  59
3b2f6 38 2c 0a 20 2f 2a 20 20 20 39 39 30 20 2a 2f 20  8,. /*   990 */ 
3b2f7 20 20 20 38 39 2c 20 20 34 31 32 2c 20 20 35 38     89,  412,  58
3b2f8 37 2c 20 20 35 38 31 2c 20 20 20 32 37 2c 20 20  7,  581,   27,  
3b2f9 32 36 30 2c 20 20 33 35 30 2c 20 20 34 31 32 2c  260,  350,  412,
3b2fa 20 20 36 31 38 2c 20 20 35 39 38 2c 0a 20 2f 2a    618,  598,. /*
3b2fb 20 20 31 30 30 30 20 2a 2f 20 20 20 20 37 35 2c    1000 */    75,
3b2fc 20 20 33 32 31 2c 20 20 35 34 31 2c 20 20 35 34    321,  541,  54
3b2fd 31 2c 20 20 31 32 35 2c 20 20 35 39 38 2c 20 20  1,  125,  598,  
3b2fe 20 38 38 2c 20 20 33 32 30 2c 20 20 32 37 38 2c   88,  320,  278,
3b2ff 20 20 35 39 37 2c 0a 20 2f 2a 20 20 31 30 31 30    597,. /*  1010
3b300 20 2a 2f 20 20 20 36 31 38 2c 20 20 20 35 37 2c   */   618,   57,
3b301 20 20 20 34 36 2c 20 20 20 34 38 2c 20 20 35 37     46,   48,  57
3b302 39 2c 20 20 35 37 38 2c 20 20 35 38 30 2c 20 20  9,  578,  580,  
3b303 35 38 30 2c 20 20 20 35 35 2c 20 20 20 35 35 2c  580,   55,   55,
3b304 0a 20 2f 2a 20 20 31 30 32 30 20 2a 2f 20 20 20  . /*  1020 */   
3b305 20 35 36 2c 20 20 20 35 36 2c 20 20 20 35 36 2c   56,   56,   56,
3b306 20 20 20 35 36 2c 20 20 34 30 39 2c 20 20 20 35     56,  409,   5
3b307 34 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20  4,   54,   54,  
3b308 20 35 34 2c 20 20 20 35 33 2c 0a 20 2f 2a 20 20   54,   53,. /*  
3b309 31 30 33 30 20 2a 2f 20 20 20 20 35 33 2c 20 20  1030 */    53,  
3b30a 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35 32 2c   52,   52,   52,
3b30b 20 20 20 35 31 2c 20 20 32 33 33 2c 20 20 33 30     51,  233,  30
3b30c 39 2c 20 20 34 30 39 2c 20 20 34 35 30 2c 20 20  9,  409,  450,  
3b30d 34 31 32 2c 0a 20 2f 2a 20 20 31 30 34 30 20 2a  412,. /*  1040 *
3b30e 2f 20 20 20 31 36 34 2c 20 20 32 38 34 2c 20 20  /   164,  284,  
3b30f 32 38 32 2c 20 20 32 37 32 2c 20 20 36 30 39 2c  282,  272,  609,
3b310 20 20 34 32 34 2c 20 20 33 30 34 2c 20 20 35 39    424,  304,  59
3b311 38 2c 20 20 20 38 37 2c 20 20 33 37 30 2c 0a 20  8,   87,  370,. 
3b312 2f 2a 20 20 31 30 35 30 20 2a 2f 20 20 20 34 30  /*  1050 */   40
3b313 39 2c 20 20 34 37 37 2c 20 20 34 31 32 2c 20 20  9,  477,  412,  
3b314 34 30 39 2c 20 20 36 30 38 2c 20 20 34 30 39 2c  409,  608,  409,
3b315 20 20 36 30 37 2c 20 20 36 30 32 2c 20 20 36 31    607,  602,  61
3b316 38 2c 20 20 36 31 38 2c 0a 20 2f 2a 20 20 31 30  8,  618,. /*  10
3b317 36 30 20 2a 2f 20 20 20 35 39 38 2c 20 20 20 39  60 */   598,   9
3b318 39 2c 20 20 35 38 36 2c 20 20 35 38 35 2c 20 20  9,  586,  585,  
3b319 31 32 32 2c 20 20 34 31 32 2c 20 20 35 38 37 2c  122,  412,  587,
3b31a 20 20 35 38 31 2c 20 20 34 31 32 2c 20 20 36 31    581,  412,  61
3b31b 38 2c 0a 20 2f 2a 20 20 31 30 37 30 20 2a 2f 20  8,. /*  1070 */ 
3b31c 20 20 34 31 32 2c 20 20 36 31 38 2c 20 20 36 31    412,  618,  61
3b31d 38 2c 20 20 35 39 38 2c 20 20 20 38 36 2c 20 20  8,  598,   86,  
3b31e 33 36 36 2c 20 20 35 39 38 2c 20 20 20 31 37 2c  366,  598,   17,
3b31f 20 20 35 39 38 2c 20 20 20 38 35 2c 0a 20 2f 2a    598,   85,. /*
3b320 20 20 31 30 38 30 20 2a 2f 20 20 20 33 31 39 2c    1080 */   319,
3b321 20 20 31 38 35 2c 20 20 35 31 39 2c 20 20 35 31    185,  519,  51
3b322 38 2c 20 20 35 38 33 2c 20 20 35 38 32 2c 20 20  8,  583,  582,  
3b323 20 35 38 2c 20 20 20 34 38 2c 20 20 35 37 39 2c   58,   48,  579,
3b324 20 20 35 37 38 2c 0a 20 2f 2a 20 20 31 30 39 30    578,. /*  1090
3b325 20 2a 2f 20 20 20 35 38 30 2c 20 20 35 38 30 2c   */   580,  580,
3b326 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35     55,   55,   5
3b327 36 2c 20 20 20 35 36 2c 20 20 20 35 36 2c 20 20  6,   56,   56,  
3b328 20 35 36 2c 20 20 34 30 39 2c 20 20 20 35 34 2c   56,  409,   54,
3b329 0a 20 2f 2a 20 20 31 31 30 30 20 2a 2f 20 20 20  . /*  1100 */   
3b32a 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 34 2c   54,   54,   54,
3b32b 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
3b32c 32 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20  2,   52,   52,  
3b32d 20 35 31 2c 20 20 32 33 33 2c 0a 20 2f 2a 20 20   51,  233,. /*  
3b32e 31 31 31 30 20 2a 2f 20 20 20 33 30 39 2c 20 20  1110 */   309,  
3b32f 35 38 34 2c 20 20 34 30 39 2c 20 20 34 31 32 2c  584,  409,  412,
3b330 20 20 34 30 39 2c 20 20 32 36 30 2c 20 20 32 36    409,  260,  26
3b331 30 2c 20 20 32 36 30 2c 20 20 34 30 38 2c 20 20  0,  260,  408,  
3b332 35 39 31 2c 0a 20 2f 2a 20 20 31 31 32 30 20 2a  591,. /*  1120 *
3b333 2f 20 20 20 34 37 34 2c 20 20 35 39 38 2c 20 20  /   474,  598,  
3b334 20 38 34 2c 20 20 31 37 30 2c 20 20 34 30 39 2c   84,  170,  409,
3b335 20 20 34 36 36 2c 20 20 35 31 38 2c 20 20 34 31    466,  518,  41
3b336 32 2c 20 20 31 32 31 2c 20 20 34 31 32 2c 0a 20  2,  121,  412,. 
3b337 2f 2a 20 20 31 31 33 30 20 2a 2f 20 20 20 36 31  /*  1130 */   61
3b338 38 2c 20 20 36 31 38 2c 20 20 36 31 38 2c 20 20  8,  618,  618,  
3b339 36 31 38 2c 20 20 36 31 38 2c 20 20 35 39 38 2c  618,  618,  598,
3b33a 20 20 20 38 33 2c 20 20 35 39 38 2c 20 20 20 37     83,  598,   7
3b33b 32 2c 20 20 34 31 32 2c 0a 20 2f 2a 20 20 31 31  2,  412,. /*  11
3b33c 34 30 20 2a 2f 20 20 20 35 38 37 2c 20 20 35 38  40 */   587,  58
3b33d 31 2c 20 20 20 35 31 2c 20 20 32 33 33 2c 20 20  1,   51,  233,  
3b33e 36 32 35 2c 20 20 33 32 39 2c 20 20 34 37 30 2c  625,  329,  470,
3b33f 20 20 35 39 38 2c 20 20 20 37 31 2c 20 20 32 35    598,   71,  25
3b340 37 2c 0a 20 2f 2a 20 20 31 31 35 30 20 2a 2f 20  7,. /*  1150 */ 
3b341 20 20 31 35 39 2c 20 20 31 32 30 2c 20 20 20 31    159,  120,   1
3b342 34 2c 20 20 34 36 32 2c 20 20 31 35 37 2c 20 20  4,  462,  157,  
3b343 31 35 38 2c 20 20 31 31 37 2c 20 20 32 36 30 2c  158,  117,  260,
3b344 20 20 34 34 38 2c 20 20 34 34 37 2c 0a 20 2f 2a    448,  447,. /*
3b345 20 20 31 31 36 30 20 2a 2f 20 20 20 34 34 36 2c    1160 */   446,
3b346 20 20 20 34 38 2c 20 20 35 37 39 2c 20 20 35 37     48,  579,  57
3b347 38 2c 20 20 35 38 30 2c 20 20 35 38 30 2c 20 20  8,  580,  580,  
3b348 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 36 2c   55,   55,   56,
3b349 20 20 20 35 36 2c 0a 20 2f 2a 20 20 31 31 37 30     56,. /*  1170
3b34a 20 2a 2f 20 20 20 20 35 36 2c 20 20 20 35 36 2c   */    56,   56,
3b34b 20 20 36 31 38 2c 20 20 20 35 34 2c 20 20 20 35    618,   54,   5
3b34c 34 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20  4,   54,   54,  
3b34d 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c   53,   53,   52,
3b34e 0a 20 2f 2a 20 20 31 31 38 30 20 2a 2f 20 20 20  . /*  1180 */   
3b34f 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c   52,   52,   51,
3b350 20 20 32 33 33 2c 20 20 20 34 34 2c 20 20 34 30    233,   44,  40
3b351 33 2c 20 20 32 36 30 2c 20 20 20 20 33 2c 20 20  3,  260,    3,  
3b352 34 30 39 2c 20 20 34 35 39 2c 0a 20 2f 2a 20 20  409,  459,. /*  
3b353 31 31 39 30 20 2a 2f 20 20 20 32 36 30 2c 20 20  1190 */   260,  
3b354 34 31 33 2c 20 20 36 31 39 2c 20 20 31 31 38 2c  413,  619,  118,
3b355 20 20 33 39 38 2c 20 20 20 31 30 2c 20 20 20 32    398,   10,   2
3b356 35 2c 20 20 20 32 34 2c 20 20 35 35 34 2c 20 20  5,   24,  554,  
3b357 33 34 38 2c 0a 20 2f 2a 20 20 31 32 30 30 20 2a  348,. /*  1200 *
3b358 2f 20 20 20 32 31 37 2c 20 20 36 31 38 2c 20 20  /   217,  618,  
3b359 34 30 36 2c 20 20 34 31 32 2c 20 20 34 30 39 2c  406,  412,  409,
3b35a 20 20 36 31 38 2c 20 20 20 20 34 2c 20 20 20 34    618,    4,   4
3b35b 34 2c 20 20 34 30 33 2c 20 20 36 31 38 2c 0a 20  4,  403,  618,. 
3b35c 2f 2a 20 20 31 32 31 30 20 2a 2f 20 20 20 20 20  /*  1210 */     
3b35d 33 2c 20 20 35 39 38 2c 20 20 20 38 32 2c 20 20  3,  598,   82,  
3b35e 36 31 38 2c 20 20 34 31 33 2c 20 20 36 31 39 2c  618,  413,  619,
3b35f 20 20 34 35 35 2c 20 20 35 34 32 2c 20 20 31 31    455,  542,  11
3b360 35 2c 20 20 34 31 32 2c 0a 20 2f 2a 20 20 31 32  5,  412,. /*  12
3b361 32 30 20 2a 2f 20 20 20 35 33 38 2c 20 20 34 30  20 */   538,  40
3b362 31 2c 20 20 35 33 36 2c 20 20 32 37 34 2c 20 20  1,  536,  274,  
3b363 35 30 36 2c 20 20 34 30 36 2c 20 20 32 35 31 2c  506,  406,  251,
3b364 20 20 35 39 38 2c 20 20 20 38 31 2c 20 20 32 31    598,   81,  21
3b365 36 2c 0a 20 2f 2a 20 20 31 32 33 30 20 2a 2f 20  6,. /*  1230 */ 
3b366 20 20 32 37 33 2c 20 20 35 36 33 2c 20 20 36 31    273,  563,  61
3b367 38 2c 20 20 32 34 33 2c 20 20 34 35 33 2c 20 20  8,  243,  453,  
3b368 36 31 38 2c 20 20 31 35 34 2c 20 20 36 31 38 2c  618,  154,  618,
3b369 20 20 36 31 38 2c 20 20 36 31 38 2c 0a 20 2f 2a    618,  618,. /*
3b36a 20 20 31 32 34 30 20 2a 2f 20 20 20 34 34 39 2c    1240 */   449,
3b36b 20 20 34 31 36 2c 20 20 36 32 33 2c 20 20 31 31    416,  623,  11
3b36c 30 2c 20 20 34 30 31 2c 20 20 36 31 38 2c 20 20  0,  401,  618,  
3b36d 34 30 39 2c 20 20 32 33 36 2c 20 20 20 36 34 2c  409,  236,   64,
3b36e 20 20 31 32 33 2c 0a 20 2f 2a 20 20 31 32 35 30    123,. /*  1250
3b36f 20 2a 2f 20 20 20 34 38 37 2c 20 20 20 34 31 2c   */   487,   41,
3b370 20 20 20 34 32 2c 20 20 35 33 31 2c 20 20 35 36     42,  531,  56
3b371 33 2c 20 20 32 30 34 2c 20 20 34 30 39 2c 20 20  3,  204,  409,  
3b372 32 36 37 2c 20 20 20 34 33 2c 20 20 34 31 31 2c  267,   43,  411,
3b373 0a 20 2f 2a 20 20 31 32 36 30 20 2a 2f 20 20 20  . /*  1260 */   
3b374 34 31 30 2c 20 20 34 31 32 2c 20 20 32 36 35 2c  410,  412,  265,
3b375 20 20 35 39 32 2c 20 20 31 30 38 2c 20 20 36 31    592,  108,  61
3b376 38 2c 20 20 31 30 37 2c 20 20 34 33 34 2c 20 20  8,  107,  434,  
3b377 33 33 32 2c 20 20 35 39 38 2c 0a 20 2f 2a 20 20  332,  598,. /*  
3b378 31 32 37 30 20 2a 2f 20 20 20 20 38 30 2c 20 20  1270 */    80,  
3b379 34 31 32 2c 20 20 36 31 38 2c 20 20 32 36 33 2c  412,  618,  263,
3b37a 20 20 20 34 31 2c 20 20 20 34 32 2c 20 20 34 34     41,   42,  44
3b37b 33 2c 20 20 36 31 38 2c 20 20 34 30 39 2c 20 20  3,  618,  409,  
3b37c 35 39 38 2c 0a 20 2f 2a 20 20 31 32 38 30 20 2a  598,. /*  1280 *
3b37d 2f 20 20 20 20 37 30 2c 20 20 20 34 33 2c 20 20  /    70,   43,  
3b37e 34 31 31 2c 20 20 34 31 30 2c 20 20 34 33 33 2c  411,  410,  433,
3b37f 20 20 32 36 31 2c 20 20 35 39 32 2c 20 20 31 34    261,  592,  14
3b380 39 2c 20 20 36 31 38 2c 20 20 35 39 37 2c 0a 20  9,  618,  597,. 
3b381 2f 2a 20 20 31 32 39 30 20 2a 2f 20 20 20 32 35  /*  1290 */   25
3b382 36 2c 20 20 32 33 37 2c 20 20 31 38 38 2c 20 20  6,  237,  188,  
3b383 34 31 32 2c 20 20 35 39 30 2c 20 20 35 39 30 2c  412,  590,  590,
3b384 20 20 35 39 30 2c 20 20 35 38 39 2c 20 20 35 38    590,  589,  58
3b385 38 2c 20 20 20 31 33 2c 0a 20 2f 2a 20 20 31 33  8,   13,. /*  13
3b386 30 30 20 2a 2f 20 20 20 36 31 38 2c 20 20 35 39  00 */   618,  59
3b387 38 2c 20 20 20 31 38 2c 20 20 33 32 38 2c 20 20  8,   18,  328,  
3b388 32 33 35 2c 20 20 36 31 38 2c 20 20 20 34 34 2c  235,  618,   44,
3b389 20 20 34 30 33 2c 20 20 33 36 30 2c 20 20 20 20    403,  360,    
3b38a 33 2c 0a 20 2f 2a 20 20 31 33 31 30 20 2a 2f 20  3,. /*  1310 */ 
3b38b 20 20 34 31 38 2c 20 20 34 36 31 2c 20 20 33 33    418,  461,  33
3b38c 39 2c 20 20 34 31 33 2c 20 20 36 31 39 2c 20 20  9,  413,  619,  
3b38d 32 32 37 2c 20 20 31 32 34 2c 20 20 35 39 30 2c  227,  124,  590,
3b38e 20 20 35 39 30 2c 20 20 35 39 30 2c 0a 20 2f 2a    590,  590,. /*
3b38f 20 20 31 33 32 30 20 2a 2f 20 20 20 35 38 39 2c    1320 */   589,
3b390 20 20 35 38 38 2c 20 20 20 31 33 2c 20 20 36 31    588,   13,  61
3b391 38 2c 20 20 34 30 36 2c 20 20 34 30 39 2c 20 20  8,  406,  409,  
3b392 36 31 38 2c 20 20 34 30 39 2c 20 20 31 33 39 2c  618,  409,  139,
3b393 20 20 20 33 34 2c 0a 20 2f 2a 20 20 31 33 33 30     34,. /*  1330
3b394 20 2a 2f 20 20 20 34 30 33 2c 20 20 33 38 37 2c   */   403,  387,
3b395 20 20 20 20 33 2c 20 20 31 34 38 2c 20 20 36 32      3,  148,  62
3b396 32 2c 20 20 33 31 32 2c 20 20 34 31 33 2c 20 20  2,  312,  413,  
3b397 36 31 39 2c 20 20 33 31 31 2c 20 20 33 33 30 2c  619,  311,  330,
3b398 0a 20 2f 2a 20 20 31 33 34 30 20 2a 2f 20 20 20  . /*  1340 */   
3b399 34 31 32 2c 20 20 34 36 30 2c 20 20 34 31 32 2c  412,  460,  412,
3b39a 20 20 34 30 31 2c 20 20 31 38 30 2c 20 20 33 35    401,  180,  35
3b39b 33 2c 20 20 34 31 32 2c 20 20 34 30 36 2c 20 20  3,  412,  406,  
3b39c 35 39 38 2c 20 20 20 37 39 2c 0a 20 2f 2a 20 20  598,   79,. /*  
3b39d 31 33 35 30 20 2a 2f 20 20 20 35 39 38 2c 20 20  1350 */   598,  
3b39e 20 37 38 2c 20 20 32 35 30 2c 20 20 35 36 33 2c   78,  250,  563,
3b39f 20 20 35 39 38 2c 20 20 20 20 39 2c 20 20 36 31    598,    9,  61
3b3a0 38 2c 20 20 36 31 32 2c 20 20 36 31 31 2c 20 20  8,  612,  611,  
3b3a1 36 31 30 2c 0a 20 2f 2a 20 20 31 33 36 30 20 2a  610,. /*  1360 *
3b3a2 2f 20 20 20 36 31 38 2c 20 20 20 20 38 2c 20 20  /   618,    8,  
3b3a3 34 35 32 2c 20 20 34 34 32 2c 20 20 32 34 32 2c  452,  442,  242,
3b3a4 20 20 34 31 35 2c 20 20 34 30 31 2c 20 20 36 31    415,  401,  61
3b3a5 38 2c 20 20 32 33 39 2c 20 20 32 33 35 2c 0a 20  8,  239,  235,. 
3b3a6 2f 2a 20 20 31 33 37 30 20 2a 2f 20 20 20 31 37  /*  1370 */   17
3b3a7 39 2c 20 20 32 33 38 2c 20 20 34 32 38 2c 20 20  9,  238,  428,  
3b3a8 20 34 31 2c 20 20 20 34 32 2c 20 20 32 38 38 2c   41,   42,  288,
3b3a9 20 20 35 36 33 2c 20 20 36 31 38 2c 20 20 36 31    563,  618,  61
3b3aa 38 2c 20 20 36 31 38 2c 0a 20 2f 2a 20 20 31 33  8,  618,. /*  13
3b3ab 38 30 20 2a 2f 20 20 20 20 34 33 2c 20 20 34 31  80 */    43,  41
3b3ac 31 2c 20 20 34 31 30 2c 20 20 36 31 38 2c 20 20  1,  410,  618,  
3b3ad 31 34 34 2c 20 20 35 39 32 2c 20 20 36 31 38 2c  144,  592,  618,
3b3ae 20 20 36 31 38 2c 20 20 31 37 37 2c 20 20 20 36    618,  177,   6
3b3af 31 2c 0a 20 2f 2a 20 20 31 33 39 30 20 2a 2f 20  1,. /*  1390 */ 
3b3b0 20 20 36 31 38 2c 20 20 35 39 36 2c 20 20 33 39    618,  596,  39
3b3b1 31 2c 20 20 36 32 30 2c 20 20 36 31 39 2c 20 20  1,  620,  619,  
3b3b2 32 38 37 2c 20 20 20 34 31 2c 20 20 20 34 32 2c  287,   41,   42,
3b3b3 20 20 34 31 34 2c 20 20 36 31 38 2c 0a 20 2f 2a    414,  618,. /*
3b3b4 20 20 31 34 30 30 20 2a 2f 20 20 20 32 39 33 2c    1400 */   293,
3b3b5 20 20 20 33 30 2c 20 20 33 39 33 2c 20 20 20 34     30,  393,   4
3b3b6 33 2c 20 20 34 31 31 2c 20 20 34 31 30 2c 20 20  3,  411,  410,  
3b3b7 32 39 32 2c 20 20 36 31 38 2c 20 20 35 39 32 2c  292,  618,  592,
3b3b8 20 20 20 33 31 2c 0a 20 2f 2a 20 20 31 34 31 30     31,. /*  1410
3b3b9 20 2a 2f 20 20 20 36 31 38 2c 20 20 33 39 35 2c   */   618,  395,
3b3ba 20 20 32 39 31 2c 20 20 20 36 30 2c 20 20 32 33    291,   60,  23
3b3bb 30 2c 20 20 20 33 37 2c 20 20 35 39 30 2c 20 20  0,   37,  590,  
3b3bc 35 39 30 2c 20 20 35 39 30 2c 20 20 35 38 39 2c  590,  590,  589,
3b3bd 0a 20 2f 2a 20 20 31 34 32 30 20 2a 2f 20 20 20  . /*  1420 */   
3b3be 35 38 38 2c 20 20 20 31 33 2c 20 20 32 31 34 2c  588,   13,  214,
3b3bf 20 20 35 35 33 2c 20 20 31 38 33 2c 20 20 32 39    553,  183,  29
3b3c0 30 2c 20 20 31 37 32 2c 20 20 33 30 31 2c 20 20  0,  172,  301,  
3b3c1 33 30 30 2c 20 20 32 39 39 2c 0a 20 2f 2a 20 20  300,  299,. /*  
3b3c2 31 34 33 30 20 2a 2f 20 20 20 31 37 38 2c 20 20  1430 */   178,  
3b3c3 32 39 37 2c 20 20 35 39 35 2c 20 20 35 36 33 2c  297,  595,  563,
3b3c4 20 20 34 35 31 2c 20 20 20 32 39 2c 20 20 32 38    451,   29,  28
3b3c5 35 2c 20 20 33 39 30 2c 20 20 35 34 30 2c 20 20  5,  390,  540,  
3b3c6 35 39 30 2c 0a 20 2f 2a 20 20 31 34 34 30 20 2a  590,. /*  1440 *
3b3c7 2f 20 20 20 35 39 30 2c 20 20 35 39 30 2c 20 20  /   590,  590,  
3b3c8 35 38 39 2c 20 20 35 38 38 2c 20 20 20 31 33 2c  589,  588,   13,
3b3c9 20 20 32 38 33 2c 20 20 35 32 30 2c 20 20 35 33    283,  520,  53
3b3ca 34 2c 20 20 31 35 30 2c 20 20 35 33 33 2c 0a 20  4,  150,  533,. 
3b3cb 2f 2a 20 20 31 34 35 30 20 2a 2f 20 20 20 32 34  /*  1450 */   24
3b3cc 31 2c 20 20 32 38 31 2c 20 20 33 38 34 2c 20 20  1,  281,  384,  
3b3cd 31 39 32 2c 20 20 31 39 31 2c 20 20 33 32 34 2c  192,  191,  324,
3b3ce 20 20 35 31 35 2c 20 20 35 31 34 2c 20 20 32 37    515,  514,  27
3b3cf 36 2c 20 20 32 34 30 2c 0a 20 2f 2a 20 20 31 34  6,  240,. /*  14
3b3d0 36 30 20 2a 2f 20 20 20 35 31 30 2c 20 20 35 32  60 */   510,  52
3b3d1 33 2c 20 20 33 30 37 2c 20 20 35 31 31 2c 20 20  3,  307,  511,  
3b3d2 31 32 38 2c 20 20 35 39 32 2c 20 20 35 30 39 2c  128,  592,  509,
3b3d3 20 20 32 32 35 2c 20 20 32 32 36 2c 20 20 34 38    225,  226,  48
3b3d4 36 2c 0a 20 2f 2a 20 20 31 34 37 30 20 2a 2f 20  6,. /*  1470 */ 
3b3d5 20 20 34 38 35 2c 20 20 32 32 34 2c 20 20 31 35    485,  224,  15
3b3d6 32 2c 20 20 34 39 31 2c 20 20 34 36 34 2c 20 20  2,  491,  464,  
3b3d7 33 30 36 2c 20 20 34 38 34 2c 20 20 31 36 33 2c  306,  484,  163,
3b3d8 20 20 31 35 33 2c 20 20 33 37 31 2c 0a 20 2f 2a    153,  371,. /*
3b3d9 20 20 31 34 38 30 20 2a 2f 20 20 20 34 37 38 2c    1480 */   478,
3b3da 20 20 31 35 31 2c 20 20 31 36 32 2c 20 20 32 35    151,  162,  25
3b3db 38 2c 20 20 33 36 39 2c 20 20 31 36 31 2c 20 20  8,  369,  161,  
3b3dc 33 36 37 2c 20 20 32 30 38 2c 20 20 34 37 35 2c  367,  208,  475,
3b3dd 20 20 34 37 36 2c 0a 20 2f 2a 20 20 31 34 39 30    476,. /*  1490
3b3de 20 2a 2f 20 20 20 20 32 36 2c 20 20 31 36 30 2c   */    26,  160,
3b3df 20 20 34 36 35 2c 20 20 31 34 30 2c 20 20 33 36    465,  140,  36
3b3e0 31 2c 20 20 31 33 31 2c 20 20 35 39 30 2c 20 20  1,  131,  590,  
3b3e1 35 39 30 2c 20 20 35 39 30 2c 20 20 31 31 36 2c  590,  590,  116,
3b3e2 0a 20 2f 2a 20 20 31 35 30 30 20 2a 2f 20 20 20  . /*  1500 */   
3b3e3 31 31 39 2c 20 20 34 35 34 2c 20 20 33 34 33 2c  119,  454,  343,
3b3e4 20 20 31 35 35 2c 20 20 31 31 34 2c 20 20 33 34    155,  114,  34
3b3e5 32 2c 20 20 31 31 33 2c 20 20 31 31 32 2c 20 20  2,  113,  112,  
3b3e6 34 34 35 2c 20 20 31 31 31 2c 0a 20 2f 2a 20 20  445,  111,. /*  
3b3e7 31 35 31 30 20 2a 2f 20 20 20 31 33 30 2c 20 20  1510 */   130,  
3b3e8 31 30 39 2c 20 20 34 33 31 2c 20 20 33 31 36 2c  109,  431,  316,
3b3e9 20 20 34 32 36 2c 20 20 34 33 30 2c 20 20 20 32    426,  430,   2
3b3ea 33 2c 20 20 34 32 39 2c 20 20 20 32 30 2c 20 20  3,  429,   20,  
3b3eb 36 30 36 2c 0a 20 2f 2a 20 20 31 35 32 30 20 2a  606,. /*  1520 *
3b3ec 2f 20 20 20 31 39 30 2c 20 20 35 30 37 2c 20 20  /   190,  507,  
3b3ed 32 35 35 2c 20 20 33 34 31 2c 20 20 32 34 34 2c  255,  341,  244,
3b3ee 20 20 20 36 33 2c 20 20 32 39 34 2c 20 20 35 39     63,  294,  59
3b3ef 33 2c 20 20 33 31 30 2c 20 20 35 37 30 2c 0a 20  3,  310,  570,. 
3b3f0 2f 2a 20 20 31 35 33 30 20 2a 2f 20 20 20 32 37  /*  1530 */   27
3b3f1 37 2c 20 20 34 30 32 2c 20 20 33 35 34 2c 20 20  7,  402,  354,  
3b3f2 32 33 35 2c 20 20 35 36 37 2c 20 20 34 39 36 2c  235,  567,  496,
3b3f3 20 20 34 39 35 2c 20 20 34 39 32 2c 20 20 34 39    495,  492,  49
3b3f4 34 2c 20 20 33 30 32 2c 0a 20 2f 2a 20 20 31 35  4,  302,. /*  15
3b3f5 34 30 20 2a 2f 20 20 20 34 35 38 2c 20 20 33 37  40 */   458,  37
3b3f6 38 2c 20 20 32 38 36 2c 20 20 32 34 35 2c 20 20  8,  286,  245,  
3b3f7 35 36 36 2c 20 20 20 20 35 2c 20 20 32 35 32 2c  566,    5,  252,
3b3f8 20 20 35 34 37 2c 20 20 31 39 33 2c 20 20 34 34    547,  193,  44
3b3f9 34 2c 0a 20 2f 2a 20 20 31 35 35 30 20 2a 2f 20  4,. /*  1550 */ 
3b3fa 20 20 32 33 33 2c 20 20 33 34 30 2c 20 20 32 30    233,  340,  20
3b3fb 37 2c 20 20 35 32 34 2c 20 20 33 36 38 2c 20 20  7,  524,  368,  
3b3fc 35 30 35 2c 20 20 33 33 34 2c 20 20 35 32 32 2c  505,  334,  522,
3b3fd 20 20 34 39 39 2c 20 20 33 39 39 2c 0a 20 2f 2a    499,  399,. /*
3b3fe 20 20 31 35 36 30 20 2a 2f 20 20 20 32 39 35 2c    1560 */   295,
3b3ff 20 20 34 39 38 2c 20 20 39 35 36 2c 20 20 34 38    498,  956,  48
3b400 38 2c 0a 7d 3b 0a 73 74 61 74 69 63 20 63 6f 6e  8,.};.static con
3b401 73 74 20 59 59 43 4f 44 45 54 59 50 45 20 79 79  st YYCODETYPE yy
3b402 5f 6c 6f 6f 6b 61 68 65 61 64 5b 5d 20 3d 20 7b  _lookahead[] = {
3b403 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20  . /*     0 */   
3b404 20 31 39 2c 20 20 31 34 32 2c 20 20 31 34 33 2c   19,  142,  143,
3b405 20 20 31 34 34 2c 20 20 31 34 35 2c 20 20 20 32    144,  145,   2
3b406 34 2c 20 20 20 20 31 2c 20 20 20 32 36 2c 20 20  4,    1,   26,  
3b407 20 37 37 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20   77,   78,. /*  
3b408 20 20 31 30 20 2a 2f 20 20 20 20 37 39 2c 20 20    10 */    79,  
3b409 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
3b40a 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
3b40b 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
3b40c 20 38 38 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a   88,. /*    20 *
3b40d 2f 20 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20  /    89,   90,  
3b40e 20 39 31 2c 20 20 20 39 32 2c 20 20 20 32 36 2c   91,   92,   26,
3b40f 20 20 20 32 37 2c 20 20 20 31 35 2c 20 20 20 32     27,   15,   2
3b410 36 2c 20 20 20 32 37 2c 20 20 31 39 37 2c 0a 20  6,   27,  197,. 
3b411 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 20 34  /*    30 */    4
3b412 39 2c 20 20 20 35 30 2c 20 20 20 37 37 2c 20 20  9,   50,   77,  
3b413 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
3b414 20 20 32 30 34 2c 20 20 20 38 32 2c 20 20 20 38    204,   82,   8
3b415 33 2c 20 20 20 38 34 2c 0a 20 2f 2a 20 20 20 20  3,   84,. /*    
3b416 34 30 20 2a 2f 20 20 20 20 38 35 2c 20 20 20 38  40 */    85,   8
3b417 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  6,   87,   88,  
3b418 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c   89,   90,   91,
3b419 20 20 20 39 32 2c 20 20 20 32 33 2c 20 20 20 36     92,   23,   6
3b41a 38 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20  8,. /*    50 */ 
3b41b 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37     69,   70,   7
3b41c 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20  1,   72,   73,  
3b41d 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c   74,   75,   76,
3b41e 20 20 20 37 37 2c 20 20 20 37 38 2c 0a 20 2f 2a     77,   78,. /*
3b41f 20 20 20 20 36 30 20 2a 2f 20 20 20 20 37 39 2c      60 */    79,
3b420 20 20 20 38 30 2c 20 20 31 36 36 2c 20 20 20 38     80,  166,   8
3b421 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
3b422 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
3b423 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 20 37 30     88,. /*    70
3b424 20 2a 2f 20 20 20 20 38 39 2c 20 20 20 39 30 2c   */    89,   90,
3b425 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31     91,   92,   1
3b426 39 2c 20 20 20 39 34 2c 20 20 20 31 39 2c 20 20  9,   94,   19,  
3b427 31 30 35 2c 20 20 31 30 36 2c 20 20 31 30 37 2c  105,  106,  107,
3b428 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20  . /*    80 */   
3b429 20 32 35 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   25,   82,   83,
3b42a 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
3b42b 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  6,   87,   88,  
3b42c 20 38 39 2c 20 20 20 39 30 2c 0a 20 2f 2a 20 20   89,   90,. /*  
3b42d 20 20 39 30 20 2a 2f 20 20 20 20 39 31 2c 20 20    90 */    91,  
3b42e 20 39 32 2c 20 20 20 39 34 2c 20 20 20 39 35 2c   92,   94,   95,
3b42f 20 20 20 39 36 2c 20 20 20 39 34 2c 20 20 20 39     96,   94,   9
3b430 35 2c 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20  5,   99,  100,  
3b431 31 30 31 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a  101,. /*   100 *
3b432 2f 20 20 20 31 31 32 2c 20 20 32 30 35 2c 20 20  /   112,  205,  
3b433 31 31 34 2c 20 20 31 31 35 2c 20 20 20 34 39 2c  114,  115,   49,
3b434 20 20 20 35 30 2c 20 20 20 32 32 2c 20 20 20 32     50,   22,   2
3b435 33 2c 20 20 31 31 30 2c 20 20 20 35 34 2c 0a 20  3,  110,   54,. 
3b436 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 20 38  /*   110 */    8
3b437 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  6,   87,   88,  
3b438 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c   89,   90,   91,
3b439 20 20 20 39 32 2c 20 20 32 32 31 2c 20 20 32 32     92,  221,  22
3b43a 32 2c 20 20 32 32 33 2c 0a 20 2f 2a 20 20 20 31  2,  223,. /*   1
3b43b 32 30 20 2a 2f 20 20 20 20 32 33 2c 20 20 31 32  20 */    23,  12
3b43c 30 2c 20 20 20 32 35 2c 20 20 20 36 38 2c 20 20  0,   25,   68,  
3b43d 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
3b43e 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37     72,   73,   7
3b43f 34 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20  4,. /*   130 */ 
3b440 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
3b441 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
3b442 20 38 30 2c 20 20 20 32 32 2c 20 20 20 38 32 2c   80,   22,   82,
3b443 20 20 20 38 33 2c 20 20 20 38 34 2c 0a 20 2f 2a     83,   84,. /*
3b444 20 20 20 31 34 30 20 2a 2f 20 20 20 20 38 35 2c     140 */    85,
3b445 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38     86,   87,   8
3b446 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20  8,   89,   90,  
3b447 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c   91,   92,   19,
3b448 20 20 20 39 32 2c 0a 20 2f 2a 20 20 20 31 35 30     92,. /*   150
3b449 20 2a 2f 20 20 20 20 32 33 2c 20 20 20 36 37 2c   */    23,   67,
3b44a 20 20 20 32 35 2c 20 20 20 39 36 2c 20 20 20 39     25,   96,   9
3b44b 37 2c 20 20 20 39 38 2c 20 20 20 39 39 2c 20 20  7,   98,   99,  
3b44c 31 30 30 2c 20 20 31 30 31 2c 20 20 31 30 32 2c  100,  101,  102,
3b44d 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20  . /*   160 */   
3b44e 31 35 30 2c 20 20 20 33 32 2c 20 20 31 35 30 2c  150,   32,  150,
3b44f 20 20 31 31 38 2c 20 20 20 32 36 2c 20 20 20 32    118,   26,   2
3b450 37 2c 20 20 31 30 39 2c 20 20 31 35 30 2c 20 20  7,  109,  150,  
3b451 31 35 30 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20  150,  150,. /*  
3b452 20 31 37 30 20 2a 2f 20 20 20 20 34 31 2c 20 20   170 */    41,  
3b453 31 36 31 2c 20 20 31 36 32 2c 20 20 31 38 30 2c  161,  162,  180,
3b454 20 20 31 38 31 2c 20 20 31 36 35 2c 20 20 31 31    181,  165,  11
3b455 33 2c 20 20 31 36 35 2c 20 20 20 34 39 2c 20 20  3,  165,   49,  
3b456 20 35 30 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a   50,. /*   180 *
3b457 2f 20 20 20 31 31 37 2c 20 20 31 38 38 2c 20 20  /   117,  188,  
3b458 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c  165,  165,  165,
3b459 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20 31 37    173,  174,  17
3b45a 30 2c 20 20 31 37 31 2c 20 20 31 37 30 2c 0a 20  0,  171,  170,. 
3b45b 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 31 37  /*   190 */   17
3b45c 31 2c 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20  1,  173,  174,  
3b45d 31 31 38 2c 20 20 31 38 34 2c 20 20 20 31 36 2c  118,  184,   16,
3b45e 20 20 31 38 36 2c 20 20 20 36 38 2c 20 20 20 36    186,   68,   6
3b45f 39 2c 20 20 20 37 30 2c 0a 20 2f 2a 20 20 20 32  9,   70,. /*   2
3b460 30 30 20 2a 2f 20 20 20 20 37 31 2c 20 20 20 37  00 */    71,   7
3b461 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  2,   73,   74,  
3b462 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
3b463 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38     78,   79,   8
3b464 30 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20  0,. /*   210 */ 
3b465 20 20 31 31 38 2c 20 20 20 38 32 2c 20 20 20 38    118,   82,   8
3b466 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
3b467 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
3b468 20 20 20 38 39 2c 20 20 20 39 30 2c 0a 20 2f 2a     89,   90,. /*
3b469 20 20 20 32 32 30 20 2a 2f 20 20 20 20 39 31 2c     220 */    91,
3b46a 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20 20 39     92,   19,   9
3b46b 38 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  8,   86,   87,  
3b46c 20 32 32 2c 20 20 20 32 34 2c 20 20 31 36 30 2c   22,   24,  160,
3b46d 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 32 33 30     88,. /*   230
3b46e 20 2a 2f 20 20 20 20 32 36 2c 20 20 20 32 37 2c   */    26,   27,
3b46f 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 31 30     94,   95,  10
3b470 39 2c 20 20 20 39 37 2c 20 20 32 32 34 2c 20 20  9,   97,  224,  
3b471 20 36 36 2c 20 20 31 31 38 2c 20 20 20 36 30 2c   66,  118,   60,
3b472 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 20  . /*   240 */   
3b473 31 35 30 2c 20 20 20 36 32 2c 20 20 31 30 34 2c  150,   62,  104,
3b474 20 20 20 32 33 2c 20 20 31 30 36 2c 20 20 20 32     23,  106,   2
3b475 35 2c 20 20 32 32 39 2c 20 20 32 33 30 2c 20 20  5,  229,  230,  
3b476 32 32 39 2c 20 20 32 33 30 2c 0a 20 2f 2a 20 20  229,  230,. /*  
3b477 20 32 35 30 20 2a 2f 20 20 20 31 36 30 2c 20 20   250 */   160,  
3b478 31 35 30 2c 20 20 20 34 39 2c 20 20 20 35 30 2c  150,   49,   50,
3b479 20 20 31 31 33 2c 20 20 31 36 35 2c 20 20 20 39    113,  165,   9
3b47a 36 2c 20 20 20 32 36 2c 20 20 31 31 37 2c 20 20  6,   26,  117,  
3b47b 20 39 39 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a   99,. /*   260 *
3b47c 2f 20 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20  /   100,  101,  
3b47d 31 39 34 2c 20 20 31 37 33 2c 20 20 31 37 34 2c  194,  173,  174,
3b47e 20 20 20 39 34 2c 20 20 31 36 35 2c 20 20 31 32     94,  165,  12
3b47f 39 2c 20 20 31 33 30 2c 20 20 20 39 38 2c 0a 20  9,  130,   98,. 
3b480 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 20 31 31  /*   270 */   11
3b481 30 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  0,   68,   69,  
3b482 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
3b483 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
3b484 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20 20 32  5,   76,. /*   2
3b485 38 30 20 2a 2f 20 20 20 20 37 37 2c 20 20 20 37  80 */    77,   7
3b486 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20  8,   79,   80,  
3b487 31 39 34 2c 20 20 20 38 32 2c 20 20 20 38 33 2c  194,   82,   83,
3b488 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
3b489 36 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20  6,. /*   290 */ 
3b48a 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38     87,   88,   8
3b48b 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20  9,   90,   91,  
3b48c 20 39 32 2c 20 20 20 31 39 2c 20 20 20 31 31 2c   92,   19,   11,
3b48d 20 20 20 39 34 2c 20 20 20 39 35 2c 0a 20 2f 2a     94,   95,. /*
3b48e 20 20 20 33 30 30 20 2a 2f 20 20 20 31 32 39 2c     300 */   129,
3b48f 20 20 31 33 30 2c 20 20 31 33 31 2c 20 20 31 31    130,  131,  11
3b490 38 2c 20 20 31 35 30 2c 20 20 32 31 35 2c 20 20  8,  150,  215,  
3b491 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35 30 2c  150,  150,  150,
3b492 20 20 20 32 35 2c 0a 20 2f 2a 20 20 20 33 31 30     25,. /*   310
3b493 20 2a 2f 20 20 20 32 32 30 2c 20 20 20 32 36 2c   */   220,   26,
3b494 20 20 20 32 37 2c 20 20 20 32 32 2c 20 20 32 31     27,   22,  21
3b495 33 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  3,   26,   27,  
3b496 20 32 36 2c 20 20 20 32 37 2c 20 20 31 36 35 2c   26,   27,  165,
3b497 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 20  . /*   320 */   
3b498 20 32 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c   25,  165,  165,
3b499 20 20 31 36 35 2c 20 20 20 33 30 2c 20 20 20 39    165,   30,   9
3b49a 34 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20  4,   49,   50,  
3b49b 20 33 34 2c 20 20 31 37 33 2c 0a 20 2f 2a 20 20   34,  173,. /*  
3b49c 20 33 33 30 20 2a 2f 20 20 20 31 37 34 2c 20 20   330 */   174,  
3b49d 31 37 33 2c 20 20 31 37 34 2c 20 20 20 38 38 2c  173,  174,   88,
3b49e 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39     89,   90,   9
3b49f 31 2c 20 20 20 39 32 2c 20 20 20 20 37 2c 20 20  1,   92,    7,  
3b4a0 20 20 38 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a    8,. /*   340 *
3b4a1 2f 20 20 20 31 36 30 2c 20 20 31 38 37 2c 20 20  /   160,  187,  
3b4a2 20 34 38 2c 20 20 20 35 37 2c 20 20 31 38 37 2c   48,   57,  187,
3b4a3 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37     68,   69,   7
3b4a4 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 0a 20  0,   71,   72,. 
3b4a5 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 20 20 37  /*   350 */    7
3b4a6 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20  3,   74,   75,  
3b4a7 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   76,   77,   78,
3b4a8 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20 32     79,   80,   2
3b4a9 33 2c 20 20 20 38 32 2c 0a 20 2f 2a 20 20 20 33  3,   82,. /*   3
3b4aa 36 30 20 2a 2f 20 20 20 20 38 33 2c 20 20 20 38  60 */    83,   8
3b4ab 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  4,   85,   86,  
3b4ac 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   87,   88,   89,
3b4ad 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39     90,   91,   9
3b4ae 32 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20  2,. /*   370 */ 
3b4af 20 20 20 31 39 2c 20 20 32 31 35 2c 20 20 31 35     19,  215,  15
3b4b0 30 2c 20 20 32 31 35 2c 20 20 31 39 34 2c 20 20  0,  215,  194,  
3b4b1 20 31 39 2c 20 20 32 32 30 2c 20 20 20 38 38 2c   19,  220,   88,
3b4b2 20 20 32 32 30 2c 20 20 20 39 34 2c 0a 20 2f 2a    220,   94,. /*
3b4b3 20 20 20 33 38 30 20 2a 2f 20 20 20 20 39 35 2c     380 */    95,
3b4b4 20 20 20 32 33 2c 20 20 31 36 30 2c 20 20 20 39     23,  160,   9
3b4b5 34 2c 20 20 20 39 35 2c 20 20 20 39 34 2c 20 20  4,   95,   94,  
3b4b6 20 39 35 2c 20 20 31 36 35 2c 20 20 20 32 36 2c   95,  165,   26,
3b4b7 20 20 20 32 37 2c 0a 20 2f 2a 20 20 20 33 39 30     27,. /*   390
3b4b8 20 2a 2f 20 20 20 20 39 35 2c 20 20 31 30 35 2c   */    95,  105,
3b4b9 20 20 31 30 36 2c 20 20 31 30 37 2c 20 20 31 31    106,  107,  11
3b4ba 33 2c 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20  3,  173,  174,  
3b4bb 32 31 37 2c 20 20 20 32 32 2c 20 20 31 35 30 2c  217,   22,  150,
3b4bc 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20 20  . /*   400 */   
3b4bd 20 34 39 2c 20 20 20 35 30 2c 20 20 31 31 36 2c   49,   50,  116,
3b4be 20 20 31 31 39 2c 20 20 20 35 37 2c 20 20 31 32    119,   57,  12
3b4bf 30 2c 20 20 20 35 30 2c 20 20 31 35 38 2c 20 20  0,   50,  158,  
3b4c0 20 32 32 2c 20 20 20 32 31 2c 0a 20 2f 2a 20 20   22,   21,. /*  
3b4c1 20 34 31 30 20 2a 2f 20 20 20 31 36 31 2c 20 20   410 */   161,  
3b4c2 31 36 32 2c 20 20 32 33 32 2c 20 20 31 33 36 2c  162,  232,  136,
3b4c3 20 20 31 36 35 2c 20 20 31 32 30 2c 20 20 31 39    165,  120,  19
3b4c4 34 2c 20 20 32 33 37 2c 20 20 20 32 33 2c 20 20  4,  237,   23,  
3b4c5 20 36 38 2c 0a 20 2f 2a 20 20 20 34 32 30 20 2a   68,. /*   420 *
3b4c6 2f 20 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20  /    69,   70,  
3b4c7 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c   71,   72,   73,
3b4c8 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37     74,   75,   7
3b4c9 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0a 20  6,   77,   78,. 
3b4ca 2f 2a 20 20 20 34 33 30 20 2a 2f 20 20 20 20 37  /*   430 */    7
3b4cb 39 2c 20 20 20 38 30 2c 20 20 20 32 32 2c 20 20  9,   80,   22,  
3b4cc 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
3b4cd 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
3b4ce 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 34  7,   88,. /*   4
3b4cf 34 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20 39  40 */    89,   9
3b4d0 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20  0,   91,   92,  
3b4d1 20 31 39 2c 20 20 20 32 33 2c 20 20 20 31 32 2c   19,   23,   12,
3b4d2 20 20 31 31 32 2c 20 20 20 32 33 2c 20 20 31 31    112,   23,  11
3b4d3 34 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a 2f 20  4,. /*   450 */ 
3b4d4 20 20 31 31 35 2c 20 20 20 36 33 2c 20 20 31 30    115,   63,  10
3b4d5 35 2c 20 20 31 30 36 2c 20 20 31 30 37 2c 20 20  5,  106,  107,  
3b4d6 20 32 33 2c 20 20 20 39 34 2c 20 20 20 39 35 2c   23,   94,   95,
3b4d7 20 20 20 39 37 2c 20 20 20 39 38 2c 0a 20 2f 2a     97,   98,. /*
3b4d8 20 20 20 34 36 30 20 2a 2f 20 20 20 31 30 34 2c     460 */   104,
3b4d9 20 20 31 35 30 2c 20 20 20 32 38 2c 20 20 31 31    150,   28,  11
3b4da 36 2c 20 20 20 32 35 2c 20 20 31 30 39 2c 20 20  6,   25,  109,  
3b4db 31 35 30 2c 20 20 31 35 30 2c 20 20 20 32 33 2c  150,  150,   23,
3b4dc 20 20 20 32 33 2c 0a 20 2f 2a 20 20 20 34 37 30     23,. /*   470
3b4dd 20 2a 2f 20 20 20 31 31 32 2c 20 20 20 32 35 2c   */   112,   25,
3b4de 20 20 31 31 34 2c 20 20 31 31 35 2c 20 20 20 34    114,  115,   4
3b4df 39 2c 20 20 20 35 30 2c 20 20 31 36 35 2c 20 20  9,   50,  165,  
3b4e0 31 35 30 2c 20 20 20 34 34 2c 20 20 20 31 31 2c  150,   44,   11,
3b4e1 0a 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20 20 20  . /*   480 */   
3b4e2 20 34 36 2c 20 20 31 36 35 2c 20 20 31 36 35 2c   46,  165,  165,
3b4e3 20 20 20 31 36 2c 20 20 31 37 33 2c 20 20 31 37     16,  173,  17
3b4e4 34 2c 20 20 20 37 36 2c 20 20 31 33 36 2c 20 20  4,   76,  136,  
3b4e5 31 30 30 2c 20 20 31 37 33 2c 0a 20 2f 2a 20 20  100,  173,. /*  
3b4e6 20 34 39 30 20 2a 2f 20 20 20 31 37 34 2c 20 20   490 */   174,  
3b4e7 20 35 37 2c 20 20 31 36 35 2c 20 20 20 36 38 2c   57,  165,   68,
3b4e8 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37     69,   70,   7
3b4e9 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20  1,   72,   73,  
3b4ea 20 37 34 2c 0a 20 2f 2a 20 20 20 35 30 30 20 2a   74,. /*   500 *
3b4eb 2f 20 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  /    75,   76,  
3b4ec 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c   77,   78,   79,
3b4ed 20 20 20 38 30 2c 20 20 31 36 36 2c 20 20 20 38     80,  166,   8
3b4ee 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 0a 20  2,   83,   84,. 
3b4ef 2f 2a 20 20 20 35 31 30 20 2a 2f 20 20 20 20 38  /*   510 */    8
3b4f0 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
3b4f1 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
3b4f2 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31     91,   92,   1
3b4f3 39 2c 20 20 31 36 39 2c 0a 20 2f 2a 20 20 20 35  9,  169,. /*   5
3b4f4 32 30 20 2a 2f 20 20 20 31 37 30 2c 20 20 31 37  20 */   170,  17
3b4f5 31 2c 20 20 20 32 33 2c 20 20 20 31 32 2c 20 20  1,   23,   12,  
3b4f6 20 32 33 2c 20 20 32 31 34 2c 20 20 31 33 38 2c   23,  214,  138,
3b4f7 20 20 20 36 30 2c 20 20 31 35 30 2c 20 20 20 36     60,  150,   6
3b4f8 32 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a 2f 20  2,. /*   530 */ 
3b4f9 20 20 20 32 34 2c 20 20 32 31 35 2c 20 20 20 32     24,  215,   2
3b4fa 36 2c 20 20 32 31 36 2c 20 20 31 31 32 2c 20 20  6,  216,  112,  
3b4fb 31 35 30 2c 20 20 31 31 34 2c 20 20 31 31 35 2c  150,  114,  115,
3b4fc 20 20 20 33 36 2c 20 20 20 32 38 2c 0a 20 2f 2a     36,   28,. /*
3b4fd 20 20 20 35 34 30 20 2a 2f 20 20 20 32 31 33 2c     540 */   213,
3b4fe 20 20 20 39 35 2c 20 20 31 30 33 2c 20 20 31 36     95,  103,  16
3b4ff 35 2c 20 20 31 31 32 2c 20 20 32 30 35 2c 20 20  5,  112,  205,  
3b500 31 31 34 2c 20 20 31 31 35 2c 20 20 20 34 39 2c  114,  115,   49,
3b501 20 20 20 35 30 2c 0a 20 2f 2a 20 20 20 35 35 30     50,. /*   550
3b502 20 2a 2f 20 20 20 31 36 35 2c 20 20 31 37 33 2c   */   165,  173,
3b503 20 20 31 37 34 2c 20 20 20 35 31 2c 20 20 20 32    174,   51,   2
3b504 33 2c 20 20 20 34 34 2c 20 20 20 32 35 2c 20 20  3,   44,   25,  
3b505 20 34 36 2c 20 20 31 37 33 2c 20 20 31 37 34 2c   46,  173,  174,
3b506 0a 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20 20 20  . /*   560 */   
3b507 20 35 38 2c 20 20 20 32 32 2c 20 20 20 32 33 2c   58,   22,   23,
3b508 20 20 20 32 32 2c 20 20 20 32 35 2c 20 20 31 36     22,   25,  16
3b509 30 2c 20 20 31 32 30 2c 20 20 20 36 38 2c 20 20  0,  120,   68,  
3b50a 20 36 39 2c 20 20 20 37 30 2c 0a 20 2f 2a 20 20   69,   70,. /*  
3b50b 20 35 37 30 20 2a 2f 20 20 20 20 37 31 2c 20 20   570 */    71,  
3b50c 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c   72,   73,   74,
3b50d 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
3b50e 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
3b50f 20 38 30 2c 0a 20 2f 2a 20 20 20 35 38 30 20 2a   80,. /*   580 *
3b510 2f 20 20 20 32 33 30 2c 20 20 20 38 32 2c 20 20  /   230,   82,  
3b511 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
3b512 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38     86,   87,   8
3b513 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 0a 20  8,   89,   90,. 
3b514 2f 2a 20 20 20 35 39 30 20 2a 2f 20 20 20 20 39  /*   590 */    9
3b515 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20  1,   92,   19,  
3b516 32 31 35 2c 20 20 20 32 32 2c 20 20 20 32 33 2c  215,   22,   23,
3b517 20 20 20 32 33 2c 20 20 20 32 35 2c 20 20 31 36     23,   25,  16
3b518 33 2c 20 20 31 39 34 2c 0a 20 2f 2a 20 20 20 36  3,  194,. /*   6
3b519 30 30 20 2a 2f 20 20 20 20 39 34 2c 20 20 31 36  00 */    94,  16
3b51a 36 2c 20 20 31 36 37 2c 20 20 31 36 38 2c 20 20  6,  167,  168,  
3b51b 20 32 35 2c 20 20 31 33 38 2c 20 20 20 36 37 2c   25,  138,   67,
3b51c 20 20 20 20 37 2c 20 20 20 20 38 2c 20 20 20 20      7,    8,    
3b51d 39 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a 2f 20  9,. /*   610 */ 
3b51e 20 20 31 30 38 2c 20 20 32 30 36 2c 20 20 32 30    108,  206,  20
3b51f 37 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  7,  169,  170,  
3b520 31 37 31 2c 20 20 31 35 30 2c 20 20 20 32 32 2c  171,  150,   22,
3b521 20 20 32 32 31 2c 20 20 32 32 32 2c 0a 20 2f 2a    221,  222,. /*
3b522 20 20 20 36 32 30 20 2a 2f 20 20 20 32 32 33 2c     620 */   223,
3b523 20 20 20 32 36 2c 20 20 20 34 39 2c 20 20 20 35     26,   49,   5
3b524 30 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  0,   86,   87,  
3b525 20 32 33 2c 20 20 31 36 31 2c 20 20 31 36 32 2c   23,  161,  162,
3b526 20 20 20 32 33 2c 0a 20 2f 2a 20 20 20 36 33 30     23,. /*   630
3b527 20 2a 2f 20 20 20 20 32 32 2c 20 20 31 36 35 2c   */    22,  165,
3b528 20 20 20 32 34 2c 20 20 31 32 30 2c 20 20 20 32     24,  120,   2
3b529 32 2c 20 20 20 32 33 2c 20 20 20 32 35 2c 20 20  2,   23,   25,  
3b52a 31 36 30 2c 20 20 32 34 31 2c 20 20 20 36 37 2c  160,  241,   67,
3b52b 0a 20 2f 2a 20 20 20 36 34 30 20 2a 2f 20 20 20  . /*   640 */   
3b52c 31 37 36 2c 20 20 20 36 38 2c 20 20 20 36 39 2c  176,   68,   69,
3b52d 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
3b52e 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  2,   73,   74,  
3b52f 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20   75,   76,. /*  
3b530 20 36 35 30 20 2a 2f 20 20 20 20 37 37 2c 20 20   650 */    77,  
3b531 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
3b532 20 20 31 36 30 2c 20 20 20 38 32 2c 20 20 20 38    160,   82,   8
3b533 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
3b534 20 38 36 2c 0a 20 2f 2a 20 20 20 36 36 30 20 2a   86,. /*   660 *
3b535 2f 20 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  /    87,   88,  
3b536 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c   89,   90,   91,
3b537 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20 31 32     92,   19,  12
3b538 39 2c 20 20 31 33 30 2c 20 20 31 35 30 2c 0a 20  9,  130,  150,. 
3b539 2f 2a 20 20 20 36 37 30 20 2a 2f 20 20 20 20 32  /*   670 */    2
3b53a 33 2c 20 20 31 39 34 2c 20 20 20 33 35 2c 20 20  3,  194,   35,  
3b53b 20 32 33 2c 20 20 32 33 30 2c 20 20 20 32 35 2c   23,  230,   25,
3b53c 20 20 31 35 30 2c 20 20 31 35 35 2c 20 20 31 35    150,  155,  15
3b53d 30 2c 20 20 20 36 37 2c 0a 20 2f 2a 20 20 20 36  0,   67,. /*   6
3b53e 38 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 30  80 */   150,  10
3b53f 35 2c 20 20 31 30 36 2c 20 20 31 30 37 2c 20 20  5,  106,  107,  
3b540 31 36 35 2c 20 20 32 32 31 2c 20 20 32 32 32 2c  165,  221,  222,
3b541 20 20 32 32 33 2c 20 20 31 39 34 2c 20 20 20 39    223,  194,   9
3b542 34 2c 0a 20 2f 2a 20 20 20 36 39 30 20 2a 2f 20  4,. /*   690 */ 
3b543 20 20 20 32 33 2c 20 20 31 36 35 2c 20 20 20 32     23,  165,   2
3b544 35 2c 20 20 31 36 35 2c 20 20 32 31 37 2c 20 20  5,  165,  217,  
3b545 31 36 35 2c 20 20 20 34 39 2c 20 20 20 35 30 2c  165,   49,   50,
3b546 20 20 20 32 35 2c 20 20 31 37 33 2c 0a 20 2f 2a     25,  173,. /*
3b547 20 20 20 37 30 30 20 2a 2f 20 20 20 31 37 34 2c     700 */   174,
3b548 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20 31 37    173,  174,  17
3b549 33 2c 20 20 31 37 34 2c 20 20 20 20 30 2c 20 20  3,  174,    0,  
3b54a 20 20 31 2c 20 20 20 20 32 2c 20 20 31 31 38 2c    1,    2,  118,
3b54b 20 20 32 32 31 2c 0a 20 2f 2a 20 20 20 37 31 30    221,. /*   710
3b54c 20 2a 2f 20 20 20 32 32 32 2c 20 20 32 32 33 2c   */   222,  223,
3b54d 20 20 31 39 33 2c 20 20 32 31 39 2c 20 20 32 33    193,  219,  23
3b54e 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  7,   68,   69,  
3b54f 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
3b550 0a 20 2f 2a 20 20 20 37 32 30 20 2a 2f 20 20 20  . /*   720 */   
3b551 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c   73,   74,   75,
3b552 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
3b553 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20  8,   79,   80,  
3b554 31 35 30 2c 20 20 20 38 32 2c 0a 20 2f 2a 20 20  150,   82,. /*  
3b555 20 37 33 30 20 2a 2f 20 20 20 20 38 33 2c 20 20   730 */    83,  
3b556 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c   84,   85,   86,
3b557 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38     87,   88,   8
3b558 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20  9,   90,   91,  
3b559 20 39 32 2c 0a 20 2f 2a 20 20 20 37 34 30 20 2a   92,. /*   740 *
3b55a 2f 20 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20  /    19,  150,  
3b55b 20 31 39 2c 20 20 31 36 35 2c 20 20 31 35 30 2c   19,  165,  150,
3b55c 20 20 20 32 34 2c 20 20 31 36 36 2c 20 20 31 36     24,  166,  16
3b55d 37 2c 20 20 31 36 38 2c 20 20 32 32 37 2c 0a 20  7,  168,  227,. 
3b55e 2f 2a 20 20 20 37 35 30 20 2a 2f 20 20 20 20 32  /*   750 */    2
3b55f 37 2c 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20  7,  173,  174,  
3b560 32 33 31 2c 20 20 31 35 30 2c 20 20 20 32 35 2c  231,  150,   25,
3b561 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 37    165,  150,  17
3b562 32 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20 37  2,  165,. /*   7
3b563 36 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 32 34  60 */   150,  24
3b564 32 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20  2,  129,  130,  
3b565 31 37 33 2c 20 20 31 37 34 2c 20 20 31 38 30 2c  173,  174,  180,
3b566 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20 31 36    173,  174,  16
3b567 35 2c 0a 20 2f 2a 20 20 20 37 37 30 20 2a 2f 20  5,. /*   770 */ 
3b568 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20 31 36     49,   50,  16
3b569 35 2c 20 20 31 35 30 2c 20 20 31 37 36 2c 20 20  5,  150,  176,  
3b56a 31 36 35 2c 20 20 20 33 35 2c 20 20 31 37 33 2c  165,   35,  173,
3b56b 20 20 31 37 34 2c 20 20 31 36 35 2c 0a 20 2f 2a    174,  165,. /*
3b56c 20 20 20 37 38 30 20 2a 2f 20 20 20 31 37 33 2c     780 */   173,
3b56d 20 20 31 37 34 2c 20 20 20 33 35 2c 20 20 20 32    174,   35,   2
3b56e 33 2c 20 20 20 32 33 2c 20 20 20 32 35 2c 20 20  3,   23,   25,  
3b56f 20 32 35 2c 20 20 31 37 33 2c 20 20 31 36 35 2c   25,  173,  165,
3b570 20 20 20 36 38 2c 0a 20 2f 2a 20 20 20 37 39 30     68,. /*   790
3b571 20 2a 2f 20 20 20 20 36 39 2c 20 20 20 37 30 2c   */    69,   70,
3b572 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37     71,   72,   7
3b573 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20  3,   74,   75,  
3b574 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   76,   77,   78,
3b575 0a 20 2f 2a 20 20 20 38 30 30 20 2a 2f 20 20 20  . /*   800 */   
3b576 20 37 39 2c 20 20 20 38 30 2c 20 20 31 35 30 2c   79,   80,  150,
3b577 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
3b578 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  4,   85,   86,  
3b579 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20   87,   88,. /*  
3b57a 20 38 31 30 20 2a 2f 20 20 20 20 38 39 2c 20 20   810 */    89,  
3b57b 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c   90,   91,   92,
3b57c 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20 31 39     19,  150,  19
3b57d 33 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20  3,  165,  150,  
3b57e 32 32 31 2c 0a 20 2f 2a 20 20 20 38 32 30 20 2a  221,. /*   820 *
3b57f 2f 20 20 20 32 32 32 2c 20 20 32 32 33 2c 20 20  /   222,  223,  
3b580 31 35 30 2c 20 20 32 31 33 2c 20 20 20 31 39 2c  150,  213,   19,
3b581 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20 20 32    173,  174,   2
3b582 33 2c 20 20 31 35 30 2c 20 20 20 39 37 2c 0a 20  3,  150,   97,. 
3b583 2f 2a 20 20 20 38 33 30 20 2a 2f 20 20 20 31 36  /*   830 */   16
3b584 35 2c 20 20 31 35 30 2c 20 20 20 32 37 2c 20 20  5,  150,   27,  
3b585 31 36 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c  165,  150,  150,
3b586 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 37    150,  165,  17
3b587 33 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 20 38  3,  174,. /*   8
3b588 34 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 31 37  40 */    22,  17
3b589 33 2c 20 20 31 37 34 2c 20 20 31 36 35 2c 20 20  3,  174,  165,  
3b58a 20 34 39 2c 20 20 20 35 30 2c 20 20 31 36 35 2c   49,   50,  165,
3b58b 20 20 20 35 32 2c 20 20 31 31 36 2c 20 20 31 36     52,  116,  16
3b58c 35 2c 0a 20 2f 2a 20 20 20 38 35 30 20 2a 2f 20  5,. /*   850 */ 
3b58d 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 32 30    165,  165,  20
3b58e 36 2c 20 20 32 30 37 2c 20 20 31 37 33 2c 20 20  6,  207,  173,  
3b58f 31 37 34 2c 20 20 31 32 36 2c 20 20 20 35 30 2c  174,  126,   50,
3b590 20 20 31 37 33 2c 20 20 31 37 34 2c 0a 20 2f 2a    173,  174,. /*
3b591 20 20 20 38 36 30 20 2a 2f 20 20 20 31 32 38 2c     860 */   128,
3b592 20 20 20 32 37 2c 20 20 31 36 30 2c 20 20 20 36     27,  160,   6
3b593 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20  8,   69,   70,  
3b594 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c   71,   72,   73,
3b595 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 38 37 30     74,. /*   870
3b596 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37 36 2c   */    75,   76,
3b597 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
3b598 39 2c 20 20 20 38 30 2c 20 20 31 35 30 2c 20 20  9,   80,  150,  
3b599 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
3b59a 0a 20 2f 2a 20 20 20 38 38 30 20 2a 2f 20 20 20  . /*   880 */   
3b59b 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
3b59c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
3b59d 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20  0,   91,   92,  
3b59e 20 31 39 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20   19,  150,. /*  
3b59f 20 38 39 30 20 2a 2f 20 20 20 20 32 33 2c 20 20   890 */    23,  
3b5a0 31 36 35 2c 20 20 31 35 30 2c 20 20 20 32 33 2c  165,  150,   23,
3b5a1 20 20 32 31 36 2c 20 20 20 32 35 2c 20 20 31 39    216,   25,  19
3b5a2 34 2c 20 20 20 33 32 2c 20 20 20 33 39 2c 20 20  4,   32,   39,  
3b5a3 31 37 33 2c 0a 20 2f 2a 20 20 20 39 30 30 20 2a  173,. /*   900 *
3b5a4 2f 20 20 20 31 37 34 2c 20 20 31 33 35 2c 20 20  /   174,  135,  
3b5a5 31 35 30 2c 20 20 31 33 37 2c 20 20 31 36 35 2c  150,  137,  165,
3b5a6 20 20 31 35 30 2c 20 20 20 34 31 2c 20 20 31 36    150,   41,  16
3b5a7 35 2c 20 20 31 35 30 2c 20 20 20 35 32 2c 0a 20  5,  150,   52,. 
3b5a8 2f 2a 20 20 20 39 31 30 20 2a 2f 20 20 20 32 33  /*   910 */   23
3b5a9 38 2c 20 20 31 30 34 2c 20 20 31 37 33 2c 20 20  8,  104,  173,  
3b5aa 31 37 34 2c 20 20 20 32 39 2c 20 20 31 37 33 2c  174,   29,  173,
3b5ab 20 20 31 37 34 2c 20 20 31 36 35 2c 20 20 20 34    174,  165,   4
3b5ac 39 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20 20 39  9,   50,. /*   9
3b5ad 32 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 32 31  20 */   165,  21
3b5ae 39 2c 20 20 32 33 38 2c 20 20 31 36 35 2c 20 20  9,  238,  165,  
3b5af 32 33 38 2c 20 20 31 37 33 2c 20 20 31 37 34 2c  238,  173,  174,
3b5b0 20 20 20 35 32 2c 20 20 31 37 33 2c 20 20 31 37     52,  173,  17
3b5b1 34 2c 0a 20 2f 2a 20 20 20 39 33 30 20 2a 2f 20  4,. /*   930 */ 
3b5b2 20 20 20 32 32 2c 20 20 31 37 33 2c 20 20 31 37     22,  173,  17
3b5b3 34 2c 20 20 20 32 33 2c 20 20 20 32 33 2c 20 20  4,   23,   23,  
3b5b4 31 36 30 2c 20 20 20 32 35 2c 20 20 20 36 38 2c  160,   25,   68,
3b5b5 20 20 20 36 39 2c 20 20 20 37 30 2c 0a 20 2f 2a     69,   70,. /*
3b5b6 20 20 20 39 34 30 20 2a 2f 20 20 20 20 37 31 2c     940 */    71,
3b5b7 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37     72,   73,   7
3b5b8 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
3b5b9 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c   77,   78,   79,
3b5ba 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 39 35 30     80,. /*   950
3b5bb 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 38 32 2c   */   150,   82,
3b5bc 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
3b5bd 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
3b5be 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
3b5bf 0a 20 2f 2a 20 20 20 39 36 30 20 2a 2f 20 20 20  . /*   960 */   
3b5c0 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c   91,   92,   19,
3b5c1 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 36    150,  150,  16
3b5c2 35 2c 20 20 31 35 30 2c 20 20 32 34 35 2c 20 20  5,  150,  245,  
3b5c3 32 34 36 2c 20 20 31 39 34 2c 0a 20 2f 2a 20 20  246,  194,. /*  
3b5c4 20 39 37 30 20 2a 2f 20 20 20 31 35 30 2c 20 20   970 */   150,  
3b5c5 31 34 34 2c 20 20 31 34 35 2c 20 20 31 37 33 2c  144,  145,  173,
3b5c6 20 20 31 37 34 2c 20 20 31 36 30 2c 20 20 31 35    174,  160,  15
3b5c7 30 2c 20 20 20 32 32 2c 20 20 31 36 35 2c 20 20  0,   22,  165,  
3b5c8 31 36 35 2c 0a 20 2f 2a 20 20 20 39 38 30 20 2a  165,. /*   980 *
3b5c9 2f 20 20 20 20 32 32 2c 20 20 31 36 35 2c 20 20  /    22,  165,  
3b5ca 31 35 30 2c 20 20 31 35 30 2c 20 20 31 31 36 2c  150,  150,  116,
3b5cb 20 20 31 36 35 2c 20 20 31 37 33 2c 20 20 31 37    165,  173,  17
3b5cc 34 2c 20 20 20 35 32 2c 20 20 31 37 33 2c 0a 20  4,   52,  173,. 
3b5cd 2f 2a 20 20 20 39 39 30 20 2a 2f 20 20 20 31 37  /*   990 */   17
3b5ce 34 2c 20 20 31 36 35 2c 20 20 20 34 39 2c 20 20  4,  165,   49,  
3b5cf 20 35 30 2c 20 20 20 32 32 2c 20 20 31 35 30 2c   50,   22,  150,
3b5d0 20 20 31 32 38 2c 20 20 31 36 35 2c 20 20 31 36    128,  165,  16
3b5d1 35 2c 20 20 31 37 33 2c 0a 20 2f 2a 20 20 31 30  5,  173,. /*  10
3b5d2 30 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 38  00 */   174,  18
3b5d3 37 2c 20 20 31 36 36 2c 20 20 31 36 36 2c 20 20  7,  166,  166,  
3b5d4 20 32 32 2c 20 20 31 37 33 2c 20 20 31 37 34 2c   22,  173,  174,
3b5d5 20 20 31 38 37 2c 20 20 31 30 39 2c 20 20 31 39    187,  109,  19
3b5d6 34 2c 0a 20 2f 2a 20 20 31 30 31 30 20 2a 2f 20  4,. /*  1010 */ 
3b5d7 20 20 31 36 35 2c 20 20 20 36 38 2c 20 20 20 36    165,   68,   6
3b5d8 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20  9,   70,   71,  
3b5d9 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c   72,   73,   74,
3b5da 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a     75,   76,. /*
3b5db 20 20 31 30 32 30 20 2a 2f 20 20 20 20 37 37 2c    1020 */    77,
3b5dc 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38     78,   79,   8
3b5dd 30 2c 20 20 31 35 30 2c 20 20 20 38 32 2c 20 20  0,  150,   82,  
3b5de 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
3b5df 20 20 20 38 36 2c 0a 20 2f 2a 20 20 31 30 33 30     86,. /*  1030
3b5e0 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38 38 2c   */    87,   88,
3b5e1 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39     89,   90,   9
3b5e2 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20  1,   92,   19,  
3b5e3 31 35 30 2c 20 20 31 39 33 2c 20 20 31 36 35 2c  150,  193,  165,
3b5e4 0a 20 2f 2a 20 20 31 30 34 30 20 2a 2f 20 20 20  . /*  1040 */   
3b5e5 31 30 32 2c 20 20 32 30 35 2c 20 20 32 30 35 2c  102,  205,  205,
3b5e6 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 32 34    150,  150,  24
3b5e7 37 2c 20 20 32 34 38 2c 20 20 31 37 33 2c 20 20  7,  248,  173,  
3b5e8 31 37 34 2c 20 20 20 31 39 2c 0a 20 2f 2a 20 20  174,   19,. /*  
3b5e9 31 30 35 30 20 2a 2f 20 20 20 31 35 30 2c 20 20  1050 */   150,  
3b5ea 20 32 30 2c 20 20 31 36 35 2c 20 20 31 35 30 2c   20,  165,  150,
3b5eb 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35    150,  150,  15
3b5ec 30 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20  0,  150,  165,  
3b5ed 31 36 35 2c 0a 20 2f 2a 20 20 31 30 36 30 20 2a  165,. /*  1060 *
3b5ee 2f 20 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20  /   173,  174,  
3b5ef 20 34 39 2c 20 20 20 35 30 2c 20 20 31 30 34 2c   49,   50,  104,
3b5f0 20 20 31 36 35 2c 20 20 20 34 39 2c 20 20 20 35    165,   49,   5
3b5f1 30 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 0a 20  0,  165,  165,. 
3b5f2 2f 2a 20 20 31 30 37 30 20 2a 2f 20 20 20 31 36  /*  1070 */   16
3b5f3 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  5,  165,  165,  
3b5f4 31 37 33 2c 20 20 31 37 34 2c 20 20 20 34 33 2c  173,  174,   43,
3b5f5 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20 31 37    173,  174,  17
3b5f6 33 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 31 30  3,  174,. /*  10
3b5f7 38 30 20 2a 2f 20 20 20 31 38 37 2c 20 20 20 32  80 */   187,   2
3b5f8 34 2c 20 20 31 39 30 2c 20 20 31 39 31 2c 20 20  4,  190,  191,  
3b5f9 20 37 31 2c 20 20 20 37 32 2c 20 20 20 36 39 2c   71,   72,   69,
3b5fa 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
3b5fb 32 2c 0a 20 2f 2a 20 20 31 30 39 30 20 2a 2f 20  2,. /*  1090 */ 
3b5fc 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
3b5fd 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  5,   76,   77,  
3b5fe 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
3b5ff 20 20 31 35 30 2c 20 20 20 38 32 2c 0a 20 2f 2a    150,   82,. /*
3b600 20 20 31 31 30 30 20 2a 2f 20 20 20 20 38 33 2c    1100 */    83,
3b601 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
3b602 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  6,   87,   88,  
3b603 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c   89,   90,   91,
3b604 20 20 20 39 32 2c 0a 20 2f 2a 20 20 31 31 31 30     92,. /*  1110
3b605 20 2a 2f 20 20 20 20 31 39 2c 20 20 20 39 38 2c   */    19,   98,
3b606 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 35    150,  165,  15
3b607 30 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  0,  150,  150,  
3b608 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35 30 2c  150,  150,  150,
3b609 0a 20 2f 2a 20 20 31 31 32 30 20 2a 2f 20 20 20  . /*  1120 */   
3b60a 20 35 39 2c 20 20 31 37 33 2c 20 20 31 37 34 2c   59,  173,  174,
3b60b 20 20 20 32 35 2c 20 20 31 35 30 2c 20 20 31 39     25,  150,  19
3b60c 30 2c 20 20 31 39 31 2c 20 20 31 36 35 2c 20 20  0,  191,  165,  
3b60d 20 35 33 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20   53,  165,. /*  
3b60e 31 31 33 30 20 2a 2f 20 20 20 31 36 35 2c 20 20  1130 */   165,  
3b60f 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c  165,  165,  165,
3b610 20 20 31 36 35 2c 20 20 31 37 33 2c 20 20 31 37    165,  173,  17
3b611 34 2c 20 20 31 37 33 2c 20 20 31 37 34 2c 20 20  4,  173,  174,  
3b612 31 36 35 2c 0a 20 2f 2a 20 20 31 31 34 30 20 2a  165,. /*  1140 *
3b613 2f 20 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20  /    49,   50,  
3b614 20 39 31 2c 20 20 20 39 32 2c 20 20 20 20 31 2c   91,   92,    1,
3b615 20 20 20 20 32 2c 20 20 20 35 33 2c 20 20 31 37      2,   53,  17
3b616 33 2c 20 20 31 37 34 2c 20 20 31 33 38 2c 0a 20  3,  174,  138,. 
3b617 2f 2a 20 20 31 31 35 30 20 2a 2f 20 20 20 31 30  /*  1150 */   10
3b618 34 2c 20 20 20 32 32 2c 20 20 20 20 35 2c 20 20  4,   22,    5,  
3b619 20 20 31 2c 20 20 20 33 35 2c 20 20 31 31 38 2c    1,   35,  118,
3b61a 20 20 31 32 37 2c 20 20 31 35 30 2c 20 20 31 39    127,  150,  19
3b61b 33 2c 20 20 31 39 33 2c 0a 20 2f 2a 20 20 31 31  3,  193,. /*  11
3b61c 36 30 20 2a 2f 20 20 20 31 39 33 2c 20 20 20 37  60 */   193,   7
3b61d 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
3b61e 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c   73,   74,   75,
3b61f 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
3b620 38 2c 0a 20 2f 2a 20 20 31 31 37 30 20 2a 2f 20  8,. /*  1170 */ 
3b621 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 31 36     79,   80,  16
3b622 35 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  5,   82,   83,  
3b623 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c   84,   85,   86,
3b624 20 20 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a     87,   88,. /*
3b625 20 20 31 31 38 30 20 2a 2f 20 20 20 20 38 39 2c    1180 */    89,
3b626 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39     90,   91,   9
3b627 32 2c 20 20 20 31 39 2c 20 20 20 32 30 2c 20 20  2,   19,   20,  
3b628 31 35 30 2c 20 20 20 32 32 2c 20 20 31 35 30 2c  150,   22,  150,
3b629 20 20 20 32 37 2c 0a 20 2f 2a 20 20 31 31 39 30     27,. /*  1190
3b62a 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 32 36 2c   */   150,   26,
3b62b 20 20 20 32 37 2c 20 20 31 30 38 2c 20 20 31 35     27,  108,  15
3b62c 30 2c 20 20 20 32 32 2c 20 20 20 37 36 2c 20 20  0,   22,   76,  
3b62d 20 37 36 2c 20 20 31 35 30 2c 20 20 20 32 35 2c   76,  150,   25,
3b62e 0a 20 2f 2a 20 20 31 32 30 30 20 2a 2f 20 20 20  . /*  1200 */   
3b62f 31 39 33 2c 20 20 31 36 35 2c 20 20 20 33 37 2c  193,  165,   37,
3b630 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36    165,  150,  16
3b631 35 2c 20 20 20 32 32 2c 20 20 20 31 39 2c 20 20  5,   22,   19,  
3b632 20 32 30 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20   20,  165,. /*  
3b633 31 32 31 30 20 2a 2f 20 20 20 20 32 32 2c 20 20  1210 */    22,  
3b634 31 37 33 2c 20 20 31 37 34 2c 20 20 31 36 35 2c  173,  174,  165,
3b635 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32     26,   27,   2
3b636 33 2c 20 20 31 35 30 2c 20 20 31 31 39 2c 20 20  3,  150,  119,  
3b637 31 36 35 2c 0a 20 2f 2a 20 20 31 32 32 30 20 2a  165,. /*  1220 *
3b638 2f 20 20 20 31 35 30 2c 20 20 20 35 36 2c 20 20  /   150,   56,  
3b639 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35 30 2c  150,  150,  150,
3b63a 20 20 20 33 37 2c 20 20 20 31 36 2c 20 20 31 37     37,   16,  17
3b63b 33 2c 20 20 31 37 34 2c 20 20 31 39 33 2c 0a 20  3,  174,  193,. 
3b63c 2f 2a 20 20 31 32 33 30 20 2a 2f 20 20 20 31 35  /*  1230 */   15
3b63d 30 2c 20 20 20 36 36 2c 20 20 31 36 35 2c 20 20  0,   66,  165,  
3b63e 31 39 33 2c 20 20 20 20 31 2c 20 20 31 36 35 2c  193,    1,  165,
3b63f 20 20 31 32 31 2c 20 20 31 36 35 2c 20 20 31 36    121,  165,  16
3b640 35 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 32  5,  165,. /*  12
3b641 34 30 20 2a 2f 20 20 20 20 32 30 2c 20 20 31 34  40 */    20,  14
3b642 36 2c 20 20 31 34 37 2c 20 20 31 31 39 2c 20 20  6,  147,  119,  
3b643 20 35 36 2c 20 20 31 36 35 2c 20 20 31 35 30 2c   56,  165,  150,
3b644 20 20 31 35 32 2c 20 20 20 31 36 2c 20 20 31 35    152,   16,  15
3b645 34 2c 0a 20 2f 2a 20 20 31 32 35 30 20 2a 2f 20  4,. /*  1250 */ 
3b646 20 20 31 35 30 2c 20 20 20 38 36 2c 20 20 20 38    150,   86,   8
3b647 37 2c 20 20 20 38 38 2c 20 20 20 36 36 2c 20 20  7,   88,   66,  
3b648 31 36 30 2c 20 20 31 35 30 2c 20 20 31 35 30 2c  160,  150,  150,
3b649 20 20 20 39 33 2c 20 20 20 39 34 2c 0a 20 2f 2a     93,   94,. /*
3b64a 20 20 31 32 36 30 20 2a 2f 20 20 20 20 39 35 2c    1260 */    95,
3b64b 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20 39    165,  150,   9
3b64c 38 2c 20 20 31 30 38 2c 20 20 31 36 35 2c 20 20  8,  108,  165,  
3b64d 31 32 37 2c 20 20 20 32 33 2c 20 20 20 36 35 2c  127,   23,   65,
3b64e 20 20 31 37 33 2c 0a 20 2f 2a 20 20 31 32 37 30    173,. /*  1270
3b64f 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 36 35 2c   */   174,  165,
3b650 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20 38    165,  150,   8
3b651 36 2c 20 20 20 38 37 2c 20 20 31 32 38 2c 20 20  6,   87,  128,  
3b652 31 36 35 2c 20 20 31 35 30 2c 20 20 31 37 33 2c  165,  150,  173,
3b653 0a 20 2f 2a 20 20 31 32 38 30 20 2a 2f 20 20 20  . /*  1280 */   
3b654 31 37 34 2c 20 20 20 39 33 2c 20 20 20 39 34 2c  174,   93,   94,
3b655 20 20 20 39 35 2c 20 20 20 32 33 2c 20 20 31 35     95,   23,  15
3b656 30 2c 20 20 20 39 38 2c 20 20 20 31 35 2c 20 20  0,   98,   15,  
3b657 31 36 35 2c 20 20 31 39 34 2c 0a 20 2f 2a 20 20  165,  194,. /*  
3b658 31 32 39 30 20 2a 2f 20 20 20 31 35 30 2c 20 20  1290 */   150,  
3b659 31 34 30 2c 20 20 20 32 32 2c 20 20 31 36 35 2c  140,   22,  165,
3b65a 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20 31 33    129,  130,  13
3b65b 31 2c 20 20 31 33 32 2c 20 20 31 33 33 2c 20 20  1,  132,  133,  
3b65c 31 33 34 2c 0a 20 2f 2a 20 20 31 33 30 30 20 2a  134,. /*  1300 *
3b65d 2f 20 20 20 31 36 35 2c 20 20 31 37 33 2c 20 20  /   165,  173,  
3b65e 31 37 34 2c 20 20 20 20 33 2c 20 20 31 31 36 2c  174,    3,  116,
3b65f 20 20 31 36 35 2c 20 20 20 31 39 2c 20 20 20 32    165,   19,   2
3b660 30 2c 20 20 31 35 30 2c 20 20 20 32 32 2c 0a 20  0,  150,   22,. 
3b661 2f 2a 20 20 31 33 31 30 20 2a 2f 20 20 20 20 20  /*  1310 */     
3b662 34 2c 20 20 31 35 30 2c 20 20 32 31 37 2c 20 20  4,  150,  217,  
3b663 20 32 36 2c 20 20 20 32 37 2c 20 20 31 37 39 2c   26,   27,  179,
3b664 20 20 31 37 39 2c 20 20 31 32 39 2c 20 20 31 33    179,  129,  13
3b665 30 2c 20 20 31 33 31 2c 0a 20 2f 2a 20 20 31 33  0,  131,. /*  13
3b666 32 30 20 2a 2f 20 20 20 31 33 32 2c 20 20 31 33  20 */   132,  13
3b667 33 2c 20 20 31 33 34 2c 20 20 31 36 35 2c 20 20  3,  134,  165,  
3b668 20 33 37 2c 20 20 31 35 30 2c 20 20 31 36 35 2c   37,  150,  165,
3b669 20 20 31 35 30 2c 20 20 31 36 34 2c 20 20 20 31    150,  164,   1
3b66a 39 2c 0a 20 2f 2a 20 20 31 33 33 30 20 2a 2f 20  9,. /*  1330 */ 
3b66b 20 20 20 32 30 2c 20 20 31 35 30 2c 20 20 20 32     20,  150,   2
3b66c 32 2c 20 20 32 34 36 2c 20 20 31 34 39 2c 20 20  2,  246,  149,  
3b66d 32 34 39 2c 20 20 20 32 36 2c 20 20 20 32 37 2c  249,   26,   27,
3b66e 20 20 32 34 39 2c 20 20 32 34 34 2c 0a 20 2f 2a    249,  244,. /*
3b66f 20 20 31 33 34 30 20 2a 2f 20 20 20 31 36 35 2c    1340 */   165,
3b670 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 20 35    150,  165,   5
3b671 36 2c 20 20 20 20 36 2c 20 20 31 35 30 2c 20 20  6,    6,  150,  
3b672 31 36 35 2c 20 20 20 33 37 2c 20 20 31 37 33 2c  165,   37,  173,
3b673 20 20 31 37 34 2c 0a 20 2f 2a 20 20 31 33 35 30    174,. /*  1350
3b674 20 2a 2f 20 20 20 31 37 33 2c 20 20 31 37 34 2c   */   173,  174,
3b675 20 20 31 35 30 2c 20 20 20 36 36 2c 20 20 31 37    150,   66,  17
3b676 33 2c 20 20 31 37 34 2c 20 20 31 36 35 2c 20 20  3,  174,  165,  
3b677 31 34 39 2c 20 20 31 34 39 2c 20 20 20 31 33 2c  149,  149,   13,
3b678 0a 20 2f 2a 20 20 31 33 36 30 20 2a 2f 20 20 20  . /*  1360 */   
3b679 31 36 35 2c 20 20 20 32 35 2c 20 20 31 35 30 2c  165,   25,  150,
3b67a 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 34    150,  150,  14
3b67b 39 2c 20 20 20 35 36 2c 20 20 31 36 35 2c 20 20  9,   56,  165,  
3b67c 31 35 30 2c 20 20 31 31 36 2c 0a 20 2f 2a 20 20  150,  116,. /*  
3b67d 31 33 37 30 20 2a 2f 20 20 20 31 35 31 2c 20 20  1370 */   151,  
3b67e 31 35 30 2c 20 20 31 35 30 2c 20 20 20 38 36 2c  150,  150,   86,
3b67f 20 20 20 38 37 2c 20 20 31 35 30 2c 20 20 20 36     87,  150,   6
3b680 36 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  6,  165,  165,  
3b681 31 36 35 2c 0a 20 2f 2a 20 20 31 33 38 30 20 2a  165,. /*  1380 *
3b682 2f 20 20 20 20 39 33 2c 20 20 20 39 34 2c 20 20  /    93,   94,  
3b683 20 39 35 2c 20 20 31 36 35 2c 20 20 31 35 30 2c   95,  165,  150,
3b684 20 20 20 39 38 2c 20 20 31 36 35 2c 20 20 31 36     98,  165,  16
3b685 35 2c 20 20 31 35 31 2c 20 20 20 32 32 2c 0a 20  5,  151,   22,. 
3b686 2f 2a 20 20 31 33 39 30 20 2a 2f 20 20 20 31 36  /*  1390 */   16
3b687 35 2c 20 20 31 39 34 2c 20 20 31 35 30 2c 20 20  5,  194,  150,  
3b688 20 32 36 2c 20 20 20 32 37 2c 20 20 31 35 30 2c   26,   27,  150,
3b689 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 31 35     86,   87,  15
3b68a 39 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 34  9,  165,. /*  14
3b68b 30 30 20 2a 2f 20 20 20 31 39 39 2c 20 20 31 32  00 */   199,  12
3b68c 36 2c 20 20 31 32 33 2c 20 20 20 39 33 2c 20 20  6,  123,   93,  
3b68d 20 39 34 2c 20 20 20 39 35 2c 20 20 32 30 30 2c   94,   95,  200,
3b68e 20 20 31 36 35 2c 20 20 20 39 38 2c 20 20 31 32    165,   98,  12
3b68f 34 2c 0a 20 2f 2a 20 20 31 34 31 30 20 2a 2f 20  4,. /*  1410 */ 
3b690 20 20 31 36 35 2c 20 20 31 32 32 2c 20 20 32 30    165,  122,  20
3b691 31 2c 20 20 31 32 35 2c 20 20 32 32 35 2c 20 20  1,  125,  225,  
3b692 31 33 35 2c 20 20 31 32 39 2c 20 20 31 33 30 2c  135,  129,  130,
3b693 20 20 31 33 31 2c 20 20 31 33 32 2c 0a 20 2f 2a    131,  132,. /*
3b694 20 20 31 34 32 30 20 2a 2f 20 20 20 31 33 33 2c    1420 */   133,
3b695 20 20 31 33 34 2c 20 20 20 20 35 2c 20 20 31 35    134,    5,  15
3b696 37 2c 20 20 31 35 37 2c 20 20 32 30 32 2c 20 20  7,  157,  202,  
3b697 31 31 38 2c 20 20 20 31 30 2c 20 20 20 31 31 2c  118,   10,   11,
3b698 20 20 20 31 32 2c 0a 20 2f 2a 20 20 31 34 33 30     12,. /*  1430
3b699 20 2a 2f 20 20 20 20 31 33 2c 20 20 20 31 34 2c   */    13,   14,
3b69a 20 20 32 30 33 2c 20 20 20 36 36 2c 20 20 20 31    203,   66,   1
3b69b 37 2c 20 20 31 30 34 2c 20 20 32 31 30 2c 20 20  7,  104,  210,  
3b69c 31 32 31 2c 20 20 32 31 31 2c 20 20 31 32 39 2c  121,  211,  129,
3b69d 0a 20 2f 2a 20 20 31 34 34 30 20 2a 2f 20 20 20  . /*  1440 */   
3b69e 31 33 30 2c 20 20 31 33 31 2c 20 20 31 33 32 2c  130,  131,  132,
3b69f 20 20 31 33 33 2c 20 20 31 33 34 2c 20 20 32 31    133,  134,  21
3b6a0 30 2c 20 20 31 37 35 2c 20 20 32 31 31 2c 20 20  0,  175,  211,  
3b6a1 20 33 31 2c 20 20 32 31 31 2c 0a 20 2f 2a 20 20   31,  211,. /*  
3b6a2 31 34 35 30 20 2a 2f 20 20 20 20 33 33 2c 20 20  1450 */    33,  
3b6a3 32 31 30 2c 20 20 31 30 34 2c 20 20 20 38 36 2c  210,  104,   86,
3b6a4 20 20 20 38 37 2c 20 20 20 34 37 2c 20 20 31 37     87,   47,  17
3b6a5 35 2c 20 20 31 38 33 2c 20 20 31 37 35 2c 20 20  5,  183,  175,  
3b6a6 20 34 32 2c 0a 20 2f 2a 20 20 31 34 36 30 20 2a   42,. /*  1460 *
3b6a7 2f 20 20 20 31 30 33 2c 20 20 20 39 34 2c 20 20  /   103,   94,  
3b6a8 31 37 38 2c 20 20 31 37 37 2c 20 20 20 32 32 2c  178,  177,   22,
3b6a9 20 20 20 39 38 2c 20 20 31 37 35 2c 20 20 20 39     98,  175,   9
3b6aa 32 2c 20 20 32 32 38 2c 20 20 31 37 35 2c 0a 20  2,  228,  175,. 
3b6ab 2f 2a 20 20 31 34 37 30 20 2a 2f 20 20 20 31 37  /*  1470 */   17
3b6ac 35 2c 20 20 32 32 38 2c 20 20 20 35 35 2c 20 20  5,  228,   55,  
3b6ad 31 38 33 2c 20 20 20 35 37 2c 20 20 31 37 38 2c  183,   57,  178,
3b6ae 20 20 31 37 35 2c 20 20 31 35 36 2c 20 20 20 36    175,  156,   6
3b6af 31 2c 20 20 20 31 38 2c 0a 20 2f 2a 20 20 31 34  1,   18,. /*  14
3b6b0 38 30 20 2a 2f 20 20 20 31 35 37 2c 20 20 20 36  80 */   157,   6
3b6b1 34 2c 20 20 31 35 36 2c 20 20 32 33 35 2c 20 20  4,  156,  235,  
3b6b2 31 35 37 2c 20 20 31 35 36 2c 20 20 20 34 35 2c  157,  156,   45,
3b6b3 20 20 31 35 37 2c 20 20 32 33 36 2c 20 20 31 35    157,  236,  15
3b6b4 37 2c 0a 20 2f 2a 20 20 31 34 39 30 20 2a 2f 20  7,. /*  1490 */ 
3b6b5 20 20 31 33 35 2c 20 20 31 35 36 2c 20 20 31 38    135,  156,  18
3b6b6 39 2c 20 20 20 36 38 2c 20 20 31 35 37 2c 20 20  9,   68,  157,  
3b6b7 32 31 38 2c 20 20 31 32 39 2c 20 20 31 33 30 2c  218,  129,  130,
3b6b8 20 20 31 33 31 2c 20 20 20 32 32 2c 0a 20 2f 2a    131,   22,. /*
3b6b9 20 20 31 35 30 30 20 2a 2f 20 20 20 31 38 39 2c    1500 */   189,
3b6ba 20 20 31 39 39 2c 20 20 31 35 37 2c 20 20 31 35    199,  157,  15
3b6bb 36 2c 20 20 31 39 32 2c 20 20 20 31 38 2c 20 20  6,  192,   18,  
3b6bc 31 39 32 2c 20 20 31 39 32 2c 20 20 31 39 39 2c  192,  192,  199,
3b6bd 20 20 31 39 32 2c 0a 20 2f 2a 20 20 31 35 31 30    192,. /*  1510
3b6be 20 2a 2f 20 20 20 32 31 38 2c 20 20 31 38 39 2c   */   218,  189,
3b6bf 20 20 20 34 30 2c 20 20 31 35 37 2c 20 20 20 33     40,  157,   3
3b6c0 38 2c 20 20 31 35 37 2c 20 20 32 34 30 2c 20 20  8,  157,  240,  
3b6c1 31 35 37 2c 20 20 32 34 30 2c 20 20 31 35 33 2c  157,  240,  153,
3b6c2 0a 20 2f 2a 20 20 31 35 32 30 20 2a 2f 20 20 20  . /*  1520 */   
3b6c3 31 39 36 2c 20 20 31 38 31 2c 20 20 31 30 35 2c  196,  181,  105,
3b6c4 20 20 31 30 36 2c 20 20 31 30 37 2c 20 20 32 34    106,  107,  24
3b6c5 33 2c 20 20 31 39 38 2c 20 20 31 36 36 2c 20 20  3,  198,  166,  
3b6c6 31 31 31 2c 20 20 32 33 30 2c 0a 20 2f 2a 20 20  111,  230,. /*  
3b6c7 31 35 33 30 20 2a 2f 20 20 20 31 37 36 2c 20 20  1530 */   176,  
3b6c8 32 32 36 2c 20 20 32 33 39 2c 20 20 31 31 36 2c  226,  239,  116,
3b6c9 20 20 32 33 30 2c 20 20 31 37 36 2c 20 20 31 36    230,  176,  16
3b6ca 36 2c 20 20 31 36 36 2c 20 20 31 37 36 2c 20 20  6,  166,  176,  
3b6cb 31 34 38 2c 0a 20 2f 2a 20 20 31 35 34 30 20 2a  148,. /*  1540 *
3b6cc 2f 20 20 20 31 39 39 2c 20 20 31 37 37 2c 20 20  /   199,  177,  
3b6cd 32 30 39 2c 20 20 32 30 39 2c 20 20 31 36 36 2c  209,  209,  166,
3b6ce 20 20 31 39 36 2c 20 20 32 33 39 2c 20 20 32 30    196,  239,  20
3b6cf 38 2c 20 20 31 38 35 2c 20 20 31 39 39 2c 0a 20  8,  185,  199,. 
3b6d0 2f 2a 20 20 31 35 35 30 20 2a 2f 20 20 20 20 39  /*  1550 */    9
3b6d1 32 2c 20 20 32 30 39 2c 20 20 32 33 33 2c 20 20  2,  209,  233,  
3b6d2 31 37 33 2c 20 20 32 33 34 2c 20 20 31 38 32 2c  173,  234,  182,
3b6d3 20 20 31 33 39 2c 20 20 31 37 33 2c 20 20 31 38    139,  173,  18
3b6d4 32 2c 20 20 31 39 31 2c 0a 20 2f 2a 20 20 31 35  2,  191,. /*  15
3b6d5 36 30 20 2a 2f 20 20 20 31 39 35 2c 20 20 31 38  60 */   195,  18
3b6d6 32 2c 20 20 32 35 30 2c 20 20 31 38 36 2c 0a 7d  2,  250,  186,.}
3b6d7 3b 0a 23 64 65 66 69 6e 65 20 59 59 5f 53 48 49  ;.#define YY_SHI
3b6d8 46 54 5f 55 53 45 5f 44 46 4c 54 20 28 2d 37 30  FT_USE_DFLT (-70
3b6d9 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 53 48 49  ).#define YY_SHI
3b6da 46 54 5f 43 4f 55 4e 54 20 28 34 31 36 29 0a 23  FT_COUNT (416).#
3b6db 64 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f  define YY_SHIFT_
3b6dc 4d 49 4e 20 20 20 28 2d 36 39 29 0a 23 64 65 66  MIN   (-69).#def
3b6dd 69 6e 65 20 59 59 5f 53 48 49 46 54 5f 4d 41 58  ine YY_SHIFT_MAX
3b6de 20 20 20 28 31 34 38 37 29 0a 73 74 61 74 69 63     (1487).static
3b6df 20 63 6f 6e 73 74 20 73 68 6f 72 74 20 79 79 5f   const short yy_
3b6e0 73 68 69 66 74 5f 6f 66 73 74 5b 5d 20 3d 20 7b  shift_ofst[] = {
3b6e1 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 31  . /*     0 */  1
3b6e2 31 34 33 2c 20 31 31 38 38 2c 20 31 34 31 37 2c  143, 1188, 1417,
3b6e3 20 31 31 38 38 2c 20 31 32 38 37 2c 20 31 32 38   1188, 1287, 128
3b6e4 37 2c 20 20 31 33 38 2c 20 20 31 33 38 2c 20 20  7,  138,  138,  
3b6e5 20 2d 32 2c 20 20 2d 31 39 2c 0a 20 2f 2a 20 20   -2,  -19,. /*  
3b6e6 20 20 31 30 20 2a 2f 20 20 31 32 38 37 2c 20 31    10 */  1287, 1
3b6e7 32 38 37 2c 20 31 32 38 37 2c 20 31 32 38 37 2c  287, 1287, 1287,
3b6e8 20 20 33 34 37 2c 20 20 33 36 32 2c 20 20 31 32    347,  362,  12
3b6e9 39 2c 20 20 31 32 39 2c 20 20 37 39 35 2c 20 31  9,  129,  795, 1
3b6ea 31 36 35 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a  165,. /*    20 *
3b6eb 2f 20 20 31 32 38 37 2c 20 31 32 38 37 2c 20 31  /  1287, 1287, 1
3b6ec 32 38 37 2c 20 31 32 38 37 2c 20 31 32 38 37 2c  287, 1287, 1287,
3b6ed 20 31 32 38 37 2c 20 31 32 38 37 2c 20 31 32 38   1287, 1287, 128
3b6ee 37 2c 20 31 32 38 37 2c 20 31 32 38 37 2c 0a 20  7, 1287, 1287,. 
3b6ef 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 31 32 38  /*    30 */  128
3b6f0 37 2c 20 31 32 38 37 2c 20 31 32 38 37 2c 20 31  7, 1287, 1287, 1
3b6f1 32 38 37 2c 20 31 32 38 37 2c 20 31 32 38 37 2c  287, 1287, 1287,
3b6f2 20 31 32 38 37 2c 20 31 32 38 37 2c 20 31 32 38   1287, 1287, 128
3b6f3 37 2c 20 31 32 38 37 2c 0a 20 2f 2a 20 20 20 20  7, 1287,. /*    
3b6f4 34 30 20 2a 2f 20 20 31 32 38 37 2c 20 31 32 38  40 */  1287, 128
3b6f5 37 2c 20 31 32 38 37 2c 20 31 32 38 37 2c 20 31  7, 1287, 1287, 1
3b6f6 32 38 37 2c 20 31 32 38 37 2c 20 31 32 38 37 2c  287, 1287, 1287,
3b6f7 20 31 32 38 37 2c 20 31 33 31 30 2c 20 31 32 38   1287, 1310, 128
3b6f8 37 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20  7,. /*    50 */ 
3b6f9 20 31 32 38 37 2c 20 31 32 38 37 2c 20 31 32 38   1287, 1287, 128
3b6fa 37 2c 20 31 32 38 37 2c 20 31 32 38 37 2c 20 31  7, 1287, 1287, 1
3b6fb 32 38 37 2c 20 31 32 38 37 2c 20 31 32 38 37 2c  287, 1287, 1287,
3b6fc 20 31 32 38 37 2c 20 31 32 38 37 2c 0a 20 2f 2a   1287, 1287,. /*
3b6fd 20 20 20 20 36 30 20 2a 2f 20 20 31 32 38 37 2c      60 */  1287,
3b6fe 20 31 32 38 37 2c 20 20 32 38 36 2c 20 20 33 36   1287,  286,  36
3b6ff 32 2c 20 20 33 36 32 2c 20 20 35 33 38 2c 20 20  2,  362,  538,  
3b700 35 33 38 2c 20 20 32 33 31 2c 20 31 32 35 33 2c  538,  231, 1253,
3b701 20 20 20 35 35 2c 0a 20 2f 2a 20 20 20 20 37 30     55,. /*    70
3b702 20 2a 2f 20 20 20 37 32 31 2c 20 20 36 34 37 2c   */   721,  647,
3b703 20 20 35 37 33 2c 20 20 34 39 39 2c 20 20 34 32    573,  499,  42
3b704 35 2c 20 20 33 35 31 2c 20 20 32 37 37 2c 20 20  5,  351,  277,  
3b705 32 30 33 2c 20 20 38 36 39 2c 20 20 38 36 39 2c  203,  869,  869,
3b706 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20  . /*    80 */   
3b707 38 36 39 2c 20 20 38 36 39 2c 20 20 38 36 39 2c  869,  869,  869,
3b708 20 20 38 36 39 2c 20 20 38 36 39 2c 20 20 38 36    869,  869,  86
3b709 39 2c 20 20 38 36 39 2c 20 20 38 36 39 2c 20 20  9,  869,  869,  
3b70a 38 36 39 2c 20 20 38 36 39 2c 0a 20 2f 2a 20 20  869,  869,. /*  
3b70b 20 20 39 30 20 2a 2f 20 20 20 38 36 39 2c 20 20    90 */   869,  
3b70c 38 36 39 2c 20 20 38 36 39 2c 20 20 39 34 33 2c  869,  869,  943,
3b70d 20 20 38 36 39 2c 20 31 30 31 37 2c 20 31 30 39    869, 1017, 109
3b70e 31 2c 20 31 30 39 31 2c 20 20 2d 36 39 2c 20 20  1, 1091,  -69,  
3b70f 2d 34 35 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a  -45,. /*   100 *
3b710 2f 20 20 20 2d 34 35 2c 20 20 2d 34 35 2c 20 20  /   -45,  -45,  
3b711 2d 34 35 2c 20 20 2d 34 35 2c 20 20 20 2d 31 2c  -45,  -45,   -1,
3b712 20 20 20 32 34 2c 20 20 32 34 35 2c 20 20 33 36     24,  245,  36
3b713 32 2c 20 20 33 36 32 2c 20 20 33 36 32 2c 0a 20  2,  362,  362,. 
3b714 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 33 36  /*   110 */   36
3b715 32 2c 20 20 33 36 32 2c 20 20 33 36 32 2c 20 20  2,  362,  362,  
3b716 33 36 32 2c 20 20 33 36 32 2c 20 20 33 36 32 2c  362,  362,  362,
3b717 20 20 33 36 32 2c 20 20 33 36 32 2c 20 20 33 36    362,  362,  36
3b718 32 2c 20 20 33 36 32 2c 0a 20 2f 2a 20 20 20 31  2,  362,. /*   1
3b719 32 30 20 2a 2f 20 20 20 33 36 32 2c 20 20 33 36  20 */   362,  36
3b71a 32 2c 20 20 33 36 32 2c 20 20 33 38 38 2c 20 20  2,  362,  388,  
3b71b 33 35 36 2c 20 20 33 36 32 2c 20 20 33 36 32 2c  356,  362,  362,
3b71c 20 20 33 36 32 2c 20 20 33 36 32 2c 20 20 33 36    362,  362,  36
3b71d 32 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20  2,. /*   130 */ 
3b71e 20 20 37 33 32 2c 20 20 38 36 38 2c 20 20 32 33    732,  868,  23
3b71f 31 2c 20 31 30 35 31 2c 20 31 34 35 38 2c 20 20  1, 1051, 1458,  
3b720 2d 37 30 2c 20 20 2d 37 30 2c 20 20 2d 37 30 2c  -70,  -70,  -70,
3b721 20 31 33 36 37 2c 20 20 20 35 37 2c 0a 20 2f 2a   1367,   57,. /*
3b722 20 20 20 31 34 30 20 2a 2f 20 20 20 34 33 34 2c     140 */   434,
3b723 20 20 34 33 34 2c 20 20 32 38 39 2c 20 20 32 39    434,  289,  29
3b724 31 2c 20 20 32 38 35 2c 20 20 20 20 31 2c 20 20  1,  285,    1,  
3b725 32 30 34 2c 20 20 35 37 32 2c 20 20 35 33 39 2c  204,  572,  539,
3b726 20 20 33 36 32 2c 0a 20 2f 2a 20 20 20 31 35 30    362,. /*   150
3b727 20 2a 2f 20 20 20 33 36 32 2c 20 20 33 36 32 2c   */   362,  362,
3b728 20 20 33 36 32 2c 20 20 33 36 32 2c 20 20 33 36    362,  362,  36
3b729 32 2c 20 20 33 36 32 2c 20 20 33 36 32 2c 20 20  2,  362,  362,  
3b72a 33 36 32 2c 20 20 33 36 32 2c 20 20 33 36 32 2c  362,  362,  362,
3b72b 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20  . /*   160 */   
3b72c 33 36 32 2c 20 20 33 36 32 2c 20 20 33 36 32 2c  362,  362,  362,
3b72d 20 20 33 36 32 2c 20 20 33 36 32 2c 20 20 33 36    362,  362,  36
3b72e 32 2c 20 20 33 36 32 2c 20 20 33 36 32 2c 20 20  2,  362,  362,  
3b72f 33 36 32 2c 20 20 33 36 32 2c 0a 20 2f 2a 20 20  362,  362,. /*  
3b730 20 31 37 30 20 2a 2f 20 20 20 33 36 32 2c 20 20   170 */   362,  
3b731 33 36 32 2c 20 20 33 36 32 2c 20 20 33 36 32 2c  362,  362,  362,
3b732 20 20 33 36 32 2c 20 20 33 36 32 2c 20 20 33 36    362,  362,  36
3b733 32 2c 20 20 33 36 32 2c 20 20 33 36 32 2c 20 20  2,  362,  362,  
3b734 33 36 32 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a  362,. /*   180 *
3b735 2f 20 20 20 33 36 32 2c 20 20 35 30 36 2c 20 20  /   362,  506,  
3b736 35 30 36 2c 20 20 35 30 36 2c 20 20 37 30 35 2c  506,  506,  705,
3b737 20 31 32 35 33 2c 20 31 32 35 33 2c 20 31 32 35   1253, 1253, 125
3b738 33 2c 20 20 2d 37 30 2c 20 20 2d 37 30 2c 0a 20  3,  -70,  -70,. 
3b739 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 2d 37  /*   190 */   -7
3b73a 30 2c 20 20 31 37 31 2c 20 20 31 37 31 2c 20 20  0,  171,  171,  
3b73b 31 36 30 2c 20 20 35 30 32 2c 20 20 35 30 32 2c  160,  502,  502,
3b73c 20 20 35 30 32 2c 20 20 34 34 36 2c 20 20 34 33    502,  446,  43
3b73d 32 2c 20 20 35 31 31 2c 0a 20 2f 2a 20 20 20 32  2,  511,. /*   2
3b73e 30 30 20 2a 2f 20 20 20 34 32 32 2c 20 20 33 35  00 */   422,  35
3b73f 38 2c 20 20 33 33 35 2c 20 20 2d 31 32 2c 20 20  8,  335,  -12,  
3b740 2d 31 32 2c 20 20 2d 31 32 2c 20 20 2d 31 32 2c  -12,  -12,  -12,
3b741 20 20 35 37 36 2c 20 20 32 39 34 2c 20 20 2d 31    576,  294,  -1
3b742 32 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20  2,. /*   210 */ 
3b743 20 20 2d 31 32 2c 20 20 32 39 35 2c 20 20 35 39    -12,  295,  59
3b744 35 2c 20 20 31 34 31 2c 20 20 36 30 30 2c 20 20  5,  141,  600,  
3b745 37 33 30 2c 20 20 37 32 33 2c 20 20 37 32 33 2c  730,  723,  723,
3b746 20 20 38 30 35 2c 20 20 37 33 30 2c 0a 20 2f 2a    805,  730,. /*
3b747 20 20 20 32 32 30 20 2a 2f 20 20 20 38 30 35 2c     220 */   805,
3b748 20 20 34 33 39 2c 20 20 39 31 31 2c 20 20 32 33    439,  911,  23
3b749 31 2c 20 20 38 36 35 2c 20 20 32 33 31 2c 20 20  1,  865,  231,  
3b74a 38 36 35 2c 20 20 38 30 37 2c 20 20 38 36 35 2c  865,  807,  865,
3b74b 20 20 37 32 33 2c 0a 20 2f 2a 20 20 20 32 33 30    723,. /*   230
3b74c 20 2a 2f 20 20 20 37 36 36 2c 20 20 36 33 33 2c   */   766,  633,
3b74d 20 20 36 33 33 2c 20 20 32 33 31 2c 20 20 32 38    633,  231,  28
3b74e 34 2c 20 20 20 36 33 2c 20 20 36 30 38 2c 20 31  4,   63,  608, 1
3b74f 34 37 36 2c 20 31 33 30 38 2c 20 31 33 30 38 2c  476, 1308, 1308,
3b750 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 31  . /*   240 */  1
3b751 34 37 32 2c 20 31 34 37 32 2c 20 31 33 30 38 2c  472, 1472, 1308,
3b752 20 31 34 37 37 2c 20 31 34 32 35 2c 20 31 32 37   1477, 1425, 127
3b753 35 2c 20 31 34 38 37 2c 20 31 34 38 37 2c 20 31  5, 1487, 1487, 1
3b754 34 38 37 2c 20 31 34 38 37 2c 0a 20 2f 2a 20 20  487, 1487,. /*  
3b755 20 32 35 30 20 2a 2f 20 20 31 33 30 38 2c 20 31   250 */  1308, 1
3b756 34 36 31 2c 20 31 32 37 35 2c 20 31 34 37 37 2c  461, 1275, 1477,
3b757 20 31 34 32 35 2c 20 31 34 32 35 2c 20 31 33 30   1425, 1425, 130
3b758 38 2c 20 31 34 36 31 2c 20 31 33 35 35 2c 20 31  8, 1461, 1355, 1
3b759 34 34 31 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a  441,. /*   260 *
3b75a 2f 20 20 31 33 30 38 2c 20 31 33 30 38 2c 20 31  /  1308, 1308, 1
3b75b 34 36 31 2c 20 31 33 30 38 2c 20 31 34 36 31 2c  461, 1308, 1461,
3b75c 20 31 33 30 38 2c 20 31 34 36 31 2c 20 31 34 34   1308, 1461, 144
3b75d 32 2c 20 31 33 34 38 2c 20 31 33 34 38 2c 0a 20  2, 1348, 1348,. 
3b75e 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 31 33 34  /*   270 */  134
3b75f 38 2c 20 31 34 30 38 2c 20 31 33 37 35 2c 20 31  8, 1408, 1375, 1
3b760 33 37 35 2c 20 31 34 34 32 2c 20 31 33 34 38 2c  375, 1442, 1348,
3b761 20 31 33 35 37 2c 20 31 33 34 38 2c 20 31 34 30   1357, 1348, 140
3b762 38 2c 20 31 33 34 38 2c 0a 20 2f 2a 20 20 20 32  8, 1348,. /*   2
3b763 38 30 20 2a 2f 20 20 31 33 34 38 2c 20 31 33 31  80 */  1348, 131
3b764 36 2c 20 31 33 33 31 2c 20 31 33 31 36 2c 20 31  6, 1331, 1316, 1
3b765 33 33 31 2c 20 31 33 31 36 2c 20 31 33 33 31 2c  331, 1316, 1331,
3b766 20 31 33 30 38 2c 20 31 33 30 38 2c 20 31 32 38   1308, 1308, 128
3b767 30 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20  0,. /*   290 */ 
3b768 20 31 32 38 38 2c 20 31 32 38 39 2c 20 31 32 38   1288, 1289, 128
3b769 35 2c 20 31 32 37 39 2c 20 31 32 37 35 2c 20 31  5, 1279, 1275, 1
3b76a 32 35 33 2c 20 31 33 33 36 2c 20 31 33 34 36 2c  253, 1336, 1346,
3b76b 20 31 33 34 36 2c 20 31 33 33 38 2c 0a 20 2f 2a   1346, 1338,. /*
3b76c 20 20 20 33 30 30 20 2a 2f 20 20 31 33 33 38 2c     300 */  1338,
3b76d 20 31 33 33 38 2c 20 31 33 33 38 2c 20 20 2d 37   1338, 1338,  -7
3b76e 30 2c 20 20 2d 37 30 2c 20 20 2d 37 30 2c 20 20  0,  -70,  -70,  
3b76f 2d 37 30 2c 20 20 2d 37 30 2c 20 20 2d 37 30 2c  -70,  -70,  -70,
3b770 20 31 30 31 33 2c 0a 20 2f 2a 20 20 20 33 31 30   1013,. /*   310
3b771 20 2a 2f 20 20 20 34 36 37 2c 20 20 36 31 32 2c   */   467,  612,
3b772 20 20 20 38 34 2c 20 20 31 37 39 2c 20 20 2d 32     84,  179,  -2
3b773 38 2c 20 20 38 37 30 2c 20 20 34 31 30 2c 20 20  8,  870,  410,  
3b774 37 36 31 2c 20 20 37 36 30 2c 20 20 36 36 37 2c  761,  760,  667,
3b775 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 20  . /*   320 */   
3b776 36 35 30 2c 20 20 35 33 31 2c 20 20 32 32 30 2c  650,  531,  220,
3b777 20 20 33 36 31 2c 20 20 33 33 31 2c 20 20 31 32    361,  331,  12
3b778 35 2c 20 20 31 32 37 2c 20 20 20 39 37 2c 20 31  5,  127,   97, 1
3b779 33 30 36 2c 20 31 33 30 30 2c 0a 20 2f 2a 20 20  306, 1300,. /*  
3b77a 20 33 33 30 20 2a 2f 20 20 31 32 37 30 2c 20 31   330 */  1270, 1
3b77b 31 35 31 2c 20 31 32 37 32 2c 20 31 32 30 33 2c  151, 1272, 1203,
3b77c 20 31 32 33 32 2c 20 31 32 36 31 2c 20 31 32 34   1232, 1261, 124
3b77d 34 2c 20 31 31 34 38 2c 20 31 31 37 34 2c 20 31  4, 1148, 1174, 1
3b77e 31 33 39 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a  139,. /*   340 *
3b77f 2f 20 20 31 31 35 36 2c 20 31 31 32 34 2c 20 31  /  1156, 1124, 1
3b780 32 32 30 2c 20 31 31 31 35 2c 20 31 32 31 30 2c  220, 1115, 1210,
3b781 20 31 32 33 33 2c 20 31 30 39 39 2c 20 31 31 39   1233, 1099, 119
3b782 33 2c 20 31 31 38 34 2c 20 31 31 37 34 2c 0a 20  3, 1184, 1174,. 
3b783 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 31 31 37  /*   350 */  117
3b784 33 2c 20 31 30 32 39 2c 20 31 31 32 31 2c 20 31  3, 1029, 1121, 1
3b785 31 32 30 2c 20 31 30 38 35 2c 20 31 31 36 32 2c  120, 1085, 1162,
3b786 20 31 31 31 39 2c 20 31 30 33 37 2c 20 31 31 35   1119, 1037, 115
3b787 32 2c 20 31 31 34 37 2c 0a 20 2f 2a 20 20 20 33  2, 1147,. /*   3
3b788 36 30 20 2a 2f 20 20 31 31 32 39 2c 20 31 30 34  60 */  1129, 104
3b789 36 2c 20 31 30 31 31 2c 20 31 30 39 33 2c 20 31  6, 1011, 1093, 1
3b78a 30 39 38 2c 20 31 30 37 35 2c 20 31 30 36 31 2c  098, 1075, 1061,
3b78b 20 31 30 33 32 2c 20 20 39 36 30 2c 20 31 30 35   1032,  960, 105
3b78c 37 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20  7,. /*   370 */ 
3b78d 20 31 30 33 31 2c 20 31 30 33 30 2c 20 20 38 39   1031, 1030,  89
3b78e 39 2c 20 20 39 33 38 2c 20 20 39 38 32 2c 20 20  9,  938,  982,  
3b78f 39 33 36 2c 20 20 39 37 32 2c 20 20 39 35 38 2c  936,  972,  958,
3b790 20 20 39 31 30 2c 20 20 39 35 35 2c 0a 20 2f 2a    910,  955,. /*
3b791 20 20 20 33 38 30 20 2a 2f 20 20 20 38 37 35 2c     380 */   875,
3b792 20 20 38 38 35 2c 20 20 39 30 38 2c 20 20 38 35    885,  908,  85
3b793 37 2c 20 20 38 35 39 2c 20 20 38 36 37 2c 20 20  7,  859,  867,  
3b794 38 30 34 2c 20 20 35 39 30 2c 20 20 38 33 34 2c  804,  590,  834,
3b795 20 20 37 34 37 2c 0a 20 2f 2a 20 20 20 33 39 30    747,. /*   390
3b796 20 2a 2f 20 20 20 38 31 38 2c 20 20 35 31 33 2c   */   818,  513,
3b797 20 20 36 31 31 2c 20 20 37 34 31 2c 20 20 36 37    611,  741,  67
3b798 33 2c 20 20 36 33 37 2c 20 20 36 31 31 2c 20 20  3,  637,  611,  
3b799 36 30 36 2c 20 20 36 30 33 2c 20 20 35 37 39 2c  606,  603,  579,
3b79a 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20 20  . /*   400 */   
3b79b 35 30 31 2c 20 20 35 34 31 2c 20 20 34 36 38 2c  501,  541,  468,
3b79c 20 20 33 38 36 2c 20 20 34 34 35 2c 20 20 33 39    386,  445,  39
3b79d 35 2c 20 20 33 37 36 2c 20 20 32 38 31 2c 20 20  5,  376,  281,  
3b79e 31 38 35 2c 20 20 31 32 30 2c 0a 20 2f 2a 20 20  185,  120,. /*  
3b79f 20 34 31 30 20 2a 2f 20 20 20 20 39 32 2c 20 20   410 */    92,  
3b7a0 20 37 35 2c 20 20 20 34 35 2c 20 20 31 31 34 2c   75,   45,  114,
3b7a1 20 20 20 32 35 2c 20 20 20 31 31 2c 20 20 20 20     25,   11,    
3b7a2 35 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59  5,.};.#define YY
3b7a3 5f 52 45 44 55 43 45 5f 55 53 45 5f 44 46 4c 54  _REDUCE_USE_DFLT
3b7a4 20 28 2d 31 36 39 29 0a 23 64 65 66 69 6e 65 20   (-169).#define 
3b7a5 59 59 5f 52 45 44 55 43 45 5f 43 4f 55 4e 54 20  YY_REDUCE_COUNT 
3b7a6 28 33 30 38 29 0a 23 64 65 66 69 6e 65 20 59 59  (308).#define YY
3b7a7 5f 52 45 44 55 43 45 5f 4d 49 4e 20 20 20 28 2d  _REDUCE_MIN   (-
3b7a8 31 36 38 29 0a 23 64 65 66 69 6e 65 20 59 59 5f  168).#define YY_
3b7a9 52 45 44 55 43 45 5f 4d 41 58 20 20 20 28 31 33  REDUCE_MAX   (13
3b7aa 39 31 29 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  91).static const
3b7ab 20 73 68 6f 72 74 20 79 79 5f 72 65 64 75 63 65   short yy_reduce
3b7ac 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20  _ofst[] = {. /* 
3b7ad 20 20 20 20 30 20 2a 2f 20 20 2d 31 34 31 2c 20      0 */  -141, 
3b7ae 20 20 39 30 2c 20 31 30 39 35 2c 20 20 32 32 32    90, 1095,  222
3b7af 2c 20 20 31 35 38 2c 20 20 31 35 36 2c 20 20 20  ,  158,  156,   
3b7b0 31 39 2c 20 20 20 31 37 2c 20 20 20 31 30 2c 20  19,   17,   10, 
3b7b1 2d 31 30 34 2c 0a 20 2f 2a 20 20 20 20 31 30 20  -104,. /*    10 
3b7b2 2a 2f 20 20 20 33 37 38 2c 20 20 33 31 36 2c 20  */   378,  316, 
3b7b3 20 33 31 31 2c 20 20 20 31 32 2c 20 20 31 38 30   311,   12,  180
3b7b4 2c 20 20 32 34 39 2c 20 20 35 39 38 2c 20 20 34  ,  249,  598,  4
3b7b5 36 34 2c 20 20 33 39 37 2c 20 31 31 38 31 2c 0a  64,  397, 1181,.
3b7b6 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 31 31   /*    20 */  11
3b7b7 37 37 2c 20 31 31 37 35 2c 20 31 31 32 38 2c 20  77, 1175, 1128, 
3b7b8 31 31 30 36 2c 20 31 30 39 36 2c 20 31 30 35 34  1106, 1096, 1054
3b7b9 2c 20 31 30 33 38 2c 20 20 39 37 34 2c 20 20 39  , 1038,  974,  9
3b7ba 36 34 2c 20 20 39 36 32 2c 0a 20 2f 2a 20 20 20  64,  962,. /*   
3b7bb 20 33 30 20 2a 2f 20 20 20 39 34 38 2c 20 20 39   30 */   948,  9
3b7bc 30 35 2c 20 20 39 30 33 2c 20 20 39 30 30 2c 20  05,  903,  900, 
3b7bd 20 38 38 37 2c 20 20 38 37 34 2c 20 20 38 33 32   887,  874,  832
3b7be 2c 20 20 38 32 36 2c 20 20 38 31 36 2c 20 20 38  ,  826,  816,  8
3b7bf 31 33 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f  13,. /*    40 */
3b7c0 20 20 20 38 30 30 2c 20 20 37 35 38 2c 20 20 37     800,  758,  7
3b7c1 35 35 2c 20 20 37 35 32 2c 20 20 37 34 32 2c 20  55,  752,  742, 
3b7c2 20 37 33 39 2c 20 20 37 32 36 2c 20 20 36 38 35   739,  726,  685
3b7c3 2c 20 20 36 38 31 2c 20 20 36 36 38 2c 0a 20 2f  ,  681,  668,. /
3b7c4 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 36 36 35  *    50 */   665
3b7c5 2c 20 20 36 35 32 2c 20 20 36 30 37 2c 20 20 36  ,  652,  607,  6
3b7c6 30 34 2c 20 20 35 39 34 2c 20 20 35 39 31 2c 20  04,  594,  591, 
3b7c7 20 35 37 38 2c 20 20 35 33 30 2c 20 20 35 32 38   578,  530,  528
3b7c8 2c 20 20 35 32 36 2c 0a 20 2f 2a 20 20 20 20 36  ,  526,. /*    6
3b7c9 30 20 2a 2f 20 20 20 33 38 35 2c 20 20 20 31 38  0 */   385,   18
3b7ca 2c 20 20 34 37 37 2c 20 20 34 36 36 2c 20 20 35  ,  477,  466,  5
3b7cb 31 39 2c 20 20 34 34 34 2c 20 20 33 35 30 2c 20  19,  444,  350, 
3b7cc 20 34 33 35 2c 20 20 34 30 35 2c 20 20 34 38 38   435,  405,  488
3b7cd 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20  ,. /*    70 */  
3b7ce 20 34 38 38 2c 20 20 34 38 38 2c 20 20 34 38 38   488,  488,  488
3b7cf 2c 20 20 34 38 38 2c 20 20 34 38 38 2c 20 20 34  ,  488,  488,  4
3b7d0 38 38 2c 20 20 34 38 38 2c 20 20 34 38 38 2c 20  88,  488,  488, 
3b7d1 20 34 38 38 2c 20 20 34 38 38 2c 0a 20 2f 2a 20   488,  488,. /* 
3b7d2 20 20 20 38 30 20 2a 2f 20 20 20 34 38 38 2c 20     80 */   488, 
3b7d3 20 34 38 38 2c 20 20 34 38 38 2c 20 20 34 38 38   488,  488,  488
3b7d4 2c 20 20 34 38 38 2c 20 20 34 38 38 2c 20 20 34  ,  488,  488,  4
3b7d5 38 38 2c 20 20 34 38 38 2c 20 20 34 38 38 2c 20  88,  488,  488, 
3b7d6 20 34 38 38 2c 0a 20 2f 2a 20 20 20 20 39 30 20   488,. /*    90 
3b7d7 2a 2f 20 20 20 34 38 38 2c 20 20 34 38 38 2c 20  */   488,  488, 
3b7d8 20 34 38 38 2c 20 20 34 38 38 2c 20 20 34 38 38   488,  488,  488
3b7d9 2c 20 20 34 38 38 2c 20 20 34 38 38 2c 20 20 34  ,  488,  488,  4
3b7da 38 38 2c 20 20 34 38 38 2c 20 20 34 38 38 2c 0a  88,  488,  488,.
3b7db 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 34   /*   100 */   4
3b7dc 38 38 2c 20 20 34 38 38 2c 20 20 34 38 38 2c 20  88,  488,  488, 
3b7dd 20 34 38 38 2c 20 20 34 38 38 2c 20 20 34 38 38   488,  488,  488
3b7de 2c 20 20 34 38 38 2c 20 31 30 34 30 2c 20 20 36  ,  488, 1040,  6
3b7df 37 38 2c 20 31 30 33 36 2c 0a 20 2f 2a 20 20 20  78, 1036,. /*   
3b7e0 31 31 30 20 2a 2f 20 20 31 30 30 37 2c 20 20 39  110 */  1007,  9
3b7e1 36 37 2c 20 20 39 36 36 2c 20 20 39 36 35 2c 20  67,  966,  965, 
3b7e2 20 38 34 35 2c 20 20 36 38 36 2c 20 20 36 31 30   845,  686,  610
3b7e3 2c 20 20 36 38 34 2c 20 20 33 31 37 2c 20 20 36  ,  684,  317,  6
3b7e4 37 32 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f  72,. /*   120 */
3b7e5 20 20 20 38 39 33 2c 20 20 33 32 37 2c 20 20 36     893,  327,  6
3b7e6 32 33 2c 20 20 35 32 32 2c 20 20 20 2d 37 2c 20  23,  522,   -7, 
3b7e7 20 38 32 30 2c 20 20 38 31 34 2c 20 20 31 35 37   820,  814,  157
3b7e8 2c 20 20 31 35 34 2c 20 20 31 30 31 2c 0a 20 2f  ,  154,  101,. /
3b7e9 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 37 30 32  *   130 */   702
3b7ea 2c 20 20 34 39 34 2c 20 20 35 38 30 2c 20 20 34  ,  494,  580,  4
3b7eb 38 38 2c 20 20 34 38 38 2c 20 20 34 38 38 2c 20  88,  488,  488, 
3b7ec 20 34 38 38 2c 20 20 34 38 38 2c 20 20 36 31 34   488,  488,  614
3b7ed 2c 20 20 35 38 36 2c 0a 20 2f 2a 20 20 20 31 34  ,  586,. /*   14
3b7ee 30 20 2a 2f 20 20 20 39 33 35 2c 20 20 38 39 32  0 */   935,  892
3b7ef 2c 20 20 39 36 38 2c 20 31 32 34 35 2c 20 31 32  ,  968, 1245, 12
3b7f0 34 32 2c 20 31 32 33 34 2c 20 31 32 32 35 2c 20  42, 1234, 1225, 
3b7f1 20 37 39 38 2c 20 20 37 39 38 2c 20 31 32 32 32   798,  798, 1222
3b7f2 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20  ,. /*   150 */  
3b7f3 31 32 32 31 2c 20 31 32 31 38 2c 20 31 32 31 34  1221, 1218, 1214
3b7f4 2c 20 31 32 31 33 2c 20 31 32 31 32 2c 20 31 32  , 1213, 1212, 12
3b7f5 30 32 2c 20 31 31 39 35 2c 20 31 31 39 31 2c 20  02, 1195, 1191, 
3b7f6 31 31 36 31 2c 20 31 31 35 38 2c 0a 20 2f 2a 20  1161, 1158,. /* 
3b7f7 20 20 31 36 30 20 2a 2f 20 20 31 31 34 30 2c 20    160 */  1140, 
3b7f8 31 31 33 35 2c 20 31 31 32 33 2c 20 31 31 31 32  1135, 1123, 1112
3b7f9 2c 20 31 31 30 37 2c 20 31 31 30 30 2c 20 31 30  , 1107, 1100, 10
3b7fa 38 30 2c 20 31 30 37 34 2c 20 31 30 37 33 2c 20  80, 1074, 1073, 
3b7fb 31 30 37 32 2c 0a 20 2f 2a 20 20 20 31 37 30 20  1072,. /*   170 
3b7fc 2a 2f 20 20 31 30 37 30 2c 20 31 30 36 37 2c 20  */  1070, 1067, 
3b7fd 31 30 34 38 2c 20 31 30 34 34 2c 20 20 39 36 39  1048, 1044,  969
3b7fe 2c 20 20 39 36 38 2c 20 20 39 30 37 2c 20 20 39  ,  968,  907,  9
3b7ff 30 36 2c 20 20 39 30 34 2c 20 20 38 39 34 2c 0a  06,  904,  894,.
3b800 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 38   /*   180 */   8
3b801 33 33 2c 20 20 38 33 37 2c 20 20 38 33 36 2c 20  33,  837,  836, 
3b802 20 33 34 30 2c 20 20 38 32 37 2c 20 20 38 31 35   340,  827,  815
3b803 2c 20 20 37 37 35 2c 20 20 20 36 38 2c 20 20 37  ,  775,   68,  7
3b804 32 32 2c 20 20 36 34 36 2c 0a 20 2f 2a 20 20 20  22,  646,. /*   
3b805 31 39 30 20 2a 2f 20 20 2d 31 36 38 2c 20 31 33  190 */  -168, 13
3b806 38 34 2c 20 31 33 38 30 2c 20 31 33 37 37 2c 20  84, 1380, 1377, 
3b807 31 33 37 39 2c 20 31 33 37 36 2c 20 31 33 37 33  1379, 1376, 1373
3b808 2c 20 31 33 33 39 2c 20 31 33 36 35 2c 20 31 33  , 1339, 1365, 13
3b809 36 38 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f  68,. /*   200 */
3b80a 20 20 31 33 36 35 2c 20 31 33 36 35 2c 20 31 33    1365, 1365, 13
3b80b 36 35 2c 20 31 33 36 35 2c 20 31 33 36 35 2c 20  65, 1365, 1365, 
3b80c 31 33 36 35 2c 20 31 33 36 35 2c 20 31 33 32 30  1365, 1365, 1320
3b80d 2c 20 31 33 31 39 2c 20 31 33 36 35 2c 0a 20 2f  , 1319, 1365,. /
3b80e 2a 20 20 20 32 31 30 20 2a 2f 20 20 31 33 36 35  *   210 */  1365
3b80f 2c 20 31 33 33 39 2c 20 31 33 37 38 2c 20 31 33  , 1339, 1378, 13
3b810 34 39 2c 20 31 33 39 31 2c 20 31 33 35 30 2c 20  49, 1391, 1350, 
3b811 31 33 34 32 2c 20 31 33 33 34 2c 20 31 33 30 37  1342, 1334, 1307
3b812 2c 20 31 33 34 31 2c 0a 20 2f 2a 20 20 20 32 32  , 1341,. /*   22
3b813 30 20 2a 2f 20 20 31 32 39 33 2c 20 31 33 36 34  0 */  1293, 1364
3b814 2c 20 31 33 36 33 2c 20 31 33 37 31 2c 20 31 33  , 1363, 1371, 13
3b815 36 32 2c 20 31 33 37 30 2c 20 31 33 35 39 2c 20  62, 1370, 1359, 
3b816 31 33 34 30 2c 20 31 33 35 34 2c 20 31 33 33 33  1340, 1354, 1333
3b817 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20  ,. /*   230 */  
3b818 31 33 30 35 2c 20 31 33 30 34 2c 20 31 32 39 39  1305, 1304, 1299
3b819 2c 20 31 33 36 31 2c 20 31 33 32 38 2c 20 31 33  , 1361, 1328, 13
3b81a 32 34 2c 20 31 33 36 36 2c 20 31 32 38 32 2c 20  24, 1366, 1282, 
3b81b 31 33 36 30 2c 20 31 33 35 38 2c 0a 20 2f 2a 20  1360, 1358,. /* 
3b81c 20 20 32 34 30 20 2a 2f 20 20 31 32 37 38 2c 20    240 */  1278, 
3b81d 31 32 37 36 2c 20 31 33 35 36 2c 20 31 32 39 32  1276, 1356, 1292
3b81e 2c 20 31 33 32 32 2c 20 31 33 30 39 2c 20 31 33  , 1322, 1309, 13
3b81f 31 37 2c 20 31 33 31 35 2c 20 31 33 31 34 2c 20  17, 1315, 1314, 
3b820 31 33 31 32 2c 0a 20 2f 2a 20 20 20 32 35 30 20  1312,. /*   250 
3b821 2a 2f 20 20 31 33 34 35 2c 20 31 33 34 37 2c 20  */  1345, 1347, 
3b822 31 33 30 32 2c 20 31 32 37 37 2c 20 31 33 31 31  1302, 1277, 1311
3b823 2c 20 31 33 30 33 2c 20 31 33 33 37 2c 20 31 33  , 1303, 1337, 13
3b824 33 35 2c 20 31 32 35 32 2c 20 31 32 34 38 2c 0a  35, 1252, 1248,.
3b825 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 31 33   /*   260 */  13
3b826 33 32 2c 20 31 33 33 30 2c 20 31 33 32 39 2c 20  32, 1330, 1329, 
3b827 31 33 32 37 2c 20 31 33 32 36 2c 20 31 33 32 33  1327, 1326, 1323
3b828 2c 20 31 33 32 31 2c 20 31 32 39 37 2c 20 31 33  , 1321, 1297, 13
3b829 30 31 2c 20 31 32 39 35 2c 0a 20 2f 2a 20 20 20  01, 1295,. /*   
3b82a 32 37 30 20 2a 2f 20 20 31 32 39 34 2c 20 31 32  270 */  1294, 12
3b82b 39 30 2c 20 31 32 34 33 2c 20 31 32 34 30 2c 20  90, 1243, 1240, 
3b82c 31 32 38 34 2c 20 31 32 39 31 2c 20 31 32 38 36  1284, 1291, 1286
3b82d 2c 20 31 32 38 33 2c 20 31 32 37 34 2c 20 31 32  , 1283, 1274, 12
3b82e 38 31 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f  81,. /*   280 */
3b82f 20 20 31 32 37 31 2c 20 31 32 33 38 2c 20 31 32    1271, 1238, 12
3b830 34 31 2c 20 31 32 33 36 2c 20 31 32 33 35 2c 20  41, 1236, 1235, 
3b831 31 32 32 37 2c 20 31 32 32 36 2c 20 31 32 36 37  1227, 1226, 1267
3b832 2c 20 31 32 36 36 2c 20 31 31 38 39 2c 0a 20 2f  , 1266, 1189,. /
3b833 2a 20 20 20 32 39 30 20 2a 2f 20 20 31 32 32 39  *   290 */  1229
3b834 2c 20 31 32 32 33 2c 20 31 32 31 31 2c 20 31 32  , 1223, 1211, 12
3b835 30 36 2c 20 31 32 30 31 2c 20 31 31 39 37 2c 20  06, 1201, 1197, 
3b836 31 32 33 39 2c 20 31 32 33 37 2c 20 31 32 31 39  1239, 1237, 1219
3b837 2c 20 31 32 31 36 2c 0a 20 2f 2a 20 20 20 33 30  , 1216,. /*   30
3b838 30 20 2a 2f 20 20 31 32 30 39 2c 20 31 32 30 38  0 */  1209, 1208
3b839 2c 20 31 31 38 35 2c 20 31 30 38 39 2c 20 31 30  , 1185, 1089, 10
3b83a 38 36 2c 20 31 30 38 37 2c 20 31 31 33 37 2c 20  86, 1087, 1137, 
3b83b 31 31 33 36 2c 20 31 31 36 34 2c 0a 7d 3b 0a 73  1136, 1164,.};.s
3b83c 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59 41 43  tatic const YYAC
3b83d 54 49 4f 4e 54 59 50 45 20 79 79 5f 64 65 66 61  TIONTYPE yy_defa
3b83e 75 6c 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20  ult[] = {. /*   
3b83f 20 20 30 20 2a 2f 20 20 20 36 33 32 2c 20 20 38    0 */   632,  8
3b840 36 36 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  66,  954,  954, 
3b841 20 38 36 36 2c 20 20 38 36 36 2c 20 20 39 35 34   866,  866,  954
3b842 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 37  ,  954,  954,  7
3b843 35 36 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f  56,. /*    10 */
3b844 20 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39     954,  954,  9
3b845 35 34 2c 20 20 38 36 34 2c 20 20 39 35 34 2c 20  54,  864,  954, 
3b846 20 39 35 34 2c 20 20 37 38 34 2c 20 20 37 38 34   954,  784,  784
3b847 2c 20 20 39 32 38 2c 20 20 39 35 34 2c 0a 20 2f  ,  928,  954,. /
3b848 2a 20 20 20 20 32 30 20 2a 2f 20 20 20 39 35 34  *    20 */   954
3b849 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b84a 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b84b 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b84c 2c 20 20 39 35 34 2c 0a 20 2f 2a 20 20 20 20 33  ,  954,. /*    3
3b84d 30 20 2a 2f 20 20 20 39 35 34 2c 20 20 39 35 34  0 */   954,  954
3b84e 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b84f 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b850 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b851 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20  ,. /*    40 */  
3b852 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b853 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b854 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b855 20 39 35 34 2c 20 20 39 35 34 2c 0a 20 2f 2a 20   954,  954,. /* 
3b856 20 20 20 35 30 20 2a 2f 20 20 20 39 35 34 2c 20     50 */   954, 
3b857 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b858 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b859 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b85a 20 39 35 34 2c 0a 20 2f 2a 20 20 20 20 36 30 20   954,. /*    60 
3b85b 2a 2f 20 20 20 39 35 34 2c 20 20 39 35 34 2c 20  */   954,  954, 
3b85c 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b85d 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 36  ,  954,  954,  6
3b85e 37 31 2c 20 20 37 36 30 2c 20 20 37 39 30 2c 0a  71,  760,  790,.
3b85f 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20 39   /*    70 */   9
3b860 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b861 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b862 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b863 32 37 2c 20 20 39 32 39 2c 0a 20 2f 2a 20 20 20  27,  929,. /*   
3b864 20 38 30 20 2a 2f 20 20 20 37 39 38 2c 20 20 37   80 */   798,  7
3b865 39 37 2c 20 20 39 30 37 2c 20 20 37 37 31 2c 20  97,  907,  771, 
3b866 20 37 39 35 2c 20 20 37 38 38 2c 20 20 37 39 32   795,  788,  792
3b867 2c 20 20 38 36 37 2c 20 20 38 36 30 2c 20 20 38  ,  867,  860,  8
3b868 36 31 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f  61,. /*    90 */
3b869 20 20 20 38 35 39 2c 20 20 38 36 33 2c 20 20 38     859,  863,  8
3b86a 36 38 2c 20 20 39 35 34 2c 20 20 37 39 31 2c 20  68,  954,  791, 
3b86b 20 38 32 37 2c 20 20 38 34 34 2c 20 20 38 32 36   827,  844,  826
3b86c 2c 20 20 38 33 38 2c 20 20 38 34 33 2c 0a 20 2f  ,  838,  843,. /
3b86d 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 38 35 30  *   100 */   850
3b86e 2c 20 20 38 34 32 2c 20 20 38 33 39 2c 20 20 38  ,  842,  839,  8
3b86f 32 39 2c 20 20 38 32 38 2c 20 20 38 33 30 2c 20  29,  828,  830, 
3b870 20 38 33 31 2c 20 20 39 35 34 2c 20 20 39 35 34   831,  954,  954
3b871 2c 20 20 39 35 34 2c 0a 20 2f 2a 20 20 20 31 31  ,  954,. /*   11
3b872 30 20 2a 2f 20 20 20 39 35 34 2c 20 20 39 35 34  0 */   954,  954
3b873 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b874 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b875 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b876 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20  ,. /*   120 */  
3b877 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b878 2c 20 20 36 35 38 2c 20 20 37 32 35 2c 20 20 39  ,  658,  725,  9
3b879 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b87a 20 39 35 34 2c 20 20 39 35 34 2c 0a 20 2f 2a 20   954,  954,. /* 
3b87b 20 20 31 33 30 20 2a 2f 20 20 20 39 35 34 2c 20    130 */   954, 
3b87c 20 39 35 34 2c 20 20 39 35 34 2c 20 20 38 33 32   954,  954,  832
3b87d 2c 20 20 38 33 33 2c 20 20 38 34 37 2c 20 20 38  ,  833,  847,  8
3b87e 34 36 2c 20 20 38 34 35 2c 20 20 39 35 34 2c 20  46,  845,  954, 
3b87f 20 36 36 33 2c 0a 20 2f 2a 20 20 20 31 34 30 20   663,. /*   140 
3b880 2a 2f 20 20 20 39 35 34 2c 20 20 39 35 34 2c 20  */   954,  954, 
3b881 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b882 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b883 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 0a  54,  954,  954,.
3b884 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 20 39   /*   150 */   9
3b885 33 34 2c 20 20 39 33 32 2c 20 20 39 35 34 2c 20  34,  932,  954, 
3b886 20 38 37 39 2c 20 20 39 35 34 2c 20 20 39 35 34   879,  954,  954
3b887 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b888 35 34 2c 20 20 39 35 34 2c 0a 20 2f 2a 20 20 20  54,  954,. /*   
3b889 31 36 30 20 2a 2f 20 20 20 39 35 34 2c 20 20 39  160 */   954,  9
3b88a 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b88b 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b88c 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b88d 35 34 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f  54,. /*   170 */
3b88e 20 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39     954,  954,  9
3b88f 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b890 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b891 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 0a 20 2f  ,  954,  954,. /
3b892 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 36 33 38  *   180 */   638
3b893 2c 20 20 37 35 36 2c 20 20 37 35 36 2c 20 20 37  ,  756,  756,  7
3b894 35 36 2c 20 20 36 33 32 2c 20 20 39 35 34 2c 20  56,  632,  954, 
3b895 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 34 36   954,  954,  946
3b896 2c 20 20 37 36 30 2c 0a 20 2f 2a 20 20 20 31 39  ,  760,. /*   19
3b897 30 20 2a 2f 20 20 20 37 35 30 2c 20 20 39 35 34  0 */   750,  954
3b898 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b899 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b89a 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b89b 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20  ,. /*   200 */  
3b89c 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b89d 2c 20 20 38 30 30 2c 20 20 37 33 39 2c 20 20 39  ,  800,  739,  9
3b89e 31 37 2c 20 20 39 31 39 2c 20 20 39 35 34 2c 20  17,  919,  954, 
3b89f 20 39 30 30 2c 20 20 37 33 37 2c 0a 20 2f 2a 20   900,  737,. /* 
3b8a0 20 20 32 31 30 20 2a 2f 20 20 20 36 36 30 2c 20    210 */   660, 
3b8a1 20 37 35 38 2c 20 20 36 37 33 2c 20 20 37 34 38   758,  673,  748
3b8a2 2c 20 20 36 34 30 2c 20 20 37 39 34 2c 20 20 37  ,  640,  794,  7
3b8a3 37 33 2c 20 20 37 37 33 2c 20 20 39 31 32 2c 20  73,  773,  912, 
3b8a4 20 37 39 34 2c 0a 20 2f 2a 20 20 20 32 32 30 20   794,. /*   220 
3b8a5 2a 2f 20 20 20 39 31 32 2c 20 20 36 39 36 2c 20  */   912,  696, 
3b8a6 20 37 31 39 2c 20 20 39 35 34 2c 20 20 37 38 34   719,  954,  784
3b8a7 2c 20 20 39 35 34 2c 20 20 37 38 34 2c 20 20 36  ,  954,  784,  6
3b8a8 39 33 2c 20 20 37 38 34 2c 20 20 37 37 33 2c 0a  93,  784,  773,.
3b8a9 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 20 38   /*   230 */   8
3b8aa 36 32 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  62,  954,  954, 
3b8ab 20 39 35 34 2c 20 20 37 35 37 2c 20 20 37 34 38   954,  757,  748
3b8ac 2c 20 20 39 35 34 2c 20 20 39 33 39 2c 20 20 37  ,  954,  939,  7
3b8ad 36 34 2c 20 20 37 36 34 2c 0a 20 2f 2a 20 20 20  64,  764,. /*   
3b8ae 32 34 30 20 2a 2f 20 20 20 39 33 31 2c 20 20 39  240 */   931,  9
3b8af 33 31 2c 20 20 37 36 34 2c 20 20 38 30 36 2c 20  31,  764,  806, 
3b8b0 20 37 32 39 2c 20 20 37 39 34 2c 20 20 37 33 36   729,  794,  736
3b8b1 2c 20 20 37 33 36 2c 20 20 37 33 36 2c 20 20 37  ,  736,  736,  7
3b8b2 33 36 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f  36,. /*   250 */
3b8b3 20 20 20 37 36 34 2c 20 20 36 35 35 2c 20 20 37     764,  655,  7
3b8b4 39 34 2c 20 20 38 30 36 2c 20 20 37 32 39 2c 20  94,  806,  729, 
3b8b5 20 37 32 39 2c 20 20 37 36 34 2c 20 20 36 35 35   729,  764,  655
3b8b6 2c 20 20 39 30 36 2c 20 20 39 30 34 2c 0a 20 2f  ,  906,  904,. /
3b8b7 2a 20 20 20 32 36 30 20 2a 2f 20 20 20 37 36 34  *   260 */   764
3b8b8 2c 20 20 37 36 34 2c 20 20 36 35 35 2c 20 20 37  ,  764,  655,  7
3b8b9 36 34 2c 20 20 36 35 35 2c 20 20 37 36 34 2c 20  64,  655,  764, 
3b8ba 20 36 35 35 2c 20 20 38 37 32 2c 20 20 37 32 37   655,  872,  727
3b8bb 2c 20 20 37 32 37 2c 0a 20 2f 2a 20 20 20 32 37  ,  727,. /*   27
3b8bc 30 20 2a 2f 20 20 20 37 32 37 2c 20 20 37 31 31  0 */   727,  711
3b8bd 2c 20 20 38 37 36 2c 20 20 38 37 36 2c 20 20 38  ,  876,  876,  8
3b8be 37 32 2c 20 20 37 32 37 2c 20 20 36 39 36 2c 20  72,  727,  696, 
3b8bf 20 37 32 37 2c 20 20 37 31 31 2c 20 20 37 32 37   727,  711,  727
3b8c0 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20  ,. /*   280 */  
3b8c1 20 37 32 37 2c 20 20 37 37 37 2c 20 20 37 37 32   727,  777,  772
3b8c2 2c 20 20 37 37 37 2c 20 20 37 37 32 2c 20 20 37  ,  777,  772,  7
3b8c3 37 37 2c 20 20 37 37 32 2c 20 20 37 36 34 2c 20  77,  772,  764, 
3b8c4 20 37 36 34 2c 20 20 39 35 34 2c 0a 20 2f 2a 20   764,  954,. /* 
3b8c5 20 20 32 39 30 20 2a 2f 20 20 20 37 38 39 2c 20    290 */   789, 
3b8c6 20 37 37 38 2c 20 20 37 38 37 2c 20 20 37 38 35   778,  787,  785
3b8c7 2c 20 20 37 39 34 2c 20 20 39 35 34 2c 20 20 37  ,  794,  954,  7
3b8c8 31 34 2c 20 20 36 34 38 2c 20 20 36 34 38 2c 20  14,  648,  648, 
3b8c9 20 36 33 37 2c 0a 20 2f 2a 20 20 20 33 30 30 20   637,. /*   300 
3b8ca 2a 2f 20 20 20 36 33 37 2c 20 20 36 33 37 2c 20  */   637,  637, 
3b8cb 20 36 33 37 2c 20 20 39 35 31 2c 20 20 39 35 31   637,  951,  951
3b8cc 2c 20 20 39 34 36 2c 20 20 36 39 38 2c 20 20 36  ,  946,  698,  6
3b8cd 39 38 2c 20 20 36 38 31 2c 20 20 39 35 34 2c 0a  98,  681,  954,.
3b8ce 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20 20 39   /*   310 */   9
3b8cf 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b8d0 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b8d1 2c 20 20 38 38 31 2c 20 20 39 35 34 2c 20 20 39  ,  881,  954,  9
3b8d2 35 34 2c 20 20 39 35 34 2c 0a 20 2f 2a 20 20 20  54,  954,. /*   
3b8d3 33 32 30 20 2a 2f 20 20 20 39 35 34 2c 20 20 39  320 */   954,  9
3b8d4 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b8d5 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b8d6 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 36  ,  954,  954,  6
3b8d7 33 33 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f  33,. /*   330 */
3b8d8 20 20 20 39 34 31 2c 20 20 39 35 34 2c 20 20 39     941,  954,  9
3b8d9 35 34 2c 20 20 39 33 38 2c 20 20 39 35 34 2c 20  54,  938,  954, 
3b8da 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b8db 2c 20 20 37 39 39 2c 20 20 39 35 34 2c 0a 20 2f  ,  799,  954,. /
3b8dc 2a 20 20 20 33 34 30 20 2a 2f 20 20 20 39 35 34  *   340 */   954
3b8dd 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b8de 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b8df 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b8e0 2c 20 20 39 31 36 2c 0a 20 2f 2a 20 20 20 33 35  ,  916,. /*   35
3b8e1 30 20 2a 2f 20 20 20 39 35 34 2c 20 20 39 35 34  0 */   954,  954
3b8e2 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b8e3 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b8e4 20 39 31 30 2c 20 20 39 35 34 2c 20 20 39 35 34   910,  954,  954
3b8e5 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20  ,. /*   360 */  
3b8e6 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b8e7 2c 20 20 39 35 34 2c 20 20 39 30 33 2c 20 20 39  ,  954,  903,  9
3b8e8 30 32 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  02,  954,  954, 
3b8e9 20 39 35 34 2c 20 20 39 35 34 2c 0a 20 2f 2a 20   954,  954,. /* 
3b8ea 20 20 33 37 30 20 2a 2f 20 20 20 39 35 34 2c 20    370 */   954, 
3b8eb 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b8ec 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b8ed 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b8ee 20 39 35 34 2c 0a 20 2f 2a 20 20 20 33 38 30 20   954,. /*   380 
3b8ef 2a 2f 20 20 20 39 35 34 2c 20 20 39 35 34 2c 20  */   954,  954, 
3b8f0 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b8f1 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39  ,  954,  954,  9
3b8f2 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 0a  54,  954,  954,.
3b8f3 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20 20 39   /*   390 */   9
3b8f4 35 34 2c 20 20 39 35 34 2c 20 20 37 38 36 2c 20  54,  954,  786, 
3b8f5 20 39 35 34 2c 20 20 37 37 39 2c 20 20 39 35 34   954,  779,  954
3b8f6 2c 20 20 38 36 35 2c 20 20 39 35 34 2c 20 20 39  ,  865,  954,  9
3b8f7 35 34 2c 20 20 39 35 34 2c 0a 20 2f 2a 20 20 20  54,  954,. /*   
3b8f8 34 30 30 20 2a 2f 20 20 20 39 35 34 2c 20 20 39  400 */   954,  9
3b8f9 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34 2c 20  54,  954,  954, 
3b8fa 20 39 35 34 2c 20 20 39 35 34 2c 20 20 39 35 34   954,  954,  954
3b8fb 2c 20 20 37 34 32 2c 20 20 38 31 35 2c 20 20 39  ,  742,  815,  9
3b8fc 35 34 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f  54,. /*   410 */
3b8fd 20 20 20 38 31 34 2c 20 20 38 31 38 2c 20 20 38     814,  818,  8
3b8fe 31 33 2c 20 20 36 36 35 2c 20 20 39 35 34 2c 20  13,  665,  954, 
3b8ff 20 36 34 36 2c 20 20 39 35 34 2c 20 20 36 32 39   646,  954,  629
3b900 2c 20 20 36 33 34 2c 20 20 39 35 30 2c 0a 20 2f  ,  634,  950,. /
3b901 2a 20 20 20 34 32 30 20 2a 2f 20 20 20 39 35 33  *   420 */   953
3b902 2c 20 20 39 35 32 2c 20 20 39 34 39 2c 20 20 39  ,  952,  949,  9
3b903 34 38 2c 20 20 39 34 37 2c 20 20 39 34 32 2c 20  48,  947,  942, 
3b904 20 39 34 30 2c 20 20 39 33 37 2c 20 20 39 33 36   940,  937,  936
3b905 2c 20 20 39 33 35 2c 0a 20 2f 2a 20 20 20 34 33  ,  935,. /*   43
3b906 30 20 2a 2f 20 20 20 39 33 33 2c 20 20 39 33 30  0 */   933,  930
3b907 2c 20 20 39 32 36 2c 20 20 38 38 35 2c 20 20 38  ,  926,  885,  8
3b908 38 33 2c 20 20 38 39 30 2c 20 20 38 38 39 2c 20  83,  890,  889, 
3b909 20 38 38 38 2c 20 20 38 38 37 2c 20 20 38 38 36   888,  887,  886
3b90a 2c 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f 20 20  ,. /*   440 */  
3b90b 20 38 38 34 2c 20 20 38 38 32 2c 20 20 38 38 30   884,  882,  880
3b90c 2c 20 20 38 30 31 2c 20 20 37 39 36 2c 20 20 37  ,  801,  796,  7
3b90d 39 33 2c 20 20 39 32 35 2c 20 20 38 37 38 2c 20  93,  925,  878, 
3b90e 20 37 33 38 2c 20 20 37 33 35 2c 0a 20 2f 2a 20   738,  735,. /* 
3b90f 20 20 34 35 30 20 2a 2f 20 20 20 37 33 34 2c 20    450 */   734, 
3b910 20 36 35 34 2c 20 20 39 34 33 2c 20 20 39 30 39   654,  943,  909
3b911 2c 20 20 39 31 38 2c 20 20 38 30 35 2c 20 20 38  ,  918,  805,  8
3b912 30 34 2c 20 20 38 30 37 2c 20 20 39 31 35 2c 20  04,  807,  915, 
3b913 20 39 31 34 2c 0a 20 2f 2a 20 20 20 34 36 30 20   914,. /*   460 
3b914 2a 2f 20 20 20 39 31 33 2c 20 20 39 31 31 2c 20  */   913,  911, 
3b915 20 39 30 38 2c 20 20 38 39 35 2c 20 20 38 30 33   908,  895,  803
3b916 2c 20 20 38 30 32 2c 20 20 37 33 30 2c 20 20 38  ,  802,  730,  8
3b917 37 30 2c 20 20 38 36 39 2c 20 20 36 35 37 2c 0a  70,  869,  657,.
3b918 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20 20 38   /*   470 */   8
3b919 39 39 2c 20 20 38 39 38 2c 20 20 38 39 37 2c 20  99,  898,  897, 
3b91a 20 39 30 31 2c 20 20 39 30 35 2c 20 20 38 39 36   901,  905,  896
3b91b 2c 20 20 37 36 36 2c 20 20 36 35 36 2c 20 20 36  ,  766,  656,  6
3b91c 35 33 2c 20 20 36 36 32 2c 0a 20 2f 2a 20 20 20  53,  662,. /*   
3b91d 34 38 30 20 2a 2f 20 20 20 37 31 37 2c 20 20 37  480 */   717,  7
3b91e 31 38 2c 20 20 37 32 36 2c 20 20 37 32 34 2c 20  18,  726,  724, 
3b91f 20 37 32 33 2c 20 20 37 32 32 2c 20 20 37 32 31   723,  722,  721
3b920 2c 20 20 37 32 30 2c 20 20 37 31 36 2c 20 20 36  ,  720,  716,  6
3b921 36 34 2c 0a 20 2f 2a 20 20 20 34 39 30 20 2a 2f  64,. /*   490 */
3b922 20 20 20 36 37 32 2c 20 20 37 31 30 2c 20 20 36     672,  710,  6
3b923 39 35 2c 20 20 36 39 34 2c 20 20 38 37 35 2c 20  95,  694,  875, 
3b924 20 38 37 37 2c 20 20 38 37 34 2c 20 20 38 37 33   877,  874,  873
3b925 2c 20 20 37 30 33 2c 20 20 37 30 32 2c 0a 20 2f  ,  703,  702,. /
3b926 2a 20 20 20 35 30 30 20 2a 2f 20 20 20 37 30 38  *   500 */   708
3b927 2c 20 20 37 30 37 2c 20 20 37 30 36 2c 20 20 37  ,  707,  706,  7
3b928 30 35 2c 20 20 37 30 34 2c 20 20 37 30 31 2c 20  05,  704,  701, 
3b929 20 37 30 30 2c 20 20 36 39 39 2c 20 20 36 39 32   700,  699,  692
3b92a 2c 20 20 36 39 31 2c 0a 20 2f 2a 20 20 20 35 31  ,  691,. /*   51
3b92b 30 20 2a 2f 20 20 20 36 39 37 2c 20 20 36 39 30  0 */   697,  690
3b92c 2c 20 20 37 31 33 2c 20 20 37 31 32 2c 20 20 37  ,  713,  712,  7
3b92d 30 39 2c 20 20 36 38 39 2c 20 20 37 33 33 2c 20  09,  689,  733, 
3b92e 20 37 33 32 2c 20 20 37 33 31 2c 20 20 37 32 38   732,  731,  728
3b92f 2c 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f 20 20  ,. /*   520 */  
3b930 20 36 38 38 2c 20 20 36 38 37 2c 20 20 36 38 36   688,  687,  686
3b931 2c 20 20 38 31 38 2c 20 20 36 38 35 2c 20 20 36  ,  818,  685,  6
3b932 38 34 2c 20 20 38 32 34 2c 20 20 38 32 33 2c 20  84,  824,  823, 
3b933 20 38 31 31 2c 20 20 38 35 34 2c 0a 20 2f 2a 20   811,  854,. /* 
3b934 20 20 35 33 30 20 2a 2f 20 20 20 37 35 33 2c 20    530 */   753, 
3b935 20 37 35 32 2c 20 20 37 35 31 2c 20 20 37 36 33   752,  751,  763
3b936 2c 20 20 37 36 32 2c 20 20 37 37 35 2c 20 20 37  ,  762,  775,  7
3b937 37 34 2c 20 20 38 30 39 2c 20 20 38 30 38 2c 20  74,  809,  808, 
3b938 20 37 37 36 2c 0a 20 2f 2a 20 20 20 35 34 30 20   776,. /*   540 
3b939 2a 2f 20 20 20 37 36 31 2c 20 20 37 35 35 2c 20  */   761,  755, 
3b93a 20 37 35 34 2c 20 20 37 37 30 2c 20 20 37 36 39   754,  770,  769
3b93b 2c 20 20 37 36 38 2c 20 20 37 36 37 2c 20 20 37  ,  768,  767,  7
3b93c 35 39 2c 20 20 37 34 39 2c 20 20 37 38 31 2c 0a  59,  749,  781,.
3b93d 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20 20 37   /*   550 */   7
3b93e 38 33 2c 20 20 37 38 32 2c 20 20 37 38 30 2c 20  83,  782,  780, 
3b93f 20 38 35 36 2c 20 20 37 36 35 2c 20 20 38 35 33   856,  765,  853
3b940 2c 20 20 39 32 34 2c 20 20 39 32 33 2c 20 20 39  ,  924,  923,  9
3b941 32 32 2c 20 20 39 32 31 2c 0a 20 2f 2a 20 20 20  22,  921,. /*   
3b942 35 36 30 20 2a 2f 20 20 20 39 32 30 2c 20 20 38  560 */   920,  8
3b943 35 38 2c 20 20 38 35 37 2c 20 20 38 32 35 2c 20  58,  857,  825, 
3b944 20 38 32 32 2c 20 20 36 37 36 2c 20 20 36 37 37   822,  676,  677
3b945 2c 20 20 38 39 33 2c 20 20 38 39 32 2c 20 20 38  ,  893,  892,  8
3b946 39 34 2c 0a 20 2f 2a 20 20 20 35 37 30 20 2a 2f  94,. /*   570 */
3b947 20 20 20 38 39 31 2c 20 20 36 37 39 2c 20 20 36     891,  679,  6
3b948 37 38 2c 20 20 36 37 35 2c 20 20 36 37 34 2c 20  78,  675,  674, 
3b949 20 38 35 35 2c 20 20 37 34 34 2c 20 20 37 34 33   855,  744,  743
3b94a 2c 20 20 38 35 31 2c 20 20 38 34 38 2c 0a 20 2f  ,  851,  848,. /
3b94b 2a 20 20 20 35 38 30 20 2a 2f 20 20 20 38 34 30  *   580 */   840
3b94c 2c 20 20 38 33 36 2c 20 20 38 35 32 2c 20 20 38  ,  836,  852,  8
3b94d 34 39 2c 20 20 38 34 31 2c 20 20 38 33 37 2c 20  49,  841,  837, 
3b94e 20 38 33 35 2c 20 20 38 33 34 2c 20 20 38 32 30   835,  834,  820
3b94f 2c 20 20 38 31 39 2c 0a 20 2f 2a 20 20 20 35 39  ,  819,. /*   59
3b950 30 20 2a 2f 20 20 20 38 31 37 2c 20 20 38 31 36  0 */   817,  816
3b951 2c 20 20 38 31 32 2c 20 20 38 32 31 2c 20 20 36  ,  812,  821,  6
3b952 36 37 2c 20 20 37 34 35 2c 20 20 37 34 31 2c 20  67,  745,  741, 
3b953 20 37 34 30 2c 20 20 38 31 30 2c 20 20 37 34 37   740,  810,  747
3b954 2c 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f 20 20  ,. /*   600 */  
3b955 20 37 34 36 2c 20 20 36 38 33 2c 20 20 36 38 32   746,  683,  682
3b956 2c 20 20 36 38 30 2c 20 20 36 36 31 2c 20 20 36  ,  680,  661,  6
3b957 35 39 2c 20 20 36 35 32 2c 20 20 36 35 30 2c 20  59,  652,  650, 
3b958 20 36 34 39 2c 20 20 36 35 31 2c 0a 20 2f 2a 20   649,  651,. /* 
3b959 20 20 36 31 30 20 2a 2f 20 20 20 36 34 37 2c 20    610 */   647, 
3b95a 20 36 34 35 2c 20 20 36 34 34 2c 20 20 36 34 33   645,  644,  643
3b95b 2c 20 20 36 34 32 2c 20 20 36 34 31 2c 20 20 36  ,  642,  641,  6
3b95c 37 30 2c 20 20 36 36 39 2c 20 20 36 36 38 2c 20  70,  669,  668, 
3b95d 20 36 36 36 2c 0a 20 2f 2a 20 20 20 36 32 30 20   666,. /*   620 
3b95e 2a 2f 20 20 20 36 36 35 2c 20 20 36 33 39 2c 20  */   665,  639, 
3b95f 20 36 33 36 2c 20 20 36 33 35 2c 20 20 36 33 31   636,  635,  631
3b960 2c 20 20 36 33 30 2c 20 20 36 32 38 2c 0a 7d 3b  ,  630,  628,.};
3b961 0a 0a 2f 2a 20 54 68 65 20 6e 65 78 74 20 74 61  ../* The next ta
3b962 62 6c 65 20 6d 61 70 73 20 74 6f 6b 65 6e 73 20  ble maps tokens 
3b963 69 6e 74 6f 20 66 61 6c 6c 62 61 63 6b 20 74 6f  into fallback to
3b964 6b 65 6e 73 2e 20 20 49 66 20 61 20 63 6f 6e 73  kens.  If a cons
3b965 74 72 75 63 74 0a 2a 2a 20 6c 69 6b 65 20 74 68  truct.** like th
3b966 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 20  e following:.** 
3b967 0a 2a 2a 20 20 20 20 20 20 25 66 61 6c 6c 62 61  .**      %fallba
3b968 63 6b 20 49 44 20 58 20 59 20 5a 2e 0a 2a 2a 0a  ck ID X Y Z..**.
3b969 2a 2a 20 61 70 70 65 61 72 73 20 69 6e 20 74 68  ** appears in th
3b96a 65 20 67 72 61 6d 6d 61 72 2c 20 74 68 65 6e 20  e grammar, then 
3b96b 49 44 20 62 65 63 6f 6d 65 73 20 61 20 66 61 6c  ID becomes a fal
3b96c 6c 62 61 63 6b 20 74 6f 6b 65 6e 20 66 6f 72 20  lback token for 
3b96d 58 2c 20 59 2c 0a 2a 2a 20 61 6e 64 20 5a 2e 20  X, Y,.** and Z. 
3b96e 20 57 68 65 6e 65 76 65 72 20 6f 6e 65 20 6f 66   Whenever one of
3b96f 20 74 68 65 20 74 6f 6b 65 6e 73 20 58 2c 20 59   the tokens X, Y
3b970 2c 20 6f 72 20 5a 20 69 73 20 69 6e 70 75 74 20  , or Z is input 
3b971 74 6f 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a  to the parser.**
3b972 20 62 75 74 20 69 74 20 64 6f 65 73 20 6e 6f 74   but it does not
3b973 20 70 61 72 73 65 2c 20 74 68 65 20 74 79 70 65   parse, the type
3b974 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 73   of the token is
3b975 20 63 68 61 6e 67 65 64 20 74 6f 20 49 44 20 61   changed to ID a
3b976 6e 64 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 20  nd.** the parse 
3b977 69 73 20 72 65 74 72 69 65 64 20 62 65 66 6f 72  is retried befor
3b978 65 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74 68  e an error is th
3b979 72 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  rown..*/.#ifdef 
3b97a 59 59 46 41 4c 4c 42 41 43 4b 0a 73 74 61 74 69  YYFALLBACK.stati
3b97b 63 20 63 6f 6e 73 74 20 59 59 43 4f 44 45 54 59  c const YYCODETY
3b97c 50 45 20 79 79 46 61 6c 6c 62 61 63 6b 5b 5d 20  PE yyFallback[] 
3b97d 3d 20 7b 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20  = {.    0,  /*  
3b97e 20 20 20 20 20 20 20 20 24 20 3d 3e 20 6e 6f 74          $ => not
3b97f 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
3b980 2f 2a 20 20 20 20 20 20 20 53 45 4d 49 20 3d 3e  /*       SEMI =>
3b981 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32   nothing */.   2
3b982 36 2c 20 20 2f 2a 20 20 20 20 45 58 50 4c 41 49  6,  /*    EXPLAI
3b983 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  N => ID */.   26
3b984 2c 20 20 2f 2a 20 20 20 20 20 20 51 55 45 52 59  ,  /*      QUERY
3b985 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
3b986 20 20 2f 2a 20 20 20 20 20 20 20 50 4c 41 4e 20    /*       PLAN 
3b987 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
3b988 20 2f 2a 20 20 20 20 20 20 42 45 47 49 4e 20 3d   /*      BEGIN =
3b989 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20  > ID */.    0,  
3b98a 2f 2a 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 3d  /* TRANSACTION =
3b98b 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
3b98c 32 36 2c 20 20 2f 2a 20 20 20 44 45 46 45 52 52  26,  /*   DEFERR
3b98d 45 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ED => ID */.   2
3b98e 36 2c 20 20 2f 2a 20 20 49 4d 4d 45 44 49 41 54  6,  /*  IMMEDIAT
3b98f 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  E => ID */.   26
3b990 2c 20 20 2f 2a 20 20 45 58 43 4c 55 53 49 56 45  ,  /*  EXCLUSIVE
3b991 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c   => ID */.    0,
3b992 20 20 2f 2a 20 20 20 20 20 43 4f 4d 4d 49 54 20    /*     COMMIT 
3b993 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
3b994 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   26,  /*        
3b995 45 4e 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  END => ID */.   
3b996 32 36 2c 20 20 2f 2a 20 20 20 52 4f 4c 4c 42 41  26,  /*   ROLLBA
3b997 43 4b 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  CK => ID */.   2
3b998 36 2c 20 20 2f 2a 20 20 53 41 56 45 50 4f 49 4e  6,  /*  SAVEPOIN
3b999 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  T => ID */.   26
3b99a 2c 20 20 2f 2a 20 20 20 20 52 45 4c 45 41 53 45  ,  /*    RELEASE
3b99b 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c   => ID */.    0,
3b99c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 54 4f 20    /*         TO 
3b99d 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
3b99e 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 54 41    0,  /*      TA
3b99f 42 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  BLE => nothing *
3b9a0 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
3b9a1 20 43 52 45 41 54 45 20 3d 3e 20 6e 6f 74 68 69   CREATE => nothi
3b9a2 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ng */.   26,  /*
3b9a3 20 20 20 20 20 20 20 20 20 49 46 20 3d 3e 20 49           IF => I
3b9a4 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  D */.    0,  /* 
3b9a5 20 20 20 20 20 20 20 4e 4f 54 20 3d 3e 20 6e 6f         NOT => no
3b9a6 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20  thing */.    0, 
3b9a7 20 2f 2a 20 20 20 20 20 45 58 49 53 54 53 20 3d   /*     EXISTS =
3b9a8 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
3b9a9 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 54 45  26,  /*       TE
3b9aa 4d 50 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20  MP => ID */.    
3b9ab 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4c  0,  /*         L
3b9ac 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  P => nothing */.
3b9ad 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
3b9ae 20 20 20 52 50 20 3d 3e 20 6e 6f 74 68 69 6e 67     RP => nothing
3b9af 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
3b9b0 20 20 20 20 20 20 20 41 53 20 3d 3e 20 6e 6f 74         AS => not
3b9b1 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
3b9b2 2f 2a 20 20 20 20 20 20 43 4f 4d 4d 41 20 3d 3e  /*      COMMA =>
3b9b3 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
3b9b4 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 49  0,  /*         I
3b9b5 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  D => nothing */.
3b9b6 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 49 4e      0,  /*    IN
3b9b7 44 45 58 45 44 20 3d 3e 20 6e 6f 74 68 69 6e 67  DEXED => nothing
3b9b8 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
3b9b9 20 20 20 20 41 42 4f 52 54 20 3d 3e 20 49 44 20      ABORT => ID 
3b9ba 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
3b9bb 20 20 41 43 54 49 4f 4e 20 3d 3e 20 49 44 20 2a    ACTION => ID *
3b9bc 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
3b9bd 20 20 41 46 54 45 52 20 3d 3e 20 49 44 20 2a 2f    AFTER => ID */
3b9be 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 41  .   26,  /*    A
3b9bf 4e 41 4c 59 5a 45 20 3d 3e 20 49 44 20 2a 2f 0a  NALYZE => ID */.
3b9c0 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
3b9c1 20 20 41 53 43 20 3d 3e 20 49 44 20 2a 2f 0a 20    ASC => ID */. 
3b9c2 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 41 54    26,  /*     AT
3b9c3 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  TACH => ID */.  
3b9c4 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 42 45 46   26,  /*     BEF
3b9c5 4f 52 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ORE => ID */.   
3b9c6 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20  26,  /*         
3b9c7 42 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  BY => ID */.   2
3b9c8 36 2c 20 20 2f 2a 20 20 20 20 43 41 53 43 41 44  6,  /*    CASCAD
3b9c9 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  E => ID */.   26
3b9ca 2c 20 20 2f 2a 20 20 20 20 20 20 20 43 41 53 54  ,  /*       CAST
3b9cb 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
3b9cc 20 20 2f 2a 20 20 20 43 4f 4c 55 4d 4e 4b 57 20    /*   COLUMNKW 
3b9cd 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
3b9ce 20 2f 2a 20 20 20 43 4f 4e 46 4c 49 43 54 20 3d   /*   CONFLICT =
3b9cf 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
3b9d0 2f 2a 20 20 20 44 41 54 41 42 41 53 45 20 3d 3e  /*   DATABASE =>
3b9d1 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
3b9d2 2a 20 20 20 20 20 20 20 44 45 53 43 20 3d 3e 20  *       DESC => 
3b9d3 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
3b9d4 20 20 20 20 20 44 45 54 41 43 48 20 3d 3e 20 49       DETACH => I
3b9d5 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
3b9d6 20 20 20 20 20 20 45 41 43 48 20 3d 3e 20 49 44        EACH => ID
3b9d7 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
3b9d8 20 20 20 20 20 46 41 49 4c 20 3d 3e 20 49 44 20       FAIL => ID 
3b9d9 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
3b9da 20 20 20 20 20 46 4f 52 20 3d 3e 20 49 44 20 2a       FOR => ID *
3b9db 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
3b9dc 20 49 47 4e 4f 52 45 20 3d 3e 20 49 44 20 2a 2f   IGNORE => ID */
3b9dd 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 49 4e 49  .   26,  /*  INI
3b9de 54 49 41 4c 4c 59 20 3d 3e 20 49 44 20 2a 2f 0a  TIALLY => ID */.
3b9df 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 49 4e     26,  /*    IN
3b9e0 53 54 45 41 44 20 3d 3e 20 49 44 20 2a 2f 0a 20  STEAD => ID */. 
3b9e1 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 4c 49 4b    26,  /*    LIK
3b9e2 45 5f 4b 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  E_KW => ID */.  
3b9e3 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 4d 41   26,  /*      MA
3b9e4 54 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  TCH => ID */.   
3b9e5 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20  26,  /*         
3b9e6 4e 4f 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  NO => ID */.   2
3b9e7 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 4b 45  6,  /*        KE
3b9e8 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  Y => ID */.   26
3b9e9 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4f 46  ,  /*         OF
3b9ea 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
3b9eb 20 20 2f 2a 20 20 20 20 20 4f 46 46 53 45 54 20    /*     OFFSET 
3b9ec 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
3b9ed 20 2f 2a 20 20 20 20 20 50 52 41 47 4d 41 20 3d   /*     PRAGMA =
3b9ee 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
3b9ef 2f 2a 20 20 20 20 20 20 52 41 49 53 45 20 3d 3e  /*      RAISE =>
3b9f0 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
3b9f1 2a 20 20 20 20 52 45 50 4c 41 43 45 20 3d 3e 20  *    REPLACE => 
3b9f2 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
3b9f3 20 20 20 52 45 53 54 52 49 43 54 20 3d 3e 20 49     RESTRICT => I
3b9f4 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
3b9f5 20 20 20 20 20 20 20 52 4f 57 20 3d 3e 20 49 44         ROW => ID
3b9f6 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
3b9f7 20 20 54 52 49 47 47 45 52 20 3d 3e 20 49 44 20    TRIGGER => ID 
3b9f8 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
3b9f9 20 20 56 41 43 55 55 4d 20 3d 3e 20 49 44 20 2a    VACUUM => ID *
3b9fa 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
3b9fb 20 20 20 56 49 45 57 20 3d 3e 20 49 44 20 2a 2f     VIEW => ID */
3b9fc 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 56  .   26,  /*    V
3b9fd 49 52 54 55 41 4c 20 3d 3e 20 49 44 20 2a 2f 0a  IRTUAL => ID */.
3b9fe 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 52 45     26,  /*    RE
3b9ff 49 4e 44 45 58 20 3d 3e 20 49 44 20 2a 2f 0a 20  INDEX => ID */. 
3ba00 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 52 45    26,  /*     RE
3ba01 4e 41 4d 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  NAME => ID */.  
3ba02 20 32 36 2c 20 20 2f 2a 20 20 20 43 54 49 4d 45   26,  /*   CTIME
3ba03 5f 4b 57 20 3d 3e 20 49 44 20 2a 2f 0a 7d 3b 0a  _KW => ID */.};.
3ba04 23 65 6e 64 69 66 20 2f 2a 20 59 59 46 41 4c 4c  #endif /* YYFALL
3ba05 42 41 43 4b 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20  BACK */../* The 
3ba06 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
3ba07 75 72 65 20 72 65 70 72 65 73 65 6e 74 73 20 61  ure represents a
3ba08 20 73 69 6e 67 6c 65 20 65 6c 65 6d 65 6e 74 20   single element 
3ba09 6f 66 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 72  of the.** parser
3ba0a 27 73 20 73 74 61 63 6b 2e 20 20 49 6e 66 6f 72  's stack.  Infor
3ba0b 6d 61 74 69 6f 6e 20 73 74 6f 72 65 64 20 69 6e  mation stored in
3ba0c 63 6c 75 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  cludes:.**.**   
3ba0d 2b 20 20 54 68 65 20 73 74 61 74 65 20 6e 75 6d  +  The state num
3ba0e 62 65 72 20 66 6f 72 20 74 68 65 20 70 61 72 73  ber for the pars
3ba0f 65 72 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c  er at this level
3ba10 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a   of the stack..*
3ba11 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20 76 61  *.**   +  The va
3ba12 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  lue of the token
3ba13 20 73 74 6f 72 65 64 20 61 74 20 74 68 69 73 20   stored at this 
3ba14 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 73 74 61  level of the sta
3ba15 63 6b 2e 0a 2a 2a 20 20 20 20 20 20 28 49 6e 20  ck..**      (In 
3ba16 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65  other words, the
3ba17 20 22 6d 61 6a 6f 72 22 20 74 6f 6b 65 6e 2e 29   "major" token.)
3ba18 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20  .**.**   +  The 
3ba19 73 65 6d 61 6e 74 69 63 20 76 61 6c 75 65 20 73  semantic value s
3ba1a 74 6f 72 65 64 20 61 74 20 74 68 69 73 20 6c 65  tored at this le
3ba1b 76 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b  vel of the stack
3ba1c 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 20 20  .  This is.**   
3ba1d 20 20 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69     the informati
3ba1e 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 61  on used by the a
3ba1f 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 69  ction routines i
3ba20 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a  n the grammar..*
3ba21 2a 20 20 20 20 20 20 49 74 20 69 73 20 73 6f 6d  *      It is som
3ba22 65 74 69 6d 65 73 20 63 61 6c 6c 65 64 20 74 68  etimes called th
3ba23 65 20 22 6d 69 6e 6f 72 22 20 74 6f 6b 65 6e 2e  e "minor" token.
3ba24 0a 2a 2f 0a 73 74 72 75 63 74 20 79 79 53 74 61  .*/.struct yySta
3ba25 63 6b 45 6e 74 72 79 20 7b 0a 20 20 59 59 41 43  ckEntry {.  YYAC
3ba26 54 49 4f 4e 54 59 50 45 20 73 74 61 74 65 6e 6f  TIONTYPE stateno
3ba27 3b 20 20 2f 2a 20 54 68 65 20 73 74 61 74 65 2d  ;  /* The state-
3ba28 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 59 59 43 4f  number */.  YYCO
3ba29 44 45 54 59 50 45 20 6d 61 6a 6f 72 3b 20 20 20  DETYPE major;   
3ba2a 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20     /* The major 
3ba2b 74 6f 6b 65 6e 20 76 61 6c 75 65 2e 20 20 54 68  token value.  Th
3ba2c 69 73 20 69 73 20 74 68 65 20 63 6f 64 65 0a 20  is is the code. 
3ba2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ba2e 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65          ** numbe
3ba2f 72 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e 20  r for the token 
3ba30 61 74 20 74 68 69 73 20 73 74 61 63 6b 20 6c 65  at this stack le
3ba31 76 65 6c 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52  vel */.  YYMINOR
3ba32 54 59 50 45 20 6d 69 6e 6f 72 3b 20 20 20 20 20  TYPE minor;     
3ba33 2f 2a 20 54 68 65 20 75 73 65 72 2d 73 75 70 70  /* The user-supp
3ba34 6c 69 65 64 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e  lied minor token
3ba35 20 76 61 6c 75 65 2e 20 20 54 68 69 73 0a 20 20   value.  This.  
3ba36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ba37 20 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65         ** is the
3ba38 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74 6f   value of the to
3ba39 6b 65 6e 20 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64  ken  */.};.typed
3ba3a 65 66 20 73 74 72 75 63 74 20 79 79 53 74 61 63  ef struct yyStac
3ba3b 6b 45 6e 74 72 79 20 79 79 53 74 61 63 6b 45 6e  kEntry yyStackEn
3ba3c 74 72 79 3b 0a 0a 2f 2a 20 54 68 65 20 73 74 61  try;../* The sta
3ba3d 74 65 20 6f 66 20 74 68 65 20 70 61 72 73 65 72  te of the parser
3ba3e 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 63   is completely c
3ba3f 6f 6e 74 61 69 6e 65 64 20 69 6e 20 61 6e 20 69  ontained in an i
3ba40 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68  nstance of.** th
3ba41 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
3ba42 63 74 75 72 65 20 2a 2f 0a 73 74 72 75 63 74 20  cture */.struct 
3ba43 79 79 50 61 72 73 65 72 20 7b 0a 20 20 69 6e 74  yyParser {.  int
3ba44 20 79 79 69 64 78 3b 20 20 20 20 20 20 20 20 20   yyidx;         
3ba45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
3ba46 64 65 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65  dex of top eleme
3ba47 6e 74 20 69 6e 20 73 74 61 63 6b 20 2a 2f 0a 23  nt in stack */.#
3ba48 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58  ifdef YYTRACKMAX
3ba49 53 54 41 43 4b 44 45 50 54 48 0a 20 20 69 6e 74  STACKDEPTH.  int
3ba4a 20 79 79 69 64 78 4d 61 78 3b 20 20 20 20 20 20   yyidxMax;      
3ba4b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
3ba4c 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 79  ximum value of y
3ba4d 79 69 64 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  yidx */.#endif. 
3ba4e 20 69 6e 74 20 79 79 65 72 72 63 6e 74 3b 20 20   int yyerrcnt;  
3ba4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3ba50 2a 20 53 68 69 66 74 73 20 6c 65 66 74 20 62 65  * Shifts left be
3ba51 66 6f 72 65 20 6f 75 74 20 6f 66 20 74 68 65 20  fore out of the 
3ba52 65 72 72 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74  error */.  sqlit
3ba53 65 33 50 61 72 73 65 72 41 52 47 5f 53 44 45 43  e3ParserARG_SDEC
3ba54 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
3ba55 20 2f 2a 20 41 20 70 6c 61 63 65 20 74 6f 20 68   /* A place to h
3ba56 6f 6c 64 20 25 65 78 74 72 61 5f 61 72 67 75 6d  old %extra_argum
3ba57 65 6e 74 20 2a 2f 0a 23 69 66 20 59 59 53 54 41  ent */.#if YYSTA
3ba58 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 69 6e 74  CKDEPTH<=0.  int
3ba59 20 79 79 73 74 6b 73 7a 3b 20 20 20 20 20 20 20   yystksz;       
3ba5a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
3ba5b 72 72 65 6e 74 20 73 69 64 65 20 6f 66 20 74 68  rrent side of th
3ba5c 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 79 79 53  e stack */.  yyS
3ba5d 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 73 74 61  tackEntry *yysta
3ba5e 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ck;        /* Th
3ba5f 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b  e parser's stack
3ba60 20 2a 2f 0a 23 65 6c 73 65 0a 20 20 79 79 53 74   */.#else.  yySt
3ba61 61 63 6b 45 6e 74 72 79 20 79 79 73 74 61 63 6b  ackEntry yystack
3ba62 5b 59 59 53 54 41 43 4b 44 45 50 54 48 5d 3b 20  [YYSTACKDEPTH]; 
3ba63 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 27 73   /* The parser's
3ba64 20 73 74 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66   stack */.#endif
3ba65 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  .};.typedef stru
3ba66 63 74 20 79 79 50 61 72 73 65 72 20 79 79 50 61  ct yyParser yyPa
3ba67 72 73 65 72 3b 0a 0a 23 69 66 6e 64 65 66 20 4e  rser;..#ifndef N
3ba68 44 45 42 55 47 0a 2f 2a 20 23 69 6e 63 6c 75 64  DEBUG./* #includ
3ba69 65 20 3c 73 74 64 69 6f 2e 68 3e 20 2a 2f 0a 73  e <stdio.h> */.s
3ba6a 74 61 74 69 63 20 46 49 4c 45 20 2a 79 79 54 72  tatic FILE *yyTr
3ba6b 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 73 74 61  aceFILE = 0;.sta
3ba6c 74 69 63 20 63 68 61 72 20 2a 79 79 54 72 61 63  tic char *yyTrac
3ba6d 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 23 65 6e  ePrompt = 0;.#en
3ba6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  dif /* NDEBUG */
3ba6f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
3ba70 0a 2f 2a 20 0a 2a 2a 20 54 75 72 6e 20 70 61 72  ./* .** Turn par
3ba71 73 65 72 20 74 72 61 63 69 6e 67 20 6f 6e 20 62  ser tracing on b
3ba72 79 20 67 69 76 69 6e 67 20 61 20 73 74 72 65 61  y giving a strea
3ba73 6d 20 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72  m to which to wr
3ba74 69 74 65 20 74 68 65 20 74 72 61 63 65 0a 2a 2a  ite the trace.**
3ba75 20 61 6e 64 20 61 20 70 72 6f 6d 70 74 20 74 6f   and a prompt to
3ba76 20 70 72 65 66 61 63 65 20 65 61 63 68 20 74 72   preface each tr
3ba77 61 63 65 20 6d 65 73 73 61 67 65 2e 20 20 54 72  ace message.  Tr
3ba78 61 63 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20  acing is turned 
3ba79 6f 66 66 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67  off.** by making
3ba7a 20 65 69 74 68 65 72 20 61 72 67 75 6d 65 6e 74   either argument
3ba7b 20 4e 55 4c 4c 20 0a 2a 2a 0a 2a 2a 20 49 6e 70   NULL .**.** Inp
3ba7c 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  uts:.** <ul>.** 
3ba7d 3c 6c 69 3e 20 41 20 46 49 4c 45 2a 20 74 6f 20  <li> A FILE* to 
3ba7e 77 68 69 63 68 20 74 72 61 63 65 20 6f 75 74 70  which trace outp
3ba7f 75 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69  ut should be wri
3ba80 74 74 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 49 66  tten..**      If
3ba81 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63   NULL, then trac
3ba82 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f 66  ing is turned of
3ba83 66 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 70 72 65  f..** <li> A pre
3ba84 66 69 78 20 73 74 72 69 6e 67 20 77 72 69 74 74  fix string writt
3ba85 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  en at the beginn
3ba86 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20  ing of every.** 
3ba87 20 20 20 20 20 6c 69 6e 65 20 6f 66 20 74 72 61       line of tra
3ba88 63 65 20 6f 75 74 70 75 74 2e 20 20 49 66 20 4e  ce output.  If N
3ba89 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63 69 6e  ULL, then tracin
3ba8a 67 20 69 73 0a 2a 2a 20 20 20 20 20 20 74 75 72  g is.**      tur
3ba8b 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c 2f 75 6c  ned off..** </ul
3ba8c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a  >.**.** Outputs:
3ba8d 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c  .** None..*/.SQL
3ba8e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
3ba8f 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 72   sqlite3ParserTr
3ba90 61 63 65 28 46 49 4c 45 20 2a 54 72 61 63 65 46  ace(FILE *TraceF
3ba91 49 4c 45 2c 20 63 68 61 72 20 2a 7a 54 72 61 63  ILE, char *zTrac
3ba92 65 50 72 6f 6d 70 74 29 7b 0a 20 20 79 79 54 72  ePrompt){.  yyTr
3ba93 61 63 65 46 49 4c 45 20 3d 20 54 72 61 63 65 46  aceFILE = TraceF
3ba94 49 4c 45 3b 0a 20 20 79 79 54 72 61 63 65 50 72  ILE;.  yyTracePr
3ba95 6f 6d 70 74 20 3d 20 7a 54 72 61 63 65 50 72 6f  ompt = zTracePro
3ba96 6d 70 74 3b 0a 20 20 69 66 28 20 79 79 54 72 61  mpt;.  if( yyTra
3ba97 63 65 46 49 4c 45 3d 3d 30 20 29 20 79 79 54 72  ceFILE==0 ) yyTr
3ba98 61 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 20  acePrompt = 0;. 
3ba99 20 65 6c 73 65 20 69 66 28 20 79 79 54 72 61 63   else if( yyTrac
3ba9a 65 50 72 6f 6d 70 74 3d 3d 30 20 29 20 79 79 54  ePrompt==0 ) yyT
3ba9b 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 7d 0a  raceFILE = 0;.}.
3ba9c 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
3ba9d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45   */..#ifndef NDE
3ba9e 42 55 47 0a 2f 2a 20 46 6f 72 20 74 72 61 63 69  BUG./* For traci
3ba9f 6e 67 20 73 68 69 66 74 73 2c 20 74 68 65 20 6e  ng shifts, the n
3baa0 61 6d 65 73 20 6f 66 20 61 6c 6c 20 74 65 72 6d  ames of all term
3baa1 69 6e 61 6c 73 20 61 6e 64 20 6e 6f 6e 74 65 72  inals and nonter
3baa2 6d 69 6e 61 6c 73 0a 2a 2a 20 61 72 65 20 72 65  minals.** are re
3baa3 71 75 69 72 65 64 2e 20 20 54 68 65 20 66 6f 6c  quired.  The fol
3baa4 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 73 75 70  lowing table sup
3baa5 70 6c 69 65 73 20 74 68 65 73 65 20 6e 61 6d 65  plies these name
3baa6 73 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  s */.static cons
3baa7 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 79 79  t char *const yy
3baa8 54 6f 6b 65 6e 4e 61 6d 65 5b 5d 20 3d 20 7b 20  TokenName[] = { 
3baa9 0a 20 20 22 24 22 2c 20 20 20 20 20 20 20 20 20  .  "$",         
3baaa 20 20 20 20 22 53 45 4d 49 22 2c 20 20 20 20 20      "SEMI",     
3baab 20 20 20 20 20 22 45 58 50 4c 41 49 4e 22 2c 20       "EXPLAIN", 
3baac 20 20 20 20 20 20 22 51 55 45 52 59 22 2c 20 20        "QUERY",  
3baad 20 20 20 20 20 0a 20 20 22 50 4c 41 4e 22 2c 20       .  "PLAN", 
3baae 20 20 20 20 20 20 20 20 20 22 42 45 47 49 4e 22           "BEGIN"
3baaf 2c 20 20 20 20 20 20 20 20 20 22 54 52 41 4e 53  ,         "TRANS
3bab0 41 43 54 49 4f 4e 22 2c 20 20 20 22 44 45 46 45  ACTION",   "DEFE
3bab1 52 52 45 44 22 2c 20 20 20 20 0a 20 20 22 49 4d  RRED",    .  "IM
3bab2 4d 45 44 49 41 54 45 22 2c 20 20 20 20 20 22 45  MEDIATE",     "E
3bab3 58 43 4c 55 53 49 56 45 22 2c 20 20 20 20 20 22  XCLUSIVE",     "
3bab4 43 4f 4d 4d 49 54 22 2c 20 20 20 20 20 20 20 20  COMMIT",        
3bab5 22 45 4e 44 22 2c 20 20 20 20 20 20 20 20 20 0a  "END",         .
3bab6 20 20 22 52 4f 4c 4c 42 41 43 4b 22 2c 20 20 20    "ROLLBACK",   
3bab7 20 20 20 22 53 41 56 45 50 4f 49 4e 54 22 2c 20     "SAVEPOINT", 
3bab8 20 20 20 20 22 52 45 4c 45 41 53 45 22 2c 20 20      "RELEASE",  
3bab9 20 20 20 20 20 22 54 4f 22 2c 20 20 20 20 20 20       "TO",      
3baba 20 20 20 20 0a 20 20 22 54 41 42 4c 45 22 2c 20      .  "TABLE", 
3babb 20 20 20 20 20 20 20 20 22 43 52 45 41 54 45 22          "CREATE"
3babc 2c 20 20 20 20 20 20 20 20 22 49 46 22 2c 20 20  ,        "IF",  
3babd 20 20 20 20 20 20 20 20 20 20 22 4e 4f 54 22 2c            "NOT",
3babe 20 20 20 20 20 20 20 20 20 0a 20 20 22 45 58 49           .  "EXI
3babf 53 54 53 22 2c 20 20 20 20 20 20 20 20 22 54 45  STS",        "TE
3bac0 4d 50 22 2c 20 20 20 20 20 20 20 20 20 20 22 4c  MP",          "L
3bac1 50 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  P",            "
3bac2 52 50 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20  RP",          . 
3bac3 20 22 41 53 22 2c 20 20 20 20 20 20 20 20 20 20   "AS",          
3bac4 20 20 22 43 4f 4d 4d 41 22 2c 20 20 20 20 20 20    "COMMA",      
3bac5 20 20 20 22 49 44 22 2c 20 20 20 20 20 20 20 20     "ID",        
3bac6 20 20 20 20 22 49 4e 44 45 58 45 44 22 2c 20 20      "INDEXED",  
3bac7 20 20 20 0a 20 20 22 41 42 4f 52 54 22 2c 20 20     .  "ABORT",  
3bac8 20 20 20 20 20 20 20 22 41 43 54 49 4f 4e 22 2c         "ACTION",
3bac9 20 20 20 20 20 20 20 20 22 41 46 54 45 52 22 2c          "AFTER",
3baca 20 20 20 20 20 20 20 20 20 22 41 4e 41 4c 59 5a           "ANALYZ
3bacb 45 22 2c 20 20 20 20 20 0a 20 20 22 41 53 43 22  E",     .  "ASC"
3bacc 2c 20 20 20 20 20 20 20 20 20 20 20 22 41 54 54  ,           "ATT
3bacd 41 43 48 22 2c 20 20 20 20 20 20 20 20 22 42 45  ACH",        "BE
3bace 46 4f 52 45 22 2c 20 20 20 20 20 20 20 20 22 42  FORE",        "B
3bacf 59 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20  Y",          .  
3bad0 22 43 41 53 43 41 44 45 22 2c 20 20 20 20 20 20  "CASCADE",      
3bad1 20 22 43 41 53 54 22 2c 20 20 20 20 20 20 20 20   "CAST",        
3bad2 20 20 22 43 4f 4c 55 4d 4e 4b 57 22 2c 20 20 20    "COLUMNKW",   
3bad3 20 20 20 22 43 4f 4e 46 4c 49 43 54 22 2c 20 20     "CONFLICT",  
3bad4 20 20 0a 20 20 22 44 41 54 41 42 41 53 45 22 2c    .  "DATABASE",
3bad5 20 20 20 20 20 20 22 44 45 53 43 22 2c 20 20 20        "DESC",   
3bad6 20 20 20 20 20 20 20 22 44 45 54 41 43 48 22 2c         "DETACH",
3bad7 20 20 20 20 20 20 20 20 22 45 41 43 48 22 2c 20          "EACH", 
3bad8 20 20 20 20 20 20 20 0a 20 20 22 46 41 49 4c 22         .  "FAIL"
3bad9 2c 20 20 20 20 20 20 20 20 20 20 22 46 4f 52 22  ,          "FOR"
3bada 2c 20 20 20 20 20 20 20 20 20 20 20 22 49 47 4e  ,           "IGN
3badb 4f 52 45 22 2c 20 20 20 20 20 20 20 20 22 49 4e  ORE",        "IN
3badc 49 54 49 41 4c 4c 59 22 2c 20 20 20 0a 20 20 22  ITIALLY",   .  "
3badd 49 4e 53 54 45 41 44 22 2c 20 20 20 20 20 20 20  INSTEAD",       
3bade 22 4c 49 4b 45 5f 4b 57 22 2c 20 20 20 20 20 20  "LIKE_KW",      
3badf 20 22 4d 41 54 43 48 22 2c 20 20 20 20 20 20 20   "MATCH",       
3bae0 20 20 22 4e 4f 22 2c 20 20 20 20 20 20 20 20 20    "NO",         
3bae1 20 0a 20 20 22 4b 45 59 22 2c 20 20 20 20 20 20   .  "KEY",      
3bae2 20 20 20 20 20 22 4f 46 22 2c 20 20 20 20 20 20       "OF",      
3bae3 20 20 20 20 20 20 22 4f 46 46 53 45 54 22 2c 20        "OFFSET", 
3bae4 20 20 20 20 20 20 20 22 50 52 41 47 4d 41 22 2c         "PRAGMA",
3bae5 20 20 20 20 20 20 0a 20 20 22 52 41 49 53 45 22        .  "RAISE"
3bae6 2c 20 20 20 20 20 20 20 20 20 22 52 45 50 4c 41  ,         "REPLA
3bae7 43 45 22 2c 20 20 20 20 20 20 20 22 52 45 53 54  CE",       "REST
3bae8 52 49 43 54 22 2c 20 20 20 20 20 20 22 52 4f 57  RICT",      "ROW
3bae9 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22 54  ",         .  "T
3baea 52 49 47 47 45 52 22 2c 20 20 20 20 20 20 20 22  RIGGER",       "
3baeb 56 41 43 55 55 4d 22 2c 20 20 20 20 20 20 20 20  VACUUM",        
3baec 22 56 49 45 57 22 2c 20 20 20 20 20 20 20 20 20  "VIEW",         
3baed 20 22 56 49 52 54 55 41 4c 22 2c 20 20 20 20 20   "VIRTUAL",     
3baee 0a 20 20 22 52 45 49 4e 44 45 58 22 2c 20 20 20  .  "REINDEX",   
3baef 20 20 20 20 22 52 45 4e 41 4d 45 22 2c 20 20 20      "RENAME",   
3baf0 20 20 20 20 20 22 43 54 49 4d 45 5f 4b 57 22 2c       "CTIME_KW",
3baf1 20 20 20 20 20 20 22 41 4e 59 22 2c 20 20 20 20        "ANY",    
3baf2 20 20 20 20 20 0a 20 20 22 4f 52 22 2c 20 20 20       .  "OR",   
3baf3 20 20 20 20 20 20 20 20 20 22 41 4e 44 22 2c 20           "AND", 
3baf4 20 20 20 20 20 20 20 20 20 20 22 49 53 22 2c 20            "IS", 
3baf5 20 20 20 20 20 20 20 20 20 20 20 22 42 45 54 57             "BETW
3baf6 45 45 4e 22 2c 20 20 20 20 20 0a 20 20 22 49 4e  EEN",     .  "IN
3baf7 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 49  ",            "I
3baf8 53 4e 55 4c 4c 22 2c 20 20 20 20 20 20 20 20 22  SNULL",        "
3baf9 4e 4f 54 4e 55 4c 4c 22 2c 20 20 20 20 20 20 20  NOTNULL",       
3bafa 22 4e 45 22 2c 20 20 20 20 20 20 20 20 20 20 0a  "NE",          .
3bafb 20 20 22 45 51 22 2c 20 20 20 20 20 20 20 20 20    "EQ",         
3bafc 20 20 20 22 47 54 22 2c 20 20 20 20 20 20 20 20     "GT",        
3bafd 20 20 20 20 22 4c 45 22 2c 20 20 20 20 20 20 20      "LE",       
3bafe 20 20 20 20 20 22 4c 54 22 2c 20 20 20 20 20 20       "LT",      
3baff 20 20 20 20 0a 20 20 22 47 45 22 2c 20 20 20 20      .  "GE",    
3bb00 20 20 20 20 20 20 20 20 22 45 53 43 41 50 45 22          "ESCAPE"
3bb01 2c 20 20 20 20 20 20 20 20 22 42 49 54 41 4e 44  ,        "BITAND
3bb02 22 2c 20 20 20 20 20 20 20 20 22 42 49 54 4f 52  ",        "BITOR
3bb03 22 2c 20 20 20 20 20 20 20 0a 20 20 22 4c 53 48  ",       .  "LSH
3bb04 49 46 54 22 2c 20 20 20 20 20 20 20 20 22 52 53  IFT",        "RS
3bb05 48 49 46 54 22 2c 20 20 20 20 20 20 20 20 22 50  HIFT",        "P
3bb06 4c 55 53 22 2c 20 20 20 20 20 20 20 20 20 20 22  LUS",          "
3bb07 4d 49 4e 55 53 22 2c 20 20 20 20 20 20 20 0a 20  MINUS",       . 
3bb08 20 22 53 54 41 52 22 2c 20 20 20 20 20 20 20 20   "STAR",        
3bb09 20 20 22 53 4c 41 53 48 22 2c 20 20 20 20 20 20    "SLASH",      
3bb0a 20 20 20 22 52 45 4d 22 2c 20 20 20 20 20 20 20     "REM",       
3bb0b 20 20 20 20 22 43 4f 4e 43 41 54 22 2c 20 20 20      "CONCAT",   
3bb0c 20 20 20 0a 20 20 22 43 4f 4c 4c 41 54 45 22 2c     .  "COLLATE",
3bb0d 20 20 20 20 20 20 20 22 42 49 54 4e 4f 54 22 2c         "BITNOT",
3bb0e 20 20 20 20 20 20 20 20 22 53 54 52 49 4e 47 22          "STRING"
3bb0f 2c 20 20 20 20 20 20 20 20 22 4a 4f 49 4e 5f 4b  ,        "JOIN_K
3bb10 57 22 2c 20 20 20 20 20 0a 20 20 22 43 4f 4e 53  W",     .  "CONS
3bb11 54 52 41 49 4e 54 22 2c 20 20 20 20 22 44 45 46  TRAINT",    "DEF
3bb12 41 55 4c 54 22 2c 20 20 20 20 20 20 20 22 4e 55  AULT",       "NU
3bb13 4c 4c 22 2c 20 20 20 20 20 20 20 20 20 20 22 50  LL",          "P
3bb14 52 49 4d 41 52 59 22 2c 20 20 20 20 20 0a 20 20  RIMARY",     .  
3bb15 22 55 4e 49 51 55 45 22 2c 20 20 20 20 20 20 20  "UNIQUE",       
3bb16 20 22 43 48 45 43 4b 22 2c 20 20 20 20 20 20 20   "CHECK",       
3bb17 20 20 22 52 45 46 45 52 45 4e 43 45 53 22 2c 20    "REFERENCES", 
3bb18 20 20 20 22 41 55 54 4f 49 4e 43 52 22 2c 20 20     "AUTOINCR",  
3bb19 20 20 0a 20 20 22 4f 4e 22 2c 20 20 20 20 20 20    .  "ON",      
3bb1a 20 20 20 20 20 20 22 49 4e 53 45 52 54 22 2c 20        "INSERT", 
3bb1b 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 22 2c         "DELETE",
3bb1c 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45 22          "UPDATE"
3bb1d 2c 20 20 20 20 20 20 0a 20 20 22 53 45 54 22 2c  ,      .  "SET",
3bb1e 20 20 20 20 20 20 20 20 20 20 20 22 44 45 46 45             "DEFE
3bb1f 52 52 41 42 4c 45 22 2c 20 20 20 20 22 46 4f 52  RRABLE",    "FOR
3bb20 45 49 47 4e 22 2c 20 20 20 20 20 20 20 22 44 52  EIGN",       "DR
3bb21 4f 50 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22  OP",        .  "
3bb22 55 4e 49 4f 4e 22 2c 20 20 20 20 20 20 20 20 20  UNION",         
3bb23 22 41 4c 4c 22 2c 20 20 20 20 20 20 20 20 20 20  "ALL",          
3bb24 20 22 45 58 43 45 50 54 22 2c 20 20 20 20 20 20   "EXCEPT",      
3bb25 20 20 22 49 4e 54 45 52 53 45 43 54 22 2c 20 20    "INTERSECT",  
3bb26 20 0a 20 20 22 53 45 4c 45 43 54 22 2c 20 20 20   .  "SELECT",   
3bb27 20 20 20 20 20 22 44 49 53 54 49 4e 43 54 22 2c       "DISTINCT",
3bb28 20 20 20 20 20 20 22 44 4f 54 22 2c 20 20 20 20        "DOT",    
3bb29 20 20 20 20 20 20 20 22 46 52 4f 4d 22 2c 20 20         "FROM",  
3bb2a 20 20 20 20 20 20 0a 20 20 22 4a 4f 49 4e 22 2c        .  "JOIN",
3bb2b 20 20 20 20 20 20 20 20 20 20 22 55 53 49 4e 47            "USING
3bb2c 22 2c 20 20 20 20 20 20 20 20 20 22 4f 52 44 45  ",         "ORDE
3bb2d 52 22 2c 20 20 20 20 20 20 20 20 20 22 47 52 4f  R",         "GRO
3bb2e 55 50 22 2c 20 20 20 20 20 20 20 0a 20 20 22 48  UP",       .  "H
3bb2f 41 56 49 4e 47 22 2c 20 20 20 20 20 20 20 20 22  AVING",        "
3bb30 4c 49 4d 49 54 22 2c 20 20 20 20 20 20 20 20 20  LIMIT",         
3bb31 22 57 48 45 52 45 22 2c 20 20 20 20 20 20 20 20  "WHERE",        
3bb32 20 22 49 4e 54 4f 22 2c 20 20 20 20 20 20 20 20   "INTO",        
3bb33 0a 20 20 22 56 41 4c 55 45 53 22 2c 20 20 20 20  .  "VALUES",    
3bb34 20 20 20 20 22 49 4e 54 45 47 45 52 22 2c 20 20      "INTEGER",  
3bb35 20 20 20 20 20 22 46 4c 4f 41 54 22 2c 20 20 20       "FLOAT",   
3bb36 20 20 20 20 20 20 22 42 4c 4f 42 22 2c 20 20 20        "BLOB",   
3bb37 20 20 20 20 20 0a 20 20 22 52 45 47 49 53 54 45       .  "REGISTE
3bb38 52 22 2c 20 20 20 20 20 20 22 56 41 52 49 41 42  R",      "VARIAB
3bb39 4c 45 22 2c 20 20 20 20 20 20 22 43 41 53 45 22  LE",      "CASE"
3bb3a 2c 20 20 20 20 20 20 20 20 20 20 22 57 48 45 4e  ,          "WHEN
3bb3b 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 54 48  ",        .  "TH
3bb3c 45 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22 45  EN",          "E
3bb3d 4c 53 45 22 2c 20 20 20 20 20 20 20 20 20 20 22  LSE",          "
3bb3e 49 4e 44 45 58 22 2c 20 20 20 20 20 20 20 20 20  INDEX",         
3bb3f 22 41 4c 54 45 52 22 2c 20 20 20 20 20 20 20 0a  "ALTER",       .
3bb40 20 20 22 41 44 44 22 2c 20 20 20 20 20 20 20 20    "ADD",        
3bb41 20 20 20 22 65 72 72 6f 72 22 2c 20 20 20 20 20     "error",     
3bb42 20 20 20 20 22 69 6e 70 75 74 22 2c 20 20 20 20      "input",    
3bb43 20 20 20 20 20 22 63 6d 64 6c 69 73 74 22 2c 20       "cmdlist", 
3bb44 20 20 20 20 0a 20 20 22 65 63 6d 64 22 2c 20 20      .  "ecmd",  
3bb45 20 20 20 20 20 20 20 20 22 65 78 70 6c 61 69 6e          "explain
3bb46 22 2c 20 20 20 20 20 20 20 22 63 6d 64 78 22 2c  ",       "cmdx",
3bb47 20 20 20 20 20 20 20 20 20 20 22 63 6d 64 22 2c            "cmd",
3bb48 20 20 20 20 20 20 20 20 20 0a 20 20 22 74 72 61           .  "tra
3bb49 6e 73 74 79 70 65 22 2c 20 20 20 20 20 22 74 72  nstype",     "tr
3bb4a 61 6e 73 5f 6f 70 74 22 2c 20 20 20 20 20 22 6e  ans_opt",     "n
3bb4b 6d 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  m",            "
3bb4c 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 22 2c 0a  savepoint_opt",.
3bb4d 20 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65 22    "create_table"
3bb4e 2c 20 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65  ,  "create_table
3bb4f 5f 61 72 67 73 22 2c 20 20 22 63 72 65 61 74 65  _args",  "create
3bb50 6b 77 22 2c 20 20 20 20 20 20 22 74 65 6d 70 22  kw",      "temp"
3bb51 2c 20 20 20 20 20 20 20 20 0a 20 20 22 69 66 6e  ,        .  "ifn
3bb52 6f 74 65 78 69 73 74 73 22 2c 20 20 20 22 64 62  otexists",   "db
3bb53 6e 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22 63  nm",          "c
3bb54 6f 6c 75 6d 6e 6c 69 73 74 22 2c 20 20 20 20 22  olumnlist",    "
3bb55 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 22 2c 0a 20  conslist_opt",. 
3bb56 20 22 73 65 6c 65 63 74 22 2c 20 20 20 20 20 20   "select",      
3bb57 20 20 22 63 6f 6c 75 6d 6e 22 2c 20 20 20 20 20    "column",     
3bb58 20 20 20 22 63 6f 6c 75 6d 6e 69 64 22 2c 20 20     "columnid",  
3bb59 20 20 20 20 22 74 79 70 65 22 2c 20 20 20 20 20      "type",     
3bb5a 20 20 20 0a 20 20 22 63 61 72 67 6c 69 73 74 22     .  "carglist"
3bb5b 2c 20 20 20 20 20 20 22 69 64 22 2c 20 20 20 20  ,      "id",    
3bb5c 20 20 20 20 20 20 20 20 22 69 64 73 22 2c 20 20          "ids",  
3bb5d 20 20 20 20 20 20 20 20 20 22 74 79 70 65 74 6f           "typeto
3bb5e 6b 65 6e 22 2c 20 20 20 0a 20 20 22 74 79 70 65  ken",   .  "type
3bb5f 6e 61 6d 65 22 2c 20 20 20 20 20 20 22 73 69 67  name",      "sig
3bb60 6e 65 64 22 2c 20 20 20 20 20 20 20 20 22 70 6c  ned",        "pl
3bb61 75 73 5f 6e 75 6d 22 2c 20 20 20 20 20 20 22 6d  us_num",      "m
3bb62 69 6e 75 73 5f 6e 75 6d 22 2c 20 20 20 0a 20 20  inus_num",   .  
3bb63 22 63 63 6f 6e 73 22 2c 20 20 20 20 20 20 20 20  "ccons",        
3bb64 20 22 74 65 72 6d 22 2c 20 20 20 20 20 20 20 20   "term",        
3bb65 20 20 22 65 78 70 72 22 2c 20 20 20 20 20 20 20    "expr",       
3bb66 20 20 20 22 6f 6e 63 6f 6e 66 22 2c 20 20 20 20     "onconf",    
3bb67 20 20 0a 20 20 22 73 6f 72 74 6f 72 64 65 72 22    .  "sortorder"
3bb68 2c 20 20 20 20 20 22 61 75 74 6f 69 6e 63 22 2c  ,     "autoinc",
3bb69 20 20 20 20 20 20 20 22 69 64 78 6c 69 73 74 5f         "idxlist_
3bb6a 6f 70 74 22 2c 20 20 20 22 72 65 66 61 72 67 73  opt",   "refargs
3bb6b 22 2c 20 20 20 20 20 0a 20 20 22 64 65 66 65 72  ",     .  "defer
3bb6c 5f 73 75 62 63 6c 61 75 73 65 22 2c 20 20 22 72  _subclause",  "r
3bb6d 65 66 61 72 67 22 2c 20 20 20 20 20 20 20 20 22  efarg",        "
3bb6e 72 65 66 61 63 74 22 2c 20 20 20 20 20 20 20 20  refact",        
3bb6f 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70  "init_deferred_p
3bb70 72 65 64 5f 6f 70 74 22 2c 0a 20 20 22 63 6f 6e  red_opt",.  "con
3bb71 73 6c 69 73 74 22 2c 20 20 20 20 20 20 22 74 63  slist",      "tc
3bb72 6f 6e 73 63 6f 6d 6d 61 22 2c 20 20 20 20 22 74  onscomma",    "t
3bb73 63 6f 6e 73 22 2c 20 20 20 20 20 20 20 20 20 22  cons",         "
3bb74 69 64 78 6c 69 73 74 22 2c 20 20 20 20 20 0a 20  idxlist",     . 
3bb75 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73   "defer_subclaus
3bb76 65 5f 6f 70 74 22 2c 20 20 22 6f 72 63 6f 6e 66  e_opt",  "orconf
3bb77 22 2c 20 20 20 20 20 20 20 20 22 72 65 73 6f 6c  ",        "resol
3bb78 76 65 74 79 70 65 22 2c 20 20 20 22 72 61 69 73  vetype",   "rais
3bb79 65 74 79 70 65 22 2c 20 20 20 0a 20 20 22 69 66  etype",   .  "if
3bb7a 65 78 69 73 74 73 22 2c 20 20 20 20 20 20 22 66  exists",      "f
3bb7b 75 6c 6c 6e 61 6d 65 22 2c 20 20 20 20 20 20 22  ullname",      "
3bb7c 6f 6e 65 73 65 6c 65 63 74 22 2c 20 20 20 20 20  oneselect",     
3bb7d 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 22  "multiselect_op"
3bb7e 2c 0a 20 20 22 64 69 73 74 69 6e 63 74 22 2c 20  ,.  "distinct", 
3bb7f 20 20 20 20 20 22 73 65 6c 63 6f 6c 6c 69 73 74       "selcollist
3bb80 22 2c 20 20 20 20 22 66 72 6f 6d 22 2c 20 20 20  ",    "from",   
3bb81 20 20 20 20 20 20 20 22 77 68 65 72 65 5f 6f 70         "where_op
3bb82 74 22 2c 20 20 20 0a 20 20 22 67 72 6f 75 70 62  t",   .  "groupb
3bb83 79 5f 6f 70 74 22 2c 20 20 20 22 68 61 76 69 6e  y_opt",   "havin
3bb84 67 5f 6f 70 74 22 2c 20 20 20 20 22 6f 72 64 65  g_opt",    "orde
3bb85 72 62 79 5f 6f 70 74 22 2c 20 20 20 22 6c 69 6d  rby_opt",   "lim
3bb86 69 74 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22 73  it_opt",   .  "s
3bb87 63 6c 70 22 2c 20 20 20 20 20 20 20 20 20 20 22  clp",          "
3bb88 61 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  as",            
3bb89 22 73 65 6c 74 61 62 6c 69 73 74 22 2c 20 20 20  "seltablist",   
3bb8a 20 22 73 74 6c 5f 70 72 65 66 69 78 22 2c 20 20   "stl_prefix",  
3bb8b 0a 20 20 22 6a 6f 69 6e 6f 70 22 2c 20 20 20 20  .  "joinop",    
3bb8c 20 20 20 20 22 69 6e 64 65 78 65 64 5f 6f 70 74      "indexed_opt
3bb8d 22 2c 20 20 20 22 6f 6e 5f 6f 70 74 22 2c 20 20  ",   "on_opt",  
3bb8e 20 20 20 20 20 20 22 75 73 69 6e 67 5f 6f 70 74        "using_opt
3bb8f 22 2c 20 20 20 0a 20 20 22 6a 6f 69 6e 6f 70 32  ",   .  "joinop2
3bb90 22 2c 20 20 20 20 20 20 20 22 69 6e 73 63 6f 6c  ",       "inscol
3bb91 6c 69 73 74 22 2c 20 20 20 20 22 73 6f 72 74 6c  list",    "sortl
3bb92 69 73 74 22 2c 20 20 20 20 20 20 22 6e 65 78 70  ist",      "nexp
3bb93 72 6c 69 73 74 22 2c 20 20 20 0a 20 20 22 73 65  rlist",   .  "se
3bb94 74 6c 69 73 74 22 2c 20 20 20 20 20 20 20 22 69  tlist",       "i
3bb95 6e 73 65 72 74 5f 63 6d 64 22 2c 20 20 20 20 22  nsert_cmd",    "
3bb96 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 22 2c  inscollist_opt",
3bb97 20 20 22 76 61 6c 75 65 6c 69 73 74 22 2c 20 20    "valuelist",  
3bb98 20 0a 20 20 22 65 78 70 72 6c 69 73 74 22 2c 20   .  "exprlist", 
3bb99 20 20 20 20 20 22 6c 69 6b 65 6f 70 22 2c 20 20       "likeop",  
3bb9a 20 20 20 20 20 20 22 62 65 74 77 65 65 6e 5f 6f        "between_o
3bb9b 70 22 2c 20 20 20 20 22 69 6e 5f 6f 70 22 2c 20  p",    "in_op", 
3bb9c 20 20 20 20 20 20 0a 20 20 22 63 61 73 65 5f 6f        .  "case_o
3bb9d 70 65 72 61 6e 64 22 2c 20 20 22 63 61 73 65 5f  perand",  "case_
3bb9e 65 78 70 72 6c 69 73 74 22 2c 20 20 22 63 61 73  exprlist",  "cas
3bb9f 65 5f 65 6c 73 65 22 2c 20 20 20 20 20 22 75 6e  e_else",     "un
3bba0 69 71 75 65 66 6c 61 67 22 2c 20 20 0a 20 20 22  iqueflag",  .  "
3bba1 63 6f 6c 6c 61 74 65 22 2c 20 20 20 20 20 20 20  collate",       
3bba2 22 6e 6d 6e 75 6d 22 2c 20 20 20 20 20 20 20 20  "nmnum",        
3bba3 20 22 6e 75 6d 62 65 72 22 2c 20 20 20 20 20 20   "number",      
3bba4 20 20 22 74 72 69 67 67 65 72 5f 64 65 63 6c 22    "trigger_decl"
3bba5 2c 0a 20 20 22 74 72 69 67 67 65 72 5f 63 6d 64  ,.  "trigger_cmd
3bba6 5f 6c 69 73 74 22 2c 20 20 22 74 72 69 67 67 65  _list",  "trigge
3bba7 72 5f 74 69 6d 65 22 2c 20 20 22 74 72 69 67 67  r_time",  "trigg
3bba8 65 72 5f 65 76 65 6e 74 22 2c 20 20 22 66 6f 72  er_event",  "for
3bba9 65 61 63 68 5f 63 6c 61 75 73 65 22 2c 0a 20 20  each_clause",.  
3bbaa 22 77 68 65 6e 5f 63 6c 61 75 73 65 22 2c 20 20  "when_clause",  
3bbab 20 22 74 72 69 67 67 65 72 5f 63 6d 64 22 2c 20   "trigger_cmd", 
3bbac 20 20 22 74 72 6e 6d 22 2c 20 20 20 20 20 20 20    "trnm",       
3bbad 20 20 20 22 74 72 69 64 78 62 79 22 2c 20 20 20     "tridxby",   
3bbae 20 20 0a 20 20 22 64 61 74 61 62 61 73 65 5f 6b    .  "database_k
3bbaf 77 5f 6f 70 74 22 2c 20 20 22 6b 65 79 5f 6f 70  w_opt",  "key_op
3bbb0 74 22 2c 20 20 20 20 20 20 20 22 61 64 64 5f 63  t",       "add_c
3bbb1 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 22 2c  olumn_fullname",
3bbb2 20 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 22    "kwcolumn_opt"
3bbb3 2c 0a 20 20 22 63 72 65 61 74 65 5f 76 74 61 62  ,.  "create_vtab
3bbb4 22 2c 20 20 20 22 76 74 61 62 61 72 67 6c 69 73  ",   "vtabarglis
3bbb5 74 22 2c 20 20 20 22 76 74 61 62 61 72 67 22 2c  t",   "vtabarg",
3bbb6 20 20 20 20 20 20 20 22 76 74 61 62 61 72 67 74         "vtabargt
3bbb7 6f 6b 65 6e 22 2c 0a 20 20 22 6c 70 22 2c 20 20  oken",.  "lp",  
3bbb8 20 20 20 20 20 20 20 20 20 20 22 61 6e 79 6c 69            "anyli
3bbb9 73 74 22 2c 20 20 20 20 20 0a 7d 3b 0a 23 65 6e  st",     .};.#en
3bbba 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  dif /* NDEBUG */
3bbbb 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
3bbbc 0a 2f 2a 20 46 6f 72 20 74 72 61 63 69 6e 67 20  ./* For tracing 
3bbbd 72 65 64 75 63 65 20 61 63 74 69 6f 6e 73 2c 20  reduce actions, 
3bbbe 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c  the names of all
3bbbf 20 72 75 6c 65 73 20 61 72 65 20 72 65 71 75 69   rules are requi
3bbc0 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  red..*/.static c
3bbc1 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
3bbc2 20 79 79 52 75 6c 65 4e 61 6d 65 5b 5d 20 3d 20   yyRuleName[] = 
3bbc3 7b 0a 20 2f 2a 20 20 20 30 20 2a 2f 20 22 69 6e  {. /*   0 */ "in
3bbc4 70 75 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 22  put ::= cmdlist"
3bbc5 2c 0a 20 2f 2a 20 20 20 31 20 2a 2f 20 22 63 6d  ,. /*   1 */ "cm
3bbc6 64 6c 69 73 74 20 3a 3a 3d 20 63 6d 64 6c 69 73  dlist ::= cmdlis
3bbc7 74 20 65 63 6d 64 22 2c 0a 20 2f 2a 20 20 20 32  t ecmd",. /*   2
3bbc8 20 2a 2f 20 22 63 6d 64 6c 69 73 74 20 3a 3a 3d   */ "cmdlist ::=
3bbc9 20 65 63 6d 64 22 2c 0a 20 2f 2a 20 20 20 33 20   ecmd",. /*   3 
3bbca 2a 2f 20 22 65 63 6d 64 20 3a 3a 3d 20 53 45 4d  */ "ecmd ::= SEM
3bbcb 49 22 2c 0a 20 2f 2a 20 20 20 34 20 2a 2f 20 22  I",. /*   4 */ "
3bbcc 65 63 6d 64 20 3a 3a 3d 20 65 78 70 6c 61 69 6e  ecmd ::= explain
3bbcd 20 63 6d 64 78 20 53 45 4d 49 22 2c 0a 20 2f 2a   cmdx SEMI",. /*
3bbce 20 20 20 35 20 2a 2f 20 22 65 78 70 6c 61 69 6e     5 */ "explain
3bbcf 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 20 36 20 2a   ::=",. /*   6 *
3bbd0 2f 20 22 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45  / "explain ::= E
3bbd1 58 50 4c 41 49 4e 22 2c 0a 20 2f 2a 20 20 20 37  XPLAIN",. /*   7
3bbd2 20 2a 2f 20 22 65 78 70 6c 61 69 6e 20 3a 3a 3d   */ "explain ::=
3bbd3 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50   EXPLAIN QUERY P
3bbd4 4c 41 4e 22 2c 0a 20 2f 2a 20 20 20 38 20 2a 2f  LAN",. /*   8 */
3bbd5 20 22 63 6d 64 78 20 3a 3a 3d 20 63 6d 64 22 2c   "cmdx ::= cmd",
3bbd6 0a 20 2f 2a 20 20 20 39 20 2a 2f 20 22 63 6d 64  . /*   9 */ "cmd
3bbd7 20 3a 3a 3d 20 42 45 47 49 4e 20 74 72 61 6e 73   ::= BEGIN trans
3bbd8 74 79 70 65 20 74 72 61 6e 73 5f 6f 70 74 22 2c  type trans_opt",
3bbd9 0a 20 2f 2a 20 20 31 30 20 2a 2f 20 22 74 72 61  . /*  10 */ "tra
3bbda 6e 73 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  ns_opt ::=",. /*
3bbdb 20 20 31 31 20 2a 2f 20 22 74 72 61 6e 73 5f 6f    11 */ "trans_o
3bbdc 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54 49  pt ::= TRANSACTI
3bbdd 4f 4e 22 2c 0a 20 2f 2a 20 20 31 32 20 2a 2f 20  ON",. /*  12 */ 
3bbde 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54  "trans_opt ::= T
3bbdf 52 41 4e 53 41 43 54 49 4f 4e 20 6e 6d 22 2c 0a  RANSACTION nm",.
3bbe0 20 2f 2a 20 20 31 33 20 2a 2f 20 22 74 72 61 6e   /*  13 */ "tran
3bbe1 73 74 79 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  stype ::=",. /* 
3bbe2 20 31 34 20 2a 2f 20 22 74 72 61 6e 73 74 79 70   14 */ "transtyp
3bbe3 65 20 3a 3a 3d 20 44 45 46 45 52 52 45 44 22 2c  e ::= DEFERRED",
3bbe4 0a 20 2f 2a 20 20 31 35 20 2a 2f 20 22 74 72 61  . /*  15 */ "tra
3bbe5 6e 73 74 79 70 65 20 3a 3a 3d 20 49 4d 4d 45 44  nstype ::= IMMED
3bbe6 49 41 54 45 22 2c 0a 20 2f 2a 20 20 31 36 20 2a  IATE",. /*  16 *
3bbe7 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d  / "transtype ::=
3bbe8 20 45 58 43 4c 55 53 49 56 45 22 2c 0a 20 2f 2a   EXCLUSIVE",. /*
3bbe9 20 20 31 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d    17 */ "cmd ::=
3bbea 20 43 4f 4d 4d 49 54 20 74 72 61 6e 73 5f 6f 70   COMMIT trans_op
3bbeb 74 22 2c 0a 20 2f 2a 20 20 31 38 20 2a 2f 20 22  t",. /*  18 */ "
3bbec 63 6d 64 20 3a 3a 3d 20 45 4e 44 20 74 72 61 6e  cmd ::= END tran
3bbed 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31 39 20  s_opt",. /*  19 
3bbee 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c  */ "cmd ::= ROLL
3bbef 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 22 2c  BACK trans_opt",
3bbf0 0a 20 2f 2a 20 20 32 30 20 2a 2f 20 22 73 61 76  . /*  20 */ "sav
3bbf1 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d 20 53  epoint_opt ::= S
3bbf2 41 56 45 50 4f 49 4e 54 22 2c 0a 20 2f 2a 20 20  AVEPOINT",. /*  
3bbf3 32 31 20 2a 2f 20 22 73 61 76 65 70 6f 69 6e 74  21 */ "savepoint
3bbf4 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  _opt ::=",. /*  
3bbf5 32 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 53  22 */ "cmd ::= S
3bbf6 41 56 45 50 4f 49 4e 54 20 6e 6d 22 2c 0a 20 2f  AVEPOINT nm",. /
3bbf7 2a 20 20 32 33 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  23 */ "cmd ::
3bbf8 3d 20 52 45 4c 45 41 53 45 20 73 61 76 65 70 6f  = RELEASE savepo
3bbf9 69 6e 74 5f 6f 70 74 20 6e 6d 22 2c 0a 20 2f 2a  int_opt nm",. /*
3bbfa 20 20 32 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d    24 */ "cmd ::=
3bbfb 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f   ROLLBACK trans_
3bbfc 6f 70 74 20 54 4f 20 73 61 76 65 70 6f 69 6e 74  opt TO savepoint
3bbfd 5f 6f 70 74 20 6e 6d 22 2c 0a 20 2f 2a 20 20 32  _opt nm",. /*  2
3bbfe 35 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72  5 */ "cmd ::= cr
3bbff 65 61 74 65 5f 74 61 62 6c 65 20 63 72 65 61 74  eate_table creat
3bc00 65 5f 74 61 62 6c 65 5f 61 72 67 73 22 2c 0a 20  e_table_args",. 
3bc01 2f 2a 20 20 32 36 20 2a 2f 20 22 63 72 65 61 74  /*  26 */ "creat
3bc02 65 5f 74 61 62 6c 65 20 3a 3a 3d 20 63 72 65 61  e_table ::= crea
3bc03 74 65 6b 77 20 74 65 6d 70 20 54 41 42 4c 45 20  tekw temp TABLE 
3bc04 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64  ifnotexists nm d
3bc05 62 6e 6d 22 2c 0a 20 2f 2a 20 20 32 37 20 2a 2f  bnm",. /*  27 */
3bc06 20 22 63 72 65 61 74 65 6b 77 20 3a 3a 3d 20 43   "createkw ::= C
3bc07 52 45 41 54 45 22 2c 0a 20 2f 2a 20 20 32 38 20  REATE",. /*  28 
3bc08 2a 2f 20 22 69 66 6e 6f 74 65 78 69 73 74 73 20  */ "ifnotexists 
3bc09 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 32 39 20 2a 2f  ::=",. /*  29 */
3bc0a 20 22 69 66 6e 6f 74 65 78 69 73 74 73 20 3a 3a   "ifnotexists ::
3bc0b 3d 20 49 46 20 4e 4f 54 20 45 58 49 53 54 53 22  = IF NOT EXISTS"
3bc0c 2c 0a 20 2f 2a 20 20 33 30 20 2a 2f 20 22 74 65  ,. /*  30 */ "te
3bc0d 6d 70 20 3a 3a 3d 20 54 45 4d 50 22 2c 0a 20 2f  mp ::= TEMP",. /
3bc0e 2a 20 20 33 31 20 2a 2f 20 22 74 65 6d 70 20 3a  *  31 */ "temp :
3bc0f 3a 3d 22 2c 0a 20 2f 2a 20 20 33 32 20 2a 2f 20  :=",. /*  32 */ 
3bc10 22 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72  "create_table_ar
3bc11 67 73 20 3a 3a 3d 20 4c 50 20 63 6f 6c 75 6d 6e  gs ::= LP column
3bc12 6c 69 73 74 20 63 6f 6e 73 6c 69 73 74 5f 6f 70  list conslist_op
3bc13 74 20 52 50 22 2c 0a 20 2f 2a 20 20 33 33 20 2a  t RP",. /*  33 *
3bc14 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65 5f  / "create_table_
3bc15 61 72 67 73 20 3a 3a 3d 20 41 53 20 73 65 6c 65  args ::= AS sele
3bc16 63 74 22 2c 0a 20 2f 2a 20 20 33 34 20 2a 2f 20  ct",. /*  34 */ 
3bc17 22 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20  "columnlist ::= 
3bc18 63 6f 6c 75 6d 6e 6c 69 73 74 20 43 4f 4d 4d 41  columnlist COMMA
3bc19 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 20 33   column",. /*  3
3bc1a 35 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69 73 74  5 */ "columnlist
3bc1b 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f   ::= column",. /
3bc1c 2a 20 20 33 36 20 2a 2f 20 22 63 6f 6c 75 6d 6e  *  36 */ "column
3bc1d 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69 64 20 74 79   ::= columnid ty
3bc1e 70 65 20 63 61 72 67 6c 69 73 74 22 2c 0a 20 2f  pe carglist",. /
3bc1f 2a 20 20 33 37 20 2a 2f 20 22 63 6f 6c 75 6d 6e  *  37 */ "column
3bc20 69 64 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20  id ::= nm",. /* 
3bc21 20 33 38 20 2a 2f 20 22 69 64 20 3a 3a 3d 20 49   38 */ "id ::= I
3bc22 44 22 2c 0a 20 2f 2a 20 20 33 39 20 2a 2f 20 22  D",. /*  39 */ "
3bc23 69 64 20 3a 3a 3d 20 49 4e 44 45 58 45 44 22 2c  id ::= INDEXED",
3bc24 0a 20 2f 2a 20 20 34 30 20 2a 2f 20 22 69 64 73  . /*  40 */ "ids
3bc25 20 3a 3a 3d 20 49 44 7c 53 54 52 49 4e 47 22 2c   ::= ID|STRING",
3bc26 0a 20 2f 2a 20 20 34 31 20 2a 2f 20 22 6e 6d 20  . /*  41 */ "nm 
3bc27 3a 3a 3d 20 69 64 22 2c 0a 20 2f 2a 20 20 34 32  ::= id",. /*  42
3bc28 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 53 54 52 49   */ "nm ::= STRI
3bc29 4e 47 22 2c 0a 20 2f 2a 20 20 34 33 20 2a 2f 20  NG",. /*  43 */ 
3bc2a 22 6e 6d 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 22  "nm ::= JOIN_KW"
3bc2b 2c 0a 20 2f 2a 20 20 34 34 20 2a 2f 20 22 74 79  ,. /*  44 */ "ty
3bc2c 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 34 35  pe ::=",. /*  45
3bc2d 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d 20 74 79   */ "type ::= ty
3bc2e 70 65 74 6f 6b 65 6e 22 2c 0a 20 2f 2a 20 20 34  petoken",. /*  4
3bc2f 36 20 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20  6 */ "typetoken 
3bc30 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 22 2c 0a 20  ::= typename",. 
3bc31 2f 2a 20 20 34 37 20 2a 2f 20 22 74 79 70 65 74  /*  47 */ "typet
3bc32 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d  oken ::= typenam
3bc33 65 20 4c 50 20 73 69 67 6e 65 64 20 52 50 22 2c  e LP signed RP",
3bc34 0a 20 2f 2a 20 20 34 38 20 2a 2f 20 22 74 79 70  . /*  48 */ "typ
3bc35 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e  etoken ::= typen
3bc36 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20 43 4f  ame LP signed CO
3bc37 4d 4d 41 20 73 69 67 6e 65 64 20 52 50 22 2c 0a  MMA signed RP",.
3bc38 20 2f 2a 20 20 34 39 20 2a 2f 20 22 74 79 70 65   /*  49 */ "type
3bc39 6e 61 6d 65 20 3a 3a 3d 20 69 64 73 22 2c 0a 20  name ::= ids",. 
3bc3a 2f 2a 20 20 35 30 20 2a 2f 20 22 74 79 70 65 6e  /*  50 */ "typen
3bc3b 61 6d 65 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65  ame ::= typename
3bc3c 20 69 64 73 22 2c 0a 20 2f 2a 20 20 35 31 20 2a   ids",. /*  51 *
3bc3d 2f 20 22 73 69 67 6e 65 64 20 3a 3a 3d 20 70 6c  / "signed ::= pl
3bc3e 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20 20 35 32  us_num",. /*  52
3bc3f 20 2a 2f 20 22 73 69 67 6e 65 64 20 3a 3a 3d 20   */ "signed ::= 
3bc40 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20  minus_num",. /* 
3bc41 20 35 33 20 2a 2f 20 22 63 61 72 67 6c 69 73 74   53 */ "carglist
3bc42 20 3a 3a 3d 20 63 61 72 67 6c 69 73 74 20 63 63   ::= carglist cc
3bc43 6f 6e 73 22 2c 0a 20 2f 2a 20 20 35 34 20 2a 2f  ons",. /*  54 */
3bc44 20 22 63 61 72 67 6c 69 73 74 20 3a 3a 3d 22 2c   "carglist ::=",
3bc45 0a 20 2f 2a 20 20 35 35 20 2a 2f 20 22 63 63 6f  . /*  55 */ "cco
3bc46 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e  ns ::= CONSTRAIN
3bc47 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20 35 36 20 2a  T nm",. /*  56 *
3bc48 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46  / "ccons ::= DEF
3bc49 41 55 4c 54 20 74 65 72 6d 22 2c 0a 20 2f 2a 20  AULT term",. /* 
3bc4a 20 35 37 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   57 */ "ccons ::
3bc4b 3d 20 44 45 46 41 55 4c 54 20 4c 50 20 65 78 70  = DEFAULT LP exp
3bc4c 72 20 52 50 22 2c 0a 20 2f 2a 20 20 35 38 20 2a  r RP",. /*  58 *
3bc4d 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46  / "ccons ::= DEF
3bc4e 41 55 4c 54 20 50 4c 55 53 20 74 65 72 6d 22 2c  AULT PLUS term",
3bc4f 0a 20 2f 2a 20 20 35 39 20 2a 2f 20 22 63 63 6f  . /*  59 */ "cco
3bc50 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4d  ns ::= DEFAULT M
3bc51 49 4e 55 53 20 74 65 72 6d 22 2c 0a 20 2f 2a 20  INUS term",. /* 
3bc52 20 36 30 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   60 */ "ccons ::
3bc53 3d 20 44 45 46 41 55 4c 54 20 69 64 22 2c 0a 20  = DEFAULT id",. 
3bc54 2f 2a 20 20 36 31 20 2a 2f 20 22 63 63 6f 6e 73  /*  61 */ "ccons
3bc55 20 3a 3a 3d 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66   ::= NULL onconf
3bc56 22 2c 0a 20 2f 2a 20 20 36 32 20 2a 2f 20 22 63  ",. /*  62 */ "c
3bc57 63 6f 6e 73 20 3a 3a 3d 20 4e 4f 54 20 4e 55 4c  cons ::= NOT NUL
3bc58 4c 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20  L onconf",. /*  
3bc59 36 33 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d  63 */ "ccons ::=
3bc5a 20 50 52 49 4d 41 52 59 20 4b 45 59 20 73 6f 72   PRIMARY KEY sor
3bc5b 74 6f 72 64 65 72 20 6f 6e 63 6f 6e 66 20 61 75  torder onconf au
3bc5c 74 6f 69 6e 63 22 2c 0a 20 2f 2a 20 20 36 34 20  toinc",. /*  64 
3bc5d 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 55 4e  */ "ccons ::= UN
3bc5e 49 51 55 45 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f  IQUE onconf",. /
3bc5f 2a 20 20 36 35 20 2a 2f 20 22 63 63 6f 6e 73 20  *  65 */ "ccons 
3bc60 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65 78 70  ::= CHECK LP exp
3bc61 72 20 52 50 22 2c 0a 20 2f 2a 20 20 36 36 20 2a  r RP",. /*  66 *
3bc62 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 52 45 46  / "ccons ::= REF
3bc63 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69  ERENCES nm idxli
3bc64 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73 22 2c  st_opt refargs",
3bc65 0a 20 2f 2a 20 20 36 37 20 2a 2f 20 22 63 63 6f  . /*  67 */ "cco
3bc66 6e 73 20 3a 3a 3d 20 64 65 66 65 72 5f 73 75 62  ns ::= defer_sub
3bc67 63 6c 61 75 73 65 22 2c 0a 20 2f 2a 20 20 36 38  clause",. /*  68
3bc68 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 43   */ "ccons ::= C
3bc69 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0a 20 2f 2a  OLLATE ids",. /*
3bc6a 20 20 36 39 20 2a 2f 20 22 61 75 74 6f 69 6e 63    69 */ "autoinc
3bc6b 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 37 30 20 2a   ::=",. /*  70 *
3bc6c 2f 20 22 61 75 74 6f 69 6e 63 20 3a 3a 3d 20 41  / "autoinc ::= A
3bc6d 55 54 4f 49 4e 43 52 22 2c 0a 20 2f 2a 20 20 37  UTOINCR",. /*  7
3bc6e 31 20 2a 2f 20 22 72 65 66 61 72 67 73 20 3a 3a  1 */ "refargs ::
3bc6f 3d 22 2c 0a 20 2f 2a 20 20 37 32 20 2a 2f 20 22  =",. /*  72 */ "
3bc70 72 65 66 61 72 67 73 20 3a 3a 3d 20 72 65 66 61  refargs ::= refa
3bc71 72 67 73 20 72 65 66 61 72 67 22 2c 0a 20 2f 2a  rgs refarg",. /*
3bc72 20 20 37 33 20 2a 2f 20 22 72 65 66 61 72 67 20    73 */ "refarg 
3bc73 3a 3a 3d 20 4d 41 54 43 48 20 6e 6d 22 2c 0a 20  ::= MATCH nm",. 
3bc74 2f 2a 20 20 37 34 20 2a 2f 20 22 72 65 66 61 72  /*  74 */ "refar
3bc75 67 20 3a 3a 3d 20 4f 4e 20 49 4e 53 45 52 54 20  g ::= ON INSERT 
3bc76 72 65 66 61 63 74 22 2c 0a 20 2f 2a 20 20 37 35  refact",. /*  75
3bc77 20 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d 20   */ "refarg ::= 
3bc78 4f 4e 20 44 45 4c 45 54 45 20 72 65 66 61 63 74  ON DELETE refact
3bc79 22 2c 0a 20 2f 2a 20 20 37 36 20 2a 2f 20 22 72  ",. /*  76 */ "r
3bc7a 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44  efarg ::= ON UPD
3bc7b 41 54 45 20 72 65 66 61 63 74 22 2c 0a 20 2f 2a  ATE refact",. /*
3bc7c 20 20 37 37 20 2a 2f 20 22 72 65 66 61 63 74 20    77 */ "refact 
3bc7d 3a 3a 3d 20 53 45 54 20 4e 55 4c 4c 22 2c 0a 20  ::= SET NULL",. 
3bc7e 2f 2a 20 20 37 38 20 2a 2f 20 22 72 65 66 61 63  /*  78 */ "refac
3bc7f 74 20 3a 3a 3d 20 53 45 54 20 44 45 46 41 55 4c  t ::= SET DEFAUL
3bc80 54 22 2c 0a 20 2f 2a 20 20 37 39 20 2a 2f 20 22  T",. /*  79 */ "
3bc81 72 65 66 61 63 74 20 3a 3a 3d 20 43 41 53 43 41  refact ::= CASCA
3bc82 44 45 22 2c 0a 20 2f 2a 20 20 38 30 20 2a 2f 20  DE",. /*  80 */ 
3bc83 22 72 65 66 61 63 74 20 3a 3a 3d 20 52 45 53 54  "refact ::= REST
3bc84 52 49 43 54 22 2c 0a 20 2f 2a 20 20 38 31 20 2a  RICT",. /*  81 *
3bc85 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 4e 4f  / "refact ::= NO
3bc86 20 41 43 54 49 4f 4e 22 2c 0a 20 2f 2a 20 20 38   ACTION",. /*  8
3bc87 32 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63  2 */ "defer_subc
3bc88 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44 45  lause ::= NOT DE
3bc89 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65  FERRABLE init_de
3bc8a 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 22  ferred_pred_opt"
3bc8b 2c 0a 20 2f 2a 20 20 38 33 20 2a 2f 20 22 64 65  ,. /*  83 */ "de
3bc8c 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a  fer_subclause ::
3bc8d 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69  = DEFERRABLE ini
3bc8e 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
3bc8f 6f 70 74 22 2c 0a 20 2f 2a 20 20 38 34 20 2a 2f  opt",. /*  84 */
3bc90 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f   "init_deferred_
3bc91 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  pred_opt ::=",. 
3bc92 2f 2a 20 20 38 35 20 2a 2f 20 22 69 6e 69 74 5f  /*  85 */ "init_
3bc93 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70  deferred_pred_op
3bc94 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20  t ::= INITIALLY 
3bc95 44 45 46 45 52 52 45 44 22 2c 0a 20 2f 2a 20 20  DEFERRED",. /*  
3bc96 38 36 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66 65  86 */ "init_defe
3bc97 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a  rred_pred_opt ::
3bc98 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45  = INITIALLY IMME
3bc99 44 49 41 54 45 22 2c 0a 20 2f 2a 20 20 38 37 20  DIATE",. /*  87 
3bc9a 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74  */ "conslist_opt
3bc9b 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 38 38 20 2a   ::=",. /*  88 *
3bc9c 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20  / "conslist_opt 
3bc9d 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f 6e 73 6c 69  ::= COMMA consli
3bc9e 73 74 22 2c 0a 20 2f 2a 20 20 38 39 20 2a 2f 20  st",. /*  89 */ 
3bc9f 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f  "conslist ::= co
3bca0 6e 73 6c 69 73 74 20 74 63 6f 6e 73 63 6f 6d 6d  nslist tconscomm
3bca1 61 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20 39  a tcons",. /*  9
3bca2 30 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a  0 */ "conslist :
3bca3 3a 3d 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20  := tcons",. /*  
3bca4 39 31 20 2a 2f 20 22 74 63 6f 6e 73 63 6f 6d 6d  91 */ "tconscomm
3bca5 61 20 3a 3a 3d 20 43 4f 4d 4d 41 22 2c 0a 20 2f  a ::= COMMA",. /
3bca6 2a 20 20 39 32 20 2a 2f 20 22 74 63 6f 6e 73 63  *  92 */ "tconsc
3bca7 6f 6d 6d 61 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  omma ::=",. /*  
3bca8 39 33 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d  93 */ "tcons ::=
3bca9 20 43 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 22 2c   CONSTRAINT nm",
3bcaa 0a 20 2f 2a 20 20 39 34 20 2a 2f 20 22 74 63 6f  . /*  94 */ "tco
3bcab 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b  ns ::= PRIMARY K
3bcac 45 59 20 4c 50 20 69 64 78 6c 69 73 74 20 61 75  EY LP idxlist au
3bcad 74 6f 69 6e 63 20 52 50 20 6f 6e 63 6f 6e 66 22  toinc RP onconf"
3bcae 2c 0a 20 2f 2a 20 20 39 35 20 2a 2f 20 22 74 63  ,. /*  95 */ "tc
3bcaf 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 4c  ons ::= UNIQUE L
3bcb0 50 20 69 64 78 6c 69 73 74 20 52 50 20 6f 6e 63  P idxlist RP onc
3bcb1 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39 36 20 2a 2f  onf",. /*  96 */
3bcb2 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43   "tcons ::= CHEC
3bcb3 4b 20 4c 50 20 65 78 70 72 20 52 50 20 6f 6e 63  K LP expr RP onc
3bcb4 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39 37 20 2a 2f  onf",. /*  97 */
3bcb5 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 46 4f 52 45   "tcons ::= FORE
3bcb6 49 47 4e 20 4b 45 59 20 4c 50 20 69 64 78 6c 69  IGN KEY LP idxli
3bcb7 73 74 20 52 50 20 52 45 46 45 52 45 4e 43 45 53  st RP REFERENCES
3bcb8 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20   nm idxlist_opt 
3bcb9 72 65 66 61 72 67 73 20 64 65 66 65 72 5f 73 75  refargs defer_su
3bcba 62 63 6c 61 75 73 65 5f 6f 70 74 22 2c 0a 20 2f  bclause_opt",. /
3bcbb 2a 20 20 39 38 20 2a 2f 20 22 64 65 66 65 72 5f  *  98 */ "defer_
3bcbc 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a  subclause_opt ::
3bcbd 3d 22 2c 0a 20 2f 2a 20 20 39 39 20 2a 2f 20 22  =",. /*  99 */ "
3bcbe 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f  defer_subclause_
3bcbf 6f 70 74 20 3a 3a 3d 20 64 65 66 65 72 5f 73 75  opt ::= defer_su
3bcc0 62 63 6c 61 75 73 65 22 2c 0a 20 2f 2a 20 31 30  bclause",. /* 10
3bcc1 30 20 2a 2f 20 22 6f 6e 63 6f 6e 66 20 3a 3a 3d  0 */ "onconf ::=
3bcc2 22 2c 0a 20 2f 2a 20 31 30 31 20 2a 2f 20 22 6f  ",. /* 101 */ "o
3bcc3 6e 63 6f 6e 66 20 3a 3a 3d 20 4f 4e 20 43 4f 4e  nconf ::= ON CON
3bcc4 46 4c 49 43 54 20 72 65 73 6f 6c 76 65 74 79 70  FLICT resolvetyp
3bcc5 65 22 2c 0a 20 2f 2a 20 31 30 32 20 2a 2f 20 22  e",. /* 102 */ "
3bcc6 6f 72 63 6f 6e 66 20 3a 3a 3d 22 2c 0a 20 2f 2a  orconf ::=",. /*
3bcc7 20 31 30 33 20 2a 2f 20 22 6f 72 63 6f 6e 66 20   103 */ "orconf 
3bcc8 3a 3a 3d 20 4f 52 20 72 65 73 6f 6c 76 65 74 79  ::= OR resolvety
3bcc9 70 65 22 2c 0a 20 2f 2a 20 31 30 34 20 2a 2f 20  pe",. /* 104 */ 
3bcca 22 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d  "resolvetype ::=
3bccb 20 72 61 69 73 65 74 79 70 65 22 2c 0a 20 2f 2a   raisetype",. /*
3bccc 20 31 30 35 20 2a 2f 20 22 72 65 73 6f 6c 76 65   105 */ "resolve
3bccd 74 79 70 65 20 3a 3a 3d 20 49 47 4e 4f 52 45 22  type ::= IGNORE"
3bcce 2c 0a 20 2f 2a 20 31 30 36 20 2a 2f 20 22 72 65  ,. /* 106 */ "re
3bccf 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 52 45  solvetype ::= RE
3bcd0 50 4c 41 43 45 22 2c 0a 20 2f 2a 20 31 30 37 20  PLACE",. /* 107 
3bcd1 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50  */ "cmd ::= DROP
3bcd2 20 54 41 42 4c 45 20 69 66 65 78 69 73 74 73 20   TABLE ifexists 
3bcd3 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 31  fullname",. /* 1
3bcd4 30 38 20 2a 2f 20 22 69 66 65 78 69 73 74 73 20  08 */ "ifexists 
3bcd5 3a 3a 3d 20 49 46 20 45 58 49 53 54 53 22 2c 0a  ::= IF EXISTS",.
3bcd6 20 2f 2a 20 31 30 39 20 2a 2f 20 22 69 66 65 78   /* 109 */ "ifex
3bcd7 69 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  ists ::=",. /* 1
3bcd8 31 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63  10 */ "cmd ::= c
3bcd9 72 65 61 74 65 6b 77 20 74 65 6d 70 20 56 49 45  reatekw temp VIE
3bcda 57 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d  W ifnotexists nm
3bcdb 20 64 62 6e 6d 20 41 53 20 73 65 6c 65 63 74 22   dbnm AS select"
3bcdc 2c 0a 20 2f 2a 20 31 31 31 20 2a 2f 20 22 63 6d  ,. /* 111 */ "cm
3bcdd 64 20 3a 3a 3d 20 44 52 4f 50 20 56 49 45 57 20  d ::= DROP VIEW 
3bcde 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
3bcdf 65 22 2c 0a 20 2f 2a 20 31 31 32 20 2a 2f 20 22  e",. /* 112 */ "
3bce0 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 22 2c  cmd ::= select",
3bce1 0a 20 2f 2a 20 31 31 33 20 2a 2f 20 22 73 65 6c  . /* 113 */ "sel
3bce2 65 63 74 20 3a 3a 3d 20 6f 6e 65 73 65 6c 65 63  ect ::= oneselec
3bce3 74 22 2c 0a 20 2f 2a 20 31 31 34 20 2a 2f 20 22  t",. /* 114 */ "
3bce4 73 65 6c 65 63 74 20 3a 3a 3d 20 73 65 6c 65 63  select ::= selec
3bce5 74 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70  t multiselect_op
3bce6 20 6f 6e 65 73 65 6c 65 63 74 22 2c 0a 20 2f 2a   oneselect",. /*
3bce7 20 31 31 35 20 2a 2f 20 22 6d 75 6c 74 69 73 65   115 */ "multise
3bce8 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f  lect_op ::= UNIO
3bce9 4e 22 2c 0a 20 2f 2a 20 31 31 36 20 2a 2f 20 22  N",. /* 116 */ "
3bcea 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a  multiselect_op :
3bceb 3a 3d 20 55 4e 49 4f 4e 20 41 4c 4c 22 2c 0a 20  := UNION ALL",. 
3bcec 2f 2a 20 31 31 37 20 2a 2f 20 22 6d 75 6c 74 69  /* 117 */ "multi
3bced 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 45 58  select_op ::= EX
3bcee 43 45 50 54 7c 49 4e 54 45 52 53 45 43 54 22 2c  CEPT|INTERSECT",
3bcef 0a 20 2f 2a 20 31 31 38 20 2a 2f 20 22 6f 6e 65  . /* 118 */ "one
3bcf0 73 65 6c 65 63 74 20 3a 3a 3d 20 53 45 4c 45 43  select ::= SELEC
3bcf1 54 20 64 69 73 74 69 6e 63 74 20 73 65 6c 63 6f  T distinct selco
3bcf2 6c 6c 69 73 74 20 66 72 6f 6d 20 77 68 65 72 65  llist from where
3bcf3 5f 6f 70 74 20 67 72 6f 75 70 62 79 5f 6f 70 74  _opt groupby_opt
3bcf4 20 68 61 76 69 6e 67 5f 6f 70 74 20 6f 72 64 65   having_opt orde
3bcf5 72 62 79 5f 6f 70 74 20 6c 69 6d 69 74 5f 6f 70  rby_opt limit_op
3bcf6 74 22 2c 0a 20 2f 2a 20 31 31 39 20 2a 2f 20 22  t",. /* 119 */ "
3bcf7 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 44 49 53  distinct ::= DIS
3bcf8 54 49 4e 43 54 22 2c 0a 20 2f 2a 20 31 32 30 20  TINCT",. /* 120 
3bcf9 2a 2f 20 22 64 69 73 74 69 6e 63 74 20 3a 3a 3d  */ "distinct ::=
3bcfa 20 41 4c 4c 22 2c 0a 20 2f 2a 20 31 32 31 20 2a   ALL",. /* 121 *
3bcfb 2f 20 22 64 69 73 74 69 6e 63 74 20 3a 3a 3d 22  / "distinct ::="
3bcfc 2c 0a 20 2f 2a 20 31 32 32 20 2a 2f 20 22 73 63  ,. /* 122 */ "sc
3bcfd 6c 70 20 3a 3a 3d 20 73 65 6c 63 6f 6c 6c 69 73  lp ::= selcollis
3bcfe 74 20 43 4f 4d 4d 41 22 2c 0a 20 2f 2a 20 31 32  t COMMA",. /* 12
3bcff 33 20 2a 2f 20 22 73 63 6c 70 20 3a 3a 3d 22 2c  3 */ "sclp ::=",
3bd00 0a 20 2f 2a 20 31 32 34 20 2a 2f 20 22 73 65 6c  . /* 124 */ "sel
3bd01 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70  collist ::= sclp
3bd02 20 65 78 70 72 20 61 73 22 2c 0a 20 2f 2a 20 31   expr as",. /* 1
3bd03 32 35 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73  25 */ "selcollis
3bd04 74 20 3a 3a 3d 20 73 63 6c 70 20 53 54 41 52 22  t ::= sclp STAR"
3bd05 2c 0a 20 2f 2a 20 31 32 36 20 2a 2f 20 22 73 65  ,. /* 126 */ "se
3bd06 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c  lcollist ::= scl
3bd07 70 20 6e 6d 20 44 4f 54 20 53 54 41 52 22 2c 0a  p nm DOT STAR",.
3bd08 20 2f 2a 20 31 32 37 20 2a 2f 20 22 61 73 20 3a   /* 127 */ "as :
3bd09 3a 3d 20 41 53 20 6e 6d 22 2c 0a 20 2f 2a 20 31  := AS nm",. /* 1
3bd0a 32 38 20 2a 2f 20 22 61 73 20 3a 3a 3d 20 69 64  28 */ "as ::= id
3bd0b 73 22 2c 0a 20 2f 2a 20 31 32 39 20 2a 2f 20 22  s",. /* 129 */ "
3bd0c 61 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 30  as ::=",. /* 130
3bd0d 20 2a 2f 20 22 66 72 6f 6d 20 3a 3a 3d 22 2c 0a   */ "from ::=",.
3bd0e 20 2f 2a 20 31 33 31 20 2a 2f 20 22 66 72 6f 6d   /* 131 */ "from
3bd0f 20 3a 3a 3d 20 46 52 4f 4d 20 73 65 6c 74 61 62   ::= FROM seltab
3bd10 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 33 32 20 2a  list",. /* 132 *
3bd11 2f 20 22 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a  / "stl_prefix ::
3bd12 3d 20 73 65 6c 74 61 62 6c 69 73 74 20 6a 6f 69  = seltablist joi
3bd13 6e 6f 70 22 2c 0a 20 2f 2a 20 31 33 33 20 2a 2f  nop",. /* 133 */
3bd14 20 22 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a 3d   "stl_prefix ::=
3bd15 22 2c 0a 20 2f 2a 20 31 33 34 20 2a 2f 20 22 73  ",. /* 134 */ "s
3bd16 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74  eltablist ::= st
3bd17 6c 5f 70 72 65 66 69 78 20 6e 6d 20 64 62 6e 6d  l_prefix nm dbnm
3bd18 20 61 73 20 69 6e 64 65 78 65 64 5f 6f 70 74 20   as indexed_opt 
3bd19 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74  on_opt using_opt
3bd1a 22 2c 0a 20 2f 2a 20 31 33 35 20 2a 2f 20 22 73  ",. /* 135 */ "s
3bd1b 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74  eltablist ::= st
3bd1c 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65 6c 65  l_prefix LP sele
3bd1d 63 74 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20  ct RP as on_opt 
3bd1e 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20  using_opt",. /* 
3bd1f 31 33 36 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69  136 */ "seltabli
3bd20 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69  st ::= stl_prefi
3bd21 78 20 4c 50 20 73 65 6c 74 61 62 6c 69 73 74 20  x LP seltablist 
3bd22 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20 75 73 69  RP as on_opt usi
3bd23 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 33 37  ng_opt",. /* 137
3bd24 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a 3d 22 2c 0a   */ "dbnm ::=",.
3bd25 20 2f 2a 20 31 33 38 20 2a 2f 20 22 64 62 6e 6d   /* 138 */ "dbnm
3bd26 20 3a 3a 3d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f   ::= DOT nm",. /
3bd27 2a 20 31 33 39 20 2a 2f 20 22 66 75 6c 6c 6e 61  * 139 */ "fullna
3bd28 6d 65 20 3a 3a 3d 20 6e 6d 20 64 62 6e 6d 22 2c  me ::= nm dbnm",
3bd29 0a 20 2f 2a 20 31 34 30 20 2a 2f 20 22 6a 6f 69  . /* 140 */ "joi
3bd2a 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f  nop ::= COMMA|JO
3bd2b 49 4e 22 2c 0a 20 2f 2a 20 31 34 31 20 2a 2f 20  IN",. /* 141 */ 
3bd2c 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e  "joinop ::= JOIN
3bd2d 5f 4b 57 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31  _KW JOIN",. /* 1
3bd2e 34 32 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a  42 */ "joinop ::
3bd2f 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49  = JOIN_KW nm JOI
3bd30 4e 22 2c 0a 20 2f 2a 20 31 34 33 20 2a 2f 20 22  N",. /* 143 */ "
3bd31 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f  joinop ::= JOIN_
3bd32 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 22 2c 0a  KW nm nm JOIN",.
3bd33 20 2f 2a 20 31 34 34 20 2a 2f 20 22 6f 6e 5f 6f   /* 144 */ "on_o
3bd34 70 74 20 3a 3a 3d 20 4f 4e 20 65 78 70 72 22 2c  pt ::= ON expr",
3bd35 0a 20 2f 2a 20 31 34 35 20 2a 2f 20 22 6f 6e 5f  . /* 145 */ "on_
3bd36 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34  opt ::=",. /* 14
3bd37 36 20 2a 2f 20 22 69 6e 64 65 78 65 64 5f 6f 70  6 */ "indexed_op
3bd38 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34 37 20  t ::=",. /* 147 
3bd39 2a 2f 20 22 69 6e 64 65 78 65 64 5f 6f 70 74 20  */ "indexed_opt 
3bd3a 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42 59 20 6e  ::= INDEXED BY n
3bd3b 6d 22 2c 0a 20 2f 2a 20 31 34 38 20 2a 2f 20 22  m",. /* 148 */ "
3bd3c 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20  indexed_opt ::= 
3bd3d 4e 4f 54 20 49 4e 44 45 58 45 44 22 2c 0a 20 2f  NOT INDEXED",. /
3bd3e 2a 20 31 34 39 20 2a 2f 20 22 75 73 69 6e 67 5f  * 149 */ "using_
3bd3f 6f 70 74 20 3a 3a 3d 20 55 53 49 4e 47 20 4c 50  opt ::= USING LP
3bd40 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50 22 2c   inscollist RP",
3bd41 0a 20 2f 2a 20 31 35 30 20 2a 2f 20 22 75 73 69  . /* 150 */ "usi
3bd42 6e 67 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  ng_opt ::=",. /*
3bd43 20 31 35 31 20 2a 2f 20 22 6f 72 64 65 72 62 79   151 */ "orderby
3bd44 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  _opt ::=",. /* 1
3bd45 35 32 20 2a 2f 20 22 6f 72 64 65 72 62 79 5f 6f  52 */ "orderby_o
3bd46 70 74 20 3a 3a 3d 20 4f 52 44 45 52 20 42 59 20  pt ::= ORDER BY 
3bd47 73 6f 72 74 6c 69 73 74 22 2c 0a 20 2f 2a 20 31  sortlist",. /* 1
3bd48 35 33 20 2a 2f 20 22 73 6f 72 74 6c 69 73 74 20  53 */ "sortlist 
3bd49 3a 3a 3d 20 73 6f 72 74 6c 69 73 74 20 43 4f 4d  ::= sortlist COM
3bd4a 4d 41 20 65 78 70 72 20 73 6f 72 74 6f 72 64 65  MA expr sortorde
3bd4b 72 22 2c 0a 20 2f 2a 20 31 35 34 20 2a 2f 20 22  r",. /* 154 */ "
3bd4c 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 65 78 70  sortlist ::= exp
3bd4d 72 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f  r sortorder",. /
3bd4e 2a 20 31 35 35 20 2a 2f 20 22 73 6f 72 74 6f 72  * 155 */ "sortor
3bd4f 64 65 72 20 3a 3a 3d 20 41 53 43 22 2c 0a 20 2f  der ::= ASC",. /
3bd50 2a 20 31 35 36 20 2a 2f 20 22 73 6f 72 74 6f 72  * 156 */ "sortor
3bd51 64 65 72 20 3a 3a 3d 20 44 45 53 43 22 2c 0a 20  der ::= DESC",. 
3bd52 2f 2a 20 31 35 37 20 2a 2f 20 22 73 6f 72 74 6f  /* 157 */ "sorto
3bd53 72 64 65 72 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  rder ::=",. /* 1
3bd54 35 38 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f 6f  58 */ "groupby_o
3bd55 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 39  pt ::=",. /* 159
3bd56 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f 6f 70 74   */ "groupby_opt
3bd57 20 3a 3a 3d 20 47 52 4f 55 50 20 42 59 20 6e 65   ::= GROUP BY ne
3bd58 78 70 72 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 36  xprlist",. /* 16
3bd59 30 20 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70 74  0 */ "having_opt
3bd5a 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36 31 20 2a   ::=",. /* 161 *
3bd5b 2f 20 22 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a  / "having_opt ::
3bd5c 3d 20 48 41 56 49 4e 47 20 65 78 70 72 22 2c 0a  = HAVING expr",.
3bd5d 20 2f 2a 20 31 36 32 20 2a 2f 20 22 6c 69 6d 69   /* 162 */ "limi
3bd5e 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  t_opt ::=",. /* 
3bd5f 31 36 33 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70  163 */ "limit_op
3bd60 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72  t ::= LIMIT expr
3bd61 22 2c 0a 20 2f 2a 20 31 36 34 20 2a 2f 20 22 6c  ",. /* 164 */ "l
3bd62 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d  imit_opt ::= LIM
3bd63 49 54 20 65 78 70 72 20 4f 46 46 53 45 54 20 65  IT expr OFFSET e
3bd64 78 70 72 22 2c 0a 20 2f 2a 20 31 36 35 20 2a 2f  xpr",. /* 165 */
3bd65 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20   "limit_opt ::= 
3bd66 4c 49 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d 41  LIMIT expr COMMA
3bd67 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 36 20   expr",. /* 166 
3bd68 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 45 4c 45  */ "cmd ::= DELE
3bd69 54 45 20 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d 65  TE FROM fullname
3bd6a 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 77 68 65   indexed_opt whe
3bd6b 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 36 37  re_opt",. /* 167
3bd6c 20 2a 2f 20 22 77 68 65 72 65 5f 6f 70 74 20 3a   */ "where_opt :
3bd6d 3a 3d 22 2c 0a 20 2f 2a 20 31 36 38 20 2a 2f 20  :=",. /* 168 */ 
3bd6e 22 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20 57  "where_opt ::= W
3bd6f 48 45 52 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20  HERE expr",. /* 
3bd70 31 36 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  169 */ "cmd ::= 
3bd71 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20 66 75  UPDATE orconf fu
3bd72 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f  llname indexed_o
3bd73 70 74 20 53 45 54 20 73 65 74 6c 69 73 74 20 77  pt SET setlist w
3bd74 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31  here_opt",. /* 1
3bd75 37 30 20 2a 2f 20 22 73 65 74 6c 69 73 74 20 3a  70 */ "setlist :
3bd76 3a 3d 20 73 65 74 6c 69 73 74 20 43 4f 4d 4d 41  := setlist COMMA
3bd77 20 6e 6d 20 45 51 20 65 78 70 72 22 2c 0a 20 2f   nm EQ expr",. /
3bd78 2a 20 31 37 31 20 2a 2f 20 22 73 65 74 6c 69 73  * 171 */ "setlis
3bd79 74 20 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70 72  t ::= nm EQ expr
3bd7a 22 2c 0a 20 2f 2a 20 31 37 32 20 2a 2f 20 22 63  ",. /* 172 */ "c
3bd7b 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
3bd7c 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20  d INTO fullname 
3bd7d 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 76  inscollist_opt v
3bd7e 61 6c 75 65 6c 69 73 74 22 2c 0a 20 2f 2a 20 31  aluelist",. /* 1
3bd7f 37 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69  73 */ "cmd ::= i
3bd80 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66  nsert_cmd INTO f
3bd81 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69  ullname inscolli
3bd82 73 74 5f 6f 70 74 20 73 65 6c 65 63 74 22 2c 0a  st_opt select",.
3bd83 20 2f 2a 20 31 37 34 20 2a 2f 20 22 63 6d 64 20   /* 174 */ "cmd 
3bd84 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49  ::= insert_cmd I
3bd85 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73  NTO fullname ins
3bd86 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 44 45 46 41  collist_opt DEFA
3bd87 55 4c 54 20 56 41 4c 55 45 53 22 2c 0a 20 2f 2a  ULT VALUES",. /*
3bd88 20 31 37 35 20 2a 2f 20 22 69 6e 73 65 72 74 5f   175 */ "insert_
3bd89 63 6d 64 20 3a 3a 3d 20 49 4e 53 45 52 54 20 6f  cmd ::= INSERT o
3bd8a 72 63 6f 6e 66 22 2c 0a 20 2f 2a 20 31 37 36 20  rconf",. /* 176 
3bd8b 2a 2f 20 22 69 6e 73 65 72 74 5f 63 6d 64 20 3a  */ "insert_cmd :
3bd8c 3a 3d 20 52 45 50 4c 41 43 45 22 2c 0a 20 2f 2a  := REPLACE",. /*
3bd8d 20 31 37 37 20 2a 2f 20 22 76 61 6c 75 65 6c 69   177 */ "valueli
3bd8e 73 74 20 3a 3a 3d 20 56 41 4c 55 45 53 20 4c 50  st ::= VALUES LP
3bd8f 20 6e 65 78 70 72 6c 69 73 74 20 52 50 22 2c 0a   nexprlist RP",.
3bd90 20 2f 2a 20 31 37 38 20 2a 2f 20 22 76 61 6c 75   /* 178 */ "valu
3bd91 65 6c 69 73 74 20 3a 3a 3d 20 76 61 6c 75 65 6c  elist ::= valuel
3bd92 69 73 74 20 43 4f 4d 4d 41 20 4c 50 20 65 78 70  ist COMMA LP exp
3bd93 72 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31  rlist RP",. /* 1
3bd94 37 39 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73  79 */ "inscollis
3bd95 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  t_opt ::=",. /* 
3bd96 31 38 30 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69  180 */ "inscolli
3bd97 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 6e  st_opt ::= LP in
3bd98 73 63 6f 6c 6c 69 73 74 20 52 50 22 2c 0a 20 2f  scollist RP",. /
3bd99 2a 20 31 38 31 20 2a 2f 20 22 69 6e 73 63 6f 6c  * 181 */ "inscol
3bd9a 6c 69 73 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c  list ::= inscoll
3bd9b 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 22 2c 0a 20  ist COMMA nm",. 
3bd9c 2f 2a 20 31 38 32 20 2a 2f 20 22 69 6e 73 63 6f  /* 182 */ "insco
3bd9d 6c 6c 69 73 74 20 3a 3a 3d 20 6e 6d 22 2c 0a 20  llist ::= nm",. 
3bd9e 2f 2a 20 31 38 33 20 2a 2f 20 22 65 78 70 72 20  /* 183 */ "expr 
3bd9f 3a 3a 3d 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 31  ::= term",. /* 1
3bda0 38 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  84 */ "expr ::= 
3bda1 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a  LP expr RP",. /*
3bda2 20 31 38 35 20 2a 2f 20 22 74 65 72 6d 20 3a 3a   185 */ "term ::
3bda3 3d 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 31 38 36  = NULL",. /* 186
3bda4 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 69 64   */ "expr ::= id
3bda5 22 2c 0a 20 2f 2a 20 31 38 37 20 2a 2f 20 22 65  ",. /* 187 */ "e
3bda6 78 70 72 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 22  xpr ::= JOIN_KW"
3bda7 2c 0a 20 2f 2a 20 31 38 38 20 2a 2f 20 22 65 78  ,. /* 188 */ "ex
3bda8 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d  pr ::= nm DOT nm
3bda9 22 2c 0a 20 2f 2a 20 31 38 39 20 2a 2f 20 22 65  ",. /* 189 */ "e
3bdaa 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e  xpr ::= nm DOT n
3bdab 6d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31  m DOT nm",. /* 1
3bdac 39 30 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20  90 */ "term ::= 
3bdad 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 7c 42 4c  INTEGER|FLOAT|BL
3bdae 4f 42 22 2c 0a 20 2f 2a 20 31 39 31 20 2a 2f 20  OB",. /* 191 */ 
3bdaf 22 74 65 72 6d 20 3a 3a 3d 20 53 54 52 49 4e 47  "term ::= STRING
3bdb0 22 2c 0a 20 2f 2a 20 31 39 32 20 2a 2f 20 22 65  ",. /* 192 */ "e
3bdb1 78 70 72 20 3a 3a 3d 20 52 45 47 49 53 54 45 52  xpr ::= REGISTER
3bdb2 22 2c 0a 20 2f 2a 20 31 39 33 20 2a 2f 20 22 65  ",. /* 193 */ "e
3bdb3 78 70 72 20 3a 3a 3d 20 56 41 52 49 41 42 4c 45  xpr ::= VARIABLE
3bdb4 22 2c 0a 20 2f 2a 20 31 39 34 20 2a 2f 20 22 65  ",. /* 194 */ "e
3bdb5 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4c  xpr ::= expr COL
3bdb6 4c 41 54 45 20 69 64 73 22 2c 0a 20 2f 2a 20 31  LATE ids",. /* 1
3bdb7 39 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  95 */ "expr ::= 
3bdb8 43 41 53 54 20 4c 50 20 65 78 70 72 20 41 53 20  CAST LP expr AS 
3bdb9 74 79 70 65 74 6f 6b 65 6e 20 52 50 22 2c 0a 20  typetoken RP",. 
3bdba 2f 2a 20 31 39 36 20 2a 2f 20 22 65 78 70 72 20  /* 196 */ "expr 
3bdbb 3a 3a 3d 20 49 44 20 4c 50 20 64 69 73 74 69 6e  ::= ID LP distin
3bdbc 63 74 20 65 78 70 72 6c 69 73 74 20 52 50 22 2c  ct exprlist RP",
3bdbd 0a 20 2f 2a 20 31 39 37 20 2a 2f 20 22 65 78 70  . /* 197 */ "exp
3bdbe 72 20 3a 3a 3d 20 49 44 20 4c 50 20 53 54 41 52  r ::= ID LP STAR
3bdbf 20 52 50 22 2c 0a 20 2f 2a 20 31 39 38 20 2a 2f   RP",. /* 198 */
3bdc0 20 22 74 65 72 6d 20 3a 3a 3d 20 43 54 49 4d 45   "term ::= CTIME
3bdc1 5f 4b 57 22 2c 0a 20 2f 2a 20 31 39 39 20 2a 2f  _KW",. /* 199 */
3bdc2 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
3bdc3 41 4e 44 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  AND expr",. /* 2
3bdc4 30 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  00 */ "expr ::= 
3bdc5 65 78 70 72 20 4f 52 20 65 78 70 72 22 2c 0a 20  expr OR expr",. 
3bdc6 2f 2a 20 32 30 31 20 2a 2f 20 22 65 78 70 72 20  /* 201 */ "expr 
3bdc7 3a 3a 3d 20 65 78 70 72 20 4c 54 7c 47 54 7c 47  ::= expr LT|GT|G
3bdc8 45 7c 4c 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20  E|LE expr",. /* 
3bdc9 32 30 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  202 */ "expr ::=
3bdca 20 65 78 70 72 20 45 51 7c 4e 45 20 65 78 70 72   expr EQ|NE expr
3bdcb 22 2c 0a 20 2f 2a 20 32 30 33 20 2a 2f 20 22 65  ",. /* 203 */ "e
3bdcc 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 42 49 54  xpr ::= expr BIT
3bdcd 41 4e 44 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54  AND|BITOR|LSHIFT
3bdce 7c 52 53 48 49 46 54 20 65 78 70 72 22 2c 0a 20  |RSHIFT expr",. 
3bdcf 2f 2a 20 32 30 34 20 2a 2f 20 22 65 78 70 72 20  /* 204 */ "expr 
3bdd0 3a 3a 3d 20 65 78 70 72 20 50 4c 55 53 7c 4d 49  ::= expr PLUS|MI
3bdd1 4e 55 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  NUS expr",. /* 2
3bdd2 30 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  05 */ "expr ::= 
3bdd3 65 78 70 72 20 53 54 41 52 7c 53 4c 41 53 48 7c  expr STAR|SLASH|
3bdd4 52 45 4d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  REM expr",. /* 2
3bdd5 30 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  06 */ "expr ::= 
3bdd6 65 78 70 72 20 43 4f 4e 43 41 54 20 65 78 70 72  expr CONCAT expr
3bdd7 22 2c 0a 20 2f 2a 20 32 30 37 20 2a 2f 20 22 6c  ",. /* 207 */ "l
3bdd8 69 6b 65 6f 70 20 3a 3a 3d 20 4c 49 4b 45 5f 4b  ikeop ::= LIKE_K
3bdd9 57 22 2c 0a 20 2f 2a 20 32 30 38 20 2a 2f 20 22  W",. /* 208 */ "
3bdda 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4c  likeop ::= NOT L
3bddb 49 4b 45 5f 4b 57 22 2c 0a 20 2f 2a 20 32 30 39  IKE_KW",. /* 209
3bddc 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20   */ "likeop ::= 
3bddd 4d 41 54 43 48 22 2c 0a 20 2f 2a 20 32 31 30 20  MATCH",. /* 210 
3bdde 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e  */ "likeop ::= N
3bddf 4f 54 20 4d 41 54 43 48 22 2c 0a 20 2f 2a 20 32  OT MATCH",. /* 2
3bde0 31 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  11 */ "expr ::= 
3bde1 65 78 70 72 20 6c 69 6b 65 6f 70 20 65 78 70 72  expr likeop expr
3bde2 22 2c 0a 20 2f 2a 20 32 31 32 20 2a 2f 20 22 65  ",. /* 212 */ "e
3bde3 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 6c 69 6b  xpr ::= expr lik
3bde4 65 6f 70 20 65 78 70 72 20 45 53 43 41 50 45 20  eop expr ESCAPE 
3bde5 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 33 20 2a  expr",. /* 213 *
3bde6 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
3bde7 20 49 53 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 22   ISNULL|NOTNULL"
3bde8 2c 0a 20 2f 2a 20 32 31 34 20 2a 2f 20 22 65 78  ,. /* 214 */ "ex
3bde9 70 72 20 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20  pr ::= expr NOT 
3bdea 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32 31 35 20 2a  NULL",. /* 215 *
3bdeb 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
3bdec 20 49 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32   IS expr",. /* 2
3bded 31 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  16 */ "expr ::= 
3bdee 65 78 70 72 20 49 53 20 4e 4f 54 20 65 78 70 72  expr IS NOT expr
3bdef 22 2c 0a 20 2f 2a 20 32 31 37 20 2a 2f 20 22 65  ",. /* 217 */ "e
3bdf0 78 70 72 20 3a 3a 3d 20 4e 4f 54 20 65 78 70 72  xpr ::= NOT expr
3bdf1 22 2c 0a 20 2f 2a 20 32 31 38 20 2a 2f 20 22 65  ",. /* 218 */ "e
3bdf2 78 70 72 20 3a 3a 3d 20 42 49 54 4e 4f 54 20 65  xpr ::= BITNOT e
3bdf3 78 70 72 22 2c 0a 20 2f 2a 20 32 31 39 20 2a 2f  xpr",. /* 219 */
3bdf4 20 22 65 78 70 72 20 3a 3a 3d 20 4d 49 4e 55 53   "expr ::= MINUS
3bdf5 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 30 20   expr",. /* 220 
3bdf6 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 50 4c 55  */ "expr ::= PLU
3bdf7 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 31  S expr",. /* 221
3bdf8 20 2a 2f 20 22 62 65 74 77 65 65 6e 5f 6f 70 20   */ "between_op 
3bdf9 3a 3a 3d 20 42 45 54 57 45 45 4e 22 2c 0a 20 2f  ::= BETWEEN",. /
3bdfa 2a 20 32 32 32 20 2a 2f 20 22 62 65 74 77 65 65  * 222 */ "betwee
3bdfb 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 42 45 54  n_op ::= NOT BET
3bdfc 57 45 45 4e 22 2c 0a 20 2f 2a 20 32 32 33 20 2a  WEEN",. /* 223 *
3bdfd 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
3bdfe 20 62 65 74 77 65 65 6e 5f 6f 70 20 65 78 70 72   between_op expr
3bdff 20 41 4e 44 20 65 78 70 72 22 2c 0a 20 2f 2a 20   AND expr",. /* 
3be00 32 32 34 20 2a 2f 20 22 69 6e 5f 6f 70 20 3a 3a  224 */ "in_op ::
3be01 3d 20 49 4e 22 2c 0a 20 2f 2a 20 32 32 35 20 2a  = IN",. /* 225 *
3be02 2f 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54  / "in_op ::= NOT
3be03 20 49 4e 22 2c 0a 20 2f 2a 20 32 32 36 20 2a 2f   IN",. /* 226 */
3be04 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
3be05 69 6e 5f 6f 70 20 4c 50 20 65 78 70 72 6c 69 73  in_op LP exprlis
3be06 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32 37 20 2a  t RP",. /* 227 *
3be07 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4c 50 20 73  / "expr ::= LP s
3be08 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32  elect RP",. /* 2
3be09 32 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  28 */ "expr ::= 
3be0a 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 73 65  expr in_op LP se
3be0b 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32  lect RP",. /* 22
3be0c 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  9 */ "expr ::= e
3be0d 78 70 72 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e  xpr in_op nm dbn
3be0e 6d 22 2c 0a 20 2f 2a 20 32 33 30 20 2a 2f 20 22  m",. /* 230 */ "
3be0f 65 78 70 72 20 3a 3a 3d 20 45 58 49 53 54 53 20  expr ::= EXISTS 
3be10 4c 50 20 73 65 6c 65 63 74 20 52 50 22 2c 0a 20  LP select RP",. 
3be11 2f 2a 20 32 33 31 20 2a 2f 20 22 65 78 70 72 20  /* 231 */ "expr 
3be12 3a 3a 3d 20 43 41 53 45 20 63 61 73 65 5f 6f 70  ::= CASE case_op
3be13 65 72 61 6e 64 20 63 61 73 65 5f 65 78 70 72 6c  erand case_exprl
3be14 69 73 74 20 63 61 73 65 5f 65 6c 73 65 20 45 4e  ist case_else EN
3be15 44 22 2c 0a 20 2f 2a 20 32 33 32 20 2a 2f 20 22  D",. /* 232 */ "
3be16 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a  case_exprlist ::
3be17 3d 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20  = case_exprlist 
3be18 57 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65  WHEN expr THEN e
3be19 78 70 72 22 2c 0a 20 2f 2a 20 32 33 33 20 2a 2f  xpr",. /* 233 */
3be1a 20 22 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20   "case_exprlist 
3be1b 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72 20 54 48  ::= WHEN expr TH
3be1c 45 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33  EN expr",. /* 23
3be1d 34 20 2a 2f 20 22 63 61 73 65 5f 65 6c 73 65 20  4 */ "case_else 
3be1e 3a 3a 3d 20 45 4c 53 45 20 65 78 70 72 22 2c 0a  ::= ELSE expr",.
3be1f 20 2f 2a 20 32 33 35 20 2a 2f 20 22 63 61 73 65   /* 235 */ "case
3be20 5f 65 6c 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  _else ::=",. /* 
3be21 32 33 36 20 2a 2f 20 22 63 61 73 65 5f 6f 70 65  236 */ "case_ope
3be22 72 61 6e 64 20 3a 3a 3d 20 65 78 70 72 22 2c 0a  rand ::= expr",.
3be23 20 2f 2a 20 32 33 37 20 2a 2f 20 22 63 61 73 65   /* 237 */ "case
3be24 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 22 2c 0a 20  _operand ::=",. 
3be25 2f 2a 20 32 33 38 20 2a 2f 20 22 65 78 70 72 6c  /* 238 */ "exprl
3be26 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73  ist ::= nexprlis
3be27 74 22 2c 0a 20 2f 2a 20 32 33 39 20 2a 2f 20 22  t",. /* 239 */ "
3be28 65 78 70 72 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20  exprlist ::=",. 
3be29 2f 2a 20 32 34 30 20 2a 2f 20 22 6e 65 78 70 72  /* 240 */ "nexpr
3be2a 6c 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69  list ::= nexprli
3be2b 73 74 20 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a  st COMMA expr",.
3be2c 20 2f 2a 20 32 34 31 20 2a 2f 20 22 6e 65 78 70   /* 241 */ "nexp
3be2d 72 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 22 2c  rlist ::= expr",
3be2e 0a 20 2f 2a 20 32 34 32 20 2a 2f 20 22 63 6d 64  . /* 242 */ "cmd
3be2f 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 75 6e   ::= createkw un
3be30 69 71 75 65 66 6c 61 67 20 49 4e 44 45 58 20 69  iqueflag INDEX i
3be31 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62  fnotexists nm db
3be32 6e 6d 20 4f 4e 20 6e 6d 20 4c 50 20 69 64 78 6c  nm ON nm LP idxl
3be33 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 32 34 33  ist RP",. /* 243
3be34 20 2a 2f 20 22 75 6e 69 71 75 65 66 6c 61 67 20   */ "uniqueflag 
3be35 3a 3a 3d 20 55 4e 49 51 55 45 22 2c 0a 20 2f 2a  ::= UNIQUE",. /*
3be36 20 32 34 34 20 2a 2f 20 22 75 6e 69 71 75 65 66   244 */ "uniquef
3be37 6c 61 67 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34  lag ::=",. /* 24
3be38 35 20 2a 2f 20 22 69 64 78 6c 69 73 74 5f 6f 70  5 */ "idxlist_op
3be39 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 36 20  t ::=",. /* 246 
3be3a 2a 2f 20 22 69 64 78 6c 69 73 74 5f 6f 70 74 20  */ "idxlist_opt 
3be3b 3a 3a 3d 20 4c 50 20 69 64 78 6c 69 73 74 20 52  ::= LP idxlist R
3be3c 50 22 2c 0a 20 2f 2a 20 32 34 37 20 2a 2f 20 22  P",. /* 247 */ "
3be3d 69 64 78 6c 69 73 74 20 3a 3a 3d 20 69 64 78 6c  idxlist ::= idxl
3be3e 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 63 6f 6c  ist COMMA nm col
3be3f 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72 22 2c  late sortorder",
3be40 0a 20 2f 2a 20 32 34 38 20 2a 2f 20 22 69 64 78  . /* 248 */ "idx
3be41 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c  list ::= nm coll
3be42 61 74 65 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a  ate sortorder",.
3be43 20 2f 2a 20 32 34 39 20 2a 2f 20 22 63 6f 6c 6c   /* 249 */ "coll
3be44 61 74 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 35  ate ::=",. /* 25
3be45 30 20 2a 2f 20 22 63 6f 6c 6c 61 74 65 20 3a 3a  0 */ "collate ::
3be46 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0a  = COLLATE ids",.
3be47 20 2f 2a 20 32 35 31 20 2a 2f 20 22 63 6d 64 20   /* 251 */ "cmd 
3be48 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45 58 20 69  ::= DROP INDEX i
3be49 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65  fexists fullname
3be4a 22 2c 0a 20 2f 2a 20 32 35 32 20 2a 2f 20 22 63  ",. /* 252 */ "c
3be4b 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 22 2c 0a  md ::= VACUUM",.
3be4c 20 2f 2a 20 32 35 33 20 2a 2f 20 22 63 6d 64 20   /* 253 */ "cmd 
3be4d 3a 3a 3d 20 56 41 43 55 55 4d 20 6e 6d 22 2c 0a  ::= VACUUM nm",.
3be4e 20 2f 2a 20 32 35 34 20 2a 2f 20 22 63 6d 64 20   /* 254 */ "cmd 
3be4f 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62  ::= PRAGMA nm db
3be50 6e 6d 22 2c 0a 20 2f 2a 20 32 35 35 20 2a 2f 20  nm",. /* 255 */ 
3be51 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20  "cmd ::= PRAGMA 
3be52 6e 6d 20 64 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d  nm dbnm EQ nmnum
3be53 22 2c 0a 20 2f 2a 20 32 35 36 20 2a 2f 20 22 63  ",. /* 256 */ "c
3be54 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d  md ::= PRAGMA nm
3be55 20 64 62 6e 6d 20 4c 50 20 6e 6d 6e 75 6d 20 52   dbnm LP nmnum R
3be56 50 22 2c 0a 20 2f 2a 20 32 35 37 20 2a 2f 20 22  P",. /* 257 */ "
3be57 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e  cmd ::= PRAGMA n
3be58 6d 20 64 62 6e 6d 20 45 51 20 6d 69 6e 75 73 5f  m dbnm EQ minus_
3be59 6e 75 6d 22 2c 0a 20 2f 2a 20 32 35 38 20 2a 2f  num",. /* 258 */
3be5a 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41   "cmd ::= PRAGMA
3be5b 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6d 69 6e 75   nm dbnm LP minu
3be5c 73 5f 6e 75 6d 20 52 50 22 2c 0a 20 2f 2a 20 32  s_num RP",. /* 2
3be5d 35 39 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d  59 */ "nmnum ::=
3be5e 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20   plus_num",. /* 
3be5f 32 36 30 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a  260 */ "nmnum ::
3be60 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 32 36 31 20 2a  = nm",. /* 261 *
3be61 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 4f 4e 22  / "nmnum ::= ON"
3be62 2c 0a 20 2f 2a 20 32 36 32 20 2a 2f 20 22 6e 6d  ,. /* 262 */ "nm
3be63 6e 75 6d 20 3a 3a 3d 20 44 45 4c 45 54 45 22 2c  num ::= DELETE",
3be64 0a 20 2f 2a 20 32 36 33 20 2a 2f 20 22 6e 6d 6e  . /* 263 */ "nmn
3be65 75 6d 20 3a 3a 3d 20 44 45 46 41 55 4c 54 22 2c  um ::= DEFAULT",
3be66 0a 20 2f 2a 20 32 36 34 20 2a 2f 20 22 70 6c 75  . /* 264 */ "plu
3be67 73 5f 6e 75 6d 20 3a 3a 3d 20 50 4c 55 53 20 6e  s_num ::= PLUS n
3be68 75 6d 62 65 72 22 2c 0a 20 2f 2a 20 32 36 35 20  umber",. /* 265 
3be69 2a 2f 20 22 70 6c 75 73 5f 6e 75 6d 20 3a 3a 3d  */ "plus_num ::=
3be6a 20 6e 75 6d 62 65 72 22 2c 0a 20 2f 2a 20 32 36   number",. /* 26
3be6b 36 20 2a 2f 20 22 6d 69 6e 75 73 5f 6e 75 6d 20  6 */ "minus_num 
3be6c 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d 62 65 72  ::= MINUS number
3be6d 22 2c 0a 20 2f 2a 20 32 36 37 20 2a 2f 20 22 6e  ",. /* 267 */ "n
3be6e 75 6d 62 65 72 20 3a 3a 3d 20 49 4e 54 45 47 45  umber ::= INTEGE
3be6f 52 7c 46 4c 4f 41 54 22 2c 0a 20 2f 2a 20 32 36  R|FLOAT",. /* 26
3be70 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72  8 */ "cmd ::= cr
3be71 65 61 74 65 6b 77 20 74 72 69 67 67 65 72 5f 64  eatekw trigger_d
3be72 65 63 6c 20 42 45 47 49 4e 20 74 72 69 67 67 65  ecl BEGIN trigge
3be73 72 5f 63 6d 64 5f 6c 69 73 74 20 45 4e 44 22 2c  r_cmd_list END",
3be74 0a 20 2f 2a 20 32 36 39 20 2a 2f 20 22 74 72 69  . /* 269 */ "tri
3be75 67 67 65 72 5f 64 65 63 6c 20 3a 3a 3d 20 74 65  gger_decl ::= te
3be76 6d 70 20 54 52 49 47 47 45 52 20 69 66 6e 6f 74  mp TRIGGER ifnot
3be77 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 74  exists nm dbnm t
3be78 72 69 67 67 65 72 5f 74 69 6d 65 20 74 72 69 67  rigger_time trig
3be79 67 65 72 5f 65 76 65 6e 74 20 4f 4e 20 66 75 6c  ger_event ON ful
3be7a 6c 6e 61 6d 65 20 66 6f 72 65 61 63 68 5f 63 6c  lname foreach_cl
3be7b 61 75 73 65 20 77 68 65 6e 5f 63 6c 61 75 73 65  ause when_clause
3be7c 22 2c 0a 20 2f 2a 20 32 37 30 20 2a 2f 20 22 74  ",. /* 270 */ "t
3be7d 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20  rigger_time ::= 
3be7e 42 45 46 4f 52 45 22 2c 0a 20 2f 2a 20 32 37 31  BEFORE",. /* 271
3be7f 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74 69 6d   */ "trigger_tim
3be80 65 20 3a 3a 3d 20 41 46 54 45 52 22 2c 0a 20 2f  e ::= AFTER",. /
3be81 2a 20 32 37 32 20 2a 2f 20 22 74 72 69 67 67 65  * 272 */ "trigge
3be82 72 5f 74 69 6d 65 20 3a 3a 3d 20 49 4e 53 54 45  r_time ::= INSTE
3be83 41 44 20 4f 46 22 2c 0a 20 2f 2a 20 32 37 33 20  AD OF",. /* 273 
3be84 2a 2f 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65  */ "trigger_time
3be85 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 37 34 20 2a   ::=",. /* 274 *
3be86 2f 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e 74  / "trigger_event
3be87 20 3a 3a 3d 20 44 45 4c 45 54 45 7c 49 4e 53 45   ::= DELETE|INSE
3be88 52 54 22 2c 0a 20 2f 2a 20 32 37 35 20 2a 2f 20  RT",. /* 275 */ 
3be89 22 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a  "trigger_event :
3be8a 3a 3d 20 55 50 44 41 54 45 22 2c 0a 20 2f 2a 20  := UPDATE",. /* 
3be8b 32 37 36 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  276 */ "trigger_
3be8c 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45  event ::= UPDATE
3be8d 20 4f 46 20 69 6e 73 63 6f 6c 6c 69 73 74 22 2c   OF inscollist",
3be8e 0a 20 2f 2a 20 32 37 37 20 2a 2f 20 22 66 6f 72  . /* 277 */ "for
3be8f 65 61 63 68 5f 63 6c 61 75 73 65 20 3a 3a 3d 22  each_clause ::="
3be90 2c 0a 20 2f 2a 20 32 37 38 20 2a 2f 20 22 66 6f  ,. /* 278 */ "fo
3be91 72 65 61 63 68 5f 63 6c 61 75 73 65 20 3a 3a 3d  reach_clause ::=
3be92 20 46 4f 52 20 45 41 43 48 20 52 4f 57 22 2c 0a   FOR EACH ROW",.
3be93 20 2f 2a 20 32 37 39 20 2a 2f 20 22 77 68 65 6e   /* 279 */ "when
3be94 5f 63 6c 61 75 73 65 20 3a 3a 3d 22 2c 0a 20 2f  _clause ::=",. /
3be95 2a 20 32 38 30 20 2a 2f 20 22 77 68 65 6e 5f 63  * 280 */ "when_c
3be96 6c 61 75 73 65 20 3a 3a 3d 20 57 48 45 4e 20 65  lause ::= WHEN e
3be97 78 70 72 22 2c 0a 20 2f 2a 20 32 38 31 20 2a 2f  xpr",. /* 281 */
3be98 20 22 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69   "trigger_cmd_li
3be99 73 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63  st ::= trigger_c
3be9a 6d 64 5f 6c 69 73 74 20 74 72 69 67 67 65 72 5f  md_list trigger_
3be9b 63 6d 64 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 32  cmd SEMI",. /* 2
3be9c 38 32 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63  82 */ "trigger_c
3be9d 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69 67  md_list ::= trig
3be9e 67 65 72 5f 63 6d 64 20 53 45 4d 49 22 2c 0a 20  ger_cmd SEMI",. 
3be9f 2f 2a 20 32 38 33 20 2a 2f 20 22 74 72 6e 6d 20  /* 283 */ "trnm 
3bea0 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 32 38 34  ::= nm",. /* 284
3bea1 20 2a 2f 20 22 74 72 6e 6d 20 3a 3a 3d 20 6e 6d   */ "trnm ::= nm
3bea2 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 32 38   DOT nm",. /* 28
3bea3 35 20 2a 2f 20 22 74 72 69 64 78 62 79 20 3a 3a  5 */ "tridxby ::
3bea4 3d 22 2c 0a 20 2f 2a 20 32 38 36 20 2a 2f 20 22  =",. /* 286 */ "
3bea5 74 72 69 64 78 62 79 20 3a 3a 3d 20 49 4e 44 45  tridxby ::= INDE
3bea6 58 45 44 20 42 59 20 6e 6d 22 2c 0a 20 2f 2a 20  XED BY nm",. /* 
3bea7 32 38 37 20 2a 2f 20 22 74 72 69 64 78 62 79 20  287 */ "tridxby 
3bea8 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58 45 44 22  ::= NOT INDEXED"
3bea9 2c 0a 20 2f 2a 20 32 38 38 20 2a 2f 20 22 74 72  ,. /* 288 */ "tr
3beaa 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 55 50  igger_cmd ::= UP
3beab 44 41 54 45 20 6f 72 63 6f 6e 66 20 74 72 6e 6d  DATE orconf trnm
3beac 20 74 72 69 64 78 62 79 20 53 45 54 20 73 65 74   tridxby SET set
3bead 6c 69 73 74 20 77 68 65 72 65 5f 6f 70 74 22 2c  list where_opt",
3beae 0a 20 2f 2a 20 32 38 39 20 2a 2f 20 22 74 72 69  . /* 289 */ "tri
3beaf 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73  gger_cmd ::= ins
3beb0 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 74 72 6e  ert_cmd INTO trn
3beb1 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74  m inscollist_opt
3beb2 20 76 61 6c 75 65 6c 69 73 74 22 2c 0a 20 2f 2a   valuelist",. /*
3beb3 20 32 39 30 20 2a 2f 20 22 74 72 69 67 67 65 72   290 */ "trigger
3beb4 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f  _cmd ::= insert_
3beb5 63 6d 64 20 49 4e 54 4f 20 74 72 6e 6d 20 69 6e  cmd INTO trnm in
3beb6 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c  scollist_opt sel
3beb7 65 63 74 22 2c 0a 20 2f 2a 20 32 39 31 20 2a 2f  ect",. /* 291 */
3beb8 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a   "trigger_cmd ::
3beb9 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 72  = DELETE FROM tr
3beba 6e 6d 20 74 72 69 64 78 62 79 20 77 68 65 72 65  nm tridxby where
3bebb 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32 39 32 20 2a  _opt",. /* 292 *
3bebc 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a  / "trigger_cmd :
3bebd 3a 3d 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20  := select",. /* 
3bebe 32 39 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  293 */ "expr ::=
3bebf 20 52 41 49 53 45 20 4c 50 20 49 47 4e 4f 52 45   RAISE LP IGNORE
3bec0 20 52 50 22 2c 0a 20 2f 2a 20 32 39 34 20 2a 2f   RP",. /* 294 */
3bec1 20 22 65 78 70 72 20 3a 3a 3d 20 52 41 49 53 45   "expr ::= RAISE
3bec2 20 4c 50 20 72 61 69 73 65 74 79 70 65 20 43 4f   LP raisetype CO
3bec3 4d 4d 41 20 6e 6d 20 52 50 22 2c 0a 20 2f 2a 20  MMA nm RP",. /* 
3bec4 32 39 35 20 2a 2f 20 22 72 61 69 73 65 74 79 70  295 */ "raisetyp
3bec5 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 22 2c  e ::= ROLLBACK",
3bec6 0a 20 2f 2a 20 32 39 36 20 2a 2f 20 22 72 61 69  . /* 296 */ "rai
3bec7 73 65 74 79 70 65 20 3a 3a 3d 20 41 42 4f 52 54  setype ::= ABORT
3bec8 22 2c 0a 20 2f 2a 20 32 39 37 20 2a 2f 20 22 72  ",. /* 297 */ "r
3bec9 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 46 41 49  aisetype ::= FAI
3beca 4c 22 2c 0a 20 2f 2a 20 32 39 38 20 2a 2f 20 22  L",. /* 298 */ "
3becb 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 52 49  cmd ::= DROP TRI
3becc 47 47 45 52 20 69 66 65 78 69 73 74 73 20 66 75  GGER ifexists fu
3becd 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 32 39 39  llname",. /* 299
3bece 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 54 54   */ "cmd ::= ATT
3becf 41 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77 5f  ACH database_kw_
3bed0 6f 70 74 20 65 78 70 72 20 41 53 20 65 78 70 72  opt expr AS expr
3bed1 20 6b 65 79 5f 6f 70 74 22 2c 0a 20 2f 2a 20 33   key_opt",. /* 3
3bed2 30 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44  00 */ "cmd ::= D
3bed3 45 54 41 43 48 20 64 61 74 61 62 61 73 65 5f 6b  ETACH database_k
3bed4 77 5f 6f 70 74 20 65 78 70 72 22 2c 0a 20 2f 2a  w_opt expr",. /*
3bed5 20 33 30 31 20 2a 2f 20 22 6b 65 79 5f 6f 70 74   301 */ "key_opt
3bed6 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 30 32 20 2a   ::=",. /* 302 *
3bed7 2f 20 22 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 4b  / "key_opt ::= K
3bed8 45 59 20 65 78 70 72 22 2c 0a 20 2f 2a 20 33 30  EY expr",. /* 30
3bed9 33 20 2a 2f 20 22 64 61 74 61 62 61 73 65 5f 6b  3 */ "database_k
3beda 77 5f 6f 70 74 20 3a 3a 3d 20 44 41 54 41 42 41  w_opt ::= DATABA
3bedb 53 45 22 2c 0a 20 2f 2a 20 33 30 34 20 2a 2f 20  SE",. /* 304 */ 
3bedc 22 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74  "database_kw_opt
3bedd 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 30 35 20 2a   ::=",. /* 305 *
3bede 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44  / "cmd ::= REIND
3bedf 45 58 22 2c 0a 20 2f 2a 20 33 30 36 20 2a 2f 20  EX",. /* 306 */ 
3bee0 22 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58  "cmd ::= REINDEX
3bee1 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 33   nm dbnm",. /* 3
3bee2 30 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41  07 */ "cmd ::= A
3bee3 4e 41 4c 59 5a 45 22 2c 0a 20 2f 2a 20 33 30 38  NALYZE",. /* 308
3bee4 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4e 41   */ "cmd ::= ANA
3bee5 4c 59 5a 45 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20  LYZE nm dbnm",. 
3bee6 2f 2a 20 33 30 39 20 2a 2f 20 22 63 6d 64 20 3a  /* 309 */ "cmd :
3bee7 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 66  := ALTER TABLE f
3bee8 75 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d 45 20 54  ullname RENAME T
3bee9 4f 20 6e 6d 22 2c 0a 20 2f 2a 20 33 31 30 20 2a  O nm",. /* 310 *
3beea 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52  / "cmd ::= ALTER
3beeb 20 54 41 42 4c 45 20 61 64 64 5f 63 6f 6c 75 6d   TABLE add_colum
3beec 6e 5f 66 75 6c 6c 6e 61 6d 65 20 41 44 44 20 6b  n_fullname ADD k
3beed 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 63 6f 6c 75  wcolumn_opt colu
3beee 6d 6e 22 2c 0a 20 2f 2a 20 33 31 31 20 2a 2f 20  mn",. /* 311 */ 
3beef 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c  "add_column_full
3bef0 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e 61 6d  name ::= fullnam
3bef1 65 22 2c 0a 20 2f 2a 20 33 31 32 20 2a 2f 20 22  e",. /* 312 */ "
3bef2 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d  kwcolumn_opt ::=
3bef3 22 2c 0a 20 2f 2a 20 33 31 33 20 2a 2f 20 22 6b  ",. /* 313 */ "k
3bef4 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 20  wcolumn_opt ::= 
3bef5 43 4f 4c 55 4d 4e 4b 57 22 2c 0a 20 2f 2a 20 33  COLUMNKW",. /* 3
3bef6 31 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63  14 */ "cmd ::= c
3bef7 72 65 61 74 65 5f 76 74 61 62 22 2c 0a 20 2f 2a  reate_vtab",. /*
3bef8 20 33 31 35 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   315 */ "cmd ::=
3bef9 20 63 72 65 61 74 65 5f 76 74 61 62 20 4c 50 20   create_vtab LP 
3befa 76 74 61 62 61 72 67 6c 69 73 74 20 52 50 22 2c  vtabarglist RP",
3befb 0a 20 2f 2a 20 33 31 36 20 2a 2f 20 22 63 72 65  . /* 316 */ "cre
3befc 61 74 65 5f 76 74 61 62 20 3a 3a 3d 20 63 72 65  ate_vtab ::= cre
3befd 61 74 65 6b 77 20 56 49 52 54 55 41 4c 20 54 41  atekw VIRTUAL TA
3befe 42 4c 45 20 69 66 6e 6f 74 65 78 69 73 74 73 20  BLE ifnotexists 
3beff 6e 6d 20 64 62 6e 6d 20 55 53 49 4e 47 20 6e 6d  nm dbnm USING nm
3bf00 22 2c 0a 20 2f 2a 20 33 31 37 20 2a 2f 20 22 76  ",. /* 317 */ "v
3bf01 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76  tabarglist ::= v
3bf02 74 61 62 61 72 67 22 2c 0a 20 2f 2a 20 33 31 38  tabarg",. /* 318
3bf03 20 2a 2f 20 22 76 74 61 62 61 72 67 6c 69 73 74   */ "vtabarglist
3bf04 20 3a 3a 3d 20 76 74 61 62 61 72 67 6c 69 73 74   ::= vtabarglist
3bf05 20 43 4f 4d 4d 41 20 76 74 61 62 61 72 67 22 2c   COMMA vtabarg",
3bf06 0a 20 2f 2a 20 33 31 39 20 2a 2f 20 22 76 74 61  . /* 319 */ "vta
3bf07 62 61 72 67 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33  barg ::=",. /* 3
3bf08 32 30 20 2a 2f 20 22 76 74 61 62 61 72 67 20 3a  20 */ "vtabarg :
3bf09 3a 3d 20 76 74 61 62 61 72 67 20 76 74 61 62 61  := vtabarg vtaba
3bf0a 72 67 74 6f 6b 65 6e 22 2c 0a 20 2f 2a 20 33 32  rgtoken",. /* 32
3bf0b 31 20 2a 2f 20 22 76 74 61 62 61 72 67 74 6f 6b  1 */ "vtabargtok
3bf0c 65 6e 20 3a 3a 3d 20 41 4e 59 22 2c 0a 20 2f 2a  en ::= ANY",. /*
3bf0d 20 33 32 32 20 2a 2f 20 22 76 74 61 62 61 72 67   322 */ "vtabarg
3bf0e 74 6f 6b 65 6e 20 3a 3a 3d 20 6c 70 20 61 6e 79  token ::= lp any
3bf0f 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 33 32  list RP",. /* 32
3bf10 33 20 2a 2f 20 22 6c 70 20 3a 3a 3d 20 4c 50 22  3 */ "lp ::= LP"
3bf11 2c 0a 20 2f 2a 20 33 32 34 20 2a 2f 20 22 61 6e  ,. /* 324 */ "an
3bf12 79 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  ylist ::=",. /* 
3bf13 33 32 35 20 2a 2f 20 22 61 6e 79 6c 69 73 74 20  325 */ "anylist 
3bf14 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 4c 50 20 61  ::= anylist LP a
3bf15 6e 79 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20  nylist RP",. /* 
3bf16 33 32 36 20 2a 2f 20 22 61 6e 79 6c 69 73 74 20  326 */ "anylist 
3bf17 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 41 4e 59 22  ::= anylist ANY"
3bf18 2c 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 4e  ,.};.#endif /* N
3bf19 44 45 42 55 47 20 2a 2f 0a 0a 0a 23 69 66 20 59  DEBUG */...#if Y
3bf1a 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 2f  YSTACKDEPTH<=0./
3bf1b 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 69 6e 63 72  *.** Try to incr
3bf1c 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66  ease the size of
3bf1d 20 74 68 65 20 70 61 72 73 65 72 20 73 74 61 63   the parser stac
3bf1e 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  k..*/.static voi
3bf1f 64 20 79 79 47 72 6f 77 53 74 61 63 6b 28 79 79  d yyGrowStack(yy
3bf20 50 61 72 73 65 72 20 2a 70 29 7b 0a 20 20 69 6e  Parser *p){.  in
3bf21 74 20 6e 65 77 53 69 7a 65 3b 0a 20 20 79 79 53  t newSize;.  yyS
3bf22 74 61 63 6b 45 6e 74 72 79 20 2a 70 4e 65 77 3b  tackEntry *pNew;
3bf23 0a 0a 20 20 6e 65 77 53 69 7a 65 20 3d 20 70 2d  ..  newSize = p-
3bf24 3e 79 79 73 74 6b 73 7a 2a 32 20 2b 20 31 30 30  >yystksz*2 + 100
3bf25 3b 0a 20 20 70 4e 65 77 20 3d 20 72 65 61 6c 6c  ;.  pNew = reall
3bf26 6f 63 28 70 2d 3e 79 79 73 74 61 63 6b 2c 20 6e  oc(p->yystack, n
3bf27 65 77 53 69 7a 65 2a 73 69 7a 65 6f 66 28 70 4e  ewSize*sizeof(pN
3bf28 65 77 5b 30 5d 29 29 3b 0a 20 20 69 66 28 20 70  ew[0]));.  if( p
3bf29 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d 3e 79 79  New ){.    p->yy
3bf2a 73 74 61 63 6b 20 3d 20 70 4e 65 77 3b 0a 20 20  stack = pNew;.  
3bf2b 20 20 70 2d 3e 79 79 73 74 6b 73 7a 20 3d 20 6e    p->yystksz = n
3bf2c 65 77 53 69 7a 65 3b 0a 23 69 66 6e 64 65 66 20  ewSize;.#ifndef 
3bf2d 4e 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 79  NDEBUG.    if( y
3bf2e 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20  yTraceFILE ){.  
3bf2f 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
3bf30 61 63 65 46 49 4c 45 2c 22 25 73 53 74 61 63 6b  aceFILE,"%sStack
3bf31 20 67 72 6f 77 73 20 74 6f 20 25 64 20 65 6e 74   grows to %d ent
3bf32 72 69 65 73 21 5c 6e 22 2c 0a 20 20 20 20 20 20  ries!\n",.      
3bf33 20 20 20 20 20 20 20 20 79 79 54 72 61 63 65 50          yyTraceP
3bf34 72 6f 6d 70 74 2c 20 70 2d 3e 79 79 73 74 6b 73  rompt, p->yystks
3bf35 7a 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  z);.    }.#endif
3bf36 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  .  }.}.#endif../
3bf37 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  * .** This funct
3bf38 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 20  ion allocates a 
3bf39 6e 65 77 20 70 61 72 73 65 72 2e 0a 2a 2a 20 54  new parser..** T
3bf3a 68 65 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e 74  he only argument
3bf3b 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
3bf3c 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 68 69 63   a function whic
3bf3d 68 20 77 6f 72 6b 73 20 6c 69 6b 65 0a 2a 2a 20  h works like.** 
3bf3e 6d 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 49 6e  malloc..**.** In
3bf3f 70 75 74 73 3a 0a 2a 2a 20 41 20 70 6f 69 6e 74  puts:.** A point
3bf40 65 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  er to the functi
3bf41 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63  on used to alloc
3bf42 61 74 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a  ate memory..**.*
3bf43 2a 20 4f 75 74 70 75 74 73 3a 0a 2a 2a 20 41 20  * Outputs:.** A 
3bf44 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 61 72  pointer to a par
3bf45 73 65 72 2e 20 20 54 68 69 73 20 70 6f 69 6e 74  ser.  This point
3bf46 65 72 20 69 73 20 75 73 65 64 20 69 6e 20 73 75  er is used in su
3bf47 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 0a 2a  bsequent calls.*
3bf48 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 72 73  * to sqlite3Pars
3bf49 65 72 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61  er and sqlite3Pa
3bf4a 72 73 65 72 46 72 65 65 2e 0a 2a 2f 0a 53 51 4c  rserFree..*/.SQL
3bf4b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
3bf4c 20 2a 73 71 6c 69 74 65 33 50 61 72 73 65 72 41   *sqlite3ParserA
3bf4d 6c 6c 6f 63 28 76 6f 69 64 20 2a 28 2a 6d 61 6c  lloc(void *(*mal
3bf4e 6c 6f 63 50 72 6f 63 29 28 73 69 7a 65 5f 74 29  locProc)(size_t)
3bf4f 29 7b 0a 20 20 79 79 50 61 72 73 65 72 20 2a 70  ){.  yyParser *p
3bf50 50 61 72 73 65 72 3b 0a 20 20 70 50 61 72 73 65  Parser;.  pParse
3bf51 72 20 3d 20 28 79 79 50 61 72 73 65 72 2a 29 28  r = (yyParser*)(
3bf52 2a 6d 61 6c 6c 6f 63 50 72 6f 63 29 28 20 28 73  *mallocProc)( (s
3bf53 69 7a 65 5f 74 29 73 69 7a 65 6f 66 28 79 79 50  ize_t)sizeof(yyP
3bf54 61 72 73 65 72 29 20 29 3b 0a 20 20 69 66 28 20  arser) );.  if( 
3bf55 70 50 61 72 73 65 72 20 29 7b 0a 20 20 20 20 70  pParser ){.    p
3bf56 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3d 20  Parser->yyidx = 
3bf57 2d 31 3b 0a 23 69 66 64 65 66 20 59 59 54 52 41  -1;.#ifdef YYTRA
3bf58 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0a  CKMAXSTACKDEPTH.
3bf59 20 20 20 20 70 50 61 72 73 65 72 2d 3e 79 79 69      pParser->yyi
3bf5a 64 78 4d 61 78 20 3d 20 30 3b 0a 23 65 6e 64 69  dxMax = 0;.#endi
3bf5b 66 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50  f.#if YYSTACKDEP
3bf5c 54 48 3c 3d 30 0a 20 20 20 20 70 50 61 72 73 65  TH<=0.    pParse
3bf5d 72 2d 3e 79 79 73 74 61 63 6b 20 3d 20 4e 55 4c  r->yystack = NUL
3bf5e 4c 3b 0a 20 20 20 20 70 50 61 72 73 65 72 2d 3e  L;.    pParser->
3bf5f 79 79 73 74 6b 73 7a 20 3d 20 30 3b 0a 20 20 20  yystksz = 0;.   
3bf60 20 79 79 47 72 6f 77 53 74 61 63 6b 28 70 50 61   yyGrowStack(pPa
3bf61 72 73 65 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20  rser);.#endif.  
3bf62 7d 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72 73  }.  return pPars
3bf63 65 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f  er;.}../* The fo
3bf64 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llowing function
3bf65 20 64 65 6c 65 74 65 73 20 74 68 65 20 76 61 6c   deletes the val
3bf66 75 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ue associated wi
3bf67 74 68 20 61 0a 2a 2a 20 73 79 6d 62 6f 6c 2e 20  th a.** symbol. 
3bf68 20 54 68 65 20 73 79 6d 62 6f 6c 20 63 61 6e 20   The symbol can 
3bf69 62 65 20 65 69 74 68 65 72 20 61 20 74 65 72 6d  be either a term
3bf6a 69 6e 61 6c 20 6f 72 20 6e 6f 6e 74 65 72 6d 69  inal or nontermi
3bf6b 6e 61 6c 2e 0a 2a 2a 20 22 79 79 6d 61 6a 6f 72  nal..** "yymajor
3bf6c 22 20 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 20  " is the symbol 
3bf6d 63 6f 64 65 2c 20 61 6e 64 20 22 79 79 70 6d 69  code, and "yypmi
3bf6e 6e 6f 72 22 20 69 73 20 61 20 70 6f 69 6e 74 65  nor" is a pointe
3bf6f 72 20 74 6f 0a 2a 2a 20 74 68 65 20 76 61 6c 75  r to.** the valu
3bf70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
3bf71 64 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28  d yy_destructor(
3bf72 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70  .  yyParser *yyp
3bf73 50 61 72 73 65 72 2c 20 20 20 20 2f 2a 20 54 68  Parser,    /* Th
3bf74 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 59 59  e parser */.  YY
3bf75 43 4f 44 45 54 59 50 45 20 79 79 6d 61 6a 6f 72  CODETYPE yymajor
3bf76 2c 20 20 20 20 20 2f 2a 20 54 79 70 65 20 63 6f  ,     /* Type co
3bf77 64 65 20 66 6f 72 20 6f 62 6a 65 63 74 20 74 6f  de for object to
3bf78 20 64 65 73 74 72 6f 79 20 2a 2f 0a 20 20 59 59   destroy */.  YY
3bf79 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79 70 6d 69  MINORTYPE *yypmi
3bf7a 6e 6f 72 20 20 20 2f 2a 20 54 68 65 20 6f 62 6a  nor   /* The obj
3bf7b 65 63 74 20 74 6f 20 62 65 20 64 65 73 74 72 6f  ect to be destro
3bf7c 79 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  yed */.){.  sqli
3bf7d 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54  te3ParserARG_FET
3bf7e 43 48 3b 0a 20 20 73 77 69 74 63 68 28 20 79 79  CH;.  switch( yy
3bf7f 6d 61 6a 6f 72 20 29 7b 0a 20 20 20 20 2f 2a 20  major ){.    /* 
3bf80 48 65 72 65 20 69 73 20 69 6e 73 65 72 74 65 64  Here is inserted
3bf81 20 74 68 65 20 61 63 74 69 6f 6e 73 20 77 68 69   the actions whi
3bf82 63 68 20 74 61 6b 65 20 70 6c 61 63 65 20 77 68  ch take place wh
3bf83 65 6e 20 61 0a 20 20 20 20 2a 2a 20 74 65 72 6d  en a.    ** term
3bf84 69 6e 61 6c 20 6f 72 20 6e 6f 6e 2d 74 65 72 6d  inal or non-term
3bf85 69 6e 61 6c 20 69 73 20 64 65 73 74 72 6f 79 65  inal is destroye
3bf86 64 2e 20 20 54 68 69 73 20 63 61 6e 20 68 61 70  d.  This can hap
3bf87 70 65 6e 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20  pen.    ** when 
3bf88 74 68 65 20 73 79 6d 62 6f 6c 20 69 73 20 70 6f  the symbol is po
3bf89 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73 74  pped from the st
3bf8a 61 63 6b 20 64 75 72 69 6e 67 20 61 0a 20 20 20  ack during a.   
3bf8b 20 2a 2a 20 72 65 64 75 63 65 20 6f 72 20 64 75   ** reduce or du
3bf8c 72 69 6e 67 20 65 72 72 6f 72 20 70 72 6f 63 65  ring error proce
3bf8d 73 73 69 6e 67 20 6f 72 20 77 68 65 6e 20 61 20  ssing or when a 
3bf8e 70 61 72 73 65 72 20 69 73 20 0a 20 20 20 20 2a  parser is .    *
3bf8f 2a 20 62 65 69 6e 67 20 64 65 73 74 72 6f 79 65  * being destroye
3bf90 64 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 66  d before it is f
3bf91 69 6e 69 73 68 65 64 20 70 61 72 73 69 6e 67 2e  inished parsing.
3bf92 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e  .    **.    ** N
3bf93 6f 74 65 3a 20 64 75 72 69 6e 67 20 61 20 72 65  ote: during a re
3bf94 64 75 63 65 2c 20 74 68 65 20 6f 6e 6c 79 20 73  duce, the only s
3bf95 79 6d 62 6f 6c 73 20 64 65 73 74 72 6f 79 65 64  ymbols destroyed
3bf96 20 61 72 65 20 74 68 6f 73 65 0a 20 20 20 20 2a   are those.    *
3bf97 2a 20 77 68 69 63 68 20 61 70 70 65 61 72 20 6f  * which appear o
3bf98 6e 20 74 68 65 20 52 48 53 20 6f 66 20 74 68 65  n the RHS of the
3bf99 20 72 75 6c 65 2c 20 62 75 74 20 77 68 69 63 68   rule, but which
3bf9a 20 61 72 65 20 6e 6f 74 20 75 73 65 64 0a 20 20   are not used.  
3bf9b 20 20 2a 2a 20 69 6e 73 69 64 65 20 74 68 65 20    ** inside the 
3bf9c 43 20 63 6f 64 65 2e 0a 20 20 20 20 2a 2f 0a 20  C code..    */. 
3bf9d 20 20 20 63 61 73 65 20 31 36 30 3a 20 2f 2a 20     case 160: /* 
3bf9e 73 65 6c 65 63 74 20 2a 2f 0a 20 20 20 20 63 61  select */.    ca
3bf9f 73 65 20 31 39 34 3a 20 2f 2a 20 6f 6e 65 73 65  se 194: /* onese
3bfa0 6c 65 63 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65  lect */.{.sqlite
3bfa1 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50  3SelectDelete(pP
3bfa2 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69  arse->db, (yypmi
3bfa3 6e 6f 72 2d 3e 79 79 31 35 39 29 29 3b 0a 7d 0a  nor->yy159));.}.
3bfa4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3bfa5 20 63 61 73 65 20 31 37 33 3a 20 2f 2a 20 74 65   case 173: /* te
3bfa6 72 6d 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31  rm */.    case 1
3bfa7 37 34 3a 20 2f 2a 20 65 78 70 72 20 2a 2f 0a 7b  74: /* expr */.{
3bfa8 0a 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65  .sqlite3ExprDele
3bfa9 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28  te(pParse->db, (
3bfaa 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 34 32 29  yypminor->yy342)
3bfab 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20 20  .pExpr);.}.     
3bfac 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
3bfad 20 31 37 38 3a 20 2f 2a 20 69 64 78 6c 69 73 74   178: /* idxlist
3bfae 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  _opt */.    case
3bfaf 20 31 38 37 3a 20 2f 2a 20 69 64 78 6c 69 73 74   187: /* idxlist
3bfb0 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39 37   */.    case 197
3bfb1 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20  : /* selcollist 
3bfb2 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30 30 3a  */.    case 200:
3bfb3 20 2f 2a 20 67 72 6f 75 70 62 79 5f 6f 70 74 20   /* groupby_opt 
3bfb4 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30 32 3a  */.    case 202:
3bfb5 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70 74 20   /* orderby_opt 
3bfb6 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30 34 3a  */.    case 204:
3bfb7 20 2f 2a 20 73 63 6c 70 20 2a 2f 0a 20 20 20 20   /* sclp */.    
3bfb8 63 61 73 65 20 32 31 34 3a 20 2f 2a 20 73 6f 72  case 214: /* sor
3bfb9 74 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73  tlist */.    cas
3bfba 65 20 32 31 35 3a 20 2f 2a 20 6e 65 78 70 72 6c  e 215: /* nexprl
3bfbb 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ist */.    case 
3bfbc 32 31 36 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20  216: /* setlist 
3bfbd 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32 30 3a  */.    case 220:
3bfbe 20 2f 2a 20 65 78 70 72 6c 69 73 74 20 2a 2f 0a   /* exprlist */.
3bfbf 20 20 20 20 63 61 73 65 20 32 32 35 3a 20 2f 2a      case 225: /*
3bfc0 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 2a   case_exprlist *
3bfc1 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 4c  /.{.sqlite3ExprL
3bfc2 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  istDelete(pParse
3bfc3 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d  ->db, (yypminor-
3bfc4 3e 79 79 34 34 32 29 29 3b 0a 7d 0a 20 20 20 20  >yy442));.}.    
3bfc5 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
3bfc6 65 20 31 39 33 3a 20 2f 2a 20 66 75 6c 6c 6e 61  e 193: /* fullna
3bfc7 6d 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31  me */.    case 1
3bfc8 39 38 3a 20 2f 2a 20 66 72 6f 6d 20 2a 2f 0a 20  98: /* from */. 
3bfc9 20 20 20 63 61 73 65 20 32 30 36 3a 20 2f 2a 20     case 206: /* 
3bfca 73 65 6c 74 61 62 6c 69 73 74 20 2a 2f 0a 20 20  seltablist */.  
3bfcb 20 20 63 61 73 65 20 32 30 37 3a 20 2f 2a 20 73    case 207: /* s
3bfcc 74 6c 5f 70 72 65 66 69 78 20 2a 2f 0a 7b 0a 73  tl_prefix */.{.s
3bfcd 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
3bfce 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
3bfcf 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 34 37  (yypminor->yy347
3bfd0 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61  ));.}.      brea
3bfd1 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 39 39 3a  k;.    case 199:
3bfd2 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f   /* where_opt */
3bfd3 0a 20 20 20 20 63 61 73 65 20 32 30 31 3a 20 2f  .    case 201: /
3bfd4 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20 2a 2f 0a  * having_opt */.
3bfd5 20 20 20 20 63 61 73 65 20 32 31 30 3a 20 2f 2a      case 210: /*
3bfd6 20 6f 6e 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63   on_opt */.    c
3bfd7 61 73 65 20 32 32 34 3a 20 2f 2a 20 63 61 73 65  ase 224: /* case
3bfd8 5f 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20  _operand */.    
3bfd9 63 61 73 65 20 32 32 36 3a 20 2f 2a 20 63 61 73  case 226: /* cas
3bfda 65 5f 65 6c 73 65 20 2a 2f 0a 20 20 20 20 63 61  e_else */.    ca
3bfdb 73 65 20 32 33 36 3a 20 2f 2a 20 77 68 65 6e 5f  se 236: /* when_
3bfdc 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 63 61  clause */.    ca
3bfdd 73 65 20 32 34 31 3a 20 2f 2a 20 6b 65 79 5f 6f  se 241: /* key_o
3bfde 70 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 45  pt */.{.sqlite3E
3bfdf 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65  xprDelete(pParse
3bfe0 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d  ->db, (yypminor-
3bfe1 3e 79 79 31 32 32 29 29 3b 0a 7d 0a 20 20 20 20  >yy122));.}.    
3bfe2 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
3bfe3 65 20 32 31 31 3a 20 2f 2a 20 75 73 69 6e 67 5f  e 211: /* using_
3bfe4 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  opt */.    case 
3bfe5 32 31 33 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69  213: /* inscolli
3bfe6 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  st */.    case 2
3bfe7 31 38 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73  18: /* inscollis
3bfe8 74 5f 6f 70 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74  t_opt */.{.sqlit
3bfe9 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 70  e3IdListDelete(p
3bfea 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d  Parse->db, (yypm
3bfeb 69 6e 6f 72 2d 3e 79 79 31 38 30 29 29 3b 0a 7d  inor->yy180));.}
3bfec 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
3bfed 20 20 63 61 73 65 20 32 31 39 3a 20 2f 2a 20 76    case 219: /* v
3bfee 61 6c 75 65 6c 69 73 74 20 2a 2f 0a 7b 0a 0a 20  aluelist */.{.. 
3bfef 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
3bff0 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
3bff1 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79  b, (yypminor->yy
3bff2 34 38 37 29 2e 70 4c 69 73 74 29 3b 0a 20 20 73  487).pList);.  s
3bff3 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
3bff4 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28  te(pParse->db, (
3bff5 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 34 38 37 29  yypminor->yy487)
3bff6 2e 70 53 65 6c 65 63 74 29 3b 0a 0a 7d 0a 20 20  .pSelect);..}.  
3bff7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
3bff8 61 73 65 20 32 33 32 3a 20 2f 2a 20 74 72 69 67  ase 232: /* trig
3bff9 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 2a 2f 0a  ger_cmd_list */.
3bffa 20 20 20 20 63 61 73 65 20 32 33 37 3a 20 2f 2a      case 237: /*
3bffb 20 74 72 69 67 67 65 72 5f 63 6d 64 20 2a 2f 0a   trigger_cmd */.
3bffc 7b 0a 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54  {.sqlite3DeleteT
3bffd 72 69 67 67 65 72 53 74 65 70 28 70 50 61 72 73  riggerStep(pPars
3bffe 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72  e->db, (yypminor
3bfff 2d 3e 79 79 33 32 37 29 29 3b 0a 7d 0a 20 20 20  ->yy327));.}.   
3c000 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
3c001 73 65 20 32 33 34 3a 20 2f 2a 20 74 72 69 67 67  se 234: /* trigg
3c002 65 72 5f 65 76 65 6e 74 20 2a 2f 0a 7b 0a 73 71  er_event */.{.sq
3c003 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
3c004 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79  e(pParse->db, (y
3c005 79 70 6d 69 6e 6f 72 2d 3e 79 79 34 31 30 29 2e  ypminor->yy410).
3c006 62 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61  b);.}.      brea
3c007 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  k;.    default: 
3c008 20 62 72 65 61 6b 3b 20 20 20 2f 2a 20 49 66 20   break;   /* If 
3c009 6e 6f 20 64 65 73 74 72 75 63 74 6f 72 20 61 63  no destructor ac
3c00a 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 3a 20  tion specified: 
3c00b 64 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  do nothing */.  
3c00c 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 74  }.}../*.** Pop t
3c00d 68 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63  he parser's stac
3c00e 6b 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  k once..**.** If
3c00f 20 74 68 65 72 65 20 69 73 20 61 20 64 65 73 74   there is a dest
3c010 72 75 63 74 6f 72 20 72 6f 75 74 69 6e 65 20 61  ructor routine a
3c011 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
3c012 68 65 20 74 6f 6b 65 6e 20 77 68 69 63 68 0a 2a  he token which.*
3c013 2a 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d  * is popped from
3c014 20 74 68 65 20 73 74 61 63 6b 2c 20 74 68 65 6e   the stack, then
3c015 20 63 61 6c 6c 20 69 74 2e 0a 2a 2a 0a 2a 2a 20   call it..**.** 
3c016 52 65 74 75 72 6e 20 74 68 65 20 6d 61 6a 6f 72  Return the major
3c017 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 66 6f   token number fo
3c018 72 20 74 68 65 20 73 79 6d 62 6f 6c 20 70 6f 70  r the symbol pop
3c019 70 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ped..*/.static i
3c01a 6e 74 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72  nt yy_pop_parser
3c01b 5f 73 74 61 63 6b 28 79 79 50 61 72 73 65 72 20  _stack(yyParser 
3c01c 2a 70 50 61 72 73 65 72 29 7b 0a 20 20 59 59 43  *pParser){.  YYC
3c01d 4f 44 45 54 59 50 45 20 79 79 6d 61 6a 6f 72 3b  ODETYPE yymajor;
3c01e 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20  .  yyStackEntry 
3c01f 2a 79 79 74 6f 73 20 3d 20 26 70 50 61 72 73 65  *yytos = &pParse
3c020 72 2d 3e 79 79 73 74 61 63 6b 5b 70 50 61 72 73  r->yystack[pPars
3c021 65 72 2d 3e 79 79 69 64 78 5d 3b 0a 0a 20 20 2f  er->yyidx];..  /
3c022 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6d 65  * There is no me
3c023 63 68 61 6e 69 73 6d 20 62 79 20 77 68 69 63 68  chanism by which
3c024 20 74 68 65 20 70 61 72 73 65 72 20 73 74 61 63   the parser stac
3c025 6b 20 63 61 6e 20 62 65 20 70 6f 70 70 65 64 20  k can be popped 
3c026 62 65 6c 6f 77 0a 20 20 2a 2a 20 65 6d 70 74 79  below.  ** empty
3c027 20 69 6e 20 53 51 4c 69 74 65 2e 20 20 2a 2f 0a   in SQLite.  */.
3c028 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 72    if( NEVER(pPar
3c029 73 65 72 2d 3e 79 79 69 64 78 3c 30 29 20 29 20  ser->yyidx<0) ) 
3c02a 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 6e 64 65  return 0;.#ifnde
3c02b 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79  f NDEBUG.  if( y
3c02c 79 54 72 61 63 65 46 49 4c 45 20 26 26 20 70 50  yTraceFILE && pP
3c02d 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20  arser->yyidx>=0 
3c02e 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79  ){.    fprintf(y
3c02f 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 50 6f  yTraceFILE,"%sPo
3c030 70 70 69 6e 67 20 25 73 5c 6e 22 2c 0a 20 20 20  pping %s\n",.   
3c031 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74     yyTracePrompt
3c032 2c 0a 20 20 20 20 20 20 79 79 54 6f 6b 65 6e 4e  ,.      yyTokenN
3c033 61 6d 65 5b 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72  ame[yytos->major
3c034 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ]);.  }.#endif. 
3c035 20 79 79 6d 61 6a 6f 72 20 3d 20 79 79 74 6f 73   yymajor = yytos
3c036 2d 3e 6d 61 6a 6f 72 3b 0a 20 20 79 79 5f 64 65  ->major;.  yy_de
3c037 73 74 72 75 63 74 6f 72 28 70 50 61 72 73 65 72  structor(pParser
3c038 2c 20 79 79 6d 61 6a 6f 72 2c 20 26 79 79 74 6f  , yymajor, &yyto
3c039 73 2d 3e 6d 69 6e 6f 72 29 3b 0a 20 20 70 50 61  s->minor);.  pPa
3c03a 72 73 65 72 2d 3e 79 79 69 64 78 2d 2d 3b 0a 20  rser->yyidx--;. 
3c03b 20 72 65 74 75 72 6e 20 79 79 6d 61 6a 6f 72 3b   return yymajor;
3c03c 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 65 61 6c 6c  .}../* .** Deall
3c03d 6f 63 61 74 65 20 61 6e 64 20 64 65 73 74 72 6f  ocate and destro
3c03e 79 20 61 20 70 61 72 73 65 72 2e 20 20 44 65 73  y a parser.  Des
3c03f 74 72 75 63 74 6f 72 73 20 61 72 65 20 61 6c 6c  tructors are all
3c040 20 63 61 6c 6c 65 64 20 66 6f 72 0a 2a 2a 20 61   called for.** a
3c041 6c 6c 20 73 74 61 63 6b 20 65 6c 65 6d 65 6e 74  ll stack element
3c042 73 20 62 65 66 6f 72 65 20 73 68 75 74 74 69 6e  s before shuttin
3c043 67 20 74 68 65 20 70 61 72 73 65 72 20 64 6f 77  g the parser dow
3c044 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a  n..**.** Inputs:
3c045 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
3c046 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74    A pointer to t
3c047 68 65 20 70 61 72 73 65 72 2e 20 20 54 68 69 73  he parser.  This
3c048 20 73 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 69   should be a poi
3c049 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 6f 62  nter.**       ob
3c04a 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
3c04b 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 2e 0a  te3ParserAlloc..
3c04c 2a 2a 20 3c 6c 69 3e 20 20 41 20 70 6f 69 6e 74  ** <li>  A point
3c04d 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  er to a function
3c04e 20 75 73 65 64 20 74 6f 20 72 65 63 6c 61 69 6d   used to reclaim
3c04f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
3c050 0a 2a 2a 20 20 20 20 20 20 20 66 72 6f 6d 20 6d  .**       from m
3c051 61 6c 6c 6f 63 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a  alloc..** </ul>.
3c052 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
3c053 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
3c054 72 73 65 72 46 72 65 65 28 0a 20 20 76 6f 69 64  rserFree(.  void
3c055 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
3c056 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
3c057 61 72 73 65 72 20 74 6f 20 62 65 20 64 65 6c 65  arser to be dele
3c058 74 65 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ted */.  void (*
3c059 66 72 65 65 50 72 6f 63 29 28 76 6f 69 64 2a 29  freeProc)(void*)
3c05a 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e       /* Function
3c05b 20 75 73 65 64 20 74 6f 20 72 65 63 6c 61 69 6d   used to reclaim
3c05c 20 6d 65 6d 6f 72 79 20 2a 2f 0a 29 7b 0a 20 20   memory */.){.  
3c05d 79 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65  yyParser *pParse
3c05e 72 20 3d 20 28 79 79 50 61 72 73 65 72 2a 29 70  r = (yyParser*)p
3c05f 3b 0a 20 20 2f 2a 20 49 6e 20 53 51 4c 69 74 65  ;.  /* In SQLite
3c060 2c 20 77 65 20 6e 65 76 65 72 20 74 72 79 20 74  , we never try t
3c061 6f 20 64 65 73 74 72 6f 79 20 61 20 70 61 72 73  o destroy a pars
3c062 65 72 20 74 68 61 74 20 77 61 73 20 6e 6f 74 20  er that was not 
3c063 73 75 63 63 65 73 73 66 75 6c 6c 79 0a 20 20 2a  successfully.  *
3c064 2a 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65  * created in the
3c065 20 66 69 72 73 74 20 70 6c 61 63 65 2e 20 2a 2f   first place. */
3c066 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61  .  if( NEVER(pPa
3c067 72 73 65 72 3d 3d 30 29 20 29 20 72 65 74 75 72  rser==0) ) retur
3c068 6e 3b 0a 20 20 77 68 69 6c 65 28 20 70 50 61 72  n;.  while( pPar
3c069 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20  ser->yyidx>=0 ) 
3c06a 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74  yy_pop_parser_st
3c06b 61 63 6b 28 70 50 61 72 73 65 72 29 3b 0a 23 69  ack(pParser);.#i
3c06c 66 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d  f YYSTACKDEPTH<=
3c06d 30 0a 20 20 66 72 65 65 28 70 50 61 72 73 65 72  0.  free(pParser
3c06e 2d 3e 79 79 73 74 61 63 6b 29 3b 0a 23 65 6e 64  ->yystack);.#end
3c06f 69 66 0a 20 20 28 2a 66 72 65 65 50 72 6f 63 29  if.  (*freeProc)
3c070 28 28 76 6f 69 64 2a 29 70 50 61 72 73 65 72 29  ((void*)pParser)
3c071 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
3c072 6e 20 74 68 65 20 70 65 61 6b 20 64 65 70 74 68  n the peak depth
3c073 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 66 6f   of the stack fo
3c074 72 20 61 20 70 61 72 73 65 72 2e 0a 2a 2f 0a 23  r a parser..*/.#
3c075 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58  ifdef YYTRACKMAX
3c076 53 54 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 54  STACKDEPTH.SQLIT
3c077 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
3c078 6c 69 74 65 33 50 61 72 73 65 72 53 74 61 63 6b  lite3ParserStack
3c079 50 65 61 6b 28 76 6f 69 64 20 2a 70 29 7b 0a 20  Peak(void *p){. 
3c07a 20 79 79 50 61 72 73 65 72 20 2a 70 50 61 72 73   yyParser *pPars
3c07b 65 72 20 3d 20 28 79 79 50 61 72 73 65 72 2a 29  er = (yyParser*)
3c07c 70 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72  p;.  return pPar
3c07d 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 3b 0a 7d  ser->yyidxMax;.}
3c07e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46  .#endif../*.** F
3c07f 69 6e 64 20 74 68 65 20 61 70 70 72 6f 70 72 69  ind the appropri
3c080 61 74 65 20 61 63 74 69 6f 6e 20 66 6f 72 20 61  ate action for a
3c081 20 70 61 72 73 65 72 20 67 69 76 65 6e 20 74 68   parser given th
3c082 65 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 6c 6f  e terminal.** lo
3c083 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69  ok-ahead token i
3c084 4c 6f 6f 6b 41 68 65 61 64 2e 0a 2a 2a 0a 2a 2a  LookAhead..**.**
3c085 20 49 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65   If the look-ahe
3c086 61 64 20 74 6f 6b 65 6e 20 69 73 20 59 59 4e 4f  ad token is YYNO
3c087 43 4f 44 45 2c 20 74 68 65 6e 20 63 68 65 63 6b  CODE, then check
3c088 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 61   to see if the a
3c089 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 64 65  ction is.** inde
3c08a 70 65 6e 64 65 6e 74 20 6f 66 20 74 68 65 20 6c  pendent of the l
3c08b 6f 6f 6b 2d 61 68 65 61 64 2e 20 20 49 66 20 69  ook-ahead.  If i
3c08c 74 20 69 73 2c 20 72 65 74 75 72 6e 20 74 68 65  t is, return the
3c08d 20 61 63 74 69 6f 6e 2c 20 6f 74 68 65 72 77 69   action, otherwi
3c08e 73 65 0a 2a 2a 20 72 65 74 75 72 6e 20 59 59 5f  se.** return YY_
3c08f 4e 4f 5f 41 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74  NO_ACTION..*/.st
3c090 61 74 69 63 20 69 6e 74 20 79 79 5f 66 69 6e 64  atic int yy_find
3c091 5f 73 68 69 66 74 5f 61 63 74 69 6f 6e 28 0a 20  _shift_action(. 
3c092 20 79 79 50 61 72 73 65 72 20 2a 70 50 61 72 73   yyParser *pPars
3c093 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68  er,        /* Th
3c094 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 59 59  e parser */.  YY
3c095 43 4f 44 45 54 59 50 45 20 69 4c 6f 6f 6b 41 68  CODETYPE iLookAh
3c096 65 61 64 20 20 20 20 20 2f 2a 20 54 68 65 20 6c  ead     /* The l
3c097 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20  ook-ahead token 
3c098 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  */.){.  int i;. 
3c099 20 69 6e 74 20 73 74 61 74 65 6e 6f 20 3d 20 70   int stateno = p
3c09a 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b  Parser->yystack[
3c09b 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e  pParser->yyidx].
3c09c 73 74 61 74 65 6e 6f 3b 0a 20 0a 20 20 69 66 28  stateno;. .  if(
3c09d 20 73 74 61 74 65 6e 6f 3e 59 59 5f 53 48 49 46   stateno>YY_SHIF
3c09e 54 5f 43 4f 55 4e 54 0a 20 20 20 7c 7c 20 28 69  T_COUNT.   || (i
3c09f 20 3d 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74   = yy_shift_ofst
3c0a0 5b 73 74 61 74 65 6e 6f 5d 29 3d 3d 59 59 5f 53  [stateno])==YY_S
3c0a1 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 20 29 7b  HIFT_USE_DFLT ){
3c0a2 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64  .    return yy_d
3c0a3 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b  efault[stateno];
3c0a4 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69  .  }.  assert( i
3c0a5 4c 6f 6f 6b 41 68 65 61 64 21 3d 59 59 4e 4f 43  LookAhead!=YYNOC
3c0a6 4f 44 45 20 29 3b 0a 20 20 69 20 2b 3d 20 69 4c  ODE );.  i += iL
3c0a7 6f 6f 6b 41 68 65 61 64 3b 0a 20 20 69 66 28 20  ookAhead;.  if( 
3c0a8 69 3c 30 20 7c 7c 20 69 3e 3d 59 59 5f 41 43 54  i<0 || i>=YY_ACT
3c0a9 54 41 42 5f 43 4f 55 4e 54 20 7c 7c 20 79 79 5f  TAB_COUNT || yy_
3c0aa 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21 3d 69 4c  lookahead[i]!=iL
3c0ab 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20 20 20 20  ookAhead ){.    
3c0ac 69 66 28 20 69 4c 6f 6f 6b 41 68 65 61 64 3e 30  if( iLookAhead>0
3c0ad 20 29 7b 0a 23 69 66 64 65 66 20 59 59 46 41 4c   ){.#ifdef YYFAL
3c0ae 4c 42 41 43 4b 0a 20 20 20 20 20 20 59 59 43 4f  LBACK.      YYCO
3c0af 44 45 54 59 50 45 20 69 46 61 6c 6c 62 61 63 6b  DETYPE iFallback
3c0b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
3c0b1 46 61 6c 6c 62 61 63 6b 20 74 6f 6b 65 6e 20 2a  Fallback token *
3c0b2 2f 0a 20 20 20 20 20 20 69 66 28 20 69 4c 6f 6f  /.      if( iLoo
3c0b3 6b 41 68 65 61 64 3c 73 69 7a 65 6f 66 28 79 79  kAhead<sizeof(yy
3c0b4 46 61 6c 6c 62 61 63 6b 29 2f 73 69 7a 65 6f 66  Fallback)/sizeof
3c0b5 28 79 79 46 61 6c 6c 62 61 63 6b 5b 30 5d 29 0a  (yyFallback[0]).
3c0b6 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
3c0b7 28 69 46 61 6c 6c 62 61 63 6b 20 3d 20 79 79 46  (iFallback = yyF
3c0b8 61 6c 6c 62 61 63 6b 5b 69 4c 6f 6f 6b 41 68 65  allback[iLookAhe
3c0b9 61 64 5d 29 21 3d 30 20 29 7b 0a 23 69 66 6e 64  ad])!=0 ){.#ifnd
3c0ba 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 20 20  ef NDEBUG.      
3c0bb 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c    if( yyTraceFIL
3c0bc 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  E ){.          f
3c0bd 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
3c0be 4c 45 2c 20 22 25 73 46 41 4c 4c 42 41 43 4b 20  LE, "%sFALLBACK 
3c0bf 25 73 20 3d 3e 20 25 73 5c 6e 22 2c 0a 20 20 20  %s => %s\n",.   
3c0c0 20 20 20 20 20 20 20 20 20 20 79 79 54 72 61 63            yyTrac
3c0c1 65 50 72 6f 6d 70 74 2c 20 79 79 54 6f 6b 65 6e  ePrompt, yyToken
3c0c2 4e 61 6d 65 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d  Name[iLookAhead]
3c0c3 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 46  , yyTokenName[iF
3c0c4 61 6c 6c 62 61 63 6b 5d 29 3b 0a 20 20 20 20 20  allback]);.     
3c0c5 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
3c0c6 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f 66 69      return yy_fi
3c0c7 6e 64 5f 73 68 69 66 74 5f 61 63 74 69 6f 6e 28  nd_shift_action(
3c0c8 70 50 61 72 73 65 72 2c 20 69 46 61 6c 6c 62 61  pParser, iFallba
3c0c9 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  ck);.      }.#en
3c0ca 64 69 66 0a 23 69 66 64 65 66 20 59 59 57 49 4c  dif.#ifdef YYWIL
3c0cb 44 43 41 52 44 0a 20 20 20 20 20 20 7b 0a 20 20  DCARD.      {.  
3c0cc 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 69 20        int j = i 
3c0cd 2d 20 69 4c 6f 6f 6b 41 68 65 61 64 20 2b 20 59  - iLookAhead + Y
3c0ce 59 57 49 4c 44 43 41 52 44 3b 0a 20 20 20 20 20  YWILDCARD;.     
3c0cf 20 20 20 69 66 28 20 0a 23 69 66 20 59 59 5f 53     if( .#if YY_S
3c0d0 48 49 46 54 5f 4d 49 4e 2b 59 59 57 49 4c 44 43  HIFT_MIN+YYWILDC
3c0d1 41 52 44 3c 30 0a 20 20 20 20 20 20 20 20 20 20  ARD<0.          
3c0d2 6a 3e 3d 30 20 26 26 0a 23 65 6e 64 69 66 0a 23  j>=0 &&.#endif.#
3c0d3 69 66 20 59 59 5f 53 48 49 46 54 5f 4d 41 58 2b  if YY_SHIFT_MAX+
3c0d4 59 59 57 49 4c 44 43 41 52 44 3e 3d 59 59 5f 41  YYWILDCARD>=YY_A
3c0d5 43 54 54 41 42 5f 43 4f 55 4e 54 0a 20 20 20 20  CTTAB_COUNT.    
3c0d6 20 20 20 20 20 20 6a 3c 59 59 5f 41 43 54 54 41        j<YY_ACTTA
3c0d7 42 5f 43 4f 55 4e 54 20 26 26 0a 23 65 6e 64 69  B_COUNT &&.#endi
3c0d8 66 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 6c  f.          yy_l
3c0d9 6f 6f 6b 61 68 65 61 64 5b 6a 5d 3d 3d 59 59 57  ookahead[j]==YYW
3c0da 49 4c 44 43 41 52 44 0a 20 20 20 20 20 20 20 20  ILDCARD.        
3c0db 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  ){.#ifndef NDEBU
3c0dc 47 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  G.          if( 
3c0dd 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20  yyTraceFILE ){. 
3c0de 20 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e             fprin
3c0df 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 20  tf(yyTraceFILE, 
3c0e0 22 25 73 57 49 4c 44 43 41 52 44 20 25 73 20 3d  "%sWILDCARD %s =
3c0e1 3e 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20  > %s\n",.       
3c0e2 20 20 20 20 20 20 20 20 79 79 54 72 61 63 65 50          yyTraceP
3c0e3 72 6f 6d 70 74 2c 20 79 79 54 6f 6b 65 6e 4e 61  rompt, yyTokenNa
3c0e4 6d 65 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20  me[iLookAhead], 
3c0e5 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 59 59 57 49  yyTokenName[YYWI
3c0e6 4c 44 43 41 52 44 5d 29 3b 0a 20 20 20 20 20 20  LDCARD]);.      
3c0e7 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
3c0e8 4e 44 45 42 55 47 20 2a 2f 0a 20 20 20 20 20 20  NDEBUG */.      
3c0e9 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f 61 63      return yy_ac
3c0ea 74 69 6f 6e 5b 6a 5d 3b 0a 20 20 20 20 20 20 20  tion[j];.       
3c0eb 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69   }.      }.#endi
3c0ec 66 20 2f 2a 20 59 59 57 49 4c 44 43 41 52 44 20  f /* YYWILDCARD 
3c0ed 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  */.    }.    ret
3c0ee 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b 73  urn yy_default[s
3c0ef 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 65 6c 73 65  tateno];.  }else
3c0f0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f  {.    return yy_
3c0f1 61 63 74 69 6f 6e 5b 69 5d 3b 0a 20 20 7d 0a 7d  action[i];.  }.}
3c0f2 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65  ../*.** Find the
3c0f3 20 61 70 70 72 6f 70 72 69 61 74 65 20 61 63 74   appropriate act
3c0f4 69 6f 6e 20 66 6f 72 20 61 20 70 61 72 73 65 72  ion for a parser
3c0f5 20 67 69 76 65 6e 20 74 68 65 20 6e 6f 6e 2d 74   given the non-t
3c0f6 65 72 6d 69 6e 61 6c 0a 2a 2a 20 6c 6f 6f 6b 2d  erminal.** look-
3c0f7 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 4c 6f 6f  ahead token iLoo
3c0f8 6b 41 68 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  kAhead..**.** If
3c0f9 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20   the look-ahead 
3c0fa 74 6f 6b 65 6e 20 69 73 20 59 59 4e 4f 43 4f 44  token is YYNOCOD
3c0fb 45 2c 20 74 68 65 6e 20 63 68 65 63 6b 20 74 6f  E, then check to
3c0fc 20 73 65 65 20 69 66 20 74 68 65 20 61 63 74 69   see if the acti
3c0fd 6f 6e 20 69 73 0a 2a 2a 20 69 6e 64 65 70 65 6e  on is.** indepen
3c0fe 64 65 6e 74 20 6f 66 20 74 68 65 20 6c 6f 6f 6b  dent of the look
3c0ff 2d 61 68 65 61 64 2e 20 20 49 66 20 69 74 20 69  -ahead.  If it i
3c100 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 61 63  s, return the ac
3c101 74 69 6f 6e 2c 20 6f 74 68 65 72 77 69 73 65 0a  tion, otherwise.
3c102 2a 2a 20 72 65 74 75 72 6e 20 59 59 5f 4e 4f 5f  ** return YY_NO_
3c103 41 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69  ACTION..*/.stati
3c104 63 20 69 6e 74 20 79 79 5f 66 69 6e 64 5f 72 65  c int yy_find_re
3c105 64 75 63 65 5f 61 63 74 69 6f 6e 28 0a 20 20 69  duce_action(.  i
3c106 6e 74 20 73 74 61 74 65 6e 6f 2c 20 20 20 20 20  nt stateno,     
3c107 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
3c108 65 6e 74 20 73 74 61 74 65 20 6e 75 6d 62 65 72  ent state number
3c109 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45   */.  YYCODETYPE
3c10a 20 69 4c 6f 6f 6b 41 68 65 61 64 20 20 20 20 20   iLookAhead     
3c10b 2f 2a 20 54 68 65 20 6c 6f 6f 6b 2d 61 68 65 61  /* The look-ahea
3c10c 64 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20  d token */.){.  
3c10d 69 6e 74 20 69 3b 0a 23 69 66 64 65 66 20 59 59  int i;.#ifdef YY
3c10e 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69 66  ERRORSYMBOL.  if
3c10f 28 20 73 74 61 74 65 6e 6f 3e 59 59 5f 52 45 44  ( stateno>YY_RED
3c110 55 43 45 5f 43 4f 55 4e 54 20 29 7b 0a 20 20 20  UCE_COUNT ){.   
3c111 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75   return yy_defau
3c112 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d  lt[stateno];.  }
3c113 0a 23 65 6c 73 65 0a 20 20 61 73 73 65 72 74 28  .#else.  assert(
3c114 20 73 74 61 74 65 6e 6f 3c 3d 59 59 5f 52 45 44   stateno<=YY_RED
3c115 55 43 45 5f 43 4f 55 4e 54 20 29 3b 0a 23 65 6e  UCE_COUNT );.#en
3c116 64 69 66 0a 20 20 69 20 3d 20 79 79 5f 72 65 64  dif.  i = yy_red
3c117 75 63 65 5f 6f 66 73 74 5b 73 74 61 74 65 6e 6f  uce_ofst[stateno
3c118 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 69 21 3d  ];.  assert( i!=
3c119 59 59 5f 52 45 44 55 43 45 5f 55 53 45 5f 44 46  YY_REDUCE_USE_DF
3c11a 4c 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  LT );.  assert( 
3c11b 69 4c 6f 6f 6b 41 68 65 61 64 21 3d 59 59 4e 4f  iLookAhead!=YYNO
3c11c 43 4f 44 45 20 29 3b 0a 20 20 69 20 2b 3d 20 69  CODE );.  i += i
3c11d 4c 6f 6f 6b 41 68 65 61 64 3b 0a 23 69 66 64 65  LookAhead;.#ifde
3c11e 66 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a  f YYERRORSYMBOL.
3c11f 20 20 69 66 28 20 69 3c 30 20 7c 7c 20 69 3e 3d    if( i<0 || i>=
3c120 59 59 5f 41 43 54 54 41 42 5f 43 4f 55 4e 54 20  YY_ACTTAB_COUNT 
3c121 7c 7c 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b  || yy_lookahead[
3c122 69 5d 21 3d 69 4c 6f 6f 6b 41 68 65 61 64 20 29  i]!=iLookAhead )
3c123 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f  {.    return yy_
3c124 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d  default[stateno]
3c125 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 61 73  ;.  }.#else.  as
3c126 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
3c127 59 59 5f 41 43 54 54 41 42 5f 43 4f 55 4e 54 20  YY_ACTTAB_COUNT 
3c128 29 3b 0a 20 20 61 73 73 65 72 74 28 20 79 79 5f  );.  assert( yy_
3c129 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 3d 3d 69 4c  lookahead[i]==iL
3c12a 6f 6f 6b 41 68 65 61 64 20 29 3b 0a 23 65 6e 64  ookAhead );.#end
3c12b 69 66 0a 20 20 72 65 74 75 72 6e 20 79 79 5f 61  if.  return yy_a
3c12c 63 74 69 6f 6e 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a  ction[i];.}../*.
3c12d 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
3c12e 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
3c12f 65 64 20 69 66 20 74 68 65 20 73 74 61 63 6b 20  ed if the stack 
3c130 6f 76 65 72 66 6c 6f 77 73 2e 0a 2a 2f 0a 73 74  overflows..*/.st
3c131 61 74 69 63 20 76 6f 69 64 20 79 79 53 74 61 63  atic void yyStac
3c132 6b 4f 76 65 72 66 6c 6f 77 28 79 79 50 61 72 73  kOverflow(yyPars
3c133 65 72 20 2a 79 79 70 50 61 72 73 65 72 2c 20 59  er *yypParser, Y
3c134 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79 70 4d  YMINORTYPE *yypM
3c135 69 6e 6f 72 29 7b 0a 20 20 20 73 71 6c 69 74 65  inor){.   sqlite
3c136 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48  3ParserARG_FETCH
3c137 3b 0a 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e  ;.   yypParser->
3c138 79 79 69 64 78 2d 2d 3b 0a 23 69 66 6e 64 65 66  yyidx--;.#ifndef
3c139 20 4e 44 45 42 55 47 0a 20 20 20 69 66 28 20 79   NDEBUG.   if( y
3c13a 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20  yTraceFILE ){.  
3c13b 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
3c13c 63 65 46 49 4c 45 2c 22 25 73 53 74 61 63 6b 20  ceFILE,"%sStack 
3c13d 4f 76 65 72 66 6c 6f 77 21 5c 6e 22 2c 79 79 54  Overflow!\n",yyT
3c13e 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20  racePrompt);.   
3c13f 7d 0a 23 65 6e 64 69 66 0a 20 20 20 77 68 69 6c  }.#endif.   whil
3c140 65 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  e( yypParser->yy
3c141 69 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f  idx>=0 ) yy_pop_
3c142 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79 70  parser_stack(yyp
3c143 50 61 72 73 65 72 29 3b 0a 20 20 20 2f 2a 20 48  Parser);.   /* H
3c144 65 72 65 20 63 6f 64 65 20 69 73 20 69 6e 73 65  ere code is inse
3c145 72 74 65 64 20 77 68 69 63 68 20 77 69 6c 6c 20  rted which will 
3c146 65 78 65 63 75 74 65 20 69 66 20 74 68 65 20 70  execute if the p
3c147 61 72 73 65 72 0a 20 20 20 2a 2a 20 73 74 61 63  arser.   ** stac
3c148 6b 20 65 76 65 72 79 20 6f 76 65 72 66 6c 6f 77  k every overflow
3c149 73 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50  s */..  UNUSED_P
3c14a 41 52 41 4d 45 54 45 52 28 79 79 70 4d 69 6e 6f  ARAMETER(yypMino
3c14b 72 29 3b 20 2f 2a 20 53 69 6c 65 6e 63 65 20 73  r); /* Silence s
3c14c 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 72  ome compiler war
3c14d 6e 69 6e 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74  nings */.  sqlit
3c14e 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
3c14f 65 2c 20 22 70 61 72 73 65 72 20 73 74 61 63 6b  e, "parser stack
3c150 20 6f 76 65 72 66 6c 6f 77 22 29 3b 0a 20 20 20   overflow");.   
3c151 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
3c152 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72  _STORE; /* Suppr
3c153 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75  ess warning abou
3c154 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f  t unused %extra_
3c155 61 72 67 75 6d 65 6e 74 20 76 61 72 20 2a 2f 0a  argument var */.
3c156 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d  }../*.** Perform
3c157 20 61 20 73 68 69 66 74 20 61 63 74 69 6f 6e 2e   a shift action.
3c158 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3c159 79 79 5f 73 68 69 66 74 28 0a 20 20 79 79 50 61  yy_shift(.  yyPa
3c15a 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 2c  rser *yypParser,
3c15b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3c15c 20 70 61 72 73 65 72 20 74 6f 20 62 65 20 73 68   parser to be sh
3c15d 69 66 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 79  ifted */.  int y
3c15e 79 4e 65 77 53 74 61 74 65 2c 20 20 20 20 20 20  yNewState,      
3c15f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3c160 6e 65 77 20 73 74 61 74 65 20 74 6f 20 73 68 69  new state to shi
3c161 66 74 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 79  ft in */.  int y
3c162 79 4d 61 6a 6f 72 2c 20 20 20 20 20 20 20 20 20  yMajor,         
3c163 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3c164 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 74 6f 20 73  major token to s
3c165 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20 59 59 4d  hift in */.  YYM
3c166 49 4e 4f 52 54 59 50 45 20 2a 79 79 70 4d 69 6e  INORTYPE *yypMin
3c167 6f 72 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f  or         /* Po
3c168 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 69 6e  inter to the min
3c169 6f 72 20 74 6f 6b 65 6e 20 74 6f 20 73 68 69 66  or token to shif
3c16a 74 20 69 6e 20 2a 2f 0a 29 7b 0a 20 20 79 79 53  t in */.){.  yyS
3c16b 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 74 6f 73  tackEntry *yytos
3c16c 3b 0a 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79  ;.  yypParser->y
3c16d 79 69 64 78 2b 2b 3b 0a 23 69 66 64 65 66 20 59  yidx++;.#ifdef Y
3c16e 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45  YTRACKMAXSTACKDE
3c16f 50 54 48 0a 20 20 69 66 28 20 79 79 70 50 61 72  PTH.  if( yypPar
3c170 73 65 72 2d 3e 79 79 69 64 78 3e 79 79 70 50 61  ser->yyidx>yypPa
3c171 72 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 20 29  rser->yyidxMax )
3c172 7b 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d  {.    yypParser-
3c173 3e 79 79 69 64 78 4d 61 78 20 3d 20 79 79 70 50  >yyidxMax = yypP
3c174 61 72 73 65 72 2d 3e 79 79 69 64 78 3b 0a 20 20  arser->yyidx;.  
3c175 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20 59 59 53  }.#endif.#if YYS
3c176 54 41 43 4b 44 45 50 54 48 3e 30 20 0a 20 20 69  TACKDEPTH>0 .  i
3c177 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
3c178 69 64 78 3e 3d 59 59 53 54 41 43 4b 44 45 50 54  idx>=YYSTACKDEPT
3c179 48 20 29 7b 0a 20 20 20 20 79 79 53 74 61 63 6b  H ){.    yyStack
3c17a 4f 76 65 72 66 6c 6f 77 28 79 79 70 50 61 72 73  Overflow(yypPars
3c17b 65 72 2c 20 79 79 70 4d 69 6e 6f 72 29 3b 0a 20  er, yypMinor);. 
3c17c 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23     return;.  }.#
3c17d 65 6c 73 65 0a 20 20 69 66 28 20 79 79 70 50 61  else.  if( yypPa
3c17e 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 79 79 70  rser->yyidx>=yyp
3c17f 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20  Parser->yystksz 
3c180 29 7b 0a 20 20 20 20 79 79 47 72 6f 77 53 74 61  ){.    yyGrowSta
3c181 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  ck(yypParser);. 
3c182 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72     if( yypParser
3c183 2d 3e 79 79 69 64 78 3e 3d 79 79 70 50 61 72 73  ->yyidx>=yypPars
3c184 65 72 2d 3e 79 79 73 74 6b 73 7a 20 29 7b 0a 20  er->yystksz ){. 
3c185 20 20 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72       yyStackOver
3c186 66 6c 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20  flow(yypParser, 
3c187 79 79 70 4d 69 6e 6f 72 29 3b 0a 20 20 20 20 20  yypMinor);.     
3c188 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
3c189 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 79 74 6f   }.#endif.  yyto
3c18a 73 20 3d 20 26 79 79 70 50 61 72 73 65 72 2d 3e  s = &yypParser->
3c18b 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65  yystack[yypParse
3c18c 72 2d 3e 79 79 69 64 78 5d 3b 0a 20 20 79 79 74  r->yyidx];.  yyt
3c18d 6f 73 2d 3e 73 74 61 74 65 6e 6f 20 3d 20 28 59  os->stateno = (Y
3c18e 59 41 43 54 49 4f 4e 54 59 50 45 29 79 79 4e 65  YACTIONTYPE)yyNe
3c18f 77 53 74 61 74 65 3b 0a 20 20 79 79 74 6f 73 2d  wState;.  yytos-
3c190 3e 6d 61 6a 6f 72 20 3d 20 28 59 59 43 4f 44 45  >major = (YYCODE
3c191 54 59 50 45 29 79 79 4d 61 6a 6f 72 3b 0a 20 20  TYPE)yyMajor;.  
3c192 79 79 74 6f 73 2d 3e 6d 69 6e 6f 72 20 3d 20 2a  yytos->minor = *
3c193 79 79 70 4d 69 6e 6f 72 3b 0a 23 69 66 6e 64 65  yypMinor;.#ifnde
3c194 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79  f NDEBUG.  if( y
3c195 79 54 72 61 63 65 46 49 4c 45 20 26 26 20 79 79  yTraceFILE && yy
3c196 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 30  pParser->yyidx>0
3c197 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
3c198 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
3c199 63 65 46 49 4c 45 2c 22 25 73 53 68 69 66 74 20  ceFILE,"%sShift 
3c19a 25 64 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f  %d\n",yyTracePro
3c19b 6d 70 74 2c 79 79 4e 65 77 53 74 61 74 65 29 3b  mpt,yyNewState);
3c19c 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54  .    fprintf(yyT
3c19d 72 61 63 65 46 49 4c 45 2c 22 25 73 53 74 61 63  raceFILE,"%sStac
3c19e 6b 3a 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70  k:",yyTracePromp
3c19f 74 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b  t);.    for(i=1;
3c1a0 20 69 3c 3d 79 79 70 50 61 72 73 65 72 2d 3e 79   i<=yypParser->y
3c1a1 79 69 64 78 3b 20 69 2b 2b 29 0a 20 20 20 20 20  yidx; i++).     
3c1a2 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
3c1a3 46 49 4c 45 2c 22 20 25 73 22 2c 79 79 54 6f 6b  FILE," %s",yyTok
3c1a4 65 6e 4e 61 6d 65 5b 79 79 70 50 61 72 73 65 72  enName[yypParser
3c1a5 2d 3e 79 79 73 74 61 63 6b 5b 69 5d 2e 6d 61 6a  ->yystack[i].maj
3c1a6 6f 72 5d 29 3b 0a 20 20 20 20 66 70 72 69 6e 74  or]);.    fprint
3c1a7 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 5c  f(yyTraceFILE,"\
3c1a8 6e 22 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  n");.  }.#endif.
3c1a9 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  }../* The follow
3c1aa 69 6e 67 20 74 61 62 6c 65 20 63 6f 6e 74 61 69  ing table contai
3c1ab 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ns information a
3c1ac 62 6f 75 74 20 65 76 65 72 79 20 72 75 6c 65 20  bout every rule 
3c1ad 74 68 61 74 0a 2a 2a 20 69 73 20 75 73 65 64 20  that.** is used 
3c1ae 64 75 72 69 6e 67 20 74 68 65 20 72 65 64 75 63  during the reduc
3c1af 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  e..*/.static con
3c1b0 73 74 20 73 74 72 75 63 74 20 7b 0a 20 20 59 59  st struct {.  YY
3c1b1 43 4f 44 45 54 59 50 45 20 6c 68 73 3b 20 20 20  CODETYPE lhs;   
3c1b2 20 20 20 20 20 20 2f 2a 20 53 79 6d 62 6f 6c 20        /* Symbol 
3c1b3 6f 6e 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64  on the left-hand
3c1b4 20 73 69 64 65 20 6f 66 20 74 68 65 20 72 75 6c   side of the rul
3c1b5 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
3c1b6 63 68 61 72 20 6e 72 68 73 3b 20 20 20 20 20 2f  char nrhs;     /
3c1b7 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 69 67 68  * Number of righ
3c1b8 74 2d 68 61 6e 64 20 73 69 64 65 20 73 79 6d 62  t-hand side symb
3c1b9 6f 6c 73 20 69 6e 20 74 68 65 20 72 75 6c 65 20  ols in the rule 
3c1ba 2a 2f 0a 7d 20 79 79 52 75 6c 65 49 6e 66 6f 5b  */.} yyRuleInfo[
3c1bb 5d 20 3d 20 7b 0a 20 20 7b 20 31 34 32 2c 20 31  ] = {.  { 142, 1
3c1bc 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 32 20 7d   },.  { 143, 2 }
3c1bd 2c 0a 20 20 7b 20 31 34 33 2c 20 31 20 7d 2c 0a  ,.  { 143, 1 },.
3c1be 20 20 7b 20 31 34 34 2c 20 31 20 7d 2c 0a 20 20    { 144, 1 },.  
3c1bf 7b 20 31 34 34 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 144, 3 },.  { 
3c1c0 31 34 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34  145, 0 },.  { 14
3c1c1 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 35 2c  5, 1 },.  { 145,
3c1c2 20 33 20 7d 2c 0a 20 20 7b 20 31 34 36 2c 20 31   3 },.  { 146, 1
3c1c3 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d   },.  { 147, 3 }
3c1c4 2c 0a 20 20 7b 20 31 34 39 2c 20 30 20 7d 2c 0a  ,.  { 149, 0 },.
3c1c5 20 20 7b 20 31 34 39 2c 20 31 20 7d 2c 0a 20 20    { 149, 1 },.  
3c1c6 7b 20 31 34 39 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 149, 2 },.  { 
3c1c7 31 34 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34  148, 0 },.  { 14
3c1c8 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 38 2c  8, 1 },.  { 148,
3c1c9 20 31 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20 31   1 },.  { 148, 1
3c1ca 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d   },.  { 147, 2 }
3c1cb 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a  ,.  { 147, 2 },.
3c1cc 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20    { 147, 2 },.  
3c1cd 7b 20 31 35 31 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 151, 1 },.  { 
3c1ce 31 35 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34  151, 0 },.  { 14
3c1cf 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 2 },.  { 147,
3c1d0 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35   3 },.  { 147, 5
3c1d1 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d   },.  { 147, 2 }
3c1d2 2c 0a 20 20 7b 20 31 35 32 2c 20 36 20 7d 2c 0a  ,.  { 152, 6 },.
3c1d3 20 20 7b 20 31 35 34 2c 20 31 20 7d 2c 0a 20 20    { 154, 1 },.  
3c1d4 7b 20 31 35 36 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 156, 0 },.  { 
3c1d5 31 35 36 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 35  156, 3 },.  { 15
3c1d6 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 35 2c  5, 1 },.  { 155,
3c1d7 20 30 20 7d 2c 0a 20 20 7b 20 31 35 33 2c 20 34   0 },.  { 153, 4
3c1d8 20 7d 2c 0a 20 20 7b 20 31 35 33 2c 20 32 20 7d   },.  { 153, 2 }
3c1d9 2c 0a 20 20 7b 20 31 35 38 2c 20 33 20 7d 2c 0a  ,.  { 158, 3 },.
3c1da 20 20 7b 20 31 35 38 2c 20 31 20 7d 2c 0a 20 20    { 158, 1 },.  
3c1db 7b 20 31 36 31 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 161, 3 },.  { 
3c1dc 31 36 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  162, 1 },.  { 16
3c1dd 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 35 2c  5, 1 },.  { 165,
3c1de 20 31 20 7d 2c 0a 20 20 7b 20 31 36 36 2c 20 31   1 },.  { 166, 1
3c1df 20 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 31 20 7d   },.  { 150, 1 }
3c1e0 2c 0a 20 20 7b 20 31 35 30 2c 20 31 20 7d 2c 0a  ,.  { 150, 1 },.
3c1e1 20 20 7b 20 31 35 30 2c 20 31 20 7d 2c 0a 20 20    { 150, 1 },.  
3c1e2 7b 20 31 36 33 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 163, 0 },.  { 
3c1e3 31 36 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  163, 1 },.  { 16
3c1e4 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 37 2c  7, 1 },.  { 167,
3c1e5 20 34 20 7d 2c 0a 20 20 7b 20 31 36 37 2c 20 36   4 },.  { 167, 6
3c1e6 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 31 20 7d   },.  { 168, 1 }
3c1e7 2c 0a 20 20 7b 20 31 36 38 2c 20 32 20 7d 2c 0a  ,.  { 168, 2 },.
3c1e8 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c 0a 20 20    { 169, 1 },.  
3c1e9 7b 20 31 36 39 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 169, 1 },.  { 
3c1ea 31 36 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 36  164, 2 },.  { 16
3c1eb 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 32 2c  4, 0 },.  { 172,
3c1ec 20 32 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20 32   2 },.  { 172, 2
3c1ed 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20 34 20 7d   },.  { 172, 4 }
3c1ee 2c 0a 20 20 7b 20 31 37 32 2c 20 33 20 7d 2c 0a  ,.  { 172, 3 },.
3c1ef 20 20 7b 20 31 37 32 2c 20 33 20 7d 2c 0a 20 20    { 172, 3 },.  
3c1f0 7b 20 31 37 32 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 172, 2 },.  { 
3c1f1 31 37 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37  172, 2 },.  { 17
3c1f2 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 32 2c  2, 3 },.  { 172,
3c1f3 20 35 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20 32   5 },.  { 172, 2
3c1f4 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20 34 20 7d   },.  { 172, 4 }
3c1f5 2c 0a 20 20 7b 20 31 37 32 2c 20 34 20 7d 2c 0a  ,.  { 172, 4 },.
3c1f6 20 20 7b 20 31 37 32 2c 20 31 20 7d 2c 0a 20 20    { 172, 1 },.  
3c1f7 7b 20 31 37 32 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 172, 2 },.  { 
3c1f8 31 37 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37  177, 0 },.  { 17
3c1f9 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 39 2c  7, 1 },.  { 179,
3c1fa 20 30 20 7d 2c 0a 20 20 7b 20 31 37 39 2c 20 32   0 },.  { 179, 2
3c1fb 20 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 32 20 7d   },.  { 181, 2 }
3c1fc 2c 0a 20 20 7b 20 31 38 31 2c 20 33 20 7d 2c 0a  ,.  { 181, 3 },.
3c1fd 20 20 7b 20 31 38 31 2c 20 33 20 7d 2c 0a 20 20    { 181, 3 },.  
3c1fe 7b 20 31 38 31 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 181, 3 },.  { 
3c1ff 31 38 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38  182, 2 },.  { 18
3c200 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 32 2c  2, 2 },.  { 182,
3c201 20 31 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 31   1 },.  { 182, 1
3c202 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 32 20 7d   },.  { 182, 2 }
3c203 2c 0a 20 20 7b 20 31 38 30 2c 20 33 20 7d 2c 0a  ,.  { 180, 3 },.
3c204 20 20 7b 20 31 38 30 2c 20 32 20 7d 2c 0a 20 20    { 180, 2 },.  
3c205 7b 20 31 38 33 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 183, 0 },.  { 
3c206 31 38 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38  183, 2 },.  { 18
3c207 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35 39 2c  3, 2 },.  { 159,
3c208 20 30 20 7d 2c 0a 20 20 7b 20 31 35 39 2c 20 32   0 },.  { 159, 2
3c209 20 7d 2c 0a 20 20 7b 20 31 38 34 2c 20 33 20 7d   },.  { 184, 3 }
3c20a 2c 0a 20 20 7b 20 31 38 34 2c 20 31 20 7d 2c 0a  ,.  { 184, 1 },.
3c20b 20 20 7b 20 31 38 35 2c 20 31 20 7d 2c 0a 20 20    { 185, 1 },.  
3c20c 7b 20 31 38 35 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 185, 0 },.  { 
3c20d 31 38 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38  186, 2 },.  { 18
3c20e 36 2c 20 37 20 7d 2c 0a 20 20 7b 20 31 38 36 2c  6, 7 },.  { 186,
3c20f 20 35 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 35   5 },.  { 186, 5
3c210 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 31 30 20   },.  { 186, 10 
3c211 7d 2c 0a 20 20 7b 20 31 38 38 2c 20 30 20 7d 2c  },.  { 188, 0 },
3c212 0a 20 20 7b 20 31 38 38 2c 20 31 20 7d 2c 0a 20  .  { 188, 1 },. 
3c213 20 7b 20 31 37 35 2c 20 30 20 7d 2c 0a 20 20 7b   { 175, 0 },.  {
3c214 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   175, 3 },.  { 1
3c215 38 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38 39  89, 0 },.  { 189
3c216 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 30 2c 20  , 2 },.  { 190, 
3c217 31 20 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 31 20  1 },.  { 190, 1 
3c218 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 31 20 7d 2c  },.  { 190, 1 },
3c219 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20  .  { 147, 4 },. 
3c21a 20 7b 20 31 39 32 2c 20 32 20 7d 2c 0a 20 20 7b   { 192, 2 },.  {
3c21b 20 31 39 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   192, 0 },.  { 1
3c21c 34 37 2c 20 38 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 8 },.  { 147
3c21d 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 4 },.  { 147, 
3c21e 31 20 7d 2c 0a 20 20 7b 20 31 36 30 2c 20 31 20  1 },.  { 160, 1 
3c21f 7d 2c 0a 20 20 7b 20 31 36 30 2c 20 33 20 7d 2c  },.  { 160, 3 },
3c220 0a 20 20 7b 20 31 39 35 2c 20 31 20 7d 2c 0a 20  .  { 195, 1 },. 
3c221 20 7b 20 31 39 35 2c 20 32 20 7d 2c 0a 20 20 7b   { 195, 2 },.  {
3c222 20 31 39 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   195, 1 },.  { 1
3c223 39 34 2c 20 39 20 7d 2c 0a 20 20 7b 20 31 39 36  94, 9 },.  { 196
3c224 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 36 2c 20  , 1 },.  { 196, 
3c225 31 20 7d 2c 0a 20 20 7b 20 31 39 36 2c 20 30 20  1 },.  { 196, 0 
3c226 7d 2c 0a 20 20 7b 20 32 30 34 2c 20 32 20 7d 2c  },.  { 204, 2 },
3c227 0a 20 20 7b 20 32 30 34 2c 20 30 20 7d 2c 0a 20  .  { 204, 0 },. 
3c228 20 7b 20 31 39 37 2c 20 33 20 7d 2c 0a 20 20 7b   { 197, 3 },.  {
3c229 20 31 39 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   197, 2 },.  { 1
3c22a 39 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 30 35  97, 4 },.  { 205
3c22b 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20  , 2 },.  { 205, 
3c22c 31 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20 30 20  1 },.  { 205, 0 
3c22d 7d 2c 0a 20 20 7b 20 31 39 38 2c 20 30 20 7d 2c  },.  { 198, 0 },
3c22e 0a 20 20 7b 20 31 39 38 2c 20 32 20 7d 2c 0a 20  .  { 198, 2 },. 
3c22f 20 7b 20 32 30 37 2c 20 32 20 7d 2c 0a 20 20 7b   { 207, 2 },.  {
3c230 20 32 30 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   207, 0 },.  { 2
3c231 30 36 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 30 36  06, 7 },.  { 206
3c232 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 30 36 2c 20  , 7 },.  { 206, 
3c233 37 20 7d 2c 0a 20 20 7b 20 31 35 37 2c 20 30 20  7 },.  { 157, 0 
3c234 7d 2c 0a 20 20 7b 20 31 35 37 2c 20 32 20 7d 2c  },.  { 157, 2 },
3c235 0a 20 20 7b 20 31 39 33 2c 20 32 20 7d 2c 0a 20  .  { 193, 2 },. 
3c236 20 7b 20 32 30 38 2c 20 31 20 7d 2c 0a 20 20 7b   { 208, 1 },.  {
3c237 20 32 30 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   208, 2 },.  { 2
3c238 30 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 30 38  08, 3 },.  { 208
3c239 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 31 30 2c 20  , 4 },.  { 210, 
3c23a 32 20 7d 2c 0a 20 20 7b 20 32 31 30 2c 20 30 20  2 },.  { 210, 0 
3c23b 7d 2c 0a 20 20 7b 20 32 30 39 2c 20 30 20 7d 2c  },.  { 209, 0 },
3c23c 0a 20 20 7b 20 32 30 39 2c 20 33 20 7d 2c 0a 20  .  { 209, 3 },. 
3c23d 20 7b 20 32 30 39 2c 20 32 20 7d 2c 0a 20 20 7b   { 209, 2 },.  {
3c23e 20 32 31 31 2c 20 34 20 7d 2c 0a 20 20 7b 20 32   211, 4 },.  { 2
3c23f 31 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 32  11, 0 },.  { 202
3c240 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 32 2c 20  , 0 },.  { 202, 
3c241 33 20 7d 2c 0a 20 20 7b 20 32 31 34 2c 20 34 20  3 },.  { 214, 4 
3c242 7d 2c 0a 20 20 7b 20 32 31 34 2c 20 32 20 7d 2c  },.  { 214, 2 },
3c243 0a 20 20 7b 20 31 37 36 2c 20 31 20 7d 2c 0a 20  .  { 176, 1 },. 
3c244 20 7b 20 31 37 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 176, 1 },.  {
3c245 20 31 37 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   176, 0 },.  { 2
3c246 30 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 30  00, 0 },.  { 200
3c247 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 30 31 2c 20  , 3 },.  { 201, 
3c248 30 20 7d 2c 0a 20 20 7b 20 32 30 31 2c 20 32 20  0 },.  { 201, 2 
3c249 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 30 20 7d 2c  },.  { 203, 0 },
3c24a 0a 20 20 7b 20 32 30 33 2c 20 32 20 7d 2c 0a 20  .  { 203, 2 },. 
3c24b 20 7b 20 32 30 33 2c 20 34 20 7d 2c 0a 20 20 7b   { 203, 4 },.  {
3c24c 20 32 30 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   203, 4 },.  { 1
3c24d 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 39 39  47, 5 },.  { 199
3c24e 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 39 2c 20  , 0 },.  { 199, 
3c24f 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 37 20  2 },.  { 147, 7 
3c250 7d 2c 0a 20 20 7b 20 32 31 36 2c 20 35 20 7d 2c  },.  { 216, 5 },
3c251 0a 20 20 7b 20 32 31 36 2c 20 33 20 7d 2c 0a 20  .  { 216, 3 },. 
3c252 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b   { 147, 5 },.  {
3c253 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   147, 5 },.  { 1
3c254 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 32 31 37  47, 6 },.  { 217
3c255 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20  , 2 },.  { 217, 
3c256 31 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 34 20  1 },.  { 219, 4 
3c257 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 35 20 7d 2c  },.  { 219, 5 },
3c258 0a 20 20 7b 20 32 31 38 2c 20 30 20 7d 2c 0a 20  .  { 218, 0 },. 
3c259 20 7b 20 32 31 38 2c 20 33 20 7d 2c 0a 20 20 7b   { 218, 3 },.  {
3c25a 20 32 31 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   213, 3 },.  { 2
3c25b 31 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 34  13, 1 },.  { 174
3c25c 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20  , 1 },.  { 174, 
3c25d 33 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 31 20  3 },.  { 173, 1 
3c25e 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c  },.  { 174, 1 },
3c25f 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20  .  { 174, 1 },. 
3c260 20 7b 20 31 37 34 2c 20 33 20 7d 2c 0a 20 20 7b   { 174, 3 },.  {
3c261 20 31 37 34 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   174, 5 },.  { 1
3c262 37 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 33  73, 1 },.  { 173
3c263 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20  , 1 },.  { 174, 
3c264 31 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 31 20  1 },.  { 174, 1 
3c265 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 33 20 7d 2c  },.  { 174, 3 },
3c266 0a 20 20 7b 20 31 37 34 2c 20 36 20 7d 2c 0a 20  .  { 174, 6 },. 
3c267 20 7b 20 31 37 34 2c 20 35 20 7d 2c 0a 20 20 7b   { 174, 5 },.  {
3c268 20 31 37 34 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   174, 4 },.  { 1
3c269 37 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 34  73, 1 },.  { 174
3c26a 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20  , 3 },.  { 174, 
3c26b 33 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 33 20  3 },.  { 174, 3 
3c26c 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 33 20 7d 2c  },.  { 174, 3 },
3c26d 0a 20 20 7b 20 31 37 34 2c 20 33 20 7d 2c 0a 20  .  { 174, 3 },. 
3c26e 20 7b 20 31 37 34 2c 20 33 20 7d 2c 0a 20 20 7b   { 174, 3 },.  {
3c26f 20 31 37 34 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   174, 3 },.  { 1
3c270 37 34 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 32 31  74, 3 },.  { 221
3c271 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 31 2c 20  , 1 },.  { 221, 
3c272 32 20 7d 2c 0a 20 20 7b 20 32 32 31 2c 20 31 20  2 },.  { 221, 1 
3c273 7d 2c 0a 20 20 7b 20 32 32 31 2c 20 32 20 7d 2c  },.  { 221, 2 },
3c274 0a 20 20 7b 20 31 37 34 2c 20 33 20 7d 2c 0a 20  .  { 174, 3 },. 
3c275 20 7b 20 31 37 34 2c 20 35 20 7d 2c 0a 20 20 7b   { 174, 5 },.  {
3c276 20 31 37 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   174, 2 },.  { 1
3c277 37 34 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 34  74, 3 },.  { 174
3c278 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20  , 3 },.  { 174, 
3c279 34 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 32 20  4 },.  { 174, 2 
3c27a 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 32 20 7d 2c  },.  { 174, 2 },
3c27b 0a 20 20 7b 20 31 37 34 2c 20 32 20 7d 2c 0a 20  .  { 174, 2 },. 
3c27c 20 7b 20 31 37 34 2c 20 32 20 7d 2c 0a 20 20 7b   { 174, 2 },.  {
3c27d 20 32 32 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   222, 1 },.  { 2
3c27e 32 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 34  22, 2 },.  { 174
3c27f 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 33 2c 20  , 5 },.  { 223, 
3c280 31 20 7d 2c 0a 20 20 7b 20 32 32 33 2c 20 32 20  1 },.  { 223, 2 
3c281 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 35 20 7d 2c  },.  { 174, 5 },
3c282 0a 20 20 7b 20 31 37 34 2c 20 33 20 7d 2c 0a 20  .  { 174, 3 },. 
3c283 20 7b 20 31 37 34 2c 20 35 20 7d 2c 0a 20 20 7b   { 174, 5 },.  {
3c284 20 31 37 34 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   174, 4 },.  { 1
3c285 37 34 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 34  74, 4 },.  { 174
3c286 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 35 2c 20  , 5 },.  { 225, 
3c287 35 20 7d 2c 0a 20 20 7b 20 32 32 35 2c 20 34 20  5 },.  { 225, 4 
3c288 7d 2c 0a 20 20 7b 20 32 32 36 2c 20 32 20 7d 2c  },.  { 226, 2 },
3c289 0a 20 20 7b 20 32 32 36 2c 20 30 20 7d 2c 0a 20  .  { 226, 0 },. 
3c28a 20 7b 20 32 32 34 2c 20 31 20 7d 2c 0a 20 20 7b   { 224, 1 },.  {
3c28b 20 32 32 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   224, 0 },.  { 2
3c28c 32 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 30  20, 1 },.  { 220
3c28d 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 31 35 2c 20  , 0 },.  { 215, 
3c28e 33 20 7d 2c 0a 20 20 7b 20 32 31 35 2c 20 31 20  3 },.  { 215, 1 
3c28f 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 31 20 7d  },.  { 147, 11 }
3c290 2c 0a 20 20 7b 20 32 32 37 2c 20 31 20 7d 2c 0a  ,.  { 227, 1 },.
3c291 20 20 7b 20 32 32 37 2c 20 30 20 7d 2c 0a 20 20    { 227, 0 },.  
3c292 7b 20 31 37 38 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 178, 0 },.  { 
3c293 31 37 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38  178, 3 },.  { 18
3c294 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38 37 2c  7, 5 },.  { 187,
3c295 20 33 20 7d 2c 0a 20 20 7b 20 32 32 38 2c 20 30   3 },.  { 228, 0
3c296 20 7d 2c 0a 20 20 7b 20 32 32 38 2c 20 32 20 7d   },.  { 228, 2 }
3c297 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a  ,.  { 147, 4 },.
3c298 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20    { 147, 1 },.  
3c299 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 147, 2 },.  { 
3c29a 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34  147, 3 },.  { 14
3c29b 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 5 },.  { 147,
3c29c 20 36 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35   6 },.  { 147, 5
3c29d 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d   },.  { 147, 6 }
3c29e 2c 0a 20 20 7b 20 32 32 39 2c 20 31 20 7d 2c 0a  ,.  { 229, 1 },.
3c29f 20 20 7b 20 32 32 39 2c 20 31 20 7d 2c 0a 20 20    { 229, 1 },.  
3c2a0 7b 20 32 32 39 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 229, 1 },.  { 
3c2a1 32 32 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32  229, 1 },.  { 22
3c2a2 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 30 2c  9, 1 },.  { 170,
3c2a3 20 32 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 31   2 },.  { 170, 1
3c2a4 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 32 20 7d   },.  { 171, 2 }
3c2a5 2c 0a 20 20 7b 20 32 33 30 2c 20 31 20 7d 2c 0a  ,.  { 230, 1 },.
3c2a6 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20    { 147, 5 },.  
3c2a7 7b 20 32 33 31 2c 20 31 31 20 7d 2c 0a 20 20 7b  { 231, 11 },.  {
3c2a8 20 32 33 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   233, 1 },.  { 2
3c2a9 33 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 33  33, 1 },.  { 233
3c2aa 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 33 2c 20  , 2 },.  { 233, 
3c2ab 30 20 7d 2c 0a 20 20 7b 20 32 33 34 2c 20 31 20  0 },.  { 234, 1 
3c2ac 7d 2c 0a 20 20 7b 20 32 33 34 2c 20 31 20 7d 2c  },.  { 234, 1 },
3c2ad 0a 20 20 7b 20 32 33 34 2c 20 33 20 7d 2c 0a 20  .  { 234, 3 },. 
3c2ae 20 7b 20 32 33 35 2c 20 30 20 7d 2c 0a 20 20 7b   { 235, 0 },.  {
3c2af 20 32 33 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   235, 3 },.  { 2
3c2b0 33 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 36  36, 0 },.  { 236
3c2b1 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20  , 2 },.  { 232, 
3c2b2 33 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 32 20  3 },.  { 232, 2 
3c2b3 7d 2c 0a 20 20 7b 20 32 33 38 2c 20 31 20 7d 2c  },.  { 238, 1 },
3c2b4 0a 20 20 7b 20 32 33 38 2c 20 33 20 7d 2c 0a 20  .  { 238, 3 },. 
3c2b5 20 7b 20 32 33 39 2c 20 30 20 7d 2c 0a 20 20 7b   { 239, 0 },.  {
3c2b6 20 32 33 39 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   239, 3 },.  { 2
3c2b7 33 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 37  39, 2 },.  { 237
3c2b8 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20  , 7 },.  { 237, 
3c2b9 35 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 35 20  5 },.  { 237, 5 
3c2ba 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 35 20 7d 2c  },.  { 237, 5 },
3c2bb 0a 20 20 7b 20 32 33 37 2c 20 31 20 7d 2c 0a 20  .  { 237, 1 },. 
3c2bc 20 7b 20 31 37 34 2c 20 34 20 7d 2c 0a 20 20 7b   { 174, 4 },.  {
3c2bd 20 31 37 34 2c 20 36 20 7d 2c 0a 20 20 7b 20 31   174, 6 },.  { 1
3c2be 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 31  91, 1 },.  { 191
3c2bf 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 31 2c 20  , 1 },.  { 191, 
3c2c0 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20  1 },.  { 147, 4 
3c2c1 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c  },.  { 147, 6 },
3c2c2 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20  .  { 147, 3 },. 
3c2c3 20 7b 20 32 34 31 2c 20 30 20 7d 2c 0a 20 20 7b   { 241, 0 },.  {
3c2c4 20 32 34 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   241, 2 },.  { 2
3c2c5 34 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 30  40, 1 },.  { 240
3c2c6 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 0 },.  { 147, 
3c2c7 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20  1 },.  { 147, 3 
3c2c8 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c  },.  { 147, 1 },
3c2c9 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20  .  { 147, 3 },. 
3c2ca 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b   { 147, 6 },.  {
3c2cb 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 32   147, 6 },.  { 2
3c2cc 34 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 33  42, 1 },.  { 243
3c2cd 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34 33 2c 20  , 0 },.  { 243, 
3c2ce 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20  1 },.  { 147, 1 
3c2cf 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c  },.  { 147, 4 },
3c2d0 0a 20 20 7b 20 32 34 34 2c 20 38 20 7d 2c 0a 20  .  { 244, 8 },. 
3c2d1 20 7b 20 32 34 35 2c 20 31 20 7d 2c 0a 20 20 7b   { 245, 1 },.  {
3c2d2 20 32 34 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   245, 3 },.  { 2
3c2d3 34 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34 36  46, 0 },.  { 246
3c2d4 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 34 37 2c 20  , 2 },.  { 247, 
3c2d5 31 20 7d 2c 0a 20 20 7b 20 32 34 37 2c 20 33 20  1 },.  { 247, 3 
3c2d6 7d 2c 0a 20 20 7b 20 32 34 38 2c 20 31 20 7d 2c  },.  { 248, 1 },
3c2d7 0a 20 20 7b 20 32 34 39 2c 20 30 20 7d 2c 0a 20  .  { 249, 0 },. 
3c2d8 20 7b 20 32 34 39 2c 20 34 20 7d 2c 0a 20 20 7b   { 249, 4 },.  {
3c2d9 20 32 34 39 2c 20 32 20 7d 2c 0a 7d 3b 0a 0a 73   249, 2 },.};..s
3c2da 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 61 63  tatic void yy_ac
3c2db 63 65 70 74 28 79 79 50 61 72 73 65 72 2a 29 3b  cept(yyParser*);
3c2dc 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 44 65 63    /* Forward Dec
3c2dd 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a  laration */../*.
3c2de 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72 65 64  ** Perform a red
3c2df 75 63 65 20 61 63 74 69 6f 6e 20 61 6e 64 20 74  uce action and t
3c2e0 68 65 20 73 68 69 66 74 20 74 68 61 74 20 6d 75  he shift that mu
3c2e1 73 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a  st immediately.*
3c2e2 2a 20 66 6f 6c 6c 6f 77 20 74 68 65 20 72 65 64  * follow the red
3c2e3 75 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  uce..*/.static v
3c2e4 6f 69 64 20 79 79 5f 72 65 64 75 63 65 28 0a 20  oid yy_reduce(. 
3c2e5 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61   yyParser *yypPa
3c2e6 72 73 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  rser,         /*
3c2e7 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20   The parser */. 
3c2e8 20 69 6e 74 20 79 79 72 75 6c 65 6e 6f 20 20 20   int yyruleno   
3c2e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3c2ea 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72   Number of the r
3c2eb 75 6c 65 20 62 79 20 77 68 69 63 68 20 74 6f 20  ule by which to 
3c2ec 72 65 64 75 63 65 20 2a 2f 0a 29 7b 0a 20 20 69  reduce */.){.  i
3c2ed 6e 74 20 79 79 67 6f 74 6f 3b 20 20 20 20 20 20  nt yygoto;      
3c2ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3c2ef 2a 20 54 68 65 20 6e 65 78 74 20 73 74 61 74 65  * The next state
3c2f0 20 2a 2f 0a 20 20 69 6e 74 20 79 79 61 63 74 3b   */.  int yyact;
3c2f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c2f2 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78        /* The nex
3c2f3 74 20 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 59 59  t action */.  YY
3c2f4 4d 49 4e 4f 52 54 59 50 45 20 79 79 67 6f 74 6f  MINORTYPE yygoto
3c2f5 6d 69 6e 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a  minor;        /*
3c2f6 20 54 68 65 20 4c 48 53 20 6f 66 20 74 68 65 20   The LHS of the 
3c2f7 72 75 6c 65 20 72 65 64 75 63 65 64 20 2a 2f 0a  rule reduced */.
3c2f8 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a    yyStackEntry *
3c2f9 79 79 6d 73 70 3b 20 20 20 20 20 20 20 20 20 20  yymsp;          
3c2fa 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f 66 20    /* The top of 
3c2fb 74 68 65 20 70 61 72 73 65 72 27 73 20 73 74 61  the parser's sta
3c2fc 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 79 79 73 69  ck */.  int yysi
3c2fd 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
3c2fe 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e          /* Amoun
3c2ff 74 20 74 6f 20 70 6f 70 20 74 68 65 20 73 74 61  t to pop the sta
3c300 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50  ck */.  sqlite3P
3c301 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a  arserARG_FETCH;.
3c302 20 20 79 79 6d 73 70 20 3d 20 26 79 79 70 50 61    yymsp = &yypPa
3c303 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79  rser->yystack[yy
3c304 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b  pParser->yyidx];
3c305 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
3c306 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c    if( yyTraceFIL
3c307 45 20 26 26 20 79 79 72 75 6c 65 6e 6f 3e 3d 30  E && yyruleno>=0
3c308 20 0a 20 20 20 20 20 20 20 20 26 26 20 79 79 72   .        && yyr
3c309 75 6c 65 6e 6f 3c 28 69 6e 74 29 28 73 69 7a 65  uleno<(int)(size
3c30a 6f 66 28 79 79 52 75 6c 65 4e 61 6d 65 29 2f 73  of(yyRuleName)/s
3c30b 69 7a 65 6f 66 28 79 79 52 75 6c 65 4e 61 6d 65  izeof(yyRuleName
3c30c 5b 30 5d 29 29 20 29 7b 0a 20 20 20 20 66 70 72  [0])) ){.    fpr
3c30d 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
3c30e 2c 20 22 25 73 52 65 64 75 63 65 20 5b 25 73 5d  , "%sReduce [%s]
3c30f 2e 5c 6e 22 2c 20 79 79 54 72 61 63 65 50 72 6f  .\n", yyTracePro
3c310 6d 70 74 2c 0a 20 20 20 20 20 20 79 79 52 75 6c  mpt,.      yyRul
3c311 65 4e 61 6d 65 5b 79 79 72 75 6c 65 6e 6f 5d 29  eName[yyruleno])
3c312 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
3c313 4e 44 45 42 55 47 20 2a 2f 0a 0a 20 20 2f 2a 20  NDEBUG */..  /* 
3c314 53 69 6c 65 6e 63 65 20 63 6f 6d 70 6c 61 69 6e  Silence complain
3c315 74 73 20 66 72 6f 6d 20 70 75 72 69 66 79 20 61  ts from purify a
3c316 62 6f 75 74 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  bout yygotominor
3c317 20 62 65 69 6e 67 20 75 6e 69 6e 69 74 69 61 6c   being uninitial
3c318 69 7a 65 64 0a 20 20 2a 2a 20 69 6e 20 73 6f 6d  ized.  ** in som
3c319 65 20 63 61 73 65 73 20 77 68 65 6e 20 69 74 20  e cases when it 
3c31a 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74  is copied into t
3c31b 68 65 20 73 74 61 63 6b 20 61 66 74 65 72 20 74  he stack after t
3c31c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a  he following.  *
3c31d 2a 20 73 77 69 74 63 68 2e 20 20 79 79 67 6f 74  * switch.  yygot
3c31e 6f 6d 69 6e 6f 72 20 69 73 20 75 6e 69 6e 69 74  ominor is uninit
3c31f 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 61 20 72  ialized when a r
3c320 75 6c 65 20 72 65 64 75 63 65 73 20 74 68 61 74  ule reduces that
3c321 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 73   does.  ** not s
3c322 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  et the value of 
3c323 69 74 73 20 6c 65 66 74 2d 68 61 6e 64 20 73 69  its left-hand si
3c324 64 65 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 2e 20  de nonterminal. 
3c325 20 4c 65 61 76 69 6e 67 20 74 68 65 0a 20 20 2a   Leaving the.  *
3c326 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e  * value of the n
3c327 6f 6e 74 65 72 6d 69 6e 61 6c 20 75 6e 69 6e 69  onterminal unini
3c328 74 69 61 6c 69 7a 65 64 20 69 73 20 75 74 74 65  tialized is utte
3c329 72 6c 79 20 68 61 72 6d 6c 65 73 73 20 61 73 20  rly harmless as 
3c32a 6c 6f 6e 67 0a 20 20 2a 2a 20 61 73 20 74 68 65  long.  ** as the
3c32b 20 76 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20   value is never 
3c32c 75 73 65 64 2e 20 20 53 6f 20 72 65 61 6c 6c 79  used.  So really
3c32d 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20   the only thing 
3c32e 74 68 69 73 20 63 6f 64 65 0a 20 20 2a 2a 20 61  this code.  ** a
3c32f 63 63 6f 6d 70 6c 69 73 68 65 73 20 69 73 20 74  ccomplishes is t
3c330 6f 20 71 75 69 65 74 65 6e 20 70 75 72 69 66 79  o quieten purify
3c331 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 32 30  .  .  **.  ** 20
3c332 30 37 2d 30 31 2d 31 36 3a 20 20 54 68 65 20 77  07-01-16:  The w
3c333 69 72 65 73 68 61 72 6b 20 70 72 6f 6a 65 63 74  ireshark project
3c334 20 28 77 77 77 2e 77 69 72 65 73 68 61 72 6b 2e   (www.wireshark.
3c335 6f 72 67 29 20 72 65 70 6f 72 74 73 20 74 68 61  org) reports tha
3c336 74 0a 20 20 2a 2a 20 77 69 74 68 6f 75 74 20 74  t.  ** without t
3c337 68 69 73 20 63 6f 64 65 2c 20 74 68 65 69 72 20  his code, their 
3c338 70 61 72 73 65 72 20 73 65 67 66 61 75 6c 74 73  parser segfaults
3c339 2e 20 20 49 27 6d 20 6e 6f 74 20 73 75 72 65 20  .  I'm not sure 
3c33a 77 68 61 74 20 74 68 65 72 65 0a 20 20 2a 2a 20  what there.  ** 
3c33b 70 61 72 73 65 72 20 69 73 20 64 6f 69 6e 67 20  parser is doing 
3c33c 74 6f 20 6d 61 6b 65 20 74 68 69 73 20 68 61 70  to make this hap
3c33d 70 65 6e 2e 20 20 54 68 69 73 20 69 73 20 74 68  pen.  This is th
3c33e 65 20 73 65 63 6f 6e 64 20 62 75 67 20 72 65 70  e second bug rep
3c33f 6f 72 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 77 69  ort.  ** from wi
3c340 72 65 73 68 61 72 6b 20 74 68 69 73 20 77 65 65  reshark this wee
3c341 6b 2e 20 20 43 6c 65 61 72 6c 79 20 74 68 65 79  k.  Clearly they
3c342 20 61 72 65 20 73 74 72 65 73 73 69 6e 67 20 4c   are stressing L
3c343 65 6d 6f 6e 20 69 6e 20 77 61 79 73 0a 20 20 2a  emon in ways.  *
3c344 2a 20 74 68 61 74 20 69 74 20 68 61 73 20 6e 6f  * that it has no
3c345 74 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c  t been previousl
3c346 79 20 73 74 72 65 73 73 65 64 2e 2e 2e 20 20 28  y stressed...  (
3c347 53 51 4c 69 74 65 20 74 69 63 6b 65 74 20 23 32  SQLite ticket #2
3c348 31 37 32 29 0a 20 20 2a 2f 0a 20 20 2f 2a 6d 65  172).  */.  /*me
3c349 6d 73 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  mset(&yygotomino
3c34a 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 79 79 67  r, 0, sizeof(yyg
3c34b 6f 74 6f 6d 69 6e 6f 72 29 29 3b 2a 2f 0a 20 20  otominor));*/.  
3c34c 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 3d 20 79 79  yygotominor = yy
3c34d 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 0a 0a 20 20 73  zerominor;...  s
3c34e 77 69 74 63 68 28 20 79 79 72 75 6c 65 6e 6f 20  witch( yyruleno 
3c34f 29 7b 0a 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e  ){.  /* Beginnin
3c350 67 20 68 65 72 65 20 61 72 65 20 74 68 65 20 72  g here are the r
3c351 65 64 75 63 74 69 6f 6e 20 63 61 73 65 73 2e 20  eduction cases. 
3c352 20 41 20 74 79 70 69 63 61 6c 20 65 78 61 6d 70   A typical examp
3c353 6c 65 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 73 3a  le.  ** follows:
3c354 0a 20 20 2a 2a 20 20 20 63 61 73 65 20 30 3a 0a  .  **   case 0:.
3c355 20 20 2a 2a 20 20 23 6c 69 6e 65 20 3c 6c 69 6e    **  #line <lin
3c356 65 6e 6f 3e 20 3c 67 72 61 6d 6d 61 72 66 69 6c  eno> <grammarfil
3c357 65 3e 0a 20 20 2a 2a 20 20 20 20 20 7b 20 2e 2e  e>.  **     { ..
3c358 2e 20 7d 20 20 20 20 20 20 20 20 20 20 20 2f 2f  . }           //
3c359 20 55 73 65 72 20 73 75 70 70 6c 69 65 64 20 63   User supplied c
3c35a 6f 64 65 0a 20 20 2a 2a 20 20 23 6c 69 6e 65 20  ode.  **  #line 
3c35b 3c 6c 69 6e 65 6e 6f 3e 20 3c 74 68 69 73 66 69  <lineno> <thisfi
3c35c 6c 65 3e 0a 20 20 2a 2a 20 20 20 20 20 62 72 65  le>.  **     bre
3c35d 61 6b 3b 0a 20 20 2a 2f 0a 20 20 20 20 20 20 63  ak;.  */.      c
3c35e 61 73 65 20 35 3a 20 2f 2a 20 65 78 70 6c 61 69  ase 5: /* explai
3c35f 6e 20 3a 3a 3d 20 2a 2f 0a 7b 20 73 71 6c 69 74  n ::= */.{ sqlit
3c360 65 33 42 65 67 69 6e 50 61 72 73 65 28 70 50 61  e3BeginParse(pPa
3c361 72 73 65 2c 20 30 29 3b 20 7d 0a 20 20 20 20 20  rse, 0); }.     
3c362 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3c363 63 61 73 65 20 36 3a 20 2f 2a 20 65 78 70 6c 61  case 6: /* expla
3c364 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 2a  in ::= EXPLAIN *
3c365 2f 0a 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e  /.{ sqlite3Begin
3c366 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 31 29  Parse(pParse, 1)
3c367 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
3c368 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 3a  k;.      case 7:
3c369 20 2f 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20   /* explain ::= 
3c36a 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50 4c  EXPLAIN QUERY PL
3c36b 41 4e 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42  AN */.{ sqlite3B
3c36c 65 67 69 6e 50 61 72 73 65 28 70 50 61 72 73 65  eginParse(pParse
3c36d 2c 20 32 29 3b 20 7d 0a 20 20 20 20 20 20 20 20  , 2); }.        
3c36e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3c36f 65 20 38 3a 20 2f 2a 20 63 6d 64 78 20 3a 3a 3d  e 8: /* cmdx ::=
3c370 20 63 6d 64 20 2a 2f 0a 7b 20 73 71 6c 69 74 65   cmd */.{ sqlite
3c371 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 70 50  3FinishCoding(pP
3c372 61 72 73 65 29 3b 20 7d 0a 20 20 20 20 20 20 20  arse); }.       
3c373 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3c374 73 65 20 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d  se 9: /* cmd ::=
3c375 20 42 45 47 49 4e 20 74 72 61 6e 73 74 79 70 65   BEGIN transtype
3c376 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a 7b 73   trans_opt */.{s
3c377 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73  qlite3BeginTrans
3c378 61 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 79  action(pParse, y
3c379 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3c37a 79 33 39 32 29 3b 7d 0a 20 20 20 20 20 20 20 20  y392);}.        
3c37b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3c37c 65 20 31 33 3a 20 2f 2a 20 74 72 61 6e 73 74 79  e 13: /* transty
3c37d 70 65 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  pe ::= */.{yygot
3c37e 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d 20 54  ominor.yy392 = T
3c37f 4b 5f 44 45 46 45 52 52 45 44 3b 7d 0a 20 20 20  K_DEFERRED;}.   
3c380 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3c381 20 20 63 61 73 65 20 31 34 3a 20 2f 2a 20 74 72    case 14: /* tr
3c382 61 6e 73 74 79 70 65 20 3a 3a 3d 20 44 45 46 45  anstype ::= DEFE
3c383 52 52 45 44 20 2a 2f 0a 20 20 20 20 20 20 63 61  RRED */.      ca
3c384 73 65 20 31 35 3a 20 2f 2a 20 74 72 61 6e 73 74  se 15: /* transt
3c385 79 70 65 20 3a 3a 3d 20 49 4d 4d 45 44 49 41 54  ype ::= IMMEDIAT
3c386 45 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  E */ yytestcase(
3c387 79 79 72 75 6c 65 6e 6f 3d 3d 31 35 29 3b 0a 20  yyruleno==15);. 
3c388 20 20 20 20 20 63 61 73 65 20 31 36 3a 20 2f 2a       case 16: /*
3c389 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 45   transtype ::= E
3c38a 58 43 4c 55 53 49 56 45 20 2a 2f 20 79 79 74 65  XCLUSIVE */ yyte
3c38b 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3c38c 3d 31 36 29 3b 0a 20 20 20 20 20 20 63 61 73 65  =16);.      case
3c38d 20 31 31 35 3a 20 2f 2a 20 6d 75 6c 74 69 73 65   115: /* multise
3c38e 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f  lect_op ::= UNIO
3c38f 4e 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  N */ yytestcase(
3c390 79 79 72 75 6c 65 6e 6f 3d 3d 31 31 35 29 3b 0a  yyruleno==115);.
3c391 20 20 20 20 20 20 63 61 73 65 20 31 31 37 3a 20        case 117: 
3c392 2f 2a 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f  /* multiselect_o
3c393 70 20 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54  p ::= EXCEPT|INT
3c394 45 52 53 45 43 54 20 2a 2f 20 79 79 74 65 73 74  ERSECT */ yytest
3c395 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
3c396 31 37 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  17);.{yygotomino
3c397 72 2e 79 79 33 39 32 20 3d 20 79 79 6d 73 70 5b  r.yy392 = yymsp[
3c398 30 5d 2e 6d 61 6a 6f 72 3b 7d 0a 20 20 20 20 20  0].major;}.     
3c399 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3c39a 63 61 73 65 20 31 37 3a 20 2f 2a 20 63 6d 64 20  case 17: /* cmd 
3c39b 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74 72 61 6e 73  ::= COMMIT trans
3c39c 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 20 20 63 61  _opt */.      ca
3c39d 73 65 20 31 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a  se 18: /* cmd ::
3c39e 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f 70 74 20  = END trans_opt 
3c39f 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3c3a0 72 75 6c 65 6e 6f 3d 3d 31 38 29 3b 0a 7b 73 71  ruleno==18);.{sq
3c3a1 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73  lite3CommitTrans
3c3a2 61 63 74 69 6f 6e 28 70 50 61 72 73 65 29 3b 7d  action(pParse);}
3c3a3 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3c3a4 20 20 20 20 20 20 63 61 73 65 20 31 39 3a 20 2f        case 19: /
3c3a5 2a 20 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41  * cmd ::= ROLLBA
3c3a6 43 4b 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a  CK trans_opt */.
3c3a7 7b 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b  {sqlite3Rollback
3c3a8 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 72  Transaction(pPar
3c3a9 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  se);}.        br
3c3aa 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3c3ab 32 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 53  22: /* cmd ::= S
3c3ac 41 56 45 50 4f 49 4e 54 20 6e 6d 20 2a 2f 0a 7b  AVEPOINT nm */.{
3c3ad 0a 20 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f  .  sqlite3Savepo
3c3ae 69 6e 74 28 70 50 61 72 73 65 2c 20 53 41 56 45  int(pParse, SAVE
3c3af 50 4f 49 4e 54 5f 42 45 47 49 4e 2c 20 26 79 79  POINT_BEGIN, &yy
3c3b0 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
3c3b1 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
3c3b2 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
3c3b3 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45  3: /* cmd ::= RE
3c3b4 4c 45 41 53 45 20 73 61 76 65 70 6f 69 6e 74 5f  LEASE savepoint_
3c3b5 6f 70 74 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71  opt nm */.{.  sq
3c3b6 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 70  lite3Savepoint(p
3c3b7 50 61 72 73 65 2c 20 53 41 56 45 50 4f 49 4e 54  Parse, SAVEPOINT
3c3b8 5f 52 45 4c 45 41 53 45 2c 20 26 79 79 6d 73 70  _RELEASE, &yymsp
3c3b9 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
3c3ba 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3c3bb 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 3a 20  .      case 24: 
3c3bc 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42  /* cmd ::= ROLLB
3c3bd 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 20 54 4f  ACK trans_opt TO
3c3be 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e   savepoint_opt n
3c3bf 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  m */.{.  sqlite3
3c3c0 53 61 76 65 70 6f 69 6e 74 28 70 50 61 72 73 65  Savepoint(pParse
3c3c1 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  , SAVEPOINT_ROLL
3c3c2 42 41 43 4b 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  BACK, &yymsp[0].
3c3c3 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
3c3c4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3c3c5 20 20 20 63 61 73 65 20 32 36 3a 20 2f 2a 20 63     case 26: /* c
3c3c6 72 65 61 74 65 5f 74 61 62 6c 65 20 3a 3a 3d 20  reate_table ::= 
3c3c7 63 72 65 61 74 65 6b 77 20 74 65 6d 70 20 54 41  createkw temp TA
3c3c8 42 4c 45 20 69 66 6e 6f 74 65 78 69 73 74 73 20  BLE ifnotexists 
3c3c9 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 0a 20 20 20  nm dbnm */.{.   
3c3ca 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
3c3cb 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  e(pParse,&yymsp[
3c3cc 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -1].minor.yy0,&y
3c3cd 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3c3ce 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  0,yymsp[-4].mino
3c3cf 72 2e 79 79 33 39 32 2c 30 2c 30 2c 79 79 6d 73  r.yy392,0,0,yyms
3c3d0 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39  p[-2].minor.yy39
3c3d1 32 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  2);.}.        br
3c3d2 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3c3d3 32 37 3a 20 2f 2a 20 63 72 65 61 74 65 6b 77 20  27: /* createkw 
3c3d4 3a 3a 3d 20 43 52 45 41 54 45 20 2a 2f 0a 7b 0a  ::= CREATE */.{.
3c3d5 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6c 6f    pParse->db->lo
3c3d6 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
3c3d7 20 3d 20 30 3b 0a 20 20 79 79 67 6f 74 6f 6d 69   = 0;.  yygotomi
3c3d8 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b  nor.yy0 = yymsp[
3c3d9 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 7d 0a  0].minor.yy0;.}.
3c3da 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3c3db 20 20 20 20 20 63 61 73 65 20 32 38 3a 20 2f 2a       case 28: /*
3c3dc 20 69 66 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d   ifnotexists ::=
3c3dd 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
3c3de 31 3a 20 2f 2a 20 74 65 6d 70 20 3a 3a 3d 20 2a  1: /* temp ::= *
3c3df 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3c3e0 75 6c 65 6e 6f 3d 3d 33 31 29 3b 0a 20 20 20 20  uleno==31);.    
3c3e1 20 20 63 61 73 65 20 36 39 3a 20 2f 2a 20 61 75    case 69: /* au
3c3e2 74 6f 69 6e 63 20 3a 3a 3d 20 2a 2f 20 79 79 74  toinc ::= */ yyt
3c3e3 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3c3e4 3d 3d 36 39 29 3b 0a 20 20 20 20 20 20 63 61 73  ==69);.      cas
3c3e5 65 20 38 32 3a 20 2f 2a 20 64 65 66 65 72 5f 73  e 82: /* defer_s
3c3e6 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f 54  ubclause ::= NOT
3c3e7 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69 74   DEFERRABLE init
3c3e8 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f  _deferred_pred_o
3c3e9 70 74 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  pt */ yytestcase
3c3ea 28 79 79 72 75 6c 65 6e 6f 3d 3d 38 32 29 3b 0a  (yyruleno==82);.
3c3eb 20 20 20 20 20 20 63 61 73 65 20 38 34 3a 20 2f        case 84: /
3c3ec 2a 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f  * init_deferred_
3c3ed 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20  pred_opt ::= */ 
3c3ee 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3c3ef 65 6e 6f 3d 3d 38 34 29 3b 0a 20 20 20 20 20 20  eno==84);.      
3c3f0 63 61 73 65 20 38 36 3a 20 2f 2a 20 69 6e 69 74  case 86: /* init
3c3f1 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f  _deferred_pred_o
3c3f2 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59  pt ::= INITIALLY
3c3f3 20 49 4d 4d 45 44 49 41 54 45 20 2a 2f 20 79 79   IMMEDIATE */ yy
3c3f4 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3c3f5 6f 3d 3d 38 36 29 3b 0a 20 20 20 20 20 20 63 61  o==86);.      ca
3c3f6 73 65 20 39 38 3a 20 2f 2a 20 64 65 66 65 72 5f  se 98: /* defer_
3c3f7 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a  subclause_opt ::
3c3f8 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
3c3f9 79 79 72 75 6c 65 6e 6f 3d 3d 39 38 29 3b 0a 20  yyruleno==98);. 
3c3fa 20 20 20 20 20 63 61 73 65 20 31 30 39 3a 20 2f       case 109: /
3c3fb 2a 20 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 2a  * ifexists ::= *
3c3fc 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3c3fd 75 6c 65 6e 6f 3d 3d 31 30 39 29 3b 0a 20 20 20  uleno==109);.   
3c3fe 20 20 20 63 61 73 65 20 31 32 30 3a 20 2f 2a 20     case 120: /* 
3c3ff 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c  distinct ::= ALL
3c400 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3c401 79 72 75 6c 65 6e 6f 3d 3d 31 32 30 29 3b 0a 20  yruleno==120);. 
3c402 20 20 20 20 20 63 61 73 65 20 31 32 31 3a 20 2f       case 121: /
3c403 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 2a  * distinct ::= *
3c404 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3c405 75 6c 65 6e 6f 3d 3d 31 32 31 29 3b 0a 20 20 20  uleno==121);.   
3c406 20 20 20 63 61 73 65 20 32 32 31 3a 20 2f 2a 20     case 221: /* 
3c407 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 42  between_op ::= B
3c408 45 54 57 45 45 4e 20 2a 2f 20 79 79 74 65 73 74  ETWEEN */ yytest
3c409 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
3c40a 32 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  21);.      case 
3c40b 32 32 34 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a  224: /* in_op ::
3c40c 3d 20 49 4e 20 2a 2f 20 79 79 74 65 73 74 63 61  = IN */ yytestca
3c40d 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 32 34  se(yyruleno==224
3c40e 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  );.{yygotominor.
3c40f 79 79 33 39 32 20 3d 20 30 3b 7d 0a 20 20 20 20  yy392 = 0;}.    
3c410 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3c411 20 63 61 73 65 20 32 39 3a 20 2f 2a 20 69 66 6e   case 29: /* ifn
3c412 6f 74 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20  otexists ::= IF 
3c413 4e 4f 54 20 45 58 49 53 54 53 20 2a 2f 0a 20 20  NOT EXISTS */.  
3c414 20 20 20 20 63 61 73 65 20 33 30 3a 20 2f 2a 20      case 30: /* 
3c415 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 20 2a 2f  temp ::= TEMP */
3c416 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3c417 6c 65 6e 6f 3d 3d 33 30 29 3b 0a 20 20 20 20 20  leno==30);.     
3c418 20 63 61 73 65 20 37 30 3a 20 2f 2a 20 61 75 74   case 70: /* aut
3c419 6f 69 6e 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43  oinc ::= AUTOINC
3c41a 52 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  R */ yytestcase(
3c41b 79 79 72 75 6c 65 6e 6f 3d 3d 37 30 29 3b 0a 20  yyruleno==70);. 
3c41c 20 20 20 20 20 63 61 73 65 20 38 35 3a 20 2f 2a       case 85: /*
3c41d 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70   init_deferred_p
3c41e 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54  red_opt ::= INIT
3c41f 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 20 2a  IALLY DEFERRED *
3c420 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3c421 75 6c 65 6e 6f 3d 3d 38 35 29 3b 0a 20 20 20 20  uleno==85);.    
3c422 20 20 63 61 73 65 20 31 30 38 3a 20 2f 2a 20 69    case 108: /* i
3c423 66 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 45  fexists ::= IF E
3c424 58 49 53 54 53 20 2a 2f 20 79 79 74 65 73 74 63  XISTS */ yytestc
3c425 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30  ase(yyruleno==10
3c426 38 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  8);.      case 1
3c427 31 39 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74 20  19: /* distinct 
3c428 3a 3a 3d 20 44 49 53 54 49 4e 43 54 20 2a 2f 20  ::= DISTINCT */ 
3c429 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3c42a 65 6e 6f 3d 3d 31 31 39 29 3b 0a 20 20 20 20 20  eno==119);.     
3c42b 20 63 61 73 65 20 32 32 32 3a 20 2f 2a 20 62 65   case 222: /* be
3c42c 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54  tween_op ::= NOT
3c42d 20 42 45 54 57 45 45 4e 20 2a 2f 20 79 79 74 65   BETWEEN */ yyte
3c42e 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3c42f 3d 32 32 32 29 3b 0a 20 20 20 20 20 20 63 61 73  =222);.      cas
3c430 65 20 32 32 35 3a 20 2f 2a 20 69 6e 5f 6f 70 20  e 225: /* in_op 
3c431 3a 3a 3d 20 4e 4f 54 20 49 4e 20 2a 2f 20 79 79  ::= NOT IN */ yy
3c432 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3c433 6f 3d 3d 32 32 35 29 3b 0a 7b 79 79 67 6f 74 6f  o==225);.{yygoto
3c434 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d 20 31 3b  minor.yy392 = 1;
3c435 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3c436 0a 20 20 20 20 20 20 63 61 73 65 20 33 32 3a 20  .      case 32: 
3c437 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f  /* create_table_
3c438 61 72 67 73 20 3a 3a 3d 20 4c 50 20 63 6f 6c 75  args ::= LP colu
3c439 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c 69 73 74 5f  mnlist conslist_
3c43a 6f 70 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 73 71  opt RP */.{.  sq
3c43b 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50  lite3EndTable(pP
3c43c 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  arse,&yymsp[-1].
3c43d 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
3c43e 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29  [0].minor.yy0,0)
3c43f 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
3c440 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 33  k;.      case 33
3c441 3a 20 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c  : /* create_tabl
3c442 65 5f 61 72 67 73 20 3a 3a 3d 20 41 53 20 73 65  e_args ::= AS se
3c443 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  lect */.{.  sqli
3c444 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61 72  te3EndTable(pPar
3c445 73 65 2c 30 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e  se,0,0,yymsp[0].
3c446 6d 69 6e 6f 72 2e 79 79 31 35 39 29 3b 0a 20 20  minor.yy159);.  
3c447 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
3c448 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
3c449 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3c44a 79 31 35 39 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y159);.}.       
3c44b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3c44c 73 65 20 33 36 3a 20 2f 2a 20 63 6f 6c 75 6d 6e  se 36: /* column
3c44d 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69 64 20 74 79   ::= columnid ty
3c44e 70 65 20 63 61 72 67 6c 69 73 74 20 2a 2f 0a 7b  pe carglist */.{
3c44f 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
3c450 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 32 5d  y0.z = yymsp[-2]
3c451 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20  .minor.yy0.z;.  
3c452 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
3c453 6e 20 3d 20 28 69 6e 74 29 28 70 50 61 72 73 65  n = (int)(pParse
3c454 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 2d 79  ->sLastToken.z-y
3c455 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
3c456 79 30 2e 7a 29 20 2b 20 70 50 61 72 73 65 2d 3e  y0.z) + pParse->
3c457 73 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 7d 0a  sLastToken.n;.}.
3c458 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3c459 20 20 20 20 20 63 61 73 65 20 33 37 3a 20 2f 2a       case 37: /*
3c45a 20 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d   columnid ::= nm
3c45b 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41   */.{.  sqlite3A
3c45c 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c  ddColumn(pParse,
3c45d 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3c45e 79 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  yy0);.  yygotomi
3c45f 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b  nor.yy0 = yymsp[
3c460 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 20 20  0].minor.yy0;.  
3c461 70 50 61 72 73 65 2d 3e 63 6f 6e 73 74 72 61 69  pParse->constrai
3c462 6e 74 4e 61 6d 65 2e 6e 20 3d 20 30 3b 0a 7d 0a  ntName.n = 0;.}.
3c463 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3c464 20 20 20 20 20 63 61 73 65 20 33 38 3a 20 2f 2a       case 38: /*
3c465 20 69 64 20 3a 3a 3d 20 49 44 20 2a 2f 0a 20 20   id ::= ID */.  
3c466 20 20 20 20 63 61 73 65 20 33 39 3a 20 2f 2a 20      case 39: /* 
3c467 69 64 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 2a  id ::= INDEXED *
3c468 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3c469 75 6c 65 6e 6f 3d 3d 33 39 29 3b 0a 20 20 20 20  uleno==39);.    
3c46a 20 20 63 61 73 65 20 34 30 3a 20 2f 2a 20 69 64    case 40: /* id
3c46b 73 20 3a 3a 3d 20 49 44 7c 53 54 52 49 4e 47 20  s ::= ID|STRING 
3c46c 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3c46d 72 75 6c 65 6e 6f 3d 3d 34 30 29 3b 0a 20 20 20  ruleno==40);.   
3c46e 20 20 20 63 61 73 65 20 34 31 3a 20 2f 2a 20 6e     case 41: /* n
3c46f 6d 20 3a 3a 3d 20 69 64 20 2a 2f 20 79 79 74 65  m ::= id */ yyte
3c470 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3c471 3d 34 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65  =41);.      case
3c472 20 34 32 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 53   42: /* nm ::= S
3c473 54 52 49 4e 47 20 2a 2f 20 79 79 74 65 73 74 63  TRING */ yytestc
3c474 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 32  ase(yyruleno==42
3c475 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34 33  );.      case 43
3c476 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 4a 4f 49 4e  : /* nm ::= JOIN
3c477 5f 4b 57 20 2a 2f 20 79 79 74 65 73 74 63 61 73  _KW */ yytestcas
3c478 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 33 29 3b  e(yyruleno==43);
3c479 0a 20 20 20 20 20 20 63 61 73 65 20 34 36 3a 20  .      case 46: 
3c47a 2f 2a 20 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d  /* typetoken ::=
3c47b 20 74 79 70 65 6e 61 6d 65 20 2a 2f 20 79 79 74   typename */ yyt
3c47c 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3c47d 3d 3d 34 36 29 3b 0a 20 20 20 20 20 20 63 61 73  ==46);.      cas
3c47e 65 20 34 39 3a 20 2f 2a 20 74 79 70 65 6e 61 6d  e 49: /* typenam
3c47f 65 20 3a 3a 3d 20 69 64 73 20 2a 2f 20 79 79 74  e ::= ids */ yyt
3c480 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3c481 3d 3d 34 39 29 3b 0a 20 20 20 20 20 20 63 61 73  ==49);.      cas
3c482 65 20 31 32 37 3a 20 2f 2a 20 61 73 20 3a 3a 3d  e 127: /* as ::=
3c483 20 41 53 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74   AS nm */ yytest
3c484 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
3c485 32 37 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  27);.      case 
3c486 31 32 38 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20 69  128: /* as ::= i
3c487 64 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  ds */ yytestcase
3c488 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 32 38 29 3b  (yyruleno==128);
3c489 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 38 3a  .      case 138:
3c48a 20 2f 2a 20 64 62 6e 6d 20 3a 3a 3d 20 44 4f 54   /* dbnm ::= DOT
3c48b 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73   nm */ yytestcas
3c48c 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 33 38 29  e(yyruleno==138)
3c48d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 37  ;.      case 147
3c48e 3a 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70 74  : /* indexed_opt
3c48f 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42 59 20   ::= INDEXED BY 
3c490 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  nm */ yytestcase
3c491 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 34 37 29 3b  (yyruleno==147);
3c492 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 30 3a  .      case 250:
3c493 20 2f 2a 20 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20   /* collate ::= 
3c494 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a 2f 20 79  COLLATE ids */ y
3c495 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3c496 6e 6f 3d 3d 32 35 30 29 3b 0a 20 20 20 20 20 20  no==250);.      
3c497 63 61 73 65 20 32 35 39 3a 20 2f 2a 20 6e 6d 6e  case 259: /* nmn
3c498 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 20  um ::= plus_num 
3c499 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3c49a 72 75 6c 65 6e 6f 3d 3d 32 35 39 29 3b 0a 20 20  ruleno==259);.  
3c49b 20 20 20 20 63 61 73 65 20 32 36 30 3a 20 2f 2a      case 260: /*
3c49c 20 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f   nmnum ::= nm */
3c49d 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3c49e 6c 65 6e 6f 3d 3d 32 36 30 29 3b 0a 20 20 20 20  leno==260);.    
3c49f 20 20 63 61 73 65 20 32 36 31 3a 20 2f 2a 20 6e    case 261: /* n
3c4a0 6d 6e 75 6d 20 3a 3a 3d 20 4f 4e 20 2a 2f 20 79  mnum ::= ON */ y
3c4a1 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3c4a2 6e 6f 3d 3d 32 36 31 29 3b 0a 20 20 20 20 20 20  no==261);.      
3c4a3 63 61 73 65 20 32 36 32 3a 20 2f 2a 20 6e 6d 6e  case 262: /* nmn
3c4a4 75 6d 20 3a 3a 3d 20 44 45 4c 45 54 45 20 2a 2f  um ::= DELETE */
3c4a5 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3c4a6 6c 65 6e 6f 3d 3d 32 36 32 29 3b 0a 20 20 20 20  leno==262);.    
3c4a7 20 20 63 61 73 65 20 32 36 33 3a 20 2f 2a 20 6e    case 263: /* n
3c4a8 6d 6e 75 6d 20 3a 3a 3d 20 44 45 46 41 55 4c 54  mnum ::= DEFAULT
3c4a9 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3c4aa 79 72 75 6c 65 6e 6f 3d 3d 32 36 33 29 3b 0a 20  yruleno==263);. 
3c4ab 20 20 20 20 20 63 61 73 65 20 32 36 34 3a 20 2f       case 264: /
3c4ac 2a 20 70 6c 75 73 5f 6e 75 6d 20 3a 3a 3d 20 50  * plus_num ::= P
3c4ad 4c 55 53 20 6e 75 6d 62 65 72 20 2a 2f 20 79 79  LUS number */ yy
3c4ae 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3c4af 6f 3d 3d 32 36 34 29 3b 0a 20 20 20 20 20 20 63  o==264);.      c
3c4b0 61 73 65 20 32 36 35 3a 20 2f 2a 20 70 6c 75 73  ase 265: /* plus
3c4b1 5f 6e 75 6d 20 3a 3a 3d 20 6e 75 6d 62 65 72 20  _num ::= number 
3c4b2 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3c4b3 72 75 6c 65 6e 6f 3d 3d 32 36 35 29 3b 0a 20 20  ruleno==265);.  
3c4b4 20 20 20 20 63 61 73 65 20 32 36 36 3a 20 2f 2a      case 266: /*
3c4b5 20 6d 69 6e 75 73 5f 6e 75 6d 20 3a 3a 3d 20 4d   minus_num ::= M
3c4b6 49 4e 55 53 20 6e 75 6d 62 65 72 20 2a 2f 20 79  INUS number */ y
3c4b7 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3c4b8 6e 6f 3d 3d 32 36 36 29 3b 0a 20 20 20 20 20 20  no==266);.      
3c4b9 63 61 73 65 20 32 36 37 3a 20 2f 2a 20 6e 75 6d  case 267: /* num
3c4ba 62 65 72 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c  ber ::= INTEGER|
3c4bb 46 4c 4f 41 54 20 2a 2f 20 79 79 74 65 73 74 63  FLOAT */ yytestc
3c4bc 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36  ase(yyruleno==26
3c4bd 37 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  7);.      case 2
3c4be 38 33 3a 20 2f 2a 20 74 72 6e 6d 20 3a 3a 3d 20  83: /* trnm ::= 
3c4bf 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  nm */ yytestcase
3c4c0 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 38 33 29 3b  (yyruleno==283);
3c4c1 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
3c4c2 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0 = yymsp[0].min
3c4c3 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20 20 20 20  or.yy0;}.       
3c4c4 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3c4c5 73 65 20 34 35 3a 20 2f 2a 20 74 79 70 65 20 3a  se 45: /* type :
3c4c6 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 20 2a 2f 0a  := typetoken */.
3c4c7 7b 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d  {sqlite3AddColum
3c4c8 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79 79  nType(pParse,&yy
3c4c9 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
3c4ca 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
3c4cb 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34 37  k;.      case 47
3c4cc 3a 20 2f 2a 20 74 79 70 65 74 6f 6b 65 6e 20 3a  : /* typetoken :
3c4cd 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73  := typename LP s
3c4ce 69 67 6e 65 64 20 52 50 20 2a 2f 0a 7b 0a 20 20  igned RP */.{.  
3c4cf 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
3c4d0 7a 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  z = yymsp[-3].mi
3c4d1 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67  nor.yy0.z;.  yyg
3c4d2 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d  otominor.yy0.n =
3c4d3 20 28 69 6e 74 29 28 26 79 79 6d 73 70 5b 30 5d   (int)(&yymsp[0]
3c4d4 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
3c4d5 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
3c4d6 6e 5d 20 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  n] - yymsp[-3].m
3c4d7 69 6e 6f 72 2e 79 79 30 2e 7a 29 3b 0a 7d 0a 20  inor.yy0.z);.}. 
3c4d8 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3c4d9 20 20 20 20 63 61 73 65 20 34 38 3a 20 2f 2a 20      case 48: /* 
3c4da 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79  typetoken ::= ty
3c4db 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64  pename LP signed
3c4dc 20 43 4f 4d 4d 41 20 73 69 67 6e 65 64 20 52 50   COMMA signed RP
3c4dd 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
3c4de 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 79 79 6d 73  nor.yy0.z = yyms
3c4df 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-5].minor.yy0.
3c4e0 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  z;.  yygotominor
3c4e1 2e 79 79 30 2e 6e 20 3d 20 28 69 6e 74 29 28 26  .yy0.n = (int)(&
3c4e2 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3c4e3 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0.z[yymsp[0].mi
3c4e4 6e 6f 72 2e 79 79 30 2e 6e 5d 20 2d 20 79 79 6d  nor.yy0.n] - yym
3c4e5 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-5].minor.yy0
3c4e6 2e 7a 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  .z);.}.        b
3c4e7 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3c4e8 20 35 30 3a 20 2f 2a 20 74 79 70 65 6e 61 6d 65   50: /* typename
3c4e9 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 69 64   ::= typename id
3c4ea 73 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  s */.{yygotomino
3c4eb 72 2e 79 79 30 2e 7a 3d 79 79 6d 73 70 5b 2d 31  r.yy0.z=yymsp[-1
3c4ec 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 20 79  ].minor.yy0.z; y
3c4ed 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e  ygotominor.yy0.n
3c4ee 3d 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  =yymsp[0].minor.
3c4ef 79 79 30 2e 6e 2b 28 69 6e 74 29 28 79 79 6d 73  yy0.n+(int)(yyms
3c4f0 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
3c4f1 2d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  -yymsp[-1].minor
3c4f2 2e 79 79 30 2e 7a 29 3b 7d 0a 20 20 20 20 20 20  .yy0.z);}.      
3c4f3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3c4f4 61 73 65 20 35 35 3a 20 2f 2a 20 63 63 6f 6e 73  ase 55: /* ccons
3c4f5 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e 54 20   ::= CONSTRAINT 
3c4f6 6e 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  nm */.      case
3c4f7 20 39 33 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a   93: /* tcons ::
3c4f8 3d 20 43 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 20  = CONSTRAINT nm 
3c4f9 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3c4fa 72 75 6c 65 6e 6f 3d 3d 39 33 29 3b 0a 7b 70 50  ruleno==93);.{pP
3c4fb 61 72 73 65 2d 3e 63 6f 6e 73 74 72 61 69 6e 74  arse->constraint
3c4fc 4e 61 6d 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  Name = yymsp[0].
3c4fd 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20  minor.yy0;}.    
3c4fe 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3c4ff 20 63 61 73 65 20 35 36 3a 20 2f 2a 20 63 63 6f   case 56: /* cco
3c500 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 74  ns ::= DEFAULT t
3c501 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  erm */.      cas
3c502 65 20 35 38 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 58: /* ccons :
3c503 3a 3d 20 44 45 46 41 55 4c 54 20 50 4c 55 53 20  := DEFAULT PLUS 
3c504 74 65 72 6d 20 2a 2f 20 79 79 74 65 73 74 63 61  term */ yytestca
3c505 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 38 29  se(yyruleno==58)
3c506 3b 0a 7b 73 71 6c 69 74 65 33 41 64 64 44 65 66  ;.{sqlite3AddDef
3c507 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73 65  aultValue(pParse
3c508 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
3c509 2e 79 79 33 34 32 29 3b 7d 0a 20 20 20 20 20 20  .yy342);}.      
3c50a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3c50b 61 73 65 20 35 37 3a 20 2f 2a 20 63 63 6f 6e 73  ase 57: /* ccons
3c50c 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4c 50 20   ::= DEFAULT LP 
3c50d 65 78 70 72 20 52 50 20 2a 2f 0a 7b 73 71 6c 69  expr RP */.{sqli
3c50e 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c  te3AddDefaultVal
3c50f 75 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  ue(pParse,&yymsp
3c510 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32  [-1].minor.yy342
3c511 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
3c512 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35 39  k;.      case 59
3c513 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44  : /* ccons ::= D
3c514 45 46 41 55 4c 54 20 4d 49 4e 55 53 20 74 65 72  EFAULT MINUS ter
3c515 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 53 70 61  m */.{.  ExprSpa
3c516 6e 20 76 3b 0a 20 20 76 2e 70 45 78 70 72 20 3d  n v;.  v.pExpr =
3c517 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
3c518 61 72 73 65 2c 20 54 4b 5f 55 4d 49 4e 55 53 2c  arse, TK_UMINUS,
3c519 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
3c51a 79 79 33 34 32 2e 70 45 78 70 72 2c 20 30 2c 20  yy342.pExpr, 0, 
3c51b 30 29 3b 0a 20 20 76 2e 7a 53 74 61 72 74 20 3d  0);.  v.zStart =
3c51c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
3c51d 2e 79 79 30 2e 7a 3b 0a 20 20 76 2e 7a 45 6e 64  .yy0.z;.  v.zEnd
3c51e 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
3c51f 72 2e 79 79 33 34 32 2e 7a 45 6e 64 3b 0a 20 20  r.yy342.zEnd;.  
3c520 73 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c  sqlite3AddDefaul
3c521 74 56 61 6c 75 65 28 70 50 61 72 73 65 2c 26 76  tValue(pParse,&v
3c522 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
3c523 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36  ak;.      case 6
3c524 30 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  0: /* ccons ::= 
3c525 44 45 46 41 55 4c 54 20 69 64 20 2a 2f 0a 7b 0a  DEFAULT id */.{.
3c526 20 20 45 78 70 72 53 70 61 6e 20 76 3b 0a 20 20    ExprSpan v;.  
3c527 73 70 61 6e 45 78 70 72 28 26 76 2c 20 70 50 61  spanExpr(&v, pPa
3c528 72 73 65 2c 20 54 4b 5f 53 54 52 49 4e 47 2c 20  rse, TK_STRING, 
3c529 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3c52a 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 41  yy0);.  sqlite3A
3c52b 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70  ddDefaultValue(p
3c52c 50 61 72 73 65 2c 26 76 29 3b 0a 7d 0a 20 20 20  Parse,&v);.}.   
3c52d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3c52e 20 20 63 61 73 65 20 36 32 3a 20 2f 2a 20 63 63    case 62: /* cc
3c52f 6f 6e 73 20 3a 3a 3d 20 4e 4f 54 20 4e 55 4c 4c  ons ::= NOT NULL
3c530 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69   onconf */.{sqli
3c531 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 70 50  te3AddNotNull(pP
3c532 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  arse, yymsp[0].m
3c533 69 6e 6f 72 2e 79 79 33 39 32 29 3b 7d 0a 20 20  inor.yy392);}.  
3c534 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3c535 20 20 20 63 61 73 65 20 36 33 3a 20 2f 2a 20 63     case 63: /* c
3c536 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59  cons ::= PRIMARY
3c537 20 4b 45 59 20 73 6f 72 74 6f 72 64 65 72 20 6f   KEY sortorder o
3c538 6e 63 6f 6e 66 20 61 75 74 6f 69 6e 63 20 2a 2f  nconf autoinc */
3c539 0a 7b 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d  .{sqlite3AddPrim
3c53a 61 72 79 4b 65 79 28 70 50 61 72 73 65 2c 30 2c  aryKey(pParse,0,
3c53b 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3c53c 79 79 33 39 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d  yy392,yymsp[0].m
3c53d 69 6e 6f 72 2e 79 79 33 39 32 2c 79 79 6d 73 70  inor.yy392,yymsp
3c53e 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32  [-2].minor.yy392
3c53f 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
3c540 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 34  k;.      case 64
3c541 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 55  : /* ccons ::= U
3c542 4e 49 51 55 45 20 6f 6e 63 6f 6e 66 20 2a 2f 0a  NIQUE onconf */.
3c543 7b 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e  {sqlite3CreateIn
3c544 64 65 78 28 70 50 61 72 73 65 2c 30 2c 30 2c 30  dex(pParse,0,0,0
3c545 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  ,0,yymsp[0].mino
3c546 72 2e 79 79 33 39 32 2c 30 2c 30 2c 30 2c 30 29  r.yy392,0,0,0,0)
3c547 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3c548 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 35 3a  ;.      case 65:
3c549 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 43 48   /* ccons ::= CH
3c54a 45 43 4b 20 4c 50 20 65 78 70 72 20 52 50 20 2a  ECK LP expr RP *
3c54b 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 43 68 65  /.{sqlite3AddChe
3c54c 63 6b 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61  ckConstraint(pPa
3c54d 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  rse,yymsp[-1].mi
3c54e 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 29  nor.yy342.pExpr)
3c54f 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3c550 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 36 3a  ;.      case 66:
3c551 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 52 45   /* ccons ::= RE
3c552 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c  FERENCES nm idxl
3c553 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20  ist_opt refargs 
3c554 2a 2f 0a 7b 73 71 6c 69 74 65 33 43 72 65 61 74  */.{sqlite3Creat
3c555 65 46 6f 72 65 69 67 6e 4b 65 79 28 70 50 61 72  eForeignKey(pPar
3c556 73 65 2c 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  se,0,&yymsp[-2].
3c557 6d 69 6e 6f 72 2e 79 79 30 2c 79 79 6d 73 70 5b  minor.yy0,yymsp[
3c558 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 2c  -1].minor.yy442,
3c559 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3c55a 79 33 39 32 29 3b 7d 0a 20 20 20 20 20 20 20 20  y392);}.        
3c55b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3c55c 65 20 36 37 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 67: /* ccons :
3c55d 3a 3d 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75  := defer_subclau
3c55e 73 65 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 44 65  se */.{sqlite3De
3c55f 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 70 50  ferForeignKey(pP
3c560 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  arse,yymsp[0].mi
3c561 6e 6f 72 2e 79 79 33 39 32 29 3b 7d 0a 20 20 20  nor.yy392);}.   
3c562 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3c563 20 20 63 61 73 65 20 36 38 3a 20 2f 2a 20 63 63    case 68: /* cc
3c564 6f 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20  ons ::= COLLATE 
3c565 69 64 73 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41  ids */.{sqlite3A
3c566 64 64 43 6f 6c 6c 61 74 65 54 79 70 65 28 70 50  ddCollateType(pP
3c567 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  arse, &yymsp[0].
3c568 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20  minor.yy0);}.   
3c569 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3c56a 20 20 63 61 73 65 20 37 31 3a 20 2f 2a 20 72 65    case 71: /* re
3c56b 66 61 72 67 73 20 3a 3a 3d 20 2a 2f 0a 7b 20 79  fargs ::= */.{ y
3c56c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32  ygotominor.yy392
3c56d 20 3d 20 4f 45 5f 4e 6f 6e 65 2a 30 78 30 31 30   = OE_None*0x010
3c56e 31 3b 20 2f 2a 20 45 56 3a 20 52 2d 31 39 38 30  1; /* EV: R-1980
3c56f 33 2d 34 35 38 38 34 20 2a 2f 7d 0a 20 20 20 20  3-45884 */}.    
3c570 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3c571 20 63 61 73 65 20 37 32 3a 20 2f 2a 20 72 65 66   case 72: /* ref
3c572 61 72 67 73 20 3a 3a 3d 20 72 65 66 61 72 67 73  args ::= refargs
3c573 20 72 65 66 61 72 67 20 2a 2f 0a 7b 20 79 79 67   refarg */.{ yyg
3c574 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d  otominor.yy392 =
3c575 20 28 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f   (yymsp[-1].mino
3c576 72 2e 79 79 33 39 32 20 26 20 7e 79 79 6d 73 70  r.yy392 & ~yymsp
3c577 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 30 37 2e  [0].minor.yy207.
3c578 6d 61 73 6b 29 20 7c 20 79 79 6d 73 70 5b 30 5d  mask) | yymsp[0]
3c579 2e 6d 69 6e 6f 72 2e 79 79 32 30 37 2e 76 61 6c  .minor.yy207.val
3c57a 75 65 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ue; }.        br
3c57b 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3c57c 37 33 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a 3a  73: /* refarg ::
3c57d 3d 20 4d 41 54 43 48 20 6e 6d 20 2a 2f 0a 20 20  = MATCH nm */.  
3c57e 20 20 20 20 63 61 73 65 20 37 34 3a 20 2f 2a 20      case 74: /* 
3c57f 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 49 4e  refarg ::= ON IN
3c580 53 45 52 54 20 72 65 66 61 63 74 20 2a 2f 20 79  SERT refact */ y
3c581 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3c582 6e 6f 3d 3d 37 34 29 3b 0a 7b 20 79 79 67 6f 74  no==74);.{ yygot
3c583 6f 6d 69 6e 6f 72 2e 79 79 32 30 37 2e 76 61 6c  ominor.yy207.val
3c584 75 65 20 3d 20 30 3b 20 20 20 20 20 79 79 67 6f  ue = 0;     yygo
3c585 74 6f 6d 69 6e 6f 72 2e 79 79 32 30 37 2e 6d 61  tominor.yy207.ma
3c586 73 6b 20 3d 20 30 78 30 30 30 30 30 30 3b 20 7d  sk = 0x000000; }
3c587 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3c588 20 20 20 20 20 20 63 61 73 65 20 37 35 3a 20 2f        case 75: /
3c589 2a 20 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20  * refarg ::= ON 
3c58a 44 45 4c 45 54 45 20 72 65 66 61 63 74 20 2a 2f  DELETE refact */
3c58b 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
3c58c 79 32 30 37 2e 76 61 6c 75 65 20 3d 20 79 79 6d  y207.value = yym
3c58d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39  sp[0].minor.yy39
3c58e 32 3b 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  2;     yygotomin
3c58f 6f 72 2e 79 79 32 30 37 2e 6d 61 73 6b 20 3d 20  or.yy207.mask = 
3c590 30 78 30 30 30 30 66 66 3b 20 7d 0a 20 20 20 20  0x0000ff; }.    
3c591 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3c592 20 63 61 73 65 20 37 36 3a 20 2f 2a 20 72 65 66   case 76: /* ref
3c593 61 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54  arg ::= ON UPDAT
3c594 45 20 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79  E refact */.{ yy
3c595 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 30 37 2e  gotominor.yy207.
3c596 76 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d  value = yymsp[0]
3c597 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 3c 3c 38 3b  .minor.yy392<<8;
3c598 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3c599 32 30 37 2e 6d 61 73 6b 20 3d 20 30 78 30 30 66  207.mask = 0x00f
3c59a 66 30 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  f00; }.        b
3c59b 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3c59c 20 37 37 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a   77: /* refact :
3c59d 3a 3d 20 53 45 54 20 4e 55 4c 4c 20 2a 2f 0a 7b  := SET NULL */.{
3c59e 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
3c59f 39 32 20 3d 20 4f 45 5f 53 65 74 4e 75 6c 6c 3b  92 = OE_SetNull;
3c5a0 20 20 2f 2a 20 45 56 3a 20 52 2d 33 33 33 32 36    /* EV: R-33326
3c5a1 2d 34 35 32 35 32 20 2a 2f 7d 0a 20 20 20 20 20  -45252 */}.     
3c5a2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3c5a3 63 61 73 65 20 37 38 3a 20 2f 2a 20 72 65 66 61  case 78: /* refa
3c5a4 63 74 20 3a 3a 3d 20 53 45 54 20 44 45 46 41 55  ct ::= SET DEFAU
3c5a5 4c 54 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  LT */.{ yygotomi
3c5a6 6e 6f 72 2e 79 79 33 39 32 20 3d 20 4f 45 5f 53  nor.yy392 = OE_S
3c5a7 65 74 44 66 6c 74 3b 20 20 2f 2a 20 45 56 3a 20  etDflt;  /* EV: 
3c5a8 52 2d 33 33 33 32 36 2d 34 35 32 35 32 20 2a 2f  R-33326-45252 */
3c5a9 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3c5aa 0a 20 20 20 20 20 20 63 61 73 65 20 37 39 3a 20  .      case 79: 
3c5ab 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 43 41  /* refact ::= CA
3c5ac 53 43 41 44 45 20 2a 2f 0a 7b 20 79 79 67 6f 74  SCADE */.{ yygot
3c5ad 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d 20 4f  ominor.yy392 = O
3c5ae 45 5f 43 61 73 63 61 64 65 3b 20 20 2f 2a 20 45  E_Cascade;  /* E
3c5af 56 3a 20 52 2d 33 33 33 32 36 2d 34 35 32 35 32  V: R-33326-45252
3c5b0 20 2a 2f 7d 0a 20 20 20 20 20 20 20 20 62 72 65   */}.        bre
3c5b1 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38  ak;.      case 8
3c5b2 30 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d  0: /* refact ::=
3c5b3 20 52 45 53 54 52 49 43 54 20 2a 2f 0a 7b 20 79   RESTRICT */.{ y
3c5b4 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32  ygotominor.yy392
3c5b5 20 3d 20 4f 45 5f 52 65 73 74 72 69 63 74 3b 20   = OE_Restrict; 
3c5b6 2f 2a 20 45 56 3a 20 52 2d 33 33 33 32 36 2d 34  /* EV: R-33326-4
3c5b7 35 32 35 32 20 2a 2f 7d 0a 20 20 20 20 20 20 20  5252 */}.       
3c5b8 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3c5b9 73 65 20 38 31 3a 20 2f 2a 20 72 65 66 61 63 74  se 81: /* refact
3c5ba 20 3a 3a 3d 20 4e 4f 20 41 43 54 49 4f 4e 20 2a   ::= NO ACTION *
3c5bb 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
3c5bc 79 79 33 39 32 20 3d 20 4f 45 5f 4e 6f 6e 65 3b  yy392 = OE_None;
3c5bd 20 20 20 20 20 2f 2a 20 45 56 3a 20 52 2d 33 33       /* EV: R-33
3c5be 33 32 36 2d 34 35 32 35 32 20 2a 2f 7d 0a 20 20  326-45252 */}.  
3c5bf 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3c5c0 20 20 20 63 61 73 65 20 38 33 3a 20 2f 2a 20 64     case 83: /* d
3c5c1 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a  efer_subclause :
3c5c2 3a 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e  := DEFERRABLE in
3c5c3 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
3c5c4 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 20 20 63 61  _opt */.      ca
3c5c5 73 65 20 39 39 3a 20 2f 2a 20 64 65 66 65 72 5f  se 99: /* defer_
3c5c6 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a  subclause_opt ::
3c5c7 3d 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73  = defer_subclaus
3c5c8 65 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  e */ yytestcase(
3c5c9 79 79 72 75 6c 65 6e 6f 3d 3d 39 39 29 3b 0a 20  yyruleno==99);. 
3c5ca 20 20 20 20 20 63 61 73 65 20 31 30 31 3a 20 2f       case 101: /
3c5cb 2a 20 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 4f 4e 20  * onconf ::= ON 
3c5cc 43 4f 4e 46 4c 49 43 54 20 72 65 73 6f 6c 76 65  CONFLICT resolve
3c5cd 74 79 70 65 20 2a 2f 20 79 79 74 65 73 74 63 61  type */ yytestca
3c5ce 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 31  se(yyruleno==101
3c5cf 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  );.      case 10
3c5d0 34 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70  4: /* resolvetyp
3c5d1 65 20 3a 3a 3d 20 72 61 69 73 65 74 79 70 65 20  e ::= raisetype 
3c5d2 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3c5d3 72 75 6c 65 6e 6f 3d 3d 31 30 34 29 3b 0a 7b 79  ruleno==104);.{y
3c5d4 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32  ygotominor.yy392
3c5d5 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
3c5d6 72 2e 79 79 33 39 32 3b 7d 0a 20 20 20 20 20 20  r.yy392;}.      
3c5d7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3c5d8 61 73 65 20 38 37 3a 20 2f 2a 20 63 6f 6e 73 6c  ase 87: /* consl
3c5d9 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b  ist_opt ::= */.{
3c5da 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
3c5db 6e 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e  n = 0; yygotomin
3c5dc 6f 72 2e 79 79 30 2e 7a 20 3d 20 30 3b 7d 0a 20  or.yy0.z = 0;}. 
3c5dd 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3c5de 20 20 20 20 63 61 73 65 20 38 38 3a 20 2f 2a 20      case 88: /* 
3c5df 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  conslist_opt ::=
3c5e0 20 43 4f 4d 4d 41 20 63 6f 6e 73 6c 69 73 74 20   COMMA conslist 
3c5e1 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
3c5e2 79 79 30 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e  yy0 = yymsp[-1].
3c5e3 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20  minor.yy0;}.    
3c5e4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3c5e5 20 63 61 73 65 20 39 31 3a 20 2f 2a 20 74 63 6f   case 91: /* tco
3c5e6 6e 73 63 6f 6d 6d 61 20 3a 3a 3d 20 43 4f 4d 4d  nscomma ::= COMM
3c5e7 41 20 2a 2f 0a 7b 70 50 61 72 73 65 2d 3e 63 6f  A */.{pParse->co
3c5e8 6e 73 74 72 61 69 6e 74 4e 61 6d 65 2e 6e 20 3d  nstraintName.n =
3c5e9 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
3c5ea 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39  ak;.      case 9
3c5eb 34 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20  4: /* tcons ::= 
3c5ec 50 52 49 4d 41 52 59 20 4b 45 59 20 4c 50 20 69  PRIMARY KEY LP i
3c5ed 64 78 6c 69 73 74 20 61 75 74 6f 69 6e 63 20 52  dxlist autoinc R
3c5ee 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c  P onconf */.{sql
3c5ef 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65  ite3AddPrimaryKe
3c5f0 79 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  y(pParse,yymsp[-
3c5f1 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 2c 79  3].minor.yy442,y
3c5f2 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3c5f3 33 39 32 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  392,yymsp[-2].mi
3c5f4 6e 6f 72 2e 79 79 33 39 32 2c 30 29 3b 7d 0a 20  nor.yy392,0);}. 
3c5f5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3c5f6 20 20 20 20 63 61 73 65 20 39 35 3a 20 2f 2a 20      case 95: /* 
3c5f7 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45  tcons ::= UNIQUE
3c5f8 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 6f   LP idxlist RP o
3c5f9 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65  nconf */.{sqlite
3c5fa 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61  3CreateIndex(pPa
3c5fb 72 73 65 2c 30 2c 30 2c 30 2c 79 79 6d 73 70 5b  rse,0,0,0,yymsp[
3c5fc 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 2c  -2].minor.yy442,
3c5fd 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3c5fe 79 33 39 32 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a  y392,0,0,0,0);}.
3c5ff 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3c600 20 20 20 20 20 63 61 73 65 20 39 36 3a 20 2f 2a       case 96: /*
3c601 20 74 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b   tcons ::= CHECK
3c602 20 4c 50 20 65 78 70 72 20 52 50 20 6f 6e 63 6f   LP expr RP onco
3c603 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64  nf */.{sqlite3Ad
3c604 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74  dCheckConstraint
3c605 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32  (pParse,yymsp[-2
3c606 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45  ].minor.yy342.pE
3c607 78 70 72 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  xpr);}.        b
3c608 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3c609 20 39 37 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a   97: /* tcons ::
3c60a 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 4c 50  = FOREIGN KEY LP
3c60b 20 69 64 78 6c 69 73 74 20 52 50 20 52 45 46 45   idxlist RP REFE
3c60c 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73  RENCES nm idxlis
3c60d 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20 64 65  t_opt refargs de
3c60e 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70  fer_subclause_op
3c60f 74 20 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c 69 74  t */.{.    sqlit
3c610 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b  e3CreateForeignK
3c611 65 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  ey(pParse, yymsp
3c612 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32  [-6].minor.yy442
3c613 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  , &yymsp[-3].min
3c614 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 32  or.yy0, yymsp[-2
3c615 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 2c 20 79  ].minor.yy442, y
3c616 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3c617 79 33 39 32 29 3b 0a 20 20 20 20 73 71 6c 69 74  y392);.    sqlit
3c618 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65  e3DeferForeignKe
3c619 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  y(pParse, yymsp[
3c61a 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 29 3b  0].minor.yy392);
3c61b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
3c61c 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 30  ;.      case 100
3c61d 3a 20 2f 2a 20 6f 6e 63 6f 6e 66 20 3a 3a 3d 20  : /* onconf ::= 
3c61e 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
3c61f 79 79 33 39 32 20 3d 20 4f 45 5f 44 65 66 61 75  yy392 = OE_Defau
3c620 6c 74 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  lt;}.        bre
3c621 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
3c622 30 32 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a 3a  02: /* orconf ::
3c623 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  = */.{yygotomino
3c624 72 2e 79 79 32 35 38 20 3d 20 4f 45 5f 44 65 66  r.yy258 = OE_Def
3c625 61 75 6c 74 3b 7d 0a 20 20 20 20 20 20 20 20 62  ault;}.        b
3c626 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3c627 20 31 30 33 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20   103: /* orconf 
3c628 3a 3a 3d 20 4f 52 20 72 65 73 6f 6c 76 65 74 79  ::= OR resolvety
3c629 70 65 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  pe */.{yygotomin
3c62a 6f 72 2e 79 79 32 35 38 20 3d 20 28 75 38 29 79  or.yy258 = (u8)y
3c62b 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3c62c 33 39 32 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  392;}.        br
3c62d 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3c62e 31 30 35 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74  105: /* resolvet
3c62f 79 70 65 20 3a 3a 3d 20 49 47 4e 4f 52 45 20 2a  ype ::= IGNORE *
3c630 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
3c631 79 33 39 32 20 3d 20 4f 45 5f 49 67 6e 6f 72 65  y392 = OE_Ignore
3c632 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3c633 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 36  ;.      case 106
3c634 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70 65  : /* resolvetype
3c635 20 3a 3a 3d 20 52 45 50 4c 41 43 45 20 2a 2f 0a   ::= REPLACE */.
3c636 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
3c637 39 32 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b  92 = OE_Replace;
3c638 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3c639 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 37 3a  .      case 107:
3c63a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50   /* cmd ::= DROP
3c63b 20 54 41 42 4c 45 20 69 66 65 78 69 73 74 73 20   TABLE ifexists 
3c63c 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20  fullname */.{.  
3c63d 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65  sqlite3DropTable
3c63e 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30  (pParse, yymsp[0
3c63f 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 37 2c 20 30  ].minor.yy347, 0
3c640 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
3c641 72 2e 79 79 33 39 32 29 3b 0a 7d 0a 20 20 20 20  r.yy392);.}.    
3c642 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3c643 20 63 61 73 65 20 31 31 30 3a 20 2f 2a 20 63 6d   case 110: /* cm
3c644 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74  d ::= createkw t
3c645 65 6d 70 20 56 49 45 57 20 69 66 6e 6f 74 65 78  emp VIEW ifnotex
3c646 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53 20  ists nm dbnm AS 
3c647 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 73 71  select */.{.  sq
3c648 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28  lite3CreateView(
3c649 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d  pParse, &yymsp[-
3c64a 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  7].minor.yy0, &y
3c64b 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
3c64c 79 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  y0, &yymsp[-2].m
3c64d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b  inor.yy0, yymsp[
3c64e 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35 39 2c 20  0].minor.yy159, 
3c64f 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
3c650 79 79 33 39 32 2c 20 79 79 6d 73 70 5b 2d 34 5d  yy392, yymsp[-4]
3c651 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 29 3b 0a 7d  .minor.yy392);.}
3c652 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3c653 20 20 20 20 20 20 63 61 73 65 20 31 31 31 3a 20        case 111: 
3c654 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20  /* cmd ::= DROP 
3c655 56 49 45 57 20 69 66 65 78 69 73 74 73 20 66 75  VIEW ifexists fu
3c656 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71  llname */.{.  sq
3c657 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28 70  lite3DropTable(p
3c658 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e  Parse, yymsp[0].
3c659 6d 69 6e 6f 72 2e 79 79 33 34 37 2c 20 31 2c 20  minor.yy347, 1, 
3c65a 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3c65b 79 79 33 39 32 29 3b 0a 7d 0a 20 20 20 20 20 20  yy392);.}.      
3c65c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3c65d 61 73 65 20 31 31 32 3a 20 2f 2a 20 63 6d 64 20  ase 112: /* cmd 
3c65e 3a 3a 3d 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a  ::= select */.{.
3c65f 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73    SelectDest des
3c660 74 20 3d 20 7b 53 52 54 5f 4f 75 74 70 75 74 2c  t = {SRT_Output,
3c661 20 30 2c 20 30 2c 20 30 2c 20 30 7d 3b 0a 20 20   0, 0, 0, 0};.  
3c662 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
3c663 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  arse, yymsp[0].m
3c664 69 6e 6f 72 2e 79 79 31 35 39 2c 20 26 64 65 73  inor.yy159, &des
3c665 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  t);.  sqlite3Exp
3c666 6c 61 69 6e 42 65 67 69 6e 28 70 50 61 72 73 65  lainBegin(pParse
3c667 2d 3e 70 56 64 62 65 29 3b 0a 20 20 73 71 6c 69  ->pVdbe);.  sqli
3c668 74 65 33 45 78 70 6c 61 69 6e 53 65 6c 65 63 74  te3ExplainSelect
3c669 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20  (pParse->pVdbe, 
3c66a 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3c66b 79 31 35 39 29 3b 0a 20 20 73 71 6c 69 74 65 33  y159);.  sqlite3
3c66c 45 78 70 6c 61 69 6e 46 69 6e 69 73 68 28 70 50  ExplainFinish(pP
3c66d 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a 20 20  arse->pVdbe);.  
3c66e 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
3c66f 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
3c670 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3c671 79 31 35 39 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y159);.}.       
3c672 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3c673 73 65 20 31 31 33 3a 20 2f 2a 20 73 65 6c 65 63  se 113: /* selec
3c674 74 20 3a 3a 3d 20 6f 6e 65 73 65 6c 65 63 74 20  t ::= oneselect 
3c675 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
3c676 79 79 31 35 39 20 3d 20 79 79 6d 73 70 5b 30 5d  yy159 = yymsp[0]
3c677 2e 6d 69 6e 6f 72 2e 79 79 31 35 39 3b 7d 0a 20  .minor.yy159;}. 
3c678 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3c679 20 20 20 20 63 61 73 65 20 31 31 34 3a 20 2f 2a      case 114: /*
3c67a 20 73 65 6c 65 63 74 20 3a 3a 3d 20 73 65 6c 65   select ::= sele
3c67b 63 74 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f  ct multiselect_o
3c67c 70 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a 7b  p oneselect */.{
3c67d 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 30 5d 2e  .  if( yymsp[0].
3c67e 6d 69 6e 6f 72 2e 79 79 31 35 39 20 29 7b 0a 20  minor.yy159 ){. 
3c67f 20 20 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f     yymsp[0].mino
3c680 72 2e 79 79 31 35 39 2d 3e 6f 70 20 3d 20 28 75  r.yy159->op = (u
3c681 38 29 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  8)yymsp[-1].mino
3c682 72 2e 79 79 33 39 32 3b 0a 20 20 20 20 79 79 6d  r.yy392;.    yym
3c683 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35  sp[0].minor.yy15
3c684 39 2d 3e 70 50 72 69 6f 72 20 3d 20 79 79 6d 73  9->pPrior = yyms
3c685 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35  p[-2].minor.yy15
3c686 39 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  9;.  }else{.    
3c687 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
3c688 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
3c689 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
3c68a 79 79 31 35 39 29 3b 0a 20 20 7d 0a 20 20 79 79  yy159);.  }.  yy
3c68b 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 35 39 20  gotominor.yy159 
3c68c 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
3c68d 2e 79 79 31 35 39 3b 0a 7d 0a 20 20 20 20 20 20  .yy159;.}.      
3c68e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3c68f 61 73 65 20 31 31 36 3a 20 2f 2a 20 6d 75 6c 74  ase 116: /* mult
3c690 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55  iselect_op ::= U
3c691 4e 49 4f 4e 20 41 4c 4c 20 2a 2f 0a 7b 79 79 67  NION ALL */.{yyg
3c692 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d  otominor.yy392 =
3c693 20 54 4b 5f 41 4c 4c 3b 7d 0a 20 20 20 20 20 20   TK_ALL;}.      
3c694 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3c695 61 73 65 20 31 31 38 3a 20 2f 2a 20 6f 6e 65 73  ase 118: /* ones
3c696 65 6c 65 63 74 20 3a 3a 3d 20 53 45 4c 45 43 54  elect ::= SELECT
3c697 20 64 69 73 74 69 6e 63 74 20 73 65 6c 63 6f 6c   distinct selcol
3c698 6c 69 73 74 20 66 72 6f 6d 20 77 68 65 72 65 5f  list from where_
3c699 6f 70 74 20 67 72 6f 75 70 62 79 5f 6f 70 74 20  opt groupby_opt 
3c69a 68 61 76 69 6e 67 5f 6f 70 74 20 6f 72 64 65 72  having_opt order
3c69b 62 79 5f 6f 70 74 20 6c 69 6d 69 74 5f 6f 70 74  by_opt limit_opt
3c69c 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
3c69d 6e 6f 72 2e 79 79 31 35 39 20 3d 20 73 71 6c 69  nor.yy159 = sqli
3c69e 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61  te3SelectNew(pPa
3c69f 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69  rse,yymsp[-6].mi
3c6a0 6e 6f 72 2e 79 79 34 34 32 2c 79 79 6d 73 70 5b  nor.yy442,yymsp[
3c6a1 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 37 2c  -5].minor.yy347,
3c6a2 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
3c6a3 79 79 31 32 32 2c 79 79 6d 73 70 5b 2d 33 5d 2e  yy122,yymsp[-3].
3c6a4 6d 69 6e 6f 72 2e 79 79 34 34 32 2c 79 79 6d 73  minor.yy442,yyms
3c6a5 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32  p[-2].minor.yy12
3c6a6 32 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  2,yymsp[-1].mino
3c6a7 72 2e 79 79 34 34 32 2c 79 79 6d 73 70 5b 2d 37  r.yy442,yymsp[-7
3c6a8 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 2c 79 79  ].minor.yy392,yy
3c6a9 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 36  msp[0].minor.yy6
3c6aa 34 2e 70 4c 69 6d 69 74 2c 79 79 6d 73 70 5b 30  4.pLimit,yymsp[0
3c6ab 5d 2e 6d 69 6e 6f 72 2e 79 79 36 34 2e 70 4f 66  ].minor.yy64.pOf
3c6ac 66 73 65 74 29 3b 0a 7d 0a 20 20 20 20 20 20 20  fset);.}.       
3c6ad 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3c6ae 73 65 20 31 32 32 3a 20 2f 2a 20 73 63 6c 70 20  se 122: /* sclp 
3c6af 3a 3a 3d 20 73 65 6c 63 6f 6c 6c 69 73 74 20 43  ::= selcollist C
3c6b0 4f 4d 4d 41 20 2a 2f 0a 20 20 20 20 20 20 63 61  OMMA */.      ca
3c6b1 73 65 20 32 34 36 3a 20 2f 2a 20 69 64 78 6c 69  se 246: /* idxli
3c6b2 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 64  st_opt ::= LP id
3c6b3 78 6c 69 73 74 20 52 50 20 2a 2f 20 79 79 74 65  xlist RP */ yyte
3c6b4 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3c6b5 3d 32 34 36 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69  =246);.{yygotomi
3c6b6 6e 6f 72 2e 79 79 34 34 32 20 3d 20 79 79 6d 73  nor.yy442 = yyms
3c6b7 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34  p[-1].minor.yy44
3c6b8 32 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  2;}.        brea
3c6b9 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32  k;.      case 12
3c6ba 33 3a 20 2f 2a 20 73 63 6c 70 20 3a 3a 3d 20 2a  3: /* sclp ::= *
3c6bb 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 31  /.      case 151
3c6bc 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70 74  : /* orderby_opt
3c6bd 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
3c6be 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35 31  se(yyruleno==151
3c6bf 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 35  );.      case 15
3c6c0 38 3a 20 2f 2a 20 67 72 6f 75 70 62 79 5f 6f 70  8: /* groupby_op
3c6c1 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
3c6c2 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35  ase(yyruleno==15
3c6c3 38 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  8);.      case 2
3c6c4 33 39 3a 20 2f 2a 20 65 78 70 72 6c 69 73 74 20  39: /* exprlist 
3c6c5 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
3c6c6 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 39 29  e(yyruleno==239)
3c6c7 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 35  ;.      case 245
3c6c8 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f 6f 70 74  : /* idxlist_opt
3c6c9 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
3c6ca 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 34 35  se(yyruleno==245
3c6cb 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  );.{yygotominor.
3c6cc 79 79 34 34 32 20 3d 20 30 3b 7d 0a 20 20 20 20  yy442 = 0;}.    
3c6cd 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3c6ce 20 63 61 73 65 20 31 32 34 3a 20 2f 2a 20 73 65   case 124: /* se
3c6cf 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c  lcollist ::= scl
3c6d0 70 20 65 78 70 72 20 61 73 20 2a 2f 0a 7b 0a 20  p expr as */.{. 
3c6d1 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3c6d2 34 34 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70  442 = sqlite3Exp
3c6d3 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
3c6d4 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  se, yymsp[-2].mi
3c6d5 6e 6f 72 2e 79 79 34 34 32 2c 20 79 79 6d 73 70  nor.yy442, yymsp
3c6d6 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32  [-1].minor.yy342
3c6d7 2e 70 45 78 70 72 29 3b 0a 20 20 20 69 66 28 20  .pExpr);.   if( 
3c6d8 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3c6d9 79 30 2e 6e 3e 30 20 29 20 73 71 6c 69 74 65 33  y0.n>0 ) sqlite3
3c6da 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28  ExprListSetName(
3c6db 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69  pParse, yygotomi
3c6dc 6e 6f 72 2e 79 79 34 34 32 2c 20 26 79 79 6d 73  nor.yy442, &yyms
3c6dd 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  p[0].minor.yy0, 
3c6de 31 29 3b 0a 20 20 20 73 71 6c 69 74 65 33 45 78  1);.   sqlite3Ex
3c6df 70 72 4c 69 73 74 53 65 74 53 70 61 6e 28 70 50  prListSetSpan(pP
3c6e0 61 72 73 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72  arse,yygotominor
3c6e1 2e 79 79 34 34 32 2c 26 79 79 6d 73 70 5b 2d 31  .yy442,&yymsp[-1
3c6e2 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 29 3b 0a  ].minor.yy342);.
3c6e3 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3c6e4 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 35 3a  .      case 125:
3c6e5 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a   /* selcollist :
3c6e6 3a 3d 20 73 63 6c 70 20 53 54 41 52 20 2a 2f 0a  := sclp STAR */.
3c6e7 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 73 71  {.  Expr *p = sq
3c6e8 6c 69 74 65 33 45 78 70 72 28 70 50 61 72 73 65  lite3Expr(pParse
3c6e9 2d 3e 64 62 2c 20 54 4b 5f 41 4c 4c 2c 20 30 29  ->db, TK_ALL, 0)
3c6ea 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
3c6eb 79 79 34 34 32 20 3d 20 73 71 6c 69 74 65 33 45  yy442 = sqlite3E
3c6ec 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
3c6ed 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  arse, yymsp[-1].
3c6ee 6d 69 6e 6f 72 2e 79 79 34 34 32 2c 20 70 29 3b  minor.yy442, p);
3c6ef 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
3c6f0 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 36  ;.      case 126
3c6f1 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20  : /* selcollist 
3c6f2 3a 3a 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54 20  ::= sclp nm DOT 
3c6f3 53 54 41 52 20 2a 2f 0a 7b 0a 20 20 45 78 70 72  STAR */.{.  Expr
3c6f4 20 2a 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74   *pRight = sqlit
3c6f5 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
3c6f6 54 4b 5f 41 4c 4c 2c 20 30 2c 20 30 2c 20 26 79  TK_ALL, 0, 0, &y
3c6f7 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3c6f8 30 29 3b 0a 20 20 45 78 70 72 20 2a 70 4c 65 66  0);.  Expr *pLef
3c6f9 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  t = sqlite3PExpr
3c6fa 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
3c6fb 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  0, 0, &yymsp[-2]
3c6fc 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45  .minor.yy0);.  E
3c6fd 78 70 72 20 2a 70 44 6f 74 20 3d 20 73 71 6c 69  xpr *pDot = sqli
3c6fe 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
3c6ff 20 54 4b 5f 44 4f 54 2c 20 70 4c 65 66 74 2c 20   TK_DOT, pLeft, 
3c700 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 79 79  pRight, 0);.  yy
3c701 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34 32 20  gotominor.yy442 
3c702 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
3c703 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79  tAppend(pParse,y
3c704 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
3c705 79 34 34 32 2c 20 70 44 6f 74 29 3b 0a 7d 0a 20  y442, pDot);.}. 
3c706 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3c707 20 20 20 20 63 61 73 65 20 31 32 39 3a 20 2f 2a      case 129: /*
3c708 20 61 73 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f   as ::= */.{yygo
3c709 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20  tominor.yy0.n = 
3c70a 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
3c70b 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
3c70c 30 3a 20 2f 2a 20 66 72 6f 6d 20 3a 3a 3d 20 2a  0: /* from ::= *
3c70d 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
3c70e 79 33 34 37 20 3d 20 73 71 6c 69 74 65 33 44 62  y347 = sqlite3Db
3c70f 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73  MallocZero(pPars
3c710 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a 79  e->db, sizeof(*y
3c711 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 37  ygotominor.yy347
3c712 29 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ));}.        bre
3c713 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
3c714 33 31 3a 20 2f 2a 20 66 72 6f 6d 20 3a 3a 3d 20  31: /* from ::= 
3c715 46 52 4f 4d 20 73 65 6c 74 61 62 6c 69 73 74 20  FROM seltablist 
3c716 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
3c717 6f 72 2e 79 79 33 34 37 20 3d 20 79 79 6d 73 70  or.yy347 = yymsp
3c718 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 37 3b  [0].minor.yy347;
3c719 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
3c71a 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 79  tShiftJoinType(y
3c71b 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 37  ygotominor.yy347
3c71c 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
3c71d 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
3c71e 33 32 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69  32: /* stl_prefi
3c71f 78 20 3a 3a 3d 20 73 65 6c 74 61 62 6c 69 73 74  x ::= seltablist
3c720 20 6a 6f 69 6e 6f 70 20 2a 2f 0a 7b 0a 20 20 20   joinop */.{.   
3c721 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3c722 37 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  7 = yymsp[-1].mi
3c723 6e 6f 72 2e 79 79 33 34 37 3b 0a 20 20 20 69 66  nor.yy347;.   if
3c724 28 20 41 4c 57 41 59 53 28 79 79 67 6f 74 6f 6d  ( ALWAYS(yygotom
3c725 69 6e 6f 72 2e 79 79 33 34 37 20 26 26 20 79 79  inor.yy347 && yy
3c726 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 37 2d  gotominor.yy347-
3c727 3e 6e 53 72 63 3e 30 29 20 29 20 79 79 67 6f 74  >nSrc>0) ) yygot
3c728 6f 6d 69 6e 6f 72 2e 79 79 33 34 37 2d 3e 61 5b  ominor.yy347->a[
3c729 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3c72a 37 2d 3e 6e 53 72 63 2d 31 5d 2e 6a 6f 69 6e 74  7->nSrc-1].joint
3c72b 79 70 65 20 3d 20 28 75 38 29 79 79 6d 73 70 5b  ype = (u8)yymsp[
3c72c 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 3b 0a  0].minor.yy392;.
3c72d 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3c72e 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 33 3a  .      case 133:
3c72f 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69 78 20 3a   /* stl_prefix :
3c730 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  := */.{yygotomin
3c731 6f 72 2e 79 79 33 34 37 20 3d 20 30 3b 7d 0a 20  or.yy347 = 0;}. 
3c732 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3c733 20 20 20 20 63 61 73 65 20 31 33 34 3a 20 2f 2a      case 134: /*
3c734 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20   seltablist ::= 
3c735 73 74 6c 5f 70 72 65 66 69 78 20 6e 6d 20 64 62  stl_prefix nm db
3c736 6e 6d 20 61 73 20 69 6e 64 65 78 65 64 5f 6f 70  nm as indexed_op
3c737 74 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f  t on_opt using_o
3c738 70 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  pt */.{.  yygoto
3c739 6d 69 6e 6f 72 2e 79 79 33 34 37 20 3d 20 73 71  minor.yy347 = sq
3c73a 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
3c73b 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72 73  ndFromTerm(pPars
3c73c 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f  e,yymsp[-6].mino
3c73d 72 2e 79 79 33 34 37 2c 26 79 79 6d 73 70 5b 2d  r.yy347,&yymsp[-
3c73e 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  5].minor.yy0,&yy
3c73f 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
3c740 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  0,&yymsp[-3].min
3c741 6f 72 2e 79 79 30 2c 30 2c 79 79 6d 73 70 5b 2d  or.yy0,0,yymsp[-
3c742 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 79  1].minor.yy122,y
3c743 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3c744 31 38 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  180);.  sqlite3S
3c745 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28  rcListIndexedBy(
3c746 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69  pParse, yygotomi
3c747 6e 6f 72 2e 79 79 33 34 37 2c 20 26 79 79 6d 73  nor.yy347, &yyms
3c748 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-2].minor.yy0)
3c749 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
3c74a 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
3c74b 35 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74  5: /* seltablist
3c74c 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20   ::= stl_prefix 
3c74d 4c 50 20 73 65 6c 65 63 74 20 52 50 20 61 73 20  LP select RP as 
3c74e 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74  on_opt using_opt
3c74f 20 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f   */.{.    yygoto
3c750 6d 69 6e 6f 72 2e 79 79 33 34 37 20 3d 20 73 71  minor.yy347 = sq
3c751 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
3c752 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72 73  ndFromTerm(pPars
3c753 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f  e,yymsp[-6].mino
3c754 72 2e 79 79 33 34 37 2c 30 2c 30 2c 26 79 79 6d  r.yy347,0,0,&yym
3c755 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
3c756 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  ,yymsp[-4].minor
3c757 2e 79 79 31 35 39 2c 79 79 6d 73 70 5b 2d 31 5d  .yy159,yymsp[-1]
3c758 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 79 79 6d  .minor.yy122,yym
3c759 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38  sp[0].minor.yy18
3c75a 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20  0);.  }.        
3c75b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3c75c 65 20 31 33 36 3a 20 2f 2a 20 73 65 6c 74 61 62  e 136: /* seltab
3c75d 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65  list ::= stl_pre
3c75e 66 69 78 20 4c 50 20 73 65 6c 74 61 62 6c 69 73  fix LP seltablis
3c75f 74 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20 75  t RP as on_opt u
3c760 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20  sing_opt */.{.  
3c761 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 36 5d 2e    if( yymsp[-6].
3c762 6d 69 6e 6f 72 2e 79 79 33 34 37 3d 3d 30 20 26  minor.yy347==0 &
3c763 26 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  & yymsp[-2].mino
3c764 72 2e 79 79 30 2e 6e 3d 3d 30 20 26 26 20 79 79  r.yy0.n==0 && yy
3c765 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
3c766 31 32 32 3d 3d 30 20 26 26 20 79 79 6d 73 70 5b  122==0 && yymsp[
3c767 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 30 3d 3d  0].minor.yy180==
3c768 30 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74  0 ){.      yygot
3c769 6f 6d 69 6e 6f 72 2e 79 79 33 34 37 20 3d 20 79  ominor.yy347 = y
3c76a 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
3c76b 79 33 34 37 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  y347;.    }else{
3c76c 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70  .      Select *p
3c76d 53 75 62 71 75 65 72 79 3b 0a 20 20 20 20 20 20  Subquery;.      
3c76e 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68  sqlite3SrcListSh
3c76f 69 66 74 4a 6f 69 6e 54 79 70 65 28 79 79 6d 73  iftJoinType(yyms
3c770 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-4].minor.yy34
3c771 37 29 3b 0a 20 20 20 20 20 20 70 53 75 62 71 75  7);.      pSubqu
3c772 65 72 79 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ery = sqlite3Sel
3c773 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c 30 2c  ectNew(pParse,0,
3c774 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
3c775 79 79 33 34 37 2c 30 2c 30 2c 30 2c 30 2c 30 2c  yy347,0,0,0,0,0,
3c776 30 2c 30 29 3b 0a 20 20 20 20 20 20 79 79 67 6f  0,0);.      yygo
3c777 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 37 20 3d 20  tominor.yy347 = 
3c778 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
3c779 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61  pendFromTerm(pPa
3c77a 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69  rse,yymsp[-6].mi
3c77b 6e 6f 72 2e 79 79 33 34 37 2c 30 2c 30 2c 26 79  nor.yy347,0,0,&y
3c77c 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
3c77d 79 30 2c 70 53 75 62 71 75 65 72 79 2c 79 79 6d  y0,pSubquery,yym
3c77e 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
3c77f 32 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  22,yymsp[0].mino
3c780 72 2e 79 79 31 38 30 29 3b 0a 20 20 20 20 7d 0a  r.yy180);.    }.
3c781 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
3c782 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
3c783 37 3a 20 2f 2a 20 64 62 6e 6d 20 3a 3a 3d 20 2a  7: /* dbnm ::= *
3c784 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 36  /.      case 146
3c785 3a 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70 74  : /* indexed_opt
3c786 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
3c787 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 34 36  se(yyruleno==146
3c788 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  );.{yygotominor.
3c789 79 79 30 2e 7a 3d 30 3b 20 79 79 67 6f 74 6f 6d  yy0.z=0; yygotom
3c78a 69 6e 6f 72 2e 79 79 30 2e 6e 3d 30 3b 7d 0a 20  inor.yy0.n=0;}. 
3c78b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3c78c 20 20 20 20 63 61 73 65 20 31 33 39 3a 20 2f 2a      case 139: /*
3c78d 20 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 6e 6d   fullname ::= nm
3c78e 20 64 62 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f   dbnm */.{yygoto
3c78f 6d 69 6e 6f 72 2e 79 79 33 34 37 20 3d 20 73 71  minor.yy347 = sq
3c790 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
3c791 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c  nd(pParse->db,0,
3c792 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
3c793 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
3c794 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
3c795 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3c796 20 63 61 73 65 20 31 34 30 3a 20 2f 2a 20 6a 6f   case 140: /* jo
3c797 69 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a  inop ::= COMMA|J
3c798 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  OIN */.{ yygotom
3c799 69 6e 6f 72 2e 79 79 33 39 32 20 3d 20 4a 54 5f  inor.yy392 = JT_
3c79a 49 4e 4e 45 52 3b 20 7d 0a 20 20 20 20 20 20 20  INNER; }.       
3c79b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3c79c 73 65 20 31 34 31 3a 20 2f 2a 20 6a 6f 69 6e 6f  se 141: /* joino
3c79d 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a 4f  p ::= JOIN_KW JO
3c79e 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  IN */.{ yygotomi
3c79f 6e 6f 72 2e 79 79 33 39 32 20 3d 20 73 71 6c 69  nor.yy392 = sqli
3c7a0 74 65 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72  te3JoinType(pPar
3c7a1 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  se,&yymsp[-1].mi
3c7a2 6e 6f 72 2e 79 79 30 2c 30 2c 30 29 3b 20 7d 0a  nor.yy0,0,0); }.
3c7a3 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3c7a4 20 20 20 20 20 63 61 73 65 20 31 34 32 3a 20 2f       case 142: /
3c7a5 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49  * joinop ::= JOI
3c7a6 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e 20 2a 2f 0a  N_KW nm JOIN */.
3c7a7 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
3c7a8 33 39 32 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69  392 = sqlite3Joi
3c7a9 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79 79  nType(pParse,&yy
3c7aa 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
3c7ab 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0,&yymsp[-1].min
3c7ac 6f 72 2e 79 79 30 2c 30 29 3b 20 7d 0a 20 20 20  or.yy0,0); }.   
3c7ad 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3c7ae 20 20 63 61 73 65 20 31 34 33 3a 20 2f 2a 20 6a    case 143: /* j
3c7af 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  oinop ::= JOIN_K
3c7b0 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 20 2a 2f 0a  W nm nm JOIN */.
3c7b1 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
3c7b2 33 39 32 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69  392 = sqlite3Joi
3c7b3 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79 79  nType(pParse,&yy
3c7b4 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
3c7b5 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  0,&yymsp[-2].min
3c7b6 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31  or.yy0,&yymsp[-1
3c7b7 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 7d 0a  ].minor.yy0); }.
3c7b8 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3c7b9 20 20 20 20 20 63 61 73 65 20 31 34 34 3a 20 2f       case 144: /
3c7ba 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 4f 4e 20  * on_opt ::= ON 
3c7bb 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  expr */.      ca
3c7bc 73 65 20 31 36 31 3a 20 2f 2a 20 68 61 76 69 6e  se 161: /* havin
3c7bd 67 5f 6f 70 74 20 3a 3a 3d 20 48 41 56 49 4e 47  g_opt ::= HAVING
3c7be 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63   expr */ yytestc
3c7bf 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 36  ase(yyruleno==16
3c7c0 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  1);.      case 1
3c7c1 36 38 3a 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74  68: /* where_opt
3c7c2 20 3a 3a 3d 20 57 48 45 52 45 20 65 78 70 72 20   ::= WHERE expr 
3c7c3 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3c7c4 72 75 6c 65 6e 6f 3d 3d 31 36 38 29 3b 0a 20 20  ruleno==168);.  
3c7c5 20 20 20 20 63 61 73 65 20 32 33 34 3a 20 2f 2a      case 234: /*
3c7c6 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 20 45   case_else ::= E
3c7c7 4c 53 45 20 65 78 70 72 20 2a 2f 20 79 79 74 65  LSE expr */ yyte
3c7c8 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3c7c9 3d 32 33 34 29 3b 0a 20 20 20 20 20 20 63 61 73  =234);.      cas
3c7ca 65 20 32 33 36 3a 20 2f 2a 20 63 61 73 65 5f 6f  e 236: /* case_o
3c7cb 70 65 72 61 6e 64 20 3a 3a 3d 20 65 78 70 72 20  perand ::= expr 
3c7cc 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3c7cd 72 75 6c 65 6e 6f 3d 3d 32 33 36 29 3b 0a 7b 79  ruleno==236);.{y
3c7ce 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32  ygotominor.yy122
3c7cf 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
3c7d0 72 2e 79 79 33 34 32 2e 70 45 78 70 72 3b 7d 0a  r.yy342.pExpr;}.
3c7d1 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3c7d2 20 20 20 20 20 63 61 73 65 20 31 34 35 3a 20 2f       case 145: /
3c7d3 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a  * on_opt ::= */.
3c7d4 20 20 20 20 20 20 63 61 73 65 20 31 36 30 3a 20        case 160: 
3c7d5 2f 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a  /* having_opt ::
3c7d6 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
3c7d7 79 79 72 75 6c 65 6e 6f 3d 3d 31 36 30 29 3b 0a  yyruleno==160);.
3c7d8 20 20 20 20 20 20 63 61 73 65 20 31 36 37 3a 20        case 167: 
3c7d9 2f 2a 20 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d  /* where_opt ::=
3c7da 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3c7db 79 72 75 6c 65 6e 6f 3d 3d 31 36 37 29 3b 0a 20  yruleno==167);. 
3c7dc 20 20 20 20 20 63 61 73 65 20 32 33 35 3a 20 2f       case 235: /
3c7dd 2a 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 20  * case_else ::= 
3c7de 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3c7df 72 75 6c 65 6e 6f 3d 3d 32 33 35 29 3b 0a 20 20  ruleno==235);.  
3c7e0 20 20 20 20 63 61 73 65 20 32 33 37 3a 20 2f 2a      case 237: /*
3c7e1 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a   case_operand ::
3c7e2 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
3c7e3 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 37 29 3b 0a  yyruleno==237);.
3c7e4 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
3c7e5 32 32 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  22 = 0;}.       
3c7e6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3c7e7 73 65 20 31 34 38 3a 20 2f 2a 20 69 6e 64 65 78  se 148: /* index
3c7e8 65 64 5f 6f 70 74 20 3a 3a 3d 20 4e 4f 54 20 49  ed_opt ::= NOT I
3c7e9 4e 44 45 58 45 44 20 2a 2f 0a 7b 79 79 67 6f 74  NDEXED */.{yygot
3c7ea 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d 30 3b 20  ominor.yy0.z=0; 
3c7eb 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
3c7ec 6e 3d 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  n=1;}.        br
3c7ed 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3c7ee 31 34 39 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70  149: /* using_op
3c7ef 74 20 3a 3a 3d 20 55 53 49 4e 47 20 4c 50 20 69  t ::= USING LP i
3c7f0 6e 73 63 6f 6c 6c 69 73 74 20 52 50 20 2a 2f 0a  nscollist RP */.
3c7f1 20 20 20 20 20 20 63 61 73 65 20 31 38 30 3a 20        case 180: 
3c7f2 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  /* inscollist_op
3c7f3 74 20 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f 6c 6c  t ::= LP inscoll
3c7f4 69 73 74 20 52 50 20 2a 2f 20 79 79 74 65 73 74  ist RP */ yytest
3c7f5 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
3c7f6 38 30 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  80);.{yygotomino
3c7f7 72 2e 79 79 31 38 30 20 3d 20 79 79 6d 73 70 5b  r.yy180 = yymsp[
3c7f8 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 30 3b  -1].minor.yy180;
3c7f9 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3c7fa 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 30 3a  .      case 150:
3c7fb 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20 3a 3a   /* using_opt ::
3c7fc 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
3c7fd 31 37 39 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69  179: /* inscolli
3c7fe 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79  st_opt ::= */ yy
3c7ff 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3c800 6f 3d 3d 31 37 39 29 3b 0a 7b 79 79 67 6f 74 6f  o==179);.{yygoto
3c801 6d 69 6e 6f 72 2e 79 79 31 38 30 20 3d 20 30 3b  minor.yy180 = 0;
3c802 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3c803 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 32 3a  .      case 152:
3c804 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70 74 20   /* orderby_opt 
3c805 3a 3a 3d 20 4f 52 44 45 52 20 42 59 20 73 6f 72  ::= ORDER BY sor
3c806 74 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 63  tlist */.      c
3c807 61 73 65 20 31 35 39 3a 20 2f 2a 20 67 72 6f 75  ase 159: /* grou
3c808 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 47 52 4f 55  pby_opt ::= GROU
3c809 50 20 42 59 20 6e 65 78 70 72 6c 69 73 74 20 2a  P BY nexprlist *
3c80a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3c80b 75 6c 65 6e 6f 3d 3d 31 35 39 29 3b 0a 20 20 20  uleno==159);.   
3c80c 20 20 20 63 61 73 65 20 32 33 38 3a 20 2f 2a 20     case 238: /* 
3c80d 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e 65 78  exprlist ::= nex
3c80e 70 72 6c 69 73 74 20 2a 2f 20 79 79 74 65 73 74  prlist */ yytest
3c80f 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
3c810 33 38 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  38);.{yygotomino
3c811 72 2e 79 79 34 34 32 20 3d 20 79 79 6d 73 70 5b  r.yy442 = yymsp[
3c812 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 3b 7d  0].minor.yy442;}
3c813 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3c814 20 20 20 20 20 20 63 61 73 65 20 31 35 33 3a 20        case 153: 
3c815 2f 2a 20 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20  /* sortlist ::= 
3c816 73 6f 72 74 6c 69 73 74 20 43 4f 4d 4d 41 20 65  sortlist COMMA e
3c817 78 70 72 20 73 6f 72 74 6f 72 64 65 72 20 2a 2f  xpr sortorder */
3c818 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
3c819 2e 79 79 34 34 32 20 3d 20 73 71 6c 69 74 65 33  .yy442 = sqlite3
3c81a 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
3c81b 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e  Parse,yymsp[-3].
3c81c 6d 69 6e 6f 72 2e 79 79 34 34 32 2c 79 79 6d 73  minor.yy442,yyms
3c81d 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-1].minor.yy34
3c81e 32 2e 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20  2.pExpr);.  if( 
3c81f 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34  yygotominor.yy44
3c820 32 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  2 ) yygotominor.
3c821 79 79 34 34 32 2d 3e 61 5b 79 79 67 6f 74 6f 6d  yy442->a[yygotom
3c822 69 6e 6f 72 2e 79 79 34 34 32 2d 3e 6e 45 78 70  inor.yy442->nExp
3c823 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d  r-1].sortOrder =
3c824 20 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69   (u8)yymsp[0].mi
3c825 6e 6f 72 2e 79 79 33 39 32 3b 0a 7d 0a 20 20 20  nor.yy392;.}.   
3c826 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3c827 20 20 63 61 73 65 20 31 35 34 3a 20 2f 2a 20 73    case 154: /* s
3c828 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72  ortlist ::= expr
3c829 20 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a   sortorder */.{.
3c82a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3c82b 34 34 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70  442 = sqlite3Exp
3c82c 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
3c82d 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  se,0,yymsp[-1].m
3c82e 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72  inor.yy342.pExpr
3c82f 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  );.  if( yygotom
3c830 69 6e 6f 72 2e 79 79 34 34 32 20 26 26 20 41 4c  inor.yy442 && AL
3c831 57 41 59 53 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  WAYS(yygotominor
3c832 2e 79 79 34 34 32 2d 3e 61 29 20 29 20 79 79 67  .yy442->a) ) yyg
3c833 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34 32 2d 3e  otominor.yy442->
3c834 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d  a[0].sortOrder =
3c835 20 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69   (u8)yymsp[0].mi
3c836 6e 6f 72 2e 79 79 33 39 32 3b 0a 7d 0a 20 20 20  nor.yy392;.}.   
3c837 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3c838 20 20 63 61 73 65 20 31 35 35 3a 20 2f 2a 20 73    case 155: /* s
3c839 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 41 53 43  ortorder ::= ASC
3c83a 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
3c83b 35 37 3a 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72  57: /* sortorder
3c83c 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
3c83d 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35 37  se(yyruleno==157
3c83e 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  );.{yygotominor.
3c83f 79 79 33 39 32 20 3d 20 53 51 4c 49 54 45 5f 53  yy392 = SQLITE_S
3c840 4f 5f 41 53 43 3b 7d 0a 20 20 20 20 20 20 20 20  O_ASC;}.        
3c841 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3c842 65 20 31 35 36 3a 20 2f 2a 20 73 6f 72 74 6f 72  e 156: /* sortor
3c843 64 65 72 20 3a 3a 3d 20 44 45 53 43 20 2a 2f 0a  der ::= DESC */.
3c844 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
3c845 39 32 20 3d 20 53 51 4c 49 54 45 5f 53 4f 5f 44  92 = SQLITE_SO_D
3c846 45 53 43 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ESC;}.        br
3c847 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3c848 31 36 32 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70  162: /* limit_op
3c849 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f  t ::= */.{yygoto
3c84a 6d 69 6e 6f 72 2e 79 79 36 34 2e 70 4c 69 6d 69  minor.yy64.pLimi
3c84b 74 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e  t = 0; yygotomin
3c84c 6f 72 2e 79 79 36 34 2e 70 4f 66 66 73 65 74 20  or.yy64.pOffset 
3c84d 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 0;}.        br
3c84e 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3c84f 31 36 33 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70  163: /* limit_op
3c850 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72  t ::= LIMIT expr
3c851 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
3c852 2e 79 79 36 34 2e 70 4c 69 6d 69 74 20 3d 20 79  .yy64.pLimit = y
3c853 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3c854 33 34 32 2e 70 45 78 70 72 3b 20 79 79 67 6f 74  342.pExpr; yygot
3c855 6f 6d 69 6e 6f 72 2e 79 79 36 34 2e 70 4f 66 66  ominor.yy64.pOff
3c856 73 65 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20  set = 0;}.      
3c857 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3c858 61 73 65 20 31 36 34 3a 20 2f 2a 20 6c 69 6d 69  ase 164: /* limi
3c859 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20  t_opt ::= LIMIT 
3c85a 65 78 70 72 20 4f 46 46 53 45 54 20 65 78 70 72  expr OFFSET expr
3c85b 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
3c85c 2e 79 79 36 34 2e 70 4c 69 6d 69 74 20 3d 20 79  .yy64.pLimit = y
3c85d 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
3c85e 79 33 34 32 2e 70 45 78 70 72 3b 20 79 79 67 6f  y342.pExpr; yygo
3c85f 74 6f 6d 69 6e 6f 72 2e 79 79 36 34 2e 70 4f 66  tominor.yy64.pOf
3c860 66 73 65 74 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  fset = yymsp[0].
3c861 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70  minor.yy342.pExp
3c862 72 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  r;}.        brea
3c863 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36  k;.      case 16
3c864 35 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20  5: /* limit_opt 
3c865 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 43  ::= LIMIT expr C
3c866 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 0a 7b 79 79  OMMA expr */.{yy
3c867 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 34 2e 70  gotominor.yy64.p
3c868 4f 66 66 73 65 74 20 3d 20 79 79 6d 73 70 5b 2d  Offset = yymsp[-
3c869 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70  2].minor.yy342.p
3c86a 45 78 70 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  Expr; yygotomino
3c86b 72 2e 79 79 36 34 2e 70 4c 69 6d 69 74 20 3d 20  r.yy64.pLimit = 
3c86c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3c86d 79 33 34 32 2e 70 45 78 70 72 3b 7d 0a 20 20 20  y342.pExpr;}.   
3c86e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3c86f 20 20 63 61 73 65 20 31 36 36 3a 20 2f 2a 20 63    case 166: /* c
3c870 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52  md ::= DELETE FR
3c871 4f 4d 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65  OM fullname inde
3c872 78 65 64 5f 6f 70 74 20 77 68 65 72 65 5f 6f 70  xed_opt where_op
3c873 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  t */.{.  sqlite3
3c874 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79  SrcListIndexedBy
3c875 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
3c876 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 37 2c 20  2].minor.yy347, 
3c877 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
3c878 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  .yy0);.  sqlite3
3c879 44 65 6c 65 74 65 46 72 6f 6d 28 70 50 61 72 73  DeleteFrom(pPars
3c87a 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  e,yymsp[-2].mino
3c87b 72 2e 79 79 33 34 37 2c 79 79 6d 73 70 5b 30 5d  r.yy347,yymsp[0]
3c87c 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 29 3b 0a 7d  .minor.yy122);.}
3c87d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3c87e 20 20 20 20 20 20 63 61 73 65 20 31 36 39 3a 20        case 169: 
3c87f 2f 2a 20 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54  /* cmd ::= UPDAT
3c880 45 20 6f 72 63 6f 6e 66 20 66 75 6c 6c 6e 61 6d  E orconf fullnam
3c881 65 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 53 45  e indexed_opt SE
3c882 54 20 73 65 74 6c 69 73 74 20 77 68 65 72 65 5f  T setlist where_
3c883 6f 70 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74  opt */.{.  sqlit
3c884 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64  e3SrcListIndexed
3c885 42 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  By(pParse, yymsp
3c886 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 37  [-4].minor.yy347
3c887 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  , &yymsp[-3].min
3c888 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74  or.yy0);.  sqlit
3c889 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c  e3ExprListCheckL
3c88a 65 6e 67 74 68 28 70 50 61 72 73 65 2c 79 79 6d  ength(pParse,yym
3c88b 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
3c88c 34 32 2c 22 73 65 74 20 6c 69 73 74 22 29 3b 20  42,"set list"); 
3c88d 0a 20 20 73 71 6c 69 74 65 33 55 70 64 61 74 65  .  sqlite3Update
3c88e 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 34  (pParse,yymsp[-4
3c88f 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 37 2c 79 79  ].minor.yy347,yy
3c890 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
3c891 34 34 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  442,yymsp[0].min
3c892 6f 72 2e 79 79 31 32 32 2c 79 79 6d 73 70 5b 2d  or.yy122,yymsp[-
3c893 35 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 38 29 3b  5].minor.yy258);
3c894 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
3c895 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 30  ;.      case 170
3c896 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20 3a 3a 3d  : /* setlist ::=
3c897 20 73 65 74 6c 69 73 74 20 43 4f 4d 4d 41 20 6e   setlist COMMA n
3c898 6d 20 45 51 20 65 78 70 72 20 2a 2f 0a 7b 0a 20  m EQ expr */.{. 
3c899 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
3c89a 34 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  42 = sqlite3Expr
3c89b 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
3c89c 65 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  e, yymsp[-4].min
3c89d 6f 72 2e 79 79 34 34 32 2c 20 79 79 6d 73 70 5b  or.yy442, yymsp[
3c89e 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70  0].minor.yy342.p
3c89f 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  Expr);.  sqlite3
3c8a0 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28  ExprListSetName(
3c8a1 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69  pParse, yygotomi
3c8a2 6e 6f 72 2e 79 79 34 34 32 2c 20 26 79 79 6d 73  nor.yy442, &yyms
3c8a3 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
3c8a4 20 31 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62   1);.}.        b
3c8a5 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3c8a6 20 31 37 31 3a 20 2f 2a 20 73 65 74 6c 69 73 74   171: /* setlist
3c8a7 20 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70 72 20   ::= nm EQ expr 
3c8a8 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
3c8a9 6f 72 2e 79 79 34 34 32 20 3d 20 73 71 6c 69 74  or.yy442 = sqlit
3c8aa 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
3c8ab 28 70 50 61 72 73 65 2c 20 30 2c 20 79 79 6d 73  (pParse, 0, yyms
3c8ac 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32  p[0].minor.yy342
3c8ad 2e 70 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74  .pExpr);.  sqlit
3c8ae 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d  e3ExprListSetNam
3c8af 65 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f  e(pParse, yygoto
3c8b0 6d 69 6e 6f 72 2e 79 79 34 34 32 2c 20 26 79 79  minor.yy442, &yy
3c8b1 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
3c8b2 30 2c 20 31 29 3b 0a 7d 0a 20 20 20 20 20 20 20  0, 1);.}.       
3c8b3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3c8b4 73 65 20 31 37 32 3a 20 2f 2a 20 63 6d 64 20 3a  se 172: /* cmd :
3c8b5 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
3c8b6 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63  TO fullname insc
3c8b7 6f 6c 6c 69 73 74 5f 6f 70 74 20 76 61 6c 75 65  ollist_opt value
3c8b8 6c 69 73 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  list */.{sqlite3
3c8b9 49 6e 73 65 72 74 28 70 50 61 72 73 65 2c 20 79  Insert(pParse, y
3c8ba 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
3c8bb 79 33 34 37 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  y347, yymsp[0].m
3c8bc 69 6e 6f 72 2e 79 79 34 38 37 2e 70 4c 69 73 74  inor.yy487.pList
3c8bd 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
3c8be 2e 79 79 34 38 37 2e 70 53 65 6c 65 63 74 2c 20  .yy487.pSelect, 
3c8bf 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3c8c0 79 79 31 38 30 2c 20 79 79 6d 73 70 5b 2d 34 5d  yy180, yymsp[-4]
3c8c1 2e 6d 69 6e 6f 72 2e 79 79 32 35 38 29 3b 7d 0a  .minor.yy258);}.
3c8c2 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3c8c3 20 20 20 20 20 63 61 73 65 20 31 37 33 3a 20 2f       case 173: /
3c8c4 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74  * cmd ::= insert
3c8c5 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61  _cmd INTO fullna
3c8c6 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  me inscollist_op
3c8c7 74 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 73 71 6c  t select */.{sql
3c8c8 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72 73  ite3Insert(pPars
3c8c9 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  e, yymsp[-2].min
3c8ca 6f 72 2e 79 79 33 34 37 2c 20 30 2c 20 79 79 6d  or.yy347, 0, yym
3c8cb 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35  sp[0].minor.yy15
3c8cc 39 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  9, yymsp[-1].min
3c8cd 6f 72 2e 79 79 31 38 30 2c 20 79 79 6d 73 70 5b  or.yy180, yymsp[
3c8ce 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 38 29  -4].minor.yy258)
3c8cf 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3c8d0 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 34  ;.      case 174
3c8d1 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e 73  : /* cmd ::= ins
3c8d2 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c  ert_cmd INTO ful
3c8d3 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74  lname inscollist
3c8d4 5f 6f 70 74 20 44 45 46 41 55 4c 54 20 56 41 4c  _opt DEFAULT VAL
3c8d5 55 45 53 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 49  UES */.{sqlite3I
3c8d6 6e 73 65 72 74 28 70 50 61 72 73 65 2c 20 79 79  nsert(pParse, yy
3c8d7 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
3c8d8 33 34 37 2c 20 30 2c 20 30 2c 20 79 79 6d 73 70  347, 0, 0, yymsp
3c8d9 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 30  [-2].minor.yy180
3c8da 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  , yymsp[-5].mino
3c8db 72 2e 79 79 32 35 38 29 3b 7d 0a 20 20 20 20 20  r.yy258);}.     
3c8dc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3c8dd 63 61 73 65 20 31 37 35 3a 20 2f 2a 20 69 6e 73  case 175: /* ins
3c8de 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 49 4e 53 45  ert_cmd ::= INSE
3c8df 52 54 20 6f 72 63 6f 6e 66 20 2a 2f 0a 7b 79 79  RT orconf */.{yy
3c8e0 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 35 38 20  gotominor.yy258 
3c8e1 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
3c8e2 2e 79 79 32 35 38 3b 7d 0a 20 20 20 20 20 20 20  .yy258;}.       
3c8e3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3c8e4 73 65 20 31 37 36 3a 20 2f 2a 20 69 6e 73 65 72  se 176: /* inser
3c8e5 74 5f 63 6d 64 20 3a 3a 3d 20 52 45 50 4c 41 43  t_cmd ::= REPLAC
3c8e6 45 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  E */.{yygotomino
3c8e7 72 2e 79 79 32 35 38 20 3d 20 4f 45 5f 52 65 70  r.yy258 = OE_Rep
3c8e8 6c 61 63 65 3b 7d 0a 20 20 20 20 20 20 20 20 62  lace;}.        b
3c8e9 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3c8ea 20 31 37 37 3a 20 2f 2a 20 76 61 6c 75 65 6c 69   177: /* valueli
3c8eb 73 74 20 3a 3a 3d 20 56 41 4c 55 45 53 20 4c 50  st ::= VALUES LP
3c8ec 20 6e 65 78 70 72 6c 69 73 74 20 52 50 20 2a 2f   nexprlist RP */
3c8ed 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
3c8ee 2e 79 79 34 38 37 2e 70 4c 69 73 74 20 3d 20 79  .yy487.pList = y
3c8ef 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3c8f0 79 34 34 32 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  y442;.  yygotomi
3c8f1 6e 6f 72 2e 79 79 34 38 37 2e 70 53 65 6c 65 63  nor.yy487.pSelec
3c8f2 74 20 3d 20 30 3b 0a 7d 0a 20 20 20 20 20 20 20  t = 0;.}.       
3c8f3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3c8f4 73 65 20 31 37 38 3a 20 2f 2a 20 76 61 6c 75 65  se 178: /* value
3c8f5 6c 69 73 74 20 3a 3a 3d 20 76 61 6c 75 65 6c 69  list ::= valueli
3c8f6 73 74 20 43 4f 4d 4d 41 20 4c 50 20 65 78 70 72  st COMMA LP expr
3c8f7 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 53  list RP */.{.  S
3c8f8 65 6c 65 63 74 20 2a 70 52 69 67 68 74 20 3d 20  elect *pRight = 
3c8f9 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77  sqlite3SelectNew
3c8fa 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
3c8fb 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 2c 20  1].minor.yy442, 
3c8fc 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
3c8fd 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 79  , 0, 0);.  if( y
3c8fe 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
3c8ff 79 34 38 37 2e 70 4c 69 73 74 20 29 7b 0a 20 20  y487.pList ){.  
3c900 20 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f    yymsp[-4].mino
3c901 72 2e 79 79 34 38 37 2e 70 53 65 6c 65 63 74 20  r.yy487.pSelect 
3c902 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e  = sqlite3SelectN
3c903 65 77 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  ew(pParse, yymsp
3c904 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 38 37  [-4].minor.yy487
3c905 2e 70 4c 69 73 74 2c 20 30 2c 20 30 2c 20 30 2c  .pList, 0, 0, 0,
3c906 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b   0, 0, 0, 0, 0);
3c907 0a 20 20 20 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  .    yymsp[-4].m
3c908 69 6e 6f 72 2e 79 79 34 38 37 2e 70 4c 69 73 74  inor.yy487.pList
3c909 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 79 79 67 6f   = 0;.  }.  yygo
3c90a 74 6f 6d 69 6e 6f 72 2e 79 79 34 38 37 2e 70 4c  tominor.yy487.pL
3c90b 69 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 79  ist = 0;.  if( y
3c90c 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
3c90d 79 34 38 37 2e 70 53 65 6c 65 63 74 3d 3d 30 20  y487.pSelect==0 
3c90e 7c 7c 20 70 52 69 67 68 74 3d 3d 30 20 29 7b 0a  || pRight==0 ){.
3c90f 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
3c910 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
3c911 64 62 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20  db, pRight);.   
3c912 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
3c913 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
3c914 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
3c915 2e 79 79 34 38 37 2e 70 53 65 6c 65 63 74 29 3b  .yy487.pSelect);
3c916 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
3c917 2e 79 79 34 38 37 2e 70 53 65 6c 65 63 74 20 3d  .yy487.pSelect =
3c918 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
3c919 20 70 52 69 67 68 74 2d 3e 6f 70 20 3d 20 54 4b   pRight->op = TK
3c91a 5f 41 4c 4c 3b 0a 20 20 20 20 70 52 69 67 68 74  _ALL;.    pRight
3c91b 2d 3e 70 50 72 69 6f 72 20 3d 20 79 79 6d 73 70  ->pPrior = yymsp
3c91c 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 38 37  [-4].minor.yy487
3c91d 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 70 52  .pSelect;.    pR
3c91e 69 67 68 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c  ight->selFlags |
3c91f 3d 20 53 46 5f 56 61 6c 75 65 73 3b 0a 20 20 20  = SF_Values;.   
3c920 20 70 52 69 67 68 74 2d 3e 70 50 72 69 6f 72 2d   pRight->pPrior-
3c921 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f  >selFlags |= SF_
3c922 56 61 6c 75 65 73 3b 0a 20 20 20 20 79 79 67 6f  Values;.    yygo
3c923 74 6f 6d 69 6e 6f 72 2e 79 79 34 38 37 2e 70 53  tominor.yy487.pS
3c924 65 6c 65 63 74 20 3d 20 70 52 69 67 68 74 3b 0a  elect = pRight;.
3c925 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20 20 62 72    }.}.        br
3c926 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3c927 31 38 31 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69  181: /* inscolli
3c928 73 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c 69 73  st ::= inscollis
3c929 74 20 43 4f 4d 4d 41 20 6e 6d 20 2a 2f 0a 7b 79  t COMMA nm */.{y
3c92a 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 30  ygotominor.yy180
3c92b 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74   = sqlite3IdList
3c92c 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64  Append(pParse->d
3c92d 62 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  b,yymsp[-2].mino
3c92e 72 2e 79 79 31 38 30 2c 26 79 79 6d 73 70 5b 30  r.yy180,&yymsp[0
3c92f 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
3c930 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3c931 20 20 20 20 63 61 73 65 20 31 38 32 3a 20 2f 2a      case 182: /*
3c932 20 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20   inscollist ::= 
3c933 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  nm */.{yygotomin
3c934 6f 72 2e 79 79 31 38 30 20 3d 20 73 71 6c 69 74  or.yy180 = sqlit
3c935 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 70  e3IdListAppend(p
3c936 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79 6d  Parse->db,0,&yym
3c937 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
3c938 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3c939 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 33  ;.      case 183
3c93a 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 74 65  : /* expr ::= te
3c93b 72 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  rm */.{yygotomin
3c93c 6f 72 2e 79 79 33 34 32 20 3d 20 79 79 6d 73 70  or.yy342 = yymsp
3c93d 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 3b  [0].minor.yy342;
3c93e 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3c93f 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 34 3a  .      case 184:
3c940 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4c 50 20   /* expr ::= LP 
3c941 65 78 70 72 20 52 50 20 2a 2f 0a 7b 79 79 67 6f  expr RP */.{yygo
3c942 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45  tominor.yy342.pE
3c943 78 70 72 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e  xpr = yymsp[-1].
3c944 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70  minor.yy342.pExp
3c945 72 3b 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f  r; spanSet(&yygo
3c946 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2c 26 79  tominor.yy342,&y
3c947 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
3c948 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
3c949 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
3c94a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3c94b 61 73 65 20 31 38 35 3a 20 2f 2a 20 74 65 72 6d  ase 185: /* term
3c94c 20 3a 3a 3d 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20   ::= NULL */.   
3c94d 20 20 20 63 61 73 65 20 31 39 30 3a 20 2f 2a 20     case 190: /* 
3c94e 74 65 72 6d 20 3a 3a 3d 20 49 4e 54 45 47 45 52  term ::= INTEGER
3c94f 7c 46 4c 4f 41 54 7c 42 4c 4f 42 20 2a 2f 20 79  |FLOAT|BLOB */ y
3c950 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3c951 6e 6f 3d 3d 31 39 30 29 3b 0a 20 20 20 20 20 20  no==190);.      
3c952 63 61 73 65 20 31 39 31 3a 20 2f 2a 20 74 65 72  case 191: /* ter
3c953 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 20 2a 2f 20  m ::= STRING */ 
3c954 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3c955 65 6e 6f 3d 3d 31 39 31 29 3b 0a 7b 73 70 61 6e  eno==191);.{span
3c956 45 78 70 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  Expr(&yygotomino
3c957 72 2e 79 79 33 34 32 2c 20 70 50 61 72 73 65 2c  r.yy342, pParse,
3c958 20 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c   yymsp[0].major,
3c959 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
3c95a 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
3c95b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3c95c 65 20 31 38 36 3a 20 2f 2a 20 65 78 70 72 20 3a  e 186: /* expr :
3c95d 3a 3d 20 69 64 20 2a 2f 0a 20 20 20 20 20 20 63  := id */.      c
3c95e 61 73 65 20 31 38 37 3a 20 2f 2a 20 65 78 70 72  ase 187: /* expr
3c95f 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 2a 2f 20   ::= JOIN_KW */ 
3c960 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3c961 65 6e 6f 3d 3d 31 38 37 29 3b 0a 7b 73 70 61 6e  eno==187);.{span
3c962 45 78 70 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  Expr(&yygotomino
3c963 72 2e 79 79 33 34 32 2c 20 70 50 61 72 73 65 2c  r.yy342, pParse,
3c964 20 54 4b 5f 49 44 2c 20 26 79 79 6d 73 70 5b 30   TK_ID, &yymsp[0
3c965 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
3c966 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3c967 20 20 20 20 63 61 73 65 20 31 38 38 3a 20 2f 2a      case 188: /*
3c968 20 65 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54   expr ::= nm DOT
3c969 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20   nm */.{.  Expr 
3c96a 2a 74 65 6d 70 31 20 3d 20 73 71 6c 69 74 65 33  *temp1 = sqlite3
3c96b 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
3c96c 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  _ID, 0, 0, &yyms
3c96d 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-2].minor.yy0)
3c96e 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 32 20  ;.  Expr *temp2 
3c96f 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
3c970 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
3c971 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69   0, &yymsp[0].mi
3c972 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 79 79 67 6f  nor.yy0);.  yygo
3c973 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45  tominor.yy342.pE
3c974 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78  xpr = sqlite3PEx
3c975 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f  pr(pParse, TK_DO
3c976 54 2c 20 74 65 6d 70 31 2c 20 74 65 6d 70 32 2c  T, temp1, temp2,
3c977 20 30 29 3b 0a 20 20 73 70 61 6e 53 65 74 28 26   0);.  spanSet(&
3c978 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3c979 32 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  2,&yymsp[-2].min
3c97a 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d  or.yy0,&yymsp[0]
3c97b 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
3c97c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3c97d 20 20 20 20 63 61 73 65 20 31 38 39 3a 20 2f 2a      case 189: /*
3c97e 20 65 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54   expr ::= nm DOT
3c97f 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a   nm DOT nm */.{.
3c980 20 20 45 78 70 72 20 2a 74 65 6d 70 31 20 3d 20    Expr *temp1 = 
3c981 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
3c982 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
3c983 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  , &yymsp[-4].min
3c984 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20  or.yy0);.  Expr 
3c985 2a 74 65 6d 70 32 20 3d 20 73 71 6c 69 74 65 33  *temp2 = sqlite3
3c986 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
3c987 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  _ID, 0, 0, &yyms
3c988 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-2].minor.yy0)
3c989 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 33 20  ;.  Expr *temp3 
3c98a 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
3c98b 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
3c98c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69   0, &yymsp[0].mi
3c98d 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72  nor.yy0);.  Expr
3c98e 20 2a 74 65 6d 70 34 20 3d 20 73 71 6c 69 74 65   *temp4 = sqlite
3c98f 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
3c990 4b 5f 44 4f 54 2c 20 74 65 6d 70 32 2c 20 74 65  K_DOT, temp2, te
3c991 6d 70 33 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74  mp3, 0);.  yygot
3c992 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78  ominor.yy342.pEx
3c993 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
3c994 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54  r(pParse, TK_DOT
3c995 2c 20 74 65 6d 70 31 2c 20 74 65 6d 70 34 2c 20  , temp1, temp4, 
3c996 30 29 3b 0a 20 20 73 70 61 6e 53 65 74 28 26 79  0);.  spanSet(&y
3c997 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32  ygotominor.yy342
3c998 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  ,&yymsp[-4].mino
3c999 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
3c99a 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
3c99b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3c99c 20 20 20 63 61 73 65 20 31 39 32 3a 20 2f 2a 20     case 192: /* 
3c99d 65 78 70 72 20 3a 3a 3d 20 52 45 47 49 53 54 45  expr ::= REGISTE
3c99e 52 20 2a 2f 0a 7b 0a 20 20 2f 2a 20 57 68 65 6e  R */.{.  /* When
3c99f 20 64 6f 69 6e 67 20 61 20 6e 65 73 74 65 64 20   doing a nested 
3c9a0 70 61 72 73 65 2c 20 6f 6e 65 20 63 61 6e 20 69  parse, one can i
3c9a1 6e 63 6c 75 64 65 20 74 65 72 6d 73 20 69 6e 20  nclude terms in 
3c9a2 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20  an expression.  
3c9a3 2a 2a 20 74 68 61 74 20 6c 6f 6f 6b 20 6c 69 6b  ** that look lik
3c9a4 65 20 74 68 69 73 3a 20 20 20 23 31 20 23 32 20  e this:   #1 #2 
3c9a5 2e 2e 2e 20 20 54 68 65 73 65 20 74 65 72 6d 73  ...  These terms
3c9a6 20 72 65 66 65 72 20 74 6f 20 72 65 67 69 73 74   refer to regist
3c9a7 65 72 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  ers.  ** in the 
3c9a8 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e  virtual machine.
3c9a9 20 20 23 4e 20 69 73 20 74 68 65 20 4e 2d 74 68    #N is the N-th
3c9aa 20 72 65 67 69 73 74 65 72 2e 20 2a 2f 0a 20 20   register. */.  
3c9ab 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  if( pParse->nest
3c9ac 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ed==0 ){.    sql
3c9ad 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
3c9ae 72 73 65 2c 20 22 6e 65 61 72 20 5c 22 25 54 5c  rse, "near \"%T\
3c9af 22 3a 20 73 79 6e 74 61 78 20 65 72 72 6f 72 22  ": syntax error"
3c9b0 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
3c9b1 72 2e 79 79 30 29 3b 0a 20 20 20 20 79 79 67 6f  r.yy0);.    yygo
3c9b2 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45  tominor.yy342.pE
3c9b3 78 70 72 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  xpr = 0;.  }else
3c9b4 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  {.    yygotomino
3c9b5 72 2e 79 79 33 34 32 2e 70 45 78 70 72 20 3d 20  r.yy342.pExpr = 
3c9b6 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
3c9b7 72 73 65 2c 20 54 4b 5f 52 45 47 49 53 54 45 52  rse, TK_REGISTER
3c9b8 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30  , 0, 0, &yymsp[0
3c9b9 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
3c9ba 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
3c9bb 72 2e 79 79 33 34 32 2e 70 45 78 70 72 20 29 20  r.yy342.pExpr ) 
3c9bc 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28  sqlite3GetInt32(
3c9bd 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3c9be 79 79 30 2e 7a 5b 31 5d 2c 20 26 79 79 67 6f 74  yy0.z[1], &yygot
3c9bf 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78  ominor.yy342.pEx
3c9c0 70 72 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 7d  pr->iTable);.  }
3c9c1 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f  .  spanSet(&yygo
3c9c2 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2c 20 26  tominor.yy342, &
3c9c3 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3c9c4 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0, &yymsp[0].mi
3c9c5 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
3c9c6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3c9c7 20 63 61 73 65 20 31 39 33 3a 20 2f 2a 20 65 78   case 193: /* ex
3c9c8 70 72 20 3a 3a 3d 20 56 41 52 49 41 42 4c 45 20  pr ::= VARIABLE 
3c9c9 2a 2f 0a 7b 0a 20 20 73 70 61 6e 45 78 70 72 28  */.{.  spanExpr(
3c9ca 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  &yygotominor.yy3
3c9cb 34 32 2c 20 70 50 61 72 73 65 2c 20 54 4b 5f 56  42, pParse, TK_V
3c9cc 41 52 49 41 42 4c 45 2c 20 26 79 79 6d 73 70 5b  ARIABLE, &yymsp[
3c9cd 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  0].minor.yy0);. 
3c9ce 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73 69   sqlite3ExprAssi
3c9cf 67 6e 56 61 72 4e 75 6d 62 65 72 28 70 50 61 72  gnVarNumber(pPar
3c9d0 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  se, yygotominor.
3c9d1 79 79 33 34 32 2e 70 45 78 70 72 29 3b 0a 20 20  yy342.pExpr);.  
3c9d2 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d  spanSet(&yygotom
3c9d3 69 6e 6f 72 2e 79 79 33 34 32 2c 20 26 79 79 6d  inor.yy342, &yym
3c9d4 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
3c9d5 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
3c9d6 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
3c9d7 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3c9d8 73 65 20 31 39 34 3a 20 2f 2a 20 65 78 70 72 20  se 194: /* expr 
3c9d9 3a 3a 3d 20 65 78 70 72 20 43 4f 4c 4c 41 54 45  ::= expr COLLATE
3c9da 20 69 64 73 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f   ids */.{.  yygo
3c9db 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45  tominor.yy342.pE
3c9dc 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70  xpr = sqlite3Exp
3c9dd 72 53 65 74 43 6f 6c 6c 42 79 54 6f 6b 65 6e 28  rSetCollByToken(
3c9de 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32  pParse, yymsp[-2
3c9df 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45  ].minor.yy342.pE
3c9e0 78 70 72 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  xpr, &yymsp[0].m
3c9e1 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 79 79 67  inor.yy0);.  yyg
3c9e2 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 7a  otominor.yy342.z
3c9e3 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 32  Start = yymsp[-2
3c9e4 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 7a 53  ].minor.yy342.zS
3c9e5 74 61 72 74 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  tart;.  yygotomi
3c9e6 6e 6f 72 2e 79 79 33 34 32 2e 7a 45 6e 64 20 3d  nor.yy342.zEnd =
3c9e7 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
3c9e8 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e  .yy0.z[yymsp[0].
3c9e9 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 7d 0a  minor.yy0.n];.}.
3c9ea 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3c9eb 20 20 20 20 20 63 61 73 65 20 31 39 35 3a 20 2f       case 195: /
3c9ec 2a 20 65 78 70 72 20 3a 3a 3d 20 43 41 53 54 20  * expr ::= CAST 
3c9ed 4c 50 20 65 78 70 72 20 41 53 20 74 79 70 65 74  LP expr AS typet
3c9ee 6f 6b 65 6e 20 52 50 20 2a 2f 0a 7b 0a 20 20 79  oken RP */.{.  y
3c9ef 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32  ygotominor.yy342
3c9f0 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
3c9f1 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
3c9f2 5f 43 41 53 54 2c 20 79 79 6d 73 70 5b 2d 33 5d  _CAST, yymsp[-3]
3c9f3 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78  .minor.yy342.pEx
3c9f4 70 72 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 31  pr, 0, &yymsp[-1
3c9f5 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
3c9f6 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d  spanSet(&yygotom
3c9f7 69 6e 6f 72 2e 79 79 33 34 32 2c 26 79 79 6d 73  inor.yy342,&yyms
3c9f8 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-5].minor.yy0,
3c9f9 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3c9fa 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
3c9fb 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3c9fc 65 20 31 39 36 3a 20 2f 2a 20 65 78 70 72 20 3a  e 196: /* expr :
3c9fd 3a 3d 20 49 44 20 4c 50 20 64 69 73 74 69 6e 63  := ID LP distinc
3c9fe 74 20 65 78 70 72 6c 69 73 74 20 52 50 20 2a 2f  t exprlist RP */
3c9ff 0a 7b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d  .{.  if( yymsp[-
3ca00 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 20 26  1].minor.yy442 &
3ca01 26 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  & yymsp[-1].mino
3ca02 72 2e 79 79 34 34 32 2d 3e 6e 45 78 70 72 3e 70  r.yy442->nExpr>p
3ca03 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69  Parse->db->aLimi
3ca04 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46  t[SQLITE_LIMIT_F
3ca05 55 4e 43 54 49 4f 4e 5f 41 52 47 5d 20 29 7b 0a  UNCTION_ARG] ){.
3ca06 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
3ca07 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f  Msg(pParse, "too
3ca08 20 6d 61 6e 79 20 61 72 67 75 6d 65 6e 74 73 20   many arguments 
3ca09 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 25 54 22 2c  on function %T",
3ca0a 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f   &yymsp[-4].mino
3ca0b 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 79 79  r.yy0);.  }.  yy
3ca0c 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e  gotominor.yy342.
3ca0d 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45  pExpr = sqlite3E
3ca0e 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72  xprFunction(pPar
3ca0f 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  se, yymsp[-1].mi
3ca10 6e 6f 72 2e 79 79 34 34 32 2c 20 26 79 79 6d 73  nor.yy442, &yyms
3ca11 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-4].minor.yy0)
3ca12 3b 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67  ;.  spanSet(&yyg
3ca13 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2c 26  otominor.yy342,&
3ca14 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
3ca15 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
3ca16 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 69 66 28 20  nor.yy0);.  if( 
3ca17 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
3ca18 79 79 33 39 32 20 26 26 20 79 79 67 6f 74 6f 6d  yy392 && yygotom
3ca19 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72  inor.yy342.pExpr
3ca1a 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69   ){.    yygotomi
3ca1b 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 2d  nor.yy342.pExpr-
3ca1c 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 44 69 73  >flags |= EP_Dis
3ca1d 74 69 6e 63 74 3b 0a 20 20 7d 0a 7d 0a 20 20 20  tinct;.  }.}.   
3ca1e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3ca1f 20 20 63 61 73 65 20 31 39 37 3a 20 2f 2a 20 65    case 197: /* e
3ca20 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 53 54  xpr ::= ID LP ST
3ca21 41 52 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67  AR RP */.{.  yyg
3ca22 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70  otominor.yy342.p
3ca23 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78  Expr = sqlite3Ex
3ca24 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73  prFunction(pPars
3ca25 65 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d  e, 0, &yymsp[-3]
3ca26 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73  .minor.yy0);.  s
3ca27 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69  panSet(&yygotomi
3ca28 6e 6f 72 2e 79 79 33 34 32 2c 26 79 79 6d 73 70  nor.yy342,&yymsp
3ca29 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-3].minor.yy0,&
3ca2a 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3ca2b 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
3ca2c 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3ca2d 20 31 39 38 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a   198: /* term ::
3ca2e 3d 20 43 54 49 4d 45 5f 4b 57 20 2a 2f 0a 7b 0a  = CTIME_KW */.{.
3ca2f 20 20 2f 2a 20 54 68 65 20 43 55 52 52 45 4e 54    /* The CURRENT
3ca30 5f 54 49 4d 45 2c 20 43 55 52 52 45 4e 54 5f 44  _TIME, CURRENT_D
3ca31 41 54 45 2c 20 61 6e 64 20 43 55 52 52 45 4e 54  ATE, and CURRENT
3ca32 5f 54 49 4d 45 53 54 41 4d 50 20 76 61 6c 75 65  _TIMESTAMP value
3ca33 73 20 61 72 65 0a 20 20 2a 2a 20 74 72 65 61 74  s are.  ** treat
3ca34 65 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 73 20  ed as functions 
3ca35 74 68 61 74 20 72 65 74 75 72 6e 20 63 6f 6e 73  that return cons
3ca36 74 61 6e 74 73 20 2a 2f 0a 20 20 79 79 67 6f 74  tants */.  yygot
3ca37 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78  ominor.yy342.pEx
3ca38 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  pr = sqlite3Expr
3ca39 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2c  Function(pParse,
3ca3a 20 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   0,&yymsp[0].min
3ca3b 6f 72 2e 79 79 30 29 3b 0a 20 20 69 66 28 20 79  or.yy0);.  if( y
3ca3c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32  ygotominor.yy342
3ca3d 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 79 79  .pExpr ){.    yy
3ca3e 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e  gotominor.yy342.
3ca3f 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43  pExpr->op = TK_C
3ca40 4f 4e 53 54 5f 46 55 4e 43 3b 20 20 0a 20 20 7d  ONST_FUNC;  .  }
3ca41 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f  .  spanSet(&yygo
3ca42 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2c 20 26  tominor.yy342, &
3ca43 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3ca44 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0, &yymsp[0].mi
3ca45 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
3ca46 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3ca47 20 63 61 73 65 20 31 39 39 3a 20 2f 2a 20 65 78   case 199: /* ex
3ca48 70 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e 44 20  pr ::= expr AND 
3ca49 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  expr */.      ca
3ca4a 73 65 20 32 30 30 3a 20 2f 2a 20 65 78 70 72 20  se 200: /* expr 
3ca4b 3a 3a 3d 20 65 78 70 72 20 4f 52 20 65 78 70 72  ::= expr OR expr
3ca4c 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3ca4d 79 72 75 6c 65 6e 6f 3d 3d 32 30 30 29 3b 0a 20  yruleno==200);. 
3ca4e 20 20 20 20 20 63 61 73 65 20 32 30 31 3a 20 2f       case 201: /
3ca4f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
3ca50 4c 54 7c 47 54 7c 47 45 7c 4c 45 20 65 78 70 72  LT|GT|GE|LE expr
3ca51 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3ca52 79 72 75 6c 65 6e 6f 3d 3d 32 30 31 29 3b 0a 20  yruleno==201);. 
3ca53 20 20 20 20 20 63 61 73 65 20 32 30 32 3a 20 2f       case 202: /
3ca54 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
3ca55 45 51 7c 4e 45 20 65 78 70 72 20 2a 2f 20 79 79  EQ|NE expr */ yy
3ca56 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3ca57 6f 3d 3d 32 30 32 29 3b 0a 20 20 20 20 20 20 63  o==202);.      c
3ca58 61 73 65 20 32 30 33 3a 20 2f 2a 20 65 78 70 72  ase 203: /* expr
3ca59 20 3a 3a 3d 20 65 78 70 72 20 42 49 54 41 4e 44   ::= expr BITAND
3ca5a 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52 53  |BITOR|LSHIFT|RS
3ca5b 48 49 46 54 20 65 78 70 72 20 2a 2f 20 79 79 74  HIFT expr */ yyt
3ca5c 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3ca5d 3d 3d 32 30 33 29 3b 0a 20 20 20 20 20 20 63 61  ==203);.      ca
3ca5e 73 65 20 32 30 34 3a 20 2f 2a 20 65 78 70 72 20  se 204: /* expr 
3ca5f 3a 3a 3d 20 65 78 70 72 20 50 4c 55 53 7c 4d 49  ::= expr PLUS|MI
3ca60 4e 55 53 20 65 78 70 72 20 2a 2f 20 79 79 74 65  NUS expr */ yyte
3ca61 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3ca62 3d 32 30 34 29 3b 0a 20 20 20 20 20 20 63 61 73  =204);.      cas
3ca63 65 20 32 30 35 3a 20 2f 2a 20 65 78 70 72 20 3a  e 205: /* expr :
3ca64 3a 3d 20 65 78 70 72 20 53 54 41 52 7c 53 4c 41  := expr STAR|SLA
3ca65 53 48 7c 52 45 4d 20 65 78 70 72 20 2a 2f 20 79  SH|REM expr */ y
3ca66 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3ca67 6e 6f 3d 3d 32 30 35 29 3b 0a 20 20 20 20 20 20  no==205);.      
3ca68 63 61 73 65 20 32 30 36 3a 20 2f 2a 20 65 78 70  case 206: /* exp
3ca69 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e 43 41  r ::= expr CONCA
3ca6a 54 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74  T expr */ yytest
3ca6b 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
3ca6c 30 36 29 3b 0a 7b 73 70 61 6e 42 69 6e 61 72 79  06);.{spanBinary
3ca6d 45 78 70 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  Expr(&yygotomino
3ca6e 72 2e 79 79 33 34 32 2c 70 50 61 72 73 65 2c 79  r.yy342,pParse,y
3ca6f 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c 26  ymsp[-1].major,&
3ca70 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
3ca71 79 79 33 34 32 2c 26 79 79 6d 73 70 5b 30 5d 2e  yy342,&yymsp[0].
3ca72 6d 69 6e 6f 72 2e 79 79 33 34 32 29 3b 7d 0a 20  minor.yy342);}. 
3ca73 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3ca74 20 20 20 20 63 61 73 65 20 32 30 37 3a 20 2f 2a      case 207: /*
3ca75 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4c 49 4b 45   likeop ::= LIKE
3ca76 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  _KW */.      cas
3ca77 65 20 32 30 39 3a 20 2f 2a 20 6c 69 6b 65 6f 70  e 209: /* likeop
3ca78 20 3a 3a 3d 20 4d 41 54 43 48 20 2a 2f 20 79 79   ::= MATCH */ yy
3ca79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3ca7a 6f 3d 3d 32 30 39 29 3b 0a 7b 79 79 67 6f 74 6f  o==209);.{yygoto
3ca7b 6d 69 6e 6f 72 2e 79 79 33 31 38 2e 65 4f 70 65  minor.yy318.eOpe
3ca7c 72 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b 30 5d  rator = yymsp[0]
3ca7d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79 67 6f  .minor.yy0; yygo
3ca7e 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 38 2e 62 4e  tominor.yy318.bN
3ca7f 6f 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  ot = 0;}.       
3ca80 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3ca81 73 65 20 32 30 38 3a 20 2f 2a 20 6c 69 6b 65 6f  se 208: /* likeo
3ca82 70 20 3a 3a 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b  p ::= NOT LIKE_K
3ca83 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  W */.      case 
3ca84 32 31 30 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a  210: /* likeop :
3ca85 3a 3d 20 4e 4f 54 20 4d 41 54 43 48 20 2a 2f 20  := NOT MATCH */ 
3ca86 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3ca87 65 6e 6f 3d 3d 32 31 30 29 3b 0a 7b 79 79 67 6f  eno==210);.{yygo
3ca88 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 38 2e 65 4f  tominor.yy318.eO
3ca89 70 65 72 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b  perator = yymsp[
3ca8a 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79  0].minor.yy0; yy
3ca8b 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 38 2e  gotominor.yy318.
3ca8c 62 4e 6f 74 20 3d 20 31 3b 7d 0a 20 20 20 20 20  bNot = 1;}.     
3ca8d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3ca8e 63 61 73 65 20 32 31 31 3a 20 2f 2a 20 65 78 70  case 211: /* exp
3ca8f 72 20 3a 3a 3d 20 65 78 70 72 20 6c 69 6b 65 6f  r ::= expr likeo
3ca90 70 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 45 78  p expr */.{.  Ex
3ca91 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a 20  prList *pList;. 
3ca92 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33   pList = sqlite3
3ca93 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
3ca94 50 61 72 73 65 2c 30 2c 20 79 79 6d 73 70 5b 30  Parse,0, yymsp[0
3ca95 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45  ].minor.yy342.pE
3ca96 78 70 72 29 3b 0a 20 20 70 4c 69 73 74 20 3d 20  xpr);.  pList = 
3ca97 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
3ca98 70 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c 69  ppend(pParse,pLi
3ca99 73 74 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  st, yymsp[-2].mi
3ca9a 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 29  nor.yy342.pExpr)
3ca9b 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
3ca9c 79 79 33 34 32 2e 70 45 78 70 72 20 3d 20 73 71  yy342.pExpr = sq
3ca9d 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f  lite3ExprFunctio
3ca9e 6e 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c  n(pParse, pList,
3ca9f 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f   &yymsp[-1].mino
3caa0 72 2e 79 79 33 31 38 2e 65 4f 70 65 72 61 74 6f  r.yy318.eOperato
3caa1 72 29 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b  r);.  if( yymsp[
3caa2 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 38 2e  -1].minor.yy318.
3caa3 62 4e 6f 74 20 29 20 79 79 67 6f 74 6f 6d 69 6e  bNot ) yygotomin
3caa4 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 20 3d  or.yy342.pExpr =
3caa5 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
3caa6 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79  arse, TK_NOT, yy
3caa7 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e  gotominor.yy342.
3caa8 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20  pExpr, 0, 0);.  
3caa9 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3caaa 32 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70  2.zStart = yymsp
3caab 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32  [-2].minor.yy342
3caac 2e 7a 53 74 61 72 74 3b 0a 20 20 79 79 67 6f 74  .zStart;.  yygot
3caad 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 7a 45 6e  ominor.yy342.zEn
3caae 64 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  d = yymsp[0].min
3caaf 6f 72 2e 79 79 33 34 32 2e 7a 45 6e 64 3b 0a 20  or.yy342.zEnd;. 
3cab0 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
3cab1 2e 79 79 33 34 32 2e 70 45 78 70 72 20 29 20 79  .yy342.pExpr ) y
3cab2 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32  ygotominor.yy342
3cab3 2e 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d  .pExpr->flags |=
3cab4 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 3b 0a 7d   EP_InfixFunc;.}
3cab5 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3cab6 20 20 20 20 20 20 63 61 73 65 20 32 31 32 3a 20        case 212: 
3cab7 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
3cab8 20 6c 69 6b 65 6f 70 20 65 78 70 72 20 45 53 43   likeop expr ESC
3cab9 41 50 45 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20  APE expr */.{.  
3caba 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b  ExprList *pList;
3cabb 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  .  pList = sqlit
3cabc 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
3cabd 28 70 50 61 72 73 65 2c 30 2c 20 79 79 6d 73 70  (pParse,0, yymsp
3cabe 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32  [-2].minor.yy342
3cabf 2e 70 45 78 70 72 29 3b 0a 20 20 70 4c 69 73 74  .pExpr);.  pList
3cac0 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
3cac1 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
3cac2 70 4c 69 73 74 2c 20 79 79 6d 73 70 5b 2d 34 5d  pList, yymsp[-4]
3cac3 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78  .minor.yy342.pEx
3cac4 70 72 29 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73  pr);.  pList = s
3cac5 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
3cac6 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c 69 73  pend(pParse,pLis
3cac7 74 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  t, yymsp[0].mino
3cac8 72 2e 79 79 33 34 32 2e 70 45 78 70 72 29 3b 0a  r.yy342.pExpr);.
3cac9 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3caca 33 34 32 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  342.pExpr = sqli
3cacb 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28  te3ExprFunction(
3cacc 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 26  pParse, pList, &
3cacd 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
3cace 79 79 33 31 38 2e 65 4f 70 65 72 61 74 6f 72 29  yy318.eOperator)
3cacf 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33  ;.  if( yymsp[-3
3cad0 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 38 2e 62 4e  ].minor.yy318.bN
3cad1 6f 74 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  ot ) yygotominor
3cad2 2e 79 79 33 34 32 2e 70 45 78 70 72 20 3d 20 73  .yy342.pExpr = s
3cad3 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
3cad4 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f  se, TK_NOT, yygo
3cad5 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45  tominor.yy342.pE
3cad6 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 79 79  xpr, 0, 0);.  yy
3cad7 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e  gotominor.yy342.
3cad8 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d  zStart = yymsp[-
3cad9 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 7a  4].minor.yy342.z
3cada 53 74 61 72 74 3b 0a 20 20 79 79 67 6f 74 6f 6d  Start;.  yygotom
3cadb 69 6e 6f 72 2e 79 79 33 34 32 2e 7a 45 6e 64 20  inor.yy342.zEnd 
3cadc 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
3cadd 2e 79 79 33 34 32 2e 7a 45 6e 64 3b 0a 20 20 69  .yy342.zEnd;.  i
3cade 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
3cadf 79 33 34 32 2e 70 45 78 70 72 20 29 20 79 79 67  y342.pExpr ) yyg
3cae0 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70  otominor.yy342.p
3cae1 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  Expr->flags |= E
3cae2 50 5f 49 6e 66 69 78 46 75 6e 63 3b 0a 7d 0a 20  P_InfixFunc;.}. 
3cae3 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3cae4 20 20 20 20 63 61 73 65 20 32 31 33 3a 20 2f 2a      case 213: /*
3cae5 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49   expr ::= expr I
3cae6 53 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 20 2a 2f  SNULL|NOTNULL */
3cae7 0a 7b 73 70 61 6e 55 6e 61 72 79 50 6f 73 74 66  .{spanUnaryPostf
3cae8 69 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ix(&yygotominor.
3cae9 79 79 33 34 32 2c 70 50 61 72 73 65 2c 79 79 6d  yy342,pParse,yym
3caea 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 26 79 79 6d  sp[0].major,&yym
3caeb 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
3caec 34 32 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  42,&yymsp[0].min
3caed 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
3caee 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3caef 61 73 65 20 32 31 34 3a 20 2f 2a 20 65 78 70 72  ase 214: /* expr
3caf0 20 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20 4e 55   ::= expr NOT NU
3caf1 4c 4c 20 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72 79  LL */.{spanUnary
3caf2 50 6f 73 74 66 69 78 28 26 79 79 67 6f 74 6f 6d  Postfix(&yygotom
3caf3 69 6e 6f 72 2e 79 79 33 34 32 2c 70 50 61 72 73  inor.yy342,pPars
3caf4 65 2c 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 26 79 79  e,TK_NOTNULL,&yy
3caf5 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
3caf6 33 34 32 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  342,&yymsp[0].mi
3caf7 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
3caf8 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3caf9 63 61 73 65 20 32 31 35 3a 20 2f 2a 20 65 78 70  case 215: /* exp
3cafa 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 65 78  r ::= expr IS ex
3cafb 70 72 20 2a 2f 0a 7b 0a 20 20 73 70 61 6e 42 69  pr */.{.  spanBi
3cafc 6e 61 72 79 45 78 70 72 28 26 79 79 67 6f 74 6f  naryExpr(&yygoto
3cafd 6d 69 6e 6f 72 2e 79 79 33 34 32 2c 70 50 61 72  minor.yy342,pPar
3cafe 73 65 2c 54 4b 5f 49 53 2c 26 79 79 6d 73 70 5b  se,TK_IS,&yymsp[
3caff 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2c  -2].minor.yy342,
3cb00 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3cb01 79 79 33 34 32 29 3b 0a 20 20 62 69 6e 61 72 79  yy342);.  binary
3cb02 54 6f 55 6e 61 72 79 49 66 4e 75 6c 6c 28 70 50  ToUnaryIfNull(pP
3cb03 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  arse, yymsp[0].m
3cb04 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72  inor.yy342.pExpr
3cb05 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
3cb06 33 34 32 2e 70 45 78 70 72 2c 20 54 4b 5f 49 53  342.pExpr, TK_IS
3cb07 4e 55 4c 4c 29 3b 0a 7d 0a 20 20 20 20 20 20 20  NULL);.}.       
3cb08 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3cb09 73 65 20 32 31 36 3a 20 2f 2a 20 65 78 70 72 20  se 216: /* expr 
3cb0a 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e 4f 54 20  ::= expr IS NOT 
3cb0b 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73 70 61 6e  expr */.{.  span
3cb0c 42 69 6e 61 72 79 45 78 70 72 28 26 79 79 67 6f  BinaryExpr(&yygo
3cb0d 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2c 70 50  tominor.yy342,pP
3cb0e 61 72 73 65 2c 54 4b 5f 49 53 4e 4f 54 2c 26 79  arse,TK_ISNOT,&y
3cb0f 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
3cb10 79 33 34 32 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  y342,&yymsp[0].m
3cb11 69 6e 6f 72 2e 79 79 33 34 32 29 3b 0a 20 20 62  inor.yy342);.  b
3cb12 69 6e 61 72 79 54 6f 55 6e 61 72 79 49 66 4e 75  inaryToUnaryIfNu
3cb13 6c 6c 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  ll(pParse, yymsp
3cb14 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e  [0].minor.yy342.
3cb15 70 45 78 70 72 2c 20 79 79 67 6f 74 6f 6d 69 6e  pExpr, yygotomin
3cb16 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 2c 20  or.yy342.pExpr, 
3cb17 54 4b 5f 4e 4f 54 4e 55 4c 4c 29 3b 0a 7d 0a 20  TK_NOTNULL);.}. 
3cb18 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3cb19 20 20 20 20 63 61 73 65 20 32 31 37 3a 20 2f 2a      case 217: /*
3cb1a 20 65 78 70 72 20 3a 3a 3d 20 4e 4f 54 20 65 78   expr ::= NOT ex
3cb1b 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  pr */.      case
3cb1c 20 32 31 38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   218: /* expr ::
3cb1d 3d 20 42 49 54 4e 4f 54 20 65 78 70 72 20 2a 2f  = BITNOT expr */
3cb1e 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3cb1f 6c 65 6e 6f 3d 3d 32 31 38 29 3b 0a 7b 73 70 61  leno==218);.{spa
3cb20 6e 55 6e 61 72 79 50 72 65 66 69 78 28 26 79 79  nUnaryPrefix(&yy
3cb21 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2c  gotominor.yy342,
3cb22 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d  pParse,yymsp[-1]
3cb23 2e 6d 61 6a 6f 72 2c 26 79 79 6d 73 70 5b 30 5d  .major,&yymsp[0]
3cb24 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2c 26 79 79  .minor.yy342,&yy
3cb25 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
3cb26 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
3cb27 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
3cb28 31 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  19: /* expr ::= 
3cb29 4d 49 4e 55 53 20 65 78 70 72 20 2a 2f 0a 7b 73  MINUS expr */.{s
3cb2a 70 61 6e 55 6e 61 72 79 50 72 65 66 69 78 28 26  panUnaryPrefix(&
3cb2b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3cb2c 32 2c 70 50 61 72 73 65 2c 54 4b 5f 55 4d 49 4e  2,pParse,TK_UMIN
3cb2d 55 53 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  US,&yymsp[0].min
3cb2e 6f 72 2e 79 79 33 34 32 2c 26 79 79 6d 73 70 5b  or.yy342,&yymsp[
3cb2f 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  -1].minor.yy0);}
3cb30 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3cb31 20 20 20 20 20 20 63 61 73 65 20 32 32 30 3a 20        case 220: 
3cb32 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53  /* expr ::= PLUS
3cb33 20 65 78 70 72 20 2a 2f 0a 7b 73 70 61 6e 55 6e   expr */.{spanUn
3cb34 61 72 79 50 72 65 66 69 78 28 26 79 79 67 6f 74  aryPrefix(&yygot
3cb35 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2c 70 50 61  ominor.yy342,pPa
3cb36 72 73 65 2c 54 4b 5f 55 50 4c 55 53 2c 26 79 79  rse,TK_UPLUS,&yy
3cb37 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
3cb38 34 32 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  42,&yymsp[-1].mi
3cb39 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
3cb3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3cb3b 63 61 73 65 20 32 32 33 3a 20 2f 2a 20 65 78 70  case 223: /* exp
3cb3c 72 20 3a 3a 3d 20 65 78 70 72 20 62 65 74 77 65  r ::= expr betwe
3cb3d 65 6e 5f 6f 70 20 65 78 70 72 20 41 4e 44 20 65  en_op expr AND e
3cb3e 78 70 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 4c  xpr */.{.  ExprL
3cb3f 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 73 71 6c  ist *pList = sql
3cb40 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
3cb41 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 79 79 6d  nd(pParse,0, yym
3cb42 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
3cb43 34 32 2e 70 45 78 70 72 29 3b 0a 20 20 70 4c 69  42.pExpr);.  pLi
3cb44 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
3cb45 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
3cb46 65 2c 70 4c 69 73 74 2c 20 79 79 6d 73 70 5b 30  e,pList, yymsp[0
3cb47 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45  ].minor.yy342.pE
3cb48 78 70 72 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  xpr);.  yygotomi
3cb49 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 20  nor.yy342.pExpr 
3cb4a 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
3cb4b 50 61 72 73 65 2c 20 54 4b 5f 42 45 54 57 45 45  Parse, TK_BETWEE
3cb4c 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  N, yymsp[-4].min
3cb4d 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 2c 20  or.yy342.pExpr, 
3cb4e 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 79 79 67  0, 0);.  if( yyg
3cb4f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70  otominor.yy342.p
3cb50 45 78 70 72 20 29 7b 0a 20 20 20 20 79 79 67 6f  Expr ){.    yygo
3cb51 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45  tominor.yy342.pE
3cb52 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70  xpr->x.pList = p
3cb53 4c 69 73 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  List;.  }else{. 
3cb54 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
3cb55 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  stDelete(pParse-
3cb56 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 7d  >db, pList);.  }
3cb57 20 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33   .  if( yymsp[-3
3cb58 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 20 29 20  ].minor.yy392 ) 
3cb59 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3cb5a 32 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  2.pExpr = sqlite
3cb5b 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
3cb5c 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e  K_NOT, yygotomin
3cb5d 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 2c 20  or.yy342.pExpr, 
3cb5e 30 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  0, 0);.  yygotom
3cb5f 69 6e 6f 72 2e 79 79 33 34 32 2e 7a 53 74 61 72  inor.yy342.zStar
3cb60 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  t = yymsp[-4].mi
3cb61 6e 6f 72 2e 79 79 33 34 32 2e 7a 53 74 61 72 74  nor.yy342.zStart
3cb62 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
3cb63 79 79 33 34 32 2e 7a 45 6e 64 20 3d 20 79 79 6d  yy342.zEnd = yym
3cb64 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
3cb65 32 2e 7a 45 6e 64 3b 0a 7d 0a 20 20 20 20 20 20  2.zEnd;.}.      
3cb66 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3cb67 61 73 65 20 32 32 36 3a 20 2f 2a 20 65 78 70 72  ase 226: /* expr
3cb68 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20   ::= expr in_op 
3cb69 4c 50 20 65 78 70 72 6c 69 73 74 20 52 50 20 2a  LP exprlist RP *
3cb6a 2f 0a 7b 0a 20 20 20 20 69 66 28 20 79 79 6d 73  /.{.    if( yyms
3cb6b 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34  p[-1].minor.yy44
3cb6c 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  2==0 ){.      /*
3cb6d 20 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20   Expressions of 
3cb6e 74 68 65 20 66 6f 72 6d 0a 20 20 20 20 20 20 2a  the form.      *
3cb6f 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20  *.      **      
3cb70 65 78 70 72 31 20 49 4e 20 28 29 0a 20 20 20 20  expr1 IN ().    
3cb71 20 20 2a 2a 20 20 20 20 20 20 65 78 70 72 31 20    **      expr1 
3cb72 4e 4f 54 20 49 4e 20 28 29 0a 20 20 20 20 20 20  NOT IN ().      
3cb73 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 73 69 6d 70  **.      ** simp
3cb74 6c 69 66 79 20 74 6f 20 63 6f 6e 73 74 61 6e 74  lify to constant
3cb75 73 20 30 20 28 66 61 6c 73 65 29 20 61 6e 64 20  s 0 (false) and 
3cb76 31 20 28 74 72 75 65 29 2c 20 72 65 73 70 65 63  1 (true), respec
3cb77 74 69 76 65 6c 79 2c 0a 20 20 20 20 20 20 2a 2a  tively,.      **
3cb78 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74   regardless of t
3cb79 68 65 20 76 61 6c 75 65 20 6f 66 20 65 78 70 72  he value of expr
3cb7a 31 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  1..      */.    
3cb7b 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3cb7c 33 34 32 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  342.pExpr = sqli
3cb7d 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
3cb7e 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20   TK_INTEGER, 0, 
3cb7f 30 2c 20 26 73 71 6c 69 74 65 33 49 6e 74 54 6f  0, &sqlite3IntTo
3cb80 6b 65 6e 73 5b 79 79 6d 73 70 5b 2d 33 5d 2e 6d  kens[yymsp[-3].m
3cb81 69 6e 6f 72 2e 79 79 33 39 32 5d 29 3b 0a 20 20  inor.yy392]);.  
3cb82 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
3cb83 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
3cb84 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
3cb85 72 2e 79 79 33 34 32 2e 70 45 78 70 72 29 3b 0a  r.yy342.pExpr);.
3cb86 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3cb87 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
3cb88 34 32 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  42.pExpr = sqlit
3cb89 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
3cb8a 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d  TK_IN, yymsp[-4]
3cb8b 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78  .minor.yy342.pEx
3cb8c 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  pr, 0, 0);.     
3cb8d 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
3cb8e 2e 79 79 33 34 32 2e 70 45 78 70 72 20 29 7b 0a  .yy342.pExpr ){.
3cb8f 20 20 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69          yygotomi
3cb90 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 2d  nor.yy342.pExpr-
3cb91 3e 78 2e 70 4c 69 73 74 20 3d 20 79 79 6d 73 70  >x.pList = yymsp
3cb92 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32  [-1].minor.yy442
3cb93 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
3cb94 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70  3ExprSetHeight(p
3cb95 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
3cb96 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 29 3b  or.yy342.pExpr);
3cb97 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
3cb98 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
3cb99 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  rListDelete(pPar
3cb9a 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31  se->db, yymsp[-1
3cb9b 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 29 3b 0a  ].minor.yy442);.
3cb9c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
3cb9d 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ( yymsp[-3].mino
3cb9e 72 2e 79 79 33 39 32 20 29 20 79 79 67 6f 74 6f  r.yy392 ) yygoto
3cb9f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70  minor.yy342.pExp
3cba0 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
3cba1 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c  (pParse, TK_NOT,
3cba2 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
3cba3 34 32 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b  42.pExpr, 0, 0);
3cba4 0a 20 20 20 20 7d 0a 20 20 20 20 79 79 67 6f 74  .    }.    yygot
3cba5 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 7a 53 74  ominor.yy342.zSt
3cba6 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e  art = yymsp[-4].
3cba7 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 7a 53 74 61  minor.yy342.zSta
3cba8 72 74 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69  rt;.    yygotomi
3cba9 6e 6f 72 2e 79 79 33 34 32 2e 7a 45 6e 64 20 3d  nor.yy342.zEnd =
3cbaa 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
3cbab 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e  .yy0.z[yymsp[0].
3cbac 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 20 20  minor.yy0.n];.  
3cbad 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3cbae 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 37 3a  .      case 227:
3cbaf 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4c 50 20   /* expr ::= LP 
3cbb0 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20  select RP */.{. 
3cbb1 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
3cbb2 79 33 34 32 2e 70 45 78 70 72 20 3d 20 73 71 6c  y342.pExpr = sql
3cbb3 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
3cbb4 2c 20 54 4b 5f 53 45 4c 45 43 54 2c 20 30 2c 20  , TK_SELECT, 0, 
3cbb5 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 79  0, 0);.    if( y
3cbb6 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32  ygotominor.yy342
3cbb7 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20  .pExpr ){.      
3cbb8 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3cbb9 32 2e 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65  2.pExpr->x.pSele
3cbba 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  ct = yymsp[-1].m
3cbbb 69 6e 6f 72 2e 79 79 31 35 39 3b 0a 20 20 20 20  inor.yy159;.    
3cbbc 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74    ExprSetPropert
3cbbd 79 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  y(yygotominor.yy
3cbbe 33 34 32 2e 70 45 78 70 72 2c 20 45 50 5f 78 49  342.pExpr, EP_xI
3cbbf 73 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20  sSelect);.      
3cbc0 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
3cbc1 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79 67  ight(pParse, yyg
3cbc2 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70  otominor.yy342.p
3cbc3 45 78 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Expr);.    }else
3cbc4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  {.      sqlite3S
3cbc5 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61 72  electDelete(pPar
3cbc6 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31  se->db, yymsp[-1
3cbc7 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35 39 29 3b 0a  ].minor.yy159);.
3cbc8 20 20 20 20 7d 0a 20 20 20 20 79 79 67 6f 74 6f      }.    yygoto
3cbc9 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 7a 53 74 61  minor.yy342.zSta
3cbca 72 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  rt = yymsp[-2].m
3cbcb 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 20 20  inor.yy0.z;.    
3cbcc 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3cbcd 32 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b  2.zEnd = &yymsp[
3cbce 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79  0].minor.yy0.z[y
3cbcf 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3cbd0 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20 20 20  0.n];.  }.      
3cbd1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3cbd2 61 73 65 20 32 32 38 3a 20 2f 2a 20 65 78 70 72  ase 228: /* expr
3cbd3 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20   ::= expr in_op 
3cbd4 4c 50 20 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a  LP select RP */.
3cbd5 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  {.    yygotomino
3cbd6 72 2e 79 79 33 34 32 2e 70 45 78 70 72 20 3d 20  r.yy342.pExpr = 
3cbd7 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
3cbd8 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d 73  rse, TK_IN, yyms
3cbd9 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-4].minor.yy34
3cbda 32 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a  2.pExpr, 0, 0);.
3cbdb 20 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69      if( yygotomi
3cbdc 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 20  nor.yy342.pExpr 
3cbdd 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d  ){.      yygotom
3cbde 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72  inor.yy342.pExpr
3cbdf 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 79 79  ->x.pSelect = yy
3cbe0 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
3cbe1 31 35 39 3b 0a 20 20 20 20 20 20 45 78 70 72 53  159;.      ExprS
3cbe2 65 74 50 72 6f 70 65 72 74 79 28 79 79 67 6f 74  etProperty(yygot
3cbe3 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78  ominor.yy342.pEx
3cbe4 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
3cbe5 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
3cbe6 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50  ExprSetHeight(pP
3cbe7 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
3cbe8 72 2e 79 79 33 34 32 2e 70 45 78 70 72 29 3b 0a  r.yy342.pExpr);.
3cbe9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3cbea 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
3cbeb 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
3cbec 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
3cbed 2e 79 79 31 35 39 29 3b 0a 20 20 20 20 7d 0a 20  .yy159);.    }. 
3cbee 20 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d     if( yymsp[-3]
3cbef 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 20 29 20 79  .minor.yy392 ) y
3cbf0 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32  ygotominor.yy342
3cbf1 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
3cbf2 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
3cbf3 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  _NOT, yygotomino
3cbf4 72 2e 79 79 33 34 32 2e 70 45 78 70 72 2c 20 30  r.yy342.pExpr, 0
3cbf5 2c 20 30 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f  , 0);.    yygoto
3cbf6 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 7a 53 74 61  minor.yy342.zSta
3cbf7 72 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  rt = yymsp[-4].m
3cbf8 69 6e 6f 72 2e 79 79 33 34 32 2e 7a 53 74 61 72  inor.yy342.zStar
3cbf9 74 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  t;.    yygotomin
3cbfa 6f 72 2e 79 79 33 34 32 2e 7a 45 6e 64 20 3d 20  or.yy342.zEnd = 
3cbfb 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3cbfc 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d  yy0.z[yymsp[0].m
3cbfd 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d  inor.yy0.n];.  }
3cbfe 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3cbff 20 20 20 20 20 20 63 61 73 65 20 32 32 39 3a 20        case 229: 
3cc00 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
3cc01 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d 20 2a   in_op nm dbnm *
3cc02 2f 0a 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20  /.{.    SrcList 
3cc03 2a 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53  *pSrc = sqlite3S
3cc04 72 63 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  rcListAppend(pPa
3cc05 72 73 65 2d 3e 64 62 2c 20 30 2c 26 79 79 6d 73  rse->db, 0,&yyms
3cc06 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-1].minor.yy0,
3cc07 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3cc08 79 79 30 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f  yy0);.    yygoto
3cc09 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70  minor.yy342.pExp
3cc0a 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
3cc0b 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20  (pParse, TK_IN, 
3cc0c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
3cc0d 79 79 33 34 32 2e 70 45 78 70 72 2c 20 30 2c 20  yy342.pExpr, 0, 
3cc0e 30 29 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f  0);.    if( yygo
3cc0f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45  tominor.yy342.pE
3cc10 78 70 72 20 29 7b 0a 20 20 20 20 20 20 79 79 67  xpr ){.      yyg
3cc11 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70  otominor.yy342.p
3cc12 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 20  Expr->x.pSelect 
3cc13 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e  = sqlite3SelectN
3cc14 65 77 28 70 50 61 72 73 65 2c 20 30 2c 70 53 72  ew(pParse, 0,pSr
3cc15 63 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 29  c,0,0,0,0,0,0,0)
3cc16 3b 0a 20 20 20 20 20 20 45 78 70 72 53 65 74 50  ;.      ExprSetP
3cc17 72 6f 70 65 72 74 79 28 79 79 67 6f 74 6f 6d 69  roperty(yygotomi
3cc18 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 2c  nor.yy342.pExpr,
3cc19 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a   EP_xIsSelect);.
3cc1a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
3cc1b 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72 73  rSetHeight(pPars
3cc1c 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  e, yygotominor.y
3cc1d 79 33 34 32 2e 70 45 78 70 72 29 3b 0a 20 20 20  y342.pExpr);.   
3cc1e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
3cc1f 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
3cc20 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
3cc21 53 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Src);.    }.    
3cc22 69 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  if( yymsp[-2].mi
3cc23 6e 6f 72 2e 79 79 33 39 32 20 29 20 79 79 67 6f  nor.yy392 ) yygo
3cc24 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45  tominor.yy342.pE
3cc25 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78  xpr = sqlite3PEx
3cc26 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f  pr(pParse, TK_NO
3cc27 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  T, yygotominor.y
3cc28 79 33 34 32 2e 70 45 78 70 72 2c 20 30 2c 20 30  y342.pExpr, 0, 0
3cc29 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  );.    yygotomin
3cc2a 6f 72 2e 79 79 33 34 32 2e 7a 53 74 61 72 74 20  or.yy342.zStart 
3cc2b 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  = yymsp[-3].mino
3cc2c 72 2e 79 79 33 34 32 2e 7a 53 74 61 72 74 3b 0a  r.yy342.zStart;.
3cc2d 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
3cc2e 79 79 33 34 32 2e 7a 45 6e 64 20 3d 20 79 79 6d  yy342.zEnd = yym
3cc2f 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
3cc30 7a 20 3f 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  z ? &yymsp[0].mi
3cc31 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b  nor.yy0.z[yymsp[
3cc32 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20  0].minor.yy0.n] 
3cc33 3a 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  : &yymsp[-1].min
3cc34 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 2d  or.yy0.z[yymsp[-
3cc35 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b  1].minor.yy0.n];
3cc36 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  .  }.        bre
3cc37 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
3cc38 33 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  30: /* expr ::= 
3cc39 45 58 49 53 54 53 20 4c 50 20 73 65 6c 65 63 74  EXISTS LP select
3cc3a 20 52 50 20 2a 2f 0a 7b 0a 20 20 20 20 45 78 70   RP */.{.    Exp
3cc3b 72 20 2a 70 20 3d 20 79 79 67 6f 74 6f 6d 69 6e  r *p = yygotomin
3cc3c 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 20 3d  or.yy342.pExpr =
3cc3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
3cc3e 61 72 73 65 2c 20 54 4b 5f 45 58 49 53 54 53 2c  arse, TK_EXISTS,
3cc3f 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69   0, 0, 0);.    i
3cc40 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 70 2d  f( p ){.      p-
3cc41 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 79 79 6d  >x.pSelect = yym
3cc42 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
3cc43 35 39 3b 0a 20 20 20 20 20 20 45 78 70 72 53 65  59;.      ExprSe
3cc44 74 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f  tProperty(p, EP_
3cc45 78 49 73 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  xIsSelect);.    
3cc46 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74    sqlite3ExprSet
3cc47 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 70  Height(pParse, p
3cc48 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
3cc49 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
3cc4a 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
3cc4b 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  db, yymsp[-1].mi
3cc4c 6e 6f 72 2e 79 79 31 35 39 29 3b 0a 20 20 20 20  nor.yy159);.    
3cc4d 7d 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  }.    yygotomino
3cc4e 72 2e 79 79 33 34 32 2e 7a 53 74 61 72 74 20 3d  r.yy342.zStart =
3cc4f 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
3cc50 2e 79 79 30 2e 7a 3b 0a 20 20 20 20 79 79 67 6f  .yy0.z;.    yygo
3cc51 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 7a 45  tominor.yy342.zE
3cc52 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  nd = &yymsp[0].m
3cc53 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70  inor.yy0.z[yymsp
3cc54 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d  [0].minor.yy0.n]
3cc55 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ;.  }.        br
3cc56 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3cc57 32 33 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  231: /* expr ::=
3cc58 20 43 41 53 45 20 63 61 73 65 5f 6f 70 65 72 61   CASE case_opera
3cc59 6e 64 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74  nd case_exprlist
3cc5a 20 63 61 73 65 5f 65 6c 73 65 20 45 4e 44 20 2a   case_else END *
3cc5b 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
3cc5c 72 2e 79 79 33 34 32 2e 70 45 78 70 72 20 3d 20  r.yy342.pExpr = 
3cc5d 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
3cc5e 72 73 65 2c 20 54 4b 5f 43 41 53 45 2c 20 79 79  rse, TK_CASE, yy
3cc5f 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
3cc60 31 32 32 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  122, yymsp[-1].m
3cc61 69 6e 6f 72 2e 79 79 31 32 32 2c 20 30 29 3b 0a  inor.yy122, 0);.
3cc62 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
3cc63 72 2e 79 79 33 34 32 2e 70 45 78 70 72 20 29 7b  r.yy342.pExpr ){
3cc64 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
3cc65 2e 79 79 33 34 32 2e 70 45 78 70 72 2d 3e 78 2e  .yy342.pExpr->x.
3cc66 70 4c 69 73 74 20 3d 20 79 79 6d 73 70 5b 2d 32  pList = yymsp[-2
3cc67 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 3b 0a 20  ].minor.yy442;. 
3cc68 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65     sqlite3ExprSe
3cc69 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20  tHeight(pParse, 
3cc6a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3cc6b 32 2e 70 45 78 70 72 29 3b 0a 20 20 7d 65 6c 73  2.pExpr);.  }els
3cc6c 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  e{.    sqlite3Ex
3cc6d 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61  prListDelete(pPa
3cc6e 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d  rse->db, yymsp[-
3cc6f 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32 29 3b  2].minor.yy442);
3cc70 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  .  }.  yygotomin
3cc71 6f 72 2e 79 79 33 34 32 2e 7a 53 74 61 72 74 20  or.yy342.zStart 
3cc72 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  = yymsp[-4].mino
3cc73 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74  r.yy0.z;.  yygot
3cc74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 7a 45 6e  ominor.yy342.zEn
3cc75 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  d = &yymsp[0].mi
3cc76 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b  nor.yy0.z[yymsp[
3cc77 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b  0].minor.yy0.n];
3cc78 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
3cc79 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 32  ;.      case 232
3cc7a 3a 20 2f 2a 20 63 61 73 65 5f 65 78 70 72 6c 69  : /* case_exprli
3cc7b 73 74 20 3a 3a 3d 20 63 61 73 65 5f 65 78 70 72  st ::= case_expr
3cc7c 6c 69 73 74 20 57 48 45 4e 20 65 78 70 72 20 54  list WHEN expr T
3cc7d 48 45 4e 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20  HEN expr */.{.  
3cc7e 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34  yygotominor.yy44
3cc7f 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  2 = sqlite3ExprL
3cc80 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
3cc81 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  ,yymsp[-4].minor
3cc82 2e 79 79 34 34 32 2c 20 79 79 6d 73 70 5b 2d 32  .yy442, yymsp[-2
3cc83 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45  ].minor.yy342.pE
3cc84 78 70 72 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  xpr);.  yygotomi
3cc85 6e 6f 72 2e 79 79 34 34 32 20 3d 20 73 71 6c 69  nor.yy442 = sqli
3cc86 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
3cc87 64 28 70 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d  d(pParse,yygotom
3cc88 69 6e 6f 72 2e 79 79 34 34 32 2c 20 79 79 6d 73  inor.yy442, yyms
3cc89 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32  p[0].minor.yy342
3cc8a 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20 20  .pExpr);.}.     
3cc8b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3cc8c 63 61 73 65 20 32 33 33 3a 20 2f 2a 20 63 61 73  case 233: /* cas
3cc8d 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 57  e_exprlist ::= W
3cc8e 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78  HEN expr THEN ex
3cc8f 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  pr */.{.  yygoto
3cc90 6d 69 6e 6f 72 2e 79 79 34 34 32 20 3d 20 73 71  minor.yy442 = sq
3cc91 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
3cc92 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 79 79  end(pParse,0, yy
3cc93 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
3cc94 33 34 32 2e 70 45 78 70 72 29 3b 0a 20 20 79 79  342.pExpr);.  yy
3cc95 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34 32 20  gotominor.yy442 
3cc96 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
3cc97 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79  tAppend(pParse,y
3cc98 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34 32  ygotominor.yy442
3cc99 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
3cc9a 2e 79 79 33 34 32 2e 70 45 78 70 72 29 3b 0a 7d  .yy342.pExpr);.}
3cc9b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3cc9c 20 20 20 20 20 20 63 61 73 65 20 32 34 30 3a 20        case 240: 
3cc9d 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d  /* nexprlist ::=
3cc9e 20 6e 65 78 70 72 6c 69 73 74 20 43 4f 4d 4d 41   nexprlist COMMA
3cc9f 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f   expr */.{yygoto
3cca0 6d 69 6e 6f 72 2e 79 79 34 34 32 20 3d 20 73 71  minor.yy442 = sq
3cca1 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
3cca2 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70  end(pParse,yymsp
3cca3 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34 32  [-2].minor.yy442
3cca4 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
3cca5 79 79 33 34 32 2e 70 45 78 70 72 29 3b 7d 0a 20  yy342.pExpr);}. 
3cca6 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3cca7 20 20 20 20 63 61 73 65 20 32 34 31 3a 20 2f 2a      case 241: /*
3cca8 20 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 65   nexprlist ::= e
3cca9 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  xpr */.{yygotomi
3ccaa 6e 6f 72 2e 79 79 34 34 32 20 3d 20 73 71 6c 69  nor.yy442 = sqli
3ccab 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
3ccac 64 28 70 50 61 72 73 65 2c 30 2c 79 79 6d 73 70  d(pParse,0,yymsp
3ccad 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e  [0].minor.yy342.
3ccae 70 45 78 70 72 29 3b 7d 0a 20 20 20 20 20 20 20  pExpr);}.       
3ccaf 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3ccb0 73 65 20 32 34 32 3a 20 2f 2a 20 63 6d 64 20 3a  se 242: /* cmd :
3ccb1 3a 3d 20 63 72 65 61 74 65 6b 77 20 75 6e 69 71  := createkw uniq
3ccb2 75 65 66 6c 61 67 20 49 4e 44 45 58 20 69 66 6e  ueflag INDEX ifn
3ccb3 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d  otexists nm dbnm
3ccb4 20 4f 4e 20 6e 6d 20 4c 50 20 69 64 78 6c 69 73   ON nm LP idxlis
3ccb5 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  t RP */.{.  sqli
3ccb6 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 70  te3CreateIndex(p
3ccb7 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 36  Parse, &yymsp[-6
3ccb8 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
3ccb9 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
3ccba 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  0, .            
3ccbb 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
3ccbc 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 70 50  SrcListAppend(pP
3ccbd 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79 6d 73  arse->db,0,&yyms
3ccbe 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
3ccbf 30 29 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  0), yymsp[-1].mi
3ccc0 6e 6f 72 2e 79 79 34 34 32 2c 20 79 79 6d 73 70  nor.yy442, yymsp
3ccc1 5b 2d 39 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32  [-9].minor.yy392
3ccc2 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
3ccc3 20 20 20 20 20 20 20 20 26 79 79 6d 73 70 5b 2d          &yymsp[-
3ccc4 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  10].minor.yy0, &
3ccc5 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3ccc6 79 30 2c 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53  y0, SQLITE_SO_AS
3ccc7 43 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e  C, yymsp[-7].min
3ccc8 6f 72 2e 79 79 33 39 32 29 3b 0a 7d 0a 20 20 20  or.yy392);.}.   
3ccc9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3ccca 20 20 63 61 73 65 20 32 34 33 3a 20 2f 2a 20 75    case 243: /* u
3cccb 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20 55 4e  niqueflag ::= UN
3cccc 49 51 55 45 20 2a 2f 0a 20 20 20 20 20 20 63 61  IQUE */.      ca
3cccd 73 65 20 32 39 36 3a 20 2f 2a 20 72 61 69 73 65  se 296: /* raise
3ccce 74 79 70 65 20 3a 3a 3d 20 41 42 4f 52 54 20 2a  type ::= ABORT *
3cccf 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3ccd0 75 6c 65 6e 6f 3d 3d 32 39 36 29 3b 0a 7b 79 79  uleno==296);.{yy
3ccd1 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 20  gotominor.yy392 
3ccd2 3d 20 4f 45 5f 41 62 6f 72 74 3b 7d 0a 20 20 20  = OE_Abort;}.   
3ccd3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3ccd4 20 20 63 61 73 65 20 32 34 34 3a 20 2f 2a 20 75    case 244: /* u
3ccd5 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20 2a 2f  niqueflag ::= */
3ccd6 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
3ccd7 33 39 32 20 3d 20 4f 45 5f 4e 6f 6e 65 3b 7d 0a  392 = OE_None;}.
3ccd8 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3ccd9 20 20 20 20 20 63 61 73 65 20 32 34 37 3a 20 2f       case 247: /
3ccda 2a 20 69 64 78 6c 69 73 74 20 3a 3a 3d 20 69 64  * idxlist ::= id
3ccdb 78 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 63  xlist COMMA nm c
3ccdc 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72  ollate sortorder
3ccdd 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20   */.{.  Expr *p 
3ccde 3d 20 30 3b 0a 20 20 69 66 28 20 79 79 6d 73 70  = 0;.  if( yymsp
3ccdf 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  [-1].minor.yy0.n
3cce0 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71  >0 ){.    p = sq
3cce1 6c 69 74 65 33 45 78 70 72 28 70 50 61 72 73 65  lite3Expr(pParse
3cce2 2d 3e 64 62 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c  ->db, TK_COLUMN,
3cce3 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
3cce4 45 78 70 72 53 65 74 43 6f 6c 6c 42 79 54 6f 6b  ExprSetCollByTok
3cce5 65 6e 28 70 50 61 72 73 65 2c 20 70 2c 20 26 79  en(pParse, p, &y
3cce6 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3cce7 79 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74  y0);.  }.  yygot
3cce8 6f 6d 69 6e 6f 72 2e 79 79 34 34 32 20 3d 20 73  ominor.yy442 = s
3cce9 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
3ccea 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73  pend(pParse,yyms
3cceb 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 34  p[-4].minor.yy44
3ccec 32 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  2, p);.  sqlite3
3cced 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28  ExprListSetName(
3ccee 70 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d 69 6e  pParse,yygotomin
3ccef 6f 72 2e 79 79 34 34 32 2c 26 79 79 6d 73 70 5b  or.yy442,&yymsp[
3ccf0 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 31 29  -2].minor.yy0,1)
3ccf1 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
3ccf2 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 70  istCheckLength(p
3ccf3 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
3ccf4 6f 72 2e 79 79 34 34 32 2c 20 22 69 6e 64 65 78  or.yy442, "index
3ccf5 22 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f  ");.  if( yygoto
3ccf6 6d 69 6e 6f 72 2e 79 79 34 34 32 20 29 20 79 79  minor.yy442 ) yy
3ccf7 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34 32 2d  gotominor.yy442-
3ccf8 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  >a[yygotominor.y
3ccf9 79 34 34 32 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73  y442->nExpr-1].s
3ccfa 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 29 79  ortOrder = (u8)y
3ccfb 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3ccfc 33 39 32 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  392;.}.        b
3ccfd 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3ccfe 20 32 34 38 3a 20 2f 2a 20 69 64 78 6c 69 73 74   248: /* idxlist
3ccff 20 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61 74 65 20   ::= nm collate 
3cd00 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20  sortorder */.{. 
3cd01 20 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a 20 20   Expr *p = 0;.  
3cd02 69 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  if( yymsp[-1].mi
3cd03 6e 6f 72 2e 79 79 30 2e 6e 3e 30 20 29 7b 0a 20  nor.yy0.n>0 ){. 
3cd04 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 50 45     p = sqlite3PE
3cd05 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43  xpr(pParse, TK_C
3cd06 4f 4c 55 4d 4e 2c 20 30 2c 20 30 2c 20 30 29 3b  OLUMN, 0, 0, 0);
3cd07 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
3cd08 53 65 74 43 6f 6c 6c 42 79 54 6f 6b 65 6e 28 70  SetCollByToken(p
3cd09 50 61 72 73 65 2c 20 70 2c 20 26 79 79 6d 73 70  Parse, p, &yymsp
3cd0a 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-1].minor.yy0);
3cd0b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  .  }.  yygotomin
3cd0c 6f 72 2e 79 79 34 34 32 20 3d 20 73 71 6c 69 74  or.yy442 = sqlit
3cd0d 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
3cd0e 28 70 50 61 72 73 65 2c 30 2c 20 70 29 3b 0a 20  (pParse,0, p);. 
3cd0f 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
3cd10 53 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  SetName(pParse, 
3cd11 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34  yygotominor.yy44
3cd12 32 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  2, &yymsp[-2].mi
3cd13 6e 6f 72 2e 79 79 30 2c 20 31 29 3b 0a 20 20 73  nor.yy0, 1);.  s
3cd14 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68  qlite3ExprListCh
3cd15 65 63 6b 4c 65 6e 67 74 68 28 70 50 61 72 73 65  eckLength(pParse
3cd16 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
3cd17 34 34 32 2c 20 22 69 6e 64 65 78 22 29 3b 0a 20  442, "index");. 
3cd18 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
3cd19 2e 79 79 34 34 32 20 29 20 79 79 67 6f 74 6f 6d  .yy442 ) yygotom
3cd1a 69 6e 6f 72 2e 79 79 34 34 32 2d 3e 61 5b 79 79  inor.yy442->a[yy
3cd1b 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 34 32 2d  gotominor.yy442-
3cd1c 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f 72 74 4f 72  >nExpr-1].sortOr
3cd1d 64 65 72 20 3d 20 28 75 38 29 79 79 6d 73 70 5b  der = (u8)yymsp[
3cd1e 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 3b 0a  0].minor.yy392;.
3cd1f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3cd20 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 39 3a  .      case 249:
3cd21 20 2f 2a 20 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20   /* collate ::= 
3cd22 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
3cd23 79 79 30 2e 7a 20 3d 20 30 3b 20 79 79 67 6f 74  yy0.z = 0; yygot
3cd24 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30  ominor.yy0.n = 0
3cd25 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3cd26 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 31  ;.      case 251
3cd27 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f  : /* cmd ::= DRO
3cd28 50 20 49 4e 44 45 58 20 69 66 65 78 69 73 74 73  P INDEX ifexists
3cd29 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 73 71   fullname */.{sq
3cd2a 6c 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28 70  lite3DropIndex(p
3cd2b 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e  Parse, yymsp[0].
3cd2c 6d 69 6e 6f 72 2e 79 79 33 34 37 2c 20 79 79 6d  minor.yy347, yym
3cd2d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
3cd2e 39 32 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  92);}.        br
3cd2f 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3cd30 32 35 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  252: /* cmd ::= 
3cd31 56 41 43 55 55 4d 20 2a 2f 0a 20 20 20 20 20 20  VACUUM */.      
3cd32 63 61 73 65 20 32 35 33 3a 20 2f 2a 20 63 6d 64  case 253: /* cmd
3cd33 20 3a 3a 3d 20 56 41 43 55 55 4d 20 6e 6d 20 2a   ::= VACUUM nm *
3cd34 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3cd35 75 6c 65 6e 6f 3d 3d 32 35 33 29 3b 0a 7b 73 71  uleno==253);.{sq
3cd36 6c 69 74 65 33 56 61 63 75 75 6d 28 70 50 61 72  lite3Vacuum(pPar
3cd37 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  se);}.        br
3cd38 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3cd39 32 35 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  254: /* cmd ::= 
3cd3a 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 2a  PRAGMA nm dbnm *
3cd3b 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61  /.{sqlite3Pragma
3cd3c 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d  (pParse,&yymsp[-
3cd3d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  1].minor.yy0,&yy
3cd3e 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
3cd3f 2c 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  ,0,0);}.        
3cd40 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3cd41 65 20 32 35 35 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 255: /* cmd ::
3cd42 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d  = PRAGMA nm dbnm
3cd43 20 45 51 20 6e 6d 6e 75 6d 20 2a 2f 0a 7b 73 71   EQ nmnum */.{sq
3cd44 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50 61 72  lite3Pragma(pPar
3cd45 73 65 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  se,&yymsp[-3].mi
3cd46 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d  nor.yy0,&yymsp[-
3cd47 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  2].minor.yy0,&yy
3cd48 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
3cd49 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ,0);}.        br
3cd4a 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3cd4b 32 35 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  256: /* cmd ::= 
3cd4c 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c  PRAGMA nm dbnm L
3cd4d 50 20 6e 6d 6e 75 6d 20 52 50 20 2a 2f 0a 7b 73  P nmnum RP */.{s
3cd4e 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50 61  qlite3Pragma(pPa
3cd4f 72 73 65 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  rse,&yymsp[-4].m
3cd50 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
3cd51 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -3].minor.yy0,&y
3cd52 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3cd53 79 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  y0,0);}.        
3cd54 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3cd55 65 20 32 35 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 257: /* cmd ::
3cd56 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d  = PRAGMA nm dbnm
3cd57 20 45 51 20 6d 69 6e 75 73 5f 6e 75 6d 20 2a 2f   EQ minus_num */
3cd58 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28  .{sqlite3Pragma(
3cd59 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33  pParse,&yymsp[-3
3cd5a 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
3cd5b 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
3cd5c 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
3cd5d 2e 79 79 30 2c 31 29 3b 7d 0a 20 20 20 20 20 20  .yy0,1);}.      
3cd5e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3cd5f 61 73 65 20 32 35 38 3a 20 2f 2a 20 63 6d 64 20  ase 258: /* cmd 
3cd60 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62  ::= PRAGMA nm db
3cd61 6e 6d 20 4c 50 20 6d 69 6e 75 73 5f 6e 75 6d 20  nm LP minus_num 
3cd62 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72  RP */.{sqlite3Pr
3cd63 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d  agma(pParse,&yym
3cd64 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-4].minor.yy0
3cd65 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ,&yymsp[-3].mino
3cd66 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d  r.yy0,&yymsp[-1]
3cd67 2e 6d 69 6e 6f 72 2e 79 79 30 2c 31 29 3b 7d 0a  .minor.yy0,1);}.
3cd68 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3cd69 20 20 20 20 20 63 61 73 65 20 32 36 38 3a 20 2f       case 268: /
3cd6a 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65  * cmd ::= create
3cd6b 6b 77 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20  kw trigger_decl 
3cd6c 42 45 47 49 4e 20 74 72 69 67 67 65 72 5f 63 6d  BEGIN trigger_cm
3cd6d 64 5f 6c 69 73 74 20 45 4e 44 20 2a 2f 0a 7b 0a  d_list END */.{.
3cd6e 20 20 54 6f 6b 65 6e 20 61 6c 6c 3b 0a 20 20 61    Token all;.  a
3cd6f 6c 6c 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 33 5d  ll.z = yymsp[-3]
3cd70 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20  .minor.yy0.z;.  
3cd71 61 6c 6c 2e 6e 20 3d 20 28 69 6e 74 29 28 79 79  all.n = (int)(yy
3cd72 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
3cd73 2e 7a 20 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  .z - yymsp[-3].m
3cd74 69 6e 6f 72 2e 79 79 30 2e 7a 29 20 2b 20 79 79  inor.yy0.z) + yy
3cd75 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
3cd76 2e 6e 3b 0a 20 20 73 71 6c 69 74 65 33 46 69 6e  .n;.  sqlite3Fin
3cd77 69 73 68 54 72 69 67 67 65 72 28 70 50 61 72 73  ishTrigger(pPars
3cd78 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  e, yymsp[-1].min
3cd79 6f 72 2e 79 79 33 32 37 2c 20 26 61 6c 6c 29 3b  or.yy327, &all);
3cd7a 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
3cd7b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 39  ;.      case 269
3cd7c 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 64 65 63  : /* trigger_dec
3cd7d 6c 20 3a 3a 3d 20 74 65 6d 70 20 54 52 49 47 47  l ::= temp TRIGG
3cd7e 45 52 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e  ER ifnotexists n
3cd7f 6d 20 64 62 6e 6d 20 74 72 69 67 67 65 72 5f 74  m dbnm trigger_t
3cd80 69 6d 65 20 74 72 69 67 67 65 72 5f 65 76 65 6e  ime trigger_even
3cd81 74 20 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20 66 6f  t ON fullname fo
3cd82 72 65 61 63 68 5f 63 6c 61 75 73 65 20 77 68 65  reach_clause whe
3cd83 6e 5f 63 6c 61 75 73 65 20 2a 2f 0a 7b 0a 20 20  n_clause */.{.  
3cd84 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67  sqlite3BeginTrig
3cd85 67 65 72 28 70 50 61 72 73 65 2c 20 26 79 79 6d  ger(pParse, &yym
3cd86 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-7].minor.yy0
3cd87 2c 20 26 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  , &yymsp[-6].min
3cd88 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 35  or.yy0, yymsp[-5
3cd89 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39 32 2c 20 79  ].minor.yy392, y
3cd8a 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
3cd8b 79 34 31 30 2e 61 2c 20 79 79 6d 73 70 5b 2d 34  y410.a, yymsp[-4
3cd8c 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 62 2c  ].minor.yy410.b,
3cd8d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
3cd8e 2e 79 79 33 34 37 2c 20 79 79 6d 73 70 5b 30 5d  .yy347, yymsp[0]
3cd8f 2e 6d 69 6e 6f 72 2e 79 79 31 32 32 2c 20 79 79  .minor.yy122, yy
3cd90 6d 73 70 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79  msp[-10].minor.y
3cd91 79 33 39 32 2c 20 79 79 6d 73 70 5b 2d 38 5d 2e  y392, yymsp[-8].
3cd92 6d 69 6e 6f 72 2e 79 79 33 39 32 29 3b 0a 20 20  minor.yy392);.  
3cd93 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20  yygotominor.yy0 
3cd94 3d 20 28 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  = (yymsp[-6].min
3cd95 6f 72 2e 79 79 30 2e 6e 3d 3d 30 3f 79 79 6d 73  or.yy0.n==0?yyms
3cd96 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3a  p[-7].minor.yy0:
3cd97 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
3cd98 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
3cd99 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3cd9a 65 20 32 37 30 3a 20 2f 2a 20 74 72 69 67 67 65  e 270: /* trigge
3cd9b 72 5f 74 69 6d 65 20 3a 3a 3d 20 42 45 46 4f 52  r_time ::= BEFOR
3cd9c 45 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  E */.      case 
3cd9d 32 37 33 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  273: /* trigger_
3cd9e 74 69 6d 65 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  time ::= */ yyte
3cd9f 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3cda0 3d 32 37 33 29 3b 0a 7b 20 79 79 67 6f 74 6f 6d  =273);.{ yygotom
3cda1 69 6e 6f 72 2e 79 79 33 39 32 20 3d 20 54 4b 5f  inor.yy392 = TK_
3cda2 42 45 46 4f 52 45 3b 20 7d 0a 20 20 20 20 20 20  BEFORE; }.      
3cda3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3cda4 61 73 65 20 32 37 31 3a 20 2f 2a 20 74 72 69 67  ase 271: /* trig
3cda5 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 41 46 54  ger_time ::= AFT
3cda6 45 52 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  ER */.{ yygotomi
3cda7 6e 6f 72 2e 79 79 33 39 32 20 3d 20 54 4b 5f 41  nor.yy392 = TK_A
3cda8 46 54 45 52 3b 20 20 7d 0a 20 20 20 20 20 20 20  FTER;  }.       
3cda9 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3cdaa 73 65 20 32 37 32 3a 20 2f 2a 20 74 72 69 67 67  se 272: /* trigg
3cdab 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 49 4e 53 54  er_time ::= INST
3cdac 45 41 44 20 4f 46 20 2a 2f 0a 7b 20 79 79 67 6f  EAD OF */.{ yygo
3cdad 74 6f 6d 69 6e 6f 72 2e 79 79 33 39 32 20 3d 20  tominor.yy392 = 
3cdae 54 4b 5f 49 4e 53 54 45 41 44 3b 7d 0a 20 20 20  TK_INSTEAD;}.   
3cdaf 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3cdb0 20 20 63 61 73 65 20 32 37 34 3a 20 2f 2a 20 74    case 274: /* t
3cdb1 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d  rigger_event ::=
3cdb2 20 44 45 4c 45 54 45 7c 49 4e 53 45 52 54 20 2a   DELETE|INSERT *
3cdb3 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 35  /.      case 275
3cdb4 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76 65  : /* trigger_eve
3cdb5 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 20 2a 2f  nt ::= UPDATE */
3cdb6 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3cdb7 6c 65 6e 6f 3d 3d 32 37 35 29 3b 0a 7b 79 79 67  leno==275);.{yyg
3cdb8 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 61  otominor.yy410.a
3cdb9 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f   = yymsp[0].majo
3cdba 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  r; yygotominor.y
3cdbb 79 34 31 30 2e 62 20 3d 20 30 3b 7d 0a 20 20 20  y410.b = 0;}.   
3cdbc 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3cdbd 20 20 63 61 73 65 20 32 37 36 3a 20 2f 2a 20 74    case 276: /* t
3cdbe 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d  rigger_event ::=
3cdbf 20 55 50 44 41 54 45 20 4f 46 20 69 6e 73 63 6f   UPDATE OF insco
3cdc0 6c 6c 69 73 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f  llist */.{yygoto
3cdc1 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 61 20 3d 20  minor.yy410.a = 
3cdc2 54 4b 5f 55 50 44 41 54 45 3b 20 79 79 67 6f 74  TK_UPDATE; yygot
3cdc3 6f 6d 69 6e 6f 72 2e 79 79 34 31 30 2e 62 20 3d  ominor.yy410.b =
3cdc4 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
3cdc5 79 79 31 38 30 3b 7d 0a 20 20 20 20 20 20 20 20  yy180;}.        
3cdc6 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3cdc7 65 20 32 37 39 3a 20 2f 2a 20 77 68 65 6e 5f 63  e 279: /* when_c
3cdc8 6c 61 75 73 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20  lause ::= */.   
3cdc9 20 20 20 63 61 73 65 20 33 30 31 3a 20 2f 2a 20     case 301: /* 
3cdca 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79  key_opt ::= */ y
3cdcb 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3cdcc 6e 6f 3d 3d 33 30 31 29 3b 0a 7b 20 79 79 67 6f  no==301);.{ yygo
3cdcd 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20  tominor.yy122 = 
3cdce 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0; }.        bre
3cdcf 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
3cdd0 38 30 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c 61 75  80: /* when_clau
3cdd1 73 65 20 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72  se ::= WHEN expr
3cdd2 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
3cdd3 30 32 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 3a  02: /* key_opt :
3cdd4 3a 3d 20 4b 45 59 20 65 78 70 72 20 2a 2f 20 79  := KEY expr */ y
3cdd5 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3cdd6 6e 6f 3d 3d 33 30 32 29 3b 0a 7b 20 79 79 67 6f  no==302);.{ yygo
3cdd7 74 6f 6d 69 6e 6f 72 2e 79 79 31 32 32 20 3d 20  tominor.yy122 = 
3cdd8 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3cdd9 79 33 34 32 2e 70 45 78 70 72 3b 20 7d 0a 20 20  y342.pExpr; }.  
3cdda 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3cddb 20 20 20 63 61 73 65 20 32 38 31 3a 20 2f 2a 20     case 281: /* 
3cddc 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74  trigger_cmd_list
3cddd 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d 64   ::= trigger_cmd
3cdde 5f 6c 69 73 74 20 74 72 69 67 67 65 72 5f 63 6d  _list trigger_cm
3cddf 64 20 53 45 4d 49 20 2a 2f 0a 7b 0a 20 20 61 73  d SEMI */.{.  as
3cde0 73 65 72 74 28 20 79 79 6d 73 70 5b 2d 32 5d 2e  sert( yymsp[-2].
3cde1 6d 69 6e 6f 72 2e 79 79 33 32 37 21 3d 30 20 29  minor.yy327!=0 )
3cde2 3b 0a 20 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  ;.  yymsp[-2].mi
3cde3 6e 6f 72 2e 79 79 33 32 37 2d 3e 70 4c 61 73 74  nor.yy327->pLast
3cde4 2d 3e 70 4e 65 78 74 20 3d 20 79 79 6d 73 70 5b  ->pNext = yymsp[
3cde5 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 37 3b  -1].minor.yy327;
3cde6 0a 20 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  .  yymsp[-2].min
3cde7 6f 72 2e 79 79 33 32 37 2d 3e 70 4c 61 73 74 20  or.yy327->pLast 
3cde8 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
3cde9 72 2e 79 79 33 32 37 3b 0a 20 20 79 79 67 6f 74  r.yy327;.  yygot
3cdea 6f 6d 69 6e 6f 72 2e 79 79 33 32 37 20 3d 20 79  ominor.yy327 = y
3cdeb 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
3cdec 79 33 32 37 3b 0a 7d 0a 20 20 20 20 20 20 20 20  y327;.}.        
3cded 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3cdee 65 20 32 38 32 3a 20 2f 2a 20 74 72 69 67 67 65  e 282: /* trigge
3cdef 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74  r_cmd_list ::= t
3cdf0 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 20  rigger_cmd SEMI 
3cdf1 2a 2f 0a 7b 20 0a 20 20 61 73 73 65 72 74 28 20  */.{ .  assert( 
3cdf2 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3cdf3 79 79 33 32 37 21 3d 30 20 29 3b 0a 20 20 79 79  yy327!=0 );.  yy
3cdf4 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
3cdf5 33 32 37 2d 3e 70 4c 61 73 74 20 3d 20 79 79 6d  327->pLast = yym
3cdf6 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
3cdf7 32 37 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  27;.  yygotomino
3cdf8 72 2e 79 79 33 32 37 20 3d 20 79 79 6d 73 70 5b  r.yy327 = yymsp[
3cdf9 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 37 3b  -1].minor.yy327;
3cdfa 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
3cdfb 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 34  ;.      case 284
3cdfc 3a 20 2f 2a 20 74 72 6e 6d 20 3a 3a 3d 20 6e 6d  : /* trnm ::= nm
3cdfd 20 44 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 79   DOT nm */.{.  y
3cdfe 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d  ygotominor.yy0 =
3cdff 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
3ce00 79 79 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 72  yy0;.  sqlite3Er
3ce01 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
3ce02 20 20 20 20 20 20 20 20 22 71 75 61 6c 69 66 69          "qualifi
3ce03 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 61  ed table names a
3ce04 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f  re not allowed o
3ce05 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  n INSERT, UPDATE
3ce06 2c 20 61 6e 64 20 44 45 4c 45 54 45 20 22 0a 20  , and DELETE ". 
3ce07 20 20 20 20 20 20 20 22 73 74 61 74 65 6d 65 6e         "statemen
3ce08 74 73 20 77 69 74 68 69 6e 20 74 72 69 67 67 65  ts within trigge
3ce09 72 73 22 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  rs");.}.        
3ce0a 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3ce0b 65 20 32 38 36 3a 20 2f 2a 20 74 72 69 64 78 62  e 286: /* tridxb
3ce0c 79 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42 59  y ::= INDEXED BY
3ce0d 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74   nm */.{.  sqlit
3ce0e 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
3ce0f 65 2c 0a 20 20 20 20 20 20 20 20 22 74 68 65 20  e,.        "the 
3ce10 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73  INDEXED BY claus
3ce11 65 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64  e is not allowed
3ce12 20 6f 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45   on UPDATE or DE
3ce13 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20  LETE statements 
3ce14 22 0a 20 20 20 20 20 20 20 20 22 77 69 74 68 69  ".        "withi
3ce15 6e 20 74 72 69 67 67 65 72 73 22 29 3b 0a 7d 0a  n triggers");.}.
3ce16 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3ce17 20 20 20 20 20 63 61 73 65 20 32 38 37 3a 20 2f       case 287: /
3ce18 2a 20 74 72 69 64 78 62 79 20 3a 3a 3d 20 4e 4f  * tridxby ::= NO
3ce19 54 20 49 4e 44 45 58 45 44 20 2a 2f 0a 7b 0a 20  T INDEXED */.{. 
3ce1a 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
3ce1b 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
3ce1c 20 22 74 68 65 20 4e 4f 54 20 49 4e 44 45 58 45   "the NOT INDEXE
3ce1d 44 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20  D clause is not 
3ce1e 61 6c 6c 6f 77 65 64 20 6f 6e 20 55 50 44 41 54  allowed on UPDAT
3ce1f 45 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74  E or DELETE stat
3ce20 65 6d 65 6e 74 73 20 22 0a 20 20 20 20 20 20 20  ements ".       
3ce21 20 22 77 69 74 68 69 6e 20 74 72 69 67 67 65 72   "within trigger
3ce22 73 22 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  s");.}.        b
3ce23 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3ce24 20 32 38 38 3a 20 2f 2a 20 74 72 69 67 67 65 72   288: /* trigger
3ce25 5f 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20  _cmd ::= UPDATE 
3ce26 6f 72 63 6f 6e 66 20 74 72 6e 6d 20 74 72 69 64  orconf trnm trid
3ce27 78 62 79 20 53 45 54 20 73 65 74 6c 69 73 74 20  xby SET setlist 
3ce28 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 20 79  where_opt */.{ y
3ce29 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 37  ygotominor.yy327
3ce2a 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
3ce2b 72 55 70 64 61 74 65 53 74 65 70 28 70 50 61 72  rUpdateStep(pPar
3ce2c 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d  se->db, &yymsp[-
3ce2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79  4].minor.yy0, yy
3ce2e 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
3ce2f 34 34 32 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  442, yymsp[0].mi
3ce30 6e 6f 72 2e 79 79 31 32 32 2c 20 79 79 6d 73 70  nor.yy122, yymsp
3ce31 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 38  [-5].minor.yy258
3ce32 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ); }.        bre
3ce33 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
3ce34 38 39 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63  89: /* trigger_c
3ce35 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
3ce36 64 20 49 4e 54 4f 20 74 72 6e 6d 20 69 6e 73 63  d INTO trnm insc
3ce37 6f 6c 6c 69 73 74 5f 6f 70 74 20 76 61 6c 75 65  ollist_opt value
3ce38 6c 69 73 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  list */.{yygotom
3ce39 69 6e 6f 72 2e 79 79 33 32 37 20 3d 20 73 71 6c  inor.yy327 = sql
3ce3a 69 74 65 33 54 72 69 67 67 65 72 49 6e 73 65 72  ite3TriggerInser
3ce3b 74 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62  tStep(pParse->db
3ce3c 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
3ce3d 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 31  or.yy0, yymsp[-1
3ce3e 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 30 2c 20 79  ].minor.yy180, y
3ce3f 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3ce40 34 38 37 2e 70 4c 69 73 74 2c 20 79 79 6d 73 70  487.pList, yymsp
3ce41 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 38 37 2e  [0].minor.yy487.
3ce42 70 53 65 6c 65 63 74 2c 20 79 79 6d 73 70 5b 2d  pSelect, yymsp[-
3ce43 34 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 38 29 3b  4].minor.yy258);
3ce44 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3ce45 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 30 3a  .      case 290:
3ce46 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 20   /* trigger_cmd 
3ce47 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49  ::= insert_cmd I
3ce48 4e 54 4f 20 74 72 6e 6d 20 69 6e 73 63 6f 6c 6c  NTO trnm inscoll
3ce49 69 73 74 5f 6f 70 74 20 73 65 6c 65 63 74 20 2a  ist_opt select *
3ce4a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
3ce4b 79 33 32 37 20 3d 20 73 71 6c 69 74 65 33 54 72  y327 = sqlite3Tr
3ce4c 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 28  iggerInsertStep(
3ce4d 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d  pParse->db, &yym
3ce4e 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
3ce4f 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
3ce50 72 2e 79 79 31 38 30 2c 20 30 2c 20 79 79 6d 73  r.yy180, 0, yyms
3ce51 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 35 39  p[0].minor.yy159
3ce52 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
3ce53 72 2e 79 79 32 35 38 29 3b 7d 0a 20 20 20 20 20  r.yy258);}.     
3ce54 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3ce55 63 61 73 65 20 32 39 31 3a 20 2f 2a 20 74 72 69  case 291: /* tri
3ce56 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 44 45 4c  gger_cmd ::= DEL
3ce57 45 54 45 20 46 52 4f 4d 20 74 72 6e 6d 20 74 72  ETE FROM trnm tr
3ce58 69 64 78 62 79 20 77 68 65 72 65 5f 6f 70 74 20  idxby where_opt 
3ce59 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
3ce5a 79 79 33 32 37 20 3d 20 73 71 6c 69 74 65 33 54  yy327 = sqlite3T
3ce5b 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70  riggerDeleteStep
3ce5c 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79  (pParse->db, &yy
3ce5d 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
3ce5e 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0, yymsp[0].mino
3ce5f 72 2e 79 79 31 32 32 29 3b 7d 0a 20 20 20 20 20  r.yy122);}.     
3ce60 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3ce61 63 61 73 65 20 32 39 32 3a 20 2f 2a 20 74 72 69  case 292: /* tri
3ce62 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c  gger_cmd ::= sel
3ce63 65 63 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ect */.{yygotomi
3ce64 6e 6f 72 2e 79 79 33 32 37 20 3d 20 73 71 6c 69  nor.yy327 = sqli
3ce65 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 63 74  te3TriggerSelect
3ce66 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c  Step(pParse->db,
3ce67 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
3ce68 79 79 31 35 39 29 3b 20 7d 0a 20 20 20 20 20 20  yy159); }.      
3ce69 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3ce6a 61 73 65 20 32 39 33 3a 20 2f 2a 20 65 78 70 72  ase 293: /* expr
3ce6b 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 49 47   ::= RAISE LP IG
3ce6c 4e 4f 52 45 20 52 50 20 2a 2f 0a 7b 0a 20 20 79  NORE RP */.{.  y
3ce6d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32  ygotominor.yy342
3ce6e 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
3ce6f 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
3ce70 5f 52 41 49 53 45 2c 20 30 2c 20 30 2c 20 30 29  _RAISE, 0, 0, 0)
3ce71 3b 20 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  ; .  if( yygotom
3ce72 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72  inor.yy342.pExpr
3ce73 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69   ){.    yygotomi
3ce74 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 2d  nor.yy342.pExpr-
3ce75 3e 61 66 66 69 6e 69 74 79 20 3d 20 4f 45 5f 49  >affinity = OE_I
3ce76 67 6e 6f 72 65 3b 0a 20 20 7d 0a 20 20 79 79 67  gnore;.  }.  yyg
3ce77 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 7a  otominor.yy342.z
3ce78 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 33  Start = yymsp[-3
3ce79 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20  ].minor.yy0.z;. 
3ce7a 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
3ce7b 34 32 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70  42.zEnd = &yymsp
3ce7c 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b  [0].minor.yy0.z[
3ce7d 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3ce7e 79 30 2e 6e 5d 3b 0a 7d 0a 20 20 20 20 20 20 20  y0.n];.}.       
3ce7f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3ce80 73 65 20 32 39 34 3a 20 2f 2a 20 65 78 70 72 20  se 294: /* expr 
3ce81 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 72 61 69  ::= RAISE LP rai
3ce82 73 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d 20  setype COMMA nm 
3ce83 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  RP */.{.  yygoto
3ce84 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70 45 78 70  minor.yy342.pExp
3ce85 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
3ce86 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 41 49 53  (pParse, TK_RAIS
3ce87 45 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b  E, 0, 0, &yymsp[
3ce88 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 20  -1].minor.yy0); 
3ce89 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e  .  if( yygotomin
3ce8a 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 20 29  or.yy342.pExpr )
3ce8b 20 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e   {.    yygotomin
3ce8c 6f 72 2e 79 79 33 34 32 2e 70 45 78 70 72 2d 3e  or.yy342.pExpr->
3ce8d 61 66 66 69 6e 69 74 79 20 3d 20 28 63 68 61 72  affinity = (char
3ce8e 29 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  )yymsp[-3].minor
3ce8f 2e 79 79 33 39 32 3b 0a 20 20 7d 0a 20 20 79 79  .yy392;.  }.  yy
3ce90 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 32 2e  gotominor.yy342.
3ce91 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d  zStart = yymsp[-
3ce92 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a  5].minor.yy0.z;.
3ce93 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3ce94 33 34 32 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73  342.zEnd = &yyms
3ce95 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
3ce96 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  [yymsp[0].minor.
3ce97 79 79 30 2e 6e 5d 3b 0a 7d 0a 20 20 20 20 20 20  yy0.n];.}.      
3ce98 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3ce99 61 73 65 20 32 39 35 3a 20 2f 2a 20 72 61 69 73  ase 295: /* rais
3ce9a 65 74 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41  etype ::= ROLLBA
3ce9b 43 4b 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  CK */.{yygotomin
3ce9c 6f 72 2e 79 79 33 39 32 20 3d 20 4f 45 5f 52 6f  or.yy392 = OE_Ro
3ce9d 6c 6c 62 61 63 6b 3b 7d 0a 20 20 20 20 20 20 20  llback;}.       
3ce9e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3ce9f 73 65 20 32 39 37 3a 20 2f 2a 20 72 61 69 73 65  se 297: /* raise
3cea0 74 79 70 65 20 3a 3a 3d 20 46 41 49 4c 20 2a 2f  type ::= FAIL */
3cea1 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
3cea2 33 39 32 20 3d 20 4f 45 5f 46 61 69 6c 3b 7d 0a  392 = OE_Fail;}.
3cea3 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3cea4 20 20 20 20 20 63 61 73 65 20 32 39 38 3a 20 2f       case 298: /
3cea5 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54  * cmd ::= DROP T
3cea6 52 49 47 47 45 52 20 69 66 65 78 69 73 74 73 20  RIGGER ifexists 
3cea7 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20  fullname */.{.  
3cea8 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67  sqlite3DropTrigg
3cea9 65 72 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  er(pParse,yymsp[
3ceaa 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 37 2c 79  0].minor.yy347,y
3ceab 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3ceac 79 33 39 32 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y392);.}.       
3cead 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3ceae 73 65 20 32 39 39 3a 20 2f 2a 20 63 6d 64 20 3a  se 299: /* cmd :
3ceaf 3a 3d 20 41 54 54 41 43 48 20 64 61 74 61 62 61  := ATTACH databa
3ceb0 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20 41  se_kw_opt expr A
3ceb1 53 20 65 78 70 72 20 6b 65 79 5f 6f 70 74 20 2a  S expr key_opt *
3ceb2 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 74 74  /.{.  sqlite3Att
3ceb3 61 63 68 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ach(pParse, yyms
3ceb4 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-3].minor.yy34
3ceb5 32 2e 70 45 78 70 72 2c 20 79 79 6d 73 70 5b 2d  2.pExpr, yymsp[-
3ceb6 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 32 2e 70  1].minor.yy342.p
3ceb7 45 78 70 72 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  Expr, yymsp[0].m
3ceb8 69 6e 6f 72 2e 79 79 31 32 32 29 3b 0a 7d 0a 20  inor.yy122);.}. 
3ceb9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3ceba 20 20 20 20 63 61 73 65 20 33 30 30 3a 20 2f 2a      case 300: /*
3cebb 20 63 6d 64 20 3a 3a 3d 20 44 45 54 41 43 48 20   cmd ::= DETACH 
3cebc 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20  database_kw_opt 
3cebd 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  expr */.{.  sqli
3cebe 74 65 33 44 65 74 61 63 68 28 70 50 61 72 73 65  te3Detach(pParse
3cebf 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
3cec0 2e 79 79 33 34 32 2e 70 45 78 70 72 29 3b 0a 7d  .yy342.pExpr);.}
3cec1 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3cec2 20 20 20 20 20 20 63 61 73 65 20 33 30 35 3a 20        case 305: 
3cec3 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44  /* cmd ::= REIND
3cec4 45 58 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 65  EX */.{sqlite3Re
3cec5 69 6e 64 65 78 28 70 50 61 72 73 65 2c 20 30 2c  index(pParse, 0,
3cec6 20 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72   0);}.        br
3cec7 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3cec8 33 30 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  306: /* cmd ::= 
3cec9 52 45 49 4e 44 45 58 20 6e 6d 20 64 62 6e 6d 20  REINDEX nm dbnm 
3ceca 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 65 69 6e 64  */.{sqlite3Reind
3cecb 65 78 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  ex(pParse, &yyms
3cecc 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-1].minor.yy0,
3cecd 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
3cece 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
3cecf 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3ced0 65 20 33 30 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 307: /* cmd ::
3ced1 3d 20 41 4e 41 4c 59 5a 45 20 2a 2f 0a 7b 73 71  = ANALYZE */.{sq
3ced2 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 70 50 61  lite3Analyze(pPa
3ced3 72 73 65 2c 20 30 2c 20 30 29 3b 7d 0a 20 20 20  rse, 0, 0);}.   
3ced4 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3ced5 20 20 63 61 73 65 20 33 30 38 3a 20 2f 2a 20 63    case 308: /* c
3ced6 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e  md ::= ANALYZE n
3ced7 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74  m dbnm */.{sqlit
3ced8 65 33 41 6e 61 6c 79 7a 65 28 70 50 61 72 73 65  e3Analyze(pParse
3ced9 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  , &yymsp[-1].min
3ceda 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30  or.yy0, &yymsp[0
3cedb 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
3cedc 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3cedd 20 20 20 20 63 61 73 65 20 33 30 39 3a 20 2f 2a      case 309: /*
3cede 20 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54   cmd ::= ALTER T
3cedf 41 42 4c 45 20 66 75 6c 6c 6e 61 6d 65 20 52 45  ABLE fullname RE
3cee0 4e 41 4d 45 20 54 4f 20 6e 6d 20 2a 2f 0a 7b 0a  NAME TO nm */.{.
3cee1 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52 65    sqlite3AlterRe
3cee2 6e 61 6d 65 54 61 62 6c 65 28 70 50 61 72 73 65  nameTable(pParse
3cee3 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  ,yymsp[-3].minor
3cee4 2e 79 79 33 34 37 2c 26 79 79 6d 73 70 5b 30 5d  .yy347,&yymsp[0]
3cee5 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
3cee6 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3cee7 20 20 20 20 63 61 73 65 20 33 31 30 3a 20 2f 2a      case 310: /*
3cee8 20 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54   cmd ::= ALTER T
3cee9 41 42 4c 45 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f  ABLE add_column_
3ceea 66 75 6c 6c 6e 61 6d 65 20 41 44 44 20 6b 77 63  fullname ADD kwc
3ceeb 6f 6c 75 6d 6e 5f 6f 70 74 20 63 6f 6c 75 6d 6e  olumn_opt column
3ceec 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41   */.{.  sqlite3A
3ceed 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c  lterFinishAddCol
3ceee 75 6d 6e 28 70 50 61 72 73 65 2c 20 26 79 79 6d  umn(pParse, &yym
3ceef 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
3cef0 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
3cef1 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 31  k;.      case 31
3cef2 31 3a 20 2f 2a 20 61 64 64 5f 63 6f 6c 75 6d 6e  1: /* add_column
3cef3 5f 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75  _fullname ::= fu
3cef4 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 70 50  llname */.{.  pP
3cef5 61 72 73 65 2d 3e 64 62 2d 3e 6c 6f 6f 6b 61 73  arse->db->lookas
3cef6 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30  ide.bEnabled = 0
3cef7 3b 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72  ;.  sqlite3Alter
3cef8 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 70  BeginAddColumn(p
3cef9 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e  Parse, yymsp[0].
3cefa 6d 69 6e 6f 72 2e 79 79 33 34 37 29 3b 0a 7d 0a  minor.yy347);.}.
3cefb 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3cefc 20 20 20 20 20 63 61 73 65 20 33 31 34 3a 20 2f       case 314: /
3cefd 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65  * cmd ::= create
3cefe 5f 76 74 61 62 20 2a 2f 0a 7b 73 71 6c 69 74 65  _vtab */.{sqlite
3ceff 33 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65  3VtabFinishParse
3cf00 28 70 50 61 72 73 65 2c 30 29 3b 7d 0a 20 20 20  (pParse,0);}.   
3cf01 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3cf02 20 20 63 61 73 65 20 33 31 35 3a 20 2f 2a 20 63    case 315: /* c
3cf03 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74  md ::= create_vt
3cf04 61 62 20 4c 50 20 76 74 61 62 61 72 67 6c 69 73  ab LP vtabarglis
3cf05 74 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  t RP */.{sqlite3
3cf06 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65 28  VtabFinishParse(
3cf07 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d  pParse,&yymsp[0]
3cf08 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20  .minor.yy0);}.  
3cf09 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3cf0a 20 20 20 63 61 73 65 20 33 31 36 3a 20 2f 2a 20     case 316: /* 
3cf0b 63 72 65 61 74 65 5f 76 74 61 62 20 3a 3a 3d 20  create_vtab ::= 
3cf0c 63 72 65 61 74 65 6b 77 20 56 49 52 54 55 41 4c  createkw VIRTUAL
3cf0d 20 54 41 42 4c 45 20 69 66 6e 6f 74 65 78 69 73   TABLE ifnotexis
3cf0e 74 73 20 6e 6d 20 64 62 6e 6d 20 55 53 49 4e 47  ts nm dbnm USING
3cf0f 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c   nm */.{.    sql
3cf10 69 74 65 33 56 74 61 62 42 65 67 69 6e 50 61 72  ite3VtabBeginPar
3cf11 73 65 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  se(pParse, &yyms
3cf12 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
3cf13 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f   &yymsp[-2].mino
3cf14 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d  r.yy0, &yymsp[0]
3cf15 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73  .minor.yy0, yyms
3cf16 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 39  p[-4].minor.yy39
3cf17 32 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  2);.}.        br
3cf18 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3cf19 33 31 39 3a 20 2f 2a 20 76 74 61 62 61 72 67 20  319: /* vtabarg 
3cf1a 3a 3a 3d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56  ::= */.{sqlite3V
3cf1b 74 61 62 41 72 67 49 6e 69 74 28 70 50 61 72 73  tabArgInit(pPars
3cf1c 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  e);}.        bre
3cf1d 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
3cf1e 32 31 3a 20 2f 2a 20 76 74 61 62 61 72 67 74 6f  21: /* vtabargto
3cf1f 6b 65 6e 20 3a 3a 3d 20 41 4e 59 20 2a 2f 0a 20  ken ::= ANY */. 
3cf20 20 20 20 20 20 63 61 73 65 20 33 32 32 3a 20 2f       case 322: /
3cf21 2a 20 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a  * vtabargtoken :
3cf22 3a 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20 52 50  := lp anylist RP
3cf23 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3cf24 79 72 75 6c 65 6e 6f 3d 3d 33 32 32 29 3b 0a 20  yruleno==322);. 
3cf25 20 20 20 20 20 63 61 73 65 20 33 32 33 3a 20 2f       case 323: /
3cf26 2a 20 6c 70 20 3a 3a 3d 20 4c 50 20 2a 2f 20 79  * lp ::= LP */ y
3cf27 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3cf28 6e 6f 3d 3d 33 32 33 29 3b 0a 7b 73 71 6c 69 74  no==323);.{sqlit
3cf29 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 28  e3VtabArgExtend(
3cf2a 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d  pParse,&yymsp[0]
3cf2b 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20  .minor.yy0);}.  
3cf2c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3cf2d 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20     default:.    
3cf2e 20 20 2f 2a 20 28 30 29 20 69 6e 70 75 74 20 3a    /* (0) input :
3cf2f 3a 3d 20 63 6d 64 6c 69 73 74 20 2a 2f 20 79 79  := cmdlist */ yy
3cf30 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3cf31 6f 3d 3d 30 29 3b 0a 20 20 20 20 20 20 2f 2a 20  o==0);.      /* 
3cf32 28 31 29 20 63 6d 64 6c 69 73 74 20 3a 3a 3d 20  (1) cmdlist ::= 
3cf33 63 6d 64 6c 69 73 74 20 65 63 6d 64 20 2a 2f 20  cmdlist ecmd */ 
3cf34 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3cf35 65 6e 6f 3d 3d 31 29 3b 0a 20 20 20 20 20 20 2f  eno==1);.      /
3cf36 2a 20 28 32 29 20 63 6d 64 6c 69 73 74 20 3a 3a  * (2) cmdlist ::
3cf37 3d 20 65 63 6d 64 20 2a 2f 20 79 79 74 65 73 74  = ecmd */ yytest
3cf38 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
3cf39 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 29 20  );.      /* (3) 
3cf3a 65 63 6d 64 20 3a 3a 3d 20 53 45 4d 49 20 2a 2f  ecmd ::= SEMI */
3cf3b 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3cf3c 6c 65 6e 6f 3d 3d 33 29 3b 0a 20 20 20 20 20 20  leno==3);.      
3cf3d 2f 2a 20 28 34 29 20 65 63 6d 64 20 3a 3a 3d 20  /* (4) ecmd ::= 
3cf3e 65 78 70 6c 61 69 6e 20 63 6d 64 78 20 53 45 4d  explain cmdx SEM
3cf3f 49 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  I */ yytestcase(
3cf40 79 79 72 75 6c 65 6e 6f 3d 3d 34 29 3b 0a 20 20  yyruleno==4);.  
3cf41 20 20 20 20 2f 2a 20 28 31 30 29 20 74 72 61 6e      /* (10) tran
3cf42 73 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74  s_opt ::= */ yyt
3cf43 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3cf44 3d 3d 31 30 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ==10);.      /* 
3cf45 28 31 31 29 20 74 72 61 6e 73 5f 6f 70 74 20 3a  (11) trans_opt :
3cf46 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 20 2a  := TRANSACTION *
3cf47 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3cf48 75 6c 65 6e 6f 3d 3d 31 31 29 3b 0a 20 20 20 20  uleno==11);.    
3cf49 20 20 2f 2a 20 28 31 32 29 20 74 72 61 6e 73 5f    /* (12) trans_
3cf4a 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54  opt ::= TRANSACT
3cf4b 49 4f 4e 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74  ION nm */ yytest
3cf4c 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
3cf4d 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 30  2);.      /* (20
3cf4e 29 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20  ) savepoint_opt 
3cf4f 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54 20 2a 2f  ::= SAVEPOINT */
3cf50 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3cf51 6c 65 6e 6f 3d 3d 32 30 29 3b 0a 20 20 20 20 20  leno==20);.     
3cf52 20 2f 2a 20 28 32 31 29 20 73 61 76 65 70 6f 69   /* (21) savepoi
3cf53 6e 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79  nt_opt ::= */ yy
3cf54 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3cf55 6f 3d 3d 32 31 29 3b 0a 20 20 20 20 20 20 2f 2a  o==21);.      /*
3cf56 20 28 32 35 29 20 63 6d 64 20 3a 3a 3d 20 63 72   (25) cmd ::= cr
3cf57 65 61 74 65 5f 74 61 62 6c 65 20 63 72 65 61 74  eate_table creat
3cf58 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 2a 2f 20  e_table_args */ 
3cf59 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3cf5a 65 6e 6f 3d 3d 32 35 29 3b 0a 20 20 20 20 20 20  eno==25);.      
3cf5b 2f 2a 20 28 33 34 29 20 63 6f 6c 75 6d 6e 6c 69  /* (34) columnli
3cf5c 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73  st ::= columnlis
3cf5d 74 20 43 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 20 2a  t COMMA column *
3cf5e 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3cf5f 75 6c 65 6e 6f 3d 3d 33 34 29 3b 0a 20 20 20 20  uleno==34);.    
3cf60 20 20 2f 2a 20 28 33 35 29 20 63 6f 6c 75 6d 6e    /* (35) column
3cf61 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 20  list ::= column 
3cf62 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3cf63 72 75 6c 65 6e 6f 3d 3d 33 35 29 3b 0a 20 20 20  ruleno==35);.   
3cf64 20 20 20 2f 2a 20 28 34 34 29 20 74 79 70 65 20     /* (44) type 
3cf65 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
3cf66 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 34 29 3b  e(yyruleno==44);
3cf67 0a 20 20 20 20 20 20 2f 2a 20 28 35 31 29 20 73  .      /* (51) s
3cf68 69 67 6e 65 64 20 3a 3a 3d 20 70 6c 75 73 5f 6e  igned ::= plus_n
3cf69 75 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  um */ yytestcase
3cf6a 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 31 29 3b 0a  (yyruleno==51);.
3cf6b 20 20 20 20 20 20 2f 2a 20 28 35 32 29 20 73 69        /* (52) si
3cf6c 67 6e 65 64 20 3a 3a 3d 20 6d 69 6e 75 73 5f 6e  gned ::= minus_n
3cf6d 75 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  um */ yytestcase
3cf6e 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 32 29 3b 0a  (yyruleno==52);.
3cf6f 20 20 20 20 20 20 2f 2a 20 28 35 33 29 20 63 61        /* (53) ca
3cf70 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61 72 67 6c  rglist ::= cargl
3cf71 69 73 74 20 63 63 6f 6e 73 20 2a 2f 20 79 79 74  ist ccons */ yyt
3cf72 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3cf73 3d 3d 35 33 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ==53);.      /* 
3cf74 28 35 34 29 20 63 61 72 67 6c 69 73 74 20 3a 3a  (54) carglist ::
3cf75 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
3cf76 79 79 72 75 6c 65 6e 6f 3d 3d 35 34 29 3b 0a 20  yyruleno==54);. 
3cf77 20 20 20 20 20 2f 2a 20 28 36 31 29 20 63 63 6f       /* (61) cco
3cf78 6e 73 20 3a 3a 3d 20 4e 55 4c 4c 20 6f 6e 63 6f  ns ::= NULL onco
3cf79 6e 66 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  nf */ yytestcase
3cf7a 28 79 79 72 75 6c 65 6e 6f 3d 3d 36 31 29 3b 0a  (yyruleno==61);.
3cf7b 20 20 20 20 20 20 2f 2a 20 28 38 39 29 20 63 6f        /* (89) co
3cf7c 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e 73 6c  nslist ::= consl
3cf7d 69 73 74 20 74 63 6f 6e 73 63 6f 6d 6d 61 20 74  ist tconscomma t
3cf7e 63 6f 6e 73 20 2a 2f 20 79 79 74 65 73 74 63 61  cons */ yytestca
3cf7f 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 38 39 29  se(yyruleno==89)
3cf80 3b 0a 20 20 20 20 20 20 2f 2a 20 28 39 30 29 20  ;.      /* (90) 
3cf81 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 74 63 6f  conslist ::= tco
3cf82 6e 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  ns */ yytestcase
3cf83 28 79 79 72 75 6c 65 6e 6f 3d 3d 39 30 29 3b 0a  (yyruleno==90);.
3cf84 20 20 20 20 20 20 2f 2a 20 28 39 32 29 20 74 63        /* (92) tc
3cf85 6f 6e 73 63 6f 6d 6d 61 20 3a 3a 3d 20 2a 2f 20  onscomma ::= */ 
3cf86 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3cf87 65 6e 6f 3d 3d 39 32 29 3b 0a 20 20 20 20 20 20  eno==92);.      
3cf88 2f 2a 20 28 32 37 37 29 20 66 6f 72 65 61 63 68  /* (277) foreach
3cf89 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 2a 2f 20 79  _clause ::= */ y
3cf8a 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3cf8b 6e 6f 3d 3d 32 37 37 29 3b 0a 20 20 20 20 20 20  no==277);.      
3cf8c 2f 2a 20 28 32 37 38 29 20 66 6f 72 65 61 63 68  /* (278) foreach
3cf8d 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 46 4f 52 20  _clause ::= FOR 
3cf8e 45 41 43 48 20 52 4f 57 20 2a 2f 20 79 79 74 65  EACH ROW */ yyte
3cf8f 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3cf90 3d 32 37 38 29 3b 0a 20 20 20 20 20 20 2f 2a 20  =278);.      /* 
3cf91 28 32 38 35 29 20 74 72 69 64 78 62 79 20 3a 3a  (285) tridxby ::
3cf92 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
3cf93 79 79 72 75 6c 65 6e 6f 3d 3d 32 38 35 29 3b 0a  yyruleno==285);.
3cf94 20 20 20 20 20 20 2f 2a 20 28 33 30 33 29 20 64        /* (303) d
3cf95 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a  atabase_kw_opt :
3cf96 3a 3d 20 44 41 54 41 42 41 53 45 20 2a 2f 20 79  := DATABASE */ y
3cf97 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3cf98 6e 6f 3d 3d 33 30 33 29 3b 0a 20 20 20 20 20 20  no==303);.      
3cf99 2f 2a 20 28 33 30 34 29 20 64 61 74 61 62 61 73  /* (304) databas
3cf9a 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20  e_kw_opt ::= */ 
3cf9b 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3cf9c 65 6e 6f 3d 3d 33 30 34 29 3b 0a 20 20 20 20 20  eno==304);.     
3cf9d 20 2f 2a 20 28 33 31 32 29 20 6b 77 63 6f 6c 75   /* (312) kwcolu
3cf9e 6d 6e 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79  mn_opt ::= */ yy
3cf9f 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3cfa0 6f 3d 3d 33 31 32 29 3b 0a 20 20 20 20 20 20 2f  o==312);.      /
3cfa1 2a 20 28 33 31 33 29 20 6b 77 63 6f 6c 75 6d 6e  * (313) kwcolumn
3cfa2 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b  _opt ::= COLUMNK
3cfa3 57 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  W */ yytestcase(
3cfa4 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 33 29 3b 0a  yyruleno==313);.
3cfa5 20 20 20 20 20 20 2f 2a 20 28 33 31 37 29 20 76        /* (317) v
3cfa6 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76  tabarglist ::= v
3cfa7 74 61 62 61 72 67 20 2a 2f 20 79 79 74 65 73 74  tabarg */ yytest
3cfa8 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
3cfa9 31 37 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33  17);.      /* (3
3cfaa 31 38 29 20 76 74 61 62 61 72 67 6c 69 73 74 20  18) vtabarglist 
3cfab 3a 3a 3d 20 76 74 61 62 61 72 67 6c 69 73 74 20  ::= vtabarglist 
3cfac 43 4f 4d 4d 41 20 76 74 61 62 61 72 67 20 2a 2f  COMMA vtabarg */
3cfad 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3cfae 6c 65 6e 6f 3d 3d 33 31 38 29 3b 0a 20 20 20 20  leno==318);.    
3cfaf 20 20 2f 2a 20 28 33 32 30 29 20 76 74 61 62 61    /* (320) vtaba
3cfb0 72 67 20 3a 3a 3d 20 76 74 61 62 61 72 67 20 76  rg ::= vtabarg v
3cfb1 74 61 62 61 72 67 74 6f 6b 65 6e 20 2a 2f 20 79  tabargtoken */ y
3cfb2 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3cfb3 6e 6f 3d 3d 33 32 30 29 3b 0a 20 20 20 20 20 20  no==320);.      
3cfb4 2f 2a 20 28 33 32 34 29 20 61 6e 79 6c 69 73 74  /* (324) anylist
3cfb5 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
3cfb6 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 34  se(yyruleno==324
3cfb7 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 32 35  );.      /* (325
3cfb8 29 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e  ) anylist ::= an
3cfb9 79 6c 69 73 74 20 4c 50 20 61 6e 79 6c 69 73 74  ylist LP anylist
3cfba 20 52 50 20 2a 2f 20 79 79 74 65 73 74 63 61 73   RP */ yytestcas
3cfbb 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 35 29  e(yyruleno==325)
3cfbc 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 32 36 29  ;.      /* (326)
3cfbd 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79   anylist ::= any
3cfbe 6c 69 73 74 20 41 4e 59 20 2a 2f 20 79 79 74 65  list ANY */ yyte
3cfbf 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3cfc0 3d 33 32 36 29 3b 0a 20 20 20 20 20 20 20 20 62  =326);.        b
3cfc1 72 65 61 6b 3b 0a 20 20 7d 3b 0a 20 20 61 73 73  reak;.  };.  ass
3cfc2 65 72 74 28 20 79 79 72 75 6c 65 6e 6f 3e 3d 30  ert( yyruleno>=0
3cfc3 20 26 26 20 79 79 72 75 6c 65 6e 6f 3c 73 69 7a   && yyruleno<siz
3cfc4 65 6f 66 28 79 79 52 75 6c 65 49 6e 66 6f 29 2f  eof(yyRuleInfo)/
3cfc5 73 69 7a 65 6f 66 28 79 79 52 75 6c 65 49 6e 66  sizeof(yyRuleInf
3cfc6 6f 5b 30 5d 29 20 29 3b 0a 20 20 79 79 67 6f 74  o[0]) );.  yygot
3cfc7 6f 20 3d 20 79 79 52 75 6c 65 49 6e 66 6f 5b 79  o = yyRuleInfo[y
3cfc8 79 72 75 6c 65 6e 6f 5d 2e 6c 68 73 3b 0a 20 20  yruleno].lhs;.  
3cfc9 79 79 73 69 7a 65 20 3d 20 79 79 52 75 6c 65 49  yysize = yyRuleI
3cfca 6e 66 6f 5b 79 79 72 75 6c 65 6e 6f 5d 2e 6e 72  nfo[yyruleno].nr
3cfcb 68 73 3b 0a 20 20 79 79 70 50 61 72 73 65 72 2d  hs;.  yypParser-
3cfcc 3e 79 79 69 64 78 20 2d 3d 20 79 79 73 69 7a 65  >yyidx -= yysize
3cfcd 3b 0a 20 20 79 79 61 63 74 20 3d 20 79 79 5f 66  ;.  yyact = yy_f
3cfce 69 6e 64 5f 72 65 64 75 63 65 5f 61 63 74 69 6f  ind_reduce_actio
3cfcf 6e 28 79 79 6d 73 70 5b 2d 79 79 73 69 7a 65 5d  n(yymsp[-yysize]
3cfd0 2e 73 74 61 74 65 6e 6f 2c 28 59 59 43 4f 44 45  .stateno,(YYCODE
3cfd1 54 59 50 45 29 79 79 67 6f 74 6f 29 3b 0a 20 20  TYPE)yygoto);.  
3cfd2 69 66 28 20 79 79 61 63 74 20 3c 20 59 59 4e 53  if( yyact < YYNS
3cfd3 54 41 54 45 20 29 7b 0a 23 69 66 64 65 66 20 4e  TATE ){.#ifdef N
3cfd4 44 45 42 55 47 0a 20 20 20 20 2f 2a 20 49 66 20  DEBUG.    /* If 
3cfd5 77 65 20 61 72 65 20 6e 6f 74 20 64 65 62 75 67  we are not debug
3cfd6 67 69 6e 67 20 61 6e 64 20 74 68 65 20 72 65 64  ging and the red
3cfd7 75 63 65 20 61 63 74 69 6f 6e 20 70 6f 70 70 65  uce action poppe
3cfd8 64 20 61 74 20 6c 65 61 73 74 0a 20 20 20 20 2a  d at least.    *
3cfd9 2a 20 6f 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66  * one element of
3cfda 66 20 74 68 65 20 73 74 61 63 6b 2c 20 74 68 65  f the stack, the
3cfdb 6e 20 77 65 20 63 61 6e 20 70 75 73 68 20 74 68  n we can push th
3cfdc 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 62 61  e new element ba
3cfdd 63 6b 0a 20 20 20 20 2a 2a 20 6f 6e 74 6f 20 74  ck.    ** onto t
3cfde 68 65 20 73 74 61 63 6b 20 68 65 72 65 2c 20 61  he stack here, a
3cfdf 6e 64 20 73 6b 69 70 20 74 68 65 20 73 74 61 63  nd skip the stac
3cfe0 6b 20 6f 76 65 72 66 6c 6f 77 20 74 65 73 74 20  k overflow test 
3cfe1 69 6e 20 79 79 5f 73 68 69 66 74 28 29 2e 0a 20  in yy_shift().. 
3cfe2 20 20 20 2a 2a 20 54 68 61 74 20 67 69 76 65 73     ** That gives
3cfe3 20 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20 73   a significant s
3cfe4 70 65 65 64 20 69 6d 70 72 6f 76 65 6d 65 6e 74  peed improvement
3cfe5 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 79 79 73  . */.    if( yys
3cfe6 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 79 79 70  ize ){.      yyp
3cfe7 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2b 2b 3b  Parser->yyidx++;
3cfe8 0a 20 20 20 20 20 20 79 79 6d 73 70 20 2d 3d 20  .      yymsp -= 
3cfe9 79 79 73 69 7a 65 2d 31 3b 0a 20 20 20 20 20 20  yysize-1;.      
3cfea 79 79 6d 73 70 2d 3e 73 74 61 74 65 6e 6f 20 3d  yymsp->stateno =
3cfeb 20 28 59 59 41 43 54 49 4f 4e 54 59 50 45 29 79   (YYACTIONTYPE)y
3cfec 79 61 63 74 3b 0a 20 20 20 20 20 20 79 79 6d 73  yact;.      yyms
3cfed 70 2d 3e 6d 61 6a 6f 72 20 3d 20 28 59 59 43 4f  p->major = (YYCO
3cfee 44 45 54 59 50 45 29 79 79 67 6f 74 6f 3b 0a 20  DETYPE)yygoto;. 
3cfef 20 20 20 20 20 79 79 6d 73 70 2d 3e 6d 69 6e 6f       yymsp->mino
3cff0 72 20 3d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 3b  r = yygotominor;
3cff1 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  .    }else.#endi
3cff2 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 79 79  f.    {.      yy
3cff3 5f 73 68 69 66 74 28 79 79 70 50 61 72 73 65 72  _shift(yypParser
3cff4 2c 79 79 61 63 74 2c 79 79 67 6f 74 6f 2c 26 79  ,yyact,yygoto,&y
3cff5 79 67 6f 74 6f 6d 69 6e 6f 72 29 3b 0a 20 20 20  ygotominor);.   
3cff6 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
3cff7 61 73 73 65 72 74 28 20 79 79 61 63 74 20 3d 3d  assert( yyact ==
3cff8 20 59 59 4e 53 54 41 54 45 20 2b 20 59 59 4e 52   YYNSTATE + YYNR
3cff9 55 4c 45 20 2b 20 31 20 29 3b 0a 20 20 20 20 79  ULE + 1 );.    y
3cffa 79 5f 61 63 63 65 70 74 28 79 79 70 50 61 72 73  y_accept(yypPars
3cffb 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  er);.  }.}../*.*
3cffc 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
3cffd 63 6f 64 65 20 65 78 65 63 75 74 65 73 20 77 68  code executes wh
3cffe 65 6e 20 74 68 65 20 70 61 72 73 65 20 66 61 69  en the parse fai
3cfff 6c 73 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 59 59  ls.*/.#ifndef YY
3d000 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45 52 59 0a  NOERRORRECOVERY.
3d001 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 70  static void yy_p
3d002 61 72 73 65 5f 66 61 69 6c 65 64 28 0a 20 20 79  arse_failed(.  y
3d003 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
3d004 65 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er           /* 
3d005 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b  The parser */.){
3d006 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
3d007 41 52 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64  ARG_FETCH;.#ifnd
3d008 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20  ef NDEBUG.  if( 
3d009 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20  yyTraceFILE ){. 
3d00a 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
3d00b 63 65 46 49 4c 45 2c 22 25 73 46 61 69 6c 21 5c  ceFILE,"%sFail!\
3d00c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74  n",yyTracePrompt
3d00d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
3d00e 77 68 69 6c 65 28 20 79 79 70 50 61 72 73 65 72  while( yypParser
3d00f 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f  ->yyidx>=0 ) yy_
3d010 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b  pop_parser_stack
3d011 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 2f  (yypParser);.  /
3d012 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73 20 69  * Here code is i
3d013 6e 73 65 72 74 65 64 20 77 68 69 63 68 20 77 69  nserted which wi
3d014 6c 6c 20 62 65 20 65 78 65 63 75 74 65 64 20 77  ll be executed w
3d015 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a  henever the.  **
3d016 20 70 61 72 73 65 72 20 66 61 69 6c 73 20 2a 2f   parser fails */
3d017 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
3d018 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75  ARG_STORE; /* Su
3d019 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61  ppress warning a
3d01a 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74  bout unused %ext
3d01b 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69  ra_argument vari
3d01c 61 62 6c 65 20 2a 2f 0a 7d 0a 23 65 6e 64 69 66  able */.}.#endif
3d01d 20 2f 2a 20 59 59 4e 4f 45 52 52 4f 52 52 45 43   /* YYNOERRORREC
3d01e 4f 56 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  OVERY */../*.** 
3d01f 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
3d020 64 65 20 65 78 65 63 75 74 65 73 20 77 68 65 6e  de executes when
3d021 20 61 20 73 79 6e 74 61 78 20 65 72 72 6f 72 20   a syntax error 
3d022 66 69 72 73 74 20 6f 63 63 75 72 73 2e 0a 2a 2f  first occurs..*/
3d023 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f  .static void yy_
3d024 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 0a 20 20  syntax_error(.  
3d025 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72  yyParser *yypPar
3d026 73 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ser,           /
3d027 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a  * The parser */.
3d028 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20 20    int yymajor,  
3d029 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d02a 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 79   /* The major ty
3d02b 70 65 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  pe of the error 
3d02c 74 6f 6b 65 6e 20 2a 2f 0a 20 20 59 59 4d 49 4e  token */.  YYMIN
3d02d 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f 72 20 20  ORTYPE yyminor  
3d02e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3d02f 20 6d 69 6e 6f 72 20 74 79 70 65 20 6f 66 20 74   minor type of t
3d030 68 65 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20 2a  he error token *
3d031 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61  /.){.  sqlite3Pa
3d032 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 23  rserARG_FETCH;.#
3d033 64 65 66 69 6e 65 20 54 4f 4b 45 4e 20 28 79 79  define TOKEN (yy
3d034 6d 69 6e 6f 72 2e 79 79 30 29 0a 0a 20 20 55 4e  minor.yy0)..  UN
3d035 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 79  USED_PARAMETER(y
3d036 79 6d 61 6a 6f 72 29 3b 20 20 2f 2a 20 53 69 6c  ymajor);  /* Sil
3d037 65 6e 63 65 20 73 6f 6d 65 20 63 6f 6d 70 69 6c  ence some compil
3d038 65 72 20 77 61 72 6e 69 6e 67 73 20 2a 2f 0a 20  er warnings */. 
3d039 20 61 73 73 65 72 74 28 20 54 4f 4b 45 4e 2e 7a   assert( TOKEN.z
3d03a 5b 30 5d 20 29 3b 20 20 2f 2a 20 54 68 65 20 74  [0] );  /* The t
3d03b 6f 6b 65 6e 69 7a 65 72 20 61 6c 77 61 79 73 20  okenizer always 
3d03c 67 69 76 65 73 20 75 73 20 61 20 74 6f 6b 65 6e  gives us a token
3d03d 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 72 72   */.  sqlite3Err
3d03e 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
3d03f 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74  ear \"%T\": synt
3d040 61 78 20 65 72 72 6f 72 22 2c 20 26 54 4f 4b 45  ax error", &TOKE
3d041 4e 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 72  N);.  sqlite3Par
3d042 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a  serARG_STORE; /*
3d043 20 53 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e   Suppress warnin
3d044 67 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25  g about unused %
3d045 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76  extra_argument v
3d046 61 72 69 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a  ariable */.}../*
3d047 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
3d048 67 20 69 73 20 65 78 65 63 75 74 65 64 20 77 68  g is executed wh
3d049 65 6e 20 74 68 65 20 70 61 72 73 65 72 20 61 63  en the parser ac
3d04a 63 65 70 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20  cepts.*/.static 
3d04b 76 6f 69 64 20 79 79 5f 61 63 63 65 70 74 28 0a  void yy_accept(.
3d04c 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50    yyParser *yypP
3d04d 61 72 73 65 72 20 20 20 20 20 20 20 20 20 20 20  arser           
3d04e 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
3d04f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 72  .){.  sqlite3Par
3d050 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 23 69  serARG_FETCH;.#i
3d051 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69  fndef NDEBUG.  i
3d052 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29  f( yyTraceFILE )
3d053 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79  {.    fprintf(yy
3d054 54 72 61 63 65 46 49 4c 45 2c 22 25 73 41 63 63  TraceFILE,"%sAcc
3d055 65 70 74 21 5c 6e 22 2c 79 79 54 72 61 63 65 50  ept!\n",yyTraceP
3d056 72 6f 6d 70 74 29 3b 0a 20 20 7d 0a 23 65 6e 64  rompt);.  }.#end
3d057 69 66 0a 20 20 77 68 69 6c 65 28 20 79 79 70 50  if.  while( yypP
3d058 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20  arser->yyidx>=0 
3d059 29 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f  ) yy_pop_parser_
3d05a 73 74 61 63 6b 28 79 79 70 50 61 72 73 65 72 29  stack(yypParser)
3d05b 3b 0a 20 20 2f 2a 20 48 65 72 65 20 63 6f 64 65  ;.  /* Here code
3d05c 20 69 73 20 69 6e 73 65 72 74 65 64 20 77 68 69   is inserted whi
3d05d 63 68 20 77 69 6c 6c 20 62 65 20 65 78 65 63 75  ch will be execu
3d05e 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 65  ted whenever the
3d05f 0a 20 20 2a 2a 20 70 61 72 73 65 72 20 61 63 63  .  ** parser acc
3d060 65 70 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  epts */.  sqlite
3d061 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45  3ParserARG_STORE
3d062 3b 20 2f 2a 20 53 75 70 70 72 65 73 73 20 77 61  ; /* Suppress wa
3d063 72 6e 69 6e 67 20 61 62 6f 75 74 20 75 6e 75 73  rning about unus
3d064 65 64 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65  ed %extra_argume
3d065 6e 74 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 7d  nt variable */.}
3d066 0a 0a 2f 2a 20 54 68 65 20 6d 61 69 6e 20 70 61  ../* The main pa
3d067 72 73 65 72 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a  rser program..**
3d068 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
3d069 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ent is a pointer
3d06a 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 65 20   to a structure 
3d06b 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a  obtained from.**
3d06c 20 22 73 71 6c 69 74 65 33 50 61 72 73 65 72 41   "sqlite3ParserA
3d06d 6c 6c 6f 63 22 20 77 68 69 63 68 20 64 65 73 63  lloc" which desc
3d06e 72 69 62 65 73 20 74 68 65 20 63 75 72 72 65 6e  ribes the curren
3d06f 74 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 70  t state of the p
3d070 61 72 73 65 72 2e 0a 2a 2a 20 54 68 65 20 73 65  arser..** The se
3d071 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
3d072 20 74 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e   the major token
3d073 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 74 68   number.  The th
3d074 69 72 64 20 69 73 0a 2a 2a 20 74 68 65 20 6d 69  ird is.** the mi
3d075 6e 6f 72 20 74 6f 6b 65 6e 2e 20 20 54 68 65 20  nor token.  The 
3d076 66 6f 75 72 74 68 20 6f 70 74 69 6f 6e 61 6c 20  fourth optional 
3d077 61 72 67 75 6d 65 6e 74 20 69 73 20 77 68 61 74  argument is what
3d078 65 76 65 72 20 74 68 65 0a 2a 2a 20 75 73 65 72  ever the.** user
3d079 20 77 61 6e 74 73 20 28 61 6e 64 20 73 70 65 63   wants (and spec
3d07a 69 66 69 65 64 20 69 6e 20 74 68 65 20 67 72 61  ified in the gra
3d07b 6d 6d 61 72 29 20 61 6e 64 20 69 73 20 61 76 61  mmar) and is ava
3d07c 69 6c 61 62 6c 65 20 66 6f 72 0a 2a 2a 20 75 73  ilable for.** us
3d07d 65 20 62 79 20 74 68 65 20 61 63 74 69 6f 6e 20  e by the action 
3d07e 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20  routines..**.** 
3d07f 49 6e 70 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a  Inputs:.** <ul>.
3d080 2a 2a 20 3c 6c 69 3e 20 41 20 70 6f 69 6e 74 65  ** <li> A pointe
3d081 72 20 74 6f 20 74 68 65 20 70 61 72 73 65 72 20  r to the parser 
3d082 28 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63  (an opaque struc
3d083 74 75 72 65 2e 29 0a 2a 2a 20 3c 6c 69 3e 20 54  ture.).** <li> T
3d084 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e  he major token n
3d085 75 6d 62 65 72 2e 0a 2a 2a 20 3c 6c 69 3e 20 54  umber..** <li> T
3d086 68 65 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20 6e  he minor token n
3d087 75 6d 62 65 72 2e 0a 2a 2a 20 3c 6c 69 3e 20 41  umber..** <li> A
3d088 6e 20 6f 70 74 69 6f 6e 20 61 72 67 75 6d 65 6e  n option argumen
3d089 74 20 6f 66 20 61 20 67 72 61 6d 6d 61 72 2d 73  t of a grammar-s
3d08a 70 65 63 69 66 69 65 64 20 74 79 70 65 2e 0a 2a  pecified type..*
3d08b 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75  * </ul>.**.** Ou
3d08c 74 70 75 74 73 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a  tputs:.** None..
3d08d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
3d08e 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
3d08f 72 73 65 72 28 0a 20 20 76 6f 69 64 20 2a 79 79  rser(.  void *yy
3d090 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
3d091 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
3d092 65 72 20 2a 2f 0a 20 20 69 6e 74 20 79 79 6d 61  er */.  int yyma
3d093 6a 6f 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  jor,            
3d094 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f       /* The majo
3d095 72 20 74 6f 6b 65 6e 20 63 6f 64 65 20 6e 75 6d  r token code num
3d096 62 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ber */.  sqlite3
3d097 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20  ParserTOKENTYPE 
3d098 79 79 6d 69 6e 6f 72 20 20 20 20 20 20 20 2f 2a  yyminor       /*
3d099 20 54 68 65 20 76 61 6c 75 65 20 66 6f 72 20 74   The value for t
3d09a 68 65 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 73 71  he token */.  sq
3d09b 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 50  lite3ParserARG_P
3d09c 44 45 43 4c 20 20 20 20 20 20 20 20 20 20 20 20  DECL            
3d09d 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 25     /* Optional %
3d09e 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 70  extra_argument p
3d09f 61 72 61 6d 65 74 65 72 20 2a 2f 0a 29 7b 0a 20  arameter */.){. 
3d0a0 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 79 79 6d   YYMINORTYPE yym
3d0a1 69 6e 6f 72 75 6e 69 6f 6e 3b 0a 20 20 69 6e 74  inorunion;.  int
3d0a2 20 79 79 61 63 74 3b 20 20 20 20 20 20 20 20 20   yyact;         
3d0a3 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
3d0a4 20 61 63 74 69 6f 6e 2e 20 2a 2f 0a 23 69 66 20   action. */.#if 
3d0a5 21 64 65 66 69 6e 65 64 28 59 59 45 52 52 4f 52  !defined(YYERROR
3d0a6 53 59 4d 42 4f 4c 29 20 26 26 20 21 64 65 66 69  SYMBOL) && !defi
3d0a7 6e 65 64 28 59 59 4e 4f 45 52 52 4f 52 52 45 43  ned(YYNOERRORREC
3d0a8 4f 56 45 52 59 29 0a 20 20 69 6e 74 20 79 79 65  OVERY).  int yye
3d0a9 6e 64 6f 66 69 6e 70 75 74 3b 20 20 20 20 20 2f  ndofinput;     /
3d0aa 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65  * True if we are
3d0ab 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 69   at the end of i
3d0ac 6e 70 75 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 23  nput */.#endif.#
3d0ad 69 66 64 65 66 20 59 59 45 52 52 4f 52 53 59 4d  ifdef YYERRORSYM
3d0ae 42 4f 4c 0a 20 20 69 6e 74 20 79 79 65 72 72 6f  BOL.  int yyerro
3d0af 72 68 69 74 20 3d 20 30 3b 20 20 20 2f 2a 20 54  rhit = 0;   /* T
3d0b0 72 75 65 20 69 66 20 79 79 6d 61 6a 6f 72 20 68  rue if yymajor h
3d0b1 61 73 20 69 6e 76 6f 6b 65 64 20 61 6e 20 65 72  as invoked an er
3d0b2 72 6f 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  ror */.#endif.  
3d0b3 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72  yyParser *yypPar
3d0b4 73 65 72 3b 20 20 2f 2a 20 54 68 65 20 70 61 72  ser;  /* The par
3d0b5 73 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 28 72 65  ser */..  /* (re
3d0b6 29 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20  )initialize the 
3d0b7 70 61 72 73 65 72 2c 20 69 66 20 6e 65 63 65 73  parser, if neces
3d0b8 73 61 72 79 20 2a 2f 0a 20 20 79 79 70 50 61 72  sary */.  yypPar
3d0b9 73 65 72 20 3d 20 28 79 79 50 61 72 73 65 72 2a  ser = (yyParser*
3d0ba 29 79 79 70 3b 0a 20 20 69 66 28 20 79 79 70 50  )yyp;.  if( yypP
3d0bb 61 72 73 65 72 2d 3e 79 79 69 64 78 3c 30 20 29  arser->yyidx<0 )
3d0bc 7b 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50  {.#if YYSTACKDEP
3d0bd 54 48 3c 3d 30 0a 20 20 20 20 69 66 28 20 79 79  TH<=0.    if( yy
3d0be 70 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a  pParser->yystksz
3d0bf 20 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a   <=0 ){.      /*
3d0c0 6d 65 6d 73 65 74 28 26 79 79 6d 69 6e 6f 72 75  memset(&yyminoru
3d0c1 6e 69 6f 6e 2c 20 30 2c 20 73 69 7a 65 6f 66 28  nion, 0, sizeof(
3d0c2 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 29 3b 2a  yyminorunion));*
3d0c3 2f 0a 20 20 20 20 20 20 79 79 6d 69 6e 6f 72 75  /.      yyminoru
3d0c4 6e 69 6f 6e 20 3d 20 79 79 7a 65 72 6f 6d 69 6e  nion = yyzeromin
3d0c5 6f 72 3b 0a 20 20 20 20 20 20 79 79 53 74 61 63  or;.      yyStac
3d0c6 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70 50 61 72  kOverflow(yypPar
3d0c7 73 65 72 2c 20 26 79 79 6d 69 6e 6f 72 75 6e 69  ser, &yyminoruni
3d0c8 6f 6e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  on);.      retur
3d0c9 6e 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  n;.    }.#endif.
3d0ca 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
3d0cb 79 69 64 78 20 3d 20 30 3b 0a 20 20 20 20 79 79  yidx = 0;.    yy
3d0cc 70 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e  pParser->yyerrcn
3d0cd 74 20 3d 20 2d 31 3b 0a 20 20 20 20 79 79 70 50  t = -1;.    yypP
3d0ce 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 30  arser->yystack[0
3d0cf 5d 2e 73 74 61 74 65 6e 6f 20 3d 20 30 3b 0a 20  ].stateno = 0;. 
3d0d0 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79     yypParser->yy
3d0d1 73 74 61 63 6b 5b 30 5d 2e 6d 61 6a 6f 72 20 3d  stack[0].major =
3d0d2 20 30 3b 0a 20 20 7d 0a 20 20 79 79 6d 69 6e 6f   0;.  }.  yymino
3d0d3 72 75 6e 69 6f 6e 2e 79 79 30 20 3d 20 79 79 6d  runion.yy0 = yym
3d0d4 69 6e 6f 72 3b 0a 23 69 66 20 21 64 65 66 69 6e  inor;.#if !defin
3d0d5 65 64 28 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c  ed(YYERRORSYMBOL
3d0d6 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 59 59  ) && !defined(YY
3d0d7 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45 52 59 29  NOERRORRECOVERY)
3d0d8 0a 20 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20  .  yyendofinput 
3d0d9 3d 20 28 79 79 6d 61 6a 6f 72 3d 3d 30 29 3b 0a  = (yymajor==0);.
3d0da 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
3d0db 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45 3b  ParserARG_STORE;
3d0dc 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
3d0dd 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49  .  if( yyTraceFI
3d0de 4c 45 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74  LE ){.    fprint
3d0df 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25  f(yyTraceFILE,"%
3d0e0 73 49 6e 70 75 74 20 25 73 5c 6e 22 2c 79 79 54  sInput %s\n",yyT
3d0e1 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 54 6f 6b  racePrompt,yyTok
3d0e2 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f 72 5d 29  enName[yymajor])
3d0e3 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
3d0e4 64 6f 7b 0a 20 20 20 20 79 79 61 63 74 20 3d 20  do{.    yyact = 
3d0e5 79 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f 61 63  yy_find_shift_ac
3d0e6 74 69 6f 6e 28 79 79 70 50 61 72 73 65 72 2c 28  tion(yypParser,(
3d0e7 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a  YYCODETYPE)yymaj
3d0e8 6f 72 29 3b 0a 20 20 20 20 69 66 28 20 79 79 61  or);.    if( yya
3d0e9 63 74 3c 59 59 4e 53 54 41 54 45 20 29 7b 0a 20  ct<YYNSTATE ){. 
3d0ea 20 20 20 20 20 79 79 5f 73 68 69 66 74 28 79 79       yy_shift(yy
3d0eb 70 50 61 72 73 65 72 2c 79 79 61 63 74 2c 79 79  pParser,yyact,yy
3d0ec 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e  major,&yyminorun
3d0ed 69 6f 6e 29 3b 0a 20 20 20 20 20 20 79 79 70 50  ion);.      yypP
3d0ee 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 2d  arser->yyerrcnt-
3d0ef 2d 3b 0a 20 20 20 20 20 20 79 79 6d 61 6a 6f 72  -;.      yymajor
3d0f0 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20   = YYNOCODE;.   
3d0f1 20 7d 65 6c 73 65 20 69 66 28 20 79 79 61 63 74   }else if( yyact
3d0f2 20 3c 20 59 59 4e 53 54 41 54 45 20 2b 20 59 59   < YYNSTATE + YY
3d0f3 4e 52 55 4c 45 20 29 7b 0a 20 20 20 20 20 20 79  NRULE ){.      y
3d0f4 79 5f 72 65 64 75 63 65 28 79 79 70 50 61 72 73  y_reduce(yypPars
3d0f5 65 72 2c 79 79 61 63 74 2d 59 59 4e 53 54 41 54  er,yyact-YYNSTAT
3d0f6 45 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  E);.    }else{. 
3d0f7 20 20 20 20 20 61 73 73 65 72 74 28 20 79 79 61       assert( yya
3d0f8 63 74 20 3d 3d 20 59 59 5f 45 52 52 4f 52 5f 41  ct == YY_ERROR_A
3d0f9 43 54 49 4f 4e 20 29 3b 0a 23 69 66 64 65 66 20  CTION );.#ifdef 
3d0fa 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20  YYERRORSYMBOL.  
3d0fb 20 20 20 20 69 6e 74 20 79 79 6d 78 3b 0a 23 65      int yymx;.#e
3d0fc 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45  ndif.#ifndef NDE
3d0fd 42 55 47 0a 20 20 20 20 20 20 69 66 28 20 79 79  BUG.      if( yy
3d0fe 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20  TraceFILE ){.   
3d0ff 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54       fprintf(yyT
3d100 72 61 63 65 46 49 4c 45 2c 22 25 73 53 79 6e 74  raceFILE,"%sSynt
3d101 61 78 20 45 72 72 6f 72 21 5c 6e 22 2c 79 79 54  ax Error!\n",yyT
3d102 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20  racePrompt);.   
3d103 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64     }.#endif.#ifd
3d104 65 66 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c  ef YYERRORSYMBOL
3d105 0a 20 20 20 20 20 20 2f 2a 20 41 20 73 79 6e 74  .      /* A synt
3d106 61 78 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  ax error has occ
3d107 75 72 72 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20  urred..      ** 
3d108 54 68 65 20 72 65 73 70 6f 6e 73 65 20 74 6f 20  The response to 
3d109 61 6e 20 65 72 72 6f 72 20 64 65 70 65 6e 64 73  an error depends
3d10a 20 75 70 6f 6e 20 77 68 65 74 68 65 72 20 6f 72   upon whether or
3d10b 20 6e 6f 74 20 74 68 65 0a 20 20 20 20 20 20 2a   not the.      *
3d10c 2a 20 67 72 61 6d 6d 61 72 20 64 65 66 69 6e 65  * grammar define
3d10d 73 20 61 6e 20 65 72 72 6f 72 20 74 6f 6b 65 6e  s an error token
3d10e 20 22 45 52 52 4f 52 22 2e 20 20 0a 20 20 20 20   "ERROR".  .    
3d10f 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68    **.      ** Th
3d110 69 73 20 69 73 20 77 68 61 74 20 77 65 20 64 6f  is is what we do
3d111 20 69 66 20 74 68 65 20 67 72 61 6d 6d 61 72 20   if the grammar 
3d112 64 6f 65 73 20 64 65 66 69 6e 65 20 45 52 52 4f  does define ERRO
3d113 52 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  R:.      **.    
3d114 20 20 2a 2a 20 20 2a 20 43 61 6c 6c 20 74 68 65    **  * Call the
3d115 20 25 73 79 6e 74 61 78 5f 65 72 72 6f 72 20 66   %syntax_error f
3d116 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a  unction..      *
3d117 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 42 65  *.      **  * Be
3d118 67 69 6e 20 70 6f 70 70 69 6e 67 20 74 68 65 20  gin popping the 
3d119 73 74 61 63 6b 20 75 6e 74 69 6c 20 77 65 20 65  stack until we e
3d11a 6e 74 65 72 20 61 20 73 74 61 74 65 20 77 68 65  nter a state whe
3d11b 72 65 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 69  re.      **    i
3d11c 74 20 69 73 20 6c 65 67 61 6c 20 74 6f 20 73 68  t is legal to sh
3d11d 69 66 74 20 74 68 65 20 65 72 72 6f 72 20 73 79  ift the error sy
3d11e 6d 62 6f 6c 2c 20 74 68 65 6e 20 73 68 69 66 74  mbol, then shift
3d11f 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 74 68 65  .      **    the
3d120 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2e 0a 20   error symbol.. 
3d121 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
3d122 20 20 2a 20 53 65 74 20 74 68 65 20 65 72 72 6f    * Set the erro
3d123 72 20 63 6f 75 6e 74 20 74 6f 20 74 68 72 65 65  r count to three
3d124 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
3d125 20 2a 2a 20 20 2a 20 42 65 67 69 6e 20 61 63 63   **  * Begin acc
3d126 65 70 74 69 6e 67 20 61 6e 64 20 73 68 69 66 74  epting and shift
3d127 69 6e 67 20 6e 65 77 20 74 6f 6b 65 6e 73 2e 20  ing new tokens. 
3d128 20 4e 6f 20 6e 65 77 20 65 72 72 6f 72 0a 20 20   No new error.  
3d129 20 20 20 20 2a 2a 20 20 20 20 70 72 6f 63 65 73      **    proces
3d12a 73 69 6e 67 20 77 69 6c 6c 20 6f 63 63 75 72 20  sing will occur 
3d12b 75 6e 74 69 6c 20 74 68 72 65 65 20 74 6f 6b 65  until three toke
3d12c 6e 73 20 68 61 76 65 20 62 65 65 6e 0a 20 20 20  ns have been.   
3d12d 20 20 20 2a 2a 20 20 20 20 73 68 69 66 74 65 64     **    shifted
3d12e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a 20   successfully.. 
3d12f 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2f       **.      */
3d130 0a 20 20 20 20 20 20 69 66 28 20 79 79 70 50 61  .      if( yypPa
3d131 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 3c 30  rser->yyerrcnt<0
3d132 20 29 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 73   ){.        yy_s
3d133 79 6e 74 61 78 5f 65 72 72 6f 72 28 79 79 70 50  yntax_error(yypP
3d134 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79  arser,yymajor,yy
3d135 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20  minorunion);.   
3d136 20 20 20 7d 0a 20 20 20 20 20 20 79 79 6d 78 20     }.      yymx 
3d137 3d 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73  = yypParser->yys
3d138 74 61 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e  tack[yypParser->
3d139 79 79 69 64 78 5d 2e 6d 61 6a 6f 72 3b 0a 20 20  yyidx].major;.  
3d13a 20 20 20 20 69 66 28 20 79 79 6d 78 3d 3d 59 59      if( yymx==YY
3d13b 45 52 52 4f 52 53 59 4d 42 4f 4c 20 7c 7c 20 79  ERRORSYMBOL || y
3d13c 79 65 72 72 6f 72 68 69 74 20 29 7b 0a 23 69 66  yerrorhit ){.#if
3d13d 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  ndef NDEBUG.    
3d13e 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46      if( yyTraceF
3d13f 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ILE ){.         
3d140 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
3d141 46 49 4c 45 2c 22 25 73 44 69 73 63 61 72 64 20  FILE,"%sDiscard 
3d142 69 6e 70 75 74 20 74 6f 6b 65 6e 20 25 73 5c 6e  input token %s\n
3d143 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
3d144 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79  yyTracePrompt,yy
3d145 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f  TokenName[yymajo
3d146 72 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23  r]);.        }.#
3d147 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 79 79  endif.        yy
3d148 5f 64 65 73 74 72 75 63 74 6f 72 28 79 79 70 50  _destructor(yypP
3d149 61 72 73 65 72 2c 20 28 59 59 43 4f 44 45 54 59  arser, (YYCODETY
3d14a 50 45 29 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69  PE)yymajor,&yymi
3d14b 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20  norunion);.     
3d14c 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e     yymajor = YYN
3d14d 4f 43 4f 44 45 3b 0a 20 20 20 20 20 20 7d 65 6c  OCODE;.      }el
3d14e 73 65 7b 0a 20 20 20 20 20 20 20 20 20 77 68 69  se{.         whi
3d14f 6c 65 28 0a 20 20 20 20 20 20 20 20 20 20 79 79  le(.          yy
3d150 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3e  pParser->yyidx >
3d151 3d 20 30 20 26 26 0a 20 20 20 20 20 20 20 20 20  = 0 &&.         
3d152 20 79 79 6d 78 20 21 3d 20 59 59 45 52 52 4f 52   yymx != YYERROR
3d153 53 59 4d 42 4f 4c 20 26 26 0a 20 20 20 20 20 20  SYMBOL &&.      
3d154 20 20 20 20 28 79 79 61 63 74 20 3d 20 79 79 5f      (yyact = yy_
3d155 66 69 6e 64 5f 72 65 64 75 63 65 5f 61 63 74 69  find_reduce_acti
3d156 6f 6e 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  on(.            
3d157 20 20 20 20 20 20 20 20 20 20 20 20 79 79 70 50              yypP
3d158 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79  arser->yystack[y
3d159 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d  ypParser->yyidx]
3d15a 2e 73 74 61 74 65 6e 6f 2c 0a 20 20 20 20 20 20  .stateno,.      
3d15b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d15c 20 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 29    YYERRORSYMBOL)
3d15d 29 20 3e 3d 20 59 59 4e 53 54 41 54 45 0a 20 20  ) >= YYNSTATE.  
3d15e 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
3d15f 20 20 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72     yy_pop_parser
3d160 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73 65 72  _stack(yypParser
3d161 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
3d162 20 20 20 20 20 69 66 28 20 79 79 70 50 61 72 73       if( yypPars
3d163 65 72 2d 3e 79 79 69 64 78 20 3c 20 30 20 7c 7c  er->yyidx < 0 ||
3d164 20 79 79 6d 61 6a 6f 72 3d 3d 30 20 29 7b 0a 20   yymajor==0 ){. 
3d165 20 20 20 20 20 20 20 20 20 79 79 5f 64 65 73 74           yy_dest
3d166 72 75 63 74 6f 72 28 79 79 70 50 61 72 73 65 72  ructor(yypParser
3d167 2c 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d  ,(YYCODETYPE)yym
3d168 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69  ajor,&yyminoruni
3d169 6f 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 79  on);.          y
3d16a 79 5f 70 61 72 73 65 5f 66 61 69 6c 65 64 28 79  y_parse_failed(y
3d16b 79 70 50 61 72 73 65 72 29 3b 0a 20 20 20 20 20  ypParser);.     
3d16c 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59       yymajor = Y
3d16d 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 20 20 20  YNOCODE;.       
3d16e 20 7d 65 6c 73 65 20 69 66 28 20 79 79 6d 78 21   }else if( yymx!
3d16f 3d 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 29  =YYERRORSYMBOL )
3d170 7b 0a 20 20 20 20 20 20 20 20 20 20 59 59 4d 49  {.          YYMI
3d171 4e 4f 52 54 59 50 45 20 75 32 3b 0a 20 20 20 20  NORTYPE u2;.    
3d172 20 20 20 20 20 20 75 32 2e 59 59 45 52 52 53 59        u2.YYERRSY
3d173 4d 44 54 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  MDT = 0;.       
3d174 20 20 20 79 79 5f 73 68 69 66 74 28 79 79 70 50     yy_shift(yypP
3d175 61 72 73 65 72 2c 79 79 61 63 74 2c 59 59 45 52  arser,yyact,YYER
3d176 52 4f 52 53 59 4d 42 4f 4c 2c 26 75 32 29 3b 0a  RORSYMBOL,&u2);.
3d177 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3d178 7d 0a 20 20 20 20 20 20 79 79 70 50 61 72 73 65  }.      yypParse
3d179 72 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 33 3b  r->yyerrcnt = 3;
3d17a 0a 20 20 20 20 20 20 79 79 65 72 72 6f 72 68 69  .      yyerrorhi
3d17b 74 20 3d 20 31 3b 0a 23 65 6c 69 66 20 64 65 66  t = 1;.#elif def
3d17c 69 6e 65 64 28 59 59 4e 4f 45 52 52 4f 52 52 45  ined(YYNOERRORRE
3d17d 43 4f 56 45 52 59 29 0a 20 20 20 20 20 20 2f 2a  COVERY).      /*
3d17e 20 49 66 20 74 68 65 20 59 59 4e 4f 45 52 52 4f   If the YYNOERRO
3d17f 52 52 45 43 4f 56 45 52 59 20 6d 61 63 72 6f 20  RRECOVERY macro 
3d180 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e  is defined, then
3d181 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20   do not attempt 
3d182 74 6f 0a 20 20 20 20 20 20 2a 2a 20 64 6f 20 61  to.      ** do a
3d183 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72  ny kind of error
3d184 20 72 65 63 6f 76 65 72 79 2e 20 20 49 6e 73 74   recovery.  Inst
3d185 65 61 64 2c 20 73 69 6d 70 6c 79 20 69 6e 76 6f  ead, simply invo
3d186 6b 65 20 74 68 65 20 73 79 6e 74 61 78 0a 20 20  ke the syntax.  
3d187 20 20 20 20 2a 2a 20 65 72 72 6f 72 20 72 6f 75      ** error rou
3d188 74 69 6e 65 20 61 6e 64 20 63 6f 6e 74 69 6e 75  tine and continu
3d189 65 20 67 6f 69 6e 67 20 61 73 20 69 66 20 6e 6f  e going as if no
3d18a 74 68 69 6e 67 20 68 61 64 20 68 61 70 70 65 6e  thing had happen
3d18b 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ed..      **.   
3d18c 20 20 20 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f     ** Applicatio
3d18d 6e 73 20 63 61 6e 20 73 65 74 20 74 68 69 73 20  ns can set this 
3d18e 6d 61 63 72 6f 20 28 66 6f 72 20 65 78 61 6d 70  macro (for examp
3d18f 6c 65 20 69 6e 73 69 64 65 20 25 69 6e 63 6c 75  le inside %inclu
3d190 64 65 29 20 69 66 0a 20 20 20 20 20 20 2a 2a 20  de) if.      ** 
3d191 74 68 65 79 20 69 6e 74 65 6e 64 20 74 6f 20 61  they intend to a
3d192 62 61 6e 64 6f 6e 20 74 68 65 20 70 61 72 73 65  bandon the parse
3d193 20 75 70 6f 6e 20 74 68 65 20 66 69 72 73 74 20   upon the first 
3d194 73 79 6e 74 61 78 20 65 72 72 6f 72 20 73 65 65  syntax error see
3d195 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  n..      */.    
3d196 20 20 79 79 5f 73 79 6e 74 61 78 5f 65 72 72 6f    yy_syntax_erro
3d197 72 28 79 79 70 50 61 72 73 65 72 2c 79 79 6d 61  r(yypParser,yyma
3d198 6a 6f 72 2c 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  jor,yyminorunion
3d199 29 3b 0a 20 20 20 20 20 20 79 79 5f 64 65 73 74  );.      yy_dest
3d19a 72 75 63 74 6f 72 28 79 79 70 50 61 72 73 65 72  ructor(yypParser
3d19b 2c 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d  ,(YYCODETYPE)yym
3d19c 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69  ajor,&yyminoruni
3d19d 6f 6e 29 3b 0a 20 20 20 20 20 20 79 79 6d 61 6a  on);.      yymaj
3d19e 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20  or = YYNOCODE;. 
3d19f 20 20 20 20 20 0a 23 65 6c 73 65 20 20 2f 2a 20       .#else  /* 
3d1a0 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 69 73  YYERRORSYMBOL is
3d1a1 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 2a 2f 0a   not defined */.
3d1a2 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73        /* This is
3d1a3 20 77 68 61 74 20 77 65 20 64 6f 20 69 66 20 74   what we do if t
3d1a4 68 65 20 67 72 61 6d 6d 61 72 20 64 6f 65 73 20  he grammar does 
3d1a5 6e 6f 74 20 64 65 66 69 6e 65 20 45 52 52 4f 52  not define ERROR
3d1a6 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  :.      **.     
3d1a7 20 2a 2a 20 20 2a 20 52 65 70 6f 72 74 20 61 6e   **  * Report an
3d1a8 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20   error message, 
3d1a9 61 6e 64 20 74 68 72 6f 77 20 61 77 61 79 20 74  and throw away t
3d1aa 68 65 20 69 6e 70 75 74 20 74 6f 6b 65 6e 2e 0a  he input token..
3d1ab 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
3d1ac 2a 20 20 2a 20 49 66 20 74 68 65 20 69 6e 70 75  *  * If the inpu
3d1ad 74 20 74 6f 6b 65 6e 20 69 73 20 24 2c 20 74 68  t token is $, th
3d1ae 65 6e 20 66 61 69 6c 20 74 68 65 20 70 61 72 73  en fail the pars
3d1af 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  e..      **.    
3d1b0 20 20 2a 2a 20 41 73 20 62 65 66 6f 72 65 2c 20    ** As before, 
3d1b1 73 75 62 73 65 71 75 65 6e 74 20 65 72 72 6f 72  subsequent error
3d1b2 20 6d 65 73 73 61 67 65 73 20 61 72 65 20 73 75   messages are su
3d1b3 70 70 72 65 73 73 65 64 20 75 6e 74 69 6c 0a 20  ppressed until. 
3d1b4 20 20 20 20 20 2a 2a 20 74 68 72 65 65 20 69 6e       ** three in
3d1b5 70 75 74 20 74 6f 6b 65 6e 73 20 68 61 76 65 20  put tokens have 
3d1b6 62 65 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c  been successfull
3d1b7 79 20 73 68 69 66 74 65 64 2e 0a 20 20 20 20 20  y shifted..     
3d1b8 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 79 79   */.      if( yy
3d1b9 70 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e  pParser->yyerrcn
3d1ba 74 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  t<=0 ){.        
3d1bb 79 79 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28  yy_syntax_error(
3d1bc 79 79 70 50 61 72 73 65 72 2c 79 79 6d 61 6a 6f  yypParser,yymajo
3d1bd 72 2c 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b  r,yyminorunion);
3d1be 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 79  .      }.      y
3d1bf 79 70 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63  ypParser->yyerrc
3d1c0 6e 74 20 3d 20 33 3b 0a 20 20 20 20 20 20 79 79  nt = 3;.      yy
3d1c1 5f 64 65 73 74 72 75 63 74 6f 72 28 79 79 70 50  _destructor(yypP
3d1c2 61 72 73 65 72 2c 28 59 59 43 4f 44 45 54 59 50  arser,(YYCODETYP
3d1c3 45 29 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e  E)yymajor,&yymin
3d1c4 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20  orunion);.      
3d1c5 69 66 28 20 79 79 65 6e 64 6f 66 69 6e 70 75 74  if( yyendofinput
3d1c6 20 29 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 70   ){.        yy_p
3d1c7 61 72 73 65 5f 66 61 69 6c 65 64 28 79 79 70 50  arse_failed(yypP
3d1c8 61 72 73 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a  arser);.      }.
3d1c9 20 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20        yymajor = 
3d1ca 59 59 4e 4f 43 4f 44 45 3b 0a 23 65 6e 64 69 66  YYNOCODE;.#endif
3d1cb 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28  .    }.  }while(
3d1cc 20 79 79 6d 61 6a 6f 72 21 3d 59 59 4e 4f 43 4f   yymajor!=YYNOCO
3d1cd 44 45 20 26 26 20 79 79 70 50 61 72 73 65 72 2d  DE && yypParser-
3d1ce 3e 79 79 69 64 78 3e 3d 30 20 29 3b 0a 20 20 72  >yyidx>=0 );.  r
3d1cf 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  eturn;.}../*****
3d1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
3d1d1 20 70 61 72 73 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   parse.c *******
3d1d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d1d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d1d4 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
3d1d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
3d1d6 66 69 6c 65 20 74 6f 6b 65 6e 69 7a 65 2e 63 20  file tokenize.c 
3d1d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d1d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d1d9 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
3d1da 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
3d1db 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
3d1dc 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
3d1dd 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
3d1de 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
3d1df 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
3d1e0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
3d1e1 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
3d1e2 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
3d1e3 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
3d1e4 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
3d1e5 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
3d1e6 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
3d1e7 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
3d1e8 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
3d1e9 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
3d1ea 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
3d1eb 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
3d1ec 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
3d1ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d1ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d1ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d1f1 0a 2a 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72  .** An tokenizer
3d1f2 20 66 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54   for SQL.**.** T
3d1f3 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
3d1f4 73 20 43 20 63 6f 64 65 20 74 68 61 74 20 73 70  s C code that sp
3d1f5 6c 69 74 73 20 61 6e 20 53 51 4c 20 69 6e 70 75  lits an SQL inpu
3d1f6 74 20 73 74 72 69 6e 67 20 75 70 20 69 6e 74 6f  t string up into
3d1f7 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74  .** individual t
3d1f8 6f 6b 65 6e 73 20 61 6e 64 20 73 65 6e 64 73 20  okens and sends 
3d1f9 74 68 6f 73 65 20 74 6f 6b 65 6e 73 20 6f 6e 65  those tokens one
3d1fa 2d 62 79 2d 6f 6e 65 20 6f 76 65 72 20 74 6f 20  -by-one over to 
3d1fb 74 68 65 0a 2a 2a 20 70 61 72 73 65 72 20 66 6f  the.** parser fo
3d1fc 72 20 61 6e 61 6c 79 73 69 73 2e 0a 2a 2f 0a 2f  r analysis..*/./
3d1fd 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c  * #include <stdl
3d1fe 69 62 2e 68 3e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ib.h> */../*.** 
3d1ff 54 68 65 20 63 68 61 72 4d 61 70 28 29 20 6d 61  The charMap() ma
3d200 63 72 6f 20 6d 61 70 73 20 61 6c 70 68 61 62 65  cro maps alphabe
3d201 74 69 63 20 63 68 61 72 61 63 74 65 72 73 20 69  tic characters i
3d202 6e 74 6f 20 74 68 65 69 72 0a 2a 2a 20 6c 6f 77  nto their.** low
3d203 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 65 71  er-case ASCII eq
3d204 75 69 76 61 6c 65 6e 74 2e 20 20 4f 6e 20 41 53  uivalent.  On AS
3d205 43 49 49 20 6d 61 63 68 69 6e 65 73 2c 20 74 68  CII machines, th
3d206 69 73 20 69 73 20 6a 75 73 74 0a 2a 2a 20 61 6e  is is just.** an
3d207 20 75 70 70 65 72 2d 74 6f 2d 6c 6f 77 65 72 20   upper-to-lower 
3d208 63 61 73 65 20 6d 61 70 2e 20 20 4f 6e 20 45 42  case map.  On EB
3d209 43 44 49 43 20 6d 61 63 68 69 6e 65 73 20 77 65  CDIC machines we
3d20a 20 61 6c 73 6f 20 6e 65 65 64 0a 2a 2a 20 74 6f   also need.** to
3d20b 20 61 64 6a 75 73 74 20 74 68 65 20 65 6e 63 6f   adjust the enco
3d20c 64 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 6c 70 68  ding.  Only alph
3d20d 61 62 65 74 69 63 20 63 68 61 72 61 63 74 65 72  abetic character
3d20e 73 20 61 6e 64 20 75 6e 64 65 72 73 63 6f 72 65  s and underscore
3d20f 73 0a 2a 2a 20 6e 65 65 64 20 74 6f 20 62 65 20  s.** need to be 
3d210 74 72 61 6e 73 6c 61 74 65 64 2e 0a 2a 2f 0a 23  translated..*/.#
3d211 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
3d212 49 49 0a 23 20 64 65 66 69 6e 65 20 63 68 61 72  II.# define char
3d213 4d 61 70 28 58 29 20 73 71 6c 69 74 65 33 55 70  Map(X) sqlite3Up
3d214 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69  perToLower[(unsi
3d215 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 23 65 6e  gned char)X].#en
3d216 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
3d217 45 5f 45 42 43 44 49 43 0a 23 20 64 65 66 69 6e  E_EBCDIC.# defin
3d218 65 20 63 68 61 72 4d 61 70 28 58 29 20 65 62 63  e charMap(X) ebc
3d219 64 69 63 54 6f 41 73 63 69 69 5b 28 75 6e 73 69  dicToAscii[(unsi
3d21a 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 63 6f 6e  gned char)X].con
3d21b 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
3d21c 20 65 62 63 64 69 63 54 6f 41 73 63 69 69 5b 5d   ebcdicToAscii[]
3d21d 20 3d 20 7b 0a 2f 2a 20 30 20 20 20 31 20 20 20   = {./* 0   1   
3d21e 32 20 20 20 33 20 20 20 34 20 20 20 35 20 20 20  2   3   4   5   
3d21f 36 20 20 20 37 20 20 20 38 20 20 20 39 20 20 20  6   7   8   9   
3d220 41 20 20 20 42 20 20 20 43 20 20 20 44 20 20 20  A   B   C   D   
3d221 45 20 20 20 46 20 2a 2f 0a 20 20 20 30 2c 20 20  E   F */.   0,  
3d222 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d223 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d224 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d225 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 30  0,  0,  0,  /* 0
3d226 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
3d227 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d228 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d229 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d22a 30 2c 20 20 30 2c 20 20 2f 2a 20 31 78 20 2a 2f  0,  0,  /* 1x */
3d22b 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
3d22c 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d22d 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d22e 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d22f 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20  0,  /* 2x */.   
3d230 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d231 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d232 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d233 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d234 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* 3x */.   0,  
3d235 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d236 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d237 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d238 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 34  0,  0,  0,  /* 4
3d239 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
3d23a 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d23b 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d23c 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d23d 30 2c 20 20 30 2c 20 20 2f 2a 20 35 78 20 2a 2f  0,  0,  /* 5x */
3d23e 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
3d23f 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d240 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d241 30 2c 20 20 30 2c 20 39 35 2c 20 20 30 2c 20 20  0,  0, 95,  0,  
3d242 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20  0,  /* 6x */.   
3d243 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d244 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d245 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d246 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d247 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 30 2c 20 39  /* 7x */.   0, 9
3d248 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30  7, 98, 99,100,10
3d249 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30  1,102,103,104,10
3d24a 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  5,  0,  0,  0,  
3d24b 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 38  0,  0,  0,  /* 8
3d24c 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30  x */.   0,106,10
3d24d 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31  7,108,109,110,11
3d24e 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20 20  1,112,113,114,  
3d24f 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d250 30 2c 20 20 30 2c 20 20 2f 2a 20 39 78 20 2a 2f  0,  0,  /* 9x */
3d251 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31 31  .   0,  0,115,11
3d252 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32  6,117,118,119,12
3d253 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20 20  0,121,122,  0,  
3d254 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d255 30 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20  0,  /* Ax */.   
3d256 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d257 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d258 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d259 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d25a 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 30 2c 20 39  /* Bx */.   0, 9
3d25b 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30  7, 98, 99,100,10
3d25c 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30  1,102,103,104,10
3d25d 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  5,  0,  0,  0,  
3d25e 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 43  0,  0,  0,  /* C
3d25f 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30  x */.   0,106,10
3d260 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31  7,108,109,110,11
3d261 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20 20  1,112,113,114,  
3d262 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d263 30 2c 20 20 30 2c 20 20 2f 2a 20 44 78 20 2a 2f  0,  0,  /* Dx */
3d264 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31 31  .   0,  0,115,11
3d265 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32  6,117,118,119,12
3d266 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20 20  0,121,122,  0,  
3d267 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d268 30 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20  0,  /* Ex */.   
3d269 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d26a 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d26b 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d26c 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3d26d 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 65 6e 64  /* Fx */.};.#end
3d26e 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  if../*.** The sq
3d26f 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65  lite3KeywordCode
3d270 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f 6b 73 20   function looks 
3d271 75 70 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72  up an identifier
3d272 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66   to determine if
3d273 0a 2a 2a 20 69 74 20 69 73 20 61 20 6b 65 79 77  .** it is a keyw
3d274 6f 72 64 2e 20 20 49 66 20 69 74 20 69 73 20 61  ord.  If it is a
3d275 20 6b 65 79 77 6f 72 64 2c 20 74 68 65 20 74 6f   keyword, the to
3d276 6b 65 6e 20 63 6f 64 65 20 6f 66 20 74 68 61 74  ken code of that
3d277 20 6b 65 79 77 6f 72 64 20 69 73 20 0a 2a 2a 20   keyword is .** 
3d278 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68  returned.  If th
3d279 65 20 69 6e 70 75 74 20 69 73 20 6e 6f 74 20 61  e input is not a
3d27a 20 6b 65 79 77 6f 72 64 2c 20 54 4b 5f 49 44 20   keyword, TK_ID 
3d27b 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
3d27c 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ** The implement
3d27d 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f  ation of this ro
3d27e 75 74 69 6e 65 20 77 61 73 20 67 65 6e 65 72 61  utine was genera
3d27f 74 65 64 20 62 79 20 61 20 70 72 6f 67 72 61 6d  ted by a program
3d280 2c 0a 2a 2a 20 6d 6b 6b 65 79 77 6f 72 64 68 61  ,.** mkkeywordha
3d281 73 68 2e 68 2c 20 6c 6f 63 61 74 65 64 20 69 6e  sh.h, located in
3d282 20 74 68 65 20 74 6f 6f 6c 20 73 75 62 64 69 72   the tool subdir
3d283 65 63 74 6f 72 79 20 6f 66 20 74 68 65 20 64 69  ectory of the di
3d284 73 74 72 69 62 75 74 69 6f 6e 2e 0a 2a 2a 20 54  stribution..** T
3d285 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65  he output of the
3d286 20 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 63   mkkeywordhash.c
3d287 20 70 72 6f 67 72 61 6d 20 69 73 20 77 72 69 74   program is writ
3d288 74 65 6e 20 69 6e 74 6f 20 61 20 66 69 6c 65 0a  ten into a file.
3d289 2a 2a 20 6e 61 6d 65 64 20 6b 65 79 77 6f 72 64  ** named keyword
3d28a 68 61 73 68 2e 68 20 61 6e 64 20 74 68 65 6e 20  hash.h and then 
3d28b 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68  included into th
3d28c 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 62  is source file b
3d28d 79 0a 2a 2a 20 74 68 65 20 23 69 6e 63 6c 75 64  y.** the #includ
3d28e 65 20 62 65 6c 6f 77 2e 0a 2a 2f 0a 2f 2a 2a 2a  e below..*/./***
3d28f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
3d290 75 64 65 20 6b 65 79 77 6f 72 64 68 61 73 68 2e  ude keywordhash.
3d291 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
3d292 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a  of tokenize.c **
3d293 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
3d294 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
3d295 6e 20 66 69 6c 65 20 6b 65 79 77 6f 72 64 68 61  n file keywordha
3d296 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.h ***********
3d297 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d298 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
3d299 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
3d29a 74 61 69 6e 73 20 61 75 74 6f 6d 61 74 69 63 61  tains automatica
3d29b 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f  lly generated co
3d29c 64 65 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  de ******.**.** 
3d29d 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
3d29e 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 61   file has been a
3d29f 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e  utomatically gen
3d2a0 65 72 61 74 65 64 20 62 79 0a 2a 2a 0a 2a 2a 20  erated by.**.** 
3d2a1 20 20 73 71 6c 69 74 65 2f 74 6f 6f 6c 2f 6d 6b    sqlite/tool/mk
3d2a2 6b 65 79 77 6f 72 64 68 61 73 68 2e 63 0a 2a 2a  keywordhash.c.**
3d2a3 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
3d2a4 74 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d  this file implem
3d2a5 65 6e 74 73 20 61 20 66 75 6e 63 74 69 6f 6e 20  ents a function 
3d2a6 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 20  that determines 
3d2a7 77 68 65 74 68 65 72 0a 2a 2a 20 6f 72 20 6e 6f  whether.** or no
3d2a8 74 20 61 20 67 69 76 65 6e 20 69 64 65 6e 74 69  t a given identi
3d2a9 66 69 65 72 20 69 73 20 72 65 61 6c 6c 79 20 61  fier is really a
3d2aa 6e 20 53 51 4c 20 6b 65 79 77 6f 72 64 2e 20 20  n SQL keyword.  
3d2ab 54 68 65 20 73 61 6d 65 20 74 68 69 6e 67 0a 2a  The same thing.*
3d2ac 2a 20 6d 69 67 68 74 20 62 65 20 69 6d 70 6c 65  * might be imple
3d2ad 6d 65 6e 74 65 64 20 6d 6f 72 65 20 64 69 72 65  mented more dire
3d2ae 63 74 6c 79 20 75 73 69 6e 67 20 61 20 68 61 6e  ctly using a han
3d2af 64 2d 77 72 69 74 74 65 6e 20 68 61 73 68 20 74  d-written hash t
3d2b0 61 62 6c 65 2e 0a 2a 2a 20 42 75 74 20 62 79 20  able..** But by 
3d2b1 75 73 69 6e 67 20 74 68 69 73 20 61 75 74 6f 6d  using this autom
3d2b2 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74  atically generat
3d2b3 65 64 20 63 6f 64 65 2c 20 74 68 65 20 73 69 7a  ed code, the siz
3d2b4 65 20 6f 66 20 74 68 65 20 63 6f 64 65 0a 2a 2a  e of the code.**
3d2b5 20 69 73 20 73 75 62 73 74 61 6e 74 69 61 6c 6c   is substantiall
3d2b6 79 20 72 65 64 75 63 65 64 2e 20 20 54 68 69 73  y reduced.  This
3d2b7 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f   is important fo
3d2b8 72 20 65 6d 62 65 64 64 65 64 20 61 70 70 6c 69  r embedded appli
3d2b9 63 61 74 69 6f 6e 73 0a 2a 2a 20 6f 6e 20 70 6c  cations.** on pl
3d2ba 61 74 66 6f 72 6d 73 20 77 69 74 68 20 6c 69 6d  atforms with lim
3d2bb 69 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a  ited memory..*/.
3d2bc 2f 2a 20 48 61 73 68 20 73 63 6f 72 65 3a 20 31  /* Hash score: 1
3d2bd 37 35 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  75 */.static int
3d2be 20 6b 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e   keywordCode(con
3d2bf 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
3d2c0 6e 29 7b 0a 20 20 2f 2a 20 7a 54 65 78 74 5b 5d  n){.  /* zText[]
3d2c1 20 65 6e 63 6f 64 65 73 20 38 31 31 20 62 79 74   encodes 811 byt
3d2c2 65 73 20 6f 66 20 6b 65 79 77 6f 72 64 73 20 69  es of keywords i
3d2c3 6e 20 35 34 31 20 62 79 74 65 73 20 2a 2f 0a 20  n 541 bytes */. 
3d2c4 20 2f 2a 20 20 20 52 45 49 4e 44 45 58 45 44 45   /*   REINDEXEDE
3d2c5 53 43 41 50 45 41 43 48 45 43 4b 45 59 42 45 46  SCAPEACHECKEYBEF
3d2c6 4f 52 45 49 47 4e 4f 52 45 47 45 58 50 4c 41 49  OREIGNOREGEXPLAI
3d2c7 4e 53 54 45 41 44 44 41 54 41 42 41 53 45 4c 45  NSTEADDATABASELE
3d2c8 43 54 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a  CT       */.  /*
3d2c9 20 20 20 41 42 4c 45 46 54 48 45 4e 44 45 46 45     ABLEFTHENDEFE
3d2ca 52 52 41 42 4c 45 4c 53 45 58 43 45 50 54 52 41  RRABLELSEXCEPTRA
3d2cb 4e 53 41 43 54 49 4f 4e 41 54 55 52 41 4c 54 45  NSACTIONATURALTE
3d2cc 52 41 49 53 45 58 43 4c 55 53 49 56 45 20 20 20  RAISEXCLUSIVE   
3d2cd 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20        */.  /*   
3d2ce 58 49 53 54 53 41 56 45 50 4f 49 4e 54 45 52 53  XISTSAVEPOINTERS
3d2cf 45 43 54 52 49 47 47 45 52 45 46 45 52 45 4e 43  ECTRIGGEREFERENC
3d2d0 45 53 43 4f 4e 53 54 52 41 49 4e 54 4f 46 46 53  ESCONSTRAINTOFFS
3d2d1 45 54 45 4d 50 4f 52 41 52 59 20 20 20 20 20 20  ETEMPORARY      
3d2d2 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 55 4e 49     */.  /*   UNI
3d2d3 51 55 45 52 59 41 54 54 41 43 48 41 56 49 4e 47  QUERYATTACHAVING
3d2d4 52 4f 55 50 44 41 54 45 42 45 47 49 4e 4e 45 52  ROUPDATEBEGINNER
3d2d5 45 4c 45 41 53 45 42 45 54 57 45 45 4e 4f 54 4e  ELEASEBETWEENOTN
3d2d6 55 4c 4c 49 4b 45 20 20 20 20 20 20 20 20 20 20  ULLIKE          
3d2d7 2a 2f 0a 20 20 2f 2a 20 20 20 43 41 53 43 41 44  */.  /*   CASCAD
3d2d8 45 4c 45 54 45 43 41 53 45 43 4f 4c 4c 41 54 45  ELETECASECOLLATE
3d2d9 43 52 45 41 54 45 43 55 52 52 45 4e 54 5f 44 41  CREATECURRENT_DA
3d2da 54 45 44 45 54 41 43 48 49 4d 4d 45 44 49 41 54  TEDETACHIMMEDIAT
3d2db 45 4a 4f 49 4e 20 20 20 20 20 20 20 20 2a 2f 0a  EJOIN        */.
3d2dc 20 20 2f 2a 20 20 20 53 45 52 54 4d 41 54 43 48    /*   SERTMATCH
3d2dd 50 4c 41 4e 41 4c 59 5a 45 50 52 41 47 4d 41 42  PLANALYZEPRAGMAB
3d2de 4f 52 54 56 41 4c 55 45 53 56 49 52 54 55 41 4c  ORTVALUESVIRTUAL
3d2df 49 4d 49 54 57 48 45 4e 57 48 45 52 45 4e 41 4d  IMITWHENWHERENAM
3d2e0 45 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f  E         */.  /
3d2e1 2a 20 20 20 41 46 54 45 52 45 50 4c 41 43 45 41  *   AFTEREPLACEA
3d2e2 4e 44 45 46 41 55 4c 54 41 55 54 4f 49 4e 43 52  NDEFAULTAUTOINCR
3d2e3 45 4d 45 4e 54 43 41 53 54 43 4f 4c 55 4d 4e 43  EMENTCASTCOLUMNC
3d2e4 4f 4d 4d 49 54 43 4f 4e 46 4c 49 43 54 43 52 4f  OMMITCONFLICTCRO
3d2e5 53 53 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20  SS     */.  /*  
3d2e6 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 53 54 41   CURRENT_TIMESTA
3d2e7 4d 50 52 49 4d 41 52 59 44 45 46 45 52 52 45 44  MPRIMARYDEFERRED
3d2e8 49 53 54 49 4e 43 54 44 52 4f 50 46 41 49 4c 46  ISTINCTDROPFAILF
3d2e9 52 4f 4d 46 55 4c 4c 47 4c 4f 42 59 49 46 20 20  ROMFULLGLOBYIF  
3d2ea 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 49 53      */.  /*   IS
3d2eb 4e 55 4c 4c 4f 52 44 45 52 45 53 54 52 49 43 54  NULLORDERESTRICT
3d2ec 4f 55 54 45 52 49 47 48 54 52 4f 4c 4c 42 41 43  OUTERIGHTROLLBAC
3d2ed 4b 52 4f 57 55 4e 49 4f 4e 55 53 49 4e 47 56 41  KROWUNIONUSINGVA
3d2ee 43 55 55 4d 56 49 45 57 20 20 20 20 20 20 20 20  CUUMVIEW        
3d2ef 20 2a 2f 0a 20 20 2f 2a 20 20 20 49 4e 49 54 49   */.  /*   INITI
3d2f0 41 4c 4c 59 20 20 20 20 20 20 20 20 20 20 20 20  ALLY            
3d2f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d2f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d2f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f                */
3d2f4 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
3d2f5 63 68 61 72 20 7a 54 65 78 74 5b 35 34 30 5d 20  char zText[540] 
3d2f6 3d 20 7b 0a 20 20 20 20 27 52 27 2c 27 45 27 2c  = {.    'R','E',
3d2f7 27 49 27 2c 27 4e 27 2c 27 44 27 2c 27 45 27 2c  'I','N','D','E',
3d2f8 27 58 27 2c 27 45 27 2c 27 44 27 2c 27 45 27 2c  'X','E','D','E',
3d2f9 27 53 27 2c 27 43 27 2c 27 41 27 2c 27 50 27 2c  'S','C','A','P',
3d2fa 27 45 27 2c 27 41 27 2c 27 43 27 2c 27 48 27 2c  'E','A','C','H',
3d2fb 0a 20 20 20 20 27 45 27 2c 27 43 27 2c 27 4b 27  .    'E','C','K'
3d2fc 2c 27 45 27 2c 27 59 27 2c 27 42 27 2c 27 45 27  ,'E','Y','B','E'
3d2fd 2c 27 46 27 2c 27 4f 27 2c 27 52 27 2c 27 45 27  ,'F','O','R','E'
3d2fe 2c 27 49 27 2c 27 47 27 2c 27 4e 27 2c 27 4f 27  ,'I','G','N','O'
3d2ff 2c 27 52 27 2c 27 45 27 2c 27 47 27 2c 0a 20 20  ,'R','E','G',.  
3d300 20 20 27 45 27 2c 27 58 27 2c 27 50 27 2c 27 4c    'E','X','P','L
3d301 27 2c 27 41 27 2c 27 49 27 2c 27 4e 27 2c 27 53  ','A','I','N','S
3d302 27 2c 27 54 27 2c 27 45 27 2c 27 41 27 2c 27 44  ','T','E','A','D
3d303 27 2c 27 44 27 2c 27 41 27 2c 27 54 27 2c 27 41  ','D','A','T','A
3d304 27 2c 27 42 27 2c 27 41 27 2c 0a 20 20 20 20 27  ','B','A',.    '
3d305 53 27 2c 27 45 27 2c 27 4c 27 2c 27 45 27 2c 27  S','E','L','E','
3d306 43 27 2c 27 54 27 2c 27 41 27 2c 27 42 27 2c 27  C','T','A','B','
3d307 4c 27 2c 27 45 27 2c 27 46 27 2c 27 54 27 2c 27  L','E','F','T','
3d308 48 27 2c 27 45 27 2c 27 4e 27 2c 27 44 27 2c 27  H','E','N','D','
3d309 45 27 2c 27 46 27 2c 0a 20 20 20 20 27 45 27 2c  E','F',.    'E',
3d30a 27 52 27 2c 27 52 27 2c 27 41 27 2c 27 42 27 2c  'R','R','A','B',
3d30b 27 4c 27 2c 27 45 27 2c 27 4c 27 2c 27 53 27 2c  'L','E','L','S',
3d30c 27 45 27 2c 27 58 27 2c 27 43 27 2c 27 45 27 2c  'E','X','C','E',
3d30d 27 50 27 2c 27 54 27 2c 27 52 27 2c 27 41 27 2c  'P','T','R','A',
3d30e 27 4e 27 2c 0a 20 20 20 20 27 53 27 2c 27 41 27  'N',.    'S','A'
3d30f 2c 27 43 27 2c 27 54 27 2c 27 49 27 2c 27 4f 27  ,'C','T','I','O'
3d310 2c 27 4e 27 2c 27 41 27 2c 27 54 27 2c 27 55 27  ,'N','A','T','U'
3d311 2c 27 52 27 2c 27 41 27 2c 27 4c 27 2c 27 54 27  ,'R','A','L','T'
3d312 2c 27 45 27 2c 27 52 27 2c 27 41 27 2c 27 49 27  ,'E','R','A','I'
3d313 2c 0a 20 20 20 20 27 53 27 2c 27 45 27 2c 27 58  ,.    'S','E','X
3d314 27 2c 27 43 27 2c 27 4c 27 2c 27 55 27 2c 27 53  ','C','L','U','S
3d315 27 2c 27 49 27 2c 27 56 27 2c 27 45 27 2c 27 58  ','I','V','E','X
3d316 27 2c 27 49 27 2c 27 53 27 2c 27 54 27 2c 27 53  ','I','S','T','S
3d317 27 2c 27 41 27 2c 27 56 27 2c 27 45 27 2c 0a 20  ','A','V','E',. 
3d318 20 20 20 27 50 27 2c 27 4f 27 2c 27 49 27 2c 27     'P','O','I','
3d319 4e 27 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c 27  N','T','E','R','
3d31a 53 27 2c 27 45 27 2c 27 43 27 2c 27 54 27 2c 27  S','E','C','T','
3d31b 52 27 2c 27 49 27 2c 27 47 27 2c 27 47 27 2c 27  R','I','G','G','
3d31c 45 27 2c 27 52 27 2c 27 45 27 2c 0a 20 20 20 20  E','R','E',.    
3d31d 27 46 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c  'F','E','R','E',
3d31e 27 4e 27 2c 27 43 27 2c 27 45 27 2c 27 53 27 2c  'N','C','E','S',
3d31f 27 43 27 2c 27 4f 27 2c 27 4e 27 2c 27 53 27 2c  'C','O','N','S',
3d320 27 54 27 2c 27 52 27 2c 27 41 27 2c 27 49 27 2c  'T','R','A','I',
3d321 27 4e 27 2c 27 54 27 2c 0a 20 20 20 20 27 4f 27  'N','T',.    'O'
3d322 2c 27 46 27 2c 27 46 27 2c 27 53 27 2c 27 45 27  ,'F','F','S','E'
3d323 2c 27 54 27 2c 27 45 27 2c 27 4d 27 2c 27 50 27  ,'T','E','M','P'
3d324 2c 27 4f 27 2c 27 52 27 2c 27 41 27 2c 27 52 27  ,'O','R','A','R'
3d325 2c 27 59 27 2c 27 55 27 2c 27 4e 27 2c 27 49 27  ,'Y','U','N','I'
3d326 2c 27 51 27 2c 0a 20 20 20 20 27 55 27 2c 27 45  ,'Q',.    'U','E
3d327 27 2c 27 52 27 2c 27 59 27 2c 27 41 27 2c 27 54  ','R','Y','A','T
3d328 27 2c 27 54 27 2c 27 41 27 2c 27 43 27 2c 27 48  ','T','A','C','H
3d329 27 2c 27 41 27 2c 27 56 27 2c 27 49 27 2c 27 4e  ','A','V','I','N
3d32a 27 2c 27 47 27 2c 27 52 27 2c 27 4f 27 2c 27 55  ','G','R','O','U
3d32b 27 2c 0a 20 20 20 20 27 50 27 2c 27 44 27 2c 27  ',.    'P','D','
3d32c 41 27 2c 27 54 27 2c 27 45 27 2c 27 42 27 2c 27  A','T','E','B','
3d32d 45 27 2c 27 47 27 2c 27 49 27 2c 27 4e 27 2c 27  E','G','I','N','
3d32e 4e 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27  N','E','R','E','
3d32f 4c 27 2c 27 45 27 2c 27 41 27 2c 27 53 27 2c 0a  L','E','A','S',.
3d330 20 20 20 20 27 45 27 2c 27 42 27 2c 27 45 27 2c      'E','B','E',
3d331 27 54 27 2c 27 57 27 2c 27 45 27 2c 27 45 27 2c  'T','W','E','E',
3d332 27 4e 27 2c 27 4f 27 2c 27 54 27 2c 27 4e 27 2c  'N','O','T','N',
3d333 27 55 27 2c 27 4c 27 2c 27 4c 27 2c 27 49 27 2c  'U','L','L','I',
3d334 27 4b 27 2c 27 45 27 2c 27 43 27 2c 0a 20 20 20  'K','E','C',.   
3d335 20 27 41 27 2c 27 53 27 2c 27 43 27 2c 27 41 27   'A','S','C','A'
3d336 2c 27 44 27 2c 27 45 27 2c 27 4c 27 2c 27 45 27  ,'D','E','L','E'
3d337 2c 27 54 27 2c 27 45 27 2c 27 43 27 2c 27 41 27  ,'T','E','C','A'
3d338 2c 27 53 27 2c 27 45 27 2c 27 43 27 2c 27 4f 27  ,'S','E','C','O'
3d339 2c 27 4c 27 2c 27 4c 27 2c 0a 20 20 20 20 27 41  ,'L','L',.    'A
3d33a 27 2c 27 54 27 2c 27 45 27 2c 27 43 27 2c 27 52  ','T','E','C','R
3d33b 27 2c 27 45 27 2c 27 41 27 2c 27 54 27 2c 27 45  ','E','A','T','E
3d33c 27 2c 27 43 27 2c 27 55 27 2c 27 52 27 2c 27 52  ','C','U','R','R
3d33d 27 2c 27 45 27 2c 27 4e 27 2c 27 54 27 2c 27 5f  ','E','N','T','_
3d33e 27 2c 27 44 27 2c 0a 20 20 20 20 27 41 27 2c 27  ','D',.    'A','
3d33f 54 27 2c 27 45 27 2c 27 44 27 2c 27 45 27 2c 27  T','E','D','E','
3d340 54 27 2c 27 41 27 2c 27 43 27 2c 27 48 27 2c 27  T','A','C','H','
3d341 49 27 2c 27 4d 27 2c 27 4d 27 2c 27 45 27 2c 27  I','M','M','E','
3d342 44 27 2c 27 49 27 2c 27 41 27 2c 27 54 27 2c 27  D','I','A','T','
3d343 45 27 2c 0a 20 20 20 20 27 4a 27 2c 27 4f 27 2c  E',.    'J','O',
3d344 27 49 27 2c 27 4e 27 2c 27 53 27 2c 27 45 27 2c  'I','N','S','E',
3d345 27 52 27 2c 27 54 27 2c 27 4d 27 2c 27 41 27 2c  'R','T','M','A',
3d346 27 54 27 2c 27 43 27 2c 27 48 27 2c 27 50 27 2c  'T','C','H','P',
3d347 27 4c 27 2c 27 41 27 2c 27 4e 27 2c 27 41 27 2c  'L','A','N','A',
3d348 0a 20 20 20 20 27 4c 27 2c 27 59 27 2c 27 5a 27  .    'L','Y','Z'
3d349 2c 27 45 27 2c 27 50 27 2c 27 52 27 2c 27 41 27  ,'E','P','R','A'
3d34a 2c 27 47 27 2c 27 4d 27 2c 27 41 27 2c 27 42 27  ,'G','M','A','B'
3d34b 2c 27 4f 27 2c 27 52 27 2c 27 54 27 2c 27 56 27  ,'O','R','T','V'
3d34c 2c 27 41 27 2c 27 4c 27 2c 27 55 27 2c 0a 20 20  ,'A','L','U',.  
3d34d 20 20 27 45 27 2c 27 53 27 2c 27 56 27 2c 27 49    'E','S','V','I
3d34e 27 2c 27 52 27 2c 27 54 27 2c 27 55 27 2c 27 41  ','R','T','U','A
3d34f 27 2c 27 4c 27 2c 27 49 27 2c 27 4d 27 2c 27 49  ','L','I','M','I
3d350 27 2c 27 54 27 2c 27 57 27 2c 27 48 27 2c 27 45  ','T','W','H','E
3d351 27 2c 27 4e 27 2c 27 57 27 2c 0a 20 20 20 20 27  ','N','W',.    '
3d352 48 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27  H','E','R','E','
3d353 4e 27 2c 27 41 27 2c 27 4d 27 2c 27 45 27 2c 27  N','A','M','E','
3d354 41 27 2c 27 46 27 2c 27 54 27 2c 27 45 27 2c 27  A','F','T','E','
3d355 52 27 2c 27 45 27 2c 27 50 27 2c 27 4c 27 2c 27  R','E','P','L','
3d356 41 27 2c 27 43 27 2c 0a 20 20 20 20 27 45 27 2c  A','C',.    'E',
3d357 27 41 27 2c 27 4e 27 2c 27 44 27 2c 27 45 27 2c  'A','N','D','E',
3d358 27 46 27 2c 27 41 27 2c 27 55 27 2c 27 4c 27 2c  'F','A','U','L',
3d359 27 54 27 2c 27 41 27 2c 27 55 27 2c 27 54 27 2c  'T','A','U','T',
3d35a 27 4f 27 2c 27 49 27 2c 27 4e 27 2c 27 43 27 2c  'O','I','N','C',
3d35b 27 52 27 2c 0a 20 20 20 20 27 45 27 2c 27 4d 27  'R',.    'E','M'
3d35c 2c 27 45 27 2c 27 4e 27 2c 27 54 27 2c 27 43 27  ,'E','N','T','C'
3d35d 2c 27 41 27 2c 27 53 27 2c 27 54 27 2c 27 43 27  ,'A','S','T','C'
3d35e 2c 27 4f 27 2c 27 4c 27 2c 27 55 27 2c 27 4d 27  ,'O','L','U','M'
3d35f 2c 27 4e 27 2c 27 43 27 2c 27 4f 27 2c 27 4d 27  ,'N','C','O','M'
3d360 2c 0a 20 20 20 20 27 4d 27 2c 27 49 27 2c 27 54  ,.    'M','I','T
3d361 27 2c 27 43 27 2c 27 4f 27 2c 27 4e 27 2c 27 46  ','C','O','N','F
3d362 27 2c 27 4c 27 2c 27 49 27 2c 27 43 27 2c 27 54  ','L','I','C','T
3d363 27 2c 27 43 27 2c 27 52 27 2c 27 4f 27 2c 27 53  ','C','R','O','S
3d364 27 2c 27 53 27 2c 27 43 27 2c 27 55 27 2c 0a 20  ','S','C','U',. 
3d365 20 20 20 27 52 27 2c 27 52 27 2c 27 45 27 2c 27     'R','R','E','
3d366 4e 27 2c 27 54 27 2c 27 5f 27 2c 27 54 27 2c 27  N','T','_','T','
3d367 49 27 2c 27 4d 27 2c 27 45 27 2c 27 53 27 2c 27  I','M','E','S','
3d368 54 27 2c 27 41 27 2c 27 4d 27 2c 27 50 27 2c 27  T','A','M','P','
3d369 52 27 2c 27 49 27 2c 27 4d 27 2c 0a 20 20 20 20  R','I','M',.    
3d36a 27 41 27 2c 27 52 27 2c 27 59 27 2c 27 44 27 2c  'A','R','Y','D',
3d36b 27 45 27 2c 27 46 27 2c 27 45 27 2c 27 52 27 2c  'E','F','E','R',
3d36c 27 52 27 2c 27 45 27 2c 27 44 27 2c 27 49 27 2c  'R','E','D','I',
3d36d 27 53 27 2c 27 54 27 2c 27 49 27 2c 27 4e 27 2c  'S','T','I','N',
3d36e 27 43 27 2c 27 54 27 2c 0a 20 20 20 20 27 44 27  'C','T',.    'D'
3d36f 2c 27 52 27 2c 27 4f 27 2c 27 50 27 2c 27 46 27  ,'R','O','P','F'
3d370 2c 27 41 27 2c 27 49 27 2c 27 4c 27 2c 27 46 27  ,'A','I','L','F'
3d371 2c 27 52 27 2c 27 4f 27 2c 27 4d 27 2c 27 46 27  ,'R','O','M','F'
3d372 2c 27 55 27 2c 27 4c 27 2c 27 4c 27 2c 27 47 27  ,'U','L','L','G'
3d373 2c 27 4c 27 2c 0a 20 20 20 20 27 4f 27 2c 27 42  ,'L',.    'O','B
3d374 27 2c 27 59 27 2c 27 49 27 2c 27 46 27 2c 27 49  ','Y','I','F','I
3d375 27 2c 27 53 27 2c 27 4e 27 2c 27 55 27 2c 27 4c  ','S','N','U','L
3d376 27 2c 27 4c 27 2c 27 4f 27 2c 27 52 27 2c 27 44  ','L','O','R','D
3d377 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 53  ','E','R','E','S
3d378 27 2c 0a 20 20 20 20 27 54 27 2c 27 52 27 2c 27  ',.    'T','R','
3d379 49 27 2c 27 43 27 2c 27 54 27 2c 27 4f 27 2c 27  I','C','T','O','
3d37a 55 27 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c 27  U','T','E','R','
3d37b 49 27 2c 27 47 27 2c 27 48 27 2c 27 54 27 2c 27  I','G','H','T','
3d37c 52 27 2c 27 4f 27 2c 27 4c 27 2c 27 4c 27 2c 0a  R','O','L','L',.
3d37d 20 20 20 20 27 42 27 2c 27 41 27 2c 27 43 27 2c      'B','A','C',
3d37e 27 4b 27 2c 27 52 27 2c 27 4f 27 2c 27 57 27 2c  'K','R','O','W',
3d37f 27 55 27 2c 27 4e 27 2c 27 49 27 2c 27 4f 27 2c  'U','N','I','O',
3d380 27 4e 27 2c 27 55 27 2c 27 53 27 2c 27 49 27 2c  'N','U','S','I',
3d381 27 4e 27 2c 27 47 27 2c 27 56 27 2c 0a 20 20 20  'N','G','V',.   
3d382 20 27 41 27 2c 27 43 27 2c 27 55 27 2c 27 55 27   'A','C','U','U'
3d383 2c 27 4d 27 2c 27 56 27 2c 27 49 27 2c 27 45 27  ,'M','V','I','E'
3d384 2c 27 57 27 2c 27 49 27 2c 27 4e 27 2c 27 49 27  ,'W','I','N','I'
3d385 2c 27 54 27 2c 27 49 27 2c 27 41 27 2c 27 4c 27  ,'T','I','A','L'
3d386 2c 27 4c 27 2c 27 59 27 2c 0a 20 20 7d 3b 0a 20  ,'L','Y',.  };. 
3d387 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
3d388 73 69 67 6e 65 64 20 63 68 61 72 20 61 48 61 73  signed char aHas
3d389 68 5b 31 32 37 5d 20 3d 20 7b 0a 20 20 20 20 20  h[127] = {.     
3d38a 20 37 32 2c 20 31 30 31 2c 20 31 31 34 2c 20 20   72, 101, 114,  
3d38b 37 30 2c 20 20 20 30 2c 20 20 34 35 2c 20 20 20  70,   0,  45,   
3d38c 30 2c 20 20 20 30 2c 20 20 37 38 2c 20 20 20 30  0,   0,  78,   0
3d38d 2c 20 20 37 33 2c 20 20 20 30 2c 20 20 20 30 2c  ,  73,   0,   0,
3d38e 0a 20 20 20 20 20 20 34 32 2c 20 20 31 32 2c 20  .      42,  12, 
3d38f 20 37 34 2c 20 20 31 35 2c 20 20 20 30 2c 20 31   74,  15,   0, 1
3d390 31 33 2c 20 20 38 31 2c 20 20 35 30 2c 20 31 30  13,  81,  50, 10
3d391 38 2c 20 20 20 30 2c 20 20 31 39 2c 20 20 20 30  8,   0,  19,   0
3d392 2c 20 20 20 30 2c 0a 20 20 20 20 20 31 31 38 2c  ,   0,.     118,
3d393 20 20 20 30 2c 20 31 31 36 2c 20 31 31 31 2c 20     0, 116, 111, 
3d394 20 20 30 2c 20 20 32 32 2c 20 20 38 39 2c 20 20    0,  22,  89,  
3d395 20 30 2c 20 20 20 39 2c 20 20 20 30 2c 20 20 20   0,   9,   0,   
3d396 30 2c 20 20 36 36 2c 20 20 36 37 2c 0a 20 20 20  0,  66,  67,.   
3d397 20 20 20 20 30 2c 20 20 36 35 2c 20 20 20 36 2c      0,  65,   6,
3d398 20 20 20 30 2c 20 20 34 38 2c 20 20 38 36 2c 20     0,  48,  86, 
3d399 20 39 38 2c 20 20 20 30 2c 20 31 31 35 2c 20 20   98,   0, 115,  
3d39a 39 37 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 34  97,   0,   0,  4
3d39b 34 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 39 39  4,.       0,  99
3d39c 2c 20 20 32 34 2c 20 20 20 30 2c 20 20 31 37 2c  ,  24,   0,  17,
3d39d 20 20 20 30 2c 20 31 31 39 2c 20 20 34 39 2c 20     0, 119,  49, 
3d39e 20 32 33 2c 20 20 20 30 2c 20 20 20 35 2c 20 31   23,   0,   5, 1
3d39f 30 36 2c 20 20 32 35 2c 0a 20 20 20 20 20 20 39  06,  25,.      9
3d3a0 32 2c 20 20 20 30 2c 20 20 20 30 2c 20 31 32 31  2,   0,   0, 121
3d3a1 2c 20 31 30 32 2c 20 20 35 36 2c 20 31 32 30 2c  , 102,  56, 120,
3d3a2 20 20 35 33 2c 20 20 32 38 2c 20 20 35 31 2c 20    53,  28,  51, 
3d3a3 20 20 30 2c 20 20 38 37 2c 20 20 20 30 2c 0a 20    0,  87,   0,. 
3d3a4 20 20 20 20 20 39 36 2c 20 20 32 36 2c 20 20 20       96,  26,   
3d3a5 30 2c 20 20 39 35 2c 20 20 20 30 2c 20 20 20 30  0,  95,   0,   0
3d3a6 2c 20 20 20 30 2c 20 20 39 31 2c 20 20 38 38 2c  ,   0,  91,  88,
3d3a7 20 20 39 33 2c 20 20 38 34 2c 20 31 30 35 2c 20    93,  84, 105, 
3d3a8 20 31 34 2c 0a 20 20 20 20 20 20 33 39 2c 20 31   14,.      39, 1
3d3a9 30 34 2c 20 20 20 30 2c 20 20 37 37 2c 20 20 20  04,   0,  77,   
3d3aa 30 2c 20 20 31 38 2c 20 20 38 35 2c 20 31 30 37  0,  18,  85, 107
3d3ab 2c 20 20 33 32 2c 20 20 20 30 2c 20 31 31 37 2c  ,  32,   0, 117,
3d3ac 20 20 37 36 2c 20 31 30 39 2c 0a 20 20 20 20 20    76, 109,.     
3d3ad 20 35 38 2c 20 20 34 36 2c 20 20 38 30 2c 20 20   58,  46,  80,  
3d3ae 20 30 2c 20 20 20 30 2c 20 20 39 30 2c 20 20 34   0,   0,  90,  4
3d3af 30 2c 20 20 20 30 2c 20 31 31 32 2c 20 20 20 30  0,   0, 112,   0
3d3b0 2c 20 20 33 36 2c 20 20 20 30 2c 20 20 20 30 2c  ,  36,   0,   0,
3d3b1 0a 20 20 20 20 20 20 32 39 2c 20 20 20 30 2c 20  .      29,   0, 
3d3b2 20 38 32 2c 20 20 35 39 2c 20 20 36 30 2c 20 20   82,  59,  60,  
3d3b3 20 30 2c 20 20 32 30 2c 20 20 35 37 2c 20 20 20   0,  20,  57,   
3d3b4 30 2c 20 20 35 32 2c 0a 20 20 7d 3b 0a 20 20 73  0,  52,.  };.  s
3d3b5 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
3d3b6 67 6e 65 64 20 63 68 61 72 20 61 4e 65 78 74 5b  gned char aNext[
3d3b7 31 32 31 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  121] = {.       
3d3b8 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
3d3b9 2c 20 20 20 34 2c 20 20 20 30 2c 20 20 20 30 2c  ,   4,   0,   0,
3d3ba 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
3d3bb 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20    0,   0,   0,. 
3d3bc 20 20 20 20 20 20 30 2c 20 20 20 32 2c 20 20 20        0,   2,   
3d3bd 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
3d3be 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 31 33 2c  ,   0,   0,  13,
3d3bf 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
3d3c0 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20    0,.       0,  
3d3c1 20 37 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   7,   0,   0,   
3d3c2 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
3d3c3 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
3d3c4 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20     0,   0,.     
3d3c5 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
3d3c6 20 30 2c 20 20 33 33 2c 20 20 20 30 2c 20 20 32   0,  33,   0,  2
3d3c7 31 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  1,   0,   0,   0
3d3c8 2c 20 20 34 33 2c 20 20 20 33 2c 20 20 34 37 2c  ,  43,   3,  47,
3d3c9 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20  .       0,   0, 
3d3ca 20 20 30 2c 20 20 20 30 2c 20 20 33 30 2c 20 20    0,   0,  30,  
3d3cb 20 30 2c 20 20 35 34 2c 20 20 20 30 2c 20 20 33   0,  54,   0,  3
3d3cc 38 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  8,   0,   0,   0
3d3cd 2c 20 20 20 31 2c 0a 20 20 20 20 20 20 36 32 2c  ,   1,.      62,
3d3ce 20 20 20 30 2c 20 20 20 30 2c 20 20 36 33 2c 20     0,   0,  63, 
3d3cf 20 20 30 2c 20 20 34 31 2c 20 20 20 30 2c 20 20    0,  41,   0,  
3d3d0 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
3d3d1 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20  0,   0,   0,.   
3d3d2 20 20 20 36 31 2c 20 20 20 30 2c 20 20 20 30 2c     61,   0,   0,
3d3d3 20 20 20 30 2c 20 20 20 30 2c 20 20 33 31 2c 20     0,   0,  31, 
3d3d4 20 35 35 2c 20 20 31 36 2c 20 20 33 34 2c 20 20   55,  16,  34,  
3d3d5 31 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20  10,   0,   0,   
3d3d6 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30  0,.       0,   0
3d3d7 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 31 31 2c  ,   0,   0,  11,
3d3d8 20 20 36 38 2c 20 20 37 35 2c 20 20 20 30 2c 20    68,  75,   0, 
3d3d9 20 20 38 2c 20 20 20 30 2c 20 31 30 30 2c 20 20    8,   0, 100,  
3d3da 39 34 2c 20 20 20 30 2c 0a 20 20 20 20 20 31 30  94,   0,.     10
3d3db 33 2c 20 20 20 30 2c 20 20 38 33 2c 20 20 20 30  3,   0,  83,   0
3d3dc 2c 20 20 37 31 2c 20 20 20 30 2c 20 20 20 30 2c  ,  71,   0,   0,
3d3dd 20 31 31 30 2c 20 20 32 37 2c 20 20 33 37 2c 20   110,  27,  37, 
3d3de 20 36 39 2c 20 20 37 39 2c 20 20 20 30 2c 0a 20   69,  79,   0,. 
3d3df 20 20 20 20 20 33 35 2c 20 20 36 34 2c 20 20 20       35,  64,   
3d3e0 30 2c 20 20 20 30 2c 0a 20 20 7d 3b 0a 20 20 73  0,   0,.  };.  s
3d3e1 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
3d3e2 67 6e 65 64 20 63 68 61 72 20 61 4c 65 6e 5b 31  gned char aLen[1
3d3e3 32 31 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 37  21] = {.       7
3d3e4 2c 20 20 20 37 2c 20 20 20 35 2c 20 20 20 34 2c  ,   7,   5,   4,
3d3e5 20 20 20 36 2c 20 20 20 34 2c 20 20 20 35 2c 20     6,   4,   5, 
3d3e6 20 20 33 2c 20 20 20 36 2c 20 20 20 37 2c 20 20    3,   6,   7,  
3d3e7 20 33 2c 20 20 20 36 2c 20 20 20 36 2c 0a 20 20   3,   6,   6,.  
3d3e8 20 20 20 20 20 37 2c 20 20 20 37 2c 20 20 20 33       7,   7,   3
3d3e9 2c 20 20 20 38 2c 20 20 20 32 2c 20 20 20 36 2c  ,   8,   2,   6,
3d3ea 20 20 20 35 2c 20 20 20 34 2c 20 20 20 34 2c 20     5,   4,   4, 
3d3eb 20 20 33 2c 20 20 31 30 2c 20 20 20 34 2c 20 20    3,  10,   4,  
3d3ec 20 36 2c 0a 20 20 20 20 20 20 31 31 2c 20 20 20   6,.      11,   
3d3ed 36 2c 20 20 20 32 2c 20 20 20 37 2c 20 20 20 35  6,   2,   7,   5
3d3ee 2c 20 20 20 35 2c 20 20 20 39 2c 20 20 20 36 2c  ,   5,   9,   6,
3d3ef 20 20 20 39 2c 20 20 20 39 2c 20 20 20 37 2c 20     9,   9,   7, 
3d3f0 20 31 30 2c 20 20 31 30 2c 0a 20 20 20 20 20 20   10,  10,.      
3d3f1 20 34 2c 20 20 20 36 2c 20 20 20 32 2c 20 20 20   4,   6,   2,   
3d3f2 33 2c 20 20 20 39 2c 20 20 20 34 2c 20 20 20 32  3,   9,   4,   2
3d3f3 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 36 2c  ,   6,   5,   6,
3d3f4 20 20 20 36 2c 20 20 20 35 2c 20 20 20 36 2c 0a     6,   5,   6,.
3d3f5 20 20 20 20 20 20 20 35 2c 20 20 20 35 2c 20 20         5,   5,  
3d3f6 20 37 2c 20 20 20 37 2c 20 20 20 37 2c 20 20 20   7,   7,   7,   
3d3f7 33 2c 20 20 20 32 2c 20 20 20 34 2c 20 20 20 34  3,   2,   4,   4
3d3f8 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20 36 2c  ,   7,   3,   6,
3d3f9 20 20 20 34 2c 0a 20 20 20 20 20 20 20 37 2c 20     4,.       7, 
3d3fa 20 20 36 2c 20 20 31 32 2c 20 20 20 36 2c 20 20    6,  12,   6,  
3d3fb 20 39 2c 20 20 20 34 2c 20 20 20 36 2c 20 20 20   9,   4,   6,   
3d3fc 35 2c 20 20 20 34 2c 20 20 20 37 2c 20 20 20 36  5,   4,   7,   6
3d3fd 2c 20 20 20 35 2c 20 20 20 36 2c 0a 20 20 20 20  ,   5,   6,.    
3d3fe 20 20 20 37 2c 20 20 20 35 2c 20 20 20 34 2c 20     7,   5,   4, 
3d3ff 20 20 35 2c 20 20 20 36 2c 20 20 20 35 2c 20 20    5,   6,   5,  
3d400 20 37 2c 20 20 20 33 2c 20 20 20 37 2c 20 20 31   7,   3,   7,  1
3d401 33 2c 20 20 20 32 2c 20 20 20 32 2c 20 20 20 34  3,   2,   2,   4
3d402 2c 0a 20 20 20 20 20 20 20 36 2c 20 20 20 36 2c  ,.       6,   6,
3d403 20 20 20 38 2c 20 20 20 35 2c 20 20 31 37 2c 20     8,   5,  17, 
3d404 20 31 32 2c 20 20 20 37 2c 20 20 20 38 2c 20 20   12,   7,   8,  
3d405 20 38 2c 20 20 20 32 2c 20 20 20 34 2c 20 20 20   8,   2,   4,   
3d406 34 2c 20 20 20 34 2c 0a 20 20 20 20 20 20 20 34  4,   4,.       4
3d407 2c 20 20 20 34 2c 20 20 20 32 2c 20 20 20 32 2c  ,   4,   2,   2,
3d408 20 20 20 36 2c 20 20 20 35 2c 20 20 20 38 2c 20     6,   5,   8, 
3d409 20 20 35 2c 20 20 20 35 2c 20 20 20 38 2c 20 20    5,   5,   8,  
3d40a 20 33 2c 20 20 20 35 2c 20 20 20 35 2c 0a 20 20   3,   5,   5,.  
3d40b 20 20 20 20 20 36 2c 20 20 20 34 2c 20 20 20 39       6,   4,   9
3d40c 2c 20 20 20 33 2c 0a 20 20 7d 3b 0a 20 20 73 74  ,   3,.  };.  st
3d40d 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
3d40e 6e 65 64 20 73 68 6f 72 74 20 69 6e 74 20 61 4f  ned short int aO
3d40f 66 66 73 65 74 5b 31 32 31 5d 20 3d 20 7b 0a 20  ffset[121] = {. 
3d410 20 20 20 20 20 20 30 2c 20 20 20 32 2c 20 20 20        0,   2,   
3d411 32 2c 20 20 20 38 2c 20 20 20 39 2c 20 20 31 34  2,   8,   9,  14
3d412 2c 20 20 31 36 2c 20 20 32 30 2c 20 20 32 33 2c  ,  16,  20,  23,
3d413 20 20 32 35 2c 20 20 32 35 2c 20 20 32 39 2c 20    25,  25,  29, 
3d414 20 33 33 2c 0a 20 20 20 20 20 20 33 36 2c 20 20   33,.      36,  
3d415 34 31 2c 20 20 34 36 2c 20 20 34 38 2c 20 20 35  41,  46,  48,  5
3d416 33 2c 20 20 35 34 2c 20 20 35 39 2c 20 20 36 32  3,  54,  59,  62
3d417 2c 20 20 36 35 2c 20 20 36 37 2c 20 20 36 39 2c  ,  65,  67,  69,
3d418 20 20 37 38 2c 20 20 38 31 2c 0a 20 20 20 20 20    78,  81,.     
3d419 20 38 36 2c 20 20 39 31 2c 20 20 39 35 2c 20 20   86,  91,  95,  
3d41a 39 36 2c 20 31 30 31 2c 20 31 30 35 2c 20 31 30  96, 101, 105, 10
3d41b 39 2c 20 31 31 37 2c 20 31 32 32 2c 20 31 32 38  9, 117, 122, 128
3d41c 2c 20 31 33 36 2c 20 31 34 32 2c 20 31 35 32 2c  , 136, 142, 152,
3d41d 0a 20 20 20 20 20 31 35 39 2c 20 31 36 32 2c 20  .     159, 162, 
3d41e 31 36 32 2c 20 31 36 35 2c 20 31 36 37 2c 20 31  162, 165, 167, 1
3d41f 36 37 2c 20 31 37 31 2c 20 31 37 36 2c 20 31 37  67, 171, 176, 17
3d420 39 2c 20 31 38 34 2c 20 31 38 39 2c 20 31 39 34  9, 184, 189, 194
3d421 2c 20 31 39 37 2c 0a 20 20 20 20 20 32 30 33 2c  , 197,.     203,
3d422 20 32 30 36 2c 20 32 31 30 2c 20 32 31 37 2c 20   206, 210, 217, 
3d423 32 32 33 2c 20 32 32 33 2c 20 32 32 33 2c 20 32  223, 223, 223, 2
3d424 32 36 2c 20 32 32 39 2c 20 32 33 33 2c 20 32 33  26, 229, 233, 23
3d425 34 2c 20 32 33 38 2c 20 32 34 34 2c 0a 20 20 20  4, 238, 244,.   
3d426 20 20 32 34 38 2c 20 32 35 35 2c 20 32 36 31 2c    248, 255, 261,
3d427 20 32 37 33 2c 20 32 37 39 2c 20 32 38 38 2c 20   273, 279, 288, 
3d428 32 39 30 2c 20 32 39 36 2c 20 33 30 31 2c 20 33  290, 296, 301, 3
3d429 30 33 2c 20 33 31 30 2c 20 33 31 35 2c 20 33 32  03, 310, 315, 32
3d42a 30 2c 0a 20 20 20 20 20 33 32 36 2c 20 33 33 32  0,.     326, 332
3d42b 2c 20 33 33 37 2c 20 33 34 31 2c 20 33 34 34 2c  , 337, 341, 344,
3d42c 20 33 35 30 2c 20 33 35 34 2c 20 33 36 31 2c 20   350, 354, 361, 
3d42d 33 36 33 2c 20 33 37 30 2c 20 33 37 32 2c 20 33  363, 370, 372, 3
3d42e 37 34 2c 20 33 38 33 2c 0a 20 20 20 20 20 33 38  74, 383,.     38
3d42f 37 2c 20 33 39 33 2c 20 33 39 39 2c 20 34 30 37  7, 393, 399, 407
3d430 2c 20 34 31 32 2c 20 34 31 32 2c 20 34 32 38 2c  , 412, 412, 428,
3d431 20 34 33 35 2c 20 34 34 32 2c 20 34 34 33 2c 20   435, 442, 443, 
3d432 34 35 30 2c 20 34 35 34 2c 20 34 35 38 2c 0a 20  450, 454, 458,. 
3d433 20 20 20 20 34 36 32 2c 20 34 36 36 2c 20 34 36      462, 466, 46
3d434 39 2c 20 34 37 31 2c 20 34 37 33 2c 20 34 37 39  9, 471, 473, 479
3d435 2c 20 34 38 33 2c 20 34 39 31 2c 20 34 39 35 2c  , 483, 491, 495,
3d436 20 35 30 30 2c 20 35 30 38 2c 20 35 31 31 2c 20   500, 508, 511, 
3d437 35 31 36 2c 0a 20 20 20 20 20 35 32 31 2c 20 35  516,.     521, 5
3d438 32 37 2c 20 35 33 31 2c 20 35 33 36 2c 0a 20 20  27, 531, 536,.  
3d439 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  };.  static cons
3d43a 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
3d43b 61 43 6f 64 65 5b 31 32 31 5d 20 3d 20 7b 0a 20  aCode[121] = {. 
3d43c 20 20 20 54 4b 5f 52 45 49 4e 44 45 58 2c 20 20     TK_REINDEX,  
3d43d 20 20 54 4b 5f 49 4e 44 45 58 45 44 2c 20 20 20    TK_INDEXED,   
3d43e 20 54 4b 5f 49 4e 44 45 58 2c 20 20 20 20 20 20   TK_INDEX,      
3d43f 54 4b 5f 44 45 53 43 2c 20 20 20 20 20 20 20 54  TK_DESC,       T
3d440 4b 5f 45 53 43 41 50 45 2c 20 20 20 20 20 0a 20  K_ESCAPE,     . 
3d441 20 20 20 54 4b 5f 45 41 43 48 2c 20 20 20 20 20     TK_EACH,     
3d442 20 20 54 4b 5f 43 48 45 43 4b 2c 20 20 20 20 20    TK_CHECK,     
3d443 20 54 4b 5f 4b 45 59 2c 20 20 20 20 20 20 20 20   TK_KEY,        
3d444 54 4b 5f 42 45 46 4f 52 45 2c 20 20 20 20 20 54  TK_BEFORE,     T
3d445 4b 5f 46 4f 52 45 49 47 4e 2c 20 20 20 20 0a 20  K_FOREIGN,    . 
3d446 20 20 20 54 4b 5f 46 4f 52 2c 20 20 20 20 20 20     TK_FOR,      
3d447 20 20 54 4b 5f 49 47 4e 4f 52 45 2c 20 20 20 20    TK_IGNORE,    
3d448 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20   TK_LIKE_KW,    
3d449 54 4b 5f 45 58 50 4c 41 49 4e 2c 20 20 20 20 54  TK_EXPLAIN,    T
3d44a 4b 5f 49 4e 53 54 45 41 44 2c 20 20 20 20 0a 20  K_INSTEAD,    . 
3d44b 20 20 20 54 4b 5f 41 44 44 2c 20 20 20 20 20 20     TK_ADD,      
3d44c 20 20 54 4b 5f 44 41 54 41 42 41 53 45 2c 20 20    TK_DATABASE,  
3d44d 20 54 4b 5f 41 53 2c 20 20 20 20 20 20 20 20 20   TK_AS,         
3d44e 54 4b 5f 53 45 4c 45 43 54 2c 20 20 20 20 20 54  TK_SELECT,     T
3d44f 4b 5f 54 41 42 4c 45 2c 20 20 20 20 20 20 0a 20  K_TABLE,      . 
3d450 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20     TK_JOIN_KW,  
3d451 20 20 54 4b 5f 54 48 45 4e 2c 20 20 20 20 20 20    TK_THEN,      
3d452 20 54 4b 5f 45 4e 44 2c 20 20 20 20 20 20 20 20   TK_END,        
3d453 54 4b 5f 44 45 46 45 52 52 41 42 4c 45 2c 20 54  TK_DEFERRABLE, T
3d454 4b 5f 45 4c 53 45 2c 20 20 20 20 20 20 20 0a 20  K_ELSE,       . 
3d455 20 20 20 54 4b 5f 45 58 43 45 50 54 2c 20 20 20     TK_EXCEPT,   
3d456 20 20 54 4b 5f 54 52 41 4e 53 41 43 54 49 4f 4e    TK_TRANSACTION
3d457 2c 54 4b 5f 41 43 54 49 4f 4e 2c 20 20 20 20 20  ,TK_ACTION,     
3d458 54 4b 5f 4f 4e 2c 20 20 20 20 20 20 20 20 20 54  TK_ON,         T
3d459 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a 20  K_JOIN_KW,    . 
3d45a 20 20 20 54 4b 5f 41 4c 54 45 52 2c 20 20 20 20     TK_ALTER,    
3d45b 20 20 54 4b 5f 52 41 49 53 45 2c 20 20 20 20 20    TK_RAISE,     
3d45c 20 54 4b 5f 45 58 43 4c 55 53 49 56 45 2c 20 20   TK_EXCLUSIVE,  
3d45d 54 4b 5f 45 58 49 53 54 53 2c 20 20 20 20 20 54  TK_EXISTS,     T
3d45e 4b 5f 53 41 56 45 50 4f 49 4e 54 2c 20 20 0a 20  K_SAVEPOINT,  . 
3d45f 20 20 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 2c     TK_INTERSECT,
3d460 20 20 54 4b 5f 54 52 49 47 47 45 52 2c 20 20 20    TK_TRIGGER,   
3d461 20 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 2c 20   TK_REFERENCES, 
3d462 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 54  TK_CONSTRAINT, T
3d463 4b 5f 49 4e 54 4f 2c 20 20 20 20 20 20 20 0a 20  K_INTO,       . 
3d464 20 20 20 54 4b 5f 4f 46 46 53 45 54 2c 20 20 20     TK_OFFSET,   
3d465 20 20 54 4b 5f 4f 46 2c 20 20 20 20 20 20 20 20    TK_OF,        
3d466 20 54 4b 5f 53 45 54 2c 20 20 20 20 20 20 20 20   TK_SET,        
3d467 54 4b 5f 54 45 4d 50 2c 20 20 20 20 20 20 20 54  TK_TEMP,       T
3d468 4b 5f 54 45 4d 50 2c 20 20 20 20 20 20 20 0a 20  K_TEMP,       . 
3d469 20 20 20 54 4b 5f 4f 52 2c 20 20 20 20 20 20 20     TK_OR,       
3d46a 20 20 54 4b 5f 55 4e 49 51 55 45 2c 20 20 20 20    TK_UNIQUE,    
3d46b 20 54 4b 5f 51 55 45 52 59 2c 20 20 20 20 20 20   TK_QUERY,      
3d46c 54 4b 5f 41 54 54 41 43 48 2c 20 20 20 20 20 54  TK_ATTACH,     T
3d46d 4b 5f 48 41 56 49 4e 47 2c 20 20 20 20 20 0a 20  K_HAVING,     . 
3d46e 20 20 20 54 4b 5f 47 52 4f 55 50 2c 20 20 20 20     TK_GROUP,    
3d46f 20 20 54 4b 5f 55 50 44 41 54 45 2c 20 20 20 20    TK_UPDATE,    
3d470 20 54 4b 5f 42 45 47 49 4e 2c 20 20 20 20 20 20   TK_BEGIN,      
3d471 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54  TK_JOIN_KW,    T
3d472 4b 5f 52 45 4c 45 41 53 45 2c 20 20 20 20 0a 20  K_RELEASE,    . 
3d473 20 20 20 54 4b 5f 42 45 54 57 45 45 4e 2c 20 20     TK_BETWEEN,  
3d474 20 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 20 20    TK_NOTNULL,   
3d475 20 54 4b 5f 4e 4f 54 2c 20 20 20 20 20 20 20 20   TK_NOT,        
3d476 54 4b 5f 4e 4f 2c 20 20 20 20 20 20 20 20 20 54  TK_NO,         T
3d477 4b 5f 4e 55 4c 4c 2c 20 20 20 20 20 20 20 0a 20  K_NULL,       . 
3d478 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20     TK_LIKE_KW,  
3d479 20 20 54 4b 5f 43 41 53 43 41 44 45 2c 20 20 20    TK_CASCADE,   
3d47a 20 54 4b 5f 41 53 43 2c 20 20 20 20 20 20 20 20   TK_ASC,        
3d47b 54 4b 5f 44 45 4c 45 54 45 2c 20 20 20 20 20 54  TK_DELETE,     T
3d47c 4b 5f 43 41 53 45 2c 20 20 20 20 20 20 20 0a 20  K_CASE,       . 
3d47d 20 20 20 54 4b 5f 43 4f 4c 4c 41 54 45 2c 20 20     TK_COLLATE,  
3d47e 20 20 54 4b 5f 43 52 45 41 54 45 2c 20 20 20 20    TK_CREATE,    
3d47f 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20   TK_CTIME_KW,   
3d480 54 4b 5f 44 45 54 41 43 48 2c 20 20 20 20 20 54  TK_DETACH,     T
3d481 4b 5f 49 4d 4d 45 44 49 41 54 45 2c 20 20 0a 20  K_IMMEDIATE,  . 
3d482 20 20 20 54 4b 5f 4a 4f 49 4e 2c 20 20 20 20 20     TK_JOIN,     
3d483 20 20 54 4b 5f 49 4e 53 45 52 54 2c 20 20 20 20    TK_INSERT,    
3d484 20 54 4b 5f 4d 41 54 43 48 2c 20 20 20 20 20 20   TK_MATCH,      
3d485 54 4b 5f 50 4c 41 4e 2c 20 20 20 20 20 20 20 54  TK_PLAN,       T
3d486 4b 5f 41 4e 41 4c 59 5a 45 2c 20 20 20 20 0a 20  K_ANALYZE,    . 
3d487 20 20 20 54 4b 5f 50 52 41 47 4d 41 2c 20 20 20     TK_PRAGMA,   
3d488 20 20 54 4b 5f 41 42 4f 52 54 2c 20 20 20 20 20    TK_ABORT,     
3d489 20 54 4b 5f 56 41 4c 55 45 53 2c 20 20 20 20 20   TK_VALUES,     
3d48a 54 4b 5f 56 49 52 54 55 41 4c 2c 20 20 20 20 54  TK_VIRTUAL,    T
3d48b 4b 5f 4c 49 4d 49 54 2c 20 20 20 20 20 20 0a 20  K_LIMIT,      . 
3d48c 20 20 20 54 4b 5f 57 48 45 4e 2c 20 20 20 20 20     TK_WHEN,     
3d48d 20 20 54 4b 5f 57 48 45 52 45 2c 20 20 20 20 20    TK_WHERE,     
3d48e 20 54 4b 5f 52 45 4e 41 4d 45 2c 20 20 20 20 20   TK_RENAME,     
3d48f 54 4b 5f 41 46 54 45 52 2c 20 20 20 20 20 20 54  TK_AFTER,      T
3d490 4b 5f 52 45 50 4c 41 43 45 2c 20 20 20 20 0a 20  K_REPLACE,    . 
3d491 20 20 20 54 4b 5f 41 4e 44 2c 20 20 20 20 20 20     TK_AND,      
3d492 20 20 54 4b 5f 44 45 46 41 55 4c 54 2c 20 20 20    TK_DEFAULT,   
3d493 20 54 4b 5f 41 55 54 4f 49 4e 43 52 2c 20 20 20   TK_AUTOINCR,   
3d494 54 4b 5f 54 4f 2c 20 20 20 20 20 20 20 20 20 54  TK_TO,         T
3d495 4b 5f 49 4e 2c 20 20 20 20 20 20 20 20 20 0a 20  K_IN,         . 
3d496 20 20 20 54 4b 5f 43 41 53 54 2c 20 20 20 20 20     TK_CAST,     
3d497 20 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 2c 20 20    TK_COLUMNKW,  
3d498 20 54 4b 5f 43 4f 4d 4d 49 54 2c 20 20 20 20 20   TK_COMMIT,     
3d499 54 4b 5f 43 4f 4e 46 4c 49 43 54 2c 20 20 20 54  TK_CONFLICT,   T
3d49a 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a 20  K_JOIN_KW,    . 
3d49b 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20     TK_CTIME_KW, 
3d49c 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20    TK_CTIME_KW,  
3d49d 20 54 4b 5f 50 52 49 4d 41 52 59 2c 20 20 20 20   TK_PRIMARY,    
3d49e 54 4b 5f 44 45 46 45 52 52 45 44 2c 20 20 20 54  TK_DEFERRED,   T
3d49f 4b 5f 44 49 53 54 49 4e 43 54 2c 20 20 20 0a 20  K_DISTINCT,   . 
3d4a0 20 20 20 54 4b 5f 49 53 2c 20 20 20 20 20 20 20     TK_IS,       
3d4a1 20 20 54 4b 5f 44 52 4f 50 2c 20 20 20 20 20 20    TK_DROP,      
3d4a2 20 54 4b 5f 46 41 49 4c 2c 20 20 20 20 20 20 20   TK_FAIL,       
3d4a3 54 4b 5f 46 52 4f 4d 2c 20 20 20 20 20 20 20 54  TK_FROM,       T
3d4a4 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a 20  K_JOIN_KW,    . 
3d4a5 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20     TK_LIKE_KW,  
3d4a6 20 20 54 4b 5f 42 59 2c 20 20 20 20 20 20 20 20    TK_BY,        
3d4a7 20 54 4b 5f 49 46 2c 20 20 20 20 20 20 20 20 20   TK_IF,         
3d4a8 54 4b 5f 49 53 4e 55 4c 4c 2c 20 20 20 20 20 54  TK_ISNULL,     T
3d4a9 4b 5f 4f 52 44 45 52 2c 20 20 20 20 20 20 0a 20  K_ORDER,      . 
3d4aa 20 20 20 54 4b 5f 52 45 53 54 52 49 43 54 2c 20     TK_RESTRICT, 
3d4ab 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20    TK_JOIN_KW,   
3d4ac 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20   TK_JOIN_KW,    
3d4ad 54 4b 5f 52 4f 4c 4c 42 41 43 4b 2c 20 20 20 54  TK_ROLLBACK,   T
3d4ae 4b 5f 52 4f 57 2c 20 20 20 20 20 20 20 20 0a 20  K_ROW,        . 
3d4af 20 20 20 54 4b 5f 55 4e 49 4f 4e 2c 20 20 20 20     TK_UNION,    
3d4b0 20 20 54 4b 5f 55 53 49 4e 47 2c 20 20 20 20 20    TK_USING,     
3d4b1 20 54 4b 5f 56 41 43 55 55 4d 2c 20 20 20 20 20   TK_VACUUM,     
3d4b2 54 4b 5f 56 49 45 57 2c 20 20 20 20 20 20 20 54  TK_VIEW,       T
3d4b3 4b 5f 49 4e 49 54 49 41 4c 4c 59 2c 20 20 0a 20  K_INITIALLY,  . 
3d4b4 20 20 20 54 4b 5f 41 4c 4c 2c 20 20 20 20 20 20     TK_ALL,      
3d4b5 20 20 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 68 2c    .  };.  int h,
3d4b6 20 69 3b 0a 20 20 69 66 28 20 6e 3c 32 20 29 20   i;.  if( n<2 ) 
3d4b7 72 65 74 75 72 6e 20 54 4b 5f 49 44 3b 0a 20 20  return TK_ID;.  
3d4b8 68 20 3d 20 28 28 63 68 61 72 4d 61 70 28 7a 5b  h = ((charMap(z[
3d4b9 30 5d 29 2a 34 29 20 5e 0a 20 20 20 20 20 20 28  0])*4) ^.      (
3d4ba 63 68 61 72 4d 61 70 28 7a 5b 6e 2d 31 5d 29 2a  charMap(z[n-1])*
3d4bb 33 29 20 5e 0a 20 20 20 20 20 20 6e 29 20 25 20  3) ^.      n) % 
3d4bc 31 32 37 3b 0a 20 20 66 6f 72 28 69 3d 28 28 69  127;.  for(i=((i
3d4bd 6e 74 29 61 48 61 73 68 5b 68 5d 29 2d 31 3b 20  nt)aHash[h])-1; 
3d4be 69 3e 3d 30 3b 20 69 3d 28 28 69 6e 74 29 61 4e  i>=0; i=((int)aN
3d4bf 65 78 74 5b 69 5d 29 2d 31 29 7b 0a 20 20 20 20  ext[i])-1){.    
3d4c0 69 66 28 20 61 4c 65 6e 5b 69 5d 3d 3d 6e 20 26  if( aLen[i]==n &
3d4c1 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  & sqlite3StrNICm
3d4c2 70 28 26 7a 54 65 78 74 5b 61 4f 66 66 73 65 74  p(&zText[aOffset
3d4c3 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30 20 29 7b 0a  [i]],z,n)==0 ){.
3d4c4 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3d4c5 69 3d 3d 30 20 29 3b 20 2f 2a 20 52 45 49 4e 44  i==0 ); /* REIND
3d4c6 45 58 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  EX */.      test
3d4c7 63 61 73 65 28 20 69 3d 3d 31 20 29 3b 20 2f 2a  case( i==1 ); /*
3d4c8 20 49 4e 44 45 58 45 44 20 2a 2f 0a 20 20 20 20   INDEXED */.    
3d4c9 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32    testcase( i==2
3d4ca 20 29 3b 20 2f 2a 20 49 4e 44 45 58 20 2a 2f 0a   ); /* INDEX */.
3d4cb 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3d4cc 69 3d 3d 33 20 29 3b 20 2f 2a 20 44 45 53 43 20  i==3 ); /* DESC 
3d4cd 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3d4ce 65 28 20 69 3d 3d 34 20 29 3b 20 2f 2a 20 45 53  e( i==4 ); /* ES
3d4cf 43 41 50 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  CAPE */.      te
3d4d0 73 74 63 61 73 65 28 20 69 3d 3d 35 20 29 3b 20  stcase( i==5 ); 
3d4d1 2f 2a 20 45 41 43 48 20 2a 2f 0a 20 20 20 20 20  /* EACH */.     
3d4d2 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 20   testcase( i==6 
3d4d3 29 3b 20 2f 2a 20 43 48 45 43 4b 20 2a 2f 0a 20  ); /* CHECK */. 
3d4d4 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3d4d5 3d 3d 37 20 29 3b 20 2f 2a 20 4b 45 59 20 2a 2f  ==7 ); /* KEY */
3d4d6 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3d4d7 20 69 3d 3d 38 20 29 3b 20 2f 2a 20 42 45 46 4f   i==8 ); /* BEFO
3d4d8 52 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  RE */.      test
3d4d9 63 61 73 65 28 20 69 3d 3d 39 20 29 3b 20 2f 2a  case( i==9 ); /*
3d4da 20 46 4f 52 45 49 47 4e 20 2a 2f 0a 20 20 20 20   FOREIGN */.    
3d4db 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
3d4dc 30 20 29 3b 20 2f 2a 20 46 4f 52 20 2a 2f 0a 20  0 ); /* FOR */. 
3d4dd 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3d4de 3d 3d 31 31 20 29 3b 20 2f 2a 20 49 47 4e 4f 52  ==11 ); /* IGNOR
3d4df 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
3d4e0 61 73 65 28 20 69 3d 3d 31 32 20 29 3b 20 2f 2a  ase( i==12 ); /*
3d4e1 20 52 45 47 45 58 50 20 2a 2f 0a 20 20 20 20 20   REGEXP */.     
3d4e2 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 33   testcase( i==13
3d4e3 20 29 3b 20 2f 2a 20 45 58 50 4c 41 49 4e 20 2a   ); /* EXPLAIN *
3d4e4 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
3d4e5 28 20 69 3d 3d 31 34 20 29 3b 20 2f 2a 20 49 4e  ( i==14 ); /* IN
3d4e6 53 54 45 41 44 20 2a 2f 0a 20 20 20 20 20 20 74  STEAD */.      t
3d4e7 65 73 74 63 61 73 65 28 20 69 3d 3d 31 35 20 29  estcase( i==15 )
3d4e8 3b 20 2f 2a 20 41 44 44 20 2a 2f 0a 20 20 20 20  ; /* ADD */.    
3d4e9 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
3d4ea 36 20 29 3b 20 2f 2a 20 44 41 54 41 42 41 53 45  6 ); /* DATABASE
3d4eb 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3d4ec 73 65 28 20 69 3d 3d 31 37 20 29 3b 20 2f 2a 20  se( i==17 ); /* 
3d4ed 41 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  AS */.      test
3d4ee 63 61 73 65 28 20 69 3d 3d 31 38 20 29 3b 20 2f  case( i==18 ); /
3d4ef 2a 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20 20  * SELECT */.    
3d4f0 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
3d4f1 39 20 29 3b 20 2f 2a 20 54 41 42 4c 45 20 2a 2f  9 ); /* TABLE */
3d4f2 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3d4f3 20 69 3d 3d 32 30 20 29 3b 20 2f 2a 20 4c 45 46   i==20 ); /* LEF
3d4f4 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
3d4f5 61 73 65 28 20 69 3d 3d 32 31 20 29 3b 20 2f 2a  ase( i==21 ); /*
3d4f6 20 54 48 45 4e 20 2a 2f 0a 20 20 20 20 20 20 74   THEN */.      t
3d4f7 65 73 74 63 61 73 65 28 20 69 3d 3d 32 32 20 29  estcase( i==22 )
3d4f8 3b 20 2f 2a 20 45 4e 44 20 2a 2f 0a 20 20 20 20  ; /* END */.    
3d4f9 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32    testcase( i==2
3d4fa 33 20 29 3b 20 2f 2a 20 44 45 46 45 52 52 41 42  3 ); /* DEFERRAB
3d4fb 4c 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  LE */.      test
3d4fc 63 61 73 65 28 20 69 3d 3d 32 34 20 29 3b 20 2f  case( i==24 ); /
3d4fd 2a 20 45 4c 53 45 20 2a 2f 0a 20 20 20 20 20 20  * ELSE */.      
3d4fe 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 35 20  testcase( i==25 
3d4ff 29 3b 20 2f 2a 20 45 58 43 45 50 54 20 2a 2f 0a  ); /* EXCEPT */.
3d500 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3d501 69 3d 3d 32 36 20 29 3b 20 2f 2a 20 54 52 41 4e  i==26 ); /* TRAN
3d502 53 41 43 54 49 4f 4e 20 2a 2f 0a 20 20 20 20 20  SACTION */.     
3d503 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 37   testcase( i==27
3d504 20 29 3b 20 2f 2a 20 41 43 54 49 4f 4e 20 2a 2f   ); /* ACTION */
3d505 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3d506 20 69 3d 3d 32 38 20 29 3b 20 2f 2a 20 4f 4e 20   i==28 ); /* ON 
3d507 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3d508 65 28 20 69 3d 3d 32 39 20 29 3b 20 2f 2a 20 4e  e( i==29 ); /* N
3d509 41 54 55 52 41 4c 20 2a 2f 0a 20 20 20 20 20 20  ATURAL */.      
3d50a 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 30 20  testcase( i==30 
3d50b 29 3b 20 2f 2a 20 41 4c 54 45 52 20 2a 2f 0a 20  ); /* ALTER */. 
3d50c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3d50d 3d 3d 33 31 20 29 3b 20 2f 2a 20 52 41 49 53 45  ==31 ); /* RAISE
3d50e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3d50f 73 65 28 20 69 3d 3d 33 32 20 29 3b 20 2f 2a 20  se( i==32 ); /* 
3d510 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 20  EXCLUSIVE */.   
3d511 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3d512 33 33 20 29 3b 20 2f 2a 20 45 58 49 53 54 53 20  33 ); /* EXISTS 
3d513 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3d514 65 28 20 69 3d 3d 33 34 20 29 3b 20 2f 2a 20 53  e( i==34 ); /* S
3d515 41 56 45 50 4f 49 4e 54 20 2a 2f 0a 20 20 20 20  AVEPOINT */.    
3d516 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33    testcase( i==3
3d517 35 20 29 3b 20 2f 2a 20 49 4e 54 45 52 53 45 43  5 ); /* INTERSEC
3d518 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
3d519 61 73 65 28 20 69 3d 3d 33 36 20 29 3b 20 2f 2a  ase( i==36 ); /*
3d51a 20 54 52 49 47 47 45 52 20 2a 2f 0a 20 20 20 20   TRIGGER */.    
3d51b 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33    testcase( i==3
3d51c 37 20 29 3b 20 2f 2a 20 52 45 46 45 52 45 4e 43  7 ); /* REFERENC
3d51d 45 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ES */.      test
3d51e 63 61 73 65 28 20 69 3d 3d 33 38 20 29 3b 20 2f  case( i==38 ); /
3d51f 2a 20 43 4f 4e 53 54 52 41 49 4e 54 20 2a 2f 0a  * CONSTRAINT */.
3d520 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3d521 69 3d 3d 33 39 20 29 3b 20 2f 2a 20 49 4e 54 4f  i==39 ); /* INTO
3d522 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3d523 73 65 28 20 69 3d 3d 34 30 20 29 3b 20 2f 2a 20  se( i==40 ); /* 
3d524 4f 46 46 53 45 54 20 2a 2f 0a 20 20 20 20 20 20  OFFSET */.      
3d525 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 31 20  testcase( i==41 
3d526 29 3b 20 2f 2a 20 4f 46 20 2a 2f 0a 20 20 20 20  ); /* OF */.    
3d527 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34    testcase( i==4
3d528 32 20 29 3b 20 2f 2a 20 53 45 54 20 2a 2f 0a 20  2 ); /* SET */. 
3d529 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3d52a 3d 3d 34 33 20 29 3b 20 2f 2a 20 54 45 4d 50 4f  ==43 ); /* TEMPO
3d52b 52 41 52 59 20 2a 2f 0a 20 20 20 20 20 20 74 65  RARY */.      te
3d52c 73 74 63 61 73 65 28 20 69 3d 3d 34 34 20 29 3b  stcase( i==44 );
3d52d 20 2f 2a 20 54 45 4d 50 20 2a 2f 0a 20 20 20 20   /* TEMP */.    
3d52e 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34    testcase( i==4
3d52f 35 20 29 3b 20 2f 2a 20 4f 52 20 2a 2f 0a 20 20  5 ); /* OR */.  
3d530 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3d531 3d 34 36 20 29 3b 20 2f 2a 20 55 4e 49 51 55 45  =46 ); /* UNIQUE
3d532 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3d533 73 65 28 20 69 3d 3d 34 37 20 29 3b 20 2f 2a 20  se( i==47 ); /* 
3d534 51 55 45 52 59 20 2a 2f 0a 20 20 20 20 20 20 74  QUERY */.      t
3d535 65 73 74 63 61 73 65 28 20 69 3d 3d 34 38 20 29  estcase( i==48 )
3d536 3b 20 2f 2a 20 41 54 54 41 43 48 20 2a 2f 0a 20  ; /* ATTACH */. 
3d537 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3d538 3d 3d 34 39 20 29 3b 20 2f 2a 20 48 41 56 49 4e  ==49 ); /* HAVIN
3d539 47 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  G */.      testc
3d53a 61 73 65 28 20 69 3d 3d 35 30 20 29 3b 20 2f 2a  ase( i==50 ); /*
3d53b 20 47 52 4f 55 50 20 2a 2f 0a 20 20 20 20 20 20   GROUP */.      
3d53c 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 31 20  testcase( i==51 
3d53d 29 3b 20 2f 2a 20 55 50 44 41 54 45 20 2a 2f 0a  ); /* UPDATE */.
3d53e 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3d53f 69 3d 3d 35 32 20 29 3b 20 2f 2a 20 42 45 47 49  i==52 ); /* BEGI
3d540 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  N */.      testc
3d541 61 73 65 28 20 69 3d 3d 35 33 20 29 3b 20 2f 2a  ase( i==53 ); /*
3d542 20 49 4e 4e 45 52 20 2a 2f 0a 20 20 20 20 20 20   INNER */.      
3d543 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 34 20  testcase( i==54 
3d544 29 3b 20 2f 2a 20 52 45 4c 45 41 53 45 20 2a 2f  ); /* RELEASE */
3d545 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3d546 20 69 3d 3d 35 35 20 29 3b 20 2f 2a 20 42 45 54   i==55 ); /* BET
3d547 57 45 45 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65  WEEN */.      te
3d548 73 74 63 61 73 65 28 20 69 3d 3d 35 36 20 29 3b  stcase( i==56 );
3d549 20 2f 2a 20 4e 4f 54 4e 55 4c 4c 20 2a 2f 0a 20   /* NOTNULL */. 
3d54a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3d54b 3d 3d 35 37 20 29 3b 20 2f 2a 20 4e 4f 54 20 2a  ==57 ); /* NOT *
3d54c 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
3d54d 28 20 69 3d 3d 35 38 20 29 3b 20 2f 2a 20 4e 4f  ( i==58 ); /* NO
3d54e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3d54f 73 65 28 20 69 3d 3d 35 39 20 29 3b 20 2f 2a 20  se( i==59 ); /* 
3d550 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65  NULL */.      te
3d551 73 74 63 61 73 65 28 20 69 3d 3d 36 30 20 29 3b  stcase( i==60 );
3d552 20 2f 2a 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20   /* LIKE */.    
3d553 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36    testcase( i==6
3d554 31 20 29 3b 20 2f 2a 20 43 41 53 43 41 44 45 20  1 ); /* CASCADE 
3d555 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3d556 65 28 20 69 3d 3d 36 32 20 29 3b 20 2f 2a 20 41  e( i==62 ); /* A
3d557 53 43 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  SC */.      test
3d558 63 61 73 65 28 20 69 3d 3d 36 33 20 29 3b 20 2f  case( i==63 ); /
3d559 2a 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 20 20  * DELETE */.    
3d55a 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36    testcase( i==6
3d55b 34 20 29 3b 20 2f 2a 20 43 41 53 45 20 2a 2f 0a  4 ); /* CASE */.
3d55c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3d55d 69 3d 3d 36 35 20 29 3b 20 2f 2a 20 43 4f 4c 4c  i==65 ); /* COLL
3d55e 41 54 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ATE */.      tes
3d55f 74 63 61 73 65 28 20 69 3d 3d 36 36 20 29 3b 20  tcase( i==66 ); 
3d560 2f 2a 20 43 52 45 41 54 45 20 2a 2f 0a 20 20 20  /* CREATE */.   
3d561 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3d562 36 37 20 29 3b 20 2f 2a 20 43 55 52 52 45 4e 54  67 ); /* CURRENT
3d563 5f 44 41 54 45 20 2a 2f 0a 20 20 20 20 20 20 74  _DATE */.      t
3d564 65 73 74 63 61 73 65 28 20 69 3d 3d 36 38 20 29  estcase( i==68 )
3d565 3b 20 2f 2a 20 44 45 54 41 43 48 20 2a 2f 0a 20  ; /* DETACH */. 
3d566 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3d567 3d 3d 36 39 20 29 3b 20 2f 2a 20 49 4d 4d 45 44  ==69 ); /* IMMED
3d568 49 41 54 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  IATE */.      te
3d569 73 74 63 61 73 65 28 20 69 3d 3d 37 30 20 29 3b  stcase( i==70 );
3d56a 20 2f 2a 20 4a 4f 49 4e 20 2a 2f 0a 20 20 20 20   /* JOIN */.    
3d56b 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37    testcase( i==7
3d56c 31 20 29 3b 20 2f 2a 20 49 4e 53 45 52 54 20 2a  1 ); /* INSERT *
3d56d 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
3d56e 28 20 69 3d 3d 37 32 20 29 3b 20 2f 2a 20 4d 41  ( i==72 ); /* MA
3d56f 54 43 48 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  TCH */.      tes
3d570 74 63 61 73 65 28 20 69 3d 3d 37 33 20 29 3b 20  tcase( i==73 ); 
3d571 2f 2a 20 50 4c 41 4e 20 2a 2f 0a 20 20 20 20 20  /* PLAN */.     
3d572 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 34   testcase( i==74
3d573 20 29 3b 20 2f 2a 20 41 4e 41 4c 59 5a 45 20 2a   ); /* ANALYZE *
3d574 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
3d575 28 20 69 3d 3d 37 35 20 29 3b 20 2f 2a 20 50 52  ( i==75 ); /* PR
3d576 41 47 4d 41 20 2a 2f 0a 20 20 20 20 20 20 74 65  AGMA */.      te
3d577 73 74 63 61 73 65 28 20 69 3d 3d 37 36 20 29 3b  stcase( i==76 );
3d578 20 2f 2a 20 41 42 4f 52 54 20 2a 2f 0a 20 20 20   /* ABORT */.   
3d579 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3d57a 37 37 20 29 3b 20 2f 2a 20 56 41 4c 55 45 53 20  77 ); /* VALUES 
3d57b 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3d57c 65 28 20 69 3d 3d 37 38 20 29 3b 20 2f 2a 20 56  e( i==78 ); /* V
3d57d 49 52 54 55 41 4c 20 2a 2f 0a 20 20 20 20 20 20  IRTUAL */.      
3d57e 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 39 20  testcase( i==79 
3d57f 29 3b 20 2f 2a 20 4c 49 4d 49 54 20 2a 2f 0a 20  ); /* LIMIT */. 
3d580 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3d581 3d 3d 38 30 20 29 3b 20 2f 2a 20 57 48 45 4e 20  ==80 ); /* WHEN 
3d582 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3d583 65 28 20 69 3d 3d 38 31 20 29 3b 20 2f 2a 20 57  e( i==81 ); /* W
3d584 48 45 52 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  HERE */.      te
3d585 73 74 63 61 73 65 28 20 69 3d 3d 38 32 20 29 3b  stcase( i==82 );
3d586 20 2f 2a 20 52 45 4e 41 4d 45 20 2a 2f 0a 20 20   /* RENAME */.  
3d587 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3d588 3d 38 33 20 29 3b 20 2f 2a 20 41 46 54 45 52 20  =83 ); /* AFTER 
3d589 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3d58a 65 28 20 69 3d 3d 38 34 20 29 3b 20 2f 2a 20 52  e( i==84 ); /* R
3d58b 45 50 4c 41 43 45 20 2a 2f 0a 20 20 20 20 20 20  EPLACE */.      
3d58c 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 35 20  testcase( i==85 
3d58d 29 3b 20 2f 2a 20 41 4e 44 20 2a 2f 0a 20 20 20  ); /* AND */.   
3d58e 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3d58f 38 36 20 29 3b 20 2f 2a 20 44 45 46 41 55 4c 54  86 ); /* DEFAULT
3d590 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3d591 73 65 28 20 69 3d 3d 38 37 20 29 3b 20 2f 2a 20  se( i==87 ); /* 
3d592 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f  AUTOINCREMENT */
3d593 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3d594 20 69 3d 3d 38 38 20 29 3b 20 2f 2a 20 54 4f 20   i==88 ); /* TO 
3d595 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3d596 65 28 20 69 3d 3d 38 39 20 29 3b 20 2f 2a 20 49  e( i==89 ); /* I
3d597 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  N */.      testc
3d598 61 73 65 28 20 69 3d 3d 39 30 20 29 3b 20 2f 2a  ase( i==90 ); /*
3d599 20 43 41 53 54 20 2a 2f 0a 20 20 20 20 20 20 74   CAST */.      t
3d59a 65 73 74 63 61 73 65 28 20 69 3d 3d 39 31 20 29  estcase( i==91 )
3d59b 3b 20 2f 2a 20 43 4f 4c 55 4d 4e 20 2a 2f 0a 20  ; /* COLUMN */. 
3d59c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3d59d 3d 3d 39 32 20 29 3b 20 2f 2a 20 43 4f 4d 4d 49  ==92 ); /* COMMI
3d59e 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
3d59f 61 73 65 28 20 69 3d 3d 39 33 20 29 3b 20 2f 2a  ase( i==93 ); /*
3d5a0 20 43 4f 4e 46 4c 49 43 54 20 2a 2f 0a 20 20 20   CONFLICT */.   
3d5a1 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3d5a2 39 34 20 29 3b 20 2f 2a 20 43 52 4f 53 53 20 2a  94 ); /* CROSS *
3d5a3 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
3d5a4 28 20 69 3d 3d 39 35 20 29 3b 20 2f 2a 20 43 55  ( i==95 ); /* CU
3d5a5 52 52 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50 20  RRENT_TIMESTAMP 
3d5a6 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3d5a7 65 28 20 69 3d 3d 39 36 20 29 3b 20 2f 2a 20 43  e( i==96 ); /* C
3d5a8 55 52 52 45 4e 54 5f 54 49 4d 45 20 2a 2f 0a 20  URRENT_TIME */. 
3d5a9 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3d5aa 3d 3d 39 37 20 29 3b 20 2f 2a 20 50 52 49 4d 41  ==97 ); /* PRIMA
3d5ab 52 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  RY */.      test
3d5ac 63 61 73 65 28 20 69 3d 3d 39 38 20 29 3b 20 2f  case( i==98 ); /
3d5ad 2a 20 44 45 46 45 52 52 45 44 20 2a 2f 0a 20 20  * DEFERRED */.  
3d5ae 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3d5af 3d 39 39 20 29 3b 20 2f 2a 20 44 49 53 54 49 4e  =99 ); /* DISTIN
3d5b0 43 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  CT */.      test
3d5b1 63 61 73 65 28 20 69 3d 3d 31 30 30 20 29 3b 20  case( i==100 ); 
3d5b2 2f 2a 20 49 53 20 2a 2f 0a 20 20 20 20 20 20 74  /* IS */.      t
3d5b3 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 31 20  estcase( i==101 
3d5b4 29 3b 20 2f 2a 20 44 52 4f 50 20 2a 2f 0a 20 20  ); /* DROP */.  
3d5b5 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3d5b6 3d 31 30 32 20 29 3b 20 2f 2a 20 46 41 49 4c 20  =102 ); /* FAIL 
3d5b7 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3d5b8 65 28 20 69 3d 3d 31 30 33 20 29 3b 20 2f 2a 20  e( i==103 ); /* 
3d5b9 46 52 4f 4d 20 2a 2f 0a 20 20 20 20 20 20 74 65  FROM */.      te
3d5ba 73 74 63 61 73 65 28 20 69 3d 3d 31 30 34 20 29  stcase( i==104 )
3d5bb 3b 20 2f 2a 20 46 55 4c 4c 20 2a 2f 0a 20 20 20  ; /* FULL */.   
3d5bc 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3d5bd 31 30 35 20 29 3b 20 2f 2a 20 47 4c 4f 42 20 2a  105 ); /* GLOB *
3d5be 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
3d5bf 28 20 69 3d 3d 31 30 36 20 29 3b 20 2f 2a 20 42  ( i==106 ); /* B
3d5c0 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  Y */.      testc
3d5c1 61 73 65 28 20 69 3d 3d 31 30 37 20 29 3b 20 2f  ase( i==107 ); /
3d5c2 2a 20 49 46 20 2a 2f 0a 20 20 20 20 20 20 74 65  * IF */.      te
3d5c3 73 74 63 61 73 65 28 20 69 3d 3d 31 30 38 20 29  stcase( i==108 )
3d5c4 3b 20 2f 2a 20 49 53 4e 55 4c 4c 20 2a 2f 0a 20  ; /* ISNULL */. 
3d5c5 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3d5c6 3d 3d 31 30 39 20 29 3b 20 2f 2a 20 4f 52 44 45  ==109 ); /* ORDE
3d5c7 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  R */.      testc
3d5c8 61 73 65 28 20 69 3d 3d 31 31 30 20 29 3b 20 2f  ase( i==110 ); /
3d5c9 2a 20 52 45 53 54 52 49 43 54 20 2a 2f 0a 20 20  * RESTRICT */.  
3d5ca 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3d5cb 3d 31 31 31 20 29 3b 20 2f 2a 20 4f 55 54 45 52  =111 ); /* OUTER
3d5cc 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3d5cd 73 65 28 20 69 3d 3d 31 31 32 20 29 3b 20 2f 2a  se( i==112 ); /*
3d5ce 20 52 49 47 48 54 20 2a 2f 0a 20 20 20 20 20 20   RIGHT */.      
3d5cf 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 33  testcase( i==113
3d5d0 20 29 3b 20 2f 2a 20 52 4f 4c 4c 42 41 43 4b 20   ); /* ROLLBACK 
3d5d1 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3d5d2 65 28 20 69 3d 3d 31 31 34 20 29 3b 20 2f 2a 20  e( i==114 ); /* 
3d5d3 52 4f 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ROW */.      tes
3d5d4 74 63 61 73 65 28 20 69 3d 3d 31 31 35 20 29 3b  tcase( i==115 );
3d5d5 20 2f 2a 20 55 4e 49 4f 4e 20 2a 2f 0a 20 20 20   /* UNION */.   
3d5d6 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3d5d7 31 31 36 20 29 3b 20 2f 2a 20 55 53 49 4e 47 20  116 ); /* USING 
3d5d8 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3d5d9 65 28 20 69 3d 3d 31 31 37 20 29 3b 20 2f 2a 20  e( i==117 ); /* 
3d5da 56 41 43 55 55 4d 20 2a 2f 0a 20 20 20 20 20 20  VACUUM */.      
3d5db 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 38  testcase( i==118
3d5dc 20 29 3b 20 2f 2a 20 56 49 45 57 20 2a 2f 0a 20   ); /* VIEW */. 
3d5dd 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3d5de 3d 3d 31 31 39 20 29 3b 20 2f 2a 20 49 4e 49 54  ==119 ); /* INIT
3d5df 49 41 4c 4c 59 20 2a 2f 0a 20 20 20 20 20 20 74  IALLY */.      t
3d5e0 65 73 74 63 61 73 65 28 20 69 3d 3d 31 32 30 20  estcase( i==120 
3d5e1 29 3b 20 2f 2a 20 41 4c 4c 20 2a 2f 0a 20 20 20  ); /* ALL */.   
3d5e2 20 20 20 72 65 74 75 72 6e 20 61 43 6f 64 65 5b     return aCode[
3d5e3 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  i];.    }.  }.  
3d5e4 72 65 74 75 72 6e 20 54 4b 5f 49 44 3b 0a 7d 0a  return TK_ID;.}.
3d5e5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3d5e6 6e 74 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72  nt sqlite3Keywor
3d5e7 64 43 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69  dCode(const unsi
3d5e8 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e  gned char *z, in
3d5e9 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 6b  t n){.  return k
3d5ea 65 79 77 6f 72 64 43 6f 64 65 28 28 63 68 61 72  eywordCode((char
3d5eb 2a 29 7a 2c 20 6e 29 3b 0a 7d 0a 23 64 65 66 69  *)z, n);.}.#defi
3d5ec 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 4b 45 59 57  ne SQLITE_N_KEYW
3d5ed 4f 52 44 20 31 32 31 0a 0a 2f 2a 2a 2a 2a 2a 2a  ORD 121../******
3d5ee 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
3d5ef 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20 2a 2a  keywordhash.h **
3d5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d5f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d5f2 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
3d5f3 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
3d5f4 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
3d5f5 74 20 6f 66 66 20 69 6e 20 74 6f 6b 65 6e 69 7a  t off in tokeniz
3d5f6 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
3d5f7 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  *******/.../*.**
3d5f8 20 49 66 20 58 20 69 73 20 61 20 63 68 61 72 61   If X is a chara
3d5f9 63 74 65 72 20 74 68 61 74 20 63 61 6e 20 62 65  cter that can be
3d5fa 20 75 73 65 64 20 69 6e 20 61 6e 20 69 64 65 6e   used in an iden
3d5fb 74 69 66 69 65 72 20 74 68 65 6e 0a 2a 2a 20 49  tifier then.** I
3d5fc 64 43 68 61 72 28 58 29 20 77 69 6c 6c 20 62 65  dChar(X) will be
3d5fd 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73   true.  Otherwis
3d5fe 65 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a  e it is false..*
3d5ff 2a 0a 2a 2a 20 46 6f 72 20 41 53 43 49 49 2c 20  *.** For ASCII, 
3d600 61 6e 79 20 63 68 61 72 61 63 74 65 72 20 77 69  any character wi
3d601 74 68 20 74 68 65 20 68 69 67 68 2d 6f 72 64 65  th the high-orde
3d602 72 20 62 69 74 20 73 65 74 20 69 73 0a 2a 2a 20  r bit set is.** 
3d603 61 6c 6c 6f 77 65 64 20 69 6e 20 61 6e 20 69 64  allowed in an id
3d604 65 6e 74 69 66 69 65 72 2e 20 20 46 6f 72 20 37  entifier.  For 7
3d605 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 73 2c  -bit characters,
3d606 20 0a 2a 2a 20 73 71 6c 69 74 65 33 49 73 49 64   .** sqlite3IsId
3d607 43 68 61 72 5b 58 5d 20 6d 75 73 74 20 62 65 20  Char[X] must be 
3d608 31 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 45 42 43  1..**.** For EBC
3d609 44 49 43 2c 20 74 68 65 20 72 75 6c 65 73 20 61  DIC, the rules a
3d60a 72 65 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20  re more complex 
3d60b 62 75 74 20 68 61 76 65 20 74 68 65 20 73 61 6d  but have the sam
3d60c 65 0a 2a 2a 20 65 6e 64 20 72 65 73 75 6c 74 2e  e.** end result.
3d60d 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31  .**.** Ticket #1
3d60e 30 36 36 2e 20 20 74 68 65 20 53 51 4c 20 73 74  066.  the SQL st
3d60f 61 6e 64 61 72 64 20 64 6f 65 73 20 6e 6f 74 20  andard does not 
3d610 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 74 68 65  allow '$' in the
3d611 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20 69 64  .** middle of id
3d612 65 6e 74 66 69 65 72 73 2e 20 20 42 75 74 20 6d  entfiers.  But m
3d613 61 6e 79 20 53 51 4c 20 69 6d 70 6c 65 6d 65 6e  any SQL implemen
3d614 74 61 74 69 6f 6e 73 20 64 6f 2e 20 0a 2a 2a 20  tations do. .** 
3d615 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c 6f  SQLite will allo
3d616 77 20 27 24 27 20 69 6e 20 69 64 65 6e 74 69 66  w '$' in identif
3d617 69 65 72 73 20 66 6f 72 20 63 6f 6d 70 61 74 69  iers for compati
3d618 62 69 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20 74  bility..** But t
3d619 68 65 20 66 65 61 74 75 72 65 20 69 73 20 75 6e  he feature is un
3d61a 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 23  documented..*/.#
3d61b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
3d61c 49 49 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61  II.#define IdCha
3d61d 72 28 43 29 20 20 28 28 73 71 6c 69 74 65 33 43  r(C)  ((sqlite3C
3d61e 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65  typeMap[(unsigne
3d61f 64 20 63 68 61 72 29 43 5d 26 30 78 34 36 29 21  d char)C]&0x46)!
3d620 3d 30 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  =0).#endif.#ifde
3d621 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a  f SQLITE_EBCDIC.
3d622 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
3d623 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65  onst char sqlite
3d624 33 49 73 45 62 63 64 69 63 49 64 43 68 61 72 5b  3IsEbcdicIdChar[
3d625 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20 78  ] = {./* x0 x1 x
3d626 32 20 78 33 20 78 34 20 78 35 20 78 36 20 78 37  2 x3 x4 x5 x6 x7
3d627 20 78 38 20 78 39 20 78 41 20 78 42 20 78 43 20   x8 x9 xA xB xC 
3d628 78 44 20 78 45 20 78 46 20 2a 2f 0a 20 20 20 20  xD xE xF */.    
3d629 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 0, 1, 1, 1, 1
3d62a 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
3d62b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
3d62c 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 30 2c   /* 4x */.    0,
3d62d 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3d62e 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31  1, 1, 1, 1, 0, 1
3d62f 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
3d630 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20 30  * 5x */.    0, 0
3d631 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3d632 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
3d633 30 2c 20 31 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 1, 0, 0,  /* 
3d634 36 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  6x */.    0, 1, 
3d635 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3d636 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 0, 0, 0, 0,
3d637 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78   0, 0, 0,  /* 7x
3d638 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
3d639 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3d63a 31 2c 20 31 2c 20 30 2c 20 30 2c 20 31 2c 20 31  1, 1, 0, 0, 1, 1
3d63b 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 38 78 20 2a  , 1, 0,  /* 8x *
3d63c 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
3d63d 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3d63e 20 31 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20   1, 0, 0, 1, 0, 
3d63f 31 2c 20 30 2c 20 20 2f 2a 20 39 78 20 2a 2f 0a  1, 0,  /* 9x */.
3d640 20 20 20 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20      1, 0, 1, 1, 
3d641 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3d642 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 0, 1, 1, 1,
3d643 20 30 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a 20 20   0,  /* Ax */.  
3d644 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c    0, 0, 0, 0, 0,
3d645 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
3d646 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
3d647 2c 20 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20  ,  /* Bx */.    
3d648 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
3d649 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
3d64a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3d64b 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 20 30 2c   /* Cx */.    0,
3d64c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3d64d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31  1, 1, 1, 1, 0, 1
3d64e 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f  , 1, 1, 1, 1,  /
3d64f 2a 20 44 78 20 2a 2f 0a 20 20 20 20 30 2c 20 30  * Dx */.    0, 0
3d650 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3d651 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20   1, 1, 1, 0, 1, 
3d652 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
3d653 45 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  Ex */.    1, 1, 
3d654 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3d655 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c  , 1, 1, 0, 1, 1,
3d656 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 46 78   1, 1, 0,  /* Fx
3d657 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 49   */.};.#define I
3d658 64 43 68 61 72 28 43 29 20 20 28 28 28 63 3d 43  dChar(C)  (((c=C
3d659 29 3e 3d 30 78 34 32 20 26 26 20 73 71 6c 69 74  )>=0x42 && sqlit
3d65a 65 33 49 73 45 62 63 64 69 63 49 64 43 68 61 72  e3IsEbcdicIdChar
3d65b 5b 63 2d 30 78 34 30 5d 29 29 0a 23 65 6e 64 69  [c-0x40])).#endi
3d65c 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  f.../*.** Return
3d65d 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   the length of t
3d65e 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65  he token that be
3d65f 67 69 6e 73 20 61 74 20 7a 5b 30 5d 2e 20 0a 2a  gins at z[0]. .*
3d660 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65  * Store the toke
3d661 6e 20 74 79 70 65 20 69 6e 20 2a 74 6f 6b 65 6e  n type in *token
3d662 54 79 70 65 20 62 65 66 6f 72 65 20 72 65 74 75  Type before retu
3d663 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rning..*/.SQLITE
3d664 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
3d665 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 63 6f 6e  ite3GetToken(con
3d666 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
3d667 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f 6b 65 6e 54   *z, int *tokenT
3d668 79 70 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 63  ype){.  int i, c
3d669 3b 0a 20 20 73 77 69 74 63 68 28 20 2a 7a 20 29  ;.  switch( *z )
3d66a 7b 0a 20 20 20 20 63 61 73 65 20 27 20 27 3a 20  {.    case ' ': 
3d66b 63 61 73 65 20 27 5c 74 27 3a 20 63 61 73 65 20  case '\t': case 
3d66c 27 5c 6e 27 3a 20 63 61 73 65 20 27 5c 66 27 3a  '\n': case '\f':
3d66d 20 63 61 73 65 20 27 5c 72 27 3a 20 7b 0a 20 20   case '\r': {.  
3d66e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b      testcase( z[
3d66f 30 5d 3d 3d 27 20 27 20 29 3b 0a 20 20 20 20 20  0]==' ' );.     
3d670 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d   testcase( z[0]=
3d671 3d 27 5c 74 27 20 29 3b 0a 20 20 20 20 20 20 74  ='\t' );.      t
3d672 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
3d673 5c 6e 27 20 29 3b 0a 20 20 20 20 20 20 74 65 73  \n' );.      tes
3d674 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 5c 66  tcase( z[0]=='\f
3d675 27 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  ' );.      testc
3d676 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 5c 72 27 20  ase( z[0]=='\r' 
3d677 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31  );.      for(i=1
3d678 3b 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  ; sqlite3Isspace
3d679 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  (z[i]); i++){}. 
3d67a 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
3d67b 3d 20 54 4b 5f 53 50 41 43 45 3b 0a 20 20 20 20  = TK_SPACE;.    
3d67c 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20    return i;.    
3d67d 7d 0a 20 20 20 20 63 61 73 65 20 27 2d 27 3a 20  }.    case '-': 
3d67e 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d  {.      if( z[1]
3d67f 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20  =='-' ){.       
3d680 20 2f 2a 20 49 4d 50 3a 20 52 2d 35 30 34 31 37   /* IMP: R-50417
3d681 2d 32 37 39 37 36 20 2d 2d 20 73 79 6e 74 61 78  -27976 -- syntax
3d682 20 64 69 61 67 72 61 6d 20 66 6f 72 20 63 6f 6d   diagram for com
3d683 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20  ments */.       
3d684 20 66 6f 72 28 69 3d 32 3b 20 28 63 3d 7a 5b 69   for(i=2; (c=z[i
3d685 5d 29 21 3d 30 20 26 26 20 63 21 3d 27 5c 6e 27  ])!=0 && c!='\n'
3d686 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20  ; i++){}.       
3d687 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
3d688 5f 53 50 41 43 45 3b 20 20 20 2f 2a 20 49 4d 50  _SPACE;   /* IMP
3d689 3a 20 52 2d 32 32 39 33 34 2d 32 35 31 33 34 20  : R-22934-25134 
3d68a 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
3d68b 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n i;.      }.   
3d68c 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
3d68d 54 4b 5f 4d 49 4e 55 53 3b 0a 20 20 20 20 20 20  TK_MINUS;.      
3d68e 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
3d68f 20 20 20 20 63 61 73 65 20 27 28 27 3a 20 7b 0a      case '(': {.
3d690 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3d691 20 3d 20 54 4b 5f 4c 50 3b 0a 20 20 20 20 20 20   = TK_LP;.      
3d692 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
3d693 20 20 20 20 63 61 73 65 20 27 29 27 3a 20 7b 0a      case ')': {.
3d694 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3d695 20 3d 20 54 4b 5f 52 50 3b 0a 20 20 20 20 20 20   = TK_RP;.      
3d696 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
3d697 20 20 20 20 63 61 73 65 20 27 3b 27 3a 20 7b 0a      case ';': {.
3d698 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3d699 20 3d 20 54 4b 5f 53 45 4d 49 3b 0a 20 20 20 20   = TK_SEMI;.    
3d69a 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
3d69b 7d 0a 20 20 20 20 63 61 73 65 20 27 2b 27 3a 20  }.    case '+': 
3d69c 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
3d69d 70 65 20 3d 20 54 4b 5f 50 4c 55 53 3b 0a 20 20  pe = TK_PLUS;.  
3d69e 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
3d69f 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2a 27    }.    case '*'
3d6a0 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e  : {.      *token
3d6a1 54 79 70 65 20 3d 20 54 4b 5f 53 54 41 52 3b 0a  Type = TK_STAR;.
3d6a2 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
3d6a3 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
3d6a4 2f 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  /': {.      if( 
3d6a5 7a 5b 31 5d 21 3d 27 2a 27 20 7c 7c 20 7a 5b 32  z[1]!='*' || z[2
3d6a6 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ]==0 ){.        
3d6a7 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
3d6a8 53 4c 41 53 48 3b 0a 20 20 20 20 20 20 20 20 72  SLASH;.        r
3d6a9 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
3d6aa 0a 20 20 20 20 20 20 2f 2a 20 49 4d 50 3a 20 52  .      /* IMP: R
3d6ab 2d 35 30 34 31 37 2d 32 37 39 37 36 20 2d 2d 20  -50417-27976 -- 
3d6ac 73 79 6e 74 61 78 20 64 69 61 67 72 61 6d 20 66  syntax diagram f
3d6ad 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a 20  or comments */. 
3d6ae 20 20 20 20 20 66 6f 72 28 69 3d 33 2c 20 63 3d       for(i=3, c=
3d6af 7a 5b 32 5d 3b 20 28 63 21 3d 27 2a 27 20 7c 7c  z[2]; (c!='*' ||
3d6b0 20 7a 5b 69 5d 21 3d 27 2f 27 29 20 26 26 20 28   z[i]!='/') && (
3d6b1 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29  c=z[i])!=0; i++)
3d6b2 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 63 20 29  {}.      if( c )
3d6b3 20 69 2b 2b 3b 0a 20 20 20 20 20 20 2a 74 6f 6b   i++;.      *tok
3d6b4 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50 41 43  enType = TK_SPAC
3d6b5 45 3b 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 32  E;   /* IMP: R-2
3d6b6 32 39 33 34 2d 32 35 31 33 34 20 2a 2f 0a 20 20  2934-25134 */.  
3d6b7 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20      return i;.  
3d6b8 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 25 27    }.    case '%'
3d6b9 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e  : {.      *token
3d6ba 54 79 70 65 20 3d 20 54 4b 5f 52 45 4d 3b 0a 20  Type = TK_REM;. 
3d6bb 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
3d6bc 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3d     }.    case '=
3d6bd 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  ': {.      *toke
3d6be 6e 54 79 70 65 20 3d 20 54 4b 5f 45 51 3b 0a 20  nType = TK_EQ;. 
3d6bf 20 20 20 20 20 72 65 74 75 72 6e 20 31 20 2b 20       return 1 + 
3d6c0 28 7a 5b 31 5d 3d 3d 27 3d 27 29 3b 0a 20 20 20  (z[1]=='=');.   
3d6c1 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3c 27 3a   }.    case '<':
3d6c2 20 7b 0a 20 20 20 20 20 20 69 66 28 20 28 63 3d   {.      if( (c=
3d6c3 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29 7b 0a 20 20  z[1])=='=' ){.  
3d6c4 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3d6c5 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20   = TK_LE;.      
3d6c6 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20    return 2;.    
3d6c7 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27    }else if( c=='
3d6c8 3e 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74  >' ){.        *t
3d6c9 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4e 45  okenType = TK_NE
3d6ca 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
3d6cb 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20   2;.      }else 
3d6cc 69 66 28 20 63 3d 3d 27 3c 27 20 29 7b 0a 20 20  if( c=='<' ){.  
3d6cd 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3d6ce 20 3d 20 54 4b 5f 4c 53 48 49 46 54 3b 0a 20 20   = TK_LSHIFT;.  
3d6cf 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
3d6d0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
3d6d1 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
3d6d2 3d 20 54 4b 5f 4c 54 3b 0a 20 20 20 20 20 20 20  = TK_LT;.       
3d6d3 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
3d6d4 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73   }.    }.    cas
3d6d5 65 20 27 3e 27 3a 20 7b 0a 20 20 20 20 20 20 69  e '>': {.      i
3d6d6 66 28 20 28 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27  f( (c=z[1])=='='
3d6d7 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b   ){.        *tok
3d6d8 65 6e 54 79 70 65 20 3d 20 54 4b 5f 47 45 3b 0a  enType = TK_GE;.
3d6d9 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32          return 2
3d6da 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
3d6db 28 20 63 3d 3d 27 3e 27 20 29 7b 0a 20 20 20 20  ( c=='>' ){.    
3d6dc 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
3d6dd 20 54 4b 5f 52 53 48 49 46 54 3b 0a 20 20 20 20   TK_RSHIFT;.    
3d6de 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
3d6df 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3d6e0 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
3d6e1 54 4b 5f 47 54 3b 0a 20 20 20 20 20 20 20 20 72  TK_GT;.        r
3d6e2 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
3d6e3 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
3d6e4 27 21 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  '!': {.      if(
3d6e5 20 7a 5b 31 5d 21 3d 27 3d 27 20 29 7b 0a 20 20   z[1]!='=' ){.  
3d6e6 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3d6e7 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
3d6e8 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b         return 2;
3d6e9 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
3d6ea 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3d6eb 20 3d 20 54 4b 5f 4e 45 3b 0a 20 20 20 20 20 20   = TK_NE;.      
3d6ec 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20    return 2;.    
3d6ed 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61    }.    }.    ca
3d6ee 73 65 20 27 7c 27 3a 20 7b 0a 20 20 20 20 20 20  se '|': {.      
3d6ef 69 66 28 20 7a 5b 31 5d 21 3d 27 7c 27 20 29 7b  if( z[1]!='|' ){
3d6f0 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
3d6f1 79 70 65 20 3d 20 54 4b 5f 42 49 54 4f 52 3b 0a  ype = TK_BITOR;.
3d6f2 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
3d6f3 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
3d6f4 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
3d6f5 65 20 3d 20 54 4b 5f 43 4f 4e 43 41 54 3b 0a 20  e = TK_CONCAT;. 
3d6f6 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b         return 2;
3d6f7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
3d6f8 20 20 20 63 61 73 65 20 27 2c 27 3a 20 7b 0a 20     case ',': {. 
3d6f9 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
3d6fa 3d 20 54 4b 5f 43 4f 4d 4d 41 3b 0a 20 20 20 20  = TK_COMMA;.    
3d6fb 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
3d6fc 7d 0a 20 20 20 20 63 61 73 65 20 27 26 27 3a 20  }.    case '&': 
3d6fd 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
3d6fe 70 65 20 3d 20 54 4b 5f 42 49 54 41 4e 44 3b 0a  pe = TK_BITAND;.
3d6ff 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
3d700 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
3d701 7e 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  ~': {.      *tok
3d702 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54 4e  enType = TK_BITN
3d703 4f 54 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  OT;.      return
3d704 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
3d705 73 65 20 27 60 27 3a 0a 20 20 20 20 63 61 73 65  se '`':.    case
3d706 20 27 5c 27 27 3a 0a 20 20 20 20 63 61 73 65 20   '\'':.    case 
3d707 27 22 27 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  '"': {.      int
3d708 20 64 65 6c 69 6d 20 3d 20 7a 5b 30 5d 3b 0a 20   delim = z[0];. 
3d709 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 64       testcase( d
3d70a 65 6c 69 6d 3d 3d 27 60 27 20 29 3b 0a 20 20 20  elim=='`' );.   
3d70b 20 20 20 74 65 73 74 63 61 73 65 28 20 64 65 6c     testcase( del
3d70c 69 6d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20  im=='\'' );.    
3d70d 20 20 74 65 73 74 63 61 73 65 28 20 64 65 6c 69    testcase( deli
3d70e 6d 3d 3d 27 22 27 20 29 3b 0a 20 20 20 20 20 20  m=='"' );.      
3d70f 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d  for(i=1; (c=z[i]
3d710 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  )!=0; i++){.    
3d711 20 20 20 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d      if( c==delim
3d712 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
3d713 28 20 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20  ( z[i+1]==delim 
3d714 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
3d715 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  ++;.          }e
3d716 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
3d717 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
3d718 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
3d719 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
3d71a 63 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20  c=='\'' ){.     
3d71b 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
3d71c 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 20  TK_STRING;.     
3d71d 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20     return i+1;. 
3d71e 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63       }else if( c
3d71f 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a  !=0 ){.        *
3d720 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
3d721 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  D;.        retur
3d722 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c  n i+1;.      }el
3d723 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b  se{.        *tok
3d724 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
3d725 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 72 65 74  GAL;.        ret
3d726 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20  urn i;.      }. 
3d727 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2e     }.    case '.
3d728 27 3a 20 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ': {.#ifndef SQL
3d729 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
3d72a 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 69 66  G_POINT.      if
3d72b 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69  ( !sqlite3Isdigi
3d72c 74 28 7a 5b 31 5d 29 20 29 0a 23 65 6e 64 69 66  t(z[1]) ).#endif
3d72d 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20  .      {.       
3d72e 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
3d72f 5f 44 4f 54 3b 0a 20 20 20 20 20 20 20 20 72 65  _DOT;.        re
3d730 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
3d731 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
3d732 6e 65 78 74 20 63 68 61 72 61 63 74 65 72 20 69  next character i
3d733 73 20 61 20 64 69 67 69 74 2c 20 74 68 69 73 20  s a digit, this 
3d734 69 73 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f  is a floating po
3d735 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6e 75 6d  int.      ** num
3d736 62 65 72 20 74 68 61 74 20 62 65 67 69 6e 73 20  ber that begins 
3d737 77 69 74 68 20 22 2e 22 2e 20 20 46 61 6c 6c 20  with ".".  Fall 
3d738 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 6e 65  thru into the ne
3d739 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d  xt case */.    }
3d73a 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a 20 63  .    case '0': c
3d73b 61 73 65 20 27 31 27 3a 20 63 61 73 65 20 27 32  ase '1': case '2
3d73c 27 3a 20 63 61 73 65 20 27 33 27 3a 20 63 61 73  ': case '3': cas
3d73d 65 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65 20  e '4':.    case 
3d73e 27 35 27 3a 20 63 61 73 65 20 27 36 27 3a 20 63  '5': case '6': c
3d73f 61 73 65 20 27 37 27 3a 20 63 61 73 65 20 27 38  ase '7': case '8
3d740 27 3a 20 63 61 73 65 20 27 39 27 3a 20 7b 0a 20  ': case '9': {. 
3d741 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
3d742 5b 30 5d 3d 3d 27 30 27 20 29 3b 20 20 74 65 73  [0]=='0' );  tes
3d743 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 31 27  tcase( z[0]=='1'
3d744 20 29 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a   );  testcase( z
3d745 5b 30 5d 3d 3d 27 32 27 20 29 3b 0a 20 20 20 20  [0]=='2' );.    
3d746 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
3d747 3d 3d 27 33 27 20 29 3b 20 20 74 65 73 74 63 61  =='3' );  testca
3d748 73 65 28 20 7a 5b 30 5d 3d 3d 27 34 27 20 29 3b  se( z[0]=='4' );
3d749 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
3d74a 3d 3d 27 35 27 20 29 3b 0a 20 20 20 20 20 20 74  =='5' );.      t
3d74b 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
3d74c 36 27 20 29 3b 20 20 74 65 73 74 63 61 73 65 28  6' );  testcase(
3d74d 20 7a 5b 30 5d 3d 3d 27 37 27 20 29 3b 20 20 74   z[0]=='7' );  t
3d74e 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
3d74f 38 27 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  8' );.      test
3d750 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 39 27 20  case( z[0]=='9' 
3d751 29 3b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  );.      *tokenT
3d752 79 70 65 20 3d 20 54 4b 5f 49 4e 54 45 47 45 52  ype = TK_INTEGER
3d753 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
3d754 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
3d755 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 23 69  z[i]); i++){}.#i
3d756 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
3d757 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
3d758 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d  .      if( z[i]=
3d759 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='.' ){.        
3d75a 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69  i++;.        whi
3d75b 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67  le( sqlite3Isdig
3d75c 69 74 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b  it(z[i]) ){ i++;
3d75d 20 7d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65   }.        *toke
3d75e 6e 54 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54  nType = TK_FLOAT
3d75f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3d760 69 66 28 20 28 7a 5b 69 5d 3d 3d 27 65 27 20 7c  if( (z[i]=='e' |
3d761 7c 20 7a 5b 69 5d 3d 3d 27 45 27 29 20 26 26 0a  | z[i]=='E') &&.
3d762 20 20 20 20 20 20 20 20 20 20 20 28 20 73 71 6c             ( sql
3d763 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b 69 2b  ite3Isdigit(z[i+
3d764 31 5d 29 20 0a 20 20 20 20 20 20 20 20 20 20 20  1]) .           
3d765 20 7c 7c 20 28 28 7a 5b 69 2b 31 5d 3d 3d 27 2b   || ((z[i+1]=='+
3d766 27 20 7c 7c 20 7a 5b 69 2b 31 5d 3d 3d 27 2d 27  ' || z[i+1]=='-'
3d767 29 20 26 26 20 73 71 6c 69 74 65 33 49 73 64 69  ) && sqlite3Isdi
3d768 67 69 74 28 7a 5b 69 2b 32 5d 29 29 0a 20 20 20  git(z[i+2])).   
3d769 20 20 20 20 20 20 20 20 29 0a 20 20 20 20 20 20          ).      
3d76a 29 7b 0a 20 20 20 20 20 20 20 20 69 20 2b 3d 20  ){.        i += 
3d76b 32 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  2;.        while
3d76c 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ( sqlite3Isdigit
3d76d 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d  (z[i]) ){ i++; }
3d76e 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
3d76f 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a  ype = TK_FLOAT;.
3d770 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
3d771 20 20 20 20 20 77 68 69 6c 65 28 20 49 64 43 68       while( IdCh
3d772 61 72 28 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 20  ar(z[i]) ){.    
3d773 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
3d774 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20   TK_ILLEGAL;.   
3d775 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
3d776 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  }.      return i
3d777 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
3d778 20 27 5b 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f   '[': {.      fo
3d779 72 28 69 3d 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63  r(i=1, c=z[0]; c
3d77a 21 3d 27 5d 27 20 26 26 20 28 63 3d 7a 5b 69 5d  !=']' && (c=z[i]
3d77b 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  )!=0; i++){}.   
3d77c 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
3d77d 63 3d 3d 27 5d 27 20 3f 20 54 4b 5f 49 44 20 3a  c==']' ? TK_ID :
3d77e 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20   TK_ILLEGAL;.   
3d77f 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
3d780 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3f 27 3a   }.    case '?':
3d781 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
3d782 79 70 65 20 3d 20 54 4b 5f 56 41 52 49 41 42 4c  ype = TK_VARIABL
3d783 45 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31  E;.      for(i=1
3d784 3b 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ; sqlite3Isdigit
3d785 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  (z[i]); i++){}. 
3d786 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
3d787 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 23     }.    case '#
3d788 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69  ': {.      for(i
3d789 3d 31 3b 20 73 71 6c 69 74 65 33 49 73 64 69 67  =1; sqlite3Isdig
3d78a 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d  it(z[i]); i++){}
3d78b 0a 20 20 20 20 20 20 69 66 28 20 69 3e 31 20 29  .      if( i>1 )
3d78c 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  {.        /* Par
3d78d 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 66  ameters of the f
3d78e 6f 72 6d 20 23 4e 4e 4e 20 28 77 68 65 72 65 20  orm #NNN (where 
3d78f 4e 4e 4e 20 69 73 20 61 20 6e 75 6d 62 65 72 29  NNN is a number)
3d790 20 61 72 65 20 75 73 65 64 0a 20 20 20 20 20 20   are used.      
3d791 20 20 2a 2a 20 69 6e 74 65 72 6e 61 6c 6c 79 20    ** internally 
3d792 62 79 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64  by sqlite3Nested
3d793 50 61 72 73 65 2e 20 20 2a 2f 0a 20 20 20 20 20  Parse.  */.     
3d794 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
3d795 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20  TK_REGISTER;.   
3d796 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
3d797 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20       }.      /* 
3d798 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74  Fall through int
3d799 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20  o the next case 
3d79a 69 66 20 74 68 65 20 27 23 27 20 69 73 20 6e 6f  if the '#' is no
3d79b 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79 0a 20 20  t followed by.  
3d79c 20 20 20 20 2a 2a 20 61 20 64 69 67 69 74 2e 20      ** a digit. 
3d79d 54 72 79 20 74 6f 20 6d 61 74 63 68 20 23 41 41  Try to match #AA
3d79e 41 41 20 77 68 65 72 65 20 41 41 41 41 20 69 73  AA where AAAA is
3d79f 20 61 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d   a parameter nam
3d7a0 65 2e 20 2a 2f 0a 20 20 20 20 7d 0a 23 69 66 6e  e. */.    }.#ifn
3d7a1 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
3d7a2 54 43 4c 5f 56 41 52 49 41 42 4c 45 0a 20 20 20  TCL_VARIABLE.   
3d7a3 20 63 61 73 65 20 27 24 27 3a 0a 23 65 6e 64 69   case '$':.#endi
3d7a4 66 0a 20 20 20 20 63 61 73 65 20 27 40 27 3a 20  f.    case '@': 
3d7a5 20 2f 2a 20 46 6f 72 20 63 6f 6d 70 61 74 69 62   /* For compatib
3d7a6 69 6c 69 74 79 20 77 69 74 68 20 4d 53 20 53 51  ility with MS SQ
3d7a7 4c 20 53 65 72 76 65 72 20 2a 2f 0a 20 20 20 20  L Server */.    
3d7a8 63 61 73 65 20 27 3a 27 3a 20 7b 0a 20 20 20 20  case ':': {.    
3d7a9 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 20    int n = 0;.   
3d7aa 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30     testcase( z[0
3d7ab 5d 3d 3d 27 24 27 20 29 3b 20 20 74 65 73 74 63  ]=='$' );  testc
3d7ac 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 40 27 20 29  ase( z[0]=='@' )
3d7ad 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30  ;  testcase( z[0
3d7ae 5d 3d 3d 27 3a 27 20 29 3b 0a 20 20 20 20 20 20  ]==':' );.      
3d7af 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
3d7b0 56 41 52 49 41 42 4c 45 3b 0a 20 20 20 20 20 20  VARIABLE;.      
3d7b1 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d  for(i=1; (c=z[i]
3d7b2 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  )!=0; i++){.    
3d7b3 20 20 20 20 69 66 28 20 49 64 43 68 61 72 28 63      if( IdChar(c
3d7b4 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  ) ){.          n
3d7b5 2b 2b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ++;.#ifndef SQLI
3d7b6 54 45 5f 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49  TE_OMIT_TCL_VARI
3d7b7 41 42 4c 45 0a 20 20 20 20 20 20 20 20 7d 65 6c  ABLE.        }el
3d7b8 73 65 20 69 66 28 20 63 3d 3d 27 28 27 20 26 26  se if( c=='(' &&
3d7b9 20 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20   n>0 ){.        
3d7ba 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 20 20    do{.          
3d7bb 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    i++;.         
3d7bc 20 7d 77 68 69 6c 65 28 20 28 63 3d 7a 5b 69 5d   }while( (c=z[i]
3d7bd 29 21 3d 30 20 26 26 20 21 73 71 6c 69 74 65 33  )!=0 && !sqlite3
3d7be 49 73 73 70 61 63 65 28 63 29 20 26 26 20 63 21  Isspace(c) && c!
3d7bf 3d 27 29 27 20 29 3b 0a 20 20 20 20 20 20 20 20  =')' );.        
3d7c0 20 20 69 66 28 20 63 3d 3d 27 29 27 20 29 7b 0a    if( c==')' ){.
3d7c1 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b              i++;
3d7c2 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
3d7c3 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 74  {.            *t
3d7c4 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c  okenType = TK_IL
3d7c5 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 20  LEGAL;.         
3d7c6 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65   }.          bre
3d7c7 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ak;.        }els
3d7c8 65 20 69 66 28 20 63 3d 3d 27 3a 27 20 26 26 20  e if( c==':' && 
3d7c9 7a 5b 69 2b 31 5d 3d 3d 27 3a 27 20 29 7b 0a 20  z[i+1]==':' ){. 
3d7ca 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 23 65           i++;.#e
3d7cb 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7d 65 6c  ndif.        }el
3d7cc 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72  se{.          br
3d7cd 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
3d7ce 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
3d7cf 20 6e 3d 3d 30 20 29 20 2a 74 6f 6b 65 6e 54 79   n==0 ) *tokenTy
3d7d0 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  pe = TK_ILLEGAL;
3d7d1 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
3d7d2 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
3d7d3 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
3d7d4 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65  LITERAL.    case
3d7d5 20 27 78 27 3a 20 63 61 73 65 20 27 58 27 3a 20   'x': case 'X': 
3d7d6 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
3d7d7 28 20 7a 5b 30 5d 3d 3d 27 78 27 20 29 3b 20 74  ( z[0]=='x' ); t
3d7d8 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
3d7d9 58 27 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  X' );.      if( 
3d7da 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20  z[1]=='\'' ){.  
3d7db 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3d7dc 20 3d 20 54 4b 5f 42 4c 4f 42 3b 0a 20 20 20 20   = TK_BLOB;.    
3d7dd 20 20 20 20 66 6f 72 28 69 3d 32 3b 20 73 71 6c      for(i=2; sql
3d7de 69 74 65 33 49 73 78 64 69 67 69 74 28 7a 5b 69  ite3Isxdigit(z[i
3d7df 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20  ]); i++){}.     
3d7e0 20 20 20 69 66 28 20 7a 5b 69 5d 21 3d 27 5c 27     if( z[i]!='\'
3d7e1 27 20 7c 7c 20 69 25 32 20 29 7b 0a 20 20 20 20  ' || i%2 ){.    
3d7e2 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3d7e3 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
3d7e4 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
3d7e5 7a 5b 69 5d 20 26 26 20 7a 5b 69 5d 21 3d 27 5c  z[i] && z[i]!='\
3d7e6 27 27 20 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20 20  '' ){ i++; }.   
3d7e7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
3d7e8 66 28 20 7a 5b 69 5d 20 29 20 69 2b 2b 3b 0a 20  f( z[i] ) i++;. 
3d7e9 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b         return i;
3d7ea 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f  .      }.      /
3d7eb 2a 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c  * Otherwise fall
3d7ec 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
3d7ed 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20  next case */.   
3d7ee 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 64 65   }.#endif.    de
3d7ef 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69  fault: {.      i
3d7f0 66 28 20 21 49 64 43 68 61 72 28 2a 7a 29 20 29  f( !IdChar(*z) )
3d7f1 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
3d7f2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
3d7f3 6f 72 28 69 3d 31 3b 20 49 64 43 68 61 72 28 7a  or(i=1; IdChar(z
3d7f4 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  [i]); i++){}.   
3d7f5 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
3d7f6 6b 65 79 77 6f 72 64 43 6f 64 65 28 28 63 68 61  keywordCode((cha
3d7f7 72 2a 29 7a 2c 20 69 29 3b 0a 20 20 20 20 20 20  r*)z, i);.      
3d7f8 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a  return i;.    }.
3d7f9 20 20 7d 0a 20 20 2a 74 6f 6b 65 6e 54 79 70 65    }.  *tokenType
3d7fa 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
3d7fb 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
3d7fc 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73  .** Run the pars
3d7fd 65 72 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20  er on the given 
3d7fe 53 51 4c 20 73 74 72 69 6e 67 2e 20 20 54 68 65  SQL string.  The
3d7ff 20 70 61 72 73 65 72 20 73 74 72 75 63 74 75 72   parser structur
3d800 65 20 69 73 0a 2a 2a 20 70 61 73 73 65 64 20 69  e is.** passed i
3d801 6e 2e 20 20 41 6e 20 53 51 4c 49 54 45 5f 20 73  n.  An SQLITE_ s
3d802 74 61 74 75 73 20 63 6f 64 65 20 69 73 20 72 65  tatus code is re
3d803 74 75 72 6e 65 64 2e 20 20 49 66 20 61 6e 20 65  turned.  If an e
3d804 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20 74  rror occurs.** t
3d805 68 65 6e 20 61 6e 20 61 6e 64 20 61 74 74 65 6d  hen an and attem
3d806 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 77 72  pt is made to wr
3d807 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ite an error mes
3d808 73 61 67 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d 65  sage into .** me
3d809 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
3d80a 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
3d80b 63 28 29 20 61 6e 64 20 74 6f 20 6d 61 6b 65 20  c() and to make 
3d80c 2a 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e 74 20  *pzErrMsg point 
3d80d 74 6f 20 74 68 61 74 0a 2a 2a 20 65 72 72 6f 72  to that.** error
3d80e 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 53 51 4c   message..*/.SQL
3d80f 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
3d810 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72  sqlite3RunParser
3d811 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
3d812 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
3d813 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73  , char **pzErrMs
3d814 67 29 7b 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d  g){.  int nErr =
3d815 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
3d816 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
3d817 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e  of errors encoun
3d818 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  tered */.  int i
3d819 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3d81a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
3d81b 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
3d81c 76 6f 69 64 20 2a 70 45 6e 67 69 6e 65 3b 20 20  void *pEngine;  
3d81d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d81e 2f 2a 20 54 68 65 20 4c 45 4d 4f 4e 2d 67 65 6e  /* The LEMON-gen
3d81f 65 72 61 74 65 64 20 4c 41 4c 52 28 31 29 20 70  erated LALR(1) p
3d820 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74  arser */.  int t
3d821 6f 6b 65 6e 54 79 70 65 3b 20 20 20 20 20 20 20  okenType;       
3d822 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 79             /* ty
3d823 70 65 20 6f 66 20 74 68 65 20 6e 65 78 74 20 74  pe of the next t
3d824 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6c 61  oken */.  int la
3d825 73 74 54 6f 6b 65 6e 50 61 72 73 65 64 20 3d 20  stTokenParsed = 
3d826 2d 31 3b 20 20 20 20 20 20 20 2f 2a 20 74 79 70  -1;       /* typ
3d827 65 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75  e of the previou
3d828 73 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 75 38 20  s token */.  u8 
3d829 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b  enableLookaside;
3d82a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3d82b 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 64  Saved value of d
3d82c 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e  b->lookaside.bEn
3d82d 61 62 6c 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  abled */.  sqlit
3d82e 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
3d82f 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  >db;       /* Th
3d830 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
3d831 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6d  ction */.  int m
3d832 78 53 71 6c 4c 65 6e 3b 20 20 20 20 20 20 20 20  xSqlLen;        
3d833 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
3d834 78 20 6c 65 6e 67 74 68 20 6f 66 20 61 6e 20 53  x length of an S
3d835 51 4c 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 0a 20  QL string */... 
3d836 20 6d 78 53 71 6c 4c 65 6e 20 3d 20 64 62 2d 3e   mxSqlLen = db->
3d837 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
3d838 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 5d 3b  MIT_SQL_LENGTH];
3d839 0a 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76  .  if( db->activ
3d83a 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a 20  eVdbeCnt==0 ){. 
3d83b 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65     db->u1.isInte
3d83c 72 72 75 70 74 65 64 20 3d 20 30 3b 0a 20 20 7d  rrupted = 0;.  }
3d83d 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20  .  pParse->rc = 
3d83e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61  SQLITE_OK;.  pPa
3d83f 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 7a 53 71  rse->zTail = zSq
3d840 6c 3b 0a 20 20 69 20 3d 20 30 3b 0a 20 20 61 73  l;.  i = 0;.  as
3d841 73 65 72 74 28 20 70 7a 45 72 72 4d 73 67 21 3d  sert( pzErrMsg!=
3d842 30 20 29 3b 0a 20 20 70 45 6e 67 69 6e 65 20 3d  0 );.  pEngine =
3d843 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c   sqlite3ParserAl
3d844 6c 6f 63 28 28 76 6f 69 64 2a 28 2a 29 28 73 69  loc((void*(*)(si
3d845 7a 65 5f 74 29 29 73 71 6c 69 74 65 33 4d 61 6c  ze_t))sqlite3Mal
3d846 6c 6f 63 29 3b 0a 20 20 69 66 28 20 70 45 6e 67  loc);.  if( pEng
3d847 69 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62  ine==0 ){.    db
3d848 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
3d849 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   1;.    return S
3d84a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
3d84b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
3d84c 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20  e->pNewTable==0 
3d84d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
3d84e 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72  rse->pNewTrigger
3d84f 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
3d850 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3d 3d 30   pParse->nVar==0
3d851 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
3d852 61 72 73 65 2d 3e 6e 7a 56 61 72 3d 3d 30 20 29  arse->nzVar==0 )
3d853 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
3d854 73 65 2d 3e 61 7a 56 61 72 3d 3d 30 20 29 3b 0a  se->azVar==0 );.
3d855 20 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64    enableLookasid
3d856 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  e = db->lookasid
3d857 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 69 66  e.bEnabled;.  if
3d858 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ( db->lookaside.
3d859 70 53 74 61 72 74 20 29 20 64 62 2d 3e 6c 6f 6f  pStart ) db->loo
3d85a 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
3d85b 3d 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 21 64  = 1;.  while( !d
3d85c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
3d85d 26 26 20 7a 53 71 6c 5b 69 5d 21 3d 30 20 29 7b  && zSql[i]!=0 ){
3d85e 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d  .    assert( i>=
3d85f 30 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  0 );.    pParse-
3d860 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 20 3d 20  >sLastToken.z = 
3d861 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 70 50  &zSql[i];.    pP
3d862 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e  arse->sLastToken
3d863 2e 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  .n = sqlite3GetT
3d864 6f 6b 65 6e 28 28 75 6e 73 69 67 6e 65 64 20 63  oken((unsigned c
3d865 68 61 72 2a 29 26 7a 53 71 6c 5b 69 5d 2c 26 74  har*)&zSql[i],&t
3d866 6f 6b 65 6e 54 79 70 65 29 3b 0a 20 20 20 20 69  okenType);.    i
3d867 20 2b 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61 73   += pParse->sLas
3d868 74 54 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 69 66  tToken.n;.    if
3d869 28 20 69 3e 6d 78 53 71 6c 4c 65 6e 20 29 7b 0a  ( i>mxSqlLen ){.
3d86a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63        pParse->rc
3d86b 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47   = SQLITE_TOOBIG
3d86c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
3d86d 20 20 20 7d 0a 20 20 20 20 73 77 69 74 63 68 28     }.    switch(
3d86e 20 74 6f 6b 65 6e 54 79 70 65 20 29 7b 0a 20 20   tokenType ){.  
3d86f 20 20 20 20 63 61 73 65 20 54 4b 5f 53 50 41 43      case TK_SPAC
3d870 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28  E: {.        if(
3d871 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
3d872 75 70 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20  upted ){.       
3d873 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
3d874 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e 74 65  sg(pParse, "inte
3d875 72 72 75 70 74 22 29 3b 0a 20 20 20 20 20 20 20  rrupt");.       
3d876 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
3d877 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
3d878 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
3d879 20 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20   abort_parse;.  
3d87a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
3d87b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
3d87c 20 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4c 4c       case TK_ILL
3d87d 45 47 41 4c 3a 20 7b 0a 20 20 20 20 20 20 20 20  EGAL: {.        
3d87e 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
3d87f 2c 20 2a 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20  , *pzErrMsg);.  
3d880 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20        *pzErrMsg 
3d881 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
3d882 28 64 62 2c 20 22 75 6e 72 65 63 6f 67 6e 69 7a  (db, "unrecogniz
3d883 65 64 20 74 6f 6b 65 6e 3a 20 5c 22 25 54 5c 22  ed token: \"%T\"
3d884 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
3d885 20 20 20 20 20 20 20 20 20 20 20 26 70 50 61 72             &pPar
3d886 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 29 3b  se->sLastToken);
3d887 0a 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b  .        nErr++;
3d888 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62  .        goto ab
3d889 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20 20 20  ort_parse;.     
3d88a 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b   }.      case TK
3d88b 5f 53 45 4d 49 3a 20 7b 0a 20 20 20 20 20 20 20  _SEMI: {.       
3d88c 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d   pParse->zTail =
3d88d 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 20   &zSql[i];.     
3d88e 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20     /* Fall thru 
3d88f 69 6e 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74  into the default
3d890 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 7d   case */.      }
3d891 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20  .      default: 
3d892 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
3d893 33 50 61 72 73 65 72 28 70 45 6e 67 69 6e 65 2c  3Parser(pEngine,
3d894 20 74 6f 6b 65 6e 54 79 70 65 2c 20 70 50 61 72   tokenType, pPar
3d895 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20  se->sLastToken, 
3d896 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20  pParse);.       
3d897 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73 65 64   lastTokenParsed
3d898 20 3d 20 74 6f 6b 65 6e 54 79 70 65 3b 0a 20 20   = tokenType;.  
3d899 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65        if( pParse
3d89a 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc!=SQLITE_OK 
3d89b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ){.          got
3d89c 6f 20 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20  o abort_parse;. 
3d89d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3d89e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
3d89f 20 20 20 20 7d 0a 20 20 7d 0a 61 62 6f 72 74 5f      }.  }.abort_
3d8a0 70 61 72 73 65 3a 0a 20 20 69 66 28 20 7a 53 71  parse:.  if( zSq
3d8a1 6c 5b 69 5d 3d 3d 30 20 26 26 20 6e 45 72 72 3d  l[i]==0 && nErr=
3d8a2 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 72 63  =0 && pParse->rc
3d8a3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3d8a4 20 20 20 69 66 28 20 6c 61 73 74 54 6f 6b 65 6e     if( lastToken
3d8a5 50 61 72 73 65 64 21 3d 54 4b 5f 53 45 4d 49 20  Parsed!=TK_SEMI 
3d8a6 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
3d8a7 50 61 72 73 65 72 28 70 45 6e 67 69 6e 65 2c 20  Parser(pEngine, 
3d8a8 54 4b 5f 53 45 4d 49 2c 20 70 50 61 72 73 65 2d  TK_SEMI, pParse-
3d8a9 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61  >sLastToken, pPa
3d8aa 72 73 65 29 3b 0a 20 20 20 20 20 20 70 50 61 72  rse);.      pPar
3d8ab 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71  se->zTail = &zSq
3d8ac 6c 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  l[i];.    }.    
3d8ad 73 71 6c 69 74 65 33 50 61 72 73 65 72 28 70 45  sqlite3Parser(pE
3d8ae 6e 67 69 6e 65 2c 20 30 2c 20 70 50 61 72 73 65  ngine, 0, pParse
3d8af 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50  ->sLastToken, pP
3d8b0 61 72 73 65 29 3b 0a 20 20 7d 0a 23 69 66 64 65  arse);.  }.#ifde
3d8b1 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43  f YYTRACKMAXSTAC
3d8b2 4b 44 45 50 54 48 0a 20 20 73 71 6c 69 74 65 33  KDEPTH.  sqlite3
3d8b3 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45  StatusSet(SQLITE
3d8b4 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53  _STATUS_PARSER_S
3d8b5 54 41 43 4b 2c 0a 20 20 20 20 20 20 73 71 6c 69  TACK,.      sqli
3d8b6 74 65 33 50 61 72 73 65 72 53 74 61 63 6b 50 65  te3ParserStackPe
3d8b7 61 6b 28 70 45 6e 67 69 6e 65 29 0a 20 20 29 3b  ak(pEngine).  );
3d8b8 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59 44 45 42  .#endif /* YYDEB
3d8b9 55 47 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50  UG */.  sqlite3P
3d8ba 61 72 73 65 72 46 72 65 65 28 70 45 6e 67 69 6e  arserFree(pEngin
3d8bb 65 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29  e, sqlite3_free)
3d8bc 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  ;.  db->lookasid
3d8bd 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61  e.bEnabled = ena
3d8be 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 0a 20 20  bleLookaside;.  
3d8bf 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
3d8c0 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 50 61 72  iled ){.    pPar
3d8c1 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
3d8c2 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28  NOMEM;.  }.  if(
3d8c3 20 70 50 61 72 73 65 2d 3e 72 63 21 3d 53 51 4c   pParse->rc!=SQL
3d8c4 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 72 73 65  ITE_OK && pParse
3d8c5 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ->rc!=SQLITE_DON
3d8c6 45 20 26 26 20 70 50 61 72 73 65 2d 3e 7a 45 72  E && pParse->zEr
3d8c7 72 4d 73 67 3d 3d 30 20 29 7b 0a 20 20 20 20 73  rMsg==0 ){.    s
3d8c8 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
3d8c9 26 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67  &pParse->zErrMsg
3d8ca 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
3d8cb 74 65 33 45 72 72 53 74 72 28 70 50 61 72 73 65  te3ErrStr(pParse
3d8cc 2d 3e 72 63 29 29 3b 0a 20 20 7d 0a 20 20 61 73  ->rc));.  }.  as
3d8cd 73 65 72 74 28 20 70 7a 45 72 72 4d 73 67 21 3d  sert( pzErrMsg!=
3d8ce 30 20 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73  0 );.  if( pPars
3d8cf 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  e->zErrMsg ){.  
3d8d0 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 70 50    *pzErrMsg = pP
3d8d1 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20  arse->zErrMsg;. 
3d8d2 20 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 70     sqlite3_log(p
3d8d3 50 61 72 73 65 2d 3e 72 63 2c 20 22 25 73 22 2c  Parse->rc, "%s",
3d8d4 20 2a 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20   *pzErrMsg);.   
3d8d5 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
3d8d6 20 3d 20 30 3b 0a 20 20 20 20 6e 45 72 72 2b 2b   = 0;.    nErr++
3d8d7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72  ;.  }.  if( pPar
3d8d8 73 65 2d 3e 70 56 64 62 65 20 26 26 20 70 50 61  se->pVdbe && pPa
3d8d9 72 73 65 2d 3e 6e 45 72 72 3e 30 20 26 26 20 70  rse->nErr>0 && p
3d8da 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
3d8db 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
3d8dc 64 62 65 44 65 6c 65 74 65 28 70 50 61 72 73 65  dbeDelete(pParse
3d8dd 2d 3e 70 56 64 62 65 29 3b 0a 20 20 20 20 70 50  ->pVdbe);.    pP
3d8de 61 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 30 3b  arse->pVdbe = 0;
3d8df 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
3d8e0 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
3d8e1 43 41 43 48 45 0a 20 20 69 66 28 20 70 50 61 72  CACHE.  if( pPar
3d8e2 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b  se->nested==0 ){
3d8e3 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
3d8e4 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61  ee(db, pParse->a
3d8e5 54 61 62 6c 65 4c 6f 63 6b 29 3b 0a 20 20 20 20  TableLock);.    
3d8e6 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f  pParse->aTableLo
3d8e7 63 6b 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72  ck = 0;.    pPar
3d8e8 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 20 3d  se->nTableLock =
3d8e9 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23   0;.  }.#endif.#
3d8ea 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3d8eb 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
3d8ec 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
3d8ed 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63  Parse->apVtabLoc
3d8ee 6b 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66  k);.#endif..  if
3d8ef 28 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54  ( !IN_DECLARE_VT
3d8f0 41 42 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  AB ){.    /* If 
3d8f1 74 68 65 20 70 50 61 72 73 65 2d 3e 64 65 63 6c  the pParse->decl
3d8f2 61 72 65 56 74 61 62 20 66 6c 61 67 20 69 73 20  areVtab flag is 
3d8f3 73 65 74 2c 20 64 6f 20 6e 6f 74 20 64 65 6c 65  set, do not dele
3d8f4 74 65 20 61 6e 79 20 74 61 62 6c 65 20 0a 20 20  te any table .  
3d8f5 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65 20 62    ** structure b
3d8f6 75 69 6c 74 20 75 70 20 69 6e 20 70 50 61 72 73  uilt up in pPars
3d8f7 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2e 20 54 68  e->pNewTable. Th
3d8f8 65 20 63 61 6c 6c 69 6e 67 20 63 6f 64 65 20 28  e calling code (
3d8f9 73 65 65 20 76 74 61 62 2e 63 29 0a 20 20 20 20  see vtab.c).    
3d8fa 2a 2a 20 77 69 6c 6c 20 74 61 6b 65 20 72 65 73  ** will take res
3d8fb 70 6f 6e 73 69 62 69 6c 69 74 79 20 66 6f 72 20  ponsibility for 
3d8fc 66 72 65 65 69 6e 67 20 74 68 65 20 54 61 62 6c  freeing the Tabl
3d8fd 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 20  e structure..   
3d8fe 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 44   */.    sqlite3D
3d8ff 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70  eleteTable(db, p
3d900 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
3d901 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  );.  }..  sqlite
3d902 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 64  3DeleteTrigger(d
3d903 62 2c 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  b, pParse->pNewT
3d904 72 69 67 67 65 72 29 3b 0a 20 20 66 6f 72 28 69  rigger);.  for(i
3d905 3d 70 50 61 72 73 65 2d 3e 6e 7a 56 61 72 2d 31  =pParse->nzVar-1
3d906 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 20 73 71 6c  ; i>=0; i--) sql
3d907 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
3d908 50 61 72 73 65 2d 3e 61 7a 56 61 72 5b 69 5d 29  Parse->azVar[i])
3d909 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
3d90a 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 7a  e(db, pParse->az
3d90b 56 61 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  Var);.  sqlite3D
3d90c 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65  bFree(db, pParse
3d90d 2d 3e 61 41 6c 69 61 73 29 3b 0a 20 20 77 68 69  ->aAlias);.  whi
3d90e 6c 65 28 20 70 50 61 72 73 65 2d 3e 70 41 69 6e  le( pParse->pAin
3d90f 63 20 29 7b 0a 20 20 20 20 41 75 74 6f 69 6e 63  c ){.    Autoinc
3d910 49 6e 66 6f 20 2a 70 20 3d 20 70 50 61 72 73 65  Info *p = pParse
3d911 2d 3e 70 41 69 6e 63 3b 0a 20 20 20 20 70 50 61  ->pAinc;.    pPa
3d912 72 73 65 2d 3e 70 41 69 6e 63 20 3d 20 70 2d 3e  rse->pAinc = p->
3d913 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74  pNext;.    sqlit
3d914 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b  e3DbFree(db, p);
3d915 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 50  .  }.  while( pP
3d916 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62  arse->pZombieTab
3d917 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   ){.    Table *p
3d918 20 3d 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d 62   = pParse->pZomb
3d919 69 65 54 61 62 3b 0a 20 20 20 20 70 50 61 72 73  ieTab;.    pPars
3d91a 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 3d 20  e->pZombieTab = 
3d91b 70 2d 3e 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 0a  p->pNextZombie;.
3d91c 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
3d91d 65 54 61 62 6c 65 28 64 62 2c 20 70 29 3b 0a 20  eTable(db, p);. 
3d91e 20 7d 0a 20 20 69 66 28 20 6e 45 72 72 3e 30 20   }.  if( nErr>0 
3d91f 26 26 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53  && pParse->rc==S
3d920 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3d921 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c  pParse->rc = SQL
3d922 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
3d923 20 72 65 74 75 72 6e 20 6e 45 72 72 3b 0a 7d 0a   return nErr;.}.
3d924 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3d925 20 45 6e 64 20 6f 66 20 74 6f 6b 65 6e 69 7a 65   End of tokenize
3d926 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
3d927 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d928 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3d929 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3d92a 20 42 65 67 69 6e 20 66 69 6c 65 20 63 6f 6d 70   Begin file comp
3d92b 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lete.c *********
3d92c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d92d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3d92e 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
3d92f 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
3d930 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
3d931 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
3d932 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
3d933 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
3d934 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
3d935 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
3d936 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
3d937 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
3d938 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
3d939 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
3d93a 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
3d93b 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
3d93c 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
3d93d 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
3d93e 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
3d93f 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
3d940 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
3d941 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d942 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d943 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d944 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3d945 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41 6e 20 74 6f  *******.** An to
3d946 6b 65 6e 69 7a 65 72 20 66 6f 72 20 53 51 4c 0a  kenizer for SQL.
3d947 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
3d948 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20  contains C code 
3d949 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
3d94a 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  the sqlite3_comp
3d94b 6c 65 74 65 28 29 20 41 50 49 2e 0a 2a 2a 20 54  lete() API..** T
3d94c 68 69 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f  his code used to
3d94d 20 62 65 20 70 61 72 74 20 6f 66 20 74 68 65 20   be part of the 
3d94e 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 73 6f 75 72  tokenizer.c sour
3d94f 63 65 20 66 69 6c 65 2e 20 20 42 75 74 20 62 79  ce file.  But by
3d950 0a 2a 2a 20 73 65 70 61 72 61 74 69 6e 67 20 69  .** separating i
3d951 74 20 6f 75 74 2c 20 74 68 65 20 63 6f 64 65 20  t out, the code 
3d952 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69  will be automati
3d953 63 61 6c 6c 79 20 6f 6d 69 74 74 65 64 20 66 72  cally omitted fr
3d954 6f 6d 0a 2a 2a 20 73 74 61 74 69 63 20 6c 69 6e  om.** static lin
3d955 6b 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 75  ks that do not u
3d956 73 65 20 69 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65  se it..*/.#ifnde
3d957 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f  f SQLITE_OMIT_CO
3d958 4d 50 4c 45 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68  MPLETE../*.** Th
3d959 69 73 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e  is is defined in
3d95a 20 74 6f 6b 65 6e 69 7a 65 2e 63 2e 20 20 57 65   tokenize.c.  We
3d95b 20 6a 75 73 74 20 68 61 76 65 20 74 6f 20 69 6d   just have to im
3d95c 70 6f 72 74 20 74 68 65 20 64 65 66 69 6e 69 74  port the definit
3d95d 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ion..*/.#ifndef 
3d95e 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54  SQLITE_AMALGAMAT
3d95f 49 4f 4e 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ION.#ifdef SQLIT
3d960 45 5f 41 53 43 49 49 0a 23 64 65 66 69 6e 65 20  E_ASCII.#define 
3d961 49 64 43 68 61 72 28 43 29 20 20 28 28 73 71 6c  IdChar(C)  ((sql
3d962 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e  ite3CtypeMap[(un
3d963 73 69 67 6e 65 64 20 63 68 61 72 29 43 5d 26 30  signed char)C]&0
3d964 78 34 36 29 21 3d 30 29 0a 23 65 6e 64 69 66 0a  x46)!=0).#endif.
3d965 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42  #ifdef SQLITE_EB
3d966 43 44 49 43 0a 53 51 4c 49 54 45 5f 50 52 49 56  CDIC.SQLITE_PRIV
3d967 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73  ATE const char s
3d968 71 6c 69 74 65 33 49 73 45 62 63 64 69 63 49 64  qlite3IsEbcdicId
3d969 43 68 61 72 5b 5d 3b 0a 23 64 65 66 69 6e 65 20  Char[];.#define 
3d96a 49 64 43 68 61 72 28 43 29 20 20 28 28 28 63 3d  IdChar(C)  (((c=
3d96b 43 29 3e 3d 30 78 34 32 20 26 26 20 73 71 6c 69  C)>=0x42 && sqli
3d96c 74 65 33 49 73 45 62 63 64 69 63 49 64 43 68 61  te3IsEbcdicIdCha
3d96d 72 5b 63 2d 30 78 34 30 5d 29 29 0a 23 65 6e 64  r[c-0x40])).#end
3d96e 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  if.#endif /* SQL
3d96f 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
3d970 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 6f 6b 65   */.../*.** Toke
3d971 6e 20 74 79 70 65 73 20 75 73 65 64 20 62 79 20  n types used by 
3d972 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  the sqlite3_comp
3d973 6c 65 74 65 28 29 20 72 6f 75 74 69 6e 65 2e 20  lete() routine. 
3d974 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72 0a   See the header.
3d975 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74  ** comments on t
3d976 68 61 74 20 70 72 6f 63 65 64 75 72 65 20 66 6f  hat procedure fo
3d977 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
3d978 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65  ormation..*/.#de
3d979 66 69 6e 65 20 74 6b 53 45 4d 49 20 20 20 20 30  fine tkSEMI    0
3d97a 0a 23 64 65 66 69 6e 65 20 74 6b 57 53 20 20 20  .#define tkWS   
3d97b 20 20 20 31 0a 23 64 65 66 69 6e 65 20 74 6b 4f     1.#define tkO
3d97c 54 48 45 52 20 20 20 32 0a 23 69 66 6e 64 65 66  THER   2.#ifndef
3d97d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
3d97e 47 47 45 52 0a 23 64 65 66 69 6e 65 20 74 6b 45  GGER.#define tkE
3d97f 58 50 4c 41 49 4e 20 33 0a 23 64 65 66 69 6e 65  XPLAIN 3.#define
3d980 20 74 6b 43 52 45 41 54 45 20 20 34 0a 23 64 65   tkCREATE  4.#de
3d981 66 69 6e 65 20 74 6b 54 45 4d 50 20 20 20 20 35  fine tkTEMP    5
3d982 0a 23 64 65 66 69 6e 65 20 74 6b 54 52 49 47 47  .#define tkTRIGG
3d983 45 52 20 36 0a 23 64 65 66 69 6e 65 20 74 6b 45  ER 6.#define tkE
3d984 4e 44 20 20 20 20 20 37 0a 23 65 6e 64 69 66 0a  ND     7.#endif.
3d985 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
3d986 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e 20  UE if the given 
3d987 53 51 4c 20 73 74 72 69 6e 67 20 65 6e 64 73 20  SQL string ends 
3d988 69 6e 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a  in a semicolon..
3d989 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20 68 61  **.** Special ha
3d98a 6e 64 6c 69 6e 67 20 69 73 20 72 65 71 75 69 72  ndling is requir
3d98b 65 20 66 6f 72 20 43 52 45 41 54 45 20 54 52 49  e for CREATE TRI
3d98c 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 73 2e  GGER statements.
3d98d 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20 74 68 65  .** Whenever the
3d98e 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
3d98f 6b 65 79 77 6f 72 64 73 20 61 72 65 20 73 65 65  keywords are see
3d990 6e 2c 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  n, the statement
3d991 0a 2a 2a 20 6d 75 73 74 20 65 6e 64 20 77 69 74  .** must end wit
3d992 68 20 22 3b 45 4e 44 3b 22 2e 0a 2a 2a 0a 2a 2a  h ";END;"..**.**
3d993 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   This implementa
3d994 74 69 6f 6e 20 75 73 65 73 20 61 20 73 74 61 74  tion uses a stat
3d995 65 20 6d 61 63 68 69 6e 65 20 77 69 74 68 20 38  e machine with 8
3d996 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
3d997 20 28 30 29 20 49 4e 56 41 4c 49 44 20 20 20 57   (0) INVALID   W
3d998 65 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 73  e have not yet s
3d999 65 65 6e 20 61 20 6e 6f 6e 2d 77 68 69 74 65 73  een a non-whites
3d99a 70 61 63 65 20 63 68 61 72 61 63 74 65 72 2e 0a  pace character..
3d99b 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 53 54 41 52  **.**   (1) STAR
3d99c 54 20 20 20 20 20 41 74 20 74 68 65 20 62 65 67  T     At the beg
3d99d 69 6e 6e 69 6e 67 20 6f 72 20 65 6e 64 20 6f 66  inning or end of
3d99e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
3d99f 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  t.  This routine
3d9a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3d9a1 20 20 20 20 72 65 74 75 72 6e 73 20 31 20 69 66      returns 1 if
3d9a2 20 69 74 20 65 6e 64 73 20 69 6e 20 74 68 65 20   it ends in the 
3d9a3 53 54 41 52 54 20 73 74 61 74 65 20 61 6e 64 20  START state and 
3d9a4 30 20 69 66 20 69 74 20 65 6e 64 73 0a 2a 2a 20  0 if it ends.** 
3d9a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d9a6 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61  in any other sta
3d9a7 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 32 29 20  te..**.**   (2) 
3d9a8 4e 4f 52 4d 41 4c 20 20 20 20 57 65 20 61 72 65  NORMAL    We are
3d9a9 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
3d9aa 66 20 73 74 61 74 65 6d 65 6e 74 20 77 68 69 63  f statement whic
3d9ab 68 20 65 6e 64 73 20 77 69 74 68 20 61 20 73 69  h ends with a si
3d9ac 6e 67 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  ngle.**         
3d9ad 20 20 20 20 20 20 20 20 73 65 6d 69 63 6f 6c 6f          semicolo
3d9ae 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29 20 45  n..**.**   (3) E
3d9af 58 50 4c 41 49 4e 20 20 20 54 68 65 20 6b 65 79  XPLAIN   The key
3d9b0 77 6f 72 64 20 45 58 50 4c 41 49 4e 20 68 61 73  word EXPLAIN has
3d9b1 20 62 65 65 6e 20 73 65 65 6e 20 61 74 20 74 68   been seen at th
3d9b2 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 0a  e beginning of .
3d9b3 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
3d9b4 20 20 20 61 20 73 74 61 74 65 6d 65 6e 74 2e 0a     a statement..
3d9b5 2a 2a 0a 2a 2a 20 20 20 28 34 29 20 43 52 45 41  **.**   (4) CREA
3d9b6 54 45 20 20 20 20 54 68 65 20 6b 65 79 77 6f 72  TE    The keywor
3d9b7 64 20 43 52 45 41 54 45 20 68 61 73 20 62 65 65  d CREATE has bee
3d9b8 6e 20 73 65 65 6e 20 61 74 20 74 68 65 20 62 65  n seen at the be
3d9b9 67 69 6e 6e 69 6e 67 20 6f 66 20 61 0a 2a 2a 20  ginning of a.** 
3d9ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d9bb 73 74 61 74 65 6d 65 6e 74 2c 20 70 6f 73 73 69  statement, possi
3d9bc 62 6c 79 20 70 72 65 63 65 65 64 65 64 20 62 79  bly preceeded by
3d9bd 20 45 58 50 4c 41 49 4e 20 61 6e 64 2f 6f 72 20   EXPLAIN and/or 
3d9be 66 6f 6c 6c 6f 77 65 64 20 62 79 0a 2a 2a 20 20  followed by.**  
3d9bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
3d9c0 45 4d 50 20 6f 72 20 54 45 4d 50 4f 52 41 52 59  EMP or TEMPORARY
3d9c1 0a 2a 2a 0a 2a 2a 20 20 20 28 35 29 20 54 52 49  .**.**   (5) TRI
3d9c2 47 47 45 52 20 20 20 57 65 20 61 72 65 20 69 6e  GGER   We are in
3d9c3 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 61   the middle of a
3d9c4 20 74 72 69 67 67 65 72 20 64 65 66 69 6e 69 74   trigger definit
3d9c5 69 6f 6e 20 74 68 61 74 20 6d 75 73 74 20 62 65  ion that must be
3d9c6 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3d9c7 20 20 20 20 65 6e 64 65 64 20 62 79 20 61 20 73      ended by a s
3d9c8 65 6d 69 63 6f 6c 6f 6e 2c 20 74 68 65 20 6b 65  emicolon, the ke
3d9c9 79 77 6f 72 64 20 45 4e 44 2c 20 61 6e 64 20 61  yword END, and a
3d9ca 6e 6f 74 68 65 72 20 73 65 6d 69 63 6f 6c 6f 6e  nother semicolon
3d9cb 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36 29 20 53 45  ..**.**   (6) SE
3d9cc 4d 49 20 20 20 20 20 20 57 65 27 76 65 20 73 65  MI      We've se
3d9cd 65 6e 20 74 68 65 20 66 69 72 73 74 20 73 65 6d  en the first sem
3d9ce 69 63 6f 6c 6f 6e 20 69 6e 20 74 68 65 20 22 3b  icolon in the ";
3d9cf 45 4e 44 3b 22 20 74 68 61 74 20 6f 63 63 75 72  END;" that occur
3d9d0 73 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  s at.**         
3d9d1 20 20 20 20 20 20 20 20 74 68 65 20 65 6e 64 20          the end 
3d9d2 6f 66 20 61 20 74 72 69 67 67 65 72 20 64 65 66  of a trigger def
3d9d3 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20  inition..**.**  
3d9d4 20 28 37 29 20 45 4e 44 20 20 20 20 20 20 20 57   (7) END       W
3d9d5 65 27 76 65 20 73 65 65 6e 20 74 68 65 20 22 3b  e've seen the ";
3d9d6 45 4e 44 22 20 6f 66 20 74 68 65 20 22 3b 45 4e  END" of the ";EN
3d9d7 44 3b 22 20 74 68 61 74 20 6f 63 63 75 72 73 20  D;" that occurs 
3d9d8 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 20 20  at the end.**   
3d9d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 66                of
3d9da 20 61 20 74 72 69 67 67 65 72 20 64 69 66 69 6e   a trigger difin
3d9db 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 72 61  ition..**.** Tra
3d9dc 6e 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65 6e  nsitions between
3d9dd 20 73 74 61 74 65 73 20 61 62 6f 76 65 20 61 72   states above ar
3d9de 65 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20  e determined by 
3d9df 74 6f 6b 65 6e 73 20 65 78 74 72 61 63 74 65 64  tokens extracted
3d9e0 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e 70  .** from the inp
3d9e1 75 74 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ut.  The followi
3d9e2 6e 67 20 74 6f 6b 65 6e 73 20 61 72 65 20 73 69  ng tokens are si
3d9e3 67 6e 69 66 69 63 61 6e 74 3a 0a 2a 2a 0a 2a 2a  gnificant:.**.**
3d9e4 20 20 20 28 30 29 20 74 6b 53 45 4d 49 20 20 20     (0) tkSEMI   
3d9e5 20 20 20 41 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a     A semicolon..
3d9e6 2a 2a 20 20 20 28 31 29 20 74 6b 57 53 20 20 20  **   (1) tkWS   
3d9e7 20 20 20 20 20 57 68 69 74 65 73 70 61 63 65 2e       Whitespace.
3d9e8 0a 2a 2a 20 20 20 28 32 29 20 74 6b 4f 54 48 45  .**   (2) tkOTHE
3d9e9 52 20 20 20 20 20 41 6e 79 20 6f 74 68 65 72 20  R     Any other 
3d9ea 53 51 4c 20 74 6f 6b 65 6e 2e 0a 2a 2a 20 20 20  SQL token..**   
3d9eb 28 33 29 20 74 6b 45 58 50 4c 41 49 4e 20 20 20  (3) tkEXPLAIN   
3d9ec 54 68 65 20 22 65 78 70 6c 61 69 6e 22 20 6b 65  The "explain" ke
3d9ed 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 34 29 20  yword..**   (4) 
3d9ee 74 6b 43 52 45 41 54 45 20 20 20 20 54 68 65 20  tkCREATE    The 
3d9ef 22 63 72 65 61 74 65 22 20 6b 65 79 77 6f 72 64  "create" keyword
3d9f0 2e 0a 2a 2a 20 20 20 28 35 29 20 74 6b 54 45 4d  ..**   (5) tkTEM
3d9f1 50 20 20 20 20 20 20 54 68 65 20 22 74 65 6d 70  P      The "temp
3d9f2 22 20 6f 72 20 22 74 65 6d 70 6f 72 61 72 79 22  " or "temporary"
3d9f3 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28   keyword..**   (
3d9f4 36 29 20 74 6b 54 52 49 47 47 45 52 20 20 20 54  6) tkTRIGGER   T
3d9f5 68 65 20 22 74 72 69 67 67 65 72 22 20 6b 65 79  he "trigger" key
3d9f6 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 37 29 20 74  word..**   (7) t
3d9f7 6b 45 4e 44 20 20 20 20 20 20 20 54 68 65 20 22  kEND       The "
3d9f8 65 6e 64 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a  end" keyword..**
3d9f9 0a 2a 2a 20 57 68 69 74 65 73 70 61 63 65 20 6e  .** Whitespace n
3d9fa 65 76 65 72 20 63 61 75 73 65 73 20 61 20 73 74  ever causes a st
3d9fb 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 61  ate transition a
3d9fc 6e 64 20 69 73 20 61 6c 77 61 79 73 20 69 67 6e  nd is always ign
3d9fd 6f 72 65 64 2e 0a 2a 2a 20 54 68 69 73 20 6d 65  ored..** This me
3d9fe 61 6e 73 20 74 68 61 74 20 61 20 53 51 4c 20 73  ans that a SQL s
3d9ff 74 72 69 6e 67 20 6f 66 20 61 6c 6c 20 77 68 69  tring of all whi
3da00 74 65 73 70 61 63 65 20 69 73 20 69 6e 76 61 6c  tespace is inval
3da01 69 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 65 20  id..**.** If we 
3da02 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 53 51 4c  compile with SQL
3da03 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
3da04 2c 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 6f 6d  , all of the com
3da05 70 75 74 61 74 69 6f 6e 20 6e 65 65 64 65 64 0a  putation needed.
3da06 2a 2a 20 74 6f 20 72 65 63 6f 67 6e 69 7a 65 20  ** to recognize 
3da07 74 68 65 20 65 6e 64 20 6f 66 20 61 20 74 72 69  the end of a tri
3da08 67 67 65 72 20 63 61 6e 20 62 65 20 6f 6d 69 74  gger can be omit
3da09 74 65 64 2e 20 20 41 6c 6c 20 77 65 20 68 61 76  ted.  All we hav
3da0a 65 20 74 6f 20 64 6f 0a 2a 2a 20 69 73 20 6c 6f  e to do.** is lo
3da0b 6f 6b 20 66 6f 72 20 61 20 73 65 6d 69 63 6f 6c  ok for a semicol
3da0c 6f 6e 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70  on that is not p
3da0d 61 72 74 20 6f 66 20 61 6e 20 73 74 72 69 6e 67  art of an string
3da0e 20 6f 72 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a   or comment..*/.
3da0f 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3da10 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
3da11 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
3da12 29 7b 0a 20 20 75 38 20 73 74 61 74 65 20 3d 20  ){.  u8 state = 
3da13 30 3b 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20  0;   /* Current 
3da14 73 74 61 74 65 2c 20 75 73 69 6e 67 20 6e 75 6d  state, using num
3da15 62 65 72 73 20 64 65 66 69 6e 65 64 20 69 6e 20  bers defined in 
3da16 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 2a  header comment *
3da17 2f 0a 20 20 75 38 20 74 6f 6b 65 6e 3b 20 20 20  /.  u8 token;   
3da18 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20      /* Value of 
3da19 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 2a  the next token *
3da1a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
3da1b 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
3da1c 20 2f 2a 20 41 20 63 6f 6d 70 6c 65 78 20 73 74   /* A complex st
3da1d 61 74 65 6d 65 6e 74 20 6d 61 63 68 69 6e 65 20  atement machine 
3da1e 75 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 74  used to detect t
3da1f 68 65 20 65 6e 64 20 6f 66 20 61 20 43 52 45 41  he end of a CREA
3da20 54 45 20 54 52 49 47 47 45 52 0a 20 20 2a 2a 20  TE TRIGGER.  ** 
3da21 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 69 73  statement.  This
3da22 20 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 63   is the normal c
3da23 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74  ase..  */.  stat
3da24 69 63 20 63 6f 6e 73 74 20 75 38 20 74 72 61 6e  ic const u8 tran
3da25 73 5b 38 5d 5b 38 5d 20 3d 20 7b 0a 20 20 20 20  s[8][8] = {.    
3da26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3da27 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20 20 20 20 20   /* Token:      
3da28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3da29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3da2a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
3da2b 20 20 2f 2a 20 53 74 61 74 65 3a 20 20 20 20 20    /* State:     
3da2c 20 20 2a 2a 20 20 53 45 4d 49 20 20 57 53 20 20    **  SEMI  WS  
3da2d 4f 54 48 45 52 20 20 45 58 50 4c 41 49 4e 20 20  OTHER  EXPLAIN  
3da2e 43 52 45 41 54 45 20 20 54 45 4d 50 20 20 54 52  CREATE  TEMP  TR
3da2f 49 47 47 45 52 20 20 45 4e 44 20 2a 2f 0a 20 20  IGGER  END */.  
3da30 20 20 20 2f 2a 20 30 20 49 4e 56 41 4c 49 44 3a     /* 0 INVALID:
3da31 20 2a 2f 20 7b 20 20 20 20 31 2c 20 20 30 2c 20   */ {    1,  0, 
3da32 20 20 20 20 32 2c 20 20 20 20 20 20 20 33 2c 20      2,       3, 
3da33 20 20 20 20 20 34 2c 20 20 20 20 32 2c 20 20 20       4,    2,   
3da34 20 20 20 20 32 2c 20 20 20 32 2c 20 7d 2c 0a 20      2,   2, },. 
3da35 20 20 20 20 2f 2a 20 31 20 20 20 53 54 41 52 54      /* 1   START
3da36 3a 20 2a 2f 20 7b 20 20 20 20 31 2c 20 20 31 2c  : */ {    1,  1,
3da37 20 20 20 20 20 32 2c 20 20 20 20 20 20 20 33 2c       2,       3,
3da38 20 20 20 20 20 20 34 2c 20 20 20 20 32 2c 20 20        4,    2,  
3da39 20 20 20 20 20 32 2c 20 20 20 32 2c 20 7d 2c 0a       2,   2, },.
3da3a 20 20 20 20 20 2f 2a 20 32 20 20 4e 4f 52 4d 41       /* 2  NORMA
3da3b 4c 3a 20 2a 2f 20 7b 20 20 20 20 31 2c 20 20 32  L: */ {    1,  2
3da3c 2c 20 20 20 20 20 32 2c 20 20 20 20 20 20 20 32  ,     2,       2
3da3d 2c 20 20 20 20 20 20 32 2c 20 20 20 20 32 2c 20  ,      2,    2, 
3da3e 20 20 20 20 20 20 32 2c 20 20 20 32 2c 20 7d 2c        2,   2, },
3da3f 0a 20 20 20 20 20 2f 2a 20 33 20 45 58 50 4c 41  .     /* 3 EXPLA
3da40 49 4e 3a 20 2a 2f 20 7b 20 20 20 20 31 2c 20 20  IN: */ {    1,  
3da41 33 2c 20 20 20 20 20 33 2c 20 20 20 20 20 20 20  3,     3,       
3da42 32 2c 20 20 20 20 20 20 34 2c 20 20 20 20 32 2c  2,      4,    2,
3da43 20 20 20 20 20 20 20 32 2c 20 20 20 32 2c 20 7d         2,   2, }
3da44 2c 0a 20 20 20 20 20 2f 2a 20 34 20 20 43 52 45  ,.     /* 4  CRE
3da45 41 54 45 3a 20 2a 2f 20 7b 20 20 20 20 31 2c 20  ATE: */ {    1, 
3da46 20 34 2c 20 20 20 20 20 32 2c 20 20 20 20 20 20   4,     2,      
3da47 20 32 2c 20 20 20 20 20 20 32 2c 20 20 20 20 34   2,      2,    4
3da48 2c 20 20 20 20 20 20 20 35 2c 20 20 20 32 2c 20  ,       5,   2, 
3da49 7d 2c 0a 20 20 20 20 20 2f 2a 20 35 20 54 52 49  },.     /* 5 TRI
3da4a 47 47 45 52 3a 20 2a 2f 20 7b 20 20 20 20 36 2c  GGER: */ {    6,
3da4b 20 20 35 2c 20 20 20 20 20 35 2c 20 20 20 20 20    5,     5,     
3da4c 20 20 35 2c 20 20 20 20 20 20 35 2c 20 20 20 20    5,      5,    
3da4d 35 2c 20 20 20 20 20 20 20 35 2c 20 20 20 35 2c  5,       5,   5,
3da4e 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 36 20 20 20   },.     /* 6   
3da4f 20 53 45 4d 49 3a 20 2a 2f 20 7b 20 20 20 20 36   SEMI: */ {    6
3da50 2c 20 20 36 2c 20 20 20 20 20 35 2c 20 20 20 20  ,  6,     5,    
3da51 20 20 20 35 2c 20 20 20 20 20 20 35 2c 20 20 20     5,      5,   
3da52 20 35 2c 20 20 20 20 20 20 20 35 2c 20 20 20 37   5,       5,   7
3da53 2c 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 37 20 20  , },.     /* 7  
3da54 20 20 20 45 4e 44 3a 20 2a 2f 20 7b 20 20 20 20     END: */ {    
3da55 31 2c 20 20 37 2c 20 20 20 20 20 35 2c 20 20 20  1,  7,     5,   
3da56 20 20 20 20 35 2c 20 20 20 20 20 20 35 2c 20 20      5,      5,  
3da57 20 20 35 2c 20 20 20 20 20 20 20 35 2c 20 20 20    5,       5,   
3da58 35 2c 20 7d 2c 0a 20 20 7d 3b 0a 23 65 6c 73 65  5, },.  };.#else
3da59 0a 20 20 2f 2a 20 49 66 20 74 72 69 67 67 65 72  .  /* If trigger
3da5a 73 20 61 72 65 20 6e 6f 74 20 73 75 70 70 6f 72  s are not suppor
3da5b 74 65 64 20 62 79 20 74 68 69 73 20 63 6f 6d 70  ted by this comp
3da5c 69 6c 65 20 74 68 65 6e 20 74 68 65 20 73 74 61  ile then the sta
3da5d 74 65 6d 65 6e 74 20 6d 61 63 68 69 6e 65 0a 20  tement machine. 
3da5e 20 2a 2a 20 75 73 65 64 20 74 6f 20 64 65 74 65   ** used to dete
3da5f 63 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20  ct the end of a 
3da60 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6d 75 63  statement is muc
3da61 68 20 73 69 6d 70 6c 69 65 72 0a 20 20 2a 2f 0a  h simplier.  */.
3da62 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
3da63 38 20 74 72 61 6e 73 5b 33 5d 5b 33 5d 20 3d 20  8 trans[3][3] = 
3da64 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
3da65 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a         /* Token:
3da66 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
3da67 20 20 20 2f 2a 20 53 74 61 74 65 3a 20 20 20 20     /* State:    
3da68 20 20 20 2a 2a 20 20 53 45 4d 49 20 20 57 53 20     **  SEMI  WS 
3da69 20 4f 54 48 45 52 20 2a 2f 0a 20 20 20 20 20 2f   OTHER */.     /
3da6a 2a 20 30 20 49 4e 56 41 4c 49 44 3a 20 2a 2f 20  * 0 INVALID: */ 
3da6b 7b 20 20 20 20 31 2c 20 20 30 2c 20 20 20 20 20  {    1,  0,     
3da6c 32 2c 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 31 20  2, },.     /* 1 
3da6d 20 20 53 54 41 52 54 3a 20 2a 2f 20 7b 20 20 20    START: */ {   
3da6e 20 31 2c 20 20 31 2c 20 20 20 20 20 32 2c 20 7d   1,  1,     2, }
3da6f 2c 0a 20 20 20 20 20 2f 2a 20 32 20 20 4e 4f 52  ,.     /* 2  NOR
3da70 4d 41 4c 3a 20 2a 2f 20 7b 20 20 20 20 31 2c 20  MAL: */ {    1, 
3da71 20 32 2c 20 20 20 20 20 32 2c 20 7d 2c 0a 20 20   2,     2, },.  
3da72 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  };.#endif /* SQL
3da73 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
3da74 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20 2a 7a   */..  while( *z
3da75 53 71 6c 20 29 7b 0a 20 20 20 20 73 77 69 74 63  Sql ){.    switc
3da76 68 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20  h( *zSql ){.    
3da77 20 20 63 61 73 65 20 27 3b 27 3a 20 7b 20 20 2f    case ';': {  /
3da78 2a 20 41 20 73 65 6d 69 63 6f 6c 6f 6e 20 2a 2f  * A semicolon */
3da79 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d  .        token =
3da7a 20 74 6b 53 45 4d 49 3b 0a 20 20 20 20 20 20 20   tkSEMI;.       
3da7b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
3da7c 20 20 20 20 20 20 63 61 73 65 20 27 20 27 3a 0a        case ' ':.
3da7d 20 20 20 20 20 20 63 61 73 65 20 27 5c 72 27 3a        case '\r':
3da7e 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 74 27  .      case '\t'
3da7f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 6e  :.      case '\n
3da80 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c  ':.      case '\
3da81 66 27 3a 20 7b 20 20 2f 2a 20 57 68 69 74 65 20  f': {  /* White 
3da82 73 70 61 63 65 20 69 73 20 69 67 6e 6f 72 65 64  space is ignored
3da83 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 6f 6b 65   */.        toke
3da84 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20 20 20  n = tkWS;.      
3da85 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
3da86 0a 20 20 20 20 20 20 63 61 73 65 20 27 2f 27 3a  .      case '/':
3da87 20 7b 20 20 20 2f 2a 20 43 2d 73 74 79 6c 65 20   {   /* C-style 
3da88 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  comments */.    
3da89 20 20 20 20 69 66 28 20 7a 53 71 6c 5b 31 5d 21      if( zSql[1]!
3da8a 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='*' ){.        
3da8b 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
3da8c 52 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  R;.          bre
3da8d 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
3da8e 20 20 20 20 20 20 7a 53 71 6c 20 2b 3d 20 32 3b        zSql += 2;
3da8f 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
3da90 7a 53 71 6c 5b 30 5d 20 26 26 20 28 7a 53 71 6c  zSql[0] && (zSql
3da91 5b 30 5d 21 3d 27 2a 27 20 7c 7c 20 7a 53 71 6c  [0]!='*' || zSql
3da92 5b 31 5d 21 3d 27 2f 27 29 20 29 7b 20 7a 53 71  [1]!='/') ){ zSq
3da93 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 69  l++; }.        i
3da94 66 28 20 7a 53 71 6c 5b 30 5d 3d 3d 30 20 29 20  f( zSql[0]==0 ) 
3da95 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
3da96 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20    zSql++;.      
3da97 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a    token = tkWS;.
3da98 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3da99 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
3da9a 65 20 27 2d 27 3a 20 7b 20 20 20 2f 2a 20 53 51  e '-': {   /* SQ
3da9b 4c 2d 73 74 79 6c 65 20 63 6f 6d 6d 65 6e 74 73  L-style comments
3da9c 20 66 72 6f 6d 20 22 2d 2d 22 20 74 6f 20 65 6e   from "--" to en
3da9d 64 20 6f 66 20 6c 69 6e 65 20 2a 2f 0a 20 20 20  d of line */.   
3da9e 20 20 20 20 20 69 66 28 20 7a 53 71 6c 5b 31 5d       if( zSql[1]
3da9f 21 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20  !='-' ){.       
3daa0 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48     token = tkOTH
3daa1 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  ER;.          br
3daa2 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
3daa3 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a         while( *z
3daa4 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d 27 5c  Sql && *zSql!='\
3daa5 6e 27 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a  n' ){ zSql++; }.
3daa6 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a 53 71          if( *zSq
3daa7 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 73 74  l==0 ) return st
3daa8 61 74 65 3d 3d 31 3b 0a 20 20 20 20 20 20 20 20  ate==1;.        
3daa9 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20  token = tkWS;.  
3daaa 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3daab 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
3daac 27 5b 27 3a 20 7b 20 20 20 2f 2a 20 4d 69 63 72  '[': {   /* Micr
3daad 6f 73 6f 66 74 2d 73 74 79 6c 65 20 69 64 65 6e  osoft-style iden
3daae 74 69 66 69 65 72 73 20 69 6e 20 5b 2e 2e 2e 5d  tifiers in [...]
3daaf 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 53 71 6c   */.        zSql
3dab0 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  ++;.        whil
3dab1 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71  e( *zSql && *zSq
3dab2 6c 21 3d 27 5d 27 20 29 7b 20 7a 53 71 6c 2b 2b  l!=']' ){ zSql++
3dab3 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
3dab4 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74 75 72  *zSql==0 ) retur
3dab5 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b  n 0;.        tok
3dab6 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20  en = tkOTHER;.  
3dab7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3dab8 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
3dab9 27 60 27 3a 20 20 20 20 20 2f 2a 20 47 72 61 76  '`':     /* Grav
3daba 65 2d 61 63 63 65 6e 74 20 71 75 6f 74 65 64 20  e-accent quoted 
3dabb 73 79 6d 62 6f 6c 73 20 75 73 65 64 20 62 79 20  symbols used by 
3dabc 4d 79 53 51 4c 20 2a 2f 0a 20 20 20 20 20 20 63  MySQL */.      c
3dabd 61 73 65 20 27 22 27 3a 20 20 20 20 20 2f 2a 20  ase '"':     /* 
3dabe 73 69 6e 67 6c 65 2d 20 61 6e 64 20 64 6f 75 62  single- and doub
3dabf 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67  le-quoted string
3dac0 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  s */.      case 
3dac1 27 5c 27 27 3a 20 7b 0a 20 20 20 20 20 20 20 20  '\'': {.        
3dac2 69 6e 74 20 63 20 3d 20 2a 7a 53 71 6c 3b 0a 20  int c = *zSql;. 
3dac3 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20         zSql++;. 
3dac4 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a         while( *z
3dac5 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d 63 20  Sql && *zSql!=c 
3dac6 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20  ){ zSql++; }.   
3dac7 20 20 20 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d       if( *zSql==
3dac8 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
3dac9 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
3daca 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 62  OTHER;.        b
3dacb 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
3dacc 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 23      default: {.#
3dacd 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43  ifdef SQLITE_EBC
3dace 44 49 43 0a 20 20 20 20 20 20 20 20 75 6e 73 69  DIC.        unsi
3dacf 67 6e 65 64 20 63 68 61 72 20 63 3b 0a 23 65 6e  gned char c;.#en
3dad0 64 69 66 0a 20 20 20 20 20 20 20 20 69 66 28 20  dif.        if( 
3dad1 49 64 43 68 61 72 28 28 75 38 29 2a 7a 53 71 6c  IdChar((u8)*zSql
3dad2 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  ) ){.          /
3dad3 2a 20 4b 65 79 77 6f 72 64 73 20 61 6e 64 20 75  * Keywords and u
3dad4 6e 71 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69  nquoted identifi
3dad5 65 72 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ers */.         
3dad6 20 69 6e 74 20 6e 49 64 3b 0a 20 20 20 20 20 20   int nId;.      
3dad7 20 20 20 20 66 6f 72 28 6e 49 64 3d 31 3b 20 49      for(nId=1; I
3dad8 64 43 68 61 72 28 7a 53 71 6c 5b 6e 49 64 5d 29  dChar(zSql[nId])
3dad9 3b 20 6e 49 64 2b 2b 29 7b 7d 0a 23 69 66 64 65  ; nId++){}.#ifde
3dada 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
3dadb 49 47 47 45 52 0a 20 20 20 20 20 20 20 20 20 20  IGGER.          
3dadc 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b  token = tkOTHER;
3dadd 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20  .#else.         
3dade 20 73 77 69 74 63 68 28 20 2a 7a 53 71 6c 20 29   switch( *zSql )
3dadf 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 61  {.            ca
3dae0 73 65 20 27 63 27 3a 20 63 61 73 65 20 27 43 27  se 'c': case 'C'
3dae1 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
3dae2 20 20 69 66 28 20 6e 49 64 3d 3d 36 20 26 26 20    if( nId==6 && 
3dae3 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
3dae4 7a 53 71 6c 2c 20 22 63 72 65 61 74 65 22 2c 20  zSql, "create", 
3dae5 36 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  6)==0 ){.       
3dae6 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d           token =
3dae7 20 74 6b 43 52 45 41 54 45 3b 0a 20 20 20 20 20   tkCREATE;.     
3dae8 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
3dae9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3daea 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b  token = tkOTHER;
3daeb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
3daec 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  .              b
3daed 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
3daee 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
3daef 63 61 73 65 20 27 74 27 3a 20 63 61 73 65 20 27  case 't': case '
3daf0 54 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  T': {.          
3daf1 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 37 20 26      if( nId==7 &
3daf2 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  & sqlite3StrNICm
3daf3 70 28 7a 53 71 6c 2c 20 22 74 72 69 67 67 65 72  p(zSql, "trigger
3daf4 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ", 7)==0 ){.    
3daf5 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65              toke
3daf6 6e 20 3d 20 74 6b 54 52 49 47 47 45 52 3b 0a 20  n = tkTRIGGER;. 
3daf7 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c               }el
3daf8 73 65 20 69 66 28 20 6e 49 64 3d 3d 34 20 26 26  se if( nId==4 &&
3daf9 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
3dafa 28 7a 53 71 6c 2c 20 22 74 65 6d 70 22 2c 20 34  (zSql, "temp", 4
3dafb 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
3dafc 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
3dafd 74 6b 54 45 4d 50 3b 0a 20 20 20 20 20 20 20 20  tkTEMP;.        
3dafe 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
3daff 6e 49 64 3d 3d 39 20 26 26 20 73 71 6c 69 74 65  nId==9 && sqlite
3db00 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20  3StrNICmp(zSql, 
3db01 22 74 65 6d 70 6f 72 61 72 79 22 2c 20 39 29 3d  "temporary", 9)=
3db02 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
3db03 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
3db04 54 45 4d 50 3b 0a 20 20 20 20 20 20 20 20 20 20  TEMP;.          
3db05 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3db06 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e             token
3db07 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20   = tkOTHER;.    
3db08 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
3db09 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
3db0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
3db0b 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20             case 
3db0c 27 65 27 3a 20 20 63 61 73 65 20 27 45 27 3a 20  'e':  case 'E': 
3db0d 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
3db0e 69 66 28 20 6e 49 64 3d 3d 33 20 26 26 20 73 71  if( nId==3 && sq
3db0f 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53  lite3StrNICmp(zS
3db10 71 6c 2c 20 22 65 6e 64 22 2c 20 33 29 3d 3d 30  ql, "end", 3)==0
3db11 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
3db12 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 45 4e      token = tkEN
3db13 44 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  D;.             
3db14 20 7d 65 6c 73 65 0a 23 69 66 6e 64 65 66 20 53   }else.#ifndef S
3db15 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
3db16 49 4e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  IN.             
3db17 20 69 66 28 20 6e 49 64 3d 3d 37 20 26 26 20 73   if( nId==7 && s
3db18 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a  qlite3StrNICmp(z
3db19 53 71 6c 2c 20 22 65 78 70 6c 61 69 6e 22 2c 20  Sql, "explain", 
3db1a 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  7)==0 ){.       
3db1b 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d           token =
3db1c 20 74 6b 45 58 50 4c 41 49 4e 3b 0a 20 20 20 20   tkEXPLAIN;.    
3db1d 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a            }else.
3db1e 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20  #endif.         
3db1f 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20       {.         
3db20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
3db21 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20  kOTHER;.        
3db22 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
3db23 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3db24 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
3db25 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20         default: 
3db26 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
3db27 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b  token = tkOTHER;
3db28 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  .              b
3db29 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
3db2a 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
3db2b 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
3db2c 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f  _OMIT_TRIGGER */
3db2d 0a 20 20 20 20 20 20 20 20 20 20 7a 53 71 6c 20  .          zSql 
3db2e 2b 3d 20 6e 49 64 2d 31 3b 0a 20 20 20 20 20 20  += nId-1;.      
3db2f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
3db30 20 20 20 2f 2a 20 4f 70 65 72 61 74 6f 72 73 20     /* Operators 
3db31 61 6e 64 20 73 70 65 63 69 61 6c 20 73 79 6d 62  and special symb
3db32 6f 6c 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ols */.         
3db33 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
3db34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
3db35 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3db36 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 74 61   }.    }.    sta
3db37 74 65 20 3d 20 74 72 61 6e 73 5b 73 74 61 74 65  te = trans[state
3db38 5d 5b 74 6f 6b 65 6e 5d 3b 0a 20 20 20 20 7a 53  ][token];.    zS
3db39 71 6c 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ql++;.  }.  retu
3db3a 72 6e 20 73 74 61 74 65 3d 3d 31 3b 0a 7d 0a 0a  rn state==1;.}..
3db3b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
3db3c 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20  MIT_UTF16./*.** 
3db3d 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
3db3e 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
3db3f 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
3db40 28 29 20 72 6f 75 74 69 6e 65 20 64 65 73 63 72  () routine descr
3db41 69 62 65 64 0a 2a 2a 20 61 62 6f 76 65 2c 20 65  ibed.** above, e
3db42 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 70  xcept that the p
3db43 61 72 61 6d 65 74 65 72 20 69 73 20 72 65 71 75  arameter is requ
3db44 69 72 65 64 20 74 6f 20 62 65 20 55 54 46 2d 31  ired to be UTF-1
3db45 36 20 65 6e 63 6f 64 65 64 2c 20 6e 6f 74 0a 2a  6 encoded, not.*
3db46 2a 20 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49  * UTF-8..*/.SQLI
3db47 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3db48 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 63 6f  e3_complete16(co
3db49 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 29 7b  nst void *zSql){
3db4a 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
3db4b 20 2a 70 56 61 6c 3b 0a 20 20 63 68 61 72 20 63   *pVal;.  char c
3db4c 6f 6e 73 74 20 2a 7a 53 71 6c 38 3b 0a 20 20 69  onst *zSql8;.  i
3db4d 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  nt rc = SQLITE_N
3db4e 4f 4d 45 4d 3b 0a 0a 23 69 66 6e 64 65 66 20 53  OMEM;..#ifndef S
3db4f 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
3db50 4e 49 54 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  NIT.  rc = sqlit
3db51 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b  e3_initialize();
3db52 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
3db53 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20  rn rc;.#endif.  
3db54 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61  pVal = sqlite3Va
3db55 6c 75 65 4e 65 77 28 30 29 3b 0a 20 20 73 71 6c  lueNew(0);.  sql
3db56 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
3db57 70 56 61 6c 2c 20 2d 31 2c 20 7a 53 71 6c 2c 20  pVal, -1, zSql, 
3db58 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
3db59 56 45 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  VE, SQLITE_STATI
3db5a 43 29 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73 71  C);.  zSql8 = sq
3db5b 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70  lite3ValueText(p
3db5c 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Val, SQLITE_UTF8
3db5d 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 38 20 29  );.  if( zSql8 )
3db5e 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
3db5f 65 33 5f 63 6f 6d 70 6c 65 74 65 28 7a 53 71 6c  e3_complete(zSql
3db60 38 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  8);.  }else{.   
3db61 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
3db62 45 4d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  EM;.  }.  sqlite
3db63 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29  3ValueFree(pVal)
3db64 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ;.  return sqlit
3db65 65 33 41 70 69 45 78 69 74 28 30 2c 20 72 63 29  e3ApiExit(0, rc)
3db66 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
3db67 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
3db68 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  */.#endif /* SQL
3db69 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54  ITE_OMIT_COMPLET
3db6a 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  E */../*********
3db6b 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 63 6f 6d  ***** End of com
3db6c 70 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  plete.c ********
3db6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3db6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3db6f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
3db70 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
3db71 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   main.c ********
3db72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3db73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3db74 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
3db75 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
3db76 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
3db77 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
3db78 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
3db79 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
3db7a 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
3db7b 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
3db7c 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
3db7d 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
3db7e 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
3db7f 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
3db80 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
3db81 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
3db82 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
3db83 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
3db84 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
3db85 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
3db86 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
3db87 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
3db88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3db89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3db8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3db8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
3db8c 4d 61 69 6e 20 66 69 6c 65 20 66 6f 72 20 74 68  Main file for th
3db8d 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
3db8e 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20  .  The routines 
3db8f 69 6e 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20  in this file.** 
3db90 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72  implement the pr
3db91 6f 67 72 61 6d 6d 65 72 20 69 6e 74 65 72 66 61  ogrammer interfa
3db92 63 65 20 74 6f 20 74 68 65 20 6c 69 62 72 61 72  ce to the librar
3db93 79 2e 20 20 52 6f 75 74 69 6e 65 73 20 69 6e 0a  y.  Routines in.
3db94 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 73 20 61  ** other files a
3db95 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20  re for internal 
3db96 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 61 6e  use by SQLite an
3db97 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a  d should not be.
3db98 2a 2a 20 61 63 63 65 73 73 65 64 20 62 79 20 75  ** accessed by u
3db99 73 65 72 73 20 6f 66 20 74 68 65 20 6c 69 62 72  sers of the libr
3db9a 61 72 79 2e 0a 2a 2f 0a 0a 23 69 66 64 65 66 20  ary..*/..#ifdef 
3db9b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
3db9c 53 33 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  S3./************
3db9d 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74 73 33 2e  ** Include fts3.
3db9e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
3db9f 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a  of main.c ******
3dba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dba1 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
3dba2 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74  ** Begin file ft
3dba3 73 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s3.h ***********
3dba4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dba5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dba6 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f 63  */./*.** 2006 Oc
3dba7 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 10.**.** The a
3dba8 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
3dba9 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
3dbaa 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
3dbab 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
3dbac 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
3dbad 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
3dbae 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
3dbaf 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
3dbb0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
3dbb1 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
3dbb2 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
3dbb3 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
3dbb4 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
3dbb5 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
3dbb6 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
3dbb7 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
3dbb8 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
3dbb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbbb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbbc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbbd 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
3dbbe 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20  his header file 
3dbbf 69 73 20 75 73 65 64 20 62 79 20 70 72 6f 67 72  is used by progr
3dbc0 61 6d 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f  ams that want to
3dbc1 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68   link against th
3dbc2 65 0a 2a 2a 20 46 54 53 33 20 6c 69 62 72 61 72  e.** FTS3 librar
3dbc3 79 2e 20 20 41 6c 6c 20 69 74 20 64 6f 65 73 20  y.  All it does 
3dbc4 69 73 20 64 65 63 6c 61 72 65 20 74 68 65 20 73  is declare the s
3dbc5 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 29  qlite3Fts3Init()
3dbc6 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a   interface..*/..
3dbc7 23 69 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22  #if 0.extern "C"
3dbc8 20 7b 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f   {.#endif  /* __
3dbc9 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 53 51  cplusplus */..SQ
3dbca 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
3dbcb 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74   sqlite3Fts3Init
3dbcc 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 0a  (sqlite3 *db);..
3dbcd 23 69 66 20 30 0a 7d 20 20 2f 2a 20 65 78 74 65  #if 0.}  /* exte
3dbce 72 6e 20 22 43 22 20 2a 2f 0a 23 65 6e 64 69 66  rn "C" */.#endif
3dbcf 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73    /* __cplusplus
3dbd0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
3dbd1 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33  **** End of fts3
3dbd2 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
3dbd3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbd4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbd5 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
3dbd6 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
3dbd7 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
3dbd8 66 20 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a  f in main.c ****
3dbd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbda 2a 2a 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64  ***/.#endif.#ifd
3dbdb 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
3dbdc 5f 52 54 52 45 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  _RTREE./********
3dbdd 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 72  ****** Include r
3dbde 74 72 65 65 2e 68 20 69 6e 20 74 68 65 20 6d 69  tree.h in the mi
3dbdf 64 64 6c 65 20 6f 66 20 6d 61 69 6e 2e 63 20 2a  ddle of main.c *
3dbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbe1 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
3dbe2 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
3dbe3 65 20 72 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a  e rtree.h ******
3dbe4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbe5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbe6 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
3dbe7 38 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54  8 May 26.**.** T
3dbe8 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
3dbe9 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
3dbea 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
3dbeb 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
3dbec 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
3dbed 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
3dbee 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
3dbef 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
3dbf0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
3dbf1 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
3dbf2 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
3dbf3 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
3dbf4 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
3dbf5 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
3dbf6 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
3dbf7 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
3dbf8 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
3dbf9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbfa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dbfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
3dbfe 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
3dbff 69 6c 65 20 69 73 20 75 73 65 64 20 62 79 20 70  ile is used by p
3dc00 72 6f 67 72 61 6d 73 20 74 68 61 74 20 77 61 6e  rograms that wan
3dc01 74 20 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e 73  t to link agains
3dc02 74 20 74 68 65 0a 2a 2a 20 52 54 52 45 45 20 6c  t the.** RTREE l
3dc03 69 62 72 61 72 79 2e 20 20 41 6c 6c 20 69 74 20  ibrary.  All it 
3dc04 64 6f 65 73 20 69 73 20 64 65 63 6c 61 72 65 20  does is declare 
3dc05 74 68 65 20 73 71 6c 69 74 65 33 52 74 72 65 65  the sqlite3Rtree
3dc06 49 6e 69 74 28 29 20 69 6e 74 65 72 66 61 63 65  Init() interface
3dc07 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74 65  ..*/..#if 0.exte
3dc08 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 20  rn "C" {.#endif 
3dc09 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20   /* __cplusplus 
3dc0a 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
3dc0b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 74  TE int sqlite3Rt
3dc0c 72 65 65 49 6e 69 74 28 73 71 6c 69 74 65 33 20  reeInit(sqlite3 
3dc0d 2a 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20  *db);..#if 0.}  
3dc0e 2f 2a 20 65 78 74 65 72 6e 20 22 43 22 20 2a 2f  /* extern "C" */
3dc0f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70  .#endif  /* __cp
3dc10 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 2f 2a 2a 2a  lusplus */../***
3dc11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
3dc12 6f 66 20 72 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a  of rtree.h *****
3dc13 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dc14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dc15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
3dc16 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
3dc17 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
3dc18 6c 65 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e  left off in main
3dc19 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
3dc1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64  **********/.#end
3dc1b 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
3dc1c 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 2f 2a 2a 2a  _ENABLE_ICU./***
3dc1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
3dc1e 75 64 65 20 73 71 6c 69 74 65 69 63 75 2e 68 20  ude sqliteicu.h 
3dc1f 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
3dc20 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   main.c ********
3dc21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
3dc22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
3dc23 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 69 63 75  n file sqliteicu
3dc24 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
3dc25 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dc26 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
3dc27 2a 20 32 30 30 38 20 4d 61 79 20 32 36 0a 2a 2a  * 2008 May 26.**
3dc28 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
3dc29 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
3dc2a 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
3dc2b 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
3dc2c 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
3dc2d 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
3dc2e 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
3dc2f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
3dc30 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
3dc31 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
3dc32 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
3dc33 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
3dc34 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
3dc35 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
3dc36 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
3dc37 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
3dc38 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
3dc39 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
3dc3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dc3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dc3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dc3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dc3e 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61  *.**.** This hea
3dc3f 64 65 72 20 66 69 6c 65 20 69 73 20 75 73 65 64  der file is used
3dc40 20 62 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61   by programs tha
3dc41 74 20 77 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61  t want to link a
3dc42 67 61 69 6e 73 74 20 74 68 65 0a 2a 2a 20 49 43  gainst the.** IC
3dc43 55 20 65 78 74 65 6e 73 69 6f 6e 2e 20 20 41 6c  U extension.  Al
3dc44 6c 20 69 74 20 64 6f 65 73 20 69 73 20 64 65 63  l it does is dec
3dc45 6c 61 72 65 20 74 68 65 20 73 71 6c 69 74 65 33  lare the sqlite3
3dc46 49 63 75 49 6e 69 74 28 29 20 69 6e 74 65 72 66  IcuInit() interf
3dc47 61 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65  ace..*/..#if 0.e
3dc48 78 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64  xtern "C" {.#end
3dc49 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c  if  /* __cpluspl
3dc4a 75 73 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52  us */..SQLITE_PR
3dc4b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
3dc4c 33 49 63 75 49 6e 69 74 28 73 71 6c 69 74 65 33  3IcuInit(sqlite3
3dc4d 20 2a 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20   *db);..#if 0.} 
3dc4e 20 2f 2a 20 65 78 74 65 72 6e 20 22 43 22 20 2a   /* extern "C" *
3dc4f 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63  /.#endif  /* __c
3dc50 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 0a 2f 2a  plusplus */.../*
3dc51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
3dc52 64 20 6f 66 20 73 71 6c 69 74 65 69 63 75 2e 68  d of sqliteicu.h
3dc53 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
3dc54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dc55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
3dc56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
3dc57 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
3dc58 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6d 61  e left off in ma
3dc59 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
3dc5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65  ************/.#e
3dc5b 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
3dc5c 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f  LITE_AMALGAMATIO
3dc5d 4e 0a 2f 2a 20 49 4d 50 4c 45 4d 45 4e 54 41 54  N./* IMPLEMENTAT
3dc5e 49 4f 4e 2d 4f 46 3a 20 52 2d 34 36 36 35 36 2d  ION-OF: R-46656-
3dc5f 34 35 31 35 36 20 54 68 65 20 73 71 6c 69 74 65  45156 The sqlite
3dc60 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 73 74 72 69  3_version[] stri
3dc61 6e 67 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 63  ng constant.** c
3dc62 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 65 78 74  ontains the text
3dc63 20 6f 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49   of SQLITE_VERSI
3dc64 4f 4e 20 6d 61 63 72 6f 2e 20 0a 2a 2f 0a 53 51  ON macro. .*/.SQ
3dc65 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
3dc66 68 61 72 20 73 71 6c 69 74 65 33 5f 76 65 72 73  har sqlite3_vers
3dc67 69 6f 6e 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 56  ion[] = SQLITE_V
3dc68 45 52 53 49 4f 4e 3b 0a 23 65 6e 64 69 66 0a 0a  ERSION;.#endif..
3dc69 2f 2a 20 49 4d 50 4c 45 4d 45 4e 54 41 54 49 4f  /* IMPLEMENTATIO
3dc6a 4e 2d 4f 46 3a 20 52 2d 35 33 35 33 36 2d 34 32  N-OF: R-53536-42
3dc6b 35 37 35 20 54 68 65 20 73 71 6c 69 74 65 33 5f  575 The sqlite3_
3dc6c 6c 69 62 76 65 72 73 69 6f 6e 28 29 20 66 75 6e  libversion() fun
3dc6d 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 0a 2a 2a  ction returns.**
3dc6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
3dc6f 65 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  e to the sqlite3
3dc70 5f 76 65 72 73 69 6f 6e 5b 5d 20 73 74 72 69 6e  _version[] strin
3dc71 67 20 63 6f 6e 73 74 61 6e 74 2e 20 0a 2a 2f 0a  g constant. .*/.
3dc72 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
3dc73 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c   char *sqlite3_l
3dc74 69 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 7b  ibversion(void){
3dc75 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
3dc76 76 65 72 73 69 6f 6e 3b 20 7d 0a 0a 2f 2a 20 49  version; }../* I
3dc77 4d 50 4c 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46  MPLEMENTATION-OF
3dc78 3a 20 52 2d 36 33 31 32 34 2d 33 39 33 30 30 20  : R-63124-39300 
3dc79 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6f 75 72  The sqlite3_sour
3dc7a 63 65 69 64 28 29 20 66 75 6e 63 74 69 6f 6e 20  ceid() function 
3dc7b 72 65 74 75 72 6e 73 20 61 0a 2a 2a 20 70 6f 69  returns a.** poi
3dc7c 6e 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67  nter to a string
3dc7d 20 63 6f 6e 73 74 61 6e 74 20 77 68 6f 73 65 20   constant whose 
3dc7e 76 61 6c 75 65 20 69 73 20 74 68 65 20 73 61 6d  value is the sam
3dc7f 65 20 61 73 20 74 68 65 0a 2a 2a 20 53 51 4c 49  e as the.** SQLI
3dc80 54 45 5f 53 4f 55 52 43 45 5f 49 44 20 43 20 70  TE_SOURCE_ID C p
3dc81 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72  reprocessor macr
3dc82 6f 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  o. .*/.SQLITE_AP
3dc83 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
3dc84 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 76  lite3_sourceid(v
3dc85 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c  oid){ return SQL
3dc86 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 3b 20 7d  ITE_SOURCE_ID; }
3dc87 0a 0a 2f 2a 20 49 4d 50 4c 45 4d 45 4e 54 41 54  ../* IMPLEMENTAT
3dc88 49 4f 4e 2d 4f 46 3a 20 52 2d 33 35 32 31 30 2d  ION-OF: R-35210-
3dc89 36 33 35 30 38 20 54 68 65 20 73 71 6c 69 74 65  63508 The sqlite
3dc8a 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d  3_libversion_num
3dc8b 62 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a  ber() function.*
3dc8c 2a 20 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74  * returns an int
3dc8d 65 67 65 72 20 65 71 75 61 6c 20 74 6f 20 53 51  eger equal to SQ
3dc8e 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d  LITE_VERSION_NUM
3dc8f 42 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  BER..*/.SQLITE_A
3dc90 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c  PI int sqlite3_l
3dc91 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
3dc92 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53  (void){ return S
3dc93 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
3dc94 4d 42 45 52 3b 20 7d 0a 0a 2f 2a 20 49 4d 50 4c  MBER; }../* IMPL
3dc95 45 4d 45 4e 54 41 54 49 4f 4e 2d 4f 46 3a 20 52  EMENTATION-OF: R
3dc96 2d 32 30 37 39 30 2d 31 34 30 32 35 20 54 68 65  -20790-14025 The
3dc97 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73   sqlite3_threads
3dc98 61 66 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 72  afe() function r
3dc99 65 74 75 72 6e 73 0a 2a 2a 20 7a 65 72 6f 20 69  eturns.** zero i
3dc9a 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 53 51  f and only if SQ
3dc9b 4c 69 74 65 20 77 61 73 20 63 6f 6d 70 69 6c 65  Lite was compile
3dc9c 64 20 77 69 74 68 20 6d 75 74 65 78 69 6e 67 20  d with mutexing 
3dc9d 63 6f 64 65 20 6f 6d 69 74 74 65 64 20 64 75 65  code omitted due
3dc9e 20 74 6f 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54   to.** the SQLIT
3dc9f 45 5f 54 48 52 45 41 44 53 41 46 45 20 63 6f 6d  E_THREADSAFE com
3dca0 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e  pile-time option
3dca1 20 62 65 69 6e 67 20 73 65 74 20 74 6f 20 30 2e   being set to 0.
3dca2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
3dca3 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  nt sqlite3_threa
3dca4 64 73 61 66 65 28 76 6f 69 64 29 7b 20 72 65 74  dsafe(void){ ret
3dca5 75 72 6e 20 53 51 4c 49 54 45 5f 54 48 52 45 41  urn SQLITE_THREA
3dca6 44 53 41 46 45 3b 20 7d 0a 0a 23 69 66 20 21 64  DSAFE; }..#if !d
3dca7 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
3dca8 49 54 5f 54 52 41 43 45 29 20 26 26 20 64 65 66  IT_TRACE) && def
3dca9 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
3dcaa 4c 45 5f 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a  LE_IOTRACE)./*.*
3dcab 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * If the followi
3dcac 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e  ng function poin
3dcad 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20  ter is not NULL 
3dcae 61 6e 64 20 69 66 0a 2a 2a 20 53 51 4c 49 54 45  and if.** SQLITE
3dcaf 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20  _ENABLE_IOTRACE 
3dcb0 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e  is enabled, then
3dcb1 20 6d 65 73 73 61 67 65 73 20 64 65 73 63 72 69   messages descri
3dcb2 62 69 6e 67 0a 2a 2a 20 49 2f 4f 20 61 63 74 69  bing.** I/O acti
3dcb3 76 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 75  ve are written u
3dcb4 73 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69  sing this functi
3dcb5 6f 6e 2e 20 20 54 68 65 73 65 20 6d 65 73 73 61  on.  These messa
3dcb6 67 65 73 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e  ges.** are inten
3dcb7 64 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e  ded for debuggin
3dcb8 67 20 61 63 74 69 76 69 74 79 20 6f 6e 6c 79 2e  g activity only.
3dcb9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
3dcba 54 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65  TE void (*sqlite
3dcbb 33 49 6f 54 72 61 63 65 29 28 63 6f 6e 73 74 20  3IoTrace)(const 
3dcbc 63 68 61 72 2a 2c 20 2e 2e 2e 29 20 3d 20 30 3b  char*, ...) = 0;
3dcbd 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
3dcbe 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
3dcbf 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
3dcc0 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 72 69  points to a stri
3dcc1 6e 67 20 77 68 69 63 68 20 69 73 20 74 68 65 0a  ng which is the.
3dcc2 2a 2a 20 6e 61 6d 65 20 6f 66 20 61 20 64 69 72  ** name of a dir
3dcc3 65 63 74 6f 72 79 2c 20 74 68 65 6e 20 74 68 61  ectory, then tha
3dcc4 74 20 64 69 72 65 63 74 6f 72 79 20 77 69 6c 6c  t directory will
3dcc5 20 62 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72   be used to stor
3dcc6 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66  e.** temporary f
3dcc7 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  iles..**.** See 
3dcc8 61 6c 73 6f 20 74 68 65 20 22 50 52 41 47 4d 41  also the "PRAGMA
3dcc9 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65   temp_store_dire
3dcca 63 74 6f 72 79 22 20 53 51 4c 20 63 6f 6d 6d 61  ctory" SQL comma
3dccb 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  nd..*/.SQLITE_AP
3dccc 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  I char *sqlite3_
3dccd 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d  temp_directory =
3dcce 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68   0;../*.** If th
3dccf 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62  e following glob
3dcd0 61 6c 20 76 61 72 69 61 62 6c 65 20 70 6f 69 6e  al variable poin
3dcd1 74 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77  ts to a string w
3dcd2 68 69 63 68 20 69 73 20 74 68 65 0a 2a 2a 20 6e  hich is the.** n
3dcd3 61 6d 65 20 6f 66 20 61 20 64 69 72 65 63 74 6f  ame of a directo
3dcd4 72 79 2c 20 74 68 65 6e 20 74 68 61 74 20 64 69  ry, then that di
3dcd5 72 65 63 74 6f 72 79 20 77 69 6c 6c 20 62 65 20  rectory will be 
3dcd6 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a  used to store.**
3dcd7 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69   all database fi
3dcd8 6c 65 73 20 73 70 65 63 69 66 69 65 64 20 77 69  les specified wi
3dcd9 74 68 20 61 20 72 65 6c 61 74 69 76 65 20 70 61  th a relative pa
3dcda 74 68 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 53 65  thname..**.** Se
3dcdb 65 20 61 6c 73 6f 20 74 68 65 20 22 50 52 41 47  e also the "PRAG
3dcdc 4d 41 20 64 61 74 61 5f 73 74 6f 72 65 5f 64 69  MA data_store_di
3dcdd 72 65 63 74 6f 72 79 22 20 53 51 4c 20 63 6f 6d  rectory" SQL com
3dcde 6d 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  mand..*/.SQLITE_
3dcdf 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65  API char *sqlite
3dce0 33 5f 64 61 74 61 5f 64 69 72 65 63 74 6f 72 79  3_data_directory
3dce1 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69   = 0;../*.** Ini
3dce2 74 69 61 6c 69 7a 65 20 53 51 4c 69 74 65 2e 20  tialize SQLite. 
3dce3 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75   .**.** This rou
3dce4 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c  tine must be cal
3dce5 6c 65 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a  led to initializ
3dce6 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  e the memory all
3dce7 6f 63 61 74 69 6f 6e 2c 0a 2a 2a 20 56 46 53 2c  ocation,.** VFS,
3dce8 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62 73 79   and mutex subsy
3dce9 73 74 65 6d 73 20 70 72 69 6f 72 20 74 6f 20 64  stems prior to d
3dcea 6f 69 6e 67 20 61 6e 79 20 73 65 72 69 6f 75 73  oing any serious
3dceb 20 77 6f 72 6b 20 77 69 74 68 0a 2a 2a 20 53 51   work with.** SQ
3dcec 4c 69 74 65 2e 20 20 42 75 74 20 61 73 20 6c 6f  Lite.  But as lo
3dced 6e 67 20 61 73 20 79 6f 75 20 64 6f 20 6e 6f 74  ng as you do not
3dcee 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 53 51   compile with SQ
3dcef 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
3dcf0 49 54 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  IT.** this routi
3dcf1 6e 65 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65  ne will be calle
3dcf2 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  d automatically 
3dcf3 62 79 20 6b 65 79 20 72 6f 75 74 69 6e 65 73 20  by key routines 
3dcf4 73 75 63 68 20 61 73 0a 2a 2a 20 73 71 6c 69 74  such as.** sqlit
3dcf5 65 33 5f 6f 70 65 6e 28 29 2e 20 20 0a 2a 2a 0a  e3_open().  .**.
3dcf6 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
3dcf7 69 73 20 61 20 6e 6f 2d 6f 70 20 65 78 63 65 70  is a no-op excep
3dcf8 74 20 6f 6e 20 69 74 73 20 76 65 72 79 20 66 69  t on its very fi
3dcf9 72 73 74 20 63 61 6c 6c 20 66 6f 72 20 74 68 65  rst call for the
3dcfa 20 70 72 6f 63 65 73 73 2c 0a 2a 2a 20 6f 72 20   process,.** or 
3dcfb 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 63 61  for the first ca
3dcfc 6c 6c 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20  ll after a call 
3dcfd 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  to sqlite3_shutd
3dcfe 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  own..**.** The f
3dcff 69 72 73 74 20 74 68 72 65 61 64 20 74 6f 20 63  irst thread to c
3dd00 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  all this routine
3dd01 20 72 75 6e 73 20 74 68 65 20 69 6e 69 74 69 61   runs the initia
3dd02 6c 69 7a 61 74 69 6f 6e 20 74 6f 0a 2a 2a 20 63  lization to.** c
3dd03 6f 6d 70 6c 65 74 69 6f 6e 2e 20 20 49 66 20 73  ompletion.  If s
3dd04 75 62 73 65 71 75 65 6e 74 20 74 68 72 65 61 64  ubsequent thread
3dd05 73 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  s call this rout
3dd06 69 6e 65 20 62 65 66 6f 72 65 20 74 68 65 20 66  ine before the f
3dd07 69 72 73 74 0a 2a 2a 20 74 68 72 65 61 64 20 68  irst.** thread h
3dd08 61 73 20 66 69 6e 69 73 68 65 64 20 74 68 65 20  as finished the 
3dd09 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 70  initialization p
3dd0a 72 6f 63 65 73 73 2c 20 74 68 65 6e 20 74 68 65  rocess, then the
3dd0b 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 74   subsequent.** t
3dd0c 68 72 65 61 64 73 20 6d 75 73 74 20 62 6c 6f 63  hreads must bloc
3dd0d 6b 20 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73  k until the firs
3dd0e 74 20 74 68 72 65 61 64 20 66 69 6e 69 73 68 65  t thread finishe
3dd0f 73 20 77 69 74 68 20 74 68 65 20 69 6e 69 74 69  s with the initi
3dd10 61 6c 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  alization..**.**
3dd11 20 54 68 65 20 66 69 72 73 74 20 74 68 72 65 61   The first threa
3dd12 64 20 6d 69 67 68 74 20 63 61 6c 6c 20 74 68 69  d might call thi
3dd13 73 20 72 6f 75 74 69 6e 65 20 72 65 63 75 72 73  s routine recurs
3dd14 69 76 65 6c 79 2e 20 20 52 65 63 75 72 73 69 76  ively.  Recursiv
3dd15 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 74 68  e.** calls to th
3dd16 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c  is routine shoul
3dd17 64 20 6e 6f 74 20 62 6c 6f 63 6b 2c 20 6f 66 20  d not block, of 
3dd18 63 6f 75 72 73 65 2e 20 20 4f 74 68 65 72 77 69  course.  Otherwi
3dd19 73 65 20 74 68 65 0a 2a 2a 20 69 6e 69 74 69 61  se the.** initia
3dd1a 6c 69 7a 61 74 69 6f 6e 20 70 72 6f 63 65 73 73  lization process
3dd1b 20 77 6f 75 6c 64 20 6e 65 76 65 72 20 63 6f 6d   would never com
3dd1c 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 4c 65 74  plete..**.** Let
3dd1d 20 58 20 62 65 20 74 68 65 20 66 69 72 73 74 20   X be the first 
3dd1e 74 68 72 65 61 64 20 74 6f 20 65 6e 74 65 72 20  thread to enter 
3dd1f 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 4c  this routine.  L
3dd20 65 74 20 59 20 62 65 20 73 6f 6d 65 20 6f 74 68  et Y be some oth
3dd21 65 72 0a 2a 2a 20 74 68 72 65 61 64 2e 20 20 54  er.** thread.  T
3dd22 68 65 6e 20 77 68 69 6c 65 20 74 68 65 20 69 6e  hen while the in
3dd23 69 74 69 61 6c 20 69 6e 76 6f 63 61 74 69 6f 6e  itial invocation
3dd24 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
3dd25 20 62 79 20 58 20 69 73 0a 2a 2a 20 69 6e 63 6f   by X is.** inco
3dd26 6d 70 6c 65 74 65 2c 20 69 74 20 69 73 20 72 65  mplete, it is re
3dd27 71 75 69 72 65 64 20 74 68 61 74 3a 0a 2a 2a 0a  quired that:.**.
3dd28 2a 2a 20 20 20 20 2a 20 20 43 61 6c 6c 73 20 74  **    *  Calls t
3dd29 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  o this routine f
3dd2a 72 6f 6d 20 59 20 6d 75 73 74 20 62 6c 6f 63 6b  rom Y must block
3dd2b 20 75 6e 74 69 6c 20 74 68 65 20 6f 75 74 65 72   until the outer
3dd2c 2d 6d 6f 73 74 0a 2a 2a 20 20 20 20 20 20 20 63  -most.**       c
3dd2d 61 6c 6c 20 62 79 20 58 20 63 6f 6d 70 6c 65 74  all by X complet
3dd2e 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20  es..**.**    *  
3dd2f 52 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20  Recursive calls 
3dd30 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
3dd31 66 72 6f 6d 20 74 68 72 65 61 64 20 58 20 72 65  from thread X re
3dd32 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79  turn immediately
3dd33 0a 2a 2a 20 20 20 20 20 20 20 77 69 74 68 6f 75  .**       withou
3dd34 74 20 62 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 53  t blocking..*/.S
3dd35 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
3dd36 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
3dd37 28 76 6f 69 64 29 7b 0a 20 20 4d 55 54 45 58 5f  (void){.  MUTEX_
3dd38 4c 4f 47 49 43 28 20 73 71 6c 69 74 65 33 5f 6d  LOGIC( sqlite3_m
3dd39 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3b 20 29  utex *pMaster; )
3dd3a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61         /* The ma
3dd3b 69 6e 20 73 74 61 74 69 63 20 6d 75 74 65 78 20  in static mutex 
3dd3c 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
3dd3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3dd3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3dd3f 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65    /* Result code
3dd40 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
3dd41 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 72 63  TE_OMIT_WSD.  rc
3dd42 20 3d 20 73 71 6c 69 74 65 33 5f 77 73 64 5f 69   = sqlite3_wsd_i
3dd43 6e 69 74 28 34 30 39 36 2c 20 32 34 29 3b 0a 20  nit(4096, 24);. 
3dd44 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3dd45 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
3dd46 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a   rc;.  }.#endif.
3dd47 0a 20 20 2f 2a 20 49 66 20 53 51 4c 69 74 65 20  .  /* If SQLite 
3dd48 69 73 20 61 6c 72 65 61 64 79 20 63 6f 6d 70 6c  is already compl
3dd49 65 74 65 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65  etely initialize
3dd4a 64 2c 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c  d, then this cal
3dd4b 6c 0a 20 20 2a 2a 20 74 6f 20 73 71 6c 69 74 65  l.  ** to sqlite
3dd4c 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 73  3_initialize() s
3dd4d 68 6f 75 6c 64 20 62 65 20 61 20 6e 6f 2d 6f 70  hould be a no-op
3dd4e 2e 20 20 42 75 74 20 74 68 65 20 69 6e 69 74 69  .  But the initi
3dd4f 61 6c 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a 20 6d  alization.  ** m
3dd50 75 73 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 2e  ust be complete.
3dd51 20 20 53 6f 20 69 73 49 6e 69 74 20 6d 75 73 74    So isInit must
3dd52 20 6e 6f 74 20 62 65 20 73 65 74 20 75 6e 74 69   not be set unti
3dd53 6c 20 74 68 65 20 76 65 72 79 20 65 6e 64 0a 20  l the very end. 
3dd54 20 2a 2a 20 6f 66 20 74 68 69 73 20 72 6f 75 74   ** of this rout
3dd55 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ine..  */.  if( 
3dd56 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3dd57 66 69 67 2e 69 73 49 6e 69 74 20 29 20 72 65 74  fig.isInit ) ret
3dd58 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  urn SQLITE_OK;..
3dd59 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
3dd5a 41 42 4c 45 5f 53 51 4c 4c 4f 47 0a 20 20 7b 0a  ABLE_SQLLOG.  {.
3dd5b 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20      extern void 
3dd5c 73 71 6c 69 74 65 33 5f 69 6e 69 74 5f 73 71 6c  sqlite3_init_sql
3dd5d 6c 6f 67 28 76 6f 69 64 29 3b 0a 20 20 20 20 73  log(void);.    s
3dd5e 71 6c 69 74 65 33 5f 69 6e 69 74 5f 73 71 6c 6c  qlite3_init_sqll
3dd5f 6f 67 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  og();.  }.#endif
3dd60 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
3dd61 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79   the mutex subsy
3dd62 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69  stem is initiali
3dd63 7a 65 64 2e 20 20 49 66 20 75 6e 61 62 6c 65 20  zed.  If unable 
3dd64 74 6f 20 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c  to .  ** initial
3dd65 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 75  ize the mutex su
3dd66 62 73 79 73 74 65 6d 2c 20 72 65 74 75 72 6e 20  bsystem, return 
3dd67 65 61 72 6c 79 20 77 69 74 68 20 74 68 65 20 65  early with the e
3dd68 72 72 6f 72 2e 0a 20 20 2a 2a 20 49 66 20 74 68  rror..  ** If th
3dd69 65 20 73 79 73 74 65 6d 20 69 73 20 73 6f 20 73  e system is so s
3dd6a 69 63 6b 20 74 68 61 74 20 77 65 20 61 72 65 20  ick that we are 
3dd6b 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61  unable to alloca
3dd6c 74 65 20 61 20 6d 75 74 65 78 2c 0a 20 20 2a 2a  te a mutex,.  **
3dd6d 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 6d 75   there is not mu
3dd6e 63 68 20 53 51 4c 69 74 65 20 69 73 20 67 6f 69  ch SQLite is goi
3dd6f 6e 67 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f  ng to be able to
3dd70 20 64 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54   do..  **.  ** T
3dd71 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
3dd72 65 6d 20 6d 75 73 74 20 74 61 6b 65 20 63 61 72  em must take car
3dd73 65 20 6f 66 20 73 65 72 69 61 6c 69 7a 69 6e 67  e of serializing
3dd74 20 69 74 73 20 6f 77 6e 0a 20 20 2a 2a 20 69 6e   its own.  ** in
3dd75 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 20 20  itialization..  
3dd76 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
3dd77 33 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20  3MutexInit();.  
3dd78 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
3dd79 72 63 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61  rc;..  /* Initia
3dd7a 6c 69 7a 65 20 74 68 65 20 6d 61 6c 6c 6f 63 28  lize the malloc(
3dd7b 29 20 73 79 73 74 65 6d 20 61 6e 64 20 74 68 65  ) system and the
3dd7c 20 72 65 63 75 72 73 69 76 65 20 70 49 6e 69 74   recursive pInit
3dd7d 4d 75 74 65 78 20 6d 75 74 65 78 2e 0a 20 20 2a  Mutex mutex..  *
3dd7e 2a 20 54 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  * This operation
3dd7f 20 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79   is protected by
3dd80 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54   the STATIC_MAST
3dd81 45 52 20 6d 75 74 65 78 2e 20 20 4e 6f 74 65 20  ER mutex.  Note 
3dd82 74 68 61 74 0a 20 20 2a 2a 20 4d 75 74 65 78 41  that.  ** MutexA
3dd83 6c 6c 6f 63 28 29 20 69 73 20 63 61 6c 6c 65 64  lloc() is called
3dd84 20 66 6f 72 20 61 20 73 74 61 74 69 63 20 6d 75   for a static mu
3dd85 74 65 78 20 70 72 69 6f 72 20 74 6f 20 69 6e 69  tex prior to ini
3dd86 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a 20 20  tializing the.  
3dd87 2a 2a 20 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73  ** malloc subsys
3dd88 74 65 6d 20 2d 20 74 68 69 73 20 69 6d 70 6c 69  tem - this impli
3dd89 65 73 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f  es that the allo
3dd8a 63 61 74 69 6f 6e 20 6f 66 20 61 20 73 74 61 74  cation of a stat
3dd8b 69 63 0a 20 20 2a 2a 20 6d 75 74 65 78 20 6d 75  ic.  ** mutex mu
3dd8c 73 74 20 6e 6f 74 20 72 65 71 75 69 72 65 20 73  st not require s
3dd8d 75 70 70 6f 72 74 20 66 72 6f 6d 20 74 68 65 20  upport from the 
3dd8e 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d  malloc subsystem
3dd8f 2e 0a 20 20 2a 2f 0a 20 20 4d 55 54 45 58 5f 4c  ..  */.  MUTEX_L
3dd90 4f 47 49 43 28 20 70 4d 61 73 74 65 72 20 3d 20  OGIC( pMaster = 
3dd91 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
3dd92 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
3dd93 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 20 29  TATIC_MASTER); )
3dd94 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3dd95 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 29 3b  _enter(pMaster);
3dd96 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  .  sqlite3Global
3dd97 43 6f 6e 66 69 67 2e 69 73 4d 75 74 65 78 49 6e  Config.isMutexIn
3dd98 69 74 20 3d 20 31 3b 0a 20 20 69 66 28 20 21 73  it = 1;.  if( !s
3dd99 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
3dd9a 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20  ig.isMallocInit 
3dd9b 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
3dd9c 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 29 3b  te3MallocInit();
3dd9d 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
3dd9e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3dd9f 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3dda0 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74  fig.isMallocInit
3dda1 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 21 73   = 1;.    if( !s
3dda2 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
3dda3 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 20 29 7b  ig.pInitMutex ){
3dda4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
3dda5 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74  obalConfig.pInit
3dda6 4d 75 74 65 78 20 3d 0a 20 20 20 20 20 20 20 20  Mutex =.        
3dda7 20 20 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41     sqlite3MutexA
3dda8 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
3dda9 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 20  X_RECURSIVE);.  
3ddaa 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47      if( sqlite3G
3ddab 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72  lobalConfig.bCor
3ddac 65 4d 75 74 65 78 20 26 26 20 21 73 71 6c 69 74  eMutex && !sqlit
3ddad 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
3ddae 49 6e 69 74 4d 75 74 65 78 20 29 7b 0a 20 20 20  InitMutex ){.   
3ddaf 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
3ddb0 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a  _NOMEM;.      }.
3ddb1 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
3ddb2 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
3ddb3 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
3ddb4 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e 69  alConfig.nRefIni
3ddb5 74 4d 75 74 65 78 2b 2b 3b 0a 20 20 7d 0a 20 20  tMutex++;.  }.  
3ddb6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
3ddb7 61 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 0a 20  ave(pMaster);.. 
3ddb8 20 2f 2a 20 49 66 20 72 63 20 69 73 20 6e 6f 74   /* If rc is not
3ddb9 20 53 51 4c 49 54 45 5f 4f 4b 20 61 74 20 74 68   SQLITE_OK at th
3ddba 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 65  is point, then e
3ddbb 69 74 68 65 72 20 74 68 65 20 6d 61 6c 6c 6f 63  ither the malloc
3ddbc 0a 20 20 2a 2a 20 73 75 62 73 79 73 74 65 6d 20  .  ** subsystem 
3ddbd 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 69 6e 69  could not be ini
3ddbe 74 69 61 6c 69 7a 65 64 20 6f 72 20 74 68 65 20  tialized or the 
3ddbf 73 79 73 74 65 6d 20 66 61 69 6c 65 64 20 74 6f  system failed to
3ddc0 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a 2a 20 74   allocate.  ** t
3ddc1 68 65 20 70 49 6e 69 74 4d 75 74 65 78 20 6d 75  he pInitMutex mu
3ddc2 74 65 78 2e 20 52 65 74 75 72 6e 20 61 6e 20 65  tex. Return an e
3ddc3 72 72 6f 72 20 69 6e 20 65 69 74 68 65 72 20 63  rror in either c
3ddc4 61 73 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 72  ase.  */.  if( r
3ddc5 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
3ddc6 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
3ddc7 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20 74 68 65 20   }..  /* Do the 
3ddc8 72 65 73 74 20 6f 66 20 74 68 65 20 69 6e 69 74  rest of the init
3ddc9 69 61 6c 69 7a 61 74 69 6f 6e 20 75 6e 64 65 72  ialization under
3ddca 20 74 68 65 20 72 65 63 75 72 73 69 76 65 20 6d   the recursive m
3ddcb 75 74 65 78 20 73 6f 0a 20 20 2a 2a 20 74 68 61  utex so.  ** tha
3ddcc 74 20 77 65 20 77 69 6c 6c 20 62 65 20 61 62 6c  t we will be abl
3ddcd 65 20 74 6f 20 68 61 6e 64 6c 65 20 72 65 63 75  e to handle recu
3ddce 72 73 69 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f  rsive calls into
3ddcf 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e  .  ** sqlite3_in
3ddd0 69 74 69 61 6c 69 7a 65 28 29 2e 20 20 54 68 65  itialize().  The
3ddd1 20 72 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73   recursive calls
3ddd2 20 6e 6f 72 6d 61 6c 6c 79 20 63 6f 6d 65 20 74   normally come t
3ddd3 68 72 6f 75 67 68 0a 20 20 2a 2a 20 73 71 6c 69  hrough.  ** sqli
3ddd4 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 77 68  te3_os_init() wh
3ddd5 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73 20 73 71  en it invokes sq
3ddd6 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
3ddd7 65 72 28 29 2c 20 62 75 74 20 6f 74 68 65 72 0a  er(), but other.
3ddd8 20 20 2a 2a 20 72 65 63 75 72 73 69 76 65 20 63    ** recursive c
3ddd9 61 6c 6c 73 20 6d 69 67 68 74 20 61 6c 73 6f 20  alls might also 
3ddda 62 65 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 2a  be possible..  *
3dddb 2a 0a 20 20 2a 2a 20 49 4d 50 4c 45 4d 45 4e 54  *.  ** IMPLEMENT
3dddc 41 54 49 4f 4e 2d 4f 46 3a 20 52 2d 30 30 31 34  ATION-OF: R-0014
3dddd 30 2d 33 37 34 34 35 20 53 51 4c 69 74 65 20 61  0-37445 SQLite a
3ddde 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 72  utomatically ser
3dddf 69 61 6c 69 7a 65 73 20 63 61 6c 6c 73 0a 20 20  ializes calls.  
3dde0 2a 2a 20 74 6f 20 74 68 65 20 78 49 6e 69 74 20  ** to the xInit 
3dde1 6d 65 74 68 6f 64 2c 20 73 6f 20 74 68 65 20 78  method, so the x
3dde2 49 6e 69 74 20 6d 65 74 68 6f 64 20 6e 65 65 64  Init method need
3dde3 20 6e 6f 74 20 62 65 20 74 68 72 65 61 64 73 61   not be threadsa
3dde4 66 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  fe..  **.  ** Th
3dde5 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 75 74 65  e following mute
3dde6 78 20 69 73 20 77 68 61 74 20 73 65 72 69 61 6c  x is what serial
3dde7 69 7a 65 73 20 61 63 63 65 73 73 20 74 6f 20 74  izes access to t
3dde8 68 65 20 61 70 70 64 65 66 20 70 63 61 63 68 65  he appdef pcache
3dde9 20 78 49 6e 69 74 0a 20 20 2a 2a 20 6d 65 74 68   xInit.  ** meth
3ddea 6f 64 73 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ods.  The sqlite
3ddeb 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73  3_pcache_methods
3ddec 2e 78 49 6e 69 74 28 29 20 61 6c 6c 20 69 73 20  .xInit() all is 
3dded 65 6d 62 65 64 64 65 64 20 69 6e 20 74 68 65 0a  embedded in the.
3ddee 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c    ** call to sql
3ddef 69 74 65 33 50 63 61 63 68 65 49 6e 69 74 69 61  ite3PcacheInitia
3ddf0 6c 69 7a 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 73  lize()..  */.  s
3ddf1 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
3ddf2 65 72 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  er(sqlite3Global
3ddf3 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65  Config.pInitMute
3ddf4 78 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  x);.  if( sqlite
3ddf5 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
3ddf6 49 6e 69 74 3d 3d 30 20 26 26 20 73 71 6c 69 74  Init==0 && sqlit
3ddf7 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
3ddf8 6e 50 72 6f 67 72 65 73 73 3d 3d 30 20 29 7b 0a  nProgress==0 ){.
3ddf9 20 20 20 20 46 75 6e 63 44 65 66 48 61 73 68 20      FuncDefHash 
3ddfa 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c  *pHash = &GLOBAL
3ddfb 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71  (FuncDefHash, sq
3ddfc 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74  lite3GlobalFunct
3ddfd 69 6f 6e 73 29 3b 0a 20 20 20 20 73 71 6c 69 74  ions);.    sqlit
3ddfe 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
3ddff 6e 50 72 6f 67 72 65 73 73 20 3d 20 31 3b 0a 20  nProgress = 1;. 
3de00 20 20 20 6d 65 6d 73 65 74 28 70 48 61 73 68 2c     memset(pHash,
3de01 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74   0, sizeof(sqlit
3de02 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e  e3GlobalFunction
3de03 73 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  s));.    sqlite3
3de04 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75  RegisterGlobalFu
3de05 6e 63 74 69 6f 6e 73 28 29 3b 0a 20 20 20 20 69  nctions();.    i
3de06 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
3de07 43 6f 6e 66 69 67 2e 69 73 50 43 61 63 68 65 49  Config.isPCacheI
3de08 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  nit==0 ){.      
3de09 72 63 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63  rc = sqlite3Pcac
3de0a 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a  heInitialize();.
3de0b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
3de0c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3de0d 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
3de0e 61 6c 43 6f 6e 66 69 67 2e 69 73 50 43 61 63 68  alConfig.isPCach
3de0f 65 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 20  eInit = 1;.     
3de10 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 49   rc = sqlite3OsI
3de11 6e 69 74 28 29 3b 0a 20 20 20 20 7d 0a 20 20 20  nit();.    }.   
3de12 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
3de13 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
3de14 74 65 33 50 43 61 63 68 65 42 75 66 66 65 72 53  te3PCacheBufferS
3de15 65 74 75 70 28 20 73 71 6c 69 74 65 33 47 6c 6f  etup( sqlite3Glo
3de16 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 2c  balConfig.pPage,
3de17 20 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69   .          sqli
3de18 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3de19 73 7a 50 61 67 65 2c 20 73 71 6c 69 74 65 33 47  szPage, sqlite3G
3de1a 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67  lobalConfig.nPag
3de1b 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
3de1c 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
3de1d 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  Init = 1;.    }.
3de1e 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3de1f 6c 43 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65  lConfig.inProgre
3de20 73 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  ss = 0;.  }.  sq
3de21 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
3de22 65 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  e(sqlite3GlobalC
3de23 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78  onfig.pInitMutex
3de24 29 3b 0a 0a 20 20 2f 2a 20 47 6f 20 62 61 63 6b  );..  /* Go back
3de25 20 75 6e 64 65 72 20 74 68 65 20 73 74 61 74 69   under the stati
3de26 63 20 6d 75 74 65 78 20 61 6e 64 20 63 6c 65 61  c mutex and clea
3de27 6e 20 75 70 20 74 68 65 20 72 65 63 75 72 73 69  n up the recursi
3de28 76 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 74 6f  ve.  ** mutex to
3de29 20 70 72 65 76 65 6e 74 20 61 20 72 65 73 6f 75   prevent a resou
3de2a 72 63 65 20 6c 65 61 6b 2e 0a 20 20 2a 2f 0a 20  rce leak..  */. 
3de2b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
3de2c 6e 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20  nter(pMaster);. 
3de2d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3de2e 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74  nfig.nRefInitMut
3de2f 65 78 2d 2d 3b 0a 20 20 69 66 28 20 73 71 6c 69  ex--;.  if( sqli
3de30 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3de31 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 3c 3d 30  nRefInitMutex<=0
3de32 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
3de33 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3de34 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65  fig.nRefInitMute
3de35 78 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69  x==0 );.    sqli
3de36 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73  te3_mutex_free(s
3de37 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
3de38 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 29 3b 0a  ig.pInitMutex);.
3de39 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3de3a 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74  lConfig.pInitMut
3de3b 65 78 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  ex = 0;.  }.  sq
3de3c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
3de3d 65 28 70 4d 61 73 74 65 72 29 3b 0a 0a 20 20 2f  e(pMaster);..  /
3de3e 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
3de3f 69 73 20 6a 75 73 74 20 61 20 73 61 6e 69 74 79  is just a sanity
3de40 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73   check to make s
3de41 75 72 65 20 53 51 4c 69 74 65 20 68 61 73 0a 20  ure SQLite has. 
3de42 20 2a 2a 20 62 65 65 6e 20 63 6f 6d 70 69 6c 65   ** been compile
3de43 64 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 49 74  d correctly.  It
3de44 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f   is important to
3de45 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65 2c 20   run this code, 
3de46 62 75 74 0a 20 20 2a 2a 20 77 65 20 64 6f 6e 27  but.  ** we don'
3de47 74 20 77 61 6e 74 20 74 6f 20 72 75 6e 20 69 74  t want to run it
3de48 20 74 6f 6f 20 6f 66 74 65 6e 20 61 6e 64 20 73   too often and s
3de49 6f 61 6b 20 75 70 20 43 50 55 20 63 79 63 6c 65  oak up CPU cycle
3de4a 73 20 66 6f 72 20 6e 6f 0a 20 20 2a 2a 20 72 65  s for no.  ** re
3de4b 61 73 6f 6e 2e 20 20 53 6f 20 77 65 20 72 75 6e  ason.  So we run
3de4c 20 69 74 20 6f 6e 63 65 20 64 75 72 69 6e 67 20   it once during 
3de4d 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a  initialization..
3de4e 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45    */.#ifndef NDE
3de4f 42 55 47 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  BUG.#ifndef SQLI
3de50 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
3de51 5f 50 4f 49 4e 54 0a 20 20 2f 2a 20 54 68 69 73  _POINT.  /* This
3de52 20 73 65 63 74 69 6f 6e 20 6f 66 20 63 6f 64 65   section of code
3de53 27 73 20 6f 6e 6c 79 20 22 6f 75 74 70 75 74 22  's only "output"
3de54 20 69 73 20 76 69 61 20 61 73 73 65 72 74 28 29   is via assert()
3de55 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 2a 2f 0a   statements. */.
3de56 20 20 69 66 20 28 20 72 63 3d 3d 53 51 4c 49 54    if ( rc==SQLIT
3de57 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 36 34 20  E_OK ){.    u64 
3de58 78 20 3d 20 28 28 28 75 36 34 29 31 29 3c 3c 36  x = (((u64)1)<<6
3de59 33 29 2d 31 3b 0a 20 20 20 20 64 6f 75 62 6c 65  3)-1;.    double
3de5a 20 79 3b 0a 20 20 20 20 61 73 73 65 72 74 28 73   y;.    assert(s
3de5b 69 7a 65 6f 66 28 78 29 3d 3d 38 29 3b 0a 20 20  izeof(x)==8);.  
3de5c 20 20 61 73 73 65 72 74 28 73 69 7a 65 6f 66 28    assert(sizeof(
3de5d 78 29 3d 3d 73 69 7a 65 6f 66 28 79 29 29 3b 0a  x)==sizeof(y));.
3de5e 20 20 20 20 6d 65 6d 63 70 79 28 26 79 2c 20 26      memcpy(&y, &
3de5f 78 2c 20 38 29 3b 0a 20 20 20 20 61 73 73 65 72  x, 8);.    asser
3de60 74 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28  t( sqlite3IsNaN(
3de61 79 29 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  y) );.  }.#endif
3de62 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 6f  .#endif..  /* Do
3de63 20 65 78 74 72 61 20 69 6e 69 74 69 61 6c 69 7a   extra initializ
3de64 61 74 69 6f 6e 20 73 74 65 70 73 20 72 65 71 75  ation steps requ
3de65 65 73 74 65 64 20 62 79 20 74 68 65 20 53 51 4c  ested by the SQL
3de66 49 54 45 5f 45 58 54 52 41 5f 49 4e 49 54 0a 20  ITE_EXTRA_INIT. 
3de67 20 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65   ** compile-time
3de68 20 6f 70 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23 69   option..  */.#i
3de69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 58 54 52  fdef SQLITE_EXTR
3de6a 41 5f 49 4e 49 54 0a 20 20 69 66 28 20 72 63 3d  A_INIT.  if( rc=
3de6b 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 71  =SQLITE_OK && sq
3de6c 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3de6d 67 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20  g.isInit ){.    
3de6e 69 6e 74 20 53 51 4c 49 54 45 5f 45 58 54 52 41  int SQLITE_EXTRA
3de6f 5f 49 4e 49 54 28 63 6f 6e 73 74 20 63 68 61 72  _INIT(const char
3de70 2a 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  *);.    rc = SQL
3de71 49 54 45 5f 45 58 54 52 41 5f 49 4e 49 54 28 30  ITE_EXTRA_INIT(0
3de72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
3de73 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
3de74 2a 0a 2a 2a 20 55 6e 64 6f 20 74 68 65 20 65 66  *.** Undo the ef
3de75 66 65 63 74 73 20 6f 66 20 73 71 6c 69 74 65 33  fects of sqlite3
3de76 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20 20  _initialize().  
3de77 4d 75 73 74 20 6e 6f 74 20 62 65 20 63 61 6c 6c  Must not be call
3de78 65 64 20 77 68 69 6c 65 0a 2a 2a 20 74 68 65 72  ed while.** ther
3de79 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e  e are outstandin
3de7a 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  g database conne
3de7b 63 74 69 6f 6e 73 20 6f 72 20 6d 65 6d 6f 72 79  ctions or memory
3de7c 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 72 0a   allocations or.
3de7d 2a 2a 20 77 68 69 6c 65 20 61 6e 79 20 70 61 72  ** while any par
3de7e 74 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 6f  t of SQLite is o
3de7f 74 68 65 72 77 69 73 65 20 69 6e 20 75 73 65 20  therwise in use 
3de80 69 6e 20 61 6e 79 20 74 68 72 65 61 64 2e 20 20  in any thread.  
3de81 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  This.** routine 
3de82 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66  is not threadsaf
3de83 65 2e 20 20 42 75 74 20 69 74 20 69 73 20 73 61  e.  But it is sa
3de84 66 65 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 69  fe to invoke thi
3de85 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6f 6e 20  s routine.** on 
3de86 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 61  when SQLite is a
3de87 6c 72 65 61 64 79 20 73 68 75 74 20 64 6f 77 6e  lready shut down
3de88 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69 73 20  .  If SQLite is 
3de89 61 6c 72 65 61 64 79 20 73 68 75 74 20 64 6f 77  already shut dow
3de8a 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72  n.** when this r
3de8b 6f 75 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65  outine is invoke
3de8c 64 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  d, then this rou
3de8d 74 69 6e 65 20 69 73 20 61 20 68 61 72 6d 6c 65  tine is a harmle
3de8e 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c  ss no-op..*/.SQL
3de8f 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3de90 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 76 6f 69  te3_shutdown(voi
3de91 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  d){.  if( sqlite
3de92 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
3de93 49 6e 69 74 20 29 7b 0a 23 69 66 64 65 66 20 53  Init ){.#ifdef S
3de94 51 4c 49 54 45 5f 45 58 54 52 41 5f 53 48 55 54  QLITE_EXTRA_SHUT
3de95 44 4f 57 4e 0a 20 20 20 20 76 6f 69 64 20 53 51  DOWN.    void SQ
3de96 4c 49 54 45 5f 45 58 54 52 41 5f 53 48 55 54 44  LITE_EXTRA_SHUTD
3de97 4f 57 4e 28 76 6f 69 64 29 3b 0a 20 20 20 20 53  OWN(void);.    S
3de98 51 4c 49 54 45 5f 45 58 54 52 41 5f 53 48 55 54  QLITE_EXTRA_SHUT
3de99 44 4f 57 4e 28 29 3b 0a 23 65 6e 64 69 66 0a 20  DOWN();.#endif. 
3de9a 20 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e     sqlite3_os_en
3de9b 64 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  d();.    sqlite3
3de9c 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65  _reset_auto_exte
3de9d 6e 73 69 6f 6e 28 29 3b 0a 20 20 20 20 73 71 6c  nsion();.    sql
3de9e 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
3de9f 2e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d  .isInit = 0;.  }
3dea0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
3dea1 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 50 43 61  obalConfig.isPCa
3dea2 63 68 65 49 6e 69 74 20 29 7b 0a 20 20 20 20 73  cheInit ){.    s
3dea3 71 6c 69 74 65 33 50 63 61 63 68 65 53 68 75 74  qlite3PcacheShut
3dea4 64 6f 77 6e 28 29 3b 0a 20 20 20 20 73 71 6c 69  down();.    sqli
3dea5 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3dea6 69 73 50 43 61 63 68 65 49 6e 69 74 20 3d 20 30  isPCacheInit = 0
3dea7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
3dea8 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3dea9 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 29 7b 0a  isMallocInit ){.
3deaa 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f      sqlite3Mallo
3deab 63 45 6e 64 28 29 3b 0a 20 20 20 20 73 71 6c 69  cEnd();.    sqli
3deac 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3dead 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 3d 20 30  isMallocInit = 0
3deae 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
3deaf 45 5f 4f 4d 49 54 5f 53 48 55 54 44 4f 57 4e 5f  E_OMIT_SHUTDOWN_
3deb0 44 49 52 45 43 54 4f 52 49 45 53 0a 20 20 20 20  DIRECTORIES.    
3deb1 2f 2a 20 54 68 65 20 68 65 61 70 20 73 75 62 73  /* The heap subs
3deb2 79 73 74 65 6d 20 68 61 73 20 6e 6f 77 20 62 65  ystem has now be
3deb3 65 6e 20 73 68 75 74 64 6f 77 6e 20 61 6e 64 20  en shutdown and 
3deb4 74 68 65 73 65 20 76 61 6c 75 65 73 20 61 72 65  these values are
3deb5 20 73 75 70 70 6f 73 65 64 0a 20 20 20 20 2a 2a   supposed.    **
3deb6 20 74 6f 20 62 65 20 4e 55 4c 4c 20 6f 72 20 70   to be NULL or p
3deb7 6f 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 74  oint to memory t
3deb8 68 61 74 20 77 61 73 20 6f 62 74 61 69 6e 65 64  hat was obtained
3deb9 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
3deba 6c 6c 6f 63 28 29 2c 0a 20 20 20 20 2a 2a 20 77  lloc(),.    ** w
3debb 68 69 63 68 20 77 6f 75 6c 64 20 72 65 6c 79 20  hich would rely 
3debc 6f 6e 20 74 68 61 74 20 68 65 61 70 20 73 75 62  on that heap sub
3debd 73 79 73 74 65 6d 3b 20 74 68 65 72 65 66 6f 72  system; therefor
3debe 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  e, make sure the
3debf 73 65 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73  se.    ** values
3dec0 20 63 61 6e 6e 6f 74 20 72 65 66 65 72 20 74 6f   cannot refer to
3dec1 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74 68 61   heap memory tha
3dec2 74 20 77 61 73 20 6a 75 73 74 20 69 6e 76 61 6c  t was just inval
3dec3 69 64 61 74 65 64 20 77 68 65 6e 20 74 68 65 0a  idated when the.
3dec4 20 20 20 20 2a 2a 20 68 65 61 70 20 73 75 62 73      ** heap subs
3dec5 79 73 74 65 6d 20 77 61 73 20 73 68 75 74 64 6f  ystem was shutdo
3dec6 77 6e 2e 20 20 54 68 69 73 20 69 73 20 6f 6e 6c  wn.  This is onl
3dec7 79 20 64 6f 6e 65 20 69 66 20 74 68 65 20 63 75  y done if the cu
3dec8 72 72 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 20 20  rrent call to.  
3dec9 20 20 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69    ** this functi
3deca 6f 6e 20 72 65 73 75 6c 74 65 64 20 69 6e 20 74  on resulted in t
3decb 68 65 20 68 65 61 70 20 73 75 62 73 79 73 74 65  he heap subsyste
3decc 6d 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67  m actually being
3decd 20 73 68 75 74 64 6f 77 6e 2e 0a 20 20 20 20 2a   shutdown..    *
3dece 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 64 61  /.    sqlite3_da
3decf 74 61 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 30  ta_directory = 0
3ded0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 74 65  ;.    sqlite3_te
3ded1 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 30  mp_directory = 0
3ded2 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69  ;.#endif.  }.  i
3ded3 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
3ded4 43 6f 6e 66 69 67 2e 69 73 4d 75 74 65 78 49 6e  Config.isMutexIn
3ded5 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  it ){.    sqlite
3ded6 33 4d 75 74 65 78 45 6e 64 28 29 3b 0a 20 20 20  3MutexEnd();.   
3ded7 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3ded8 6e 66 69 67 2e 69 73 4d 75 74 65 78 49 6e 69 74  nfig.isMutexInit
3ded9 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74   = 0;.  }..  ret
3deda 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
3dedb 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49  ../*.** This API
3dedc 20 61 6c 6c 6f 77 73 20 61 70 70 6c 69 63 61 74   allows applicat
3dedd 69 6f 6e 73 20 74 6f 20 6d 6f 64 69 66 79 20 74  ions to modify t
3dede 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67  he global config
3dedf 75 72 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68  uration of.** th
3dee0 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
3dee1 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 0a 2a 2a   at run-time..**
3dee2 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
3dee3 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20   should only be 
3dee4 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72  called when ther
3dee5 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e  e are no outstan
3dee6 64 69 6e 67 0a 2a 2a 20 64 61 74 61 62 61 73 65  ding.** database
3dee7 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 72 20   connections or 
3dee8 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
3dee9 6e 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ns.  This routin
3deea 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 74 68 72 65  e is not.** thre
3deeb 61 64 73 61 66 65 2e 20 20 46 61 69 6c 75 72 65  adsafe.  Failure
3deec 20 74 6f 20 68 65 65 64 20 74 68 65 73 65 20 77   to heed these w
3deed 61 72 6e 69 6e 67 73 20 63 61 6e 20 6c 65 61 64  arnings can lead
3deee 20 74 6f 20 75 6e 70 72 65 64 69 63 74 61 62 6c   to unpredictabl
3deef 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72 2e 0a 2a  e.** behavior..*
3def0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
3def1 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
3def2 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20  int op, ...){.  
3def3 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e  va_list ap;.  in
3def4 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
3def5 3b 0a 0a 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f  ;..  /* sqlite3_
3def6 63 6f 6e 66 69 67 28 29 20 73 68 61 6c 6c 20 72  config() shall r
3def7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
3def8 55 53 45 20 69 66 20 69 74 20 69 73 20 69 6e 76  USE if it is inv
3def9 6f 6b 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a 20  oked while.  ** 
3defa 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
3defb 72 79 20 69 73 20 69 6e 20 75 73 65 2e 20 2a 2f  ry is in use. */
3defc 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
3defd 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69  obalConfig.isIni
3defe 74 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  t ) return SQLIT
3deff 45 5f 4d 49 53 55 53 45 5f 42 4b 50 54 3b 0a 0a  E_MISUSE_BKPT;..
3df00 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 6f    va_start(ap, o
3df01 70 29 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70  p);.  switch( op
3df02 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 4d 75 74 65   ){..    /* Mute
3df03 78 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  x configuration 
3df04 6f 70 74 69 6f 6e 73 20 61 72 65 20 6f 6e 6c 79  options are only
3df05 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 61 20   available in a 
3df06 74 68 72 65 61 64 73 61 66 65 0a 20 20 20 20 2a  threadsafe.    *
3df07 2a 20 63 6f 6d 70 69 6c 65 2e 20 0a 20 20 20 20  * compile. .    
3df08 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
3df09 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
3df0a 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45  ) && SQLITE_THRE
3df0b 41 44 53 41 46 45 3e 30 0a 20 20 20 20 63 61 73  ADSAFE>0.    cas
3df0c 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
3df0d 53 49 4e 47 4c 45 54 48 52 45 41 44 3a 20 7b 0a  SINGLETHREAD: {.
3df0e 20 20 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65        /* Disable
3df0f 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f   all mutexing */
3df10 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
3df11 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65  obalConfig.bCore
3df12 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20 20 20 20  Mutex = 0;.     
3df13 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3df14 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20  nfig.bFullMutex 
3df15 3d 20 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  = 0;.      break
3df16 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
3df17 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d   SQLITE_CONFIG_M
3df18 55 4c 54 49 54 48 52 45 41 44 3a 20 7b 0a 20 20  ULTITHREAD: {.  
3df19 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 6d      /* Disable m
3df1a 75 74 65 78 69 6e 67 20 6f 66 20 64 61 74 61 62  utexing of datab
3df1b 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ase connections 
3df1c 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 45 6e 61 62  */.      /* Enab
3df1d 6c 65 20 6d 75 74 65 78 69 6e 67 20 6f 66 20 63  le mutexing of c
3df1e 6f 72 65 20 64 61 74 61 20 73 74 72 75 63 74 75  ore data structu
3df1f 72 65 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  res */.      sql
3df20 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
3df21 2e 62 43 6f 72 65 4d 75 74 65 78 20 3d 20 31 3b  .bCoreMutex = 1;
3df22 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
3df23 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c  obalConfig.bFull
3df24 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20 20 20 20  Mutex = 0;.     
3df25 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
3df26 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f    case SQLITE_CO
3df27 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 3a  NFIG_SERIALIZED:
3df28 20 7b 0a 20 20 20 20 20 20 2f 2a 20 45 6e 61 62   {.      /* Enab
3df29 6c 65 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20  le all mutexing 
3df2a 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
3df2b 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f  GlobalConfig.bCo
3df2c 72 65 4d 75 74 65 78 20 3d 20 31 3b 0a 20 20 20  reMutex = 1;.   
3df2d 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
3df2e 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65  Config.bFullMute
3df2f 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 62 72 65  x = 1;.      bre
3df30 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
3df31 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  se SQLITE_CONFIG
3df32 5f 4d 55 54 45 58 3a 20 7b 0a 20 20 20 20 20 20  _MUTEX: {.      
3df33 2f 2a 20 53 70 65 63 69 66 79 20 61 6e 20 61 6c  /* Specify an al
3df34 74 65 72 6e 61 74 69 76 65 20 6d 75 74 65 78 20  ternative mutex 
3df35 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
3df36 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
3df37 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
3df38 78 20 3d 20 2a 76 61 5f 61 72 67 28 61 70 2c 20  x = *va_arg(ap, 
3df39 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
3df3a 74 68 6f 64 73 2a 29 3b 0a 20 20 20 20 20 20 62  thods*);.      b
3df3b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
3df3c 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
3df3d 49 47 5f 47 45 54 4d 55 54 45 58 3a 20 7b 0a 20  IG_GETMUTEX: {. 
3df3e 20 20 20 20 20 2f 2a 20 52 65 74 72 69 65 76 65       /* Retrieve
3df3f 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74   the current mut
3df40 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
3df41 6e 20 2a 2f 0a 20 20 20 20 20 20 2a 76 61 5f 61  n */.      *va_a
3df42 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d  rg(ap, sqlite3_m
3df43 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 29 20 3d  utex_methods*) =
3df44 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3df45 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 20 20 20 20  nfig.mutex;.    
3df46 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
3df47 65 6e 64 69 66 0a 0a 0a 20 20 20 20 63 61 73 65  endif...    case
3df48 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d   SQLITE_CONFIG_M
3df49 41 4c 4c 4f 43 3a 20 7b 0a 20 20 20 20 20 20 2f  ALLOC: {.      /
3df4a 2a 20 53 70 65 63 69 66 79 20 61 6e 20 61 6c 74  * Specify an alt
3df4b 65 72 6e 61 74 69 76 65 20 6d 61 6c 6c 6f 63 20  ernative malloc 
3df4c 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
3df4d 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
3df4e 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20  lobalConfig.m = 
3df4f 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69  *va_arg(ap, sqli
3df50 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a  te3_mem_methods*
3df51 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
3df52 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
3df53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
3df54 4d 41 4c 4c 4f 43 3a 20 7b 0a 20 20 20 20 20 20  MALLOC: {.      
3df55 2f 2a 20 52 65 74 72 69 65 76 65 20 74 68 65 20  /* Retrieve the 
3df56 63 75 72 72 65 6e 74 20 6d 61 6c 6c 6f 63 28 29  current malloc()
3df57 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3df58 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  */.      if( sql
3df59 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
3df5a 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 20  .m.xMalloc==0 ) 
3df5b 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66  sqlite3MemSetDef
3df5c 61 75 6c 74 28 29 3b 0a 20 20 20 20 20 20 2a 76  ault();.      *v
3df5d 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65  a_arg(ap, sqlite
3df5e 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 29 20  3_mem_methods*) 
3df5f 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
3df60 6f 6e 66 69 67 2e 6d 3b 0a 20 20 20 20 20 20 62  onfig.m;.      b
3df61 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
3df62 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
3df63 49 47 5f 4d 45 4d 53 54 41 54 55 53 3a 20 7b 0a  IG_MEMSTATUS: {.
3df64 20 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20        /* Enable 
3df65 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20 6d  or disable the m
3df66 61 6c 6c 6f 63 20 73 74 61 74 75 73 20 63 6f 6c  alloc status col
3df67 6c 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  lection */.     
3df68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3df69 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 3d 20  nfig.bMemstat = 
3df6a 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
3df6b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
3df6c 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
3df6d 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
3df6e 43 48 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44  CH: {.      /* D
3df6f 65 73 69 67 6e 61 74 65 20 61 20 62 75 66 66 65  esignate a buffe
3df70 72 20 66 6f 72 20 73 63 72 61 74 63 68 20 6d 65  r for scratch me
3df71 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20  mory space */.  
3df72 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3df73 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68  lConfig.pScratch
3df74 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f   = va_arg(ap, vo
3df75 69 64 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  id*);.      sqli
3df76 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3df77 73 7a 53 63 72 61 74 63 68 20 3d 20 76 61 5f 61  szScratch = va_a
3df78 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
3df79 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
3df7a 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 20  Config.nScratch 
3df7b 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
3df7c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
3df7d 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
3df7e 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
3df7f 45 43 41 43 48 45 3a 20 7b 0a 20 20 20 20 20 20  ECACHE: {.      
3df80 2f 2a 20 44 65 73 69 67 6e 61 74 65 20 61 20 62  /* Designate a b
3df81 75 66 66 65 72 20 66 6f 72 20 70 61 67 65 20 63  uffer for page c
3df82 61 63 68 65 20 6d 65 6d 6f 72 79 20 73 70 61 63  ache memory spac
3df83 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  e */.      sqlit
3df84 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
3df85 50 61 67 65 20 3d 20 76 61 5f 61 72 67 28 61 70  Page = va_arg(ap
3df86 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20  , void*);.      
3df87 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3df88 66 69 67 2e 73 7a 50 61 67 65 20 3d 20 76 61 5f  fig.szPage = va_
3df89 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
3df8a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3df8b 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 20 3d 20  lConfig.nPage = 
3df8c 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
3df8d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
3df8e 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 53 51    }..    case SQ
3df8f 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43  LITE_CONFIG_PCAC
3df90 48 45 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 6e  HE: {.      /* n
3df91 6f 2d 6f 70 20 2a 2f 0a 20 20 20 20 20 20 62 72  o-op */.      br
3df92 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
3df93 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ase SQLITE_CONFI
3df94 47 5f 47 45 54 50 43 41 43 48 45 3a 20 7b 0a 20  G_GETPCACHE: {. 
3df95 20 20 20 20 20 2f 2a 20 6e 6f 77 20 61 6e 20 65       /* now an e
3df96 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 63  rror */.      rc
3df97 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
3df98 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
3df99 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 53 51    }..    case SQ
3df9a 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43  LITE_CONFIG_PCAC
3df9b 48 45 32 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  HE2: {.      /* 
3df9c 53 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72  Specify an alter
3df9d 6e 61 74 69 76 65 20 70 61 67 65 20 63 61 63 68  native page cach
3df9e 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
3df9f 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
3dfa0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
3dfa1 61 63 68 65 32 20 3d 20 2a 76 61 5f 61 72 67 28  ache2 = *va_arg(
3dfa2 61 70 2c 20 73 71 6c 69 74 65 33 5f 70 63 61 63  ap, sqlite3_pcac
3dfa3 68 65 5f 6d 65 74 68 6f 64 73 32 2a 29 3b 0a 20  he_methods2*);. 
3dfa4 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3dfa5 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
3dfa6 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 43  E_CONFIG_GETPCAC
3dfa7 48 45 32 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  HE2: {.      if(
3dfa8 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3dfa9 6e 66 69 67 2e 70 63 61 63 68 65 32 2e 78 49 6e  nfig.pcache2.xIn
3dfaa 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  it==0 ){.       
3dfab 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65   sqlite3PCacheSe
3dfac 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 20 20  tDefault();.    
3dfad 20 20 7d 0a 20 20 20 20 20 20 2a 76 61 5f 61 72    }.      *va_ar
3dfae 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 70 63  g(ap, sqlite3_pc
3dfaf 61 63 68 65 5f 6d 65 74 68 6f 64 73 32 2a 29 20  ache_methods2*) 
3dfb0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
3dfb1 6f 6e 66 69 67 2e 70 63 61 63 68 65 32 3b 0a 20  onfig.pcache2;. 
3dfb2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3dfb3 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  }..#if defined(S
3dfb4 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
3dfb5 53 59 53 33 29 20 7c 7c 20 64 65 66 69 6e 65 64  SYS3) || defined
3dfb6 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d  (SQLITE_ENABLE_M
3dfb7 45 4d 53 59 53 35 29 0a 20 20 20 20 63 61 73 65  EMSYS5).    case
3dfb8 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48   SQLITE_CONFIG_H
3dfb9 45 41 50 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  EAP: {.      /* 
3dfba 44 65 73 69 67 6e 61 74 65 20 61 20 62 75 66 66  Designate a buff
3dfbb 65 72 20 66 6f 72 20 68 65 61 70 20 6d 65 6d 6f  er for heap memo
3dfbc 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20  ry space */.    
3dfbd 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
3dfbe 6f 6e 66 69 67 2e 70 48 65 61 70 20 3d 20 76 61  onfig.pHeap = va
3dfbf 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b  _arg(ap, void*);
3dfc0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
3dfc1 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70  obalConfig.nHeap
3dfc2 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
3dfc3 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
3dfc4 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e  3GlobalConfig.mn
3dfc5 52 65 71 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  Req = va_arg(ap,
3dfc6 20 69 6e 74 29 3b 0a 0a 20 20 20 20 20 20 69 66   int);..      if
3dfc7 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
3dfc8 6f 6e 66 69 67 2e 6d 6e 52 65 71 3c 31 20 29 7b  onfig.mnReq<1 ){
3dfc9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
3dfca 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e 52  GlobalConfig.mnR
3dfcb 65 71 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65  eq = 1;.      }e
3dfcc 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 47  lse if( sqlite3G
3dfcd 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65  lobalConfig.mnRe
3dfce 71 3e 28 31 3c 3c 31 32 29 20 29 7b 0a 20 20 20  q>(1<<12) ){.   
3dfcf 20 20 20 20 20 2f 2a 20 63 61 70 20 6d 69 6e 20       /* cap min 
3dfd0 72 65 71 75 65 73 74 20 73 69 7a 65 20 61 74 20  request size at 
3dfd1 32 5e 31 32 20 2a 2f 0a 20 20 20 20 20 20 20 20  2^12 */.        
3dfd2 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3dfd3 66 69 67 2e 6d 6e 52 65 71 20 3d 20 28 31 3c 3c  fig.mnReq = (1<<
3dfd4 31 32 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  12);.      }..  
3dfd5 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47      if( sqlite3G
3dfd6 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61  lobalConfig.pHea
3dfd7 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  p==0 ){.        
3dfd8 2f 2a 20 49 66 20 74 68 65 20 68 65 61 70 20 70  /* If the heap p
3dfd9 6f 69 6e 74 65 72 20 69 73 20 4e 55 4c 4c 2c 20  ointer is NULL, 
3dfda 74 68 65 6e 20 72 65 73 74 6f 72 65 20 74 68 65  then restore the
3dfdb 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e   malloc implemen
3dfdc 74 61 74 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a  tation.        *
3dfdd 2a 20 62 61 63 6b 20 74 6f 20 4e 55 4c 4c 20 70  * back to NULL p
3dfde 6f 69 6e 74 65 72 73 20 74 6f 6f 2e 20 20 54 68  ointers too.  Th
3dfdf 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20 74 68  is will cause th
3dfe0 65 20 6d 61 6c 6c 6f 63 20 74 6f 20 67 6f 0a 20  e malloc to go. 
3dfe1 20 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 20 74         ** back t
3dfe2 6f 20 69 74 73 20 64 65 66 61 75 6c 74 20 69 6d  o its default im
3dfe3 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68 65  plementation whe
3dfe4 6e 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  n sqlite3_initia
3dfe5 6c 69 7a 65 28 29 20 69 73 0a 20 20 20 20 20 20  lize() is.      
3dfe6 20 20 2a 2a 20 72 75 6e 2e 0a 20 20 20 20 20 20    ** run..      
3dfe7 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 6d 65 6d    */.        mem
3dfe8 73 65 74 28 26 73 71 6c 69 74 65 33 47 6c 6f 62  set(&sqlite3Glob
3dfe9 61 6c 43 6f 6e 66 69 67 2e 6d 2c 20 30 2c 20 73  alConfig.m, 0, s
3dfea 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 47 6c 6f  izeof(sqlite3Glo
3dfeb 62 61 6c 43 6f 6e 66 69 67 2e 6d 29 29 3b 0a 20  balConfig.m));. 
3dfec 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
3dfed 20 20 20 20 2f 2a 20 54 68 65 20 68 65 61 70 20      /* The heap 
3dfee 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e  pointer is not N
3dfef 55 4c 4c 2c 20 74 68 65 6e 20 69 6e 73 74 61 6c  ULL, then instal
3dff0 6c 20 6f 6e 65 20 6f 66 20 74 68 65 0a 20 20 20  l one of the.   
3dff1 20 20 20 20 20 2a 2a 20 6d 65 6d 35 2e 63 2f 6d       ** mem5.c/m
3dff2 65 6d 33 2e 63 20 6d 65 74 68 6f 64 73 2e 20 49  em3.c methods. I
3dff3 66 20 6e 65 69 74 68 65 72 20 45 4e 41 42 4c 45  f neither ENABLE
3dff4 5f 4d 45 4d 53 59 53 33 20 6e 6f 72 0a 20 20 20  _MEMSYS3 nor.   
3dff5 20 20 20 20 20 2a 2a 20 45 4e 41 42 4c 45 5f 4d       ** ENABLE_M
3dff6 45 4d 53 59 53 35 20 69 73 20 64 65 66 69 6e 65  EMSYS5 is define
3dff7 64 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  d, return an err
3dff8 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 23  or..        */.#
3dff9 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
3dffa 42 4c 45 5f 4d 45 4d 53 59 53 33 0a 20 20 20 20  BLE_MEMSYS3.    
3dffb 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3dffc 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a 73 71 6c  lConfig.m = *sql
3dffd 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73  ite3MemGetMemsys
3dffe 33 28 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64  3();.#endif.#ifd
3dfff 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
3e000 5f 4d 45 4d 53 59 53 35 0a 20 20 20 20 20 20 20  _MEMSYS5.       
3e001 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3e002 6e 66 69 67 2e 6d 20 3d 20 2a 73 71 6c 69 74 65  nfig.m = *sqlite
3e003 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 29  3MemGetMemsys5()
3e004 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7d  ;.#endif.      }
3e005 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
3e006 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
3e007 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
3e008 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3a 20 7b 0a  IG_LOOKASIDE: {.
3e009 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
3e00a 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 4c 6f 6f 6b  balConfig.szLook
3e00b 61 73 69 64 65 20 3d 20 76 61 5f 61 72 67 28 61  aside = va_arg(a
3e00c 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 73  p, int);.      s
3e00d 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
3e00e 69 67 2e 6e 4c 6f 6f 6b 61 73 69 64 65 20 3d 20  ig.nLookaside = 
3e00f 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
3e010 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
3e011 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20    }.    .    /* 
3e012 52 65 63 6f 72 64 20 61 20 70 6f 69 6e 74 65 72  Record a pointer
3e013 20 74 6f 20 74 68 65 20 6c 6f 67 67 65 72 20 66   to the logger f
3e014 75 6e 63 63 74 69 6f 6e 20 61 6e 64 20 69 74 73  uncction and its
3e015 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
3e016 0a 20 20 20 20 2a 2a 20 54 68 65 20 64 65 66 61  .    ** The defa
3e017 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20 20 4c 6f  ult is NULL.  Lo
3e018 67 67 69 6e 67 20 69 73 20 64 69 73 61 62 6c 65  gging is disable
3e019 64 20 69 66 20 74 68 65 20 66 75 6e 63 74 69 6f  d if the functio
3e01a 6e 20 70 6f 69 6e 74 65 72 20 69 73 0a 20 20 20  n pointer is.   
3e01b 20 2a 2a 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f   ** NULL..    */
3e01c 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
3e01d 5f 43 4f 4e 46 49 47 5f 4c 4f 47 3a 20 7b 0a 20  _CONFIG_LOG: {. 
3e01e 20 20 20 20 20 2f 2a 20 4d 53 56 43 20 69 73 20       /* MSVC is 
3e01f 70 69 63 6b 79 20 61 62 6f 75 74 20 70 75 6c 6c  picky about pull
3e020 69 6e 67 20 66 75 6e 63 20 70 74 72 73 20 66 72  ing func ptrs fr
3e021 6f 6d 20 76 61 20 6c 69 73 74 73 2e 0a 20 20 20  om va lists..   
3e022 20 20 20 2a 2a 20 68 74 74 70 3a 2f 2f 73 75 70     ** http://sup
3e023 70 6f 72 74 2e 6d 69 63 72 6f 73 6f 66 74 2e 63  port.microsoft.c
3e024 6f 6d 2f 6b 62 2f 34 37 39 36 31 0a 20 20 20 20  om/kb/47961.    
3e025 20 20 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62    ** sqlite3Glob
3e026 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f 67 20 3d 20  alConfig.xLog = 
3e027 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 28  va_arg(ap, void(
3e028 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  *)(void*,int,con
3e029 73 74 20 63 68 61 72 2a 29 29 3b 0a 20 20 20 20  st char*));.    
3e02a 20 20 2a 2f 0a 20 20 20 20 20 20 74 79 70 65 64    */.      typed
3e02b 65 66 20 76 6f 69 64 28 2a 4c 4f 47 46 55 4e 43  ef void(*LOGFUNC
3e02c 5f 74 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  _t)(void*,int,co
3e02d 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20  nst char*);.    
3e02e 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
3e02f 6f 6e 66 69 67 2e 78 4c 6f 67 20 3d 20 76 61 5f  onfig.xLog = va_
3e030 61 72 67 28 61 70 2c 20 4c 4f 47 46 55 4e 43 5f  arg(ap, LOGFUNC_
3e031 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
3e032 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 4c  3GlobalConfig.pL
3e033 6f 67 41 72 67 20 3d 20 76 61 5f 61 72 67 28 61  ogArg = va_arg(a
3e034 70 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 20 20 20  p, void*);.     
3e035 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
3e036 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
3e037 4f 4e 46 49 47 5f 55 52 49 3a 20 7b 0a 20 20 20  ONFIG_URI: {.   
3e038 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
3e039 43 6f 6e 66 69 67 2e 62 4f 70 65 6e 55 72 69 20  Config.bOpenUri 
3e03a 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
3e03b 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
3e03c 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20      }..    case 
3e03d 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 43 4f  SQLITE_CONFIG_CO
3e03e 56 45 52 49 4e 47 5f 49 4e 44 45 58 5f 53 43 41  VERING_INDEX_SCA
3e03f 4e 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  N: {.      sqlit
3e040 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
3e041 55 73 65 43 69 73 20 3d 20 76 61 5f 61 72 67 28  UseCis = va_arg(
3e042 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
3e043 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69  break;.    }..#i
3e044 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
3e045 4c 45 5f 53 51 4c 4c 4f 47 0a 20 20 20 20 63 61  LE_SQLLOG.    ca
3e046 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  se SQLITE_CONFIG
3e047 5f 53 51 4c 4c 4f 47 3a 20 7b 0a 20 20 20 20 20  _SQLLOG: {.     
3e048 20 74 79 70 65 64 65 66 20 76 6f 69 64 28 2a 53   typedef void(*S
3e049 51 4c 4c 4f 47 46 55 4e 43 5f 74 29 28 76 6f 69  QLLOGFUNC_t)(voi
3e04a 64 2a 2c 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f  d*, sqlite3*, co
3e04b 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b  nst char*, int);
3e04c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
3e04d 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 53 71 6c 6c  obalConfig.xSqll
3e04e 6f 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  og = va_arg(ap, 
3e04f 53 51 4c 4c 4f 47 46 55 4e 43 5f 74 29 3b 0a 20  SQLLOGFUNC_t);. 
3e050 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
3e051 61 6c 43 6f 6e 66 69 67 2e 70 53 71 6c 6c 6f 67  alConfig.pSqllog
3e052 41 72 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  Arg = va_arg(ap,
3e053 20 76 6f 69 64 20 2a 29 3b 0a 20 20 20 20 20 20   void *);.      
3e054 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
3e055 64 69 66 0a 0a 20 20 20 20 64 65 66 61 75 6c 74  dif..    default
3e056 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  : {.      rc = S
3e057 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
3e058 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
3e059 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70 29    }.  va_end(ap)
3e05a 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
3e05b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 74  ../*.** Set up t
3e05c 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66  he lookaside buf
3e05d 66 65 72 73 20 66 6f 72 20 61 20 64 61 74 61 62  fers for a datab
3e05e 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  ase connection..
3e05f 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
3e060 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20  _OK on success. 
3e061 20 0a 2a 2a 20 49 66 20 6c 6f 6f 6b 61 73 69 64   .** If lookasid
3e062 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 63 74  e is already act
3e063 69 76 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ive, return SQLI
3e064 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 54  TE_BUSY..**.** T
3e065 68 65 20 73 7a 20 70 61 72 61 6d 65 74 65 72 20  he sz parameter 
3e066 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
3e067 20 62 79 74 65 73 20 69 6e 20 65 61 63 68 20 6c   bytes in each l
3e068 6f 6f 6b 61 73 69 64 65 20 73 6c 6f 74 2e 0a 2a  ookaside slot..*
3e069 2a 20 54 68 65 20 63 6e 74 20 70 61 72 61 6d 65  * The cnt parame
3e06a 74 65 72 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ter is the numbe
3e06b 72 20 6f 66 20 73 6c 6f 74 73 2e 20 20 49 66 20  r of slots.  If 
3e06c 70 53 74 61 72 74 20 69 73 20 4e 55 4c 4c 20 74  pStart is NULL t
3e06d 68 65 0a 2a 2a 20 73 70 61 63 65 20 66 6f 72 20  he.** space for 
3e06e 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65  the lookaside me
3e06f 6d 6f 72 79 20 69 73 20 6f 62 74 61 69 6e 65 64  mory is obtained
3e070 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
3e071 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 70 53  lloc()..** If pS
3e072 74 61 72 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  tart is not NULL
3e073 20 74 68 65 6e 20 69 74 20 69 73 20 73 7a 2a 63   then it is sz*c
3e074 6e 74 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  nt bytes of memo
3e075 72 79 20 74 6f 20 75 73 65 20 66 6f 72 0a 2a 2a  ry to use for.**
3e076 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 6d   the lookaside m
3e077 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  emory..*/.static
3e078 20 69 6e 74 20 73 65 74 75 70 4c 6f 6f 6b 61 73   int setupLookas
3e079 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ide(sqlite3 *db,
3e07a 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74   void *pBuf, int
3e07b 20 73 7a 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20   sz, int cnt){. 
3e07c 20 76 6f 69 64 20 2a 70 53 74 61 72 74 3b 0a 20   void *pStart;. 
3e07d 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69   if( db->lookasi
3e07e 64 65 2e 6e 4f 75 74 20 29 7b 0a 20 20 20 20 72  de.nOut ){.    r
3e07f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
3e080 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 46 72 65 65  Y;.  }.  /* Free
3e081 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 6c 6f   any existing lo
3e082 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 66  okaside buffer f
3e083 6f 72 20 74 68 69 73 20 68 61 6e 64 6c 65 20 62  or this handle b
3e084 65 66 6f 72 65 0a 20 20 2a 2a 20 61 6c 6c 6f 63  efore.  ** alloc
3e085 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e 65 20  ating a new one 
3e086 73 6f 20 77 65 20 64 6f 6e 27 74 20 68 61 76 65  so we don't have
3e087 20 74 6f 20 68 61 76 65 20 73 70 61 63 65 20 66   to have space f
3e088 6f 72 20 0a 20 20 2a 2a 20 62 6f 74 68 20 61 74  or .  ** both at
3e089 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a   the same time..
3e08a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6c    */.  if( db->l
3e08b 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63  ookaside.bMalloc
3e08c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ed ){.    sqlite
3e08d 33 5f 66 72 65 65 28 64 62 2d 3e 6c 6f 6f 6b 61  3_free(db->looka
3e08e 73 69 64 65 2e 70 53 74 61 72 74 29 3b 0a 20 20  side.pStart);.  
3e08f 7d 0a 20 20 2f 2a 20 54 68 65 20 73 69 7a 65 20  }.  /* The size 
3e090 6f 66 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20 73  of a lookaside s
3e091 6c 6f 74 20 61 66 74 65 72 20 52 4f 55 4e 44 44  lot after ROUNDD
3e092 4f 57 4e 38 20 6e 65 65 64 73 20 74 6f 20 62 65  OWN8 needs to be
3e093 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 74 68 61   larger.  ** tha
3e094 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 62  n a pointer to b
3e095 65 20 75 73 65 66 75 6c 2e 0a 20 20 2a 2f 0a 20  e useful..  */. 
3e096 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38   sz = ROUNDDOWN8
3e097 28 73 7a 29 3b 20 20 2f 2a 20 49 4d 50 3a 20 52  (sz);  /* IMP: R
3e098 2d 33 33 30 33 38 2d 30 39 33 38 32 20 2a 2f 0a  -33038-09382 */.
3e099 20 20 69 66 28 20 73 7a 3c 3d 28 69 6e 74 29 73    if( sz<=(int)s
3e09a 69 7a 65 6f 66 28 4c 6f 6f 6b 61 73 69 64 65 53  izeof(LookasideS
3e09b 6c 6f 74 2a 29 20 29 20 73 7a 20 3d 20 30 3b 0a  lot*) ) sz = 0;.
3e09c 20 20 69 66 28 20 63 6e 74 3c 30 20 29 20 63 6e    if( cnt<0 ) cn
3e09d 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 7a 3d  t = 0;.  if( sz=
3e09e 3d 30 20 7c 7c 20 63 6e 74 3d 3d 30 20 29 7b 0a  =0 || cnt==0 ){.
3e09f 20 20 20 20 73 7a 20 3d 20 30 3b 0a 20 20 20 20      sz = 0;.    
3e0a0 70 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 7d 65  pStart = 0;.  }e
3e0a1 6c 73 65 20 69 66 28 20 70 42 75 66 3d 3d 30 20  lse if( pBuf==0 
3e0a2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  ){.    sqlite3Be
3e0a3 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
3e0a4 29 3b 0a 20 20 20 20 70 53 74 61 72 74 20 3d 20  );.    pStart = 
3e0a5 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73  sqlite3Malloc( s
3e0a6 7a 2a 63 6e 74 20 29 3b 20 20 2f 2a 20 49 4d 50  z*cnt );  /* IMP
3e0a7 3a 20 52 2d 36 31 39 34 39 2d 33 35 37 32 37 20  : R-61949-35727 
3e0a8 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e  */.    sqlite3En
3e0a9 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
3e0aa 0a 20 20 20 20 69 66 28 20 70 53 74 61 72 74 20  .    if( pStart 
3e0ab 29 20 63 6e 74 20 3d 20 73 71 6c 69 74 65 33 4d  ) cnt = sqlite3M
3e0ac 61 6c 6c 6f 63 53 69 7a 65 28 70 53 74 61 72 74  allocSize(pStart
3e0ad 29 2f 73 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  )/sz;.  }else{. 
3e0ae 20 20 20 70 53 74 61 72 74 20 3d 20 70 42 75 66     pStart = pBuf
3e0af 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 6c 6f 6f 6b  ;.  }.  db->look
3e0b0 61 73 69 64 65 2e 70 53 74 61 72 74 20 3d 20 70  aside.pStart = p
3e0b1 53 74 61 72 74 3b 0a 20 20 64 62 2d 3e 6c 6f 6f  Start;.  db->loo
3e0b2 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 30  kaside.pFree = 0
3e0b3 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  ;.  db->lookasid
3e0b4 65 2e 73 7a 20 3d 20 28 75 31 36 29 73 7a 3b 0a  e.sz = (u16)sz;.
3e0b5 20 20 69 66 28 20 70 53 74 61 72 74 20 29 7b 0a    if( pStart ){.
3e0b6 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 4c      int i;.    L
3e0b7 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 3b  ookasideSlot *p;
3e0b8 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 20  .    assert( sz 
3e0b9 3e 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 4c 6f  > (int)sizeof(Lo
3e0ba 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 20 29 3b  okasideSlot*) );
3e0bb 0a 20 20 20 20 70 20 3d 20 28 4c 6f 6f 6b 61 73  .    p = (Lookas
3e0bc 69 64 65 53 6c 6f 74 2a 29 70 53 74 61 72 74 3b  ideSlot*)pStart;
3e0bd 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e 74 2d 31  .    for(i=cnt-1
3e0be 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20  ; i>=0; i--){.  
3e0bf 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64      p->pNext = d
3e0c0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72  b->lookaside.pFr
3e0c1 65 65 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f  ee;.      db->lo
3e0c2 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20  okaside.pFree = 
3e0c3 70 3b 0a 20 20 20 20 20 20 70 20 3d 20 28 4c 6f  p;.      p = (Lo
3e0c4 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 26 28 28  okasideSlot*)&((
3e0c5 75 38 2a 29 70 29 5b 73 7a 5d 3b 0a 20 20 20 20  u8*)p)[sz];.    
3e0c6 7d 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  }.    db->lookas
3e0c7 69 64 65 2e 70 45 6e 64 20 3d 20 70 3b 0a 20 20  ide.pEnd = p;.  
3e0c8 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
3e0c9 62 45 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 20 20  bEnabled = 1;.  
3e0ca 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
3e0cb 62 4d 61 6c 6c 6f 63 65 64 20 3d 20 70 42 75 66  bMalloced = pBuf
3e0cc 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 7d 65 6c 73  ==0 ?1:0;.  }els
3e0cd 65 7b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61  e{.    db->looka
3e0ce 73 69 64 65 2e 70 45 6e 64 20 3d 20 30 3b 0a 20  side.pEnd = 0;. 
3e0cf 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
3e0d0 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20  .bEnabled = 0;. 
3e0d1 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
3e0d2 2e 62 4d 61 6c 6c 6f 63 65 64 20 3d 20 30 3b 0a  .bMalloced = 0;.
3e0d3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
3e0d4 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
3e0d5 20 52 65 74 75 72 6e 20 74 68 65 20 6d 75 74 65   Return the mute
3e0d6 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
3e0d7 68 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  h a database con
3e0d8 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  nection..*/.SQLI
3e0d9 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d  TE_API sqlite3_m
3e0da 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 64 62  utex *sqlite3_db
3e0db 5f 6d 75 74 65 78 28 73 71 6c 69 74 65 33 20 2a  _mutex(sqlite3 *
3e0dc 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62  db){.  return db
3e0dd 2d 3e 6d 75 74 65 78 3b 0a 7d 0a 0a 2f 2a 0a 2a  ->mutex;.}../*.*
3e0de 2a 20 46 72 65 65 20 75 70 20 61 73 20 6d 75 63  * Free up as muc
3e0df 68 20 6d 65 6d 6f 72 79 20 61 73 20 77 65 20 63  h memory as we c
3e0e0 61 6e 20 66 72 6f 6d 20 74 68 65 20 67 69 76 65  an from the give
3e0e1 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  n database.** co
3e0e2 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  nnection..*/.SQL
3e0e3 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3e0e4 74 65 33 5f 64 62 5f 72 65 6c 65 61 73 65 5f 6d  te3_db_release_m
3e0e5 65 6d 6f 72 79 28 73 71 6c 69 74 65 33 20 2a 64  emory(sqlite3 *d
3e0e6 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  b){.  int i;.  s
3e0e7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
3e0e8 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
3e0e9 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
3e0ea 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 66 6f 72  erAll(db);.  for
3e0eb 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
3e0ec 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65   i++){.    Btree
3e0ed 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b   *pBt = db->aDb[
3e0ee 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20  i].pBt;.    if( 
3e0ef 70 42 74 20 29 7b 0a 20 20 20 20 20 20 50 61 67  pBt ){.      Pag
3e0f0 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c  er *pPager = sql
3e0f1 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
3e0f2 42 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Bt);.      sqlit
3e0f3 65 33 50 61 67 65 72 53 68 72 69 6e 6b 28 70 50  e3PagerShrink(pP
3e0f4 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ager);.    }.  }
3e0f5 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
3e0f6 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 73  eaveAll(db);.  s
3e0f7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
3e0f8 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
3e0f9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3e0fa 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 66  K;.}../*.** Conf
3e0fb 69 67 75 72 61 74 69 6f 6e 20 73 65 74 74 69 6e  iguration settin
3e0fc 67 73 20 66 6f 72 20 61 6e 20 69 6e 64 69 76 69  gs for an indivi
3e0fd 64 75 61 6c 20 64 61 74 61 62 61 73 65 20 63 6f  dual database co
3e0fe 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49  nnection.*/.SQLI
3e0ff 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3e100 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c  e3_db_config(sql
3e101 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 70  ite3 *db, int op
3e102 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73  , ...){.  va_lis
3e103 74 20 61 70 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  t ap;.  int rc;.
3e104 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 6f    va_start(ap, o
3e105 70 29 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70  p);.  switch( op
3e106 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
3e107 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f  ITE_DBCONFIG_LOO
3e108 4b 41 53 49 44 45 3a 20 7b 0a 20 20 20 20 20 20  KASIDE: {.      
3e109 76 6f 69 64 20 2a 70 42 75 66 20 3d 20 76 61 5f  void *pBuf = va_
3e10a 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 20  arg(ap, void*); 
3e10b 2f 2a 20 49 4d 50 3a 20 52 2d 32 36 38 33 35 2d  /* IMP: R-26835-
3e10c 31 30 39 36 34 20 2a 2f 0a 20 20 20 20 20 20 69  10964 */.      i
3e10d 6e 74 20 73 7a 20 3d 20 76 61 5f 61 72 67 28 61  nt sz = va_arg(a
3e10e 70 2c 20 69 6e 74 29 3b 20 20 20 20 20 20 20 2f  p, int);       /
3e10f 2a 20 49 4d 50 3a 20 52 2d 34 37 38 37 31 2d 32  * IMP: R-47871-2
3e110 35 39 39 34 20 2a 2f 0a 20 20 20 20 20 20 69 6e  5994 */.      in
3e111 74 20 63 6e 74 20 3d 20 76 61 5f 61 72 67 28 61  t cnt = va_arg(a
3e112 70 2c 20 69 6e 74 29 3b 20 20 20 20 20 20 2f 2a  p, int);      /*
3e113 20 49 4d 50 3a 20 52 2d 30 34 34 36 30 2d 35 33   IMP: R-04460-53
3e114 33 38 36 20 2a 2f 0a 20 20 20 20 20 20 72 63 20  386 */.      rc 
3e115 3d 20 73 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65  = setupLookaside
3e116 28 64 62 2c 20 70 42 75 66 2c 20 73 7a 2c 20 63  (db, pBuf, sz, c
3e117 6e 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  nt);.      break
3e118 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
3e119 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61  ult: {.      sta
3e11a 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
3e11b 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6f   {.        int o
3e11c 70 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f  p;      /* The o
3e11d 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 20  pcode */.       
3e11e 20 75 33 32 20 6d 61 73 6b 3b 20 20 20 20 2f 2a   u32 mask;    /*
3e11f 20 4d 61 73 6b 20 6f 66 20 74 68 65 20 62 69 74   Mask of the bit
3e120 20 69 6e 20 73 71 6c 69 74 65 33 2e 66 6c 61 67   in sqlite3.flag
3e121 73 20 74 6f 20 73 65 74 2f 63 6c 65 61 72 20 2a  s to set/clear *
3e122 2f 0a 20 20 20 20 20 20 7d 20 61 46 6c 61 67 4f  /.      } aFlagO
3e123 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20  p[] = {.        
3e124 7b 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49  { SQLITE_DBCONFI
3e125 47 5f 45 4e 41 42 4c 45 5f 46 4b 45 59 2c 20 20  G_ENABLE_FKEY,  
3e126 20 20 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e    SQLITE_Foreign
3e127 4b 65 79 73 20 20 20 20 7d 2c 0a 20 20 20 20 20  Keys    },.     
3e128 20 20 20 7b 20 53 51 4c 49 54 45 5f 44 42 43 4f     { SQLITE_DBCO
3e129 4e 46 49 47 5f 45 4e 41 42 4c 45 5f 54 52 49 47  NFIG_ENABLE_TRIG
3e12a 47 45 52 2c 20 53 51 4c 49 54 45 5f 45 6e 61 62  GER, SQLITE_Enab
3e12b 6c 65 54 72 69 67 67 65 72 20 20 7d 2c 0a 20 20  leTrigger  },.  
3e12c 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 75 6e 73      };.      uns
3e12d 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20  igned int i;.   
3e12e 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
3e12f 52 52 4f 52 3b 20 2f 2a 20 49 4d 50 3a 20 52 2d  RROR; /* IMP: R-
3e130 34 32 37 39 30 2d 32 33 33 37 32 20 2a 2f 0a 20  42790-23372 */. 
3e131 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
3e132 41 72 72 61 79 53 69 7a 65 28 61 46 6c 61 67 4f  ArraySize(aFlagO
3e133 70 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  p); i++){.      
3e134 20 20 69 66 28 20 61 46 6c 61 67 4f 70 5b 69 5d    if( aFlagOp[i]
3e135 2e 6f 70 3d 3d 6f 70 20 29 7b 0a 20 20 20 20 20  .op==op ){.     
3e136 20 20 20 20 20 69 6e 74 20 6f 6e 6f 66 66 20 3d       int onoff =
3e137 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
3e138 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  ;.          int 
3e139 2a 70 52 65 73 20 3d 20 76 61 5f 61 72 67 28 61  *pRes = va_arg(a
3e13a 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 20 20  p, int*);.      
3e13b 20 20 20 20 69 6e 74 20 6f 6c 64 46 6c 61 67 73      int oldFlags
3e13c 20 3d 20 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20   = db->flags;.  
3e13d 20 20 20 20 20 20 20 20 69 66 28 20 6f 6e 6f 66          if( onof
3e13e 66 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  f>0 ){.         
3e13f 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20     db->flags |= 
3e140 61 46 6c 61 67 4f 70 5b 69 5d 2e 6d 61 73 6b 3b  aFlagOp[i].mask;
3e141 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
3e142 20 69 66 28 20 6f 6e 6f 66 66 3d 3d 30 20 29 7b   if( onoff==0 ){
3e143 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d  .            db-
3e144 3e 66 6c 61 67 73 20 26 3d 20 7e 61 46 6c 61 67  >flags &= ~aFlag
3e145 4f 70 5b 69 5d 2e 6d 61 73 6b 3b 0a 20 20 20 20  Op[i].mask;.    
3e146 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
3e147 20 20 69 66 28 20 6f 6c 64 46 6c 61 67 73 21 3d    if( oldFlags!=
3e148 64 62 2d 3e 66 6c 61 67 73 20 29 7b 0a 20 20 20  db->flags ){.   
3e149 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
3e14a 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74  ExpirePreparedSt
3e14b 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20  atements(db);.  
3e14c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3e14d 20 20 20 20 69 66 28 20 70 52 65 73 20 29 7b 0a      if( pRes ){.
3e14e 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 52 65              *pRe
3e14f 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26  s = (db->flags &
3e150 20 61 46 6c 61 67 4f 70 5b 69 5d 2e 6d 61 73 6b   aFlagOp[i].mask
3e151 29 21 3d 30 3b 0a 20 20 20 20 20 20 20 20 20 20  )!=0;.          
3e152 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  }.          rc =
3e153 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
3e154 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3e155 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
3e156 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3e157 7d 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61  }.  }.  va_end(a
3e158 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
3e159 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  .}.../*.** Retur
3e15a 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 62 75  n true if the bu
3e15b 66 66 65 72 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 63  ffer z[0..n-1] c
3e15c 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 73 70 61 63  ontains all spac
3e15d 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
3e15e 74 20 61 6c 6c 53 70 61 63 65 73 28 63 6f 6e 73  t allSpaces(cons
3e15f 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e  t char *z, int n
3e160 29 7b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 30 20  ){.  while( n>0 
3e161 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 20 27 20 29  && z[n-1]==' ' )
3e162 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 72 65 74 75 72  { n--; }.  retur
3e163 6e 20 6e 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n n==0;.}../*.**
3e164 20 54 68 69 73 20 69 73 20 74 68 65 20 64 65 66   This is the def
3e165 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67 20 66  ault collating f
3e166 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 22 42  unction named "B
3e167 49 4e 41 52 59 22 20 77 68 69 63 68 20 69 73 20  INARY" which is 
3e168 61 6c 77 61 79 73 0a 2a 2a 20 61 76 61 69 6c 61  always.** availa
3e169 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ble..**.** If th
3e16a 65 20 70 61 64 46 6c 61 67 20 61 72 67 75 6d 65  e padFlag argume
3e16b 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74  nt is not NULL t
3e16c 68 65 6e 20 73 70 61 63 65 20 70 61 64 64 69 6e  hen space paddin
3e16d 67 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20  g at the end.** 
3e16e 6f 66 20 73 74 72 69 6e 67 73 20 69 73 20 69 67  of strings is ig
3e16f 6e 6f 72 65 64 2e 20 20 54 68 69 73 20 69 6d 70  nored.  This imp
3e170 6c 65 6d 65 6e 74 73 20 74 68 65 20 52 54 52 49  lements the RTRI
3e171 4d 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 2a 2f 0a  M collation..*/.
3e172 73 74 61 74 69 63 20 69 6e 74 20 62 69 6e 43 6f  static int binCo
3e173 6c 6c 46 75 6e 63 28 0a 20 20 76 6f 69 64 20 2a  llFunc(.  void *
3e174 70 61 64 46 6c 61 67 2c 0a 20 20 69 6e 74 20 6e  padFlag,.  int n
3e175 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64  Key1, const void
3e176 20 2a 70 4b 65 79 31 2c 0a 20 20 69 6e 74 20 6e   *pKey1,.  int n
3e177 4b 65 79 32 2c 20 63 6f 6e 73 74 20 76 6f 69 64  Key2, const void
3e178 20 2a 70 4b 65 79 32 0a 29 7b 0a 20 20 69 6e 74   *pKey2.){.  int
3e179 20 72 63 2c 20 6e 3b 0a 20 20 6e 20 3d 20 6e 4b   rc, n;.  n = nK
3e17a 65 79 31 3c 6e 4b 65 79 32 20 3f 20 6e 4b 65 79  ey1<nKey2 ? nKey
3e17b 31 20 3a 20 6e 4b 65 79 32 3b 0a 20 20 72 63 20  1 : nKey2;.  rc 
3e17c 3d 20 6d 65 6d 63 6d 70 28 70 4b 65 79 31 2c 20  = memcmp(pKey1, 
3e17d 70 4b 65 79 32 2c 20 6e 29 3b 0a 20 20 69 66 28  pKey2, n);.  if(
3e17e 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66   rc==0 ){.    if
3e17f 28 20 70 61 64 46 6c 61 67 0a 20 20 20 20 20 26  ( padFlag.     &
3e180 26 20 61 6c 6c 53 70 61 63 65 73 28 28 28 63 68  & allSpaces(((ch
3e181 61 72 2a 29 70 4b 65 79 31 29 2b 6e 2c 20 6e 4b  ar*)pKey1)+n, nK
3e182 65 79 31 2d 6e 29 0a 20 20 20 20 20 26 26 20 61  ey1-n).     && a
3e183 6c 6c 53 70 61 63 65 73 28 28 28 63 68 61 72 2a  llSpaces(((char*
3e184 29 70 4b 65 79 32 29 2b 6e 2c 20 6e 4b 65 79 32  )pKey2)+n, nKey2
3e185 2d 6e 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  -n).    ){.     
3e186 20 2f 2a 20 4c 65 61 76 65 20 72 63 20 75 6e 63   /* Leave rc unc
3e187 68 61 6e 67 65 64 20 61 74 20 30 20 2a 2f 0a 20  hanged at 0 */. 
3e188 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
3e189 72 63 20 3d 20 6e 4b 65 79 31 20 2d 20 6e 4b 65  rc = nKey1 - nKe
3e18a 79 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  y2;.    }.  }.  
3e18b 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
3e18c 0a 2a 2a 20 41 6e 6f 74 68 65 72 20 62 75 69 6c  .** Another buil
3e18d 74 2d 69 6e 20 63 6f 6c 6c 61 74 69 6e 67 20 73  t-in collating s
3e18e 65 71 75 65 6e 63 65 3a 20 4e 4f 43 41 53 45 2e  equence: NOCASE.
3e18f 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 6c   .**.** This col
3e190 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
3e191 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62  is intended to b
3e192 65 20 75 73 65 64 20 66 6f 72 20 22 63 61 73 65  e used for "case
3e193 20 69 6e 64 65 70 65 6e 64 61 6e 74 0a 2a 2a 20   independant.** 
3e194 63 6f 6d 70 61 72 69 73 6f 6e 22 2e 20 53 51 4c  comparison". SQL
3e195 69 74 65 27 73 20 6b 6e 6f 77 6c 65 64 67 65 20  ite's knowledge 
3e196 6f 66 20 75 70 70 65 72 20 61 6e 64 20 6c 6f 77  of upper and low
3e197 65 72 20 63 61 73 65 20 65 71 75 69 76 61 6c 65  er case equivale
3e198 6e 74 73 0a 2a 2a 20 65 78 74 65 6e 64 73 20 6f  nts.** extends o
3e199 6e 6c 79 20 74 6f 20 74 68 65 20 32 36 20 63 68  nly to the 26 ch
3e19a 61 72 61 63 74 65 72 73 20 75 73 65 64 20 69 6e  aracters used in
3e19b 20 74 68 65 20 45 6e 67 6c 69 73 68 20 6c 61 6e   the English lan
3e19c 67 75 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 74 20  guage..**.** At 
3e19d 74 68 65 20 6d 6f 6d 65 6e 74 20 74 68 65 72 65  the moment there
3e19e 20 69 73 20 6f 6e 6c 79 20 61 20 55 54 46 2d 38   is only a UTF-8
3e19f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
3e1a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
3e1a1 6f 63 61 73 65 43 6f 6c 6c 61 74 69 6e 67 46 75  ocaseCollatingFu
3e1a2 6e 63 28 0a 20 20 76 6f 69 64 20 2a 4e 6f 74 55  nc(.  void *NotU
3e1a3 73 65 64 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 31  sed,.  int nKey1
3e1a4 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
3e1a5 65 79 31 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 32  ey1,.  int nKey2
3e1a6 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
3e1a7 65 79 32 0a 29 7b 0a 20 20 69 6e 74 20 72 20 3d  ey2.){.  int r =
3e1a8 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
3e1a9 28 0a 20 20 20 20 20 20 28 63 6f 6e 73 74 20 63  (.      (const c
3e1aa 68 61 72 20 2a 29 70 4b 65 79 31 2c 20 28 63 6f  har *)pKey1, (co
3e1ab 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79 32  nst char *)pKey2
3e1ac 2c 20 28 6e 4b 65 79 31 3c 6e 4b 65 79 32 29 3f  , (nKey1<nKey2)?
3e1ad 6e 4b 65 79 31 3a 6e 4b 65 79 32 29 3b 0a 20 20  nKey1:nKey2);.  
3e1ae 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
3e1af 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28  (NotUsed);.  if(
3e1b0 20 30 3d 3d 72 20 29 7b 0a 20 20 20 20 72 20 3d   0==r ){.    r =
3e1b1 20 6e 4b 65 79 31 2d 6e 4b 65 79 32 3b 0a 20 20   nKey1-nKey2;.  
3e1b2 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a  }.  return r;.}.
3e1b3 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
3e1b4 65 20 52 4f 57 49 44 20 6f 66 20 74 68 65 20 6d  e ROWID of the m
3e1b5 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 73 65 72  ost recent inser
3e1b6 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  t.*/.SQLITE_API 
3e1b7 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c  sqlite_int64 sql
3e1b8 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
3e1b9 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 33 20 2a  _rowid(sqlite3 *
3e1ba 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62  db){.  return db
3e1bb 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 7d 0a 0a  ->lastRowid;.}..
3e1bc 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
3e1bd 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67   number of chang
3e1be 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72  es in the most r
3e1bf 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71  ecent call to sq
3e1c0 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2f  lite3_exec()..*/
3e1c1 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
3e1c2 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28  sqlite3_changes(
3e1c3 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
3e1c4 72 65 74 75 72 6e 20 64 62 2d 3e 6e 43 68 61 6e  return db->nChan
3e1c5 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ge;.}../*.** Ret
3e1c6 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
3e1c7 66 20 63 68 61 6e 67 65 73 20 73 69 6e 63 65 20  f changes since 
3e1c8 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
3e1c9 64 6c 65 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a  dle was opened..
3e1ca 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
3e1cb 74 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f  t sqlite3_total_
3e1cc 63 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20  changes(sqlite3 
3e1cd 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64  *db){.  return d
3e1ce 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b  b->nTotalChange;
3e1cf 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
3e1d0 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69  all open savepoi
3e1d1 6e 74 73 2e 20 54 68 69 73 20 66 75 6e 63 74 69  nts. This functi
3e1d2 6f 6e 20 6f 6e 6c 79 20 6d 61 6e 69 70 75 6c 61  on only manipula
3e1d3 74 65 73 20 66 69 65 6c 64 73 20 6f 66 20 74 68  tes fields of th
3e1d4 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 68 61  e.** database ha
3e1d5 6e 64 6c 65 20 6f 62 6a 65 63 74 2c 20 69 74 20  ndle object, it 
3e1d6 64 6f 65 73 20 6e 6f 74 20 63 6c 6f 73 65 20 61  does not close a
3e1d7 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 20 74 68  ny savepoints th
3e1d8 61 74 20 6d 61 79 20 62 65 20 6f 70 65 6e 0a 2a  at may be open.*
3e1d9 2a 20 61 74 20 74 68 65 20 62 2d 74 72 65 65 2f  * at the b-tree/
3e1da 70 61 67 65 72 20 6c 65 76 65 6c 2e 0a 2a 2f 0a  pager level..*/.
3e1db 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
3e1dc 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65  oid sqlite3Close
3e1dd 53 61 76 65 70 6f 69 6e 74 73 28 73 71 6c 69 74  Savepoints(sqlit
3e1de 65 33 20 2a 64 62 29 7b 0a 20 20 77 68 69 6c 65  e3 *db){.  while
3e1df 28 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74  ( db->pSavepoint
3e1e0 20 29 7b 0a 20 20 20 20 53 61 76 65 70 6f 69 6e   ){.    Savepoin
3e1e1 74 20 2a 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53  t *pTmp = db->pS
3e1e2 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 64 62  avepoint;.    db
3e1e3 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 70  ->pSavepoint = p
3e1e4 54 6d 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  Tmp->pNext;.    
3e1e5 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
3e1e6 2c 20 70 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 64  , pTmp);.  }.  d
3e1e7 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20  b->nSavepoint = 
3e1e8 30 3b 0a 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d  0;.  db->nStatem
3e1e9 65 6e 74 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 69  ent = 0;.  db->i
3e1ea 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65  sTransactionSave
3e1eb 70 6f 69 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  point = 0;.}../*
3e1ec 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 64  .** Invoke the d
3e1ed 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69  estructor functi
3e1ee 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  on associated wi
3e1ef 74 68 20 46 75 6e 63 44 65 66 20 70 2c 20 69 66  th FuncDef p, if
3e1f0 20 61 6e 79 2e 20 45 78 63 65 70 74 2c 0a 2a 2a   any. Except,.**
3e1f1 20 69 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20   if this is not 
3e1f2 74 68 65 20 6c 61 73 74 20 63 6f 70 79 20 6f 66  the last copy of
3e1f3 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c 20 64   the function, d
3e1f4 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 69 74 2e  o not invoke it.
3e1f5 20 4d 75 6c 74 69 70 6c 65 0a 2a 2a 20 63 6f 70   Multiple.** cop
3e1f6 69 65 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  ies of a single 
3e1f7 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 63 72 65  function are cre
3e1f8 61 74 65 64 20 77 68 65 6e 20 63 72 65 61 74 65  ated when create
3e1f9 5f 66 75 6e 63 74 69 6f 6e 28 29 20 69 73 20 63  _function() is c
3e1fa 61 6c 6c 65 64 0a 2a 2a 20 77 69 74 68 20 53 51  alled.** with SQ
3e1fb 4c 49 54 45 5f 41 4e 59 20 61 73 20 74 68 65 20  LITE_ANY as the 
3e1fc 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2f 0a 73 74 61  encoding..*/.sta
3e1fd 74 69 63 20 76 6f 69 64 20 66 75 6e 63 74 69 6f  tic void functio
3e1fe 6e 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33  nDestroy(sqlite3
3e1ff 20 2a 64 62 2c 20 46 75 6e 63 44 65 66 20 2a 70   *db, FuncDef *p
3e200 29 7b 0a 20 20 46 75 6e 63 44 65 73 74 72 75 63  ){.  FuncDestruc
3e201 74 6f 72 20 2a 70 44 65 73 74 72 75 63 74 6f 72  tor *pDestructor
3e202 20 3d 20 70 2d 3e 70 44 65 73 74 72 75 63 74 6f   = p->pDestructo
3e203 72 3b 0a 20 20 69 66 28 20 70 44 65 73 74 72 75  r;.  if( pDestru
3e204 63 74 6f 72 20 29 7b 0a 20 20 20 20 70 44 65 73  ctor ){.    pDes
3e205 74 72 75 63 74 6f 72 2d 3e 6e 52 65 66 2d 2d 3b  tructor->nRef--;
3e206 0a 20 20 20 20 69 66 28 20 70 44 65 73 74 72 75  .    if( pDestru
3e207 63 74 6f 72 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b  ctor->nRef==0 ){
3e208 0a 20 20 20 20 20 20 70 44 65 73 74 72 75 63 74  .      pDestruct
3e209 6f 72 2d 3e 78 44 65 73 74 72 6f 79 28 70 44 65  or->xDestroy(pDe
3e20a 73 74 72 75 63 74 6f 72 2d 3e 70 55 73 65 72 44  structor->pUserD
3e20b 61 74 61 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ata);.      sqli
3e20c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 44  te3DbFree(db, pD
3e20d 65 73 74 72 75 63 74 6f 72 29 3b 0a 20 20 20 20  estructor);.    
3e20e 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  }.  }.}../*.** D
3e20f 69 73 63 6f 6e 6e 65 63 74 20 61 6c 6c 20 73 71  isconnect all sq
3e210 6c 69 74 65 33 5f 76 74 61 62 20 6f 62 6a 65 63  lite3_vtab objec
3e211 74 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 20 74  ts that belong t
3e212 6f 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  o database conne
3e213 63 74 69 6f 6e 0a 2a 2a 20 64 62 2e 20 54 68 69  ction.** db. Thi
3e214 73 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  s is called when
3e215 20 64 62 20 69 73 20 62 65 69 6e 67 20 63 6c 6f   db is being clo
3e216 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  sed..*/.static v
3e217 6f 69 64 20 64 69 73 63 6f 6e 6e 65 63 74 41 6c  oid disconnectAl
3e218 6c 56 74 61 62 28 73 71 6c 69 74 65 33 20 2a 64  lVtab(sqlite3 *d
3e219 62 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  b){.#ifndef SQLI
3e21a 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
3e21b 41 42 4c 45 0a 20 20 69 6e 74 20 69 3b 0a 20 20  ABLE.  int i;.  
3e21c 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
3e21d 72 41 6c 6c 28 64 62 29 3b 0a 20 20 66 6f 72 28  rAll(db);.  for(
3e21e 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
3e21f 69 2b 2b 29 7b 0a 20 20 20 20 53 63 68 65 6d 61  i++){.    Schema
3e220 20 2a 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e   *pSchema = db->
3e221 61 44 62 5b 69 5d 2e 70 53 63 68 65 6d 61 3b 0a  aDb[i].pSchema;.
3e222 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b      if( db->aDb[
3e223 69 5d 2e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20  i].pSchema ){.  
3e224 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b      HashElem *p;
3e225 0a 20 20 20 20 20 20 66 6f 72 28 70 3d 73 71 6c  .      for(p=sql
3e226 69 74 65 48 61 73 68 46 69 72 73 74 28 26 70 53  iteHashFirst(&pS
3e227 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b  chema->tblHash);
3e228 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68   p; p=sqliteHash
3e229 4e 65 78 74 28 70 29 29 7b 0a 20 20 20 20 20 20  Next(p)){.      
3e22a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
3e22b 28 54 61 62 6c 65 20 2a 29 73 71 6c 69 74 65 48  (Table *)sqliteH
3e22c 61 73 68 44 61 74 61 28 70 29 3b 0a 20 20 20 20  ashData(p);.    
3e22d 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61      if( IsVirtua
3e22e 6c 28 70 54 61 62 29 20 29 20 73 71 6c 69 74 65  l(pTab) ) sqlite
3e22f 33 56 74 61 62 44 69 73 63 6f 6e 6e 65 63 74 28  3VtabDisconnect(
3e230 64 62 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20  db, pTab);.     
3e231 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73   }.    }.  }.  s
3e232 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
3e233 41 6c 6c 28 64 62 29 3b 0a 23 65 6c 73 65 0a 20  All(db);.#else. 
3e234 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
3e235 52 28 64 62 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a  R(db);.#endif.}.
3e236 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
3e237 55 45 20 69 66 20 64 61 74 61 62 61 73 65 20 63  UE if database c
3e238 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 68 61 73  onnection db has
3e239 20 75 6e 66 69 6e 61 6c 69 7a 65 64 20 70 72 65   unfinalized pre
3e23a 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65  pared.** stateme
3e23b 6e 74 73 20 6f 72 20 75 6e 66 69 6e 69 73 68 65  nts or unfinishe
3e23c 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  d sqlite3_backup
3e23d 20 6f 62 6a 65 63 74 73 2e 20 20 0a 2a 2f 0a 73   objects.  .*/.s
3e23e 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 6e 65 63  tatic int connec
3e23f 74 69 6f 6e 49 73 42 75 73 79 28 73 71 6c 69 74  tionIsBusy(sqlit
3e240 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 6a  e3 *db){.  int j
3e241 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
3e242 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
3e243 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69  b->mutex) );.  i
3e244 66 28 20 64 62 2d 3e 70 56 64 62 65 20 29 20 72  f( db->pVdbe ) r
3e245 65 74 75 72 6e 20 31 3b 0a 20 20 66 6f 72 28 6a  eturn 1;.  for(j
3e246 3d 30 3b 20 6a 3c 64 62 2d 3e 6e 44 62 3b 20 6a  =0; j<db->nDb; j
3e247 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  ++){.    Btree *
3e248 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d  pBt = db->aDb[j]
3e249 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42  .pBt;.    if( pB
3e24a 74 20 26 26 20 73 71 6c 69 74 65 33 42 74 72 65  t && sqlite3Btre
3e24b 65 49 73 49 6e 42 61 63 6b 75 70 28 70 42 74 29  eIsInBackup(pBt)
3e24c 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d   ) return 1;.  }
3e24d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
3e24e 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 65  /*.** Close an e
3e24f 78 69 73 74 69 6e 67 20 53 51 4c 69 74 65 20 64  xisting SQLite d
3e250 61 74 61 62 61 73 65 0a 2a 2f 0a 73 74 61 74 69  atabase.*/.stati
3e251 63 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6c 6f  c int sqlite3Clo
3e252 73 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  se(sqlite3 *db, 
3e253 69 6e 74 20 66 6f 72 63 65 5a 6f 6d 62 69 65 29  int forceZombie)
3e254 7b 0a 20 20 69 66 28 20 21 64 62 20 29 7b 0a 20  {.  if( !db ){. 
3e255 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3e256 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  _OK;.  }.  if( !
3e257 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
3e258 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29  ckSickOrOk(db) )
3e259 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
3e25a 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b 50 54 3b  ITE_MISUSE_BKPT;
3e25b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
3e25c 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
3e25d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 46 6f 72  utex);..  /* For
3e25e 63 65 20 78 44 69 73 63 6f 6e 6e 65 63 74 20 63  ce xDisconnect c
3e25f 61 6c 6c 73 20 6f 6e 20 61 6c 6c 20 76 69 72 74  alls on all virt
3e260 75 61 6c 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20  ual tables */.  
3e261 64 69 73 63 6f 6e 6e 65 63 74 41 6c 6c 56 74 61  disconnectAllVta
3e262 62 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  b(db);..  /* If 
3e263 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
3e264 20 6f 70 65 6e 2c 20 74 68 65 20 64 69 73 63 6f   open, the disco
3e265 6e 6e 65 63 74 41 6c 6c 56 74 61 62 28 29 20 63  nnectAllVtab() c
3e266 61 6c 6c 20 61 62 6f 76 65 0a 20 20 2a 2a 20 77  all above.  ** w
3e267 69 6c 6c 20 6e 6f 74 20 68 61 76 65 20 63 61 6c  ill not have cal
3e268 6c 65 64 20 74 68 65 20 78 44 69 73 63 6f 6e 6e  led the xDisconn
3e269 65 63 74 28 29 20 6d 65 74 68 6f 64 20 6f 6e 20  ect() method on 
3e26a 61 6e 79 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a  any virtual.  **
3e26b 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64   tables in the d
3e26c 62 2d 3e 61 56 54 72 61 6e 73 5b 5d 20 61 72 72  b->aVTrans[] arr
3e26d 61 79 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  ay. The followin
3e26e 67 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c  g sqlite3VtabRol
3e26f 6c 62 61 63 6b 28 29 0a 20 20 2a 2a 20 63 61 6c  lback().  ** cal
3e270 6c 20 77 69 6c 6c 20 64 6f 20 73 6f 2e 20 57 65  l will do so. We
3e271 20 6e 65 65 64 20 74 6f 20 64 6f 20 74 68 69 73   need to do this
3e272 20 62 65 66 6f 72 65 20 74 68 65 20 63 68 65 63   before the chec
3e273 6b 20 66 6f 72 20 61 63 74 69 76 65 0a 20 20 2a  k for active.  *
3e274 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  * SQL statements
3e275 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 65 20 76   below, as the v
3e276 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  -table implement
3e277 61 74 69 6f 6e 20 6d 61 79 20 62 65 20 73 74 6f  ation may be sto
3e278 72 69 6e 67 0a 20 20 2a 2a 20 73 6f 6d 65 20 70  ring.  ** some p
3e279 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
3e27a 74 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20  ts internally.. 
3e27b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 74 61   */.  sqlite3Vta
3e27c 62 52 6f 6c 6c 62 61 63 6b 28 64 62 29 3b 0a 0a  bRollback(db);..
3e27d 20 20 2f 2a 20 4c 65 67 61 63 79 20 62 65 68 61    /* Legacy beha
3e27e 76 69 6f 72 20 28 73 71 6c 69 74 65 33 5f 63 6c  vior (sqlite3_cl
3e27f 6f 73 65 28 29 20 62 65 68 61 76 69 6f 72 29 20  ose() behavior) 
3e280 69 73 20 74 6f 20 72 65 74 75 72 6e 0a 20 20 2a  is to return.  *
3e281 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 66  * SQLITE_BUSY if
3e282 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   the connection 
3e283 63 61 6e 20 6e 6f 74 20 62 65 20 63 6c 6f 73 65  can not be close
3e284 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20  d immediately.. 
3e285 20 2a 2f 0a 20 20 69 66 28 20 21 66 6f 72 63 65   */.  if( !force
3e286 5a 6f 6d 62 69 65 20 26 26 20 63 6f 6e 6e 65 63  Zombie && connec
3e287 74 69 6f 6e 49 73 42 75 73 79 28 64 62 29 20 29  tionIsBusy(db) )
3e288 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
3e289 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 42 55  or(db, SQLITE_BU
3e28a 53 59 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 63  SY, "unable to c
3e28b 6c 6f 73 65 20 64 75 65 20 74 6f 20 75 6e 66 69  lose due to unfi
3e28c 6e 61 6c 69 7a 65 64 20 22 0a 20 20 20 20 20 20  nalized ".      
3e28d 20 22 73 74 61 74 65 6d 65 6e 74 73 20 6f 72 20   "statements or 
3e28e 75 6e 66 69 6e 69 73 68 65 64 20 62 61 63 6b 75  unfinished backu
3e28f 70 73 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ps");.    sqlite
3e290 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
3e291 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65  ->mutex);.    re
3e292 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
3e293 3b 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51  ;.  }..#ifdef SQ
3e294 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 51 4c 4c  LITE_ENABLE_SQLL
3e295 4f 47 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  OG.  if( sqlite3
3e296 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 53 71  GlobalConfig.xSq
3e297 6c 6c 6f 67 20 29 7b 0a 20 20 20 20 2f 2a 20 43  llog ){.    /* C
3e298 6c 6f 73 69 6e 67 20 74 68 65 20 68 61 6e 64 6c  losing the handl
3e299 65 2e 20 46 6f 75 72 74 68 20 70 61 72 61 6d 65  e. Fourth parame
3e29a 74 65 72 20 69 73 20 70 61 73 73 65 64 20 74 68  ter is passed th
3e29b 65 20 76 61 6c 75 65 20 32 2e 20 2a 2f 0a 20 20  e value 2. */.  
3e29c 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
3e29d 6f 6e 66 69 67 2e 78 53 71 6c 6c 6f 67 28 73 71  onfig.xSqllog(sq
3e29e 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3e29f 67 2e 70 53 71 6c 6c 6f 67 41 72 67 2c 20 64 62  g.pSqllogArg, db
3e2a0 2c 20 30 2c 20 32 29 3b 0a 20 20 7d 0a 23 65 6e  , 0, 2);.  }.#en
3e2a1 64 69 66 0a 0a 20 20 2f 2a 20 43 6f 6e 76 65 72  dif..  /* Conver
3e2a2 74 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  t the connection
3e2a3 20 69 6e 74 6f 20 61 20 7a 6f 6d 62 69 65 20 61   into a zombie a
3e2a4 6e 64 20 74 68 65 6e 20 63 6c 6f 73 65 20 69 74  nd then close it
3e2a5 2e 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 6d 61 67  ..  */.  db->mag
3e2a6 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49  ic = SQLITE_MAGI
3e2a7 43 5f 5a 4f 4d 42 49 45 3b 0a 20 20 73 71 6c 69  C_ZOMBIE;.  sqli
3e2a8 74 65 33 4c 65 61 76 65 4d 75 74 65 78 41 6e 64  te3LeaveMutexAnd
3e2a9 43 6c 6f 73 65 5a 6f 6d 62 69 65 28 64 62 29 3b  CloseZombie(db);
3e2aa 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3e2ab 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77  _OK;.}../*.** Tw
3e2ac 6f 20 76 61 72 69 61 74 69 6f 6e 73 20 6f 6e 20  o variations on 
3e2ad 74 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72  the public inter
3e2ae 66 61 63 65 20 66 6f 72 20 63 6c 6f 73 69 6e 67  face for closing
3e2af 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63   a database.** c
3e2b0 6f 6e 6e 65 63 74 69 6f 6e 2e 20 54 68 65 20 73  onnection. The s
3e2b1 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 20 76  qlite3_close() v
3e2b2 65 72 73 69 6f 6e 20 72 65 74 75 72 6e 73 20 53  ersion returns S
3e2b3 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 0a 2a  QLITE_BUSY and.*
3e2b4 2a 20 6c 65 61 76 65 73 20 74 68 65 20 63 6f 6e  * leaves the con
3e2b5 6e 65 63 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69  nection option i
3e2b6 66 20 74 68 65 72 65 20 61 72 65 20 75 6e 66 69  f there are unfi
3e2b7 6e 61 6c 69 7a 65 64 20 70 72 65 70 61 72 65 64  nalized prepared
3e2b8 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 6f  .** statements o
3e2b9 72 20 75 6e 66 69 6e 69 73 68 65 64 20 73 71 6c  r unfinished sql
3e2ba 69 74 65 33 5f 62 61 63 6b 75 70 73 2e 20 20 54  ite3_backups.  T
3e2bb 68 65 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  he sqlite3_close
3e2bc 5f 76 32 28 29 0a 2a 2a 20 76 65 72 73 69 6f 6e  _v2().** version
3e2bd 20 66 6f 72 63 65 73 20 74 68 65 20 63 6f 6e 6e   forces the conn
3e2be 65 63 74 69 6f 6e 20 74 6f 20 62 65 63 6f 6d 65  ection to become
3e2bf 20 61 20 7a 6f 6d 62 69 65 20 69 66 20 74 68 65   a zombie if the
3e2c0 72 65 20 61 72 65 0a 2a 2a 20 75 6e 63 6c 6f 73  re are.** unclos
3e2c1 65 64 20 72 65 73 6f 75 72 63 65 73 2c 20 61 6e  ed resources, an
3e2c2 64 20 61 72 72 61 6e 67 65 73 20 66 6f 72 20 64  d arranges for d
3e2c3 65 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 65 6e  eallocation when
3e2c4 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 70 72 65   the last.** pre
3e2c5 70 61 72 65 20 73 74 61 74 65 6d 65 6e 74 20 6f  pare statement o
3e2c6 72 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  r sqlite3_backup
3e2c7 20 63 6c 6f 73 65 73 2e 0a 2a 2f 0a 53 51 4c 49   closes..*/.SQLI
3e2c8 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3e2c9 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33  e3_close(sqlite3
3e2ca 20 2a 64 62 29 7b 20 72 65 74 75 72 6e 20 73 71   *db){ return sq
3e2cb 6c 69 74 65 33 43 6c 6f 73 65 28 64 62 2c 30 29  lite3Close(db,0)
3e2cc 3b 20 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ; }.SQLITE_API i
3e2cd 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  nt sqlite3_close
3e2ce 5f 76 32 28 73 71 6c 69 74 65 33 20 2a 64 62 29  _v2(sqlite3 *db)
3e2cf 7b 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33  { return sqlite3
3e2d0 43 6c 6f 73 65 28 64 62 2c 31 29 3b 20 7d 0a 0a  Close(db,1); }..
3e2d1 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65  ./*.** Close the
3e2d2 20 6d 75 74 65 78 20 6f 6e 20 64 61 74 61 62 61   mutex on databa
3e2d3 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62  se connection db
3e2d4 2e 0a 2a 2a 0a 2a 2a 20 46 75 72 74 68 65 72 6d  ..**.** Furtherm
3e2d5 6f 72 65 2c 20 69 66 20 64 61 74 61 62 61 73 65  ore, if database
3e2d6 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 69   connection db i
3e2d7 73 20 61 20 7a 6f 6d 62 69 65 20 28 6d 65 61 6e  s a zombie (mean
3e2d8 69 6e 67 20 74 68 61 74 20 74 68 65 72 65 0a 2a  ing that there.*
3e2d9 2a 20 68 61 73 20 62 65 65 6e 20 61 20 70 72 69  * has been a pri
3e2da 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  or call to sqlit
3e2db 65 33 5f 63 6c 6f 73 65 28 64 62 29 20 6f 72 20  e3_close(db) or 
3e2dc 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5f 76 32  sqlite3_close_v2
3e2dd 28 64 62 29 29 20 61 6e 64 0a 2a 2a 20 65 76 65  (db)) and.** eve
3e2de 72 79 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ry sqlite3_stmt 
3e2df 68 61 73 20 6e 6f 77 20 62 65 65 6e 20 66 69 6e  has now been fin
3e2e0 61 6c 69 7a 65 64 20 61 6e 64 20 65 76 65 72 79  alized and every
3e2e1 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
3e2e2 68 61 73 0a 2a 2a 20 66 69 6e 69 73 68 65 64 2c  has.** finished,
3e2e3 20 74 68 65 6e 20 66 72 65 65 20 61 6c 6c 20 72   then free all r
3e2e4 65 73 6f 75 72 63 65 73 2e 0a 2a 2f 0a 53 51 4c  esources..*/.SQL
3e2e5 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
3e2e6 20 73 71 6c 69 74 65 33 4c 65 61 76 65 4d 75 74   sqlite3LeaveMut
3e2e7 65 78 41 6e 64 43 6c 6f 73 65 5a 6f 6d 62 69 65  exAndCloseZombie
3e2e8 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
3e2e9 20 48 61 73 68 45 6c 65 6d 20 2a 69 3b 20 20 20   HashElem *i;   
3e2ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e2eb 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 69   /* Hash table i
3e2ec 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74  terator */.  int
3e2ed 20 6a 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   j;..  /* If the
3e2ee 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69  re are outstandi
3e2ef 6e 67 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ng sqlite3_stmt 
3e2f0 6f 72 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  or sqlite3_backu
3e2f1 70 20 6f 62 6a 65 63 74 73 0a 20 20 2a 2a 20 6f  p objects.  ** o
3e2f2 72 20 69 66 20 74 68 65 20 63 6f 6e 6e 65 63 74  r if the connect
3e2f3 69 6f 6e 20 68 61 73 20 6e 6f 74 20 79 65 74 20  ion has not yet 
3e2f4 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 73  been closed by s
3e2f5 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5f 76 32 28  qlite3_close_v2(
3e2f6 29 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 6a 75 73  ),.  ** then jus
3e2f7 74 20 6c 65 61 76 65 20 74 68 65 20 6d 75 74 65  t leave the mute
3e2f8 78 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20  x and return..  
3e2f9 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 67  */.  if( db->mag
3e2fa 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43  ic!=SQLITE_MAGIC
3e2fb 5f 5a 4f 4d 42 49 45 20 7c 7c 20 63 6f 6e 6e 65  _ZOMBIE || conne
3e2fc 63 74 69 6f 6e 49 73 42 75 73 79 28 64 62 29 20  ctionIsBusy(db) 
3e2fd 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ){.    sqlite3_m
3e2fe 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
3e2ff 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 75 72  utex);.    retur
3e300 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  n;.  }..  /* If 
3e301 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70 6f  we reach this po
3e302 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  int, it means th
3e303 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  at the database 
3e304 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 0a 20  connection has. 
3e305 20 2a 2a 20 63 6c 6f 73 65 64 20 61 6c 6c 20 73   ** closed all s
3e306 71 6c 69 74 65 33 5f 73 74 6d 74 20 61 6e 64 20  qlite3_stmt and 
3e307 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f  sqlite3_backup o
3e308 62 6a 65 63 74 73 20 61 6e 64 20 68 61 73 20 62  bjects and has b
3e309 65 65 6e 0a 20 20 2a 2a 20 70 61 73 65 64 20 74  een.  ** pased t
3e30a 6f 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 20  o sqlite3_close 
3e30b 28 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 69 74  (meaning that it
3e30c 20 69 73 20 61 20 7a 6f 6d 62 69 65 29 2e 20 20   is a zombie).  
3e30d 54 68 65 72 65 66 6f 72 65 2c 0a 20 20 2a 2a 20  Therefore,.  ** 
3e30e 67 6f 20 61 68 65 61 64 20 61 6e 64 20 66 72 65  go ahead and fre
3e30f 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 2e  e all resources.
3e310 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 46 72 65 65  .  */..  /* Free
3e311 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67   any outstanding
3e312 20 53 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63   Savepoint struc
3e313 74 75 72 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69  tures. */.  sqli
3e314 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e  te3CloseSavepoin
3e315 74 73 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 43 6c  ts(db);..  /* Cl
3e316 6f 73 65 20 61 6c 6c 20 64 61 74 61 62 61 73 65  ose all database
3e317 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a   connections */.
3e318 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 64 62 2d    for(j=0; j<db-
3e319 3e 6e 44 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  >nDb; j++){.    
3e31a 73 74 72 75 63 74 20 44 62 20 2a 70 44 62 20 3d  struct Db *pDb =
3e31b 20 26 64 62 2d 3e 61 44 62 5b 6a 5d 3b 0a 20 20   &db->aDb[j];.  
3e31c 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 20 29    if( pDb->pBt )
3e31d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  {.      sqlite3B
3e31e 74 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70  treeClose(pDb->p
3e31f 42 74 29 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e  Bt);.      pDb->
3e320 70 42 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  pBt = 0;.      i
3e321 66 28 20 6a 21 3d 31 20 29 7b 0a 20 20 20 20 20  f( j!=1 ){.     
3e322 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20     pDb->pSchema 
3e323 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
3e324 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 43 6c 65 61   }.  }.  /* Clea
3e325 72 20 74 68 65 20 54 45 4d 50 20 73 63 68 65 6d  r the TEMP schem
3e326 61 20 73 65 70 61 72 61 74 65 6c 79 20 61 6e 64  a separately and
3e327 20 6c 61 73 74 20 2a 2f 0a 20 20 69 66 28 20 64   last */.  if( d
3e328 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d  b->aDb[1].pSchem
3e329 61 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  a ){.    sqlite3
3e32a 53 63 68 65 6d 61 43 6c 65 61 72 28 64 62 2d 3e  SchemaClear(db->
3e32b 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 29 3b  aDb[1].pSchema);
3e32c 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 74  .  }.  sqlite3Vt
3e32d 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 64 62 29  abUnlockList(db)
3e32e 3b 0a 0a 20 20 2f 2a 20 46 72 65 65 20 75 70 20  ;..  /* Free up 
3e32f 74 68 65 20 61 72 72 61 79 20 6f 66 20 61 75 78  the array of aux
3e330 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 73  iliary databases
3e331 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 43 6f 6c   */.  sqlite3Col
3e332 6c 61 70 73 65 44 61 74 61 62 61 73 65 41 72 72  lapseDatabaseArr
3e333 61 79 28 64 62 29 3b 0a 20 20 61 73 73 65 72 74  ay(db);.  assert
3e334 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 29 3b 0a  ( db->nDb<=2 );.
3e335 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
3e336 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63  b==db->aDbStatic
3e337 20 29 3b 0a 0a 20 20 2f 2a 20 54 65 6c 6c 20 74   );..  /* Tell t
3e338 68 65 20 63 6f 64 65 20 69 6e 20 6e 6f 74 69 66  he code in notif
3e339 79 2e 63 20 74 68 61 74 20 74 68 65 20 63 6f 6e  y.c that the con
3e33a 6e 65 63 74 69 6f 6e 20 6e 6f 20 6c 6f 6e 67 65  nection no longe
3e33b 72 20 68 6f 6c 64 73 20 61 6e 79 0a 20 20 2a 2a  r holds any.  **
3e33c 20 6c 6f 63 6b 73 20 61 6e 64 20 64 6f 65 73 20   locks and does 
3e33d 6e 6f 74 20 72 65 71 75 69 72 65 20 61 6e 79 20  not require any 
3e33e 66 75 72 74 68 65 72 20 75 6e 6c 6f 63 6b 2d 6e  further unlock-n
3e33f 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 73 2e  otify callbacks.
3e340 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 43  .  */.  sqlite3C
3e341 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28  onnectionClosed(
3e342 64 62 29 3b 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b  db);..  for(j=0;
3e343 20 6a 3c 41 72 72 61 79 53 69 7a 65 28 64 62 2d   j<ArraySize(db-
3e344 3e 61 46 75 6e 63 2e 61 29 3b 20 6a 2b 2b 29 7b  >aFunc.a); j++){
3e345 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 4e  .    FuncDef *pN
3e346 65 78 74 2c 20 2a 70 48 61 73 68 2c 20 2a 70 3b  ext, *pHash, *p;
3e347 0a 20 20 20 20 66 6f 72 28 70 3d 64 62 2d 3e 61  .    for(p=db->a
3e348 46 75 6e 63 2e 61 5b 6a 5d 3b 20 70 3b 20 70 3d  Func.a[j]; p; p=
3e349 70 48 61 73 68 29 7b 0a 20 20 20 20 20 20 70 48  pHash){.      pH
3e34a 61 73 68 20 3d 20 70 2d 3e 70 48 61 73 68 3b 0a  ash = p->pHash;.
3e34b 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 20 29        while( p )
3e34c 7b 0a 20 20 20 20 20 20 20 20 66 75 6e 63 74 69  {.        functi
3e34d 6f 6e 44 65 73 74 72 6f 79 28 64 62 2c 20 70 29  onDestroy(db, p)
3e34e 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74 20  ;.        pNext 
3e34f 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  = p->pNext;.    
3e350 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
3e351 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20  e(db, p);.      
3e352 20 20 70 20 3d 20 70 4e 65 78 74 3b 0a 20 20 20    p = pNext;.   
3e353 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
3e354 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73   for(i=sqliteHas
3e355 68 46 69 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c  hFirst(&db->aCol
3e356 6c 53 65 71 29 3b 20 69 3b 20 69 3d 73 71 6c 69  lSeq); i; i=sqli
3e357 74 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a  teHashNext(i)){.
3e358 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
3e359 6c 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29  ll = (CollSeq *)
3e35a 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69  sqliteHashData(i
3e35b 29 3b 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65  );.    /* Invoke
3e35c 20 61 6e 79 20 64 65 73 74 72 75 63 74 6f 72 73   any destructors
3e35d 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20   registered for 
3e35e 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
3e35f 63 65 20 75 73 65 72 20 64 61 74 61 2e 20 2a 2f  ce user data. */
3e360 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
3e361 33 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69  3; j++){.      i
3e362 66 28 20 70 43 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c  f( pColl[j].xDel
3e363 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c   ){.        pCol
3e364 6c 5b 6a 5d 2e 78 44 65 6c 28 70 43 6f 6c 6c 5b  l[j].xDel(pColl[
3e365 6a 5d 2e 70 55 73 65 72 29 3b 0a 20 20 20 20 20  j].pUser);.     
3e366 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
3e367 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
3e368 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  Coll);.  }.  sql
3e369 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 64  ite3HashClear(&d
3e36a 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 0a 23 69  b->aCollSeq);.#i
3e36b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
3e36c 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
3e36d 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73   for(i=sqliteHas
3e36e 68 46 69 72 73 74 28 26 64 62 2d 3e 61 4d 6f 64  hFirst(&db->aMod
3e36f 75 6c 65 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74  ule); i; i=sqlit
3e370 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20  eHashNext(i)){. 
3e371 20 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 20     Module *pMod 
3e372 3d 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69  = (Module *)sqli
3e373 74 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20  teHashData(i);. 
3e374 20 20 20 69 66 28 20 70 4d 6f 64 2d 3e 78 44 65     if( pMod->xDe
3e375 73 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 70  stroy ){.      p
3e376 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79 28 70 4d  Mod->xDestroy(pM
3e377 6f 64 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 7d  od->pAux);.    }
3e378 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
3e379 65 65 28 64 62 2c 20 70 4d 6f 64 29 3b 0a 20 20  ee(db, pMod);.  
3e37a 7d 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43  }.  sqlite3HashC
3e37b 6c 65 61 72 28 26 64 62 2d 3e 61 4d 6f 64 75 6c  lear(&db->aModul
3e37c 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71  e);.#endif..  sq
3e37d 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
3e37e 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 20 2f 2a  QLITE_OK, 0); /*
3e37f 20 44 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79   Deallocates any
3e380 20 63 61 63 68 65 64 20 65 72 72 6f 72 20 73 74   cached error st
3e381 72 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 66 28 20  rings. */.  if( 
3e382 64 62 2d 3e 70 45 72 72 20 29 7b 0a 20 20 20 20  db->pErr ){.    
3e383 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
3e384 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 7d 0a  (db->pErr);.  }.
3e385 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78    sqlite3CloseEx
3e386 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a 0a 20  tensions(db);.. 
3e387 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c   db->magic = SQL
3e388 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b  ITE_MAGIC_ERROR;
3e389 0a 0a 20 20 2f 2a 20 54 68 65 20 74 65 6d 70 2d  ..  /* The temp-
3e38a 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
3e38b 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 64 69 66  is allocated dif
3e38c 66 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20 74 68  ferently from th
3e38d 65 20 6f 74 68 65 72 20 73 63 68 65 6d 61 0a 20  e other schema. 
3e38e 20 2a 2a 20 6f 62 6a 65 63 74 73 20 28 75 73 69   ** objects (usi
3e38f 6e 67 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  ng sqliteMalloc(
3e390 29 20 64 69 72 65 63 74 6c 79 2c 20 69 6e 73 74  ) directly, inst
3e391 65 61 64 20 6f 66 20 73 71 6c 69 74 65 33 42 74  ead of sqlite3Bt
3e392 72 65 65 53 63 68 65 6d 61 28 29 29 2e 0a 20 20  reeSchema())..  
3e393 2a 2a 20 53 6f 20 69 74 20 6e 65 65 64 73 20 74  ** So it needs t
3e394 6f 20 62 65 20 66 72 65 65 64 20 68 65 72 65 2e  o be freed here.
3e395 20 54 6f 64 6f 3a 20 57 68 79 20 6e 6f 74 20 72   Todo: Why not r
3e396 6f 6c 6c 20 74 68 65 20 74 65 6d 70 20 73 63 68  oll the temp sch
3e397 65 6d 61 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68  ema into.  ** th
3e398 65 20 73 61 6d 65 20 73 71 6c 69 74 65 4d 61 6c  e same sqliteMal
3e399 6c 6f 63 28 29 20 61 73 20 74 68 65 20 6f 6e 65  loc() as the one
3e39a 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 65 73 20   that allocates 
3e39b 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20  the database .  
3e39c 2a 2a 20 73 74 72 75 63 74 75 72 65 3f 0a 20 20  ** structure?.  
3e39d 2a 2f 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  */.  sqlite3DbFr
3e39e 65 65 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b 31  ee(db, db->aDb[1
3e39f 5d 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71  ].pSchema);.  sq
3e3a0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
3e3a1 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
3e3a2 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49  db->magic = SQLI
3e3a3 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 3b  TE_MAGIC_CLOSED;
3e3a4 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3e3a5 5f 66 72 65 65 28 64 62 2d 3e 6d 75 74 65 78 29  _free(db->mutex)
3e3a6 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ;.  assert( db->
3e3a7 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3d 3d  lookaside.nOut==
3e3a8 30 20 29 3b 20 20 2f 2a 20 46 61 69 6c 73 20 6f  0 );  /* Fails o
3e3a9 6e 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65  n a lookaside me
3e3aa 6d 6f 72 79 20 6c 65 61 6b 20 2a 2f 0a 20 20 69  mory leak */.  i
3e3ab 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  f( db->lookaside
3e3ac 2e 62 4d 61 6c 6c 6f 63 65 64 20 29 7b 0a 20 20  .bMalloced ){.  
3e3ad 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64    sqlite3_free(d
3e3ae 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74  b->lookaside.pSt
3e3af 61 72 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  art);.  }.  sqli
3e3b0 74 65 33 5f 66 72 65 65 28 64 62 29 3b 0a 7d 0a  te3_free(db);.}.
3e3b1 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20  ./*.** Rollback 
3e3b2 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  all database fil
3e3b3 65 73 2e 20 20 49 66 20 74 72 69 70 43 6f 64 65  es.  If tripCode
3e3b4 20 69 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4f   is not SQLITE_O
3e3b5 4b 2c 20 74 68 65 6e 0a 2a 2a 20 61 6e 79 20 6f  K, then.** any o
3e3b6 70 65 6e 20 63 75 72 73 6f 72 73 20 61 72 65 20  pen cursors are 
3e3b7 69 6e 76 61 6c 69 64 61 74 65 64 20 28 22 74 72  invalidated ("tr
3e3b8 69 70 70 65 64 22 20 2d 20 61 73 20 69 6e 20 22  ipped" - as in "
3e3b9 74 72 69 70 70 69 6e 67 20 61 20 63 69 72 63 75  tripping a circu
3e3ba 69 74 0a 2a 2a 20 62 72 65 61 6b 65 72 22 29 20  it.** breaker") 
3e3bb 61 6e 64 20 6d 61 64 65 20 74 6f 20 72 65 74 75  and made to retu
3e3bc 72 6e 20 74 72 69 70 43 6f 64 65 20 69 66 20 74  rn tripCode if t
3e3bd 68 65 72 65 20 61 72 65 20 61 6e 79 20 66 75 72  here are any fur
3e3be 74 68 65 72 0a 2a 2a 20 61 74 74 65 6d 70 74 73  ther.** attempts
3e3bf 20 74 6f 20 75 73 65 20 74 68 61 74 20 63 75 72   to use that cur
3e3c0 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  sor..*/.SQLITE_P
3e3c1 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
3e3c2 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 73  te3RollbackAll(s
3e3c3 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
3e3c4 74 72 69 70 43 6f 64 65 29 7b 0a 20 20 69 6e 74  tripCode){.  int
3e3c5 20 69 3b 0a 20 20 69 6e 74 20 69 6e 54 72 61 6e   i;.  int inTran
3e3c6 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28  s = 0;.  assert(
3e3c7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
3e3c8 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
3e3c9 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  ;.  sqlite3Begin
3e3ca 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
3e3cb 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
3e3cc 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
3e3cd 42 74 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61  Btree *p = db->a
3e3ce 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69  Db[i].pBt;.    i
3e3cf 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 69 66  f( p ){.      if
3e3d0 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  ( sqlite3BtreeIs
3e3d1 49 6e 54 72 61 6e 73 28 70 29 20 29 7b 0a 20 20  InTrans(p) ){.  
3e3d2 20 20 20 20 20 20 69 6e 54 72 61 6e 73 20 3d 20        inTrans = 
3e3d3 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
3e3d4 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c   sqlite3BtreeRol
3e3d5 6c 62 61 63 6b 28 70 2c 20 74 72 69 70 43 6f 64  lback(p, tripCod
3e3d6 65 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44  e);.      db->aD
3e3d7 62 5b 69 5d 2e 69 6e 54 72 61 6e 73 20 3d 20 30  b[i].inTrans = 0
3e3d8 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
3e3d9 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63  lite3VtabRollbac
3e3da 6b 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33  k(db);.  sqlite3
3e3db 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
3e3dc 29 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c  );..  if( db->fl
3e3dd 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72  ags&SQLITE_Inter
3e3de 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20  nChanges ){.    
3e3df 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
3e3e0 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
3e3e1 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  db);.    sqlite3
3e3e2 52 65 73 65 74 41 6c 6c 53 63 68 65 6d 61 73 4f  ResetAllSchemasO
3e3e3 66 43 6f 6e 6e 65 63 74 69 6f 6e 28 64 62 29 3b  fConnection(db);
3e3e4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 79 20 64  .  }..  /* Any d
3e3e5 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69  eferred constrai
3e3e6 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 73 20 68 61  nt violations ha
3e3e7 76 65 20 6e 6f 77 20 62 65 65 6e 20 72 65 73 6f  ve now been reso
3e3e8 6c 76 65 64 2e 20 2a 2f 0a 20 20 64 62 2d 3e 6e  lved. */.  db->n
3e3e9 44 65 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 30  DeferredCons = 0
3e3ea 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 68  ;..  /* If one h
3e3eb 61 73 20 62 65 65 6e 20 63 6f 6e 66 69 67 75 72  as been configur
3e3ec 65 64 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 72  ed, invoke the r
3e3ed 6f 6c 6c 62 61 63 6b 2d 68 6f 6f 6b 20 63 61 6c  ollback-hook cal
3e3ee 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28 20 64  lback */.  if( d
3e3ef 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c  b->xRollbackCall
3e3f0 62 61 63 6b 20 26 26 20 28 69 6e 54 72 61 6e 73  back && (inTrans
3e3f1 20 7c 7c 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d   || !db->autoCom
3e3f2 6d 69 74 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e  mit) ){.    db->
3e3f3 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63  xRollbackCallbac
3e3f4 6b 28 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41  k(db->pRollbackA
3e3f5 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  rg);.  }.}../*.*
3e3f6 2a 20 52 65 74 75 72 6e 20 61 20 73 74 61 74 69  * Return a stati
3e3f7 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 64 65  c string that de
3e3f8 73 63 72 69 62 65 73 20 74 68 65 20 6b 69 6e 64  scribes the kind
3e3f9 20 6f 66 20 65 72 72 6f 72 20 73 70 65 63 69 66   of error specif
3e3fa 69 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72  ied in the.** ar
3e3fb 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  gument..*/.SQLIT
3e3fc 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
3e3fd 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45 72 72  char *sqlite3Err
3e3fe 53 74 72 28 69 6e 74 20 72 63 29 7b 0a 20 20 73  Str(int rc){.  s
3e3ff 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
3e400 2a 20 63 6f 6e 73 74 20 61 4d 73 67 5b 5d 20 3d  * const aMsg[] =
3e401 20 7b 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45   {.    /* SQLITE
3e402 5f 4f 4b 20 20 20 20 20 20 20 20 20 20 2a 2f 20  _OK          */ 
3e403 22 6e 6f 74 20 61 6e 20 65 72 72 6f 72 22 2c 0a  "not an error",.
3e404 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 45 52      /* SQLITE_ER
3e405 52 4f 52 20 20 20 20 20 20 20 2a 2f 20 22 53 51  ROR       */ "SQ
3e406 4c 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 6f 72  L logic error or
3e407 20 6d 69 73 73 69 6e 67 20 64 61 74 61 62 61 73   missing databas
3e408 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  e",.    /* SQLIT
3e409 45 5f 49 4e 54 45 52 4e 41 4c 20 20 20 20 2a 2f  E_INTERNAL    */
3e40a 20 30 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54   0,.    /* SQLIT
3e40b 45 5f 50 45 52 4d 20 20 20 20 20 20 20 20 2a 2f  E_PERM        */
3e40c 20 22 61 63 63 65 73 73 20 70 65 72 6d 69 73 73   "access permiss
3e40d 69 6f 6e 20 64 65 6e 69 65 64 22 2c 0a 20 20 20  ion denied",.   
3e40e 20 2f 2a 20 53 51 4c 49 54 45 5f 41 42 4f 52 54   /* SQLITE_ABORT
3e40f 20 20 20 20 20 20 20 2a 2f 20 22 63 61 6c 6c 62         */ "callb
3e410 61 63 6b 20 72 65 71 75 65 73 74 65 64 20 71 75  ack requested qu
3e411 65 72 79 20 61 62 6f 72 74 22 2c 0a 20 20 20 20  ery abort",.    
3e412 2f 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 20  /* SQLITE_BUSY  
3e413 20 20 20 20 20 20 2a 2f 20 22 64 61 74 61 62 61        */ "databa
3e414 73 65 20 69 73 20 6c 6f 63 6b 65 64 22 2c 0a 20  se is locked",. 
3e415 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43     /* SQLITE_LOC
3e416 4b 45 44 20 20 20 20 20 20 2a 2f 20 22 64 61 74  KED      */ "dat
3e417 61 62 61 73 65 20 74 61 62 6c 65 20 69 73 20 6c  abase table is l
3e418 6f 63 6b 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53  ocked",.    /* S
3e419 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 20  QLITE_NOMEM     
3e41a 20 20 2a 2f 20 22 6f 75 74 20 6f 66 20 6d 65 6d    */ "out of mem
3e41b 6f 72 79 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  ory",.    /* SQL
3e41c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20  ITE_READONLY    
3e41d 2a 2f 20 22 61 74 74 65 6d 70 74 20 74 6f 20 77  */ "attempt to w
3e41e 72 69 74 65 20 61 20 72 65 61 64 6f 6e 6c 79 20  rite a readonly 
3e41f 64 61 74 61 62 61 73 65 22 2c 0a 20 20 20 20 2f  database",.    /
3e420 2a 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  * SQLITE_INTERRU
3e421 50 54 20 20 20 2a 2f 20 22 69 6e 74 65 72 72 75  PT   */ "interru
3e422 70 74 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51  pted",.    /* SQ
3e423 4c 49 54 45 5f 49 4f 45 52 52 20 20 20 20 20 20  LITE_IOERR      
3e424 20 2a 2f 20 22 64 69 73 6b 20 49 2f 4f 20 65 72   */ "disk I/O er
3e425 72 6f 72 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  ror",.    /* SQL
3e426 49 54 45 5f 43 4f 52 52 55 50 54 20 20 20 20 20  ITE_CORRUPT     
3e427 2a 2f 20 22 64 61 74 61 62 61 73 65 20 64 69 73  */ "database dis
3e428 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f  k image is malfo
3e429 72 6d 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51  rmed",.    /* SQ
3e42a 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 20 20  LITE_NOTFOUND   
3e42b 20 2a 2f 20 22 75 6e 6b 6e 6f 77 6e 20 6f 70 65   */ "unknown ope
3e42c 72 61 74 69 6f 6e 22 2c 0a 20 20 20 20 2f 2a 20  ration",.    /* 
3e42d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 20 20 20 20  SQLITE_FULL     
3e42e 20 20 20 2a 2f 20 22 64 61 74 61 62 61 73 65 20     */ "database 
3e42f 6f 72 20 64 69 73 6b 20 69 73 20 66 75 6c 6c 22  or disk is full"
3e430 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
3e431 43 41 4e 54 4f 50 45 4e 20 20 20 20 2a 2f 20 22  CANTOPEN    */ "
3e432 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 64  unable to open d
3e433 61 74 61 62 61 73 65 20 66 69 6c 65 22 2c 0a 20  atabase file",. 
3e434 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 50 52 4f     /* SQLITE_PRO
3e435 54 4f 43 4f 4c 20 20 20 20 2a 2f 20 22 6c 6f 63  TOCOL    */ "loc
3e436 6b 69 6e 67 20 70 72 6f 74 6f 63 6f 6c 22 2c 0a  king protocol",.
3e437 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4d      /* SQLITE_EM
3e438 50 54 59 20 20 20 20 20 20 20 2a 2f 20 22 74 61  PTY       */ "ta
3e439 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20  ble contains no 
3e43a 64 61 74 61 22 2c 0a 20 20 20 20 2f 2a 20 53 51  data",.    /* SQ
3e43b 4c 49 54 45 5f 53 43 48 45 4d 41 20 20 20 20 20  LITE_SCHEMA     
3e43c 20 2a 2f 20 22 64 61 74 61 62 61 73 65 20 73 63   */ "database sc
3e43d 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64  hema has changed
3e43e 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ",.    /* SQLITE
3e43f 5f 54 4f 4f 42 49 47 20 20 20 20 20 20 2a 2f 20  _TOOBIG      */ 
3e440 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20  "string or blob 
3e441 74 6f 6f 20 62 69 67 22 2c 0a 20 20 20 20 2f 2a  too big",.    /*
3e442 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
3e443 4e 54 20 20 2a 2f 20 22 63 6f 6e 73 74 72 61 69  NT  */ "constrai
3e444 6e 74 20 66 61 69 6c 65 64 22 2c 0a 20 20 20 20  nt failed",.    
3e445 2f 2a 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54  /* SQLITE_MISMAT
3e446 43 48 20 20 20 20 2a 2f 20 22 64 61 74 61 74 79  CH    */ "dataty
3e447 70 65 20 6d 69 73 6d 61 74 63 68 22 2c 0a 20 20  pe mismatch",.  
3e448 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55    /* SQLITE_MISU
3e449 53 45 20 20 20 20 20 20 2a 2f 20 22 6c 69 62 72  SE      */ "libr
3e44a 61 72 79 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c  ary routine call
3e44b 65 64 20 6f 75 74 20 6f 66 20 73 65 71 75 65 6e  ed out of sequen
3e44c 63 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  ce",.    /* SQLI
3e44d 54 45 5f 4e 4f 4c 46 53 20 20 20 20 20 20 20 2a  TE_NOLFS       *
3e44e 2f 20 22 6c 61 72 67 65 20 66 69 6c 65 20 73 75  / "large file su
3e44f 70 70 6f 72 74 20 69 73 20 64 69 73 61 62 6c 65  pport is disable
3e450 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  d",.    /* SQLIT
3e451 45 5f 41 55 54 48 20 20 20 20 20 20 20 20 2a 2f  E_AUTH        */
3e452 20 22 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20   "authorization 
3e453 64 65 6e 69 65 64 22 2c 0a 20 20 20 20 2f 2a 20  denied",.    /* 
3e454 53 51 4c 49 54 45 5f 46 4f 52 4d 41 54 20 20 20  SQLITE_FORMAT   
3e455 20 20 20 2a 2f 20 22 61 75 78 69 6c 69 61 72 79     */ "auxiliary
3e456 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74   database format
3e457 20 65 72 72 6f 72 22 2c 0a 20 20 20 20 2f 2a 20   error",.    /* 
3e458 53 51 4c 49 54 45 5f 52 41 4e 47 45 20 20 20 20  SQLITE_RANGE    
3e459 20 20 20 2a 2f 20 22 62 69 6e 64 20 6f 72 20 63     */ "bind or c
3e45a 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 6f 75 74 20  olumn index out 
3e45b 6f 66 20 72 61 6e 67 65 22 2c 0a 20 20 20 20 2f  of range",.    /
3e45c 2a 20 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20  * SQLITE_NOTADB 
3e45d 20 20 20 20 20 2a 2f 20 22 66 69 6c 65 20 69 73       */ "file is
3e45e 20 65 6e 63 72 79 70 74 65 64 20 6f 72 20 69 73   encrypted or is
3e45f 20 6e 6f 74 20 61 20 64 61 74 61 62 61 73 65 22   not a database"
3e460 2c 0a 20 20 7d 3b 0a 20 20 63 6f 6e 73 74 20 63  ,.  };.  const c
3e461 68 61 72 20 2a 7a 45 72 72 20 3d 20 22 75 6e 6b  har *zErr = "unk
3e462 6e 6f 77 6e 20 65 72 72 6f 72 22 3b 0a 20 20 73  nown error";.  s
3e463 77 69 74 63 68 28 20 72 63 20 29 7b 0a 20 20 20  witch( rc ){.   
3e464 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 42 4f   case SQLITE_ABO
3e465 52 54 5f 52 4f 4c 4c 42 41 43 4b 3a 20 7b 0a 20  RT_ROLLBACK: {. 
3e466 20 20 20 20 20 7a 45 72 72 20 3d 20 22 61 62 6f       zErr = "abo
3e467 72 74 20 64 75 65 20 74 6f 20 52 4f 4c 4c 42 41  rt due to ROLLBA
3e468 43 4b 22 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  CK";.      break
3e469 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
3e46a 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 63 20  ult: {.      rc 
3e46b 26 3d 20 30 78 66 66 3b 0a 20 20 20 20 20 20 69  &= 0xff;.      i
3e46c 66 28 20 41 4c 57 41 59 53 28 72 63 3e 3d 30 29  f( ALWAYS(rc>=0)
3e46d 20 26 26 20 72 63 3c 41 72 72 61 79 53 69 7a 65   && rc<ArraySize
3e46e 28 61 4d 73 67 29 20 26 26 20 61 4d 73 67 5b 72  (aMsg) && aMsg[r
3e46f 63 5d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  c]!=0 ){.       
3e470 20 7a 45 72 72 20 3d 20 61 4d 73 67 5b 72 63 5d   zErr = aMsg[rc]
3e471 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3e472 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
3e473 0a 20 20 72 65 74 75 72 6e 20 7a 45 72 72 3b 0a  .  return zErr;.
3e474 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
3e475 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73  utine implements
3e476 20 61 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b   a busy callback
3e477 20 74 68 61 74 20 73 6c 65 65 70 73 20 61 6e 64   that sleeps and
3e478 20 74 72 69 65 73 0a 2a 2a 20 61 67 61 69 6e 20   tries.** again 
3e479 75 6e 74 69 6c 20 61 20 74 69 6d 65 6f 75 74 20  until a timeout 
3e47a 76 61 6c 75 65 20 69 73 20 72 65 61 63 68 65 64  value is reached
3e47b 2e 20 20 54 68 65 20 74 69 6d 65 6f 75 74 20 76  .  The timeout v
3e47c 61 6c 75 65 20 69 73 0a 2a 2a 20 61 6e 20 69 6e  alue is.** an in
3e47d 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20  teger number of 
3e47e 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 70 61 73  milliseconds pas
3e47f 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 66 69  sed in as the fi
3e480 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e  rst.** argument.
3e481 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
3e482 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75 73 79  qliteDefaultBusy
3e483 43 61 6c 6c 62 61 63 6b 28 0a 20 76 6f 69 64 20  Callback(. void 
3e484 2a 70 74 72 2c 20 20 20 20 20 20 20 20 20 20 20  *ptr,           
3e485 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
3e486 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 69  connection */. i
3e487 6e 74 20 63 6f 75 6e 74 20 20 20 20 20 20 20 20  nt count        
3e488 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
3e489 72 20 6f 66 20 74 69 6d 65 73 20 74 61 62 6c 65  r of times table
3e48a 20 68 61 73 20 62 65 65 6e 20 62 75 73 79 20 2a   has been busy *
3e48b 2f 0a 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  /.){.#if SQLITE_
3e48c 4f 53 5f 57 49 4e 20 7c 7c 20 28 64 65 66 69 6e  OS_WIN || (defin
3e48d 65 64 28 48 41 56 45 5f 55 53 4c 45 45 50 29 20  ed(HAVE_USLEEP) 
3e48e 26 26 20 48 41 56 45 5f 55 53 4c 45 45 50 29 0a  && HAVE_USLEEP).
3e48f 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
3e490 38 20 64 65 6c 61 79 73 5b 5d 20 3d 0a 20 20 20  8 delays[] =.   
3e491 20 20 7b 20 31 2c 20 32 2c 20 35 2c 20 31 30 2c    { 1, 2, 5, 10,
3e492 20 31 35 2c 20 32 30 2c 20 32 35 2c 20 32 35 2c   15, 20, 25, 25,
3e493 20 20 32 35 2c 20 20 35 30 2c 20 20 35 30 2c 20    25,  50,  50, 
3e494 31 30 30 20 7d 3b 0a 20 20 73 74 61 74 69 63 20  100 };.  static 
3e495 63 6f 6e 73 74 20 75 38 20 74 6f 74 61 6c 73 5b  const u8 totals[
3e496 5d 20 3d 0a 20 20 20 20 20 7b 20 30 2c 20 31 2c  ] =.     { 0, 1,
3e497 20 33 2c 20 20 38 2c 20 31 38 2c 20 33 33 2c 20   3,  8, 18, 33, 
3e498 35 33 2c 20 37 38 2c 20 31 30 33 2c 20 31 32 38  53, 78, 103, 128
3e499 2c 20 31 37 38 2c 20 32 32 38 20 7d 3b 0a 23 20  , 178, 228 };.# 
3e49a 64 65 66 69 6e 65 20 4e 44 45 4c 41 59 20 41 72  define NDELAY Ar
3e49b 72 61 79 53 69 7a 65 28 64 65 6c 61 79 73 29 0a  raySize(delays).
3e49c 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
3e49d 28 73 71 6c 69 74 65 33 20 2a 29 70 74 72 3b 0a  (sqlite3 *)ptr;.
3e49e 20 20 69 6e 74 20 74 69 6d 65 6f 75 74 20 3d 20    int timeout = 
3e49f 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74 3b  db->busyTimeout;
3e4a0 0a 20 20 69 6e 74 20 64 65 6c 61 79 2c 20 70 72  .  int delay, pr
3e4a1 69 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ior;..  assert( 
3e4a2 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 20 69 66  count>=0 );.  if
3e4a3 28 20 63 6f 75 6e 74 20 3c 20 4e 44 45 4c 41 59  ( count < NDELAY
3e4a4 20 29 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d 20   ){.    delay = 
3e4a5 64 65 6c 61 79 73 5b 63 6f 75 6e 74 5d 3b 0a 20  delays[count];. 
3e4a6 20 20 20 70 72 69 6f 72 20 3d 20 74 6f 74 61 6c     prior = total
3e4a7 73 5b 63 6f 75 6e 74 5d 3b 0a 20 20 7d 65 6c 73  s[count];.  }els
3e4a8 65 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d 20 64  e{.    delay = d
3e4a9 65 6c 61 79 73 5b 4e 44 45 4c 41 59 2d 31 5d 3b  elays[NDELAY-1];
3e4aa 0a 20 20 20 20 70 72 69 6f 72 20 3d 20 74 6f 74  .    prior = tot
3e4ab 61 6c 73 5b 4e 44 45 4c 41 59 2d 31 5d 20 2b 20  als[NDELAY-1] + 
3e4ac 64 65 6c 61 79 2a 28 63 6f 75 6e 74 2d 28 4e 44  delay*(count-(ND
3e4ad 45 4c 41 59 2d 31 29 29 3b 0a 20 20 7d 0a 20 20  ELAY-1));.  }.  
3e4ae 69 66 28 20 70 72 69 6f 72 20 2b 20 64 65 6c 61  if( prior + dela
3e4af 79 20 3e 20 74 69 6d 65 6f 75 74 20 29 7b 0a 20  y > timeout ){. 
3e4b0 20 20 20 64 65 6c 61 79 20 3d 20 74 69 6d 65 6f     delay = timeo
3e4b1 75 74 20 2d 20 70 72 69 6f 72 3b 0a 20 20 20 20  ut - prior;.    
3e4b2 69 66 28 20 64 65 6c 61 79 3c 3d 30 20 29 20 72  if( delay<=0 ) r
3e4b3 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73  eturn 0;.  }.  s
3e4b4 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 64 62  qlite3OsSleep(db
3e4b5 2d 3e 70 56 66 73 2c 20 64 65 6c 61 79 2a 31 30  ->pVfs, delay*10
3e4b6 30 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  00);.  return 1;
3e4b7 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33  .#else.  sqlite3
3e4b8 20 2a 64 62 20 3d 20 28 73 71 6c 69 74 65 33 20   *db = (sqlite3 
3e4b9 2a 29 70 74 72 3b 0a 20 20 69 6e 74 20 74 69 6d  *)ptr;.  int tim
3e4ba 65 6f 75 74 20 3d 20 28 28 73 71 6c 69 74 65 33  eout = ((sqlite3
3e4bb 20 2a 29 70 74 72 29 2d 3e 62 75 73 79 54 69 6d   *)ptr)->busyTim
3e4bc 65 6f 75 74 3b 0a 20 20 69 66 28 20 28 63 6f 75  eout;.  if( (cou
3e4bd 6e 74 2b 31 29 2a 31 30 30 30 20 3e 20 74 69 6d  nt+1)*1000 > tim
3e4be 65 6f 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75  eout ){.    retu
3e4bf 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  rn 0;.  }.  sqli
3e4c0 74 65 33 4f 73 53 6c 65 65 70 28 64 62 2d 3e 70  te3OsSleep(db->p
3e4c1 56 66 73 2c 20 31 30 30 30 30 30 30 29 3b 0a 20  Vfs, 1000000);. 
3e4c2 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69   return 1;.#endi
3e4c3 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b  f.}../*.** Invok
3e4c4 65 20 74 68 65 20 67 69 76 65 6e 20 62 75 73 79  e the given busy
3e4c5 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20   handler..**.** 
3e4c6 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
3e4c7 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 6f  called when an o
3e4c8 70 65 72 61 74 69 6f 6e 20 66 61 69 6c 65 64 20  peration failed 
3e4c9 77 69 74 68 20 61 20 6c 6f 63 6b 2e 0a 2a 2a 20  with a lock..** 
3e4ca 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  If this routine 
3e4cb 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f  returns non-zero
3e4cc 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 72 65  , the lock is re
3e4cd 74 72 69 65 64 2e 20 20 49 66 20 69 74 0a 2a 2a  tried.  If it.**
3e4ce 20 72 65 74 75 72 6e 73 20 30 2c 20 74 68 65 20   returns 0, the 
3e4cf 6f 70 65 72 61 74 69 6f 6e 20 61 62 6f 72 74 73  operation aborts
3e4d0 20 77 69 74 68 20 61 6e 20 53 51 4c 49 54 45 5f   with an SQLITE_
3e4d1 42 55 53 59 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53  BUSY error..*/.S
3e4d2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
3e4d3 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42  t sqlite3InvokeB
3e4d4 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79 48  usyHandler(BusyH
3e4d5 61 6e 64 6c 65 72 20 2a 70 29 7b 0a 20 20 69 6e  andler *p){.  in
3e4d6 74 20 72 63 3b 0a 20 20 69 66 28 20 4e 45 56 45  t rc;.  if( NEVE
3e4d7 52 28 70 3d 3d 30 29 20 7c 7c 20 70 2d 3e 78 46  R(p==0) || p->xF
3e4d8 75 6e 63 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 42 75  unc==0 || p->nBu
3e4d9 73 79 3c 30 20 29 20 72 65 74 75 72 6e 20 30 3b  sy<0 ) return 0;
3e4da 0a 20 20 72 63 20 3d 20 70 2d 3e 78 46 75 6e 63  .  rc = p->xFunc
3e4db 28 70 2d 3e 70 41 72 67 2c 20 70 2d 3e 6e 42 75  (p->pArg, p->nBu
3e4dc 73 79 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30  sy);.  if( rc==0
3e4dd 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 42 75 73 79   ){.    p->nBusy
3e4de 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a   = -1;.  }else{.
3e4df 20 20 20 20 70 2d 3e 6e 42 75 73 79 2b 2b 3b 0a      p->nBusy++;.
3e4e0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
3e4e1 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20   .}../*.** This 
3e4e2 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65  routine sets the
3e4e3 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 66   busy callback f
3e4e4 6f 72 20 61 6e 20 53 71 6c 69 74 65 20 64 61 74  or an Sqlite dat
3e4e5 61 62 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a 20  abase to the.** 
3e4e6 67 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b 20 66  given callback f
3e4e7 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65  unction with the
3e4e8 20 67 69 76 65 6e 20 61 72 67 75 6d 65 6e 74 2e   given argument.
3e4e9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
3e4ea 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  nt sqlite3_busy_
3e4eb 68 61 6e 64 6c 65 72 28 0a 20 20 73 71 6c 69 74  handler(.  sqlit
3e4ec 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28 2a  e3 *db,.  int (*
3e4ed 78 42 75 73 79 29 28 76 6f 69 64 2a 2c 69 6e 74  xBusy)(void*,int
3e4ee 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a  ),.  void *pArg.
3e4ef 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
3e4f0 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
3e4f1 65 78 29 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48  ex);.  db->busyH
3e4f2 61 6e 64 6c 65 72 2e 78 46 75 6e 63 20 3d 20 78  andler.xFunc = x
3e4f3 42 75 73 79 3b 0a 20 20 64 62 2d 3e 62 75 73 79  Busy;.  db->busy
3e4f4 48 61 6e 64 6c 65 72 2e 70 41 72 67 20 3d 20 70  Handler.pArg = p
3e4f5 41 72 67 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48  Arg;.  db->busyH
3e4f6 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20 30  andler.nBusy = 0
3e4f7 3b 0a 20 20 64 62 2d 3e 62 75 73 79 54 69 6d 65  ;.  db->busyTime
3e4f8 6f 75 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  out = 0;.  sqlit
3e4f9 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
3e4fa 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
3e4fb 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
3e4fc 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
3e4fd 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43  _OMIT_PROGRESS_C
3e4fe 41 4c 4c 42 41 43 4b 0a 2f 2a 0a 2a 2a 20 54 68  ALLBACK./*.** Th
3e4ff 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
3e500 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
3e501 6c 62 61 63 6b 20 66 6f 72 20 61 6e 20 53 71 6c  lback for an Sql
3e502 69 74 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  ite database to 
3e503 74 68 65 0a 2a 2a 20 67 69 76 65 6e 20 63 61 6c  the.** given cal
3e504 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77  lback function w
3e505 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 61 72  ith the given ar
3e506 67 75 6d 65 6e 74 2e 20 54 68 65 20 70 72 6f 67  gument. The prog
3e507 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 77 69  ress callback wi
3e508 6c 6c 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b 65 64  ll.** be invoked
3e509 20 65 76 65 72 79 20 6e 4f 70 73 20 6f 70 63 6f   every nOps opco
3e50a 64 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  des..*/.SQLITE_A
3e50b 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
3e50c 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72  progress_handler
3e50d 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
3e50e 20 0a 20 20 69 6e 74 20 6e 4f 70 73 2c 0a 20 20   .  int nOps,.  
3e50f 69 6e 74 20 28 2a 78 50 72 6f 67 72 65 73 73 29  int (*xProgress)
3e510 28 76 6f 69 64 2a 29 2c 20 0a 20 20 76 6f 69 64  (void*), .  void
3e511 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c 69   *pArg.){.  sqli
3e512 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
3e513 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66  db->mutex);.  if
3e514 28 20 6e 4f 70 73 3e 30 20 29 7b 0a 20 20 20 20  ( nOps>0 ){.    
3e515 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 20 3d 20  db->xProgress = 
3e516 78 50 72 6f 67 72 65 73 73 3b 0a 20 20 20 20 64  xProgress;.    d
3e517 62 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73 20  b->nProgressOps 
3e518 3d 20 6e 4f 70 73 3b 0a 20 20 20 20 64 62 2d 3e  = nOps;.    db->
3e519 70 50 72 6f 67 72 65 73 73 41 72 67 20 3d 20 70  pProgressArg = p
3e51a 41 72 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Arg;.  }else{.  
3e51b 20 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 20    db->xProgress 
3e51c 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6e 50 72  = 0;.    db->nPr
3e51d 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a 20  ogressOps = 0;. 
3e51e 20 20 20 64 62 2d 3e 70 50 72 6f 67 72 65 73 73     db->pProgress
3e51f 41 72 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73  Arg = 0;.  }.  s
3e520 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
3e521 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 7d  ve(db->mutex);.}
3e522 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
3e523 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e 73  This routine ins
3e524 74 61 6c 6c 73 20 61 20 64 65 66 61 75 6c 74 20  talls a default 
3e525 62 75 73 79 20 68 61 6e 64 6c 65 72 20 74 68 61  busy handler tha
3e526 74 20 77 61 69 74 73 20 66 6f 72 20 74 68 65 0a  t waits for the.
3e527 2a 2a 20 73 70 65 63 69 66 69 65 64 20 6e 75 6d  ** specified num
3e528 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f  ber of milliseco
3e529 6e 64 73 20 62 65 66 6f 72 65 20 72 65 74 75 72  nds before retur
3e52a 6e 69 6e 67 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54  ning 0..*/.SQLIT
3e52b 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3e52c 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 73  3_busy_timeout(s
3e52d 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
3e52e 6d 73 29 7b 0a 20 20 69 66 28 20 6d 73 3e 30 20  ms){.  if( ms>0 
3e52f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62  ){.    sqlite3_b
3e530 75 73 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c 20  usy_handler(db, 
3e531 73 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75 73  sqliteDefaultBus
3e532 79 43 61 6c 6c 62 61 63 6b 2c 20 28 76 6f 69 64  yCallback, (void
3e533 2a 29 64 62 29 3b 0a 20 20 20 20 64 62 2d 3e 62  *)db);.    db->b
3e534 75 73 79 54 69 6d 65 6f 75 74 20 3d 20 6d 73 3b  usyTimeout = ms;
3e535 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
3e536 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c  lite3_busy_handl
3e537 65 72 28 64 62 2c 20 30 2c 20 30 29 3b 0a 20 20  er(db, 0, 0);.  
3e538 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
3e539 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
3e53a 61 75 73 65 20 61 6e 79 20 70 65 6e 64 69 6e 67  ause any pending
3e53b 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 73 74   operation to st
3e53c 6f 70 20 61 74 20 69 74 73 20 65 61 72 6c 69 65  op at its earlie
3e53d 73 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e 0a  st opportunity..
3e53e 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
3e53f 69 64 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  id sqlite3_inter
3e540 72 75 70 74 28 73 71 6c 69 74 65 33 20 2a 64 62  rupt(sqlite3 *db
3e541 29 7b 0a 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e  ){.  db->u1.isIn
3e542 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b 0a 7d  terrupted = 1;.}
3e543 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  .../*.** This fu
3e544 6e 63 74 69 6f 6e 20 69 73 20 65 78 61 63 74 6c  nction is exactl
3e545 79 20 74 68 65 20 73 61 6d 65 20 61 73 20 73 71  y the same as sq
3e546 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
3e547 63 74 69 6f 6e 28 29 2c 20 65 78 63 65 70 74 0a  ction(), except.
3e548 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20 64 65  ** that it is de
3e549 73 69 67 6e 65 64 20 74 6f 20 62 65 20 63 61 6c  signed to be cal
3e54a 6c 65 64 20 62 79 20 69 6e 74 65 72 6e 61 6c 20  led by internal 
3e54b 63 6f 64 65 2e 20 54 68 65 20 64 69 66 66 65 72  code. The differ
3e54c 65 6e 63 65 20 69 73 0a 2a 2a 20 74 68 61 74 20  ence is.** that 
3e54d 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  if a malloc() fa
3e54e 69 6c 73 20 69 6e 20 73 71 6c 69 74 65 33 5f 63  ils in sqlite3_c
3e54f 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29  reate_function()
3e550 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 0a  , an error code.
3e551 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  ** is returned a
3e552 6e 64 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69  nd the mallocFai
3e553 6c 65 64 20 66 6c 61 67 20 63 6c 65 61 72 65 64  led flag cleared
3e554 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  . .*/.SQLITE_PRI
3e555 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
3e556 43 72 65 61 74 65 46 75 6e 63 28 0a 20 20 73 71  CreateFunc(.  sq
3e557 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e  lite3 *db,.  con
3e558 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 69  st char *zFuncti
3e559 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41  onName,.  int nA
3e55a 72 67 2c 0a 20 20 69 6e 74 20 65 6e 63 2c 0a 20  rg,.  int enc,. 
3e55b 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61 74 61   void *pUserData
3e55c 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63  ,.  void (*xFunc
3e55d 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
3e55e 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
3e55f 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64  alue **),.  void
3e560 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65   (*xStep)(sqlite
3e561 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
3e562 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29  qlite3_value **)
3e563 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61  ,.  void (*xFina
3e564 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  l)(sqlite3_conte
3e565 78 74 2a 29 2c 0a 20 20 46 75 6e 63 44 65 73 74  xt*),.  FuncDest
3e566 72 75 63 74 6f 72 20 2a 70 44 65 73 74 72 75 63  ructor *pDestruc
3e567 74 6f 72 0a 29 7b 0a 20 20 46 75 6e 63 44 65 66  tor.){.  FuncDef
3e568 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65   *p;.  int nName
3e569 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
3e56a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
3e56b 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
3e56c 69 66 28 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d  if( zFunctionNam
3e56d 65 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20 28 78  e==0 ||.      (x
3e56e 46 75 6e 63 20 26 26 20 28 78 46 69 6e 61 6c 20  Func && (xFinal 
3e56f 7c 7c 20 78 53 74 65 70 29 29 20 7c 7c 20 0a 20  || xStep)) || . 
3e570 20 20 20 20 20 28 21 78 46 75 6e 63 20 26 26 20       (!xFunc && 
3e571 28 78 46 69 6e 61 6c 20 26 26 20 21 78 53 74 65  (xFinal && !xSte
3e572 70 29 29 20 7c 7c 0a 20 20 20 20 20 20 28 21 78  p)) ||.      (!x
3e573 46 75 6e 63 20 26 26 20 28 21 78 46 69 6e 61 6c  Func && (!xFinal
3e574 20 26 26 20 78 53 74 65 70 29 29 20 7c 7c 0a 20   && xStep)) ||. 
3e575 20 20 20 20 20 28 6e 41 72 67 3c 2d 31 20 7c 7c       (nArg<-1 ||
3e576 20 6e 41 72 67 3e 53 51 4c 49 54 45 5f 4d 41 58   nArg>SQLITE_MAX
3e577 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 29 20 7c  _FUNCTION_ARG) |
3e578 7c 0a 20 20 20 20 20 20 28 32 35 35 3c 28 6e 4e  |.      (255<(nN
3e579 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
3e57a 6c 65 6e 33 30 28 20 7a 46 75 6e 63 74 69 6f 6e  len30( zFunction
3e57b 4e 61 6d 65 29 29 29 20 29 7b 0a 20 20 20 20 72  Name))) ){.    r
3e57c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
3e57d 55 53 45 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20  USE_BKPT;.  }.  
3e57e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
3e57f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 2f 2a 20  OMIT_UTF16.  /* 
3e580 49 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20  If SQLITE_UTF16 
3e581 69 73 20 73 70 65 63 69 66 69 65 64 20 61 73 20  is specified as 
3e582 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 74 79 70  the encoding typ
3e583 65 2c 20 74 72 61 6e 73 66 6f 72 6d 20 74 68 69  e, transform thi
3e584 73 0a 20 20 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66  s.  ** to one of
3e585 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20   SQLITE_UTF16LE 
3e586 6f 72 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  or SQLITE_UTF16B
3e587 45 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a  E using the.  **
3e588 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
3e589 49 56 45 20 6d 61 63 72 6f 2e 20 53 51 4c 49 54  IVE macro. SQLIT
3e58a 45 5f 55 54 46 31 36 20 69 73 20 6e 6f 74 20 75  E_UTF16 is not u
3e58b 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a  sed internally..
3e58c 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 53 51 4c    **.  ** If SQL
3e58d 49 54 45 5f 41 4e 59 20 69 73 20 73 70 65 63 69  ITE_ANY is speci
3e58e 66 69 65 64 2c 20 61 64 64 20 74 68 72 65 65 20  fied, add three 
3e58f 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  versions of the 
3e590 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f  function.  ** to
3e591 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e   the hash table.
3e592 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 6e 63 3d  .  */.  if( enc=
3e593 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20 29 7b  =SQLITE_UTF16 ){
3e594 0a 20 20 20 20 65 6e 63 20 3d 20 53 51 4c 49 54  .    enc = SQLIT
3e595 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20  E_UTF16NATIVE;. 
3e596 20 7d 65 6c 73 65 20 69 66 28 20 65 6e 63 3d 3d   }else if( enc==
3e597 53 51 4c 49 54 45 5f 41 4e 59 20 29 7b 0a 20 20  SQLITE_ANY ){.  
3e598 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 63    int rc;.    rc
3e599 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65   = sqlite3Create
3e59a 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74 69  Func(db, zFuncti
3e59b 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51  onName, nArg, SQ
3e59c 4c 49 54 45 5f 55 54 46 38 2c 0a 20 20 20 20 20  LITE_UTF8,.     
3e59d 20 20 20 20 70 55 73 65 72 44 61 74 61 2c 20 78      pUserData, x
3e59e 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69  Func, xStep, xFi
3e59f 6e 61 6c 2c 20 70 44 65 73 74 72 75 63 74 6f 72  nal, pDestructor
3e5a0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
3e5a1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3e5a2 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72    rc = sqlite3Cr
3e5a3 65 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75  eateFunc(db, zFu
3e5a4 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67  nctionName, nArg
3e5a5 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  , SQLITE_UTF16LE
3e5a6 2c 0a 20 20 20 20 20 20 20 20 20 20 70 55 73 65  ,.          pUse
3e5a7 72 44 61 74 61 2c 20 78 46 75 6e 63 2c 20 78 53  rData, xFunc, xS
3e5a8 74 65 70 2c 20 78 46 69 6e 61 6c 2c 20 70 44 65  tep, xFinal, pDe
3e5a9 73 74 72 75 63 74 6f 72 29 3b 0a 20 20 20 20 7d  structor);.    }
3e5aa 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
3e5ab 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
3e5ac 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
3e5ad 0a 20 20 20 20 65 6e 63 20 3d 20 53 51 4c 49 54  .    enc = SQLIT
3e5ae 45 5f 55 54 46 31 36 42 45 3b 0a 20 20 7d 0a 23  E_UTF16BE;.  }.#
3e5af 65 6c 73 65 0a 20 20 65 6e 63 20 3d 20 53 51 4c  else.  enc = SQL
3e5b0 49 54 45 5f 55 54 46 38 3b 0a 23 65 6e 64 69 66  ITE_UTF8;.#endif
3e5b1 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69  .  .  /* Check i
3e5b2 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 66 75  f an existing fu
3e5b3 6e 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20  nction is being 
3e5b4 6f 76 65 72 72 69 64 64 65 6e 20 6f 72 20 64 65  overridden or de
3e5b5 6c 65 74 65 64 2e 20 49 66 20 73 6f 2c 0a 20 20  leted. If so,.  
3e5b6 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 61 72 65  ** and there are
3e5b7 20 61 63 74 69 76 65 20 56 4d 73 2c 20 74 68 65   active VMs, the
3e5b8 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  n return SQLITE_
3e5b9 42 55 53 59 2e 20 49 66 20 61 20 66 75 6e 63 74  BUSY. If a funct
3e5ba 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 62 65 69 6e  ion.  ** is bein
3e5bb 67 20 6f 76 65 72 72 69 64 64 65 6e 2f 64 65 6c  g overridden/del
3e5bc 65 74 65 64 20 62 75 74 20 74 68 65 72 65 20 61  eted but there a
3e5bd 72 65 20 6e 6f 20 61 63 74 69 76 65 20 56 4d 73  re no active VMs
3e5be 2c 20 61 6c 6c 6f 77 20 74 68 65 0a 20 20 2a 2a  , allow the.  **
3e5bf 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 63 6f   operation to co
3e5c0 6e 74 69 6e 75 65 20 62 75 74 20 69 6e 76 61 6c  ntinue but inval
3e5c1 69 64 61 74 65 20 61 6c 6c 20 70 72 65 63 6f 6d  idate all precom
3e5c2 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73  piled statements
3e5c3 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 73 71 6c  ..  */.  p = sql
3e5c4 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
3e5c5 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61  (db, zFunctionNa
3e5c6 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c  me, nName, nArg,
3e5c7 20 28 75 38 29 65 6e 63 2c 20 30 29 3b 0a 20 20   (u8)enc, 0);.  
3e5c8 69 66 28 20 70 20 26 26 20 70 2d 3e 69 50 72 65  if( p && p->iPre
3e5c9 66 45 6e 63 3d 3d 65 6e 63 20 26 26 20 70 2d 3e  fEnc==enc && p->
3e5ca 6e 41 72 67 3d 3d 6e 41 72 67 20 29 7b 0a 20 20  nArg==nArg ){.  
3e5cb 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65    if( db->active
3e5cc 56 64 62 65 43 6e 74 20 29 7b 0a 20 20 20 20 20  VdbeCnt ){.     
3e5cd 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
3e5ce 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a  , SQLITE_BUSY, .
3e5cf 20 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20          "unable 
3e5d0 74 6f 20 64 65 6c 65 74 65 2f 6d 6f 64 69 66 79  to delete/modify
3e5d1 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64   user-function d
3e5d2 75 65 20 74 6f 20 61 63 74 69 76 65 20 73 74 61  ue to active sta
3e5d3 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 20  tements");.     
3e5d4 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61   assert( !db->ma
3e5d5 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
3e5d6 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3e5d7 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73  E_BUSY;.    }els
3e5d8 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
3e5d9 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74  ExpirePreparedSt
3e5da 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20  atements(db);.  
3e5db 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 73    }.  }..  p = s
3e5dc 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
3e5dd 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e  on(db, zFunction
3e5de 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72  Name, nName, nAr
3e5df 67 2c 20 28 75 38 29 65 6e 63 2c 20 31 29 3b 0a  g, (u8)enc, 1);.
3e5e0 20 20 61 73 73 65 72 74 28 70 20 7c 7c 20 64 62    assert(p || db
3e5e1 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b  ->mallocFailed);
3e5e2 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20  .  if( !p ){.   
3e5e3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
3e5e4 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  OMEM;.  }..  /* 
3e5e5 49 66 20 61 6e 20 6f 6c 64 65 72 20 76 65 72 73  If an older vers
3e5e6 69 6f 6e 20 6f 66 20 74 68 65 20 66 75 6e 63 74  ion of the funct
3e5e7 69 6f 6e 20 77 69 74 68 20 61 20 63 6f 6e 66 69  ion with a confi
3e5e8 67 75 72 65 64 20 64 65 73 74 72 75 63 74 6f 72  gured destructor
3e5e9 20 69 73 0a 20 20 2a 2a 20 62 65 69 6e 67 20 72   is.  ** being r
3e5ea 65 70 6c 61 63 65 64 20 69 6e 76 6f 6b 65 20 74  eplaced invoke t
3e5eb 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75  he destructor fu
3e5ec 6e 63 74 69 6f 6e 20 68 65 72 65 2e 20 2a 2f 0a  nction here. */.
3e5ed 20 20 66 75 6e 63 74 69 6f 6e 44 65 73 74 72 6f    functionDestro
3e5ee 79 28 64 62 2c 20 70 29 3b 0a 0a 20 20 69 66 28  y(db, p);..  if(
3e5ef 20 70 44 65 73 74 72 75 63 74 6f 72 20 29 7b 0a   pDestructor ){.
3e5f0 20 20 20 20 70 44 65 73 74 72 75 63 74 6f 72 2d      pDestructor-
3e5f1 3e 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 70  >nRef++;.  }.  p
3e5f2 2d 3e 70 44 65 73 74 72 75 63 74 6f 72 20 3d 20  ->pDestructor = 
3e5f3 70 44 65 73 74 72 75 63 74 6f 72 3b 0a 20 20 70  pDestructor;.  p
3e5f4 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 70  ->flags = 0;.  p
3e5f5 2d 3e 78 46 75 6e 63 20 3d 20 78 46 75 6e 63 3b  ->xFunc = xFunc;
3e5f6 0a 20 20 70 2d 3e 78 53 74 65 70 20 3d 20 78 53  .  p->xStep = xS
3e5f7 74 65 70 3b 0a 20 20 70 2d 3e 78 46 69 6e 61 6c  tep;.  p->xFinal
3e5f8 69 7a 65 20 3d 20 78 46 69 6e 61 6c 3b 0a 20 20  ize = xFinal;.  
3e5f9 70 2d 3e 70 55 73 65 72 44 61 74 61 20 3d 20 70  p->pUserData = p
3e5fa 55 73 65 72 44 61 74 61 3b 0a 20 20 70 2d 3e 6e  UserData;.  p->n
3e5fb 41 72 67 20 3d 20 28 75 31 36 29 6e 41 72 67 3b  Arg = (u16)nArg;
3e5fc 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3e5fd 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  _OK;.}../*.** Cr
3e5fe 65 61 74 65 20 6e 65 77 20 75 73 65 72 20 66 75  eate new user fu
3e5ff 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49  nctions..*/.SQLI
3e600 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3e601 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
3e602 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  on(.  sqlite3 *d
3e603 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b,.  const char 
3e604 2a 7a 46 75 6e 63 2c 0a 20 20 69 6e 74 20 6e 41  *zFunc,.  int nA
3e605 72 67 2c 0a 20 20 69 6e 74 20 65 6e 63 2c 0a 20  rg,.  int enc,. 
3e606 20 76 6f 69 64 20 2a 70 2c 0a 20 20 76 6f 69 64   void *p,.  void
3e607 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65   (*xFunc)(sqlite
3e608 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
3e609 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29  qlite3_value **)
3e60a 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70  ,.  void (*xStep
3e60b 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
3e60c 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
3e60d 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64  alue **),.  void
3e60e 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74   (*xFinal)(sqlit
3e60f 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a  e3_context*).){.
3e610 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
3e611 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
3e612 5f 76 32 28 64 62 2c 20 7a 46 75 6e 63 2c 20 6e  _v2(db, zFunc, n
3e613 41 72 67 2c 20 65 6e 63 2c 20 70 2c 20 78 46 75  Arg, enc, p, xFu
3e614 6e 63 2c 20 78 53 74 65 70 2c 0a 20 20 20 20 20  nc, xStep,.     
3e615 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e616 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 78                 x
3e617 46 69 6e 61 6c 2c 20 30 29 3b 0a 7d 0a 0a 53 51  Final, 0);.}..SQ
3e618 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
3e619 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
3e61a 74 69 6f 6e 5f 76 32 28 0a 20 20 73 71 6c 69 74  tion_v2(.  sqlit
3e61b 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20  e3 *db,.  const 
3e61c 63 68 61 72 20 2a 7a 46 75 6e 63 2c 0a 20 20 69  char *zFunc,.  i
3e61d 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65  nt nArg,.  int e
3e61e 6e 63 2c 0a 20 20 76 6f 69 64 20 2a 70 2c 0a 20  nc,.  void *p,. 
3e61f 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73   void (*xFunc)(s
3e620 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
3e621 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
3e622 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  e **),.  void (*
3e623 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63  xStep)(sqlite3_c
3e624 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
3e625 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20  te3_value **),. 
3e626 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28   void (*xFinal)(
3e627 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
3e628 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73  ),.  void (*xDes
3e629 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 0a 29 7b  troy)(void *).){
3e62a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
3e62b 54 45 5f 45 52 52 4f 52 3b 0a 20 20 46 75 6e 63  TE_ERROR;.  Func
3e62c 44 65 73 74 72 75 63 74 6f 72 20 2a 70 41 72 67  Destructor *pArg
3e62d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
3e62e 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
3e62f 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 78 44  mutex);.  if( xD
3e630 65 73 74 72 6f 79 20 29 7b 0a 20 20 20 20 70 41  estroy ){.    pA
3e631 72 67 20 3d 20 28 46 75 6e 63 44 65 73 74 72 75  rg = (FuncDestru
3e632 63 74 6f 72 20 2a 29 73 71 6c 69 74 65 33 44 62  ctor *)sqlite3Db
3e633 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
3e634 69 7a 65 6f 66 28 46 75 6e 63 44 65 73 74 72 75  izeof(FuncDestru
3e635 63 74 6f 72 29 29 3b 0a 20 20 20 20 69 66 28 20  ctor));.    if( 
3e636 21 70 41 72 67 20 29 7b 0a 20 20 20 20 20 20 78  !pArg ){.      x
3e637 44 65 73 74 72 6f 79 28 70 29 3b 0a 20 20 20 20  Destroy(p);.    
3e638 20 20 67 6f 74 6f 20 6f 75 74 3b 0a 20 20 20 20    goto out;.    
3e639 7d 0a 20 20 20 20 70 41 72 67 2d 3e 78 44 65 73  }.    pArg->xDes
3e63a 74 72 6f 79 20 3d 20 78 44 65 73 74 72 6f 79 3b  troy = xDestroy;
3e63b 0a 20 20 20 20 70 41 72 67 2d 3e 70 55 73 65 72  .    pArg->pUser
3e63c 44 61 74 61 20 3d 20 70 3b 0a 20 20 7d 0a 20 20  Data = p;.  }.  
3e63d 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61  rc = sqlite3Crea
3e63e 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63  teFunc(db, zFunc
3e63f 2c 20 6e 41 72 67 2c 20 65 6e 63 2c 20 70 2c 20  , nArg, enc, p, 
3e640 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46  xFunc, xStep, xF
3e641 69 6e 61 6c 2c 20 70 41 72 67 29 3b 0a 20 20 69  inal, pArg);.  i
3e642 66 28 20 70 41 72 67 20 26 26 20 70 41 72 67 2d  f( pArg && pArg-
3e643 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
3e644 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
3e645 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 78 44 65  TE_OK );.    xDe
3e646 73 74 72 6f 79 28 70 29 3b 0a 20 20 20 20 73 71  stroy(p);.    sq
3e647 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
3e648 70 41 72 67 29 3b 0a 20 20 7d 0a 0a 20 6f 75 74  pArg);.  }.. out
3e649 3a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  :.  rc = sqlite3
3e64a 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
3e64b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3e64c 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
3e64d 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
3e64e 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
3e64f 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
3e650 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3e651 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
3e652 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33  ion16(.  sqlite3
3e653 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 76 6f   *db,.  const vo
3e654 69 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d  id *zFunctionNam
3e655 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20  e,.  int nArg,. 
3e656 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 0a 20   int eTextRep,. 
3e657 20 76 6f 69 64 20 2a 70 2c 0a 20 20 76 6f 69 64   void *p,.  void
3e658 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65   (*xFunc)(sqlite
3e659 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
3e65a 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c  qlite3_value**),
3e65b 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29  .  void (*xStep)
3e65c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
3e65d 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
3e65e 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28  lue**),.  void (
3e65f 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33  *xFinal)(sqlite3
3e660 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20  _context*).){.  
3e661 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72 20 2a  int rc;.  char *
3e662 7a 46 75 6e 63 38 3b 0a 20 20 73 71 6c 69 74 65  zFunc8;.  sqlite
3e663 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
3e664 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65  ->mutex);.  asse
3e665 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rt( !db->mallocF
3e666 61 69 6c 65 64 20 29 3b 0a 20 20 7a 46 75 6e 63  ailed );.  zFunc
3e667 38 20 3d 20 73 71 6c 69 74 65 33 55 74 66 31 36  8 = sqlite3Utf16
3e668 74 6f 38 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f  to8(db, zFunctio
3e669 6e 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54  nName, -1, SQLIT
3e66a 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a  E_UTF16NATIVE);.
3e66b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72    rc = sqlite3Cr
3e66c 65 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75  eateFunc(db, zFu
3e66d 6e 63 38 2c 20 6e 41 72 67 2c 20 65 54 65 78 74  nc8, nArg, eText
3e66e 52 65 70 2c 20 70 2c 20 78 46 75 6e 63 2c 20 78  Rep, p, xFunc, x
3e66f 53 74 65 70 2c 20 78 46 69 6e 61 6c 2c 30 29 3b  Step, xFinal,0);
3e670 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
3e671 28 64 62 2c 20 7a 46 75 6e 63 38 29 3b 0a 20 20  (db, zFunc8);.  
3e672 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
3e673 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73  xit(db, rc);.  s
3e674 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
3e675 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
3e676 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
3e677 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 63  ndif.../*.** Dec
3e678 6c 61 72 65 20 74 68 61 74 20 61 20 66 75 6e 63  lare that a func
3e679 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 6f 76  tion has been ov
3e67a 65 72 6c 6f 61 64 65 64 20 62 79 20 61 20 76 69  erloaded by a vi
3e67b 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a  rtual table..**.
3e67c 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69  ** If the functi
3e67d 6f 6e 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  on already exist
3e67e 73 20 61 73 20 61 20 72 65 67 75 6c 61 72 20 67  s as a regular g
3e67f 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 2c 20  lobal function, 
3e680 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75  then.** this rou
3e681 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
3e682 20 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f    If the functio
3e683 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  n does not exist
3e684 2c 20 74 68 65 6e 20 63 72 65 61 74 65 0a 2a 2a  , then create.**
3e685 20 61 20 6e 65 77 20 6f 6e 65 20 74 68 61 74 20   a new one that 
3e686 61 6c 77 61 79 73 20 74 68 72 6f 77 73 20 61 20  always throws a 
3e687 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 2e 20  run-time error. 
3e688 20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 76 69 72   .**.** When vir
3e689 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 74 65  tual tables inte
3e68a 6e 64 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e  nd to provide an
3e68b 20 6f 76 65 72 6c 6f 61 64 65 64 20 66 75 6e 63   overloaded func
3e68c 74 69 6f 6e 2c 20 74 68 65 79 0a 2a 2a 20 73 68  tion, they.** sh
3e68d 6f 75 6c 64 20 63 61 6c 6c 20 74 68 69 73 20 72  ould call this r
3e68e 6f 75 74 69 6e 65 20 74 6f 20 6d 61 6b 65 20 73  outine to make s
3e68f 75 72 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 66  ure the global f
3e690 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 2e 0a  unction exists..
3e691 2a 2a 20 41 20 67 6c 6f 62 61 6c 20 66 75 6e 63  ** A global func
3e692 74 69 6f 6e 20 6d 75 73 74 20 65 78 69 73 74 20  tion must exist 
3e693 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 6e 61 6d  in order for nam
3e694 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 74 6f 20  e resolution to 
3e695 77 6f 72 6b 0a 2a 2a 20 70 72 6f 70 65 72 6c 79  work.** properly
3e696 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3e697 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 72  int sqlite3_over
3e698 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 0a 20  load_function(. 
3e699 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20   sqlite3 *db,.  
3e69a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
3e69b 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 0a 29 7b  e,.  int nArg.){
3e69c 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73  .  int nName = s
3e69d 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
3e69e 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63 20  Name);.  int rc 
3e69f 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73  = SQLITE_OK;.  s
3e6a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
3e6a1 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
3e6a2 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64   if( sqlite3Find
3e6a3 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e 61  Function(db, zNa
3e6a4 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c  me, nName, nArg,
3e6a5 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 29   SQLITE_UTF8, 0)
3e6a6 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
3e6a7 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
3e6a8 63 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 41 72  c(db, zName, nAr
3e6a9 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 0a  g, SQLITE_UTF8,.
3e6aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e6ab 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 73 71             0, sq
3e6ac 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63  lite3InvalidFunc
3e6ad 74 69 6f 6e 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  tion, 0, 0, 0);.
3e6ae 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
3e6af 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
3e6b0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
3e6b1 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
3e6b2 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
3e6b3 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
3e6b4 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 2f  ITE_OMIT_TRACE./
3e6b5 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
3e6b6 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 2e 20  trace function. 
3e6b7 20 54 68 65 20 70 41 72 67 20 66 72 6f 6d 20 74   The pArg from t
3e6b8 68 65 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  he previously re
3e6b9 67 69 73 74 65 72 65 64 20 74 72 61 63 65 0a 2a  gistered trace.*
3e6ba 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  * is returned.  
3e6bb 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 74 72  .**.** A NULL tr
3e6bc 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61  ace function mea
3e6bd 6e 73 20 74 68 61 74 20 6e 6f 20 74 72 61 63 69  ns that no traci
3e6be 6e 67 20 69 73 20 65 78 65 63 75 74 65 73 2e 20  ng is executes. 
3e6bf 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 74   A non-NULL.** t
3e6c0 72 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65  race is a pointe
3e6c1 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20  r to a function 
3e6c2 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20  that is invoked 
3e6c3 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
3e6c4 65 61 63 68 0a 2a 2a 20 53 51 4c 20 73 74 61 74  each.** SQL stat
3e6c5 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ement..*/.SQLITE
3e6c6 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
3e6c7 65 33 5f 74 72 61 63 65 28 73 71 6c 69 74 65 33  e3_trace(sqlite3
3e6c8 20 2a 64 62 2c 20 76 6f 69 64 20 28 2a 78 54 72   *db, void (*xTr
3e6c9 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  ace)(void*,const
3e6ca 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 20 2a 70   char*), void *p
3e6cb 41 72 67 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f  Arg){.  void *pO
3e6cc 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ld;.  sqlite3_mu
3e6cd 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
3e6ce 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64  tex);.  pOld = d
3e6cf 62 2d 3e 70 54 72 61 63 65 41 72 67 3b 0a 20 20  b->pTraceArg;.  
3e6d0 64 62 2d 3e 78 54 72 61 63 65 20 3d 20 78 54 72  db->xTrace = xTr
3e6d1 61 63 65 3b 0a 20 20 64 62 2d 3e 70 54 72 61 63  ace;.  db->pTrac
3e6d2 65 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73  eArg = pArg;.  s
3e6d3 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
3e6d4 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
3e6d5 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a   return pOld;.}.
3e6d6 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
3e6d7 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f   profile functio
3e6d8 6e 2e 20 20 54 68 65 20 70 41 72 67 20 66 72 6f  n.  The pArg fro
3e6d9 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79  m the previously
3e6da 20 72 65 67 69 73 74 65 72 65 64 20 0a 2a 2a 20   registered .** 
3e6db 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e  profile function
3e6dc 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 0a   is returned.  .
3e6dd 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 70 72 6f  **.** A NULL pro
3e6de 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65  file function me
3e6df 61 6e 73 20 74 68 61 74 20 6e 6f 20 70 72 6f 66  ans that no prof
3e6e0 69 6c 69 6e 67 20 69 73 20 65 78 65 63 75 74 65  iling is execute
3e6e1 73 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a  s.  A non-NULL.*
3e6e2 2a 20 70 72 6f 66 69 6c 65 20 69 73 20 61 20 70  * profile is a p
3e6e3 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63  ointer to a func
3e6e4 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 76  tion that is inv
3e6e5 6f 6b 65 64 20 61 74 20 74 68 65 20 63 6f 6e 63  oked at the conc
3e6e6 6c 75 73 69 6f 6e 20 6f 66 0a 2a 2a 20 65 61 63  lusion of.** eac
3e6e7 68 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  h SQL statement 
3e6e8 74 68 61 74 20 69 73 20 72 75 6e 2e 0a 2a 2f 0a  that is run..*/.
3e6e9 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
3e6ea 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65  *sqlite3_profile
3e6eb 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
3e6ec 0a 20 20 76 6f 69 64 20 28 2a 78 50 72 6f 66 69  .  void (*xProfi
3e6ed 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20  le)(void*,const 
3e6ee 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 75 69 6e  char*,sqlite_uin
3e6ef 74 36 34 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41  t64),.  void *pA
3e6f0 72 67 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f  rg.){.  void *pO
3e6f1 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ld;.  sqlite3_mu
3e6f2 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
3e6f3 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64  tex);.  pOld = d
3e6f4 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 3b 0a  b->pProfileArg;.
3e6f5 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 3d    db->xProfile =
3e6f6 20 78 50 72 6f 66 69 6c 65 3b 0a 20 20 64 62 2d   xProfile;.  db-
3e6f7 3e 70 50 72 6f 66 69 6c 65 41 72 67 20 3d 20 70  >pProfileArg = p
3e6f8 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  Arg;.  sqlite3_m
3e6f9 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
3e6fa 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
3e6fb 70 4f 6c 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  pOld;.}.#endif /
3e6fc 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  * SQLITE_OMIT_TR
3e6fd 41 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  ACE */../*.** Re
3e6fe 67 69 73 74 65 72 20 61 20 66 75 6e 63 74 69 6f  gister a functio
3e6ff 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20  n to be invoked 
3e700 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69  when a transacti
3e701 6f 6e 20 63 6f 6d 6d 69 74 73 2e 0a 2a 2a 20 49  on commits..** I
3e702 66 20 74 68 65 20 69 6e 76 6f 6b 65 64 20 66 75  f the invoked fu
3e703 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 6e  nction returns n
3e704 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  on-zero, then th
3e705 65 20 63 6f 6d 6d 69 74 20 62 65 63 6f 6d 65 73  e commit becomes
3e706 20 61 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2e 0a   a.** rollback..
3e707 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
3e708 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6d 6d  id *sqlite3_comm
3e709 69 74 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74  it_hook(.  sqlit
3e70a 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
3e70b 20 20 20 20 20 2f 2a 20 41 74 74 61 63 68 20 74       /* Attach t
3e70c 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68 69 73 20  he hook to this 
3e70d 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e  database */.  in
3e70e 74 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76  t (*xCallback)(v
3e70f 6f 69 64 2a 29 2c 20 20 2f 2a 20 46 75 6e 63 74  oid*),  /* Funct
3e710 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 6f 6e  ion to invoke on
3e711 20 65 61 63 68 20 63 6f 6d 6d 69 74 20 2a 2f 0a   each commit */.
3e712 20 20 76 6f 69 64 20 2a 70 41 72 67 20 20 20 20    void *pArg    
3e713 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
3e714 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66  rgument to the f
3e715 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20  unction */.){.  
3e716 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a 20 20 73 71  void *pOld;.  sq
3e717 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
3e718 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
3e719 70 4f 6c 64 20 3d 20 64 62 2d 3e 70 43 6f 6d 6d  pOld = db->pComm
3e71a 69 74 41 72 67 3b 0a 20 20 64 62 2d 3e 78 43 6f  itArg;.  db->xCo
3e71b 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20 3d 20 78  mmitCallback = x
3e71c 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 64 62 2d 3e  Callback;.  db->
3e71d 70 43 6f 6d 6d 69 74 41 72 67 20 3d 20 70 41 72  pCommitArg = pAr
3e71e 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  g;.  sqlite3_mut
3e71f 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
3e720 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4f  ex);.  return pO
3e721 6c 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67  ld;.}../*.** Reg
3e722 69 73 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b  ister a callback
3e723 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 65   to be invoked e
3e724 61 63 68 20 74 69 6d 65 20 61 20 72 6f 77 20 69  ach time a row i
3e725 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a 20 69 6e  s updated,.** in
3e726 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74 65  serted or delete
3e727 64 20 75 73 69 6e 67 20 74 68 69 73 20 64 61 74  d using this dat
3e728 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3e729 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3e72a 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 70  void *sqlite3_up
3e72b 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c  date_hook(.  sql
3e72c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
3e72d 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63 68         /* Attach
3e72e 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68 69   the hook to thi
3e72f 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  s database */.  
3e730 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b  void (*xCallback
3e731 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 68 61 72  )(void*,int,char
3e732 20 63 6f 6e 73 74 20 2a 2c 63 68 61 72 20 63 6f   const *,char co
3e733 6e 73 74 20 2a 2c 73 71 6c 69 74 65 5f 69 6e 74  nst *,sqlite_int
3e734 36 34 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72  64),.  void *pAr
3e735 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g               
3e736 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20   /* Argument to 
3e737 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
3e738 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 3b  ){.  void *pRet;
3e739 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3e73a 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
3e73b 29 3b 0a 20 20 70 52 65 74 20 3d 20 64 62 2d 3e  );.  pRet = db->
3e73c 70 55 70 64 61 74 65 41 72 67 3b 0a 20 20 64 62  pUpdateArg;.  db
3e73d 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63  ->xUpdateCallbac
3e73e 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20  k = xCallback;. 
3e73f 20 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67 20   db->pUpdateArg 
3e740 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65  = pArg;.  sqlite
3e741 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
3e742 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
3e743 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pRet;.}../*.*
3e744 2a 20 52 65 67 69 73 74 65 72 20 61 20 63 61 6c  * Register a cal
3e745 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f  lback to be invo
3e746 6b 65 64 20 65 61 63 68 20 74 69 6d 65 20 61 20  ked each time a 
3e747 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72  transaction is r
3e748 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 62 79  olled.** back by
3e749 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 63   this database c
3e74a 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  onnection..*/.SQ
3e74b 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
3e74c 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f  qlite3_rollback_
3e74d 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 20  hook(.  sqlite3 
3e74e 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
3e74f 20 20 2f 2a 20 41 74 74 61 63 68 20 74 68 65 20    /* Attach the 
3e750 68 6f 6f 6b 20 74 6f 20 74 68 69 73 20 64 61 74  hook to this dat
3e751 61 62 61 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20  abase */.  void 
3e752 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69  (*xCallback)(voi
3e753 64 2a 29 2c 20 2f 2a 20 43 61 6c 6c 62 61 63 6b  d*), /* Callback
3e754 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76   function */.  v
3e755 6f 69 64 20 2a 70 41 72 67 20 20 20 20 20 20 20  oid *pArg       
3e756 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
3e757 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63  ment to the func
3e758 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69  tion */.){.  voi
3e759 64 20 2a 70 52 65 74 3b 0a 20 20 73 71 6c 69 74  d *pRet;.  sqlit
3e75a 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
3e75b 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 52 65  b->mutex);.  pRe
3e75c 74 20 3d 20 64 62 2d 3e 70 52 6f 6c 6c 62 61 63  t = db->pRollbac
3e75d 6b 41 72 67 3b 0a 20 20 64 62 2d 3e 78 52 6f 6c  kArg;.  db->xRol
3e75e 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 20 3d 20  lbackCallback = 
3e75f 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 64 62 2d  xCallback;.  db-
3e760 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67 20 3d 20  >pRollbackArg = 
3e761 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pArg;.  sqlite3_
3e762 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
3e763 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
3e764 20 70 52 65 74 3b 0a 7d 0a 0a 23 69 66 6e 64 65   pRet;.}..#ifnde
3e765 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 41  f SQLITE_OMIT_WA
3e766 4c 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  L./*.** The sqli
3e767 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28 29 20 63  te3_wal_hook() c
3e768 61 6c 6c 62 61 63 6b 20 72 65 67 69 73 74 65 72  allback register
3e769 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 77 61  ed by sqlite3_wa
3e76a 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69 6e 74  l_autocheckpoint
3e76b 28 29 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20 73 71  ()..** Invoke sq
3e76c 6c 69 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70  lite3_wal_checkp
3e76d 6f 69 6e 74 20 69 66 20 74 68 65 20 6e 75 6d 62  oint if the numb
3e76e 65 72 20 6f 66 20 66 72 61 6d 65 73 20 69 6e 20  er of frames in 
3e76f 74 68 65 20 6c 6f 67 20 66 69 6c 65 0a 2a 2a 20  the log file.** 
3e770 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
3e771 73 71 6c 69 74 65 33 2e 70 57 61 6c 41 72 67 20  sqlite3.pWalArg 
3e772 63 61 73 74 20 74 6f 20 61 6e 20 69 6e 74 65 67  cast to an integ
3e773 65 72 20 28 74 68 65 20 76 61 6c 75 65 20 63 6f  er (the value co
3e774 6e 66 69 67 75 72 65 64 20 62 79 0a 2a 2a 20 77  nfigured by.** w
3e775 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69 6e  al_autocheckpoin
3e776 74 28 29 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45  t())..*/ .SQLITE
3e777 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
3e778 69 74 65 33 57 61 6c 44 65 66 61 75 6c 74 48 6f  ite3WalDefaultHo
3e779 6f 6b 28 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69  ok(.  void *pCli
3e77a 65 6e 74 44 61 74 61 2c 20 20 20 20 20 2f 2a 20  entData,     /* 
3e77b 41 72 67 75 6d 65 6e 74 20 2a 2f 0a 20 20 73 71  Argument */.  sq
3e77c 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
3e77d 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69       /* Connecti
3e77e 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  on */.  const ch
3e77f 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 20 20 2f  ar *zDb,       /
3e780 2a 20 44 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  * Database */.  
3e781 69 6e 74 20 6e 46 72 61 6d 65 20 20 20 20 20 20  int nFrame      
3e782 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
3e783 66 20 57 41 4c 20 2a 2f 0a 29 7b 0a 20 20 69 66  f WAL */.){.  if
3e784 28 20 6e 46 72 61 6d 65 3e 3d 53 51 4c 49 54 45  ( nFrame>=SQLITE
3e785 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 43 6c 69  _PTR_TO_INT(pCli
3e786 65 6e 74 44 61 74 61 29 20 29 7b 0a 20 20 20 20  entData) ){.    
3e787 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
3e788 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
3e789 73 71 6c 69 74 65 33 5f 77 61 6c 5f 63 68 65 63  sqlite3_wal_chec
3e78a 6b 70 6f 69 6e 74 28 64 62 2c 20 7a 44 62 29 3b  kpoint(db, zDb);
3e78b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42  .    sqlite3EndB
3e78c 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
3e78d 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
3e78e 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20  TE_OK;.}.#endif 
3e78f 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57  /* SQLITE_OMIT_W
3e790 41 4c 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  AL */../*.** Con
3e791 66 69 67 75 72 65 20 61 6e 20 73 71 6c 69 74 65  figure an sqlite
3e792 33 5f 77 61 6c 5f 68 6f 6f 6b 28 29 20 63 61 6c  3_wal_hook() cal
3e793 6c 62 61 63 6b 20 74 6f 20 61 75 74 6f 6d 61 74  lback to automat
3e794 69 63 61 6c 6c 79 20 63 68 65 63 6b 70 6f 69 6e  ically checkpoin
3e795 74 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20  t.** a database 
3e796 61 66 74 65 72 20 63 6f 6d 6d 69 74 74 69 6e 67  after committing
3e797 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
3e798 66 20 74 68 65 72 65 20 61 72 65 20 6e 46 72 61  f there are nFra
3e799 6d 65 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 66 72  me or.** more fr
3e79a 61 6d 65 73 20 69 6e 20 74 68 65 20 6c 6f 67 20  ames in the log 
3e79b 66 69 6c 65 2e 20 50 61 73 73 69 6e 67 20 7a 65  file. Passing ze
3e79c 72 6f 20 6f 72 20 61 20 6e 65 67 61 74 69 76 65  ro or a negative
3e79d 20 76 61 6c 75 65 20 61 73 20 74 68 65 0a 2a 2a   value as the.**
3e79e 20 6e 46 72 61 6d 65 20 70 61 72 61 6d 65 74 65   nFrame paramete
3e79f 72 20 64 69 73 61 62 6c 65 73 20 61 75 74 6f 6d  r disables autom
3e7a0 61 74 69 63 20 63 68 65 63 6b 70 6f 69 6e 74 73  atic checkpoints
3e7a1 20 65 6e 74 69 72 65 6c 79 2e 0a 2a 2a 0a 2a 2a   entirely..**.**
3e7a2 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65   The callback re
3e7a3 67 69 73 74 65 72 65 64 20 62 79 20 74 68 69 73  gistered by this
3e7a4 20 66 75 6e 63 74 69 6f 6e 20 72 65 70 6c 61 63   function replac
3e7a5 65 73 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20  es any existing 
3e7a6 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 72 65 67 69  callback.** regi
3e7a7 73 74 65 72 65 64 20 75 73 69 6e 67 20 73 71 6c  stered using sql
3e7a8 69 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28 29 2e  ite3_wal_hook().
3e7a9 20 4c 69 6b 65 77 69 73 65 2c 20 72 65 67 69 73   Likewise, regis
3e7aa 74 65 72 69 6e 67 20 61 20 63 61 6c 6c 62 61 63  tering a callbac
3e7ab 6b 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74  k.** using sqlit
3e7ac 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28 29 20 64 69  e3_wal_hook() di
3e7ad 73 61 62 6c 65 73 20 74 68 65 20 61 75 74 6f 6d  sables the autom
3e7ae 61 74 69 63 20 63 68 65 63 6b 70 6f 69 6e 74 20  atic checkpoint 
3e7af 6d 65 63 68 61 6e 69 73 6d 0a 2a 2a 20 63 6f 6e  mechanism.** con
3e7b0 66 69 67 75 72 65 64 20 62 79 20 74 68 69 73 20  figured by this 
3e7b1 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  function..*/.SQL
3e7b2 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3e7b3 74 65 33 5f 77 61 6c 5f 61 75 74 6f 63 68 65 63  te3_wal_autochec
3e7b4 6b 70 6f 69 6e 74 28 73 71 6c 69 74 65 33 20 2a  kpoint(sqlite3 *
3e7b5 64 62 2c 20 69 6e 74 20 6e 46 72 61 6d 65 29 7b  db, int nFrame){
3e7b6 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
3e7b7 4d 49 54 5f 57 41 4c 0a 20 20 55 4e 55 53 45 44  MIT_WAL.  UNUSED
3e7b8 5f 50 41 52 41 4d 45 54 45 52 28 64 62 29 3b 0a  _PARAMETER(db);.
3e7b9 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
3e7ba 45 52 28 6e 46 72 61 6d 65 29 3b 0a 23 65 6c 73  ER(nFrame);.#els
3e7bb 65 0a 20 20 69 66 28 20 6e 46 72 61 6d 65 3e 30  e.  if( nFrame>0
3e7bc 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
3e7bd 77 61 6c 5f 68 6f 6f 6b 28 64 62 2c 20 73 71 6c  wal_hook(db, sql
3e7be 69 74 65 33 57 61 6c 44 65 66 61 75 6c 74 48 6f  ite3WalDefaultHo
3e7bf 6f 6b 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54  ok, SQLITE_INT_T
3e7c0 4f 5f 50 54 52 28 6e 46 72 61 6d 65 29 29 3b 0a  O_PTR(nFrame));.
3e7c1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
3e7c2 69 74 65 33 5f 77 61 6c 5f 68 6f 6f 6b 28 64 62  ite3_wal_hook(db
3e7c3 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6e  , 0, 0);.  }.#en
3e7c4 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  dif.  return SQL
3e7c5 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
3e7c6 20 52 65 67 69 73 74 65 72 20 61 20 63 61 6c 6c   Register a call
3e7c7 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f 6b  back to be invok
3e7c8 65 64 20 65 61 63 68 20 74 69 6d 65 20 61 20 74  ed each time a t
3e7c9 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 77 72  ransaction is wr
3e7ca 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68  itten.** into th
3e7cb 65 20 77 72 69 74 65 2d 61 68 65 61 64 2d 6c 6f  e write-ahead-lo
3e7cc 67 20 62 79 20 74 68 69 73 20 64 61 74 61 62 61  g by this databa
3e7cd 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
3e7ce 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
3e7cf 64 20 2a 73 71 6c 69 74 65 33 5f 77 61 6c 5f 68  d *sqlite3_wal_h
3e7d0 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ook(.  sqlite3 *
3e7d1 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
3e7d2 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63 68         /* Attach
3e7d3 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68 69   the hook to thi
3e7d4 73 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  s db handle */. 
3e7d5 20 69 6e 74 28 2a 78 43 61 6c 6c 62 61 63 6b 29   int(*xCallback)
3e7d6 28 76 6f 69 64 20 2a 2c 20 73 71 6c 69 74 65 33  (void *, sqlite3
3e7d7 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
3e7d8 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41  int),.  void *pA
3e7d9 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rg              
3e7da 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
3e7db 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64   argument passed
3e7dc 20 74 6f 20 78 43 61 6c 6c 62 61 63 6b 28 29 20   to xCallback() 
3e7dd 2a 2f 0a 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  */.){.#ifndef SQ
3e7de 4c 49 54 45 5f 4f 4d 49 54 5f 57 41 4c 0a 20 20  LITE_OMIT_WAL.  
3e7df 76 6f 69 64 20 2a 70 52 65 74 3b 0a 20 20 73 71  void *pRet;.  sq
3e7e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
3e7e1 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
3e7e2 70 52 65 74 20 3d 20 64 62 2d 3e 70 57 61 6c 41  pRet = db->pWalA
3e7e3 72 67 3b 0a 20 20 64 62 2d 3e 78 57 61 6c 43 61  rg;.  db->xWalCa
3e7e4 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61  llback = xCallba
3e7e5 63 6b 3b 0a 20 20 64 62 2d 3e 70 57 61 6c 41 72  ck;.  db->pWalAr
3e7e6 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69  g = pArg;.  sqli
3e7e7 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
3e7e8 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
3e7e9 74 75 72 6e 20 70 52 65 74 3b 0a 23 65 6c 73 65  turn pRet;.#else
3e7ea 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e  .  return 0;.#en
3e7eb 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  dif.}../*.** Che
3e7ec 63 6b 70 6f 69 6e 74 20 64 61 74 61 62 61 73 65  ckpoint database
3e7ed 20 7a 44 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   zDb..*/.SQLITE_
3e7ee 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
3e7ef 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 5f 76  wal_checkpoint_v
3e7f0 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  2(.  sqlite3 *db
3e7f1 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3e7f2 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
3e7f3 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e   handle */.  con
3e7f4 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 20 20  st char *zDb,   
3e7f5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3e7f6 4e 61 6d 65 20 6f 66 20 61 74 74 61 63 68 65 64  Name of attached
3e7f7 20 64 61 74 61 62 61 73 65 20 28 6f 72 20 4e 55   database (or NU
3e7f8 4c 4c 29 20 2a 2f 0a 20 20 69 6e 74 20 65 4d 6f  LL) */.  int eMo
3e7f9 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  de,             
3e7fa 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49           /* SQLI
3e7fb 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 2a 20  TE_CHECKPOINT_* 
3e7fc 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a  value */.  int *
3e7fd 70 6e 4c 6f 67 2c 20 20 20 20 20 20 20 20 20 20  pnLog,          
3e7fe 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
3e7ff 54 3a 20 53 69 7a 65 20 6f 66 20 57 41 4c 20 6c  T: Size of WAL l
3e800 6f 67 20 69 6e 20 66 72 61 6d 65 73 20 2a 2f 0a  og in frames */.
3e801 20 20 69 6e 74 20 2a 70 6e 43 6b 70 74 20 20 20    int *pnCkpt   
3e802 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e803 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20    /* OUT: Total 
3e804 6e 75 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73  number of frames
3e805 20 63 68 65 63 6b 70 6f 69 6e 74 65 64 20 2a 2f   checkpointed */
3e806 0a 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .){.#ifdef SQLIT
3e807 45 5f 4f 4d 49 54 5f 57 41 4c 0a 20 20 72 65 74  E_OMIT_WAL.  ret
3e808 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23  urn SQLITE_OK;.#
3e809 65 6c 73 65 0a 20 20 69 6e 74 20 72 63 3b 20 20  else.  int rc;  
3e80a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e80b 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
3e80c 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69   code */.  int i
3e80d 44 62 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f  Db = SQLITE_MAX_
3e80e 41 54 54 41 43 48 45 44 3b 20 20 2f 2a 20 73 71  ATTACHED;  /* sq
3e80f 6c 69 74 65 33 2e 61 44 62 5b 5d 20 69 6e 64 65  lite3.aDb[] inde
3e810 78 20 6f 66 20 64 62 20 74 6f 20 63 68 65 63 6b  x of db to check
3e811 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 49  point */..  /* I
3e812 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 75  nitialize the ou
3e813 74 70 75 74 20 76 61 72 69 61 62 6c 65 73 20 74  tput variables t
3e814 6f 20 2d 31 20 69 6e 20 63 61 73 65 20 61 6e 20  o -1 in case an 
3e815 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 2a 2f  error occurs. */
3e816 0a 20 20 69 66 28 20 70 6e 4c 6f 67 20 29 20 2a  .  if( pnLog ) *
3e817 70 6e 4c 6f 67 20 3d 20 2d 31 3b 0a 20 20 69 66  pnLog = -1;.  if
3e818 28 20 70 6e 43 6b 70 74 20 29 20 2a 70 6e 43 6b  ( pnCkpt ) *pnCk
3e819 70 74 20 3d 20 2d 31 3b 0a 0a 20 20 61 73 73 65  pt = -1;..  asse
3e81a 72 74 28 20 53 51 4c 49 54 45 5f 43 48 45 43 4b  rt( SQLITE_CHECK
3e81b 50 4f 49 4e 54 5f 46 55 4c 4c 3e 53 51 4c 49 54  POINT_FULL>SQLIT
3e81c 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 50 41 53  E_CHECKPOINT_PAS
3e81d 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74  SIVE );.  assert
3e81e 28 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f  ( SQLITE_CHECKPO
3e81f 49 4e 54 5f 46 55 4c 4c 3c 53 51 4c 49 54 45 5f  INT_FULL<SQLITE_
3e820 43 48 45 43 4b 50 4f 49 4e 54 5f 52 45 53 54 41  CHECKPOINT_RESTA
3e821 52 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  RT );.  assert( 
3e822 53 51 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e  SQLITE_CHECKPOIN
3e823 54 5f 50 41 53 53 49 56 45 2b 32 3d 3d 53 51 4c  T_PASSIVE+2==SQL
3e824 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f 52  ITE_CHECKPOINT_R
3e825 45 53 54 41 52 54 20 29 3b 0a 20 20 69 66 28 20  ESTART );.  if( 
3e826 65 4d 6f 64 65 3c 53 51 4c 49 54 45 5f 43 48 45  eMode<SQLITE_CHE
3e827 43 4b 50 4f 49 4e 54 5f 50 41 53 53 49 56 45 20  CKPOINT_PASSIVE 
3e828 7c 7c 20 65 4d 6f 64 65 3e 53 51 4c 49 54 45 5f  || eMode>SQLITE_
3e829 43 48 45 43 4b 50 4f 49 4e 54 5f 52 45 53 54 41  CHECKPOINT_RESTA
3e82a 52 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  RT ){.    return
3e82b 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
3e82c 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d    }..  sqlite3_m
3e82d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
3e82e 75 74 65 78 29 3b 0a 20 20 69 66 28 20 7a 44 62  utex);.  if( zDb
3e82f 20 26 26 20 7a 44 62 5b 30 5d 20 29 7b 0a 20 20   && zDb[0] ){.  
3e830 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 46    iDb = sqlite3F
3e831 69 6e 64 44 62 4e 61 6d 65 28 64 62 2c 20 7a 44  indDbName(db, zD
3e832 62 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 44  b);.  }.  if( iD
3e833 62 3c 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  b<0 ){.    rc = 
3e834 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
3e835 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
3e836 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c  b, SQLITE_ERROR,
3e837 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61   "unknown databa
3e838 73 65 3a 20 25 73 22 2c 20 7a 44 62 29 3b 0a 20  se: %s", zDb);. 
3e839 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
3e83a 20 73 71 6c 69 74 65 33 43 68 65 63 6b 70 6f 69   sqlite3Checkpoi
3e83b 6e 74 28 64 62 2c 20 69 44 62 2c 20 65 4d 6f 64  nt(db, iDb, eMod
3e83c 65 2c 20 70 6e 4c 6f 67 2c 20 70 6e 43 6b 70 74  e, pnLog, pnCkpt
3e83d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  );.    sqlite3Er
3e83e 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a  ror(db, rc, 0);.
3e83f 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
3e840 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
3e841 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
3e842 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
3e843 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
3e844 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a  ;.#endif.}.../*.
3e845 2a 2a 20 43 68 65 63 6b 70 6f 69 6e 74 20 64 61  ** Checkpoint da
3e846 74 61 62 61 73 65 20 7a 44 62 2e 20 49 66 20 7a  tabase zDb. If z
3e847 44 62 20 69 73 20 4e 55 4c 4c 2c 20 6f 72 20 69  Db is NULL, or i
3e848 66 20 74 68 65 20 62 75 66 66 65 72 20 7a 44 62  f the buffer zDb
3e849 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 63 6f   points.** to co
3e84a 6e 74 61 69 6e 73 20 61 20 7a 65 72 6f 2d 6c 65  ntains a zero-le
3e84b 6e 67 74 68 20 73 74 72 69 6e 67 2c 20 61 6c 6c  ngth string, all
3e84c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
3e84d 73 65 73 20 61 72 65 20 0a 2a 2a 20 63 68 65 63  ses are .** chec
3e84e 6b 70 6f 69 6e 74 65 64 2e 0a 2a 2f 0a 53 51 4c  kpointed..*/.SQL
3e84f 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3e850 74 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69  te3_wal_checkpoi
3e851 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  nt(sqlite3 *db, 
3e852 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 29  const char *zDb)
3e853 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
3e854 65 33 5f 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e  e3_wal_checkpoin
3e855 74 5f 76 32 28 64 62 2c 20 7a 44 62 2c 20 53 51  t_v2(db, zDb, SQ
3e856 4c 49 54 45 5f 43 48 45 43 4b 50 4f 49 4e 54 5f  LITE_CHECKPOINT_
3e857 50 41 53 53 49 56 45 2c 20 30 2c 20 30 29 3b 0a  PASSIVE, 0, 0);.
3e858 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
3e859 45 5f 4f 4d 49 54 5f 57 41 4c 0a 2f 2a 0a 2a 2a  E_OMIT_WAL./*.**
3e85a 20 52 75 6e 20 61 20 63 68 65 63 6b 70 6f 69 6e   Run a checkpoin
3e85b 74 20 6f 6e 20 64 61 74 61 62 61 73 65 20 69 44  t on database iD
3e85c 62 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d  b. This is a no-
3e85d 6f 70 20 69 66 20 64 61 74 61 62 61 73 65 20 69  op if database i
3e85e 44 62 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 75 72  Db is.** not cur
3e85f 72 65 6e 74 6c 79 20 6f 70 65 6e 20 69 6e 20 57  rently open in W
3e860 41 4c 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49  AL mode..**.** I
3e861 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
3e862 69 73 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 64  is open on the d
3e863 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 63 68  atabase being ch
3e864 65 63 6b 70 6f 69 6e 74 65 64 2c 20 74 68 69 73  eckpointed, this
3e865 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65   .** function re
3e866 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4c 4f 43  turns SQLITE_LOC
3e867 4b 45 44 20 61 6e 64 20 61 20 63 68 65 63 6b 70  KED and a checkp
3e868 6f 69 6e 74 20 69 73 20 6e 6f 74 20 61 74 74 65  oint is not atte
3e869 6d 70 74 65 64 2e 20 49 66 20 0a 2a 2a 20 61 6e  mpted. If .** an
3e86a 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68   error occurs wh
3e86b 69 6c 65 20 72 75 6e 6e 69 6e 67 20 74 68 65 20  ile running the 
3e86c 63 68 65 63 6b 70 6f 69 6e 74 2c 20 61 6e 20 53  checkpoint, an S
3e86d 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
3e86e 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64   is .** returned
3e86f 20 28 69 2e 65 2e 20 53 51 4c 49 54 45 5f 49 4f   (i.e. SQLITE_IO
3e870 45 52 52 29 2e 20 4f 74 68 65 72 77 69 73 65 2c  ERR). Otherwise,
3e871 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a   SQLITE_OK..**.*
3e872 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 6e 20 64  * The mutex on d
3e873 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64  atabase handle d
3e874 62 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64  b should be held
3e875 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   by the caller. 
3e876 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 61 73 73  The mutex.** ass
3e877 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
3e878 20 73 70 65 63 69 66 69 63 20 62 2d 74 72 65 65   specific b-tree
3e879 20 62 65 69 6e 67 20 63 68 65 63 6b 70 6f 69 6e   being checkpoin
3e87a 74 65 64 20 69 73 20 74 61 6b 65 6e 20 62 79 0a  ted is taken by.
3e87b 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** this function
3e87c 20 77 68 69 6c 65 20 74 68 65 20 63 68 65 63 6b   while the check
3e87d 70 6f 69 6e 74 20 69 73 20 72 75 6e 6e 69 6e 67  point is running
3e87e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 44 62 20 69  ..**.** If iDb i
3e87f 73 20 70 61 73 73 65 64 20 53 51 4c 49 54 45 5f  s passed SQLITE_
3e880 4d 41 58 5f 41 54 54 41 43 48 45 44 2c 20 74 68  MAX_ATTACHED, th
3e881 65 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20  en all attached 
3e882 64 61 74 61 62 61 73 65 73 20 61 72 65 0a 2a 2a  databases are.**
3e883 20 63 68 65 63 6b 70 6f 69 6e 74 65 64 2e 20 49   checkpointed. I
3e884 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 65 6e  f an error is en
3e885 63 6f 75 6e 74 65 72 65 64 20 69 74 20 69 73 20  countered it is 
3e886 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61  returned immedia
3e887 74 65 6c 79 20 2d 0a 2a 2a 20 6e 6f 20 61 74 74  tely -.** no att
3e888 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20  empt is made to 
3e889 63 68 65 63 6b 70 6f 69 6e 74 20 61 6e 79 20 72  checkpoint any r
3e88a 65 6d 61 69 6e 69 6e 67 20 64 61 74 61 62 61 73  emaining databas
3e88b 65 73 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65  es..**.** Parame
3e88c 74 65 72 20 65 4d 6f 64 65 20 69 73 20 6f 6e 65  ter eMode is one
3e88d 20 6f 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b   of SQLITE_CHECK
3e88e 50 4f 49 4e 54 5f 50 41 53 53 49 56 45 2c 20 46  POINT_PASSIVE, F
3e88f 55 4c 4c 20 6f 72 20 52 45 53 54 41 52 54 2e 0a  ULL or RESTART..
3e890 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
3e891 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65  E int sqlite3Che
3e892 63 6b 70 6f 69 6e 74 28 73 71 6c 69 74 65 33 20  ckpoint(sqlite3 
3e893 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 69 6e  *db, int iDb, in
3e894 74 20 65 4d 6f 64 65 2c 20 69 6e 74 20 2a 70 6e  t eMode, int *pn
3e895 4c 6f 67 2c 20 69 6e 74 20 2a 70 6e 43 6b 70 74  Log, int *pnCkpt
3e896 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
3e897 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
3e898 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
3e899 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  ode */.  int i; 
3e89a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e89b 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
3e89c 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f   to iterate thro
3e89d 75 67 68 20 61 74 74 61 63 68 65 64 20 64 62 73  ugh attached dbs
3e89e 20 2a 2f 0a 20 20 69 6e 74 20 62 42 75 73 79 20   */.  int bBusy 
3e89f 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
3e8a0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
3e8a1 20 53 51 4c 49 54 45 5f 42 55 53 59 20 68 61 73   SQLITE_BUSY has
3e8a2 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72 65   been encountere
3e8a3 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  d */..  assert( 
3e8a4 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
3e8a5 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
3e8a6 0a 20 20 61 73 73 65 72 74 28 20 21 70 6e 4c 6f  .  assert( !pnLo
3e8a7 67 20 7c 7c 20 2a 70 6e 4c 6f 67 3d 3d 2d 31 20  g || *pnLog==-1 
3e8a8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 6e  );.  assert( !pn
3e8a9 43 6b 70 74 20 7c 7c 20 2a 70 6e 43 6b 70 74 3d  Ckpt || *pnCkpt=
3e8aa 3d 2d 31 20 29 3b 0a 0a 20 20 66 6f 72 28 69 3d  =-1 );..  for(i=
3e8ab 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 20 26 26 20  0; i<db->nDb && 
3e8ac 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69  rc==SQLITE_OK; i
3e8ad 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3d 3d  ++){.    if( i==
3e8ae 69 44 62 20 7c 7c 20 69 44 62 3d 3d 53 51 4c 49  iDb || iDb==SQLI
3e8af 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 20  TE_MAX_ATTACHED 
3e8b0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
3e8b1 6c 69 74 65 33 42 74 72 65 65 43 68 65 63 6b 70  lite3BtreeCheckp
3e8b2 6f 69 6e 74 28 64 62 2d 3e 61 44 62 5b 69 5d 2e  oint(db->aDb[i].
3e8b3 70 42 74 2c 20 65 4d 6f 64 65 2c 20 70 6e 4c 6f  pBt, eMode, pnLo
3e8b4 67 2c 20 70 6e 43 6b 70 74 29 3b 0a 20 20 20 20  g, pnCkpt);.    
3e8b5 20 20 70 6e 4c 6f 67 20 3d 20 30 3b 0a 20 20 20    pnLog = 0;.   
3e8b6 20 20 20 70 6e 43 6b 70 74 20 3d 20 30 3b 0a 20     pnCkpt = 0;. 
3e8b7 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
3e8b8 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20  ITE_BUSY ){.    
3e8b9 20 20 20 20 62 42 75 73 79 20 3d 20 31 3b 0a 20      bBusy = 1;. 
3e8ba 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
3e8bb 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20  TE_OK;.      }. 
3e8bc 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
3e8bd 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rn (rc==SQLITE_O
3e8be 4b 20 26 26 20 62 42 75 73 79 29 20 3f 20 53 51  K && bBusy) ? SQ
3e8bf 4c 49 54 45 5f 42 55 53 59 20 3a 20 72 63 3b 0a  LITE_BUSY : rc;.
3e8c0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
3e8c1 54 45 5f 4f 4d 49 54 5f 57 41 4c 20 2a 2f 0a 0a  TE_OMIT_WAL */..
3e8c2 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
3e8c3 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 72 75 65  ion returns true
3e8c4 20 69 66 20 6d 61 69 6e 2d 6d 65 6d 6f 72 79 20   if main-memory 
3e8c5 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69  should be used i
3e8c6 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 61 20 74  nstead of.** a t
3e8c7 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 66 6f  emporary file fo
3e8c8 72 20 74 72 61 6e 73 69 65 6e 74 20 70 61 67 65  r transient page
3e8c9 72 20 66 69 6c 65 73 20 61 6e 64 20 73 74 61 74  r files and stat
3e8ca 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 2e 0a  ement journals..
3e8cb 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74  ** The value ret
3e8cc 75 72 6e 65 64 20 64 65 70 65 6e 64 73 20 6f 6e  urned depends on
3e8cd 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 64 62   the value of db
3e8ce 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20 28 72 75  ->temp_store (ru
3e8cf 6e 74 69 6d 65 0a 2a 2a 20 70 61 72 61 6d 65 74  ntime.** paramet
3e8d0 65 72 29 20 61 6e 64 20 74 68 65 20 63 6f 6d 70  er) and the comp
3e8d1 69 6c 65 20 74 69 6d 65 20 76 61 6c 75 65 20 6f  ile time value o
3e8d2 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54  f SQLITE_TEMP_ST
3e8d3 4f 52 45 2e 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c  ORE. The.** foll
3e8d4 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 73 63  owing table desc
3e8d5 72 69 62 65 73 20 74 68 65 20 72 65 6c 61 74 69  ribes the relati
3e8d6 6f 6e 73 68 69 70 20 62 65 74 77 65 65 6e 20 74  onship between t
3e8d7 68 65 73 65 20 74 77 6f 20 76 61 6c 75 65 73 0a  hese two values.
3e8d8 2a 2a 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63  ** and this func
3e8d9 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 76 61 6c  tions return val
3e8da 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49  ue..**.**   SQLI
3e8db 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 20 20  TE_TEMP_STORE   
3e8dc 20 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65    db->temp_store
3e8dd 20 20 20 20 20 4c 6f 63 61 74 69 6f 6e 20 6f 66       Location of
3e8de 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62   temporary datab
3e8df 61 73 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  ase.**   -------
3e8e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d  ----------     -
3e8e1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20  -------------   
3e8e2 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
3e8e3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3e8e4 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20 20  .**   0         
3e8e5 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 20              any 
3e8e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
3e8e7 69 6c 65 20 20 20 20 20 20 28 72 65 74 75 72 6e  ile      (return
3e8e8 20 30 29 0a 2a 2a 20 20 20 31 20 20 20 20 20 20   0).**   1      
3e8e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
3e8ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e8eb 20 20 66 69 6c 65 20 20 20 20 20 20 28 72 65 74    file      (ret
3e8ec 75 72 6e 20 30 29 0a 2a 2a 20 20 20 31 20 20 20  urn 0).**   1   
3e8ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e8ee 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
3e8ef 20 20 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28       memory    (
3e8f0 72 65 74 75 72 6e 20 31 29 0a 2a 2a 20 20 20 31  return 1).**   1
3e8f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e8f2 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20       0          
3e8f3 20 20 20 20 20 20 20 20 66 69 6c 65 20 20 20 20          file    
3e8f4 20 20 28 72 65 74 75 72 6e 20 30 29 0a 2a 2a 20    (return 0).** 
3e8f5 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
3e8f6 20 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20          1       
3e8f7 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 20             file 
3e8f8 20 20 20 20 20 28 72 65 74 75 72 6e 20 30 29 0a       (return 0).
3e8f9 2a 2a 20 20 20 32 20 20 20 20 20 20 20 20 20 20  **   2          
3e8fa 20 20 20 20 20 20 20 20 20 20 20 32 20 20 20 20             2    
3e8fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65                me
3e8fc 6d 6f 72 79 20 20 20 20 28 72 65 74 75 72 6e 20  mory    (return 
3e8fd 31 29 0a 2a 2a 20 20 20 32 20 20 20 20 20 20 20  1).**   2       
3e8fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
3e8ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e900 20 6d 65 6d 6f 72 79 20 20 20 20 28 72 65 74 75   memory    (retu
3e901 72 6e 20 31 29 0a 2a 2a 20 20 20 33 20 20 20 20  rn 1).**   3    
3e902 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3e903 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20   any            
3e904 20 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28 72      memory    (r
3e905 65 74 75 72 6e 20 31 29 0a 2a 2f 0a 53 51 4c 49  eturn 1).*/.SQLI
3e906 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
3e907 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f  qlite3TempInMemo
3e908 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ry(const sqlite3
3e909 20 2a 64 62 29 7b 0a 23 69 66 20 53 51 4c 49 54   *db){.#if SQLIT
3e90a 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 0a  E_TEMP_STORE==1.
3e90b 20 20 72 65 74 75 72 6e 20 28 20 64 62 2d 3e 74    return ( db->t
3e90c 65 6d 70 5f 73 74 6f 72 65 3d 3d 32 20 29 3b 0a  emp_store==2 );.
3e90d 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
3e90e 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 32 0a  E_TEMP_STORE==2.
3e90f 20 20 72 65 74 75 72 6e 20 28 20 64 62 2d 3e 74    return ( db->t
3e910 65 6d 70 5f 73 74 6f 72 65 21 3d 31 20 29 3b 0a  emp_store!=1 );.
3e911 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
3e912 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 33 0a  E_TEMP_STORE==3.
3e913 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64    return 1;.#end
3e914 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 45  if.#if SQLITE_TE
3e915 4d 50 5f 53 54 4f 52 45 3c 31 20 7c 7c 20 53 51  MP_STORE<1 || SQ
3e916 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3e  LITE_TEMP_STORE>
3e917 33 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 23 65  3.  return 0;.#e
3e918 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ndif.}../*.** Re
3e919 74 75 72 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64  turn UTF-8 encod
3e91a 65 64 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75  ed English langu
3e91b 61 67 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20  age explanation 
3e91c 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
3e91d 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a  nt.** error..*/.
3e91e 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
3e91f 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 65   char *sqlite3_e
3e920 72 72 6d 73 67 28 73 71 6c 69 74 65 33 20 2a 64  rrmsg(sqlite3 *d
3e921 62 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  b){.  const char
3e922 20 2a 7a 3b 0a 20 20 69 66 28 20 21 64 62 20 29   *z;.  if( !db )
3e923 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
3e924 69 74 65 33 45 72 72 53 74 72 28 53 51 4c 49 54  ite3ErrStr(SQLIT
3e925 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20  E_NOMEM);.  }.  
3e926 69 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65  if( !sqlite3Safe
3e927 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
3e928 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  db) ){.    retur
3e929 6e 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  n sqlite3ErrStr(
3e92a 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b  SQLITE_MISUSE_BK
3e92b 50 54 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  PT);.  }.  sqlit
3e92c 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
3e92d 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28  b->mutex);.  if(
3e92e 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
3e92f 64 20 29 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c  d ){.    z = sql
3e930 69 74 65 33 45 72 72 53 74 72 28 53 51 4c 49 54  ite3ErrStr(SQLIT
3e931 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 7d 65 6c 73  E_NOMEM);.  }els
3e932 65 7b 0a 20 20 20 20 7a 20 3d 20 28 63 68 61 72  e{.    z = (char
3e933 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
3e934 74 65 78 74 28 64 62 2d 3e 70 45 72 72 29 3b 0a  text(db->pErr);.
3e935 20 20 20 20 61 73 73 65 72 74 28 20 21 64 62 2d      assert( !db-
3e936 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
3e937 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b  .    if( z==0 ){
3e938 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  .      z = sqlit
3e939 65 33 45 72 72 53 74 72 28 64 62 2d 3e 65 72 72  e3ErrStr(db->err
3e93a 43 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Code);.    }.  }
3e93b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3e93c 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
3e93d 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d  );.  return z;.}
3e93e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
3e93f 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a  _OMIT_UTF16./*.*
3e940 2a 20 52 65 74 75 72 6e 20 55 54 46 2d 31 36 20  * Return UTF-16 
3e941 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73 68 20  encoded English 
3e942 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61 6e 61  language explana
3e943 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74  tion of the most
3e944 20 72 65 63 65 6e 74 0a 2a 2a 20 65 72 72 6f 72   recent.** error
3e945 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3e946 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
3e947 74 65 33 5f 65 72 72 6d 73 67 31 36 28 73 71 6c  te3_errmsg16(sql
3e948 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 74 61  ite3 *db){.  sta
3e949 74 69 63 20 63 6f 6e 73 74 20 75 31 36 20 6f 75  tic const u16 ou
3e94a 74 4f 66 4d 65 6d 5b 5d 20 3d 20 7b 0a 20 20 20  tOfMem[] = {.   
3e94b 20 27 6f 27 2c 20 27 75 27 2c 20 27 74 27 2c 20   'o', 'u', 't', 
3e94c 27 20 27 2c 20 27 6f 27 2c 20 27 66 27 2c 20 27  ' ', 'o', 'f', '
3e94d 20 27 2c 20 27 6d 27 2c 20 27 65 27 2c 20 27 6d   ', 'm', 'e', 'm
3e94e 27 2c 20 27 6f 27 2c 20 27 72 27 2c 20 27 79 27  ', 'o', 'r', 'y'
3e94f 2c 20 30 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69  , 0.  };.  stati
3e950 63 20 63 6f 6e 73 74 20 75 31 36 20 6d 69 73 75  c const u16 misu
3e951 73 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 27 6c 27  se[] = {.    'l'
3e952 2c 20 27 69 27 2c 20 27 62 27 2c 20 27 72 27 2c  , 'i', 'b', 'r',
3e953 20 27 61 27 2c 20 27 72 27 2c 20 27 79 27 2c 20   'a', 'r', 'y', 
3e954 27 20 27 2c 20 0a 20 20 20 20 27 72 27 2c 20 27  ' ', .    'r', '
3e955 6f 27 2c 20 27 75 27 2c 20 27 74 27 2c 20 27 69  o', 'u', 't', 'i
3e956 27 2c 20 27 6e 27 2c 20 27 65 27 2c 20 27 20 27  ', 'n', 'e', ' '
3e957 2c 20 0a 20 20 20 20 27 63 27 2c 20 27 61 27 2c  , .    'c', 'a',
3e958 20 27 6c 27 2c 20 27 6c 27 2c 20 27 65 27 2c 20   'l', 'l', 'e', 
3e959 27 64 27 2c 20 27 20 27 2c 20 0a 20 20 20 20 27  'd', ' ', .    '
3e95a 6f 27 2c 20 27 75 27 2c 20 27 74 27 2c 20 27 20  o', 'u', 't', ' 
3e95b 27 2c 20 0a 20 20 20 20 27 6f 27 2c 20 27 66 27  ', .    'o', 'f'
3e95c 2c 20 27 20 27 2c 20 0a 20 20 20 20 27 73 27 2c  , ' ', .    's',
3e95d 20 27 65 27 2c 20 27 71 27 2c 20 27 75 27 2c 20   'e', 'q', 'u', 
3e95e 27 65 27 2c 20 27 6e 27 2c 20 27 63 27 2c 20 27  'e', 'n', 'c', '
3e95f 65 27 2c 20 30 0a 20 20 7d 3b 0a 0a 20 20 63 6f  e', 0.  };..  co
3e960 6e 73 74 20 76 6f 69 64 20 2a 7a 3b 0a 20 20 69  nst void *z;.  i
3e961 66 28 20 21 64 62 20 29 7b 0a 20 20 20 20 72 65  f( !db ){.    re
3e962 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 6f 75 74  turn (void *)out
3e963 4f 66 4d 65 6d 3b 0a 20 20 7d 0a 20 20 69 66 28  OfMem;.  }.  if(
3e964 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79 43   !sqlite3SafetyC
3e965 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29  heckSickOrOk(db)
3e966 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28   ){.    return (
3e967 76 6f 69 64 20 2a 29 6d 69 73 75 73 65 3b 0a 20  void *)misuse;. 
3e968 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
3e969 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
3e96a 65 78 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  ex);.  if( db->m
3e96b 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
3e96c 20 20 20 7a 20 3d 20 28 76 6f 69 64 20 2a 29 6f     z = (void *)o
3e96d 75 74 4f 66 4d 65 6d 3b 0a 20 20 7d 65 6c 73 65  utOfMem;.  }else
3e96e 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65  {.    z = sqlite
3e96f 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 64  3_value_text16(d
3e970 62 2d 3e 70 45 72 72 29 3b 0a 20 20 20 20 69 66  b->pErr);.    if
3e971 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( z==0 ){.      
3e972 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53  sqlite3ValueSetS
3e973 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c  tr(db->pErr, -1,
3e974 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 64   sqlite3ErrStr(d
3e975 62 2d 3e 65 72 72 43 6f 64 65 29 2c 0a 20 20 20  b->errCode),.   
3e976 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55          SQLITE_U
3e977 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  TF8, SQLITE_STAT
3e978 49 43 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73  IC);.      z = s
3e979 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
3e97a 74 31 36 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20  t16(db->pErr);. 
3e97b 20 20 20 7d 0a 20 20 20 20 2f 2a 20 41 20 6d 61     }.    /* A ma
3e97c 6c 6c 6f 63 28 29 20 6d 61 79 20 68 61 76 65 20  lloc() may have 
3e97d 66 61 69 6c 65 64 20 77 69 74 68 69 6e 20 74 68  failed within th
3e97e 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  e call to sqlite
3e97f 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29  3_value_text16()
3e980 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 2e 20 49  .    ** above. I
3e981 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61  f this is the ca
3e982 73 65 2c 20 74 68 65 6e 20 74 68 65 20 64 62 2d  se, then the db-
3e983 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c  >mallocFailed fl
3e984 61 67 20 6e 65 65 64 73 20 74 6f 0a 20 20 20 20  ag needs to.    
3e985 2a 2a 20 62 65 20 63 6c 65 61 72 65 64 20 62 65  ** be cleared be
3e986 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 20  fore returning. 
3e987 44 6f 20 74 68 69 73 20 64 69 72 65 63 74 6c 79  Do this directly
3e988 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 76 69 61  , instead of via
3e989 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 41  .    ** sqlite3A
3e98a 70 69 45 78 69 74 28 29 2c 20 74 6f 20 61 76 6f  piExit(), to avo
3e98b 69 64 20 73 65 74 74 69 6e 67 20 74 68 65 20 64  id setting the d
3e98c 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 65  atabase handle e
3e98d 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 20 20  rror message..  
3e98e 20 20 2a 2f 0a 20 20 20 20 64 62 2d 3e 6d 61 6c    */.    db->mal
3e98f 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20  locFailed = 0;. 
3e990 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
3e991 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
3e992 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b  ex);.  return z;
3e993 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
3e994 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
3e995 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  /../*.** Return 
3e996 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
3e997 65 72 72 6f 72 20 63 6f 64 65 20 67 65 6e 65 72  error code gener
3e998 61 74 65 64 20 62 79 20 61 6e 20 53 51 4c 69 74  ated by an SQLit
3e999 65 20 72 6f 75 74 69 6e 65 2e 20 49 66 20 4e 55  e routine. If NU
3e99a 4c 4c 20 69 73 0a 2a 2a 20 70 61 73 73 65 64 20  LL is.** passed 
3e99b 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
3e99c 2c 20 77 65 20 61 73 73 75 6d 65 20 61 20 6d 61  , we assume a ma
3e99d 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 64 75  lloc() failed du
3e99e 72 69 6e 67 20 73 71 6c 69 74 65 33 5f 6f 70 65  ring sqlite3_ope
3e99f 6e 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  n()..*/.SQLITE_A
3e9a0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
3e9a1 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a  rrcode(sqlite3 *
3e9a2 64 62 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26  db){.  if( db &&
3e9a3 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79 43   !sqlite3SafetyC
3e9a4 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29  heckSickOrOk(db)
3e9a5 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
3e9a6 51 4c 49 54 45 5f 4d 49 53 55 53 45 5f 42 4b 50  QLITE_MISUSE_BKP
3e9a7 54 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62  T;.  }.  if( !db
3e9a8 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
3e9a9 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75  iled ){.    retu
3e9aa 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
3e9ab 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 64 62  .  }.  return db
3e9ac 2d 3e 65 72 72 43 6f 64 65 20 26 20 64 62 2d 3e  ->errCode & db->
3e9ad 65 72 72 4d 61 73 6b 3b 0a 7d 0a 53 51 4c 49 54  errMask;.}.SQLIT
3e9ae 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3e9af 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f  3_extended_errco
3e9b0 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  de(sqlite3 *db){
3e9b1 0a 20 20 69 66 28 20 64 62 20 26 26 20 21 73 71  .  if( db && !sq
3e9b2 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
3e9b3 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a  SickOrOk(db) ){.
3e9b4 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3e9b5 45 5f 4d 49 53 55 53 45 5f 42 4b 50 54 3b 0a 20  E_MISUSE_BKPT;. 
3e9b6 20 7d 0a 20 20 69 66 28 20 21 64 62 20 7c 7c 20   }.  if( !db || 
3e9b7 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
3e9b8 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
3e9b9 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
3e9ba 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 65 72  .  return db->er
3e9bb 72 43 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rCode;.}../*.** 
3e9bc 52 65 74 75 72 6e 20 61 20 73 74 72 69 6e 67 20  Return a string 
3e9bd 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74  that describes t
3e9be 68 65 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72  he kind of error
3e9bf 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
3e9c0 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20  e.** argument.  
3e9c1 46 6f 72 20 6e 6f 77 2c 20 74 68 69 73 20 73 69  For now, this si
3e9c2 6d 70 6c 79 20 63 61 6c 6c 73 20 74 68 65 20 69  mply calls the i
3e9c3 6e 74 65 72 6e 61 6c 20 73 71 6c 69 74 65 33 45  nternal sqlite3E
3e9c4 72 72 53 74 72 28 29 0a 2a 2a 20 66 75 6e 63 74  rrStr().** funct
3e9c5 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ion..*/.SQLITE_A
3e9c6 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
3e9c7 71 6c 69 74 65 33 5f 65 72 72 73 74 72 28 69 6e  qlite3_errstr(in
3e9c8 74 20 72 63 29 7b 0a 20 20 72 65 74 75 72 6e 20  t rc){.  return 
3e9c9 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 72 63  sqlite3ErrStr(rc
3e9ca 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  );.}../*.** Crea
3e9cb 74 65 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69  te a new collati
3e9cc 6e 67 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  ng function for 
3e9cd 64 61 74 61 62 61 73 65 20 22 64 62 22 2e 20 20  database "db".  
3e9ce 54 68 65 20 6e 61 6d 65 20 69 73 20 7a 4e 61 6d  The name is zNam
3e9cf 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 65 6e 63  e.** and the enc
3e9d0 6f 64 69 6e 67 20 69 73 20 65 6e 63 2e 0a 2a 2f  oding is enc..*/
3e9d1 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72 65 61  .static int crea
3e9d2 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73  teCollation(.  s
3e9d3 71 6c 69 74 65 33 2a 20 64 62 2c 0a 20 20 63 6f  qlite3* db,.  co
3e9d4 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
3e9d5 20 0a 20 20 75 38 20 65 6e 63 2c 0a 20 20 76 6f   .  u8 enc,.  vo
3e9d6 69 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28  id* pCtx,.  int(
3e9d7 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a  *xCompare)(void*
3e9d8 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
3e9d9 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
3e9da 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44 65 6c 29  ),.  void(*xDel)
3e9db 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 43 6f 6c  (void*).){.  Col
3e9dc 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69  lSeq *pColl;.  i
3e9dd 6e 74 20 65 6e 63 32 3b 0a 20 20 69 6e 74 20 6e  nt enc2;.  int n
3e9de 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
3e9df 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20  rlen30(zName);. 
3e9e0 20 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69   .  assert( sqli
3e9e1 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
3e9e2 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  b->mutex) );..  
3e9e3 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 55 54 46  /* If SQLITE_UTF
3e9e4 31 36 20 69 73 20 73 70 65 63 69 66 69 65 64 20  16 is specified 
3e9e5 61 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  as the encoding 
3e9e6 74 79 70 65 2c 20 74 72 61 6e 73 66 6f 72 6d 20  type, transform 
3e9e7 74 68 69 73 0a 20 20 2a 2a 20 74 6f 20 6f 6e 65  this.  ** to one
3e9e8 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36   of SQLITE_UTF16
3e9e9 4c 45 20 6f 72 20 53 51 4c 49 54 45 5f 55 54 46  LE or SQLITE_UTF
3e9ea 31 36 42 45 20 75 73 69 6e 67 20 74 68 65 0a 20  16BE using the. 
3e9eb 20 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36   ** SQLITE_UTF16
3e9ec 4e 41 54 49 56 45 20 6d 61 63 72 6f 2e 20 53 51  NATIVE macro. SQ
3e9ed 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20 6e 6f  LITE_UTF16 is no
3e9ee 74 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c  t used internall
3e9ef 79 2e 0a 20 20 2a 2f 0a 20 20 65 6e 63 32 20 3d  y..  */.  enc2 =
3e9f0 20 65 6e 63 3b 0a 20 20 74 65 73 74 63 61 73 65   enc;.  testcase
3e9f1 28 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45 5f 55  ( enc2==SQLITE_U
3e9f2 54 46 31 36 20 29 3b 0a 20 20 74 65 73 74 63 61  TF16 );.  testca
3e9f3 73 65 28 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45  se( enc2==SQLITE
3e9f4 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 29  _UTF16_ALIGNED )
3e9f5 3b 0a 20 20 69 66 28 20 65 6e 63 32 3d 3d 53 51  ;.  if( enc2==SQ
3e9f6 4c 49 54 45 5f 55 54 46 31 36 20 7c 7c 20 65 6e  LITE_UTF16 || en
3e9f7 63 32 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  c2==SQLITE_UTF16
3e9f8 5f 41 4c 49 47 4e 45 44 20 29 7b 0a 20 20 20 20  _ALIGNED ){.    
3e9f9 65 6e 63 32 20 3d 20 53 51 4c 49 54 45 5f 55 54  enc2 = SQLITE_UT
3e9fa 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d 0a 20  F16NATIVE;.  }. 
3e9fb 20 69 66 28 20 65 6e 63 32 3c 53 51 4c 49 54 45   if( enc2<SQLITE
3e9fc 5f 55 54 46 38 20 7c 7c 20 65 6e 63 32 3e 53 51  _UTF8 || enc2>SQ
3e9fd 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 7b 0a  LITE_UTF16BE ){.
3e9fe 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3e9ff 45 5f 4d 49 53 55 53 45 5f 42 4b 50 54 3b 0a 20  E_MISUSE_BKPT;. 
3ea00 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69   }..  /* Check i
3ea01 66 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 72  f this call is r
3ea02 65 6d 6f 76 69 6e 67 20 6f 72 20 72 65 70 6c 61  emoving or repla
3ea03 63 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67  cing an existing
3ea04 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 20 20 2a 2a   collation .  **
3ea05 20 73 65 71 75 65 6e 63 65 2e 20 49 66 20 73 6f   sequence. If so
3ea06 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20  , and there are 
3ea07 61 63 74 69 76 65 20 56 4d 73 2c 20 72 65 74 75  active VMs, retu
3ea08 72 6e 20 62 75 73 79 2e 20 49 66 20 74 68 65 72  rn busy. If ther
3ea09 65 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 20 61 63  e.  ** are no ac
3ea0a 74 69 76 65 20 56 4d 73 2c 20 69 6e 76 61 6c 69  tive VMs, invali
3ea0b 64 61 74 65 20 61 6e 79 20 70 72 65 2d 63 6f 6d  date any pre-com
3ea0c 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73  piled statements
3ea0d 2e 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c 6c 20 3d  ..  */.  pColl =
3ea0e 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
3ea0f 53 65 71 28 64 62 2c 20 28 75 38 29 65 6e 63 32  Seq(db, (u8)enc2
3ea10 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 69  , zName, 0);.  i
3ea11 66 28 20 70 43 6f 6c 6c 20 26 26 20 70 43 6f 6c  f( pColl && pCol
3ea12 6c 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 20 20 69  l->xCmp ){.    i
3ea13 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62  f( db->activeVdb
3ea14 65 43 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71  eCnt ){.      sq
3ea15 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
3ea16 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20 20 20  QLITE_BUSY, .   
3ea17 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20       "unable to 
3ea18 64 65 6c 65 74 65 2f 6d 6f 64 69 66 79 20 63 6f  delete/modify co
3ea19 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
3ea1a 20 64 75 65 20 74 6f 20 61 63 74 69 76 65 20 73   due to active s
3ea1b 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20  tatements");.   
3ea1c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3ea1d 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 20  _BUSY;.    }.   
3ea1e 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72   sqlite3ExpirePr
3ea1f 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73  eparedStatements
3ea20 28 64 62 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  (db);..    /* If
3ea21 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
3ea22 6e 63 65 20 70 43 6f 6c 6c 20 77 61 73 20 63 72  nce pColl was cr
3ea23 65 61 74 65 64 20 64 69 72 65 63 74 6c 79 20 62  eated directly b
3ea24 79 20 61 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20  y a call to.    
3ea25 2a 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ** sqlite3_creat
3ea26 65 5f 63 6f 6c 6c 61 74 69 6f 6e 2c 20 61 6e 64  e_collation, and
3ea27 20 6e 6f 74 20 67 65 6e 65 72 61 74 65 64 20 62   not generated b
3ea28 79 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 29  y synthCollSeq()
3ea29 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e  ,.    ** then an
3ea2a 79 20 63 6f 70 69 65 73 20 6d 61 64 65 20 62 79  y copies made by
3ea2b 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 29 20   synthCollSeq() 
3ea2c 6e 65 65 64 20 74 6f 20 62 65 20 69 6e 76 61 6c  need to be inval
3ea2d 69 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a 20 41  idated..    ** A
3ea2e 6c 73 6f 2c 20 63 6f 6c 6c 61 74 69 6f 6e 20 64  lso, collation d
3ea2f 65 73 74 72 75 63 74 6f 72 20 2d 20 43 6f 6c 6c  estructor - Coll
3ea30 53 65 71 2e 78 44 65 6c 28 29 20 2d 20 66 75 6e  Seq.xDel() - fun
3ea31 63 74 69 6f 6e 20 6d 61 79 20 6e 65 65 64 0a 20  ction may need. 
3ea32 20 20 20 2a 2a 20 74 6f 20 62 65 20 63 61 6c 6c     ** to be call
3ea33 65 64 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20  ed..    */ .    
3ea34 69 66 28 20 28 70 43 6f 6c 6c 2d 3e 65 6e 63 20  if( (pColl->enc 
3ea35 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f  & ~SQLITE_UTF16_
3ea36 41 4c 49 47 4e 45 44 29 3d 3d 65 6e 63 32 20 29  ALIGNED)==enc2 )
3ea37 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20  {.      CollSeq 
3ea38 2a 61 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33  *aColl = sqlite3
3ea39 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 43  HashFind(&db->aC
3ea3a 6f 6c 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e  ollSeq, zName, n
3ea3b 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 6e 74  Name);.      int
3ea3c 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d   j;.      for(j=
3ea3d 30 3b 20 6a 3c 33 3b 20 6a 2b 2b 29 7b 0a 20 20  0; j<3; j++){.  
3ea3e 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
3ea3f 20 3d 20 26 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20   = &aColl[j];.  
3ea40 20 20 20 20 20 20 69 66 28 20 70 2d 3e 65 6e 63        if( p->enc
3ea41 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b 0a  ==pColl->enc ){.
3ea42 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 2d            if( p-
3ea43 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 20 20 20  >xDel ){.       
3ea44 20 20 20 20 20 70 2d 3e 78 44 65 6c 28 70 2d 3e       p->xDel(p->
3ea45 70 55 73 65 72 29 3b 0a 20 20 20 20 20 20 20 20  pUser);.        
3ea46 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d    }.          p-
3ea47 3e 78 43 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20  >xCmp = 0;.     
3ea48 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
3ea49 20 7d 0a 20 20 7d 0a 0a 20 20 70 43 6f 6c 6c 20   }.  }..  pColl 
3ea4a 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c  = sqlite3FindCol
3ea4b 6c 53 65 71 28 64 62 2c 20 28 75 38 29 65 6e 63  lSeq(db, (u8)enc
3ea4c 32 2c 20 7a 4e 61 6d 65 2c 20 31 29 3b 0a 20 20  2, zName, 1);.  
3ea4d 69 66 28 20 70 43 6f 6c 6c 3d 3d 30 20 29 20 72  if( pColl==0 ) r
3ea4e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
3ea4f 45 4d 3b 0a 20 20 70 43 6f 6c 6c 2d 3e 78 43 6d  EM;.  pColl->xCm
3ea50 70 20 3d 20 78 43 6f 6d 70 61 72 65 3b 0a 20 20  p = xCompare;.  
3ea51 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 20 3d 20 70  pColl->pUser = p
3ea52 43 74 78 3b 0a 20 20 70 43 6f 6c 6c 2d 3e 78 44  Ctx;.  pColl->xD
3ea53 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 70 43 6f  el = xDel;.  pCo
3ea54 6c 6c 2d 3e 65 6e 63 20 3d 20 28 75 38 29 28 65  ll->enc = (u8)(e
3ea55 6e 63 32 20 7c 20 28 65 6e 63 20 26 20 53 51 4c  nc2 | (enc & SQL
3ea56 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45  ITE_UTF16_ALIGNE
3ea57 44 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 72  D));.  sqlite3Er
3ea58 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  ror(db, SQLITE_O
3ea59 4b 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  K, 0);.  return 
3ea5a 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
3ea5b 2a 0a 2a 2a 20 54 68 69 73 20 61 72 72 61 79 20  *.** This array 
3ea5c 64 65 66 69 6e 65 73 20 68 61 72 64 20 75 70 70  defines hard upp
3ea5d 65 72 20 62 6f 75 6e 64 73 20 6f 6e 20 6c 69 6d  er bounds on lim
3ea5e 69 74 20 76 61 6c 75 65 73 2e 20 20 54 68 65 0a  it values.  The.
3ea5f 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 6d  ** initializer m
3ea60 75 73 74 20 62 65 20 6b 65 70 74 20 69 6e 20 73  ust be kept in s
3ea61 79 6e 63 20 77 69 74 68 20 74 68 65 20 53 51 4c  ync with the SQL
3ea62 49 54 45 5f 4c 49 4d 49 54 5f 2a 0a 2a 2a 20 23  ITE_LIMIT_*.** #
3ea63 64 65 66 69 6e 65 73 20 69 6e 20 73 71 6c 69 74  defines in sqlit
3ea64 65 33 2e 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  e3.h..*/.static 
3ea65 63 6f 6e 73 74 20 69 6e 74 20 61 48 61 72 64 4c  const int aHardL
3ea66 69 6d 69 74 5b 5d 20 3d 20 7b 0a 20 20 53 51 4c  imit[] = {.  SQL
3ea67 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2c 0a  ITE_MAX_LENGTH,.
3ea68 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c    SQLITE_MAX_SQL
3ea69 5f 4c 45 4e 47 54 48 2c 0a 20 20 53 51 4c 49 54  _LENGTH,.  SQLIT
3ea6a 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 2c 0a 20 20  E_MAX_COLUMN,.  
3ea6b 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
3ea6c 44 45 50 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f  DEPTH,.  SQLITE_
3ea6d 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MAX_COMPOUND_SEL
3ea6e 45 43 54 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41  ECT,.  SQLITE_MA
3ea6f 58 5f 56 44 42 45 5f 4f 50 2c 0a 20 20 53 51 4c  X_VDBE_OP,.  SQL
3ea70 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e  ITE_MAX_FUNCTION
3ea71 5f 41 52 47 2c 0a 20 20 53 51 4c 49 54 45 5f 4d  _ARG,.  SQLITE_M
3ea72 41 58 5f 41 54 54 41 43 48 45 44 2c 0a 20 20 53  AX_ATTACHED,.  S
3ea73 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
3ea74 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 2c 0a 20  ATTERN_LENGTH,. 
3ea75 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49   SQLITE_MAX_VARI
3ea76 41 42 4c 45 5f 4e 55 4d 42 45 52 2c 0a 20 20 53  ABLE_NUMBER,.  S
3ea77 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45  QLITE_MAX_TRIGGE
3ea78 52 5f 44 45 50 54 48 2c 0a 7d 3b 0a 0a 2f 2a 0a  R_DEPTH,.};../*.
3ea79 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  ** Make sure the
3ea7a 20 68 61 72 64 20 6c 69 6d 69 74 73 20 61 72 65   hard limits are
3ea7b 20 73 65 74 20 74 6f 20 72 65 61 73 6f 6e 61 62   set to reasonab
3ea7c 6c 65 20 76 61 6c 75 65 73 0a 2a 2f 0a 23 69 66  le values.*/.#if
3ea7d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
3ea7e 54 48 3c 31 30 30 0a 23 20 65 72 72 6f 72 20 53  TH<100.# error S
3ea7f 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48  QLITE_MAX_LENGTH
3ea80 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73   must be at leas
3ea81 74 20 31 30 30 0a 23 65 6e 64 69 66 0a 23 69 66  t 100.#endif.#if
3ea82 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f   SQLITE_MAX_SQL_
3ea83 4c 45 4e 47 54 48 3c 31 30 30 0a 23 20 65 72 72  LENGTH<100.# err
3ea84 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51  or SQLITE_MAX_SQ
3ea85 4c 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20 62 65  L_LENGTH must be
3ea86 20 61 74 20 6c 65 61 73 74 20 31 30 30 0a 23 65   at least 100.#e
3ea87 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
3ea88 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 3e 53  MAX_SQL_LENGTH>S
3ea89 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48  QLITE_MAX_LENGTH
3ea8a 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
3ea8b 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 6d  MAX_SQL_LENGTH m
3ea8c 75 73 74 20 6e 6f 74 20 62 65 20 67 72 65 61 74  ust not be great
3ea8d 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4d  er than SQLITE_M
3ea8e 41 58 5f 4c 45 4e 47 54 48 0a 23 65 6e 64 69 66  AX_LENGTH.#endif
3ea8f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
3ea90 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 3c  COMPOUND_SELECT<
3ea91 32 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45  2.# error SQLITE
3ea92 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  _MAX_COMPOUND_SE
3ea93 4c 45 43 54 20 6d 75 73 74 20 62 65 20 61 74 20  LECT must be at 
3ea94 6c 65 61 73 74 20 32 0a 23 65 6e 64 69 66 0a 23  least 2.#endif.#
3ea95 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44  if SQLITE_MAX_VD
3ea96 42 45 5f 4f 50 3c 34 30 0a 23 20 65 72 72 6f 72  BE_OP<40.# error
3ea97 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45   SQLITE_MAX_VDBE
3ea98 5f 4f 50 20 6d 75 73 74 20 62 65 20 61 74 20 6c  _OP must be at l
3ea99 65 61 73 74 20 34 30 0a 23 65 6e 64 69 66 0a 23  east 40.#endif.#
3ea9a 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55  if SQLITE_MAX_FU
3ea9b 4e 43 54 49 4f 4e 5f 41 52 47 3c 30 20 7c 7c 20  NCTION_ARG<0 || 
3ea9c 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54  SQLITE_MAX_FUNCT
3ea9d 49 4f 4e 5f 41 52 47 3e 31 30 30 30 0a 23 20 65  ION_ARG>1000.# e
3ea9e 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f  rror SQLITE_MAX_
3ea9f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 6d 75 73  FUNCTION_ARG mus
3eaa0 74 20 62 65 20 62 65 74 77 65 65 6e 20 30 20 61  t be between 0 a
3eaa1 6e 64 20 31 30 30 30 0a 23 65 6e 64 69 66 0a 23  nd 1000.#endif.#
3eaa2 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54  if SQLITE_MAX_AT
3eaa3 54 41 43 48 45 44 3c 30 20 7c 7c 20 53 51 4c 49  TACHED<0 || SQLI
3eaa4 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 3e  TE_MAX_ATTACHED>
3eaa5 36 32 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54  62.# error SQLIT
3eaa6 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 20 6d  E_MAX_ATTACHED m
3eaa7 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 30  ust be between 0
3eaa8 20 61 6e 64 20 36 32 0a 23 65 6e 64 69 66 0a 23   and 62.#endif.#
3eaa9 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49  if SQLITE_MAX_LI
3eaaa 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
3eaab 48 3c 31 0a 23 20 65 72 72 6f 72 20 53 51 4c 49  H<1.# error SQLI
3eaac 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54  TE_MAX_LIKE_PATT
3eaad 45 52 4e 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20  ERN_LENGTH must 
3eaae 62 65 20 61 74 20 6c 65 61 73 74 20 31 0a 23 65  be at least 1.#e
3eaaf 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
3eab0 4d 41 58 5f 43 4f 4c 55 4d 4e 3e 33 32 37 36 37  MAX_COLUMN>32767
3eab1 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
3eab2 4d 41 58 5f 43 4f 4c 55 4d 4e 20 6d 75 73 74 20  MAX_COLUMN must 
3eab3 6e 6f 74 20 65 78 63 65 65 64 20 33 32 37 36 37  not exceed 32767
3eab4 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
3eab5 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44  TE_MAX_TRIGGER_D
3eab6 45 50 54 48 3c 31 0a 23 20 65 72 72 6f 72 20 53  EPTH<1.# error S
3eab7 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45  QLITE_MAX_TRIGGE
3eab8 52 5f 44 45 50 54 48 20 6d 75 73 74 20 62 65 20  R_DEPTH must be 
3eab9 61 74 20 6c 65 61 73 74 20 31 0a 23 65 6e 64 69  at least 1.#endi
3eaba 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  f.../*.** Change
3eabb 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20   the value of a 
3eabc 6c 69 6d 69 74 2e 20 20 52 65 70 6f 72 74 20 74  limit.  Report t
3eabd 68 65 20 6f 6c 64 20 76 61 6c 75 65 2e 0a 2a 2a  he old value..**
3eabe 20 49 66 20 61 6e 20 69 6e 76 61 6c 69 64 20 6c   If an invalid l
3eabf 69 6d 69 74 20 69 6e 64 65 78 20 69 73 20 73 75  imit index is su
3eac0 70 70 6c 69 65 64 2c 20 72 65 70 6f 72 74 20 2d  pplied, report -
3eac1 31 2e 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68  1..** Make no ch
3eac2 61 6e 67 65 73 20 62 75 74 20 73 74 69 6c 6c 20  anges but still 
3eac3 72 65 70 6f 72 74 20 74 68 65 20 6f 6c 64 20 76  report the old v
3eac4 61 6c 75 65 20 69 66 20 74 68 65 0a 2a 2a 20 6e  alue if the.** n
3eac5 65 77 20 6c 69 6d 69 74 20 69 73 20 6e 65 67 61  ew limit is nega
3eac6 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65  tive..**.** A ne
3eac7 77 20 6c 6f 77 65 72 20 6c 69 6d 69 74 20 64 6f  w lower limit do
3eac8 65 73 20 6e 6f 74 20 73 68 72 69 6e 6b 20 65 78  es not shrink ex
3eac9 69 73 74 69 6e 67 20 63 6f 6e 73 74 72 75 63 74  isting construct
3eaca 73 2e 0a 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20  s..** It merely 
3eacb 70 72 65 76 65 6e 74 73 20 6e 65 77 20 63 6f 6e  prevents new con
3eacc 73 74 72 75 63 74 73 20 74 68 61 74 20 65 78 63  structs that exc
3eacd 65 65 64 20 74 68 65 20 6c 69 6d 69 74 0a 2a 2a  eed the limit.**
3eace 20 66 72 6f 6d 20 66 6f 72 6d 69 6e 67 2e 0a 2a   from forming..*
3eacf 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
3ead0 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73   sqlite3_limit(s
3ead1 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
3ead2 6c 69 6d 69 74 49 64 2c 20 69 6e 74 20 6e 65 77  limitId, int new
3ead3 4c 69 6d 69 74 29 7b 0a 20 20 69 6e 74 20 6f 6c  Limit){.  int ol
3ead4 64 4c 69 6d 69 74 3b 0a 0a 0a 20 20 2f 2a 20 45  dLimit;...  /* E
3ead5 56 49 44 45 4e 43 45 2d 4f 46 3a 20 52 2d 33 30  VIDENCE-OF: R-30
3ead6 31 38 39 2d 35 34 30 39 37 20 46 6f 72 20 65 61  189-54097 For ea
3ead7 63 68 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72  ch limit categor
3ead8 79 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4e  y SQLITE_LIMIT_N
3ead9 41 4d 45 0a 20 20 2a 2a 20 74 68 65 72 65 20 69  AME.  ** there i
3eada 73 20 61 20 68 61 72 64 20 75 70 70 65 72 20 62  s a hard upper b
3eadb 6f 75 6e 64 20 73 65 74 20 61 74 20 63 6f 6d 70  ound set at comp
3eadc 69 6c 65 2d 74 69 6d 65 20 62 79 20 61 20 43 20  ile-time by a C 
3eadd 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 20 20 2a  preprocessor.  *
3eade 2a 20 6d 61 63 72 6f 20 63 61 6c 6c 65 64 20 53  * macro called S
3eadf 51 4c 49 54 45 5f 4d 41 58 5f 4e 41 4d 45 2e 20  QLITE_MAX_NAME. 
3eae0 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f 22 20 69  (The "_LIMIT_" i
3eae1 6e 20 74 68 65 20 6e 61 6d 65 20 69 73 20 63 68  n the name is ch
3eae2 61 6e 67 65 64 20 74 6f 0a 20 20 2a 2a 20 22 5f  anged to.  ** "_
3eae3 4d 41 58 5f 22 2e 29 0a 20 20 2a 2f 0a 20 20 61  MAX_".).  */.  a
3eae4 73 73 65 72 74 28 20 61 48 61 72 64 4c 69 6d 69  ssert( aHardLimi
3eae5 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
3eae6 45 4e 47 54 48 5d 3d 3d 53 51 4c 49 54 45 5f 4d  ENGTH]==SQLITE_M
3eae7 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 61  AX_LENGTH );.  a
3eae8 73 73 65 72 74 28 20 61 48 61 72 64 4c 69 6d 69  ssert( aHardLimi
3eae9 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53  t[SQLITE_LIMIT_S
3eaea 51 4c 5f 4c 45 4e 47 54 48 5d 3d 3d 53 51 4c 49  QL_LENGTH]==SQLI
3eaeb 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54  TE_MAX_SQL_LENGT
3eaec 48 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61  H );.  assert( a
3eaed 48 61 72 64 4c 69 6d 69 74 5b 53 51 4c 49 54 45  HardLimit[SQLITE
3eaee 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 3d 3d  _LIMIT_COLUMN]==
3eaef 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
3eaf0 4e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61  N );.  assert( a
3eaf1 48 61 72 64 4c 69 6d 69 74 5b 53 51 4c 49 54 45  HardLimit[SQLITE
3eaf2 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 54  _LIMIT_EXPR_DEPT
3eaf3 48 5d 3d 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 45  H]==SQLITE_MAX_E
3eaf4 58 50 52 5f 44 45 50 54 48 20 29 3b 0a 20 20 61  XPR_DEPTH );.  a
3eaf5 73 73 65 72 74 28 20 61 48 61 72 64 4c 69 6d 69  ssert( aHardLimi
3eaf6 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43  t[SQLITE_LIMIT_C
3eaf7 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 5d 3d  OMPOUND_SELECT]=
3eaf8 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50  =SQLITE_MAX_COMP
3eaf9 4f 55 4e 44 5f 53 45 4c 45 43 54 29 3b 0a 20 20  OUND_SELECT);.  
3eafa 61 73 73 65 72 74 28 20 61 48 61 72 64 4c 69 6d  assert( aHardLim
3eafb 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
3eafc 56 44 42 45 5f 4f 50 5d 3d 3d 53 51 4c 49 54 45  VDBE_OP]==SQLITE
3eafd 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 20 29 3b 0a  _MAX_VDBE_OP );.
3eafe 20 20 61 73 73 65 72 74 28 20 61 48 61 72 64 4c    assert( aHardL
3eaff 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
3eb00 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 5d 3d  T_FUNCTION_ARG]=
3eb01 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43  =SQLITE_MAX_FUNC
3eb02 54 49 4f 4e 5f 41 52 47 20 29 3b 0a 20 20 61 73  TION_ARG );.  as
3eb03 73 65 72 74 28 20 61 48 61 72 64 4c 69 6d 69 74  sert( aHardLimit
3eb04 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54  [SQLITE_LIMIT_AT
3eb05 54 41 43 48 45 44 5d 3d 3d 53 51 4c 49 54 45 5f  TACHED]==SQLITE_
3eb06 4d 41 58 5f 41 54 54 41 43 48 45 44 20 29 3b 0a  MAX_ATTACHED );.
3eb07 20 20 61 73 73 65 72 74 28 20 61 48 61 72 64 4c    assert( aHardL
3eb08 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
3eb09 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  T_LIKE_PATTERN_L
3eb0a 45 4e 47 54 48 5d 3d 3d 0a 20 20 20 20 20 20 20  ENGTH]==.       
3eb0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3eb0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3eb0d 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4d          SQLITE_M
3eb0e 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  AX_LIKE_PATTERN_
3eb0f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 61 73 73 65  LENGTH );.  asse
3eb10 72 74 28 20 61 48 61 72 64 4c 69 6d 69 74 5b 53  rt( aHardLimit[S
3eb11 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49  QLITE_LIMIT_VARI
3eb12 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 3d 3d 53 51  ABLE_NUMBER]==SQ
3eb13 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c  LITE_MAX_VARIABL
3eb14 45 5f 4e 55 4d 42 45 52 29 3b 0a 20 20 61 73 73  E_NUMBER);.  ass
3eb15 65 72 74 28 20 61 48 61 72 64 4c 69 6d 69 74 5b  ert( aHardLimit[
3eb16 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49  SQLITE_LIMIT_TRI
3eb17 47 47 45 52 5f 44 45 50 54 48 5d 3d 3d 53 51 4c  GGER_DEPTH]==SQL
3eb18 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f  ITE_MAX_TRIGGER_
3eb19 44 45 50 54 48 20 29 3b 0a 20 20 61 73 73 65 72  DEPTH );.  asser
3eb1a 74 28 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  t( SQLITE_LIMIT_
3eb1b 54 52 49 47 47 45 52 5f 44 45 50 54 48 3d 3d 28  TRIGGER_DEPTH==(
3eb1c 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 2d 31  SQLITE_N_LIMIT-1
3eb1d 29 20 29 3b 0a 0a 0a 20 20 69 66 28 20 6c 69 6d  ) );...  if( lim
3eb1e 69 74 49 64 3c 30 20 7c 7c 20 6c 69 6d 69 74 49  itId<0 || limitI
3eb1f 64 3e 3d 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49  d>=SQLITE_N_LIMI
3eb20 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  T ){.    return 
3eb21 2d 31 3b 0a 20 20 7d 0a 20 20 6f 6c 64 4c 69 6d  -1;.  }.  oldLim
3eb22 69 74 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b  it = db->aLimit[
3eb23 6c 69 6d 69 74 49 64 5d 3b 0a 20 20 69 66 28 20  limitId];.  if( 
3eb24 6e 65 77 4c 69 6d 69 74 3e 3d 30 20 29 7b 20 20  newLimit>=0 ){  
3eb25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3eb26 20 2f 2a 20 49 4d 50 3a 20 52 2d 35 32 34 37 36   /* IMP: R-52476
3eb27 2d 32 38 37 33 32 20 2a 2f 0a 20 20 20 20 69 66  -28732 */.    if
3eb28 28 20 6e 65 77 4c 69 6d 69 74 3e 61 48 61 72 64  ( newLimit>aHard
3eb29 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 20 29  Limit[limitId] )
3eb2a 7b 0a 20 20 20 20 20 20 6e 65 77 4c 69 6d 69 74  {.      newLimit
3eb2b 20 3d 20 61 48 61 72 64 4c 69 6d 69 74 5b 6c 69   = aHardLimit[li
3eb2c 6d 69 74 49 64 5d 3b 20 20 2f 2a 20 49 4d 50 3a  mitId];  /* IMP:
3eb2d 20 52 2d 35 31 34 36 33 2d 32 35 36 33 34 20 2a   R-51463-25634 *
3eb2e 2f 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e  /.    }.    db->
3eb2f 61 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 20  aLimit[limitId] 
3eb30 3d 20 6e 65 77 4c 69 6d 69 74 3b 0a 20 20 7d 0a  = newLimit;.  }.
3eb31 20 20 72 65 74 75 72 6e 20 6f 6c 64 4c 69 6d 69    return oldLimi
3eb32 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
3eb33 20 20 20 20 20 20 20 2f 2a 20 49 4d 50 3a 20 52         /* IMP: R
3eb34 2d 35 33 33 34 31 2d 33 35 34 31 39 20 2a 2f 0a  -53341-35419 */.
3eb35 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
3eb36 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
3eb37 6f 20 70 61 72 73 65 20 62 6f 74 68 20 55 52 49  o parse both URI
3eb38 73 20 61 6e 64 20 6e 6f 6e 2d 55 52 49 20 66 69  s and non-URI fi
3eb39 6c 65 6e 61 6d 65 73 20 70 61 73 73 65 64 20 62  lenames passed b
3eb3a 79 20 74 68 65 0a 2a 2a 20 75 73 65 72 20 74 6f  y the.** user to
3eb3b 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 20 73   API functions s
3eb3c 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f 72  qlite3_open() or
3eb3d 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
3eb3e 28 29 2c 20 61 6e 64 20 66 6f 72 20 64 61 74 61  (), and for data
3eb3f 62 61 73 65 0a 2a 2a 20 55 52 49 73 20 73 70 65  base.** URIs spe
3eb40 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20 6f  cified as part o
3eb41 66 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65  f ATTACH stateme
3eb42 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  nts..**.** The f
3eb43 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
3eb44 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
3eb45 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
3eb46 65 20 56 46 53 20 74 6f 20 75 73 65 20 28 6f 72  e VFS to use (or
3eb47 0a 2a 2a 20 61 20 4e 55 4c 4c 20 74 6f 20 73 69  .** a NULL to si
3eb48 67 6e 69 66 79 20 74 68 65 20 64 65 66 61 75 6c  gnify the defaul
3eb49 74 20 56 46 53 29 20 69 66 20 74 68 65 20 55 52  t VFS) if the UR
3eb4a 49 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61  I does not conta
3eb4b 69 6e 20 61 20 22 76 66 73 3d 78 78 78 22 0a 2a  in a "vfs=xxx".*
3eb4c 2a 20 71 75 65 72 79 20 70 61 72 61 6d 65 74 65  * query paramete
3eb4d 72 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  r. The second ar
3eb4e 67 75 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 73 20  gument contains 
3eb4f 74 68 65 20 55 52 49 20 28 6f 72 20 6e 6f 6e 2d  the URI (or non-
3eb50 55 52 49 20 66 69 6c 65 6e 61 6d 65 29 0a 2a 2a  URI filename).**
3eb51 20 69 74 73 65 6c 66 2e 20 57 68 65 6e 20 74 68   itself. When th
3eb52 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
3eb53 61 6c 6c 65 64 20 74 68 65 20 2a 70 46 6c 61 67  alled the *pFlag
3eb54 73 20 76 61 72 69 61 62 6c 65 20 73 68 6f 75 6c  s variable shoul
3eb55 64 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 74 68 65  d contain.** the
3eb56 20 64 65 66 61 75 6c 74 20 66 6c 61 67 73 20 74   default flags t
3eb57 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62  o open the datab
3eb58 61 73 65 20 68 61 6e 64 6c 65 20 77 69 74 68 2e  ase handle with.
3eb59 20 54 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65   The value store
3eb5a 64 20 69 6e 0a 2a 2a 20 2a 70 46 6c 61 67 73 20  d in.** *pFlags 
3eb5b 6d 61 79 20 62 65 20 75 70 64 61 74 65 64 20 62  may be updated b
3eb5c 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20  efore returning 
3eb5d 69 66 20 74 68 65 20 55 52 49 20 66 69 6c 65 6e  if the URI filen
3eb5e 61 6d 65 20 63 6f 6e 74 61 69 6e 73 20 0a 2a 2a  ame contains .**
3eb5f 20 22 63 61 63 68 65 3d 78 78 78 22 20 6f 72 20   "cache=xxx" or 
3eb60 22 6d 6f 64 65 3d 78 78 78 22 20 71 75 65 72 79  "mode=xxx" query
3eb61 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a   parameters..**.
3eb62 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
3eb63 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
3eb64 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73  eturned. In this
3eb65 20 63 61 73 65 20 2a 70 70 56 66 73 20 69 73 20   case *ppVfs is 
3eb66 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 0a  set to point to.
3eb67 2a 2a 20 74 68 65 20 56 46 53 20 74 68 61 74 20  ** the VFS that 
3eb68 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 74  should be used t
3eb69 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62  o open the datab
3eb6a 61 73 65 20 66 69 6c 65 2e 20 2a 70 7a 46 69 6c  ase file. *pzFil
3eb6b 65 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 70  e is set to.** p
3eb6c 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72  oint to a buffer
3eb6d 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
3eb6e 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65  name of the file
3eb6f 20 74 6f 20 6f 70 65 6e 2e 20 49 74 20 69 73 20   to open. It is 
3eb70 74 68 65 20 0a 2a 2a 20 72 65 73 70 6f 6e 73 69  the .** responsi
3eb71 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61  bility of the ca
3eb72 6c 6c 65 72 20 74 6f 20 65 76 65 6e 74 75 61 6c  ller to eventual
3eb73 6c 79 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f  ly call sqlite3_
3eb74 66 72 65 65 28 29 20 74 6f 20 72 65 6c 65 61 73  free() to releas
3eb75 65 0a 2a 2a 20 74 68 69 73 20 62 75 66 66 65 72  e.** this buffer
3eb76 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72  ..**.** If an er
3eb77 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e  ror occurs, then
3eb78 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
3eb79 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
3eb7a 64 20 61 6e 64 20 2a 70 7a 45 72 72 4d 73 67 0a  d and *pzErrMsg.
3eb7b 2a 2a 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f  ** may be set to
3eb7c 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66   point to a buff
3eb7d 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e  er containing an
3eb7e 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67   English languag
3eb7f 65 20 65 72 72 6f 72 20 0a 2a 2a 20 6d 65 73 73  e error .** mess
3eb80 61 67 65 2e 20 49 74 20 69 73 20 74 68 65 20 72  age. It is the r
3eb81 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66  esponsibility of
3eb82 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 65   the caller to e
3eb83 76 65 6e 74 75 61 6c 6c 79 20 72 65 6c 65 61 73  ventually releas
3eb84 65 0a 2a 2a 20 74 68 69 73 20 62 75 66 66 65 72  e.** this buffer
3eb85 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69   by calling sqli
3eb86 74 65 33 5f 66 72 65 65 28 29 2e 0a 2a 2f 0a 53  te3_free()..*/.S
3eb87 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
3eb88 74 20 73 71 6c 69 74 65 33 50 61 72 73 65 55 72  t sqlite3ParseUr
3eb89 69 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  i(.  const char 
3eb8a 2a 7a 44 65 66 61 75 6c 74 56 66 73 2c 20 20 20  *zDefaultVfs,   
3eb8b 20 20 20 20 20 2f 2a 20 56 46 53 20 74 6f 20 75       /* VFS to u
3eb8c 73 65 20 69 66 20 6e 6f 20 22 76 66 73 3d 78 78  se if no "vfs=xx
3eb8d 78 22 20 71 75 65 72 79 20 6f 70 74 69 6f 6e 20  x" query option 
3eb8e 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
3eb8f 2a 7a 55 72 69 2c 20 20 20 20 20 20 20 20 20 20  *zUri,          
3eb90 20 20 20 20 20 2f 2a 20 4e 75 6c 2d 74 65 72 6d       /* Nul-term
3eb91 69 6e 61 74 65 64 20 55 52 49 20 74 6f 20 70 61  inated URI to pa
3eb92 72 73 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  rse */.  unsigne
3eb93 64 20 69 6e 74 20 2a 70 46 6c 61 67 73 2c 20 20  d int *pFlags,  
3eb94 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
3eb95 55 54 3a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  UT: SQLITE_OPEN_
3eb96 58 58 58 20 66 6c 61 67 73 20 2a 2f 0a 20 20 73  XXX flags */.  s
3eb97 71 6c 69 74 65 33 5f 76 66 73 20 2a 2a 70 70 56  qlite3_vfs **ppV
3eb98 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  fs,            /
3eb99 2a 20 4f 55 54 3a 20 56 46 53 20 74 6f 20 75 73  * OUT: VFS to us
3eb9a 65 20 2a 2f 20 0a 20 20 63 68 61 72 20 2a 2a 70  e */ .  char **p
3eb9b 7a 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20  zFile,          
3eb9c 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
3eb9d 46 69 6c 65 6e 61 6d 65 20 63 6f 6d 70 6f 6e 65  Filename compone
3eb9e 6e 74 20 6f 66 20 55 52 49 20 2a 2f 0a 20 20 63  nt of URI */.  c
3eb9f 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20  har **pzErrMsg  
3eba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3eba1 2a 20 4f 55 54 3a 20 45 72 72 6f 72 20 6d 65 73  * OUT: Error mes
3eba2 73 61 67 65 20 28 69 66 20 72 63 21 3d 53 51 4c  sage (if rc!=SQL
3eba3 49 54 45 5f 4f 4b 29 20 2a 2f 0a 29 7b 0a 20 20  ITE_OK) */.){.  
3eba4 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
3eba5 4f 4b 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  OK;.  unsigned i
3eba6 6e 74 20 66 6c 61 67 73 20 3d 20 2a 70 46 6c 61  nt flags = *pFla
3eba7 67 73 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  gs;.  const char
3eba8 20 2a 7a 56 66 73 20 3d 20 7a 44 65 66 61 75 6c   *zVfs = zDefaul
3eba9 74 56 66 73 3b 0a 20 20 63 68 61 72 20 2a 7a 46  tVfs;.  char *zF
3ebaa 69 6c 65 3b 0a 20 20 63 68 61 72 20 63 3b 0a 20  ile;.  char c;. 
3ebab 20 69 6e 74 20 6e 55 72 69 20 3d 20 73 71 6c 69   int nUri = sqli
3ebac 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 55 72 69  te3Strlen30(zUri
3ebad 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 2a 70  );..  assert( *p
3ebae 7a 45 72 72 4d 73 67 3d 3d 30 20 29 3b 0a 0a 20  zErrMsg==0 );.. 
3ebaf 20 69 66 28 20 28 28 66 6c 61 67 73 20 26 20 53   if( ((flags & S
3ebb0 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49 29 20  QLITE_OPEN_URI) 
3ebb1 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  || sqlite3Global
3ebb2 43 6f 6e 66 69 67 2e 62 4f 70 65 6e 55 72 69 29  Config.bOpenUri)
3ebb3 20 0a 20 20 20 26 26 20 6e 55 72 69 3e 3d 35 20   .   && nUri>=5 
3ebb4 26 26 20 6d 65 6d 63 6d 70 28 7a 55 72 69 2c 20  && memcmp(zUri, 
3ebb5 22 66 69 6c 65 3a 22 2c 20 35 29 3d 3d 30 20 0a  "file:", 5)==0 .
3ebb6 20 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a    ){.    char *z
3ebb7 4f 70 74 3b 0a 20 20 20 20 69 6e 74 20 65 53 74  Opt;.    int eSt
3ebb8 61 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ate;            
3ebb9 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72         /* Parser
3ebba 20 73 74 61 74 65 20 77 68 65 6e 20 70 61 72 73   state when pars
3ebbb 69 6e 67 20 55 52 49 20 2a 2f 0a 20 20 20 20 69  ing URI */.    i
3ebbc 6e 74 20 69 49 6e 3b 20 20 20 20 20 20 20 20 20  nt iIn;         
3ebbd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3ebbe 49 6e 70 75 74 20 63 68 61 72 61 63 74 65 72 20  Input character 
3ebbf 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74  index */.    int
3ebc0 20 69 4f 75 74 20 3d 20 30 3b 20 20 20 20 20 20   iOut = 0;      
3ebc1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75             /* Ou
3ebc2 74 70 75 74 20 63 68 61 72 61 63 74 65 72 20 69  tput character i
3ebc3 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ndex */.    int 
3ebc4 6e 42 79 74 65 20 3d 20 6e 55 72 69 2b 32 3b 20  nByte = nUri+2; 
3ebc5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
3ebc6 65 73 20 6f 66 20 73 70 61 63 65 20 74 6f 20 61  es of space to a
3ebc7 6c 6c 6f 63 61 74 65 20 2a 2f 0a 0a 20 20 20 20  llocate */..    
3ebc8 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
3ebc9 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55 52 49   SQLITE_OPEN_URI
3ebca 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f 20   flag is set to 
3ebcb 69 6e 64 69 63 61 74 65 20 74 6f 20 74 68 65 20  indicate to the 
3ebcc 56 46 53 20 78 4f 70 65 6e 20 0a 20 20 20 20 2a  VFS xOpen .    *
3ebcd 2a 20 6d 65 74 68 6f 64 20 74 68 61 74 20 74 68  * method that th
3ebce 65 72 65 20 6d 61 79 20 62 65 20 65 78 74 72 61  ere may be extra
3ebcf 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f 6c 6c   parameters foll
3ebd0 6f 77 69 6e 67 20 74 68 65 20 66 69 6c 65 2d 6e  owing the file-n
3ebd1 61 6d 65 2e 20 20 2a 2f 0a 20 20 20 20 66 6c 61  ame.  */.    fla
3ebd2 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45  gs |= SQLITE_OPE
3ebd3 4e 5f 55 52 49 3b 0a 0a 20 20 20 20 66 6f 72 28  N_URI;..    for(
3ebd4 69 49 6e 3d 30 3b 20 69 49 6e 3c 6e 55 72 69 3b  iIn=0; iIn<nUri;
3ebd5 20 69 49 6e 2b 2b 29 20 6e 42 79 74 65 20 2b 3d   iIn++) nByte +=
3ebd6 20 28 7a 55 72 69 5b 69 49 6e 5d 3d 3d 27 26 27   (zUri[iIn]=='&'
3ebd7 29 3b 0a 20 20 20 20 7a 46 69 6c 65 20 3d 20 73  );.    zFile = s
3ebd8 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42  qlite3_malloc(nB
3ebd9 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  yte);.    if( !z
3ebda 46 69 6c 65 20 29 20 72 65 74 75 72 6e 20 53 51  File ) return SQ
3ebdb 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 20  LITE_NOMEM;..   
3ebdc 20 2f 2a 20 44 69 73 63 61 72 64 20 74 68 65 20   /* Discard the 
3ebdd 73 63 68 65 6d 65 20 61 6e 64 20 61 75 74 68 6f  scheme and autho
3ebde 72 69 74 79 20 73 65 67 6d 65 6e 74 73 20 6f 66  rity segments of
3ebdf 20 74 68 65 20 55 52 49 2e 20 2a 2f 0a 20 20 20   the URI. */.   
3ebe0 20 69 66 28 20 7a 55 72 69 5b 35 5d 3d 3d 27 2f   if( zUri[5]=='/
3ebe1 27 20 26 26 20 7a 55 72 69 5b 36 5d 3d 3d 27 2f  ' && zUri[6]=='/
3ebe2 27 20 29 7b 0a 20 20 20 20 20 20 69 49 6e 20 3d  ' ){.      iIn =
3ebe3 20 37 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   7;.      while(
3ebe4 20 7a 55 72 69 5b 69 49 6e 5d 20 26 26 20 7a 55   zUri[iIn] && zU
3ebe5 72 69 5b 69 49 6e 5d 21 3d 27 2f 27 20 29 20 69  ri[iIn]!='/' ) i
3ebe6 49 6e 2b 2b 3b 0a 0a 20 20 20 20 20 20 69 66 28  In++;..      if(
3ebe7 20 69 49 6e 21 3d 37 20 26 26 20 28 69 49 6e 21   iIn!=7 && (iIn!
3ebe8 3d 31 36 20 7c 7c 20 6d 65 6d 63 6d 70 28 22 6c  =16 || memcmp("l
3ebe9 6f 63 61 6c 68 6f 73 74 22 2c 20 26 7a 55 72 69  ocalhost", &zUri
3ebea 5b 37 5d 2c 20 39 29 29 20 29 7b 0a 20 20 20 20  [7], 9)) ){.    
3ebeb 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20      *pzErrMsg = 
3ebec 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
3ebed 22 69 6e 76 61 6c 69 64 20 75 72 69 20 61 75 74  "invalid uri aut
3ebee 68 6f 72 69 74 79 3a 20 25 2e 2a 73 22 2c 20 0a  hority: %.*s", .
3ebef 20 20 20 20 20 20 20 20 20 20 20 20 69 49 6e 2d              iIn-
3ebf0 37 2c 20 26 7a 55 72 69 5b 37 5d 29 3b 0a 20 20  7, &zUri[7]);.  
3ebf1 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
3ebf2 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20  E_ERROR;.       
3ebf3 20 67 6f 74 6f 20 70 61 72 73 65 5f 75 72 69 5f   goto parse_uri_
3ebf4 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  out;.      }.   
3ebf5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 49   }else{.      iI
3ebf6 6e 20 3d 20 35 3b 0a 20 20 20 20 7d 0a 0a 20 20  n = 5;.    }..  
3ebf7 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 66 69    /* Copy the fi
3ebf8 6c 65 6e 61 6d 65 20 61 6e 64 20 61 6e 79 20 71  lename and any q
3ebf9 75 65 72 79 20 70 61 72 61 6d 65 74 65 72 73 20  uery parameters 
3ebfa 69 6e 74 6f 20 74 68 65 20 7a 46 69 6c 65 20 62  into the zFile b
3ebfb 75 66 66 65 72 2e 20 0a 20 20 20 20 2a 2a 20 44  uffer. .    ** D
3ebfc 65 63 6f 64 65 20 25 48 48 20 65 73 63 61 70 65  ecode %HH escape
3ebfd 20 63 6f 64 65 73 20 61 6c 6f 6e 67 20 74 68 65   codes along the
3ebfe 20 77 61 79 2e 20 0a 20 20 20 20 2a 2a 0a 20 20   way. .    **.  
3ebff 20 20 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73    ** Within this
3ec00 20 6c 6f 6f 70 2c 20 76 61 72 69 61 62 6c 65 20   loop, variable 
3ec01 65 53 74 61 74 65 20 6d 61 79 20 62 65 20 73 65  eState may be se
3ec02 74 20 74 6f 20 30 2c 20 31 20 6f 72 20 32 2c 20  t to 0, 1 or 2, 
3ec03 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20 2a 2a  depending.    **
3ec04 20 6f 6e 20 74 68 65 20 70 61 72 73 69 6e 67 20   on the parsing 
3ec05 63 6f 6e 74 65 78 74 2e 20 41 73 20 66 6f 6c 6c  context. As foll
3ec06 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ows:.    **.    
3ec07 2a 2a 20 20 20 30 3a 20 50 61 72 73 69 6e 67 20  **   0: Parsing 
3ec08 66 69 6c 65 2d 6e 61 6d 65 2e 0a 20 20 20 20 2a  file-name..    *
3ec09 2a 20 20 20 31 3a 20 50 61 72 73 69 6e 67 20 6e  *   1: Parsing n
3ec0a 61 6d 65 20 73 65 63 74 69 6f 6e 20 6f 66 20 61  ame section of a
3ec0b 20 6e 61 6d 65 3d 76 61 6c 75 65 20 71 75 65 72   name=value quer
3ec0c 79 20 70 61 72 61 6d 65 74 65 72 2e 0a 20 20 20  y parameter..   
3ec0d 20 2a 2a 20 20 20 32 3a 20 50 61 72 73 69 6e 67   **   2: Parsing
3ec0e 20 76 61 6c 75 65 20 73 65 63 74 69 6f 6e 20 6f   value section o
3ec0f 66 20 61 20 6e 61 6d 65 3d 76 61 6c 75 65 20 71  f a name=value q
3ec10 75 65 72 79 20 70 61 72 61 6d 65 74 65 72 2e 0a  uery parameter..
3ec11 20 20 20 20 2a 2f 0a 20 20 20 20 65 53 74 61 74      */.    eStat
3ec12 65 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65  e = 0;.    while
3ec13 28 20 28 63 20 3d 20 7a 55 72 69 5b 69 49 6e 5d  ( (c = zUri[iIn]
3ec14 29 21 3d 30 20 26 26 20 63 21 3d 27 23 27 20 29  )!=0 && c!='#' )
3ec15 7b 0a 20 20 20 20 20 20 69 49 6e 2b 2b 3b 0a 20  {.      iIn++;. 
3ec16 20 20 20 20 20 69 66 28 20 63 3d 3d 27 25 27 20       if( c=='%' 
3ec17 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74  .       && sqlit
3ec18 65 33 49 73 78 64 69 67 69 74 28 7a 55 72 69 5b  e3Isxdigit(zUri[
3ec19 69 49 6e 5d 29 20 0a 20 20 20 20 20 20 20 26 26  iIn]) .       &&
3ec1a 20 73 71 6c 69 74 65 33 49 73 78 64 69 67 69 74   sqlite3Isxdigit
3ec1b 28 7a 55 72 69 5b 69 49 6e 2b 31 5d 29 20 0a 20  (zUri[iIn+1]) . 
3ec1c 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
3ec1d 69 6e 74 20 6f 63 74 65 74 20 3d 20 28 73 71 6c  int octet = (sql
3ec1e 69 74 65 33 48 65 78 54 6f 49 6e 74 28 7a 55 72  ite3HexToInt(zUr
3ec1f 69 5b 69 49 6e 2b 2b 5d 29 20 3c 3c 20 34 29 3b  i[iIn++]) << 4);
3ec20 0a 20 20 20 20 20 20 20 20 6f 63 74 65 74 20 2b  .        octet +
3ec21 3d 20 73 71 6c 69 74 65 33 48 65 78 54 6f 49 6e  = sqlite3HexToIn
3ec22 74 28 7a 55 72 69 5b 69 49 6e 2b 2b 5d 29 3b 0a  t(zUri[iIn++]);.
3ec23 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
3ec24 20 6f 63 74 65 74 3e 3d 30 20 26 26 20 6f 63 74   octet>=0 && oct
3ec25 65 74 3c 32 35 36 20 29 3b 0a 20 20 20 20 20 20  et<256 );.      
3ec26 20 20 69 66 28 20 6f 63 74 65 74 3d 3d 30 20 29    if( octet==0 )
3ec27 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
3ec28 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 74 61  his branch is ta
3ec29 6b 65 6e 20 77 68 65 6e 20 22 25 30 30 22 20 61  ken when "%00" a
3ec2a 70 70 65 61 72 73 20 77 69 74 68 69 6e 20 74 68  ppears within th
3ec2b 65 20 55 52 49 2e 20 49 6e 20 74 68 69 73 0a 20  e URI. In this. 
3ec2c 20 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65           ** case
3ec2d 20 77 65 20 69 67 6e 6f 72 65 20 61 6c 6c 20 74   we ignore all t
3ec2e 65 78 74 20 69 6e 20 74 68 65 20 72 65 6d 61 69  ext in the remai
3ec2f 6e 64 65 72 20 6f 66 20 74 68 65 20 70 61 74 68  nder of the path
3ec30 2c 20 6e 61 6d 65 20 6f 72 0a 20 20 20 20 20 20  , name or.      
3ec31 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 63 75 72      ** value cur
3ec32 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 70 61 72  rently being par
3ec33 73 65 64 2e 20 53 6f 20 69 67 6e 6f 72 65 20 74  sed. So ignore t
3ec34 68 65 20 63 75 72 72 65 6e 74 20 63 68 61 72 61  he current chara
3ec35 63 74 65 72 0a 20 20 20 20 20 20 20 20 20 20 2a  cter.          *
3ec36 2a 20 61 6e 64 20 73 6b 69 70 20 74 6f 20 74 68  * and skip to th
3ec37 65 20 6e 65 78 74 20 22 3f 22 2c 20 22 3d 22 20  e next "?", "=" 
3ec38 6f 72 20 22 26 22 2c 20 61 73 20 61 70 70 72 6f  or "&", as appro
3ec39 70 72 69 61 74 65 2e 20 2a 2f 0a 20 20 20 20 20  priate. */.     
3ec3a 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 20 3d       while( (c =
3ec3b 20 7a 55 72 69 5b 69 49 6e 5d 29 21 3d 30 20 26   zUri[iIn])!=0 &
3ec3c 26 20 63 21 3d 27 23 27 20 0a 20 20 20 20 20 20  & c!='#' .      
3ec3d 20 20 20 20 20 20 20 20 26 26 20 28 65 53 74 61          && (eSta
3ec3e 74 65 21 3d 30 20 7c 7c 20 63 21 3d 27 3f 27 29  te!=0 || c!='?')
3ec3f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26  .              &
3ec40 26 20 28 65 53 74 61 74 65 21 3d 31 20 7c 7c 20  & (eState!=1 || 
3ec41 28 63 21 3d 27 3d 27 20 26 26 20 63 21 3d 27 26  (c!='=' && c!='&
3ec42 27 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  ')).            
3ec43 20 20 26 26 20 28 65 53 74 61 74 65 21 3d 32 20    && (eState!=2 
3ec44 7c 7c 20 63 21 3d 27 26 27 29 0a 20 20 20 20 20  || c!='&').     
3ec45 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
3ec46 20 20 20 20 69 49 6e 2b 2b 3b 0a 20 20 20 20 20      iIn++;.     
3ec47 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
3ec48 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
3ec49 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 20 3d     }.        c =
3ec4a 20 6f 63 74 65 74 3b 0a 20 20 20 20 20 20 7d 65   octet;.      }e
3ec4b 6c 73 65 20 69 66 28 20 65 53 74 61 74 65 3d 3d  lse if( eState==
3ec4c 31 20 26 26 20 28 63 3d 3d 27 26 27 20 7c 7c 20  1 && (c=='&' || 
3ec4d 63 3d 3d 27 3d 27 29 20 29 7b 0a 20 20 20 20 20  c=='=') ){.     
3ec4e 20 20 20 69 66 28 20 7a 46 69 6c 65 5b 69 4f 75     if( zFile[iOu
3ec4f 74 2d 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  t-1]==0 ){.     
3ec50 20 20 20 20 20 2f 2a 20 41 6e 20 65 6d 70 74 79       /* An empty
3ec51 20 6f 70 74 69 6f 6e 20 6e 61 6d 65 2e 20 49 67   option name. Ig
3ec52 6e 6f 72 65 20 74 68 69 73 20 6f 70 74 69 6f 6e  nore this option
3ec53 20 61 6c 74 6f 67 65 74 68 65 72 2e 20 2a 2f 0a   altogether. */.
3ec54 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28            while(
3ec55 20 7a 55 72 69 5b 69 49 6e 5d 20 26 26 20 7a 55   zUri[iIn] && zU
3ec56 72 69 5b 69 49 6e 5d 21 3d 27 23 27 20 26 26 20  ri[iIn]!='#' && 
3ec57 7a 55 72 69 5b 69 49 6e 2d 31 5d 21 3d 27 26 27  zUri[iIn-1]!='&'
3ec58 20 29 20 69 49 6e 2b 2b 3b 0a 20 20 20 20 20 20   ) iIn++;.      
3ec59 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
3ec5a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
3ec5b 69 66 28 20 63 3d 3d 27 26 27 20 29 7b 0a 20 20  if( c=='&' ){.  
3ec5c 20 20 20 20 20 20 20 20 7a 46 69 6c 65 5b 69 4f          zFile[iO
3ec5d 75 74 2b 2b 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  ut++] = '\0';.  
3ec5e 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
3ec5f 20 20 20 20 20 20 20 65 53 74 61 74 65 20 3d 20         eState = 
3ec60 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  2;.        }.   
3ec61 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20       c = 0;.    
3ec62 20 20 7d 65 6c 73 65 20 69 66 28 20 28 65 53 74    }else if( (eSt
3ec63 61 74 65 3d 3d 30 20 26 26 20 63 3d 3d 27 3f 27  ate==0 && c=='?'
3ec64 29 20 7c 7c 20 28 65 53 74 61 74 65 3d 3d 32 20  ) || (eState==2 
3ec65 26 26 20 63 3d 3d 27 26 27 29 20 29 7b 0a 20 20  && c=='&') ){.  
3ec66 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20        c = 0;.   
3ec67 20 20 20 20 20 65 53 74 61 74 65 20 3d 20 31 3b       eState = 1;
3ec68 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a  .      }.      z
3ec69 46 69 6c 65 5b 69 4f 75 74 2b 2b 5d 20 3d 20 63  File[iOut++] = c
3ec6a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
3ec6b 65 53 74 61 74 65 3d 3d 31 20 29 20 7a 46 69 6c  eState==1 ) zFil
3ec6c 65 5b 69 4f 75 74 2b 2b 5d 20 3d 20 27 5c 30 27  e[iOut++] = '\0'
3ec6d 3b 0a 20 20 20 20 7a 46 69 6c 65 5b 69 4f 75 74  ;.    zFile[iOut
3ec6e 2b 2b 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  ++] = '\0';.    
3ec6f 7a 46 69 6c 65 5b 69 4f 75 74 2b 2b 5d 20 3d 20  zFile[iOut++] = 
3ec70 27 5c 30 27 3b 0a 0a 20 20 20 20 2f 2a 20 43 68  '\0';..    /* Ch
3ec71 65 63 6b 20 69 66 20 74 68 65 72 65 20 77 65 72  eck if there wer
3ec72 65 20 61 6e 79 20 6f 70 74 69 6f 6e 73 20 73 70  e any options sp
3ec73 65 63 69 66 69 65 64 20 74 68 61 74 20 73 68 6f  ecified that sho
3ec74 75 6c 64 20 62 65 20 69 6e 74 65 72 70 72 65 74  uld be interpret
3ec75 65 64 20 0a 20 20 20 20 2a 2a 20 68 65 72 65 2e  ed .    ** here.
3ec76 20 4f 70 74 69 6f 6e 73 20 74 68 61 74 20 61 72   Options that ar
3ec77 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 68 65  e interpreted he
3ec78 72 65 20 69 6e 63 6c 75 64 65 20 22 76 66 73 22  re include "vfs"
3ec79 20 61 6e 64 20 74 68 6f 73 65 20 74 68 61 74 0a   and those that.
3ec7a 20 20 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e      ** correspon
3ec7b 64 20 74 6f 20 66 6c 61 67 73 20 74 68 61 74 20  d to flags that 
3ec7c 6d 61 79 20 62 65 20 70 61 73 73 65 64 20 74 6f  may be passed to
3ec7d 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65   the sqlite3_ope
3ec7e 6e 5f 76 32 28 29 0a 20 20 20 20 2a 2a 20 6d 65  n_v2().    ** me
3ec7f 74 68 6f 64 2e 20 2a 2f 0a 20 20 20 20 7a 4f 70  thod. */.    zOp
3ec80 74 20 3d 20 26 7a 46 69 6c 65 5b 73 71 6c 69 74  t = &zFile[sqlit
3ec81 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65  e3Strlen30(zFile
3ec82 29 2b 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28  )+1];.    while(
3ec83 20 7a 4f 70 74 5b 30 5d 20 29 7b 0a 20 20 20 20   zOpt[0] ){.    
3ec84 20 20 69 6e 74 20 6e 4f 70 74 20 3d 20 73 71 6c    int nOpt = sql
3ec85 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4f 70  ite3Strlen30(zOp
3ec86 74 29 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  t);.      char *
3ec87 7a 56 61 6c 20 3d 20 26 7a 4f 70 74 5b 6e 4f 70  zVal = &zOpt[nOp
3ec88 74 2b 31 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20  t+1];.      int 
3ec89 6e 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 53 74  nVal = sqlite3St
3ec8a 72 6c 65 6e 33 30 28 7a 56 61 6c 29 3b 0a 0a 20  rlen30(zVal);.. 
3ec8b 20 20 20 20 20 69 66 28 20 6e 4f 70 74 3d 3d 33       if( nOpt==3
3ec8c 20 26 26 20 6d 65 6d 63 6d 70 28 22 76 66 73 22   && memcmp("vfs"
3ec8d 2c 20 7a 4f 70 74 2c 20 33 29 3d 3d 30 20 29 7b  , zOpt, 3)==0 ){
3ec8e 0a 20 20 20 20 20 20 20 20 7a 56 66 73 20 3d 20  .        zVfs = 
3ec8f 7a 56 61 6c 3b 0a 20 20 20 20 20 20 7d 65 6c 73  zVal;.      }els
3ec90 65 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63  e{.        struc
3ec91 74 20 4f 70 65 6e 4d 6f 64 65 20 7b 0a 20 20 20  t OpenMode {.   
3ec92 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
3ec93 72 20 2a 7a 3b 0a 20 20 20 20 20 20 20 20 20 20  r *z;.          
3ec94 69 6e 74 20 6d 6f 64 65 3b 0a 20 20 20 20 20 20  int mode;.      
3ec95 20 20 7d 20 2a 61 4d 6f 64 65 20 3d 20 30 3b 0a    } *aMode = 0;.
3ec96 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d          char *zM
3ec97 6f 64 65 54 79 70 65 20 3d 20 30 3b 0a 20 20 20  odeType = 0;.   
3ec98 20 20 20 20 20 69 6e 74 20 6d 61 73 6b 20 3d 20       int mask = 
3ec99 30 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c  0;.        int l
3ec9a 69 6d 69 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20  imit = 0;..     
3ec9b 20 20 20 69 66 28 20 6e 4f 70 74 3d 3d 35 20 26     if( nOpt==5 &
3ec9c 26 20 6d 65 6d 63 6d 70 28 22 63 61 63 68 65 22  & memcmp("cache"
3ec9d 2c 20 7a 4f 70 74 2c 20 35 29 3d 3d 30 20 29 7b  , zOpt, 5)==0 ){
3ec9e 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69  .          stati
3ec9f 63 20 73 74 72 75 63 74 20 4f 70 65 6e 4d 6f 64  c struct OpenMod
3eca0 65 20 61 43 61 63 68 65 4d 6f 64 65 5b 5d 20 3d  e aCacheMode[] =
3eca1 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b   {.            {
3eca2 20 22 73 68 61 72 65 64 22 2c 20 20 53 51 4c 49   "shared",  SQLI
3eca3 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41  TE_OPEN_SHAREDCA
3eca4 43 48 45 20 7d 2c 0a 20 20 20 20 20 20 20 20 20  CHE },.         
3eca5 20 20 20 7b 20 22 70 72 69 76 61 74 65 22 2c 20     { "private", 
3eca6 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56  SQLITE_OPEN_PRIV
3eca7 41 54 45 43 41 43 48 45 20 7d 2c 0a 20 20 20 20  ATECACHE },.    
3eca8 20 20 20 20 20 20 20 20 7b 20 30 2c 20 30 20 7d          { 0, 0 }
3eca9 0a 20 20 20 20 20 20 20 20 20 20 7d 3b 0a 0a 20  .          };.. 
3ecaa 20 20 20 20 20 20 20 20 20 6d 61 73 6b 20 3d 20           mask = 
3ecab 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52  SQLITE_OPEN_SHAR
3ecac 45 44 43 41 43 48 45 7c 53 51 4c 49 54 45 5f 4f  EDCACHE|SQLITE_O
3ecad 50 45 4e 5f 50 52 49 56 41 54 45 43 41 43 48 45  PEN_PRIVATECACHE
3ecae 3b 0a 20 20 20 20 20 20 20 20 20 20 61 4d 6f 64  ;.          aMod
3ecaf 65 20 3d 20 61 43 61 63 68 65 4d 6f 64 65 3b 0a  e = aCacheMode;.
3ecb0 20 20 20 20 20 20 20 20 20 20 6c 69 6d 69 74 20            limit 
3ecb1 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20  = mask;.        
3ecb2 20 20 7a 4d 6f 64 65 54 79 70 65 20 3d 20 22 63    zModeType = "c
3ecb3 61 63 68 65 22 3b 0a 20 20 20 20 20 20 20 20 7d  ache";.        }
3ecb4 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 4f 70  .        if( nOp
3ecb5 74 3d 3d 34 20 26 26 20 6d 65 6d 63 6d 70 28 22  t==4 && memcmp("
3ecb6 6d 6f 64 65 22 2c 20 7a 4f 70 74 2c 20 34 29 3d  mode", zOpt, 4)=
3ecb7 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
3ecb8 73 74 61 74 69 63 20 73 74 72 75 63 74 20 4f 70  static struct Op
3ecb9 65 6e 4d 6f 64 65 20 61 4f 70 65 6e 4d 6f 64 65  enMode aOpenMode
3ecba 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20  [] = {.         
3ecbb 20 20 20 7b 20 22 72 6f 22 2c 20 20 53 51 4c 49     { "ro",  SQLI
3ecbc 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
3ecbd 20 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   },.            
3ecbe 7b 20 22 72 77 22 2c 20 20 53 51 4c 49 54 45 5f  { "rw",  SQLITE_
3ecbf 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7d  OPEN_READWRITE }
3ecc0 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  , .            {
3ecc1 20 22 72 77 63 22 2c 20 53 51 4c 49 54 45 5f 4f   "rwc", SQLITE_O
3ecc2 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20  PEN_READWRITE | 
3ecc3 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
3ecc4 54 45 20 7d 2c 0a 20 20 20 20 20 20 20 20 20 20  TE },.          
3ecc5 20 20 7b 20 22 6d 65 6d 6f 72 79 22 2c 20 53 51    { "memory", SQ
3ecc6 4c 49 54 45 5f 4f 50 45 4e 5f 4d 45 4d 4f 52 59  LITE_OPEN_MEMORY
3ecc7 20 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   },.            
3ecc8 7b 20 30 2c 20 30 20 7d 0a 20 20 20 20 20 20 20  { 0, 0 }.       
3ecc9 20 20 20 7d 3b 0a 0a 20 20 20 20 20 20 20 20 20     };..         
3ecca 20 6d 61 73 6b 20 3d 20 53 51 4c 49 54 45 5f 4f   mask = SQLITE_O
3eccb 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 7c 20 53  PEN_READONLY | S
3eccc 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
3eccd 52 49 54 45 0a 20 20 20 20 20 20 20 20 20 20 20  RITE.           
3ecce 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45          | SQLITE
3eccf 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c 20 53  _OPEN_CREATE | S
3ecd0 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 45 4d 4f 52  QLITE_OPEN_MEMOR
3ecd1 59 3b 0a 20 20 20 20 20 20 20 20 20 20 61 4d 6f  Y;.          aMo
3ecd2 64 65 20 3d 20 61 4f 70 65 6e 4d 6f 64 65 3b 0a  de = aOpenMode;.
3ecd3 20 20 20 20 20 20 20 20 20 20 6c 69 6d 69 74 20            limit 
3ecd4 3d 20 6d 61 73 6b 20 26 20 66 6c 61 67 73 3b 0a  = mask & flags;.
3ecd5 20 20 20 20 20 20 20 20 20 20 7a 4d 6f 64 65 54            zModeT
3ecd6 79 70 65 20 3d 20 22 61 63 63 65 73 73 22 3b 0a  ype = "access";.
3ecd7 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
3ecd8 20 20 20 69 66 28 20 61 4d 6f 64 65 20 29 7b 0a     if( aMode ){.
3ecd9 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b            int i;
3ecda 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6d  .          int m
3ecdb 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ode = 0;.       
3ecdc 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 4d 6f 64     for(i=0; aMod
3ecdd 65 5b 69 5d 2e 7a 3b 20 69 2b 2b 29 7b 0a 20 20  e[i].z; i++){.  
3ecde 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
3ecdf 63 68 61 72 20 2a 7a 20 3d 20 61 4d 6f 64 65 5b  char *z = aMode[
3ece0 69 5d 2e 7a 3b 0a 20 20 20 20 20 20 20 20 20 20  i].z;.          
3ece1 20 20 69 66 28 20 6e 56 61 6c 3d 3d 73 71 6c 69    if( nVal==sqli
3ece2 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 20 26  te3Strlen30(z) &
3ece3 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 7a 56 61 6c  & 0==memcmp(zVal
3ece4 2c 20 7a 2c 20 6e 56 61 6c 29 20 29 7b 0a 20 20  , z, nVal) ){.  
3ece5 20 20 20 20 20 20 20 20 20 20 20 20 6d 6f 64 65              mode
3ece6 20 3d 20 61 4d 6f 64 65 5b 69 5d 2e 6d 6f 64 65   = aMode[i].mode
3ece7 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
3ece8 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
3ece9 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
3ecea 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6d  .          if( m
3eceb 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ode==0 ){.      
3ecec 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20        *pzErrMsg 
3eced 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
3ecee 66 28 22 6e 6f 20 73 75 63 68 20 25 73 20 6d 6f  f("no such %s mo
3ecef 64 65 3a 20 25 73 22 2c 20 7a 4d 6f 64 65 54 79  de: %s", zModeTy
3ecf0 70 65 2c 20 7a 56 61 6c 29 3b 0a 20 20 20 20 20  pe, zVal);.     
3ecf1 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
3ecf2 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
3ecf3 20 20 20 20 20 20 67 6f 74 6f 20 70 61 72 73 65        goto parse
3ecf4 5f 75 72 69 5f 6f 75 74 3b 0a 20 20 20 20 20 20  _uri_out;.      
3ecf5 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
3ecf6 69 66 28 20 28 6d 6f 64 65 20 26 20 7e 53 51 4c  if( (mode & ~SQL
3ecf7 49 54 45 5f 4f 50 45 4e 5f 4d 45 4d 4f 52 59 29  ITE_OPEN_MEMORY)
3ecf8 3e 6c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20  >limit ){.      
3ecf9 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20        *pzErrMsg 
3ecfa 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
3ecfb 66 28 22 25 73 20 6d 6f 64 65 20 6e 6f 74 20 61  f("%s mode not a
3ecfc 6c 6c 6f 77 65 64 3a 20 25 73 22 2c 0a 20 20 20  llowed: %s",.   
3ecfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ecfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ecff 20 20 20 20 20 7a 4d 6f 64 65 54 79 70 65 2c 20       zModeType, 
3ed00 7a 56 61 6c 29 3b 0a 20 20 20 20 20 20 20 20 20  zVal);.         
3ed01 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 50     rc = SQLITE_P
3ed02 45 52 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ERM;.           
3ed03 20 67 6f 74 6f 20 70 61 72 73 65 5f 75 72 69 5f   goto parse_uri_
3ed04 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  out;.          }
3ed05 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61 67 73  .          flags
3ed06 20 3d 20 28 66 6c 61 67 73 20 26 20 7e 6d 61 73   = (flags & ~mas
3ed07 6b 29 20 7c 20 6d 6f 64 65 3b 0a 20 20 20 20 20  k) | mode;.     
3ed08 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
3ed09 20 20 20 20 7a 4f 70 74 20 3d 20 26 7a 56 61 6c      zOpt = &zVal
3ed0a 5b 6e 56 61 6c 2b 31 5d 3b 0a 20 20 20 20 7d 0a  [nVal+1];.    }.
3ed0b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 46  .  }else{.    zF
3ed0c 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  ile = sqlite3_ma
3ed0d 6c 6c 6f 63 28 6e 55 72 69 2b 32 29 3b 0a 20 20  lloc(nUri+2);.  
3ed0e 20 20 69 66 28 20 21 7a 46 69 6c 65 20 29 20 72    if( !zFile ) r
3ed0f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
3ed10 45 4d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a  EM;.    memcpy(z
3ed11 46 69 6c 65 2c 20 7a 55 72 69 2c 20 6e 55 72 69  File, zUri, nUri
3ed12 29 3b 0a 20 20 20 20 7a 46 69 6c 65 5b 6e 55 72  );.    zFile[nUr
3ed13 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 7a  i] = '\0';.    z
3ed14 46 69 6c 65 5b 6e 55 72 69 2b 31 5d 20 3d 20 27  File[nUri+1] = '
3ed15 5c 30 27 3b 0a 20 20 20 20 66 6c 61 67 73 20 26  \0';.    flags &
3ed16 3d 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 55  = ~SQLITE_OPEN_U
3ed17 52 49 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 56 66  RI;.  }..  *ppVf
3ed18 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  s = sqlite3_vfs_
3ed19 66 69 6e 64 28 7a 56 66 73 29 3b 0a 20 20 69 66  find(zVfs);.  if
3ed1a 28 20 2a 70 70 56 66 73 3d 3d 30 20 29 7b 0a 20  ( *ppVfs==0 ){. 
3ed1b 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73     *pzErrMsg = s
3ed1c 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
3ed1d 6e 6f 20 73 75 63 68 20 76 66 73 3a 20 25 73 22  no such vfs: %s"
3ed1e 2c 20 7a 56 66 73 29 3b 0a 20 20 20 20 72 63 20  , zVfs);.    rc 
3ed1f 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
3ed20 20 20 7d 0a 20 70 61 72 73 65 5f 75 72 69 5f 6f    }. parse_uri_o
3ed21 75 74 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ut:.  if( rc!=SQ
3ed22 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
3ed23 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 69 6c  qlite3_free(zFil
3ed24 65 29 3b 0a 20 20 20 20 7a 46 69 6c 65 20 3d 20  e);.    zFile = 
3ed25 30 3b 0a 20 20 7d 0a 20 20 2a 70 46 6c 61 67 73  0;.  }.  *pFlags
3ed26 20 3d 20 66 6c 61 67 73 3b 0a 20 20 2a 70 7a 46   = flags;.  *pzF
3ed27 69 6c 65 20 3d 20 7a 46 69 6c 65 3b 0a 20 20 72  ile = zFile;.  r
3ed28 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
3ed29 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
3ed2a 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b 20 6f   does the work o
3ed2b 66 20 6f 70 65 6e 69 6e 67 20 61 20 64 61 74 61  f opening a data
3ed2c 62 61 73 65 20 6f 6e 20 62 65 68 61 6c 66 20 6f  base on behalf o
3ed2d 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65  f.** sqlite3_ope
3ed2e 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  n() and sqlite3_
3ed2f 6f 70 65 6e 31 36 28 29 2e 20 54 68 65 20 64 61  open16(). The da
3ed30 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20  tabase filename 
3ed31 22 7a 46 69 6c 65 6e 61 6d 65 22 20 20 0a 2a 2a  "zFilename"  .**
3ed32 20 69 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 65   is UTF-8 encode
3ed33 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
3ed34 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28 0a 20   openDatabase(. 
3ed35 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
3ed36 6c 65 6e 61 6d 65 2c 20 2f 2a 20 44 61 74 61 62  lename, /* Datab
3ed37 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 55 54 46  ase filename UTF
3ed38 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20  -8 encoded */.  
3ed39 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20  sqlite3 **ppDb, 
3ed3a 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52         /* OUT: R
3ed3b 65 74 75 72 6e 65 64 20 64 61 74 61 62 61 73 65  eturned database
3ed3c 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 75 6e 73   handle */.  uns
3ed3d 69 67 6e 65 64 20 69 6e 74 20 66 6c 61 67 73 2c  igned int flags,
3ed3e 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e      /* Operation
3ed3f 61 6c 20 66 6c 61 67 73 20 2a 2f 0a 20 20 63 6f  al flags */.  co
3ed40 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20  nst char *zVfs  
3ed41 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
3ed42 74 68 65 20 56 46 53 20 74 6f 20 75 73 65 20 2a  the VFS to use *
3ed43 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
3ed44 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  db;             
3ed45 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20         /* Store 
3ed46 61 6c 6c 6f 63 61 74 65 64 20 68 61 6e 64 6c 65  allocated handle
3ed47 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72   here */.  int r
3ed48 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
3ed49 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
3ed4a 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69  turn code */.  i
3ed4b 6e 74 20 69 73 54 68 72 65 61 64 73 61 66 65 3b  nt isThreadsafe;
3ed4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3ed4d 2a 20 54 72 75 65 20 66 6f 72 20 74 68 72 65 61  * True for threa
3ed4e 64 73 61 66 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  dsafe connection
3ed4f 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 70  s */.  char *zOp
3ed50 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  en = 0;         
3ed51 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61         /* Filena
3ed52 6d 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 70  me argument to p
3ed53 61 73 73 20 74 6f 20 42 74 72 65 65 4f 70 65 6e  ass to BtreeOpen
3ed54 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45  () */.  char *zE
3ed55 72 72 4d 73 67 20 3d 20 30 3b 20 20 20 20 20 20  rrMsg = 0;      
3ed56 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72          /* Error
3ed57 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 73 71   message from sq
3ed58 6c 69 74 65 33 50 61 72 73 65 55 72 69 28 29 20  lite3ParseUri() 
3ed59 2a 2f 0a 0a 20 20 2a 70 70 44 62 20 3d 20 30 3b  */..  *ppDb = 0;
3ed5a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
3ed5b 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
3ed5c 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69  rc = sqlite3_ini
3ed5d 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28  tialize();.  if(
3ed5e 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
3ed5f 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4f 6e  .#endif..  /* On
3ed60 6c 79 20 61 6c 6c 6f 77 20 73 65 6e 73 69 62 6c  ly allow sensibl
3ed61 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f  e combinations o
3ed62 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 66 6c  f bits in the fl
3ed63 61 67 73 20 61 72 67 75 6d 65 6e 74 2e 20 20 0a  ags argument.  .
3ed64 20 20 2a 2a 20 54 68 72 6f 77 20 61 6e 20 65 72    ** Throw an er
3ed65 72 6f 72 20 69 66 20 61 6e 79 20 6e 6f 6e 2d 73  ror if any non-s
3ed66 65 6e 73 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  ense combination
3ed67 20 69 73 20 75 73 65 64 2e 20 20 49 66 20 77 65   is used.  If we
3ed68 0a 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 62 6c 6f  .  ** do not blo
3ed69 63 6b 20 69 6c 6c 65 67 61 6c 20 63 6f 6d 62 69  ck illegal combi
3ed6a 6e 61 74 69 6f 6e 73 20 68 65 72 65 2c 20 69 74  nations here, it
3ed6b 20 63 6f 75 6c 64 20 74 72 69 67 67 65 72 0a 20   could trigger. 
3ed6c 20 2a 2a 20 61 73 73 65 72 74 28 29 20 73 74 61   ** assert() sta
3ed6d 74 65 6d 65 6e 74 73 20 69 6e 20 64 65 65 70 65  tements in deepe
3ed6e 72 20 6c 61 79 65 72 73 2e 20 20 53 65 6e 73 69  r layers.  Sensi
3ed6f 62 6c 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73  ble combinations
3ed70 0a 20 20 2a 2a 20 61 72 65 3a 0a 20 20 2a 2a 0a  .  ** are:.  **.
3ed71 20 20 2a 2a 20 20 31 3a 20 20 53 51 4c 49 54 45    **  1:  SQLITE
3ed72 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 0a 20  _OPEN_READONLY. 
3ed73 20 2a 2a 20 20 32 3a 20 20 53 51 4c 49 54 45 5f   **  2:  SQLITE_
3ed74 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 0a 20  OPEN_READWRITE. 
3ed75 20 2a 2a 20 20 36 3a 20 20 53 51 4c 49 54 45 5f   **  6:  SQLITE_
3ed76 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c  OPEN_READWRITE |
3ed77 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
3ed78 41 54 45 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ATE.  */.  asser
3ed79 74 28 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  t( SQLITE_OPEN_R
3ed7a 45 41 44 4f 4e 4c 59 20 20 3d 3d 20 30 78 30 31  EADONLY  == 0x01
3ed7b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51   );.  assert( SQ
3ed7c 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
3ed7d 49 54 45 20 3d 3d 20 30 78 30 32 20 29 3b 0a 20  ITE == 0x02 );. 
3ed7e 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 5f   assert( SQLITE_
3ed7f 4f 50 45 4e 5f 43 52 45 41 54 45 20 20 20 20 3d  OPEN_CREATE    =
3ed80 3d 20 30 78 30 34 20 29 3b 0a 20 20 74 65 73 74  = 0x04 );.  test
3ed81 63 61 73 65 28 20 28 31 3c 3c 28 66 6c 61 67 73  case( (1<<(flags
3ed82 26 37 29 29 3d 3d 30 78 30 32 20 29 3b 20 2f 2a  &7))==0x02 ); /*
3ed83 20 52 45 41 44 4f 4e 4c 59 20 2a 2f 0a 20 20 74   READONLY */.  t
3ed84 65 73 74 63 61 73 65 28 20 28 31 3c 3c 28 66 6c  estcase( (1<<(fl
3ed85 61 67 73 26 37 29 29 3d 3d 30 78 30 34 20 29 3b  ags&7))==0x04 );
3ed86 20 2f 2a 20 52 45 41 44 57 52 49 54 45 20 2a 2f   /* READWRITE */
3ed87 0a 20 20 74 65 73 74 63 61 73 65 28 20 28 31 3c  .  testcase( (1<
3ed88 3c 28 66 6c 61 67 73 26 37 29 29 3d 3d 30 78 34  <(flags&7))==0x4
3ed89 30 20 29 3b 20 2f 2a 20 52 45 41 44 57 52 49 54  0 ); /* READWRIT
3ed8a 45 20 7c 20 43 52 45 41 54 45 20 2a 2f 0a 20 20  E | CREATE */.  
3ed8b 69 66 28 20 28 28 31 3c 3c 28 66 6c 61 67 73 26  if( ((1<<(flags&
3ed8c 37 29 29 20 26 20 30 78 34 36 29 3d 3d 30 20 29  7)) & 0x46)==0 )
3ed8d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
3ed8e 49 53 55 53 45 5f 42 4b 50 54 3b 0a 0a 20 20 69  ISUSE_BKPT;..  i
3ed8f 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
3ed90 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65  Config.bCoreMute
3ed91 78 3d 3d 30 20 29 7b 0a 20 20 20 20 69 73 54 68  x==0 ){.    isTh
3ed92 72 65 61 64 73 61 66 65 20 3d 20 30 3b 0a 20 20  readsafe = 0;.  
3ed93 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20  }else if( flags 
3ed94 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f  & SQLITE_OPEN_NO
3ed95 4d 55 54 45 58 20 29 7b 0a 20 20 20 20 69 73 54  MUTEX ){.    isT
3ed96 68 72 65 61 64 73 61 66 65 20 3d 20 30 3b 0a 20  hreadsafe = 0;. 
3ed97 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73   }else if( flags
3ed98 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46   & SQLITE_OPEN_F
3ed99 55 4c 4c 4d 55 54 45 58 20 29 7b 0a 20 20 20 20  ULLMUTEX ){.    
3ed9a 69 73 54 68 72 65 61 64 73 61 66 65 20 3d 20 31  isThreadsafe = 1
3ed9b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
3ed9c 73 54 68 72 65 61 64 73 61 66 65 20 3d 20 73 71  sThreadsafe = sq
3ed9d 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3ed9e 67 2e 62 46 75 6c 6c 4d 75 74 65 78 3b 0a 20 20  g.bFullMutex;.  
3ed9f 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  }.  if( flags & 
3eda0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56  SQLITE_OPEN_PRIV
3eda1 41 54 45 43 41 43 48 45 20 29 7b 0a 20 20 20 20  ATECACHE ){.    
3eda2 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45  flags &= ~SQLITE
3eda3 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48  _OPEN_SHAREDCACH
3eda4 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  E;.  }else if( s
3eda5 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
3eda6 69 67 2e 73 68 61 72 65 64 43 61 63 68 65 45 6e  ig.sharedCacheEn
3eda7 61 62 6c 65 64 20 29 7b 0a 20 20 20 20 66 6c 61  abled ){.    fla
3eda8 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45  gs |= SQLITE_OPE
3eda9 4e 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20  N_SHAREDCACHE;. 
3edaa 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20   }..  /* Remove 
3edab 68 61 72 6d 66 75 6c 20 62 69 74 73 20 66 72 6f  harmful bits fro
3edac 6d 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61  m the flags para
3edad 6d 65 74 65 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20  meter.  **.  ** 
3edae 54 68 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  The SQLITE_OPEN_
3edaf 4e 4f 4d 55 54 45 58 20 61 6e 64 20 53 51 4c 49  NOMUTEX and SQLI
3edb0 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45  TE_OPEN_FULLMUTE
3edb1 58 20 66 6c 61 67 73 20 77 65 72 65 0a 20 20 2a  X flags were.  *
3edb2 2a 20 64 65 61 6c 74 20 77 69 74 68 20 69 6e 20  * dealt with in 
3edb3 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 6f 64  the previous cod
3edb4 65 20 62 6c 6f 63 6b 2e 20 20 42 65 73 69 64 65  e block.  Beside
3edb5 73 20 74 68 65 73 65 2c 20 74 68 65 20 6f 6e 6c  s these, the onl
3edb6 79 0a 20 20 2a 2a 20 76 61 6c 69 64 20 69 6e 70  y.  ** valid inp
3edb7 75 74 20 66 6c 61 67 73 20 66 6f 72 20 73 71 6c  ut flags for sql
3edb8 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 61  ite3_open_v2() a
3edb9 72 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  re SQLITE_OPEN_R
3edba 45 41 44 4f 4e 4c 59 2c 0a 20 20 2a 2a 20 53 51  EADONLY,.  ** SQ
3edbb 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
3edbc 49 54 45 2c 20 53 51 4c 49 54 45 5f 4f 50 45 4e  ITE, SQLITE_OPEN
3edbd 5f 43 52 45 41 54 45 2c 20 53 51 4c 49 54 45 5f  _CREATE, SQLITE_
3edbe 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45  OPEN_SHAREDCACHE
3edbf 2c 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 50  ,.  ** SQLITE_OP
3edc0 45 4e 5f 50 52 49 56 41 54 45 43 41 43 48 45 2c  EN_PRIVATECACHE,
3edc1 20 61 6e 64 20 73 6f 6d 65 20 72 65 73 65 72 76   and some reserv
3edc2 65 64 20 62 69 74 73 2e 20 20 53 69 6c 65 6e 74  ed bits.  Silent
3edc3 6c 79 20 6d 61 73 6b 0a 20 20 2a 2a 20 6f 66 66  ly mask.  ** off
3edc4 20 61 6c 6c 20 6f 74 68 65 72 20 66 6c 61 67 73   all other flags
3edc5 2e 0a 20 20 2a 2f 0a 20 20 66 6c 61 67 73 20 26  ..  */.  flags &
3edc6 3d 20 20 7e 28 20 53 51 4c 49 54 45 5f 4f 50 45  =  ~( SQLITE_OPE
3edc7 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20  N_DELETEONCLOSE 
3edc8 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  |.              
3edc9 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43   SQLITE_OPEN_EXC
3edca 4c 55 53 49 56 45 20 7c 0a 20 20 20 20 20 20 20  LUSIVE |.       
3edcb 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
3edcc 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 7c 0a 20 20  PEN_MAIN_DB |.  
3edcd 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
3edce 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42  ITE_OPEN_TEMP_DB
3edcf 20 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20   | .            
3edd0 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54     SQLITE_OPEN_T
3edd1 52 41 4e 53 49 45 4e 54 5f 44 42 20 7c 20 0a 20  RANSIENT_DB | . 
3edd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
3edd3 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a  LITE_OPEN_MAIN_J
3edd4 4f 55 52 4e 41 4c 20 7c 20 0a 20 20 20 20 20 20  OURNAL | .      
3edd5 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
3edd6 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41  OPEN_TEMP_JOURNA
3edd7 4c 20 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20  L | .           
3edd8 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
3edd9 53 55 42 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20  SUBJOURNAL | .  
3edda 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
3eddb 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f  ITE_OPEN_MASTER_
3eddc 4a 4f 55 52 4e 41 4c 20 7c 0a 20 20 20 20 20 20  JOURNAL |.      
3eddd 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
3edde 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 7c 0a 20  OPEN_NOMUTEX |. 
3eddf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
3ede0 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55  LITE_OPEN_FULLMU
3ede1 54 45 58 20 7c 0a 20 20 20 20 20 20 20 20 20 20  TEX |.          
3ede2 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
3ede3 5f 57 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20  _WAL.           
3ede4 20 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63    );..  /* Alloc
3ede5 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 20 64  ate the sqlite d
3ede6 61 74 61 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ata structure */
3ede7 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33 4d  .  db = sqlite3M
3ede8 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
3ede9 66 28 73 71 6c 69 74 65 33 29 20 29 3b 0a 20 20  f(sqlite3) );.  
3edea 69 66 28 20 64 62 3d 3d 30 20 29 20 67 6f 74 6f  if( db==0 ) goto
3edeb 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 69   opendb_out;.  i
3edec 66 28 20 69 73 54 68 72 65 61 64 73 61 66 65 20  f( isThreadsafe 
3eded 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 75 74 65 78  ){.    db->mutex
3edee 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
3edef 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
3edf0 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 20  X_RECURSIVE);.  
3edf1 20 20 69 66 28 20 64 62 2d 3e 6d 75 74 65 78 3d    if( db->mutex=
3edf2 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
3edf3 74 65 33 5f 66 72 65 65 28 64 62 29 3b 0a 20 20  te3_free(db);.  
3edf4 20 20 20 20 64 62 20 3d 20 30 3b 0a 20 20 20 20      db = 0;.    
3edf5 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75    goto opendb_ou
3edf6 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  t;.    }.  }.  s
3edf7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
3edf8 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
3edf9 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 30   db->errMask = 0
3edfa 78 66 66 3b 0a 20 20 64 62 2d 3e 6e 44 62 20 3d  xff;.  db->nDb =
3edfb 20 32 3b 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20   2;.  db->magic 
3edfc 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42  = SQLITE_MAGIC_B
3edfd 55 53 59 3b 0a 20 20 64 62 2d 3e 61 44 62 20 3d  USY;.  db->aDb =
3edfe 20 64 62 2d 3e 61 44 62 53 74 61 74 69 63 3b 0a   db->aDbStatic;.
3edff 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
3ee00 66 28 64 62 2d 3e 61 4c 69 6d 69 74 29 3d 3d 73  f(db->aLimit)==s
3ee01 69 7a 65 6f 66 28 61 48 61 72 64 4c 69 6d 69 74  izeof(aHardLimit
3ee02 29 20 29 3b 0a 20 20 6d 65 6d 63 70 79 28 64 62  ) );.  memcpy(db
3ee03 2d 3e 61 4c 69 6d 69 74 2c 20 61 48 61 72 64 4c  ->aLimit, aHardL
3ee04 69 6d 69 74 2c 20 73 69 7a 65 6f 66 28 64 62 2d  imit, sizeof(db-
3ee05 3e 61 4c 69 6d 69 74 29 29 3b 0a 20 20 64 62 2d  >aLimit));.  db-
3ee06 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b  >autoCommit = 1;
3ee07 0a 20 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76  .  db->nextAutov
3ee08 61 63 20 3d 20 2d 31 3b 0a 20 20 64 62 2d 3e 6e  ac = -1;.  db->n
3ee09 65 78 74 50 61 67 65 73 69 7a 65 20 3d 20 30 3b  extPagesize = 0;
3ee0a 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20  .  db->flags |= 
3ee0b 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e  SQLITE_ShortColN
3ee0c 61 6d 65 73 20 7c 20 53 51 4c 49 54 45 5f 41 75  ames | SQLITE_Au
3ee0d 74 6f 49 6e 64 65 78 20 7c 20 53 51 4c 49 54 45  toIndex | SQLITE
3ee0e 5f 45 6e 61 62 6c 65 54 72 69 67 67 65 72 0a 23  _EnableTrigger.#
3ee0f 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  if SQLITE_DEFAUL
3ee10 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 3c 34 0a  T_FILE_FORMAT<4.
3ee11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ee12 20 7c 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79   | SQLITE_Legacy
3ee13 46 69 6c 65 46 6d 74 0a 23 65 6e 64 69 66 0a 23  FileFmt.#endif.#
3ee14 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
3ee15 42 4c 45 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  BLE_LOAD_EXTENSI
3ee16 4f 4e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ON.             
3ee17 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 4c 6f 61      | SQLITE_Loa
3ee18 64 45 78 74 65 6e 73 69 6f 6e 0a 23 65 6e 64 69  dExtension.#endi
3ee19 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46  f.#if SQLITE_DEF
3ee1a 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45 5f 54  AULT_RECURSIVE_T
3ee1b 52 49 47 47 45 52 53 0a 20 20 20 20 20 20 20 20  RIGGERS.        
3ee1c 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54           | SQLIT
3ee1d 45 5f 52 65 63 54 72 69 67 67 65 72 73 0a 23 65  E_RecTriggers.#e
3ee1e 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64  ndif.#if defined
3ee1f 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f  (SQLITE_DEFAULT_
3ee20 46 4f 52 45 49 47 4e 5f 4b 45 59 53 29 20 26 26  FOREIGN_KEYS) &&
3ee21 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
3ee22 46 4f 52 45 49 47 4e 5f 4b 45 59 53 0a 20 20 20  FOREIGN_KEYS.   
3ee23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
3ee24 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65  SQLITE_ForeignKe
3ee25 79 73 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  ys.#endif.      
3ee26 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 49  ;.  sqlite3HashI
3ee27 6e 69 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  nit(&db->aCollSe
3ee28 71 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  q);.#ifndef SQLI
3ee29 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
3ee2a 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 48 61  ABLE.  sqlite3Ha
3ee2b 73 68 49 6e 69 74 28 26 64 62 2d 3e 61 4d 6f 64  shInit(&db->aMod
3ee2c 75 6c 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  ule);.#endif..  
3ee2d 2f 2a 20 41 64 64 20 74 68 65 20 64 65 66 61 75  /* Add the defau
3ee2e 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  lt collation seq
3ee2f 75 65 6e 63 65 20 42 49 4e 41 52 59 2e 20 42 49  uence BINARY. BI
3ee30 4e 41 52 59 20 77 6f 72 6b 73 20 66 6f 72 20 62  NARY works for b
3ee31 6f 74 68 20 55 54 46 2d 38 0a 20 20 2a 2a 20 61  oth UTF-8.  ** a
3ee32 6e 64 20 55 54 46 2d 31 36 2c 20 73 6f 20 61 64  nd UTF-16, so ad
3ee33 64 20 61 20 76 65 72 73 69 6f 6e 20 66 6f 72 20  d a version for 
3ee34 65 61 63 68 20 74 6f 20 61 76 6f 69 64 20 61 6e  each to avoid an
3ee35 79 20 75 6e 6e 65 63 65 73 73 61 72 79 0a 20 20  y unnecessary.  
3ee36 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20  ** conversions. 
3ee37 54 68 65 20 6f 6e 6c 79 20 65 72 72 6f 72 20 74  The only error t
3ee38 68 61 74 20 63 61 6e 20 6f 63 63 75 72 20 68 65  hat can occur he
3ee39 72 65 20 69 73 20 61 20 6d 61 6c 6c 6f 63 28 29  re is a malloc()
3ee3a 20 66 61 69 6c 75 72 65 2e 0a 20 20 2a 2f 0a 20   failure..  */. 
3ee3b 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e   createCollation
3ee3c 28 64 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53  (db, "BINARY", S
3ee3d 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 62  QLITE_UTF8, 0, b
3ee3e 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a  inCollFunc, 0);.
3ee3f 20 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f    createCollatio
3ee40 6e 28 64 62 2c 20 22 42 49 4e 41 52 59 22 2c 20  n(db, "BINARY", 
3ee41 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20  SQLITE_UTF16BE, 
3ee42 30 2c 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20  0, binCollFunc, 
3ee43 30 29 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c  0);.  createColl
3ee44 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52  ation(db, "BINAR
3ee45 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  Y", SQLITE_UTF16
3ee46 4c 45 2c 20 30 2c 20 62 69 6e 43 6f 6c 6c 46 75  LE, 0, binCollFu
3ee47 6e 63 2c 20 30 29 3b 0a 20 20 63 72 65 61 74 65  nc, 0);.  create
3ee48 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 52  Collation(db, "R
3ee49 54 52 49 4d 22 2c 20 53 51 4c 49 54 45 5f 55 54  TRIM", SQLITE_UT
3ee4a 46 38 2c 20 28 76 6f 69 64 2a 29 31 2c 20 62 69  F8, (void*)1, bi
3ee4b 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20  nCollFunc, 0);. 
3ee4c 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
3ee4d 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74  ailed ){.    got
3ee4e 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20  o opendb_out;.  
3ee4f 7d 0a 20 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  }.  db->pDfltCol
3ee50 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43  l = sqlite3FindC
3ee51 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c 49 54  ollSeq(db, SQLIT
3ee52 45 5f 55 54 46 38 2c 20 22 42 49 4e 41 52 59 22  E_UTF8, "BINARY"
3ee53 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20  , 0);.  assert( 
3ee54 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 21 3d 30  db->pDfltColl!=0
3ee55 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 73 6f 20 61   );..  /* Also a
3ee56 64 64 20 61 20 55 54 46 2d 38 20 63 61 73 65 2d  dd a UTF-8 case-
3ee57 69 6e 73 65 6e 73 69 74 69 76 65 20 63 6f 6c 6c  insensitive coll
3ee58 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 20  ation sequence. 
3ee59 2a 2f 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61  */.  createColla
3ee5a 74 69 6f 6e 28 64 62 2c 20 22 4e 4f 43 41 53 45  tion(db, "NOCASE
3ee5b 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  ", SQLITE_UTF8, 
3ee5c 30 2c 20 6e 6f 63 61 73 65 43 6f 6c 6c 61 74 69  0, nocaseCollati
3ee5d 6e 67 46 75 6e 63 2c 20 30 29 3b 0a 0a 20 20 2f  ngFunc, 0);..  /
3ee5e 2a 20 50 61 72 73 65 20 74 68 65 20 66 69 6c 65  * Parse the file
3ee5f 6e 61 6d 65 2f 55 52 49 20 61 72 67 75 6d 65 6e  name/URI argumen
3ee60 74 2e 20 2a 2f 0a 20 20 64 62 2d 3e 6f 70 65 6e  t. */.  db->open
3ee61 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20  Flags = flags;. 
3ee62 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 72   rc = sqlite3Par
3ee63 73 65 55 72 69 28 7a 56 66 73 2c 20 7a 46 69 6c  seUri(zVfs, zFil
3ee64 65 6e 61 6d 65 2c 20 26 66 6c 61 67 73 2c 20 26  ename, &flags, &
3ee65 64 62 2d 3e 70 56 66 73 2c 20 26 7a 4f 70 65 6e  db->pVfs, &zOpen
3ee66 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20 69  , &zErrMsg);.  i
3ee67 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3ee68 20 29 7b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   ){.    if( rc==
3ee69 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 20 64  SQLITE_NOMEM ) d
3ee6a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
3ee6b 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 1;.    sqlite3
3ee6c 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 7a 45  Error(db, rc, zE
3ee6d 72 72 4d 73 67 20 3f 20 22 25 73 22 20 3a 20 30  rrMsg ? "%s" : 0
3ee6e 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  , zErrMsg);.    
3ee6f 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72  sqlite3_free(zEr
3ee70 72 4d 73 67 29 3b 0a 20 20 20 20 67 6f 74 6f 20  rMsg);.    goto 
3ee71 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a  opendb_out;.  }.
3ee72 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 62  .  /* Open the b
3ee73 61 63 6b 65 6e 64 20 64 61 74 61 62 61 73 65 20  ackend database 
3ee74 64 72 69 76 65 72 20 2a 2f 0a 20 20 72 63 20 3d  driver */.  rc =
3ee75 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65   sqlite3BtreeOpe
3ee76 6e 28 64 62 2d 3e 70 56 66 73 2c 20 7a 4f 70 65  n(db->pVfs, zOpe
3ee77 6e 2c 20 64 62 2c 20 26 64 62 2d 3e 61 44 62 5b  n, db, &db->aDb[
3ee78 30 5d 2e 70 42 74 2c 20 30 2c 0a 20 20 20 20 20  0].pBt, 0,.     
3ee79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ee7a 20 20 20 66 6c 61 67 73 20 7c 20 53 51 4c 49 54     flags | SQLIT
3ee7b 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 3b  E_OPEN_MAIN_DB);
3ee7c 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
3ee7d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
3ee7e 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
3ee7f 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
3ee80 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
3ee81 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  M;.    }.    sql
3ee82 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63  ite3Error(db, rc
3ee83 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6f  , 0);.    goto o
3ee84 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 20  pendb_out;.  }. 
3ee85 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 68   db->aDb[0].pSch
3ee86 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 53 63 68  ema = sqlite3Sch
3ee87 65 6d 61 47 65 74 28 64 62 2c 20 64 62 2d 3e 61  emaGet(db, db->a
3ee88 44 62 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 64 62  Db[0].pBt);.  db
3ee89 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61  ->aDb[1].pSchema
3ee8a 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
3ee8b 47 65 74 28 64 62 2c 20 30 29 3b 0a 0a 0a 20 20  Get(db, 0);...  
3ee8c 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 73  /* The default s
3ee8d 61 66 65 74 79 5f 6c 65 76 65 6c 20 66 6f 72 20  afety_level for 
3ee8e 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
3ee8f 65 20 69 73 20 27 66 75 6c 6c 27 3b 20 66 6f 72  e is 'full'; for
3ee90 20 74 68 65 20 74 65 6d 70 0a 20 20 2a 2a 20 64   the temp.  ** d
3ee91 61 74 61 62 61 73 65 20 69 74 20 69 73 20 27 4e  atabase it is 'N
3ee92 4f 4e 45 27 2e 20 54 68 69 73 20 6d 61 74 63 68  ONE'. This match
3ee93 65 73 20 74 68 65 20 70 61 67 65 72 20 6c 61 79  es the pager lay
3ee94 65 72 20 64 65 66 61 75 6c 74 73 2e 20 20 0a 20  er defaults.  . 
3ee95 20 2a 2f 0a 20 20 64 62 2d 3e 61 44 62 5b 30 5d   */.  db->aDb[0]
3ee96 2e 7a 4e 61 6d 65 20 3d 20 22 6d 61 69 6e 22 3b  .zName = "main";
3ee97 0a 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 73 61  .  db->aDb[0].sa
3ee98 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 33 3b 0a  fety_level = 3;.
3ee99 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61    db->aDb[1].zNa
3ee9a 6d 65 20 3d 20 22 74 65 6d 70 22 3b 0a 20 20 64  me = "temp";.  d
3ee9b 62 2d 3e 61 44 62 5b 31 5d 2e 73 61 66 65 74 79  b->aDb[1].safety
3ee9c 5f 6c 65 76 65 6c 20 3d 20 31 3b 0a 0a 20 20 64  _level = 1;..  d
3ee9d 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54  b->magic = SQLIT
3ee9e 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a 20 20  E_MAGIC_OPEN;.  
3ee9f 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
3eea0 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
3eea1 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d   opendb_out;.  }
3eea2 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20  ..  /* Register 
3eea3 61 6c 6c 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e  all built-in fun
3eea4 63 74 69 6f 6e 73 2c 20 62 75 74 20 64 6f 20 6e  ctions, but do n
3eea5 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65  ot attempt to re
3eea6 61 64 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61  ad the.  ** data
3eea7 62 61 73 65 20 73 63 68 65 6d 61 20 79 65 74 2e  base schema yet.
3eea8 20 54 68 69 73 20 69 73 20 64 65 6c 61 79 65 64   This is delayed
3eea9 20 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74   until the first
3eeaa 20 74 69 6d 65 20 74 68 65 20 64 61 74 61 62 61   time the databa
3eeab 73 65 0a 20 20 2a 2a 20 69 73 20 61 63 63 65 73  se.  ** is acces
3eeac 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  sed..  */.  sqli
3eead 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
3eeae 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 73 71  ITE_OK, 0);.  sq
3eeaf 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 75 69  lite3RegisterBui
3eeb0 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 64 62  ltinFunctions(db
3eeb1 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 61 75  );..  /* Load au
3eeb2 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f  tomatic extensio
3eeb3 6e 73 20 2d 20 65 78 74 65 6e 73 69 6f 6e 73 20  ns - extensions 
3eeb4 74 68 61 74 20 68 61 76 65 20 62 65 65 6e 20 72  that have been r
3eeb5 65 67 69 73 74 65 72 65 64 0a 20 20 2a 2a 20 75  egistered.  ** u
3eeb6 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33  sing the sqlite3
3eeb7 5f 61 75 74 6f 6d 61 74 69 63 5f 65 78 74 65 6e  _automatic_exten
3eeb8 73 69 6f 6e 28 29 20 41 50 49 2e 0a 20 20 2a 2f  sion() API..  */
3eeb9 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
3eeba 65 72 72 63 6f 64 65 28 64 62 29 3b 0a 20 20 69  errcode(db);.  i
3eebb 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
3eebc 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41   ){.    sqlite3A
3eebd 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  utoLoadExtension
3eebe 73 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20  s(db);.    rc = 
3eebf 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
3eec0 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  db);.    if( rc!
3eec1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
3eec2 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f      goto opendb_
3eec3 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  out;.    }.  }..
3eec4 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
3eec5 41 42 4c 45 5f 46 54 53 31 0a 20 20 69 66 28 20  ABLE_FTS1.  if( 
3eec6 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
3eec7 64 20 29 7b 0a 20 20 20 20 65 78 74 65 72 6e 20  d ){.    extern 
3eec8 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 31 49  int sqlite3Fts1I
3eec9 6e 69 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  nit(sqlite3*);. 
3eeca 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46     rc = sqlite3F
3eecb 74 73 31 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d  ts1Init(db);.  }
3eecc 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
3eecd 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
3eece 53 32 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61  S2.  if( !db->ma
3eecf 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63  llocFailed && rc
3eed0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3eed1 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71     extern int sq
3eed2 6c 69 74 65 33 46 74 73 32 49 6e 69 74 28 73 71  lite3Fts2Init(sq
3eed3 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 72 63 20  lite3*);.    rc 
3eed4 3d 20 73 71 6c 69 74 65 33 46 74 73 32 49 6e 69  = sqlite3Fts2Ini
3eed5 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  t(db);.  }.#endi
3eed6 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
3eed7 5f 45 4e 41 42 4c 45 5f 46 54 53 33 0a 20 20 69  _ENABLE_FTS3.  i
3eed8 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  f( !db->mallocFa
3eed9 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49  iled && rc==SQLI
3eeda 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
3eedb 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  = sqlite3Fts3Ini
3eedc 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  t(db);.  }.#endi
3eedd 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
3eede 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 20 20 69 66  _ENABLE_ICU.  if
3eedf 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
3eee0 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  led && rc==SQLIT
3eee1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  E_OK ){.    rc =
3eee2 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28   sqlite3IcuInit(
3eee3 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  db);.  }.#endif.
3eee4 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
3eee5 4e 41 42 4c 45 5f 52 54 52 45 45 0a 20 20 69 66  NABLE_RTREE.  if
3eee6 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
3eee7 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  led && rc==SQLIT
3eee8 45 5f 4f 4b 29 7b 0a 20 20 20 20 72 63 20 3d 20  E_OK){.    rc = 
3eee9 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74  sqlite3RtreeInit
3eeea 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  (db);.  }.#endif
3eeeb 0a 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  ..  sqlite3Error
3eeec 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a 0a 20 20  (db, rc, 0);..  
3eeed 2f 2a 20 2d 44 53 51 4c 49 54 45 5f 44 45 46 41  /* -DSQLITE_DEFA
3eeee 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45  ULT_LOCKING_MODE
3eeef 3d 31 20 6d 61 6b 65 73 20 45 58 43 4c 55 53 49  =1 makes EXCLUSI
3eef0 56 45 20 74 68 65 20 64 65 66 61 75 6c 74 20 6c  VE the default l
3eef1 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 6d 6f 64 65  ocking.  ** mode
3eef2 2e 20 20 2d 44 53 51 4c 49 54 45 5f 44 45 46 41  .  -DSQLITE_DEFA
3eef3 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45  ULT_LOCKING_MODE
3eef4 3d 30 20 6d 61 6b 65 20 4e 4f 52 4d 41 4c 20 74  =0 make NORMAL t
3eef5 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69  he default locki
3eef6 6e 67 0a 20 20 2a 2a 20 6d 6f 64 65 2e 20 20 44  ng.  ** mode.  D
3eef7 6f 69 6e 67 20 6e 6f 74 68 69 6e 67 20 61 74 20  oing nothing at 
3eef8 61 6c 6c 20 61 6c 73 6f 20 6d 61 6b 65 73 20 4e  all also makes N
3eef9 4f 52 4d 41 4c 20 74 68 65 20 64 65 66 61 75 6c  ORMAL the defaul
3eefa 74 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  t..  */.#ifdef S
3eefb 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f  QLITE_DEFAULT_LO
3eefc 43 4b 49 4e 47 5f 4d 4f 44 45 0a 20 20 64 62 2d  CKING_MODE.  db-
3eefd 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 3d 20  >dfltLockMode = 
3eefe 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c  SQLITE_DEFAULT_L
3eeff 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3b 0a 20 20 73  OCKING_MODE;.  s
3ef00 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69  qlite3PagerLocki
3ef01 6e 67 4d 6f 64 65 28 73 71 6c 69 74 65 33 42 74  ngMode(sqlite3Bt
3ef02 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62  reePager(db->aDb
3ef03 5b 30 5d 2e 70 42 74 29 2c 0a 20 20 20 20 20 20  [0].pBt),.      
3ef04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ef05 20 20 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55      SQLITE_DEFAU
3ef06 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 29  LT_LOCKING_MODE)
3ef07 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45  ;.#endif..  /* E
3ef08 6e 61 62 6c 65 20 74 68 65 20 6c 6f 6f 6b 61 73  nable the lookas
3ef09 69 64 65 2d 6d 61 6c 6c 6f 63 20 73 75 62 73 79  ide-malloc subsy
3ef0a 73 74 65 6d 20 2a 2f 0a 20 20 73 65 74 75 70 4c  stem */.  setupL
3ef0b 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 30 2c 20  ookaside(db, 0, 
3ef0c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3ef0d 66 69 67 2e 73 7a 4c 6f 6f 6b 61 73 69 64 65 2c  fig.szLookaside,
3ef0e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3ef0f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
3ef10 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 4c 6f  GlobalConfig.nLo
3ef11 6f 6b 61 73 69 64 65 29 3b 0a 0a 20 20 73 71 6c  okaside);..  sql
3ef12 69 74 65 33 5f 77 61 6c 5f 61 75 74 6f 63 68 65  ite3_wal_autoche
3ef13 63 6b 70 6f 69 6e 74 28 64 62 2c 20 53 51 4c 49  ckpoint(db, SQLI
3ef14 54 45 5f 44 45 46 41 55 4c 54 5f 57 41 4c 5f 41  TE_DEFAULT_WAL_A
3ef15 55 54 4f 43 48 45 43 4b 50 4f 49 4e 54 29 3b 0a  UTOCHECKPOINT);.
3ef16 0a 6f 70 65 6e 64 62 5f 6f 75 74 3a 0a 20 20 73  .opendb_out:.  s
3ef17 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4f 70 65  qlite3_free(zOpe
3ef18 6e 29 3b 0a 20 20 69 66 28 20 64 62 20 29 7b 0a  n);.  if( db ){.
3ef19 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
3ef1a 6d 75 74 65 78 21 3d 30 20 7c 7c 20 69 73 54 68  mutex!=0 || isTh
3ef1b 72 65 61 64 73 61 66 65 3d 3d 30 20 7c 7c 20 73  readsafe==0 || s
3ef1c 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
3ef1d 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 3d 3d 30  ig.bFullMutex==0
3ef1e 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   );.    sqlite3_
3ef1f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
3ef20 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 63  mutex);.  }.  rc
3ef21 20 3d 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f   = sqlite3_errco
3ef22 64 65 28 64 62 29 3b 0a 20 20 61 73 73 65 72 74  de(db);.  assert
3ef23 28 20 64 62 21 3d 30 20 7c 7c 20 72 63 3d 3d 53  ( db!=0 || rc==S
3ef24 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
3ef25 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
3ef26 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c  NOMEM ){.    sql
3ef27 69 74 65 33 5f 63 6c 6f 73 65 28 64 62 29 3b 0a  ite3_close(db);.
3ef28 20 20 20 20 64 62 20 3d 20 30 3b 0a 20 20 7d 65      db = 0;.  }e
3ef29 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49  lse if( rc!=SQLI
3ef2a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 64 62 2d  TE_OK ){.    db-
3ef2b 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f  >magic = SQLITE_
3ef2c 4d 41 47 49 43 5f 53 49 43 4b 3b 0a 20 20 7d 0a  MAGIC_SICK;.  }.
3ef2d 20 20 2a 70 70 44 62 20 3d 20 64 62 3b 0a 23 69    *ppDb = db;.#i
3ef2e 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
3ef2f 4c 45 5f 53 51 4c 4c 4f 47 0a 20 20 69 66 28 20  LE_SQLLOG.  if( 
3ef30 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3ef31 66 69 67 2e 78 53 71 6c 6c 6f 67 20 29 7b 0a 20  fig.xSqllog ){. 
3ef32 20 20 20 2f 2a 20 4f 70 65 6e 69 6e 67 20 61 20     /* Opening a 
3ef33 64 62 20 68 61 6e 64 6c 65 2e 20 46 6f 75 72 74  db handle. Fourt
3ef34 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 70  h parameter is p
3ef35 61 73 73 65 64 20 30 2e 20 2a 2f 0a 20 20 20 20  assed 0. */.    
3ef36 76 6f 69 64 20 2a 70 41 72 67 20 3d 20 73 71 6c  void *pArg = sql
3ef37 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
3ef38 2e 70 53 71 6c 6c 6f 67 41 72 67 3b 0a 20 20 20  .pSqllogArg;.   
3ef39 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3ef3a 6e 66 69 67 2e 78 53 71 6c 6c 6f 67 28 70 41 72  nfig.xSqllog(pAr
3ef3b 67 2c 20 64 62 2c 20 7a 46 69 6c 65 6e 61 6d 65  g, db, zFilename
3ef3c 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  , 0);.  }.#endif
3ef3d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
3ef3e 33 41 70 69 45 78 69 74 28 30 2c 20 72 63 29 3b  3ApiExit(0, rc);
3ef3f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61  .}../*.** Open a
3ef40 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 68 61   new database ha
3ef41 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ndle..*/.SQLITE_
3ef42 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
3ef43 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68  open(.  const ch
3ef44 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 0a  ar *zFilename, .
3ef45 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62    sqlite3 **ppDb
3ef46 20 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 6f 70   .){.  return op
3ef47 65 6e 44 61 74 61 62 61 73 65 28 7a 46 69 6c 65  enDatabase(zFile
3ef48 6e 61 6d 65 2c 20 70 70 44 62 2c 0a 20 20 20 20  name, ppDb,.    
3ef49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ef4a 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45    SQLITE_OPEN_RE
3ef4b 41 44 57 52 49 54 45 20 7c 20 53 51 4c 49 54 45  ADWRITE | SQLITE
3ef4c 5f 4f 50 45 4e 5f 43 52 45 41 54 45 2c 20 30 29  _OPEN_CREATE, 0)
3ef4d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
3ef4e 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  nt sqlite3_open_
3ef4f 76 32 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  v2(.  const char
3ef50 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a   *filename,   /*
3ef51 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61   Database filena
3ef52 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20  me (UTF-8) */.  
3ef53 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20  sqlite3 **ppDb, 
3ef54 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
3ef55 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65  SQLite db handle
3ef56 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c   */.  int flags,
3ef57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3ef58 20 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73   Flags */.  cons
3ef59 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20  t char *zVfs    
3ef5a 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56      /* Name of V
3ef5b 46 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65  FS module to use
3ef5c 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20   */.){.  return 
3ef5d 6f 70 65 6e 44 61 74 61 62 61 73 65 28 66 69 6c  openDatabase(fil
3ef5e 65 6e 61 6d 65 2c 20 70 70 44 62 2c 20 28 75 6e  ename, ppDb, (un
3ef5f 73 69 67 6e 65 64 20 69 6e 74 29 66 6c 61 67 73  signed int)flags
3ef60 2c 20 7a 56 66 73 29 3b 0a 7d 0a 0a 23 69 66 6e  , zVfs);.}..#ifn
3ef61 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
3ef62 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e  UTF16./*.** Open
3ef63 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20   a new database 
3ef64 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  handle..*/.SQLIT
3ef65 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3ef66 33 5f 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73  3_open16(.  cons
3ef67 74 20 76 6f 69 64 20 2a 7a 46 69 6c 65 6e 61 6d  t void *zFilenam
3ef68 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a  e, .  sqlite3 **
3ef69 70 70 44 62 0a 29 7b 0a 20 20 63 68 61 72 20 63  ppDb.){.  char c
3ef6a 6f 6e 73 74 20 2a 7a 46 69 6c 65 6e 61 6d 65 38  onst *zFilename8
3ef6b 3b 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65  ;   /* zFilename
3ef6c 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d   encoded in UTF-
3ef6d 38 20 69 6e 73 74 65 61 64 20 6f 66 20 55 54 46  8 instead of UTF
3ef6e 2d 31 36 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  -16 */.  sqlite3
3ef6f 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20  _value *pVal;.  
3ef70 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
3ef71 74 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a  t( zFilename );.
3ef72 20 20 61 73 73 65 72 74 28 20 70 70 44 62 20 29    assert( ppDb )
3ef73 3b 0a 20 20 2a 70 70 44 62 20 3d 20 30 3b 0a 23  ;.  *ppDb = 0;.#
3ef74 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
3ef75 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63  IT_AUTOINIT.  rc
3ef76 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69   = sqlite3_initi
3ef77 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72  alize();.  if( r
3ef78 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23  c ) return rc;.#
3ef79 65 6e 64 69 66 0a 20 20 70 56 61 6c 20 3d 20 73  endif.  pVal = s
3ef7a 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 30  qlite3ValueNew(0
3ef7b 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75  );.  sqlite3Valu
3ef7c 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31  eSetStr(pVal, -1
3ef7d 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 53 51 4c  , zFilename, SQL
3ef7e 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c  ITE_UTF16NATIVE,
3ef7f 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
3ef80 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 38 20 3d 20  .  zFilename8 = 
3ef81 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
3ef82 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54  (pVal, SQLITE_UT
3ef83 46 38 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65  F8);.  if( zFile
3ef84 6e 61 6d 65 38 20 29 7b 0a 20 20 20 20 72 63 20  name8 ){.    rc 
3ef85 3d 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28 7a  = openDatabase(z
3ef86 46 69 6c 65 6e 61 6d 65 38 2c 20 70 70 44 62 2c  Filename8, ppDb,
3ef87 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3ef88 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
3ef89 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53  EN_READWRITE | S
3ef8a 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
3ef8b 45 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72  E, 0);.    asser
3ef8c 74 28 20 2a 70 70 44 62 20 7c 7c 20 72 63 3d 3d  t( *ppDb || rc==
3ef8d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a  SQLITE_NOMEM );.
3ef8e 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
3ef8f 54 45 5f 4f 4b 20 26 26 20 21 44 62 48 61 73 50  TE_OK && !DbHasP
3ef90 72 6f 70 65 72 74 79 28 2a 70 70 44 62 2c 20 30  roperty(*ppDb, 0
3ef91 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65  , DB_SchemaLoade
3ef92 64 29 20 29 7b 0a 20 20 20 20 20 20 45 4e 43 28  d) ){.      ENC(
3ef93 2a 70 70 44 62 29 20 3d 20 53 51 4c 49 54 45 5f  *ppDb) = SQLITE_
3ef94 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 20  UTF16NATIVE;.   
3ef95 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
3ef96 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
3ef97 4d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  M;.  }.  sqlite3
3ef98 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b  ValueFree(pVal);
3ef99 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
3ef9a 65 33 41 70 69 45 78 69 74 28 30 2c 20 72 63 29  e3ApiExit(0, rc)
3ef9b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
3ef9c 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
3ef9d 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  */../*.** Regist
3ef9e 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69  er a new collati
3ef9f 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74 68  on sequence with
3efa0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
3efa1 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49  ndle db..*/.SQLI
3efa2 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3efa3 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
3efa4 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 20  ion(.  sqlite3* 
3efa5 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  db, .  const cha
3efa6 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74  r *zName, .  int
3efa7 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70   enc, .  void* p
3efa8 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d  Ctx,.  int(*xCom
3efa9 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  pare)(void*,int,
3efaa 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
3efab 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a  const void*).){.
3efac 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
3efad 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
3efae 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73  db->mutex);.  as
3efaf 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
3efb0 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 63 20  cFailed );.  rc 
3efb1 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f  = createCollatio
3efb2 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 28 75 38  n(db, zName, (u8
3efb3 29 65 6e 63 2c 20 70 43 74 78 2c 20 78 43 6f 6d  )enc, pCtx, xCom
3efb4 70 61 72 65 2c 20 30 29 3b 0a 20 20 72 63 20 3d  pare, 0);.  rc =
3efb5 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
3efb6 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74  db, rc);.  sqlit
3efb7 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
3efb8 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
3efb9 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
3efba 20 52 65 67 69 73 74 65 72 20 61 20 6e 65 77 20   Register a new 
3efbb 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
3efbc 63 65 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ce with the data
3efbd 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 0a  base handle db..
3efbe 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
3efbf 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
3efc0 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a 20  _collation_v2(. 
3efc1 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a 20   sqlite3* db, . 
3efc2 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
3efc3 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e 63 2c 20  me, .  int enc, 
3efc4 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20  .  void* pCtx,. 
3efc5 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28   int(*xCompare)(
3efc6 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
3efc7 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
3efc8 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a  void*),.  void(*
3efc9 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a  xDel)(void*).){.
3efca 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
3efcb 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
3efcc 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73  db->mutex);.  as
3efcd 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
3efce 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 63 20  cFailed );.  rc 
3efcf 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f  = createCollatio
3efd0 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 28 75 38  n(db, zName, (u8
3efd1 29 65 6e 63 2c 20 70 43 74 78 2c 20 78 43 6f 6d  )enc, pCtx, xCom
3efd2 70 61 72 65 2c 20 78 44 65 6c 29 3b 0a 20 20 72  pare, xDel);.  r
3efd3 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
3efd4 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
3efd5 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
3efd6 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
3efd7 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69  return rc;.}..#i
3efd8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
3efd9 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65  T_UTF16./*.** Re
3efda 67 69 73 74 65 72 20 61 20 6e 65 77 20 63 6f 6c  gister a new col
3efdb 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
3efdc 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
3efdd 65 20 68 61 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a  e handle db..*/.
3efde 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3efdf 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
3efe0 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c  llation16(.  sql
3efe1 69 74 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e  ite3* db, .  con
3efe2 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a  st void *zName,.
3efe3 20 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f    int enc, .  vo
3efe4 69 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28  id* pCtx,.  int(
3efe5 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a  *xCompare)(void*
3efe6 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
3efe7 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
3efe8 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ).){.  int rc = 
3efe9 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 68 61  SQLITE_OK;.  cha
3efea 72 20 2a 7a 4e 61 6d 65 38 3b 0a 20 20 73 71 6c  r *zName8;.  sql
3efeb 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
3efec 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61  (db->mutex);.  a
3efed 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
3efee 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7a 4e  ocFailed );.  zN
3efef 61 6d 65 38 20 3d 20 73 71 6c 69 74 65 33 55 74  ame8 = sqlite3Ut
3eff0 66 31 36 74 6f 38 28 64 62 2c 20 7a 4e 61 6d 65  f16to8(db, zName
3eff1 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  , -1, SQLITE_UTF
3eff2 31 36 4e 41 54 49 56 45 29 3b 0a 20 20 69 66 28  16NATIVE);.  if(
3eff3 20 7a 4e 61 6d 65 38 20 29 7b 0a 20 20 20 20 72   zName8 ){.    r
3eff4 63 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74  c = createCollat
3eff5 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 38 2c 20  ion(db, zName8, 
3eff6 28 75 38 29 65 6e 63 2c 20 70 43 74 78 2c 20 78  (u8)enc, pCtx, x
3eff7 43 6f 6d 70 61 72 65 2c 20 30 29 3b 0a 20 20 20  Compare, 0);.   
3eff8 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
3eff9 62 2c 20 7a 4e 61 6d 65 38 29 3b 0a 20 20 7d 0a  b, zName8);.  }.
3effa 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
3effb 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
3effc 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
3effd 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
3effe 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
3efff 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
3f000 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
3f001 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
3f002 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
3f003 6e 63 65 20 66 61 63 74 6f 72 79 20 63 61 6c 6c  nce factory call
3f004 62 61 63 6b 20 77 69 74 68 20 74 68 65 20 64 61  back with the da
3f005 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a  tabase handle.**
3f006 20 64 62 2e 20 52 65 70 6c 61 63 65 20 61 6e 79   db. Replace any
3f007 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 74   previously inst
3f008 61 6c 6c 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  alled collation 
3f009 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f 72 79  sequence factory
3f00a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3f00b 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  int sqlite3_coll
3f00c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20  ation_needed(.  
3f00d 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
3f00e 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65  void *pCollNeede
3f00f 64 41 72 67 2c 20 0a 20 20 76 6f 69 64 28 2a 78  dArg, .  void(*x
3f010 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f 69 64  CollNeeded)(void
3f011 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
3f012 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68  TextRep,const ch
3f013 61 72 2a 29 0a 29 7b 0a 20 20 73 71 6c 69 74 65  ar*).){.  sqlite
3f014 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
3f015 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e  ->mutex);.  db->
3f016 78 43 6f 6c 6c 4e 65 65 64 65 64 20 3d 20 78 43  xCollNeeded = xC
3f017 6f 6c 6c 4e 65 65 64 65 64 3b 0a 20 20 64 62 2d  ollNeeded;.  db-
3f018 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 3d  >xCollNeeded16 =
3f019 20 30 3b 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c 4e   0;.  db->pCollN
3f01a 65 65 64 65 64 41 72 67 20 3d 20 70 43 6f 6c 6c  eededArg = pColl
3f01b 4e 65 65 64 65 64 41 72 67 3b 0a 20 20 73 71 6c  NeededArg;.  sql
3f01c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
3f01d 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
3f01e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
3f01f 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
3f020 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a  TE_OMIT_UTF16./*
3f021 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63  .** Register a c
3f022 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
3f023 65 20 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61  e factory callba
3f024 63 6b 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ck with the data
3f025 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64  base handle.** d
3f026 62 2e 20 52 65 70 6c 61 63 65 20 61 6e 79 20 70  b. Replace any p
3f027 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c  reviously instal
3f028 6c 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  led collation se
3f029 71 75 65 6e 63 65 20 66 61 63 74 6f 72 79 2e 0a  quence factory..
3f02a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
3f02b 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  t sqlite3_collat
3f02c 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 0a 20 20  ion_needed16(.  
3f02d 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
3f02e 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65  void *pCollNeede
3f02f 64 41 72 67 2c 20 0a 20 20 76 6f 69 64 28 2a 78  dArg, .  void(*x
3f030 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28 76 6f  CollNeeded16)(vo
3f031 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74  id*,sqlite3*,int
3f032 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20   eTextRep,const 
3f033 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 73 71 6c 69  void*).){.  sqli
3f034 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
3f035 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62  db->mutex);.  db
3f036 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 3d 20  ->xCollNeeded = 
3f037 30 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65  0;.  db->xCollNe
3f038 65 64 65 64 31 36 20 3d 20 78 43 6f 6c 6c 4e 65  eded16 = xCollNe
3f039 65 64 65 64 31 36 3b 0a 20 20 64 62 2d 3e 70 43  eded16;.  db->pC
3f03a 6f 6c 6c 4e 65 65 64 65 64 41 72 67 20 3d 20 70  ollNeededArg = p
3f03b 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20  CollNeededArg;. 
3f03c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
3f03d 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
3f03e 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3f03f 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  _OK;.}.#endif /*
3f040 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
3f041 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  16 */..#ifndef S
3f042 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
3f043 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 54 68 69 73  CATED./*.** This
3f044 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 77   function is now
3f045 20 61 6e 20 61 6e 61 63 68 72 6f 6e 69 73 6d 2e   an anachronism.
3f046 20 49 74 20 75 73 65 64 20 74 6f 20 62 65 20 75   It used to be u
3f047 73 65 64 20 74 6f 20 72 65 63 6f 76 65 72 20 66  sed to recover f
3f048 72 6f 6d 20 61 0a 2a 2a 20 6d 61 6c 6c 6f 63 28  rom a.** malloc(
3f049 29 20 66 61 69 6c 75 72 65 2c 20 62 75 74 20 53  ) failure, but S
3f04a 51 4c 69 74 65 20 6e 6f 77 20 64 6f 65 73 20 74  QLite now does t
3f04b 68 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  his automaticall
3f04c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  y..*/.SQLITE_API
3f04d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f   int sqlite3_glo
3f04e 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69 64  bal_recover(void
3f04f 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ){.  return SQLI
3f050 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  TE_OK;.}.#endif.
3f051 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73  ./*.** Test to s
3f052 65 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ee whether or no
3f053 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  t the database c
3f054 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20  onnection is in 
3f055 61 75 74 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 6d 6f  autocommit.** mo
3f056 64 65 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45  de.  Return TRUE
3f057 20 69 66 20 69 74 20 69 73 20 61 6e 64 20 46 41   if it is and FA
3f058 4c 53 45 20 69 66 20 6e 6f 74 2e 20 20 41 75 74  LSE if not.  Aut
3f059 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20  ocommit mode is 
3f05a 6f 6e 0a 2a 2a 20 62 79 20 64 65 66 61 75 6c 74  on.** by default
3f05b 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 69 73  .  Autocommit is
3f05c 20 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 42   disabled by a B
3f05d 45 47 49 4e 20 73 74 61 74 65 6d 65 6e 74 20 61  EGIN statement a
3f05e 6e 64 20 72 65 65 6e 61 62 6c 65 64 0a 2a 2a 20  nd reenabled.** 
3f05f 62 79 20 74 68 65 20 6e 65 78 74 20 43 4f 4d 4d  by the next COMM
3f060 49 54 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2e 0a  IT or ROLLBACK..
3f061 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 20 54 48 49 53 20  **.******* THIS 
3f062 49 53 20 41 4e 20 45 58 50 45 52 49 4d 45 4e 54  IS AN EXPERIMENT
3f063 41 4c 20 41 50 49 20 41 4e 44 20 49 53 20 53 55  AL API AND IS SU
3f064 42 4a 45 43 54 20 54 4f 20 43 48 41 4e 47 45 20  BJECT TO CHANGE 
3f065 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45  ******.*/.SQLITE
3f066 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3f067 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28  _get_autocommit(
3f068 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
3f069 72 65 74 75 72 6e 20 64 62 2d 3e 61 75 74 6f 43  return db->autoC
3f06a 6f 6d 6d 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ommit;.}../*.** 
3f06b 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
3f06c 75 74 69 6e 65 73 20 61 72 65 20 73 75 62 74 69  utines are subti
3f06d 74 75 74 65 73 20 66 6f 72 20 63 6f 6e 73 74 61  tutes for consta
3f06e 6e 74 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  nts SQLITE_CORRU
3f06f 50 54 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49  PT,.** SQLITE_MI
3f070 53 55 53 45 2c 20 53 51 4c 49 54 45 5f 43 41 4e  SUSE, SQLITE_CAN
3f071 54 4f 50 45 4e 2c 20 53 51 4c 49 54 45 5f 49 4f  TOPEN, SQLITE_IO
3f072 45 52 52 20 61 6e 64 20 70 6f 73 73 69 62 6c 79  ERR and possibly
3f073 20 6f 74 68 65 72 20 65 72 72 6f 72 0a 2a 2a 20   other error.** 
3f074 63 6f 6e 73 74 61 6e 74 73 2e 20 20 54 68 65 79  constants.  They
3f075 20 73 65 72 76 65 72 20 74 77 6f 20 70 75 72 70   server two purp
3f076 6f 73 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e  oses:.**.**   1.
3f077 20 20 53 65 72 76 65 20 61 73 20 61 20 63 6f 6e    Serve as a con
3f078 76 65 6e 69 65 6e 74 20 70 6c 61 63 65 20 74 6f  venient place to
3f079 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f 69 6e   set a breakpoin
3f07a 74 20 69 6e 20 61 20 64 65 62 75 67 67 65 72 0a  t in a debugger.
3f07b 2a 2a 20 20 20 20 20 20 20 74 6f 20 64 65 74 65  **       to dete
3f07c 63 74 20 77 68 65 6e 20 76 65 72 73 69 6f 6e 20  ct when version 
3f07d 65 72 72 6f 72 20 63 6f 6e 64 69 74 69 6f 6e 73  error conditions
3f07e 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 20   occurs..**.**  
3f07f 20 32 2e 20 20 49 6e 76 6f 6b 65 20 73 71 6c 69   2.  Invoke sqli
3f080 74 65 33 5f 6c 6f 67 28 29 20 74 6f 20 70 72 6f  te3_log() to pro
3f081 76 69 64 65 20 74 68 65 20 73 6f 75 72 63 65 20  vide the source 
3f082 63 6f 64 65 20 6c 6f 63 61 74 69 6f 6e 20 77 68  code location wh
3f083 65 72 65 0a 2a 2a 20 20 20 20 20 20 20 61 20 6c  ere.**       a l
3f084 6f 77 2d 6c 65 76 65 6c 20 65 72 72 6f 72 20 69  ow-level error i
3f085 73 20 66 69 72 73 74 20 64 65 74 65 63 74 65 64  s first detected
3f086 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
3f087 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43  ATE int sqlite3C
3f088 6f 72 72 75 70 74 45 72 72 6f 72 28 69 6e 74 20  orruptError(int 
3f089 6c 69 6e 65 6e 6f 29 7b 0a 20 20 74 65 73 74 63  lineno){.  testc
3f08a 61 73 65 28 20 73 71 6c 69 74 65 33 47 6c 6f 62  ase( sqlite3Glob
3f08b 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f 67 21 3d 30  alConfig.xLog!=0
3f08c 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6c 6f   );.  sqlite3_lo
3f08d 67 28 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  g(SQLITE_CORRUPT
3f08e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
3f08f 22 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70  "database corrup
3f090 74 69 6f 6e 20 61 74 20 6c 69 6e 65 20 25 64 20  tion at line %d 
3f091 6f 66 20 5b 25 2e 31 30 73 5d 22 2c 0a 20 20 20  of [%.10s]",.   
3f092 20 20 20 20 20 20 20 20 20 20 20 6c 69 6e 65 6e             linen
3f093 6f 2c 20 32 30 2b 73 71 6c 69 74 65 33 5f 73 6f  o, 20+sqlite3_so
3f094 75 72 63 65 69 64 28 29 29 3b 0a 20 20 72 65 74  urceid());.  ret
3f095 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
3f096 50 54 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  PT;.}.SQLITE_PRI
3f097 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
3f098 4d 69 73 75 73 65 45 72 72 6f 72 28 69 6e 74 20  MisuseError(int 
3f099 6c 69 6e 65 6e 6f 29 7b 0a 20 20 74 65 73 74 63  lineno){.  testc
3f09a 61 73 65 28 20 73 71 6c 69 74 65 33 47 6c 6f 62  ase( sqlite3Glob
3f09b 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f 67 21 3d 30  alConfig.xLog!=0
3f09c 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6c 6f   );.  sqlite3_lo
3f09d 67 28 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c  g(SQLITE_MISUSE,
3f09e 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
3f09f 22 6d 69 73 75 73 65 20 61 74 20 6c 69 6e 65 20  "misuse at line 
3f0a0 25 64 20 6f 66 20 5b 25 2e 31 30 73 5d 22 2c 0a  %d of [%.10s]",.
3f0a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 69                li
3f0a2 6e 65 6e 6f 2c 20 32 30 2b 73 71 6c 69 74 65 33  neno, 20+sqlite3
3f0a3 5f 73 6f 75 72 63 65 69 64 28 29 29 3b 0a 20 20  _sourceid());.  
3f0a4 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
3f0a5 53 55 53 45 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  SUSE;.}.SQLITE_P
3f0a6 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
3f0a7 65 33 43 61 6e 74 6f 70 65 6e 45 72 72 6f 72 28  e3CantopenError(
3f0a8 69 6e 74 20 6c 69 6e 65 6e 6f 29 7b 0a 20 20 74  int lineno){.  t
3f0a9 65 73 74 63 61 73 65 28 20 73 71 6c 69 74 65 33  estcase( sqlite3
3f0aa 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 78 4c 6f  GlobalConfig.xLo
3f0ab 67 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65  g!=0 );.  sqlite
3f0ac 33 5f 6c 6f 67 28 53 51 4c 49 54 45 5f 43 41 4e  3_log(SQLITE_CAN
3f0ad 54 4f 50 45 4e 2c 20 0a 20 20 20 20 20 20 20 20  TOPEN, .        
3f0ae 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 6f 70        "cannot op
3f0af 65 6e 20 66 69 6c 65 20 61 74 20 6c 69 6e 65 20  en file at line 
3f0b0 25 64 20 6f 66 20 5b 25 2e 31 30 73 5d 22 2c 0a  %d of [%.10s]",.
3f0b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 69                li
3f0b2 6e 65 6e 6f 2c 20 32 30 2b 73 71 6c 69 74 65 33  neno, 20+sqlite3
3f0b3 5f 73 6f 75 72 63 65 69 64 28 29 29 3b 0a 20 20  _sourceid());.  
3f0b4 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41  return SQLITE_CA
3f0b5 4e 54 4f 50 45 4e 3b 0a 7d 0a 0a 0a 23 69 66 6e  NTOPEN;.}...#ifn
3f0b6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
3f0b7 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a  DEPRECATED./*.**
3f0b8 20 54 68 69 73 20 69 73 20 61 20 63 6f 6e 76 65   This is a conve
3f0b9 6e 69 65 6e 63 65 20 72 6f 75 74 69 6e 65 20 74  nience routine t
3f0ba 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 20 74  hat makes sure t
3f0bb 68 61 74 20 61 6c 6c 20 74 68 72 65 61 64 2d 73  hat all thread-s
3f0bc 70 65 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 20  pecific.** data 
3f0bd 66 6f 72 20 74 68 69 73 20 74 68 72 65 61 64 20  for this thread 
3f0be 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63  has been dealloc
3f0bf 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  ated..**.** SQLi
3f0c0 74 65 20 6e 6f 20 6c 6f 6e 67 65 72 20 75 73 65  te no longer use
3f0c1 73 20 74 68 72 65 61 64 2d 73 70 65 63 69 66 69  s thread-specifi
3f0c2 63 20 64 61 74 61 20 73 6f 20 74 68 69 73 20 72  c data so this r
3f0c3 6f 75 74 69 6e 65 20 69 73 20 6e 6f 77 20 61 0a  outine is now a.
3f0c4 2a 2a 20 6e 6f 2d 6f 70 2e 20 20 49 74 20 69 73  ** no-op.  It is
3f0c5 20 72 65 74 61 69 6e 65 64 20 66 6f 72 20 68 69   retained for hi
3f0c6 73 74 6f 72 69 63 61 6c 20 63 6f 6d 70 61 74 69  storical compati
3f0c7 62 69 6c 69 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54  bility..*/.SQLIT
3f0c8 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
3f0c9 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e 75  e3_thread_cleanu
3f0ca 70 28 76 6f 69 64 29 7b 0a 7d 0a 23 65 6e 64 69  p(void){.}.#endi
3f0cb 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  f../*.** Return 
3f0cc 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  meta information
3f0cd 20 61 62 6f 75 74 20 61 20 73 70 65 63 69 66 69   about a specifi
3f0ce 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 64 61  c column of a da
3f0cf 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a  tabase table..**
3f0d0 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 69 6e 20   See comment in 
3f0d1 73 71 6c 69 74 65 33 2e 68 20 28 73 71 6c 69 74  sqlite3.h (sqlit
3f0d2 65 2e 68 2e 69 6e 29 20 66 6f 72 20 64 65 74 61  e.h.in) for deta
3f0d3 69 6c 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ils..*/.#ifdef S
3f0d4 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
3f0d5 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 53 51 4c  UMN_METADATA.SQL
3f0d6 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3f0d7 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e  te3_table_column
3f0d8 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 73 71 6c  _metadata(.  sql
3f0d9 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
3f0da 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e           /* Conn
3f0db 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f  ection handle */
3f0dc 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3f0dd 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f  DbName,        /
3f0de 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d 65 20  * Database name 
3f0df 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e  or NULL */.  con
3f0e0 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e  st char *zTableN
3f0e1 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c  ame,     /* Tabl
3f0e2 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73  e name */.  cons
3f0e3 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e  t char *zColumnN
3f0e4 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d  ame,    /* Colum
3f0e5 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72  n name */.  char
3f0e6 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61 54   const **pzDataT
3f0e7 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50 55  ype,    /* OUTPU
3f0e8 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61 74 61  T: Declared data
3f0e9 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 61 72 20   type */.  char 
3f0ea 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65  const **pzCollSe
3f0eb 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54  q,     /* OUTPUT
3f0ec 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  : Collation sequ
3f0ed 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69  ence name */.  i
3f0ee 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20  nt *pNotNull,   
3f0ef 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
3f0f0 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 4e 4f  TPUT: True if NO
3f0f1 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e  T NULL constrain
3f0f2 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 6e  t exists */.  in
3f0f3 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c 20  t *pPrimaryKey, 
3f0f4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
3f0f5 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c  PUT: True if col
3f0f6 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b 20 2a  umn part of PK *
3f0f7 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f 69 6e  /.  int *pAutoin
3f0f8 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
3f0f9 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20  /* OUTPUT: True 
3f0fa 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75 74  if column is aut
3f0fb 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 29  o-increment */.)
3f0fc 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 68  {.  int rc;.  ch
3f0fd 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ar *zErrMsg = 0;
3f0fe 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d  .  Table *pTab =
3f0ff 20 30 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43   0;.  Column *pC
3f100 6f 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43  ol = 0;.  int iC
3f101 6f 6c 3b 0a 0a 20 20 63 68 61 72 20 63 6f 6e 73  ol;..  char cons
3f102 74 20 2a 7a 44 61 74 61 54 79 70 65 20 3d 20 30  t *zDataType = 0
3f103 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ;.  char const *
3f104 7a 43 6f 6c 6c 53 65 71 20 3d 20 30 3b 0a 20 20  zCollSeq = 0;.  
3f105 69 6e 74 20 6e 6f 74 6e 75 6c 6c 20 3d 20 30 3b  int notnull = 0;
3f106 0a 20 20 69 6e 74 20 70 72 69 6d 61 72 79 6b 65  .  int primaryke
3f107 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 61 75 74  y = 0;.  int aut
3f108 6f 69 6e 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  oinc = 0;..  /* 
3f109 45 6e 73 75 72 65 20 74 68 65 20 64 61 74 61 62  Ensure the datab
3f10a 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 62  ase schema has b
3f10b 65 65 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 20 20  een loaded */.  
3f10c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
3f10d 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
3f10e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
3f10f 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 72 63  terAll(db);.  rc
3f110 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64   = sqlite3Init(d
3f111 62 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20  b, &zErrMsg);.  
3f112 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72  if( SQLITE_OK!=r
3f113 63 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 72  c ){.    goto er
3f114 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  ror_out;.  }..  
3f115 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61  /* Locate the ta
3f116 62 6c 65 20 69 6e 20 71 75 65 73 74 69 6f 6e 20  ble in question 
3f117 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69  */.  pTab = sqli
3f118 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
3f119 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 7a 44 62   zTableName, zDb
3f11a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 70 54  Name);.  if( !pT
3f11b 61 62 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c  ab || pTab->pSel
3f11c 65 63 74 20 29 7b 0a 20 20 20 20 70 54 61 62 20  ect ){.    pTab 
3f11d 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72  = 0;.    goto er
3f11e 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  ror_out;.  }..  
3f11f 2f 2a 20 46 69 6e 64 20 74 68 65 20 63 6f 6c 75  /* Find the colu
3f120 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e 66  mn for which inf
3f121 6f 20 69 73 20 72 65 71 75 65 73 74 65 64 20 2a  o is requested *
3f122 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49  /.  if( sqlite3I
3f123 73 52 6f 77 69 64 28 7a 43 6f 6c 75 6d 6e 4e 61  sRowid(zColumnNa
3f124 6d 65 29 20 29 7b 0a 20 20 20 20 69 43 6f 6c 20  me) ){.    iCol 
3f125 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20  = pTab->iPKey;. 
3f126 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29     if( iCol>=0 )
3f127 7b 0a 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26  {.      pCol = &
3f128 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  pTab->aCol[iCol]
3f129 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
3f12a 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b  .    for(iCol=0;
3f12b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c   iCol<pTab->nCol
3f12c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  ; iCol++){.     
3f12d 20 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61   pCol = &pTab->a
3f12e 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 20  Col[iCol];.     
3f12f 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53   if( 0==sqlite3S
3f130 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61  trICmp(pCol->zNa
3f131 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29  me, zColumnName)
3f132 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
3f133 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
3f134 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70  .    if( iCol==p
3f135 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20  Tab->nCol ){.   
3f136 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20     pTab = 0;.   
3f137 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f 75     goto error_ou
3f138 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  t;.    }.  }..  
3f139 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
3f13a 20 62 6c 6f 63 6b 20 73 74 6f 72 65 73 20 74 68   block stores th
3f13b 65 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69  e meta informati
3f13c 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20  on that will be 
3f13d 72 65 74 75 72 6e 65 64 0a 20 20 2a 2a 20 74 6f  returned.  ** to
3f13e 20 74 68 65 20 63 61 6c 6c 65 72 20 69 6e 20 6c   the caller in l
3f13f 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 7a  ocal variables z
3f140 44 61 74 61 54 79 70 65 2c 20 7a 43 6f 6c 6c 53  DataType, zCollS
3f141 65 71 2c 20 6e 6f 74 6e 75 6c 6c 2c 20 70 72 69  eq, notnull, pri
3f142 6d 61 72 79 6b 65 79 0a 20 20 2a 2a 20 61 6e 64  marykey.  ** and
3f143 20 61 75 74 6f 69 6e 63 2e 20 41 74 20 74 68 69   autoinc. At thi
3f144 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20 61 72  s point there ar
3f145 65 20 74 77 6f 20 70 6f 73 73 69 62 69 6c 69 74  e two possibilit
3f146 69 65 73 3a 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20  ies:.  ** .  ** 
3f147 20 20 20 20 31 2e 20 54 68 65 20 73 70 65 63 69      1. The speci
3f148 66 69 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  fied column name
3f149 20 77 61 73 20 72 6f 77 69 64 22 2c 20 22 6f 69   was rowid", "oi
3f14a 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f 22 20  d" or "_rowid_" 
3f14b 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 61 6e 64  .  **        and
3f14c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 65 78 70   there is no exp
3f14d 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64  licitly declared
3f14e 20 49 50 4b 20 63 6f 6c 75 6d 6e 2e 20 0a 20 20   IPK column. .  
3f14f 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 32 2e 20 54  **.  **     2. T
3f150 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  he table is not 
3f151 61 20 76 69 65 77 20 61 6e 64 20 74 68 65 20 63  a view and the c
3f152 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 64 65 6e 74  olumn name ident
3f153 69 66 69 65 64 20 61 6e 20 0a 20 20 2a 2a 20 20  ified an .  **  
3f154 20 20 20 20 20 20 65 78 70 6c 69 63 69 74 6c 79        explicitly
3f155 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e   declared column
3f156 2e 20 43 6f 70 79 20 6d 65 74 61 20 69 6e 66 6f  . Copy meta info
3f157 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 2a 70 43  rmation from *pC
3f158 6f 6c 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20  ol..  */ .  if( 
3f159 70 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 44 61 74  pCol ){.    zDat
3f15a 61 54 79 70 65 20 3d 20 70 43 6f 6c 2d 3e 7a 54  aType = pCol->zT
3f15b 79 70 65 3b 0a 20 20 20 20 7a 43 6f 6c 6c 53 65  ype;.    zCollSe
3f15c 71 20 3d 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 3b  q = pCol->zColl;
3f15d 0a 20 20 20 20 6e 6f 74 6e 75 6c 6c 20 3d 20 70  .    notnull = p
3f15e 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 21 3d 30 3b  Col->notNull!=0;
3f15f 0a 20 20 20 20 70 72 69 6d 61 72 79 6b 65 79 20  .    primarykey 
3f160 20 3d 20 28 70 43 6f 6c 2d 3e 63 6f 6c 46 6c 61   = (pCol->colFla
3f161 67 73 20 26 20 43 4f 4c 46 4c 41 47 5f 50 52 49  gs & COLFLAG_PRI
3f162 4d 4b 45 59 29 21 3d 30 3b 0a 20 20 20 20 61 75  MKEY)!=0;.    au
3f163 74 6f 69 6e 63 20 3d 20 70 54 61 62 2d 3e 69 50  toinc = pTab->iP
3f164 4b 65 79 3d 3d 69 43 6f 6c 20 26 26 20 28 70 54  Key==iCol && (pT
3f165 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
3f166 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 29  F_Autoincrement)
3f167 21 3d 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  !=0;.  }else{.  
3f168 20 20 7a 44 61 74 61 54 79 70 65 20 3d 20 22 49    zDataType = "I
3f169 4e 54 45 47 45 52 22 3b 0a 20 20 20 20 70 72 69  NTEGER";.    pri
3f16a 6d 61 72 79 6b 65 79 20 3d 20 31 3b 0a 20 20 7d  marykey = 1;.  }
3f16b 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c 53 65 71  .  if( !zCollSeq
3f16c 20 29 7b 0a 20 20 20 20 7a 43 6f 6c 6c 53 65 71   ){.    zCollSeq
3f16d 20 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20 20 7d   = "BINARY";.  }
3f16e 0a 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 73  ..error_out:.  s
3f16f 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
3f170 41 6c 6c 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 57  All(db);..  /* W
3f171 68 65 74 68 65 72 20 74 68 65 20 66 75 6e 63 74  hether the funct
3f172 69 6f 6e 20 63 61 6c 6c 20 73 75 63 63 65 65 64  ion call succeed
3f173 65 64 20 6f 72 20 66 61 69 6c 65 64 2c 20 73 65  ed or failed, se
3f174 74 20 74 68 65 20 6f 75 74 70 75 74 20 70 61 72  t the output par
3f175 61 6d 65 74 65 72 73 0a 20 20 2a 2a 20 74 6f 20  ameters.  ** to 
3f176 77 68 61 74 65 76 65 72 20 74 68 65 69 72 20 6c  whatever their l
3f177 6f 63 61 6c 20 63 6f 75 6e 74 65 72 70 61 72 74  ocal counterpart
3f178 73 20 63 6f 6e 74 61 69 6e 2e 20 49 66 20 61 6e  s contain. If an
3f179 20 65 72 72 6f 72 20 64 69 64 20 6f 63 63 75 72   error did occur
3f17a 2c 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 73 20  ,.  ** this has 
3f17b 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 7a 65  the effect of ze
3f17c 72 6f 69 6e 67 20 61 6c 6c 20 6f 75 74 70 75 74  roing all output
3f17d 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a   parameters..  *
3f17e 2f 0a 20 20 69 66 28 20 70 7a 44 61 74 61 54 79  /.  if( pzDataTy
3f17f 70 65 20 29 20 2a 70 7a 44 61 74 61 54 79 70 65  pe ) *pzDataType
3f180 20 3d 20 7a 44 61 74 61 54 79 70 65 3b 0a 20 20   = zDataType;.  
3f181 69 66 28 20 70 7a 43 6f 6c 6c 53 65 71 20 29 20  if( pzCollSeq ) 
3f182 2a 70 7a 43 6f 6c 6c 53 65 71 20 3d 20 7a 43 6f  *pzCollSeq = zCo
3f183 6c 6c 53 65 71 3b 0a 20 20 69 66 28 20 70 4e 6f  llSeq;.  if( pNo
3f184 74 4e 75 6c 6c 20 29 20 2a 70 4e 6f 74 4e 75 6c  tNull ) *pNotNul
3f185 6c 20 3d 20 6e 6f 74 6e 75 6c 6c 3b 0a 20 20 69  l = notnull;.  i
3f186 66 28 20 70 50 72 69 6d 61 72 79 4b 65 79 20 29  f( pPrimaryKey )
3f187 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 20 3d 20   *pPrimaryKey = 
3f188 70 72 69 6d 61 72 79 6b 65 79 3b 0a 20 20 69 66  primarykey;.  if
3f189 28 20 70 41 75 74 6f 69 6e 63 20 29 20 2a 70 41  ( pAutoinc ) *pA
3f18a 75 74 6f 69 6e 63 20 3d 20 61 75 74 6f 69 6e 63  utoinc = autoinc
3f18b 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  ;..  if( SQLITE_
3f18c 4f 4b 3d 3d 72 63 20 26 26 20 21 70 54 61 62 20  OK==rc && !pTab 
3f18d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ){.    sqlite3Db
3f18e 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67  Free(db, zErrMsg
3f18f 29 3b 0a 20 20 20 20 7a 45 72 72 4d 73 67 20 3d  );.    zErrMsg =
3f190 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
3f191 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 74 61 62  db, "no such tab
3f192 6c 65 20 63 6f 6c 75 6d 6e 3a 20 25 73 2e 25 73  le column: %s.%s
3f193 22 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 0a 20  ", zTableName,. 
3f194 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 4e 61         zColumnNa
3f195 6d 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  me);.    rc = SQ
3f196 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
3f197 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
3f198 62 2c 20 72 63 2c 20 28 7a 45 72 72 4d 73 67 3f  b, rc, (zErrMsg?
3f199 22 25 73 22 3a 30 29 2c 20 7a 45 72 72 4d 73 67  "%s":0), zErrMsg
3f19a 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
3f19b 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b  ee(db, zErrMsg);
3f19c 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
3f19d 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a  piExit(db, rc);.
3f19e 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
3f19f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
3f1a0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
3f1a1 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53  .#endif../*.** S
3f1a2 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c  leep for a littl
3f1a3 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e  e while.  Return
3f1a4 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74   the amount of t
3f1a5 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 53 51  ime slept..*/.SQ
3f1a6 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
3f1a7 69 74 65 33 5f 73 6c 65 65 70 28 69 6e 74 20 6d  ite3_sleep(int m
3f1a8 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  s){.  sqlite3_vf
3f1a9 73 20 2a 70 56 66 73 3b 0a 20 20 69 6e 74 20 72  s *pVfs;.  int r
3f1aa 63 3b 0a 20 20 70 56 66 73 20 3d 20 73 71 6c 69  c;.  pVfs = sqli
3f1ab 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 30 29 3b  te3_vfs_find(0);
3f1ac 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30 20 29  .  if( pVfs==0 )
3f1ad 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a   return 0;..  /*
3f1ae 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   This function w
3f1af 6f 72 6b 73 20 69 6e 20 6d 69 6c 6c 69 73 65 63  orks in millisec
3f1b0 6f 6e 64 73 2c 20 62 75 74 20 74 68 65 20 75 6e  onds, but the un
3f1b1 64 65 72 6c 79 69 6e 67 20 4f 73 53 6c 65 65 70  derlying OsSleep
3f1b2 28 29 20 0a 20 20 2a 2a 20 41 50 49 20 75 73 65  () .  ** API use
3f1b3 73 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2e 20  s microseconds. 
3f1b4 48 65 6e 63 65 20 74 68 65 20 31 30 30 30 27 73  Hence the 1000's
3f1b5 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 28 73  ..  */.  rc = (s
3f1b6 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 70 56  qlite3OsSleep(pV
3f1b7 66 73 2c 20 31 30 30 30 2a 6d 73 29 2f 31 30 30  fs, 1000*ms)/100
3f1b8 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  0);.  return rc;
3f1b9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65  .}../*.** Enable
3f1ba 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20   or disable the 
3f1bb 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
3f1bc 63 6f 64 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  codes..*/.SQLITE
3f1bd 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3f1be 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74  _extended_result
3f1bf 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 20 2a  _codes(sqlite3 *
3f1c0 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 7b 0a  db, int onoff){.
3f1c1 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
3f1c2 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
3f1c3 3b 0a 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20  ;.  db->errMask 
3f1c4 3d 20 6f 6e 6f 66 66 20 3f 20 30 78 66 66 66 66  = onoff ? 0xffff
3f1c5 66 66 66 66 20 3a 20 30 78 66 66 3b 0a 20 20 73  ffff : 0xff;.  s
3f1c6 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
3f1c7 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
3f1c8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3f1c9 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f  K;.}../*.** Invo
3f1ca 6b 65 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74  ke the xFileCont
3f1cb 72 6f 6c 20 6d 65 74 68 6f 64 20 6f 6e 20 61 20  rol method on a 
3f1cc 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62  particular datab
3f1cd 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ase..*/.SQLITE_A
3f1ce 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
3f1cf 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69  ile_control(sqli
3f1d0 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
3f1d1 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e  har *zDbName, in
3f1d2 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67  t op, void *pArg
3f1d3 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
3f1d4 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 42 74  LITE_ERROR;.  Bt
3f1d5 72 65 65 20 2a 70 42 74 72 65 65 3b 0a 0a 20 20  ree *pBtree;..  
3f1d6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
3f1d7 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
3f1d8 20 20 70 42 74 72 65 65 20 3d 20 73 71 6c 69 74    pBtree = sqlit
3f1d9 65 33 44 62 4e 61 6d 65 54 6f 42 74 72 65 65 28  e3DbNameToBtree(
3f1da 64 62 2c 20 7a 44 62 4e 61 6d 65 29 3b 0a 20 20  db, zDbName);.  
3f1db 69 66 28 20 70 42 74 72 65 65 20 29 7b 0a 20 20  if( pBtree ){.  
3f1dc 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b    Pager *pPager;
3f1dd 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  .    sqlite3_fil
3f1de 65 20 2a 66 64 3b 0a 20 20 20 20 73 71 6c 69 74  e *fd;.    sqlit
3f1df 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74  e3BtreeEnter(pBt
3f1e0 72 65 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72  ree);.    pPager
3f1e1 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
3f1e2 61 67 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20  ager(pBtree);.  
3f1e3 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
3f1e4 21 3d 30 20 29 3b 0a 20 20 20 20 66 64 20 3d 20  !=0 );.    fd = 
3f1e5 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65  sqlite3PagerFile
3f1e6 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 61 73  (pPager);.    as
3f1e7 73 65 72 74 28 20 66 64 21 3d 30 20 29 3b 0a 20  sert( fd!=0 );. 
3f1e8 20 20 20 69 66 28 20 6f 70 3d 3d 53 51 4c 49 54     if( op==SQLIT
3f1e9 45 5f 46 43 4e 54 4c 5f 46 49 4c 45 5f 50 4f 49  E_FCNTL_FILE_POI
3f1ea 4e 54 45 52 20 29 7b 0a 20 20 20 20 20 20 2a 28  NTER ){.      *(
3f1eb 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2a 29 70  sqlite3_file**)p
3f1ec 41 72 67 20 3d 20 66 64 3b 0a 20 20 20 20 20 20  Arg = fd;.      
3f1ed 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
3f1ee 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 64      }else if( fd
3f1ef 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  ->pMethods ){.  
3f1f0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
3f1f1 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 66 64  OsFileControl(fd
3f1f2 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a 20 20 20  , op, pArg);.   
3f1f3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
3f1f4 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55   = SQLITE_NOTFOU
3f1f5 4e 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ND;.    }.    sq
3f1f6 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
3f1f7 70 42 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20 73  pBtree);.  }.  s
3f1f8 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
3f1f9 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
3f1fa 20 72 65 74 75 72 6e 20 72 63 3b 20 20 20 0a 7d   return rc;   .}
3f1fb 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63  ../*.** Interfac
3f1fc 65 20 74 6f 20 74 68 65 20 74 65 73 74 69 6e 67  e to the testing
3f1fd 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54   logic..*/.SQLIT
3f1fe 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3f1ff 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 69  3_test_control(i
3f200 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20 69  nt op, ...){.  i
3f201 6e 74 20 72 63 20 3d 20 30 3b 0a 23 69 66 6e 64  nt rc = 0;.#ifnd
3f202 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
3f203 55 49 4c 54 49 4e 5f 54 45 53 54 0a 20 20 76 61  UILTIN_TEST.  va
3f204 5f 6c 69 73 74 20 61 70 3b 0a 20 20 76 61 5f 73  _list ap;.  va_s
3f205 74 61 72 74 28 61 70 2c 20 6f 70 29 3b 0a 20 20  tart(ap, op);.  
3f206 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 0a 20  switch( op ){.. 
3f207 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 53 61 76     /*.    ** Sav
3f208 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74  e the current st
3f209 61 74 65 20 6f 66 20 74 68 65 20 50 52 4e 47 2e  ate of the PRNG.
3f20a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
3f20b 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
3f20c 5f 50 52 4e 47 5f 53 41 56 45 3a 20 7b 0a 20 20  _PRNG_SAVE: {.  
3f20d 20 20 20 20 73 71 6c 69 74 65 33 50 72 6e 67 53      sqlite3PrngS
3f20e 61 76 65 53 74 61 74 65 28 29 3b 0a 20 20 20 20  aveState();.    
3f20f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
3f210 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 52 65      /*.    ** Re
3f211 73 74 6f 72 65 20 74 68 65 20 73 74 61 74 65 20  store the state 
3f212 6f 66 20 74 68 65 20 50 52 4e 47 20 74 6f 20 74  of the PRNG to t
3f213 68 65 20 6c 61 73 74 20 73 74 61 74 65 20 73 61  he last state sa
3f214 76 65 64 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a  ved using.    **
3f215 20 50 52 4e 47 5f 53 41 56 45 2e 20 20 49 66 20   PRNG_SAVE.  If 
3f216 50 52 4e 47 5f 53 41 56 45 20 68 61 73 20 6e 65  PRNG_SAVE has ne
3f217 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20  ver before been 
3f218 63 61 6c 6c 65 64 2c 20 74 68 65 6e 0a 20 20 20  called, then.   
3f219 20 2a 2a 20 74 68 69 73 20 76 65 72 62 20 61 63   ** this verb ac
3f21a 74 73 20 6c 69 6b 65 20 50 52 4e 47 5f 52 45 53  ts like PRNG_RES
3f21b 45 54 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ET..    */.    c
3f21c 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ase SQLITE_TESTC
3f21d 54 52 4c 5f 50 52 4e 47 5f 52 45 53 54 4f 52 45  TRL_PRNG_RESTORE
3f21e 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
3f21f 33 50 72 6e 67 52 65 73 74 6f 72 65 53 74 61 74  3PrngRestoreStat
3f220 65 28 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  e();.      break
3f221 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a  ;.    }..    /*.
3f222 20 20 20 20 2a 2a 20 52 65 73 65 74 20 74 68 65      ** Reset the
3f223 20 50 52 4e 47 20 62 61 63 6b 20 74 6f 20 69 74   PRNG back to it
3f224 73 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20  s uninitialized 
3f225 73 74 61 74 65 2e 20 20 54 68 65 20 6e 65 78 74  state.  The next
3f226 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20   call.    ** to 
3f227 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
3f228 73 73 28 29 20 77 69 6c 6c 20 72 65 73 65 65 64  ss() will reseed
3f229 20 74 68 65 20 50 52 4e 47 20 75 73 69 6e 67 20   the PRNG using 
3f22a 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 0a 20 20  a single call.  
3f22b 20 20 2a 2a 20 74 6f 20 74 68 65 20 78 52 61 6e    ** to the xRan
3f22c 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20 6f  domness method o
3f22d 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 56 46  f the default VF
3f22e 53 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  S..    */.    ca
3f22f 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  se SQLITE_TESTCT
3f230 52 4c 5f 50 52 4e 47 5f 52 45 53 45 54 3a 20 7b  RL_PRNG_RESET: {
3f231 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 72  .      sqlite3Pr
3f232 6e 67 52 65 73 65 74 53 74 61 74 65 28 29 3b 0a  ngResetState();.
3f233 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3f234 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a   }..    /*.    *
3f235 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  *  sqlite3_test_
3f236 63 6f 6e 74 72 6f 6c 28 42 49 54 56 45 43 5f 54  control(BITVEC_T
3f237 45 53 54 2c 20 73 69 7a 65 2c 20 70 72 6f 67 72  EST, size, progr
3f238 61 6d 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  am).    **.    *
3f239 2a 20 52 75 6e 20 61 20 74 65 73 74 20 61 67 61  * Run a test aga
3f23a 69 6e 73 74 20 61 20 42 69 74 76 65 63 20 6f 62  inst a Bitvec ob
3f23b 6a 65 63 74 20 6f 66 20 73 69 7a 65 2e 20 20 54  ject of size.  T
3f23c 68 65 20 70 72 6f 67 72 61 6d 20 61 72 67 75 6d  he program argum
3f23d 65 6e 74 0a 20 20 20 20 2a 2a 20 69 73 20 61 6e  ent.    ** is an
3f23e 20 61 72 72 61 79 20 6f 66 20 69 6e 74 65 67 65   array of intege
3f23f 72 73 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  rs that defines 
3f240 74 68 65 20 74 65 73 74 2e 20 20 52 65 74 75 72  the test.  Retur
3f241 6e 20 2d 31 20 6f 6e 20 61 0a 20 20 20 20 2a 2a  n -1 on a.    **
3f242 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
3f243 6f 6e 20 65 72 72 6f 72 2c 20 30 20 6f 6e 20 73  on error, 0 on s
3f244 75 63 63 65 73 73 2c 20 6f 72 20 6e 6f 6e 2d 7a  uccess, or non-z
3f245 65 72 6f 20 66 6f 72 20 61 6e 20 65 72 72 6f 72  ero for an error
3f246 2e 0a 20 20 20 20 2a 2a 20 53 65 65 20 74 68 65  ..    ** See the
3f247 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42 75   sqlite3BitvecBu
3f248 69 6c 74 69 6e 54 65 73 74 28 29 20 66 6f 72 20  iltinTest() for 
3f249 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
3f24a 6d 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20  mation..    */. 
3f24b 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54     case SQLITE_T
3f24c 45 53 54 43 54 52 4c 5f 42 49 54 56 45 43 5f 54  ESTCTRL_BITVEC_T
3f24d 45 53 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  EST: {.      int
3f24e 20 73 7a 20 3d 20 76 61 5f 61 72 67 28 61 70 2c   sz = va_arg(ap,
3f24f 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 6e 74   int);.      int
3f250 20 2a 61 50 72 6f 67 20 3d 20 76 61 5f 61 72 67   *aProg = va_arg
3f251 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20  (ap, int*);.    
3f252 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69    rc = sqlite3Bi
3f253 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28  tvecBuiltinTest(
3f254 73 7a 2c 20 61 50 72 6f 67 29 3b 0a 20 20 20 20  sz, aProg);.    
3f255 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
3f256 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 73      /*.    **  s
3f257 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
3f258 72 6f 6c 28 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f  rol(BENIGN_MALLO
3f259 43 5f 48 4f 4f 4b 53 2c 20 78 42 65 67 69 6e 2c  C_HOOKS, xBegin,
3f25a 20 78 45 6e 64 29 0a 20 20 20 20 2a 2a 0a 20 20   xEnd).    **.  
3f25b 20 20 2a 2a 20 52 65 67 69 73 74 65 72 20 68 6f    ** Register ho
3f25c 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 74 6f 20 69  oks to call to i
3f25d 6e 64 69 63 61 74 65 20 77 68 69 63 68 20 6d 61  ndicate which ma
3f25e 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 73 20  lloc() failures 
3f25f 0a 20 20 20 20 2a 2a 20 61 72 65 20 62 65 6e 69  .    ** are beni
3f260 67 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  gn..    */.    c
3f261 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ase SQLITE_TESTC
3f262 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f  TRL_BENIGN_MALLO
3f263 43 5f 48 4f 4f 4b 53 3a 20 7b 0a 20 20 20 20 20  C_HOOKS: {.     
3f264 20 74 79 70 65 64 65 66 20 76 6f 69 64 20 28 2a   typedef void (*
3f265 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 29 28 76  void_function)(v
3f266 6f 69 64 29 3b 0a 20 20 20 20 20 20 76 6f 69 64  oid);.      void
3f267 5f 66 75 6e 63 74 69 6f 6e 20 78 42 65 6e 69 67  _function xBenig
3f268 6e 42 65 67 69 6e 3b 0a 20 20 20 20 20 20 76 6f  nBegin;.      vo
3f269 69 64 5f 66 75 6e 63 74 69 6f 6e 20 78 42 65 6e  id_function xBen
3f26a 69 67 6e 45 6e 64 3b 0a 20 20 20 20 20 20 78 42  ignEnd;.      xB
3f26b 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 76 61 5f  enignBegin = va_
3f26c 61 72 67 28 61 70 2c 20 76 6f 69 64 5f 66 75 6e  arg(ap, void_fun
3f26d 63 74 69 6f 6e 29 3b 0a 20 20 20 20 20 20 78 42  ction);.      xB
3f26e 65 6e 69 67 6e 45 6e 64 20 3d 20 76 61 5f 61 72  enignEnd = va_ar
3f26f 67 28 61 70 2c 20 76 6f 69 64 5f 66 75 6e 63 74  g(ap, void_funct
3f270 69 6f 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ion);.      sqli
3f271 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48  te3BenignMallocH
3f272 6f 6f 6b 73 28 78 42 65 6e 69 67 6e 42 65 67 69  ooks(xBenignBegi
3f273 6e 2c 20 78 42 65 6e 69 67 6e 45 6e 64 29 3b 0a  n, xBenignEnd);.
3f274 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3f275 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a   }..    /*.    *
3f276 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  *  sqlite3_test_
3f277 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54  control(SQLITE_T
3f278 45 53 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f  ESTCTRL_PENDING_
3f279 42 59 54 45 2c 20 75 6e 73 69 67 6e 65 64 20 69  BYTE, unsigned i
3f27a 6e 74 20 58 29 0a 20 20 20 20 2a 2a 0a 20 20 20  nt X).    **.   
3f27b 20 2a 2a 20 53 65 74 20 74 68 65 20 50 45 4e 44   ** Set the PEND
3f27c 49 4e 47 20 62 79 74 65 20 74 6f 20 74 68 65 20  ING byte to the 
3f27d 76 61 6c 75 65 20 69 6e 20 74 68 65 20 61 72 67  value in the arg
3f27e 75 6d 65 6e 74 2c 20 69 66 20 58 3e 30 2e 0a 20  ument, if X>0.. 
3f27f 20 20 20 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68     ** Make no ch
3f280 61 6e 67 65 73 20 69 66 20 58 3d 3d 30 2e 20 20  anges if X==0.  
3f281 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65  Return the value
3f282 20 6f 66 20 74 68 65 20 70 65 6e 64 69 6e 67 20   of the pending 
3f283 62 79 74 65 0a 20 20 20 20 2a 2a 20 61 73 20 69  byte.    ** as i
3f284 74 20 65 78 69 73 74 69 6e 67 20 62 65 66 6f 72  t existing befor
3f285 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  e this routine w
3f286 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20 20 20 2a  as called..    *
3f287 2a 0a 20 20 20 20 2a 2a 20 49 4d 50 4f 52 54 41  *.    ** IMPORTA
3f288 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67 20 74 68  NT:  Changing th
3f289 65 20 50 45 4e 44 49 4e 47 20 62 79 74 65 20 66  e PENDING byte f
3f28a 72 6f 6d 20 30 78 34 30 30 30 30 30 30 30 20 72  rom 0x40000000 r
3f28b 65 73 75 6c 74 73 20 69 6e 0a 20 20 20 20 2a 2a  esults in.    **
3f28c 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65   an incompatible
3f28d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66   database file f
3f28e 6f 72 6d 61 74 2e 20 20 43 68 61 6e 67 69 6e 67  ormat.  Changing
3f28f 20 74 68 65 20 50 45 4e 44 49 4e 47 20 62 79 74   the PENDING byt
3f290 65 0a 20 20 20 20 2a 2a 20 77 68 69 6c 65 20 61  e.    ** while a
3f291 6e 79 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ny database conn
3f292 65 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20 72  ection is open r
3f293 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 69  esults in undefi
3f294 6e 65 64 20 61 6e 64 0a 20 20 20 20 2a 2a 20 64  ned and.    ** d
3f295 69 6c 65 74 65 72 69 6f 75 73 20 62 65 68 61 76  ileterious behav
3f296 69 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ior..    */.    
3f297 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54  case SQLITE_TEST
3f298 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 59 54  CTRL_PENDING_BYT
3f299 45 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  E: {.      rc = 
3f29a 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 23 69  PENDING_BYTE;.#i
3f29b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
3f29c 54 5f 57 53 44 0a 20 20 20 20 20 20 7b 0a 20 20  T_WSD.      {.  
3f29d 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69        unsigned i
3f29e 6e 74 20 6e 65 77 56 61 6c 20 3d 20 76 61 5f 61  nt newVal = va_a
3f29f 72 67 28 61 70 2c 20 75 6e 73 69 67 6e 65 64 20  rg(ap, unsigned 
3f2a0 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66  int);.        if
3f2a1 28 20 6e 65 77 56 61 6c 20 29 20 73 71 6c 69 74  ( newVal ) sqlit
3f2a2 65 33 50 65 6e 64 69 6e 67 42 79 74 65 20 3d 20  e3PendingByte = 
3f2a3 6e 65 77 56 61 6c 3b 0a 20 20 20 20 20 20 7d 0a  newVal;.      }.
3f2a4 23 65 6e 64 69 66 0a 20 20 20 20 20 20 62 72 65  #endif.      bre
3f2a5 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ak;.    }..    /
3f2a6 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74 65  *.    **  sqlite
3f2a7 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53  3_test_control(S
3f2a8 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41  QLITE_TESTCTRL_A
3f2a9 53 53 45 52 54 2c 20 69 6e 74 20 58 29 0a 20 20  SSERT, int X).  
3f2aa 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73    **.    ** This
3f2ab 20 61 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 73   action provides
3f2ac 20 61 20 72 75 6e 2d 74 69 6d 65 20 74 65 73 74   a run-time test
3f2ad 20 74 6f 20 73 65 65 20 77 68 65 74 68 65 72 20   to see whether 
3f2ae 6f 72 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 61 73  or not.    ** as
3f2af 73 65 72 74 28 29 20 77 61 73 20 65 6e 61 62 6c  sert() was enabl
3f2b0 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  ed at compile-ti
3f2b1 6d 65 2e 20 20 49 66 20 58 20 69 73 20 74 72 75  me.  If X is tru
3f2b2 65 20 61 6e 64 20 61 73 73 65 72 74 28 29 0a 20  e and assert(). 
3f2b3 20 20 20 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64     ** is enabled
3f2b4 2c 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72  , then the retur
3f2b5 6e 20 76 61 6c 75 65 20 69 73 20 74 72 75 65 2e  n value is true.
3f2b6 20 20 49 66 20 58 20 69 73 20 74 72 75 65 20 61    If X is true a
3f2b7 6e 64 0a 20 20 20 20 2a 2a 20 61 73 73 65 72 74  nd.    ** assert
3f2b8 28 29 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20  () is disabled, 
3f2b9 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e 20  then the return 
3f2ba 76 61 6c 75 65 20 69 73 20 7a 65 72 6f 2e 20 20  value is zero.  
3f2bb 49 66 20 58 20 69 73 0a 20 20 20 20 2a 2a 20 66  If X is.    ** f
3f2bc 61 6c 73 65 20 61 6e 64 20 61 73 73 65 72 74 28  alse and assert(
3f2bd 29 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68  ) is enabled, th
3f2be 65 6e 20 74 68 65 20 61 73 73 65 72 74 69 6f 6e  en the assertion
3f2bf 20 66 69 72 65 73 20 61 6e 64 20 74 68 65 0a 20   fires and the. 
3f2c0 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 62     ** process ab
3f2c1 6f 72 74 73 2e 20 20 49 66 20 58 20 69 73 20 66  orts.  If X is f
3f2c2 61 6c 73 65 20 61 6e 64 20 61 73 73 65 72 74 28  alse and assert(
3f2c3 29 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74  ) is disabled, t
3f2c4 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 72  hen the.    ** r
3f2c5 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 7a  eturn value is z
3f2c6 65 72 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ero..    */.    
3f2c7 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54  case SQLITE_TEST
3f2c8 43 54 52 4c 5f 41 53 53 45 52 54 3a 20 7b 0a 20  CTRL_ASSERT: {. 
3f2c9 20 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 69 6e       volatile in
3f2ca 74 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 61  t x = 0;.      a
3f2cb 73 73 65 72 74 28 20 28 78 20 3d 20 76 61 5f 61  ssert( (x = va_a
3f2cc 72 67 28 61 70 2c 69 6e 74 29 29 21 3d 30 20 29  rg(ap,int))!=0 )
3f2cd 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 78 3b 0a  ;.      rc = x;.
3f2ce 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3f2cf 20 7d 0a 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20   }...    /*.    
3f2d0 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74  **  sqlite3_test
3f2d1 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f  _control(SQLITE_
3f2d2 54 45 53 54 43 54 52 4c 5f 41 4c 57 41 59 53 2c  TESTCTRL_ALWAYS,
3f2d3 20 69 6e 74 20 58 29 0a 20 20 20 20 2a 2a 0a 20   int X).    **. 
3f2d4 20 20 20 2a 2a 20 54 68 69 73 20 61 63 74 69 6f     ** This actio
3f2d5 6e 20 70 72 6f 76 69 64 65 73 20 61 20 72 75 6e  n provides a run
3f2d6 2d 74 69 6d 65 20 74 65 73 74 20 74 6f 20 73 65  -time test to se
3f2d7 65 20 68 6f 77 20 74 68 65 20 41 4c 57 41 59 53  e how the ALWAYS
3f2d8 20 61 6e 64 0a 20 20 20 20 2a 2a 20 4e 45 56 45   and.    ** NEVE
3f2d9 52 20 6d 61 63 72 6f 73 20 77 65 72 65 20 64 65  R macros were de
3f2da 66 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 65  fined at compile
3f2db 2d 74 69 6d 65 2e 0a 20 20 20 20 2a 2a 0a 20 20  -time..    **.  
3f2dc 20 20 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20    ** The return 
3f2dd 76 61 6c 75 65 20 69 73 20 41 4c 57 41 59 53 28  value is ALWAYS(
3f2de 58 29 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20  X).  .    **.   
3f2df 20 2a 2a 20 54 68 65 20 72 65 63 6f 6d 6d 65 6e   ** The recommen
3f2e0 64 65 64 20 74 65 73 74 20 69 73 20 58 3d 3d 32  ded test is X==2
3f2e1 2e 20 20 49 66 20 74 68 65 20 72 65 74 75 72 6e  .  If the return
3f2e2 20 76 61 6c 75 65 20 69 73 20 32 2c 20 74 68 61   value is 2, tha
3f2e3 74 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 41  t means.    ** A
3f2e4 4c 57 41 59 53 28 29 20 61 6e 64 20 4e 45 56 45  LWAYS() and NEVE
3f2e5 52 28 29 20 61 72 65 20 62 6f 74 68 20 6e 6f 2d  R() are both no-
3f2e6 6f 70 20 70 61 73 73 2d 74 68 72 6f 75 67 68 20  op pass-through 
3f2e7 6d 61 63 72 6f 73 2c 20 77 68 69 63 68 20 69 73  macros, which is
3f2e8 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 65 66 61   the.    ** defa
3f2e9 75 6c 74 20 73 65 74 74 69 6e 67 2e 20 20 49 66  ult setting.  If
3f2ea 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
3f2eb 65 20 69 73 20 31 2c 20 74 68 65 6e 20 41 4c 57  e is 1, then ALW
3f2ec 41 59 53 28 29 20 69 73 20 65 69 74 68 65 72 0a  AYS() is either.
3f2ed 20 20 20 20 2a 2a 20 68 61 72 64 2d 63 6f 64 65      ** hard-code
3f2ee 64 20 74 6f 20 74 72 75 65 20 6f 72 20 65 6c 73  d to true or els
3f2ef 65 20 69 74 20 61 73 73 65 72 74 73 20 69 66 20  e it asserts if 
3f2f0 69 74 73 20 61 72 67 75 6d 65 6e 74 20 69 73 20  its argument is 
3f2f1 66 61 6c 73 65 2e 0a 20 20 20 20 2a 2a 20 54 68  false..    ** Th
3f2f2 65 20 66 69 72 73 74 20 62 65 68 61 76 69 6f 72  e first behavior
3f2f3 20 28 68 61 72 64 2d 63 6f 64 65 64 20 74 6f 20   (hard-coded to 
3f2f4 74 72 75 65 29 20 69 73 20 74 68 65 20 63 61 73  true) is the cas
3f2f5 65 20 69 66 0a 20 20 20 20 2a 2a 20 53 51 4c 49  e if.    ** SQLI
3f2f6 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45  TE_TESTCTRL_ASSE
3f2f7 52 54 20 73 68 6f 77 73 20 74 68 61 74 20 61 73  RT shows that as
3f2f8 73 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c  sert() is disabl
3f2f9 65 64 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  ed and the secon
3f2fa 64 0a 20 20 20 20 2a 2a 20 62 65 68 61 76 69 6f  d.    ** behavio
3f2fb 72 20 28 61 73 73 65 72 74 20 69 66 20 74 68 65  r (assert if the
3f2fc 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 41 4c 57   argument to ALW
3f2fd 41 59 53 28 29 20 69 73 20 66 61 6c 73 65 29 20  AYS() is false) 
3f2fe 69 73 20 74 68 65 20 63 61 73 65 20 69 66 0a 20  is the case if. 
3f2ff 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 54 45 53     ** SQLITE_TES
3f300 54 43 54 52 4c 5f 41 53 53 45 52 54 20 73 68 6f  TCTRL_ASSERT sho
3f301 77 73 20 74 68 61 74 20 61 73 73 65 72 74 28 29  ws that assert()
3f302 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a 20 20 20   is enabled..   
3f303 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72   **.    ** The r
3f304 75 6e 2d 74 69 6d 65 20 74 65 73 74 20 70 72 6f  un-time test pro
3f305 63 65 64 75 72 65 20 6d 69 67 68 74 20 6c 6f 6f  cedure might loo
3f306 6b 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65  k something like
3f307 20 74 68 69 73 3a 0a 20 20 20 20 2a 2a 0a 20 20   this:.    **.  
3f308 20 20 2a 2a 20 20 20 20 69 66 28 20 73 71 6c 69    **    if( sqli
3f309 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
3f30a 28 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c  (SQLITE_TESTCTRL
3f30b 5f 41 4c 57 41 59 53 2c 20 32 29 3d 3d 32 20 29  _ALWAYS, 2)==2 )
3f30c 7b 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 2f 2f  {.    **      //
3f30d 20 41 4c 57 41 59 53 28 29 20 61 6e 64 20 4e 45   ALWAYS() and NE
3f30e 56 45 52 28 29 20 61 72 65 20 6e 6f 2d 6f 70 20  VER() are no-op 
3f30f 70 61 73 73 2d 74 68 72 6f 75 67 68 20 6d 61 63  pass-through mac
3f310 72 6f 73 0a 20 20 20 20 2a 2a 20 20 20 20 7d 65  ros.    **    }e
3f311 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f  lse if( sqlite3_
3f312 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c  test_control(SQL
3f313 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53  ITE_TESTCTRL_ASS
3f314 45 52 54 2c 20 31 29 20 29 7b 0a 20 20 20 20 2a  ERT, 1) ){.    *
3f315 2a 20 20 20 20 20 20 2f 2f 20 41 4c 57 41 59 53  *      // ALWAYS
3f316 28 78 29 20 61 73 73 65 72 74 73 20 74 68 61 74  (x) asserts that
3f317 20 78 20 69 73 20 74 72 75 65 2e 20 4e 45 56 45   x is true. NEVE
3f318 52 28 78 29 20 61 73 73 65 72 74 73 20 78 20 69  R(x) asserts x i
3f319 73 20 66 61 6c 73 65 2e 0a 20 20 20 20 2a 2a 20  s false..    ** 
3f31a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 2a     }else{.    **
3f31b 20 20 20 20 20 20 2f 2f 20 41 4c 57 41 59 53 28        // ALWAYS(
3f31c 78 29 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74  x) is a constant
3f31d 20 31 2e 20 20 4e 45 56 45 52 28 78 29 20 69 73   1.  NEVER(x) is
3f31e 20 61 20 63 6f 6e 73 74 61 6e 74 20 30 2e 0a 20   a constant 0.. 
3f31f 20 20 20 2a 2a 20 20 20 20 7d 0a 20 20 20 20 2a     **    }.    *
3f320 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  /.    case SQLIT
3f321 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57 41 59  E_TESTCTRL_ALWAY
3f322 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 78  S: {.      int x
3f323 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74   = va_arg(ap,int
3f324 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 41 4c  );.      rc = AL
3f325 57 41 59 53 28 78 29 3b 0a 20 20 20 20 20 20 62  WAYS(x);.      b
3f326 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
3f327 20 2f 2a 20 20 20 73 71 6c 69 74 65 33 5f 74 65   /*   sqlite3_te
3f328 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54  st_control(SQLIT
3f329 45 5f 54 45 53 54 43 54 52 4c 5f 52 45 53 45 52  E_TESTCTRL_RESER
3f32a 56 45 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  VE, sqlite3 *db,
3f32b 20 69 6e 74 20 4e 29 0a 20 20 20 20 2a 2a 0a 20   int N).    **. 
3f32c 20 20 20 2a 2a 20 53 65 74 20 74 68 65 20 6e 52     ** Set the nR
3f32d 65 73 65 72 76 65 20 73 69 7a 65 20 74 6f 20 4e  eserve size to N
3f32e 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61   for the main da
3f32f 74 61 62 61 73 65 20 6f 6e 20 74 68 65 20 64 61  tabase on the da
3f330 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 63 6f  tabase.    ** co
3f331 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 0a 20 20 20  nnection db..   
3f332 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c   */.    case SQL
3f333 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 52 45 53  ITE_TESTCTRL_RES
3f334 45 52 56 45 3a 20 7b 0a 20 20 20 20 20 20 73 71  ERVE: {.      sq
3f335 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 61 5f 61  lite3 *db = va_a
3f336 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 2a 29  rg(ap, sqlite3*)
3f337 3b 0a 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20  ;.      int x = 
3f338 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a  va_arg(ap,int);.
3f339 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
3f33a 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
3f33b 74 65 78 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  tex);.      sqli
3f33c 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53  te3BtreeSetPageS
3f33d 69 7a 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70  ize(db->aDb[0].p
3f33e 42 74 2c 20 30 2c 20 78 2c 20 30 29 3b 0a 20 20  Bt, 0, x, 0);.  
3f33f 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
3f340 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
3f341 78 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  x);.      break;
3f342 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 20  .    }..    /*  
3f343 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
3f344 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54  trol(SQLITE_TEST
3f345 43 54 52 4c 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  CTRL_OPTIMIZATIO
3f346 4e 53 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  NS, sqlite3 *db,
3f347 20 69 6e 74 20 4e 29 0a 20 20 20 20 2a 2a 0a 20   int N).    **. 
3f348 20 20 20 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20     ** Enable or 
3f349 64 69 73 61 62 6c 65 20 76 61 72 69 6f 75 73 20  disable various 
3f34a 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 66 6f  optimizations fo
3f34b 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73  r testing purpos
3f34c 65 73 2e 20 20 54 68 65 20 0a 20 20 20 20 2a 2a  es.  The .    **
3f34d 20 61 72 67 75 6d 65 6e 74 20 4e 20 69 73 20 61   argument N is a
3f34e 20 62 69 74 6d 61 73 6b 20 6f 66 20 6f 70 74 69   bitmask of opti
3f34f 6d 69 7a 61 74 69 6f 6e 73 20 74 6f 20 62 65 20  mizations to be 
3f350 64 69 73 61 62 6c 65 64 2e 20 20 46 6f 72 20 6e  disabled.  For n
3f351 6f 72 6d 61 6c 0a 20 20 20 20 2a 2a 20 6f 70 65  ormal.    ** ope
3f352 72 61 74 69 6f 6e 20 4e 20 73 68 6f 75 6c 64 20  ration N should 
3f353 62 65 20 30 2e 20 20 54 68 65 20 69 64 65 61 20  be 0.  The idea 
3f354 69 73 20 74 68 61 74 20 61 20 74 65 73 74 20 70  is that a test p
3f355 72 6f 67 72 61 6d 20 28 6c 69 6b 65 20 74 68 65  rogram (like the
3f356 0a 20 20 20 20 2a 2a 20 53 51 4c 20 4c 6f 67 69  .    ** SQL Logi
3f357 63 20 54 65 73 74 20 6f 72 20 53 4c 54 20 74 65  c Test or SLT te
3f358 73 74 20 6d 6f 64 75 6c 65 29 20 63 61 6e 20 72  st module) can r
3f359 75 6e 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20  un the same SQL 
3f35a 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 0a 20  multiple times. 
3f35b 20 20 20 2a 2a 20 77 69 74 68 20 76 61 72 69 6f     ** with vario
3f35c 75 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73  us optimizations
3f35d 20 64 69 73 61 62 6c 65 64 20 74 6f 20 76 65 72   disabled to ver
3f35e 69 66 79 20 74 68 61 74 20 74 68 65 20 73 61 6d  ify that the sam
3f35f 65 20 61 6e 73 77 65 72 0a 20 20 20 20 2a 2a 20  e answer.    ** 
3f360 69 73 20 6f 62 74 61 69 6e 65 64 20 69 6e 20 65  is obtained in e
3f361 76 65 72 79 20 63 61 73 65 2e 0a 20 20 20 20 2a  very case..    *
3f362 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  /.    case SQLIT
3f363 45 5f 54 45 53 54 43 54 52 4c 5f 4f 50 54 49 4d  E_TESTCTRL_OPTIM
3f364 49 5a 41 54 49 4f 4e 53 3a 20 7b 0a 20 20 20 20  IZATIONS: {.    
3f365 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
3f366 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74  va_arg(ap, sqlit
3f367 65 33 2a 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e  e3*);.      db->
3f368 64 62 4f 70 74 46 6c 61 67 73 20 3d 20 28 75 31  dbOptFlags = (u1
3f369 36 29 28 76 61 5f 61 72 67 28 61 70 2c 20 69 6e  6)(va_arg(ap, in
3f36a 74 29 20 26 20 30 78 66 66 66 66 29 3b 0a 20 20  t) & 0xffff);.  
3f36b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
3f36c 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
3f36d 4e 5f 4b 45 59 57 4f 52 44 0a 20 20 20 20 2f 2a  N_KEYWORD.    /*
3f36e 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
3f36f 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53  ntrol(SQLITE_TES
3f370 54 43 54 52 4c 5f 49 53 4b 45 59 57 4f 52 44 2c  TCTRL_ISKEYWORD,
3f371 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 57 6f   const char *zWo
3f372 72 64 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  rd).    **.    *
3f373 2a 20 49 66 20 7a 57 6f 72 64 20 69 73 20 61 20  * If zWord is a 
3f374 6b 65 79 77 6f 72 64 20 72 65 63 6f 67 6e 69 7a  keyword recogniz
3f375 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
3f376 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68  , then return th
3f377 65 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20  e.    ** number 
3f378 6f 66 20 6b 65 79 77 6f 72 64 73 2e 20 20 4f 72  of keywords.  Or
3f379 20 69 66 20 7a 57 6f 72 64 20 69 73 20 6e 6f 74   if zWord is not
3f37a 20 61 20 6b 65 79 77 6f 72 64 2c 20 72 65 74 75   a keyword, retu
3f37b 72 6e 20 30 2e 0a 20 20 20 20 2a 2a 20 0a 20 20  rn 0..    ** .  
3f37c 20 20 2a 2a 20 54 68 69 73 20 74 65 73 74 20 66    ** This test f
3f37d 65 61 74 75 72 65 20 69 73 20 6f 6e 6c 79 20 61  eature is only a
3f37e 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20  vailable in the 
3f37f 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 73 69 6e  amalgamation sin
3f380 63 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 53 51  ce.    ** the SQ
3f381 4c 49 54 45 5f 4e 5f 4b 45 59 57 4f 52 44 20 6d  LITE_N_KEYWORD m
3f382 61 63 72 6f 20 69 73 20 6e 6f 74 20 64 65 66 69  acro is not defi
3f383 6e 65 64 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ned in this file
3f384 20 69 66 20 53 51 4c 69 74 65 0a 20 20 20 20 2a   if SQLite.    *
3f385 2a 20 69 73 20 62 75 69 6c 74 20 75 73 69 6e 67  * is built using
3f386 20 73 65 70 61 72 61 74 65 20 73 6f 75 72 63 65   separate source
3f387 20 66 69 6c 65 73 2e 0a 20 20 20 20 2a 2f 0a 20   files..    */. 
3f388 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54     case SQLITE_T
3f389 45 53 54 43 54 52 4c 5f 49 53 4b 45 59 57 4f 52  ESTCTRL_ISKEYWOR
3f38a 44 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  D: {.      const
3f38b 20 63 68 61 72 20 2a 7a 57 6f 72 64 20 3d 20 76   char *zWord = v
3f38c 61 5f 61 72 67 28 61 70 2c 20 63 6f 6e 73 74 20  a_arg(ap, const 
3f38d 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 69 6e  char*);.      in
3f38e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  t n = sqlite3Str
3f38f 6c 65 6e 33 30 28 7a 57 6f 72 64 29 3b 0a 20 20  len30(zWord);.  
3f390 20 20 20 20 72 63 20 3d 20 28 73 71 6c 69 74 65      rc = (sqlite
3f391 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 28 75 38  3KeywordCode((u8
3f392 2a 29 7a 57 6f 72 64 2c 20 6e 29 21 3d 54 4b 5f  *)zWord, n)!=TK_
3f393 49 44 29 20 3f 20 53 51 4c 49 54 45 5f 4e 5f 4b  ID) ? SQLITE_N_K
3f394 45 59 57 4f 52 44 20 3a 20 30 3b 0a 20 20 20 20  EYWORD : 0;.    
3f395 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
3f396 65 6e 64 69 66 20 0a 0a 20 20 20 20 2f 2a 20 73  endif ..    /* s
3f397 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
3f398 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54 43  rol(SQLITE_TESTC
3f399 54 52 4c 5f 53 43 52 41 54 43 48 4d 41 4c 4c 4f  TRL_SCRATCHMALLO
3f39a 43 2c 20 73 7a 2c 20 26 70 4e 65 77 2c 20 70 46  C, sz, &pNew, pF
3f39b 72 65 65 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20  ree);.    **.   
3f39c 20 2a 2a 20 50 61 73 73 20 70 46 72 65 65 20 69   ** Pass pFree i
3f39d 6e 74 6f 20 73 71 6c 69 74 65 33 53 63 72 61 74  nto sqlite3Scrat
3f39e 63 68 46 72 65 65 28 29 2e 20 0a 20 20 20 20 2a  chFree(). .    *
3f39f 2a 20 49 66 20 73 7a 3e 30 20 74 68 65 6e 20 61  * If sz>0 then a
3f3a0 6c 6c 6f 63 61 74 65 20 61 20 73 63 72 61 74 63  llocate a scratc
3f3a1 68 20 62 75 66 66 65 72 20 69 6e 74 6f 20 70 4e  h buffer into pN
3f3a2 65 77 2e 20 20 0a 20 20 20 20 2a 2f 0a 20 20 20  ew.  .    */.   
3f3a3 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53   case SQLITE_TES
3f3a4 54 43 54 52 4c 5f 53 43 52 41 54 43 48 4d 41 4c  TCTRL_SCRATCHMAL
3f3a5 4c 4f 43 3a 20 7b 0a 20 20 20 20 20 20 76 6f 69  LOC: {.      voi
3f3a6 64 20 2a 70 46 72 65 65 2c 20 2a 2a 70 70 4e 65  d *pFree, **ppNe
3f3a7 77 3b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 3b  w;.      int sz;
3f3a8 0a 20 20 20 20 20 20 73 7a 20 3d 20 76 61 5f 61  .      sz = va_a
3f3a9 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
3f3aa 20 20 20 70 70 4e 65 77 20 3d 20 76 61 5f 61 72     ppNew = va_ar
3f3ab 67 28 61 70 2c 20 76 6f 69 64 2a 2a 29 3b 0a 20  g(ap, void**);. 
3f3ac 20 20 20 20 20 70 46 72 65 65 20 3d 20 76 61 5f       pFree = va_
3f3ad 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a  arg(ap, void*);.
3f3ae 20 20 20 20 20 20 69 66 28 20 73 7a 20 29 20 2a        if( sz ) *
3f3af 70 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 53  ppNew = sqlite3S
3f3b0 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 73 7a 29  cratchMalloc(sz)
3f3b1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  ;.      sqlite3S
3f3b2 63 72 61 74 63 68 46 72 65 65 28 70 46 72 65 65  cratchFree(pFree
3f3b3 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
3f3b4 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 20 20      }..    /*   
3f3b5 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
3f3b6 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54  trol(SQLITE_TEST
3f3b7 43 54 52 4c 5f 4c 4f 43 41 4c 54 49 4d 45 5f 46  CTRL_LOCALTIME_F
3f3b8 41 55 4c 54 2c 20 69 6e 74 20 6f 6e 6f 66 66 29  AULT, int onoff)
3f3b9 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ;.    **.    ** 
3f3ba 49 66 20 70 61 72 61 6d 65 74 65 72 20 6f 6e 6f  If parameter ono
3f3bb 66 66 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  ff is non-zero, 
3f3bc 63 6f 6e 66 69 67 75 72 65 20 74 68 65 20 77 72  configure the wr
3f3bd 61 70 70 65 72 73 20 73 6f 20 74 68 61 74 20 61  appers so that a
3f3be 6c 6c 0a 20 20 20 20 2a 2a 20 73 75 62 73 65 71  ll.    ** subseq
3f3bf 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 6c 6f  uent calls to lo
3f3c0 63 61 6c 74 69 6d 65 28 29 20 61 6e 64 20 76 61  caltime() and va
3f3c1 72 69 61 6e 74 73 20 66 61 69 6c 2e 20 49 66 20  riants fail. If 
3f3c2 6f 6e 6f 66 66 20 69 73 20 7a 65 72 6f 2c 0a 20  onoff is zero,. 
3f3c3 20 20 20 2a 2a 20 75 6e 64 6f 20 74 68 69 73 20     ** undo this 
3f3c4 73 65 74 74 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a  setting..    */.
3f3c5 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
3f3c6 54 45 53 54 43 54 52 4c 5f 4c 4f 43 41 4c 54 49  TESTCTRL_LOCALTI
3f3c7 4d 45 5f 46 41 55 4c 54 3a 20 7b 0a 20 20 20 20  ME_FAULT: {.    
3f3c8 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
3f3c9 6f 6e 66 69 67 2e 62 4c 6f 63 61 6c 74 69 6d 65  onfig.bLocaltime
3f3ca 46 61 75 6c 74 20 3d 20 76 61 5f 61 72 67 28 61  Fault = va_arg(a
3f3cb 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 62  p, int);.      b
3f3cc 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66  reak;.    }..#if
3f3cd 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
3f3ce 45 4e 41 42 4c 45 5f 54 52 45 45 5f 45 58 50 4c  ENABLE_TREE_EXPL
3f3cf 41 49 4e 29 0a 20 20 20 20 2f 2a 20 20 20 73 71  AIN).    /*   sq
3f3d0 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72  lite3_test_contr
3f3d1 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ol(SQLITE_TESTCT
3f3d2 52 4c 5f 45 58 50 4c 41 49 4e 5f 53 54 4d 54 2c  RL_EXPLAIN_STMT,
3f3d3 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
3f3d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
3f3d5 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 63 6f 6e  qlite3_stmt*,con
3f3d6 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20 20 20 20  st char**);.    
3f3d7 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 63 6f 6d  **.    ** If com
3f3d8 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54  piled with SQLIT
3f3d9 45 5f 45 4e 41 42 4c 45 5f 54 52 45 45 5f 45 58  E_ENABLE_TREE_EX
3f3da 50 4c 41 49 4e 2c 20 65 61 63 68 20 73 71 6c 69  PLAIN, each sqli
3f3db 74 65 33 5f 73 74 6d 74 20 68 6f 6c 64 73 0a 20  te3_stmt holds. 
3f3dc 20 20 20 2a 2a 20 61 20 73 74 72 69 6e 67 20 74     ** a string t
3f3dd 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68  hat describes th
3f3de 65 20 6f 70 74 69 6d 69 7a 65 64 20 70 61 72 73  e optimized pars
3f3df 65 20 74 72 65 65 2e 20 20 54 68 69 73 20 74 65  e tree.  This te
3f3e0 73 74 2d 63 6f 6e 74 72 6f 6c 0a 20 20 20 20 2a  st-control.    *
3f3e1 2a 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  * returns a poin
3f3e2 74 65 72 20 74 6f 20 74 68 61 74 20 73 74 72 69  ter to that stri
3f3e3 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ng..    */.    c
3f3e4 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ase SQLITE_TESTC
3f3e5 54 52 4c 5f 45 58 50 4c 41 49 4e 5f 53 54 4d 54  TRL_EXPLAIN_STMT
3f3e6 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
3f3e7 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20  3_stmt *pStmt = 
3f3e8 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74  va_arg(ap, sqlit
3f3e9 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 20 20 20  e3_stmt*);.     
3f3ea 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a   const char **pz
3f3eb 52 65 74 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  Ret = va_arg(ap,
3f3ec 20 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a   const char**);.
3f3ed 20 20 20 20 20 20 2a 70 7a 52 65 74 20 3d 20 73        *pzRet = s
3f3ee 71 6c 69 74 65 33 56 64 62 65 45 78 70 6c 61 6e  qlite3VdbeExplan
3f3ef 61 74 69 6f 6e 28 28 56 64 62 65 2a 29 70 53 74  ation((Vdbe*)pSt
3f3f0 6d 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  mt);.      break
3f3f1 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ;.    }.#endif..
3f3f2 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70 29    }.  va_end(ap)
3f3f3 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ;.#endif /* SQLI
3f3f4 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f  TE_OMIT_BUILTIN_
3f3f5 54 45 53 54 20 2a 2f 0a 20 20 72 65 74 75 72 6e  TEST */.  return
3f3f6 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
3f3f7 69 73 20 69 73 20 61 20 75 74 69 6c 69 74 79 20  is is a utility 
3f3f8 72 6f 75 74 69 6e 65 2c 20 75 73 65 66 75 6c 20  routine, useful 
3f3f9 74 6f 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74  to VFS implement
3f3fa 61 74 69 6f 6e 73 2c 20 74 68 61 74 20 63 68 65  ations, that che
3f3fb 63 6b 73 0a 2a 2a 20 74 6f 20 73 65 65 20 69 66  cks.** to see if
3f3fc 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
3f3fd 20 77 61 73 20 61 20 55 52 49 20 74 68 61 74 20   was a URI that 
3f3fe 63 6f 6e 74 61 69 6e 65 64 20 61 20 73 70 65 63  contained a spec
3f3ff 69 66 69 63 20 71 75 65 72 79 20 0a 2a 2a 20 70  ific query .** p
3f400 61 72 61 6d 65 74 65 72 2c 20 61 6e 64 20 69 66  arameter, and if
3f401 20 73 6f 20 6f 62 74 61 69 6e 73 20 74 68 65 20   so obtains the 
3f402 76 61 6c 75 65 20 6f 66 20 74 68 65 20 71 75 65  value of the que
3f403 72 79 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a  ry parameter..**
3f404 0a 2a 2a 20 54 68 65 20 7a 46 69 6c 65 6e 61 6d  .** The zFilenam
3f405 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  e argument is th
3f406 65 20 66 69 6c 65 6e 61 6d 65 20 70 6f 69 6e 74  e filename point
3f407 65 72 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74  er passed into t
3f408 68 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 6d 65  he xOpen().** me
3f409 74 68 6f 64 20 6f 66 20 61 20 56 46 53 20 69 6d  thod of a VFS im
3f40a 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54  plementation.  T
3f40b 68 65 20 7a 50 61 72 61 6d 20 61 72 67 75 6d 65  he zParam argume
3f40c 6e 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  nt is the name o
3f40d 66 20 74 68 65 0a 2a 2a 20 71 75 65 72 79 20 70  f the.** query p
3f40e 61 72 61 6d 65 74 65 72 20 77 65 20 73 65 65 6b  arameter we seek
3f40f 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
3f410 72 65 74 75 72 6e 73 20 74 68 65 20 76 61 6c 75  returns the valu
3f411 65 20 6f 66 20 74 68 65 20 7a 50 61 72 61 6d 0a  e of the zParam.
3f412 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 66 20  ** parameter if 
3f413 69 74 20 65 78 69 73 74 73 2e 20 20 49 66 20 74  it exists.  If t
3f414 68 65 20 70 61 72 61 6d 65 74 65 72 20 64 6f 65  he parameter doe
3f415 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 74 68 69  s not exist, thi
3f416 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74  s routine.** ret
3f417 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  urns a NULL poin
3f418 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ter..*/.SQLITE_A
3f419 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
3f41a 71 6c 69 74 65 33 5f 75 72 69 5f 70 61 72 61 6d  qlite3_uri_param
3f41b 65 74 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20  eter(const char 
3f41c 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 63 6f 6e 73  *zFilename, cons
3f41d 74 20 63 68 61 72 20 2a 7a 50 61 72 61 6d 29 7b  t char *zParam){
3f41e 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65  .  if( zFilename
3f41f 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
3f420 20 20 7a 46 69 6c 65 6e 61 6d 65 20 2b 3d 20 73    zFilename += s
3f421 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
3f422 46 69 6c 65 6e 61 6d 65 29 20 2b 20 31 3b 0a 20  Filename) + 1;. 
3f423 20 77 68 69 6c 65 28 20 7a 46 69 6c 65 6e 61 6d   while( zFilenam
3f424 65 5b 30 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20  e[0] ){.    int 
3f425 78 20 3d 20 73 74 72 63 6d 70 28 7a 46 69 6c 65  x = strcmp(zFile
3f426 6e 61 6d 65 2c 20 7a 50 61 72 61 6d 29 3b 0a 20  name, zParam);. 
3f427 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 2b 3d 20     zFilename += 
3f428 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
3f429 7a 46 69 6c 65 6e 61 6d 65 29 20 2b 20 31 3b 0a  zFilename) + 1;.
3f42a 20 20 20 20 69 66 28 20 78 3d 3d 30 20 29 20 72      if( x==0 ) r
3f42b 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b  eturn zFilename;
3f42c 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 2b  .    zFilename +
3f42d 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
3f42e 30 28 7a 46 69 6c 65 6e 61 6d 65 29 20 2b 20 31  0(zFilename) + 1
3f42f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
3f430 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
3f431 6e 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c 75  n a boolean valu
3f432 65 20 66 6f 72 20 61 20 71 75 65 72 79 20 70 61  e for a query pa
3f433 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  rameter..*/.SQLI
3f434 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3f435 65 33 5f 75 72 69 5f 62 6f 6f 6c 65 61 6e 28 63  e3_uri_boolean(c
3f436 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
3f437 6e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  name, const char
3f438 20 2a 7a 50 61 72 61 6d 2c 20 69 6e 74 20 62 44   *zParam, int bD
3f439 66 6c 74 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  flt){.  const ch
3f43a 61 72 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 5f  ar *z = sqlite3_
3f43b 75 72 69 5f 70 61 72 61 6d 65 74 65 72 28 7a 46  uri_parameter(zF
3f43c 69 6c 65 6e 61 6d 65 2c 20 7a 50 61 72 61 6d 29  ilename, zParam)
3f43d 3b 0a 20 20 62 44 66 6c 74 20 3d 20 62 44 66 6c  ;.  bDflt = bDfl
3f43e 74 21 3d 30 3b 0a 20 20 72 65 74 75 72 6e 20 7a  t!=0;.  return z
3f43f 20 3f 20 73 71 6c 69 74 65 33 47 65 74 42 6f 6f   ? sqlite3GetBoo
3f440 6c 65 61 6e 28 7a 2c 20 62 44 66 6c 74 29 20 3a  lean(z, bDflt) :
3f441 20 62 44 66 6c 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   bDflt;.}../*.**
3f442 20 52 65 74 75 72 6e 20 61 20 36 34 2d 62 69 74   Return a 64-bit
3f443 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 66   integer value f
3f444 6f 72 20 61 20 71 75 65 72 79 20 70 61 72 61 6d  or a query param
3f445 65 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eter..*/.SQLITE_
3f446 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  API sqlite3_int6
3f447 34 20 73 71 6c 69 74 65 33 5f 75 72 69 5f 69 6e  4 sqlite3_uri_in
3f448 74 36 34 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  t64(.  const cha
3f449 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20  r *zFilename,   
3f44a 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 61 73 20   /* Filename as 
3f44b 70 61 73 73 65 64 20 74 6f 20 78 4f 70 65 6e 20  passed to xOpen 
3f44c 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
3f44d 2a 7a 50 61 72 61 6d 2c 20 20 20 20 20 20 20 2f  *zParam,       /
3f44e 2a 20 55 52 49 20 70 61 72 61 6d 65 74 65 72 20  * URI parameter 
3f44f 73 6f 75 67 68 74 20 2a 2f 0a 20 20 73 71 6c 69  sought */.  sqli
3f450 74 65 33 5f 69 6e 74 36 34 20 62 44 66 6c 74 20  te3_int64 bDflt 
3f451 20 20 20 20 20 20 2f 2a 20 72 65 74 75 72 6e 20        /* return 
3f452 69 66 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  if parameter is 
3f453 6d 69 73 73 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20  missing */.){.  
3f454 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
3f455 73 71 6c 69 74 65 33 5f 75 72 69 5f 70 61 72 61  sqlite3_uri_para
3f456 6d 65 74 65 72 28 7a 46 69 6c 65 6e 61 6d 65 2c  meter(zFilename,
3f457 20 7a 50 61 72 61 6d 29 3b 0a 20 20 73 71 6c 69   zParam);.  sqli
3f458 74 65 33 5f 69 6e 74 36 34 20 76 3b 0a 20 20 69  te3_int64 v;.  i
3f459 66 28 20 7a 20 26 26 20 73 71 6c 69 74 65 33 41  f( z && sqlite3A
3f45a 74 6f 69 36 34 28 7a 2c 20 26 76 2c 20 73 71 6c  toi64(z, &v, sql
3f45b 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 2c  ite3Strlen30(z),
3f45c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3d 3d 53   SQLITE_UTF8)==S
3f45d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3f45e 62 44 66 6c 74 20 3d 20 76 3b 0a 20 20 7d 0a 20  bDflt = v;.  }. 
3f45f 20 72 65 74 75 72 6e 20 62 44 66 6c 74 3b 0a 7d   return bDflt;.}
3f460 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
3f461 68 65 20 42 74 72 65 65 20 70 6f 69 6e 74 65 72  he Btree pointer
3f462 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 7a   identified by z
3f463 44 62 4e 61 6d 65 2e 20 20 52 65 74 75 72 6e 20  DbName.  Return 
3f464 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e  NULL if not foun
3f465 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
3f466 56 41 54 45 20 42 74 72 65 65 20 2a 73 71 6c 69  VATE Btree *sqli
3f467 74 65 33 44 62 4e 61 6d 65 54 6f 42 74 72 65 65  te3DbNameToBtree
3f468 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
3f469 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d  nst char *zDbNam
3f46a 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66  e){.  int i;.  f
3f46b 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
3f46c 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  b; i++){.    if(
3f46d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 0a   db->aDb[i].pBt.
3f46e 20 20 20 20 20 26 26 20 28 7a 44 62 4e 61 6d 65       && (zDbName
3f46f 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 53 74  ==0 || sqlite3St
3f470 72 49 43 6d 70 28 7a 44 62 4e 61 6d 65 2c 20 64  rICmp(zDbName, d
3f471 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 29  b->aDb[i].zName)
3f472 3d 3d 30 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  ==0).    ){.    
3f473 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 61 44 62    return db->aDb
3f474 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 7d 0a 20  [i].pBt;.    }. 
3f475 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
3f476 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
3f477 68 65 20 66 69 6c 65 6e 61 6d 65 20 6f 66 20 74  he filename of t
3f478 68 65 20 64 61 74 61 62 61 73 65 20 61 73 73 6f  he database asso
3f479 63 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61  ciated with a da
3f47a 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63  tabase.** connec
3f47b 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
3f47c 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
3f47d 73 71 6c 69 74 65 33 5f 64 62 5f 66 69 6c 65 6e  sqlite3_db_filen
3f47e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ame(sqlite3 *db,
3f47f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
3f480 4e 61 6d 65 29 7b 0a 20 20 42 74 72 65 65 20 2a  Name){.  Btree *
3f481 70 42 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4e  pBt = sqlite3DbN
3f482 61 6d 65 54 6f 42 74 72 65 65 28 64 62 2c 20 7a  ameToBtree(db, z
3f483 44 62 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72  DbName);.  retur
3f484 6e 20 70 42 74 20 3f 20 73 71 6c 69 74 65 33 42  n pBt ? sqlite3B
3f485 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28  treeGetFilename(
3f486 70 42 74 29 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a  pBt) : 0;.}../*.
3f487 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20 64  ** Return 1 if d
3f488 61 74 61 62 61 73 65 20 69 73 20 72 65 61 64 2d  atabase is read-
3f489 6f 6e 6c 79 20 6f 72 20 30 20 69 66 20 72 65 61  only or 0 if rea
3f48a 64 2f 77 72 69 74 65 2e 20 20 52 65 74 75 72 6e  d/write.  Return
3f48b 20 2d 31 20 69 66 0a 2a 2a 20 6e 6f 20 73 75 63   -1 if.** no suc
3f48c 68 20 64 61 74 61 62 61 73 65 20 65 78 69 73 74  h database exist
3f48d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
3f48e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f   int sqlite3_db_
3f48f 72 65 61 64 6f 6e 6c 79 28 73 71 6c 69 74 65 33  readonly(sqlite3
3f490 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
3f491 20 2a 7a 44 62 4e 61 6d 65 29 7b 0a 20 20 42 74   *zDbName){.  Bt
3f492 72 65 65 20 2a 70 42 74 20 3d 20 73 71 6c 69 74  ree *pBt = sqlit
3f493 65 33 44 62 4e 61 6d 65 54 6f 42 74 72 65 65 28  e3DbNameToBtree(
3f494 64 62 2c 20 7a 44 62 4e 61 6d 65 29 3b 0a 20 20  db, zDbName);.  
3f495 72 65 74 75 72 6e 20 70 42 74 20 3f 20 73 71 6c  return pBt ? sql
3f496 69 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f  ite3PagerIsreado
3f497 6e 6c 79 28 73 71 6c 69 74 65 33 42 74 72 65 65  nly(sqlite3Btree
3f498 50 61 67 65 72 28 70 42 74 29 29 20 3a 20 2d 31  Pager(pBt)) : -1
3f499 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
3f49a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61 69 6e  **** End of main
3f49b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
3f49c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f49d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f49e 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
3f49f 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
3f4a0 6e 6f 74 69 66 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a  notify.c *******
3f4a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f4a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f4a3 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20  ***/./*.** 2009 
3f4a4 4d 61 72 63 68 20 33 0a 2a 2a 0a 2a 2a 20 54 68  March 3.**.** Th
3f4a5 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
3f4a6 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
3f4a7 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
3f4a8 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
3f4a9 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
3f4aa 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
3f4ab 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
3f4ac 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
3f4ad 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
3f4ae 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
3f4af 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
3f4b0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
3f4b1 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
3f4b2 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
3f4b3 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
3f4b4 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
3f4b5 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
3f4b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f4b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f4b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f4b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f4ba 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
3f4bb 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
3f4bc 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
3f4bd 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
3f4be 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28  3_unlock_notify(
3f4bf 29 0a 2a 2a 20 41 50 49 20 6d 65 74 68 6f 64 20  ).** API method 
3f4c0 61 6e 64 20 69 74 73 20 61 73 73 6f 63 69 61 74  and its associat
3f4c1 65 64 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79  ed functionality
3f4c2 2e 0a 2a 2f 0a 0a 2f 2a 20 4f 6d 69 74 20 74 68  ..*/../* Omit th
3f4c3 69 73 20 65 6e 74 69 72 65 20 66 69 6c 65 20 69  is entire file i
3f4c4 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
3f4c5 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 20 69 73  UNLOCK_NOTIFY is
3f4c6 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e 20 2a 2f   not defined. */
3f4c7 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
3f4c8 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54  NABLE_UNLOCK_NOT
3f4c9 49 46 59 0a 0a 2f 2a 0a 2a 2a 20 50 75 62 6c 69  IFY../*.** Publi
3f4ca 63 20 69 6e 74 65 72 66 61 63 65 73 3a 0a 2a 2a  c interfaces:.**
3f4cb 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e  .**   sqlite3Con
3f4cc 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 29  nectionBlocked()
3f4cd 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e  .**   sqlite3Con
3f4ce 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28  nectionUnlocked(
3f4cf 29 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f  ).**   sqlite3Co
3f4d0 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 29  nnectionClosed()
3f4d1 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 5f 75 6e  .**   sqlite3_un
3f4d2 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 0a 2a 2f  lock_notify().*/
3f4d3 0a 0a 23 64 65 66 69 6e 65 20 61 73 73 65 72 74  ..#define assert
3f4d4 4d 75 74 65 78 48 65 6c 64 28 29 20 5c 0a 20 20  MutexHeld() \.  
3f4d5 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
3f4d6 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74  mutex_held(sqlit
3f4d7 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
3f4d8 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
3f4d9 5f 4d 41 53 54 45 52 29 29 20 29 0a 0a 2f 2a 0a  _MASTER)) )../*.
3f4da 2a 2a 20 48 65 61 64 20 6f 66 20 61 20 6c 69 6e  ** Head of a lin
3f4db 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  ked list of all 
3f4dc 73 71 6c 69 74 65 33 20 6f 62 6a 65 63 74 73 20  sqlite3 objects 
3f4dd 63 72 65 61 74 65 64 20 62 79 20 74 68 69 73 20  created by this 
3f4de 70 72 6f 63 65 73 73 0a 2a 2a 20 66 6f 72 20 77  process.** for w
3f4df 68 69 63 68 20 65 69 74 68 65 72 20 73 71 6c 69  hich either sqli
3f4e0 74 65 33 2e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e  te3.pBlockingCon
3f4e1 6e 65 63 74 69 6f 6e 20 6f 72 20 73 71 6c 69 74  nection or sqlit
3f4e2 65 33 2e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63  e3.pUnlockConnec
3f4e3 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e  tion.** is not N
3f4e4 55 4c 4c 2e 20 54 68 69 73 20 76 61 72 69 61 62  ULL. This variab
3f4e5 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 61 63 63 65  le may only acce
3f4e6 73 73 65 64 20 77 68 69 6c 65 20 74 68 65 20 53  ssed while the S
3f4e7 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20  TATIC_MASTER.** 
3f4e8 6d 75 74 65 78 20 69 73 20 68 65 6c 64 2e 0a 2a  mutex is held..*
3f4e9 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  /.static sqlite3
3f4ea 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c   *SQLITE_WSD sql
3f4eb 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 20  ite3BlockedList 
3f4ec 3d 20 30 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44  = 0;..#ifndef ND
3f4ed 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  EBUG./*.** This 
3f4ee 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 63 6f  function is a co
3f4ef 6d 70 6c 65 78 20 61 73 73 65 72 74 28 29 20 74  mplex assert() t
3f4f0 68 61 74 20 76 65 72 69 66 69 65 73 20 74 68 65  hat verifies the
3f4f1 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 70   following .** p
3f4f2 72 6f 70 65 72 74 69 65 73 20 6f 66 20 74 68 65  roperties of the
3f4f3 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
3f4f4 69 6f 6e 73 20 6c 69 73 74 3a 0a 2a 2a 0a 2a 2a  ions list:.**.**
3f4f5 20 20 20 31 29 20 45 61 63 68 20 65 6e 74 72 79     1) Each entry
3f4f6 20 69 6e 20 74 68 65 20 6c 69 73 74 20 68 61 73   in the list has
3f4f7 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75   a non-NULL valu
3f4f8 65 20 66 6f 72 20 65 69 74 68 65 72 20 0a 2a 2a  e for either .**
3f4f9 20 20 20 20 20 20 70 55 6e 6c 6f 63 6b 43 6f 6e        pUnlockCon
3f4fa 6e 65 63 74 69 6f 6e 20 6f 72 20 70 42 6c 6f 63  nection or pBloc
3f4fb 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 2c 20  kingConnection, 
3f4fc 6f 72 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 20  or both..**.**  
3f4fd 20 32 29 20 41 6c 6c 20 65 6e 74 72 69 65 73 20   2) All entries 
3f4fe 69 6e 20 74 68 65 20 6c 69 73 74 20 74 68 61 74  in the list that
3f4ff 20 73 68 61 72 65 20 61 20 63 6f 6d 6d 6f 6e 20   share a common 
3f500 76 61 6c 75 65 20 66 6f 72 20 0a 2a 2a 20 20 20  value for .**   
3f501 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79     xUnlockNotify
3f502 20 61 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67   are grouped tog
3f503 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 33  ether..**.**   3
3f504 29 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e  ) If the argumen
3f505 74 20 64 62 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  t db is not NULL
3f506 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74  , then none of t
3f507 68 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  he entries in th
3f508 65 0a 2a 2a 20 20 20 20 20 20 62 6c 6f 63 6b 65  e.**      blocke
3f509 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69  d connections li
3f50a 73 74 20 68 61 76 65 20 70 55 6e 6c 6f 63 6b 43  st have pUnlockC
3f50b 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 42 6c  onnection or pBl
3f50c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
3f50d 0a 2a 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20  .**      set to 
3f50e 64 62 2e 20 54 68 69 73 20 69 73 20 75 73 65 64  db. This is used
3f50f 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20 63 6f   when closing co
3f510 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 0a 2a 2f 0a  nnection db..*/.
3f511 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63  static void chec
3f512 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65 73 28  kListProperties(
3f513 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
3f514 73 71 6c 69 74 65 33 20 2a 70 3b 0a 20 20 66 6f  sqlite3 *p;.  fo
3f515 72 28 70 3d 73 71 6c 69 74 65 33 42 6c 6f 63 6b  r(p=sqlite3Block
3f516 65 64 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e  edList; p; p=p->
3f517 70 4e 65 78 74 42 6c 6f 63 6b 65 64 29 7b 0a 20  pNextBlocked){. 
3f518 20 20 20 69 6e 74 20 73 65 65 6e 20 3d 20 30 3b     int seen = 0;
3f519 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 70 32  .    sqlite3 *p2
3f51a 3b 0a 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79  ;..    /* Verify
3f51b 20 70 72 6f 70 65 72 74 79 20 28 31 29 20 2a 2f   property (1) */
3f51c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
3f51d 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f  pUnlockConnectio
3f51e 6e 20 7c 7c 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e  n || p->pBlockin
3f51f 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 29 3b 0a 0a  gConnection );..
3f520 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 70 72      /* Verify pr
3f521 6f 70 65 72 74 79 20 28 32 29 20 2a 2f 0a 20 20  operty (2) */.  
3f522 20 20 66 6f 72 28 70 32 3d 73 71 6c 69 74 65 33    for(p2=sqlite3
3f523 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 70 32 21  BlockedList; p2!
3f524 3d 70 3b 20 70 32 3d 70 32 2d 3e 70 4e 65 78 74  =p; p2=p2->pNext
3f525 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20 20 20 20  Blocked){.      
3f526 69 66 28 20 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e  if( p2->xUnlockN
3f527 6f 74 69 66 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63  otify==p->xUnloc
3f528 6b 4e 6f 74 69 66 79 20 29 20 73 65 65 6e 20 3d  kNotify ) seen =
3f529 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   1;.      assert
3f52a 28 20 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74  ( p2->xUnlockNot
3f52b 69 66 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63 6b 4e  ify==p->xUnlockN
3f52c 6f 74 69 66 79 20 7c 7c 20 21 73 65 65 6e 20 29  otify || !seen )
3f52d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
3f52e 64 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70 55 6e 6c  db==0 || p->pUnl
3f52f 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 21 3d 64  ockConnection!=d
3f530 62 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  b );.      asser
3f531 74 28 20 64 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70  t( db==0 || p->p
3f532 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
3f533 6f 6e 21 3d 64 62 20 29 3b 0a 20 20 20 20 7d 0a  on!=db );.    }.
3f534 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65    }.}.#else.# de
3f535 66 69 6e 65 20 63 68 65 63 6b 4c 69 73 74 50 72  fine checkListPr
3f536 6f 70 65 72 74 69 65 73 28 78 29 0a 23 65 6e 64  operties(x).#end
3f537 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  if../*.** Remove
3f538 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 66   connection db f
3f539 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65 64 20  rom the blocked 
3f53a 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74  connections list
3f53b 2e 20 49 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  . If connection.
3f53c 2a 2a 20 64 62 20 69 73 20 6e 6f 74 20 63 75 72  ** db is not cur
3f53d 72 65 6e 74 6c 79 20 61 20 70 61 72 74 20 6f 66  rently a part of
3f53e 20 74 68 65 20 6c 69 73 74 2c 20 74 68 69 73 20   the list, this 
3f53f 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
3f540 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  -op..*/.static v
3f541 6f 69 64 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c  oid removeFromBl
3f542 6f 63 6b 65 64 4c 69 73 74 28 73 71 6c 69 74 65  ockedList(sqlite
3f543 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 65  3 *db){.  sqlite
3f544 33 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72 74  3 **pp;.  assert
3f545 4d 75 74 65 78 48 65 6c 64 28 29 3b 0a 20 20 66  MutexHeld();.  f
3f546 6f 72 28 70 70 3d 26 73 71 6c 69 74 65 33 42 6c  or(pp=&sqlite3Bl
3f547 6f 63 6b 65 64 4c 69 73 74 3b 20 2a 70 70 3b 20  ockedList; *pp; 
3f548 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65  pp = &(*pp)->pNe
3f549 78 74 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20 20  xtBlocked){.    
3f54a 69 66 28 20 2a 70 70 3d 3d 64 62 20 29 7b 0a 20  if( *pp==db ){. 
3f54b 20 20 20 20 20 2a 70 70 20 3d 20 28 2a 70 70 29       *pp = (*pp)
3f54c 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 3b 0a  ->pNextBlocked;.
3f54d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3f54e 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
3f54f 41 64 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64  Add connection d
3f550 62 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 65 64  b to the blocked
3f551 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73   connections lis
3f552 74 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64  t. It is assumed
3f553 0a 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20 6e  .** that it is n
3f554 6f 74 20 61 6c 72 65 61 64 79 20 61 20 70 61 72  ot already a par
3f555 74 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 0a 2a  t of the list..*
3f556 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 64  /.static void ad
3f557 64 54 6f 42 6c 6f 63 6b 65 64 4c 69 73 74 28 73  dToBlockedList(s
3f558 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73  qlite3 *db){.  s
3f559 71 6c 69 74 65 33 20 2a 2a 70 70 3b 0a 20 20 61  qlite3 **pp;.  a
3f55a 73 73 65 72 74 4d 75 74 65 78 48 65 6c 64 28 29  ssertMutexHeld()
3f55b 3b 0a 20 20 66 6f 72 28 0a 20 20 20 20 70 70 3d  ;.  for(.    pp=
3f55c 26 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c  &sqlite3BlockedL
3f55d 69 73 74 3b 20 0a 20 20 20 20 2a 70 70 20 26 26  ist; .    *pp &&
3f55e 20 28 2a 70 70 29 2d 3e 78 55 6e 6c 6f 63 6b 4e   (*pp)->xUnlockN
3f55f 6f 74 69 66 79 21 3d 64 62 2d 3e 78 55 6e 6c 6f  otify!=db->xUnlo
3f560 63 6b 4e 6f 74 69 66 79 3b 20 0a 20 20 20 20 70  ckNotify; .    p
3f561 70 3d 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 42  p=&(*pp)->pNextB
3f562 6c 6f 63 6b 65 64 0a 20 20 29 3b 0a 20 20 64 62  locked.  );.  db
3f563 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 20 3d  ->pNextBlocked =
3f564 20 2a 70 70 3b 0a 20 20 2a 70 70 20 3d 20 64 62   *pp;.  *pp = db
3f565 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69  ;.}../*.** Obtai
3f566 6e 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53  n the STATIC_MAS
3f567 54 45 52 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74  TER mutex..*/.st
3f568 61 74 69 63 20 76 6f 69 64 20 65 6e 74 65 72 4d  atic void enterM
3f569 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71  utex(void){.  sq
3f56a 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
3f56b 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  r(sqlite3MutexAl
3f56c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
3f56d 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29  _STATIC_MASTER))
3f56e 3b 0a 20 20 63 68 65 63 6b 4c 69 73 74 50 72 6f  ;.  checkListPro
3f56f 70 65 72 74 69 65 73 28 30 29 3b 0a 7d 0a 0a 2f  perties(0);.}../
3f570 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 74 68 65  *.** Release the
3f571 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d   STATIC_MASTER m
3f572 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  utex..*/.static 
3f573 76 6f 69 64 20 6c 65 61 76 65 4d 75 74 65 78 28  void leaveMutex(
3f574 76 6f 69 64 29 7b 0a 20 20 61 73 73 65 72 74 4d  void){.  assertM
3f575 75 74 65 78 48 65 6c 64 28 29 3b 0a 20 20 63 68  utexHeld();.  ch
3f576 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65  eckListPropertie
3f577 73 28 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  s(0);.  sqlite3_
3f578 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69  mutex_leave(sqli
3f579 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
3f57a 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
3f57b 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 0a 2f  C_MASTER));.}../
3f57c 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 6e  *.** Register an
3f57d 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   unlock-notify c
3f57e 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54  allback..**.** T
3f57f 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66  his is called af
3f580 74 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 22  ter connection "
3f581 64 62 22 20 68 61 73 20 61 74 74 65 6d 70 74 65  db" has attempte
3f582 64 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f 6e  d some operation
3f583 0a 2a 2a 20 62 75 74 20 68 61 73 20 72 65 63 65  .** but has rece
3f584 69 76 65 64 20 61 6e 20 53 51 4c 49 54 45 5f 4c  ived an SQLITE_L
3f585 4f 43 4b 45 44 20 65 72 72 6f 72 20 62 65 63 61  OCKED error beca
3f586 75 73 65 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e  use another conn
3f587 65 63 74 69 6f 6e 0a 2a 2a 20 28 63 61 6c 6c 20  ection.** (call 
3f588 69 74 20 70 4f 74 68 65 72 29 20 69 6e 20 74 68  it pOther) in th
3f589 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 77  e same process w
3f58a 61 73 20 62 75 73 79 20 75 73 69 6e 67 20 74 68  as busy using th
3f58b 65 20 73 61 6d 65 20 73 68 61 72 65 64 0a 2a 2a  e same shared.**
3f58c 20 63 61 63 68 65 2e 20 20 70 4f 74 68 65 72 20   cache.  pOther 
3f58d 69 73 20 66 6f 75 6e 64 20 62 79 20 6c 6f 6f 6b  is found by look
3f58e 69 6e 67 20 61 74 20 64 62 2d 3e 70 42 6c 6f 63  ing at db->pBloc
3f58f 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  kingConnection..
3f590 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69  **.** If there i
3f591 73 20 6e 6f 20 62 6c 6f 63 6b 69 6e 67 20 63 6f  s no blocking co
3f592 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 20 63 61  nnection, the ca
3f593 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65  llback is invoke
3f594 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 0a 2a  d immediately,.*
3f595 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f  * before this ro
3f596 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a  utine returns..*
3f597 2a 0a 2a 2a 20 49 66 20 70 4f 74 68 65 72 20 69  *.** If pOther i
3f598 73 20 61 6c 72 65 61 64 79 20 62 6c 6f 63 6b 65  s already blocke
3f599 64 20 6f 6e 20 64 62 2c 20 74 68 65 6e 20 72 65  d on db, then re
3f59a 70 6f 72 74 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  port SQLITE_LOCK
3f59b 45 44 2c 20 74 6f 20 69 6e 64 69 63 61 74 65 0a  ED, to indicate.
3f59c 2a 2a 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a  ** a deadlock..*
3f59d 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  *.** Otherwise, 
3f59e 6d 61 6b 65 20 61 72 72 61 6e 67 65 6d 65 6e 74  make arrangement
3f59f 73 20 74 6f 20 69 6e 76 6f 6b 65 20 78 4e 6f 74  s to invoke xNot
3f5a0 69 66 79 20 77 68 65 6e 20 70 4f 74 68 65 72 20  ify when pOther 
3f5a1 64 72 6f 70 73 0a 2a 2a 20 69 74 73 20 6c 6f 63  drops.** its loc
3f5a2 6b 73 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63  ks..**.** Each c
3f5a3 61 6c 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74  all to this rout
3f5a4 69 6e 65 20 6f 76 65 72 72 69 64 65 73 20 61 6e  ine overrides an
3f5a5 79 20 70 72 69 6f 72 20 63 61 6c 6c 62 61 63 6b  y prior callback
3f5a6 73 20 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20  s registered.** 
3f5a7 6f 6e 20 74 68 65 20 73 61 6d 65 20 22 64 62 22  on the same "db"
3f5a8 2e 20 20 49 66 20 78 4e 6f 74 69 66 79 3d 3d 30  .  If xNotify==0
3f5a9 20 74 68 65 6e 20 61 6e 79 20 70 72 69 6f 72 20   then any prior 
3f5aa 63 61 6c 6c 62 61 63 6b 73 20 61 72 65 20 69 6d  callbacks are im
3f5ab 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 63 61 6e  mediately.** can
3f5ac 63 65 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  celled..*/.SQLIT
3f5ad 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3f5ae 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28  3_unlock_notify(
3f5af 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
3f5b0 20 20 76 6f 69 64 20 28 2a 78 4e 6f 74 69 66 79    void (*xNotify
3f5b1 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 2c  )(void **, int),
3f5b2 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b  .  void *pArg.){
3f5b3 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
3f5b4 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65  TE_OK;..  sqlite
3f5b5 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
3f5b6 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 65 6e 74 65  ->mutex);.  ente
3f5b7 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 69 66 28  rMutex();..  if(
3f5b8 20 78 4e 6f 74 69 66 79 3d 3d 30 20 29 7b 0a 20   xNotify==0 ){. 
3f5b9 20 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f     removeFromBlo
3f5ba 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a 20 20  ckedList(db);.  
3f5bb 20 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43    db->pBlockingC
3f5bc 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 0a 20  onnection = 0;. 
3f5bd 20 20 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f     db->pUnlockCo
3f5be 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 0a 20 20  nnection = 0;.  
3f5bf 20 20 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74    db->xUnlockNot
3f5c0 69 66 79 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  ify = 0;.    db-
3f5c1 3e 70 55 6e 6c 6f 63 6b 41 72 67 20 3d 20 30 3b  >pUnlockArg = 0;
3f5c2 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 30 3d 3d  .  }else if( 0==
3f5c3 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e  db->pBlockingCon
3f5c4 6e 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 2f  nection ){.    /
3f5c5 2a 20 54 68 65 20 62 6c 6f 63 6b 69 6e 67 20 74  * The blocking t
3f5c6 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 62  ransaction has b
3f5c7 65 65 6e 20 63 6f 6e 63 6c 75 64 65 64 2e 20 4f  een concluded. O
3f5c8 72 20 74 68 65 72 65 20 6e 65 76 65 72 20 77 61  r there never wa
3f5c9 73 20 61 20 0a 20 20 20 20 2a 2a 20 62 6c 6f 63  s a .    ** bloc
3f5ca 6b 69 6e 67 20 74 72 61 6e 73 61 63 74 69 6f 6e  king transaction
3f5cb 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65  . In either case
3f5cc 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 6e 6f 74  , invoke the not
3f5cd 69 66 79 20 63 61 6c 6c 62 61 63 6b 0a 20 20 20  ify callback.   
3f5ce 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e   ** immediately.
3f5cf 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 78 4e 6f   .    */.    xNo
3f5d0 74 69 66 79 28 26 70 41 72 67 2c 20 31 29 3b 0a  tify(&pArg, 1);.
3f5d1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
3f5d2 69 74 65 33 20 2a 70 3b 0a 0a 20 20 20 20 66 6f  ite3 *p;..    fo
3f5d3 72 28 70 3d 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e  r(p=db->pBlockin
3f5d4 67 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 70 20 26  gConnection; p &
3f5d5 26 20 70 21 3d 64 62 3b 20 70 3d 70 2d 3e 70 55  & p!=db; p=p->pU
3f5d6 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 29  nlockConnection)
3f5d7 7b 7d 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a  {}.    if( p ){.
3f5d8 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
3f5d9 45 5f 4c 4f 43 4b 45 44 3b 20 20 20 20 20 20 20  E_LOCKED;       
3f5da 20 20 20 20 20 20 20 2f 2a 20 44 65 61 64 6c 6f         /* Deadlo
3f5db 63 6b 20 64 65 74 65 63 74 65 64 2e 20 2a 2f 0a  ck detected. */.
3f5dc 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3f5dd 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e   db->pUnlockConn
3f5de 65 63 74 69 6f 6e 20 3d 20 64 62 2d 3e 70 42 6c  ection = db->pBl
3f5df 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
3f5e0 3b 0a 20 20 20 20 20 20 64 62 2d 3e 78 55 6e 6c  ;.      db->xUnl
3f5e1 6f 63 6b 4e 6f 74 69 66 79 20 3d 20 78 4e 6f 74  ockNotify = xNot
3f5e2 69 66 79 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70  ify;.      db->p
3f5e3 55 6e 6c 6f 63 6b 41 72 67 20 3d 20 70 41 72 67  UnlockArg = pArg
3f5e4 3b 0a 20 20 20 20 20 20 72 65 6d 6f 76 65 46 72  ;.      removeFr
3f5e5 6f 6d 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62  omBlockedList(db
3f5e6 29 3b 0a 20 20 20 20 20 20 61 64 64 54 6f 42 6c  );.      addToBl
3f5e7 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a 20  ockedList(db);. 
3f5e8 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6c 65 61 76     }.  }..  leav
3f5e9 65 4d 75 74 65 78 28 29 3b 0a 20 20 61 73 73 65  eMutex();.  asse
3f5ea 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rt( !db->mallocF
3f5eb 61 69 6c 65 64 20 29 3b 0a 20 20 73 71 6c 69 74  ailed );.  sqlit
3f5ec 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20  e3Error(db, rc, 
3f5ed 28 72 63 3f 22 64 61 74 61 62 61 73 65 20 69 73  (rc?"database is
3f5ee 20 64 65 61 64 6c 6f 63 6b 65 64 22 3a 30 29 29   deadlocked":0))
3f5ef 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
3f5f0 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
3f5f1 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
3f5f2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
3f5f3 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
3f5f4 64 20 77 68 69 6c 65 20 73 74 65 70 70 69 6e 67  d while stepping
3f5f5 20 6f 72 20 70 72 65 70 61 72 69 6e 67 20 61 20   or preparing a 
3f5f6 73 74 61 74 65 6d 65 6e 74 20 0a 2a 2a 20 61 73  statement .** as
3f5f7 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63 6f  sociated with co
3f5f8 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 20 54 68 65  nnection db. The
3f5f9 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20   operation will 
3f5fa 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
3f5fb 43 4b 45 44 0a 2a 2a 20 74 6f 20 74 68 65 20 75  CKED.** to the u
3f5fc 73 65 72 20 62 65 63 61 75 73 65 20 69 74 20 72  ser because it r
3f5fd 65 71 75 69 72 65 73 20 61 20 6c 6f 63 6b 20 74  equires a lock t
3f5fe 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  hat will not be 
3f5ff 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 75 6e 74  available.** unt
3f600 69 6c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 42  il connection pB
3f601 6c 6f 63 6b 65 72 20 63 6f 6e 63 6c 75 64 65 73  locker concludes
3f602 20 69 74 73 20 63 75 72 72 65 6e 74 20 74 72 61   its current tra
3f603 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  nsaction..*/.SQL
3f604 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
3f605 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69   sqlite3Connecti
3f606 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65  onBlocked(sqlite
3f607 33 20 2a 64 62 2c 20 73 71 6c 69 74 65 33 20 2a  3 *db, sqlite3 *
3f608 70 42 6c 6f 63 6b 65 72 29 7b 0a 20 20 65 6e 74  pBlocker){.  ent
3f609 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28  erMutex();.  if(
3f60a 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f   db->pBlockingCo
3f60b 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 26 26 20 64  nnection==0 && d
3f60c 62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63  b->pUnlockConnec
3f60d 74 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 61  tion==0 ){.    a
3f60e 64 64 54 6f 42 6c 6f 63 6b 65 64 4c 69 73 74 28  ddToBlockedList(
3f60f 64 62 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 70  db);.  }.  db->p
3f610 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
3f611 6f 6e 20 3d 20 70 42 6c 6f 63 6b 65 72 3b 0a 20  on = pBlocker;. 
3f612 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d   leaveMutex();.}
3f613 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
3f614 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
3f615 77 68 65 6e 0a 2a 2a 20 74 68 65 20 74 72 61 6e  when.** the tran
3f616 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 62  saction opened b
3f617 79 20 64 61 74 61 62 61 73 65 20 64 62 20 68 61  y database db ha
3f618 73 20 6a 75 73 74 20 66 69 6e 69 73 68 65 64 2e  s just finished.
3f619 20 4c 6f 63 6b 73 20 68 65 6c 64 20 0a 2a 2a 20   Locks held .** 
3f61a 62 79 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  by database conn
3f61b 65 63 74 69 6f 6e 20 64 62 20 68 61 76 65 20 62  ection db have b
3f61c 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a  een released..**
3f61d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
3f61e 6e 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20  n loops through 
3f61f 65 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68  each entry in th
3f620 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  e blocked connec
3f621 74 69 6f 6e 73 0a 2a 2a 20 6c 69 73 74 20 61 6e  tions.** list an
3f622 64 20 64 6f 65 73 20 74 68 65 20 66 6f 6c 6c 6f  d does the follo
3f623 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29  wing:.**.**   1)
3f624 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 33 2e   If the sqlite3.
3f625 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
3f626 69 6f 6e 20 6d 65 6d 62 65 72 20 6f 66 20 61 20  ion member of a 
3f627 6c 69 73 74 20 65 6e 74 72 79 20 69 73 0a 2a 2a  list entry is.**
3f628 20 20 20 20 20 20 73 65 74 20 74 6f 20 64 62 2c        set to db,
3f629 20 74 68 65 6e 20 73 65 74 20 70 42 6c 6f 63 6b   then set pBlock
3f62a 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 30 2e  ingConnection=0.
3f62b 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66 20 74  .**.**   2) If t
3f62c 68 65 20 73 71 6c 69 74 65 33 2e 70 55 6e 6c 6f  he sqlite3.pUnlo
3f62d 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 65 6d  ckConnection mem
3f62e 62 65 72 20 6f 66 20 61 20 6c 69 73 74 20 65 6e  ber of a list en
3f62f 74 72 79 20 69 73 0a 2a 2a 20 20 20 20 20 20 73  try is.**      s
3f630 65 74 20 74 6f 20 64 62 2c 20 74 68 65 6e 20 69  et to db, then i
3f631 6e 76 6f 6b 65 20 74 68 65 20 63 6f 6e 66 69 67  nvoke the config
3f632 75 72 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69  ured unlock-noti
3f633 66 79 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64 0a  fy callback and.
3f634 2a 2a 20 20 20 20 20 20 73 65 74 20 70 55 6e 6c  **      set pUnl
3f635 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 30 2e  ockConnection=0.
3f636 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20 49 66 20 74  .**.**   3) If t
3f637 68 65 20 74 77 6f 20 73 74 65 70 73 20 61 62 6f  he two steps abo
3f638 76 65 20 6d 65 61 6e 20 74 68 61 74 20 70 42 6c  ve mean that pBl
3f639 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
3f63a 3d 3d 30 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ==0 and.**      
3f63b 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f  pUnlockConnectio
3f63c 6e 3d 3d 30 2c 20 72 65 6d 6f 76 65 20 74 68 65  n==0, remove the
3f63d 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20   entry from the 
3f63e 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69  blocked connecti
3f63f 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 6c 69 73 74  ons.**      list
3f640 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
3f641 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
3f642 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b  ConnectionUnlock
3f643 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ed(sqlite3 *db){
3f644 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e 6c 6f 63  .  void (*xUnloc
3f645 6b 4e 6f 74 69 66 79 29 28 76 6f 69 64 20 2a 2a  kNotify)(void **
3f646 2c 20 69 6e 74 29 20 3d 20 30 3b 20 2f 2a 20 55  , int) = 0; /* U
3f647 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 62 20  nlock-notify cb 
3f648 74 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 69  to invoke */.  i
3f649 6e 74 20 6e 41 72 67 20 3d 20 30 3b 20 20 20 20  nt nArg = 0;    
3f64a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f64b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
3f64c 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
3f64d 61 41 72 67 5b 5d 20 2a 2f 0a 20 20 73 71 6c 69  aArg[] */.  sqli
3f64e 74 65 33 20 2a 2a 70 70 3b 20 20 20 20 20 20 20  te3 **pp;       
3f64f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f650 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
3f651 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 76   variable */.  v
3f652 6f 69 64 20 2a 2a 61 41 72 67 3b 20 20 20 20 20  oid **aArg;     
3f653 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
3f654 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 75 6e  uments to the un
3f655 6c 6f 63 6b 20 63 61 6c 6c 62 61 63 6b 20 2a 2f  lock callback */
3f656 0a 20 20 76 6f 69 64 20 2a 2a 61 44 79 6e 20 3d  .  void **aDyn =
3f657 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   0;           /*
3f658 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c   Dynamically all
3f659 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f 72  ocated space for
3f65a 20 61 41 72 67 5b 5d 20 2a 2f 0a 20 20 76 6f 69   aArg[] */.  voi
3f65b 64 20 2a 61 53 74 61 74 69 63 5b 31 36 5d 3b 20  d *aStatic[16]; 
3f65c 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74          /* Start
3f65d 65 72 20 73 70 61 63 65 20 66 6f 72 20 61 41 72  er space for aAr
3f65e 67 5b 5d 2e 20 20 4e 6f 20 6d 61 6c 6c 6f 63 20  g[].  No malloc 
3f65f 72 65 71 75 69 72 65 64 20 2a 2f 0a 0a 20 20 61  required */..  a
3f660 41 72 67 20 3d 20 61 53 74 61 74 69 63 3b 0a 20  Arg = aStatic;. 
3f661 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b 20 20   enterMutex();  
3f662 20 20 20 20 20 20 20 2f 2a 20 45 6e 74 65 72 20         /* Enter 
3f663 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75  STATIC_MASTER mu
3f664 74 65 78 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69  tex */..  /* Thi
3f665 73 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 63 65  s loop runs once
3f666 20 66 6f 72 20 65 61 63 68 20 65 6e 74 72 79 20   for each entry 
3f667 69 6e 20 74 68 65 20 62 6c 6f 63 6b 65 64 2d 63  in the blocked-c
3f668 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e  onnections list.
3f669 20 2a 2f 0a 20 20 66 6f 72 28 70 70 3d 26 73 71   */.  for(pp=&sq
3f66a 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74  lite3BlockedList
3f66b 3b 20 2a 70 70 3b 20 2f 2a 20 6e 6f 2d 6f 70 20  ; *pp; /* no-op 
3f66c 2a 2f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  */ ){.    sqlite
3f66d 33 20 2a 70 20 3d 20 2a 70 70 3b 0a 0a 20 20 20  3 *p = *pp;..   
3f66e 20 2f 2a 20 53 74 65 70 20 31 2e 20 2a 2f 0a 20   /* Step 1. */. 
3f66f 20 20 20 69 66 28 20 70 2d 3e 70 42 6c 6f 63 6b     if( p->pBlock
3f670 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 64  ingConnection==d
3f671 62 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 42  b ){.      p->pB
3f672 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f  lockingConnectio
3f673 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20  n = 0;.    }..  
3f674 20 20 2f 2a 20 53 74 65 70 20 32 2e 20 2a 2f 0a    /* Step 2. */.
3f675 20 20 20 20 69 66 28 20 70 2d 3e 70 55 6e 6c 6f      if( p->pUnlo
3f676 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 64 62  ckConnection==db
3f677 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
3f678 28 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69  ( p->xUnlockNoti
3f679 66 79 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  fy );.      if( 
3f67a 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79  p->xUnlockNotify
3f67b 21 3d 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20  !=xUnlockNotify 
3f67c 26 26 20 6e 41 72 67 21 3d 30 20 29 7b 0a 20 20  && nArg!=0 ){.  
3f67d 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74        xUnlockNot
3f67e 69 66 79 28 61 41 72 67 2c 20 6e 41 72 67 29 3b  ify(aArg, nArg);
3f67f 0a 20 20 20 20 20 20 20 20 6e 41 72 67 20 3d 20  .        nArg = 
3f680 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  0;.      }..    
3f681 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65    sqlite3BeginBe
3f682 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
3f683 20 20 20 20 61 73 73 65 72 74 28 20 61 41 72 67      assert( aArg
3f684 3d 3d 61 44 79 6e 20 7c 7c 20 28 61 44 79 6e 3d  ==aDyn || (aDyn=
3f685 3d 30 20 26 26 20 61 41 72 67 3d 3d 61 53 74 61  =0 && aArg==aSta
3f686 74 69 63 29 20 29 3b 0a 20 20 20 20 20 20 61 73  tic) );.      as
3f687 73 65 72 74 28 20 6e 41 72 67 3c 3d 28 69 6e 74  sert( nArg<=(int
3f688 29 41 72 72 61 79 53 69 7a 65 28 61 53 74 61 74  )ArraySize(aStat
3f689 69 63 29 20 7c 7c 20 61 41 72 67 3d 3d 61 44 79  ic) || aArg==aDy
3f68a 6e 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28  n );.      if( (
3f68b 21 61 44 79 6e 20 26 26 20 6e 41 72 67 3d 3d 28  !aDyn && nArg==(
3f68c 69 6e 74 29 41 72 72 61 79 53 69 7a 65 28 61 53  int)ArraySize(aS
3f68d 74 61 74 69 63 29 29 0a 20 20 20 20 20 20 20 7c  tatic)).       |
3f68e 7c 20 28 61 44 79 6e 20 26 26 20 6e 41 72 67 3d  | (aDyn && nArg=
3f68f 3d 28 69 6e 74 29 28 73 71 6c 69 74 65 33 4d 61  =(int)(sqlite3Ma
3f690 6c 6c 6f 63 53 69 7a 65 28 61 44 79 6e 29 2f 73  llocSize(aDyn)/s
3f691 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29 29 0a 20  izeof(void*))). 
3f692 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
3f693 2f 2a 20 54 68 65 20 61 41 72 67 5b 5d 20 61 72  /* The aArg[] ar
3f694 72 61 79 20 6e 65 65 64 73 20 74 6f 20 67 72 6f  ray needs to gro
3f695 77 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 76 6f  w. */.        vo
3f696 69 64 20 2a 2a 70 4e 65 77 20 3d 20 28 76 6f 69  id **pNew = (voi
3f697 64 20 2a 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  d **)sqlite3Mall
3f698 6f 63 28 6e 41 72 67 2a 73 69 7a 65 6f 66 28 76  oc(nArg*sizeof(v
3f699 6f 69 64 20 2a 29 2a 32 29 3b 0a 20 20 20 20 20  oid *)*2);.     
3f69a 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20     if( pNew ){. 
3f69b 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
3f69c 70 4e 65 77 2c 20 61 41 72 67 2c 20 6e 41 72 67  pNew, aArg, nArg
3f69d 2a 73 69 7a 65 6f 66 28 76 6f 69 64 20 2a 29 29  *sizeof(void *))
3f69e 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
3f69f 74 65 33 5f 66 72 65 65 28 61 44 79 6e 29 3b 0a  te3_free(aDyn);.
3f6a0 20 20 20 20 20 20 20 20 20 20 61 44 79 6e 20 3d            aDyn =
3f6a1 20 61 41 72 67 20 3d 20 70 4e 65 77 3b 0a 20 20   aArg = pNew;.  
3f6a2 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
3f6a3 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 6f         /* This o
3f6a4 63 63 75 72 73 20 77 68 65 6e 20 74 68 65 20 61  ccurs when the a
3f6a5 72 72 61 79 20 6f 66 20 63 6f 6e 74 65 78 74 20  rray of context 
3f6a6 70 6f 69 6e 74 65 72 73 20 74 68 61 74 20 6e 65  pointers that ne
3f6a7 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20  ed to.          
3f6a8 2a 2a 20 62 65 20 70 61 73 73 65 64 20 74 6f 20  ** be passed to 
3f6a9 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66  the unlock-notif
3f6aa 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6c 61  y callback is la
3f6ab 72 67 65 72 20 74 68 61 6e 20 74 68 65 0a 20 20  rger than the.  
3f6ac 20 20 20 20 20 20 20 20 2a 2a 20 61 53 74 61 74          ** aStat
3f6ad 69 63 5b 5d 20 61 72 72 61 79 20 61 6c 6c 6f 63  ic[] array alloc
3f6ae 61 74 65 64 20 6f 6e 20 74 68 65 20 73 74 61 63  ated on the stac
3f6af 6b 20 61 6e 64 20 74 68 65 20 61 74 74 65 6d 70  k and the attemp
3f6b0 74 20 74 6f 20 0a 20 20 20 20 20 20 20 20 20 20  t to .          
3f6b1 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 61 20 6c 61  ** allocate a la
3f6b2 72 67 65 72 20 61 72 72 61 79 20 66 72 6f 6d 20  rger array from 
3f6b3 74 68 65 20 68 65 61 70 20 68 61 73 20 66 61 69  the heap has fai
3f6b4 6c 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  led..          *
3f6b5 2a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54  *.          ** T
3f6b6 68 69 73 20 69 73 20 61 20 64 69 66 66 69 63 75  his is a difficu
3f6b7 6c 74 20 73 69 74 75 61 74 69 6f 6e 20 74 6f 20  lt situation to 
3f6b8 68 61 6e 64 6c 65 2e 20 52 65 74 75 72 6e 69 6e  handle. Returnin
3f6b9 67 20 61 6e 20 65 72 72 6f 72 0a 20 20 20 20 20  g an error.     
3f6ba 20 20 20 20 20 2a 2a 20 63 6f 64 65 20 74 6f 20       ** code to 
3f6bb 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 69 6e  the caller is in
3f6bc 73 75 66 66 69 63 69 65 6e 74 2c 20 61 73 20 65  sufficient, as e
3f6bd 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 20  ven if an error 
3f6be 63 6f 64 65 0a 20 20 20 20 20 20 20 20 20 20 2a  code.          *
3f6bf 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 68  * is returned th
3f6c0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  e transaction on
3f6c1 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 77   connection db w
3f6c2 69 6c 6c 20 73 74 69 6c 6c 20 62 65 0a 20 20 20  ill still be.   
3f6c3 20 20 20 20 20 20 20 2a 2a 20 63 6c 6f 73 65 64         ** closed
3f6c4 20 61 6e 64 20 74 68 65 20 75 6e 6c 6f 63 6b 2d   and the unlock-
3f6c5 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 73  notify callbacks
3f6c6 20 6f 6e 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e   on blocked conn
3f6c7 65 63 74 69 6f 6e 73 0a 20 20 20 20 20 20 20 20  ections.        
3f6c8 20 20 2a 2a 20 77 69 6c 6c 20 67 6f 20 75 6e 69    ** will go uni
3f6c9 73 73 75 65 64 2e 20 54 68 69 73 20 6d 69 67 68  ssued. This migh
3f6ca 74 20 63 61 75 73 65 20 74 68 65 20 61 70 70 6c  t cause the appl
3f6cb 69 63 61 74 69 6f 6e 20 74 6f 20 77 61 69 74 0a  ication to wait.
3f6cc 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 64            ** ind
3f6cd 65 66 69 6e 69 74 65 6c 79 20 66 6f 72 20 61 6e  efinitely for an
3f6ce 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   unlock-notify c
3f6cf 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 77 69 6c  allback that wil
3f6d0 6c 20 6e 65 76 65 72 20 0a 20 20 20 20 20 20 20  l never .       
3f6d1 20 20 20 2a 2a 20 61 72 72 69 76 65 2e 0a 20 20     ** arrive..  
3f6d2 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
3f6d3 20 20 20 20 20 2a 2a 20 49 6e 73 74 65 61 64 2c       ** Instead,
3f6d4 20 69 6e 76 6f 6b 65 20 74 68 65 20 75 6e 6c 6f   invoke the unlo
3f6d5 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61  ck-notify callba
3f6d6 63 6b 20 77 69 74 68 20 74 68 65 20 63 6f 6e 74  ck with the cont
3f6d7 65 78 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ext.          **
3f6d8 20 61 72 72 61 79 20 61 6c 72 65 61 64 79 20 61   array already a
3f6d9 63 63 75 6d 75 6c 61 74 65 64 2e 20 57 65 20 63  ccumulated. We c
3f6da 61 6e 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68  an then clear th
3f6db 65 20 61 72 72 61 79 20 61 6e 64 0a 20 20 20 20  e array and.    
3f6dc 20 20 20 20 20 20 2a 2a 20 62 65 67 69 6e 20 61        ** begin a
3f6dd 63 63 75 6d 75 6c 61 74 69 6e 67 20 61 6e 79 20  ccumulating any 
3f6de 66 75 72 74 68 65 72 20 63 6f 6e 74 65 78 74 20  further context 
3f6df 70 6f 69 6e 74 65 72 73 20 77 69 74 68 6f 75 74  pointers without
3f6e0 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72   .          ** r
3f6e1 65 71 75 69 72 69 6e 67 20 61 6e 79 20 64 79 6e  equiring any dyn
3f6e2 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  amic allocation.
3f6e3 20 54 68 69 73 20 69 73 20 73 75 62 2d 6f 70 74   This is sub-opt
3f6e4 69 6d 61 6c 20 62 65 63 61 75 73 65 0a 20 20 20  imal because.   
3f6e5 20 20 20 20 20 20 20 2a 2a 20 69 74 20 6d 65 61         ** it mea
3f6e6 6e 73 20 74 68 61 74 20 69 6e 73 74 65 61 64 20  ns that instead 
3f6e7 6f 66 20 6f 6e 65 20 63 61 6c 6c 62 61 63 6b 20  of one callback 
3f6e8 77 69 74 68 20 61 20 6c 61 72 67 65 20 61 72 72  with a large arr
3f6e9 61 79 20 6f 66 0a 20 20 20 20 20 20 20 20 20 20  ay of.          
3f6ea 2a 2a 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74  ** context point
3f6eb 65 72 73 20 74 68 65 20 61 70 70 6c 69 63 61 74  ers the applicat
3f6ec 69 6f 6e 20 77 69 6c 6c 20 72 65 63 65 69 76 65  ion will receive
3f6ed 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 20 20 20   two or more.   
3f6ee 20 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 62 61         ** callba
3f6ef 63 6b 73 20 77 69 74 68 20 73 6d 61 6c 6c 65 72  cks with smaller
3f6f0 20 61 72 72 61 79 73 20 6f 66 20 63 6f 6e 74 65   arrays of conte
3f6f1 78 74 20 70 6f 69 6e 74 65 72 73 2c 20 77 68 69  xt pointers, whi
3f6f2 63 68 20 77 69 6c 6c 0a 20 20 20 20 20 20 20 20  ch will.        
3f6f3 20 20 2a 2a 20 72 65 64 75 63 65 20 74 68 65 20    ** reduce the 
3f6f4 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61 62 69  applications abi
3f6f5 6c 69 74 79 20 74 6f 20 70 72 69 6f 72 69 74 69  lity to prioriti
3f6f6 7a 65 20 6d 75 6c 74 69 70 6c 65 20 0a 20 20 20  ze multiple .   
3f6f7 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 6e 65 63         ** connec
3f6f8 74 69 6f 6e 73 2e 20 42 75 74 20 69 74 20 69 73  tions. But it is
3f6f9 20 74 68 65 20 62 65 73 74 20 74 68 61 74 20 63   the best that c
3f6fa 61 6e 20 62 65 20 64 6f 6e 65 20 75 6e 64 65 72  an be done under
3f6fb 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a   the.          *
3f6fc 2a 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73 2e  * circumstances.
3f6fd 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  .          */.  
3f6fe 20 20 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e          xUnlockN
3f6ff 6f 74 69 66 79 28 61 41 72 67 2c 20 6e 41 72 67  otify(aArg, nArg
3f700 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 41 72  );.          nAr
3f701 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  g = 0;.        }
3f702 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
3f703 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
3f704 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 20 20 20 20  alloc();..      
3f705 61 41 72 67 5b 6e 41 72 67 2b 2b 5d 20 3d 20 70  aArg[nArg++] = p
3f706 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67 3b 0a 20 20  ->pUnlockArg;.  
3f707 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66      xUnlockNotif
3f708 79 20 3d 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f  y = p->xUnlockNo
3f709 74 69 66 79 3b 0a 20 20 20 20 20 20 70 2d 3e 70  tify;.      p->p
3f70a 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
3f70b 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 78   = 0;.      p->x
3f70c 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 3d 20 30  UnlockNotify = 0
3f70d 3b 0a 20 20 20 20 20 20 70 2d 3e 70 55 6e 6c 6f  ;.      p->pUnlo
3f70e 63 6b 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 7d  ckArg = 0;.    }
3f70f 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70 20 33 2e  ..    /* Step 3.
3f710 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70   */.    if( p->p
3f711 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
3f712 6f 6e 3d 3d 30 20 26 26 20 70 2d 3e 70 55 6e 6c  on==0 && p->pUnl
3f713 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30  ockConnection==0
3f714 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d   ){.      /* Rem
3f715 6f 76 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  ove connection p
3f716 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65   from the blocke
3f717 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69  d connections li
3f718 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 2a 70 70  st. */.      *pp
3f719 20 3d 20 70 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b   = p->pNextBlock
3f71a 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65  ed;.      p->pNe
3f71b 78 74 42 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20  xtBlocked = 0;. 
3f71c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
3f71d 70 70 20 3d 20 26 70 2d 3e 70 4e 65 78 74 42 6c  pp = &p->pNextBl
3f71e 6f 63 6b 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d  ocked;.    }.  }
3f71f 0a 0a 20 20 69 66 28 20 6e 41 72 67 21 3d 30 20  ..  if( nArg!=0 
3f720 29 7b 0a 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f  ){.    xUnlockNo
3f721 74 69 66 79 28 61 41 72 67 2c 20 6e 41 72 67 29  tify(aArg, nArg)
3f722 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
3f723 66 72 65 65 28 61 44 79 6e 29 3b 0a 20 20 6c 65  free(aDyn);.  le
3f724 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20 20 20  aveMutex();     
3f725 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 53 54 41      /* Leave STA
3f726 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78  TIC_MASTER mutex
3f727 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   */.}../*.** Thi
3f728 73 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  s is called when
3f729 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
3f72a 6e 6e 65 63 74 69 6f 6e 20 70 61 73 73 65 64 20  nnection passed 
3f72b 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 69  as an argument i
3f72c 73 20 0a 2a 2a 20 62 65 69 6e 67 20 63 6c 6f 73  s .** being clos
3f72d 65 64 2e 20 54 68 65 20 63 6f 6e 6e 65 63 74 69  ed. The connecti
3f72e 6f 6e 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72  on is removed fr
3f72f 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 6c  om the blocked l
3f730 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ist..*/.SQLITE_P
3f731 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
3f732 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f  te3ConnectionClo
3f733 73 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29  sed(sqlite3 *db)
3f734 7b 0a 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  {.  sqlite3Conne
3f735 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64 62  ctionUnlocked(db
3f736 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28  );.  enterMutex(
3f737 29 3b 0a 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42  );.  removeFromB
3f738 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a  lockedList(db);.
3f739 20 20 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65    checkListPrope
3f73a 72 74 69 65 73 28 64 62 29 3b 0a 20 20 6c 65 61  rties(db);.  lea
3f73b 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65 6e  veMutex();.}.#en
3f73c 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
3f73d 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6e 6f 74 69  **** End of noti
3f73e 66 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  fy.c ***********
3f73f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f741 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
3f742 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
3f743 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  fts3.c *********
3f744 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f745 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f746 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20  ***/./*.** 2006 
3f747 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65  Oct 10.**.** The
3f748 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
3f749 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
3f74a 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
3f74b 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
3f74c 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
3f74d 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
3f74e 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
3f74f 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
3f750 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
3f751 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
3f752 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
3f753 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
3f754 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
3f755 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
3f756 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
3f757 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
3f758 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
3f759 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f75a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f75b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f75c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3f75d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
3f75e 20 54 68 69 73 20 69 73 20 61 6e 20 53 51 4c 69   This is an SQLi
3f75f 74 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d  te module implem
3f760 65 6e 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78 74  enting full-text
3f761 20 73 65 61 72 63 68 2e 0a 2a 2f 0a 0a 2f 2a 0a   search..*/../*.
3f762 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74  ** The code in t
3f763 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79  his file is only
3f764 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a   compiled if:.**
3f765 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54  .**     * The FT
3f766 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69  S3 module is bei
3f767 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65  ng built as an e
3f768 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20  xtension.**     
3f769 20 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65    (in which case
3f76a 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20   SQLITE_CORE is 
3f76b 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72  not defined), or
3f76c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  .**.**     * The
3f76d 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
3f76e 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f  being built into
3f76f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20   the core of.** 
3f770 20 20 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e        SQLite (in
3f771 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49   which case SQLI
3f772 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69  TE_ENABLE_FTS3 i
3f773 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 0a  s defined)..*/..
3f774 2f 2a 20 54 68 65 20 66 75 6c 6c 2d 74 65 78 74  /* The full-text
3f775 20 69 6e 64 65 78 20 69 73 20 73 74 6f 72 65 64   index is stored
3f776 20 69 6e 20 61 20 73 65 72 69 65 73 20 6f 66 20   in a series of 
3f777 62 2b 74 72 65 65 20 28 2d 6c 69 6b 65 29 0a 2a  b+tree (-like).*
3f778 2a 20 73 74 72 75 63 74 75 72 65 73 20 63 61 6c  * structures cal
3f779 6c 65 64 20 73 65 67 6d 65 6e 74 73 20 77 68 69  led segments whi
3f77a 63 68 20 6d 61 70 20 74 65 72 6d 73 20 74 6f 20  ch map terms to 
3f77b 64 6f 63 6c 69 73 74 73 2e 20 20 54 68 65 0a 2a  doclists.  The.*
3f77c 2a 20 73 74 72 75 63 74 75 72 65 73 20 61 72 65  * structures are
3f77d 20 6c 69 6b 65 20 62 2b 74 72 65 65 73 20 69 6e   like b+trees in
3f77e 20 6c 61 79 6f 75 74 2c 20 62 75 74 20 61 72 65   layout, but are
3f77f 20 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72 6f   constructed fro
3f780 6d 20 74 68 65 0a 2a 2a 20 62 6f 74 74 6f 6d 20  m the.** bottom 
3f781 75 70 20 69 6e 20 6f 70 74 69 6d 61 6c 20 66 61  up in optimal fa
3f782 73 68 69 6f 6e 20 61 6e 64 20 61 72 65 20 6e 6f  shion and are no
3f783 74 20 75 70 64 61 74 61 62 6c 65 2e 20 20 53 69  t updatable.  Si
3f784 6e 63 65 20 74 72 65 65 73 0a 2a 2a 20 61 72 65  nce trees.** are
3f785 20 62 75 69 6c 74 20 66 72 6f 6d 20 74 68 65 20   built from the 
3f786 62 6f 74 74 6f 6d 20 75 70 2c 20 74 68 69 6e 67  bottom up, thing
3f787 73 20 77 69 6c 6c 20 62 65 20 64 65 73 63 72 69  s will be descri
3f788 62 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20  bed from the.** 
3f789 62 6f 74 74 6f 6d 20 75 70 2e 0a 2a 2a 0a 2a 2a  bottom up..**.**
3f78a 0a 2a 2a 2a 2a 20 56 61 72 69 6e 74 73 20 2a 2a  .**** Varints **
3f78b 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63 20  **.** The basic 
3f78c 75 6e 69 74 20 6f 66 20 65 6e 63 6f 64 69 6e 67  unit of encoding
3f78d 20 69 73 20 61 20 76 61 72 69 61 62 6c 65 2d 6c   is a variable-l
3f78e 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 63 61  ength integer ca
3f78f 6c 6c 65 64 20 61 0a 2a 2a 20 76 61 72 69 6e 74  lled a.** varint
3f790 2e 20 20 57 65 20 65 6e 63 6f 64 65 20 76 61 72  .  We encode var
3f791 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74  iable-length int
3f792 65 67 65 72 73 20 69 6e 20 6c 69 74 74 6c 65 2d  egers in little-
3f793 65 6e 64 69 61 6e 20 6f 72 64 65 72 0a 2a 2a 20  endian order.** 
3f794 75 73 69 6e 67 20 73 65 76 65 6e 20 62 69 74 73  using seven bits
3f795 20 2a 20 70 65 72 20 62 79 74 65 20 61 73 20 66   * per byte as f
3f796 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45  ollows:.**.** KE
3f797 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 41 20  Y:.**         A 
3f798 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37 20  = 0xxxxxxx    7 
3f799 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64  bits of data and
3f79a 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a   one flag bit.**
3f79b 20 20 20 20 20 20 20 20 20 42 20 3d 20 31 78 78           B = 1xx
3f79c 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20  xxxxx    7 bits 
3f79d 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20  of data and one 
3f79e 66 6c 61 67 20 62 69 74 0a 2a 2a 0a 2a 2a 20 20  flag bit.**.**  
3f79f 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a 20 31 34  7 bits - A.** 14
3f7a0 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a 20 32 31   bits - BA.** 21
3f7a1 20 62 69 74 73 20 2d 20 42 42 41 0a 2a 2a 20 61   bits - BBA.** a
3f7a2 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nd so on..**.** 
3f7a3 54 68 69 73 20 69 73 20 73 69 6d 69 6c 61 72 20  This is similar 
3f7a4 69 6e 20 63 6f 6e 63 65 70 74 20 74 6f 20 68 6f  in concept to ho
3f7a5 77 20 73 71 6c 69 74 65 20 65 6e 63 6f 64 65 73  w sqlite encodes
3f7a6 20 22 76 61 72 69 6e 74 73 22 20 62 75 74 0a 2a   "varints" but.*
3f7a7 2a 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69  * the encoding i
3f7a8 73 20 6e 6f 74 20 74 68 65 20 73 61 6d 65 2e 20  s not the same. 
3f7a9 20 53 51 4c 69 74 65 20 76 61 72 69 6e 74 73 20   SQLite varints 
3f7aa 61 72 65 20 62 69 67 2d 65 6e 64 69 61 6e 0a 2a  are big-endian.*
3f7ab 2a 20 61 72 65 20 61 72 65 20 6c 69 6d 69 74 65  * are are limite
3f7ac 64 20 74 6f 20 39 20 62 79 74 65 73 20 69 6e 20  d to 9 bytes in 
3f7ad 6c 65 6e 67 74 68 20 77 68 65 72 65 61 73 20 46  length whereas F
3f7ae 54 53 33 20 76 61 72 69 6e 74 73 20 61 72 65 0a  TS3 varints are.
3f7af 2a 2a 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e  ** little-endian
3f7b0 20 61 6e 64 20 63 61 6e 20 62 65 20 75 70 20 74   and can be up t
3f7b1 6f 20 31 30 20 62 79 74 65 73 20 69 6e 20 6c 65  o 10 bytes in le
3f7b2 6e 67 74 68 20 28 69 6e 20 74 68 65 6f 72 79 29  ngth (in theory)
3f7b3 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 20  ..**.** Example 
3f7b4 65 6e 63 6f 64 69 6e 67 73 3a 0a 2a 2a 0a 2a 2a  encodings:.**.**
3f7b5 20 20 20 20 20 31 3a 20 20 20 20 30 78 30 31 0a       1:    0x01.
3f7b6 2a 2a 20 20 20 31 32 37 3a 20 20 20 20 30 78 37  **   127:    0x7
3f7b7 66 0a 2a 2a 20 20 20 31 32 38 3a 20 20 20 20 30  f.**   128:    0
3f7b8 78 38 31 20 30 78 30 30 0a 2a 2a 0a 2a 2a 0a 2a  x81 0x00.**.**.*
3f7b9 2a 2a 2a 20 44 6f 63 75 6d 65 6e 74 20 6c 69 73  *** Document lis
3f7ba 74 73 20 2a 2a 2a 2a 0a 2a 2a 20 41 20 64 6f 63  ts ****.** A doc
3f7bb 6c 69 73 74 20 28 64 6f 63 75 6d 65 6e 74 20 6c  list (document l
3f7bc 69 73 74 29 20 68 6f 6c 64 73 20 61 20 64 6f 63  ist) holds a doc
3f7bd 69 64 2d 73 6f 72 74 65 64 20 6c 69 73 74 20 6f  id-sorted list o
3f7be 66 20 68 69 74 73 20 66 6f 72 20 61 0a 2a 2a 20  f hits for a.** 
3f7bf 67 69 76 65 6e 20 74 65 72 6d 2e 20 20 44 6f 63  given term.  Doc
3f7c0 6c 69 73 74 73 20 68 6f 6c 64 20 64 6f 63 69 64  lists hold docid
3f7c1 73 20 61 6e 64 20 61 73 73 6f 63 69 61 74 65 64  s and associated
3f7c2 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69 6f 6e 73   token positions
3f7c3 2e 0a 2a 2a 20 41 20 64 6f 63 69 64 20 69 73 20  ..** A docid is 
3f7c4 74 68 65 20 75 6e 69 71 75 65 20 69 6e 74 65 67  the unique integ
3f7c5 65 72 20 69 64 65 6e 74 69 66 69 65 72 20 66 6f  er identifier fo
3f7c6 72 20 61 20 73 69 6e 67 6c 65 20 64 6f 63 75 6d  r a single docum
3f7c7 65 6e 74 2e 0a 2a 2a 20 41 20 70 6f 73 69 74 69  ent..** A positi
3f7c8 6f 6e 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  on is the index 
3f7c9 6f 66 20 61 20 77 6f 72 64 20 77 69 74 68 69 6e  of a word within
3f7ca 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 2e 20 20   the document.  
3f7cb 54 68 65 20 66 69 72 73 74 20 0a 2a 2a 20 77 6f  The first .** wo
3f7cc 72 64 20 6f 66 20 74 68 65 20 64 6f 63 75 6d 65  rd of the docume
3f7cd 6e 74 20 68 61 73 20 61 20 70 6f 73 69 74 69 6f  nt has a positio
3f7ce 6e 20 6f 66 20 30 2e 0a 2a 2a 0a 2a 2a 20 46 54  n of 0..**.** FT
3f7cf 53 33 20 75 73 65 64 20 74 6f 20 6f 70 74 69 6f  S3 used to optio
3f7d0 6e 61 6c 6c 79 20 73 74 6f 72 65 20 63 68 61 72  nally store char
3f7d1 61 63 74 65 72 20 6f 66 66 73 65 74 73 20 75 73  acter offsets us
3f7d2 69 6e 67 20 61 20 63 6f 6d 70 69 6c 65 2d 74 69  ing a compile-ti
3f7d3 6d 65 0a 2a 2a 20 6f 70 74 69 6f 6e 2e 20 20 42  me.** option.  B
3f7d4 75 74 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e  ut that function
3f7d5 61 6c 69 74 79 20 69 73 20 6e 6f 20 6c 6f 6e 67  ality is no long
3f7d6 65 72 20 73 75 70 70 6f 72 74 65 64 2e 0a 2a 2a  er supported..**
3f7d7 0a 2a 2a 20 41 20 64 6f 63 6c 69 73 74 20 69 73  .** A doclist is
3f7d8 20 73 74 6f 72 65 64 20 6c 69 6b 65 20 74 68 69   stored like thi
3f7d9 73 3a 0a 2a 2a 0a 2a 2a 20 61 72 72 61 79 20 7b  s:.**.** array {
3f7da 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 64 6f 63  .**   varint doc
3f7db 69 64 3b 20 20 20 20 20 20 20 20 20 20 28 64 65  id;          (de
3f7dc 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69 6f 75  lta from previou
3f7dd 73 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 20 20  s doclist).**   
3f7de 61 72 72 61 79 20 7b 20 20 20 20 20 20 20 20 20  array {         
3f7df 20 20 20 20 20 20 20 28 70 6f 73 69 74 69 6f 6e         (position
3f7e0 20 6c 69 73 74 20 66 6f 72 20 63 6f 6c 75 6d 6e   list for column
3f7e1 20 30 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e   0).**     varin
3f7e2 74 20 70 6f 73 69 74 69 6f 6e 3b 20 20 20 20 20  t position;     
3f7e3 28 32 20 6d 6f 72 65 20 74 68 61 6e 20 74 68 65  (2 more than the
3f7e4 20 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65 76   delta from prev
3f7e5 69 6f 75 73 20 70 6f 73 69 74 69 6f 6e 29 0a 2a  ious position).*
3f7e6 2a 20 20 20 7d 0a 2a 2a 20 20 20 61 72 72 61 79  *   }.**   array
3f7e7 20 7b 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74   {.**     varint
3f7e8 20 50 4f 53 5f 43 4f 4c 55 4d 4e 3b 20 20 20 28   POS_COLUMN;   (
3f7e9 6d 61 72 6b 73 20 73 74 61 72 74 20 6f 66 20 70  marks start of p
3f7ea 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72  osition list for
3f7eb 20 6e 65 77 20 63 6f 6c 75 6d 6e 29 0a 2a 2a 20   new column).** 
3f7ec 20 20 20 20 76 61 72 69 6e 74 20 63 6f 6c 75 6d      varint colum
3f7ed 6e 3b 20 20 20 20 20 20 20 28 69 6e 64 65 78 20  n;       (index 
3f7ee 6f 66 20 6e 65 77 20 63 6f 6c 75 6d 6e 29 0a 2a  of new column).*
3f7ef 2a 20 20 20 20 20 61 72 72 61 79 20 7b 0a 2a 2a  *     array {.**
3f7f0 20 20 20 20 20 20 20 76 61 72 69 6e 74 20 70 6f         varint po
3f7f1 73 69 74 69 6f 6e 3b 20 20 20 28 32 20 6d 6f 72  sition;   (2 mor
3f7f2 65 20 74 68 61 6e 20 74 68 65 20 64 65 6c 74 61  e than the delta
3f7f3 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 70   from previous p
3f7f4 6f 73 69 74 69 6f 6e 29 0a 2a 2a 20 20 20 20 20  osition).**     
3f7f5 7d 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20 76 61  }.**   }.**   va
3f7f6 72 69 6e 74 20 50 4f 53 5f 45 4e 44 3b 20 20 20  rint POS_END;   
3f7f7 20 20 20 20 20 28 6d 61 72 6b 73 20 65 6e 64 20       (marks end 
3f7f8 6f 66 20 70 6f 73 69 74 69 6f 6e 73 20 66 6f 72  of positions for
3f7f9 20 74 68 69 73 20 64 6f 63 75 6d 65 6e 74 2e 0a   this document..
3f7fa 2a 2a 20 7d 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c  ** }.**.** Here,
3f7fb 20 61 72 72 61 79 20 7b 20 58 20 7d 20 6d 65 61   array { X } mea
3f7fc 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  ns zero or more 
3f7fd 6f 63 63 75 72 72 65 6e 63 65 73 20 6f 66 20 58  occurrences of X
3f7fe 2c 20 61 64 6a 61 63 65 6e 74 20 69 6e 0a 2a 2a  , adjacent in.**
3f7ff 20 6d 65 6d 6f 72 79 2e 20 20 41 20 22 70 6f 73   memory.  A "pos
3f800 69 74 69 6f 6e 22 20 69 73 20 61 6e 20 69 6e 64  ition" is an ind
3f801 65 78 20 6f 66 20 61 20 74 6f 6b 65 6e 20 69 6e  ex of a token in
3f802 20 74 68 65 20 74 6f 6b 65 6e 20 73 74 72 65 61   the token strea
3f803 6d 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 62  m.** generated b
3f804 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 2e  y the tokenizer.
3f805 20 4e 6f 74 65 20 74 68 61 74 20 50 4f 53 5f 45   Note that POS_E
3f806 4e 44 20 61 6e 64 20 50 4f 53 5f 43 4f 4c 55 4d  ND and POS_COLUM
3f807 4e 20 6f 63 63 75 72 20 0a 2a 2a 20 69 6e 20 74  N occur .** in t
3f808 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 61 6c 20  he same logical 
3f809 70 6c 61 63 65 20 61 73 20 74 68 65 20 70 6f 73  place as the pos
3f80a 69 74 69 6f 6e 20 65 6c 65 6d 65 6e 74 2c 20 61  ition element, a
3f80b 6e 64 20 61 63 74 20 61 73 20 73 65 6e 74 69 6e  nd act as sentin
3f80c 61 6c 73 0a 2a 2a 20 65 6e 64 69 6e 67 20 61 20  als.** ending a 
3f80d 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 61 72  position list ar
3f80e 72 61 79 2e 20 20 50 4f 53 5f 45 4e 44 20 69 73  ray.  POS_END is
3f80f 20 30 2e 20 20 50 4f 53 5f 43 4f 4c 55 4d 4e 20   0.  POS_COLUMN 
3f810 69 73 20 31 2e 0a 2a 2a 20 54 68 65 20 70 6f 73  is 1..** The pos
3f811 69 74 69 6f 6e 73 20 6e 75 6d 62 65 72 73 20 61  itions numbers a
3f812 72 65 20 6e 6f 74 20 73 74 6f 72 65 64 20 6c 69  re not stored li
3f813 74 65 72 61 6c 6c 79 20 62 75 74 20 72 61 74 68  terally but rath
3f814 65 72 20 61 73 20 74 77 6f 20 6d 6f 72 65 0a 2a  er as two more.*
3f815 2a 20 74 68 61 6e 20 74 68 65 20 64 69 66 66 65  * than the diffe
3f816 72 65 6e 63 65 20 66 72 6f 6d 20 74 68 65 20 70  rence from the p
3f817 72 69 6f 72 20 70 6f 73 69 74 69 6f 6e 2c 20 6f  rior position, o
3f818 72 20 74 68 65 20 6a 75 73 74 20 74 68 65 20 70  r the just the p
3f819 6f 73 69 74 69 6f 6e 20 70 6c 75 73 0a 2a 2a 20  osition plus.** 
3f81a 32 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  2 for the first 
3f81b 70 6f 73 69 74 69 6f 6e 2e 20 20 45 78 61 6d 70  position.  Examp
3f81c 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 6c 61 62 65  le:.**.**   labe
3f81d 6c 3a 20 20 20 20 20 20 20 41 20 42 20 43 20 44  l:       A B C D
3f81e 20 45 20 20 46 20 20 47 20 48 20 20 20 49 20 20   E  F  G H   I  
3f81f 4a 20 4b 0a 2a 2a 20 20 20 76 61 6c 75 65 3a 20  J K.**   value: 
3f820 20 20 20 20 31 32 33 20 35 20 39 20 31 20 31 20      123 5 9 1 1 
3f821 31 34 20 33 35 20 30 20 32 33 34 20 37 32 20 30  14 35 0 234 72 0
3f822 0a 2a 2a 0a 2a 2a 20 54 68 65 20 31 32 33 20 76  .**.** The 123 v
3f823 61 6c 75 65 20 69 73 20 74 68 65 20 66 69 72 73  alue is the firs
3f824 74 20 64 6f 63 69 64 2e 20 20 46 6f 72 20 63 6f  t docid.  For co
3f825 6c 75 6d 6e 20 7a 65 72 6f 20 69 6e 20 74 68 69  lumn zero in thi
3f826 73 20 64 6f 63 75 6d 65 6e 74 0a 2a 2a 20 74 68  s document.** th
3f827 65 72 65 20 61 72 65 20 74 77 6f 20 6d 61 74 63  ere are two matc
3f828 68 65 73 20 61 74 20 70 6f 73 69 74 69 6f 6e 73  hes at positions
3f829 20 33 20 61 6e 64 20 31 30 20 28 35 2d 32 20 61   3 and 10 (5-2 a
3f82a 6e 64 20 39 2d 32 2b 33 29 2e 20 20 54 68 65 20  nd 9-2+3).  The 
3f82b 31 0a 2a 2a 20 61 74 20 44 20 73 69 67 6e 61 6c  1.** at D signal
3f82c 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 61  s the start of a
3f82d 20 6e 65 77 20 63 6f 6c 75 6d 6e 3b 20 74 68 65   new column; the
3f82e 20 31 20 61 74 20 45 20 69 6e 64 69 63 61 74 65   1 at E indicate
3f82f 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 6e 65  s that the.** ne
3f830 77 20 63 6f 6c 75 6d 6e 20 69 73 20 63 6f 6c 75  w column is colu
3f831 6d 6e 20 6e 75 6d 62 65 72 20 31 2e 20 20 54 68  mn number 1.  Th
3f832 65 72 65 20 61 72 65 20 74 77 6f 20 70 6f 73 69  ere are two posi
3f833 74 69 6f 6e 73 20 61 74 20 31 32 20 61 6e 64 20  tions at 12 and 
3f834 34 35 0a 2a 2a 20 28 31 34 2d 32 20 61 6e 64 20  45.** (14-2 and 
3f835 33 35 2d 32 2b 31 32 29 2e 20 20 54 68 65 20 30  35-2+12).  The 0
3f836 20 61 74 20 48 20 69 6e 64 69 63 61 74 65 20 74   at H indicate t
3f837 68 65 20 65 6e 64 2d 6f 66 2d 64 6f 63 75 6d 65  he end-of-docume
3f838 6e 74 2e 20 20 54 68 65 0a 2a 2a 20 32 33 34 20  nt.  The.** 234 
3f839 61 74 20 49 20 69 73 20 74 68 65 20 64 65 6c 74  at I is the delt
3f83a 61 20 74 6f 20 6e 65 78 74 20 64 6f 63 69 64 20  a to next docid 
3f83b 28 33 35 37 29 2e 20 20 49 74 20 68 61 73 20 6f  (357).  It has o
3f83c 6e 65 20 70 6f 73 69 74 69 6f 6e 20 37 30 0a 2a  ne position 70.*
3f83d 2a 20 28 37 32 2d 32 29 20 61 6e 64 20 74 68 65  * (72-2) and the
3f83e 6e 20 74 65 72 6d 69 6e 61 74 65 73 20 77 69 74  n terminates wit
3f83f 68 20 74 68 65 20 30 20 61 74 20 4b 2e 0a 2a 2a  h the 0 at K..**
3f840 0a 2a 2a 20 41 20 22 70 6f 73 69 74 69 6f 6e 2d  .** A "position-
3f841 6c 69 73 74 22 20 69 73 20 74 68 65 20 6c 69 73  list" is the lis
3f842 74 20 6f 66 20 70 6f 73 69 74 69 6f 6e 73 20 66  t of positions f
3f843 6f 72 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6c 75  or multiple colu
3f844 6d 6e 73 20 66 6f 72 0a 2a 2a 20 61 20 73 69 6e  mns for.** a sin
3f845 67 6c 65 20 64 6f 63 69 64 2e 20 20 41 20 22 63  gle docid.  A "c
3f846 6f 6c 75 6d 6e 2d 6c 69 73 74 22 20 69 73 20 74  olumn-list" is t
3f847 68 65 20 73 65 74 20 6f 66 20 70 6f 73 69 74 69  he set of positi
3f848 6f 6e 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65  ons for a single
3f849 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 20 20 48 65 6e  .** column.  Hen
3f84a 63 65 2c 20 61 20 70 6f 73 69 74 69 6f 6e 2d 6c  ce, a position-l
3f84b 69 73 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  ist consists of 
3f84c 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63 6f 6c 75  one or more colu
3f84d 6d 6e 2d 6c 69 73 74 73 2c 0a 2a 2a 20 61 20 64  mn-lists,.** a d
3f84e 6f 63 75 6d 65 6e 74 20 72 65 63 6f 72 64 20 63  ocument record c
3f84f 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 64 6f 63  onsists of a doc
3f850 69 64 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61  id followed by a
3f851 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 20 61   position-list a
3f852 6e 64 0a 2a 2a 20 61 20 64 6f 63 6c 69 73 74 20  nd.** a doclist 
3f853 63 6f 6e 73 69 73 74 73 20 6f 66 20 6f 6e 65 20  consists of one 
3f854 6f 72 20 6d 6f 72 65 20 64 6f 63 75 6d 65 6e 74  or more document
3f855 20 72 65 63 6f 72 64 73 2e 0a 2a 2a 0a 2a 2a 20   records..**.** 
3f856 41 20 62 61 72 65 20 64 6f 63 6c 69 73 74 20 6f  A bare doclist o
3f857 6d 69 74 73 20 74 68 65 20 70 6f 73 69 74 69 6f  mits the positio
3f858 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 62  n information, b
3f859 65 63 6f 6d 69 6e 67 20 61 6e 20 0a 2a 2a 20 61  ecoming an .** a
3f85a 72 72 61 79 20 6f 66 20 76 61 72 69 6e 74 2d 65  rray of varint-e
3f85b 6e 63 6f 64 65 64 20 64 6f 63 69 64 73 2e 0a 2a  ncoded docids..*
3f85c 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 6c  *.**** Segment l
3f85d 65 61 66 20 6e 6f 64 65 73 20 2a 2a 2a 2a 0a 2a  eaf nodes ****.*
3f85e 2a 20 53 65 67 6d 65 6e 74 20 6c 65 61 66 20 6e  * Segment leaf n
3f85f 6f 64 65 73 20 73 74 6f 72 65 20 74 65 72 6d 73  odes store terms
3f860 20 61 6e 64 20 64 6f 63 6c 69 73 74 73 2c 20 6f   and doclists, o
3f861 72 64 65 72 65 64 20 62 79 20 74 65 72 6d 2e 20  rdered by term. 
3f862 20 4c 65 61 66 0a 2a 2a 20 6e 6f 64 65 73 20 61   Leaf.** nodes a
3f863 72 65 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67  re written using
3f864 20 4c 65 61 66 57 72 69 74 65 72 2c 20 61 6e 64   LeafWriter, and
3f865 20 72 65 61 64 20 75 73 69 6e 67 20 4c 65 61 66   read using Leaf
3f866 52 65 61 64 65 72 20 28 74 6f 0a 2a 2a 20 69 74  Reader (to.** it
3f867 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 20  erate through a 
3f868 73 69 6e 67 6c 65 20 6c 65 61 66 20 6e 6f 64 65  single leaf node
3f869 27 73 20 64 61 74 61 29 20 61 6e 64 20 4c 65 61  's data) and Lea
3f86a 76 65 73 52 65 61 64 65 72 20 28 74 6f 0a 2a 2a  vesReader (to.**
3f86b 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
3f86c 20 61 20 73 65 67 6d 65 6e 74 27 73 20 65 6e 74   a segment's ent
3f86d 69 72 65 20 6c 65 61 66 20 6c 61 79 65 72 29 2e  ire leaf layer).
3f86e 20 20 4c 65 61 66 20 6e 6f 64 65 73 20 68 61 76    Leaf nodes hav
3f86f 65 0a 2a 2a 20 74 68 65 20 66 6f 72 6d 61 74 3a  e.** the format:
3f870 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74 20 69 48  .**.** varint iH
3f871 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20  eight;          
3f872 20 20 20 28 68 65 69 67 68 74 20 66 72 6f 6d 20     (height from 
3f873 6c 65 61 66 20 6c 65 76 65 6c 2c 20 61 6c 77 61  leaf level, alwa
3f874 79 73 20 30 29 0a 2a 2a 20 76 61 72 69 6e 74 20  ys 0).** varint 
3f875 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  nTerm;          
3f876 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20       (length of 
3f877 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20 63  first term).** c
3f878 68 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d  har pTerm[nTerm]
3f879 3b 20 20 20 20 20 20 20 20 20 20 28 63 6f 6e 74  ;          (cont
3f87a 65 6e 74 20 6f 66 20 66 69 72 73 74 20 74 65 72  ent of first ter
3f87b 6d 29 0a 2a 2a 20 76 61 72 69 6e 74 20 6e 44 6f  m).** varint nDo
3f87c 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  clist;          
3f87d 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 74 65 72    (length of ter
3f87e 6d 27 73 20 61 73 73 6f 63 69 61 74 65 64 20 64  m's associated d
3f87f 6f 63 6c 69 73 74 29 0a 2a 2a 20 63 68 61 72 20  oclist).** char 
3f880 70 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c 69 73  pDoclist[nDoclis
3f881 74 5d 3b 20 20 20 20 28 63 6f 6e 74 65 6e 74 20  t];    (content 
3f882 6f 66 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 61  of doclist).** a
3f883 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20 20 20 20  rray {.**       
3f884 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f885 20 20 20 20 20 20 28 66 75 72 74 68 65 72 20 74        (further t
3f886 65 72 6d 73 20 61 72 65 20 64 65 6c 74 61 2d 65  erms are delta-e
3f887 6e 63 6f 64 65 64 29 0a 2a 2a 20 20 20 76 61 72  ncoded).**   var
3f888 69 6e 74 20 6e 50 72 65 66 69 78 3b 20 20 20 20  int nPrefix;    
3f889 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f         (length o
3f88a 66 20 70 72 65 66 69 78 20 73 68 61 72 65 64 20  f prefix shared 
3f88b 77 69 74 68 20 70 72 65 76 69 6f 75 73 20 74 65  with previous te
3f88c 72 6d 29 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20  rm).**   varint 
3f88d 6e 53 75 66 66 69 78 3b 20 20 20 20 20 20 20 20  nSuffix;        
3f88e 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 75 6e     (length of un
3f88f 73 68 61 72 65 64 20 73 75 66 66 69 78 29 0a 2a  shared suffix).*
3f890 2a 20 20 20 63 68 61 72 20 70 54 65 72 6d 53 75  *   char pTermSu
3f891 66 66 69 78 5b 6e 53 75 66 66 69 78 5d 3b 28 75  ffix[nSuffix];(u
3f892 6e 73 68 61 72 65 64 20 73 75 66 66 69 78 20 6f  nshared suffix o
3f893 66 20 6e 65 78 74 20 74 65 72 6d 29 0a 2a 2a 20  f next term).** 
3f894 20 20 76 61 72 69 6e 74 20 6e 44 6f 63 6c 69 73    varint nDoclis
3f895 74 3b 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e  t;          (len
3f896 67 74 68 20 6f 66 20 74 65 72 6d 27 73 20 61 73  gth of term's as
3f897 73 6f 63 69 61 74 65 64 20 64 6f 63 6c 69 73 74  sociated doclist
3f898 29 0a 2a 2a 20 20 20 63 68 61 72 20 70 44 6f 63  ).**   char pDoc
3f899 6c 69 73 74 5b 6e 44 6f 63 6c 69 73 74 5d 3b 20  list[nDoclist]; 
3f89a 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20 64 6f 63   (content of doc
3f89b 6c 69 73 74 29 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a  list).** }.**.**
3f89c 20 48 65 72 65 2c 20 61 72 72 61 79 20 7b 20 58   Here, array { X
3f89d 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72   } means zero or
3f89e 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e 63 65   more occurrence
3f89f 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e 74  s of X, adjacent
3f8a0 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 0a 2a   in.** memory..*
3f8a1 2a 0a 2a 2a 20 4c 65 61 66 20 6e 6f 64 65 73 20  *.** Leaf nodes 
3f8a2 61 72 65 20 62 72 6f 6b 65 6e 20 69 6e 74 6f 20  are broken into 
3f8a3 62 6c 6f 63 6b 73 20 77 68 69 63 68 20 61 72 65  blocks which are
3f8a4 20 73 74 6f 72 65 64 20 63 6f 6e 74 69 67 75 6f   stored contiguo
3f8a5 75 73 6c 79 20 69 6e 0a 2a 2a 20 74 68 65 20 25  usly in.** the %
3f8a6 5f 73 65 67 6d 65 6e 74 73 20 74 61 62 6c 65 20  _segments table 
3f8a7 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e  in sorted order.
3f8a8 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61    This means tha
3f8a9 74 20 77 68 65 6e 20 74 68 65 20 65 6e 64 0a 2a  t when the end.*
3f8aa 2a 20 6f 66 20 61 20 6e 6f 64 65 20 69 73 20 72  * of a node is r
3f8ab 65 61 63 68 65 64 2c 20 74 68 65 20 6e 65 78 74  eached, the next
3f8ac 20 74 65 72 6d 20 69 73 20 69 6e 20 74 68 65 20   term is in the 
3f8ad 6e 6f 64 65 20 77 69 74 68 20 74 68 65 20 6e 65  node with the ne
3f8ae 78 74 0a 2a 2a 20 67 72 65 61 74 65 72 20 6e 6f  xt.** greater no
3f8af 64 65 20 69 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77  de id..**.** New
3f8b0 20 64 61 74 61 20 69 73 20 73 70 69 6c 6c 65 64   data is spilled
3f8b1 20 74 6f 20 61 20 6e 65 77 20 6c 65 61 66 20 6e   to a new leaf n
3f8b2 6f 64 65 20 77 68 65 6e 20 74 68 65 20 63 75 72  ode when the cur
3f8b3 72 65 6e 74 20 6e 6f 64 65 0a 2a 2a 20 65 78 63  rent node.** exc
3f8b4 65 65 64 73 20 4c 45 41 46 5f 4d 41 58 20 62 79  eeds LEAF_MAX by
3f8b5 74 65 73 20 28 64 65 66 61 75 6c 74 20 32 30 34  tes (default 204
3f8b6 38 29 2e 20 20 4e 65 77 20 64 61 74 61 20 77 68  8).  New data wh
3f8b7 69 63 68 20 69 74 73 65 6c 66 20 69 73 0a 2a 2a  ich itself is.**
3f8b8 20 6c 61 72 67 65 72 20 74 68 61 6e 20 53 54 41   larger than STA
3f8b9 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 28 64 65 66  NDALONE_MIN (def
3f8ba 61 75 6c 74 20 31 30 32 34 29 20 69 73 20 70 6c  ault 1024) is pl
3f8bb 61 63 65 64 20 69 6e 20 61 20 73 74 61 6e 64 61  aced in a standa
3f8bc 6c 6f 6e 65 0a 2a 2a 20 6e 6f 64 65 20 28 61 20  lone.** node (a 
3f8bd 6c 65 61 66 20 6e 6f 64 65 20 77 69 74 68 20 61  leaf node with a
3f8be 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 61 6e 64   single term and
3f8bf 20 64 6f 63 6c 69 73 74 29 2e 20 20 54 68 65 20   doclist).  The 
3f8c0 67 6f 61 6c 20 6f 66 0a 2a 2a 20 74 68 65 73 65  goal of.** these
3f8c1 20 73 65 74 74 69 6e 67 73 20 69 73 20 74 6f 20   settings is to 
3f8c2 70 61 63 6b 20 74 6f 67 65 74 68 65 72 20 67 72  pack together gr
3f8c3 6f 75 70 73 20 6f 66 20 73 6d 61 6c 6c 20 64 6f  oups of small do
3f8c4 63 6c 69 73 74 73 20 77 68 69 6c 65 0a 2a 2a 20  clists while.** 
3f8c5 6d 61 6b 69 6e 67 20 69 74 20 65 66 66 69 63 69  making it effici
3f8c6 65 6e 74 20 74 6f 20 64 69 72 65 63 74 6c 79 20  ent to directly 
3f8c7 61 63 63 65 73 73 20 6c 61 72 67 65 20 64 6f 63  access large doc
3f8c8 6c 69 73 74 73 2e 20 20 54 68 65 0a 2a 2a 20 61  lists.  The.** a
3f8c9 73 73 75 6d 70 74 69 6f 6e 20 69 73 20 74 68 61  ssumption is tha
3f8ca 74 20 6c 61 72 67 65 20 64 6f 63 6c 69 73 74 73  t large doclists
3f8cb 20 72 65 70 72 65 73 65 6e 74 20 74 65 72 6d 73   represent terms
3f8cc 20 77 68 69 63 68 20 61 72 65 20 6d 6f 72 65 0a   which are more.
3f8cd 2a 2a 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20  ** likely to be 
3f8ce 71 75 65 72 79 20 74 61 72 67 65 74 73 2e 0a 2a  query targets..*
3f8cf 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  *.** TODO(shess)
3f8d0 20 49 74 20 6d 61 79 20 62 65 20 75 73 65 66 75   It may be usefu
3f8d1 6c 20 66 6f 72 20 62 6c 6f 63 6b 69 6e 67 20 64  l for blocking d
3f8d2 65 63 69 73 69 6f 6e 73 20 74 6f 20 62 65 20 6d  ecisions to be m
3f8d3 6f 72 65 0a 2a 2a 20 64 79 6e 61 6d 69 63 2e 20  ore.** dynamic. 
3f8d4 20 46 6f 72 20 69 6e 73 74 61 6e 63 65 2c 20 69   For instance, i
3f8d5 74 20 6d 61 79 20 6d 61 6b 65 20 6d 6f 72 65 20  t may make more 
3f8d6 73 65 6e 73 65 20 74 6f 20 68 61 76 65 20 61 20  sense to have a 
3f8d7 32 2e 35 6b 20 6c 65 61 66 0a 2a 2a 20 6e 6f 64  2.5k leaf.** nod
3f8d8 65 20 72 61 74 68 65 72 20 74 68 61 6e 20 73 70  e rather than sp
3f8d9 6c 69 74 74 69 6e 67 20 69 6e 74 6f 20 32 6b 20  litting into 2k 
3f8da 61 6e 64 20 2e 35 6b 20 6e 6f 64 65 73 2e 20 20  and .5k nodes.  
3f8db 4d 79 20 69 6e 74 75 69 74 69 6f 6e 20 69 73 0a  My intuition is.
3f8dc 2a 2a 20 74 68 61 74 20 74 68 69 73 20 6d 69 67  ** that this mig
3f8dd 68 74 20 65 78 74 65 6e 64 20 74 68 72 6f 75 67  ht extend throug
3f8de 68 20 32 78 20 6f 72 20 34 78 20 74 68 65 20 70  h 2x or 4x the p
3f8df 61 67 65 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 0a 2a  agesize..**.**.*
3f8e0 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e 74 65  *** Segment inte
3f8e1 72 69 6f 72 20 6e 6f 64 65 73 20 2a 2a 2a 2a 0a  rior nodes ****.
3f8e2 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e 74 65 72  ** Segment inter
3f8e3 69 6f 72 20 6e 6f 64 65 73 20 73 74 6f 72 65 20  ior nodes store 
3f8e4 62 6c 6f 63 6b 69 64 73 20 66 6f 72 20 73 75 62  blockids for sub
3f8e5 74 72 65 65 20 6e 6f 64 65 73 20 61 6e 64 20 74  tree nodes and t
3f8e6 65 72 6d 73 0a 2a 2a 20 74 6f 20 64 65 73 63 72  erms.** to descr
3f8e7 69 62 65 20 77 68 61 74 20 64 61 74 61 20 69 73  ibe what data is
3f8e8 20 73 74 6f 72 65 64 20 62 79 20 74 68 65 20 65   stored by the e
3f8e9 61 63 68 20 73 75 62 74 72 65 65 2e 20 20 49 6e  ach subtree.  In
3f8ea 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64 65 73 20  terior.** nodes 
3f8eb 61 72 65 20 77 72 69 74 74 65 6e 20 75 73 69 6e  are written usin
3f8ec 67 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72  g InteriorWriter
3f8ed 2c 20 61 6e 64 20 72 65 61 64 20 75 73 69 6e 67  , and read using
3f8ee 0a 2a 2a 20 49 6e 74 65 72 69 6f 72 52 65 61 64  .** InteriorRead
3f8ef 65 72 2e 20 20 49 6e 74 65 72 69 6f 72 57 72 69  er.  InteriorWri
3f8f0 74 65 72 73 20 61 72 65 20 63 72 65 61 74 65 64  ters are created
3f8f1 20 61 73 20 6e 65 65 64 65 64 20 77 68 65 6e 0a   as needed when.
3f8f2 2a 2a 20 53 65 67 6d 65 6e 74 57 72 69 74 65 72  ** SegmentWriter
3f8f3 20 63 72 65 61 74 65 73 20 6e 65 77 20 6c 65 61   creates new lea
3f8f4 66 20 6e 6f 64 65 73 2c 20 6f 72 20 77 68 65 6e  f nodes, or when
3f8f5 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64   an interior nod
3f8f6 65 0a 2a 2a 20 69 74 73 65 6c 66 20 67 72 6f 77  e.** itself grow
3f8f7 73 20 74 6f 6f 20 62 69 67 20 61 6e 64 20 6d 75  s too big and mu
3f8f8 73 74 20 62 65 20 73 70 6c 69 74 2e 20 20 54 68  st be split.  Th
3f8f9 65 20 66 6f 72 6d 61 74 20 6f 66 20 69 6e 74 65  e format of inte
3f8fa 72 69 6f 72 0a 2a 2a 20 6e 6f 64 65 73 3a 0a 2a  rior.** nodes:.*
3f8fb 2a 0a 2a 2a 20 76 61 72 69 6e 74 20 69 48 65 69  *.** varint iHei
3f8fc 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 28  ght;           (
3f8fd 68 65 69 67 68 74 20 66 72 6f 6d 20 6c 65 61 66  height from leaf
3f8fe 20 6c 65 76 65 6c 2c 20 61 6c 77 61 79 73 20 3e   level, always >
3f8ff 30 29 0a 2a 2a 20 76 61 72 69 6e 74 20 69 42 6c  0).** varint iBl
3f900 6f 63 6b 69 64 3b 20 20 20 20 20 20 20 20 20 20  ockid;          
3f901 28 62 6c 6f 63 6b 20 69 64 20 6f 66 20 6e 6f 64  (block id of nod
3f902 65 27 73 20 6c 65 66 74 6d 6f 73 74 20 73 75 62  e's leftmost sub
3f903 74 72 65 65 29 0a 2a 2a 20 6f 70 74 69 6f 6e 61  tree).** optiona
3f904 6c 20 7b 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20  l {.**   varint 
3f905 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  nTerm;          
3f906 20 28 6c 65 6e 67 74 68 20 6f 66 20 66 69 72 73   (length of firs
3f907 74 20 74 65 72 6d 29 0a 2a 2a 20 20 20 63 68 61  t term).**   cha
3f908 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 3b 20  r pTerm[nTerm]; 
3f909 20 20 20 20 20 28 63 6f 6e 74 65 6e 74 20 6f 66       (content of
3f90a 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20   first term).** 
3f90b 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20    array {.**    
3f90c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3f90d 20 20 20 20 20 20 20 20 20 20 20 20 28 66 75 72              (fur
3f90e 74 68 65 72 20 74 65 72 6d 73 20 61 72 65 20 64  ther terms are d
3f90f 65 6c 74 61 2d 65 6e 63 6f 64 65 64 29 0a 2a 2a  elta-encoded).**
3f910 20 20 20 20 20 76 61 72 69 6e 74 20 6e 50 72 65       varint nPre
3f911 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  fix;            
3f912 28 6c 65 6e 67 74 68 20 6f 66 20 73 68 61 72 65  (length of share
3f913 64 20 70 72 65 66 69 78 20 77 69 74 68 20 70 72  d prefix with pr
3f914 65 76 69 6f 75 73 20 74 65 72 6d 29 0a 2a 2a 20  evious term).** 
3f915 20 20 20 20 76 61 72 69 6e 74 20 6e 53 75 66 66      varint nSuff
3f916 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 28  ix;            (
3f917 6c 65 6e 67 74 68 20 6f 66 20 75 6e 73 68 61 72  length of unshar
3f918 65 64 20 73 75 66 66 69 78 29 0a 2a 2a 20 20 20  ed suffix).**   
3f919 20 20 63 68 61 72 20 70 54 65 72 6d 53 75 66 66    char pTermSuff
3f91a 69 78 5b 6e 53 75 66 66 69 78 5d 3b 20 28 75 6e  ix[nSuffix]; (un
3f91b 73 68 61 72 65 64 20 73 75 66 66 69 78 20 6f 66  shared suffix of
3f91c 20 6e 65 78 74 20 74 65 72 6d 29 0a 2a 2a 20 20   next term).**  
3f91d 20 7d 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a 20 48 65   }.** }.**.** He
3f91e 72 65 2c 20 6f 70 74 69 6f 6e 61 6c 20 7b 20 58  re, optional { X
3f91f 20 7d 20 6d 65 61 6e 73 20 61 6e 20 6f 70 74 69   } means an opti
3f920 6f 6e 61 6c 20 65 6c 65 6d 65 6e 74 2c 20 77 68  onal element, wh
3f921 69 6c 65 20 61 72 72 61 79 20 7b 20 58 20 7d 0a  ile array { X }.
3f922 2a 2a 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72  ** means zero or
3f923 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e 63 65   more occurrence
3f924 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e 74  s of X, adjacent
3f925 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a   in memory..**.*
3f926 2a 20 41 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  * An interior no
3f927 64 65 20 65 6e 63 6f 64 65 73 20 6e 20 74 65 72  de encodes n ter
3f928 6d 73 20 73 65 70 61 72 61 74 69 6e 67 20 6e 2b  ms separating n+
3f929 31 20 73 75 62 74 72 65 65 73 2e 20 20 54 68 65  1 subtrees.  The
3f92a 0a 2a 2a 20 73 75 62 74 72 65 65 20 62 6c 6f 63  .** subtree bloc
3f92b 6b 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75  ks are contiguou
3f92c 73 2c 20 73 6f 20 6f 6e 6c 79 20 74 68 65 20 66  s, so only the f
3f92d 69 72 73 74 20 73 75 62 74 72 65 65 27 73 20 62  irst subtree's b
3f92e 6c 6f 63 6b 69 64 0a 2a 2a 20 69 73 20 65 6e 63  lockid.** is enc
3f92f 6f 64 65 64 2e 20 20 54 68 65 20 73 75 62 74 72  oded.  The subtr
3f930 65 65 20 61 74 20 69 42 6c 6f 63 6b 69 64 20 77  ee at iBlockid w
3f931 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 6c 6c 20  ill contain all 
3f932 74 65 72 6d 73 20 6c 65 73 73 0a 2a 2a 20 74 68  terms less.** th
3f933 61 6e 20 74 68 65 20 66 69 72 73 74 20 74 65 72  an the first ter
3f934 6d 20 65 6e 63 6f 64 65 64 20 28 6f 72 20 61 6c  m encoded (or al
3f935 6c 20 74 65 72 6d 73 20 69 66 20 6e 6f 20 74 65  l terms if no te
3f936 72 6d 20 69 73 20 65 6e 63 6f 64 65 64 29 2e 0a  rm is encoded)..
3f937 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6f  ** Otherwise, fo
3f938 72 20 74 65 72 6d 73 20 67 72 65 61 74 65 72 20  r terms greater 
3f939 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
3f93a 20 70 54 65 72 6d 5b 69 5d 20 62 75 74 20 6c 65   pTerm[i] but le
3f93b 73 73 0a 2a 2a 20 74 68 61 6e 20 70 54 65 72 6d  ss.** than pTerm
3f93c 5b 69 2b 31 5d 2c 20 74 68 65 20 73 75 62 74 72  [i+1], the subtr
3f93d 65 65 20 66 6f 72 20 74 68 61 74 20 74 65 72 6d  ee for that term
3f93e 20 77 69 6c 6c 20 62 65 20 72 6f 6f 74 65 64 20   will be rooted 
3f93f 61 74 0a 2a 2a 20 69 42 6c 6f 63 6b 69 64 2b 69  at.** iBlockid+i
3f940 2e 20 20 49 6e 74 65 72 69 6f 72 20 6e 6f 64 65  .  Interior node
3f941 73 20 6f 6e 6c 79 20 73 74 6f 72 65 20 65 6e 6f  s only store eno
3f942 75 67 68 20 74 65 72 6d 20 64 61 74 61 20 74 6f  ugh term data to
3f943 0a 2a 2a 20 64 69 73 74 69 6e 67 75 69 73 68 20  .** distinguish 
3f944 61 64 6a 61 63 65 6e 74 20 63 68 69 6c 64 72 65  adjacent childre
3f945 6e 20 28 69 66 20 74 68 65 20 72 69 67 68 74 6d  n (if the rightm
3f946 6f 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ost term of the 
3f947 6c 65 66 74 0a 2a 2a 20 63 68 69 6c 64 20 69 73  left.** child is
3f948 20 22 73 6f 6d 65 74 68 69 6e 67 22 2c 20 61 6e   "something", an
3f949 64 20 74 68 65 20 6c 65 66 74 6d 6f 73 74 20 74  d the leftmost t
3f94a 65 72 6d 20 6f 66 20 74 68 65 20 72 69 67 68 74  erm of the right
3f94b 20 63 68 69 6c 64 20 69 73 0a 2a 2a 20 22 77 69   child is.** "wi
3f94c 63 6b 65 64 22 2c 20 6f 6e 6c 79 20 22 77 22 20  cked", only "w" 
3f94d 69 73 20 73 74 6f 72 65 64 29 2e 0a 2a 2a 0a 2a  is stored)..**.*
3f94e 2a 20 4e 65 77 20 64 61 74 61 20 69 73 20 73 70  * New data is sp
3f94f 69 6c 6c 65 64 20 74 6f 20 61 20 6e 65 77 20 69  illed to a new i
3f950 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 61 74 20  nterior node at 
3f951 74 68 65 20 73 61 6d 65 20 68 65 69 67 68 74 20  the same height 
3f952 77 68 65 6e 0a 2a 2a 20 74 68 65 20 63 75 72 72  when.** the curr
3f953 65 6e 74 20 6e 6f 64 65 20 65 78 63 65 65 64 73  ent node exceeds
3f954 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 62 79   INTERIOR_MAX by
3f955 74 65 73 20 28 64 65 66 61 75 6c 74 20 32 30 34  tes (default 204
3f956 38 29 2e 0a 2a 2a 20 49 4e 54 45 52 49 4f 52 5f  8)..** INTERIOR_
3f957 4d 49 4e 5f 54 45 52 4d 53 20 28 64 65 66 61 75  MIN_TERMS (defau
3f958 6c 74 20 37 29 20 6b 65 65 70 73 20 6c 61 72 67  lt 7) keeps larg
3f959 65 20 74 65 72 6d 73 20 66 72 6f 6d 20 6d 6f 6e  e terms from mon
3f95a 6f 70 6f 6c 69 7a 69 6e 67 0a 2a 2a 20 69 6e 74  opolizing.** int
3f95b 65 72 69 6f 72 20 6e 6f 64 65 73 20 61 6e 64 20  erior nodes and 
3f95c 6d 61 6b 69 6e 67 20 74 68 65 20 74 72 65 65 20  making the tree 
3f95d 74 6f 6f 20 73 6b 69 6e 6e 79 2e 20 20 54 68 65  too skinny.  The
3f95e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 0a   interior nodes.
3f95f 2a 2a 20 61 74 20 61 20 67 69 76 65 6e 20 68 65  ** at a given he
3f960 69 67 68 74 20 61 72 65 20 6e 61 74 75 72 61 6c  ight are natural
3f961 6c 79 20 74 72 61 63 6b 65 64 20 62 79 20 69 6e  ly tracked by in
3f962 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 61 74 0a  terior nodes at.
3f963 2a 2a 20 68 65 69 67 68 74 2b 31 2c 20 61 6e 64  ** height+1, and
3f964 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a   so on..**.**.**
3f965 2a 2a 20 53 65 67 6d 65 6e 74 20 64 69 72 65 63  ** Segment direc
3f966 74 6f 72 79 20 2a 2a 2a 2a 0a 2a 2a 20 54 68 65  tory ****.** The
3f967 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74 6f   segment directo
3f968 72 79 20 69 6e 20 74 61 62 6c 65 20 25 5f 73 65  ry in table %_se
3f969 67 64 69 72 20 73 74 6f 72 65 73 20 6d 65 74 61  gdir stores meta
3f96a 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72  -information for
3f96b 0a 2a 2a 20 6d 65 72 67 69 6e 67 20 61 6e 64 20  .** merging and 
3f96c 64 65 6c 65 74 69 6e 67 20 73 65 67 6d 65 6e 74  deleting segment
3f96d 73 2c 20 61 6e 64 20 61 6c 73 6f 20 74 68 65 20  s, and also the 
3f96e 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 74 68 65  root node of the
3f96f 0a 2a 2a 20 73 65 67 6d 65 6e 74 27 73 20 74 72  .** segment's tr
3f970 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 6f  ee..**.** The ro
3f971 6f 74 20 6e 6f 64 65 20 69 73 20 74 68 65 20 74  ot node is the t
3f972 6f 70 20 6e 6f 64 65 20 6f 66 20 74 68 65 20 73  op node of the s
3f973 65 67 6d 65 6e 74 27 73 20 74 72 65 65 20 61 66  egment's tree af
3f974 74 65 72 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20  ter encoding.** 
3f975 74 68 65 20 65 6e 74 69 72 65 20 73 65 67 6d 65  the entire segme
3f976 6e 74 2c 20 72 65 73 74 72 69 63 74 65 64 20 74  nt, restricted t
3f977 6f 20 52 4f 4f 54 5f 4d 41 58 20 62 79 74 65 73  o ROOT_MAX bytes
3f978 20 28 64 65 66 61 75 6c 74 20 31 30 32 34 29 2e   (default 1024).
3f979 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c 64 20 62  .** This could b
3f97a 65 20 65 69 74 68 65 72 20 61 20 6c 65 61 66 20  e either a leaf 
3f97b 6e 6f 64 65 20 6f 72 20 61 6e 20 69 6e 74 65 72  node or an inter
3f97c 69 6f 72 20 6e 6f 64 65 2e 20 20 49 66 20 74 68  ior node.  If th
3f97d 65 20 74 6f 70 0a 2a 2a 20 6e 6f 64 65 20 72 65  e top.** node re
3f97e 71 75 69 72 65 73 20 6d 6f 72 65 20 74 68 61 6e  quires more than
3f97f 20 52 4f 4f 54 5f 4d 41 58 20 62 79 74 65 73 2c   ROOT_MAX bytes,
3f980 20 69 74 20 69 73 20 66 6c 75 73 68 65 64 20 74   it is flushed t
3f981 6f 20 25 5f 73 65 67 6d 65 6e 74 73 0a 2a 2a 20  o %_segments.** 
3f982 61 6e 64 20 61 20 6e 65 77 20 72 6f 6f 74 20 69  and a new root i
3f983 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 69 73 20  nterior node is 
3f984 67 65 6e 65 72 61 74 65 64 20 28 77 68 69 63 68  generated (which
3f985 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 66   should always f
3f986 69 74 0a 2a 2a 20 77 69 74 68 69 6e 20 52 4f 4f  it.** within ROO
3f987 54 5f 4d 41 58 20 62 65 63 61 75 73 65 20 69 74  T_MAX because it
3f988 20 6f 6e 6c 79 20 6e 65 65 64 73 20 73 70 61 63   only needs spac
3f989 65 20 66 6f 72 20 32 20 76 61 72 69 6e 74 73 2c  e for 2 varints,
3f98a 20 74 68 65 0a 2a 2a 20 68 65 69 67 68 74 20 61   the.** height a
3f98b 6e 64 20 74 68 65 20 62 6c 6f 63 6b 69 64 20 6f  nd the blockid o
3f98c 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 72  f the previous r
3f98d 6f 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  oot)..**.** The 
3f98e 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e  meta-information
3f98f 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 20   in the segment 
3f990 64 69 72 65 63 74 6f 72 79 20 69 73 3a 0a 2a 2a  directory is:.**
3f991 20 20 20 6c 65 76 65 6c 20 20 20 20 20 20 20 20     level        
3f992 20 20 20 20 20 20 20 2d 20 73 65 67 6d 65 6e 74         - segment
3f993 20 6c 65 76 65 6c 20 28 73 65 65 20 62 65 6c 6f   level (see belo
3f994 77 29 0a 2a 2a 20 20 20 69 64 78 20 20 20 20 20  w).**   idx     
3f995 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 69 6e              - in
3f996 64 65 78 20 77 69 74 68 69 6e 20 6c 65 76 65 6c  dex within level
3f997 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3f998 20 20 20 20 20 20 20 20 20 20 2d 20 28 6c 65 76            - (lev
3f999 65 6c 2c 69 64 78 20 75 6e 69 71 75 65 6c 79 20  el,idx uniquely 
3f99a 69 64 65 6e 74 69 66 79 20 61 20 73 65 67 6d 65  identify a segme
3f99b 6e 74 29 0a 2a 2a 20 20 20 73 74 61 72 74 5f 62  nt).**   start_b
3f99c 6c 6f 63 6b 20 20 20 20 20 20 20 20 20 2d 20 66  lock         - f
3f99d 69 72 73 74 20 6c 65 61 66 20 6e 6f 64 65 0a 2a  irst leaf node.*
3f99e 2a 20 20 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62  *   leaves_end_b
3f99f 6c 6f 63 6b 20 20 20 20 2d 20 6c 61 73 74 20 6c  lock    - last l
3f9a0 65 61 66 20 6e 6f 64 65 0a 2a 2a 20 20 20 65 6e  eaf node.**   en
3f9a1 64 5f 62 6c 6f 63 6b 20 20 20 20 20 20 20 20 20  d_block         
3f9a2 20 20 2d 20 6c 61 73 74 20 62 6c 6f 63 6b 20 28    - last block (
3f9a3 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65 72 69  including interi
3f9a4 6f 72 20 6e 6f 64 65 73 29 0a 2a 2a 20 20 20 72  or nodes).**   r
3f9a5 6f 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20  oot             
3f9a6 20 20 20 2d 20 63 6f 6e 74 65 6e 74 73 20 6f 66     - contents of
3f9a7 20 72 6f 6f 74 20 6e 6f 64 65 0a 2a 2a 0a 2a 2a   root node.**.**
3f9a8 20 49 66 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64   If the root nod
3f9a9 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f 64 65  e is a leaf node
3f9aa 2c 20 74 68 65 6e 20 73 74 61 72 74 5f 62 6c 6f  , then start_blo
3f9ab 63 6b 2c 0a 2a 2a 20 6c 65 61 76 65 73 5f 65 6e  ck,.** leaves_en
3f9ac 64 5f 62 6c 6f 63 6b 2c 20 61 6e 64 20 65 6e 64  d_block, and end
3f9ad 5f 62 6c 6f 63 6b 20 61 72 65 20 61 6c 6c 20 30  _block are all 0
3f9ae 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67  ..**.**.**** Seg
3f9af 6d 65 6e 74 20 6d 65 72 67 69 6e 67 20 2a 2a 2a  ment merging ***
3f9b0 2a 0a 2a 2a 20 54 6f 20 61 6d 6f 72 74 69 7a 65  *.** To amortize
3f9b1 20 75 70 64 61 74 65 20 63 6f 73 74 73 2c 20 73   update costs, s
3f9b2 65 67 6d 65 6e 74 73 20 61 72 65 20 67 72 6f 75  egments are grou
3f9b3 70 65 64 20 69 6e 74 6f 20 6c 65 76 65 6c 73 20  ped into levels 
3f9b4 61 6e 64 0a 2a 2a 20 6d 65 72 67 65 64 20 69 6e  and.** merged in
3f9b5 20 62 61 74 63 68 65 73 2e 20 20 45 61 63 68 20   batches.  Each 
3f9b6 69 6e 63 72 65 61 73 65 20 69 6e 20 6c 65 76 65  increase in leve
3f9b7 6c 20 72 65 70 72 65 73 65 6e 74 73 20 65 78 70  l represents exp
3f9b8 6f 6e 65 6e 74 69 61 6c 6c 79 0a 2a 2a 20 6d 6f  onentially.** mo
3f9b9 72 65 20 64 6f 63 75 6d 65 6e 74 73 2e 0a 2a 2a  re documents..**
3f9ba 0a 2a 2a 20 4e 65 77 20 64 6f 63 75 6d 65 6e 74  .** New document
3f9bb 73 20 28 61 63 74 75 61 6c 6c 79 2c 20 64 6f 63  s (actually, doc
3f9bc 75 6d 65 6e 74 20 75 70 64 61 74 65 73 29 20 61  ument updates) a
3f9bd 72 65 20 74 6f 6b 65 6e 69 7a 65 64 20 61 6e 64  re tokenized and
3f9be 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 6e 64 69  .** written indi
3f9bf 76 69 64 75 61 6c 6c 79 20 28 75 73 69 6e 67 20  vidually (using 
3f9c0 4c 65 61 66 57 72 69 74 65 72 29 20 74 6f 20 61  LeafWriter) to a
3f9c1 20 6c 65 76 65 6c 20 30 20 73 65 67 6d 65 6e 74   level 0 segment
3f9c2 2c 20 77 69 74 68 0a 2a 2a 20 69 6e 63 72 65 6d  , with.** increm
3f9c3 65 6e 74 69 6e 67 20 69 64 78 2e 20 20 57 68 65  enting idx.  Whe
3f9c4 6e 20 69 64 78 20 72 65 61 63 68 65 73 20 4d 45  n idx reaches ME
3f9c5 52 47 45 5f 43 4f 55 4e 54 20 28 64 65 66 61 75  RGE_COUNT (defau
3f9c6 6c 74 20 31 36 29 2c 20 61 6c 6c 0a 2a 2a 20 6c  lt 16), all.** l
3f9c7 65 76 65 6c 20 30 20 73 65 67 6d 65 6e 74 73 20  evel 0 segments 
3f9c8 61 72 65 20 6d 65 72 67 65 64 20 69 6e 74 6f 20  are merged into 
3f9c9 61 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 31  a single level 1
3f9ca 20 73 65 67 6d 65 6e 74 2e 20 20 4c 65 76 65 6c   segment.  Level
3f9cb 20 31 0a 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74   1.** is populat
3f9cc 65 64 20 6c 69 6b 65 20 6c 65 76 65 6c 20 30 2c  ed like level 0,
3f9cd 20 61 6e 64 20 65 76 65 6e 74 75 61 6c 6c 79 20   and eventually 
3f9ce 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6c 65 76 65  MERGE_COUNT leve
3f9cf 6c 20 31 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20  l 1.** segments 
3f9d0 61 72 65 20 6d 65 72 67 65 64 20 74 6f 20 61 20  are merged to a 
3f9d1 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 32 20 73  single level 2 s
3f9d2 65 67 6d 65 6e 74 20 28 72 65 70 72 65 73 65 6e  egment (represen
3f9d3 74 69 6e 67 0a 2a 2a 20 4d 45 52 47 45 5f 43 4f  ting.** MERGE_CO
3f9d4 55 4e 54 5e 32 20 75 70 64 61 74 65 73 29 2c 20  UNT^2 updates), 
3f9d5 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a  and so on..**.**
3f9d6 20 41 20 73 65 67 6d 65 6e 74 20 6d 65 72 67 65   A segment merge
3f9d7 20 74 72 61 76 65 72 73 65 73 20 61 6c 6c 20 73   traverses all s
3f9d8 65 67 6d 65 6e 74 73 20 61 74 20 61 20 67 69 76  egments at a giv
3f9d9 65 6e 20 6c 65 76 65 6c 20 69 6e 0a 2a 2a 20 70  en level in.** p
3f9da 61 72 61 6c 6c 65 6c 2c 20 70 65 72 66 6f 72 6d  arallel, perform
3f9db 69 6e 67 20 61 20 73 74 72 61 69 67 68 74 66 6f  ing a straightfo
3f9dc 72 77 61 72 64 20 73 6f 72 74 65 64 20 6d 65 72  rward sorted mer
3f9dd 67 65 2e 20 20 53 69 6e 63 65 20 73 65 67 6d 65  ge.  Since segme
3f9de 6e 74 0a 2a 2a 20 6c 65 61 66 20 6e 6f 64 65 73  nt.** leaf nodes
3f9df 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 6e 20   are written in 
3f9e0 74 6f 20 74 68 65 20 25 5f 73 65 67 6d 65 6e 74  to the %_segment
3f9e1 73 20 74 61 62 6c 65 20 69 6e 20 6f 72 64 65 72  s table in order
3f9e2 2c 20 74 68 69 73 0a 2a 2a 20 6d 65 72 67 65 20  , this.** merge 
3f9e3 74 72 61 76 65 72 73 65 73 20 74 68 65 20 75 6e  traverses the un
3f9e4 64 65 72 6c 79 69 6e 67 20 73 71 6c 69 74 65 20  derlying sqlite 
3f9e5 64 69 73 6b 20 73 74 72 75 63 74 75 72 65 73 20  disk structures 
3f9e6 65 66 66 69 63 69 65 6e 74 6c 79 2e 0a 2a 2a 20  efficiently..** 
3f9e7 41 66 74 65 72 20 74 68 65 20 6d 65 72 67 65 2c  After the merge,
3f9e8 20 61 6c 6c 20 73 65 67 6d 65 6e 74 20 62 6c 6f   all segment blo
3f9e9 63 6b 73 20 66 72 6f 6d 20 74 68 65 20 6d 65 72  cks from the mer
3f9ea 67 65 64 20 6c 65 76 65 6c 20 61 72 65 0a 2a 2a  ged level are.**
3f9eb 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   deleted..**.** 
3f9ec 4d 45 52 47 45 5f 43 4f 55 4e 54 20 63 6f 6e 74  MERGE_COUNT cont
3f9ed 72 6f 6c 73 20 68 6f 77 20 6f 66 74 65 6e 20 77  rols how often w
3f9ee 65 20 6d 65 72 67 65 20 73 65 67 6d 65 6e 74 73  e merge segments
3f9ef 2e 20 20 31 36 20 73 65 65 6d 73 20 74 6f 20 62  .  16 seems to b
3f9f0 65 0a 2a 2a 20 73 6f 6d 65 77 68 61 74 20 6f 66  e.** somewhat of
3f9f1 20 61 20 73 77 65 65 74 20 73 70 6f 74 20 66 6f   a sweet spot fo
3f9f2 72 20 69 6e 73 65 72 74 69 6f 6e 20 70 65 72 66  r insertion perf
3f9f3 6f 72 6d 61 6e 63 65 2e 20 20 33 32 20 61 6e 64  ormance.  32 and
3f9f4 20 36 34 20 73 68 6f 77 0a 2a 2a 20 76 65 72 79   64 show.** very
3f9f5 20 73 69 6d 69 6c 61 72 20 70 65 72 66 6f 72 6d   similar perform
3f9f6 61 6e 63 65 20 6e 75 6d 62 65 72 73 20 74 6f 20  ance numbers to 
3f9f7 31 36 20 6f 6e 20 69 6e 73 65 72 74 69 6f 6e 2c  16 on insertion,
3f9f8 20 74 68 6f 75 67 68 20 74 68 65 79 27 72 65 0a   though they're.
3f9f9 2a 2a 20 61 20 74 69 6e 79 20 62 69 74 20 73 6c  ** a tiny bit sl
3f9fa 6f 77 65 72 20 28 70 65 72 68 61 70 73 20 64 75  ower (perhaps du
3f9fb 65 20 74 6f 20 6d 6f 72 65 20 6f 76 65 72 68 65  e to more overhe
3f9fc 61 64 20 69 6e 20 6d 65 72 67 65 2d 74 69 6d 65  ad in merge-time
3f9fd 0a 2a 2a 20 73 6f 72 74 69 6e 67 29 2e 20 20 38  .** sorting).  8
3f9fe 20 69 73 20 61 62 6f 75 74 20 32 30 25 20 73 6c   is about 20% sl
3f9ff 6f 77 65 72 20 74 68 61 6e 20 31 36 2c 20 34 20  ower than 16, 4 
3fa00 61 62 6f 75 74 20 35 30 25 20 73 6c 6f 77 65 72  about 50% slower
3fa01 20 74 68 61 6e 0a 2a 2a 20 31 36 2c 20 32 20 61   than.** 16, 2 a
3fa02 62 6f 75 74 20 36 36 25 20 73 6c 6f 77 65 72 20  bout 66% slower 
3fa03 74 68 61 6e 20 31 36 2e 0a 2a 2a 0a 2a 2a 20 41  than 16..**.** A
3fa04 74 20 71 75 65 72 79 20 74 69 6d 65 2c 20 68 69  t query time, hi
3fa05 67 68 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 69  gh MERGE_COUNT i
3fa06 6e 63 72 65 61 73 65 73 20 74 68 65 20 6e 75 6d  ncreases the num
3fa07 62 65 72 20 6f 66 20 73 65 67 6d 65 6e 74 73 0a  ber of segments.
3fa08 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 20 74 6f  ** which need to
3fa09 20 62 65 20 73 63 61 6e 6e 65 64 20 61 6e 64 20   be scanned and 
3fa0a 6d 65 72 67 65 64 2e 20 20 46 6f 72 20 69 6e 73  merged.  For ins
3fa0b 74 61 6e 63 65 2c 20 77 69 74 68 20 31 30 30 6b  tance, with 100k
3fa0c 20 64 6f 63 73 0a 2a 2a 20 69 6e 73 65 72 74 65   docs.** inserte
3fa0d 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 45 52 47  d:.**.**    MERG
3fa0e 45 5f 43 4f 55 4e 54 20 20 20 73 65 67 6d 65 6e  E_COUNT   segmen
3fa0f 74 73 0a 2a 2a 20 20 20 20 20 20 20 31 36 20 20  ts.**       16  
3fa10 20 20 20 20 20 20 20 20 20 32 35 0a 2a 2a 20 20           25.**  
3fa11 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 20        8         
3fa12 20 20 31 32 0a 2a 2a 20 20 20 20 20 20 20 20 34    12.**        4
3fa13 20 20 20 20 20 20 20 20 20 20 20 31 30 0a 2a 2a             10.**
3fa14 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20          2       
3fa15 20 20 20 20 20 36 0a 2a 2a 0a 2a 2a 20 54 68 69       6.**.** Thi
3fa16 73 20 61 70 70 65 61 72 73 20 74 6f 20 68 61 76  s appears to hav
3fa17 65 20 6f 6e 6c 79 20 61 20 6d 6f 64 65 72 61 74  e only a moderat
3fa18 65 20 69 6d 70 61 63 74 20 6f 6e 20 71 75 65 72  e impact on quer
3fa19 69 65 73 20 66 6f 72 20 76 65 72 79 0a 2a 2a 20  ies for very.** 
3fa1a 66 72 65 71 75 65 6e 74 20 74 65 72 6d 73 20 28  frequent terms (
3fa1b 77 68 69 63 68 20 61 72 65 20 73 6f 6d 65 77 68  which are somewh
3fa1c 61 74 20 64 6f 6d 69 6e 61 74 65 64 20 62 79 20  at dominated by 
3fa1d 73 65 67 6d 65 6e 74 20 6d 65 72 67 65 0a 2a 2a  segment merge.**
3fa1e 20 63 6f 73 74 73 29 2c 20 61 6e 64 20 69 6e 66   costs), and inf
3fa1f 72 65 71 75 65 6e 74 20 61 6e 64 20 6e 6f 6e 2d  requent and non-
3fa20 65 78 69 73 74 65 6e 74 20 74 65 72 6d 73 20 73  existent terms s
3fa21 74 69 6c 6c 20 73 65 65 6d 20 74 6f 20 62 65 20  till seem to be 
3fa22 66 61 73 74 0a 2a 2a 20 65 76 65 6e 20 77 69 74  fast.** even wit
3fa23 68 20 6d 61 6e 79 20 73 65 67 6d 65 6e 74 73 2e  h many segments.
3fa24 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73  .**.** TODO(shes
3fa25 73 29 20 54 68 61 74 20 73 61 69 64 2c 20 69 74  s) That said, it
3fa26 20 77 6f 75 6c 64 20 62 65 20 6e 69 63 65 20 74   would be nice t
3fa27 6f 20 68 61 76 65 20 61 20 62 65 74 74 65 72 20  o have a better 
3fa28 71 75 65 72 79 2d 73 69 64 65 0a 2a 2a 20 61 72  query-side.** ar
3fa29 67 75 6d 65 6e 74 20 66 6f 72 20 4d 45 52 47 45  gument for MERGE
3fa2a 5f 43 4f 55 4e 54 20 6f 66 20 31 36 2e 20 20 41  _COUNT of 16.  A
3fa2b 6c 73 6f 2c 20 69 74 20 69 73 20 70 6f 73 73 69  lso, it is possi
3fa2c 62 6c 65 2f 6c 69 6b 65 6c 79 20 74 68 61 74 0a  ble/likely that.
3fa2d 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73  ** optimizations
3fa2e 20 74 6f 20 74 68 69 6e 67 73 20 6c 69 6b 65 20   to things like 
3fa2f 64 6f 63 6c 69 73 74 20 6d 65 72 67 69 6e 67 20  doclist merging 
3fa30 77 69 6c 6c 20 73 77 69 6e 67 20 74 68 65 20 73  will swing the s
3fa31 77 65 65 74 0a 2a 2a 20 73 70 6f 74 20 61 72 6f  weet.** spot aro
3fa32 75 6e 64 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a  und..**.**.**.**
3fa33 2a 2a 20 48 61 6e 64 6c 69 6e 67 20 6f 66 20 64  ** Handling of d
3fa34 65 6c 65 74 69 6f 6e 73 20 61 6e 64 20 75 70 64  eletions and upd
3fa35 61 74 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53 69 6e  ates ****.** Sin
3fa36 63 65 20 77 65 27 72 65 20 75 73 69 6e 67 20 61  ce we're using a
3fa37 20 73 65 67 6d 65 6e 74 65 64 20 73 74 72 75 63   segmented struc
3fa38 74 75 72 65 2c 20 77 69 74 68 20 6e 6f 20 64 6f  ture, with no do
3fa39 63 69 64 2d 6f 72 69 65 6e 74 65 64 0a 2a 2a 20  cid-oriented.** 
3fa3a 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20 74  index into the t
3fa3b 65 72 6d 20 69 6e 64 65 78 2c 20 77 65 20 63 6c  erm index, we cl
3fa3c 65 61 72 6c 79 20 63 61 6e 6e 6f 74 20 73 69 6d  early cannot sim
3fa3d 70 6c 79 20 75 70 64 61 74 65 20 74 68 65 20 74  ply update the t
3fa3e 65 72 6d 0a 2a 2a 20 69 6e 64 65 78 20 77 68 65  erm.** index whe
3fa3f 6e 20 61 20 64 6f 63 75 6d 65 6e 74 20 69 73 20  n a document is 
3fa40 64 65 6c 65 74 65 64 20 6f 72 20 75 70 64 61 74  deleted or updat
3fa41 65 64 2e 20 20 46 6f 72 20 64 65 6c 65 74 69 6f  ed.  For deletio
3fa42 6e 73 2c 20 77 65 0a 2a 2a 20 77 72 69 74 65 20  ns, we.** write 
3fa43 61 6e 20 65 6d 70 74 79 20 64 6f 63 6c 69 73 74  an empty doclist
3fa44 20 28 76 61 72 69 6e 74 28 64 6f 63 69 64 29 20   (varint(docid) 
3fa45 76 61 72 69 6e 74 28 50 4f 53 5f 45 4e 44 29 29  varint(POS_END))
3fa46 2c 20 66 6f 72 20 75 70 64 61 74 65 73 0a 2a 2a  , for updates.**
3fa47 20 77 65 20 73 69 6d 70 6c 79 20 77 72 69 74 65   we simply write
3fa48 20 74 68 65 20 6e 65 77 20 64 6f 63 6c 69 73 74   the new doclist
3fa49 2e 20 20 53 65 67 6d 65 6e 74 20 6d 65 72 67 65  .  Segment merge
3fa4a 73 20 6f 76 65 72 77 72 69 74 65 20 6f 6c 64 65  s overwrite olde
3fa4b 72 0a 2a 2a 20 64 61 74 61 20 66 6f 72 20 61 20  r.** data for a 
3fa4c 70 61 72 74 69 63 75 6c 61 72 20 64 6f 63 69 64  particular docid
3fa4d 20 77 69 74 68 20 6e 65 77 65 72 20 64 61 74 61   with newer data
3fa4e 2c 20 73 6f 20 64 65 6c 65 74 65 73 20 6f 72 20  , so deletes or 
3fa4f 75 70 64 61 74 65 73 0a 2a 2a 20 77 69 6c 6c 20  updates.** will 
3fa50 65 76 65 6e 74 75 61 6c 6c 79 20 6f 76 65 72 74  eventually overt
3fa51 61 6b 65 20 74 68 65 20 65 61 72 6c 69 65 72 20  ake the earlier 
3fa52 64 61 74 61 20 61 6e 64 20 6b 6e 6f 63 6b 20 69  data and knock i
3fa53 74 20 6f 75 74 2e 20 20 54 68 65 0a 2a 2a 20 71  t out.  The.** q
3fa54 75 65 72 79 20 6c 6f 67 69 63 20 6c 69 6b 65 77  uery logic likew
3fa55 69 73 65 20 6d 65 72 67 65 73 20 64 6f 63 6c 69  ise merges docli
3fa56 73 74 73 20 73 6f 20 74 68 61 74 20 6e 65 77 65  sts so that newe
3fa57 72 20 64 61 74 61 20 6b 6e 6f 63 6b 73 20 6f 75  r data knocks ou
3fa58 74 0a 2a 2a 20 6f 6c 64 65 72 20 64 61 74 61 2e  t.** older data.
3fa59 0a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/../**********
3fa5a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74 73  **** Include fts
3fa5b 33 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69  3Int.h in the mi
3fa5c 64 64 6c 65 20 6f 66 20 66 74 73 33 2e 63 20 2a  ddle of fts3.c *
3fa5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fa5e 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
3fa5f 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
3fa60 66 74 73 33 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  fts3Int.h ******
3fa61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fa62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fa63 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20  ***/./*.** 2009 
3fa64 4e 6f 76 20 31 32 0a 2a 2a 0a 2a 2a 20 54 68 65  Nov 12.**.** The
3fa65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
3fa66 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
3fa67 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
3fa68 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
3fa69 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
3fa6a 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
3fa6b 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
3fa6c 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
3fa6d 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
3fa6e 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
3fa6f 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
3fa70 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
3fa71 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
3fa72 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
3fa73 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
3fa74 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
3fa75 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
3fa76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fa77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fa78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fa79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fa7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2f  **********.**.*/
3fa7b 0a 23 69 66 6e 64 65 66 20 5f 46 54 53 49 4e 54  .#ifndef _FTSINT
3fa7c 5f 48 0a 23 64 65 66 69 6e 65 20 5f 46 54 53 49  _H.#define _FTSI
3fa7d 4e 54 5f 48 0a 0a 23 69 66 20 21 64 65 66 69 6e  NT_H..#if !defin
3fa7e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21 64  ed(NDEBUG) && !d
3fa7f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
3fa80 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65 20 4e  BUG) .# define N
3fa81 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66 0a 0a  DEBUG 1.#endif..
3fa82 2f 2a 0a 2a 2a 20 46 54 53 34 20 69 73 20 72 65  /*.** FTS4 is re
3fa83 61 6c 6c 79 20 61 6e 20 65 78 74 65 6e 73 69 6f  ally an extensio
3fa84 6e 20 66 6f 72 20 46 54 53 33 2e 20 20 49 74 20  n for FTS3.  It 
3fa85 69 73 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67  is enabled using
3fa86 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 45   the.** SQLITE_E
3fa87 4e 41 42 4c 45 5f 46 54 53 33 20 6d 61 63 72 6f  NABLE_FTS3 macro
3fa88 2e 20 20 42 75 74 20 74 6f 20 61 76 6f 69 64 20  .  But to avoid 
3fa89 63 6f 6e 66 75 73 69 6f 6e 20 77 65 20 61 6c 73  confusion we als
3fa8a 6f 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 53 51 4c  o all.** the SQL
3fa8b 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 34 20  ITE_ENABLE_FTS4 
3fa8c 6d 61 63 72 6f 20 74 6f 20 73 65 72 76 65 20 61  macro to serve a
3fa8d 73 20 61 6e 20 61 6c 69 73 73 65 20 66 6f 72 20  s an alisse for 
3fa8e 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
3fa8f 53 33 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  S3..*/.#if defin
3fa90 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
3fa91 5f 46 54 53 34 29 20 26 26 20 21 64 65 66 69 6e  _FTS4) && !defin
3fa92 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
3fa93 5f 46 54 53 33 29 0a 23 20 64 65 66 69 6e 65 20  _FTS3).# define 
3fa94 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
3fa95 53 33 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21  S3.#endif..#if !
3fa96 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
3fa97 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORE) || defined(
3fa98 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
3fa99 53 33 29 0a 0a 2f 2a 20 49 66 20 6e 6f 74 20 62  S3)../* If not b
3fa9a 75 69 6c 64 69 6e 67 20 61 73 20 70 61 72 74 20  uilding as part 
3fa9b 6f 66 20 74 68 65 20 63 6f 72 65 2c 20 69 6e 63  of the core, inc
3fa9c 6c 75 64 65 20 73 71 6c 69 74 65 33 65 78 74 2e  lude sqlite3ext.
3fa9d 68 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  h. */.#ifndef SQ
3fa9e 4c 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49 54 45  LITE_CORE.SQLITE
3fa9f 5f 41 50 49 20 65 78 74 65 72 6e 20 63 6f 6e 73  _API extern cons
3faa0 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f  t sqlite3_api_ro
3faa1 75 74 69 6e 65 73 20 2a 73 71 6c 69 74 65 33 5f  utines *sqlite3_
3faa2 61 70 69 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  api;.#endif../**
3faa3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
3faa4 6c 75 64 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69  lude fts3_tokeni
3faa5 7a 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  zer.h in the mid
3faa6 64 6c 65 20 6f 66 20 66 74 73 33 49 6e 74 2e 68  dle of fts3Int.h
3faa7 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a   **********/./**
3faa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
3faa9 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b  in file fts3_tok
3faaa 65 6e 69 7a 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a  enizer.h *******
3faab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3faac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
3faad 2a 2a 20 32 30 30 36 20 4a 75 6c 79 20 31 30 0a  ** 2006 July 10.
3faae 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
3faaf 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
3fab0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
3fab1 72 63 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 2a  rce code..**.***
3fab2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fab3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fab4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fab5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fab6 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 44 65 66 69 6e 65  ******.** Define
3fab7 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20  s the interface 
3fab8 74 6f 20 74 6f 6b 65 6e 69 7a 65 72 73 20 75 73  to tokenizers us
3fab9 65 64 20 62 79 20 66 75 6c 6c 74 65 78 74 2d 73  ed by fulltext-s
3faba 65 61 72 63 68 2e 20 20 54 68 65 72 65 0a 2a 2a  earch.  There.**
3fabb 20 61 72 65 20 74 68 72 65 65 20 62 61 73 69 63   are three basic
3fabc 20 63 6f 6d 70 6f 6e 65 6e 74 73 3a 0a 2a 2a 0a   components:.**.
3fabd 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  ** sqlite3_token
3fabe 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 69 73 20 61  izer_module is a
3fabf 20 73 69 6e 67 6c 65 74 6f 6e 20 64 65 66 69 6e   singleton defin
3fac0 69 6e 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65  ing the tokenize
3fac1 72 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66  r.** interface f
3fac2 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20  unctions.  This 
3fac3 69 73 20 65 73 73 65 6e 74 69 61 6c 6c 79 20 74  is essentially t
3fac4 68 65 20 63 6c 61 73 73 20 73 74 72 75 63 74 75  he class structu
3fac5 72 65 20 66 6f 72 0a 2a 2a 20 74 6f 6b 65 6e 69  re for.** tokeni
3fac6 7a 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69  zers..**.** sqli
3fac7 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 69 73  te3_tokenizer is
3fac8 20 75 73 65 64 20 74 6f 20 64 65 66 69 6e 65 20   used to define 
3fac9 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 6f 6b  a particular tok
3faca 65 6e 69 7a 65 72 2c 20 70 65 72 68 61 70 73 0a  enizer, perhaps.
3facb 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 75 73  ** including cus
3facc 74 6f 6d 69 7a 61 74 69 6f 6e 20 69 6e 66 6f 72  tomization infor
3facd 6d 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 61  mation defined a
3face 74 20 63 72 65 61 74 69 6f 6e 20 74 69 6d 65 2e  t creation time.
3facf 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74  .**.** sqlite3_t
3fad0 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
3fad1 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  is generated by 
3fad2 61 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 20 67  a tokenizer to g
3fad3 65 6e 65 72 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e  enerate.** token
3fad4 73 20 66 72 6f 6d 20 61 20 70 61 72 74 69 63 75  s from a particu
3fad5 6c 61 72 20 69 6e 70 75 74 2e 0a 2a 2f 0a 23 69  lar input..*/.#i
3fad6 66 6e 64 65 66 20 5f 46 54 53 33 5f 54 4f 4b 45  fndef _FTS3_TOKE
3fad7 4e 49 5a 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65  NIZER_H_.#define
3fad8 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45 52   _FTS3_TOKENIZER
3fad9 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  _H_../* TODO(she
3fada 73 73 29 20 4f 6e 6c 79 20 75 73 65 64 20 66 6f  ss) Only used fo
3fadb 72 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20  r SQLITE_OK and 
3fadc 53 51 4c 49 54 45 5f 44 4f 4e 45 20 61 74 20 74  SQLITE_DONE at t
3fadd 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 20 49 66 20  his time..** If 
3fade 74 6f 6b 65 6e 69 7a 65 72 73 20 61 72 65 20 74  tokenizers are t
3fadf 6f 20 62 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20  o be allowed to 
3fae0 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 2a 28 29  call sqlite3_*()
3fae1 20 66 75 6e 63 74 69 6f 6e 73 2c 20 74 68 65 6e   functions, then
3fae2 0a 2a 2a 20 77 65 20 77 69 6c 6c 20 6e 65 65 64  .** we will need
3fae3 20 61 20 77 61 79 20 74 6f 20 72 65 67 69 73 74   a way to regist
3fae4 65 72 20 74 68 65 20 41 50 49 20 63 6f 6e 73 69  er the API consi
3fae5 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 0a 2f 2a 0a  stently..*/../*.
3fae6 2a 2a 20 53 74 72 75 63 74 75 72 65 73 20 75 73  ** Structures us
3fae7 65 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e 69  ed by the tokeni
3fae8 7a 65 72 20 69 6e 74 65 72 66 61 63 65 2e 20 57  zer interface. W
3fae9 68 65 6e 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69  hen a new tokeni
3faea 7a 65 72 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  zer.** implement
3faeb 61 74 69 6f 6e 20 69 73 20 72 65 67 69 73 74 65  ation is registe
3faec 72 65 64 2c 20 74 68 65 20 63 61 6c 6c 65 72 20  red, the caller 
3faed 70 72 6f 76 69 64 65 73 20 61 20 70 6f 69 6e 74  provides a point
3faee 65 72 20 74 6f 0a 2a 2a 20 61 6e 20 73 71 6c 69  er to.** an sqli
3faef 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
3faf0 64 75 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  dule containing 
3faf1 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20  pointers to the 
3faf2 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63  callback.** func
3faf3 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6b 65 20  tions that make 
3faf4 75 70 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  up an implementa
3faf5 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  tion..**.** When
3faf6 20 61 6e 20 66 74 73 33 20 74 61 62 6c 65 20 69   an fts3 table i
3faf7 73 20 63 72 65 61 74 65 64 2c 20 69 74 20 70 61  s created, it pa
3faf8 73 73 65 73 20 61 6e 79 20 61 72 67 75 6d 65 6e  sses any argumen
3faf9 74 73 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20  ts passed to.** 
3fafa 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 6c  the tokenizer cl
3fafb 61 75 73 65 20 6f 66 20 74 68 65 20 43 52 45 41  ause of the CREA
3fafc 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
3fafd 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 74 68   statement to th
3fafe 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b  e.** sqlite3_tok
3faff 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78 43  enizer_module.xC
3fb00 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e  reate() function
3fb01 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65   of the requeste
3fb02 64 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 69  d tokenizer.** i
3fb03 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 54  mplementation. T
3fb04 68 65 20 78 43 72 65 61 74 65 28 29 20 66 75 6e  he xCreate() fun
3fb05 63 74 69 6f 6e 20 69 6e 20 74 75 72 6e 20 72 65  ction in turn re
3fb06 74 75 72 6e 73 20 61 6e 20 0a 2a 2a 20 73 71 6c  turns an .** sql
3fb07 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73  ite3_tokenizer s
3fb08 74 72 75 63 74 75 72 65 20 72 65 70 72 65 73 65  tructure represe
3fb09 6e 74 69 6e 67 20 74 68 65 20 73 70 65 63 69 66  nting the specif
3fb0a 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 0a  ic tokenizer to.
3fb0b 2a 2a 20 62 65 20 75 73 65 64 20 66 6f 72 20 74  ** be used for t
3fb0c 68 65 20 66 74 73 33 20 74 61 62 6c 65 20 28 63  he fts3 table (c
3fb0d 75 73 74 6f 6d 69 7a 65 64 20 62 79 20 74 68 65  ustomized by the
3fb0e 20 74 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75 73   tokenizer claus
3fb0f 65 20 61 72 67 75 6d 65 6e 74 73 29 2e 0a 2a 2a  e arguments)..**
3fb10 0a 2a 2a 20 54 6f 20 74 6f 6b 65 6e 69 7a 65 20  .** To tokenize 
3fb11 61 6e 20 69 6e 70 75 74 20 62 75 66 66 65 72 2c  an input buffer,
3fb12 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b   the sqlite3_tok
3fb13 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78 4f  enizer_module.xO
3fb14 70 65 6e 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20  pen().** method 
3fb15 69 73 20 63 61 6c 6c 65 64 2e 20 49 74 20 72 65  is called. It re
3fb16 74 75 72 6e 73 20 61 6e 20 73 71 6c 69 74 65 33  turns an sqlite3
3fb17 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
3fb18 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74  r object.** that
3fb19 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f 20   may be used to 
3fb1a 74 6f 6b 65 6e 69 7a 65 20 61 20 73 70 65 63 69  tokenize a speci
3fb1b 66 69 63 20 69 6e 70 75 74 20 62 75 66 66 65 72  fic input buffer
3fb1c 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68 65   based on.** the
3fb1d 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 72 75   tokenization ru
3fb1e 6c 65 73 20 73 75 70 70 6c 69 65 64 20 62 79 20  les supplied by 
3fb1f 61 20 73 70 65 63 69 66 69 63 20 73 71 6c 69 74  a specific sqlit
3fb20 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20  e3_tokenizer.** 
3fb21 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65 64  object..*/.typed
3fb22 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
3fb23 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
3fb24 6c 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  le sqlite3_token
3fb25 69 7a 65 72 5f 6d 6f 64 75 6c 65 3b 0a 74 79 70  izer_module;.typ
3fb26 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
3fb27 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73 71  te3_tokenizer sq
3fb28 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 3b  lite3_tokenizer;
3fb29 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
3fb2a 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
3fb2b 72 5f 63 75 72 73 6f 72 20 73 71 6c 69 74 65 33  r_cursor sqlite3
3fb2c 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
3fb2d 72 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69 74  r;..struct sqlit
3fb2e 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
3fb2f 75 6c 65 20 7b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  ule {..  /*.  **
3fb30 20 53 74 72 75 63 74 75 72 65 20 76 65 72 73 69   Structure versi
3fb31 6f 6e 2e 20 53 68 6f 75 6c 64 20 61 6c 77 61 79  on. Should alway
3fb32 73 20 62 65 20 73 65 74 20 74 6f 20 30 20 6f 72  s be set to 0 or
3fb33 20 31 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69   1..  */.  int i
3fb34 56 65 72 73 69 6f 6e 3b 0a 0a 20 20 2f 2a 0a 20  Version;..  /*. 
3fb35 20 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77   ** Create a new
3fb36 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 54 68 65 20   tokenizer. The 
3fb37 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 61 72  values in the ar
3fb38 67 76 5b 5d 20 61 72 72 61 79 20 61 72 65 20 74  gv[] array are t
3fb39 68 65 0a 20 20 2a 2a 20 61 72 67 75 6d 65 6e 74  he.  ** argument
3fb3a 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20  s passed to the 
3fb3b 22 74 6f 6b 65 6e 69 7a 65 72 22 20 63 6c 61 75  "tokenizer" clau
3fb3c 73 65 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  se of the CREATE
3fb3d 20 56 49 52 54 55 41 4c 0a 20 20 2a 2a 20 54 41   VIRTUAL.  ** TA
3fb3e 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 74 68  BLE statement th
3fb3f 61 74 20 63 72 65 61 74 65 64 20 74 68 65 20 66  at created the f
3fb40 74 73 33 20 74 61 62 6c 65 2e 20 46 6f 72 20 65  ts3 table. For e
3fb41 78 61 6d 70 6c 65 2c 20 69 66 0a 20 20 2a 2a 20  xample, if.  ** 
3fb42 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 51  the following SQ
3fb43 4c 20 69 73 20 65 78 65 63 75 74 65 64 3a 0a 20  L is executed:. 
3fb44 20 2a 2a 0a 20 20 2a 2a 20 20 20 43 52 45 41 54   **.  **   CREAT
3fb45 45 20 2e 2e 20 55 53 49 4e 47 20 66 74 73 33 28  E .. USING fts3(
3fb46 20 2e 2e 2e 20 2c 20 74 6f 6b 65 6e 69 7a 65 72   ... , tokenizer
3fb47 20 3c 74 6f 6b 65 6e 69 7a 65 72 2d 6e 61 6d 65   <tokenizer-name
3fb48 3e 20 61 72 67 31 20 61 72 67 32 29 0a 20 20 2a  > arg1 arg2).  *
3fb49 2a 0a 20 20 2a 2a 20 74 68 65 6e 20 61 72 67 63  *.  ** then argc
3fb4a 20 69 73 20 73 65 74 20 74 6f 20 32 2c 20 61 6e   is set to 2, an
3fb4b 64 20 74 68 65 20 61 72 67 76 5b 5d 20 61 72 72  d the argv[] arr
3fb4c 61 79 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e  ay contains poin
3fb4d 74 65 72 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65  ters.  ** to the
3fb4e 20 73 74 72 69 6e 67 73 20 22 61 72 67 31 22 20   strings "arg1" 
3fb4f 61 6e 64 20 22 61 72 67 32 22 2e 0a 20 20 2a 2a  and "arg2"..  **
3fb50 0a 20 20 2a 2a 20 54 68 69 73 20 6d 65 74 68 6f  .  ** This metho
3fb51 64 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20  d should return 
3fb52 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b  either SQLITE_OK
3fb53 20 28 30 29 2c 20 6f 72 20 61 6e 20 53 51 4c 69   (0), or an SQLi
3fb54 74 65 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20 63  te error .  ** c
3fb55 6f 64 65 2e 20 49 66 20 53 51 4c 49 54 45 5f 4f  ode. If SQLITE_O
3fb56 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74  K is returned, t
3fb57 68 65 6e 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72  hen *ppTokenizer
3fb58 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 0a 20   should be set. 
3fb59 20 2a 2a 20 74 6f 20 70 6f 69 6e 74 20 61 74 20   ** to point at 
3fb5a 74 68 65 20 6e 65 77 6c 79 20 63 72 65 61 74 65  the newly create
3fb5b 64 20 74 6f 6b 65 6e 69 7a 65 72 20 73 74 72 75  d tokenizer stru
3fb5c 63 74 75 72 65 2e 20 54 68 65 20 67 65 6e 65 72  cture. The gener
3fb5d 69 63 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  ic.  ** sqlite3_
3fb5e 74 6f 6b 65 6e 69 7a 65 72 2e 70 4d 6f 64 75 6c  tokenizer.pModul
3fb5f 65 20 76 61 72 69 61 62 6c 65 20 73 68 6f 75 6c  e variable shoul
3fb60 64 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c  d not be initial
3fb61 69 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 68 69  ised by.  ** thi
3fb62 73 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 65 20  s callback. The 
3fb63 63 61 6c 6c 65 72 20 77 69 6c 6c 20 64 6f 20 73  caller will do s
3fb64 6f 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  o..  */.  int (*
3fb65 78 43 72 65 61 74 65 29 28 0a 20 20 20 20 69 6e  xCreate)(.    in
3fb66 74 20 61 72 67 63 2c 20 20 20 20 20 20 20 20 20  t argc,         
3fb67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fb68 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72 67    /* Size of arg
3fb69 76 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 63  v array */.    c
3fb6a 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
3fb6b 2a 61 72 67 76 2c 20 20 20 20 20 20 20 20 20 20  *argv,          
3fb6c 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20     /* Tokenizer 
3fb6d 61 72 67 75 6d 65 6e 74 20 73 74 72 69 6e 67 73  argument strings
3fb6e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   */.    sqlite3_
3fb6f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f  tokenizer **ppTo
3fb70 6b 65 6e 69 7a 65 72 20 20 20 20 20 2f 2a 20 4f  kenizer     /* O
3fb71 55 54 3a 20 43 72 65 61 74 65 64 20 74 6f 6b 65  UT: Created toke
3fb72 6e 69 7a 65 72 20 2a 2f 0a 20 20 29 3b 0a 0a 20  nizer */.  );.. 
3fb73 20 2f 2a 0a 20 20 2a 2a 20 44 65 73 74 72 6f 79   /*.  ** Destroy
3fb74 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74 6f 6b   an existing tok
3fb75 65 6e 69 7a 65 72 2e 20 54 68 65 20 66 74 73 33  enizer. The fts3
3fb76 20 6d 6f 64 75 6c 65 20 63 61 6c 6c 73 20 74 68   module calls th
3fb77 69 73 20 6d 65 74 68 6f 64 0a 20 20 2a 2a 20 65  is method.  ** e
3fb78 78 61 63 74 6c 79 20 6f 6e 63 65 20 66 6f 72 20  xactly once for 
3fb79 65 61 63 68 20 73 75 63 63 65 73 73 66 75 6c 20  each successful 
3fb7a 63 61 6c 6c 20 74 6f 20 78 43 72 65 61 74 65 28  call to xCreate(
3fb7b 29 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  )..  */.  int (*
3fb7c 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65  xDestroy)(sqlite
3fb7d 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
3fb7e 6b 65 6e 69 7a 65 72 29 3b 0a 0a 20 20 2f 2a 0a  kenizer);..  /*.
3fb7f 20 20 2a 2a 20 43 72 65 61 74 65 20 61 20 74 6f    ** Create a to
3fb80 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 74  kenizer cursor t
3fb81 6f 20 74 6f 6b 65 6e 69 7a 65 20 61 6e 20 69 6e  o tokenize an in
3fb82 70 75 74 20 62 75 66 66 65 72 2e 20 54 68 65 20  put buffer. The 
3fb83 63 61 6c 6c 65 72 0a 20 20 2a 2a 20 69 73 20 72  caller.  ** is r
3fb84 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65  esponsible for e
3fb85 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 68 65  nsuring that the
3fb86 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 72 65   input buffer re
3fb87 6d 61 69 6e 73 20 76 61 6c 69 64 0a 20 20 2a 2a  mains valid.  **
3fb88 20 75 6e 74 69 6c 20 74 68 65 20 63 75 72 73 6f   until the curso
3fb89 72 20 69 73 20 63 6c 6f 73 65 64 20 28 75 73 69  r is closed (usi
3fb8a 6e 67 20 74 68 65 20 78 43 6c 6f 73 65 28 29 20  ng the xClose() 
3fb8b 6d 65 74 68 6f 64 29 2e 20 0a 20 20 2a 2f 0a 20  method). .  */. 
3fb8c 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 0a 20   int (*xOpen)(. 
3fb8d 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e     sqlite3_token
3fb8e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
3fb8f 2c 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e  ,       /* Token
3fb90 69 7a 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  izer object */. 
3fb91 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
3fb92 49 6e 70 75 74 2c 20 69 6e 74 20 6e 42 79 74 65  Input, int nByte
3fb93 73 2c 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74  s,      /* Input
3fb94 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 73   buffer */.    s
3fb95 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3fb96 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73  _cursor **ppCurs
3fb97 6f 72 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65 61  or  /* OUT: Crea
3fb98 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 63 75  ted tokenizer cu
3fb99 72 73 6f 72 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20  rsor */.  );..  
3fb9a 2f 2a 0a 20 20 2a 2a 20 44 65 73 74 72 6f 79 20  /*.  ** Destroy 
3fb9b 61 6e 20 65 78 69 73 74 69 6e 67 20 74 6f 6b 65  an existing toke
3fb9c 6e 69 7a 65 72 20 63 75 72 73 6f 72 2e 20 54 68  nizer cursor. Th
3fb9d 65 20 66 74 73 33 20 6d 6f 64 75 6c 65 20 63 61  e fts3 module ca
3fb9e 6c 6c 73 20 74 68 69 73 20 0a 20 20 2a 2a 20 6d  lls this .  ** m
3fb9f 65 74 68 6f 64 20 65 78 61 63 74 6c 79 20 6f 6e  ethod exactly on
3fba0 63 65 20 66 6f 72 20 65 61 63 68 20 73 75 63 63  ce for each succ
3fba1 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 78  essful call to x
3fba2 4f 70 65 6e 28 29 2e 0a 20 20 2a 2f 0a 20 20 69  Open()..  */.  i
3fba3 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c  nt (*xClose)(sql
3fba4 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
3fba5 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 3b  ursor *pCursor);
3fba6 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 72  ..  /*.  ** Retr
3fba7 69 65 76 65 20 74 68 65 20 6e 65 78 74 20 74 6f  ieve the next to
3fba8 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 74 6f 6b  ken from the tok
3fba9 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 70 43  enizer cursor pC
3fbaa 75 72 73 6f 72 2e 20 54 68 69 73 0a 20 20 2a 2a  ursor. This.  **
3fbab 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20 65   method should e
3fbac 69 74 68 65 72 20 72 65 74 75 72 6e 20 53 51 4c  ither return SQL
3fbad 49 54 45 5f 4f 4b 20 61 6e 64 20 73 65 74 20 74  ITE_OK and set t
3fbae 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65  he values of the
3fbaf 0a 20 20 2a 2a 20 22 4f 55 54 22 20 76 61 72 69  .  ** "OUT" vari
3fbb0 61 62 6c 65 73 20 69 64 65 6e 74 69 66 69 65 64  ables identified
3fbb1 20 62 65 6c 6f 77 2c 20 6f 72 20 53 51 4c 49 54   below, or SQLIT
3fbb2 45 5f 44 4f 4e 45 20 74 6f 20 69 6e 64 69 63 61  E_DONE to indica
3fbb3 74 65 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65  te that.  ** the
3fbb4 20 65 6e 64 20 6f 66 20 74 68 65 20 62 75 66 66   end of the buff
3fbb5 65 72 20 68 61 73 20 62 65 65 6e 20 72 65 61 63  er has been reac
3fbb6 68 65 64 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74  hed, or an SQLit
3fbb7 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 20 20  e error code..  
3fbb8 2a 2a 0a 20 20 2a 2a 20 2a 70 70 54 6f 6b 65 6e  **.  ** *ppToken
3fbb9 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74   should be set t
3fbba 6f 20 70 6f 69 6e 74 20 61 74 20 61 20 62 75 66  o point at a buf
3fbbb 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  fer containing t
3fbbc 68 65 20 0a 20 20 2a 2a 20 6e 6f 72 6d 61 6c 69  he .  ** normali
3fbbd 7a 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  zed version of t
3fbbe 68 65 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 61  he token (i.e. a
3fbbf 66 74 65 72 20 61 6e 79 20 63 61 73 65 2d 66 6f  fter any case-fo
3fbc0 6c 64 69 6e 67 20 61 6e 64 2f 6f 72 0a 20 20 2a  lding and/or.  *
3fbc1 2a 20 73 74 65 6d 6d 69 6e 67 20 68 61 73 20 62  * stemming has b
3fbc2 65 65 6e 20 70 65 72 66 6f 72 6d 65 64 29 2e 20  een performed). 
3fbc3 2a 70 6e 42 79 74 65 73 20 73 68 6f 75 6c 64 20  *pnBytes should 
3fbc4 62 65 20 73 65 74 20 74 6f 20 74 68 65 20 6c 65  be set to the le
3fbc5 6e 67 74 68 0a 20 20 2a 2a 20 6f 66 20 74 68 69  ngth.  ** of thi
3fbc6 73 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65  s buffer in byte
3fbc7 73 2e 20 54 68 65 20 69 6e 70 75 74 20 74 65 78  s. The input tex
3fbc8 74 20 74 68 61 74 20 67 65 6e 65 72 61 74 65 64  t that generated
3fbc9 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 0a 20 20   the token is.  
3fbca 2a 2a 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  ** identified by
3fbcb 20 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74   the byte offset
3fbcc 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70  s returned in *p
3fbcd 69 53 74 61 72 74 4f 66 66 73 65 74 20 61 6e 64  iStartOffset and
3fbce 0a 20 20 2a 2a 20 2a 70 69 45 6e 64 4f 66 66 73  .  ** *piEndOffs
3fbcf 65 74 2e 20 2a 70 69 53 74 61 72 74 4f 66 66 73  et. *piStartOffs
3fbd0 65 74 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74  et should be set
3fbd1 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66   to the index of
3fbd2 20 74 68 65 20 66 69 72 73 74 0a 20 20 2a 2a 20   the first.  ** 
3fbd3 62 79 74 65 20 6f 66 20 74 68 65 20 74 6f 6b 65  byte of the toke
3fbd4 6e 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20 62  n in the input b
3fbd5 75 66 66 65 72 2e 20 2a 70 69 45 6e 64 4f 66 66  uffer. *piEndOff
3fbd6 73 65 74 20 73 68 6f 75 6c 64 20 62 65 20 73 65  set should be se
3fbd7 74 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 69 6e  t.  ** to the in
3fbd8 64 65 78 20 6f 66 20 74 68 65 20 66 69 72 73 74  dex of the first
3fbd9 20 62 79 74 65 20 6a 75 73 74 20 70 61 73 74 20   byte just past 
3fbda 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74  the end of the t
3fbdb 6f 6b 65 6e 20 69 6e 0a 20 20 2a 2a 20 74 68 65  oken in.  ** the
3fbdc 20 69 6e 70 75 74 20 62 75 66 66 65 72 2e 0a 20   input buffer.. 
3fbdd 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 62 75 66   **.  ** The buf
3fbde 66 65 72 20 2a 70 70 54 6f 6b 65 6e 20 69 73 20  fer *ppToken is 
3fbdf 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20  set to point at 
3fbe0 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 74 68  is managed by th
3fbe1 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a  e tokenizer.  **
3fbe2 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
3fbe3 20 49 74 20 69 73 20 6f 6e 6c 79 20 72 65 71 75   It is only requ
3fbe4 69 72 65 64 20 74 6f 20 62 65 20 76 61 6c 69 64  ired to be valid
3fbe5 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20   until the next 
3fbe6 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 78 4e 65  call.  ** to xNe
3fbe7 78 74 28 29 20 6f 72 20 78 43 6c 6f 73 65 28 29  xt() or xClose()
3fbe8 2e 20 0a 20 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44  . .  */.  /* TOD
3fbe9 4f 28 73 68 65 73 73 29 20 63 75 72 72 65 6e 74  O(shess) current
3fbea 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3fbeb 72 65 71 75 69 72 65 73 20 70 49 6e 70 75 74 20  requires pInput 
3fbec 74 6f 20 62 65 0a 20 20 2a 2a 20 6e 75 6c 2d 74  to be.  ** nul-t
3fbed 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 69 73  erminated.  This
3fbee 20 73 68 6f 75 6c 64 20 65 69 74 68 65 72 20 62   should either b
3fbef 65 20 66 69 78 65 64 2c 20 6f 72 20 70 49 6e 70  e fixed, or pInp
3fbf0 75 74 2f 6e 42 79 74 65 73 0a 20 20 2a 2a 20 73  ut/nBytes.  ** s
3fbf1 68 6f 75 6c 64 20 62 65 20 63 6f 6e 76 65 72 74  hould be convert
3fbf2 65 64 20 74 6f 20 7a 49 6e 70 75 74 2e 0a 20 20  ed to zInput..  
3fbf3 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74  */.  int (*xNext
3fbf4 29 28 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 74  )(.    sqlite3_t
3fbf5 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
3fbf6 2a 70 43 75 72 73 6f 72 2c 20 20 20 2f 2a 20 54  *pCursor,   /* T
3fbf7 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20  okenizer cursor 
3fbf8 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  */.    const cha
3fbf9 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20 69 6e 74  r **ppToken, int
3fbfa 20 2a 70 6e 42 79 74 65 73 2c 20 20 2f 2a 20 4f   *pnBytes,  /* O
3fbfb 55 54 3a 20 4e 6f 72 6d 61 6c 69 7a 65 64 20 74  UT: Normalized t
3fbfc 65 78 74 20 66 6f 72 20 74 6f 6b 65 6e 20 2a 2f  ext for token */
3fbfd 0a 20 20 20 20 69 6e 74 20 2a 70 69 53 74 61 72  .    int *piStar
3fbfe 74 4f 66 66 73 65 74 2c 20 20 2f 2a 20 4f 55 54  tOffset,  /* OUT
3fbff 3a 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66  : Byte offset of
3fc00 20 74 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74 20   token in input 
3fc01 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 69 6e  buffer */.    in
3fc02 74 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20  t *piEndOffset, 
3fc03 20 20 20 2f 2a 20 4f 55 54 3a 20 42 79 74 65 20     /* OUT: Byte 
3fc04 6f 66 66 73 65 74 20 6f 66 20 65 6e 64 20 6f 66  offset of end of
3fc05 20 74 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74 20   token in input 
3fc06 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 69 6e  buffer */.    in
3fc07 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20  t *piPosition   
3fc08 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65     /* OUT: Numbe
3fc09 72 20 6f 66 20 74 6f 6b 65 6e 73 20 72 65 74 75  r of tokens retu
3fc0a 72 6e 65 64 20 62 65 66 6f 72 65 20 74 68 69 73  rned before this
3fc0b 20 6f 6e 65 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20   one */.  );..  
3fc0c 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
3fc0d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fc0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fc0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fc10 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 4d 65  ********.  ** Me
3fc11 74 68 6f 64 73 20 62 65 6c 6f 77 20 74 68 69 73  thods below this
3fc12 20 70 6f 69 6e 74 20 61 72 65 20 6f 6e 6c 79 20   point are only 
3fc13 61 76 61 69 6c 61 62 6c 65 20 69 66 20 69 56 65  available if iVe
3fc14 72 73 69 6f 6e 3e 3d 31 2e 0a 20 20 2a 2f 0a 0a  rsion>=1..  */..
3fc15 20 20 2f 2a 20 0a 20 20 2a 2a 20 43 6f 6e 66 69    /* .  ** Confi
3fc16 67 75 72 65 20 74 68 65 20 6c 61 6e 67 75 61 67  gure the languag
3fc17 65 20 69 64 20 6f 66 20 61 20 74 6f 6b 65 6e 69  e id of a tokeni
3fc18 7a 65 72 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f  zer cursor..  */
3fc19 0a 20 20 69 6e 74 20 28 2a 78 4c 61 6e 67 75 61  .  int (*xLangua
3fc1a 67 65 69 64 29 28 73 71 6c 69 74 65 33 5f 74 6f  geid)(sqlite3_to
3fc1b 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
3fc1c 70 43 73 72 2c 20 69 6e 74 20 69 4c 61 6e 67 69  pCsr, int iLangi
3fc1d 64 29 3b 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 73  d);.};..struct s
3fc1e 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3fc1f 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74   {.  const sqlit
3fc20 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
3fc21 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 2f  ule *pModule;  /
3fc22 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 66 6f 72  * The module for
3fc23 20 74 68 69 73 20 74 6f 6b 65 6e 69 7a 65 72 20   this tokenizer 
3fc24 2a 2f 0a 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65  */.  /* Tokenize
3fc25 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  r implementation
3fc26 73 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79  s will typically
3fc27 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20   add additional 
3fc28 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 73 74  fields */.};..st
3fc29 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  ruct sqlite3_tok
3fc2a 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a  enizer_cursor {.
3fc2b 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
3fc2c 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b  zer *pTokenizer;
3fc2d 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69         /* Tokeni
3fc2e 7a 65 72 20 66 6f 72 20 74 68 69 73 20 63 75 72  zer for this cur
3fc2f 73 6f 72 2e 20 2a 2f 0a 20 20 2f 2a 20 54 6f 6b  sor. */.  /* Tok
3fc30 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74  enizer implement
3fc31 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69  ations will typi
3fc32 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69  cally add additi
3fc33 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d  onal fields */.}
3fc34 3b 0a 0a 69 6e 74 20 66 74 73 33 5f 67 6c 6f 62  ;..int fts3_glob
3fc35 61 6c 5f 74 65 72 6d 5f 63 6e 74 28 69 6e 74 20  al_term_cnt(int 
3fc36 69 54 65 72 6d 2c 20 69 6e 74 20 69 43 6f 6c 29  iTerm, int iCol)
3fc37 3b 0a 69 6e 74 20 66 74 73 33 5f 74 65 72 6d 5f  ;.int fts3_term_
3fc38 63 6e 74 28 69 6e 74 20 69 54 65 72 6d 2c 20 69  cnt(int iTerm, i
3fc39 6e 74 20 69 43 6f 6c 29 3b 0a 0a 0a 23 65 6e 64  nt iCol);...#end
3fc3a 69 66 20 2f 2a 20 5f 46 54 53 33 5f 54 4f 4b 45  if /* _FTS3_TOKE
3fc3b 4e 49 5a 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a  NIZER_H_ */../**
3fc3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
3fc3d 20 6f 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a   of fts3_tokeniz
3fc3e 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.h ***********
3fc3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
3fc41 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
3fc42 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
3fc43 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73   left off in fts
3fc44 33 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  3Int.h *********
3fc45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
3fc46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
3fc47 6c 75 64 65 20 66 74 73 33 5f 68 61 73 68 2e 68  lude fts3_hash.h
3fc48 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
3fc49 66 20 66 74 73 33 49 6e 74 2e 68 20 2a 2a 2a 2a  f fts3Int.h ****
3fc4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
3fc4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
3fc4c 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 68 61 73  in file fts3_has
3fc4d 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.h ************
3fc4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fc4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
3fc50 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
3fc51 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 22.**.** The a
3fc52 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
3fc53 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
3fc54 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
3fc55 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
3fc56 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
3fc57 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
3fc58 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
3fc59 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
3fc5a 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
3fc5b 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
3fc5c 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
3fc5d 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
3fc5e 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
3fc5f 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
3fc60 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
3fc61 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
3fc62 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
3fc63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fc64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fc65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fc66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fc67 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  ***.** This is t
3fc68 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 20 66  he header file f
3fc69 6f 72 20 74 68 65 20 67 65 6e 65 72 69 63 20 68  or the generic h
3fc6a 61 73 68 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d  ash-table implem
3fc6b 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20  enation.** used 
3fc6c 69 6e 20 53 51 4c 69 74 65 2e 20 20 57 65 27 76  in SQLite.  We'v
3fc6d 65 20 6d 6f 64 69 66 69 65 64 20 69 74 20 73 6c  e modified it sl
3fc6e 69 67 68 74 6c 79 20 74 6f 20 73 65 72 76 65 20  ightly to serve 
3fc6f 61 73 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 0a  as a standalone.
3fc70 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 69 6d  ** hash table im
3fc71 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  plementation for
3fc72 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69   the full-text i
3fc73 6e 64 65 78 69 6e 67 20 6d 6f 64 75 6c 65 2e 0a  ndexing module..
3fc74 2a 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 46  **.*/.#ifndef _F
3fc75 54 53 33 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66  TS3_HASH_H_.#def
3fc76 69 6e 65 20 5f 46 54 53 33 5f 48 41 53 48 5f 48  ine _FTS3_HASH_H
3fc77 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  _../* Forward de
3fc78 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74  clarations of st
3fc79 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74 79 70  ructures. */.typ
3fc7a 65 64 65 66 20 73 74 72 75 63 74 20 46 74 73 33  edef struct Fts3
3fc7b 48 61 73 68 20 46 74 73 33 48 61 73 68 3b 0a 74  Hash Fts3Hash;.t
3fc7c 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 74  ypedef struct Ft
3fc7d 73 33 48 61 73 68 45 6c 65 6d 20 46 74 73 33 48  s3HashElem Fts3H
3fc7e 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63  ashElem;../* A c
3fc7f 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 62  omplete hash tab
3fc80 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  le is an instanc
3fc81 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
3fc82 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
3fc83 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f   The internals o
3fc84 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
3fc85 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f   are intended to
3fc86 20 62 65 20 6f 70 61 71 75 65 20 2d 2d 20 63 6c   be opaque -- cl
3fc87 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 68 6f  ient.** code sho
3fc88 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70 74 20  uld not attempt 
3fc89 74 6f 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64  to access or mod
3fc8a 69 66 79 20 74 68 65 20 66 69 65 6c 64 73 20 6f  ify the fields o
3fc8b 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
3fc8c 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20 43  .** directly.  C
3fc8d 68 61 6e 67 65 20 74 68 69 73 20 73 74 72 75 63  hange this struc
3fc8e 74 75 72 65 20 6f 6e 6c 79 20 62 79 20 75 73 69  ture only by usi
3fc8f 6e 67 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20  ng the routines 
3fc90 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76 65  below..** Howeve
3fc91 72 2c 20 6d 61 6e 79 20 6f 66 20 74 68 65 20 22  r, many of the "
3fc92 70 72 6f 63 65 64 75 72 65 73 22 20 61 6e 64 20  procedures" and 
3fc93 22 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20  "functions" for 
3fc94 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a 2a 2a  modifying and.**
3fc95 20 61 63 63 65 73 73 69 6e 67 20 74 68 69 73 20   accessing this 
3fc96 73 74 72 75 63 74 75 72 65 20 61 72 65 20 72 65  structure are re
3fc97 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20  ally macros, so 
3fc98 77 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 20  we can't really 
3fc99 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73 74 72  make.** this str
3fc9a 75 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0a 2a  ucture opaque..*
3fc9b 2f 0a 73 74 72 75 63 74 20 46 74 73 33 48 61 73  /.struct Fts3Has
3fc9c 68 20 7b 0a 20 20 63 68 61 72 20 6b 65 79 43 6c  h {.  char keyCl
3fc9d 61 73 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ass;          /*
3fc9e 20 48 41 53 48 5f 49 4e 54 2c 20 5f 50 4f 49 4e   HASH_INT, _POIN
3fc9f 54 45 52 2c 20 5f 53 54 52 49 4e 47 2c 20 5f 42  TER, _STRING, _B
3fca0 49 4e 41 52 59 20 2a 2f 0a 20 20 63 68 61 72 20  INARY */.  char 
3fca1 63 6f 70 79 4b 65 79 3b 20 20 20 20 20 20 20 20  copyKey;        
3fca2 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 6f     /* True if co
3fca3 70 79 20 6f 66 20 6b 65 79 20 6d 61 64 65 20 6f  py of key made o
3fca4 6e 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 69 6e  n insert */.  in
3fca5 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20  t count;        
3fca6 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
3fca7 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  of entries in th
3fca8 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 46 74  is table */.  Ft
3fca9 73 33 48 61 73 68 45 6c 65 6d 20 2a 66 69 72 73  s3HashElem *firs
3fcaa 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 66 69 72  t;    /* The fir
3fcab 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  st element of th
3fcac 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74  e array */.  int
3fcad 20 68 74 73 69 7a 65 3b 20 20 20 20 20 20 20 20   htsize;        
3fcae 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
3fcaf 66 20 62 75 63 6b 65 74 73 20 69 6e 20 74 68 65  f buckets in the
3fcb0 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
3fcb1 20 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20   struct _fts3ht 
3fcb2 7b 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20  {        /* the 
3fcb3 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
3fcb4 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20    int count;    
3fcb5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
3fcb6 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
3fcb7 77 69 74 68 20 74 68 69 73 20 68 61 73 68 20 2a  with this hash *
3fcb8 2f 0a 20 20 20 20 46 74 73 33 48 61 73 68 45 6c  /.    Fts3HashEl
3fcb9 65 6d 20 2a 63 68 61 69 6e 3b 20 20 20 20 20 2f  em *chain;     /
3fcba 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 72  * Pointer to fir
3fcbb 73 74 20 65 6e 74 72 79 20 77 69 74 68 20 74 68  st entry with th
3fcbc 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 7d 20 2a  is hash */.  } *
3fcbd 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20  ht;.};../* Each 
3fcbe 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 68  element in the h
3fcbf 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20  ash table is an 
3fcc0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
3fcc1 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74  following .** st
3fcc2 72 75 63 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c  ructure.  All el
3fcc3 65 6d 65 6e 74 73 20 61 72 65 20 73 74 6f 72 65  ements are store
3fcc4 64 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f  d on a single do
3fcc5 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74  ubly-linked list
3fcc6 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20 74  ..**.** Again, t
3fcc7 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
3fcc8 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
3fcc9 6f 70 61 71 75 65 2c 20 62 75 74 20 69 74 20 63  opaque, but it c
3fcca 61 6e 27 74 20 72 65 61 6c 6c 79 0a 2a 2a 20 62  an't really.** b
3fccb 65 20 6f 70 61 71 75 65 20 62 65 63 61 75 73 65  e opaque because
3fccc 20 69 74 20 69 73 20 75 73 65 64 20 62 79 20 6d   it is used by m
3fccd 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  acros..*/.struct
3fcce 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20 7b 0a   Fts3HashElem {.
3fccf 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a    Fts3HashElem *
3fcd0 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20 2f 2a 20  next, *prev; /* 
3fcd1 4e 65 78 74 20 61 6e 64 20 70 72 65 76 69 6f 75  Next and previou
3fcd2 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68  s elements in th
3fcd3 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69  e table */.  voi
3fcd4 64 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20  d *data;        
3fcd5 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20          /* Data 
3fcd6 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
3fcd7 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a  this element */.
3fcd8 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20 69 6e    void *pKey; in
3fcd9 74 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20  t nKey;      /* 
3fcda 4b 65 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  Key associated w
3fcdb 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74  ith this element
3fcdc 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
3fcdd 65 72 65 20 61 72 65 20 32 20 64 69 66 66 65 72  ere are 2 differ
3fcde 65 6e 74 20 6d 6f 64 65 73 20 6f 66 20 6f 70 65  ent modes of ope
3fcdf 72 61 74 69 6f 6e 20 66 6f 72 20 61 20 68 61 73  ration for a has
3fce0 68 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20  h table:.**.**  
3fce1 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e   FTS3_HASH_STRIN
3fce2 47 20 20 20 20 20 20 20 20 70 4b 65 79 20 70 6f  G        pKey po
3fce3 69 6e 74 73 20 74 6f 20 61 20 73 74 72 69 6e 67  ints to a string
3fce4 20 74 68 61 74 20 69 73 20 6e 4b 65 79 20 62 79   that is nKey by
3fce5 74 65 73 20 6c 6f 6e 67 0a 2a 2a 20 20 20 20 20  tes long.**     
3fce6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fce7 20 20 20 20 20 20 28 69 6e 63 6c 75 64 69 6e 67        (including
3fce8 20 74 68 65 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e   the null-termin
3fce9 61 74 6f 72 2c 20 69 66 20 61 6e 79 29 2e 20 20  ator, if any).  
3fcea 43 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  Case.**         
3fceb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fcec 20 20 69 73 20 72 65 73 70 65 63 74 65 64 20 69    is respected i
3fced 6e 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a  n comparisons..*
3fcee 2a 0a 2a 2a 20 20 20 46 54 53 33 5f 48 41 53 48  *.**   FTS3_HASH
3fcef 5f 42 49 4e 41 52 59 20 20 20 20 20 20 20 20 70  _BINARY        p
3fcf0 4b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20 62 69  Key points to bi
3fcf1 6e 61 72 79 20 64 61 74 61 20 6e 4b 65 79 20 62  nary data nKey b
3fcf2 79 74 65 73 20 6c 6f 6e 67 2e 20 0a 2a 2a 20 20  ytes long. .**  
3fcf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fcf4 20 20 20 20 20 20 20 20 20 6d 65 6d 63 6d 70 28           memcmp(
3fcf5 29 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d  ) is used to com
3fcf6 70 61 72 65 20 6b 65 79 73 2e 0a 2a 2a 0a 2a 2a  pare keys..**.**
3fcf7 20 41 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b   A copy of the k
3fcf8 65 79 20 69 73 20 6d 61 64 65 20 69 66 20 74 68  ey is made if th
3fcf9 65 20 63 6f 70 79 4b 65 79 20 70 61 72 61 6d 65  e copyKey parame
3fcfa 74 65 72 20 74 6f 20 66 74 73 33 48 61 73 68 49  ter to fts3HashI
3fcfb 6e 69 74 20 69 73 20 31 2e 20 20 0a 2a 2f 0a 23  nit is 1.  .*/.#
3fcfc 64 65 66 69 6e 65 20 46 54 53 33 5f 48 41 53 48  define FTS3_HASH
3fcfd 5f 53 54 52 49 4e 47 20 20 20 20 31 0a 23 64 65  _STRING    1.#de
3fcfe 66 69 6e 65 20 46 54 53 33 5f 48 41 53 48 5f 42  fine FTS3_HASH_B
3fcff 49 4e 41 52 59 20 20 20 20 32 0a 0a 2f 2a 0a 2a  INARY    2../*.*
3fd00 2a 20 41 63 63 65 73 73 20 72 6f 75 74 69 6e 65  * Access routine
3fd01 73 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c 20 69  s.  To delete, i
3fd02 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69  nsert a NULL poi
3fd03 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nter..*/.SQLITE_
3fd04 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
3fd05 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74  ite3Fts3HashInit
3fd06 28 46 74 73 33 48 61 73 68 20 2a 70 4e 65 77 2c  (Fts3Hash *pNew,
3fd07 20 63 68 61 72 20 6b 65 79 43 6c 61 73 73 2c 20   char keyClass, 
3fd08 63 68 61 72 20 63 6f 70 79 4b 65 79 29 3b 0a 53  char copyKey);.S
3fd09 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
3fd0a 69 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48  id *sqlite3Fts3H
3fd0b 61 73 68 49 6e 73 65 72 74 28 46 74 73 33 48 61  ashInsert(Fts3Ha
3fd0c 73 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  sh*, const void 
3fd0d 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c  *pKey, int nKey,
3fd0e 20 76 6f 69 64 20 2a 70 44 61 74 61 29 3b 0a 53   void *pData);.S
3fd0f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
3fd10 69 64 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48  id *sqlite3Fts3H
3fd11 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 46 74  ashFind(const Ft
3fd12 73 33 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 76  s3Hash*, const v
3fd13 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  oid *pKey, int n
3fd14 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Key);.SQLITE_PRI
3fd15 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
3fd16 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 28 46  3Fts3HashClear(F
3fd17 74 73 33 48 61 73 68 2a 29 3b 0a 53 51 4c 49 54  ts3Hash*);.SQLIT
3fd18 45 5f 50 52 49 56 41 54 45 20 46 74 73 33 48 61  E_PRIVATE Fts3Ha
3fd19 73 68 45 6c 65 6d 20 2a 73 71 6c 69 74 65 33 46  shElem *sqlite3F
3fd1a 74 73 33 48 61 73 68 46 69 6e 64 45 6c 65 6d 28  ts3HashFindElem(
3fd1b 63 6f 6e 73 74 20 46 74 73 33 48 61 73 68 20 2a  const Fts3Hash *
3fd1c 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 20  , const void *, 
3fd1d 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 68 6f  int);../*.** Sho
3fd1e 72 74 68 61 6e 64 20 66 6f 72 20 74 68 65 20 66  rthand for the f
3fd1f 75 6e 63 74 69 6f 6e 73 20 61 62 6f 76 65 0a 2a  unctions above.*
3fd20 2f 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  /.#define fts3Ha
3fd21 73 68 49 6e 69 74 20 20 20 20 20 73 71 6c 69 74  shInit     sqlit
3fd22 65 33 46 74 73 33 48 61 73 68 49 6e 69 74 0a 23  e3Fts3HashInit.#
3fd23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 49  define fts3HashI
3fd24 6e 73 65 72 74 20 20 20 73 71 6c 69 74 65 33 46  nsert   sqlite3F
3fd25 74 73 33 48 61 73 68 49 6e 73 65 72 74 0a 23 64  ts3HashInsert.#d
3fd26 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 46 69  efine fts3HashFi
3fd27 6e 64 20 20 20 20 20 73 71 6c 69 74 65 33 46 74  nd     sqlite3Ft
3fd28 73 33 48 61 73 68 46 69 6e 64 0a 23 64 65 66 69  s3HashFind.#defi
3fd29 6e 65 20 66 74 73 33 48 61 73 68 43 6c 65 61 72  ne fts3HashClear
3fd2a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 48      sqlite3Fts3H
3fd2b 61 73 68 43 6c 65 61 72 0a 23 64 65 66 69 6e 65  ashClear.#define
3fd2c 20 66 74 73 33 48 61 73 68 46 69 6e 64 45 6c 65   fts3HashFindEle
3fd2d 6d 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73  m sqlite3Fts3Has
3fd2e 68 46 69 6e 64 45 6c 65 6d 0a 0a 2f 2a 0a 2a 2a  hFindElem../*.**
3fd2f 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f 6f 70   Macros for loop
3fd30 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65  ing over all ele
3fd31 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73 68 20  ments of a hash 
3fd32 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64 69 6f  table.  The idio
3fd33 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 68 69  m is.** like thi
3fd34 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 46 74 73 33 48  s:.**.**   Fts3H
3fd35 61 73 68 20 68 3b 0a 2a 2a 20 20 20 46 74 73 33  ash h;.**   Fts3
3fd36 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a 2a 20  HashElem *p;.** 
3fd37 20 20 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72 28 70    ....**   for(p
3fd38 3d 66 74 73 33 48 61 73 68 46 69 72 73 74 28 26  =fts3HashFirst(&
3fd39 68 29 3b 20 70 3b 20 70 3d 66 74 73 33 48 61 73  h); p; p=fts3Has
3fd3a 68 4e 65 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20  hNext(p)){.**   
3fd3b 20 20 53 6f 6d 65 53 74 72 75 63 74 75 72 65 20    SomeStructure 
3fd3c 2a 70 44 61 74 61 20 3d 20 66 74 73 33 48 61 73  *pData = fts3Has
3fd3d 68 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20  hData(p);.**    
3fd3e 20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67   // do something
3fd3f 20 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20 20   with pData.**  
3fd40 20 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 74   }.*/.#define ft
3fd41 73 33 48 61 73 68 46 69 72 73 74 28 48 29 20 20  s3HashFirst(H)  
3fd42 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 64 65  ((H)->first).#de
3fd43 66 69 6e 65 20 66 74 73 33 48 61 73 68 4e 65 78  fine fts3HashNex
3fd44 74 28 45 29 20 20 20 28 28 45 29 2d 3e 6e 65 78  t(E)   ((E)->nex
3fd45 74 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48  t).#define fts3H
3fd46 61 73 68 44 61 74 61 28 45 29 20 20 20 28 28 45  ashData(E)   ((E
3fd47 29 2d 3e 64 61 74 61 29 0a 23 64 65 66 69 6e 65  )->data).#define
3fd48 20 66 74 73 33 48 61 73 68 4b 65 79 28 45 29 20   fts3HashKey(E) 
3fd49 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 29 0a 23     ((E)->pKey).#
3fd4a 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 4b  define fts3HashK
3fd4b 65 79 73 69 7a 65 28 45 29 20 28 28 45 29 2d 3e  eysize(E) ((E)->
3fd4c 6e 4b 65 79 29 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d  nKey)../*.** Num
3fd4d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
3fd4e 6e 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a 2a  n a hash table.*
3fd4f 2f 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  /.#define fts3Ha
3fd50 73 68 43 6f 75 6e 74 28 48 29 20 20 28 28 48 29  shCount(H)  ((H)
3fd51 2d 3e 63 6f 75 6e 74 29 0a 0a 23 65 6e 64 69 66  ->count)..#endif
3fd52 20 2f 2a 20 5f 46 54 53 33 5f 48 41 53 48 5f 48   /* _FTS3_HASH_H
3fd53 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  _ */../*********
3fd54 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
3fd55 33 5f 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a  3_hash.h *******
3fd56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fd57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fd58 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
3fd59 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
3fd5a 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
3fd5b 66 66 20 69 6e 20 66 74 73 33 49 6e 74 2e 68 20  ff in fts3Int.h 
3fd5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3fd5d 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ****/../*.** Thi
3fd5e 73 20 63 6f 6e 73 74 61 6e 74 20 63 6f 6e 74 72  s constant contr
3fd5f 6f 6c 73 20 68 6f 77 20 6f 66 74 65 6e 20 73 65  ols how often se
3fd60 67 6d 65 6e 74 73 20 61 72 65 20 6d 65 72 67 65  gments are merge
3fd61 64 2e 20 4f 6e 63 65 20 74 68 65 72 65 20 61 72  d. Once there ar
3fd62 65 0a 2a 2a 20 46 54 53 33 5f 4d 45 52 47 45 5f  e.** FTS3_MERGE_
3fd63 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 20 6f  COUNT segments o
3fd64 66 20 6c 65 76 65 6c 20 4e 2c 20 74 68 65 79 20  f level N, they 
3fd65 61 72 65 20 6d 65 72 67 65 64 20 69 6e 74 6f 20  are merged into 
3fd66 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 73 65 67 6d  a single.** segm
3fd67 65 6e 74 20 6f 66 20 6c 65 76 65 6c 20 4e 2b 31  ent of level N+1
3fd68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54 53  ..*/.#define FTS
3fd69 33 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 20 31 36  3_MERGE_COUNT 16
3fd6a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ../*.** This is 
3fd6b 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75  the maximum amou
3fd6c 6e 74 20 6f 66 20 64 61 74 61 20 28 69 6e 20 62  nt of data (in b
3fd6d 79 74 65 73 29 20 74 6f 20 73 74 6f 72 65 20 69  ytes) to store i
3fd6e 6e 20 74 68 65 20 0a 2a 2a 20 46 74 73 33 54 61  n the .** Fts3Ta
3fd6f 62 6c 65 2e 70 65 6e 64 69 6e 67 54 65 72 6d 73  ble.pendingTerms
3fd70 20 68 61 73 68 20 74 61 62 6c 65 2e 20 4e 6f 72   hash table. Nor
3fd71 6d 61 6c 6c 79 2c 20 74 68 65 20 68 61 73 68 20  mally, the hash 
3fd72 74 61 62 6c 65 20 69 73 0a 2a 2a 20 70 6f 70 75  table is.** popu
3fd73 6c 61 74 65 64 20 61 73 20 64 6f 63 75 6d 65 6e  lated as documen
3fd74 74 73 20 61 72 65 20 69 6e 73 65 72 74 65 64 2f  ts are inserted/
3fd75 75 70 64 61 74 65 64 2f 64 65 6c 65 74 65 64 20  updated/deleted 
3fd76 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
3fd77 0a 2a 2a 20 61 6e 64 20 75 73 65 64 20 74 6f 20  .** and used to 
3fd78 63 72 65 61 74 65 20 61 20 6e 65 77 20 73 65 67  create a new seg
3fd79 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 74 72  ment when the tr
3fd7a 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d  ansaction is com
3fd7b 6d 69 74 74 65 64 2e 0a 2a 2a 20 48 6f 77 65 76  mitted..** Howev
3fd7c 65 72 20 69 66 20 74 68 69 73 20 6c 69 6d 69 74  er if this limit
3fd7d 20 69 73 20 72 65 61 63 68 65 64 20 6d 69 64 77   is reached midw
3fd7e 61 79 20 74 68 72 6f 75 67 68 20 61 20 74 72 61  ay through a tra
3fd7f 6e 73 61 63 74 69 6f 6e 2c 20 61 20 6e 65 77 20  nsaction, a new 
3fd80 0a 2a 2a 20 73 65 67 6d 65 6e 74 20 69 73 20 63  .** segment is c
3fd81 72 65 61 74 65 64 20 61 6e 64 20 74 68 65 20 68  reated and the h
3fd82 61 73 68 20 74 61 62 6c 65 20 63 6c 65 61 72 65  ash table cleare
3fd83 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a  d immediately..*
3fd84 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 4d  /.#define FTS3_M
3fd85 41 58 5f 50 45 4e 44 49 4e 47 5f 44 41 54 41 20  AX_PENDING_DATA 
3fd86 28 31 2a 31 30 32 34 2a 31 30 32 34 29 0a 0a 2f  (1*1024*1024)../
3fd87 2a 0a 2a 2a 20 4d 61 63 72 6f 20 74 6f 20 72 65  *.** Macro to re
3fd88 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
3fd89 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61  of elements in a
3fd8a 6e 20 61 72 72 61 79 2e 20 53 51 4c 69 74 65 20  n array. SQLite 
3fd8b 68 61 73 20 61 0a 2a 2a 20 73 69 6d 69 6c 61 72  has a.** similar
3fd8c 20 6d 61 63 72 6f 20 63 61 6c 6c 65 64 20 41 72   macro called Ar
3fd8d 72 61 79 53 69 7a 65 28 29 2e 20 55 73 65 20 61  raySize(). Use a
3fd8e 20 64 69 66 66 65 72 65 6e 74 20 6e 61 6d 65 20   different name 
3fd8f 74 6f 20 61 76 6f 69 64 0a 2a 2a 20 61 20 63 6f  to avoid.** a co
3fd90 6c 6c 69 73 69 6f 6e 20 77 68 65 6e 20 62 75 69  llision when bui
3fd91 6c 64 69 6e 67 20 61 6e 20 61 6d 61 6c 67 61 6d  lding an amalgam
3fd92 61 74 69 6f 6e 20 77 69 74 68 20 62 75 69 6c 74  ation with built
3fd93 2d 69 6e 20 46 54 53 33 2e 0a 2a 2f 0a 23 64 65  -in FTS3..*/.#de
3fd94 66 69 6e 65 20 53 69 7a 65 6f 66 41 72 72 61 79  fine SizeofArray
3fd95 28 58 29 20 28 28 69 6e 74 29 28 73 69 7a 65 6f  (X) ((int)(sizeo
3fd96 66 28 58 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d  f(X)/sizeof(X[0]
3fd97 29 29 29 0a 0a 0a 23 69 66 6e 64 65 66 20 4d 49  )))...#ifndef MI
3fd98 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78  N.# define MIN(x
3fd99 2c 79 29 20 28 28 78 29 3c 28 79 29 3f 28 78 29  ,y) ((x)<(y)?(x)
3fd9a 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a 23 69 66  :(y)).#endif.#if
3fd9b 6e 64 65 66 20 4d 41 58 0a 23 20 64 65 66 69 6e  ndef MAX.# defin
3fd9c 65 20 4d 41 58 28 78 2c 79 29 20 28 28 78 29 3e  e MAX(x,y) ((x)>
3fd9d 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 23 65 6e  (y)?(x):(y)).#en
3fd9e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d  dif../*.** Maxim
3fd9f 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 76  um length of a v
3fda0 61 72 69 6e 74 20 65 6e 63 6f 64 65 64 20 69 6e  arint encoded in
3fda1 74 65 67 65 72 2e 20 54 68 65 20 76 61 72 69 6e  teger. The varin
3fda2 74 20 66 6f 72 6d 61 74 20 69 73 20 64 69 66 66  t format is diff
3fda3 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68  erent.** from th
3fda4 61 74 20 75 73 65 64 20 62 79 20 53 51 4c 69 74  at used by SQLit
3fda5 65 2c 20 73 6f 20 74 68 65 20 6d 61 78 69 6d 75  e, so the maximu
3fda6 6d 20 6c 65 6e 67 74 68 20 69 73 20 31 30 2c 20  m length is 10, 
3fda7 6e 6f 74 20 39 2e 0a 2a 2f 0a 23 64 65 66 69 6e  not 9..*/.#defin
3fda8 65 20 46 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41  e FTS3_VARINT_MA
3fda9 58 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 46 54 53 34  X 10../*.** FTS4
3fdaa 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
3fdab 6d 61 79 20 6d 61 69 6e 74 61 69 6e 20 6d 75 6c  may maintain mul
3fdac 74 69 70 6c 65 20 69 6e 64 65 78 65 73 20 2d 20  tiple indexes - 
3fdad 6f 6e 65 20 69 6e 64 65 78 20 6f 66 20 61 6c 6c  one index of all
3fdae 20 74 65 72 6d 73 0a 2a 2a 20 69 6e 20 74 68 65   terms.** in the
3fdaf 20 64 6f 63 75 6d 65 6e 74 20 73 65 74 20 61 6e   document set an
3fdb0 64 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 70  d zero or more p
3fdb1 72 65 66 69 78 20 69 6e 64 65 78 65 73 2e 20 41  refix indexes. A
3fdb2 6c 6c 20 69 6e 64 65 78 65 73 20 61 72 65 20 73  ll indexes are s
3fdb3 74 6f 72 65 64 0a 2a 2a 20 61 73 20 6f 6e 65 20  tored.** as one 
3fdb4 6f 72 20 6d 6f 72 65 20 62 2b 2d 74 72 65 65 73  or more b+-trees
3fdb5 20 69 6e 20 74 68 65 20 25 5f 73 65 67 6d 65 6e   in the %_segmen
3fdb6 74 73 20 61 6e 64 20 25 5f 73 65 67 64 69 72 20  ts and %_segdir 
3fdb7 74 61 62 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 49  tables. .**.** I
3fdb8 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f  t is possible to
3fdb9 20 64 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68   determine which
3fdba 20 69 6e 64 65 78 20 61 20 62 2b 2d 74 72 65 65   index a b+-tree
3fdbb 20 62 65 6c 6f 6e 67 73 20 74 6f 20 62 61 73 65   belongs to base
3fdbc 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75  d on the.** valu
3fdbd 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
3fdbe 22 25 5f 73 65 67 64 69 72 2e 6c 65 76 65 6c 22  "%_segdir.level"
3fdbf 20 63 6f 6c 75 6d 6e 2e 20 47 69 76 65 6e 20 74   column. Given t
3fdc0 68 69 73 20 76 61 6c 75 65 20 4c 2c 20 74 68 65  his value L, the
3fdc1 20 69 6e 64 65 78 0a 2a 2a 20 74 68 61 74 20 74   index.** that t
3fdc2 68 65 20 62 2b 2d 74 72 65 65 20 62 65 6c 6f 6e  he b+-tree belon
3fdc3 67 73 20 74 6f 20 69 73 20 28 4c 3c 3c 31 30 29  gs to is (L<<10)
3fdc4 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  . In other words
3fdc5 2c 20 61 6c 6c 20 62 2b 2d 74 72 65 65 73 20 77  , all b+-trees w
3fdc6 69 74 68 0a 2a 2a 20 6c 65 76 65 6c 20 76 61 6c  ith.** level val
3fdc7 75 65 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e  ues between 0 an
3fdc8 64 20 31 30 32 33 20 28 69 6e 63 6c 75 73 69 76  d 1023 (inclusiv
3fdc9 65 29 20 62 65 6c 6f 6e 67 20 74 6f 20 69 6e 64  e) belong to ind
3fdca 65 78 20 30 2c 20 61 6c 6c 20 6c 65 76 65 6c 73  ex 0, all levels
3fdcb 0a 2a 2a 20 62 65 74 77 65 65 6e 20 31 30 32 34  .** between 1024
3fdcc 20 61 6e 64 20 32 30 34 37 20 74 6f 20 69 6e 64   and 2047 to ind
3fdcd 65 78 20 31 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e  ex 1, and so on.
3fdce 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 63 6f 6e  .**.** It is con
3fdcf 73 69 64 65 72 65 64 20 69 6d 70 6f 73 73 69 62  sidered impossib
3fdd0 6c 65 20 66 6f 72 20 61 6e 20 69 6e 64 65 78 20  le for an index 
3fdd1 74 6f 20 75 73 65 20 6d 6f 72 65 20 74 68 61 6e  to use more than
3fdd2 20 31 30 32 34 20 6c 65 76 65 6c 73 2e 20 49 6e   1024 levels. In
3fdd3 20 0a 2a 2a 20 74 68 65 6f 72 79 20 74 68 6f 75   .** theory thou
3fdd4 67 68 20 74 68 69 73 20 6d 61 79 20 68 61 70 70  gh this may happ
3fdd5 65 6e 2c 20 62 75 74 20 6f 6e 6c 79 20 61 66 74  en, but only aft
3fdd6 65 72 20 61 74 20 6c 65 61 73 74 20 0a 2a 2a 20  er at least .** 
3fdd7 28 46 54 53 33 5f 4d 45 52 47 45 5f 43 4f 55 4e  (FTS3_MERGE_COUN
3fdd8 54 5e 31 30 32 34 29 20 73 65 70 61 72 61 74 65  T^1024) separate
3fdd9 20 66 6c 75 73 68 65 73 20 6f 66 20 74 68 65 20   flushes of the 
3fdda 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 74 61  pending-terms ta
3fddb 62 6c 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  bles..*/.#define
3fddc 20 46 54 53 33 5f 53 45 47 44 49 52 5f 4d 41 58   FTS3_SEGDIR_MAX
3fddd 4c 45 56 45 4c 20 20 20 20 20 20 31 30 32 34 0a  LEVEL      1024.
3fdde 23 64 65 66 69 6e 65 20 46 54 53 33 5f 53 45 47  #define FTS3_SEG
3fddf 44 49 52 5f 4d 41 58 4c 45 56 45 4c 5f 53 54 52  DIR_MAXLEVEL_STR
3fde0 20 22 31 30 32 34 22 0a 0a 2f 2a 0a 2a 2a 20 54   "1024"../*.** T
3fde1 68 65 20 74 65 73 74 63 61 73 65 28 29 20 6d 61  he testcase() ma
3fde2 63 72 6f 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  cro is only used
3fde3 20 62 79 20 74 68 65 20 61 6d 61 6c 67 61 6d 61   by the amalgama
3fde4 74 69 6f 6e 2e 20 20 49 66 20 75 6e 64 65 66 69  tion.  If undefi
3fde5 6e 65 64 2c 0a 2a 2a 20 6d 61 6b 65 20 69 74 20  ned,.** make it 
3fde6 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 23 69 66 6e  a no-op..*/.#ifn
3fde7 64 65 66 20 74 65 73 74 63 61 73 65 0a 23 20 64  def testcase.# d
3fde8 65 66 69 6e 65 20 74 65 73 74 63 61 73 65 28 58  efine testcase(X
3fde9 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
3fdea 54 65 72 6d 69 6e 61 74 6f 72 20 76 61 6c 75 65  Terminator value
3fdeb 73 20 66 6f 72 20 70 6f 73 69 74 69 6f 6e 2d 6c  s for position-l
3fdec 69 73 74 73 20 61 6e 64 20 63 6f 6c 75 6d 6e 2d  ists and column-
3fded 6c 69 73 74 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  lists..*/.#defin
3fdee 65 20 50 4f 53 5f 43 4f 4c 55 4d 4e 20 20 28 31  e POS_COLUMN  (1
3fdef 29 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 2d  )     /* Column-
3fdf0 6c 69 73 74 20 74 65 72 6d 69 6e 61 74 6f 72 20  list terminator 
3fdf1 2a 2f 0a 23 64 65 66 69 6e 65 20 50 4f 53 5f 45  */.#define POS_E
3fdf2 4e 44 20 20 20 20 20 28 30 29 20 20 20 20 20 2f  ND     (0)     /
3fdf3 2a 20 50 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 20  * Position-list 
3fdf4 74 65 72 6d 69 6e 61 74 6f 72 20 2a 2f 20 0a 0a  terminator */ ..
3fdf5 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69  /*.** This secti
3fdf6 6f 6e 20 70 72 6f 76 69 64 65 73 20 64 65 66 69  on provides defi
3fdf7 6e 69 74 69 6f 6e 73 20 74 6f 20 61 6c 6c 6f 77  nitions to allow
3fdf8 20 74 68 65 0a 2a 2a 20 46 54 53 33 20 65 78 74   the.** FTS3 ext
3fdf9 65 6e 73 69 6f 6e 20 74 6f 20 62 65 20 63 6f 6d  ension to be com
3fdfa 70 69 6c 65 64 20 6f 75 74 73 69 64 65 20 6f 66  piled outside of
3fdfb 20 74 68 65 20 0a 2a 2a 20 61 6d 61 6c 67 61 6d   the .** amalgam
3fdfc 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ation..*/.#ifnde
3fdfd 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d  f SQLITE_AMALGAM
3fdfe 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 4d 61 63 72  ATION./*.** Macr
3fdff 6f 73 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  os indicating th
3fe00 61 74 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65  at conditional e
3fe01 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 61  xpressions are a
3fe02 6c 77 61 79 73 20 74 72 75 65 20 6f 72 0a 2a 2a  lways true or.**
3fe03 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23 69 66 64 65   false..*/.#ifde
3fe04 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47  f SQLITE_COVERAG
3fe05 45 5f 54 45 53 54 0a 23 20 64 65 66 69 6e 65 20  E_TEST.# define 
3fe06 41 4c 57 41 59 53 28 78 29 20 28 31 29 0a 23 20  ALWAYS(x) (1).# 
3fe07 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20  define NEVER(X) 
3fe08 20 28 30 29 0a 23 65 6c 73 65 0a 23 20 64 65 66   (0).#else.# def
3fe09 69 6e 65 20 41 4c 57 41 59 53 28 78 29 20 28 78  ine ALWAYS(x) (x
3fe0a 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 52  ).# define NEVER
3fe0b 28 78 29 20 20 28 78 29 0a 23 65 6e 64 69 66 0a  (x)  (x).#endif.
3fe0c 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20  ./*.** Internal 
3fe0d 74 79 70 65 73 20 75 73 65 64 20 62 79 20 53 51  types used by SQ
3fe0e 4c 69 74 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  Lite..*/.typedef
3fe0f 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75   unsigned char u
3fe10 38 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d  8;         /* 1-
3fe11 62 79 74 65 20 28 6f 72 20 6c 61 72 67 65 72 29  byte (or larger)
3fe12 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
3fe13 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 68 6f  r */.typedef sho
3fe14 72 74 20 69 6e 74 20 69 31 36 3b 20 20 20 20 20  rt int i16;     
3fe15 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79 74 65         /* 2-byte
3fe16 20 28 6f 72 20 6c 61 72 67 65 72 29 20 73 69 67   (or larger) sig
3fe17 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74  ned integer */.t
3fe18 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20  ypedef unsigned 
3fe19 69 6e 74 20 75 33 32 3b 20 20 20 20 20 20 20 20  int u32;        
3fe1a 20 2f 2a 20 34 2d 62 79 74 65 20 75 6e 73 69 67   /* 4-byte unsig
3fe1b 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74  ned integer */.t
3fe1c 79 70 65 64 65 66 20 73 71 6c 69 74 65 33 5f 75  ypedef sqlite3_u
3fe1d 69 6e 74 36 34 20 75 36 34 3b 20 20 20 20 20 20  int64 u64;      
3fe1e 20 2f 2a 20 38 2d 62 79 74 65 20 75 6e 73 69 67   /* 8-byte unsig
3fe1f 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74  ned integer */.t
3fe20 79 70 65 64 65 66 20 73 71 6c 69 74 65 33 5f 69  ypedef sqlite3_i
3fe21 6e 74 36 34 20 69 36 34 3b 20 20 20 20 20 20 20  nt64 i64;       
3fe22 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e 65   /* 8-byte signe
3fe23 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a  d integer */../*
3fe24 0a 2a 2a 20 4d 61 63 72 6f 20 75 73 65 64 20 74  .** Macro used t
3fe25 6f 20 73 75 70 70 72 65 73 73 20 63 6f 6d 70 69  o suppress compi
3fe26 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 66 6f 72  ler warnings for
3fe27 20 75 6e 75 73 65 64 20 70 61 72 61 6d 65 74 65   unused paramete
3fe28 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 55  rs..*/.#define U
3fe29 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
3fe2a 78 29 20 28 76 6f 69 64 29 28 78 29 0a 0a 2f 2a  x) (void)(x)../*
3fe2b 0a 2a 2a 20 41 63 74 69 76 61 74 65 20 61 73 73  .** Activate ass
3fe2c 65 72 74 28 29 20 6f 6e 6c 79 20 69 66 20 53 51  ert() only if SQ
3fe2d 4c 49 54 45 5f 54 45 53 54 20 69 73 20 65 6e 61  LITE_TEST is ena
3fe2e 62 6c 65 64 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  bled..*/.#if !de
3fe2f 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26  fined(NDEBUG) &&
3fe30 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
3fe31 5f 44 45 42 55 47 29 20 0a 23 20 64 65 66 69 6e  _DEBUG) .# defin
3fe32 65 20 4e 44 45 42 55 47 20 31 0a 23 65 6e 64 69  e NDEBUG 1.#endi
3fe33 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 45 53  f../*.** The TES
3fe34 54 4f 4e 4c 59 20 6d 61 63 72 6f 20 69 73 20 75  TONLY macro is u
3fe35 73 65 64 20 74 6f 20 65 6e 63 6c 6f 73 65 20 76  sed to enclose v
3fe36 61 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74  ariable declarat
3fe37 69 6f 6e 73 20 6f 72 0a 2a 2a 20 6f 74 68 65 72  ions or.** other
3fe38 20 62 69 74 73 20 6f 66 20 63 6f 64 65 20 74 68   bits of code th
3fe39 61 74 20 61 72 65 20 6e 65 65 64 65 64 20 74 6f  at are needed to
3fe3a 20 73 75 70 70 6f 72 74 20 74 68 65 20 61 72 67   support the arg
3fe3b 75 6d 65 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e  uments.** within
3fe3c 20 74 65 73 74 63 61 73 65 28 29 20 61 6e 64 20   testcase() and 
3fe3d 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 2e  assert() macros.
3fe3e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
3fe3f 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c  SQLITE_DEBUG) ||
3fe40 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
3fe41 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a 23  COVERAGE_TEST).#
3fe42 20 64 65 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59   define TESTONLY
3fe43 28 58 29 20 20 58 0a 23 65 6c 73 65 0a 23 20 64  (X)  X.#else.# d
3fe44 65 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58  efine TESTONLY(X
3fe45 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  ).#endif..#endif
3fe46 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47   /* SQLITE_AMALG
3fe47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 64  AMATION */..#ifd
3fe48 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
3fe49 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3fe4a 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 43 6f  nt sqlite3Fts3Co
3fe4b 72 72 75 70 74 28 76 6f 69 64 29 3b 0a 23 20 64  rrupt(void);.# d
3fe4c 65 66 69 6e 65 20 46 54 53 5f 43 4f 52 52 55 50  efine FTS_CORRUP
3fe4d 54 5f 56 54 41 42 20 73 71 6c 69 74 65 33 46 74  T_VTAB sqlite3Ft
3fe4e 73 33 43 6f 72 72 75 70 74 28 29 0a 23 65 6c 73  s3Corrupt().#els
3fe4f 65 0a 23 20 64 65 66 69 6e 65 20 46 54 53 5f 43  e.# define FTS_C
3fe50 4f 52 52 55 50 54 5f 56 54 41 42 20 53 51 4c 49  ORRUPT_VTAB SQLI
3fe51 54 45 5f 43 4f 52 52 55 50 54 5f 56 54 41 42 0a  TE_CORRUPT_VTAB.
3fe52 23 65 6e 64 69 66 0a 0a 74 79 70 65 64 65 66 20  #endif..typedef 
3fe53 73 74 72 75 63 74 20 46 74 73 33 54 61 62 6c 65  struct Fts3Table
3fe54 20 46 74 73 33 54 61 62 6c 65 3b 0a 74 79 70 65   Fts3Table;.type
3fe55 64 65 66 20 73 74 72 75 63 74 20 46 74 73 33 43  def struct Fts3C
3fe56 75 72 73 6f 72 20 46 74 73 33 43 75 72 73 6f 72  ursor Fts3Cursor
3fe57 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
3fe58 20 46 74 73 33 45 78 70 72 20 46 74 73 33 45 78   Fts3Expr Fts3Ex
3fe59 70 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  pr;.typedef stru
3fe5a 63 74 20 46 74 73 33 50 68 72 61 73 65 20 46 74  ct Fts3Phrase Ft
3fe5b 73 33 50 68 72 61 73 65 3b 0a 74 79 70 65 64 65  s3Phrase;.typede
3fe5c 66 20 73 74 72 75 63 74 20 46 74 73 33 50 68 72  f struct Fts3Phr
3fe5d 61 73 65 54 6f 6b 65 6e 20 46 74 73 33 50 68 72  aseToken Fts3Phr
3fe5e 61 73 65 54 6f 6b 65 6e 3b 0a 0a 74 79 70 65 64  aseToken;..typed
3fe5f 65 66 20 73 74 72 75 63 74 20 46 74 73 33 44 6f  ef struct Fts3Do
3fe60 63 6c 69 73 74 20 46 74 73 33 44 6f 63 6c 69 73  clist Fts3Doclis
3fe61 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
3fe62 74 20 46 74 73 33 53 65 67 46 69 6c 74 65 72 20  t Fts3SegFilter 
3fe63 46 74 73 33 53 65 67 46 69 6c 74 65 72 3b 0a 74  Fts3SegFilter;.t
3fe64 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 74  ypedef struct Ft
3fe65 73 33 44 65 66 65 72 72 65 64 54 6f 6b 65 6e 20  s3DeferredToken 
3fe66 46 74 73 33 44 65 66 65 72 72 65 64 54 6f 6b 65  Fts3DeferredToke
3fe67 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  n;.typedef struc
3fe68 74 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20  t Fts3SegReader 
3fe69 46 74 73 33 53 65 67 52 65 61 64 65 72 3b 0a 74  Fts3SegReader;.t
3fe6a 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 74  ypedef struct Ft
3fe6b 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72  s3MultiSegReader
3fe6c 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61   Fts3MultiSegRea
3fe6d 64 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f  der;../*.** A co
3fe6e 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 66 75  nnection to a fu
3fe6f 6c 6c 74 65 78 74 20 69 6e 64 65 78 20 69 73 20  lltext index is 
3fe70 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
3fe71 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
3fe72 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 20 78  structure. The x
3fe73 43 72 65 61 74 65 20 61 6e 64 20 78 43 6f 6e 6e  Create and xConn
3fe74 65 63 74 20 6d 65 74 68 6f 64 73 20 63 72 65 61  ect methods crea
3fe75 74 65 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a  te an instance.*
3fe76 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  * of this struct
3fe77 75 72 65 20 61 6e 64 20 78 44 65 73 74 72 6f 79  ure and xDestroy
3fe78 20 61 6e 64 20 78 44 69 73 63 6f 6e 6e 65 63 74   and xDisconnect
3fe79 20 66 72 65 65 20 74 68 61 74 20 69 6e 73 74 61   free that insta
3fe7a 6e 63 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65  nce..** All othe
3fe7b 72 20 6d 65 74 68 6f 64 73 20 72 65 63 65 69 76  r methods receiv
3fe7c 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  e a pointer to t
3fe7d 68 65 20 73 74 72 75 63 74 75 72 65 20 61 73 20  he structure as 
3fe7e 6f 6e 65 20 6f 66 20 74 68 65 69 72 0a 2a 2a 20  one of their.** 
3fe7f 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74  arguments..*/.st
3fe80 72 75 63 74 20 46 74 73 33 54 61 62 6c 65 20 7b  ruct Fts3Table {
3fe81 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
3fe82 62 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  base;           
3fe83 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73 73     /* Base class
3fe84 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20   used by SQLite 
3fe85 63 6f 72 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  core */.  sqlite
3fe86 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
3fe87 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3fe88 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
3fe89 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tion */.  const 
3fe8a 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20  char *zDb;      
3fe8b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6c 6f 67            /* log
3fe8c 69 63 61 6c 20 64 61 74 61 62 61 73 65 20 6e 61  ical database na
3fe8d 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  me */.  const ch
3fe8e 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
3fe8f 20 20 20 20 20 20 20 20 2f 2a 20 76 69 72 74 75          /* virtu
3fe90 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f  al table name */
3fe91 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20  .  int nColumn; 
3fe92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fe93 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20     /* number of 
3fe94 6e 61 6d 65 64 20 63 6f 6c 75 6d 6e 73 20 69 6e  named columns in
3fe95 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
3fe96 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c  /.  char **azCol
3fe97 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  umn;            
3fe98 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 61      /* column na
3fe99 6d 65 73 2e 20 20 6d 61 6c 6c 6f 63 65 64 20 2a  mes.  malloced *
3fe9a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  /.  sqlite3_toke
3fe9b 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
3fe9c 72 3b 20 20 2f 2a 20 74 6f 6b 65 6e 69 7a 65 72  r;  /* tokenizer
3fe9d 20 66 6f 72 20 69 6e 73 65 72 74 73 20 61 6e 64   for inserts and
3fe9e 20 71 75 65 72 69 65 73 20 2a 2f 0a 20 20 63 68   queries */.  ch
3fe9f 61 72 20 2a 7a 43 6f 6e 74 65 6e 74 54 62 6c 3b  ar *zContentTbl;
3fea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3fea1 20 63 6f 6e 74 65 6e 74 3d 78 78 78 20 6f 70 74   content=xxx opt
3fea2 69 6f 6e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  ion, or NULL */.
3fea3 20 20 63 68 61 72 20 2a 7a 4c 61 6e 67 75 61 67    char *zLanguag
3fea4 65 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  eid;            
3fea5 20 20 2f 2a 20 6c 61 6e 67 75 61 67 65 69 64 3d    /* languageid=
3fea6 78 78 78 20 6f 70 74 69 6f 6e 2c 20 6f 72 20 4e  xxx option, or N
3fea7 55 4c 4c 20 2a 2f 0a 20 20 75 38 20 62 41 75 74  ULL */.  u8 bAut
3fea8 6f 69 6e 63 72 6d 65 72 67 65 3b 20 20 20 20 20  oincrmerge;     
3fea9 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
3feaa 20 69 66 20 61 75 74 6f 6d 65 72 67 65 3d 31 20   if automerge=1 
3feab 2a 2f 0a 20 20 75 33 32 20 6e 4c 65 61 66 41 64  */.  u32 nLeafAd
3feac 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
3fead 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
3feae 66 20 6c 65 61 66 20 62 6c 6f 63 6b 73 20 61 64  f leaf blocks ad
3feaf 64 65 64 20 74 68 69 73 20 74 72 61 6e 73 20 2a  ded this trans *
3feb0 2f 0a 0a 20 20 2f 2a 20 50 72 65 63 6f 6d 70 69  /..  /* Precompi
3feb1 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 75  led statements u
3feb2 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65  sed by the imple
3feb3 6d 65 6e 74 61 74 69 6f 6e 2e 20 45 61 63 68 20  mentation. Each 
3feb4 6f 66 20 74 68 65 73 65 20 0a 20 20 2a 2a 20 73  of these .  ** s
3feb5 74 61 74 65 6d 65 6e 74 73 20 69 73 20 72 75 6e  tatements is run
3feb6 20 61 6e 64 20 72 65 73 65 74 20 77 69 74 68 69   and reset withi
3feb7 6e 20 61 20 73 69 6e 67 6c 65 20 76 69 72 74 75  n a single virtu
3feb8 61 6c 20 74 61 62 6c 65 20 41 50 49 20 63 61 6c  al table API cal
3feb9 6c 2e 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  l. .  */.  sqlit
3feba 65 33 5f 73 74 6d 74 20 2a 61 53 74 6d 74 5b 33  e3_stmt *aStmt[3
3febb 37 5d 3b 0a 0a 20 20 63 68 61 72 20 2a 7a 52 65  7];..  char *zRe
3febc 61 64 45 78 70 72 6c 69 73 74 3b 0a 20 20 63 68  adExprlist;.  ch
3febd 61 72 20 2a 7a 57 72 69 74 65 45 78 70 72 6c 69  ar *zWriteExprli
3febe 73 74 3b 0a 0a 20 20 69 6e 74 20 6e 4e 6f 64 65  st;..  int nNode
3febf 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
3fec0 20 20 20 20 20 20 20 2f 2a 20 53 6f 66 74 20 6c         /* Soft l
3fec1 69 6d 69 74 20 66 6f 72 20 6e 6f 64 65 20 73 69  imit for node si
3fec2 7a 65 20 2a 2f 0a 20 20 75 38 20 62 46 74 73 34  ze */.  u8 bFts4
3fec3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3fec4 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
3fec5 66 6f 72 20 46 54 53 34 2c 20 66 61 6c 73 65 20  for FTS4, false 
3fec6 66 6f 72 20 46 54 53 33 20 2a 2f 0a 20 20 75 38  for FTS3 */.  u8
3fec7 20 62 48 61 73 53 74 61 74 3b 20 20 20 20 20 20   bHasStat;      
3fec8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3fec9 20 54 72 75 65 20 69 66 20 25 5f 73 74 61 74 20   True if %_stat 
3feca 74 61 62 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a  table exists */.
3fecb 20 20 75 38 20 62 48 61 73 44 6f 63 73 69 7a 65    u8 bHasDocsize
3fecc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3fecd 20 20 2f 2a 20 54 72 75 65 20 69 66 20 25 5f 64    /* True if %_d
3fece 6f 63 73 69 7a 65 20 74 61 62 6c 65 20 65 78 69  ocsize table exi
3fecf 73 74 73 20 2a 2f 0a 20 20 75 38 20 62 44 65 73  sts */.  u8 bDes
3fed0 63 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20  cIdx;           
3fed1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
3fed2 20 69 66 20 64 6f 63 6c 69 73 74 73 20 61 72 65   if doclists are
3fed3 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65   in reverse orde
3fed4 72 20 2a 2f 0a 20 20 75 38 20 62 49 67 6e 6f 72  r */.  u8 bIgnor
3fed5 65 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20  eSavepoint;     
3fed6 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
3fed7 6f 20 69 67 6e 6f 72 65 20 78 53 61 76 65 70 6f  o ignore xSavepo
3fed8 69 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20  int invocations 
3fed9 2a 2f 0a 20 20 69 6e 74 20 6e 50 67 73 7a 3b 20  */.  int nPgsz; 
3feda 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3fedb 20 20 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a       /* Page siz
3fedc 65 20 66 6f 72 20 68 6f 73 74 20 64 61 74 61 62  e for host datab
3fedd 61 73 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ase */.  char *z
3fede 53 65 67 6d 65 6e 74 73 54 62 6c 3b 20 20 20 20  SegmentsTbl;    
3fedf 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
3fee0 20 6f 66 20 25 5f 73 65 67 6d 65 6e 74 73 20 74   of %_segments t
3fee1 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  able */.  sqlite
3fee2 33 5f 62 6c 6f 62 20 2a 70 53 65 67 6d 65 6e 74  3_blob *pSegment
3fee3 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f  s;        /* Blo
3fee4 62 20 68 61 6e 64 6c 65 20 6f 70 65 6e 20 6f 6e  b handle open on
3fee5 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61 62 6c   %_segments tabl
3fee6 65 20 2a 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a  e */..  /* .  **
3fee7 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
3fee8 72 72 61 79 20 6f 66 20 68 61 73 68 20 74 61 62  rray of hash tab
3fee9 6c 65 73 20 69 73 20 75 73 65 64 20 74 6f 20 62  les is used to b
3feea 75 66 66 65 72 20 70 65 6e 64 69 6e 67 20 69 6e  uffer pending in
3feeb 64 65 78 20 0a 20 20 2a 2a 20 75 70 64 61 74 65  dex .  ** update
3feec 73 20 64 75 72 69 6e 67 20 74 72 61 6e 73 61 63  s during transac
3feed 74 69 6f 6e 73 2e 20 41 6c 6c 20 70 65 6e 64 69  tions. All pendi
3feee 6e 67 20 75 70 64 61 74 65 73 20 62 75 66 66 65  ng updates buffe
3feef 72 65 64 20 61 74 20 61 6e 79 20 6f 6e 65 0a 20  red at any one. 
3fef0 20 2a 2a 20 74 69 6d 65 20 6d 75 73 74 20 73 68   ** time must sh
3fef1 61 72 65 20 61 20 63 6f 6d 6d 6f 6e 20 6c 61 6e  are a common lan
3fef2 67 75 61 67 65 2d 69 64 20 28 73 65 65 20 74 68  guage-id (see th
3fef3 65 20 46 54 53 34 20 6c 61 6e 67 69 64 3d 20 66  e FTS4 langid= f
3fef4 65 61 74 75 72 65 29 2e 0a 20 20 2a 2a 20 54 68  eature)..  ** Th
3fef5 65 20 63 75 72 72 65 6e 74 20 6c 61 6e 67 75 61  e current langua
3fef6 67 65 20 69 64 20 69 73 20 73 74 6f 72 65 64 20  ge id is stored 
3fef7 69 6e 20 76 61 72 69 61 62 6c 65 20 69 50 72 65  in variable iPre
3fef8 76 4c 61 6e 67 69 64 2e 0a 20 20 2a 2a 0a 20 20  vLangid..  **.  
3fef9 2a 2a 20 41 20 73 69 6e 67 6c 65 20 46 54 53 34  ** A single FTS4
3fefa 20 74 61 62 6c 65 20 6d 61 79 20 68 61 76 65 20   table may have 
3fefb 6d 75 6c 74 69 70 6c 65 20 66 75 6c 6c 2d 74 65  multiple full-te
3fefc 78 74 20 69 6e 64 65 78 65 73 2e 20 46 6f 72 20  xt indexes. For 
3fefd 65 61 63 68 20 69 6e 64 65 78 0a 20 20 2a 2a 20  each index.  ** 
3fefe 74 68 65 72 65 20 69 73 20 61 6e 20 65 6e 74 72  there is an entr
3feff 79 20 69 6e 20 74 68 65 20 61 49 6e 64 65 78 5b  y in the aIndex[
3ff00 5d 20 61 72 72 61 79 2e 20 49 6e 64 65 78 20 30  ] array. Index 0
3ff01 20 69 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20   is an index of 
3ff02 61 6c 6c 20 74 68 65 0a 20 20 2a 2a 20 74 65 72  all the.  ** ter
3ff03 6d 73 20 74 68 61 74 20 61 70 70 65 61 72 20 69  ms that appear i
3ff04 6e 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 20 73  n the document s
3ff05 65 74 2e 20 45 61 63 68 20 73 75 62 73 65 71 75  et. Each subsequ
3ff06 65 6e 74 20 69 6e 64 65 78 20 69 6e 20 61 49 6e  ent index in aIn
3ff07 64 65 78 5b 5d 0a 20 20 2a 2a 20 69 73 20 61 6e  dex[].  ** is an
3ff08 20 69 6e 64 65 78 20 6f 66 20 70 72 65 66 69 78   index of prefix
3ff09 65 73 20 6f 66 20 61 20 73 70 65 63 69 66 69 63  es of a specific
3ff0a 20 6c 65 6e 67 74 68 2e 0a 20 20 2a 2a 0a 20 20   length..  **.  
3ff0b 2a 2a 20 56 61 72 69 61 62 6c 65 20 6e 50 65 6e  ** Variable nPen
3ff0c 64 69 6e 67 44 61 74 61 20 63 6f 6e 74 61 69 6e  dingData contain
3ff0d 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 74 68  s an estimate th
3ff0e 65 20 6d 65 6d 6f 72 79 20 63 6f 6e 73 75 6d 65  e memory consume
3ff0f 64 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 70  d by the .  ** p
3ff10 65 6e 64 69 6e 67 20 64 61 74 61 20 73 74 72 75  ending data stru
3ff11 63 74 75 72 65 73 2c 20 69 6e 63 6c 75 64 69 6e  ctures, includin
3ff12 67 20 68 61 73 68 20 74 61 62 6c 65 20 6f 76 65  g hash table ove
3ff13 72 68 65 61 64 2c 20 62 75 74 20 6e 6f 74 20 69  rhead, but not i
3ff14 6e 63 6c 75 64 69 6e 67 0a 20 20 2a 2a 20 6d 61  ncluding.  ** ma
3ff15 6c 6c 6f 63 20 6f 76 65 72 68 65 61 64 2e 20 20  lloc overhead.  
3ff16 57 68 65 6e 20 6e 50 65 6e 64 69 6e 67 44 61 74  When nPendingDat
3ff17 61 20 65 78 63 65 65 64 73 20 6e 4d 61 78 50 65  a exceeds nMaxPe
3ff18 6e 64 69 6e 67 44 61 74 61 2c 20 61 6c 6c 20 68  ndingData, all h
3ff19 61 73 68 0a 20 20 2a 2a 20 74 61 62 6c 65 73 20  ash.  ** tables 
3ff1a 61 72 65 20 66 6c 75 73 68 65 64 20 74 6f 20 64  are flushed to d
3ff1b 69 73 6b 2e 20 56 61 72 69 61 62 6c 65 20 69 50  isk. Variable iP
3ff1c 72 65 76 44 6f 63 69 64 20 69 73 20 74 68 65 20  revDocid is the 
3ff1d 64 6f 63 69 64 20 6f 66 20 74 68 65 20 6d 6f 73  docid of the mos
3ff1e 74 20 0a 20 20 2a 2a 20 72 65 63 65 6e 74 6c 79  t .  ** recently
3ff1f 20 69 6e 73 65 72 74 65 64 20 72 65 63 6f 72 64   inserted record
3ff20 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e  ..  */.  int nIn
3ff21 64 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  dex;            
3ff22 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
3ff23 20 6f 66 20 61 49 6e 64 65 78 5b 5d 20 2a 2f 0a   of aIndex[] */.
3ff24 20 20 73 74 72 75 63 74 20 46 74 73 33 49 6e 64    struct Fts3Ind
3ff25 65 78 20 7b 0a 20 20 20 20 69 6e 74 20 6e 50 72  ex {.    int nPr
3ff26 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20  efix;           
3ff27 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 69 78         /* Prefix
3ff28 20 6c 65 6e 67 74 68 20 28 30 20 66 6f 72 20 6d   length (0 for m
3ff29 61 69 6e 20 74 65 72 6d 73 20 69 6e 64 65 78 29  ain terms index)
3ff2a 20 2a 2f 0a 20 20 20 20 46 74 73 33 48 61 73 68   */.    Fts3Hash
3ff2b 20 68 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20   hPending;      
3ff2c 20 20 20 20 20 20 2f 2a 20 50 65 6e 64 69 6e 67        /* Pending
3ff2d 20 74 65 72 6d 73 20 74 61 62 6c 65 20 66 6f 72   terms table for
3ff2e 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20   this index */. 
3ff2f 20 7d 20 2a 61 49 6e 64 65 78 3b 0a 20 20 69 6e   } *aIndex;.  in
3ff30 74 20 6e 4d 61 78 50 65 6e 64 69 6e 67 44 61 74  t nMaxPendingDat
3ff31 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  a;            /*
3ff32 20 4d 61 78 20 70 65 6e 64 69 6e 67 20 64 61 74   Max pending dat
3ff33 61 20 62 65 66 6f 72 65 20 66 6c 75 73 68 20 74  a before flush t
3ff34 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 69 6e 74 20  o disk */.  int 
3ff35 6e 50 65 6e 64 69 6e 67 44 61 74 61 3b 20 20 20  nPendingData;   
3ff36 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
3ff37 75 72 72 65 6e 74 20 62 79 74 65 73 20 6f 66 20  urrent bytes of 
3ff38 70 65 6e 64 69 6e 67 20 64 61 74 61 20 2a 2f 0a  pending data */.
3ff39 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
3ff3a 50 72 65 76 44 6f 63 69 64 3b 20 20 20 20 20 20  PrevDocid;      
3ff3b 20 20 2f 2a 20 44 6f 63 69 64 20 6f 66 20 6d 6f    /* Docid of mo
3ff3c 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65  st recently inse
3ff3d 72 74 65 64 20 64 6f 63 75 6d 65 6e 74 20 2a 2f  rted document */
3ff3e 0a 20 20 69 6e 74 20 69 50 72 65 76 4c 61 6e 67  .  int iPrevLang
3ff3f 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  id;             
3ff40 20 20 20 2f 2a 20 4c 61 6e 67 69 64 20 6f 66 20     /* Langid of 
3ff41 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65  recently inserte
3ff42 64 20 64 6f 63 75 6d 65 6e 74 20 2a 2f 0a 0a 23  d document */..#
3ff43 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
3ff44 45 5f 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69  E_DEBUG) || defi
3ff45 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 52  ned(SQLITE_COVER
3ff46 41 47 45 5f 54 45 53 54 29 0a 20 20 2f 2a 20 53  AGE_TEST).  /* S
3ff47 74 61 74 65 20 76 61 72 69 61 62 6c 65 73 20 75  tate variables u
3ff48 73 65 64 20 66 6f 72 20 76 61 6c 69 64 61 74 69  sed for validati
3ff49 6e 67 20 74 68 61 74 20 74 68 65 20 74 72 61 6e  ng that the tran
3ff4a 73 61 63 74 69 6f 6e 20 63 6f 6e 74 72 6f 6c 0a  saction control.
3ff4b 20 20 2a 2a 20 6d 65 74 68 6f 64 73 20 6f 66 20    ** methods of 
3ff4c 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
3ff4d 65 20 61 72 65 20 63 61 6c 6c 65 64 20 61 74 20  e are called at 
3ff4e 61 70 70 72 6f 70 72 69 61 74 65 20 74 69 6d 65  appropriate time
3ff4f 73 2e 20 20 54 68 65 73 65 0a 20 20 2a 2a 20 76  s.  These.  ** v
3ff50 61 6c 75 65 73 20 64 6f 20 6e 6f 74 20 63 6f 6e  alues do not con
3ff51 74 72 69 62 75 74 65 20 74 6f 20 46 54 53 20 66  tribute to FTS f
3ff52 75 6e 63 74 69 6f 6e 61 6c 69 74 79 3b 20 74 68  unctionality; th
3ff53 65 79 20 61 72 65 20 75 73 65 64 20 66 6f 72 0a  ey are used for.
3ff54 20 20 2a 2a 20 76 65 72 69 66 79 69 6e 67 20 74    ** verifying t
3ff55 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20  he operation of 
3ff56 74 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 2e  the SQLite core.
3ff57 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 54 72  .  */.  int inTr
3ff58 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f  ansaction;     /
3ff59 2a 20 54 72 75 65 20 61 66 74 65 72 20 78 42 65  * True after xBe
3ff5a 67 69 6e 20 62 75 74 20 62 65 66 6f 72 65 20 78  gin but before x
3ff5b 43 6f 6d 6d 69 74 2f 78 52 6f 6c 6c 62 61 63 6b  Commit/xRollback
3ff5c 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53 61 76 65   */.  int mxSave
3ff5d 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20  point;       /* 
3ff5e 4c 61 72 67 65 73 74 20 76 61 6c 69 64 20 78 53  Largest valid xS
3ff5f 61 76 65 70 6f 69 6e 74 20 69 6e 74 65 67 65 72  avepoint integer
3ff60 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f   */.#endif.};../
3ff61 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 6f  *.** When the co
3ff62 72 65 20 77 61 6e 74 73 20 74 6f 20 72 65 61 64  re wants to read
3ff63 20 66 72 6f 6d 20 74 68 65 20 76 69 72 74 75 61   from the virtua
3ff64 6c 20 74 61 62 6c 65 2c 20 69 74 20 63 72 65 61  l table, it crea
3ff65 74 65 73 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c  tes a.** virtual
3ff66 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20 28 61   table cursor (a
3ff67 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
3ff68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
3ff69 63 74 75 72 65 29 20 75 73 69 6e 67 0a 2a 2a 20  cture) using.** 
3ff6a 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64  the xOpen method
3ff6b 2e 20 43 75 72 73 6f 72 73 20 61 72 65 20 64 65  . Cursors are de
3ff6c 73 74 72 6f 79 65 64 20 75 73 69 6e 67 20 74 68  stroyed using th
3ff6d 65 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 2e  e xClose method.
3ff6e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 74 73 33 43  .*/.struct Fts3C
3ff6f 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65  ursor {.  sqlite
3ff70 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 62 61  3_vtab_cursor ba
3ff71 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 42 61 73  se;       /* Bas
3ff72 65 20 63 6c 61 73 73 20 75 73 65 64 20 62 79 20  e class used by 
3ff73 53 51 4c 69 74 65 20 63 6f 72 65 20 2a 2f 0a 20  SQLite core */. 
3ff74 20 69 31 36 20 65 53 65 61 72 63 68 3b 20 20 20   i16 eSearch;   
3ff75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ff76 20 2f 2a 20 53 65 61 72 63 68 20 73 74 72 61 74   /* Search strat
3ff77 65 67 79 20 28 73 65 65 20 62 65 6c 6f 77 29 20  egy (see below) 
3ff78 2a 2f 0a 20 20 75 38 20 69 73 45 6f 66 3b 20 20  */.  u8 isEof;  
3ff79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ff7a 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
3ff7b 61 74 20 45 6e 64 20 4f 66 20 52 65 73 75 6c 74  at End Of Result
3ff7c 73 20 2a 2f 0a 20 20 75 38 20 69 73 52 65 71 75  s */.  u8 isRequ
3ff7d 69 72 65 53 65 65 6b 3b 20 20 20 20 20 20 20 20  ireSeek;        
3ff7e 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
3ff7f 66 20 6d 75 73 74 20 73 65 65 6b 20 70 53 74 6d  f must seek pStm
3ff80 74 20 74 6f 20 25 5f 63 6f 6e 74 65 6e 74 20 72  t to %_content r
3ff81 6f 77 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ow */.  sqlite3_
3ff82 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20  stmt *pStmt;    
3ff83 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 70 61          /* Prepa
3ff84 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  red statement in
3ff85 20 75 73 65 20 62 79 20 74 68 65 20 63 75 72 73   use by the curs
3ff86 6f 72 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72  or */.  Fts3Expr
3ff87 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20   *pExpr;        
3ff88 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
3ff89 64 20 4d 41 54 43 48 20 71 75 65 72 79 20 73 74  d MATCH query st
3ff8a 72 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 4c  ring */.  int iL
3ff8b 61 6e 67 69 64 3b 20 20 20 20 20 20 20 20 20 20  angid;          
3ff8c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 6e            /* Lan
3ff8d 67 75 61 67 65 20 62 65 69 6e 67 20 71 75 65 72  guage being quer
3ff8e 69 65 64 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74  ied for */.  int
3ff8f 20 6e 50 68 72 61 73 65 3b 20 20 20 20 20 20 20   nPhrase;       
3ff90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3ff91 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 61  Number of matcha
3ff92 62 6c 65 20 70 68 72 61 73 65 73 20 69 6e 20 71  ble phrases in q
3ff93 75 65 72 79 20 2a 2f 0a 20 20 46 74 73 33 44 65  uery */.  Fts3De
3ff94 66 65 72 72 65 64 54 6f 6b 65 6e 20 2a 70 44 65  ferredToken *pDe
3ff95 66 65 72 72 65 64 3b 20 20 20 2f 2a 20 44 65 66  ferred;   /* Def
3ff96 65 72 72 65 64 20 73 65 61 72 63 68 20 74 6f 6b  erred search tok
3ff97 65 6e 73 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20  ens, if any */. 
3ff98 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
3ff99 50 72 65 76 49 64 3b 20 20 20 20 20 20 20 20 20  PrevId;         
3ff9a 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69 64 20   /* Previous id 
3ff9b 72 65 61 64 20 66 72 6f 6d 20 61 44 6f 63 6c 69  read from aDocli
3ff9c 73 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 4e  st */.  char *pN
3ff9d 65 78 74 49 64 3b 20 20 20 20 20 20 20 20 20 20  extId;          
3ff9e 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
3ff9f 65 72 20 69 6e 74 6f 20 74 68 65 20 62 6f 64 79  er into the body
3ffa0 20 6f 66 20 61 44 6f 63 6c 69 73 74 20 2a 2f 0a   of aDoclist */.
3ffa1 20 20 63 68 61 72 20 2a 61 44 6f 63 6c 69 73 74    char *aDoclist
3ffa2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3ffa3 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 6f 63    /* List of doc
3ffa4 69 64 73 20 66 6f 72 20 66 75 6c 6c 2d 74 65 78  ids for full-tex
3ffa5 74 20 71 75 65 72 69 65 73 20 2a 2f 0a 20 20 69  t queries */.  i
3ffa6 6e 74 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20  nt nDoclist;    
3ffa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3ffa8 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72  * Size of buffer
3ffa9 20 61 74 20 61 44 6f 63 6c 69 73 74 20 2a 2f 0a   at aDoclist */.
3ffaa 20 20 75 38 20 62 44 65 73 63 3b 20 20 20 20 20    u8 bDesc;     
3ffab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ffac 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 6f 72    /* True to sor
3ffad 74 20 69 6e 20 64 65 73 63 65 6e 64 69 6e 67 20  t in descending 
3ffae 6f 72 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 65  order */.  int e
3ffaf 45 76 61 6c 6d 6f 64 65 3b 20 20 20 20 20 20 20  Evalmode;       
3ffb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e             /* An
3ffb1 20 46 54 53 33 5f 45 56 41 4c 5f 58 58 20 63 6f   FTS3_EVAL_XX co
3ffb2 6e 73 74 61 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  nstant */.  int 
3ffb3 6e 52 6f 77 41 76 67 3b 20 20 20 20 20 20 20 20  nRowAvg;        
3ffb4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
3ffb5 76 65 72 61 67 65 20 73 69 7a 65 20 6f 66 20 64  verage size of d
3ffb6 61 74 61 62 61 73 65 20 72 6f 77 73 2c 20 69 6e  atabase rows, in
3ffb7 20 70 61 67 65 73 20 2a 2f 0a 20 20 73 71 6c 69   pages */.  sqli
3ffb8 74 65 33 5f 69 6e 74 36 34 20 6e 44 6f 63 3b 20  te3_int64 nDoc; 
3ffb9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
3ffba 6f 63 75 6d 65 6e 74 73 20 69 6e 20 74 61 62 6c  ocuments in tabl
3ffbb 65 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 4d 61  e */..  int isMa
3ffbc 74 63 68 69 6e 66 6f 4e 65 65 64 65 64 3b 20 20  tchinfoNeeded;  
3ffbd 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
3ffbe 77 68 65 6e 20 61 4d 61 74 63 68 69 6e 66 6f 5b  when aMatchinfo[
3ffbf 5d 20 6e 65 65 64 73 20 66 69 6c 6c 69 6e 67 20  ] needs filling 
3ffc0 69 6e 20 2a 2f 0a 20 20 75 33 32 20 2a 61 4d 61  in */.  u32 *aMa
3ffc1 74 63 68 69 6e 66 6f 3b 20 20 20 20 20 20 20 20  tchinfo;        
3ffc2 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72          /* Infor
3ffc3 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 6d 6f 73  mation about mos
3ffc4 74 20 72 65 63 65 6e 74 20 6d 61 74 63 68 20 2a  t recent match *
3ffc5 2f 0a 20 20 69 6e 74 20 6e 4d 61 74 63 68 69 6e  /.  int nMatchin
3ffc6 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  fo;             
3ffc7 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3ffc8 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 4d 61   elements in aMa
3ffc9 74 63 68 69 6e 66 6f 5b 5d 20 2a 2f 0a 20 20 63  tchinfo[] */.  c
3ffca 68 61 72 20 2a 7a 4d 61 74 63 68 69 6e 66 6f 3b  har *zMatchinfo;
3ffcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3ffcc 2a 20 4d 61 74 63 68 69 6e 66 6f 20 73 70 65 63  * Matchinfo spec
3ffcd 69 66 69 63 61 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a  ification */.};.
3ffce 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 45 56  .#define FTS3_EV
3ffcf 41 4c 5f 46 49 4c 54 45 52 20 20 20 20 30 0a 23  AL_FILTER    0.#
3ffd0 64 65 66 69 6e 65 20 46 54 53 33 5f 45 56 41 4c  define FTS3_EVAL
3ffd1 5f 4e 45 58 54 20 20 20 20 20 20 31 0a 23 64 65  _NEXT      1.#de
3ffd2 66 69 6e 65 20 46 54 53 33 5f 45 56 41 4c 5f 4d  fine FTS3_EVAL_M
3ffd3 41 54 43 48 49 4e 46 4f 20 32 0a 0a 2f 2a 0a 2a  ATCHINFO 2../*.*
3ffd4 2a 20 54 68 65 20 46 74 73 33 43 75 72 73 6f 72  * The Fts3Cursor
3ffd5 2e 65 53 65 61 72 63 68 20 6d 65 6d 62 65 72 20  .eSearch member 
3ffd6 69 73 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f  is always set to
3ffd7 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
3ffd8 6f 77 69 6e 67 2e 0a 2a 2a 20 41 63 74 75 61 6c  owing..** Actual
3ffd9 79 2c 20 46 74 73 33 43 75 72 73 6f 72 2e 65 53  y, Fts3Cursor.eS
3ffda 65 61 72 63 68 20 63 61 6e 20 62 65 20 67 72 65  earch can be gre
3ffdb 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
3ffdc 61 6c 20 74 6f 0a 2a 2a 20 46 54 53 33 5f 46 55  al to.** FTS3_FU
3ffdd 4c 4c 54 45 58 54 5f 53 45 41 52 43 48 2e 20 20  LLTEXT_SEARCH.  
3ffde 49 66 20 73 6f 2c 20 74 68 65 6e 20 46 74 73 33  If so, then Fts3
3ffdf 43 75 72 73 6f 72 2e 65 53 65 61 72 63 68 20 2d  Cursor.eSearch -
3ffe0 20 32 20 69 73 20 74 68 65 20 69 6e 64 65 78 0a   2 is the index.
3ffe1 2a 2a 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ** of the column
3ffe2 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64 2e   to be searched.
3ffe3 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69    For example, i
3ffe4 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41  n.**.**     CREA
3ffe5 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
3ffe6 20 65 78 31 20 55 53 49 4e 47 20 66 74 73 33 28   ex1 USING fts3(
3ffe7 61 2c 62 2c 63 2c 64 29 3b 0a 2a 2a 20 20 20 20  a,b,c,d);.**    
3ffe8 20 53 45 4c 45 43 54 20 64 6f 63 69 64 20 46 52   SELECT docid FR
3ffe9 4f 4d 20 65 78 31 20 57 48 45 52 45 20 62 20 4d  OM ex1 WHERE b M
3ffea 41 54 43 48 20 27 6f 6e 65 20 74 77 6f 20 74 68  ATCH 'one two th
3ffeb 72 65 65 27 3b 0a 2a 2a 20 0a 2a 2a 20 42 65 63  ree';.** .** Bec
3ffec 61 75 73 65 20 74 68 65 20 4c 48 53 20 6f 66 20  ause the LHS of 
3ffed 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74  the MATCH operat
3ffee 6f 72 20 69 73 20 32 6e 64 20 63 6f 6c 75 6d 6e  or is 2nd column
3ffef 20 22 62 22 2c 0a 2a 2a 20 46 74 73 33 43 75 72   "b",.** Fts3Cur
3fff0 73 6f 72 2e 65 53 65 61 72 63 68 20 77 69 6c 6c  sor.eSearch will
3fff1 20 62 65 20 73 65 74 20 74 6f 20 46 54 53 33 5f   be set to FTS3_
3fff2 46 55 4c 4c 54 45 58 54 5f 53 45 41 52 43 48 2b  FULLTEXT_SEARCH+
3fff3 31 2e 20 20 28 2b 30 20 66 6f 72 20 61 2c 0a 2a  1.  (+0 for a,.*
3fff4 2a 20 2b 31 20 66 6f 72 20 62 2c 20 2b 32 20 66  * +1 for b, +2 f
3fff5 6f 72 20 63 2c 20 2b 33 20 66 6f 72 20 64 2e 29  or c, +3 for d.)
3fff6 20 20 49 66 20 74 68 65 20 4c 48 53 20 6f 66 20    If the LHS of 
3fff7 4d 41 54 43 48 20 77 65 72 65 20 22 65 78 31 22  MATCH were "ex1"
3fff8 20 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20   .** indicating 
3fff9 74 68 61 74 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73  that all columns
3fffa 20 73 68 6f 75 6c 64 20 62 65 20 73 65 61 72 63   should be searc
3fffb 68 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 65 53 65  hed,.** then eSe
3fffc 61 72 63 68 20 77 6f 75 6c 64 20 62 65 20 73 65  arch would be se
3fffd 74 20 74 6f 20 46 54 53 33 5f 46 55 4c 4c 54 45  t to FTS3_FULLTE
3fffe 58 54 5f 53 45 41 52 43 48 2b 34 2e 0a 2a 2f 0a  XT_SEARCH+4..*/.
3ffff 23 64 65 66 69 6e 65 20 46 54 53 33 5f 46 55 4c  #define FTS3_FUL
40000 4c 53 43 41 4e 5f 53 45 41 52 43 48 20 30 20 20  LSCAN_SEARCH 0  
40001 20 20 2f 2a 20 4c 69 6e 65 61 72 20 73 63 61 6e    /* Linear scan
40002 20 6f 66 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61   of %_content ta
40003 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46  ble */.#define F
40004 54 53 33 5f 44 4f 43 49 44 5f 53 45 41 52 43 48  TS3_DOCID_SEARCH
40005 20 20 20 20 31 20 20 20 20 2f 2a 20 4c 6f 6f 6b      1    /* Look
40006 75 70 20 62 79 20 72 6f 77 69 64 20 6f 6e 20 25  up by rowid on %
40007 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 2a  _content table *
40008 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 46  /.#define FTS3_F
40009 55 4c 4c 54 45 58 54 5f 53 45 41 52 43 48 20 32  ULLTEXT_SEARCH 2
4000a 20 20 20 20 2f 2a 20 46 75 6c 6c 2d 74 65 78 74      /* Full-text
4000b 20 69 6e 64 65 78 20 73 65 61 72 63 68 20 2a 2f   index search */
4000c 0a 0a 0a 73 74 72 75 63 74 20 46 74 73 33 44 6f  ...struct Fts3Do
4000d 63 6c 69 73 74 20 7b 0a 20 20 63 68 61 72 20 2a  clist {.  char *
4000e 61 41 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  aAll;           
4000f 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61           /* Arra
40010 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 64 6f 63  y containing doc
40011 6c 69 73 74 20 28 6f 72 20 4e 55 4c 4c 29 20 2a  list (or NULL) *
40012 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 3b 20 20 20  /.  int nAll;   
40013 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40014 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 5b     /* Size of a[
40015 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  ] in bytes */.  
40016 63 68 61 72 20 2a 70 4e 65 78 74 44 6f 63 69 64  char *pNextDocid
40017 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
40018 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 78  * Pointer to nex
40019 74 20 64 6f 63 69 64 20 2a 2f 0a 0a 20 20 73 71  t docid */..  sq
4001a 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 44 6f 63  lite3_int64 iDoc
4001b 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  id;          /* 
4001c 43 75 72 72 65 6e 74 20 64 6f 63 69 64 20 28 69  Current docid (i
4001d 66 20 70 4c 69 73 74 21 3d 30 29 20 2a 2f 0a 20  f pList!=0) */. 
4001e 20 69 6e 74 20 62 46 72 65 65 4c 69 73 74 3b 20   int bFreeList; 
4001f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40020 2f 2a 20 54 72 75 65 20 69 66 20 70 4c 69 73 74  /* True if pList
40021 20 73 68 6f 75 6c 64 20 62 65 20 73 71 6c 69 74   should be sqlit
40022 65 33 5f 66 72 65 65 28 29 64 20 2a 2f 0a 20 20  e3_free()d */.  
40023 63 68 61 72 20 2a 70 4c 69 73 74 3b 20 20 20 20  char *pList;    
40024 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
40025 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 6f 73  * Pointer to pos
40026 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 6c 6c 6f  ition list follo
40027 77 69 6e 67 20 69 44 6f 63 69 64 20 2a 2f 0a 20  wing iDocid */. 
40028 20 69 6e 74 20 6e 4c 69 73 74 3b 20 20 20 20 20   int nList;     
40029 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4002a 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 70 6f 73  /* Length of pos
4002b 69 74 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 7d 3b  ition list */.};
4002c 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 70 68 72 61 73  ../*.** A "phras
4002d 65 22 20 69 73 20 61 20 73 65 71 75 65 6e 63 65  e" is a sequence
4002e 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   of one or more 
4002f 74 6f 6b 65 6e 73 20 74 68 61 74 20 6d 75 73 74  tokens that must
40030 20 6d 61 74 63 68 20 69 6e 0a 2a 2a 20 73 65 71   match in.** seq
40031 75 65 6e 63 65 2e 20 20 41 20 73 69 6e 67 6c 65  uence.  A single
40032 20 74 6f 6b 65 6e 20 69 73 20 74 68 65 20 62 61   token is the ba
40033 73 65 20 63 61 73 65 20 61 6e 64 20 74 68 65 20  se case and the 
40034 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20 63 61 73 65  most common case
40035 2e 0a 2a 2a 20 46 6f 72 20 61 20 73 65 71 75 65  ..** For a seque
40036 6e 63 65 20 6f 66 20 74 6f 6b 65 6e 73 20 63 6f  nce of tokens co
40037 6e 74 61 69 6e 65 64 20 69 6e 20 64 6f 75 62 6c  ntained in doubl
40038 65 2d 71 75 6f 74 65 73 20 28 69 2e 65 2e 20 22  e-quotes (i.e. "
40039 6f 6e 65 20 74 77 6f 20 74 68 72 65 65 22 29 0a  one two three").
4003a 2a 2a 20 6e 54 6f 6b 65 6e 20 77 69 6c 6c 20 62  ** nToken will b
4003b 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
4003c 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 73 74  tokens in the st
4003d 72 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ring..*/.struct 
4003e 46 74 73 33 50 68 72 61 73 65 54 6f 6b 65 6e 20  Fts3PhraseToken 
4003f 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20  {.  char *z;    
40040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40041 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74      /* Text of t
40042 68 65 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  he token */.  in
40043 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  t n;            
40044 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
40045 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
40046 20 69 6e 20 62 75 66 66 65 72 20 7a 20 2a 2f 0a   in buffer z */.
40047 20 20 69 6e 74 20 69 73 50 72 65 66 69 78 3b 20    int isPrefix; 
40048 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40049 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 6f 6b    /* True if tok
4004a 65 6e 20 65 6e 64 73 20 77 69 74 68 20 61 20 22  en ends with a "
4004b 2a 22 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a  *" character */.
4004c 20 20 69 6e 74 20 62 46 69 72 73 74 3b 20 20 20    int bFirst;   
4004d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4004e 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 6f 6b    /* True if tok
4004f 65 6e 20 6d 75 73 74 20 61 70 70 65 61 72 20 61  en must appear a
40050 74 20 70 6f 73 69 74 69 6f 6e 20 30 20 2a 2f 0a  t position 0 */.
40051 0a 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 73 20  .  /* Variables 
40052 61 62 6f 76 65 20 74 68 69 73 20 70 6f 69 6e 74  above this point
40053 20 61 72 65 20 70 6f 70 75 6c 61 74 65 64 20 77   are populated w
40054 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69  hen the expressi
40055 6f 6e 20 69 73 0a 20 20 2a 2a 20 70 61 72 73 65  on is.  ** parse
40056 64 20 28 62 79 20 63 6f 64 65 20 69 6e 20 66 74  d (by code in ft
40057 73 33 5f 65 78 70 72 2e 63 29 2e 20 42 65 6c 6f  s3_expr.c). Belo
40058 77 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  w this point the
40059 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 0a 20   variables are. 
4005a 20 2a 2a 20 75 73 65 64 20 77 68 65 6e 20 65 76   ** used when ev
4005b 61 6c 75 61 74 69 6e 67 20 74 68 65 20 65 78 70  aluating the exp
4005c 72 65 73 73 69 6f 6e 2e 20 2a 2f 0a 20 20 46 74  ression. */.  Ft
4005d 73 33 44 65 66 65 72 72 65 64 54 6f 6b 65 6e 20  s3DeferredToken 
4005e 2a 70 44 65 66 65 72 72 65 64 3b 20 20 20 2f 2a  *pDeferred;   /*
4005f 20 44 65 66 65 72 72 65 64 20 74 6f 6b 65 6e 20   Deferred token 
40060 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 69 73 20  object for this 
40061 74 6f 6b 65 6e 20 2a 2f 0a 20 20 46 74 73 33 4d  token */.  Fts3M
40062 75 6c 74 69 53 65 67 52 65 61 64 65 72 20 2a 70  ultiSegReader *p
40063 53 65 67 63 73 72 3b 20 20 20 20 2f 2a 20 53 65  Segcsr;    /* Se
40064 67 6d 65 6e 74 2d 72 65 61 64 65 72 20 66 6f 72  gment-reader for
40065 20 74 68 69 73 20 74 6f 6b 65 6e 20 2a 2f 0a 7d   this token */.}
40066 3b 0a 0a 73 74 72 75 63 74 20 46 74 73 33 50 68  ;..struct Fts3Ph
40067 72 61 73 65 20 7b 0a 20 20 2f 2a 20 43 61 63 68  rase {.  /* Cach
40068 65 20 6f 66 20 64 6f 63 6c 69 73 74 20 66 6f 72  e of doclist for
40069 20 74 68 69 73 20 70 68 72 61 73 65 2e 20 2a 2f   this phrase. */
4006a 0a 20 20 46 74 73 33 44 6f 63 6c 69 73 74 20 64  .  Fts3Doclist d
4006b 6f 63 6c 69 73 74 3b 0a 20 20 69 6e 74 20 62 49  oclist;.  int bI
4006c 6e 63 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  ncr;            
4006d 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
4006e 64 6f 63 6c 69 73 74 20 69 73 20 6c 6f 61 64 65  doclist is loade
4006f 64 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20  d incrementally 
40070 2a 2f 0a 20 20 69 6e 74 20 69 44 6f 63 6c 69 73  */.  int iDoclis
40071 74 54 6f 6b 65 6e 3b 0a 0a 20 20 2f 2a 20 56 61  tToken;..  /* Va
40072 72 69 61 62 6c 65 73 20 62 65 6c 6f 77 20 74 68  riables below th
40073 69 73 20 70 6f 69 6e 74 20 61 72 65 20 70 6f 70  is point are pop
40074 75 6c 61 74 65 64 20 62 79 20 66 74 73 33 5f 65  ulated by fts3_e
40075 78 70 72 2e 63 20 77 68 65 6e 20 70 61 72 73 69  xpr.c when parsi
40076 6e 67 20 0a 20 20 2a 2a 20 61 20 4d 41 54 43 48  ng .  ** a MATCH
40077 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 45 76 65   expression. Eve
40078 72 79 74 68 69 6e 67 20 61 62 6f 76 65 20 69 73  rything above is
40079 20 70 61 72 74 20 6f 66 20 74 68 65 20 65 76 61   part of the eva
4007a 6c 75 61 74 69 6f 6e 20 70 68 61 73 65 2e 20 0a  luation phase. .
4007b 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 65    */.  int nToke
4007c 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
4007d 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
4007e 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 70 68 72  okens in the phr
4007f 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  ase */.  int iCo
40080 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20  lumn;           
40081 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
40082 63 6f 6c 75 6d 6e 20 74 68 69 73 20 70 68 72 61  column this phra
40083 73 65 20 6d 75 73 74 20 6d 61 74 63 68 20 2a 2f  se must match */
40084 0a 20 20 46 74 73 33 50 68 72 61 73 65 54 6f 6b  .  Fts3PhraseTok
40085 65 6e 20 61 54 6f 6b 65 6e 5b 31 5d 3b 20 2f 2a  en aToken[1]; /*
40086 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65   One entry for e
40087 61 63 68 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65  ach token in the
40088 20 70 68 72 61 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f   phrase */.};../
40089 2a 0a 2a 2a 20 41 20 74 72 65 65 20 6f 66 20 74  *.** A tree of t
4008a 68 65 73 65 20 6f 62 6a 65 63 74 73 20 66 6f 72  hese objects for
4008b 6d 73 20 74 68 65 20 52 48 53 20 6f 66 20 61 20  ms the RHS of a 
4008c 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 2e 0a  MATCH operator..
4008d 2a 2a 0a 2a 2a 20 49 66 20 46 74 73 33 45 78 70  **.** If Fts3Exp
4008e 72 2e 65 54 79 70 65 20 69 73 20 46 54 53 51 55  r.eType is FTSQU
4008f 45 52 59 5f 50 48 52 41 53 45 20 61 6e 64 20 69  ERY_PHRASE and i
40090 73 4c 6f 61 64 65 64 20 69 73 20 74 72 75 65 2c  sLoaded is true,
40091 20 74 68 65 6e 20 61 44 6f 63 6c 69 73 74 20 0a   then aDoclist .
40092 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 6d  ** points to a m
40093 61 6c 6c 6f 63 65 64 20 62 75 66 66 65 72 2c 20  alloced buffer, 
40094 73 69 7a 65 20 6e 44 6f 63 6c 69 73 74 20 62 79  size nDoclist by
40095 74 65 73 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20  tes, containing 
40096 74 68 65 20 72 65 73 75 6c 74 73 20 0a 2a 2a 20  the results .** 
40097 6f 66 20 74 68 69 73 20 70 68 72 61 73 65 20 71  of this phrase q
40098 75 65 72 79 20 69 6e 20 46 54 53 33 20 64 6f 63  uery in FTS3 doc
40099 6c 69 73 74 20 66 6f 72 6d 61 74 2e 20 41 73 20  list format. As 
4009a 75 73 75 61 6c 2c 20 74 68 65 20 69 6e 69 74 69  usual, the initi
4009b 61 6c 20 0a 2a 2a 20 22 4c 65 6e 67 74 68 22 20  al .** "Length" 
4009c 66 69 65 6c 64 20 66 6f 75 6e 64 20 69 6e 20 64  field found in d
4009d 6f 63 6c 69 73 74 73 20 73 74 6f 72 65 64 20 6f  oclists stored o
4009e 6e 20 64 69 73 6b 20 69 73 20 6f 6d 69 74 74 65  n disk is omitte
4009f 64 20 66 72 6f 6d 20 74 68 69 73 20 0a 2a 2a 20  d from this .** 
400a0 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 56 61  buffer..**.** Va
400a1 72 69 61 62 6c 65 20 61 4d 49 20 69 73 20 75 73  riable aMI is us
400a2 65 64 20 6f 6e 6c 79 20 66 6f 72 20 46 54 53 51  ed only for FTSQ
400a3 55 45 52 59 5f 4e 45 41 52 20 6e 6f 64 65 73 20  UERY_NEAR nodes 
400a4 74 6f 20 73 74 6f 72 65 20 74 68 65 20 67 6c 6f  to store the glo
400a5 62 61 6c 0a 2a 2a 20 6d 61 74 63 68 69 6e 66 6f  bal.** matchinfo
400a6 20 64 61 74 61 2e 20 49 66 20 69 74 20 69 73 20   data. If it is 
400a7 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69  not NULL, it poi
400a8 6e 74 73 20 74 6f 20 61 6e 20 61 72 72 61 79 20  nts to an array 
400a9 6f 66 20 73 69 7a 65 20 6e 43 6f 6c 2a 33 2c 0a  of size nCol*3,.
400aa 2a 2a 20 77 68 65 72 65 20 6e 43 6f 6c 20 69 73  ** where nCol is
400ab 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
400ac 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 71 75  olumns in the qu
400ad 65 72 69 65 64 20 46 54 53 20 74 61 62 6c 65 2e  eried FTS table.
400ae 20 54 68 65 20 61 72 72 61 79 0a 2a 2a 20 69 73   The array.** is
400af 20 70 6f 70 75 6c 61 74 65 64 20 61 73 20 66 6f   populated as fo
400b0 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61  llows:.**.**   a
400b1 4d 49 5b 69 43 6f 6c 2a 33 20 2b 20 30 5d 20 3d  MI[iCol*3 + 0] =
400b2 20 55 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 20 20   Undefined.**   
400b3 61 4d 49 5b 69 43 6f 6c 2a 33 20 2b 20 31 5d 20  aMI[iCol*3 + 1] 
400b4 3d 20 4e 75 6d 62 65 72 20 6f 66 20 6f 63 63 75  = Number of occu
400b5 72 72 65 6e 63 65 73 0a 2a 2a 20 20 20 61 4d 49  rrences.**   aMI
400b6 5b 69 43 6f 6c 2a 33 20 2b 20 32 5d 20 3d 20 4e  [iCol*3 + 2] = N
400b7 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 63 6f  umber of rows co
400b8 6e 74 61 69 6e 69 6e 67 20 61 74 20 6c 65 61 73  ntaining at leas
400b9 74 20 6f 6e 65 20 69 6e 73 74 61 6e 63 65 0a 2a  t one instance.*
400ba 2a 0a 2a 2a 20 54 68 65 20 61 4d 49 20 61 72 72  *.** The aMI arr
400bb 61 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  ay is allocated 
400bc 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 6d 61  using sqlite3_ma
400bd 6c 6c 6f 63 28 29 2e 20 49 74 20 73 68 6f 75 6c  lloc(). It shoul
400be 64 20 62 65 20 66 72 65 65 64 20 0a 2a 2a 20 77  d be freed .** w
400bf 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69  hen the expressi
400c0 6f 6e 20 6e 6f 64 65 20 69 73 2e 0a 2a 2f 0a 73  on node is..*/.s
400c1 74 72 75 63 74 20 46 74 73 33 45 78 70 72 20 7b  truct Fts3Expr {
400c2 0a 20 20 69 6e 74 20 65 54 79 70 65 3b 20 20 20  .  int eType;   
400c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
400c4 20 4f 6e 65 20 6f 66 20 74 68 65 20 46 54 53 51   One of the FTSQ
400c5 55 45 52 59 5f 58 58 58 20 76 61 6c 75 65 73 20  UERY_XXX values 
400c6 64 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f  defined below */
400c7 0a 20 20 69 6e 74 20 6e 4e 65 61 72 3b 20 20 20  .  int nNear;   
400c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
400c9 20 56 61 6c 69 64 20 69 66 20 65 54 79 70 65 3d   Valid if eType=
400ca 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 2a  =FTSQUERY_NEAR *
400cb 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 50  /.  Fts3Expr *pP
400cc 61 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20 2f  arent;         /
400cd 2a 20 70 50 61 72 65 6e 74 2d 3e 70 4c 65 66 74  * pParent->pLeft
400ce 3d 3d 74 68 69 73 20 6f 72 20 70 50 61 72 65 6e  ==this or pParen
400cf 74 2d 3e 70 52 69 67 68 74 3d 3d 74 68 69 73 20  t->pRight==this 
400d0 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  */.  Fts3Expr *p
400d1 4c 65 66 74 3b 20 20 20 20 20 20 20 20 20 20 20  Left;           
400d2 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 20  /* Left operand 
400d3 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  */.  Fts3Expr *p
400d4 52 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20  Right;          
400d5 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64  /* Right operand
400d6 20 2a 2f 0a 20 20 46 74 73 33 50 68 72 61 73 65   */.  Fts3Phrase
400d7 20 2a 70 50 68 72 61 73 65 3b 20 20 20 20 20 20   *pPhrase;      
400d8 20 2f 2a 20 56 61 6c 69 64 20 69 66 20 65 54 79   /* Valid if eTy
400d9 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe==FTSQUERY_PHR
400da 41 53 45 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65  ASE */..  /* The
400db 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 75   following are u
400dc 73 65 64 20 62 79 20 74 68 65 20 66 74 73 33 5f  sed by the fts3_
400dd 65 76 61 6c 2e 63 20 6d 6f 64 75 6c 65 2e 20 2a  eval.c module. *
400de 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
400df 34 20 69 44 6f 63 69 64 3b 20 20 20 20 20 20 2f  4 iDocid;      /
400e0 2a 20 43 75 72 72 65 6e 74 20 64 6f 63 69 64 20  * Current docid 
400e1 2a 2f 0a 20 20 75 38 20 62 45 6f 66 3b 20 20 20  */.  u8 bEof;   
400e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
400e3 2f 2a 20 54 72 75 65 20 74 68 69 73 20 65 78 70  /* True this exp
400e4 72 65 73 73 69 6f 6e 20 69 73 20 61 74 20 45 4f  ression is at EO
400e5 46 20 61 6c 72 65 61 64 79 20 2a 2f 0a 20 20 75  F already */.  u
400e6 38 20 62 53 74 61 72 74 3b 20 20 20 20 20 20 20  8 bStart;       
400e7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
400e8 65 20 69 66 20 69 44 6f 63 69 64 20 69 73 20 76  e if iDocid is v
400e9 61 6c 69 64 20 2a 2f 0a 20 20 75 38 20 62 44 65  alid */.  u8 bDe
400ea 66 65 72 72 65 64 3b 20 20 20 20 20 20 20 20 20  ferred;         
400eb 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
400ec 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20  this expression 
400ed 69 73 20 65 6e 74 69 72 65 6c 79 20 64 65 66 65  is entirely defe
400ee 72 72 65 64 20 2a 2f 0a 0a 20 20 75 33 32 20 2a  rred */..  u32 *
400ef 61 4d 49 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  aMI;.};../*.** C
400f0 61 6e 64 69 64 61 74 65 20 76 61 6c 75 65 73 20  andidate values 
400f1 66 6f 72 20 46 74 73 33 51 75 65 72 79 2e 65 54  for Fts3Query.eT
400f2 79 70 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 74  ype. Note that t
400f3 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20  he order of the 
400f4 66 69 72 73 74 0a 2a 2a 20 66 6f 75 72 20 76 61  first.** four va
400f5 6c 75 65 73 20 69 73 20 69 6e 20 6f 72 64 65 72  lues is in order
400f6 20 6f 66 20 70 72 65 63 65 64 65 6e 63 65 20 77   of precedence w
400f7 68 65 6e 20 70 61 72 73 69 6e 67 20 65 78 70 72  hen parsing expr
400f8 65 73 73 69 6f 6e 73 2e 20 46 6f 72 20 0a 2a 2a  essions. For .**
400f9 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 66 6f   example, the fo
400fa 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
400fb 20 22 61 20 4f 52 20 62 20 41 4e 44 20 63 20 4e   "a OR b AND c N
400fc 4f 54 20 64 20 4e 45 41 52 20 65 22 0a 2a 2a 0a  OT d NEAR e".**.
400fd 2a 2a 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74  ** is equivalent
400fe 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 61 20   to:.**.**   "a 
400ff 4f 52 20 28 62 20 41 4e 44 20 28 63 20 4e 4f 54  OR (b AND (c NOT
40100 20 28 64 20 4e 45 41 52 20 65 29 29 29 22 0a 2a   (d NEAR e)))".*
40101 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55 45  /.#define FTSQUE
40102 52 59 5f 4e 45 41 52 20 20 20 31 0a 23 64 65 66  RY_NEAR   1.#def
40103 69 6e 65 20 46 54 53 51 55 45 52 59 5f 4e 4f 54  ine FTSQUERY_NOT
40104 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 46 54      2.#define FT
40105 53 51 55 45 52 59 5f 41 4e 44 20 20 20 20 33 0a  SQUERY_AND    3.
40106 23 64 65 66 69 6e 65 20 46 54 53 51 55 45 52 59  #define FTSQUERY
40107 5f 4f 52 20 20 20 20 20 34 0a 23 64 65 66 69 6e  _OR     4.#defin
40108 65 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  e FTSQUERY_PHRAS
40109 45 20 35 0a 0a 0a 2f 2a 20 66 74 73 33 5f 77 72  E 5.../* fts3_wr
4010a 69 74 65 2e 63 20 2a 2f 0a 53 51 4c 49 54 45 5f  ite.c */.SQLITE_
4010b 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4010c 74 65 33 46 74 73 33 55 70 64 61 74 65 4d 65 74  te3Fts3UpdateMet
4010d 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62  hod(sqlite3_vtab
4010e 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
4010f 6c 75 65 2a 2a 2c 73 71 6c 69 74 65 33 5f 69 6e  lue**,sqlite3_in
40110 74 36 34 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  t64*);.SQLITE_PR
40111 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
40112 33 46 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d  3Fts3PendingTerm
40113 73 46 6c 75 73 68 28 46 74 73 33 54 61 62 6c 65  sFlush(Fts3Table
40114 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
40115 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
40116 46 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73  Fts3PendingTerms
40117 43 6c 65 61 72 28 46 74 73 33 54 61 62 6c 65 20  Clear(Fts3Table 
40118 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
40119 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
4011a 73 33 4f 70 74 69 6d 69 7a 65 28 46 74 73 33 54  s3Optimize(Fts3T
4011b 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  able *);.SQLITE_
4011c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4011d 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72  te3Fts3SegReader
4011e 4e 65 77 28 69 6e 74 2c 20 69 6e 74 2c 20 73 71  New(int, int, sq
4011f 6c 69 74 65 33 5f 69 6e 74 36 34 2c 0a 20 20 73  lite3_int64,.  s
40120 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 73 71  qlite3_int64, sq
40121 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 63 6f 6e  lite3_int64, con
40122 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20  st char *, int, 
40123 46 74 73 33 53 65 67 52 65 61 64 65 72 2a 2a 29  Fts3SegReader**)
40124 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
40125 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
40126 53 65 67 52 65 61 64 65 72 50 65 6e 64 69 6e 67  SegReaderPending
40127 28 0a 20 20 46 74 73 33 54 61 62 6c 65 2a 2c 69  (.  Fts3Table*,i
40128 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  nt,const char*,i
40129 6e 74 2c 69 6e 74 2c 46 74 73 33 53 65 67 52 65  nt,int,Fts3SegRe
4012a 61 64 65 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  ader**);.SQLITE_
4012b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4012c 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65  ite3Fts3SegReade
4012d 72 46 72 65 65 28 46 74 73 33 53 65 67 52 65 61  rFree(Fts3SegRea
4012e 64 65 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  der *);.SQLITE_P
4012f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
40130 65 33 46 74 73 33 41 6c 6c 53 65 67 64 69 72 73  e3Fts3AllSegdirs
40131 28 46 74 73 33 54 61 62 6c 65 2a 2c 20 69 6e 74  (Fts3Table*, int
40132 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 73 71 6c 69  , int, int, sqli
40133 74 65 33 5f 73 74 6d 74 20 2a 2a 29 3b 0a 53 51  te3_stmt **);.SQ
40134 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
40135 20 73 71 6c 69 74 65 33 46 74 73 33 52 65 61 64   sqlite3Fts3Read
40136 4c 6f 63 6b 28 46 74 73 33 54 61 62 6c 65 20 2a  Lock(Fts3Table *
40137 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
40138 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
40139 33 52 65 61 64 42 6c 6f 63 6b 28 46 74 73 33 54  3ReadBlock(Fts3T
4013a 61 62 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f 69  able*, sqlite3_i
4013b 6e 74 36 34 2c 20 63 68 61 72 20 2a 2a 2c 20 69  nt64, char **, i
4013c 6e 74 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 53 51 4c  nt*, int*);..SQL
4013d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4013e 73 71 6c 69 74 65 33 46 74 73 33 53 65 6c 65 63  sqlite3Fts3Selec
4013f 74 44 6f 63 74 6f 74 61 6c 28 46 74 73 33 54 61  tDoctotal(Fts3Ta
40140 62 6c 65 20 2a 2c 20 73 71 6c 69 74 65 33 5f 73  ble *, sqlite3_s
40141 74 6d 74 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  tmt **);.SQLITE_
40142 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
40143 74 65 33 46 74 73 33 53 65 6c 65 63 74 44 6f 63  te3Fts3SelectDoc
40144 73 69 7a 65 28 46 74 73 33 54 61 62 6c 65 20 2a  size(Fts3Table *
40145 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c  , sqlite3_int64,
40146 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
40147 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
40148 54 45 5f 44 49 53 41 42 4c 45 5f 46 54 53 34 5f  TE_DISABLE_FTS4_
40149 44 45 46 45 52 52 45 44 0a 53 51 4c 49 54 45 5f  DEFERRED.SQLITE_
4014a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4014b 69 74 65 33 46 74 73 33 46 72 65 65 44 65 66 65  ite3Fts3FreeDefe
4014c 72 72 65 64 54 6f 6b 65 6e 73 28 46 74 73 33 43  rredTokens(Fts3C
4014d 75 72 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45  ursor *);.SQLITE
4014e 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4014f 69 74 65 33 46 74 73 33 44 65 66 65 72 54 6f 6b  ite3Fts3DeferTok
40150 65 6e 28 46 74 73 33 43 75 72 73 6f 72 20 2a 2c  en(Fts3Cursor *,
40151 20 46 74 73 33 50 68 72 61 73 65 54 6f 6b 65 6e   Fts3PhraseToken
40152 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   *, int);.SQLITE
40153 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
40154 69 74 65 33 46 74 73 33 43 61 63 68 65 44 65 66  ite3Fts3CacheDef
40155 65 72 72 65 64 44 6f 63 6c 69 73 74 73 28 46 74  erredDoclists(Ft
40156 73 33 43 75 72 73 6f 72 20 2a 29 3b 0a 53 51 4c  s3Cursor *);.SQL
40157 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
40158 20 73 71 6c 69 74 65 33 46 74 73 33 46 72 65 65   sqlite3Fts3Free
40159 44 65 66 65 72 72 65 64 44 6f 63 6c 69 73 74 73  DeferredDoclists
4015a 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29 3b 0a  (Fts3Cursor *);.
4015b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4015c 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 44 65  nt sqlite3Fts3De
4015d 66 65 72 72 65 64 54 6f 6b 65 6e 4c 69 73 74 28  ferredTokenList(
4015e 46 74 73 33 44 65 66 65 72 72 65 64 54 6f 6b 65  Fts3DeferredToke
4015f 6e 20 2a 2c 20 63 68 61 72 20 2a 2a 2c 20 69 6e  n *, char **, in
40160 74 20 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65  t *);.#else.# de
40161 66 69 6e 65 20 73 71 6c 69 74 65 33 46 74 73 33  fine sqlite3Fts3
40162 46 72 65 65 44 65 66 65 72 72 65 64 54 6f 6b 65  FreeDeferredToke
40163 6e 73 28 78 29 0a 23 20 64 65 66 69 6e 65 20 73  ns(x).# define s
40164 71 6c 69 74 65 33 46 74 73 33 44 65 66 65 72 54  qlite3Fts3DeferT
40165 6f 6b 65 6e 28 78 2c 79 2c 7a 29 20 53 51 4c 49  oken(x,y,z) SQLI
40166 54 45 5f 4f 4b 0a 23 20 64 65 66 69 6e 65 20 73  TE_OK.# define s
40167 71 6c 69 74 65 33 46 74 73 33 43 61 63 68 65 44  qlite3Fts3CacheD
40168 65 66 65 72 72 65 64 44 6f 63 6c 69 73 74 73 28  eferredDoclists(
40169 78 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64  x) SQLITE_OK.# d
4016a 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 74 73  efine sqlite3Fts
4016b 33 46 72 65 65 44 65 66 65 72 72 65 64 44 6f 63  3FreeDeferredDoc
4016c 6c 69 73 74 73 28 78 29 0a 23 20 64 65 66 69 6e  lists(x).# defin
4016d 65 20 73 71 6c 69 74 65 33 46 74 73 33 44 65 66  e sqlite3Fts3Def
4016e 65 72 72 65 64 54 6f 6b 65 6e 4c 69 73 74 28 78  erredTokenList(x
4016f 2c 79 2c 7a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a  ,y,z) SQLITE_OK.
40170 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
40171 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
40172 74 65 33 46 74 73 33 53 65 67 6d 65 6e 74 73 43  te3Fts3SegmentsC
40173 6c 6f 73 65 28 46 74 73 33 54 61 62 6c 65 20 2a  lose(Fts3Table *
40174 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
40175 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
40176 33 4d 61 78 4c 65 76 65 6c 28 46 74 73 33 54 61  3MaxLevel(Fts3Ta
40177 62 6c 65 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a 0a  ble *, int *);..
40178 2f 2a 20 53 70 65 63 69 61 6c 20 76 61 6c 75 65  /* Special value
40179 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 62 79  s interpreted by
4017a 20 73 71 6c 69 74 65 33 53 65 67 52 65 61 64 65   sqlite3SegReade
4017b 72 43 75 72 73 6f 72 28 29 20 2a 2f 0a 23 64 65  rCursor() */.#de
4017c 66 69 6e 65 20 46 54 53 33 5f 53 45 47 43 55 52  fine FTS3_SEGCUR
4017d 53 4f 52 5f 50 45 4e 44 49 4e 47 20 20 20 20 20  SOR_PENDING     
4017e 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 46 54     -1.#define FT
4017f 53 33 5f 53 45 47 43 55 52 53 4f 52 5f 41 4c 4c  S3_SEGCURSOR_ALL
40180 20 20 20 20 20 20 20 20 20 20 20 20 2d 32 0a 0a              -2..
40181 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
40182 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 53 65  nt sqlite3Fts3Se
40183 67 52 65 61 64 65 72 53 74 61 72 74 28 46 74 73  gReaderStart(Fts
40184 33 54 61 62 6c 65 2a 2c 20 46 74 73 33 4d 75 6c  3Table*, Fts3Mul
40185 74 69 53 65 67 52 65 61 64 65 72 2a 2c 20 46 74  tiSegReader*, Ft
40186 73 33 53 65 67 46 69 6c 74 65 72 2a 29 3b 0a 53  s3SegFilter*);.S
40187 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
40188 74 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67  t sqlite3Fts3Seg
40189 52 65 61 64 65 72 53 74 65 70 28 46 74 73 33 54  ReaderStep(Fts3T
4018a 61 62 6c 65 20 2a 2c 20 46 74 73 33 4d 75 6c 74  able *, Fts3Mult
4018b 69 53 65 67 52 65 61 64 65 72 20 2a 29 3b 0a 53  iSegReader *);.S
4018c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4018d 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53 65  id sqlite3Fts3Se
4018e 67 52 65 61 64 65 72 46 69 6e 69 73 68 28 46 74  gReaderFinish(Ft
4018f 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72  s3MultiSegReader
40190 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49   *);..SQLITE_PRI
40191 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
40192 46 74 73 33 53 65 67 52 65 61 64 65 72 43 75 72  Fts3SegReaderCur
40193 73 6f 72 28 46 74 73 33 54 61 62 6c 65 20 2a 2c  sor(Fts3Table *,
40194 20 0a 20 20 20 20 69 6e 74 2c 20 69 6e 74 2c 20   .    int, int, 
40195 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  int, const char 
40196 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
40197 2c 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65  , Fts3MultiSegRe
40198 61 64 65 72 20 2a 29 3b 0a 0a 2f 2a 20 46 6c 61  ader *);../* Fla
40199 67 73 20 61 6c 6c 6f 77 65 64 20 61 73 20 70 61  gs allowed as pa
4019a 72 74 20 6f 66 20 74 68 65 20 34 74 68 20 61 72  rt of the 4th ar
4019b 67 75 6d 65 6e 74 20 74 6f 20 53 65 67 6d 65 6e  gument to Segmen
4019c 74 52 65 61 64 65 72 49 74 65 72 61 74 65 28 29  tReaderIterate()
4019d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 33   */.#define FTS3
4019e 5f 53 45 47 4d 45 4e 54 5f 52 45 51 55 49 52 45  _SEGMENT_REQUIRE
4019f 5f 50 4f 53 20 20 20 30 78 30 30 30 30 30 30 30  _POS   0x0000000
401a0 31 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 53  1.#define FTS3_S
401a1 45 47 4d 45 4e 54 5f 49 47 4e 4f 52 45 5f 45 4d  EGMENT_IGNORE_EM
401a2 50 54 59 20 20 30 78 30 30 30 30 30 30 30 32 0a  PTY  0x00000002.
401a3 23 64 65 66 69 6e 65 20 46 54 53 33 5f 53 45 47  #define FTS3_SEG
401a4 4d 45 4e 54 5f 43 4f 4c 55 4d 4e 5f 46 49 4c 54  MENT_COLUMN_FILT
401a5 45 52 20 30 78 30 30 30 30 30 30 30 34 0a 23 64  ER 0x00000004.#d
401a6 65 66 69 6e 65 20 46 54 53 33 5f 53 45 47 4d 45  efine FTS3_SEGME
401a7 4e 54 5f 50 52 45 46 49 58 20 20 20 20 20 20 20  NT_PREFIX       
401a8 20 30 78 30 30 30 30 30 30 30 38 0a 23 64 65 66   0x00000008.#def
401a9 69 6e 65 20 46 54 53 33 5f 53 45 47 4d 45 4e 54  ine FTS3_SEGMENT
401aa 5f 53 43 41 4e 20 20 20 20 20 20 20 20 20 20 30  _SCAN          0
401ab 78 30 30 30 30 30 30 31 30 0a 23 64 65 66 69 6e  x00000010.#defin
401ac 65 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 46  e FTS3_SEGMENT_F
401ad 49 52 53 54 20 20 20 20 20 20 20 20 20 30 78 30  IRST         0x0
401ae 30 30 30 30 30 32 30 0a 0a 2f 2a 20 54 79 70 65  0000020../* Type
401af 20 70 61 73 73 65 64 20 61 73 20 34 74 68 20 61   passed as 4th a
401b0 72 67 75 6d 65 6e 74 20 74 6f 20 53 65 67 6d 65  rgument to Segme
401b1 6e 74 52 65 61 64 65 72 49 74 65 72 61 74 65 28  ntReaderIterate(
401b2 29 20 2a 2f 0a 73 74 72 75 63 74 20 46 74 73 33  ) */.struct Fts3
401b3 53 65 67 46 69 6c 74 65 72 20 7b 0a 20 20 63 6f  SegFilter {.  co
401b4 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b  nst char *zTerm;
401b5 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 0a 20 20  .  int nTerm;.  
401b6 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 69 6e 74 20  int iCol;.  int 
401b7 66 6c 61 67 73 3b 0a 7d 3b 0a 0a 73 74 72 75 63  flags;.};..struc
401b8 74 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65  t Fts3MultiSegRe
401b9 61 64 65 72 20 7b 0a 20 20 2f 2a 20 55 73 65 64  ader {.  /* Used
401ba 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 73   internally by s
401bb 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61  qlite3Fts3SegRea
401bc 64 65 72 58 58 58 28 29 20 63 61 6c 6c 73 20 2a  derXXX() calls *
401bd 2f 0a 20 20 46 74 73 33 53 65 67 52 65 61 64 65  /.  Fts3SegReade
401be 72 20 2a 2a 61 70 53 65 67 6d 65 6e 74 3b 20 20  r **apSegment;  
401bf 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
401c0 46 74 73 33 53 65 67 52 65 61 64 65 72 20 6f 62  Fts3SegReader ob
401c1 6a 65 63 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  jects */.  int n
401c2 53 65 67 6d 65 6e 74 3b 20 20 20 20 20 20 20 20  Segment;        
401c3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
401c4 7a 65 20 6f 66 20 61 70 53 65 67 6d 65 6e 74 20  ze of apSegment 
401c5 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 6e  array */.  int n
401c6 41 64 76 61 6e 63 65 3b 20 20 20 20 20 20 20 20  Advance;        
401c7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f             /* Ho
401c8 77 20 6d 61 6e 79 20 73 65 67 2d 72 65 61 64 65  w many seg-reade
401c9 72 73 20 74 6f 20 61 64 76 61 6e 63 65 20 2a 2f  rs to advance */
401ca 0a 20 20 46 74 73 33 53 65 67 46 69 6c 74 65 72  .  Fts3SegFilter
401cb 20 2a 70 46 69 6c 74 65 72 3b 20 20 20 20 20 20   *pFilter;      
401cc 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
401cd 20 66 69 6c 74 65 72 20 6f 62 6a 65 63 74 20 2a   filter object *
401ce 2f 0a 20 20 63 68 61 72 20 2a 61 42 75 66 66 65  /.  char *aBuffe
401cf 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
401d0 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f      /* Buffer to
401d1 20 6d 65 72 67 65 20 64 6f 63 6c 69 73 74 73 20   merge doclists 
401d2 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 66  in */.  int nBuf
401d3 66 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  fer;            
401d4 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63          /* Alloc
401d5 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 42 75  ated size of aBu
401d6 66 66 65 72 5b 5d 20 69 6e 20 62 79 74 65 73 20  ffer[] in bytes 
401d7 2a 2f 0a 0a 20 20 69 6e 74 20 69 43 6f 6c 46 69  */..  int iColFi
401d8 6c 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  lter;           
401d9 20 20 20 20 20 20 2f 2a 20 49 66 20 3e 3d 30 2c        /* If >=0,
401da 20 66 69 6c 74 65 72 20 66 6f 72 20 74 68 69 73   filter for this
401db 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74   column */.  int
401dc 20 62 52 65 73 74 61 72 74 3b 0a 0a 20 20 2f 2a   bRestart;..  /*
401dd 20 55 73 65 64 20 62 79 20 66 74 73 33 2e 63 20   Used by fts3.c 
401de 6f 6e 6c 79 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  only. */.  int n
401df 43 6f 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Cost;           
401e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
401e1 73 74 20 6f 66 20 72 75 6e 6e 69 6e 67 20 69 74  st of running it
401e2 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  erator */.  int 
401e3 62 4c 6f 6f 6b 75 70 3b 20 20 20 20 20 20 20 20  bLookup;        
401e4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
401e5 72 75 65 20 69 66 20 61 20 6c 6f 6f 6b 75 70 20  rue if a lookup 
401e6 6f 66 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72  of a single entr
401e7 79 2e 20 2a 2f 0a 0a 20 20 2f 2a 20 4f 75 74 70  y. */..  /* Outp
401e8 75 74 20 76 61 6c 75 65 73 2e 20 56 61 6c 69 64  ut values. Valid
401e9 20 6f 6e 6c 79 20 61 66 74 65 72 20 46 74 73 33   only after Fts3
401ea 53 65 67 52 65 61 64 65 72 53 74 65 70 28 29 20  SegReaderStep() 
401eb 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 52  returns SQLITE_R
401ec 4f 57 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  OW. */.  char *z
401ed 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20  Term;           
401ee 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
401ef 74 65 72 20 74 6f 20 74 65 72 6d 20 62 75 66 66  ter to term buff
401f0 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72  er */.  int nTer
401f1 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
401f2 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
401f3 6f 66 20 7a 54 65 72 6d 20 69 6e 20 62 79 74 65  of zTerm in byte
401f4 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 44 6f  s */.  char *aDo
401f5 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  clist;          
401f6 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
401f7 72 20 74 6f 20 64 6f 63 6c 69 73 74 20 62 75 66  r to doclist buf
401f8 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 6f  fer */.  int nDo
401f9 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  clist;          
401fa 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
401fb 20 6f 66 20 61 44 6f 63 6c 69 73 74 5b 5d 20 69   of aDoclist[] i
401fc 6e 20 62 79 74 65 73 20 2a 2f 0a 7d 3b 0a 0a 53  n bytes */.};..S
401fd 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
401fe 74 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 63  t sqlite3Fts3Inc
401ff 72 6d 65 72 67 65 28 46 74 73 33 54 61 62 6c 65  rmerge(Fts3Table
40200 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 0a 2f 2a 20  *,int,int);../* 
40201 66 74 73 33 2e 63 20 2a 2f 0a 53 51 4c 49 54 45  fts3.c */.SQLITE
40202 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
40203 69 74 65 33 46 74 73 33 50 75 74 56 61 72 69 6e  ite3Fts3PutVarin
40204 74 28 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65  t(char *, sqlite
40205 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45  3_int64);.SQLITE
40206 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
40207 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e  ite3Fts3GetVarin
40208 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  t(const char *, 
40209 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 29 3b  sqlite_int64 *);
4020a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4020b 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 47  int sqlite3Fts3G
4020c 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74  etVarint32(const
4020d 20 63 68 61 72 20 2a 2c 20 69 6e 74 20 2a 29 3b   char *, int *);
4020e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4020f 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 56  int sqlite3Fts3V
40210 61 72 69 6e 74 4c 65 6e 28 73 71 6c 69 74 65 33  arintLen(sqlite3
40211 5f 75 69 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45  _uint64);.SQLITE
40212 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
40213 6c 69 74 65 33 46 74 73 33 44 65 71 75 6f 74 65  lite3Fts3Dequote
40214 28 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45  (char *);.SQLITE
40215 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
40216 6c 69 74 65 33 46 74 73 33 44 6f 63 6c 69 73 74  lite3Fts3Doclist
40217 50 72 65 76 28 69 6e 74 2c 63 68 61 72 2a 2c 69  Prev(int,char*,i
40218 6e 74 2c 63 68 61 72 2a 2a 2c 73 71 6c 69 74 65  nt,char**,sqlite
40219 33 5f 69 6e 74 36 34 2a 2c 69 6e 74 2a 2c 75 38  3_int64*,int*,u8
4021a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4021b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
4021c 73 33 45 76 61 6c 50 68 72 61 73 65 53 74 61 74  s3EvalPhraseStat
4021d 73 28 46 74 73 33 43 75 72 73 6f 72 20 2a 2c 20  s(Fts3Cursor *, 
4021e 46 74 73 33 45 78 70 72 20 2a 2c 20 75 33 32 20  Fts3Expr *, u32 
4021f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
40220 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
40221 73 33 46 69 72 73 74 46 69 6c 74 65 72 28 73 71  s3FirstFilter(sq
40222 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 63 68 61  lite3_int64, cha
40223 72 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a  r *, int, char *
40224 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
40225 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74  E void sqlite3Ft
40226 73 33 43 72 65 61 74 65 53 74 61 74 54 61 62 6c  s3CreateStatTabl
40227 65 28 69 6e 74 2a 2c 20 46 74 73 33 54 61 62 6c  e(int*, Fts3Tabl
40228 65 2a 29 3b 0a 0a 2f 2a 20 66 74 73 33 5f 74 6f  e*);../* fts3_to
40229 6b 65 6e 69 7a 65 72 2e 63 20 2a 2f 0a 53 51 4c  kenizer.c */.SQL
4022a 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
4022b 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 46  t char *sqlite3F
4022c 74 73 33 4e 65 78 74 54 6f 6b 65 6e 28 63 6f 6e  ts3NextToken(con
4022d 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 20 2a  st char *, int *
4022e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4022f 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
40230 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28 73  3InitHashTable(s
40231 71 6c 69 74 65 33 20 2a 2c 20 46 74 73 33 48 61  qlite3 *, Fts3Ha
40232 73 68 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  sh *, const char
40233 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
40234 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
40235 74 73 33 49 6e 69 74 54 6f 6b 65 6e 69 7a 65 72  ts3InitTokenizer
40236 28 46 74 73 33 48 61 73 68 20 2a 70 48 61 73 68  (Fts3Hash *pHash
40237 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
40238 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  .    sqlite3_tok
40239 65 6e 69 7a 65 72 20 2a 2a 2c 20 63 68 61 72 20  enizer **, char 
4023a 2a 2a 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  **.);.SQLITE_PRI
4023b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4023c 46 74 73 33 49 73 49 64 43 68 61 72 28 63 68 61  Fts3IsIdChar(cha
4023d 72 29 3b 0a 0a 2f 2a 20 66 74 73 33 5f 73 6e 69  r);../* fts3_sni
4023e 70 70 65 74 2e 63 20 2a 2f 0a 53 51 4c 49 54 45  ppet.c */.SQLITE
4023f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
40240 6c 69 74 65 33 46 74 73 33 4f 66 66 73 65 74 73  lite3Fts3Offsets
40241 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
40242 2a 2c 20 46 74 73 33 43 75 72 73 6f 72 2a 29 3b  *, Fts3Cursor*);
40243 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
40244 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
40245 53 6e 69 70 70 65 74 28 73 71 6c 69 74 65 33 5f  Snippet(sqlite3_
40246 63 6f 6e 74 65 78 74 20 2a 2c 20 46 74 73 33 43  context *, Fts3C
40247 75 72 73 6f 72 20 2a 2c 20 63 6f 6e 73 74 20 63  ursor *, const c
40248 68 61 72 20 2a 2c 0a 20 20 63 6f 6e 73 74 20 63  har *,.  const c
40249 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  har *, const cha
4024a 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 0a 29 3b  r *, int, int.);
4024b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4024c 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
4024d 4d 61 74 63 68 69 6e 66 6f 28 73 71 6c 69 74 65  Matchinfo(sqlite
4024e 33 5f 63 6f 6e 74 65 78 74 20 2a 2c 20 46 74 73  3_context *, Fts
4024f 33 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e 73 74  3Cursor *, const
40250 20 63 68 61 72 20 2a 29 3b 0a 0a 2f 2a 20 66 74   char *);../* ft
40251 73 33 5f 65 78 70 72 2e 63 20 2a 2f 0a 53 51 4c  s3_expr.c */.SQL
40252 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
40253 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 50  sqlite3Fts3ExprP
40254 61 72 73 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b  arse(sqlite3_tok
40255 65 6e 69 7a 65 72 20 2a 2c 20 69 6e 74 2c 0a 20  enizer *, int,. 
40256 20 63 68 61 72 20 2a 2a 2c 20 69 6e 74 2c 20 69   char **, int, i
40257 6e 74 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63  nt, int, const c
40258 68 61 72 20 2a 2c 20 69 6e 74 2c 20 46 74 73 33  har *, int, Fts3
40259 45 78 70 72 20 2a 2a 0a 29 3b 0a 53 51 4c 49 54  Expr **.);.SQLIT
4025a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
4025b 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46 72  qlite3Fts3ExprFr
4025c 65 65 28 46 74 73 33 45 78 70 72 20 2a 29 3b 0a  ee(Fts3Expr *);.
4025d 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
4025e 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ST.SQLITE_PRIVAT
4025f 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
40260 33 45 78 70 72 49 6e 69 74 54 65 73 74 49 6e 74  3ExprInitTestInt
40261 65 72 66 61 63 65 28 73 71 6c 69 74 65 33 20 2a  erface(sqlite3 *
40262 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  db);.SQLITE_PRIV
40263 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
40264 74 73 33 49 6e 69 74 54 65 72 6d 28 73 71 6c 69  ts3InitTerm(sqli
40265 74 65 33 20 2a 64 62 29 3b 0a 23 65 6e 64 69 66  te3 *db);.#endif
40266 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
40267 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
40268 4f 70 65 6e 54 6f 6b 65 6e 69 7a 65 72 28 73 71  OpenTokenizer(sq
40269 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
4026a 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  *, int, const ch
4026b 61 72 20 2a 2c 20 69 6e 74 2c 0a 20 20 73 71 6c  ar *, int,.  sql
4026c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
4026d 75 72 73 6f 72 20 2a 2a 0a 29 3b 0a 0a 2f 2a 20  ursor **.);../* 
4026e 66 74 73 33 5f 61 75 78 2e 63 20 2a 2f 0a 53 51  fts3_aux.c */.SQ
4026f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
40270 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74   sqlite3Fts3Init
40271 41 75 78 28 73 71 6c 69 74 65 33 20 2a 64 62 29  Aux(sqlite3 *db)
40272 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
40273 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74  E void sqlite3Ft
40274 73 33 45 76 61 6c 50 68 72 61 73 65 43 6c 65 61  s3EvalPhraseClea
40275 6e 75 70 28 46 74 73 33 50 68 72 61 73 65 20 2a  nup(Fts3Phrase *
40276 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
40277 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
40278 73 33 4d 73 72 49 6e 63 72 53 74 61 72 74 28 0a  s3MsrIncrStart(.
40279 20 20 20 20 46 74 73 33 54 61 62 6c 65 2a 2c 20      Fts3Table*, 
4027a 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64  Fts3MultiSegRead
4027b 65 72 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  er*, int, const 
4027c 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  char*, int);.SQL
4027d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4027e 73 71 6c 69 74 65 33 46 74 73 33 4d 73 72 49 6e  sqlite3Fts3MsrIn
4027f 63 72 4e 65 78 74 28 0a 20 20 20 20 46 74 73 33  crNext(.    Fts3
40280 54 61 62 6c 65 20 2a 2c 20 46 74 73 33 4d 75 6c  Table *, Fts3Mul
40281 74 69 53 65 67 52 65 61 64 65 72 20 2a 2c 20 73  tiSegReader *, s
40282 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 2c 20  qlite3_int64 *, 
40283 63 68 61 72 20 2a 2a 2c 20 69 6e 74 20 2a 29 3b  char **, int *);
40284 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
40285 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 45  int sqlite3Fts3E
40286 76 61 6c 50 68 72 61 73 65 50 6f 73 6c 69 73 74  valPhrasePoslist
40287 28 46 74 73 33 43 75 72 73 6f 72 20 2a 2c 20 46  (Fts3Cursor *, F
40288 74 73 33 45 78 70 72 20 2a 2c 20 69 6e 74 20 69  ts3Expr *, int i
40289 43 6f 6c 2c 20 63 68 61 72 20 2a 2a 29 3b 20 0a  Col, char **); .
4028a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4028b 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 4d 73  nt sqlite3Fts3Ms
4028c 72 4f 76 66 6c 28 46 74 73 33 43 75 72 73 6f 72  rOvfl(Fts3Cursor
4028d 20 2a 2c 20 46 74 73 33 4d 75 6c 74 69 53 65 67   *, Fts3MultiSeg
4028e 52 65 61 64 65 72 20 2a 2c 20 69 6e 74 20 2a 29  Reader *, int *)
4028f 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
40290 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
40291 4d 73 72 49 6e 63 72 52 65 73 74 61 72 74 28 46  MsrIncrRestart(F
40292 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65  ts3MultiSegReade
40293 72 20 2a 70 43 73 72 29 3b 0a 0a 2f 2a 20 66 74  r *pCsr);../* ft
40294 73 33 5f 75 6e 69 63 6f 64 65 32 2e 63 20 28 66  s3_unicode2.c (f
40295 75 6e 63 74 69 6f 6e 73 20 67 65 6e 65 72 61 74  unctions generat
40296 65 64 20 62 79 20 70 61 72 73 69 6e 67 20 75 6e  ed by parsing un
40297 69 63 6f 64 65 20 74 65 78 74 20 66 69 6c 65 73  icode text files
40298 29 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ) */.#ifdef SQLI
40299 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 34 5f 55  TE_ENABLE_FTS4_U
4029a 4e 49 43 4f 44 45 36 31 0a 53 51 4c 49 54 45 5f  NICODE61.SQLITE_
4029b 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4029c 74 65 33 46 74 73 55 6e 69 63 6f 64 65 46 6f 6c  te3FtsUnicodeFol
4029d 64 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  d(int, int);.SQL
4029e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4029f 73 71 6c 69 74 65 33 46 74 73 55 6e 69 63 6f 64  sqlite3FtsUnicod
402a0 65 49 73 61 6c 6e 75 6d 28 69 6e 74 29 3b 0a 53  eIsalnum(int);.S
402a1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
402a2 74 20 73 71 6c 69 74 65 33 46 74 73 55 6e 69 63  t sqlite3FtsUnic
402a3 6f 64 65 49 73 64 69 61 63 72 69 74 69 63 28 69  odeIsdiacritic(i
402a4 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e  nt);.#endif..#en
402a5 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 43  dif /* !SQLITE_C
402a6 4f 52 45 20 7c 7c 20 53 51 4c 49 54 45 5f 45 4e  ORE || SQLITE_EN
402a7 41 42 4c 45 5f 46 54 53 33 20 2a 2f 0a 23 65 6e  ABLE_FTS3 */.#en
402a8 64 69 66 20 2f 2a 20 5f 46 54 53 49 4e 54 5f 48  dif /* _FTSINT_H
402a9 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
402aa 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33  **** End of fts3
402ab 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
402ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
402ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
402ae 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
402af 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
402b0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
402b1 66 20 69 6e 20 66 74 73 33 2e 63 20 2a 2a 2a 2a  f in fts3.c ****
402b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
402b3 2a 2a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  ***/.#if !define
402b4 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
402b5 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
402b6 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 23  _ENABLE_FTS3)..#
402b7 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
402b8 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 26  E_ENABLE_FTS3) &
402b9 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
402ba 45 5f 43 4f 52 45 29 0a 23 20 64 65 66 69 6e 65  E_CORE).# define
402bb 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31 0a 23   SQLITE_CORE 1.#
402bc 65 6e 64 69 66 0a 0a 2f 2a 20 23 69 6e 63 6c 75  endif../* #inclu
402bd 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a 2f  de <assert.h> */
402be 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74  ./* #include <st
402bf 64 6c 69 62 2e 68 3e 20 2a 2f 0a 2f 2a 20 23 69  dlib.h> */./* #i
402c0 6e 63 6c 75 64 65 20 3c 73 74 64 64 65 66 2e 68  nclude <stddef.h
402c1 3e 20 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65  > */./* #include
402c2 20 3c 73 74 64 69 6f 2e 68 3e 20 2a 2f 0a 2f 2a   <stdio.h> */./*
402c3 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e   #include <strin
402c4 67 2e 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e 63 6c  g.h> */./* #incl
402c5 75 64 65 20 3c 73 74 64 61 72 67 2e 68 3e 20 2a  ude <stdarg.h> *
402c6 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
402c7 45 5f 43 4f 52 45 20 0a 20 20 53 51 4c 49 54 45  E_CORE .  SQLITE
402c8 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31  _EXTENSION_INIT1
402c9 0a 23 65 6e 64 69 66 0a 0a 73 74 61 74 69 63 20  .#endif..static 
402ca 69 6e 74 20 66 74 73 33 45 76 61 6c 4e 65 78 74  int fts3EvalNext
402cb 28 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73  (Fts3Cursor *pCs
402cc 72 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 66  r);.static int f
402cd 74 73 33 45 76 61 6c 53 74 61 72 74 28 46 74 73  ts3EvalStart(Fts
402ce 33 43 75 72 73 6f 72 20 2a 70 43 73 72 29 3b 0a  3Cursor *pCsr);.
402cf 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 54  static int fts3T
402d0 65 72 6d 53 65 67 52 65 61 64 65 72 43 75 72 73  ermSegReaderCurs
402d1 6f 72 28 0a 20 20 20 20 46 74 73 33 43 75 72 73  or(.    Fts3Curs
402d2 6f 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  or *, const char
402d3 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 46 74   *, int, int, Ft
402d4 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72  s3MultiSegReader
402d5 20 2a 2a 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 57 72   **);../* .** Wr
402d6 69 74 65 20 61 20 36 34 2d 62 69 74 20 76 61 72  ite a 64-bit var
402d7 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74  iable-length int
402d8 65 67 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73  eger to memory s
402d9 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e  tarting at p[0].
402da 0a 2a 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f  .** The length o
402db 66 20 64 61 74 61 20 77 72 69 74 74 65 6e 20 77  f data written w
402dc 69 6c 6c 20 62 65 20 62 65 74 77 65 65 6e 20 31  ill be between 1
402dd 20 61 6e 64 20 46 54 53 33 5f 56 41 52 49 4e 54   and FTS3_VARINT
402de 5f 4d 41 58 20 62 79 74 65 73 2e 0a 2a 2a 20 54  _MAX bytes..** T
402df 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
402e0 65 73 20 77 72 69 74 74 65 6e 20 69 73 20 72 65  es written is re
402e1 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  turned..*/.SQLIT
402e2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
402e3 6c 69 74 65 33 46 74 73 33 50 75 74 56 61 72 69  lite3Fts3PutVari
402e4 6e 74 28 63 68 61 72 20 2a 70 2c 20 73 71 6c 69  nt(char *p, sqli
402e5 74 65 5f 69 6e 74 36 34 20 76 29 7b 0a 20 20 75  te_int64 v){.  u
402e6 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 71 20  nsigned char *q 
402e7 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
402e8 20 2a 29 20 70 3b 0a 20 20 73 71 6c 69 74 65 5f   *) p;.  sqlite_
402e9 75 69 6e 74 36 34 20 76 75 20 3d 20 76 3b 0a 20  uint64 vu = v;. 
402ea 20 64 6f 7b 0a 20 20 20 20 2a 71 2b 2b 20 3d 20   do{.    *q++ = 
402eb 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 20  (unsigned char) 
402ec 28 28 76 75 20 26 20 30 78 37 66 29 20 7c 20 30  ((vu & 0x7f) | 0
402ed 78 38 30 29 3b 0a 20 20 20 20 76 75 20 3e 3e 3d  x80);.    vu >>=
402ee 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 75   7;.  }while( vu
402ef 21 3d 30 20 29 3b 0a 20 20 71 5b 2d 31 5d 20 26  !=0 );.  q[-1] &
402f0 3d 20 30 78 37 66 3b 20 20 2f 2a 20 74 75 72 6e  = 0x7f;  /* turn
402f1 20 6f 66 66 20 68 69 67 68 20 62 69 74 20 69 6e   off high bit in
402f2 20 66 69 6e 61 6c 20 62 79 74 65 20 2a 2f 0a 20   final byte */. 
402f3 20 61 73 73 65 72 74 28 20 71 20 2d 20 28 75 6e   assert( q - (un
402f4 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 20  signed char *)p 
402f5 3c 3d 20 46 54 53 33 5f 56 41 52 49 4e 54 5f 4d  <= FTS3_VARINT_M
402f6 41 58 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  AX );.  return (
402f7 69 6e 74 29 20 28 71 20 2d 20 28 75 6e 73 69 67  int) (q - (unsig
402f8 6e 65 64 20 63 68 61 72 20 2a 29 70 29 3b 0a 7d  ned char *)p);.}
402f9 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 61 64 20 61 20  ../* .** Read a 
402fa 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d  64-bit variable-
402fb 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 66  length integer f
402fc 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 72 74  rom memory start
402fd 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20  ing at p[0]..** 
402fe 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
402ff 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 2c  r of bytes read,
40300 20 6f 72 20 30 20 6f 6e 20 65 72 72 6f 72 2e 0a   or 0 on error..
40301 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 69 73 20  ** The value is 
40302 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a 2f  stored in *v..*/
40303 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
40304 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 47  int sqlite3Fts3G
40305 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74 20 63  etVarint(const c
40306 68 61 72 20 2a 70 2c 20 73 71 6c 69 74 65 5f 69  har *p, sqlite_i
40307 6e 74 36 34 20 2a 76 29 7b 0a 20 20 63 6f 6e 73  nt64 *v){.  cons
40308 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
40309 2a 71 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69  *q = (const unsi
4030a 67 6e 65 64 20 63 68 61 72 20 2a 29 20 70 3b 0a  gned char *) p;.
4030b 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20    sqlite_uint64 
4030c 78 20 3d 20 30 2c 20 79 20 3d 20 31 3b 0a 20 20  x = 0, y = 1;.  
4030d 77 68 69 6c 65 28 20 28 2a 71 26 30 78 38 30 29  while( (*q&0x80)
4030e 3d 3d 30 78 38 30 20 26 26 20 71 2d 28 75 6e 73  ==0x80 && q-(uns
4030f 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 3c 46  igned char *)p<F
40310 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58 20 29  TS3_VARINT_MAX )
40311 7b 0a 20 20 20 20 78 20 2b 3d 20 79 20 2a 20 28  {.    x += y * (
40312 2a 71 2b 2b 20 26 20 30 78 37 66 29 3b 0a 20 20  *q++ & 0x7f);.  
40313 20 20 79 20 3c 3c 3d 20 37 3b 0a 20 20 7d 0a 20    y <<= 7;.  }. 
40314 20 78 20 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b 29   x += y * (*q++)
40315 3b 0a 20 20 2a 76 20 3d 20 28 73 71 6c 69 74 65  ;.  *v = (sqlite
40316 5f 69 6e 74 36 34 29 20 78 3b 0a 20 20 72 65 74  _int64) x;.  ret
40317 75 72 6e 20 28 69 6e 74 29 20 28 71 20 2d 20 28  urn (int) (q - (
40318 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
40319 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 6d  p);.}../*.** Sim
4031a 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33 46  ilar to sqlite3F
4031b 74 73 33 47 65 74 56 61 72 69 6e 74 28 29 2c 20  ts3GetVarint(), 
4031c 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20  except that the 
4031d 6f 75 74 70 75 74 20 69 73 20 74 72 75 6e 63 61  output is trunca
4031e 74 65 64 20 74 6f 20 61 0a 2a 2a 20 33 32 2d 62  ted to a.** 32-b
4031f 69 74 20 69 6e 74 65 67 65 72 20 62 65 66 6f 72  it integer befor
40320 65 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64  e it is returned
40321 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
40322 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
40323 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 63  ts3GetVarint32(c
40324 6f 6e 73 74 20 63 68 61 72 20 2a 70 2c 20 69 6e  onst char *p, in
40325 74 20 2a 70 69 29 7b 0a 20 73 71 6c 69 74 65 5f  t *pi){. sqlite_
40326 69 6e 74 36 34 20 69 3b 0a 20 69 6e 74 20 72 65  int64 i;. int re
40327 74 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47  t = sqlite3Fts3G
40328 65 74 56 61 72 69 6e 74 28 70 2c 20 26 69 29 3b  etVarint(p, &i);
40329 0a 20 2a 70 69 20 3d 20 28 69 6e 74 29 20 69 3b  . *pi = (int) i;
4032a 0a 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a  . return ret;.}.
4032b 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
4032c 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
4032d 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 6e  s required to en
4032e 63 6f 64 65 20 76 20 61 73 20 61 20 76 61 72 69  code v as a vari
4032f 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  nt.*/.SQLITE_PRI
40330 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
40331 46 74 73 33 56 61 72 69 6e 74 4c 65 6e 28 73 71  Fts3VarintLen(sq
40332 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 76 29 7b  lite3_uint64 v){
40333 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20  .  int i = 0;.  
40334 64 6f 7b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20  do{.    i++;.   
40335 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69   v >>= 7;.  }whi
40336 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 20 72 65  le( v!=0 );.  re
40337 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn i;.}../*.**
40338 20 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d   Convert an SQL-
40339 73 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72  style quoted str
4033a 69 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61  ing into a norma
4033b 6c 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f  l string by remo
4033c 76 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74  ving.** the quot
4033d 65 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 54  e characters.  T
4033e 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73  he conversion is
4033f 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20   done in-place. 
40340 20 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74   If the.** input
40341 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20   does not begin 
40342 77 69 74 68 20 61 20 71 75 6f 74 65 20 63 68 61  with a quote cha
40343 72 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69  racter, then thi
40344 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20  s routine.** is 
40345 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45  a no-op..**.** E
40346 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  xamples:.**.**  
40347 20 20 20 22 61 62 63 22 20 20 20 62 65 63 6f 6d     "abc"   becom
40348 65 73 20 20 20 61 62 63 0a 2a 2a 20 20 20 20 20  es   abc.**     
40349 27 78 79 7a 27 20 20 20 62 65 63 6f 6d 65 73 20  'xyz'   becomes 
4034a 20 20 78 79 7a 0a 2a 2a 20 20 20 20 20 5b 70 71    xyz.**     [pq
4034b 72 5d 20 20 20 62 65 63 6f 6d 65 73 20 20 20 70  r]   becomes   p
4034c 71 72 0a 2a 2a 20 20 20 20 20 60 6d 6e 6f 60 20  qr.**     `mno` 
4034d 20 20 62 65 63 6f 6d 65 73 20 20 20 6d 6e 6f 0a    becomes   mno.
4034e 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  **.*/.SQLITE_PRI
4034f 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
40350 33 46 74 73 33 44 65 71 75 6f 74 65 28 63 68 61  3Fts3Dequote(cha
40351 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20 71 75  r *z){.  char qu
40352 6f 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ote;            
40353 20 20 20 20 20 20 20 20 20 2f 2a 20 51 75 6f 74           /* Quot
40354 65 20 63 68 61 72 61 63 74 65 72 20 28 69 66 20  e character (if 
40355 61 6e 79 20 29 20 2a 2f 0a 0a 20 20 71 75 6f 74  any ) */..  quot
40356 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 69 66 28 20  e = z[0];.  if( 
40357 71 75 6f 74 65 3d 3d 27 5b 27 20 7c 7c 20 71 75  quote=='[' || qu
40358 6f 74 65 3d 3d 27 5c 27 27 20 7c 7c 20 71 75 6f  ote=='\'' || quo
40359 74 65 3d 3d 27 22 27 20 7c 7c 20 71 75 6f 74 65  te=='"' || quote
4035a 3d 3d 27 60 27 20 29 7b 0a 20 20 20 20 69 6e 74  =='`' ){.    int
4035b 20 69 49 6e 20 3d 20 31 3b 20 20 20 20 20 20 20   iIn = 1;       
4035c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
4035d 64 65 78 20 6f 66 20 6e 65 78 74 20 62 79 74 65  dex of next byte
4035e 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 69 6e   to read from in
4035f 70 75 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  put */.    int i
40360 4f 75 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Out = 0;        
40361 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
40362 78 20 6f 66 20 6e 65 78 74 20 62 79 74 65 20 74  x of next byte t
40363 6f 20 77 72 69 74 65 20 74 6f 20 6f 75 74 70 75  o write to outpu
40364 74 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20  t */..    /* If 
40365 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 77  the first byte w
40366 61 73 20 61 20 27 5b 27 2c 20 74 68 65 6e 20 74  as a '[', then t
40367 68 65 20 63 6c 6f 73 65 2d 71 75 6f 74 65 20 63  he close-quote c
40368 68 61 72 61 63 74 65 72 20 69 73 20 61 20 27 5d  haracter is a ']
40369 27 20 2a 2f 0a 20 20 20 20 69 66 28 20 71 75 6f  ' */.    if( quo
4036a 74 65 3d 3d 27 5b 27 20 29 20 71 75 6f 74 65 20  te=='[' ) quote 
4036b 3d 20 27 5d 27 3b 20 20 0a 0a 20 20 20 20 77 68  = ']';  ..    wh
4036c 69 6c 65 28 20 41 4c 57 41 59 53 28 7a 5b 69 49  ile( ALWAYS(z[iI
4036d 6e 5d 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28  n]) ){.      if(
4036e 20 7a 5b 69 49 6e 5d 3d 3d 71 75 6f 74 65 20 29   z[iIn]==quote )
4036f 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b  {.        if( z[
40370 69 49 6e 2b 31 5d 21 3d 71 75 6f 74 65 20 29 20  iIn+1]!=quote ) 
40371 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7a  break;.        z
40372 5b 69 4f 75 74 2b 2b 5d 20 3d 20 71 75 6f 74 65  [iOut++] = quote
40373 3b 0a 20 20 20 20 20 20 20 20 69 49 6e 20 2b 3d  ;.        iIn +=
40374 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   2;.      }else{
40375 0a 20 20 20 20 20 20 20 20 7a 5b 69 4f 75 74 2b  .        z[iOut+
40376 2b 5d 20 3d 20 7a 5b 69 49 6e 2b 2b 5d 3b 0a 20  +] = z[iIn++];. 
40377 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
40378 20 7a 5b 69 4f 75 74 5d 20 3d 20 27 5c 30 27 3b   z[iOut] = '\0';
40379 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
4037a 61 64 20 61 20 73 69 6e 67 6c 65 20 76 61 72 69  ad a single vari
4037b 6e 74 20 66 72 6f 6d 20 74 68 65 20 64 6f 63 6c  nt from the docl
4037c 69 73 74 20 61 74 20 2a 70 70 20 61 6e 64 20 61  ist at *pp and a
4037d 64 76 61 6e 63 65 20 2a 70 70 20 74 6f 20 70 6f  dvance *pp to po
4037e 69 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69  int.** to the fi
4037f 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68  rst byte past th
40380 65 20 65 6e 64 20 6f 66 20 74 68 65 20 76 61 72  e end of the var
40381 69 6e 74 2e 20 20 41 64 64 20 74 68 65 20 76 61  int.  Add the va
40382 6c 75 65 20 6f 66 20 74 68 65 20 76 61 72 69 6e  lue of the varin
40383 74 0a 2a 2a 20 74 6f 20 2a 70 56 61 6c 2e 0a 2a  t.** to *pVal..*
40384 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  /.static void ft
40385 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74  s3GetDeltaVarint
40386 28 63 68 61 72 20 2a 2a 70 70 2c 20 73 71 6c 69  (char **pp, sqli
40387 74 65 33 5f 69 6e 74 36 34 20 2a 70 56 61 6c 29  te3_int64 *pVal)
40388 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  {.  sqlite3_int6
40389 34 20 69 56 61 6c 3b 0a 20 20 2a 70 70 20 2b 3d  4 iVal;.  *pp +=
4038a 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56   sqlite3Fts3GetV
4038b 61 72 69 6e 74 28 2a 70 70 2c 20 26 69 56 61 6c  arint(*pp, &iVal
4038c 29 3b 0a 20 20 2a 70 56 61 6c 20 2b 3d 20 69 56  );.  *pVal += iV
4038d 61 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65  al;.}../*.** Whe
4038e 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
4038f 69 73 20 63 61 6c 6c 65 64 2c 20 2a 70 70 20 70  is called, *pp p
40390 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72  oints to the fir
40391 73 74 20 62 79 74 65 20 66 6f 6c 6c 6f 77 69 6e  st byte followin
40392 67 20 61 0a 2a 2a 20 76 61 72 69 6e 74 20 74 68  g a.** varint th
40393 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 20  at is part of a 
40394 64 6f 63 6c 69 73 74 20 28 6f 72 20 70 6f 73 69  doclist (or posi
40395 74 69 6f 6e 2d 6c 69 73 74 2c 20 6f 72 20 61 6e  tion-list, or an
40396 79 20 6f 74 68 65 72 20 6c 69 73 74 0a 2a 2a 20  y other list.** 
40397 6f 66 20 76 61 72 69 6e 74 73 29 2e 20 54 68 69  of varints). Thi
40398 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f 76 65 73  s function moves
40399 20 2a 70 70 20 74 6f 20 70 6f 69 6e 74 20 74 6f   *pp to point to
4039a 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
4039b 61 74 20 76 61 72 69 6e 74 2c 0a 2a 2a 20 61 6e  at varint,.** an
4039c 64 20 73 65 74 73 20 2a 70 56 61 6c 20 62 79 20  d sets *pVal by 
4039d 74 68 65 20 76 61 72 69 6e 74 20 76 61 6c 75 65  the varint value
4039e 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ..**.** Argument
4039f 20 70 53 74 61 72 74 20 70 6f 69 6e 74 73 20 74   pStart points t
403a0 6f 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  o the first byte
403a1 20 6f 66 20 74 68 65 20 64 6f 63 6c 69 73 74 20   of the doclist 
403a2 74 68 61 74 20 74 68 65 0a 2a 2a 20 76 61 72 69  that the.** vari
403a3 6e 74 20 69 73 20 70 61 72 74 20 6f 66 2e 0a 2a  nt is part of..*
403a4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  /.static void ft
403a5 73 33 47 65 74 52 65 76 65 72 73 65 56 61 72 69  s3GetReverseVari
403a6 6e 74 28 0a 20 20 63 68 61 72 20 2a 2a 70 70 2c  nt(.  char **pp,
403a7 20 0a 20 20 63 68 61 72 20 2a 70 53 74 61 72 74   .  char *pStart
403a8 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  , .  sqlite3_int
403a9 36 34 20 2a 70 56 61 6c 0a 29 7b 0a 20 20 73 71  64 *pVal.){.  sq
403aa 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 56 61 6c  lite3_int64 iVal
403ab 3b 0a 20 20 63 68 61 72 20 2a 70 3b 0a 0a 20 20  ;.  char *p;..  
403ac 2f 2a 20 50 6f 69 6e 74 65 72 20 70 20 6e 6f 77  /* Pointer p now
403ad 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 66   points at the f
403ae 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74  irst byte past t
403af 68 65 20 76 61 72 69 6e 74 20 77 65 20 61 72 65  he varint we are
403b0 20 0a 20 20 2a 2a 20 69 6e 74 65 72 65 73 74 65   .  ** intereste
403b1 64 20 69 6e 2e 20 53 6f 2c 20 75 6e 6c 65 73 73  d in. So, unless
403b2 20 74 68 65 20 64 6f 63 6c 69 73 74 20 69 73 20   the doclist is 
403b3 63 6f 72 72 75 70 74 2c 20 74 68 65 20 30 78 38  corrupt, the 0x8
403b4 30 20 62 69 74 20 69 73 0a 20 20 2a 2a 20 63 6c  0 bit is.  ** cl
403b5 65 61 72 20 6f 6e 20 63 68 61 72 61 63 74 65 72  ear on character
403b6 20 70 5b 2d 31 5d 2e 20 2a 2f 0a 20 20 66 6f 72   p[-1]. */.  for
403b7 28 70 20 3d 20 28 2a 70 70 29 2d 32 3b 20 70 3e  (p = (*pp)-2; p>
403b8 3d 70 53 74 61 72 74 20 26 26 20 2a 70 26 30 78  =pStart && *p&0x
403b9 38 30 3b 20 70 2d 2d 29 3b 0a 20 20 70 2b 2b 3b  80; p--);.  p++;
403ba 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 0a 20 20 73  .  *pp = p;..  s
403bb 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61 72  qlite3Fts3GetVar
403bc 69 6e 74 28 70 2c 20 26 69 56 61 6c 29 3b 0a 20  int(p, &iVal);. 
403bd 20 2a 70 56 61 6c 20 3d 20 69 56 61 6c 3b 0a 7d   *pVal = iVal;.}
403be 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 44 69 73  ../*.** The xDis
403bf 63 6f 6e 6e 65 63 74 28 29 20 76 69 72 74 75 61  connect() virtua
403c0 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 2e 0a  l table method..
403c1 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
403c2 73 33 44 69 73 63 6f 6e 6e 65 63 74 4d 65 74 68  s3DisconnectMeth
403c3 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  od(sqlite3_vtab 
403c4 2a 70 56 74 61 62 29 7b 0a 20 20 46 74 73 33 54  *pVtab){.  Fts3T
403c5 61 62 6c 65 20 2a 70 20 3d 20 28 46 74 73 33 54  able *p = (Fts3T
403c6 61 62 6c 65 20 2a 29 70 56 74 61 62 3b 0a 20 20  able *)pVtab;.  
403c7 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74  int i;..  assert
403c8 28 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74  ( p->nPendingDat
403c9 61 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  a==0 );.  assert
403ca 28 20 70 2d 3e 70 53 65 67 6d 65 6e 74 73 3d 3d  ( p->pSegments==
403cb 30 20 29 3b 0a 0a 20 20 2f 2a 20 46 72 65 65 20  0 );..  /* Free 
403cc 61 6e 79 20 70 72 65 70 61 72 65 64 20 73 74 61  any prepared sta
403cd 74 65 6d 65 6e 74 73 20 68 65 6c 64 20 2a 2f 0a  tements held */.
403ce 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 53 69 7a    for(i=0; i<Siz
403cf 65 6f 66 41 72 72 61 79 28 70 2d 3e 61 53 74 6d  eofArray(p->aStm
403d0 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  t); i++){.    sq
403d1 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70  lite3_finalize(p
403d2 2d 3e 61 53 74 6d 74 5b 69 5d 29 3b 0a 20 20 7d  ->aStmt[i]);.  }
403d3 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
403d4 70 2d 3e 7a 53 65 67 6d 65 6e 74 73 54 62 6c 29  p->zSegmentsTbl)
403d5 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
403d6 28 70 2d 3e 7a 52 65 61 64 45 78 70 72 6c 69 73  (p->zReadExprlis
403d7 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  t);.  sqlite3_fr
403d8 65 65 28 70 2d 3e 7a 57 72 69 74 65 45 78 70 72  ee(p->zWriteExpr
403d9 6c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  list);.  sqlite3
403da 5f 66 72 65 65 28 70 2d 3e 7a 43 6f 6e 74 65 6e  _free(p->zConten
403db 74 54 62 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33  tTbl);.  sqlite3
403dc 5f 66 72 65 65 28 70 2d 3e 7a 4c 61 6e 67 75 61  _free(p->zLangua
403dd 67 65 69 64 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76  geid);..  /* Inv
403de 6f 6b 65 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65  oke the tokenize
403df 72 20 64 65 73 74 72 75 63 74 6f 72 20 74 6f 20  r destructor to 
403e0 66 72 65 65 20 74 68 65 20 74 6f 6b 65 6e 69 7a  free the tokeniz
403e1 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 70 54 6f 6b  er. */.  p->pTok
403e2 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d  enizer->pModule-
403e3 3e 78 44 65 73 74 72 6f 79 28 70 2d 3e 70 54 6f  >xDestroy(p->pTo
403e4 6b 65 6e 69 7a 65 72 29 3b 0a 0a 20 20 73 71 6c  kenizer);..  sql
403e5 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
403e6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
403e7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74  ;.}../*.** Const
403e8 72 75 63 74 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  ruct one or more
403e9 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
403ea 66 72 6f 6d 20 74 68 65 20 66 6f 72 6d 61 74 20  from the format 
403eb 73 74 72 69 6e 67 20 67 69 76 65 6e 0a 2a 2a 20  string given.** 
403ec 61 6e 64 20 74 68 65 6e 20 65 76 61 6c 75 61 74  and then evaluat
403ed 65 20 74 68 6f 73 65 20 73 74 61 74 65 6d 65 6e  e those statemen
403ee 74 73 2e 20 54 68 65 20 73 75 63 63 65 73 73 20  ts. The success 
403ef 63 6f 64 65 20 69 73 20 77 72 69 74 74 65 6e 0a  code is written.
403f0 2a 2a 20 69 6e 74 6f 20 2a 70 52 63 2e 0a 2a 2a  ** into *pRc..**
403f1 0a 2a 2a 20 49 66 20 2a 70 52 63 20 69 73 20 69  .** If *pRc is i
403f2 6e 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72  nitially non-zer
403f3 6f 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  o then this rout
403f4 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
403f5 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
403f6 74 73 33 44 62 45 78 65 63 28 0a 20 20 69 6e 74  ts3DbExec(.  int
403f7 20 2a 70 52 63 2c 20 20 20 20 20 20 20 20 20 20   *pRc,          
403f8 20 20 20 20 2f 2a 20 53 75 63 63 65 73 73 20 63      /* Success c
403f9 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ode */.  sqlite3
403fa 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
403fb 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e 20 77  /* Database in w
403fc 68 69 63 68 20 74 6f 20 72 75 6e 20 53 51 4c 20  hich to run SQL 
403fd 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
403fe 2a 7a 46 6f 72 6d 61 74 2c 20 20 20 2f 2a 20 46  *zFormat,   /* F
403ff 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 66 6f 72  ormat string for
40400 20 53 51 4c 20 2a 2f 0a 20 20 2e 2e 2e 20 20 20   SQL */.  ...   
40401 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40402 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 74 6f   /* Arguments to
40403 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69   the format stri
40404 6e 67 20 2a 2f 0a 29 7b 0a 20 20 76 61 5f 6c 69  ng */.){.  va_li
40405 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a  st ap;.  char *z
40406 53 71 6c 3b 0a 20 20 69 66 28 20 2a 70 52 63 20  Sql;.  if( *pRc 
40407 29 20 72 65 74 75 72 6e 3b 0a 20 20 76 61 5f 73  ) return;.  va_s
40408 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74  tart(ap, zFormat
40409 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69  );.  zSql = sqli
4040a 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 7a 46 6f  te3_vmprintf(zFo
4040b 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
4040c 65 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 7a  end(ap);.  if( z
4040d 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70  Sql==0 ){.    *p
4040e 52 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  Rc = SQLITE_NOME
4040f 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  M;.  }else{.    
40410 2a 70 52 63 20 3d 20 73 71 6c 69 74 65 33 5f 65  *pRc = sqlite3_e
40411 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 30 2c  xec(db, zSql, 0,
40412 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
40413 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a  te3_free(zSql);.
40414 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
40415 20 78 44 65 73 74 72 6f 79 28 29 20 76 69 72 74   xDestroy() virt
40416 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64  ual table method
40417 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
40418 66 74 73 33 44 65 73 74 72 6f 79 4d 65 74 68 6f  fts3DestroyMetho
40419 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  d(sqlite3_vtab *
4041a 70 56 74 61 62 29 7b 0a 20 20 46 74 73 33 54 61  pVtab){.  Fts3Ta
4041b 62 6c 65 20 2a 70 20 3d 20 28 46 74 73 33 54 61  ble *p = (Fts3Ta
4041c 62 6c 65 20 2a 29 70 56 74 61 62 3b 0a 20 20 69  ble *)pVtab;.  i
4041d 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
4041e 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  K;              
4041f 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
40420 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
40421 7a 44 62 20 3d 20 70 2d 3e 7a 44 62 3b 20 20 20  zDb = p->zDb;   
40422 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
40423 64 61 74 61 62 61 73 65 20 28 65 2e 67 2e 20 22  database (e.g. "
40424 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22 29 20 2a  main", "temp") *
40425 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
40426 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20 20 20 20  = p->db;        
40427 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
40428 20 68 61 6e 64 6c 65 20 2a 2f 0a 0a 20 20 2f 2a   handle */..  /*
40429 20 44 72 6f 70 20 74 68 65 20 73 68 61 64 6f 77   Drop the shadow
4042a 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 69 66 28   tables */.  if(
4042b 20 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c 3d   p->zContentTbl=
4042c 3d 30 20 29 7b 0a 20 20 20 20 66 74 73 33 44 62  =0 ){.    fts3Db
4042d 45 78 65 63 28 26 72 63 2c 20 64 62 2c 20 22 44  Exec(&rc, db, "D
4042e 52 4f 50 20 54 41 42 4c 45 20 49 46 20 45 58 49  ROP TABLE IF EXI
4042f 53 54 53 20 25 51 2e 27 25 71 5f 63 6f 6e 74 65  STS %Q.'%q_conte
40430 6e 74 27 22 2c 20 7a 44 62 2c 20 70 2d 3e 7a 4e  nt'", zDb, p->zN
40431 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 66 74 73 33  ame);.  }.  fts3
40432 44 62 45 78 65 63 28 26 72 63 2c 20 64 62 2c 20  DbExec(&rc, db, 
40433 22 44 52 4f 50 20 54 41 42 4c 45 20 49 46 20 45  "DROP TABLE IF E
40434 58 49 53 54 53 20 25 51 2e 27 25 71 5f 73 65 67  XISTS %Q.'%q_seg
40435 6d 65 6e 74 73 27 22 2c 20 7a 44 62 2c 70 2d 3e  ments'", zDb,p->
40436 7a 4e 61 6d 65 29 3b 0a 20 20 66 74 73 33 44 62  zName);.  fts3Db
40437 45 78 65 63 28 26 72 63 2c 20 64 62 2c 20 22 44  Exec(&rc, db, "D
40438 52 4f 50 20 54 41 42 4c 45 20 49 46 20 45 58 49  ROP TABLE IF EXI
40439 53 54 53 20 25 51 2e 27 25 71 5f 73 65 67 64 69  STS %Q.'%q_segdi
4043a 72 27 22 2c 20 7a 44 62 2c 20 70 2d 3e 7a 4e 61  r'", zDb, p->zNa
4043b 6d 65 29 3b 0a 20 20 66 74 73 33 44 62 45 78 65  me);.  fts3DbExe
4043c 63 28 26 72 63 2c 20 64 62 2c 20 22 44 52 4f 50  c(&rc, db, "DROP
4043d 20 54 41 42 4c 45 20 49 46 20 45 58 49 53 54 53   TABLE IF EXISTS
4043e 20 25 51 2e 27 25 71 5f 64 6f 63 73 69 7a 65 27   %Q.'%q_docsize'
4043f 22 2c 20 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65  ", zDb, p->zName
40440 29 3b 0a 20 20 66 74 73 33 44 62 45 78 65 63 28  );.  fts3DbExec(
40441 26 72 63 2c 20 64 62 2c 20 22 44 52 4f 50 20 54  &rc, db, "DROP T
40442 41 42 4c 45 20 49 46 20 45 58 49 53 54 53 20 25  ABLE IF EXISTS %
40443 51 2e 27 25 71 5f 73 74 61 74 27 22 2c 20 7a 44  Q.'%q_stat'", zD
40444 62 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20  b, p->zName);.. 
40445 20 2f 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e   /* If everythin
40446 67 20 68 61 73 20 77 6f 72 6b 65 64 2c 20 69 6e  g has worked, in
40447 76 6f 6b 65 20 66 74 73 33 44 69 73 63 6f 6e 6e  voke fts3Disconn
40448 65 63 74 4d 65 74 68 6f 64 28 29 20 74 6f 20 66  ectMethod() to f
40449 72 65 65 20 74 68 65 0a 20 20 2a 2a 20 6d 65 6d  ree the.  ** mem
4044a 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  ory associated w
4044b 69 74 68 20 74 68 65 20 46 74 73 33 54 61 62 6c  ith the Fts3Tabl
4044c 65 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  e structure and 
4044d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
4044e 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65  ..  ** Otherwise
4044f 2c 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 69  , return an SQLi
40450 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 20  te error code.. 
40451 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 72 63   */.  return (rc
40452 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 66 74  ==SQLITE_OK ? ft
40453 73 33 44 69 73 63 6f 6e 6e 65 63 74 4d 65 74 68  s3DisconnectMeth
40454 6f 64 28 70 56 74 61 62 29 20 3a 20 72 63 29 3b  od(pVtab) : rc);
40455 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b  .}.../*.** Invok
40456 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72  e sqlite3_declar
40457 65 5f 76 74 61 62 28 29 20 74 6f 20 64 65 63 6c  e_vtab() to decl
40458 61 72 65 20 74 68 65 20 73 63 68 65 6d 61 20 66  are the schema f
40459 6f 72 20 74 68 65 20 46 54 53 33 20 74 61 62 6c  or the FTS3 tabl
4045a 65 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74  e.** passed as t
4045b 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
4045c 74 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20  t. This is done 
4045d 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 78  as part of the x
4045e 43 6f 6e 6e 65 63 74 28 29 0a 2a 2a 20 61 6e 64  Connect().** and
4045f 20 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f   xCreate() metho
40460 64 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 2a 70 52  ds..**.** If *pR
40461 63 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 77 68  c is non-zero wh
40462 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
40463 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69   is called, it i
40464 73 20 61 20 6e 6f 2d 6f 70 2e 20 0a 2a 2a 20 4f  s a no-op. .** O
40465 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e 20  therwise, if an 
40466 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e  error occurs, an
40467 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f   SQLite error co
40468 64 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  de is stored in 
40469 2a 70 52 63 0a 2a 2a 20 62 65 66 6f 72 65 20 72  *pRc.** before r
4046a 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61  eturning..*/.sta
4046b 74 69 63 20 76 6f 69 64 20 66 74 73 33 44 65 63  tic void fts3Dec
4046c 6c 61 72 65 56 74 61 62 28 69 6e 74 20 2a 70 52  lareVtab(int *pR
4046d 63 2c 20 46 74 73 33 54 61 62 6c 65 20 2a 70 29  c, Fts3Table *p)
4046e 7b 0a 20 20 69 66 28 20 2a 70 52 63 3d 3d 53 51  {.  if( *pRc==SQ
4046f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
40470 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
40471 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
40472 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c  Iterator variabl
40473 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 3b  e */.    int rc;
40474 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40475 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
40476 20 63 6f 64 65 20 2a 2f 0a 20 20 20 20 63 68 61   code */.    cha
40477 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20 20 20 20  r *zSql;        
40478 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51             /* SQ
40479 4c 20 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73  L statement pass
4047a 65 64 20 74 6f 20 64 65 63 6c 61 72 65 5f 76 74  ed to declare_vt
4047b 61 62 28 29 20 2a 2f 0a 20 20 20 20 63 68 61 72  ab() */.    char
4047c 20 2a 7a 43 6f 6c 73 3b 20 20 20 20 20 20 20 20   *zCols;        
4047d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73            /* Lis
4047e 74 20 6f 66 20 75 73 65 72 20 64 65 66 69 6e 65  t of user define
4047f 64 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 20  d columns */.   
40480 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 61   const char *zLa
40481 6e 67 75 61 67 65 69 64 3b 0a 0a 20 20 20 20 7a  nguageid;..    z
40482 4c 61 6e 67 75 61 67 65 69 64 20 3d 20 28 70 2d  Languageid = (p-
40483 3e 7a 4c 61 6e 67 75 61 67 65 69 64 20 3f 20 70  >zLanguageid ? p
40484 2d 3e 7a 4c 61 6e 67 75 61 67 65 69 64 20 3a 20  ->zLanguageid : 
40485 22 5f 5f 6c 61 6e 67 69 64 22 29 3b 0a 20 20 20  "__langid");.   
40486 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 6f   sqlite3_vtab_co
40487 6e 66 69 67 28 70 2d 3e 64 62 2c 20 53 51 4c 49  nfig(p->db, SQLI
40488 54 45 5f 56 54 41 42 5f 43 4f 4e 53 54 52 41 49  TE_VTAB_CONSTRAI
40489 4e 54 5f 53 55 50 50 4f 52 54 2c 20 31 29 3b 0a  NT_SUPPORT, 1);.
4048a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61  .    /* Create a
4048b 20 6c 69 73 74 20 6f 66 20 75 73 65 72 20 63 6f   list of user co
4048c 6c 75 6d 6e 73 20 66 6f 72 20 74 68 65 20 76 69  lumns for the vi
4048d 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20  rtual table */. 
4048e 20 20 20 7a 43 6f 6c 73 20 3d 20 73 71 6c 69 74     zCols = sqlit
4048f 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 51 2c 20  e3_mprintf("%Q, 
40490 22 2c 20 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 5b 30  ", p->azColumn[0
40491 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b  ]);.    for(i=1;
40492 20 7a 43 6f 6c 73 20 26 26 20 69 3c 70 2d 3e 6e   zCols && i<p->n
40493 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
40494 20 20 20 20 7a 43 6f 6c 73 20 3d 20 73 71 6c 69      zCols = sqli
40495 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 7a 25  te3_mprintf("%z%
40496 51 2c 20 22 2c 20 7a 43 6f 6c 73 2c 20 70 2d 3e  Q, ", zCols, p->
40497 61 7a 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20  azColumn[i]);.  
40498 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61    }..    /* Crea
40499 74 65 20 74 68 65 20 77 68 6f 6c 65 20 22 43 52  te the whole "CR
4049a 45 41 54 45 20 54 41 42 4c 45 22 20 73 74 61 74  EATE TABLE" stat
4049b 65 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f  ement to pass to
4049c 20 53 51 4c 69 74 65 20 2a 2f 0a 20 20 20 20 7a   SQLite */.    z
4049d 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Sql = sqlite3_mp
4049e 72 69 6e 74 66 28 0a 20 20 20 20 20 20 20 20 22  rintf(.        "
4049f 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 28 25  CREATE TABLE x(%
404a0 73 20 25 51 20 48 49 44 44 45 4e 2c 20 64 6f 63  s %Q HIDDEN, doc
404a1 69 64 20 48 49 44 44 45 4e 2c 20 25 51 20 48 49  id HIDDEN, %Q HI
404a2 44 44 45 4e 29 22 2c 20 0a 20 20 20 20 20 20 20  DDEN)", .       
404a3 20 7a 43 6f 6c 73 2c 20 70 2d 3e 7a 4e 61 6d 65   zCols, p->zName
404a4 2c 20 7a 4c 61 6e 67 75 61 67 65 69 64 0a 20 20  , zLanguageid.  
404a5 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 43    );.    if( !zC
404a6 6f 6c 73 20 7c 7c 20 21 7a 53 71 6c 20 29 7b 0a  ols || !zSql ){.
404a7 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
404a8 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c  E_NOMEM;.    }el
404a9 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  se{.      rc = s
404aa 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76  qlite3_declare_v
404ab 74 61 62 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 29  tab(p->db, zSql)
404ac 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c  ;.    }..    sql
404ad 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b  ite3_free(zSql);
404ae 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
404af 65 28 7a 43 6f 6c 73 29 3b 0a 20 20 20 20 2a 70  e(zCols);.    *p
404b0 52 63 20 3d 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a  Rc = rc;.  }.}..
404b1 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65  /*.** Create the
404b2 20 25 5f 73 74 61 74 20 74 61 62 6c 65 20 69 66   %_stat table if
404b3 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72   it does not alr
404b4 65 61 64 79 20 65 78 69 73 74 2e 0a 2a 2f 0a 53  eady exist..*/.S
404b5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
404b6 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 43 72  id sqlite3Fts3Cr
404b7 65 61 74 65 53 74 61 74 54 61 62 6c 65 28 69 6e  eateStatTable(in
404b8 74 20 2a 70 52 63 2c 20 46 74 73 33 54 61 62 6c  t *pRc, Fts3Tabl
404b9 65 20 2a 70 29 7b 0a 20 20 66 74 73 33 44 62 45  e *p){.  fts3DbE
404ba 78 65 63 28 70 52 63 2c 20 70 2d 3e 64 62 2c 20  xec(pRc, p->db, 
404bb 0a 20 20 20 20 20 20 22 43 52 45 41 54 45 20 54  .      "CREATE T
404bc 41 42 4c 45 20 49 46 20 4e 4f 54 20 45 58 49 53  ABLE IF NOT EXIS
404bd 54 53 20 25 51 2e 27 25 71 5f 73 74 61 74 27 22  TS %Q.'%q_stat'"
404be 0a 20 20 20 20 20 20 20 20 20 20 22 28 69 64 20  .          "(id 
404bf 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
404c0 4b 45 59 2c 20 76 61 6c 75 65 20 42 4c 4f 42 29  KEY, value BLOB)
404c1 3b 22 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 44 62  ;",.      p->zDb
404c2 2c 20 70 2d 3e 7a 4e 61 6d 65 0a 20 20 29 3b 0a  , p->zName.  );.
404c3 20 20 69 66 28 20 28 2a 70 52 63 29 3d 3d 53 51    if( (*pRc)==SQ
404c4 4c 49 54 45 5f 4f 4b 20 29 20 70 2d 3e 62 48 61  LITE_OK ) p->bHa
404c5 73 53 74 61 74 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a  sStat = 1;.}../*
404c6 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65 20 62  .** Create the b
404c7 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 74 61 62  acking store tab
404c8 6c 65 73 20 28 25 5f 63 6f 6e 74 65 6e 74 2c 20  les (%_content, 
404c9 25 5f 73 65 67 6d 65 6e 74 73 20 61 6e 64 20 25  %_segments and %
404ca 5f 73 65 67 64 69 72 29 0a 2a 2a 20 72 65 71 75  _segdir).** requ
404cb 69 72 65 64 20 62 79 20 74 68 65 20 46 54 53 33  ired by the FTS3
404cc 20 74 61 62 6c 65 20 70 61 73 73 65 64 20 61 73   table passed as
404cd 20 74 68 65 20 6f 6e 6c 79 20 61 72 67 75 6d 65   the only argume
404ce 6e 74 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65  nt. This is done
404cf 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 74  .** as part of t
404d0 68 65 20 76 74 61 62 20 78 43 72 65 61 74 65 28  he vtab xCreate(
404d1 29 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20  ) method..**.** 
404d2 49 66 20 74 68 65 20 70 2d 3e 62 48 61 73 44 6f  If the p->bHasDo
404d3 63 73 69 7a 65 20 62 6f 6f 6c 65 61 6e 20 69 73  csize boolean is
404d4 20 74 72 75 65 20 28 69 6e 64 69 63 61 74 69 6e   true (indicatin
404d5 67 20 74 68 61 74 20 74 68 69 73 20 69 73 20 61  g that this is a
404d6 6e 0a 2a 2a 20 46 54 53 34 20 74 61 62 6c 65 2c  n.** FTS4 table,
404d7 20 6e 6f 74 20 61 6e 20 46 54 53 33 20 74 61 62   not an FTS3 tab
404d8 6c 65 29 20 74 68 65 6e 20 61 6c 73 6f 20 63 72  le) then also cr
404d9 65 61 74 65 20 74 68 65 20 25 5f 64 6f 63 73 69  eate the %_docsi
404da 7a 65 20 61 6e 64 0a 2a 2a 20 25 5f 73 74 61 74  ze and.** %_stat
404db 20 74 61 62 6c 65 73 20 72 65 71 75 69 72 65 64   tables required
404dc 20 62 79 20 46 54 53 34 2e 0a 2a 2f 0a 73 74 61   by FTS4..*/.sta
404dd 74 69 63 20 69 6e 74 20 66 74 73 33 43 72 65 61  tic int fts3Crea
404de 74 65 54 61 62 6c 65 73 28 46 74 73 33 54 61 62  teTables(Fts3Tab
404df 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  le *p){.  int rc
404e0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
404e1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
404e2 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e  urn code */.  in
404e3 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
404e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
404e5 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
404e6 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  le */.  sqlite3 
404e7 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20  *db = p->db;    
404e8 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
404e9 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
404ea 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e  on */..  if( p->
404eb 7a 43 6f 6e 74 65 6e 74 54 62 6c 3d 3d 30 20 29  zContentTbl==0 )
404ec 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
404ed 20 2a 7a 4c 61 6e 67 75 61 67 65 69 64 20 3d 20   *zLanguageid = 
404ee 70 2d 3e 7a 4c 61 6e 67 75 61 67 65 69 64 3b 0a  p->zLanguageid;.
404ef 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65      char *zConte
404f0 6e 74 43 6f 6c 73 3b 20 20 20 20 20 20 20 20 20  ntCols;         
404f1 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 6f 66 20    /* Columns of 
404f2 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20  %_content table 
404f3 2a 2f 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74  */..    /* Creat
404f4 65 20 61 20 6c 69 73 74 20 6f 66 20 75 73 65 72  e a list of user
404f5 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74 68 65   columns for the
404f6 20 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 2a   content table *
404f7 2f 0a 20 20 20 20 7a 43 6f 6e 74 65 6e 74 43 6f  /.    zContentCo
404f8 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ls = sqlite3_mpr
404f9 69 6e 74 66 28 22 64 6f 63 69 64 20 49 4e 54 45  intf("docid INTE
404fa 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 22  GER PRIMARY KEY"
404fb 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
404fc 7a 43 6f 6e 74 65 6e 74 43 6f 6c 73 20 26 26 20  zContentCols && 
404fd 69 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  i<p->nColumn; i+
404fe 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  +){.      char *
404ff 7a 20 3d 20 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 5b  z = p->azColumn[
40500 69 5d 3b 0a 20 20 20 20 20 20 7a 43 6f 6e 74 65  i];.      zConte
40501 6e 74 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33  ntCols = sqlite3
40502 5f 6d 70 72 69 6e 74 66 28 22 25 7a 2c 20 27 63  _mprintf("%z, 'c
40503 25 64 25 71 27 22 2c 20 7a 43 6f 6e 74 65 6e 74  %d%q'", zContent
40504 43 6f 6c 73 2c 20 69 2c 20 7a 29 3b 0a 20 20 20  Cols, i, z);.   
40505 20 7d 0a 20 20 20 20 69 66 28 20 7a 4c 61 6e 67   }.    if( zLang
40506 75 61 67 65 69 64 20 26 26 20 7a 43 6f 6e 74 65  uageid && zConte
40507 6e 74 43 6f 6c 73 20 29 7b 0a 20 20 20 20 20 20  ntCols ){.      
40508 7a 43 6f 6e 74 65 6e 74 43 6f 6c 73 20 3d 20 73  zContentCols = s
40509 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
4050a 25 7a 2c 20 6c 61 6e 67 69 64 22 2c 20 7a 43 6f  %z, langid", zCo
4050b 6e 74 65 6e 74 43 6f 6c 73 2c 20 7a 4c 61 6e 67  ntentCols, zLang
4050c 75 61 67 65 69 64 29 3b 0a 20 20 20 20 7d 0a 20  uageid);.    }. 
4050d 20 20 20 69 66 28 20 7a 43 6f 6e 74 65 6e 74 43     if( zContentC
4050e 6f 6c 73 3d 3d 30 20 29 20 72 63 20 3d 20 53 51  ols==0 ) rc = SQ
4050f 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 0a 20  LITE_NOMEM;.  . 
40510 20 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65     /* Create the
40511 20 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 2a   content table *
40512 2f 0a 20 20 20 20 66 74 73 33 44 62 45 78 65 63  /.    fts3DbExec
40513 28 26 72 63 2c 20 64 62 2c 20 0a 20 20 20 20 20  (&rc, db, .     
40514 20 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20    "CREATE TABLE 
40515 25 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 28  %Q.'%q_content'(
40516 25 73 29 22 2c 0a 20 20 20 20 20 20 20 70 2d 3e  %s)",.       p->
40517 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a  zDb, p->zName, z
40518 43 6f 6e 74 65 6e 74 43 6f 6c 73 0a 20 20 20 20  ContentCols.    
40519 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
4051a 72 65 65 28 7a 43 6f 6e 74 65 6e 74 43 6f 6c 73  ree(zContentCols
4051b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65  );.  }..  /* Cre
4051c 61 74 65 20 6f 74 68 65 72 20 74 61 62 6c 65 73  ate other tables
4051d 20 2a 2f 0a 20 20 66 74 73 33 44 62 45 78 65 63   */.  fts3DbExec
4051e 28 26 72 63 2c 20 64 62 2c 20 0a 20 20 20 20 20  (&rc, db, .     
4051f 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 25   "CREATE TABLE %
40520 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74 73 27 28  Q.'%q_segments'(
40521 62 6c 6f 63 6b 69 64 20 49 4e 54 45 47 45 52 20  blockid INTEGER 
40522 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 6c 6f  PRIMARY KEY, blo
40523 63 6b 20 42 4c 4f 42 29 3b 22 2c 0a 20 20 20 20  ck BLOB);",.    
40524 20 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61    p->zDb, p->zNa
40525 6d 65 0a 20 20 29 3b 0a 20 20 66 74 73 33 44 62  me.  );.  fts3Db
40526 45 78 65 63 28 26 72 63 2c 20 64 62 2c 20 0a 20  Exec(&rc, db, . 
40527 20 20 20 20 20 22 43 52 45 41 54 45 20 54 41 42       "CREATE TAB
40528 4c 45 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72  LE %Q.'%q_segdir
40529 27 28 22 0a 20 20 20 20 20 20 20 20 22 6c 65 76  '(".        "lev
4052a 65 6c 20 49 4e 54 45 47 45 52 2c 22 0a 20 20 20  el INTEGER,".   
4052b 20 20 20 20 20 22 69 64 78 20 49 4e 54 45 47 45       "idx INTEGE
4052c 52 2c 22 0a 20 20 20 20 20 20 20 20 22 73 74 61  R,".        "sta
4052d 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52  rt_block INTEGER
4052e 2c 22 0a 20 20 20 20 20 20 20 20 22 6c 65 61 76  ,".        "leav
4052f 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 49 4e 54  es_end_block INT
40530 45 47 45 52 2c 22 0a 20 20 20 20 20 20 20 20 22  EGER,".        "
40531 65 6e 64 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45  end_block INTEGE
40532 52 2c 22 0a 20 20 20 20 20 20 20 20 22 72 6f 6f  R,".        "roo
40533 74 20 42 4c 4f 42 2c 22 0a 20 20 20 20 20 20 20  t BLOB,".       
40534 20 22 50 52 49 4d 41 52 59 20 4b 45 59 28 6c 65   "PRIMARY KEY(le
40535 76 65 6c 2c 20 69 64 78 29 22 0a 20 20 20 20 20  vel, idx)".     
40536 20 22 29 3b 22 2c 0a 20 20 20 20 20 20 70 2d 3e   ");",.      p->
40537 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 0a 20 20  zDb, p->zName.  
40538 29 3b 0a 20 20 69 66 28 20 70 2d 3e 62 48 61 73  );.  if( p->bHas
40539 44 6f 63 73 69 7a 65 20 29 7b 0a 20 20 20 20 66  Docsize ){.    f
4053a 74 73 33 44 62 45 78 65 63 28 26 72 63 2c 20 64  ts3DbExec(&rc, d
4053b 62 2c 20 0a 20 20 20 20 20 20 20 20 22 43 52 45  b, .        "CRE
4053c 41 54 45 20 54 41 42 4c 45 20 25 51 2e 27 25 71  ATE TABLE %Q.'%q
4053d 5f 64 6f 63 73 69 7a 65 27 28 64 6f 63 69 64 20  _docsize'(docid 
4053e 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
4053f 4b 45 59 2c 20 73 69 7a 65 20 42 4c 4f 42 29 3b  KEY, size BLOB);
40540 22 2c 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 44  ",.        p->zD
40541 62 2c 20 70 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20  b, p->zName.    
40542 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  );.  }.  assert(
40543 20 70 2d 3e 62 48 61 73 53 74 61 74 3d 3d 70 2d   p->bHasStat==p-
40544 3e 62 46 74 73 34 20 29 3b 0a 20 20 69 66 28 20  >bFts4 );.  if( 
40545 70 2d 3e 62 48 61 73 53 74 61 74 20 29 7b 0a 20  p->bHasStat ){. 
40546 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 43 72     sqlite3Fts3Cr
40547 65 61 74 65 53 74 61 74 54 61 62 6c 65 28 26 72  eateStatTable(&r
40548 63 2c 20 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74  c, p);.  }.  ret
40549 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
4054a 20 53 74 6f 72 65 20 74 68 65 20 63 75 72 72 65   Store the curre
4054b 6e 74 20 64 61 74 61 62 61 73 65 20 70 61 67 65  nt database page
4054c 2d 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 69  -size in bytes i
4054d 6e 20 70 2d 3e 6e 50 67 73 7a 2e 0a 2a 2a 0a 2a  n p->nPgsz..**.*
4054e 2a 20 49 66 20 2a 70 52 63 20 69 73 20 6e 6f 6e  * If *pRc is non
4054f 2d 7a 65 72 6f 20 77 68 65 6e 20 74 68 69 73 20  -zero when this 
40550 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
40551 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f  ed, it is a no-o
40552 70 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  p. .** Otherwise
40553 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  , if an error oc
40554 63 75 72 73 2c 20 61 6e 20 53 51 4c 69 74 65 20  curs, an SQLite 
40555 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 74  error code is st
40556 6f 72 65 64 20 69 6e 20 2a 70 52 63 0a 2a 2a 20  ored in *pRc.** 
40557 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
40558 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
40559 20 66 74 73 33 44 61 74 61 62 61 73 65 50 61 67   fts3DatabasePag
4055a 65 53 69 7a 65 28 69 6e 74 20 2a 70 52 63 2c 20  eSize(int *pRc, 
4055b 46 74 73 33 54 61 62 6c 65 20 2a 70 29 7b 0a 20  Fts3Table *p){. 
4055c 20 69 66 28 20 2a 70 52 63 3d 3d 53 51 4c 49 54   if( *pRc==SQLIT
4055d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20  E_OK ){.    int 
4055e 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
4055f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
40560 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 20 20  urn code */.    
40561 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20  char *zSql;     
40562 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
40563 20 53 51 4c 20 74 65 78 74 20 22 50 52 41 47 4d   SQL text "PRAGM
40564 41 20 25 51 2e 70 61 67 65 5f 73 69 7a 65 22 20  A %Q.page_size" 
40565 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  */.    sqlite3_s
40566 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20 20  tmt *pStmt;     
40567 20 20 20 20 20 2f 2a 20 43 6f 6d 70 69 6c 65 64       /* Compiled
40568 20 22 50 52 41 47 4d 41 20 25 51 2e 70 61 67 65   "PRAGMA %Q.page
40569 5f 73 69 7a 65 22 20 73 74 61 74 65 6d 65 6e 74  _size" statement
4056a 20 2a 2f 0a 20 20 0a 20 20 20 20 7a 53 71 6c 20   */.  .    zSql 
4056b 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
4056c 66 28 22 50 52 41 47 4d 41 20 25 51 2e 70 61 67  f("PRAGMA %Q.pag
4056d 65 5f 73 69 7a 65 22 2c 20 70 2d 3e 7a 44 62 29  e_size", p->zDb)
4056e 3b 0a 20 20 20 20 69 66 28 20 21 7a 53 71 6c 20  ;.    if( !zSql 
4056f 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
40570 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
40571 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
40572 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
40573 65 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 2d  e(p->db, zSql, -
40574 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  1, &pStmt, 0);. 
40575 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
40576 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
40577 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70    sqlite3_step(p
40578 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 20 20 70  Stmt);.        p
40579 2d 3e 6e 50 67 73 7a 20 3d 20 73 71 6c 69 74 65  ->nPgsz = sqlite
4057a 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70 53 74  3_column_int(pSt
4057b 6d 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  mt, 0);.        
4057c 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e  rc = sqlite3_fin
4057d 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20  alize(pStmt);.  
4057e 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63      }else if( rc
4057f 3d 3d 53 51 4c 49 54 45 5f 41 55 54 48 20 29 7b  ==SQLITE_AUTH ){
40580 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 50 67 73  .        p->nPgs
40581 7a 20 3d 20 31 30 32 34 3b 0a 20 20 20 20 20 20  z = 1024;.      
40582 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
40583 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
40584 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e      assert( p->n
40585 50 67 73 7a 3e 30 20 7c 7c 20 72 63 21 3d 53 51  Pgsz>0 || rc!=SQ
40586 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 73  LITE_OK );.    s
40587 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c  qlite3_free(zSql
40588 29 3b 0a 20 20 20 20 2a 70 52 63 20 3d 20 72 63  );.    *pRc = rc
40589 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 22  ;.  }.}../*.** "
4058a 53 70 65 63 69 61 6c 22 20 46 54 53 34 20 61 72  Special" FTS4 ar
4058b 67 75 6d 65 6e 74 73 20 61 72 65 20 63 6f 6c 75  guments are colu
4058c 6d 6e 20 73 70 65 63 69 66 69 63 61 74 69 6f 6e  mn specification
4058d 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  s of the followi
4058e 6e 67 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20  ng form:.**.**  
4058f 20 3c 6b 65 79 3e 20 3d 20 3c 76 61 6c 75 65 3e   <key> = <value>
40590 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61 79  .**.** There may
40591 20 6e 6f 74 20 62 65 20 77 68 69 74 65 73 70 61   not be whitespa
40592 63 65 20 73 75 72 72 6f 75 6e 64 69 6e 67 20 74  ce surrounding t
40593 68 65 20 22 3d 22 20 63 68 61 72 61 63 74 65 72  he "=" character
40594 2e 20 54 68 65 20 3c 76 61 6c 75 65 3e 20 0a 2a  . The <value> .*
40595 2a 20 74 65 72 6d 20 6d 61 79 20 62 65 20 71 75  * term may be qu
40596 6f 74 65 64 2c 20 62 75 74 20 74 68 65 20 3c 6b  oted, but the <k
40597 65 79 3e 20 6d 61 79 20 6e 6f 74 2e 0a 2a 2f 0a  ey> may not..*/.
40598 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 49  static int fts3I
40599 73 53 70 65 63 69 61 6c 43 6f 6c 75 6d 6e 28 0a  sSpecialColumn(.
4059a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c    const char *z,
4059b 20 0a 20 20 69 6e 74 20 2a 70 6e 4b 65 79 2c 0a   .  int *pnKey,.
4059c 20 20 63 68 61 72 20 2a 2a 70 7a 56 61 6c 75 65    char **pzValue
4059d 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 56 61 6c  .){.  char *zVal
4059e 75 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ue;.  const char
4059f 20 2a 7a 43 73 72 20 3d 20 7a 3b 0a 0a 20 20 77   *zCsr = z;..  w
405a0 68 69 6c 65 28 20 2a 7a 43 73 72 21 3d 27 3d 27  hile( *zCsr!='='
405a1 20 29 7b 0a 20 20 20 20 69 66 28 20 2a 7a 43 73   ){.    if( *zCs
405a2 72 3d 3d 27 5c 30 27 20 29 20 72 65 74 75 72 6e  r=='\0' ) return
405a3 20 30 3b 0a 20 20 20 20 7a 43 73 72 2b 2b 3b 0a   0;.    zCsr++;.
405a4 20 20 7d 0a 0a 20 20 2a 70 6e 4b 65 79 20 3d 20    }..  *pnKey = 
405a5 28 69 6e 74 29 28 7a 43 73 72 2d 7a 29 3b 0a 20  (int)(zCsr-z);. 
405a6 20 7a 56 61 6c 75 65 20 3d 20 73 71 6c 69 74 65   zValue = sqlite
405a7 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20  3_mprintf("%s", 
405a8 26 7a 43 73 72 5b 31 5d 29 3b 0a 20 20 69 66 28  &zCsr[1]);.  if(
405a9 20 7a 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 73   zValue ){.    s
405aa 71 6c 69 74 65 33 46 74 73 33 44 65 71 75 6f 74  qlite3Fts3Dequot
405ab 65 28 7a 56 61 6c 75 65 29 3b 0a 20 20 7d 0a 20  e(zValue);.  }. 
405ac 20 2a 70 7a 56 61 6c 75 65 20 3d 20 7a 56 61 6c   *pzValue = zVal
405ad 75 65 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a  ue;.  return 1;.
405ae 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20  }../*.** Append 
405af 74 68 65 20 6f 75 74 70 75 74 20 6f 66 20 61 20  the output of a 
405b0 70 72 69 6e 74 66 28 29 20 73 74 79 6c 65 20 66  printf() style f
405b1 6f 72 6d 61 74 74 69 6e 67 20 74 6f 20 61 6e 20  ormatting to an 
405b2 65 78 69 73 74 69 6e 67 20 73 74 72 69 6e 67 2e  existing string.
405b3 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
405b4 66 74 73 33 41 70 70 65 6e 64 66 28 0a 20 20 69  fts3Appendf(.  i
405b5 6e 74 20 2a 70 52 63 2c 20 20 20 20 20 20 20 20  nt *pRc,        
405b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
405b7 2a 20 49 4e 2f 4f 55 54 3a 20 45 72 72 6f 72 20  * IN/OUT: Error 
405b8 63 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  code */.  char *
405b9 2a 70 7a 2c 20 20 20 20 20 20 20 20 20 20 20 20  *pz,            
405ba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f            /* IN/
405bb 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20  OUT: Pointer to 
405bc 73 74 72 69 6e 67 20 62 75 66 66 65 72 20 2a 2f  string buffer */
405bd 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
405be 46 6f 72 6d 61 74 2c 20 20 20 20 20 20 20 20 20  Format,         
405bf 20 20 20 2f 2a 20 50 72 69 6e 74 66 20 66 6f 72     /* Printf for
405c0 6d 61 74 20 73 74 72 69 6e 67 20 74 6f 20 61 70  mat string to ap
405c1 70 65 6e 64 20 2a 2f 0a 20 20 2e 2e 2e 20 20 20  pend */.  ...   
405c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
405c3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
405c4 75 6d 65 6e 74 73 20 66 6f 72 20 70 72 69 6e 74  uments for print
405c5 66 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20  f format string 
405c6 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 2a 70 52 63  */.){.  if( *pRc
405c7 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
405c8 20 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20     va_list ap;. 
405c9 20 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20     char *z;.    
405ca 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f  va_start(ap, zFo
405cb 72 6d 61 74 29 3b 0a 20 20 20 20 7a 20 3d 20 73  rmat);.    z = s
405cc 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28  qlite3_vmprintf(
405cd 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
405ce 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
405cf 20 20 69 66 28 20 7a 20 26 26 20 2a 70 7a 20 29    if( z && *pz )
405d0 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 32  {.      char *z2
405d1 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
405d2 74 66 28 22 25 73 25 73 22 2c 20 2a 70 7a 2c 20  tf("%s%s", *pz, 
405d3 7a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  z);.      sqlite
405d4 33 5f 66 72 65 65 28 7a 29 3b 0a 20 20 20 20 20  3_free(z);.     
405d5 20 7a 20 3d 20 7a 32 3b 0a 20 20 20 20 7d 0a 20   z = z2;.    }. 
405d6 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 2a 70     if( z==0 ) *p
405d7 52 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  Rc = SQLITE_NOME
405d8 4d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  M;.    sqlite3_f
405d9 72 65 65 28 2a 70 7a 29 3b 0a 20 20 20 20 2a 70  ree(*pz);.    *p
405da 7a 20 3d 20 7a 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  z = z;.  }.}../*
405db 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 63 6f 70  .** Return a cop
405dc 79 20 6f 66 20 69 6e 70 75 74 20 73 74 72 69 6e  y of input strin
405dd 67 20 7a 49 6e 70 75 74 20 65 6e 63 6c 6f 73 65  g zInput enclose
405de 64 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f 74  d in double-quot
405df 65 73 20 28 22 29 20 61 6e 64 0a 2a 2a 20 77 69  es (") and.** wi
405e0 74 68 20 61 6c 6c 20 64 6f 75 62 6c 65 20 71 75  th all double qu
405e1 6f 74 65 20 63 68 61 72 61 63 74 65 72 73 20 65  ote characters e
405e2 73 63 61 70 65 64 2e 20 46 6f 72 20 65 78 61 6d  scaped. For exam
405e3 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 66  ple:.**.**     f
405e4 74 73 33 51 75 6f 74 65 49 64 28 22 75 6e 20 5c  ts3QuoteId("un \
405e5 22 7a 69 70 5c 22 22 29 20 20 20 2d 3e 20 20 20  "zip\"")   ->   
405e6 20 22 75 6e 20 5c 22 5c 22 7a 69 70 5c 22 5c 22   "un \"\"zip\"\"
405e7 22 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e  ".**.** The poin
405e8 74 65 72 20 72 65 74 75 72 6e 65 64 20 70 6f 69  ter returned poi
405e9 6e 74 73 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  nts to memory ob
405ea 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
405eb 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 49 74  te3_malloc(). It
405ec 0a 2a 2a 20 69 73 20 74 68 65 20 63 61 6c 6c 65  .** is the calle
405ed 72 73 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  rs responsibilit
405ee 79 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65  y to call sqlite
405ef 33 5f 66 72 65 65 28 29 20 74 6f 20 72 65 6c 65  3_free() to rele
405f0 61 73 65 20 74 68 69 73 0a 2a 2a 20 6d 65 6d 6f  ase this.** memo
405f1 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68  ry..*/.static ch
405f2 61 72 20 2a 66 74 73 33 51 75 6f 74 65 49 64 28  ar *fts3QuoteId(
405f3 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 49 6e 70  char const *zInp
405f4 75 74 29 7b 0a 20 20 69 6e 74 20 6e 52 65 74 3b  ut){.  int nRet;
405f5 0a 20 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a 20  .  char *zRet;. 
405f6 20 6e 52 65 74 20 3d 20 32 20 2b 20 28 69 6e 74   nRet = 2 + (int
405f7 29 73 74 72 6c 65 6e 28 7a 49 6e 70 75 74 29 2a  )strlen(zInput)*
405f8 32 20 2b 20 31 3b 0a 20 20 7a 52 65 74 20 3d 20  2 + 1;.  zRet = 
405f9 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
405fa 52 65 74 29 3b 0a 20 20 69 66 28 20 7a 52 65 74  Ret);.  if( zRet
405fb 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
405fc 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 7a 52 65     char *z = zRe
405fd 74 3b 0a 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20  t;.    *(z++) = 
405fe 27 22 27 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  '"';.    for(i=0
405ff 3b 20 7a 49 6e 70 75 74 5b 69 5d 3b 20 69 2b 2b  ; zInput[i]; i++
40600 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 49 6e  ){.      if( zIn
40601 70 75 74 5b 69 5d 3d 3d 27 22 27 20 29 20 2a 28  put[i]=='"' ) *(
40602 7a 2b 2b 29 20 3d 20 27 22 27 3b 0a 20 20 20 20  z++) = '"';.    
40603 20 20 2a 28 7a 2b 2b 29 20 3d 20 7a 49 6e 70 75    *(z++) = zInpu
40604 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  t[i];.    }.    
40605 2a 28 7a 2b 2b 29 20 3d 20 27 22 27 3b 0a 20 20  *(z++) = '"';.  
40606 20 20 2a 28 7a 2b 2b 29 20 3d 20 27 5c 30 27 3b    *(z++) = '\0';
40607 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 52  .  }.  return zR
40608 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  et;.}../*.** Ret
40609 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 63 6f  urn a list of co
4060a 6d 6d 61 20 73 65 70 61 72 61 74 65 64 20 53 51  mma separated SQ
4060b 4c 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 6e  L expressions an
4060c 64 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  d a FROM clause 
4060d 74 68 61 74 20 0a 2a 2a 20 63 6f 75 6c 64 20 62  that .** could b
4060e 65 20 75 73 65 64 20 69 6e 20 61 20 53 45 4c 45  e used in a SELE
4060f 43 54 20 73 74 61 74 65 6d 65 6e 74 20 73 75 63  CT statement suc
40610 68 20 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69  h as the followi
40611 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  ng:.**.**     SE
40612 4c 45 43 54 20 3c 6c 69 73 74 20 6f 66 20 65 78  LECT <list of ex
40613 70 72 65 73 73 69 6f 6e 73 3e 20 46 52 4f 4d 20  pressions> FROM 
40614 25 5f 63 6f 6e 74 65 6e 74 20 41 53 20 78 20 2e  %_content AS x .
40615 2e 2e 0a 2a 2a 0a 2a 2a 20 74 6f 20 72 65 74 75  ...**.** to retu
40616 72 6e 20 74 68 65 20 64 6f 63 69 64 2c 20 66 6f  rn the docid, fo
40617 6c 6c 6f 77 65 64 20 62 79 20 65 61 63 68 20 63  llowed by each c
40618 6f 6c 75 6d 6e 20 6f 66 20 74 65 78 74 20 64 61  olumn of text da
40619 74 61 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 66  ta in order.** f
4061a 72 6f 6d 20 6c 65 66 74 20 74 6f 20 77 72 69 74  rom left to writ
4061b 65 2e 20 49 66 20 70 61 72 61 6d 65 74 65 72 20  e. If parameter 
4061c 7a 46 75 6e 63 20 69 73 20 6e 6f 74 20 4e 55 4c  zFunc is not NUL
4061d 4c 2c 20 74 68 65 6e 20 69 6e 73 74 65 61 64 20  L, then instead 
4061e 6f 66 0a 2a 2a 20 62 65 69 6e 67 20 72 65 74 75  of.** being retu
4061f 72 6e 65 64 20 64 69 72 65 63 74 6c 79 20 65 61  rned directly ea
40620 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 65 78  ch column of tex
40621 74 20 64 61 74 61 20 69 73 20 70 61 73 73 65 64  t data is passed
40622 20 74 6f 20 61 6e 20 53 51 4c 0a 2a 2a 20 66 75   to an SQL.** fu
40623 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 7a 46 75  nction named zFu
40624 6e 63 20 66 69 72 73 74 2e 20 46 6f 72 20 65 78  nc first. For ex
40625 61 6d 70 6c 65 2c 20 69 66 20 7a 46 75 6e 63 20  ample, if zFunc 
40626 69 73 20 22 75 6e 7a 69 70 22 20 61 6e 64 20 74  is "unzip" and t
40627 68 65 0a 2a 2a 20 74 61 62 6c 65 20 68 61 73 20  he.** table has 
40628 74 68 65 20 74 68 72 65 65 20 75 73 65 72 2d 64  the three user-d
40629 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e 73 20 22  efined columns "
4062a 61 22 2c 20 22 62 22 2c 20 61 6e 64 20 22 63 22  a", "b", and "c"
4062b 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  , the following.
4062c 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 72 65 74  ** string is ret
4062d 75 72 6e 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  urned:.**.**    
4062e 20 22 64 6f 63 69 64 2c 20 75 6e 7a 69 70 28 78   "docid, unzip(x
4062f 2e 27 61 27 29 2c 20 75 6e 7a 69 70 28 78 2e 27  .'a'), unzip(x.'
40630 62 27 29 2c 20 75 6e 7a 69 70 28 78 2e 27 63 27  b'), unzip(x.'c'
40631 29 20 46 52 4f 4d 20 25 5f 63 6f 6e 74 65 6e 74  ) FROM %_content
40632 20 41 53 20 78 22 0a 2a 2a 0a 2a 2a 20 54 68 65   AS x".**.** The
40633 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65   pointer returne
40634 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 75  d points to a bu
40635 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 62  ffer allocated b
40636 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  y sqlite3_malloc
40637 28 29 2e 20 49 74 0a 2a 2a 20 69 73 20 74 68 65  (). It.** is the
40638 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20   responsibility 
40639 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  of the caller to
4063a 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65   eventually free
4063b 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 2a 70   it..**.** If *p
4063c 52 63 20 69 73 20 6e 6f 74 20 53 51 4c 49 54 45  Rc is not SQLITE
4063d 5f 4f 4b 20 77 68 65 6e 20 74 68 69 73 20 66 75  _OK when this fu
4063e 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
4063f 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 20  , it is a no-op 
40640 28 61 6e 64 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70  (and.** a NULL p
40641 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e  ointer is return
40642 65 64 29 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ed). Otherwise, 
40643 69 66 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20  if an OOM error 
40644 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 2a  is encountered.*
40645 2a 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  * by this functi
40646 6f 6e 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  on, NULL is retu
40647 72 6e 65 64 20 61 6e 64 20 2a 70 52 63 20 69 73  rned and *pRc is
40648 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e   set to SQLITE_N
40649 4f 4d 45 4d 2e 20 49 66 0a 2a 2a 20 6e 6f 20 65  OMEM. If.** no e
4064a 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 52  rror occurs, *pR
4064b 63 20 69 73 20 6c 65 66 74 20 75 6e 6d 6f 64 69  c is left unmodi
4064c 66 69 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  fied..*/.static 
4064d 63 68 61 72 20 2a 66 74 73 33 52 65 61 64 45 78  char *fts3ReadEx
4064e 70 72 4c 69 73 74 28 46 74 73 33 54 61 62 6c 65  prList(Fts3Table
4064f 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *p, const char 
40650 2a 7a 46 75 6e 63 2c 20 69 6e 74 20 2a 70 52 63  *zFunc, int *pRc
40651 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65 74 20  ){.  char *zRet 
40652 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 46 72  = 0;.  char *zFr
40653 65 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a  ee = 0;.  char *
40654 7a 46 75 6e 63 74 69 6f 6e 3b 0a 20 20 69 6e 74  zFunction;.  int
40655 20 69 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 7a 43   i;..  if( p->zC
40656 6f 6e 74 65 6e 74 54 62 6c 3d 3d 30 20 29 7b 0a  ontentTbl==0 ){.
40657 20 20 20 20 69 66 28 20 21 7a 46 75 6e 63 20 29      if( !zFunc )
40658 7b 0a 20 20 20 20 20 20 7a 46 75 6e 63 74 69 6f  {.      zFunctio
40659 6e 20 3d 20 22 22 3b 0a 20 20 20 20 7d 65 6c 73  n = "";.    }els
4065a 65 7b 0a 20 20 20 20 20 20 7a 46 72 65 65 20 3d  e{.      zFree =
4065b 20 7a 46 75 6e 63 74 69 6f 6e 20 3d 20 66 74 73   zFunction = fts
4065c 33 51 75 6f 74 65 49 64 28 7a 46 75 6e 63 29 3b  3QuoteId(zFunc);
4065d 0a 20 20 20 20 7d 0a 20 20 20 20 66 74 73 33 41  .    }.    fts3A
4065e 70 70 65 6e 64 66 28 70 52 63 2c 20 26 7a 52 65  ppendf(pRc, &zRe
4065f 74 2c 20 22 64 6f 63 69 64 22 29 3b 0a 20 20 20  t, "docid");.   
40660 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
40661 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
40662 20 20 20 20 66 74 73 33 41 70 70 65 6e 64 66 28      fts3Appendf(
40663 70 52 63 2c 20 26 7a 52 65 74 2c 20 22 2c 25 73  pRc, &zRet, ",%s
40664 28 78 2e 27 63 25 64 25 71 27 29 22 2c 20 7a 46  (x.'c%d%q')", zF
40665 75 6e 63 74 69 6f 6e 2c 20 69 2c 20 70 2d 3e 61  unction, i, p->a
40666 7a 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20 20  zColumn[i]);.   
40667 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 4c   }.    if( p->zL
40668 61 6e 67 75 61 67 65 69 64 20 29 7b 0a 20 20 20  anguageid ){.   
40669 20 20 20 66 74 73 33 41 70 70 65 6e 64 66 28 70     fts3Appendf(p
4066a 52 63 2c 20 26 7a 52 65 74 2c 20 22 2c 20 78 2e  Rc, &zRet, ", x.
4066b 25 51 22 2c 20 22 6c 61 6e 67 69 64 22 29 3b 0a  %Q", "langid");.
4066c 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
4066d 33 5f 66 72 65 65 28 7a 46 72 65 65 29 3b 0a 20  3_free(zFree);. 
4066e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 74 73 33   }else{.    fts3
4066f 41 70 70 65 6e 64 66 28 70 52 63 2c 20 26 7a 52  Appendf(pRc, &zR
40670 65 74 2c 20 22 72 6f 77 69 64 22 29 3b 0a 20 20  et, "rowid");.  
40671 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
40672 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
40673 20 20 20 20 20 66 74 73 33 41 70 70 65 6e 64 66       fts3Appendf
40674 28 70 52 63 2c 20 26 7a 52 65 74 2c 20 22 2c 20  (pRc, &zRet, ", 
40675 78 2e 27 25 71 27 22 2c 20 70 2d 3e 61 7a 43 6f  x.'%q'", p->azCo
40676 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  lumn[i]);.    }.
40677 20 20 20 20 69 66 28 20 70 2d 3e 7a 4c 61 6e 67      if( p->zLang
40678 75 61 67 65 69 64 20 29 7b 0a 20 20 20 20 20 20  uageid ){.      
40679 66 74 73 33 41 70 70 65 6e 64 66 28 70 52 63 2c  fts3Appendf(pRc,
4067a 20 26 7a 52 65 74 2c 20 22 2c 20 78 2e 25 51 22   &zRet, ", x.%Q"
4067b 2c 20 70 2d 3e 7a 4c 61 6e 67 75 61 67 65 69 64  , p->zLanguageid
4067c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66  );.    }.  }.  f
4067d 74 73 33 41 70 70 65 6e 64 66 28 70 52 63 2c 20  ts3Appendf(pRc, 
4067e 26 7a 52 65 74 2c 20 22 20 46 52 4f 4d 20 27 25  &zRet, " FROM '%
4067f 71 27 2e 27 25 71 25 73 27 20 41 53 20 78 22 2c  q'.'%q%s' AS x",
40680 20 0a 20 20 20 20 20 20 70 2d 3e 7a 44 62 2c 0a   .      p->zDb,.
40681 20 20 20 20 20 20 28 70 2d 3e 7a 43 6f 6e 74 65        (p->zConte
40682 6e 74 54 62 6c 20 3f 20 70 2d 3e 7a 43 6f 6e 74  ntTbl ? p->zCont
40683 65 6e 74 54 62 6c 20 3a 20 70 2d 3e 7a 4e 61 6d  entTbl : p->zNam
40684 65 29 2c 0a 20 20 20 20 20 20 28 70 2d 3e 7a 43  e),.      (p->zC
40685 6f 6e 74 65 6e 74 54 62 6c 20 3f 20 22 22 20 3a  ontentTbl ? "" :
40686 20 22 5f 63 6f 6e 74 65 6e 74 22 29 0a 20 20 29   "_content").  )
40687 3b 0a 20 20 72 65 74 75 72 6e 20 7a 52 65 74 3b  ;.  return zRet;
40688 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
40689 20 61 20 6c 69 73 74 20 6f 66 20 4e 20 63 6f 6d   a list of N com
4068a 6d 61 20 73 65 70 61 72 61 74 65 64 20 71 75 65  ma separated que
4068b 73 74 69 6f 6e 20 6d 61 72 6b 73 2c 20 77 68 65  stion marks, whe
4068c 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62  re N is the numb
4068d 65 72 0a 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73  er.** of columns
4068e 20 69 6e 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e   in the %_conten
4068f 74 20 74 61 62 6c 65 20 28 6f 6e 65 20 66 6f 72  t table (one for
40690 20 74 68 65 20 64 6f 63 69 64 20 70 6c 75 73 20   the docid plus 
40691 6f 6e 65 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20  one for each.** 
40692 75 73 65 72 2d 64 65 66 69 6e 65 64 20 74 65 78  user-defined tex
40693 74 20 63 6f 6c 75 6d 6e 29 2e 0a 2a 2a 0a 2a 2a  t column)..**.**
40694 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a 46 75   If argument zFu
40695 6e 63 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  nc is not NULL, 
40696 74 68 65 6e 20 61 6c 6c 20 62 75 74 20 74 68 65  then all but the
40697 20 66 69 72 73 74 20 71 75 65 73 74 69 6f 6e 20   first question 
40698 6d 61 72 6b 0a 2a 2a 20 69 73 20 70 72 65 63 65  mark.** is prece
40699 64 65 64 20 62 79 20 7a 46 75 6e 63 20 61 6e 64  ded by zFunc and
4069a 20 61 6e 20 6f 70 65 6e 20 62 72 61 63 6b 65 74   an open bracket
4069b 2c 20 61 6e 64 20 66 6f 6c 6c 6f 77 65 64 20 62  , and followed b
4069c 79 20 61 20 63 6c 6f 73 65 64 0a 2a 2a 20 62 72  y a closed.** br
4069d 61 63 6b 65 74 2e 20 46 6f 72 20 65 78 61 6d 70  acket. For examp
4069e 6c 65 2c 20 69 66 20 7a 46 75 6e 63 20 69 73 20  le, if zFunc is 
4069f 22 7a 69 70 22 20 61 6e 64 20 74 68 65 20 46 54  "zip" and the FT
406a0 53 33 20 74 61 62 6c 65 20 68 61 73 20 74 68 72  S3 table has thr
406a1 65 65 20 0a 2a 2a 20 75 73 65 72 2d 64 65 66 69  ee .** user-defi
406a2 6e 65 64 20 74 65 78 74 20 63 6f 6c 75 6d 6e 73  ned text columns
406a3 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  , the following 
406a4 73 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e  string is return
406a5 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 3f  ed:.**.**     "?
406a6 2c 20 7a 69 70 28 3f 29 2c 20 7a 69 70 28 3f 29  , zip(?), zip(?)
406a7 2c 20 7a 69 70 28 3f 29 22 0a 2a 2a 0a 2a 2a 20  , zip(?)".**.** 
406a8 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75  The pointer retu
406a9 72 6e 65 64 20 70 6f 69 6e 74 73 20 74 6f 20 61  rned points to a
406aa 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65   buffer allocate
406ab 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c  d by sqlite3_mal
406ac 6c 6f 63 28 29 2e 20 49 74 0a 2a 2a 20 69 73 20  loc(). It.** is 
406ad 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  the responsibili
406ae 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72  ty of the caller
406af 20 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 66   to eventually f
406b0 72 65 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  ree it..**.** If
406b1 20 2a 70 52 63 20 69 73 20 6e 6f 74 20 53 51 4c   *pRc is not SQL
406b2 49 54 45 5f 4f 4b 20 77 68 65 6e 20 74 68 69 73  ITE_OK when this
406b3 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
406b4 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d  led, it is a no-
406b5 6f 70 20 28 61 6e 64 0a 2a 2a 20 61 20 4e 55 4c  op (and.** a NUL
406b6 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74  L pointer is ret
406b7 75 72 6e 65 64 29 2e 20 4f 74 68 65 72 77 69 73  urned). Otherwis
406b8 65 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 65 72 72  e, if an OOM err
406b9 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
406ba 64 0a 2a 2a 20 62 79 20 74 68 69 73 20 66 75 6e  d.** by this fun
406bb 63 74 69 6f 6e 2c 20 4e 55 4c 4c 20 69 73 20 72  ction, NULL is r
406bc 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 52 63  eturned and *pRc
406bd 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54   is set to SQLIT
406be 45 5f 4e 4f 4d 45 4d 2e 20 49 66 0a 2a 2a 20 6e  E_NOMEM. If.** n
406bf 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  o error occurs, 
406c0 2a 70 52 63 20 69 73 20 6c 65 66 74 20 75 6e 6d  *pRc is left unm
406c1 6f 64 69 66 69 65 64 2e 0a 2a 2f 0a 73 74 61 74  odified..*/.stat
406c2 69 63 20 63 68 61 72 20 2a 66 74 73 33 57 72 69  ic char *fts3Wri
406c3 74 65 45 78 70 72 4c 69 73 74 28 46 74 73 33 54  teExprList(Fts3T
406c4 61 62 6c 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63  able *p, const c
406c5 68 61 72 20 2a 7a 46 75 6e 63 2c 20 69 6e 74 20  har *zFunc, int 
406c6 2a 70 52 63 29 7b 0a 20 20 63 68 61 72 20 2a 7a  *pRc){.  char *z
406c7 52 65 74 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  Ret = 0;.  char 
406c8 2a 7a 46 72 65 65 20 3d 20 30 3b 0a 20 20 63 68  *zFree = 0;.  ch
406c9 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 3b 0a 20  ar *zFunction;. 
406ca 20 69 6e 74 20 69 3b 0a 0a 20 20 69 66 28 20 21   int i;..  if( !
406cb 7a 46 75 6e 63 20 29 7b 0a 20 20 20 20 7a 46 75  zFunc ){.    zFu
406cc 6e 63 74 69 6f 6e 20 3d 20 22 22 3b 0a 20 20 7d  nction = "";.  }
406cd 65 6c 73 65 7b 0a 20 20 20 20 7a 46 72 65 65 20  else{.    zFree 
406ce 3d 20 7a 46 75 6e 63 74 69 6f 6e 20 3d 20 66 74  = zFunction = ft
406cf 73 33 51 75 6f 74 65 49 64 28 7a 46 75 6e 63 29  s3QuoteId(zFunc)
406d0 3b 0a 20 20 7d 0a 20 20 66 74 73 33 41 70 70 65  ;.  }.  fts3Appe
406d1 6e 64 66 28 70 52 63 2c 20 26 7a 52 65 74 2c 20  ndf(pRc, &zRet, 
406d2 22 3f 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  "?");.  for(i=0;
406d3 20 69 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69   i<p->nColumn; i
406d4 2b 2b 29 7b 0a 20 20 20 20 66 74 73 33 41 70 70  ++){.    fts3App
406d5 65 6e 64 66 28 70 52 63 2c 20 26 7a 52 65 74 2c  endf(pRc, &zRet,
406d6 20 22 2c 25 73 28 3f 29 22 2c 20 7a 46 75 6e 63   ",%s(?)", zFunc
406d7 74 69 6f 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28  tion);.  }.  if(
406d8 20 70 2d 3e 7a 4c 61 6e 67 75 61 67 65 69 64 20   p->zLanguageid 
406d9 29 7b 0a 20 20 20 20 66 74 73 33 41 70 70 65 6e  ){.    fts3Appen
406da 64 66 28 70 52 63 2c 20 26 7a 52 65 74 2c 20 22  df(pRc, &zRet, "
406db 2c 20 3f 22 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  , ?");.  }.  sql
406dc 69 74 65 33 5f 66 72 65 65 28 7a 46 72 65 65 29  ite3_free(zFree)
406dd 3b 0a 20 20 72 65 74 75 72 6e 20 7a 52 65 74 3b  ;.  return zRet;
406de 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
406df 75 6e 63 74 69 6f 6e 20 69 6e 74 65 72 70 72 65  unction interpre
406e0 74 73 20 74 68 65 20 73 74 72 69 6e 67 20 61 74  ts the string at
406e1 20 28 2a 70 70 29 20 61 73 20 61 20 6e 6f 6e 2d   (*pp) as a non-
406e2 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67 65 72  negative integer
406e3 0a 2a 2a 20 76 61 6c 75 65 2e 20 49 74 20 72 65  .** value. It re
406e4 61 64 73 20 74 68 65 20 69 6e 74 65 67 65 72 20  ads the integer 
406e5 61 6e 64 20 73 65 74 73 20 2a 70 6e 4f 75 74 20  and sets *pnOut 
406e6 74 6f 20 74 68 65 20 76 61 6c 75 65 20 72 65 61  to the value rea
406e7 64 2c 20 74 68 65 6e 20 0a 2a 2a 20 73 65 74 73  d, then .** sets
406e8 20 2a 70 70 20 74 6f 20 70 6f 69 6e 74 20 74 6f   *pp to point to
406e9 20 74 68 65 20 62 79 74 65 20 69 6d 6d 65 64 69   the byte immedi
406ea 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20  ately following 
406eb 74 68 65 20 6c 61 73 74 20 62 79 74 65 20 6f 66  the last byte of
406ec 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20  .** the integer 
406ed 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c  value..**.** Onl
406ee 79 20 64 65 63 69 6d 61 6c 20 64 69 67 69 74 73  y decimal digits
406ef 20 28 27 30 27 2e 2e 27 39 27 29 20 6d 61 79 20   ('0'..'9') may 
406f0 62 65 20 70 61 72 74 20 6f 66 20 61 6e 20 69 6e  be part of an in
406f1 74 65 67 65 72 20 76 61 6c 75 65 2e 20 0a 2a 2a  teger value. .**
406f2 0a 2a 2a 20 49 66 20 2a 70 70 20 64 6f 65 73 20  .** If *pp does 
406f3 6e 6f 74 20 62 65 69 6e 67 20 77 69 74 68 20 61  not being with a
406f4 20 64 65 63 69 6d 61 6c 20 64 69 67 69 74 20 53   decimal digit S
406f5 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72  QLITE_ERROR is r
406f6 65 74 75 72 6e 65 64 20 61 6e 64 0a 2a 2a 20 74  eturned and.** t
406f7 68 65 20 6f 75 74 70 75 74 20 76 61 6c 75 65 20  he output value 
406f8 75 6e 64 65 66 69 6e 65 64 2e 20 4f 74 68 65 72  undefined. Other
406f9 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 20 69  wise SQLITE_OK i
406fa 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
406fb 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
406fc 69 73 20 75 73 65 64 20 77 68 65 6e 20 70 61 72  is used when par
406fd 73 69 6e 67 20 74 68 65 20 22 70 72 65 66 69 78  sing the "prefix
406fe 3d 22 20 46 54 53 34 20 70 61 72 61 6d 65 74 65  =" FTS4 paramete
406ff 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
40700 20 66 74 73 33 47 6f 62 62 6c 65 49 6e 74 28 63   fts3GobbleInt(c
40701 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 70 2c 20  onst char **pp, 
40702 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20 20 63  int *pnOut){.  c
40703 6f 6e 73 74 20 63 68 61 72 20 2a 70 3b 20 20 20  onst char *p;   
40704 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
40705 2a 20 49 74 65 72 61 74 6f 72 20 70 6f 69 6e 74  * Iterator point
40706 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 74  er */.  int nInt
40707 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
40708 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
40709 74 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 66 6f  t value */..  fo
4070a 72 28 70 3d 2a 70 70 3b 20 70 5b 30 5d 3e 3d 27  r(p=*pp; p[0]>='
4070b 30 27 20 26 26 20 70 5b 30 5d 3c 3d 27 39 27 3b  0' && p[0]<='9';
4070c 20 70 2b 2b 29 7b 0a 20 20 20 20 6e 49 6e 74 20   p++){.    nInt 
4070d 3d 20 6e 49 6e 74 20 2a 20 31 30 20 2b 20 28 70  = nInt * 10 + (p
4070e 5b 30 5d 20 2d 20 27 30 27 29 3b 0a 20 20 7d 0a  [0] - '0');.  }.
4070f 20 20 69 66 28 20 70 3d 3d 2a 70 70 20 29 20 72    if( p==*pp ) r
40710 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
40711 4f 52 3b 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 6e  OR;.  *pnOut = n
40712 49 6e 74 3b 0a 20 20 2a 70 70 20 3d 20 70 3b 0a  Int;.  *pp = p;.
40713 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
40714 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  OK;.}../*.** Thi
40715 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
40716 6c 6c 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  lled to allocate
40717 20 61 6e 20 61 72 72 61 79 20 6f 66 20 46 74 73   an array of Fts
40718 33 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 65  3Index structure
40719 73 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 69 6e  s.** representin
4071a 67 20 74 68 65 20 69 6e 64 65 78 65 73 20 6d 61  g the indexes ma
4071b 69 6e 74 61 69 6e 65 64 20 62 79 20 74 68 65 20  intained by the 
4071c 63 75 72 72 65 6e 74 20 46 54 53 20 74 61 62 6c  current FTS tabl
4071d 65 2e 20 46 54 53 20 74 61 62 6c 65 73 0a 2a 2a  e. FTS tables.**
4071e 20 61 6c 77 61 79 73 20 6d 61 69 6e 74 61 69 6e   always maintain
4071f 20 74 68 65 20 6d 61 69 6e 20 22 74 65 72 6d 73   the main "terms
40720 22 20 69 6e 64 65 78 2c 20 62 75 74 20 6d 61 79  " index, but may
40721 20 61 6c 73 6f 20 6d 61 69 6e 74 61 69 6e 20 6f   also maintain o
40722 6e 65 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 22 70  ne or.** more "p
40723 72 65 66 69 78 22 20 69 6e 64 65 78 65 73 2c 20  refix" indexes, 
40724 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65  depending on the
40725 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 22 70   value of the "p
40726 72 65 66 69 78 3d 22 20 70 61 72 61 6d 65 74 65  refix=" paramete
40727 72 0a 2a 2a 20 28 69 66 20 61 6e 79 29 20 73 70  r.** (if any) sp
40728 65 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20  ecified as part 
40729 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49  of the CREATE VI
4072a 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74  RTUAL TABLE stat
4072b 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 41 72 67  ement..**.** Arg
4072c 75 6d 65 6e 74 20 7a 50 61 72 61 6d 20 69 73 20  ument zParam is 
4072d 70 61 73 73 65 64 20 74 68 65 20 76 61 6c 75 65  passed the value
4072e 20 6f 66 20 74 68 65 20 22 70 72 65 66 69 78 3d   of the "prefix=
4072f 22 20 6f 70 74 69 6f 6e 20 69 66 20 6f 6e 65 20  " option if one 
40730 77 61 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64  was.** specified
40731 2c 20 6f 72 20 4e 55 4c 4c 20 6f 74 68 65 72 77  , or NULL otherw
40732 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f  ise..**.** If no
40733 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 53   error occurs, S
40734 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
40735 72 6e 65 64 20 61 6e 64 20 2a 61 70 49 6e 64 65  rned and *apInde
40736 78 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74  x set to point t
40737 6f 0a 2a 2a 20 74 68 65 20 61 6c 6c 6f 63 61 74  o.** the allocat
40738 65 64 20 61 72 72 61 79 2e 20 2a 70 6e 49 6e 64  ed array. *pnInd
40739 65 78 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ex is set to the
4073a 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   number of eleme
4073b 6e 74 73 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72  nts in the.** ar
4073c 72 61 79 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ray. If an error
4073d 20 64 6f 65 73 20 6f 63 63 75 72 2c 20 61 6e 20   does occur, an 
4073e 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
4073f 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
40740 2a 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20  *.** Regardless 
40741 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  of whether or no
40742 74 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65  t an error is re
40743 74 75 72 6e 65 64 2c 20 69 74 20 69 73 20 74 68  turned, it is th
40744 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79  e responsibility
40745 0a 2a 2a 20 6f 66 20 74 68 65 20 63 61 6c 6c 65  .** of the calle
40746 72 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65  r to call sqlite
40747 33 5f 66 72 65 65 28 29 20 6f 6e 20 74 68 65 20  3_free() on the 
40748 6f 75 74 70 75 74 20 61 72 72 61 79 20 74 6f 20  output array to 
40749 66 72 65 65 20 69 74 2e 0a 2a 2f 0a 73 74 61 74  free it..*/.stat
4074a 69 63 20 69 6e 74 20 66 74 73 33 50 72 65 66 69  ic int fts3Prefi
4074b 78 50 61 72 61 6d 65 74 65 72 28 0a 20 20 63 6f  xParameter(.  co
4074c 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 72 61 6d  nst char *zParam
4074d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
4074e 20 41 42 43 20 69 6e 20 70 72 65 66 69 78 3d 41   ABC in prefix=A
4074f 42 43 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  BC parameter to 
40750 70 61 72 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a  parse */.  int *
40751 70 6e 49 6e 64 65 78 2c 20 20 20 20 20 20 20 20  pnIndex,        
40752 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
40753 54 3a 20 73 69 7a 65 20 6f 66 20 2a 61 70 49 6e  T: size of *apIn
40754 64 65 78 5b 5d 20 61 72 72 61 79 20 2a 2f 0a 20  dex[] array */. 
40755 20 73 74 72 75 63 74 20 46 74 73 33 49 6e 64 65   struct Fts3Inde
40756 78 20 2a 2a 61 70 49 6e 64 65 78 20 20 20 20 20  x **apIndex     
40757 20 2f 2a 20 4f 55 54 3a 20 41 72 72 61 79 20 6f   /* OUT: Array o
40758 66 20 69 6e 64 65 78 65 73 20 66 6f 72 20 74 68  f indexes for th
40759 69 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  is table */.){. 
4075a 20 73 74 72 75 63 74 20 46 74 73 33 49 6e 64 65   struct Fts3Inde
4075b 78 20 2a 61 49 6e 64 65 78 3b 20 20 20 20 20 20  x *aIndex;      
4075c 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 61 72   /* Allocated ar
4075d 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e  ray */.  int nIn
4075e 64 65 78 20 3d 20 31 3b 20 20 20 20 20 20 20 20  dex = 1;        
4075f 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
40760 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
40761 20 61 72 72 61 79 20 2a 2f 0a 0a 20 20 69 66 28   array */..  if(
40762 20 7a 50 61 72 61 6d 20 26 26 20 7a 50 61 72 61   zParam && zPara
40763 6d 5b 30 5d 20 29 7b 0a 20 20 20 20 63 6f 6e 73  m[0] ){.    cons
40764 74 20 63 68 61 72 20 2a 70 3b 0a 20 20 20 20 6e  t char *p;.    n
40765 49 6e 64 65 78 2b 2b 3b 0a 20 20 20 20 66 6f 72  Index++;.    for
40766 28 70 3d 7a 50 61 72 61 6d 3b 20 2a 70 3b 20 70  (p=zParam; *p; p
40767 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 2a  ++){.      if( *
40768 70 3d 3d 27 2c 27 20 29 20 6e 49 6e 64 65 78 2b  p==',' ) nIndex+
40769 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  +;.    }.  }..  
4076a 61 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  aIndex = sqlite3
4076b 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 73  _malloc(sizeof(s
4076c 74 72 75 63 74 20 46 74 73 33 49 6e 64 65 78 29  truct Fts3Index)
4076d 20 2a 20 6e 49 6e 64 65 78 29 3b 0a 20 20 2a 61   * nIndex);.  *a
4076e 70 49 6e 64 65 78 20 3d 20 61 49 6e 64 65 78 3b  pIndex = aIndex;
4076f 0a 20 20 2a 70 6e 49 6e 64 65 78 20 3d 20 6e 49  .  *pnIndex = nI
40770 6e 64 65 78 3b 0a 20 20 69 66 28 20 21 61 49 6e  ndex;.  if( !aIn
40771 64 65 78 20 29 7b 0a 20 20 20 20 72 65 74 75 72  dex ){.    retur
40772 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
40773 20 20 7d 0a 0a 20 20 6d 65 6d 73 65 74 28 61 49    }..  memset(aI
40774 6e 64 65 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28  ndex, 0, sizeof(
40775 73 74 72 75 63 74 20 46 74 73 33 49 6e 64 65 78  struct Fts3Index
40776 29 20 2a 20 6e 49 6e 64 65 78 29 3b 0a 20 20 69  ) * nIndex);.  i
40777 66 28 20 7a 50 61 72 61 6d 20 29 7b 0a 20 20 20  f( zParam ){.   
40778 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 20 3d   const char *p =
40779 20 7a 50 61 72 61 6d 3b 0a 20 20 20 20 69 6e 74   zParam;.    int
4077a 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b   i;.    for(i=1;
4077b 20 69 3c 6e 49 6e 64 65 78 3b 20 69 2b 2b 29 7b   i<nIndex; i++){
4077c 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 72 65 66  .      int nPref
4077d 69 78 3b 0a 20 20 20 20 20 20 69 66 28 20 66 74  ix;.      if( ft
4077e 73 33 47 6f 62 62 6c 65 49 6e 74 28 26 70 2c 20  s3GobbleInt(&p, 
4077f 26 6e 50 72 65 66 69 78 29 20 29 20 72 65 74 75  &nPrefix) ) retu
40780 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
40781 0a 20 20 20 20 20 20 61 49 6e 64 65 78 5b 69 5d  .      aIndex[i]
40782 2e 6e 50 72 65 66 69 78 20 3d 20 6e 50 72 65 66  .nPrefix = nPref
40783 69 78 3b 0a 20 20 20 20 20 20 70 2b 2b 3b 0a 20  ix;.      p++;. 
40784 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
40785 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
40786 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
40787 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77  tion is called w
40788 68 65 6e 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67  hen initializing
40789 20 61 6e 20 46 54 53 34 20 74 61 62 6c 65 20 74   an FTS4 table t
4078a 68 61 74 20 75 73 65 73 20 74 68 65 0a 2a 2a 20  hat uses the.** 
4078b 63 6f 6e 74 65 6e 74 3d 78 78 78 20 6f 70 74 69  content=xxx opti
4078c 6f 6e 2e 20 49 74 20 64 65 74 65 72 6d 69 6e 65  on. It determine
4078d 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
4078e 61 6e 64 20 6e 61 6d 65 73 20 6f 66 20 74 68 65  and names of the
4078f 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 6f 66 20 74   columns.** of t
40790 68 65 20 6e 65 77 20 46 54 53 34 20 74 61 62 6c  he new FTS4 tabl
40791 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69  e..**.** The thi
40792 72 64 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73  rd argument pass
40793 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  ed to this funct
40794 69 6f 6e 20 69 73 20 74 68 65 20 76 61 6c 75 65  ion is the value
40795 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 0a 2a   passed to the.*
40796 2a 20 63 6f 6e 66 69 67 3d 78 78 78 20 6f 70 74  * config=xxx opt
40797 69 6f 6e 20 28 69 2e 65 2e 20 22 78 78 78 22 29  ion (i.e. "xxx")
40798 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
40799 71 75 65 72 69 65 73 20 74 68 65 20 64 61 74 61  queries the data
4079a 62 61 73 65 20 66 6f 72 0a 2a 2a 20 61 20 74 61  base for.** a ta
4079b 62 6c 65 20 6f 66 20 74 68 61 74 20 6e 61 6d 65  ble of that name
4079c 2e 20 49 66 20 66 6f 75 6e 64 2c 20 74 68 65 20  . If found, the 
4079d 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 73  output variables
4079e 20 61 72 65 20 70 6f 70 75 6c 61 74 65 64 0a 2a   are populated.*
4079f 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  * as follows:.**
407a0 0a 2a 2a 20 20 20 2a 70 6e 43 6f 6c 3a 20 20 20  .**   *pnCol:   
407a1 53 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  Set to the numbe
407a2 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 61 62  r of columns tab
407a3 6c 65 20 78 78 78 20 68 61 73 2c 0a 2a 2a 0a 2a  le xxx has,.**.*
407a4 2a 20 20 20 2a 70 6e 53 74 72 3a 20 20 20 53 65  *   *pnStr:   Se
407a5 74 20 74 6f 20 74 68 65 20 74 6f 74 61 6c 20 61  t to the total a
407a6 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20 72  mount of space r
407a7 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65  equired to store
407a8 20 61 20 63 6f 70 79 0a 2a 2a 20 20 20 20 20 20   a copy.**      
407a9 20 20 20 20 20 20 20 6f 66 20 65 61 63 68 20 63         of each c
407aa 6f 6c 75 6d 6e 73 20 6e 61 6d 65 2c 20 69 6e 63  olumns name, inc
407ab 6c 75 64 69 6e 67 20 74 68 65 20 6e 75 6c 2d 74  luding the nul-t
407ac 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a  erminator..**.**
407ad 20 20 20 2a 70 61 7a 43 6f 6c 3a 20 20 53 65 74     *pazCol:  Set
407ae 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20   to point to an 
407af 61 72 72 61 79 20 6f 66 20 2a 70 6e 43 6f 6c 20  array of *pnCol 
407b0 73 74 72 69 6e 67 73 2e 20 45 61 63 68 20 73 74  strings. Each st
407b1 72 69 6e 67 20 69 73 0a 2a 2a 20 20 20 20 20 20  ring is.**      
407b2 20 20 20 20 20 20 20 74 68 65 20 6e 61 6d 65 20         the name 
407b3 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  of the correspon
407b4 64 69 6e 67 20 63 6f 6c 75 6d 6e 20 69 6e 20 74  ding column in t
407b5 61 62 6c 65 20 78 78 78 2e 20 54 68 65 20 61 72  able xxx. The ar
407b6 72 61 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ray.**          
407b7 20 20 20 61 6e 64 20 69 74 73 20 63 6f 6e 74 65     and its conte
407b8 6e 74 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65  nts are allocate
407b9 64 20 75 73 69 6e 67 20 61 20 73 69 6e 67 6c 65  d using a single
407ba 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 49 74 0a   allocation. It.
407bb 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 69  **             i
407bc 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69  s the responsibi
407bd 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c  lity of the call
407be 65 72 20 74 6f 20 66 72 65 65 20 74 68 69 73 20  er to free this 
407bf 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 20 20  allocation.**   
407c0 20 20 20 20 20 20 20 20 20 20 62 79 20 65 76 65            by eve
407c1 6e 74 75 61 6c 6c 79 20 70 61 73 73 69 6e 67 20  ntually passing 
407c2 74 68 65 20 2a 70 61 7a 43 6f 6c 20 76 61 6c 75  the *pazCol valu
407c3 65 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65  e to sqlite3_fre
407c4 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  e()..**.** If th
407c5 65 20 74 61 62 6c 65 20 63 61 6e 6e 6f 74 20 62  e table cannot b
407c6 65 20 66 6f 75 6e 64 2c 20 61 6e 20 65 72 72 6f  e found, an erro
407c7 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
407c8 65 64 20 61 6e 64 20 74 68 65 20 6f 75 74 70 75  ed and the outpu
407c9 74 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 61  t.** variables a
407ca 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 20 4f 72  re undefined. Or
407cb 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 69 73 20 65  , if an OOM is e
407cc 6e 63 6f 75 6e 74 65 72 65 64 2c 20 53 51 4c 49  ncountered, SQLI
407cd 54 45 5f 4e 4f 4d 45 4d 20 69 73 0a 2a 2a 20 72  TE_NOMEM is.** r
407ce 65 74 75 72 6e 65 64 20 28 61 6e 64 20 74 68 65  eturned (and the
407cf 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65   output variable
407d0 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 29  s are undefined)
407d1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
407d2 66 74 73 33 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  fts3ContentColum
407d3 6e 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ns(.  sqlite3 *d
407d4 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
407d5 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
407d6 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  e handle */.  co
407d7 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 20  nst char *zDb,  
407d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
407d9 20 4e 61 6d 65 20 6f 66 20 64 62 20 28 69 2e 65   Name of db (i.e
407da 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22  . "main", "temp"
407db 20 65 74 63 2e 29 20 2a 2f 0a 20 20 63 6f 6e 73   etc.) */.  cons
407dc 74 20 63 68 61 72 20 2a 7a 54 62 6c 2c 20 20 20  t char *zTbl,   
407dd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
407de 61 6d 65 20 6f 66 20 63 6f 6e 74 65 6e 74 20 74  ame of content t
407df 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  able */.  const 
407e0 63 68 61 72 20 2a 2a 2a 70 61 7a 43 6f 6c 2c 20  char ***pazCol, 
407e1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
407e2 3a 20 4d 61 6c 6c 6f 63 27 64 20 61 72 72 61 79  : Malloc'd array
407e3 20 6f 66 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73   of column names
407e4 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c   */.  int *pnCol
407e5 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
407e6 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 69        /* OUT: Si
407e7 7a 65 20 6f 66 20 61 72 72 61 79 20 2a 70 61 7a  ze of array *paz
407e8 43 6f 6c 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  Col */.  int *pn
407e9 53 74 72 20 20 20 20 20 20 20 20 20 20 20 20 20  Str             
407ea 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
407eb 20 42 79 74 65 73 20 6f 66 20 73 74 72 69 6e 67   Bytes of string
407ec 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 29 7b 0a 20   content */.){. 
407ed 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
407ee 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
407ef 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
407f0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b  */.  char *zSql;
407f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
407f2 20 20 20 20 20 2f 2a 20 22 53 45 4c 45 43 54 20       /* "SELECT 
407f3 2a 22 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20  *" statement on 
407f4 7a 54 62 6c 20 2a 2f 20 20 0a 20 20 73 71 6c 69  zTbl */  .  sqli
407f5 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 20  te3_stmt *pStmt 
407f6 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  = 0;        /* C
407f7 6f 6d 70 69 6c 65 64 20 76 65 72 73 69 6f 6e 20  ompiled version 
407f8 6f 66 20 7a 53 71 6c 20 2a 2f 0a 0a 20 20 7a 53  of zSql */..  zS
407f9 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ql = sqlite3_mpr
407fa 69 6e 74 66 28 22 53 45 4c 45 43 54 20 2a 20 46  intf("SELECT * F
407fb 52 4f 4d 20 25 51 2e 25 51 22 2c 20 7a 44 62 2c  ROM %Q.%Q", zDb,
407fc 20 7a 54 62 6c 29 3b 0a 20 20 69 66 28 20 21 7a   zTbl);.  if( !z
407fd 53 71 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  Sql ){.    rc = 
407fe 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
407ff 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
40800 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
40801 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70  db, zSql, -1, &p
40802 53 74 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  Stmt, 0);.  }.  
40803 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71  sqlite3_free(zSq
40804 6c 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53  l);..  if( rc==S
40805 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
40806 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 61 7a 43  const char **azC
40807 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ol;           /*
40808 20 4f 75 74 70 75 74 20 61 72 72 61 79 20 2a 2f   Output array */
40809 0a 20 20 20 20 69 6e 74 20 6e 53 74 72 20 3d 20  .    int nStr = 
4080a 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
4080b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c     /* Size of al
4080c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 28  l column names (
4080d 69 6e 63 6c 2e 20 30 78 30 30 29 20 2a 2f 0a 20  incl. 0x00) */. 
4080e 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20     int nCol;    
4080f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40810 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61   /* Number of ta
40811 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20  ble columns */. 
40812 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20     int i;       
40813 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40814 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72   /* Used to iter
40815 61 74 65 20 74 68 72 6f 75 67 68 20 63 6f 6c 75  ate through colu
40816 6d 6e 73 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4c  mns */..    /* L
40817 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20  oop through the 
40818 72 65 74 75 72 6e 65 64 20 63 6f 6c 75 6d 6e 73  returned columns
40819 2e 20 53 65 74 20 6e 53 74 72 20 74 6f 20 74 68  . Set nStr to th
4081a 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
4081b 73 20 6f 66 0a 20 20 20 20 2a 2a 20 73 70 61 63  s of.    ** spac
4081c 65 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74  e required to st
4081d 6f 72 65 20 61 20 63 6f 70 79 20 6f 66 20 65 61  ore a copy of ea
4081e 63 68 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2c 20  ch column name, 
4081f 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 0a 20 20  including the.  
40820 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61    ** nul-termina
40821 74 6f 72 20 62 79 74 65 2e 20 20 2a 2f 0a 20 20  tor byte.  */.  
40822 20 20 6e 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33    nCol = sqlite3
40823 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53  _column_count(pS
40824 74 6d 74 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  tmt);.    for(i=
40825 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
40826 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
40827 72 20 2a 7a 43 6f 6c 20 3d 20 73 71 6c 69 74 65  r *zCol = sqlite
40828 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 70 53  3_column_name(pS
40829 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 6e  tmt, i);.      n
4082a 53 74 72 20 2b 3d 20 28 69 6e 74 29 73 74 72 6c  Str += (int)strl
4082b 65 6e 28 7a 43 6f 6c 29 20 2b 20 31 3b 0a 20 20  en(zCol) + 1;.  
4082c 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f    }..    /* Allo
4082d 63 61 74 65 20 61 6e 64 20 70 6f 70 75 6c 61 74  cate and populat
4082e 65 20 74 68 65 20 61 72 72 61 79 20 74 6f 20 72  e the array to r
4082f 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 20 20 61 7a  eturn. */.    az
40830 43 6f 6c 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  Col = (const cha
40831 72 20 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  r **)sqlite3_mal
40832 6c 6f 63 28 73 69 7a 65 6f 66 28 63 68 61 72 20  loc(sizeof(char 
40833 2a 29 20 2a 20 6e 43 6f 6c 20 2b 20 6e 53 74 72  *) * nCol + nStr
40834 29 3b 0a 20 20 20 20 69 66 28 20 61 7a 43 6f 6c  );.    if( azCol
40835 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ==0 ){.      rc 
40836 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
40837 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
40838 20 63 68 61 72 20 2a 70 20 3d 20 28 63 68 61 72   char *p = (char
40839 20 2a 29 26 61 7a 43 6f 6c 5b 6e 43 6f 6c 5d 3b   *)&azCol[nCol];
4083a 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
4083b 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
4083c 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
4083d 20 2a 7a 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33   *zCol = sqlite3
4083e 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 70 53 74  _column_name(pSt
4083f 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 20 20  mt, i);.        
40840 69 6e 74 20 6e 20 3d 20 28 69 6e 74 29 73 74 72  int n = (int)str
40841 6c 65 6e 28 7a 43 6f 6c 29 2b 31 3b 0a 20 20 20  len(zCol)+1;.   
40842 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 7a       memcpy(p, z
40843 43 6f 6c 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20  Col, n);.       
40844 20 61 7a 43 6f 6c 5b 69 5d 20 3d 20 70 3b 0a 20   azCol[i] = p;. 
40845 20 20 20 20 20 20 20 70 20 2b 3d 20 6e 3b 0a 20         p += n;. 
40846 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
40847 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
40848 65 28 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20 2f  e(pStmt);..    /
40849 2a 20 53 65 74 20 74 68 65 20 6f 75 74 70 75 74  * Set the output
4084a 20 76 61 72 69 61 62 6c 65 73 2e 20 2a 2f 0a 20   variables. */. 
4084b 20 20 20 2a 70 6e 43 6f 6c 20 3d 20 6e 43 6f 6c     *pnCol = nCol
4084c 3b 0a 20 20 20 20 2a 70 6e 53 74 72 20 3d 20 6e  ;.    *pnStr = n
4084d 53 74 72 3b 0a 20 20 20 20 2a 70 61 7a 43 6f 6c  Str;.    *pazCol
4084e 20 3d 20 61 7a 43 6f 6c 3b 0a 20 20 7d 0a 0a 20   = azCol;.  }.. 
4084f 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
40850 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
40851 6f 6e 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d  on is the implem
40852 65 6e 74 61 74 69 6f 6e 20 6f 66 20 62 6f 74 68  entation of both
40853 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 20 61 6e   the xConnect an
40854 64 20 78 43 72 65 61 74 65 0a 2a 2a 20 6d 65 74  d xCreate.** met
40855 68 6f 64 73 20 6f 66 20 74 68 65 20 46 54 53 33  hods of the FTS3
40856 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a   virtual table..
40857 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 67 76 5b 5d  **.** The argv[]
40858 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20   array contains 
40859 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a  the following:.*
4085a 2a 0a 2a 2a 20 20 20 61 72 67 76 5b 30 5d 20 20  *.**   argv[0]  
4085b 20 2d 3e 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20   -> module name 
4085c 20 28 22 66 74 73 33 22 20 6f 72 20 22 66 74 73   ("fts3" or "fts
4085d 34 22 29 0a 2a 2a 20 20 20 61 72 67 76 5b 31 5d  4").**   argv[1]
4085e 20 20 20 2d 3e 20 64 61 74 61 62 61 73 65 20 6e     -> database n
4085f 61 6d 65 0a 2a 2a 20 20 20 61 72 67 76 5b 32 5d  ame.**   argv[2]
40860 20 20 20 2d 3e 20 74 61 62 6c 65 20 6e 61 6d 65     -> table name
40861 0a 2a 2a 20 20 20 61 72 67 76 5b 2e 2e 2e 5d 20  .**   argv[...] 
40862 2d 3e 20 22 63 6f 6c 75 6d 6e 20 6e 61 6d 65 22  -> "column name"
40863 20 61 6e 64 20 6f 74 68 65 72 20 6d 6f 64 75 6c   and other modul
40864 65 20 61 72 67 75 6d 65 6e 74 20 66 69 65 6c 64  e argument field
40865 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
40866 20 66 74 73 33 49 6e 69 74 56 74 61 62 28 0a 20   fts3InitVtab(. 
40867 20 69 6e 74 20 69 73 43 72 65 61 74 65 2c 20 20   int isCreate,  
40868 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40869 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 78 43 72   /* True for xCr
4086a 65 61 74 65 2c 20 66 61 6c 73 65 20 66 6f 72 20  eate, false for 
4086b 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 73 71  xConnect */.  sq
4086c 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
4086d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4086e 20 54 68 65 20 53 51 4c 69 74 65 20 64 61 74 61   The SQLite data
4086f 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
40870 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c  */.  void *pAux,
40871 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40872 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62       /* Hash tab
40873 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 6f  le containing to
40874 6b 65 6e 69 7a 65 72 73 20 2a 2f 0a 20 20 69 6e  kenizers */.  in
40875 74 20 61 72 67 63 2c 20 20 20 20 20 20 20 20 20  t argc,         
40876 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
40877 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
40878 6e 74 73 20 69 6e 20 61 72 67 76 20 61 72 72 61  nts in argv arra
40879 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  y */.  const cha
4087a 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c  r * const *argv,
4087b 20 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74         /* xCreat
4087c 65 2f 78 43 6f 6e 6e 65 63 74 20 61 72 67 75 6d  e/xConnect argum
4087d 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 73  ent array */.  s
4087e 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70  qlite3_vtab **pp
4087f 56 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f  VTab,          /
40880 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 73 75  * Write the resu
40881 6c 74 69 6e 67 20 76 74 61 62 20 73 74 72 75 63  lting vtab struc
40882 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63  ture here */.  c
40883 68 61 72 20 2a 2a 70 7a 45 72 72 20 20 20 20 20  har **pzErr     
40884 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
40885 2a 20 57 72 69 74 65 20 61 6e 79 20 65 72 72 6f  * Write any erro
40886 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 2a  r message here *
40887 2f 0a 29 7b 0a 20 20 46 74 73 33 48 61 73 68 20  /.){.  Fts3Hash 
40888 2a 70 48 61 73 68 20 3d 20 28 46 74 73 33 48 61  *pHash = (Fts3Ha
40889 73 68 20 2a 29 70 41 75 78 3b 0a 20 20 46 74 73  sh *)pAux;.  Fts
4088a 33 54 61 62 6c 65 20 2a 70 20 3d 20 30 3b 20 20  3Table *p = 0;  
4088b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4088c 50 6f 69 6e 74 65 72 20 74 6f 20 61 6c 6c 6f 63  Pointer to alloc
4088d 61 74 65 64 20 76 74 61 62 20 2a 2f 0a 20 20 69  ated vtab */.  i
4088e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
4088f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  K;             /
40890 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
40891 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
40892 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40893 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
40894 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  ariable */.  int
40895 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20   nByte;         
40896 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
40897 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69  Size of allocati
40898 6f 6e 20 75 73 65 64 20 66 6f 72 20 2a 70 20 2a  on used for *p *
40899 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20  /.  int iCol;   
4089a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4089b 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 69 6e      /* Column in
4089c 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 74  dex */.  int nSt
4089d 72 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20  ring = 0;       
4089e 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
4089f 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 68 6f  s required to ho
408a0 6c 64 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61  ld all column na
408a1 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  mes */.  int nCo
408a2 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  l = 0;          
408a3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
408a4 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
408a5 20 74 68 65 20 46 54 53 20 74 61 62 6c 65 20 2a   the FTS table *
408a6 2f 0a 20 20 63 68 61 72 20 2a 7a 43 73 72 3b 20  /.  char *zCsr; 
408a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
408a8 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72      /* Space for
408a9 20 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e 20   holding column 
408aa 6e 61 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  names */.  int n
408ab 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
408ac 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
408ad 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20  tes required to 
408ae 68 6f 6c 64 20 64 61 74 61 62 61 73 65 20 6e 61  hold database na
408af 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d  me */.  int nNam
408b0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
408b1 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
408b2 20 72 65 71 75 69 72 65 64 20 74 6f 20 68 6f 6c   required to hol
408b3 64 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  d table name */.
408b4 20 20 69 6e 74 20 69 73 46 74 73 34 20 3d 20 28    int isFts4 = (
408b5 61 72 67 76 5b 30 5d 5b 33 5d 3d 3d 27 34 27 29  argv[0][3]=='4')
408b6 3b 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 46 54  ; /* True for FT
408b7 53 34 2c 20 66 61 6c 73 65 20 66 6f 72 20 46 54  S4, false for FT
408b8 53 33 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  S3 */.  const ch
408b9 61 72 20 2a 2a 61 43 6f 6c 3b 20 20 20 20 20 20  ar **aCol;      
408ba 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79          /* Array
408bb 20 6f 66 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73   of column names
408bc 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f   */.  sqlite3_to
408bd 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
408be 7a 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20  zer = 0;        
408bf 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 66 6f 72  /* Tokenizer for
408c0 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 0a   this table */..
408c1 20 20 69 6e 74 20 6e 49 6e 64 65 78 3b 20 20 20    int nIndex;   
408c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
408c3 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 49 6e    /* Size of aIn
408c4 64 65 78 5b 5d 20 61 72 72 61 79 20 2a 2f 0a 20  dex[] array */. 
408c5 20 73 74 72 75 63 74 20 46 74 73 33 49 6e 64 65   struct Fts3Inde
408c6 78 20 2a 61 49 6e 64 65 78 20 3d 20 30 3b 20 20  x *aIndex = 0;  
408c7 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 69 6e 64   /* Array of ind
408c8 65 78 65 73 20 66 6f 72 20 74 68 69 73 20 74 61  exes for this ta
408c9 62 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65  ble */..  /* The
408ca 20 72 65 73 75 6c 74 73 20 6f 66 20 70 61 72 73   results of pars
408cb 69 6e 67 20 73 75 70 70 6f 72 74 65 64 20 46 54  ing supported FT
408cc 53 34 20 6b 65 79 3d 76 61 6c 75 65 20 6f 70 74  S4 key=value opt
408cd 69 6f 6e 73 3a 20 2a 2f 0a 20 20 69 6e 74 20 62  ions: */.  int b
408ce 4e 6f 44 6f 63 73 69 7a 65 20 3d 20 30 3b 20 20  NoDocsize = 0;  
408cf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
408d0 75 65 20 74 6f 20 6f 6d 69 74 20 25 5f 64 6f 63  ue to omit %_doc
408d1 73 69 7a 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  size table */.  
408d2 69 6e 74 20 62 44 65 73 63 49 64 78 20 3d 20 30  int bDescIdx = 0
408d3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
408d4 2f 2a 20 54 72 75 65 20 74 6f 20 73 74 6f 72 65  /* True to store
408d5 20 64 65 73 63 65 6e 64 69 6e 67 20 69 6e 64 65   descending inde
408d6 78 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  xes */.  char *z
408d7 50 72 65 66 69 78 20 3d 20 30 3b 20 20 20 20 20  Prefix = 0;     
408d8 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66           /* Pref
408d9 69 78 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c  ix parameter val
408da 75 65 20 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a  ue (or NULL) */.
408db 20 20 63 68 61 72 20 2a 7a 43 6f 6d 70 72 65 73    char *zCompres
408dc 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  s = 0;          
408dd 20 20 2f 2a 20 63 6f 6d 70 72 65 73 73 3d 3f 20    /* compress=? 
408de 70 61 72 61 6d 65 74 65 72 20 28 6f 72 20 4e 55  parameter (or NU
408df 4c 4c 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  LL) */.  char *z
408e0 55 6e 63 6f 6d 70 72 65 73 73 20 3d 20 30 3b 20  Uncompress = 0; 
408e1 20 20 20 20 20 20 20 20 20 2f 2a 20 75 6e 63 6f           /* unco
408e2 6d 70 72 65 73 73 3d 3f 20 70 61 72 61 6d 65 74  mpress=? paramet
408e3 65 72 20 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a  er (or NULL) */.
408e4 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 6e 74    char *zContent
408e5 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
408e6 20 20 2f 2a 20 63 6f 6e 74 65 6e 74 3d 3f 20 70    /* content=? p
408e7 61 72 61 6d 65 74 65 72 20 28 6f 72 20 4e 55 4c  arameter (or NUL
408e8 4c 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4c  L) */.  char *zL
408e9 61 6e 67 75 61 67 65 69 64 20 3d 20 30 3b 20 20  anguageid = 0;  
408ea 20 20 20 20 20 20 20 20 2f 2a 20 6c 61 6e 67 75          /* langu
408eb 61 67 65 69 64 3d 3f 20 70 61 72 61 6d 65 74 65  ageid=? paramete
408ec 72 20 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 0a  r (or NULL) */..
408ed 20 20 61 73 73 65 72 74 28 20 73 74 72 6c 65 6e    assert( strlen
408ee 28 61 72 67 76 5b 30 5d 29 3d 3d 34 20 29 3b 0a  (argv[0])==4 );.
408ef 20 20 61 73 73 65 72 74 28 20 28 73 71 6c 69 74    assert( (sqlit
408f0 65 33 5f 73 74 72 6e 69 63 6d 70 28 61 72 67 76  e3_strnicmp(argv
408f1 5b 30 5d 2c 20 22 66 74 73 34 22 2c 20 34 29 3d  [0], "fts4", 4)=
408f2 3d 30 20 26 26 20 69 73 46 74 73 34 29 0a 20 20  =0 && isFts4).  
408f3 20 20 20 20 20 7c 7c 20 28 73 71 6c 69 74 65 33       || (sqlite3
408f4 5f 73 74 72 6e 69 63 6d 70 28 61 72 67 76 5b 30  _strnicmp(argv[0
408f5 5d 2c 20 22 66 74 73 33 22 2c 20 34 29 3d 3d 30  ], "fts3", 4)==0
408f6 20 26 26 20 21 69 73 46 74 73 34 29 0a 20 20 29   && !isFts4).  )
408f7 3b 0a 0a 20 20 6e 44 62 20 3d 20 28 69 6e 74 29  ;..  nDb = (int)
408f8 73 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d 29 20  strlen(argv[1]) 
408f9 2b 20 31 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 28  + 1;.  nName = (
408fa 69 6e 74 29 73 74 72 6c 65 6e 28 61 72 67 76 5b  int)strlen(argv[
408fb 32 5d 29 20 2b 20 31 3b 0a 0a 20 20 61 43 6f 6c  2]) + 1;..  aCol
408fc 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
408fd 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
408fe 28 73 69 7a 65 6f 66 28 63 6f 6e 73 74 20 63 68  (sizeof(const ch
408ff 61 72 20 2a 29 20 2a 20 28 61 72 67 63 2d 32 29  ar *) * (argc-2)
40900 20 29 3b 0a 20 20 69 66 28 20 21 61 43 6f 6c 20   );.  if( !aCol 
40901 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
40902 4e 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65 74 28  NOMEM;.  memset(
40903 28 76 6f 69 64 20 2a 29 61 43 6f 6c 2c 20 30 2c  (void *)aCol, 0,
40904 20 73 69 7a 65 6f 66 28 63 6f 6e 73 74 20 63 68   sizeof(const ch
40905 61 72 20 2a 29 20 2a 20 28 61 72 67 63 2d 32 29  ar *) * (argc-2)
40906 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 74 68  );..  /* Loop th
40907 72 6f 75 67 68 20 61 6c 6c 20 6f 66 20 74 68 65  rough all of the
40908 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65   arguments passe
40909 64 20 62 79 20 74 68 65 20 75 73 65 72 20 74 6f  d by the user to
4090a 20 74 68 65 20 46 54 53 33 2f 34 0a 20 20 2a 2a   the FTS3/4.  **
4090b 20 6d 6f 64 75 6c 65 20 28 69 2e 65 2e 20 61 6c   module (i.e. al
4090c 6c 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  l the column nam
4090d 65 73 20 61 6e 64 20 73 70 65 63 69 61 6c 20 61  es and special a
4090e 72 67 75 6d 65 6e 74 73 29 2e 20 54 68 69 73 20  rguments). This 
4090f 6c 6f 6f 70 0a 20 20 2a 2a 20 64 6f 65 73 20 74  loop.  ** does t
40910 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20  he following:.  
40911 2a 2a 0a 20 20 2a 2a 20 20 20 2b 20 46 69 67 75  **.  **   + Figu
40912 72 65 73 20 6f 75 74 20 74 68 65 20 6e 75 6d 62  res out the numb
40913 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 68  er of columns th
40914 65 20 46 54 53 58 20 74 61 62 6c 65 20 77 69 6c  e FTSX table wil
40915 6c 20 68 61 76 65 2c 20 61 6e 64 0a 20 20 2a 2a  l have, and.  **
40916 20 20 20 20 20 74 68 65 20 6e 75 6d 62 65 72 20       the number 
40917 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63  of bytes of spac
40918 65 20 74 68 61 74 20 6d 75 73 74 20 62 65 20 61  e that must be a
40919 6c 6c 6f 63 61 74 65 64 20 74 6f 20 73 74 6f 72  llocated to stor
4091a 65 20 63 6f 70 69 65 73 0a 20 20 2a 2a 20 20 20  e copies.  **   
4091b 20 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20    of the column 
4091c 6e 61 6d 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  names..  **.  **
4091d 20 20 20 2b 20 49 66 20 74 68 65 72 65 20 69 73     + If there is
4091e 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 73 70 65   a tokenizer spe
4091f 63 69 66 69 63 61 74 69 6f 6e 20 69 6e 63 6c 75  cification inclu
40920 64 65 64 20 69 6e 20 74 68 65 20 61 72 67 75 6d  ded in the argum
40921 65 6e 74 73 2c 0a 20 20 2a 2a 20 20 20 20 20 69  ents,.  **     i
40922 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65 20 74  nitializes the t
40923 6f 6b 65 6e 69 7a 65 72 20 70 54 6f 6b 65 6e 69  okenizer pTokeni
40924 7a 65 72 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  zer..  */.  for(
40925 69 3d 33 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=3; rc==SQLITE_
40926 4f 4b 20 26 26 20 69 3c 61 72 67 63 3b 20 69 2b  OK && i<argc; i+
40927 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 63 6f 6e  +){.    char con
40928 73 74 20 2a 7a 20 3d 20 61 72 67 76 5b 69 5d 3b  st *z = argv[i];
40929 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20  .    int nKey;. 
4092a 20 20 20 63 68 61 72 20 2a 7a 56 61 6c 3b 0a 0a     char *zVal;..
4092b 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20      /* Check if 
4092c 74 68 69 73 20 69 73 20 61 20 74 6f 6b 65 6e 69  this is a tokeni
4092d 7a 65 72 20 73 70 65 63 69 66 69 63 61 74 69 6f  zer specificatio
4092e 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 54  n */.    if( !pT
4092f 6f 6b 65 6e 69 7a 65 72 20 0a 20 20 20 20 20 26  okenizer .     &
40930 26 20 73 74 72 6c 65 6e 28 7a 29 3e 38 0a 20 20  & strlen(z)>8.  
40931 20 20 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33     && 0==sqlite3
40932 5f 73 74 72 6e 69 63 6d 70 28 7a 2c 20 22 74 6f  _strnicmp(z, "to
40933 6b 65 6e 69 7a 65 22 2c 20 38 29 20 0a 20 20 20  kenize", 8) .   
40934 20 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 46    && 0==sqlite3F
40935 74 73 33 49 73 49 64 43 68 61 72 28 7a 5b 38 5d  ts3IsIdChar(z[8]
40936 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72  ).    ){.      r
40937 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49  c = sqlite3Fts3I
40938 6e 69 74 54 6f 6b 65 6e 69 7a 65 72 28 70 48 61  nitTokenizer(pHa
40939 73 68 2c 20 26 7a 5b 39 5d 2c 20 26 70 54 6f 6b  sh, &z[9], &pTok
4093a 65 6e 69 7a 65 72 2c 20 70 7a 45 72 72 29 3b 0a  enizer, pzErr);.
4093b 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68      }..    /* Ch
4093c 65 63 6b 20 69 66 20 69 74 20 69 73 20 61 6e 20  eck if it is an 
4093d 46 54 53 34 20 73 70 65 63 69 61 6c 20 61 72 67  FTS4 special arg
4093e 75 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 65 6c  ument. */.    el
4093f 73 65 20 69 66 28 20 69 73 46 74 73 34 20 26 26  se if( isFts4 &&
40940 20 66 74 73 33 49 73 53 70 65 63 69 61 6c 43 6f   fts3IsSpecialCo
40941 6c 75 6d 6e 28 7a 2c 20 26 6e 4b 65 79 2c 20 26  lumn(z, &nKey, &
40942 7a 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 20 73  zVal) ){.      s
40943 74 72 75 63 74 20 46 74 73 34 4f 70 74 69 6f 6e  truct Fts4Option
40944 20 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74   {.        const
40945 20 63 68 61 72 20 2a 7a 4f 70 74 3b 0a 20 20 20   char *zOpt;.   
40946 20 20 20 20 20 69 6e 74 20 6e 4f 70 74 3b 0a 20       int nOpt;. 
40947 20 20 20 20 20 7d 20 61 46 74 73 34 4f 70 74 5b       } aFts4Opt[
40948 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 7b 20  ] = {.        { 
40949 22 6d 61 74 63 68 69 6e 66 6f 22 2c 20 20 20 39  "matchinfo",   9
4094a 20 7d 2c 20 20 20 20 20 2f 2a 20 30 20 2d 3e 20   },     /* 0 -> 
4094b 4d 41 54 43 48 49 4e 46 4f 20 2a 2f 0a 20 20 20  MATCHINFO */.   
4094c 20 20 20 20 20 7b 20 22 70 72 65 66 69 78 22 2c       { "prefix",
4094d 20 20 20 20 20 20 36 20 7d 2c 20 20 20 20 20 2f        6 },     /
4094e 2a 20 31 20 2d 3e 20 50 52 45 46 49 58 20 2a 2f  * 1 -> PREFIX */
4094f 0a 20 20 20 20 20 20 20 20 7b 20 22 63 6f 6d 70  .        { "comp
40950 72 65 73 73 22 2c 20 20 20 20 38 20 7d 2c 20 20  ress",    8 },  
40951 20 20 20 2f 2a 20 32 20 2d 3e 20 43 4f 4d 50 52     /* 2 -> COMPR
40952 45 53 53 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b  ESS */.        {
40953 20 22 75 6e 63 6f 6d 70 72 65 73 73 22 2c 20 31   "uncompress", 1
40954 30 20 7d 2c 20 20 20 20 20 2f 2a 20 33 20 2d 3e  0 },     /* 3 ->
40955 20 55 4e 43 4f 4d 50 52 45 53 53 20 2a 2f 0a 20   UNCOMPRESS */. 
40956 20 20 20 20 20 20 20 7b 20 22 6f 72 64 65 72 22         { "order"
40957 2c 20 20 20 20 20 20 20 35 20 7d 2c 20 20 20 20  ,       5 },    
40958 20 2f 2a 20 34 20 2d 3e 20 4f 52 44 45 52 20 2a   /* 4 -> ORDER *
40959 2f 0a 20 20 20 20 20 20 20 20 7b 20 22 63 6f 6e  /.        { "con
4095a 74 65 6e 74 22 2c 20 20 20 20 20 37 20 7d 2c 20  tent",     7 }, 
4095b 20 20 20 20 2f 2a 20 35 20 2d 3e 20 43 4f 4e 54      /* 5 -> CONT
4095c 45 4e 54 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b  ENT */.        {
4095d 20 22 6c 61 6e 67 75 61 67 65 69 64 22 2c 20 31   "languageid", 1
4095e 30 20 7d 20 20 20 20 20 20 2f 2a 20 36 20 2d 3e  0 }      /* 6 ->
4095f 20 4c 41 4e 47 55 41 47 45 49 44 20 2a 2f 0a 20   LANGUAGEID */. 
40960 20 20 20 20 20 7d 3b 0a 0a 20 20 20 20 20 20 69       };..      i
40961 6e 74 20 69 4f 70 74 3b 0a 20 20 20 20 20 20 69  nt iOpt;.      i
40962 66 28 20 21 7a 56 61 6c 20 29 7b 0a 20 20 20 20  f( !zVal ){.    
40963 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
40964 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c  NOMEM;.      }el
40965 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  se{.        for(
40966 69 4f 70 74 3d 30 3b 20 69 4f 70 74 3c 53 69 7a  iOpt=0; iOpt<Siz
40967 65 6f 66 41 72 72 61 79 28 61 46 74 73 34 4f 70  eofArray(aFts4Op
40968 74 29 3b 20 69 4f 70 74 2b 2b 29 7b 0a 20 20 20  t); iOpt++){.   
40969 20 20 20 20 20 20 20 73 74 72 75 63 74 20 46 74         struct Ft
4096a 73 34 4f 70 74 69 6f 6e 20 2a 70 4f 70 20 3d 20  s4Option *pOp = 
4096b 26 61 46 74 73 34 4f 70 74 5b 69 4f 70 74 5d 3b  &aFts4Opt[iOpt];
4096c 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e  .          if( n
4096d 4b 65 79 3d 3d 70 4f 70 2d 3e 6e 4f 70 74 20 26  Key==pOp->nOpt &
4096e 26 20 21 73 71 6c 69 74 65 33 5f 73 74 72 6e 69  & !sqlite3_strni
4096f 63 6d 70 28 7a 2c 20 70 4f 70 2d 3e 7a 4f 70 74  cmp(z, pOp->zOpt
40970 2c 20 70 4f 70 2d 3e 6e 4f 70 74 29 20 29 7b 0a  , pOp->nOpt) ){.
40971 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
40972 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  k;.          }. 
40973 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
40974 20 69 66 28 20 69 4f 70 74 3d 3d 53 69 7a 65 6f   if( iOpt==Sizeo
40975 66 41 72 72 61 79 28 61 46 74 73 34 4f 70 74 29  fArray(aFts4Opt)
40976 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70   ){.          *p
40977 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zErr = sqlite3_m
40978 70 72 69 6e 74 66 28 22 75 6e 72 65 63 6f 67 6e  printf("unrecogn
40979 69 7a 65 64 20 70 61 72 61 6d 65 74 65 72 3a 20  ized parameter: 
4097a 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20  %s", z);.       
4097b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
4097c 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 7d 65  RROR;.        }e
4097d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73  lse{.          s
4097e 77 69 74 63 68 28 20 69 4f 70 74 20 29 7b 0a 20  witch( iOpt ){. 
4097f 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20             case 
40980 30 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0:              
40981 20 2f 2a 20 4d 41 54 43 48 49 4e 46 4f 20 2a 2f   /* MATCHINFO */
40982 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
40983 66 28 20 73 74 72 6c 65 6e 28 7a 56 61 6c 29 21  f( strlen(zVal)!
40984 3d 34 20 7c 7c 20 73 71 6c 69 74 65 33 5f 73 74  =4 || sqlite3_st
40985 72 6e 69 63 6d 70 28 7a 56 61 6c 2c 20 22 66 74  rnicmp(zVal, "ft
40986 73 33 22 2c 20 34 29 20 29 7b 0a 20 20 20 20 20  s3", 4) ){.     
40987 20 20 20 20 20 20 20 20 20 20 20 2a 70 7a 45 72             *pzEr
40988 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  r = sqlite3_mpri
40989 6e 74 66 28 22 75 6e 72 65 63 6f 67 6e 69 7a 65  ntf("unrecognize
4098a 64 20 6d 61 74 63 68 69 6e 66 6f 3a 20 25 73 22  d matchinfo: %s"
4098b 2c 20 7a 56 61 6c 29 3b 0a 20 20 20 20 20 20 20  , zVal);.       
4098c 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
4098d 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
4098e 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
4098f 20 20 20 20 20 20 20 20 20 20 62 4e 6f 44 6f 63            bNoDoc
40990 73 69 7a 65 20 3d 20 31 3b 0a 20 20 20 20 20 20  size = 1;.      
40991 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a          break;..
40992 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65              case
40993 20 31 3a 20 20 20 20 20 20 20 20 20 20 20 20 20   1:             
40994 20 20 2f 2a 20 50 52 45 46 49 58 20 2a 2f 0a 20    /* PREFIX */. 
40995 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
40996 69 74 65 33 5f 66 72 65 65 28 7a 50 72 65 66 69  ite3_free(zPrefi
40997 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  x);.            
40998 20 20 7a 50 72 65 66 69 78 20 3d 20 7a 56 61 6c    zPrefix = zVal
40999 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
4099a 7a 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  zVal = 0;.      
4099b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a          break;..
4099c 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65              case
4099d 20 32 3a 20 20 20 20 20 20 20 20 20 20 20 20 20   2:             
4099e 20 20 2f 2a 20 43 4f 4d 50 52 45 53 53 20 2a 2f    /* COMPRESS */
4099f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  .              s
409a0 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d  qlite3_free(zCom
409a1 70 72 65 73 73 29 3b 0a 20 20 20 20 20 20 20 20  press);.        
409a2 20 20 20 20 20 20 7a 43 6f 6d 70 72 65 73 73 20        zCompress 
409a3 3d 20 7a 56 61 6c 3b 0a 20 20 20 20 20 20 20 20  = zVal;.        
409a4 20 20 20 20 20 20 7a 56 61 6c 20 3d 20 30 3b 0a        zVal = 0;.
409a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
409a6 65 61 6b 3b 0a 0a 20 20 20 20 20 20 20 20 20 20  eak;..          
409a7 20 20 63 61 73 65 20 33 3a 20 20 20 20 20 20 20    case 3:       
409a8 20 20 20 20 20 20 20 20 2f 2a 20 55 4e 43 4f 4d          /* UNCOM
409a9 50 52 45 53 53 20 2a 2f 0a 20 20 20 20 20 20 20  PRESS */.       
409aa 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
409ab 72 65 65 28 7a 55 6e 63 6f 6d 70 72 65 73 73 29  ree(zUncompress)
409ac 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
409ad 7a 55 6e 63 6f 6d 70 72 65 73 73 20 3d 20 7a 56  zUncompress = zV
409ae 61 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  al;.            
409af 20 20 7a 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20    zVal = 0;.    
409b0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
409b1 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 61  ..            ca
409b2 73 65 20 34 3a 20 20 20 20 20 20 20 20 20 20 20  se 4:           
409b3 20 20 20 20 2f 2a 20 4f 52 44 45 52 20 2a 2f 0a      /* ORDER */.
409b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
409b5 28 20 28 73 74 72 6c 65 6e 28 7a 56 61 6c 29 21  ( (strlen(zVal)!
409b6 3d 33 20 7c 7c 20 73 71 6c 69 74 65 33 5f 73 74  =3 || sqlite3_st
409b7 72 6e 69 63 6d 70 28 7a 56 61 6c 2c 20 22 61 73  rnicmp(zVal, "as
409b8 63 22 2c 20 33 29 29 20 0a 20 20 20 20 20 20 20  c", 3)) .       
409b9 20 20 20 20 20 20 20 20 26 26 20 28 73 74 72 6c          && (strl
409ba 65 6e 28 7a 56 61 6c 29 21 3d 34 20 7c 7c 20 73  en(zVal)!=4 || s
409bb 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28  qlite3_strnicmp(
409bc 7a 56 61 6c 2c 20 22 64 65 73 63 22 2c 20 34 29  zVal, "desc", 4)
409bd 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ) .             
409be 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
409bf 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c      *pzErr = sql
409c0 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e  ite3_mprintf("un
409c1 72 65 63 6f 67 6e 69 7a 65 64 20 6f 72 64 65 72  recognized order
409c2 3a 20 25 73 22 2c 20 7a 56 61 6c 29 3b 0a 20 20  : %s", zVal);.  
409c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63                rc
409c4 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
409c5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
409c6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  .              b
409c7 44 65 73 63 49 64 78 20 3d 20 28 7a 56 61 6c 5b  DescIdx = (zVal[
409c8 30 5d 3d 3d 27 64 27 20 7c 7c 20 7a 56 61 6c 5b  0]=='d' || zVal[
409c9 30 5d 3d 3d 27 44 27 29 3b 0a 20 20 20 20 20 20  0]=='D');.      
409ca 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a          break;..
409cb 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65              case
409cc 20 35 3a 20 20 20 20 20 20 20 20 20 20 20 20 20   5:             
409cd 20 2f 2a 20 43 4f 4e 54 45 4e 54 20 2a 2f 0a 20   /* CONTENT */. 
409ce 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
409cf 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6e 74 65  ite3_free(zConte
409d0 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nt);.           
409d1 20 20 20 7a 43 6f 6e 74 65 6e 74 20 3d 20 7a 56     zContent = zV
409d2 61 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  al;.            
409d3 20 20 7a 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20    zVal = 0;.    
409d4 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
409d5 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 61  ..            ca
409d6 73 65 20 36 3a 20 20 20 20 20 20 20 20 20 20 20  se 6:           
409d7 20 20 20 2f 2a 20 4c 41 4e 47 55 41 47 45 49 44     /* LANGUAGEID
409d8 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
409d9 20 20 61 73 73 65 72 74 28 20 69 4f 70 74 3d 3d    assert( iOpt==
409da 36 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  6 );.           
409db 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
409dc 7a 4c 61 6e 67 75 61 67 65 69 64 29 3b 0a 20 20  zLanguageid);.  
409dd 20 20 20 20 20 20 20 20 20 20 20 20 7a 4c 61 6e              zLan
409de 67 75 61 67 65 69 64 20 3d 20 7a 56 61 6c 3b 0a  guageid = zVal;.
409df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 56                zV
409e0 61 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  al = 0;.        
409e1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
409e2 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
409e3 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
409e4 65 33 5f 66 72 65 65 28 7a 56 61 6c 29 3b 0a 20  e3_free(zVal);. 
409e5 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
409e6 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c 20    /* Otherwise, 
409e7 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  the argument is 
409e8 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 20 2a  a column name. *
409e9 2f 0a 20 20 20 20 65 6c 73 65 20 7b 0a 20 20 20  /.    else {.   
409ea 20 20 20 6e 53 74 72 69 6e 67 20 2b 3d 20 28 69     nString += (i
409eb 6e 74 29 28 73 74 72 6c 65 6e 28 7a 29 20 2b 20  nt)(strlen(z) + 
409ec 31 29 3b 0a 20 20 20 20 20 20 61 43 6f 6c 5b 6e  1);.      aCol[n
409ed 43 6f 6c 2b 2b 5d 20 3d 20 7a 3b 0a 20 20 20 20  Col++] = z;.    
409ee 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61  }.  }..  /* If a
409ef 20 63 6f 6e 74 65 6e 74 3d 78 78 78 20 6f 70 74   content=xxx opt
409f0 69 6f 6e 20 77 61 73 20 73 70 65 63 69 66 69 65  ion was specifie
409f1 64 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  d, the following
409f2 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 31 2e  :.  **.  **   1.
409f3 20 49 67 6e 6f 72 65 20 61 6e 79 20 63 6f 6d 70   Ignore any comp
409f4 72 65 73 73 3d 20 61 6e 64 20 75 6e 63 6f 6d 70  ress= and uncomp
409f5 72 65 73 73 3d 20 6f 70 74 69 6f 6e 73 2e 0a 20  ress= options.. 
409f6 20 2a 2a 0a 20 20 2a 2a 20 20 20 32 2e 20 49 66   **.  **   2. If
409f7 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73   no column names
409f8 20 77 65 72 65 20 73 70 65 63 69 66 69 65 64 20   were specified 
409f9 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 43  as part of the C
409fa 52 45 41 54 45 20 56 49 52 54 55 41 4c 0a 20 20  REATE VIRTUAL.  
409fb 2a 2a 20 20 20 20 20 20 54 41 42 4c 45 20 73 74  **      TABLE st
409fc 61 74 65 6d 65 6e 74 2c 20 75 73 65 20 61 6c 6c  atement, use all
409fd 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 74 68   columns from th
409fe 65 20 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 2e  e content table.
409ff 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  .  */.  if( rc==
40a00 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 43 6f  SQLITE_OK && zCo
40a01 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c  ntent ){.    sql
40a02 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d 70 72  ite3_free(zCompr
40a03 65 73 73 29 3b 20 0a 20 20 20 20 73 71 6c 69 74  ess); .    sqlit
40a04 65 33 5f 66 72 65 65 28 7a 55 6e 63 6f 6d 70 72  e3_free(zUncompr
40a05 65 73 73 29 3b 20 0a 20 20 20 20 7a 43 6f 6d 70  ess); .    zComp
40a06 72 65 73 73 20 3d 20 30 3b 0a 20 20 20 20 7a 55  ress = 0;.    zU
40a07 6e 63 6f 6d 70 72 65 73 73 20 3d 20 30 3b 0a 20  ncompress = 0;. 
40a08 20 20 20 69 66 28 20 6e 43 6f 6c 3d 3d 30 20 29     if( nCol==0 )
40a09 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
40a0a 66 72 65 65 28 28 76 6f 69 64 2a 29 61 43 6f 6c  free((void*)aCol
40a0b 29 3b 20 0a 20 20 20 20 20 20 61 43 6f 6c 20 3d  ); .      aCol =
40a0c 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66   0;.      rc = f
40a0d 74 73 33 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e  ts3ContentColumn
40a0e 73 28 64 62 2c 20 61 72 67 76 5b 31 5d 2c 20 7a  s(db, argv[1], z
40a0f 43 6f 6e 74 65 6e 74 2c 20 26 61 43 6f 6c 2c 20  Content, &aCol, 
40a10 26 6e 43 6f 6c 2c 20 26 6e 53 74 72 69 6e 67 29  &nCol, &nString)
40a11 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61  ;..      /* If a
40a12 20 6c 61 6e 67 75 61 67 65 69 64 3d 20 6f 70 74   languageid= opt
40a13 69 6f 6e 20 77 61 73 20 73 70 65 63 69 66 69 65  ion was specifie
40a14 64 2c 20 72 65 6d 6f 76 65 20 74 68 65 20 6c 61  d, remove the la
40a15 6e 67 75 61 67 65 20 69 64 0a 20 20 20 20 20 20  nguage id.      
40a16 2a 2a 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74  ** column from t
40a17 68 65 20 61 43 6f 6c 5b 5d 20 61 72 72 61 79 2e  he aCol[] array.
40a18 20 2a 2f 20 0a 20 20 20 20 20 20 69 66 28 20 72   */ .      if( r
40a19 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
40a1a 7a 4c 61 6e 67 75 61 67 65 69 64 20 29 7b 0a 20  zLanguageid ){. 
40a1b 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20         int j;.  
40a1c 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
40a1d 3c 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20  <nCol; j++){.   
40a1e 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
40a1f 65 33 5f 73 74 72 69 63 6d 70 28 7a 4c 61 6e 67  e3_stricmp(zLang
40a20 75 61 67 65 69 64 2c 20 61 43 6f 6c 5b 6a 5d 29  uageid, aCol[j])
40a21 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
40a22 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20     int k;.      
40a23 20 20 20 20 20 20 66 6f 72 28 6b 3d 6a 3b 20 6b        for(k=j; k
40a24 3c 6e 43 6f 6c 3b 20 6b 2b 2b 29 20 61 43 6f 6c  <nCol; k++) aCol
40a25 5b 6b 5d 20 3d 20 61 43 6f 6c 5b 6b 2b 31 5d 3b  [k] = aCol[k+1];
40a26 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 43 6f  .            nCo
40a27 6c 2d 2d 3b 0a 20 20 20 20 20 20 20 20 20 20 20  l--;.           
40a28 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
40a29 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
40a2a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
40a2b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
40a2c 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 74 73 33 5f  _OK ) goto fts3_
40a2d 69 6e 69 74 5f 6f 75 74 3b 0a 0a 20 20 69 66 28  init_out;..  if(
40a2e 20 6e 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20   nCol==0 ){.    
40a2f 61 73 73 65 72 74 28 20 6e 53 74 72 69 6e 67 3d  assert( nString=
40a30 3d 30 20 29 3b 0a 20 20 20 20 61 43 6f 6c 5b 30  =0 );.    aCol[0
40a31 5d 20 3d 20 22 63 6f 6e 74 65 6e 74 22 3b 0a 20  ] = "content";. 
40a32 20 20 20 6e 53 74 72 69 6e 67 20 3d 20 38 3b 0a     nString = 8;.
40a33 20 20 20 20 6e 43 6f 6c 20 3d 20 31 3b 0a 20 20      nCol = 1;.  
40a34 7d 0a 0a 20 20 69 66 28 20 70 54 6f 6b 65 6e 69  }..  if( pTokeni
40a35 7a 65 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  zer==0 ){.    rc
40a36 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e   = sqlite3Fts3In
40a37 69 74 54 6f 6b 65 6e 69 7a 65 72 28 70 48 61 73  itTokenizer(pHas
40a38 68 2c 20 22 73 69 6d 70 6c 65 22 2c 20 26 70 54  h, "simple", &pT
40a39 6f 6b 65 6e 69 7a 65 72 2c 20 70 7a 45 72 72 29  okenizer, pzErr)
40a3a 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
40a3b 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66  LITE_OK ) goto f
40a3c 74 73 33 5f 69 6e 69 74 5f 6f 75 74 3b 0a 20 20  ts3_init_out;.  
40a3d 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b  }.  assert( pTok
40a3e 65 6e 69 7a 65 72 20 29 3b 0a 0a 20 20 72 63 20  enizer );..  rc 
40a3f 3d 20 66 74 73 33 50 72 65 66 69 78 50 61 72 61  = fts3PrefixPara
40a40 6d 65 74 65 72 28 7a 50 72 65 66 69 78 2c 20 26  meter(zPrefix, &
40a41 6e 49 6e 64 65 78 2c 20 26 61 49 6e 64 65 78 29  nIndex, &aIndex)
40a42 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
40a43 54 45 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  TE_ERROR ){.    
40a44 61 73 73 65 72 74 28 20 7a 50 72 65 66 69 78 20  assert( zPrefix 
40a45 29 3b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20  );.    *pzErr = 
40a46 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
40a47 22 65 72 72 6f 72 20 70 61 72 73 69 6e 67 20 70  "error parsing p
40a48 72 65 66 69 78 20 70 61 72 61 6d 65 74 65 72 3a  refix parameter:
40a49 20 25 73 22 2c 20 7a 50 72 65 66 69 78 29 3b 0a   %s", zPrefix);.
40a4a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51    }.  if( rc!=SQ
40a4b 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66  LITE_OK ) goto f
40a4c 74 73 33 5f 69 6e 69 74 5f 6f 75 74 3b 0a 0a 20  ts3_init_out;.. 
40a4d 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64   /* Allocate and
40a4e 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 46 74   populate the Ft
40a4f 73 33 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  s3Table structur
40a50 65 2e 20 2a 2f 0a 20 20 6e 42 79 74 65 20 3d 20  e. */.  nByte = 
40a51 73 69 7a 65 6f 66 28 46 74 73 33 54 61 62 6c 65  sizeof(Fts3Table
40a52 29 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20  ) +             
40a53 20 20 20 20 20 2f 2a 20 46 74 73 33 54 61 62 6c       /* Fts3Tabl
40a54 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6e  e */.          n
40a55 43 6f 6c 20 2a 20 73 69 7a 65 6f 66 28 63 68 61  Col * sizeof(cha
40a56 72 20 2a 29 20 2b 20 20 20 20 20 20 20 20 20 20  r *) +          
40a57 20 20 20 20 2f 2a 20 61 7a 43 6f 6c 75 6d 6e 20      /* azColumn 
40a58 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6e 49 6e  */.          nIn
40a59 64 65 78 20 2a 20 73 69 7a 65 6f 66 28 73 74 72  dex * sizeof(str
40a5a 75 63 74 20 46 74 73 33 49 6e 64 65 78 29 20 2b  uct Fts3Index) +
40a5b 20 20 2f 2a 20 61 49 6e 64 65 78 20 2a 2f 0a 20    /* aIndex */. 
40a5c 20 20 20 20 20 20 20 20 20 6e 4e 61 6d 65 20 2b           nName +
40a5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40a5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
40a5f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20   zName */.      
40a60 20 20 20 20 6e 44 62 20 2b 20 20 20 20 20 20 20      nDb +       
40a61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40a62 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 44 62 20           /* zDb 
40a63 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6e 53 74  */.          nSt
40a64 72 69 6e 67 3b 20 20 20 20 20 20 20 20 20 20 20  ring;           
40a65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40a66 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 61    /* Space for a
40a67 7a 43 6f 6c 75 6d 6e 20 73 74 72 69 6e 67 73 20  zColumn strings 
40a68 2a 2f 0a 20 20 70 20 3d 20 28 46 74 73 33 54 61  */.  p = (Fts3Ta
40a69 62 6c 65 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  ble*)sqlite3_mal
40a6a 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 69 66  loc(nByte);.  if
40a6b 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  ( p==0 ){.    rc
40a6c 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
40a6d 0a 20 20 20 20 67 6f 74 6f 20 66 74 73 33 5f 69  .    goto fts3_i
40a6e 6e 69 74 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6d  nit_out;.  }.  m
40a6f 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 42 79 74  emset(p, 0, nByt
40a70 65 29 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62  e);.  p->db = db
40a71 3b 0a 20 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d  ;.  p->nColumn =
40a72 20 6e 43 6f 6c 3b 0a 20 20 70 2d 3e 6e 50 65 6e   nCol;.  p->nPen
40a73 64 69 6e 67 44 61 74 61 20 3d 20 30 3b 0a 20 20  dingData = 0;.  
40a74 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20 28 63  p->azColumn = (c
40a75 68 61 72 20 2a 2a 29 26 70 5b 31 5d 3b 0a 20 20  har **)&p[1];.  
40a76 70 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20  p->pTokenizer = 
40a77 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 70 2d  pTokenizer;.  p-
40a78 3e 6e 4d 61 78 50 65 6e 64 69 6e 67 44 61 74 61  >nMaxPendingData
40a79 20 3d 20 46 54 53 33 5f 4d 41 58 5f 50 45 4e 44   = FTS3_MAX_PEND
40a7a 49 4e 47 5f 44 41 54 41 3b 0a 20 20 70 2d 3e 62  ING_DATA;.  p->b
40a7b 48 61 73 44 6f 63 73 69 7a 65 20 3d 20 28 69 73  HasDocsize = (is
40a7c 46 74 73 34 20 26 26 20 62 4e 6f 44 6f 63 73 69  Fts4 && bNoDocsi
40a7d 7a 65 3d 3d 30 29 3b 0a 20 20 70 2d 3e 62 48 61  ze==0);.  p->bHa
40a7e 73 53 74 61 74 20 3d 20 69 73 46 74 73 34 3b 0a  sStat = isFts4;.
40a7f 20 20 70 2d 3e 62 46 74 73 34 20 3d 20 69 73 46    p->bFts4 = isF
40a80 74 73 34 3b 0a 20 20 70 2d 3e 62 44 65 73 63 49  ts4;.  p->bDescI
40a81 64 78 20 3d 20 62 44 65 73 63 49 64 78 3b 0a 20  dx = bDescIdx;. 
40a82 20 70 2d 3e 62 41 75 74 6f 69 6e 63 72 6d 65 72   p->bAutoincrmer
40a83 67 65 20 3d 20 30 78 66 66 3b 20 20 20 2f 2a 20  ge = 0xff;   /* 
40a84 30 78 66 66 20 6d 65 61 6e 73 20 73 65 74 74 69  0xff means setti
40a85 6e 67 20 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a 20 20  ng unknown */.  
40a86 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c 20 3d  p->zContentTbl =
40a87 20 7a 43 6f 6e 74 65 6e 74 3b 0a 20 20 70 2d 3e   zContent;.  p->
40a88 7a 4c 61 6e 67 75 61 67 65 69 64 20 3d 20 7a 4c  zLanguageid = zL
40a89 61 6e 67 75 61 67 65 69 64 3b 0a 20 20 7a 43 6f  anguageid;.  zCo
40a8a 6e 74 65 6e 74 20 3d 20 30 3b 0a 20 20 7a 4c 61  ntent = 0;.  zLa
40a8b 6e 67 75 61 67 65 69 64 20 3d 20 30 3b 0a 20 20  nguageid = 0;.  
40a8c 54 45 53 54 4f 4e 4c 59 28 20 70 2d 3e 69 6e 54  TESTONLY( p->inT
40a8d 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 2d 31 20  ransaction = -1 
40a8e 29 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 70  );.  TESTONLY( p
40a8f 2d 3e 6d 78 53 61 76 65 70 6f 69 6e 74 20 3d 20  ->mxSavepoint = 
40a90 2d 31 20 29 3b 0a 0a 20 20 70 2d 3e 61 49 6e 64  -1 );..  p->aInd
40a91 65 78 20 3d 20 28 73 74 72 75 63 74 20 46 74 73  ex = (struct Fts
40a92 33 49 6e 64 65 78 20 2a 29 26 70 2d 3e 61 7a 43  3Index *)&p->azC
40a93 6f 6c 75 6d 6e 5b 6e 43 6f 6c 5d 3b 0a 20 20 6d  olumn[nCol];.  m
40a94 65 6d 63 70 79 28 70 2d 3e 61 49 6e 64 65 78 2c  emcpy(p->aIndex,
40a95 20 61 49 6e 64 65 78 2c 20 73 69 7a 65 6f 66 28   aIndex, sizeof(
40a96 73 74 72 75 63 74 20 46 74 73 33 49 6e 64 65 78  struct Fts3Index
40a97 29 20 2a 20 6e 49 6e 64 65 78 29 3b 0a 20 20 70  ) * nIndex);.  p
40a98 2d 3e 6e 49 6e 64 65 78 20 3d 20 6e 49 6e 64 65  ->nIndex = nInde
40a99 78 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  x;.  for(i=0; i<
40a9a 6e 49 6e 64 65 78 3b 20 69 2b 2b 29 7b 0a 20 20  nIndex; i++){.  
40a9b 20 20 66 74 73 33 48 61 73 68 49 6e 69 74 28 26    fts3HashInit(&
40a9c 70 2d 3e 61 49 6e 64 65 78 5b 69 5d 2e 68 50 65  p->aIndex[i].hPe
40a9d 6e 64 69 6e 67 2c 20 46 54 53 33 5f 48 41 53 48  nding, FTS3_HASH
40a9e 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20 20 7d  _STRING, 1);.  }
40a9f 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74  ..  /* Fill in t
40aa0 68 65 20 7a 4e 61 6d 65 20 61 6e 64 20 7a 44 62  he zName and zDb
40aa1 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 76   fields of the v
40aa2 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 20 2a  tab structure. *
40aa3 2f 0a 20 20 7a 43 73 72 20 3d 20 28 63 68 61 72  /.  zCsr = (char
40aa4 20 2a 29 26 70 2d 3e 61 49 6e 64 65 78 5b 6e 49   *)&p->aIndex[nI
40aa5 6e 64 65 78 5d 3b 0a 20 20 70 2d 3e 7a 4e 61 6d  ndex];.  p->zNam
40aa6 65 20 3d 20 7a 43 73 72 3b 0a 20 20 6d 65 6d 63  e = zCsr;.  memc
40aa7 70 79 28 7a 43 73 72 2c 20 61 72 67 76 5b 32 5d  py(zCsr, argv[2]
40aa8 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 7a 43 73 72  , nName);.  zCsr
40aa9 20 2b 3d 20 6e 4e 61 6d 65 3b 0a 20 20 70 2d 3e   += nName;.  p->
40aaa 7a 44 62 20 3d 20 7a 43 73 72 3b 0a 20 20 6d 65  zDb = zCsr;.  me
40aab 6d 63 70 79 28 7a 43 73 72 2c 20 61 72 67 76 5b  mcpy(zCsr, argv[
40aac 31 5d 2c 20 6e 44 62 29 3b 0a 20 20 7a 43 73 72  1], nDb);.  zCsr
40aad 20 2b 3d 20 6e 44 62 3b 0a 0a 20 20 2f 2a 20 46   += nDb;..  /* F
40aae 69 6c 6c 20 69 6e 20 74 68 65 20 61 7a 43 6f 6c  ill in the azCol
40aaf 75 6d 6e 20 61 72 72 61 79 20 2a 2f 0a 20 20 66  umn array */.  f
40ab0 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c  or(iCol=0; iCol<
40ab1 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20  nCol; iCol++){. 
40ab2 20 20 20 63 68 61 72 20 2a 7a 3b 20 0a 20 20 20     char *z; .   
40ab3 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 20 20   int n = 0;.    
40ab4 7a 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  z = (char *)sqli
40ab5 74 65 33 46 74 73 33 4e 65 78 74 54 6f 6b 65 6e  te3Fts3NextToken
40ab6 28 61 43 6f 6c 5b 69 43 6f 6c 5d 2c 20 26 6e 29  (aCol[iCol], &n)
40ab7 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 43 73  ;.    memcpy(zCs
40ab8 72 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 43  r, z, n);.    zC
40ab9 73 72 5b 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  sr[n] = '\0';.  
40aba 20 20 73 71 6c 69 74 65 33 46 74 73 33 44 65 71    sqlite3Fts3Deq
40abb 75 6f 74 65 28 7a 43 73 72 29 3b 0a 20 20 20 20  uote(zCsr);.    
40abc 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 5b 69 43 6f 6c  p->azColumn[iCol
40abd 5d 20 3d 20 7a 43 73 72 3b 0a 20 20 20 20 7a 43  ] = zCsr;.    zC
40abe 73 72 20 2b 3d 20 6e 2b 31 3b 0a 20 20 20 20 61  sr += n+1;.    a
40abf 73 73 65 72 74 28 20 7a 43 73 72 20 3c 3d 20 26  ssert( zCsr <= &
40ac0 28 28 63 68 61 72 20 2a 29 70 29 5b 6e 42 79 74  ((char *)p)[nByt
40ac1 65 5d 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  e] );.  }..  if(
40ac2 20 28 7a 43 6f 6d 70 72 65 73 73 3d 3d 30 29 21   (zCompress==0)!
40ac3 3d 28 7a 55 6e 63 6f 6d 70 72 65 73 73 3d 3d 30  =(zUncompress==0
40ac4 29 20 29 7b 0a 20 20 20 20 63 68 61 72 20 63 6f  ) ){.    char co
40ac5 6e 73 74 20 2a 7a 4d 69 73 73 20 3d 20 28 7a 43  nst *zMiss = (zC
40ac6 6f 6d 70 72 65 73 73 3d 3d 30 20 3f 20 22 63 6f  ompress==0 ? "co
40ac7 6d 70 72 65 73 73 22 20 3a 20 22 75 6e 63 6f 6d  mpress" : "uncom
40ac8 70 72 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20  press");.    rc 
40ac9 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
40aca 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c      *pzErr = sql
40acb 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 6d 69  ite3_mprintf("mi
40acc 73 73 69 6e 67 20 25 73 20 70 61 72 61 6d 65 74  ssing %s paramet
40acd 65 72 20 69 6e 20 66 74 73 34 20 63 6f 6e 73 74  er in fts4 const
40ace 72 75 63 74 6f 72 22 2c 20 7a 4d 69 73 73 29 3b  ructor", zMiss);
40acf 0a 20 20 7d 0a 20 20 70 2d 3e 7a 52 65 61 64 45  .  }.  p->zReadE
40ad0 78 70 72 6c 69 73 74 20 3d 20 66 74 73 33 52 65  xprlist = fts3Re
40ad1 61 64 45 78 70 72 4c 69 73 74 28 70 2c 20 7a 55  adExprList(p, zU
40ad2 6e 63 6f 6d 70 72 65 73 73 2c 20 26 72 63 29 3b  ncompress, &rc);
40ad3 0a 20 20 70 2d 3e 7a 57 72 69 74 65 45 78 70 72  .  p->zWriteExpr
40ad4 6c 69 73 74 20 3d 20 66 74 73 33 57 72 69 74 65  list = fts3Write
40ad5 45 78 70 72 4c 69 73 74 28 70 2c 20 7a 43 6f 6d  ExprList(p, zCom
40ad6 70 72 65 73 73 2c 20 26 72 63 29 3b 0a 20 20 69  press, &rc);.  i
40ad7 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
40ad8 20 29 20 67 6f 74 6f 20 66 74 73 33 5f 69 6e 69   ) goto fts3_ini
40ad9 74 5f 6f 75 74 3b 0a 0a 20 20 2f 2a 20 49 66 20  t_out;..  /* If 
40ada 74 68 69 73 20 69 73 20 61 6e 20 78 43 72 65 61  this is an xCrea
40adb 74 65 20 63 61 6c 6c 2c 20 63 72 65 61 74 65 20  te call, create 
40adc 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 74  the underlying t
40add 61 62 6c 65 73 20 69 6e 20 74 68 65 20 0a 20 20  ables in the .  
40ade 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 4f 44  ** database. TOD
40adf 4f 3a 20 46 6f 72 20 78 43 6f 6e 6e 65 63 74 28  O: For xConnect(
40ae0 29 2c 20 69 74 20 63 6f 75 6c 64 20 76 65 72 69  ), it could veri
40ae1 66 79 20 74 68 61 74 20 73 61 69 64 20 74 61 62  fy that said tab
40ae2 6c 65 73 20 65 78 69 73 74 2e 0a 20 20 2a 2f 0a  les exist..  */.
40ae3 20 20 69 66 28 20 69 73 43 72 65 61 74 65 20 29    if( isCreate )
40ae4 7b 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 43  {.    rc = fts3C
40ae5 72 65 61 74 65 54 61 62 6c 65 73 28 70 29 3b 0a  reateTables(p);.
40ae6 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
40ae7 74 6f 20 73 65 65 20 69 66 20 61 20 6c 65 67 61  to see if a lega
40ae8 63 79 20 66 74 73 33 20 74 61 62 6c 65 20 68 61  cy fts3 table ha
40ae9 73 20 62 65 65 6e 20 22 75 70 67 72 61 64 65 64  s been "upgraded
40aea 22 20 62 79 20 74 68 65 0a 20 20 2a 2a 20 61 64  " by the.  ** ad
40aeb 64 69 74 69 6f 6e 20 6f 66 20 61 20 25 5f 73 74  dition of a %_st
40aec 61 74 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74  at table so that
40aed 20 69 74 20 63 61 6e 20 75 73 65 20 69 6e 63 72   it can use incr
40aee 65 6d 65 6e 74 61 6c 20 6d 65 72 67 65 2e 0a 20  emental merge.. 
40aef 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 46 74 73   */.  if( !isFts
40af0 34 20 26 26 20 21 69 73 43 72 65 61 74 65 20 29  4 && !isCreate )
40af1 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 20 3d 20  {.    int rc2 = 
40af2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 66  SQLITE_OK;.    f
40af3 74 73 33 44 62 45 78 65 63 28 26 72 63 32 2c 20  ts3DbExec(&rc2, 
40af4 64 62 2c 20 22 53 45 4c 45 43 54 20 31 20 46 52  db, "SELECT 1 FR
40af5 4f 4d 20 25 51 2e 27 25 71 5f 73 74 61 74 27 20  OM %Q.'%q_stat' 
40af6 57 48 45 52 45 20 69 64 3d 32 22 2c 0a 20 20 20  WHERE id=2",.   
40af7 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 7a              p->z
40af8 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  Db, p->zName);. 
40af9 20 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c 49     if( rc2==SQLI
40afa 54 45 5f 4f 4b 20 29 20 70 2d 3e 62 48 61 73 53  TE_OK ) p->bHasS
40afb 74 61 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  tat = 1;.  }..  
40afc 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68  /* Figure out th
40afd 65 20 70 61 67 65 2d 73 69 7a 65 20 66 6f 72 20  e page-size for 
40afe 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 54 68  the database. Th
40aff 69 73 20 69 73 20 72 65 71 75 69 72 65 64 20 69  is is required i
40b00 6e 20 6f 72 64 65 72 20 74 6f 0a 20 20 2a 2a 20  n order to.  ** 
40b01 65 73 74 69 6d 61 74 65 20 74 68 65 20 63 6f 73  estimate the cos
40b02 74 20 6f 66 20 6c 6f 61 64 69 6e 67 20 6c 61 72  t of loading lar
40b03 67 65 20 64 6f 63 6c 69 73 74 73 20 66 72 6f 6d  ge doclists from
40b04 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
40b05 2a 2f 0a 20 20 66 74 73 33 44 61 74 61 62 61 73  */.  fts3Databas
40b06 65 50 61 67 65 53 69 7a 65 28 26 72 63 2c 20 70  ePageSize(&rc, p
40b07 29 3b 0a 20 20 70 2d 3e 6e 4e 6f 64 65 53 69 7a  );.  p->nNodeSiz
40b08 65 20 3d 20 70 2d 3e 6e 50 67 73 7a 2d 33 35 3b  e = p->nPgsz-35;
40b09 0a 0a 20 20 2f 2a 20 44 65 63 6c 61 72 65 20 74  ..  /* Declare t
40b0a 68 65 20 74 61 62 6c 65 20 73 63 68 65 6d 61 20  he table schema 
40b0b 74 6f 20 53 51 4c 69 74 65 2e 20 2a 2f 0a 20 20  to SQLite. */.  
40b0c 66 74 73 33 44 65 63 6c 61 72 65 56 74 61 62 28  fts3DeclareVtab(
40b0d 26 72 63 2c 20 70 29 3b 0a 0a 66 74 73 33 5f 69  &rc, p);..fts3_i
40b0e 6e 69 74 5f 6f 75 74 3a 0a 20 20 73 71 6c 69 74  nit_out:.  sqlit
40b0f 65 33 5f 66 72 65 65 28 7a 50 72 65 66 69 78 29  e3_free(zPrefix)
40b10 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
40b11 28 61 49 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69  (aIndex);.  sqli
40b12 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d 70 72 65  te3_free(zCompre
40b13 73 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ss);.  sqlite3_f
40b14 72 65 65 28 7a 55 6e 63 6f 6d 70 72 65 73 73 29  ree(zUncompress)
40b15 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
40b16 28 7a 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 73 71  (zContent);.  sq
40b17 6c 69 74 65 33 5f 66 72 65 65 28 7a 4c 61 6e 67  lite3_free(zLang
40b18 75 61 67 65 69 64 29 3b 0a 20 20 73 71 6c 69 74  uageid);.  sqlit
40b19 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20 2a 29  e3_free((void *)
40b1a 61 43 6f 6c 29 3b 0a 20 20 69 66 28 20 72 63 21  aCol);.  if( rc!
40b1b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
40b1c 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20    if( p ){.     
40b1d 20 66 74 73 33 44 69 73 63 6f 6e 6e 65 63 74 4d   fts3DisconnectM
40b1e 65 74 68 6f 64 28 28 73 71 6c 69 74 65 33 5f 76  ethod((sqlite3_v
40b1f 74 61 62 20 2a 29 70 29 3b 0a 20 20 20 20 7d 65  tab *)p);.    }e
40b20 6c 73 65 20 69 66 28 20 70 54 6f 6b 65 6e 69 7a  lse if( pTokeniz
40b21 65 72 20 29 7b 0a 20 20 20 20 20 20 70 54 6f 6b  er ){.      pTok
40b22 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d  enizer->pModule-
40b23 3e 78 44 65 73 74 72 6f 79 28 70 54 6f 6b 65 6e  >xDestroy(pToken
40b24 69 7a 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  izer);.    }.  }
40b25 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
40b26 28 20 70 2d 3e 70 53 65 67 6d 65 6e 74 73 3d 3d  ( p->pSegments==
40b27 30 20 29 3b 0a 20 20 20 20 2a 70 70 56 54 61 62  0 );.    *ppVTab
40b28 20 3d 20 26 70 2d 3e 62 61 73 65 3b 0a 20 20 7d   = &p->base;.  }
40b29 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
40b2a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 43 6f 6e 6e  ./*.** The xConn
40b2b 65 63 74 28 29 20 61 6e 64 20 78 43 72 65 61 74  ect() and xCreat
40b2c 65 28 29 20 6d 65 74 68 6f 64 73 20 66 6f 72 20  e() methods for 
40b2d 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
40b2e 65 2e 20 41 6c 6c 20 74 68 65 0a 2a 2a 20 77 6f  e. All the.** wo
40b2f 72 6b 20 69 73 20 64 6f 6e 65 20 69 6e 20 66 75  rk is done in fu
40b30 6e 63 74 69 6f 6e 20 66 74 73 33 49 6e 69 74 56  nction fts3InitV
40b31 74 61 62 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  tab()..*/.static
40b32 20 69 6e 74 20 66 74 73 33 43 6f 6e 6e 65 63 74   int fts3Connect
40b33 4d 65 74 68 6f 64 28 0a 20 20 73 71 6c 69 74 65  Method(.  sqlite
40b34 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
40b35 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
40b36 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
40b37 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78   */.  void *pAux
40b38 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
40b39 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
40b3a 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 72 20 68 61   to tokenizer ha
40b3b 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  sh table */.  in
40b3c 74 20 61 72 67 63 2c 20 20 20 20 20 20 20 20 20  t argc,         
40b3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
40b3e 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
40b3f 6e 74 73 20 69 6e 20 61 72 67 76 20 61 72 72 61  nts in argv arra
40b40 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  y */.  const cha
40b41 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c  r * const *argv,
40b42 20 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74         /* xCreat
40b43 65 2f 78 43 6f 6e 6e 65 63 74 20 61 72 67 75 6d  e/xConnect argum
40b44 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 73  ent array */.  s
40b45 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70  qlite3_vtab **pp
40b46 56 74 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f  Vtab,          /
40b47 2a 20 4f 55 54 3a 20 4e 65 77 20 73 71 6c 69 74  * OUT: New sqlit
40b48 65 33 5f 76 74 61 62 20 6f 62 6a 65 63 74 20 2a  e3_vtab object *
40b49 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  /.  char **pzErr
40b4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40b4b 20 20 20 20 2f 2a 20 4f 55 54 3a 20 73 71 6c 69      /* OUT: sqli
40b4c 74 65 33 5f 6d 61 6c 6c 6f 63 27 64 20 65 72 72  te3_malloc'd err
40b4d 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 29 7b  or message */.){
40b4e 0a 20 20 72 65 74 75 72 6e 20 66 74 73 33 49 6e  .  return fts3In
40b4f 69 74 56 74 61 62 28 30 2c 20 64 62 2c 20 70 41  itVtab(0, db, pA
40b50 75 78 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20  ux, argc, argv, 
40b51 70 70 56 74 61 62 2c 20 70 7a 45 72 72 29 3b 0a  ppVtab, pzErr);.
40b52 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  }.static int fts
40b53 33 43 72 65 61 74 65 4d 65 74 68 6f 64 28 0a 20  3CreateMethod(. 
40b54 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
40b55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40b56 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
40b57 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69  nection */.  voi
40b58 64 20 2a 70 41 75 78 2c 20 20 20 20 20 20 20 20  d *pAux,        
40b59 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
40b5a 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f 6b 65 6e  Pointer to token
40b5b 69 7a 65 72 20 68 61 73 68 20 74 61 62 6c 65 20  izer hash table 
40b5c 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 20  */.  int argc,  
40b5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40b5e 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
40b5f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 72  f elements in ar
40b60 67 76 20 61 72 72 61 79 20 2a 2f 0a 20 20 63 6f  gv array */.  co
40b61 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74  nst char * const
40b62 20 2a 61 72 67 76 2c 20 20 20 20 20 20 20 2f 2a   *argv,       /*
40b63 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63   xCreate/xConnec
40b64 74 20 61 72 67 75 6d 65 6e 74 20 61 72 72 61 79  t argument array
40b65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74   */.  sqlite3_vt
40b66 61 62 20 2a 2a 70 70 56 74 61 62 2c 20 20 20 20  ab **ppVtab,    
40b67 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 65        /* OUT: Ne
40b68 77 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 6f  w sqlite3_vtab o
40b69 62 6a 65 63 74 20 2a 2f 0a 20 20 63 68 61 72 20  bject */.  char 
40b6a 2a 2a 70 7a 45 72 72 20 20 20 20 20 20 20 20 20  **pzErr         
40b6b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
40b6c 54 3a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  T: sqlite3_mallo
40b6d 63 27 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67  c'd error messag
40b6e 65 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e  e */.){.  return
40b6f 20 66 74 73 33 49 6e 69 74 56 74 61 62 28 31 2c   fts3InitVtab(1,
40b70 20 64 62 2c 20 70 41 75 78 2c 20 61 72 67 63 2c   db, pAux, argc,
40b71 20 61 72 67 76 2c 20 70 70 56 74 61 62 2c 20 70   argv, ppVtab, p
40b72 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  zErr);.}../* .**
40b73 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
40b74 6f 66 20 74 68 65 20 78 42 65 73 74 49 6e 64 65  of the xBestInde
40b75 78 20 6d 65 74 68 6f 64 20 66 6f 72 20 46 54 53  x method for FTS
40b76 33 20 74 61 62 6c 65 73 2e 20 54 68 65 72 65 0a  3 tables. There.
40b77 2a 2a 20 61 72 65 20 74 68 72 65 65 20 70 6f 73  ** are three pos
40b78 73 69 62 6c 65 20 73 74 72 61 74 65 67 69 65 73  sible strategies
40b79 2c 20 69 6e 20 6f 72 64 65 72 20 6f 66 20 70 72  , in order of pr
40b7a 65 66 65 72 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20  eference:.**.** 
40b7b 20 20 31 2e 20 44 69 72 65 63 74 20 6c 6f 6f 6b    1. Direct look
40b7c 75 70 20 62 79 20 72 6f 77 69 64 20 6f 72 20 64  up by rowid or d
40b7d 6f 63 69 64 2e 20 0a 2a 2a 20 20 20 32 2e 20 46  ocid. .**   2. F
40b7e 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 20  ull-text search 
40b7f 75 73 69 6e 67 20 61 20 4d 41 54 43 48 20 6f 70  using a MATCH op
40b80 65 72 61 74 6f 72 20 6f 6e 20 61 20 6e 6f 6e 2d  erator on a non-
40b81 64 6f 63 69 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a  docid column..**
40b82 20 20 20 33 2e 20 4c 69 6e 65 61 72 20 73 63 61     3. Linear sca
40b83 6e 20 6f 66 20 25 5f 63 6f 6e 74 65 6e 74 20 74  n of %_content t
40b84 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
40b85 69 6e 74 20 66 74 73 33 42 65 73 74 49 6e 64 65  int fts3BestInde
40b86 78 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f  xMethod(sqlite3_
40b87 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c  vtab *pVTab, sql
40b88 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
40b89 2a 70 49 6e 66 6f 29 7b 0a 20 20 46 74 73 33 54  *pInfo){.  Fts3T
40b8a 61 62 6c 65 20 2a 70 20 3d 20 28 46 74 73 33 54  able *p = (Fts3T
40b8b 61 62 6c 65 20 2a 29 70 56 54 61 62 3b 0a 20 20  able *)pVTab;.  
40b8c 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
40b8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40b8e 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
40b8f 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43  able */.  int iC
40b90 6f 6e 73 20 3d 20 2d 31 3b 20 20 20 20 20 20 20  ons = -1;       
40b91 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
40b92 65 78 20 6f 66 20 63 6f 6e 73 74 72 61 69 6e 74  ex of constraint
40b93 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74   to use */.  int
40b94 20 69 4c 61 6e 67 69 64 43 6f 6e 73 20 3d 20 2d   iLangidCons = -
40b95 31 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  1;           /* 
40b96 49 6e 64 65 78 20 6f 66 20 6c 61 6e 67 69 64 3d  Index of langid=
40b97 78 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 69 66  x constraint, if
40b98 20 70 72 65 73 65 6e 74 20 2a 2f 0a 0a 20 20 2f   present */..  /
40b99 2a 20 42 79 20 64 65 66 61 75 6c 74 20 75 73 65  * By default use
40b9a 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63   a full table sc
40b9b 61 6e 2e 20 54 68 69 73 20 69 73 20 61 6e 20 65  an. This is an e
40b9c 78 70 65 6e 73 69 76 65 20 6f 70 74 69 6f 6e 2c  xpensive option,
40b9d 0a 20 20 2a 2a 20 73 6f 20 73 65 61 72 63 68 20  .  ** so search 
40b9e 74 68 72 6f 75 67 68 20 74 68 65 20 63 6f 6e 73  through the cons
40b9f 74 72 61 69 6e 74 73 20 74 6f 20 73 65 65 20 69  traints to see i
40ba0 66 20 61 20 6d 6f 72 65 20 65 66 66 69 63 69 65  f a more efficie
40ba1 6e 74 20 0a 20 20 2a 2a 20 73 74 72 61 74 65 67  nt .  ** strateg
40ba2 79 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a 20  y is possible.. 
40ba3 20 2a 2f 0a 20 20 70 49 6e 66 6f 2d 3e 69 64 78   */.  pInfo->idx
40ba4 4e 75 6d 20 3d 20 46 54 53 33 5f 46 55 4c 4c 53  Num = FTS3_FULLS
40ba5 43 41 4e 5f 53 45 41 52 43 48 3b 0a 20 20 70 49  CAN_SEARCH;.  pI
40ba6 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f  nfo->estimatedCo
40ba7 73 74 20 3d 20 35 30 30 30 30 30 3b 0a 20 20 66  st = 500000;.  f
40ba8 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 66 6f 2d  or(i=0; i<pInfo-
40ba9 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b  >nConstraint; i+
40baa 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 73  +){.    struct s
40bab 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
40bac 73 74 72 61 69 6e 74 20 2a 70 43 6f 6e 73 20 3d  straint *pCons =
40bad 20 26 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72   &pInfo->aConstr
40bae 61 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28  aint[i];.    if(
40baf 20 70 43 6f 6e 73 2d 3e 75 73 61 62 6c 65 3d 3d   pCons->usable==
40bb0 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20  0 ) continue;.. 
40bb1 20 20 20 2f 2a 20 41 20 64 69 72 65 63 74 20 6c     /* A direct l
40bb2 6f 6f 6b 75 70 20 6f 6e 20 74 68 65 20 72 6f 77  ookup on the row
40bb3 69 64 20 6f 72 20 64 6f 63 69 64 20 63 6f 6c 75  id or docid colu
40bb4 6d 6e 2e 20 41 73 73 69 67 6e 20 61 20 63 6f 73  mn. Assign a cos
40bb5 74 20 6f 66 20 31 2e 30 2e 20 2a 2f 0a 20 20 20  t of 1.0. */.   
40bb6 20 69 66 28 20 69 43 6f 6e 73 3c 30 20 0a 20 20   if( iCons<0 .  
40bb7 20 20 20 26 26 20 70 43 6f 6e 73 2d 3e 6f 70 3d     && pCons->op=
40bb8 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f  =SQLITE_INDEX_CO
40bb9 4e 53 54 52 41 49 4e 54 5f 45 51 20 0a 20 20 20  NSTRAINT_EQ .   
40bba 20 20 26 26 20 28 70 43 6f 6e 73 2d 3e 69 43 6f    && (pCons->iCo
40bbb 6c 75 6d 6e 3c 30 20 7c 7c 20 70 43 6f 6e 73 2d  lumn<0 || pCons-
40bbc 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 2d 3e 6e 43 6f  >iColumn==p->nCo
40bbd 6c 75 6d 6e 2b 31 20 29 0a 20 20 20 20 29 7b 0a  lumn+1 ).    ){.
40bbe 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 69 64 78        pInfo->idx
40bbf 4e 75 6d 20 3d 20 46 54 53 33 5f 44 4f 43 49 44  Num = FTS3_DOCID
40bc0 5f 53 45 41 52 43 48 3b 0a 20 20 20 20 20 20 70  _SEARCH;.      p
40bc1 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43  Info->estimatedC
40bc2 6f 73 74 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20  ost = 1.0;.     
40bc3 20 69 43 6f 6e 73 20 3d 20 69 3b 0a 20 20 20 20   iCons = i;.    
40bc4 7d 0a 0a 20 20 20 20 2f 2a 20 41 20 4d 41 54 43  }..    /* A MATC
40bc5 48 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 55 73  H constraint. Us
40bc6 65 20 61 20 66 75 6c 6c 2d 74 65 78 74 20 73 65  e a full-text se
40bc7 61 72 63 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  arch..    **.   
40bc8 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   ** If there is 
40bc9 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 4d 41  more than one MA
40bca 54 43 48 20 63 6f 6e 73 74 72 61 69 6e 74 20 61  TCH constraint a
40bcb 76 61 69 6c 61 62 6c 65 2c 20 75 73 65 20 74 68  vailable, use th
40bcc 65 20 66 69 72 73 74 0a 20 20 20 20 2a 2a 20 6f  e first.    ** o
40bcd 6e 65 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20  ne encountered. 
40bce 49 66 20 74 68 65 72 65 20 69 73 20 62 6f 74 68  If there is both
40bcf 20 61 20 4d 41 54 43 48 20 63 6f 6e 73 74 72 61   a MATCH constra
40bd0 69 6e 74 20 61 6e 64 20 61 20 64 69 72 65 63 74  int and a direct
40bd1 0a 20 20 20 20 2a 2a 20 72 6f 77 69 64 2f 64 6f  .    ** rowid/do
40bd2 63 69 64 20 6c 6f 6f 6b 75 70 2c 20 70 72 65 66  cid lookup, pref
40bd3 65 72 20 74 68 65 20 4d 41 54 43 48 20 73 74 72  er the MATCH str
40bd4 61 74 65 67 79 2e 20 54 68 69 73 20 69 73 20 64  ategy. This is d
40bd5 6f 6e 65 20 65 76 65 6e 20 0a 20 20 20 20 2a 2a  one even .    **
40bd6 20 74 68 6f 75 67 68 20 74 68 65 20 72 6f 77 69   though the rowi
40bd7 64 2f 64 6f 63 69 64 20 6c 6f 6f 6b 75 70 20 69  d/docid lookup i
40bd8 73 20 66 61 73 74 65 72 20 74 68 61 6e 20 61 20  s faster than a 
40bd9 4d 41 54 43 48 20 71 75 65 72 79 2c 20 73 65 6c  MATCH query, sel
40bda 65 63 74 69 6e 67 0a 20 20 20 20 2a 2a 20 69 74  ecting.    ** it
40bdb 20 77 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20 61   would lead to a
40bdc 6e 20 22 75 6e 61 62 6c 65 20 74 6f 20 75 73 65  n "unable to use
40bdd 20 66 75 6e 63 74 69 6f 6e 20 4d 41 54 43 48 20   function MATCH 
40bde 69 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64  in the requested
40bdf 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 65 78 74   .    ** context
40be0 22 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a  " error..    */.
40be1 20 20 20 20 69 66 28 20 70 43 6f 6e 73 2d 3e 6f      if( pCons->o
40be2 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  p==SQLITE_INDEX_
40be3 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48  CONSTRAINT_MATCH
40be4 20 0a 20 20 20 20 20 26 26 20 70 43 6f 6e 73 2d   .     && pCons-
40be5 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70  >iColumn>=0 && p
40be6 43 6f 6e 73 2d 3e 69 43 6f 6c 75 6d 6e 3c 3d 70  Cons->iColumn<=p
40be7 2d 3e 6e 43 6f 6c 75 6d 6e 0a 20 20 20 20 29 7b  ->nColumn.    ){
40be8 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 69 64  .      pInfo->id
40be9 78 4e 75 6d 20 3d 20 46 54 53 33 5f 46 55 4c 4c  xNum = FTS3_FULL
40bea 54 45 58 54 5f 53 45 41 52 43 48 20 2b 20 70 43  TEXT_SEARCH + pC
40beb 6f 6e 73 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  ons->iColumn;.  
40bec 20 20 20 20 70 49 6e 66 6f 2d 3e 65 73 74 69 6d      pInfo->estim
40bed 61 74 65 64 43 6f 73 74 20 3d 20 32 2e 30 3b 0a  atedCost = 2.0;.
40bee 20 20 20 20 20 20 69 43 6f 6e 73 20 3d 20 69 3b        iCons = i;
40bef 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 45  .    }..    /* E
40bf0 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
40bf1 6e 74 20 6f 6e 20 74 68 65 20 6c 61 6e 67 69 64  nt on the langid
40bf2 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 69   column */.    i
40bf3 66 28 20 70 43 6f 6e 73 2d 3e 6f 70 3d 3d 53 51  f( pCons->op==SQ
40bf4 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
40bf5 52 41 49 4e 54 5f 45 51 20 0a 20 20 20 20 20 26  RAINT_EQ .     &
40bf6 26 20 70 43 6f 6e 73 2d 3e 69 43 6f 6c 75 6d 6e  & pCons->iColumn
40bf7 3d 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 2b 20 32  ==p->nColumn + 2
40bf8 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 4c  .    ){.      iL
40bf9 61 6e 67 69 64 43 6f 6e 73 20 3d 20 69 3b 0a 20  angidCons = i;. 
40bfa 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
40bfb 69 43 6f 6e 73 3e 3d 30 20 29 7b 0a 20 20 20 20  iCons>=0 ){.    
40bfc 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  pInfo->aConstrai
40bfd 6e 74 55 73 61 67 65 5b 69 43 6f 6e 73 5d 2e 61  ntUsage[iCons].a
40bfe 72 67 76 49 6e 64 65 78 20 3d 20 31 3b 0a 20 20  rgvIndex = 1;.  
40bff 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72    pInfo->aConstr
40c00 61 69 6e 74 55 73 61 67 65 5b 69 43 6f 6e 73 5d  aintUsage[iCons]
40c01 2e 6f 6d 69 74 20 3d 20 31 3b 0a 20 20 7d 20 0a  .omit = 1;.  } .
40c02 20 20 69 66 28 20 69 4c 61 6e 67 69 64 43 6f 6e    if( iLangidCon
40c03 73 3e 3d 30 20 29 7b 0a 20 20 20 20 70 49 6e 66  s>=0 ){.    pInf
40c04 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  o->aConstraintUs
40c05 61 67 65 5b 69 4c 61 6e 67 69 64 43 6f 6e 73 5d  age[iLangidCons]
40c06 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 32 3b 0a  .argvIndex = 2;.
40c07 20 20 7d 20 0a 0a 20 20 2f 2a 20 52 65 67 61 72    } ..  /* Regar
40c08 64 6c 65 73 73 20 6f 66 20 74 68 65 20 73 74 72  dless of the str
40c09 61 74 65 67 79 20 73 65 6c 65 63 74 65 64 2c 20  ategy selected, 
40c0a 46 54 53 20 63 61 6e 20 64 65 6c 69 76 65 72 20  FTS can deliver 
40c0b 72 6f 77 73 20 69 6e 20 72 6f 77 69 64 20 28 6f  rows in rowid (o
40c0c 72 0a 20 20 2a 2a 20 64 6f 63 69 64 29 20 6f 72  r.  ** docid) or
40c0d 64 65 72 2e 20 42 6f 74 68 20 61 73 63 65 6e 64  der. Both ascend
40c0e 69 6e 67 20 61 6e 64 20 64 65 73 63 65 6e 64 69  ing and descendi
40c0f 6e 67 20 61 72 65 20 70 6f 73 73 69 62 6c 65 2e  ng are possible.
40c10 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e   .  */.  if( pIn
40c11 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 3d 3d 31 20  fo->nOrderBy==1 
40c12 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 73 71  ){.    struct sq
40c13 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65  lite3_index_orde
40c14 72 62 79 20 2a 70 4f 72 64 65 72 20 3d 20 26 70  rby *pOrder = &p
40c15 49 6e 66 6f 2d 3e 61 4f 72 64 65 72 42 79 5b 30  Info->aOrderBy[0
40c16 5d 3b 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65  ];.    if( pOrde
40c17 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 7c 7c 20  r->iColumn<0 || 
40c18 70 4f 72 64 65 72 2d 3e 69 43 6f 6c 75 6d 6e 3d  pOrder->iColumn=
40c19 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 20 29 7b  =p->nColumn+1 ){
40c1a 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65  .      if( pOrde
40c1b 72 2d 3e 64 65 73 63 20 29 7b 0a 20 20 20 20 20  r->desc ){.     
40c1c 20 20 20 70 49 6e 66 6f 2d 3e 69 64 78 53 74 72     pInfo->idxStr
40c1d 20 3d 20 22 44 45 53 43 22 3b 0a 20 20 20 20 20   = "DESC";.     
40c1e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
40c1f 70 49 6e 66 6f 2d 3e 69 64 78 53 74 72 20 3d 20  pInfo->idxStr = 
40c20 22 41 53 43 22 3b 0a 20 20 20 20 20 20 7d 0a 20  "ASC";.      }. 
40c21 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6f 72 64 65       pInfo->orde
40c22 72 42 79 43 6f 6e 73 75 6d 65 64 20 3d 20 31 3b  rByConsumed = 1;
40c23 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73  .    }.  }..  as
40c24 73 65 72 74 28 20 70 2d 3e 70 53 65 67 6d 65 6e  sert( p->pSegmen
40c25 74 73 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72  ts==0 );.  retur
40c26 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
40c27 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
40c28 74 69 6f 6e 20 6f 66 20 78 4f 70 65 6e 20 6d 65  tion of xOpen me
40c29 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
40c2a 69 6e 74 20 66 74 73 33 4f 70 65 6e 4d 65 74 68  int fts3OpenMeth
40c2b 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  od(sqlite3_vtab 
40c2c 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f  *pVTab, sqlite3_
40c2d 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 2a 70 70  vtab_cursor **pp
40c2e 43 73 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  Csr){.  sqlite3_
40c2f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 73  vtab_cursor *pCs
40c30 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
40c31 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 63 75   /* Allocated cu
40c32 72 73 6f 72 20 2a 2f 0a 0a 20 20 55 4e 55 53 45  rsor */..  UNUSE
40c33 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 54 61  D_PARAMETER(pVTa
40c34 62 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61  b);..  /* Alloca
40c35 74 65 20 61 20 62 75 66 66 65 72 20 6c 61 72 67  te a buffer larg
40c36 65 20 65 6e 6f 75 67 68 20 66 6f 72 20 61 6e 20  e enough for an 
40c37 46 74 73 33 43 75 72 73 6f 72 20 73 74 72 75 63  Fts3Cursor struc
40c38 74 75 72 65 2e 20 49 66 20 74 68 65 0a 20 20 2a  ture. If the.  *
40c39 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 63  * allocation suc
40c3a 63 65 65 64 73 2c 20 7a 65 72 6f 20 69 74 20 61  ceeds, zero it a
40c3b 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  nd return SQLITE
40c3c 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  _OK. Otherwise, 
40c3d 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 61 6c 6c  .  ** if the all
40c3e 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 72  ocation fails, r
40c3f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
40c40 45 4d 2e 0a 20 20 2a 2f 0a 20 20 2a 70 70 43 73  EM..  */.  *ppCs
40c41 72 20 3d 20 70 43 73 72 20 3d 20 28 73 71 6c 69  r = pCsr = (sqli
40c42 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
40c43 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
40c44 28 73 69 7a 65 6f 66 28 46 74 73 33 43 75 72 73  (sizeof(Fts3Curs
40c45 6f 72 29 29 3b 0a 20 20 69 66 28 20 21 70 43 73  or));.  if( !pCs
40c46 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  r ){.    return 
40c47 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
40c48 7d 0a 20 20 6d 65 6d 73 65 74 28 70 43 73 72 2c  }.  memset(pCsr,
40c49 20 30 2c 20 73 69 7a 65 6f 66 28 46 74 73 33 43   0, sizeof(Fts3C
40c4a 75 72 73 6f 72 29 29 3b 0a 20 20 72 65 74 75 72  ursor));.  retur
40c4b 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
40c4c 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20  /*.** Close the 
40c4d 63 75 72 73 6f 72 2e 20 20 46 6f 72 20 61 64 64  cursor.  For add
40c4e 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
40c4f 69 6f 6e 20 73 65 65 20 74 68 65 20 64 6f 63 75  ion see the docu
40c50 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20  mentation.** on 
40c51 74 68 65 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f  the xClose metho
40c52 64 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  d of the virtual
40c53 20 74 61 62 6c 65 20 69 6e 74 65 72 66 61 63 65   table interface
40c54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
40c55 66 74 73 33 43 6c 6f 73 65 4d 65 74 68 6f 64 28  fts3CloseMethod(
40c56 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
40c57 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20  sor *pCursor){. 
40c58 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73   Fts3Cursor *pCs
40c59 72 20 3d 20 28 46 74 73 33 43 75 72 73 6f 72 20  r = (Fts3Cursor 
40c5a 2a 29 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 73  *)pCursor;.  ass
40c5b 65 72 74 28 20 28 28 46 74 73 33 54 61 62 6c 65  ert( ((Fts3Table
40c5c 20 2a 29 70 43 73 72 2d 3e 62 61 73 65 2e 70 56   *)pCsr->base.pV
40c5d 74 61 62 29 2d 3e 70 53 65 67 6d 65 6e 74 73 3d  tab)->pSegments=
40c5e 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  =0 );.  sqlite3_
40c5f 66 69 6e 61 6c 69 7a 65 28 70 43 73 72 2d 3e 70  finalize(pCsr->p
40c60 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  Stmt);.  sqlite3
40c61 46 74 73 33 45 78 70 72 46 72 65 65 28 70 43 73  Fts3ExprFree(pCs
40c62 72 2d 3e 70 45 78 70 72 29 3b 0a 20 20 73 71 6c  r->pExpr);.  sql
40c63 69 74 65 33 46 74 73 33 46 72 65 65 44 65 66 65  ite3Fts3FreeDefe
40c64 72 72 65 64 54 6f 6b 65 6e 73 28 70 43 73 72 29  rredTokens(pCsr)
40c65 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
40c66 28 70 43 73 72 2d 3e 61 44 6f 63 6c 69 73 74 29  (pCsr->aDoclist)
40c67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
40c68 28 70 43 73 72 2d 3e 61 4d 61 74 63 68 69 6e 66  (pCsr->aMatchinf
40c69 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 28  o);.  assert( ((
40c6a 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43 73 72  Fts3Table *)pCsr
40c6b 2d 3e 62 61 73 65 2e 70 56 74 61 62 29 2d 3e 70  ->base.pVtab)->p
40c6c 53 65 67 6d 65 6e 74 73 3d 3d 30 20 29 3b 0a 20  Segments==0 );. 
40c6d 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
40c6e 73 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  sr);.  return SQ
40c6f 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
40c70 2a 20 49 66 20 70 43 73 72 2d 3e 70 53 74 6d 74  * If pCsr->pStmt
40c71 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 70 72   has not been pr
40c72 65 70 61 72 65 64 20 28 69 2e 65 2e 20 69 66 20  epared (i.e. if 
40c73 70 43 73 72 2d 3e 70 53 74 6d 74 3d 3d 30 29 2c  pCsr->pStmt==0),
40c74 20 74 68 65 6e 0a 2a 2a 20 63 6f 6d 70 6f 73 65   then.** compose
40c75 20 61 6e 64 20 70 72 65 70 61 72 65 20 61 6e 20   and prepare an 
40c76 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 6f 66  SQL statement of
40c77 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a   the form:.**.**
40c78 20 20 20 20 22 53 45 4c 45 43 54 20 3c 63 6f 6c      "SELECT <col
40c79 75 6d 6e 73 3e 20 46 52 4f 4d 20 25 5f 63 6f 6e  umns> FROM %_con
40c7a 74 65 6e 74 20 57 48 45 52 45 20 72 6f 77 69 64  tent WHERE rowid
40c7b 20 3d 20 3f 22 0a 2a 2a 0a 2a 2a 20 28 6f 72 20   = ?".**.** (or 
40c7c 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66  the equivalent f
40c7d 6f 72 20 61 20 63 6f 6e 74 65 6e 74 3d 78 78 78  or a content=xxx
40c7e 20 74 61 62 6c 65 29 20 61 6e 64 20 73 65 74 20   table) and set 
40c7f 70 43 73 72 2d 3e 70 53 74 6d 74 20 74 6f 0a 2a  pCsr->pStmt to.*
40c80 2a 20 69 74 2e 20 49 66 20 61 6e 20 65 72 72 6f  * it. If an erro
40c81 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e  r occurs, return
40c82 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
40c83 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68   code..**.** Oth
40c84 65 72 77 69 73 65 2c 20 73 65 74 20 2a 70 70 53  erwise, set *ppS
40c85 74 6d 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  tmt to point to 
40c86 70 43 73 72 2d 3e 70 53 74 6d 74 20 61 6e 64 20  pCsr->pStmt and 
40c87 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
40c88 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
40c89 66 74 73 33 43 75 72 73 6f 72 53 65 65 6b 53 74  fts3CursorSeekSt
40c8a 6d 74 28 46 74 73 33 43 75 72 73 6f 72 20 2a 70  mt(Fts3Cursor *p
40c8b 43 73 72 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d  Csr, sqlite3_stm
40c8c 74 20 2a 2a 70 70 53 74 6d 74 29 7b 0a 20 20 69  t **ppStmt){.  i
40c8d 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
40c8e 4b 3b 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 70  K;.  if( pCsr->p
40c8f 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 46  Stmt==0 ){.    F
40c90 74 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28 46  ts3Table *p = (F
40c91 74 73 33 54 61 62 6c 65 20 2a 29 70 43 73 72 2d  ts3Table *)pCsr-
40c92 3e 62 61 73 65 2e 70 56 74 61 62 3b 0a 20 20 20  >base.pVtab;.   
40c93 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20   char *zSql;.   
40c94 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f   zSql = sqlite3_
40c95 6d 70 72 69 6e 74 66 28 22 53 45 4c 45 43 54 20  mprintf("SELECT 
40c96 25 73 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d  %s WHERE rowid =
40c97 20 3f 22 2c 20 70 2d 3e 7a 52 65 61 64 45 78 70   ?", p->zReadExp
40c98 72 6c 69 73 74 29 3b 0a 20 20 20 20 69 66 28 20  rlist);.    if( 
40c99 21 7a 53 71 6c 20 29 20 72 65 74 75 72 6e 20 53  !zSql ) return S
40c9a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
40c9b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
40c9c 65 70 61 72 65 5f 76 32 28 70 2d 3e 64 62 2c 20  epare_v2(p->db, 
40c9d 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 43 73 72 2d  zSql, -1, &pCsr-
40c9e 3e 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20  >pStmt, 0);.    
40c9f 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71  sqlite3_free(zSq
40ca0 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 53 74 6d  l);.  }.  *ppStm
40ca1 74 20 3d 20 70 43 73 72 2d 3e 70 53 74 6d 74 3b  t = pCsr->pStmt;
40ca2 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
40ca3 0a 2f 2a 0a 2a 2a 20 50 6f 73 69 74 69 6f 6e 20  ./*.** Position 
40ca4 74 68 65 20 70 43 73 72 2d 3e 70 53 74 6d 74 20  the pCsr->pStmt 
40ca5 73 74 61 74 65 6d 65 6e 74 20 73 6f 20 74 68 61  statement so tha
40ca6 74 20 69 74 20 69 73 20 6f 6e 20 74 68 65 20 72  t it is on the r
40ca7 6f 77 0a 2a 2a 20 6f 66 20 74 68 65 20 25 5f 63  ow.** of the %_c
40ca8 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 74 68 61  ontent table tha
40ca9 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c  t contains the l
40caa 61 73 74 20 6d 61 74 63 68 2e 20 20 52 65 74 75  ast match.  Retu
40cab 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  rn.** SQLITE_OK 
40cac 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 0a 2a 2f  on success.  .*/
40cad 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
40cae 43 75 72 73 6f 72 53 65 65 6b 28 73 71 6c 69 74  CursorSeek(sqlit
40caf 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  e3_context *pCon
40cb0 74 65 78 74 2c 20 46 74 73 33 43 75 72 73 6f 72  text, Fts3Cursor
40cb1 20 2a 70 43 73 72 29 7b 0a 20 20 69 6e 74 20 72   *pCsr){.  int r
40cb2 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
40cb3 20 69 66 28 20 70 43 73 72 2d 3e 69 73 52 65 71   if( pCsr->isReq
40cb4 75 69 72 65 53 65 65 6b 20 29 7b 0a 20 20 20 20  uireSeek ){.    
40cb5 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
40cb6 74 6d 74 20 3d 20 30 3b 0a 0a 20 20 20 20 72 63  tmt = 0;..    rc
40cb7 20 3d 20 66 74 73 33 43 75 72 73 6f 72 53 65 65   = fts3CursorSee
40cb8 6b 53 74 6d 74 28 70 43 73 72 2c 20 26 70 53 74  kStmt(pCsr, &pSt
40cb9 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  mt);.    if( rc=
40cba 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
40cbb 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
40cbc 5f 69 6e 74 36 34 28 70 43 73 72 2d 3e 70 53 74  _int64(pCsr->pSt
40cbd 6d 74 2c 20 31 2c 20 70 43 73 72 2d 3e 69 50 72  mt, 1, pCsr->iPr
40cbe 65 76 49 64 29 3b 0a 20 20 20 20 20 20 70 43 73  evId);.      pCs
40cbf 72 2d 3e 69 73 52 65 71 75 69 72 65 53 65 65 6b  r->isRequireSeek
40cc0 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20   = 0;.      if( 
40cc1 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69  SQLITE_ROW==sqli
40cc2 74 65 33 5f 73 74 65 70 28 70 43 73 72 2d 3e 70  te3_step(pCsr->p
40cc3 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 20  Stmt) ){.       
40cc4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
40cc5 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  K;.      }else{.
40cc6 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
40cc7 69 74 65 33 5f 72 65 73 65 74 28 70 43 73 72 2d  ite3_reset(pCsr-
40cc8 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 20  >pStmt);.       
40cc9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
40cca 4f 4b 20 26 26 20 28 28 46 74 73 33 54 61 62 6c  OK && ((Fts3Tabl
40ccb 65 20 2a 29 70 43 73 72 2d 3e 62 61 73 65 2e 70  e *)pCsr->base.p
40ccc 56 74 61 62 29 2d 3e 7a 43 6f 6e 74 65 6e 74 54  Vtab)->zContentT
40ccd 62 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  bl==0 ){.       
40cce 20 20 20 2f 2a 20 49 66 20 6e 6f 20 72 6f 77 20     /* If no row 
40ccf 77 61 73 20 66 6f 75 6e 64 20 61 6e 64 20 6e 6f  was found and no
40cd0 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72   error has occur
40cd1 65 64 2c 20 74 68 65 6e 20 74 68 65 20 25 5f 63  ed, then the %_c
40cd2 6f 6e 74 65 6e 74 0a 20 20 20 20 20 20 20 20 20  ontent.         
40cd3 20 2a 2a 20 74 61 62 6c 65 20 69 73 20 6d 69 73   ** table is mis
40cd4 73 69 6e 67 20 61 20 72 6f 77 20 74 68 61 74 20  sing a row that 
40cd5 69 73 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68  is present in th
40cd6 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65  e full-text inde
40cd7 78 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  x..          ** 
40cd8 54 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75  The data structu
40cd9 72 65 73 20 61 72 65 20 63 6f 72 72 75 70 74 2e  res are corrupt.
40cda 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72    */.          r
40cdb 63 20 3d 20 46 54 53 5f 43 4f 52 52 55 50 54 5f  c = FTS_CORRUPT_
40cdc 56 54 41 42 3b 0a 20 20 20 20 20 20 20 20 20 20  VTAB;.          
40cdd 70 43 73 72 2d 3e 69 73 45 6f 66 20 3d 20 31 3b  pCsr->isEof = 1;
40cde 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
40cdf 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
40ce0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
40ce1 4b 20 26 26 20 70 43 6f 6e 74 65 78 74 20 29 7b  K && pContext ){
40ce2 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
40ce3 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 70  ult_error_code(p
40ce4 43 6f 6e 74 65 78 74 2c 20 72 63 29 3b 0a 20 20  Context, rc);.  
40ce5 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
40ce6 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
40ce7 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ction is used to
40ce8 20 70 72 6f 63 65 73 73 20 61 20 73 69 6e 67 6c   process a singl
40ce9 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  e interior node 
40cea 77 68 65 6e 20 73 65 61 72 63 68 69 6e 67 0a 2a  when searching.*
40ceb 2a 20 61 20 62 2d 74 72 65 65 20 66 6f 72 20 61  * a b-tree for a
40cec 20 74 65 72 6d 20 6f 72 20 74 65 72 6d 20 70 72   term or term pr
40ced 65 66 69 78 2e 20 54 68 65 20 6e 6f 64 65 20 64  efix. The node d
40cee 61 74 61 20 69 73 20 70 61 73 73 65 64 20 74 6f  ata is passed to
40cef 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69   this .** functi
40cf0 6f 6e 20 76 69 61 20 74 68 65 20 7a 4e 6f 64 65  on via the zNode
40cf1 2f 6e 4e 6f 64 65 20 70 61 72 61 6d 65 74 65 72  /nNode parameter
40cf2 73 2e 20 54 68 65 20 74 65 72 6d 20 74 6f 20 73  s. The term to s
40cf3 65 61 72 63 68 20 66 6f 72 20 69 73 0a 2a 2a 20  earch for is.** 
40cf4 70 61 73 73 65 64 20 69 6e 20 7a 54 65 72 6d 2f  passed in zTerm/
40cf5 6e 54 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  nTerm..**.** If 
40cf6 70 69 46 69 72 73 74 20 69 73 20 6e 6f 74 20 4e  piFirst is not N
40cf7 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 20 66  ULL, then this f
40cf8 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 2a 70 69  unction sets *pi
40cf9 46 69 72 73 74 20 74 6f 20 74 68 65 20 62 6c 6f  First to the blo
40cfa 63 6b 69 64 0a 2a 2a 20 6f 66 20 74 68 65 20 63  ckid.** of the c
40cfb 68 69 6c 64 20 6e 6f 64 65 20 74 68 61 74 20 68  hild node that h
40cfc 65 61 64 73 20 74 68 65 20 73 75 62 2d 74 72 65  eads the sub-tre
40cfd 65 20 74 68 61 74 20 6d 61 79 20 63 6f 6e 74 61  e that may conta
40cfe 69 6e 20 74 68 65 20 74 65 72 6d 2e 0a 2a 2a 0a  in the term..**.
40cff 2a 2a 20 49 66 20 70 69 4c 61 73 74 20 69 73 20  ** If piLast is 
40d00 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 2a  not NULL, then *
40d01 70 69 4c 61 73 74 20 69 73 20 73 65 74 20 74 6f  piLast is set to
40d02 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   the right-most 
40d03 63 68 69 6c 64 20 6e 6f 64 65 0a 2a 2a 20 74 68  child node.** th
40d04 61 74 20 68 65 61 64 73 20 61 20 73 75 62 2d 74  at heads a sub-t
40d05 72 65 65 20 74 68 61 74 20 6d 61 79 20 63 6f 6e  ree that may con
40d06 74 61 69 6e 20 61 20 74 65 72 6d 20 66 6f 72 20  tain a term for 
40d07 77 68 69 63 68 20 7a 54 65 72 6d 2f 6e 54 65 72  which zTerm/nTer
40d08 6d 20 69 73 0a 2a 2a 20 61 20 70 72 65 66 69 78  m is.** a prefix
40d09 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 4f 4f  ..**.** If an OO
40d0a 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  M error occurs, 
40d0b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20  SQLITE_NOMEM is 
40d0c 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
40d0d 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a  ise, SQLITE_OK..
40d0e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
40d0f 73 33 53 63 61 6e 49 6e 74 65 72 69 6f 72 4e 6f  s3ScanInteriorNo
40d10 64 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  de(.  const char
40d11 20 2a 7a 54 65 72 6d 2c 20 20 20 20 20 20 20 20   *zTerm,        
40d12 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f        /* Term to
40d13 20 73 65 6c 65 63 74 20 6c 65 61 76 65 73 20 66   select leaves f
40d14 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72  or */.  int nTer
40d15 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m,              
40d16 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
40d17 6f 66 20 74 65 72 6d 20 7a 54 65 72 6d 20 69 6e  of term zTerm in
40d18 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 6f 6e 73   bytes */.  cons
40d19 74 20 63 68 61 72 20 2a 7a 4e 6f 64 65 2c 20 20  t char *zNode,  
40d1a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
40d1b 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  uffer containing
40d1c 20 73 65 67 6d 65 6e 74 20 69 6e 74 65 72 69 6f   segment interio
40d1d 72 20 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  r node */.  int 
40d1e 6e 4e 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20  nNode,          
40d1f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
40d20 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20 61 74  ize of buffer at
40d21 20 7a 4e 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69   zNode */.  sqli
40d22 74 65 33 5f 69 6e 74 36 34 20 2a 70 69 46 69 72  te3_int64 *piFir
40d23 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  st,         /* O
40d24 55 54 3a 20 53 65 6c 65 63 74 65 64 20 63 68 69  UT: Selected chi
40d25 6c 64 20 6e 6f 64 65 20 2a 2f 0a 20 20 73 71 6c  ld node */.  sql
40d26 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 69 4c 61  ite3_int64 *piLa
40d27 73 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  st           /* 
40d28 4f 55 54 3a 20 53 65 6c 65 63 74 65 64 20 63 68  OUT: Selected ch
40d29 69 6c 64 20 6e 6f 64 65 20 2a 2f 0a 29 7b 0a 20  ild node */.){. 
40d2a 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
40d2b 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
40d2c 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
40d2d 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
40d2e 2a 7a 43 73 72 20 3d 20 7a 4e 6f 64 65 3b 20 20  *zCsr = zNode;  
40d2f 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 74       /* Cursor t
40d30 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
40d31 68 20 6e 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73  h node */.  cons
40d32 74 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20 26  t char *zEnd = &
40d33 7a 43 73 72 5b 6e 4e 6f 64 65 5d 3b 2f 2a 20 45  zCsr[nNode];/* E
40d34 6e 64 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 6e  nd of interior n
40d35 6f 64 65 20 62 75 66 66 65 72 20 2a 2f 0a 20 20  ode buffer */.  
40d36 63 68 61 72 20 2a 7a 42 75 66 66 65 72 20 3d 20  char *zBuffer = 
40d37 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
40d38 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 6c 6f 61  /* Buffer to loa
40d39 64 20 74 65 72 6d 73 20 69 6e 74 6f 20 2a 2f 0a  d terms into */.
40d3a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 30    int nAlloc = 0
40d3b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
40d3c 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c    /* Size of all
40d3d 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 2a 2f  ocated buffer */
40d3e 0a 20 20 69 6e 74 20 69 73 46 69 72 73 74 54 65  .  int isFirstTe
40d3f 72 6d 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20  rm = 1;         
40d40 20 20 20 2f 2a 20 54 72 75 65 20 77 68 65 6e 20     /* True when 
40d41 70 72 6f 63 65 73 73 69 6e 67 20 66 69 72 73 74  processing first
40d42 20 74 65 72 6d 20 6f 6e 20 70 61 67 65 20 2a 2f   term on page */
40d43 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
40d44 20 69 43 68 69 6c 64 3b 20 20 20 20 20 20 20 20   iChild;        
40d45 20 20 20 2f 2a 20 42 6c 6f 63 6b 20 69 64 20 6f     /* Block id o
40d46 66 20 63 68 69 6c 64 20 6e 6f 64 65 20 74 6f 20  f child node to 
40d47 64 65 73 63 65 6e 64 20 74 6f 20 2a 2f 0a 0a 20  descend to */.. 
40d48 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20 74 68   /* Skip over th
40d49 65 20 27 68 65 69 67 68 74 27 20 76 61 72 69 6e  e 'height' varin
40d4a 74 20 74 68 61 74 20 6f 63 63 75 72 73 20 61 74  t that occurs at
40d4b 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 76   the start of ev
40d4c 65 72 79 20 0a 20 20 2a 2a 20 69 6e 74 65 72 69  ery .  ** interi
40d4d 6f 72 20 6e 6f 64 65 2e 20 54 68 65 6e 20 6c 6f  or node. Then lo
40d4e 61 64 20 74 68 65 20 62 6c 6f 63 6b 69 64 20 6f  ad the blockid o
40d4f 66 20 74 68 65 20 6c 65 66 74 2d 63 68 69 6c 64  f the left-child
40d50 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 0a 20   of the b-tree. 
40d51 20 2a 2a 20 6e 6f 64 65 20 69 6e 74 6f 20 76 61   ** node into va
40d52 72 69 61 62 6c 65 20 69 43 68 69 6c 64 2e 20 20  riable iChild.  
40d53 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 76 65 6e 20  .  **.  ** Even 
40d54 69 66 20 74 68 65 20 64 61 74 61 20 73 74 72 75  if the data stru
40d55 63 74 75 72 65 20 6f 6e 20 64 69 73 6b 20 69 73  cture on disk is
40d56 20 63 6f 72 72 75 70 74 65 64 2c 20 74 68 69 73   corrupted, this
40d57 20 28 72 65 61 64 69 6e 67 20 74 77 6f 0a 20 20   (reading two.  
40d58 2a 2a 20 76 61 72 69 6e 74 73 20 66 72 6f 6d 20  ** varints from 
40d59 74 68 65 20 62 75 66 66 65 72 29 20 64 6f 65 73  the buffer) does
40d5a 20 6e 6f 74 20 72 69 73 6b 20 61 6e 20 6f 76 65   not risk an ove
40d5b 72 72 65 61 64 2e 20 49 66 20 7a 4e 6f 64 65 20  rread. If zNode 
40d5c 69 73 20 61 0a 20 20 2a 2a 20 72 6f 6f 74 20 6e  is a.  ** root n
40d5d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 62 75  ode, then the bu
40d5e 66 66 65 72 20 63 6f 6d 65 73 20 66 72 6f 6d 20  ffer comes from 
40d5f 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
40d60 6e 74 2e 20 53 51 4c 69 74 65 20 64 6f 65 73 0a  nt. SQLite does.
40d61 20 20 2a 2a 20 6e 6f 74 20 6d 61 6b 65 20 74 68    ** not make th
40d62 69 73 20 67 75 61 72 61 6e 74 65 65 20 65 78 70  is guarantee exp
40d63 6c 69 63 69 74 6c 79 2c 20 62 75 74 20 69 6e 20  licitly, but in 
40d64 70 72 61 63 74 69 63 65 20 74 68 65 72 65 20 61  practice there a
40d65 72 65 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 65  re always.  ** e
40d66 69 74 68 65 72 20 6d 6f 72 65 20 74 68 61 6e 20  ither more than 
40d67 32 30 20 62 79 74 65 73 20 6f 66 20 61 6c 6c 6f  20 bytes of allo
40d68 63 61 74 65 64 20 73 70 61 63 65 20 66 6f 6c 6c  cated space foll
40d69 6f 77 69 6e 67 20 74 68 65 20 6e 4e 6f 64 65 20  owing the nNode 
40d6a 62 79 74 65 73 20 6f 66 0a 20 20 2a 2a 20 63 6f  bytes of.  ** co
40d6b 6e 74 65 6e 74 73 2c 20 6f 72 20 74 77 6f 20 7a  ntents, or two z
40d6c 65 72 6f 20 62 79 74 65 73 2e 20 4f 72 2c 20 69  ero bytes. Or, i
40d6d 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20 72 65  f the node is re
40d6e 61 64 20 66 72 6f 6d 20 74 68 65 20 25 5f 73 65  ad from the %_se
40d6f 67 6d 65 6e 74 73 0a 20 20 2a 2a 20 74 61 62 6c  gments.  ** tabl
40d70 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 61 72  e, then there ar
40d71 65 20 61 6c 77 61 79 73 20 32 30 20 62 79 74 65  e always 20 byte
40d72 73 20 6f 66 20 7a 65 72 6f 65 64 20 70 61 64 64  s of zeroed padd
40d73 69 6e 67 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  ing following th
40d74 65 0a 20 20 2a 2a 20 6e 4e 6f 64 65 20 62 79 74  e.  ** nNode byt
40d75 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 28 73  es of content (s
40d76 65 65 20 73 71 6c 69 74 65 33 46 74 73 33 52 65  ee sqlite3Fts3Re
40d77 61 64 42 6c 6f 63 6b 28 29 20 66 6f 72 20 64 65  adBlock() for de
40d78 74 61 69 6c 73 29 2e 0a 20 20 2a 2f 0a 20 20 7a  tails)..  */.  z
40d79 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 46 74  Csr += sqlite3Ft
40d7a 73 33 47 65 74 56 61 72 69 6e 74 28 7a 43 73 72  s3GetVarint(zCsr
40d7b 2c 20 26 69 43 68 69 6c 64 29 3b 0a 20 20 7a 43  , &iChild);.  zC
40d7c 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73  sr += sqlite3Fts
40d7d 33 47 65 74 56 61 72 69 6e 74 28 7a 43 73 72 2c  3GetVarint(zCsr,
40d7e 20 26 69 43 68 69 6c 64 29 3b 0a 20 20 69 66 28   &iChild);.  if(
40d7f 20 7a 43 73 72 3e 7a 45 6e 64 20 29 7b 0a 20 20   zCsr>zEnd ){.  
40d80 20 20 72 65 74 75 72 6e 20 46 54 53 5f 43 4f 52    return FTS_COR
40d81 52 55 50 54 5f 56 54 41 42 3b 0a 20 20 7d 0a 20  RUPT_VTAB;.  }. 
40d82 20 0a 20 20 77 68 69 6c 65 28 20 7a 43 73 72 3c   .  while( zCsr<
40d83 7a 45 6e 64 20 26 26 20 28 70 69 46 69 72 73 74  zEnd && (piFirst
40d84 20 7c 7c 20 70 69 4c 61 73 74 29 20 29 7b 0a 20   || piLast) ){. 
40d85 20 20 20 69 6e 74 20 63 6d 70 3b 20 20 20 20 20     int cmp;     
40d86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40d87 20 2f 2a 20 6d 65 6d 63 6d 70 28 29 20 72 65 73   /* memcmp() res
40d88 75 6c 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ult */.    int n
40d89 53 75 66 66 69 78 3b 20 20 20 20 20 20 20 20 20  Suffix;         
40d8a 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
40d8b 20 6f 66 20 74 65 72 6d 20 73 75 66 66 69 78 20   of term suffix 
40d8c 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 72 65 66  */.    int nPref
40d8d 69 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ix = 0;         
40d8e 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
40d8f 74 65 72 6d 20 70 72 65 66 69 78 20 2a 2f 0a 20  term prefix */. 
40d90 20 20 20 69 6e 74 20 6e 42 75 66 66 65 72 3b 20     int nBuffer; 
40d91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40d92 20 2f 2a 20 54 6f 74 61 6c 20 74 65 72 6d 20 73   /* Total term s
40d93 69 7a 65 20 2a 2f 0a 20 20 0a 20 20 20 20 2f 2a  ize */.  .    /*
40d94 20 4c 6f 61 64 20 74 68 65 20 6e 65 78 74 20 74   Load the next t
40d95 65 72 6d 20 6f 6e 20 74 68 65 20 6e 6f 64 65 20  erm on the node 
40d96 69 6e 74 6f 20 7a 42 75 66 66 65 72 2e 20 55 73  into zBuffer. Us
40d97 65 20 72 65 61 6c 6c 6f 63 28 29 20 74 6f 20 65  e realloc() to e
40d98 78 70 61 6e 64 0a 20 20 20 20 2a 2a 20 74 68 65  xpand.    ** the
40d99 20 73 69 7a 65 20 6f 66 20 7a 42 75 66 66 65 72   size of zBuffer
40d9a 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 20 2a   if required.  *
40d9b 2f 0a 20 20 20 20 69 66 28 20 21 69 73 46 69 72  /.    if( !isFir
40d9c 73 74 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20  stTerm ){.      
40d9d 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 46  zCsr += sqlite3F
40d9e 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 7a  ts3GetVarint32(z
40d9f 43 73 72 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a  Csr, &nPrefix);.
40da0 20 20 20 20 7d 0a 20 20 20 20 69 73 46 69 72 73      }.    isFirs
40da1 74 54 65 72 6d 20 3d 20 30 3b 0a 20 20 20 20 7a  tTerm = 0;.    z
40da2 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 46 74  Csr += sqlite3Ft
40da3 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 7a 43  s3GetVarint32(zC
40da4 73 72 2c 20 26 6e 53 75 66 66 69 78 29 3b 0a 20  sr, &nSuffix);. 
40da5 20 20 20 0a 20 20 20 20 69 66 28 20 6e 50 72 65     .    if( nPre
40da6 66 69 78 3c 30 20 7c 7c 20 6e 53 75 66 66 69 78  fix<0 || nSuffix
40da7 3c 30 20 7c 7c 20 26 7a 43 73 72 5b 6e 53 75 66  <0 || &zCsr[nSuf
40da8 66 69 78 5d 3e 7a 45 6e 64 20 29 7b 0a 20 20 20  fix]>zEnd ){.   
40da9 20 20 20 72 63 20 3d 20 46 54 53 5f 43 4f 52 52     rc = FTS_CORR
40daa 55 50 54 5f 56 54 41 42 3b 0a 20 20 20 20 20 20  UPT_VTAB;.      
40dab 67 6f 74 6f 20 66 69 6e 69 73 68 5f 73 63 61 6e  goto finish_scan
40dac 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
40dad 6e 50 72 65 66 69 78 2b 6e 53 75 66 66 69 78 3e  nPrefix+nSuffix>
40dae 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20  nAlloc ){.      
40daf 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20  char *zNew;.    
40db0 20 20 6e 41 6c 6c 6f 63 20 3d 20 28 6e 50 72 65    nAlloc = (nPre
40db1 66 69 78 2b 6e 53 75 66 66 69 78 29 20 2a 20 32  fix+nSuffix) * 2
40db2 3b 0a 20 20 20 20 20 20 7a 4e 65 77 20 3d 20 28  ;.      zNew = (
40db3 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 72  char *)sqlite3_r
40db4 65 61 6c 6c 6f 63 28 7a 42 75 66 66 65 72 2c 20  ealloc(zBuffer, 
40db5 6e 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 69  nAlloc);.      i
40db6 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20 20 20 20  f( !zNew ){.    
40db7 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
40db8 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67  NOMEM;.        g
40db9 6f 74 6f 20 66 69 6e 69 73 68 5f 73 63 61 6e 3b  oto finish_scan;
40dba 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a  .      }.      z
40dbb 42 75 66 66 65 72 20 3d 20 7a 4e 65 77 3b 0a 20  Buffer = zNew;. 
40dbc 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
40dbd 20 7a 42 75 66 66 65 72 20 29 3b 0a 20 20 20 20   zBuffer );.    
40dbe 6d 65 6d 63 70 79 28 26 7a 42 75 66 66 65 72 5b  memcpy(&zBuffer[
40dbf 6e 50 72 65 66 69 78 5d 2c 20 7a 43 73 72 2c 20  nPrefix], zCsr, 
40dc0 6e 53 75 66 66 69 78 29 3b 0a 20 20 20 20 6e 42  nSuffix);.    nB
40dc1 75 66 66 65 72 20 3d 20 6e 50 72 65 66 69 78 20  uffer = nPrefix 
40dc2 2b 20 6e 53 75 66 66 69 78 3b 0a 20 20 20 20 7a  + nSuffix;.    z
40dc3 43 73 72 20 2b 3d 20 6e 53 75 66 66 69 78 3b 0a  Csr += nSuffix;.
40dc4 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 61 72 65 20  .    /* Compare 
40dc5 74 68 65 20 74 65 72 6d 20 77 65 20 61 72 65 20  the term we are 
40dc6 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 77 69  searching for wi
40dc7 74 68 20 74 68 65 20 74 65 72 6d 20 6a 75 73 74  th the term just
40dc8 20 6c 6f 61 64 65 64 20 66 72 6f 6d 0a 20 20 20   loaded from.   
40dc9 20 2a 2a 20 74 68 65 20 69 6e 74 65 72 69 6f 72   ** the interior
40dca 20 6e 6f 64 65 2e 20 49 66 20 74 68 65 20 73 70   node. If the sp
40dcb 65 63 69 66 69 65 64 20 74 65 72 6d 20 69 73 20  ecified term is 
40dcc 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
40dcd 65 71 75 61 6c 0a 20 20 20 20 2a 2a 20 74 6f 20  equal.    ** to 
40dce 74 68 65 20 74 65 72 6d 20 66 72 6f 6d 20 74 68  the term from th
40dcf 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2c  e interior node,
40dd0 20 74 68 65 6e 20 61 6c 6c 20 74 65 72 6d 73 20   then all terms 
40dd1 6f 6e 20 74 68 65 20 73 75 62 2d 74 72 65 65 20  on the sub-tree 
40dd2 0a 20 20 20 20 2a 2a 20 68 65 61 64 65 64 20 62  .    ** headed b
40dd3 79 20 6e 6f 64 65 20 69 43 68 69 6c 64 20 61 72  y node iChild ar
40dd4 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 7a  e smaller than z
40dd5 54 65 72 6d 2e 20 4e 6f 20 6e 65 65 64 20 74 6f  Term. No need to
40dd6 20 73 65 61 72 63 68 20 0a 20 20 20 20 2a 2a 20   search .    ** 
40dd7 69 43 68 69 6c 64 2e 0a 20 20 20 20 2a 2a 0a 20  iChild..    **. 
40dd8 20 20 20 2a 2a 20 49 66 20 74 68 65 20 69 6e 74     ** If the int
40dd9 65 72 69 6f 72 20 6e 6f 64 65 20 74 65 72 6d 20  erior node term 
40dda 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  is larger than t
40ddb 68 65 20 73 70 65 63 69 66 69 65 64 20 74 65 72  he specified ter
40ddc 6d 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 74  m, then.    ** t
40ddd 68 65 20 74 72 65 65 20 68 65 61 64 65 64 20 62  he tree headed b
40dde 79 20 69 43 68 69 6c 64 20 6d 61 79 20 63 6f 6e  y iChild may con
40ddf 74 61 69 6e 20 74 68 65 20 73 70 65 63 69 66 69  tain the specifi
40de0 65 64 20 74 65 72 6d 2e 0a 20 20 20 20 2a 2f 0a  ed term..    */.
40de1 20 20 20 20 63 6d 70 20 3d 20 6d 65 6d 63 6d 70      cmp = memcmp
40de2 28 7a 54 65 72 6d 2c 20 7a 42 75 66 66 65 72 2c  (zTerm, zBuffer,
40de3 20 28 6e 42 75 66 66 65 72 3e 6e 54 65 72 6d 20   (nBuffer>nTerm 
40de4 3f 20 6e 54 65 72 6d 20 3a 20 6e 42 75 66 66 65  ? nTerm : nBuffe
40de5 72 29 29 3b 0a 20 20 20 20 69 66 28 20 70 69 46  r));.    if( piF
40de6 69 72 73 74 20 26 26 20 28 63 6d 70 3c 30 20 7c  irst && (cmp<0 |
40de7 7c 20 28 63 6d 70 3d 3d 30 20 26 26 20 6e 42 75  | (cmp==0 && nBu
40de8 66 66 65 72 3e 6e 54 65 72 6d 29 29 20 29 7b 0a  ffer>nTerm)) ){.
40de9 20 20 20 20 20 20 2a 70 69 46 69 72 73 74 20 3d        *piFirst =
40dea 20 69 43 68 69 6c 64 3b 0a 20 20 20 20 20 20 70   iChild;.      p
40deb 69 46 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20  iFirst = 0;.    
40dec 7d 0a 0a 20 20 20 20 69 66 28 20 70 69 4c 61 73  }..    if( piLas
40ded 74 20 26 26 20 63 6d 70 3c 30 20 29 7b 0a 20 20  t && cmp<0 ){.  
40dee 20 20 20 20 2a 70 69 4c 61 73 74 20 3d 20 69 43      *piLast = iC
40def 68 69 6c 64 3b 0a 20 20 20 20 20 20 70 69 4c 61  hild;.      piLa
40df0 73 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20  st = 0;.    }.. 
40df1 20 20 20 69 43 68 69 6c 64 2b 2b 3b 0a 20 20 7d     iChild++;.  }
40df2 3b 0a 0a 20 20 69 66 28 20 70 69 46 69 72 73 74  ;..  if( piFirst
40df3 20 29 20 2a 70 69 46 69 72 73 74 20 3d 20 69 43   ) *piFirst = iC
40df4 68 69 6c 64 3b 0a 20 20 69 66 28 20 70 69 4c 61  hild;.  if( piLa
40df5 73 74 20 29 20 2a 70 69 4c 61 73 74 20 3d 20 69  st ) *piLast = i
40df6 43 68 69 6c 64 3b 0a 0a 20 66 69 6e 69 73 68 5f  Child;.. finish_
40df7 73 63 61 6e 3a 0a 20 20 73 71 6c 69 74 65 33 5f  scan:.  sqlite3_
40df8 66 72 65 65 28 7a 42 75 66 66 65 72 29 3b 0a 20  free(zBuffer);. 
40df9 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
40dfa 2f 2a 0a 2a 2a 20 54 68 65 20 62 75 66 66 65 72  /*.** The buffer
40dfb 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61   pointed to by a
40dfc 72 67 75 6d 65 6e 74 20 7a 4e 6f 64 65 20 28 73  rgument zNode (s
40dfd 69 7a 65 20 6e 4e 6f 64 65 20 62 79 74 65 73 29  ize nNode bytes)
40dfe 20 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 2a 2a 20   contains an.** 
40dff 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 6f 66  interior node of
40e00 20 61 20 62 2d 74 72 65 65 20 73 65 67 6d 65 6e   a b-tree segmen
40e01 74 2e 20 54 68 65 20 7a 54 65 72 6d 20 62 75 66  t. The zTerm buf
40e02 66 65 72 20 28 73 69 7a 65 20 6e 54 65 72 6d 20  fer (size nTerm 
40e03 62 79 74 65 73 29 0a 2a 2a 20 63 6f 6e 74 61 69  bytes).** contai
40e04 6e 73 20 61 20 74 65 72 6d 2e 20 54 68 69 73 20  ns a term. This 
40e05 66 75 6e 63 74 69 6f 6e 20 73 65 61 72 63 68 65  function searche
40e06 73 20 74 68 65 20 73 75 62 2d 74 72 65 65 20 68  s the sub-tree h
40e07 65 61 64 65 64 20 62 79 20 74 68 65 20 7a 4e 6f  eaded by the zNo
40e08 64 65 0a 2a 2a 20 6e 6f 64 65 20 66 6f 72 20 74  de.** node for t
40e09 68 65 20 72 61 6e 67 65 20 6f 66 20 6c 65 61 66  he range of leaf
40e0a 20 6e 6f 64 65 73 20 74 68 61 74 20 6d 61 79 20   nodes that may 
40e0b 63 6f 6e 74 61 69 6e 20 74 68 65 20 73 70 65 63  contain the spec
40e0c 69 66 69 65 64 20 74 65 72 6d 0a 2a 2a 20 6f 72  ified term.** or
40e0d 20 74 65 72 6d 73 20 66 6f 72 20 77 68 69 63 68   terms for which
40e0e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 74   the specified t
40e0f 65 72 6d 20 69 73 20 61 20 70 72 65 66 69 78 2e  erm is a prefix.
40e10 0a 2a 2a 0a 2a 2a 20 49 66 20 70 69 4c 65 61 66  .**.** If piLeaf
40e11 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
40e12 65 6e 20 2a 70 69 4c 65 61 66 20 69 73 20 73 65  en *piLeaf is se
40e13 74 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 69 64  t to the blockid
40e14 20 6f 66 20 74 68 65 20 0a 2a 2a 20 6c 65 66 74   of the .** left
40e15 2d 6d 6f 73 74 20 6c 65 61 66 20 6e 6f 64 65 20  -most leaf node 
40e16 69 6e 20 74 68 65 20 74 72 65 65 20 74 68 61 74  in the tree that
40e17 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 74 68 65   may contain the
40e18 20 73 70 65 63 69 66 69 65 64 20 74 65 72 6d 2e   specified term.
40e19 0a 2a 2a 20 49 66 20 70 69 4c 65 61 66 32 20 69  .** If piLeaf2 i
40e1a 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
40e1b 20 2a 70 69 4c 65 61 66 32 20 69 73 20 73 65 74   *piLeaf2 is set
40e1c 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 69 64 20   to the blockid 
40e1d 6f 66 20 74 68 65 0a 2a 2a 20 72 69 67 68 74 2d  of the.** right-
40e1e 6d 6f 73 74 20 6c 65 61 66 20 6e 6f 64 65 20 74  most leaf node t
40e1f 68 61 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20  hat may contain 
40e20 61 20 74 65 72 6d 20 66 6f 72 20 77 68 69 63 68  a term for which
40e21 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
40e22 2a 20 74 65 72 6d 20 69 73 20 61 20 70 72 65 66  * term is a pref
40e23 69 78 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  ix..**.** It is 
40e24 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 74 68  possible that th
40e25 65 20 72 61 6e 67 65 20 6f 66 20 72 65 74 75 72  e range of retur
40e26 6e 65 64 20 6c 65 61 66 20 6e 6f 64 65 73 20 64  ned leaf nodes d
40e27 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20  oes not contain 
40e28 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65  .** the specifie
40e29 64 20 74 65 72 6d 20 6f 72 20 61 6e 79 20 74 65  d term or any te
40e2a 72 6d 73 20 66 6f 72 20 77 68 69 63 68 20 69 74  rms for which it
40e2b 20 69 73 20 61 20 70 72 65 66 69 78 2e 20 48 6f   is a prefix. Ho
40e2c 77 65 76 65 72 2c 20 69 66 20 74 68 65 20 0a 2a  wever, if the .*
40e2d 2a 20 73 65 67 6d 65 6e 74 20 64 6f 65 73 20 63  * segment does c
40e2e 6f 6e 74 61 69 6e 20 61 6e 79 20 73 75 63 68 20  ontain any such 
40e2f 74 65 72 6d 73 2c 20 74 68 65 79 20 61 72 65 20  terms, they are 
40e30 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20 74 68  stored within th
40e31 65 20 69 64 65 6e 74 69 66 69 65 64 0a 2a 2a 20  e identified.** 
40e32 72 61 6e 67 65 2e 20 42 65 63 61 75 73 65 20 74  range. Because t
40e33 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c  his function onl
40e34 79 20 69 6e 73 70 65 63 74 73 20 69 6e 74 65 72  y inspects inter
40e35 69 6f 72 20 73 65 67 6d 65 6e 74 20 6e 6f 64 65  ior segment node
40e36 73 20 28 61 6e 64 0a 2a 2a 20 6e 65 76 65 72 20  s (and.** never 
40e37 6c 6f 61 64 73 20 6c 65 61 66 20 6e 6f 64 65 73  loads leaf nodes
40e38 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 29 2c 20 69   into memory), i
40e39 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
40e3a 65 20 74 6f 20 62 65 20 73 75 72 65 2e 0a 2a 2a  e to be sure..**
40e3b 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
40e3c 6f 63 63 75 72 73 2c 20 61 6e 20 65 72 72 6f 72  occurs, an error
40e3d 20 63 6f 64 65 20 6f 74 68 65 72 20 74 68 61 6e   code other than
40e3e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
40e3f 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 73 74 61 74  turned..*/ .stat
40e40 69 63 20 69 6e 74 20 66 74 73 33 53 65 6c 65 63  ic int fts3Selec
40e41 74 4c 65 61 66 28 0a 20 20 46 74 73 33 54 61 62  tLeaf(.  Fts3Tab
40e42 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  le *p,          
40e43 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
40e44 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
40e45 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
40e46 20 2a 7a 54 65 72 6d 2c 20 20 20 20 20 20 20 20   *zTerm,        
40e47 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f        /* Term to
40e48 20 73 65 6c 65 63 74 20 6c 65 61 76 65 73 20 66   select leaves f
40e49 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72  or */.  int nTer
40e4a 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m,              
40e4b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
40e4c 6f 66 20 74 65 72 6d 20 7a 54 65 72 6d 20 69 6e  of term zTerm in
40e4d 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 6f 6e 73   bytes */.  cons
40e4e 74 20 63 68 61 72 20 2a 7a 4e 6f 64 65 2c 20 20  t char *zNode,  
40e4f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
40e50 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  uffer containing
40e51 20 73 65 67 6d 65 6e 74 20 69 6e 74 65 72 69 6f   segment interio
40e52 72 20 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  r node */.  int 
40e53 6e 4e 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20  nNode,          
40e54 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
40e55 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20 61 74  ize of buffer at
40e56 20 7a 4e 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69   zNode */.  sqli
40e57 74 65 33 5f 69 6e 74 36 34 20 2a 70 69 4c 65 61  te3_int64 *piLea
40e58 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  f,          /* S
40e59 65 6c 65 63 74 65 64 20 6c 65 61 66 20 6e 6f 64  elected leaf nod
40e5a 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  e */.  sqlite3_i
40e5b 6e 74 36 34 20 2a 70 69 4c 65 61 66 32 20 20 20  nt64 *piLeaf2   
40e5c 20 20 20 20 20 20 20 2f 2a 20 53 65 6c 65 63 74         /* Select
40e5d 65 64 20 6c 65 61 66 20 6e 6f 64 65 20 2a 2f 0a  ed leaf node */.
40e5e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
40e5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40e60 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
40e61 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 48 65  ode */.  int iHe
40e62 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20  ight;           
40e63 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67           /* Heig
40e64 68 74 20 6f 66 20 74 68 69 73 20 6e 6f 64 65 20  ht of this node 
40e65 69 6e 20 74 72 65 65 20 2a 2f 0a 0a 20 20 61 73  in tree */..  as
40e66 73 65 72 74 28 20 70 69 4c 65 61 66 20 7c 7c 20  sert( piLeaf || 
40e67 70 69 4c 65 61 66 32 20 29 3b 0a 0a 20 20 73 71  piLeaf2 );..  sq
40e68 6c 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69  lite3Fts3GetVari
40e69 6e 74 33 32 28 7a 4e 6f 64 65 2c 20 26 69 48 65  nt32(zNode, &iHe
40e6a 69 67 68 74 29 3b 0a 20 20 72 63 20 3d 20 66 74  ight);.  rc = ft
40e6b 73 33 53 63 61 6e 49 6e 74 65 72 69 6f 72 4e 6f  s3ScanInteriorNo
40e6c 64 65 28 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 2c  de(zTerm, nTerm,
40e6d 20 7a 4e 6f 64 65 2c 20 6e 4e 6f 64 65 2c 20 70   zNode, nNode, p
40e6e 69 4c 65 61 66 2c 20 70 69 4c 65 61 66 32 29 3b  iLeaf, piLeaf2);
40e6f 0a 20 20 61 73 73 65 72 74 28 20 21 70 69 4c 65  .  assert( !piLe
40e70 61 66 32 20 7c 7c 20 21 70 69 4c 65 61 66 20 7c  af2 || !piLeaf |
40e71 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  | rc!=SQLITE_OK 
40e72 7c 7c 20 28 2a 70 69 4c 65 61 66 3c 3d 2a 70 69  || (*piLeaf<=*pi
40e73 4c 65 61 66 32 29 20 29 3b 0a 0a 20 20 69 66 28  Leaf2) );..  if(
40e74 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
40e75 26 20 69 48 65 69 67 68 74 3e 31 20 29 7b 0a 20  & iHeight>1 ){. 
40e76 20 20 20 63 68 61 72 20 2a 7a 42 6c 6f 62 20 3d     char *zBlob =
40e77 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
40e78 20 2f 2a 20 42 6c 6f 62 20 72 65 61 64 20 66 72   /* Blob read fr
40e79 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61  om %_segments ta
40e7a 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ble */.    int n
40e7b 42 6c 6f 62 3b 20 20 20 20 20 20 20 20 20 20 20  Blob;           
40e7c 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
40e7d 20 6f 66 20 7a 42 6c 6f 62 20 69 6e 20 62 79 74   of zBlob in byt
40e7e 65 73 20 2a 2f 0a 0a 20 20 20 20 69 66 28 20 70  es */..    if( p
40e7f 69 4c 65 61 66 20 26 26 20 70 69 4c 65 61 66 32  iLeaf && piLeaf2
40e80 20 26 26 20 28 2a 70 69 4c 65 61 66 21 3d 2a 70   && (*piLeaf!=*p
40e81 69 4c 65 61 66 32 29 20 29 7b 0a 20 20 20 20 20  iLeaf2) ){.     
40e82 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
40e83 33 52 65 61 64 42 6c 6f 63 6b 28 70 2c 20 2a 70  3ReadBlock(p, *p
40e84 69 4c 65 61 66 2c 20 26 7a 42 6c 6f 62 2c 20 26  iLeaf, &zBlob, &
40e85 6e 42 6c 6f 62 2c 20 30 29 3b 0a 20 20 20 20 20  nBlob, 0);.     
40e86 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
40e87 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
40e88 20 3d 20 66 74 73 33 53 65 6c 65 63 74 4c 65 61   = fts3SelectLea
40e89 66 28 70 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72  f(p, zTerm, nTer
40e8a 6d 2c 20 7a 42 6c 6f 62 2c 20 6e 42 6c 6f 62 2c  m, zBlob, nBlob,
40e8b 20 70 69 4c 65 61 66 2c 20 30 29 3b 0a 20 20 20   piLeaf, 0);.   
40e8c 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
40e8d 65 33 5f 66 72 65 65 28 7a 42 6c 6f 62 29 3b 0a  e3_free(zBlob);.
40e8e 20 20 20 20 20 20 70 69 4c 65 61 66 20 3d 20 30        piLeaf = 0
40e8f 3b 0a 20 20 20 20 20 20 7a 42 6c 6f 62 20 3d 20  ;.      zBlob = 
40e90 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  0;.    }..    if
40e91 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
40e92 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
40e93 6c 69 74 65 33 46 74 73 33 52 65 61 64 42 6c 6f  lite3Fts3ReadBlo
40e94 63 6b 28 70 2c 20 70 69 4c 65 61 66 3f 2a 70 69  ck(p, piLeaf?*pi
40e95 4c 65 61 66 3a 2a 70 69 4c 65 61 66 32 2c 20 26  Leaf:*piLeaf2, &
40e96 7a 42 6c 6f 62 2c 20 26 6e 42 6c 6f 62 2c 20 30  zBlob, &nBlob, 0
40e97 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
40e98 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
40e99 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73  {.      rc = fts
40e9a 33 53 65 6c 65 63 74 4c 65 61 66 28 70 2c 20 7a  3SelectLeaf(p, z
40e9b 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 7a 42 6c  Term, nTerm, zBl
40e9c 6f 62 2c 20 6e 42 6c 6f 62 2c 20 70 69 4c 65 61  ob, nBlob, piLea
40e9d 66 2c 20 70 69 4c 65 61 66 32 29 3b 0a 20 20 20  f, piLeaf2);.   
40e9e 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
40e9f 72 65 65 28 7a 42 6c 6f 62 29 3b 0a 20 20 7d 0a  ree(zBlob);.  }.
40ea0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
40ea1 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
40ea2 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
40ea3 63 72 65 61 74 65 20 64 65 6c 74 61 2d 65 6e 63  create delta-enc
40ea4 6f 64 65 64 20 73 65 72 69 61 6c 69 7a 65 64 20  oded serialized 
40ea5 6c 69 73 74 73 20 6f 66 20 46 54 53 33 20 0a 2a  lists of FTS3 .*
40ea6 2a 20 76 61 72 69 6e 74 73 2e 20 45 61 63 68 20  * varints. Each 
40ea7 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 75 6e  call to this fun
40ea8 63 74 69 6f 6e 20 61 70 70 65 6e 64 73 20 61 20  ction appends a 
40ea9 73 69 6e 67 6c 65 20 76 61 72 69 6e 74 20 74 6f  single varint to
40eaa 20 61 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74   a list..*/.stat
40eab 69 63 20 76 6f 69 64 20 66 74 73 33 50 75 74 44  ic void fts3PutD
40eac 65 6c 74 61 56 61 72 69 6e 74 28 0a 20 20 63 68  eltaVarint(.  ch
40ead 61 72 20 2a 2a 70 70 2c 20 20 20 20 20 20 20 20  ar **pp,        
40eae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
40eaf 20 49 4e 2f 4f 55 54 3a 20 4f 75 74 70 75 74 20   IN/OUT: Output 
40eb0 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 73 71 6c  pointer */.  sql
40eb1 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 69 50 72  ite3_int64 *piPr
40eb2 65 76 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ev,          /* 
40eb3 49 4e 2f 4f 55 54 3a 20 50 72 65 76 69 6f 75 73  IN/OUT: Previous
40eb4 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 74   value written t
40eb5 6f 20 6c 69 73 74 20 2a 2f 0a 20 20 73 71 6c 69  o list */.  sqli
40eb6 74 65 33 5f 69 6e 74 36 34 20 69 56 61 6c 20 20  te3_int64 iVal  
40eb7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
40eb8 72 69 74 65 20 74 68 69 73 20 76 61 6c 75 65 20  rite this value 
40eb9 74 6f 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 29  to the list */.)
40eba 7b 0a 20 20 61 73 73 65 72 74 28 20 69 56 61 6c  {.  assert( iVal
40ebb 2d 2a 70 69 50 72 65 76 20 3e 20 30 20 7c 7c 20  -*piPrev > 0 || 
40ebc 28 2a 70 69 50 72 65 76 3d 3d 30 20 26 26 20 69  (*piPrev==0 && i
40ebd 56 61 6c 3d 3d 30 29 20 29 3b 0a 20 20 2a 70 70  Val==0) );.  *pp
40ebe 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50   += sqlite3Fts3P
40ebf 75 74 56 61 72 69 6e 74 28 2a 70 70 2c 20 69 56  utVarint(*pp, iV
40ec0 61 6c 2d 2a 70 69 50 72 65 76 29 3b 0a 20 20 2a  al-*piPrev);.  *
40ec1 70 69 50 72 65 76 20 3d 20 69 56 61 6c 3b 0a 7d  piPrev = iVal;.}
40ec2 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ../*.** When thi
40ec3 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
40ec4 6c 6c 65 64 2c 20 2a 70 70 50 6f 73 6c 69 73 74  lled, *ppPoslist
40ec5 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 70   is assumed to p
40ec6 6f 69 6e 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20  oint to the .** 
40ec7 73 74 61 72 74 20 6f 66 20 61 20 70 6f 73 69 74  start of a posit
40ec8 69 6f 6e 2d 6c 69 73 74 2e 20 41 66 74 65 72 20  ion-list. After 
40ec9 69 74 20 72 65 74 75 72 6e 73 2c 20 2a 70 70 50  it returns, *ppP
40eca 6f 73 6c 69 73 74 20 70 6f 69 6e 74 73 20 74 6f  oslist points to
40ecb 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 62 79   the.** first by
40ecc 74 65 20 61 66 74 65 72 20 74 68 65 20 70 6f 73  te after the pos
40ecd 69 74 69 6f 6e 2d 6c 69 73 74 2e 0a 2a 2a 0a 2a  ition-list..**.*
40ece 2a 20 41 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73  * A position lis
40ecf 74 20 69 73 20 6c 69 73 74 20 6f 66 20 70 6f 73  t is list of pos
40ed0 69 74 69 6f 6e 73 20 28 64 65 6c 74 61 20 65 6e  itions (delta en
40ed1 63 6f 64 65 64 29 20 61 6e 64 20 63 6f 6c 75 6d  coded) and colum
40ed2 6e 73 20 66 6f 72 20 0a 2a 2a 20 61 20 73 69 6e  ns for .** a sin
40ed3 67 6c 65 20 64 6f 63 75 6d 65 6e 74 20 72 65 63  gle document rec
40ed4 6f 72 64 20 6f 66 20 61 20 64 6f 63 6c 69 73 74  ord of a doclist
40ed5 2e 20 20 53 6f 2c 20 69 6e 20 6f 74 68 65 72 20  .  So, in other 
40ed6 77 6f 72 64 73 2c 20 74 68 69 73 0a 2a 2a 20 72  words, this.** r
40ed7 6f 75 74 69 6e 65 20 61 64 76 61 6e 63 65 73 20  outine advances 
40ed8 2a 70 70 50 6f 73 6c 69 73 74 20 73 6f 20 74 68  *ppPoslist so th
40ed9 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  at it points to 
40eda 74 68 65 20 6e 65 78 74 20 64 6f 63 69 64 20 69  the next docid i
40edb 6e 0a 2a 2a 20 74 68 65 20 64 6f 63 6c 69 73 74  n.** the doclist
40edc 2c 20 6f 72 20 74 6f 20 74 68 65 20 66 69 72 73  , or to the firs
40edd 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20  t byte past the 
40ede 65 6e 64 20 6f 66 20 74 68 65 20 64 6f 63 6c 69  end of the docli
40edf 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 70 20  st..**.** If pp 
40ee0 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  is not NULL, the
40ee1 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  n the contents o
40ee2 66 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 6c  f the position l
40ee3 69 73 74 20 61 72 65 20 63 6f 70 69 65 64 0a 2a  ist are copied.*
40ee4 2a 20 74 6f 20 2a 70 70 2e 20 2a 70 70 20 69 73  * to *pp. *pp is
40ee5 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f   set to point to
40ee6 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20   the first byte 
40ee7 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 62 79  past the last by
40ee8 74 65 20 63 6f 70 69 65 64 0a 2a 2a 20 62 65 66  te copied.** bef
40ee9 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ore this functio
40eea 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a 73 74  n returns..*/.st
40eeb 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 50 6f  atic void fts3Po
40eec 73 6c 69 73 74 43 6f 70 79 28 63 68 61 72 20 2a  slistCopy(char *
40eed 2a 70 70 2c 20 63 68 61 72 20 2a 2a 70 70 50 6f  *pp, char **ppPo
40eee 73 6c 69 73 74 29 7b 0a 20 20 63 68 61 72 20 2a  slist){.  char *
40eef 70 45 6e 64 20 3d 20 2a 70 70 50 6f 73 6c 69 73  pEnd = *ppPoslis
40ef0 74 3b 0a 20 20 63 68 61 72 20 63 20 3d 20 30 3b  t;.  char c = 0;
40ef1 0a 0a 20 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f  ..  /* The end o
40ef2 66 20 61 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73  f a position lis
40ef3 74 20 69 73 20 6d 61 72 6b 65 64 20 62 79 20 61  t is marked by a
40ef4 20 7a 65 72 6f 20 65 6e 63 6f 64 65 64 20 61 73   zero encoded as
40ef5 20 61 6e 20 46 54 53 33 20 0a 20 20 2a 2a 20 76   an FTS3 .  ** v
40ef6 61 72 69 6e 74 2e 20 41 20 73 69 6e 67 6c 65 20  arint. A single 
40ef7 50 4f 53 5f 45 4e 44 20 28 30 29 20 62 79 74 65  POS_END (0) byte
40ef8 2e 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 65  . Except, if the
40ef9 20 30 20 62 79 74 65 20 69 73 20 70 72 65 63 65   0 byte is prece
40efa 64 65 64 20 62 79 0a 20 20 2a 2a 20 61 20 62 79  ded by.  ** a by
40efb 74 65 20 77 69 74 68 20 74 68 65 20 30 78 38 30  te with the 0x80
40efc 20 62 69 74 20 73 65 74 2c 20 74 68 65 6e 20 69   bit set, then i
40efd 74 20 69 73 20 6e 6f 74 20 61 20 76 61 72 69 6e  t is not a varin
40efe 74 20 30 2c 20 62 75 74 20 74 68 65 20 74 61 69  t 0, but the tai
40eff 6c 0a 20 20 2a 2a 20 6f 66 20 73 6f 6d 65 20 6f  l.  ** of some o
40f00 74 68 65 72 2c 20 6d 75 6c 74 69 2d 62 79 74 65  ther, multi-byte
40f01 2c 20 76 61 6c 75 65 2e 0a 20 20 2a 2a 0a 20 20  , value..  **.  
40f02 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
40f03 20 77 68 69 6c 65 2d 6c 6f 6f 70 20 6d 6f 76 65   while-loop move
40f04 73 20 70 45 6e 64 20 74 6f 20 70 6f 69 6e 74 20  s pEnd to point 
40f05 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 79 74  to the first byt
40f06 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 0a 20  e that is not . 
40f07 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20   ** immediately 
40f08 70 72 65 63 65 64 65 64 20 62 79 20 61 20 62 79  preceded by a by
40f09 74 65 20 77 69 74 68 20 74 68 65 20 30 78 38 30  te with the 0x80
40f0a 20 62 69 74 20 73 65 74 2e 20 54 68 65 6e 20 69   bit set. Then i
40f0b 6e 63 72 65 6d 65 6e 74 73 0a 20 20 2a 2a 20 70  ncrements.  ** p
40f0c 45 6e 64 20 6f 6e 63 65 20 6d 6f 72 65 20 73 6f  End once more so
40f0d 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20   that it points 
40f0e 74 6f 20 74 68 65 20 62 79 74 65 20 69 6d 6d 65  to the byte imme
40f0f 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e  diately followin
40f10 67 20 74 68 65 0a 20 20 2a 2a 20 6c 61 73 74 20  g the.  ** last 
40f11 62 79 74 65 20 69 6e 20 74 68 65 20 70 6f 73 69  byte in the posi
40f12 74 69 6f 6e 2d 6c 69 73 74 2e 0a 20 20 2a 2f 0a  tion-list..  */.
40f13 20 20 77 68 69 6c 65 28 20 2a 70 45 6e 64 20 7c    while( *pEnd |
40f14 20 63 20 29 7b 0a 20 20 20 20 63 20 3d 20 2a 70   c ){.    c = *p
40f15 45 6e 64 2b 2b 20 26 20 30 78 38 30 3b 0a 20 20  End++ & 0x80;.  
40f16 20 20 74 65 73 74 63 61 73 65 28 20 63 21 3d 30    testcase( c!=0
40f17 20 26 26 20 28 2a 70 45 6e 64 29 3d 3d 30 20 29   && (*pEnd)==0 )
40f18 3b 0a 20 20 7d 0a 20 20 70 45 6e 64 2b 2b 3b 20  ;.  }.  pEnd++; 
40f19 20 2f 2a 20 41 64 76 61 6e 63 65 20 70 61 73 74   /* Advance past
40f1a 20 74 68 65 20 50 4f 53 5f 45 4e 44 20 74 65 72   the POS_END ter
40f1b 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 2a 2f 0a  minator byte */.
40f1c 0a 20 20 69 66 28 20 70 70 20 29 7b 0a 20 20 20  .  if( pp ){.   
40f1d 20 69 6e 74 20 6e 20 3d 20 28 69 6e 74 29 28 70   int n = (int)(p
40f1e 45 6e 64 20 2d 20 2a 70 70 50 6f 73 6c 69 73 74  End - *ppPoslist
40f1f 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 20 3d  );.    char *p =
40f20 20 2a 70 70 3b 0a 20 20 20 20 6d 65 6d 63 70 79   *pp;.    memcpy
40f21 28 70 2c 20 2a 70 70 50 6f 73 6c 69 73 74 2c 20  (p, *ppPoslist, 
40f22 6e 29 3b 0a 20 20 20 20 70 20 2b 3d 20 6e 3b 0a  n);.    p += n;.
40f23 20 20 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 7d      *pp = p;.  }
40f24 0a 20 20 2a 70 70 50 6f 73 6c 69 73 74 20 3d 20  .  *ppPoslist = 
40f25 70 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  pEnd;.}../*.** W
40f26 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
40f27 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 2a 70 70  n is called, *pp
40f28 50 6f 73 6c 69 73 74 20 69 73 20 61 73 73 75 6d  Poslist is assum
40f29 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  ed to point to t
40f2a 68 65 20 0a 2a 2a 20 73 74 61 72 74 20 6f 66 20  he .** start of 
40f2b 61 20 63 6f 6c 75 6d 6e 2d 6c 69 73 74 2e 20 41  a column-list. A
40f2c 66 74 65 72 20 69 74 20 72 65 74 75 72 6e 73 2c  fter it returns,
40f2d 20 2a 70 70 50 6f 73 6c 69 73 74 20 70 6f 69 6e   *ppPoslist poin
40f2e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 74 6f 20  ts to the.** to 
40f2f 74 68 65 20 74 65 72 6d 69 6e 61 74 6f 72 20 28  the terminator (
40f30 50 4f 53 5f 43 4f 4c 55 4d 4e 20 6f 72 20 50 4f  POS_COLUMN or PO
40f31 53 5f 45 4e 44 29 20 62 79 74 65 20 6f 66 20 74  S_END) byte of t
40f32 68 65 20 63 6f 6c 75 6d 6e 2d 6c 69 73 74 2e 0a  he column-list..
40f33 2a 2a 0a 2a 2a 20 41 20 63 6f 6c 75 6d 6e 2d 6c  **.** A column-l
40f34 69 73 74 20 69 73 20 6c 69 73 74 20 6f 66 20 64  ist is list of d
40f35 65 6c 74 61 2d 65 6e 63 6f 64 65 64 20 70 6f 73  elta-encoded pos
40f36 69 74 69 6f 6e 73 20 66 6f 72 20 61 20 73 69 6e  itions for a sin
40f37 67 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 77 69  gle column.** wi
40f38 74 68 69 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f  thin a single do
40f39 63 75 6d 65 6e 74 20 77 69 74 68 69 6e 20 61 20  cument within a 
40f3a 64 6f 63 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54  doclist..**.** T
40f3b 68 65 20 63 6f 6c 75 6d 6e 2d 6c 69 73 74 20 69  he column-list i
40f3c 73 20 74 65 72 6d 69 6e 61 74 65 64 20 65 69 74  s terminated eit
40f3d 68 65 72 20 62 79 20 61 20 50 4f 53 5f 43 4f 4c  her by a POS_COL
40f3e 55 4d 4e 20 76 61 72 69 6e 74 20 28 31 29 20 6f  UMN varint (1) o
40f3f 72 0a 2a 2a 20 61 20 50 4f 53 5f 45 4e 44 20 76  r.** a POS_END v
40f40 61 72 69 6e 74 20 28 30 29 2e 20 20 54 68 69 73  arint (0).  This
40f41 20 72 6f 75 74 69 6e 65 20 6c 65 61 76 65 73 20   routine leaves 
40f42 2a 70 70 50 6f 73 6c 69 73 74 20 70 6f 69 6e 74  *ppPoslist point
40f43 69 6e 67 20 74 6f 0a 2a 2a 20 74 68 65 20 50 4f  ing to.** the PO
40f44 53 5f 43 4f 4c 55 4d 4e 20 6f 72 20 50 4f 53 5f  S_COLUMN or POS_
40f45 45 4e 44 20 74 68 61 74 20 74 65 72 6d 69 6e 61  END that termina
40f46 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 6c  tes the column-l
40f47 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 70  ist..**.** If pp
40f48 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
40f49 65 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  en the contents 
40f4a 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 6c 69  of the column-li
40f4b 73 74 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a  st are copied.**
40f4c 20 74 6f 20 2a 70 70 2e 20 2a 70 70 20 69 73 20   to *pp. *pp is 
40f4d 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  set to point to 
40f4e 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70  the first byte p
40f4f 61 73 74 20 74 68 65 20 6c 61 73 74 20 62 79 74  ast the last byt
40f50 65 20 63 6f 70 69 65 64 0a 2a 2a 20 62 65 66 6f  e copied.** befo
40f51 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  re this function
40f52 20 72 65 74 75 72 6e 73 2e 20 20 54 68 65 20 50   returns.  The P
40f53 4f 53 5f 43 4f 4c 55 4d 4e 20 6f 72 20 50 4f 53  OS_COLUMN or POS
40f54 5f 45 4e 44 20 74 65 72 6d 69 6e 61 74 6f 72 0a  _END terminator.
40f55 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 70 69 65 64  ** is not copied
40f56 20 69 6e 74 6f 20 2a 70 70 2e 0a 2a 2f 0a 73 74   into *pp..*/.st
40f57 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 43 6f  atic void fts3Co
40f58 6c 75 6d 6e 6c 69 73 74 43 6f 70 79 28 63 68 61  lumnlistCopy(cha
40f59 72 20 2a 2a 70 70 2c 20 63 68 61 72 20 2a 2a 70  r **pp, char **p
40f5a 70 50 6f 73 6c 69 73 74 29 7b 0a 20 20 63 68 61  pPoslist){.  cha
40f5b 72 20 2a 70 45 6e 64 20 3d 20 2a 70 70 50 6f 73  r *pEnd = *ppPos
40f5c 6c 69 73 74 3b 0a 20 20 63 68 61 72 20 63 20 3d  list;.  char c =
40f5d 20 30 3b 0a 0a 20 20 2f 2a 20 41 20 63 6f 6c 75   0;..  /* A colu
40f5e 6d 6e 2d 6c 69 73 74 20 69 73 20 74 65 72 6d 69  mn-list is termi
40f5f 6e 61 74 65 64 20 62 79 20 65 69 74 68 65 72 20  nated by either 
40f60 61 20 30 78 30 31 20 6f 72 20 30 78 30 30 20 62  a 0x01 or 0x00 b
40f61 79 74 65 20 74 68 61 74 20 69 73 0a 20 20 2a 2a  yte that is.  **
40f62 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 20 6d   not part of a m
40f63 75 6c 74 69 2d 62 79 74 65 20 76 61 72 69 6e 74  ulti-byte varint
40f64 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20  ..  */.  while( 
40f65 30 78 46 45 20 26 20 28 2a 70 45 6e 64 20 7c 20  0xFE & (*pEnd | 
40f66 63 29 20 29 7b 0a 20 20 20 20 63 20 3d 20 2a 70  c) ){.    c = *p
40f67 45 6e 64 2b 2b 20 26 20 30 78 38 30 3b 0a 20 20  End++ & 0x80;.  
40f68 20 20 74 65 73 74 63 61 73 65 28 20 63 21 3d 30    testcase( c!=0
40f69 20 26 26 20 28 28 2a 70 45 6e 64 29 26 30 78 66   && ((*pEnd)&0xf
40f6a 65 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69  e)==0 );.  }.  i
40f6b 66 28 20 70 70 20 29 7b 0a 20 20 20 20 69 6e 74  f( pp ){.    int
40f6c 20 6e 20 3d 20 28 69 6e 74 29 28 70 45 6e 64 20   n = (int)(pEnd 
40f6d 2d 20 2a 70 70 50 6f 73 6c 69 73 74 29 3b 0a 20  - *ppPoslist);. 
40f6e 20 20 20 63 68 61 72 20 2a 70 20 3d 20 2a 70 70     char *p = *pp
40f6f 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20  ;.    memcpy(p, 
40f70 2a 70 70 50 6f 73 6c 69 73 74 2c 20 6e 29 3b 0a  *ppPoslist, n);.
40f71 20 20 20 20 70 20 2b 3d 20 6e 3b 0a 20 20 20 20      p += n;.    
40f72 2a 70 70 20 3d 20 70 3b 0a 20 20 7d 0a 20 20 2a  *pp = p;.  }.  *
40f73 70 70 50 6f 73 6c 69 73 74 20 3d 20 70 45 6e 64  ppPoslist = pEnd
40f74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 75 65  ;.}../*.** Value
40f75 20 75 73 65 64 20 74 6f 20 73 69 67 6e 69 66 79   used to signify
40f76 20 74 68 65 20 65 6e 64 20 6f 66 20 61 6e 20 70   the end of an p
40f77 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 2e 20 54 68  osition-list. Th
40f78 69 73 20 69 73 20 73 61 66 65 20 62 65 63 61 75  is is safe becau
40f79 73 65 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20  se.** it is not 
40f7a 70 6f 73 73 69 62 6c 65 20 74 6f 20 68 61 76 65  possible to have
40f7b 20 61 20 64 6f 63 75 6d 65 6e 74 20 77 69 74 68   a document with
40f7c 20 32 5e 33 31 20 74 65 72 6d 73 2e 0a 2a 2f 0a   2^31 terms..*/.
40f7d 23 64 65 66 69 6e 65 20 50 4f 53 49 54 49 4f 4e  #define POSITION
40f7e 5f 4c 49 53 54 5f 45 4e 44 20 30 78 37 66 66 66  _LIST_END 0x7fff
40f7f 66 66 66 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ffff../*.** This
40f80 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
40f81 64 20 74 6f 20 68 65 6c 70 20 70 61 72 73 65 20  d to help parse 
40f82 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 73 2e 20  position-lists. 
40f83 57 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  When this functi
40f84 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2c  on is.** called,
40f85 20 2a 70 70 20 6d 61 79 20 70 6f 69 6e 74 20 74   *pp may point t
40f86 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  o the start of t
40f87 68 65 20 6e 65 78 74 20 76 61 72 69 6e 74 20 69  he next varint i
40f88 6e 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 2d 6c  n the position-l
40f89 69 73 74 0a 2a 2a 20 62 65 69 6e 67 20 70 61 72  ist.** being par
40f8a 73 65 64 2c 20 6f 72 20 69 74 20 6d 61 79 20 70  sed, or it may p
40f8b 6f 69 6e 74 20 74 6f 20 31 20 62 79 74 65 20 70  oint to 1 byte p
40f8c 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
40f8d 68 65 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74  he position-list
40f8e 0a 2a 2a 20 28 69 6e 20 77 68 69 63 68 20 63 61  .** (in which ca
40f8f 73 65 20 2a 2a 70 70 20 77 69 6c 6c 20 62 65 20  se **pp will be 
40f90 61 20 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74  a terminator byt
40f91 65 73 20 50 4f 53 5f 45 4e 44 20 28 30 29 20 6f  es POS_END (0) o
40f92 72 0a 2a 2a 20 28 31 29 29 2e 0a 2a 2a 0a 2a 2a  r.** (1))..**.**
40f93 20 49 66 20 2a 70 70 20 70 6f 69 6e 74 73 20 70   If *pp points p
40f94 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
40f95 68 65 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74  he current posit
40f96 69 6f 6e 2d 6c 69 73 74 2c 20 73 65 74 20 2a 70  ion-list, set *p
40f97 69 20 74 6f 20 0a 2a 2a 20 50 4f 53 49 54 49 4f  i to .** POSITIO
40f98 4e 5f 4c 49 53 54 5f 45 4e 44 20 61 6e 64 20 72  N_LIST_END and r
40f99 65 74 75 72 6e 2e 20 4f 74 68 65 72 77 69 73 65  eturn. Otherwise
40f9a 2c 20 72 65 61 64 20 74 68 65 20 6e 65 78 74 20  , read the next 
40f9b 76 61 72 69 6e 74 20 66 72 6f 6d 20 2a 70 70 2c  varint from *pp,
40f9c 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 20 74 68  .** increment th
40f9d 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
40f9e 6f 66 20 2a 70 69 20 62 79 20 74 68 65 20 76 61  of *pi by the va
40f9f 6c 75 65 20 72 65 61 64 2c 20 61 6e 64 20 73 65  lue read, and se
40fa0 74 20 2a 70 70 20 74 6f 0a 2a 2a 20 70 6f 69 6e  t *pp to.** poin
40fa1 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61  t to the next va
40fa2 6c 75 65 20 62 65 66 6f 72 65 20 72 65 74 75 72  lue before retur
40fa3 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f  ning..**.** Befo
40fa4 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20  re calling this 
40fa5 72 6f 75 74 69 6e 65 20 2a 70 69 20 6d 75 73 74  routine *pi must
40fa6 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   be initialized 
40fa7 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a  to the value of.
40fa8 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  ** the previous 
40fa9 70 6f 73 69 74 69 6f 6e 2c 20 6f 72 20 7a 65 72  position, or zer
40faa 6f 20 69 66 20 77 65 20 61 72 65 20 72 65 61 64  o if we are read
40fab 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 6f  ing the first po
40fac 73 69 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65  sition.** in the
40fad 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 2e 20   position-list. 
40fae 20 42 65 63 61 75 73 65 20 70 6f 73 69 74 69 6f   Because positio
40faf 6e 73 20 61 72 65 20 64 65 6c 74 61 2d 65 6e 63  ns are delta-enc
40fb0 6f 64 65 64 2c 20 74 68 65 20 76 61 6c 75 65 0a  oded, the value.
40fb1 2a 2a 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f  ** of the previo
40fb2 75 73 20 70 6f 73 69 74 69 6f 6e 20 69 73 20 6e  us position is n
40fb3 65 65 64 65 64 20 69 6e 20 6f 72 64 65 72 20 74  eeded in order t
40fb4 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 76 61  o compute the va
40fb5 6c 75 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65  lue of.** the ne
40fb6 78 74 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a  xt position..*/.
40fb7 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
40fb8 52 65 61 64 4e 65 78 74 50 6f 73 28 0a 20 20 63  ReadNextPos(.  c
40fb9 68 61 72 20 2a 2a 70 70 2c 20 20 20 20 20 20 20  har **pp,       
40fba 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
40fbb 49 4e 2f 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20  IN/OUT: Pointer 
40fbc 69 6e 74 6f 20 70 6f 73 69 74 69 6f 6e 2d 6c 69  into position-li
40fbd 73 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 73  st buffer */.  s
40fbe 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 69  qlite3_int64 *pi
40fbf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
40fc0 49 4e 2f 4f 55 54 3a 20 56 61 6c 75 65 20 72 65  IN/OUT: Value re
40fc1 61 64 20 66 72 6f 6d 20 70 6f 73 69 74 69 6f 6e  ad from position
40fc2 2d 6c 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 69 66  -list */.){.  if
40fc3 28 20 28 2a 2a 70 70 29 26 30 78 46 45 20 29 7b  ( (**pp)&0xFE ){
40fc4 0a 20 20 20 20 66 74 73 33 47 65 74 44 65 6c 74  .    fts3GetDelt
40fc5 61 56 61 72 69 6e 74 28 70 70 2c 20 70 69 29 3b  aVarint(pp, pi);
40fc6 0a 20 20 20 20 2a 70 69 20 2d 3d 20 32 3b 0a 20  .    *pi -= 2;. 
40fc7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 69 20   }else{.    *pi 
40fc8 3d 20 50 4f 53 49 54 49 4f 4e 5f 4c 49 53 54 5f  = POSITION_LIST_
40fc9 45 4e 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  END;.  }.}../*.*
40fca 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 69  * If parameter i
40fcb 43 6f 6c 20 69 73 20 6e 6f 74 20 30 2c 20 77 72  Col is not 0, wr
40fcc 69 74 65 20 61 6e 20 50 4f 53 5f 43 4f 4c 55 4d  ite an POS_COLUM
40fcd 4e 20 28 31 29 20 62 79 74 65 20 66 6f 6c 6c 6f  N (1) byte follo
40fce 77 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 76 61  wed by.** the va
40fcf 6c 75 65 20 6f 66 20 69 43 6f 6c 20 65 6e 63 6f  lue of iCol enco
40fd0 64 65 64 20 61 73 20 61 20 76 61 72 69 6e 74 20  ded as a varint 
40fd1 74 6f 20 2a 70 70 2e 20 20 20 54 68 69 73 20 77  to *pp.   This w
40fd2 69 6c 6c 20 73 74 61 72 74 20 61 20 6e 65 77 0a  ill start a new.
40fd3 2a 2a 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2e 0a  ** column list..
40fd4 2a 2a 0a 2a 2a 20 53 65 74 20 2a 70 70 20 74 6f  **.** Set *pp to
40fd5 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 62 79   point to the by
40fd6 74 65 20 6a 75 73 74 20 61 66 74 65 72 20 74 68  te just after th
40fd7 65 20 6c 61 73 74 20 62 79 74 65 20 77 72 69 74  e last byte writ
40fd8 74 65 6e 20 62 65 66 6f 72 65 20 0a 2a 2a 20 72  ten before .** r
40fd9 65 74 75 72 6e 69 6e 67 20 28 64 6f 20 6e 6f 74  eturning (do not
40fda 20 6d 6f 64 69 66 79 20 69 74 20 69 66 20 69 43   modify it if iC
40fdb 6f 6c 3d 3d 30 29 2e 20 52 65 74 75 72 6e 20 74  ol==0). Return t
40fdc 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
40fdd 6f 66 20 62 79 74 65 73 0a 2a 2a 20 77 72 69 74  of bytes.** writ
40fde 74 65 6e 20 28 30 20 69 66 20 69 43 6f 6c 3d 3d  ten (0 if iCol==
40fdf 30 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  0)..*/.static in
40fe0 74 20 66 74 73 33 50 75 74 43 6f 6c 4e 75 6d 62  t fts3PutColNumb
40fe1 65 72 28 63 68 61 72 20 2a 2a 70 70 2c 20 69 6e  er(char **pp, in
40fe2 74 20 69 43 6f 6c 29 7b 0a 20 20 69 6e 74 20 6e  t iCol){.  int n
40fe3 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
40fe4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
40fe5 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 72  mber of bytes wr
40fe6 69 74 74 65 6e 20 2a 2f 0a 20 20 69 66 28 20 69  itten */.  if( i
40fe7 43 6f 6c 20 29 7b 0a 20 20 20 20 63 68 61 72 20  Col ){.    char 
40fe8 2a 70 20 3d 20 2a 70 70 3b 20 20 20 20 20 20 20  *p = *pp;       
40fe9 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
40fea 75 74 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20  ut pointer */.  
40feb 20 20 6e 20 3d 20 31 20 2b 20 73 71 6c 69 74 65    n = 1 + sqlite
40fec 33 46 74 73 33 50 75 74 56 61 72 69 6e 74 28 26  3Fts3PutVarint(&
40fed 70 5b 31 5d 2c 20 69 43 6f 6c 29 3b 0a 20 20 20  p[1], iCol);.   
40fee 20 2a 70 20 3d 20 30 78 30 31 3b 0a 20 20 20 20   *p = 0x01;.    
40fef 2a 70 70 20 3d 20 26 70 5b 6e 5d 3b 0a 20 20 7d  *pp = &p[n];.  }
40ff0 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
40ff1 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
40ff2 65 20 75 6e 69 6f 6e 20 6f 66 20 74 77 6f 20 70  e union of two p
40ff3 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73 2e 20 20  osition lists.  
40ff4 54 68 65 20 6f 75 74 70 75 74 20 77 72 69 74 74  The output writt
40ff5 65 6e 0a 2a 2a 20 69 6e 74 6f 20 2a 70 70 20 63  en.** into *pp c
40ff6 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 70 6f 73 69  ontains all posi
40ff7 74 69 6f 6e 73 20 6f 66 20 62 6f 74 68 20 2a 70  tions of both *p
40ff8 70 31 20 61 6e 64 20 2a 70 70 32 20 69 6e 20 73  p1 and *pp2 in s
40ff9 6f 72 74 65 64 0a 2a 2a 20 6f 72 64 65 72 20 61  orted.** order a
40ffa 6e 64 20 77 69 74 68 20 61 6e 79 20 64 75 70 6c  nd with any dupl
40ffb 69 63 61 74 65 73 20 72 65 6d 6f 76 65 64 2e 20  icates removed. 
40ffc 20 41 6c 6c 20 70 6f 69 6e 74 65 72 73 20 61 72   All pointers ar
40ffd 65 0a 2a 2a 20 75 70 64 61 74 65 64 20 61 70 70  e.** updated app
40ffe 72 6f 70 72 69 61 74 65 6c 79 2e 20 20 20 54 68  ropriately.   Th
40fff 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70  e caller is resp
41000 6f 6e 73 69 62 6c 65 20 66 6f 72 20 69 6e 73 75  onsible for insu
41001 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74 68 65  ring.** that the
41002 72 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70 61  re is enough spa
41003 63 65 20 69 6e 20 2a 70 70 20 74 6f 20 68 6f 6c  ce in *pp to hol
41004 64 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 6f  d the complete o
41005 75 74 70 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  utput..*/.static
41006 20 76 6f 69 64 20 66 74 73 33 50 6f 73 6c 69 73   void fts3Poslis
41007 74 4d 65 72 67 65 28 0a 20 20 63 68 61 72 20 2a  tMerge(.  char *
41008 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20 20 20  *pp,            
41009 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74            /* Out
4100a 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20  put buffer */.  
4100b 63 68 61 72 20 2a 2a 70 70 31 2c 20 20 20 20 20  char **pp1,     
4100c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4100d 2f 2a 20 4c 65 66 74 20 69 6e 70 75 74 20 6c 69  /* Left input li
4100e 73 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70  st */.  char **p
4100f 70 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p2              
41010 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74          /* Right
41011 20 69 6e 70 75 74 20 6c 69 73 74 20 2a 2f 0a 29   input list */.)
41012 7b 0a 20 20 63 68 61 72 20 2a 70 20 3d 20 2a 70  {.  char *p = *p
41013 70 3b 0a 20 20 63 68 61 72 20 2a 70 31 20 3d 20  p;.  char *p1 = 
41014 2a 70 70 31 3b 0a 20 20 63 68 61 72 20 2a 70 32  *pp1;.  char *p2
41015 20 3d 20 2a 70 70 32 3b 0a 0a 20 20 77 68 69 6c   = *pp2;..  whil
41016 65 28 20 2a 70 31 20 7c 7c 20 2a 70 32 20 29 7b  e( *p1 || *p2 ){
41017 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 31 3b 20  .    int iCol1; 
41018 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
41019 75 72 72 65 6e 74 20 63 6f 6c 75 6d 6e 20 69 6e  urrent column in
4101a 64 65 78 20 69 6e 20 70 70 31 20 2a 2f 0a 20 20  dex in pp1 */.  
4101b 20 20 69 6e 74 20 69 43 6f 6c 32 3b 20 20 20 20    int iCol2;    
4101c 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 72       /* The curr
4101d 65 6e 74 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78  ent column index
4101e 20 69 6e 20 70 70 32 20 2a 2f 0a 0a 20 20 20 20   in pp2 */..    
4101f 69 66 28 20 2a 70 31 3d 3d 50 4f 53 5f 43 4f 4c  if( *p1==POS_COL
41020 55 4d 4e 20 29 20 73 71 6c 69 74 65 33 46 74 73  UMN ) sqlite3Fts
41021 33 47 65 74 56 61 72 69 6e 74 33 32 28 26 70 31  3GetVarint32(&p1
41022 5b 31 5d 2c 20 26 69 43 6f 6c 31 29 3b 0a 20 20  [1], &iCol1);.  
41023 20 20 65 6c 73 65 20 69 66 28 20 2a 70 31 3d 3d    else if( *p1==
41024 50 4f 53 5f 45 4e 44 20 29 20 69 43 6f 6c 31 20  POS_END ) iCol1 
41025 3d 20 50 4f 53 49 54 49 4f 4e 5f 4c 49 53 54 5f  = POSITION_LIST_
41026 45 4e 44 3b 0a 20 20 20 20 65 6c 73 65 20 69 43  END;.    else iC
41027 6f 6c 31 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66  ol1 = 0;..    if
41028 28 20 2a 70 32 3d 3d 50 4f 53 5f 43 4f 4c 55 4d  ( *p2==POS_COLUM
41029 4e 20 29 20 73 71 6c 69 74 65 33 46 74 73 33 47  N ) sqlite3Fts3G
4102a 65 74 56 61 72 69 6e 74 33 32 28 26 70 32 5b 31  etVarint32(&p2[1
4102b 5d 2c 20 26 69 43 6f 6c 32 29 3b 0a 20 20 20 20  ], &iCol2);.    
4102c 65 6c 73 65 20 69 66 28 20 2a 70 32 3d 3d 50 4f  else if( *p2==PO
4102d 53 5f 45 4e 44 20 29 20 69 43 6f 6c 32 20 3d 20  S_END ) iCol2 = 
4102e 50 4f 53 49 54 49 4f 4e 5f 4c 49 53 54 5f 45 4e  POSITION_LIST_EN
4102f 44 3b 0a 20 20 20 20 65 6c 73 65 20 69 43 6f 6c  D;.    else iCol
41030 32 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 28 20  2 = 0;..    if( 
41031 69 43 6f 6c 31 3d 3d 69 43 6f 6c 32 20 29 7b 0a  iCol1==iCol2 ){.
41032 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
41033 74 36 34 20 69 31 20 3d 20 30 3b 20 20 20 20 20  t64 i1 = 0;     
41034 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73 69 74 69    /* Last positi
41035 6f 6e 20 66 72 6f 6d 20 70 70 31 20 2a 2f 0a 20  on from pp1 */. 
41036 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
41037 36 34 20 69 32 20 3d 20 30 3b 20 20 20 20 20 20  64 i2 = 0;      
41038 20 2f 2a 20 4c 61 73 74 20 70 6f 73 69 74 69 6f   /* Last positio
41039 6e 20 66 72 6f 6d 20 70 70 32 20 2a 2f 0a 20 20  n from pp2 */.  
4103a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
4103b 34 20 69 50 72 65 76 20 3d 20 30 3b 0a 20 20 20  4 iPrev = 0;.   
4103c 20 20 20 69 6e 74 20 6e 20 3d 20 66 74 73 33 50     int n = fts3P
4103d 75 74 43 6f 6c 4e 75 6d 62 65 72 28 26 70 2c 20  utColNumber(&p, 
4103e 69 43 6f 6c 31 29 3b 0a 20 20 20 20 20 20 70 31  iCol1);.      p1
4103f 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 70 32 20   += n;.      p2 
41040 2b 3d 20 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  += n;..      /* 
41041 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 62  At this point, b
41042 6f 74 68 20 70 31 20 61 6e 64 20 70 32 20 70 6f  oth p1 and p2 po
41043 69 6e 74 20 74 6f 20 74 68 65 20 73 74 61 72 74  int to the start
41044 20 6f 66 20 63 6f 6c 75 6d 6e 2d 6c 69 73 74 73   of column-lists
41045 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68  .      ** for th
41046 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 28 74  e same column (t
41047 68 65 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 69  he column with i
41048 6e 64 65 78 20 69 43 6f 6c 31 20 61 6e 64 20 69  ndex iCol1 and i
41049 43 6f 6c 32 29 2e 0a 20 20 20 20 20 20 2a 2a 20  Col2)..      ** 
4104a 41 20 63 6f 6c 75 6d 6e 2d 6c 69 73 74 20 69 73  A column-list is
4104b 20 61 20 6c 69 73 74 20 6f 66 20 6e 6f 6e 2d 6e   a list of non-n
4104c 65 67 61 74 69 76 65 20 64 65 6c 74 61 2d 65 6e  egative delta-en
4104d 63 6f 64 65 64 20 76 61 72 69 6e 74 73 2c 20 65  coded varints, e
4104e 61 63 68 20 0a 20 20 20 20 20 20 2a 2a 20 69 6e  ach .      ** in
4104f 63 72 65 6d 65 6e 74 65 64 20 62 79 20 32 20 62  cremented by 2 b
41050 65 66 6f 72 65 20 62 65 69 6e 67 20 73 74 6f 72  efore being stor
41051 65 64 2e 20 45 61 63 68 20 6c 69 73 74 20 69 73  ed. Each list is
41052 20 74 65 72 6d 69 6e 61 74 65 64 20 62 79 20 61   terminated by a
41053 0a 20 20 20 20 20 20 2a 2a 20 50 4f 53 5f 45 4e  .      ** POS_EN
41054 44 20 28 30 29 20 6f 72 20 50 4f 53 5f 43 4f 4c  D (0) or POS_COL
41055 55 4d 4e 20 28 31 29 2e 20 54 68 65 20 66 6f 6c  UMN (1). The fol
41056 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6d 65 72  lowing block mer
41057 67 65 73 20 74 68 65 20 74 77 6f 20 6c 69 73 74  ges the two list
41058 73 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 77  s.      ** and w
41059 72 69 74 65 73 20 74 68 65 20 72 65 73 75 6c 74  rites the result
4105a 73 20 74 6f 20 62 75 66 66 65 72 20 70 2e 20 70  s to buffer p. p
4105b 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
4105c 67 20 74 6f 20 74 68 65 20 62 79 74 65 0a 20 20  g to the byte.  
4105d 20 20 20 20 2a 2a 20 61 66 74 65 72 20 74 68 65      ** after the
4105e 20 6c 69 73 74 20 77 72 69 74 74 65 6e 2e 20 4e   list written. N
4105f 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 28 50 4f  o terminator (PO
41060 53 5f 45 4e 44 20 6f 72 20 50 4f 53 5f 43 4f 4c  S_END or POS_COL
41061 55 4d 4e 29 20 69 73 0a 20 20 20 20 20 20 2a 2a  UMN) is.      **
41062 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
41063 6f 75 74 70 75 74 2e 0a 20 20 20 20 20 20 2a 2f  output..      */
41064 0a 20 20 20 20 20 20 66 74 73 33 47 65 74 44 65  .      fts3GetDe
41065 6c 74 61 56 61 72 69 6e 74 28 26 70 31 2c 20 26  ltaVarint(&p1, &
41066 69 31 29 3b 0a 20 20 20 20 20 20 66 74 73 33 47  i1);.      fts3G
41067 65 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26 70  etDeltaVarint(&p
41068 32 2c 20 26 69 32 29 3b 0a 20 20 20 20 20 20 64  2, &i2);.      d
41069 6f 20 7b 0a 20 20 20 20 20 20 20 20 66 74 73 33  o {.        fts3
4106a 50 75 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26  PutDeltaVarint(&
4106b 70 2c 20 26 69 50 72 65 76 2c 20 28 69 31 3c 69  p, &iPrev, (i1<i
4106c 32 29 20 3f 20 69 31 20 3a 20 69 32 29 3b 20 0a  2) ? i1 : i2); .
4106d 20 20 20 20 20 20 20 20 69 50 72 65 76 20 2d 3d          iPrev -=
4106e 20 32 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   2;.        if( 
4106f 69 31 3d 3d 69 32 20 29 7b 0a 20 20 20 20 20 20  i1==i2 ){.      
41070 20 20 20 20 66 74 73 33 52 65 61 64 4e 65 78 74      fts3ReadNext
41071 50 6f 73 28 26 70 31 2c 20 26 69 31 29 3b 0a 20  Pos(&p1, &i1);. 
41072 20 20 20 20 20 20 20 20 20 66 74 73 33 52 65 61           fts3Rea
41073 64 4e 65 78 74 50 6f 73 28 26 70 32 2c 20 26 69  dNextPos(&p2, &i
41074 32 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  2);.        }els
41075 65 20 69 66 28 20 69 31 3c 69 32 20 29 7b 0a 20  e if( i1<i2 ){. 
41076 20 20 20 20 20 20 20 20 20 66 74 73 33 52 65 61           fts3Rea
41077 64 4e 65 78 74 50 6f 73 28 26 70 31 2c 20 26 69  dNextPos(&p1, &i
41078 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  1);.        }els
41079 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  e{.          fts
4107a 33 52 65 61 64 4e 65 78 74 50 6f 73 28 26 70 32  3ReadNextPos(&p2
4107b 2c 20 26 69 32 29 3b 0a 20 20 20 20 20 20 20 20  , &i2);.        
4107c 7d 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20  }.      }while( 
4107d 69 31 21 3d 50 4f 53 49 54 49 4f 4e 5f 4c 49 53  i1!=POSITION_LIS
4107e 54 5f 45 4e 44 20 7c 7c 20 69 32 21 3d 50 4f 53  T_END || i2!=POS
4107f 49 54 49 4f 4e 5f 4c 49 53 54 5f 45 4e 44 20 29  ITION_LIST_END )
41080 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
41081 69 43 6f 6c 31 3c 69 43 6f 6c 32 20 29 7b 0a 20  iCol1<iCol2 ){. 
41082 20 20 20 20 20 70 31 20 2b 3d 20 66 74 73 33 50       p1 += fts3P
41083 75 74 43 6f 6c 4e 75 6d 62 65 72 28 26 70 2c 20  utColNumber(&p, 
41084 69 43 6f 6c 31 29 3b 0a 20 20 20 20 20 20 66 74  iCol1);.      ft
41085 73 33 43 6f 6c 75 6d 6e 6c 69 73 74 43 6f 70 79  s3ColumnlistCopy
41086 28 26 70 2c 20 26 70 31 29 3b 0a 20 20 20 20 7d  (&p, &p1);.    }
41087 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 32 20 2b  else{.      p2 +
41088 3d 20 66 74 73 33 50 75 74 43 6f 6c 4e 75 6d 62  = fts3PutColNumb
41089 65 72 28 26 70 2c 20 69 43 6f 6c 32 29 3b 0a 20  er(&p, iCol2);. 
4108a 20 20 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c       fts3Columnl
4108b 69 73 74 43 6f 70 79 28 26 70 2c 20 26 70 32 29  istCopy(&p, &p2)
4108c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a  ;.    }.  }..  *
4108d 70 2b 2b 20 3d 20 50 4f 53 5f 45 4e 44 3b 0a 20  p++ = POS_END;. 
4108e 20 2a 70 70 20 3d 20 70 3b 0a 20 20 2a 70 70 31   *pp = p;.  *pp1
4108f 20 3d 20 70 31 20 2b 20 31 3b 0a 20 20 2a 70 70   = p1 + 1;.  *pp
41090 32 20 3d 20 70 32 20 2b 20 31 3b 0a 7d 0a 0a 2f  2 = p2 + 1;.}../
41091 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
41092 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 6d 65  on is used to me
41093 72 67 65 20 74 77 6f 20 70 6f 73 69 74 69 6f 6e  rge two position
41094 20 6c 69 73 74 73 20 69 6e 74 6f 20 6f 6e 65 2e   lists into one.
41095 20 57 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20 63   When it is.** c
41096 61 6c 6c 65 64 2c 20 2a 70 70 31 20 61 6e 64 20  alled, *pp1 and 
41097 2a 70 70 32 20 6d 75 73 74 20 62 6f 74 68 20 70  *pp2 must both p
41098 6f 69 6e 74 20 74 6f 20 70 6f 73 69 74 69 6f 6e  oint to position
41099 20 6c 69 73 74 73 2e 20 41 20 70 6f 73 69 74 69   lists. A positi
4109a 6f 6e 2d 6c 69 73 74 20 69 73 0a 2a 2a 20 74 68  on-list is.** th
4109b 65 20 70 61 72 74 20 6f 66 20 61 20 64 6f 63 6c  e part of a docl
4109c 69 73 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  ist that follows
4109d 20 65 61 63 68 20 64 6f 63 75 6d 65 6e 74 20 69   each document i
4109e 64 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  d. For example, 
4109f 69 66 20 61 20 72 6f 77 0a 2a 2a 20 63 6f 6e 74  if a row.** cont
410a0 61 69 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ains:.**.**     
410a1 27 61 20 62 20 63 27 7c 27 78 20 79 20 7a 27 7c  'a b c'|'x y z'|
410a2 27 61 20 62 20 62 20 61 27 0a 2a 2a 0a 2a 2a 20  'a b b a'.**.** 
410a3 54 68 65 6e 20 74 68 65 20 70 6f 73 69 74 69 6f  Then the positio
410a4 6e 20 6c 69 73 74 20 66 6f 72 20 74 68 69 73 20  n list for this 
410a5 72 6f 77 20 66 6f 72 20 74 6f 6b 65 6e 20 27 62  row for token 'b
410a6 27 20 77 6f 75 6c 64 20 63 6f 6e 73 69 73 74 20  ' would consist 
410a7 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 30 78  of:.**.**     0x
410a8 30 32 20 30 78 30 31 20 30 78 30 32 20 30 78 30  02 0x01 0x02 0x0
410a9 33 20 30 78 30 33 20 30 78 30 30 0a 2a 2a 0a 2a  3 0x03 0x00.**.*
410aa 2a 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e 63  * When this func
410ab 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 62 6f  tion returns, bo
410ac 74 68 20 2a 70 70 31 20 61 6e 64 20 2a 70 70 32  th *pp1 and *pp2
410ad 20 61 72 65 20 6c 65 66 74 20 70 6f 69 6e 74 69   are left pointi
410ae 6e 67 20 74 6f 20 74 68 65 0a 2a 2a 20 62 79 74  ng to the.** byt
410af 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  e following the 
410b0 30 78 30 30 20 74 65 72 6d 69 6e 61 74 6f 72 20  0x00 terminator 
410b1 6f 66 20 74 68 65 69 72 20 72 65 73 70 65 63 74  of their respect
410b2 69 76 65 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73  ive position lis
410b3 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 73 53  ts..**.** If isS
410b4 61 76 65 4c 65 66 74 20 69 73 20 30 2c 20 61 6e  aveLeft is 0, an
410b5 20 65 6e 74 72 79 20 69 73 20 61 64 64 65 64 20   entry is added 
410b6 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 70 6f  to the output po
410b7 73 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20  sition list for 
410b8 0a 2a 2a 20 65 61 63 68 20 70 6f 73 69 74 69 6f  .** each positio
410b9 6e 20 69 6e 20 2a 70 70 32 20 66 6f 72 20 77 68  n in *pp2 for wh
410ba 69 63 68 20 74 68 65 72 65 20 65 78 69 73 74 73  ich there exists
410bb 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 70 6f 73   one or more pos
410bc 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 2a 70 70  itions in.** *pp
410bd 31 20 73 6f 20 74 68 61 74 20 28 70 6f 73 28 2a  1 so that (pos(*
410be 70 70 32 29 3e 70 6f 73 28 2a 70 70 31 29 20 26  pp2)>pos(*pp1) &
410bf 26 20 70 6f 73 28 2a 70 70 32 29 2d 70 6f 73 28  & pos(*pp2)-pos(
410c0 2a 70 70 31 29 3c 3d 6e 54 6f 6b 65 6e 29 2e 20  *pp1)<=nToken). 
410c1 69 2e 65 2e 0a 2a 2a 20 77 68 65 6e 20 74 68 65  i.e..** when the
410c2 20 2a 70 70 31 20 74 6f 6b 65 6e 20 61 70 70 65   *pp1 token appe
410c3 61 72 73 20 62 65 66 6f 72 65 20 74 68 65 20 2a  ars before the *
410c4 70 70 32 20 74 6f 6b 65 6e 2c 20 62 75 74 20 6e  pp2 token, but n
410c5 6f 74 20 6d 6f 72 65 20 74 68 61 6e 20 6e 54 6f  ot more than nTo
410c6 6b 65 6e 0a 2a 2a 20 73 6c 6f 74 73 20 62 65 66  ken.** slots bef
410c7 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 65 2e  ore it..**.** e.
410c8 67 2e 20 6e 54 6f 6b 65 6e 3d 3d 31 20 73 65 61  g. nToken==1 sea
410c9 72 63 68 65 73 20 66 6f 72 20 61 64 6a 61 63 65  rches for adjace
410ca 6e 74 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2f  nt positions..*/
410cb 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
410cc 50 6f 73 6c 69 73 74 50 68 72 61 73 65 4d 65 72  PoslistPhraseMer
410cd 67 65 28 0a 20 20 63 68 61 72 20 2a 2a 70 70 2c  ge(.  char **pp,
410ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
410cf 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a        /* IN/OUT:
410d0 20 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 6f 75   Preallocated ou
410d1 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20  tput buffer */. 
410d2 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 20 20 20   int nToken,    
410d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
410d4 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 64 69 66 66   /* Maximum diff
410d5 65 72 65 6e 63 65 20 69 6e 20 74 6f 6b 65 6e 20  erence in token 
410d6 70 6f 73 69 74 69 6f 6e 73 20 2a 2f 0a 20 20 69  positions */.  i
410d7 6e 74 20 69 73 53 61 76 65 4c 65 66 74 2c 20 20  nt isSaveLeft,  
410d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
410d9 2a 20 53 61 76 65 20 74 68 65 20 6c 65 66 74 20  * Save the left 
410da 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  position */.  in
410db 74 20 69 73 45 78 61 63 74 2c 20 20 20 20 20 20  t isExact,      
410dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
410dd 20 49 66 20 2a 70 70 31 20 69 73 20 65 78 61 63   If *pp1 is exac
410de 74 6c 79 20 6e 54 6f 6b 65 6e 73 20 62 65 66 6f  tly nTokens befo
410df 72 65 20 2a 70 70 32 20 2a 2f 0a 20 20 63 68 61  re *pp2 */.  cha
410e0 72 20 2a 2a 70 70 31 2c 20 20 20 20 20 20 20 20  r **pp1,        
410e1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
410e2 49 4e 2f 4f 55 54 3a 20 4c 65 66 74 20 69 6e 70  IN/OUT: Left inp
410e3 75 74 20 6c 69 73 74 20 2a 2f 0a 20 20 63 68 61  ut list */.  cha
410e4 72 20 2a 2a 70 70 32 20 20 20 20 20 20 20 20 20  r **pp2         
410e5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
410e6 49 4e 2f 4f 55 54 3a 20 52 69 67 68 74 20 69 6e  IN/OUT: Right in
410e7 70 75 74 20 6c 69 73 74 20 2a 2f 0a 29 7b 0a 20  put list */.){. 
410e8 20 63 68 61 72 20 2a 70 20 3d 20 2a 70 70 3b 0a   char *p = *pp;.
410e9 20 20 63 68 61 72 20 2a 70 31 20 3d 20 2a 70 70    char *p1 = *pp
410ea 31 3b 0a 20 20 63 68 61 72 20 2a 70 32 20 3d 20  1;.  char *p2 = 
410eb 2a 70 70 32 3b 0a 20 20 69 6e 74 20 69 43 6f 6c  *pp2;.  int iCol
410ec 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f  1 = 0;.  int iCo
410ed 6c 32 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4e 65  l2 = 0;..  /* Ne
410ee 76 65 72 20 73 65 74 20 62 6f 74 68 20 69 73 53  ver set both isS
410ef 61 76 65 4c 65 66 74 20 61 6e 64 20 69 73 45 78  aveLeft and isEx
410f0 61 63 74 20 66 6f 72 20 74 68 65 20 73 61 6d 65  act for the same
410f1 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 2a 2f 0a   invocation. */.
410f2 20 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65    assert( isSave
410f3 4c 65 66 74 3d 3d 30 20 7c 7c 20 69 73 45 78 61  Left==0 || isExa
410f4 63 74 3d 3d 30 20 29 3b 0a 0a 20 20 61 73 73 65  ct==0 );..  asse
410f5 72 74 28 20 70 21 3d 30 20 26 26 20 2a 70 31 21  rt( p!=0 && *p1!
410f6 3d 30 20 26 26 20 2a 70 32 21 3d 30 20 29 3b 0a  =0 && *p2!=0 );.
410f7 20 20 69 66 28 20 2a 70 31 3d 3d 50 4f 53 5f 43    if( *p1==POS_C
410f8 4f 4c 55 4d 4e 20 29 7b 20 0a 20 20 20 20 70 31  OLUMN ){ .    p1
410f9 2b 2b 3b 0a 20 20 20 20 70 31 20 2b 3d 20 73 71  ++;.    p1 += sq
410fa 6c 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69  lite3Fts3GetVari
410fb 6e 74 33 32 28 70 31 2c 20 26 69 43 6f 6c 31 29  nt32(p1, &iCol1)
410fc 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 70 32 3d  ;.  }.  if( *p2=
410fd 3d 50 4f 53 5f 43 4f 4c 55 4d 4e 20 29 7b 20 0a  =POS_COLUMN ){ .
410fe 20 20 20 20 70 32 2b 2b 3b 0a 20 20 20 20 70 32      p2++;.    p2
410ff 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47   += sqlite3Fts3G
41100 65 74 56 61 72 69 6e 74 33 32 28 70 32 2c 20 26  etVarint32(p2, &
41101 69 43 6f 6c 32 29 3b 0a 20 20 7d 0a 0a 20 20 77  iCol2);.  }..  w
41102 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 69  hile( 1 ){.    i
41103 66 28 20 69 43 6f 6c 31 3d 3d 69 43 6f 6c 32 20  f( iCol1==iCol2 
41104 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70  ){.      char *p
41105 53 61 76 65 20 3d 20 70 3b 0a 20 20 20 20 20 20  Save = p;.      
41106 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50  sqlite3_int64 iP
41107 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 73  rev = 0;.      s
41108 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50 6f  qlite3_int64 iPo
41109 73 31 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71  s1 = 0;.      sq
4110a 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50 6f 73  lite3_int64 iPos
4110b 32 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 69 66  2 = 0;..      if
4110c 28 20 69 43 6f 6c 31 20 29 7b 0a 20 20 20 20 20  ( iCol1 ){.     
4110d 20 20 20 2a 70 2b 2b 20 3d 20 50 4f 53 5f 43 4f     *p++ = POS_CO
4110e 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 20 20 70 20  LUMN;.        p 
4110f 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50 75  += sqlite3Fts3Pu
41110 74 56 61 72 69 6e 74 28 70 2c 20 69 43 6f 6c 31  tVarint(p, iCol1
41111 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
41112 20 20 61 73 73 65 72 74 28 20 2a 70 31 21 3d 50    assert( *p1!=P
41113 4f 53 5f 45 4e 44 20 26 26 20 2a 70 31 21 3d 50  OS_END && *p1!=P
41114 4f 53 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20  OS_COLUMN );.   
41115 20 20 20 61 73 73 65 72 74 28 20 2a 70 32 21 3d     assert( *p2!=
41116 50 4f 53 5f 45 4e 44 20 26 26 20 2a 70 32 21 3d  POS_END && *p2!=
41117 50 4f 53 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20  POS_COLUMN );.  
41118 20 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61      fts3GetDelta
41119 56 61 72 69 6e 74 28 26 70 31 2c 20 26 69 50 6f  Varint(&p1, &iPo
4111a 73 31 29 3b 20 69 50 6f 73 31 20 2d 3d 20 32 3b  s1); iPos1 -= 2;
4111b 0a 20 20 20 20 20 20 66 74 73 33 47 65 74 44 65  .      fts3GetDe
4111c 6c 74 61 56 61 72 69 6e 74 28 26 70 32 2c 20 26  ltaVarint(&p2, &
4111d 69 50 6f 73 32 29 3b 20 69 50 6f 73 32 20 2d 3d  iPos2); iPos2 -=
4111e 20 32 3b 0a 0a 20 20 20 20 20 20 77 68 69 6c 65   2;..      while
4111f 28 20 31 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ( 1 ){.        i
41120 66 28 20 69 50 6f 73 32 3d 3d 69 50 6f 73 31 2b  f( iPos2==iPos1+
41121 6e 54 6f 6b 65 6e 20 0a 20 20 20 20 20 20 20 20  nToken .        
41122 20 7c 7c 20 28 69 73 45 78 61 63 74 3d 3d 30 20   || (isExact==0 
41123 26 26 20 69 50 6f 73 32 3e 69 50 6f 73 31 20 26  && iPos2>iPos1 &
41124 26 20 69 50 6f 73 32 3c 3d 69 50 6f 73 31 2b 6e  & iPos2<=iPos1+n
41125 54 6f 6b 65 6e 29 20 0a 20 20 20 20 20 20 20 20  Token) .        
41126 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
41127 69 74 65 33 5f 69 6e 74 36 34 20 69 53 61 76 65  ite3_int64 iSave
41128 3b 0a 20 20 20 20 20 20 20 20 20 20 69 53 61 76  ;.          iSav
41129 65 20 3d 20 69 73 53 61 76 65 4c 65 66 74 20 3f  e = isSaveLeft ?
4112a 20 69 50 6f 73 31 20 3a 20 69 50 6f 73 32 3b 0a   iPos1 : iPos2;.
4112b 20 20 20 20 20 20 20 20 20 20 66 74 73 33 50 75            fts3Pu
4112c 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26 70 2c  tDeltaVarint(&p,
4112d 20 26 69 50 72 65 76 2c 20 69 53 61 76 65 2b 32   &iPrev, iSave+2
4112e 29 3b 20 69 50 72 65 76 20 2d 3d 20 32 3b 0a 20  ); iPrev -= 2;. 
4112f 20 20 20 20 20 20 20 20 20 70 53 61 76 65 20 3d           pSave =
41130 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73   0;.          as
41131 73 65 72 74 28 20 70 20 29 3b 0a 20 20 20 20 20  sert( p );.     
41132 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
41133 20 28 21 69 73 53 61 76 65 4c 65 66 74 20 26 26   (!isSaveLeft &&
41134 20 69 50 6f 73 32 3c 3d 28 69 50 6f 73 31 2b 6e   iPos2<=(iPos1+n
41135 54 6f 6b 65 6e 29 29 20 7c 7c 20 69 50 6f 73 32  Token)) || iPos2
41136 3c 3d 69 50 6f 73 31 20 29 7b 0a 20 20 20 20 20  <=iPos1 ){.     
41137 20 20 20 20 20 69 66 28 20 28 2a 70 32 26 30 78       if( (*p2&0x
41138 46 45 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  FE)==0 ) break;.
41139 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47 65            fts3Ge
4113a 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26 70 32  tDeltaVarint(&p2
4113b 2c 20 26 69 50 6f 73 32 29 3b 20 69 50 6f 73 32  , &iPos2); iPos2
4113c 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 7d   -= 2;.        }
4113d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
4113e 69 66 28 20 28 2a 70 31 26 30 78 46 45 29 3d 3d  if( (*p1&0xFE)==
4113f 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  0 ) break;.     
41140 20 20 20 20 20 66 74 73 33 47 65 74 44 65 6c 74       fts3GetDelt
41141 61 56 61 72 69 6e 74 28 26 70 31 2c 20 26 69 50  aVarint(&p1, &iP
41142 6f 73 31 29 3b 20 69 50 6f 73 31 20 2d 3d 20 32  os1); iPos1 -= 2
41143 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
41144 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 70    }..      if( p
41145 53 61 76 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Save ){.        
41146 61 73 73 65 72 74 28 20 70 70 20 26 26 20 70 20  assert( pp && p 
41147 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70  );.        p = p
41148 53 61 76 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  Save;.      }.. 
41149 20 20 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c       fts3Columnl
4114a 69 73 74 43 6f 70 79 28 30 2c 20 26 70 31 29 3b  istCopy(0, &p1);
4114b 0a 20 20 20 20 20 20 66 74 73 33 43 6f 6c 75 6d  .      fts3Colum
4114c 6e 6c 69 73 74 43 6f 70 79 28 30 2c 20 26 70 32  nlistCopy(0, &p2
4114d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
4114e 20 28 2a 70 31 26 30 78 46 45 29 3d 3d 30 20 26   (*p1&0xFE)==0 &
4114f 26 20 28 2a 70 32 26 30 78 46 45 29 3d 3d 30 20  & (*p2&0xFE)==0 
41150 29 3b 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d  );.      if( 0==
41151 2a 70 31 20 7c 7c 20 30 3d 3d 2a 70 32 20 29 20  *p1 || 0==*p2 ) 
41152 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 70 31  break;..      p1
41153 2b 2b 3b 0a 20 20 20 20 20 20 70 31 20 2b 3d 20  ++;.      p1 += 
41154 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61  sqlite3Fts3GetVa
41155 72 69 6e 74 33 32 28 70 31 2c 20 26 69 43 6f 6c  rint32(p1, &iCol
41156 31 29 3b 0a 20 20 20 20 20 20 70 32 2b 2b 3b 0a  1);.      p2++;.
41157 20 20 20 20 20 20 70 32 20 2b 3d 20 73 71 6c 69        p2 += sqli
41158 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74  te3Fts3GetVarint
41159 33 32 28 70 32 2c 20 26 69 43 6f 6c 32 29 3b 0a  32(p2, &iCol2);.
4115a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64      }..    /* Ad
4115b 76 61 6e 63 65 20 70 6f 69 6e 74 65 72 20 70 31  vance pointer p1
4115c 20 6f 72 20 70 32 20 28 77 68 69 63 68 65 76 65   or p2 (whicheve
4115d 72 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  r corresponds to
4115e 20 74 68 65 20 73 6d 61 6c 6c 65 72 20 6f 66 0a   the smaller of.
4115f 20 20 20 20 2a 2a 20 69 43 6f 6c 31 20 61 6e 64      ** iCol1 and
41160 20 69 43 6f 6c 32 29 20 73 6f 20 74 68 61 74 20   iCol2) so that 
41161 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 65 69 74  it points to eit
41162 68 65 72 20 74 68 65 20 30 78 30 30 20 74 68 61  her the 0x00 tha
41163 74 20 6d 61 72 6b 73 20 74 68 65 0a 20 20 20 20  t marks the.    
41164 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 70 6f  ** end of the po
41165 73 69 74 69 6f 6e 20 6c 69 73 74 2c 20 6f 72 20  sition list, or 
41166 74 68 65 20 30 78 30 31 20 74 68 61 74 20 70 72  the 0x01 that pr
41167 65 63 65 64 65 73 20 74 68 65 20 6e 65 78 74 20  ecedes the next 
41168 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 2d 6e  .    ** column-n
41169 75 6d 62 65 72 20 69 6e 20 74 68 65 20 70 6f 73  umber in the pos
4116a 69 74 69 6f 6e 20 6c 69 73 74 2e 20 0a 20 20 20  ition list. .   
4116b 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 28   */.    else if(
4116c 20 69 43 6f 6c 31 3c 69 43 6f 6c 32 20 29 7b 0a   iCol1<iCol2 ){.
4116d 20 20 20 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e        fts3Column
4116e 6c 69 73 74 43 6f 70 79 28 30 2c 20 26 70 31 29  listCopy(0, &p1)
4116f 3b 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 2a  ;.      if( 0==*
41170 70 31 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  p1 ) break;.    
41171 20 20 70 31 2b 2b 3b 0a 20 20 20 20 20 20 70 31    p1++;.      p1
41172 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47   += sqlite3Fts3G
41173 65 74 56 61 72 69 6e 74 33 32 28 70 31 2c 20 26  etVarint32(p1, &
41174 69 43 6f 6c 31 29 3b 0a 20 20 20 20 7d 65 6c 73  iCol1);.    }els
41175 65 7b 0a 20 20 20 20 20 20 66 74 73 33 43 6f 6c  e{.      fts3Col
41176 75 6d 6e 6c 69 73 74 43 6f 70 79 28 30 2c 20 26  umnlistCopy(0, &
41177 70 32 29 3b 0a 20 20 20 20 20 20 69 66 28 20 30  p2);.      if( 0
41178 3d 3d 2a 70 32 20 29 20 62 72 65 61 6b 3b 0a 20  ==*p2 ) break;. 
41179 20 20 20 20 20 70 32 2b 2b 3b 0a 20 20 20 20 20       p2++;.     
4117a 20 70 32 20 2b 3d 20 73 71 6c 69 74 65 33 46 74   p2 += sqlite3Ft
4117b 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 32  s3GetVarint32(p2
4117c 2c 20 26 69 43 6f 6c 32 29 3b 0a 20 20 20 20 7d  , &iCol2);.    }
4117d 0a 20 20 7d 0a 0a 20 20 66 74 73 33 50 6f 73 6c  .  }..  fts3Posl
4117e 69 73 74 43 6f 70 79 28 30 2c 20 26 70 32 29 3b  istCopy(0, &p2);
4117f 0a 20 20 66 74 73 33 50 6f 73 6c 69 73 74 43 6f  .  fts3PoslistCo
41180 70 79 28 30 2c 20 26 70 31 29 3b 0a 20 20 2a 70  py(0, &p1);.  *p
41181 70 31 20 3d 20 70 31 3b 0a 20 20 2a 70 70 32 20  p1 = p1;.  *pp2 
41182 3d 20 70 32 3b 0a 20 20 69 66 28 20 2a 70 70 3d  = p2;.  if( *pp=
41183 3d 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =p ){.    return
41184 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 2b 2b 20 3d   0;.  }.  *p++ =
41185 20 30 78 30 30 3b 0a 20 20 2a 70 70 20 3d 20 70   0x00;.  *pp = p
41186 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  ;.  return 1;.}.
41187 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f  ./*.** Merge two
41188 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 73 20   position-lists 
41189 61 73 20 72 65 71 75 69 72 65 64 20 62 79 20 74  as required by t
4118a 68 65 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72  he NEAR operator
4118b 2e 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a  . The argument.*
4118c 2a 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73  * position lists
4118d 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74   correspond to t
4118e 68 65 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68  he left and righ
4118f 74 20 70 68 72 61 73 65 73 20 6f 66 20 61 6e 20  t phrases of an 
41190 65 78 70 72 65 73 73 69 6f 6e 20 0a 2a 2a 20 6c  expression .** l
41191 69 6b 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22  ike:.**.**     "
41192 70 68 72 61 73 65 20 31 22 20 4e 45 41 52 20 22  phrase 1" NEAR "
41193 70 68 72 61 73 65 20 6e 75 6d 62 65 72 20 32 22  phrase number 2"
41194 0a 2a 2a 0a 2a 2a 20 50 6f 73 69 74 69 6f 6e 20  .**.** Position 
41195 6c 69 73 74 20 2a 70 70 31 20 63 6f 72 72 65 73  list *pp1 corres
41196 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 6c 65 66  ponds to the lef
41197 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74  t-hand side of t
41198 68 65 20 4e 45 41 52 20 0a 2a 2a 20 65 78 70 72  he NEAR .** expr
41199 65 73 73 69 6f 6e 20 61 6e 64 20 2a 70 70 32 20  ession and *pp2 
4119a 74 6f 20 74 68 65 20 72 69 67 68 74 2e 20 41 73  to the right. As
4119b 20 75 73 75 61 6c 2c 20 74 68 65 20 69 6e 64 65   usual, the inde
4119c 78 65 73 20 69 6e 20 74 68 65 20 70 6f 73 69 74  xes in the posit
4119d 69 6f 6e 20 0a 2a 2a 20 6c 69 73 74 73 20 61 72  ion .** lists ar
4119e 65 20 74 68 65 20 6f 66 66 73 65 74 73 20 6f 66  e the offsets of
4119f 20 74 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20   the last token 
411a0 69 6e 20 65 61 63 68 20 70 68 72 61 73 65 20 28  in each phrase (
411a1 74 6f 6b 65 6e 73 20 22 31 22 20 61 6e 64 20 22  tokens "1" and "
411a2 32 22 20 0a 2a 2a 20 69 6e 20 74 68 65 20 65 78  2" .** in the ex
411a3 61 6d 70 6c 65 20 61 62 6f 76 65 29 2e 0a 2a 2a  ample above)..**
411a4 0a 2a 2a 20 54 68 65 20 6f 75 74 70 75 74 20 70  .** The output p
411a5 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 2d 20 77  osition list - w
411a6 72 69 74 74 65 6e 20 74 6f 20 2a 70 70 20 2d 20  ritten to *pp - 
411a7 69 73 20 61 20 63 6f 70 79 20 6f 66 20 2a 70 70  is a copy of *pp
411a8 32 20 77 69 74 68 20 74 68 6f 73 65 0a 2a 2a 20  2 with those.** 
411a9 65 6e 74 72 69 65 73 20 74 68 61 74 20 61 72 65  entries that are
411aa 20 6e 6f 74 20 73 75 66 66 69 63 69 65 6e 74 6c   not sufficientl
411ab 79 20 4e 45 41 52 20 65 6e 74 72 69 65 73 20 69  y NEAR entries i
411ac 6e 20 2a 70 70 31 20 72 65 6d 6f 76 65 64 2e 0a  n *pp1 removed..
411ad 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
411ae 73 33 50 6f 73 6c 69 73 74 4e 65 61 72 4d 65 72  s3PoslistNearMer
411af 67 65 28 0a 20 20 63 68 61 72 20 2a 2a 70 70 2c  ge(.  char **pp,
411b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
411b1 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20        /* Output 
411b2 62 75 66 66 65 72 20 2a 2f 0a 20 20 63 68 61 72  buffer */.  char
411b3 20 2a 61 54 6d 70 2c 20 20 20 20 20 20 20 20 20   *aTmp,         
411b4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
411b5 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72 20  emporary buffer 
411b6 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  space */.  int n
411b7 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20  Right,          
411b8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
411b9 78 69 6d 75 6d 20 64 69 66 66 65 72 65 6e 63 65  ximum difference
411ba 20 69 6e 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69   in token positi
411bb 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 65  ons */.  int nLe
411bc 66 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ft,             
411bd 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
411be 6d 75 6d 20 64 69 66 66 65 72 65 6e 63 65 20 69  mum difference i
411bf 6e 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69 6f 6e  n token position
411c0 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 70  s */.  char **pp
411c1 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
411c2 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54         /* IN/OUT
411c3 3a 20 4c 65 66 74 20 69 6e 70 75 74 20 6c 69 73  : Left input lis
411c4 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 70  t */.  char **pp
411c5 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2               
411c6 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54         /* IN/OUT
411c7 3a 20 52 69 67 68 74 20 69 6e 70 75 74 20 6c 69  : Right input li
411c8 73 74 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20  st */.){.  char 
411c9 2a 70 31 20 3d 20 2a 70 70 31 3b 0a 20 20 63 68  *p1 = *pp1;.  ch
411ca 61 72 20 2a 70 32 20 3d 20 2a 70 70 32 3b 0a 0a  ar *p2 = *pp2;..
411cb 20 20 63 68 61 72 20 2a 70 54 6d 70 31 20 3d 20    char *pTmp1 = 
411cc 61 54 6d 70 3b 0a 20 20 63 68 61 72 20 2a 70 54  aTmp;.  char *pT
411cd 6d 70 32 3b 0a 20 20 63 68 61 72 20 2a 61 54 6d  mp2;.  char *aTm
411ce 70 32 3b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20  p2;.  int res = 
411cf 31 3b 0a 0a 20 20 66 74 73 33 50 6f 73 6c 69 73  1;..  fts3Poslis
411d0 74 50 68 72 61 73 65 4d 65 72 67 65 28 26 70 54  tPhraseMerge(&pT
411d1 6d 70 31 2c 20 6e 52 69 67 68 74 2c 20 30 2c 20  mp1, nRight, 0, 
411d2 30 2c 20 70 70 31 2c 20 70 70 32 29 3b 0a 20 20  0, pp1, pp2);.  
411d3 61 54 6d 70 32 20 3d 20 70 54 6d 70 32 20 3d 20  aTmp2 = pTmp2 = 
411d4 70 54 6d 70 31 3b 0a 20 20 2a 70 70 31 20 3d 20  pTmp1;.  *pp1 = 
411d5 70 31 3b 0a 20 20 2a 70 70 32 20 3d 20 70 32 3b  p1;.  *pp2 = p2;
411d6 0a 20 20 66 74 73 33 50 6f 73 6c 69 73 74 50 68  .  fts3PoslistPh
411d7 72 61 73 65 4d 65 72 67 65 28 26 70 54 6d 70 32  raseMerge(&pTmp2
411d8 2c 20 6e 4c 65 66 74 2c 20 31 2c 20 30 2c 20 70  , nLeft, 1, 0, p
411d9 70 32 2c 20 70 70 31 29 3b 0a 20 20 69 66 28 20  p2, pp1);.  if( 
411da 70 54 6d 70 31 21 3d 61 54 6d 70 20 26 26 20 70  pTmp1!=aTmp && p
411db 54 6d 70 32 21 3d 61 54 6d 70 32 20 29 7b 0a 20  Tmp2!=aTmp2 ){. 
411dc 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74 4d 65     fts3PoslistMe
411dd 72 67 65 28 70 70 2c 20 26 61 54 6d 70 2c 20 26  rge(pp, &aTmp, &
411de 61 54 6d 70 32 29 3b 0a 20 20 7d 65 6c 73 65 20  aTmp2);.  }else 
411df 69 66 28 20 70 54 6d 70 31 21 3d 61 54 6d 70 20  if( pTmp1!=aTmp 
411e0 29 7b 0a 20 20 20 20 66 74 73 33 50 6f 73 6c 69  ){.    fts3Posli
411e1 73 74 43 6f 70 79 28 70 70 2c 20 26 61 54 6d 70  stCopy(pp, &aTmp
411e2 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  );.  }else if( p
411e3 54 6d 70 32 21 3d 61 54 6d 70 32 20 29 7b 0a 20  Tmp2!=aTmp2 ){. 
411e4 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74 43 6f     fts3PoslistCo
411e5 70 79 28 70 70 2c 20 26 61 54 6d 70 32 29 3b 0a  py(pp, &aTmp2);.
411e6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 73    }else{.    res
411e7 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74   = 0;.  }..  ret
411e8 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 20 0a  urn res;.}../* .
411e9 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
411ea 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  f this function 
411eb 69 73 20 75 73 65 64 20 74 6f 20 6d 65 72 67 65  is used to merge
411ec 20 74 6f 67 65 74 68 65 72 20 74 68 65 20 28 70   together the (p
411ed 6f 74 65 6e 74 69 61 6c 6c 79 0a 2a 2a 20 6c 61  otentially.** la
411ee 72 67 65 20 6e 75 6d 62 65 72 20 6f 66 29 20 64  rge number of) d
411ef 6f 63 6c 69 73 74 73 20 66 6f 72 20 65 61 63 68  oclists for each
411f0 20 74 65 72 6d 20 74 68 61 74 20 6d 61 74 63 68   term that match
411f1 65 73 20 61 20 70 72 65 66 69 78 20 71 75 65 72  es a prefix quer
411f2 79 2e 0a 2a 2a 20 53 65 65 20 66 75 6e 63 74 69  y..** See functi
411f3 6f 6e 20 66 74 73 33 54 65 72 6d 53 65 6c 65 63  on fts3TermSelec
411f4 74 4d 65 72 67 65 28 29 20 66 6f 72 20 64 65 74  tMerge() for det
411f5 61 69 6c 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ails..*/.typedef
411f6 20 73 74 72 75 63 74 20 54 65 72 6d 53 65 6c 65   struct TermSele
411f7 63 74 20 54 65 72 6d 53 65 6c 65 63 74 3b 0a 73  ct TermSelect;.s
411f8 74 72 75 63 74 20 54 65 72 6d 53 65 6c 65 63 74  truct TermSelect
411f9 20 7b 0a 20 20 63 68 61 72 20 2a 61 61 4f 75 74   {.  char *aaOut
411fa 70 75 74 5b 31 36 5d 3b 20 20 20 20 20 20 20 20  put[16];        
411fb 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64       /* Malloc'd
411fc 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 73 20   output buffers 
411fd 2a 2f 0a 20 20 69 6e 74 20 61 6e 4f 75 74 70 75  */.  int anOutpu
411fe 74 5b 31 36 5d 3b 20 20 20 20 20 20 20 20 20 20  t[16];          
411ff 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 65 61 63       /* Size eac
41200 68 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  h output buffer 
41201 69 6e 20 62 79 74 65 73 20 2a 2f 0a 7d 3b 0a 0a  in bytes */.};..
41202 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
41203 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72  ion is used to r
41204 65 61 64 20 61 20 73 69 6e 67 6c 65 20 76 61 72  ead a single var
41205 69 6e 74 20 66 72 6f 6d 20 61 20 62 75 66 66 65  int from a buffe
41206 72 2e 20 50 61 72 61 6d 65 74 65 72 0a 2a 2a 20  r. Parameter.** 
41207 70 45 6e 64 20 70 6f 69 6e 74 73 20 31 20 62 79  pEnd points 1 by
41208 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  te past the end 
41209 6f 66 20 74 68 65 20 62 75 66 66 65 72 2e 20 57  of the buffer. W
4120a 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
4120b 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2c 20  n is.** called, 
4120c 69 66 20 2a 70 70 20 70 6f 69 6e 74 73 20 74 6f  if *pp points to
4120d 20 70 45 6e 64 20 6f 72 20 67 72 65 61 74 65 72   pEnd or greater
4120e 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 64 20 6f  , then the end o
4120f 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a 20  f the buffer.** 
41210 68 61 73 20 62 65 65 6e 20 72 65 61 63 68 65 64  has been reached
41211 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 2a  . In this case *
41212 70 70 20 69 73 20 73 65 74 20 74 6f 20 30 20 61  pp is set to 0 a
41213 6e 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  nd the function 
41214 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49  returns..**.** I
41215 66 20 2a 70 70 20 64 6f 65 73 20 6e 6f 74 20 70  f *pp does not p
41216 6f 69 6e 74 20 74 6f 20 6f 72 20 70 61 73 74 20  oint to or past 
41217 70 45 6e 64 2c 20 74 68 65 6e 20 61 20 73 69 6e  pEnd, then a sin
41218 67 6c 65 20 76 61 72 69 6e 74 20 69 73 20 72 65  gle varint is re
41219 61 64 0a 2a 2a 20 66 72 6f 6d 20 2a 70 70 2e 20  ad.** from *pp. 
4121a 2a 70 70 20 69 73 20 74 68 65 6e 20 73 65 74 20  *pp is then set 
4121b 74 6f 20 70 6f 69 6e 74 20 31 20 62 79 74 65 20  to point 1 byte 
4121c 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
4121d 74 68 65 20 72 65 61 64 20 76 61 72 69 6e 74 2e  the read varint.
4121e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 44 65 73 63 49  .**.** If bDescI
4121f 64 78 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65  dx is false, the
41220 20 76 61 6c 75 65 20 72 65 61 64 20 69 73 20 61   value read is a
41221 64 64 65 64 20 74 6f 20 2a 70 56 61 6c 20 62 65  dded to *pVal be
41222 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a  fore returning..
41223 2a 2a 20 49 66 20 69 74 20 69 73 20 74 72 75 65  ** If it is true
41224 2c 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64  , the value read
41225 20 69 73 20 73 75 62 74 72 61 63 74 65 64 20 66   is subtracted f
41226 72 6f 6d 20 2a 70 56 61 6c 20 62 65 66 6f 72 65  rom *pVal before
41227 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69   this .** functi
41228 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a 73  on returns..*/.s
41229 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 47  tatic void fts3G
4122a 65 74 44 65 6c 74 61 56 61 72 69 6e 74 33 28 0a  etDeltaVarint3(.
4122b 20 20 63 68 61 72 20 2a 2a 70 70 2c 20 20 20 20    char **pp,    
4122c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4122d 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 50 6f 69    /* IN/OUT: Poi
4122e 6e 74 20 74 6f 20 72 65 61 64 20 76 61 72 69 6e  nt to read varin
4122f 74 20 66 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72  t from */.  char
41230 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20 20 20   *pEnd,         
41231 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
41232 6e 64 20 6f 66 20 62 75 66 66 65 72 20 2a 2f 0a  nd of buffer */.
41233 20 20 69 6e 74 20 62 44 65 73 63 49 64 78 2c 20    int bDescIdx, 
41234 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41235 20 20 2f 2a 20 54 72 75 65 20 69 66 20 64 6f 63    /* True if doc
41236 69 64 73 20 61 72 65 20 64 65 73 63 65 6e 64 69  ids are descendi
41237 6e 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ng */.  sqlite3_
41238 69 6e 74 36 34 20 2a 70 56 61 6c 20 20 20 20 20  int64 *pVal     
41239 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55          /* IN/OU
4123a 54 3a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65  T: Integer value
4123b 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 2a 70 70   */.){.  if( *pp
4123c 3e 3d 70 45 6e 64 20 29 7b 0a 20 20 20 20 2a 70  >=pEnd ){.    *p
4123d 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  p = 0;.  }else{.
4123e 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
4123f 34 20 69 56 61 6c 3b 0a 20 20 20 20 2a 70 70 20  4 iVal;.    *pp 
41240 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65  += sqlite3Fts3Ge
41241 74 56 61 72 69 6e 74 28 2a 70 70 2c 20 26 69 56  tVarint(*pp, &iV
41242 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 62 44 65  al);.    if( bDe
41243 73 63 49 64 78 20 29 7b 0a 20 20 20 20 20 20 2a  scIdx ){.      *
41244 70 56 61 6c 20 2d 3d 20 69 56 61 6c 3b 0a 20 20  pVal -= iVal;.  
41245 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a    }else{.      *
41246 70 56 61 6c 20 2b 3d 20 69 56 61 6c 3b 0a 20 20  pVal += iVal;.  
41247 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
41248 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
41249 73 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 20  s used to write 
4124a 61 20 73 69 6e 67 6c 65 20 76 61 72 69 6e 74 20  a single varint 
4124b 74 6f 20 61 20 62 75 66 66 65 72 2e 20 54 68 65  to a buffer. The
4124c 20 76 61 72 69 6e 74 0a 2a 2a 20 69 73 20 77 72   varint.** is wr
4124d 69 74 74 65 6e 20 74 6f 20 2a 70 70 2e 20 42 65  itten to *pp. Be
4124e 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20  fore returning, 
4124f 2a 70 70 20 69 73 20 73 65 74 20 74 6f 20 70 6f  *pp is set to po
41250 69 6e 74 20 31 20 62 79 74 65 20 70 61 73 74 20  int 1 byte past 
41251 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68  the.** end of th
41252 65 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 2e  e value written.
41253 0a 2a 2a 0a 2a 2a 20 49 66 20 2a 70 62 46 69 72  .**.** If *pbFir
41254 73 74 20 69 73 20 7a 65 72 6f 20 77 68 65 6e 20  st is zero when 
41255 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
41256 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 76 61 6c   called, the val
41257 75 65 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a  ue written to.**
41258 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20 74   the buffer is t
41259 68 61 74 20 6f 66 20 70 61 72 61 6d 65 74 65 72  hat of parameter
4125a 20 69 56 61 6c 2e 20 0a 2a 2a 0a 2a 2a 20 49 66   iVal. .**.** If
4125b 20 2a 70 62 46 69 72 73 74 20 69 73 20 6e 6f 6e   *pbFirst is non
4125c 2d 7a 65 72 6f 20 77 68 65 6e 20 74 68 69 73 20  -zero when this 
4125d 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
4125e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  ed, then the val
4125f 75 65 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69  ue .** written i
41260 73 20 65 69 74 68 65 72 20 28 69 56 61 6c 2d 2a  s either (iVal-*
41261 70 69 50 72 65 76 29 20 28 69 66 20 62 44 65 73  piPrev) (if bDes
41262 63 49 64 78 20 69 73 20 7a 65 72 6f 29 20 6f 72  cIdx is zero) or
41263 20 28 2a 70 69 50 72 65 76 2d 69 56 61 6c 29 0a   (*piPrev-iVal).
41264 2a 2a 20 28 69 66 20 62 44 65 73 63 49 64 78 20  ** (if bDescIdx 
41265 69 73 20 6e 6f 6e 2d 7a 65 72 6f 29 2e 0a 2a 2a  is non-zero)..**
41266 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72  .** Before retur
41267 6e 69 6e 67 2c 20 74 68 69 73 20 66 75 6e 63 74  ning, this funct
41268 69 6f 6e 20 61 6c 77 61 79 73 20 73 65 74 73 20  ion always sets 
41269 2a 70 62 46 69 72 73 74 20 74 6f 20 31 20 61 6e  *pbFirst to 1 an
4126a 64 20 2a 70 69 50 72 65 76 0a 2a 2a 20 74 6f 20  d *piPrev.** to 
4126b 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 61 72  the value of par
4126c 61 6d 65 74 65 72 20 69 56 61 6c 2e 0a 2a 2f 0a  ameter iVal..*/.
4126d 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
4126e 50 75 74 44 65 6c 74 61 56 61 72 69 6e 74 33 28  PutDeltaVarint3(
4126f 0a 20 20 63 68 61 72 20 2a 2a 70 70 2c 20 20 20  .  char **pp,   
41270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41271 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 4f 75     /* IN/OUT: Ou
41272 74 70 75 74 20 70 6f 69 6e 74 65 72 20 2a 2f 0a  tput pointer */.
41273 20 20 69 6e 74 20 62 44 65 73 63 49 64 78 2c 20    int bDescIdx, 
41274 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41275 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 64 65    /* True for de
41276 73 63 65 6e 64 69 6e 67 20 64 6f 63 69 64 73 20  scending docids 
41277 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
41278 36 34 20 2a 70 69 50 72 65 76 2c 20 20 20 20 20  64 *piPrev,     
41279 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
4127a 50 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 77  Previous value w
4127b 72 69 74 74 65 6e 20 74 6f 20 6c 69 73 74 20 2a  ritten to list *
4127c 2f 0a 20 20 69 6e 74 20 2a 70 62 46 69 72 73 74  /.  int *pbFirst
4127d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
4127e 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 54      /* IN/OUT: T
4127f 72 75 65 20 61 66 74 65 72 20 66 69 72 73 74 20  rue after first 
41280 69 6e 74 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20  int written */. 
41281 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
41282 56 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  Val             
41283 20 2f 2a 20 57 72 69 74 65 20 74 68 69 73 20 76   /* Write this v
41284 61 6c 75 65 20 74 6f 20 74 68 65 20 6c 69 73 74  alue to the list
41285 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
41286 5f 69 6e 74 36 34 20 69 57 72 69 74 65 3b 0a 20  _int64 iWrite;. 
41287 20 69 66 28 20 62 44 65 73 63 49 64 78 3d 3d 30   if( bDescIdx==0
41288 20 7c 7c 20 2a 70 62 46 69 72 73 74 3d 3d 30 20   || *pbFirst==0 
41289 29 7b 0a 20 20 20 20 69 57 72 69 74 65 20 3d 20  ){.    iWrite = 
4128a 69 56 61 6c 20 2d 20 2a 70 69 50 72 65 76 3b 0a  iVal - *piPrev;.
4128b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 57 72    }else{.    iWr
4128c 69 74 65 20 3d 20 2a 70 69 50 72 65 76 20 2d 20  ite = *piPrev - 
4128d 69 56 61 6c 3b 0a 20 20 7d 0a 20 20 61 73 73 65  iVal;.  }.  asse
4128e 72 74 28 20 2a 70 62 46 69 72 73 74 20 7c 7c 20  rt( *pbFirst || 
4128f 2a 70 69 50 72 65 76 3d 3d 30 20 29 3b 0a 20 20  *piPrev==0 );.  
41290 61 73 73 65 72 74 28 20 2a 70 62 46 69 72 73 74  assert( *pbFirst
41291 3d 3d 30 20 7c 7c 20 69 57 72 69 74 65 3e 30 20  ==0 || iWrite>0 
41292 29 3b 0a 20 20 2a 70 70 20 2b 3d 20 73 71 6c 69  );.  *pp += sqli
41293 74 65 33 46 74 73 33 50 75 74 56 61 72 69 6e 74  te3Fts3PutVarint
41294 28 2a 70 70 2c 20 69 57 72 69 74 65 29 3b 0a 20  (*pp, iWrite);. 
41295 20 2a 70 69 50 72 65 76 20 3d 20 69 56 61 6c 3b   *piPrev = iVal;
41296 0a 20 20 2a 70 62 46 69 72 73 74 20 3d 20 31 3b  .  *pbFirst = 1;
41297 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .}.../*.** This 
41298 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 62 79  macro is used by
41299 20 76 61 72 69 6f 75 73 20 66 75 6e 63 74 69 6f   various functio
4129a 6e 73 20 74 68 61 74 20 6d 65 72 67 65 20 64 6f  ns that merge do
4129b 63 6c 69 73 74 73 2e 20 54 68 65 20 74 77 6f 0a  clists. The two.
4129c 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65  ** arguments are
4129d 20 36 34 2d 62 69 74 20 64 6f 63 69 64 20 76 61   64-bit docid va
4129e 6c 75 65 73 2e 20 49 66 20 74 68 65 20 76 61 6c  lues. If the val
4129f 75 65 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20  ue of the stack 
412a0 76 61 72 69 61 62 6c 65 0a 2a 2a 20 62 44 65 73  variable.** bDes
412a1 63 44 6f 63 6c 69 73 74 20 69 73 20 30 20 77 68  cDoclist is 0 wh
412a2 65 6e 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73  en this macro is
412a3 20 69 6e 76 6f 6b 65 64 2c 20 74 68 65 6e 20 69   invoked, then i
412a4 74 20 72 65 74 75 72 6e 73 20 28 69 31 2d 69 32  t returns (i1-i2
412a5 29 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  ). .** Otherwise
412a6 2c 20 28 69 32 2d 69 31 29 2e 0a 2a 2a 0a 2a 2a  , (i2-i1)..**.**
412a7 20 55 73 69 6e 67 20 74 68 69 73 20 6d 61 6b 65   Using this make
412a8 73 20 69 74 20 65 61 73 69 65 72 20 74 6f 20 77  s it easier to w
412a9 72 69 74 65 20 63 6f 64 65 20 74 68 61 74 20 63  rite code that c
412aa 61 6e 20 6d 65 72 67 65 20 64 6f 63 6c 69 73 74  an merge doclist
412ab 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 73 6f  s that are.** so
412ac 72 74 65 64 20 69 6e 20 65 69 74 68 65 72 20 61  rted in either a
412ad 73 63 65 6e 64 69 6e 67 20 6f 72 20 64 65 73 63  scending or desc
412ae 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f  ending order..*/
412af 0a 23 64 65 66 69 6e 65 20 44 4f 43 49 44 5f 43  .#define DOCID_C
412b0 4d 50 28 69 31 2c 20 69 32 29 20 28 28 62 44 65  MP(i1, i2) ((bDe
412b1 73 63 44 6f 63 6c 69 73 74 3f 2d 31 3a 31 29 20  scDoclist?-1:1) 
412b2 2a 20 28 69 31 2d 69 32 29 29 0a 0a 2f 2a 0a 2a  * (i1-i2))../*.*
412b3 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
412b4 64 6f 65 73 20 61 6e 20 22 4f 52 22 20 6d 65 72  does an "OR" mer
412b5 67 65 20 6f 66 20 74 77 6f 20 64 6f 63 6c 69 73  ge of two doclis
412b6 74 73 20 28 6f 75 74 70 75 74 20 63 6f 6e 74 61  ts (output conta
412b7 69 6e 73 20 61 6c 6c 0a 2a 2a 20 70 6f 73 69 74  ins all.** posit
412b8 69 6f 6e 73 20 63 6f 6e 74 61 69 6e 65 64 20 69  ions contained i
412b9 6e 20 65 69 74 68 65 72 20 61 72 67 75 6d 65 6e  n either argumen
412ba 74 20 64 6f 63 6c 69 73 74 29 2e 20 49 66 20 74  t doclist). If t
412bb 68 65 20 64 6f 63 69 64 73 20 69 6e 20 74 68 65  he docids in the
412bc 20 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 63 6c 69   .** input docli
412bd 73 74 73 20 61 72 65 20 73 6f 72 74 65 64 20 69  sts are sorted i
412be 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  n ascending orde
412bf 72 2c 20 70 61 72 61 6d 65 74 65 72 20 62 44 65  r, parameter bDe
412c0 73 63 44 6f 63 6c 69 73 74 0a 2a 2a 20 73 68 6f  scDoclist.** sho
412c1 75 6c 64 20 62 65 20 66 61 6c 73 65 2e 20 49 66  uld be false. If
412c2 20 74 68 65 79 20 61 72 65 20 73 6f 72 74 65 64   they are sorted
412c3 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72   in ascending or
412c4 64 65 72 2c 20 69 74 20 73 68 6f 75 6c 64 20 62  der, it should b
412c5 65 0a 2a 2a 20 70 61 73 73 65 64 20 61 20 6e 6f  e.** passed a no
412c6 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a 2a  n-zero value..**
412c7 0a 2a 2a 20 49 66 20 6e 6f 20 65 72 72 6f 72 20  .** If no error 
412c8 6f 63 63 75 72 73 2c 20 2a 70 61 4f 75 74 20 69  occurs, *paOut i
412c9 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61  s set to point a
412ca 74 20 61 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c  t an sqlite3_mal
412cb 6c 6f 63 27 64 20 62 75 66 66 65 72 0a 2a 2a 20  loc'd buffer.** 
412cc 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f  containing the o
412cd 75 74 70 75 74 20 64 6f 63 6c 69 73 74 20 61 6e  utput doclist an
412ce 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  d SQLITE_OK is r
412cf 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73  eturned. In this
412d0 20 63 61 73 65 0a 2a 2a 20 2a 70 6e 4f 75 74 20   case.** *pnOut 
412d1 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75  is set to the nu
412d2 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
412d3 20 74 68 65 20 6f 75 74 70 75 74 20 64 6f 63 6c   the output docl
412d4 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ist..**.** If an
412d5 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61   error occurs, a
412d6 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
412d7 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
412d8 20 54 68 65 20 6f 75 74 70 75 74 20 76 61 6c 75   The output valu
412d9 65 73 0a 2a 2a 20 61 72 65 20 75 6e 64 65 66 69  es.** are undefi
412da 6e 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65  ned in this case
412db 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
412dc 66 74 73 33 44 6f 63 6c 69 73 74 4f 72 4d 65 72  fts3DoclistOrMer
412dd 67 65 28 0a 20 20 69 6e 74 20 62 44 65 73 63 44  ge(.  int bDescD
412de 6f 63 6c 69 73 74 2c 20 20 20 20 20 20 20 20 20  oclist,         
412df 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
412e0 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 64   arguments are d
412e1 65 73 63 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61  esc */.  char *a
412e2 31 2c 20 69 6e 74 20 6e 31 2c 20 20 20 20 20 20  1, int n1,      
412e3 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
412e4 74 20 64 6f 63 6c 69 73 74 20 2a 2f 0a 20 20 63  t doclist */.  c
412e5 68 61 72 20 2a 61 32 2c 20 69 6e 74 20 6e 32 2c  har *a2, int n2,
412e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
412e7 2a 20 53 65 63 6f 6e 64 20 64 6f 63 6c 69 73 74  * Second doclist
412e8 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 61 4f   */.  char **paO
412e9 75 74 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 20 20  ut, int *pnOut  
412ea 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61        /* OUT: Ma
412eb 6c 6c 6f 63 27 64 20 64 6f 63 6c 69 73 74 20 2a  lloc'd doclist *
412ec 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  /.){.  sqlite3_i
412ed 6e 74 36 34 20 69 31 20 3d 20 30 3b 0a 20 20 73  nt64 i1 = 0;.  s
412ee 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 32 20  qlite3_int64 i2 
412ef 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  = 0;.  sqlite3_i
412f0 6e 74 36 34 20 69 50 72 65 76 20 3d 20 30 3b 0a  nt64 iPrev = 0;.
412f1 20 20 63 68 61 72 20 2a 70 45 6e 64 31 20 3d 20    char *pEnd1 = 
412f2 26 61 31 5b 6e 31 5d 3b 0a 20 20 63 68 61 72 20  &a1[n1];.  char 
412f3 2a 70 45 6e 64 32 20 3d 20 26 61 32 5b 6e 32 5d  *pEnd2 = &a2[n2]
412f4 3b 0a 20 20 63 68 61 72 20 2a 70 31 20 3d 20 61  ;.  char *p1 = a
412f5 31 3b 0a 20 20 63 68 61 72 20 2a 70 32 20 3d 20  1;.  char *p2 = 
412f6 61 32 3b 0a 20 20 63 68 61 72 20 2a 70 3b 0a 20  a2;.  char *p;. 
412f7 20 63 68 61 72 20 2a 61 4f 75 74 3b 0a 20 20 69   char *aOut;.  i
412f8 6e 74 20 62 46 69 72 73 74 4f 75 74 20 3d 20 30  nt bFirstOut = 0
412f9 3b 0a 0a 20 20 2a 70 61 4f 75 74 20 3d 20 30 3b  ;..  *paOut = 0;
412fa 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 30 3b 0a 0a  .  *pnOut = 0;..
412fb 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70    /* Allocate sp
412fc 61 63 65 20 66 6f 72 20 74 68 65 20 6f 75 74 70  ace for the outp
412fd 75 74 2e 20 42 6f 74 68 20 74 68 65 20 69 6e 70  ut. Both the inp
412fe 75 74 20 61 6e 64 20 6f 75 74 70 75 74 20 64 6f  ut and output do
412ff 63 6c 69 73 74 73 0a 20 20 2a 2a 20 61 72 65 20  clists.  ** are 
41300 64 65 6c 74 61 20 65 6e 63 6f 64 65 64 2e 20 49  delta encoded. I
41301 66 20 74 68 65 79 20 61 72 65 20 69 6e 20 61 73  f they are in as
41302 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 28 62  cending order (b
41303 44 65 73 63 44 6f 63 6c 69 73 74 3d 3d 30 29 2c  DescDoclist==0),
41304 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 66  .  ** then the f
41305 69 72 73 74 20 64 6f 63 69 64 20 69 6e 20 65 61  irst docid in ea
41306 63 68 20 6c 69 73 74 20 69 73 20 73 69 6d 70 6c  ch list is simpl
41307 79 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 76  y encoded as a v
41308 61 72 69 6e 74 2e 20 46 6f 72 0a 20 20 2a 2a 20  arint. For.  ** 
41309 65 61 63 68 20 73 75 62 73 65 71 75 65 6e 74 20  each subsequent 
4130a 64 6f 63 69 64 2c 20 74 68 65 20 76 61 72 69 6e  docid, the varin
4130b 74 20 73 74 6f 72 65 64 20 69 73 20 74 68 65 20  t stored is the 
4130c 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65  difference betwe
4130d 65 6e 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 72  en the.  ** curr
4130e 65 6e 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73  ent and previous
4130f 20 64 6f 63 69 64 20 28 61 20 70 6f 73 69 74 69   docid (a positi
41310 76 65 20 6e 75 6d 62 65 72 20 2d 20 73 69 6e 63  ve number - sinc
41311 65 20 74 68 65 20 6c 69 73 74 20 69 73 20 69 6e  e the list is in
41312 0a 20 20 2a 2a 20 61 73 63 65 6e 64 69 6e 67 20  .  ** ascending 
41313 6f 72 64 65 72 29 2e 0a 20 20 2a 2a 0a 20 20 2a  order)..  **.  *
41314 2a 20 54 68 65 20 66 69 72 73 74 20 64 6f 63 69  * The first doci
41315 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  d written to the
41316 20 6f 75 74 70 75 74 20 69 73 20 74 68 65 72 65   output is there
41317 66 6f 72 65 20 65 6e 63 6f 64 65 64 20 75 73 69  fore encoded usi
41318 6e 67 20 74 68 65 20 0a 20 20 2a 2a 20 73 61 6d  ng the .  ** sam
41319 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
4131a 73 20 61 73 20 69 74 20 69 73 20 69 6e 20 77 68  s as it is in wh
4131b 69 63 68 65 76 65 72 20 6f 66 20 74 68 65 20 69  ichever of the i
4131c 6e 70 75 74 20 6c 69 73 74 73 20 69 74 20 69 73  nput lists it is
4131d 0a 20 20 2a 2a 20 72 65 61 64 20 66 72 6f 6d 2e  .  ** read from.
4131e 20 41 6e 64 20 65 61 63 68 20 73 75 62 73 65 71   And each subseq
4131f 75 65 6e 74 20 64 6f 63 69 64 20 72 65 61 64 20  uent docid read 
41320 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 69 6e  from the same in
41321 70 75 74 20 6c 69 73 74 20 0a 20 20 2a 2a 20 63  put list .  ** c
41322 6f 6e 73 75 6d 65 73 20 65 69 74 68 65 72 20 74  onsumes either t
41323 68 65 20 73 61 6d 65 20 6f 72 20 6c 65 73 73 20  he same or less 
41324 62 79 74 65 73 20 61 73 20 69 74 20 64 69 64 20  bytes as it did 
41325 69 6e 20 74 68 65 20 69 6e 70 75 74 20 28 73 69  in the input (si
41326 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 64 69 66  nce.  ** the dif
41327 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20  ference between 
41328 69 74 20 61 6e 64 20 74 68 65 20 70 72 65 76 69  it and the previ
41329 6f 75 73 20 76 61 6c 75 65 20 69 6e 20 74 68 65  ous value in the
4132a 20 6f 75 74 70 75 74 20 6d 75 73 74 0a 20 20 2a   output must.  *
4132b 2a 20 62 65 20 61 20 70 6f 73 69 74 69 76 65 20  * be a positive 
4132c 76 61 6c 75 65 20 6c 65 73 73 20 74 68 61 6e 20  value less than 
4132d 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
4132e 64 65 6c 74 61 20 76 61 6c 75 65 20 72 65 61 64  delta value read
4132f 20 66 72 6f 6d 20 0a 20 20 2a 2a 20 74 68 65 20   from .  ** the 
41330 69 6e 70 75 74 20 6c 69 73 74 29 2e 20 54 68 65  input list). The
41331 20 73 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 61   same argument a
41332 70 70 6c 69 65 73 20 74 6f 20 61 6c 6c 20 62 75  pplies to all bu
41333 74 20 74 68 65 20 66 69 72 73 74 20 64 6f 63 69  t the first doci
41334 64 0a 20 20 2a 2a 20 72 65 61 64 20 66 72 6f 6d  d.  ** read from
41335 20 74 68 65 20 27 6f 74 68 65 72 27 20 6c 69 73   the 'other' lis
41336 74 2e 20 41 6e 64 20 74 6f 20 74 68 65 20 63 6f  t. And to the co
41337 6e 74 65 6e 74 73 20 6f 66 20 61 6c 6c 20 70 6f  ntents of all po
41338 73 69 74 69 6f 6e 20 6c 69 73 74 73 0a 20 20 2a  sition lists.  *
41339 2a 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 63  * that will be c
4133a 6f 70 69 65 64 20 61 6e 64 20 6d 65 72 67 65 64  opied and merged
4133b 20 66 72 6f 6d 20 74 68 65 20 69 6e 70 75 74 20   from the input 
4133c 74 6f 20 74 68 65 20 6f 75 74 70 75 74 2e 0a 20  to the output.. 
4133d 20 2a 2a 0a 20 20 2a 2a 20 48 6f 77 65 76 65 72   **.  ** However
4133e 2c 20 69 66 20 74 68 65 20 66 69 72 73 74 20 64  , if the first d
4133f 6f 63 69 64 20 63 6f 70 69 65 64 20 74 6f 20 74  ocid copied to t
41340 68 65 20 6f 75 74 70 75 74 20 69 73 20 61 20 6e  he output is a n
41341 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2c 0a  egative number,.
41342 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 65 6e    ** then the en
41343 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 66 69  coding of the fi
41344 72 73 74 20 64 6f 63 69 64 20 66 72 6f 6d 20 74  rst docid from t
41345 68 65 20 27 6f 74 68 65 72 27 20 69 6e 70 75 74  he 'other' input
41346 20 6c 69 73 74 20 6d 61 79 0a 20 20 2a 2a 20 62   list may.  ** b
41347 65 20 6c 61 72 67 65 72 20 69 6e 20 74 68 65 20  e larger in the 
41348 6f 75 74 70 75 74 20 74 68 61 6e 20 69 74 20 77  output than it w
41349 61 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20  as in the input 
4134a 28 73 69 6e 63 65 20 74 68 65 20 64 65 6c 74 61  (since the delta
4134b 20 76 61 6c 75 65 0a 20 20 2a 2a 20 6d 61 79 20   value.  ** may 
4134c 62 65 20 61 20 6c 61 72 67 65 72 20 70 6f 73 69  be a larger posi
4134d 74 69 76 65 20 69 6e 74 65 67 65 72 20 74 68 61  tive integer tha
4134e 6e 20 74 68 65 20 61 63 74 75 61 6c 20 64 6f 63  n the actual doc
4134f 69 64 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  id)..  **.  ** T
41350 68 65 20 73 70 61 63 65 20 72 65 71 75 69 72 65  he space require
41351 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6f  d to store the o
41352 75 74 70 75 74 20 69 73 20 74 68 65 72 65 66 6f  utput is therefo
41353 72 65 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68  re the sum of th
41354 65 0a 20 20 2a 2a 20 73 69 7a 65 73 20 6f 66 20  e.  ** sizes of 
41355 74 68 65 20 74 77 6f 20 69 6e 70 75 74 73 2c 20  the two inputs, 
41356 70 6c 75 73 20 65 6e 6f 75 67 68 20 73 70 61 63  plus enough spac
41357 65 20 66 6f 72 20 65 78 61 63 74 6c 79 20 6f 6e  e for exactly on
41358 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74 0a 20  e of the input. 
41359 20 2a 2a 20 64 6f 63 69 64 73 20 74 6f 20 67 72   ** docids to gr
4135a 6f 77 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41  ow. .  **.  ** A
4135b 20 73 79 6d 65 74 72 69 63 20 61 72 67 75 6d 65   symetric argume
4135c 6e 74 20 6d 61 79 20 62 65 20 6d 61 64 65 20 69  nt may be made i
4135d 66 20 74 68 65 20 64 6f 63 6c 69 73 74 73 20 61  f the doclists a
4135e 72 65 20 69 6e 20 64 65 73 63 65 6e 64 69 6e 67  re in descending
4135f 20 0a 20 20 2a 2a 20 6f 72 64 65 72 2e 0a 20 20   .  ** order..  
41360 2a 2f 0a 20 20 61 4f 75 74 20 3d 20 73 71 6c 69  */.  aOut = sqli
41361 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 31 2b 6e 32  te3_malloc(n1+n2
41362 2b 46 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58  +FTS3_VARINT_MAX
41363 2d 31 29 3b 0a 20 20 69 66 28 20 21 61 4f 75 74  -1);.  if( !aOut
41364 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
41365 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 70 20 3d 20 61  _NOMEM;..  p = a
41366 4f 75 74 3b 0a 20 20 66 74 73 33 47 65 74 44 65  Out;.  fts3GetDe
41367 6c 74 61 56 61 72 69 6e 74 33 28 26 70 31 2c 20  ltaVarint3(&p1, 
41368 70 45 6e 64 31 2c 20 30 2c 20 26 69 31 29 3b 0a  pEnd1, 0, &i1);.
41369 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61    fts3GetDeltaVa
4136a 72 69 6e 74 33 28 26 70 32 2c 20 70 45 6e 64 32  rint3(&p2, pEnd2
4136b 2c 20 30 2c 20 26 69 32 29 3b 0a 20 20 77 68 69  , 0, &i2);.  whi
4136c 6c 65 28 20 70 31 20 7c 7c 20 70 32 20 29 7b 0a  le( p1 || p2 ){.
4136d 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
4136e 34 20 69 44 69 66 66 20 3d 20 44 4f 43 49 44 5f  4 iDiff = DOCID_
4136f 43 4d 50 28 69 31 2c 20 69 32 29 3b 0a 0a 20 20  CMP(i1, i2);..  
41370 20 20 69 66 28 20 70 32 20 26 26 20 70 31 20 26    if( p2 && p1 &
41371 26 20 69 44 69 66 66 3d 3d 30 20 29 7b 0a 20 20  & iDiff==0 ){.  
41372 20 20 20 20 66 74 73 33 50 75 74 44 65 6c 74 61      fts3PutDelta
41373 56 61 72 69 6e 74 33 28 26 70 2c 20 62 44 65 73  Varint3(&p, bDes
41374 63 44 6f 63 6c 69 73 74 2c 20 26 69 50 72 65 76  cDoclist, &iPrev
41375 2c 20 26 62 46 69 72 73 74 4f 75 74 2c 20 69 31  , &bFirstOut, i1
41376 29 3b 0a 20 20 20 20 20 20 66 74 73 33 50 6f 73  );.      fts3Pos
41377 6c 69 73 74 4d 65 72 67 65 28 26 70 2c 20 26 70  listMerge(&p, &p
41378 31 2c 20 26 70 32 29 3b 0a 20 20 20 20 20 20 66  1, &p2);.      f
41379 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e  ts3GetDeltaVarin
4137a 74 33 28 26 70 31 2c 20 70 45 6e 64 31 2c 20 62  t3(&p1, pEnd1, b
4137b 44 65 73 63 44 6f 63 6c 69 73 74 2c 20 26 69 31  DescDoclist, &i1
4137c 29 3b 0a 20 20 20 20 20 20 66 74 73 33 47 65 74  );.      fts3Get
4137d 44 65 6c 74 61 56 61 72 69 6e 74 33 28 26 70 32  DeltaVarint3(&p2
4137e 2c 20 70 45 6e 64 32 2c 20 62 44 65 73 63 44 6f  , pEnd2, bDescDo
4137f 63 6c 69 73 74 2c 20 26 69 32 29 3b 0a 20 20 20  clist, &i2);.   
41380 20 7d 65 6c 73 65 20 69 66 28 20 21 70 32 20 7c   }else if( !p2 |
41381 7c 20 28 70 31 20 26 26 20 69 44 69 66 66 3c 30  | (p1 && iDiff<0
41382 29 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33 50  ) ){.      fts3P
41383 75 74 44 65 6c 74 61 56 61 72 69 6e 74 33 28 26  utDeltaVarint3(&
41384 70 2c 20 62 44 65 73 63 44 6f 63 6c 69 73 74 2c  p, bDescDoclist,
41385 20 26 69 50 72 65 76 2c 20 26 62 46 69 72 73 74   &iPrev, &bFirst
41386 4f 75 74 2c 20 69 31 29 3b 0a 20 20 20 20 20 20  Out, i1);.      
41387 66 74 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28  fts3PoslistCopy(
41388 26 70 2c 20 26 70 31 29 3b 0a 20 20 20 20 20 20  &p, &p1);.      
41389 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69  fts3GetDeltaVari
4138a 6e 74 33 28 26 70 31 2c 20 70 45 6e 64 31 2c 20  nt3(&p1, pEnd1, 
4138b 62 44 65 73 63 44 6f 63 6c 69 73 74 2c 20 26 69  bDescDoclist, &i
4138c 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  1);.    }else{. 
4138d 20 20 20 20 20 66 74 73 33 50 75 74 44 65 6c 74       fts3PutDelt
4138e 61 56 61 72 69 6e 74 33 28 26 70 2c 20 62 44 65  aVarint3(&p, bDe
4138f 73 63 44 6f 63 6c 69 73 74 2c 20 26 69 50 72 65  scDoclist, &iPre
41390 76 2c 20 26 62 46 69 72 73 74 4f 75 74 2c 20 69  v, &bFirstOut, i
41391 32 29 3b 0a 20 20 20 20 20 20 66 74 73 33 50 6f  2);.      fts3Po
41392 73 6c 69 73 74 43 6f 70 79 28 26 70 2c 20 26 70  slistCopy(&p, &p
41393 32 29 3b 0a 20 20 20 20 20 20 66 74 73 33 47 65  2);.      fts3Ge
41394 74 44 65 6c 74 61 56 61 72 69 6e 74 33 28 26 70  tDeltaVarint3(&p
41395 32 2c 20 70 45 6e 64 32 2c 20 62 44 65 73 63 44  2, pEnd2, bDescD
41396 6f 63 6c 69 73 74 2c 20 26 69 32 29 3b 0a 20 20  oclist, &i2);.  
41397 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 61 4f 75    }.  }..  *paOu
41398 74 20 3d 20 61 4f 75 74 3b 0a 20 20 2a 70 6e 4f  t = aOut;.  *pnO
41399 75 74 20 3d 20 28 69 6e 74 29 28 70 2d 61 4f 75  ut = (int)(p-aOu
4139a 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 2a 70  t);.  assert( *p
4139b 6e 4f 75 74 3c 3d 6e 31 2b 6e 32 2b 46 54 53 33  nOut<=n1+n2+FTS3
4139c 5f 56 41 52 49 4e 54 5f 4d 41 58 2d 31 20 29 3b  _VARINT_MAX-1 );
4139d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
4139e 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
4139f 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  is function does
413a0 20 61 20 22 70 68 72 61 73 65 22 20 6d 65 72 67   a "phrase" merg
413a1 65 20 6f 66 20 74 77 6f 20 64 6f 63 6c 69 73 74  e of two doclist
413a2 73 2e 20 49 6e 20 61 20 70 68 72 61 73 65 20 6d  s. In a phrase m
413a3 65 72 67 65 2c 0a 2a 2a 20 74 68 65 20 6f 75 74  erge,.** the out
413a4 70 75 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 63  put contains a c
413a5 6f 70 79 20 6f 66 20 65 61 63 68 20 70 6f 73 69  opy of each posi
413a6 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 72 69  tion from the ri
413a7 67 68 74 2d 68 61 6e 64 20 69 6e 70 75 74 0a 2a  ght-hand input.*
413a8 2a 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 77 68  * doclist for wh
413a9 69 63 68 20 74 68 65 72 65 20 69 73 20 61 20 70  ich there is a p
413aa 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 6c  osition in the l
413ab 65 66 74 2d 68 61 6e 64 20 69 6e 70 75 74 20 64  eft-hand input d
413ac 6f 63 6c 69 73 74 0a 2a 2a 20 65 78 61 63 74 6c  oclist.** exactl
413ad 79 20 6e 44 69 73 74 20 74 6f 6b 65 6e 73 20 62  y nDist tokens b
413ae 65 66 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20  efore it..**.** 
413af 49 66 20 74 68 65 20 64 6f 63 69 64 73 20 69 6e  If the docids in
413b0 20 74 68 65 20 69 6e 70 75 74 20 64 6f 63 6c 69   the input docli
413b1 73 74 73 20 61 72 65 20 73 6f 72 74 65 64 20 69  sts are sorted i
413b2 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  n ascending orde
413b3 72 2c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  r,.** parameter 
413b4 62 44 65 73 63 44 6f 63 6c 69 73 74 20 73 68 6f  bDescDoclist sho
413b5 75 6c 64 20 62 65 20 66 61 6c 73 65 2e 20 49 66  uld be false. If
413b6 20 74 68 65 79 20 61 72 65 20 73 6f 72 74 65 64   they are sorted
413b7 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 0a 2a   in ascending .*
413b8 2a 20 6f 72 64 65 72 2c 20 69 74 20 73 68 6f 75  * order, it shou
413b9 6c 64 20 62 65 20 70 61 73 73 65 64 20 61 20 6e  ld be passed a n
413ba 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a  on-zero value..*
413bb 2a 0a 2a 2a 20 54 68 65 20 72 69 67 68 74 2d 68  *.** The right-h
413bc 61 6e 64 20 69 6e 70 75 74 20 64 6f 63 6c 69 73  and input doclis
413bd 74 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e  t is overwritten
413be 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
413bf 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
413c0 64 20 66 74 73 33 44 6f 63 6c 69 73 74 50 68 72  d fts3DoclistPhr
413c1 61 73 65 4d 65 72 67 65 28 0a 20 20 69 6e 74 20  aseMerge(.  int 
413c2 62 44 65 73 63 44 6f 63 6c 69 73 74 2c 20 20 20  bDescDoclist,   
413c3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
413c4 72 75 65 20 69 66 20 61 72 67 75 6d 65 6e 74 73  rue if arguments
413c5 20 61 72 65 20 64 65 73 63 20 2a 2f 0a 20 20 69   are desc */.  i
413c6 6e 74 20 6e 44 69 73 74 2c 20 20 20 20 20 20 20  nt nDist,       
413c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
413c8 2a 20 44 69 73 74 61 6e 63 65 20 66 72 6f 6d 20  * Distance from 
413c9 6c 65 66 74 20 74 6f 20 72 69 67 68 74 20 28 31  left to right (1
413ca 3d 61 64 6a 61 63 65 6e 74 29 20 2a 2f 0a 20 20  =adjacent) */.  
413cb 63 68 61 72 20 2a 61 4c 65 66 74 2c 20 69 6e 74  char *aLeft, int
413cc 20 6e 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20   nLeft,         
413cd 2f 2a 20 4c 65 66 74 20 64 6f 63 6c 69 73 74 20  /* Left doclist 
413ce 2a 2f 0a 20 20 63 68 61 72 20 2a 61 52 69 67 68  */.  char *aRigh
413cf 74 2c 20 69 6e 74 20 2a 70 6e 52 69 67 68 74 20  t, int *pnRight 
413d0 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
413d1 52 69 67 68 74 2f 6f 75 74 70 75 74 20 64 6f 63  Right/output doc
413d2 6c 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  list */.){.  sql
413d3 69 74 65 33 5f 69 6e 74 36 34 20 69 31 20 3d 20  ite3_int64 i1 = 
413d4 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  0;.  sqlite3_int
413d5 36 34 20 69 32 20 3d 20 30 3b 0a 20 20 73 71 6c  64 i2 = 0;.  sql
413d6 69 74 65 33 5f 69 6e 74 36 34 20 69 50 72 65 76  ite3_int64 iPrev
413d7 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 70 45   = 0;.  char *pE
413d8 6e 64 31 20 3d 20 26 61 4c 65 66 74 5b 6e 4c 65  nd1 = &aLeft[nLe
413d9 66 74 5d 3b 0a 20 20 63 68 61 72 20 2a 70 45 6e  ft];.  char *pEn
413da 64 32 20 3d 20 26 61 52 69 67 68 74 5b 2a 70 6e  d2 = &aRight[*pn
413db 52 69 67 68 74 5d 3b 0a 20 20 63 68 61 72 20 2a  Right];.  char *
413dc 70 31 20 3d 20 61 4c 65 66 74 3b 0a 20 20 63 68  p1 = aLeft;.  ch
413dd 61 72 20 2a 70 32 20 3d 20 61 52 69 67 68 74 3b  ar *p2 = aRight;
413de 0a 20 20 63 68 61 72 20 2a 70 3b 0a 20 20 69 6e  .  char *p;.  in
413df 74 20 62 46 69 72 73 74 4f 75 74 20 3d 20 30 3b  t bFirstOut = 0;
413e0 0a 20 20 63 68 61 72 20 2a 61 4f 75 74 20 3d 20  .  char *aOut = 
413e1 61 52 69 67 68 74 3b 0a 0a 20 20 61 73 73 65 72  aRight;..  asser
413e2 74 28 20 6e 44 69 73 74 3e 30 20 29 3b 0a 0a 20  t( nDist>0 );.. 
413e3 20 70 20 3d 20 61 4f 75 74 3b 0a 20 20 66 74 73   p = aOut;.  fts
413e4 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 33  3GetDeltaVarint3
413e5 28 26 70 31 2c 20 70 45 6e 64 31 2c 20 30 2c 20  (&p1, pEnd1, 0, 
413e6 26 69 31 29 3b 0a 20 20 66 74 73 33 47 65 74 44  &i1);.  fts3GetD
413e7 65 6c 74 61 56 61 72 69 6e 74 33 28 26 70 32 2c  eltaVarint3(&p2,
413e8 20 70 45 6e 64 32 2c 20 30 2c 20 26 69 32 29 3b   pEnd2, 0, &i2);
413e9 0a 0a 20 20 77 68 69 6c 65 28 20 70 31 20 26 26  ..  while( p1 &&
413ea 20 70 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74   p2 ){.    sqlit
413eb 65 33 5f 69 6e 74 36 34 20 69 44 69 66 66 20 3d  e3_int64 iDiff =
413ec 20 44 4f 43 49 44 5f 43 4d 50 28 69 31 2c 20 69   DOCID_CMP(i1, i
413ed 32 29 3b 0a 20 20 20 20 69 66 28 20 69 44 69 66  2);.    if( iDif
413ee 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 68  f==0 ){.      ch
413ef 61 72 20 2a 70 53 61 76 65 20 3d 20 70 3b 0a 20  ar *pSave = p;. 
413f0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
413f1 36 34 20 69 50 72 65 76 53 61 76 65 20 3d 20 69  64 iPrevSave = i
413f2 50 72 65 76 3b 0a 20 20 20 20 20 20 69 6e 74 20  Prev;.      int 
413f3 62 46 69 72 73 74 4f 75 74 53 61 76 65 20 3d 20  bFirstOutSave = 
413f4 62 46 69 72 73 74 4f 75 74 3b 0a 0a 20 20 20 20  bFirstOut;..    
413f5 20 20 66 74 73 33 50 75 74 44 65 6c 74 61 56 61    fts3PutDeltaVa
413f6 72 69 6e 74 33 28 26 70 2c 20 62 44 65 73 63 44  rint3(&p, bDescD
413f7 6f 63 6c 69 73 74 2c 20 26 69 50 72 65 76 2c 20  oclist, &iPrev, 
413f8 26 62 46 69 72 73 74 4f 75 74 2c 20 69 31 29 3b  &bFirstOut, i1);
413f9 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 66 74  .      if( 0==ft
413fa 73 33 50 6f 73 6c 69 73 74 50 68 72 61 73 65 4d  s3PoslistPhraseM
413fb 65 72 67 65 28 26 70 2c 20 6e 44 69 73 74 2c 20  erge(&p, nDist, 
413fc 30 2c 20 31 2c 20 26 70 31 2c 20 26 70 32 29 20  0, 1, &p1, &p2) 
413fd 29 7b 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70  ){.        p = p
413fe 53 61 76 65 3b 0a 20 20 20 20 20 20 20 20 69 50  Save;.        iP
413ff 72 65 76 20 3d 20 69 50 72 65 76 53 61 76 65 3b  rev = iPrevSave;
41400 0a 20 20 20 20 20 20 20 20 62 46 69 72 73 74 4f  .        bFirstO
41401 75 74 20 3d 20 62 46 69 72 73 74 4f 75 74 53 61  ut = bFirstOutSa
41402 76 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ve;.      }.    
41403 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61    fts3GetDeltaVa
41404 72 69 6e 74 33 28 26 70 31 2c 20 70 45 6e 64 31  rint3(&p1, pEnd1
41405 2c 20 62 44 65 73 63 44 6f 63 6c 69 73 74 2c 20  , bDescDoclist, 
41406 26 69 31 29 3b 0a 20 20 20 20 20 20 66 74 73 33  &i1);.      fts3
41407 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 33 28  GetDeltaVarint3(
41408 26 70 32 2c 20 70 45 6e 64 32 2c 20 62 44 65 73  &p2, pEnd2, bDes
41409 63 44 6f 63 6c 69 73 74 2c 20 26 69 32 29 3b 0a  cDoclist, &i2);.
4140a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 44      }else if( iD
4140b 69 66 66 3c 30 20 29 7b 0a 20 20 20 20 20 20 66  iff<0 ){.      f
4140c 74 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 30  ts3PoslistCopy(0
4140d 2c 20 26 70 31 29 3b 0a 20 20 20 20 20 20 66 74  , &p1);.      ft
4140e 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74  s3GetDeltaVarint
4140f 33 28 26 70 31 2c 20 70 45 6e 64 31 2c 20 62 44  3(&p1, pEnd1, bD
41410 65 73 63 44 6f 63 6c 69 73 74 2c 20 26 69 31 29  escDoclist, &i1)
41411 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
41412 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74 43 6f     fts3PoslistCo
41413 70 79 28 30 2c 20 26 70 32 29 3b 0a 20 20 20 20  py(0, &p2);.    
41414 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61    fts3GetDeltaVa
41415 72 69 6e 74 33 28 26 70 32 2c 20 70 45 6e 64 32  rint3(&p2, pEnd2
41416 2c 20 62 44 65 73 63 44 6f 63 6c 69 73 74 2c 20  , bDescDoclist, 
41417 26 69 32 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  &i2);.    }.  }.
41418 0a 20 20 2a 70 6e 52 69 67 68 74 20 3d 20 28 69  .  *pnRight = (i
41419 6e 74 29 28 70 20 2d 20 61 4f 75 74 29 3b 0a 7d  nt)(p - aOut);.}
4141a 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ../*.** Argument
4141b 20 70 4c 69 73 74 20 70 6f 69 6e 74 73 20 74 6f   pList points to
4141c 20 61 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74   a position list
4141d 20 6e 4c 69 73 74 20 62 79 74 65 73 20 69 6e 20   nList bytes in 
4141e 73 69 7a 65 2e 20 54 68 69 73 0a 2a 2a 20 66 75  size. This.** fu
4141f 6e 63 74 69 6f 6e 20 63 68 65 63 6b 73 20 74 6f  nction checks to
41420 20 73 65 65 20 69 66 20 74 68 65 20 70 6f 73 69   see if the posi
41421 74 69 6f 6e 20 6c 69 73 74 20 63 6f 6e 74 61 69  tion list contai
41422 6e 73 20 61 6e 79 20 65 6e 74 72 69 65 73 20 66  ns any entries f
41423 6f 72 0a 2a 2a 20 61 20 74 6f 6b 65 6e 20 69 6e  or.** a token in
41424 20 70 6f 73 69 74 69 6f 6e 20 30 20 28 6f 66 20   position 0 (of 
41425 61 6e 79 20 63 6f 6c 75 6d 6e 29 2e 20 49 66 20  any column). If 
41426 73 6f 2c 20 69 74 20 77 72 69 74 65 73 20 61 72  so, it writes ar
41427 67 75 6d 65 6e 74 20 69 44 65 6c 74 61 0a 2a 2a  gument iDelta.**
41428 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 62   to the output b
41429 75 66 66 65 72 20 70 4f 75 74 2c 20 66 6f 6c 6c  uffer pOut, foll
4142a 6f 77 65 64 20 62 79 20 61 20 70 6f 73 69 74 69  owed by a positi
4142b 6f 6e 20 6c 69 73 74 20 63 6f 6e 73 69 73 74 69  on list consisti
4142c 6e 67 20 6f 6e 6c 79 0a 2a 2a 20 6f 66 20 74 68  ng only.** of th
4142d 65 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 70  e entries from p
4142e 4c 69 73 74 20 61 74 20 70 6f 73 69 74 69 6f 6e  List at position
4142f 20 30 2c 20 61 6e 64 20 74 65 72 6d 69 6e 61 74   0, and terminat
41430 65 64 20 62 79 20 61 6e 20 30 78 30 30 20 62 79  ed by an 0x00 by
41431 74 65 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  te..** The value
41432 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
41433 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
41434 20 77 72 69 74 74 65 6e 20 74 6f 20 70 4f 75 74   written to pOut
41435 20 28 69 66 20 61 6e 79 29 2e 0a 2a 2f 0a 53 51   (if any)..*/.SQ
41436 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
41437 20 73 71 6c 69 74 65 33 46 74 73 33 46 69 72 73   sqlite3Fts3Firs
41438 74 46 69 6c 74 65 72 28 0a 20 20 73 71 6c 69 74  tFilter(.  sqlit
41439 65 33 5f 69 6e 74 36 34 20 69 44 65 6c 74 61 2c  e3_int64 iDelta,
4143a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
4143b 72 69 6e 74 20 74 68 61 74 20 6d 61 79 20 62 65  rint that may be
4143c 20 77 72 69 74 74 65 6e 20 74 6f 20 70 4f 75 74   written to pOut
4143d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 4c 69 73   */.  char *pLis
4143e 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
4143f 20 20 20 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f        /* Positio
41440 6e 20 6c 69 73 74 20 28 6e 6f 20 30 78 30 30 20  n list (no 0x00 
41441 74 65 72 6d 29 20 2a 2f 0a 20 20 69 6e 74 20 6e  term) */.  int n
41442 4c 69 73 74 2c 20 20 20 20 20 20 20 20 20 20 20  List,           
41443 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
41444 7a 65 20 6f 66 20 70 4c 69 73 74 20 69 6e 20 62  ze of pList in b
41445 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ytes */.  char *
41446 70 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 20  pOut            
41447 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
41448 74 65 20 6f 75 74 70 75 74 20 68 65 72 65 20 2a  te output here *
41449 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 4f 75 74 20  /.){.  int nOut 
4144a 3d 20 30 3b 0a 20 20 69 6e 74 20 62 57 72 69 74  = 0;.  int bWrit
4144b 74 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ten = 0;        
4144c 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f         /* True o
4144d 6e 63 65 20 69 44 65 6c 74 61 20 68 61 73 20 62  nce iDelta has b
4144e 65 65 6e 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20  een written */. 
4144f 20 63 68 61 72 20 2a 70 20 3d 20 70 4c 69 73 74   char *p = pList
41450 3b 0a 20 20 63 68 61 72 20 2a 70 45 6e 64 20 3d  ;.  char *pEnd =
41451 20 26 70 4c 69 73 74 5b 6e 4c 69 73 74 5d 3b 0a   &pList[nList];.
41452 0a 20 20 69 66 28 20 2a 70 21 3d 30 78 30 31 20  .  if( *p!=0x01 
41453 29 7b 0a 20 20 20 20 69 66 28 20 2a 70 3d 3d 30  ){.    if( *p==0
41454 78 30 32 20 29 7b 0a 20 20 20 20 20 20 6e 4f 75  x02 ){.      nOu
41455 74 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33  t += sqlite3Fts3
41456 50 75 74 56 61 72 69 6e 74 28 26 70 4f 75 74 5b  PutVarint(&pOut[
41457 6e 4f 75 74 5d 2c 20 69 44 65 6c 74 61 29 3b 0a  nOut], iDelta);.
41458 20 20 20 20 20 20 70 4f 75 74 5b 6e 4f 75 74 2b        pOut[nOut+
41459 2b 5d 20 3d 20 30 78 30 32 3b 0a 20 20 20 20 20  +] = 0x02;.     
4145a 20 62 57 72 69 74 74 65 6e 20 3d 20 31 3b 0a 20   bWritten = 1;. 
4145b 20 20 20 7d 0a 20 20 20 20 66 74 73 33 43 6f 6c     }.    fts3Col
4145c 75 6d 6e 6c 69 73 74 43 6f 70 79 28 30 2c 20 26  umnlistCopy(0, &
4145d 70 29 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65  p);.  }..  while
4145e 28 20 70 3c 70 45 6e 64 20 26 26 20 2a 70 3d 3d  ( p<pEnd && *p==
4145f 30 78 30 31 20 29 7b 0a 20 20 20 20 73 71 6c 69  0x01 ){.    sqli
41460 74 65 33 5f 69 6e 74 36 34 20 69 43 6f 6c 3b 0a  te3_int64 iCol;.
41461 20 20 20 20 70 2b 2b 3b 0a 20 20 20 20 70 20 2b      p++;.    p +
41462 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  = sqlite3Fts3Get
41463 56 61 72 69 6e 74 28 70 2c 20 26 69 43 6f 6c 29  Varint(p, &iCol)
41464 3b 0a 20 20 20 20 69 66 28 20 2a 70 3d 3d 30 78  ;.    if( *p==0x
41465 30 32 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  02 ){.      if( 
41466 62 57 72 69 74 74 65 6e 3d 3d 30 20 29 7b 0a 20  bWritten==0 ){. 
41467 20 20 20 20 20 20 20 6e 4f 75 74 20 2b 3d 20 73         nOut += s
41468 71 6c 69 74 65 33 46 74 73 33 50 75 74 56 61 72  qlite3Fts3PutVar
41469 69 6e 74 28 26 70 4f 75 74 5b 6e 4f 75 74 5d 2c  int(&pOut[nOut],
4146a 20 69 44 65 6c 74 61 29 3b 0a 20 20 20 20 20 20   iDelta);.      
4146b 20 20 62 57 72 69 74 74 65 6e 20 3d 20 31 3b 0a    bWritten = 1;.
4146c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f        }.      pO
4146d 75 74 5b 6e 4f 75 74 2b 2b 5d 20 3d 20 30 78 30  ut[nOut++] = 0x0
4146e 31 3b 0a 20 20 20 20 20 20 6e 4f 75 74 20 2b 3d  1;.      nOut +=
4146f 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74 56   sqlite3Fts3PutV
41470 61 72 69 6e 74 28 26 70 4f 75 74 5b 6e 4f 75 74  arint(&pOut[nOut
41471 5d 2c 20 69 43 6f 6c 29 3b 0a 20 20 20 20 20 20  ], iCol);.      
41472 70 4f 75 74 5b 6e 4f 75 74 2b 2b 5d 20 3d 20 30  pOut[nOut++] = 0
41473 78 30 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  x02;.    }.    f
41474 74 73 33 43 6f 6c 75 6d 6e 6c 69 73 74 43 6f 70  ts3ColumnlistCop
41475 79 28 30 2c 20 26 70 29 3b 0a 20 20 7d 0a 20 20  y(0, &p);.  }.  
41476 69 66 28 20 62 57 72 69 74 74 65 6e 20 29 7b 0a  if( bWritten ){.
41477 20 20 20 20 70 4f 75 74 5b 6e 4f 75 74 2b 2b 5d      pOut[nOut++]
41478 20 3d 20 30 78 30 30 3b 0a 20 20 7d 0a 0a 20 20   = 0x00;.  }..  
41479 72 65 74 75 72 6e 20 6e 4f 75 74 3b 0a 7d 0a 0a  return nOut;.}..
4147a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 61 6c 6c  ./*.** Merge all
4147b 20 64 6f 63 6c 69 73 74 73 20 69 6e 20 74 68 65   doclists in the
4147c 20 54 65 72 6d 53 65 6c 65 63 74 2e 61 61 4f 75   TermSelect.aaOu
4147d 74 70 75 74 5b 5d 20 61 72 72 61 79 20 69 6e 74  tput[] array int
4147e 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 64 6f  o a single.** do
4147f 63 6c 69 73 74 20 73 74 6f 72 65 64 20 69 6e 20  clist stored in 
41480 54 65 72 6d 53 65 6c 65 63 74 2e 61 61 4f 75 74  TermSelect.aaOut
41481 70 75 74 5b 30 5d 2e 20 49 66 20 73 75 63 63 65  put[0]. If succe
41482 73 73 66 75 6c 2c 20 64 65 6c 65 74 65 20 61 6c  ssful, delete al
41483 6c 0a 2a 2a 20 6f 74 68 65 72 20 64 6f 63 6c 69  l.** other docli
41484 73 74 73 20 28 65 78 63 65 70 74 20 74 68 65 20  sts (except the 
41485 61 61 4f 75 74 70 75 74 5b 30 5d 20 6f 6e 65 29  aaOutput[0] one)
41486 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49   and return SQLI
41487 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  TE_OK..**.** If 
41488 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63  an OOM error occ
41489 75 72 73 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  urs, return SQLI
4148a 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68 69  TE_NOMEM. In thi
4148b 73 20 63 61 73 65 20 69 74 20 69 73 0a 2a 2a 20  s case it is.** 
4148c 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  the responsibili
4148d 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72  ty of the caller
4148e 20 74 6f 20 66 72 65 65 20 61 6e 79 20 64 6f 63   to free any doc
4148f 6c 69 73 74 73 20 6c 65 66 74 20 69 6e 20 74 68  lists left in th
41490 65 0a 2a 2a 20 54 65 72 6d 53 65 6c 65 63 74 2e  e.** TermSelect.
41491 61 61 4f 75 74 70 75 74 5b 5d 20 61 72 72 61 79  aaOutput[] array
41492 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
41493 66 74 73 33 54 65 72 6d 53 65 6c 65 63 74 46 69  fts3TermSelectFi
41494 6e 69 73 68 4d 65 72 67 65 28 46 74 73 33 54 61  nishMerge(Fts3Ta
41495 62 6c 65 20 2a 70 2c 20 54 65 72 6d 53 65 6c 65  ble *p, TermSele
41496 63 74 20 2a 70 54 53 29 7b 0a 20 20 63 68 61 72  ct *pTS){.  char
41497 20 2a 61 4f 75 74 20 3d 20 30 3b 0a 20 20 69 6e   *aOut = 0;.  in
41498 74 20 6e 4f 75 74 20 3d 20 30 3b 0a 20 20 69 6e  t nOut = 0;.  in
41499 74 20 69 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20  t i;..  /* Loop 
4149a 74 68 72 6f 75 67 68 20 74 68 65 20 64 6f 63 6c  through the docl
4149b 69 73 74 73 20 69 6e 20 74 68 65 20 61 61 4f 75  ists in the aaOu
4149c 74 70 75 74 5b 5d 20 61 72 72 61 79 2e 20 4d 65  tput[] array. Me
4149d 72 67 65 20 74 68 65 6d 20 61 6c 6c 0a 20 20 2a  rge them all.  *
4149e 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  * into a single 
4149f 64 6f 63 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20  doclist..  */.  
414a0 66 6f 72 28 69 3d 30 3b 20 69 3c 53 69 7a 65 6f  for(i=0; i<Sizeo
414a1 66 41 72 72 61 79 28 70 54 53 2d 3e 61 61 4f 75  fArray(pTS->aaOu
414a2 74 70 75 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  tput); i++){.   
414a3 20 69 66 28 20 70 54 53 2d 3e 61 61 4f 75 74 70   if( pTS->aaOutp
414a4 75 74 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 69  ut[i] ){.      i
414a5 66 28 20 21 61 4f 75 74 20 29 7b 0a 20 20 20 20  f( !aOut ){.    
414a6 20 20 20 20 61 4f 75 74 20 3d 20 70 54 53 2d 3e      aOut = pTS->
414a7 61 61 4f 75 74 70 75 74 5b 69 5d 3b 0a 20 20 20  aaOutput[i];.   
414a8 20 20 20 20 20 6e 4f 75 74 20 3d 20 70 54 53 2d       nOut = pTS-
414a9 3e 61 6e 4f 75 74 70 75 74 5b 69 5d 3b 0a 20 20  >anOutput[i];.  
414aa 20 20 20 20 20 20 70 54 53 2d 3e 61 61 4f 75 74        pTS->aaOut
414ab 70 75 74 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20  put[i] = 0;.    
414ac 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
414ad 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20 20 20 20   int nNew;.     
414ae 20 20 20 63 68 61 72 20 2a 61 4e 65 77 3b 0a 0a     char *aNew;..
414af 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d          int rc =
414b0 20 66 74 73 33 44 6f 63 6c 69 73 74 4f 72 4d 65   fts3DoclistOrMe
414b1 72 67 65 28 70 2d 3e 62 44 65 73 63 49 64 78 2c  rge(p->bDescIdx,
414b2 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 54   .            pT
414b3 53 2d 3e 61 61 4f 75 74 70 75 74 5b 69 5d 2c 20  S->aaOutput[i], 
414b4 70 54 53 2d 3e 61 6e 4f 75 74 70 75 74 5b 69 5d  pTS->anOutput[i]
414b5 2c 20 61 4f 75 74 2c 20 6e 4f 75 74 2c 20 26 61  , aOut, nOut, &a
414b6 4e 65 77 2c 20 26 6e 4e 65 77 0a 20 20 20 20 20  New, &nNew.     
414b7 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66     );.        if
414b8 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
414b9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
414ba 69 74 65 33 5f 66 72 65 65 28 61 4f 75 74 29 3b  ite3_free(aOut);
414bb 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
414bc 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
414bd 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
414be 5f 66 72 65 65 28 70 54 53 2d 3e 61 61 4f 75 74  _free(pTS->aaOut
414bf 70 75 74 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20  put[i]);.       
414c0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 4f   sqlite3_free(aO
414c1 75 74 29 3b 0a 20 20 20 20 20 20 20 20 70 54 53  ut);.        pTS
414c2 2d 3e 61 61 4f 75 74 70 75 74 5b 69 5d 20 3d 20  ->aaOutput[i] = 
414c3 30 3b 0a 20 20 20 20 20 20 20 20 61 4f 75 74 20  0;.        aOut 
414c4 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 20 20  = aNew;.        
414c5 6e 4f 75 74 20 3d 20 6e 4e 65 77 3b 0a 20 20 20  nOut = nNew;.   
414c6 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
414c7 20 20 70 54 53 2d 3e 61 61 4f 75 74 70 75 74 5b    pTS->aaOutput[
414c8 30 5d 20 3d 20 61 4f 75 74 3b 0a 20 20 70 54 53  0] = aOut;.  pTS
414c9 2d 3e 61 6e 4f 75 74 70 75 74 5b 30 5d 20 3d 20  ->anOutput[0] = 
414ca 6e 4f 75 74 3b 0a 20 20 72 65 74 75 72 6e 20 53  nOut;.  return S
414cb 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
414cc 2a 2a 20 4d 65 72 67 65 20 74 68 65 20 64 6f 63  ** Merge the doc
414cd 6c 69 73 74 20 61 44 6f 63 6c 69 73 74 2f 6e 44  list aDoclist/nD
414ce 6f 63 6c 69 73 74 20 69 6e 74 6f 20 74 68 65 20  oclist into the 
414cf 54 65 72 6d 53 65 6c 65 63 74 20 6f 62 6a 65 63  TermSelect objec
414d0 74 20 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 74  t passed.** as t
414d1 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
414d2 74 2e 20 54 68 65 20 6d 65 72 67 65 20 69 73 20  t. The merge is 
414d3 61 6e 20 22 4f 52 22 20 6d 65 72 67 65 20 28 73  an "OR" merge (s
414d4 65 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66  ee function.** f
414d5 74 73 33 44 6f 63 6c 69 73 74 4f 72 4d 65 72 67  ts3DoclistOrMerg
414d6 65 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 29  e() for details)
414d7 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
414d8 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
414d9 77 69 74 68 20 74 68 65 20 64 6f 63 6c 69 73 74  with the doclist
414da 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20 74   for each term t
414db 68 61 74 20 6d 61 74 63 68 65 73 0a 2a 2a 20 61  hat matches.** a
414dc 20 71 75 65 72 69 65 64 20 70 72 65 66 69 78 2e   queried prefix.
414dd 20 49 74 20 6d 65 72 67 65 73 20 61 6c 6c 20 74   It merges all t
414de 68 65 73 65 20 64 6f 63 6c 69 73 74 73 20 69 6e  hese doclists in
414df 74 6f 20 6f 6e 65 2c 20 74 68 65 20 64 6f 63 6c  to one, the docl
414e0 69 73 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73  ist.** for the s
414e1 70 65 63 69 66 69 65 64 20 70 72 65 66 69 78 2e  pecified prefix.
414e2 20 53 69 6e 63 65 20 74 68 65 72 65 20 63 61 6e   Since there can
414e3 20 62 65 20 61 20 76 65 72 79 20 6c 61 72 67 65   be a very large
414e4 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 64 6f   number of.** do
414e5 63 6c 69 73 74 73 20 74 6f 20 6d 65 72 67 65 2c  clists to merge,
414e6 20 74 68 65 20 6d 65 72 67 69 6e 67 20 69 73 20   the merging is 
414e7 64 6f 6e 65 20 70 61 69 72 2d 77 69 73 65 20 75  done pair-wise u
414e8 73 69 6e 67 20 74 68 65 20 54 65 72 6d 53 65 6c  sing the TermSel
414e9 65 63 74 0a 2a 2a 20 6f 62 6a 65 63 74 2e 0a 2a  ect.** object..*
414ea 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
414eb 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  on returns SQLIT
414ec 45 5f 4f 4b 20 69 66 20 74 68 65 20 6d 65 72 67  E_OK if the merg
414ed 65 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  e is successful,
414ee 20 6f 72 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65   or an.** SQLite
414ef 20 65 72 72 6f 72 20 63 6f 64 65 20 28 53 51 4c   error code (SQL
414f0 49 54 45 5f 4e 4f 4d 45 4d 29 20 69 66 20 61 6e  ITE_NOMEM) if an
414f1 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a   error occurs..*
414f2 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
414f3 33 54 65 72 6d 53 65 6c 65 63 74 4d 65 72 67 65  3TermSelectMerge
414f4 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  (.  Fts3Table *p
414f5 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
414f6 20 20 20 20 2f 2a 20 46 54 53 20 74 61 62 6c 65      /* FTS table
414f7 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 54 65 72   handle */.  Ter
414f8 6d 53 65 6c 65 63 74 20 2a 70 54 53 2c 20 20 20  mSelect *pTS,   
414f9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
414fa 54 65 72 6d 53 65 6c 65 63 74 20 6f 62 6a 65 63  TermSelect objec
414fb 74 20 74 6f 20 6d 65 72 67 65 20 69 6e 74 6f 20  t to merge into 
414fc 2a 2f 0a 20 20 63 68 61 72 20 2a 61 44 6f 63 6c  */.  char *aDocl
414fd 69 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ist,            
414fe 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
414ff 74 6f 20 64 6f 63 6c 69 73 74 20 2a 2f 0a 20 20  to doclist */.  
41500 69 6e 74 20 6e 44 6f 63 6c 69 73 74 20 20 20 20  int nDoclist    
41501 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41502 2f 2a 20 53 69 7a 65 20 6f 66 20 61 44 6f 63 6c  /* Size of aDocl
41503 69 73 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  ist in bytes */.
41504 29 7b 0a 20 20 69 66 28 20 70 54 53 2d 3e 61 61  ){.  if( pTS->aa
41505 4f 75 74 70 75 74 5b 30 5d 3d 3d 30 20 29 7b 0a  Output[0]==0 ){.
41506 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
41507 73 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d  s the first term
41508 20 73 65 6c 65 63 74 65 64 2c 20 63 6f 70 79 20   selected, copy 
41509 74 68 65 20 64 6f 63 6c 69 73 74 20 74 6f 20 74  the doclist to t
4150a 68 65 20 6f 75 74 70 75 74 0a 20 20 20 20 2a 2a  he output.    **
4150b 20 62 75 66 66 65 72 20 75 73 69 6e 67 20 6d 65   buffer using me
4150c 6d 63 70 79 28 29 2e 20 2a 2f 0a 20 20 20 20 70  mcpy(). */.    p
4150d 54 53 2d 3e 61 61 4f 75 74 70 75 74 5b 30 5d 20  TS->aaOutput[0] 
4150e 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
4150f 28 6e 44 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20  (nDoclist);.    
41510 70 54 53 2d 3e 61 6e 4f 75 74 70 75 74 5b 30 5d  pTS->anOutput[0]
41511 20 3d 20 6e 44 6f 63 6c 69 73 74 3b 0a 20 20 20   = nDoclist;.   
41512 20 69 66 28 20 70 54 53 2d 3e 61 61 4f 75 74 70   if( pTS->aaOutp
41513 75 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 6d  ut[0] ){.      m
41514 65 6d 63 70 79 28 70 54 53 2d 3e 61 61 4f 75 74  emcpy(pTS->aaOut
41515 70 75 74 5b 30 5d 2c 20 61 44 6f 63 6c 69 73 74  put[0], aDoclist
41516 2c 20 6e 44 6f 63 6c 69 73 74 29 3b 0a 20 20 20  , nDoclist);.   
41517 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65   }else{.      re
41518 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
41519 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  M;.    }.  }else
4151a 7b 0a 20 20 20 20 63 68 61 72 20 2a 61 4d 65 72  {.    char *aMer
4151b 67 65 20 3d 20 61 44 6f 63 6c 69 73 74 3b 0a 20  ge = aDoclist;. 
4151c 20 20 20 69 6e 74 20 6e 4d 65 72 67 65 20 3d 20     int nMerge = 
4151d 6e 44 6f 63 6c 69 73 74 3b 0a 20 20 20 20 69 6e  nDoclist;.    in
4151e 74 20 69 4f 75 74 3b 0a 0a 20 20 20 20 66 6f 72  t iOut;..    for
4151f 28 69 4f 75 74 3d 30 3b 20 69 4f 75 74 3c 53 69  (iOut=0; iOut<Si
41520 7a 65 6f 66 41 72 72 61 79 28 70 54 53 2d 3e 61  zeofArray(pTS->a
41521 61 4f 75 74 70 75 74 29 3b 20 69 4f 75 74 2b 2b  aOutput); iOut++
41522 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 54 53  ){.      if( pTS
41523 2d 3e 61 61 4f 75 74 70 75 74 5b 69 4f 75 74 5d  ->aaOutput[iOut]
41524 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ==0 ){.        a
41525 73 73 65 72 74 28 20 69 4f 75 74 3e 30 20 29 3b  ssert( iOut>0 );
41526 0a 20 20 20 20 20 20 20 20 70 54 53 2d 3e 61 61  .        pTS->aa
41527 4f 75 74 70 75 74 5b 69 4f 75 74 5d 20 3d 20 61  Output[iOut] = a
41528 4d 65 72 67 65 3b 0a 20 20 20 20 20 20 20 20 70  Merge;.        p
41529 54 53 2d 3e 61 6e 4f 75 74 70 75 74 5b 69 4f 75  TS->anOutput[iOu
4152a 74 5d 20 3d 20 6e 4d 65 72 67 65 3b 0a 20 20 20  t] = nMerge;.   
4152b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
4152c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
4152d 20 63 68 61 72 20 2a 61 4e 65 77 3b 0a 20 20 20   char *aNew;.   
4152e 20 20 20 20 20 69 6e 74 20 6e 4e 65 77 3b 0a 0a       int nNew;..
4152f 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d          int rc =
41530 20 66 74 73 33 44 6f 63 6c 69 73 74 4f 72 4d 65   fts3DoclistOrMe
41531 72 67 65 28 70 2d 3e 62 44 65 73 63 49 64 78 2c  rge(p->bDescIdx,
41532 20 61 4d 65 72 67 65 2c 20 6e 4d 65 72 67 65 2c   aMerge, nMerge,
41533 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 54   .            pT
41534 53 2d 3e 61 61 4f 75 74 70 75 74 5b 69 4f 75 74  S->aaOutput[iOut
41535 5d 2c 20 70 54 53 2d 3e 61 6e 4f 75 74 70 75 74  ], pTS->anOutput
41536 5b 69 4f 75 74 5d 2c 20 26 61 4e 65 77 2c 20 26  [iOut], &aNew, &
41537 6e 4e 65 77 0a 20 20 20 20 20 20 20 20 29 3b 0a  nNew.        );.
41538 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
41539 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
4153a 20 20 20 20 20 20 20 69 66 28 20 61 4d 65 72 67         if( aMerg
4153b 65 21 3d 61 44 6f 63 6c 69 73 74 20 29 20 73 71  e!=aDoclist ) sq
4153c 6c 69 74 65 33 5f 66 72 65 65 28 61 4d 65 72 67  lite3_free(aMerg
4153d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  e);.          re
4153e 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
4153f 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20   }..        if( 
41540 61 4d 65 72 67 65 21 3d 61 44 6f 63 6c 69 73 74  aMerge!=aDoclist
41541 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28   ) sqlite3_free(
41542 61 4d 65 72 67 65 29 3b 0a 20 20 20 20 20 20 20  aMerge);.       
41543 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54   sqlite3_free(pT
41544 53 2d 3e 61 61 4f 75 74 70 75 74 5b 69 4f 75 74  S->aaOutput[iOut
41545 5d 29 3b 0a 20 20 20 20 20 20 20 20 70 54 53 2d  ]);.        pTS-
41546 3e 61 61 4f 75 74 70 75 74 5b 69 4f 75 74 5d 20  >aaOutput[iOut] 
41547 3d 20 30 3b 0a 20 20 0a 20 20 20 20 20 20 20 20  = 0;.  .        
41548 61 4d 65 72 67 65 20 3d 20 61 4e 65 77 3b 0a 20  aMerge = aNew;. 
41549 20 20 20 20 20 20 20 6e 4d 65 72 67 65 20 3d 20         nMerge = 
4154a 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 69 66  nNew;.        if
4154b 28 20 28 69 4f 75 74 2b 31 29 3d 3d 53 69 7a 65  ( (iOut+1)==Size
4154c 6f 66 41 72 72 61 79 28 70 54 53 2d 3e 61 61 4f  ofArray(pTS->aaO
4154d 75 74 70 75 74 29 20 29 7b 0a 20 20 20 20 20 20  utput) ){.      
4154e 20 20 20 20 70 54 53 2d 3e 61 61 4f 75 74 70 75      pTS->aaOutpu
4154f 74 5b 69 4f 75 74 5d 20 3d 20 61 4d 65 72 67 65  t[iOut] = aMerge
41550 3b 0a 20 20 20 20 20 20 20 20 20 20 70 54 53 2d  ;.          pTS-
41551 3e 61 6e 4f 75 74 70 75 74 5b 69 4f 75 74 5d 20  >anOutput[iOut] 
41552 3d 20 6e 4d 65 72 67 65 3b 0a 20 20 20 20 20 20  = nMerge;.      
41553 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
41554 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  }.  }.  return S
41555 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
41556 2a 2a 20 41 70 70 65 6e 64 20 53 65 67 52 65 61  ** Append SegRea
41557 64 65 72 20 6f 62 6a 65 63 74 20 70 4e 65 77 20  der object pNew 
41558 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
41559 65 20 70 43 73 72 2d 3e 61 70 53 65 67 6d 65 6e  e pCsr->apSegmen
4155a 74 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a 73 74  t[] array..*/.st
4155b 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65 67  atic int fts3Seg
4155c 52 65 61 64 65 72 43 75 72 73 6f 72 41 70 70 65  ReaderCursorAppe
4155d 6e 64 28 0a 20 20 46 74 73 33 4d 75 6c 74 69 53  nd(.  Fts3MultiS
4155e 65 67 52 65 61 64 65 72 20 2a 70 43 73 72 2c 20  egReader *pCsr, 
4155f 0a 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72  .  Fts3SegReader
41560 20 2a 70 4e 65 77 0a 29 7b 0a 20 20 69 66 28 20   *pNew.){.  if( 
41561 28 70 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 25  (pCsr->nSegment%
41562 31 36 29 3d 3d 30 20 29 7b 0a 20 20 20 20 46 74  16)==0 ){.    Ft
41563 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a 61 70  s3SegReader **ap
41564 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79  New;.    int nBy
41565 74 65 20 3d 20 28 70 43 73 72 2d 3e 6e 53 65 67  te = (pCsr->nSeg
41566 6d 65 6e 74 20 2b 20 31 36 29 2a 73 69 7a 65 6f  ment + 16)*sizeo
41567 66 28 46 74 73 33 53 65 67 52 65 61 64 65 72 2a  f(Fts3SegReader*
41568 29 3b 0a 20 20 20 20 61 70 4e 65 77 20 3d 20 28  );.    apNew = (
41569 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a  Fts3SegReader **
4156a 29 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  )sqlite3_realloc
4156b 28 70 43 73 72 2d 3e 61 70 53 65 67 6d 65 6e 74  (pCsr->apSegment
4156c 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66  , nByte);.    if
4156d 28 20 21 61 70 4e 65 77 20 29 7b 0a 20 20 20 20  ( !apNew ){.    
4156e 20 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67    sqlite3Fts3Seg
4156f 52 65 61 64 65 72 46 72 65 65 28 70 4e 65 77 29  ReaderFree(pNew)
41570 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
41571 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
41572 20 7d 0a 20 20 20 20 70 43 73 72 2d 3e 61 70 53   }.    pCsr->apS
41573 65 67 6d 65 6e 74 20 3d 20 61 70 4e 65 77 3b 0a  egment = apNew;.
41574 20 20 7d 0a 20 20 70 43 73 72 2d 3e 61 70 53 65    }.  pCsr->apSe
41575 67 6d 65 6e 74 5b 70 43 73 72 2d 3e 6e 53 65 67  gment[pCsr->nSeg
41576 6d 65 6e 74 2b 2b 5d 20 3d 20 70 4e 65 77 3b 0a  ment++] = pNew;.
41577 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
41578 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  OK;.}../*.** Add
41579 20 73 65 67 2d 72 65 61 64 65 72 20 6f 62 6a 65   seg-reader obje
4157a 63 74 73 20 74 6f 20 74 68 65 20 46 74 73 33 4d  cts to the Fts3M
4157b 75 6c 74 69 53 65 67 52 65 61 64 65 72 20 6f 62  ultiSegReader ob
4157c 6a 65 63 74 20 70 61 73 73 65 64 20 61 73 20 74  ject passed as t
4157d 68 65 0a 2a 2a 20 38 74 68 20 61 72 67 75 6d 65  he.** 8th argume
4157e 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  nt..**.** This f
4157f 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
41580 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63  SQLITE_OK if suc
41581 63 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53  cessful, or an S
41582 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
41583 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a  .** otherwise..*
41584 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
41585 33 53 65 67 52 65 61 64 65 72 43 75 72 73 6f 72  3SegReaderCursor
41586 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  (.  Fts3Table *p
41587 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
41588 20 20 20 20 2f 2a 20 46 54 53 33 20 74 61 62 6c      /* FTS3 tabl
41589 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e  e handle */.  in
4158a 74 20 69 4c 61 6e 67 69 64 2c 20 20 20 20 20 20  t iLangid,      
4158b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4158c 20 4c 61 6e 67 75 61 67 65 20 69 64 20 2a 2f 0a   Language id */.
4158d 20 20 69 6e 74 20 69 49 6e 64 65 78 2c 20 20 20    int iIndex,   
4158e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4158f 20 20 2f 2a 20 49 6e 64 65 78 20 74 6f 20 73 65    /* Index to se
41590 61 72 63 68 20 28 66 72 6f 6d 20 30 20 74 6f 20  arch (from 0 to 
41591 70 2d 3e 6e 49 6e 64 65 78 2d 31 29 20 2a 2f 0a  p->nIndex-1) */.
41592 20 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 20 20    int iLevel,   
41593 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41594 20 20 2f 2a 20 4c 65 76 65 6c 20 6f 66 20 73 65    /* Level of se
41595 67 6d 65 6e 74 73 20 74 6f 20 73 63 61 6e 20 2a  gments to scan *
41596 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
41597 7a 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20  zTerm,          
41598 20 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20 71      /* Term to q
41599 75 65 72 79 20 66 6f 72 20 2a 2f 0a 20 20 69 6e  uery for */.  in
4159a 74 20 6e 54 65 72 6d 2c 20 20 20 20 20 20 20 20  t nTerm,        
4159b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4159c 20 53 69 7a 65 20 6f 66 20 7a 54 65 72 6d 20 69   Size of zTerm i
4159d 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74  n bytes */.  int
4159e 20 69 73 50 72 65 66 69 78 2c 20 20 20 20 20 20   isPrefix,      
4159f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
415a0 54 72 75 65 20 66 6f 72 20 61 20 70 72 65 66 69  True for a prefi
415a1 78 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 69 6e  x search */.  in
415a2 74 20 69 73 53 63 61 6e 2c 20 20 20 20 20 20 20  t isScan,       
415a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
415a4 20 54 72 75 65 20 74 6f 20 73 63 61 6e 20 66 72   True to scan fr
415a5 6f 6d 20 7a 54 65 72 6d 20 74 6f 20 45 4f 46 20  om zTerm to EOF 
415a6 2a 2f 0a 20 20 46 74 73 33 4d 75 6c 74 69 53 65  */.  Fts3MultiSe
415a7 67 52 65 61 64 65 72 20 2a 70 43 73 72 20 20 20  gReader *pCsr   
415a8 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f       /* Cursor o
415a9 62 6a 65 63 74 20 74 6f 20 70 6f 70 75 6c 61 74  bject to populat
415aa 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
415ab 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
415ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72            /* Err
415ad 6f 72 20 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c  or code */.  sql
415ae 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
415af 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
415b0 53 74 61 74 65 6d 65 6e 74 20 74 6f 20 69 74 65  Statement to ite
415b1 72 61 74 65 20 74 68 72 6f 75 67 68 20 73 65 67  rate through seg
415b2 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 72  ments */.  int r
415b3 63 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  c2;             
415b4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
415b5 73 75 6c 74 20 6f 66 20 73 71 6c 69 74 65 33 5f  sult of sqlite3_
415b6 72 65 73 65 74 28 29 20 2a 2f 0a 0a 20 20 2f 2a  reset() */..  /*
415b7 20 49 66 20 69 4c 65 76 65 6c 20 69 73 20 6c 65   If iLevel is le
415b8 73 73 20 74 68 61 6e 20 30 20 61 6e 64 20 74 68  ss than 0 and th
415b9 69 73 20 69 73 20 6e 6f 74 20 61 20 73 63 61 6e  is is not a scan
415ba 2c 20 69 6e 63 6c 75 64 65 20 61 20 73 65 67 2d  , include a seg-
415bb 72 65 61 64 65 72 20 0a 20 20 2a 2a 20 66 6f 72  reader .  ** for
415bc 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 74 65 72   the pending-ter
415bd 6d 73 2e 20 49 66 20 74 68 69 73 20 69 73 20 61  ms. If this is a
415be 20 73 63 61 6e 2c 20 74 68 65 6e 20 74 68 69 73   scan, then this
415bf 20 63 61 6c 6c 20 6d 75 73 74 20 62 65 20 62 65   call must be be
415c0 69 6e 67 0a 20 20 2a 2a 20 6d 61 64 65 20 62 79  ing.  ** made by
415c1 20 61 6e 20 66 74 73 34 61 75 78 20 6d 6f 64 75   an fts4aux modu
415c2 6c 65 2c 20 6e 6f 74 20 61 6e 20 46 54 53 20 74  le, not an FTS t
415c3 61 62 6c 65 2e 20 49 6e 20 74 68 69 73 20 63 61  able. In this ca
415c4 73 65 20 63 61 6c 6c 69 6e 67 0a 20 20 2a 2a 20  se calling.  ** 
415c5 46 74 73 33 53 65 67 52 65 61 64 65 72 50 65 6e  Fts3SegReaderPen
415c6 64 69 6e 67 20 6d 69 67 68 74 20 73 65 67 66 61  ding might segfa
415c7 75 6c 74 2c 20 61 73 20 74 68 65 20 64 61 74 61  ult, as the data
415c8 20 73 74 72 75 63 74 75 72 65 73 20 75 73 65 64   structures used
415c9 20 62 79 20 0a 20 20 2a 2a 20 66 74 73 34 61 75   by .  ** fts4au
415ca 78 20 61 72 65 20 6e 6f 74 20 63 6f 6d 70 6c 65  x are not comple
415cb 74 65 6c 79 20 70 6f 70 75 6c 61 74 65 64 2e 20  tely populated. 
415cc 53 6f 20 69 74 27 73 20 65 61 73 69 65 73 74 20  So it's easiest 
415cd 74 6f 20 66 69 6c 74 65 72 20 74 68 65 73 65 0a  to filter these.
415ce 20 20 2a 2a 20 63 61 6c 6c 73 20 6f 75 74 20 68    ** calls out h
415cf 65 72 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 69  ere.  */.  if( i
415d0 4c 65 76 65 6c 3c 30 20 26 26 20 70 2d 3e 61 49  Level<0 && p->aI
415d1 6e 64 65 78 20 29 7b 0a 20 20 20 20 46 74 73 33  ndex ){.    Fts3
415d2 53 65 67 52 65 61 64 65 72 20 2a 70 53 65 67 20  SegReader *pSeg 
415d3 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  = 0;.    rc = sq
415d4 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64  lite3Fts3SegRead
415d5 65 72 50 65 6e 64 69 6e 67 28 70 2c 20 69 49 6e  erPending(p, iIn
415d6 64 65 78 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72  dex, zTerm, nTer
415d7 6d 2c 20 69 73 50 72 65 66 69 78 2c 20 26 70 53  m, isPrefix, &pS
415d8 65 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  eg);.    if( rc=
415d9 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 53  =SQLITE_OK && pS
415da 65 67 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  eg ){.      rc =
415db 20 66 74 73 33 53 65 67 52 65 61 64 65 72 43 75   fts3SegReaderCu
415dc 72 73 6f 72 41 70 70 65 6e 64 28 70 43 73 72 2c  rsorAppend(pCsr,
415dd 20 70 53 65 67 29 3b 0a 20 20 20 20 7d 0a 20 20   pSeg);.    }.  
415de 7d 0a 0a 20 20 69 66 28 20 69 4c 65 76 65 6c 21  }..  if( iLevel!
415df 3d 46 54 53 33 5f 53 45 47 43 55 52 53 4f 52 5f  =FTS3_SEGCURSOR_
415e0 50 45 4e 44 49 4e 47 20 29 7b 0a 20 20 20 20 69  PENDING ){.    i
415e1 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
415e2 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
415e3 71 6c 69 74 65 33 46 74 73 33 41 6c 6c 53 65 67  qlite3Fts3AllSeg
415e4 64 69 72 73 28 70 2c 20 69 4c 61 6e 67 69 64 2c  dirs(p, iLangid,
415e5 20 69 49 6e 64 65 78 2c 20 69 4c 65 76 65 6c 2c   iIndex, iLevel,
415e6 20 26 70 53 74 6d 74 29 3b 0a 20 20 20 20 7d 0a   &pStmt);.    }.
415e7 0a 20 20 20 20 77 68 69 6c 65 28 20 72 63 3d 3d  .    while( rc==
415e8 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 53 51 4c  SQLITE_OK && SQL
415e9 49 54 45 5f 52 4f 57 3d 3d 28 72 63 20 3d 20 73  ITE_ROW==(rc = s
415ea 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d  qlite3_step(pStm
415eb 74 29 29 20 29 7b 0a 20 20 20 20 20 20 46 74 73  t)) ){.      Fts
415ec 33 53 65 67 52 65 61 64 65 72 20 2a 70 53 65 67  3SegReader *pSeg
415ed 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20   = 0;..      /* 
415ee 52 65 61 64 20 74 68 65 20 76 61 6c 75 65 73 20  Read the values 
415ef 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
415f0 53 45 4c 45 43 54 20 69 6e 74 6f 20 6c 6f 63 61  SELECT into loca
415f1 6c 20 76 61 72 69 61 62 6c 65 73 2e 20 2a 2f 0a  l variables. */.
415f2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
415f3 74 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 20  t64 iStartBlock 
415f4 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
415f5 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c 20 31 29  _int64(pStmt, 1)
415f6 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
415f7 69 6e 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64  int64 iLeavesEnd
415f8 42 6c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f  Block = sqlite3_
415f9 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 53 74  column_int64(pSt
415fa 6d 74 2c 20 32 29 3b 0a 20 20 20 20 20 20 73 71  mt, 2);.      sq
415fb 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 45 6e 64  lite3_int64 iEnd
415fc 42 6c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f  Block = sqlite3_
415fd 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 53 74  column_int64(pSt
415fe 6d 74 2c 20 33 29 3b 0a 20 20 20 20 20 20 69 6e  mt, 3);.      in
415ff 74 20 6e 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65  t nRoot = sqlite
41600 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70  3_column_bytes(p
41601 53 74 6d 74 2c 20 34 29 3b 0a 20 20 20 20 20 20  Stmt, 4);.      
41602 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 52 6f 6f  char const *zRoo
41603 74 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  t = sqlite3_colu
41604 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 34  mn_blob(pStmt, 4
41605 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  );..      /* If 
41606 7a 54 65 72 6d 20 69 73 20 6e 6f 74 20 4e 55 4c  zTerm is not NUL
41607 4c 2c 20 61 6e 64 20 74 68 69 73 20 73 65 67 6d  L, and this segm
41608 65 6e 74 20 69 73 20 6e 6f 74 20 73 74 6f 72 65  ent is not store
41609 64 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 69 74  d entirely on it
4160a 73 0a 20 20 20 20 20 20 2a 2a 20 72 6f 6f 74 20  s.      ** root 
4160b 6e 6f 64 65 2c 20 74 68 65 20 72 61 6e 67 65 20  node, the range 
4160c 6f 66 20 6c 65 61 76 65 73 20 73 63 61 6e 6e 65  of leaves scanne
4160d 64 20 63 61 6e 20 62 65 20 72 65 64 75 63 65 64  d can be reduced
4160e 2e 20 44 6f 20 74 68 69 73 2e 20 2a 2f 0a 20 20  . Do this. */.  
4160f 20 20 20 20 69 66 28 20 69 53 74 61 72 74 42 6c      if( iStartBl
41610 6f 63 6b 20 26 26 20 7a 54 65 72 6d 20 29 7b 0a  ock && zTerm ){.
41611 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
41612 69 6e 74 36 34 20 2a 70 69 20 3d 20 28 69 73 50  int64 *pi = (isP
41613 72 65 66 69 78 20 3f 20 26 69 4c 65 61 76 65 73  refix ? &iLeaves
41614 45 6e 64 42 6c 6f 63 6b 20 3a 20 30 29 3b 0a 20  EndBlock : 0);. 
41615 20 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33         rc = fts3
41616 53 65 6c 65 63 74 4c 65 61 66 28 70 2c 20 7a 54  SelectLeaf(p, zT
41617 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 7a 52 6f 6f  erm, nTerm, zRoo
41618 74 2c 20 6e 52 6f 6f 74 2c 20 26 69 53 74 61 72  t, nRoot, &iStar
41619 74 42 6c 6f 63 6b 2c 20 70 69 29 3b 0a 20 20 20  tBlock, pi);.   
4161a 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
4161b 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 69  ITE_OK ) goto fi
4161c 6e 69 73 68 65 64 3b 0a 20 20 20 20 20 20 20 20  nished;.        
4161d 69 66 28 20 69 73 50 72 65 66 69 78 3d 3d 30 20  if( isPrefix==0 
4161e 26 26 20 69 73 53 63 61 6e 3d 3d 30 20 29 20 69  && isScan==0 ) i
4161f 4c 65 61 76 65 73 45 6e 64 42 6c 6f 63 6b 20 3d  LeavesEndBlock =
41620 20 69 53 74 61 72 74 42 6c 6f 63 6b 3b 0a 20 20   iStartBlock;.  
41621 20 20 20 20 7d 0a 20 0a 20 20 20 20 20 20 72 63      }. .      rc
41622 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 53 65   = sqlite3Fts3Se
41623 67 52 65 61 64 65 72 4e 65 77 28 70 43 73 72 2d  gReaderNew(pCsr-
41624 3e 6e 53 65 67 6d 65 6e 74 2b 31 2c 20 0a 20 20  >nSegment+1, .  
41625 20 20 20 20 20 20 20 20 28 69 73 50 72 65 66 69          (isPrefi
41626 78 3d 3d 30 20 26 26 20 69 73 53 63 61 6e 3d 3d  x==0 && isScan==
41627 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 69 53  0),.          iS
41628 74 61 72 74 42 6c 6f 63 6b 2c 20 69 4c 65 61 76  tartBlock, iLeav
41629 65 73 45 6e 64 42 6c 6f 63 6b 2c 20 0a 20 20 20  esEndBlock, .   
4162a 20 20 20 20 20 20 20 69 45 6e 64 42 6c 6f 63 6b         iEndBlock
4162b 2c 20 7a 52 6f 6f 74 2c 20 6e 52 6f 6f 74 2c 20  , zRoot, nRoot, 
4162c 26 70 53 65 67 0a 20 20 20 20 20 20 29 3b 0a 20  &pSeg.      );. 
4162d 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
4162e 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 69  ITE_OK ) goto fi
4162f 6e 69 73 68 65 64 3b 0a 20 20 20 20 20 20 72 63  nished;.      rc
41630 20 3d 20 66 74 73 33 53 65 67 52 65 61 64 65 72   = fts3SegReader
41631 43 75 72 73 6f 72 41 70 70 65 6e 64 28 70 43 73  CursorAppend(pCs
41632 72 2c 20 70 53 65 67 29 3b 0a 20 20 20 20 7d 0a  r, pSeg);.    }.
41633 20 20 7d 0a 0a 20 66 69 6e 69 73 68 65 64 3a 0a    }.. finished:.
41634 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 5f    rc2 = sqlite3_
41635 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20  reset(pStmt);.  
41636 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
41637 4f 4e 45 20 29 20 72 63 20 3d 20 72 63 32 3b 0a  ONE ) rc = rc2;.
41638 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
41639 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 61 20  ./*.** Set up a 
4163a 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 66 6f  cursor object fo
4163b 72 20 69 74 65 72 61 74 69 6e 67 20 74 68 72 6f  r iterating thro
4163c 75 67 68 20 61 20 66 75 6c 6c 2d 74 65 78 74 20  ugh a full-text 
4163d 69 6e 64 65 78 20 6f 72 20 61 20 0a 2a 2a 20 73  index or a .** s
4163e 69 6e 67 6c 65 20 6c 65 76 65 6c 20 74 68 65 72  ingle level ther
4163f 65 69 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ein..*/.SQLITE_P
41640 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
41641 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72 43  e3Fts3SegReaderC
41642 75 72 73 6f 72 28 0a 20 20 46 74 73 33 54 61 62  ursor(.  Fts3Tab
41643 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  le *p,          
41644 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53 33           /* FTS3
41645 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f   table handle */
41646 0a 20 20 69 6e 74 20 69 4c 61 6e 67 69 64 2c 20  .  int iLangid, 
41647 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41648 20 20 20 2f 2a 20 4c 61 6e 67 75 61 67 65 2d 69     /* Language-i
41649 64 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20  d to search */. 
4164a 20 69 6e 74 20 69 49 6e 64 65 78 2c 20 20 20 20   int iIndex,    
4164b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4164c 20 2f 2a 20 49 6e 64 65 78 20 74 6f 20 73 65 61   /* Index to sea
4164d 72 63 68 20 28 66 72 6f 6d 20 30 20 74 6f 20 70  rch (from 0 to p
4164e 2d 3e 6e 49 6e 64 65 78 2d 31 29 20 2a 2f 0a 20  ->nIndex-1) */. 
4164f 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 20 20 20   int iLevel,    
41650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41651 20 2f 2a 20 4c 65 76 65 6c 20 6f 66 20 73 65 67   /* Level of seg
41652 6d 65 6e 74 73 20 74 6f 20 73 63 61 6e 20 2a 2f  ments to scan */
41653 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
41654 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20  Term,           
41655 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20 71 75     /* Term to qu
41656 65 72 79 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74  ery for */.  int
41657 20 6e 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20   nTerm,         
41658 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
41659 53 69 7a 65 20 6f 66 20 7a 54 65 72 6d 20 69 6e  Size of zTerm in
4165a 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20   bytes */.  int 
4165b 69 73 50 72 65 66 69 78 2c 20 20 20 20 20 20 20  isPrefix,       
4165c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
4165d 72 75 65 20 66 6f 72 20 61 20 70 72 65 66 69 78  rue for a prefix
4165e 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 69 6e 74   search */.  int
4165f 20 69 73 53 63 61 6e 2c 20 20 20 20 20 20 20 20   isScan,        
41660 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
41661 54 72 75 65 20 74 6f 20 73 63 61 6e 20 66 72 6f  True to scan fro
41662 6d 20 7a 54 65 72 6d 20 74 6f 20 45 4f 46 20 2a  m zTerm to EOF *
41663 2f 0a 20 20 46 74 73 33 4d 75 6c 74 69 53 65 67  /.  Fts3MultiSeg
41664 52 65 61 64 65 72 20 2a 70 43 73 72 20 20 20 20  Reader *pCsr    
41665 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 62 6a     /* Cursor obj
41666 65 63 74 20 74 6f 20 70 6f 70 75 6c 61 74 65 20  ect to populate 
41667 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20  */.){.  assert( 
41668 69 49 6e 64 65 78 3e 3d 30 20 26 26 20 69 49 6e  iIndex>=0 && iIn
41669 64 65 78 3c 70 2d 3e 6e 49 6e 64 65 78 20 29 3b  dex<p->nIndex );
4166a 0a 20 20 61 73 73 65 72 74 28 20 69 4c 65 76 65  .  assert( iLeve
4166b 6c 3d 3d 46 54 53 33 5f 53 45 47 43 55 52 53 4f  l==FTS3_SEGCURSO
4166c 52 5f 41 4c 4c 0a 20 20 20 20 20 20 7c 7c 20 20  R_ALL.      ||  
4166d 69 4c 65 76 65 6c 3d 3d 46 54 53 33 5f 53 45 47  iLevel==FTS3_SEG
4166e 43 55 52 53 4f 52 5f 50 45 4e 44 49 4e 47 20 0a  CURSOR_PENDING .
4166f 20 20 20 20 20 20 7c 7c 20 20 69 4c 65 76 65 6c        ||  iLevel
41670 3e 3d 30 0a 20 20 29 3b 0a 20 20 61 73 73 65 72  >=0.  );.  asser
41671 74 28 20 69 4c 65 76 65 6c 3c 46 54 53 33 5f 53  t( iLevel<FTS3_S
41672 45 47 44 49 52 5f 4d 41 58 4c 45 56 45 4c 20 29  EGDIR_MAXLEVEL )
41673 3b 0a 20 20 61 73 73 65 72 74 28 20 46 54 53 33  ;.  assert( FTS3
41674 5f 53 45 47 43 55 52 53 4f 52 5f 41 4c 4c 3c 30  _SEGCURSOR_ALL<0
41675 20 26 26 20 46 54 53 33 5f 53 45 47 43 55 52 53   && FTS3_SEGCURS
41676 4f 52 5f 50 45 4e 44 49 4e 47 3c 30 20 29 3b 0a  OR_PENDING<0 );.
41677 20 20 61 73 73 65 72 74 28 20 69 73 50 72 65 66    assert( isPref
41678 69 78 3d 3d 30 20 7c 7c 20 69 73 53 63 61 6e 3d  ix==0 || isScan=
41679 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28  =0 );..  memset(
4167a 70 43 73 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pCsr, 0, sizeof(
4167b 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64  Fts3MultiSegRead
4167c 65 72 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 66  er));.  return f
4167d 74 73 33 53 65 67 52 65 61 64 65 72 43 75 72 73  ts3SegReaderCurs
4167e 6f 72 28 0a 20 20 20 20 20 20 70 2c 20 69 4c 61  or(.      p, iLa
4167f 6e 67 69 64 2c 20 69 49 6e 64 65 78 2c 20 69 4c  ngid, iIndex, iL
41680 65 76 65 6c 2c 20 7a 54 65 72 6d 2c 20 6e 54 65  evel, zTerm, nTe
41681 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20 69 73  rm, isPrefix, is
41682 53 63 61 6e 2c 20 70 43 73 72 0a 20 20 29 3b 0a  Scan, pCsr.  );.
41683 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20 61 64 64 69  }../*.** In addi
41684 74 69 6f 6e 20 74 6f 20 69 74 73 20 63 75 72 72  tion to its curr
41685 65 6e 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  ent configuratio
41686 6e 2c 20 68 61 76 65 20 74 68 65 20 46 74 73 33  n, have the Fts3
41687 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72 0a 2a  MultiSegReader.*
41688 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  * passed as the 
41689 34 74 68 20 61 72 67 75 6d 65 6e 74 20 61 6c 73  4th argument als
4168a 6f 20 73 63 61 6e 20 74 68 65 20 64 6f 63 6c 69  o scan the docli
4168b 73 74 20 66 6f 72 20 74 65 72 6d 20 7a 54 65 72  st for term zTer
4168c 6d 2f 6e 54 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 53  m/nTerm..**.** S
4168d 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
4168e 72 6e 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72  rned if no error
4168f 20 6f 63 63 75 72 73 2c 20 6f 74 68 65 72 77 69   occurs, otherwi
41690 73 65 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  se an SQLite err
41691 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74  or code..*/.stat
41692 69 63 20 69 6e 74 20 66 74 73 33 53 65 67 52 65  ic int fts3SegRe
41693 61 64 65 72 43 75 72 73 6f 72 41 64 64 5a 65 72  aderCursorAddZer
41694 6f 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  o(.  Fts3Table *
41695 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
41696 20 20 20 20 20 2f 2a 20 46 54 53 20 76 69 72 74       /* FTS virt
41697 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
41698 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 61 6e 67 69   */.  int iLangi
41699 64 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  d,.  const char 
4169a 2a 7a 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20  *zTerm,         
4169b 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20       /* Term to 
4169c 73 63 61 6e 20 64 6f 63 6c 69 73 74 20 6f 66 20  scan doclist of 
4169d 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c 20  */.  int nTerm, 
4169e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4169f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
416a0 66 20 62 79 74 65 73 20 69 6e 20 7a 54 65 72 6d  f bytes in zTerm
416a1 20 2a 2f 0a 20 20 46 74 73 33 4d 75 6c 74 69 53   */.  Fts3MultiS
416a2 65 67 52 65 61 64 65 72 20 2a 70 43 73 72 20 20  egReader *pCsr  
416a3 20 20 20 20 20 20 2f 2a 20 46 74 73 33 4d 75 6c        /* Fts3Mul
416a4 74 69 53 65 67 52 65 61 64 65 72 20 74 6f 20 6d  tiSegReader to m
416a5 6f 64 69 66 79 20 2a 2f 0a 29 7b 0a 20 20 72 65  odify */.){.  re
416a6 74 75 72 6e 20 66 74 73 33 53 65 67 52 65 61 64  turn fts3SegRead
416a7 65 72 43 75 72 73 6f 72 28 70 2c 20 0a 20 20 20  erCursor(p, .   
416a8 20 20 20 69 4c 61 6e 67 69 64 2c 20 30 2c 20 46     iLangid, 0, F
416a9 54 53 33 5f 53 45 47 43 55 52 53 4f 52 5f 41 4c  TS3_SEGCURSOR_AL
416aa 4c 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 2c  L, zTerm, nTerm,
416ab 20 30 2c 20 30 2c 70 43 73 72 0a 20 20 29 3b 0a   0, 0,pCsr.  );.
416ac 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 6e  }../*.** Open an
416ad 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61   Fts3MultiSegRea
416ae 64 65 72 20 74 6f 20 73 63 61 6e 20 74 68 65 20  der to scan the 
416af 64 6f 63 6c 69 73 74 20 66 6f 72 20 74 65 72 6d  doclist for term
416b0 20 7a 54 65 72 6d 2f 6e 54 65 72 6d 2e 20 4f 72   zTerm/nTerm. Or
416b1 2c 0a 2a 2a 20 69 66 20 69 73 50 72 65 66 69 78  ,.** if isPrefix
416b2 20 69 73 20 74 72 75 65 2c 20 74 6f 20 73 63 61   is true, to sca
416b3 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 20 66 6f  n the doclist fo
416b4 72 20 61 6c 6c 20 74 65 72 6d 73 20 66 6f 72 20  r all terms for 
416b5 77 68 69 63 68 20 0a 2a 2a 20 7a 54 65 72 6d 2f  which .** zTerm/
416b6 6e 54 65 72 6d 20 69 73 20 61 20 70 72 65 66 69  nTerm is a prefi
416b7 78 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  x. If successful
416b8 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
416b9 4f 4b 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20  OK and write.** 
416ba 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
416bb 20 6e 65 77 20 46 74 73 33 4d 75 6c 74 69 53 65   new Fts3MultiSe
416bc 67 52 65 61 64 65 72 20 74 6f 20 2a 70 70 53 65  gReader to *ppSe
416bd 67 63 73 72 2e 20 4f 74 68 65 72 77 69 73 65 2c  gcsr. Otherwise,
416be 20 72 65 74 75 72 6e 0a 2a 2a 20 61 6e 20 53 51   return.** an SQ
416bf 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e  Lite error code.
416c0 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 74 68 65  .**.** It is the
416c1 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20   responsibility 
416c2 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  of the caller to
416c3 20 66 72 65 65 20 74 68 69 73 20 6f 62 6a 65 63   free this objec
416c4 74 20 62 79 20 65 76 65 6e 74 75 61 6c 6c 79 0a  t by eventually.
416c5 2a 2a 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f  ** passing it to
416c6 20 66 74 73 33 53 65 67 52 65 61 64 65 72 43 75   fts3SegReaderCu
416c7 72 73 6f 72 46 72 65 65 28 29 20 0a 2a 2a 0a 2a  rsorFree() .**.*
416c8 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
416c9 65 74 75 72 6e 65 64 20 69 66 20 6e 6f 20 65 72  eturned if no er
416ca 72 6f 72 20 6f 63 63 75 72 73 2c 20 6f 74 68 65  ror occurs, othe
416cb 72 77 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20  rwise an SQLite 
416cc 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 20 4f  error code..** O
416cd 75 74 70 75 74 20 70 61 72 61 6d 65 74 65 72 20  utput parameter 
416ce 2a 70 70 53 65 67 63 73 72 20 69 73 20 73 65 74  *ppSegcsr is set
416cf 20 74 6f 20 30 20 69 66 20 61 6e 20 65 72 72 6f   to 0 if an erro
416d0 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61  r occurs..*/.sta
416d1 74 69 63 20 69 6e 74 20 66 74 73 33 54 65 72 6d  tic int fts3Term
416d2 53 65 67 52 65 61 64 65 72 43 75 72 73 6f 72 28  SegReaderCursor(
416d3 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70  .  Fts3Cursor *p
416d4 43 73 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  Csr,            
416d5 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61     /* Virtual ta
416d6 62 6c 65 20 63 75 72 73 6f 72 20 68 61 6e 64 6c  ble cursor handl
416d7 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
416d8 72 20 2a 7a 54 65 72 6d 2c 20 20 20 20 20 20 20  r *zTerm,       
416d9 20 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 74         /* Term t
416da 6f 20 71 75 65 72 79 20 66 6f 72 20 2a 2f 0a 20  o query for */. 
416db 20 69 6e 74 20 6e 54 65 72 6d 2c 20 20 20 20 20   int nTerm,     
416dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
416dd 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 54 65 72   /* Size of zTer
416de 6d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  m in bytes */.  
416df 69 6e 74 20 69 73 50 72 65 66 69 78 2c 20 20 20  int isPrefix,   
416e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
416e1 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 70 72  /* True for a pr
416e2 65 66 69 78 20 73 65 61 72 63 68 20 2a 2f 0a 20  efix search */. 
416e3 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61   Fts3MultiSegRea
416e4 64 65 72 20 2a 2a 70 70 53 65 67 63 73 72 20 20  der **ppSegcsr  
416e5 20 2f 2a 20 4f 55 54 3a 20 41 6c 6c 6f 63 61 74   /* OUT: Allocat
416e6 65 64 20 73 65 67 2d 72 65 61 64 65 72 20 63 75  ed seg-reader cu
416e7 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 46 74 73  rsor */.){.  Fts
416e8 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72 20  3MultiSegReader 
416e9 2a 70 53 65 67 63 73 72 3b 20 20 20 20 2f 2a 20  *pSegcsr;    /* 
416ea 4f 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f 63 61  Object to alloca
416eb 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f  te and return */
416ec 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
416ed 54 45 5f 4e 4f 4d 45 4d 3b 20 20 20 20 20 20 20  TE_NOMEM;       
416ee 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
416ef 65 20 2a 2f 0a 0a 20 20 70 53 65 67 63 73 72 20  e */..  pSegcsr 
416f0 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
416f1 28 73 69 7a 65 6f 66 28 46 74 73 33 4d 75 6c 74  (sizeof(Fts3Mult
416f2 69 53 65 67 52 65 61 64 65 72 29 29 3b 0a 20 20  iSegReader));.  
416f3 69 66 28 20 70 53 65 67 63 73 72 20 29 7b 0a 20  if( pSegcsr ){. 
416f4 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 6e     int i;.    in
416f5 74 20 62 46 6f 75 6e 64 20 3d 20 30 3b 20 20 20  t bFound = 0;   
416f6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
416f7 72 75 65 20 6f 6e 63 65 20 61 6e 20 69 6e 64 65  rue once an inde
416f8 78 20 68 61 73 20 62 65 65 6e 20 66 6f 75 6e 64  x has been found
416f9 20 2a 2f 0a 20 20 20 20 46 74 73 33 54 61 62 6c   */.    Fts3Tabl
416fa 65 20 2a 70 20 3d 20 28 46 74 73 33 54 61 62 6c  e *p = (Fts3Tabl
416fb 65 20 2a 29 70 43 73 72 2d 3e 62 61 73 65 2e 70  e *)pCsr->base.p
416fc 56 74 61 62 3b 0a 0a 20 20 20 20 69 66 28 20 69  Vtab;..    if( i
416fd 73 50 72 65 66 69 78 20 29 7b 0a 20 20 20 20 20  sPrefix ){.     
416fe 20 66 6f 72 28 69 3d 31 3b 20 62 46 6f 75 6e 64   for(i=1; bFound
416ff 3d 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 49 6e 64  ==0 && i<p->nInd
41700 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ex; i++){.      
41701 20 20 69 66 28 20 70 2d 3e 61 49 6e 64 65 78 5b    if( p->aIndex[
41702 69 5d 2e 6e 50 72 65 66 69 78 3d 3d 6e 54 65 72  i].nPrefix==nTer
41703 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  m ){.          b
41704 46 6f 75 6e 64 20 3d 20 31 3b 0a 20 20 20 20 20  Found = 1;.     
41705 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
41706 33 46 74 73 33 53 65 67 52 65 61 64 65 72 43 75  3Fts3SegReaderCu
41707 72 73 6f 72 28 70 2c 20 70 43 73 72 2d 3e 69 4c  rsor(p, pCsr->iL
41708 61 6e 67 69 64 2c 20 0a 20 20 20 20 20 20 20 20  angid, .        
41709 20 20 20 20 20 20 69 2c 20 46 54 53 33 5f 53 45        i, FTS3_SE
4170a 47 43 55 52 53 4f 52 5f 41 4c 4c 2c 20 7a 54 65  GCURSOR_ALL, zTe
4170b 72 6d 2c 20 6e 54 65 72 6d 2c 20 30 2c 20 30 2c  rm, nTerm, 0, 0,
4170c 20 70 53 65 67 63 73 72 0a 20 20 20 20 20 20 20   pSegcsr.       
4170d 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20     );.          
4170e 70 53 65 67 63 73 72 2d 3e 62 4c 6f 6f 6b 75 70  pSegcsr->bLookup
4170f 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
41710 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 66        }..      f
41711 6f 72 28 69 3d 31 3b 20 62 46 6f 75 6e 64 3d 3d  or(i=1; bFound==
41712 30 20 26 26 20 69 3c 70 2d 3e 6e 49 6e 64 65 78  0 && i<p->nIndex
41713 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
41714 69 66 28 20 70 2d 3e 61 49 6e 64 65 78 5b 69 5d  if( p->aIndex[i]
41715 2e 6e 50 72 65 66 69 78 3d 3d 6e 54 65 72 6d 2b  .nPrefix==nTerm+
41716 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  1 ){.          b
41717 46 6f 75 6e 64 20 3d 20 31 3b 0a 20 20 20 20 20  Found = 1;.     
41718 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
41719 33 46 74 73 33 53 65 67 52 65 61 64 65 72 43 75  3Fts3SegReaderCu
4171a 72 73 6f 72 28 70 2c 20 70 43 73 72 2d 3e 69 4c  rsor(p, pCsr->iL
4171b 61 6e 67 69 64 2c 20 0a 20 20 20 20 20 20 20 20  angid, .        
4171c 20 20 20 20 20 20 69 2c 20 46 54 53 33 5f 53 45        i, FTS3_SE
4171d 47 43 55 52 53 4f 52 5f 41 4c 4c 2c 20 7a 54 65  GCURSOR_ALL, zTe
4171e 72 6d 2c 20 6e 54 65 72 6d 2c 20 31 2c 20 30 2c  rm, nTerm, 1, 0,
4171f 20 70 53 65 67 63 73 72 0a 20 20 20 20 20 20 20   pSegcsr.       
41720 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20     );.          
41721 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
41722 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  K ){.           
41723 20 72 63 20 3d 20 66 74 73 33 53 65 67 52 65 61   rc = fts3SegRea
41724 64 65 72 43 75 72 73 6f 72 41 64 64 5a 65 72 6f  derCursorAddZero
41725 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  (.              
41726 20 20 70 2c 20 70 43 73 72 2d 3e 69 4c 61 6e 67    p, pCsr->iLang
41727 69 64 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d  id, zTerm, nTerm
41728 2c 20 70 53 65 67 63 73 72 0a 20 20 20 20 20 20  , pSegcsr.      
41729 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20        );.       
4172a 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
4172b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
4172c 20 20 69 66 28 20 62 46 6f 75 6e 64 3d 3d 30 20    if( bFound==0 
4172d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
4172e 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64  lite3Fts3SegRead
4172f 65 72 43 75 72 73 6f 72 28 70 2c 20 70 43 73 72  erCursor(p, pCsr
41730 2d 3e 69 4c 61 6e 67 69 64 2c 20 0a 20 20 20 20  ->iLangid, .    
41731 20 20 20 20 20 20 30 2c 20 46 54 53 33 5f 53 45        0, FTS3_SE
41732 47 43 55 52 53 4f 52 5f 41 4c 4c 2c 20 7a 54 65  GCURSOR_ALL, zTe
41733 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
41734 66 69 78 2c 20 30 2c 20 70 53 65 67 63 73 72 0a  fix, 0, pSegcsr.
41735 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 70        );.      p
41736 53 65 67 63 73 72 2d 3e 62 4c 6f 6f 6b 75 70 20  Segcsr->bLookup 
41737 3d 20 21 69 73 50 72 65 66 69 78 3b 0a 20 20 20  = !isPrefix;.   
41738 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 70 53 65 67   }.  }..  *ppSeg
41739 63 73 72 20 3d 20 70 53 65 67 63 73 72 3b 0a 20  csr = pSegcsr;. 
4173a 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
4173b 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 46 74 73  *.** Free an Fts
4173c 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72 20  3MultiSegReader 
4173d 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 66 74 73  allocated by fts
4173e 33 54 65 72 6d 53 65 67 52 65 61 64 65 72 43 75  3TermSegReaderCu
4173f 72 73 6f 72 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  rsor()..*/.stati
41740 63 20 76 6f 69 64 20 66 74 73 33 53 65 67 52 65  c void fts3SegRe
41741 61 64 65 72 43 75 72 73 6f 72 46 72 65 65 28 46  aderCursorFree(F
41742 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65  ts3MultiSegReade
41743 72 20 2a 70 53 65 67 63 73 72 29 7b 0a 20 20 73  r *pSegcsr){.  s
41744 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61  qlite3Fts3SegRea
41745 64 65 72 46 69 6e 69 73 68 28 70 53 65 67 63 73  derFinish(pSegcs
41746 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  r);.  sqlite3_fr
41747 65 65 28 70 53 65 67 63 73 72 29 3b 0a 7d 0a 0a  ee(pSegcsr);.}..
41748 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
41749 69 6f 6e 20 72 65 74 72 65 69 76 65 73 20 74 68  ion retreives th
4174a 65 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74 68  e doclist for th
4174b 65 20 73 70 65 63 69 66 69 65 64 20 74 65 72 6d  e specified term
4174c 20 28 6f 72 20 74 65 72 6d 0a 2a 2a 20 70 72 65   (or term.** pre
4174d 66 69 78 29 20 66 72 6f 6d 20 74 68 65 20 64 61  fix) from the da
4174e 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  tabase..*/.stati
4174f 63 20 69 6e 74 20 66 74 73 33 54 65 72 6d 53 65  c int fts3TermSe
41750 6c 65 63 74 28 0a 20 20 46 74 73 33 54 61 62 6c  lect(.  Fts3Tabl
41751 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  e *p,           
41752 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75          /* Virtu
41753 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20  al table handle 
41754 2a 2f 0a 20 20 46 74 73 33 50 68 72 61 73 65 54  */.  Fts3PhraseT
41755 6f 6b 65 6e 20 2a 70 54 6f 6b 2c 20 20 20 20 20  oken *pTok,     
41756 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 74 6f       /* Token to
41757 20 71 75 65 72 79 20 66 6f 72 20 2a 2f 0a 20 20   query for */.  
41758 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20  int iColumn,    
41759 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4175a 2f 2a 20 43 6f 6c 75 6d 6e 20 74 6f 20 71 75 65  /* Column to que
4175b 72 79 20 28 6f 72 20 2d 76 65 20 66 6f 72 20 61  ry (or -ve for a
4175c 6c 6c 20 63 6f 6c 75 6d 6e 73 29 20 2a 2f 0a 20  ll columns) */. 
4175d 20 69 6e 74 20 2a 70 6e 4f 75 74 2c 20 20 20 20   int *pnOut,    
4175e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4175f 20 2f 2a 20 4f 55 54 3a 20 53 69 7a 65 20 6f 66   /* OUT: Size of
41760 20 62 75 66 66 65 72 20 61 74 20 2a 70 70 4f 75   buffer at *ppOu
41761 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 70  t */.  char **pp
41762 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 20 20  Out             
41763 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d         /* OUT: M
41764 61 6c 6c 6f 63 65 64 20 72 65 73 75 6c 74 20 62  alloced result b
41765 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e  uffer */.){.  in
41766 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
41767 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
41768 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
41769 20 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65    Fts3MultiSegRe
4176a 61 64 65 72 20 2a 70 53 65 67 63 73 72 3b 20 20  ader *pSegcsr;  
4176b 20 20 2f 2a 20 53 65 67 2d 72 65 61 64 65 72 20    /* Seg-reader 
4176c 63 75 72 73 6f 72 20 66 6f 72 20 74 68 69 73 20  cursor for this 
4176d 74 65 72 6d 20 2a 2f 0a 20 20 54 65 72 6d 53 65  term */.  TermSe
4176e 6c 65 63 74 20 74 73 63 3b 20 20 20 20 20 20 20  lect tsc;       
4176f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 62 6a            /* Obj
41770 65 63 74 20 66 6f 72 20 70 61 69 72 2d 77 69 73  ect for pair-wis
41771 65 20 64 6f 63 6c 69 73 74 20 6d 65 72 67 69 6e  e doclist mergin
41772 67 20 2a 2f 0a 20 20 46 74 73 33 53 65 67 46 69  g */.  Fts3SegFi
41773 6c 74 65 72 20 66 69 6c 74 65 72 3b 20 20 20 20  lter filter;    
41774 20 20 20 20 20 20 20 2f 2a 20 53 65 67 6d 65 6e         /* Segmen
41775 74 20 74 65 72 6d 20 66 69 6c 74 65 72 20 63 6f  t term filter co
41776 6e 66 69 67 75 72 61 74 69 6f 6e 20 2a 2f 0a 0a  nfiguration */..
41777 20 20 70 53 65 67 63 73 72 20 3d 20 70 54 6f 6b    pSegcsr = pTok
41778 2d 3e 70 53 65 67 63 73 72 3b 0a 20 20 6d 65 6d  ->pSegcsr;.  mem
41779 73 65 74 28 26 74 73 63 2c 20 30 2c 20 73 69 7a  set(&tsc, 0, siz
4177a 65 6f 66 28 54 65 72 6d 53 65 6c 65 63 74 29 29  eof(TermSelect))
4177b 3b 0a 0a 20 20 66 69 6c 74 65 72 2e 66 6c 61 67  ;..  filter.flag
4177c 73 20 3d 20 46 54 53 33 5f 53 45 47 4d 45 4e 54  s = FTS3_SEGMENT
4177d 5f 49 47 4e 4f 52 45 5f 45 4d 50 54 59 20 7c 20  _IGNORE_EMPTY | 
4177e 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 52 45 51  FTS3_SEGMENT_REQ
4177f 55 49 52 45 5f 50 4f 53 0a 20 20 20 20 20 20 20  UIRE_POS.       
41780 20 7c 20 28 70 54 6f 6b 2d 3e 69 73 50 72 65 66   | (pTok->isPref
41781 69 78 20 3f 20 46 54 53 33 5f 53 45 47 4d 45 4e  ix ? FTS3_SEGMEN
41782 54 5f 50 52 45 46 49 58 20 3a 20 30 29 0a 20 20  T_PREFIX : 0).  
41783 20 20 20 20 20 20 7c 20 28 70 54 6f 6b 2d 3e 62        | (pTok->b
41784 46 69 72 73 74 20 3f 20 46 54 53 33 5f 53 45 47  First ? FTS3_SEG
41785 4d 45 4e 54 5f 46 49 52 53 54 20 3a 20 30 29 0a  MENT_FIRST : 0).
41786 20 20 20 20 20 20 20 20 7c 20 28 69 43 6f 6c 75          | (iColu
41787 6d 6e 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 3f 20  mn<p->nColumn ? 
41788 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 43 4f 4c  FTS3_SEGMENT_COL
41789 55 4d 4e 5f 46 49 4c 54 45 52 20 3a 20 30 29 3b  UMN_FILTER : 0);
4178a 0a 20 20 66 69 6c 74 65 72 2e 69 43 6f 6c 20 3d  .  filter.iCol =
4178b 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 66 69 6c 74   iColumn;.  filt
4178c 65 72 2e 7a 54 65 72 6d 20 3d 20 70 54 6f 6b 2d  er.zTerm = pTok-
4178d 3e 7a 3b 0a 20 20 66 69 6c 74 65 72 2e 6e 54 65  >z;.  filter.nTe
4178e 72 6d 20 3d 20 70 54 6f 6b 2d 3e 6e 3b 0a 0a 20  rm = pTok->n;.. 
4178f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
41790 33 53 65 67 52 65 61 64 65 72 53 74 61 72 74 28  3SegReaderStart(
41791 70 2c 20 70 53 65 67 63 73 72 2c 20 26 66 69 6c  p, pSegcsr, &fil
41792 74 65 72 29 3b 0a 20 20 77 68 69 6c 65 28 20 53  ter);.  while( S
41793 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 0a 20 20 20  QLITE_OK==rc.   
41794 20 20 20 26 26 20 53 51 4c 49 54 45 5f 52 4f 57     && SQLITE_ROW
41795 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 46  ==(rc = sqlite3F
41796 74 73 33 53 65 67 52 65 61 64 65 72 53 74 65 70  ts3SegReaderStep
41797 28 70 2c 20 70 53 65 67 63 73 72 29 29 20 0a 20  (p, pSegcsr)) . 
41798 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 74 73   ){.    rc = fts
41799 33 54 65 72 6d 53 65 6c 65 63 74 4d 65 72 67 65  3TermSelectMerge
4179a 28 70 2c 20 26 74 73 63 2c 20 70 53 65 67 63 73  (p, &tsc, pSegcs
4179b 72 2d 3e 61 44 6f 63 6c 69 73 74 2c 20 70 53 65  r->aDoclist, pSe
4179c 67 63 73 72 2d 3e 6e 44 6f 63 6c 69 73 74 29 3b  gcsr->nDoclist);
4179d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
4179e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
4179f 20 72 63 20 3d 20 66 74 73 33 54 65 72 6d 53 65   rc = fts3TermSe
417a0 6c 65 63 74 46 69 6e 69 73 68 4d 65 72 67 65 28  lectFinishMerge(
417a1 70 2c 20 26 74 73 63 29 3b 0a 20 20 7d 0a 20 20  p, &tsc);.  }.  
417a2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
417a3 4b 20 29 7b 0a 20 20 20 20 2a 70 70 4f 75 74 20  K ){.    *ppOut 
417a4 3d 20 74 73 63 2e 61 61 4f 75 74 70 75 74 5b 30  = tsc.aaOutput[0
417a5 5d 3b 0a 20 20 20 20 2a 70 6e 4f 75 74 20 3d 20  ];.    *pnOut = 
417a6 74 73 63 2e 61 6e 4f 75 74 70 75 74 5b 30 5d 3b  tsc.anOutput[0];
417a7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
417a8 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
417a9 3b 20 69 3c 53 69 7a 65 6f 66 41 72 72 61 79 28  ; i<SizeofArray(
417aa 74 73 63 2e 61 61 4f 75 74 70 75 74 29 3b 20 69  tsc.aaOutput); i
417ab 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
417ac 65 33 5f 66 72 65 65 28 74 73 63 2e 61 61 4f 75  e3_free(tsc.aaOu
417ad 74 70 75 74 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  tput[i]);.    }.
417ae 20 20 7d 0a 0a 20 20 66 74 73 33 53 65 67 52 65    }..  fts3SegRe
417af 61 64 65 72 43 75 72 73 6f 72 46 72 65 65 28 70  aderCursorFree(p
417b0 53 65 67 63 73 72 29 3b 0a 20 20 70 54 6f 6b 2d  Segcsr);.  pTok-
417b1 3e 70 53 65 67 63 73 72 20 3d 20 30 3b 0a 20 20  >pSegcsr = 0;.  
417b2 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
417b3 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
417b4 6e 20 63 6f 75 6e 74 73 20 74 68 65 20 74 6f 74  n counts the tot
417b5 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 64 6f 63  al number of doc
417b6 69 64 73 20 69 6e 20 74 68 65 20 64 6f 63 6c 69  ids in the docli
417b7 73 74 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20  st stored.** in 
417b8 62 75 66 66 65 72 20 61 4c 69 73 74 5b 5d 2c 20  buffer aList[], 
417b9 73 69 7a 65 20 6e 4c 69 73 74 20 62 79 74 65 73  size nList bytes
417ba 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69  ..**.** If the i
417bb 73 50 6f 73 6c 69 73 74 20 61 72 67 75 6d 65 6e  sPoslist argumen
417bc 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  t is true, then 
417bd 69 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68  it is assumed th
417be 61 74 20 74 68 65 20 64 6f 63 6c 69 73 74 0a 2a  at the doclist.*
417bf 2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 73  * contains a pos
417c0 69 74 69 6f 6e 2d 6c 69 73 74 20 66 6f 6c 6c 6f  ition-list follo
417c1 77 69 6e 67 20 65 61 63 68 20 64 6f 63 69 64 2e  wing each docid.
417c2 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 69   Otherwise, it i
417c3 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20 74 68 61  s assumed.** tha
417c4 74 20 74 68 65 20 64 6f 63 6c 69 73 74 20 69 73  t the doclist is
417c5 20 73 69 6d 70 6c 79 20 61 20 6c 69 73 74 20 6f   simply a list o
417c6 66 20 64 6f 63 69 64 73 20 73 74 6f 72 65 64 20  f docids stored 
417c7 61 73 20 64 65 6c 74 61 20 65 6e 63 6f 64 65 64  as delta encoded
417c8 20 0a 2a 2a 20 76 61 72 69 6e 74 73 2e 0a 2a 2f   .** varints..*/
417c9 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
417ca 44 6f 63 6c 69 73 74 43 6f 75 6e 74 44 6f 63 69  DoclistCountDoci
417cb 64 73 28 63 68 61 72 20 2a 61 4c 69 73 74 2c 20  ds(char *aList, 
417cc 69 6e 74 20 6e 4c 69 73 74 29 7b 0a 20 20 69 6e  int nList){.  in
417cd 74 20 6e 44 6f 63 20 3d 20 30 3b 20 20 20 20 20  t nDoc = 0;     
417ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
417cf 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f   Return value */
417d0 0a 20 20 69 66 28 20 61 4c 69 73 74 20 29 7b 0a  .  if( aList ){.
417d1 20 20 20 20 63 68 61 72 20 2a 61 45 6e 64 20 3d      char *aEnd =
417d2 20 26 61 4c 69 73 74 5b 6e 4c 69 73 74 5d 3b 20   &aList[nList]; 
417d3 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
417d4 6f 6e 65 20 62 79 74 65 20 61 66 74 65 72 20 45  one byte after E
417d5 4f 46 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a  OF */.    char *
417d6 70 20 3d 20 61 4c 69 73 74 3b 20 20 20 20 20 20  p = aList;      
417d7 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
417d8 72 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20  r */.    while( 
417d9 70 3c 61 45 6e 64 20 29 7b 0a 20 20 20 20 20 20  p<aEnd ){.      
417da 6e 44 6f 63 2b 2b 3b 0a 20 20 20 20 20 20 77 68  nDoc++;.      wh
417db 69 6c 65 28 20 28 2a 70 2b 2b 29 26 30 78 38 30  ile( (*p++)&0x80
417dc 20 29 3b 20 20 20 20 20 2f 2a 20 53 6b 69 70 20   );     /* Skip 
417dd 64 6f 63 69 64 20 76 61 72 69 6e 74 20 2a 2f 0a  docid varint */.
417de 20 20 20 20 20 20 66 74 73 33 50 6f 73 6c 69 73        fts3Poslis
417df 74 43 6f 70 79 28 30 2c 20 26 70 29 3b 20 20 20  tCopy(0, &p);   
417e0 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20 70 6f 73  /* Skip over pos
417e1 69 74 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20  ition list */.  
417e2 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
417e3 6e 20 6e 44 6f 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n nDoc;.}../*.**
417e4 20 41 64 76 61 6e 63 65 20 74 68 65 20 63 75 72   Advance the cur
417e5 73 6f 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20  sor to the next 
417e6 72 6f 77 20 69 6e 20 74 68 65 20 25 5f 63 6f 6e  row in the %_con
417e7 74 65 6e 74 20 74 61 62 6c 65 20 74 68 61 74 0a  tent table that.
417e8 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20 73  ** matches the s
417e9 65 61 72 63 68 20 63 72 69 74 65 72 69 61 2e 20  earch criteria. 
417ea 20 46 6f 72 20 61 20 4d 41 54 43 48 20 73 65 61   For a MATCH sea
417eb 72 63 68 2c 20 74 68 69 73 20 77 69 6c 6c 20 62  rch, this will b
417ec 65 0a 2a 2a 20 74 68 65 20 6e 65 78 74 20 72 6f  e.** the next ro
417ed 77 20 74 68 61 74 20 6d 61 74 63 68 65 73 2e 20  w that matches. 
417ee 46 6f 72 20 61 20 66 75 6c 6c 2d 74 61 62 6c 65  For a full-table
417ef 20 73 63 61 6e 2c 20 74 68 69 73 20 77 69 6c 6c   scan, this will
417f0 20 62 65 0a 2a 2a 20 73 69 6d 70 6c 79 20 74 68   be.** simply th
417f1 65 20 6e 65 78 74 20 72 6f 77 20 69 6e 20 74 68  e next row in th
417f2 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c  e %_content tabl
417f3 65 2e 20 20 46 6f 72 20 61 20 64 6f 63 69 64 20  e.  For a docid 
417f4 6c 6f 6f 6b 75 70 2c 0a 2a 2a 20 74 68 69 73 20  lookup,.** this 
417f5 72 6f 75 74 69 6e 65 20 73 69 6d 70 6c 79 20 73  routine simply s
417f6 65 74 73 20 74 68 65 20 45 4f 46 20 66 6c 61 67  ets the EOF flag
417f7 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
417f8 51 4c 49 54 45 5f 4f 4b 20 69 66 20 6e 6f 74 68  QLITE_OK if noth
417f9 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 20  ing goes wrong. 
417fa 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
417fb 74 75 72 6e 65 64 0a 2a 2a 20 65 76 65 6e 20 69  turned.** even i
417fc 66 20 77 65 20 72 65 61 63 68 20 65 6e 64 2d 6f  f we reach end-o
417fd 66 2d 66 69 6c 65 2e 20 20 54 68 65 20 66 74 73  f-file.  The fts
417fe 33 45 6f 66 4d 65 74 68 6f 64 28 29 20 77 69 6c  3EofMethod() wil
417ff 6c 20 62 65 20 63 61 6c 6c 65 64 0a 2a 2a 20 73  l be called.** s
41800 75 62 73 65 71 75 65 6e 74 6c 79 20 74 6f 20 64  ubsequently to d
41801 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
41802 20 6f 72 20 6e 6f 74 20 61 6e 20 45 4f 46 20 77   or not an EOF w
41803 61 73 20 68 69 74 2e 0a 2a 2f 0a 73 74 61 74 69  as hit..*/.stati
41804 63 20 69 6e 74 20 66 74 73 33 4e 65 78 74 4d 65  c int fts3NextMe
41805 74 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61  thod(sqlite3_vta
41806 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  b_cursor *pCurso
41807 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  r){.  int rc;.  
41808 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72  Fts3Cursor *pCsr
41809 20 3d 20 28 46 74 73 33 43 75 72 73 6f 72 20 2a   = (Fts3Cursor *
4180a 29 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20  )pCursor;.  if( 
4180b 70 43 73 72 2d 3e 65 53 65 61 72 63 68 3d 3d 46  pCsr->eSearch==F
4180c 54 53 33 5f 44 4f 43 49 44 5f 53 45 41 52 43 48  TS3_DOCID_SEARCH
4180d 20 7c 7c 20 70 43 73 72 2d 3e 65 53 65 61 72 63   || pCsr->eSearc
4180e 68 3d 3d 46 54 53 33 5f 46 55 4c 4c 53 43 41 4e  h==FTS3_FULLSCAN
4180f 5f 53 45 41 52 43 48 20 29 7b 0a 20 20 20 20 69  _SEARCH ){.    i
41810 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 21 3d 73  f( SQLITE_ROW!=s
41811 71 6c 69 74 65 33 5f 73 74 65 70 28 70 43 73 72  qlite3_step(pCsr
41812 2d 3e 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20  ->pStmt) ){.    
41813 20 20 70 43 73 72 2d 3e 69 73 45 6f 66 20 3d 20    pCsr->isEof = 
41814 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  1;.      rc = sq
41815 6c 69 74 65 33 5f 72 65 73 65 74 28 70 43 73 72  lite3_reset(pCsr
41816 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 7d 65  ->pStmt);.    }e
41817 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 73 72 2d  lse{.      pCsr-
41818 3e 69 50 72 65 76 49 64 20 3d 20 73 71 6c 69 74  >iPrevId = sqlit
41819 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
4181a 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20 30 29 3b  pCsr->pStmt, 0);
4181b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
4181c 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  TE_OK;.    }.  }
4181d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 66  else{.    rc = f
4181e 74 73 33 45 76 61 6c 4e 65 78 74 28 28 46 74 73  ts3EvalNext((Fts
4181f 33 43 75 72 73 6f 72 20 2a 29 70 43 75 72 73 6f  3Cursor *)pCurso
41820 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  r);.  }.  assert
41821 28 20 28 28 46 74 73 33 54 61 62 6c 65 20 2a 29  ( ((Fts3Table *)
41822 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62  pCsr->base.pVtab
41823 29 2d 3e 70 53 65 67 6d 65 6e 74 73 3d 3d 30 20  )->pSegments==0 
41824 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
41825 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  }../*.** This is
41826 20 74 68 65 20 78 46 69 6c 74 65 72 20 69 6e 74   the xFilter int
41827 65 72 66 61 63 65 20 66 6f 72 20 74 68 65 20 76  erface for the v
41828 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 53  irtual table.  S
41829 65 65 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 61  ee.** the virtua
4182a 6c 20 74 61 62 6c 65 20 78 46 69 6c 74 65 72 20  l table xFilter 
4182b 6d 65 74 68 6f 64 20 64 6f 63 75 6d 65 6e 74 61  method documenta
4182c 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f  tion for additio
4182d 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  nal.** informati
4182e 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 64 78  on..**.** If idx
4182f 4e 75 6d 3d 3d 46 54 53 33 5f 46 55 4c 4c 53 43  Num==FTS3_FULLSC
41830 41 4e 5f 53 45 41 52 43 48 20 74 68 65 6e 20 64  AN_SEARCH then d
41831 6f 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73  o a full table s
41832 63 61 6e 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74  can against.** t
41833 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62  he %_content tab
41834 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 64 78  le..**.** If idx
41835 4e 75 6d 3d 3d 46 54 53 33 5f 44 4f 43 49 44 5f  Num==FTS3_DOCID_
41836 53 45 41 52 43 48 20 74 68 65 6e 20 64 6f 20 61  SEARCH then do a
41837 20 64 6f 63 69 64 20 6c 6f 6f 6b 75 70 20 66 6f   docid lookup fo
41838 72 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79  r a single entry
41839 0a 2a 2a 20 69 6e 20 74 68 65 20 25 5f 63 6f 6e  .** in the %_con
4183a 74 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  tent table..**.*
4183b 2a 20 49 66 20 69 64 78 4e 75 6d 3e 3d 46 54 53  * If idxNum>=FTS
4183c 33 5f 46 55 4c 4c 54 45 58 54 5f 53 45 41 52 43  3_FULLTEXT_SEARC
4183d 48 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 66  H then use the f
4183e 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 2e 20  ull text index. 
4183f 20 54 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6f   The.** column o
41840 6e 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64 20  n the left-hand 
41841 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54 43  side of the MATC
41842 48 20 6f 70 65 72 61 74 6f 72 20 69 73 20 63 6f  H operator is co
41843 6c 75 6d 6e 0a 2a 2a 20 6e 75 6d 62 65 72 20 69  lumn.** number i
41844 64 78 4e 75 6d 2d 46 54 53 33 5f 46 55 4c 4c 54  dxNum-FTS3_FULLT
41845 45 58 54 5f 53 45 41 52 43 48 2c 20 30 20 69 6e  EXT_SEARCH, 0 in
41846 64 65 78 65 64 2e 20 20 61 72 67 76 5b 30 5d 20  dexed.  argv[0] 
41847 69 73 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  is the right-han
41848 64 0a 2a 2a 20 73 69 64 65 20 6f 66 20 74 68 65  d.** side of the
41849 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 2e   MATCH operator.
4184a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
4184b 74 73 33 46 69 6c 74 65 72 4d 65 74 68 6f 64 28  ts3FilterMethod(
4184c 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
4184d 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
4184e 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72     /* The cursor
4184f 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20 71   used for this q
41850 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 64  uery */.  int id
41851 78 4e 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20  xNum,           
41852 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72            /* Str
41853 61 74 65 67 79 20 69 6e 64 65 78 20 2a 2f 0a 20  ategy index */. 
41854 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78   const char *idx
41855 53 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  Str,            
41856 20 2f 2a 20 55 6e 75 73 65 64 20 2a 2f 0a 20 20   /* Unused */.  
41857 69 6e 74 20 6e 56 61 6c 2c 20 20 20 20 20 20 20  int nVal,       
41858 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41859 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65  /* Number of ele
4185a 6d 65 6e 74 73 20 69 6e 20 61 70 56 61 6c 20 2a  ments in apVal *
4185b 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  /.  sqlite3_valu
4185c 65 20 2a 2a 61 70 56 61 6c 20 20 20 20 20 20 20  e **apVal       
4185d 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73      /* Arguments
4185e 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 69 6e   for the indexin
4185f 67 20 73 63 68 65 6d 65 20 2a 2f 0a 29 7b 0a 20  g scheme */.){. 
41860 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72 20   int rc;.  char 
41861 2a 7a 53 71 6c 3b 20 20 20 20 20 20 20 20 20 20  *zSql;          
41862 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51             /* SQ
41863 4c 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64  L statement used
41864 20 74 6f 20 61 63 63 65 73 73 20 25 5f 63 6f 6e   to access %_con
41865 74 65 6e 74 20 2a 2f 0a 20 20 46 74 73 33 54 61  tent */.  Fts3Ta
41866 62 6c 65 20 2a 70 20 3d 20 28 46 74 73 33 54 61  ble *p = (Fts3Ta
41867 62 6c 65 20 2a 29 70 43 75 72 73 6f 72 2d 3e 70  ble *)pCursor->p
41868 56 74 61 62 3b 0a 20 20 46 74 73 33 43 75 72 73  Vtab;.  Fts3Curs
41869 6f 72 20 2a 70 43 73 72 20 3d 20 28 46 74 73 33  or *pCsr = (Fts3
4186a 43 75 72 73 6f 72 20 2a 29 70 43 75 72 73 6f 72  Cursor *)pCursor
4186b 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
4186c 4d 45 54 45 52 28 69 64 78 53 74 72 29 3b 0a 20  METER(idxStr);. 
4186d 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
4186e 52 28 6e 56 61 6c 29 3b 0a 0a 20 20 61 73 73 65  R(nVal);..  asse
4186f 72 74 28 20 69 64 78 4e 75 6d 3e 3d 30 20 26 26  rt( idxNum>=0 &&
41870 20 69 64 78 4e 75 6d 3c 3d 28 46 54 53 33 5f 46   idxNum<=(FTS3_F
41871 55 4c 4c 54 45 58 54 5f 53 45 41 52 43 48 2b 70  ULLTEXT_SEARCH+p
41872 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 29 3b 0a 20 20  ->nColumn) );.  
41873 61 73 73 65 72 74 28 20 6e 56 61 6c 3d 3d 30 20  assert( nVal==0 
41874 7c 7c 20 6e 56 61 6c 3d 3d 31 20 7c 7c 20 6e 56  || nVal==1 || nV
41875 61 6c 3d 3d 32 20 29 3b 0a 20 20 61 73 73 65 72  al==2 );.  asser
41876 74 28 20 28 6e 56 61 6c 3d 3d 30 29 3d 3d 28 69  t( (nVal==0)==(i
41877 64 78 4e 75 6d 3d 3d 46 54 53 33 5f 46 55 4c 4c  dxNum==FTS3_FULL
41878 53 43 41 4e 5f 53 45 41 52 43 48 29 20 29 3b 0a  SCAN_SEARCH) );.
41879 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 53 65    assert( p->pSe
4187a 67 6d 65 6e 74 73 3d 3d 30 20 29 3b 0a 0a 20 20  gments==0 );..  
4187b 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20 63  /* In case the c
4187c 75 72 73 6f 72 20 68 61 73 20 62 65 65 6e 20 75  ursor has been u
4187d 73 65 64 20 62 65 66 6f 72 65 2c 20 63 6c 65 61  sed before, clea
4187e 72 20 69 74 20 6e 6f 77 2e 20 2a 2f 0a 20 20 73  r it now. */.  s
4187f 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
41880 70 43 73 72 2d 3e 70 53 74 6d 74 29 3b 0a 20 20  pCsr->pStmt);.  
41881 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73  sqlite3_free(pCs
41882 72 2d 3e 61 44 6f 63 6c 69 73 74 29 3b 0a 20 20  r->aDoclist);.  
41883 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46  sqlite3Fts3ExprF
41884 72 65 65 28 70 43 73 72 2d 3e 70 45 78 70 72 29  ree(pCsr->pExpr)
41885 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 43 75 72  ;.  memset(&pCur
41886 73 6f 72 5b 31 5d 2c 20 30 2c 20 73 69 7a 65 6f  sor[1], 0, sizeo
41887 66 28 46 74 73 33 43 75 72 73 6f 72 29 2d 73 69  f(Fts3Cursor)-si
41888 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61  zeof(sqlite3_vta
41889 62 5f 63 75 72 73 6f 72 29 29 3b 0a 0a 20 20 69  b_cursor));..  i
4188a 66 28 20 69 64 78 53 74 72 20 29 7b 0a 20 20 20  f( idxStr ){.   
4188b 20 70 43 73 72 2d 3e 62 44 65 73 63 20 3d 20 28   pCsr->bDesc = (
4188c 69 64 78 53 74 72 5b 30 5d 3d 3d 27 44 27 29 3b  idxStr[0]=='D');
4188d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43  .  }else{.    pC
4188e 73 72 2d 3e 62 44 65 73 63 20 3d 20 70 2d 3e 62  sr->bDesc = p->b
4188f 44 65 73 63 49 64 78 3b 0a 20 20 7d 0a 20 20 70  DescIdx;.  }.  p
41890 43 73 72 2d 3e 65 53 65 61 72 63 68 20 3d 20 28  Csr->eSearch = (
41891 69 31 36 29 69 64 78 4e 75 6d 3b 0a 0a 20 20 69  i16)idxNum;..  i
41892 66 28 20 69 64 78 4e 75 6d 21 3d 46 54 53 33 5f  f( idxNum!=FTS3_
41893 44 4f 43 49 44 5f 53 45 41 52 43 48 20 26 26 20  DOCID_SEARCH && 
41894 69 64 78 4e 75 6d 21 3d 46 54 53 33 5f 46 55 4c  idxNum!=FTS3_FUL
41895 4c 53 43 41 4e 5f 53 45 41 52 43 48 20 29 7b 0a  LSCAN_SEARCH ){.
41896 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 69      int iCol = i
41897 64 78 4e 75 6d 2d 46 54 53 33 5f 46 55 4c 4c 54  dxNum-FTS3_FULLT
41898 45 58 54 5f 53 45 41 52 43 48 3b 0a 20 20 20 20  EXT_SEARCH;.    
41899 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 51 75 65  const char *zQue
4189a 72 79 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  ry = (const char
4189b 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
4189c 5f 74 65 78 74 28 61 70 56 61 6c 5b 30 5d 29 3b  _text(apVal[0]);
4189d 0a 0a 20 20 20 20 69 66 28 20 7a 51 75 65 72 79  ..    if( zQuery
4189e 3d 3d 30 20 26 26 20 73 71 6c 69 74 65 33 5f 76  ==0 && sqlite3_v
4189f 61 6c 75 65 5f 74 79 70 65 28 61 70 56 61 6c 5b  alue_type(apVal[
418a0 30 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  0])!=SQLITE_NULL
418a1 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
418a2 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
418a3 20 20 20 7d 0a 0a 20 20 20 20 70 43 73 72 2d 3e     }..    pCsr->
418a4 69 4c 61 6e 67 69 64 20 3d 20 30 3b 0a 20 20 20  iLangid = 0;.   
418a5 20 69 66 28 20 6e 56 61 6c 3d 3d 32 20 29 20 70   if( nVal==2 ) p
418a6 43 73 72 2d 3e 69 4c 61 6e 67 69 64 20 3d 20 73  Csr->iLangid = s
418a7 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
418a8 28 61 70 56 61 6c 5b 31 5d 29 3b 0a 0a 20 20 20  (apVal[1]);..   
418a9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
418aa 33 45 78 70 72 50 61 72 73 65 28 70 2d 3e 70 54  3ExprParse(p->pT
418ab 6f 6b 65 6e 69 7a 65 72 2c 20 70 43 73 72 2d 3e  okenizer, pCsr->
418ac 69 4c 61 6e 67 69 64 2c 0a 20 20 20 20 20 20 20  iLangid,.       
418ad 20 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 2c 20 70 2d   p->azColumn, p-
418ae 3e 62 46 74 73 34 2c 20 70 2d 3e 6e 43 6f 6c 75  >bFts4, p->nColu
418af 6d 6e 2c 20 69 43 6f 6c 2c 20 7a 51 75 65 72 79  mn, iCol, zQuery
418b0 2c 20 2d 31 2c 20 26 70 43 73 72 2d 3e 70 45 78  , -1, &pCsr->pEx
418b1 70 72 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66  pr.    );.    if
418b2 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
418b3 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  ){.      if( rc=
418b4 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 7b  =SQLITE_ERROR ){
418b5 0a 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20  .        static 
418b6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72  const char *zErr
418b7 20 3d 20 22 6d 61 6c 66 6f 72 6d 65 64 20 4d 41   = "malformed MA
418b8 54 43 48 20 65 78 70 72 65 73 73 69 6f 6e 3a 20  TCH expression: 
418b9 5b 25 73 5d 22 3b 0a 20 20 20 20 20 20 20 20 70  [%s]";.        p
418ba 2d 3e 62 61 73 65 2e 7a 45 72 72 4d 73 67 20 3d  ->base.zErrMsg =
418bb 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
418bc 28 7a 45 72 72 2c 20 7a 51 75 65 72 79 29 3b 0a  (zErr, zQuery);.
418bd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
418be 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a  turn rc;.    }..
418bf 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
418c0 46 74 73 33 52 65 61 64 4c 6f 63 6b 28 70 29 3b  Fts3ReadLock(p);
418c1 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
418c2 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
418c3 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d 20 66 74  rc;..    rc = ft
418c4 73 33 45 76 61 6c 53 74 61 72 74 28 70 43 73 72  s3EvalStart(pCsr
418c5 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 46  );..    sqlite3F
418c6 74 73 33 53 65 67 6d 65 6e 74 73 43 6c 6f 73 65  ts3SegmentsClose
418c7 28 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  (p);.    if( rc!
418c8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
418c9 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 43 73 72  urn rc;.    pCsr
418ca 2d 3e 70 4e 65 78 74 49 64 20 3d 20 70 43 73 72  ->pNextId = pCsr
418cb 2d 3e 61 44 6f 63 6c 69 73 74 3b 0a 20 20 20 20  ->aDoclist;.    
418cc 70 43 73 72 2d 3e 69 50 72 65 76 49 64 20 3d 20  pCsr->iPrevId = 
418cd 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d  0;.  }..  /* Com
418ce 70 69 6c 65 20 61 20 53 45 4c 45 43 54 20 73 74  pile a SELECT st
418cf 61 74 65 6d 65 6e 74 20 66 6f 72 20 74 68 69 73  atement for this
418d0 20 63 75 72 73 6f 72 2e 20 46 6f 72 20 61 20 66   cursor. For a f
418d1 75 6c 6c 2d 74 61 62 6c 65 2d 73 63 61 6e 2c 20  ull-table-scan, 
418d2 74 68 65 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  the.  ** stateme
418d3 6e 74 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68  nt loops through
418d4 20 61 6c 6c 20 72 6f 77 73 20 6f 66 20 74 68 65   all rows of the
418d5 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65   %_content table
418d6 2e 20 46 6f 72 20 61 0a 20 20 2a 2a 20 66 75 6c  . For a.  ** ful
418d7 6c 2d 74 65 78 74 20 71 75 65 72 79 20 6f 72 20  l-text query or 
418d8 64 6f 63 69 64 20 6c 6f 6f 6b 75 70 2c 20 74 68  docid lookup, th
418d9 65 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74 72  e statement retr
418da 69 65 76 65 73 20 61 20 73 69 6e 67 6c 65 0a 20  ieves a single. 
418db 20 2a 2a 20 72 6f 77 20 62 79 20 64 6f 63 69 64   ** row by docid
418dc 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 64 78  ..  */.  if( idx
418dd 4e 75 6d 3d 3d 46 54 53 33 5f 46 55 4c 4c 53 43  Num==FTS3_FULLSC
418de 41 4e 5f 53 45 41 52 43 48 20 29 7b 0a 20 20 20  AN_SEARCH ){.   
418df 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f   zSql = sqlite3_
418e0 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20 20  mprintf(.       
418e1 20 22 53 45 4c 45 43 54 20 25 73 20 4f 52 44 45   "SELECT %s ORDE
418e2 52 20 42 59 20 72 6f 77 69 64 20 25 73 22 2c 0a  R BY rowid %s",.
418e3 20 20 20 20 20 20 20 20 70 2d 3e 7a 52 65 61 64          p->zRead
418e4 45 78 70 72 6c 69 73 74 2c 20 28 70 43 73 72 2d  Exprlist, (pCsr-
418e5 3e 62 44 65 73 63 20 3f 20 22 44 45 53 43 22 20  >bDesc ? "DESC" 
418e6 3a 20 22 41 53 43 22 29 0a 20 20 20 20 29 3b 0a  : "ASC").    );.
418e7 20 20 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a      if( zSql ){.
418e8 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
418e9 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 70 2d  e3_prepare_v2(p-
418ea 3e 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26  >db, zSql, -1, &
418eb 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20 30 29 3b  pCsr->pStmt, 0);
418ec 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
418ed 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 20 20 7d  ree(zSql);.    }
418ee 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
418ef 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
418f0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
418f1 20 69 64 78 4e 75 6d 3d 3d 46 54 53 33 5f 44 4f   idxNum==FTS3_DO
418f2 43 49 44 5f 53 45 41 52 43 48 20 29 7b 0a 20 20  CID_SEARCH ){.  
418f3 20 20 72 63 20 3d 20 66 74 73 33 43 75 72 73 6f    rc = fts3Curso
418f4 72 53 65 65 6b 53 74 6d 74 28 70 43 73 72 2c 20  rSeekStmt(pCsr, 
418f5 26 70 43 73 72 2d 3e 70 53 74 6d 74 29 3b 0a 20  &pCsr->pStmt);. 
418f6 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
418f7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
418f8 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
418f9 76 61 6c 75 65 28 70 43 73 72 2d 3e 70 53 74 6d  value(pCsr->pStm
418fa 74 2c 20 31 2c 20 61 70 56 61 6c 5b 30 5d 29 3b  t, 1, apVal[0]);
418fb 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
418fc 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
418fd 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
418fe 65 74 75 72 6e 20 66 74 73 33 4e 65 78 74 4d 65  eturn fts3NextMe
418ff 74 68 6f 64 28 70 43 75 72 73 6f 72 29 3b 0a 7d  thod(pCursor);.}
41900 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 69 73  ../* .** This is
41901 20 74 68 65 20 78 45 6f 66 20 6d 65 74 68 6f 64   the xEof method
41902 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
41903 74 61 62 6c 65 2e 20 53 51 4c 69 74 65 20 63 61  table. SQLite ca
41904 6c 6c 73 20 74 68 69 73 20 0a 2a 2a 20 72 6f 75  lls this .** rou
41905 74 69 6e 65 20 74 6f 20 66 69 6e 64 20 6f 75 74  tine to find out
41906 20 69 66 20 69 74 20 68 61 73 20 72 65 61 63 68   if it has reach
41907 65 64 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20  ed the end of a 
41908 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 73  result set..*/.s
41909 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 6f  tatic int fts3Eo
4190a 66 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f  fMethod(sqlite3_
4190b 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
4190c 72 73 6f 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  rsor){.  return 
4190d 28 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29 70  ((Fts3Cursor *)p
4190e 43 75 72 73 6f 72 29 2d 3e 69 73 45 6f 66 3b 0a  Cursor)->isEof;.
4190f 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 69  }../* .** This i
41910 73 20 74 68 65 20 78 52 6f 77 69 64 20 6d 65 74  s the xRowid met
41911 68 6f 64 2e 20 54 68 65 20 53 51 4c 69 74 65 20  hod. The SQLite 
41912 63 6f 72 65 20 63 61 6c 6c 73 20 74 68 69 73 20  core calls this 
41913 72 6f 75 74 69 6e 65 20 74 6f 0a 2a 2a 20 72 65  routine to.** re
41914 74 72 69 65 76 65 20 74 68 65 20 72 6f 77 69 64  trieve the rowid
41915 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
41916 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75   row of the resu
41917 6c 74 20 73 65 74 2e 20 66 74 73 33 0a 2a 2a 20  lt set. fts3.** 
41918 65 78 70 6f 73 65 73 20 25 5f 63 6f 6e 74 65 6e  exposes %_conten
41919 74 2e 64 6f 63 69 64 20 61 73 20 74 68 65 20 72  t.docid as the r
4191a 6f 77 69 64 20 66 6f 72 20 74 68 65 20 76 69 72  owid for the vir
4191b 74 75 61 6c 20 74 61 62 6c 65 2e 20 54 68 65 0a  tual table. The.
4191c 2a 2a 20 72 6f 77 69 64 20 73 68 6f 75 6c 64 20  ** rowid should 
4191d 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70  be written to *p
4191e 52 6f 77 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  Rowid..*/.static
4191f 20 69 6e 74 20 66 74 73 33 52 6f 77 69 64 4d 65   int fts3RowidMe
41920 74 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61  thod(sqlite3_vta
41921 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  b_cursor *pCurso
41922 72 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  r, sqlite_int64 
41923 2a 70 52 6f 77 69 64 29 7b 0a 20 20 46 74 73 33  *pRowid){.  Fts3
41924 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28  Cursor *pCsr = (
41925 46 74 73 33 43 75 72 73 6f 72 20 2a 29 20 70 43  Fts3Cursor *) pC
41926 75 72 73 6f 72 3b 0a 20 20 2a 70 52 6f 77 69 64  ursor;.  *pRowid
41927 20 3d 20 70 43 73 72 2d 3e 69 50 72 65 76 49 64   = pCsr->iPrevId
41928 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
41929 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  E_OK;.}../* .** 
4192a 54 68 69 73 20 69 73 20 74 68 65 20 78 43 6f 6c  This is the xCol
4192b 75 6d 6e 20 6d 65 74 68 6f 64 2c 20 63 61 6c 6c  umn method, call
4192c 65 64 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20  ed by SQLite to 
4192d 72 65 71 75 65 73 74 20 61 20 76 61 6c 75 65 20  request a value 
4192e 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 72 6f 77 20  from.** the row 
4192f 74 68 61 74 20 74 68 65 20 73 75 70 70 6c 69 65  that the supplie
41930 64 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74  d cursor current
41931 6c 79 20 70 6f 69 6e 74 73 20 74 6f 2e 0a 2a 2a  ly points to..**
41932 0a 2a 2a 20 49 66 3a 0a 2a 2a 0a 2a 2a 20 20 20  .** If:.**.**   
41933 28 69 43 6f 6c 20 3c 20 20 70 2d 3e 6e 43 6f 6c  (iCol <  p->nCol
41934 75 6d 6e 29 20 20 20 2d 3e 20 54 68 65 20 76 61  umn)   -> The va
41935 6c 75 65 20 6f 66 20 74 68 65 20 69 43 6f 6c 27  lue of the iCol'
41936 74 68 20 75 73 65 72 20 63 6f 6c 75 6d 6e 2e 0a  th user column..
41937 2a 2a 20 20 20 28 69 43 6f 6c 20 3d 3d 20 70 2d  **   (iCol == p-
41938 3e 6e 43 6f 6c 75 6d 6e 29 20 20 20 2d 3e 20 4d  >nColumn)   -> M
41939 61 67 69 63 20 63 6f 6c 75 6d 6e 20 77 69 74 68  agic column with
4193a 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 61   the same name a
4193b 73 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 20  s the table..** 
4193c 20 20 28 69 43 6f 6c 20 3d 3d 20 70 2d 3e 6e 43    (iCol == p->nC
4193d 6f 6c 75 6d 6e 2b 31 29 20 2d 3e 20 44 6f 63 69  olumn+1) -> Doci
4193e 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 28 69  d column.**   (i
4193f 43 6f 6c 20 3d 3d 20 70 2d 3e 6e 43 6f 6c 75 6d  Col == p->nColum
41940 6e 2b 32 29 20 2d 3e 20 4c 61 6e 67 69 64 20 63  n+2) -> Langid c
41941 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 61 74 69 63 20  olumn.*/.static 
41942 69 6e 74 20 66 74 73 33 43 6f 6c 75 6d 6e 4d 65  int fts3ColumnMe
41943 74 68 6f 64 28 0a 20 20 73 71 6c 69 74 65 33 5f  thod(.  sqlite3_
41944 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
41945 72 73 6f 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f  rsor,   /* Curso
41946 72 20 74 6f 20 72 65 74 72 69 65 76 65 20 76 61  r to retrieve va
41947 6c 75 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 73 71  lue from */.  sq
41948 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
41949 43 74 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Ctx,          /*
4194a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 73 71 6c   Context for sql
4194b 69 74 65 33 5f 72 65 73 75 6c 74 5f 78 78 78 28  ite3_result_xxx(
4194c 29 20 63 61 6c 6c 73 20 2a 2f 0a 20 20 69 6e 74  ) calls */.  int
4194d 20 69 43 6f 6c 20 20 20 20 20 20 20 20 20 20 20   iCol           
4194e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4194f 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20  Index of column 
41950 74 6f 20 72 65 61 64 20 76 61 6c 75 65 20 66 72  to read value fr
41951 6f 6d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  om */.){.  int r
41952 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
41953 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
41954 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 46  turn Code */.  F
41955 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72 20  ts3Cursor *pCsr 
41956 3d 20 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29  = (Fts3Cursor *)
41957 20 70 43 75 72 73 6f 72 3b 0a 20 20 46 74 73 33   pCursor;.  Fts3
41958 54 61 62 6c 65 20 2a 70 20 3d 20 28 46 74 73 33  Table *p = (Fts3
41959 54 61 62 6c 65 20 2a 29 70 43 75 72 73 6f 72 2d  Table *)pCursor-
4195a 3e 70 56 74 61 62 3b 0a 0a 20 20 2f 2a 20 54 68  >pVtab;..  /* Th
4195b 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 73  e column value s
4195c 75 70 70 6c 69 65 64 20 62 79 20 53 51 4c 69 74  upplied by SQLit
4195d 65 20 6d 75 73 74 20 62 65 20 69 6e 20 72 61 6e  e must be in ran
4195e 67 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ge. */.  assert(
4195f 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c   iCol>=0 && iCol
41960 3c 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 20 29  <=p->nColumn+2 )
41961 3b 0a 0a 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70  ;..  if( iCol==p
41962 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 20 29 7b 0a 20  ->nColumn+1 ){. 
41963 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20     /* This call 
41964 69 73 20 61 20 72 65 71 75 65 73 74 20 66 6f 72  is a request for
41965 20 74 68 65 20 22 64 6f 63 69 64 22 20 63 6f 6c   the "docid" col
41966 75 6d 6e 2e 20 53 69 6e 63 65 20 22 64 6f 63 69  umn. Since "doci
41967 64 22 20 69 73 20 61 6e 20 0a 20 20 20 20 2a 2a  d" is an .    **
41968 20 61 6c 69 61 73 20 66 6f 72 20 22 72 6f 77 69   alias for "rowi
41969 64 22 2c 20 75 73 65 20 74 68 65 20 78 52 6f 77  d", use the xRow
4196a 69 64 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 6f  id() method to o
4196b 62 74 61 69 6e 20 74 68 65 20 76 61 6c 75 65 2e  btain the value.
4196c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
4196d 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34  te3_result_int64
4196e 28 70 43 74 78 2c 20 70 43 73 72 2d 3e 69 50 72  (pCtx, pCsr->iPr
4196f 65 76 49 64 29 3b 0a 20 20 7d 65 6c 73 65 20 69  evId);.  }else i
41970 66 28 20 69 43 6f 6c 3d 3d 70 2d 3e 6e 43 6f 6c  f( iCol==p->nCol
41971 75 6d 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  umn ){.    /* Th
41972 65 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 20 77  e extra column w
41973 68 6f 73 65 20 6e 61 6d 65 20 69 73 20 74 68 65  hose name is the
41974 20 73 61 6d 65 20 61 73 20 74 68 65 20 74 61 62   same as the tab
41975 6c 65 2e 0a 20 20 20 20 2a 2a 20 52 65 74 75 72  le..    ** Retur
41976 6e 20 61 20 62 6c 6f 62 20 77 68 69 63 68 20 69  n a blob which i
41977 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
41978 68 65 20 63 75 72 73 6f 72 2e 20 20 2a 2f 0a 20  he cursor.  */. 
41979 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
4197a 74 5f 62 6c 6f 62 28 70 43 74 78 2c 20 26 70 43  t_blob(pCtx, &pC
4197b 73 72 2c 20 73 69 7a 65 6f 66 28 70 43 73 72 29  sr, sizeof(pCsr)
4197c 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
4197d 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  NT);.  }else if(
4197e 20 69 43 6f 6c 3d 3d 70 2d 3e 6e 43 6f 6c 75 6d   iCol==p->nColum
4197f 6e 2b 32 20 26 26 20 70 43 73 72 2d 3e 70 45 78  n+2 && pCsr->pEx
41980 70 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  pr ){.    sqlite
41981 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 70  3_result_int64(p
41982 43 74 78 2c 20 70 43 73 72 2d 3e 69 4c 61 6e 67  Ctx, pCsr->iLang
41983 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  id);.  }else{.  
41984 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74    /* The request
41985 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 65 69 74  ed column is eit
41986 68 65 72 20 61 20 75 73 65 72 20 63 6f 6c 75 6d  her a user colum
41987 6e 20 28 6f 6e 65 20 74 68 61 74 20 63 6f 6e 74  n (one that cont
41988 61 69 6e 73 20 0a 20 20 20 20 2a 2a 20 69 6e 64  ains .    ** ind
41989 65 78 65 64 20 64 61 74 61 29 2c 20 6f 72 20 74  exed data), or t
4198a 68 65 20 6c 61 6e 67 75 61 67 65 2d 69 64 20 63  he language-id c
4198b 6f 6c 75 6d 6e 2e 20 20 2a 2f 0a 20 20 20 20 72  olumn.  */.    r
4198c 63 20 3d 20 66 74 73 33 43 75 72 73 6f 72 53 65  c = fts3CursorSe
4198d 65 6b 28 30 2c 20 70 43 73 72 29 3b 0a 0a 20 20  ek(0, pCsr);..  
4198e 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
4198f 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28  _OK ){.      if(
41990 20 69 43 6f 6c 3d 3d 70 2d 3e 6e 43 6f 6c 75 6d   iCol==p->nColum
41991 6e 2b 32 20 29 7b 0a 20 20 20 20 20 20 20 20 69  n+2 ){.        i
41992 6e 74 20 69 4c 61 6e 67 69 64 20 3d 20 30 3b 0a  nt iLangid = 0;.
41993 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 7a          if( p->z
41994 4c 61 6e 67 75 61 67 65 69 64 20 29 7b 0a 20 20  Languageid ){.  
41995 20 20 20 20 20 20 20 20 69 4c 61 6e 67 69 64 20          iLangid 
41996 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
41997 5f 69 6e 74 28 70 43 73 72 2d 3e 70 53 74 6d 74  _int(pCsr->pStmt
41998 2c 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b  , p->nColumn+1);
41999 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
4199a 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
4199b 74 5f 69 6e 74 28 70 43 74 78 2c 20 69 4c 61 6e  t_int(pCtx, iLan
4199c 67 69 64 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  gid);.      }els
4199d 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 61  e if( sqlite3_da
4199e 74 61 5f 63 6f 75 6e 74 28 70 43 73 72 2d 3e 70  ta_count(pCsr->p
4199f 53 74 6d 74 29 3e 28 69 43 6f 6c 2b 31 29 20 29  Stmt)>(iCol+1) )
419a0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
419a1 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 70  3_result_value(p
419a2 43 74 78 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c  Ctx, sqlite3_col
419a3 75 6d 6e 5f 76 61 6c 75 65 28 70 43 73 72 2d 3e  umn_value(pCsr->
419a4 70 53 74 6d 74 2c 20 69 43 6f 6c 2b 31 29 29 3b  pStmt, iCol+1));
419a5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
419a6 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 28 28   }..  assert( ((
419a7 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43 73 72  Fts3Table *)pCsr
419a8 2d 3e 62 61 73 65 2e 70 56 74 61 62 29 2d 3e 70  ->base.pVtab)->p
419a9 53 65 67 6d 65 6e 74 73 3d 3d 30 20 29 3b 0a 20  Segments==0 );. 
419aa 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
419ab 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  * .** This funct
419ac 69 6f 6e 20 69 73 20 74 68 65 20 69 6d 70 6c 65  ion is the imple
419ad 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
419ae 20 78 55 70 64 61 74 65 20 63 61 6c 6c 62 61 63   xUpdate callbac
419af 6b 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 46 54  k used by .** FT
419b0 53 33 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  S3 virtual table
419b1 73 2e 20 49 74 20 69 73 20 69 6e 76 6f 6b 65 64  s. It is invoked
419b2 20 62 79 20 53 51 4c 69 74 65 20 65 61 63 68 20   by SQLite each 
419b3 74 69 6d 65 20 61 20 72 6f 77 20 69 73 20 74 6f  time a row is to
419b4 20 62 65 0a 2a 2a 20 69 6e 73 65 72 74 65 64 2c   be.** inserted,
419b5 20 75 70 64 61 74 65 64 20 6f 72 20 64 65 6c 65   updated or dele
419b6 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ted..*/.static i
419b7 6e 74 20 66 74 73 33 55 70 64 61 74 65 4d 65 74  nt fts3UpdateMet
419b8 68 6f 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  hod(.  sqlite3_v
419b9 74 61 62 20 2a 70 56 74 61 62 2c 20 20 20 20 20  tab *pVtab,     
419ba 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61         /* Virtua
419bb 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  l table handle *
419bc 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20  /.  int nArg,   
419bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
419be 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61      /* Size of a
419bf 72 67 75 6d 65 6e 74 20 61 72 72 61 79 20 2a 2f  rgument array */
419c0 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
419c1 20 2a 2a 61 70 56 61 6c 2c 20 20 20 20 20 20 20   **apVal,       
419c2 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 61     /* Array of a
419c3 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 73 71  rguments */.  sq
419c4 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52 6f 77  lite_int64 *pRow
419c5 69 64 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  id            /*
419c6 20 4f 55 54 3a 20 54 68 65 20 61 66 66 65 63 74   OUT: The affect
419c7 65 64 20 28 6f 72 20 65 66 66 65 63 74 65 64 29  ed (or effected)
419c8 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a 20 20 72   rowid */.){.  r
419c9 65 74 75 72 6e 20 73 71 6c 69 74 65 33 46 74 73  eturn sqlite3Fts
419ca 33 55 70 64 61 74 65 4d 65 74 68 6f 64 28 70 56  3UpdateMethod(pV
419cb 74 61 62 2c 20 6e 41 72 67 2c 20 61 70 56 61 6c  tab, nArg, apVal
419cc 2c 20 70 52 6f 77 69 64 29 3b 0a 7d 0a 0a 2f 2a  , pRowid);.}../*
419cd 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
419ce 6f 6e 20 6f 66 20 78 53 79 6e 63 28 29 20 6d 65  on of xSync() me
419cf 74 68 6f 64 2e 20 46 6c 75 73 68 20 74 68 65 20  thod. Flush the 
419d0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
419d1 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 0a 2a 2a  pending-terms.**
419d2 20 68 61 73 68 2d 74 61 62 6c 65 20 74 6f 20 74   hash-table to t
419d3 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a  he database..*/.
419d4 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53  static int fts3S
419d5 79 6e 63 4d 65 74 68 6f 64 28 73 71 6c 69 74 65  yncMethod(sqlite
419d6 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a  3_vtab *pVtab){.
419d7 0a 20 20 2f 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20  .  /* Following 
419d8 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 6d  an incremental-m
419d9 65 72 67 65 20 6f 70 65 72 61 74 69 6f 6e 2c 20  erge operation, 
419da 61 73 73 75 6d 69 6e 67 20 74 68 61 74 20 74 68  assuming that th
419db 65 20 69 6e 70 75 74 0a 20 20 2a 2a 20 73 65 67  e input.  ** seg
419dc 6d 65 6e 74 73 20 61 72 65 20 6e 6f 74 20 63 6f  ments are not co
419dd 6d 70 6c 65 74 65 6c 79 20 63 6f 6e 73 75 6d 65  mpletely consume
419de 64 20 28 74 68 65 20 75 73 75 61 6c 20 63 61 73  d (the usual cas
419df 65 29 2c 20 74 68 65 79 20 61 72 65 20 75 70 64  e), they are upd
419e0 61 74 65 64 0a 20 20 2a 2a 20 69 6e 20 70 6c 61  ated.  ** in pla
419e1 63 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65  ce to remove the
419e2 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 68 61   entries that ha
419e3 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
419e4 6d 65 72 67 65 64 2e 20 54 68 69 73 0a 20 20 2a  merged. This.  *
419e5 2a 20 69 6e 76 6f 6c 76 65 73 20 75 70 64 61 74  * involves updat
419e6 69 6e 67 20 74 68 65 20 6c 65 61 66 20 62 6c 6f  ing the leaf blo
419e7 63 6b 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ck that contains
419e8 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 75 6e   the smallest un
419e9 6d 65 72 67 65 64 0a 20 20 2a 2a 20 65 6e 74 72  merged.  ** entr
419ea 79 20 61 6e 64 20 65 61 63 68 20 62 6c 6f 63 6b  y and each block
419eb 20 28 69 66 20 61 6e 79 29 20 62 65 74 77 65 65   (if any) betwee
419ec 6e 20 74 68 65 20 6c 65 61 66 20 61 6e 64 20 74  n the leaf and t
419ed 68 65 20 72 6f 6f 74 20 6e 6f 64 65 2e 20 53 6f  he root node. So
419ee 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 68 65 69  .  ** if the hei
419ef 67 68 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74  ght of the input
419f0 20 73 65 67 6d 65 6e 74 20 62 2d 74 72 65 65 73   segment b-trees
419f1 20 69 73 20 4e 2c 20 61 6e 64 20 69 6e 70 75 74   is N, and input
419f2 20 73 65 67 6d 65 6e 74 73 0a 20 20 2a 2a 20 61   segments.  ** a
419f3 72 65 20 6d 65 72 67 65 64 20 65 69 67 68 74 20  re merged eight 
419f4 61 74 20 61 20 74 69 6d 65 2c 20 75 70 64 61 74  at a time, updat
419f5 69 6e 67 20 74 68 65 20 69 6e 70 75 74 20 73 65  ing the input se
419f6 67 6d 65 6e 74 73 20 61 74 20 74 68 65 20 65 6e  gments at the en
419f7 64 0a 20 20 2a 2a 20 6f 66 20 61 6e 20 69 6e 63  d.  ** of an inc
419f8 72 65 6d 65 6e 74 61 6c 2d 6d 65 72 67 65 20 72  remental-merge r
419f9 65 71 75 69 72 65 73 20 77 72 69 74 69 6e 67 20  equires writing 
419fa 28 38 2a 28 31 2b 4e 29 29 20 62 6c 6f 63 6b 73  (8*(1+N)) blocks
419fb 2e 20 4e 20 69 73 20 75 73 75 61 6c 6c 79 0a 20  . N is usually. 
419fc 20 2a 2a 20 73 6d 61 6c 6c 20 2d 20 6f 66 74 65   ** small - ofte
419fd 6e 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20  n between 0 and 
419fe 32 2e 20 53 6f 20 74 68 65 20 6f 76 65 72 68 65  2. So the overhe
419ff 61 64 20 6f 66 20 74 68 65 20 69 6e 63 72 65 6d  ad of the increm
41a00 65 6e 74 61 6c 0a 20 20 2a 2a 20 6d 65 72 67 65  ental.  ** merge
41a01 20 69 73 20 73 6f 6d 65 77 68 65 72 65 20 62 65   is somewhere be
41a02 74 77 65 65 6e 20 38 20 61 6e 64 20 32 34 20 62  tween 8 and 24 b
41a03 6c 6f 63 6b 73 2e 20 54 6f 20 61 76 6f 69 64 20  locks. To avoid 
41a04 74 68 69 73 20 6f 76 65 72 68 65 61 64 0a 20 20  this overhead.  
41a05 2a 2a 20 64 77 61 72 66 69 6e 67 20 74 68 65 20  ** dwarfing the 
41a06 61 63 74 75 61 6c 20 70 72 6f 64 75 63 74 69 76  actual productiv
41a07 65 20 77 6f 72 6b 20 61 63 63 6f 6d 70 6c 69 73  e work accomplis
41a08 68 65 64 2c 20 74 68 65 20 69 6e 63 72 65 6d 65  hed, the increme
41a09 6e 74 61 6c 20 6d 65 72 67 65 0a 20 20 2a 2a 20  ntal merge.  ** 
41a0a 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65  is only attempte
41a0b 64 20 69 66 20 69 74 20 77 69 6c 6c 20 77 72 69  d if it will wri
41a0c 74 65 20 61 74 20 6c 65 61 73 74 20 36 34 20 6c  te at least 64 l
41a0d 65 61 66 20 62 6c 6f 63 6b 73 2e 20 48 65 6e 63  eaf blocks. Henc
41a0e 65 0a 20 20 2a 2a 20 6e 4d 69 6e 4d 65 72 67 65  e.  ** nMinMerge
41a0f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 66 20 63  ..  **.  ** Of c
41a10 6f 75 72 73 65 2c 20 75 70 64 61 74 69 6e 67 20  ourse, updating 
41a11 74 68 65 20 69 6e 70 75 74 20 73 65 67 6d 65 6e  the input segmen
41a12 74 73 20 61 6c 73 6f 20 69 6e 76 6f 6c 76 65 73  ts also involves
41a13 20 64 65 6c 65 74 69 6e 67 20 61 20 62 75 6e 63   deleting a bunc
41a14 68 0a 20 20 2a 2a 20 6f 66 20 62 6c 6f 63 6b 73  h.  ** of blocks
41a15 20 66 72 6f 6d 20 74 68 65 20 73 65 67 6d 65 6e   from the segmen
41a16 74 73 20 74 61 62 6c 65 2e 20 42 75 74 20 74 68  ts table. But th
41a17 69 73 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64  is is not consid
41a18 65 72 65 64 20 6f 76 65 72 68 65 61 64 0a 20 20  ered overhead.  
41a19 2a 2a 20 61 73 20 69 74 20 77 6f 75 6c 64 20 61  ** as it would a
41a1a 6c 73 6f 20 62 65 20 72 65 71 75 69 72 65 64 20  lso be required 
41a1b 62 79 20 61 20 63 72 69 73 69 73 2d 6d 65 72 67  by a crisis-merg
41a1c 65 20 74 68 61 74 20 75 73 65 64 20 74 68 65 20  e that used the 
41a1d 73 61 6d 65 20 69 6e 70 75 74 20 0a 20 20 2a 2a  same input .  **
41a1e 20 73 65 67 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a   segments..  */.
41a1f 20 20 63 6f 6e 73 74 20 75 33 32 20 6e 4d 69 6e    const u32 nMin
41a20 4d 65 72 67 65 20 3d 20 36 34 3b 20 20 20 20 20  Merge = 64;     
41a21 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f    /* Minimum amo
41a22 75 6e 74 20 6f 66 20 69 6e 63 72 2d 6d 65 72 67  unt of incr-merg
41a23 65 20 77 6f 72 6b 20 74 6f 20 64 6f 20 2a 2f 0a  e work to do */.
41a24 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 20  .  Fts3Table *p 
41a25 3d 20 28 46 74 73 33 54 61 62 6c 65 2a 29 70 56  = (Fts3Table*)pV
41a26 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  tab;.  int rc = 
41a27 73 71 6c 69 74 65 33 46 74 73 33 50 65 6e 64 69  sqlite3Fts3Pendi
41a28 6e 67 54 65 72 6d 73 46 6c 75 73 68 28 70 29 3b  ngTermsFlush(p);
41a29 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
41a2a 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 62 41 75 74  TE_OK && p->bAut
41a2b 6f 69 6e 63 72 6d 65 72 67 65 3d 3d 31 20 26 26  oincrmerge==1 &&
41a2c 20 70 2d 3e 6e 4c 65 61 66 41 64 64 3e 28 6e 4d   p->nLeafAdd>(nM
41a2d 69 6e 4d 65 72 67 65 2f 31 36 29 20 29 7b 0a 20  inMerge/16) ){. 
41a2e 20 20 20 69 6e 74 20 6d 78 4c 65 76 65 6c 20 3d     int mxLevel =
41a2f 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
41a30 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 72 65 6c 61   /* Maximum rela
41a31 74 69 76 65 20 6c 65 76 65 6c 20 76 61 6c 75 65  tive level value
41a32 20 69 6e 20 64 62 20 2a 2f 0a 20 20 20 20 69 6e   in db */.    in
41a33 74 20 41 3b 20 20 20 20 20 20 20 20 20 20 20 20  t A;            
41a34 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
41a35 6e 63 72 2d 6d 65 72 67 65 20 70 61 72 61 6d 65  ncr-merge parame
41a36 74 65 72 20 41 20 2a 2f 0a 0a 20 20 20 20 72 63  ter A */..    rc
41a37 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 4d 61   = sqlite3Fts3Ma
41a38 78 4c 65 76 65 6c 28 70 2c 20 26 6d 78 4c 65 76  xLevel(p, &mxLev
41a39 65 6c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  el);.    assert(
41a3a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
41a3b 7c 20 6d 78 4c 65 76 65 6c 3d 3d 30 20 29 3b 0a  | mxLevel==0 );.
41a3c 20 20 20 20 41 20 3d 20 70 2d 3e 6e 4c 65 61 66      A = p->nLeaf
41a3d 41 64 64 20 2a 20 6d 78 4c 65 76 65 6c 3b 0a 20  Add * mxLevel;. 
41a3e 20 20 20 41 20 2b 3d 20 28 41 2f 32 29 3b 0a 20     A += (A/2);. 
41a3f 20 20 20 69 66 28 20 41 3e 28 69 6e 74 29 6e 4d     if( A>(int)nM
41a40 69 6e 4d 65 72 67 65 20 29 20 72 63 20 3d 20 73  inMerge ) rc = s
41a41 71 6c 69 74 65 33 46 74 73 33 49 6e 63 72 6d 65  qlite3Fts3Incrme
41a42 72 67 65 28 70 2c 20 41 2c 20 38 29 3b 0a 20 20  rge(p, A, 8);.  
41a43 7d 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 53  }.  sqlite3Fts3S
41a44 65 67 6d 65 6e 74 73 43 6c 6f 73 65 28 70 29 3b  egmentsClose(p);
41a45 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
41a46 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
41a47 61 74 69 6f 6e 20 6f 66 20 78 42 65 67 69 6e 28  ation of xBegin(
41a48 29 20 6d 65 74 68 6f 64 2e 20 54 68 69 73 20 69  ) method. This i
41a49 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
41a4a 61 74 69 63 20 69 6e 74 20 66 74 73 33 42 65 67  atic int fts3Beg
41a4b 69 6e 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33  inMethod(sqlite3
41a4c 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20  _vtab *pVtab){. 
41a4d 20 46 74 73 33 54 61 62 6c 65 20 2a 70 20 3d 20   Fts3Table *p = 
41a4e 28 46 74 73 33 54 61 62 6c 65 2a 29 70 56 74 61  (Fts3Table*)pVta
41a4f 62 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  b;.  UNUSED_PARA
41a50 4d 45 54 45 52 28 70 56 74 61 62 29 3b 0a 20 20  METER(pVtab);.  
41a51 61 73 73 65 72 74 28 20 70 2d 3e 70 53 65 67 6d  assert( p->pSegm
41a52 65 6e 74 73 3d 3d 30 20 29 3b 0a 20 20 61 73 73  ents==0 );.  ass
41a53 65 72 74 28 20 70 2d 3e 6e 50 65 6e 64 69 6e 67  ert( p->nPending
41a54 44 61 74 61 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Data==0 );.  ass
41a55 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 61  ert( p->inTransa
41a56 63 74 69 6f 6e 21 3d 31 20 29 3b 0a 20 20 54 45  ction!=1 );.  TE
41a57 53 54 4f 4e 4c 59 28 20 70 2d 3e 69 6e 54 72 61  STONLY( p->inTra
41a58 6e 73 61 63 74 69 6f 6e 20 3d 20 31 20 29 3b 0a  nsaction = 1 );.
41a59 20 20 54 45 53 54 4f 4e 4c 59 28 20 70 2d 3e 6d    TESTONLY( p->m
41a5a 78 53 61 76 65 70 6f 69 6e 74 20 3d 20 2d 31 3b  xSavepoint = -1;
41a5b 20 29 3b 0a 20 20 70 2d 3e 6e 4c 65 61 66 41 64   );.  p->nLeafAd
41a5c 64 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  d = 0;.  return 
41a5d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
41a5e 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
41a5f 6f 6e 20 6f 66 20 78 43 6f 6d 6d 69 74 28 29 20  on of xCommit() 
41a60 6d 65 74 68 6f 64 2e 20 54 68 69 73 20 69 73 20  method. This is 
41a61 61 20 6e 6f 2d 6f 70 2e 20 54 68 65 20 63 6f 6e  a no-op. The con
41a62 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 65 20  tents of.** the 
41a63 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 68 61  pending-terms ha
41a64 73 68 2d 74 61 62 6c 65 20 68 61 76 65 20 61 6c  sh-table have al
41a65 72 65 61 64 79 20 62 65 65 6e 20 66 6c 75 73 68  ready been flush
41a66 65 64 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  ed into the data
41a67 62 61 73 65 0a 2a 2a 20 62 79 20 66 74 73 33 53  base.** by fts3S
41a68 79 6e 63 4d 65 74 68 6f 64 28 29 2e 0a 2a 2f 0a  yncMethod()..*/.
41a69 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 43  static int fts3C
41a6a 6f 6d 6d 69 74 4d 65 74 68 6f 64 28 73 71 6c 69  ommitMethod(sqli
41a6b 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29  te3_vtab *pVtab)
41a6c 7b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 46 74  {.  TESTONLY( Ft
41a6d 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28 46 74  s3Table *p = (Ft
41a6e 73 33 54 61 62 6c 65 2a 29 70 56 74 61 62 20 29  s3Table*)pVtab )
41a6f 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
41a70 45 54 45 52 28 70 56 74 61 62 29 3b 0a 20 20 61  ETER(pVtab);.  a
41a71 73 73 65 72 74 28 20 70 2d 3e 6e 50 65 6e 64 69  ssert( p->nPendi
41a72 6e 67 44 61 74 61 3d 3d 30 20 29 3b 0a 20 20 61  ngData==0 );.  a
41a73 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e  ssert( p->inTran
41a74 73 61 63 74 69 6f 6e 21 3d 30 20 29 3b 0a 20 20  saction!=0 );.  
41a75 61 73 73 65 72 74 28 20 70 2d 3e 70 53 65 67 6d  assert( p->pSegm
41a76 65 6e 74 73 3d 3d 30 20 29 3b 0a 20 20 54 45 53  ents==0 );.  TES
41a77 54 4f 4e 4c 59 28 20 70 2d 3e 69 6e 54 72 61 6e  TONLY( p->inTran
41a78 73 61 63 74 69 6f 6e 20 3d 20 30 20 29 3b 0a 20  saction = 0 );. 
41a79 20 54 45 53 54 4f 4e 4c 59 28 20 70 2d 3e 6d 78   TESTONLY( p->mx
41a7a 53 61 76 65 70 6f 69 6e 74 20 3d 20 2d 31 3b 20  Savepoint = -1; 
41a7b 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
41a7c 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
41a7d 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
41a7e 66 20 78 52 6f 6c 6c 62 61 63 6b 28 29 2e 20 44  f xRollback(). D
41a7f 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65  iscard the conte
41a80 6e 74 73 20 6f 66 20 74 68 65 20 70 65 6e 64 69  nts of the pendi
41a81 6e 67 2d 74 65 72 6d 73 0a 2a 2a 20 68 61 73 68  ng-terms.** hash
41a82 2d 74 61 62 6c 65 2e 20 41 6e 79 20 63 68 61 6e  -table. Any chan
41a83 67 65 73 20 6d 61 64 65 20 74 6f 20 74 68 65 20  ges made to the 
41a84 64 61 74 61 62 61 73 65 20 61 72 65 20 72 65 76  database are rev
41a85 65 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e  erted by SQLite.
41a86 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
41a87 74 73 33 52 6f 6c 6c 62 61 63 6b 4d 65 74 68 6f  ts3RollbackMetho
41a88 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  d(sqlite3_vtab *
41a89 70 56 74 61 62 29 7b 0a 20 20 46 74 73 33 54 61  pVtab){.  Fts3Ta
41a8a 62 6c 65 20 2a 70 20 3d 20 28 46 74 73 33 54 61  ble *p = (Fts3Ta
41a8b 62 6c 65 2a 29 70 56 74 61 62 3b 0a 20 20 73 71  ble*)pVtab;.  sq
41a8c 6c 69 74 65 33 46 74 73 33 50 65 6e 64 69 6e 67  lite3Fts3Pending
41a8d 54 65 72 6d 73 43 6c 65 61 72 28 70 29 3b 0a 20  TermsClear(p);. 
41a8e 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72   assert( p->inTr
41a8f 61 6e 73 61 63 74 69 6f 6e 21 3d 30 20 29 3b 0a  ansaction!=0 );.
41a90 20 20 54 45 53 54 4f 4e 4c 59 28 20 70 2d 3e 69    TESTONLY( p->i
41a91 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 30  nTransaction = 0
41a92 20 29 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20   );.  TESTONLY( 
41a93 70 2d 3e 6d 78 53 61 76 65 70 6f 69 6e 74 20 3d  p->mxSavepoint =
41a94 20 2d 31 3b 20 29 3b 0a 20 20 72 65 74 75 72 6e   -1; );.  return
41a95 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
41a96 2a 0a 2a 2a 20 57 68 65 6e 20 63 61 6c 6c 65 64  *.** When called
41a97 2c 20 2a 70 70 50 6f 73 6c 69 73 74 20 6d 75 73  , *ppPoslist mus
41a98 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 62  t point to the b
41a99 79 74 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  yte immediately 
41a9a 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 0a 2a 2a  following the.**
41a9b 20 65 6e 64 20 6f 66 20 61 20 70 6f 73 69 74 69   end of a positi
41a9c 6f 6e 2d 6c 69 73 74 2e 20 69 2e 65 2e 20 28 20  on-list. i.e. ( 
41a9d 28 2a 70 70 50 6f 73 6c 69 73 74 29 5b 2d 31 5d  (*ppPoslist)[-1]
41a9e 3d 3d 50 4f 53 5f 45 4e 44 20 29 2e 20 54 68 69  ==POS_END ). Thi
41a9f 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 6f  s function.** mo
41aa0 76 65 73 20 2a 70 70 50 6f 73 6c 69 73 74 20 73  ves *ppPoslist s
41aa1 6f 20 74 68 61 74 20 69 74 20 69 6e 73 74 65 61  o that it instea
41aa2 64 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  d points to the 
41aa3 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68  first byte of th
41aa4 65 0a 2a 2a 20 73 61 6d 65 20 70 6f 73 69 74 69  e.** same positi
41aa5 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74  on list..*/.stat
41aa6 69 63 20 76 6f 69 64 20 66 74 73 33 52 65 76 65  ic void fts3Reve
41aa7 72 73 65 50 6f 73 6c 69 73 74 28 63 68 61 72 20  rsePoslist(char 
41aa8 2a 70 53 74 61 72 74 2c 20 63 68 61 72 20 2a 2a  *pStart, char **
41aa9 70 70 50 6f 73 6c 69 73 74 29 7b 0a 20 20 63 68  ppPoslist){.  ch
41aaa 61 72 20 2a 70 20 3d 20 26 28 2a 70 70 50 6f 73  ar *p = &(*ppPos
41aab 6c 69 73 74 29 5b 2d 32 5d 3b 0a 20 20 63 68 61  list)[-2];.  cha
41aac 72 20 63 20 3d 20 30 3b 0a 0a 20 20 77 68 69 6c  r c = 0;..  whil
41aad 65 28 20 70 3e 70 53 74 61 72 74 20 26 26 20 28  e( p>pStart && (
41aae 63 3d 2a 70 2d 2d 29 3d 3d 30 20 29 3b 0a 20 20  c=*p--)==0 );.  
41aaf 77 68 69 6c 65 28 20 70 3e 70 53 74 61 72 74 20  while( p>pStart 
41ab0 26 26 20 28 2a 70 20 26 20 30 78 38 30 29 20 7c  && (*p & 0x80) |
41ab1 20 63 20 29 7b 20 0a 20 20 20 20 63 20 3d 20 2a   c ){ .    c = *
41ab2 70 2d 2d 3b 20 0a 20 20 7d 0a 20 20 69 66 28 20  p--; .  }.  if( 
41ab3 70 3e 70 53 74 61 72 74 20 29 7b 20 70 20 3d 20  p>pStart ){ p = 
41ab4 26 70 5b 32 5d 3b 20 7d 0a 20 20 77 68 69 6c 65  &p[2]; }.  while
41ab5 28 20 2a 70 2b 2b 26 30 78 38 30 20 29 3b 0a 20  ( *p++&0x80 );. 
41ab6 20 2a 70 70 50 6f 73 6c 69 73 74 20 3d 20 70 3b   *ppPoslist = p;
41ab7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72  .}../*.** Helper
41ab8 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 62   function used b
41ab9 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  y the implementa
41aba 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 76 65 72  tion of the over
41abb 6c 6f 61 64 65 64 20 73 6e 69 70 70 65 74 28 29  loaded snippet()
41abc 2c 0a 2a 2a 20 6f 66 66 73 65 74 73 28 29 20 61  ,.** offsets() a
41abd 6e 64 20 6f 70 74 69 6d 69 7a 65 28 29 20 53 51  nd optimize() SQ
41abe 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a  L functions..**.
41abf 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  ** If the value 
41ac0 70 61 73 73 65 64 20 61 73 20 74 68 65 20 74 68  passed as the th
41ac1 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ird argument is 
41ac2 61 20 62 6c 6f 62 20 6f 66 20 73 69 7a 65 0a 2a  a blob of size.*
41ac3 2a 20 73 69 7a 65 6f 66 28 46 74 73 33 43 75 72  * sizeof(Fts3Cur
41ac4 73 6f 72 2a 29 2c 20 74 68 65 6e 20 74 68 65 20  sor*), then the 
41ac5 62 6c 6f 62 20 63 6f 6e 74 65 6e 74 73 20 61 72  blob contents ar
41ac6 65 20 63 6f 70 69 65 64 20 74 6f 20 74 68 65 20  e copied to the 
41ac7 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61  .** output varia
41ac8 62 6c 65 20 2a 70 70 43 73 72 20 61 6e 64 20 53  ble *ppCsr and S
41ac9 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
41aca 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rned. Otherwise,
41acb 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73   an error.** mes
41acc 73 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20  sage is written 
41acd 74 6f 20 63 6f 6e 74 65 78 74 20 70 43 6f 6e 74  to context pCont
41ace 65 78 74 20 61 6e 64 20 53 51 4c 49 54 45 5f 45  ext and SQLITE_E
41acf 52 52 4f 52 20 72 65 74 75 72 6e 65 64 2e 20 54  RROR returned. T
41ad0 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 70 61 73  he.** string pas
41ad1 73 65 64 20 76 69 61 20 7a 46 75 6e 63 20 69 73  sed via zFunc is
41ad2 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66   used as part of
41ad3 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
41ad4 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ge..*/.static in
41ad5 74 20 66 74 73 33 46 75 6e 63 74 69 6f 6e 41 72  t fts3FunctionAr
41ad6 67 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  g(.  sqlite3_con
41ad7 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 20  text *pContext, 
41ad8 20 20 20 20 20 2f 2a 20 53 51 4c 20 66 75 6e 63       /* SQL func
41ad9 74 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74 65 78  tion call contex
41ada 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
41adb 72 20 2a 7a 46 75 6e 63 2c 20 20 20 20 20 20 20  r *zFunc,       
41adc 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69         /* Functi
41add 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 71 6c  on name */.  sql
41ade 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
41adf 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
41ae0 61 72 67 76 5b 30 5d 20 70 61 73 73 65 64 20 74  argv[0] passed t
41ae1 6f 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  o function */.  
41ae2 46 74 73 33 43 75 72 73 6f 72 20 2a 2a 70 70 43  Fts3Cursor **ppC
41ae3 73 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  sr              
41ae4 2f 2a 20 4f 55 54 3a 20 53 74 6f 72 65 20 63 75  /* OUT: Store cu
41ae5 72 73 6f 72 20 68 61 6e 64 6c 65 20 68 65 72 65  rsor handle here
41ae6 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 43 75 72   */.){.  Fts3Cur
41ae7 73 6f 72 20 2a 70 52 65 74 3b 0a 20 20 69 66 28  sor *pRet;.  if(
41ae8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
41ae9 79 70 65 28 70 56 61 6c 29 21 3d 53 51 4c 49 54  ype(pVal)!=SQLIT
41aea 45 5f 42 4c 4f 42 20 0a 20 20 20 7c 7c 20 73 71  E_BLOB .   || sq
41aeb 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
41aec 73 28 70 56 61 6c 29 21 3d 73 69 7a 65 6f 66 28  s(pVal)!=sizeof(
41aed 46 74 73 33 43 75 72 73 6f 72 20 2a 29 0a 20 20  Fts3Cursor *).  
41aee 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72  ){.    char *zEr
41aef 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  r = sqlite3_mpri
41af0 6e 74 66 28 22 69 6c 6c 65 67 61 6c 20 66 69 72  ntf("illegal fir
41af1 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 25  st argument to %
41af2 73 22 2c 20 7a 46 75 6e 63 29 3b 0a 20 20 20 20  s", zFunc);.    
41af3 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
41af4 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 7a  rror(pContext, z
41af5 45 72 72 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71  Err, -1);.    sq
41af6 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72 29  lite3_free(zErr)
41af7 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
41af8 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
41af9 20 6d 65 6d 63 70 79 28 26 70 52 65 74 2c 20 73   memcpy(&pRet, s
41afa 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
41afb 62 28 70 56 61 6c 29 2c 20 73 69 7a 65 6f 66 28  b(pVal), sizeof(
41afc 46 74 73 33 43 75 72 73 6f 72 20 2a 29 29 3b 0a  Fts3Cursor *));.
41afd 20 20 2a 70 70 43 73 72 20 3d 20 70 52 65 74 3b    *ppCsr = pRet;
41afe 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
41aff 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  _OK;.}../*.** Im
41b00 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
41b01 74 68 65 20 73 6e 69 70 70 65 74 28 29 20 66 75  the snippet() fu
41b02 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 0a  nction for FTS3.
41b03 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
41b04 74 73 33 53 6e 69 70 70 65 74 46 75 6e 63 28 0a  ts3SnippetFunc(.
41b05 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
41b06 74 20 2a 70 43 6f 6e 74 65 78 74 2c 20 20 20 20  t *pContext,    
41b07 20 20 2f 2a 20 53 51 4c 69 74 65 20 66 75 6e 63    /* SQLite func
41b08 74 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74 65 78  tion call contex
41b09 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 6c 2c  t */.  int nVal,
41b0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41b0b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
41b0c 66 20 61 70 56 61 6c 5b 5d 20 61 72 72 61 79 20  f apVal[] array 
41b0d 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  */.  sqlite3_val
41b0e 75 65 20 2a 2a 61 70 56 61 6c 20 20 20 20 20 20  ue **apVal      
41b0f 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
41b10 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b   arguments */.){
41b11 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70  .  Fts3Cursor *p
41b12 43 73 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Csr;            
41b13 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 68 61 6e     /* Cursor han
41b14 64 6c 65 20 70 61 73 73 65 64 20 74 68 72 6f 75  dle passed throu
41b15 67 68 20 61 70 56 61 6c 5b 30 5d 20 2a 2f 0a 20  gh apVal[0] */. 
41b16 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74   const char *zSt
41b17 61 72 74 20 3d 20 22 3c 62 3e 22 3b 0a 20 20 63  art = "<b>";.  c
41b18 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 20  onst char *zEnd 
41b19 3d 20 22 3c 2f 62 3e 22 3b 0a 20 20 63 6f 6e 73  = "</b>";.  cons
41b1a 74 20 63 68 61 72 20 2a 7a 45 6c 6c 69 70 73 69  t char *zEllipsi
41b1b 73 20 3d 20 22 3c 62 3e 2e 2e 2e 3c 2f 62 3e 22  s = "<b>...</b>"
41b1c 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d  ;.  int iCol = -
41b1d 31 3b 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20  1;.  int nToken 
41b1e 3d 20 31 35 3b 20 20 20 20 20 20 20 20 20 20 20  = 15;           
41b1f 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20       /* Default 
41b20 6e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73  number of tokens
41b21 20 69 6e 20 73 6e 69 70 70 65 74 20 2a 2f 0a 0a   in snippet */..
41b22 20 20 2f 2a 20 54 68 65 72 65 20 6d 75 73 74 20    /* There must 
41b23 62 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  be at least one 
41b24 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20  argument passed 
41b25 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
41b26 20 28 6f 74 68 65 72 77 69 73 65 0a 20 20 2a 2a   (otherwise.  **
41b27 20 74 68 65 20 6e 6f 6e 2d 6f 76 65 72 6c 6f 61   the non-overloa
41b28 64 65 64 20 76 65 72 73 69 6f 6e 20 77 6f 75 6c  ded version woul
41b29 64 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c  d have been call
41b2a 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68  ed instead of th
41b2b 69 73 20 6f 6e 65 29 2e 0a 20 20 2a 2f 0a 20 20  is one)..  */.  
41b2c 61 73 73 65 72 74 28 20 6e 56 61 6c 3e 3d 31 20  assert( nVal>=1 
41b2d 29 3b 0a 0a 20 20 69 66 28 20 6e 56 61 6c 3e 36  );..  if( nVal>6
41b2e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
41b2f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f  result_error(pCo
41b30 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20  ntext, .        
41b31 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66  "wrong number of
41b32 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 66 75   arguments to fu
41b33 6e 63 74 69 6f 6e 20 73 6e 69 70 70 65 74 28 29  nction snippet()
41b34 22 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75  ", -1);.    retu
41b35 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 74  rn;.  }.  if( ft
41b36 73 33 46 75 6e 63 74 69 6f 6e 41 72 67 28 70 43  s3FunctionArg(pC
41b37 6f 6e 74 65 78 74 2c 20 22 73 6e 69 70 70 65 74  ontext, "snippet
41b38 22 2c 20 61 70 56 61 6c 5b 30 5d 2c 20 26 70 43  ", apVal[0], &pC
41b39 73 72 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  sr) ) return;.. 
41b3a 20 73 77 69 74 63 68 28 20 6e 56 61 6c 20 29 7b   switch( nVal ){
41b3b 0a 20 20 20 20 63 61 73 65 20 36 3a 20 6e 54 6f  .    case 6: nTo
41b3c 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ken = sqlite3_va
41b3d 6c 75 65 5f 69 6e 74 28 61 70 56 61 6c 5b 35 5d  lue_int(apVal[5]
41b3e 29 3b 0a 20 20 20 20 63 61 73 65 20 35 3a 20 69  );.    case 5: i
41b3f 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Col = sqlite3_va
41b40 6c 75 65 5f 69 6e 74 28 61 70 56 61 6c 5b 34 5d  lue_int(apVal[4]
41b41 29 3b 0a 20 20 20 20 63 61 73 65 20 34 3a 20 7a  );.    case 4: z
41b42 45 6c 6c 69 70 73 69 73 20 3d 20 28 63 6f 6e 73  Ellipsis = (cons
41b43 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f  t char*)sqlite3_
41b44 76 61 6c 75 65 5f 74 65 78 74 28 61 70 56 61 6c  value_text(apVal
41b45 5b 33 5d 29 3b 0a 20 20 20 20 63 61 73 65 20 33  [3]);.    case 3
41b46 3a 20 7a 45 6e 64 20 3d 20 28 63 6f 6e 73 74 20  : zEnd = (const 
41b47 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
41b48 6c 75 65 5f 74 65 78 74 28 61 70 56 61 6c 5b 32  lue_text(apVal[2
41b49 5d 29 3b 0a 20 20 20 20 63 61 73 65 20 32 3a 20  ]);.    case 2: 
41b4a 7a 53 74 61 72 74 20 3d 20 28 63 6f 6e 73 74 20  zStart = (const 
41b4b 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
41b4c 6c 75 65 5f 74 65 78 74 28 61 70 56 61 6c 5b 31  lue_text(apVal[1
41b4d 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 7a  ]);.  }.  if( !z
41b4e 45 6c 6c 69 70 73 69 73 20 7c 7c 20 21 7a 45 6e  Ellipsis || !zEn
41b4f 64 20 7c 7c 20 21 7a 53 74 61 72 74 20 29 7b 0a  d || !zStart ){.
41b50 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
41b51 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 70  lt_error_nomem(p
41b52 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 65 6c 73  Context);.  }els
41b53 65 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d  e if( SQLITE_OK=
41b54 3d 66 74 73 33 43 75 72 73 6f 72 53 65 65 6b 28  =fts3CursorSeek(
41b55 70 43 6f 6e 74 65 78 74 2c 20 70 43 73 72 29 20  pContext, pCsr) 
41b56 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74  ){.    sqlite3Ft
41b57 73 33 53 6e 69 70 70 65 74 28 70 43 6f 6e 74 65  s3Snippet(pConte
41b58 78 74 2c 20 70 43 73 72 2c 20 7a 53 74 61 72 74  xt, pCsr, zStart
41b59 2c 20 7a 45 6e 64 2c 20 7a 45 6c 6c 69 70 73 69  , zEnd, zEllipsi
41b5a 73 2c 20 69 43 6f 6c 2c 20 6e 54 6f 6b 65 6e 29  s, iCol, nToken)
41b5b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
41b5c 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
41b5d 20 74 68 65 20 6f 66 66 73 65 74 73 28 29 20 66   the offsets() f
41b5e 75 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54 53 33  unction for FTS3
41b5f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
41b60 66 74 73 33 4f 66 66 73 65 74 73 46 75 6e 63 28  fts3OffsetsFunc(
41b61 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
41b62 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 20 20 20  xt *pContext,   
41b63 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 66 75 6e     /* SQLite fun
41b64 63 74 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74 65  ction call conte
41b65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 6c  xt */.  int nVal
41b66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
41b67 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
41b68 6f 66 20 61 72 67 75 6d 65 6e 74 20 61 72 72 61  of argument arra
41b69 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  y */.  sqlite3_v
41b6a 61 6c 75 65 20 2a 2a 61 70 56 61 6c 20 20 20 20  alue **apVal    
41b6b 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
41b6c 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a  of arguments */.
41b6d 29 7b 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20  ){.  Fts3Cursor 
41b6e 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20 20 20  *pCsr;          
41b6f 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 68       /* Cursor h
41b70 61 6e 64 6c 65 20 70 61 73 73 65 64 20 74 68 72  andle passed thr
41b71 6f 75 67 68 20 61 70 56 61 6c 5b 30 5d 20 2a 2f  ough apVal[0] */
41b72 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
41b73 45 54 45 52 28 6e 56 61 6c 29 3b 0a 0a 20 20 61  ETER(nVal);..  a
41b74 73 73 65 72 74 28 20 6e 56 61 6c 3d 3d 31 20 29  ssert( nVal==1 )
41b75 3b 0a 20 20 69 66 28 20 66 74 73 33 46 75 6e 63  ;.  if( fts3Func
41b76 74 69 6f 6e 41 72 67 28 70 43 6f 6e 74 65 78 74  tionArg(pContext
41b77 2c 20 22 6f 66 66 73 65 74 73 22 2c 20 61 70 56  , "offsets", apV
41b78 61 6c 5b 30 5d 2c 20 26 70 43 73 72 29 20 29 20  al[0], &pCsr) ) 
41b79 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
41b7a 28 20 70 43 73 72 20 29 3b 0a 20 20 69 66 28 20  ( pCsr );.  if( 
41b7b 53 51 4c 49 54 45 5f 4f 4b 3d 3d 66 74 73 33 43  SQLITE_OK==fts3C
41b7c 75 72 73 6f 72 53 65 65 6b 28 70 43 6f 6e 74 65  ursorSeek(pConte
41b7d 78 74 2c 20 70 43 73 72 29 20 29 7b 0a 20 20 20  xt, pCsr) ){.   
41b7e 20 73 71 6c 69 74 65 33 46 74 73 33 4f 66 66 73   sqlite3Fts3Offs
41b7f 65 74 73 28 70 43 6f 6e 74 65 78 74 2c 20 70 43  ets(pContext, pC
41b80 73 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a  sr);.  }.}../* .
41b81 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
41b82 6e 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c  n of the special
41b83 20 6f 70 74 69 6d 69 7a 65 28 29 20 66 75 6e 63   optimize() func
41b84 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 2e 20 54  tion for FTS3. T
41b85 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  his .** function
41b86 20 6d 65 72 67 65 73 20 61 6c 6c 20 73 65 67 6d   merges all segm
41b87 65 6e 74 73 20 69 6e 20 74 68 65 20 64 61 74 61  ents in the data
41b88 62 61 73 65 20 74 6f 20 61 20 73 69 6e 67 6c 65  base to a single
41b89 20 73 65 67 6d 65 6e 74 2e 0a 2a 2a 20 45 78 61   segment..** Exa
41b8a 6d 70 6c 65 20 75 73 61 67 65 20 69 73 3a 0a 2a  mple usage is:.*
41b8b 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 6f 70  *.**   SELECT op
41b8c 74 69 6d 69 7a 65 28 74 29 20 46 52 4f 4d 20 74  timize(t) FROM t
41b8d 20 4c 49 4d 49 54 20 31 3b 0a 2a 2a 0a 2a 2a 20   LIMIT 1;.**.** 
41b8e 77 68 65 72 65 20 27 74 27 20 69 73 20 74 68 65  where 't' is the
41b8f 20 6e 61 6d 65 20 6f 66 20 61 6e 20 46 54 53 33   name of an FTS3
41b90 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69   table..*/.stati
41b91 63 20 76 6f 69 64 20 66 74 73 33 4f 70 74 69 6d  c void fts3Optim
41b92 69 7a 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  izeFunc(.  sqlit
41b93 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  e3_context *pCon
41b94 74 65 78 74 2c 20 20 20 20 20 20 2f 2a 20 53 51  text,      /* SQ
41b95 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e 20 63 61  Lite function ca
41b96 6c 6c 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ll context */.  
41b97 69 6e 74 20 6e 56 61 6c 2c 20 20 20 20 20 20 20  int nVal,       
41b98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41b99 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72 67 75 6d  /* Size of argum
41b9a 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 73  ent array */.  s
41b9b 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
41b9c 70 56 61 6c 20 20 20 20 20 20 20 20 20 20 20 2f  pVal           /
41b9d 2a 20 41 72 72 61 79 20 6f 66 20 61 72 67 75 6d  * Array of argum
41b9e 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ents */.){.  int
41b9f 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
41ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
41ba1 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
41ba2 20 46 74 73 33 54 61 62 6c 65 20 2a 70 3b 20 20   Fts3Table *p;  
41ba3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41ba4 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c   /* Virtual tabl
41ba5 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 46 74  e handle */.  Ft
41ba6 73 33 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  s3Cursor *pCurso
41ba7 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r;            /*
41ba8 20 43 75 72 73 6f 72 20 68 61 6e 64 6c 65 20 70   Cursor handle p
41ba9 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 61 70  assed through ap
41baa 56 61 6c 5b 30 5d 20 2a 2f 0a 0a 20 20 55 4e 55  Val[0] */..  UNU
41bab 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 56  SED_PARAMETER(nV
41bac 61 6c 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  al);..  assert( 
41bad 6e 56 61 6c 3d 3d 31 20 29 3b 0a 20 20 69 66 28  nVal==1 );.  if(
41bae 20 66 74 73 33 46 75 6e 63 74 69 6f 6e 41 72 67   fts3FunctionArg
41baf 28 70 43 6f 6e 74 65 78 74 2c 20 22 6f 70 74 69  (pContext, "opti
41bb0 6d 69 7a 65 22 2c 20 61 70 56 61 6c 5b 30 5d 2c  mize", apVal[0],
41bb1 20 26 70 43 75 72 73 6f 72 29 20 29 20 72 65 74   &pCursor) ) ret
41bb2 75 72 6e 3b 0a 20 20 70 20 3d 20 28 46 74 73 33  urn;.  p = (Fts3
41bb3 54 61 62 6c 65 20 2a 29 70 43 75 72 73 6f 72 2d  Table *)pCursor-
41bb4 3e 62 61 73 65 2e 70 56 74 61 62 3b 0a 20 20 61  >base.pVtab;.  a
41bb5 73 73 65 72 74 28 20 70 20 29 3b 0a 0a 20 20 72  ssert( p );..  r
41bb6 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 4f  c = sqlite3Fts3O
41bb7 70 74 69 6d 69 7a 65 28 70 29 3b 0a 0a 20 20 73  ptimize(p);..  s
41bb8 77 69 74 63 68 28 20 72 63 20 29 7b 0a 20 20 20  witch( rc ){.   
41bb9 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4f 4b 3a   case SQLITE_OK:
41bba 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
41bbb 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74  esult_text(pCont
41bbc 65 78 74 2c 20 22 49 6e 64 65 78 20 6f 70 74 69  ext, "Index opti
41bbd 6d 69 7a 65 64 22 2c 20 2d 31 2c 20 53 51 4c 49  mized", -1, SQLI
41bbe 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
41bbf 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
41bc0 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3a 0a 20  e SQLITE_DONE:. 
41bc1 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
41bc2 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78  ult_text(pContex
41bc3 74 2c 20 22 49 6e 64 65 78 20 61 6c 72 65 61 64  t, "Index alread
41bc4 79 20 6f 70 74 69 6d 61 6c 22 2c 20 2d 31 2c 20  y optimal", -1, 
41bc5 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
41bc6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
41bc7 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20   default:.      
41bc8 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
41bc9 72 72 6f 72 5f 63 6f 64 65 28 70 43 6f 6e 74 65  rror_code(pConte
41bca 78 74 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 62  xt, rc);.      b
41bcb 72 65 61 6b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  reak;.  }.}../*.
41bcc 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
41bcd 6e 20 6f 66 20 74 68 65 20 6d 61 74 63 68 69 6e  n of the matchin
41bce 66 6f 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f  fo() function fo
41bcf 72 20 46 54 53 33 0a 2a 2f 0a 73 74 61 74 69 63  r FTS3.*/.static
41bd0 20 76 6f 69 64 20 66 74 73 33 4d 61 74 63 68 69   void fts3Matchi
41bd1 6e 66 6f 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  nfoFunc(.  sqlit
41bd2 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  e3_context *pCon
41bd3 74 65 78 74 2c 20 20 20 20 20 20 2f 2a 20 53 51  text,      /* SQ
41bd4 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e 20 63 61  Lite function ca
41bd5 6c 6c 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ll context */.  
41bd6 69 6e 74 20 6e 56 61 6c 2c 20 20 20 20 20 20 20  int nVal,       
41bd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41bd8 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72 67 75 6d  /* Size of argum
41bd9 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 73  ent array */.  s
41bda 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
41bdb 70 56 61 6c 20 20 20 20 20 20 20 20 20 20 20 2f  pVal           /
41bdc 2a 20 41 72 72 61 79 20 6f 66 20 61 72 67 75 6d  * Array of argum
41bdd 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20 46 74 73  ents */.){.  Fts
41bde 33 43 75 72 73 6f 72 20 2a 70 43 73 72 3b 20 20  3Cursor *pCsr;  
41bdf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
41be0 43 75 72 73 6f 72 20 68 61 6e 64 6c 65 20 70 61  Cursor handle pa
41be1 73 73 65 64 20 74 68 72 6f 75 67 68 20 61 70 56  ssed through apV
41be2 61 6c 5b 30 5d 20 2a 2f 0a 20 20 61 73 73 65 72  al[0] */.  asser
41be3 74 28 20 6e 56 61 6c 3d 3d 31 20 7c 7c 20 6e 56  t( nVal==1 || nV
41be4 61 6c 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 53  al==2 );.  if( S
41be5 51 4c 49 54 45 5f 4f 4b 3d 3d 66 74 73 33 46 75  QLITE_OK==fts3Fu
41be6 6e 63 74 69 6f 6e 41 72 67 28 70 43 6f 6e 74 65  nctionArg(pConte
41be7 78 74 2c 20 22 6d 61 74 63 68 69 6e 66 6f 22 2c  xt, "matchinfo",
41be8 20 61 70 56 61 6c 5b 30 5d 2c 20 26 70 43 73 72   apVal[0], &pCsr
41be9 29 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ) ){.    const c
41bea 68 61 72 20 2a 7a 41 72 67 20 3d 20 30 3b 0a 20  har *zArg = 0;. 
41beb 20 20 20 69 66 28 20 6e 56 61 6c 3e 31 20 29 7b     if( nVal>1 ){
41bec 0a 20 20 20 20 20 20 7a 41 72 67 20 3d 20 28 63  .      zArg = (c
41bed 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
41bee 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
41bef 70 56 61 6c 5b 31 5d 29 3b 0a 20 20 20 20 7d 0a  pVal[1]);.    }.
41bf0 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 4d      sqlite3Fts3M
41bf1 61 74 63 68 69 6e 66 6f 28 70 43 6f 6e 74 65 78  atchinfo(pContex
41bf2 74 2c 20 70 43 73 72 2c 20 7a 41 72 67 29 3b 0a  t, pCsr, zArg);.
41bf3 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
41bf4 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d  s routine implem
41bf5 65 6e 74 73 20 74 68 65 20 78 46 69 6e 64 46 75  ents the xFindFu
41bf6 6e 63 74 69 6f 6e 20 6d 65 74 68 6f 64 20 66 6f  nction method fo
41bf7 72 20 74 68 65 20 46 54 53 33 0a 2a 2a 20 76 69  r the FTS3.** vi
41bf8 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a  rtual table..*/.
41bf9 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 46  static int fts3F
41bfa 69 6e 64 46 75 6e 63 74 69 6f 6e 4d 65 74 68 6f  indFunctionMetho
41bfb 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  d(.  sqlite3_vta
41bfc 62 20 2a 70 56 74 61 62 2c 20 20 20 20 20 20 20  b *pVtab,       
41bfd 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20       /* Virtual 
41bfe 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  table handle */.
41bff 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20    int nArg,     
41c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41c01 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 53    /* Number of S
41c02 51 4c 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75  QL function argu
41c03 6d 65 6e 74 73 20 2a 2f 0a 20 20 63 6f 6e 73 74  ments */.  const
41c04 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20   char *zName,   
41c05 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
41c06 6d 65 20 6f 66 20 53 51 4c 20 66 75 6e 63 74 69  me of SQL functi
41c07 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 2a  on */.  void (**
41c08 70 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  pxFunc)(sqlite3_
41c09 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
41c0a 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 20 2f  ite3_value**), /
41c0b 2a 20 4f 55 54 3a 20 52 65 73 75 6c 74 20 2a 2f  * OUT: Result */
41c0c 0a 20 20 76 6f 69 64 20 2a 2a 70 70 41 72 67 20  .  void **ppArg 
41c0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41c0e 20 20 20 2f 2a 20 55 6e 75 73 65 64 20 2a 2f 0a     /* Unused */.
41c0f 29 7b 0a 20 20 73 74 72 75 63 74 20 4f 76 65 72  ){.  struct Over
41c10 6c 6f 61 64 65 64 20 7b 0a 20 20 20 20 63 6f 6e  loaded {.    con
41c11 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a  st char *zName;.
41c12 20 20 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63      void (*xFunc
41c13 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
41c14 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
41c15 61 6c 75 65 2a 2a 29 3b 0a 20 20 7d 20 61 4f 76  alue**);.  } aOv
41c16 65 72 6c 6f 61 64 5b 5d 20 3d 20 7b 0a 20 20 20  erload[] = {.   
41c17 20 7b 20 22 73 6e 69 70 70 65 74 22 2c 20 66 74   { "snippet", ft
41c18 73 33 53 6e 69 70 70 65 74 46 75 6e 63 20 7d 2c  s3SnippetFunc },
41c19 0a 20 20 20 20 7b 20 22 6f 66 66 73 65 74 73 22  .    { "offsets"
41c1a 2c 20 66 74 73 33 4f 66 66 73 65 74 73 46 75 6e  , fts3OffsetsFun
41c1b 63 20 7d 2c 0a 20 20 20 20 7b 20 22 6f 70 74 69  c },.    { "opti
41c1c 6d 69 7a 65 22 2c 20 66 74 73 33 4f 70 74 69 6d  mize", fts3Optim
41c1d 69 7a 65 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b  izeFunc },.    {
41c1e 20 22 6d 61 74 63 68 69 6e 66 6f 22 2c 20 66 74   "matchinfo", ft
41c1f 73 33 4d 61 74 63 68 69 6e 66 6f 46 75 6e 63 20  s3MatchinfoFunc 
41c20 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b  },.  };.  int i;
41c21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41c22 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
41c23 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
41c24 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  /..  UNUSED_PARA
41c25 4d 45 54 45 52 28 70 56 74 61 62 29 3b 0a 20 20  METER(pVtab);.  
41c26 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
41c27 28 6e 41 72 67 29 3b 0a 20 20 55 4e 55 53 45 44  (nArg);.  UNUSED
41c28 5f 50 41 52 41 4d 45 54 45 52 28 70 70 41 72 67  _PARAMETER(ppArg
41c29 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  );..  for(i=0; i
41c2a 3c 53 69 7a 65 6f 66 41 72 72 61 79 28 61 4f 76  <SizeofArray(aOv
41c2b 65 72 6c 6f 61 64 29 3b 20 69 2b 2b 29 7b 0a 20  erload); i++){. 
41c2c 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 4e     if( strcmp(zN
41c2d 61 6d 65 2c 20 61 4f 76 65 72 6c 6f 61 64 5b 69  ame, aOverload[i
41c2e 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20  ].zName)==0 ){. 
41c2f 20 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 61       *pxFunc = a
41c30 4f 76 65 72 6c 6f 61 64 5b 69 5d 2e 78 46 75 6e  Overload[i].xFun
41c31 63 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  c;.      return 
41c32 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  1;.    }.  }..  
41c33 2f 2a 20 4e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f  /* No function o
41c34 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  f the specified 
41c35 6e 61 6d 65 20 77 61 73 20 66 6f 75 6e 64 2e 20  name was found. 
41c36 52 65 74 75 72 6e 20 30 2e 20 2a 2f 0a 20 20 72  Return 0. */.  r
41c37 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
41c38 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
41c39 20 6f 66 20 46 54 53 33 20 78 52 65 6e 61 6d 65   of FTS3 xRename
41c3a 20 6d 65 74 68 6f 64 2e 20 52 65 6e 61 6d 65 20   method. Rename 
41c3b 61 6e 20 66 74 73 33 20 74 61 62 6c 65 2e 0a 2a  an fts3 table..*
41c3c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
41c3d 33 52 65 6e 61 6d 65 4d 65 74 68 6f 64 28 0a 20  3RenameMethod(. 
41c3e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
41c3f 56 74 61 62 2c 20 20 20 20 20 20 20 20 20 20 20  Vtab,           
41c40 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c   /* Virtual tabl
41c41 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  e handle */.  co
41c42 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  nst char *zName 
41c43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
41c44 20 4e 65 77 20 6e 61 6d 65 20 6f 66 20 74 61 62   New name of tab
41c45 6c 65 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 54  le */.){.  Fts3T
41c46 61 62 6c 65 20 2a 70 20 3d 20 28 46 74 73 33 54  able *p = (Fts3T
41c47 61 62 6c 65 20 2a 29 70 56 74 61 62 3b 0a 20 20  able *)pVtab;.  
41c48 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d  sqlite3 *db = p-
41c49 3e 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  >db;            
41c4a 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
41c4b 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ection */.  int 
41c4c 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
41c4d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
41c4e 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 0a 20  eturn Code */.. 
41c4f 20 2f 2a 20 41 73 20 69 74 20 68 61 70 70 65 6e   /* As it happen
41c50 73 2c 20 74 68 65 20 70 65 6e 64 69 6e 67 20 74  s, the pending t
41c51 65 72 6d 73 20 74 61 62 6c 65 20 69 73 20 61 6c  erms table is al
41c52 77 61 79 73 20 65 6d 70 74 79 20 68 65 72 65 2e  ways empty here.
41c53 20 54 68 69 73 20 69 73 0a 20 20 2a 2a 20 62 65   This is.  ** be
41c54 63 61 75 73 65 20 61 6e 20 22 41 4c 54 45 52 20  cause an "ALTER 
41c55 54 41 42 4c 45 20 52 45 4e 41 4d 45 20 54 41 42  TABLE RENAME TAB
41c56 4c 45 22 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  LE" statement in
41c57 73 69 64 65 20 61 20 74 72 61 6e 73 61 63 74 69  side a transacti
41c58 6f 6e 20 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20  on .  ** always 
41c59 6f 70 65 6e 73 20 61 20 73 61 76 65 70 6f 69 6e  opens a savepoin
41c5a 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41  t transaction. A
41c5b 6e 64 20 74 68 65 20 78 53 61 76 65 70 6f 69 6e  nd the xSavepoin
41c5c 74 28 29 20 6d 65 74 68 6f 64 20 0a 20 20 2a 2a  t() method .  **
41c5d 20 66 6c 75 73 68 65 73 20 74 68 65 20 70 65 6e   flushes the pen
41c5e 64 69 6e 67 20 74 65 72 6d 73 20 74 61 62 6c 65  ding terms table
41c5f 2e 20 42 75 74 20 6c 65 61 76 65 20 74 68 65 20  . But leave the 
41c60 28 6e 6f 2d 6f 70 29 20 63 61 6c 6c 20 74 6f 0a  (no-op) call to.
41c61 20 20 2a 2a 20 50 65 6e 64 69 6e 67 54 65 72 6d    ** PendingTerm
41c62 73 46 6c 75 73 68 28 29 20 69 6e 20 69 6e 20 63  sFlush() in in c
41c63 61 73 65 20 74 68 61 74 20 63 68 61 6e 67 65 73  ase that changes
41c64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
41c65 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61   p->nPendingData
41c66 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  ==0 );.  rc = sq
41c67 6c 69 74 65 33 46 74 73 33 50 65 6e 64 69 6e 67  lite3Fts3Pending
41c68 54 65 72 6d 73 46 6c 75 73 68 28 70 29 3b 0a 0a  TermsFlush(p);..
41c69 20 20 69 66 28 20 70 2d 3e 7a 43 6f 6e 74 65 6e    if( p->zConten
41c6a 74 54 62 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 66  tTbl==0 ){.    f
41c6b 74 73 33 44 62 45 78 65 63 28 26 72 63 2c 20 64  ts3DbExec(&rc, d
41c6c 62 2c 0a 20 20 20 20 20 20 22 41 4c 54 45 52 20  b,.      "ALTER 
41c6d 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 63 6f 6e  TABLE %Q.'%q_con
41c6e 74 65 6e 74 27 20 20 52 45 4e 41 4d 45 20 54 4f  tent'  RENAME TO
41c6f 20 27 25 71 5f 63 6f 6e 74 65 6e 74 27 3b 22 2c   '%q_content';",
41c70 0a 20 20 20 20 20 20 70 2d 3e 7a 44 62 2c 20 70  .      p->zDb, p
41c71 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 0a 20  ->zName, zName. 
41c72 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28     );.  }..  if(
41c73 20 70 2d 3e 62 48 61 73 44 6f 63 73 69 7a 65 20   p->bHasDocsize 
41c74 29 7b 0a 20 20 20 20 66 74 73 33 44 62 45 78 65  ){.    fts3DbExe
41c75 63 28 26 72 63 2c 20 64 62 2c 0a 20 20 20 20 20  c(&rc, db,.     
41c76 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51   "ALTER TABLE %Q
41c77 2e 27 25 71 5f 64 6f 63 73 69 7a 65 27 20 20 52  .'%q_docsize'  R
41c78 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f 64 6f 63  ENAME TO '%q_doc
41c79 73 69 7a 65 27 3b 22 2c 0a 20 20 20 20 20 20 70  size';",.      p
41c7a 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c  ->zDb, p->zName,
41c7b 20 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20   zName.    );.  
41c7c 7d 0a 20 20 69 66 28 20 70 2d 3e 62 48 61 73 53  }.  if( p->bHasS
41c7d 74 61 74 20 29 7b 0a 20 20 20 20 66 74 73 33 44  tat ){.    fts3D
41c7e 62 45 78 65 63 28 26 72 63 2c 20 64 62 2c 0a 20  bExec(&rc, db,. 
41c7f 20 20 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c       "ALTER TABL
41c80 45 20 25 51 2e 27 25 71 5f 73 74 61 74 27 20 20  E %Q.'%q_stat'  
41c81 52 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f 73 74  RENAME TO '%q_st
41c82 61 74 27 3b 22 2c 0a 20 20 20 20 20 20 70 2d 3e  at';",.      p->
41c83 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a  zDb, p->zName, z
41c84 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20 7d 0a  Name.    );.  }.
41c85 20 20 66 74 73 33 44 62 45 78 65 63 28 26 72 63    fts3DbExec(&rc
41c86 2c 20 64 62 2c 0a 20 20 20 20 22 41 4c 54 45 52  , db,.    "ALTER
41c87 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 73 65   TABLE %Q.'%q_se
41c88 67 6d 65 6e 74 73 27 20 52 45 4e 41 4d 45 20 54  gments' RENAME T
41c89 4f 20 27 25 71 5f 73 65 67 6d 65 6e 74 73 27 3b  O '%q_segments';
41c8a 22 2c 0a 20 20 20 20 70 2d 3e 7a 44 62 2c 20 70  ",.    p->zDb, p
41c8b 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 0a 20  ->zName, zName. 
41c8c 20 29 3b 0a 20 20 66 74 73 33 44 62 45 78 65 63   );.  fts3DbExec
41c8d 28 26 72 63 2c 20 64 62 2c 0a 20 20 20 20 22 41  (&rc, db,.    "A
41c8e 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25  LTER TABLE %Q.'%
41c8f 71 5f 73 65 67 64 69 72 27 20 20 20 52 45 4e 41  q_segdir'   RENA
41c90 4d 45 20 54 4f 20 27 25 71 5f 73 65 67 64 69 72  ME TO '%q_segdir
41c91 27 3b 22 2c 0a 20 20 20 20 70 2d 3e 7a 44 62 2c  ';",.    p->zDb,
41c92 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65   p->zName, zName
41c93 0a 20 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  .  );.  return r
41c94 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
41c95 78 53 61 76 65 70 6f 69 6e 74 28 29 20 6d 65 74  xSavepoint() met
41c96 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 46 6c 75 73 68  hod..**.** Flush
41c97 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
41c98 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 74 65 72   the pending-ter
41c99 6d 73 20 74 61 62 6c 65 20 74 6f 20 64 69 73 6b  ms table to disk
41c9a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
41c9b 66 74 73 33 53 61 76 65 70 6f 69 6e 74 4d 65 74  fts3SavepointMet
41c9c 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62  hod(sqlite3_vtab
41c9d 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 69 53 61   *pVtab, int iSa
41c9e 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20  vepoint){.  int 
41c9f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
41ca0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
41ca1 45 52 28 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a  ER(iSavepoint);.
41ca2 20 20 61 73 73 65 72 74 28 20 28 28 46 74 73 33    assert( ((Fts3
41ca3 54 61 62 6c 65 20 2a 29 70 56 74 61 62 29 2d 3e  Table *)pVtab)->
41ca4 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b  inTransaction );
41ca5 0a 20 20 61 73 73 65 72 74 28 20 28 28 46 74 73  .  assert( ((Fts
41ca6 33 54 61 62 6c 65 20 2a 29 70 56 74 61 62 29 2d  3Table *)pVtab)-
41ca7 3e 6d 78 53 61 76 65 70 6f 69 6e 74 20 3c 20 69  >mxSavepoint < i
41ca8 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 54  Savepoint );.  T
41ca9 45 53 54 4f 4e 4c 59 28 20 28 28 46 74 73 33 54  ESTONLY( ((Fts3T
41caa 61 62 6c 65 20 2a 29 70 56 74 61 62 29 2d 3e 6d  able *)pVtab)->m
41cab 78 53 61 76 65 70 6f 69 6e 74 20 3d 20 69 53 61  xSavepoint = iSa
41cac 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 69 66 28  vepoint );.  if(
41cad 20 28 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70   ((Fts3Table *)p
41cae 56 74 61 62 29 2d 3e 62 49 67 6e 6f 72 65 53 61  Vtab)->bIgnoreSa
41caf 76 65 70 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20 20  vepoint==0 ){.  
41cb0 20 20 72 63 20 3d 20 66 74 73 33 53 79 6e 63 4d    rc = fts3SyncM
41cb1 65 74 68 6f 64 28 70 56 74 61 62 29 3b 0a 20 20  ethod(pVtab);.  
41cb2 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
41cb3 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 52 65 6c  ../*.** The xRel
41cb4 65 61 73 65 28 29 20 6d 65 74 68 6f 64 2e 0a 2a  ease() method..*
41cb5 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6e  *.** This is a n
41cb6 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
41cb7 69 6e 74 20 66 74 73 33 52 65 6c 65 61 73 65 4d  int fts3ReleaseM
41cb8 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74  ethod(sqlite3_vt
41cb9 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 69  ab *pVtab, int i
41cba 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 54 45  Savepoint){.  TE
41cbb 53 54 4f 4e 4c 59 28 20 46 74 73 33 54 61 62 6c  STONLY( Fts3Tabl
41cbc 65 20 2a 70 20 3d 20 28 46 74 73 33 54 61 62 6c  e *p = (Fts3Tabl
41cbd 65 2a 29 70 56 74 61 62 20 29 3b 0a 20 20 55 4e  e*)pVtab );.  UN
41cbe 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69  USED_PARAMETER(i
41cbf 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 55 4e  Savepoint);.  UN
41cc0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
41cc1 56 74 61 62 29 3b 0a 20 20 61 73 73 65 72 74 28  Vtab);.  assert(
41cc2 20 70 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f   p->inTransactio
41cc3 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  n );.  assert( p
41cc4 2d 3e 6d 78 53 61 76 65 70 6f 69 6e 74 20 3e 3d  ->mxSavepoint >=
41cc5 20 69 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20   iSavepoint );. 
41cc6 20 54 45 53 54 4f 4e 4c 59 28 20 70 2d 3e 6d 78   TESTONLY( p->mx
41cc7 53 61 76 65 70 6f 69 6e 74 20 3d 20 69 53 61 76  Savepoint = iSav
41cc8 65 70 6f 69 6e 74 2d 31 20 29 3b 0a 20 20 72 65  epoint-1 );.  re
41cc9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
41cca 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 52 6f  }../*.** The xRo
41ccb 6c 6c 62 61 63 6b 54 6f 28 29 20 6d 65 74 68 6f  llbackTo() metho
41ccc 64 2e 0a 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 64  d..**.** Discard
41ccd 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
41cce 20 74 68 65 20 70 65 6e 64 69 6e 67 20 74 65 72   the pending ter
41ccf 6d 73 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  ms table..*/.sta
41cd0 74 69 63 20 69 6e 74 20 66 74 73 33 52 6f 6c 6c  tic int fts3Roll
41cd1 62 61 63 6b 54 6f 4d 65 74 68 6f 64 28 73 71 6c  backToMethod(sql
41cd2 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
41cd3 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74  , int iSavepoint
41cd4 29 7b 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  ){.  Fts3Table *
41cd5 70 20 3d 20 28 46 74 73 33 54 61 62 6c 65 2a 29  p = (Fts3Table*)
41cd6 70 56 74 61 62 3b 0a 20 20 55 4e 55 53 45 44 5f  pVtab;.  UNUSED_
41cd7 50 41 52 41 4d 45 54 45 52 28 69 53 61 76 65 70  PARAMETER(iSavep
41cd8 6f 69 6e 74 29 3b 0a 20 20 61 73 73 65 72 74 28  oint);.  assert(
41cd9 20 70 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f   p->inTransactio
41cda 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  n );.  assert( p
41cdb 2d 3e 6d 78 53 61 76 65 70 6f 69 6e 74 20 3e 3d  ->mxSavepoint >=
41cdc 20 69 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20   iSavepoint );. 
41cdd 20 54 45 53 54 4f 4e 4c 59 28 20 70 2d 3e 6d 78   TESTONLY( p->mx
41cde 53 61 76 65 70 6f 69 6e 74 20 3d 20 69 53 61 76  Savepoint = iSav
41cdf 65 70 6f 69 6e 74 20 29 3b 0a 20 20 73 71 6c 69  epoint );.  sqli
41ce0 74 65 33 46 74 73 33 50 65 6e 64 69 6e 67 54 65  te3Fts3PendingTe
41ce1 72 6d 73 43 6c 65 61 72 28 70 29 3b 0a 20 20 72  rmsClear(p);.  r
41ce2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
41ce3 0a 7d 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .}..static const
41ce4 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
41ce5 66 74 73 33 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20  fts3Module = {. 
41ce6 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 20 20 20   /* iVersion    
41ce7 20 20 2a 2f 20 32 2c 0a 20 20 2f 2a 20 78 43 72    */ 2,.  /* xCr
41ce8 65 61 74 65 20 20 20 20 20 20 20 2a 2f 20 66 74  eate       */ ft
41ce9 73 33 43 72 65 61 74 65 4d 65 74 68 6f 64 2c 0a  s3CreateMethod,.
41cea 20 20 2f 2a 20 78 43 6f 6e 6e 65 63 74 20 20 20    /* xConnect   
41ceb 20 20 20 2a 2f 20 66 74 73 33 43 6f 6e 6e 65 63     */ fts3Connec
41cec 74 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 42  tMethod,.  /* xB
41ced 65 73 74 49 6e 64 65 78 20 20 20 20 2a 2f 20 66  estIndex    */ f
41cee 74 73 33 42 65 73 74 49 6e 64 65 78 4d 65 74 68  ts3BestIndexMeth
41cef 6f 64 2c 0a 20 20 2f 2a 20 78 44 69 73 63 6f 6e  od,.  /* xDiscon
41cf0 6e 65 63 74 20 20 20 2a 2f 20 66 74 73 33 44 69  nect   */ fts3Di
41cf1 73 63 6f 6e 6e 65 63 74 4d 65 74 68 6f 64 2c 0a  sconnectMethod,.
41cf2 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 20 20    /* xDestroy   
41cf3 20 20 20 2a 2f 20 66 74 73 33 44 65 73 74 72 6f     */ fts3Destro
41cf4 79 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 4f  yMethod,.  /* xO
41cf5 70 65 6e 20 20 20 20 20 20 20 20 20 2a 2f 20 66  pen         */ f
41cf6 74 73 33 4f 70 65 6e 4d 65 74 68 6f 64 2c 0a 20  ts3OpenMethod,. 
41cf7 20 2f 2a 20 78 43 6c 6f 73 65 20 20 20 20 20 20   /* xClose      
41cf8 20 20 2a 2f 20 66 74 73 33 43 6c 6f 73 65 4d 65    */ fts3CloseMe
41cf9 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 46 69 6c 74  thod,.  /* xFilt
41cfa 65 72 20 20 20 20 20 20 20 2a 2f 20 66 74 73 33  er       */ fts3
41cfb 46 69 6c 74 65 72 4d 65 74 68 6f 64 2c 0a 20 20  FilterMethod,.  
41cfc 2f 2a 20 78 4e 65 78 74 20 20 20 20 20 20 20 20  /* xNext        
41cfd 20 2a 2f 20 66 74 73 33 4e 65 78 74 4d 65 74 68   */ fts3NextMeth
41cfe 6f 64 2c 0a 20 20 2f 2a 20 78 45 6f 66 20 20 20  od,.  /* xEof   
41cff 20 20 20 20 20 20 20 2a 2f 20 66 74 73 33 45 6f         */ fts3Eo
41d00 66 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 43  fMethod,.  /* xC
41d01 6f 6c 75 6d 6e 20 20 20 20 20 20 20 2a 2f 20 66  olumn       */ f
41d02 74 73 33 43 6f 6c 75 6d 6e 4d 65 74 68 6f 64 2c  ts3ColumnMethod,
41d03 0a 20 20 2f 2a 20 78 52 6f 77 69 64 20 20 20 20  .  /* xRowid    
41d04 20 20 20 20 2a 2f 20 66 74 73 33 52 6f 77 69 64      */ fts3Rowid
41d05 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 55 70  Method,.  /* xUp
41d06 64 61 74 65 20 20 20 20 20 20 20 2a 2f 20 66 74  date       */ ft
41d07 73 33 55 70 64 61 74 65 4d 65 74 68 6f 64 2c 0a  s3UpdateMethod,.
41d08 20 20 2f 2a 20 78 42 65 67 69 6e 20 20 20 20 20    /* xBegin     
41d09 20 20 20 2a 2f 20 66 74 73 33 42 65 67 69 6e 4d     */ fts3BeginM
41d0a 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 53 79 6e  ethod,.  /* xSyn
41d0b 63 20 20 20 20 20 20 20 20 20 2a 2f 20 66 74 73  c         */ fts
41d0c 33 53 79 6e 63 4d 65 74 68 6f 64 2c 0a 20 20 2f  3SyncMethod,.  /
41d0d 2a 20 78 43 6f 6d 6d 69 74 20 20 20 20 20 20 20  * xCommit       
41d0e 2a 2f 20 66 74 73 33 43 6f 6d 6d 69 74 4d 65 74  */ fts3CommitMet
41d0f 68 6f 64 2c 0a 20 20 2f 2a 20 78 52 6f 6c 6c 62  hod,.  /* xRollb
41d10 61 63 6b 20 20 20 20 20 2a 2f 20 66 74 73 33 52  ack     */ fts3R
41d11 6f 6c 6c 62 61 63 6b 4d 65 74 68 6f 64 2c 0a 20  ollbackMethod,. 
41d12 20 2f 2a 20 78 46 69 6e 64 46 75 6e 63 74 69 6f   /* xFindFunctio
41d13 6e 20 2a 2f 20 66 74 73 33 46 69 6e 64 46 75 6e  n */ fts3FindFun
41d14 63 74 69 6f 6e 4d 65 74 68 6f 64 2c 0a 20 20 2f  ctionMethod,.  /
41d15 2a 20 78 52 65 6e 61 6d 65 20 2a 2f 20 20 20 20  * xRename */    
41d16 20 20 20 66 74 73 33 52 65 6e 61 6d 65 4d 65 74     fts3RenameMet
41d17 68 6f 64 2c 0a 20 20 2f 2a 20 78 53 61 76 65 70  hod,.  /* xSavep
41d18 6f 69 6e 74 20 20 20 20 2a 2f 20 66 74 73 33 53  oint    */ fts3S
41d19 61 76 65 70 6f 69 6e 74 4d 65 74 68 6f 64 2c 0a  avepointMethod,.
41d1a 20 20 2f 2a 20 78 52 65 6c 65 61 73 65 20 20 20    /* xRelease   
41d1b 20 20 20 2a 2f 20 66 74 73 33 52 65 6c 65 61 73     */ fts3Releas
41d1c 65 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 52  eMethod,.  /* xR
41d1d 6f 6c 6c 62 61 63 6b 54 6f 20 20 20 2a 2f 20 66  ollbackTo   */ f
41d1e 74 73 33 52 6f 6c 6c 62 61 63 6b 54 6f 4d 65 74  ts3RollbackToMet
41d1f 68 6f 64 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  hod,.};../*.** T
41d20 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
41d21 72 65 67 69 73 74 65 72 65 64 20 61 73 20 74 68  registered as th
41d22 65 20 6d 6f 64 75 6c 65 20 64 65 73 74 72 75 63  e module destruc
41d23 74 6f 72 20 28 63 61 6c 6c 65 64 20 77 68 65 6e  tor (called when
41d24 20 61 6e 0a 2a 2a 20 46 54 53 33 20 65 6e 61 62   an.** FTS3 enab
41d25 6c 65 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e  led database con
41d26 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65  nection is close
41d27 64 29 2e 20 49 74 20 66 72 65 65 73 20 74 68 65  d). It frees the
41d28 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63   memory.** alloc
41d29 61 74 65 64 20 66 6f 72 20 74 68 65 20 74 6f 6b  ated for the tok
41d2a 65 6e 69 7a 65 72 20 68 61 73 68 20 74 61 62 6c  enizer hash tabl
41d2b 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
41d2c 64 20 68 61 73 68 44 65 73 74 72 6f 79 28 76 6f  d hashDestroy(vo
41d2d 69 64 20 2a 70 29 7b 0a 20 20 46 74 73 33 48 61  id *p){.  Fts3Ha
41d2e 73 68 20 2a 70 48 61 73 68 20 3d 20 28 46 74 73  sh *pHash = (Fts
41d2f 33 48 61 73 68 20 2a 29 70 3b 0a 20 20 73 71 6c  3Hash *)p;.  sql
41d30 69 74 65 33 46 74 73 33 48 61 73 68 43 6c 65 61  ite3Fts3HashClea
41d31 72 28 70 48 61 73 68 29 3b 0a 20 20 73 71 6c 69  r(pHash);.  sqli
41d32 74 65 33 5f 66 72 65 65 28 70 48 61 73 68 29 3b  te3_free(pHash);
41d33 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 74  .}../*.** The ft
41d34 73 33 20 62 75 69 6c 74 2d 69 6e 20 74 6f 6b 65  s3 built-in toke
41d35 6e 69 7a 65 72 73 20 2d 20 22 73 69 6d 70 6c 65  nizers - "simple
41d36 22 2c 20 22 70 6f 72 74 65 72 22 20 61 6e 64 20  ", "porter" and 
41d37 22 69 63 75 22 2d 20 61 72 65 20 0a 2a 2a 20 69  "icu"- are .** i
41d38 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 66 69  mplemented in fi
41d39 6c 65 73 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  les fts3_tokeniz
41d3a 65 72 31 2e 63 2c 20 66 74 73 33 5f 70 6f 72 74  er1.c, fts3_port
41d3b 65 72 2e 63 20 61 6e 64 20 66 74 73 33 5f 69 63  er.c and fts3_ic
41d3c 75 2e 63 0a 2a 2a 20 72 65 73 70 65 63 74 69 76  u.c.** respectiv
41d3d 65 6c 79 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ely. The followi
41d3e 6e 67 20 74 68 72 65 65 20 66 6f 72 77 61 72 64  ng three forward
41d3f 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 61 72   declarations ar
41d40 65 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 0a  e for functions.
41d41 2a 2a 20 64 65 63 6c 61 72 65 64 20 69 6e 20 74  ** declared in t
41d42 68 65 73 65 20 66 69 6c 65 73 20 75 73 65 64 20  hese files used 
41d43 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20  to retrieve the 
41d44 72 65 73 70 65 63 74 69 76 65 20 69 6d 70 6c 65  respective imple
41d45 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  mentations..**.*
41d46 2a 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65  * Calling sqlite
41d47 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e  3Fts3SimpleToken
41d48 69 7a 65 72 4d 6f 64 75 6c 65 28 29 20 73 65 74  izerModule() set
41d49 73 20 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e  s the value poin
41d4a 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65  ted.** to by the
41d4b 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 70 6f 69   argument to poi
41d4c 6e 74 20 74 6f 20 74 68 65 20 22 73 69 6d 70 6c  nt to the "simpl
41d4d 65 22 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6d 70  e" tokenizer imp
41d4e 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20  lementation..** 
41d4f 41 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2f 0a 53 51  And so on..*/.SQ
41d50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
41d51 64 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d  d sqlite3Fts3Sim
41d52 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  pleTokenizerModu
41d53 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  le(sqlite3_token
41d54 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
41d55 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 53 51  t**ppModule);.SQ
41d56 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
41d57 64 20 73 71 6c 69 74 65 33 46 74 73 33 50 6f 72  d sqlite3Fts3Por
41d58 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  terTokenizerModu
41d59 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  le(sqlite3_token
41d5a 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
41d5b 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 23 69  t**ppModule);.#i
41d5c 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
41d5d 4c 45 5f 46 54 53 34 5f 55 4e 49 43 4f 44 45 36  LE_FTS4_UNICODE6
41d5e 31 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  1.SQLITE_PRIVATE
41d5f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
41d60 33 55 6e 69 63 6f 64 65 54 6f 6b 65 6e 69 7a 65  3UnicodeTokenize
41d61 72 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  r(sqlite3_tokeni
41d62 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
41d63 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 23 65 6e  **ppModule);.#en
41d64 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
41d65 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 53 51 4c  E_ENABLE_ICU.SQL
41d66 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
41d67 20 73 71 6c 69 74 65 33 46 74 73 33 49 63 75 54   sqlite3Fts3IcuT
41d68 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73  okenizerModule(s
41d69 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
41d6a 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70  _module const**p
41d6b 70 4d 6f 64 75 6c 65 29 3b 0a 23 65 6e 64 69 66  pModule);.#endif
41d6c 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
41d6d 73 65 20 74 68 65 20 66 74 73 33 20 65 78 74 65  se the fts3 exte
41d6e 6e 73 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 65  nsion. If this e
41d6f 78 74 65 6e 73 69 6f 6e 20 69 73 20 62 75 69 6c  xtension is buil
41d70 74 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20  t as part.** of 
41d71 74 68 65 20 73 71 6c 69 74 65 20 6c 69 62 72 61  the sqlite libra
41d72 72 79 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75  ry, then this fu
41d73 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
41d74 20 64 69 72 65 63 74 6c 79 20 62 79 0a 2a 2a 20   directly by.** 
41d75 53 51 4c 69 74 65 2e 20 49 66 20 66 74 73 33 20  SQLite. If fts3 
41d76 69 73 20 62 75 69 6c 74 20 61 73 20 61 20 64 79  is built as a dy
41d77 6e 61 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 61 62  namically loadab
41d78 6c 65 20 65 78 74 65 6e 73 69 6f 6e 2c 20 74 68  le extension, th
41d79 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69  is.** function i
41d7a 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
41d7b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f  sqlite3_extensio
41d7c 6e 5f 69 6e 69 74 28 29 20 65 6e 74 72 79 20 70  n_init() entry p
41d7d 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oint..*/.SQLITE_
41d7e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
41d7f 74 65 33 46 74 73 33 49 6e 69 74 28 73 71 6c 69  te3Fts3Init(sqli
41d80 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
41d81 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
41d82 20 20 46 74 73 33 48 61 73 68 20 2a 70 48 61 73    Fts3Hash *pHas
41d83 68 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73  h = 0;.  const s
41d84 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
41d85 5f 6d 6f 64 75 6c 65 20 2a 70 53 69 6d 70 6c 65  _module *pSimple
41d86 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71   = 0;.  const sq
41d87 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
41d88 6d 6f 64 75 6c 65 20 2a 70 50 6f 72 74 65 72 20  module *pPorter 
41d89 3d 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  = 0;.#ifdef SQLI
41d8a 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 34 5f 55  TE_ENABLE_FTS4_U
41d8b 4e 49 43 4f 44 45 36 31 0a 20 20 63 6f 6e 73 74  NICODE61.  const
41d8c 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
41d8d 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 55 6e 69 63  er_module *pUnic
41d8e 6f 64 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  ode = 0;.#endif.
41d8f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
41d90 4e 41 42 4c 45 5f 49 43 55 0a 20 20 63 6f 6e 73  NABLE_ICU.  cons
41d91 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
41d92 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 49 63 75  zer_module *pIcu
41d93 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 46   = 0;.  sqlite3F
41d94 74 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d  ts3IcuTokenizerM
41d95 6f 64 75 6c 65 28 26 70 49 63 75 29 3b 0a 23 65  odule(&pIcu);.#e
41d96 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
41d97 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 34 5f  ITE_ENABLE_FTS4_
41d98 55 4e 49 43 4f 44 45 36 31 0a 20 20 73 71 6c 69  UNICODE61.  sqli
41d99 74 65 33 46 74 73 33 55 6e 69 63 6f 64 65 54 6f  te3Fts3UnicodeTo
41d9a 6b 65 6e 69 7a 65 72 28 26 70 55 6e 69 63 6f 64  kenizer(&pUnicod
41d9b 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  e);.#endif..#ifd
41d9c 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
41d9d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
41d9e 33 49 6e 69 74 54 65 72 6d 28 64 62 29 3b 0a 20  3InitTerm(db);. 
41d9f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
41da0 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
41da1 23 65 6e 64 69 66 0a 0a 20 20 72 63 20 3d 20 73  #endif..  rc = s
41da2 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 41 75  qlite3Fts3InitAu
41da3 78 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63 21  x(db);.  if( rc!
41da4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
41da5 75 72 6e 20 72 63 3b 0a 0a 20 20 73 71 6c 69 74  urn rc;..  sqlit
41da6 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65  e3Fts3SimpleToke
41da7 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70 53 69  nizerModule(&pSi
41da8 6d 70 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  mple);.  sqlite3
41da9 46 74 73 33 50 6f 72 74 65 72 54 6f 6b 65 6e 69  Fts3PorterTokeni
41daa 7a 65 72 4d 6f 64 75 6c 65 28 26 70 50 6f 72 74  zerModule(&pPort
41dab 65 72 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  er);..  /* Alloc
41dac 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ate and initiali
41dad 73 65 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c  se the hash-tabl
41dae 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  e used to store 
41daf 74 6f 6b 65 6e 69 7a 65 72 73 2e 20 2a 2f 0a 20  tokenizers. */. 
41db0 20 70 48 61 73 68 20 3d 20 73 71 6c 69 74 65 33   pHash = sqlite3
41db1 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46  _malloc(sizeof(F
41db2 74 73 33 48 61 73 68 29 29 3b 0a 20 20 69 66 28  ts3Hash));.  if(
41db3 20 21 70 48 61 73 68 20 29 7b 0a 20 20 20 20 72   !pHash ){.    r
41db4 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
41db5 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
41db6 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
41db7 69 74 28 70 48 61 73 68 2c 20 46 54 53 33 5f 48  it(pHash, FTS3_H
41db8 41 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a  ASH_STRING, 1);.
41db9 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 74    }..  /* Load t
41dba 68 65 20 62 75 69 6c 74 2d 69 6e 20 74 6f 6b 65  he built-in toke
41dbb 6e 69 7a 65 72 73 20 69 6e 74 6f 20 74 68 65 20  nizers into the 
41dbc 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
41dbd 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
41dbe 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  K ){.    if( sql
41dbf 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
41dc0 72 74 28 70 48 61 73 68 2c 20 22 73 69 6d 70 6c  rt(pHash, "simpl
41dc1 65 22 2c 20 37 2c 20 28 76 6f 69 64 20 2a 29 70  e", 7, (void *)p
41dc2 53 69 6d 70 6c 65 29 0a 20 20 20 20 20 7c 7c 20  Simple).     || 
41dc3 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
41dc4 6e 73 65 72 74 28 70 48 61 73 68 2c 20 22 70 6f  nsert(pHash, "po
41dc5 72 74 65 72 22 2c 20 37 2c 20 28 76 6f 69 64 20  rter", 7, (void 
41dc6 2a 29 70 50 6f 72 74 65 72 29 20 0a 0a 23 69 66  *)pPorter) ..#if
41dc7 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
41dc8 45 5f 46 54 53 34 5f 55 4e 49 43 4f 44 45 36 31  E_FTS4_UNICODE61
41dc9 0a 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33  .     || sqlite3
41dca 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 70  Fts3HashInsert(p
41dcb 48 61 73 68 2c 20 22 75 6e 69 63 6f 64 65 36 31  Hash, "unicode61
41dcc 22 2c 20 31 30 2c 20 28 76 6f 69 64 20 2a 29 70  ", 10, (void *)p
41dcd 55 6e 69 63 6f 64 65 29 20 0a 23 65 6e 64 69 66  Unicode) .#endif
41dce 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
41dcf 4e 41 42 4c 45 5f 49 43 55 0a 20 20 20 20 20 7c  NABLE_ICU.     |
41dd0 7c 20 28 70 49 63 75 20 26 26 20 73 71 6c 69 74  | (pIcu && sqlit
41dd1 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
41dd2 28 70 48 61 73 68 2c 20 22 69 63 75 22 2c 20 34  (pHash, "icu", 4
41dd3 2c 20 28 76 6f 69 64 20 2a 29 70 49 63 75 29 29  , (void *)pIcu))
41dd4 0a 23 65 6e 64 69 66 0a 20 20 20 20 29 7b 0a 20  .#endif.    ){. 
41dd5 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
41dd6 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
41dd7 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
41dd8 5f 54 45 53 54 0a 20 20 69 66 28 20 72 63 3d 3d  _TEST.  if( rc==
41dd9 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
41dda 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
41ddb 33 45 78 70 72 49 6e 69 74 54 65 73 74 49 6e 74  3ExprInitTestInt
41ddc 65 72 66 61 63 65 28 64 62 29 3b 0a 20 20 7d 0a  erface(db);.  }.
41ddd 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 43 72 65  #endif..  /* Cre
41dde 61 74 65 20 74 68 65 20 76 69 72 74 75 61 6c 20  ate the virtual 
41ddf 74 61 62 6c 65 20 77 72 61 70 70 65 72 20 61 72  table wrapper ar
41de0 6f 75 6e 64 20 74 68 65 20 68 61 73 68 2d 74 61  ound the hash-ta
41de1 62 6c 65 20 61 6e 64 20 6f 76 65 72 6c 6f 61 64  ble and overload
41de2 20 0a 20 20 2a 2a 20 74 68 65 20 74 77 6f 20 73   .  ** the two s
41de3 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 73 2e  calar functions.
41de4 20 49 66 20 74 68 69 73 20 69 73 20 73 75 63 63   If this is succ
41de5 65 73 73 66 75 6c 2c 20 72 65 67 69 73 74 65 72  essful, register
41de6 20 74 68 65 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65   the.  ** module
41de7 20 77 69 74 68 20 73 71 6c 69 74 65 2e 0a 20 20   with sqlite..  
41de8 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  */.  if( SQLITE_
41de9 4f 4b 3d 3d 72 63 20 0a 20 20 20 26 26 20 53 51  OK==rc .   && SQ
41dea 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73  LITE_OK==(rc = s
41deb 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61  qlite3Fts3InitHa
41dec 73 68 54 61 62 6c 65 28 64 62 2c 20 70 48 61 73  shTable(db, pHas
41ded 68 2c 20 22 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  h, "fts3_tokeniz
41dee 65 72 22 29 29 0a 20 20 20 26 26 20 53 51 4c 49  er")).   && SQLI
41def 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
41df0 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75  ite3_overload_fu
41df1 6e 63 74 69 6f 6e 28 64 62 2c 20 22 73 6e 69 70  nction(db, "snip
41df2 70 65 74 22 2c 20 2d 31 29 29 0a 20 20 20 26 26  pet", -1)).   &&
41df3 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
41df4 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f  = sqlite3_overlo
41df5 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20  ad_function(db, 
41df6 22 6f 66 66 73 65 74 73 22 2c 20 31 29 29 0a 20  "offsets", 1)). 
41df7 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d    && SQLITE_OK==
41df8 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76  (rc = sqlite3_ov
41df9 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28  erload_function(
41dfa 64 62 2c 20 22 6d 61 74 63 68 69 6e 66 6f 22 2c  db, "matchinfo",
41dfb 20 31 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54   1)).   && SQLIT
41dfc 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
41dfd 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  te3_overload_fun
41dfe 63 74 69 6f 6e 28 64 62 2c 20 22 6d 61 74 63 68  ction(db, "match
41dff 69 6e 66 6f 22 2c 20 32 29 29 0a 20 20 20 26 26  info", 2)).   &&
41e00 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
41e01 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f  = sqlite3_overlo
41e02 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20  ad_function(db, 
41e03 22 6f 70 74 69 6d 69 7a 65 22 2c 20 31 29 29 0a  "optimize", 1)).
41e04 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71    ){.    rc = sq
41e05 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
41e06 75 6c 65 5f 76 32 28 0a 20 20 20 20 20 20 20 20  ule_v2(.        
41e07 64 62 2c 20 22 66 74 73 33 22 2c 20 26 66 74 73  db, "fts3", &fts
41e08 33 4d 6f 64 75 6c 65 2c 20 28 76 6f 69 64 20 2a  3Module, (void *
41e09 29 70 48 61 73 68 2c 20 68 61 73 68 44 65 73 74  )pHash, hashDest
41e0a 72 6f 79 0a 20 20 20 20 29 3b 0a 20 20 20 20 69  roy.    );.    i
41e0b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
41e0c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
41e0d 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
41e0e 64 75 6c 65 5f 76 32 28 0a 20 20 20 20 20 20 20  dule_v2(.       
41e0f 20 20 20 64 62 2c 20 22 66 74 73 34 22 2c 20 26     db, "fts4", &
41e10 66 74 73 33 4d 6f 64 75 6c 65 2c 20 28 76 6f 69  fts3Module, (voi
41e11 64 20 2a 29 70 48 61 73 68 2c 20 30 0a 20 20 20  d *)pHash, 0.   
41e12 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20     );.    }.    
41e13 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
41e14 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 68 61    /* An error ha
41e15 73 20 6f 63 63 75 72 72 65 64 2e 20 44 65 6c 65  s occurred. Dele
41e16 74 65 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  te the hash tabl
41e17 65 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  e and return the
41e18 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a   error code. */.
41e19 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
41e1a 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28  LITE_OK );.  if(
41e1b 20 70 48 61 73 68 20 29 7b 0a 20 20 20 20 73 71   pHash ){.    sq
41e1c 6c 69 74 65 33 46 74 73 33 48 61 73 68 43 6c 65  lite3Fts3HashCle
41e1d 61 72 28 70 48 61 73 68 29 3b 0a 20 20 20 20 73  ar(pHash);.    s
41e1e 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 61 73  qlite3_free(pHas
41e1f 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  h);.  }.  return
41e20 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c   rc;.}../*.** Al
41e21 6c 6f 63 61 74 65 20 61 6e 20 46 74 73 33 4d 75  locate an Fts3Mu
41e22 6c 74 69 53 65 67 52 65 61 64 65 72 20 66 6f 72  ltiSegReader for
41e23 20 65 61 63 68 20 74 6f 6b 65 6e 20 69 6e 20 74   each token in t
41e24 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 68 65  he expression he
41e25 61 64 65 64 0a 2a 2a 20 62 79 20 70 45 78 70 72  aded.** by pExpr
41e26 2e 20 0a 2a 2a 0a 2a 2a 20 41 6e 20 46 74 73 33  . .**.** An Fts3
41e27 53 65 67 52 65 61 64 65 72 20 6f 62 6a 65 63 74  SegReader object
41e28 20 69 73 20 61 20 63 75 72 73 6f 72 20 74 68 61   is a cursor tha
41e29 74 20 63 61 6e 20 73 65 65 6b 20 6f 72 20 73 63  t can seek or sc
41e2a 61 6e 20 61 20 72 61 6e 67 65 20 6f 66 0a 2a 2a  an a range of.**
41e2b 20 65 6e 74 72 69 65 73 20 77 69 74 68 69 6e 20   entries within 
41e2c 61 20 73 69 6e 67 6c 65 20 73 65 67 6d 65 6e 74  a single segment
41e2d 20 62 2d 74 72 65 65 2e 20 41 6e 20 46 74 73 33   b-tree. An Fts3
41e2e 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72 20 75  MultiSegReader u
41e2f 73 65 73 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20  ses multiple.** 
41e30 46 74 73 33 53 65 67 52 65 61 64 65 72 20 6f 62  Fts3SegReader ob
41e31 6a 65 63 74 73 20 69 6e 74 65 72 6e 61 6c 6c 79  jects internally
41e32 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 69   to provide an i
41e33 6e 74 65 72 66 61 63 65 20 74 6f 20 73 65 65 6b  nterface to seek
41e34 20 6f 72 20 73 63 61 6e 0a 2a 2a 20 77 69 74 68   or scan.** with
41e35 69 6e 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20  in the union of 
41e36 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 6f 66 20  all segments of 
41e37 61 20 62 2d 74 72 65 65 2e 20 48 65 6e 63 65 20  a b-tree. Hence 
41e38 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  the name..**.** 
41e39 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64  If the allocated
41e3a 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61   Fts3MultiSegRea
41e3b 64 65 72 20 6a 75 73 74 20 73 65 65 6b 73 20 74  der just seeks t
41e3c 6f 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79  o a single entry
41e3d 20 69 6e 20 61 0a 2a 2a 20 73 65 67 6d 65 6e 74   in a.** segment
41e3e 20 62 2d 74 72 65 65 20 28 69 66 20 74 68 65 20   b-tree (if the 
41e3f 74 65 72 6d 20 69 73 20 6e 6f 74 20 61 20 70 72  term is not a pr
41e40 65 66 69 78 20 6f 72 20 69 74 20 69 73 20 61 20  efix or it is a 
41e41 70 72 65 66 69 78 20 66 6f 72 20 77 68 69 63 68  prefix for which
41e42 0a 2a 2a 20 74 68 65 72 65 20 65 78 69 73 74 73  .** there exists
41e43 20 70 72 65 66 69 78 20 62 2d 74 72 65 65 20 6f   prefix b-tree o
41e44 66 20 74 68 65 20 72 69 67 68 74 20 6c 65 6e 67  f the right leng
41e45 74 68 29 20 74 68 65 6e 20 69 74 20 6d 61 79 20  th) then it may 
41e46 62 65 20 74 72 61 76 65 72 73 65 64 0a 2a 2a 20  be traversed.** 
41e47 61 6e 64 20 6d 65 72 67 65 64 20 69 6e 63 72 65  and merged incre
41e48 6d 65 6e 74 61 6c 6c 79 2e 20 4f 74 68 65 72 77  mentally. Otherw
41e49 69 73 65 2c 20 69 74 20 68 61 73 20 74 6f 20 62  ise, it has to b
41e4a 65 20 6d 65 72 67 65 64 20 69 6e 74 6f 20 61 6e  e merged into an
41e4b 20 69 6e 2d 6d 65 6d 6f 72 79 20 0a 2a 2a 20 64   in-memory .** d
41e4c 6f 63 6c 69 73 74 20 61 6e 64 20 74 68 65 6e 20  oclist and then 
41e4d 74 72 61 76 65 72 73 65 64 2e 0a 2a 2f 0a 73 74  traversed..*/.st
41e4e 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 45 76  atic void fts3Ev
41e4f 61 6c 41 6c 6c 6f 63 61 74 65 52 65 61 64 65 72  alAllocateReader
41e50 73 28 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20  s(.  Fts3Cursor 
41e51 2a 70 43 73 72 2c 20 20 20 20 20 20 20 20 20 20  *pCsr,          
41e52 20 20 20 20 20 2f 2a 20 46 54 53 20 63 75 72 73       /* FTS curs
41e53 6f 72 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 46  or handle */.  F
41e54 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 2c 20  ts3Expr *pExpr, 
41e55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
41e56 2a 20 41 6c 6c 6f 63 61 74 65 20 72 65 61 64 65  * Allocate reade
41e57 72 73 20 66 6f 72 20 74 68 69 73 20 65 78 70 72  rs for this expr
41e58 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ession */.  int 
41e59 2a 70 6e 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20  *pnToken,       
41e5a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
41e5b 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72  UT: Total number
41e5c 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 70 68   of tokens in ph
41e5d 72 61 73 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 2a  rase. */.  int *
41e5e 70 6e 4f 72 2c 20 20 20 20 20 20 20 20 20 20 20  pnOr,           
41e5f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
41e60 54 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20  T: Total number 
41e61 6f 66 20 4f 52 20 6e 6f 64 65 73 20 69 6e 20 65  of OR nodes in e
41e62 78 70 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  xpr. */.  int *p
41e63 52 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Rc              
41e64 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f            /* IN/
41e65 4f 55 54 3a 20 45 72 72 6f 72 20 63 6f 64 65 20  OUT: Error code 
41e66 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 45 78 70  */.){.  if( pExp
41e67 72 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d  r && SQLITE_OK==
41e68 2a 70 52 63 20 29 7b 0a 20 20 20 20 69 66 28 20  *pRc ){.    if( 
41e69 70 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d 46 54  pExpr->eType==FT
41e6a 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29 7b  SQUERY_PHRASE ){
41e6b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
41e6c 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d      int nToken =
41e6d 20 70 45 78 70 72 2d 3e 70 50 68 72 61 73 65 2d   pExpr->pPhrase-
41e6e 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 2a  >nToken;.      *
41e6f 70 6e 54 6f 6b 65 6e 20 2b 3d 20 6e 54 6f 6b 65  pnToken += nToke
41e70 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  n;.      for(i=0
41e71 3b 20 69 3c 6e 54 6f 6b 65 6e 3b 20 69 2b 2b 29  ; i<nToken; i++)
41e72 7b 0a 20 20 20 20 20 20 20 20 46 74 73 33 50 68  {.        Fts3Ph
41e73 72 61 73 65 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65  raseToken *pToke
41e74 6e 20 3d 20 26 70 45 78 70 72 2d 3e 70 50 68 72  n = &pExpr->pPhr
41e75 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 3b 0a  ase->aToken[i];.
41e76 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d          int rc =
41e77 20 66 74 73 33 54 65 72 6d 53 65 67 52 65 61 64   fts3TermSegRead
41e78 65 72 43 75 72 73 6f 72 28 70 43 73 72 2c 20 0a  erCursor(pCsr, .
41e79 20 20 20 20 20 20 20 20 20 20 20 20 70 54 6f 6b              pTok
41e7a 65 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e  en->z, pToken->n
41e7b 2c 20 70 54 6f 6b 65 6e 2d 3e 69 73 50 72 65 66  , pToken->isPref
41e7c 69 78 2c 20 26 70 54 6f 6b 65 6e 2d 3e 70 53 65  ix, &pToken->pSe
41e7d 67 63 73 72 0a 20 20 20 20 20 20 20 20 29 3b 0a  gcsr.        );.
41e7e 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
41e7f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
41e80 20 20 20 20 20 20 20 2a 70 52 63 20 3d 20 72 63         *pRc = rc
41e81 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
41e82 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rn;.        }.  
41e83 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
41e84 72 74 28 20 70 45 78 70 72 2d 3e 70 50 68 72 61  rt( pExpr->pPhra
41e85 73 65 2d 3e 69 44 6f 63 6c 69 73 74 54 6f 6b 65  se->iDoclistToke
41e86 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 45  n==0 );.      pE
41e87 78 70 72 2d 3e 70 50 68 72 61 73 65 2d 3e 69 44  xpr->pPhrase->iD
41e88 6f 63 6c 69 73 74 54 6f 6b 65 6e 20 3d 20 2d 31  oclistToken = -1
41e89 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
41e8a 20 20 20 2a 70 6e 4f 72 20 2b 3d 20 28 70 45 78     *pnOr += (pEx
41e8b 70 72 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  pr->eType==FTSQU
41e8c 45 52 59 5f 4f 52 29 3b 0a 20 20 20 20 20 20 66  ERY_OR);.      f
41e8d 74 73 33 45 76 61 6c 41 6c 6c 6f 63 61 74 65 52  ts3EvalAllocateR
41e8e 65 61 64 65 72 73 28 70 43 73 72 2c 20 70 45 78  eaders(pCsr, pEx
41e8f 70 72 2d 3e 70 4c 65 66 74 2c 20 70 6e 54 6f 6b  pr->pLeft, pnTok
41e90 65 6e 2c 20 70 6e 4f 72 2c 20 70 52 63 29 3b 0a  en, pnOr, pRc);.
41e91 20 20 20 20 20 20 66 74 73 33 45 76 61 6c 41 6c        fts3EvalAl
41e92 6c 6f 63 61 74 65 52 65 61 64 65 72 73 28 70 43  locateReaders(pC
41e93 73 72 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  sr, pExpr->pRigh
41e94 74 2c 20 70 6e 54 6f 6b 65 6e 2c 20 70 6e 4f 72  t, pnToken, pnOr
41e95 2c 20 70 52 63 29 3b 0a 20 20 20 20 7d 0a 20 20  , pRc);.    }.  
41e96 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d  }.}../*.** Argum
41e97 65 6e 74 73 20 70 4c 69 73 74 2f 6e 4c 69 73 74  ents pList/nList
41e98 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 64 6f 63   contain the doc
41e99 6c 69 73 74 20 66 6f 72 20 74 6f 6b 65 6e 20 69  list for token i
41e9a 54 6f 6b 65 6e 20 6f 66 20 70 68 72 61 73 65 20  Token of phrase 
41e9b 70 2e 0a 2a 2a 20 49 74 20 69 73 20 6d 65 72 67  p..** It is merg
41e9c 65 64 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e  ed into the main
41e9d 20 64 6f 63 6c 69 73 74 20 73 74 6f 72 65 64 20   doclist stored 
41e9e 69 6e 20 70 2d 3e 64 6f 63 6c 69 73 74 2e 61 41  in p->doclist.aA
41e9f 6c 6c 2f 6e 41 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54  ll/nAll..**.** T
41ea0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73  his function ass
41ea1 75 6d 65 73 20 74 68 61 74 20 70 4c 69 73 74 20  umes that pList 
41ea2 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66  points to a buff
41ea3 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69  er allocated usi
41ea4 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61  ng.** sqlite3_ma
41ea5 6c 6c 6f 63 28 29 2e 20 54 68 69 73 20 66 75 6e  lloc(). This fun
41ea6 63 74 69 6f 6e 20 74 61 6b 65 73 20 72 65 73 70  ction takes resp
41ea7 6f 6e 73 69 62 69 6c 69 74 79 20 66 6f 72 20 65  onsibility for e
41ea8 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 66 72 65  ventually.** fre
41ea9 65 69 6e 67 20 74 68 65 20 62 75 66 66 65 72 2e  eing the buffer.
41eaa 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
41eab 66 74 73 33 45 76 61 6c 50 68 72 61 73 65 4d 65  fts3EvalPhraseMe
41eac 72 67 65 54 6f 6b 65 6e 28 0a 20 20 46 74 73 33  rgeToken(.  Fts3
41ead 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
41eae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
41eaf 54 53 20 54 61 62 6c 65 20 70 6f 69 6e 74 65 72  TS Table pointer
41eb0 20 2a 2f 0a 20 20 46 74 73 33 50 68 72 61 73 65   */.  Fts3Phrase
41eb1 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
41eb2 20 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65 20        /* Phrase 
41eb3 74 6f 20 6d 65 72 67 65 20 70 4c 69 73 74 2f 6e  to merge pList/n
41eb4 4c 69 73 74 20 69 6e 74 6f 20 2a 2f 0a 20 20 69  List into */.  i
41eb5 6e 74 20 69 54 6f 6b 65 6e 2c 20 20 20 20 20 20  nt iToken,      
41eb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
41eb7 2a 20 54 6f 6b 65 6e 20 70 4c 69 73 74 2f 6e 4c  * Token pList/nL
41eb8 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  ist corresponds 
41eb9 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 4c  to */.  char *pL
41eba 69 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ist,            
41ebb 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
41ebc 65 72 20 74 6f 20 64 6f 63 6c 69 73 74 20 2a 2f  er to doclist */
41ebd 0a 20 20 69 6e 74 20 6e 4c 69 73 74 20 20 20 20  .  int nList    
41ebe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41ebf 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
41ec0 62 79 74 65 73 20 69 6e 20 70 4c 69 73 74 20 2a  bytes in pList *
41ec1 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  /.){.  assert( i
41ec2 54 6f 6b 65 6e 21 3d 70 2d 3e 69 44 6f 63 6c 69  Token!=p->iDocli
41ec3 73 74 54 6f 6b 65 6e 20 29 3b 0a 0a 20 20 69 66  stToken );..  if
41ec4 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20  ( pList==0 ){.  
41ec5 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
41ec6 2d 3e 64 6f 63 6c 69 73 74 2e 61 41 6c 6c 29 3b  ->doclist.aAll);
41ec7 0a 20 20 20 20 70 2d 3e 64 6f 63 6c 69 73 74 2e  .    p->doclist.
41ec8 61 41 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70 2d  aAll = 0;.    p-
41ec9 3e 64 6f 63 6c 69 73 74 2e 6e 41 6c 6c 20 3d 20  >doclist.nAll = 
41eca 30 3b 0a 20 20 7d 0a 0a 20 20 65 6c 73 65 20 69  0;.  }..  else i
41ecb 66 28 20 70 2d 3e 69 44 6f 63 6c 69 73 74 54 6f  f( p->iDoclistTo
41ecc 6b 65 6e 3c 30 20 29 7b 0a 20 20 20 20 70 2d 3e  ken<0 ){.    p->
41ecd 64 6f 63 6c 69 73 74 2e 61 41 6c 6c 20 3d 20 70  doclist.aAll = p
41ece 4c 69 73 74 3b 0a 20 20 20 20 70 2d 3e 64 6f 63  List;.    p->doc
41ecf 6c 69 73 74 2e 6e 41 6c 6c 20 3d 20 6e 4c 69 73  list.nAll = nLis
41ed0 74 3b 0a 20 20 7d 0a 0a 20 20 65 6c 73 65 20 69  t;.  }..  else i
41ed1 66 28 20 70 2d 3e 64 6f 63 6c 69 73 74 2e 61 41  f( p->doclist.aA
41ed2 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ll==0 ){.    sql
41ed3 69 74 65 33 5f 66 72 65 65 28 70 4c 69 73 74 29  ite3_free(pList)
41ed4 3b 0a 20 20 7d 0a 0a 20 20 65 6c 73 65 20 7b 0a  ;.  }..  else {.
41ed5 20 20 20 20 63 68 61 72 20 2a 70 4c 65 66 74 3b      char *pLeft;
41ed6 0a 20 20 20 20 63 68 61 72 20 2a 70 52 69 67 68  .    char *pRigh
41ed7 74 3b 0a 20 20 20 20 69 6e 74 20 6e 4c 65 66 74  t;.    int nLeft
41ed8 3b 0a 20 20 20 20 69 6e 74 20 6e 52 69 67 68 74  ;.    int nRight
41ed9 3b 0a 20 20 20 20 69 6e 74 20 6e 44 69 66 66 3b  ;.    int nDiff;
41eda 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 44 6f  ..    if( p->iDo
41edb 63 6c 69 73 74 54 6f 6b 65 6e 3c 69 54 6f 6b 65  clistToken<iToke
41edc 6e 20 29 7b 0a 20 20 20 20 20 20 70 4c 65 66 74  n ){.      pLeft
41edd 20 3d 20 70 2d 3e 64 6f 63 6c 69 73 74 2e 61 41   = p->doclist.aA
41ede 6c 6c 3b 0a 20 20 20 20 20 20 6e 4c 65 66 74 20  ll;.      nLeft 
41edf 3d 20 70 2d 3e 64 6f 63 6c 69 73 74 2e 6e 41 6c  = p->doclist.nAl
41ee0 6c 3b 0a 20 20 20 20 20 20 70 52 69 67 68 74 20  l;.      pRight 
41ee1 3d 20 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 6e  = pList;.      n
41ee2 52 69 67 68 74 20 3d 20 6e 4c 69 73 74 3b 0a 20  Right = nList;. 
41ee3 20 20 20 20 20 6e 44 69 66 66 20 3d 20 69 54 6f       nDiff = iTo
41ee4 6b 65 6e 20 2d 20 70 2d 3e 69 44 6f 63 6c 69 73  ken - p->iDoclis
41ee5 74 54 6f 6b 65 6e 3b 0a 20 20 20 20 7d 65 6c 73  tToken;.    }els
41ee6 65 7b 0a 20 20 20 20 20 20 70 52 69 67 68 74 20  e{.      pRight 
41ee7 3d 20 70 2d 3e 64 6f 63 6c 69 73 74 2e 61 41 6c  = p->doclist.aAl
41ee8 6c 3b 0a 20 20 20 20 20 20 6e 52 69 67 68 74 20  l;.      nRight 
41ee9 3d 20 70 2d 3e 64 6f 63 6c 69 73 74 2e 6e 41 6c  = p->doclist.nAl
41eea 6c 3b 0a 20 20 20 20 20 20 70 4c 65 66 74 20 3d  l;.      pLeft =
41eeb 20 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 6e 4c   pList;.      nL
41eec 65 66 74 20 3d 20 6e 4c 69 73 74 3b 0a 20 20 20  eft = nList;.   
41eed 20 20 20 6e 44 69 66 66 20 3d 20 70 2d 3e 69 44     nDiff = p->iD
41eee 6f 63 6c 69 73 74 54 6f 6b 65 6e 20 2d 20 69 54  oclistToken - iT
41eef 6f 6b 65 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  oken;.    }..   
41ef0 20 66 74 73 33 44 6f 63 6c 69 73 74 50 68 72 61   fts3DoclistPhra
41ef1 73 65 4d 65 72 67 65 28 70 54 61 62 2d 3e 62 44  seMerge(pTab->bD
41ef2 65 73 63 49 64 78 2c 20 6e 44 69 66 66 2c 20 70  escIdx, nDiff, p
41ef3 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20 70 52 69  Left, nLeft, pRi
41ef4 67 68 74 2c 26 6e 52 69 67 68 74 29 3b 0a 20 20  ght,&nRight);.  
41ef5 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
41ef6 4c 65 66 74 29 3b 0a 20 20 20 20 70 2d 3e 64 6f  Left);.    p->do
41ef7 63 6c 69 73 74 2e 61 41 6c 6c 20 3d 20 70 52 69  clist.aAll = pRi
41ef8 67 68 74 3b 0a 20 20 20 20 70 2d 3e 64 6f 63 6c  ght;.    p->docl
41ef9 69 73 74 2e 6e 41 6c 6c 20 3d 20 6e 52 69 67 68  ist.nAll = nRigh
41efa 74 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 54  t;.  }..  if( iT
41efb 6f 6b 65 6e 3e 70 2d 3e 69 44 6f 63 6c 69 73 74  oken>p->iDoclist
41efc 54 6f 6b 65 6e 20 29 20 70 2d 3e 69 44 6f 63 6c  Token ) p->iDocl
41efd 69 73 74 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b 65  istToken = iToke
41efe 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64  n;.}../*.** Load
41eff 20 74 68 65 20 64 6f 63 6c 69 73 74 20 66 6f 72   the doclist for
41f00 20 70 68 72 61 73 65 20 70 20 69 6e 74 6f 20 70   phrase p into p
41f01 2d 3e 64 6f 63 6c 69 73 74 2e 61 41 6c 6c 2f 6e  ->doclist.aAll/n
41f02 41 6c 6c 2e 20 54 68 65 20 6c 6f 61 64 65 64 20  All. The loaded 
41f03 64 6f 63 6c 69 73 74 0a 2a 2a 20 64 6f 65 73 20  doclist.** does 
41f04 6e 6f 74 20 74 61 6b 65 20 64 65 66 65 72 72 65  not take deferre
41f05 64 20 74 6f 6b 65 6e 73 20 69 6e 74 6f 20 61 63  d tokens into ac
41f06 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  count..**.** SQL
41f07 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
41f08 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f  ed if no error o
41f09 63 63 75 72 73 2c 20 6f 74 68 65 72 77 69 73 65  ccurs, otherwise
41f0a 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
41f0b 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   code..*/.static
41f0c 20 69 6e 74 20 66 74 73 33 45 76 61 6c 50 68 72   int fts3EvalPhr
41f0d 61 73 65 4c 6f 61 64 28 0a 20 20 46 74 73 33 43  aseLoad(.  Fts3C
41f0e 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20  ursor *pCsr,    
41f0f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54             /* FT
41f10 53 20 43 75 72 73 6f 72 20 68 61 6e 64 6c 65 20  S Cursor handle 
41f11 2a 2f 0a 20 20 46 74 73 33 50 68 72 61 73 65 20  */.  Fts3Phrase 
41f12 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *p              
41f13 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65 20 6f       /* Phrase o
41f14 62 6a 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 46 74  bject */.){.  Ft
41f15 73 33 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20  s3Table *pTab = 
41f16 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43 73  (Fts3Table *)pCs
41f17 72 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b 0a 20  r->base.pVtab;. 
41f18 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 0a 20 20 69   int iToken;.  i
41f19 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
41f1a 4b 3b 0a 0a 20 20 66 6f 72 28 69 54 6f 6b 65 6e  K;..  for(iToken
41f1b 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
41f1c 4b 20 26 26 20 69 54 6f 6b 65 6e 3c 70 2d 3e 6e  K && iToken<p->n
41f1d 54 6f 6b 65 6e 3b 20 69 54 6f 6b 65 6e 2b 2b 29  Token; iToken++)
41f1e 7b 0a 20 20 20 20 46 74 73 33 50 68 72 61 73 65  {.    Fts3Phrase
41f1f 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20  Token *pToken = 
41f20 26 70 2d 3e 61 54 6f 6b 65 6e 5b 69 54 6f 6b 65  &p->aToken[iToke
41f21 6e 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  n];.    assert( 
41f22 70 54 6f 6b 65 6e 2d 3e 70 44 65 66 65 72 72 65  pToken->pDeferre
41f23 64 3d 3d 30 20 7c 7c 20 70 54 6f 6b 65 6e 2d 3e  d==0 || pToken->
41f24 70 53 65 67 63 73 72 3d 3d 30 20 29 3b 0a 0a 20  pSegcsr==0 );.. 
41f25 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 70     if( pToken->p
41f26 53 65 67 63 73 72 20 29 7b 0a 20 20 20 20 20 20  Segcsr ){.      
41f27 69 6e 74 20 6e 54 68 69 73 20 3d 20 30 3b 0a 20  int nThis = 0;. 
41f28 20 20 20 20 20 63 68 61 72 20 2a 70 54 68 69 73       char *pThis
41f29 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d   = 0;.      rc =
41f2a 20 66 74 73 33 54 65 72 6d 53 65 6c 65 63 74 28   fts3TermSelect(
41f2b 70 54 61 62 2c 20 70 54 6f 6b 65 6e 2c 20 70 2d  pTab, pToken, p-
41f2c 3e 69 43 6f 6c 75 6d 6e 2c 20 26 6e 54 68 69 73  >iColumn, &nThis
41f2d 2c 20 26 70 54 68 69 73 29 3b 0a 20 20 20 20 20  , &pThis);.     
41f2e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
41f2f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 66 74  OK ){.        ft
41f30 73 33 45 76 61 6c 50 68 72 61 73 65 4d 65 72 67  s3EvalPhraseMerg
41f31 65 54 6f 6b 65 6e 28 70 54 61 62 2c 20 70 2c 20  eToken(pTab, p, 
41f32 69 54 6f 6b 65 6e 2c 20 70 54 68 69 73 2c 20 6e  iToken, pThis, n
41f33 54 68 69 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20  This);.      }. 
41f34 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
41f35 20 70 54 6f 6b 65 6e 2d 3e 70 53 65 67 63 73 72   pToken->pSegcsr
41f36 3d 3d 30 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  ==0 );.  }..  re
41f37 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
41f38 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
41f39 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 65 61 63  is called on eac
41f3a 68 20 70 68 72 61 73 65 20 61 66 74 65 72 20 74  h phrase after t
41f3b 68 65 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74  he position list
41f3c 73 20 66 6f 72 0a 2a 2a 20 61 6e 79 20 64 65 66  s for.** any def
41f3d 65 72 72 65 64 20 74 6f 6b 65 6e 73 20 68 61 76  erred tokens hav
41f3e 65 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 69 6e  e been loaded in
41f3f 74 6f 20 6d 65 6d 6f 72 79 2e 20 49 74 20 75 70  to memory. It up
41f40 64 61 74 65 73 20 74 68 65 20 70 68 72 61 73 65  dates the phrase
41f41 73 0a 2a 2a 20 63 75 72 72 65 6e 74 20 70 6f 73  s.** current pos
41f42 69 74 69 6f 6e 20 6c 69 73 74 20 74 6f 20 69 6e  ition list to in
41f43 63 6c 75 64 65 20 6f 6e 6c 79 20 74 68 6f 73 65  clude only those
41f44 20 70 6f 73 69 74 69 6f 6e 73 20 74 68 61 74 20   positions that 
41f45 61 72 65 20 72 65 61 6c 6c 79 0a 2a 2a 20 69 6e  are really.** in
41f46 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20 70  stances of the p
41f47 68 72 61 73 65 20 28 61 66 74 65 72 20 63 6f 6e  hrase (after con
41f48 73 69 64 65 72 69 6e 67 20 64 65 66 65 72 72 65  sidering deferre
41f49 64 20 74 6f 6b 65 6e 73 29 2e 20 49 66 20 74 68  d tokens). If th
41f4a 69 73 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74  is.** means that
41f4b 20 74 68 65 20 70 68 72 61 73 65 20 64 6f 65 73   the phrase does
41f4c 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20 74   not appear in t
41f4d 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20  he current row, 
41f4e 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74 0a 2a 2a  doclist.pList.**
41f4f 20 61 6e 64 20 64 6f 63 6c 69 73 74 2e 6e 4c 69   and doclist.nLi
41f50 73 74 20 61 72 65 20 62 6f 74 68 20 7a 65 72 6f  st are both zero
41f51 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  ed..**.** SQLITE
41f52 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20  _OK is returned 
41f53 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75  if no error occu
41f54 72 73 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e  rs, otherwise an
41f55 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f   SQLite error co
41f56 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  de..*/.static in
41f57 74 20 66 74 73 33 45 76 61 6c 44 65 66 65 72 72  t fts3EvalDeferr
41f58 65 64 50 68 72 61 73 65 28 46 74 73 33 43 75 72  edPhrase(Fts3Cur
41f59 73 6f 72 20 2a 70 43 73 72 2c 20 46 74 73 33 50  sor *pCsr, Fts3P
41f5a 68 72 61 73 65 20 2a 70 50 68 72 61 73 65 29 7b  hrase *pPhrase){
41f5b 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 20 20  .  int iToken;  
41f5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41f5d 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74     /* Used to it
41f5e 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 70 68  erate through ph
41f5f 72 61 73 65 20 74 6f 6b 65 6e 73 20 2a 2f 0a 20  rase tokens */. 
41f60 20 63 68 61 72 20 2a 61 50 6f 73 6c 69 73 74 20   char *aPoslist 
41f61 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
41f62 20 2f 2a 20 50 6f 73 69 74 69 6f 6e 20 6c 69 73   /* Position lis
41f63 74 20 66 6f 72 20 64 65 66 65 72 72 65 64 20 74  t for deferred t
41f64 6f 6b 65 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  okens */.  int n
41f65 50 6f 73 6c 69 73 74 20 3d 20 30 3b 20 20 20 20  Poslist = 0;    
41f66 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
41f67 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
41f68 20 61 50 6f 73 6c 69 73 74 20 2a 2f 0a 20 20 69   aPoslist */.  i
41f69 6e 74 20 69 50 72 65 76 20 3d 20 2d 31 3b 20 20  nt iPrev = -1;  
41f6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
41f6b 2a 20 54 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 6f  * Token number o
41f6c 66 20 70 72 65 76 69 6f 75 73 20 64 65 66 65 72  f previous defer
41f6d 72 65 64 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 20 20  red token */..  
41f6e 61 73 73 65 72 74 28 20 70 50 68 72 61 73 65 2d  assert( pPhrase-
41f6f 3e 64 6f 63 6c 69 73 74 2e 62 46 72 65 65 4c 69  >doclist.bFreeLi
41f70 73 74 3d 3d 30 20 29 3b 0a 0a 20 20 66 6f 72 28  st==0 );..  for(
41f71 69 54 6f 6b 65 6e 3d 30 3b 20 69 54 6f 6b 65 6e  iToken=0; iToken
41f72 3c 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  <pPhrase->nToken
41f73 3b 20 69 54 6f 6b 65 6e 2b 2b 29 7b 0a 20 20 20  ; iToken++){.   
41f74 20 46 74 73 33 50 68 72 61 73 65 54 6f 6b 65 6e   Fts3PhraseToken
41f75 20 2a 70 54 6f 6b 65 6e 20 3d 20 26 70 50 68 72   *pToken = &pPhr
41f76 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 54 6f 6b  ase->aToken[iTok
41f77 65 6e 5d 3b 0a 20 20 20 20 46 74 73 33 44 65 66  en];.    Fts3Def
41f78 65 72 72 65 64 54 6f 6b 65 6e 20 2a 70 44 65 66  erredToken *pDef
41f79 65 72 72 65 64 20 3d 20 70 54 6f 6b 65 6e 2d 3e  erred = pToken->
41f7a 70 44 65 66 65 72 72 65 64 3b 0a 0a 20 20 20 20  pDeferred;..    
41f7b 69 66 28 20 70 44 65 66 65 72 72 65 64 20 29 7b  if( pDeferred ){
41f7c 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 4c 69  .      char *pLi
41f7d 73 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4c  st;.      int nL
41f7e 69 73 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 72  ist;.      int r
41f7f 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 44  c = sqlite3Fts3D
41f80 65 66 65 72 72 65 64 54 6f 6b 65 6e 4c 69 73 74  eferredTokenList
41f81 28 70 44 65 66 65 72 72 65 64 2c 20 26 70 4c 69  (pDeferred, &pLi
41f82 73 74 2c 20 26 6e 4c 69 73 74 29 3b 0a 20 20 20  st, &nList);.   
41f83 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
41f84 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
41f85 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69  ;..      if( pLi
41f86 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  st==0 ){.       
41f87 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 50   sqlite3_free(aP
41f88 6f 73 6c 69 73 74 29 3b 0a 20 20 20 20 20 20 20  oslist);.       
41f89 20 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73   pPhrase->doclis
41f8a 74 2e 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20  t.pList = 0;.   
41f8b 20 20 20 20 20 70 50 68 72 61 73 65 2d 3e 64 6f       pPhrase->do
41f8c 63 6c 69 73 74 2e 6e 4c 69 73 74 20 3d 20 30 3b  clist.nList = 0;
41f8d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
41f8e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 20 20  SQLITE_OK;..    
41f8f 20 20 7d 65 6c 73 65 20 69 66 28 20 61 50 6f 73    }else if( aPos
41f90 6c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  list==0 ){.     
41f91 20 20 20 61 50 6f 73 6c 69 73 74 20 3d 20 70 4c     aPoslist = pL
41f92 69 73 74 3b 0a 20 20 20 20 20 20 20 20 6e 50 6f  ist;.        nPo
41f93 73 6c 69 73 74 20 3d 20 6e 4c 69 73 74 3b 0a 0a  slist = nList;..
41f94 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
41f95 20 20 20 20 20 63 68 61 72 20 2a 61 4f 75 74 20       char *aOut 
41f96 3d 20 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20  = pList;.       
41f97 20 63 68 61 72 20 2a 70 31 20 3d 20 61 50 6f 73   char *p1 = aPos
41f98 6c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 63 68  list;.        ch
41f99 61 72 20 2a 70 32 20 3d 20 61 4f 75 74 3b 0a 0a  ar *p2 = aOut;..
41f9a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
41f9b 69 50 72 65 76 3e 3d 30 20 29 3b 0a 20 20 20 20  iPrev>=0 );.    
41f9c 20 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74 50      fts3PoslistP
41f9d 68 72 61 73 65 4d 65 72 67 65 28 26 61 4f 75 74  hraseMerge(&aOut
41f9e 2c 20 69 54 6f 6b 65 6e 2d 69 50 72 65 76 2c 20  , iToken-iPrev, 
41f9f 30 2c 20 31 2c 20 26 70 31 2c 20 26 70 32 29 3b  0, 1, &p1, &p2);
41fa0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
41fa1 5f 66 72 65 65 28 61 50 6f 73 6c 69 73 74 29 3b  _free(aPoslist);
41fa2 0a 20 20 20 20 20 20 20 20 61 50 6f 73 6c 69 73  .        aPoslis
41fa3 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 20 20 20  t = pList;.     
41fa4 20 20 20 6e 50 6f 73 6c 69 73 74 20 3d 20 28 69     nPoslist = (i
41fa5 6e 74 29 28 61 4f 75 74 20 2d 20 61 50 6f 73 6c  nt)(aOut - aPosl
41fa6 69 73 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ist);.        if
41fa7 28 20 6e 50 6f 73 6c 69 73 74 3d 3d 30 20 29 7b  ( nPoslist==0 ){
41fa8 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
41fa9 65 33 5f 66 72 65 65 28 61 50 6f 73 6c 69 73 74  e3_free(aPoslist
41faa 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 68  );.          pPh
41fab 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c  rase->doclist.pL
41fac 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ist = 0;.       
41fad 20 20 20 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c     pPhrase->docl
41fae 69 73 74 2e 6e 4c 69 73 74 20 3d 20 30 3b 0a 20  ist.nList = 0;. 
41faf 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
41fb0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
41fb1 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
41fb2 20 20 20 69 50 72 65 76 20 3d 20 69 54 6f 6b 65     iPrev = iToke
41fb3 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  n;.    }.  }..  
41fb4 69 66 28 20 69 50 72 65 76 3e 3d 30 20 29 7b 0a  if( iPrev>=0 ){.
41fb5 20 20 20 20 69 6e 74 20 6e 4d 61 78 55 6e 64 65      int nMaxUnde
41fb6 66 65 72 72 65 64 20 3d 20 70 50 68 72 61 73 65  ferred = pPhrase
41fb7 2d 3e 69 44 6f 63 6c 69 73 74 54 6f 6b 65 6e 3b  ->iDoclistToken;
41fb8 0a 20 20 20 20 69 66 28 20 6e 4d 61 78 55 6e 64  .    if( nMaxUnd
41fb9 65 66 65 72 72 65 64 3c 30 20 29 7b 0a 20 20 20  eferred<0 ){.   
41fba 20 20 20 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c     pPhrase->docl
41fbb 69 73 74 2e 70 4c 69 73 74 20 3d 20 61 50 6f 73  ist.pList = aPos
41fbc 6c 69 73 74 3b 0a 20 20 20 20 20 20 70 50 68 72  list;.      pPhr
41fbd 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 6e 4c 69  ase->doclist.nLi
41fbe 73 74 20 3d 20 6e 50 6f 73 6c 69 73 74 3b 0a 20  st = nPoslist;. 
41fbf 20 20 20 20 20 70 50 68 72 61 73 65 2d 3e 64 6f       pPhrase->do
41fc0 63 6c 69 73 74 2e 69 44 6f 63 69 64 20 3d 20 70  clist.iDocid = p
41fc1 43 73 72 2d 3e 69 50 72 65 76 49 64 3b 0a 20 20  Csr->iPrevId;.  
41fc2 20 20 20 20 70 50 68 72 61 73 65 2d 3e 64 6f 63      pPhrase->doc
41fc3 6c 69 73 74 2e 62 46 72 65 65 4c 69 73 74 20 3d  list.bFreeList =
41fc4 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   1;.    }else{. 
41fc5 20 20 20 20 20 69 6e 74 20 6e 44 69 73 74 61 6e       int nDistan
41fc6 63 65 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  ce;.      char *
41fc7 70 31 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  p1;.      char *
41fc8 70 32 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  p2;.      char *
41fc9 61 4f 75 74 3b 0a 0a 20 20 20 20 20 20 69 66 28  aOut;..      if(
41fca 20 6e 4d 61 78 55 6e 64 65 66 65 72 72 65 64 3e   nMaxUndeferred>
41fcb 69 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 20  iPrev ){.       
41fcc 20 70 31 20 3d 20 61 50 6f 73 6c 69 73 74 3b 0a   p1 = aPoslist;.
41fcd 20 20 20 20 20 20 20 20 70 32 20 3d 20 70 50 68          p2 = pPh
41fce 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c  rase->doclist.pL
41fcf 69 73 74 3b 0a 20 20 20 20 20 20 20 20 6e 44 69  ist;.        nDi
41fd0 73 74 61 6e 63 65 20 3d 20 6e 4d 61 78 55 6e 64  stance = nMaxUnd
41fd1 65 66 65 72 72 65 64 20 2d 20 69 50 72 65 76 3b  eferred - iPrev;
41fd2 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
41fd3 20 20 20 20 20 20 70 31 20 3d 20 70 50 68 72 61        p1 = pPhra
41fd4 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c 69 73  se->doclist.pLis
41fd5 74 3b 0a 20 20 20 20 20 20 20 20 70 32 20 3d 20  t;.        p2 = 
41fd6 61 50 6f 73 6c 69 73 74 3b 0a 20 20 20 20 20 20  aPoslist;.      
41fd7 20 20 6e 44 69 73 74 61 6e 63 65 20 3d 20 69 50    nDistance = iP
41fd8 72 65 76 20 2d 20 6e 4d 61 78 55 6e 64 65 66 65  rev - nMaxUndefe
41fd9 72 72 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  rred;.      }.. 
41fda 20 20 20 20 20 61 4f 75 74 20 3d 20 28 63 68 61       aOut = (cha
41fdb 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  r *)sqlite3_mall
41fdc 6f 63 28 6e 50 6f 73 6c 69 73 74 2b 38 29 3b 0a  oc(nPoslist+8);.
41fdd 20 20 20 20 20 20 69 66 28 20 21 61 4f 75 74 20        if( !aOut 
41fde 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
41fdf 65 33 5f 66 72 65 65 28 61 50 6f 73 6c 69 73 74  e3_free(aPoslist
41fe0 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
41fe1 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
41fe2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 0a 20        }.      . 
41fe3 20 20 20 20 20 70 50 68 72 61 73 65 2d 3e 64 6f       pPhrase->do
41fe4 63 6c 69 73 74 2e 70 4c 69 73 74 20 3d 20 61 4f  clist.pList = aO
41fe5 75 74 3b 0a 20 20 20 20 20 20 69 66 28 20 66 74  ut;.      if( ft
41fe6 73 33 50 6f 73 6c 69 73 74 50 68 72 61 73 65 4d  s3PoslistPhraseM
41fe7 65 72 67 65 28 26 61 4f 75 74 2c 20 6e 44 69 73  erge(&aOut, nDis
41fe8 74 61 6e 63 65 2c 20 30 2c 20 31 2c 20 26 70 31  tance, 0, 1, &p1
41fe9 2c 20 26 70 32 29 20 29 7b 0a 20 20 20 20 20 20  , &p2) ){.      
41fea 20 20 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69    pPhrase->docli
41feb 73 74 2e 62 46 72 65 65 4c 69 73 74 20 3d 20 31  st.bFreeList = 1
41fec 3b 0a 20 20 20 20 20 20 20 20 70 50 68 72 61 73  ;.        pPhras
41fed 65 2d 3e 64 6f 63 6c 69 73 74 2e 6e 4c 69 73 74  e->doclist.nList
41fee 20 3d 20 28 69 6e 74 29 28 61 4f 75 74 20 2d 20   = (int)(aOut - 
41fef 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74  pPhrase->doclist
41ff0 2e 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 7d  .pList);.      }
41ff1 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
41ff2 6c 69 74 65 33 5f 66 72 65 65 28 61 4f 75 74 29  lite3_free(aOut)
41ff3 3b 0a 20 20 20 20 20 20 20 20 70 50 68 72 61 73  ;.        pPhras
41ff4 65 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74  e->doclist.pList
41ff5 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 50   = 0;.        pP
41ff6 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 6e  hrase->doclist.n
41ff7 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  List = 0;.      
41ff8 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
41ff9 66 72 65 65 28 61 50 6f 73 6c 69 73 74 29 3b 0a  free(aPoslist);.
41ffa 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
41ffb 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
41ffc 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
41ffd 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
41ffe 66 6f 72 20 65 61 63 68 20 46 74 73 33 50 68 72  for each Fts3Phr
41fff 61 73 65 20 69 6e 20 61 20 66 75 6c 6c 2d 74 65  ase in a full-te
42000 78 74 20 71 75 65 72 79 20 0a 2a 2a 20 65 78 70  xt query .** exp
42001 72 65 73 73 69 6f 6e 20 74 6f 20 69 6e 69 74 69  ression to initi
42002 61 6c 69 7a 65 20 74 68 65 20 6d 65 63 68 61 6e  alize the mechan
42003 69 73 6d 20 66 6f 72 20 72 65 74 75 72 6e 69 6e  ism for returnin
42004 67 20 72 6f 77 73 2e 20 4f 6e 63 65 20 74 68 69  g rows. Once thi
42005 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61  s.** function ha
42006 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 73 75  s been called su
42007 63 63 65 73 73 66 75 6c 6c 79 20 6f 6e 20 61 6e  ccessfully on an
42008 20 46 74 73 33 50 68 72 61 73 65 2c 20 69 74 20   Fts3Phrase, it 
42009 6d 61 79 20 62 65 0a 2a 2a 20 75 73 65 64 20 77  may be.** used w
4200a 69 74 68 20 66 74 73 33 45 76 61 6c 50 68 72 61  ith fts3EvalPhra
4200b 73 65 4e 65 78 74 28 29 20 74 6f 20 69 74 65 72  seNext() to iter
4200c 61 74 65 20 74 68 72 6f 75 67 68 20 74 68 65 20  ate through the 
4200d 6d 61 74 63 68 69 6e 67 20 64 6f 63 69 64 73 2e  matching docids.
4200e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65  .**.** If parame
4200f 74 65 72 20 62 4f 70 74 4f 6b 20 69 73 20 74 72  ter bOptOk is tr
42010 75 65 2c 20 74 68 65 6e 20 74 68 65 20 70 68 72  ue, then the phr
42011 61 73 65 20 6d 61 79 20 28 6f 72 20 6d 61 79 20  ase may (or may 
42012 6e 6f 74 29 20 75 73 65 20 74 68 65 0a 2a 2a 20  not) use the.** 
42013 69 6e 63 72 65 6d 65 6e 74 61 6c 20 6c 6f 61 64  incremental load
42014 69 6e 67 20 73 74 72 61 74 65 67 79 2e 20 4f 74  ing strategy. Ot
42015 68 65 72 77 69 73 65 2c 20 74 68 65 20 65 6e 74  herwise, the ent
42016 69 72 65 20 64 6f 63 6c 69 73 74 20 69 73 20 6c  ire doclist is l
42017 6f 61 64 65 64 20 69 6e 74 6f 0a 2a 2a 20 6d 65  oaded into.** me
42018 6d 6f 72 79 20 77 69 74 68 69 6e 20 74 68 69 73  mory within this
42019 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c   call..**.** SQL
4201a 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
4201b 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f  ed if no error o
4201c 63 63 75 72 73 2c 20 6f 74 68 65 72 77 69 73 65  ccurs, otherwise
4201d 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
4201e 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   code..*/.static
4201f 20 69 6e 74 20 66 74 73 33 45 76 61 6c 50 68 72   int fts3EvalPhr
42020 61 73 65 53 74 61 72 74 28 46 74 73 33 43 75 72  aseStart(Fts3Cur
42021 73 6f 72 20 2a 70 43 73 72 2c 20 69 6e 74 20 62  sor *pCsr, int b
42022 4f 70 74 4f 6b 2c 20 46 74 73 33 50 68 72 61 73  OptOk, Fts3Phras
42023 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  e *p){.  int rc;
42024 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42025 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f           /* Erro
42026 72 20 63 6f 64 65 20 2a 2f 0a 20 20 46 74 73 33  r code */.  Fts3
42027 50 68 72 61 73 65 54 6f 6b 65 6e 20 2a 70 46 69  PhraseToken *pFi
42028 72 73 74 20 3d 20 26 70 2d 3e 61 54 6f 6b 65 6e  rst = &p->aToken
42029 5b 30 5d 3b 0a 20 20 46 74 73 33 54 61 62 6c 65  [0];.  Fts3Table
4202a 20 2a 70 54 61 62 20 3d 20 28 46 74 73 33 54 61   *pTab = (Fts3Ta
4202b 62 6c 65 20 2a 29 70 43 73 72 2d 3e 62 61 73 65  ble *)pCsr->base
4202c 2e 70 56 74 61 62 3b 0a 0a 20 20 69 66 28 20 70  .pVtab;..  if( p
4202d 43 73 72 2d 3e 62 44 65 73 63 3d 3d 70 54 61 62  Csr->bDesc==pTab
4202e 2d 3e 62 44 65 73 63 49 64 78 20 0a 20 20 20 26  ->bDescIdx .   &
4202f 26 20 62 4f 70 74 4f 6b 3d 3d 31 20 0a 20 20 20  & bOptOk==1 .   
42030 26 26 20 70 2d 3e 6e 54 6f 6b 65 6e 3d 3d 31 20  && p->nToken==1 
42031 0a 20 20 20 26 26 20 70 46 69 72 73 74 2d 3e 70  .   && pFirst->p
42032 53 65 67 63 73 72 20 0a 20 20 20 26 26 20 70 46  Segcsr .   && pF
42033 69 72 73 74 2d 3e 70 53 65 67 63 73 72 2d 3e 62  irst->pSegcsr->b
42034 4c 6f 6f 6b 75 70 20 0a 20 20 20 26 26 20 70 46  Lookup .   && pF
42035 69 72 73 74 2d 3e 62 46 69 72 73 74 3d 3d 30 0a  irst->bFirst==0.
42036 20 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 65 20    ){.    /* Use 
42037 74 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20  the incremental 
42038 61 70 70 72 6f 61 63 68 2e 20 2a 2f 0a 20 20 20  approach. */.   
42039 20 69 6e 74 20 69 43 6f 6c 20 3d 20 28 70 2d 3e   int iCol = (p->
4203a 69 43 6f 6c 75 6d 6e 20 3e 3d 20 70 54 61 62 2d  iColumn >= pTab-
4203b 3e 6e 43 6f 6c 75 6d 6e 20 3f 20 2d 31 20 3a 20  >nColumn ? -1 : 
4203c 70 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  p->iColumn);.   
4203d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
4203e 33 4d 73 72 49 6e 63 72 53 74 61 72 74 28 0a 20  3MsrIncrStart(. 
4203f 20 20 20 20 20 20 20 70 54 61 62 2c 20 70 46 69         pTab, pFi
42040 72 73 74 2d 3e 70 53 65 67 63 73 72 2c 20 69 43  rst->pSegcsr, iC
42041 6f 6c 2c 20 70 46 69 72 73 74 2d 3e 7a 2c 20 70  ol, pFirst->z, p
42042 46 69 72 73 74 2d 3e 6e 29 3b 0a 20 20 20 20 70  First->n);.    p
42043 2d 3e 62 49 6e 63 72 20 3d 20 31 3b 0a 0a 20 20  ->bIncr = 1;..  
42044 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4c 6f  }else{.    /* Lo
42045 61 64 20 74 68 65 20 66 75 6c 6c 20 64 6f 63 6c  ad the full docl
42046 69 73 74 20 66 6f 72 20 74 68 65 20 70 68 72 61  ist for the phra
42047 73 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e 20  se into memory. 
42048 2a 2f 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33  */.    rc = fts3
42049 45 76 61 6c 50 68 72 61 73 65 4c 6f 61 64 28 70  EvalPhraseLoad(p
4204a 43 73 72 2c 20 70 29 3b 0a 20 20 20 20 70 2d 3e  Csr, p);.    p->
4204b 62 49 6e 63 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a  bIncr = 0;.  }..
4204c 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
4204d 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 6e 54  LITE_OK || p->nT
4204e 6f 6b 65 6e 3c 31 20 7c 7c 20 70 2d 3e 61 54 6f  oken<1 || p->aTo
4204f 6b 65 6e 5b 30 5d 2e 70 53 65 67 63 73 72 3d 3d  ken[0].pSegcsr==
42050 30 20 7c 7c 20 70 2d 3e 62 49 6e 63 72 20 29 3b  0 || p->bIncr );
42051 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
42052 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
42053 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
42054 69 74 65 72 61 74 65 20 62 61 63 6b 77 61 72 64  iterate backward
42055 73 20 28 66 72 6f 6d 20 74 68 65 20 65 6e 64 20  s (from the end 
42056 74 6f 20 73 74 61 72 74 29 20 0a 2a 2a 20 74 68  to start) .** th
42057 72 6f 75 67 68 20 64 6f 63 6c 69 73 74 73 2e 20  rough doclists. 
42058 49 74 20 69 73 20 75 73 65 64 20 62 79 20 74 68  It is used by th
42059 69 73 20 6d 6f 64 75 6c 65 20 74 6f 20 69 74 65  is module to ite
4205a 72 61 74 65 20 74 68 72 6f 75 67 68 20 70 68 72  rate through phr
4205b 61 73 65 0a 2a 2a 20 64 6f 63 6c 69 73 74 73 20  ase.** doclists 
4205c 69 6e 20 72 65 76 65 72 73 65 20 61 6e 64 20 62  in reverse and b
4205d 79 20 74 68 65 20 66 74 73 33 5f 77 72 69 74 65  y the fts3_write
4205e 2e 63 20 6d 6f 64 75 6c 65 20 74 6f 20 69 74 65  .c module to ite
4205f 72 61 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20  rate through.** 
42060 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 6c 69  pending-terms li
42061 73 74 73 20 77 68 65 6e 20 77 72 69 74 69 6e 67  sts when writing
42062 20 74 6f 20 64 61 74 61 62 61 73 65 73 20 77 69   to databases wi
42063 74 68 20 22 6f 72 64 65 72 3d 64 65 73 63 22 2e  th "order=desc".
42064 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 6f 63 6c 69  .**.** The docli
42065 73 74 20 6d 61 79 20 62 65 20 73 6f 72 74 65 64  st may be sorted
42066 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 28 70   in ascending (p
42067 61 72 61 6d 65 74 65 72 20 62 44 65 73 63 49 64  arameter bDescId
42068 78 3d 3d 30 29 20 6f 72 20 0a 2a 2a 20 64 65 73  x==0) or .** des
42069 63 65 6e 64 69 6e 67 20 28 70 61 72 61 6d 65 74  cending (paramet
4206a 65 72 20 62 44 65 73 63 49 64 78 3d 3d 31 29 20  er bDescIdx==1) 
4206b 6f 72 64 65 72 20 6f 66 20 64 6f 63 69 64 2e 20  order of docid. 
4206c 52 65 67 61 72 64 6c 65 73 73 2c 20 74 68 69 73  Regardless, this
4206d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 74 65  .** function ite
4206e 72 61 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65  rates from the e
4206f 6e 64 20 6f 66 20 74 68 65 20 64 6f 63 6c 69 73  nd of the doclis
42070 74 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69  t to the beginni
42071 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
42072 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
42073 65 33 46 74 73 33 44 6f 63 6c 69 73 74 50 72 65  e3Fts3DoclistPre
42074 76 28 0a 20 20 69 6e 74 20 62 44 65 73 63 49 64  v(.  int bDescId
42075 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
42076 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
42077 74 68 65 20 64 6f 63 6c 69 73 74 20 69 73 20 64  the doclist is d
42078 65 73 63 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61  esc */.  char *a
42079 44 6f 63 6c 69 73 74 2c 20 20 20 20 20 20 20 20  Doclist,        
4207a 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
4207b 74 65 72 20 74 6f 20 65 6e 74 69 72 65 20 64 6f  ter to entire do
4207c 63 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  clist */.  int n
4207d 44 6f 63 6c 69 73 74 2c 20 20 20 20 20 20 20 20  Doclist,        
4207e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
4207f 6e 67 74 68 20 6f 66 20 61 44 6f 63 6c 69 73 74  ngth of aDoclist
42080 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63   in bytes */.  c
42081 68 61 72 20 2a 2a 70 70 49 74 65 72 2c 20 20 20  har **ppIter,   
42082 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
42083 2a 20 49 4e 2f 4f 55 54 3a 20 49 74 65 72 61 74  * IN/OUT: Iterat
42084 6f 72 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20  or pointer */.  
42085 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
42086 69 44 6f 63 69 64 2c 20 20 20 20 20 20 20 20 20  iDocid,         
42087 2f 2a 20 49 4e 2f 4f 55 54 3a 20 44 6f 63 69 64  /* IN/OUT: Docid
42088 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   pointer */.  in
42089 74 20 2a 70 6e 4c 69 73 74 2c 20 20 20 20 20 20  t *pnList,      
4208a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4208b 20 4f 55 54 3a 20 4c 69 73 74 20 6c 65 6e 67 74   OUT: List lengt
4208c 68 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75  h pointer */.  u
4208d 38 20 2a 70 62 45 6f 66 20 20 20 20 20 20 20 20  8 *pbEof        
4208e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4208f 2a 20 4f 55 54 3a 20 45 6e 64 2d 6f 66 2d 66 69  * OUT: End-of-fi
42090 6c 65 20 66 6c 61 67 20 2a 2f 0a 29 7b 0a 20 20  le flag */.){.  
42091 63 68 61 72 20 2a 70 20 3d 20 2a 70 70 49 74 65  char *p = *ppIte
42092 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44  r;..  assert( nD
42093 6f 63 6c 69 73 74 3e 30 20 29 3b 0a 20 20 61 73  oclist>0 );.  as
42094 73 65 72 74 28 20 2a 70 62 45 6f 66 3d 3d 30 20  sert( *pbEof==0 
42095 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 20 7c  );.  assert( p |
42096 7c 20 2a 70 69 44 6f 63 69 64 3d 3d 30 20 29 3b  | *piDocid==0 );
42097 0a 20 20 61 73 73 65 72 74 28 20 21 70 20 7c 7c  .  assert( !p ||
42098 20 28 70 3e 61 44 6f 63 6c 69 73 74 20 26 26 20   (p>aDoclist && 
42099 70 3c 26 61 44 6f 63 6c 69 73 74 5b 6e 44 6f 63  p<&aDoclist[nDoc
4209a 6c 69 73 74 5d 29 20 29 3b 0a 0a 20 20 69 66 28  list]) );..  if(
4209b 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c   p==0 ){.    sql
4209c 69 74 65 33 5f 69 6e 74 36 34 20 69 44 6f 63 69  ite3_int64 iDoci
4209d 64 20 3d 20 30 3b 0a 20 20 20 20 63 68 61 72 20  d = 0;.    char 
4209e 2a 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20  *pNext = 0;.    
4209f 63 68 61 72 20 2a 70 44 6f 63 69 64 20 3d 20 61  char *pDocid = a
420a0 44 6f 63 6c 69 73 74 3b 0a 20 20 20 20 63 68 61  Doclist;.    cha
420a1 72 20 2a 70 45 6e 64 20 3d 20 26 61 44 6f 63 6c  r *pEnd = &aDocl
420a2 69 73 74 5b 6e 44 6f 63 6c 69 73 74 5d 3b 0a 20  ist[nDoclist];. 
420a3 20 20 20 69 6e 74 20 69 4d 75 6c 20 3d 20 31 3b     int iMul = 1;
420a4 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 70 44 6f  ..    while( pDo
420a5 63 69 64 3c 70 45 6e 64 20 29 7b 0a 20 20 20 20  cid<pEnd ){.    
420a6 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
420a7 69 44 65 6c 74 61 3b 0a 20 20 20 20 20 20 70 44  iDelta;.      pD
420a8 6f 63 69 64 20 2b 3d 20 73 71 6c 69 74 65 33 46  ocid += sqlite3F
420a9 74 73 33 47 65 74 56 61 72 69 6e 74 28 70 44 6f  ts3GetVarint(pDo
420aa 63 69 64 2c 20 26 69 44 65 6c 74 61 29 3b 0a 20  cid, &iDelta);. 
420ab 20 20 20 20 20 69 44 6f 63 69 64 20 2b 3d 20 28       iDocid += (
420ac 69 4d 75 6c 20 2a 20 69 44 65 6c 74 61 29 3b 0a  iMul * iDelta);.
420ad 20 20 20 20 20 20 70 4e 65 78 74 20 3d 20 70 44        pNext = pD
420ae 6f 63 69 64 3b 0a 20 20 20 20 20 20 66 74 73 33  ocid;.      fts3
420af 50 6f 73 6c 69 73 74 43 6f 70 79 28 30 2c 20 26  PoslistCopy(0, &
420b0 70 44 6f 63 69 64 29 3b 0a 20 20 20 20 20 20 77  pDocid);.      w
420b1 68 69 6c 65 28 20 70 44 6f 63 69 64 3c 70 45 6e  hile( pDocid<pEn
420b2 64 20 26 26 20 2a 70 44 6f 63 69 64 3d 3d 30 20  d && *pDocid==0 
420b3 29 20 70 44 6f 63 69 64 2b 2b 3b 0a 20 20 20 20  ) pDocid++;.    
420b4 20 20 69 4d 75 6c 20 3d 20 28 62 44 65 73 63 49    iMul = (bDescI
420b5 64 78 20 3f 20 2d 31 20 3a 20 31 29 3b 0a 20 20  dx ? -1 : 1);.  
420b6 20 20 7d 0a 0a 20 20 20 20 2a 70 6e 4c 69 73 74    }..    *pnList
420b7 20 3d 20 28 69 6e 74 29 28 70 45 6e 64 20 2d 20   = (int)(pEnd - 
420b8 70 4e 65 78 74 29 3b 0a 20 20 20 20 2a 70 70 49  pNext);.    *ppI
420b9 74 65 72 20 3d 20 70 4e 65 78 74 3b 0a 20 20 20  ter = pNext;.   
420ba 20 2a 70 69 44 6f 63 69 64 20 3d 20 69 44 6f 63   *piDocid = iDoc
420bb 69 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  id;.  }else{.   
420bc 20 69 6e 74 20 69 4d 75 6c 20 3d 20 28 62 44 65   int iMul = (bDe
420bd 73 63 49 64 78 20 3f 20 2d 31 20 3a 20 31 29 3b  scIdx ? -1 : 1);
420be 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  .    sqlite3_int
420bf 36 34 20 69 44 65 6c 74 61 3b 0a 20 20 20 20 66  64 iDelta;.    f
420c0 74 73 33 47 65 74 52 65 76 65 72 73 65 56 61 72  ts3GetReverseVar
420c1 69 6e 74 28 26 70 2c 20 61 44 6f 63 6c 69 73 74  int(&p, aDoclist
420c2 2c 20 26 69 44 65 6c 74 61 29 3b 0a 20 20 20 20  , &iDelta);.    
420c3 2a 70 69 44 6f 63 69 64 20 2d 3d 20 28 69 4d 75  *piDocid -= (iMu
420c4 6c 20 2a 20 69 44 65 6c 74 61 29 3b 0a 0a 20 20  l * iDelta);..  
420c5 20 20 69 66 28 20 70 3d 3d 61 44 6f 63 6c 69 73    if( p==aDoclis
420c6 74 20 29 7b 0a 20 20 20 20 20 20 2a 70 62 45 6f  t ){.      *pbEo
420c7 66 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  f = 1;.    }else
420c8 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 53  {.      char *pS
420c9 61 76 65 20 3d 20 70 3b 0a 20 20 20 20 20 20 66  ave = p;.      f
420ca 74 73 33 52 65 76 65 72 73 65 50 6f 73 6c 69 73  ts3ReversePoslis
420cb 74 28 61 44 6f 63 6c 69 73 74 2c 20 26 70 29 3b  t(aDoclist, &p);
420cc 0a 20 20 20 20 20 20 2a 70 6e 4c 69 73 74 20 3d  .      *pnList =
420cd 20 28 69 6e 74 29 28 70 53 61 76 65 20 2d 20 70   (int)(pSave - p
420ce 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70  );.    }.    *pp
420cf 49 74 65 72 20 3d 20 70 3b 0a 20 20 7d 0a 7d 0a  Iter = p;.  }.}.
420d0 0a 2f 2a 0a 2a 2a 20 49 74 65 72 61 74 65 20 66  ./*.** Iterate f
420d1 6f 72 77 61 72 64 73 20 74 68 72 6f 75 67 68 20  orwards through 
420d2 61 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 53 51  a doclist..*/.SQ
420d3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
420d4 64 20 73 71 6c 69 74 65 33 46 74 73 33 44 6f 63  d sqlite3Fts3Doc
420d5 6c 69 73 74 4e 65 78 74 28 0a 20 20 69 6e 74 20  listNext(.  int 
420d6 62 44 65 73 63 49 64 78 2c 20 20 20 20 20 20 20  bDescIdx,       
420d7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
420d8 72 75 65 20 69 66 20 74 68 65 20 64 6f 63 6c 69  rue if the docli
420d9 73 74 20 69 73 20 64 65 73 63 20 2a 2f 0a 20 20  st is desc */.  
420da 63 68 61 72 20 2a 61 44 6f 63 6c 69 73 74 2c 20  char *aDoclist, 
420db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
420dc 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 65 6e  /* Pointer to en
420dd 74 69 72 65 20 64 6f 63 6c 69 73 74 20 2a 2f 0a  tire doclist */.
420de 20 20 69 6e 74 20 6e 44 6f 63 6c 69 73 74 2c 20    int nDoclist, 
420df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
420e0 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61    /* Length of a
420e1 44 6f 63 6c 69 73 74 20 69 6e 20 62 79 74 65 73  Doclist in bytes
420e2 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 70 49   */.  char **ppI
420e3 74 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  ter,            
420e4 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a        /* IN/OUT:
420e5 20 49 74 65 72 61 74 6f 72 20 70 6f 69 6e 74 65   Iterator pointe
420e6 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  r */.  sqlite3_i
420e7 6e 74 36 34 20 2a 70 69 44 6f 63 69 64 2c 20 20  nt64 *piDocid,  
420e8 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54         /* IN/OUT
420e9 3a 20 44 6f 63 69 64 20 70 6f 69 6e 74 65 72 20  : Docid pointer 
420ea 2a 2f 0a 20 20 75 38 20 2a 70 62 45 6f 66 20 20  */.  u8 *pbEof  
420eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
420ec 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64       /* OUT: End
420ed 2d 6f 66 2d 66 69 6c 65 20 66 6c 61 67 20 2a 2f  -of-file flag */
420ee 0a 29 7b 0a 20 20 63 68 61 72 20 2a 70 20 3d 20  .){.  char *p = 
420ef 2a 70 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65  *ppIter;..  asse
420f0 72 74 28 20 6e 44 6f 63 6c 69 73 74 3e 30 20 29  rt( nDoclist>0 )
420f1 3b 0a 20 20 61 73 73 65 72 74 28 20 2a 70 62 45  ;.  assert( *pbE
420f2 6f 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  of==0 );.  asser
420f3 74 28 20 70 20 7c 7c 20 2a 70 69 44 6f 63 69 64  t( p || *piDocid
420f4 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
420f5 20 21 70 20 7c 7c 20 28 70 3e 3d 61 44 6f 63 6c   !p || (p>=aDocl
420f6 69 73 74 20 26 26 20 70 3c 3d 26 61 44 6f 63 6c  ist && p<=&aDocl
420f7 69 73 74 5b 6e 44 6f 63 6c 69 73 74 5d 29 20 29  ist[nDoclist]) )
420f8 3b 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b  ;..  if( p==0 ){
420f9 0a 20 20 20 20 70 20 3d 20 61 44 6f 63 6c 69 73  .    p = aDoclis
420fa 74 3b 0a 20 20 20 20 70 20 2b 3d 20 73 71 6c 69  t;.    p += sqli
420fb 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74  te3Fts3GetVarint
420fc 28 70 2c 20 70 69 44 6f 63 69 64 29 3b 0a 20 20  (p, piDocid);.  
420fd 7d 65 6c 73 65 7b 0a 20 20 20 20 66 74 73 33 50  }else{.    fts3P
420fe 6f 73 6c 69 73 74 43 6f 70 79 28 30 2c 20 26 70  oslistCopy(0, &p
420ff 29 3b 0a 20 20 20 20 69 66 28 20 70 3e 3d 26 61  );.    if( p>=&a
42100 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c 69 73 74  Doclist[nDoclist
42101 5d 20 29 7b 0a 20 20 20 20 20 20 2a 70 62 45 6f  ] ){.      *pbEo
42102 66 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  f = 1;.    }else
42103 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
42104 69 6e 74 36 34 20 69 56 61 72 3b 0a 20 20 20 20  int64 iVar;.    
42105 20 20 70 20 2b 3d 20 73 71 6c 69 74 65 33 46 74    p += sqlite3Ft
42106 73 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26  s3GetVarint(p, &
42107 69 56 61 72 29 3b 0a 20 20 20 20 20 20 2a 70 69  iVar);.      *pi
42108 44 6f 63 69 64 20 2b 3d 20 28 28 62 44 65 73 63  Docid += ((bDesc
42109 49 64 78 20 3f 20 2d 31 20 3a 20 31 29 20 2a 20  Idx ? -1 : 1) * 
4210a 69 56 61 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  iVar);.    }.  }
4210b 0a 0a 20 20 2a 70 70 49 74 65 72 20 3d 20 70 3b  ..  *ppIter = p;
4210c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70  .}../*.** Attemp
4210d 74 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 70 68  t to move the ph
4210e 72 61 73 65 20 69 74 65 72 61 74 6f 72 20 74 6f  rase iterator to
4210f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65   point to the ne
42110 78 74 20 6d 61 74 63 68 69 6e 67 20 64 6f 63 69  xt matching doci
42111 64 2e 20 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  d. .** If an err
42112 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72  or occurs, retur
42113 6e 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  n an SQLite erro
42114 72 20 63 6f 64 65 2e 20 4f 74 68 65 72 77 69 73  r code. Otherwis
42115 65 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51  e, return .** SQ
42116 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49  LITE_OK..**.** I
42117 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 22 6e  f there is no "n
42118 65 78 74 22 20 65 6e 74 72 79 20 61 6e 64 20 6e  ext" entry and n
42119 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  o error occurs, 
4211a 74 68 65 6e 20 2a 70 62 45 6f 66 20 69 73 20 73  then *pbEof is s
4211b 65 74 20 74 6f 0a 2a 2a 20 31 20 62 65 66 6f 72  et to.** 1 befor
4211c 65 20 72 65 74 75 72 6e 69 6e 67 2e 20 4f 74 68  e returning. Oth
4211d 65 72 77 69 73 65 2c 20 69 66 20 6e 6f 20 65 72  erwise, if no er
4211e 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 64 20 74  ror occurs and t
4211f 68 65 20 69 74 65 72 61 74 6f 72 20 69 73 0a 2a  he iterator is.*
42120 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61  * successfully a
42121 64 76 61 6e 63 65 64 2c 20 2a 70 62 45 6f 66 20  dvanced, *pbEof 
42122 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a  is set to 0..*/.
42123 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 45  static int fts3E
42124 76 61 6c 50 68 72 61 73 65 4e 65 78 74 28 0a 20  valPhraseNext(. 
42125 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73   Fts3Cursor *pCs
42126 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
42127 20 2f 2a 20 46 54 53 20 43 75 72 73 6f 72 20 68   /* FTS Cursor h
42128 61 6e 64 6c 65 20 2a 2f 0a 20 20 46 74 73 33 50  andle */.  Fts3P
42129 68 72 61 73 65 20 2a 70 2c 20 20 20 20 20 20 20  hrase *p,       
4212a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 68             /* Ph
4212b 72 61 73 65 20 6f 62 6a 65 63 74 20 74 6f 20 61  rase object to a
4212c 64 76 61 6e 63 65 20 74 6f 20 6e 65 78 74 20 64  dvance to next d
4212d 6f 63 69 64 20 2a 2f 0a 20 20 75 38 20 2a 70 62  ocid */.  u8 *pb
4212e 45 6f 66 20 20 20 20 20 20 20 20 20 20 20 20 20  Eof             
4212f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
42130 3a 20 53 65 74 20 74 6f 20 31 20 69 66 20 45 4f  : Set to 1 if EO
42131 46 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  F */.){.  int rc
42132 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
42133 46 74 73 33 44 6f 63 6c 69 73 74 20 2a 70 44 4c  Fts3Doclist *pDL
42134 20 3d 20 26 70 2d 3e 64 6f 63 6c 69 73 74 3b 0a   = &p->doclist;.
42135 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61    Fts3Table *pTa
42136 62 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a  b = (Fts3Table *
42137 29 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61  )pCsr->base.pVta
42138 62 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 62 49 6e  b;..  if( p->bIn
42139 63 72 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  cr ){.    assert
4213a 28 20 70 2d 3e 6e 54 6f 6b 65 6e 3d 3d 31 20 29  ( p->nToken==1 )
4213b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 44  ;.    assert( pD
4213c 4c 2d 3e 70 4e 65 78 74 44 6f 63 69 64 3d 3d 30  L->pNextDocid==0
4213d 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   );.    rc = sql
4213e 69 74 65 33 46 74 73 33 4d 73 72 49 6e 63 72 4e  ite3Fts3MsrIncrN
4213f 65 78 74 28 70 54 61 62 2c 20 70 2d 3e 61 54 6f  ext(pTab, p->aTo
42140 6b 65 6e 5b 30 5d 2e 70 53 65 67 63 73 72 2c 20  ken[0].pSegcsr, 
42141 0a 20 20 20 20 20 20 20 20 26 70 44 4c 2d 3e 69  .        &pDL->i
42142 44 6f 63 69 64 2c 20 26 70 44 4c 2d 3e 70 4c 69  Docid, &pDL->pLi
42143 73 74 2c 20 26 70 44 4c 2d 3e 6e 4c 69 73 74 0a  st, &pDL->nList.
42144 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 72      );.    if( r
42145 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
42146 21 70 44 4c 2d 3e 70 4c 69 73 74 20 29 7b 0a 20  !pDL->pList ){. 
42147 20 20 20 20 20 2a 70 62 45 6f 66 20 3d 20 31 3b       *pbEof = 1;
42148 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
42149 66 28 20 70 43 73 72 2d 3e 62 44 65 73 63 21 3d  f( pCsr->bDesc!=
4214a 70 54 61 62 2d 3e 62 44 65 73 63 49 64 78 20 26  pTab->bDescIdx &
4214b 26 20 70 44 4c 2d 3e 6e 41 6c 6c 20 29 7b 0a 20  & pDL->nAll ){. 
4214c 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 44 6f     sqlite3Fts3Do
4214d 63 6c 69 73 74 50 72 65 76 28 70 54 61 62 2d 3e  clistPrev(pTab->
4214e 62 44 65 73 63 49 64 78 2c 20 70 44 4c 2d 3e 61  bDescIdx, pDL->a
4214f 41 6c 6c 2c 20 70 44 4c 2d 3e 6e 41 6c 6c 2c 20  All, pDL->nAll, 
42150 0a 20 20 20 20 20 20 20 20 26 70 44 4c 2d 3e 70  .        &pDL->p
42151 4e 65 78 74 44 6f 63 69 64 2c 20 26 70 44 4c 2d  NextDocid, &pDL-
42152 3e 69 44 6f 63 69 64 2c 20 26 70 44 4c 2d 3e 6e  >iDocid, &pDL->n
42153 4c 69 73 74 2c 20 70 62 45 6f 66 0a 20 20 20 20  List, pbEof.    
42154 29 3b 0a 20 20 20 20 70 44 4c 2d 3e 70 4c 69 73  );.    pDL->pLis
42155 74 20 3d 20 70 44 4c 2d 3e 70 4e 65 78 74 44 6f  t = pDL->pNextDo
42156 63 69 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  cid;.  }else{.  
42157 20 20 63 68 61 72 20 2a 70 49 74 65 72 3b 20 20    char *pIter;  
42158 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42159 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
4215a 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72  d to iterate thr
4215b 6f 75 67 68 20 61 41 6c 6c 20 2a 2f 0a 20 20 20  ough aAll */.   
4215c 20 63 68 61 72 20 2a 70 45 6e 64 20 3d 20 26 70   char *pEnd = &p
4215d 44 4c 2d 3e 61 41 6c 6c 5b 70 44 4c 2d 3e 6e 41  DL->aAll[pDL->nA
4215e 6c 6c 5d 3b 20 20 20 20 20 2f 2a 20 31 20 62 79  ll];     /* 1 by
4215f 74 65 20 70 61 73 74 20 65 6e 64 20 6f 66 20 61  te past end of a
42160 41 6c 6c 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  All */.    if( p
42161 44 4c 2d 3e 70 4e 65 78 74 44 6f 63 69 64 20 29  DL->pNextDocid )
42162 7b 0a 20 20 20 20 20 20 70 49 74 65 72 20 3d 20  {.      pIter = 
42163 70 44 4c 2d 3e 70 4e 65 78 74 44 6f 63 69 64 3b  pDL->pNextDocid;
42164 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
42165 20 20 70 49 74 65 72 20 3d 20 70 44 4c 2d 3e 61    pIter = pDL->a
42166 41 6c 6c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  All;.    }..    
42167 69 66 28 20 70 49 74 65 72 3e 3d 70 45 6e 64 20  if( pIter>=pEnd 
42168 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 68  ){.      /* We h
42169 61 76 65 20 61 6c 72 65 61 64 79 20 72 65 61 63  ave already reac
4216a 68 65 64 20 74 68 65 20 65 6e 64 20 6f 66 20 74  hed the end of t
4216b 68 69 73 20 64 6f 63 6c 69 73 74 2e 20 45 4f 46  his doclist. EOF
4216c 2e 20 2a 2f 0a 20 20 20 20 20 20 2a 70 62 45 6f  . */.      *pbEo
4216d 66 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  f = 1;.    }else
4216e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
4216f 69 6e 74 36 34 20 69 44 65 6c 74 61 3b 0a 20 20  int64 iDelta;.  
42170 20 20 20 20 70 49 74 65 72 20 2b 3d 20 73 71 6c      pIter += sql
42171 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e  ite3Fts3GetVarin
42172 74 28 70 49 74 65 72 2c 20 26 69 44 65 6c 74 61  t(pIter, &iDelta
42173 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61  );.      if( pTa
42174 62 2d 3e 62 44 65 73 63 49 64 78 3d 3d 30 20 7c  b->bDescIdx==0 |
42175 7c 20 70 44 4c 2d 3e 70 4e 65 78 74 44 6f 63 69  | pDL->pNextDoci
42176 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  d==0 ){.        
42177 70 44 4c 2d 3e 69 44 6f 63 69 64 20 2b 3d 20 69  pDL->iDocid += i
42178 44 65 6c 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c  Delta;.      }el
42179 73 65 7b 0a 20 20 20 20 20 20 20 20 70 44 4c 2d  se{.        pDL-
4217a 3e 69 44 6f 63 69 64 20 2d 3d 20 69 44 65 6c 74  >iDocid -= iDelt
4217b 61 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  a;.      }.     
4217c 20 70 44 4c 2d 3e 70 4c 69 73 74 20 3d 20 70 49   pDL->pList = pI
4217d 74 65 72 3b 0a 20 20 20 20 20 20 66 74 73 33 50  ter;.      fts3P
4217e 6f 73 6c 69 73 74 43 6f 70 79 28 30 2c 20 26 70  oslistCopy(0, &p
4217f 49 74 65 72 29 3b 0a 20 20 20 20 20 20 70 44 4c  Iter);.      pDL
42180 2d 3e 6e 4c 69 73 74 20 3d 20 28 69 6e 74 29 28  ->nList = (int)(
42181 70 49 74 65 72 20 2d 20 70 44 4c 2d 3e 70 4c 69  pIter - pDL->pLi
42182 73 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 70  st);..      /* p
42183 49 74 65 72 20 6e 6f 77 20 70 6f 69 6e 74 73 20  Iter now points 
42184 6a 75 73 74 20 70 61 73 74 20 74 68 65 20 30 78  just past the 0x
42185 30 30 20 74 68 61 74 20 74 65 72 6d 69 6e 61 74  00 that terminat
42186 65 73 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 2d  es the position-
42187 0a 20 20 20 20 20 20 2a 2a 20 6c 69 73 74 20 66  .      ** list f
42188 6f 72 20 64 6f 63 75 6d 65 6e 74 20 70 44 4c 2d  or document pDL-
42189 3e 69 44 6f 63 69 64 2e 20 48 6f 77 65 76 65 72  >iDocid. However
4218a 2c 20 69 66 20 74 68 69 73 20 70 6f 73 69 74 69  , if this positi
4218b 6f 6e 2d 6c 69 73 74 20 77 61 73 0a 20 20 20 20  on-list was.    
4218c 20 20 2a 2a 20 65 64 69 74 65 64 20 69 6e 20 70    ** edited in p
4218d 6c 61 63 65 20 62 79 20 66 74 73 33 45 76 61 6c  lace by fts3Eval
4218e 4e 65 61 72 54 72 69 6d 28 29 2c 20 74 68 65 6e  NearTrim(), then
4218f 20 70 49 74 65 72 20 6d 61 79 20 6e 6f 74 20 61   pIter may not a
42190 63 74 75 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a  ctually.      **
42191 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 74   point to the st
42192 61 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20  art of the next 
42193 64 6f 63 69 64 20 76 61 6c 75 65 2e 20 54 68 65  docid value. The
42194 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c 69 6e 65 20   following line 
42195 64 65 61 6c 73 0a 20 20 20 20 20 20 2a 2a 20 77  deals.      ** w
42196 69 74 68 20 74 68 69 73 20 63 61 73 65 20 62 79  ith this case by
42197 20 61 64 76 61 6e 63 69 6e 67 20 70 49 74 65 72   advancing pIter
42198 20 70 61 73 74 20 74 68 65 20 7a 65 72 6f 2d 70   past the zero-p
42199 61 64 64 69 6e 67 20 61 64 64 65 64 20 62 79 0a  adding added by.
4219a 20 20 20 20 20 20 2a 2a 20 66 74 73 33 45 76 61        ** fts3Eva
4219b 6c 4e 65 61 72 54 72 69 6d 28 29 2e 20 20 2a 2f  lNearTrim().  */
4219c 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 49  .      while( pI
4219d 74 65 72 3c 70 45 6e 64 20 26 26 20 2a 70 49 74  ter<pEnd && *pIt
4219e 65 72 3d 3d 30 20 29 20 70 49 74 65 72 2b 2b 3b  er==0 ) pIter++;
4219f 0a 0a 20 20 20 20 20 20 70 44 4c 2d 3e 70 4e 65  ..      pDL->pNe
421a0 78 74 44 6f 63 69 64 20 3d 20 70 49 74 65 72 3b  xtDocid = pIter;
421a1 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
421a2 49 74 65 72 3e 3d 26 70 44 4c 2d 3e 61 41 6c 6c  Iter>=&pDL->aAll
421a3 5b 70 44 4c 2d 3e 6e 41 6c 6c 5d 20 7c 7c 20 2a  [pDL->nAll] || *
421a4 70 49 74 65 72 20 29 3b 0a 20 20 20 20 20 20 2a  pIter );.      *
421a5 70 62 45 6f 66 20 3d 20 30 3b 0a 20 20 20 20 7d  pbEof = 0;.    }
421a6 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
421a7 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 0a 2a 2a 20 49  c;.}../*.**.** I
421a8 66 20 2a 70 52 63 20 69 73 20 6e 6f 74 20 53 51  f *pRc is not SQ
421a9 4c 49 54 45 5f 4f 4b 20 77 68 65 6e 20 74 68 69  LITE_OK when thi
421aa 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
421ab 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f  lled, it is a no
421ac 2d 6f 70 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  -op..** Otherwis
421ad 65 2c 20 66 74 73 33 45 76 61 6c 50 68 72 61 73  e, fts3EvalPhras
421ae 65 53 74 61 72 74 28 29 20 69 73 20 63 61 6c 6c  eStart() is call
421af 65 64 20 6f 6e 20 61 6c 6c 20 70 68 72 61 73 65  ed on all phrase
421b0 73 20 77 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20  s within the.** 
421b1 65 78 70 72 65 73 73 69 6f 6e 2e 20 41 6c 73 6f  expression. Also
421b2 20 74 68 65 20 46 74 73 33 45 78 70 72 2e 62 44   the Fts3Expr.bD
421b3 65 66 65 72 72 65 64 20 76 61 72 69 61 62 6c 65  eferred variable
421b4 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20   is set to true 
421b5 66 6f 72 20 61 6e 79 0a 2a 2a 20 65 78 70 72 65  for any.** expre
421b6 73 73 69 6f 6e 73 20 66 6f 72 20 77 68 69 63 68  ssions for which
421b7 20 61 6c 6c 20 64 65 73 63 65 6e 64 65 6e 74 20   all descendent 
421b8 74 6f 6b 65 6e 73 20 61 72 65 20 64 65 66 65 72  tokens are defer
421b9 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61  red..**.** If pa
421ba 72 61 6d 65 74 65 72 20 62 4f 70 74 4f 6b 20 69  rameter bOptOk i
421bb 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74 20  s zero, then it 
421bc 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 68  is guaranteed th
421bd 61 74 20 74 68 65 0a 2a 2a 20 46 74 73 33 50 68  at the.** Fts3Ph
421be 72 61 73 65 2e 64 6f 63 6c 69 73 74 2e 61 41 6c  rase.doclist.aAl
421bf 6c 2f 6e 41 6c 6c 20 76 61 72 69 61 62 6c 65 73  l/nAll variables
421c0 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 65 6e 74   contain the ent
421c1 69 72 65 20 64 6f 63 6c 69 73 74 20 66 6f 72 0a  ire doclist for.
421c2 2a 2a 20 65 61 63 68 20 70 68 72 61 73 65 20 69  ** each phrase i
421c3 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
421c4 20 28 73 75 62 6a 65 63 74 20 74 6f 20 64 65 66   (subject to def
421c5 65 72 72 65 64 20 74 6f 6b 65 6e 20 70 72 6f 63  erred token proc
421c6 65 73 73 69 6e 67 29 2e 0a 2a 2a 20 4f 72 2c 20  essing)..** Or, 
421c7 69 66 20 62 4f 70 74 4f 6b 20 69 73 20 6e 6f 6e  if bOptOk is non
421c8 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 6f 6e 65 20  -zero, then one 
421c9 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e 73 20 77  or more tokens w
421ca 69 74 68 69 6e 20 74 68 65 20 65 78 70 72 65 73  ithin the expres
421cb 73 69 6f 6e 0a 2a 2a 20 6d 61 79 20 62 65 20 6c  sion.** may be l
421cc 6f 61 64 65 64 20 69 6e 63 72 65 6d 65 6e 74 61  oaded incrementa
421cd 6c 6c 79 2c 20 6d 65 61 6e 69 6e 67 20 64 6f 63  lly, meaning doc
421ce 6c 69 73 74 2e 61 41 6c 6c 2f 6e 41 6c 6c 20 69  list.aAll/nAll i
421cf 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e  s not available.
421d0 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  .**.** If an err
421d1 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e  or occurs within
421d2 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
421d3 2a 70 52 63 20 69 73 20 73 65 74 20 74 6f 20 61  *pRc is set to a
421d4 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 0a 2a  n SQLite error.*
421d5 2a 20 63 6f 64 65 20 62 65 66 6f 72 65 20 72 65  * code before re
421d6 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  turning..*/.stat
421d7 69 63 20 76 6f 69 64 20 66 74 73 33 45 76 61 6c  ic void fts3Eval
421d8 53 74 61 72 74 52 65 61 64 65 72 73 28 0a 20 20  StartReaders(.  
421d9 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72  Fts3Cursor *pCsr
421da 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
421db 2f 2a 20 46 54 53 20 43 75 72 73 6f 72 20 68 61  /* FTS Cursor ha
421dc 6e 64 6c 65 20 2a 2f 0a 20 20 46 74 73 33 45 78  ndle */.  Fts3Ex
421dd 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20  pr *pExpr,      
421de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
421df 72 65 73 73 69 6f 6e 20 74 6f 20 69 6e 69 74 69  ression to initi
421e0 61 6c 69 7a 65 20 70 68 72 61 73 65 73 20 69 6e  alize phrases in
421e1 20 2a 2f 0a 20 20 69 6e 74 20 62 4f 70 74 4f 6b   */.  int bOptOk
421e2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
421e3 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
421e4 20 65 6e 61 62 6c 65 20 69 6e 63 72 65 6d 65 6e   enable incremen
421e5 74 61 6c 20 6c 6f 61 64 69 6e 67 20 2a 2f 0a 20  tal loading */. 
421e6 20 69 6e 74 20 2a 70 52 63 20 20 20 20 20 20 20   int *pRc       
421e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
421e8 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 45 72 72 6f   /* IN/OUT: Erro
421e9 72 20 63 6f 64 65 20 2a 2f 0a 29 7b 0a 20 20 69  r code */.){.  i
421ea 66 28 20 70 45 78 70 72 20 26 26 20 53 51 4c 49  f( pExpr && SQLI
421eb 54 45 5f 4f 4b 3d 3d 2a 70 52 63 20 29 7b 0a 20  TE_OK==*pRc ){. 
421ec 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 65 54     if( pExpr->eT
421ed 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48  ype==FTSQUERY_PH
421ee 52 41 53 45 20 29 7b 0a 20 20 20 20 20 20 69 6e  RASE ){.      in
421ef 74 20 69 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  t i;.      int n
421f0 54 6f 6b 65 6e 20 3d 20 70 45 78 70 72 2d 3e 70  Token = pExpr->p
421f1 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a  Phrase->nToken;.
421f2 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
421f3 3c 6e 54 6f 6b 65 6e 3b 20 69 2b 2b 29 7b 0a 20  <nToken; i++){. 
421f4 20 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72         if( pExpr
421f5 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65  ->pPhrase->aToke
421f6 6e 5b 69 5d 2e 70 44 65 66 65 72 72 65 64 3d 3d  n[i].pDeferred==
421f7 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  0 ) break;.     
421f8 20 7d 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e   }.      pExpr->
421f9 62 44 65 66 65 72 72 65 64 20 3d 20 28 69 3d 3d  bDeferred = (i==
421fa 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 2a  nToken);.      *
421fb 70 52 63 20 3d 20 66 74 73 33 45 76 61 6c 50 68  pRc = fts3EvalPh
421fc 72 61 73 65 53 74 61 72 74 28 70 43 73 72 2c 20  raseStart(pCsr, 
421fd 62 4f 70 74 4f 6b 2c 20 70 45 78 70 72 2d 3e 70  bOptOk, pExpr->p
421fe 50 68 72 61 73 65 29 3b 0a 20 20 20 20 7d 65 6c  Phrase);.    }el
421ff 73 65 7b 0a 20 20 20 20 20 20 66 74 73 33 45 76  se{.      fts3Ev
42200 61 6c 53 74 61 72 74 52 65 61 64 65 72 73 28 70  alStartReaders(p
42201 43 73 72 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  Csr, pExpr->pLef
42202 74 2c 20 62 4f 70 74 4f 6b 2c 20 70 52 63 29 3b  t, bOptOk, pRc);
42203 0a 20 20 20 20 20 20 66 74 73 33 45 76 61 6c 53  .      fts3EvalS
42204 74 61 72 74 52 65 61 64 65 72 73 28 70 43 73 72  tartReaders(pCsr
42205 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
42206 20 62 4f 70 74 4f 6b 2c 20 70 52 63 29 3b 0a 20   bOptOk, pRc);. 
42207 20 20 20 20 20 70 45 78 70 72 2d 3e 62 44 65 66       pExpr->bDef
42208 65 72 72 65 64 20 3d 20 28 70 45 78 70 72 2d 3e  erred = (pExpr->
42209 70 4c 65 66 74 2d 3e 62 44 65 66 65 72 72 65 64  pLeft->bDeferred
4220a 20 26 26 20 70 45 78 70 72 2d 3e 70 52 69 67 68   && pExpr->pRigh
4220b 74 2d 3e 62 44 65 66 65 72 72 65 64 29 3b 0a 20  t->bDeferred);. 
4220c 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
4220d 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20 74 68  * An array of th
4220e 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
4220f 63 74 75 72 65 73 20 69 73 20 61 73 73 65 6d 62  ctures is assemb
42210 6c 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  led as part of t
42211 68 65 20 70 72 6f 63 65 73 73 0a 2a 2a 20 6f 66  he process.** of
42212 20 73 65 6c 65 63 74 69 6e 67 20 74 6f 6b 65 6e   selecting token
42213 73 20 74 6f 20 64 65 66 65 72 20 62 65 66 6f 72  s to defer befor
42214 65 20 74 68 65 20 71 75 65 72 79 20 73 74 61 72  e the query star
42215 74 73 20 65 78 65 63 75 74 69 6e 67 20 28 61 73  ts executing (as
42216 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20   part.** of the 
42217 78 46 69 6c 74 65 72 28 29 20 6d 65 74 68 6f 64  xFilter() method
42218 29 2e 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20  ). There is one 
42219 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 61  element in the a
4221a 72 72 61 79 20 66 6f 72 20 65 61 63 68 0a 2a 2a  rray for each.**
4221b 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 46 54   token in the FT
4221c 53 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a  S expression..**
4221d 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61 72 65 20 64  .** Tokens are d
4221e 69 76 69 64 65 64 20 69 6e 74 6f 20 41 4e 44 2f  ivided into AND/
4221f 4e 45 41 52 20 63 6c 75 73 74 65 72 73 2e 20 41  NEAR clusters. A
42220 6c 6c 20 74 6f 6b 65 6e 73 20 69 6e 20 61 20 63  ll tokens in a c
42221 6c 75 73 74 65 72 20 62 65 6c 6f 6e 67 0a 2a 2a  luster belong.**
42222 20 74 6f 20 70 68 72 61 73 65 73 20 74 68 61 74   to phrases that
42223 20 61 72 65 20 63 6f 6e 6e 65 63 74 65 64 20 6f   are connected o
42224 6e 6c 79 20 62 79 20 41 4e 44 20 61 6e 64 20 4e  nly by AND and N
42225 45 41 52 20 6f 70 65 72 61 74 6f 72 73 20 28 6e  EAR operators (n
42226 6f 74 20 4f 52 20 6f 72 0a 2a 2a 20 4e 4f 54 29  ot OR or.** NOT)
42227 2e 20 57 68 65 6e 20 64 65 74 65 72 6d 69 6e 69  . When determini
42228 6e 67 20 74 6f 6b 65 6e 73 20 74 6f 20 64 65 66  ng tokens to def
42229 65 72 2c 20 65 61 63 68 20 41 4e 44 2f 4e 45 41  er, each AND/NEA
4222a 52 20 63 6c 75 73 74 65 72 20 69 73 20 63 6f 6e  R cluster is con
4222b 73 69 64 65 72 65 64 0a 2a 2a 20 73 65 70 61 72  sidered.** separ
4222c 61 74 65 6c 79 2e 20 54 68 65 20 72 6f 6f 74 20  ately. The root 
4222d 6f 66 20 61 20 74 6f 6b 65 6e 73 20 41 4e 44 2f  of a tokens AND/
4222e 4e 45 41 52 20 63 6c 75 73 74 65 72 20 69 73 20  NEAR cluster is 
4222f 73 74 6f 72 65 64 20 69 6e 20 0a 2a 2a 20 46 74  stored in .** Ft
42230 73 33 54 6f 6b 65 6e 41 6e 64 43 6f 73 74 2e 70  s3TokenAndCost.p
42231 52 6f 6f 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  Root..*/.typedef
42232 20 73 74 72 75 63 74 20 46 74 73 33 54 6f 6b 65   struct Fts3Toke
42233 6e 41 6e 64 43 6f 73 74 20 46 74 73 33 54 6f 6b  nAndCost Fts3Tok
42234 65 6e 41 6e 64 43 6f 73 74 3b 0a 73 74 72 75 63  enAndCost;.struc
42235 74 20 46 74 73 33 54 6f 6b 65 6e 41 6e 64 43 6f  t Fts3TokenAndCo
42236 73 74 20 7b 0a 20 20 46 74 73 33 50 68 72 61 73  st {.  Fts3Phras
42237 65 20 2a 70 50 68 72 61 73 65 3b 20 20 20 20 20  e *pPhrase;     
42238 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 68         /* The ph
42239 72 61 73 65 20 74 68 65 20 74 6f 6b 65 6e 20 62  rase the token b
4223a 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 20 20 69  elongs to */.  i
4223b 6e 74 20 69 54 6f 6b 65 6e 3b 20 20 20 20 20 20  nt iToken;      
4223c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4223d 2a 20 50 6f 73 69 74 69 6f 6e 20 6f 66 20 74 6f  * Position of to
4223e 6b 65 6e 20 69 6e 20 70 68 72 61 73 65 20 2a 2f  ken in phrase */
4223f 0a 20 20 46 74 73 33 50 68 72 61 73 65 54 6f 6b  .  Fts3PhraseTok
42240 65 6e 20 2a 70 54 6f 6b 65 6e 3b 20 20 20 20 20  en *pToken;     
42241 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20     /* The token 
42242 69 74 73 65 6c 66 20 2a 2f 0a 20 20 46 74 73 33  itself */.  Fts3
42243 45 78 70 72 20 2a 70 52 6f 6f 74 3b 20 20 20 20  Expr *pRoot;    
42244 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
42245 6f 6f 74 20 6f 66 20 4e 45 41 52 2f 41 4e 44 20  oot of NEAR/AND 
42246 63 6c 75 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74  cluster */.  int
42247 20 6e 4f 76 66 6c 3b 20 20 20 20 20 20 20 20 20   nOvfl;         
42248 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
42249 4e 75 6d 62 65 72 20 6f 66 20 6f 76 65 72 66 6c  Number of overfl
4224a 6f 77 20 70 61 67 65 73 20 74 6f 20 6c 6f 61 64  ow pages to load
4224b 20 64 6f 63 6c 69 73 74 20 2a 2f 0a 20 20 69 6e   doclist */.  in
4224c 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  t iCol;         
4224d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4224e 20 54 68 65 20 63 6f 6c 75 6d 6e 20 74 68 65 20   The column the 
4224f 74 6f 6b 65 6e 20 6d 75 73 74 20 6d 61 74 63 68  token must match
42250 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
42251 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
42252 73 65 64 20 74 6f 20 70 6f 70 75 6c 61 74 65 20  sed to populate 
42253 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 46 74 73  an allocated Fts
42254 33 54 6f 6b 65 6e 41 6e 64 43 6f 73 74 20 61 72  3TokenAndCost ar
42255 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 2a 70  ray..**.** If *p
42256 52 63 20 69 73 20 6e 6f 74 20 53 51 4c 49 54 45  Rc is not SQLITE
42257 5f 4f 4b 20 77 68 65 6e 20 74 68 69 73 20 66 75  _OK when this fu
42258 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
42259 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  , it is a no-op.
4225a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69  .** Otherwise, i
4225b 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
4225c 73 20 64 75 72 69 6e 67 20 65 78 65 63 75 74 69  s during executi
4225d 6f 6e 2c 20 2a 70 52 63 20 69 73 20 73 65 74 20  on, *pRc is set 
4225e 74 6f 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20  to an.** SQLite 
4225f 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73  error code..*/.s
42260 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 45  tatic void fts3E
42261 76 61 6c 54 6f 6b 65 6e 43 6f 73 74 73 28 0a 20  valTokenCosts(. 
42262 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73   Fts3Cursor *pCs
42263 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
42264 20 2f 2a 20 46 54 53 20 43 75 72 73 6f 72 20 68   /* FTS Cursor h
42265 61 6e 64 6c 65 20 2a 2f 0a 20 20 46 74 73 33 45  andle */.  Fts3E
42266 78 70 72 20 2a 70 52 6f 6f 74 2c 20 20 20 20 20  xpr *pRoot,     
42267 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
42268 6f 74 20 6f 66 20 63 75 72 72 65 6e 74 20 41 4e  ot of current AN
42269 44 2f 4e 45 41 52 20 63 6c 75 73 74 65 72 20 2a  D/NEAR cluster *
4226a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 45  /.  Fts3Expr *pE
4226b 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  xpr,            
4226c 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
4226d 6e 20 74 6f 20 63 6f 6e 73 69 64 65 72 20 2a 2f  n to consider */
4226e 0a 20 20 46 74 73 33 54 6f 6b 65 6e 41 6e 64 43  .  Fts3TokenAndC
4226f 6f 73 74 20 2a 2a 70 70 54 43 2c 20 20 20 20 20  ost **ppTC,     
42270 20 20 20 2f 2a 20 57 72 69 74 65 20 6e 65 77 20     /* Write new 
42271 65 6e 74 72 69 65 73 20 74 6f 20 2a 28 2a 70 70  entries to *(*pp
42272 54 43 29 2b 2b 20 2a 2f 0a 20 20 46 74 73 33 45  TC)++ */.  Fts3E
42273 78 70 72 20 2a 2a 2a 70 70 4f 72 2c 20 20 20 20  xpr ***ppOr,    
42274 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
42275 69 74 65 20 6e 65 77 20 4f 52 20 72 6f 6f 74 20  ite new OR root 
42276 74 6f 20 2a 28 2a 70 70 4f 72 29 2b 2b 20 2a 2f  to *(*ppOr)++ */
42277 0a 20 20 69 6e 74 20 2a 70 52 63 20 20 20 20 20  .  int *pRc     
42278 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42279 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 45 72     /* IN/OUT: Er
4227a 72 6f 72 20 63 6f 64 65 20 2a 2f 0a 29 7b 0a 20  ror code */.){. 
4227b 20 69 66 28 20 2a 70 52 63 3d 3d 53 51 4c 49 54   if( *pRc==SQLIT
4227c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
4227d 70 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d 46 54  pExpr->eType==FT
4227e 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29 7b  SQUERY_PHRASE ){
4227f 0a 20 20 20 20 20 20 46 74 73 33 50 68 72 61 73  .      Fts3Phras
42280 65 20 2a 70 50 68 72 61 73 65 20 3d 20 70 45 78  e *pPhrase = pEx
42281 70 72 2d 3e 70 50 68 72 61 73 65 3b 0a 20 20 20  pr->pPhrase;.   
42282 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
42283 66 6f 72 28 69 3d 30 3b 20 2a 70 52 63 3d 3d 53  for(i=0; *pRc==S
42284 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 70 50  QLITE_OK && i<pP
42285 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 69  hrase->nToken; i
42286 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 46 74 73  ++){.        Fts
42287 33 54 6f 6b 65 6e 41 6e 64 43 6f 73 74 20 2a 70  3TokenAndCost *p
42288 54 43 20 3d 20 28 2a 70 70 54 43 29 2b 2b 3b 0a  TC = (*ppTC)++;.
42289 20 20 20 20 20 20 20 20 70 54 43 2d 3e 70 50 68          pTC->pPh
4228a 72 61 73 65 20 3d 20 70 50 68 72 61 73 65 3b 0a  rase = pPhrase;.
4228b 20 20 20 20 20 20 20 20 70 54 43 2d 3e 69 54 6f          pTC->iTo
4228c 6b 65 6e 20 3d 20 69 3b 0a 20 20 20 20 20 20 20  ken = i;.       
4228d 20 70 54 43 2d 3e 70 52 6f 6f 74 20 3d 20 70 52   pTC->pRoot = pR
4228e 6f 6f 74 3b 0a 20 20 20 20 20 20 20 20 70 54 43  oot;.        pTC
4228f 2d 3e 70 54 6f 6b 65 6e 20 3d 20 26 70 50 68 72  ->pToken = &pPhr
42290 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 3b 0a  ase->aToken[i];.
42291 20 20 20 20 20 20 20 20 70 54 43 2d 3e 69 43 6f          pTC->iCo
42292 6c 20 3d 20 70 50 68 72 61 73 65 2d 3e 69 43 6f  l = pPhrase->iCo
42293 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 2a 70  lumn;.        *p
42294 52 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  Rc = sqlite3Fts3
42295 4d 73 72 4f 76 66 6c 28 70 43 73 72 2c 20 70 54  MsrOvfl(pCsr, pT
42296 43 2d 3e 70 54 6f 6b 65 6e 2d 3e 70 53 65 67 63  C->pToken->pSegc
42297 73 72 2c 20 26 70 54 43 2d 3e 6e 4f 76 66 6c 29  sr, &pTC->nOvfl)
42298 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
42299 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 65  lse if( pExpr->e
4229a 54 79 70 65 21 3d 46 54 53 51 55 45 52 59 5f 4e  Type!=FTSQUERY_N
4229b 4f 54 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  OT ){.      asse
4229c 72 74 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65  rt( pExpr->eType
4229d 3d 3d 46 54 53 51 55 45 52 59 5f 4f 52 0a 20 20  ==FTSQUERY_OR.  
4229e 20 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70           || pExp
4229f 72 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  r->eType==FTSQUE
422a0 52 59 5f 41 4e 44 0a 20 20 20 20 20 20 20 20 20  RY_AND.         
422a1 20 20 7c 7c 20 70 45 78 70 72 2d 3e 65 54 79 70    || pExpr->eTyp
422a2 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
422a3 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20  .      );.      
422a4 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70  assert( pExpr->p
422a5 4c 65 66 74 20 26 26 20 70 45 78 70 72 2d 3e 70  Left && pExpr->p
422a6 52 69 67 68 74 20 29 3b 0a 20 20 20 20 20 20 69  Right );.      i
422a7 66 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65 3d  f( pExpr->eType=
422a8 3d 46 54 53 51 55 45 52 59 5f 4f 52 20 29 7b 0a  =FTSQUERY_OR ){.
422a9 20 20 20 20 20 20 20 20 70 52 6f 6f 74 20 3d 20          pRoot = 
422aa 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20  pExpr->pLeft;.  
422ab 20 20 20 20 20 20 2a 2a 70 70 4f 72 20 3d 20 70        **ppOr = p
422ac 52 6f 6f 74 3b 0a 20 20 20 20 20 20 20 20 28 2a  Root;.        (*
422ad 70 70 4f 72 29 2b 2b 3b 0a 20 20 20 20 20 20 7d  ppOr)++;.      }
422ae 0a 20 20 20 20 20 20 66 74 73 33 45 76 61 6c 54  .      fts3EvalT
422af 6f 6b 65 6e 43 6f 73 74 73 28 70 43 73 72 2c 20  okenCosts(pCsr, 
422b0 70 52 6f 6f 74 2c 20 70 45 78 70 72 2d 3e 70 4c  pRoot, pExpr->pL
422b1 65 66 74 2c 20 70 70 54 43 2c 20 70 70 4f 72 2c  eft, ppTC, ppOr,
422b2 20 70 52 63 29 3b 0a 20 20 20 20 20 20 69 66 28   pRc);.      if(
422b3 20 70 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d 46   pExpr->eType==F
422b4 54 53 51 55 45 52 59 5f 4f 52 20 29 7b 0a 20 20  TSQUERY_OR ){.  
422b5 20 20 20 20 20 20 70 52 6f 6f 74 20 3d 20 70 45        pRoot = pE
422b6 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20  xpr->pRight;.   
422b7 20 20 20 20 20 2a 2a 70 70 4f 72 20 3d 20 70 52       **ppOr = pR
422b8 6f 6f 74 3b 0a 20 20 20 20 20 20 20 20 28 2a 70  oot;.        (*p
422b9 70 4f 72 29 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a  pOr)++;.      }.
422ba 20 20 20 20 20 20 66 74 73 33 45 76 61 6c 54 6f        fts3EvalTo
422bb 6b 65 6e 43 6f 73 74 73 28 70 43 73 72 2c 20 70  kenCosts(pCsr, p
422bc 52 6f 6f 74 2c 20 70 45 78 70 72 2d 3e 70 52 69  Root, pExpr->pRi
422bd 67 68 74 2c 20 70 70 54 43 2c 20 70 70 4f 72 2c  ght, ppTC, ppOr,
422be 20 70 52 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   pRc);.    }.  }
422bf 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d  .}../*.** Determ
422c0 69 6e 65 20 74 68 65 20 61 76 65 72 61 67 65 20  ine the average 
422c1 64 6f 63 75 6d 65 6e 74 20 28 72 6f 77 29 20 73  document (row) s
422c2 69 7a 65 20 69 6e 20 70 61 67 65 73 2e 20 49 66  ize in pages. If
422c3 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20   successful,.** 
422c4 77 72 69 74 65 20 74 68 69 73 20 76 61 6c 75 65  write this value
422c5 20 74 6f 20 2a 70 6e 50 61 67 65 20 61 6e 64 20   to *pnPage and 
422c6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
422c7 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74  . Otherwise, ret
422c8 75 72 6e 0a 2a 2a 20 61 6e 20 53 51 4c 69 74 65  urn.** an SQLite
422c9 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a   error code..**.
422ca 2a 2a 20 54 68 65 20 61 76 65 72 61 67 65 20 64  ** The average d
422cb 6f 63 75 6d 65 6e 74 20 73 69 7a 65 20 69 6e 20  ocument size in 
422cc 70 61 67 65 73 20 69 73 20 63 61 6c 63 75 6c 61  pages is calcula
422cd 74 65 64 20 62 79 20 66 69 72 73 74 20 63 61 6c  ted by first cal
422ce 63 75 6c 61 74 69 6e 67 20 0a 2a 2a 20 64 65 74  culating .** det
422cf 65 72 6d 69 6e 69 6e 67 20 74 68 65 20 61 76 65  ermining the ave
422d0 72 61 67 65 20 73 69 7a 65 20 69 6e 20 62 79 74  rage size in byt
422d1 65 73 2c 20 42 2e 20 49 66 20 42 20 69 73 20 6c  es, B. If B is l
422d2 65 73 73 20 74 68 61 6e 20 74 68 65 20 61 6d 6f  ess than the amo
422d3 75 6e 74 0a 2a 2a 20 6f 66 20 64 61 74 61 20 74  unt.** of data t
422d4 68 61 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20  hat will fit on 
422d5 61 20 73 69 6e 67 6c 65 20 6c 65 61 66 20 70 61  a single leaf pa
422d6 67 65 20 6f 66 20 61 6e 20 69 6e 74 6b 65 79 20  ge of an intkey 
422d7 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 74 68 69 73  table in.** this
422d8 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20   database, then 
422d9 74 68 65 20 61 76 65 72 61 67 65 20 64 6f 63 73  the average docs
422da 69 7a 65 20 69 73 20 31 2e 20 4f 74 68 65 72 77  ize is 1. Otherw
422db 69 73 65 2c 20 69 74 20 69 73 20 31 20 70 6c 75  ise, it is 1 plu
422dc 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  s.** the number 
422dd 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  of overflow page
422de 73 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 61 20  s consumed by a 
422df 72 65 63 6f 72 64 20 42 20 62 79 74 65 73 20 69  record B bytes i
422e0 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69  n size..*/.stati
422e1 63 20 69 6e 74 20 66 74 73 33 45 76 61 6c 41 76  c int fts3EvalAv
422e2 65 72 61 67 65 44 6f 63 73 69 7a 65 28 46 74 73  erageDocsize(Fts
422e3 33 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 69  3Cursor *pCsr, i
422e4 6e 74 20 2a 70 6e 50 61 67 65 29 7b 0a 20 20 69  nt *pnPage){.  i
422e5 66 28 20 70 43 73 72 2d 3e 6e 52 6f 77 41 76 67  f( pCsr->nRowAvg
422e6 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ==0 ){.    /* Th
422e7 65 20 61 76 65 72 61 67 65 20 64 6f 63 75 6d 65  e average docume
422e8 6e 74 20 73 69 7a 65 2c 20 77 68 69 63 68 20 69  nt size, which i
422e9 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 63 61  s required to ca
422ea 6c 63 75 6c 61 74 65 20 74 68 65 20 63 6f 73 74  lculate the cost
422eb 0a 20 20 20 20 2a 2a 20 6f 66 20 65 61 63 68 20  .    ** of each 
422ec 64 6f 63 6c 69 73 74 2c 20 68 61 73 20 6e 6f 74  doclist, has not
422ed 20 79 65 74 20 62 65 65 6e 20 64 65 74 65 72 6d   yet been determ
422ee 69 6e 65 64 2e 20 52 65 61 64 20 74 68 65 20 72  ined. Read the r
422ef 65 71 75 69 72 65 64 20 0a 20 20 20 20 2a 2a 20  equired .    ** 
422f0 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 25 5f  data from the %_
422f1 73 74 61 74 20 74 61 62 6c 65 20 74 6f 20 63 61  stat table to ca
422f2 6c 63 75 6c 61 74 65 20 69 74 2e 0a 20 20 20 20  lculate it..    
422f3 2a 2a 0a 20 20 20 20 2a 2a 20 45 6e 74 72 79 20  **.    ** Entry 
422f4 30 20 6f 66 20 74 68 65 20 25 5f 73 74 61 74 20  0 of the %_stat 
422f5 74 61 62 6c 65 20 69 73 20 61 20 62 6c 6f 62 20  table is a blob 
422f6 63 6f 6e 74 61 69 6e 69 6e 67 20 28 6e 43 6f 6c  containing (nCol
422f7 2b 31 29 20 46 54 53 33 20 0a 20 20 20 20 2a 2a  +1) FTS3 .    **
422f8 20 76 61 72 69 6e 74 73 2c 20 77 68 65 72 65 20   varints, where 
422f9 6e 43 6f 6c 20 69 73 20 74 68 65 20 6e 75 6d 62  nCol is the numb
422fa 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
422fb 20 74 68 65 20 46 54 53 33 20 74 61 62 6c 65 2e   the FTS3 table.
422fc 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 69 72 73  .    ** The firs
422fd 74 20 76 61 72 69 6e 74 20 69 73 20 74 68 65 20  t varint is the 
422fe 6e 75 6d 62 65 72 20 6f 66 20 64 6f 63 75 6d 65  number of docume
422ff 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74  nts currently st
42300 6f 72 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74  ored in.    ** t
42301 68 65 20 74 61 62 6c 65 2e 20 54 68 65 20 66 6f  he table. The fo
42302 6c 6c 6f 77 69 6e 67 20 6e 43 6f 6c 20 76 61 72  llowing nCol var
42303 69 6e 74 73 20 63 6f 6e 74 61 69 6e 20 74 68 65  ints contain the
42304 20 74 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66   total amount of
42305 0a 20 20 20 20 2a 2a 20 64 61 74 61 20 73 74 6f  .    ** data sto
42306 72 65 64 20 69 6e 20 61 6c 6c 20 72 6f 77 73 20  red in all rows 
42307 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f  of each column o
42308 66 20 74 68 65 20 74 61 62 6c 65 2c 20 66 72 6f  f the table, fro
42309 6d 20 6c 65 66 74 0a 20 20 20 20 2a 2a 20 74 6f  m left.    ** to
4230a 20 72 69 67 68 74 2e 0a 20 20 20 20 2a 2f 0a 20   right..    */. 
4230b 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 46     int rc;.    F
4230c 74 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28 46  ts3Table *p = (F
4230d 74 73 33 54 61 62 6c 65 2a 29 70 43 73 72 2d 3e  ts3Table*)pCsr->
4230e 62 61 73 65 2e 70 56 74 61 62 3b 0a 20 20 20 20  base.pVtab;.    
4230f 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
42310 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  tmt;.    sqlite3
42311 5f 69 6e 74 36 34 20 6e 44 6f 63 20 3d 20 30 3b  _int64 nDoc = 0;
42312 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  .    sqlite3_int
42313 36 34 20 6e 42 79 74 65 20 3d 20 30 3b 0a 20 20  64 nByte = 0;.  
42314 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 45    const char *pE
42315 6e 64 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  nd;.    const ch
42316 61 72 20 2a 61 3b 0a 0a 20 20 20 20 72 63 20 3d  ar *a;..    rc =
42317 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 6c 65   sqlite3Fts3Sele
42318 63 74 44 6f 63 74 6f 74 61 6c 28 70 2c 20 26 70  ctDoctotal(p, &p
42319 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72  Stmt);.    if( r
4231a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
4231b 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 61 20  eturn rc;.    a 
4231c 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
4231d 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29 3b  _blob(pStmt, 0);
4231e 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 20 29  .    assert( a )
4231f 3b 0a 0a 20 20 20 20 70 45 6e 64 20 3d 20 26 61  ;..    pEnd = &a
42320 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
42321 62 79 74 65 73 28 70 53 74 6d 74 2c 20 30 29 5d  bytes(pStmt, 0)]
42322 3b 0a 20 20 20 20 61 20 2b 3d 20 73 71 6c 69 74  ;.    a += sqlit
42323 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 28  e3Fts3GetVarint(
42324 61 2c 20 26 6e 44 6f 63 29 3b 0a 20 20 20 20 77  a, &nDoc);.    w
42325 68 69 6c 65 28 20 61 3c 70 45 6e 64 20 29 7b 0a  hile( a<pEnd ){.
42326 20 20 20 20 20 20 61 20 2b 3d 20 73 71 6c 69 74        a += sqlit
42327 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 28  e3Fts3GetVarint(
42328 61 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20  a, &nByte);.    
42329 7d 0a 20 20 20 20 69 66 28 20 6e 44 6f 63 3d 3d  }.    if( nDoc==
4232a 30 20 7c 7c 20 6e 42 79 74 65 3d 3d 30 20 29 7b  0 || nByte==0 ){
4232b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
4232c 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 20  eset(pStmt);.   
4232d 20 20 20 72 65 74 75 72 6e 20 46 54 53 5f 43 4f     return FTS_CO
4232e 52 52 55 50 54 5f 56 54 41 42 3b 0a 20 20 20 20  RRUPT_VTAB;.    
4232f 7d 0a 0a 20 20 20 20 70 43 73 72 2d 3e 6e 44 6f  }..    pCsr->nDo
42330 63 20 3d 20 6e 44 6f 63 3b 0a 20 20 20 20 70 43  c = nDoc;.    pC
42331 73 72 2d 3e 6e 52 6f 77 41 76 67 20 3d 20 28 69  sr->nRowAvg = (i
42332 6e 74 29 28 28 28 6e 42 79 74 65 20 2f 20 6e 44  nt)(((nByte / nD
42333 6f 63 29 20 2b 20 70 2d 3e 6e 50 67 73 7a 29 20  oc) + p->nPgsz) 
42334 2f 20 70 2d 3e 6e 50 67 73 7a 29 3b 0a 20 20 20  / p->nPgsz);.   
42335 20 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e 6e   assert( pCsr->n
42336 52 6f 77 41 76 67 3e 30 20 29 3b 20 0a 20 20 20  RowAvg>0 ); .   
42337 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65   rc = sqlite3_re
42338 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20  set(pStmt);.    
42339 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
4233a 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
4233b 20 7d 0a 0a 20 20 2a 70 6e 50 61 67 65 20 3d 20   }..  *pnPage = 
4233c 70 43 73 72 2d 3e 6e 52 6f 77 41 76 67 3b 0a 20  pCsr->nRowAvg;. 
4233d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
4233e 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  K;.}../*.** This
4233f 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
42340 6c 65 64 20 74 6f 20 73 65 6c 65 63 74 20 74 68  led to select th
42341 65 20 74 6f 6b 65 6e 73 20 28 69 66 20 61 6e 79  e tokens (if any
42342 29 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 0a  ) that will be .
42343 2a 2a 20 64 65 66 65 72 72 65 64 2e 20 54 68 65  ** deferred. The
42344 20 61 72 72 61 79 20 61 54 43 5b 5d 20 68 61 73   array aTC[] has
42345 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 6f   already been po
42346 70 75 6c 61 74 65 64 20 77 68 65 6e 20 74 68 69  pulated when thi
42347 73 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a  s is.** called..
42348 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
42349 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  ion is called on
4234a 63 65 20 66 6f 72 20 65 61 63 68 20 41 4e 44 2f  ce for each AND/
4234b 4e 45 41 52 20 63 6c 75 73 74 65 72 20 69 6e 20  NEAR cluster in 
4234c 74 68 65 20 0a 2a 2a 20 65 78 70 72 65 73 73 69  the .** expressi
4234d 6f 6e 2e 20 45 61 63 68 20 69 6e 76 6f 63 61 74  on. Each invocat
4234e 69 6f 6e 20 64 65 74 65 72 6d 69 6e 65 73 20 77  ion determines w
4234f 68 69 63 68 20 74 6f 6b 65 6e 73 20 74 6f 20 64  hich tokens to d
42350 65 66 65 72 20 77 69 74 68 69 6e 0a 2a 2a 20 74  efer within.** t
42351 68 65 20 63 6c 75 73 74 65 72 20 77 69 74 68 20  he cluster with 
42352 72 6f 6f 74 20 6e 6f 64 65 20 70 52 6f 6f 74 2e  root node pRoot.
42353 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62   See comments ab
42354 6f 76 65 20 74 68 65 20 64 65 66 69 6e 69 74 69  ove the definiti
42355 6f 6e 0a 2a 2a 20 6f 66 20 73 74 72 75 63 74 20  on.** of struct 
42356 46 74 73 33 54 6f 6b 65 6e 41 6e 64 43 6f 73 74  Fts3TokenAndCost
42357 20 66 6f 72 20 6d 6f 72 65 20 64 65 74 61 69 6c   for more detail
42358 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65  s..**.** If no e
42359 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 53 51 4c  rror occurs, SQL
4235a 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
4235b 65 64 20 61 6e 64 20 73 71 6c 69 74 65 33 46 74  ed and sqlite3Ft
4235c 73 33 44 65 66 65 72 54 6f 6b 65 6e 28 29 0a 2a  s3DeferToken().*
4235d 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 65 61 63 68  * called on each
4235e 20 74 6f 6b 65 6e 20 74 6f 20 64 65 66 65 72 2e   token to defer.
4235f 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 53   Otherwise, an S
42360 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
42361 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e   is.** returned.
42362 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
42363 74 73 33 45 76 61 6c 53 65 6c 65 63 74 44 65 66  ts3EvalSelectDef
42364 65 72 72 65 64 28 0a 20 20 46 74 73 33 43 75 72  erred(.  Fts3Cur
42365 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20 20 20  sor *pCsr,      
42366 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53 20           /* FTS 
42367 43 75 72 73 6f 72 20 68 61 6e 64 6c 65 20 2a 2f  Cursor handle */
42368 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 52 6f  .  Fts3Expr *pRo
42369 6f 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ot,             
4236a 20 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 20 74     /* Consider t
4236b 6f 6b 65 6e 73 20 77 69 74 68 20 74 68 69 73 20  okens with this 
4236c 72 6f 6f 74 20 6e 6f 64 65 20 2a 2f 0a 20 20 46  root node */.  F
4236d 74 73 33 54 6f 6b 65 6e 41 6e 64 43 6f 73 74 20  ts3TokenAndCost 
4236e 2a 61 54 43 2c 20 20 20 20 20 20 20 20 20 20 2f  *aTC,          /
4236f 2a 20 41 72 72 61 79 20 6f 66 20 65 78 70 72 65  * Array of expre
42370 73 73 69 6f 6e 20 74 6f 6b 65 6e 73 20 61 6e 64  ssion tokens and
42371 20 63 6f 73 74 73 20 2a 2f 0a 20 20 69 6e 74 20   costs */.  int 
42372 6e 54 43 20 20 20 20 20 20 20 20 20 20 20 20 20  nTC             
42373 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
42374 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
42375 20 69 6e 20 61 54 43 5b 5d 20 2a 2f 0a 29 7b 0a   in aTC[] */.){.
42376 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61    Fts3Table *pTa
42377 62 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a  b = (Fts3Table *
42378 29 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61  )pCsr->base.pVta
42379 62 3b 0a 20 20 69 6e 74 20 6e 44 6f 63 53 69 7a  b;.  int nDocSiz
4237a 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
4237b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
4237c 66 20 70 61 67 65 73 20 70 65 72 20 64 6f 63 20  f pages per doc 
4237d 6c 6f 61 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20  loaded */.  int 
4237e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
4237f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
42380 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
42381 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20  int ii;         
42382 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42383 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
42384 61 62 6c 65 20 66 6f 72 20 76 61 72 69 6f 75 73  able for various
42385 20 70 75 72 70 6f 73 65 73 20 2a 2f 0a 20 20 69   purposes */.  i
42386 6e 74 20 6e 4f 76 66 6c 20 3d 20 30 3b 20 20 20  nt nOvfl = 0;   
42387 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
42388 2a 20 54 6f 74 61 6c 20 6f 76 65 72 66 6c 6f 77  * Total overflow
42389 20 70 61 67 65 73 20 75 73 65 64 20 62 79 20 64   pages used by d
4238a 6f 63 6c 69 73 74 73 20 2a 2f 0a 20 20 69 6e 74  oclists */.  int
4238b 20 6e 54 6f 6b 65 6e 20 3d 20 30 3b 20 20 20 20   nToken = 0;    
4238c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4238d 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  Total number of 
4238e 74 6f 6b 65 6e 73 20 69 6e 20 63 6c 75 73 74 65  tokens in cluste
4238f 72 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 4d 69 6e  r */..  int nMin
42390 45 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Est = 0;        
42391 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d          /* The m
42392 69 6e 69 6d 75 6d 20 63 6f 75 6e 74 20 66 6f 72  inimum count for
42393 20 61 6e 79 20 70 68 72 61 73 65 20 73 6f 20 66   any phrase so f
42394 61 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f  ar. */.  int nLo
42395 61 64 34 20 3d 20 31 3b 20 20 20 20 20 20 20 20  ad4 = 1;        
42396 20 20 20 20 20 20 20 20 20 2f 2a 20 28 50 68 72           /* (Phr
42397 61 73 65 73 20 74 68 61 74 20 77 69 6c 6c 20 62  ases that will b
42398 65 20 6c 6f 61 64 65 64 29 5e 34 2e 20 2a 2f 0a  e loaded)^4. */.
42399 0a 20 20 2f 2a 20 54 6f 6b 65 6e 73 20 61 72 65  .  /* Tokens are
4239a 20 6e 65 76 65 72 20 64 65 66 65 72 72 65 64 20   never deferred 
4239b 66 6f 72 20 46 54 53 20 74 61 62 6c 65 73 20 63  for FTS tables c
4239c 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68 65  reated using the
4239d 20 63 6f 6e 74 65 6e 74 3d 78 78 78 0a 20 20 2a   content=xxx.  *
4239e 2a 20 6f 70 74 69 6f 6e 2e 20 54 68 65 20 72 65  * option. The re
4239f 61 73 6f 6e 20 62 65 69 6e 67 20 74 68 61 74 20  ason being that 
423a0 69 74 20 69 73 20 6e 6f 74 20 67 75 61 72 61 6e  it is not guaran
423a1 74 65 65 64 20 74 68 61 74 20 74 68 65 20 63 6f  teed that the co
423a2 6e 74 65 6e 74 0a 20 20 2a 2a 20 74 61 62 6c 65  ntent.  ** table
423a3 20 61 63 74 75 61 6c 6c 79 20 63 6f 6e 74 61 69   actually contai
423a4 6e 73 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  ns the same data
423a5 20 61 73 20 74 68 65 20 69 6e 64 65 78 2e 20 54   as the index. T
423a6 6f 20 70 72 65 76 65 6e 74 20 74 68 69 73 20 66  o prevent this f
423a7 72 6f 6d 0a 20 20 2a 2a 20 63 61 75 73 69 6e 67  rom.  ** causing
423a8 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2c 20 74   any problems, t
423a9 68 65 20 64 65 66 65 72 72 65 64 20 74 6f 6b 65  he deferred toke
423aa 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  n optimization i
423ab 73 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 20 20 2a  s completely.  *
423ac 2a 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 63  * disabled for c
423ad 6f 6e 74 65 6e 74 3d 78 78 78 20 74 61 62 6c 65  ontent=xxx table
423ae 73 2e 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62  s. */.  if( pTab
423af 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c 20 29 7b  ->zContentTbl ){
423b0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
423b1 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  TE_OK;.  }..  /*
423b2 20 43 6f 75 6e 74 20 74 68 65 20 74 6f 6b 65 6e   Count the token
423b3 73 20 69 6e 20 74 68 69 73 20 41 4e 44 2f 4e 45  s in this AND/NE
423b4 41 52 20 63 6c 75 73 74 65 72 2e 20 49 66 20 6e  AR cluster. If n
423b5 6f 6e 65 20 6f 66 20 74 68 65 20 64 6f 63 6c 69  one of the docli
423b6 73 74 73 0a 20 20 2a 2a 20 61 73 73 6f 63 69 61  sts.  ** associa
423b7 74 65 64 20 77 69 74 68 20 74 68 65 20 74 6f 6b  ted with the tok
423b8 65 6e 73 20 73 70 69 6c 6c 20 6f 6e 74 6f 20 6f  ens spill onto o
423b9 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 6f  verflow pages, o
423ba 72 20 69 66 20 74 68 65 72 65 20 69 73 0a 20 20  r if there is.  
423bb 2a 2a 20 6f 6e 6c 79 20 31 20 74 6f 6b 65 6e 2c  ** only 1 token,
423bc 20 65 78 69 74 20 65 61 72 6c 79 2e 20 4e 6f 20   exit early. No 
423bd 74 6f 6b 65 6e 73 20 74 6f 20 64 65 66 65 72 20  tokens to defer 
423be 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 2a 2f  in this case. */
423bf 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c  .  for(ii=0; ii<
423c0 6e 54 43 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  nTC; ii++){.    
423c1 69 66 28 20 61 54 43 5b 69 69 5d 2e 70 52 6f 6f  if( aTC[ii].pRoo
423c2 74 3d 3d 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20  t==pRoot ){.    
423c3 20 20 6e 4f 76 66 6c 20 2b 3d 20 61 54 43 5b 69    nOvfl += aTC[i
423c4 69 5d 2e 6e 4f 76 66 6c 3b 0a 20 20 20 20 20 20  i].nOvfl;.      
423c5 6e 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20 7d 0a  nToken++;.    }.
423c6 20 20 7d 0a 20 20 69 66 28 20 6e 4f 76 66 6c 3d    }.  if( nOvfl=
423c7 3d 30 20 7c 7c 20 6e 54 6f 6b 65 6e 3c 32 20 29  =0 || nToken<2 )
423c8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
423c9 4b 3b 0a 0a 20 20 2f 2a 20 4f 62 74 61 69 6e 20  K;..  /* Obtain 
423ca 74 68 65 20 61 76 65 72 61 67 65 20 64 6f 63 73  the average docs
423cb 69 7a 65 20 28 69 6e 20 70 61 67 65 73 29 2e 20  ize (in pages). 
423cc 2a 2f 0a 20 20 72 63 20 3d 20 66 74 73 33 45 76  */.  rc = fts3Ev
423cd 61 6c 41 76 65 72 61 67 65 44 6f 63 73 69 7a 65  alAverageDocsize
423ce 28 70 43 73 72 2c 20 26 6e 44 6f 63 53 69 7a 65  (pCsr, &nDocSize
423cf 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  );.  assert( rc!
423d0 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 44  =SQLITE_OK || nD
423d1 6f 63 53 69 7a 65 3e 30 20 29 3b 0a 0a 0a 20 20  ocSize>0 );...  
423d2 2f 2a 20 49 74 65 72 61 74 65 20 74 68 72 6f 75  /* Iterate throu
423d3 67 68 20 61 6c 6c 20 74 6f 6b 65 6e 73 20 69 6e  gh all tokens in
423d4 20 74 68 69 73 20 41 4e 44 2f 4e 45 41 52 20 63   this AND/NEAR c
423d5 6c 75 73 74 65 72 2c 20 69 6e 20 61 73 63 65 6e  luster, in ascen
423d6 64 69 6e 67 20 6f 72 64 65 72 20 0a 20 20 2a 2a  ding order .  **
423d7 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   of the number o
423d8 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  f overflow pages
423d9 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 6c 6f   that will be lo
423da 61 64 65 64 20 62 79 20 74 68 65 20 70 61 67 65  aded by the page
423db 72 20 6c 61 79 65 72 20 0a 20 20 2a 2a 20 74 6f  r layer .  ** to
423dc 20 72 65 74 72 69 65 76 65 20 74 68 65 20 65 6e   retrieve the en
423dd 74 69 72 65 20 64 6f 63 6c 69 73 74 20 66 6f 72  tire doclist for
423de 20 74 68 65 20 74 6f 6b 65 6e 20 66 72 6f 6d 20   the token from 
423df 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e  the full-text in
423e0 64 65 78 2e 0a 20 20 2a 2a 20 4c 6f 61 64 20 74  dex..  ** Load t
423e1 68 65 20 64 6f 63 6c 69 73 74 73 20 66 6f 72 20  he doclists for 
423e2 74 6f 6b 65 6e 73 20 74 68 61 74 20 61 72 65 20  tokens that are 
423e3 65 69 74 68 65 72 3a 0a 20 20 2a 2a 0a 20 20 2a  either:.  **.  *
423e4 2a 20 20 20 61 2e 20 54 68 65 20 63 68 65 61 70  *   a. The cheap
423e5 65 73 74 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65  est token in the
423e6 20 65 6e 74 69 72 65 20 71 75 65 72 79 20 28 69   entire query (i
423e7 2e 65 2e 20 74 68 65 20 6f 6e 65 20 76 69 73 69  .e. the one visi
423e8 74 65 64 20 62 79 20 74 68 65 0a 20 20 2a 2a 20  ted by the.  ** 
423e9 20 20 20 20 20 66 69 72 73 74 20 69 74 65 72 61       first itera
423ea 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f  tion of this loo
423eb 70 29 2c 20 6f 72 0a 20 20 2a 2a 0a 20 20 2a 2a  p), or.  **.  **
423ec 20 20 20 62 2e 20 50 61 72 74 20 6f 66 20 61 20     b. Part of a 
423ed 6d 75 6c 74 69 2d 74 6f 6b 65 6e 20 70 68 72 61  multi-token phra
423ee 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 66  se..  **.  ** Af
423ef 74 65 72 20 65 61 63 68 20 74 6f 6b 65 6e 20 64  ter each token d
423f0 6f 63 6c 69 73 74 20 69 73 20 6c 6f 61 64 65 64  oclist is loaded
423f1 2c 20 6d 65 72 67 65 20 69 74 20 77 69 74 68 20  , merge it with 
423f2 74 68 65 20 6f 74 68 65 72 73 20 66 72 6f 6d 20  the others from 
423f3 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20 70 68  the.  ** same ph
423f4 72 61 73 65 20 61 6e 64 20 63 6f 75 6e 74 20 74  rase and count t
423f5 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 6f 63  he number of doc
423f6 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 20  uments that the 
423f7 6d 65 72 67 65 64 20 64 6f 63 6c 69 73 74 0a 20  merged doclist. 
423f8 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 2e 20 53 65   ** contains. Se
423f9 74 20 76 61 72 69 61 62 6c 65 20 22 6e 4d 69 6e  t variable "nMin
423fa 45 73 74 22 20 74 6f 20 74 68 65 20 73 6d 61 6c  Est" to the smal
423fb 6c 65 73 74 20 6e 75 6d 62 65 72 20 6f 66 20 64  lest number of d
423fc 6f 63 75 6d 65 6e 74 73 20 69 6e 20 0a 20 20 2a  ocuments in .  *
423fd 2a 20 61 6e 79 20 70 68 72 61 73 65 20 64 6f 63  * any phrase doc
423fe 6c 69 73 74 20 66 6f 72 20 77 68 69 63 68 20 31  list for which 1
423ff 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e 20 64   or more token d
42400 6f 63 6c 69 73 74 73 20 68 61 76 65 20 62 65 65  oclists have bee
42401 6e 20 6c 6f 61 64 65 64 2e 0a 20 20 2a 2a 20 4c  n loaded..  ** L
42402 65 74 20 6e 4f 74 68 65 72 20 62 65 20 74 68 65  et nOther be the
42403 20 6e 75 6d 62 65 72 20 6f 66 20 6f 74 68 65 72   number of other
42404 20 70 68 72 61 73 65 73 20 66 6f 72 20 77 68 69   phrases for whi
42405 63 68 20 69 74 20 69 73 20 63 65 72 74 61 69 6e  ch it is certain
42406 20 74 68 61 74 0a 20 20 2a 2a 20 6f 6e 65 20 6f   that.  ** one o
42407 72 20 6d 6f 72 65 20 74 6f 6b 65 6e 73 20 77 69  r more tokens wi
42408 6c 6c 20 6e 6f 74 20 62 65 20 64 65 66 65 72 72  ll not be deferr
42409 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ed..  **.  ** Th
4240a 65 6e 2c 20 66 6f 72 20 65 61 63 68 20 74 6f 6b  en, for each tok
4240b 65 6e 2c 20 64 65 66 65 72 20 69 74 20 69 66 20  en, defer it if 
4240c 6c 6f 61 64 69 6e 67 20 74 68 65 20 64 6f 63 6c  loading the docl
4240d 69 73 74 20 77 6f 75 6c 64 20 72 65 73 75 6c 74  ist would result
4240e 20 69 6e 0a 20 20 2a 2a 20 6c 6f 61 64 69 6e 67   in.  ** loading
4240f 20 4e 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66   N or more overf
42410 6c 6f 77 20 70 61 67 65 73 20 69 6e 74 6f 20 6d  low pages into m
42411 65 6d 6f 72 79 2c 20 77 68 65 72 65 20 4e 20 69  emory, where N i
42412 73 20 63 6f 6d 70 75 74 65 64 20 61 73 3a 0a 20  s computed as:. 
42413 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 28 6e 4d 69   **.  **    (nMi
42414 6e 45 73 74 20 2b 20 34 5e 6e 4f 74 68 65 72 20  nEst + 4^nOther 
42415 2d 20 31 29 20 2f 20 28 34 5e 6e 4f 74 68 65 72  - 1) / (4^nOther
42416 29 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d  ).  */.  for(ii=
42417 30 3b 20 69 69 3c 6e 54 6f 6b 65 6e 20 26 26 20  0; ii<nToken && 
42418 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69  rc==SQLITE_OK; i
42419 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 69 54  i++){.    int iT
4241a 43 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  C;              
4241b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
4241c 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75  to iterate throu
4241d 67 68 20 61 54 43 5b 5d 20 61 72 72 61 79 2e 20  gh aTC[] array. 
4241e 2a 2f 0a 20 20 20 20 46 74 73 33 54 6f 6b 65 6e  */.    Fts3Token
4241f 41 6e 64 43 6f 73 74 20 2a 70 54 43 20 3d 20 30  AndCost *pTC = 0
42420 3b 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 63  ;    /* Set to c
42421 68 65 61 70 65 73 74 20 72 65 6d 61 69 6e 69 6e  heapest remainin
42422 67 20 74 6f 6b 65 6e 2e 20 2a 2f 0a 0a 20 20 20  g token. */..   
42423 20 2f 2a 20 53 65 74 20 70 54 43 20 74 6f 20 70   /* Set pTC to p
42424 6f 69 6e 74 20 74 6f 20 74 68 65 20 63 68 65 61  oint to the chea
42425 70 65 73 74 20 72 65 6d 61 69 6e 69 6e 67 20 74  pest remaining t
42426 6f 6b 65 6e 2e 20 2a 2f 0a 20 20 20 20 66 6f 72  oken. */.    for
42427 28 69 54 43 3d 30 3b 20 69 54 43 3c 6e 54 43 3b  (iTC=0; iTC<nTC;
42428 20 69 54 43 2b 2b 29 7b 0a 20 20 20 20 20 20 69   iTC++){.      i
42429 66 28 20 61 54 43 5b 69 54 43 5d 2e 70 54 6f 6b  f( aTC[iTC].pTok
4242a 65 6e 20 26 26 20 61 54 43 5b 69 54 43 5d 2e 70  en && aTC[iTC].p
4242b 52 6f 6f 74 3d 3d 70 52 6f 6f 74 20 0a 20 20 20  Root==pRoot .   
4242c 20 20 20 20 26 26 20 28 21 70 54 43 20 7c 7c 20      && (!pTC || 
4242d 61 54 43 5b 69 54 43 5d 2e 6e 4f 76 66 6c 3c 70  aTC[iTC].nOvfl<p
4242e 54 43 2d 3e 6e 4f 76 66 6c 29 20 0a 20 20 20 20  TC->nOvfl) .    
4242f 20 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 43    ){.        pTC
42430 20 3d 20 26 61 54 43 5b 69 54 43 5d 3b 0a 20 20   = &aTC[iTC];.  
42431 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
42432 61 73 73 65 72 74 28 20 70 54 43 20 29 3b 0a 0a  assert( pTC );..
42433 20 20 20 20 69 66 28 20 69 69 20 26 26 20 70 54      if( ii && pT
42434 43 2d 3e 6e 4f 76 66 6c 3e 3d 28 28 6e 4d 69 6e  C->nOvfl>=((nMin
42435 45 73 74 2b 28 6e 4c 6f 61 64 34 2f 34 29 2d 31  Est+(nLoad4/4)-1
42436 29 2f 28 6e 4c 6f 61 64 34 2f 34 29 29 2a 6e 44  )/(nLoad4/4))*nD
42437 6f 63 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  ocSize ){.      
42438 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  /* The number of
42439 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20   overflow pages 
4243a 74 6f 20 6c 6f 61 64 20 66 6f 72 20 74 68 69 73  to load for this
4243b 20 28 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20   (and therefore 
4243c 61 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 73 75 62  all.      ** sub
4243d 73 65 71 75 65 6e 74 29 20 74 6f 6b 65 6e 73 20  sequent) tokens 
4243e 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
4243f 74 68 65 20 65 73 74 69 6d 61 74 65 64 20 6e 75  the estimated nu
42440 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 0a 20  mber of pages . 
42441 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77 69 6c       ** that wil
42442 6c 20 62 65 20 6c 6f 61 64 65 64 20 69 66 20 61  l be loaded if a
42443 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 74 6f  ll subsequent to
42444 6b 65 6e 73 20 61 72 65 20 64 65 66 65 72 72 65  kens are deferre
42445 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
42446 20 20 46 74 73 33 50 68 72 61 73 65 54 6f 6b 65    Fts3PhraseToke
42447 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 70 54 43 2d  n *pToken = pTC-
42448 3e 70 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 72  >pToken;.      r
42449 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 44  c = sqlite3Fts3D
4244a 65 66 65 72 54 6f 6b 65 6e 28 70 43 73 72 2c 20  eferToken(pCsr, 
4244b 70 54 6f 6b 65 6e 2c 20 70 54 43 2d 3e 69 43 6f  pToken, pTC->iCo
4244c 6c 29 3b 0a 20 20 20 20 20 20 66 74 73 33 53 65  l);.      fts3Se
4244d 67 52 65 61 64 65 72 43 75 72 73 6f 72 46 72 65  gReaderCursorFre
4244e 65 28 70 54 6f 6b 65 6e 2d 3e 70 53 65 67 63 73  e(pToken->pSegcs
4244f 72 29 3b 0a 20 20 20 20 20 20 70 54 6f 6b 65 6e  r);.      pToken
42450 2d 3e 70 53 65 67 63 73 72 20 3d 20 30 3b 0a 20  ->pSegcsr = 0;. 
42451 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
42452 2f 2a 20 53 65 74 20 6e 4c 6f 61 64 34 20 74 6f  /* Set nLoad4 to
42453 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 28 34   the value of (4
42454 5e 6e 4f 74 68 65 72 29 20 66 6f 72 20 74 68 65  ^nOther) for the
42455 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20   next iteration 
42456 6f 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  of the.      ** 
42457 66 6f 72 2d 6c 6f 6f 70 2e 20 45 78 63 65 70 74  for-loop. Except
42458 2c 20 6c 69 6d 69 74 20 74 68 65 20 76 61 6c 75  , limit the valu
42459 65 20 74 6f 20 32 5e 32 34 20 74 6f 20 70 72 65  e to 2^24 to pre
4245a 76 65 6e 74 20 69 74 20 66 72 6f 6d 20 0a 20 20  vent it from .  
4245b 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 69      ** overflowi
4245c 6e 67 20 74 68 65 20 33 32 2d 62 69 74 20 69 6e  ng the 32-bit in
4245d 74 65 67 65 72 20 69 74 20 69 73 20 73 74 6f 72  teger it is stor
4245e 65 64 20 69 6e 2e 20 2a 2f 0a 20 20 20 20 20 20  ed in. */.      
4245f 69 66 28 20 69 69 3c 31 32 20 29 20 6e 4c 6f 61  if( ii<12 ) nLoa
42460 64 34 20 3d 20 6e 4c 6f 61 64 34 2a 34 3b 0a 0a  d4 = nLoad4*4;..
42461 20 20 20 20 20 20 69 66 28 20 69 69 3d 3d 30 20        if( ii==0 
42462 7c 7c 20 70 54 43 2d 3e 70 50 68 72 61 73 65 2d  || pTC->pPhrase-
42463 3e 6e 54 6f 6b 65 6e 3e 31 20 29 7b 0a 20 20 20  >nToken>1 ){.   
42464 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74       /* Either t
42465 68 69 73 20 69 73 20 74 68 65 20 63 68 65 61 70  his is the cheap
42466 65 73 74 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65  est token in the
42467 20 65 6e 74 69 72 65 20 71 75 65 72 79 2c 20 6f   entire query, o
42468 72 20 69 74 20 69 73 0a 20 20 20 20 20 20 20 20  r it is.        
42469 2a 2a 20 70 61 72 74 20 6f 66 20 61 20 6d 75 6c  ** part of a mul
4246a 74 69 2d 74 6f 6b 65 6e 20 70 68 72 61 73 65 2e  ti-token phrase.
4246b 20 45 69 74 68 65 72 20 77 61 79 2c 20 74 68 65   Either way, the
4246c 20 65 6e 74 69 72 65 20 64 6f 63 6c 69 73 74 20   entire doclist 
4246d 77 69 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20  will.        ** 
4246e 28 65 76 65 6e 74 75 61 6c 6c 79 29 20 62 65 20  (eventually) be 
4246f 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f  loaded into memo
42470 72 79 2e 20 49 74 20 6d 61 79 20 61 73 20 77 65  ry. It may as we
42471 6c 6c 20 62 65 20 6e 6f 77 2e 20 2a 2f 0a 20 20  ll be now. */.  
42472 20 20 20 20 20 20 46 74 73 33 50 68 72 61 73 65        Fts3Phrase
42473 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20  Token *pToken = 
42474 70 54 43 2d 3e 70 54 6f 6b 65 6e 3b 0a 20 20 20  pTC->pToken;.   
42475 20 20 20 20 20 69 6e 74 20 6e 4c 69 73 74 20 3d       int nList =
42476 20 30 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72   0;.        char
42477 20 2a 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20   *pList = 0;.   
42478 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 54 65       rc = fts3Te
42479 72 6d 53 65 6c 65 63 74 28 70 54 61 62 2c 20 70  rmSelect(pTab, p
4247a 54 6f 6b 65 6e 2c 20 70 54 43 2d 3e 69 43 6f 6c  Token, pTC->iCol
4247b 2c 20 26 6e 4c 69 73 74 2c 20 26 70 4c 69 73 74  , &nList, &pList
4247c 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
4247d 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
4247e 20 7c 7c 20 70 4c 69 73 74 3d 3d 30 20 29 3b 0a   || pList==0 );.
4247f 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
42480 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
42481 20 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 75 6e         int nCoun
42482 74 3b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  t;.          fts
42483 33 45 76 61 6c 50 68 72 61 73 65 4d 65 72 67 65  3EvalPhraseMerge
42484 54 6f 6b 65 6e 28 70 54 61 62 2c 20 70 54 43 2d  Token(pTab, pTC-
42485 3e 70 50 68 72 61 73 65 2c 20 70 54 43 2d 3e 69  >pPhrase, pTC->i
42486 54 6f 6b 65 6e 2c 70 4c 69 73 74 2c 6e 4c 69 73  Token,pList,nLis
42487 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 43  t);.          nC
42488 6f 75 6e 74 20 3d 20 66 74 73 33 44 6f 63 6c 69  ount = fts3Docli
42489 73 74 43 6f 75 6e 74 44 6f 63 69 64 73 28 0a 20  stCountDocids(. 
4248a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54 43               pTC
4248b 2d 3e 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69  ->pPhrase->docli
4248c 73 74 2e 61 41 6c 6c 2c 20 70 54 43 2d 3e 70 50  st.aAll, pTC->pP
4248d 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 6e  hrase->doclist.n
4248e 41 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 29 3b  All.          );
4248f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69  .          if( i
42490 69 3d 3d 30 20 7c 7c 20 6e 43 6f 75 6e 74 3c 6e  i==0 || nCount<n
42491 4d 69 6e 45 73 74 20 29 20 6e 4d 69 6e 45 73 74  MinEst ) nMinEst
42492 20 3d 20 6e 43 6f 75 6e 74 3b 0a 20 20 20 20 20   = nCount;.     
42493 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
42494 20 7d 0a 20 20 20 20 70 54 43 2d 3e 70 54 6f 6b   }.    pTC->pTok
42495 65 6e 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72  en = 0;.  }..  r
42496 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
42497 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
42498 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   is called from 
42499 77 69 74 68 69 6e 20 74 68 65 20 78 46 69 6c 74  within the xFilt
4249a 65 72 20 6d 65 74 68 6f 64 2e 20 49 74 20 69 6e  er method. It in
4249b 69 74 69 61 6c 69 7a 65 73 0a 2a 2a 20 74 68 65  itializes.** the
4249c 20 66 75 6c 6c 2d 74 65 78 74 20 71 75 65 72 79   full-text query
4249d 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65   currently store
4249e 64 20 69 6e 20 70 43 73 72 2d 3e 70 45 78 70 72  d in pCsr->pExpr
4249f 2e 20 54 6f 20 69 74 65 72 61 74 65 20 74 68 72  . To iterate thr
424a0 6f 75 67 68 0a 2a 2a 20 74 68 65 20 72 65 73 75  ough.** the resu
424a1 6c 74 73 20 6f 66 20 61 20 71 75 65 72 79 2c 20  lts of a query, 
424a2 74 68 65 20 63 61 6c 6c 65 72 20 64 6f 65 73 3a  the caller does:
424a3 0a 2a 2a 0a 2a 2a 20 20 20 20 66 74 73 33 45 76  .**.**    fts3Ev
424a4 61 6c 53 74 61 72 74 28 70 43 73 72 29 3b 0a 2a  alStart(pCsr);.*
424a5 2a 20 20 20 20 77 68 69 6c 65 28 20 31 20 29 7b  *    while( 1 ){
424a6 0a 2a 2a 20 20 20 20 20 20 66 74 73 33 45 76 61  .**      fts3Eva
424a7 6c 4e 65 78 74 28 70 43 73 72 29 3b 0a 2a 2a 20  lNext(pCsr);.** 
424a8 20 20 20 20 20 69 66 28 20 70 43 73 72 2d 3e 62       if( pCsr->b
424a9 45 6f 66 20 29 20 62 72 65 61 6b 3b 0a 2a 2a 20  Eof ) break;.** 
424aa 20 20 20 20 20 2e 2e 2e 20 72 65 74 75 72 6e 20       ... return 
424ab 72 6f 77 20 70 43 73 72 2d 3e 69 50 72 65 76 49  row pCsr->iPrevI
424ac 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20  d to the caller 
424ad 2e 2e 2e 0a 2a 2a 20 20 20 20 7d 0a 2a 2f 0a 73  ....**    }.*/.s
424ae 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 76  tatic int fts3Ev
424af 61 6c 53 74 61 72 74 28 46 74 73 33 43 75 72 73  alStart(Fts3Curs
424b0 6f 72 20 2a 70 43 73 72 29 7b 0a 20 20 46 74 73  or *pCsr){.  Fts
424b1 33 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 28  3Table *pTab = (
424b2 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43 73 72  Fts3Table *)pCsr
424b3 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b 0a 20 20  ->base.pVtab;.  
424b4 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
424b5 4f 4b 3b 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e  OK;.  int nToken
424b6 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 4f 72 20   = 0;.  int nOr 
424b7 3d 20 30 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  = 0;..  /* Alloc
424b8 61 74 65 20 61 20 4d 75 6c 74 69 53 65 67 52 65  ate a MultiSegRe
424b9 61 64 65 72 20 66 6f 72 20 65 61 63 68 20 74 6f  ader for each to
424ba 6b 65 6e 20 69 6e 20 74 68 65 20 65 78 70 72 65  ken in the expre
424bb 73 73 69 6f 6e 2e 20 2a 2f 0a 20 20 66 74 73 33  ssion. */.  fts3
424bc 45 76 61 6c 41 6c 6c 6f 63 61 74 65 52 65 61 64  EvalAllocateRead
424bd 65 72 73 28 70 43 73 72 2c 20 70 43 73 72 2d 3e  ers(pCsr, pCsr->
424be 70 45 78 70 72 2c 20 26 6e 54 6f 6b 65 6e 2c 20  pExpr, &nToken, 
424bf 26 6e 4f 72 2c 20 26 72 63 29 3b 0a 0a 20 20 2f  &nOr, &rc);..  /
424c0 2a 20 44 65 74 65 72 6d 69 6e 65 20 77 68 69 63  * Determine whic
424c1 68 2c 20 69 66 20 61 6e 79 2c 20 74 6f 6b 65 6e  h, if any, token
424c2 73 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73  s in the express
424c3 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 64 65  ion should be de
424c4 66 65 72 72 65 64 2e 20 2a 2f 0a 23 69 66 6e 64  ferred. */.#ifnd
424c5 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c  ef SQLITE_DISABL
424c6 45 5f 46 54 53 34 5f 44 45 46 45 52 52 45 44 0a  E_FTS4_DEFERRED.
424c7 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
424c8 5f 4f 4b 20 26 26 20 6e 54 6f 6b 65 6e 3e 31 20  _OK && nToken>1 
424c9 26 26 20 70 54 61 62 2d 3e 62 46 74 73 34 20 29  && pTab->bFts4 )
424ca 7b 0a 20 20 20 20 46 74 73 33 54 6f 6b 65 6e 41  {.    Fts3TokenA
424cb 6e 64 43 6f 73 74 20 2a 61 54 43 3b 0a 20 20 20  ndCost *aTC;.   
424cc 20 46 74 73 33 45 78 70 72 20 2a 2a 61 70 4f 72   Fts3Expr **apOr
424cd 3b 0a 20 20 20 20 61 54 43 20 3d 20 28 46 74 73  ;.    aTC = (Fts
424ce 33 54 6f 6b 65 6e 41 6e 64 43 6f 73 74 20 2a 29  3TokenAndCost *)
424cf 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 0a  sqlite3_malloc(.
424d0 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 46          sizeof(F
424d1 74 73 33 54 6f 6b 65 6e 41 6e 64 43 6f 73 74 29  ts3TokenAndCost)
424d2 20 2a 20 6e 54 6f 6b 65 6e 0a 20 20 20 20 20 20   * nToken.      
424d3 2b 20 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70  + sizeof(Fts3Exp
424d4 72 20 2a 29 20 2a 20 6e 4f 72 20 2a 20 32 0a 20  r *) * nOr * 2. 
424d5 20 20 20 29 3b 0a 20 20 20 20 61 70 4f 72 20 3d     );.    apOr =
424d6 20 28 46 74 73 33 45 78 70 72 20 2a 2a 29 26 61   (Fts3Expr **)&a
424d7 54 43 5b 6e 54 6f 6b 65 6e 5d 3b 0a 0a 20 20 20  TC[nToken];..   
424d8 20 69 66 28 20 21 61 54 43 20 29 7b 0a 20 20 20   if( !aTC ){.   
424d9 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
424da 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  OMEM;.    }else{
424db 0a 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20  .      int ii;. 
424dc 20 20 20 20 20 46 74 73 33 54 6f 6b 65 6e 41 6e       Fts3TokenAn
424dd 64 43 6f 73 74 20 2a 70 54 43 20 3d 20 61 54 43  dCost *pTC = aTC
424de 3b 0a 20 20 20 20 20 20 46 74 73 33 45 78 70 72  ;.      Fts3Expr
424df 20 2a 2a 70 70 4f 72 20 3d 20 61 70 4f 72 3b 0a   **ppOr = apOr;.
424e0 0a 20 20 20 20 20 20 66 74 73 33 45 76 61 6c 54  .      fts3EvalT
424e1 6f 6b 65 6e 43 6f 73 74 73 28 70 43 73 72 2c 20  okenCosts(pCsr, 
424e2 30 2c 20 70 43 73 72 2d 3e 70 45 78 70 72 2c 20  0, pCsr->pExpr, 
424e3 26 70 54 43 2c 20 26 70 70 4f 72 2c 20 26 72 63  &pTC, &ppOr, &rc
424e4 29 3b 0a 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20  );.      nToken 
424e5 3d 20 28 69 6e 74 29 28 70 54 43 2d 61 54 43 29  = (int)(pTC-aTC)
424e6 3b 0a 20 20 20 20 20 20 6e 4f 72 20 3d 20 28 69  ;.      nOr = (i
424e7 6e 74 29 28 70 70 4f 72 2d 61 70 4f 72 29 3b 0a  nt)(ppOr-apOr);.
424e8 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
424e9 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
424ea 20 20 20 20 72 63 20 3d 20 66 74 73 33 45 76 61      rc = fts3Eva
424eb 6c 53 65 6c 65 63 74 44 65 66 65 72 72 65 64 28  lSelectDeferred(
424ec 70 43 73 72 2c 20 30 2c 20 61 54 43 2c 20 6e 54  pCsr, 0, aTC, nT
424ed 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 66  oken);.        f
424ee 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  or(ii=0; rc==SQL
424ef 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 4f 72  ITE_OK && ii<nOr
424f0 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ; ii++){.       
424f1 20 20 20 72 63 20 3d 20 66 74 73 33 45 76 61 6c     rc = fts3Eval
424f2 53 65 6c 65 63 74 44 65 66 65 72 72 65 64 28 70  SelectDeferred(p
424f3 43 73 72 2c 20 61 70 4f 72 5b 69 69 5d 2c 20 61  Csr, apOr[ii], a
424f4 54 43 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20  TC, nToken);.   
424f5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
424f6 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
424f7 65 65 28 61 54 43 29 3b 0a 20 20 20 20 7d 0a 20  ee(aTC);.    }. 
424f8 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 66 74 73   }.#endif..  fts
424f9 33 45 76 61 6c 53 74 61 72 74 52 65 61 64 65 72  3EvalStartReader
424fa 73 28 70 43 73 72 2c 20 70 43 73 72 2d 3e 70 45  s(pCsr, pCsr->pE
424fb 78 70 72 2c 20 31 2c 20 26 72 63 29 3b 0a 20 20  xpr, 1, &rc);.  
424fc 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
424fd 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74  .** Invalidate t
424fe 68 65 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74  he current posit
424ff 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20 70 68 72  ion list for phr
42500 61 73 65 20 70 50 68 72 61 73 65 2e 0a 2a 2f 0a  ase pPhrase..*/.
42501 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
42502 45 76 61 6c 49 6e 76 61 6c 69 64 61 74 65 50 6f  EvalInvalidatePo
42503 73 6c 69 73 74 28 46 74 73 33 50 68 72 61 73 65  slist(Fts3Phrase
42504 20 2a 70 50 68 72 61 73 65 29 7b 0a 20 20 69 66   *pPhrase){.  if
42505 28 20 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69  ( pPhrase->docli
42506 73 74 2e 62 46 72 65 65 4c 69 73 74 20 29 7b 0a  st.bFreeList ){.
42507 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
42508 28 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73  (pPhrase->doclis
42509 74 2e 70 4c 69 73 74 29 3b 0a 20 20 7d 0a 20 20  t.pList);.  }.  
4250a 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74  pPhrase->doclist
4250b 2e 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 70 50  .pList = 0;.  pP
4250c 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 6e  hrase->doclist.n
4250d 4c 69 73 74 20 3d 20 30 3b 0a 20 20 70 50 68 72  List = 0;.  pPhr
4250e 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 62 46 72  ase->doclist.bFr
4250f 65 65 4c 69 73 74 20 3d 20 30 3b 0a 7d 0a 0a 2f  eeList = 0;.}../
42510 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
42511 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  on is called to 
42512 65 64 69 74 20 74 68 65 20 70 6f 73 69 74 69 6f  edit the positio
42513 6e 20 6c 69 73 74 20 61 73 73 6f 63 69 61 74 65  n list associate
42514 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 70 68  d with.** the ph
42515 72 61 73 65 20 6f 62 6a 65 63 74 20 70 61 73 73  rase object pass
42516 65 64 20 61 73 20 74 68 65 20 66 69 66 74 68 20  ed as the fifth 
42517 61 72 67 75 6d 65 6e 74 20 61 63 63 6f 72 64 69  argument accordi
42518 6e 67 20 74 6f 20 61 20 4e 45 41 52 0a 2a 2a 20  ng to a NEAR.** 
42519 63 6f 6e 64 69 74 69 6f 6e 2e 20 46 6f 72 20 65  condition. For e
4251a 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  xample:.**.**   
4251b 20 20 61 62 63 20 4e 45 41 52 2f 35 20 22 64 65    abc NEAR/5 "de
4251c 66 20 67 68 69 22 0a 2a 2a 0a 2a 2a 20 50 61 72  f ghi".**.** Par
4251d 61 6d 65 74 65 72 20 6e 4e 65 61 72 20 69 73 20  ameter nNear is 
4251e 70 61 73 73 65 64 20 74 68 65 20 4e 45 41 52 20  passed the NEAR 
4251f 64 69 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  distance of the 
42520 65 78 70 72 65 73 73 69 6f 6e 20 28 35 20 69 6e  expression (5 in
42521 0a 2a 2a 20 74 68 65 20 65 78 61 6d 70 6c 65 20  .** the example 
42522 61 62 6f 76 65 29 2e 20 57 68 65 6e 20 74 68 69  above). When thi
42523 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
42524 6c 6c 65 64 2c 20 2a 70 61 50 6f 73 6c 69 73 74  lled, *paPoslist
42525 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68   points to.** th
42526 65 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 2c  e position list,
42527 20 61 6e 64 20 2a 70 6e 54 6f 6b 65 6e 20 69 73   and *pnToken is
42528 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
42529 68 72 61 73 65 20 74 6f 6b 65 6e 73 20 69 6e 2c  hrase tokens in,
4252a 20 74 68 65 0a 2a 2a 20 70 68 72 61 73 65 20 6f   the.** phrase o
4252b 6e 20 74 68 65 20 6f 74 68 65 72 20 73 69 64 65  n the other side
4252c 20 6f 66 20 74 68 65 20 4e 45 41 52 20 6f 70 65   of the NEAR ope
4252d 72 61 74 6f 72 20 74 6f 20 70 50 68 72 61 73 65  rator to pPhrase
4252e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a  . For example,.*
4252f 2a 20 69 66 20 70 50 68 72 61 73 65 20 72 65 66  * if pPhrase ref
42530 65 72 73 20 74 6f 20 74 68 65 20 22 64 65 66 20  ers to the "def 
42531 67 68 69 22 20 70 68 72 61 73 65 2c 20 74 68 65  ghi" phrase, the
42532 6e 20 2a 70 61 50 6f 73 6c 69 73 74 20 70 6f 69  n *paPoslist poi
42533 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 20 70 6f  nts to.** the po
42534 73 69 74 69 6f 6e 20 6c 69 73 74 20 61 73 73 6f  sition list asso
42535 63 69 61 74 65 64 20 77 69 74 68 20 70 68 72 61  ciated with phra
42536 73 65 20 22 61 62 63 22 2e 0a 2a 2a 0a 2a 2a 20  se "abc"..**.** 
42537 41 6c 6c 20 70 6f 73 69 74 69 6f 6e 73 20 69 6e  All positions in
42538 20 74 68 65 20 70 50 68 72 61 73 65 20 70 6f 73   the pPhrase pos
42539 69 74 69 6f 6e 20 6c 69 73 74 20 74 68 61 74 20  ition list that 
4253a 61 72 65 20 6e 6f 74 20 73 75 66 66 69 63 69 65  are not sufficie
4253b 6e 74 6c 79 0a 2a 2a 20 63 6c 6f 73 65 20 74 6f  ntly.** close to
4253c 20 61 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74   a position in t
4253d 68 65 20 2a 70 61 50 6f 73 6c 69 73 74 20 70 6f  he *paPoslist po
4253e 73 69 74 69 6f 6e 20 6c 69 73 74 20 61 72 65 20  sition list are 
4253f 72 65 6d 6f 76 65 64 2e 20 49 66 20 74 68 69 73  removed. If this
42540 0a 2a 2a 20 6c 65 61 76 65 73 20 30 20 70 6f 73  .** leaves 0 pos
42541 69 74 69 6f 6e 73 2c 20 7a 65 72 6f 20 69 73 20  itions, zero is 
42542 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
42543 69 73 65 2c 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a  ise, non-zero..*
42544 2a 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75  *.** Before retu
42545 72 6e 69 6e 67 2c 20 2a 70 61 50 6f 73 6c 69 73  rning, *paPoslis
42546 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e  t is set to poin
42547 74 20 74 6f 20 74 68 65 20 70 6f 73 69 74 69 6f  t to the positio
42548 6e 20 6c 73 69 74 20 0a 2a 2a 20 61 73 73 6f 63  n lsit .** assoc
42549 69 61 74 65 64 20 77 69 74 68 20 70 50 68 72 61  iated with pPhra
4254a 73 65 2e 20 41 6e 64 20 2a 70 6e 54 6f 6b 65 6e  se. And *pnToken
4254b 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e   is set to the n
4254c 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20  umber of tokens 
4254d 69 6e 0a 2a 2a 20 70 50 68 72 61 73 65 2e 0a 2a  in.** pPhrase..*
4254e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
4254f 33 45 76 61 6c 4e 65 61 72 54 72 69 6d 28 0a 20  3EvalNearTrim(. 
42550 20 69 6e 74 20 6e 4e 65 61 72 2c 20 20 20 20 20   int nNear,     
42551 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42552 20 2f 2a 20 4e 45 41 52 20 64 69 73 74 61 6e 63   /* NEAR distanc
42553 65 2e 20 41 73 20 69 6e 20 22 4e 45 41 52 2f 6e  e. As in "NEAR/n
42554 4e 65 61 72 22 2e 20 2a 2f 0a 20 20 63 68 61 72  Near". */.  char
42555 20 2a 61 54 6d 70 2c 20 20 20 20 20 20 20 20 20   *aTmp,         
42556 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
42557 65 6d 70 6f 72 61 72 79 20 73 70 61 63 65 20 74  emporary space t
42558 6f 20 75 73 65 20 2a 2f 0a 20 20 63 68 61 72 20  o use */.  char 
42559 2a 2a 70 61 50 6f 73 6c 69 73 74 2c 20 20 20 20  **paPoslist,    
4255a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e             /* IN
4255b 2f 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e 20 6c  /OUT: Position l
4255c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  ist */.  int *pn
4255d 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20  Token,          
4255e 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
4255f 55 54 3a 20 54 6f 6b 65 6e 73 20 69 6e 20 70 68  UT: Tokens in ph
42560 72 61 73 65 20 6f 66 20 2a 70 61 50 6f 73 6c 69  rase of *paPosli
42561 73 74 20 2a 2f 0a 20 20 46 74 73 33 50 68 72 61  st */.  Fts3Phra
42562 73 65 20 2a 70 50 68 72 61 73 65 20 20 20 20 20  se *pPhrase     
42563 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
42564 68 72 61 73 65 20 6f 62 6a 65 63 74 20 74 6f 20  hrase object to 
42565 74 72 69 6d 20 74 68 65 20 64 6f 63 6c 69 73 74  trim the doclist
42566 20 6f 66 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20   of */.){.  int 
42567 6e 50 61 72 61 6d 31 20 3d 20 6e 4e 65 61 72 20  nParam1 = nNear 
42568 2b 20 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65  + pPhrase->nToke
42569 6e 3b 0a 20 20 69 6e 74 20 6e 50 61 72 61 6d 32  n;.  int nParam2
4256a 20 3d 20 6e 4e 65 61 72 20 2b 20 2a 70 6e 54 6f   = nNear + *pnTo
4256b 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 4e 65 77 3b  ken;.  int nNew;
4256c 0a 20 20 63 68 61 72 20 2a 70 32 3b 20 0a 20 20  .  char *p2; .  
4256d 63 68 61 72 20 2a 70 4f 75 74 3b 20 0a 20 20 69  char *pOut; .  i
4256e 6e 74 20 72 65 73 3b 0a 0a 20 20 61 73 73 65 72  nt res;..  asser
4256f 74 28 20 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c  t( pPhrase->docl
42570 69 73 74 2e 70 4c 69 73 74 20 29 3b 0a 0a 20 20  ist.pList );..  
42571 70 32 20 3d 20 70 4f 75 74 20 3d 20 70 50 68 72  p2 = pOut = pPhr
42572 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c 69  ase->doclist.pLi
42573 73 74 3b 0a 20 20 72 65 73 20 3d 20 66 74 73 33  st;.  res = fts3
42574 50 6f 73 6c 69 73 74 4e 65 61 72 4d 65 72 67 65  PoslistNearMerge
42575 28 0a 20 20 20 20 26 70 4f 75 74 2c 20 61 54 6d  (.    &pOut, aTm
42576 70 2c 20 6e 50 61 72 61 6d 31 2c 20 6e 50 61 72  p, nParam1, nPar
42577 61 6d 32 2c 20 70 61 50 6f 73 6c 69 73 74 2c 20  am2, paPoslist, 
42578 26 70 32 0a 20 20 29 3b 0a 20 20 69 66 28 20 72  &p2.  );.  if( r
42579 65 73 20 29 7b 0a 20 20 20 20 6e 4e 65 77 20 3d  es ){.    nNew =
4257a 20 28 69 6e 74 29 28 70 4f 75 74 20 2d 20 70 50   (int)(pOut - pP
4257b 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70  hrase->doclist.p
4257c 4c 69 73 74 29 20 2d 20 31 3b 0a 20 20 20 20 61  List) - 1;.    a
4257d 73 73 65 72 74 28 20 70 50 68 72 61 73 65 2d 3e  ssert( pPhrase->
4257e 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74 5b 6e 4e  doclist.pList[nN
4257f 65 77 5d 3d 3d 27 5c 30 27 20 29 3b 0a 20 20 20  ew]=='\0' );.   
42580 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3c 3d 70   assert( nNew<=p
42581 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e  Phrase->doclist.
42582 6e 4c 69 73 74 20 26 26 20 6e 4e 65 77 3e 30 20  nList && nNew>0 
42583 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70  );.    memset(&p
42584 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e  Phrase->doclist.
42585 70 4c 69 73 74 5b 6e 4e 65 77 5d 2c 20 30 2c 20  pList[nNew], 0, 
42586 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74  pPhrase->doclist
42587 2e 6e 4c 69 73 74 20 2d 20 6e 4e 65 77 29 3b 0a  .nList - nNew);.
42588 20 20 20 20 70 50 68 72 61 73 65 2d 3e 64 6f 63      pPhrase->doc
42589 6c 69 73 74 2e 6e 4c 69 73 74 20 3d 20 6e 4e 65  list.nList = nNe
4258a 77 3b 0a 20 20 20 20 2a 70 61 50 6f 73 6c 69 73  w;.    *paPoslis
4258b 74 20 3d 20 70 50 68 72 61 73 65 2d 3e 64 6f 63  t = pPhrase->doc
4258c 6c 69 73 74 2e 70 4c 69 73 74 3b 0a 20 20 20 20  list.pList;.    
4258d 2a 70 6e 54 6f 6b 65 6e 20 3d 20 70 50 68 72 61  *pnToken = pPhra
4258e 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 7d 0a  se->nToken;.  }.
4258f 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d  .  return res;.}
42590 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
42591 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
42592 20 69 66 20 2a 70 52 63 20 69 73 20 6f 74 68 65   if *pRc is othe
42593 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b  r than SQLITE_OK
42594 20 77 68 65 6e 20 69 74 20 69 73 20 63 61 6c 6c   when it is call
42595 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  ed..** Otherwise
42596 2c 20 69 74 20 61 64 76 61 6e 63 65 73 20 74 68  , it advances th
42597 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61 73  e expression pas
42598 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
42599 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a  d argument to.**
4259a 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65   point to the ne
4259b 78 74 20 6d 61 74 63 68 69 6e 67 20 72 6f 77 20  xt matching row 
4259c 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  in the database.
4259d 20 45 78 70 72 65 73 73 69 6f 6e 73 20 69 74 65   Expressions ite
4259e 72 61 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20  rate through.** 
4259f 6d 61 74 63 68 69 6e 67 20 72 6f 77 73 20 69 6e  matching rows in
425a0 20 64 6f 63 69 64 20 6f 72 64 65 72 2e 20 41 73   docid order. As
425a1 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 69 66  cending order if
425a2 20 46 74 73 33 43 75 72 73 6f 72 2e 62 44 65 73   Fts3Cursor.bDes
425a3 63 20 69 73 20 7a 65 72 6f 2c 0a 2a 2a 20 6f 72  c is zero,.** or
425a4 20 64 65 73 63 65 6e 64 69 6e 67 20 69 66 20 69   descending if i
425a5 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a  t is non-zero..*
425a6 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
425a7 20 6f 63 63 75 72 73 2c 20 2a 70 52 63 20 69 73   occurs, *pRc is
425a8 20 73 65 74 20 74 6f 20 61 6e 20 53 51 4c 69 74   set to an SQLit
425a9 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 4f 74  e error code. Ot
425aa 68 65 72 77 69 73 65 2c 20 69 66 0a 2a 2a 20 73  herwise, if.** s
425ab 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 20 66  uccessful, the f
425ac 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c  ollowing variabl
425ad 65 73 20 69 6e 20 70 45 78 70 72 20 61 72 65 20  es in pExpr are 
425ae 73 65 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 46 74 73  set:.**.**   Fts
425af 33 45 78 70 72 2e 62 45 6f 66 20 20 20 20 20 20  3Expr.bEof      
425b0 20 20 20 20 20 20 20 20 20 20 28 6e 6f 6e 2d 7a            (non-z
425b1 65 72 6f 20 69 66 20 45 4f 46 20 2d 20 74 68 65  ero if EOF - the
425b2 72 65 20 69 73 20 6e 6f 20 6e 65 78 74 20 72 6f  re is no next ro
425b3 77 29 0a 2a 2a 20 20 20 46 74 73 33 45 78 70 72  w).**   Fts3Expr
425b4 2e 69 44 6f 63 69 64 20 20 20 20 20 20 20 20 20  .iDocid         
425b5 20 20 20 20 20 28 76 61 6c 69 64 20 69 66 20 62       (valid if b
425b6 45 6f 66 3d 3d 30 2e 20 54 68 65 20 64 6f 63 69  Eof==0. The doci
425b7 64 20 6f 66 20 74 68 65 20 6e 65 78 74 20 72 6f  d of the next ro
425b8 77 29 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  w).**.** If the 
425b9 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66  expression is of
425ba 20 74 79 70 65 20 46 54 53 51 55 45 52 59 5f 50   type FTSQUERY_P
425bb 48 52 41 53 45 2c 20 61 6e 64 20 74 68 65 20 65  HRASE, and the e
425bc 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74  xpression is not
425bd 0a 2a 2a 20 61 74 20 45 4f 46 2c 20 74 68 65 6e  .** at EOF, then
425be 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   the following v
425bf 61 72 69 61 62 6c 65 73 20 61 72 65 20 70 6f 70  ariables are pop
425c0 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ulated with the 
425c1 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 0a 2a 2a  position list.**
425c2 20 66 6f 72 20 74 68 65 20 70 68 72 61 73 65 20   for the phrase 
425c3 66 6f 72 20 74 68 65 20 76 69 73 69 74 65 64 20  for the visited 
425c4 72 6f 77 3a 0a 2a 2a 0a 2a 2a 20 20 20 46 54 73  row:.**.**   FTs
425c5 33 45 78 70 72 2e 70 50 68 72 61 73 65 2d 3e 64  3Expr.pPhrase->d
425c6 6f 63 6c 69 73 74 2e 6e 4c 69 73 74 20 20 20 20  oclist.nList    
425c7 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 70      (length of p
425c8 4c 69 73 74 20 69 6e 20 62 79 74 65 73 29 0a 2a  List in bytes).*
425c9 2a 20 20 20 46 54 73 33 45 78 70 72 2e 70 50 68  *   FTs3Expr.pPh
425ca 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c  rase->doclist.pL
425cb 69 73 74 20 20 20 20 20 20 20 20 28 70 6f 69 6e  ist        (poin
425cc 74 65 72 20 74 6f 20 70 6f 73 69 74 69 6f 6e 20  ter to position 
425cd 6c 69 73 74 29 0a 2a 2a 0a 2a 2a 20 49 74 20 73  list).**.** It s
425ce 61 79 73 20 61 62 6f 76 65 20 74 68 61 74 20 74  ays above that t
425cf 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 64 76  his function adv
425d0 61 6e 63 65 73 20 74 68 65 20 65 78 70 72 65 73  ances the expres
425d1 73 69 6f 6e 20 74 6f 20 74 68 65 20 6e 65 78 74  sion to the next
425d2 0a 2a 2a 20 6d 61 74 63 68 69 6e 67 20 72 6f 77  .** matching row
425d3 2e 20 54 68 69 73 20 69 73 20 75 73 75 61 6c 6c  . This is usuall
425d4 79 20 74 72 75 65 2c 20 62 75 74 20 74 68 65 72  y true, but ther
425d5 65 20 61 72 65 20 74 68 65 20 66 6f 6c 6c 6f 77  e are the follow
425d6 69 6e 67 20 65 78 63 65 70 74 69 6f 6e 73 3a 0a  ing exceptions:.
425d7 2a 2a 0a 2a 2a 20 20 20 31 2e 20 44 65 66 65 72  **.**   1. Defer
425d8 72 65 64 20 74 6f 6b 65 6e 73 20 61 72 65 20 6e  red tokens are n
425d9 6f 74 20 74 61 6b 65 6e 20 69 6e 74 6f 20 61 63  ot taken into ac
425da 63 6f 75 6e 74 2e 20 49 66 20 61 20 70 68 72 61  count. If a phra
425db 73 65 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 20  se consists.**  
425dc 20 20 20 20 65 6e 74 69 72 65 6c 79 20 6f 66 20      entirely of 
425dd 64 65 66 65 72 72 65 64 20 74 6f 6b 65 6e 73 2c  deferred tokens,
425de 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   it is assumed t
425df 6f 20 6d 61 74 63 68 20 65 76 65 72 79 20 72 6f  o match every ro
425e0 77 20 69 6e 0a 2a 2a 20 20 20 20 20 20 74 68 65  w in.**      the
425e1 20 64 62 2e 20 49 6e 20 74 68 69 73 20 63 61 73   db. In this cas
425e2 65 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 2d 6c  e the position-l
425e3 69 73 74 20 69 73 20 6e 6f 74 20 70 6f 70 75 6c  ist is not popul
425e4 61 74 65 64 20 61 74 20 61 6c 6c 2e 20 0a 2a 2a  ated at all. .**
425e5 0a 2a 2a 20 20 20 20 20 20 4f 72 2c 20 69 66 20  .**      Or, if 
425e6 61 20 70 68 72 61 73 65 20 63 6f 6e 74 61 69 6e  a phrase contain
425e7 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 64 65  s one or more de
425e8 66 65 72 72 65 64 20 74 6f 6b 65 6e 73 20 61 6e  ferred tokens an
425e9 64 20 6f 6e 65 20 6f 72 0a 2a 2a 20 20 20 20 20  d one or.**     
425ea 20 6d 6f 72 65 20 6e 6f 6e 2d 64 65 66 65 72 72   more non-deferr
425eb 65 64 20 74 6f 6b 65 6e 73 2c 20 74 68 65 6e 20  ed tokens, then 
425ec 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
425ed 73 20 61 64 76 61 6e 63 65 64 20 74 6f 20 74 68  s advanced to th
425ee 65 20 0a 2a 2a 20 20 20 20 20 20 6e 65 78 74 20  e .**      next 
425ef 70 6f 73 73 69 62 6c 65 20 6d 61 74 63 68 2c 20  possible match, 
425f0 63 6f 6e 73 69 64 65 72 69 6e 67 20 6f 6e 6c 79  considering only
425f1 20 6e 6f 6e 2d 64 65 66 65 72 72 65 64 20 74 6f   non-deferred to
425f2 6b 65 6e 73 2e 20 49 6e 20 6f 74 68 65 72 0a 2a  kens. In other.*
425f3 2a 20 20 20 20 20 20 77 6f 72 64 73 2c 20 69 66  *      words, if
425f4 20 74 68 65 20 70 68 72 61 73 65 20 69 73 20 22   the phrase is "
425f5 41 20 42 20 43 22 2c 20 61 6e 64 20 22 42 22 20  A B C", and "B" 
425f6 69 73 20 64 65 66 65 72 72 65 64 2c 20 74 68 65  is deferred, the
425f7 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 20   expression.**  
425f8 20 20 20 20 69 73 20 61 64 76 61 6e 63 65 64 20      is advanced 
425f9 74 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20  to the next row 
425fa 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 6e  that contains an
425fb 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 22 41 20   instance of "A 
425fc 2a 20 43 22 2c 20 0a 2a 2a 20 20 20 20 20 20 77  * C", .**      w
425fd 68 65 72 65 20 22 2a 22 20 6d 61 79 20 6d 61 74  here "*" may mat
425fe 63 68 20 61 6e 79 20 73 69 6e 67 6c 65 20 74 6f  ch any single to
425ff 6b 65 6e 2e 20 54 68 65 20 70 6f 73 69 74 69 6f  ken. The positio
42600 6e 20 6c 69 73 74 20 69 6e 20 74 68 69 73 20 63  n list in this c
42601 61 73 65 0a 2a 2a 20 20 20 20 20 20 69 73 20 70  ase.**      is p
42602 6f 70 75 6c 61 74 65 64 20 61 73 20 66 6f 72 20  opulated as for 
42603 22 41 20 2a 20 43 22 20 62 65 66 6f 72 65 20 72  "A * C" before r
42604 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  eturning..**.** 
42605 20 20 32 2e 20 4e 45 41 52 20 69 73 20 74 72 65    2. NEAR is tre
42606 61 74 65 64 20 61 73 20 41 4e 44 2e 20 49 66 20  ated as AND. If 
42607 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
42608 73 20 22 78 20 4e 45 41 52 20 79 22 2c 20 69 74  s "x NEAR y", it
42609 20 69 73 20 0a 2a 2a 20 20 20 20 20 20 61 64 76   is .**      adv
4260a 61 6e 63 65 64 20 74 6f 20 70 6f 69 6e 74 20 74  anced to point t
4260b 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 74  o the next row t
4260c 68 61 74 20 6d 61 74 63 68 65 73 20 22 78 20 41  hat matches "x A
4260d 4e 44 20 79 22 2e 0a 2a 2a 20 0a 2a 2a 20 53 65  ND y"..** .** Se
4260e 65 20 66 74 73 33 45 76 61 6c 54 65 73 74 44 65  e fts3EvalTestDe
4260f 66 65 72 72 65 64 41 6e 64 4e 65 61 72 28 29 20  ferredAndNear() 
42610 66 6f 72 20 64 65 74 61 69 6c 73 20 6f 6e 20 74  for details on t
42611 65 73 74 69 6e 67 20 69 66 20 61 20 72 6f 77 20  esting if a row 
42612 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 20 6d  is.** really a m
42613 61 74 63 68 2c 20 74 61 6b 69 6e 67 20 69 6e 74  atch, taking int
42614 6f 20 61 63 63 6f 75 6e 74 20 64 65 66 65 72 72  o account deferr
42615 65 64 20 74 6f 6b 65 6e 73 20 61 6e 64 20 4e 45  ed tokens and NE
42616 41 52 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2f  AR operators..*/
42617 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73  .static void fts
42618 33 45 76 61 6c 4e 65 78 74 52 6f 77 28 0a 20 20  3EvalNextRow(.  
42619 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72  Fts3Cursor *pCsr
4261a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
4261b 2f 2a 20 46 54 53 20 43 75 72 73 6f 72 20 68 61  /* FTS Cursor ha
4261c 6e 64 6c 65 20 2a 2f 0a 20 20 46 74 73 33 45 78  ndle */.  Fts3Ex
4261d 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20  pr *pExpr,      
4261e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
4261f 72 2e 20 74 6f 20 61 64 76 61 6e 63 65 20 74 6f  r. to advance to
42620 20 6e 65 78 74 20 6d 61 74 63 68 69 6e 67 20 72   next matching r
42621 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 63  ow */.  int *pRc
42622 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42623 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55          /* IN/OU
42624 54 3a 20 45 72 72 6f 72 20 63 6f 64 65 20 2a 2f  T: Error code */
42625 0a 29 7b 0a 20 20 69 66 28 20 2a 70 52 63 3d 3d  .){.  if( *pRc==
42626 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
42627 20 69 6e 74 20 62 44 65 73 63 44 6f 63 6c 69 73   int bDescDoclis
42628 74 20 3d 20 70 43 73 72 2d 3e 62 44 65 73 63 3b  t = pCsr->bDesc;
42629 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
4262a 20 62 79 20 44 4f 43 49 44 5f 43 4d 50 28 29 20   by DOCID_CMP() 
4262b 6d 61 63 72 6f 20 2a 2f 0a 20 20 20 20 61 73 73  macro */.    ass
4262c 65 72 74 28 20 70 45 78 70 72 2d 3e 62 45 6f 66  ert( pExpr->bEof
4262d 3d 3d 30 20 29 3b 0a 20 20 20 20 70 45 78 70 72  ==0 );.    pExpr
4262e 2d 3e 62 53 74 61 72 74 20 3d 20 31 3b 0a 0a 20  ->bStart = 1;.. 
4262f 20 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72     switch( pExpr
42630 2d 3e 65 54 79 70 65 20 29 7b 0a 20 20 20 20 20  ->eType ){.     
42631 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e   case FTSQUERY_N
42632 45 41 52 3a 0a 20 20 20 20 20 20 63 61 73 65 20  EAR:.      case 
42633 46 54 53 51 55 45 52 59 5f 41 4e 44 3a 20 7b 0a  FTSQUERY_AND: {.
42634 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72          Fts3Expr
42635 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d   *pLeft = pExpr-
42636 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20  >pLeft;.        
42637 46 74 73 33 45 78 70 72 20 2a 70 52 69 67 68 74  Fts3Expr *pRight
42638 20 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74   = pExpr->pRight
42639 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
4263a 28 20 21 70 4c 65 66 74 2d 3e 62 44 65 66 65 72  ( !pLeft->bDefer
4263b 72 65 64 20 7c 7c 20 21 70 52 69 67 68 74 2d 3e  red || !pRight->
4263c 62 44 65 66 65 72 72 65 64 20 29 3b 0a 0a 20 20  bDeferred );..  
4263d 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d        if( pLeft-
4263e 3e 62 44 65 66 65 72 72 65 64 20 29 7b 0a 20 20  >bDeferred ){.  
4263f 20 20 20 20 20 20 20 20 2f 2a 20 4c 48 53 20 69          /* LHS i
42640 73 20 65 6e 74 69 72 65 6c 79 20 64 65 66 65 72  s entirely defer
42641 72 65 64 2e 20 53 6f 20 77 65 20 61 73 73 75 6d  red. So we assum
42642 65 20 69 74 20 6d 61 74 63 68 65 73 20 65 76 65  e it matches eve
42643 72 79 20 72 6f 77 2e 0a 20 20 20 20 20 20 20 20  ry row..        
42644 20 20 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 65    ** Advance the
42645 20 52 48 53 20 69 74 65 72 61 74 6f 72 20 74 6f   RHS iterator to
42646 20 66 69 6e 64 20 74 68 65 20 6e 65 78 74 20 72   find the next r
42647 6f 77 20 76 69 73 69 74 65 64 2e 20 2a 2f 0a 20  ow visited. */. 
42648 20 20 20 20 20 20 20 20 20 66 74 73 33 45 76 61           fts3Eva
42649 6c 4e 65 78 74 52 6f 77 28 70 43 73 72 2c 20 70  lNextRow(pCsr, p
4264a 52 69 67 68 74 2c 20 70 52 63 29 3b 0a 20 20 20  Right, pRc);.   
4264b 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 44         pExpr->iD
4264c 6f 63 69 64 20 3d 20 70 52 69 67 68 74 2d 3e 69  ocid = pRight->i
4264d 44 6f 63 69 64 3b 0a 20 20 20 20 20 20 20 20 20  Docid;.         
4264e 20 70 45 78 70 72 2d 3e 62 45 6f 66 20 3d 20 70   pExpr->bEof = p
4264f 52 69 67 68 74 2d 3e 62 45 6f 66 3b 0a 20 20 20  Right->bEof;.   
42650 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
42651 52 69 67 68 74 2d 3e 62 44 65 66 65 72 72 65 64  Right->bDeferred
42652 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
42653 20 52 48 53 20 69 73 20 65 6e 74 69 72 65 6c 79   RHS is entirely
42654 20 64 65 66 65 72 72 65 64 2e 20 53 6f 20 77 65   deferred. So we
42655 20 61 73 73 75 6d 65 20 69 74 20 6d 61 74 63 68   assume it match
42656 65 73 20 65 76 65 72 79 20 72 6f 77 2e 0a 20 20  es every row..  
42657 20 20 20 20 20 20 20 20 2a 2a 20 41 64 76 61 6e          ** Advan
42658 63 65 20 74 68 65 20 4c 48 53 20 69 74 65 72 61  ce the LHS itera
42659 74 6f 72 20 74 6f 20 66 69 6e 64 20 74 68 65 20  tor to find the 
4265a 6e 65 78 74 20 72 6f 77 20 76 69 73 69 74 65 64  next row visited
4265b 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 66  . */.          f
4265c 74 73 33 45 76 61 6c 4e 65 78 74 52 6f 77 28 70  ts3EvalNextRow(p
4265d 43 73 72 2c 20 70 4c 65 66 74 2c 20 70 52 63 29  Csr, pLeft, pRc)
4265e 3b 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70  ;.          pExp
4265f 72 2d 3e 69 44 6f 63 69 64 20 3d 20 70 4c 65 66  r->iDocid = pLef
42660 74 2d 3e 69 44 6f 63 69 64 3b 0a 20 20 20 20 20  t->iDocid;.     
42661 20 20 20 20 20 70 45 78 70 72 2d 3e 62 45 6f 66       pExpr->bEof
42662 20 3d 20 70 4c 65 66 74 2d 3e 62 45 6f 66 3b 0a   = pLeft->bEof;.
42663 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
42664 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 69 74           /* Neit
42665 68 65 72 20 74 68 65 20 52 48 53 20 6f 72 20 4c  her the RHS or L
42666 48 53 20 61 72 65 20 64 65 66 65 72 72 65 64 2e  HS are deferred.
42667 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 66 74   */.          ft
42668 73 33 45 76 61 6c 4e 65 78 74 52 6f 77 28 70 43  s3EvalNextRow(pC
42669 73 72 2c 20 70 4c 65 66 74 2c 20 70 52 63 29 3b  sr, pLeft, pRc);
4266a 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33 45  .          fts3E
4266b 76 61 6c 4e 65 78 74 52 6f 77 28 70 43 73 72 2c  valNextRow(pCsr,
4266c 20 70 52 69 67 68 74 2c 20 70 52 63 29 3b 0a 20   pRight, pRc);. 
4266d 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
4266e 21 70 4c 65 66 74 2d 3e 62 45 6f 66 20 26 26 20  !pLeft->bEof && 
4266f 21 70 52 69 67 68 74 2d 3e 62 45 6f 66 20 26 26  !pRight->bEof &&
42670 20 2a 70 52 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b   *pRc==SQLITE_OK
42671 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
42672 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 44  sqlite3_int64 iD
42673 69 66 66 20 3d 20 44 4f 43 49 44 5f 43 4d 50 28  iff = DOCID_CMP(
42674 70 4c 65 66 74 2d 3e 69 44 6f 63 69 64 2c 20 70  pLeft->iDocid, p
42675 52 69 67 68 74 2d 3e 69 44 6f 63 69 64 29 3b 0a  Right->iDocid);.
42676 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
42677 69 44 69 66 66 3d 3d 30 20 29 20 62 72 65 61 6b  iDiff==0 ) break
42678 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
42679 28 20 69 44 69 66 66 3c 30 20 29 7b 0a 20 20 20  ( iDiff<0 ){.   
4267a 20 20 20 20 20 20 20 20 20 20 20 66 74 73 33 45             fts3E
4267b 76 61 6c 4e 65 78 74 52 6f 77 28 70 43 73 72 2c  valNextRow(pCsr,
4267c 20 70 4c 65 66 74 2c 20 70 52 63 29 3b 0a 20 20   pLeft, pRc);.  
4267d 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
4267e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66  .              f
4267f 74 73 33 45 76 61 6c 4e 65 78 74 52 6f 77 28 70  ts3EvalNextRow(p
42680 43 73 72 2c 20 70 52 69 67 68 74 2c 20 70 52 63  Csr, pRight, pRc
42681 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  );.            }
42682 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
42683 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 44         pExpr->iD
42684 6f 63 69 64 20 3d 20 70 4c 65 66 74 2d 3e 69 44  ocid = pLeft->iD
42685 6f 63 69 64 3b 0a 20 20 20 20 20 20 20 20 20 20  ocid;.          
42686 70 45 78 70 72 2d 3e 62 45 6f 66 20 3d 20 28 70  pExpr->bEof = (p
42687 4c 65 66 74 2d 3e 62 45 6f 66 20 7c 7c 20 70 52  Left->bEof || pR
42688 69 67 68 74 2d 3e 62 45 6f 66 29 3b 0a 20 20 20  ight->bEof);.   
42689 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62       }.        b
4268a 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
4268b 0a 20 20 20 20 20 20 63 61 73 65 20 46 54 53 51  .      case FTSQ
4268c 55 45 52 59 5f 4f 52 3a 20 7b 0a 20 20 20 20 20  UERY_OR: {.     
4268d 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c 65     Fts3Expr *pLe
4268e 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66  ft = pExpr->pLef
4268f 74 3b 0a 20 20 20 20 20 20 20 20 46 74 73 33 45  t;.        Fts3E
42690 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 45  xpr *pRight = pE
42691 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20  xpr->pRight;.   
42692 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
42693 36 34 20 69 43 6d 70 20 3d 20 44 4f 43 49 44 5f  64 iCmp = DOCID_
42694 43 4d 50 28 70 4c 65 66 74 2d 3e 69 44 6f 63 69  CMP(pLeft->iDoci
42695 64 2c 20 70 52 69 67 68 74 2d 3e 69 44 6f 63 69  d, pRight->iDoci
42696 64 29 3b 0a 0a 20 20 20 20 20 20 20 20 61 73 73  d);..        ass
42697 65 72 74 28 20 70 4c 65 66 74 2d 3e 62 53 74 61  ert( pLeft->bSta
42698 72 74 20 7c 7c 20 70 4c 65 66 74 2d 3e 69 44 6f  rt || pLeft->iDo
42699 63 69 64 3d 3d 70 52 69 67 68 74 2d 3e 69 44 6f  cid==pRight->iDo
4269a 63 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20 61  cid );.        a
4269b 73 73 65 72 74 28 20 70 52 69 67 68 74 2d 3e 62  ssert( pRight->b
4269c 53 74 61 72 74 20 7c 7c 20 70 4c 65 66 74 2d 3e  Start || pLeft->
4269d 69 44 6f 63 69 64 3d 3d 70 52 69 67 68 74 2d 3e  iDocid==pRight->
4269e 69 44 6f 63 69 64 20 29 3b 0a 0a 20 20 20 20 20  iDocid );..     
4269f 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 62     if( pRight->b
426a0 45 6f 66 20 7c 7c 20 28 70 4c 65 66 74 2d 3e 62  Eof || (pLeft->b
426a1 45 6f 66 3d 3d 30 20 26 26 20 69 43 6d 70 3c 30  Eof==0 && iCmp<0
426a2 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  ) ){.          f
426a3 74 73 33 45 76 61 6c 4e 65 78 74 52 6f 77 28 70  ts3EvalNextRow(p
426a4 43 73 72 2c 20 70 4c 65 66 74 2c 20 70 52 63 29  Csr, pLeft, pRc)
426a5 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
426a6 69 66 28 20 70 4c 65 66 74 2d 3e 62 45 6f 66 20  if( pLeft->bEof 
426a7 7c 7c 20 28 70 52 69 67 68 74 2d 3e 62 45 6f 66  || (pRight->bEof
426a8 3d 3d 30 20 26 26 20 69 43 6d 70 3e 30 29 20 29  ==0 && iCmp>0) )
426a9 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33  {.          fts3
426aa 45 76 61 6c 4e 65 78 74 52 6f 77 28 70 43 73 72  EvalNextRow(pCsr
426ab 2c 20 70 52 69 67 68 74 2c 20 70 52 63 29 3b 0a  , pRight, pRc);.
426ac 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
426ad 20 20 20 20 20 20 20 20 20 66 74 73 33 45 76 61           fts3Eva
426ae 6c 4e 65 78 74 52 6f 77 28 70 43 73 72 2c 20 70  lNextRow(pCsr, p
426af 4c 65 66 74 2c 20 70 52 63 29 3b 0a 20 20 20 20  Left, pRc);.    
426b0 20 20 20 20 20 20 66 74 73 33 45 76 61 6c 4e 65        fts3EvalNe
426b1 78 74 52 6f 77 28 70 43 73 72 2c 20 70 52 69 67  xtRow(pCsr, pRig
426b2 68 74 2c 20 70 52 63 29 3b 0a 20 20 20 20 20 20  ht, pRc);.      
426b3 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 70 45 78    }..        pEx
426b4 70 72 2d 3e 62 45 6f 66 20 3d 20 28 70 4c 65 66  pr->bEof = (pLef
426b5 74 2d 3e 62 45 6f 66 20 26 26 20 70 52 69 67 68  t->bEof && pRigh
426b6 74 2d 3e 62 45 6f 66 29 3b 0a 20 20 20 20 20 20  t->bEof);.      
426b7 20 20 69 43 6d 70 20 3d 20 44 4f 43 49 44 5f 43    iCmp = DOCID_C
426b8 4d 50 28 70 4c 65 66 74 2d 3e 69 44 6f 63 69 64  MP(pLeft->iDocid
426b9 2c 20 70 52 69 67 68 74 2d 3e 69 44 6f 63 69 64  , pRight->iDocid
426ba 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
426bb 52 69 67 68 74 2d 3e 62 45 6f 66 20 7c 7c 20 28  Right->bEof || (
426bc 70 4c 65 66 74 2d 3e 62 45 6f 66 3d 3d 30 20 26  pLeft->bEof==0 &
426bd 26 20 20 69 43 6d 70 3c 30 29 20 29 7b 0a 20 20  &  iCmp<0) ){.  
426be 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
426bf 44 6f 63 69 64 20 3d 20 70 4c 65 66 74 2d 3e 69  Docid = pLeft->i
426c0 44 6f 63 69 64 3b 0a 20 20 20 20 20 20 20 20 7d  Docid;.        }
426c1 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
426c2 70 45 78 70 72 2d 3e 69 44 6f 63 69 64 20 3d 20  pExpr->iDocid = 
426c3 70 52 69 67 68 74 2d 3e 69 44 6f 63 69 64 3b 0a  pRight->iDocid;.
426c4 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
426c5 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
426c6 7d 0a 0a 20 20 20 20 20 20 63 61 73 65 20 46 54  }..      case FT
426c7 53 51 55 45 52 59 5f 4e 4f 54 3a 20 7b 0a 20 20  SQUERY_NOT: {.  
426c8 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20 2a        Fts3Expr *
426c9 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70  pLeft = pExpr->p
426ca 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 46 74  Left;.        Ft
426cb 73 33 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d  s3Expr *pRight =
426cc 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a   pExpr->pRight;.
426cd 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 52 69  .        if( pRi
426ce 67 68 74 2d 3e 62 53 74 61 72 74 3d 3d 30 20 29  ght->bStart==0 )
426cf 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33  {.          fts3
426d0 45 76 61 6c 4e 65 78 74 52 6f 77 28 70 43 73 72  EvalNextRow(pCsr
426d1 2c 20 70 52 69 67 68 74 2c 20 70 52 63 29 3b 0a  , pRight, pRc);.
426d2 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
426d3 28 20 2a 70 52 63 21 3d 53 51 4c 49 54 45 5f 4f  ( *pRc!=SQLITE_O
426d4 4b 20 7c 7c 20 70 52 69 67 68 74 2d 3e 62 53 74  K || pRight->bSt
426d5 61 72 74 20 29 3b 0a 20 20 20 20 20 20 20 20 7d  art );.        }
426d6 0a 0a 20 20 20 20 20 20 20 20 66 74 73 33 45 76  ..        fts3Ev
426d7 61 6c 4e 65 78 74 52 6f 77 28 70 43 73 72 2c 20  alNextRow(pCsr, 
426d8 70 4c 65 66 74 2c 20 70 52 63 29 3b 0a 20 20 20  pLeft, pRc);.   
426d9 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e       if( pLeft->
426da 62 45 6f 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20  bEof==0 ){.     
426db 20 20 20 20 20 77 68 69 6c 65 28 20 21 2a 70 52       while( !*pR
426dc 63 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  c .             
426dd 20 26 26 20 21 70 52 69 67 68 74 2d 3e 62 45 6f   && !pRight->bEo
426de 66 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  f .             
426df 20 26 26 20 44 4f 43 49 44 5f 43 4d 50 28 70 4c   && DOCID_CMP(pL
426e0 65 66 74 2d 3e 69 44 6f 63 69 64 2c 20 70 52 69  eft->iDocid, pRi
426e1 67 68 74 2d 3e 69 44 6f 63 69 64 29 3e 30 20 0a  ght->iDocid)>0 .
426e2 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20            ){.   
426e3 20 20 20 20 20 20 20 20 20 66 74 73 33 45 76 61           fts3Eva
426e4 6c 4e 65 78 74 52 6f 77 28 70 43 73 72 2c 20 70  lNextRow(pCsr, p
426e5 52 69 67 68 74 2c 20 70 52 63 29 3b 0a 20 20 20  Right, pRc);.   
426e6 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
426e7 20 7d 0a 20 20 20 20 20 20 20 20 70 45 78 70 72   }.        pExpr
426e8 2d 3e 69 44 6f 63 69 64 20 3d 20 70 4c 65 66 74  ->iDocid = pLeft
426e9 2d 3e 69 44 6f 63 69 64 3b 0a 20 20 20 20 20 20  ->iDocid;.      
426ea 20 20 70 45 78 70 72 2d 3e 62 45 6f 66 20 3d 20    pExpr->bEof = 
426eb 70 4c 65 66 74 2d 3e 62 45 6f 66 3b 0a 20 20 20  pLeft->bEof;.   
426ec 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
426ed 20 20 7d 0a 0a 20 20 20 20 20 20 64 65 66 61 75    }..      defau
426ee 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 46 74  lt: {.        Ft
426ef 73 33 50 68 72 61 73 65 20 2a 70 50 68 72 61 73  s3Phrase *pPhras
426f0 65 20 3d 20 70 45 78 70 72 2d 3e 70 50 68 72 61  e = pExpr->pPhra
426f1 73 65 3b 0a 20 20 20 20 20 20 20 20 66 74 73 33  se;.        fts3
426f2 45 76 61 6c 49 6e 76 61 6c 69 64 61 74 65 50 6f  EvalInvalidatePo
426f3 73 6c 69 73 74 28 70 50 68 72 61 73 65 29 3b 0a  slist(pPhrase);.
426f4 20 20 20 20 20 20 20 20 2a 70 52 63 20 3d 20 66          *pRc = f
426f5 74 73 33 45 76 61 6c 50 68 72 61 73 65 4e 65 78  ts3EvalPhraseNex
426f6 74 28 70 43 73 72 2c 20 70 50 68 72 61 73 65 2c  t(pCsr, pPhrase,
426f7 20 26 70 45 78 70 72 2d 3e 62 45 6f 66 29 3b 0a   &pExpr->bEof);.
426f8 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
426f9 44 6f 63 69 64 20 3d 20 70 50 68 72 61 73 65 2d  Docid = pPhrase-
426fa 3e 64 6f 63 6c 69 73 74 2e 69 44 6f 63 69 64 3b  >doclist.iDocid;
426fb 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
426fc 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
426fd 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 2a 70  }.}../*.** If *p
426fe 52 63 20 69 73 20 6e 6f 74 20 53 51 4c 49 54 45  Rc is not SQLITE
426ff 5f 4f 4b 2c 20 6f 72 20 69 66 20 70 45 78 70 72  _OK, or if pExpr
42700 20 69 73 20 6e 6f 74 20 74 68 65 20 72 6f 6f 74   is not the root
42701 20 6e 6f 64 65 20 6f 66 20 61 20 4e 45 41 52 0a   node of a NEAR.
42702 2a 2a 20 63 6c 75 73 74 65 72 2c 20 74 68 65 6e  ** cluster, then
42703 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
42704 65 74 75 72 6e 73 20 31 20 69 6d 6d 65 64 69 61  eturns 1 immedia
42705 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65  tely..**.** Othe
42706 72 77 69 73 65 2c 20 69 74 20 63 68 65 63 6b 73  rwise, it checks
42707 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   if the current 
42708 72 6f 77 20 72 65 61 6c 6c 79 20 64 6f 65 73 20  row really does 
42709 6d 61 74 63 68 20 74 68 65 20 4e 45 41 52 20 0a  match the NEAR .
4270a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 75  ** expression, u
4270b 73 69 6e 67 20 74 68 65 20 64 61 74 61 20 63 75  sing the data cu
4270c 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69  rrently stored i
4270d 6e 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 6c  n the position l
4270e 69 73 74 73 20 0a 2a 2a 20 28 46 74 73 33 45 78  ists .** (Fts3Ex
4270f 70 72 2d 3e 70 50 68 72 61 73 65 2e 64 6f 63 6c  pr->pPhrase.docl
42710 69 73 74 2e 70 4c 69 73 74 2f 6e 4c 69 73 74 29  ist.pList/nList)
42711 20 66 6f 72 20 65 61 63 68 20 70 68 72 61 73 65   for each phrase
42712 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   in the expressi
42713 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  on. .**.** If th
42714 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 69 73  e current row is
42715 20 61 20 6d 61 74 63 68 2c 20 74 68 65 20 70 6f   a match, the po
42716 73 69 74 69 6f 6e 20 6c 69 73 74 20 61 73 73 6f  sition list asso
42717 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68  ciated with each
42718 0a 2a 2a 20 70 68 72 61 73 65 20 69 6e 20 74 68  .** phrase in th
42719 65 20 4e 45 41 52 20 65 78 70 72 65 73 73 69 6f  e NEAR expressio
4271a 6e 20 69 73 20 65 64 69 74 65 64 20 69 6e 20 70  n is edited in p
4271b 6c 61 63 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20  lace to contain 
4271c 6f 6e 6c 79 20 74 68 6f 73 65 0a 2a 2a 20 70 68  only those.** ph
4271d 72 61 73 65 20 69 6e 73 74 61 6e 63 65 73 20 73  rase instances s
4271e 75 66 66 69 63 69 65 6e 74 6c 79 20 63 6c 6f 73  ufficiently clos
4271f 65 20 74 6f 20 74 68 65 69 72 20 70 65 65 72 73  e to their peers
42720 20 74 6f 20 73 61 74 69 73 66 79 20 61 6c 6c 20   to satisfy all 
42721 4e 45 41 52 0a 2a 2a 20 63 6f 6e 73 74 72 61 69  NEAR.** constrai
42722 6e 74 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73  nts. In this cas
42723 65 20 69 74 20 72 65 74 75 72 6e 73 20 31 2e 20  e it returns 1. 
42724 49 66 20 74 68 65 20 4e 45 41 52 20 65 78 70 72  If the NEAR expr
42725 65 73 73 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  ession does not 
42726 0a 2a 2a 20 6d 61 74 63 68 20 74 68 65 20 63 75  .** match the cu
42727 72 72 65 6e 74 20 72 6f 77 2c 20 30 20 69 73 20  rrent row, 0 is 
42728 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 70 6f  returned. The po
42729 73 69 74 69 6f 6e 20 6c 69 73 74 73 20 6d 61 79  sition lists may
4272a 20 6f 72 20 6d 61 79 20 6e 6f 74 0a 2a 2a 20 62   or may not.** b
4272b 65 20 65 64 69 74 65 64 20 69 66 20 30 20 69 73  e edited if 0 is
4272c 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
4272d 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 76 61  atic int fts3Eva
4272e 6c 4e 65 61 72 54 65 73 74 28 46 74 73 33 45 78  lNearTest(Fts3Ex
4272f 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a  pr *pExpr, int *
42730 70 52 63 29 7b 0a 20 20 69 6e 74 20 72 65 73 20  pRc){.  int res 
42731 3d 20 31 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66  = 1;..  /* The f
42732 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 72  ollowing block r
42733 75 6e 73 20 69 66 20 70 45 78 70 72 20 69 73 20  uns if pExpr is 
42734 74 68 65 20 72 6f 6f 74 20 6f 66 20 61 20 4e 45  the root of a NE
42735 41 52 20 71 75 65 72 79 2e 0a 20 20 2a 2a 20 46  AR query..  ** F
42736 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20  or example, the 
42737 71 75 65 72 79 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  query:.  **.  **
42738 20 20 20 20 20 20 20 20 20 22 77 22 20 4e 45 41           "w" NEA
42739 52 20 22 78 22 20 4e 45 41 52 20 22 79 22 20 4e  R "x" NEAR "y" N
4273a 45 41 52 20 22 7a 22 0a 20 20 2a 2a 0a 20 20 2a  EAR "z".  **.  *
4273b 2a 20 77 68 69 63 68 20 69 73 20 72 65 70 72 65  * which is repre
4273c 73 65 6e 74 65 64 20 69 6e 20 74 72 65 65 20 66  sented in tree f
4273d 6f 72 6d 20 61 73 3a 0a 20 20 2a 2a 0a 20 20 2a  orm as:.  **.  *
4273e 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
4273f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42740 7c 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20  |.  **          
42741 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42742 2b 2d 2d 4e 45 41 52 2d 2d 2b 20 20 20 20 20 20  +--NEAR--+      
42743 3c 2d 2d 20 72 6f 6f 74 20 6f 66 20 4e 45 41 52  <-- root of NEAR
42744 20 71 75 65 72 79 0a 20 20 2a 2a 20 20 20 20 20   query.  **     
42745 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42746 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 7c 0a       |        |.
42747 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
42748 20 20 20 20 20 20 20 20 20 2b 2d 2d 4e 45 41 52           +--NEAR
42749 2d 2d 2b 20 20 20 22 7a 22 0a 20 20 2a 2a 20 20  --+   "z".  **  
4274a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4274b 20 20 20 7c 20 20 20 20 20 20 20 20 7c 0a 20 20     |        |.  
4274c 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
4274d 20 20 2b 2d 2d 4e 45 41 52 2d 2d 2b 20 20 20 22    +--NEAR--+   "
4274e 79 22 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  y".  **         
4274f 20 20 20 20 20 20 20 7c 20 20 20 20 20 20 20 20         |        
42750 7c 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20  |.  **          
42751 20 20 20 20 20 22 77 22 20 20 20 20 20 20 22 78       "w"      "x
42752 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ".  **.  ** The 
42753 72 69 67 68 74 2d 68 61 6e 64 20 63 68 69 6c 64  right-hand child
42754 20 6f 66 20 61 20 4e 45 41 52 20 6e 6f 64 65 20   of a NEAR node 
42755 69 73 20 61 6c 77 61 79 73 20 61 20 70 68 72 61  is always a phra
42756 73 65 2e 20 54 68 65 20 0a 20 20 2a 2a 20 6c 65  se. The .  ** le
42757 66 74 2d 68 61 6e 64 20 63 68 69 6c 64 20 6d 61  ft-hand child ma
42758 79 20 62 65 20 65 69 74 68 65 72 20 61 20 70 68  y be either a ph
42759 72 61 73 65 20 6f 72 20 61 20 4e 45 41 52 20 6e  rase or a NEAR n
4275a 6f 64 65 2e 20 54 68 65 72 65 20 61 72 65 0a 20  ode. There are. 
4275b 20 2a 2a 20 6e 6f 20 65 78 63 65 70 74 69 6f 6e   ** no exception
4275c 73 20 74 6f 20 74 68 69 73 20 2d 20 69 74 27 73  s to this - it's
4275d 20 74 68 65 20 77 61 79 20 74 68 65 20 70 61 72   the way the par
4275e 73 65 72 20 69 6e 20 66 74 73 33 5f 65 78 70 72  ser in fts3_expr
4275f 2e 63 20 77 6f 72 6b 73 2e 0a 20 20 2a 2f 0a 20  .c works..  */. 
42760 20 69 66 28 20 2a 70 52 63 3d 3d 53 51 4c 49 54   if( *pRc==SQLIT
42761 45 5f 4f 4b 20 0a 20 20 20 26 26 20 70 45 78 70  E_OK .   && pExp
42762 72 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  r->eType==FTSQUE
42763 52 59 5f 4e 45 41 52 20 0a 20 20 20 26 26 20 70  RY_NEAR .   && p
42764 45 78 70 72 2d 3e 62 45 6f 66 3d 3d 30 0a 20 20  Expr->bEof==0.  
42765 20 26 26 20 28 70 45 78 70 72 2d 3e 70 50 61 72   && (pExpr->pPar
42766 65 6e 74 3d 3d 30 20 7c 7c 20 70 45 78 70 72 2d  ent==0 || pExpr-
42767 3e 70 50 61 72 65 6e 74 2d 3e 65 54 79 70 65 21  >pParent->eType!
42768 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 29 0a  =FTSQUERY_NEAR).
42769 20 20 29 7b 0a 20 20 20 20 46 74 73 33 45 78 70    ){.    Fts3Exp
4276a 72 20 2a 70 3b 20 0a 20 20 20 20 69 6e 74 20 6e  r *p; .    int n
4276b 54 6d 70 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Tmp = 0;        
4276c 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
4276d 73 20 6f 66 20 74 65 6d 70 20 73 70 61 63 65 20  s of temp space 
4276e 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 61 54 6d  */.    char *aTm
4276f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
42770 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 70 61       /* Temp spa
42771 63 65 20 66 6f 72 20 50 6f 73 6c 69 73 74 4e 65  ce for PoslistNe
42772 61 72 4d 65 72 67 65 28 29 20 2a 2f 0a 0a 20 20  arMerge() */..  
42773 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 65    /* Allocate te
42774 6d 70 6f 72 61 72 79 20 77 6f 72 6b 69 6e 67 20  mporary working 
42775 73 70 61 63 65 2e 20 2a 2f 0a 20 20 20 20 66 6f  space. */.    fo
42776 72 28 70 3d 70 45 78 70 72 3b 20 70 2d 3e 70 4c  r(p=pExpr; p->pL
42777 65 66 74 3b 20 70 3d 70 2d 3e 70 4c 65 66 74 29  eft; p=p->pLeft)
42778 7b 0a 20 20 20 20 20 20 6e 54 6d 70 20 2b 3d 20  {.      nTmp += 
42779 70 2d 3e 70 52 69 67 68 74 2d 3e 70 50 68 72 61  p->pRight->pPhra
4277a 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 6e 4c 69 73  se->doclist.nLis
4277b 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 54 6d  t;.    }.    nTm
4277c 70 20 2b 3d 20 70 2d 3e 70 50 68 72 61 73 65 2d  p += p->pPhrase-
4277d 3e 64 6f 63 6c 69 73 74 2e 6e 4c 69 73 74 3b 0a  >doclist.nList;.
4277e 20 20 20 20 69 66 28 20 6e 54 6d 70 3d 3d 30 20      if( nTmp==0 
4277f 29 7b 0a 20 20 20 20 20 20 72 65 73 20 3d 20 30  ){.      res = 0
42780 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
42781 20 20 20 61 54 6d 70 20 3d 20 73 71 6c 69 74 65     aTmp = sqlite
42782 33 5f 6d 61 6c 6c 6f 63 28 6e 54 6d 70 2a 32 29  3_malloc(nTmp*2)
42783 3b 0a 20 20 20 20 20 20 69 66 28 20 21 61 54 6d  ;.      if( !aTm
42784 70 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 52  p ){.        *pR
42785 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
42786 3b 0a 20 20 20 20 20 20 20 20 72 65 73 20 3d 20  ;.        res = 
42787 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  0;.      }else{.
42788 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 61 50          char *aP
42789 6f 73 6c 69 73 74 20 3d 20 70 2d 3e 70 50 68 72  oslist = p->pPhr
4278a 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c 69  ase->doclist.pLi
4278b 73 74 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  st;.        int 
4278c 6e 54 6f 6b 65 6e 20 3d 20 70 2d 3e 70 50 68 72  nToken = p->pPhr
4278d 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 0a 20 20  ase->nToken;..  
4278e 20 20 20 20 20 20 66 6f 72 28 70 3d 70 2d 3e 70        for(p=p->p
4278f 50 61 72 65 6e 74 3b 72 65 73 20 26 26 20 70 20  Parent;res && p 
42790 26 26 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53  && p->eType==FTS
42791 51 55 45 52 59 5f 4e 45 41 52 3b 20 70 3d 70 2d  QUERY_NEAR; p=p-
42792 3e 70 50 61 72 65 6e 74 29 7b 0a 20 20 20 20 20  >pParent){.     
42793 20 20 20 20 20 46 74 73 33 50 68 72 61 73 65 20       Fts3Phrase 
42794 2a 70 50 68 72 61 73 65 20 3d 20 70 2d 3e 70 52  *pPhrase = p->pR
42795 69 67 68 74 2d 3e 70 50 68 72 61 73 65 3b 0a 20  ight->pPhrase;. 
42796 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 4e 65           int nNe
42797 61 72 20 3d 20 70 2d 3e 6e 4e 65 61 72 3b 0a 20  ar = p->nNear;. 
42798 20 20 20 20 20 20 20 20 20 72 65 73 20 3d 20 66           res = f
42799 74 73 33 45 76 61 6c 4e 65 61 72 54 72 69 6d 28  ts3EvalNearTrim(
4279a 6e 4e 65 61 72 2c 20 61 54 6d 70 2c 20 26 61 50  nNear, aTmp, &aP
4279b 6f 73 6c 69 73 74 2c 20 26 6e 54 6f 6b 65 6e 2c  oslist, &nToken,
4279c 20 70 50 68 72 61 73 65 29 3b 0a 20 20 20 20 20   pPhrase);.     
4279d 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 61 50     }..        aP
4279e 6f 73 6c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  oslist = pExpr->
4279f 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73 65 2d  pRight->pPhrase-
427a0 3e 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74 3b 0a  >doclist.pList;.
427a1 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d          nToken =
427a2 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e   pExpr->pRight->
427a3 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b  pPhrase->nToken;
427a4 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 3d 70  .        for(p=p
427a5 45 78 70 72 2d 3e 70 4c 65 66 74 3b 20 70 20 26  Expr->pLeft; p &
427a6 26 20 72 65 73 3b 20 70 3d 70 2d 3e 70 4c 65 66  & res; p=p->pLef
427a7 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  t){.          in
427a8 74 20 6e 4e 65 61 72 3b 0a 20 20 20 20 20 20 20  t nNear;.       
427a9 20 20 20 46 74 73 33 50 68 72 61 73 65 20 2a 70     Fts3Phrase *p
427aa 50 68 72 61 73 65 3b 0a 20 20 20 20 20 20 20 20  Phrase;.        
427ab 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 61    assert( p->pPa
427ac 72 65 6e 74 20 26 26 20 70 2d 3e 70 50 61 72 65  rent && p->pPare
427ad 6e 74 2d 3e 70 4c 65 66 74 3d 3d 70 20 29 3b 0a  nt->pLeft==p );.
427ae 20 20 20 20 20 20 20 20 20 20 6e 4e 65 61 72 20            nNear 
427af 3d 20 70 2d 3e 70 50 61 72 65 6e 74 2d 3e 6e 4e  = p->pParent->nN
427b0 65 61 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70  ear;.          p
427b1 50 68 72 61 73 65 20 3d 20 28 0a 20 20 20 20 20  Phrase = (.     
427b2 20 20 20 20 20 20 20 20 20 70 2d 3e 65 54 79 70           p->eTyp
427b3 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
427b4 20 3f 20 70 2d 3e 70 52 69 67 68 74 2d 3e 70 50   ? p->pRight->pP
427b5 68 72 61 73 65 20 3a 20 70 2d 3e 70 50 68 72 61  hrase : p->pPhra
427b6 73 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  se.             
427b7 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65   );.          re
427b8 73 20 3d 20 66 74 73 33 45 76 61 6c 4e 65 61 72  s = fts3EvalNear
427b9 54 72 69 6d 28 6e 4e 65 61 72 2c 20 61 54 6d 70  Trim(nNear, aTmp
427ba 2c 20 26 61 50 6f 73 6c 69 73 74 2c 20 26 6e 54  , &aPoslist, &nT
427bb 6f 6b 65 6e 2c 20 70 50 68 72 61 73 65 29 3b 0a  oken, pPhrase);.
427bc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
427bd 7d 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  }..      sqlite3
427be 5f 66 72 65 65 28 61 54 6d 70 29 3b 0a 20 20 20  _free(aTmp);.   
427bf 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
427c0 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54   res;.}../*.** T
427c1 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
427c2 61 20 68 65 6c 70 65 72 20 66 75 6e 63 74 69 6f  a helper functio
427c3 6e 20 66 6f 72 20 66 74 73 33 45 76 61 6c 54 65  n for fts3EvalTe
427c4 73 74 44 65 66 65 72 72 65 64 41 6e 64 4e 65 61  stDeferredAndNea
427c5 72 28 29 2e 0a 2a 2a 20 41 73 73 75 6d 69 6e 67  r()..** Assuming
427c6 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   no error occurs
427c7 20 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64   or has occurred
427c8 2c 20 49 74 20 72 65 74 75 72 6e 73 20 6e 6f 6e  , It returns non
427c9 2d 7a 65 72 6f 20 69 66 20 74 68 65 0a 2a 2a 20  -zero if the.** 
427ca 65 78 70 72 65 73 73 69 6f 6e 20 70 61 73 73 65  expression passe
427cb 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
427cc 61 72 67 75 6d 65 6e 74 20 6d 61 74 63 68 65 73  argument matches
427cd 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 70 43   the row that pC
427ce 73 72 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79  sr .** currently
427cf 20 70 6f 69 6e 74 73 20 74 6f 2c 20 6f 72 20 7a   points to, or z
427d0 65 72 6f 20 69 66 20 69 74 20 64 6f 65 73 20 6e  ero if it does n
427d1 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 2a 70 52  ot..**.** If *pR
427d2 63 20 69 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f  c is not SQLITE_
427d3 4f 4b 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  OK when this fun
427d4 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c  ction is called,
427d5 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a   it is a no-op..
427d6 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
427d7 63 63 75 72 73 20 64 75 72 69 6e 67 20 65 78 65  ccurs during exe
427d8 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 66  cution of this f
427d9 75 6e 63 74 69 6f 6e 2c 20 2a 70 52 63 20 69 73  unction, *pRc is
427da 20 73 65 74 20 74 6f 20 0a 2a 2a 20 74 68 65 20   set to .** the 
427db 61 70 70 72 6f 70 72 69 61 74 65 20 53 51 4c 69  appropriate SQLi
427dc 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 49  te error code. I
427dd 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
427de 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69  returned value i
427df 73 20 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 2e  s .** undefined.
427e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
427e1 74 73 33 45 76 61 6c 54 65 73 74 45 78 70 72 28  ts3EvalTestExpr(
427e2 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70  .  Fts3Cursor *p
427e3 43 73 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  Csr,            
427e4 20 20 20 2f 2a 20 46 54 53 20 63 75 72 73 6f 72     /* FTS cursor
427e5 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 46 74 73   handle */.  Fts
427e6 33 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20  3Expr *pExpr,   
427e7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
427e8 45 78 70 72 20 74 6f 20 74 65 73 74 2e 20 4d 61  Expr to test. Ma
427e9 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20  y or may not be 
427ea 72 6f 6f 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 2a  root. */.  int *
427eb 70 52 63 20 20 20 20 20 20 20 20 20 20 20 20 20  pRc             
427ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e             /* IN
427ed 2f 4f 55 54 3a 20 45 72 72 6f 72 20 63 6f 64 65  /OUT: Error code
427ee 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 62 48 69   */.){.  int bHi
427ef 74 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20  t = 1;          
427f0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
427f1 72 6e 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 66  rn value */.  if
427f2 28 20 2a 70 52 63 3d 3d 53 51 4c 49 54 45 5f 4f  ( *pRc==SQLITE_O
427f3 4b 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28  K ){.    switch(
427f4 20 70 45 78 70 72 2d 3e 65 54 79 70 65 20 29 7b   pExpr->eType ){
427f5 0a 20 20 20 20 20 20 63 61 73 65 20 46 54 53 51  .      case FTSQ
427f6 55 45 52 59 5f 4e 45 41 52 3a 0a 20 20 20 20 20  UERY_NEAR:.     
427f7 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 41   case FTSQUERY_A
427f8 4e 44 3a 0a 20 20 20 20 20 20 20 20 62 48 69 74  ND:.        bHit
427f9 20 3d 20 28 0a 20 20 20 20 20 20 20 20 20 20 20   = (.           
427fa 20 66 74 73 33 45 76 61 6c 54 65 73 74 45 78 70   fts3EvalTestExp
427fb 72 28 70 43 73 72 2c 20 70 45 78 70 72 2d 3e 70  r(pCsr, pExpr->p
427fc 4c 65 66 74 2c 20 70 52 63 29 0a 20 20 20 20 20  Left, pRc).     
427fd 20 20 20 20 26 26 20 66 74 73 33 45 76 61 6c 54      && fts3EvalT
427fe 65 73 74 45 78 70 72 28 70 43 73 72 2c 20 70 45  estExpr(pCsr, pE
427ff 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 70 52 63  xpr->pRight, pRc
42800 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 66 74  ).         && ft
42801 73 33 45 76 61 6c 4e 65 61 72 54 65 73 74 28 70  s3EvalNearTest(p
42802 45 78 70 72 2c 20 70 52 63 29 0a 20 20 20 20 20  Expr, pRc).     
42803 20 20 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f     );..        /
42804 2a 20 49 66 20 74 68 65 20 4e 45 41 52 20 65 78  * If the NEAR ex
42805 70 72 65 73 73 69 6f 6e 20 64 6f 65 73 20 6e 6f  pression does no
42806 74 20 6d 61 74 63 68 20 61 6e 79 20 72 6f 77 73  t match any rows
42807 2c 20 7a 65 72 6f 20 74 68 65 20 64 6f 63 6c 69  , zero the docli
42808 73 74 20 66 6f 72 20 0a 20 20 20 20 20 20 20 20  st for .        
42809 2a 2a 20 61 6c 6c 20 70 68 72 61 73 65 73 20 69  ** all phrases i
4280a 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68 65 20 4e  nvolved in the N
4280b 45 41 52 2e 20 54 68 69 73 20 69 73 20 62 65 63  EAR. This is bec
4280c 61 75 73 65 20 74 68 65 20 73 6e 69 70 70 65 74  ause the snippet
4280d 28 29 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  (),.        ** o
4280e 66 66 73 65 74 73 28 29 20 61 6e 64 20 6d 61 74  ffsets() and mat
4280f 63 68 69 6e 66 6f 28 29 20 66 75 6e 63 74 69 6f  chinfo() functio
42810 6e 73 20 61 72 65 20 6e 6f 74 20 73 75 70 70 6f  ns are not suppo
42811 73 65 64 20 74 6f 20 72 65 63 6f 67 6e 69 7a 65  sed to recognize
42812 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79   .        ** any
42813 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 70 68   instances of ph
42814 72 61 73 65 73 20 74 68 61 74 20 61 72 65 20 70  rases that are p
42815 61 72 74 20 6f 66 20 75 6e 6d 61 74 63 68 65 64  art of unmatched
42816 20 4e 45 41 52 20 71 75 65 72 69 65 73 2e 20 0a   NEAR queries. .
42817 20 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 65          ** For e
42818 78 61 6d 70 6c 65 20 69 66 20 74 68 69 73 20 65  xample if this e
42819 78 70 72 65 73 73 69 6f 6e 3a 0a 20 20 20 20 20  xpression:.     
4281a 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
4281b 20 20 20 20 2e 2e 2e 20 4d 41 54 43 48 20 27 61      ... MATCH 'a
4281c 20 4f 52 20 28 62 20 4e 45 41 52 20 63 29 27 0a   OR (b NEAR c)'.
4281d 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
4281e 20 20 20 2a 2a 20 69 73 20 6d 61 74 63 68 65 64     ** is matched
4281f 20 61 67 61 69 6e 73 74 20 61 20 72 6f 77 20 63   against a row c
42820 6f 6e 74 61 69 6e 69 6e 67 3a 0a 20 20 20 20 20  ontaining:.     
42821 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
42822 20 20 20 20 20 20 20 20 27 61 20 62 20 64 20 65          'a b d e
42823 27 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  '.        **.   
42824 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e 79       ** then any
42825 20 73 6e 69 70 70 65 74 28 29 20 73 68 6f 75 6c   snippet() shoul
42826 64 20 6f 6e 79 20 68 69 67 68 6c 69 67 68 74 20  d ony highlight 
42827 74 68 65 20 22 61 22 20 74 65 72 6d 2c 20 6e 6f  the "a" term, no
42828 74 20 74 68 65 20 22 62 22 0a 20 20 20 20 20 20  t the "b".      
42829 20 20 2a 2a 20 28 61 73 20 22 62 22 20 69 73 20    ** (as "b" is 
4282a 70 61 72 74 20 6f 66 20 61 20 6e 6f 6e 2d 6d 61  part of a non-ma
4282b 74 63 68 69 6e 67 20 4e 45 41 52 20 63 6c 61 75  tching NEAR clau
4282c 73 65 29 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  se)..        */.
4282d 20 20 20 20 20 20 20 20 69 66 28 20 62 48 69 74          if( bHit
4282e 3d 3d 30 20 0a 20 20 20 20 20 20 20 20 20 26 26  ==0 .         &&
4282f 20 70 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d 46   pExpr->eType==F
42830 54 53 51 55 45 52 59 5f 4e 45 41 52 20 0a 20 20  TSQUERY_NEAR .  
42831 20 20 20 20 20 20 20 26 26 20 28 70 45 78 70 72         && (pExpr
42832 2d 3e 70 50 61 72 65 6e 74 3d 3d 30 20 7c 7c 20  ->pParent==0 || 
42833 70 45 78 70 72 2d 3e 70 50 61 72 65 6e 74 2d 3e  pExpr->pParent->
42834 65 54 79 70 65 21 3d 46 54 53 51 55 45 52 59 5f  eType!=FTSQUERY_
42835 4e 45 41 52 29 0a 20 20 20 20 20 20 20 20 29 7b  NEAR).        ){
42836 0a 20 20 20 20 20 20 20 20 20 20 46 74 73 33 45  .          Fts3E
42837 78 70 72 20 2a 70 3b 0a 20 20 20 20 20 20 20 20  xpr *p;.        
42838 20 20 66 6f 72 28 70 3d 70 45 78 70 72 3b 20 70    for(p=pExpr; p
42839 2d 3e 70 50 68 72 61 73 65 3d 3d 30 3b 20 70 3d  ->pPhrase==0; p=
4283a 70 2d 3e 70 4c 65 66 74 29 7b 0a 20 20 20 20 20  p->pLeft){.     
4283b 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 52         if( p->pR
4283c 69 67 68 74 2d 3e 69 44 6f 63 69 64 3d 3d 70 43  ight->iDocid==pC
4283d 73 72 2d 3e 69 50 72 65 76 49 64 20 29 7b 0a 20  sr->iPrevId ){. 
4283e 20 20 20 20 20 20 20 20 20 20 20 20 20 66 74 73               fts
4283f 33 45 76 61 6c 49 6e 76 61 6c 69 64 61 74 65 50  3EvalInvalidateP
42840 6f 73 6c 69 73 74 28 70 2d 3e 70 52 69 67 68 74  oslist(p->pRight
42841 2d 3e 70 50 68 72 61 73 65 29 3b 0a 20 20 20 20  ->pPhrase);.    
42842 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
42843 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
42844 69 66 28 20 70 2d 3e 69 44 6f 63 69 64 3d 3d 70  if( p->iDocid==p
42845 43 73 72 2d 3e 69 50 72 65 76 49 64 20 29 7b 0a  Csr->iPrevId ){.
42846 20 20 20 20 20 20 20 20 20 20 20 20 66 74 73 33              fts3
42847 45 76 61 6c 49 6e 76 61 6c 69 64 61 74 65 50 6f  EvalInvalidatePo
42848 73 6c 69 73 74 28 70 2d 3e 70 50 68 72 61 73 65  slist(p->pPhrase
42849 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
4284a 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
4284b 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20    break;..      
4284c 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 4f 52  case FTSQUERY_OR
4284d 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  : {.        int 
4284e 62 48 69 74 31 20 3d 20 66 74 73 33 45 76 61 6c  bHit1 = fts3Eval
4284f 54 65 73 74 45 78 70 72 28 70 43 73 72 2c 20 70  TestExpr(pCsr, p
42850 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 52 63  Expr->pLeft, pRc
42851 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 62  );.        int b
42852 48 69 74 32 20 3d 20 66 74 73 33 45 76 61 6c 54  Hit2 = fts3EvalT
42853 65 73 74 45 78 70 72 28 70 43 73 72 2c 20 70 45  estExpr(pCsr, pE
42854 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 70 52 63  xpr->pRight, pRc
42855 29 3b 0a 20 20 20 20 20 20 20 20 62 48 69 74 20  );.        bHit 
42856 3d 20 62 48 69 74 31 20 7c 7c 20 62 48 69 74 32  = bHit1 || bHit2
42857 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
42858 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
42859 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e 4f  case FTSQUERY_NO
4285a 54 3a 0a 20 20 20 20 20 20 20 20 62 48 69 74 20  T:.        bHit 
4285b 3d 20 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  = (.            
4285c 66 74 73 33 45 76 61 6c 54 65 73 74 45 78 70 72  fts3EvalTestExpr
4285d 28 70 43 73 72 2c 20 70 45 78 70 72 2d 3e 70 4c  (pCsr, pExpr->pL
4285e 65 66 74 2c 20 70 52 63 29 0a 20 20 20 20 20 20  eft, pRc).      
4285f 20 20 20 26 26 20 21 66 74 73 33 45 76 61 6c 54     && !fts3EvalT
42860 65 73 74 45 78 70 72 28 70 43 73 72 2c 20 70 45  estExpr(pCsr, pE
42861 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 70 52 63  xpr->pRight, pRc
42862 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20  ).        );.   
42863 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20       break;..   
42864 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 23 69     default: {.#i
42865 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53  fndef SQLITE_DIS
42866 41 42 4c 45 5f 46 54 53 34 5f 44 45 46 45 52 52  ABLE_FTS4_DEFERR
42867 45 44 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ED.        if( p
42868 43 73 72 2d 3e 70 44 65 66 65 72 72 65 64 20 0a  Csr->pDeferred .
42869 20 20 20 20 20 20 20 20 20 26 26 20 28 70 45 78           && (pEx
4286a 70 72 2d 3e 69 44 6f 63 69 64 3d 3d 70 43 73 72  pr->iDocid==pCsr
4286b 2d 3e 69 50 72 65 76 49 64 20 7c 7c 20 70 45 78  ->iPrevId || pEx
4286c 70 72 2d 3e 62 44 65 66 65 72 72 65 64 29 0a 20  pr->bDeferred). 
4286d 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20         ){.      
4286e 20 20 20 20 46 74 73 33 50 68 72 61 73 65 20 2a      Fts3Phrase *
4286f 70 50 68 72 61 73 65 20 3d 20 70 45 78 70 72 2d  pPhrase = pExpr-
42870 3e 70 50 68 72 61 73 65 3b 0a 20 20 20 20 20 20  >pPhrase;.      
42871 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
42872 72 2d 3e 62 44 65 66 65 72 72 65 64 20 7c 7c 20  r->bDeferred || 
42873 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74  pPhrase->doclist
42874 2e 62 46 72 65 65 4c 69 73 74 3d 3d 30 20 29 3b  .bFreeList==0 );
42875 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
42876 45 78 70 72 2d 3e 62 44 65 66 65 72 72 65 64 20  Expr->bDeferred 
42877 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66  ){.            f
42878 74 73 33 45 76 61 6c 49 6e 76 61 6c 69 64 61 74  ts3EvalInvalidat
42879 65 50 6f 73 6c 69 73 74 28 70 50 68 72 61 73 65  ePoslist(pPhrase
4287a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
4287b 20 20 20 20 20 20 20 20 20 2a 70 52 63 20 3d 20           *pRc = 
4287c 66 74 73 33 45 76 61 6c 44 65 66 65 72 72 65 64  fts3EvalDeferred
4287d 50 68 72 61 73 65 28 70 43 73 72 2c 20 70 50 68  Phrase(pCsr, pPh
4287e 72 61 73 65 29 3b 0a 20 20 20 20 20 20 20 20 20  rase);.         
4287f 20 62 48 69 74 20 3d 20 28 70 50 68 72 61 73 65   bHit = (pPhrase
42880 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74 21  ->doclist.pList!
42881 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  =0);.          p
42882 45 78 70 72 2d 3e 69 44 6f 63 69 64 20 3d 20 70  Expr->iDocid = p
42883 43 73 72 2d 3e 69 50 72 65 76 49 64 3b 0a 20 20  Csr->iPrevId;.  
42884 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64        }else.#end
42885 69 66 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20  if.        {.   
42886 20 20 20 20 20 20 20 62 48 69 74 20 3d 20 28 70         bHit = (p
42887 45 78 70 72 2d 3e 62 45 6f 66 3d 3d 30 20 26 26  Expr->bEof==0 &&
42888 20 70 45 78 70 72 2d 3e 69 44 6f 63 69 64 3d 3d   pExpr->iDocid==
42889 70 43 73 72 2d 3e 69 50 72 65 76 49 64 29 3b 0a  pCsr->iPrevId);.
4288a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
4288b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
4288c 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
4288d 75 72 6e 20 62 48 69 74 3b 0a 7d 0a 0a 2f 2a 0a  urn bHit;.}../*.
4288e 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
4288f 20 69 73 20 63 61 6c 6c 65 64 20 61 73 20 74 68   is called as th
42890 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 6f 66  e second part of
42891 20 65 61 63 68 20 78 4e 65 78 74 20 6f 70 65 72   each xNext oper
42892 61 74 69 6f 6e 20 77 68 65 6e 0a 2a 2a 20 69 74  ation when.** it
42893 65 72 61 74 69 6e 67 20 74 68 72 6f 75 67 68 20  erating through 
42894 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 61  the results of a
42895 20 66 75 6c 6c 2d 74 65 78 74 20 71 75 65 72 79   full-text query
42896 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20  . At this point 
42897 74 68 65 0a 2a 2a 20 63 75 72 73 6f 72 20 70 6f  the.** cursor po
42898 69 6e 74 73 20 74 6f 20 61 20 72 6f 77 20 74 68  ints to a row th
42899 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 71  at matches the q
4289a 75 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 2c  uery expression,
4289b 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 66 6f 6c   with the.** fol
4289c 6c 6f 77 69 6e 67 20 63 61 76 65 61 74 73 3a 0a  lowing caveats:.
4289d 2a 2a 0a 2a 2a 20 20 20 2a 20 55 70 20 75 6e 74  **.**   * Up unt
4289e 69 6c 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 22  il this point, "
4289f 4e 45 41 52 22 20 6f 70 65 72 61 74 6f 72 73 20  NEAR" operators 
428a0 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  in the expressio
428a1 6e 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 20  n have been.**  
428a2 20 20 20 74 72 65 61 74 65 64 20 61 73 20 22 41     treated as "A
428a3 4e 44 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 44  ND"..**.**   * D
428a4 65 66 65 72 72 65 64 20 74 6f 6b 65 6e 73 20 68  eferred tokens h
428a5 61 76 65 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  ave not yet been
428a6 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 2a 2a 0a   considered..**.
428a7 2a 2a 20 49 66 20 2a 70 52 63 20 69 73 20 6e 6f  ** If *pRc is no
428a8 74 20 53 51 4c 49 54 45 5f 4f 4b 20 77 68 65 6e  t SQLITE_OK when
428a9 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
428aa 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 6d 6d  s called, it imm
428ab 65 64 69 61 74 65 6c 79 0a 2a 2a 20 72 65 74 75  ediately.** retu
428ac 72 6e 73 20 30 2e 20 4f 74 68 65 72 77 69 73 65  rns 0. Otherwise
428ad 2c 20 69 74 20 74 65 73 74 73 20 77 68 65 74 68  , it tests wheth
428ae 65 72 20 6f 72 20 6e 6f 74 20 61 66 74 65 72 20  er or not after 
428af 63 6f 6e 73 69 64 65 72 69 6e 67 20 4e 45 41 52  considering NEAR
428b0 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 73 20 61 6e  .** operators an
428b1 64 20 64 65 66 65 72 72 65 64 20 74 6f 6b 65 6e  d deferred token
428b2 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  s the current ro
428b3 77 20 69 73 20 73 74 69 6c 6c 20 61 20 6d 61 74  w is still a mat
428b4 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 65 78  ch for the.** ex
428b5 70 72 65 73 73 69 6f 6e 2e 20 49 74 20 72 65 74  pression. It ret
428b6 75 72 6e 73 20 31 20 69 66 20 62 6f 74 68 20 6f  urns 1 if both o
428b7 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
428b8 61 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20  are true:.**.** 
428b9 20 20 31 2e 20 2a 70 52 63 20 69 73 20 53 51 4c    1. *pRc is SQL
428ba 49 54 45 5f 4f 4b 20 77 68 65 6e 20 74 68 69 73  ITE_OK when this
428bb 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
428bc 73 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 32  s, and.**.**   2
428bd 2e 20 41 66 74 65 72 20 73 63 61 6e 6e 69 6e 67  . After scanning
428be 20 74 68 65 20 63 75 72 72 65 6e 74 20 46 54 53   the current FTS
428bf 20 74 61 62 6c 65 20 72 6f 77 20 66 6f 72 20 74   table row for t
428c0 68 65 20 64 65 66 65 72 72 65 64 20 74 6f 6b 65  he deferred toke
428c1 6e 73 2c 0a 2a 2a 20 20 20 20 20 20 69 74 20 69  ns,.**      it i
428c2 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68 61  s determined tha
428c3 74 20 74 68 65 20 72 6f 77 20 64 6f 65 73 20 2a  t the row does *
428c4 6e 6f 74 2a 20 6d 61 74 63 68 20 74 68 65 20 71  not* match the q
428c5 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20  uery..**.** Or, 
428c6 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75  if no error occu
428c7 72 73 20 61 6e 64 20 69 74 20 73 65 65 6d 73 20  rs and it seems 
428c8 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
428c9 64 6f 65 73 20 6d 61 74 63 68 20 74 68 65 20 46  does match the F
428ca 54 53 0a 2a 2a 20 71 75 65 72 79 2c 20 72 65 74  TS.** query, ret
428cb 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63  urn 0..*/.static
428cc 20 69 6e 74 20 66 74 73 33 45 76 61 6c 54 65 73   int fts3EvalTes
428cd 74 44 65 66 65 72 72 65 64 41 6e 64 4e 65 61 72  tDeferredAndNear
428ce 28 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73  (Fts3Cursor *pCs
428cf 72 2c 20 69 6e 74 20 2a 70 52 63 29 7b 0a 20 20  r, int *pRc){.  
428d0 69 6e 74 20 72 63 20 3d 20 2a 70 52 63 3b 0a 20  int rc = *pRc;. 
428d1 20 69 6e 74 20 62 4d 69 73 73 20 3d 20 30 3b 0a   int bMiss = 0;.
428d2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
428d3 5f 4f 4b 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 49  _OK ){..    /* I
428d4 66 20 74 68 65 72 65 20 61 72 65 20 6f 6e 65 20  f there are one 
428d5 6f 72 20 6d 6f 72 65 20 64 65 66 65 72 72 65 64  or more deferred
428d6 20 74 6f 6b 65 6e 73 2c 20 6c 6f 61 64 20 74 68   tokens, load th
428d7 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 69 6e  e current row in
428d8 74 6f 0a 20 20 20 20 2a 2a 20 6d 65 6d 6f 72 79  to.    ** memory
428d9 20 61 6e 64 20 73 63 61 6e 20 69 74 20 74 6f 20   and scan it to 
428da 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 70 6f  determine the po
428db 73 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20  sition list for 
428dc 65 61 63 68 20 64 65 66 65 72 72 65 64 0a 20 20  each deferred.  
428dd 20 20 2a 2a 20 74 6f 6b 65 6e 2e 20 54 68 65 6e    ** token. Then
428de 2c 20 73 65 65 20 69 66 20 74 68 69 73 20 72 6f  , see if this ro
428df 77 20 69 73 20 72 65 61 6c 6c 79 20 61 20 6d 61  w is really a ma
428e0 74 63 68 2c 20 63 6f 6e 73 69 64 65 72 69 6e 67  tch, considering
428e1 20 64 65 66 65 72 72 65 64 0a 20 20 20 20 2a 2a   deferred.    **
428e2 20 74 6f 6b 65 6e 73 20 61 6e 64 20 4e 45 41 52   tokens and NEAR
428e3 20 6f 70 65 72 61 74 6f 72 73 20 28 6e 65 69 74   operators (neit
428e4 68 65 72 20 6f 66 20 77 68 69 63 68 20 77 65 72  her of which wer
428e5 65 20 74 61 6b 65 6e 20 69 6e 74 6f 20 61 63 63  e taken into acc
428e6 6f 75 6e 74 0a 20 20 20 20 2a 2a 20 65 61 72 6c  ount.    ** earl
428e7 69 65 72 2c 20 62 79 20 66 74 73 33 45 76 61 6c  ier, by fts3Eval
428e8 4e 65 78 74 52 6f 77 28 29 29 2e 20 0a 20 20 20  NextRow()). .   
428e9 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 73 72   */.    if( pCsr
428ea 2d 3e 70 44 65 66 65 72 72 65 64 20 29 7b 0a 20  ->pDeferred ){. 
428eb 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 43 75       rc = fts3Cu
428ec 72 73 6f 72 53 65 65 6b 28 30 2c 20 70 43 73 72  rsorSeek(0, pCsr
428ed 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
428ee 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
428ef 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
428f0 65 33 46 74 73 33 43 61 63 68 65 44 65 66 65 72  e3Fts3CacheDefer
428f1 72 65 64 44 6f 63 6c 69 73 74 73 28 70 43 73 72  redDoclists(pCsr
428f2 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
428f3 0a 20 20 20 20 62 4d 69 73 73 20 3d 20 28 30 3d  .    bMiss = (0=
428f4 3d 66 74 73 33 45 76 61 6c 54 65 73 74 45 78 70  =fts3EvalTestExp
428f5 72 28 70 43 73 72 2c 20 70 43 73 72 2d 3e 70 45  r(pCsr, pCsr->pE
428f6 78 70 72 2c 20 26 72 63 29 29 3b 0a 0a 20 20 20  xpr, &rc));..   
428f7 20 2f 2a 20 46 72 65 65 20 74 68 65 20 70 6f 73   /* Free the pos
428f8 69 74 69 6f 6e 2d 6c 69 73 74 73 20 61 63 63 75  ition-lists accu
428f9 6d 75 6c 61 74 65 64 20 66 6f 72 20 65 61 63 68  mulated for each
428fa 20 64 65 66 65 72 72 65 64 20 74 6f 6b 65 6e 20   deferred token 
428fb 61 62 6f 76 65 2e 20 2a 2f 0a 20 20 20 20 73 71  above. */.    sq
428fc 6c 69 74 65 33 46 74 73 33 46 72 65 65 44 65 66  lite3Fts3FreeDef
428fd 65 72 72 65 64 44 6f 63 6c 69 73 74 73 28 70 43  erredDoclists(pC
428fe 73 72 29 3b 0a 20 20 20 20 2a 70 52 63 20 3d 20  sr);.    *pRc = 
428ff 72 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  rc;.  }.  return
42900 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20   (rc==SQLITE_OK 
42901 26 26 20 62 4d 69 73 73 29 3b 0a 7d 0a 0a 2f 2a  && bMiss);.}../*
42902 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74  .** Advance to t
42903 68 65 20 6e 65 78 74 20 64 6f 63 75 6d 65 6e 74  he next document
42904 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68   that matches th
42905 65 20 46 54 53 20 65 78 70 72 65 73 73 69 6f 6e  e FTS expression
42906 20 69 6e 0a 2a 2a 20 46 74 73 33 43 75 72 73 6f   in.** Fts3Curso
42907 72 2e 70 45 78 70 72 2e 0a 2a 2f 0a 73 74 61 74  r.pExpr..*/.stat
42908 69 63 20 69 6e 74 20 66 74 73 33 45 76 61 6c 4e  ic int fts3EvalN
42909 65 78 74 28 46 74 73 33 43 75 72 73 6f 72 20 2a  ext(Fts3Cursor *
4290a 70 43 73 72 29 7b 0a 20 20 69 6e 74 20 72 63 20  pCsr){.  int rc 
4290b 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
4290c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
4290d 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 46 74 73  rn Code */.  Fts
4290e 33 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70  3Expr *pExpr = p
4290f 43 73 72 2d 3e 70 45 78 70 72 3b 0a 20 20 61 73  Csr->pExpr;.  as
42910 73 65 72 74 28 20 70 43 73 72 2d 3e 69 73 45 6f  sert( pCsr->isEo
42911 66 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 45  f==0 );.  if( pE
42912 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43  xpr==0 ){.    pC
42913 73 72 2d 3e 69 73 45 6f 66 20 3d 20 31 3b 0a 20  sr->isEof = 1;. 
42914 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 20 7b   }else{.    do {
42915 0a 20 20 20 20 20 20 69 66 28 20 70 43 73 72 2d  .      if( pCsr-
42916 3e 69 73 52 65 71 75 69 72 65 53 65 65 6b 3d 3d  >isRequireSeek==
42917 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
42918 69 74 65 33 5f 72 65 73 65 74 28 70 43 73 72 2d  ite3_reset(pCsr-
42919 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 7d  >pStmt);.      }
4291a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73  .      assert( s
4291b 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e  qlite3_data_coun
4291c 74 28 70 43 73 72 2d 3e 70 53 74 6d 74 29 3d 3d  t(pCsr->pStmt)==
4291d 30 20 29 3b 0a 20 20 20 20 20 20 66 74 73 33 45  0 );.      fts3E
4291e 76 61 6c 4e 65 78 74 52 6f 77 28 70 43 73 72 2c  valNextRow(pCsr,
4291f 20 70 45 78 70 72 2c 20 26 72 63 29 3b 0a 20 20   pExpr, &rc);.  
42920 20 20 20 20 70 43 73 72 2d 3e 69 73 45 6f 66 20      pCsr->isEof 
42921 3d 20 70 45 78 70 72 2d 3e 62 45 6f 66 3b 0a 20  = pExpr->bEof;. 
42922 20 20 20 20 20 70 43 73 72 2d 3e 69 73 52 65 71       pCsr->isReq
42923 75 69 72 65 53 65 65 6b 20 3d 20 31 3b 0a 20 20  uireSeek = 1;.  
42924 20 20 20 20 70 43 73 72 2d 3e 69 73 4d 61 74 63      pCsr->isMatc
42925 68 69 6e 66 6f 4e 65 65 64 65 64 20 3d 20 31 3b  hinfoNeeded = 1;
42926 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 69 50 72  .      pCsr->iPr
42927 65 76 49 64 20 3d 20 70 45 78 70 72 2d 3e 69 44  evId = pExpr->iD
42928 6f 63 69 64 3b 0a 20 20 20 20 7d 77 68 69 6c 65  ocid;.    }while
42929 28 20 70 43 73 72 2d 3e 69 73 45 6f 66 3d 3d 30  ( pCsr->isEof==0
4292a 20 26 26 20 66 74 73 33 45 76 61 6c 54 65 73 74   && fts3EvalTest
4292b 44 65 66 65 72 72 65 64 41 6e 64 4e 65 61 72 28  DeferredAndNear(
4292c 70 43 73 72 2c 20 26 72 63 29 20 29 3b 0a 20 20  pCsr, &rc) );.  
4292d 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
4292e 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 61 72 74 20  ../*.** Restart 
4292f 69 6e 74 65 72 61 74 69 6f 6e 20 66 6f 72 20 65  interation for e
42930 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20  xpression pExpr 
42931 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74  so that the next
42932 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 66 74 73 33   call to.** fts3
42933 45 76 61 6c 4e 65 78 74 28 29 20 76 69 73 69 74  EvalNext() visit
42934 73 20 74 68 65 20 66 69 72 73 74 20 72 6f 77 2e  s the first row.
42935 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 69 6e   Do not allow in
42936 63 72 65 6d 65 6e 74 61 6c 20 0a 2a 2a 20 6c 6f  cremental .** lo
42937 61 64 69 6e 67 20 6f 72 20 6d 65 72 67 69 6e 67  ading or merging
42938 20 6f 66 20 70 68 72 61 73 65 20 64 6f 63 6c 69   of phrase docli
42939 73 74 73 20 66 6f 72 20 74 68 69 73 20 69 74 65  sts for this ite
4293a 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  ration..**.** If
4293b 20 2a 70 52 63 20 69 73 20 6f 74 68 65 72 20 74   *pRc is other t
4293c 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 77 68  han SQLITE_OK wh
4293d 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
4293e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69   is called, it i
4293f 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 20 49 66  s.** a no-op. If
42940 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
42941 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e   within this fun
42942 63 74 69 6f 6e 2c 20 2a 70 52 63 20 69 73 20 73  ction, *pRc is s
42943 65 74 20 74 6f 20 61 6e 0a 2a 2a 20 53 51 4c 69  et to an.** SQLi
42944 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 62 65  te error code be
42945 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a  fore returning..
42946 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
42947 74 73 33 45 76 61 6c 52 65 73 74 61 72 74 28 0a  ts3EvalRestart(.
42948 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43    Fts3Cursor *pC
42949 73 72 2c 0a 20 20 46 74 73 33 45 78 70 72 20 2a  sr,.  Fts3Expr *
4294a 70 45 78 70 72 2c 0a 20 20 69 6e 74 20 2a 70 52  pExpr,.  int *pR
4294b 63 0a 29 7b 0a 20 20 69 66 28 20 70 45 78 70 72  c.){.  if( pExpr
4294c 20 26 26 20 2a 70 52 63 3d 3d 53 51 4c 49 54 45   && *pRc==SQLITE
4294d 5f 4f 4b 20 29 7b 0a 20 20 20 20 46 74 73 33 50  _OK ){.    Fts3P
4294e 68 72 61 73 65 20 2a 70 50 68 72 61 73 65 20 3d  hrase *pPhrase =
4294f 20 70 45 78 70 72 2d 3e 70 50 68 72 61 73 65 3b   pExpr->pPhrase;
42950 0a 0a 20 20 20 20 69 66 28 20 70 50 68 72 61 73  ..    if( pPhras
42951 65 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33 45  e ){.      fts3E
42952 76 61 6c 49 6e 76 61 6c 69 64 61 74 65 50 6f 73  valInvalidatePos
42953 6c 69 73 74 28 70 50 68 72 61 73 65 29 3b 0a 20  list(pPhrase);. 
42954 20 20 20 20 20 69 66 28 20 70 50 68 72 61 73 65       if( pPhrase
42955 2d 3e 62 49 6e 63 72 20 29 7b 0a 20 20 20 20 20  ->bIncr ){.     
42956 20 20 20 61 73 73 65 72 74 28 20 70 50 68 72 61     assert( pPhra
42957 73 65 2d 3e 6e 54 6f 6b 65 6e 3d 3d 31 20 29 3b  se->nToken==1 );
42958 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
42959 20 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e   pPhrase->aToken
4295a 5b 30 5d 2e 70 53 65 67 63 73 72 20 29 3b 0a 20  [0].pSegcsr );. 
4295b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74         sqlite3Ft
4295c 73 33 4d 73 72 49 6e 63 72 52 65 73 74 61 72 74  s3MsrIncrRestart
4295d 28 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e  (pPhrase->aToken
4295e 5b 30 5d 2e 70 53 65 67 63 73 72 29 3b 0a 20 20  [0].pSegcsr);.  
4295f 20 20 20 20 20 20 2a 70 52 63 20 3d 20 66 74 73        *pRc = fts
42960 33 45 76 61 6c 50 68 72 61 73 65 53 74 61 72 74  3EvalPhraseStart
42961 28 70 43 73 72 2c 20 30 2c 20 70 50 68 72 61 73  (pCsr, 0, pPhras
42962 65 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  e);.      }..   
42963 20 20 20 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c     pPhrase->docl
42964 69 73 74 2e 70 4e 65 78 74 44 6f 63 69 64 20 3d  ist.pNextDocid =
42965 20 30 3b 0a 20 20 20 20 20 20 70 50 68 72 61 73   0;.      pPhras
42966 65 2d 3e 64 6f 63 6c 69 73 74 2e 69 44 6f 63 69  e->doclist.iDoci
42967 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20  d = 0;.    }..  
42968 20 20 70 45 78 70 72 2d 3e 69 44 6f 63 69 64 20    pExpr->iDocid 
42969 3d 20 30 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e  = 0;.    pExpr->
4296a 62 45 6f 66 20 3d 20 30 3b 0a 20 20 20 20 70 45  bEof = 0;.    pE
4296b 78 70 72 2d 3e 62 53 74 61 72 74 20 3d 20 30 3b  xpr->bStart = 0;
4296c 0a 0a 20 20 20 20 66 74 73 33 45 76 61 6c 52 65  ..    fts3EvalRe
4296d 73 74 61 72 74 28 70 43 73 72 2c 20 70 45 78 70  start(pCsr, pExp
4296e 72 2d 3e 70 4c 65 66 74 2c 20 70 52 63 29 3b 0a  r->pLeft, pRc);.
4296f 20 20 20 20 66 74 73 33 45 76 61 6c 52 65 73 74      fts3EvalRest
42970 61 72 74 28 70 43 73 72 2c 20 70 45 78 70 72 2d  art(pCsr, pExpr-
42971 3e 70 52 69 67 68 74 2c 20 70 52 63 29 3b 0a 20  >pRight, pRc);. 
42972 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 66 74 65   }.}../*.** Afte
42973 72 20 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 65  r allocating the
42974 20 46 74 73 33 45 78 70 72 2e 61 4d 49 5b 5d 20   Fts3Expr.aMI[] 
42975 61 72 72 61 79 20 66 6f 72 20 65 61 63 68 20 70  array for each p
42976 68 72 61 73 65 20 69 6e 20 74 68 65 20 0a 2a 2a  hrase in the .**
42977 20 65 78 70 72 65 73 73 69 6f 6e 20 72 6f 6f 74   expression root
42978 65 64 20 61 74 20 70 45 78 70 72 2c 20 74 68 65  ed at pExpr, the
42979 20 63 75 72 73 6f 72 20 69 74 65 72 61 74 65 73   cursor iterates
4297a 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 72 6f 77   through all row
4297b 73 20 6d 61 74 63 68 65 64 0a 2a 2a 20 62 79 20  s matched.** by 
4297c 70 45 78 70 72 2c 20 63 61 6c 6c 69 6e 67 20 74  pExpr, calling t
4297d 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  his function for
4297e 20 65 61 63 68 20 72 6f 77 2e 20 54 68 69 73 20   each row. This 
4297f 66 75 6e 63 74 69 6f 6e 20 69 6e 63 72 65 6d 65  function increme
42980 6e 74 73 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65  nts.** the value
42981 73 20 69 6e 20 46 74 73 33 45 78 70 72 2e 61 4d  s in Fts3Expr.aM
42982 49 5b 5d 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  I[] according to
42983 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 2d 6c 69   the position-li
42984 73 74 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20  st currently.** 
42985 66 6f 75 6e 64 20 69 6e 20 46 74 73 33 45 78 70  found in Fts3Exp
42986 72 2e 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69  r.pPhrase->docli
42987 73 74 2e 70 4c 69 73 74 20 66 6f 72 20 65 61 63  st.pList for eac
42988 68 20 6f 66 20 74 68 65 20 70 68 72 61 73 65 20  h of the phrase 
42989 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6e  .** expression n
4298a 6f 64 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  odes..*/.static 
4298b 76 6f 69 64 20 66 74 73 33 45 76 61 6c 55 70 64  void fts3EvalUpd
4298c 61 74 65 43 6f 75 6e 74 73 28 46 74 73 33 45 78  ateCounts(Fts3Ex
4298d 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 66  pr *pExpr){.  if
4298e 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 46  ( pExpr ){.    F
4298f 74 73 33 50 68 72 61 73 65 20 2a 70 50 68 72 61  ts3Phrase *pPhra
42990 73 65 20 3d 20 70 45 78 70 72 2d 3e 70 50 68 72  se = pExpr->pPhr
42991 61 73 65 3b 0a 20 20 20 20 69 66 28 20 70 50 68  ase;.    if( pPh
42992 72 61 73 65 20 26 26 20 70 50 68 72 61 73 65 2d  rase && pPhrase-
42993 3e 64 6f 63 6c 69 73 74 2e 70 4c 69 73 74 20 29  >doclist.pList )
42994 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  {.      int iCol
42995 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 68 61 72   = 0;.      char
42996 20 2a 70 20 3d 20 70 50 68 72 61 73 65 2d 3e 64   *p = pPhrase->d
42997 6f 63 6c 69 73 74 2e 70 4c 69 73 74 3b 0a 0a 20  oclist.pList;.. 
42998 20 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 20       assert( *p 
42999 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  );.      while( 
4299a 31 20 29 7b 0a 20 20 20 20 20 20 20 20 75 38 20  1 ){.        u8 
4299b 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69  c = 0;.        i
4299c 6e 74 20 69 43 6e 74 20 3d 20 30 3b 0a 20 20 20  nt iCnt = 0;.   
4299d 20 20 20 20 20 77 68 69 6c 65 28 20 30 78 46 45       while( 0xFE
4299e 20 26 20 28 2a 70 20 7c 20 63 29 20 29 7b 0a 20   & (*p | c) ){. 
4299f 20 20 20 20 20 20 20 20 20 69 66 28 20 28 63 26           if( (c&
429a0 30 78 38 30 29 3d 3d 30 20 29 20 69 43 6e 74 2b  0x80)==0 ) iCnt+
429a1 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d  +;.          c =
429a2 20 2a 70 2b 2b 20 26 20 30 78 38 30 3b 0a 20 20   *p++ & 0x80;.  
429a3 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
429a4 20 2f 2a 20 61 4d 49 5b 69 43 6f 6c 2a 33 20 2b   /* aMI[iCol*3 +
429a5 20 31 5d 20 3d 20 4e 75 6d 62 65 72 20 6f 66 20   1] = Number of 
429a6 6f 63 63 75 72 72 65 6e 63 65 73 0a 20 20 20 20  occurrences.    
429a7 20 20 20 20 2a 2a 20 61 4d 49 5b 69 43 6f 6c 2a      ** aMI[iCol*
429a8 33 20 2b 20 32 5d 20 3d 20 4e 75 6d 62 65 72 20  3 + 2] = Number 
429a9 6f 66 20 72 6f 77 73 20 63 6f 6e 74 61 69 6e 69  of rows containi
429aa 6e 67 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  ng at least one 
429ab 69 6e 73 74 61 6e 63 65 0a 20 20 20 20 20 20 20  instance.       
429ac 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 45 78 70   */.        pExp
429ad 72 2d 3e 61 4d 49 5b 69 43 6f 6c 2a 33 20 2b 20  r->aMI[iCol*3 + 
429ae 31 5d 20 2b 3d 20 69 43 6e 74 3b 0a 20 20 20 20  1] += iCnt;.    
429af 20 20 20 20 70 45 78 70 72 2d 3e 61 4d 49 5b 69      pExpr->aMI[i
429b0 43 6f 6c 2a 33 20 2b 20 32 5d 20 2b 3d 20 28 69  Col*3 + 2] += (i
429b1 43 6e 74 3e 30 29 3b 0a 20 20 20 20 20 20 20 20  Cnt>0);.        
429b2 69 66 28 20 2a 70 3d 3d 30 78 30 30 20 29 20 62  if( *p==0x00 ) b
429b3 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 70 2b  reak;.        p+
429b4 2b 3b 0a 20 20 20 20 20 20 20 20 70 20 2b 3d 20  +;.        p += 
429b5 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61  sqlite3Fts3GetVa
429b6 72 69 6e 74 33 32 28 70 2c 20 26 69 43 6f 6c 29  rint32(p, &iCol)
429b7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
429b8 0a 20 20 20 20 66 74 73 33 45 76 61 6c 55 70 64  .    fts3EvalUpd
429b9 61 74 65 43 6f 75 6e 74 73 28 70 45 78 70 72 2d  ateCounts(pExpr-
429ba 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 66 74 73  >pLeft);.    fts
429bb 33 45 76 61 6c 55 70 64 61 74 65 43 6f 75 6e 74  3EvalUpdateCount
429bc 73 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  s(pExpr->pRight)
429bd 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  ;.  }.}../*.** E
429be 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20  xpression pExpr 
429bf 6d 75 73 74 20 62 65 20 6f 66 20 74 79 70 65 20  must be of type 
429c0 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 2e  FTSQUERY_PHRASE.
429c1 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20  .**.** If it is 
429c2 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f  not already allo
429c3 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61  cated and popula
429c4 74 65 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69  ted, this functi
429c5 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 6e 64  on allocates and
429c6 0a 2a 2a 20 70 6f 70 75 6c 61 74 65 73 20 74 68  .** populates th
429c7 65 20 46 74 73 33 45 78 70 72 2e 61 4d 49 5b 5d  e Fts3Expr.aMI[]
429c8 20 61 72 72 61 79 20 66 6f 72 20 65 78 70 72 65   array for expre
429c9 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 49 66 20  ssion pExpr. If 
429ca 70 45 78 70 72 20 69 73 20 70 61 72 74 0a 2a 2a  pExpr is part.**
429cb 20 6f 66 20 61 20 4e 45 41 52 20 65 78 70 72 65   of a NEAR expre
429cc 73 73 69 6f 6e 2c 20 74 68 65 6e 20 69 74 20 61  ssion, then it a
429cd 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 73 20 61 6e  lso allocates an
429ce 64 20 70 6f 70 75 6c 61 74 65 73 20 74 68 65 20  d populates the 
429cf 73 61 6d 65 20 61 72 72 61 79 0a 2a 2a 20 66 6f  same array.** fo
429d0 72 20 61 6c 6c 20 6f 74 68 65 72 20 70 68 72 61  r all other phra
429d1 73 65 73 20 74 68 61 74 20 61 72 65 20 70 61 72  ses that are par
429d2 74 20 6f 66 20 74 68 65 20 4e 45 41 52 20 65 78  t of the NEAR ex
429d3 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  pression..**.** 
429d4 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
429d5 75 72 6e 65 64 20 69 66 20 74 68 65 20 61 4d 49  urned if the aMI
429d6 5b 5d 20 61 72 72 61 79 20 69 73 20 73 75 63 63  [] array is succ
429d7 65 73 73 66 75 6c 6c 79 20 61 6c 6c 6f 63 61 74  essfully allocat
429d8 65 64 20 61 6e 64 0a 2a 2a 20 70 6f 70 75 6c 61  ed and.** popula
429d9 74 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ted. Otherwise, 
429da 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  if an error occu
429db 72 73 2c 20 61 6e 20 53 51 4c 69 74 65 20 65 72  rs, an SQLite er
429dc 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
429dd 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rned..*/.static 
429de 69 6e 74 20 66 74 73 33 45 76 61 6c 47 61 74 68  int fts3EvalGath
429df 65 72 53 74 61 74 73 28 0a 20 20 46 74 73 33 43  erStats(.  Fts3C
429e0 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20  ursor *pCsr,    
429e1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
429e2 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  rsor object */. 
429e3 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72   Fts3Expr *pExpr
429e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
429e5 20 2f 2a 20 46 54 53 51 55 45 52 59 5f 50 48 52   /* FTSQUERY_PHR
429e6 41 53 45 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  ASE expression *
429e7 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  /.){.  int rc = 
429e8 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
429e9 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
429ea 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 61 73 73 65   code */..  asse
429eb 72 74 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65  rt( pExpr->eType
429ec 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  ==FTSQUERY_PHRAS
429ed 45 20 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72  E );.  if( pExpr
429ee 2d 3e 61 4d 49 3d 3d 30 20 29 7b 0a 20 20 20 20  ->aMI==0 ){.    
429ef 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61 62 20  Fts3Table *pTab 
429f0 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70  = (Fts3Table *)p
429f1 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b  Csr->base.pVtab;
429f2 0a 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70  .    Fts3Expr *p
429f3 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20  Root;           
429f4 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6f 66 20       /* Root of 
429f5 4e 45 41 52 20 65 78 70 72 65 73 73 69 6f 6e 20  NEAR expression 
429f6 2a 2f 0a 20 20 20 20 46 74 73 33 45 78 70 72 20  */.    Fts3Expr 
429f7 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *p;             
429f8 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
429f9 6f 72 20 75 73 65 64 20 66 6f 72 20 73 65 76 65  or used for seve
429fa 72 61 6c 20 70 75 72 70 6f 73 65 73 20 2a 2f 0a  ral purposes */.
429fb 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  .    sqlite3_int
429fc 36 34 20 69 50 72 65 76 49 64 20 3d 20 70 43 73  64 iPrevId = pCs
429fd 72 2d 3e 69 50 72 65 76 49 64 3b 0a 20 20 20 20  r->iPrevId;.    
429fe 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 44  sqlite3_int64 iD
429ff 6f 63 69 64 3b 0a 20 20 20 20 75 38 20 62 45 6f  ocid;.    u8 bEo
42a00 66 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20  f;..    /* Find 
42a01 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20  the root of the 
42a02 4e 45 41 52 20 65 78 70 72 65 73 73 69 6f 6e 20  NEAR expression 
42a03 2a 2f 0a 20 20 20 20 70 52 6f 6f 74 20 3d 20 70  */.    pRoot = p
42a04 45 78 70 72 3b 0a 20 20 20 20 77 68 69 6c 65 28  Expr;.    while(
42a05 20 70 52 6f 6f 74 2d 3e 70 50 61 72 65 6e 74 20   pRoot->pParent 
42a06 26 26 20 70 52 6f 6f 74 2d 3e 70 50 61 72 65 6e  && pRoot->pParen
42a07 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  t->eType==FTSQUE
42a08 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 20  RY_NEAR ){.     
42a09 20 70 52 6f 6f 74 20 3d 20 70 52 6f 6f 74 2d 3e   pRoot = pRoot->
42a0a 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 7d 0a 20  pParent;.    }. 
42a0b 20 20 20 69 44 6f 63 69 64 20 3d 20 70 52 6f 6f     iDocid = pRoo
42a0c 74 2d 3e 69 44 6f 63 69 64 3b 0a 20 20 20 20 62  t->iDocid;.    b
42a0d 45 6f 66 20 3d 20 70 52 6f 6f 74 2d 3e 62 45 6f  Eof = pRoot->bEo
42a0e 66 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  f;.    assert( p
42a0f 52 6f 6f 74 2d 3e 62 53 74 61 72 74 20 29 3b 0a  Root->bStart );.
42a10 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  .    /* Allocate
42a11 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 61   space for the a
42a12 4d 53 49 5b 5d 20 61 72 72 61 79 20 6f 66 20 65  MSI[] array of e
42a13 61 63 68 20 46 54 53 51 55 45 52 59 5f 50 48 52  ach FTSQUERY_PHR
42a14 41 53 45 20 6e 6f 64 65 20 2a 2f 0a 20 20 20 20  ASE node */.    
42a15 66 6f 72 28 70 3d 70 52 6f 6f 74 3b 20 70 3b 20  for(p=pRoot; p; 
42a16 70 3d 70 2d 3e 70 4c 65 66 74 29 7b 0a 20 20 20  p=p->pLeft){.   
42a17 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 45 20     Fts3Expr *pE 
42a18 3d 20 28 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53  = (p->eType==FTS
42a19 51 55 45 52 59 5f 50 48 52 41 53 45 3f 70 3a 70  QUERY_PHRASE?p:p
42a1a 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20  ->pRight);.     
42a1b 20 61 73 73 65 72 74 28 20 70 45 2d 3e 61 4d 49   assert( pE->aMI
42a1c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 45 2d  ==0 );.      pE-
42a1d 3e 61 4d 49 20 3d 20 28 75 33 32 20 2a 29 73 71  >aMI = (u32 *)sq
42a1e 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 70 54 61  lite3_malloc(pTa
42a1f 62 2d 3e 6e 43 6f 6c 75 6d 6e 20 2a 20 33 20 2a  b->nColumn * 3 *
42a20 20 73 69 7a 65 6f 66 28 75 33 32 29 29 3b 0a 20   sizeof(u32));. 
42a21 20 20 20 20 20 69 66 28 20 21 70 45 2d 3e 61 4d       if( !pE->aM
42a22 49 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  I ) return SQLIT
42a23 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 6d  E_NOMEM;.      m
42a24 65 6d 73 65 74 28 70 45 2d 3e 61 4d 49 2c 20 30  emset(pE->aMI, 0
42a25 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 75 6d 6e 20  , pTab->nColumn 
42a26 2a 20 33 20 2a 20 73 69 7a 65 6f 66 28 75 33 32  * 3 * sizeof(u32
42a27 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 66  ));.    }..    f
42a28 74 73 33 45 76 61 6c 52 65 73 74 61 72 74 28 70  ts3EvalRestart(p
42a29 43 73 72 2c 20 70 52 6f 6f 74 2c 20 26 72 63 29  Csr, pRoot, &rc)
42a2a 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43  ;..    while( pC
42a2b 73 72 2d 3e 69 73 45 6f 66 3d 3d 30 20 26 26 20  sr->isEof==0 && 
42a2c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
42a2d 0a 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20  ..      do {.   
42a2e 20 20 20 20 20 2f 2a 20 45 6e 73 75 72 65 20 74       /* Ensure t
42a2f 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 73 74 61  he %_content sta
42a30 74 65 6d 65 6e 74 20 69 73 20 72 65 73 65 74 2e  tement is reset.
42a31 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
42a32 70 43 73 72 2d 3e 69 73 52 65 71 75 69 72 65 53  pCsr->isRequireS
42a33 65 65 6b 3d 3d 30 20 29 20 73 71 6c 69 74 65 33  eek==0 ) sqlite3
42a34 5f 72 65 73 65 74 28 70 43 73 72 2d 3e 70 53 74  _reset(pCsr->pSt
42a35 6d 74 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  mt);.        ass
42a36 65 72 74 28 20 73 71 6c 69 74 65 33 5f 64 61 74  ert( sqlite3_dat
42a37 61 5f 63 6f 75 6e 74 28 70 43 73 72 2d 3e 70 53  a_count(pCsr->pS
42a38 74 6d 74 29 3d 3d 30 20 29 3b 0a 0a 20 20 20 20  tmt)==0 );..    
42a39 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74      /* Advance t
42a3a 6f 20 74 68 65 20 6e 65 78 74 20 64 6f 63 75 6d  o the next docum
42a3b 65 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 66  ent */.        f
42a3c 74 73 33 45 76 61 6c 4e 65 78 74 52 6f 77 28 70  ts3EvalNextRow(p
42a3d 43 73 72 2c 20 70 52 6f 6f 74 2c 20 26 72 63 29  Csr, pRoot, &rc)
42a3e 3b 0a 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e  ;.        pCsr->
42a3f 69 73 45 6f 66 20 3d 20 70 52 6f 6f 74 2d 3e 62  isEof = pRoot->b
42a40 45 6f 66 3b 0a 20 20 20 20 20 20 20 20 70 43 73  Eof;.        pCs
42a41 72 2d 3e 69 73 52 65 71 75 69 72 65 53 65 65 6b  r->isRequireSeek
42a42 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 43   = 1;.        pC
42a43 73 72 2d 3e 69 73 4d 61 74 63 68 69 6e 66 6f 4e  sr->isMatchinfoN
42a44 65 65 64 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  eeded = 1;.     
42a45 20 20 20 70 43 73 72 2d 3e 69 50 72 65 76 49 64     pCsr->iPrevId
42a46 20 3d 20 70 52 6f 6f 74 2d 3e 69 44 6f 63 69 64   = pRoot->iDocid
42a47 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20  ;.      }while( 
42a48 70 43 73 72 2d 3e 69 73 45 6f 66 3d 3d 30 20 0a  pCsr->isEof==0 .
42a49 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70 52             && pR
42a4a 6f 6f 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51  oot->eType==FTSQ
42a4b 55 45 52 59 5f 4e 45 41 52 20 0a 20 20 20 20 20  UERY_NEAR .     
42a4c 20 20 20 20 20 20 26 26 20 66 74 73 33 45 76 61        && fts3Eva
42a4d 6c 54 65 73 74 44 65 66 65 72 72 65 64 41 6e 64  lTestDeferredAnd
42a4e 4e 65 61 72 28 70 43 73 72 2c 20 26 72 63 29 20  Near(pCsr, &rc) 
42a4f 0a 20 20 20 20 20 20 29 3b 0a 0a 20 20 20 20 20  .      );..     
42a50 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
42a51 4f 4b 20 26 26 20 70 43 73 72 2d 3e 69 73 45 6f  OK && pCsr->isEo
42a52 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  f==0 ){.        
42a53 66 74 73 33 45 76 61 6c 55 70 64 61 74 65 43 6f  fts3EvalUpdateCo
42a54 75 6e 74 73 28 70 52 6f 6f 74 29 3b 0a 20 20 20  unts(pRoot);.   
42a55 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
42a56 70 43 73 72 2d 3e 69 73 45 6f 66 20 3d 20 30 3b  pCsr->isEof = 0;
42a57 0a 20 20 20 20 70 43 73 72 2d 3e 69 50 72 65 76  .    pCsr->iPrev
42a58 49 64 20 3d 20 69 50 72 65 76 49 64 3b 0a 0a 20  Id = iPrevId;.. 
42a59 20 20 20 69 66 28 20 62 45 6f 66 20 29 7b 0a 20     if( bEof ){. 
42a5a 20 20 20 20 20 70 52 6f 6f 74 2d 3e 62 45 6f 66       pRoot->bEof
42a5b 20 3d 20 62 45 6f 66 3b 0a 20 20 20 20 7d 65 6c   = bEof;.    }el
42a5c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 75  se{.      /* Cau
42a5d 74 69 6f 6e 3a 20 70 52 6f 6f 74 20 6d 61 79 20  tion: pRoot may 
42a5e 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20  iterate through 
42a5f 64 6f 63 69 64 73 20 69 6e 20 61 73 63 65 6e 64  docids in ascend
42a60 69 6e 67 20 6f 72 20 64 65 73 63 65 6e 64 69 6e  ing or descendin
42a61 67 0a 20 20 20 20 20 20 2a 2a 20 6f 72 64 65 72  g.      ** order
42a62 2e 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f  . For this reaso
42a63 6e 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 69  n, even though i
42a64 74 20 73 65 65 6d 73 20 6d 6f 72 65 20 64 65 66  t seems more def
42a65 65 6e 73 69 76 65 2c 20 74 68 65 20 0a 20 20 20  ensive, the .   
42a66 20 20 20 2a 2a 20 64 6f 20 6c 6f 6f 70 20 63 61     ** do loop ca
42a67 6e 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e  n not be written
42a68 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  :.      **.     
42a69 20 2a 2a 20 20 20 64 6f 20 7b 2e 2e 2e 7d 20 77   **   do {...} w
42a6a 68 69 6c 65 28 20 70 52 6f 6f 74 2d 3e 69 44 6f  hile( pRoot->iDo
42a6b 63 69 64 3c 69 44 6f 63 69 64 20 26 26 20 72 63  cid<iDocid && rc
42a6c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
42a6d 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 66 74       */.      ft
42a6e 73 33 45 76 61 6c 52 65 73 74 61 72 74 28 70 43  s3EvalRestart(pC
42a6f 73 72 2c 20 70 52 6f 6f 74 2c 20 26 72 63 29 3b  sr, pRoot, &rc);
42a70 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  .      do {.    
42a71 20 20 20 20 66 74 73 33 45 76 61 6c 4e 65 78 74      fts3EvalNext
42a72 52 6f 77 28 70 43 73 72 2c 20 70 52 6f 6f 74 2c  Row(pCsr, pRoot,
42a73 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 61   &rc);.        a
42a74 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 62 45  ssert( pRoot->bE
42a75 6f 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d  of==0 );.      }
42a76 77 68 69 6c 65 28 20 70 52 6f 6f 74 2d 3e 69 44  while( pRoot->iD
42a77 6f 63 69 64 21 3d 69 44 6f 63 69 64 20 26 26 20  ocid!=iDocid && 
42a78 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
42a79 0a 20 20 20 20 20 20 66 74 73 33 45 76 61 6c 54  .      fts3EvalT
42a7a 65 73 74 44 65 66 65 72 72 65 64 41 6e 64 4e 65  estDeferredAndNe
42a7b 61 72 28 70 43 73 72 2c 20 26 72 63 29 3b 0a 20  ar(pCsr, &rc);. 
42a7c 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
42a7d 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
42a7e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
42a7f 75 73 65 64 20 62 79 20 74 68 65 20 6d 61 74 63  used by the matc
42a80 68 69 6e 66 6f 28 29 20 6d 6f 64 75 6c 65 20 74  hinfo() module t
42a81 6f 20 71 75 65 72 79 20 61 20 70 68 72 61 73 65  o query a phrase
42a82 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20   .** expression 
42a83 6e 6f 64 65 20 66 6f 72 20 74 68 65 20 66 6f 6c  node for the fol
42a84 6c 6f 77 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  lowing informati
42a85 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 54  on:.**.**   1. T
42a86 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
42a87 6f 66 20 6f 63 63 75 72 72 65 6e 63 65 73 20 6f  of occurrences o
42a88 66 20 74 68 65 20 70 68 72 61 73 65 20 69 6e 20  f the phrase in 
42a89 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 0a  each column of .
42a8a 2a 2a 20 20 20 20 20 20 74 68 65 20 46 54 53 20  **      the FTS 
42a8b 74 61 62 6c 65 20 28 63 6f 6e 73 69 64 65 72 69  table (consideri
42a8c 6e 67 20 61 6c 6c 20 72 6f 77 73 29 2c 20 61 6e  ng all rows), an
42a8d 64 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 46 6f 72  d.**.**   2. For
42a8e 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2c 20 74 68   each column, th
42a8f 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
42a90 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 66 6f   in the table fo
42a91 72 20 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 20  r which the.**  
42a92 20 20 20 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61      column conta
42a93 69 6e 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65  ins at least one
42a94 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
42a95 20 70 68 72 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49   phrase..**.** I
42a96 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72  f no error occur
42a97 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  s, SQLITE_OK is 
42a98 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
42a99 20 76 61 6c 75 65 73 20 66 6f 72 20 65 61 63 68   values for each
42a9a 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 77 72 69 74 74   column.** writt
42a9b 65 6e 20 69 6e 74 6f 20 74 68 65 20 61 72 72 61  en into the arra
42a9c 79 20 61 69 4f 75 74 20 61 73 20 66 6f 6c 6c 6f  y aiOut as follo
42a9d 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 69 4f 75  ws:.**.**   aiOu
42a9e 74 5b 69 43 6f 6c 2a 33 20 2b 20 31 5d 20 3d 20  t[iCol*3 + 1] = 
42a9f 4e 75 6d 62 65 72 20 6f 66 20 6f 63 63 75 72 72  Number of occurr
42aa0 65 6e 63 65 73 0a 2a 2a 20 20 20 61 69 4f 75 74  ences.**   aiOut
42aa1 5b 69 43 6f 6c 2a 33 20 2b 20 32 5d 20 3d 20 4e  [iCol*3 + 2] = N
42aa2 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 63 6f  umber of rows co
42aa3 6e 74 61 69 6e 69 6e 67 20 61 74 20 6c 65 61 73  ntaining at leas
42aa4 74 20 6f 6e 65 20 69 6e 73 74 61 6e 63 65 0a 2a  t one instance.*
42aa5 2a 0a 2a 2a 20 43 61 76 65 61 74 73 3a 0a 2a 2a  *.** Caveats:.**
42aa6 0a 2a 2a 20 20 20 2a 20 49 66 20 61 20 70 68 72  .**   * If a phr
42aa7 61 73 65 20 63 6f 6e 73 69 73 74 73 20 65 6e 74  ase consists ent
42aa8 69 72 65 6c 79 20 6f 66 20 64 65 66 65 72 72 65  irely of deferre
42aa9 64 20 74 6f 6b 65 6e 73 2c 20 74 68 65 6e 20 61  d tokens, then a
42aaa 6c 6c 20 6f 75 74 70 75 74 20 0a 2a 2a 20 20 20  ll output .**   
42aab 20 20 76 61 6c 75 65 73 20 61 72 65 20 73 65 74    values are set
42aac 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
42aad 66 20 64 6f 63 75 6d 65 6e 74 73 20 69 6e 20 74  f documents in t
42aae 68 65 20 74 61 62 6c 65 2e 20 49 6e 20 6f 74 68  he table. In oth
42aaf 65 72 0a 2a 2a 20 20 20 20 20 77 6f 72 64 73 20  er.**     words 
42ab0 77 65 20 61 73 73 75 6d 65 20 74 68 61 74 20 76  we assume that v
42ab1 65 72 79 20 63 6f 6d 6d 6f 6e 20 74 6f 6b 65 6e  ery common token
42ab2 73 20 6f 63 63 75 72 20 65 78 61 63 74 6c 79 20  s occur exactly 
42ab3 6f 6e 63 65 20 69 6e 20 65 61 63 68 20 0a 2a 2a  once in each .**
42ab4 20 20 20 20 20 63 6f 6c 75 6d 6e 20 6f 66 20 65       column of e
42ab5 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65 20 74  ach row of the t
42ab6 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  able..**.**   * 
42ab7 49 66 20 61 20 70 68 72 61 73 65 20 63 6f 6e 74  If a phrase cont
42ab8 61 69 6e 73 20 73 6f 6d 65 20 64 65 66 65 72 72  ains some deferr
42ab9 65 64 20 74 6f 6b 65 6e 73 20 28 61 6e 64 20 73  ed tokens (and s
42aba 6f 6d 65 20 6e 6f 6e 2d 64 65 66 65 72 72 65 64  ome non-deferred
42abb 20 0a 2a 2a 20 20 20 20 20 74 6f 6b 65 6e 73 29   .**     tokens)
42abc 2c 20 63 6f 75 6e 74 20 74 68 65 20 70 6f 74 65  , count the pote
42abd 6e 74 69 61 6c 20 6f 63 63 75 72 72 65 6e 63 65  ntial occurrence
42abe 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 63   identified by c
42abf 6f 6e 73 69 64 65 72 69 6e 67 0a 2a 2a 20 20 20  onsidering.**   
42ac0 20 20 74 68 65 20 6e 6f 6e 2d 64 65 66 65 72 72    the non-deferr
42ac1 65 64 20 74 6f 6b 65 6e 73 20 69 6e 73 74 65 61  ed tokens instea
42ac2 64 20 6f 66 20 61 63 74 75 61 6c 20 70 68 72 61  d of actual phra
42ac3 73 65 20 6f 63 63 75 72 72 65 6e 63 65 73 2e 0a  se occurrences..
42ac4 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65  **.**   * If the
42ac5 20 70 68 72 61 73 65 20 69 73 20 70 61 72 74 20   phrase is part 
42ac6 6f 66 20 61 20 4e 45 41 52 20 65 78 70 72 65 73  of a NEAR expres
42ac7 73 69 6f 6e 2c 20 74 68 65 6e 20 6f 6e 6c 79 20  sion, then only 
42ac8 70 68 72 61 73 65 20 69 6e 73 74 61 6e 63 65 73  phrase instances
42ac9 0a 2a 2a 20 20 20 20 20 74 68 61 74 20 6d 65 65  .**     that mee
42aca 74 20 74 68 65 20 4e 45 41 52 20 63 6f 6e 73 74  t the NEAR const
42acb 72 61 69 6e 74 20 61 72 65 20 69 6e 63 6c 75 64  raint are includ
42acc 65 64 20 69 6e 20 74 68 65 20 63 6f 75 6e 74 73  ed in the counts
42acd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
42ace 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
42acf 74 73 33 45 76 61 6c 50 68 72 61 73 65 53 74 61  ts3EvalPhraseSta
42ad0 74 73 28 0a 20 20 46 74 73 33 43 75 72 73 6f 72  ts(.  Fts3Cursor
42ad1 20 2a 70 43 73 72 2c 20 20 20 20 20 20 20 20 20   *pCsr,         
42ad2 20 20 20 20 20 20 2f 2a 20 46 54 53 20 63 75 72        /* FTS cur
42ad3 73 6f 72 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  sor handle */.  
42ad4 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 2c  Fts3Expr *pExpr,
42ad5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42ad6 2f 2a 20 50 68 72 61 73 65 20 65 78 70 72 65 73  /* Phrase expres
42ad7 73 69 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 2a 61  sion */.  u32 *a
42ad8 69 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 20  iOut            
42ad9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72            /* Arr
42ada 61 79 20 74 6f 20 77 72 69 74 65 20 72 65 73 75  ay to write resu
42adb 6c 74 73 20 69 6e 74 6f 20 28 73 65 65 20 61 62  lts into (see ab
42adc 6f 76 65 29 20 2a 2f 0a 29 7b 0a 20 20 46 74 73  ove) */.){.  Fts
42add 33 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 28  3Table *pTab = (
42ade 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43 73 72  Fts3Table *)pCsr
42adf 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b 0a 20 20  ->base.pVtab;.  
42ae0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
42ae1 4f 4b 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a  OK;.  int iCol;.
42ae2 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 62 44  .  if( pExpr->bD
42ae3 65 66 65 72 72 65 64 20 26 26 20 70 45 78 70 72  eferred && pExpr
42ae4 2d 3e 70 50 61 72 65 6e 74 2d 3e 65 54 79 70 65  ->pParent->eType
42ae5 21 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20  !=FTSQUERY_NEAR 
42ae6 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
42ae7 43 73 72 2d 3e 6e 44 6f 63 3e 30 20 29 3b 0a 20  Csr->nDoc>0 );. 
42ae8 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69     for(iCol=0; i
42ae9 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 75 6d  Col<pTab->nColum
42aea 6e 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  n; iCol++){.    
42aeb 20 20 61 69 4f 75 74 5b 69 43 6f 6c 2a 33 20 2b    aiOut[iCol*3 +
42aec 20 31 5d 20 3d 20 28 75 33 32 29 70 43 73 72 2d   1] = (u32)pCsr-
42aed 3e 6e 44 6f 63 3b 0a 20 20 20 20 20 20 61 69 4f  >nDoc;.      aiO
42aee 75 74 5b 69 43 6f 6c 2a 33 20 2b 20 32 5d 20 3d  ut[iCol*3 + 2] =
42aef 20 28 75 33 32 29 70 43 73 72 2d 3e 6e 44 6f 63   (u32)pCsr->nDoc
42af0 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
42af1 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 45 76  .    rc = fts3Ev
42af2 61 6c 47 61 74 68 65 72 53 74 61 74 73 28 70 43  alGatherStats(pC
42af3 73 72 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20  sr, pExpr);.    
42af4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
42af5 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  K ){.      asser
42af6 74 28 20 70 45 78 70 72 2d 3e 61 4d 49 20 29 3b  t( pExpr->aMI );
42af7 0a 20 20 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d  .      for(iCol=
42af8 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43  0; iCol<pTab->nC
42af9 6f 6c 75 6d 6e 3b 20 69 43 6f 6c 2b 2b 29 7b 0a  olumn; iCol++){.
42afa 20 20 20 20 20 20 20 20 61 69 4f 75 74 5b 69 43          aiOut[iC
42afb 6f 6c 2a 33 20 2b 20 31 5d 20 3d 20 70 45 78 70  ol*3 + 1] = pExp
42afc 72 2d 3e 61 4d 49 5b 69 43 6f 6c 2a 33 20 2b 20  r->aMI[iCol*3 + 
42afd 31 5d 3b 0a 20 20 20 20 20 20 20 20 61 69 4f 75  1];.        aiOu
42afe 74 5b 69 43 6f 6c 2a 33 20 2b 20 32 5d 20 3d 20  t[iCol*3 + 2] = 
42aff 70 45 78 70 72 2d 3e 61 4d 49 5b 69 43 6f 6c 2a  pExpr->aMI[iCol*
42b00 33 20 2b 20 32 5d 3b 0a 20 20 20 20 20 20 7d 0a  3 + 2];.      }.
42b01 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
42b02 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
42b03 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
42b04 70 45 78 70 72 20 70 61 73 73 65 64 20 61 73 20  pExpr passed as 
42b05 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
42b06 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
42b07 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20  tion.** must be 
42b08 6f 66 20 74 79 70 65 20 46 54 53 51 55 45 52 59  of type FTSQUERY
42b09 5f 50 48 52 41 53 45 2e 20 0a 2a 2a 0a 2a 2a 20  _PHRASE. .**.** 
42b0a 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c  The returned val
42b0b 75 65 20 69 73 20 65 69 74 68 65 72 20 4e 55 4c  ue is either NUL
42b0c 4c 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74  L or a pointer t
42b0d 6f 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61  o a buffer conta
42b0e 69 6e 69 6e 67 0a 2a 2a 20 61 20 70 6f 73 69 74  ining.** a posit
42b0f 69 6f 6e 2d 6c 69 73 74 20 69 6e 64 69 63 61 74  ion-list indicat
42b10 69 6e 67 20 74 68 65 20 6f 63 63 75 72 72 65 6e  ing the occurren
42b11 63 65 73 20 6f 66 20 74 68 65 20 70 68 72 61 73  ces of the phras
42b12 65 20 69 6e 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c  e in column iCol
42b13 0a 2a 2a 20 6f 66 20 74 68 65 20 63 75 72 72 65  .** of the curre
42b14 6e 74 20 72 6f 77 2e 20 0a 2a 2a 0a 2a 2a 20 4d  nt row. .**.** M
42b15 6f 72 65 20 73 70 65 63 69 66 69 63 61 6c 6c 79  ore specifically
42b16 2c 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 62  , the returned b
42b17 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 73 20 31  uffer contains 1
42b18 20 76 61 72 69 6e 74 20 66 6f 72 20 65 61 63 68   varint for each
42b19 20 0a 2a 2a 20 6f 63 63 75 72 65 6e 63 65 20 6f   .** occurence o
42b1a 66 20 74 68 65 20 70 68 72 61 73 65 20 69 6e 20  f the phrase in 
42b1b 74 68 65 20 63 6f 6c 75 6d 6e 2c 20 73 74 6f 72  the column, stor
42b1c 65 64 20 75 73 69 6e 67 20 74 68 65 20 6e 6f 72  ed using the nor
42b1d 6d 61 6c 20 28 64 65 6c 74 61 2b 32 29 20 0a 2a  mal (delta+2) .*
42b1e 2a 20 63 6f 6d 70 72 65 73 73 69 6f 6e 20 61 6e  * compression an
42b1f 64 20 69 73 20 74 65 72 6d 69 6e 61 74 65 64 20  d is terminated 
42b20 62 79 20 65 69 74 68 65 72 20 61 6e 20 30 78 30  by either an 0x0
42b21 31 20 6f 72 20 30 78 30 30 20 62 79 74 65 2e 20  1 or 0x00 byte. 
42b22 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20  For example,.** 
42b23 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  if the requested
42b24 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73   column contains
42b25 20 22 61 20 62 20 58 20 63 20 64 20 58 20 58 22   "a b X c d X X"
42b26 20 61 6e 64 20 74 68 65 20 70 6f 73 69 74 69 6f   and the positio
42b27 6e 2d 6c 69 73 74 0a 2a 2a 20 66 6f 72 20 27 58  n-list.** for 'X
42b28 27 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20  ' is requested, 
42b29 74 68 65 20 62 75 66 66 65 72 20 72 65 74 75 72  the buffer retur
42b2a 6e 65 64 20 6d 61 79 20 63 6f 6e 74 61 69 6e 3a  ned may contain:
42b2b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 30 78 30 34 20  .**.**     0x04 
42b2c 30 78 30 35 20 30 78 30 33 20 30 78 30 31 20 20  0x05 0x03 0x01  
42b2d 20 6f 72 20 20 20 30 78 30 34 20 30 78 30 35 20   or   0x04 0x05 
42b2e 30 78 30 33 20 30 78 30 30 0a 2a 2a 0a 2a 2a 20  0x03 0x00.**.** 
42b2f 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f  This function wo
42b30 72 6b 73 20 72 65 67 61 72 64 6c 65 73 73 20 6f  rks regardless o
42b31 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  f whether or not
42b32 20 74 68 65 20 70 68 72 61 73 65 20 69 73 20 64   the phrase is d
42b33 65 66 65 72 72 65 64 2c 0a 2a 2a 20 69 6e 63 72  eferred,.** incr
42b34 65 6d 65 6e 74 61 6c 2c 20 6f 72 20 6e 65 69 74  emental, or neit
42b35 68 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  her..*/.SQLITE_P
42b36 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
42b37 65 33 46 74 73 33 45 76 61 6c 50 68 72 61 73 65  e3Fts3EvalPhrase
42b38 50 6f 73 6c 69 73 74 28 0a 20 20 46 74 73 33 43  Poslist(.  Fts3C
42b39 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20  ursor *pCsr,    
42b3a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54             /* FT
42b3b 53 33 20 63 75 72 73 6f 72 20 6f 62 6a 65 63 74  S3 cursor object
42b3c 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a   */.  Fts3Expr *
42b3d 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20  pExpr,          
42b3e 20 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65 20        /* Phrase 
42b3f 74 6f 20 72 65 74 75 72 6e 20 64 6f 63 6c 69 73  to return doclis
42b40 74 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69  t for */.  int i
42b41 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  Col,            
42b42 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
42b43 6c 75 6d 6e 20 74 6f 20 72 65 74 75 72 6e 20 70  lumn to return p
42b44 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72  osition list for
42b45 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 70 4f   */.  char **ppO
42b46 75 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ut              
42b47 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f        /* OUT: Po
42b48 69 6e 74 65 72 20 74 6f 20 70 6f 73 69 74 69 6f  inter to positio
42b49 6e 20 6c 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 46  n list */.){.  F
42b4a 74 73 33 50 68 72 61 73 65 20 2a 70 50 68 72 61  ts3Phrase *pPhra
42b4b 73 65 20 3d 20 70 45 78 70 72 2d 3e 70 50 68 72  se = pExpr->pPhr
42b4c 61 73 65 3b 0a 20 20 46 74 73 33 54 61 62 6c 65  ase;.  Fts3Table
42b4d 20 2a 70 54 61 62 20 3d 20 28 46 74 73 33 54 61   *pTab = (Fts3Ta
42b4e 62 6c 65 20 2a 29 70 43 73 72 2d 3e 62 61 73 65  ble *)pCsr->base
42b4f 2e 70 56 74 61 62 3b 0a 20 20 63 68 61 72 20 2a  .pVtab;.  char *
42b50 70 49 74 65 72 3b 0a 20 20 69 6e 74 20 69 54 68  pIter;.  int iTh
42b51 69 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  is;.  sqlite3_in
42b52 74 36 34 20 69 44 6f 63 69 64 3b 0a 0a 20 20 2f  t64 iDocid;..  /
42b53 2a 20 49 66 20 74 68 69 73 20 70 68 72 61 73 65  * If this phrase
42b54 20 69 73 20 61 70 70 6c 69 65 73 20 73 70 65 63   is applies spec
42b55 69 66 69 63 61 6c 6c 79 20 74 6f 20 73 6f 6d 65  ifically to some
42b56 20 63 6f 6c 75 6d 6e 20 6f 74 68 65 72 20 74 68   column other th
42b57 61 6e 20 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20  an .  ** column 
42b58 69 43 6f 6c 2c 20 72 65 74 75 72 6e 20 61 20 4e  iCol, return a N
42b59 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 2a 2f  ULL pointer.  */
42b5a 0a 20 20 2a 70 70 4f 75 74 20 3d 20 30 3b 0a 20  .  *ppOut = 0;. 
42b5b 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30   assert( iCol>=0
42b5c 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e   && iCol<pTab->n
42b5d 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 69 66 28 20  Column );.  if( 
42b5e 28 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d  (pPhrase->iColum
42b5f 6e 3c 70 54 61 62 2d 3e 6e 43 6f 6c 75 6d 6e 20  n<pTab->nColumn 
42b60 26 26 20 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c  && pPhrase->iCol
42b61 75 6d 6e 21 3d 69 43 6f 6c 29 20 29 7b 0a 20 20  umn!=iCol) ){.  
42b62 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
42b63 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 44 6f 63 69  OK;.  }..  iDoci
42b64 64 20 3d 20 70 45 78 70 72 2d 3e 69 44 6f 63 69  d = pExpr->iDoci
42b65 64 3b 0a 20 20 70 49 74 65 72 20 3d 20 70 50 68  d;.  pIter = pPh
42b66 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 70 4c  rase->doclist.pL
42b67 69 73 74 3b 0a 20 20 69 66 28 20 69 44 6f 63 69  ist;.  if( iDoci
42b68 64 21 3d 70 43 73 72 2d 3e 69 50 72 65 76 49 64  d!=pCsr->iPrevId
42b69 20 7c 7c 20 70 45 78 70 72 2d 3e 62 45 6f 66 20   || pExpr->bEof 
42b6a 29 7b 0a 20 20 20 20 69 6e 74 20 62 44 65 73 63  ){.    int bDesc
42b6b 44 6f 63 6c 69 73 74 20 3d 20 70 54 61 62 2d 3e  Doclist = pTab->
42b6c 62 44 65 73 63 49 64 78 3b 20 20 20 20 20 20 2f  bDescIdx;      /
42b6d 2a 20 46 6f 72 20 44 4f 43 49 44 5f 43 4d 50 20  * For DOCID_CMP 
42b6e 6d 61 63 72 6f 20 2a 2f 0a 20 20 20 20 69 6e 74  macro */.    int
42b6f 20 62 4f 72 20 3d 20 30 3b 0a 20 20 20 20 75 38   bOr = 0;.    u8
42b70 20 62 45 6f 66 20 3d 20 30 3b 0a 20 20 20 20 46   bEof = 0;.    F
42b71 74 73 33 45 78 70 72 20 2a 70 3b 0a 0a 20 20 20  ts3Expr *p;..   
42b72 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 69   /* Check if thi
42b73 73 20 70 68 72 61 73 65 20 64 65 73 63 65 6e 64  s phrase descend
42b74 73 20 66 72 6f 6d 20 61 6e 20 4f 52 20 65 78 70  s from an OR exp
42b75 72 65 73 73 69 6f 6e 20 6e 6f 64 65 2e 20 49 66  ression node. If
42b76 20 6e 6f 74 2c 20 0a 20 20 20 20 2a 2a 20 72 65   not, .    ** re
42b77 74 75 72 6e 20 4e 55 4c 4c 2e 20 4f 74 68 65 72  turn NULL. Other
42b78 77 69 73 65 2c 20 74 68 65 20 65 6e 74 72 79 20  wise, the entry 
42b79 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73  that corresponds
42b7a 20 74 6f 20 64 6f 63 69 64 20 0a 20 20 20 20 2a   to docid .    *
42b7b 2a 20 70 43 73 72 2d 3e 69 50 72 65 76 49 64 20  * pCsr->iPrevId 
42b7c 6d 61 79 20 6c 69 65 20 65 61 72 6c 69 65 72 20  may lie earlier 
42b7d 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 20 62  in the doclist b
42b7e 75 66 66 65 72 2e 20 2a 2f 0a 20 20 20 20 66 6f  uffer. */.    fo
42b7f 72 28 70 3d 70 45 78 70 72 2d 3e 70 50 61 72 65  r(p=pExpr->pPare
42b80 6e 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 50 61 72  nt; p; p=p->pPar
42b81 65 6e 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ent){.      if( 
42b82 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  p->eType==FTSQUE
42b83 52 59 5f 4f 52 20 29 20 62 4f 72 20 3d 20 31 3b  RY_OR ) bOr = 1;
42b84 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62  .    }.    if( b
42b85 4f 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53  Or==0 ) return S
42b86 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 20 20 2f  QLITE_OK;..    /
42b87 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 64 65  * This is the de
42b88 73 63 65 6e 64 65 6e 74 20 6f 66 20 61 6e 20 4f  scendent of an O
42b89 52 20 6e 6f 64 65 2e 20 49 6e 20 74 68 69 73 20  R node. In this 
42b8a 63 61 73 65 20 77 65 20 63 61 6e 6e 6f 74 20 75  case we cannot u
42b8b 73 65 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 63  se.    ** an inc
42b8c 72 65 6d 65 6e 74 61 6c 20 70 68 72 61 73 65 2e  remental phrase.
42b8d 20 4c 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65   Load the entire
42b8e 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74 68 65   doclist for the
42b8f 20 70 68 72 61 73 65 0a 20 20 20 20 2a 2a 20 69   phrase.    ** i
42b90 6e 74 6f 20 6d 65 6d 6f 72 79 20 69 6e 20 74 68  nto memory in th
42b91 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20  is case.  */.   
42b92 20 69 66 28 20 70 50 68 72 61 73 65 2d 3e 62 49   if( pPhrase->bI
42b93 6e 63 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ncr ){.      int
42b94 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
42b95 0a 20 20 20 20 20 20 69 6e 74 20 62 45 6f 66 53  .      int bEofS
42b96 61 76 65 20 3d 20 70 45 78 70 72 2d 3e 62 45 6f  ave = pExpr->bEo
42b97 66 3b 0a 20 20 20 20 20 20 66 74 73 33 45 76 61  f;.      fts3Eva
42b98 6c 52 65 73 74 61 72 74 28 70 43 73 72 2c 20 70  lRestart(pCsr, p
42b99 45 78 70 72 2c 20 26 72 63 29 3b 0a 20 20 20 20  Expr, &rc);.    
42b9a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c    while( rc==SQL
42b9b 49 54 45 5f 4f 4b 20 26 26 20 21 70 45 78 70 72  ITE_OK && !pExpr
42b9c 2d 3e 62 45 6f 66 20 29 7b 0a 20 20 20 20 20 20  ->bEof ){.      
42b9d 20 20 66 74 73 33 45 76 61 6c 4e 65 78 74 52 6f    fts3EvalNextRo
42b9e 77 28 70 43 73 72 2c 20 70 45 78 70 72 2c 20 26  w(pCsr, pExpr, &
42b9f 72 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  rc);.        if(
42ba0 20 62 45 6f 66 53 61 76 65 3d 3d 30 20 26 26 20   bEofSave==0 && 
42ba1 70 45 78 70 72 2d 3e 69 44 6f 63 69 64 3d 3d 69  pExpr->iDocid==i
42ba2 44 6f 63 69 64 20 29 20 62 72 65 61 6b 3b 0a 20  Docid ) break;. 
42ba3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49 74       }.      pIt
42ba4 65 72 20 3d 20 70 50 68 72 61 73 65 2d 3e 64 6f  er = pPhrase->do
42ba5 63 6c 69 73 74 2e 70 4c 69 73 74 3b 0a 20 20 20  clist.pList;.   
42ba6 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
42ba7 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 68 72  QLITE_OK || pPhr
42ba8 61 73 65 2d 3e 62 49 6e 63 72 3d 3d 30 20 29 3b  ase->bIncr==0 );
42ba9 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
42baa 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
42bab 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  n rc;.    }..   
42bac 20 69 66 28 20 70 45 78 70 72 2d 3e 62 45 6f 66   if( pExpr->bEof
42bad 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65 72 20   ){.      pIter 
42bae 3d 20 30 3b 0a 20 20 20 20 20 20 69 44 6f 63 69  = 0;.      iDoci
42baf 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  d = 0;.    }.   
42bb0 20 62 45 6f 66 20 3d 20 28 70 50 68 72 61 73 65   bEof = (pPhrase
42bb1 2d 3e 64 6f 63 6c 69 73 74 2e 6e 41 6c 6c 3d 3d  ->doclist.nAll==
42bb2 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  0);.    assert( 
42bb3 62 44 65 73 63 44 6f 63 6c 69 73 74 3d 3d 30 20  bDescDoclist==0 
42bb4 7c 7c 20 62 44 65 73 63 44 6f 63 6c 69 73 74 3d  || bDescDoclist=
42bb5 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =1 );.    assert
42bb6 28 20 70 43 73 72 2d 3e 62 44 65 73 63 3d 3d 30  ( pCsr->bDesc==0
42bb7 20 7c 7c 20 70 43 73 72 2d 3e 62 44 65 73 63 3d   || pCsr->bDesc=
42bb8 3d 31 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 70  =1 );..    if( p
42bb9 43 73 72 2d 3e 62 44 65 73 63 3d 3d 62 44 65 73  Csr->bDesc==bDes
42bba 63 44 6f 63 6c 69 73 74 20 29 7b 0a 20 20 20 20  cDoclist ){.    
42bbb 20 20 69 6e 74 20 64 75 6d 6d 79 3b 0a 20 20 20    int dummy;.   
42bbc 20 20 20 77 68 69 6c 65 28 20 28 70 49 74 65 72     while( (pIter
42bbd 3d 3d 30 20 7c 7c 20 44 4f 43 49 44 5f 43 4d 50  ==0 || DOCID_CMP
42bbe 28 69 44 6f 63 69 64 2c 20 70 43 73 72 2d 3e 69  (iDocid, pCsr->i
42bbf 50 72 65 76 49 64 29 3e 30 20 29 20 26 26 20 62  PrevId)>0 ) && b
42bc0 45 6f 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Eof==0 ){.      
42bc1 20 20 73 71 6c 69 74 65 33 46 74 73 33 44 6f 63    sqlite3Fts3Doc
42bc2 6c 69 73 74 50 72 65 76 28 0a 20 20 20 20 20 20  listPrev(.      
42bc3 20 20 20 20 20 20 62 44 65 73 63 44 6f 63 6c 69        bDescDocli
42bc4 73 74 2c 20 70 50 68 72 61 73 65 2d 3e 64 6f 63  st, pPhrase->doc
42bc5 6c 69 73 74 2e 61 41 6c 6c 2c 20 70 50 68 72 61  list.aAll, pPhra
42bc6 73 65 2d 3e 64 6f 63 6c 69 73 74 2e 6e 41 6c 6c  se->doclist.nAll
42bc7 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 26  , .            &
42bc8 70 49 74 65 72 2c 20 26 69 44 6f 63 69 64 2c 20  pIter, &iDocid, 
42bc9 26 64 75 6d 6d 79 2c 20 26 62 45 6f 66 0a 20 20  &dummy, &bEof.  
42bca 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d        );.      }
42bcb 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
42bcc 20 20 77 68 69 6c 65 28 20 28 70 49 74 65 72 3d    while( (pIter=
42bcd 3d 30 20 7c 7c 20 44 4f 43 49 44 5f 43 4d 50 28  =0 || DOCID_CMP(
42bce 69 44 6f 63 69 64 2c 20 70 43 73 72 2d 3e 69 50  iDocid, pCsr->iP
42bcf 72 65 76 49 64 29 3c 30 20 29 20 26 26 20 62 45  revId)<0 ) && bE
42bd0 6f 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  of==0 ){.       
42bd1 20 73 71 6c 69 74 65 33 46 74 73 33 44 6f 63 6c   sqlite3Fts3Docl
42bd2 69 73 74 4e 65 78 74 28 0a 20 20 20 20 20 20 20  istNext(.       
42bd3 20 20 20 20 20 62 44 65 73 63 44 6f 63 6c 69 73       bDescDoclis
42bd4 74 2c 20 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c  t, pPhrase->docl
42bd5 69 73 74 2e 61 41 6c 6c 2c 20 70 50 68 72 61 73  ist.aAll, pPhras
42bd6 65 2d 3e 64 6f 63 6c 69 73 74 2e 6e 41 6c 6c 2c  e->doclist.nAll,
42bd7 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 26 70   .            &p
42bd8 49 74 65 72 2c 20 26 69 44 6f 63 69 64 2c 20 26  Iter, &iDocid, &
42bd9 62 45 6f 66 0a 20 20 20 20 20 20 20 20 29 3b 0a  bEof.        );.
42bda 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
42bdb 20 20 20 69 66 28 20 62 45 6f 66 20 7c 7c 20 69     if( bEof || i
42bdc 44 6f 63 69 64 21 3d 70 43 73 72 2d 3e 69 50 72  Docid!=pCsr->iPr
42bdd 65 76 49 64 20 29 20 70 49 74 65 72 20 3d 20 30  evId ) pIter = 0
42bde 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 74 65  ;.  }.  if( pIte
42bdf 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  r==0 ) return SQ
42be0 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20  LITE_OK;..  if( 
42be1 2a 70 49 74 65 72 3d 3d 30 78 30 31 20 29 7b 0a  *pIter==0x01 ){.
42be2 20 20 20 20 70 49 74 65 72 2b 2b 3b 0a 20 20 20      pIter++;.   
42be3 20 70 49 74 65 72 20 2b 3d 20 73 71 6c 69 74 65   pIter += sqlite
42be4 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32  3Fts3GetVarint32
42be5 28 70 49 74 65 72 2c 20 26 69 54 68 69 73 29 3b  (pIter, &iThis);
42be6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 54  .  }else{.    iT
42be7 68 69 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 77  his = 0;.  }.  w
42be8 68 69 6c 65 28 20 69 54 68 69 73 3c 69 43 6f 6c  hile( iThis<iCol
42be9 20 29 7b 0a 20 20 20 20 66 74 73 33 43 6f 6c 75   ){.    fts3Colu
42bea 6d 6e 6c 69 73 74 43 6f 70 79 28 30 2c 20 26 70  mnlistCopy(0, &p
42beb 49 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 2a  Iter);.    if( *
42bec 70 49 74 65 72 3d 3d 30 78 30 30 20 29 20 72 65  pIter==0x00 ) re
42bed 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 49 74 65  turn 0;.    pIte
42bee 72 2b 2b 3b 0a 20 20 20 20 70 49 74 65 72 20 2b  r++;.    pIter +
42bef 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  = sqlite3Fts3Get
42bf0 56 61 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20  Varint32(pIter, 
42bf1 26 69 54 68 69 73 29 3b 0a 20 20 7d 0a 0a 20 20  &iThis);.  }..  
42bf2 2a 70 70 4f 75 74 20 3d 20 28 28 69 43 6f 6c 3d  *ppOut = ((iCol=
42bf3 3d 69 54 68 69 73 29 3f 70 49 74 65 72 3a 30 29  =iThis)?pIter:0)
42bf4 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
42bf5 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  E_OK;.}../*.** F
42bf6 72 65 65 20 61 6c 6c 20 63 6f 6d 70 6f 6e 65 6e  ree all componen
42bf7 74 73 20 6f 66 20 74 68 65 20 46 74 73 33 50 68  ts of the Fts3Ph
42bf8 72 61 73 65 20 73 74 72 75 63 74 75 72 65 20 74  rase structure t
42bf9 68 61 74 20 77 65 72 65 20 61 6c 6c 6f 63 61 74  hat were allocat
42bfa 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 65 76 61  ed by.** the eva
42bfb 6c 20 6d 6f 64 75 6c 65 2e 20 53 70 65 63 69 66  l module. Specif
42bfc 69 63 61 6c 6c 79 2c 20 74 68 69 73 20 6d 65 61  ically, this mea
42bfd 6e 73 20 74 6f 20 66 72 65 65 3a 0a 2a 2a 0a 2a  ns to free:.**.*
42bfe 2a 20 20 20 2a 20 74 68 65 20 63 6f 6e 74 65 6e  *   * the conten
42bff 74 73 20 6f 66 20 70 50 68 72 61 73 65 2d 3e 64  ts of pPhrase->d
42c00 6f 63 6c 69 73 74 2c 20 61 6e 64 0a 2a 2a 20 20  oclist, and.**  
42c01 20 2a 20 61 6e 79 20 46 74 73 33 4d 75 6c 74 69   * any Fts3Multi
42c02 53 65 67 52 65 61 64 65 72 20 6f 62 6a 65 63 74  SegReader object
42c03 73 20 68 65 6c 64 20 62 79 20 70 68 72 61 73 65  s held by phrase
42c04 20 74 6f 6b 65 6e 73 2e 0a 2a 2f 0a 53 51 4c 49   tokens..*/.SQLI
42c05 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
42c06 73 71 6c 69 74 65 33 46 74 73 33 45 76 61 6c 50  sqlite3Fts3EvalP
42c07 68 72 61 73 65 43 6c 65 61 6e 75 70 28 46 74 73  hraseCleanup(Fts
42c08 33 50 68 72 61 73 65 20 2a 70 50 68 72 61 73 65  3Phrase *pPhrase
42c09 29 7b 0a 20 20 69 66 28 20 70 50 68 72 61 73 65  ){.  if( pPhrase
42c0a 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
42c0b 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
42c0c 70 50 68 72 61 73 65 2d 3e 64 6f 63 6c 69 73 74  pPhrase->doclist
42c0d 2e 61 41 6c 6c 29 3b 0a 20 20 20 20 66 74 73 33  .aAll);.    fts3
42c0e 45 76 61 6c 49 6e 76 61 6c 69 64 61 74 65 50 6f  EvalInvalidatePo
42c0f 73 6c 69 73 74 28 70 50 68 72 61 73 65 29 3b 0a  slist(pPhrase);.
42c10 20 20 20 20 6d 65 6d 73 65 74 28 26 70 50 68 72      memset(&pPhr
42c11 61 73 65 2d 3e 64 6f 63 6c 69 73 74 2c 20 30 2c  ase->doclist, 0,
42c12 20 73 69 7a 65 6f 66 28 46 74 73 33 44 6f 63 6c   sizeof(Fts3Docl
42c13 69 73 74 29 29 3b 0a 20 20 20 20 66 6f 72 28 69  ist));.    for(i
42c14 3d 30 3b 20 69 3c 70 50 68 72 61 73 65 2d 3e 6e  =0; i<pPhrase->n
42c15 54 6f 6b 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  Token; i++){.   
42c16 20 20 20 66 74 73 33 53 65 67 52 65 61 64 65 72     fts3SegReader
42c17 43 75 72 73 6f 72 46 72 65 65 28 70 50 68 72 61  CursorFree(pPhra
42c18 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 70 53  se->aToken[i].pS
42c19 65 67 63 73 72 29 3b 0a 20 20 20 20 20 20 70 50  egcsr);.      pP
42c1a 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d  hrase->aToken[i]
42c1b 2e 70 53 65 67 63 73 72 20 3d 20 30 3b 0a 20 20  .pSegcsr = 0;.  
42c1c 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a    }.  }.}.../*.*
42c1d 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
42c1e 43 4f 52 52 55 50 54 5f 56 54 41 42 2e 0a 2a 2f  CORRUPT_VTAB..*/
42c1f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
42c20 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
42c21 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
42c22 74 73 33 43 6f 72 72 75 70 74 28 29 7b 0a 20 20  ts3Corrupt(){.  
42c23 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
42c24 52 52 55 50 54 5f 56 54 41 42 3b 0a 7d 0a 23 65  RRUPT_VTAB;.}.#e
42c25 6e 64 69 66 0a 0a 23 69 66 20 21 53 51 4c 49 54  ndif..#if !SQLIT
42c26 45 5f 43 4f 52 45 0a 2f 2a 0a 2a 2a 20 49 6e 69  E_CORE./*.** Ini
42c27 74 69 61 6c 69 7a 65 20 41 50 49 20 70 6f 69 6e  tialize API poin
42c28 74 65 72 20 74 61 62 6c 65 2c 20 69 66 20 72 65  ter table, if re
42c29 71 75 69 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  quired..*/.SQLIT
42c2a 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
42c2b 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
42c2c 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
42c2d 20 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72   .  char **pzErr
42c2e 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73 71 6c  Msg,.  const sql
42c2f 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
42c30 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53 51 4c  s *pApi.){.  SQL
42c31 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
42c32 49 54 32 28 70 41 70 69 29 0a 20 20 72 65 74 75  IT2(pApi).  retu
42c33 72 6e 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e  rn sqlite3Fts3In
42c34 69 74 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69 66  it(db);.}.#endif
42c35 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  ..#endif../*****
42c36 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
42c37 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   fts3.c ********
42c38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42c39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42c3a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
42c3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
42c3c 66 69 6c 65 20 66 74 73 33 5f 61 75 78 2e 63 20  file fts3_aux.c 
42c3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42c3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42c3f 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
42c40 32 30 31 31 20 4a 61 6e 20 32 37 0a 2a 2a 0a 2a  2011 Jan 27.**.*
42c41 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
42c42 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
42c43 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
42c44 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
42c45 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
42c46 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
42c47 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
42c48 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
42c49 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
42c4a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
42c4b 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
42c4c 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
42c4d 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
42c4e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
42c4f 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
42c50 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
42c51 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
42c52 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
42c53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42c54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42c55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42c56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
42c57 2a 2a 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  **.*/.#if !defin
42c58 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20  ed(SQLITE_CORE) 
42c59 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
42c5a 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a  E_ENABLE_FTS3)..
42c5b 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 72  /* #include <str
42c5c 69 6e 67 2e 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e  ing.h> */./* #in
42c5d 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e  clude <assert.h>
42c5e 20 2a 2f 0a 0a 74 79 70 65 64 65 66 20 73 74 72   */..typedef str
42c5f 75 63 74 20 46 74 73 33 61 75 78 54 61 62 6c 65  uct Fts3auxTable
42c60 20 46 74 73 33 61 75 78 54 61 62 6c 65 3b 0a 74   Fts3auxTable;.t
42c61 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 74  ypedef struct Ft
42c62 73 33 61 75 78 43 75 72 73 6f 72 20 46 74 73 33  s3auxCursor Fts3
42c63 61 75 78 43 75 72 73 6f 72 3b 0a 0a 73 74 72 75  auxCursor;..stru
42c64 63 74 20 46 74 73 33 61 75 78 54 61 62 6c 65 20  ct Fts3auxTable 
42c65 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  {.  sqlite3_vtab
42c66 20 62 61 73 65 3b 20 20 20 20 20 20 20 20 20 20   base;          
42c67 20 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73      /* Base clas
42c68 73 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65  s used by SQLite
42c69 20 63 6f 72 65 20 2a 2f 0a 20 20 46 74 73 33 54   core */.  Fts3T
42c6a 61 62 6c 65 20 2a 70 46 74 73 33 54 61 62 3b 0a  able *pFts3Tab;.
42c6b 7d 3b 0a 0a 73 74 72 75 63 74 20 46 74 73 33 61  };..struct Fts3a
42c6c 75 78 43 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  uxCursor {.  sql
42c6d 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
42c6e 20 62 61 73 65 3b 20 20 20 20 20 20 20 2f 2a 20   base;       /* 
42c6f 42 61 73 65 20 63 6c 61 73 73 20 75 73 65 64 20  Base class used 
42c70 62 79 20 53 51 4c 69 74 65 20 63 6f 72 65 20 2a  by SQLite core *
42c71 2f 0a 20 20 46 74 73 33 4d 75 6c 74 69 53 65 67  /.  Fts3MultiSeg
42c72 52 65 61 64 65 72 20 63 73 72 3b 20 20 20 20 20  Reader csr;     
42c73 20 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 72 69     /* Must be ri
42c74 67 68 74 20 61 66 74 65 72 20 22 62 61 73 65 22  ght after "base"
42c75 20 2a 2f 0a 20 20 46 74 73 33 53 65 67 46 69 6c   */.  Fts3SegFil
42c76 74 65 72 20 66 69 6c 74 65 72 3b 0a 20 20 63 68  ter filter;.  ch
42c77 61 72 20 2a 7a 53 74 6f 70 3b 0a 20 20 69 6e 74  ar *zStop;.  int
42c78 20 6e 53 74 6f 70 3b 20 20 20 20 20 20 20 20 20   nStop;         
42c79 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
42c7a 42 79 74 65 2d 6c 65 6e 67 74 68 20 6f 66 20 73  Byte-length of s
42c7b 74 72 69 6e 67 20 7a 53 74 6f 70 20 2a 2f 0a 20  tring zStop */. 
42c7c 20 69 6e 74 20 69 73 45 6f 66 3b 20 20 20 20 20   int isEof;     
42c7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42c7e 20 2f 2a 20 54 72 75 65 20 69 66 20 63 75 72 73   /* True if curs
42c7f 6f 72 20 69 73 20 61 74 20 45 4f 46 20 2a 2f 0a  or is at EOF */.
42c80 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
42c81 69 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20  iRowid;         
42c82 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 72 6f 77    /* Current row
42c83 69 64 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 43 6f  id */..  int iCo
42c84 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
42c85 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
42c86 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 27 63 6f  ent value of 'co
42c87 6c 27 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69  l' column */.  i
42c88 6e 74 20 6e 53 74 61 74 3b 20 20 20 20 20 20 20  nt nStat;       
42c89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
42c8a 2a 20 53 69 7a 65 20 6f 66 20 61 53 74 61 74 5b  * Size of aStat[
42c8b 5d 20 61 72 72 61 79 20 2a 2f 0a 20 20 73 74 72  ] array */.  str
42c8c 75 63 74 20 46 74 73 33 61 75 78 43 6f 6c 73 74  uct Fts3auxColst
42c8d 61 74 73 20 7b 0a 20 20 20 20 73 71 6c 69 74 65  ats {.    sqlite
42c8e 33 5f 69 6e 74 36 34 20 6e 44 6f 63 3b 20 20 20  3_int64 nDoc;   
42c8f 20 20 20 20 20 20 20 20 2f 2a 20 27 64 6f 63 75          /* 'docu
42c90 6d 65 6e 74 73 27 20 76 61 6c 75 65 73 20 66 6f  ments' values fo
42c91 72 20 63 75 72 72 65 6e 74 20 63 73 72 20 72 6f  r current csr ro
42c92 77 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  w */.    sqlite3
42c93 5f 69 6e 74 36 34 20 6e 4f 63 63 3b 20 20 20 20  _int64 nOcc;    
42c94 20 20 20 20 20 20 20 2f 2a 20 27 6f 63 63 75 72         /* 'occur
42c95 72 65 6e 63 65 73 27 20 76 61 6c 75 65 73 20 66  rences' values f
42c96 6f 72 20 63 75 72 72 65 6e 74 20 63 73 72 20 72  or current csr r
42c97 6f 77 20 2a 2f 0a 20 20 7d 20 2a 61 53 74 61 74  ow */.  } *aStat
42c98 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 63 68 65  ;.};../*.** Sche
42c99 6d 61 20 6f 66 20 74 68 65 20 74 65 72 6d 73 20  ma of the terms 
42c9a 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  table..*/.#defin
42c9b 65 20 46 54 53 33 5f 54 45 52 4d 53 5f 53 43 48  e FTS3_TERMS_SCH
42c9c 45 4d 41 20 22 43 52 45 41 54 45 20 54 41 42 4c  EMA "CREATE TABL
42c9d 45 20 78 28 74 65 72 6d 2c 20 63 6f 6c 2c 20 64  E x(term, col, d
42c9e 6f 63 75 6d 65 6e 74 73 2c 20 6f 63 63 75 72 72  ocuments, occurr
42c9f 65 6e 63 65 73 29 22 0a 0a 2f 2a 0a 2a 2a 20 54  ences)"../*.** T
42ca0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65  his function doe
42ca1 73 20 61 6c 6c 20 74 68 65 20 77 6f 72 6b 20 66  s all the work f
42ca2 6f 72 20 62 6f 74 68 20 74 68 65 20 78 43 6f 6e  or both the xCon
42ca3 6e 65 63 74 20 61 6e 64 20 78 43 72 65 61 74 65  nect and xCreate
42ca4 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 20 54 68 65   methods..** The
42ca5 73 65 20 74 61 62 6c 65 73 20 68 61 76 65 20 6e  se tables have n
42ca6 6f 20 70 65 72 73 69 73 74 65 6e 74 20 72 65 70  o persistent rep
42ca7 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
42ca8 68 65 69 72 20 6f 77 6e 2c 20 73 6f 20 78 43 6f  heir own, so xCo
42ca9 6e 6e 65 63 74 0a 2a 2a 20 61 6e 64 20 78 43 72  nnect.** and xCr
42caa 65 61 74 65 20 61 72 65 20 69 64 65 6e 74 69 63  eate are identic
42cab 61 6c 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a  al operations..*
42cac 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
42cad 33 61 75 78 43 6f 6e 6e 65 63 74 4d 65 74 68 6f  3auxConnectMetho
42cae 64 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  d(.  sqlite3 *db
42caf 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
42cb0 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
42cb1 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
42cb2 20 76 6f 69 64 20 2a 70 55 6e 75 73 65 64 2c 20   void *pUnused, 
42cb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42cb4 20 2f 2a 20 55 6e 75 73 65 64 20 2a 2f 0a 20 20   /* Unused */.  
42cb5 69 6e 74 20 61 72 67 63 2c 20 20 20 20 20 20 20  int argc,       
42cb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42cb7 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65  /* Number of ele
42cb8 6d 65 6e 74 73 20 69 6e 20 61 72 67 76 20 61 72  ments in argv ar
42cb9 72 61 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ray */.  const c
42cba 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67  har * const *arg
42cbb 76 2c 20 20 20 20 20 20 20 2f 2a 20 78 43 72 65  v,       /* xCre
42cbc 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 61 72 67  ate/xConnect arg
42cbd 75 6d 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20  ument array */. 
42cbe 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a   sqlite3_vtab **
42cbf 70 70 56 74 61 62 2c 20 20 20 20 20 20 20 20 20  ppVtab,         
42cc0 20 2f 2a 20 4f 55 54 3a 20 4e 65 77 20 73 71 6c   /* OUT: New sql
42cc1 69 74 65 33 5f 76 74 61 62 20 6f 62 6a 65 63 74  ite3_vtab object
42cc2 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45   */.  char **pzE
42cc3 72 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rr              
42cc4 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 73 71        /* OUT: sq
42cc5 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 27 64 20 65  lite3_malloc'd e
42cc6 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a  rror message */.
42cc7 29 7b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  ){.  char const 
42cc8 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20  *zDb;           
42cc9 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
42cca 64 61 74 61 62 61 73 65 20 28 65 2e 67 2e 20 22  database (e.g. "
42ccb 6d 61 69 6e 22 29 20 2a 2f 0a 20 20 63 68 61 72  main") */.  char
42ccc 20 63 6f 6e 73 74 20 2a 7a 46 74 73 33 3b 20 20   const *zFts3;  
42ccd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
42cce 61 6d 65 20 6f 66 20 66 74 73 33 20 74 61 62 6c  ame of fts3 tabl
42ccf 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b 20  e */.  int nDb; 
42cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42cd1 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
42cd2 20 6f 66 20 73 74 72 6c 65 6e 28 7a 44 62 29 20   of strlen(zDb) 
42cd3 2a 2f 0a 20 20 69 6e 74 20 6e 46 74 73 33 3b 20  */.  int nFts3; 
42cd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42cd5 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f       /* Result o
42cd6 66 20 73 74 72 6c 65 6e 28 7a 46 74 73 33 29 20  f strlen(zFts3) 
42cd7 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20  */.  int nByte; 
42cd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42cd9 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66       /* Bytes of
42cda 20 73 70 61 63 65 20 74 6f 20 61 6c 6c 6f 63 61   space to alloca
42cdb 74 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  te here */.  int
42cdc 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
42cdd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
42cde 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
42cdf 79 20 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29  y declare_vtab()
42ce0 20 2a 2f 0a 20 20 46 74 73 33 61 75 78 54 61 62   */.  Fts3auxTab
42ce1 6c 65 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20  le *p;          
42ce2 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c        /* Virtual
42ce3 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 74 6f   table object to
42ce4 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 55 4e   return */..  UN
42ce5 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
42ce6 55 6e 75 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54  Unused);..  /* T
42ce7 68 65 20 75 73 65 72 20 73 68 6f 75 6c 64 20 73  he user should s
42ce8 70 65 63 69 66 79 20 61 20 73 69 6e 67 6c 65 20  pecify a single 
42ce9 61 72 67 75 6d 65 6e 74 20 2d 20 74 68 65 20 6e  argument - the n
42cea 61 6d 65 20 6f 66 20 61 6e 20 66 74 73 33 20 74  ame of an fts3 t
42ceb 61 62 6c 65 2e 20 2a 2f 0a 20 20 69 66 28 20 61  able. */.  if( a
42cec 72 67 63 21 3d 34 20 29 7b 0a 20 20 20 20 2a 70  rgc!=4 ){.    *p
42ced 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zErr = sqlite3_m
42cee 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20 20 20  printf(.        
42cef 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66  "wrong number of
42cf0 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 66 74   arguments to ft
42cf1 73 34 61 75 78 20 63 6f 6e 73 74 72 75 63 74 6f  s4aux constructo
42cf2 72 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 72 65  r".    );.    re
42cf3 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
42cf4 52 3b 0a 20 20 7d 0a 0a 20 20 7a 44 62 20 3d 20  R;.  }..  zDb = 
42cf5 61 72 67 76 5b 31 5d 3b 20 0a 20 20 6e 44 62 20  argv[1]; .  nDb 
42cf6 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 44  = (int)strlen(zD
42cf7 62 29 3b 0a 20 20 7a 46 74 73 33 20 3d 20 61 72  b);.  zFts3 = ar
42cf8 67 76 5b 33 5d 3b 0a 20 20 6e 46 74 73 33 20 3d  gv[3];.  nFts3 =
42cf9 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 46 74   (int)strlen(zFt
42cfa 73 33 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c  s3);..  rc = sql
42cfb 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
42cfc 62 28 64 62 2c 20 46 54 53 33 5f 54 45 52 4d 53  b(db, FTS3_TERMS
42cfd 5f 53 43 48 45 4d 41 29 3b 0a 20 20 69 66 28 20  _SCHEMA);.  if( 
42cfe 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
42cff 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 6e 42  return rc;..  nB
42d00 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 46 74 73  yte = sizeof(Fts
42d01 33 61 75 78 54 61 62 6c 65 29 20 2b 20 73 69 7a  3auxTable) + siz
42d02 65 6f 66 28 46 74 73 33 54 61 62 6c 65 29 20 2b  eof(Fts3Table) +
42d03 20 6e 44 62 20 2b 20 6e 46 74 73 33 20 2b 20 32   nDb + nFts3 + 2
42d04 3b 0a 20 20 70 20 3d 20 28 46 74 73 33 61 75 78  ;.  p = (Fts3aux
42d05 54 61 62 6c 65 20 2a 29 73 71 6c 69 74 65 33 5f  Table *)sqlite3_
42d06 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20  malloc(nByte);. 
42d07 20 69 66 28 20 21 70 20 29 20 72 65 74 75 72 6e   if( !p ) return
42d08 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
42d09 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 42   memset(p, 0, nB
42d0a 79 74 65 29 3b 0a 0a 20 20 70 2d 3e 70 46 74 73  yte);..  p->pFts
42d0b 33 54 61 62 20 3d 20 28 46 74 73 33 54 61 62 6c  3Tab = (Fts3Tabl
42d0c 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 70 2d 3e  e *)&p[1];.  p->
42d0d 70 46 74 73 33 54 61 62 2d 3e 7a 44 62 20 3d 20  pFts3Tab->zDb = 
42d0e 28 63 68 61 72 20 2a 29 26 70 2d 3e 70 46 74 73  (char *)&p->pFts
42d0f 33 54 61 62 5b 31 5d 3b 0a 20 20 70 2d 3e 70 46  3Tab[1];.  p->pF
42d10 74 73 33 54 61 62 2d 3e 7a 4e 61 6d 65 20 3d 20  ts3Tab->zName = 
42d11 26 70 2d 3e 70 46 74 73 33 54 61 62 2d 3e 7a 44  &p->pFts3Tab->zD
42d12 62 5b 6e 44 62 2b 31 5d 3b 0a 20 20 70 2d 3e 70  b[nDb+1];.  p->p
42d13 46 74 73 33 54 61 62 2d 3e 64 62 20 3d 20 64 62  Fts3Tab->db = db
42d14 3b 0a 20 20 70 2d 3e 70 46 74 73 33 54 61 62 2d  ;.  p->pFts3Tab-
42d15 3e 6e 49 6e 64 65 78 20 3d 20 31 3b 0a 0a 20 20  >nIndex = 1;..  
42d16 6d 65 6d 63 70 79 28 28 63 68 61 72 20 2a 29 70  memcpy((char *)p
42d17 2d 3e 70 46 74 73 33 54 61 62 2d 3e 7a 44 62 2c  ->pFts3Tab->zDb,
42d18 20 7a 44 62 2c 20 6e 44 62 29 3b 0a 20 20 6d 65   zDb, nDb);.  me
42d19 6d 63 70 79 28 28 63 68 61 72 20 2a 29 70 2d 3e  mcpy((char *)p->
42d1a 70 46 74 73 33 54 61 62 2d 3e 7a 4e 61 6d 65 2c  pFts3Tab->zName,
42d1b 20 7a 46 74 73 33 2c 20 6e 46 74 73 33 29 3b 0a   zFts3, nFts3);.
42d1c 20 20 73 71 6c 69 74 65 33 46 74 73 33 44 65 71    sqlite3Fts3Deq
42d1d 75 6f 74 65 28 28 63 68 61 72 20 2a 29 70 2d 3e  uote((char *)p->
42d1e 70 46 74 73 33 54 61 62 2d 3e 7a 4e 61 6d 65 29  pFts3Tab->zName)
42d1f 3b 0a 0a 20 20 2a 70 70 56 74 61 62 20 3d 20 28  ;..  *ppVtab = (
42d20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 70  sqlite3_vtab *)p
42d21 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
42d22 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
42d23 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65  his function doe
42d24 73 20 74 68 65 20 77 6f 72 6b 20 66 6f 72 20 62  s the work for b
42d25 6f 74 68 20 74 68 65 20 78 44 69 73 63 6f 6e 6e  oth the xDisconn
42d26 65 63 74 20 61 6e 64 20 78 44 65 73 74 72 6f 79  ect and xDestroy
42d27 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 20 54 68 65   methods..** The
42d28 73 65 20 74 61 62 6c 65 73 20 68 61 76 65 20 6e  se tables have n
42d29 6f 20 70 65 72 73 69 73 74 65 6e 74 20 72 65 70  o persistent rep
42d2a 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
42d2b 68 65 69 72 20 6f 77 6e 2c 20 73 6f 20 78 44 69  heir own, so xDi
42d2c 73 63 6f 6e 6e 65 63 74 0a 2a 2a 20 61 6e 64 20  sconnect.** and 
42d2d 78 44 65 73 74 72 6f 79 20 61 72 65 20 69 64 65  xDestroy are ide
42d2e 6e 74 69 63 61 6c 20 6f 70 65 72 61 74 69 6f 6e  ntical operation
42d2f 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
42d30 20 66 74 73 33 61 75 78 44 69 73 63 6f 6e 6e 65   fts3auxDisconne
42d31 63 74 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33  ctMethod(sqlite3
42d32 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20  _vtab *pVtab){. 
42d33 20 46 74 73 33 61 75 78 54 61 62 6c 65 20 2a 70   Fts3auxTable *p
42d34 20 3d 20 28 46 74 73 33 61 75 78 54 61 62 6c 65   = (Fts3auxTable
42d35 20 2a 29 70 56 74 61 62 3b 0a 20 20 46 74 73 33   *)pVtab;.  Fts3
42d36 54 61 62 6c 65 20 2a 70 46 74 73 33 20 3d 20 70  Table *pFts3 = p
42d37 2d 3e 70 46 74 73 33 54 61 62 3b 0a 20 20 69 6e  ->pFts3Tab;.  in
42d38 74 20 69 3b 0a 0a 20 20 2f 2a 20 46 72 65 65 20  t i;..  /* Free 
42d39 61 6e 79 20 70 72 65 70 61 72 65 64 20 73 74 61  any prepared sta
42d3a 74 65 6d 65 6e 74 73 20 68 65 6c 64 20 2a 2f 0a  tements held */.
42d3b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 53 69 7a    for(i=0; i<Siz
42d3c 65 6f 66 41 72 72 61 79 28 70 46 74 73 33 2d 3e  eofArray(pFts3->
42d3d 61 53 74 6d 74 29 3b 20 69 2b 2b 29 7b 0a 20 20  aStmt); i++){.  
42d3e 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
42d3f 7a 65 28 70 46 74 73 33 2d 3e 61 53 74 6d 74 5b  ze(pFts3->aStmt[
42d40 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  i]);.  }.  sqlit
42d41 65 33 5f 66 72 65 65 28 70 46 74 73 33 2d 3e 7a  e3_free(pFts3->z
42d42 53 65 67 6d 65 6e 74 73 54 62 6c 29 3b 0a 20 20  SegmentsTbl);.  
42d43 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
42d44 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
42d45 5f 4f 4b 3b 0a 7d 0a 0a 23 64 65 66 69 6e 65 20  _OK;.}..#define 
42d46 46 54 53 34 41 55 58 5f 45 51 5f 43 4f 4e 53 54  FTS4AUX_EQ_CONST
42d47 52 41 49 4e 54 20 31 0a 23 64 65 66 69 6e 65 20  RAINT 1.#define 
42d48 46 54 53 34 41 55 58 5f 47 45 5f 43 4f 4e 53 54  FTS4AUX_GE_CONST
42d49 52 41 49 4e 54 20 32 0a 23 64 65 66 69 6e 65 20  RAINT 2.#define 
42d4a 46 54 53 34 41 55 58 5f 4c 45 5f 43 4f 4e 53 54  FTS4AUX_LE_CONST
42d4b 52 41 49 4e 54 20 34 0a 0a 2f 2a 0a 2a 2a 20 78  RAINT 4../*.** x
42d4c 42 65 73 74 49 6e 64 65 78 20 2d 20 41 6e 61 6c  BestIndex - Anal
42d4d 79 7a 65 20 61 20 57 48 45 52 45 20 61 6e 64 20  yze a WHERE and 
42d4e 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e  ORDER BY clause.
42d4f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
42d50 74 73 33 61 75 78 42 65 73 74 49 6e 64 65 78 4d  ts3auxBestIndexM
42d51 65 74 68 6f 64 28 0a 20 20 73 71 6c 69 74 65 33  ethod(.  sqlite3
42d52 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 0a 20  _vtab *pVTab, . 
42d53 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
42d54 6e 66 6f 20 2a 70 49 6e 66 6f 0a 29 7b 0a 20 20  nfo *pInfo.){.  
42d55 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 45 71  int i;.  int iEq
42d56 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 69 47 65   = -1;.  int iGe
42d57 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 69 4c 65   = -1;.  int iLe
42d58 20 3d 20 2d 31 3b 0a 0a 20 20 55 4e 55 53 45 44   = -1;..  UNUSED
42d59 5f 50 41 52 41 4d 45 54 45 52 28 70 56 54 61 62  _PARAMETER(pVTab
42d5a 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 76 74  );..  /* This vt
42d5b 61 62 20 64 65 6c 69 76 65 72 73 20 61 6c 77 61  ab delivers alwa
42d5c 79 73 20 72 65 73 75 6c 74 73 20 69 6e 20 22 4f  ys results in "O
42d5d 52 44 45 52 20 42 59 20 74 65 72 6d 20 41 53 43  RDER BY term ASC
42d5e 22 20 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 69 66  " order. */.  if
42d5f 28 20 70 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42  ( pInfo->nOrderB
42d60 79 3d 3d 31 20 0a 20 20 20 26 26 20 70 49 6e 66  y==1 .   && pInf
42d61 6f 2d 3e 61 4f 72 64 65 72 42 79 5b 30 5d 2e 69  o->aOrderBy[0].i
42d62 43 6f 6c 75 6d 6e 3d 3d 30 20 0a 20 20 20 26 26  Column==0 .   &&
42d63 20 70 49 6e 66 6f 2d 3e 61 4f 72 64 65 72 42 79   pInfo->aOrderBy
42d64 5b 30 5d 2e 64 65 73 63 3d 3d 30 0a 20 20 29 7b  [0].desc==0.  ){
42d65 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6f 72 64 65  .    pInfo->orde
42d66 72 42 79 43 6f 6e 73 75 6d 65 64 20 3d 20 31 3b  rByConsumed = 1;
42d67 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 61 72 63  .  }..  /* Searc
42d68 68 20 66 6f 72 20 65 71 75 61 6c 69 74 79 20 61  h for equality a
42d69 6e 64 20 72 61 6e 67 65 20 63 6f 6e 73 74 72 61  nd range constra
42d6a 69 6e 74 73 20 6f 6e 20 74 68 65 20 22 74 65 72  ints on the "ter
42d6b 6d 22 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20  m" column. */.  
42d6c 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 66 6f  for(i=0; i<pInfo
42d6d 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69  ->nConstraint; i
42d6e 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49 6e  ++){.    if( pIn
42d6f 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b  fo->aConstraint[
42d70 69 5d 2e 75 73 61 62 6c 65 20 26 26 20 70 49 6e  i].usable && pIn
42d71 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b  fo->aConstraint[
42d72 69 5d 2e 69 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b  i].iColumn==0 ){
42d73 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20  .      int op = 
42d74 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  pInfo->aConstrai
42d75 6e 74 5b 69 5d 2e 6f 70 3b 0a 20 20 20 20 20 20  nt[i].op;.      
42d76 69 66 28 20 6f 70 3d 3d 53 51 4c 49 54 45 5f 49  if( op==SQLITE_I
42d77 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
42d78 45 51 20 29 20 69 45 71 20 3d 20 69 3b 0a 20 20  EQ ) iEq = i;.  
42d79 20 20 20 20 69 66 28 20 6f 70 3d 3d 53 51 4c 49      if( op==SQLI
42d7a 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
42d7b 49 4e 54 5f 4c 54 20 29 20 69 4c 65 20 3d 20 69  INT_LT ) iLe = i
42d7c 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 3d 3d  ;.      if( op==
42d7d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
42d7e 53 54 52 41 49 4e 54 5f 4c 45 20 29 20 69 4c 65  STRAINT_LE ) iLe
42d7f 20 3d 20 69 3b 0a 20 20 20 20 20 20 69 66 28 20   = i;.      if( 
42d80 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58  op==SQLITE_INDEX
42d81 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20 29  _CONSTRAINT_GT )
42d82 20 69 47 65 20 3d 20 69 3b 0a 20 20 20 20 20 20   iGe = i;.      
42d83 69 66 28 20 6f 70 3d 3d 53 51 4c 49 54 45 5f 49  if( op==SQLITE_I
42d84 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
42d85 47 45 20 29 20 69 47 65 20 3d 20 69 3b 0a 20 20  GE ) iGe = i;.  
42d86 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69    }.  }..  if( i
42d87 45 71 3e 3d 30 20 29 7b 0a 20 20 20 20 70 49 6e  Eq>=0 ){.    pIn
42d88 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 46 54 53  fo->idxNum = FTS
42d89 34 41 55 58 5f 45 51 5f 43 4f 4e 53 54 52 41 49  4AUX_EQ_CONSTRAI
42d8a 4e 54 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 61  NT;.    pInfo->a
42d8b 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
42d8c 69 45 71 5d 2e 61 72 67 76 49 6e 64 65 78 20 3d  iEq].argvIndex =
42d8d 20 31 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 65   1;.    pInfo->e
42d8e 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 35  stimatedCost = 5
42d8f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
42d90 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 30  Info->idxNum = 0
42d91 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 65 73 74  ;.    pInfo->est
42d92 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 32 30 30  imatedCost = 200
42d93 30 30 3b 0a 20 20 20 20 69 66 28 20 69 47 65 3e  00;.    if( iGe>
42d94 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 66  =0 ){.      pInf
42d95 6f 2d 3e 69 64 78 4e 75 6d 20 2b 3d 20 46 54 53  o->idxNum += FTS
42d96 34 41 55 58 5f 47 45 5f 43 4f 4e 53 54 52 41 49  4AUX_GE_CONSTRAI
42d97 4e 54 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d  NT;.      pInfo-
42d98 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67  >aConstraintUsag
42d99 65 5b 69 47 65 5d 2e 61 72 67 76 49 6e 64 65 78  e[iGe].argvIndex
42d9a 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 49 6e 66   = 1;.      pInf
42d9b 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  o->estimatedCost
42d9c 20 2f 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20   /= 2;.    }.   
42d9d 20 69 66 28 20 69 4c 65 3e 3d 30 20 29 7b 0a 20   if( iLe>=0 ){. 
42d9e 20 20 20 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e       pInfo->idxN
42d9f 75 6d 20 2b 3d 20 46 54 53 34 41 55 58 5f 4c 45  um += FTS4AUX_LE
42da0 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20  _CONSTRAINT;.   
42da1 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74     pInfo->aConst
42da2 72 61 69 6e 74 55 73 61 67 65 5b 69 4c 65 5d 2e  raintUsage[iLe].
42da3 61 72 67 76 49 6e 64 65 78 20 3d 20 31 20 2b 20  argvIndex = 1 + 
42da4 28 69 47 65 3e 3d 30 29 3b 0a 20 20 20 20 20 20  (iGe>=0);.      
42da5 70 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64  pInfo->estimated
42da6 43 6f 73 74 20 2f 3d 20 32 3b 0a 20 20 20 20 7d  Cost /= 2;.    }
42da7 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53  .  }..  return S
42da8 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
42da9 2a 2a 20 78 4f 70 65 6e 20 2d 20 4f 70 65 6e 20  ** xOpen - Open 
42daa 61 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61  a cursor..*/.sta
42dab 74 69 63 20 69 6e 74 20 66 74 73 33 61 75 78 4f  tic int fts3auxO
42dac 70 65 6e 4d 65 74 68 6f 64 28 73 71 6c 69 74 65  penMethod(sqlite
42dad 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73  3_vtab *pVTab, s
42dae 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
42daf 6f 72 20 2a 2a 70 70 43 73 72 29 7b 0a 20 20 46  or **ppCsr){.  F
42db0 74 73 33 61 75 78 43 75 72 73 6f 72 20 2a 70 43  ts3auxCursor *pC
42db1 73 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  sr;            /
42db2 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 75 72  * Pointer to cur
42db3 73 6f 72 20 6f 62 6a 65 63 74 20 74 6f 20 72 65  sor object to re
42db4 74 75 72 6e 20 2a 2f 0a 0a 20 20 55 4e 55 53 45  turn */..  UNUSE
42db5 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 54 61  D_PARAMETER(pVTa
42db6 62 29 3b 0a 0a 20 20 70 43 73 72 20 3d 20 28 46  b);..  pCsr = (F
42db7 74 73 33 61 75 78 43 75 72 73 6f 72 20 2a 29 73  ts3auxCursor *)s
42db8 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
42db9 7a 65 6f 66 28 46 74 73 33 61 75 78 43 75 72 73  zeof(Fts3auxCurs
42dba 6f 72 29 29 3b 0a 20 20 69 66 28 20 21 70 43 73  or));.  if( !pCs
42dbb 72 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  r ) return SQLIT
42dbc 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65  E_NOMEM;.  memse
42dbd 74 28 70 43 73 72 2c 20 30 2c 20 73 69 7a 65 6f  t(pCsr, 0, sizeo
42dbe 66 28 46 74 73 33 61 75 78 43 75 72 73 6f 72 29  f(Fts3auxCursor)
42dbf 29 3b 0a 0a 20 20 2a 70 70 43 73 72 20 3d 20 28  );..  *ppCsr = (
42dc0 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
42dc1 73 6f 72 20 2a 29 70 43 73 72 3b 0a 20 20 72 65  sor *)pCsr;.  re
42dc2 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
42dc3 7d 0a 0a 2f 2a 0a 2a 2a 20 78 43 6c 6f 73 65 20  }../*.** xClose 
42dc4 2d 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72  - Close a cursor
42dc5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
42dc6 66 74 73 33 61 75 78 43 6c 6f 73 65 4d 65 74 68  fts3auxCloseMeth
42dc7 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  od(sqlite3_vtab_
42dc8 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29  cursor *pCursor)
42dc9 7b 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  {.  Fts3Table *p
42dca 46 74 73 33 20 3d 20 28 28 46 74 73 33 61 75 78  Fts3 = ((Fts3aux
42dcb 54 61 62 6c 65 20 2a 29 70 43 75 72 73 6f 72 2d  Table *)pCursor-
42dcc 3e 70 56 74 61 62 29 2d 3e 70 46 74 73 33 54 61  >pVtab)->pFts3Ta
42dcd 62 3b 0a 20 20 46 74 73 33 61 75 78 43 75 72 73  b;.  Fts3auxCurs
42dce 6f 72 20 2a 70 43 73 72 20 3d 20 28 46 74 73 33  or *pCsr = (Fts3
42dcf 61 75 78 43 75 72 73 6f 72 20 2a 29 70 43 75 72  auxCursor *)pCur
42dd0 73 6f 72 3b 0a 0a 20 20 73 71 6c 69 74 65 33 46  sor;..  sqlite3F
42dd1 74 73 33 53 65 67 6d 65 6e 74 73 43 6c 6f 73 65  ts3SegmentsClose
42dd2 28 70 46 74 73 33 29 3b 0a 20 20 73 71 6c 69 74  (pFts3);.  sqlit
42dd3 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72 46  e3Fts3SegReaderF
42dd4 69 6e 69 73 68 28 26 70 43 73 72 2d 3e 63 73 72  inish(&pCsr->csr
42dd5 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
42dd6 65 28 28 76 6f 69 64 20 2a 29 70 43 73 72 2d 3e  e((void *)pCsr->
42dd7 66 69 6c 74 65 72 2e 7a 54 65 72 6d 29 3b 0a 20  filter.zTerm);. 
42dd8 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
42dd9 73 72 2d 3e 7a 53 74 6f 70 29 3b 0a 20 20 73 71  sr->zStop);.  sq
42dda 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72 2d  lite3_free(pCsr-
42ddb 3e 61 53 74 61 74 29 3b 0a 20 20 73 71 6c 69 74  >aStat);.  sqlit
42ddc 65 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0a 20  e3_free(pCsr);. 
42ddd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
42dde 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  K;.}..static int
42ddf 20 66 74 73 33 61 75 78 47 72 6f 77 53 74 61 74   fts3auxGrowStat
42de0 41 72 72 61 79 28 46 74 73 33 61 75 78 43 75 72  Array(Fts3auxCur
42de1 73 6f 72 20 2a 70 43 73 72 2c 20 69 6e 74 20 6e  sor *pCsr, int n
42de2 53 69 7a 65 29 7b 0a 20 20 69 66 28 20 6e 53 69  Size){.  if( nSi
42de3 7a 65 3e 70 43 73 72 2d 3e 6e 53 74 61 74 20 29  ze>pCsr->nStat )
42de4 7b 0a 20 20 20 20 73 74 72 75 63 74 20 46 74 73  {.    struct Fts
42de5 33 61 75 78 43 6f 6c 73 74 61 74 73 20 2a 61 4e  3auxColstats *aN
42de6 65 77 3b 0a 20 20 20 20 61 4e 65 77 20 3d 20 28  ew;.    aNew = (
42de7 73 74 72 75 63 74 20 46 74 73 33 61 75 78 43 6f  struct Fts3auxCo
42de8 6c 73 74 61 74 73 20 2a 29 73 71 6c 69 74 65 33  lstats *)sqlite3
42de9 5f 72 65 61 6c 6c 6f 63 28 70 43 73 72 2d 3e 61  _realloc(pCsr->a
42dea 53 74 61 74 2c 20 0a 20 20 20 20 20 20 20 20 73  Stat, .        s
42deb 69 7a 65 6f 66 28 73 74 72 75 63 74 20 46 74 73  izeof(struct Fts
42dec 33 61 75 78 43 6f 6c 73 74 61 74 73 29 20 2a 20  3auxColstats) * 
42ded 6e 53 69 7a 65 0a 20 20 20 20 29 3b 0a 20 20 20  nSize.    );.   
42dee 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 20 72   if( aNew==0 ) r
42def 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
42df0 45 4d 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  EM;.    memset(&
42df1 61 4e 65 77 5b 70 43 73 72 2d 3e 6e 53 74 61 74  aNew[pCsr->nStat
42df2 5d 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 73  ], 0, .        s
42df3 69 7a 65 6f 66 28 73 74 72 75 63 74 20 46 74 73  izeof(struct Fts
42df4 33 61 75 78 43 6f 6c 73 74 61 74 73 29 20 2a 20  3auxColstats) * 
42df5 28 6e 53 69 7a 65 20 2d 20 70 43 73 72 2d 3e 6e  (nSize - pCsr->n
42df6 53 74 61 74 29 0a 20 20 20 20 29 3b 0a 20 20 20  Stat).    );.   
42df7 20 70 43 73 72 2d 3e 61 53 74 61 74 20 3d 20 61   pCsr->aStat = a
42df8 4e 65 77 3b 0a 20 20 20 20 70 43 73 72 2d 3e 6e  New;.    pCsr->n
42df9 53 74 61 74 20 3d 20 6e 53 69 7a 65 3b 0a 20 20  Stat = nSize;.  
42dfa 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
42dfb 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 78  E_OK;.}../*.** x
42dfc 4e 65 78 74 20 2d 20 41 64 76 61 6e 63 65 20 74  Next - Advance t
42dfd 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  he cursor to the
42dfe 20 6e 65 78 74 20 72 6f 77 2c 20 69 66 20 61 6e   next row, if an
42dff 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  y..*/.static int
42e00 20 66 74 73 33 61 75 78 4e 65 78 74 4d 65 74 68   fts3auxNextMeth
42e01 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  od(sqlite3_vtab_
42e02 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29  cursor *pCursor)
42e03 7b 0a 20 20 46 74 73 33 61 75 78 43 75 72 73 6f  {.  Fts3auxCurso
42e04 72 20 2a 70 43 73 72 20 3d 20 28 46 74 73 33 61  r *pCsr = (Fts3a
42e05 75 78 43 75 72 73 6f 72 20 2a 29 70 43 75 72 73  uxCursor *)pCurs
42e06 6f 72 3b 0a 20 20 46 74 73 33 54 61 62 6c 65 20  or;.  Fts3Table 
42e07 2a 70 46 74 73 33 20 3d 20 28 28 46 74 73 33 61  *pFts3 = ((Fts3a
42e08 75 78 54 61 62 6c 65 20 2a 29 70 43 75 72 73 6f  uxTable *)pCurso
42e09 72 2d 3e 70 56 74 61 62 29 2d 3e 70 46 74 73 33  r->pVtab)->pFts3
42e0a 54 61 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  Tab;.  int rc;..
42e0b 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 6f    /* Increment o
42e0c 75 72 20 70 72 65 74 65 6e 64 20 72 6f 77 69 64  ur pretend rowid
42e0d 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 70 43 73   value. */.  pCs
42e0e 72 2d 3e 69 52 6f 77 69 64 2b 2b 3b 0a 0a 20 20  r->iRowid++;..  
42e0f 66 6f 72 28 70 43 73 72 2d 3e 69 43 6f 6c 2b 2b  for(pCsr->iCol++
42e10 3b 20 70 43 73 72 2d 3e 69 43 6f 6c 3c 70 43 73  ; pCsr->iCol<pCs
42e11 72 2d 3e 6e 53 74 61 74 3b 20 70 43 73 72 2d 3e  r->nStat; pCsr->
42e12 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 69 66 28  iCol++){.    if(
42e13 20 70 43 73 72 2d 3e 61 53 74 61 74 5b 70 43 73   pCsr->aStat[pCs
42e14 72 2d 3e 69 43 6f 6c 5d 2e 6e 44 6f 63 3e 30 20  r->iCol].nDoc>0 
42e15 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
42e16 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  OK;.  }..  rc = 
42e17 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65  sqlite3Fts3SegRe
42e18 61 64 65 72 53 74 65 70 28 70 46 74 73 33 2c 20  aderStep(pFts3, 
42e19 26 70 43 73 72 2d 3e 63 73 72 29 3b 0a 20 20 69  &pCsr->csr);.  i
42e1a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f  f( rc==SQLITE_RO
42e1b 57 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d  W ){.    int i =
42e1c 20 30 3b 0a 20 20 20 20 69 6e 74 20 6e 44 6f 63   0;.    int nDoc
42e1d 6c 69 73 74 20 3d 20 70 43 73 72 2d 3e 63 73 72  list = pCsr->csr
42e1e 2e 6e 44 6f 63 6c 69 73 74 3b 0a 20 20 20 20 63  .nDoclist;.    c
42e1f 68 61 72 20 2a 61 44 6f 63 6c 69 73 74 20 3d 20  har *aDoclist = 
42e20 70 43 73 72 2d 3e 63 73 72 2e 61 44 6f 63 6c 69  pCsr->csr.aDocli
42e21 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c  st;.    int iCol
42e22 3b 0a 0a 20 20 20 20 69 6e 74 20 65 53 74 61 74  ;..    int eStat
42e23 65 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 28 20  e = 0;..    if( 
42e24 70 43 73 72 2d 3e 7a 53 74 6f 70 20 29 7b 0a 20  pCsr->zStop ){. 
42e25 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 28 70 43       int n = (pC
42e26 73 72 2d 3e 6e 53 74 6f 70 3c 70 43 73 72 2d 3e  sr->nStop<pCsr->
42e27 63 73 72 2e 6e 54 65 72 6d 29 20 3f 20 70 43 73  csr.nTerm) ? pCs
42e28 72 2d 3e 6e 53 74 6f 70 20 3a 20 70 43 73 72 2d  r->nStop : pCsr-
42e29 3e 63 73 72 2e 6e 54 65 72 6d 3b 0a 20 20 20 20  >csr.nTerm;.    
42e2a 20 20 69 6e 74 20 6d 63 20 3d 20 6d 65 6d 63 6d    int mc = memcm
42e2b 70 28 70 43 73 72 2d 3e 7a 53 74 6f 70 2c 20 70  p(pCsr->zStop, p
42e2c 43 73 72 2d 3e 63 73 72 2e 7a 54 65 72 6d 2c 20  Csr->csr.zTerm, 
42e2d 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 63  n);.      if( mc
42e2e 3c 30 20 7c 7c 20 28 6d 63 3d 3d 30 20 26 26 20  <0 || (mc==0 && 
42e2f 70 43 73 72 2d 3e 63 73 72 2e 6e 54 65 72 6d 3e  pCsr->csr.nTerm>
42e30 70 43 73 72 2d 3e 6e 53 74 6f 70 29 20 29 7b 0a  pCsr->nStop) ){.
42e31 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e 69 73          pCsr->is
42e32 45 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  Eof = 1;.       
42e33 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
42e34 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  K;.      }.    }
42e35 0a 0a 20 20 20 20 69 66 28 20 66 74 73 33 61 75  ..    if( fts3au
42e36 78 47 72 6f 77 53 74 61 74 41 72 72 61 79 28 70  xGrowStatArray(p
42e37 43 73 72 2c 20 32 29 20 29 20 72 65 74 75 72 6e  Csr, 2) ) return
42e38 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
42e39 20 20 20 6d 65 6d 73 65 74 28 70 43 73 72 2d 3e     memset(pCsr->
42e3a 61 53 74 61 74 2c 20 30 2c 20 73 69 7a 65 6f 66  aStat, 0, sizeof
42e3b 28 73 74 72 75 63 74 20 46 74 73 33 61 75 78 43  (struct Fts3auxC
42e3c 6f 6c 73 74 61 74 73 29 20 2a 20 70 43 73 72 2d  olstats) * pCsr-
42e3d 3e 6e 53 74 61 74 29 3b 0a 20 20 20 20 69 43 6f  >nStat);.    iCo
42e3e 6c 20 3d 20 30 3b 0a 0a 20 20 20 20 77 68 69 6c  l = 0;..    whil
42e3f 65 28 20 69 3c 6e 44 6f 63 6c 69 73 74 20 29 7b  e( i<nDoclist ){
42e40 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69  .      sqlite3_i
42e41 6e 74 36 34 20 76 20 3d 20 30 3b 0a 0a 20 20 20  nt64 v = 0;..   
42e42 20 20 20 69 20 2b 3d 20 73 71 6c 69 74 65 33 46     i += sqlite3F
42e43 74 73 33 47 65 74 56 61 72 69 6e 74 28 26 61 44  ts3GetVarint(&aD
42e44 6f 63 6c 69 73 74 5b 69 5d 2c 20 26 76 29 3b 0a  oclist[i], &v);.
42e45 20 20 20 20 20 20 73 77 69 74 63 68 28 20 65 53        switch( eS
42e46 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  tate ){.        
42e47 2f 2a 20 53 74 61 74 65 20 30 2e 20 49 6e 20 74  /* State 0. In t
42e48 68 69 73 20 73 74 61 74 65 20 74 68 65 20 69 6e  his state the in
42e49 74 65 67 65 72 20 6a 75 73 74 20 72 65 61 64 20  teger just read 
42e4a 77 61 73 20 61 20 64 6f 63 69 64 2e 20 2a 2f 0a  was a docid. */.
42e4b 20 20 20 20 20 20 20 20 63 61 73 65 20 30 3a 0a          case 0:.
42e4c 20 20 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e            pCsr->
42e4d 61 53 74 61 74 5b 30 5d 2e 6e 44 6f 63 2b 2b 3b  aStat[0].nDoc++;
42e4e 0a 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74  .          eStat
42e4f 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  e = 1;.         
42e50 20 69 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20   iCol = 0;.     
42e51 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20       break;..   
42e52 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20 31 2e       /* State 1.
42e53 20 49 6e 20 74 68 69 73 20 73 74 61 74 65 20 77   In this state w
42e54 65 20 61 72 65 20 65 78 70 65 63 74 69 6e 67 20  e are expecting 
42e55 65 69 74 68 65 72 20 61 20 31 2c 20 69 6e 64 69  either a 1, indi
42e56 63 61 74 69 6e 67 0a 20 20 20 20 20 20 20 20 2a  cating.        *
42e57 2a 20 74 68 61 74 20 74 68 65 20 66 6f 6c 6c 6f  * that the follo
42e58 77 69 6e 67 20 69 6e 74 65 67 65 72 20 77 69 6c  wing integer wil
42e59 6c 20 62 65 20 61 20 63 6f 6c 75 6d 6e 20 6e 75  l be a column nu
42e5a 6d 62 65 72 2c 20 6f 72 20 74 68 65 0a 20 20 20  mber, or the.   
42e5b 20 20 20 20 20 2a 2a 20 73 74 61 72 74 20 6f 66       ** start of
42e5c 20 61 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74   a position list
42e5d 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 30 2e 20 20   for column 0.  
42e5e 0a 20 20 20 20 20 20 20 20 2a 2a 20 0a 20 20 20  .        ** .   
42e5f 20 20 20 20 20 2a 2a 20 54 68 65 20 6f 6e 6c 79       ** The only
42e60 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77   difference betw
42e61 65 65 6e 20 73 74 61 74 65 20 31 20 61 6e 64 20  een state 1 and 
42e62 73 74 61 74 65 20 32 20 69 73 20 74 68 61 74 20  state 2 is that 
42e63 69 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  if the.        *
42e64 2a 20 69 6e 74 65 67 65 72 20 65 6e 63 6f 75 6e  * integer encoun
42e65 74 65 72 65 64 20 69 6e 20 73 74 61 74 65 20 31  tered in state 1
42e66 20 69 73 20 6e 6f 74 20 30 20 6f 72 20 31 2c 20   is not 0 or 1, 
42e67 74 68 65 6e 20 77 65 20 6e 65 65 64 20 74 6f 0a  then we need to.
42e68 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 63 72 65          ** incre
42e69 6d 65 6e 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20  ment the column 
42e6a 30 20 22 6e 44 6f 63 22 20 63 6f 75 6e 74 20 66  0 "nDoc" count f
42e6b 6f 72 20 74 68 69 73 20 74 65 72 6d 2e 0a 20 20  or this term..  
42e6c 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
42e6d 20 63 61 73 65 20 31 3a 0a 20 20 20 20 20 20 20   case 1:.       
42e6e 20 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3d     assert( iCol=
42e6f 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =0 );.          
42e70 69 66 28 20 76 3e 31 20 29 7b 0a 20 20 20 20 20  if( v>1 ){.     
42e71 20 20 20 20 20 20 20 70 43 73 72 2d 3e 61 53 74         pCsr->aSt
42e72 61 74 5b 31 5d 2e 6e 44 6f 63 2b 2b 3b 0a 20 20  at[1].nDoc++;.  
42e73 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
42e74 20 20 20 20 65 53 74 61 74 65 20 3d 20 32 3b 0a      eState = 2;.
42e75 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 61 6c            /* fal
42e76 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 0a 20 20  l through */..  
42e77 20 20 20 20 20 20 63 61 73 65 20 32 3a 0a 20 20        case 2:.  
42e78 20 20 20 20 20 20 20 20 69 66 28 20 76 3d 3d 30          if( v==0
42e79 20 29 7b 20 20 20 20 20 20 20 2f 2a 20 30 78 30   ){       /* 0x0
42e7a 30 2e 20 4e 65 78 74 20 69 6e 74 65 67 65 72 20  0. Next integer 
42e7b 77 69 6c 6c 20 62 65 20 61 20 64 6f 63 69 64 2e  will be a docid.
42e7c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
42e7d 65 53 74 61 74 65 20 3d 20 30 3b 0a 20 20 20 20  eState = 0;.    
42e7e 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
42e7f 76 3d 3d 31 20 29 7b 20 2f 2a 20 30 78 30 31 2e  v==1 ){ /* 0x01.
42e80 20 4e 65 78 74 20 69 6e 74 65 67 65 72 20 77 69   Next integer wi
42e81 6c 6c 20 62 65 20 61 20 63 6f 6c 75 6d 6e 20 6e  ll be a column n
42e82 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20  umber. */.      
42e83 20 20 20 20 20 20 65 53 74 61 74 65 20 3d 20 33        eState = 3
42e84 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
42e85 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e{            /*
42e86 20 32 20 6f 72 20 67 72 65 61 74 65 72 2e 20 41   2 or greater. A
42e87 20 70 6f 73 69 74 69 6f 6e 2e 20 2a 2f 0a 20 20   position. */.  
42e88 20 20 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e            pCsr->
42e89 61 53 74 61 74 5b 69 43 6f 6c 2b 31 5d 2e 6e 4f  aStat[iCol+1].nO
42e8a 63 63 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  cc++;.          
42e8b 20 20 70 43 73 72 2d 3e 61 53 74 61 74 5b 30 5d    pCsr->aStat[0]
42e8c 2e 6e 4f 63 63 2b 2b 3b 0a 20 20 20 20 20 20 20  .nOcc++;.       
42e8d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62     }.          b
42e8e 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 20 20 2f  reak;..        /
42e8f 2a 20 53 74 61 74 65 20 33 2e 20 54 68 65 20 69  * State 3. The i
42e90 6e 74 65 67 65 72 20 6a 75 73 74 20 72 65 61 64  nteger just read
42e91 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 6e 75 6d   is a column num
42e92 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ber. */.        
42e93 64 65 66 61 75 6c 74 3a 20 61 73 73 65 72 74 28  default: assert(
42e94 20 65 53 74 61 74 65 3d 3d 33 20 29 3b 0a 20 20   eState==3 );.  
42e95 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 28          iCol = (
42e96 69 6e 74 29 76 3b 0a 20 20 20 20 20 20 20 20 20  int)v;.         
42e97 20 69 66 28 20 66 74 73 33 61 75 78 47 72 6f 77   if( fts3auxGrow
42e98 53 74 61 74 41 72 72 61 79 28 70 43 73 72 2c 20  StatArray(pCsr, 
42e99 69 43 6f 6c 2b 32 29 20 29 20 72 65 74 75 72 6e  iCol+2) ) return
42e9a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
42e9b 20 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e 61           pCsr->a
42e9c 53 74 61 74 5b 69 43 6f 6c 2b 31 5d 2e 6e 44 6f  Stat[iCol+1].nDo
42e9d 63 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 65  c++;.          e
42e9e 53 74 61 74 65 20 3d 20 32 3b 0a 20 20 20 20 20  State = 2;.     
42e9f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
42ea0 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70    }.    }..    p
42ea1 43 73 72 2d 3e 69 43 6f 6c 20 3d 20 30 3b 0a 20  Csr->iCol = 0;. 
42ea2 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
42ea3 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
42ea4 70 43 73 72 2d 3e 69 73 45 6f 66 20 3d 20 31 3b  pCsr->isEof = 1;
42ea5 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
42ea6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 78 46 69 6c 74  ;.}../*.** xFilt
42ea7 65 72 20 2d 20 49 6e 69 74 69 61 6c 69 7a 65 20  er - Initialize 
42ea8 61 20 63 75 72 73 6f 72 20 74 6f 20 70 6f 69 6e  a cursor to poin
42ea9 74 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f  t at the start o
42eaa 66 20 69 74 73 20 64 61 74 61 2e 0a 2a 2f 0a 73  f its data..*/.s
42eab 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 61 75  tatic int fts3au
42eac 78 46 69 6c 74 65 72 4d 65 74 68 6f 64 28 0a 20  xFilterMethod(. 
42ead 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
42eae 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20  rsor *pCursor,  
42eaf 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 75   /* The cursor u
42eb0 73 65 64 20 66 6f 72 20 74 68 69 73 20 71 75 65  sed for this que
42eb1 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 4e  ry */.  int idxN
42eb2 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  um,             
42eb3 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 61 74          /* Strat
42eb4 65 67 79 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63  egy index */.  c
42eb5 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74  onst char *idxSt
42eb6 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  r,             /
42eb7 2a 20 55 6e 75 73 65 64 20 2a 2f 0a 20 20 69 6e  * Unused */.  in
42eb8 74 20 6e 56 61 6c 2c 20 20 20 20 20 20 20 20 20  t nVal,         
42eb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42eba 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
42ebb 6e 74 73 20 69 6e 20 61 70 56 61 6c 20 2a 2f 0a  nts in apVal */.
42ebc 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
42ebd 2a 2a 61 70 56 61 6c 20 20 20 20 20 20 20 20 20  **apVal         
42ebe 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 66    /* Arguments f
42ebf 6f 72 20 74 68 65 20 69 6e 64 65 78 69 6e 67 20  or the indexing 
42ec0 73 63 68 65 6d 65 20 2a 2f 0a 29 7b 0a 20 20 46  scheme */.){.  F
42ec1 74 73 33 61 75 78 43 75 72 73 6f 72 20 2a 70 43  ts3auxCursor *pC
42ec2 73 72 20 3d 20 28 46 74 73 33 61 75 78 43 75 72  sr = (Fts3auxCur
42ec3 73 6f 72 20 2a 29 70 43 75 72 73 6f 72 3b 0a 20  sor *)pCursor;. 
42ec4 20 46 74 73 33 54 61 62 6c 65 20 2a 70 46 74 73   Fts3Table *pFts
42ec5 33 20 3d 20 28 28 46 74 73 33 61 75 78 54 61 62  3 = ((Fts3auxTab
42ec6 6c 65 20 2a 29 70 43 75 72 73 6f 72 2d 3e 70 56  le *)pCursor->pV
42ec7 74 61 62 29 2d 3e 70 46 74 73 33 54 61 62 3b 0a  tab)->pFts3Tab;.
42ec8 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20    int rc;.  int 
42ec9 69 73 53 63 61 6e 3b 0a 0a 20 20 55 4e 55 53 45  isScan;..  UNUSE
42eca 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 56 61 6c  D_PARAMETER(nVal
42ecb 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
42ecc 4d 45 54 45 52 28 69 64 78 53 74 72 29 3b 0a 0a  METER(idxStr);..
42ecd 20 20 61 73 73 65 72 74 28 20 69 64 78 53 74 72    assert( idxStr
42ece 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
42ecf 20 69 64 78 4e 75 6d 3d 3d 46 54 53 34 41 55 58   idxNum==FTS4AUX
42ed0 5f 45 51 5f 43 4f 4e 53 54 52 41 49 4e 54 20 7c  _EQ_CONSTRAINT |
42ed1 7c 20 69 64 78 4e 75 6d 3d 3d 30 0a 20 20 20 20  | idxNum==0.    
42ed2 20 20 20 7c 7c 20 69 64 78 4e 75 6d 3d 3d 46 54     || idxNum==FT
42ed3 53 34 41 55 58 5f 4c 45 5f 43 4f 4e 53 54 52 41  S4AUX_LE_CONSTRA
42ed4 49 4e 54 20 7c 7c 20 69 64 78 4e 75 6d 3d 3d 46  INT || idxNum==F
42ed5 54 53 34 41 55 58 5f 47 45 5f 43 4f 4e 53 54 52  TS4AUX_GE_CONSTR
42ed6 41 49 4e 54 0a 20 20 20 20 20 20 20 7c 7c 20 69  AINT.       || i
42ed7 64 78 4e 75 6d 3d 3d 28 46 54 53 34 41 55 58 5f  dxNum==(FTS4AUX_
42ed8 4c 45 5f 43 4f 4e 53 54 52 41 49 4e 54 7c 46 54  LE_CONSTRAINT|FT
42ed9 53 34 41 55 58 5f 47 45 5f 43 4f 4e 53 54 52 41  S4AUX_GE_CONSTRA
42eda 49 4e 54 29 0a 20 20 29 3b 0a 20 20 69 73 53 63  INT).  );.  isSc
42edb 61 6e 20 3d 20 28 69 64 78 4e 75 6d 21 3d 46 54  an = (idxNum!=FT
42edc 53 34 41 55 58 5f 45 51 5f 43 4f 4e 53 54 52 41  S4AUX_EQ_CONSTRA
42edd 49 4e 54 29 3b 0a 0a 20 20 2f 2a 20 49 6e 20 63  INT);..  /* In c
42ede 61 73 65 20 74 68 69 73 20 63 75 72 73 6f 72 20  ase this cursor 
42edf 69 73 20 62 65 69 6e 67 20 72 65 75 73 65 64 2c  is being reused,
42ee0 20 63 6c 6f 73 65 20 61 6e 64 20 7a 65 72 6f 20   close and zero 
42ee1 69 74 2e 20 2a 2f 0a 20 20 74 65 73 74 63 61 73  it. */.  testcas
42ee2 65 28 70 43 73 72 2d 3e 66 69 6c 74 65 72 2e 7a  e(pCsr->filter.z
42ee3 54 65 72 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33  Term);.  sqlite3
42ee4 46 74 73 33 53 65 67 52 65 61 64 65 72 46 69 6e  Fts3SegReaderFin
42ee5 69 73 68 28 26 70 43 73 72 2d 3e 63 73 72 29 3b  ish(&pCsr->csr);
42ee6 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
42ee7 28 76 6f 69 64 20 2a 29 70 43 73 72 2d 3e 66 69  (void *)pCsr->fi
42ee8 6c 74 65 72 2e 7a 54 65 72 6d 29 3b 0a 20 20 73  lter.zTerm);.  s
42ee9 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72  qlite3_free(pCsr
42eea 2d 3e 61 53 74 61 74 29 3b 0a 20 20 6d 65 6d 73  ->aStat);.  mems
42eeb 65 74 28 26 70 43 73 72 2d 3e 63 73 72 2c 20 30  et(&pCsr->csr, 0
42eec 2c 20 28 28 75 38 2a 29 26 70 43 73 72 5b 31 5d  , ((u8*)&pCsr[1]
42eed 29 20 2d 20 28 75 38 2a 29 26 70 43 73 72 2d 3e  ) - (u8*)&pCsr->
42eee 63 73 72 29 3b 0a 0a 20 20 70 43 73 72 2d 3e 66  csr);..  pCsr->f
42eef 69 6c 74 65 72 2e 66 6c 61 67 73 20 3d 20 46 54  ilter.flags = FT
42ef0 53 33 5f 53 45 47 4d 45 4e 54 5f 52 45 51 55 49  S3_SEGMENT_REQUI
42ef1 52 45 5f 50 4f 53 7c 46 54 53 33 5f 53 45 47 4d  RE_POS|FTS3_SEGM
42ef2 45 4e 54 5f 49 47 4e 4f 52 45 5f 45 4d 50 54 59  ENT_IGNORE_EMPTY
42ef3 3b 0a 20 20 69 66 28 20 69 73 53 63 61 6e 20 29  ;.  if( isScan )
42ef4 20 70 43 73 72 2d 3e 66 69 6c 74 65 72 2e 66 6c   pCsr->filter.fl
42ef5 61 67 73 20 7c 3d 20 46 54 53 33 5f 53 45 47 4d  ags |= FTS3_SEGM
42ef6 45 4e 54 5f 53 43 41 4e 3b 0a 0a 20 20 69 66 28  ENT_SCAN;..  if(
42ef7 20 69 64 78 4e 75 6d 26 28 46 54 53 34 41 55 58   idxNum&(FTS4AUX
42ef8 5f 45 51 5f 43 4f 4e 53 54 52 41 49 4e 54 7c 46  _EQ_CONSTRAINT|F
42ef9 54 53 34 41 55 58 5f 47 45 5f 43 4f 4e 53 54 52  TS4AUX_GE_CONSTR
42efa 41 49 4e 54 29 20 29 7b 0a 20 20 20 20 63 6f 6e  AINT) ){.    con
42efb 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
42efc 20 2a 7a 53 74 72 20 3d 20 73 71 6c 69 74 65 33   *zStr = sqlite3
42efd 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70 56 61  _value_text(apVa
42efe 6c 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a  l[0]);.    if( z
42eff 53 74 72 20 29 7b 0a 20 20 20 20 20 20 70 43 73  Str ){.      pCs
42f00 72 2d 3e 66 69 6c 74 65 72 2e 7a 54 65 72 6d 20  r->filter.zTerm 
42f01 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
42f02 66 28 22 25 73 22 2c 20 7a 53 74 72 29 3b 0a 20  f("%s", zStr);. 
42f03 20 20 20 20 20 70 43 73 72 2d 3e 66 69 6c 74 65       pCsr->filte
42f04 72 2e 6e 54 65 72 6d 20 3d 20 73 71 6c 69 74 65  r.nTerm = sqlite
42f05 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 70  3_value_bytes(ap
42f06 56 61 6c 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69  Val[0]);.      i
42f07 66 28 20 70 43 73 72 2d 3e 66 69 6c 74 65 72 2e  f( pCsr->filter.
42f08 7a 54 65 72 6d 3d 3d 30 20 29 20 72 65 74 75 72  zTerm==0 ) retur
42f09 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
42f0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
42f0b 69 64 78 4e 75 6d 26 46 54 53 34 41 55 58 5f 4c  idxNum&FTS4AUX_L
42f0c 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 29 7b 0a  E_CONSTRAINT ){.
42f0d 20 20 20 20 69 6e 74 20 69 49 64 78 20 3d 20 28      int iIdx = (
42f0e 69 64 78 4e 75 6d 26 46 54 53 34 41 55 58 5f 47  idxNum&FTS4AUX_G
42f0f 45 5f 43 4f 4e 53 54 52 41 49 4e 54 29 20 3f 20  E_CONSTRAINT) ? 
42f10 31 20 3a 20 30 3b 0a 20 20 20 20 70 43 73 72 2d  1 : 0;.    pCsr-
42f11 3e 7a 53 74 6f 70 20 3d 20 73 71 6c 69 74 65 33  >zStop = sqlite3
42f12 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20 73  _mprintf("%s", s
42f13 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
42f14 74 28 61 70 56 61 6c 5b 69 49 64 78 5d 29 29 3b  t(apVal[iIdx]));
42f15 0a 20 20 20 20 70 43 73 72 2d 3e 6e 53 74 6f 70  .    pCsr->nStop
42f16 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
42f17 5f 62 79 74 65 73 28 61 70 56 61 6c 5b 69 49 64  _bytes(apVal[iId
42f18 78 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 43 73  x]);.    if( pCs
42f19 72 2d 3e 7a 53 74 6f 70 3d 3d 30 20 29 20 72 65  r->zStop==0 ) re
42f1a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
42f1b 4d 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73  M;.  }..  rc = s
42f1c 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61  qlite3Fts3SegRea
42f1d 64 65 72 43 75 72 73 6f 72 28 70 46 74 73 33 2c  derCursor(pFts3,
42f1e 20 30 2c 20 30 2c 20 46 54 53 33 5f 53 45 47 43   0, 0, FTS3_SEGC
42f1f 55 52 53 4f 52 5f 41 4c 4c 2c 0a 20 20 20 20 20  URSOR_ALL,.     
42f20 20 70 43 73 72 2d 3e 66 69 6c 74 65 72 2e 7a 54   pCsr->filter.zT
42f21 65 72 6d 2c 20 70 43 73 72 2d 3e 66 69 6c 74 65  erm, pCsr->filte
42f22 72 2e 6e 54 65 72 6d 2c 20 30 2c 20 69 73 53 63  r.nTerm, 0, isSc
42f23 61 6e 2c 20 26 70 43 73 72 2d 3e 63 73 72 0a 20  an, &pCsr->csr. 
42f24 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   );.  if( rc==SQ
42f25 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
42f26 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 53  c = sqlite3Fts3S
42f27 65 67 52 65 61 64 65 72 53 74 61 72 74 28 70 46  egReaderStart(pF
42f28 74 73 33 2c 20 26 70 43 73 72 2d 3e 63 73 72 2c  ts3, &pCsr->csr,
42f29 20 26 70 43 73 72 2d 3e 66 69 6c 74 65 72 29 3b   &pCsr->filter);
42f2a 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
42f2b 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d  SQLITE_OK ) rc =
42f2c 20 66 74 73 33 61 75 78 4e 65 78 74 4d 65 74 68   fts3auxNextMeth
42f2d 6f 64 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 72  od(pCursor);.  r
42f2e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
42f2f 2a 2a 20 78 45 6f 66 20 2d 20 52 65 74 75 72 6e  ** xEof - Return
42f30 20 74 72 75 65 20 69 66 20 74 68 65 20 63 75 72   true if the cur
42f31 73 6f 72 20 69 73 20 61 74 20 45 4f 46 2c 20 6f  sor is at EOF, o
42f32 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73  r false otherwis
42f33 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
42f34 20 66 74 73 33 61 75 78 45 6f 66 4d 65 74 68 6f   fts3auxEofMetho
42f35 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  d(sqlite3_vtab_c
42f36 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
42f37 0a 20 20 46 74 73 33 61 75 78 43 75 72 73 6f 72  .  Fts3auxCursor
42f38 20 2a 70 43 73 72 20 3d 20 28 46 74 73 33 61 75   *pCsr = (Fts3au
42f39 78 43 75 72 73 6f 72 20 2a 29 70 43 75 72 73 6f  xCursor *)pCurso
42f3a 72 3b 0a 20 20 72 65 74 75 72 6e 20 70 43 73 72  r;.  return pCsr
42f3b 2d 3e 69 73 45 6f 66 3b 0a 7d 0a 0a 2f 2a 0a 2a  ->isEof;.}../*.*
42f3c 2a 20 78 43 6f 6c 75 6d 6e 20 2d 20 52 65 74 75  * xColumn - Retu
42f3d 72 6e 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75  rn a column valu
42f3e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
42f3f 20 66 74 73 33 61 75 78 43 6f 6c 75 6d 6e 4d 65   fts3auxColumnMe
42f40 74 68 6f 64 28 0a 20 20 73 71 6c 69 74 65 33 5f  thod(.  sqlite3_
42f41 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
42f42 72 73 6f 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f  rsor,   /* Curso
42f43 72 20 74 6f 20 72 65 74 72 69 65 76 65 20 76 61  r to retrieve va
42f44 6c 75 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 73 71  lue from */.  sq
42f45 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
42f46 43 6f 6e 74 65 78 74 2c 20 20 20 20 20 20 2f 2a  Context,      /*
42f47 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 73 71 6c   Context for sql
42f48 69 74 65 33 5f 72 65 73 75 6c 74 5f 78 78 78 28  ite3_result_xxx(
42f49 29 20 63 61 6c 6c 73 20 2a 2f 0a 20 20 69 6e 74  ) calls */.  int
42f4a 20 69 43 6f 6c 20 20 20 20 20 20 20 20 20 20 20   iCol           
42f4b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
42f4c 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20  Index of column 
42f4d 74 6f 20 72 65 61 64 20 76 61 6c 75 65 20 66 72  to read value fr
42f4e 6f 6d 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 61  om */.){.  Fts3a
42f4f 75 78 43 75 72 73 6f 72 20 2a 70 20 3d 20 28 46  uxCursor *p = (F
42f50 74 73 33 61 75 78 43 75 72 73 6f 72 20 2a 29 70  ts3auxCursor *)p
42f51 43 75 72 73 6f 72 3b 0a 0a 20 20 61 73 73 65 72  Cursor;..  asser
42f52 74 28 20 70 2d 3e 69 73 45 6f 66 3d 3d 30 20 29  t( p->isEof==0 )
42f53 3b 0a 20 20 69 66 28 20 69 43 6f 6c 3d 3d 30 20  ;.  if( iCol==0 
42f54 29 7b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c  ){        /* Col
42f55 75 6d 6e 20 22 74 65 72 6d 22 20 2a 2f 0a 20 20  umn "term" */.  
42f56 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
42f57 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74 2c 20  _text(pContext, 
42f58 70 2d 3e 63 73 72 2e 7a 54 65 72 6d 2c 20 70 2d  p->csr.zTerm, p-
42f59 3e 63 73 72 2e 6e 54 65 72 6d 2c 20 53 51 4c 49  >csr.nTerm, SQLI
42f5a 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
42f5b 20 7d 65 6c 73 65 20 69 66 28 20 69 43 6f 6c 3d   }else if( iCol=
42f5c 3d 31 20 29 7b 20 20 2f 2a 20 43 6f 6c 75 6d 6e  =1 ){  /* Column
42f5d 20 22 63 6f 6c 22 20 2a 2f 0a 20 20 20 20 69 66   "col" */.    if
42f5e 28 20 70 2d 3e 69 43 6f 6c 20 29 7b 0a 20 20 20  ( p->iCol ){.   
42f5f 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
42f60 74 5f 69 6e 74 28 70 43 6f 6e 74 65 78 74 2c 20  t_int(pContext, 
42f61 70 2d 3e 69 43 6f 6c 2d 31 29 3b 0a 20 20 20 20  p->iCol-1);.    
42f62 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
42f63 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
42f64 28 70 43 6f 6e 74 65 78 74 2c 20 22 2a 22 2c 20  (pContext, "*", 
42f65 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  -1, SQLITE_STATI
42f66 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  C);.    }.  }els
42f67 65 20 69 66 28 20 69 43 6f 6c 3d 3d 32 20 29 7b  e if( iCol==2 ){
42f68 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 22 64 6f 63    /* Column "doc
42f69 75 6d 65 6e 74 73 22 20 2a 2f 0a 20 20 20 20 73  uments" */.    s
42f6a 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
42f6b 74 36 34 28 70 43 6f 6e 74 65 78 74 2c 20 70 2d  t64(pContext, p-
42f6c 3e 61 53 74 61 74 5b 70 2d 3e 69 43 6f 6c 5d 2e  >aStat[p->iCol].
42f6d 6e 44 6f 63 29 3b 0a 20 20 7d 65 6c 73 65 7b 20  nDoc);.  }else{ 
42f6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
42f6f 2a 20 43 6f 6c 75 6d 6e 20 22 6f 63 63 75 72 72  * Column "occurr
42f70 65 6e 63 65 73 22 20 2a 2f 0a 20 20 20 20 73 71  ences" */.    sq
42f71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
42f72 36 34 28 70 43 6f 6e 74 65 78 74 2c 20 70 2d 3e  64(pContext, p->
42f73 61 53 74 61 74 5b 70 2d 3e 69 43 6f 6c 5d 2e 6e  aStat[p->iCol].n
42f74 4f 63 63 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  Occ);.  }..  ret
42f75 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
42f76 0a 0a 2f 2a 0a 2a 2a 20 78 52 6f 77 69 64 20 2d  ../*.** xRowid -
42f77 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72   Return the curr
42f78 65 6e 74 20 72 6f 77 69 64 20 66 6f 72 20 74 68  ent rowid for th
42f79 65 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61  e cursor..*/.sta
42f7a 74 69 63 20 69 6e 74 20 66 74 73 33 61 75 78 52  tic int fts3auxR
42f7b 6f 77 69 64 4d 65 74 68 6f 64 28 0a 20 20 73 71  owidMethod(.  sq
42f7c 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
42f7d 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 20 2f 2a  r *pCursor,   /*
42f7e 20 43 75 72 73 6f 72 20 74 6f 20 72 65 74 72 69   Cursor to retri
42f7f 65 76 65 20 76 61 6c 75 65 20 66 72 6f 6d 20 2a  eve value from *
42f80 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  /.  sqlite_int64
42f81 20 2a 70 52 6f 77 69 64 20 20 20 20 20 20 20 20   *pRowid        
42f82 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 6f 77 69      /* OUT: Rowi
42f83 64 20 76 61 6c 75 65 20 2a 2f 0a 29 7b 0a 20 20  d value */.){.  
42f84 46 74 73 33 61 75 78 43 75 72 73 6f 72 20 2a 70  Fts3auxCursor *p
42f85 43 73 72 20 3d 20 28 46 74 73 33 61 75 78 43 75  Csr = (Fts3auxCu
42f86 72 73 6f 72 20 2a 29 70 43 75 72 73 6f 72 3b 0a  rsor *)pCursor;.
42f87 20 20 2a 70 52 6f 77 69 64 20 3d 20 70 43 73 72    *pRowid = pCsr
42f88 2d 3e 69 52 6f 77 69 64 3b 0a 20 20 72 65 74 75  ->iRowid;.  retu
42f89 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
42f8a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
42f8b 74 68 65 20 66 74 73 33 61 75 78 20 6d 6f 64 75  the fts3aux modu
42f8c 6c 65 20 77 69 74 68 20 64 61 74 61 62 61 73 65  le with database
42f8d 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 20   connection db. 
42f8e 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
42f8f 0a 2a 2a 20 69 66 20 73 75 63 63 65 73 73 66 75  .** if successfu
42f90 6c 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f  l or an error co
42f91 64 65 20 69 66 20 73 71 6c 69 74 65 33 5f 63 72  de if sqlite3_cr
42f92 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 66 61  eate_module() fa
42f93 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ils..*/.SQLITE_P
42f94 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
42f95 65 33 46 74 73 33 49 6e 69 74 41 75 78 28 73 71  e3Fts3InitAux(sq
42f96 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 74  lite3 *db){.  st
42f97 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
42f98 65 33 5f 6d 6f 64 75 6c 65 20 66 74 73 33 61 75  e3_module fts3au
42f99 78 5f 6d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 20  x_module = {.   
42f9a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
42f9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
42f9c 2a 20 69 56 65 72 73 69 6f 6e 20 20 20 20 20 20  * iVersion      
42f9d 2a 2f 0a 20 20 20 20 20 66 74 73 33 61 75 78 43  */.     fts3auxC
42f9e 6f 6e 6e 65 63 74 4d 65 74 68 6f 64 2c 20 20 20  onnectMethod,   
42f9f 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20       /* xCreate 
42fa0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 66 74        */.     ft
42fa1 73 33 61 75 78 43 6f 6e 6e 65 63 74 4d 65 74 68  s3auxConnectMeth
42fa2 6f 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 43  od,        /* xC
42fa3 6f 6e 6e 65 63 74 20 20 20 20 20 20 2a 2f 0a 20  onnect      */. 
42fa4 20 20 20 20 66 74 73 33 61 75 78 42 65 73 74 49      fts3auxBestI
42fa5 6e 64 65 78 4d 65 74 68 6f 64 2c 20 20 20 20 20  ndexMethod,     
42fa6 20 2f 2a 20 78 42 65 73 74 49 6e 64 65 78 20 20   /* xBestIndex  
42fa7 20 20 2a 2f 0a 20 20 20 20 20 66 74 73 33 61 75    */.     fts3au
42fa8 78 44 69 73 63 6f 6e 6e 65 63 74 4d 65 74 68 6f  xDisconnectMetho
42fa9 64 2c 20 20 20 20 20 2f 2a 20 78 44 69 73 63 6f  d,     /* xDisco
42faa 6e 6e 65 63 74 20 20 20 2a 2f 0a 20 20 20 20 20  nnect   */.     
42fab 66 74 73 33 61 75 78 44 69 73 63 6f 6e 6e 65 63  fts3auxDisconnec
42fac 74 4d 65 74 68 6f 64 2c 20 20 20 20 20 2f 2a 20  tMethod,     /* 
42fad 78 44 65 73 74 72 6f 79 20 20 20 20 20 20 2a 2f  xDestroy      */
42fae 0a 20 20 20 20 20 66 74 73 33 61 75 78 4f 70 65  .     fts3auxOpe
42faf 6e 4d 65 74 68 6f 64 2c 20 20 20 20 20 20 20 20  nMethod,        
42fb0 20 20 20 2f 2a 20 78 4f 70 65 6e 20 20 20 20 20     /* xOpen     
42fb1 20 20 20 20 2a 2f 0a 20 20 20 20 20 66 74 73 33      */.     fts3
42fb2 61 75 78 43 6c 6f 73 65 4d 65 74 68 6f 64 2c 20  auxCloseMethod, 
42fb3 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f           /* xClo
42fb4 73 65 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  se        */.   
42fb5 20 20 66 74 73 33 61 75 78 46 69 6c 74 65 72 4d    fts3auxFilterM
42fb6 65 74 68 6f 64 2c 20 20 20 20 20 20 20 20 20 2f  ethod,         /
42fb7 2a 20 78 46 69 6c 74 65 72 20 20 20 20 20 20 20  * xFilter       
42fb8 2a 2f 0a 20 20 20 20 20 66 74 73 33 61 75 78 4e  */.     fts3auxN
42fb9 65 78 74 4d 65 74 68 6f 64 2c 20 20 20 20 20 20  extMethod,      
42fba 20 20 20 20 20 2f 2a 20 78 4e 65 78 74 20 20 20       /* xNext   
42fbb 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 66 74        */.     ft
42fbc 73 33 61 75 78 45 6f 66 4d 65 74 68 6f 64 2c 20  s3auxEofMethod, 
42fbd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 45             /* xE
42fbe 6f 66 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  of          */. 
42fbf 20 20 20 20 66 74 73 33 61 75 78 43 6f 6c 75 6d      fts3auxColum
42fc0 6e 4d 65 74 68 6f 64 2c 20 20 20 20 20 20 20 20  nMethod,        
42fc1 20 2f 2a 20 78 43 6f 6c 75 6d 6e 20 20 20 20 20   /* xColumn     
42fc2 20 20 2a 2f 0a 20 20 20 20 20 66 74 73 33 61 75    */.     fts3au
42fc3 78 52 6f 77 69 64 4d 65 74 68 6f 64 2c 20 20 20  xRowidMethod,   
42fc4 20 20 20 20 20 20 20 2f 2a 20 78 52 6f 77 69 64         /* xRowid
42fc5 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
42fc6 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
42fc7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
42fc8 78 55 70 64 61 74 65 20 20 20 20 20 20 20 2a 2f  xUpdate       */
42fc9 0a 20 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  .     0,        
42fca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42fcb 20 20 20 2f 2a 20 78 42 65 67 69 6e 20 20 20 20     /* xBegin    
42fcc 20 20 20 20 2a 2f 0a 20 20 20 20 20 30 2c 20 20      */.     0,  
42fcd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42fce 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 79 6e           /* xSyn
42fcf 63 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  c         */.   
42fd0 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
42fd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
42fd2 2a 20 78 43 6f 6d 6d 69 74 20 20 20 20 20 20 20  * xCommit       
42fd3 2a 2f 0a 20 20 20 20 20 30 2c 20 20 20 20 20 20  */.     0,      
42fd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42fd5 20 20 20 20 20 2f 2a 20 78 52 6f 6c 6c 62 61 63       /* xRollbac
42fd6 6b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 30 2c  k     */.     0,
42fd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42fd8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46             /* xF
42fd9 69 6e 64 46 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  indFunction */. 
42fda 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
42fdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42fdc 20 2f 2a 20 78 52 65 6e 61 6d 65 20 20 20 20 20   /* xRename     
42fdd 20 20 2a 2f 0a 20 20 20 20 20 30 2c 20 20 20 20    */.     0,    
42fde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42fdf 20 20 20 20 20 20 20 2f 2a 20 78 53 61 76 65 70         /* xSavep
42fe0 6f 69 6e 74 20 20 20 20 2a 2f 0a 20 20 20 20 20  oint    */.     
42fe1 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
42fe2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
42fe3 78 52 65 6c 65 61 73 65 20 20 20 20 20 20 2a 2f  xRelease      */
42fe4 0a 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20  .     0         
42fe5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42fe6 20 20 20 2f 2a 20 78 52 6f 6c 6c 62 61 63 6b 54     /* xRollbackT
42fe7 6f 20 20 20 2a 2f 0a 20 20 7d 3b 0a 20 20 69 6e  o   */.  };.  in
42fe8 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
42fe9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
42fea 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
42feb 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
42fec 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 64 62  create_module(db
42fed 2c 20 22 66 74 73 34 61 75 78 22 2c 20 26 66 74  , "fts4aux", &ft
42fee 73 33 61 75 78 5f 6d 6f 64 75 6c 65 2c 20 30 29  s3aux_module, 0)
42fef 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
42ff0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
42ff1 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
42ff2 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
42ff3 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
42ff4 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
42ff5 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33  **** End of fts3
42ff6 5f 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _aux.c *********
42ff7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42ff8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42ff9 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
42ffa 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
42ffb 66 74 73 33 5f 65 78 70 72 2e 63 20 2a 2a 2a 2a  fts3_expr.c ****
42ffc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42ffd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42ffe 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
42fff 4e 6f 76 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65  Nov 28.**.** The
43000 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
43001 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
43002 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
43003 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
43004 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
43005 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
43006 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
43007 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
43008 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
43009 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
4300a 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
4300b 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
4300c 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
4300d 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
4300e 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
4300f 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
43010 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
43011 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43012 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43013 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43014 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43015 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
43016 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 63 6f 6e   This module con
43017 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20  tains code that 
43018 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 70 61 72  implements a par
43019 73 65 72 20 66 6f 72 20 66 74 73 33 20 71 75 65  ser for fts3 que
4301a 72 79 20 73 74 72 69 6e 67 73 0a 2a 2a 20 28 74  ry strings.** (t
4301b 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 61 72  he right-hand ar
4301c 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 4d 41  gument to the MA
4301d 54 43 48 20 6f 70 65 72 61 74 6f 72 29 2e 20 42  TCH operator). B
4301e 65 63 61 75 73 65 20 74 68 65 20 73 75 70 70 6f  ecause the suppo
4301f 72 74 65 64 20 0a 2a 2a 20 73 79 6e 74 61 78 20  rted .** syntax 
43020 69 73 20 72 65 6c 61 74 69 76 65 6c 79 20 73 69  is relatively si
43021 6d 70 6c 65 2c 20 74 68 65 20 77 68 6f 6c 65 20  mple, the whole 
43022 74 6f 6b 65 6e 69 7a 65 72 2f 70 61 72 73 65 72  tokenizer/parser
43023 20 73 79 73 74 65 6d 20 69 73 0a 2a 2a 20 68 61   system is.** ha
43024 6e 64 2d 63 6f 64 65 64 2e 20 0a 2a 2f 0a 23 69  nd-coded. .*/.#i
43025 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
43026 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
43027 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
43028 5f 46 54 53 33 29 0a 0a 2f 2a 0a 2a 2a 20 42 79  _FTS3)../*.** By
43029 20 64 65 66 61 75 6c 74 2c 20 74 68 69 73 20 6d   default, this m
4302a 6f 64 75 6c 65 20 70 61 72 73 65 73 20 74 68 65  odule parses the
4302b 20 6c 65 67 61 63 79 20 73 79 6e 74 61 78 20 74   legacy syntax t
4302c 68 61 74 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a  hat has been .**
4302d 20 74 72 61 64 69 74 69 6f 6e 61 6c 6c 79 20 75   traditionally u
4302e 73 65 64 20 62 79 20 66 74 73 33 2e 20 4f 72 2c  sed by fts3. Or,
4302f 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   if SQLITE_ENABL
43030 45 5f 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53  E_FTS3_PARENTHES
43031 49 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64  IS.** is defined
43032 2c 20 74 68 65 6e 20 69 74 20 75 73 65 73 20 74  , then it uses t
43033 68 65 20 6e 65 77 20 73 79 6e 74 61 78 2e 20 54  he new syntax. T
43034 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 20 62  he differences b
43035 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 6e 65  etween.** the ne
43036 77 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 73 79  w and the old sy
43037 6e 74 61 78 65 73 20 61 72 65 3a 0a 2a 2a 0a 2a  ntaxes are:.**.*
43038 2a 20 20 61 29 20 54 68 65 20 6e 65 77 20 73 79  *  a) The new sy
43039 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 70 61  ntax supports pa
4303a 72 65 6e 74 68 65 73 69 73 2e 20 54 68 65 20 6f  renthesis. The o
4303b 6c 64 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a  ld does not..**.
4303c 2a 2a 20 20 62 29 20 54 68 65 20 6e 65 77 20 73  **  b) The new s
4303d 79 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 74  yntax supports t
4303e 68 65 20 41 4e 44 20 61 6e 64 20 4e 4f 54 20 6f  he AND and NOT o
4303f 70 65 72 61 74 6f 72 73 2e 20 54 68 65 20 6f 6c  perators. The ol
43040 64 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a  d does not..**.*
43041 2a 20 20 63 29 20 54 68 65 20 6f 6c 64 20 73 79  *  c) The old sy
43042 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 74 68  ntax supports th
43043 65 20 22 2d 22 20 74 6f 6b 65 6e 20 71 75 61 6c  e "-" token qual
43044 69 66 69 65 72 2e 20 54 68 69 73 20 69 73 20 6e  ifier. This is n
43045 6f 74 20 0a 2a 2a 20 20 20 20 20 73 75 70 70 6f  ot .**     suppo
43046 72 74 65 64 20 62 79 20 74 68 65 20 6e 65 77 20  rted by the new 
43047 73 79 6e 74 61 78 20 28 69 74 20 69 73 20 72 65  syntax (it is re
43048 70 6c 61 63 65 64 20 62 79 20 74 68 65 20 4e 4f  placed by the NO
43049 54 20 6f 70 65 72 61 74 6f 72 29 2e 0a 2a 2a 0a  T operator)..**.
4304a 2a 2a 20 20 64 29 20 57 68 65 6e 20 75 73 69 6e  **  d) When usin
4304b 67 20 74 68 65 20 6f 6c 64 20 73 79 6e 74 61 78  g the old syntax
4304c 2c 20 74 68 65 20 4f 52 20 6f 70 65 72 61 74 6f  , the OR operato
4304d 72 20 68 61 73 20 61 20 67 72 65 61 74 65 72 20  r has a greater 
4304e 70 72 65 63 65 64 65 6e 63 65 0a 2a 2a 20 20 20  precedence.**   
4304f 20 20 74 68 61 6e 20 61 6e 20 69 6d 70 6c 69 63    than an implic
43050 69 74 20 41 4e 44 2e 20 57 68 65 6e 20 75 73 69  it AND. When usi
43051 6e 67 20 74 68 65 20 6e 65 77 2c 20 62 6f 74 68  ng the new, both
43052 20 69 6d 70 6c 69 63 69 74 79 20 61 6e 64 20 65   implicity and e
43053 78 70 6c 69 63 69 74 0a 2a 2a 20 20 20 20 20 41  xplicit.**     A
43054 4e 44 20 6f 70 65 72 61 74 6f 72 73 20 68 61 76  ND operators hav
43055 65 20 61 20 68 69 67 68 65 72 20 70 72 65 63 65  e a higher prece
43056 64 65 6e 63 65 20 74 68 61 6e 20 4f 52 2e 0a 2a  dence than OR..*
43057 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64  *.** If compiled
43058 20 77 69 74 68 20 53 51 4c 49 54 45 5f 54 45 53   with SQLITE_TES
43059 54 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20  T defined, then 
4305a 74 68 69 73 20 6d 6f 64 75 6c 65 20 65 78 70 6f  this module expo
4305b 72 74 73 20 74 68 65 0a 2a 2a 20 73 79 6d 62 6f  rts the.** symbo
4305c 6c 20 22 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  l "int sqlite3_f
4305d 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e  ts3_enable_paren
4305e 74 68 65 73 65 73 22 2e 20 53 65 74 74 69 6e 67  theses". Setting
4305f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 0a 2a   this variable.*
43060 2a 20 74 6f 20 7a 65 72 6f 20 63 61 75 73 65 73  * to zero causes
43061 20 74 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 75   the module to u
43062 73 65 20 74 68 65 20 6f 6c 64 20 73 79 6e 74 61  se the old synta
43063 78 2e 20 49 66 20 69 74 20 69 73 20 73 65 74 20  x. If it is set 
43064 74 6f 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20  to .** non-zero 
43065 74 68 65 20 6e 65 77 20 73 79 6e 74 61 78 20 69  the new syntax i
43066 73 20 61 63 74 69 76 61 74 65 64 2e 20 54 68 69  s activated. Thi
43067 73 20 69 73 20 73 6f 20 62 6f 74 68 20 73 79 6e  s is so both syn
43068 74 61 78 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20  taxes can.** be 
43069 74 65 73 74 65 64 20 75 73 69 6e 67 20 61 20 73  tested using a s
4306a 69 6e 67 6c 65 20 62 75 69 6c 64 20 6f 66 20 74  ingle build of t
4306b 65 73 74 66 69 78 74 75 72 65 2e 0a 2a 2a 0a 2a  estfixture..**.*
4306c 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
4306d 64 65 73 63 72 69 62 65 73 20 74 68 65 20 73 79  describes the sy
4306e 6e 74 61 78 20 73 75 70 70 6f 72 74 65 64 20 62  ntax supported b
4306f 79 20 74 68 65 20 66 74 73 33 20 4d 41 54 43 48  y the fts3 MATCH
43070 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 69 6e 20  .** operator in 
43071 61 20 73 69 6d 69 6c 61 72 20 66 6f 72 6d 61 74  a similar format
43072 20 74 6f 20 74 68 61 74 20 75 73 65 64 20 62 79   to that used by
43073 20 74 68 65 20 6c 65 6d 6f 6e 20 70 61 72 73 65   the lemon parse
43074 72 0a 2a 2a 20 67 65 6e 65 72 61 74 6f 72 2e 20  r.** generator. 
43075 54 68 69 73 20 6d 6f 64 75 6c 65 20 64 6f 65 73  This module does
43076 20 6e 6f 74 20 75 73 65 20 61 63 74 75 61 6c 6c   not use actuall
43077 79 20 6c 65 6d 6f 6e 2c 20 69 74 20 75 73 65 73  y lemon, it uses
43078 20 61 0a 2a 2a 20 63 75 73 74 6f 6d 20 70 61 72   a.** custom par
43079 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 71 75 65  ser..**.**   que
4307a 72 79 20 3a 3a 3d 20 61 6e 64 65 78 70 72 20 28  ry ::= andexpr (
4307b 4f 52 20 61 6e 64 65 78 70 72 29 2a 2e 0a 2a 2a  OR andexpr)*..**
4307c 0a 2a 2a 20 20 20 61 6e 64 65 78 70 72 20 3a 3a  .**   andexpr ::
4307d 3d 20 6e 6f 74 65 78 70 72 20 28 41 4e 44 3f 20  = notexpr (AND? 
4307e 6e 6f 74 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a  notexpr)*..**.**
4307f 20 20 20 6e 6f 74 65 78 70 72 20 3a 3a 3d 20 6e     notexpr ::= n
43080 65 61 72 65 78 70 72 20 28 4e 4f 54 20 6e 65 61  earexpr (NOT nea
43081 72 65 78 70 72 7c 2d 54 4f 4b 45 4e 29 2a 2e 0a  rexpr|-TOKEN)*..
43082 2a 2a 20 20 20 6e 6f 74 65 78 70 72 20 3a 3a 3d  **   notexpr ::=
43083 20 4c 50 20 71 75 65 72 79 20 52 50 2e 0a 2a 2a   LP query RP..**
43084 0a 2a 2a 20 20 20 6e 65 61 72 65 78 70 72 20 3a  .**   nearexpr :
43085 3a 3d 20 70 68 72 61 73 65 20 28 4e 45 41 52 20  := phrase (NEAR 
43086 64 69 73 74 61 6e 63 65 5f 6f 70 74 20 6e 65 61  distance_opt nea
43087 72 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20  rexpr)*..**.**  
43088 20 64 69 73 74 61 6e 63 65 5f 6f 70 74 20 3a 3a   distance_opt ::
43089 3d 20 2e 0a 2a 2a 20 20 20 64 69 73 74 61 6e 63  = ..**   distanc
4308a 65 5f 6f 70 74 20 3a 3a 3d 20 2f 20 49 4e 54 45  e_opt ::= / INTE
4308b 47 45 52 2e 0a 2a 2a 0a 2a 2a 20 20 20 70 68 72  GER..**.**   phr
4308c 61 73 65 20 3a 3a 3d 20 54 4f 4b 45 4e 2e 0a 2a  ase ::= TOKEN..*
4308d 2a 20 20 20 70 68 72 61 73 65 20 3a 3a 3d 20 43  *   phrase ::= C
4308e 4f 4c 55 4d 4e 3a 54 4f 4b 45 4e 2e 0a 2a 2a 20  OLUMN:TOKEN..** 
4308f 20 20 70 68 72 61 73 65 20 3a 3a 3d 20 22 54 4f    phrase ::= "TO
43090 4b 45 4e 20 54 4f 4b 45 4e 20 54 4f 4b 45 4e 2e  KEN TOKEN TOKEN.
43091 2e 2e 22 2e 0a 2a 2f 0a 0a 23 69 66 64 65 66 20  .."..*/..#ifdef 
43092 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
43093 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
43094 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70  e3_fts3_enable_p
43095 61 72 65 6e 74 68 65 73 65 73 20 3d 20 30 3b 0a  arentheses = 0;.
43096 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 53 51  #else.# ifdef SQ
43097 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
43098 5f 50 41 52 45 4e 54 48 45 53 49 53 20 0a 23 20  _PARENTHESIS .# 
43099 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
4309a 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65  fts3_enable_pare
4309b 6e 74 68 65 73 65 73 20 31 0a 23 20 65 6c 73 65  ntheses 1.# else
4309c 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74  .#  define sqlit
4309d 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70  e3_fts3_enable_p
4309e 61 72 65 6e 74 68 65 73 65 73 20 30 0a 23 20 65  arentheses 0.# e
4309f 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  ndif.#endif../*.
430a0 2a 2a 20 44 65 66 61 75 6c 74 20 73 70 61 6e 20  ** Default span 
430a1 66 6f 72 20 4e 45 41 52 20 6f 70 65 72 61 74 6f  for NEAR operato
430a2 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  rs..*/.#define S
430a3 51 4c 49 54 45 5f 46 54 53 33 5f 44 45 46 41 55  QLITE_FTS3_DEFAU
430a4 4c 54 5f 4e 45 41 52 5f 50 41 52 41 4d 20 31 30  LT_NEAR_PARAM 10
430a5 0a 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73  ../* #include <s
430a6 74 72 69 6e 67 2e 68 3e 20 2a 2f 0a 2f 2a 20 23  tring.h> */./* #
430a7 69 6e 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e  include <assert.
430a8 68 3e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 69 73 4e  h> */../*.** isN
430a9 6f 74 3a 0a 2a 2a 20 20 20 54 68 69 73 20 76 61  ot:.**   This va
430aa 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20 62  riable is used b
430ab 79 20 66 75 6e 63 74 69 6f 6e 20 67 65 74 4e 65  y function getNe
430ac 78 74 4e 6f 64 65 28 29 2e 20 57 68 65 6e 20 67  xtNode(). When g
430ad 65 74 4e 65 78 74 4e 6f 64 65 28 29 20 69 73 0a  etNextNode() is.
430ae 2a 2a 20 20 20 63 61 6c 6c 65 64 2c 20 69 74 20  **   called, it 
430af 73 65 74 73 20 50 61 72 73 65 43 6f 6e 74 65 78  sets ParseContex
430b0 74 2e 69 73 4e 6f 74 20 74 6f 20 74 72 75 65 20  t.isNot to true 
430b1 69 66 20 74 68 65 20 27 6e 65 78 74 20 6e 6f 64  if the 'next nod
430b2 65 27 20 69 73 20 61 20 0a 2a 2a 20 20 20 46 54  e' is a .**   FT
430b3 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 77 69  SQUERY_PHRASE wi
430b4 74 68 20 61 20 75 6e 61 72 79 20 22 2d 22 20 61  th a unary "-" a
430b5 74 74 61 63 68 65 64 20 74 6f 20 69 74 2e 20 69  ttached to it. i
430b6 2e 65 2e 20 22 6d 79 73 71 6c 22 20 69 6e 20 74  .e. "mysql" in t
430b7 68 65 0a 2a 2a 20 20 20 46 54 53 33 20 71 75 65  he.**   FTS3 que
430b8 72 79 20 22 73 71 6c 69 74 65 20 2d 6d 79 73 71  ry "sqlite -mysq
430b9 6c 22 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 50  l". Otherwise, P
430ba 61 72 73 65 43 6f 6e 74 65 78 74 2e 69 73 4e 6f  arseContext.isNo
430bb 74 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 20  t is set to.**  
430bc 20 7a 65 72 6f 2e 0a 2a 2f 0a 74 79 70 65 64 65   zero..*/.typede
430bd 66 20 73 74 72 75 63 74 20 50 61 72 73 65 43 6f  f struct ParseCo
430be 6e 74 65 78 74 20 50 61 72 73 65 43 6f 6e 74 65  ntext ParseConte
430bf 78 74 3b 0a 73 74 72 75 63 74 20 50 61 72 73 65  xt;.struct Parse
430c0 43 6f 6e 74 65 78 74 20 7b 0a 20 20 73 71 6c 69  Context {.  sqli
430c1 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
430c2 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20 20 20  Tokenizer;      
430c3 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64  /* Tokenizer mod
430c4 75 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 61  ule */.  int iLa
430c5 6e 67 69 64 3b 20 20 20 20 20 20 20 20 20 20 20  ngid;           
430c6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
430c7 4c 61 6e 67 75 61 67 65 20 69 64 20 75 73 65 64  Language id used
430c8 20 77 69 74 68 20 74 6f 6b 65 6e 69 7a 65 72 20   with tokenizer 
430c9 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
430ca 2a 2a 61 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20  **azCol;        
430cb 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61           /* Arra
430cc 79 20 6f 66 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  y of column name
430cd 73 20 66 6f 72 20 66 74 73 33 20 74 61 62 6c 65  s for fts3 table
430ce 20 2a 2f 0a 20 20 69 6e 74 20 62 46 74 73 34 3b   */.  int bFts4;
430cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
430d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
430d1 65 20 74 6f 20 61 6c 6c 6f 77 20 46 54 53 34 2d  e to allow FTS4-
430d2 6f 6e 6c 79 20 73 79 6e 74 61 78 20 2a 2f 0a 20  only syntax */. 
430d3 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20   int nCol;      
430d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
430d5 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
430d6 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 7a 43  f entries in azC
430d7 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 44  ol[] */.  int iD
430d8 65 66 61 75 6c 74 43 6f 6c 3b 20 20 20 20 20 20  efaultCol;      
430d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
430da 20 44 65 66 61 75 6c 74 20 63 6f 6c 75 6d 6e 20   Default column 
430db 74 6f 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e  to query */.  in
430dc 74 20 69 73 4e 6f 74 3b 20 20 20 20 20 20 20 20  t isNot;        
430dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
430de 20 20 2f 2a 20 54 72 75 65 20 69 66 20 67 65 74    /* True if get
430df 4e 65 78 74 4e 6f 64 65 28 29 20 73 65 65 73 20  NextNode() sees 
430e0 61 20 75 6e 61 72 79 20 2d 20 2a 2f 0a 20 20 73  a unary - */.  s
430e1 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
430e2 70 43 74 78 3b 20 20 20 20 20 20 20 20 20 20 20  pCtx;           
430e3 20 20 20 2f 2a 20 57 72 69 74 65 20 65 72 72 6f     /* Write erro
430e4 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 2a  r message here *
430e5 2f 0a 20 20 69 6e 74 20 6e 4e 65 73 74 3b 20 20  /.  int nNest;  
430e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
430e7 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
430e8 72 20 6f 66 20 6e 65 73 74 65 64 20 62 72 61 63  r of nested brac
430e9 6b 65 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  kets */.};../*.*
430ea 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
430eb 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  is equivalent to
430ec 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 69 73   the standard is
430ed 73 70 61 63 65 28 29 20 66 75 6e 63 74 69 6f 6e  space() function
430ee 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 61  . .**.** The sta
430ef 6e 64 61 72 64 20 69 73 73 70 61 63 65 28 29 20  ndard isspace() 
430f0 63 61 6e 20 62 65 20 61 77 6b 77 61 72 64 20 74  can be awkward t
430f1 6f 20 75 73 65 20 73 61 66 65 6c 79 2c 20 62 65  o use safely, be
430f2 63 61 75 73 65 20 61 6c 74 68 6f 75 67 68 20 69  cause although i
430f3 74 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 20  t.** is defined 
430f4 74 6f 20 61 63 63 65 70 74 20 61 6e 20 61 72 67  to accept an arg
430f5 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20 69 6e  ument of type in
430f6 74 2c 20 69 74 73 20 62 65 68 61 76 69 6f 75 72  t, its behaviour
430f7 20 77 68 65 6e 20 70 61 73 73 65 64 0a 2a 2a 20   when passed.** 
430f8 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 20  an integer that 
430f9 66 61 6c 6c 73 20 6f 75 74 73 69 64 65 20 6f 66  falls outside of
430fa 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 74 68   the range of th
430fb 65 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  e unsigned char 
430fc 74 79 70 65 0a 2a 2a 20 69 73 20 75 6e 64 65 66  type.** is undef
430fd 69 6e 65 64 20 28 61 6e 64 20 73 6f 6d 65 74 69  ined (and someti
430fe 6d 65 73 2c 20 22 75 6e 64 65 66 69 6e 65 64 22  mes, "undefined"
430ff 20 6d 65 61 6e 73 20 73 65 67 66 61 75 6c 74 29   means segfault)
43100 2e 20 54 68 69 73 20 77 72 61 70 70 65 72 0a 2a  . This wrapper.*
43101 2a 20 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20  * is defined to 
43102 61 63 63 65 70 74 20 61 6e 20 61 72 67 75 6d 65  accept an argume
43103 6e 74 20 6f 66 20 74 79 70 65 20 63 68 61 72 2c  nt of type char,
43104 20 61 6e 64 20 61 6c 77 61 79 73 20 72 65 74 75   and always retu
43105 72 6e 73 20 30 20 66 6f 72 0a 2a 2a 20 61 6e 79  rns 0 for.** any
43106 20 76 61 6c 75 65 73 20 74 68 61 74 20 66 61 6c   values that fal
43107 6c 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65  l outside of the
43108 20 72 61 6e 67 65 20 6f 66 20 74 68 65 20 75 6e   range of the un
43109 73 69 67 6e 65 64 20 63 68 61 72 20 74 79 70 65  signed char type
4310a 20 28 69 2e 65 2e 0a 2a 2a 20 6e 65 67 61 74 69   (i.e..** negati
4310b 76 65 20 76 61 6c 75 65 73 29 2e 0a 2a 2f 0a 73  ve values)..*/.s
4310c 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 69 73  tatic int fts3is
4310d 73 70 61 63 65 28 63 68 61 72 20 63 29 7b 0a 20  space(char c){. 
4310e 20 72 65 74 75 72 6e 20 63 3d 3d 27 20 27 20 7c   return c==' ' |
4310f 7c 20 63 3d 3d 27 5c 74 27 20 7c 7c 20 63 3d 3d  | c=='\t' || c==
43110 27 5c 6e 27 20 7c 7c 20 63 3d 3d 27 5c 72 27 20  '\n' || c=='\r' 
43111 7c 7c 20 63 3d 3d 27 5c 76 27 20 7c 7c 20 63 3d  || c=='\v' || c=
43112 3d 27 5c 66 27 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ='\f';.}../*.** 
43113 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62  Allocate nByte b
43114 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 75  ytes of memory u
43115 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 6d 61 6c  sing sqlite3_mal
43116 6c 6f 63 28 29 2e 20 49 66 20 73 75 63 63 65 73  loc(). If succes
43117 73 66 75 6c 2c 0a 2a 2a 20 7a 65 72 6f 20 74 68  sful,.** zero th
43118 65 20 6d 65 6d 6f 72 79 20 62 65 66 6f 72 65 20  e memory before 
43119 72 65 74 75 72 6e 69 6e 67 20 61 20 70 6f 69 6e  returning a poin
4311a 74 65 72 20 74 6f 20 69 74 2e 20 49 66 20 75 6e  ter to it. If un
4311b 73 75 63 63 65 73 73 66 75 6c 2c 20 0a 2a 2a 20  successful, .** 
4311c 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a  return NULL..*/.
4311d 73 74 61 74 69 63 20 76 6f 69 64 20 2a 66 74 73  static void *fts
4311e 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 20  3MallocZero(int 
4311f 6e 42 79 74 65 29 7b 0a 20 20 76 6f 69 64 20 2a  nByte){.  void *
43120 70 52 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  pRet = sqlite3_m
43121 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20  alloc(nByte);.  
43122 69 66 28 20 70 52 65 74 20 29 20 6d 65 6d 73 65  if( pRet ) memse
43123 74 28 70 52 65 74 2c 20 30 2c 20 6e 42 79 74 65  t(pRet, 0, nByte
43124 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74  );.  return pRet
43125 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.}..SQLITE_PRIV
43126 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
43127 74 73 33 4f 70 65 6e 54 6f 6b 65 6e 69 7a 65 72  ts3OpenTokenizer
43128 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  (.  sqlite3_toke
43129 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
4312a 72 2c 0a 20 20 69 6e 74 20 69 4c 61 6e 67 69 64  r,.  int iLangid
4312b 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
4312c 7a 2c 0a 20 20 69 6e 74 20 6e 2c 0a 20 20 73 71  z,.  int n,.  sq
4312d 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
4312e 63 75 72 73 6f 72 20 2a 2a 70 70 43 73 72 0a 29  cursor **ppCsr.)
4312f 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  {.  sqlite3_toke
43130 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e  nizer_module con
43131 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 54  st *pModule = pT
43132 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c  okenizer->pModul
43133 65 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  e;.  sqlite3_tok
43134 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70  enizer_cursor *p
43135 43 73 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  Csr = 0;.  int r
43136 63 3b 0a 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75  c;..  rc = pModu
43137 6c 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e  le->xOpen(pToken
43138 69 7a 65 72 2c 20 7a 2c 20 6e 2c 20 26 70 43 73  izer, z, n, &pCs
43139 72 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  r);.  assert( rc
4313a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
4313b 43 73 72 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20  Csr==0 );.  if( 
4313c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
4313d 0a 20 20 20 20 70 43 73 72 2d 3e 70 54 6f 6b 65  .    pCsr->pToke
4313e 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a  nizer = pTokeniz
4313f 65 72 3b 0a 20 20 20 20 69 66 28 20 70 4d 6f 64  er;.    if( pMod
43140 75 6c 65 2d 3e 69 56 65 72 73 69 6f 6e 3e 3d 31  ule->iVersion>=1
43141 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70   ){.      rc = p
43142 4d 6f 64 75 6c 65 2d 3e 78 4c 61 6e 67 75 61 67  Module->xLanguag
43143 65 69 64 28 70 43 73 72 2c 20 69 4c 61 6e 67 69  eid(pCsr, iLangi
43144 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  d);.      if( rc
43145 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
43146 20 20 20 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e         pModule->
43147 78 43 6c 6f 73 65 28 70 43 73 72 29 3b 0a 20 20  xClose(pCsr);.  
43148 20 20 20 20 20 20 70 43 73 72 20 3d 20 30 3b 0a        pCsr = 0;.
43149 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
4314a 7d 0a 20 20 2a 70 70 43 73 72 20 3d 20 70 43 73  }.  *ppCsr = pCs
4314b 72 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  r;.  return rc;.
4314c 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63  }.../*.** Extrac
4314d 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e  t the next token
4314e 20 66 72 6f 6d 20 62 75 66 66 65 72 20 7a 20 28   from buffer z (
4314f 6c 65 6e 67 74 68 20 6e 29 20 75 73 69 6e 67 20  length n) using 
43150 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a  the tokenizer.**
43151 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 66 6f 72   and other infor
43152 6d 61 74 69 6f 6e 20 28 63 6f 6c 75 6d 6e 20 6e  mation (column n
43153 61 6d 65 73 20 65 74 63 2e 29 20 69 6e 20 70 50  ames etc.) in pP
43154 61 72 73 65 2e 20 43 72 65 61 74 65 20 61 6e 20  arse. Create an 
43155 46 74 73 33 45 78 70 72 0a 2a 2a 20 73 74 72 75  Fts3Expr.** stru
43156 63 74 75 72 65 20 6f 66 20 74 79 70 65 20 46 54  cture of type FT
43157 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 63 6f  SQUERY_PHRASE co
43158 6e 74 61 69 6e 69 6e 67 20 61 20 70 68 72 61 73  ntaining a phras
43159 65 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20  e consisting of 
4315a 74 68 69 73 0a 2a 2a 20 73 69 6e 67 6c 65 20 74  this.** single t
4315b 6f 6b 65 6e 20 61 6e 64 20 73 65 74 20 2a 70 70  oken and set *pp
4315c 45 78 70 72 20 74 6f 20 70 6f 69 6e 74 20 74 6f  Expr to point to
4315d 20 69 74 2e 20 49 66 20 74 68 65 20 65 6e 64 20   it. If the end 
4315e 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 69 73  of the buffer is
4315f 0a 2a 2a 20 72 65 61 63 68 65 64 20 62 65 66 6f  .** reached befo
43160 72 65 20 61 20 74 6f 6b 65 6e 20 69 73 20 66 6f  re a token is fo
43161 75 6e 64 2c 20 73 65 74 20 2a 70 70 45 78 70 72  und, set *ppExpr
43162 20 74 6f 20 7a 65 72 6f 2e 20 49 74 20 69 73 20   to zero. It is 
43163 74 68 65 0a 2a 2a 20 72 65 73 70 6f 6e 73 69 62  the.** responsib
43164 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c  ility of the cal
43165 6c 65 72 20 74 6f 20 65 76 65 6e 74 75 61 6c 6c  ler to eventuall
43166 79 20 64 65 61 6c 6c 6f 63 61 74 65 20 74 68 65  y deallocate the
43167 20 61 6c 6c 6f 63 61 74 65 64 20 0a 2a 2a 20 46   allocated .** F
43168 74 73 33 45 78 70 72 20 73 74 72 75 63 74 75 72  ts3Expr structur
43169 65 20 28 69 66 20 61 6e 79 29 20 62 79 20 70 61  e (if any) by pa
4316a 73 73 69 6e 67 20 69 74 20 74 6f 20 73 71 6c 69  ssing it to sqli
4316b 74 65 33 5f 66 72 65 65 28 29 2e 0a 2a 2a 0a 2a  te3_free()..**.*
4316c 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
4316d 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75 6c  OK if successful
4316e 2c 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  , or SQLITE_NOME
4316f 4d 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  M if a memory al
43170 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 66 61 69 6c  location.** fail
43171 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
43172 20 67 65 74 4e 65 78 74 54 6f 6b 65 6e 28 0a 20   getNextToken(. 
43173 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 70   ParseContext *p
43174 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
43175 20 20 20 20 20 20 20 20 20 2f 2a 20 66 74 73 33           /* fts3
43176 20 71 75 65 72 79 20 70 61 72 73 65 20 63 6f 6e   query parse con
43177 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43  text */.  int iC
43178 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ol,             
43179 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4317a 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 46    /* Value for F
4317b 74 73 33 50 68 72 61 73 65 2e 69 43 6f 6c 75 6d  ts3Phrase.iColum
4317c 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  n */.  const cha
4317d 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20 20  r *z, int n,    
4317e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4317f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a  * Input string *
43180 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a 70  /.  Fts3Expr **p
43181 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20  pExpr,          
43182 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
43183 55 54 3a 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  UT: expression *
43184 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e 73 75  /.  int *pnConsu
43185 6d 65 64 20 20 20 20 20 20 20 20 20 20 20 20 20  med             
43186 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
43187 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79  UT: Number of by
43188 74 65 73 20 63 6f 6e 73 75 6d 65 64 20 2a 2f 0a  tes consumed */.
43189 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  ){.  sqlite3_tok
4318a 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
4318b 65 72 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 6f  er = pParse->pTo
4318c 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74  kenizer;.  sqlit
4318d 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
4318e 75 6c 65 20 63 6f 6e 73 74 20 2a 70 4d 6f 64 75  ule const *pModu
4318f 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d  le = pTokenizer-
43190 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20  >pModule;.  int 
43191 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  rc;.  sqlite3_to
43192 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
43193 70 43 75 72 73 6f 72 3b 0a 20 20 46 74 73 33 45  pCursor;.  Fts3E
43194 78 70 72 20 2a 70 52 65 74 20 3d 20 30 3b 0a 20  xpr *pRet = 0;. 
43195 20 69 6e 74 20 6e 43 6f 6e 73 75 6d 65 64 20 3d   int nConsumed =
43196 20 30 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69   0;..  rc = sqli
43197 74 65 33 46 74 73 33 4f 70 65 6e 54 6f 6b 65 6e  te3Fts3OpenToken
43198 69 7a 65 72 28 70 54 6f 6b 65 6e 69 7a 65 72 2c  izer(pTokenizer,
43199 20 70 50 61 72 73 65 2d 3e 69 4c 61 6e 67 69 64   pParse->iLangid
4319a 2c 20 7a 2c 20 6e 2c 20 26 70 43 75 72 73 6f 72  , z, n, &pCursor
4319b 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
4319c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 63 6f  ITE_OK ){.    co
4319d 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e  nst char *zToken
4319e 3b 0a 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e  ;.    int nToken
4319f 20 3d 20 30 2c 20 69 53 74 61 72 74 20 3d 20 30   = 0, iStart = 0
431a0 2c 20 69 45 6e 64 20 3d 20 30 2c 20 69 50 6f 73  , iEnd = 0, iPos
431a1 69 74 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 69  ition = 0;.    i
431a2 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20  nt nByte;       
431a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
431a4 20 20 20 20 20 20 20 20 2f 2a 20 74 6f 74 61 6c          /* total
431a5 20 73 70 61 63 65 20 74 6f 20 61 6c 6c 6f 63 61   space to alloca
431a6 74 65 20 2a 2f 0a 0a 20 20 20 20 72 63 20 3d 20  te */..    rc = 
431a7 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70  pModule->xNext(p
431a8 43 75 72 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c  Cursor, &zToken,
431a9 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 53 74 61 72   &nToken, &iStar
431aa 74 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 69  t, &iEnd, &iPosi
431ab 74 69 6f 6e 29 3b 0a 20 20 20 20 69 66 28 20 72  tion);.    if( r
431ac 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
431ad 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 73 69        nByte = si
431ae 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 20 2b  zeof(Fts3Expr) +
431af 20 73 69 7a 65 6f 66 28 46 74 73 33 50 68 72 61   sizeof(Fts3Phra
431b0 73 65 29 20 2b 20 6e 54 6f 6b 65 6e 3b 0a 20 20  se) + nToken;.  
431b1 20 20 20 20 70 52 65 74 20 3d 20 28 46 74 73 33      pRet = (Fts3
431b2 45 78 70 72 20 2a 29 66 74 73 33 4d 61 6c 6c 6f  Expr *)fts3Mallo
431b3 63 5a 65 72 6f 28 6e 42 79 74 65 29 3b 0a 20 20  cZero(nByte);.  
431b4 20 20 20 20 69 66 28 20 21 70 52 65 74 20 29 7b      if( !pRet ){
431b5 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
431b6 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
431b7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
431b8 20 70 52 65 74 2d 3e 65 54 79 70 65 20 3d 20 46   pRet->eType = F
431b9 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 3b 0a  TSQUERY_PHRASE;.
431ba 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50          pRet->pP
431bb 68 72 61 73 65 20 3d 20 28 46 74 73 33 50 68 72  hrase = (Fts3Phr
431bc 61 73 65 20 2a 29 26 70 52 65 74 5b 31 5d 3b 0a  ase *)&pRet[1];.
431bd 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50          pRet->pP
431be 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 20 3d 20  hrase->nToken = 
431bf 31 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d  1;.        pRet-
431c0 3e 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d  >pPhrase->iColum
431c1 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20  n = iCol;.      
431c2 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d    pRet->pPhrase-
431c3 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 6e 20 3d 20 6e  >aToken[0].n = n
431c4 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70  Token;.        p
431c5 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54  Ret->pPhrase->aT
431c6 6f 6b 65 6e 5b 30 5d 2e 7a 20 3d 20 28 63 68 61  oken[0].z = (cha
431c7 72 20 2a 29 26 70 52 65 74 2d 3e 70 50 68 72 61  r *)&pRet->pPhra
431c8 73 65 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 6d  se[1];.        m
431c9 65 6d 63 70 79 28 70 52 65 74 2d 3e 70 50 68 72  emcpy(pRet->pPhr
431ca 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 7a  ase->aToken[0].z
431cb 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e  , zToken, nToken
431cc 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20  );..        if( 
431cd 69 45 6e 64 3c 6e 20 26 26 20 7a 5b 69 45 6e 64  iEnd<n && z[iEnd
431ce 5d 3d 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20  ]=='*' ){.      
431cf 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73      pRet->pPhras
431d0 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 69 73 50  e->aToken[0].isP
431d1 72 65 66 69 78 20 3d 20 31 3b 0a 20 20 20 20 20  refix = 1;.     
431d2 20 20 20 20 20 69 45 6e 64 2b 2b 3b 0a 20 20 20       iEnd++;.   
431d3 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
431d4 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20  while( 1 ){.    
431d5 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74        if( !sqlit
431d6 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70  e3_fts3_enable_p
431d7 61 72 65 6e 74 68 65 73 65 73 20 0a 20 20 20 20  arentheses .    
431d8 20 20 20 20 20 20 20 26 26 20 69 53 74 61 72 74         && iStart
431d9 3e 30 20 26 26 20 7a 5b 69 53 74 61 72 74 2d 31  >0 && z[iStart-1
431da 5d 3d 3d 27 2d 27 20 0a 20 20 20 20 20 20 20 20  ]=='-' .        
431db 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20    ){.           
431dc 20 70 50 61 72 73 65 2d 3e 69 73 4e 6f 74 20 3d   pParse->isNot =
431dd 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
431de 69 53 74 61 72 74 2d 2d 3b 0a 20 20 20 20 20 20  iStart--;.      
431df 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50      }else if( pP
431e0 61 72 73 65 2d 3e 62 46 74 73 34 20 26 26 20 69  arse->bFts4 && i
431e1 53 74 61 72 74 3e 30 20 26 26 20 7a 5b 69 53 74  Start>0 && z[iSt
431e2 61 72 74 2d 31 5d 3d 3d 27 5e 27 20 29 7b 0a 20  art-1]=='^' ){. 
431e3 20 20 20 20 20 20 20 20 20 20 20 70 52 65 74 2d             pRet-
431e4 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e  >pPhrase->aToken
431e5 5b 30 5d 2e 62 46 69 72 73 74 20 3d 20 31 3b 0a  [0].bFirst = 1;.
431e6 20 20 20 20 20 20 20 20 20 20 20 20 69 53 74 61              iSta
431e7 72 74 2d 2d 3b 0a 20 20 20 20 20 20 20 20 20 20  rt--;.          
431e8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
431e9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
431ea 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
431eb 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e  .      }.      n
431ec 43 6f 6e 73 75 6d 65 64 20 3d 20 69 45 6e 64 3b  Consumed = iEnd;
431ed 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4d 6f 64  .    }..    pMod
431ee 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 75 72  ule->xClose(pCur
431ef 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 2a  sor);.  }.  .  *
431f0 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 6e 43 6f  pnConsumed = nCo
431f1 6e 73 75 6d 65 64 3b 0a 20 20 2a 70 70 45 78 70  nsumed;.  *ppExp
431f2 72 20 3d 20 70 52 65 74 3b 0a 20 20 72 65 74 75  r = pRet;.  retu
431f3 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
431f4 20 45 6e 6c 61 72 67 65 20 61 20 6d 65 6d 6f 72   Enlarge a memor
431f5 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 49  y allocation.  I
431f6 66 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f  f an out-of-memo
431f7 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63  ry allocation oc
431f8 63 75 72 73 2c 0a 2a 2a 20 74 68 65 6e 20 66 72  curs,.** then fr
431f9 65 65 20 74 68 65 20 6f 6c 64 20 61 6c 6c 6f 63  ee the old alloc
431fa 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ation..*/.static
431fb 20 76 6f 69 64 20 2a 66 74 73 33 52 65 61 6c 6c   void *fts3Reall
431fc 6f 63 4f 72 46 72 65 65 28 76 6f 69 64 20 2a 70  ocOrFree(void *p
431fd 4f 72 69 67 2c 20 69 6e 74 20 6e 4e 65 77 29 7b  Orig, int nNew){
431fe 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 20 3d 20  .  void *pRet = 
431ff 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
43200 70 4f 72 69 67 2c 20 6e 4e 65 77 29 3b 0a 20 20  pOrig, nNew);.  
43201 69 66 28 20 21 70 52 65 74 20 29 7b 0a 20 20 20  if( !pRet ){.   
43202 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f   sqlite3_free(pO
43203 72 69 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rig);.  }.  retu
43204 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pRet;.}../*.*
43205 2a 20 42 75 66 66 65 72 20 7a 49 6e 70 75 74 2c  * Buffer zInput,
43206 20 6c 65 6e 67 74 68 20 6e 49 6e 70 75 74 2c 20   length nInput, 
43207 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e  contains the con
43208 74 65 6e 74 73 20 6f 66 20 61 20 71 75 6f 74 65  tents of a quote
43209 64 20 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74  d string.** that
4320a 20 61 70 70 65 61 72 65 64 20 61 73 20 70 61 72   appeared as par
4320b 74 20 6f 66 20 61 6e 20 66 74 73 33 20 71 75 65  t of an fts3 que
4320c 72 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e  ry expression. N
4320d 65 69 74 68 65 72 20 71 75 6f 74 65 20 63 68 61  either quote cha
4320e 72 61 63 74 65 72 0a 2a 2a 20 69 73 20 69 6e 63  racter.** is inc
4320f 6c 75 64 65 64 20 69 6e 20 74 68 65 20 62 75 66  luded in the buf
43210 66 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69  fer. This functi
43211 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 74  on attempts to t
43212 6f 6b 65 6e 69 7a 65 20 74 68 65 20 65 6e 74 69  okenize the enti
43213 72 65 0a 2a 2a 20 69 6e 70 75 74 20 62 75 66 66  re.** input buff
43214 65 72 20 61 6e 64 20 63 72 65 61 74 65 20 61 6e  er and create an
43215 20 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74   Fts3Expr struct
43216 75 72 65 20 6f 66 20 74 79 70 65 20 46 54 53 51  ure of type FTSQ
43217 55 45 52 59 5f 50 48 52 41 53 45 20 0a 2a 2a 20  UERY_PHRASE .** 
43218 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72  containing the r
43219 65 73 75 6c 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  esults..**.** If
4321a 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c   successful, SQL
4321b 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
4321c 65 64 20 61 6e 64 20 2a 70 70 45 78 70 72 20 73  ed and *ppExpr s
4321d 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74  et to point at t
4321e 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  he.** allocated 
4321f 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74 75  Fts3Expr structu
43220 72 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 65  re. Otherwise, e
43221 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d  ither SQLITE_NOM
43222 45 4d 20 28 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  EM (out of memor
43223 79 0a 2a 2a 20 65 72 72 6f 72 29 20 6f 72 20 53  y.** error) or S
43224 51 4c 49 54 45 5f 45 52 52 4f 52 20 28 74 6f 6b  QLITE_ERROR (tok
43225 65 6e 69 7a 61 74 69 6f 6e 20 65 72 72 6f 72 29  enization error)
43226 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
43227 20 2a 70 70 45 78 70 72 20 73 65 74 0a 2a 2a 20   *ppExpr set.** 
43228 74 6f 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  to 0..*/.static 
43229 69 6e 74 20 67 65 74 4e 65 78 74 53 74 72 69 6e  int getNextStrin
4322a 67 28 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78  g(.  ParseContex
4322b 74 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  t *pParse,      
4322c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4322d 66 74 73 33 20 71 75 65 72 79 20 70 61 72 73 65  fts3 query parse
4322e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f   context */.  co
4322f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
43230 2c 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20  , int nInput,   
43231 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73        /* Input s
43232 74 72 69 6e 67 20 2a 2f 0a 20 20 46 74 73 33 45  tring */.  Fts3E
43233 78 70 72 20 2a 2a 70 70 45 78 70 72 20 20 20 20  xpr **ppExpr    
43234 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43235 20 20 20 2f 2a 20 4f 55 54 3a 20 65 78 70 72 65     /* OUT: expre
43236 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71  ssion */.){.  sq
43237 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
43238 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 50  *pTokenizer = pP
43239 61 72 73 65 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72  arse->pTokenizer
4323a 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ;.  sqlite3_toke
4323b 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e  nizer_module con
4323c 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 54  st *pModule = pT
4323d 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c  okenizer->pModul
4323e 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 46  e;.  int rc;.  F
4323f 74 73 33 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a  ts3Expr *p = 0;.
43240 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
43241 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
43242 73 6f 72 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  sor = 0;.  char 
43243 2a 7a 54 65 6d 70 20 3d 20 30 3b 0a 20 20 69 6e  *zTemp = 0;.  in
43244 74 20 6e 54 65 6d 70 20 3d 20 30 3b 0a 0a 20 20  t nTemp = 0;..  
43245 63 6f 6e 73 74 20 69 6e 74 20 6e 53 70 61 63 65  const int nSpace
43246 20 3d 20 73 69 7a 65 6f 66 28 46 74 73 33 45 78   = sizeof(Fts3Ex
43247 70 72 29 20 2b 20 73 69 7a 65 6f 66 28 46 74 73  pr) + sizeof(Fts
43248 33 50 68 72 61 73 65 29 3b 0a 20 20 69 6e 74 20  3Phrase);.  int 
43249 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0a 0a 20 20 2f  nToken = 0;..  /
4324a 2a 20 54 68 65 20 66 69 6e 61 6c 20 46 74 73 33  * The final Fts3
4324b 45 78 70 72 20 64 61 74 61 20 73 74 72 75 63 74  Expr data struct
4324c 75 72 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74  ure, including t
4324d 68 65 20 46 74 73 33 50 68 72 61 73 65 2c 0a 20  he Fts3Phrase,. 
4324e 20 2a 2a 20 46 74 73 33 50 68 72 61 73 65 54 6f   ** Fts3PhraseTo
4324f 6b 65 6e 20 73 74 72 75 63 74 75 72 65 73 20 74  ken structures t
43250 6f 6b 65 6e 20 62 75 66 66 65 72 73 20 61 72 65  oken buffers are
43251 20 61 6c 6c 20 73 74 6f 72 65 64 20 61 73 20 61   all stored as a
43252 20 73 69 6e 67 6c 65 20 0a 20 20 2a 2a 20 61 6c   single .  ** al
43253 6c 6f 63 61 74 69 6f 6e 20 73 6f 20 74 68 61 74  location so that
43254 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
43255 63 61 6e 20 62 65 20 66 72 65 65 64 20 77 69 74  can be freed wit
43256 68 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20  h a single call 
43257 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  to.  ** sqlite3_
43258 66 72 65 65 28 29 2e 20 53 65 74 74 69 6e 67 20  free(). Setting 
43259 74 68 69 73 20 75 70 20 72 65 71 75 69 72 65 73  this up requires
4325a 20 61 20 74 77 6f 20 70 61 73 73 20 61 70 70 72   a two pass appr
4325b 6f 61 63 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  oach..  **.  ** 
4325c 54 68 65 20 66 69 72 73 74 20 70 61 73 73 2c 20  The first pass, 
4325d 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20 62 65 6c  in the block bel
4325e 6f 77 2c 20 75 73 65 73 20 61 20 74 6f 6b 65 6e  ow, uses a token
4325f 69 7a 65 72 20 63 75 72 73 6f 72 20 74 6f 20 69  izer cursor to i
43260 74 65 72 61 74 65 0a 20 20 2a 2a 20 74 68 72 6f  terate.  ** thro
43261 75 67 68 20 74 68 65 20 74 6f 6b 65 6e 73 20 69  ugh the tokens i
43262 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
43263 2e 20 54 68 69 73 20 70 61 73 73 20 75 73 65 73  . This pass uses
43264 20 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46 72   fts3ReallocOrFr
43265 65 65 28 29 0a 20 20 2a 2a 20 74 6f 20 61 73 73  ee().  ** to ass
43266 65 6d 62 6c 65 20 64 61 74 61 20 69 6e 20 74 77  emble data in tw
43267 6f 20 64 79 6e 61 6d 69 63 20 62 75 66 66 65 72  o dynamic buffer
43268 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42  s:.  **.  **   B
43269 75 66 66 65 72 20 70 3a 20 50 6f 69 6e 74 73 20  uffer p: Points 
4326a 74 6f 20 74 68 65 20 46 74 73 33 45 78 70 72 20  to the Fts3Expr 
4326b 73 74 72 75 63 74 75 72 65 2c 20 66 6f 6c 6c 6f  structure, follo
4326c 77 65 64 20 62 79 20 74 68 65 20 46 74 73 33 50  wed by the Fts3P
4326d 68 72 61 73 65 0a 20 20 2a 2a 20 20 20 20 20 20  hrase.  **      
4326e 20 20 20 20 20 20 20 73 74 72 75 63 74 75 72 65         structure
4326f 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 68  , followed by th
43270 65 20 61 72 72 61 79 20 6f 66 20 46 74 73 33 50  e array of Fts3P
43271 68 72 61 73 65 54 6f 6b 65 6e 20 0a 20 20 2a 2a  hraseToken .  **
43272 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 72               str
43273 75 63 74 75 72 65 73 2e 20 54 68 69 73 20 70 61  uctures. This pa
43274 73 73 20 6f 6e 6c 79 20 70 6f 70 75 6c 61 74 65  ss only populate
43275 73 20 74 68 65 20 46 74 73 33 50 68 72 61 73 65  s the Fts3Phrase
43276 54 6f 6b 65 6e 20 61 72 72 61 79 2e 0a 20 20 2a  Token array..  *
43277 2a 0a 20 20 2a 2a 20 20 20 42 75 66 66 65 72 20  *.  **   Buffer 
43278 7a 54 65 6d 70 3a 20 43 6f 6e 74 61 69 6e 73 20  zTemp: Contains 
43279 63 6f 70 69 65 73 20 6f 66 20 61 6c 6c 20 74 6f  copies of all to
4327a 6b 65 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  kens..  **.  ** 
4327b 54 68 65 20 73 65 63 6f 6e 64 20 70 61 73 73 2c  The second pass,
4327c 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20 74 68   in the block th
4327d 61 74 20 62 65 67 69 6e 73 20 22 69 66 28 20 72  at begins "if( r
4327e 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c==SQLITE_DONE )
4327f 22 20 62 65 6c 6f 77 2c 0a 20 20 2a 2a 20 61 70  " below,.  ** ap
43280 70 65 6e 64 73 20 62 75 66 66 65 72 20 7a 54 65  pends buffer zTe
43281 6d 70 20 74 6f 20 62 75 66 66 65 72 20 70 2c 20  mp to buffer p, 
43282 61 6e 64 20 66 69 6c 6c 73 20 69 6e 20 74 68 65  and fills in the
43283 20 46 74 73 33 45 78 70 72 20 61 6e 64 20 46 74   Fts3Expr and Ft
43284 73 33 50 68 72 61 73 65 0a 20 20 2a 2a 20 73 74  s3Phrase.  ** st
43285 72 75 63 74 75 72 65 73 2e 0a 20 20 2a 2f 0a 20  ructures..  */. 
43286 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
43287 33 4f 70 65 6e 54 6f 6b 65 6e 69 7a 65 72 28 0a  3OpenTokenizer(.
43288 20 20 20 20 20 20 70 54 6f 6b 65 6e 69 7a 65 72        pTokenizer
43289 2c 20 70 50 61 72 73 65 2d 3e 69 4c 61 6e 67 69  , pParse->iLangi
4328a 64 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e 70 75  d, zInput, nInpu
4328b 74 2c 20 26 70 43 75 72 73 6f 72 29 3b 0a 20 20  t, &pCursor);.  
4328c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
4328d 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b  K ){.    int ii;
4328e 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 72  .    for(ii=0; r
4328f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 69  c==SQLITE_OK; ii
43290 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  ++){.      const
43291 20 63 68 61 72 20 2a 7a 42 79 74 65 3b 0a 20 20   char *zByte;.  
43292 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20      int nByte = 
43293 30 2c 20 69 42 65 67 69 6e 20 3d 20 30 2c 20 69  0, iBegin = 0, i
43294 45 6e 64 20 3d 20 30 2c 20 69 50 6f 73 20 3d 20  End = 0, iPos = 
43295 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 4d  0;.      rc = pM
43296 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75  odule->xNext(pCu
43297 72 73 6f 72 2c 20 26 7a 42 79 74 65 2c 20 26 6e  rsor, &zByte, &n
43298 42 79 74 65 2c 20 26 69 42 65 67 69 6e 2c 20 26  Byte, &iBegin, &
43299 69 45 6e 64 2c 20 26 69 50 6f 73 29 3b 0a 20 20  iEnd, &iPos);.  
4329a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
4329b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
4329c 20 46 74 73 33 50 68 72 61 73 65 54 6f 6b 65 6e   Fts3PhraseToken
4329d 20 2a 70 54 6f 6b 65 6e 3b 0a 0a 20 20 20 20 20   *pToken;..     
4329e 20 20 20 70 20 3d 20 66 74 73 33 52 65 61 6c 6c     p = fts3Reall
4329f 6f 63 4f 72 46 72 65 65 28 70 2c 20 6e 53 70 61  ocOrFree(p, nSpa
432a0 63 65 20 2b 20 69 69 2a 73 69 7a 65 6f 66 28 46  ce + ii*sizeof(F
432a1 74 73 33 50 68 72 61 73 65 54 6f 6b 65 6e 29 29  ts3PhraseToken))
432a2 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70  ;.        if( !p
432a3 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
432a4 0a 20 20 20 20 20 20 20 20 7a 54 65 6d 70 20 3d  .        zTemp =
432a5 20 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46 72   fts3ReallocOrFr
432a6 65 65 28 7a 54 65 6d 70 2c 20 6e 54 65 6d 70 20  ee(zTemp, nTemp 
432a7 2b 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20  + nByte);.      
432a8 20 20 69 66 28 20 21 7a 54 65 6d 70 20 29 20 67    if( !zTemp ) g
432a9 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 0a 20 20 20  oto no_mem;..   
432aa 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 54 6f       assert( nTo
432ab 6b 65 6e 3d 3d 69 69 20 29 3b 0a 20 20 20 20 20  ken==ii );.     
432ac 20 20 20 70 54 6f 6b 65 6e 20 3d 20 26 28 28 46     pToken = &((F
432ad 74 73 33 50 68 72 61 73 65 20 2a 29 28 26 70 5b  ts3Phrase *)(&p[
432ae 31 5d 29 29 2d 3e 61 54 6f 6b 65 6e 5b 69 69 5d  1]))->aToken[ii]
432af 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74  ;.        memset
432b0 28 70 54 6f 6b 65 6e 2c 20 30 2c 20 73 69 7a 65  (pToken, 0, size
432b1 6f 66 28 46 74 73 33 50 68 72 61 73 65 54 6f 6b  of(Fts3PhraseTok
432b2 65 6e 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 6d  en));..        m
432b3 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b 6e 54 65  emcpy(&zTemp[nTe
432b4 6d 70 5d 2c 20 7a 42 79 74 65 2c 20 6e 42 79 74  mp], zByte, nByt
432b5 65 29 3b 0a 20 20 20 20 20 20 20 20 6e 54 65 6d  e);.        nTem
432b6 70 20 2b 3d 20 6e 42 79 74 65 3b 0a 0a 20 20 20  p += nByte;..   
432b7 20 20 20 20 20 70 54 6f 6b 65 6e 2d 3e 6e 20 3d       pToken->n =
432b8 20 6e 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20   nByte;.        
432b9 70 54 6f 6b 65 6e 2d 3e 69 73 50 72 65 66 69 78  pToken->isPrefix
432ba 20 3d 20 28 69 45 6e 64 3c 6e 49 6e 70 75 74 20   = (iEnd<nInput 
432bb 26 26 20 7a 49 6e 70 75 74 5b 69 45 6e 64 5d 3d  && zInput[iEnd]=
432bc 3d 27 2a 27 29 3b 0a 20 20 20 20 20 20 20 20 70  ='*');.        p
432bd 54 6f 6b 65 6e 2d 3e 62 46 69 72 73 74 20 3d 20  Token->bFirst = 
432be 28 69 42 65 67 69 6e 3e 30 20 26 26 20 7a 49 6e  (iBegin>0 && zIn
432bf 70 75 74 5b 69 42 65 67 69 6e 2d 31 5d 3d 3d 27  put[iBegin-1]=='
432c0 5e 27 29 3b 0a 20 20 20 20 20 20 20 20 6e 54 6f  ^');.        nTo
432c1 6b 65 6e 20 3d 20 69 69 2b 31 3b 0a 20 20 20 20  ken = ii+1;.    
432c2 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70    }.    }..    p
432c3 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70  Module->xClose(p
432c4 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 70 43 75  Cursor);.    pCu
432c5 72 73 6f 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  rsor = 0;.  }.. 
432c6 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
432c7 44 4f 4e 45 20 29 7b 0a 20 20 20 20 69 6e 74 20  DONE ){.    int 
432c8 6a 6a 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 42  jj;.    char *zB
432c9 75 66 20 3d 20 30 3b 0a 0a 20 20 20 20 70 20 3d  uf = 0;..    p =
432ca 20 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46 72   fts3ReallocOrFr
432cb 65 65 28 70 2c 20 6e 53 70 61 63 65 20 2b 20 6e  ee(p, nSpace + n
432cc 54 6f 6b 65 6e 2a 73 69 7a 65 6f 66 28 46 74 73  Token*sizeof(Fts
432cd 33 50 68 72 61 73 65 54 6f 6b 65 6e 29 20 2b 20  3PhraseToken) + 
432ce 6e 54 65 6d 70 29 3b 0a 20 20 20 20 69 66 28 20  nTemp);.    if( 
432cf 21 70 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  !p ) goto no_mem
432d0 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20  ;.    memset(p, 
432d1 30 2c 20 28 63 68 61 72 20 2a 29 26 28 28 28 46  0, (char *)&(((F
432d2 74 73 33 50 68 72 61 73 65 20 2a 29 26 70 5b 31  ts3Phrase *)&p[1
432d3 5d 29 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 29 2d 28  ])->aToken[0])-(
432d4 63 68 61 72 20 2a 29 70 29 3b 0a 20 20 20 20 70  char *)p);.    p
432d5 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55 45  ->eType = FTSQUE
432d6 52 59 5f 50 48 52 41 53 45 3b 0a 20 20 20 20 70  RY_PHRASE;.    p
432d7 2d 3e 70 50 68 72 61 73 65 20 3d 20 28 46 74 73  ->pPhrase = (Fts
432d8 33 50 68 72 61 73 65 20 2a 29 26 70 5b 31 5d 3b  3Phrase *)&p[1];
432d9 0a 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d  .    p->pPhrase-
432da 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70 50 61 72 73  >iColumn = pPars
432db 65 2d 3e 69 44 65 66 61 75 6c 74 43 6f 6c 3b 0a  e->iDefaultCol;.
432dc 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e      p->pPhrase->
432dd 6e 54 6f 6b 65 6e 20 3d 20 6e 54 6f 6b 65 6e 3b  nToken = nToken;
432de 0a 0a 20 20 20 20 7a 42 75 66 20 3d 20 28 63 68  ..    zBuf = (ch
432df 61 72 20 2a 29 26 70 2d 3e 70 50 68 72 61 73 65  ar *)&p->pPhrase
432e0 2d 3e 61 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d  ->aToken[nToken]
432e1 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 20  ;.    if( zTemp 
432e2 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
432e3 7a 42 75 66 2c 20 7a 54 65 6d 70 2c 20 6e 54 65  zBuf, zTemp, nTe
432e4 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  mp);.      sqlit
432e5 65 33 5f 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a  e3_free(zTemp);.
432e6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
432e7 20 61 73 73 65 72 74 28 20 6e 54 65 6d 70 3d 3d   assert( nTemp==
432e8 30 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  0 );.    }..    
432e9 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 70 2d 3e  for(jj=0; jj<p->
432ea 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b  pPhrase->nToken;
432eb 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d   jj++){.      p-
432ec 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e  >pPhrase->aToken
432ed 5b 6a 6a 5d 2e 7a 20 3d 20 7a 42 75 66 3b 0a 20  [jj].z = zBuf;. 
432ee 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 70 2d 3e       zBuf += p->
432ef 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
432f0 6a 6a 5d 2e 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  jj].n;.    }.   
432f1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
432f2 0a 20 20 7d 0a 0a 20 20 2a 70 70 45 78 70 72 20  .  }..  *ppExpr 
432f3 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  = p;.  return rc
432f4 3b 0a 6e 6f 5f 6d 65 6d 3a 0a 0a 20 20 69 66 28  ;.no_mem:..  if(
432f5 20 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20   pCursor ){.    
432f6 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28  pModule->xClose(
432f7 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20  pCursor);.  }.  
432f8 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65  sqlite3_free(zTe
432f9 6d 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  mp);.  sqlite3_f
432fa 72 65 65 28 70 29 3b 0a 20 20 2a 70 70 45 78 70  ree(p);.  *ppExp
432fb 72 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  r = 0;.  return 
432fc 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a  SQLITE_NOMEM;.}.
432fd 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20  ./*.** Function 
432fe 67 65 74 4e 65 78 74 4e 6f 64 65 28 29 2c 20 77  getNextNode(), w
432ff 68 69 63 68 20 69 73 20 63 61 6c 6c 65 64 20 62  hich is called b
43300 79 20 66 74 73 33 45 78 70 72 50 61 72 73 65 28  y fts3ExprParse(
43301 29 2c 20 6d 61 79 20 69 74 73 65 6c 66 0a 2a 2a  ), may itself.**
43302 20 63 61 6c 6c 20 66 74 73 33 45 78 70 72 50 61   call fts3ExprPa
43303 72 73 65 28 29 2e 20 53 6f 20 74 68 69 73 20 66  rse(). So this f
43304 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
43305 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a  on is required..
43306 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
43307 73 33 45 78 70 72 50 61 72 73 65 28 50 61 72 73  s3ExprParse(Pars
43308 65 43 6f 6e 74 65 78 74 20 2a 2c 20 63 6f 6e 73  eContext *, cons
43309 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 46  t char *, int, F
4330a 74 73 33 45 78 70 72 20 2a 2a 2c 20 69 6e 74 20  ts3Expr **, int 
4330b 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f  *);../*.** The o
4330c 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 2a  utput variable *
4330d 70 70 45 78 70 72 20 69 73 20 70 6f 70 75 6c 61  ppExpr is popula
4330e 74 65 64 20 77 69 74 68 20 61 6e 20 61 6c 6c 6f  ted with an allo
4330f 63 61 74 65 64 20 46 74 73 33 45 78 70 72 20 0a  cated Fts3Expr .
43310 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 6f 72  ** structure, or
43311 20 73 65 74 20 74 6f 20 30 20 69 66 20 74 68 65   set to 0 if the
43312 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 70 75   end of the inpu
43313 74 20 62 75 66 66 65 72 20 69 73 20 72 65 61 63  t buffer is reac
43314 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  hed..**.** Retur
43315 6e 73 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  ns an SQLite err
43316 6f 72 20 63 6f 64 65 2e 20 53 51 4c 49 54 45 5f  or code. SQLITE_
43317 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e 67  OK if everything
43318 20 77 6f 72 6b 73 2c 20 53 51 4c 49 54 45 5f 4e   works, SQLITE_N
43319 4f 4d 45 4d 0a 2a 2a 20 69 66 20 61 20 6d 61 6c  OMEM.** if a mal
4331a 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75  loc failure occu
4331b 72 73 2c 20 6f 72 20 53 51 4c 49 54 45 5f 45 52  rs, or SQLITE_ER
4331c 52 4f 52 20 69 66 20 61 20 70 61 72 73 65 20 65  ROR if a parse e
4331d 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65  rror is encounte
4331e 72 65 64 2e 0a 2a 2a 20 49 66 20 53 51 4c 49 54  red..** If SQLIT
4331f 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 72  E_ERROR is retur
43320 6e 65 64 2c 20 70 43 6f 6e 74 65 78 74 20 69 73  ned, pContext is
43321 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20   populated with 
43322 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
43323 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
43324 67 65 74 4e 65 78 74 4e 6f 64 65 28 0a 20 20 50  getNextNode(.  P
43325 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61  arseContext *pPa
43326 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
43327 20 20 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71         /* fts3 q
43328 75 65 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65  uery parse conte
43329 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  xt */.  const ch
4332a 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20  ar *z, int n,   
4332b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4332c 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20  /* Input string 
4332d 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a  */.  Fts3Expr **
4332e 70 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20  ppExpr,         
4332f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
43330 4f 55 54 3a 20 65 78 70 72 65 73 73 69 6f 6e 20  OUT: expression 
43331 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e 73  */.  int *pnCons
43332 75 6d 65 64 20 20 20 20 20 20 20 20 20 20 20 20  umed            
43333 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
43334 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62  OUT: Number of b
43335 79 74 65 73 20 63 6f 6e 73 75 6d 65 64 20 2a 2f  ytes consumed */
43336 0a 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  .){.  static con
43337 73 74 20 73 74 72 75 63 74 20 46 74 73 33 4b 65  st struct Fts3Ke
43338 79 77 6f 72 64 20 7b 0a 20 20 20 20 63 68 61 72  yword {.    char
43339 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20   *z;            
4333a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4333b 20 20 2f 2a 20 4b 65 79 77 6f 72 64 20 74 65 78    /* Keyword tex
4333c 74 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65  t */.    unsigne
4333d 64 20 63 68 61 72 20 6e 3b 20 20 20 20 20 20 20  d char n;       
4333e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4333f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  * Length of the 
43340 6b 65 79 77 6f 72 64 20 2a 2f 0a 20 20 20 20 75  keyword */.    u
43341 6e 73 69 67 6e 65 64 20 63 68 61 72 20 70 61 72  nsigned char par
43342 65 6e 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20  enOnly;         
43343 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 76 61 6c       /* Only val
43344 69 64 20 69 6e 20 70 61 72 65 6e 20 6d 6f 64 65  id in paren mode
43345 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64   */.    unsigned
43346 20 63 68 61 72 20 65 54 79 70 65 3b 20 20 20 20   char eType;    
43347 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
43348 20 4b 65 79 77 6f 72 64 20 63 6f 64 65 20 2a 2f   Keyword code */
43349 0a 20 20 7d 20 61 4b 65 79 77 6f 72 64 5b 5d 20  .  } aKeyword[] 
4334a 3d 20 7b 0a 20 20 20 20 7b 20 22 4f 52 22 20 2c  = {.    { "OR" ,
4334b 20 20 32 2c 20 30 2c 20 46 54 53 51 55 45 52 59    2, 0, FTSQUERY
4334c 5f 4f 52 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22  _OR   },.    { "
4334d 41 4e 44 22 2c 20 20 33 2c 20 31 2c 20 46 54 53  AND",  3, 1, FTS
4334e 51 55 45 52 59 5f 41 4e 44 20 20 7d 2c 0a 20 20  QUERY_AND  },.  
4334f 20 20 7b 20 22 4e 4f 54 22 2c 20 20 33 2c 20 31    { "NOT",  3, 1
43350 2c 20 46 54 53 51 55 45 52 59 5f 4e 4f 54 20 20  , FTSQUERY_NOT  
43351 7d 2c 0a 20 20 20 20 7b 20 22 4e 45 41 52 22 2c  },.    { "NEAR",
43352 20 34 2c 20 30 2c 20 46 54 53 51 55 45 52 59 5f   4, 0, FTSQUERY_
43353 4e 45 41 52 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e  NEAR }.  };.  in
43354 74 20 69 69 3b 0a 20 20 69 6e 74 20 69 43 6f 6c  t ii;.  int iCol
43355 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 4c 65 6e 3b  ;.  int iColLen;
43356 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 46 74 73  .  int rc;.  Fts
43357 33 45 78 70 72 20 2a 70 52 65 74 20 3d 20 30 3b  3Expr *pRet = 0;
43358 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
43359 7a 49 6e 70 75 74 20 3d 20 7a 3b 0a 20 20 69 6e  zInput = z;.  in
4335a 74 20 6e 49 6e 70 75 74 20 3d 20 6e 3b 0a 0a 20  t nInput = n;.. 
4335b 20 70 50 61 72 73 65 2d 3e 69 73 4e 6f 74 20 3d   pParse->isNot =
4335c 20 30 3b 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 6f   0;..  /* Skip o
4335d 76 65 72 20 61 6e 79 20 77 68 69 74 65 73 70 61  ver any whitespa
4335e 63 65 20 62 65 66 6f 72 65 20 63 68 65 63 6b 69  ce before checki
4335f 6e 67 20 66 6f 72 20 61 20 6b 65 79 77 6f 72 64  ng for a keyword
43360 2c 20 61 6e 20 6f 70 65 6e 20 6f 72 0a 20 20 2a  , an open or.  *
43361 2a 20 63 6c 6f 73 65 20 62 72 61 63 6b 65 74 2c  * close bracket,
43362 20 6f 72 20 61 20 71 75 6f 74 65 64 20 73 74 72   or a quoted str
43363 69 6e 67 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69  ing. .  */.  whi
43364 6c 65 28 20 6e 49 6e 70 75 74 3e 30 20 26 26 20  le( nInput>0 && 
43365 66 74 73 33 69 73 73 70 61 63 65 28 2a 7a 49 6e  fts3isspace(*zIn
43366 70 75 74 29 20 29 7b 0a 20 20 20 20 6e 49 6e 70  put) ){.    nInp
43367 75 74 2d 2d 3b 0a 20 20 20 20 7a 49 6e 70 75 74  ut--;.    zInput
43368 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 49  ++;.  }.  if( nI
43369 6e 70 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72  nput==0 ){.    r
4336a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
4336b 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65  E;.  }..  /* See
4336c 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69   if we are deali
4336d 6e 67 20 77 69 74 68 20 61 20 6b 65 79 77 6f 72  ng with a keywor
4336e 64 2e 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30  d. */.  for(ii=0
4336f 3b 20 69 69 3c 28 69 6e 74 29 28 73 69 7a 65 6f  ; ii<(int)(sizeo
43370 66 28 61 4b 65 79 77 6f 72 64 29 2f 73 69 7a 65  f(aKeyword)/size
43371 6f 66 28 73 74 72 75 63 74 20 46 74 73 33 4b 65  of(struct Fts3Ke
43372 79 77 6f 72 64 29 29 3b 20 69 69 2b 2b 29 7b 0a  yword)); ii++){.
43373 20 20 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74      const struct
43374 20 46 74 73 33 4b 65 79 77 6f 72 64 20 2a 70 4b   Fts3Keyword *pK
43375 65 79 20 3d 20 26 61 4b 65 79 77 6f 72 64 5b 69  ey = &aKeyword[i
43376 69 5d 3b 0a 0a 20 20 20 20 69 66 28 20 28 70 4b  i];..    if( (pK
43377 65 79 2d 3e 70 61 72 65 6e 4f 6e 6c 79 20 26 20  ey->parenOnly & 
43378 7e 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e  ~sqlite3_fts3_en
43379 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73  able_parentheses
4337a 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f  )!=0 ){.      co
4337b 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20  ntinue;.    }.. 
4337c 20 20 20 69 66 28 20 6e 49 6e 70 75 74 3e 3d 70     if( nInput>=p
4337d 4b 65 79 2d 3e 6e 20 26 26 20 30 3d 3d 6d 65 6d  Key->n && 0==mem
4337e 63 6d 70 28 7a 49 6e 70 75 74 2c 20 70 4b 65 79  cmp(zInput, pKey
4337f 2d 3e 7a 2c 20 70 4b 65 79 2d 3e 6e 29 20 29 7b  ->z, pKey->n) ){
43380 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e 65 61 72  .      int nNear
43381 20 3d 20 53 51 4c 49 54 45 5f 46 54 53 33 5f 44   = SQLITE_FTS3_D
43382 45 46 41 55 4c 54 5f 4e 45 41 52 5f 50 41 52 41  EFAULT_NEAR_PARA
43383 4d 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b 65  M;.      int nKe
43384 79 20 3d 20 70 4b 65 79 2d 3e 6e 3b 0a 20 20 20  y = pKey->n;.   
43385 20 20 20 63 68 61 72 20 63 4e 65 78 74 3b 0a 0a     char cNext;..
43386 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73        /* If this
43387 20 69 73 20 61 20 22 4e 45 41 52 22 20 6b 65 79   is a "NEAR" key
43388 77 6f 72 64 2c 20 63 68 65 63 6b 20 66 6f 72 20  word, check for 
43389 61 6e 20 65 78 70 6c 69 63 69 74 20 6e 65 61 72  an explicit near
4338a 6e 65 73 73 2e 20 2a 2f 0a 20 20 20 20 20 20 69  ness. */.      i
4338b 66 28 20 70 4b 65 79 2d 3e 65 54 79 70 65 3d 3d  f( pKey->eType==
4338c 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b  FTSQUERY_NEAR ){
4338d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
4338e 20 6e 4b 65 79 3d 3d 34 20 29 3b 0a 20 20 20 20   nKey==4 );.    
4338f 20 20 20 20 69 66 28 20 7a 49 6e 70 75 74 5b 34      if( zInput[4
43390 5d 3d 3d 27 2f 27 20 26 26 20 7a 49 6e 70 75 74  ]=='/' && zInput
43391 5b 35 5d 3e 3d 27 30 27 20 26 26 20 7a 49 6e 70  [5]>='0' && zInp
43392 75 74 5b 35 5d 3c 3d 27 39 27 20 29 7b 0a 20 20  ut[5]<='9' ){.  
43393 20 20 20 20 20 20 20 20 6e 4e 65 61 72 20 3d 20          nNear = 
43394 30 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  0;.          for
43395 28 6e 4b 65 79 3d 35 3b 20 7a 49 6e 70 75 74 5b  (nKey=5; zInput[
43396 6e 4b 65 79 5d 3e 3d 27 30 27 20 26 26 20 7a 49  nKey]>='0' && zI
43397 6e 70 75 74 5b 6e 4b 65 79 5d 3c 3d 27 39 27 3b  nput[nKey]<='9';
43398 20 6e 4b 65 79 2b 2b 29 7b 0a 20 20 20 20 20 20   nKey++){.      
43399 20 20 20 20 20 20 6e 4e 65 61 72 20 3d 20 6e 4e        nNear = nN
4339a 65 61 72 20 2a 20 31 30 20 2b 20 28 7a 49 6e 70  ear * 10 + (zInp
4339b 75 74 5b 6e 4b 65 79 5d 20 2d 20 27 30 27 29 3b  ut[nKey] - '0');
4339c 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
4339d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
4339e 20 20 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73        /* At this
4339f 20 70 6f 69 6e 74 20 74 68 69 73 20 69 73 20 70   point this is p
433a0 72 6f 62 61 62 6c 79 20 61 20 6b 65 79 77 6f 72  robably a keywor
433a1 64 2e 20 42 75 74 20 66 6f 72 20 74 68 61 74 20  d. But for that 
433a2 74 6f 20 62 65 20 74 72 75 65 2c 0a 20 20 20 20  to be true,.    
433a3 20 20 2a 2a 20 74 68 65 20 6e 65 78 74 20 62 79    ** the next by
433a4 74 65 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20  te must contain 
433a5 65 69 74 68 65 72 20 77 68 69 74 65 73 70 61 63  either whitespac
433a6 65 2c 20 61 6e 20 6f 70 65 6e 20 6f 72 20 63 6c  e, an open or cl
433a7 6f 73 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 72  ose.      ** par
433a8 65 6e 74 68 65 73 69 73 2c 20 61 20 71 75 6f 74  enthesis, a quot
433a9 65 20 63 68 61 72 61 63 74 65 72 2c 20 6f 72 20  e character, or 
433aa 45 4f 46 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20  EOF. .      */. 
433ab 20 20 20 20 20 63 4e 65 78 74 20 3d 20 7a 49 6e       cNext = zIn
433ac 70 75 74 5b 6e 4b 65 79 5d 3b 0a 20 20 20 20 20  put[nKey];.     
433ad 20 69 66 28 20 66 74 73 33 69 73 73 70 61 63 65   if( fts3isspace
433ae 28 63 4e 65 78 74 29 20 0a 20 20 20 20 20 20 20  (cNext) .       
433af 7c 7c 20 63 4e 65 78 74 3d 3d 27 22 27 20 7c 7c  || cNext=='"' ||
433b0 20 63 4e 65 78 74 3d 3d 27 28 27 20 7c 7c 20 63   cNext=='(' || c
433b1 4e 65 78 74 3d 3d 27 29 27 20 7c 7c 20 63 4e 65  Next==')' || cNe
433b2 78 74 3d 3d 30 0a 20 20 20 20 20 20 29 7b 0a 20  xt==0.      ){. 
433b3 20 20 20 20 20 20 20 70 52 65 74 20 3d 20 28 46         pRet = (F
433b4 74 73 33 45 78 70 72 20 2a 29 66 74 73 33 4d 61  ts3Expr *)fts3Ma
433b5 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28  llocZero(sizeof(
433b6 46 74 73 33 45 78 70 72 29 29 3b 0a 20 20 20 20  Fts3Expr));.    
433b7 20 20 20 20 69 66 28 20 21 70 52 65 74 20 29 7b      if( !pRet ){
433b8 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
433b9 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
433ba 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
433bb 20 20 70 52 65 74 2d 3e 65 54 79 70 65 20 3d 20    pRet->eType = 
433bc 70 4b 65 79 2d 3e 65 54 79 70 65 3b 0a 20 20 20  pKey->eType;.   
433bd 20 20 20 20 20 70 52 65 74 2d 3e 6e 4e 65 61 72       pRet->nNear
433be 20 3d 20 6e 4e 65 61 72 3b 0a 20 20 20 20 20 20   = nNear;.      
433bf 20 20 2a 70 70 45 78 70 72 20 3d 20 70 52 65 74    *ppExpr = pRet
433c0 3b 0a 20 20 20 20 20 20 20 20 2a 70 6e 43 6f 6e  ;.        *pnCon
433c1 73 75 6d 65 64 20 3d 20 28 69 6e 74 29 28 28 7a  sumed = (int)((z
433c2 49 6e 70 75 74 20 2d 20 7a 29 20 2b 20 6e 4b 65  Input - z) + nKe
433c3 79 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  y);.        retu
433c4 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
433c5 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
433c6 54 75 72 6e 73 20 6f 75 74 20 74 68 61 74 20 77  Turns out that w
433c7 61 73 6e 27 74 20 61 20 6b 65 79 77 6f 72 64 20  asn't a keyword 
433c8 61 66 74 65 72 20 61 6c 6c 2e 20 54 68 69 73 20  after all. This 
433c9 68 61 70 70 65 6e 73 20 69 66 20 74 68 65 0a 20  happens if the. 
433ca 20 20 20 20 20 2a 2a 20 75 73 65 72 20 68 61 73       ** user has
433cb 20 73 75 70 70 6c 69 65 64 20 61 20 74 6f 6b 65   supplied a toke
433cc 6e 20 73 75 63 68 20 61 73 20 22 4f 52 61 63 6c  n such as "ORacl
433cd 65 22 2e 20 43 6f 6e 74 69 6e 75 65 2e 0a 20 20  e". Continue..  
433ce 20 20 20 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d      */.    }.  }
433cf 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72  ..  /* Check for
433d0 20 61 6e 20 6f 70 65 6e 20 62 72 61 63 6b 65 74   an open bracket
433d1 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74  . */.  if( sqlit
433d2 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70  e3_fts3_enable_p
433d3 61 72 65 6e 74 68 65 73 65 73 20 29 7b 0a 20 20  arentheses ){.  
433d4 20 20 69 66 28 20 2a 7a 49 6e 70 75 74 3d 3d 27    if( *zInput=='
433d5 28 27 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  (' ){.      int 
433d6 6e 43 6f 6e 73 75 6d 65 64 3b 0a 20 20 20 20 20  nConsumed;.     
433d7 20 70 50 61 72 73 65 2d 3e 6e 4e 65 73 74 2b 2b   pParse->nNest++
433d8 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73  ;.      rc = fts
433d9 33 45 78 70 72 50 61 72 73 65 28 70 50 61 72 73  3ExprParse(pPars
433da 65 2c 20 26 7a 49 6e 70 75 74 5b 31 5d 2c 20 6e  e, &zInput[1], n
433db 49 6e 70 75 74 2d 31 2c 20 70 70 45 78 70 72 2c  Input-1, ppExpr,
433dc 20 26 6e 43 6f 6e 73 75 6d 65 64 29 3b 0a 20 20   &nConsumed);.  
433dd 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
433de 54 45 5f 4f 4b 20 26 26 20 21 2a 70 70 45 78 70  TE_OK && !*ppExp
433df 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  r ){.        rc 
433e0 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  = SQLITE_DONE;. 
433e1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 6e       }.      *pn
433e2 43 6f 6e 73 75 6d 65 64 20 3d 20 28 69 6e 74 29  Consumed = (int)
433e3 28 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b 20  ((zInput - z) + 
433e4 31 20 2b 20 6e 43 6f 6e 73 75 6d 65 64 29 3b 0a  1 + nConsumed);.
433e5 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
433e6 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
433e7 20 43 68 65 63 6b 20 66 6f 72 20 61 20 63 6c 6f   Check for a clo
433e8 73 65 20 62 72 61 63 6b 65 74 2e 20 2a 2f 0a 20  se bracket. */. 
433e9 20 20 20 69 66 28 20 2a 7a 49 6e 70 75 74 3d 3d     if( *zInput==
433ea 27 29 27 20 29 7b 0a 20 20 20 20 20 20 70 50 61  ')' ){.      pPa
433eb 72 73 65 2d 3e 6e 4e 65 73 74 2d 2d 3b 0a 20 20  rse->nNest--;.  
433ec 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20      *pnConsumed 
433ed 3d 20 28 69 6e 74 29 28 28 7a 49 6e 70 75 74 20  = (int)((zInput 
433ee 2d 20 7a 29 20 2b 20 31 29 3b 0a 20 20 20 20 20  - z) + 1);.     
433ef 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
433f0 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ONE;.    }.  }..
433f1 20 20 2f 2a 20 53 65 65 20 69 66 20 77 65 20 61    /* See if we a
433f2 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  re dealing with 
433f3 61 20 71 75 6f 74 65 64 20 70 68 72 61 73 65 2e  a quoted phrase.
433f4 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
433f5 63 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  case, then.  ** 
433f6 73 65 61 72 63 68 20 66 6f 72 20 74 68 65 20 63  search for the c
433f7 6c 6f 73 69 6e 67 20 71 75 6f 74 65 20 61 6e 64  losing quote and
433f8 20 70 61 73 73 20 74 68 65 20 77 68 6f 6c 65 20   pass the whole 
433f9 73 74 72 69 6e 67 20 74 6f 20 67 65 74 4e 65 78  string to getNex
433fa 74 53 74 72 69 6e 67 28 29 0a 20 20 2a 2a 20 66  tString().  ** f
433fb 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 54  or processing. T
433fc 68 69 73 20 69 73 20 65 61 73 79 20 74 6f 20 64  his is easy to d
433fd 6f 2c 20 61 73 20 66 74 73 33 20 68 61 73 20 6e  o, as fts3 has n
433fe 6f 20 73 79 6e 74 61 78 20 66 6f 72 20 65 73 63  o syntax for esc
433ff 61 70 69 6e 67 0a 20 20 2a 2a 20 61 20 71 75 6f  aping.  ** a quo
43400 74 65 20 63 68 61 72 61 63 74 65 72 20 65 6d 62  te character emb
43401 65 64 64 65 64 20 69 6e 20 61 20 73 74 72 69 6e  edded in a strin
43402 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 2a 7a  g..  */.  if( *z
43403 49 6e 70 75 74 3d 3d 27 22 27 20 29 7b 0a 20 20  Input=='"' ){.  
43404 20 20 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 6e    for(ii=1; ii<n
43405 49 6e 70 75 74 20 26 26 20 7a 49 6e 70 75 74 5b  Input && zInput[
43406 69 69 5d 21 3d 27 22 27 3b 20 69 69 2b 2b 29 3b  ii]!='"'; ii++);
43407 0a 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64  .    *pnConsumed
43408 20 3d 20 28 69 6e 74 29 28 28 7a 49 6e 70 75 74   = (int)((zInput
43409 20 2d 20 7a 29 20 2b 20 69 69 20 2b 20 31 29 3b   - z) + ii + 1);
4340a 0a 20 20 20 20 69 66 28 20 69 69 3d 3d 6e 49 6e  .    if( ii==nIn
4340b 70 75 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74  put ){.      ret
4340c 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
4340d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
4340e 72 6e 20 67 65 74 4e 65 78 74 53 74 72 69 6e 67  rn getNextString
4340f 28 70 50 61 72 73 65 2c 20 26 7a 49 6e 70 75 74  (pParse, &zInput
43410 5b 31 5d 2c 20 69 69 2d 31 2c 20 70 70 45 78 70  [1], ii-1, ppExp
43411 72 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 49  r);.  }...  /* I
43412 66 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77 73 20  f control flows 
43413 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74  to this point, t
43414 68 69 73 20 6d 75 73 74 20 62 65 20 61 20 72 65  his must be a re
43415 67 75 6c 61 72 20 74 6f 6b 65 6e 2c 20 6f 72 20  gular token, or 
43416 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66  .  ** the end of
43417 20 74 68 65 20 69 6e 70 75 74 2e 20 52 65 61 64   the input. Read
43418 20 61 20 72 65 67 75 6c 61 72 20 74 6f 6b 65 6e   a regular token
43419 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74   using the sqlit
4341a 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 20 20 2a  e3_tokenizer.  *
4341b 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 42 65 66  * interface. Bef
4341c 6f 72 65 20 64 6f 69 6e 67 20 73 6f 2c 20 66 69  ore doing so, fi
4341d 67 75 72 65 20 6f 75 74 20 69 66 20 74 68 65 72  gure out if ther
4341e 65 20 69 73 20 61 6e 20 65 78 70 6c 69 63 69 74  e is an explicit
4341f 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 73 70 65  .  ** column spe
43420 63 69 66 69 65 72 20 66 6f 72 20 74 68 65 20 74  cifier for the t
43421 6f 6b 65 6e 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a  oken. .  **.  **
43422 20 54 4f 44 4f 3a 20 53 74 72 61 6e 67 65 6c 79   TODO: Strangely
43423 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  , it is not poss
43424 69 62 6c 65 20 74 6f 20 61 73 73 6f 63 69 61 74  ible to associat
43425 65 20 61 20 63 6f 6c 75 6d 6e 20 73 70 65 63 69  e a column speci
43426 66 69 65 72 0a 20 20 2a 2a 20 77 69 74 68 20 61  fier.  ** with a
43427 20 71 75 6f 74 65 64 20 70 68 72 61 73 65 2c 20   quoted phrase, 
43428 6f 6e 6c 79 20 77 69 74 68 20 61 20 73 69 6e 67  only with a sing
43429 6c 65 20 74 6f 6b 65 6e 2e 20 4e 6f 74 20 73 75  le token. Not su
4342a 72 65 20 69 66 20 74 68 69 73 20 77 61 73 0a 20  re if this was. 
4342b 20 2a 2a 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74   ** an implement
4342c 61 74 69 6f 6e 20 61 72 74 69 66 61 63 74 20 6f  ation artifact o
4342d 72 20 61 6e 20 69 6e 74 65 6e 74 69 6f 6e 61 6c  r an intentional
4342e 20 64 65 63 69 73 69 6f 6e 20 77 68 65 6e 20 66   decision when f
4342f 74 73 33 20 77 61 73 0a 20 20 2a 2a 20 66 69 72  ts3 was.  ** fir
43430 73 74 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2e 20  st implemented. 
43431 57 68 69 63 68 65 76 65 72 20 69 74 20 77 61 73  Whichever it was
43432 2c 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 64 75  , this module du
43433 70 6c 69 63 61 74 65 73 20 74 68 65 20 0a 20 20  plicates the .  
43434 2a 2a 20 6c 69 6d 69 74 61 74 69 6f 6e 2e 0a 20  ** limitation.. 
43435 20 2a 2f 0a 20 20 69 43 6f 6c 20 3d 20 70 50 61   */.  iCol = pPa
43436 72 73 65 2d 3e 69 44 65 66 61 75 6c 74 43 6f 6c  rse->iDefaultCol
43437 3b 0a 20 20 69 43 6f 6c 4c 65 6e 20 3d 20 30 3b  ;.  iColLen = 0;
43438 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c  .  for(ii=0; ii<
43439 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 3b 20 69 69  pParse->nCol; ii
4343a 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ++){.    const c
4343b 68 61 72 20 2a 7a 53 74 72 20 3d 20 70 50 61 72  har *zStr = pPar
4343c 73 65 2d 3e 61 7a 43 6f 6c 5b 69 69 5d 3b 0a 20  se->azCol[ii];. 
4343d 20 20 20 69 6e 74 20 6e 53 74 72 20 3d 20 28 69     int nStr = (i
4343e 6e 74 29 73 74 72 6c 65 6e 28 7a 53 74 72 29 3b  nt)strlen(zStr);
4343f 0a 20 20 20 20 69 66 28 20 6e 49 6e 70 75 74 3e  .    if( nInput>
43440 6e 53 74 72 20 26 26 20 7a 49 6e 70 75 74 5b 6e  nStr && zInput[n
43441 53 74 72 5d 3d 3d 27 3a 27 20 0a 20 20 20 20 20  Str]==':' .     
43442 26 26 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69  && sqlite3_strni
43443 63 6d 70 28 7a 53 74 72 2c 20 7a 49 6e 70 75 74  cmp(zStr, zInput
43444 2c 20 6e 53 74 72 29 3d 3d 30 20 0a 20 20 20 20  , nStr)==0 .    
43445 29 7b 0a 20 20 20 20 20 20 69 43 6f 6c 20 3d 20  ){.      iCol = 
43446 69 69 3b 0a 20 20 20 20 20 20 69 43 6f 6c 4c 65  ii;.      iColLe
43447 6e 20 3d 20 28 69 6e 74 29 28 28 7a 49 6e 70 75  n = (int)((zInpu
43448 74 20 2d 20 7a 29 20 2b 20 6e 53 74 72 20 2b 20  t - z) + nStr + 
43449 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  1);.      break;
4344a 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 63 20  .    }.  }.  rc 
4344b 3d 20 67 65 74 4e 65 78 74 54 6f 6b 65 6e 28 70  = getNextToken(p
4344c 50 61 72 73 65 2c 20 69 43 6f 6c 2c 20 26 7a 5b  Parse, iCol, &z[
4344d 69 43 6f 6c 4c 65 6e 5d 2c 20 6e 2d 69 43 6f 6c  iColLen], n-iCol
4344e 4c 65 6e 2c 20 70 70 45 78 70 72 2c 20 70 6e 43  Len, ppExpr, pnC
4344f 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 2a 70 6e 43  onsumed);.  *pnC
43450 6f 6e 73 75 6d 65 64 20 2b 3d 20 69 43 6f 6c 4c  onsumed += iColL
43451 65 6e 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  en;.  return rc;
43452 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72  .}../*.** The ar
43453 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 46 74 73  gument is an Fts
43454 33 45 78 70 72 20 73 74 72 75 63 74 75 72 65 20  3Expr structure 
43455 66 6f 72 20 61 20 62 69 6e 61 72 79 20 6f 70 65  for a binary ope
43456 72 61 74 6f 72 20 28 61 6e 79 20 74 79 70 65 0a  rator (any type.
43457 2a 2a 20 65 78 63 65 70 74 20 61 6e 20 46 54 53  ** except an FTS
43458 51 55 45 52 59 5f 50 48 52 41 53 45 29 2e 20 52  QUERY_PHRASE). R
43459 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72  eturn an integer
4345a 20 76 61 6c 75 65 20 72 65 70 72 65 73 65 6e 74   value represent
4345b 69 6e 67 20 74 68 65 0a 2a 2a 20 70 72 65 63 65  ing the.** prece
4345c 64 65 6e 63 65 20 6f 66 20 74 68 65 20 6f 70 65  dence of the ope
4345d 72 61 74 6f 72 2e 20 4c 6f 77 65 72 20 76 61 6c  rator. Lower val
4345e 75 65 73 20 68 61 76 65 20 61 20 68 69 67 68 65  ues have a highe
4345f 72 20 70 72 65 63 65 64 65 6e 63 65 20 28 69 2e  r precedence (i.
43460 65 2e 0a 2a 2a 20 67 72 6f 75 70 20 6d 6f 72 65  e..** group more
43461 20 74 69 67 68 74 6c 79 29 2e 20 46 6f 72 20 65   tightly). For e
43462 78 61 6d 70 6c 65 2c 20 69 6e 20 74 68 65 20 43  xample, in the C
43463 20 6c 61 6e 67 75 61 67 65 2c 20 74 68 65 20 3d   language, the =
43464 3d 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 67 72  = operator.** gr
43465 6f 75 70 73 20 6d 6f 72 65 20 74 69 67 68 74 6c  oups more tightl
43466 79 20 74 68 61 6e 20 7c 7c 2c 20 61 6e 64 20 77  y than ||, and w
43467 6f 75 6c 64 20 74 68 65 72 65 66 6f 72 65 20 68  ould therefore h
43468 61 76 65 20 61 20 68 69 67 68 65 72 20 70 72 65  ave a higher pre
43469 63 65 64 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 57  cedence..**.** W
4346a 68 65 6e 20 75 73 69 6e 67 20 74 68 65 20 6e 65  hen using the ne
4346b 77 20 66 74 73 33 20 71 75 65 72 79 20 73 79 6e  w fts3 query syn
4346c 74 61 78 20 28 77 68 65 6e 20 53 51 4c 49 54 45  tax (when SQLITE
4346d 5f 45 4e 41 42 4c 45 5f 46 54 53 33 5f 50 41 52  _ENABLE_FTS3_PAR
4346e 45 4e 54 48 45 53 49 53 0a 2a 2a 20 69 73 20 64  ENTHESIS.** is d
4346f 65 66 69 6e 65 64 29 2c 20 74 68 65 20 6f 72 64  efined), the ord
43470 65 72 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74  er of the operat
43471 6f 72 73 20 69 6e 20 70 72 65 63 65 64 65 6e 63  ors in precedenc
43472 65 20 66 72 6f 6d 20 68 69 67 68 65 73 74 20 74  e from highest t
43473 6f 0a 2a 2a 20 6c 6f 77 65 73 74 20 69 73 3a 0a  o.** lowest is:.
43474 2a 2a 0a 2a 2a 20 20 20 4e 45 41 52 0a 2a 2a 20  **.**   NEAR.** 
43475 20 20 4e 4f 54 0a 2a 2a 20 20 20 41 4e 44 20 28    NOT.**   AND (
43476 69 6e 63 6c 75 64 69 6e 67 20 69 6d 70 6c 69 63  including implic
43477 69 74 20 41 4e 44 73 29 0a 2a 2a 20 20 20 4f 52  it ANDs).**   OR
43478 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
43479 20 77 68 65 6e 20 75 73 69 6e 67 20 74 68 65 20   when using the 
4347a 6f 6c 64 20 71 75 65 72 79 20 73 79 6e 74 61 78  old query syntax
4347b 2c 20 74 68 65 20 4f 52 20 6f 70 65 72 61 74 6f  , the OR operato
4347c 72 20 68 61 73 20 61 20 68 69 67 68 65 72 0a 2a  r has a higher.*
4347d 2a 20 70 72 65 63 65 64 65 6e 63 65 20 74 68 61  * precedence tha
4347e 6e 20 74 68 65 20 41 4e 44 20 6f 70 65 72 61 74  n the AND operat
4347f 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  or..*/.static in
43480 74 20 6f 70 50 72 65 63 65 64 65 6e 63 65 28 46  t opPrecedence(F
43481 74 73 33 45 78 70 72 20 2a 70 29 7b 0a 20 20 61  ts3Expr *p){.  a
43482 73 73 65 72 74 28 20 70 2d 3e 65 54 79 70 65 21  ssert( p->eType!
43483 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45  =FTSQUERY_PHRASE
43484 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   );.  if( sqlite
43485 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61  3_fts3_enable_pa
43486 72 65 6e 74 68 65 73 65 73 20 29 7b 0a 20 20 20  rentheses ){.   
43487 20 72 65 74 75 72 6e 20 70 2d 3e 65 54 79 70 65   return p->eType
43488 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  ;.  }else if( p-
43489 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
4348a 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 72 65 74  _NEAR ){.    ret
4348b 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69  urn 1;.  }else i
4348c 66 28 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53  f( p->eType==FTS
4348d 51 55 45 52 59 5f 4f 52 20 29 7b 0a 20 20 20 20  QUERY_OR ){.    
4348e 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20  return 2;.  }.  
4348f 61 73 73 65 72 74 28 20 70 2d 3e 65 54 79 70 65  assert( p->eType
43490 3d 3d 46 54 53 51 55 45 52 59 5f 41 4e 44 20 29  ==FTSQUERY_AND )
43491 3b 0a 20 20 72 65 74 75 72 6e 20 33 3b 0a 7d 0a  ;.  return 3;.}.
43492 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20  ./*.** Argument 
43493 70 70 48 65 61 64 20 63 6f 6e 74 61 69 6e 73 20  ppHead contains 
43494 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
43495 20 63 75 72 72 65 6e 74 20 68 65 61 64 20 6f 66   current head of
43496 20 61 20 71 75 65 72 79 20 0a 2a 2a 20 65 78 70   a query .** exp
43497 72 65 73 73 69 6f 6e 20 74 72 65 65 20 62 65 69  ression tree bei
43498 6e 67 20 70 61 72 73 65 64 2e 20 70 50 72 65 76  ng parsed. pPrev
43499 20 69 73 20 74 68 65 20 65 78 70 72 65 73 73 69   is the expressi
4349a 6f 6e 20 6e 6f 64 65 20 6d 6f 73 74 20 72 65 63  on node most rec
4349b 65 6e 74 6c 79 0a 2a 2a 20 69 6e 73 65 72 74 65  ently.** inserte
4349c 64 20 69 6e 74 6f 20 74 68 65 20 74 72 65 65 2e  d into the tree.
4349d 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
4349e 64 64 73 20 70 4e 65 77 2c 20 77 68 69 63 68 20  dds pNew, which 
4349f 69 73 20 61 6c 77 61 79 73 20 61 20 62 69 6e 61  is always a bina
434a0 72 79 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 6e  ry.** operator n
434a1 6f 64 65 2c 20 69 6e 74 6f 20 74 68 65 20 65 78  ode, into the ex
434a2 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 62 61  pression tree ba
434a3 73 65 64 20 6f 6e 20 74 68 65 20 72 65 6c 61 74  sed on the relat
434a4 69 76 65 20 70 72 65 63 65 64 65 6e 63 65 0a 2a  ive precedence.*
434a5 2a 20 6f 66 20 70 4e 65 77 20 61 6e 64 20 74 68  * of pNew and th
434a6 65 20 65 78 69 73 74 69 6e 67 20 6e 6f 64 65 73  e existing nodes
434a7 20 6f 66 20 74 68 65 20 74 72 65 65 2e 20 54 68   of the tree. Th
434a8 69 73 20 6d 61 79 20 72 65 73 75 6c 74 20 69 6e  is may result in
434a9 20 74 68 65 20 68 65 61 64 0a 2a 2a 20 6f 66 20   the head.** of 
434aa 74 68 65 20 74 72 65 65 20 63 68 61 6e 67 69 6e  the tree changin
434ab 67 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65  g, in which case
434ac 20 2a 70 70 48 65 61 64 20 69 73 20 73 65 74 20   *ppHead is set 
434ad 74 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f 74 20  to the new root 
434ae 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  node..*/.static 
434af 76 6f 69 64 20 69 6e 73 65 72 74 42 69 6e 61 72  void insertBinar
434b0 79 4f 70 65 72 61 74 6f 72 28 0a 20 20 46 74 73  yOperator(.  Fts
434b1 33 45 78 70 72 20 2a 2a 70 70 48 65 61 64 2c 20  3Expr **ppHead, 
434b2 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
434b3 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64   to the root nod
434b4 65 20 6f 66 20 61 20 74 72 65 65 20 2a 2f 0a 20  e of a tree */. 
434b5 20 46 74 73 33 45 78 70 72 20 2a 70 50 72 65 76   Fts3Expr *pPrev
434b6 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 64  ,         /* Nod
434b7 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
434b8 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68  inserted into th
434b9 65 20 74 72 65 65 20 2a 2f 0a 20 20 46 74 73 33  e tree */.  Fts3
434ba 45 78 70 72 20 2a 70 4e 65 77 20 20 20 20 20 20  Expr *pNew      
434bb 20 20 20 20 20 2f 2a 20 4e 65 77 20 62 69 6e 61       /* New bina
434bc 72 79 20 6e 6f 64 65 20 74 6f 20 69 6e 73 65 72  ry node to inser
434bd 74 20 69 6e 74 6f 20 65 78 70 72 65 73 73 69 6f  t into expressio
434be 6e 20 74 72 65 65 20 2a 2f 0a 29 7b 0a 20 20 46  n tree */.){.  F
434bf 74 73 33 45 78 70 72 20 2a 70 53 70 6c 69 74 20  ts3Expr *pSplit 
434c0 3d 20 70 50 72 65 76 3b 0a 20 20 77 68 69 6c 65  = pPrev;.  while
434c1 28 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e  ( pSplit->pParen
434c2 74 20 26 26 20 6f 70 50 72 65 63 65 64 65 6e 63  t && opPrecedenc
434c3 65 28 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e  e(pSplit->pParen
434c4 74 29 3c 3d 6f 70 50 72 65 63 65 64 65 6e 63 65  t)<=opPrecedence
434c5 28 70 4e 65 77 29 20 29 7b 0a 20 20 20 20 70 53  (pNew) ){.    pS
434c6 70 6c 69 74 20 3d 20 70 53 70 6c 69 74 2d 3e 70  plit = pSplit->p
434c7 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 0a 20 20 69  Parent;.  }..  i
434c8 66 28 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65  f( pSplit->pPare
434c9 6e 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  nt ){.    assert
434ca 28 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e  ( pSplit->pParen
434cb 74 2d 3e 70 52 69 67 68 74 3d 3d 70 53 70 6c 69  t->pRight==pSpli
434cc 74 20 29 3b 0a 20 20 20 20 70 53 70 6c 69 74 2d  t );.    pSplit-
434cd 3e 70 50 61 72 65 6e 74 2d 3e 70 52 69 67 68 74  >pParent->pRight
434ce 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 4e 65   = pNew;.    pNe
434cf 77 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 53 70  w->pParent = pSp
434d0 6c 69 74 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  lit->pParent;.  
434d1 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 48 65  }else{.    *ppHe
434d2 61 64 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20  ad = pNew;.  }. 
434d3 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 70   pNew->pLeft = p
434d4 53 70 6c 69 74 3b 0a 20 20 70 53 70 6c 69 74 2d  Split;.  pSplit-
434d5 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e 65 77 3b  >pParent = pNew;
434d6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20  .}../*.** Parse 
434d7 74 68 65 20 66 74 73 33 20 71 75 65 72 79 20 65  the fts3 query e
434d8 78 70 72 65 73 73 69 6f 6e 20 66 6f 75 6e 64 20  xpression found 
434d9 69 6e 20 62 75 66 66 65 72 20 7a 2c 20 6c 65 6e  in buffer z, len
434da 67 74 68 20 6e 2e 20 54 68 69 73 20 66 75 6e 63  gth n. This func
434db 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72 6e 73 20  tion.** returns 
434dc 65 69 74 68 65 72 20 77 68 65 6e 20 74 68 65 20  either when the 
434dd 65 6e 64 20 6f 66 20 74 68 65 20 62 75 66 66 65  end of the buffe
434de 72 20 69 73 20 72 65 61 63 68 65 64 20 6f 72 20  r is reached or 
434df 61 6e 20 75 6e 6d 61 74 63 68 65 64 20 0a 2a 2a  an unmatched .**
434e0 20 63 6c 6f 73 69 6e 67 20 62 72 61 63 6b 65 74   closing bracket
434e1 20 2d 20 27 29 27 20 2d 20 69 73 20 65 6e 63 6f   - ')' - is enco
434e2 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  untered..**.** I
434e3 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51  f successful, SQ
434e4 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
434e5 6e 65 64 2c 20 2a 70 70 45 78 70 72 20 69 73 20  ned, *ppExpr is 
434e6 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  set to point to 
434e7 74 68 65 0a 2a 2a 20 70 61 72 73 65 64 20 66 6f  the.** parsed fo
434e8 72 6d 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  rm of the expres
434e9 73 69 6f 6e 20 61 6e 64 20 2a 70 6e 43 6f 6e 73  sion and *pnCons
434ea 75 6d 65 64 20 69 73 20 73 65 74 20 74 6f 20 74  umed is set to t
434eb 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
434ec 62 79 74 65 73 20 72 65 61 64 20 66 72 6f 6d 20  bytes read from 
434ed 62 75 66 66 65 72 20 7a 2e 20 4f 74 68 65 72 77  buffer z. Otherw
434ee 69 73 65 2c 20 2a 70 70 45 78 70 72 20 69 73 20  ise, *ppExpr is 
434ef 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c  set to 0 and SQL
434f0 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 6f 75  ITE_NOMEM.** (ou
434f1 74 20 6f 66 20 6d 65 6d 6f 72 79 20 65 72 72 6f  t of memory erro
434f2 72 29 20 6f 72 20 53 51 4c 49 54 45 5f 45 52 52  r) or SQLITE_ERR
434f3 4f 52 20 28 70 61 72 73 65 20 65 72 72 6f 72 29  OR (parse error)
434f4 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
434f5 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
434f6 45 78 70 72 50 61 72 73 65 28 0a 20 20 50 61 72  ExprParse(.  Par
434f7 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61 72 73  seContext *pPars
434f8 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
434f9 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71 75 65       /* fts3 que
434fa 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74  ry parse context
434fb 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
434fc 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20   *z, int n,     
434fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
434fe 20 54 65 78 74 20 6f 66 20 4d 41 54 43 48 20 71   Text of MATCH q
434ff 75 65 72 79 20 2a 2f 0a 20 20 46 74 73 33 45 78  uery */.  Fts3Ex
43500 70 72 20 2a 2a 70 70 45 78 70 72 2c 20 20 20 20  pr **ppExpr,    
43501 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43502 20 20 2f 2a 20 4f 55 54 3a 20 50 61 72 73 65 64    /* OUT: Parsed
43503 20 71 75 65 72 79 20 73 74 72 75 63 74 75 72 65   query structure
43504 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e   */.  int *pnCon
43505 73 75 6d 65 64 20 20 20 20 20 20 20 20 20 20 20  sumed           
43506 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
43507 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20   OUT: Number of 
43508 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 64 20 2a  bytes consumed *
43509 2f 0a 29 7b 0a 20 20 46 74 73 33 45 78 70 72 20  /.){.  Fts3Expr 
4350a 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20 46 74 73  *pRet = 0;.  Fts
4350b 33 45 78 70 72 20 2a 70 50 72 65 76 20 3d 20 30  3Expr *pPrev = 0
4350c 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 4e  ;.  Fts3Expr *pN
4350d 6f 74 42 72 61 6e 63 68 20 3d 20 30 3b 20 20 20  otBranch = 0;   
4350e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
4350f 6e 6c 79 20 75 73 65 64 20 69 6e 20 6c 65 67 61  nly used in lega
43510 63 79 20 70 61 72 73 65 20 6d 6f 64 65 20 2a 2f  cy parse mode */
43511 0a 20 20 69 6e 74 20 6e 49 6e 20 3d 20 6e 3b 0a  .  int nIn = n;.
43512 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49    const char *zI
43513 6e 20 3d 20 7a 3b 0a 20 20 69 6e 74 20 72 63 20  n = z;.  int rc 
43514 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
43515 6e 74 20 69 73 52 65 71 75 69 72 65 50 68 72 61  nt isRequirePhra
43516 73 65 20 3d 20 31 3b 0a 0a 20 20 77 68 69 6c 65  se = 1;..  while
43517 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
43518 29 7b 0a 20 20 20 20 46 74 73 33 45 78 70 72 20  ){.    Fts3Expr 
43519 2a 70 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  *p = 0;.    int 
4351a 6e 42 79 74 65 20 3d 20 30 3b 0a 20 20 20 20 72  nByte = 0;.    r
4351b 63 20 3d 20 67 65 74 4e 65 78 74 4e 6f 64 65 28  c = getNextNode(
4351c 70 50 61 72 73 65 2c 20 7a 49 6e 2c 20 6e 49 6e  pParse, zIn, nIn
4351d 2c 20 26 70 2c 20 26 6e 42 79 74 65 29 3b 0a 20  , &p, &nByte);. 
4351e 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
4351f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e  E_OK ){.      in
43520 74 20 69 73 50 68 72 61 73 65 3b 0a 0a 20 20 20  t isPhrase;..   
43521 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f     if( !sqlite3_
43522 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65  fts3_enable_pare
43523 6e 74 68 65 73 65 73 20 0a 20 20 20 20 20 20 20  ntheses .       
43524 26 26 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53  && p->eType==FTS
43525 51 55 45 52 59 5f 50 48 52 41 53 45 20 26 26 20  QUERY_PHRASE && 
43526 70 50 61 72 73 65 2d 3e 69 73 4e 6f 74 20 0a 20  pParse->isNot . 
43527 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
43528 2f 2a 20 43 72 65 61 74 65 20 61 6e 20 69 6d 70  /* Create an imp
43529 6c 69 63 69 74 20 4e 4f 54 20 6f 70 65 72 61 74  licit NOT operat
4352a 6f 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 46  or. */.        F
4352b 74 73 33 45 78 70 72 20 2a 70 4e 6f 74 20 3d 20  ts3Expr *pNot = 
4352c 66 74 73 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73  fts3MallocZero(s
4352d 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 29  izeof(Fts3Expr))
4352e 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70  ;.        if( !p
4352f 4e 6f 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Not ){.         
43530 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
43531 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20  Free(p);.       
43532 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
43533 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20  OMEM;.          
43534 67 6f 74 6f 20 65 78 70 72 70 61 72 73 65 5f 6f  goto exprparse_o
43535 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ut;.        }.  
43536 20 20 20 20 20 20 70 4e 6f 74 2d 3e 65 54 79 70        pNot->eTyp
43537 65 20 3d 20 46 54 53 51 55 45 52 59 5f 4e 4f 54  e = FTSQUERY_NOT
43538 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f 74 2d 3e  ;.        pNot->
43539 70 52 69 67 68 74 20 3d 20 70 3b 0a 20 20 20 20  pRight = p;.    
4353a 20 20 20 20 69 66 28 20 70 4e 6f 74 42 72 61 6e      if( pNotBran
4353b 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ch ){.          
4353c 70 4e 6f 74 2d 3e 70 4c 65 66 74 20 3d 20 70 4e  pNot->pLeft = pN
4353d 6f 74 42 72 61 6e 63 68 3b 0a 20 20 20 20 20 20  otBranch;.      
4353e 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4e 6f 74    }.        pNot
4353f 42 72 61 6e 63 68 20 3d 20 70 4e 6f 74 3b 0a 20  Branch = pNot;. 
43540 20 20 20 20 20 20 20 70 20 3d 20 70 50 72 65 76         p = pPrev
43541 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
43542 20 20 20 20 20 20 20 69 6e 74 20 65 54 79 70 65         int eType
43543 20 3d 20 70 2d 3e 65 54 79 70 65 3b 0a 20 20 20   = p->eType;.   
43544 20 20 20 20 20 69 73 50 68 72 61 73 65 20 3d 20       isPhrase = 
43545 28 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  (eType==FTSQUERY
43546 5f 50 48 52 41 53 45 20 7c 7c 20 70 2d 3e 70 4c  _PHRASE || p->pL
43547 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f  eft);..        /
43548 2a 20 54 68 65 20 69 73 52 65 71 75 69 72 65 50  * The isRequireP
43549 68 72 61 73 65 20 76 61 72 69 61 62 6c 65 20 69  hrase variable i
4354a 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69 66  s set to true if
4354b 20 61 20 70 68 72 61 73 65 20 6f 72 0a 20 20 20   a phrase or.   
4354c 20 20 20 20 20 2a 2a 20 61 6e 20 65 78 70 72 65       ** an expre
4354d 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 65 64 20  ssion contained 
4354e 69 6e 20 70 61 72 65 6e 74 68 65 73 69 73 20 69  in parenthesis i
4354f 73 20 72 65 71 75 69 72 65 64 2e 20 49 66 20 61  s required. If a
43550 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 69 6e 61  .        ** bina
43551 72 79 20 6f 70 65 72 61 74 6f 72 20 28 41 4e 44  ry operator (AND
43552 2c 20 4f 52 2c 20 4e 4f 54 20 6f 72 20 4e 45 41  , OR, NOT or NEA
43553 52 29 20 69 73 20 65 6e 63 6f 75 6e 74 65 64 20  R) is encounted 
43554 77 68 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  when.        ** 
43555 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20  isRequirePhrase 
43556 69 73 20 73 65 74 2c 20 74 68 69 73 20 69 73 20  is set, this is 
43557 61 20 73 79 6e 74 61 78 20 65 72 72 6f 72 2e 0a  a syntax error..
43558 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
43559 20 20 20 69 66 28 20 21 69 73 50 68 72 61 73 65     if( !isPhrase
4355a 20 26 26 20 69 73 52 65 71 75 69 72 65 50 68 72   && isRequirePhr
4355b 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ase ){.         
4355c 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
4355d 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20  Free(p);.       
4355e 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
4355f 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20  RROR;.          
43560 67 6f 74 6f 20 65 78 70 72 70 61 72 73 65 5f 6f  goto exprparse_o
43561 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ut;.        }.  
43562 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73 50  .        if( isP
43563 68 72 61 73 65 20 26 26 20 21 69 73 52 65 71 75  hrase && !isRequ
43564 69 72 65 50 68 72 61 73 65 20 29 7b 0a 20 20 20  irePhrase ){.   
43565 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74         /* Insert
43566 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 41 4e 44   an implicit AND
43567 20 6f 70 65 72 61 74 6f 72 2e 20 2a 2f 0a 20 20   operator. */.  
43568 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72          Fts3Expr
43569 20 2a 70 41 6e 64 3b 0a 20 20 20 20 20 20 20 20   *pAnd;.        
4356a 20 20 61 73 73 65 72 74 28 20 70 52 65 74 20 26    assert( pRet &
4356b 26 20 70 50 72 65 76 20 29 3b 0a 20 20 20 20 20  & pPrev );.     
4356c 20 20 20 20 20 70 41 6e 64 20 3d 20 66 74 73 33       pAnd = fts3
4356d 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f  MallocZero(sizeo
4356e 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20 20  f(Fts3Expr));.  
4356f 20 20 20 20 20 20 20 20 69 66 28 20 21 70 41 6e          if( !pAn
43570 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  d ){.           
43571 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
43572 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20  Free(p);.       
43573 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
43574 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
43575 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70 61 72      goto exprpar
43576 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  se_out;.        
43577 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 41    }.          pA
43578 6e 64 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51  nd->eType = FTSQ
43579 55 45 52 59 5f 41 4e 44 3b 0a 20 20 20 20 20 20  UERY_AND;.      
4357a 20 20 20 20 69 6e 73 65 72 74 42 69 6e 61 72 79      insertBinary
4357b 4f 70 65 72 61 74 6f 72 28 26 70 52 65 74 2c 20  Operator(&pRet, 
4357c 70 50 72 65 76 2c 20 70 41 6e 64 29 3b 0a 20 20  pPrev, pAnd);.  
4357d 20 20 20 20 20 20 20 20 70 50 72 65 76 20 3d 20          pPrev = 
4357e 70 41 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a  pAnd;.        }.
4357f 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73  .        /* This
43580 20 74 65 73 74 20 63 61 74 63 68 65 73 20 61 74   test catches at
43581 74 65 6d 70 74 73 20 74 6f 20 6d 61 6b 65 20 65  tempts to make e
43582 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 6f 66  ither operand of
43583 20 61 20 4e 45 41 52 0a 20 20 20 20 20 20 20 20   a NEAR.        
43584 2a 2a 20 6f 70 65 72 61 74 6f 72 20 73 6f 6d 65  ** operator some
43585 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e  thing other than
43586 20 61 20 70 68 72 61 73 65 2e 20 46 6f 72 20 65   a phrase. For e
43587 78 61 6d 70 6c 65 2c 20 65 69 74 68 65 72 20 6f  xample, either o
43588 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  f.        ** the
43589 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 20 20   following:.    
4358a 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
4358b 2a 20 20 20 20 28 62 72 61 63 6b 65 74 65 64 20  *    (bracketed 
4358c 65 78 70 72 65 73 73 69 6f 6e 29 20 4e 45 41 52  expression) NEAR
4358d 20 70 68 72 61 73 65 0a 20 20 20 20 20 20 20 20   phrase.        
4358e 2a 2a 20 20 20 20 70 68 72 61 73 65 20 4e 45 41  **    phrase NEA
4358f 52 20 28 62 72 61 63 6b 65 74 65 64 20 65 78 70  R (bracketed exp
43590 72 65 73 73 69 6f 6e 29 0a 20 20 20 20 20 20 20  ression).       
43591 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 52   **.        ** R
43592 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69  eturn an error i
43593 6e 20 65 69 74 68 65 72 20 63 61 73 65 2e 0a 20  n either case.. 
43594 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
43595 20 20 69 66 28 20 70 50 72 65 76 20 26 26 20 28    if( pPrev && (
43596 0a 20 20 20 20 20 20 20 20 20 20 20 20 28 65 54  .            (eT
43597 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45  ype==FTSQUERY_NE
43598 41 52 20 26 26 20 21 69 73 50 68 72 61 73 65 20  AR && !isPhrase 
43599 26 26 20 70 50 72 65 76 2d 3e 65 54 79 70 65 21  && pPrev->eType!
4359a 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45  =FTSQUERY_PHRASE
4359b 29 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 28 65  ).         || (e
4359c 54 79 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50  Type!=FTSQUERY_P
4359d 48 52 41 53 45 20 26 26 20 69 73 50 68 72 61 73  HRASE && isPhras
4359e 65 20 26 26 20 70 50 72 65 76 2d 3e 65 54 79 70  e && pPrev->eTyp
4359f 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
435a0 29 0a 20 20 20 20 20 20 20 20 29 29 7b 0a 20 20  ).        )){.  
435a1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46          sqlite3F
435a2 74 73 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a  ts3ExprFree(p);.
435a3 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
435a4 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
435a5 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72         goto expr
435a6 70 61 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20  parse_out;.     
435a7 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 20 20     }.  .        
435a8 69 66 28 20 69 73 50 68 72 61 73 65 20 29 7b 0a  if( isPhrase ){.
435a9 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 52            if( pR
435aa 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  et ){.          
435ab 20 20 61 73 73 65 72 74 28 20 70 50 72 65 76 20    assert( pPrev 
435ac 26 26 20 70 50 72 65 76 2d 3e 70 4c 65 66 74 20  && pPrev->pLeft 
435ad 26 26 20 70 50 72 65 76 2d 3e 70 52 69 67 68 74  && pPrev->pRight
435ae 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ==0 );.         
435af 20 20 20 70 50 72 65 76 2d 3e 70 52 69 67 68 74     pPrev->pRight
435b0 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20   = p;.          
435b1 20 20 70 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70    p->pParent = p
435b2 50 72 65 76 3b 0a 20 20 20 20 20 20 20 20 20 20  Prev;.          
435b3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
435b4 20 20 20 70 52 65 74 20 3d 20 70 3b 0a 20 20 20     pRet = p;.   
435b5 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
435b6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
435b7 20 20 69 6e 73 65 72 74 42 69 6e 61 72 79 4f 70    insertBinaryOp
435b8 65 72 61 74 6f 72 28 26 70 52 65 74 2c 20 70 50  erator(&pRet, pP
435b9 72 65 76 2c 20 70 29 3b 0a 20 20 20 20 20 20 20  rev, p);.       
435ba 20 7d 0a 20 20 20 20 20 20 20 20 69 73 52 65 71   }.        isReq
435bb 75 69 72 65 50 68 72 61 73 65 20 3d 20 21 69 73  uirePhrase = !is
435bc 50 68 72 61 73 65 3b 0a 20 20 20 20 20 20 7d 0a  Phrase;.      }.
435bd 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 42        assert( nB
435be 79 74 65 3e 30 20 29 3b 0a 20 20 20 20 7d 0a 20  yte>0 );.    }. 
435bf 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
435c0 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 6e 42 79  QLITE_OK || (nBy
435c1 74 65 3e 30 20 26 26 20 6e 42 79 74 65 3c 3d 6e  te>0 && nByte<=n
435c2 49 6e 29 20 29 3b 0a 20 20 20 20 6e 49 6e 20 2d  In) );.    nIn -
435c3 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7a 49 6e  = nByte;.    zIn
435c4 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70   += nByte;.    p
435c5 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20  Prev = p;.  }.. 
435c6 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
435c7 44 4f 4e 45 20 26 26 20 70 52 65 74 20 26 26 20  DONE && pRet && 
435c8 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20  isRequirePhrase 
435c9 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
435ca 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20  TE_ERROR;.  }.. 
435cb 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
435cc 44 4f 4e 45 20 29 7b 0a 20 20 20 20 72 63 20 3d  DONE ){.    rc =
435cd 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
435ce 69 66 28 20 21 73 71 6c 69 74 65 33 5f 66 74 73  if( !sqlite3_fts
435cf 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68  3_enable_parenth
435d0 65 73 65 73 20 26 26 20 70 4e 6f 74 42 72 61 6e  eses && pNotBran
435d1 63 68 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ch ){.      if( 
435d2 21 70 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20  !pRet ){.       
435d3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
435d4 4f 52 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  OR;.      }else{
435d5 0a 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70  .        Fts3Exp
435d6 72 20 2a 70 49 74 65 72 20 3d 20 70 4e 6f 74 42  r *pIter = pNotB
435d7 72 61 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 77  ranch;.        w
435d8 68 69 6c 65 28 20 70 49 74 65 72 2d 3e 70 4c 65  hile( pIter->pLe
435d9 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ft ){.          
435da 70 49 74 65 72 20 3d 20 70 49 74 65 72 2d 3e 70  pIter = pIter->p
435db 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Left;.        }.
435dc 20 20 20 20 20 20 20 20 70 49 74 65 72 2d 3e 70          pIter->p
435dd 4c 65 66 74 20 3d 20 70 52 65 74 3b 0a 20 20 20  Left = pRet;.   
435de 20 20 20 20 20 70 52 65 74 20 3d 20 70 4e 6f 74       pRet = pNot
435df 42 72 61 6e 63 68 3b 0a 20 20 20 20 20 20 7d 0a  Branch;.      }.
435e0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 6e 43      }.  }.  *pnC
435e1 6f 6e 73 75 6d 65 64 20 3d 20 6e 20 2d 20 6e 49  onsumed = n - nI
435e2 6e 3b 0a 0a 65 78 70 72 70 61 72 73 65 5f 6f 75  n;..exprparse_ou
435e3 74 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  t:.  if( rc!=SQL
435e4 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
435e5 6c 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65  lite3Fts3ExprFre
435e6 65 28 70 52 65 74 29 3b 0a 20 20 20 20 73 71 6c  e(pRet);.    sql
435e7 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
435e8 28 70 4e 6f 74 42 72 61 6e 63 68 29 3b 0a 20 20  (pNotBranch);.  
435e9 20 20 70 52 65 74 20 3d 20 30 3b 0a 20 20 7d 0a    pRet = 0;.  }.
435ea 20 20 2a 70 70 45 78 70 72 20 3d 20 70 52 65 74    *ppExpr = pRet
435eb 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
435ec 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65  ../*.** Paramete
435ed 72 73 20 7a 20 61 6e 64 20 6e 20 63 6f 6e 74 61  rs z and n conta
435ee 69 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  in a pointer to 
435ef 61 6e 64 20 6c 65 6e 67 74 68 20 6f 66 20 61 20  and length of a 
435f0 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e  buffer containin
435f1 67 0a 2a 2a 20 61 6e 20 66 74 73 33 20 71 75 65  g.** an fts3 que
435f2 72 79 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 72  ry expression, r
435f3 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 68 69  espectively. Thi
435f4 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d  s function attem
435f5 70 74 73 20 74 6f 20 70 61 72 73 65 20 74 68 65  pts to parse the
435f6 0a 2a 2a 20 71 75 65 72 79 20 65 78 70 72 65 73  .** query expres
435f7 73 69 6f 6e 20 61 6e 64 20 63 72 65 61 74 65 20  sion and create 
435f8 61 20 74 72 65 65 20 6f 66 20 46 74 73 33 45 78  a tree of Fts3Ex
435f9 70 72 20 73 74 72 75 63 74 75 72 65 73 20 72 65  pr structures re
435fa 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 0a 2a  presenting the.*
435fb 2a 20 70 61 72 73 65 64 20 65 78 70 72 65 73 73  * parsed express
435fc 69 6f 6e 2e 20 49 66 20 73 75 63 63 65 73 73 66  ion. If successf
435fd 75 6c 2c 20 2a 70 70 45 78 70 72 20 69 73 20 73  ul, *ppExpr is s
435fe 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  et to point to t
435ff 68 65 20 68 65 61 64 0a 2a 2a 20 6f 66 20 74 68  he head.** of th
43600 65 20 70 61 72 73 65 64 20 65 78 70 72 65 73 73  e parsed express
43601 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 53 51 4c  ion tree and SQL
43602 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
43603 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 0a  ed. If an error.
43604 2a 2a 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65  ** occurs, eithe
43605 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 28  r SQLITE_NOMEM (
43606 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72  out-of-memory er
43607 72 6f 72 29 20 6f 72 20 53 51 4c 49 54 45 5f 45  ror) or SQLITE_E
43608 52 52 4f 52 20 28 70 61 72 73 65 0a 2a 2a 20 65  RROR (parse.** e
43609 72 72 6f 72 29 20 69 73 20 72 65 74 75 72 6e 65  rror) is returne
4360a 64 20 61 6e 64 20 2a 70 70 45 78 70 72 20 69 73  d and *ppExpr is
4360b 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a   set to 0..**.**
4360c 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 6e 20   If parameter n 
4360d 69 73 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75  is a negative nu
4360e 6d 62 65 72 2c 20 74 68 65 6e 20 7a 20 69 73 20  mber, then z is 
4360f 61 73 73 75 6d 65 64 20 74 6f 20 70 6f 69 6e 74  assumed to point
43610 20 74 6f 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72   to a.** nul-ter
43611 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20 61  minated string a
43612 6e 64 20 74 68 65 20 6c 65 6e 67 74 68 20 69 73  nd the length is
43613 20 64 65 74 65 72 6d 69 6e 65 64 20 75 73 69 6e   determined usin
43614 67 20 73 74 72 6c 65 6e 28 29 2e 0a 2a 2a 0a 2a  g strlen()..**.*
43615 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61  * The first para
43616 6d 65 74 65 72 2c 20 70 54 6f 6b 65 6e 69 7a 65  meter, pTokenize
43617 72 2c 20 69 73 20 70 61 73 73 65 64 20 74 68 65  r, is passed the
43618 20 66 74 73 33 20 74 6f 6b 65 6e 69 7a 65 72 20   fts3 tokenizer 
43619 6d 6f 64 75 6c 65 20 74 6f 0a 2a 2a 20 75 73 65  module to.** use
4361a 20 74 6f 20 6e 6f 72 6d 61 6c 69 7a 65 20 71 75   to normalize qu
4361b 65 72 79 20 74 6f 6b 65 6e 73 20 77 68 69 6c 65  ery tokens while
4361c 20 70 61 72 73 69 6e 67 20 74 68 65 20 65 78 70   parsing the exp
4361d 72 65 73 73 69 6f 6e 2e 20 54 68 65 20 61 7a 43  ression. The azC
4361e 6f 6c 5b 5d 0a 2a 2a 20 61 72 72 61 79 2c 20 77  ol[].** array, w
4361f 68 69 63 68 20 69 73 20 61 73 73 75 6d 65 64 20  hich is assumed 
43620 74 6f 20 63 6f 6e 74 61 69 6e 20 6e 43 6f 6c 20  to contain nCol 
43621 65 6e 74 72 69 65 73 2c 20 73 68 6f 75 6c 64 20  entries, should 
43622 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65  contain the name
43623 73 0a 2a 2a 20 6f 66 20 65 61 63 68 20 63 6f 6c  s.** of each col
43624 75 6d 6e 20 69 6e 20 74 68 65 20 74 61 72 67 65  umn in the targe
43625 74 20 66 74 73 33 20 74 61 62 6c 65 2c 20 69 6e  t fts3 table, in
43626 20 6f 72 64 65 72 20 66 72 6f 6d 20 6c 65 66 74   order from left
43627 20 74 6f 20 72 69 67 68 74 2e 20 0a 2a 2a 20 43   to right. .** C
43628 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d 75 73 74  olumn names must
43629 20 62 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74   be nul-terminat
4362a 65 64 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a  ed strings..**.*
4362b 2a 20 54 68 65 20 69 44 65 66 61 75 6c 74 43 6f  * The iDefaultCo
4362c 6c 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75  l parameter shou
4362d 6c 64 20 62 65 20 70 61 73 73 65 64 20 74 68 65  ld be passed the
4362e 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 74 61   index of the ta
4362f 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 68  ble column.** th
43630 61 74 20 61 70 70 65 61 72 73 20 6f 6e 20 74 68  at appears on th
43631 65 20 6c 65 66 74 2d 68 61 6e 64 2d 73 69 64 65  e left-hand-side
43632 20 6f 66 20 74 68 65 20 4d 41 54 43 48 20 6f 70   of the MATCH op
43633 65 72 61 74 6f 72 20 28 74 68 65 20 64 65 66 61  erator (the defa
43634 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 74 6f  ult.** column to
43635 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74 20 66   match against f
43636 6f 72 20 74 6f 6b 65 6e 73 20 66 6f 72 20 77 68  or tokens for wh
43637 69 63 68 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ich a column nam
43638 65 20 69 73 20 6e 6f 74 20 65 78 70 6c 69 63 69  e is not explici
43639 74 6c 79 0a 2a 2a 20 73 70 65 63 69 66 69 65 64  tly.** specified
4363a 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
4363b 71 75 65 72 79 20 73 74 72 69 6e 67 29 2c 20 6f  query string), o
4363c 72 20 2d 31 20 69 66 20 74 6f 6b 65 6e 73 20 6d  r -1 if tokens m
4363d 61 79 20 62 79 20 64 65 66 61 75 6c 74 0a 2a 2a  ay by default.**
4363e 20 6d 61 74 63 68 20 61 6e 79 20 74 61 62 6c 65   match any table
4363f 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49   column..*/.SQLI
43640 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
43641 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 50 61  qlite3Fts3ExprPa
43642 72 73 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  rse(.  sqlite3_t
43643 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
43644 69 7a 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 6f  izer,      /* To
43645 6b 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a  kenizer module *
43646 2f 0a 20 20 69 6e 74 20 69 4c 61 6e 67 69 64 2c  /.  int iLangid,
43647 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43648 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 6e 67 75          /* Langu
43649 61 67 65 20 69 64 20 66 6f 72 20 74 6f 6b 65 6e  age id for token
4364a 69 7a 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a  izer */.  char *
4364b 2a 61 7a 43 6f 6c 2c 20 20 20 20 20 20 20 20 20  *azCol,         
4364c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4364d 20 41 72 72 61 79 20 6f 66 20 63 6f 6c 75 6d 6e   Array of column
4364e 20 6e 61 6d 65 73 20 66 6f 72 20 66 74 73 33 20   names for fts3 
4364f 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 62  table */.  int b
43650 46 74 73 34 2c 20 20 20 20 20 20 20 20 20 20 20  Fts4,           
43651 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
43652 2a 20 54 72 75 65 20 74 6f 20 61 6c 6c 6f 77 20  * True to allow 
43653 46 54 53 34 2d 6f 6e 6c 79 20 73 79 6e 74 61 78  FTS4-only syntax
43654 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c 20   */.  int nCol, 
43655 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43656 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
43657 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
43658 6e 20 61 7a 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69  n azCol[] */.  i
43659 6e 74 20 69 44 65 66 61 75 6c 74 43 6f 6c 2c 20  nt iDefaultCol, 
4365a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4365b 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 63 6f     /* Default co
4365c 6c 75 6d 6e 20 74 6f 20 71 75 65 72 79 20 2a 2f  lumn to query */
4365d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
4365e 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20  , int n,        
4365f 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f         /* Text o
43660 66 20 4d 41 54 43 48 20 71 75 65 72 79 20 2a 2f  f MATCH query */
43661 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a 70 70  .  Fts3Expr **pp
43662 45 78 70 72 20 20 20 20 20 20 20 20 20 20 20 20  Expr            
43663 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50         /* OUT: P
43664 61 72 73 65 64 20 71 75 65 72 79 20 73 74 72 75  arsed query stru
43665 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  cture */.){.  in
43666 74 20 6e 50 61 72 73 65 64 3b 0a 20 20 69 6e 74  t nParsed;.  int
43667 20 72 63 3b 0a 20 20 50 61 72 73 65 43 6f 6e 74   rc;.  ParseCont
43668 65 78 74 20 73 50 61 72 73 65 3b 0a 0a 20 20 6d  ext sParse;..  m
43669 65 6d 73 65 74 28 26 73 50 61 72 73 65 2c 20 30  emset(&sParse, 0
4366a 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65 43 6f  , sizeof(ParseCo
4366b 6e 74 65 78 74 29 29 3b 0a 20 20 73 50 61 72 73  ntext));.  sPars
4366c 65 2e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70  e.pTokenizer = p
4366d 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 50 61  Tokenizer;.  sPa
4366e 72 73 65 2e 69 4c 61 6e 67 69 64 20 3d 20 69 4c  rse.iLangid = iL
4366f 61 6e 67 69 64 3b 0a 20 20 73 50 61 72 73 65 2e  angid;.  sParse.
43670 61 7a 43 6f 6c 20 3d 20 28 63 6f 6e 73 74 20 63  azCol = (const c
43671 68 61 72 20 2a 2a 29 61 7a 43 6f 6c 3b 0a 20 20  har **)azCol;.  
43672 73 50 61 72 73 65 2e 6e 43 6f 6c 20 3d 20 6e 43  sParse.nCol = nC
43673 6f 6c 3b 0a 20 20 73 50 61 72 73 65 2e 69 44 65  ol;.  sParse.iDe
43674 66 61 75 6c 74 43 6f 6c 20 3d 20 69 44 65 66 61  faultCol = iDefa
43675 75 6c 74 43 6f 6c 3b 0a 20 20 73 50 61 72 73 65  ultCol;.  sParse
43676 2e 62 46 74 73 34 20 3d 20 62 46 74 73 34 3b 0a  .bFts4 = bFts4;.
43677 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20    if( z==0 ){.  
43678 20 20 2a 70 70 45 78 70 72 20 3d 20 30 3b 0a 20    *ppExpr = 0;. 
43679 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
4367a 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  _OK;.  }.  if( n
4367b 3c 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 28 69  <0 ){.    n = (i
4367c 6e 74 29 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20  nt)strlen(z);.  
4367d 7d 0a 20 20 72 63 20 3d 20 66 74 73 33 45 78 70  }.  rc = fts3Exp
4367e 72 50 61 72 73 65 28 26 73 50 61 72 73 65 2c 20  rParse(&sParse, 
4367f 7a 2c 20 6e 2c 20 70 70 45 78 70 72 2c 20 26 6e  z, n, ppExpr, &n
43680 50 61 72 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 43  Parsed);..  /* C
43681 68 65 63 6b 20 66 6f 72 20 6d 69 73 6d 61 74 63  heck for mismatc
43682 68 65 64 20 70 61 72 65 6e 74 68 65 73 69 73 20  hed parenthesis 
43683 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
43684 49 54 45 5f 4f 4b 20 26 26 20 73 50 61 72 73 65  ITE_OK && sParse
43685 2e 6e 4e 65 73 74 20 29 7b 0a 20 20 20 20 72 63  .nNest ){.    rc
43686 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
43687 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33  .    sqlite3Fts3
43688 45 78 70 72 46 72 65 65 28 2a 70 70 45 78 70 72  ExprFree(*ppExpr
43689 29 3b 0a 20 20 20 20 2a 70 70 45 78 70 72 20 3d  );.    *ppExpr =
4368a 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72   0;.  }..  retur
4368b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  n rc;.}../*.** F
4368c 72 65 65 20 61 20 70 61 72 73 65 64 20 66 74 73  ree a parsed fts
4368d 33 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  3 query expressi
4368e 6f 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  on allocated by 
4368f 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 50  sqlite3Fts3ExprP
43690 61 72 73 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  arse()..*/.SQLIT
43691 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
43692 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46 72  qlite3Fts3ExprFr
43693 65 65 28 46 74 73 33 45 78 70 72 20 2a 70 29 7b  ee(Fts3Expr *p){
43694 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
43695 61 73 73 65 72 74 28 20 70 2d 3e 65 54 79 70 65  assert( p->eType
43696 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  ==FTSQUERY_PHRAS
43697 45 20 7c 7c 20 70 2d 3e 70 50 68 72 61 73 65 3d  E || p->pPhrase=
43698 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  =0 );.    sqlite
43699 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70 2d  3Fts3ExprFree(p-
4369a 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71 6c  >pLeft);.    sql
4369b 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
4369c 28 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20  (p->pRight);.   
4369d 20 73 71 6c 69 74 65 33 46 74 73 33 45 76 61 6c   sqlite3Fts3Eval
4369e 50 68 72 61 73 65 43 6c 65 61 6e 75 70 28 70 2d  PhraseCleanup(p-
4369f 3e 70 50 68 72 61 73 65 29 3b 0a 20 20 20 20 73  >pPhrase);.    s
436a0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61  qlite3_free(p->a
436a1 4d 49 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  MI);.    sqlite3
436a2 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a  _free(p);.  }.}.
436a3 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
436a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
436a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
436a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
436a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
436a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
436a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
436aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
436ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
436ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
436ad 45 76 65 72 79 74 68 69 6e 67 20 61 66 74 65 72  Everything after
436ae 20 74 68 69 73 20 70 6f 69 6e 74 20 69 73 20 6a   this point is j
436af 75 73 74 20 74 65 73 74 20 63 6f 64 65 2e 0a 2a  ust test code..*
436b0 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
436b1 5f 54 45 53 54 0a 0a 2f 2a 20 23 69 6e 63 6c 75  _TEST../* #inclu
436b2 64 65 20 3c 73 74 64 69 6f 2e 68 3e 20 2a 2f 0a  de <stdio.h> */.
436b3 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20  ./*.** Function 
436b4 74 6f 20 71 75 65 72 79 20 74 68 65 20 68 61 73  to query the has
436b5 68 2d 74 61 62 6c 65 20 6f 66 20 74 6f 6b 65 6e  h-table of token
436b6 69 7a 65 72 73 20 28 73 65 65 20 52 45 41 44 4d  izers (see READM
436b7 45 2e 74 6f 6b 65 6e 69 7a 65 72 73 29 2e 0a 2a  E.tokenizers)..*
436b8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 71 75 65  /.static int que
436b9 72 79 54 65 73 74 54 6f 6b 65 6e 69 7a 65 72 28  ryTestTokenizer(
436ba 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
436bb 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
436bc 4e 61 6d 65 2c 20 20 0a 20 20 63 6f 6e 73 74 20  Name,  .  const 
436bd 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
436be 72 5f 6d 6f 64 75 6c 65 20 2a 2a 70 70 0a 29 7b  r_module **pp.){
436bf 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
436c0 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
436c1 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 7a  ;.  const char z
436c2 53 71 6c 5b 5d 20 3d 20 22 53 45 4c 45 43 54 20  Sql[] = "SELECT 
436c3 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f  fts3_tokenizer(?
436c4 29 22 3b 0a 0a 20 20 2a 70 70 20 3d 20 30 3b 0a  )";..  *pp = 0;.
436c5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70    rc = sqlite3_p
436c6 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53  repare_v2(db, zS
436c7 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20  ql, -1, &pStmt, 
436c8 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  0);.  if( rc!=SQ
436c9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
436ca 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
436cb 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
436cc 78 74 28 70 53 74 6d 74 2c 20 31 2c 20 7a 4e 61  xt(pStmt, 1, zNa
436cd 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53  me, -1, SQLITE_S
436ce 54 41 54 49 43 29 3b 0a 20 20 69 66 28 20 53 51  TATIC);.  if( SQ
436cf 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65  LITE_ROW==sqlite
436d0 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b  3_step(pStmt) ){
436d1 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
436d2 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74  _column_type(pSt
436d3 6d 74 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 42  mt, 0)==SQLITE_B
436d4 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  LOB ){.      mem
436d5 63 70 79 28 28 76 6f 69 64 20 2a 29 70 70 2c 20  cpy((void *)pp, 
436d6 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
436d7 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29 2c 20 73  lob(pStmt, 0), s
436d8 69 7a 65 6f 66 28 2a 70 70 29 29 3b 0a 20 20 20  izeof(*pp));.   
436d9 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
436da 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
436db 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a  e(pStmt);.}../*.
436dc 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
436dd 74 65 72 20 74 6f 20 61 20 62 75 66 66 65 72 20  ter to a buffer 
436de 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 74 65 78  containing a tex
436df 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  t representation
436e0 20 6f 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 65   of the.** expre
436e1 73 73 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20  ssion passed as 
436e2 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
436e3 6e 74 2e 20 54 68 65 20 62 75 66 66 65 72 20 69  nt. The buffer i
436e4 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a  s obtained from.
436e5 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ** sqlite3_mallo
436e6 63 28 29 2e 20 49 74 20 69 73 20 74 68 65 20 72  c(). It is the r
436e7 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66  esponsibility of
436e8 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 75   the caller to u
436e9 73 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66  se .** sqlite3_f
436ea 72 65 65 28 29 20 74 6f 20 72 65 6c 65 61 73 65  ree() to release
436eb 20 74 68 65 20 6d 65 6d 6f 72 79 2e 20 49 66 20   the memory. If 
436ec 61 6e 20 4f 4f 4d 20 63 6f 6e 64 69 74 69 6f 6e  an OOM condition
436ed 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c   is encountered,
436ee 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  .** NULL is retu
436ef 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  rned..**.** If t
436f0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
436f1 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  nt is not NULL, 
436f2 74 68 65 6e 20 69 74 73 20 63 6f 6e 74 65 6e 74  then its content
436f3 73 20 61 72 65 20 70 72 65 70 65 6e 64 65 64 20  s are prepended 
436f4 74 6f 20 0a 2a 2a 20 74 68 65 20 72 65 74 75 72  to .** the retur
436f5 6e 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 74  ned expression t
436f6 65 78 74 20 61 6e 64 20 74 68 65 6e 20 66 72 65  ext and then fre
436f7 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ed using sqlite3
436f8 5f 66 72 65 65 28 29 2e 0a 2a 2f 0a 73 74 61 74  _free()..*/.stat
436f9 69 63 20 63 68 61 72 20 2a 65 78 70 72 54 6f 53  ic char *exprToS
436fa 74 72 69 6e 67 28 46 74 73 33 45 78 70 72 20 2a  tring(Fts3Expr *
436fb 70 45 78 70 72 2c 20 63 68 61 72 20 2a 7a 42 75  pExpr, char *zBu
436fc 66 29 7b 0a 20 20 73 77 69 74 63 68 28 20 70 45  f){.  switch( pE
436fd 78 70 72 2d 3e 65 54 79 70 65 20 29 7b 0a 20 20  xpr->eType ){.  
436fe 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f    case FTSQUERY_
436ff 50 48 52 41 53 45 3a 20 7b 0a 20 20 20 20 20 20  PHRASE: {.      
43700 46 74 73 33 50 68 72 61 73 65 20 2a 70 50 68 72  Fts3Phrase *pPhr
43701 61 73 65 20 3d 20 70 45 78 70 72 2d 3e 70 50 68  ase = pExpr->pPh
43702 72 61 73 65 3b 0a 20 20 20 20 20 20 69 6e 74 20  rase;.      int 
43703 69 3b 0a 20 20 20 20 20 20 7a 42 75 66 20 3d 20  i;.      zBuf = 
43704 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
43705 0a 20 20 20 20 20 20 20 20 20 20 22 25 7a 50 48  .          "%zPH
43706 52 41 53 45 20 25 64 20 30 22 2c 20 7a 42 75 66  RASE %d 0", zBuf
43707 2c 20 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75  , pPhrase->iColu
43708 6d 6e 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  mn);.      for(i
43709 3d 30 3b 20 7a 42 75 66 20 26 26 20 69 3c 70 50  =0; zBuf && i<pP
4370a 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 69  hrase->nToken; i
4370b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75  ++){.        zBu
4370c 66 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  f = sqlite3_mpri
4370d 6e 74 66 28 22 25 7a 20 25 2e 2a 73 25 73 22 2c  ntf("%z %.*s%s",
4370e 20 7a 42 75 66 2c 20 0a 20 20 20 20 20 20 20 20   zBuf, .        
4370f 20 20 20 20 70 50 68 72 61 73 65 2d 3e 61 54 6f      pPhrase->aTo
43710 6b 65 6e 5b 69 5d 2e 6e 2c 20 70 50 68 72 61 73  ken[i].n, pPhras
43711 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 7a 2c 0a  e->aToken[i].z,.
43712 20 20 20 20 20 20 20 20 20 20 20 20 28 70 50 68              (pPh
43713 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e  rase->aToken[i].
43714 69 73 50 72 65 66 69 78 3f 22 2b 22 3a 22 22 29  isPrefix?"+":"")
43715 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .        );.    
43716 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e    }.      return
43717 20 7a 42 75 66 3b 0a 20 20 20 20 7d 0a 0a 20 20   zBuf;.    }..  
43718 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f    case FTSQUERY_
43719 4e 45 41 52 3a 0a 20 20 20 20 20 20 7a 42 75 66  NEAR:.      zBuf
4371a 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
4371b 74 66 28 22 25 7a 4e 45 41 52 2f 25 64 20 22 2c  tf("%zNEAR/%d ",
4371c 20 7a 42 75 66 2c 20 70 45 78 70 72 2d 3e 6e 4e   zBuf, pExpr->nN
4371d 65 61 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ear);.      brea
4371e 6b 3b 0a 20 20 20 20 63 61 73 65 20 46 54 53 51  k;.    case FTSQ
4371f 55 45 52 59 5f 4e 4f 54 3a 0a 20 20 20 20 20 20  UERY_NOT:.      
43720 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zBuf = sqlite3_m
43721 70 72 69 6e 74 66 28 22 25 7a 4e 4f 54 20 22 2c  printf("%zNOT ",
43722 20 7a 42 75 66 29 3b 0a 20 20 20 20 20 20 62 72   zBuf);.      br
43723 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 46 54  eak;.    case FT
43724 53 51 55 45 52 59 5f 41 4e 44 3a 0a 20 20 20 20  SQUERY_AND:.    
43725 20 20 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 33    zBuf = sqlite3
43726 5f 6d 70 72 69 6e 74 66 28 22 25 7a 41 4e 44 20  _mprintf("%zAND 
43727 22 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 20 20  ", zBuf);.      
43728 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
43729 46 54 53 51 55 45 52 59 5f 4f 52 3a 0a 20 20 20  FTSQUERY_OR:.   
4372a 20 20 20 7a 42 75 66 20 3d 20 73 71 6c 69 74 65     zBuf = sqlite
4372b 33 5f 6d 70 72 69 6e 74 66 28 22 25 7a 4f 52 20  3_mprintf("%zOR 
4372c 22 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 20 20  ", zBuf);.      
4372d 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66  break;.  }..  if
4372e 28 20 7a 42 75 66 20 29 20 7a 42 75 66 20 3d 20  ( zBuf ) zBuf = 
4372f 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
43730 22 25 7a 7b 22 2c 20 7a 42 75 66 29 3b 0a 20 20  "%z{", zBuf);.  
43731 69 66 28 20 7a 42 75 66 20 29 20 7a 42 75 66 20  if( zBuf ) zBuf 
43732 3d 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70  = exprToString(p
43733 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 7a 42 75  Expr->pLeft, zBu
43734 66 29 3b 0a 20 20 69 66 28 20 7a 42 75 66 20 29  f);.  if( zBuf )
43735 20 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 33 5f   zBuf = sqlite3_
43736 6d 70 72 69 6e 74 66 28 22 25 7a 7d 20 7b 22 2c  mprintf("%z} {",
43737 20 7a 42 75 66 29 3b 0a 0a 20 20 69 66 28 20 7a   zBuf);..  if( z
43738 42 75 66 20 29 20 7a 42 75 66 20 3d 20 65 78 70  Buf ) zBuf = exp
43739 72 54 6f 53 74 72 69 6e 67 28 70 45 78 70 72 2d  rToString(pExpr-
4373a 3e 70 52 69 67 68 74 2c 20 7a 42 75 66 29 3b 0a  >pRight, zBuf);.
4373b 20 20 69 66 28 20 7a 42 75 66 20 29 20 7a 42 75    if( zBuf ) zBu
4373c 66 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  f = sqlite3_mpri
4373d 6e 74 66 28 22 25 7a 7d 22 2c 20 7a 42 75 66 29  ntf("%z}", zBuf)
4373e 3b 0a 0a 20 20 72 65 74 75 72 6e 20 7a 42 75 66  ;..  return zBuf
4373f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
43740 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  is the implement
43741 61 74 69 6f 6e 20 6f 66 20 61 20 73 63 61 6c 61  ation of a scala
43742 72 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 75  r SQL function u
43743 73 65 64 20 74 6f 20 74 65 73 74 20 74 68 65 20  sed to test the 
43744 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 70  .** expression p
43745 61 72 73 65 72 2e 20 49 74 20 73 68 6f 75 6c 64  arser. It should
43746 20 62 65 20 63 61 6c 6c 65 64 20 61 73 20 66 6f   be called as fo
43747 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 66  llows:.**.**   f
43748 74 73 33 5f 65 78 70 72 74 65 73 74 28 3c 74 6f  ts3_exprtest(<to
43749 6b 65 6e 69 7a 65 72 3e 2c 20 3c 65 78 70 72 3e  kenizer>, <expr>
4374a 2c 20 3c 63 6f 6c 75 6d 6e 20 31 3e 2c 20 2e 2e  , <column 1>, ..
4374b 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  .);.**.** The fi
4374c 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 3c 74  rst argument, <t
4374d 6f 6b 65 6e 69 7a 65 72 3e 2c 20 69 73 20 74 68  okenizer>, is th
4374e 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 74  e name of the ft
4374f 73 33 20 74 6f 6b 65 6e 69 7a 65 72 20 75 73 65  s3 tokenizer use
43750 64 0a 2a 2a 20 74 6f 20 70 61 72 73 65 20 74 68  d.** to parse th
43751 65 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  e query expressi
43752 6f 6e 20 28 73 65 65 20 52 45 41 44 4d 45 2e 74  on (see README.t
43753 6f 6b 65 6e 69 7a 65 72 73 29 2e 20 54 68 65 20  okenizers). The 
43754 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a  second argument.
43755 2a 2a 20 69 73 20 74 68 65 20 71 75 65 72 79 20  ** is the query 
43756 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 70 61  expression to pa
43757 72 73 65 2e 20 45 61 63 68 20 73 75 62 73 65 71  rse. Each subseq
43758 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 20 69 73  uent argument is
43759 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20   the name.** of 
4375a 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  a column of the 
4375b 66 74 73 33 20 74 61 62 6c 65 20 74 68 61 74 20  fts3 table that 
4375c 74 68 65 20 71 75 65 72 79 20 65 78 70 72 65 73  the query expres
4375d 73 69 6f 6e 20 6d 61 79 20 72 65 66 65 72 20 74  sion may refer t
4375e 6f 2e 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c  o..** For exampl
4375f 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43  e:.**.**   SELEC
43760 54 20 66 74 73 33 5f 65 78 70 72 74 65 73 74 28  T fts3_exprtest(
43761 27 73 69 6d 70 6c 65 27 2c 20 27 42 69 6c 6c 20  'simple', 'Bill 
43762 63 6f 6c 32 3a 42 6c 6f 67 67 73 27 2c 20 27 63  col2:Bloggs', 'c
43763 6f 6c 31 27 2c 20 27 63 6f 6c 32 27 29 3b 0a 2a  ol1', 'col2');.*
43764 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  /.static void ft
43765 73 33 45 78 70 72 54 65 73 74 28 0a 20 20 73 71  s3ExprTest(.  sq
43766 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
43767 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
43768 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
43769 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
4376a 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
4376b 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 20 2a  r_module const *
4376c 70 4d 6f 64 75 6c 65 20 3d 20 30 3b 0a 20 20 73  pModule = 0;.  s
4376d 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
4376e 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30   *pTokenizer = 0
4376f 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 68  ;.  int rc;.  ch
43770 61 72 20 2a 2a 61 7a 43 6f 6c 20 3d 20 30 3b 0a  ar **azCol = 0;.
43771 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45    const char *zE
43772 78 70 72 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72  xpr;.  int nExpr
43773 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20  ;.  int nCol;.  
43774 69 6e 74 20 69 69 3b 0a 20 20 46 74 73 33 45 78  int ii;.  Fts3Ex
43775 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 63 68 61  pr *pExpr;.  cha
43776 72 20 2a 7a 42 75 66 20 3d 20 30 3b 0a 20 20 73  r *zBuf = 0;.  s
43777 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
43778 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
43779 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
4377a 0a 0a 20 20 69 66 28 20 61 72 67 63 3c 33 20 29  ..  if( argc<3 )
4377b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
4377c 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
4377d 78 74 2c 20 0a 20 20 20 20 20 20 20 20 22 55 73  xt, .        "Us
4377e 61 67 65 3a 20 66 74 73 33 5f 65 78 70 72 74 65  age: fts3_exprte
4377f 73 74 28 74 6f 6b 65 6e 69 7a 65 72 2c 20 65 78  st(tokenizer, ex
43780 70 72 2c 20 63 6f 6c 31 2c 20 2e 2e 2e 22 2c 20  pr, col1, ...", 
43781 2d 31 0a 20 20 20 20 29 3b 0a 20 20 20 20 72 65  -1.    );.    re
43782 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  turn;.  }..  rc 
43783 3d 20 71 75 65 72 79 54 65 73 74 54 6f 6b 65 6e  = queryTestToken
43784 69 7a 65 72 28 64 62 2c 0a 20 20 20 20 20 20 20  izer(db,.       
43785 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43786 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a     (const char *
43787 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
43788 65 78 74 28 61 72 67 76 5b 30 5d 29 2c 20 26 70  ext(argv[0]), &p
43789 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 20 72  Module);.  if( r
4378a 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
4378b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
4378c 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
4378d 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  m(context);.    
4378e 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f 75  goto exprtest_ou
4378f 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21  t;.  }else if( !
43790 70 4d 6f 64 75 6c 65 20 29 7b 0a 20 20 20 20 73  pModule ){.    s
43791 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
43792 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4e 6f  ror(context, "No
43793 20 73 75 63 68 20 74 6f 6b 65 6e 69 7a 65 72 20   such tokenizer 
43794 6d 6f 64 75 6c 65 22 2c 20 2d 31 29 3b 0a 20 20  module", -1);.  
43795 20 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f    goto exprtest_
43796 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d  out;.  }..  rc =
43797 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65 61 74   pModule->xCreat
43798 65 28 30 2c 20 30 2c 20 26 70 54 6f 6b 65 6e 69  e(0, 0, &pTokeni
43799 7a 65 72 29 3b 0a 20 20 61 73 73 65 72 74 28 20  zer);.  assert( 
4379a 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
4379b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   || rc==SQLITE_O
4379c 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  K );.  if( rc==S
4379d 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
4379e 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
4379f 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f  t_error_nomem(co
437a0 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f 74 6f  ntext);.    goto
437a1 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20   exprtest_out;. 
437a2 20 7d 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72 2d   }.  pTokenizer-
437a3 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 75  >pModule = pModu
437a4 6c 65 3b 0a 0a 20 20 7a 45 78 70 72 20 3d 20 28  le;..  zExpr = (
437a5 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
437a6 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
437a7 61 72 67 76 5b 31 5d 29 3b 0a 20 20 6e 45 78 70  argv[1]);.  nExp
437a8 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  r = sqlite3_valu
437a9 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d 29  e_bytes(argv[1])
437aa 3b 0a 20 20 6e 43 6f 6c 20 3d 20 61 72 67 63 2d  ;.  nCol = argc-
437ab 32 3b 0a 20 20 61 7a 43 6f 6c 20 3d 20 28 63 68  2;.  azCol = (ch
437ac 61 72 20 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61  ar **)sqlite3_ma
437ad 6c 6c 6f 63 28 6e 43 6f 6c 2a 73 69 7a 65 6f 66  lloc(nCol*sizeof
437ae 28 63 68 61 72 20 2a 29 29 3b 0a 20 20 69 66 28  (char *));.  if(
437af 20 21 61 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 73   !azCol ){.    s
437b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
437b1 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78  ror_nomem(contex
437b2 74 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70  t);.    goto exp
437b3 72 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 0a 20  rtest_out;.  }. 
437b4 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43   for(ii=0; ii<nC
437b5 6f 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 61  ol; ii++){.    a
437b6 7a 43 6f 6c 5b 69 69 5d 20 3d 20 28 63 68 61 72  zCol[ii] = (char
437b7 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
437b8 5f 74 65 78 74 28 61 72 67 76 5b 69 69 2b 32 5d  _text(argv[ii+2]
437b9 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73  );.  }..  rc = s
437ba 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 50 61  qlite3Fts3ExprPa
437bb 72 73 65 28 0a 20 20 20 20 20 20 70 54 6f 6b 65  rse(.      pToke
437bc 6e 69 7a 65 72 2c 20 30 2c 20 61 7a 43 6f 6c 2c  nizer, 0, azCol,
437bd 20 30 2c 20 6e 43 6f 6c 2c 20 6e 43 6f 6c 2c 20   0, nCol, nCol, 
437be 7a 45 78 70 72 2c 20 6e 45 78 70 72 2c 20 26 70  zExpr, nExpr, &p
437bf 45 78 70 72 0a 20 20 29 3b 0a 20 20 69 66 28 20  Expr.  );.  if( 
437c0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc!=SQLITE_OK &&
437c1 20 72 63 21 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc!=SQLITE_NOME
437c2 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  M ){.    sqlite3
437c3 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
437c4 6e 74 65 78 74 2c 20 22 45 72 72 6f 72 20 70 61  ntext, "Error pa
437c5 72 73 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e  rsing expression
437c6 22 2c 20 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 20  ", -1);.  }else 
437c7 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
437c8 4f 4d 45 4d 20 7c 7c 20 21 28 7a 42 75 66 20 3d  OMEM || !(zBuf =
437c9 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70 45   exprToString(pE
437ca 78 70 72 2c 20 30 29 29 20 29 7b 0a 20 20 20 20  xpr, 0)) ){.    
437cb 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
437cc 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65  rror_nomem(conte
437cd 78 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  xt);.  }else{.  
437ce 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
437cf 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a  _text(context, z
437d0 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  Buf, -1, SQLITE_
437d1 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20  TRANSIENT);.    
437d2 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 42 75  sqlite3_free(zBu
437d3 66 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74  f);.  }..  sqlit
437d4 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70  e3Fts3ExprFree(p
437d5 45 78 70 72 29 3b 0a 0a 65 78 70 72 74 65 73 74  Expr);..exprtest
437d6 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 4d 6f 64  _out:.  if( pMod
437d7 75 6c 65 20 26 26 20 70 54 6f 6b 65 6e 69 7a 65  ule && pTokenize
437d8 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 4d  r ){.    rc = pM
437d9 6f 64 75 6c 65 2d 3e 78 44 65 73 74 72 6f 79 28  odule->xDestroy(
437da 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 7d  pTokenizer);.  }
437db 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
437dc 61 7a 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  azCol);.}../*.**
437dd 20 52 65 67 69 73 74 65 72 20 74 68 65 20 71 75   Register the qu
437de 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 70  ery expression p
437df 61 72 73 65 72 20 74 65 73 74 20 66 75 6e 63 74  arser test funct
437e0 69 6f 6e 20 66 74 73 33 5f 65 78 70 72 74 65 73  ion fts3_exprtes
437e1 74 28 29 20 0a 2a 2a 20 77 69 74 68 20 64 61 74  t() .** with dat
437e2 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
437e3 20 64 62 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f   db. .*/.SQLITE_
437e4 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
437e5 74 65 33 46 74 73 33 45 78 70 72 49 6e 69 74 54  te3Fts3ExprInitT
437e6 65 73 74 49 6e 74 65 72 66 61 63 65 28 73 71 6c  estInterface(sql
437e7 69 74 65 33 2a 20 64 62 29 7b 0a 20 20 72 65 74  ite3* db){.  ret
437e8 75 72 6e 20 73 71 6c 69 74 65 33 5f 63 72 65 61  urn sqlite3_crea
437e9 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 20  te_function(.   
437ea 20 20 20 64 62 2c 20 22 66 74 73 33 5f 65 78 70     db, "fts3_exp
437eb 72 74 65 73 74 22 2c 20 2d 31 2c 20 53 51 4c 49  rtest", -1, SQLI
437ec 54 45 5f 55 54 46 38 2c 20 30 2c 20 66 74 73 33  TE_UTF8, 0, fts3
437ed 45 78 70 72 54 65 73 74 2c 20 30 2c 20 30 0a 20  ExprTest, 0, 0. 
437ee 20 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 0a 23 65   );.}..#endif.#e
437ef 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
437f0 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
437f1 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
437f2 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a  ENABLE_FTS3) */.
437f3 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
437f4 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 65 78 70   End of fts3_exp
437f5 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
437f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
437f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
437f8 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
437f9 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
437fa 5f 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  _hash.c ********
437fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
437fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
437fd 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
437fe 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54  ember 22.**.** T
437ff 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
43800 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
43801 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
43802 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
43803 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
43804 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
43805 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
43806 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
43807 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
43808 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
43809 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
4380a 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
4380b 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
4380c 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
4380d 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
4380e 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
4380f 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
43810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43811 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43812 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43813 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43814 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
43815 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  is the implement
43816 61 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 69 63  ation of generic
43817 20 68 61 73 68 2d 74 61 62 6c 65 73 20 75 73 65   hash-tables use
43818 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 20  d in SQLite..** 
43819 57 65 27 76 65 20 6d 6f 64 69 66 69 65 64 20 69  We've modified i
4381a 74 20 73 6c 69 67 68 74 6c 79 20 74 6f 20 73 65  t slightly to se
4381b 72 76 65 20 61 73 20 61 20 73 74 61 6e 64 61 6c  rve as a standal
4381c 6f 6e 65 20 68 61 73 68 20 74 61 62 6c 65 0a 2a  one hash table.*
4381d 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
4381e 20 66 6f 72 20 74 68 65 20 66 75 6c 6c 2d 74 65   for the full-te
4381f 78 74 20 69 6e 64 65 78 69 6e 67 20 6d 6f 64 75  xt indexing modu
43820 6c 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  le..*/../*.** Th
43821 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
43822 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70  ile is only comp
43823 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20  iled if:.**.**  
43824 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f     * The FTS3 mo
43825 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75  dule is being bu
43826 69 6c 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73  ilt as an extens
43827 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e  ion.**       (in
43828 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49   which case SQLI
43829 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64  TE_CORE is not d
4382a 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a  efined), or.**.*
4382b 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
4382c 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
4382d 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20   built into the 
4382e 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20  core of.**      
4382f 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68 69 63   SQLite (in whic
43830 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4e  h case SQLITE_EN
43831 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64 65 66  ABLE_FTS3 is def
43832 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64  ined)..*/.#if !d
43833 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
43834 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
43835 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
43836 33 29 0a 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20  3)../* #include 
43837 3c 61 73 73 65 72 74 2e 68 3e 20 2a 2f 0a 2f 2a  <assert.h> */./*
43838 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69   #include <stdli
43839 62 2e 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e 63 6c  b.h> */./* #incl
4383a 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 20 2a  ude <string.h> *
4383b 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63  /.../*.** Malloc
4383c 20 61 6e 64 20 46 72 65 65 20 66 75 6e 63 74 69   and Free functi
4383d 6f 6e 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ons.*/.static vo
4383e 69 64 20 2a 66 74 73 33 48 61 73 68 4d 61 6c 6c  id *fts3HashMall
4383f 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69  oc(int n){.  voi
43840 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d  d *p = sqlite3_m
43841 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20  alloc(n);.  if( 
43842 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  p ){.    memset(
43843 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20  p, 0, n);.  }.  
43844 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 73 74 61 74  return p;.}.stat
43845 69 63 20 76 6f 69 64 20 66 74 73 33 48 61 73 68  ic void fts3Hash
43846 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20  Free(void *p){. 
43847 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
43848 3b 0a 7d 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c  ;.}../* Turn bul
43849 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20  k memory into a 
4384a 68 61 73 68 20 74 61 62 6c 65 20 6f 62 6a 65 63  hash table objec
4384b 74 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e  t by initializin
4384c 67 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20  g the.** fields 
4384d 6f 66 20 74 68 65 20 48 61 73 68 20 73 74 72 75  of the Hash stru
4384e 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e  cture..**.** "pN
4384f 65 77 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ew" is a pointer
43850 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62   to the hash tab
43851 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65  le that is to be
43852 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a   initialized..**
43853 20 6b 65 79 43 6c 61 73 73 20 69 73 20 6f 6e 65   keyClass is one
43854 20 6f 66 20 74 68 65 20 63 6f 6e 73 74 61 6e 74   of the constant
43855 73 20 0a 2a 2a 20 46 54 53 33 5f 48 41 53 48 5f  s .** FTS3_HASH_
43856 42 49 4e 41 52 59 20 6f 72 20 46 54 53 33 5f 48  BINARY or FTS3_H
43857 41 53 48 5f 53 54 52 49 4e 47 2e 20 20 54 68 65  ASH_STRING.  The
43858 20 76 61 6c 75 65 20 6f 66 20 6b 65 79 43 6c 61   value of keyCla
43859 73 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65  ss .** determine
4385a 73 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6b  s what kind of k
4385b 65 79 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  ey the hash tabl
4385c 65 20 77 69 6c 6c 20 75 73 65 2e 20 20 22 63 6f  e will use.  "co
4385d 70 79 4b 65 79 22 20 69 73 0a 2a 2a 20 74 72 75  pyKey" is.** tru
4385e 65 20 69 66 20 74 68 65 20 68 61 73 68 20 74 61  e if the hash ta
4385f 62 6c 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20  ble should make 
43860 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20  its own private 
43861 63 6f 70 79 20 6f 66 20 6b 65 79 73 20 61 6e 64  copy of keys and
43862 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20  .** false if it 
43863 73 68 6f 75 6c 64 20 6a 75 73 74 20 75 73 65 20  should just use 
43864 74 68 65 20 73 75 70 70 6c 69 65 64 20 70 6f 69  the supplied poi
43865 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nter..*/.SQLITE_
43866 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
43867 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74  ite3Fts3HashInit
43868 28 46 74 73 33 48 61 73 68 20 2a 70 4e 65 77 2c  (Fts3Hash *pNew,
43869 20 63 68 61 72 20 6b 65 79 43 6c 61 73 73 2c 20   char keyClass, 
4386a 63 68 61 72 20 63 6f 70 79 4b 65 79 29 7b 0a 20  char copyKey){. 
4386b 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30   assert( pNew!=0
4386c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6b 65   );.  assert( ke
4386d 79 43 6c 61 73 73 3e 3d 46 54 53 33 5f 48 41 53  yClass>=FTS3_HAS
4386e 48 5f 53 54 52 49 4e 47 20 26 26 20 6b 65 79 43  H_STRING && keyC
4386f 6c 61 73 73 3c 3d 46 54 53 33 5f 48 41 53 48 5f  lass<=FTS3_HASH_
43870 42 49 4e 41 52 59 20 29 3b 0a 20 20 70 4e 65 77  BINARY );.  pNew
43871 2d 3e 6b 65 79 43 6c 61 73 73 20 3d 20 6b 65 79  ->keyClass = key
43872 43 6c 61 73 73 3b 0a 20 20 70 4e 65 77 2d 3e 63  Class;.  pNew->c
43873 6f 70 79 4b 65 79 20 3d 20 63 6f 70 79 4b 65 79  opyKey = copyKey
43874 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74 20  ;.  pNew->first 
43875 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 75  = 0;.  pNew->cou
43876 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  nt = 0;.  pNew->
43877 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70 4e  htsize = 0;.  pN
43878 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f  ew->ht = 0;.}../
43879 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74  * Remove all ent
4387a 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73 68  ries from a hash
4387b 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d   table.  Reclaim
4387c 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20   all memory..** 
4387d 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  Call this routin
4387e 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68 61  e to delete a ha
4387f 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 72  sh table or to r
43880 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62 6c  eset a hash tabl
43881 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70 74  e.** to the empt
43882 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49  y state..*/.SQLI
43883 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
43884 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43  sqlite3Fts3HashC
43885 6c 65 61 72 28 46 74 73 33 48 61 73 68 20 2a 70  lear(Fts3Hash *p
43886 48 29 7b 0a 20 20 46 74 73 33 48 61 73 68 45 6c  H){.  Fts3HashEl
43887 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20  em *elem;       
43888 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67    /* For looping
43889 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e   over all elemen
4388a 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ts of the table 
4388b 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48  */..  assert( pH
4388c 21 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20 3d 20  !=0 );.  elem = 
4388d 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 70 48 2d  pH->first;.  pH-
4388e 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 66 74  >first = 0;.  ft
4388f 73 33 48 61 73 68 46 72 65 65 28 70 48 2d 3e 68  s3HashFree(pH->h
43890 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 30  t);.  pH->ht = 0
43891 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d  ;.  pH->htsize =
43892 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 65 6c 65   0;.  while( ele
43893 6d 20 29 7b 0a 20 20 20 20 46 74 73 33 48 61 73  m ){.    Fts3Has
43894 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d  hElem *next_elem
43895 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20   = elem->next;. 
43896 20 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b     if( pH->copyK
43897 65 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b 65 79  ey && elem->pKey
43898 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33 48 61   ){.      fts3Ha
43899 73 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65  shFree(elem->pKe
4389a 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 74  y);.    }.    ft
4389b 73 33 48 61 73 68 46 72 65 65 28 65 6c 65 6d 29  s3HashFree(elem)
4389c 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 6e 65 78  ;.    elem = nex
4389d 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 20 70 48  t_elem;.  }.  pH
4389e 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 7d 0a 0a  ->count = 0;.}..
4389f 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20 63  /*.** Hash and c
438a0 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
438a1 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 6f 64  ons when the mod
438a2 65 20 69 73 20 46 54 53 33 5f 48 41 53 48 5f 53  e is FTS3_HASH_S
438a3 54 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69 63 20  TRING.*/.static 
438a4 69 6e 74 20 66 74 73 33 53 74 72 48 61 73 68 28  int fts3StrHash(
438a5 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
438a6 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 63  , int nKey){.  c
438a7 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28  onst char *z = (
438a8 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65  const char *)pKe
438a9 79 3b 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a  y;.  int h = 0;.
438aa 20 20 69 66 28 20 6e 4b 65 79 3c 3d 30 20 29 20    if( nKey<=0 ) 
438ab 6e 4b 65 79 20 3d 20 28 69 6e 74 29 20 73 74 72  nKey = (int) str
438ac 6c 65 6e 28 7a 29 3b 0a 20 20 77 68 69 6c 65 28  len(z);.  while(
438ad 20 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0a 20 20   nKey > 0  ){.  
438ae 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20 68    h = (h<<3) ^ h
438af 20 5e 20 2a 7a 2b 2b 3b 0a 20 20 20 20 6e 4b 65   ^ *z++;.    nKe
438b0 79 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  y--;.  }.  retur
438b1 6e 20 68 20 26 20 30 78 37 66 66 66 66 66 66 66  n h & 0x7fffffff
438b2 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 66  ;.}.static int f
438b3 74 73 33 53 74 72 43 6f 6d 70 61 72 65 28 63 6f  ts3StrCompare(co
438b4 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c  nst void *pKey1,
438b5 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73 74 20 76   int n1, const v
438b6 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e 74 20  oid *pKey2, int 
438b7 6e 32 29 7b 0a 20 20 69 66 28 20 6e 31 21 3d 6e  n2){.  if( n1!=n
438b8 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  2 ) return 1;.  
438b9 72 65 74 75 72 6e 20 73 74 72 6e 63 6d 70 28 28  return strncmp((
438ba 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65 79  const char*)pKey
438bb 31 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70  1,(const char*)p
438bc 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a  Key2,n1);.}../*.
438bd 2a 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70  ** Hash and comp
438be 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73  arison functions
438bf 20 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69   when the mode i
438c0 73 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41  s FTS3_HASH_BINA
438c1 52 59 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  RY.*/.static int
438c2 20 66 74 73 33 42 69 6e 48 61 73 68 28 63 6f 6e   fts3BinHash(con
438c3 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
438c4 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20  nt nKey){.  int 
438c5 68 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63  h = 0;.  const c
438c6 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20  har *z = (const 
438c7 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20 77  char *)pKey;.  w
438c8 68 69 6c 65 28 20 6e 4b 65 79 2d 2d 20 3e 20 30  hile( nKey-- > 0
438c9 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c   ){.    h = (h<<
438ca 33 29 20 5e 20 68 20 5e 20 2a 28 7a 2b 2b 29 3b  3) ^ h ^ *(z++);
438cb 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 20  .  }.  return h 
438cc 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a  & 0x7fffffff;.}.
438cd 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 42  static int fts3B
438ce 69 6e 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20  inCompare(const 
438cf 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 69 6e 74  void *pKey1, int
438d0 20 6e 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20   n1, const void 
438d1 2a 70 4b 65 79 32 2c 20 69 6e 74 20 6e 32 29 7b  *pKey2, int n2){
438d2 0a 20 20 69 66 28 20 6e 31 21 3d 6e 32 20 29 20  .  if( n1!=n2 ) 
438d3 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75  return 1;.  retu
438d4 72 6e 20 6d 65 6d 63 6d 70 28 70 4b 65 79 31 2c  rn memcmp(pKey1,
438d5 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a  pKey2,n1);.}../*
438d6 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
438d7 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70 72  nter to the appr
438d8 6f 70 72 69 61 74 65 20 68 61 73 68 20 66 75 6e  opriate hash fun
438d9 63 74 69 6f 6e 20 67 69 76 65 6e 20 74 68 65 20  ction given the 
438da 6b 65 79 20 63 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a  key class..**.**
438db 20 54 68 65 20 43 20 73 79 6e 74 61 78 20 69 6e   The C syntax in
438dc 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64   this function d
438dd 65 66 69 6e 69 74 69 6f 6e 20 6d 61 79 20 62 65  efinition may be
438de 20 75 6e 66 61 6d 69 6c 61 72 20 74 6f 20 73 6f   unfamilar to so
438df 6d 65 20 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d 65  me .** programme
438e0 72 73 2c 20 73 6f 20 77 65 20 70 72 6f 76 69 64  rs, so we provid
438e1 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
438e2 61 64 64 69 74 69 6f 6e 61 6c 20 65 78 70 6c 61  additional expla
438e3 6e 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68  nation:.**.** Th
438e4 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75  e name of the fu
438e5 6e 63 74 69 6f 6e 20 69 73 20 22 66 74 73 48 61  nction is "ftsHa
438e6 73 68 46 75 6e 63 74 69 6f 6e 22 2e 20 20 54 68  shFunction".  Th
438e7 65 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73  e function takes
438e8 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 70 61 72   a.** single par
438e9 61 6d 65 74 65 72 20 22 6b 65 79 43 6c 61 73 73  ameter "keyClass
438ea 22 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76  ".  The return v
438eb 61 6c 75 65 20 6f 66 20 66 74 73 48 61 73 68 46  alue of ftsHashF
438ec 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 20 69 73 20  unction().** is 
438ed 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 6f  a pointer to ano
438ee 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 20  ther function.  
438ef 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 68  Specifically, th
438f0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a  e return value.*
438f1 2a 20 6f 66 20 66 74 73 48 61 73 68 46 75 6e 63  * of ftsHashFunc
438f2 74 69 6f 6e 28 29 20 69 73 20 61 20 70 6f 69 6e  tion() is a poin
438f3 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ter to a functio
438f4 6e 20 74 68 61 74 20 74 61 6b 65 73 20 74 77 6f  n that takes two
438f5 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 77   parameters.** w
438f6 69 74 68 20 74 79 70 65 73 20 22 63 6f 6e 73 74  ith types "const
438f7 20 76 6f 69 64 2a 22 20 61 6e 64 20 22 69 6e 74   void*" and "int
438f8 22 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e  " and returns an
438f9 20 22 69 6e 74 22 2e 0a 2a 2f 0a 73 74 61 74 69   "int"..*/.stati
438fa 63 20 69 6e 74 20 28 2a 66 74 73 48 61 73 68 46  c int (*ftsHashF
438fb 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b 65 79 43  unction(int keyC
438fc 6c 61 73 73 29 29 28 63 6f 6e 73 74 20 76 6f 69  lass))(const voi
438fd 64 2a 2c 69 6e 74 29 7b 0a 20 20 69 66 28 20 6b  d*,int){.  if( k
438fe 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48 41  eyClass==FTS3_HA
438ff 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20 20  SH_STRING ){.   
43900 20 72 65 74 75 72 6e 20 26 66 74 73 33 53 74 72   return &fts3Str
43901 48 61 73 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Hash;.  }else{. 
43902 20 20 20 61 73 73 65 72 74 28 20 6b 65 79 43 6c     assert( keyCl
43903 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f 42  ass==FTS3_HASH_B
43904 49 4e 41 52 59 20 29 3b 0a 20 20 20 20 72 65 74  INARY );.    ret
43905 75 72 6e 20 26 66 74 73 33 42 69 6e 48 61 73 68  urn &fts3BinHash
43906 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
43907 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
43908 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  to the appropria
43909 74 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e  te hash function
4390a 20 67 69 76 65 6e 20 74 68 65 20 6b 65 79 20 63   given the key c
4390b 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  lass..**.** For 
4390c 68 65 6c 70 20 69 6e 20 69 6e 74 65 72 70 72 65  help in interpre
4390d 74 65 64 20 74 68 65 20 6f 62 73 63 75 72 65 20  ted the obscure 
4390e 43 20 63 6f 64 65 20 69 6e 20 74 68 65 20 66 75  C code in the fu
4390f 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
43910 6e 2c 0a 2a 2a 20 73 65 65 20 74 68 65 20 68 65  n,.** see the he
43911 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20  ader comment on 
43912 74 68 65 20 70 72 65 76 69 6f 75 73 20 66 75 6e  the previous fun
43913 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
43914 20 69 6e 74 20 28 2a 66 74 73 43 6f 6d 70 61 72   int (*ftsCompar
43915 65 46 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b 65  eFunction(int ke
43916 79 43 6c 61 73 73 29 29 28 63 6f 6e 73 74 20 76  yClass))(const v
43917 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
43918 6f 69 64 2a 2c 69 6e 74 29 7b 0a 20 20 69 66 28  oid*,int){.  if(
43919 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f   keyClass==FTS3_
4391a 48 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20  HASH_STRING ){. 
4391b 20 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 53     return &fts3S
4391c 74 72 43 6f 6d 70 61 72 65 3b 0a 20 20 7d 65 6c  trCompare;.  }el
4391d 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
4391e 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48  keyClass==FTS3_H
4391f 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a 20 20  ASH_BINARY );.  
43920 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 42 69    return &fts3Bi
43921 6e 43 6f 6d 70 61 72 65 3b 0a 20 20 7d 0a 7d 0a  nCompare;.  }.}.
43922 0a 2f 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c 65 6d  ./* Link an elem
43923 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73  ent into the has
43924 68 20 74 61 62 6c 65 0a 2a 2f 0a 73 74 61 74 69  h table.*/.stati
43925 63 20 76 6f 69 64 20 66 74 73 33 48 61 73 68 49  c void fts3HashI
43926 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 0a 20 20  nsertElement(.  
43927 46 74 73 33 48 61 73 68 20 2a 70 48 2c 20 20 20  Fts3Hash *pH,   
43928 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
43929 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61  complete hash ta
4392a 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ble */.  struct 
4392b 5f 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79 2c  _fts3ht *pEntry,
4392c 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20 69    /* The entry i
4392d 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20 69  nto which pNew i
4392e 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  s inserted */.  
4392f 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 4e  Fts3HashElem *pN
43930 65 77 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ew       /* The 
43931 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e  element to be in
43932 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 46  serted */.){.  F
43933 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 48 65  ts3HashElem *pHe
43934 61 64 3b 20 20 20 20 20 2f 2a 20 46 69 72 73 74  ad;     /* First
43935 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79   element already
43936 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20   in pEntry */.  
43937 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e  pHead = pEntry->
43938 63 68 61 69 6e 3b 0a 20 20 69 66 28 20 70 48 65  chain;.  if( pHe
43939 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e  ad ){.    pNew->
4393a 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a 20 20  next = pHead;.  
4393b 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70    pNew->prev = p
4393c 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20  Head->prev;.    
4393d 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65 76 20  if( pHead->prev 
4393e 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e  ){ pHead->prev->
4393f 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20  next = pNew; }. 
43940 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20     else         
43941 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 74 20      { pH->first 
43942 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48  = pNew; }.    pH
43943 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77  ead->prev = pNew
43944 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
43945 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e  New->next = pH->
43946 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28 20 70  first;.    if( p
43947 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 2d 3e  H->first ){ pH->
43948 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e  first->prev = pN
43949 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e  ew; }.    pNew->
4394a 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 48  prev = 0;.    pH
4394b 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a  ->first = pNew;.
4394c 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f    }.  pEntry->co
4394d 75 6e 74 2b 2b 3b 0a 20 20 70 45 6e 74 72 79 2d  unt++;.  pEntry-
4394e 3e 63 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a 7d  >chain = pNew;.}
4394f 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68 65  .../* Resize the
43950 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20 74   hash table so t
43951 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73 20  hat it cantains 
43952 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b 65  "new_size" bucke
43953 74 73 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69 7a 65  ts..** "new_size
43954 22 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65  " must be a powe
43955 72 20 6f 66 20 32 2e 20 20 54 68 65 20 68 61 73  r of 2.  The has
43956 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20 66 61  h table might fa
43957 69 6c 20 0a 2a 2a 20 74 6f 20 72 65 73 69 7a 65  il .** to resize
43958 20 69 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63   if sqliteMalloc
43959 28 29 20 66 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20  () fails..**.** 
4395a 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  Return non-zero 
4395b 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  if a memory allo
4395c 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63  cation error occ
4395d 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  urs..*/.static i
4395e 6e 74 20 66 74 73 33 52 65 68 61 73 68 28 46 74  nt fts3Rehash(Ft
4395f 73 33 48 61 73 68 20 2a 70 48 2c 20 69 6e 74 20  s3Hash *pH, int 
43960 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72  new_size){.  str
43961 75 63 74 20 5f 66 74 73 33 68 74 20 2a 6e 65 77  uct _fts3ht *new
43962 5f 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  _ht;          /*
43963 20 54 68 65 20 6e 65 77 20 68 61 73 68 20 74 61   The new hash ta
43964 62 6c 65 20 2a 2f 0a 20 20 46 74 73 33 48 61 73  ble */.  Fts3Has
43965 68 45 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65  hElem *elem, *ne
43966 78 74 5f 65 6c 65 6d 3b 20 20 2f 2a 20 46 6f 72  xt_elem;  /* For
43967 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78   looping over ex
43968 69 73 74 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20  isting elements 
43969 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48 61 73 68  */.  int (*xHash
4396a 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  )(const void*,in
4396b 74 29 3b 20 20 20 2f 2a 20 54 68 65 20 68 61 73  t);   /* The has
4396c 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20  h function */.. 
4396d 20 61 73 73 65 72 74 28 20 28 6e 65 77 5f 73 69   assert( (new_si
4396e 7a 65 20 26 20 28 6e 65 77 5f 73 69 7a 65 2d 31  ze & (new_size-1
4396f 29 29 3d 3d 30 20 29 3b 0a 20 20 6e 65 77 5f 68  ))==0 );.  new_h
43970 74 20 3d 20 28 73 74 72 75 63 74 20 5f 66 74 73  t = (struct _fts
43971 33 68 74 20 2a 29 66 74 73 33 48 61 73 68 4d 61  3ht *)fts3HashMa
43972 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65 2a 73  lloc( new_size*s
43973 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 66 74  izeof(struct _ft
43974 73 33 68 74 29 20 29 3b 0a 20 20 69 66 28 20 6e  s3ht) );.  if( n
43975 65 77 5f 68 74 3d 3d 30 20 29 20 72 65 74 75 72  ew_ht==0 ) retur
43976 6e 20 31 3b 0a 20 20 66 74 73 33 48 61 73 68 46  n 1;.  fts3HashF
43977 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70  ree(pH->ht);.  p
43978 48 2d 3e 68 74 20 3d 20 6e 65 77 5f 68 74 3b 0a  H->ht = new_ht;.
43979 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 6e    pH->htsize = n
4397a 65 77 5f 73 69 7a 65 3b 0a 20 20 78 48 61 73 68  ew_size;.  xHash
4397b 20 3d 20 66 74 73 48 61 73 68 46 75 6e 63 74 69   = ftsHashFuncti
4397c 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29  on(pH->keyClass)
4397d 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48 2d  ;.  for(elem=pH-
4397e 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69 72 73  >first, pH->firs
4397f 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20  t=0; elem; elem 
43980 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 20  = next_elem){.  
43981 20 20 69 6e 74 20 68 20 3d 20 28 2a 78 48 61 73    int h = (*xHas
43982 68 29 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65  h)(elem->pKey, e
43983 6c 65 6d 2d 3e 6e 4b 65 79 29 20 26 20 28 6e 65  lem->nKey) & (ne
43984 77 5f 73 69 7a 65 2d 31 29 3b 0a 20 20 20 20 6e  w_size-1);.    n
43985 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d  ext_elem = elem-
43986 3e 6e 65 78 74 3b 0a 20 20 20 20 66 74 73 33 48  >next;.    fts3H
43987 61 73 68 49 6e 73 65 72 74 45 6c 65 6d 65 6e 74  ashInsertElement
43988 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c  (pH, &new_ht[h],
43989 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65   elem);.  }.  re
4398a 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 54 68  turn 0;.}../* Th
4398b 69 73 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72  is function (for
4398c 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e   internal use on
4398d 6c 79 29 20 6c 6f 63 61 74 65 73 20 61 6e 20 65  ly) locates an e
4398e 6c 65 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20  lement in an.** 
4398f 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20  hash table that 
43990 6d 61 74 63 68 65 73 20 74 68 65 20 67 69 76 65  matches the give
43991 6e 20 6b 65 79 2e 20 20 54 68 65 20 68 61 73 68  n key.  The hash
43992 20 66 6f 72 20 74 68 69 73 20 6b 65 79 20 68 61   for this key ha
43993 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65  s.** already bee
43994 6e 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 69  n computed and i
43995 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  s passed as the 
43996 34 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  4th parameter..*
43997 2f 0a 73 74 61 74 69 63 20 46 74 73 33 48 61 73  /.static Fts3Has
43998 68 45 6c 65 6d 20 2a 66 74 73 33 46 69 6e 64 45  hElem *fts3FindE
43999 6c 65 6d 65 6e 74 42 79 48 61 73 68 28 0a 20 20  lementByHash(.  
4399a 63 6f 6e 73 74 20 46 74 73 33 48 61 73 68 20 2a  const Fts3Hash *
4399b 70 48 2c 20 2f 2a 20 54 68 65 20 70 48 20 74 6f  pH, /* The pH to
4399c 20 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a   be searched */.
4399d 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b    const void *pK
4399e 65 79 2c 20 20 20 2f 2a 20 54 68 65 20 6b 65 79  ey,   /* The key
4399f 20 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e   we are searchin
439a0 67 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  g for */.  int n
439a1 4b 65 79 2c 0a 20 20 69 6e 74 20 68 20 20 20 20  Key,.  int h    
439a2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
439a3 65 20 68 61 73 68 20 66 6f 72 20 74 68 69 73 20  e hash for this 
439a4 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 20 46 74 73  key. */.){.  Fts
439a5 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b  3HashElem *elem;
439a6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
439a7 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75  sed to loop thru
439a8 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73   the element lis
439a9 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74  t */.  int count
439aa 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
439ab 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
439ac 6f 66 20 65 6c 65 6d 65 6e 74 73 20 6c 65 66 74  of elements left
439ad 20 74 6f 20 74 65 73 74 20 2a 2f 0a 20 20 69 6e   to test */.  in
439ae 74 20 28 2a 78 43 6f 6d 70 61 72 65 29 28 63 6f  t (*xCompare)(co
439af 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
439b0 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20  nst void*,int); 
439b1 20 2f 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66   /* comparison f
439b2 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66  unction */..  if
439b3 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20  ( pH->ht ){.    
439b4 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a  struct _fts3ht *
439b5 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74  pEntry = &pH->ht
439b6 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20  [h];.    elem = 
439b7 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20  pEntry->chain;. 
439b8 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72     count = pEntr
439b9 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 20 20 78 43  y->count;.    xC
439ba 6f 6d 70 61 72 65 20 3d 20 66 74 73 43 6f 6d 70  ompare = ftsComp
439bb 61 72 65 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e  areFunction(pH->
439bc 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20 20 20 77  keyClass);.    w
439bd 68 69 6c 65 28 20 63 6f 75 6e 74 2d 2d 20 26 26  hile( count-- &&
439be 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 20 20 69   elem ){.      i
439bf 66 28 20 28 2a 78 43 6f 6d 70 61 72 65 29 28 65  f( (*xCompare)(e
439c0 6c 65 6d 2d 3e 70 4b 65 79 2c 65 6c 65 6d 2d 3e  lem->pKey,elem->
439c1 6e 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d  nKey,pKey,nKey)=
439c2 3d 30 20 29 7b 20 0a 20 20 20 20 20 20 20 20 72  =0 ){ .        r
439c3 65 74 75 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20  eturn elem;.    
439c4 20 20 7d 0a 20 20 20 20 20 20 65 6c 65 6d 20 3d    }.      elem =
439c5 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20   elem->next;.   
439c6 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
439c7 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20  0;.}../* Remove 
439c8 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66  a single entry f
439c9 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62  rom the hash tab
439ca 6c 65 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74  le given a point
439cb 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c  er to that.** el
439cc 65 6d 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68  ement and a hash
439cd 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27   on the element'
439ce 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  s key..*/.static
439cf 20 76 6f 69 64 20 66 74 73 33 52 65 6d 6f 76 65   void fts3Remove
439d0 45 6c 65 6d 65 6e 74 42 79 48 61 73 68 28 0a 20  ElementByHash(. 
439d1 20 46 74 73 33 48 61 73 68 20 2a 70 48 2c 20 20   Fts3Hash *pH,  
439d2 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 48         /* The pH
439d3 20 63 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65   containing "ele
439d4 6d 22 20 2a 2f 0a 20 20 46 74 73 33 48 61 73 68  m" */.  Fts3Hash
439d5 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20 20 20 2f 2a  Elem* elem,   /*
439d6 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20   The element to 
439d7 62 65 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20  be removed from 
439d8 74 68 65 20 70 48 20 2a 2f 0a 20 20 69 6e 74 20  the pH */.  int 
439d9 68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h               
439da 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20    /* Hash value 
439db 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e 74 20  for the element 
439dc 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 5f  */.){.  struct _
439dd 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79 3b 0a  fts3ht *pEntry;.
439de 20 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76    if( elem->prev
439df 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72   ){.    elem->pr
439e0 65 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d  ev->next = elem-
439e1 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b  >next; .  }else{
439e2 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d  .    pH->first =
439e3 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d   elem->next;.  }
439e4 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78  .  if( elem->nex
439e5 74 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e  t ){.    elem->n
439e6 65 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d  ext->prev = elem
439e7 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20 70 45  ->prev;.  }.  pE
439e8 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68  ntry = &pH->ht[h
439e9 5d 3b 0a 20 20 69 66 28 20 70 45 6e 74 72 79 2d  ];.  if( pEntry-
439ea 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 7b 0a  >chain==elem ){.
439eb 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69      pEntry->chai
439ec 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  n = elem->next;.
439ed 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f    }.  pEntry->co
439ee 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 45 6e  unt--;.  if( pEn
439ef 74 72 79 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b  try->count<=0 ){
439f0 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61  .    pEntry->cha
439f1 69 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  in = 0;.  }.  if
439f2 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26  ( pH->copyKey &&
439f3 20 65 6c 65 6d 2d 3e 70 4b 65 79 20 29 7b 0a 20   elem->pKey ){. 
439f4 20 20 20 66 74 73 33 48 61 73 68 46 72 65 65 28     fts3HashFree(
439f5 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 7d  elem->pKey);.  }
439f6 0a 20 20 66 74 73 33 48 61 73 68 46 72 65 65 28  .  fts3HashFree(
439f7 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e 63   elem );.  pH->c
439f8 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 48  ount--;.  if( pH
439f9 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20  ->count<=0 ){.  
439fa 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 66 69    assert( pH->fi
439fb 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73  rst==0 );.    as
439fc 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74 3d  sert( pH->count=
439fd 3d 30 20 29 3b 0a 20 20 20 20 66 74 73 33 48 61  =0 );.    fts3Ha
439fe 73 68 43 6c 65 61 72 28 70 48 29 3b 0a 20 20 7d  shClear(pH);.  }
439ff 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
43a00 54 45 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20  TE Fts3HashElem 
43a01 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68  *sqlite3Fts3Hash
43a02 46 69 6e 64 45 6c 65 6d 28 0a 20 20 63 6f 6e 73  FindElem(.  cons
43a03 74 20 46 74 73 33 48 61 73 68 20 2a 70 48 2c 20  t Fts3Hash *pH, 
43a04 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  .  const void *p
43a05 4b 65 79 2c 20 0a 20 20 69 6e 74 20 6e 4b 65 79  Key, .  int nKey
43a06 0a 29 7b 0a 20 20 69 6e 74 20 68 3b 20 20 20 20  .){.  int h;    
43a07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43a08 20 20 20 20 20 20 2f 2a 20 41 20 68 61 73 68 20        /* A hash 
43a09 6f 6e 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  on key */.  int 
43a0a 28 2a 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76  (*xHash)(const v
43a0b 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54  oid*,int);  /* T
43a0c 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e  he hash function
43a0d 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 3d 3d 30   */..  if( pH==0
43a0e 20 7c 7c 20 70 48 2d 3e 68 74 3d 3d 30 20 29 20   || pH->ht==0 ) 
43a0f 72 65 74 75 72 6e 20 30 3b 0a 20 20 78 48 61 73  return 0;.  xHas
43a10 68 20 3d 20 66 74 73 48 61 73 68 46 75 6e 63 74  h = ftsHashFunct
43a11 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73  ion(pH->keyClass
43a12 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78 48 61  );.  assert( xHa
43a13 73 68 21 3d 30 20 29 3b 0a 20 20 68 20 3d 20 28  sh!=0 );.  h = (
43a14 2a 78 48 61 73 68 29 28 70 4b 65 79 2c 6e 4b 65  *xHash)(pKey,nKe
43a15 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70  y);.  assert( (p
43a16 48 2d 3e 68 74 73 69 7a 65 20 26 20 28 70 48 2d  H->htsize & (pH-
43a17 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29  >htsize-1))==0 )
43a18 3b 0a 20 20 72 65 74 75 72 6e 20 66 74 73 33 46  ;.  return fts3F
43a19 69 6e 64 45 6c 65 6d 65 6e 74 42 79 48 61 73 68  indElementByHash
43a1a 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 20 68  (pH,pKey,nKey, h
43a1b 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31   & (pH->htsize-1
43a1c 29 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 74  ));.}../* .** At
43a1d 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20  tempt to locate 
43a1e 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  an element of th
43a1f 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 20  e hash table pH 
43a20 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68  with a key.** th
43a21 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c  at matches pKey,
43a22 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 74 68  nKey.  Return th
43a23 65 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 20  e data for this 
43a24 65 6c 65 6d 65 6e 74 20 69 66 20 69 74 20 69 73  element if it is
43a25 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55  .** found, or NU
43a26 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  LL if there is n
43a27 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49  o match..*/.SQLI
43a28 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
43a29 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68  *sqlite3Fts3Hash
43a2a 46 69 6e 64 28 63 6f 6e 73 74 20 46 74 73 33 48  Find(const Fts3H
43a2b 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 76  ash *pH, const v
43a2c 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  oid *pKey, int n
43a2d 4b 65 79 29 7b 0a 20 20 46 74 73 33 48 61 73 68  Key){.  Fts3Hash
43a2e 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 20 20 20 20  Elem *pElem;    
43a2f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
43a30 6c 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63  lement that matc
43a31 68 65 73 20 6b 65 79 20 28 69 66 20 61 6e 79 29  hes key (if any)
43a32 20 2a 2f 0a 0a 20 20 70 45 6c 65 6d 20 3d 20 73   */..  pElem = s
43a33 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69  qlite3Fts3HashFi
43a34 6e 64 45 6c 65 6d 28 70 48 2c 20 70 4b 65 79 2c  ndElem(pH, pKey,
43a35 20 6e 4b 65 79 29 3b 0a 20 20 72 65 74 75 72 6e   nKey);.  return
43a36 20 70 45 6c 65 6d 20 3f 20 70 45 6c 65 6d 2d 3e   pElem ? pElem->
43a37 64 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20  data : 0;.}../* 
43a38 49 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d 65 6e  Insert an elemen
43a39 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20  t into the hash 
43a3a 74 61 62 6c 65 20 70 48 2e 20 20 54 68 65 20 6b  table pH.  The k
43a3b 65 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79 0a  ey is pKey,nKey.
43a3c 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 20  ** and the data 
43a3d 69 73 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a 2a  is "data"..**.**
43a3e 20 49 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20 65   If no element e
43a3f 78 69 73 74 73 20 77 69 74 68 20 61 20 6d 61 74  xists with a mat
43a40 63 68 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e 20  ching key, then 
43a41 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e 74  a new.** element
43a42 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 41 20   is created.  A 
43a43 63 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79 20  copy of the key 
43a44 69 73 20 6d 61 64 65 20 69 66 20 74 68 65 20 63  is made if the c
43a45 6f 70 79 4b 65 79 0a 2a 2a 20 66 6c 61 67 20 69  opyKey.** flag i
43a46 73 20 73 65 74 2e 20 20 4e 55 4c 4c 20 69 73 20  s set.  NULL is 
43a47 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
43a48 49 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65  If another eleme
43a49 6e 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  nt already exist
43a4a 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  s with the same 
43a4b 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  key, then the.**
43a4c 20 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61 63   new data replac
43a4d 65 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20  es the old data 
43a4e 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61  and the old data
43a4f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
43a50 20 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20   The key is not 
43a51 63 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20 69  copied in this i
43a52 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d  nstance.  If a m
43a53 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65  alloc fails, the
43a54 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74  n.** the new dat
43a55 61 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  a is returned an
43a56 64 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  d the hash table
43a57 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a   is unchanged..*
43a58 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74  *.** If the "dat
43a59 61 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  a" parameter to 
43a5a 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
43a5b 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a   NULL, then the.
43a5c 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65  ** element corre
43a5d 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79  sponding to "key
43a5e 22 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f  " is removed fro
43a5f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  m the hash table
43a60 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
43a61 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
43a62 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28  3Fts3HashInsert(
43a63 0a 20 20 46 74 73 33 48 61 73 68 20 2a 70 48 2c  .  Fts3Hash *pH,
43a64 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 68          /* The h
43a65 61 73 68 20 74 61 62 6c 65 20 74 6f 20 69 6e 73  ash table to ins
43a66 65 72 74 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f  ert into */.  co
43a67 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
43a68 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f     /* The key */
43a69 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20  .  int nKey,    
43a6a 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
43a6b 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
43a6c 65 20 6b 65 79 20 2a 2f 0a 20 20 76 6f 69 64 20  e key */.  void 
43a6d 2a 64 61 74 61 20 20 20 20 20 20 20 20 20 20 20  *data           
43a6e 2f 2a 20 54 68 65 20 64 61 74 61 20 2a 2f 0a 29  /* The data */.)
43a6f 7b 0a 20 20 69 6e 74 20 68 72 61 77 3b 20 20 20  {.  int hraw;   
43a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
43a71 20 52 61 77 20 68 61 73 68 20 76 61 6c 75 65 20   Raw hash value 
43a72 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20  of the key */.  
43a73 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20  int h;          
43a74 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65            /* the
43a75 20 68 61 73 68 20 6f 66 20 74 68 65 20 6b 65 79   hash of the key
43a76 20 6d 6f 64 75 6c 6f 20 68 61 73 68 20 74 61 62   modulo hash tab
43a77 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 46 74 73  le size */.  Fts
43a78 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b  3HashElem *elem;
43a79 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74         /* Used t
43a7a 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20  o loop thru the 
43a7b 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a  element list */.
43a7c 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a    Fts3HashElem *
43a7d 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e  new_elem;   /* N
43a7e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 65 64  ew element added
43a7f 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a 20 20   to the pH */.  
43a80 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f 6e  int (*xHash)(con
43a81 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20  st void*,int);  
43a82 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75 6e 63  /* The hash func
43a83 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72  tion */..  asser
43a84 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 78 48  t( pH!=0 );.  xH
43a85 61 73 68 20 3d 20 66 74 73 48 61 73 68 46 75 6e  ash = ftsHashFun
43a86 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61  ction(pH->keyCla
43a87 73 73 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78  ss);.  assert( x
43a88 48 61 73 68 21 3d 30 20 29 3b 0a 20 20 68 72 61  Hash!=0 );.  hra
43a89 77 20 3d 20 28 2a 78 48 61 73 68 29 28 70 4b 65  w = (*xHash)(pKe
43a8a 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 61 73 73 65  y, nKey);.  asse
43a8b 72 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65 20  rt( (pH->htsize 
43a8c 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29  & (pH->htsize-1)
43a8d 29 3d 3d 30 20 29 3b 0a 20 20 68 20 3d 20 68 72  )==0 );.  h = hr
43a8e 61 77 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65  aw & (pH->htsize
43a8f 2d 31 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 66 74  -1);.  elem = ft
43a90 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79 48  s3FindElementByH
43a91 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79  ash(pH,pKey,nKey
43a92 2c 68 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20  ,h);.  if( elem 
43a93 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64  ){.    void *old
43a94 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61  _data = elem->da
43a95 74 61 3b 0a 20 20 20 20 69 66 28 20 64 61 74 61  ta;.    if( data
43a96 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 74 73  ==0 ){.      fts
43a97 33 52 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 42 79  3RemoveElementBy
43a98 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b  Hash(pH,elem,h);
43a99 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
43a9a 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64    elem->data = d
43a9b 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  ata;.    }.    r
43a9c 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74 61 3b 0a  eturn old_data;.
43a9d 20 20 7d 0a 20 20 69 66 28 20 64 61 74 61 3d 3d    }.  if( data==
43a9e 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
43a9f 69 66 28 20 28 70 48 2d 3e 68 74 73 69 7a 65 3d  if( (pH->htsize=
43aa0 3d 30 20 26 26 20 66 74 73 33 52 65 68 61 73 68  =0 && fts3Rehash
43aa1 28 70 48 2c 38 29 29 0a 20 20 20 7c 7c 20 28 70  (pH,8)).   || (p
43aa2 48 2d 3e 63 6f 75 6e 74 3e 3d 70 48 2d 3e 68 74  H->count>=pH->ht
43aa3 73 69 7a 65 20 26 26 20 66 74 73 33 52 65 68 61  size && fts3Reha
43aa4 73 68 28 70 48 2c 20 70 48 2d 3e 68 74 73 69 7a  sh(pH, pH->htsiz
43aa5 65 2a 32 29 29 0a 20 20 29 7b 0a 20 20 20 20 70  e*2)).  ){.    p
43aa6 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20  H->count = 0;.  
43aa7 20 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20    return data;. 
43aa8 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 48 2d   }.  assert( pH-
43aa9 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a 20 20 6e  >htsize>0 );.  n
43aaa 65 77 5f 65 6c 65 6d 20 3d 20 28 46 74 73 33 48  ew_elem = (Fts3H
43aab 61 73 68 45 6c 65 6d 2a 29 66 74 73 33 48 61 73  ashElem*)fts3Has
43aac 68 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28  hMalloc( sizeof(
43aad 46 74 73 33 48 61 73 68 45 6c 65 6d 29 20 29 3b  Fts3HashElem) );
43aae 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d  .  if( new_elem=
43aaf 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61 74 61  =0 ) return data
43ab0 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79  ;.  if( pH->copy
43ab1 4b 65 79 20 26 26 20 70 4b 65 79 21 3d 30 20 29  Key && pKey!=0 )
43ab2 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e  {.    new_elem->
43ab3 70 4b 65 79 20 3d 20 66 74 73 33 48 61 73 68 4d  pKey = fts3HashM
43ab4 61 6c 6c 6f 63 28 20 6e 4b 65 79 20 29 3b 0a 20  alloc( nKey );. 
43ab5 20 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 2d     if( new_elem-
43ab6 3e 70 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20  >pKey==0 ){.    
43ab7 20 20 66 74 73 33 48 61 73 68 46 72 65 65 28 6e    fts3HashFree(n
43ab8 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20  ew_elem);.      
43ab9 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20  return data;.   
43aba 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 28 76   }.    memcpy((v
43abb 6f 69 64 2a 29 6e 65 77 5f 65 6c 65 6d 2d 3e 70  oid*)new_elem->p
43abc 4b 65 79 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29  Key, pKey, nKey)
43abd 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
43abe 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20  ew_elem->pKey = 
43abf 28 76 6f 69 64 2a 29 70 4b 65 79 3b 0a 20 20 7d  (void*)pKey;.  }
43ac0 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65  .  new_elem->nKe
43ac1 79 20 3d 20 6e 4b 65 79 3b 0a 20 20 70 48 2d 3e  y = nKey;.  pH->
43ac2 63 6f 75 6e 74 2b 2b 3b 0a 20 20 61 73 73 65 72  count++;.  asser
43ac3 74 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20  t( pH->htsize>0 
43ac4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 48  );.  assert( (pH
43ac5 2d 3e 68 74 73 69 7a 65 20 26 20 28 70 48 2d 3e  ->htsize & (pH->
43ac6 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b  htsize-1))==0 );
43ac7 0a 20 20 68 20 3d 20 68 72 61 77 20 26 20 28 70  .  h = hraw & (p
43ac8 48 2d 3e 68 74 73 69 7a 65 2d 31 29 3b 0a 20 20  H->htsize-1);.  
43ac9 66 74 73 33 48 61 73 68 49 6e 73 65 72 74 45 6c  fts3HashInsertEl
43aca 65 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68  ement(pH, &pH->h
43acb 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b  t[h], new_elem);
43acc 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74  .  new_elem->dat
43acd 61 20 3d 20 64 61 74 61 3b 0a 20 20 72 65 74 75  a = data;.  retu
43ace 72 6e 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  rn 0;.}..#endif 
43acf 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
43ad0 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
43ad1 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
43ad2 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a  E_FTS3) */../***
43ad3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
43ad4 6f 66 20 66 74 73 33 5f 68 61 73 68 2e 63 20 2a  of fts3_hash.c *
43ad5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43ad6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43ad7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
43ad8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
43ad9 6e 20 66 69 6c 65 20 66 74 73 33 5f 70 6f 72 74  n file fts3_port
43ada 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
43adb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43adc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
43add 2a 20 32 30 30 36 20 53 65 70 74 65 6d 62 65 72  * 2006 September
43ade 20 33 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   30.**.** The au
43adf 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
43ae0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
43ae1 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
43ae2 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
43ae3 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
43ae4 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
43ae5 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
43ae6 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
43ae7 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
43ae8 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
43ae9 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
43aea 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
43aeb 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
43aec 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
43aed 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
43aee 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
43aef 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
43af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43af1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43af2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43af3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43af4 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  **.** Implementa
43af5 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 75 6c 6c  tion of the full
43af6 2d 74 65 78 74 2d 73 65 61 72 63 68 20 74 6f 6b  -text-search tok
43af7 65 6e 69 7a 65 72 20 74 68 61 74 20 69 6d 70 6c  enizer that impl
43af8 65 6d 65 6e 74 73 0a 2a 2a 20 61 20 50 6f 72 74  ements.** a Port
43af9 65 72 20 73 74 65 6d 6d 65 72 2e 0a 2a 2f 0a 0a  er stemmer..*/..
43afa 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69  /*.** The code i
43afb 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f  n this file is o
43afc 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a  nly compiled if:
43afd 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  .**.**     * The
43afe 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
43aff 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61  being built as a
43b00 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20  n extension.**  
43b01 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63       (in which c
43b02 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  ase SQLITE_CORE 
43b03 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c  is not defined),
43b04 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20   or.**.**     * 
43b05 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20  The FTS3 module 
43b06 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69  is being built i
43b07 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a  nto the core of.
43b08 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20  **       SQLite 
43b09 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
43b0a 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
43b0b 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a  3 is defined)..*
43b0c 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
43b0d 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
43b0e 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
43b0f 41 42 4c 45 5f 46 54 53 33 29 0a 0a 2f 2a 20 23  ABLE_FTS3)../* #
43b10 69 6e 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e  include <assert.
43b11 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64  h> */./* #includ
43b12 65 20 3c 73 74 64 6c 69 62 2e 68 3e 20 2a 2f 0a  e <stdlib.h> */.
43b13 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 64  /* #include <std
43b14 69 6f 2e 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e 63  io.h> */./* #inc
43b15 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 20  lude <string.h> 
43b16 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 61 73 73  */.../*.** Class
43b17 20 64 65 72 69 76 65 64 20 66 72 6f 6d 20 73 71   derived from sq
43b18 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a  lite3_tokenizer.
43b19 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
43b1a 74 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a  t porter_tokeniz
43b1b 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  er {.  sqlite3_t
43b1c 6f 6b 65 6e 69 7a 65 72 20 62 61 73 65 3b 20 20  okenizer base;  
43b1d 20 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73      /* Base clas
43b1e 73 20 2a 2f 0a 7d 20 70 6f 72 74 65 72 5f 74 6f  s */.} porter_to
43b1f 6b 65 6e 69 7a 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20  kenizer;../*.** 
43b20 43 6c 61 73 73 20 64 65 72 69 76 65 64 20 66 72  Class derived fr
43b21 6f 6d 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  om sqlite3_token
43b22 69 7a 65 72 5f 63 75 72 73 6f 72 0a 2a 2f 0a 74  izer_cursor.*/.t
43b23 79 70 65 64 65 66 20 73 74 72 75 63 74 20 70 6f  ypedef struct po
43b24 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  rter_tokenizer_c
43b25 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65  ursor {.  sqlite
43b26 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
43b27 6f 72 20 62 61 73 65 3b 0a 20 20 63 6f 6e 73 74  or base;.  const
43b28 20 63 68 61 72 20 2a 7a 49 6e 70 75 74 3b 20 20   char *zInput;  
43b29 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 70 75 74          /* input
43b2a 20 77 65 20 61 72 65 20 74 6f 6b 65 6e 69 7a 69   we are tokenizi
43b2b 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 70  ng */.  int nInp
43b2c 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
43b2d 20 20 20 20 20 2f 2a 20 73 69 7a 65 20 6f 66 20       /* size of 
43b2e 74 68 65 20 69 6e 70 75 74 20 2a 2f 0a 20 20 69  the input */.  i
43b2f 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20  nt iOffset;     
43b30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63              /* c
43b31 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20  urrent position 
43b32 69 6e 20 7a 49 6e 70 75 74 20 2a 2f 0a 20 20 69  in zInput */.  i
43b33 6e 74 20 69 54 6f 6b 65 6e 3b 20 20 20 20 20 20  nt iToken;      
43b34 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69              /* i
43b35 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 74 6f 6b  ndex of next tok
43b36 65 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65  en to be returne
43b37 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f  d */.  char *zTo
43b38 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ken;            
43b39 20 20 20 20 2f 2a 20 73 74 6f 72 61 67 65 20 66      /* storage f
43b3a 6f 72 20 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e  or current token
43b3b 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63   */.  int nAlloc
43b3c 61 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20  ated;           
43b3d 20 20 20 2f 2a 20 73 70 61 63 65 20 61 6c 6c 6f     /* space allo
43b3e 63 61 74 65 64 20 74 6f 20 7a 54 6f 6b 65 6e 20  cated to zToken 
43b3f 62 75 66 66 65 72 20 2a 2f 0a 7d 20 70 6f 72 74  buffer */.} port
43b40 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  er_tokenizer_cur
43b41 73 6f 72 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  sor;.../*.** Cre
43b42 61 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69  ate a new tokeni
43b43 7a 65 72 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f  zer instance..*/
43b44 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74  .static int port
43b45 65 72 43 72 65 61 74 65 28 0a 20 20 69 6e 74 20  erCreate(.  int 
43b46 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72  argc, const char
43b47 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 0a   * const *argv,.
43b48 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
43b49 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65  zer **ppTokenize
43b4a 72 0a 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74 6f  r.){.  porter_to
43b4b 6b 65 6e 69 7a 65 72 20 2a 74 3b 0a 0a 20 20 55  kenizer *t;..  U
43b4c 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
43b4d 61 72 67 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f  argc);.  UNUSED_
43b4e 50 41 52 41 4d 45 54 45 52 28 61 72 67 76 29 3b  PARAMETER(argv);
43b4f 0a 0a 20 20 74 20 3d 20 28 70 6f 72 74 65 72 5f  ..  t = (porter_
43b50 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 73 71 6c  tokenizer *) sql
43b51 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
43b52 6f 66 28 2a 74 29 29 3b 0a 20 20 69 66 28 20 74  of(*t));.  if( t
43b53 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20  ==NULL ) return 
43b54 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
43b55 6d 65 6d 73 65 74 28 74 2c 20 30 2c 20 73 69 7a  memset(t, 0, siz
43b56 65 6f 66 28 2a 74 29 29 3b 0a 20 20 2a 70 70 54  eof(*t));.  *ppT
43b57 6f 6b 65 6e 69 7a 65 72 20 3d 20 26 74 2d 3e 62  okenizer = &t->b
43b58 61 73 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ase;.  return SQ
43b59 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
43b5a 2a 20 44 65 73 74 72 6f 79 20 61 20 74 6f 6b 65  * Destroy a toke
43b5b 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20  nizer.*/.static 
43b5c 69 6e 74 20 70 6f 72 74 65 72 44 65 73 74 72 6f  int porterDestro
43b5d 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  y(sqlite3_tokeni
43b5e 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 29  zer *pTokenizer)
43b5f 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  {.  sqlite3_free
43b60 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20  (pTokenizer);.  
43b61 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
43b62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61  ;.}../*.** Prepa
43b63 72 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b 65  re to begin toke
43b64 6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63 75  nizing a particu
43b65 6c 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68 65  lar string.  The
43b66 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e 67   input.** string
43b67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64   to be tokenized
43b68 20 69 73 20 7a 49 6e 70 75 74 5b 30 2e 2e 6e 49   is zInput[0..nI
43b69 6e 70 75 74 2d 31 5d 2e 20 20 41 20 63 75 72 73  nput-1].  A curs
43b6a 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e  or.** used to in
43b6b 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b 65  crementally toke
43b6c 6e 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e 67  nize this string
43b6d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20   is returned in 
43b6e 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a 2a  .** *ppCursor..*
43b6f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72  /.static int por
43b70 74 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74  terOpen(.  sqlit
43b71 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
43b72 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20 20  okenizer,       
43b73 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a    /* The tokeniz
43b74 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  er */.  const ch
43b75 61 72 20 2a 7a 49 6e 70 75 74 2c 20 69 6e 74 20  ar *zInput, int 
43b76 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20 2f  nInput,        /
43b77 2a 20 53 74 72 69 6e 67 20 74 6f 20 62 65 20 74  * String to be t
43b78 6f 6b 65 6e 69 7a 65 64 20 2a 2f 0a 20 20 73 71  okenized */.  sq
43b79 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
43b7a 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f  cursor **ppCurso
43b7b 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b  r    /* OUT: Tok
43b7c 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
43b7d 20 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74 65 72 5f   */.){.  porter_
43b7e 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
43b7f 20 2a 63 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50   *c;..  UNUSED_P
43b80 41 52 41 4d 45 54 45 52 28 70 54 6f 6b 65 6e 69  ARAMETER(pTokeni
43b81 7a 65 72 29 3b 0a 0a 20 20 63 20 3d 20 28 70 6f  zer);..  c = (po
43b82 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  rter_tokenizer_c
43b83 75 72 73 6f 72 20 2a 29 20 73 71 6c 69 74 65 33  ursor *) sqlite3
43b84 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a  _malloc(sizeof(*
43b85 63 29 29 3b 0a 20 20 69 66 28 20 63 3d 3d 4e 55  c));.  if( c==NU
43b86 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  LL ) return SQLI
43b87 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 63 2d 3e  TE_NOMEM;..  c->
43b88 7a 49 6e 70 75 74 20 3d 20 7a 49 6e 70 75 74 3b  zInput = zInput;
43b89 0a 20 20 69 66 28 20 7a 49 6e 70 75 74 3d 3d 30  .  if( zInput==0
43b8a 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70 75   ){.    c->nInpu
43b8b 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  t = 0;.  }else i
43b8c 66 28 20 6e 49 6e 70 75 74 3c 30 20 29 7b 0a 20  f( nInput<0 ){. 
43b8d 20 20 20 63 2d 3e 6e 49 6e 70 75 74 20 3d 20 28     c->nInput = (
43b8e 69 6e 74 29 73 74 72 6c 65 6e 28 7a 49 6e 70 75  int)strlen(zInpu
43b8f 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
43b90 20 63 2d 3e 6e 49 6e 70 75 74 20 3d 20 6e 49 6e   c->nInput = nIn
43b91 70 75 74 3b 0a 20 20 7d 0a 20 20 63 2d 3e 69 4f  put;.  }.  c->iO
43b92 66 66 73 65 74 20 3d 20 30 3b 20 20 20 20 20 20  ffset = 0;      
43b93 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74             /* st
43b94 61 72 74 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61  art tokenizing a
43b95 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
43b96 2a 2f 0a 20 20 63 2d 3e 69 54 6f 6b 65 6e 20 3d  */.  c->iToken =
43b97 20 30 3b 0a 20 20 63 2d 3e 7a 54 6f 6b 65 6e 20   0;.  c->zToken 
43b98 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20 20  = NULL;         
43b99 20 20 20 20 20 20 2f 2a 20 6e 6f 20 73 70 61 63        /* no spac
43b9a 65 20 61 6c 6c 6f 63 61 74 65 64 2c 20 79 65 74  e allocated, yet
43b9b 2e 20 2a 2f 0a 20 20 63 2d 3e 6e 41 6c 6c 6f 63  . */.  c->nAlloc
43b9c 61 74 65 64 20 3d 20 30 3b 0a 0a 20 20 2a 70 70  ated = 0;..  *pp
43b9d 43 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73  Cursor = &c->bas
43b9e 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  e;.  return SQLI
43b9f 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
43ba0 43 6c 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a 61  Close a tokeniza
43ba1 74 69 6f 6e 20 63 75 72 73 6f 72 20 70 72 65 76  tion cursor prev
43ba2 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 62 79  iously opened by
43ba3 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 70 6f   a call to.** po
43ba4 72 74 65 72 4f 70 65 6e 28 29 20 61 62 6f 76 65  rterOpen() above
43ba5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
43ba6 70 6f 72 74 65 72 43 6c 6f 73 65 28 73 71 6c 69  porterClose(sqli
43ba7 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
43ba8 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a  rsor *pCursor){.
43ba9 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a    porter_tokeniz
43baa 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28  er_cursor *c = (
43bab 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
43bac 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73  _cursor *) pCurs
43bad 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  or;.  sqlite3_fr
43bae 65 65 28 63 2d 3e 7a 54 6f 6b 65 6e 29 3b 0a 20  ee(c->zToken);. 
43baf 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 29   sqlite3_free(c)
43bb0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
43bb1 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 56 6f  E_OK;.}./*.** Vo
43bb2 77 65 6c 20 6f 72 20 63 6f 6e 73 6f 6e 61 6e 74  wel or consonant
43bb3 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
43bb4 20 63 68 61 72 20 63 54 79 70 65 5b 5d 20 3d 20   char cType[] = 
43bb5 7b 0a 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c  {.   0, 1, 1, 1,
43bb6 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   0, 1, 1, 1, 0, 
43bb7 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
43bb8 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
43bb9 20 30 2c 0a 20 20 20 31 2c 20 31 2c 20 31 2c 20   0,.   1, 1, 1, 
43bba 32 2c 20 31 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 69  2, 1.};../*.** i
43bbb 73 43 6f 6e 73 6f 6e 61 6e 74 28 29 20 61 6e 64  sConsonant() and
43bbc 20 69 73 56 6f 77 65 6c 28 29 20 64 65 74 65 72   isVowel() deter
43bbd 6d 69 6e 65 20 69 66 20 74 68 65 69 72 20 66 69  mine if their fi
43bbe 72 73 74 20 63 68 61 72 61 63 74 65 72 20 69 6e  rst character in
43bbf 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 74  .** the string t
43bc0 68 65 79 20 70 6f 69 6e 74 20 74 6f 20 69 73 20  hey point to is 
43bc1 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 6f 72 20 61  a consonant or a
43bc2 20 76 6f 77 65 6c 2c 20 61 63 63 6f 72 64 69 6e   vowel, accordin
43bc3 67 0a 2a 2a 20 74 6f 20 50 6f 72 74 65 72 20 72  g.** to Porter r
43bc4 75 6c 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20 63  uls.  .**.** A c
43bc5 6f 6e 73 6f 6e 61 74 65 20 69 73 20 61 6e 79 20  onsonate is any 
43bc6 6c 65 74 74 65 72 20 6f 74 68 65 72 20 74 68 61  letter other tha
43bc7 6e 20 27 61 27 2c 20 27 65 27 2c 20 27 69 27 2c  n 'a', 'e', 'i',
43bc8 20 27 6f 27 2c 20 6f 72 20 27 75 27 2e 0a 2a 2a   'o', or 'u'..**
43bc9 20 27 59 27 20 69 73 20 61 20 63 6f 6e 73 6f 6e   'Y' is a conson
43bca 61 6e 74 20 75 6e 6c 65 73 73 20 69 74 20 66 6f  ant unless it fo
43bcb 6c 6c 6f 77 73 20 61 6e 6f 74 68 65 72 20 63 6f  llows another co
43bcc 6e 73 6f 6e 61 6e 74 2c 0a 2a 2a 20 69 6e 20 77  nsonant,.** in w
43bcd 68 69 63 68 20 63 61 73 65 20 69 74 20 69 73 20  hich case it is 
43bce 61 20 76 6f 77 65 6c 2e 0a 2a 2a 0a 2a 2a 20 49  a vowel..**.** I
43bcf 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 2c  n these routine,
43bd0 20 74 68 65 20 6c 65 74 74 65 72 73 20 61 72 65   the letters are
43bd1 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65   in reverse orde
43bd2 72 2e 20 20 53 6f 20 74 68 65 20 27 79 27 20 72  r.  So the 'y' r
43bd3 75 6c 65 0a 2a 2a 20 69 73 20 74 68 61 74 20 27  ule.** is that '
43bd4 79 27 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61 6e  y' is a consonan
43bd5 74 20 75 6e 6c 65 73 73 20 69 74 20 69 73 20 66  t unless it is f
43bd6 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 6f 74 68  ollowed by anoth
43bd7 65 72 0a 2a 2a 20 63 6f 6e 73 6f 6e 65 6e 74 2e  er.** consonent.
43bd8 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
43bd9 73 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68 61  sVowel(const cha
43bda 72 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20  r*);.static int 
43bdb 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 63 6f 6e 73  isConsonant(cons
43bdc 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e  t char *z){.  in
43bdd 74 20 6a 3b 0a 20 20 63 68 61 72 20 78 20 3d 20  t j;.  char x = 
43bde 2a 7a 3b 0a 20 20 69 66 28 20 78 3d 3d 30 20 29  *z;.  if( x==0 )
43bdf 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73   return 0;.  ass
43be0 65 72 74 28 20 78 3e 3d 27 61 27 20 26 26 20 78  ert( x>='a' && x
43be1 3c 3d 27 7a 27 20 29 3b 0a 20 20 6a 20 3d 20 63  <='z' );.  j = c
43be2 54 79 70 65 5b 78 2d 27 61 27 5d 3b 0a 20 20 69  Type[x-'a'];.  i
43be3 66 28 20 6a 3c 32 20 29 20 72 65 74 75 72 6e 20  f( j<2 ) return 
43be4 6a 3b 0a 20 20 72 65 74 75 72 6e 20 7a 5b 31 5d  j;.  return z[1]
43be5 3d 3d 30 20 7c 7c 20 69 73 56 6f 77 65 6c 28 7a  ==0 || isVowel(z
43be6 20 2b 20 31 29 3b 0a 7d 0a 73 74 61 74 69 63 20   + 1);.}.static 
43be7 69 6e 74 20 69 73 56 6f 77 65 6c 28 63 6f 6e 73  int isVowel(cons
43be8 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e  t char *z){.  in
43be9 74 20 6a 3b 0a 20 20 63 68 61 72 20 78 20 3d 20  t j;.  char x = 
43bea 2a 7a 3b 0a 20 20 69 66 28 20 78 3d 3d 30 20 29  *z;.  if( x==0 )
43beb 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73   return 0;.  ass
43bec 65 72 74 28 20 78 3e 3d 27 61 27 20 26 26 20 78  ert( x>='a' && x
43bed 3c 3d 27 7a 27 20 29 3b 0a 20 20 6a 20 3d 20 63  <='z' );.  j = c
43bee 54 79 70 65 5b 78 2d 27 61 27 5d 3b 0a 20 20 69  Type[x-'a'];.  i
43bef 66 28 20 6a 3c 32 20 29 20 72 65 74 75 72 6e 20  f( j<2 ) return 
43bf0 31 2d 6a 3b 0a 20 20 72 65 74 75 72 6e 20 69 73  1-j;.  return is
43bf1 43 6f 6e 73 6f 6e 61 6e 74 28 7a 20 2b 20 31 29  Consonant(z + 1)
43bf2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 61  ;.}../*.** Let a
43bf3 6e 79 20 73 65 71 75 65 6e 63 65 20 6f 66 20 6f  ny sequence of o
43bf4 6e 65 20 6f 72 20 6d 6f 72 65 20 76 6f 77 65 6c  ne or more vowel
43bf5 73 20 62 65 20 72 65 70 72 65 73 65 6e 74 65 64  s be represented
43bf6 20 62 79 20 56 20 61 6e 64 20 6c 65 74 0a 2a 2a   by V and let.**
43bf7 20 43 20 62 65 20 73 65 71 75 65 6e 63 65 20 6f   C be sequence o
43bf8 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63 6f  f one or more co
43bf9 6e 73 6f 6e 61 6e 74 73 2e 20 20 54 68 65 6e 20  nsonants.  Then 
43bfa 65 76 65 72 79 20 77 6f 72 64 20 63 61 6e 20 62  every word can b
43bfb 65 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 65 64  e.** represented
43bfc 20 61 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20   as:.**.**      
43bfd 20 20 20 20 20 5b 43 5d 20 28 56 43 29 7b 6d 7d       [C] (VC){m}
43bfe 20 5b 56 5d 0a 2a 2a 0a 2a 2a 20 49 6e 20 70 72   [V].**.** In pr
43bff 6f 73 65 3a 20 20 41 20 77 6f 72 64 20 69 73 20  ose:  A word is 
43c00 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 63 6f 6e 73  an optional cons
43c01 6f 6e 61 6e 74 20 66 6f 6c 6c 6f 77 65 64 20 62  onant followed b
43c02 79 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 76 6f 77  y zero or.** vow
43c03 65 6c 2d 63 6f 6e 73 6f 6e 61 6e 74 20 70 61 69  el-consonant pai
43c04 72 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61  rs followed by a
43c05 6e 20 6f 70 74 69 6f 6e 61 6c 20 76 6f 77 65 6c  n optional vowel
43c06 2e 20 20 22 6d 22 20 69 73 20 74 68 65 0a 2a 2a  .  "m" is the.**
43c07 20 6e 75 6d 62 65 72 20 6f 66 20 76 6f 77 65 6c   number of vowel
43c08 20 63 6f 6e 73 6f 6e 61 6e 74 20 70 61 69 72 73   consonant pairs
43c09 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
43c0a 63 6f 6d 70 75 74 65 73 20 74 68 65 20 76 61 6c  computes the val
43c0b 75 65 0a 2a 2a 20 6f 66 20 6d 20 66 6f 72 20 74  ue.** of m for t
43c0c 68 65 20 66 69 72 73 74 20 69 20 62 79 74 65 73  he first i bytes
43c0d 20 6f 66 20 61 20 77 6f 72 64 2e 0a 2a 2a 0a 2a   of a word..**.*
43c0e 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
43c0f 20 74 68 65 20 6d 2d 76 61 6c 75 65 20 66 6f 72   the m-value for
43c10 20 7a 20 69 73 20 31 20 6f 72 20 6d 6f 72 65 2e   z is 1 or more.
43c11 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
43c12 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74 72 75 65  ,.** return true
43c13 20 69 66 20 7a 20 63 6f 6e 74 61 69 6e 73 20 61   if z contains a
43c14 74 20 6c 65 61 73 74 20 6f 6e 65 20 76 6f 77 65  t least one vowe
43c15 6c 20 74 68 61 74 20 69 73 20 66 6f 6c 6c 6f 77  l that is follow
43c16 65 64 0a 2a 2a 20 62 79 20 61 20 63 6f 6e 73 6f  ed.** by a conso
43c17 6e 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74  nant..**.** In t
43c18 68 69 73 20 72 6f 75 74 69 6e 65 20 7a 5b 5d 20  his routine z[] 
43c19 69 73 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72  is in reverse or
43c1a 64 65 72 2e 20 20 53 6f 20 77 65 20 61 72 65 20  der.  So we are 
43c1b 72 65 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67 0a 2a  really looking.*
43c1c 2a 20 66 6f 72 20 61 6e 20 69 6e 73 74 61 6e 63  * for an instanc
43c1d 65 20 6f 66 20 6f 66 20 61 20 63 6f 6e 73 6f 6e  e of of a conson
43c1e 61 6e 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  ant followed by 
43c1f 61 20 76 6f 77 65 6c 2e 0a 2a 2f 0a 73 74 61 74  a vowel..*/.stat
43c20 69 63 20 69 6e 74 20 6d 5f 67 74 5f 30 28 63 6f  ic int m_gt_0(co
43c21 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20  nst char *z){.  
43c22 77 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a  while( isVowel(z
43c23 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66  ) ){ z++; }.  if
43c24 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  ( *z==0 ) return
43c25 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43   0;.  while( isC
43c26 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a  onsonant(z) ){ z
43c27 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a  ++; }.  return *
43c28 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65  z!=0;.}../* Like
43c29 20 6d 67 74 30 20 61 62 6f 76 65 20 65 78 63 65   mgt0 above exce
43c2a 70 74 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e  pt we are lookin
43c2b 67 20 66 6f 72 20 61 20 76 61 6c 75 65 20 6f 66  g for a value of
43c2c 20 6d 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 65   m which is.** e
43c2d 78 61 63 74 6c 79 20 31 0a 2a 2f 0a 73 74 61 74  xactly 1.*/.stat
43c2e 69 63 20 69 6e 74 20 6d 5f 65 71 5f 31 28 63 6f  ic int m_eq_1(co
43c2f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20  nst char *z){.  
43c30 77 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a  while( isVowel(z
43c31 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66  ) ){ z++; }.  if
43c32 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  ( *z==0 ) return
43c33 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43   0;.  while( isC
43c34 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a  onsonant(z) ){ z
43c35 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d  ++; }.  if( *z==
43c36 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
43c37 77 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a  while( isVowel(z
43c38 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66  ) ){ z++; }.  if
43c39 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  ( *z==0 ) return
43c3a 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43   1;.  while( isC
43c3b 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a  onsonant(z) ){ z
43c3c 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a  ++; }.  return *
43c3d 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65  z==0;.}../* Like
43c3e 20 6d 67 74 30 20 61 62 6f 76 65 20 65 78 63 65   mgt0 above exce
43c3f 70 74 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e  pt we are lookin
43c40 67 20 66 6f 72 20 61 20 76 61 6c 75 65 20 6f 66  g for a value of
43c41 20 6d 3e 31 20 69 6e 73 74 65 61 64 0a 2a 2a 20   m>1 instead.** 
43c42 6f 72 20 6d 3e 30 0a 2a 2f 0a 73 74 61 74 69 63  or m>0.*/.static
43c43 20 69 6e 74 20 6d 5f 67 74 5f 31 28 63 6f 6e 73   int m_gt_1(cons
43c44 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77 68  t char *z){.  wh
43c45 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20  ile( isVowel(z) 
43c46 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20  ){ z++; }.  if( 
43c47 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  *z==0 ) return 0
43c48 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e  ;.  while( isCon
43c49 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b  sonant(z) ){ z++
43c4a 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20  ; }.  if( *z==0 
43c4b 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68  ) return 0;.  wh
43c4c 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20  ile( isVowel(z) 
43c4d 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20  ){ z++; }.  if( 
43c4e 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  *z==0 ) return 0
43c4f 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e  ;.  while( isCon
43c50 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b  sonant(z) ){ z++
43c51 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 21  ; }.  return *z!
43c52 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  =0;.}../*.** Ret
43c53 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 72  urn TRUE if ther
43c54 65 20 69 73 20 61 20 76 6f 77 65 6c 20 61 6e 79  e is a vowel any
43c55 77 68 65 72 65 20 77 69 74 68 69 6e 20 7a 5b 30  where within z[0
43c56 2e 2e 6e 2d 31 5d 0a 2a 2f 0a 73 74 61 74 69 63  ..n-1].*/.static
43c57 20 69 6e 74 20 68 61 73 56 6f 77 65 6c 28 63 6f   int hasVowel(co
43c58 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20  nst char *z){.  
43c59 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61  while( isConsona
43c5a 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a  nt(z) ){ z++; }.
43c5b 20 20 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a    return *z!=0;.
43c5c 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
43c5d 54 52 55 45 20 69 66 20 74 68 65 20 77 6f 72 64  TRUE if the word
43c5e 20 65 6e 64 73 20 69 6e 20 61 20 64 6f 75 62 6c   ends in a doubl
43c5f 65 20 63 6f 6e 73 6f 6e 61 6e 74 2e 0a 2a 2a 0a  e consonant..**.
43c60 2a 2a 20 54 68 65 20 74 65 78 74 20 69 73 20 72  ** The text is r
43c61 65 76 65 72 73 65 64 20 68 65 72 65 2e 20 53 6f  eversed here. So
43c62 20 77 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6c   we are really l
43c63 6f 6f 6b 69 6e 67 20 61 74 0a 2a 2a 20 74 68 65  ooking at.** the
43c64 20 66 69 72 73 74 20 74 77 6f 20 63 68 61 72 61   first two chara
43c65 63 74 65 72 73 20 6f 66 20 7a 5b 5d 2e 0a 2a 2f  cters of z[]..*/
43c66 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 75 62  .static int doub
43c67 6c 65 43 6f 6e 73 6f 6e 61 6e 74 28 63 6f 6e 73  leConsonant(cons
43c68 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 72 65  t char *z){.  re
43c69 74 75 72 6e 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  turn isConsonant
43c6a 28 7a 29 20 26 26 20 7a 5b 30 5d 3d 3d 7a 5b 31  (z) && z[0]==z[1
43c6b 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ];.}../*.** Retu
43c6c 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 77  rn TRUE if the w
43c6d 6f 72 64 20 65 6e 64 73 20 77 69 74 68 20 74 68  ord ends with th
43c6e 72 65 65 20 6c 65 74 74 65 72 73 20 77 68 69 63  ree letters whic
43c6f 68 0a 2a 2a 20 61 72 65 20 63 6f 6e 73 6f 6e 61  h.** are consona
43c70 6e 74 2d 76 6f 77 65 6c 2d 63 6f 6e 73 6f 6e 65  nt-vowel-consone
43c71 6e 74 20 61 6e 64 20 77 68 65 72 65 20 74 68 65  nt and where the
43c72 20 66 69 6e 61 6c 20 63 6f 6e 73 6f 6e 61 6e 74   final consonant
43c73 0a 2a 2a 20 69 73 20 6e 6f 74 20 27 77 27 2c 20  .** is not 'w', 
43c74 27 78 27 2c 20 6f 72 20 27 79 27 2e 0a 2a 2a 0a  'x', or 'y'..**.
43c75 2a 2a 20 54 68 65 20 77 6f 72 64 20 69 73 20 72  ** The word is r
43c76 65 76 65 72 73 65 64 20 68 65 72 65 2e 20 20 53  eversed here.  S
43c77 6f 20 77 65 20 61 72 65 20 72 65 61 6c 6c 79 20  o we are really 
43c78 63 68 65 63 6b 69 6e 67 20 74 68 65 0a 2a 2a 20  checking the.** 
43c79 66 69 72 73 74 20 74 68 72 65 65 20 6c 65 74 74  first three lett
43c7a 65 72 73 20 61 6e 64 20 74 68 65 20 66 69 72 73  ers and the firs
43c7b 74 20 6f 6e 65 20 63 61 6e 6e 6f 74 20 62 65 20  t one cannot be 
43c7c 69 6e 20 5b 77 78 79 5d 2e 0a 2a 2f 0a 73 74 61  in [wxy]..*/.sta
43c7d 74 69 63 20 69 6e 74 20 73 74 61 72 5f 6f 68 28  tic int star_oh(
43c7e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a  const char *z){.
43c7f 20 20 72 65 74 75 72 6e 0a 20 20 20 20 69 73 43    return.    isC
43c80 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26 0a 20  onsonant(z) &&. 
43c81 20 20 20 7a 5b 30 5d 21 3d 27 77 27 20 26 26 20     z[0]!='w' && 
43c82 7a 5b 30 5d 21 3d 27 78 27 20 26 26 20 7a 5b 30  z[0]!='x' && z[0
43c83 5d 21 3d 27 79 27 20 26 26 0a 20 20 20 20 69 73  ]!='y' &&.    is
43c84 56 6f 77 65 6c 28 7a 2b 31 29 20 26 26 0a 20 20  Vowel(z+1) &&.  
43c85 20 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 2b    isConsonant(z+
43c86 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  2);.}../*.** If 
43c87 74 68 65 20 77 6f 72 64 20 65 6e 64 73 20 77 69  the word ends wi
43c88 74 68 20 7a 46 72 6f 6d 20 61 6e 64 20 78 43 6f  th zFrom and xCo
43c89 6e 64 28 29 20 69 73 20 74 72 75 65 20 66 6f 72  nd() is true for
43c8a 20 74 68 65 20 73 74 65 6d 0a 2a 2a 20 6f 66 20   the stem.** of 
43c8b 74 68 65 20 77 6f 72 64 20 74 68 61 74 20 70 72  the word that pr
43c8c 65 63 65 65 64 73 20 74 68 65 20 7a 46 72 6f 6d  eceeds the zFrom
43c8d 20 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 63 68   ending, then ch
43c8e 61 6e 67 65 20 74 68 65 20 0a 2a 2a 20 65 6e 64  ange the .** end
43c8f 69 6e 67 20 74 6f 20 7a 54 6f 2e 0a 2a 2a 0a 2a  ing to zTo..**.*
43c90 2a 20 54 68 65 20 69 6e 70 75 74 20 77 6f 72 64  * The input word
43c91 20 2a 70 7a 20 61 6e 64 20 7a 46 72 6f 6d 20 61   *pz and zFrom a
43c92 72 65 20 62 6f 74 68 20 69 6e 20 72 65 76 65 72  re both in rever
43c93 73 65 20 6f 72 64 65 72 2e 20 20 7a 54 6f 0a 2a  se order.  zTo.*
43c94 2a 20 69 73 20 69 6e 20 6e 6f 72 6d 61 6c 20 6f  * is in normal o
43c95 72 64 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 74  rder. .**.** Ret
43c96 75 72 6e 20 54 52 55 45 20 69 66 20 7a 46 72 6f  urn TRUE if zFro
43c97 6d 20 6d 61 74 63 68 65 73 2e 20 20 52 65 74 75  m matches.  Retu
43c98 72 6e 20 46 41 4c 53 45 20 69 66 20 7a 46 72 6f  rn FALSE if zFro
43c99 6d 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61  m does not.** ma
43c9a 74 63 68 2e 20 20 4e 6f 74 20 74 68 61 74 20 54  tch.  Not that T
43c9b 52 55 45 20 69 73 20 72 65 74 75 72 6e 65 64 20  RUE is returned 
43c9c 65 76 65 6e 20 69 66 20 78 43 6f 6e 64 28 29 20  even if xCond() 
43c9d 66 61 69 6c 73 20 61 6e 64 0a 2a 2a 20 6e 6f 20  fails and.** no 
43c9e 73 75 62 73 74 69 74 75 74 69 6f 6e 20 6f 63 63  substitution occ
43c9f 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  urs..*/.static i
43ca0 6e 74 20 73 74 65 6d 28 0a 20 20 63 68 61 72 20  nt stem(.  char 
43ca1 2a 2a 70 7a 2c 20 20 20 20 20 20 20 20 20 20 20  **pz,           
43ca2 20 20 2f 2a 20 54 68 65 20 77 6f 72 64 20 62 65    /* The word be
43ca3 69 6e 67 20 73 74 65 6d 6d 65 64 20 28 52 65 76  ing stemmed (Rev
43ca4 65 72 73 65 64 29 20 2a 2f 0a 20 20 63 6f 6e 73  ersed) */.  cons
43ca5 74 20 63 68 61 72 20 2a 7a 46 72 6f 6d 2c 20 20  t char *zFrom,  
43ca6 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 6e 64     /* If the end
43ca7 69 6e 67 20 6d 61 74 63 68 65 73 20 74 68 69 73  ing matches this
43ca8 2e 2e 2e 20 28 52 65 76 65 72 73 65 64 29 20 2a  ... (Reversed) *
43ca9 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
43caa 7a 54 6f 2c 20 20 20 20 20 20 20 2f 2a 20 2e 2e  zTo,       /* ..
43cab 2e 20 63 68 61 6e 67 65 20 74 68 65 20 65 6e 64  . change the end
43cac 69 6e 67 20 74 6f 20 74 68 69 73 20 28 6e 6f 74  ing to this (not
43cad 20 72 65 76 65 72 73 65 64 29 20 2a 2f 0a 20 20   reversed) */.  
43cae 69 6e 74 20 28 2a 78 43 6f 6e 64 29 28 63 6f 6e  int (*xCond)(con
43caf 73 74 20 63 68 61 72 2a 29 20 20 20 2f 2a 20 43  st char*)   /* C
43cb0 6f 6e 64 69 74 69 6f 6e 20 74 68 61 74 20 6d 75  ondition that mu
43cb1 73 74 20 62 65 20 74 72 75 65 20 2a 2f 0a 29 7b  st be true */.){
43cb2 0a 20 20 63 68 61 72 20 2a 7a 20 3d 20 2a 70 7a  .  char *z = *pz
43cb3 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 46 72 6f  ;.  while( *zFro
43cb4 6d 20 26 26 20 2a 7a 46 72 6f 6d 3d 3d 2a 7a 20  m && *zFrom==*z 
43cb5 29 7b 20 7a 2b 2b 3b 20 7a 46 72 6f 6d 2b 2b 3b  ){ z++; zFrom++;
43cb6 20 7d 0a 20 20 69 66 28 20 2a 7a 46 72 6f 6d 21   }.  if( *zFrom!
43cb7 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
43cb8 20 69 66 28 20 78 43 6f 6e 64 20 26 26 20 21 78   if( xCond && !x
43cb9 43 6f 6e 64 28 7a 29 20 29 20 72 65 74 75 72 6e  Cond(z) ) return
43cba 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 54   1;.  while( *zT
43cbb 6f 20 29 7b 0a 20 20 20 20 2a 28 2d 2d 7a 29 20  o ){.    *(--z) 
43cbc 3d 20 2a 28 7a 54 6f 2b 2b 29 3b 0a 20 20 7d 0a  = *(zTo++);.  }.
43cbd 20 20 2a 70 7a 20 3d 20 7a 3b 0a 20 20 72 65 74    *pz = z;.  ret
43cbe 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 1;.}../*.** 
43cbf 54 68 69 73 20 69 73 20 74 68 65 20 66 61 6c 6c  This is the fall
43cc0 62 61 63 6b 20 73 74 65 6d 6d 65 72 20 75 73 65  back stemmer use
43cc1 64 20 77 68 65 6e 20 74 68 65 20 70 6f 72 74 65  d when the porte
43cc2 72 20 73 74 65 6d 6d 65 72 20 69 73 0a 2a 2a 20  r stemmer is.** 
43cc3 69 6e 61 70 70 72 6f 70 72 69 61 74 65 2e 20 20  inappropriate.  
43cc4 54 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20 69  The input word i
43cc5 73 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68  s copied into th
43cc6 65 20 6f 75 74 70 75 74 20 77 69 74 68 0a 2a 2a  e output with.**
43cc7 20 55 53 2d 41 53 43 49 49 20 63 61 73 65 20 66   US-ASCII case f
43cc8 6f 6c 64 69 6e 67 2e 20 20 49 66 20 74 68 65 20  olding.  If the 
43cc9 69 6e 70 75 74 20 77 6f 72 64 20 69 73 20 74 6f  input word is to
43cca 6f 20 6c 6f 6e 67 20 28 6d 6f 72 65 0a 2a 2a 20  o long (more.** 
43ccb 74 68 61 6e 20 32 30 20 62 79 74 65 73 20 69 66  than 20 bytes if
43ccc 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20   it contains no 
43ccd 64 69 67 69 74 73 20 6f 72 20 6d 6f 72 65 20 74  digits or more t
43cce 68 61 6e 20 36 20 62 79 74 65 73 20 69 66 0a 2a  han 6 bytes if.*
43ccf 2a 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 64 69  * it contains di
43cd0 67 69 74 73 29 20 74 68 65 6e 20 77 6f 72 64 20  gits) then word 
43cd1 69 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20  is truncated to 
43cd2 32 30 20 6f 72 20 36 20 62 79 74 65 73 0a 2a 2a  20 or 6 bytes.**
43cd3 20 62 79 20 74 61 6b 69 6e 67 20 31 30 20 6f 72   by taking 10 or
43cd4 20 33 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68   3 bytes from th
43cd5 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20  e beginning and 
43cd6 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  end..*/.static v
43cd7 6f 69 64 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72  oid copy_stemmer
43cd8 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e  (const char *zIn
43cd9 2c 20 69 6e 74 20 6e 49 6e 2c 20 63 68 61 72 20  , int nIn, char 
43cda 2a 7a 4f 75 74 2c 20 69 6e 74 20 2a 70 6e 4f 75  *zOut, int *pnOu
43cdb 74 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6d 78 2c  t){.  int i, mx,
43cdc 20 6a 3b 0a 20 20 69 6e 74 20 68 61 73 44 69 67   j;.  int hasDig
43cdd 69 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d  it = 0;.  for(i=
43cde 30 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b 29 7b 0a  0; i<nIn; i++){.
43cdf 20 20 20 20 63 68 61 72 20 63 20 3d 20 7a 49 6e      char c = zIn
43ce0 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 63 3e 3d  [i];.    if( c>=
43ce1 27 41 27 20 26 26 20 63 3c 3d 27 5a 27 20 29 7b  'A' && c<='Z' ){
43ce2 0a 20 20 20 20 20 20 7a 4f 75 74 5b 69 5d 20 3d  .      zOut[i] =
43ce3 20 63 20 2d 20 27 41 27 20 2b 20 27 61 27 3b 0a   c - 'A' + 'a';.
43ce4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
43ce5 20 69 66 28 20 63 3e 3d 27 30 27 20 26 26 20 63   if( c>='0' && c
43ce6 3c 3d 27 39 27 20 29 20 68 61 73 44 69 67 69 74  <='9' ) hasDigit
43ce7 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a 4f 75 74   = 1;.      zOut
43ce8 5b 69 5d 20 3d 20 63 3b 0a 20 20 20 20 7d 0a 20  [i] = c;.    }. 
43ce9 20 7d 0a 20 20 6d 78 20 3d 20 68 61 73 44 69 67   }.  mx = hasDig
43cea 69 74 20 3f 20 33 20 3a 20 31 30 3b 0a 20 20 69  it ? 3 : 10;.  i
43ceb 66 28 20 6e 49 6e 3e 6d 78 2a 32 20 29 7b 0a 20  f( nIn>mx*2 ){. 
43cec 20 20 20 66 6f 72 28 6a 3d 6d 78 2c 20 69 3d 6e     for(j=mx, i=n
43ced 49 6e 2d 6d 78 3b 20 69 3c 6e 49 6e 3b 20 69 2b  In-mx; i<nIn; i+
43cee 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 7a  +, j++){.      z
43cef 4f 75 74 5b 6a 5d 20 3d 20 7a 4f 75 74 5b 69 5d  Out[j] = zOut[i]
43cf0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20  ;.    }.    i = 
43cf1 6a 3b 0a 20 20 7d 0a 20 20 7a 4f 75 74 5b 69 5d  j;.  }.  zOut[i]
43cf2 20 3d 20 30 3b 0a 20 20 2a 70 6e 4f 75 74 20 3d   = 0;.  *pnOut =
43cf3 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74   i;.}.../*.** St
43cf4 65 6d 20 74 68 65 20 69 6e 70 75 74 20 77 6f 72  em the input wor
43cf5 64 20 7a 49 6e 5b 30 2e 2e 6e 49 6e 2d 31 5d 2e  d zIn[0..nIn-1].
43cf6 20 20 53 74 6f 72 65 20 74 68 65 20 6f 75 74 70    Store the outp
43cf7 75 74 20 69 6e 20 7a 4f 75 74 2e 0a 2a 2a 20 7a  ut in zOut..** z
43cf8 4f 75 74 20 69 73 20 61 74 20 6c 65 61 73 74 20  Out is at least 
43cf9 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f  big enough to ho
43cfa 6c 64 20 6e 49 6e 20 62 79 74 65 73 2e 20 20 57  ld nIn bytes.  W
43cfb 72 69 74 65 20 74 68 65 20 61 63 74 75 61 6c 0a  rite the actual.
43cfc 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6f  ** size of the o
43cfd 75 74 70 75 74 20 77 6f 72 64 20 28 65 78 63 6c  utput word (excl
43cfe 75 73 69 76 65 20 6f 66 20 74 68 65 20 27 5c 30  usive of the '\0
43cff 27 20 74 65 72 6d 69 6e 61 74 6f 72 29 20 69 6e  ' terminator) in
43d00 74 6f 20 2a 70 6e 4f 75 74 2e 0a 2a 2a 0a 2a 2a  to *pnOut..**.**
43d01 20 41 6e 79 20 75 70 70 65 72 2d 63 61 73 65 20   Any upper-case 
43d02 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68  characters in th
43d03 65 20 55 53 2d 41 53 43 49 49 20 63 68 61 72 61  e US-ASCII chara
43d04 63 74 65 72 20 73 65 74 20 28 5b 41 2d 5a 5d 29  cter set ([A-Z])
43d05 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65  .** are converte
43d06 64 20 74 6f 20 6c 6f 77 65 72 20 63 61 73 65 2e  d to lower case.
43d07 20 20 55 70 70 65 72 2d 63 61 73 65 20 55 54 46    Upper-case UTF
43d08 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 0a   characters are.
43d09 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a  ** unchanged..**
43d0a 0a 2a 2a 20 57 6f 72 64 73 20 74 68 61 74 20 61  .** Words that a
43d0b 72 65 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61  re longer than a
43d0c 62 6f 75 74 20 32 30 20 62 79 74 65 73 20 61 72  bout 20 bytes ar
43d0d 65 20 73 74 65 6d 6d 65 64 20 62 79 20 72 65 74  e stemmed by ret
43d0e 61 69 6e 69 6e 67 0a 2a 2a 20 61 20 66 65 77 20  aining.** a few 
43d0f 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62  bytes from the b
43d10 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 74 68 65  eginning and the
43d11 20 65 6e 64 20 6f 66 20 74 68 65 20 77 6f 72 64   end of the word
43d12 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 77 6f 72  .  If the.** wor
43d13 64 20 63 6f 6e 74 61 69 6e 73 20 64 69 67 69 74  d contains digit
43d14 73 2c 20 33 20 62 79 74 65 73 20 61 72 65 20 74  s, 3 bytes are t
43d15 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 62 65  aken from the be
43d16 67 69 6e 6e 69 6e 67 20 61 6e 64 0a 2a 2a 20 33  ginning and.** 3
43d17 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20   bytes from the 
43d18 65 6e 64 2e 20 20 46 6f 72 20 6c 6f 6e 67 20 77  end.  For long w
43d19 6f 72 64 73 20 77 69 74 68 6f 75 74 20 64 69 67  ords without dig
43d1a 69 74 73 2c 20 31 30 20 62 79 74 65 73 0a 2a 2a  its, 10 bytes.**
43d1b 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20   are taken from 
43d1c 65 61 63 68 20 65 6e 64 2e 20 20 55 53 2d 41 53  each end.  US-AS
43d1d 43 49 49 20 63 61 73 65 20 66 6f 6c 64 69 6e 67  CII case folding
43d1e 20 73 74 69 6c 6c 20 61 70 70 6c 69 65 73 2e 0a   still applies..
43d1f 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e  ** .** If the in
43d20 70 75 74 20 77 6f 72 64 20 63 6f 6e 74 61 69 6e  put word contain
43d21 73 20 6e 6f 74 20 64 69 67 69 74 73 20 62 75 74  s not digits but
43d22 20 64 6f 65 73 20 63 68 61 72 61 63 74 65 72 73   does characters
43d23 20 6e 6f 74 20 0a 2a 2a 20 69 6e 20 5b 61 2d 7a   not .** in [a-z
43d24 41 2d 5a 5d 20 74 68 65 6e 20 6e 6f 20 73 74 65  A-Z] then no ste
43d25 6d 6d 69 6e 67 20 69 73 20 61 74 74 65 6d 70 74  mming is attempt
43d26 65 64 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74  ed and this rout
43d27 69 6e 65 20 6a 75 73 74 20 0a 2a 2a 20 63 6f 70  ine just .** cop
43d28 69 65 73 20 74 68 65 20 69 6e 70 75 74 20 69 6e  ies the input in
43d29 74 6f 20 74 68 65 20 69 6e 70 75 74 20 69 6e 74  to the input int
43d2a 6f 20 74 68 65 20 6f 75 74 70 75 74 20 77 69 74  o the output wit
43d2b 68 20 55 53 2d 41 53 43 49 49 0a 2a 2a 20 63 61  h US-ASCII.** ca
43d2c 73 65 20 66 6f 6c 64 69 6e 67 2e 0a 2a 2a 0a 2a  se folding..**.*
43d2d 2a 20 53 74 65 6d 6d 69 6e 67 20 6e 65 76 65 72  * Stemming never
43d2e 20 69 6e 63 72 65 61 73 65 73 20 74 68 65 20 6c   increases the l
43d2f 65 6e 67 74 68 20 6f 66 20 74 68 65 20 77 6f 72  ength of the wor
43d30 64 2e 20 20 53 6f 20 74 68 65 72 65 20 69 73 0a  d.  So there is.
43d31 2a 2a 20 6e 6f 20 63 68 61 6e 63 65 20 6f 66 20  ** no chance of 
43d32 6f 76 65 72 66 6c 6f 77 69 6e 67 20 74 68 65 20  overflowing the 
43d33 7a 4f 75 74 20 62 75 66 66 65 72 2e 0a 2a 2f 0a  zOut buffer..*/.
43d34 73 74 61 74 69 63 20 76 6f 69 64 20 70 6f 72 74  static void port
43d35 65 72 5f 73 74 65 6d 6d 65 72 28 63 6f 6e 73 74  er_stemmer(const
43d36 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20   char *zIn, int 
43d37 6e 49 6e 2c 20 63 68 61 72 20 2a 7a 4f 75 74 2c  nIn, char *zOut,
43d38 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20 20   int *pnOut){.  
43d39 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72  int i, j;.  char
43d3a 20 7a 52 65 76 65 72 73 65 5b 32 38 5d 3b 0a 20   zReverse[28];. 
43d3b 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 32 3b 0a 20   char *z, *z2;. 
43d3c 20 69 66 28 20 6e 49 6e 3c 33 20 7c 7c 20 6e 49   if( nIn<3 || nI
43d3d 6e 3e 3d 28 69 6e 74 29 73 69 7a 65 6f 66 28 7a  n>=(int)sizeof(z
43d3e 52 65 76 65 72 73 65 29 2d 37 20 29 7b 0a 20 20  Reverse)-7 ){.  
43d3f 20 20 2f 2a 20 54 68 65 20 77 6f 72 64 20 69 73    /* The word is
43d40 20 74 6f 6f 20 62 69 67 20 6f 72 20 74 6f 6f 20   too big or too 
43d41 73 6d 61 6c 6c 20 66 6f 72 20 74 68 65 20 70 6f  small for the po
43d42 72 74 65 72 20 73 74 65 6d 6d 65 72 2e 0a 20 20  rter stemmer..  
43d43 20 20 2a 2a 20 46 61 6c 6c 62 61 63 6b 20 74 6f    ** Fallback to
43d44 20 74 68 65 20 63 6f 70 79 20 73 74 65 6d 6d 65   the copy stemme
43d45 72 20 2a 2f 0a 20 20 20 20 63 6f 70 79 5f 73 74  r */.    copy_st
43d46 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49 6e 2c 20  emmer(zIn, nIn, 
43d47 7a 4f 75 74 2c 20 70 6e 4f 75 74 29 3b 0a 20 20  zOut, pnOut);.  
43d48 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
43d49 66 6f 72 28 69 3d 30 2c 20 6a 3d 73 69 7a 65 6f  for(i=0, j=sizeo
43d4a 66 28 7a 52 65 76 65 72 73 65 29 2d 36 3b 20 69  f(zReverse)-6; i
43d4b 3c 6e 49 6e 3b 20 69 2b 2b 2c 20 6a 2d 2d 29 7b  <nIn; i++, j--){
43d4c 0a 20 20 20 20 63 68 61 72 20 63 20 3d 20 7a 49  .    char c = zI
43d4d 6e 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 63 3e  n[i];.    if( c>
43d4e 3d 27 41 27 20 26 26 20 63 3c 3d 27 5a 27 20 29  ='A' && c<='Z' )
43d4f 7b 0a 20 20 20 20 20 20 7a 52 65 76 65 72 73 65  {.      zReverse
43d50 5b 6a 5d 20 3d 20 63 20 2b 20 27 61 27 20 2d 20  [j] = c + 'a' - 
43d51 27 41 27 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  'A';.    }else i
43d52 66 28 20 63 3e 3d 27 61 27 20 26 26 20 63 3c 3d  f( c>='a' && c<=
43d53 27 7a 27 20 29 7b 0a 20 20 20 20 20 20 7a 52 65  'z' ){.      zRe
43d54 76 65 72 73 65 5b 6a 5d 20 3d 20 63 3b 0a 20 20  verse[j] = c;.  
43d55 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
43d56 2a 20 54 68 65 20 75 73 65 20 6f 66 20 61 20 63  * The use of a c
43d57 68 61 72 61 63 74 65 72 20 6e 6f 74 20 69 6e 20  haracter not in 
43d58 5b 61 2d 7a 41 2d 5a 5d 20 6d 65 61 6e 73 20 74  [a-zA-Z] means t
43d59 68 61 74 20 77 65 20 66 61 6c 6c 62 61 63 6b 0a  hat we fallback.
43d5a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20        ** to the 
43d5b 63 6f 70 79 20 73 74 65 6d 6d 65 72 20 2a 2f 0a  copy stemmer */.
43d5c 20 20 20 20 20 20 63 6f 70 79 5f 73 74 65 6d 6d        copy_stemm
43d5d 65 72 28 7a 49 6e 2c 20 6e 49 6e 2c 20 7a 4f 75  er(zIn, nIn, zOu
43d5e 74 2c 20 70 6e 4f 75 74 29 3b 0a 20 20 20 20 20  t, pnOut);.     
43d5f 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
43d60 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 7a 52 65   }.  memset(&zRe
43d61 76 65 72 73 65 5b 73 69 7a 65 6f 66 28 7a 52 65  verse[sizeof(zRe
43d62 76 65 72 73 65 29 2d 35 5d 2c 20 30 2c 20 35 29  verse)-5], 0, 5)
43d63 3b 0a 20 20 7a 20 3d 20 26 7a 52 65 76 65 72 73  ;.  z = &zRevers
43d64 65 5b 6a 2b 31 5d 3b 0a 0a 0a 20 20 2f 2a 20 53  e[j+1];...  /* S
43d65 74 65 70 20 31 61 20 2a 2f 0a 20 20 69 66 28 20  tep 1a */.  if( 
43d66 7a 5b 30 5d 3d 3d 27 73 27 20 29 7b 0a 20 20 20  z[0]=='s' ){.   
43d67 20 69 66 28 0a 20 20 20 20 20 21 73 74 65 6d 28   if(.     !stem(
43d68 26 7a 2c 20 22 73 65 73 73 22 2c 20 22 73 73 22  &z, "sess", "ss"
43d69 2c 20 30 29 20 26 26 0a 20 20 20 20 20 21 73 74  , 0) &&.     !st
43d6a 65 6d 28 26 7a 2c 20 22 73 65 69 22 2c 20 22 69  em(&z, "sei", "i
43d6b 22 2c 20 30 29 20 20 26 26 0a 20 20 20 20 20 21  ", 0)  &&.     !
43d6c 73 74 65 6d 28 26 7a 2c 20 22 73 73 22 2c 20 22  stem(&z, "ss", "
43d6d 73 73 22 2c 20 30 29 0a 20 20 20 20 29 7b 0a 20  ss", 0).    ){. 
43d6e 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a       z++;.    }.
43d6f 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 31    }..  /* Step 1
43d70 62 20 2a 2f 20 20 0a 20 20 7a 32 20 3d 20 7a 3b  b */  .  z2 = z;
43d71 0a 20 20 69 66 28 20 73 74 65 6d 28 26 7a 2c 20  .  if( stem(&z, 
43d72 22 64 65 65 22 2c 20 22 65 65 22 2c 20 6d 5f 67  "dee", "ee", m_g
43d73 74 5f 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20 44  t_0) ){.    /* D
43d74 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68 65 20  o nothing.  The 
43d75 77 6f 72 6b 20 77 61 73 20 61 6c 6c 20 69 6e 20  work was all in 
43d76 74 68 65 20 74 65 73 74 20 2a 2f 0a 20 20 7d 65  the test */.  }e
43d77 6c 73 65 20 69 66 28 20 0a 20 20 20 20 20 28 73  lse if( .     (s
43d78 74 65 6d 28 26 7a 2c 20 22 67 6e 69 22 2c 20 22  tem(&z, "gni", "
43d79 22 2c 20 68 61 73 56 6f 77 65 6c 29 20 7c 7c 20  ", hasVowel) || 
43d7a 73 74 65 6d 28 26 7a 2c 20 22 64 65 22 2c 20 22  stem(&z, "de", "
43d7b 22 2c 20 68 61 73 56 6f 77 65 6c 29 29 0a 20 20  ", hasVowel)).  
43d7c 20 20 20 20 26 26 20 7a 21 3d 7a 32 0a 20 20 29      && z!=z2.  )
43d7d 7b 0a 20 20 20 20 20 69 66 28 20 73 74 65 6d 28  {.     if( stem(
43d7e 26 7a 2c 20 22 74 61 22 2c 20 22 61 74 65 22 2c  &z, "ta", "ate",
43d7f 20 30 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20   0) ||.         
43d80 73 74 65 6d 28 26 7a 2c 20 22 6c 62 22 2c 20 22  stem(&z, "lb", "
43d81 62 6c 65 22 2c 20 30 29 20 7c 7c 0a 20 20 20 20  ble", 0) ||.    
43d82 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 7a       stem(&z, "z
43d83 69 22 2c 20 22 69 7a 65 22 2c 20 30 29 20 29 7b  i", "ize", 0) ){
43d84 0a 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f  .       /* Do no
43d85 74 68 69 6e 67 2e 20 20 54 68 65 20 77 6f 72 6b  thing.  The work
43d86 20 77 61 73 20 61 6c 6c 20 69 6e 20 74 68 65 20   was all in the 
43d87 74 65 73 74 20 2a 2f 0a 20 20 20 20 20 7d 65 6c  test */.     }el
43d88 73 65 20 69 66 28 20 64 6f 75 62 6c 65 43 6f 6e  se if( doubleCon
43d89 73 6f 6e 61 6e 74 28 7a 29 20 26 26 20 28 2a 7a  sonant(z) && (*z
43d8a 21 3d 27 6c 27 20 26 26 20 2a 7a 21 3d 27 73 27  !='l' && *z!='s'
43d8b 20 26 26 20 2a 7a 21 3d 27 7a 27 29 20 29 7b 0a   && *z!='z') ){.
43d8c 20 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20         z++;.    
43d8d 20 7d 65 6c 73 65 20 69 66 28 20 6d 5f 65 71 5f   }else if( m_eq_
43d8e 31 28 7a 29 20 26 26 20 73 74 61 72 5f 6f 68 28  1(z) && star_oh(
43d8f 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 2a 28 2d  z) ){.       *(-
43d90 2d 7a 29 20 3d 20 27 65 27 3b 0a 20 20 20 20 20  -z) = 'e';.     
43d91 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  }.  }..  /* Step
43d92 20 31 63 20 2a 2f 0a 20 20 69 66 28 20 7a 5b 30   1c */.  if( z[0
43d93 5d 3d 3d 27 79 27 20 26 26 20 68 61 73 56 6f 77  ]=='y' && hasVow
43d94 65 6c 28 7a 2b 31 29 20 29 7b 0a 20 20 20 20 7a  el(z+1) ){.    z
43d95 5b 30 5d 20 3d 20 27 69 27 3b 0a 20 20 7d 0a 0a  [0] = 'i';.  }..
43d96 20 20 2f 2a 20 53 74 65 70 20 32 20 2a 2f 0a 20    /* Step 2 */. 
43d97 20 73 77 69 74 63 68 28 20 7a 5b 31 5d 20 29 7b   switch( z[1] ){
43d98 0a 20 20 20 63 61 73 65 20 27 61 27 3a 0a 20 20  .   case 'a':.  
43d99 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c 61 6e     stem(&z, "lan
43d9a 6f 69 74 61 22 2c 20 22 61 74 65 22 2c 20 6d 5f  oita", "ate", m_
43d9b 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
43d9c 65 6d 28 26 7a 2c 20 22 6c 61 6e 6f 69 74 22 2c  em(&z, "lanoit",
43d9d 20 22 74 69 6f 6e 22 2c 20 6d 5f 67 74 5f 30 29   "tion", m_gt_0)
43d9e 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;.     break;.  
43d9f 20 63 61 73 65 20 27 63 27 3a 0a 20 20 20 20 20   case 'c':.     
43da0 73 74 65 6d 28 26 7a 2c 20 22 69 63 6e 65 22 2c  stem(&z, "icne",
43da1 20 22 65 6e 63 65 22 2c 20 6d 5f 67 74 5f 30 29   "ence", m_gt_0)
43da2 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
43da3 2c 20 22 69 63 6e 61 22 2c 20 22 61 6e 63 65 22  , "icna", "ance"
43da4 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20  , m_gt_0);.     
43da5 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
43da6 65 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a  e':.     stem(&z
43da7 2c 20 22 72 65 7a 69 22 2c 20 22 69 7a 65 22 2c  , "rezi", "ize",
43da8 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
43da9 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 67  reak;.   case 'g
43daa 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ':.     stem(&z,
43dab 20 22 69 67 6f 6c 22 2c 20 22 6c 6f 67 22 2c 20   "igol", "log", 
43dac 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72  m_gt_0);.     br
43dad 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6c 27  eak;.   case 'l'
43dae 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  :.     stem(&z, 
43daf 22 69 6c 62 22 2c 20 22 62 6c 65 22 2c 20 6d 5f  "ilb", "ble", m_
43db0 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
43db1 65 6d 28 26 7a 2c 20 22 69 6c 6c 61 22 2c 20 22  em(&z, "illa", "
43db2 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  al", m_gt_0) ||.
43db3 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
43db4 6c 74 6e 65 22 2c 20 22 65 6e 74 22 2c 20 6d 5f  ltne", "ent", m_
43db5 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
43db6 65 6d 28 26 7a 2c 20 22 69 6c 65 22 2c 20 22 65  em(&z, "ile", "e
43db7 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
43db8 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 73     stem(&z, "ils
43db9 75 6f 22 2c 20 22 6f 75 73 22 2c 20 6d 5f 67 74  uo", "ous", m_gt
43dba 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b  _0);.     break;
43dbb 0a 20 20 20 63 61 73 65 20 27 6f 27 3a 0a 20 20  .   case 'o':.  
43dbc 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69     stem(&z, "noi
43dbd 74 61 7a 69 22 2c 20 22 69 7a 65 22 2c 20 6d 5f  tazi", "ize", m_
43dbe 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
43dbf 65 6d 28 26 7a 2c 20 22 6e 6f 69 74 61 22 2c 20  em(&z, "noita", 
43dc0 22 61 74 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c  "ate", m_gt_0) |
43dc1 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  |.     stem(&z, 
43dc2 22 72 6f 74 61 22 2c 20 22 61 74 65 22 2c 20 6d  "rota", "ate", m
43dc3 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65  _gt_0);.     bre
43dc4 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 73 27 3a  ak;.   case 's':
43dc5 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
43dc6 6d 73 69 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f  msila", "al", m_
43dc7 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
43dc8 65 6d 28 26 7a 2c 20 22 73 73 65 6e 65 76 69 22  em(&z, "ssenevi"
43dc9 2c 20 22 69 76 65 22 2c 20 6d 5f 67 74 5f 30 29  , "ive", m_gt_0)
43dca 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
43dcb 2c 20 22 73 73 65 6e 6c 75 66 22 2c 20 22 66 75  , "ssenluf", "fu
43dcc 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20  l", m_gt_0) ||. 
43dcd 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73 73      stem(&z, "ss
43dce 65 6e 73 75 6f 22 2c 20 22 6f 75 73 22 2c 20 6d  ensuo", "ous", m
43dcf 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65  _gt_0);.     bre
43dd0 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 74 27 3a  ak;.   case 't':
43dd1 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
43dd2 69 74 69 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f  itila", "al", m_
43dd3 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
43dd4 65 6d 28 26 7a 2c 20 22 69 74 69 76 69 22 2c 20  em(&z, "itivi", 
43dd5 22 69 76 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c  "ive", m_gt_0) |
43dd6 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  |.     stem(&z, 
43dd7 22 69 74 69 6c 69 62 22 2c 20 22 62 6c 65 22 2c  "itilib", "ble",
43dd8 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
43dd9 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  reak;.  }..  /* 
43dda 53 74 65 70 20 33 20 2a 2f 0a 20 20 73 77 69 74  Step 3 */.  swit
43ddb 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 20 20 20 63  ch( z[0] ){.   c
43ddc 61 73 65 20 27 65 27 3a 0a 20 20 20 20 20 73 74  ase 'e':.     st
43ddd 65 6d 28 26 7a 2c 20 22 65 74 61 63 69 22 2c 20  em(&z, "etaci", 
43dde 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c  "ic", m_gt_0) ||
43ddf 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
43de0 65 76 69 74 61 22 2c 20 22 22 2c 20 6d 5f 67 74  evita", "", m_gt
43de1 5f 30 29 20 20 20 7c 7c 0a 20 20 20 20 20 73 74  _0)   ||.     st
43de2 65 6d 28 26 7a 2c 20 22 65 7a 69 6c 61 22 2c 20  em(&z, "ezila", 
43de3 22 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20  "al", m_gt_0);. 
43de4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
43de5 73 65 20 27 69 27 3a 0a 20 20 20 20 20 73 74 65  se 'i':.     ste
43de6 6d 28 26 7a 2c 20 22 69 74 69 63 69 22 2c 20 22  m(&z, "itici", "
43de7 69 63 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20  ic", m_gt_0);.  
43de8 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
43de9 65 20 27 6c 27 3a 0a 20 20 20 20 20 73 74 65 6d  e 'l':.     stem
43dea 28 26 7a 2c 20 22 6c 61 63 69 22 2c 20 22 69 63  (&z, "laci", "ic
43deb 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
43dec 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c 75 66     stem(&z, "luf
43ded 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a  ", "", m_gt_0);.
43dee 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
43def 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20 73 74  ase 's':.     st
43df0 65 6d 28 26 7a 2c 20 22 73 73 65 6e 22 2c 20 22  em(&z, "ssen", "
43df1 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
43df2 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f   break;.  }..  /
43df3 2a 20 53 74 65 70 20 34 20 2a 2f 0a 20 20 73 77  * Step 4 */.  sw
43df4 69 74 63 68 28 20 7a 5b 31 5d 20 29 7b 0a 20 20  itch( z[1] ){.  
43df5 20 63 61 73 65 20 27 61 27 3a 0a 20 20 20 20 20   case 'a':.     
43df6 69 66 28 20 7a 5b 30 5d 3d 3d 27 6c 27 20 26 26  if( z[0]=='l' &&
43df7 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a   m_gt_1(z+2) ){.
43df8 20 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20         z += 2;. 
43df9 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b      }.     break
43dfa 3b 0a 20 20 20 63 61 73 65 20 27 63 27 3a 0a 20  ;.   case 'c':. 
43dfb 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65      if( z[0]=='e
43dfc 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 6e 27 20 26  ' && z[2]=='n' &
43dfd 26 20 28 7a 5b 33 5d 3d 3d 27 61 27 20 7c 7c 20  & (z[3]=='a' || 
43dfe 7a 5b 33 5d 3d 3d 27 65 27 29 20 20 26 26 20 6d  z[3]=='e')  && m
43dff 5f 67 74 5f 31 28 7a 2b 34 29 20 20 29 7b 0a 20  _gt_1(z+4)  ){. 
43e00 20 20 20 20 20 20 7a 20 2b 3d 20 34 3b 0a 20 20        z += 4;.  
43e01 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b     }.     break;
43e02 0a 20 20 20 63 61 73 65 20 27 65 27 3a 0a 20 20  .   case 'e':.  
43e03 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 72 27     if( z[0]=='r'
43e04 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20   && m_gt_1(z+2) 
43e05 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 32  ){.       z += 2
43e06 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72  ;.     }.     br
43e07 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 69 27  eak;.   case 'i'
43e08 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
43e09 3d 27 63 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a  ='c' && m_gt_1(z
43e0a 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20  +2) ){.       z 
43e0b 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20  += 2;.     }.   
43e0c 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
43e0d 20 27 6c 27 3a 0a 20 20 20 20 20 69 66 28 20 7a   'l':.     if( z
43e0e 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32 5d  [0]=='e' && z[2]
43e0f 3d 3d 27 62 27 20 26 26 20 28 7a 5b 33 5d 3d 3d  =='b' && (z[3]==
43e10 27 61 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 69 27  'a' || z[3]=='i'
43e11 29 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 34 29  ) && m_gt_1(z+4)
43e12 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20   ){.       z += 
43e13 34 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62  4;.     }.     b
43e14 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6e  reak;.   case 'n
43e15 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d  ':.     if( z[0]
43e16 3d 3d 27 74 27 20 29 7b 0a 20 20 20 20 20 20 20  =='t' ){.       
43e17 69 66 28 20 7a 5b 32 5d 3d 3d 27 61 27 20 29 7b  if( z[2]=='a' ){
43e18 0a 20 20 20 20 20 20 20 20 20 69 66 28 20 6d 5f  .         if( m_
43e19 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20 20  gt_1(z+3) ){.   
43e1a 20 20 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0a          z += 3;.
43e1b 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
43e1c 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b 32 5d    }else if( z[2]
43e1d 3d 3d 27 65 27 20 29 7b 0a 20 20 20 20 20 20 20  =='e' ){.       
43e1e 20 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65 6d    stem(&z, "tnem
43e1f 65 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 20  e", "", m_gt_1) 
43e20 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d  ||.         stem
43e21 28 26 7a 2c 20 22 74 6e 65 6d 22 2c 20 22 22 2c  (&z, "tnem", "",
43e22 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20 20 20 20   m_gt_1) ||.    
43e23 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 74       stem(&z, "t
43e24 6e 65 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29  ne", "", m_gt_1)
43e25 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  ;.       }.     
43e26 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  }.     break;.  
43e27 20 63 61 73 65 20 27 6f 27 3a 0a 20 20 20 20 20   case 'o':.     
43e28 69 66 28 20 7a 5b 30 5d 3d 3d 27 75 27 20 29 7b  if( z[0]=='u' ){
43e29 0a 20 20 20 20 20 20 20 69 66 28 20 6d 5f 67 74  .       if( m_gt
43e2a 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20  _1(z+2) ){.     
43e2b 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20 20      z += 2;.    
43e2c 20 20 20 7d 0a 20 20 20 20 20 7d 65 6c 73 65 20     }.     }else 
43e2d 69 66 28 20 7a 5b 33 5d 3d 3d 27 73 27 20 7c 7c  if( z[3]=='s' ||
43e2e 20 7a 5b 33 5d 3d 3d 27 74 27 20 29 7b 0a 20 20   z[3]=='t' ){.  
43e2f 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6e       stem(&z, "n
43e30 6f 69 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29  oi", "", m_gt_1)
43e31 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72  ;.     }.     br
43e32 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 73 27  eak;.   case 's'
43e33 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
43e34 3d 27 6d 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 69  ='m' && z[2]=='i
43e35 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 33 29  ' && m_gt_1(z+3)
43e36 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20   ){.       z += 
43e37 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62  3;.     }.     b
43e38 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 74  reak;.   case 't
43e39 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ':.     stem(&z,
43e3a 20 22 65 74 61 22 2c 20 22 22 2c 20 6d 5f 67 74   "eta", "", m_gt
43e3b 5f 31 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _1) ||.     stem
43e3c 28 26 7a 2c 20 22 69 74 69 22 2c 20 22 22 2c 20  (&z, "iti", "", 
43e3d 6d 5f 67 74 5f 31 29 3b 0a 20 20 20 20 20 62 72  m_gt_1);.     br
43e3e 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 75 27  eak;.   case 'u'
43e3f 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
43e40 3d 27 73 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 6f  ='s' && z[2]=='o
43e41 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 33 29  ' && m_gt_1(z+3)
43e42 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20   ){.       z += 
43e43 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62  3;.     }.     b
43e44 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 76  reak;.   case 'v
43e45 27 3a 0a 20 20 20 63 61 73 65 20 27 7a 27 3a 0a  ':.   case 'z':.
43e46 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27       if( z[0]=='
43e47 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 69 27 20  e' && z[2]=='i' 
43e48 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29  && m_gt_1(z+3) )
43e49 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b  {.       z += 3;
43e4a 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65  .     }.     bre
43e4b 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74  ak;.  }..  /* St
43e4c 65 70 20 35 61 20 2a 2f 0a 20 20 69 66 28 20 7a  ep 5a */.  if( z
43e4d 5b 30 5d 3d 3d 27 65 27 20 29 7b 0a 20 20 20 20  [0]=='e' ){.    
43e4e 69 66 28 20 6d 5f 67 74 5f 31 28 7a 2b 31 29 20  if( m_gt_1(z+1) 
43e4f 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  ){.      z++;.  
43e50 20 20 7d 65 6c 73 65 20 69 66 28 20 6d 5f 65 71    }else if( m_eq
43e51 5f 31 28 7a 2b 31 29 20 26 26 20 21 73 74 61 72  _1(z+1) && !star
43e52 5f 6f 68 28 7a 2b 31 29 20 29 7b 0a 20 20 20 20  _oh(z+1) ){.    
43e53 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d    z++;.    }.  }
43e54 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 62 20 2a  ..  /* Step 5b *
43e55 2f 0a 20 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a  /.  if( m_gt_1(z
43e56 29 20 26 26 20 7a 5b 30 5d 3d 3d 27 6c 27 20 26  ) && z[0]=='l' &
43e57 26 20 7a 5b 31 5d 3d 3d 27 6c 27 20 29 7b 0a 20  & z[1]=='l' ){. 
43e58 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f     z++;.  }..  /
43e59 2a 20 7a 5b 5d 20 69 73 20 6e 6f 77 20 74 68 65  * z[] is now the
43e5a 20 73 74 65 6d 6d 65 64 20 77 6f 72 64 20 69 6e   stemmed word in
43e5b 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 2e 20   reverse order. 
43e5c 20 46 6c 69 70 20 69 74 20 62 61 63 6b 0a 20 20   Flip it back.  
43e5d 2a 2a 20 61 72 6f 75 6e 64 20 69 6e 74 6f 20 66  ** around into f
43e5e 6f 72 77 61 72 64 20 6f 72 64 65 72 20 61 6e 64  orward order and
43e5f 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20   return..  */.  
43e60 2a 70 6e 4f 75 74 20 3d 20 69 20 3d 20 28 69 6e  *pnOut = i = (in
43e61 74 29 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 7a  t)strlen(z);.  z
43e62 4f 75 74 5b 69 5d 20 3d 20 30 3b 0a 20 20 77 68  Out[i] = 0;.  wh
43e63 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20 20 7a  ile( *z ){.    z
43e64 4f 75 74 5b 2d 2d 69 5d 20 3d 20 2a 28 7a 2b 2b  Out[--i] = *(z++
43e65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
43e66 43 68 61 72 61 63 74 65 72 73 20 74 68 61 74 20  Characters that 
43e67 63 61 6e 20 62 65 20 70 61 72 74 20 6f 66 20 61  can be part of a
43e68 20 74 6f 6b 65 6e 2e 20 20 57 65 20 61 73 73 75   token.  We assu
43e69 6d 65 20 61 6e 79 20 63 68 61 72 61 63 74 65 72  me any character
43e6a 0a 2a 2a 20 77 68 6f 73 65 20 76 61 6c 75 65 20  .** whose value 
43e6b 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
43e6c 30 78 38 30 20 28 61 6e 79 20 55 54 46 20 63 68  0x80 (any UTF ch
43e6d 61 72 61 63 74 65 72 29 20 63 61 6e 20 62 65 0a  aracter) can be.
43e6e 2a 2a 20 70 61 72 74 20 6f 66 20 61 20 74 6f 6b  ** part of a tok
43e6f 65 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  en.  In other wo
43e70 72 64 73 2c 20 64 65 6c 69 6d 69 74 65 72 73 20  rds, delimiters 
43e71 61 6c 6c 20 6d 75 73 74 20 68 61 76 65 0a 2a 2a  all must have.**
43e72 20 76 61 6c 75 65 73 20 6f 66 20 30 78 37 66 20   values of 0x7f 
43e73 6f 72 20 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61  or lower..*/.sta
43e74 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 70  tic const char p
43e75 6f 72 74 65 72 49 64 43 68 61 72 5b 5d 20 3d 20  orterIdChar[] = 
43e76 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20 78 33  {./* x0 x1 x2 x3
43e77 20 78 34 20 78 35 20 78 36 20 78 37 20 78 38 20   x4 x5 x6 x7 x8 
43e78 78 39 20 78 41 20 78 42 20 78 43 20 78 44 20 78  x9 xA xB xC xD x
43e79 45 20 78 46 20 2a 2f 0a 20 20 20 20 31 2c 20 31  E xF */.    1, 1
43e7a 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
43e7b 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
43e7c 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
43e7d 33 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  3x */.    0, 1, 
43e7e 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
43e7f 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
43e80 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78   1, 1, 1,  /* 4x
43e81 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c   */.    1, 1, 1,
43e82 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
43e83 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 1, 1, 0, 0, 0
43e84 2c 20 30 2c 20 31 2c 20 20 2f 2a 20 35 78 20 2a  , 0, 1,  /* 5x *
43e85 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
43e86 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
43e87 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
43e88 31 2c 20 31 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a  1, 1,  /* 6x */.
43e89 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20      1, 1, 1, 1, 
43e8a 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
43e8b 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 0, 0, 0, 0,
43e8c 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b   0,  /* 7x */.};
43e8d 0a 23 64 65 66 69 6e 65 20 69 73 44 65 6c 69 6d  .#define isDelim
43e8e 28 43 29 20 28 28 28 63 68 3d 43 29 26 30 78 38  (C) (((ch=C)&0x8
43e8f 30 29 3d 3d 30 20 26 26 20 28 63 68 3c 30 78 33  0)==0 && (ch<0x3
43e90 30 20 7c 7c 20 21 70 6f 72 74 65 72 49 64 43 68  0 || !porterIdCh
43e91 61 72 5b 63 68 2d 30 78 33 30 5d 29 29 0a 0a 2f  ar[ch-0x30]))../
43e92 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65  *.** Extract the
43e93 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d   next token from
43e94 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20   a tokenization 
43e95 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72  cursor.  The cur
43e96 73 6f 72 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65  sor must.** have
43e97 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 20   been opened by 
43e98 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20  a prior call to 
43e99 70 6f 72 74 65 72 4f 70 65 6e 28 29 2e 0a 2a 2f  porterOpen()..*/
43e9a 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74  .static int port
43e9b 65 72 4e 65 78 74 28 0a 20 20 73 71 6c 69 74 65  erNext(.  sqlite
43e9c 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
43e9d 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a  or *pCursor,  /*
43e9e 20 43 75 72 73 6f 72 20 72 65 74 75 72 6e 65 64   Cursor returned
43e9f 20 62 79 20 70 6f 72 74 65 72 4f 70 65 6e 20 2a   by porterOpen *
43ea0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
43ea1 2a 70 7a 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20  *pzToken,       
43ea2 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
43ea3 2a 70 7a 54 6f 6b 65 6e 20 69 73 20 74 68 65 20  *pzToken is the 
43ea4 74 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20  token text */.  
43ea5 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20 20 20  int *pnBytes,   
43ea6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ea7 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62      /* OUT: Numb
43ea8 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
43ea9 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  oken */.  int *p
43eaa 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 20 20  iStartOffset,   
43eab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
43eac 20 4f 55 54 3a 20 53 74 61 72 74 69 6e 67 20 6f   OUT: Starting o
43ead 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a  ffset of token *
43eae 2f 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66  /.  int *piEndOf
43eaf 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20  fset,           
43eb0 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
43eb1 45 6e 64 69 6e 67 20 6f 66 66 73 65 74 20 6f 66  Ending offset of
43eb2 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
43eb3 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20  *piPosition     
43eb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43eb5 2f 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e  /* OUT: Position
43eb6 20 69 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b 65   integer of toke
43eb7 6e 20 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74 65 72  n */.){.  porter
43eb8 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
43eb9 72 20 2a 63 20 3d 20 28 70 6f 72 74 65 72 5f 74  r *c = (porter_t
43eba 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
43ebb 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 63 6f  *) pCursor;.  co
43ebc 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 63 2d  nst char *z = c-
43ebd 3e 7a 49 6e 70 75 74 3b 0a 0a 20 20 77 68 69 6c  >zInput;..  whil
43ebe 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d  e( c->iOffset<c-
43ebf 3e 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20 20 69  >nInput ){.    i
43ec0 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c  nt iStartOffset,
43ec1 20 63 68 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61   ch;..    /* Sca
43ec2 6e 20 70 61 73 74 20 64 65 6c 69 6d 69 74 65 72  n past delimiter
43ec3 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20   characters */. 
43ec4 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66     while( c->iOf
43ec5 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 26  fset<c->nInput &
43ec6 26 20 69 73 44 65 6c 69 6d 28 7a 5b 63 2d 3e 69  & isDelim(z[c->i
43ec7 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20 20 20  Offset]) ){.    
43ec8 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a    c->iOffset++;.
43ec9 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f      }..    /* Co
43eca 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69 74 65  unt non-delimite
43ecb 72 20 63 68 61 72 61 63 74 65 72 73 2e 20 2a 2f  r characters. */
43ecc 0a 20 20 20 20 69 53 74 61 72 74 4f 66 66 73 65  .    iStartOffse
43ecd 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a  t = c->iOffset;.
43ece 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f      while( c->iO
43ecf 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20  ffset<c->nInput 
43ed0 26 26 20 21 69 73 44 65 6c 69 6d 28 7a 5b 63 2d  && !isDelim(z[c-
43ed1 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20  >iOffset]) ){.  
43ed2 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b      c->iOffset++
43ed3 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
43ed4 20 63 2d 3e 69 4f 66 66 73 65 74 3e 69 53 74 61   c->iOffset>iSta
43ed5 72 74 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20  rtOffset ){.    
43ed6 20 20 69 6e 74 20 6e 20 3d 20 63 2d 3e 69 4f 66    int n = c->iOf
43ed7 66 73 65 74 2d 69 53 74 61 72 74 4f 66 66 73 65  fset-iStartOffse
43ed8 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 63  t;.      if( n>c
43ed9 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 29 7b 0a  ->nAllocated ){.
43eda 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 70 4e          char *pN
43edb 65 77 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e 6e  ew;.        c->n
43edc 41 6c 6c 6f 63 61 74 65 64 20 3d 20 6e 2b 32 30  Allocated = n+20
43edd 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d  ;.        pNew =
43ede 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
43edf 28 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 63 2d 3e 6e  (c->zToken, c->n
43ee0 41 6c 6c 6f 63 61 74 65 64 29 3b 0a 20 20 20 20  Allocated);.    
43ee1 20 20 20 20 69 66 28 20 21 70 4e 65 77 20 29 20      if( !pNew ) 
43ee2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
43ee3 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e  MEM;.        c->
43ee4 7a 54 6f 6b 65 6e 20 3d 20 70 4e 65 77 3b 0a 20  zToken = pNew;. 
43ee5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 6f 72       }.      por
43ee6 74 65 72 5f 73 74 65 6d 6d 65 72 28 26 7a 5b 69  ter_stemmer(&z[i
43ee7 53 74 61 72 74 4f 66 66 73 65 74 5d 2c 20 6e 2c  StartOffset], n,
43ee8 20 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 70 6e 42 79   c->zToken, pnBy
43ee9 74 65 73 29 3b 0a 20 20 20 20 20 20 2a 70 7a 54  tes);.      *pzT
43eea 6f 6b 65 6e 20 3d 20 63 2d 3e 7a 54 6f 6b 65 6e  oken = c->zToken
43eeb 3b 0a 20 20 20 20 20 20 2a 70 69 53 74 61 72 74  ;.      *piStart
43eec 4f 66 66 73 65 74 20 3d 20 69 53 74 61 72 74 4f  Offset = iStartO
43eed 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70 69  ffset;.      *pi
43eee 45 6e 64 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69  EndOffset = c->i
43eef 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70  Offset;.      *p
43ef0 69 50 6f 73 69 74 69 6f 6e 20 3d 20 63 2d 3e 69  iPosition = c->i
43ef1 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 72  Token++;.      r
43ef2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
43ef3 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
43ef4 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
43ef5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65  .}../*.** The se
43ef6 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 74 68  t of routines th
43ef7 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  at implement the
43ef8 20 70 6f 72 74 65 72 2d 73 74 65 6d 6d 65 72 20   porter-stemmer 
43ef9 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61  tokenizer.*/.sta
43efa 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
43efb 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
43efc 6c 65 20 70 6f 72 74 65 72 54 6f 6b 65 6e 69 7a  le porterTokeniz
43efd 65 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30  erModule = {.  0
43efe 2c 0a 20 20 70 6f 72 74 65 72 43 72 65 61 74 65  ,.  porterCreate
43eff 2c 0a 20 20 70 6f 72 74 65 72 44 65 73 74 72 6f  ,.  porterDestro
43f00 79 2c 0a 20 20 70 6f 72 74 65 72 4f 70 65 6e 2c  y,.  porterOpen,
43f01 0a 20 20 70 6f 72 74 65 72 43 6c 6f 73 65 2c 0a  .  porterClose,.
43f02 20 20 70 6f 72 74 65 72 4e 65 78 74 2c 0a 20 20    porterNext,.  
43f03 30 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  0.};../*.** Allo
43f04 63 61 74 65 20 61 20 6e 65 77 20 70 6f 72 74 65  cate a new porte
43f05 72 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 20 52 65  r tokenizer.  Re
43f06 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
43f07 6f 20 74 68 65 20 6e 65 77 0a 2a 2a 20 74 6f 6b  o the new.** tok
43f08 65 6e 69 7a 65 72 20 69 6e 20 2a 70 70 4d 6f 64  enizer in *ppMod
43f09 75 6c 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ule.*/.SQLITE_PR
43f0a 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
43f0b 65 33 46 74 73 33 50 6f 72 74 65 72 54 6f 6b 65  e3Fts3PorterToke
43f0c 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73  nizerModule(.  s
43f0d 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
43f0e 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70  _module const**p
43f0f 70 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70  pModule.){.  *pp
43f10 4d 6f 64 75 6c 65 20 3d 20 26 70 6f 72 74 65 72  Module = &porter
43f11 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b  TokenizerModule;
43f12 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  .}..#endif /* !d
43f13 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
43f14 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
43f15 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
43f16 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  3) */../********
43f17 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74  ****** End of ft
43f18 73 33 5f 70 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a  s3_porter.c ****
43f19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43f1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43f1b 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
43f1c 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
43f1d 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  e fts3_tokenizer
43f1e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
43f1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43f20 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
43f21 37 20 4a 75 6e 65 20 32 32 0a 2a 2a 0a 2a 2a 20  7 June 22.**.** 
43f22 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
43f23 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
43f24 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
43f25 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
43f26 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
43f27 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
43f28 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
43f29 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
43f2a 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
43f2b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
43f2c 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
43f2d 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
43f2e 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
43f2f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
43f30 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
43f31 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
43f32 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
43f33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43f34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43f35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43f36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43f37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
43f38 0a 2a 2a 20 54 68 69 73 20 69 73 20 70 61 72 74  .** This is part
43f39 20 6f 66 20 61 6e 20 53 51 4c 69 74 65 20 6d 6f   of an SQLite mo
43f3a 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e  dule implementin
43f3b 67 20 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72  g full-text sear
43f3c 63 68 2e 0a 2a 2a 20 54 68 69 73 20 70 61 72 74  ch..** This part
43f3d 69 63 75 6c 61 72 20 66 69 6c 65 20 69 6d 70 6c  icular file impl
43f3e 65 6d 65 6e 74 73 20 74 68 65 20 67 65 6e 65 72  ements the gener
43f3f 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 74  ic tokenizer int
43f40 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  erface..*/../*.*
43f41 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
43f42 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20  is file is only 
43f43 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a  compiled if:.**.
43f44 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53  **     * The FTS
43f45 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e  3 module is bein
43f46 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78  g built as an ex
43f47 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20  tension.**      
43f48 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20   (in which case 
43f49 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e  SQLITE_CORE is n
43f4a 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a  ot defined), or.
43f4b 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20  **.**     * The 
43f4c 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62  FTS3 module is b
43f4d 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20  eing built into 
43f4e 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20  the core of.**  
43f4f 20 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20       SQLite (in 
43f50 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54  which case SQLIT
43f51 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73  E_ENABLE_FTS3 is
43f52 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69   defined)..*/.#i
43f53 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
43f54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
43f55 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
43f56 5f 46 54 53 33 29 0a 0a 2f 2a 20 23 69 6e 63 6c  _FTS3)../* #incl
43f57 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a  ude <assert.h> *
43f58 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73  /./* #include <s
43f59 74 72 69 6e 67 2e 68 3e 20 2a 2f 0a 0a 2f 2a 0a  tring.h> */../*.
43f5a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
43f5b 6e 20 6f 66 20 74 68 65 20 53 51 4c 20 73 63 61  n of the SQL sca
43f5c 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  lar function for
43f5d 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 75   accessing the u
43f5e 6e 64 65 72 6c 79 69 6e 67 20 0a 2a 2a 20 68 61  nderlying .** ha
43f5f 73 68 20 74 61 62 6c 65 2e 20 54 68 69 73 20 66  sh table. This f
43f60 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63  unction may be c
43f61 61 6c 6c 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73  alled as follows
43f62 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54  :.**.**   SELECT
43f63 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e   <function-name>
43f64 28 3c 6b 65 79 2d 6e 61 6d 65 3e 29 3b 0a 2a 2a  (<key-name>);.**
43f65 20 20 20 53 45 4c 45 43 54 20 3c 66 75 6e 63 74     SELECT <funct
43f66 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e  ion-name>(<key-n
43f67 61 6d 65 3e 2c 20 3c 70 6f 69 6e 74 65 72 3e 29  ame>, <pointer>)
43f68 3b 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 3c 66  ;.**.** where <f
43f69 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73  unction-name> is
43f6a 20 74 68 65 20 6e 61 6d 65 20 70 61 73 73 65 64   the name passed
43f6b 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
43f6c 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68  rgument.** to th
43f6d 65 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  e sqlite3Fts3Ini
43f6e 74 48 61 73 68 54 61 62 6c 65 28 29 20 66 75 6e  tHashTable() fun
43f6f 63 74 69 6f 6e 20 28 65 2e 67 2e 20 27 66 74 73  ction (e.g. 'fts
43f70 33 5f 74 6f 6b 65 6e 69 7a 65 72 27 29 2e 0a 2a  3_tokenizer')..*
43f71 2a 0a 2a 2a 20 49 66 20 74 68 65 20 3c 70 6f 69  *.** If the <poi
43f72 6e 74 65 72 3e 20 61 72 67 75 6d 65 6e 74 20 69  nter> argument i
43f73 73 20 73 70 65 63 69 66 69 65 64 2c 20 69 74 20  s specified, it 
43f74 6d 75 73 74 20 62 65 20 61 20 62 6c 6f 62 20 76  must be a blob v
43f75 61 6c 75 65 0a 2a 2a 20 63 6f 6e 74 61 69 6e 69  alue.** containi
43f76 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ng a pointer to 
43f77 62 65 20 73 74 6f 72 65 64 20 61 73 20 74 68 65  be stored as the
43f78 20 68 61 73 68 20 64 61 74 61 20 63 6f 72 72 65   hash data corre
43f79 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 74  sponding.** to t
43f7a 68 65 20 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e  he string <key-n
43f7b 61 6d 65 3e 2e 20 49 66 20 3c 70 6f 69 6e 74 65  ame>. If <pointe
43f7c 72 3e 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66  r> is not specif
43f7d 69 65 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65  ied, then.** the
43f7e 20 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d   string <key-nam
43f7f 65 3e 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20  e> must already 
43f80 65 78 69 73 74 20 69 6e 20 74 68 65 20 68 61 73  exist in the has
43f81 20 74 61 62 6c 65 2e 20 4f 74 68 65 72 77 69 73   table. Otherwis
43f82 65 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69  e,.** an error i
43f83 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
43f84 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * Whether or not
43f85 20 74 68 65 20 3c 70 6f 69 6e 74 65 72 3e 20 61   the <pointer> a
43f86 72 67 75 6d 65 6e 74 20 69 73 20 73 70 65 63 69  rgument is speci
43f87 66 69 65 64 2c 20 74 68 65 20 76 61 6c 75 65 20  fied, the value 
43f88 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 61  returned.** is a
43f89 20 62 6c 6f 62 20 63 6f 6e 74 61 69 6e 69 6e 67   blob containing
43f8a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 73 74 6f   the pointer sto
43f8b 72 65 64 20 61 73 20 74 68 65 20 68 61 73 68 20  red as the hash 
43f8c 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e 64 69  data correspondi
43f8d 6e 67 0a 2a 2a 20 74 6f 20 73 74 72 69 6e 67 20  ng.** to string 
43f8e 3c 6b 65 79 2d 6e 61 6d 65 3e 20 28 61 66 74 65  <key-name> (afte
43f8f 72 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65  r the hash-table
43f90 20 69 73 20 75 70 64 61 74 65 64 2c 20 69 66 20   is updated, if 
43f91 61 70 70 6c 69 63 61 62 6c 65 29 2e 0a 2a 2f 0a  applicable)..*/.
43f92 73 74 61 74 69 63 20 76 6f 69 64 20 73 63 61 6c  static void scal
43f93 61 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  arFunc(.  sqlite
43f94 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
43f95 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
43f96 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
43f97 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 46 74 73 33  **argv.){.  Fts3
43f98 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 76  Hash *pHash;.  v
43f99 6f 69 64 20 2a 70 50 74 72 20 3d 20 30 3b 0a 20  oid *pPtr = 0;. 
43f9a 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
43f9b 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69  char *zName;.  i
43f9c 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 20 20 61 73 73  nt nName;..  ass
43f9d 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20  ert( argc==1 || 
43f9e 61 72 67 63 3d 3d 32 20 29 3b 0a 0a 20 20 70 48  argc==2 );..  pH
43f9f 61 73 68 20 3d 20 28 46 74 73 33 48 61 73 68 20  ash = (Fts3Hash 
43fa0 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  *)sqlite3_user_d
43fa1 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20  ata(context);.. 
43fa2 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
43fa3 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
43fa4 5b 30 5d 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20  [0]);.  nName = 
43fa5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
43fa6 74 65 73 28 61 72 67 76 5b 30 5d 29 2b 31 3b 0a  tes(argv[0])+1;.
43fa7 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29  .  if( argc==2 )
43fa8 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 4f 6c 64  {.    void *pOld
43fa9 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71  ;.    int n = sq
43faa 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
43fab 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20  s(argv[1]);.    
43fac 69 66 28 20 6e 21 3d 73 69 7a 65 6f 66 28 70 50  if( n!=sizeof(pP
43fad 74 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  tr) ){.      sql
43fae 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
43faf 72 28 63 6f 6e 74 65 78 74 2c 20 22 61 72 67 75  r(context, "argu
43fb0 6d 65 6e 74 20 74 79 70 65 20 6d 69 73 6d 61 74  ment type mismat
43fb1 63 68 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  ch", -1);.      
43fb2 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
43fb3 20 20 70 50 74 72 20 3d 20 2a 28 76 6f 69 64 20    pPtr = *(void 
43fb4 2a 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  **)sqlite3_value
43fb5 5f 62 6c 6f 62 28 61 72 67 76 5b 31 5d 29 3b 0a  _blob(argv[1]);.
43fb6 20 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74      pOld = sqlit
43fb7 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
43fb8 28 70 48 61 73 68 2c 20 28 76 6f 69 64 20 2a 29  (pHash, (void *)
43fb9 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 50  zName, nName, pP
43fba 74 72 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c  tr);.    if( pOl
43fbb 64 3d 3d 70 50 74 72 20 29 7b 0a 20 20 20 20 20  d==pPtr ){.     
43fbc 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
43fbd 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22  error(context, "
43fbe 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c 20  out of memory", 
43fbf 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  -1);.      retur
43fc0 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  n;.    }.  }else
43fc1 7b 0a 20 20 20 20 70 50 74 72 20 3d 20 73 71 6c  {.    pPtr = sql
43fc2 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64  ite3Fts3HashFind
43fc3 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e  (pHash, zName, n
43fc4 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21  Name);.    if( !
43fc5 70 50 74 72 20 29 7b 0a 20 20 20 20 20 20 63 68  pPtr ){.      ch
43fc6 61 72 20 2a 7a 45 72 72 20 3d 20 73 71 6c 69 74  ar *zErr = sqlit
43fc7 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e  e3_mprintf("unkn
43fc8 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25  own tokenizer: %
43fc9 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
43fca 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
43fcb 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
43fcc 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 20 20 20  zErr, -1);.     
43fcd 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
43fce 72 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  rr);.      retur
43fcf 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  n;.    }.  }..  
43fd0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
43fd1 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 76 6f  lob(context, (vo
43fd2 69 64 20 2a 29 26 70 50 74 72 2c 20 73 69 7a 65  id *)&pPtr, size
43fd3 6f 66 28 70 50 74 72 29 2c 20 53 51 4c 49 54 45  of(pPtr), SQLITE
43fd4 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a  _TRANSIENT);.}..
43fd5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
43fd6 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 49 73  nt sqlite3Fts3Is
43fd7 49 64 43 68 61 72 28 63 68 61 72 20 63 29 7b 0a  IdChar(char c){.
43fd8 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
43fd9 68 61 72 20 69 73 46 74 73 49 64 43 68 61 72 5b  har isFtsIdChar[
43fda 5d 20 3d 20 7b 0a 20 20 20 20 20 20 30 2c 20 30  ] = {.      0, 0
43fdb 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
43fdc 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
43fdd 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
43fde 30 78 20 2a 2f 0a 20 20 20 20 20 20 30 2c 20 30  0x */.      0, 0
43fdf 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
43fe0 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
43fe1 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
43fe2 31 78 20 2a 2f 0a 20 20 20 20 20 20 30 2c 20 30  1x */.      0, 0
43fe3 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c  , 0, 0, 1, 0, 0,
43fe4 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
43fe5 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
43fe6 32 78 20 2a 2f 0a 20 20 20 20 20 20 31 2c 20 31  2x */.      1, 1
43fe7 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
43fe8 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
43fe9 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
43fea 33 78 20 2a 2f 0a 20 20 20 20 20 20 30 2c 20 31  3x */.      0, 1
43feb 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
43fec 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
43fed 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
43fee 34 78 20 2a 2f 0a 20 20 20 20 20 20 31 2c 20 31  4x */.      1, 1
43fef 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
43ff0 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
43ff1 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a 20  0, 0, 0, 1,  /* 
43ff2 35 78 20 2a 2f 0a 20 20 20 20 20 20 30 2c 20 31  5x */.      0, 1
43ff3 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
43ff4 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
43ff5 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
43ff6 36 78 20 2a 2f 0a 20 20 20 20 20 20 31 2c 20 31  6x */.      1, 1
43ff7 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
43ff8 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
43ff9 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
43ffa 37 78 20 2a 2f 0a 20 20 7d 3b 0a 20 20 72 65 74  7x */.  };.  ret
43ffb 75 72 6e 20 28 63 26 30 78 38 30 20 7c 7c 20 69  urn (c&0x80 || i
43ffc 73 46 74 73 49 64 43 68 61 72 5b 28 69 6e 74 29  sFtsIdChar[(int)
43ffd 28 63 29 5d 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45  (c)]);.}..SQLITE
43ffe 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
43fff 68 61 72 20 2a 73 71 6c 69 74 65 33 46 74 73 33  har *sqlite3Fts3
44000 4e 65 78 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20  NextToken(const 
44001 63 68 61 72 20 2a 7a 53 74 72 2c 20 69 6e 74 20  char *zStr, int 
44002 2a 70 6e 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  *pn){.  const ch
44003 61 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74 20  ar *z1;.  const 
44004 63 68 61 72 20 2a 7a 32 20 3d 20 30 3b 0a 0a 20  char *z2 = 0;.. 
44005 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 73 74 61   /* Find the sta
44006 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20 74  rt of the next t
44007 6f 6b 65 6e 2e 20 2a 2f 0a 20 20 7a 31 20 3d 20  oken. */.  z1 = 
44008 7a 53 74 72 3b 0a 20 20 77 68 69 6c 65 28 20 7a  zStr;.  while( z
44009 32 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72  2==0 ){.    char
4400a 20 63 20 3d 20 2a 7a 31 3b 0a 20 20 20 20 73 77   c = *z1;.    sw
4400b 69 74 63 68 28 20 63 20 29 7b 0a 20 20 20 20 20  itch( c ){.     
4400c 20 63 61 73 65 20 27 5c 30 27 3a 20 72 65 74 75   case '\0': retu
4400d 72 6e 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20  rn 0;        /* 
4400e 4e 6f 20 6d 6f 72 65 20 74 6f 6b 65 6e 73 20 68  No more tokens h
4400f 65 72 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ere */.      cas
44010 65 20 27 5c 27 27 3a 0a 20 20 20 20 20 20 63 61  e '\'':.      ca
44011 73 65 20 27 22 27 3a 0a 20 20 20 20 20 20 63 61  se '"':.      ca
44012 73 65 20 27 60 27 3a 20 7b 0a 20 20 20 20 20 20  se '`': {.      
44013 20 20 7a 32 20 3d 20 7a 31 3b 0a 20 20 20 20 20    z2 = z1;.     
44014 20 20 20 77 68 69 6c 65 28 20 2a 2b 2b 7a 32 20     while( *++z2 
44015 26 26 20 28 2a 7a 32 21 3d 63 20 7c 7c 20 2a 2b  && (*z2!=c || *+
44016 2b 7a 32 3d 3d 63 29 20 29 3b 0a 20 20 20 20 20  +z2==c) );.     
44017 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
44018 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 5b 27  }.      case '['
44019 3a 0a 20 20 20 20 20 20 20 20 7a 32 20 3d 20 26  :.        z2 = &
4401a 7a 31 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 77  z1[1];.        w
4401b 68 69 6c 65 28 20 2a 7a 32 20 26 26 20 7a 32 5b  hile( *z2 && z2[
4401c 30 5d 21 3d 27 5d 27 20 29 20 7a 32 2b 2b 3b 0a  0]!=']' ) z2++;.
4401d 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a 32 20          if( *z2 
4401e 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20 20 20 20  ) z2++;.        
4401f 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 64 65  break;..      de
44020 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 69  fault:.        i
44021 66 28 20 73 71 6c 69 74 65 33 46 74 73 33 49 73  f( sqlite3Fts3Is
44022 49 64 43 68 61 72 28 2a 7a 31 29 20 29 7b 0a 20  IdChar(*z1) ){. 
44023 20 20 20 20 20 20 20 20 20 7a 32 20 3d 20 26 7a           z2 = &z
44024 31 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  1[1];.          
44025 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 46 74  while( sqlite3Ft
44026 73 33 49 73 49 64 43 68 61 72 28 2a 7a 32 29 20  s3IsIdChar(*z2) 
44027 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20 20 20 20  ) z2++;.        
44028 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
44029 20 7a 31 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d   z1++;.        }
4402a 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70  .    }.  }..  *p
4402b 6e 20 3d 20 28 69 6e 74 29 28 7a 32 2d 7a 31 29  n = (int)(z2-z1)
4402c 3b 0a 20 20 72 65 74 75 72 6e 20 7a 31 3b 0a 7d  ;.  return z1;.}
4402d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
4402e 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
4402f 49 6e 69 74 54 6f 6b 65 6e 69 7a 65 72 28 0a 20  InitTokenizer(. 
44030 20 46 74 73 33 48 61 73 68 20 2a 70 48 61 73 68   Fts3Hash *pHash
44031 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
44032 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 68 61   /* Tokenizer ha
44033 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f  sh table */.  co
44034 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 2c 20  nst char *zArg, 
44035 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
44036 20 54 6f 6b 65 6e 69 7a 65 72 20 6e 61 6d 65 20   Tokenizer name 
44037 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
44038 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 2c 20  enizer **ppTok, 
44039 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b       /* OUT: Tok
4403a 65 6e 69 7a 65 72 20 28 69 66 20 61 70 70 6c 69  enizer (if appli
4403b 63 61 62 6c 65 29 20 2a 2f 0a 20 20 63 68 61 72  cable) */.  char
4403c 20 2a 2a 70 7a 45 72 72 20 20 20 20 20 20 20 20   **pzErr        
4403d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
4403e 55 54 3a 20 53 65 74 20 74 6f 20 6d 61 6c 6c 6f  UT: Set to mallo
4403f 63 65 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ced error messag
44040 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
44041 3b 0a 20 20 63 68 61 72 20 2a 7a 20 3d 20 28 63  ;.  char *z = (c
44042 68 61 72 20 2a 29 7a 41 72 67 3b 0a 20 20 69 6e  har *)zArg;.  in
44043 74 20 6e 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  t n = 0;.  char 
44044 2a 7a 43 6f 70 79 3b 0a 20 20 63 68 61 72 20 2a  *zCopy;.  char *
44045 7a 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20  zEnd;           
44046 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
44047 6e 74 65 72 20 74 6f 20 6e 75 6c 2d 74 65 72 6d  nter to nul-term
44048 20 6f 66 20 7a 43 6f 70 79 20 2a 2f 0a 20 20 73   of zCopy */.  s
44049 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
4404a 5f 6d 6f 64 75 6c 65 20 2a 6d 3b 0a 0a 20 20 7a  _module *m;..  z
4404b 43 6f 70 79 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Copy = sqlite3_m
4404c 70 72 69 6e 74 66 28 22 25 73 22 2c 20 7a 41 72  printf("%s", zAr
4404d 67 29 3b 0a 20 20 69 66 28 20 21 7a 43 6f 70 79  g);.  if( !zCopy
4404e 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
4404f 5f 4e 4f 4d 45 4d 3b 0a 20 20 7a 45 6e 64 20 3d  _NOMEM;.  zEnd =
44050 20 26 7a 43 6f 70 79 5b 73 74 72 6c 65 6e 28 7a   &zCopy[strlen(z
44051 43 6f 70 79 29 5d 3b 0a 0a 20 20 7a 20 3d 20 28  Copy)];..  z = (
44052 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 46 74  char *)sqlite3Ft
44053 73 33 4e 65 78 74 54 6f 6b 65 6e 28 7a 43 6f 70  s3NextToken(zCop
44054 79 2c 20 26 6e 29 3b 0a 20 20 7a 5b 6e 5d 20 3d  y, &n);.  z[n] =
44055 20 27 5c 30 27 3b 0a 20 20 73 71 6c 69 74 65 33   '\0';.  sqlite3
44056 46 74 73 33 44 65 71 75 6f 74 65 28 7a 29 3b 0a  Fts3Dequote(z);.
44057 0a 20 20 6d 20 3d 20 28 73 71 6c 69 74 65 33 5f  .  m = (sqlite3_
44058 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
44059 20 2a 29 73 71 6c 69 74 65 33 46 74 73 33 48 61   *)sqlite3Fts3Ha
4405a 73 68 46 69 6e 64 28 70 48 61 73 68 2c 7a 2c 28  shFind(pHash,z,(
4405b 69 6e 74 29 73 74 72 6c 65 6e 28 7a 29 2b 31 29  int)strlen(z)+1)
4405c 3b 0a 20 20 69 66 28 20 21 6d 20 29 7b 0a 20 20  ;.  if( !m ){.  
4405d 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
4405e 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e  e3_mprintf("unkn
4405f 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25  own tokenizer: %
44060 73 22 2c 20 7a 29 3b 0a 20 20 20 20 72 63 20 3d  s", z);.    rc =
44061 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
44062 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72   }else{.    char
44063 20 63 6f 6e 73 74 20 2a 2a 61 41 72 67 20 3d 20   const **aArg = 
44064 30 3b 0a 20 20 20 20 69 6e 74 20 69 41 72 67 20  0;.    int iArg 
44065 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 26 7a 5b  = 0;.    z = &z[
44066 6e 2b 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28  n+1];.    while(
44067 20 7a 3c 7a 45 6e 64 20 26 26 20 28 4e 55 4c 4c   z<zEnd && (NULL
44068 21 3d 28 7a 20 3d 20 28 63 68 61 72 20 2a 29 73  !=(z = (char *)s
44069 71 6c 69 74 65 33 46 74 73 33 4e 65 78 74 54 6f  qlite3Fts3NextTo
4406a 6b 65 6e 28 7a 2c 20 26 6e 29 29 29 20 29 7b 0a  ken(z, &n))) ){.
4406b 20 20 20 20 20 20 69 6e 74 20 6e 4e 65 77 20 3d        int nNew =
4406c 20 73 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a   sizeof(char *)*
4406d 28 69 41 72 67 2b 31 29 3b 0a 20 20 20 20 20 20  (iArg+1);.      
4406e 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 61 4e 65  char const **aNe
4406f 77 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  w = (const char 
44070 2a 2a 29 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  **)sqlite3_reall
44071 6f 63 28 28 76 6f 69 64 20 2a 29 61 41 72 67 2c  oc((void *)aArg,
44072 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 69 66   nNew);.      if
44073 28 20 21 61 4e 65 77 20 29 7b 0a 20 20 20 20 20  ( !aNew ){.     
44074 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
44075 7a 43 6f 70 79 29 3b 0a 20 20 20 20 20 20 20 20  zCopy);.        
44076 73 71 6c 69 74 65 33 5f 66 72 65 65 28 28 76 6f  sqlite3_free((vo
44077 69 64 20 2a 29 61 41 72 67 29 3b 0a 20 20 20 20  id *)aArg);.    
44078 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
44079 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
4407a 0a 20 20 20 20 20 20 61 41 72 67 20 3d 20 61 4e  .      aArg = aN
4407b 65 77 3b 0a 20 20 20 20 20 20 61 41 72 67 5b 69  ew;.      aArg[i
4407c 41 72 67 2b 2b 5d 20 3d 20 7a 3b 0a 20 20 20 20  Arg++] = z;.    
4407d 20 20 7a 5b 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20    z[n] = '\0';. 
4407e 20 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33       sqlite3Fts3
4407f 44 65 71 75 6f 74 65 28 7a 29 3b 0a 20 20 20 20  Dequote(z);.    
44080 20 20 7a 20 3d 20 26 7a 5b 6e 2b 31 5d 3b 0a 20    z = &z[n+1];. 
44081 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 6d 2d     }.    rc = m-
44082 3e 78 43 72 65 61 74 65 28 69 41 72 67 2c 20 61  >xCreate(iArg, a
44083 41 72 67 2c 20 70 70 54 6f 6b 29 3b 0a 20 20 20  Arg, ppTok);.   
44084 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
44085 49 54 45 5f 4f 4b 20 7c 7c 20 2a 70 70 54 6f 6b  ITE_OK || *ppTok
44086 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d   );.    if( rc!=
44087 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
44088 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69     *pzErr = sqli
44089 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b  te3_mprintf("unk
4408a 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 22 29  nown tokenizer")
4408b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
4408c 20 20 20 28 2a 70 70 54 6f 6b 29 2d 3e 70 4d 6f     (*ppTok)->pMo
4408d 64 75 6c 65 20 3d 20 6d 3b 20 0a 20 20 20 20 7d  dule = m; .    }
4408e 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
4408f 65 28 28 76 6f 69 64 20 2a 29 61 41 72 67 29 3b  e((void *)aArg);
44090 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
44091 66 72 65 65 28 7a 43 6f 70 79 29 3b 0a 20 20 72  free(zCopy);.  r
44092 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69  eturn rc;.}...#i
44093 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
44094 0a 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 74  ../* #include <t
44095 63 6c 2e 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e 63  cl.h> */./* #inc
44096 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 20  lude <string.h> 
44097 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  */../*.** Implem
44098 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 73 70  entation of a sp
44099 65 63 69 61 6c 20 53 51 4c 20 73 63 61 6c 61 72  ecial SQL scalar
4409a 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 65   function for te
4409b 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 73  sting tokenizers
4409c 20 0a 2a 2a 20 64 65 73 69 67 6e 65 64 20 74 6f   .** designed to
4409d 20 62 65 20 75 73 65 64 20 69 6e 20 63 6f 6e 63   be used in conc
4409e 65 72 74 20 77 69 74 68 20 74 68 65 20 54 63 6c  ert with the Tcl
4409f 20 74 65 73 74 69 6e 67 20 66 72 61 6d 65 77 6f   testing framewo
440a0 72 6b 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63  rk. This.** func
440a1 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 63 61 6c  tion must be cal
440a2 6c 65 64 20 77 69 74 68 20 74 77 6f 20 6f 72 20  led with two or 
440a3 6d 6f 72 65 20 61 72 67 75 6d 65 6e 74 73 3a 0a  more arguments:.
440a4 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c  **.**   SELECT <
440a5 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c  function-name>(<
440a6 6b 65 79 2d 6e 61 6d 65 3e 2c 20 2e 2e 2e 2c 20  key-name>, ..., 
440a7 3c 69 6e 70 75 74 2d 73 74 72 69 6e 67 3e 29 3b  <input-string>);
440a8 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 3c 66 75  .**.** where <fu
440a9 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20  nction-name> is 
440aa 74 68 65 20 6e 61 6d 65 20 70 61 73 73 65 64 20  the name passed 
440ab 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
440ac 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65  gument.** to the
440ad 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74   sqlite3Fts3Init
440ae 48 61 73 68 54 61 62 6c 65 28 29 20 66 75 6e 63  HashTable() func
440af 74 69 6f 6e 20 28 65 2e 67 2e 20 27 66 74 73 33  tion (e.g. 'fts3
440b0 5f 74 6f 6b 65 6e 69 7a 65 72 27 29 0a 2a 2a 20  _tokenizer').** 
440b1 63 6f 6e 63 61 74 65 6e 61 74 65 64 20 77 69 74  concatenated wit
440b2 68 20 74 68 65 20 73 74 72 69 6e 67 20 27 5f 74  h the string '_t
440b3 65 73 74 27 20 28 65 2e 67 2e 20 27 66 74 73 33  est' (e.g. 'fts3
440b4 5f 74 6f 6b 65 6e 69 7a 65 72 5f 74 65 73 74 27  _tokenizer_test'
440b5 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  )..**.** The ret
440b6 75 72 6e 20 76 61 6c 75 65 20 69 73 20 61 20 73  urn value is a s
440b7 74 72 69 6e 67 20 74 68 61 74 20 6d 61 79 20 62  tring that may b
440b8 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73  e interpreted as
440b9 20 61 20 54 63 6c 0a 2a 2a 20 6c 69 73 74 2e 20   a Tcl.** list. 
440ba 46 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e 20 69  For each token i
440bb 6e 20 74 68 65 20 3c 69 6e 70 75 74 2d 73 74 72  n the <input-str
440bc 69 6e 67 3e 2c 20 74 68 72 65 65 20 65 6c 65 6d  ing>, three elem
440bd 65 6e 74 73 20 61 72 65 0a 2a 2a 20 61 64 64 65  ents are.** adde
440be 64 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e 65  d to the returne
440bf 64 20 6c 69 73 74 2e 20 54 68 65 20 66 69 72 73  d list. The firs
440c0 74 20 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 70  t is the token p
440c1 6f 73 69 74 69 6f 6e 2c 20 74 68 65 20 0a 2a 2a  osition, the .**
440c2 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 74   second is the t
440c3 6f 6b 65 6e 20 74 65 78 74 20 28 66 6f 6c 64 65  oken text (folde
440c4 64 2c 20 73 74 65 6d 6d 65 64 2c 20 65 74 63 2e  d, stemmed, etc.
440c5 29 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 20  ) and the third 
440c6 69 73 20 74 68 65 0a 2a 2a 20 73 75 62 73 74 72  is the.** substr
440c7 69 6e 67 20 6f 66 20 3c 69 6e 70 75 74 2d 73 74  ing of <input-st
440c8 72 69 6e 67 3e 20 61 73 73 6f 63 69 61 74 65 64  ring> associated
440c9 20 77 69 74 68 20 74 68 65 20 74 6f 6b 65 6e 2e   with the token.
440ca 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 0a 2a   For example, .*
440cb 2a 20 75 73 69 6e 67 20 74 68 65 20 62 75 69 6c  * using the buil
440cc 74 2d 69 6e 20 22 73 69 6d 70 6c 65 22 20 74 6f  t-in "simple" to
440cd 6b 65 6e 69 7a 65 72 3a 0a 2a 2a 0a 2a 2a 20 20  kenizer:.**.**  
440ce 20 53 45 4c 45 43 54 20 66 74 73 5f 74 6f 6b 65   SELECT fts_toke
440cf 6e 69 7a 65 72 5f 74 65 73 74 28 27 73 69 6d 70  nizer_test('simp
440d0 6c 65 27 2c 20 27 49 20 64 6f 6e 27 74 20 73 65  le', 'I don't se
440d1 65 20 68 6f 77 27 29 3b 0a 2a 2a 0a 2a 2a 20 77  e how');.**.** w
440d2 69 6c 6c 20 72 65 74 75 72 6e 20 74 68 65 20 73  ill return the s
440d3 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 22  tring:.**.**   "
440d4 7b 30 20 69 20 49 20 31 20 64 6f 6e 74 20 64 6f  {0 i I 1 dont do
440d5 6e 27 74 20 32 20 73 65 65 20 73 65 65 20 33 20  n't 2 see see 3 
440d6 68 6f 77 20 68 6f 77 7d 22 0a 2a 2a 20 20 20 0a  how how}".**   .
440d7 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74  */.static void t
440d8 65 73 74 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  estFunc(.  sqlit
440d9 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
440da 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
440db 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
440dc 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 46 74 73   **argv.){.  Fts
440dd 33 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 20 20  3Hash *pHash;.  
440de 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
440df 72 5f 6d 6f 64 75 6c 65 20 2a 70 3b 0a 20 20 73  r_module *p;.  s
440e0 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
440e1 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30   *pTokenizer = 0
440e2 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ;.  sqlite3_toke
440e3 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43  nizer_cursor *pC
440e4 73 72 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74  sr = 0;..  const
440e5 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b   char *zErr = 0;
440e6 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
440e7 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61  zName;.  int nNa
440e8 6d 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  me;.  const char
440e9 20 2a 7a 49 6e 70 75 74 3b 0a 20 20 69 6e 74 20   *zInput;.  int 
440ea 6e 49 6e 70 75 74 3b 0a 0a 20 20 63 6f 6e 73 74  nInput;..  const
440eb 20 63 68 61 72 20 2a 61 7a 41 72 67 5b 36 34 5d   char *azArg[64]
440ec 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ;..  const char 
440ed 2a 7a 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e  *zToken;.  int n
440ee 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 69 6e 74  Token = 0;.  int
440ef 20 69 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 69   iStart = 0;.  i
440f0 6e 74 20 69 45 6e 64 20 3d 20 30 3b 0a 20 20 69  nt iEnd = 0;.  i
440f1 6e 74 20 69 50 6f 73 20 3d 20 30 3b 0a 20 20 69  nt iPos = 0;.  i
440f2 6e 74 20 69 3b 0a 0a 20 20 54 63 6c 5f 4f 62 6a  nt i;..  Tcl_Obj
440f3 20 2a 70 52 65 74 3b 0a 0a 20 20 69 66 28 20 61   *pRet;..  if( a
440f4 72 67 63 3c 32 20 29 7b 0a 20 20 20 20 73 71 6c  rgc<2 ){.    sql
440f5 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
440f6 72 28 63 6f 6e 74 65 78 74 2c 20 22 69 6e 73 75  r(context, "insu
440f7 66 66 69 63 69 65 6e 74 20 61 72 67 75 6d 65 6e  fficient argumen
440f8 74 73 22 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65  ts", -1);.    re
440f9 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 6e 4e 61  turn;.  }..  nNa
440fa 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  me = sqlite3_val
440fb 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
440fc 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f  );.  zName = (co
440fd 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
440fe 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
440ff 67 76 5b 30 5d 29 3b 0a 20 20 6e 49 6e 70 75 74  gv[0]);.  nInput
44100 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
44101 5f 62 79 74 65 73 28 61 72 67 76 5b 61 72 67 63  _bytes(argv[argc
44102 2d 31 5d 29 3b 0a 20 20 7a 49 6e 70 75 74 20 3d  -1]);.  zInput =
44103 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
44104 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
44105 74 28 61 72 67 76 5b 61 72 67 63 2d 31 5d 29 3b  t(argv[argc-1]);
44106 0a 0a 20 20 70 48 61 73 68 20 3d 20 28 46 74 73  ..  pHash = (Fts
44107 33 48 61 73 68 20 2a 29 73 71 6c 69 74 65 33 5f  3Hash *)sqlite3_
44108 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
44109 74 29 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74  t);.  p = (sqlit
4410a 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
4410b 75 6c 65 20 2a 29 73 71 6c 69 74 65 33 46 74 73  ule *)sqlite3Fts
4410c 33 48 61 73 68 46 69 6e 64 28 70 48 61 73 68 2c  3HashFind(pHash,
4410d 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29   zName, nName+1)
4410e 3b 0a 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20  ;..  if( !p ){. 
4410f 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20     char *zErr = 
44110 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
44111 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a  "unknown tokeniz
44112 65 72 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  er: %s", zName);
44113 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
44114 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
44115 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20  t, zErr, -1);.  
44116 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
44117 45 72 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  Err);.    return
44118 3b 0a 20 20 7d 0a 0a 20 20 70 52 65 74 20 3d 20  ;.  }..  pRet = 
44119 54 63 6c 5f 4e 65 77 4f 62 6a 28 29 3b 0a 20 20  Tcl_NewObj();.  
4411a 54 63 6c 5f 49 6e 63 72 52 65 66 43 6f 75 6e 74  Tcl_IncrRefCount
4411b 28 70 52 65 74 29 3b 0a 0a 20 20 66 6f 72 28 69  (pRet);..  for(i
4411c 3d 31 3b 20 69 3c 61 72 67 63 2d 31 3b 20 69 2b  =1; i<argc-1; i+
4411d 2b 29 7b 0a 20 20 20 20 61 7a 41 72 67 5b 69 2d  +){.    azArg[i-
4411e 31 5d 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  1] = (const char
4411f 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
44120 5f 74 65 78 74 28 61 72 67 76 5b 69 5d 29 3b 0a  _text(argv[i]);.
44121 20 20 7d 0a 0a 20 20 69 66 28 20 53 51 4c 49 54    }..  if( SQLIT
44122 45 5f 4f 4b 21 3d 70 2d 3e 78 43 72 65 61 74 65  E_OK!=p->xCreate
44123 28 61 72 67 63 2d 32 2c 20 61 7a 41 72 67 2c 20  (argc-2, azArg, 
44124 26 70 54 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0a  &pTokenizer) ){.
44125 20 20 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f      zErr = "erro
44126 72 20 69 6e 20 78 43 72 65 61 74 65 28 29 22 3b  r in xCreate()";
44127 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68  .    goto finish
44128 3b 0a 20 20 7d 0a 20 20 70 54 6f 6b 65 6e 69 7a  ;.  }.  pTokeniz
44129 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 3b  er->pModule = p;
4412a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 74  .  if( sqlite3Ft
4412b 73 33 4f 70 65 6e 54 6f 6b 65 6e 69 7a 65 72 28  s3OpenTokenizer(
4412c 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 30 2c 20 7a  pTokenizer, 0, z
4412d 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20 26  Input, nInput, &
4412e 70 43 73 72 29 20 29 7b 0a 20 20 20 20 7a 45 72  pCsr) ){.    zEr
4412f 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 4f  r = "error in xO
44130 70 65 6e 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f  pen()";.    goto
44131 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 0a 20 20   finish;.  }..  
44132 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 4f 4b  while( SQLITE_OK
44133 3d 3d 70 2d 3e 78 4e 65 78 74 28 70 43 73 72 2c  ==p->xNext(pCsr,
44134 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65   &zToken, &nToke
44135 6e 2c 20 26 69 53 74 61 72 74 2c 20 26 69 45 6e  n, &iStart, &iEn
44136 64 2c 20 26 69 50 6f 73 29 20 29 7b 0a 20 20 20  d, &iPos) ){.   
44137 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65   Tcl_ListObjAppe
44138 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65  ndElement(0, pRe
44139 74 2c 20 54 63 6c 5f 4e 65 77 49 6e 74 4f 62 6a  t, Tcl_NewIntObj
4413a 28 69 50 6f 73 29 29 3b 0a 20 20 20 20 54 63 6c  (iPos));.    Tcl
4413b 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c  _ListObjAppendEl
4413c 65 6d 65 6e 74 28 30 2c 20 70 52 65 74 2c 20 54  ement(0, pRet, T
4413d 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f 62 6a 28  cl_NewStringObj(
4413e 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 29  zToken, nToken))
4413f 3b 0a 20 20 20 20 7a 54 6f 6b 65 6e 20 3d 20 26  ;.    zToken = &
44140 7a 49 6e 70 75 74 5b 69 53 74 61 72 74 5d 3b 0a  zInput[iStart];.
44141 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 69 45 6e      nToken = iEn
44142 64 2d 69 53 74 61 72 74 3b 0a 20 20 20 20 54 63  d-iStart;.    Tc
44143 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64 45  l_ListObjAppendE
44144 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65 74 2c 20  lement(0, pRet, 
44145 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f 62 6a  Tcl_NewStringObj
44146 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29  (zToken, nToken)
44147 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 53 51  );.  }..  if( SQ
44148 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 43 6c 6f  LITE_OK!=p->xClo
44149 73 65 28 70 43 73 72 29 20 29 7b 0a 20 20 20 20  se(pCsr) ){.    
4414a 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20 69 6e  zErr = "error in
4414b 20 78 43 6c 6f 73 65 28 29 22 3b 0a 20 20 20 20   xClose()";.    
4414c 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d  goto finish;.  }
4414d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
4414e 21 3d 70 2d 3e 78 44 65 73 74 72 6f 79 28 70 54  !=p->xDestroy(pT
4414f 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0a 20 20 20  okenizer) ){.   
44150 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20 69   zErr = "error i
44151 6e 20 78 44 65 73 74 72 6f 79 28 29 22 3b 0a 20  n xDestroy()";. 
44152 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a     goto finish;.
44153 20 20 7d 0a 0a 66 69 6e 69 73 68 3a 0a 20 20 69    }..finish:.  i
44154 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20 20 73  f( zErr ){.    s
44155 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
44156 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72  ror(context, zEr
44157 72 2c 20 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 7b  r, -1);.  }else{
44158 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
44159 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
4415a 2c 20 54 63 6c 5f 47 65 74 53 74 72 69 6e 67 28  , Tcl_GetString(
4415b 70 52 65 74 29 2c 20 2d 31 2c 20 53 51 4c 49 54  pRet), -1, SQLIT
4415c 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
4415d 7d 0a 20 20 54 63 6c 5f 44 65 63 72 52 65 66 43  }.  Tcl_DecrRefC
4415e 6f 75 6e 74 28 70 52 65 74 29 3b 0a 7d 0a 0a 73  ount(pRet);.}..s
4415f 74 61 74 69 63 0a 69 6e 74 20 72 65 67 69 73 74  tatic.int regist
44160 65 72 54 6f 6b 65 6e 69 7a 65 72 28 0a 20 20 73  erTokenizer(.  s
44161 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63  qlite3 *db, .  c
44162 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 63  har *zName, .  c
44163 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
44164 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70  enizer_module *p
44165 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
44166 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
44167 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  tmt;.  const cha
44168 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53 45 4c 45  r zSql[] = "SELE
44169 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65  CT fts3_tokenize
4416a 72 28 3f 2c 20 3f 29 22 3b 0a 0a 20 20 72 63 20  r(?, ?)";..  rc 
4416b 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
4416c 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  e_v2(db, zSql, -
4416d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  1, &pStmt, 0);. 
4416e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
4416f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
44170 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69   rc;.  }..  sqli
44171 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 70 53  te3_bind_text(pS
44172 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d  tmt, 1, zName, -
44173 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
44174 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  );.  sqlite3_bin
44175 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 32 2c  d_blob(pStmt, 2,
44176 20 26 70 2c 20 73 69 7a 65 6f 66 28 70 29 2c 20   &p, sizeof(p), 
44177 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
44178 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70    sqlite3_step(p
44179 53 74 6d 74 29 3b 0a 0a 20 20 72 65 74 75 72 6e  Stmt);..  return
4417a 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
4417b 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 73 74 61  e(pStmt);.}..sta
4417c 74 69 63 0a 69 6e 74 20 71 75 65 72 79 54 6f 6b  tic.int queryTok
4417d 65 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69 74 65  enizer(.  sqlite
4417e 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20 2a  3 *db, .  char *
4417f 7a 4e 61 6d 65 2c 20 20 0a 20 20 63 6f 6e 73 74  zName,  .  const
44180 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
44181 65 72 5f 6d 6f 64 75 6c 65 20 2a 2a 70 70 0a 29  er_module **pp.)
44182 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
44183 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
44184 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  t;.  const char 
44185 7a 53 71 6c 5b 5d 20 3d 20 22 53 45 4c 45 43 54  zSql[] = "SELECT
44186 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28   fts3_tokenizer(
44187 3f 29 22 3b 0a 0a 20 20 2a 70 70 20 3d 20 30 3b  ?)";..  *pp = 0;
44188 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
44189 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a  prepare_v2(db, z
4418a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c  Sql, -1, &pStmt,
4418b 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53   0);.  if( rc!=S
4418c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
4418d 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
4418e 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74    sqlite3_bind_t
4418f 65 78 74 28 70 53 74 6d 74 2c 20 31 2c 20 7a 4e  ext(pStmt, 1, zN
44190 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  ame, -1, SQLITE_
44191 53 54 41 54 49 43 29 3b 0a 20 20 69 66 28 20 53  STATIC);.  if( S
44192 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74  QLITE_ROW==sqlit
44193 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29  e3_step(pStmt) )
44194 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
44195 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53  3_column_type(pS
44196 74 6d 74 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f  tmt, 0)==SQLITE_
44197 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20 6d 65  BLOB ){.      me
44198 6d 63 70 79 28 28 76 6f 69 64 20 2a 29 70 70 2c  mcpy((void *)pp,
44199 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
4419a 62 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29 2c 20  blob(pStmt, 0), 
4419b 73 69 7a 65 6f 66 28 2a 70 70 29 29 3b 0a 20 20  sizeof(*pp));.  
4419c 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
4419d 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  n sqlite3_finali
4419e 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 53 51  ze(pStmt);.}..SQ
4419f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
441a0 64 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d  d sqlite3Fts3Sim
441a1 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  pleTokenizerModu
441a2 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  le(sqlite3_token
441a3 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
441a4 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 0a 2f  t**ppModule);../
441a5 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
441a6 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 61 6c 61  ion of the scala
441a7 72 20 66 75 6e 63 74 69 6f 6e 20 66 74 73 33 5f  r function fts3_
441a8 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65 72 6e  tokenizer_intern
441a9 61 6c 5f 74 65 73 74 28 29 2e 0a 2a 2a 20 54 68  al_test()..** Th
441aa 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
441ab 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20  sed for testing 
441ac 6f 6e 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74 20  only, it is not 
441ad 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 0a  included in the.
441ae 2a 2a 20 62 75 69 6c 64 20 75 6e 6c 65 73 73 20  ** build unless 
441af 53 51 4c 49 54 45 5f 54 45 53 54 20 69 73 20 64  SQLITE_TEST is d
441b0 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  efined..**.** Th
441b1 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69  e purpose of thi
441b2 73 20 69 73 20 74 6f 20 74 65 73 74 20 74 68 61  s is to test tha
441b3 74 20 74 68 65 20 66 74 73 33 5f 74 6f 6b 65 6e  t the fts3_token
441b4 69 7a 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 0a  izer() function.
441b5 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 61  ** can be used a
441b6 73 20 64 65 73 69 67 6e 65 64 20 62 79 20 74 68  s designed by th
441b7 65 20 43 2d 63 6f 64 65 20 69 6e 20 74 68 65 20  e C-code in the 
441b8 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 20 61  queryTokenizer a
441b9 6e 64 0a 2a 2a 20 72 65 67 69 73 74 65 72 54 6f  nd.** registerTo
441ba 6b 65 6e 69 7a 65 72 28 29 20 66 75 6e 63 74 69  kenizer() functi
441bb 6f 6e 73 20 61 62 6f 76 65 2e 20 54 68 65 73 65  ons above. These
441bc 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61   two functions a
441bd 72 65 20 72 65 70 65 61 74 65 64 0a 2a 2a 20 69  re repeated.** i
441be 6e 20 74 68 65 20 52 45 41 44 4d 45 2e 74 6f 6b  n the README.tok
441bf 65 6e 69 7a 65 72 20 66 69 6c 65 20 61 73 20 61  enizer file as a
441c0 6e 20 65 78 61 6d 70 6c 65 2c 20 73 6f 20 69 74  n example, so it
441c1 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f   is important to
441c2 0a 2a 2a 20 74 65 73 74 20 74 68 65 6d 2e 0a 2a  .** test them..*
441c3 2a 0a 2a 2a 20 54 6f 20 72 75 6e 20 74 68 65 20  *.** To run the 
441c4 74 65 73 74 73 2c 20 65 76 61 6c 75 61 74 65 20  tests, evaluate 
441c5 74 68 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  the fts3_tokeniz
441c6 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74  er_internal_test
441c7 28 29 20 73 63 61 6c 61 72 0a 2a 2a 20 66 75 6e  () scalar.** fun
441c8 63 74 69 6f 6e 20 77 69 74 68 20 6e 6f 20 61 72  ction with no ar
441c9 67 75 6d 65 6e 74 73 2e 20 41 6e 20 61 73 73 65  guments. An asse
441ca 72 74 28 29 20 77 69 6c 6c 20 66 61 69 6c 20 69  rt() will fail i
441cb 66 20 61 20 70 72 6f 62 6c 65 6d 20 69 73 0a 2a  f a problem is.*
441cc 2a 20 64 65 74 65 63 74 65 64 2e 20 69 2e 65 2e  * detected. i.e.
441cd 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45  :.**.**     SELE
441ce 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65  CT fts3_tokenize
441cf 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74 28  r_internal_test(
441d0 29 3b 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20  );.**.*/.static 
441d1 76 6f 69 64 20 69 6e 74 54 65 73 74 46 75 6e 63  void intTestFunc
441d2 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
441d3 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
441d4 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
441d5 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
441d6 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
441d7 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
441d8 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
441d9 70 31 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  p1;.  const sqli
441da 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
441db 64 75 6c 65 20 2a 70 32 3b 0a 20 20 73 71 6c 69  dule *p2;.  sqli
441dc 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74  te3 *db = (sqlit
441dd 65 33 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65  e3 *)sqlite3_use
441de 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b  r_data(context);
441df 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
441e0 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 55 4e  ETER(argc);.  UN
441e1 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
441e2 72 67 76 29 3b 0a 0a 20 20 2f 2a 20 54 65 73 74  rgv);..  /* Test
441e3 20 74 68 65 20 71 75 65 72 79 20 66 75 6e 63 74   the query funct
441e4 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ion */.  sqlite3
441e5 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69  Fts3SimpleTokeni
441e6 7a 65 72 4d 6f 64 75 6c 65 28 26 70 31 29 3b 0a  zerModule(&p1);.
441e7 20 20 72 63 20 3d 20 71 75 65 72 79 54 6f 6b 65    rc = queryToke
441e8 6e 69 7a 65 72 28 64 62 2c 20 22 73 69 6d 70 6c  nizer(db, "simpl
441e9 65 22 2c 20 26 70 32 29 3b 0a 20 20 61 73 73 65  e", &p2);.  asse
441ea 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
441eb 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  K );.  assert( p
441ec 31 3d 3d 70 32 20 29 3b 0a 20 20 72 63 20 3d 20  1==p2 );.  rc = 
441ed 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28 64  queryTokenizer(d
441ee 62 2c 20 22 6e 6f 73 75 63 68 74 6f 6b 65 6e 69  b, "nosuchtokeni
441ef 7a 65 72 22 2c 20 26 70 32 29 3b 0a 20 20 61 73  zer", &p2);.  as
441f0 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
441f1 5f 45 52 52 4f 52 20 29 3b 0a 20 20 61 73 73 65  _ERROR );.  asse
441f2 72 74 28 20 70 32 3d 3d 30 20 29 3b 0a 20 20 61  rt( p2==0 );.  a
441f3 73 73 65 72 74 28 20 30 3d 3d 73 74 72 63 6d 70  ssert( 0==strcmp
441f4 28 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  (sqlite3_errmsg(
441f5 64 62 29 2c 20 22 75 6e 6b 6e 6f 77 6e 20 74 6f  db), "unknown to
441f6 6b 65 6e 69 7a 65 72 3a 20 6e 6f 73 75 63 68 74  kenizer: nosucht
441f7 6f 6b 65 6e 69 7a 65 72 22 29 20 29 3b 0a 0a 20  okenizer") );.. 
441f8 20 2f 2a 20 54 65 73 74 20 74 68 65 20 73 74 6f   /* Test the sto
441f9 72 61 67 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  rage function */
441fa 0a 20 20 72 63 20 3d 20 72 65 67 69 73 74 65 72  .  rc = register
441fb 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 6e  Tokenizer(db, "n
441fc 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 2c  osuchtokenizer",
441fd 20 70 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20   p1);.  assert( 
441fe 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
441ff 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54 6f 6b  .  rc = queryTok
44200 65 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f 73 75  enizer(db, "nosu
44201 63 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20 26 70  chtokenizer", &p
44202 32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  2);.  assert( rc
44203 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
44204 20 61 73 73 65 72 74 28 20 70 32 3d 3d 70 31 20   assert( p2==p1 
44205 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  );..  sqlite3_re
44206 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
44207 74 2c 20 22 6f 6b 22 2c 20 2d 31 2c 20 53 51 4c  t, "ok", -1, SQL
44208 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a  ITE_STATIC);.}..
44209 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65  #endif../*.** Se
4420a 74 20 75 70 20 53 51 4c 20 6f 62 6a 65 63 74 73  t up SQL objects
4420b 20 69 6e 20 64 61 74 61 62 61 73 65 20 64 62 20   in database db 
4420c 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74  used to access t
4420d 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a  he contents of.*
4420e 2a 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  * the hash table
4420f 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61   pointed to by a
44210 72 67 75 6d 65 6e 74 20 70 48 61 73 68 2e 20 54  rgument pHash. T
44211 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 75  he hash table mu
44212 73 74 0a 2a 2a 20 62 65 65 6e 20 69 6e 69 74 69  st.** been initi
44213 61 6c 69 73 65 64 20 74 6f 20 75 73 65 20 73 74  alised to use st
44214 72 69 6e 67 20 6b 65 79 73 2c 20 61 6e 64 20 74  ring keys, and t
44215 6f 20 74 61 6b 65 20 61 20 70 72 69 76 61 74 65  o take a private
44216 20 63 6f 70 79 20 0a 2a 2a 20 6f 66 20 74 68 65   copy .** of the
44217 20 6b 65 79 20 77 68 65 6e 20 61 20 76 61 6c 75   key when a valu
44218 65 20 69 73 20 69 6e 73 65 72 74 65 64 2e 20 69  e is inserted. i
44219 2e 65 2e 20 62 79 20 61 20 63 61 6c 6c 20 73 69  .e. by a call si
4421a 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20  milar to:.**.** 
4421b 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61     sqlite3Fts3Ha
4421c 73 68 49 6e 69 74 28 70 48 61 73 68 2c 20 46 54  shInit(pHash, FT
4421d 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20  S3_HASH_STRING, 
4421e 31 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  1);.**.** This f
4421f 75 6e 63 74 69 6f 6e 20 61 64 64 73 20 61 20 73  unction adds a s
44220 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 28  calar function (
44221 73 65 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65  see header comme
44222 6e 74 20 61 62 6f 76 65 0a 2a 2a 20 73 63 61 6c  nt above.** scal
44223 61 72 46 75 6e 63 28 29 20 69 6e 20 74 68 69 73  arFunc() in this
44224 20 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c   file for detail
44225 73 29 20 61 6e 64 2c 20 69 66 20 45 4e 41 42 4c  s) and, if ENABL
44226 45 5f 54 41 42 4c 45 20 69 73 0a 2a 2a 20 64 65  E_TABLE is.** de
44227 66 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 61  fined at compila
44228 74 69 6f 6e 20 74 69 6d 65 2c 20 61 20 74 65 6d  tion time, a tem
44229 70 6f 72 61 72 79 20 76 69 72 74 75 61 6c 20 74  porary virtual t
4422a 61 62 6c 65 20 28 73 65 65 20 68 65 61 64 65 72  able (see header
4422b 20 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 61 62 6f   .** comment abo
4422c 76 65 20 73 74 72 75 63 74 20 48 61 73 68 54 61  ve struct HashTa
4422d 62 6c 65 56 74 61 62 29 20 74 6f 20 74 68 65 20  bleVtab) to the 
4422e 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e  database schema.
4422f 20 42 6f 74 68 20 0a 2a 2a 20 70 72 6f 76 69 64   Both .** provid
44230 65 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63  e read/write acc
44231 65 73 73 20 74 6f 20 74 68 65 20 63 6f 6e 74 65  ess to the conte
44232 6e 74 73 20 6f 66 20 2a 70 48 61 73 68 2e 0a 2a  nts of *pHash..*
44233 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61  *.** The third a
44234 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20  rgument to this 
44235 66 75 6e 63 74 69 6f 6e 2c 20 7a 4e 61 6d 65 2c  function, zName,
44236 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20   is used as the 
44237 6e 61 6d 65 0a 2a 2a 20 6f 66 20 62 6f 74 68 20  name.** of both 
44238 74 68 65 20 73 63 61 6c 61 72 20 61 6e 64 2c 20  the scalar and, 
44239 69 66 20 63 72 65 61 74 65 64 2c 20 74 68 65 20  if created, the 
4423a 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
4423b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
4423c 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
4423d 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28 0a 20  InitHashTable(. 
4423e 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20   sqlite3 *db, . 
4423f 20 46 74 73 33 48 61 73 68 20 2a 70 48 61 73 68   Fts3Hash *pHash
44240 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
44241 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20 69 6e 74 20  *zName.){.  int 
44242 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
44243 20 20 76 6f 69 64 20 2a 70 20 3d 20 28 76 6f 69    void *p = (voi
44244 64 20 2a 29 70 48 61 73 68 3b 0a 20 20 63 6f 6e  d *)pHash;.  con
44245 73 74 20 69 6e 74 20 61 6e 79 20 3d 20 53 51 4c  st int any = SQL
44246 49 54 45 5f 41 4e 59 3b 0a 0a 23 69 66 64 65 66  ITE_ANY;..#ifdef
44247 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 63   SQLITE_TEST.  c
44248 68 61 72 20 2a 7a 54 65 73 74 20 3d 20 30 3b 0a  har *zTest = 0;.
44249 20 20 63 68 61 72 20 2a 7a 54 65 73 74 32 20 3d    char *zTest2 =
4424a 20 30 3b 0a 20 20 76 6f 69 64 20 2a 70 64 62 20   0;.  void *pdb 
4424b 3d 20 28 76 6f 69 64 20 2a 29 64 62 3b 0a 20 20  = (void *)db;.  
4424c 7a 54 65 73 74 20 3d 20 73 71 6c 69 74 65 33 5f  zTest = sqlite3_
4424d 6d 70 72 69 6e 74 66 28 22 25 73 5f 74 65 73 74  mprintf("%s_test
4424e 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7a 54 65  ", zName);.  zTe
4424f 73 74 32 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  st2 = sqlite3_mp
44250 72 69 6e 74 66 28 22 25 73 5f 69 6e 74 65 72 6e  rintf("%s_intern
44251 61 6c 5f 74 65 73 74 22 2c 20 7a 4e 61 6d 65 29  al_test", zName)
44252 3b 0a 20 20 69 66 28 20 21 7a 54 65 73 74 20 7c  ;.  if( !zTest |
44253 7c 20 21 7a 54 65 73 74 32 20 29 7b 0a 20 20 20  | !zTest2 ){.   
44254 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
44255 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  EM;.  }.#endif..
44256 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
44257 3d 72 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  =rc ){.    rc = 
44258 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
44259 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d  unction(db, zNam
4425a 65 2c 20 31 2c 20 61 6e 79 2c 20 70 2c 20 73 63  e, 1, any, p, sc
4425b 61 6c 61 72 46 75 6e 63 2c 20 30 2c 20 30 29 3b  alarFunc, 0, 0);
4425c 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54  .  }.  if( SQLIT
4425d 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20  E_OK==rc ){.    
4425e 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65  rc = sqlite3_cre
4425f 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c  ate_function(db,
44260 20 7a 4e 61 6d 65 2c 20 32 2c 20 61 6e 79 2c 20   zName, 2, any, 
44261 70 2c 20 73 63 61 6c 61 72 46 75 6e 63 2c 20 30  p, scalarFunc, 0
44262 2c 20 30 29 3b 0a 20 20 7d 0a 23 69 66 64 65 66  , 0);.  }.#ifdef
44263 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69   SQLITE_TEST.  i
44264 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63  f( SQLITE_OK==rc
44265 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
44266 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
44267 74 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 2c 20  tion(db, zTest, 
44268 2d 31 2c 20 61 6e 79 2c 20 70 2c 20 74 65 73 74  -1, any, p, test
44269 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20 7d  Func, 0, 0);.  }
4426a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
4426b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 72 63 20 3d  ==rc ){.    rc =
4426c 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
4426d 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54 65  function(db, zTe
4426e 73 74 32 2c 20 30 2c 20 61 6e 79 2c 20 70 64 62  st2, 0, any, pdb
4426f 2c 20 69 6e 74 54 65 73 74 46 75 6e 63 2c 20 30  , intTestFunc, 0
44270 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  , 0);.  }.#endif
44271 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
44272 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 66  TEST.  sqlite3_f
44273 72 65 65 28 7a 54 65 73 74 29 3b 0a 20 20 73 71  ree(zTest);.  sq
44274 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65 73 74  lite3_free(zTest
44275 32 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65  2);.#endif..  re
44276 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64  turn rc;.}..#end
44277 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
44278 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
44279 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
4427a 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f  ABLE_FTS3) */../
4427b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
4427c 6e 64 20 6f 66 20 66 74 73 33 5f 74 6f 6b 65 6e  nd of fts3_token
4427d 69 7a 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  izer.c *********
4427e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4427f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
44280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
44281 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 74  egin file fts3_t
44282 6f 6b 65 6e 69 7a 65 72 31 2e 63 20 2a 2a 2a 2a  okenizer1.c ****
44283 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44284 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
44285 2a 0a 2a 2a 20 32 30 30 36 20 4f 63 74 20 31 30  *.** 2006 Oct 10
44286 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
44287 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
44288 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
44289 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
4428a 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
4428b 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
4428c 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
4428d 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
4428e 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
4428f 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
44290 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
44291 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
44292 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
44293 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
44294 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
44295 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
44296 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
44297 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
44298 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44299 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4429a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4429b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4429c 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65  ****.**.** Imple
4429d 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
4429e 20 22 73 69 6d 70 6c 65 22 20 66 75 6c 6c 2d 74   "simple" full-t
4429f 65 78 74 2d 73 65 61 72 63 68 20 74 6f 6b 65 6e  ext-search token
442a0 69 7a 65 72 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  izer..*/../*.** 
442a1 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
442a2 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f   file is only co
442a3 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a  mpiled if:.**.**
442a4 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20       * The FTS3 
442a5 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20  module is being 
442a6 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74 65  built as an exte
442a7 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28  nsion.**       (
442a8 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51  in which case SQ
442a9 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74  LITE_CORE is not
442aa 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a   defined), or.**
442ab 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54  .**     * The FT
442ac 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69  S3 module is bei
442ad 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68  ng built into th
442ae 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20  e core of.**    
442af 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68     SQLite (in wh
442b0 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f  ich case SQLITE_
442b1 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64  ENABLE_FTS3 is d
442b2 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20  efined)..*/.#if 
442b3 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
442b4 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64  CORE) || defined
442b5 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
442b6 54 53 33 29 0a 0a 2f 2a 20 23 69 6e 63 6c 75 64  TS3)../* #includ
442b7 65 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a 2f 0a  e <assert.h> */.
442b8 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74 64  /* #include <std
442b9 6c 69 62 2e 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e  lib.h> */./* #in
442ba 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e 20  clude <stdio.h> 
442bb 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c  */./* #include <
442bc 73 74 72 69 6e 67 2e 68 3e 20 2a 2f 0a 0a 0a 74  string.h> */...t
442bd 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 69  ypedef struct si
442be 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 7b  mple_tokenizer {
442bf 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
442c0 69 7a 65 72 20 62 61 73 65 3b 0a 20 20 63 68 61  izer base;.  cha
442c1 72 20 64 65 6c 69 6d 5b 31 32 38 5d 3b 20 20 20  r delim[128];   
442c2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61            /* fla
442c3 67 20 41 53 43 49 49 20 64 65 6c 69 6d 69 74 65  g ASCII delimite
442c4 72 73 20 2a 2f 0a 7d 20 73 69 6d 70 6c 65 5f 74  rs */.} simple_t
442c5 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 74 79 70 65 64  okenizer;..typed
442c6 65 66 20 73 74 72 75 63 74 20 73 69 6d 70 6c 65  ef struct simple
442c7 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
442c8 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  r {.  sqlite3_to
442c9 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 62  kenizer_cursor b
442ca 61 73 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ase;.  const cha
442cb 72 20 2a 70 49 6e 70 75 74 3b 20 20 20 20 20 20  r *pInput;      
442cc 20 20 20 20 2f 2a 20 69 6e 70 75 74 20 77 65 20      /* input we 
442cd 61 72 65 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 2a  are tokenizing *
442ce 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 3b 20  /.  int nBytes; 
442cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
442d0 20 2f 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20   /* size of the 
442d1 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 69  input */.  int i
442d2 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
442d3 20 20 20 20 20 20 20 20 2f 2a 20 63 75 72 72 65          /* curre
442d4 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 70  nt position in p
442d5 49 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 69  Input */.  int i
442d6 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20  Token;          
442d7 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 64 65 78          /* index
442d8 20 6f 66 20 6e 65 78 74 20 74 6f 6b 65 6e 20 74   of next token t
442d9 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f  o be returned */
442da 0a 20 20 63 68 61 72 20 2a 70 54 6f 6b 65 6e 3b  .  char *pToken;
442db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
442dc 2f 2a 20 73 74 6f 72 61 67 65 20 66 6f 72 20 63  /* storage for c
442dd 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a  urrent token */.
442de 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 41 6c 6c 6f    int nTokenAllo
442df 63 61 74 65 64 3b 20 20 20 20 20 20 20 20 20 2f  cated;         /
442e0 2a 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65  * space allocate
442e1 64 20 74 6f 20 7a 54 6f 6b 65 6e 20 62 75 66 66  d to zToken buff
442e2 65 72 20 2a 2f 0a 7d 20 73 69 6d 70 6c 65 5f 74  er */.} simple_t
442e3 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 3b  okenizer_cursor;
442e4 0a 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69  ...static int si
442e5 6d 70 6c 65 44 65 6c 69 6d 28 73 69 6d 70 6c 65  mpleDelim(simple
442e6 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74 2c 20 75  _tokenizer *t, u
442e7 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 29 7b  nsigned char c){
442e8 0a 20 20 72 65 74 75 72 6e 20 63 3c 30 78 38 30  .  return c<0x80
442e9 20 26 26 20 74 2d 3e 64 65 6c 69 6d 5b 63 5d 3b   && t->delim[c];
442ea 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  .}.static int ft
442eb 73 33 5f 69 73 61 6c 6e 75 6d 28 69 6e 74 20 78  s3_isalnum(int x
442ec 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 78 3e 3d  ){.  return (x>=
442ed 27 30 27 20 26 26 20 78 3c 3d 27 39 27 29 20 7c  '0' && x<='9') |
442ee 7c 20 28 78 3e 3d 27 41 27 20 26 26 20 78 3c 3d  | (x>='A' && x<=
442ef 27 5a 27 29 20 7c 7c 20 28 78 3e 3d 27 61 27 20  'Z') || (x>='a' 
442f0 26 26 20 78 3c 3d 27 7a 27 29 3b 0a 7d 0a 0a 2f  && x<='z');.}../
442f1 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
442f2 77 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74  w tokenizer inst
442f3 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ance..*/.static 
442f4 69 6e 74 20 73 69 6d 70 6c 65 43 72 65 61 74 65  int simpleCreate
442f5 28 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f  (.  int argc, co
442f6 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74  nst char * const
442f7 20 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65   *argv,.  sqlite
442f8 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70  3_tokenizer **pp
442f9 54 6f 6b 65 6e 69 7a 65 72 0a 29 7b 0a 20 20 73  Tokenizer.){.  s
442fa 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20  imple_tokenizer 
442fb 2a 74 3b 0a 0a 20 20 74 20 3d 20 28 73 69 6d 70  *t;..  t = (simp
442fc 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20  le_tokenizer *) 
442fd 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
442fe 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 20 20 69 66  izeof(*t));.  if
442ff 28 20 74 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75  ( t==NULL ) retu
44300 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
44301 0a 20 20 6d 65 6d 73 65 74 28 74 2c 20 30 2c 20  .  memset(t, 0, 
44302 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 0a 20 20  sizeof(*t));..  
44303 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 44  /* TODO(shess) D
44304 65 6c 69 6d 69 74 65 72 73 20 6e 65 65 64 20 74  elimiters need t
44305 6f 20 72 65 6d 61 69 6e 20 74 68 65 20 73 61 6d  o remain the sam
44306 65 20 66 72 6f 6d 20 72 75 6e 20 74 6f 20 72 75  e from run to ru
44307 6e 2c 0a 20 20 2a 2a 20 65 6c 73 65 20 77 65 20  n,.  ** else we 
44308 6e 65 65 64 20 74 6f 20 72 65 69 6e 64 65 78 2e  need to reindex.
44309 20 20 4f 6e 65 20 73 6f 6c 75 74 69 6f 6e 20 77    One solution w
4430a 6f 75 6c 64 20 62 65 20 61 20 6d 65 74 61 2d 74  ould be a meta-t
4430b 61 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 74 72 61  able to.  ** tra
4430c 63 6b 20 73 75 63 68 20 69 6e 66 6f 72 6d 61 74  ck such informat
4430d 69 6f 6e 20 69 6e 20 74 68 65 20 64 61 74 61 62  ion in the datab
4430e 61 73 65 2c 20 74 68 65 6e 20 77 65 27 64 20 6f  ase, then we'd o
4430f 6e 6c 79 20 77 61 6e 74 20 74 68 69 73 0a 20 20  nly want this.  
44310 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  ** information o
44311 6e 20 74 68 65 20 69 6e 69 74 69 61 6c 20 63 72  n the initial cr
44312 65 61 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  eate..  */.  if(
44313 20 61 72 67 63 3e 31 20 29 7b 0a 20 20 20 20 69   argc>1 ){.    i
44314 6e 74 20 69 2c 20 6e 20 3d 20 28 69 6e 74 29 73  nt i, n = (int)s
44315 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d 29 3b 0a  trlen(argv[1]);.
44316 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
44317 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e  ; i++){.      un
44318 73 69 67 6e 65 64 20 63 68 61 72 20 63 68 20 3d  signed char ch =
44319 20 61 72 67 76 5b 31 5d 5b 69 5d 3b 0a 20 20 20   argv[1][i];.   
4431a 20 20 20 2f 2a 20 57 65 20 65 78 70 6c 69 63 69     /* We explici
4431b 74 6c 79 20 64 6f 6e 27 74 20 73 75 70 70 6f 72  tly don't suppor
4431c 74 20 55 54 46 2d 38 20 64 65 6c 69 6d 69 74 65  t UTF-8 delimite
4431d 72 73 20 66 6f 72 20 6e 6f 77 2e 20 2a 2f 0a 20  rs for now. */. 
4431e 20 20 20 20 20 69 66 28 20 63 68 3e 3d 30 78 38       if( ch>=0x8
4431f 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
44320 69 74 65 33 5f 66 72 65 65 28 74 29 3b 0a 20 20  ite3_free(t);.  
44321 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
44322 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
44323 20 7d 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c 69   }.      t->deli
44324 6d 5b 63 68 5d 20 3d 20 31 3b 0a 20 20 20 20 7d  m[ch] = 1;.    }
44325 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
44326 2f 2a 20 4d 61 72 6b 20 6e 6f 6e 2d 61 6c 70 68  /* Mark non-alph
44327 61 6e 75 6d 65 72 69 63 20 41 53 43 49 49 20 63  anumeric ASCII c
44328 68 61 72 61 63 74 65 72 73 20 61 73 20 64 65 6c  haracters as del
44329 69 6d 69 74 65 72 73 20 2a 2f 0a 20 20 20 20 69  imiters */.    i
4432a 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
4432b 31 3b 20 69 3c 30 78 38 30 3b 20 69 2b 2b 29 7b  1; i<0x80; i++){
4432c 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c 69 6d 5b  .      t->delim[
4432d 69 5d 20 3d 20 21 66 74 73 33 5f 69 73 61 6c 6e  i] = !fts3_isaln
4432e 75 6d 28 69 29 20 3f 20 2d 31 20 3a 20 30 3b 0a  um(i) ? -1 : 0;.
4432f 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 70      }.  }..  *pp
44330 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 26 74 2d 3e  Tokenizer = &t->
44331 62 61 73 65 3b 0a 20 20 72 65 74 75 72 6e 20 53  base;.  return S
44332 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
44333 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 74 6f 6b  ** Destroy a tok
44334 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63  enizer.*/.static
44335 20 69 6e 74 20 73 69 6d 70 6c 65 44 65 73 74 72   int simpleDestr
44336 6f 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  oy(sqlite3_token
44337 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
44338 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ){.  sqlite3_fre
44339 65 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20  e(pTokenizer);. 
4433a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
4433b 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70  K;.}../*.** Prep
4433c 61 72 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b  are to begin tok
4433d 65 6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63  enizing a partic
4433e 75 6c 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68  ular string.  Th
4433f 65 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e  e input.** strin
44340 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65  g to be tokenize
44341 64 20 69 73 20 70 49 6e 70 75 74 5b 30 2e 2e 6e  d is pInput[0..n
44342 42 79 74 65 73 2d 31 5d 2e 20 20 41 20 63 75 72  Bytes-1].  A cur
44343 73 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69  sor.** used to i
44344 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b  ncrementally tok
44345 65 6e 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e  enize this strin
44346 67 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  g is returned in
44347 20 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a   .** *ppCursor..
44348 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69  */.static int si
44349 6d 70 6c 65 4f 70 65 6e 28 0a 20 20 73 71 6c 69  mpleOpen(.  sqli
4434a 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
4434b 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20  Tokenizer,      
4434c 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69     /* The tokeni
4434d 7a 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  zer */.  const c
4434e 68 61 72 20 2a 70 49 6e 70 75 74 2c 20 69 6e 74  har *pInput, int
4434f 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20   nBytes,        
44350 2f 2a 20 53 74 72 69 6e 67 20 74 6f 20 62 65 20  /* String to be 
44351 74 6f 6b 65 6e 69 7a 65 64 20 2a 2f 0a 20 20 73  tokenized */.  s
44352 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
44353 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73  _cursor **ppCurs
44354 6f 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f  or    /* OUT: To
44355 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f  kenization curso
44356 72 20 2a 2f 0a 29 7b 0a 20 20 73 69 6d 70 6c 65  r */.){.  simple
44357 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
44358 72 20 2a 63 3b 0a 0a 20 20 55 4e 55 53 45 44 5f  r *c;..  UNUSED_
44359 50 41 52 41 4d 45 54 45 52 28 70 54 6f 6b 65 6e  PARAMETER(pToken
4435a 69 7a 65 72 29 3b 0a 0a 20 20 63 20 3d 20 28 73  izer);..  c = (s
4435b 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f  imple_tokenizer_
4435c 63 75 72 73 6f 72 20 2a 29 20 73 71 6c 69 74 65  cursor *) sqlite
4435d 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
4435e 2a 63 29 29 3b 0a 20 20 69 66 28 20 63 3d 3d 4e  *c));.  if( c==N
4435f 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ULL ) return SQL
44360 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 63 2d  ITE_NOMEM;..  c-
44361 3e 70 49 6e 70 75 74 20 3d 20 70 49 6e 70 75 74  >pInput = pInput
44362 3b 0a 20 20 69 66 28 20 70 49 6e 70 75 74 3d 3d  ;.  if( pInput==
44363 30 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 42 79 74  0 ){.    c->nByt
44364 65 73 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20  es = 0;.  }else 
44365 69 66 28 20 6e 42 79 74 65 73 3c 30 20 29 7b 0a  if( nBytes<0 ){.
44366 20 20 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20      c->nBytes = 
44367 28 69 6e 74 29 73 74 72 6c 65 6e 28 70 49 6e 70  (int)strlen(pInp
44368 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ut);.  }else{.  
44369 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20 6e 42    c->nBytes = nB
4436a 79 74 65 73 3b 0a 20 20 7d 0a 20 20 63 2d 3e 69  ytes;.  }.  c->i
4436b 4f 66 66 73 65 74 20 3d 20 30 3b 20 20 20 20 20  Offset = 0;     
4436c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
4436d 74 61 72 74 20 74 6f 6b 65 6e 69 7a 69 6e 67 20  tart tokenizing 
4436e 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
4436f 20 2a 2f 0a 20 20 63 2d 3e 69 54 6f 6b 65 6e 20   */.  c->iToken 
44370 3d 20 30 3b 0a 20 20 63 2d 3e 70 54 6f 6b 65 6e  = 0;.  c->pToken
44371 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20   = NULL;        
44372 20 20 20 20 20 20 20 2f 2a 20 6e 6f 20 73 70 61         /* no spa
44373 63 65 20 61 6c 6c 6f 63 61 74 65 64 2c 20 79 65  ce allocated, ye
44374 74 2e 20 2a 2f 0a 20 20 63 2d 3e 6e 54 6f 6b 65  t. */.  c->nToke
44375 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 30 3b 0a  nAllocated = 0;.
44376 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 26  .  *ppCursor = &
44377 63 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74 75 72  c->base;.  retur
44378 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
44379 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74 6f  /*.** Close a to
4437a 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f  kenization curso
4437b 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65  r previously ope
4437c 6e 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  ned by a call to
4437d 0a 2a 2a 20 73 69 6d 70 6c 65 4f 70 65 6e 28 29  .** simpleOpen()
4437e 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74 69   above..*/.stati
4437f 63 20 69 6e 74 20 73 69 6d 70 6c 65 43 6c 6f 73  c int simpleClos
44380 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
44381 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
44382 73 6f 72 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74  sor){.  simple_t
44383 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
44384 2a 63 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b  *c = (simple_tok
44385 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29  enizer_cursor *)
44386 20 70 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c 69   pCursor;.  sqli
44387 74 65 33 5f 66 72 65 65 28 63 2d 3e 70 54 6f 6b  te3_free(c->pTok
44388 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  en);.  sqlite3_f
44389 72 65 65 28 63 29 3b 0a 20 20 72 65 74 75 72 6e  ree(c);.  return
4438a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
4438b 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65  *.** Extract the
4438c 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d   next token from
4438d 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20   a tokenization 
4438e 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72  cursor.  The cur
4438f 73 6f 72 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65  sor must.** have
44390 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 20   been opened by 
44391 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20  a prior call to 
44392 73 69 6d 70 6c 65 4f 70 65 6e 28 29 2e 0a 2a 2f  simpleOpen()..*/
44393 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70  .static int simp
44394 6c 65 4e 65 78 74 28 0a 20 20 73 71 6c 69 74 65  leNext(.  sqlite
44395 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
44396 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a  or *pCursor,  /*
44397 20 43 75 72 73 6f 72 20 72 65 74 75 72 6e 65 64   Cursor returned
44398 20 62 79 20 73 69 6d 70 6c 65 4f 70 65 6e 20 2a   by simpleOpen *
44399 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
4439a 2a 70 70 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20  *ppToken,       
4439b 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
4439c 2a 70 70 54 6f 6b 65 6e 20 69 73 20 74 68 65 20  *ppToken is the 
4439d 74 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20  token text */.  
4439e 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20 20 20  int *pnBytes,   
4439f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
443a0 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62      /* OUT: Numb
443a1 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
443a2 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  oken */.  int *p
443a3 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 20 20  iStartOffset,   
443a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
443a5 20 4f 55 54 3a 20 53 74 61 72 74 69 6e 67 20 6f   OUT: Starting o
443a6 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a  ffset of token *
443a7 2f 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66  /.  int *piEndOf
443a8 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20  fset,           
443a9 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
443aa 45 6e 64 69 6e 67 20 6f 66 66 73 65 74 20 6f 66  Ending offset of
443ab 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
443ac 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20  *piPosition     
443ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
443ae 2f 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e  /* OUT: Position
443af 20 69 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b 65   integer of toke
443b0 6e 20 2a 2f 0a 29 7b 0a 20 20 73 69 6d 70 6c 65  n */.){.  simple
443b1 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
443b2 72 20 2a 63 20 3d 20 28 73 69 6d 70 6c 65 5f 74  r *c = (simple_t
443b3 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
443b4 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 73 69  *) pCursor;.  si
443b5 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  mple_tokenizer *
443b6 74 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65  t = (simple_toke
443b7 6e 69 7a 65 72 20 2a 29 20 70 43 75 72 73 6f 72  nizer *) pCursor
443b8 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20  ->pTokenizer;.  
443b9 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
443ba 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
443bb 72 20 2a 29 63 2d 3e 70 49 6e 70 75 74 3b 0a 0a  r *)c->pInput;..
443bc 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66    while( c->iOff
443bd 73 65 74 3c 63 2d 3e 6e 42 79 74 65 73 20 29 7b  set<c->nBytes ){
443be 0a 20 20 20 20 69 6e 74 20 69 53 74 61 72 74 4f  .    int iStartO
443bf 66 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a 20 53  ffset;..    /* S
443c0 63 61 6e 20 70 61 73 74 20 64 65 6c 69 6d 69 74  can past delimit
443c1 65 72 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f  er characters */
443c2 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69  .    while( c->i
443c3 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74 65 73  Offset<c->nBytes
443c4 20 26 26 20 73 69 6d 70 6c 65 44 65 6c 69 6d 28   && simpleDelim(
443c5 74 2c 20 70 5b 63 2d 3e 69 4f 66 66 73 65 74 5d  t, p[c->iOffset]
443c6 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f  ) ){.      c->iO
443c7 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a  ffset++;.    }..
443c8 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 6e 6f 6e      /* Count non
443c9 2d 64 65 6c 69 6d 69 74 65 72 20 63 68 61 72 61  -delimiter chara
443ca 63 74 65 72 73 2e 20 2a 2f 0a 20 20 20 20 69 53  cters. */.    iS
443cb 74 61 72 74 4f 66 66 73 65 74 20 3d 20 63 2d 3e  tartOffset = c->
443cc 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69  iOffset;.    whi
443cd 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63  le( c->iOffset<c
443ce 2d 3e 6e 42 79 74 65 73 20 26 26 20 21 73 69 6d  ->nBytes && !sim
443cf 70 6c 65 44 65 6c 69 6d 28 74 2c 20 70 5b 63 2d  pleDelim(t, p[c-
443d0 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20  >iOffset]) ){.  
443d1 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b      c->iOffset++
443d2 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
443d3 20 63 2d 3e 69 4f 66 66 73 65 74 3e 69 53 74 61   c->iOffset>iSta
443d4 72 74 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20  rtOffset ){.    
443d5 20 20 69 6e 74 20 69 2c 20 6e 20 3d 20 63 2d 3e    int i, n = c->
443d6 69 4f 66 66 73 65 74 2d 69 53 74 61 72 74 4f 66  iOffset-iStartOf
443d7 66 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20  fset;.      if( 
443d8 6e 3e 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63  n>c->nTokenAlloc
443d9 61 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  ated ){.        
443da 63 68 61 72 20 2a 70 4e 65 77 3b 0a 20 20 20 20  char *pNew;.    
443db 20 20 20 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c      c->nTokenAll
443dc 6f 63 61 74 65 64 20 3d 20 6e 2b 32 30 3b 0a 20  ocated = n+20;. 
443dd 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71         pNew = sq
443de 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 63 2d  lite3_realloc(c-
443df 3e 70 54 6f 6b 65 6e 2c 20 63 2d 3e 6e 54 6f 6b  >pToken, c->nTok
443e0 65 6e 41 6c 6c 6f 63 61 74 65 64 29 3b 0a 20 20  enAllocated);.  
443e1 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77 20        if( !pNew 
443e2 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
443e3 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 63  NOMEM;.        c
443e4 2d 3e 70 54 6f 6b 65 6e 20 3d 20 70 4e 65 77 3b  ->pToken = pNew;
443e5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
443e6 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b  or(i=0; i<n; i++
443e7 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 4f  ){.        /* TO
443e8 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 6e  DO(shess) This n
443e9 65 65 64 73 20 65 78 70 61 6e 73 69 6f 6e 20 74  eeds expansion t
443ea 6f 20 68 61 6e 64 6c 65 20 55 54 46 2d 38 0a 20  o handle UTF-8. 
443eb 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65 2d 69         ** case-i
443ec 6e 73 65 6e 73 69 74 69 76 69 74 79 2e 0a 20 20  nsensitivity..  
443ed 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
443ee 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
443ef 68 20 3d 20 70 5b 69 53 74 61 72 74 4f 66 66 73  h = p[iStartOffs
443f0 65 74 2b 69 5d 3b 0a 20 20 20 20 20 20 20 20 63  et+i];.        c
443f1 2d 3e 70 54 6f 6b 65 6e 5b 69 5d 20 3d 20 28 63  ->pToken[i] = (c
443f2 68 61 72 29 28 28 63 68 3e 3d 27 41 27 20 26 26  har)((ch>='A' &&
443f3 20 63 68 3c 3d 27 5a 27 29 20 3f 20 63 68 2d 27   ch<='Z') ? ch-'
443f4 41 27 2b 27 61 27 20 3a 20 63 68 29 3b 0a 20 20  A'+'a' : ch);.  
443f5 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 70 54      }.      *ppT
443f6 6f 6b 65 6e 20 3d 20 63 2d 3e 70 54 6f 6b 65 6e  oken = c->pToken
443f7 3b 0a 20 20 20 20 20 20 2a 70 6e 42 79 74 65 73  ;.      *pnBytes
443f8 20 3d 20 6e 3b 0a 20 20 20 20 20 20 2a 70 69 53   = n;.      *piS
443f9 74 61 72 74 4f 66 66 73 65 74 20 3d 20 69 53 74  tartOffset = iSt
443fa 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  artOffset;.     
443fb 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20   *piEndOffset = 
443fc 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20  c->iOffset;.    
443fd 20 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20    *piPosition = 
443fe 63 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20  c->iToken++;..  
443ff 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
44400 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  E_OK;.    }.  }.
44401 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
44402 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  DONE;.}../*.** T
44403 68 65 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e  he set of routin
44404 65 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  es that implemen
44405 74 20 74 68 65 20 73 69 6d 70 6c 65 20 74 6f 6b  t the simple tok
44406 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63  enizer.*/.static
44407 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
44408 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
44409 73 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d  simpleTokenizerM
4440a 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 0a 20  odule = {.  0,. 
4440b 20 73 69 6d 70 6c 65 43 72 65 61 74 65 2c 0a 20   simpleCreate,. 
4440c 20 73 69 6d 70 6c 65 44 65 73 74 72 6f 79 2c 0a   simpleDestroy,.
4440d 20 20 73 69 6d 70 6c 65 4f 70 65 6e 2c 0a 20 20    simpleOpen,.  
4440e 73 69 6d 70 6c 65 43 6c 6f 73 65 2c 0a 20 20 73  simpleClose,.  s
4440f 69 6d 70 6c 65 4e 65 78 74 2c 0a 20 20 30 2c 0a  impleNext,.  0,.
44410 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  };../*.** Alloca
44411 74 65 20 61 20 6e 65 77 20 73 69 6d 70 6c 65 20  te a new simple 
44412 74 6f 6b 65 6e 69 7a 65 72 2e 20 20 52 65 74 75  tokenizer.  Retu
44413 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
44414 74 68 65 20 6e 65 77 0a 2a 2a 20 74 6f 6b 65 6e  the new.** token
44415 69 7a 65 72 20 69 6e 20 2a 70 70 4d 6f 64 75 6c  izer in *ppModul
44416 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  e.*/.SQLITE_PRIV
44417 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
44418 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69  Fts3SimpleTokeni
44419 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c  zerModule(.  sql
4441a 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
4441b 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d  odule const**ppM
4441c 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f  odule.){.  *ppMo
4441d 64 75 6c 65 20 3d 20 26 73 69 6d 70 6c 65 54 6f  dule = &simpleTo
4441e 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d  kenizerModule;.}
4441f 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
44420 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
44421 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
44422 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
44423 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
44424 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33  **** End of fts3
44425 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20 2a 2a  _tokenizer1.c **
44426 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44427 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44428 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
44429 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
4442a 66 74 73 33 5f 77 72 69 74 65 2e 63 20 2a 2a 2a  fts3_write.c ***
4442b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4442c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4442d 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20  ***/./*.** 2009 
4442e 4f 63 74 20 32 33 0a 2a 2a 0a 2a 2a 20 54 68 65  Oct 23.**.** The
4442f 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
44430 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
44431 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
44432 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
44433 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
44434 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
44435 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
44436 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
44437 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
44438 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
44439 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
4443a 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
4443b 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
4443c 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
4443d 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
4443e 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
4443f 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
44440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44441 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44442 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44443 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44444 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
44445 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 70 61   This file is pa
44446 72 74 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65  rt of the SQLite
44447 20 46 54 53 33 20 65 78 74 65 6e 73 69 6f 6e 20   FTS3 extension 
44448 6d 6f 64 75 6c 65 2e 20 53 70 65 63 69 66 69 63  module. Specific
44449 61 6c 6c 79 2c 0a 2a 2a 20 74 68 69 73 20 66 69  ally,.** this fi
4444a 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
4444b 20 74 6f 20 69 6e 73 65 72 74 2c 20 75 70 64 61   to insert, upda
4444c 74 65 20 61 6e 64 20 64 65 6c 65 74 65 20 72 6f  te and delete ro
4444d 77 73 20 66 72 6f 6d 20 46 54 53 33 0a 2a 2a 20  ws from FTS3.** 
4444e 74 61 62 6c 65 73 2e 20 49 74 20 61 6c 73 6f 20  tables. It also 
4444f 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f  contains code to
44450 20 6d 65 72 67 65 20 46 54 53 33 20 62 2d 74 72   merge FTS3 b-tr
44451 65 65 20 73 65 67 6d 65 6e 74 73 2e 20 53 6f 6d  ee segments. Som
44452 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 75 62 2d  e.** of the sub-
44453 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f  routines used to
44454 20 6d 65 72 67 65 20 73 65 67 6d 65 6e 74 73 20   merge segments 
44455 61 72 65 20 61 6c 73 6f 20 75 73 65 64 20 62 79  are also used by
44456 20 74 68 65 20 71 75 65 72 79 20 0a 2a 2a 20 63   the query .** c
44457 6f 64 65 20 69 6e 20 66 74 73 33 2e 63 2e 0a 2a  ode in fts3.c..*
44458 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
44459 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
4445a 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
4445b 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 2f 2a 20  NABLE_FTS3)../* 
4445c 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67  #include <string
4445d 2e 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75  .h> */./* #inclu
4445e 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a 2f  de <assert.h> */
4445f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74  ./* #include <st
44460 64 6c 69 62 2e 68 3e 20 2a 2f 0a 0a 0a 23 64 65  dlib.h> */...#de
44461 66 69 6e 65 20 46 54 53 5f 4d 41 58 5f 41 50 50  fine FTS_MAX_APP
44462 45 4e 44 41 42 4c 45 5f 48 45 49 47 48 54 20 31  ENDABLE_HEIGHT 1
44463 36 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 66 75  6../*.** When fu
44464 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 20 6e 6f  ll-text index no
44465 64 65 73 20 61 72 65 20 6c 6f 61 64 65 64 20 66  des are loaded f
44466 72 6f 6d 20 64 69 73 6b 2c 20 74 68 65 20 62 75  rom disk, the bu
44467 66 66 65 72 20 74 68 61 74 20 74 68 65 79 0a 2a  ffer that they.*
44468 2a 20 61 72 65 20 6c 6f 61 64 65 64 20 69 6e 74  * are loaded int
44469 6f 20 68 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77  o has the follow
4446a 69 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  ing number of by
4446b 74 65 73 20 6f 66 20 70 61 64 64 69 6e 67 20 61  tes of padding a
4446c 74 20 74 68 65 20 65 6e 64 20 0a 2a 2a 20 6f 66  t the end .** of
4446d 20 69 74 2e 20 69 2e 65 2e 20 69 66 20 61 20 66   it. i.e. if a f
4446e 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 20 6e  ull-text index n
4446f 6f 64 65 20 69 73 20 39 30 30 20 62 79 74 65 73  ode is 900 bytes
44470 20 69 6e 20 73 69 7a 65 2c 20 74 68 65 6e 20 61   in size, then a
44471 20 62 75 66 66 65 72 0a 2a 2a 20 6f 66 20 39 32   buffer.** of 92
44472 30 20 62 79 74 65 73 20 69 73 20 61 6c 6c 6f 63  0 bytes is alloc
44473 61 74 65 64 20 66 6f 72 20 69 74 2e 0a 2a 2a 0a  ated for it..**.
44474 2a 2a 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68  ** This means th
44475 61 74 20 69 66 20 77 65 20 68 61 76 65 20 61 20  at if we have a 
44476 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61 20 62  pointer into a b
44477 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  uffer containing
44478 20 6e 6f 64 65 20 64 61 74 61 2c 0a 2a 2a 20 69   node data,.** i
44479 74 20 69 73 20 61 6c 77 61 79 73 20 73 61 66 65  t is always safe
4447a 20 74 6f 20 72 65 61 64 20 75 70 20 74 6f 20 74   to read up to t
4447b 77 6f 20 76 61 72 69 6e 74 73 20 66 72 6f 6d 20  wo varints from 
4447c 69 74 20 77 69 74 68 6f 75 74 20 72 69 73 6b 69  it without riski
4447d 6e 67 20 61 6e 0a 2a 2a 20 6f 76 65 72 72 65 61  ng an.** overrea
4447e 64 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20 6e  d, even if the n
4447f 6f 64 65 20 64 61 74 61 20 69 73 20 63 6f 72 72  ode data is corr
44480 75 70 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  upted..*/.#defin
44481 65 20 46 54 53 33 5f 4e 4f 44 45 5f 50 41 44 44  e FTS3_NODE_PADD
44482 49 4e 47 20 28 46 54 53 33 5f 56 41 52 49 4e 54  ING (FTS3_VARINT
44483 5f 4d 41 58 2a 32 29 0a 0a 2f 2a 0a 2a 2a 20 55  _MAX*2)../*.** U
44484 6e 64 65 72 20 63 65 72 74 61 69 6e 20 63 69 72  nder certain cir
44485 63 75 6d 73 74 61 6e 63 65 73 2c 20 62 2d 74 72  cumstances, b-tr
44486 65 65 20 6e 6f 64 65 73 20 28 64 6f 63 6c 69 73  ee nodes (doclis
44487 74 73 29 20 63 61 6e 20 62 65 20 6c 6f 61 64 65  ts) can be loade
44488 64 20 69 6e 74 6f 0a 2a 2a 20 6d 65 6d 6f 72 79  d into.** memory
44489 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 69   incrementally i
4448a 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c 20 61 74  nstead of all at
4448b 20 6f 6e 63 65 2e 20 54 68 69 73 20 63 61 6e 20   once. This can 
4448c 62 65 20 61 20 62 69 67 20 70 65 72 66 6f 72 6d  be a big perform
4448d 61 6e 63 65 0a 2a 2a 20 77 69 6e 20 28 72 65 64  ance.** win (red
4448e 75 63 65 64 20 49 4f 20 61 6e 64 20 43 50 55 29  uced IO and CPU)
4448f 20 69 66 20 53 51 4c 69 74 65 20 73 74 6f 70 73   if SQLite stops
44490 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 76 69 72   calling the vir
44491 74 75 61 6c 20 74 61 62 6c 65 20 78 4e 65 78 74  tual table xNext
44492 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 62 65 66  ().** method bef
44493 6f 72 65 20 72 65 74 72 69 65 76 69 6e 67 20 61  ore retrieving a
44494 6c 6c 20 71 75 65 72 79 20 72 65 73 75 6c 74 73  ll query results
44495 20 28 61 73 20 6d 61 79 20 68 61 70 70 65 6e 2c   (as may happen,
44496 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a   for example,.**
44497 20 69 66 20 61 20 71 75 65 72 79 20 68 61 73 20   if a query has 
44498 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 29 2e  a LIMIT clause).
44499 0a 2a 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74  .**.** Increment
4449a 61 6c 20 6c 6f 61 64 69 6e 67 20 69 73 20 75 73  al loading is us
4449b 65 64 20 66 6f 72 20 62 2d 74 72 65 65 20 6e 6f  ed for b-tree no
4449c 64 65 73 20 46 54 53 33 5f 4e 4f 44 45 5f 43 48  des FTS3_NODE_CH
4449d 55 4e 4b 5f 54 48 52 45 53 48 4f 4c 44 20 0a 2a  UNK_THRESHOLD .*
4449e 2a 20 62 79 74 65 73 20 61 6e 64 20 6c 61 72 67  * bytes and larg
4449f 65 72 2e 20 4e 6f 64 65 73 20 61 72 65 20 6c 6f  er. Nodes are lo
444a0 61 64 65 64 20 69 6e 20 63 68 75 6e 6b 73 20 6f  aded in chunks o
444a1 66 20 46 54 53 33 5f 4e 4f 44 45 5f 43 48 55 4e  f FTS3_NODE_CHUN
444a2 4b 53 49 5a 45 20 62 79 74 65 73 2e 0a 2a 2a 20  KSIZE bytes..** 
444a3 54 68 65 20 63 6f 64 65 20 69 73 20 77 72 69 74  The code is writ
444a4 74 65 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20  ten so that the 
444a5 68 61 72 64 20 6c 6f 77 65 72 2d 6c 69 6d 69 74  hard lower-limit
444a6 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 65   for each of the
444a7 73 65 20 76 61 6c 75 65 73 20 0a 2a 2a 20 69 73  se values .** is
444a8 20 31 2e 20 43 6c 65 61 72 6c 79 20 73 75 63 68   1. Clearly such
444a9 20 73 6d 61 6c 6c 20 76 61 6c 75 65 73 20 77 6f   small values wo
444aa 75 6c 64 20 62 65 20 69 6e 65 66 66 69 63 69 65  uld be inefficie
444ab 6e 74 2c 20 62 75 74 20 63 61 6e 20 62 65 20 75  nt, but can be u
444ac 73 65 66 75 6c 20 0a 2a 2a 20 66 6f 72 20 74 65  seful .** for te
444ad 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 2e 0a  sting purposes..
444ae 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 6d 6f  **.** If this mo
444af 64 75 6c 65 20 69 73 20 62 75 69 6c 74 20 77 69  dule is built wi
444b0 74 68 20 53 51 4c 49 54 45 5f 54 45 53 54 20 64  th SQLITE_TEST d
444b1 65 66 69 6e 65 64 2c 20 74 68 65 73 65 20 63 6f  efined, these co
444b2 6e 73 74 61 6e 74 73 20 6d 61 79 0a 2a 2a 20 62  nstants may.** b
444b3 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61 74 20  e overridden at 
444b4 72 75 6e 74 69 6d 65 20 66 6f 72 20 74 65 73 74  runtime for test
444b5 69 6e 67 20 70 75 72 70 6f 73 65 73 2e 20 46 69  ing purposes. Fi
444b6 6c 65 20 66 74 73 33 5f 74 65 73 74 2e 63 20 63  le fts3_test.c c
444b7 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 54 63 6c  ontains.** a Tcl
444b8 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 72 65   interface to re
444b9 61 64 20 61 6e 64 20 77 72 69 74 65 20 74 68 65  ad and write the
444ba 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 64   values..*/.#ifd
444bb 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 69  ef SQLITE_TEST.i
444bc 6e 74 20 74 65 73 74 5f 66 74 73 33 5f 6e 6f 64  nt test_fts3_nod
444bd 65 5f 63 68 75 6e 6b 73 69 7a 65 20 3d 20 28 34  e_chunksize = (4
444be 2a 31 30 32 34 29 3b 0a 69 6e 74 20 74 65 73 74  *1024);.int test
444bf 5f 66 74 73 33 5f 6e 6f 64 65 5f 63 68 75 6e 6b  _fts3_node_chunk
444c0 5f 74 68 72 65 73 68 6f 6c 64 20 3d 20 28 34 2a  _threshold = (4*
444c1 31 30 32 34 29 2a 34 3b 0a 23 20 64 65 66 69 6e  1024)*4;.# defin
444c2 65 20 46 54 53 33 5f 4e 4f 44 45 5f 43 48 55 4e  e FTS3_NODE_CHUN
444c3 4b 53 49 5a 45 20 20 20 20 20 20 20 74 65 73 74  KSIZE       test
444c4 5f 66 74 73 33 5f 6e 6f 64 65 5f 63 68 75 6e 6b  _fts3_node_chunk
444c5 73 69 7a 65 0a 23 20 64 65 66 69 6e 65 20 46 54  size.# define FT
444c6 53 33 5f 4e 4f 44 45 5f 43 48 55 4e 4b 5f 54 48  S3_NODE_CHUNK_TH
444c7 52 45 53 48 4f 4c 44 20 74 65 73 74 5f 66 74 73  RESHOLD test_fts
444c8 33 5f 6e 6f 64 65 5f 63 68 75 6e 6b 5f 74 68 72  3_node_chunk_thr
444c9 65 73 68 6f 6c 64 0a 23 65 6c 73 65 0a 23 20 64  eshold.#else.# d
444ca 65 66 69 6e 65 20 46 54 53 33 5f 4e 4f 44 45 5f  efine FTS3_NODE_
444cb 43 48 55 4e 4b 53 49 5a 45 20 28 34 2a 31 30 32  CHUNKSIZE (4*102
444cc 34 29 20 0a 23 20 64 65 66 69 6e 65 20 46 54 53  4) .# define FTS
444cd 33 5f 4e 4f 44 45 5f 43 48 55 4e 4b 5f 54 48 52  3_NODE_CHUNK_THR
444ce 45 53 48 4f 4c 44 20 28 46 54 53 33 5f 4e 4f 44  ESHOLD (FTS3_NOD
444cf 45 5f 43 48 55 4e 4b 53 49 5a 45 2a 34 29 0a 23  E_CHUNKSIZE*4).#
444d0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
444d1 20 74 77 6f 20 76 61 6c 75 65 73 20 74 68 61 74   two values that
444d2 20 6d 61 79 20 62 65 20 6d 65 61 6e 69 6e 67 66   may be meaningf
444d3 75 6c 6c 79 20 62 6f 75 6e 64 20 74 6f 20 74 68  ully bound to th
444d4 65 20 3a 31 20 70 61 72 61 6d 65 74 65 72 20 69  e :1 parameter i
444d5 6e 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20  n.** statements 
444d6 53 51 4c 5f 52 45 50 4c 41 43 45 5f 53 54 41 54  SQL_REPLACE_STAT
444d7 20 61 6e 64 20 53 51 4c 5f 53 45 4c 45 43 54 5f   and SQL_SELECT_
444d8 53 54 41 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  STAT..*/.#define
444d9 20 46 54 53 5f 53 54 41 54 5f 44 4f 43 54 4f 54   FTS_STAT_DOCTOT
444da 41 4c 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e  AL      0.#defin
444db 65 20 46 54 53 5f 53 54 41 54 5f 49 4e 43 52 4d  e FTS_STAT_INCRM
444dc 45 52 47 45 48 49 4e 54 20 31 0a 23 64 65 66 69  ERGEHINT 1.#defi
444dd 6e 65 20 46 54 53 5f 53 54 41 54 5f 41 55 54 4f  ne FTS_STAT_AUTO
444de 49 4e 43 52 4d 45 52 47 45 20 32 0a 0a 2f 2a 0a  INCRMERGE 2../*.
444df 2a 2a 20 49 66 20 46 54 53 5f 4c 4f 47 5f 4d 45  ** If FTS_LOG_ME
444e0 52 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 2c  RGES is defined,
444e1 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 6c 6f   call sqlite3_lo
444e2 67 28 29 20 74 6f 20 72 65 70 6f 72 74 20 65 61  g() to report ea
444e3 63 68 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20  ch automatic.** 
444e4 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20  and incremental 
444e5 6d 65 72 67 65 20 6f 70 65 72 61 74 69 6f 6e 20  merge operation 
444e6 74 68 61 74 20 74 61 6b 65 73 20 70 6c 61 63 65  that takes place
444e7 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 66  . This is used f
444e8 6f 72 20 0a 2a 2a 20 64 65 62 75 67 67 69 6e 67  or .** debugging
444e9 20 46 54 53 20 6f 6e 6c 79 2c 20 69 74 20 73 68   FTS only, it sh
444ea 6f 75 6c 64 20 6e 6f 74 20 75 73 75 61 6c 6c 79  ould not usually
444eb 20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 69 6e   be turned on in
444ec 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 73   production.** s
444ed 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 64 65  ystems..*/.#ifde
444ee 66 20 46 54 53 33 5f 4c 4f 47 5f 4d 45 52 47 45  f FTS3_LOG_MERGE
444ef 53 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  S.static void ft
444f0 73 33 4c 6f 67 4d 65 72 67 65 28 69 6e 74 20 6e  s3LogMerge(int n
444f1 4d 65 72 67 65 2c 20 73 71 6c 69 74 65 33 5f 69  Merge, sqlite3_i
444f2 6e 74 36 34 20 69 41 62 73 4c 65 76 65 6c 29 7b  nt64 iAbsLevel){
444f3 0a 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 53  .  sqlite3_log(S
444f4 51 4c 49 54 45 5f 4f 4b 2c 20 22 25 64 2d 77 61  QLITE_OK, "%d-wa
444f5 79 20 6d 65 72 67 65 20 66 72 6f 6d 20 6c 65 76  y merge from lev
444f6 65 6c 20 25 64 22 2c 20 6e 4d 65 72 67 65 2c 20  el %d", nMerge, 
444f7 28 69 6e 74 29 69 41 62 73 4c 65 76 65 6c 29 3b  (int)iAbsLevel);
444f8 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  .}.#else.#define
444f9 20 66 74 73 33 4c 6f 67 4d 65 72 67 65 28 78 2c   fts3LogMerge(x,
444fa 20 79 29 0a 23 65 6e 64 69 66 0a 0a 0a 74 79 70   y).#endif...typ
444fb 65 64 65 66 20 73 74 72 75 63 74 20 50 65 6e 64  edef struct Pend
444fc 69 6e 67 4c 69 73 74 20 50 65 6e 64 69 6e 67 4c  ingList PendingL
444fd 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ist;.typedef str
444fe 75 63 74 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20  uct SegmentNode 
444ff 53 65 67 6d 65 6e 74 4e 6f 64 65 3b 0a 74 79 70  SegmentNode;.typ
44500 65 64 65 66 20 73 74 72 75 63 74 20 53 65 67 6d  edef struct Segm
44501 65 6e 74 57 72 69 74 65 72 20 53 65 67 6d 65 6e  entWriter Segmen
44502 74 57 72 69 74 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20  tWriter;../*.** 
44503 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
44504 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 61 74  he following dat
44505 61 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75  a structure is u
44506 73 65 64 20 74 6f 20 62 75 69 6c 64 20 64 6f 63  sed to build doc
44507 6c 69 73 74 73 0a 2a 2a 20 69 6e 63 72 65 6d 65  lists.** increme
44508 6e 74 61 6c 6c 79 2e 20 53 65 65 20 66 75 6e 63  ntally. See func
44509 74 69 6f 6e 20 66 74 73 33 50 65 6e 64 69 6e 67  tion fts3Pending
4450a 4c 69 73 74 41 70 70 65 6e 64 28 29 20 66 6f 72  ListAppend() for
4450b 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 73 74 72   details..*/.str
4450c 75 63 74 20 50 65 6e 64 69 6e 67 4c 69 73 74 20  uct PendingList 
4450d 7b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 20  {.  int nData;. 
4450e 20 63 68 61 72 20 2a 61 44 61 74 61 3b 0a 20 20   char *aData;.  
4450f 69 6e 74 20 6e 53 70 61 63 65 3b 0a 20 20 73 71  int nSpace;.  sq
44510 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4c 61 73  lite3_int64 iLas
44511 74 44 6f 63 69 64 3b 0a 20 20 73 71 6c 69 74 65  tDocid;.  sqlite
44512 33 5f 69 6e 74 36 34 20 69 4c 61 73 74 43 6f 6c  3_int64 iLastCol
44513 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ;.  sqlite3_int6
44514 34 20 69 4c 61 73 74 50 6f 73 3b 0a 7d 3b 0a 0a  4 iLastPos;.};..
44515 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 63 75 72 73  ./*.** Each curs
44516 6f 72 20 68 61 73 20 61 20 28 70 6f 73 73 69 62  or has a (possib
44517 6c 79 20 65 6d 70 74 79 29 20 6c 69 6e 6b 65 64  ly empty) linked
44518 20 6c 69 73 74 20 6f 66 20 74 68 65 20 66 6f 6c   list of the fol
44519 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 74 73 2e 0a  lowing objects..
4451a 2a 2f 0a 73 74 72 75 63 74 20 46 74 73 33 44 65  */.struct Fts3De
4451b 66 65 72 72 65 64 54 6f 6b 65 6e 20 7b 0a 20 20  ferredToken {.  
4451c 46 74 73 33 50 68 72 61 73 65 54 6f 6b 65 6e 20  Fts3PhraseToken 
4451d 2a 70 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20  *pToken;        
4451e 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 6f  /* Pointer to co
4451f 72 72 65 73 70 6f 6e 64 69 6e 67 20 65 78 70 72  rresponding expr
44520 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
44521 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  iCol;           
44522 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
44523 6f 6c 75 6d 6e 20 74 6f 6b 65 6e 20 6d 75 73 74  olumn token must
44524 20 6f 63 63 75 72 20 69 6e 20 2a 2f 0a 20 20 46   occur in */.  F
44525 74 73 33 44 65 66 65 72 72 65 64 54 6f 6b 65 6e  ts3DeferredToken
44526 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 2f   *pNext;       /
44527 2a 20 4e 65 78 74 20 69 6e 20 6c 69 73 74 20 6f  * Next in list o
44528 66 20 64 65 66 65 72 72 65 64 20 74 6f 6b 65 6e  f deferred token
44529 73 20 2a 2f 0a 20 20 50 65 6e 64 69 6e 67 4c 69  s */.  PendingLi
4452a 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 20  st *pList;      
4452b 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69 73         /* Doclis
4452c 74 20 69 73 20 61 73 73 65 6d 62 6c 65 64 20 68  t is assembled h
4452d 65 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ere */.};../*.**
4452e 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
4452f 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
44530 73 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 74  s used to iterat
44531 65 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 65  e through the te
44532 72 6d 73 20 6f 6e 0a 2a 2a 20 61 20 63 6f 6e 74  rms on.** a cont
44533 69 67 75 6f 75 73 20 73 65 74 20 6f 66 20 73 65  iguous set of se
44534 67 6d 65 6e 74 20 62 2d 74 72 65 65 20 6c 65 61  gment b-tree lea
44535 66 20 6e 6f 64 65 73 2e 20 41 6c 74 68 6f 75 67  f nodes. Althoug
44536 68 20 74 68 65 20 64 65 74 61 69 6c 73 20 6f 66  h the details of
44537 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75  .** this structu
44538 72 65 20 61 72 65 20 6f 6e 6c 79 20 6d 61 6e 69  re are only mani
44539 70 75 6c 61 74 65 64 20 62 79 20 63 6f 64 65 20  pulated by code 
4453a 69 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 6f 70  in this file, op
4453b 61 71 75 65 20 68 61 6e 64 6c 65 73 0a 2a 2a 20  aque handles.** 
4453c 6f 66 20 74 79 70 65 20 46 74 73 33 53 65 67 52  of type Fts3SegR
4453d 65 61 64 65 72 2a 20 61 72 65 20 61 6c 73 6f 20  eader* are also 
4453e 75 73 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20  used by code in 
4453f 66 74 73 33 2e 63 20 74 6f 20 69 74 65 72 61 74  fts3.c to iterat
44540 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 65 72  e through.** ter
44541 6d 73 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67  ms when querying
44542 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69   the full-text i
44543 6e 64 65 78 2e 20 53 65 65 20 66 75 6e 63 74 69  ndex. See functi
44544 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 71 6c  ons:.**.**   sql
44545 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65  ite3Fts3SegReade
44546 72 4e 65 77 28 29 0a 2a 2a 20 20 20 73 71 6c 69  rNew().**   sqli
44547 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72  te3Fts3SegReader
44548 46 72 65 65 28 29 0a 2a 2a 20 20 20 73 71 6c 69  Free().**   sqli
44549 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72  te3Fts3SegReader
4454a 49 74 65 72 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20  Iterate().**.** 
4454b 4d 65 74 68 6f 64 73 20 75 73 65 64 20 74 6f 20  Methods used to 
4454c 6d 61 6e 69 70 75 6c 61 74 65 20 46 74 73 33 53  manipulate Fts3S
4454d 65 67 52 65 61 64 65 72 20 73 74 72 75 63 74 75  egReader structu
4454e 72 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 66 74 73  res:.**.**   fts
4454f 33 53 65 67 52 65 61 64 65 72 4e 65 78 74 28 29  3SegReaderNext()
44550 0a 2a 2a 20 20 20 66 74 73 33 53 65 67 52 65 61  .**   fts3SegRea
44551 64 65 72 46 69 72 73 74 44 6f 63 69 64 28 29 0a  derFirstDocid().
44552 2a 2a 20 20 20 66 74 73 33 53 65 67 52 65 61 64  **   fts3SegRead
44553 65 72 4e 65 78 74 44 6f 63 69 64 28 29 0a 2a 2f  erNextDocid().*/
44554 0a 73 74 72 75 63 74 20 46 74 73 33 53 65 67 52  .struct Fts3SegR
44555 65 61 64 65 72 20 7b 0a 20 20 69 6e 74 20 69 49  eader {.  int iI
44556 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
44557 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
44558 65 78 20 77 69 74 68 69 6e 20 6c 65 76 65 6c 2c  ex within level,
44559 20 6f 72 20 30 78 37 46 46 46 46 46 46 46 20 66   or 0x7FFFFFFF f
4455a 6f 72 20 50 54 20 2a 2f 0a 20 20 75 38 20 62 4c  or PT */.  u8 bL
4455b 6f 6f 6b 75 70 3b 20 20 20 20 20 20 20 20 20 20  ookup;          
4455c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
4455d 75 65 20 66 6f 72 20 61 20 6c 6f 6f 6b 75 70 20  ue for a lookup 
4455e 6f 6e 6c 79 20 2a 2f 0a 20 20 75 38 20 72 6f 6f  only */.  u8 roo
4455f 74 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20  tOnly;          
44560 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
44561 65 20 66 6f 72 20 61 20 72 6f 6f 74 2d 6f 6e 6c  e for a root-onl
44562 79 20 72 65 61 64 65 72 20 2a 2f 0a 0a 20 20 73  y reader */..  s
44563 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 53 74  qlite3_int64 iSt
44564 61 72 74 42 6c 6f 63 6b 3b 20 20 20 20 20 20 2f  artBlock;      /
44565 2a 20 52 6f 77 69 64 20 6f 66 20 66 69 72 73 74  * Rowid of first
44566 20 6c 65 61 66 20 62 6c 6f 63 6b 20 74 6f 20 74   leaf block to t
44567 72 61 76 65 72 73 65 20 2a 2f 0a 20 20 73 71 6c  raverse */.  sql
44568 69 74 65 33 5f 69 6e 74 36 34 20 69 4c 65 61 66  ite3_int64 iLeaf
44569 45 6e 64 42 6c 6f 63 6b 3b 20 20 20 20 2f 2a 20  EndBlock;    /* 
4456a 52 6f 77 69 64 20 6f 66 20 66 69 6e 61 6c 20 6c  Rowid of final l
4456b 65 61 66 20 62 6c 6f 63 6b 20 74 6f 20 74 72 61  eaf block to tra
4456c 76 65 72 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74  verse */.  sqlit
4456d 65 33 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f  e3_int64 iEndBlo
4456e 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 6f  ck;        /* Ro
4456f 77 69 64 20 6f 66 20 66 69 6e 61 6c 20 62 6c 6f  wid of final blo
44570 63 6b 20 69 6e 20 73 65 67 6d 65 6e 74 20 28 6f  ck in segment (o
44571 72 20 30 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65  r 0) */.  sqlite
44572 33 5f 69 6e 74 36 34 20 69 43 75 72 72 65 6e 74  3_int64 iCurrent
44573 42 6c 6f 63 6b 3b 20 20 20 20 2f 2a 20 43 75 72  Block;    /* Cur
44574 72 65 6e 74 20 6c 65 61 66 20 62 6c 6f 63 6b 20  rent leaf block 
44575 28 6f 72 20 30 29 20 2a 2f 0a 0a 20 20 63 68 61  (or 0) */..  cha
44576 72 20 2a 61 4e 6f 64 65 3b 20 20 20 20 20 20 20  r *aNode;       
44577 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
44578 50 6f 69 6e 74 65 72 20 74 6f 20 6e 6f 64 65 20  Pointer to node 
44579 64 61 74 61 20 28 6f 72 20 4e 55 4c 4c 29 20 2a  data (or NULL) *
4457a 2f 0a 20 20 69 6e 74 20 6e 4e 6f 64 65 3b 20 20  /.  int nNode;  
4457b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4457c 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62      /* Size of b
4457d 75 66 66 65 72 20 61 74 20 61 4e 6f 64 65 20 28  uffer at aNode (
4457e 6f 72 20 30 29 20 2a 2f 0a 20 20 69 6e 74 20 6e  or 0) */.  int n
4457f 50 6f 70 75 6c 61 74 65 3b 20 20 20 20 20 20 20  Populate;       
44580 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
44581 20 3e 30 2c 20 62 79 74 65 73 20 6f 66 20 62 75   >0, bytes of bu
44582 66 66 65 72 20 61 4e 6f 64 65 5b 5d 20 6c 6f 61  ffer aNode[] loa
44583 64 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ded */.  sqlite3
44584 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b 20 20 20  _blob *pBlob;   
44585 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e           /* If n
44586 6f 74 20 4e 55 4c 4c 2c 20 62 6c 6f 62 20 68 61  ot NULL, blob ha
44587 6e 64 6c 65 20 74 6f 20 72 65 61 64 20 6e 6f 64  ndle to read nod
44588 65 20 2a 2f 0a 0a 20 20 46 74 73 33 48 61 73 68  e */..  Fts3Hash
44589 45 6c 65 6d 20 2a 2a 70 70 4e 65 78 74 45 6c 65  Elem **ppNextEle
4458a 6d 3b 0a 0a 20 20 2f 2a 20 56 61 72 69 61 62 6c  m;..  /* Variabl
4458b 65 73 20 73 65 74 20 62 79 20 66 74 73 33 53 65  es set by fts3Se
4458c 67 52 65 61 64 65 72 4e 65 78 74 28 29 2e 20 54  gReaderNext(). T
4458d 68 65 73 65 20 6d 61 79 20 62 65 20 72 65 61 64  hese may be read
4458e 20 64 69 72 65 63 74 6c 79 0a 20 20 2a 2a 20 62   directly.  ** b
4458f 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 54 68  y the caller. Th
44590 65 79 20 61 72 65 20 76 61 6c 69 64 20 66 72 6f  ey are valid fro
44591 6d 20 74 68 65 20 74 69 6d 65 20 53 65 67 6d 65  m the time Segme
44592 6e 74 52 65 61 64 65 72 4e 65 77 28 29 20 72 65  ntReaderNew() re
44593 74 75 72 6e 73 0a 20 20 2a 2a 20 75 6e 74 69 6c  turns.  ** until
44594 20 53 65 67 6d 65 6e 74 52 65 61 64 65 72 4e 65   SegmentReaderNe
44595 78 74 28 29 20 72 65 74 75 72 6e 73 20 73 6f 6d  xt() returns som
44596 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61  ething other tha
44597 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 2a 2a  n SQLITE_OK.  **
44598 20 28 69 2e 65 2e 20 53 51 4c 49 54 45 5f 44 4f   (i.e. SQLITE_DO
44599 4e 45 29 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  NE)..  */.  int 
4459a 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  nTerm;          
4459b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
4459c 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
4459d 6e 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20 2a  n current term *
4459e 2f 0a 20 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b  /.  char *zTerm;
4459f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
445a0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
445a1 6f 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20 2a  o current term *
445a2 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 41 6c 6c  /.  int nTermAll
445a3 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  oc;             
445a4 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64      /* Allocated
445a5 20 73 69 7a 65 20 6f 66 20 7a 54 65 72 6d 20 62   size of zTerm b
445a6 75 66 66 65 72 20 2a 2f 0a 20 20 63 68 61 72 20  uffer */.  char 
445a7 2a 61 44 6f 63 6c 69 73 74 3b 20 20 20 20 20 20  *aDoclist;      
445a8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
445a9 69 6e 74 65 72 20 74 6f 20 64 6f 63 6c 69 73 74  inter to doclist
445aa 20 6f 66 20 63 75 72 72 65 6e 74 20 65 6e 74 72   of current entr
445ab 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 6f 63 6c  y */.  int nDocl
445ac 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ist;            
445ad 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
445ae 66 20 64 6f 63 6c 69 73 74 20 69 6e 20 63 75 72  f doclist in cur
445af 72 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 0a 20  rent entry */.. 
445b0 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
445b1 67 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20  g variables are 
445b2 75 73 65 64 20 62 79 20 66 74 73 33 53 65 67 52  used by fts3SegR
445b3 65 61 64 65 72 4e 65 78 74 44 6f 63 69 64 28 29  eaderNextDocid()
445b4 20 74 6f 20 69 74 65 72 61 74 65 20 0a 20 20 2a   to iterate .  *
445b5 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 63 75  * through the cu
445b6 72 72 65 6e 74 20 64 6f 63 6c 69 73 74 20 28 61  rrent doclist (a
445b7 44 6f 63 6c 69 73 74 2f 6e 44 6f 63 6c 69 73 74  Doclist/nDoclist
445b8 29 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 2a  )..  */.  char *
445b9 70 4f 66 66 73 65 74 4c 69 73 74 3b 0a 20 20 69  pOffsetList;.  i
445ba 6e 74 20 6e 4f 66 66 73 65 74 4c 69 73 74 3b 20  nt nOffsetList; 
445bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
445bc 2a 20 46 6f 72 20 64 65 73 63 65 6e 64 69 6e 67  * For descending
445bd 20 70 65 6e 64 69 6e 67 20 73 65 67 2d 72 65 61   pending seg-rea
445be 64 65 72 73 20 6f 6e 6c 79 20 2a 2f 0a 20 20 73  ders only */.  s
445bf 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 44 6f  qlite3_int64 iDo
445c0 63 69 64 3b 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65  cid;.};..#define
445c1 20 66 74 73 33 53 65 67 52 65 61 64 65 72 49 73   fts3SegReaderIs
445c2 50 65 6e 64 69 6e 67 28 70 29 20 28 28 70 29 2d  Pending(p) ((p)-
445c3 3e 70 70 4e 65 78 74 45 6c 65 6d 21 3d 30 29 0a  >ppNextElem!=0).
445c4 23 64 65 66 69 6e 65 20 66 74 73 33 53 65 67 52  #define fts3SegR
445c5 65 61 64 65 72 49 73 52 6f 6f 74 4f 6e 6c 79 28  eaderIsRootOnly(
445c6 70 29 20 28 28 70 29 2d 3e 72 6f 6f 74 4f 6e 6c  p) ((p)->rootOnl
445c7 79 21 3d 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  y!=0)../*.** An 
445c8 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
445c9 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73   structure is us
445ca 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 73  ed to create a s
445cb 65 67 6d 65 6e 74 20 62 2d 74 72 65 65 20 69 6e  egment b-tree in
445cc 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
445cd 2e 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 20 64  . The internal d
445ce 65 74 61 69 6c 73 20 6f 66 20 74 68 69 73 20 74  etails of this t
445cf 79 70 65 20 61 72 65 20 6f 6e 6c 79 20 61 63 63  ype are only acc
445d0 65 73 73 65 64 20 62 79 20 74 68 65 0a 2a 2a 20  essed by the.** 
445d1 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69  following functi
445d2 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 66 74 73  ons:.**.**   fts
445d3 33 53 65 67 57 72 69 74 65 72 41 64 64 28 29 0a  3SegWriterAdd().
445d4 2a 2a 20 20 20 66 74 73 33 53 65 67 57 72 69 74  **   fts3SegWrit
445d5 65 72 46 6c 75 73 68 28 29 0a 2a 2a 20 20 20 66  erFlush().**   f
445d6 74 73 33 53 65 67 57 72 69 74 65 72 46 72 65 65  ts3SegWriterFree
445d7 28 29 0a 2a 2f 0a 73 74 72 75 63 74 20 53 65 67  ().*/.struct Seg
445d8 6d 65 6e 74 57 72 69 74 65 72 20 7b 0a 20 20 53  mentWriter {.  S
445d9 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 54 72 65  egmentNode *pTre
445da 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
445db 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 69 6e 74  * Pointer to int
445dc 65 72 69 6f 72 20 74 72 65 65 20 73 74 72 75 63  erior tree struc
445dd 74 75 72 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ture */.  sqlite
445de 33 5f 69 6e 74 36 34 20 69 46 69 72 73 74 3b 20  3_int64 iFirst; 
445df 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
445e0 73 74 20 73 6c 6f 74 20 69 6e 20 25 5f 73 65 67  st slot in %_seg
445e1 6d 65 6e 74 73 20 77 72 69 74 74 65 6e 20 2a 2f  ments written */
445e2 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
445e3 20 69 46 72 65 65 3b 20 20 20 20 20 20 20 20 20   iFree;         
445e4 20 20 20 2f 2a 20 4e 65 78 74 20 66 72 65 65 20     /* Next free 
445e5 73 6c 6f 74 20 69 6e 20 25 5f 73 65 67 6d 65 6e  slot in %_segmen
445e6 74 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54  ts */.  char *zT
445e7 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  erm;            
445e8 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
445e9 65 72 20 74 6f 20 70 72 65 76 69 6f 75 73 20 74  er to previous t
445ea 65 72 6d 20 62 75 66 66 65 72 20 2a 2f 0a 20 20  erm buffer */.  
445eb 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20  int nTerm;      
445ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
445ed 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
445ee 65 73 20 69 6e 20 7a 54 65 72 6d 20 2a 2f 0a 20  es in zTerm */. 
445ef 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63 3b 20 20 20   int nMalloc;   
445f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
445f1 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61 6c 6c   /* Size of mall
445f2 6f 63 27 64 20 62 75 66 66 65 72 20 61 74 20 7a  oc'd buffer at z
445f3 4d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72  Malloc */.  char
445f4 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20   *zMalloc;      
445f5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
445f6 61 6c 6c 6f 63 27 64 20 73 70 61 63 65 20 28 70  alloc'd space (p
445f7 6f 73 73 69 62 6c 79 29 20 75 73 65 64 20 66 6f  ossibly) used fo
445f8 72 20 7a 54 65 72 6d 20 2a 2f 0a 20 20 69 6e 74  r zTerm */.  int
445f9 20 6e 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20   nSize;         
445fa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
445fb 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69  Size of allocati
445fc 6f 6e 20 61 74 20 61 44 61 74 61 20 2a 2f 0a 20  on at aData */. 
445fd 20 69 6e 74 20 6e 44 61 74 61 3b 20 20 20 20 20   int nData;     
445fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
445ff 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 64 61 74   /* Bytes of dat
44600 61 20 69 6e 20 61 44 61 74 61 20 2a 2f 0a 20 20  a in aData */.  
44601 63 68 61 72 20 2a 61 44 61 74 61 3b 20 20 20 20  char *aData;    
44602 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44603 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 6c  /* Pointer to bl
44604 6f 63 6b 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28  ock from malloc(
44605 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  ) */.};../*.** T
44606 79 70 65 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20  ype SegmentNode 
44607 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 66  is used by the f
44608 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 66  ollowing three f
44609 75 6e 63 74 69 6f 6e 73 20 74 6f 20 63 72 65 61  unctions to crea
4460a 74 65 0a 2a 2a 20 74 68 65 20 69 6e 74 65 72 69  te.** the interi
4460b 6f 72 20 70 61 72 74 20 6f 66 20 74 68 65 20 73  or part of the s
4460c 65 67 6d 65 6e 74 20 62 2b 2d 74 72 65 65 20 73  egment b+-tree s
4460d 74 72 75 63 74 75 72 65 73 20 28 65 76 65 72 79  tructures (every
4460e 74 68 69 6e 67 20 65 78 63 65 70 74 0a 2a 2a 20  thing except.** 
4460f 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 73 29 2e  the leaf nodes).
44610 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   These functions
44611 20 61 6e 64 20 74 79 70 65 20 61 72 65 20 6f 6e   and type are on
44612 6c 79 20 65 76 65 72 20 75 73 65 64 20 62 79 20  ly ever used by 
44613 63 6f 64 65 0a 2a 2a 20 77 69 74 68 69 6e 20 74  code.** within t
44614 68 65 20 66 74 73 33 53 65 67 57 72 69 74 65 72  he fts3SegWriter
44615 58 58 58 28 29 20 66 61 6d 69 6c 79 20 6f 66 20  XXX() family of 
44616 66 75 6e 63 74 69 6f 6e 73 20 64 65 73 63 72 69  functions descri
44617 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2a 0a 2a 2a  bed above..**.**
44618 20 20 20 66 74 73 33 4e 6f 64 65 41 64 64 54 65     fts3NodeAddTe
44619 72 6d 28 29 0a 2a 2a 20 20 20 66 74 73 33 4e 6f  rm().**   fts3No
4461a 64 65 57 72 69 74 65 28 29 0a 2a 2a 20 20 20 66  deWrite().**   f
4461b 74 73 33 4e 6f 64 65 46 72 65 65 28 29 0a 2a 2a  ts3NodeFree().**
4461c 0a 2a 2a 20 57 68 65 6e 20 61 20 62 2b 74 72 65  .** When a b+tre
4461d 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  e is written to 
4461e 74 68 65 20 64 61 74 61 62 61 73 65 20 28 65 69  the database (ei
4461f 74 68 65 72 20 61 73 20 61 20 72 65 73 75 6c 74  ther as a result
44620 20 6f 66 20 61 20 6d 65 72 67 65 0a 2a 2a 20 6f   of a merge.** o
44621 72 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 74 65  r the pending-te
44622 72 6d 73 20 74 61 62 6c 65 20 62 65 69 6e 67 20  rms table being 
44623 66 6c 75 73 68 65 64 29 2c 20 6c 65 61 76 65 73  flushed), leaves
44624 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 6e 74   are written int
44625 6f 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61  o the .** databa
44626 73 65 20 66 69 6c 65 20 61 73 20 73 6f 6f 6e 20  se file as soon 
44627 61 73 20 74 68 65 79 20 61 72 65 20 63 6f 6d 70  as they are comp
44628 6c 65 74 65 6c 79 20 70 6f 70 75 6c 61 74 65 64  letely populated
44629 2e 20 54 68 65 20 69 6e 74 65 72 69 6f 72 20 6f  . The interior o
4462a 66 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 69 73  f.** the tree is
4462b 20 61 73 73 65 6d 62 6c 65 64 20 69 6e 20 6d 65   assembled in me
4462c 6d 6f 72 79 20 61 6e 64 20 77 72 69 74 74 65 6e  mory and written
4462d 20 6f 75 74 20 6f 6e 6c 79 20 6f 6e 63 65 20 61   out only once a
4462e 6c 6c 20 6c 65 61 76 65 73 20 68 61 76 65 0a 2a  ll leaves have.*
4462f 2a 20 62 65 65 6e 20 70 6f 70 75 6c 61 74 65 64  * been populated
44630 20 61 6e 64 20 73 74 6f 72 65 64 2e 20 54 68 69   and stored. Thi
44631 73 20 69 73 20 4f 6b 2c 20 61 73 20 74 68 65 20  s is Ok, as the 
44632 62 2b 2d 74 72 65 65 20 66 61 6e 6f 75 74 20 69  b+-tree fanout i
44633 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 76 65 72  s usually.** ver
44634 79 20 6c 61 72 67 65 2c 20 6d 65 61 6e 69 6e 67  y large, meaning
44635 20 74 68 61 74 20 74 68 65 20 69 6e 74 65 72 69   that the interi
44636 6f 72 20 6f 66 20 74 68 65 20 74 72 65 65 20 63  or of the tree c
44637 6f 6e 73 75 6d 65 73 20 72 65 6c 61 74 69 76 65  onsumes relative
44638 6c 79 20 0a 2a 2a 20 6c 69 74 74 6c 65 20 6d 65  ly .** little me
44639 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  mory..*/.struct 
4463a 53 65 67 6d 65 6e 74 4e 6f 64 65 20 7b 0a 20 20  SegmentNode {.  
4463b 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 50 61  SegmentNode *pPa
4463c 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  rent;           
4463d 2f 2a 20 50 61 72 65 6e 74 20 6e 6f 64 65 20 28  /* Parent node (
4463e 6f 72 20 4e 55 4c 4c 20 66 6f 72 20 72 6f 6f 74  or NULL for root
4463f 20 6e 6f 64 65 29 20 2a 2f 0a 20 20 53 65 67 6d   node) */.  Segm
44640 65 6e 74 4e 6f 64 65 20 2a 70 52 69 67 68 74 3b  entNode *pRight;
44641 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
44642 6f 69 6e 74 65 72 20 74 6f 20 72 69 67 68 74 2d  ointer to right-
44643 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 20 53 65 67  sibling */.  Seg
44644 6d 65 6e 74 4e 6f 64 65 20 2a 70 4c 65 66 74 6d  mentNode *pLeftm
44645 6f 73 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ost;         /* 
44646 50 6f 69 6e 74 65 72 20 74 6f 20 6c 65 66 74 2d  Pointer to left-
44647 6d 6f 73 74 20 6e 6f 64 65 20 6f 66 20 74 68 69  most node of thi
44648 73 20 64 65 70 74 68 20 2a 2f 0a 20 20 69 6e 74  s depth */.  int
44649 20 6e 45 6e 74 72 79 3b 20 20 20 20 20 20 20 20   nEntry;        
4464a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4464b 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20  Number of terms 
4464c 77 72 69 74 74 65 6e 20 74 6f 20 6e 6f 64 65 20  written to node 
4464d 73 6f 20 66 61 72 20 2a 2f 0a 20 20 63 68 61 72  so far */.  char
4464e 20 2a 7a 54 65 72 6d 3b 20 20 20 20 20 20 20 20   *zTerm;        
4464f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
44650 6f 69 6e 74 65 72 20 74 6f 20 70 72 65 76 69 6f  ointer to previo
44651 75 73 20 74 65 72 6d 20 62 75 66 66 65 72 20 2a  us term buffer *
44652 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20 20  /.  int nTerm;  
44653 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44654 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
44655 20 62 79 74 65 73 20 69 6e 20 7a 54 65 72 6d 20   bytes in zTerm 
44656 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63  */.  int nMalloc
44657 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
44658 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
44659 6d 61 6c 6c 6f 63 27 64 20 62 75 66 66 65 72 20  malloc'd buffer 
4465a 61 74 20 7a 4d 61 6c 6c 6f 63 20 2a 2f 0a 20 20  at zMalloc */.  
4465b 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20  char *zMalloc;  
4465c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4465d 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 73 70 61 63  /* Malloc'd spac
4465e 65 20 28 70 6f 73 73 69 62 6c 79 29 20 75 73 65  e (possibly) use
4465f 64 20 66 6f 72 20 7a 54 65 72 6d 20 2a 2f 0a 20  d for zTerm */. 
44660 20 69 6e 74 20 6e 44 61 74 61 3b 20 20 20 20 20   int nData;     
44661 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44662 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 76 61 6c   /* Bytes of val
44663 69 64 20 64 61 74 61 20 73 6f 20 66 61 72 20 2a  id data so far *
44664 2f 0a 20 20 63 68 61 72 20 2a 61 44 61 74 61 3b  /.  char *aData;
44665 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44666 20 20 20 20 2f 2a 20 4e 6f 64 65 20 64 61 74 61      /* Node data
44667 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 56 61   */.};../*.** Va
44668 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74  lid values for t
44669 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
4466a 6e 74 20 74 6f 20 66 74 73 33 53 71 6c 53 74 6d  nt to fts3SqlStm
4466b 74 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  t()..*/.#define 
4466c 53 51 4c 5f 44 45 4c 45 54 45 5f 43 4f 4e 54 45  SQL_DELETE_CONTE
4466d 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 20 30  NT             0
4466e 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 49 53 5f  .#define SQL_IS_
4466f 45 4d 50 54 59 20 20 20 20 20 20 20 20 20 20 20  EMPTY           
44670 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
44671 65 20 53 51 4c 5f 44 45 4c 45 54 45 5f 41 4c 4c  e SQL_DELETE_ALL
44672 5f 43 4f 4e 54 45 4e 54 20 20 20 20 20 20 20 20  _CONTENT        
44673 20 32 20 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f   2 .#define SQL_
44674 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 45 47 4d 45  DELETE_ALL_SEGME
44675 4e 54 53 20 20 20 20 20 20 20 20 33 0a 23 64 65  NTS        3.#de
44676 66 69 6e 65 20 53 51 4c 5f 44 45 4c 45 54 45 5f  fine SQL_DELETE_
44677 41 4c 4c 5f 53 45 47 44 49 52 20 20 20 20 20 20  ALL_SEGDIR      
44678 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51      4.#define SQ
44679 4c 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 44 4f 43  L_DELETE_ALL_DOC
4467a 53 49 5a 45 20 20 20 20 20 20 20 20 20 35 0a 23  SIZE         5.#
4467b 64 65 66 69 6e 65 20 53 51 4c 5f 44 45 4c 45 54  define SQL_DELET
4467c 45 5f 41 4c 4c 5f 53 54 41 54 20 20 20 20 20 20  E_ALL_STAT      
4467d 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20        6.#define 
4467e 53 51 4c 5f 53 45 4c 45 43 54 5f 43 4f 4e 54 45  SQL_SELECT_CONTE
4467f 4e 54 5f 42 59 5f 52 4f 57 49 44 20 20 20 20 37  NT_BY_ROWID    7
44680 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 4e 45 58  .#define SQL_NEX
44681 54 5f 53 45 47 4d 45 4e 54 5f 49 4e 44 45 58 20  T_SEGMENT_INDEX 
44682 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e          8.#defin
44683 65 20 53 51 4c 5f 49 4e 53 45 52 54 5f 53 45 47  e SQL_INSERT_SEG
44684 4d 45 4e 54 53 20 20 20 20 20 20 20 20 20 20 20  MENTS           
44685 20 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 4e   9.#define SQL_N
44686 45 58 54 5f 53 45 47 4d 45 4e 54 53 5f 49 44 20  EXT_SEGMENTS_ID 
44687 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 65 66           10.#def
44688 69 6e 65 20 53 51 4c 5f 49 4e 53 45 52 54 5f 53  ine SQL_INSERT_S
44689 45 47 44 49 52 20 20 20 20 20 20 20 20 20 20 20  EGDIR           
4468a 20 20 31 31 0a 23 64 65 66 69 6e 65 20 53 51 4c    11.#define SQL
4468b 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 20 20 20  _SELECT_LEVEL   
4468c 20 20 20 20 20 20 20 20 20 20 20 31 32 0a 23 64             12.#d
4468d 65 66 69 6e 65 20 53 51 4c 5f 53 45 4c 45 43 54  efine SQL_SELECT
4468e 5f 4c 45 56 45 4c 5f 52 41 4e 47 45 20 20 20 20  _LEVEL_RANGE    
4468f 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 53      13.#define S
44690 51 4c 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 5f  QL_SELECT_LEVEL_
44691 43 4f 55 4e 54 20 20 20 20 20 20 20 20 31 34 0a  COUNT        14.
44692 23 64 65 66 69 6e 65 20 53 51 4c 5f 53 45 4c 45  #define SQL_SELE
44693 43 54 5f 53 45 47 44 49 52 5f 4d 41 58 5f 4c 45  CT_SEGDIR_MAX_LE
44694 56 45 4c 20 20 20 31 35 0a 23 64 65 66 69 6e 65  VEL   15.#define
44695 20 53 51 4c 5f 44 45 4c 45 54 45 5f 53 45 47 44   SQL_DELETE_SEGD
44696 49 52 5f 4c 45 56 45 4c 20 20 20 20 20 20 20 31  IR_LEVEL       1
44697 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 44 45  6.#define SQL_DE
44698 4c 45 54 45 5f 53 45 47 4d 45 4e 54 53 5f 52 41  LETE_SEGMENTS_RA
44699 4e 47 45 20 20 20 20 20 31 37 0a 23 64 65 66 69  NGE     17.#defi
4469a 6e 65 20 53 51 4c 5f 43 4f 4e 54 45 4e 54 5f 49  ne SQL_CONTENT_I
4469b 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20  NSERT           
4469c 20 31 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f   18.#define SQL_
4469d 44 45 4c 45 54 45 5f 44 4f 43 53 49 5a 45 20 20  DELETE_DOCSIZE  
4469e 20 20 20 20 20 20 20 20 20 20 31 39 0a 23 64 65            19.#de
4469f 66 69 6e 65 20 53 51 4c 5f 52 45 50 4c 41 43 45  fine SQL_REPLACE
446a0 5f 44 4f 43 53 49 5a 45 20 20 20 20 20 20 20 20  _DOCSIZE        
446a1 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 53 51     20.#define SQ
446a2 4c 5f 53 45 4c 45 43 54 5f 44 4f 43 53 49 5a 45  L_SELECT_DOCSIZE
446a3 20 20 20 20 20 20 20 20 20 20 20 20 32 31 0a 23              21.#
446a4 64 65 66 69 6e 65 20 53 51 4c 5f 53 45 4c 45 43  define SQL_SELEC
446a5 54 5f 53 54 41 54 20 20 20 20 20 20 20 20 20 20  T_STAT          
446a6 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20       22.#define 
446a7 53 51 4c 5f 52 45 50 4c 41 43 45 5f 53 54 41 54  SQL_REPLACE_STAT
446a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 33                23
446a9 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 53 45  ..#define SQL_SE
446aa 4c 45 43 54 5f 41 4c 4c 5f 50 52 45 46 49 58 5f  LECT_ALL_PREFIX_
446ab 4c 45 56 45 4c 20 20 20 32 34 0a 23 64 65 66 69  LEVEL   24.#defi
446ac 6e 65 20 53 51 4c 5f 44 45 4c 45 54 45 5f 41 4c  ne SQL_DELETE_AL
446ad 4c 5f 54 45 52 4d 53 5f 53 45 47 44 49 52 20 20  L_TERMS_SEGDIR  
446ae 20 32 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f   25.#define SQL_
446af 44 45 4c 45 54 45 5f 53 45 47 44 49 52 5f 52 41  DELETE_SEGDIR_RA
446b0 4e 47 45 20 20 20 20 20 20 20 32 36 0a 23 64 65  NGE       26.#de
446b1 66 69 6e 65 20 53 51 4c 5f 53 45 4c 45 43 54 5f  fine SQL_SELECT_
446b2 41 4c 4c 5f 4c 41 4e 47 49 44 20 20 20 20 20 20  ALL_LANGID      
446b3 20 20 20 32 37 0a 23 64 65 66 69 6e 65 20 53 51     27.#define SQ
446b4 4c 5f 46 49 4e 44 5f 4d 45 52 47 45 5f 4c 45 56  L_FIND_MERGE_LEV
446b5 45 4c 20 20 20 20 20 20 20 20 20 20 32 38 0a 23  EL          28.#
446b6 64 65 66 69 6e 65 20 53 51 4c 5f 4d 41 58 5f 4c  define SQL_MAX_L
446b7 45 41 46 5f 4e 4f 44 45 5f 45 53 54 49 4d 41 54  EAF_NODE_ESTIMAT
446b8 45 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65 20  E    29.#define 
446b9 53 51 4c 5f 44 45 4c 45 54 45 5f 53 45 47 44 49  SQL_DELETE_SEGDI
446ba 52 5f 45 4e 54 52 59 20 20 20 20 20 20 20 33 30  R_ENTRY       30
446bb 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 53 48 49  .#define SQL_SHI
446bc 46 54 5f 53 45 47 44 49 52 5f 45 4e 54 52 59 20  FT_SEGDIR_ENTRY 
446bd 20 20 20 20 20 20 20 33 31 0a 23 64 65 66 69 6e         31.#defin
446be 65 20 53 51 4c 5f 53 45 4c 45 43 54 5f 53 45 47  e SQL_SELECT_SEG
446bf 44 49 52 20 20 20 20 20 20 20 20 20 20 20 20 20  DIR             
446c0 33 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 43  32.#define SQL_C
446c1 48 4f 4d 50 5f 53 45 47 44 49 52 20 20 20 20 20  HOMP_SEGDIR     
446c2 20 20 20 20 20 20 20 20 20 33 33 0a 23 64 65 66           33.#def
446c3 69 6e 65 20 53 51 4c 5f 53 45 47 4d 45 4e 54 5f  ine SQL_SEGMENT_
446c4 49 53 5f 41 50 50 45 4e 44 41 42 4c 45 20 20 20  IS_APPENDABLE   
446c5 20 20 33 34 0a 23 64 65 66 69 6e 65 20 53 51 4c    34.#define SQL
446c6 5f 53 45 4c 45 43 54 5f 49 4e 44 45 58 45 53 20  _SELECT_INDEXES 
446c7 20 20 20 20 20 20 20 20 20 20 20 33 35 0a 23 64             35.#d
446c8 65 66 69 6e 65 20 53 51 4c 5f 53 45 4c 45 43 54  efine SQL_SELECT
446c9 5f 4d 58 4c 45 56 45 4c 20 20 20 20 20 20 20 20  _MXLEVEL        
446ca 20 20 20 20 33 36 0a 0a 2f 2a 0a 2a 2a 20 54 68      36../*.** Th
446cb 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
446cc 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e  sed to obtain an
446cd 20 53 51 4c 69 74 65 20 70 72 65 70 61 72 65 64   SQLite prepared
446ce 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c   statement handl
446cf 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 74 61  e.** for the sta
446d0 74 65 6d 65 6e 74 20 69 64 65 6e 74 69 66 69 65  tement identifie
446d1 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20  d by the second 
446d2 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 73 75 63  argument. If suc
446d3 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 2a 70 70 20  cessful,.** *pp 
446d4 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 72 65  is set to the re
446d5 71 75 65 73 74 65 64 20 73 74 61 74 65 6d 65 6e  quested statemen
446d6 74 20 68 61 6e 64 6c 65 20 61 6e 64 20 53 51 4c  t handle and SQL
446d7 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e  ITE_OK returned.
446d8 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61  .** Otherwise, a
446d9 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
446da 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
446db 61 6e 64 20 2a 70 70 20 69 73 20 73 65 74 20 74  and *pp is set t
446dc 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72  o 0..**.** If ar
446dd 67 75 6d 65 6e 74 20 61 70 56 61 6c 20 69 73 20  gument apVal is 
446de 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69  not NULL, then i
446df 74 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20  t must point to 
446e0 61 6e 20 61 72 72 61 79 20 77 69 74 68 0a 2a 2a  an array with.**
446e1 20 61 74 20 6c 65 61 73 74 20 61 73 20 6d 61 6e   at least as man
446e2 79 20 65 6e 74 72 69 65 73 20 61 73 20 74 68 65  y entries as the
446e3 20 72 65 71 75 65 73 74 65 64 20 73 74 61 74 65   requested state
446e4 6d 65 6e 74 20 68 61 73 20 62 6f 75 6e 64 20 0a  ment has bound .
446e5 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 2e 20 54  ** parameters. T
446e6 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 62 6f  he values are bo
446e7 75 6e 64 20 74 6f 20 74 68 65 20 73 74 61 74 65  und to the state
446e8 6d 65 6e 74 73 20 70 61 72 61 6d 65 74 65 72 73  ments parameters
446e9 20 62 65 66 6f 72 65 0a 2a 2a 20 72 65 74 75 72   before.** retur
446ea 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ning..*/.static 
446eb 69 6e 74 20 66 74 73 33 53 71 6c 53 74 6d 74 28  int fts3SqlStmt(
446ec 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c  .  Fts3Table *p,
446ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
446ee 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61     /* Virtual ta
446ef 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ble handle */.  
446f0 69 6e 74 20 65 53 74 6d 74 2c 20 20 20 20 20 20  int eStmt,      
446f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
446f2 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 51  /* One of the SQ
446f3 4c 5f 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20  L_XXX constants 
446f4 61 62 6f 76 65 20 2a 2f 0a 20 20 73 71 6c 69 74  above */.  sqlit
446f5 65 33 5f 73 74 6d 74 20 2a 2a 70 70 2c 20 20 20  e3_stmt **pp,   
446f6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
446f7 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e  T: Statement han
446f8 64 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  dle */.  sqlite3
446f9 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 20 20  _value **apVal  
446fa 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
446fb 65 73 20 74 6f 20 62 69 6e 64 20 74 6f 20 73 74  es to bind to st
446fc 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20  atement */.){.  
446fd 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a 53 71  const char *azSq
446fe 6c 5b 5d 20 3d 20 7b 0a 2f 2a 20 30 20 20 2a 2f  l[] = {./* 0  */
446ff 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
44700 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 20 57  Q.'%q_content' W
44701 48 45 52 45 20 72 6f 77 69 64 20 3d 20 3f 22 2c  HERE rowid = ?",
44702 0a 2f 2a 20 31 20 20 2a 2f 20 20 22 53 45 4c 45  ./* 1  */  "SELE
44703 43 54 20 4e 4f 54 20 45 58 49 53 54 53 28 53 45  CT NOT EXISTS(SE
44704 4c 45 43 54 20 64 6f 63 69 64 20 46 52 4f 4d 20  LECT docid FROM 
44705 25 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 20  %Q.'%q_content' 
44706 57 48 45 52 45 20 72 6f 77 69 64 21 3d 3f 29 22  WHERE rowid!=?)"
44707 2c 0a 2f 2a 20 32 20 20 2a 2f 20 20 22 44 45 4c  ,./* 2  */  "DEL
44708 45 54 45 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f  ETE FROM %Q.'%q_
44709 63 6f 6e 74 65 6e 74 27 22 2c 0a 2f 2a 20 33 20  content'",./* 3 
4470a 20 2a 2f 20 20 22 44 45 4c 45 54 45 20 46 52 4f   */  "DELETE FRO
4470b 4d 20 25 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74  M %Q.'%q_segment
4470c 73 27 22 2c 0a 2f 2a 20 34 20 20 2a 2f 20 20 22  s'",./* 4  */  "
4470d 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 27  DELETE FROM %Q.'
4470e 25 71 5f 73 65 67 64 69 72 27 22 2c 0a 2f 2a 20  %q_segdir'",./* 
4470f 35 20 20 2a 2f 20 20 22 44 45 4c 45 54 45 20 46  5  */  "DELETE F
44710 52 4f 4d 20 25 51 2e 27 25 71 5f 64 6f 63 73 69  ROM %Q.'%q_docsi
44711 7a 65 27 22 2c 0a 2f 2a 20 36 20 20 2a 2f 20 20  ze'",./* 6  */  
44712 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e  "DELETE FROM %Q.
44713 27 25 71 5f 73 74 61 74 27 22 2c 0a 2f 2a 20 37  '%q_stat'",./* 7
44714 20 20 2a 2f 20 20 22 53 45 4c 45 43 54 20 25 73    */  "SELECT %s
44715 20 57 48 45 52 45 20 72 6f 77 69 64 3d 3f 22 2c   WHERE rowid=?",
44716 0a 2f 2a 20 38 20 20 2a 2f 20 20 22 53 45 4c 45  ./* 8  */  "SELE
44717 43 54 20 28 53 45 4c 45 43 54 20 6d 61 78 28 69  CT (SELECT max(i
44718 64 78 29 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f  dx) FROM %Q.'%q_
44719 73 65 67 64 69 72 27 20 57 48 45 52 45 20 6c 65  segdir' WHERE le
4471a 76 65 6c 20 3d 20 3f 29 20 2b 20 31 22 2c 0a 2f  vel = ?) + 1",./
4471b 2a 20 39 20 20 2a 2f 20 20 22 52 45 50 4c 41 43  * 9  */  "REPLAC
4471c 45 20 49 4e 54 4f 20 25 51 2e 27 25 71 5f 73 65  E INTO %Q.'%q_se
4471d 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 2c  gments'(blockid,
4471e 20 62 6c 6f 63 6b 29 20 56 41 4c 55 45 53 28 3f   block) VALUES(?
4471f 2c 20 3f 29 22 2c 0a 2f 2a 20 31 30 20 2a 2f 20  , ?)",./* 10 */ 
44720 20 22 53 45 4c 45 43 54 20 63 6f 61 6c 65 73 63   "SELECT coalesc
44721 65 28 28 53 45 4c 45 43 54 20 6d 61 78 28 62 6c  e((SELECT max(bl
44722 6f 63 6b 69 64 29 20 46 52 4f 4d 20 25 51 2e 27  ockid) FROM %Q.'
44723 25 71 5f 73 65 67 6d 65 6e 74 73 27 29 20 2b 20  %q_segments') + 
44724 31 2c 20 31 29 22 2c 0a 2f 2a 20 31 31 20 2a 2f  1, 1)",./* 11 */
44725 20 20 22 52 45 50 4c 41 43 45 20 49 4e 54 4f 20    "REPLACE INTO 
44726 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27 20 56  %Q.'%q_segdir' V
44727 41 4c 55 45 53 28 3f 2c 3f 2c 3f 2c 3f 2c 3f 2c  ALUES(?,?,?,?,?,
44728 3f 29 22 2c 0a 0a 20 20 20 20 20 20 20 20 20 20  ?)",..          
44729 2f 2a 20 52 65 74 75 72 6e 20 73 65 67 6d 65 6e  /* Return segmen
4472a 74 73 20 69 6e 20 6f 72 64 65 72 20 66 72 6f 6d  ts in order from
4472b 20 6f 6c 64 65 73 74 20 74 6f 20 6e 65 77 65 73   oldest to newes
4472c 74 2e 2a 2f 20 0a 2f 2a 20 31 32 20 2a 2f 20 20  t.*/ ./* 12 */  
4472d 22 53 45 4c 45 43 54 20 69 64 78 2c 20 73 74 61  "SELECT idx, sta
4472e 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65 73  rt_block, leaves
4472f 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 65 6e 64 5f  _end_block, end_
44730 62 6c 6f 63 6b 2c 20 72 6f 6f 74 20 22 0a 20 20  block, root ".  
44731 20 20 20 20 20 20 20 20 20 20 22 46 52 4f 4d 20            "FROM 
44732 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27 20 57  %Q.'%q_segdir' W
44733 48 45 52 45 20 6c 65 76 65 6c 20 3d 20 3f 20 4f  HERE level = ? O
44734 52 44 45 52 20 42 59 20 69 64 78 20 41 53 43 22  RDER BY idx ASC"
44735 2c 0a 2f 2a 20 31 33 20 2a 2f 20 20 22 53 45 4c  ,./* 13 */  "SEL
44736 45 43 54 20 69 64 78 2c 20 73 74 61 72 74 5f 62  ECT idx, start_b
44737 6c 6f 63 6b 2c 20 6c 65 61 76 65 73 5f 65 6e 64  lock, leaves_end
44738 5f 62 6c 6f 63 6b 2c 20 65 6e 64 5f 62 6c 6f 63  _block, end_bloc
44739 6b 2c 20 72 6f 6f 74 20 22 0a 20 20 20 20 20 20  k, root ".      
4473a 20 20 20 20 20 20 22 46 52 4f 4d 20 25 51 2e 27        "FROM %Q.'
4473b 25 71 5f 73 65 67 64 69 72 27 20 57 48 45 52 45  %q_segdir' WHERE
4473c 20 6c 65 76 65 6c 20 42 45 54 57 45 45 4e 20 3f   level BETWEEN ?
4473d 20 41 4e 44 20 3f 22 0a 20 20 20 20 20 20 20 20   AND ?".        
4473e 20 20 20 20 22 4f 52 44 45 52 20 42 59 20 6c 65      "ORDER BY le
4473f 76 65 6c 20 44 45 53 43 2c 20 69 64 78 20 41 53  vel DESC, idx AS
44740 43 22 2c 0a 0a 2f 2a 20 31 34 20 2a 2f 20 20 22  C",../* 14 */  "
44741 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 20  SELECT count(*) 
44742 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67 64  FROM %Q.'%q_segd
44743 69 72 27 20 57 48 45 52 45 20 6c 65 76 65 6c 20  ir' WHERE level 
44744 3d 20 3f 22 2c 0a 2f 2a 20 31 35 20 2a 2f 20 20  = ?",./* 15 */  
44745 22 53 45 4c 45 43 54 20 6d 61 78 28 6c 65 76 65  "SELECT max(leve
44746 6c 29 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73  l) FROM %Q.'%q_s
44747 65 67 64 69 72 27 20 57 48 45 52 45 20 6c 65 76  egdir' WHERE lev
44748 65 6c 20 42 45 54 57 45 45 4e 20 3f 20 41 4e 44  el BETWEEN ? AND
44749 20 3f 22 2c 0a 0a 2f 2a 20 31 36 20 2a 2f 20 20   ?",../* 16 */  
4474a 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e  "DELETE FROM %Q.
4474b 27 25 71 5f 73 65 67 64 69 72 27 20 57 48 45 52  '%q_segdir' WHER
4474c 45 20 6c 65 76 65 6c 20 3d 20 3f 22 2c 0a 2f 2a  E level = ?",./*
4474d 20 31 37 20 2a 2f 20 20 22 44 45 4c 45 54 45 20   17 */  "DELETE 
4474e 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67 6d  FROM %Q.'%q_segm
4474f 65 6e 74 73 27 20 57 48 45 52 45 20 62 6c 6f 63  ents' WHERE bloc
44750 6b 69 64 20 42 45 54 57 45 45 4e 20 3f 20 41 4e  kid BETWEEN ? AN
44751 44 20 3f 22 2c 0a 2f 2a 20 31 38 20 2a 2f 20 20  D ?",./* 18 */  
44752 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51 2e  "INSERT INTO %Q.
44753 27 25 71 5f 63 6f 6e 74 65 6e 74 27 20 56 41 4c  '%q_content' VAL
44754 55 45 53 28 25 73 29 22 2c 0a 2f 2a 20 31 39 20  UES(%s)",./* 19 
44755 2a 2f 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d  */  "DELETE FROM
44756 20 25 51 2e 27 25 71 5f 64 6f 63 73 69 7a 65 27   %Q.'%q_docsize'
44757 20 57 48 45 52 45 20 64 6f 63 69 64 20 3d 20 3f   WHERE docid = ?
44758 22 2c 0a 2f 2a 20 32 30 20 2a 2f 20 20 22 52 45  ",./* 20 */  "RE
44759 50 4c 41 43 45 20 49 4e 54 4f 20 25 51 2e 27 25  PLACE INTO %Q.'%
4475a 71 5f 64 6f 63 73 69 7a 65 27 20 56 41 4c 55 45  q_docsize' VALUE
4475b 53 28 3f 2c 3f 29 22 2c 0a 2f 2a 20 32 31 20 2a  S(?,?)",./* 21 *
4475c 2f 20 20 22 53 45 4c 45 43 54 20 73 69 7a 65 20  /  "SELECT size 
4475d 46 52 4f 4d 20 25 51 2e 27 25 71 5f 64 6f 63 73  FROM %Q.'%q_docs
4475e 69 7a 65 27 20 57 48 45 52 45 20 64 6f 63 69 64  ize' WHERE docid
4475f 3d 3f 22 2c 0a 2f 2a 20 32 32 20 2a 2f 20 20 22  =?",./* 22 */  "
44760 53 45 4c 45 43 54 20 76 61 6c 75 65 20 46 52 4f  SELECT value FRO
44761 4d 20 25 51 2e 27 25 71 5f 73 74 61 74 27 20 57  M %Q.'%q_stat' W
44762 48 45 52 45 20 69 64 3d 3f 22 2c 0a 2f 2a 20 32  HERE id=?",./* 2
44763 33 20 2a 2f 20 20 22 52 45 50 4c 41 43 45 20 49  3 */  "REPLACE I
44764 4e 54 4f 20 25 51 2e 27 25 71 5f 73 74 61 74 27  NTO %Q.'%q_stat'
44765 20 56 41 4c 55 45 53 28 3f 2c 3f 29 22 2c 0a 2f   VALUES(?,?)",./
44766 2a 20 32 34 20 2a 2f 20 20 22 22 2c 0a 2f 2a 20  * 24 */  "",./* 
44767 32 35 20 2a 2f 20 20 22 22 2c 0a 0a 2f 2a 20 32  25 */  "",../* 2
44768 36 20 2a 2f 20 22 44 45 4c 45 54 45 20 46 52 4f  6 */ "DELETE FRO
44769 4d 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27  M %Q.'%q_segdir'
4476a 20 57 48 45 52 45 20 6c 65 76 65 6c 20 42 45 54   WHERE level BET
4476b 57 45 45 4e 20 3f 20 41 4e 44 20 3f 22 2c 0a 2f  WEEN ? AND ?",./
4476c 2a 20 32 37 20 2a 2f 20 22 53 45 4c 45 43 54 20  * 27 */ "SELECT 
4476d 44 49 53 54 49 4e 43 54 20 6c 65 76 65 6c 20 2f  DISTINCT level /
4476e 20 28 31 30 32 34 20 2a 20 3f 29 20 46 52 4f 4d   (1024 * ?) FROM
4476f 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27 22   %Q.'%q_segdir'"
44770 2c 0a 0a 2f 2a 20 54 68 69 73 20 73 74 61 74 65  ,../* This state
44771 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20  ment is used to 
44772 64 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68 20  determine which 
44773 6c 65 76 65 6c 20 74 6f 20 72 65 61 64 20 74 68  level to read th
44774 65 20 69 6e 70 75 74 20 66 72 6f 6d 0a 2a 2a 20  e input from.** 
44775 77 68 65 6e 20 70 65 72 66 6f 72 6d 69 6e 67 20  when performing 
44776 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 6d  an incremental m
44777 65 72 67 65 2e 20 49 74 20 72 65 74 75 72 6e 73  erge. It returns
44778 20 74 68 65 20 61 62 73 6f 6c 75 74 65 20 6c 65   the absolute le
44779 76 65 6c 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66  vel number.** of
4477a 20 74 68 65 20 6f 6c 64 65 73 74 20 6c 65 76 65   the oldest leve
4477b 6c 20 69 6e 20 74 68 65 20 64 62 20 74 68 61 74  l in the db that
4477c 20 63 6f 6e 74 61 69 6e 73 20 61 74 20 6c 65 61   contains at lea
4477d 73 74 20 3f 20 73 65 67 6d 65 6e 74 73 2e 20 4f  st ? segments. O
4477e 72 2c 0a 2a 2a 20 69 66 20 6e 6f 20 6c 65 76 65  r,.** if no leve
4477f 6c 20 69 6e 20 74 68 65 20 46 54 53 20 69 6e 64  l in the FTS ind
44780 65 78 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65  ex contains more
44781 20 74 68 61 6e 20 3f 20 73 65 67 6d 65 6e 74 73   than ? segments
44782 2c 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a  , the statement.
44783 2a 2a 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 20  ** returns zero 
44784 72 6f 77 73 2e 20 20 2a 2f 0a 2f 2a 20 32 38 20  rows.  */./* 28 
44785 2a 2f 20 22 53 45 4c 45 43 54 20 6c 65 76 65 6c  */ "SELECT level
44786 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67   FROM %Q.'%q_seg
44787 64 69 72 27 20 47 52 4f 55 50 20 42 59 20 6c 65  dir' GROUP BY le
44788 76 65 6c 20 48 41 56 49 4e 47 20 63 6f 75 6e 74  vel HAVING count
44789 28 2a 29 3e 3d 3f 22 0a 20 20 20 20 20 20 20 20  (*)>=?".        
4478a 20 22 20 20 4f 52 44 45 52 20 42 59 20 28 6c 65   "  ORDER BY (le
4478b 76 65 6c 20 25 25 20 31 30 32 34 29 20 41 53 43  vel %% 1024) ASC
4478c 20 4c 49 4d 49 54 20 31 22 2c 0a 0a 2f 2a 20 45   LIMIT 1",../* E
4478d 73 74 69 6d 61 74 65 20 74 68 65 20 75 70 70 65  stimate the uppe
4478e 72 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e  r limit on the n
4478f 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 6e 6f  umber of leaf no
44790 64 65 73 20 69 6e 20 61 20 6e 65 77 20 73 65 67  des in a new seg
44791 6d 65 6e 74 0a 2a 2a 20 63 72 65 61 74 65 64 20  ment.** created 
44792 62 79 20 6d 65 72 67 69 6e 67 20 74 68 65 20 6f  by merging the o
44793 6c 64 65 73 74 20 3a 32 20 73 65 67 6d 65 6e 74  ldest :2 segment
44794 73 20 66 72 6f 6d 20 61 62 73 6f 6c 75 74 65 20  s from absolute 
44795 6c 65 76 65 6c 20 3a 31 2e 20 53 65 65 20 0a 2a  level :1. See .*
44796 2a 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74  * function sqlit
44797 65 33 46 74 73 33 49 6e 63 72 6d 65 72 67 65 28  e3Fts3Incrmerge(
44798 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 20  ) for details.  
44799 2a 2f 0a 2f 2a 20 32 39 20 2a 2f 20 22 53 45 4c  */./* 29 */ "SEL
4479a 45 43 54 20 32 20 2a 20 74 6f 74 61 6c 28 31 20  ECT 2 * total(1 
4479b 2b 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f  + leaves_end_blo
4479c 63 6b 20 2d 20 73 74 61 72 74 5f 62 6c 6f 63 6b  ck - start_block
4479d 29 20 22 0a 20 20 20 20 20 20 20 20 20 22 20 20  ) ".         "  
4479e 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67 64  FROM %Q.'%q_segd
4479f 69 72 27 20 57 48 45 52 45 20 6c 65 76 65 6c 20  ir' WHERE level 
447a0 3d 20 3f 20 41 4e 44 20 69 64 78 20 3c 20 3f 22  = ? AND idx < ?"
447a1 2c 0a 0a 2f 2a 20 53 51 4c 5f 44 45 4c 45 54 45  ,../* SQL_DELETE
447a2 5f 53 45 47 44 49 52 5f 45 4e 54 52 59 0a 2a 2a  _SEGDIR_ENTRY.**
447a3 20 20 20 44 65 6c 65 74 65 20 74 68 65 20 25 5f     Delete the %_
447a4 73 65 67 64 69 72 20 65 6e 74 72 79 20 6f 6e 20  segdir entry on 
447a5 61 62 73 6f 6c 75 74 65 20 6c 65 76 65 6c 20 3a  absolute level :
447a6 31 20 77 69 74 68 20 69 6e 64 65 78 20 3a 32 2e  1 with index :2.
447a7 20 20 2a 2f 0a 2f 2a 20 33 30 20 2a 2f 20 22 44    */./* 30 */ "D
447a8 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 27 25  ELETE FROM %Q.'%
447a9 71 5f 73 65 67 64 69 72 27 20 57 48 45 52 45 20  q_segdir' WHERE 
447aa 6c 65 76 65 6c 20 3d 20 3f 20 41 4e 44 20 69 64  level = ? AND id
447ab 78 20 3d 20 3f 22 2c 0a 0a 2f 2a 20 53 51 4c 5f  x = ?",../* SQL_
447ac 53 48 49 46 54 5f 53 45 47 44 49 52 5f 45 4e 54  SHIFT_SEGDIR_ENT
447ad 52 59 0a 2a 2a 20 20 20 4d 6f 64 69 66 79 20 74  RY.**   Modify t
447ae 68 65 20 69 64 78 20 76 61 6c 75 65 20 66 6f 72  he idx value for
447af 20 74 68 65 20 73 65 67 6d 65 6e 74 20 77 69 74   the segment wit
447b0 68 20 69 64 78 3d 3a 33 20 6f 6e 20 61 62 73 6f  h idx=:3 on abso
447b1 6c 75 74 65 20 6c 65 76 65 6c 20 3a 32 0a 2a 2a  lute level :2.**
447b2 20 20 20 74 6f 20 3a 31 2e 20 20 2a 2f 0a 2f 2a     to :1.  */./*
447b3 20 33 31 20 2a 2f 20 22 55 50 44 41 54 45 20 25   31 */ "UPDATE %
447b4 51 2e 27 25 71 5f 73 65 67 64 69 72 27 20 53 45  Q.'%q_segdir' SE
447b5 54 20 69 64 78 20 3d 20 3f 20 57 48 45 52 45 20  T idx = ? WHERE 
447b6 6c 65 76 65 6c 3d 3f 20 41 4e 44 20 69 64 78 3d  level=? AND idx=
447b7 3f 22 2c 0a 0a 2f 2a 20 53 51 4c 5f 53 45 4c 45  ?",../* SQL_SELE
447b8 43 54 5f 53 45 47 44 49 52 0a 2a 2a 20 20 20 52  CT_SEGDIR.**   R
447b9 65 61 64 20 61 20 73 69 6e 67 6c 65 20 65 6e 74  ead a single ent
447ba 72 79 20 66 72 6f 6d 20 74 68 65 20 25 5f 73 65  ry from the %_se
447bb 67 64 69 72 20 74 61 62 6c 65 2e 20 54 68 65 20  gdir table. The 
447bc 65 6e 74 72 79 20 66 72 6f 6d 20 61 62 73 6f 6c  entry from absol
447bd 75 74 65 20 0a 2a 2a 20 20 20 6c 65 76 65 6c 20  ute .**   level 
447be 3a 31 20 77 69 74 68 20 69 6e 64 65 78 20 76 61  :1 with index va
447bf 6c 75 65 20 3a 32 2e 20 20 2a 2f 0a 2f 2a 20 33  lue :2.  */./* 3
447c0 32 20 2a 2f 20 20 22 53 45 4c 45 43 54 20 69 64  2 */  "SELECT id
447c1 78 2c 20 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 20  x, start_block, 
447c2 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b  leaves_end_block
447c3 2c 20 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f  , end_block, roo
447c4 74 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  t ".            
447c5 22 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67  "FROM %Q.'%q_seg
447c6 64 69 72 27 20 57 48 45 52 45 20 6c 65 76 65 6c  dir' WHERE level
447c7 20 3d 20 3f 20 41 4e 44 20 69 64 78 20 3d 20 3f   = ? AND idx = ?
447c8 22 2c 0a 0a 2f 2a 20 53 51 4c 5f 43 48 4f 4d 50  ",../* SQL_CHOMP
447c9 5f 53 45 47 44 49 52 0a 2a 2a 20 20 20 55 70 64  _SEGDIR.**   Upd
447ca 61 74 65 20 74 68 65 20 73 74 61 72 74 5f 62 6c  ate the start_bl
447cb 6f 63 6b 20 28 3a 31 29 20 61 6e 64 20 72 6f 6f  ock (:1) and roo
447cc 74 20 28 3a 32 29 20 66 69 65 6c 64 73 20 6f 66  t (:2) fields of
447cd 20 74 68 65 20 25 5f 73 65 67 64 69 72 0a 2a 2a   the %_segdir.**
447ce 20 20 20 65 6e 74 72 79 20 6c 6f 63 61 74 65 64     entry located
447cf 20 6f 6e 20 61 62 73 6f 6c 75 74 65 20 6c 65 76   on absolute lev
447d0 65 6c 20 3a 33 20 77 69 74 68 20 69 6e 64 65 78  el :3 with index
447d1 20 3a 34 2e 20 20 2a 2f 0a 2f 2a 20 33 33 20 2a   :4.  */./* 33 *
447d2 2f 20 20 22 55 50 44 41 54 45 20 25 51 2e 27 25  /  "UPDATE %Q.'%
447d3 71 5f 73 65 67 64 69 72 27 20 53 45 54 20 73 74  q_segdir' SET st
447d4 61 72 74 5f 62 6c 6f 63 6b 20 3d 20 3f 2c 20 72  art_block = ?, r
447d5 6f 6f 74 20 3d 20 3f 22 0a 20 20 20 20 20 20 20  oot = ?".       
447d6 20 20 20 20 20 22 57 48 45 52 45 20 6c 65 76 65       "WHERE leve
447d7 6c 20 3d 20 3f 20 41 4e 44 20 69 64 78 20 3d 20  l = ? AND idx = 
447d8 3f 22 2c 0a 0a 2f 2a 20 53 51 4c 5f 53 45 47 4d  ?",../* SQL_SEGM
447d9 45 4e 54 5f 49 53 5f 41 50 50 45 4e 44 41 42 4c  ENT_IS_APPENDABL
447da 45 0a 2a 2a 20 20 20 52 65 74 75 72 6e 20 61 20  E.**   Return a 
447db 73 69 6e 67 6c 65 20 72 6f 77 20 69 66 20 74 68  single row if th
447dc 65 20 73 65 67 6d 65 6e 74 20 77 69 74 68 20 65  e segment with e
447dd 6e 64 5f 62 6c 6f 63 6b 3d 3f 20 69 73 20 61 70  nd_block=? is ap
447de 70 65 6e 64 61 62 6c 65 2e 20 4f 72 0a 2a 2a 20  pendable. Or.** 
447df 20 20 6e 6f 20 72 6f 77 73 20 6f 74 68 65 72 77    no rows otherw
447e0 69 73 65 2e 20 20 2a 2f 0a 2f 2a 20 33 34 20 2a  ise.  */./* 34 *
447e1 2f 20 20 22 53 45 4c 45 43 54 20 31 20 46 52 4f  /  "SELECT 1 FRO
447e2 4d 20 25 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74  M %Q.'%q_segment
447e3 73 27 20 57 48 45 52 45 20 62 6c 6f 63 6b 69 64  s' WHERE blockid
447e4 3d 3f 20 41 4e 44 20 62 6c 6f 63 6b 20 49 53 20  =? AND block IS 
447e5 4e 55 4c 4c 22 2c 0a 0a 2f 2a 20 53 51 4c 5f 53  NULL",../* SQL_S
447e6 45 4c 45 43 54 5f 49 4e 44 45 58 45 53 0a 2a 2a  ELECT_INDEXES.**
447e7 20 20 20 52 65 74 75 72 6e 20 74 68 65 20 6c 69     Return the li
447e8 73 74 20 6f 66 20 76 61 6c 69 64 20 73 65 67 6d  st of valid segm
447e9 65 6e 74 20 69 6e 64 65 78 65 73 20 66 6f 72 20  ent indexes for 
447ea 61 62 73 6f 6c 75 74 65 20 6c 65 76 65 6c 20 3f  absolute level ?
447eb 20 20 2a 2f 0a 2f 2a 20 33 35 20 2a 2f 20 20 22    */./* 35 */  "
447ec 53 45 4c 45 43 54 20 69 64 78 20 46 52 4f 4d 20  SELECT idx FROM 
447ed 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27 20 57  %Q.'%q_segdir' W
447ee 48 45 52 45 20 6c 65 76 65 6c 3d 3f 20 4f 52 44  HERE level=? ORD
447ef 45 52 20 42 59 20 31 20 41 53 43 22 2c 0a 0a 2f  ER BY 1 ASC",../
447f0 2a 20 53 51 4c 5f 53 45 4c 45 43 54 5f 4d 58 4c  * SQL_SELECT_MXL
447f1 45 56 45 4c 0a 2a 2a 20 20 20 52 65 74 75 72 6e  EVEL.**   Return
447f2 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 65 6c   the largest rel
447f3 61 74 69 76 65 20 6c 65 76 65 6c 20 69 6e 20 74  ative level in t
447f4 68 65 20 46 54 53 20 69 6e 64 65 78 20 6f 72 20  he FTS index or 
447f5 69 6e 64 65 78 65 73 2e 20 20 2a 2f 0a 2f 2a 20  indexes.  */./* 
447f6 33 36 20 2a 2f 20 20 22 53 45 4c 45 43 54 20 6d  36 */  "SELECT m
447f7 61 78 28 20 6c 65 76 65 6c 20 25 25 20 31 30 32  ax( level %% 102
447f8 34 20 29 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f  4 ) FROM %Q.'%q_
447f9 73 65 67 64 69 72 27 22 0a 20 20 7d 3b 0a 20 20  segdir'".  };.  
447fa 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
447fb 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  OK;.  sqlite3_st
447fc 6d 74 20 2a 70 53 74 6d 74 3b 0a 0a 20 20 61 73  mt *pStmt;..  as
447fd 73 65 72 74 28 20 53 69 7a 65 6f 66 41 72 72 61  sert( SizeofArra
447fe 79 28 61 7a 53 71 6c 29 3d 3d 53 69 7a 65 6f 66  y(azSql)==Sizeof
447ff 41 72 72 61 79 28 70 2d 3e 61 53 74 6d 74 29 20  Array(p->aStmt) 
44800 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 53 74  );.  assert( eSt
44801 6d 74 3c 53 69 7a 65 6f 66 41 72 72 61 79 28 61  mt<SizeofArray(a
44802 7a 53 71 6c 29 20 26 26 20 65 53 74 6d 74 3e 3d  zSql) && eStmt>=
44803 30 20 29 3b 0a 20 20 0a 20 20 70 53 74 6d 74 20  0 );.  .  pStmt 
44804 3d 20 70 2d 3e 61 53 74 6d 74 5b 65 53 74 6d 74  = p->aStmt[eStmt
44805 5d 3b 0a 20 20 69 66 28 20 21 70 53 74 6d 74 20  ];.  if( !pStmt 
44806 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71  ){.    char *zSq
44807 6c 3b 0a 20 20 20 20 69 66 28 20 65 53 74 6d 74  l;.    if( eStmt
44808 3d 3d 53 51 4c 5f 43 4f 4e 54 45 4e 54 5f 49 4e  ==SQL_CONTENT_IN
44809 53 45 52 54 20 29 7b 0a 20 20 20 20 20 20 7a 53  SERT ){.      zS
4480a 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ql = sqlite3_mpr
4480b 69 6e 74 66 28 61 7a 53 71 6c 5b 65 53 74 6d 74  intf(azSql[eStmt
4480c 5d 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e  ], p->zDb, p->zN
4480d 61 6d 65 2c 20 70 2d 3e 7a 57 72 69 74 65 45 78  ame, p->zWriteEx
4480e 70 72 6c 69 73 74 29 3b 0a 20 20 20 20 7d 65 6c  prlist);.    }el
4480f 73 65 20 69 66 28 20 65 53 74 6d 74 3d 3d 53 51  se if( eStmt==SQ
44810 4c 5f 53 45 4c 45 43 54 5f 43 4f 4e 54 45 4e 54  L_SELECT_CONTENT
44811 5f 42 59 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20  _BY_ROWID ){.   
44812 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65     zSql = sqlite
44813 33 5f 6d 70 72 69 6e 74 66 28 61 7a 53 71 6c 5b  3_mprintf(azSql[
44814 65 53 74 6d 74 5d 2c 20 70 2d 3e 7a 52 65 61 64  eStmt], p->zRead
44815 45 78 70 72 6c 69 73 74 29 3b 0a 20 20 20 20 7d  Exprlist);.    }
44816 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 53 71 6c  else{.      zSql
44817 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
44818 74 66 28 61 7a 53 71 6c 5b 65 53 74 6d 74 5d 2c  tf(azSql[eStmt],
44819 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d   p->zDb, p->zNam
4481a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  e);.    }.    if
4481b 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20  ( !zSql ){.     
4481c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
4481d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  EM;.    }else{. 
4481e 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
4481f 33 5f 70 72 65 70 61 72 65 5f 76 32 28 70 2d 3e  3_prepare_v2(p->
44820 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70  db, zSql, -1, &p
44821 53 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20  Stmt, NULL);.   
44822 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
44823 7a 53 71 6c 29 3b 0a 20 20 20 20 20 20 61 73 73  zSql);.      ass
44824 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
44825 4f 4b 20 7c 7c 20 70 53 74 6d 74 3d 3d 30 20 29  OK || pStmt==0 )
44826 3b 0a 20 20 20 20 20 20 70 2d 3e 61 53 74 6d 74  ;.      p->aStmt
44827 5b 65 53 74 6d 74 5d 20 3d 20 70 53 74 6d 74 3b  [eStmt] = pStmt;
44828 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
44829 20 61 70 56 61 6c 20 29 7b 0a 20 20 20 20 69 6e   apVal ){.    in
4482a 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 6e 50 61  t i;.    int nPa
4482b 72 61 6d 20 3d 20 73 71 6c 69 74 65 33 5f 62 69  ram = sqlite3_bi
4482c 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75  nd_parameter_cou
4482d 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 66  nt(pStmt);.    f
4482e 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49  or(i=0; rc==SQLI
4482f 54 45 5f 4f 4b 20 26 26 20 69 3c 6e 50 61 72 61  TE_OK && i<nPara
44830 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72  m; i++){.      r
44831 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
44832 5f 76 61 6c 75 65 28 70 53 74 6d 74 2c 20 69 2b  _value(pStmt, i+
44833 31 2c 20 61 70 56 61 6c 5b 69 5d 29 3b 0a 20 20  1, apVal[i]);.  
44834 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20    }.  }.  *pp = 
44835 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20  pStmt;.  return 
44836 72 63 3b 0a 7d 0a 0a 0a 73 74 61 74 69 63 20 69  rc;.}...static i
44837 6e 74 20 66 74 73 33 53 65 6c 65 63 74 44 6f 63  nt fts3SelectDoc
44838 73 69 7a 65 28 0a 20 20 46 74 73 33 54 61 62 6c  size(.  Fts3Tabl
44839 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20  e *pTab,        
4483a 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53 33 20          /* FTS3 
4483b 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  table handle */.
4483c 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
4483d 69 44 6f 63 69 64 2c 20 20 20 20 20 20 20 20 20  iDocid,         
4483e 20 20 2f 2a 20 44 6f 63 69 64 20 74 6f 20 62 69    /* Docid to bi
4483f 6e 64 20 66 6f 72 20 53 51 4c 5f 53 45 4c 45 43  nd for SQL_SELEC
44840 54 5f 44 4f 43 53 49 5a 45 20 2a 2f 0a 20 20 73  T_DOCSIZE */.  s
44841 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
44842 53 74 6d 74 20 20 20 20 20 20 20 20 20 20 20 2f  Stmt           /
44843 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74  * OUT: Statement
44844 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 7b 0a 20 20   handle */.){.  
44845 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
44846 74 6d 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  tmt = 0;        
44847 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 72 65 71  /* Statement req
44848 75 65 73 74 65 64 20 66 72 6f 6d 20 66 74 73 33  uested from fts3
44849 53 71 6c 53 74 6d 74 28 29 20 2a 2f 0a 20 20 69  SqlStmt() */.  i
4484a 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
4484b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4484c 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
4484d 0a 0a 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c  ..  rc = fts3Sql
4484e 53 74 6d 74 28 70 54 61 62 2c 20 53 51 4c 5f 53  Stmt(pTab, SQL_S
4484f 45 4c 45 43 54 5f 44 4f 43 53 49 5a 45 2c 20 26  ELECT_DOCSIZE, &
44850 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28  pStmt, 0);.  if(
44851 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
44852 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69  {.    sqlite3_bi
44853 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c 20  nd_int64(pStmt, 
44854 31 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20 20 20  1, iDocid);.    
44855 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
44856 70 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 69 66  p(pStmt);.    if
44857 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc!=SQLITE_ROW
44858 20 7c 7c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   || sqlite3_colu
44859 6d 6e 5f 74 79 70 65 28 70 53 74 6d 74 2c 20 30  mn_type(pStmt, 0
4485a 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29  )!=SQLITE_BLOB )
4485b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
4485c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74  ite3_reset(pStmt
4485d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
4485e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20  =SQLITE_OK ) rc 
4485f 3d 20 46 54 53 5f 43 4f 52 52 55 50 54 5f 56 54  = FTS_CORRUPT_VT
44860 41 42 3b 0a 20 20 20 20 20 20 70 53 74 6d 74 20  AB;.      pStmt 
44861 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 0;.    }else{.
44862 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
44863 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  E_OK;.    }.  }.
44864 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20 70 53 74  .  *ppStmt = pSt
44865 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  mt;.  return rc;
44866 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
44867 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
44868 73 33 53 65 6c 65 63 74 44 6f 63 74 6f 74 61 6c  s3SelectDoctotal
44869 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  (.  Fts3Table *p
4486a 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  Tab,            
4486b 20 20 20 20 2f 2a 20 46 74 73 33 20 74 61 62 6c      /* Fts3 tabl
4486c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 73 71  e handle */.  sq
4486d 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
4486e 74 6d 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a  tmt           /*
4486f 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20   OUT: Statement 
44870 68 61 6e 64 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73  handle */.){.  s
44871 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
44872 6d 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63  mt = 0;.  int rc
44873 3b 0a 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c  ;.  rc = fts3Sql
44874 53 74 6d 74 28 70 54 61 62 2c 20 53 51 4c 5f 53  Stmt(pTab, SQL_S
44875 45 4c 45 43 54 5f 53 54 41 54 2c 20 26 70 53 74  ELECT_STAT, &pSt
44876 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  mt, 0);.  if( rc
44877 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
44878 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
44879 69 6e 74 28 70 53 74 6d 74 2c 20 31 2c 20 46 54  int(pStmt, 1, FT
4487a 53 5f 53 54 41 54 5f 44 4f 43 54 4f 54 41 4c 29  S_STAT_DOCTOTAL)
4487b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
4487c 33 5f 73 74 65 70 28 70 53 74 6d 74 29 21 3d 53  3_step(pStmt)!=S
4487d 51 4c 49 54 45 5f 52 4f 57 0a 20 20 20 20 20 7c  QLITE_ROW.     |
4487e 7c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  | sqlite3_column
4487f 5f 74 79 70 65 28 70 53 74 6d 74 2c 20 30 29 21  _type(pStmt, 0)!
44880 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 0a 20 20 20  =SQLITE_BLOB.   
44881 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
44882 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74  qlite3_reset(pSt
44883 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  mt);.      if( r
44884 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c==SQLITE_OK ) r
44885 63 20 3d 20 46 54 53 5f 43 4f 52 52 55 50 54 5f  c = FTS_CORRUPT_
44886 56 54 41 42 3b 0a 20 20 20 20 20 20 70 53 74 6d  VTAB;.      pStm
44887 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  t = 0;.    }.  }
44888 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20 70 53 74  .  *ppStmt = pSt
44889 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  mt;.  return rc;
4488a 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
4488b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
4488c 73 33 53 65 6c 65 63 74 44 6f 63 73 69 7a 65 28  s3SelectDocsize(
4488d 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 54  .  Fts3Table *pT
4488e 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ab,             
4488f 20 20 20 2f 2a 20 46 74 73 33 20 74 61 62 6c 65     /* Fts3 table
44890 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 73 71 6c   handle */.  sql
44891 69 74 65 33 5f 69 6e 74 36 34 20 69 44 6f 63 69  ite3_int64 iDoci
44892 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  d,           /* 
44893 44 6f 63 69 64 20 74 6f 20 72 65 61 64 20 73 69  Docid to read si
44894 7a 65 20 64 61 74 61 20 66 6f 72 20 2a 2f 0a 20  ze data for */. 
44895 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
44896 70 70 53 74 6d 74 20 20 20 20 20 20 20 20 20 20  ppStmt          
44897 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65   /* OUT: Stateme
44898 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 7b 0a  nt handle */.){.
44899 20 20 72 65 74 75 72 6e 20 66 74 73 33 53 65 6c    return fts3Sel
4489a 65 63 74 44 6f 63 73 69 7a 65 28 70 54 61 62 2c  ectDocsize(pTab,
4489b 20 69 44 6f 63 69 64 2c 20 70 70 53 74 6d 74 29   iDocid, ppStmt)
4489c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 6d 69 6c  ;.}../*.** Simil
4489d 61 72 20 74 6f 20 66 74 73 33 53 71 6c 53 74 6d  ar to fts3SqlStm
4489e 74 28 29 2e 20 45 78 63 65 70 74 2c 20 61 66 74  t(). Except, aft
4489f 65 72 20 62 69 6e 64 69 6e 67 20 74 68 65 20 70  er binding the p
448a0 61 72 61 6d 65 74 65 72 73 20 69 6e 0a 2a 2a 20  arameters in.** 
448a1 61 72 72 61 79 20 61 70 56 61 6c 5b 5d 20 74 6f  array apVal[] to
448a2 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
448a3 6e 74 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  nt identified by
448a4 20 65 53 74 6d 74 2c 20 74 68 65 20 73 74 61 74   eStmt, the stat
448a5 65 6d 65 6e 74 0a 2a 2a 20 69 73 20 65 78 65 63  ement.** is exec
448a6 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  uted..**.** Retu
448a7 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66  rns SQLITE_OK if
448a8 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
448a9 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 65  s successfully e
448aa 78 65 63 75 74 65 64 2c 20 6f 72 20 61 6e 0a 2a  xecuted, or an.*
448ab 2a 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  * SQLite error c
448ac 6f 64 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a  ode otherwise..*
448ad 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  /.static void ft
448ae 73 33 53 71 6c 45 78 65 63 28 0a 20 20 69 6e 74  s3SqlExec(.  int
448af 20 2a 70 52 43 2c 20 20 20 20 20 20 20 20 20 20   *pRC,          
448b0 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
448b1 63 6f 64 65 20 2a 2f 0a 20 20 46 74 73 33 54 61  code */.  Fts3Ta
448b2 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ble *p,         
448b3 20 20 20 2f 2a 20 54 68 65 20 46 54 53 33 20 74     /* The FTS3 t
448b4 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 65 53  able */.  int eS
448b5 74 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  tmt,            
448b6 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 73     /* Index of s
448b7 74 61 74 65 6d 65 6e 74 20 74 6f 20 65 76 61 6c  tatement to eval
448b8 75 61 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  uate */.  sqlite
448b9 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 20  3_value **apVal 
448ba 20 20 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 73     /* Parameters
448bb 20 74 6f 20 62 69 6e 64 20 2a 2f 0a 29 7b 0a 20   to bind */.){. 
448bc 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
448bd 53 74 6d 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  Stmt;.  int rc;.
448be 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 74    if( *pRC ) ret
448bf 75 72 6e 3b 0a 20 20 72 63 20 3d 20 66 74 73 33  urn;.  rc = fts3
448c0 53 71 6c 53 74 6d 74 28 70 2c 20 65 53 74 6d 74  SqlStmt(p, eStmt
448c1 2c 20 26 70 53 74 6d 74 2c 20 61 70 56 61 6c 29  , &pStmt, apVal)
448c2 3b 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ; .  if( rc==SQL
448c3 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
448c4 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
448c5 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
448c6 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74 29  te3_reset(pStmt)
448c7 3b 0a 20 20 7d 0a 20 20 2a 70 52 43 20 3d 20 72  ;.  }.  *pRC = r
448c8 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  c;.}.../*.** Thi
448c9 73 20 66 75 6e 63 74 69 6f 6e 20 65 6e 73 75 72  s function ensur
448ca 65 73 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c  es that the call
448cb 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20  er has obtained 
448cc 61 20 73 68 61 72 65 64 2d 63 61 63 68 65 0a 2a  a shared-cache.*
448cd 2a 20 74 61 62 6c 65 2d 6c 6f 63 6b 20 6f 6e 20  * table-lock on 
448ce 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61  the %_content ta
448cf 62 6c 65 2e 20 54 68 69 73 20 69 73 20 72 65 71  ble. This is req
448d0 75 69 72 65 64 20 62 65 66 6f 72 65 20 72 65 61  uired before rea
448d1 64 69 6e 67 0a 2a 2a 20 64 61 74 61 20 66 72 6f  ding.** data fro
448d2 6d 20 74 68 65 20 66 74 73 33 20 74 61 62 6c 65  m the fts3 table
448d3 2e 20 49 66 20 74 68 69 73 20 6c 6f 63 6b 20 69  . If this lock i
448d4 73 20 6e 6f 74 20 61 63 71 75 69 72 65 64 20 66  s not acquired f
448d5 69 72 73 74 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  irst, then.** th
448d6 65 20 63 61 6c 6c 65 72 20 6d 61 79 20 65 6e 64  e caller may end
448d7 20 75 70 20 68 6f 6c 64 69 6e 67 20 72 65 61 64   up holding read
448d8 2d 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 25 5f  -locks on the %_
448d9 73 65 67 6d 65 6e 74 73 20 61 6e 64 20 25 5f 73  segments and %_s
448da 65 67 64 69 72 0a 2a 2a 20 74 61 62 6c 65 73 2c  egdir.** tables,
448db 20 62 75 74 20 6e 6f 20 72 65 61 64 2d 6c 6f 63   but no read-loc
448dc 6b 20 6f 6e 20 74 68 65 20 25 5f 63 6f 6e 74 65  k on the %_conte
448dd 6e 74 20 74 61 62 6c 65 2e 20 49 66 20 74 68 69  nt table. If thi
448de 73 20 68 61 70 70 65 6e 73 20 0a 2a 2a 20 61 20  s happens .** a 
448df 73 65 63 6f 6e 64 20 63 6f 6e 6e 65 63 74 69 6f  second connectio
448e0 6e 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 20 74  n will be able t
448e1 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 66  o write to the f
448e2 74 73 33 20 74 61 62 6c 65 2c 20 62 75 74 0a 2a  ts3 table, but.*
448e3 2a 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  * attempting to 
448e4 63 6f 6d 6d 69 74 20 74 68 6f 73 65 20 77 72 69  commit those wri
448e5 74 65 73 20 6d 69 67 68 74 20 72 65 74 75 72 6e  tes might return
448e6 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 6f   SQLITE_LOCKED o
448e7 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  r.** SQLITE_LOCK
448e8 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 28  ED_SHAREDCACHE (
448e9 62 65 63 61 75 73 65 20 74 68 65 20 63 6f 6d 6d  because the comm
448ea 69 74 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6f  it attempts to o
448eb 62 74 61 69 6e 20 0a 2a 2a 20 77 72 69 74 65 2d  btain .** write-
448ec 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 25 5f 73  locks on the %_s
448ed 65 67 6d 65 6e 74 73 20 61 6e 64 20 25 5f 73 65  egments and %_se
448ee 67 64 69 72 20 2a 2a 20 74 61 62 6c 65 73 29 2e  gdir ** tables).
448ef 20 0a 2a 2a 0a 2a 2a 20 57 65 20 74 72 79 20 74   .**.** We try t
448f0 6f 20 61 76 6f 69 64 20 74 68 69 73 20 62 65 63  o avoid this bec
448f1 61 75 73 65 20 69 66 20 46 54 53 33 20 72 65 74  ause if FTS3 ret
448f2 75 72 6e 73 20 61 6e 79 20 65 72 72 6f 72 20 77  urns any error w
448f3 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 0a 2a  hen committing.*
448f4 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  * a transaction,
448f5 20 74 68 65 20 77 68 6f 6c 65 20 74 72 61 6e 73   the whole trans
448f6 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 72  action will be r
448f7 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 41 6e 64 20  olled back. And 
448f8 74 68 69 73 20 69 73 0a 2a 2a 20 6e 6f 74 20 77  this is.** not w
448f9 68 61 74 20 75 73 65 72 73 20 65 78 70 65 63 74  hat users expect
448fa 20 77 68 65 6e 20 74 68 65 79 20 67 65 74 20 53   when they get S
448fb 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41  QLITE_LOCKED_SHA
448fc 52 45 44 43 41 43 48 45 2e 20 49 74 20 63 61 6e  REDCACHE. It can
448fd 0a 2a 2a 20 73 74 69 6c 6c 20 68 61 70 70 65 6e  .** still happen
448fe 20 69 66 20 74 68 65 20 75 73 65 72 20 72 65 61   if the user rea
448ff 64 73 20 64 61 74 61 20 64 69 72 65 63 74 6c 79  ds data directly
44900 20 66 72 6f 6d 20 74 68 65 20 25 5f 73 65 67 6d   from the %_segm
44901 65 6e 74 73 20 6f 72 0a 2a 2a 20 25 5f 73 65 67  ents or.** %_seg
44902 64 69 72 20 74 61 62 6c 65 73 20 69 6e 73 74 65  dir tables inste
44903 61 64 20 6f 66 20 67 6f 69 6e 67 20 74 68 72 6f  ad of going thro
44904 75 67 68 20 46 54 53 33 20 74 68 6f 75 67 68 2e  ugh FTS3 though.
44905 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 65 61 73  .**.** This reas
44906 6f 6e 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 61  oning does not a
44907 70 70 6c 79 20 74 6f 20 61 20 63 6f 6e 74 65 6e  pply to a conten
44908 74 3d 78 78 78 20 74 61 62 6c 65 2e 0a 2a 2f 0a  t=xxx table..*/.
44909 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4490a 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 52 65  nt sqlite3Fts3Re
4490b 61 64 4c 6f 63 6b 28 46 74 73 33 54 61 62 6c 65  adLock(Fts3Table
4490c 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20   *p){.  int rc; 
4490d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4490e 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
4490f 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69  n code */.  sqli
44910 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
44911 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
44912 74 61 74 65 6d 65 6e 74 20 75 73 65 64 20 74 6f  tatement used to
44913 20 6f 62 74 61 69 6e 20 6c 6f 63 6b 20 2a 2f 0a   obtain lock */.
44914 0a 20 20 69 66 28 20 70 2d 3e 7a 43 6f 6e 74 65  .  if( p->zConte
44915 6e 74 54 62 6c 3d 3d 30 20 29 7b 0a 20 20 20 20  ntTbl==0 ){.    
44916 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
44917 28 70 2c 20 53 51 4c 5f 53 45 4c 45 43 54 5f 43  (p, SQL_SELECT_C
44918 4f 4e 54 45 4e 54 5f 42 59 5f 52 4f 57 49 44 2c  ONTENT_BY_ROWID,
44919 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20   &pStmt, 0);.   
4491a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
4491b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
4491c 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 53  te3_bind_null(pS
4491d 74 6d 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  tmt, 1);.      s
4491e 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d  qlite3_step(pStm
4491f 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  t);.      rc = s
44920 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74  qlite3_reset(pSt
44921 6d 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  mt);.    }.  }el
44922 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  se{.    rc = SQL
44923 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 72  ITE_OK;.  }..  r
44924 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
44925 2a 2a 20 46 54 53 20 6d 61 69 6e 74 61 69 6e 73  ** FTS maintains
44926 20 61 20 73 65 70 61 72 61 74 65 20 69 6e 64 65   a separate inde
44927 78 65 73 20 66 6f 72 20 65 61 63 68 20 6c 61 6e  xes for each lan
44928 67 75 61 67 65 2d 69 64 20 28 61 20 33 32 2d 62  guage-id (a 32-b
44929 69 74 20 69 6e 74 65 67 65 72 29 2e 0a 2a 2a 20  it integer)..** 
4492a 57 69 74 68 69 6e 20 65 61 63 68 20 6c 61 6e 67  Within each lang
4492b 75 61 67 65 20 69 64 2c 20 61 20 73 65 70 61 72  uage id, a separ
4492c 61 74 65 20 69 6e 64 65 78 20 69 73 20 6d 61 69  ate index is mai
4492d 6e 74 61 69 6e 65 64 20 74 6f 20 73 74 6f 72 65  ntained to store
4492e 20 74 68 65 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74   the.** document
4492f 20 74 65 72 6d 73 2c 20 61 6e 64 20 65 61 63 68   terms, and each
44930 20 63 6f 6e 66 69 67 75 72 65 64 20 70 72 65 66   configured pref
44931 69 78 20 73 69 7a 65 20 28 63 6f 6e 66 69 67 75  ix size (configu
44932 72 65 64 20 74 68 65 20 46 54 53 20 0a 2a 2a 20  red the FTS .** 
44933 22 70 72 65 66 69 78 3d 22 20 6f 70 74 69 6f 6e  "prefix=" option
44934 29 2e 20 41 6e 64 20 65 61 63 68 20 69 6e 64 65  ). And each inde
44935 78 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 6d 75  x consists of mu
44936 6c 74 69 70 6c 65 20 6c 65 76 65 6c 73 20 28 22  ltiple levels ("
44937 72 65 6c 61 74 69 76 65 0a 2a 2a 20 6c 65 76 65  relative.** leve
44938 6c 73 22 29 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20  ls")..**.** All 
44939 74 68 72 65 65 20 6f 66 20 74 68 65 73 65 20 76  three of these v
4493a 61 6c 75 65 73 20 28 74 68 65 20 6c 61 6e 67 75  alues (the langu
4493b 61 67 65 20 69 64 2c 20 74 68 65 20 73 70 65 63  age id, the spec
4493c 69 66 69 63 20 69 6e 64 65 78 20 61 6e 64 20 74  ific index and t
4493d 68 65 0a 2a 2a 20 6c 65 76 65 6c 20 77 69 74 68  he.** level with
4493e 69 6e 20 74 68 65 20 69 6e 64 65 78 29 20 61 72  in the index) ar
4493f 65 20 65 6e 63 6f 64 65 64 20 69 6e 20 36 34 2d  e encoded in 64-
44940 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75  bit integer valu
44941 65 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20  es stored.** in 
44942 74 68 65 20 25 5f 73 65 67 64 69 72 20 74 61 62  the %_segdir tab
44943 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 54 68 69 73  le on disk. This
44944 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
44945 64 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 72  d to convert thr
44946 65 65 0a 2a 2a 20 73 65 70 61 72 61 74 65 20 63  ee.** separate c
44947 6f 6d 70 6f 6e 65 6e 74 20 76 61 6c 75 65 73 20  omponent values 
44948 69 6e 74 6f 20 74 68 65 20 73 69 6e 67 6c 65 20  into the single 
44949 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76  64-bit integer v
4494a 61 6c 75 65 20 74 68 61 74 0a 2a 2a 20 63 61 6e  alue that.** can
4494b 20 62 65 20 75 73 65 64 20 74 6f 20 71 75 65 72   be used to quer
4494c 79 20 74 68 65 20 25 5f 73 65 67 64 69 72 20 74  y the %_segdir t
4494d 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63  able..**.** Spec
4494e 69 66 69 63 61 6c 6c 79 2c 20 65 61 63 68 20 6c  ifically, each l
4494f 61 6e 67 75 61 67 65 2d 69 64 2f 69 6e 64 65 78  anguage-id/index
44950 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 69 73 20   combination is 
44951 61 6c 6c 6f 63 61 74 65 64 20 31 30 32 34 20 0a  allocated 1024 .
44952 2a 2a 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65  ** 64-bit intege
44953 72 20 6c 65 76 65 6c 20 76 61 6c 75 65 73 20 28  r level values (
44954 22 61 62 73 6f 6c 75 74 65 20 6c 65 76 65 6c 73  "absolute levels
44955 22 29 2e 20 54 68 65 20 6d 61 69 6e 20 74 65 72  "). The main ter
44956 6d 73 20 69 6e 64 65 78 0a 2a 2a 20 66 6f 72 20  ms index.** for 
44957 6c 61 6e 67 75 61 67 65 2d 69 64 20 30 20 69 73  language-id 0 is
44958 20 61 6c 6c 6f 63 61 74 65 20 76 61 6c 75 65 73   allocate values
44959 20 30 2d 31 30 32 33 2e 20 54 68 65 20 66 69 72   0-1023. The fir
4495a 73 74 20 70 72 65 66 69 78 20 69 6e 64 65 78 0a  st prefix index.
4495b 2a 2a 20 28 69 66 20 61 6e 79 29 20 66 6f 72 20  ** (if any) for 
4495c 6c 61 6e 67 75 61 67 65 2d 69 64 20 30 20 69 73  language-id 0 is
4495d 20 61 6c 6c 6f 63 61 74 65 64 20 76 61 6c 75 65   allocated value
4495e 73 20 31 30 32 34 2d 32 30 34 37 2e 20 41 6e 64  s 1024-2047. And
4495f 20 73 6f 20 6f 6e 2e 0a 2a 2a 20 4c 61 6e 67 75   so on..** Langu
44960 61 67 65 20 31 20 69 6e 64 65 78 65 73 20 61 72  age 1 indexes ar
44961 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6d 6d 65  e allocated imme
44962 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e  diately followin
44963 67 20 6c 61 6e 67 75 61 67 65 20 30 2e 0a 2a 2a  g language 0..**
44964 0a 2a 2a 20 53 6f 2c 20 66 6f 72 20 61 20 73 79  .** So, for a sy
44965 73 74 65 6d 20 77 69 74 68 20 6e 50 72 65 66 69  stem with nPrefi
44966 78 20 70 72 65 66 69 78 20 69 6e 64 65 78 65 73  x prefix indexes
44967 20 63 6f 6e 66 69 67 75 72 65 64 2c 20 74 68 65   configured, the
44968 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 61 62 73   block of.** abs
44969 6f 6c 75 74 65 20 6c 65 76 65 6c 73 20 74 68 61  olute levels tha
4496a 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  t corresponds to
4496b 20 6c 61 6e 67 75 61 67 65 2d 69 64 20 69 4c 61   language-id iLa
4496c 6e 67 69 64 20 61 6e 64 20 69 6e 64 65 78 20 0a  ngid and index .
4496d 2a 2a 20 69 49 6e 64 65 78 20 73 74 61 72 74 73  ** iIndex starts
4496e 20 61 74 20 61 62 73 6f 6c 75 74 65 20 6c 65 76   at absolute lev
4496f 65 6c 20 28 28 69 4c 61 6e 67 69 64 20 2a 20 28  el ((iLangid * (
44970 6e 50 72 65 66 69 78 2b 31 29 20 2b 20 69 49 6e  nPrefix+1) + iIn
44971 64 65 78 29 20 2a 20 31 30 32 34 29 2e 0a 2a 2f  dex) * 1024)..*/
44972 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
44973 69 6e 74 36 34 20 67 65 74 41 62 73 6f 6c 75 74  int64 getAbsolut
44974 65 4c 65 76 65 6c 28 0a 20 20 46 74 73 33 54 61  eLevel(.  Fts3Ta
44975 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ble *p,         
44976 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53            /* FTS
44977 33 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  3 table handle *
44978 2f 0a 20 20 69 6e 74 20 69 4c 61 6e 67 69 64 2c  /.  int iLangid,
44979 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4497a 20 20 20 20 2f 2a 20 4c 61 6e 67 75 61 67 65 20      /* Language 
4497b 69 64 20 2a 2f 0a 20 20 69 6e 74 20 69 49 6e 64  id */.  int iInd
4497c 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ex,             
4497d 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
4497e 20 69 6e 20 70 2d 3e 61 49 6e 64 65 78 5b 5d 20   in p->aIndex[] 
4497f 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 76 65 6c 20  */.  int iLevel 
44980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44981 20 20 20 20 20 2f 2a 20 4c 65 76 65 6c 20 6f 66       /* Level of
44982 20 73 65 67 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a   segments */.){.
44983 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
44984 69 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 20  iBase;          
44985 20 20 2f 2a 20 46 69 72 73 74 20 61 62 73 6f 6c    /* First absol
44986 75 74 65 20 6c 65 76 65 6c 20 66 6f 72 20 69 4c  ute level for iL
44987 61 6e 67 69 64 2f 69 49 6e 64 65 78 20 2a 2f 0a  angid/iIndex */.
44988 20 20 61 73 73 65 72 74 28 20 69 4c 61 6e 67 69    assert( iLangi
44989 64 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  d>=0 );.  assert
4498a 28 20 70 2d 3e 6e 49 6e 64 65 78 3e 30 20 29 3b  ( p->nIndex>0 );
4498b 0a 20 20 61 73 73 65 72 74 28 20 69 49 6e 64 65  .  assert( iInde
4498c 78 3e 3d 30 20 26 26 20 69 49 6e 64 65 78 3c 70  x>=0 && iIndex<p
4498d 2d 3e 6e 49 6e 64 65 78 20 29 3b 0a 0a 20 20 69  ->nIndex );..  i
4498e 42 61 73 65 20 3d 20 28 28 73 71 6c 69 74 65 33  Base = ((sqlite3
4498f 5f 69 6e 74 36 34 29 69 4c 61 6e 67 69 64 20 2a  _int64)iLangid *
44990 20 70 2d 3e 6e 49 6e 64 65 78 20 2b 20 69 49 6e   p->nIndex + iIn
44991 64 65 78 29 20 2a 20 46 54 53 33 5f 53 45 47 44  dex) * FTS3_SEGD
44992 49 52 5f 4d 41 58 4c 45 56 45 4c 3b 0a 20 20 72  IR_MAXLEVEL;.  r
44993 65 74 75 72 6e 20 69 42 61 73 65 20 2b 20 69 4c  eturn iBase + iL
44994 65 76 65 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  evel;.}../*.** S
44995 65 74 20 2a 70 70 53 74 6d 74 20 74 6f 20 61 20  et *ppStmt to a 
44996 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  statement handle
44997 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65   that may be use
44998 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72  d to iterate thr
44999 6f 75 67 68 0a 2a 2a 20 61 6c 6c 20 72 6f 77 73  ough.** all rows
4499a 20 69 6e 20 74 68 65 20 25 5f 73 65 67 64 69 72   in the %_segdir
4499b 20 74 61 62 6c 65 2c 20 66 72 6f 6d 20 6f 6c 64   table, from old
4499c 65 73 74 20 74 6f 20 6e 65 77 65 73 74 2e 20 49  est to newest. I
4499d 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a  f successful,.**
4499e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
4499f 4b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  K. If an error o
449a0 63 63 75 72 73 20 77 68 69 6c 65 20 70 72 65 70  ccurs while prep
449a1 61 72 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d  aring the statem
449a2 65 6e 74 2c 20 0a 2a 2a 20 72 65 74 75 72 6e 20  ent, .** return 
449a3 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
449a4 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  code..**.** Ther
449a5 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f  e is only ever o
449a6 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ne instance of t
449a7 68 69 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  his SQL statemen
449a8 74 20 63 6f 6d 70 69 6c 65 64 20 66 6f 72 0a 2a  t compiled for.*
449a9 2a 20 65 61 63 68 20 46 54 53 33 20 74 61 62 6c  * each FTS3 tabl
449aa 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 61  e..**.** The sta
449ab 74 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 74  tement returns t
449ac 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6c  he following col
449ad 75 6d 6e 73 20 66 72 6f 6d 20 74 68 65 20 25 5f  umns from the %_
449ae 73 65 67 64 69 72 20 74 61 62 6c 65 3a 0a 2a 2a  segdir table:.**
449af 0a 2a 2a 20 20 20 30 3a 20 69 64 78 0a 2a 2a 20  .**   0: idx.** 
449b0 20 20 31 3a 20 73 74 61 72 74 5f 62 6c 6f 63 6b    1: start_block
449b1 0a 2a 2a 20 20 20 32 3a 20 6c 65 61 76 65 73 5f  .**   2: leaves_
449b2 65 6e 64 5f 62 6c 6f 63 6b 0a 2a 2a 20 20 20 33  end_block.**   3
449b3 3a 20 65 6e 64 5f 62 6c 6f 63 6b 0a 2a 2a 20 20  : end_block.**  
449b4 20 34 3a 20 72 6f 6f 74 0a 2a 2f 0a 53 51 4c 49   4: root.*/.SQLI
449b5 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
449b6 71 6c 69 74 65 33 46 74 73 33 41 6c 6c 53 65 67  qlite3Fts3AllSeg
449b7 64 69 72 73 28 0a 20 20 46 74 73 33 54 61 62 6c  dirs(.  Fts3Tabl
449b8 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  e *p,           
449b9 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53 33 20          /* FTS3 
449ba 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
449bb 4c 61 6e 67 69 64 2c 20 20 20 20 20 20 20 20 20  Langid,         
449bc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
449bd 6e 67 75 61 67 65 20 62 65 69 6e 67 20 71 75 65  nguage being que
449be 72 69 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 49  ried */.  int iI
449bf 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 20 20  ndex,           
449c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
449c1 65 78 20 66 6f 72 20 70 2d 3e 61 49 6e 64 65 78  ex for p->aIndex
449c2 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 76  [] */.  int iLev
449c3 65 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  el,             
449c4 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 76 65 6c          /* Level
449c5 20 74 6f 20 73 65 6c 65 63 74 20 28 72 65 6c 61   to select (rela
449c6 74 69 76 65 20 6c 65 76 65 6c 29 20 2a 2f 0a 20  tive level) */. 
449c7 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
449c8 70 70 53 74 6d 74 20 20 20 20 20 20 20 20 20 20  ppStmt          
449c9 20 2f 2a 20 4f 55 54 3a 20 43 6f 6d 70 69 6c 65   /* OUT: Compile
449ca 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29  d statement */.)
449cb 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
449cc 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
449cd 74 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74  t = 0;..  assert
449ce 28 20 69 4c 65 76 65 6c 3d 3d 46 54 53 33 5f 53  ( iLevel==FTS3_S
449cf 45 47 43 55 52 53 4f 52 5f 41 4c 4c 20 7c 7c 20  EGCURSOR_ALL || 
449d0 69 4c 65 76 65 6c 3e 3d 30 20 29 3b 0a 20 20 61  iLevel>=0 );.  a
449d1 73 73 65 72 74 28 20 69 4c 65 76 65 6c 3c 46 54  ssert( iLevel<FT
449d2 53 33 5f 53 45 47 44 49 52 5f 4d 41 58 4c 45 56  S3_SEGDIR_MAXLEV
449d3 45 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  EL );.  assert( 
449d4 69 49 6e 64 65 78 3e 3d 30 20 26 26 20 69 49 6e  iIndex>=0 && iIn
449d5 64 65 78 3c 70 2d 3e 6e 49 6e 64 65 78 20 29 3b  dex<p->nIndex );
449d6 0a 0a 20 20 69 66 28 20 69 4c 65 76 65 6c 3c 30  ..  if( iLevel<0
449d7 20 29 7b 0a 20 20 20 20 2f 2a 20 22 53 45 4c 45   ){.    /* "SELE
449d8 43 54 20 2a 20 46 52 4f 4d 20 25 5f 73 65 67 64  CT * FROM %_segd
449d9 69 72 20 57 48 45 52 45 20 6c 65 76 65 6c 20 42  ir WHERE level B
449da 45 54 57 45 45 4e 20 3f 20 41 4e 44 20 3f 20 4f  ETWEEN ? AND ? O
449db 52 44 45 52 20 42 59 20 2e 2e 2e 22 20 2a 2f 0a  RDER BY ..." */.
449dc 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c      rc = fts3Sql
449dd 53 74 6d 74 28 70 2c 20 53 51 4c 5f 53 45 4c 45  Stmt(p, SQL_SELE
449de 43 54 5f 4c 45 56 45 4c 5f 52 41 4e 47 45 2c 20  CT_LEVEL_RANGE, 
449df 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20  &pStmt, 0);.    
449e0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
449e1 4b 20 29 7b 20 0a 20 20 20 20 20 20 73 71 6c 69  K ){ .      sqli
449e2 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
449e3 53 74 6d 74 2c 20 31 2c 20 67 65 74 41 62 73 6f  Stmt, 1, getAbso
449e4 6c 75 74 65 4c 65 76 65 6c 28 70 2c 20 69 4c 61  luteLevel(p, iLa
449e5 6e 67 69 64 2c 20 69 49 6e 64 65 78 2c 20 30 29  ngid, iIndex, 0)
449e6 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
449e7 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d  _bind_int64(pStm
449e8 74 2c 20 32 2c 20 0a 20 20 20 20 20 20 20 20 20  t, 2, .         
449e9 20 67 65 74 41 62 73 6f 6c 75 74 65 4c 65 76 65   getAbsoluteLeve
449ea 6c 28 70 2c 20 69 4c 61 6e 67 69 64 2c 20 69 49  l(p, iLangid, iI
449eb 6e 64 65 78 2c 20 46 54 53 33 5f 53 45 47 44 49  ndex, FTS3_SEGDI
449ec 52 5f 4d 41 58 4c 45 56 45 4c 2d 31 29 0a 20 20  R_MAXLEVEL-1).  
449ed 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d      );.    }.  }
449ee 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 22 53 45  else{.    /* "SE
449ef 4c 45 43 54 20 2a 20 46 52 4f 4d 20 25 5f 73 65  LECT * FROM %_se
449f0 67 64 69 72 20 57 48 45 52 45 20 6c 65 76 65 6c  gdir WHERE level
449f1 20 3d 20 3f 20 4f 52 44 45 52 20 42 59 20 2e 2e   = ? ORDER BY ..
449f2 2e 22 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 66  ." */.    rc = f
449f3 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51  ts3SqlStmt(p, SQ
449f4 4c 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 2c 20  L_SELECT_LEVEL, 
449f5 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20  &pStmt, 0);.    
449f6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
449f7 4b 20 29 7b 20 0a 20 20 20 20 20 20 73 71 6c 69  K ){ .      sqli
449f8 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
449f9 53 74 6d 74 2c 20 31 2c 20 67 65 74 41 62 73 6f  Stmt, 1, getAbso
449fa 6c 75 74 65 4c 65 76 65 6c 28 70 2c 20 69 4c 61  luteLevel(p, iLa
449fb 6e 67 69 64 2c 20 69 49 6e 64 65 78 2c 69 4c 65  ngid, iIndex,iLe
449fc 76 65 6c 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  vel));.    }.  }
449fd 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20 70 53 74  .  *ppStmt = pSt
449fe 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  mt;.  return rc;
449ff 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e  .}.../*.** Appen
44a00 64 20 61 20 73 69 6e 67 6c 65 20 76 61 72 69 6e  d a single varin
44a01 74 20 74 6f 20 61 20 50 65 6e 64 69 6e 67 4c 69  t to a PendingLi
44a02 73 74 20 62 75 66 66 65 72 2e 20 53 51 4c 49 54  st buffer. SQLIT
44a03 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
44a04 0a 2a 2a 20 69 66 20 73 75 63 63 65 73 73 66 75  .** if successfu
44a05 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20  l, or an SQLite 
44a06 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68 65 72  error code other
44a07 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  wise..**.** This
44a08 20 66 75 6e 63 74 69 6f 6e 20 61 6c 73 6f 20 73   function also s
44a09 65 72 76 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74  erves to allocat
44a0a 65 20 74 68 65 20 50 65 6e 64 69 6e 67 4c 69 73  e the PendingLis
44a0b 74 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65  t structure itse
44a0c 6c 66 2e 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  lf..** For examp
44a0d 6c 65 2c 20 74 6f 20 63 72 65 61 74 65 20 61 20  le, to create a 
44a0e 6e 65 77 20 50 65 6e 64 69 6e 67 4c 69 73 74 20  new PendingList 
44a0f 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
44a10 6e 69 6e 67 20 74 77 6f 0a 2a 2a 20 76 61 72 69  ning two.** vari
44a11 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 65 6e  nts:.**.**   Pen
44a12 64 69 6e 67 4c 69 73 74 20 2a 70 20 3d 20 30 3b  dingList *p = 0;
44a13 0a 2a 2a 20 20 20 66 74 73 33 50 65 6e 64 69 6e  .**   fts3Pendin
44a14 67 4c 69 73 74 41 70 70 65 6e 64 56 61 72 69 6e  gListAppendVarin
44a15 74 28 26 70 2c 20 31 29 3b 0a 2a 2a 20 20 20 66  t(&p, 1);.**   f
44a16 74 73 33 50 65 6e 64 69 6e 67 4c 69 73 74 41 70  ts3PendingListAp
44a17 70 65 6e 64 56 61 72 69 6e 74 28 26 70 2c 20 32  pendVarint(&p, 2
44a18 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  );.*/.static int
44a19 20 66 74 73 33 50 65 6e 64 69 6e 67 4c 69 73 74   fts3PendingList
44a1a 41 70 70 65 6e 64 56 61 72 69 6e 74 28 0a 20 20  AppendVarint(.  
44a1b 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a 2a 70 70  PendingList **pp
44a1c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
44a1d 2f 2a 20 49 4e 2f 4f 55 54 3a 20 50 6f 69 6e 74  /* IN/OUT: Point
44a1e 65 72 20 74 6f 20 50 65 6e 64 69 6e 67 4c 69 73  er to PendingLis
44a1f 74 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 73 71  t struct */.  sq
44a20 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 20 20 20  lite3_int64 i   
44a21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
44a22 20 56 61 6c 75 65 20 74 6f 20 61 70 70 65 6e 64   Value to append
44a23 20 74 6f 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20   to data */.){. 
44a24 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a 70 20   PendingList *p 
44a25 3d 20 2a 70 70 3b 0a 0a 20 20 2f 2a 20 41 6c 6c  = *pp;..  /* All
44a26 6f 63 61 74 65 20 6f 72 20 67 72 6f 77 20 74 68  ocate or grow th
44a27 65 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 61 73  e PendingList as
44a28 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20   required. */.  
44a29 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 70 20  if( !p ){.    p 
44a2a 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
44a2b 28 73 69 7a 65 6f 66 28 2a 70 29 20 2b 20 31 30  (sizeof(*p) + 10
44a2c 30 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29  0);.    if( !p )
44a2d 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
44a2e 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
44a2f 20 7d 0a 20 20 20 20 70 2d 3e 6e 53 70 61 63 65   }.    p->nSpace
44a30 20 3d 20 31 30 30 3b 0a 20 20 20 20 70 2d 3e 61   = 100;.    p->a
44a31 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 26  Data = (char *)&
44a32 70 5b 31 5d 3b 0a 20 20 20 20 70 2d 3e 6e 44 61  p[1];.    p->nDa
44a33 74 61 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 6c  ta = 0;.  }.  el
44a34 73 65 20 69 66 28 20 70 2d 3e 6e 44 61 74 61 2b  se if( p->nData+
44a35 46 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58 2b  FTS3_VARINT_MAX+
44a36 31 3e 70 2d 3e 6e 53 70 61 63 65 20 29 7b 0a 20  1>p->nSpace ){. 
44a37 20 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 70 2d     int nNew = p-
44a38 3e 6e 53 70 61 63 65 20 2a 20 32 3b 0a 20 20 20  >nSpace * 2;.   
44a39 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61   p = sqlite3_rea
44a3a 6c 6c 6f 63 28 70 2c 20 73 69 7a 65 6f 66 28 2a  lloc(p, sizeof(*
44a3b 70 29 20 2b 20 6e 4e 65 77 29 3b 0a 20 20 20 20  p) + nNew);.    
44a3c 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20  if( !p ){.      
44a3d 73 71 6c 69 74 65 33 5f 66 72 65 65 28 2a 70 70  sqlite3_free(*pp
44a3e 29 3b 0a 20 20 20 20 20 20 2a 70 70 20 3d 20 30  );.      *pp = 0
44a3f 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
44a40 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
44a41 20 7d 0a 20 20 20 20 70 2d 3e 6e 53 70 61 63 65   }.    p->nSpace
44a42 20 3d 20 6e 4e 65 77 3b 0a 20 20 20 20 70 2d 3e   = nNew;.    p->
44a43 61 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29  aData = (char *)
44a44 26 70 5b 31 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  &p[1];.  }..  /*
44a45 20 41 70 70 65 6e 64 20 74 68 65 20 6e 65 77 20   Append the new 
44a46 73 65 72 69 61 6c 69 7a 65 64 20 76 61 72 69 6e  serialized varin
44a47 74 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  t to the end of 
44a48 74 68 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 70  the list. */.  p
44a49 2d 3e 6e 44 61 74 61 20 2b 3d 20 73 71 6c 69 74  ->nData += sqlit
44a4a 65 33 46 74 73 33 50 75 74 56 61 72 69 6e 74 28  e3Fts3PutVarint(
44a4b 26 70 2d 3e 61 44 61 74 61 5b 70 2d 3e 6e 44 61  &p->aData[p->nDa
44a4c 74 61 5d 2c 20 69 29 3b 0a 20 20 70 2d 3e 61 44  ta], i);.  p->aD
44a4d 61 74 61 5b 70 2d 3e 6e 44 61 74 61 5d 20 3d 20  ata[p->nData] = 
44a4e 27 5c 30 27 3b 0a 20 20 2a 70 70 20 3d 20 70 3b  '\0';.  *pp = p;
44a4f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
44a50 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  _OK;.}../*.** Ad
44a51 64 20 61 20 64 6f 63 69 64 2f 63 6f 6c 75 6d 6e  d a docid/column
44a52 2f 70 6f 73 69 74 69 6f 6e 20 65 6e 74 72 79 20  /position entry 
44a53 74 6f 20 61 20 50 65 6e 64 69 6e 67 4c 69 73 74  to a PendingList
44a54 20 73 74 72 75 63 74 75 72 65 2e 20 4e 6f 6e 2d   structure. Non-
44a55 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 74 75 72  zero.** is retur
44a56 6e 65 64 20 69 66 20 74 68 65 20 73 74 72 75 63  ned if the struc
44a57 74 75 72 65 20 69 73 20 73 71 6c 69 74 65 33 5f  ture is sqlite3_
44a58 72 65 61 6c 6c 6f 63 65 64 20 61 73 20 70 61 72  realloced as par
44a59 74 20 6f 66 20 61 64 64 69 6e 67 0a 2a 2a 20 74  t of adding.** t
44a5a 68 65 20 65 6e 74 72 79 2e 20 4f 74 68 65 72 77  he entry. Otherw
44a5b 69 73 65 2c 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a  ise, zero..**.**
44a5c 20 49 66 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72   If an OOM error
44a5d 20 6f 63 63 75 72 73 2c 20 2a 70 52 63 20 69 73   occurs, *pRc is
44a5e 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e   set to SQLITE_N
44a5f 4f 4d 45 4d 20 62 65 66 6f 72 65 20 72 65 74 75  OMEM before retu
44a60 72 6e 69 6e 67 2e 0a 2a 2a 20 5a 65 72 6f 20 69  rning..** Zero i
44a61 73 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 65  s always returne
44a62 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20  d in this case. 
44a63 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 6e 6f  Otherwise, if no
44a64 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72   OOM error occur
44a65 73 2c 0a 2a 2a 20 69 74 20 69 73 20 73 65 74 20  s,.** it is set 
44a66 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f  to SQLITE_OK..*/
44a67 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
44a68 50 65 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e  PendingListAppen
44a69 64 28 0a 20 20 50 65 6e 64 69 6e 67 4c 69 73 74  d(.  PendingList
44a6a 20 2a 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20   **pp,          
44a6b 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
44a6c 50 65 6e 64 69 6e 67 4c 69 73 74 20 73 74 72 75  PendingList stru
44a6d 63 74 75 72 65 20 2a 2f 0a 20 20 73 71 6c 69 74  cture */.  sqlit
44a6e 65 33 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c  e3_int64 iDocid,
44a6f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f             /* Do
44a70 63 69 64 20 66 6f 72 20 65 6e 74 72 79 20 74 6f  cid for entry to
44a71 20 61 64 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65   add */.  sqlite
44a72 33 5f 69 6e 74 36 34 20 69 43 6f 6c 2c 20 20 20  3_int64 iCol,   
44a73 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
44a74 75 6d 6e 20 66 6f 72 20 65 6e 74 72 79 20 74 6f  umn for entry to
44a75 20 61 64 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65   add */.  sqlite
44a76 33 5f 69 6e 74 36 34 20 69 50 6f 73 2c 20 20 20  3_int64 iPos,   
44a77 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73            /* Pos
44a78 69 74 69 6f 6e 20 6f 66 20 74 65 72 6d 20 66 6f  ition of term fo
44a79 72 20 65 6e 74 72 79 20 74 6f 20 61 64 64 20 2a  r entry to add *
44a7a 2f 0a 20 20 69 6e 74 20 2a 70 52 63 20 20 20 20  /.  int *pRc    
44a7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44a7c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75      /* OUT: Retu
44a7d 72 6e 20 63 6f 64 65 20 2a 2f 0a 29 7b 0a 20 20  rn code */.){.  
44a7e 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a 70 20 3d  PendingList *p =
44a7f 20 2a 70 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d   *pp;.  int rc =
44a80 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61   SQLITE_OK;..  a
44a81 73 73 65 72 74 28 20 21 70 20 7c 7c 20 70 2d 3e  ssert( !p || p->
44a82 69 4c 61 73 74 44 6f 63 69 64 3c 3d 69 44 6f 63  iLastDocid<=iDoc
44a83 69 64 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 20  id );..  if( !p 
44a84 7c 7c 20 70 2d 3e 69 4c 61 73 74 44 6f 63 69 64  || p->iLastDocid
44a85 21 3d 69 44 6f 63 69 64 20 29 7b 0a 20 20 20 20  !=iDocid ){.    
44a86 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 44  sqlite3_int64 iD
44a87 65 6c 74 61 20 3d 20 69 44 6f 63 69 64 20 2d 20  elta = iDocid - 
44a88 28 70 20 3f 20 70 2d 3e 69 4c 61 73 74 44 6f 63  (p ? p->iLastDoc
44a89 69 64 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28  id : 0);.    if(
44a8a 20 70 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65   p ){.      asse
44a8b 72 74 28 20 70 2d 3e 6e 44 61 74 61 3c 70 2d 3e  rt( p->nData<p->
44a8c 6e 53 70 61 63 65 20 29 3b 0a 20 20 20 20 20 20  nSpace );.      
44a8d 61 73 73 65 72 74 28 20 70 2d 3e 61 44 61 74 61  assert( p->aData
44a8e 5b 70 2d 3e 6e 44 61 74 61 5d 3d 3d 30 20 29 3b  [p->nData]==0 );
44a8f 0a 20 20 20 20 20 20 70 2d 3e 6e 44 61 74 61 2b  .      p->nData+
44a90 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  +;.    }.    if(
44a91 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
44a92 3d 20 66 74 73 33 50 65 6e 64 69 6e 67 4c 69 73  = fts3PendingLis
44a93 74 41 70 70 65 6e 64 56 61 72 69 6e 74 28 26 70  tAppendVarint(&p
44a94 2c 20 69 44 65 6c 74 61 29 29 20 29 7b 0a 20 20  , iDelta)) ){.  
44a95 20 20 20 20 67 6f 74 6f 20 70 65 6e 64 69 6e 67      goto pending
44a96 6c 69 73 74 61 70 70 65 6e 64 5f 6f 75 74 3b 0a  listappend_out;.
44a97 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 4c 61      }.    p->iLa
44a98 73 74 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20  stCol = -1;.    
44a99 70 2d 3e 69 4c 61 73 74 50 6f 73 20 3d 20 30 3b  p->iLastPos = 0;
44a9a 0a 20 20 20 20 70 2d 3e 69 4c 61 73 74 44 6f 63  .    p->iLastDoc
44a9b 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a 20 20 7d  id = iDocid;.  }
44a9c 0a 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 26 26  .  if( iCol>0 &&
44a9d 20 70 2d 3e 69 4c 61 73 74 43 6f 6c 21 3d 69 43   p->iLastCol!=iC
44a9e 6f 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 53 51  ol ){.    if( SQ
44a9f 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 66  LITE_OK!=(rc = f
44aa0 74 73 33 50 65 6e 64 69 6e 67 4c 69 73 74 41 70  ts3PendingListAp
44aa1 70 65 6e 64 56 61 72 69 6e 74 28 26 70 2c 20 31  pendVarint(&p, 1
44aa2 29 29 0a 20 20 20 20 20 7c 7c 20 53 51 4c 49 54  )).     || SQLIT
44aa3 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 66 74 73 33  E_OK!=(rc = fts3
44aa4 50 65 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e  PendingListAppen
44aa5 64 56 61 72 69 6e 74 28 26 70 2c 20 69 43 6f 6c  dVarint(&p, iCol
44aa6 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  )).    ){.      
44aa7 67 6f 74 6f 20 70 65 6e 64 69 6e 67 6c 69 73 74  goto pendinglist
44aa8 61 70 70 65 6e 64 5f 6f 75 74 3b 0a 20 20 20 20  append_out;.    
44aa9 7d 0a 20 20 20 20 70 2d 3e 69 4c 61 73 74 43 6f  }.    p->iLastCo
44aaa 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70 2d  l = iCol;.    p-
44aab 3e 69 4c 61 73 74 50 6f 73 20 3d 20 30 3b 0a 20  >iLastPos = 0;. 
44aac 20 7d 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30   }.  if( iCol>=0
44aad 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
44aae 69 50 6f 73 3e 70 2d 3e 69 4c 61 73 74 50 6f 73  iPos>p->iLastPos
44aaf 20 7c 7c 20 28 69 50 6f 73 3d 3d 30 20 26 26 20   || (iPos==0 && 
44ab0 70 2d 3e 69 4c 61 73 74 50 6f 73 3d 3d 30 29 20  p->iLastPos==0) 
44ab1 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33  );.    rc = fts3
44ab2 50 65 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e  PendingListAppen
44ab3 64 56 61 72 69 6e 74 28 26 70 2c 20 32 2b 69 50  dVarint(&p, 2+iP
44ab4 6f 73 2d 70 2d 3e 69 4c 61 73 74 50 6f 73 29 3b  os-p->iLastPos);
44ab5 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
44ab6 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
44ab7 70 2d 3e 69 4c 61 73 74 50 6f 73 20 3d 20 69 50  p->iLastPos = iP
44ab8 6f 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  os;.    }.  }.. 
44ab9 70 65 6e 64 69 6e 67 6c 69 73 74 61 70 70 65 6e  pendinglistappen
44aba 64 5f 6f 75 74 3a 0a 20 20 2a 70 52 63 20 3d 20  d_out:.  *pRc = 
44abb 72 63 3b 0a 20 20 69 66 28 20 70 21 3d 2a 70 70  rc;.  if( p!=*pp
44abc 20 29 7b 0a 20 20 20 20 2a 70 70 20 3d 20 70 3b   ){.    *pp = p;
44abd 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
44abe 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
44abf 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 50  ../*.** Free a P
44ac0 65 6e 64 69 6e 67 4c 69 73 74 20 6f 62 6a 65 63  endingList objec
44ac1 74 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 66  t allocated by f
44ac2 74 73 33 50 65 6e 64 69 6e 67 4c 69 73 74 41 70  ts3PendingListAp
44ac3 70 65 6e 64 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  pend()..*/.stati
44ac4 63 20 76 6f 69 64 20 66 74 73 33 50 65 6e 64 69  c void fts3Pendi
44ac5 6e 67 4c 69 73 74 44 65 6c 65 74 65 28 50 65 6e  ngListDelete(Pen
44ac6 64 69 6e 67 4c 69 73 74 20 2a 70 4c 69 73 74 29  dingList *pList)
44ac7 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  {.  sqlite3_free
44ac8 28 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (pList);.}../*.*
44ac9 2a 20 41 64 64 20 61 6e 20 65 6e 74 72 79 20 74  * Add an entry t
44aca 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 70 65 6e  o one of the pen
44acb 64 69 6e 67 2d 74 65 72 6d 73 20 68 61 73 68 20  ding-terms hash 
44acc 74 61 62 6c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  tables..*/.stati
44acd 63 20 69 6e 74 20 66 74 73 33 50 65 6e 64 69 6e  c int fts3Pendin
44ace 67 54 65 72 6d 73 41 64 64 4f 6e 65 28 0a 20 20  gTermsAddOne(.  
44acf 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 0a 20 20  Fts3Table *p,.  
44ad0 69 6e 74 20 69 43 6f 6c 2c 0a 20 20 69 6e 74 20  int iCol,.  int 
44ad1 69 50 6f 73 2c 0a 20 20 46 74 73 33 48 61 73 68  iPos,.  Fts3Hash
44ad2 20 2a 70 48 61 73 68 2c 20 20 20 20 20 20 20 20   *pHash,        
44ad3 20 20 20 20 20 20 20 20 2f 2a 20 50 65 6e 64 69          /* Pendi
44ad4 6e 67 20 74 65 72 6d 73 20 68 61 73 68 20 74 61  ng terms hash ta
44ad5 62 6c 65 20 74 6f 20 61 64 64 20 65 6e 74 72 79  ble to add entry
44ad6 20 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63   to */.  const c
44ad7 68 61 72 20 2a 7a 54 6f 6b 65 6e 2c 0a 20 20 69  har *zToken,.  i
44ad8 6e 74 20 6e 54 6f 6b 65 6e 0a 29 7b 0a 20 20 50  nt nToken.){.  P
44ad9 65 6e 64 69 6e 67 4c 69 73 74 20 2a 70 4c 69 73  endingList *pLis
44ada 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  t;.  int rc = SQ
44adb 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 70 4c 69 73  LITE_OK;..  pLis
44adc 74 20 3d 20 28 50 65 6e 64 69 6e 67 4c 69 73 74  t = (PendingList
44add 20 2a 29 66 74 73 33 48 61 73 68 46 69 6e 64 28   *)fts3HashFind(
44ade 70 48 61 73 68 2c 20 7a 54 6f 6b 65 6e 2c 20 6e  pHash, zToken, n
44adf 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 70 4c  Token);.  if( pL
44ae0 69 73 74 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 50  ist ){.    p->nP
44ae1 65 6e 64 69 6e 67 44 61 74 61 20 2d 3d 20 28 70  endingData -= (p
44ae2 4c 69 73 74 2d 3e 6e 44 61 74 61 20 2b 20 6e 54  List->nData + nT
44ae3 6f 6b 65 6e 20 2b 20 73 69 7a 65 6f 66 28 46 74  oken + sizeof(Ft
44ae4 73 33 48 61 73 68 45 6c 65 6d 29 29 3b 0a 20 20  s3HashElem));.  
44ae5 7d 0a 20 20 69 66 28 20 66 74 73 33 50 65 6e 64  }.  if( fts3Pend
44ae6 69 6e 67 4c 69 73 74 41 70 70 65 6e 64 28 26 70  ingListAppend(&p
44ae7 4c 69 73 74 2c 20 70 2d 3e 69 50 72 65 76 44 6f  List, p->iPrevDo
44ae8 63 69 64 2c 20 69 43 6f 6c 2c 20 69 50 6f 73 2c  cid, iCol, iPos,
44ae9 20 26 72 63 29 20 29 7b 0a 20 20 20 20 69 66 28   &rc) ){.    if(
44aea 20 70 4c 69 73 74 3d 3d 66 74 73 33 48 61 73 68   pList==fts3Hash
44aeb 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a 54  Insert(pHash, zT
44aec 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 2c 20 70 4c  oken, nToken, pL
44aed 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a  ist) ){.      /*
44aee 20 4d 61 6c 6c 6f 63 20 66 61 69 6c 65 64 20 77   Malloc failed w
44aef 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20 74  hile inserting t
44af0 68 65 20 6e 65 77 20 65 6e 74 72 79 2e 20 54 68  he new entry. Th
44af1 69 73 20 63 61 6e 20 6f 6e 6c 79 20 0a 20 20 20  is can only .   
44af2 20 20 20 2a 2a 20 68 61 70 70 65 6e 20 69 66 20     ** happen if 
44af3 74 68 65 72 65 20 77 61 73 20 6e 6f 20 70 72 65  there was no pre
44af4 76 69 6f 75 73 20 65 6e 74 72 79 20 66 6f 72 20  vious entry for 
44af5 74 68 69 73 20 74 6f 6b 65 6e 2e 0a 20 20 20 20  this token..    
44af6 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72    */.      asser
44af7 74 28 20 30 3d 3d 66 74 73 33 48 61 73 68 46 69  t( 0==fts3HashFi
44af8 6e 64 28 70 48 61 73 68 2c 20 7a 54 6f 6b 65 6e  nd(pHash, zToken
44af9 2c 20 6e 54 6f 6b 65 6e 29 20 29 3b 0a 20 20 20  , nToken) );.   
44afa 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
44afb 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 72 63  pList);.      rc
44afc 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
44afd 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
44afe 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
44aff 7b 0a 20 20 20 20 70 2d 3e 6e 50 65 6e 64 69 6e  {.    p->nPendin
44b00 67 44 61 74 61 20 2b 3d 20 28 70 4c 69 73 74 2d  gData += (pList-
44b01 3e 6e 44 61 74 61 20 2b 20 6e 54 6f 6b 65 6e 20  >nData + nToken 
44b02 2b 20 73 69 7a 65 6f 66 28 46 74 73 33 48 61 73  + sizeof(Fts3Has
44b03 68 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a 20 20 72  hElem));.  }.  r
44b04 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
44b05 2a 2a 20 54 6f 6b 65 6e 69 7a 65 20 74 68 65 20  ** Tokenize the 
44b06 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  nul-terminated s
44b07 74 72 69 6e 67 20 7a 54 65 78 74 20 61 6e 64 20  tring zText and 
44b08 61 64 64 20 61 6c 6c 20 74 6f 6b 65 6e 73 20 74  add all tokens t
44b09 6f 20 74 68 65 0a 2a 2a 20 70 65 6e 64 69 6e 67  o the.** pending
44b0a 2d 74 65 72 6d 73 20 68 61 73 68 2d 74 61 62 6c  -terms hash-tabl
44b0b 65 2e 20 54 68 65 20 64 6f 63 69 64 20 75 73 65  e. The docid use
44b0c 64 20 69 73 20 74 68 61 74 20 63 75 72 72 65 6e  d is that curren
44b0d 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a  tly stored in.**
44b0e 20 70 2d 3e 69 50 72 65 76 44 6f 63 69 64 2c 20   p->iPrevDocid, 
44b0f 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  and the column i
44b10 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 61  s specified by a
44b11 72 67 75 6d 65 6e 74 20 69 43 6f 6c 2e 0a 2a 2a  rgument iCol..**
44b12 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
44b13 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  l, SQLITE_OK is 
44b14 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
44b15 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65  ise, an SQLite e
44b16 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74  rror code..*/.st
44b17 61 74 69 63 20 69 6e 74 20 66 74 73 33 50 65 6e  atic int fts3Pen
44b18 64 69 6e 67 54 65 72 6d 73 41 64 64 28 0a 20 20  dingTermsAdd(.  
44b19 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20  Fts3Table *p,   
44b1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44b1b 2f 2a 20 54 61 62 6c 65 20 69 6e 74 6f 20 77 68  /* Table into wh
44b1c 69 63 68 20 74 65 78 74 20 77 69 6c 6c 20 62 65  ich text will be
44b1d 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 69   inserted */.  i
44b1e 6e 74 20 69 4c 61 6e 67 69 64 2c 20 20 20 20 20  nt iLangid,     
44b1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
44b20 2a 20 4c 61 6e 67 75 61 67 65 20 69 64 20 74 6f  * Language id to
44b21 20 75 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20   use */.  const 
44b22 63 68 61 72 20 2a 7a 54 65 78 74 2c 20 20 20 20  char *zText,    
44b23 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
44b24 74 20 6f 66 20 64 6f 63 75 6d 65 6e 74 20 74 6f  t of document to
44b25 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
44b26 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20    int iCol,     
44b27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44b28 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 69 6e 74 6f    /* Column into
44b29 20 77 68 69 63 68 20 74 65 78 74 20 69 73 20 62   which text is b
44b2a 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 2a 2f  eing inserted */
44b2b 0a 20 20 75 33 32 20 2a 70 6e 57 6f 72 64 20 20  .  u32 *pnWord  
44b2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44b2d 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 49 6e     /* IN/OUT: In
44b2e 63 72 2e 20 62 79 20 6e 75 6d 62 65 72 20 74 6f  cr. by number to
44b2f 6b 65 6e 73 20 69 6e 73 65 72 74 65 64 20 2a 2f  kens inserted */
44b30 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
44b31 69 6e 74 20 69 53 74 61 72 74 20 3d 20 30 3b 0a  int iStart = 0;.
44b32 20 20 69 6e 74 20 69 45 6e 64 20 3d 20 30 3b 0a    int iEnd = 0;.
44b33 20 20 69 6e 74 20 69 50 6f 73 20 3d 20 30 3b 0a    int iPos = 0;.
44b34 20 20 69 6e 74 20 6e 57 6f 72 64 20 3d 20 30 3b    int nWord = 0;
44b35 0a 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ..  char const *
44b36 7a 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54  zToken;.  int nT
44b37 6f 6b 65 6e 20 3d 20 30 3b 0a 0a 20 20 73 71 6c  oken = 0;..  sql
44b38 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
44b39 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 2d 3e  pTokenizer = p->
44b3a 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71  pTokenizer;.  sq
44b3b 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
44b3c 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 20 2a 70 4d  module const *pM
44b3d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a  odule = pTokeniz
44b3e 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 73  er->pModule;.  s
44b3f 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
44b40 5f 63 75 72 73 6f 72 20 2a 70 43 73 72 3b 0a 20  _cursor *pCsr;. 
44b41 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 73 71   int (*xNext)(sq
44b42 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
44b43 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
44b44 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
44b45 72 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e  r**,int*,int*,in
44b46 74 2a 2c 69 6e 74 2a 29 3b 0a 0a 20 20 61 73 73  t*,int*);..  ass
44b47 65 72 74 28 20 70 54 6f 6b 65 6e 69 7a 65 72 20  ert( pTokenizer 
44b48 26 26 20 70 4d 6f 64 75 6c 65 20 29 3b 0a 0a 20  && pModule );.. 
44b49 20 2f 2a 20 49 66 20 74 68 65 20 75 73 65 72 20   /* If the user 
44b4a 68 61 73 20 69 6e 73 65 72 74 65 64 20 61 20 4e  has inserted a N
44b4b 55 4c 4c 20 76 61 6c 75 65 2c 20 74 68 69 73 20  ULL value, this 
44b4c 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20  function may be 
44b4d 63 61 6c 6c 65 64 20 77 69 74 68 0a 20 20 2a 2a  called with.  **
44b4e 20 7a 54 65 78 74 3d 3d 30 2e 20 49 6e 20 74 68   zText==0. In th
44b4f 69 73 20 63 61 73 65 2c 20 61 64 64 20 7a 65 72  is case, add zer
44b50 6f 20 74 6f 6b 65 6e 20 65 6e 74 72 69 65 73 20  o token entries 
44b51 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  to the hash tabl
44b52 65 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65 74 75  e and .  ** retu
44b53 72 6e 20 65 61 72 6c 79 2e 20 2a 2f 0a 20 20 69  rn early. */.  i
44b54 66 28 20 7a 54 65 78 74 3d 3d 30 20 29 7b 0a 20  f( zText==0 ){. 
44b55 20 20 20 2a 70 6e 57 6f 72 64 20 3d 20 30 3b 0a     *pnWord = 0;.
44b56 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
44b57 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  E_OK;.  }..  rc 
44b58 3d 20 73 71 6c 69 74 65 33 46 74 73 33 4f 70 65  = sqlite3Fts3Ope
44b59 6e 54 6f 6b 65 6e 69 7a 65 72 28 70 54 6f 6b 65  nTokenizer(pToke
44b5a 6e 69 7a 65 72 2c 20 69 4c 61 6e 67 69 64 2c 20  nizer, iLangid, 
44b5b 7a 54 65 78 74 2c 20 2d 31 2c 20 26 70 43 73 72  zText, -1, &pCsr
44b5c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
44b5d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
44b5e 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
44b5f 78 4e 65 78 74 20 3d 20 70 4d 6f 64 75 6c 65 2d  xNext = pModule-
44b60 3e 78 4e 65 78 74 3b 0a 20 20 77 68 69 6c 65 28  >xNext;.  while(
44b61 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 0a 20   SQLITE_OK==rc. 
44b62 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f       && SQLITE_O
44b63 4b 3d 3d 28 72 63 20 3d 20 78 4e 65 78 74 28 70  K==(rc = xNext(p
44b64 43 73 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e  Csr, &zToken, &n
44b65 54 6f 6b 65 6e 2c 20 26 69 53 74 61 72 74 2c 20  Token, &iStart, 
44b66 26 69 45 6e 64 2c 20 26 69 50 6f 73 29 29 0a 20  &iEnd, &iPos)). 
44b67 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
44b68 20 20 20 69 66 28 20 69 50 6f 73 3e 3d 6e 57 6f     if( iPos>=nWo
44b69 72 64 20 29 20 6e 57 6f 72 64 20 3d 20 69 50 6f  rd ) nWord = iPo
44b6a 73 2b 31 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 73  s+1;..    /* Pos
44b6b 69 74 69 6f 6e 73 20 63 61 6e 6e 6f 74 20 62 65  itions cannot be
44b6c 20 6e 65 67 61 74 69 76 65 3b 20 77 65 20 75 73   negative; we us
44b6d 65 20 2d 31 20 61 73 20 61 20 74 65 72 6d 69 6e  e -1 as a termin
44b6e 61 74 6f 72 20 69 6e 74 65 72 6e 61 6c 6c 79 2e  ator internally.
44b6f 0a 20 20 20 20 2a 2a 20 54 6f 6b 65 6e 73 20 6d  .    ** Tokens m
44b70 75 73 74 20 68 61 76 65 20 61 20 6e 6f 6e 2d 7a  ust have a non-z
44b71 65 72 6f 20 6c 65 6e 67 74 68 2e 0a 20 20 20 20  ero length..    
44b72 2a 2f 0a 20 20 20 20 69 66 28 20 69 50 6f 73 3c  */.    if( iPos<
44b73 30 20 7c 7c 20 21 7a 54 6f 6b 65 6e 20 7c 7c 20  0 || !zToken || 
44b74 6e 54 6f 6b 65 6e 3c 3d 30 20 29 7b 0a 20 20 20  nToken<=0 ){.   
44b75 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
44b76 52 52 4f 52 3b 0a 20 20 20 20 20 20 62 72 65 61  RROR;.      brea
44b77 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
44b78 20 41 64 64 20 74 68 65 20 74 65 72 6d 20 74 6f   Add the term to
44b79 20 74 68 65 20 74 65 72 6d 73 20 69 6e 64 65 78   the terms index
44b7a 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 66 74 73   */.    rc = fts
44b7b 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 41 64 64  3PendingTermsAdd
44b7c 4f 6e 65 28 0a 20 20 20 20 20 20 20 20 70 2c 20  One(.        p, 
44b7d 69 43 6f 6c 2c 20 69 50 6f 73 2c 20 26 70 2d 3e  iCol, iPos, &p->
44b7e 61 49 6e 64 65 78 5b 30 5d 2e 68 50 65 6e 64 69  aIndex[0].hPendi
44b7f 6e 67 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b  ng, zToken, nTok
44b80 65 6e 0a 20 20 20 20 29 3b 0a 20 20 20 20 0a 20  en.    );.    . 
44b81 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 74 65     /* Add the te
44b82 72 6d 20 74 6f 20 65 61 63 68 20 6f 66 20 74 68  rm to each of th
44b83 65 20 70 72 65 66 69 78 20 69 6e 64 65 78 65 73  e prefix indexes
44b84 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20   that it is not 
44b85 74 6f 6f 20 0a 20 20 20 20 2a 2a 20 73 68 6f 72  too .    ** shor
44b86 74 20 66 6f 72 2e 20 2a 2f 0a 20 20 20 20 66 6f  t for. */.    fo
44b87 72 28 69 3d 31 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=1; rc==SQLIT
44b88 45 5f 4f 4b 20 26 26 20 69 3c 70 2d 3e 6e 49 6e  E_OK && i<p->nIn
44b89 64 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  dex; i++){.     
44b8a 20 73 74 72 75 63 74 20 46 74 73 33 49 6e 64 65   struct Fts3Inde
44b8b 78 20 2a 70 49 6e 64 65 78 20 3d 20 26 70 2d 3e  x *pIndex = &p->
44b8c 61 49 6e 64 65 78 5b 69 5d 3b 0a 20 20 20 20 20  aIndex[i];.     
44b8d 20 69 66 28 20 6e 54 6f 6b 65 6e 3c 70 49 6e 64   if( nToken<pInd
44b8e 65 78 2d 3e 6e 50 72 65 66 69 78 20 29 20 63 6f  ex->nPrefix ) co
44b8f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 72 63  ntinue;.      rc
44b90 20 3d 20 66 74 73 33 50 65 6e 64 69 6e 67 54 65   = fts3PendingTe
44b91 72 6d 73 41 64 64 4f 6e 65 28 0a 20 20 20 20 20  rmsAddOne(.     
44b92 20 20 20 20 20 70 2c 20 69 43 6f 6c 2c 20 69 50       p, iCol, iP
44b93 6f 73 2c 20 26 70 49 6e 64 65 78 2d 3e 68 50 65  os, &pIndex->hPe
44b94 6e 64 69 6e 67 2c 20 7a 54 6f 6b 65 6e 2c 20 70  nding, zToken, p
44b95 49 6e 64 65 78 2d 3e 6e 50 72 65 66 69 78 0a 20  Index->nPrefix. 
44b96 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20       );.    }.  
44b97 7d 0a 0a 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43  }..  pModule->xC
44b98 6c 6f 73 65 28 70 43 73 72 29 3b 0a 20 20 2a 70  lose(pCsr);.  *p
44b99 6e 57 6f 72 64 20 2b 3d 20 6e 57 6f 72 64 3b 0a  nWord += nWord;.
44b9a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51    return (rc==SQ
44b9b 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49  LITE_DONE ? SQLI
44b9c 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 7d 0a 0a  TE_OK : rc);.}..
44b9d 2f 2a 20 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 74  /* .** Calling t
44b9e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64  his function ind
44b9f 69 63 61 74 65 73 20 74 68 61 74 20 73 75 62 73  icates that subs
44ba0 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20  equent calls to 
44ba1 0a 2a 2a 20 66 74 73 33 50 65 6e 64 69 6e 67 54  .** fts3PendingT
44ba2 65 72 6d 73 41 64 64 28 29 20 61 72 65 20 74 6f  ermsAdd() are to
44ba3 20 61 64 64 20 74 65 72 6d 2f 70 6f 73 69 74 69   add term/positi
44ba4 6f 6e 2d 6c 69 73 74 20 70 61 69 72 73 20 66 6f  on-list pairs fo
44ba5 72 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74  r the.** content
44ba6 73 20 6f 66 20 74 68 65 20 64 6f 63 75 6d 65 6e  s of the documen
44ba7 74 20 77 69 74 68 20 64 6f 63 69 64 20 69 44 6f  t with docid iDo
44ba8 63 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  cid..*/.static i
44ba9 6e 74 20 66 74 73 33 50 65 6e 64 69 6e 67 54 65  nt fts3PendingTe
44baa 72 6d 73 44 6f 63 69 64 28 0a 20 20 46 74 73 33  rmsDocid(.  Fts3
44bab 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20  Table *p,       
44bac 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
44bad 75 6c 6c 2d 74 65 78 74 20 74 61 62 6c 65 20 68  ull-text table h
44bae 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  andle */.  int i
44baf 4c 61 6e 67 69 64 2c 20 20 20 20 20 20 20 20 20  Langid,         
44bb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
44bb1 6e 67 75 61 67 65 20 69 64 20 6f 66 20 72 6f 77  nguage id of row
44bb2 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20 2a   being written *
44bb3 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  /.  sqlite_int64
44bb4 20 69 44 6f 63 69 64 20 20 20 20 20 20 20 20 20   iDocid         
44bb5 20 20 20 20 2f 2a 20 44 6f 63 69 64 20 6f 66 20      /* Docid of 
44bb6 72 6f 77 20 62 65 69 6e 67 20 77 72 69 74 74 65  row being writte
44bb7 6e 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74  n */.){.  assert
44bb8 28 20 69 4c 61 6e 67 69 64 3e 3d 30 20 29 3b 0a  ( iLangid>=0 );.
44bb9 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  .  /* TODO(shess
44bba 29 20 45 78 70 6c 6f 72 65 20 77 68 65 74 68 65  ) Explore whethe
44bbb 72 20 70 61 72 74 69 61 6c 6c 79 20 66 6c 75 73  r partially flus
44bbc 68 69 6e 67 20 74 68 65 20 62 75 66 66 65 72 20  hing the buffer 
44bbd 6f 6e 0a 20 20 2a 2a 20 66 6f 72 63 65 64 2d 66  on.  ** forced-f
44bbe 6c 75 73 68 20 77 6f 75 6c 64 20 70 72 6f 76 69  lush would provi
44bbf 64 65 20 62 65 74 74 65 72 20 70 65 72 66 6f 72  de better perfor
44bc0 6d 61 6e 63 65 2e 20 20 49 20 73 75 73 70 65 63  mance.  I suspec
44bc1 74 20 74 68 61 74 20 69 66 0a 20 20 2a 2a 20 77  t that if.  ** w
44bc2 65 20 6f 72 64 65 72 65 64 20 74 68 65 20 64 6f  e ordered the do
44bc3 63 6c 69 73 74 73 20 62 79 20 73 69 7a 65 20 61  clists by size a
44bc4 6e 64 20 66 6c 75 73 68 65 64 20 74 68 65 20 6c  nd flushed the l
44bc5 61 72 67 65 73 74 20 75 6e 74 69 6c 20 74 68 65  argest until the
44bc6 0a 20 20 2a 2a 20 62 75 66 66 65 72 20 77 61 73  .  ** buffer was
44bc7 20 68 61 6c 66 20 65 6d 70 74 79 2c 20 74 68 61   half empty, tha
44bc8 74 20 77 6f 75 6c 64 20 6c 65 74 20 74 68 65 20  t would let the 
44bc9 6c 65 73 73 20 66 72 65 71 75 65 6e 74 20 74 65  less frequent te
44bca 72 6d 73 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74  rms.  ** generat
44bcb 65 20 6c 6f 6e 67 65 72 20 64 6f 63 6c 69 73 74  e longer doclist
44bcc 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 44  s..  */.  if( iD
44bcd 6f 63 69 64 3c 3d 70 2d 3e 69 50 72 65 76 44 6f  ocid<=p->iPrevDo
44bce 63 69 64 20 0a 20 20 20 7c 7c 20 70 2d 3e 69 50  cid .   || p->iP
44bcf 72 65 76 4c 61 6e 67 69 64 21 3d 69 4c 61 6e 67  revLangid!=iLang
44bd0 69 64 0a 20 20 20 7c 7c 20 70 2d 3e 6e 50 65 6e  id.   || p->nPen
44bd1 64 69 6e 67 44 61 74 61 3e 70 2d 3e 6e 4d 61 78  dingData>p->nMax
44bd2 50 65 6e 64 69 6e 67 44 61 74 61 20 0a 20 20 29  PendingData .  )
44bd3 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73  {.    int rc = s
44bd4 71 6c 69 74 65 33 46 74 73 33 50 65 6e 64 69 6e  qlite3Fts3Pendin
44bd5 67 54 65 72 6d 73 46 6c 75 73 68 28 70 29 3b 0a  gTermsFlush(p);.
44bd6 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
44bd7 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
44bd8 63 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 50 72 65  c;.  }.  p->iPre
44bd9 76 44 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b  vDocid = iDocid;
44bda 0a 20 20 70 2d 3e 69 50 72 65 76 4c 61 6e 67 69  .  p->iPrevLangi
44bdb 64 20 3d 20 69 4c 61 6e 67 69 64 3b 0a 20 20 72  d = iLangid;.  r
44bdc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
44bdd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 63 61 72  .}../*.** Discar
44bde 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  d the contents o
44bdf 66 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 74 65  f the pending-te
44be0 72 6d 73 20 68 61 73 68 20 74 61 62 6c 65 73 2e  rms hash tables.
44be1 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
44be2 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
44be3 46 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73  Fts3PendingTerms
44be4 43 6c 65 61 72 28 46 74 73 33 54 61 62 6c 65 20  Clear(Fts3Table 
44be5 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  *p){.  int i;.  
44be6 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 49  for(i=0; i<p->nI
44be7 6e 64 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ndex; i++){.    
44be8 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 45  Fts3HashElem *pE
44be9 6c 65 6d 3b 0a 20 20 20 20 46 74 73 33 48 61 73  lem;.    Fts3Has
44bea 68 20 2a 70 48 61 73 68 20 3d 20 26 70 2d 3e 61  h *pHash = &p->a
44beb 49 6e 64 65 78 5b 69 5d 2e 68 50 65 6e 64 69 6e  Index[i].hPendin
44bec 67 3b 0a 20 20 20 20 66 6f 72 28 70 45 6c 65 6d  g;.    for(pElem
44bed 3d 66 74 73 33 48 61 73 68 46 69 72 73 74 28 70  =fts3HashFirst(p
44bee 48 61 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70 45  Hash); pElem; pE
44bef 6c 65 6d 3d 66 74 73 33 48 61 73 68 4e 65 78 74  lem=fts3HashNext
44bf0 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 20 20  (pElem)){.      
44bf1 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a 70 4c 69  PendingList *pLi
44bf2 73 74 20 3d 20 28 50 65 6e 64 69 6e 67 4c 69 73  st = (PendingLis
44bf3 74 20 2a 29 66 74 73 33 48 61 73 68 44 61 74 61  t *)fts3HashData
44bf4 28 70 45 6c 65 6d 29 3b 0a 20 20 20 20 20 20 66  (pElem);.      f
44bf5 74 73 33 50 65 6e 64 69 6e 67 4c 69 73 74 44 65  ts3PendingListDe
44bf6 6c 65 74 65 28 70 4c 69 73 74 29 3b 0a 20 20 20  lete(pList);.   
44bf7 20 7d 0a 20 20 20 20 66 74 73 33 48 61 73 68 43   }.    fts3HashC
44bf8 6c 65 61 72 28 70 48 61 73 68 29 3b 0a 20 20 7d  lear(pHash);.  }
44bf9 0a 20 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44 61  .  p->nPendingDa
44bfa 74 61 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ta = 0;.}../*.**
44bfb 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
44bfc 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
44bfd 78 55 70 64 61 74 65 28 29 20 6d 65 74 68 6f 64  xUpdate() method
44bfe 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20 49   as part of an I
44bff 4e 53 45 52 54 0a 2a 2a 20 6f 70 65 72 61 74 69  NSERT.** operati
44c00 6f 6e 2e 20 49 74 20 61 64 64 73 20 65 6e 74 72  on. It adds entr
44c01 69 65 73 20 66 6f 72 20 65 61 63 68 20 74 65 72  ies for each ter
44c02 6d 20 69 6e 20 74 68 65 20 6e 65 77 20 72 65 63  m in the new rec
44c03 6f 72 64 20 74 6f 20 74 68 65 0a 2a 2a 20 70 65  ord to the.** pe
44c04 6e 64 69 6e 67 54 65 72 6d 73 20 68 61 73 68 20  ndingTerms hash 
44c05 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 72 67  table..**.** Arg
44c06 75 6d 65 6e 74 20 61 70 56 61 6c 20 69 73 20 74  ument apVal is t
44c07 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 73  he same as the s
44c08 69 6d 69 6c 61 72 6c 79 20 6e 61 6d 65 64 20 61  imilarly named a
44c09 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74  rgument passed t
44c0a 6f 0a 2a 2a 20 66 74 73 33 49 6e 73 65 72 74 44  o.** fts3InsertD
44c0b 61 74 61 28 29 2e 20 50 61 72 61 6d 65 74 65 72  ata(). Parameter
44c0c 20 69 44 6f 63 69 64 20 69 73 20 74 68 65 20 64   iDocid is the d
44c0d 6f 63 69 64 20 6f 66 20 74 68 65 20 6e 65 77 20  ocid of the new 
44c0e 72 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  row..*/.static i
44c0f 6e 74 20 66 74 73 33 49 6e 73 65 72 74 54 65 72  nt fts3InsertTer
44c10 6d 73 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20  ms(.  Fts3Table 
44c11 2a 70 2c 20 0a 20 20 69 6e 74 20 69 4c 61 6e 67  *p, .  int iLang
44c12 69 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76  id, .  sqlite3_v
44c13 61 6c 75 65 20 2a 2a 61 70 56 61 6c 2c 20 0a 20  alue **apVal, . 
44c14 20 75 33 32 20 2a 61 53 7a 0a 29 7b 0a 20 20 69   u32 *aSz.){.  i
44c15 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
44c16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
44c17 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
44c18 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 32  ble */.  for(i=2
44c19 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32  ; i<p->nColumn+2
44c1a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73  ; i++){.    cons
44c1b 74 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20  t char *zText = 
44c1c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
44c1d 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
44c1e 28 61 70 56 61 6c 5b 69 5d 29 3b 0a 20 20 20 20  (apVal[i]);.    
44c1f 69 6e 74 20 72 63 20 3d 20 66 74 73 33 50 65 6e  int rc = fts3Pen
44c20 64 69 6e 67 54 65 72 6d 73 41 64 64 28 70 2c 20  dingTermsAdd(p, 
44c21 69 4c 61 6e 67 69 64 2c 20 7a 54 65 78 74 2c 20  iLangid, zText, 
44c22 69 2d 32 2c 20 26 61 53 7a 5b 69 2d 32 5d 29 3b  i-2, &aSz[i-2]);
44c23 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
44c24 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
44c25 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
44c26 0a 20 20 20 20 61 53 7a 5b 70 2d 3e 6e 43 6f 6c  .    aSz[p->nCol
44c27 75 6d 6e 5d 20 2b 3d 20 73 71 6c 69 74 65 33 5f  umn] += sqlite3_
44c28 76 61 6c 75 65 5f 62 79 74 65 73 28 61 70 56 61  value_bytes(apVa
44c29 6c 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 74  l[i]);.  }.  ret
44c2a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
44c2b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
44c2c 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
44c2d 62 79 20 74 68 65 20 78 55 70 64 61 74 65 28 29  by the xUpdate()
44c2e 20 6d 65 74 68 6f 64 20 66 6f 72 20 61 6e 20 49   method for an I
44c2f 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f 6e 2e  NSERT operation.
44c30 0a 2a 2a 20 54 68 65 20 61 70 56 61 6c 20 70 61  .** The apVal pa
44c31 72 61 6d 65 74 65 72 20 69 73 20 70 61 73 73 65  rameter is passe
44c32 64 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  d a copy of the 
44c33 61 70 56 61 6c 20 61 72 67 75 6d 65 6e 74 20 70  apVal argument p
44c34 61 73 73 65 64 20 62 79 0a 2a 2a 20 53 51 4c 69  assed by.** SQLi
44c35 74 65 20 74 6f 20 74 68 65 20 78 55 70 64 61 74  te to the xUpdat
44c36 65 28 29 20 6d 65 74 68 6f 64 2e 20 69 2e 65 3a  e() method. i.e:
44c37 0a 2a 2a 0a 2a 2a 20 20 20 61 70 56 61 6c 5b 30  .**.**   apVal[0
44c38 5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ]               
44c39 20 4e 6f 74 20 75 73 65 64 20 66 6f 72 20 49 4e   Not used for IN
44c3a 53 45 52 54 2e 0a 2a 2a 20 20 20 61 70 56 61 6c  SERT..**   apVal
44c3b 5b 31 5d 20 20 20 20 20 20 20 20 20 20 20 20 20  [1]             
44c3c 20 20 20 72 6f 77 69 64 0a 2a 2a 20 20 20 61 70     rowid.**   ap
44c3d 56 61 6c 5b 32 5d 20 20 20 20 20 20 20 20 20 20  Val[2]          
44c3e 20 20 20 20 20 20 4c 65 66 74 2d 6d 6f 73 74 20        Left-most 
44c3f 75 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c  user-defined col
44c40 75 6d 6e 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20  umn.**   ....** 
44c41 20 20 61 70 56 61 6c 5b 70 2d 3e 6e 43 6f 6c 75    apVal[p->nColu
44c42 6d 6e 2b 31 5d 20 20 20 20 20 52 69 67 68 74 2d  mn+1]     Right-
44c43 6d 6f 73 74 20 75 73 65 72 2d 64 65 66 69 6e 65  most user-define
44c44 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 61 70  d column.**   ap
44c45 56 61 6c 5b 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32  Val[p->nColumn+2
44c46 5d 20 20 20 20 20 48 69 64 64 65 6e 20 63 6f 6c  ]     Hidden col
44c47 75 6d 6e 20 77 69 74 68 20 73 61 6d 65 20 6e 61  umn with same na
44c48 6d 65 20 61 73 20 74 61 62 6c 65 0a 2a 2a 20 20  me as table.**  
44c49 20 61 70 56 61 6c 5b 70 2d 3e 6e 43 6f 6c 75 6d   apVal[p->nColum
44c4a 6e 2b 33 5d 20 20 20 20 20 48 69 64 64 65 6e 20  n+3]     Hidden 
44c4b 22 64 6f 63 69 64 22 20 63 6f 6c 75 6d 6e 20 28  "docid" column (
44c4c 61 6c 69 61 73 20 66 6f 72 20 72 6f 77 69 64 29  alias for rowid)
44c4d 0a 2a 2a 20 20 20 61 70 56 61 6c 5b 70 2d 3e 6e  .**   apVal[p->n
44c4e 43 6f 6c 75 6d 6e 2b 34 5d 20 20 20 20 20 48 69  Column+4]     Hi
44c4f 64 64 65 6e 20 6c 61 6e 67 75 61 67 65 69 64 20  dden languageid 
44c50 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 61 74 69 63  column.*/.static
44c51 20 69 6e 74 20 66 74 73 33 49 6e 73 65 72 74 44   int fts3InsertD
44c52 61 74 61 28 0a 20 20 46 74 73 33 54 61 62 6c 65  ata(.  Fts3Table
44c53 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
44c54 20 20 20 20 20 20 20 2f 2a 20 46 75 6c 6c 2d 74         /* Full-t
44c55 65 78 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73  ext table */.  s
44c56 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
44c57 70 56 61 6c 2c 20 20 20 20 20 20 20 20 20 20 2f  pVal,          /
44c58 2a 20 41 72 72 61 79 20 6f 66 20 76 61 6c 75 65  * Array of value
44c59 73 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 20  s to insert */. 
44c5a 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a   sqlite3_int64 *
44c5b 70 69 44 6f 63 69 64 20 20 20 20 20 20 20 20 20  piDocid         
44c5c 20 2f 2a 20 4f 55 54 3a 20 44 6f 63 69 64 20 66   /* OUT: Docid f
44c5d 6f 72 20 72 6f 77 20 6a 75 73 74 20 69 6e 73 65  or row just inse
44c5e 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  rted */.){.  int
44c5f 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
44c60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
44c61 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
44c62 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
44c63 43 6f 6e 74 65 6e 74 49 6e 73 65 72 74 3b 20 20  ContentInsert;  
44c64 20 2f 2a 20 49 4e 53 45 52 54 20 49 4e 54 4f 20   /* INSERT INTO 
44c65 25 5f 63 6f 6e 74 65 6e 74 20 56 41 4c 55 45 53  %_content VALUES
44c66 28 2e 2e 2e 29 20 2a 2f 0a 0a 20 20 69 66 28 20  (...) */..  if( 
44c67 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c 20 29  p->zContentTbl )
44c68 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61  {.    sqlite3_va
44c69 6c 75 65 20 2a 70 52 6f 77 69 64 20 3d 20 61 70  lue *pRowid = ap
44c6a 56 61 6c 5b 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 33  Val[p->nColumn+3
44c6b 5d 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ];.    if( sqlit
44c6c 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 52  e3_value_type(pR
44c6d 6f 77 69 64 29 3d 3d 53 51 4c 49 54 45 5f 4e 55  owid)==SQLITE_NU
44c6e 4c 4c 20 29 7b 0a 20 20 20 20 20 20 70 52 6f 77  LL ){.      pRow
44c6f 69 64 20 3d 20 61 70 56 61 6c 5b 31 5d 3b 0a 20  id = apVal[1];. 
44c70 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
44c71 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
44c72 70 52 6f 77 69 64 29 21 3d 53 51 4c 49 54 45 5f  pRowid)!=SQLITE_
44c73 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20  INTEGER ){.     
44c74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
44c75 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 7d  ONSTRAINT;.    }
44c76 0a 20 20 20 20 2a 70 69 44 6f 63 69 64 20 3d 20  .    *piDocid = 
44c77 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
44c78 74 36 34 28 70 52 6f 77 69 64 29 3b 0a 20 20 20  t64(pRowid);.   
44c79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
44c7a 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63  K;.  }..  /* Loc
44c7b 61 74 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ate the statemen
44c7c 74 20 68 61 6e 64 6c 65 20 75 73 65 64 20 74 6f  t handle used to
44c7d 20 69 6e 73 65 72 74 20 64 61 74 61 20 69 6e 74   insert data int
44c7e 6f 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 0a  o the %_content.
44c7f 20 20 2a 2a 20 74 61 62 6c 65 2e 20 54 68 65 20    ** table. The 
44c80 53 51 4c 20 66 6f 72 20 74 68 69 73 20 73 74 61  SQL for this sta
44c81 74 65 6d 65 6e 74 20 69 73 3a 0a 20 20 2a 2a 0a  tement is:.  **.
44c82 20 20 2a 2a 20 20 20 49 4e 53 45 52 54 20 49 4e    **   INSERT IN
44c83 54 4f 20 25 5f 63 6f 6e 74 65 6e 74 20 56 41 4c  TO %_content VAL
44c84 55 45 53 28 3f 2c 20 3f 2c 20 3f 2c 20 2e 2e 2e  UES(?, ?, ?, ...
44c85 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ).  **.  ** The 
44c86 73 74 61 74 65 6d 65 6e 74 20 66 65 61 74 75 72  statement featur
44c87 65 73 20 4e 20 27 3f 27 20 76 61 72 69 61 62 6c  es N '?' variabl
44c88 65 73 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74  es, where N is t
44c89 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 65  he number of use
44c8a 72 0a 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 63  r.  ** defined c
44c8b 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 46 54  olumns in the FT
44c8c 53 33 20 74 61 62 6c 65 2c 20 70 6c 75 73 20 6f  S3 table, plus o
44c8d 6e 65 20 66 6f 72 20 74 68 65 20 64 6f 63 69 64  ne for the docid
44c8e 20 66 69 65 6c 64 2e 0a 20 20 2a 2f 0a 20 20 72   field..  */.  r
44c8f 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28  c = fts3SqlStmt(
44c90 70 2c 20 53 51 4c 5f 43 4f 4e 54 45 4e 54 5f 49  p, SQL_CONTENT_I
44c91 4e 53 45 52 54 2c 20 26 70 43 6f 6e 74 65 6e 74  NSERT, &pContent
44c92 49 6e 73 65 72 74 2c 20 26 61 70 56 61 6c 5b 31  Insert, &apVal[1
44c93 5d 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  ]);.  if( rc==SQ
44c94 4c 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 7a 4c  LITE_OK && p->zL
44c95 61 6e 67 75 61 67 65 69 64 20 29 7b 0a 20 20 20  anguageid ){.   
44c96 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
44c97 6e 64 5f 69 6e 74 28 0a 20 20 20 20 20 20 20 20  nd_int(.        
44c98 70 43 6f 6e 74 65 6e 74 49 6e 73 65 72 74 2c 20  pContentInsert, 
44c99 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 2c 20 0a 20  p->nColumn+2, . 
44c9a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76         sqlite3_v
44c9b 61 6c 75 65 5f 69 6e 74 28 61 70 56 61 6c 5b 70  alue_int(apVal[p
44c9c 2d 3e 6e 43 6f 6c 75 6d 6e 2b 34 5d 29 0a 20 20  ->nColumn+4]).  
44c9d 20 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72    );.  }.  if( r
44c9e 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
44c9f 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
44ca0 54 68 65 72 65 20 69 73 20 61 20 71 75 69 72 6b  There is a quirk
44ca1 20 68 65 72 65 2e 20 54 68 65 20 75 73 65 72 73   here. The users
44ca2 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e   INSERT statemen
44ca3 74 20 6d 61 79 20 68 61 76 65 20 73 70 65 63 69  t may have speci
44ca4 66 69 65 64 0a 20 20 2a 2a 20 61 20 76 61 6c 75  fied.  ** a valu
44ca5 65 20 66 6f 72 20 74 68 65 20 22 72 6f 77 69 64  e for the "rowid
44ca6 22 20 66 69 65 6c 64 2c 20 66 6f 72 20 74 68 65  " field, for the
44ca7 20 22 64 6f 63 69 64 22 20 66 69 65 6c 64 2c 20   "docid" field, 
44ca8 6f 72 20 66 6f 72 20 62 6f 74 68 2e 0a 20 20 2a  or for both..  *
44ca9 2a 20 57 68 69 63 68 20 69 73 20 61 20 70 72 6f  * Which is a pro
44caa 62 6c 65 6d 2c 20 73 69 6e 63 65 20 22 72 6f 77  blem, since "row
44cab 69 64 22 20 61 6e 64 20 22 64 6f 63 69 64 22 20  id" and "docid" 
44cac 61 72 65 20 61 6c 69 61 73 65 73 20 66 6f 72 20  are aliases for 
44cad 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20 76 61  the.  ** same va
44cae 6c 75 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  lue. For example
44caf 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 49 4e  :.  **.  **   IN
44cb0 53 45 52 54 20 49 4e 54 4f 20 66 74 73 33 74 62  SERT INTO fts3tb
44cb1 6c 28 72 6f 77 69 64 2c 20 64 6f 63 69 64 29 20  l(rowid, docid) 
44cb2 56 41 4c 55 45 53 28 31 2c 20 32 29 3b 0a 20 20  VALUES(1, 2);.  
44cb3 2a 2a 0a 20 20 2a 2a 20 49 6e 20 46 54 53 33 2c  **.  ** In FTS3,
44cb4 20 74 68 69 73 20 69 73 20 61 6e 20 65 72 72 6f   this is an erro
44cb5 72 2e 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f  r. It is an erro
44cb6 72 20 74 6f 20 73 70 65 63 69 66 79 20 6e 6f 6e  r to specify non
44cb7 2d 4e 55 4c 4c 20 76 61 6c 75 65 73 0a 20 20 2a  -NULL values.  *
44cb8 2a 20 66 6f 72 20 62 6f 74 68 20 64 6f 63 69 64  * for both docid
44cb9 20 61 6e 64 20 73 6f 6d 65 20 6f 74 68 65 72 20   and some other 
44cba 72 6f 77 69 64 20 61 6c 69 61 73 2e 0a 20 20 2a  rowid alias..  *
44cbb 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e  /.  if( SQLITE_N
44cbc 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c  ULL!=sqlite3_val
44cbd 75 65 5f 74 79 70 65 28 61 70 56 61 6c 5b 33 2b  ue_type(apVal[3+
44cbe 70 2d 3e 6e 43 6f 6c 75 6d 6e 5d 29 20 29 7b 0a  p->nColumn]) ){.
44cbf 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e      if( SQLITE_N
44cc0 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c  ULL==sqlite3_val
44cc1 75 65 5f 74 79 70 65 28 61 70 56 61 6c 5b 30 5d  ue_type(apVal[0]
44cc2 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54 45  ).     && SQLITE
44cc3 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76  _NULL!=sqlite3_v
44cc4 61 6c 75 65 5f 74 79 70 65 28 61 70 56 61 6c 5b  alue_type(apVal[
44cc5 31 5d 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  1]).    ){.     
44cc6 20 2f 2a 20 41 20 72 6f 77 69 64 2f 64 6f 63 69   /* A rowid/doci
44cc7 64 20 63 6f 6e 66 6c 69 63 74 2e 20 2a 2f 0a 20  d conflict. */. 
44cc8 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
44cc9 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a  TE_ERROR;.    }.
44cca 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
44ccb 5f 62 69 6e 64 5f 76 61 6c 75 65 28 70 43 6f 6e  _bind_value(pCon
44ccc 74 65 6e 74 49 6e 73 65 72 74 2c 20 31 2c 20 61  tentInsert, 1, a
44ccd 70 56 61 6c 5b 33 2b 70 2d 3e 6e 43 6f 6c 75 6d  pVal[3+p->nColum
44cce 6e 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  n]);.    if( rc!
44ccf 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
44cd0 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
44cd1 2a 20 45 78 65 63 75 74 65 20 74 68 65 20 73 74  * Execute the st
44cd2 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e 73 65 72  atement to inser
44cd3 74 20 74 68 65 20 72 65 63 6f 72 64 2e 20 53 65  t the record. Se
44cd4 74 20 2a 70 69 44 6f 63 69 64 20 74 6f 20 74 68  t *piDocid to th
44cd5 65 20 0a 20 20 2a 2a 20 6e 65 77 20 64 6f 63 69  e .  ** new doci
44cd6 64 20 76 61 6c 75 65 2e 20 0a 20 20 2a 2f 0a 20  d value. .  */. 
44cd7 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 43   sqlite3_step(pC
44cd8 6f 6e 74 65 6e 74 49 6e 73 65 72 74 29 3b 0a 20  ontentInsert);. 
44cd9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65   rc = sqlite3_re
44cda 73 65 74 28 70 43 6f 6e 74 65 6e 74 49 6e 73 65  set(pContentInse
44cdb 72 74 29 3b 0a 0a 20 20 2a 70 69 44 6f 63 69 64  rt);..  *piDocid
44cdc 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f   = sqlite3_last_
44cdd 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 70 2d 3e  insert_rowid(p->
44cde 64 62 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  db);.  return rc
44cdf 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d  ;.}..../*.** Rem
44ce0 6f 76 65 20 61 6c 6c 20 64 61 74 61 20 66 72 6f  ove all data fro
44ce1 6d 20 74 68 65 20 46 54 53 33 20 74 61 62 6c 65  m the FTS3 table
44ce2 2e 20 43 6c 65 61 72 20 74 68 65 20 68 61 73 68  . Clear the hash
44ce3 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e   table containin
44ce4 67 0a 2a 2a 20 70 65 6e 64 69 6e 67 20 74 65 72  g.** pending ter
44ce5 6d 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ms..*/.static in
44ce6 74 20 66 74 73 33 44 65 6c 65 74 65 41 6c 6c 28  t fts3DeleteAll(
44ce7 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 69 6e  Fts3Table *p, in
44ce8 74 20 62 43 6f 6e 74 65 6e 74 29 7b 0a 20 20 69  t bContent){.  i
44ce9 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
44cea 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  K;             /
44ceb 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
44cec 0a 0a 20 20 2f 2a 20 44 69 73 63 61 72 64 20 74  ..  /* Discard t
44ced 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
44cee 68 65 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73  he pending-terms
44cef 20 68 61 73 68 20 74 61 62 6c 65 2e 20 2a 2f 0a   hash table. */.
44cf0 20 20 73 71 6c 69 74 65 33 46 74 73 33 50 65 6e    sqlite3Fts3Pen
44cf1 64 69 6e 67 54 65 72 6d 73 43 6c 65 61 72 28 70  dingTermsClear(p
44cf2 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20  );..  /* Delete 
44cf3 65 76 65 72 79 74 68 69 6e 67 20 66 72 6f 6d 20  everything from 
44cf4 74 68 65 20 73 68 61 64 6f 77 20 74 61 62 6c 65  the shadow table
44cf5 73 2e 20 45 78 63 65 70 74 2c 20 6c 65 61 76 65  s. Except, leave
44cf6 20 25 5f 63 6f 6e 74 65 6e 74 20 61 73 0a 20 20   %_content as.  
44cf7 2a 2a 20 69 73 20 69 66 20 62 43 6f 6e 74 65 6e  ** is if bConten
44cf8 74 20 69 73 20 66 61 6c 73 65 2e 20 20 2a 2f 0a  t is false.  */.
44cf9 20 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 43 6f    assert( p->zCo
44cfa 6e 74 65 6e 74 54 62 6c 3d 3d 30 20 7c 7c 20 62  ntentTbl==0 || b
44cfb 43 6f 6e 74 65 6e 74 3d 3d 30 20 29 3b 0a 20 20  Content==0 );.  
44cfc 69 66 28 20 62 43 6f 6e 74 65 6e 74 20 29 20 66  if( bContent ) f
44cfd 74 73 33 53 71 6c 45 78 65 63 28 26 72 63 2c 20  ts3SqlExec(&rc, 
44cfe 70 2c 20 53 51 4c 5f 44 45 4c 45 54 45 5f 41 4c  p, SQL_DELETE_AL
44cff 4c 5f 43 4f 4e 54 45 4e 54 2c 20 30 29 3b 0a 20  L_CONTENT, 0);. 
44d00 20 66 74 73 33 53 71 6c 45 78 65 63 28 26 72 63   fts3SqlExec(&rc
44d01 2c 20 70 2c 20 53 51 4c 5f 44 45 4c 45 54 45 5f  , p, SQL_DELETE_
44d02 41 4c 4c 5f 53 45 47 4d 45 4e 54 53 2c 20 30 29  ALL_SEGMENTS, 0)
44d03 3b 0a 20 20 66 74 73 33 53 71 6c 45 78 65 63 28  ;.  fts3SqlExec(
44d04 26 72 63 2c 20 70 2c 20 53 51 4c 5f 44 45 4c 45  &rc, p, SQL_DELE
44d05 54 45 5f 41 4c 4c 5f 53 45 47 44 49 52 2c 20 30  TE_ALL_SEGDIR, 0
44d06 29 3b 0a 20 20 69 66 28 20 70 2d 3e 62 48 61 73  );.  if( p->bHas
44d07 44 6f 63 73 69 7a 65 20 29 7b 0a 20 20 20 20 66  Docsize ){.    f
44d08 74 73 33 53 71 6c 45 78 65 63 28 26 72 63 2c 20  ts3SqlExec(&rc, 
44d09 70 2c 20 53 51 4c 5f 44 45 4c 45 54 45 5f 41 4c  p, SQL_DELETE_AL
44d0a 4c 5f 44 4f 43 53 49 5a 45 2c 20 30 29 3b 0a 20  L_DOCSIZE, 0);. 
44d0b 20 7d 0a 20 20 69 66 28 20 70 2d 3e 62 48 61 73   }.  if( p->bHas
44d0c 53 74 61 74 20 29 7b 0a 20 20 20 20 66 74 73 33  Stat ){.    fts3
44d0d 53 71 6c 45 78 65 63 28 26 72 63 2c 20 70 2c 20  SqlExec(&rc, p, 
44d0e 53 51 4c 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53  SQL_DELETE_ALL_S
44d0f 54 41 54 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  TAT, 0);.  }.  r
44d10 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
44d11 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  **.*/.static int
44d12 20 6c 61 6e 67 69 64 46 72 6f 6d 53 65 6c 65 63   langidFromSelec
44d13 74 28 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20  t(Fts3Table *p, 
44d14 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
44d15 65 6c 65 63 74 29 7b 0a 20 20 69 6e 74 20 69 4c  elect){.  int iL
44d16 61 6e 67 69 64 20 3d 20 30 3b 0a 20 20 69 66 28  angid = 0;.  if(
44d17 20 70 2d 3e 7a 4c 61 6e 67 75 61 67 65 69 64 20   p->zLanguageid 
44d18 29 20 69 4c 61 6e 67 69 64 20 3d 20 73 71 6c 69  ) iLangid = sqli
44d19 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70  te3_column_int(p
44d1a 53 65 6c 65 63 74 2c 20 70 2d 3e 6e 43 6f 6c 75  Select, p->nColu
44d1b 6d 6e 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e 20  mn+1);.  return 
44d1c 69 4c 61 6e 67 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a  iLangid;.}../*.*
44d1d 2a 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d  * The first elem
44d1e 65 6e 74 20 69 6e 20 74 68 65 20 61 70 56 61 6c  ent in the apVal
44d1f 5b 5d 20 61 72 72 61 79 20 69 73 20 61 73 73 75  [] array is assu
44d20 6d 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74  med to contain t
44d21 68 65 20 64 6f 63 69 64 0a 2a 2a 20 28 61 6e 20  he docid.** (an 
44d22 69 6e 74 65 67 65 72 29 20 6f 66 20 61 20 72 6f  integer) of a ro
44d23 77 20 61 62 6f 75 74 20 74 6f 20 62 65 20 64 65  w about to be de
44d24 6c 65 74 65 64 2e 20 52 65 6d 6f 76 65 20 61 6c  leted. Remove al
44d25 6c 20 74 65 72 6d 73 20 66 72 6f 6d 20 74 68 65  l terms from the
44d26 0a 2a 2a 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e  .** full-text in
44d27 64 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  dex..*/.static v
44d28 6f 69 64 20 66 74 73 33 44 65 6c 65 74 65 54 65  oid fts3DeleteTe
44d29 72 6d 73 28 20 0a 20 20 69 6e 74 20 2a 70 52 43  rms( .  int *pRC
44d2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
44d2b 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 2a  /* Result code *
44d2c 2f 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  /.  Fts3Table *p
44d2d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
44d2e 68 65 20 46 54 53 20 74 61 62 6c 65 20 74 6f 20  he FTS table to 
44d2f 64 65 6c 65 74 65 20 66 72 6f 6d 20 2a 2f 0a 20  delete from */. 
44d30 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
44d31 70 52 6f 77 69 64 2c 20 20 2f 2a 20 54 68 65 20  pRowid,  /* The 
44d32 64 6f 63 69 64 20 74 6f 20 62 65 20 64 65 6c 65  docid to be dele
44d33 74 65 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 53  ted */.  u32 *aS
44d34 7a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  z,              
44d35 20 2f 2a 20 53 69 7a 65 73 20 6f 66 20 64 65 6c   /* Sizes of del
44d36 65 74 65 64 20 64 6f 63 75 6d 65 6e 74 20 77 72  eted document wr
44d37 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20  itten here */.  
44d38 69 6e 74 20 2a 70 62 46 6f 75 6e 64 20 20 20 20  int *pbFound    
44d39 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
44d3a 53 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 72  Set to true if r
44d3b 6f 77 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 65  ow really does e
44d3c 78 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  xist */.){.  int
44d3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73   rc;.  sqlite3_s
44d3e 74 6d 74 20 2a 70 53 65 6c 65 63 74 3b 0a 0a 20  tmt *pSelect;.. 
44d3f 20 61 73 73 65 72 74 28 20 2a 70 62 46 6f 75 6e   assert( *pbFoun
44d40 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 2a 70  d==0 );.  if( *p
44d41 52 43 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 72  RC ) return;.  r
44d42 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28  c = fts3SqlStmt(
44d43 70 2c 20 53 51 4c 5f 53 45 4c 45 43 54 5f 43 4f  p, SQL_SELECT_CO
44d44 4e 54 45 4e 54 5f 42 59 5f 52 4f 57 49 44 2c 20  NTENT_BY_ROWID, 
44d45 26 70 53 65 6c 65 63 74 2c 20 26 70 52 6f 77 69  &pSelect, &pRowi
44d46 64 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  d);.  if( rc==SQ
44d47 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
44d48 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73  f( SQLITE_ROW==s
44d49 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 65 6c  qlite3_step(pSel
44d4a 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 69 6e  ect) ){.      in
44d4b 74 20 69 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  t i;.      int i
44d4c 4c 61 6e 67 69 64 20 3d 20 6c 61 6e 67 69 64 46  Langid = langidF
44d4d 72 6f 6d 53 65 6c 65 63 74 28 70 2c 20 70 53 65  romSelect(p, pSe
44d4e 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 72 63 20  lect);.      rc 
44d4f 3d 20 66 74 73 33 50 65 6e 64 69 6e 67 54 65 72  = fts3PendingTer
44d50 6d 73 44 6f 63 69 64 28 70 2c 20 69 4c 61 6e 67  msDocid(p, iLang
44d51 69 64 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id, sqlite3_colu
44d52 6d 6e 5f 69 6e 74 36 34 28 70 53 65 6c 65 63 74  mn_int64(pSelect
44d53 2c 20 30 29 29 3b 0a 20 20 20 20 20 20 66 6f 72  , 0));.      for
44d54 28 69 3d 31 3b 20 72 63 3d 3d 53 51 4c 49 54 45  (i=1; rc==SQLITE
44d55 5f 4f 4b 20 26 26 20 69 3c 3d 70 2d 3e 6e 43 6f  _OK && i<=p->nCo
44d56 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  lumn; i++){.    
44d57 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
44d58 7a 54 65 78 74 20 3d 20 28 63 6f 6e 73 74 20 63  zText = (const c
44d59 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f  har *)sqlite3_co
44d5a 6c 75 6d 6e 5f 74 65 78 74 28 70 53 65 6c 65 63  lumn_text(pSelec
44d5b 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 20 20 72  t, i);.        r
44d5c 63 20 3d 20 66 74 73 33 50 65 6e 64 69 6e 67 54  c = fts3PendingT
44d5d 65 72 6d 73 41 64 64 28 70 2c 20 69 4c 61 6e 67  ermsAdd(p, iLang
44d5e 69 64 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20 26  id, zText, -1, &
44d5f 61 53 7a 5b 69 2d 31 5d 29 3b 0a 20 20 20 20 20  aSz[i-1]);.     
44d60 20 20 20 61 53 7a 5b 70 2d 3e 6e 43 6f 6c 75 6d     aSz[p->nColum
44d61 6e 5d 20 2b 3d 20 73 71 6c 69 74 65 33 5f 63 6f  n] += sqlite3_co
44d62 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53 65 6c 65  lumn_bytes(pSele
44d63 63 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 7d 0a  ct, i);.      }.
44d64 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
44d65 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
44d66 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74     sqlite3_reset
44d67 28 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20  (pSelect);.     
44d68 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20     *pRC = rc;.  
44d69 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
44d6a 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 62 46      }.      *pbF
44d6b 6f 75 6e 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  ound = 1;.    }.
44d6c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
44d6d 5f 72 65 73 65 74 28 70 53 65 6c 65 63 74 29 3b  _reset(pSelect);
44d6e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
44d6f 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 65 6c  lite3_reset(pSel
44d70 65 63 74 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 43  ect);.  }.  *pRC
44d71 20 3d 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   = rc;.}../*.** 
44d72 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74  Forward declarat
44d73 69 6f 6e 20 74 6f 20 61 63 63 6f 75 6e 74 20 66  ion to account f
44d74 6f 72 20 74 68 65 20 63 69 72 63 75 6c 61 72 20  or the circular 
44d75 64 65 70 65 6e 64 65 6e 63 79 20 62 65 74 77 65  dependency betwe
44d76 65 6e 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20  en.** functions 
44d77 66 74 73 33 53 65 67 6d 65 6e 74 4d 65 72 67 65  fts3SegmentMerge
44d78 28 29 20 61 6e 64 20 66 74 73 33 41 6c 6c 6f 63  () and fts3Alloc
44d79 61 74 65 53 65 67 64 69 72 49 64 78 28 29 2e 0a  ateSegdirIdx()..
44d7a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
44d7b 73 33 53 65 67 6d 65 6e 74 4d 65 72 67 65 28 46  s3SegmentMerge(F
44d7c 74 73 33 54 61 62 6c 65 20 2a 2c 20 69 6e 74 2c  ts3Table *, int,
44d7d 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 20   int, int);../* 
44d7e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
44d7f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65  n allocates a ne
44d80 77 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c 20 69  w level iLevel i
44d81 6e 64 65 78 20 69 6e 20 74 68 65 20 73 65 67 64  ndex in the segd
44d82 69 72 20 74 61 62 6c 65 2e 0a 2a 2a 20 55 73 75  ir table..** Usu
44d83 61 6c 6c 79 2c 20 69 6e 64 65 78 65 73 20 61 72  ally, indexes ar
44d84 65 20 61 6c 6c 6f 63 61 74 65 64 20 77 69 74 68  e allocated with
44d85 69 6e 20 61 20 6c 65 76 65 6c 20 73 65 71 75 65  in a level seque
44d86 6e 74 69 61 6c 6c 79 20 73 74 61 72 74 69 6e 67  ntially starting
44d87 0a 2a 2a 20 77 69 74 68 20 30 2c 20 73 6f 20 74  .** with 0, so t
44d88 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 64  he allocated ind
44d89 65 78 20 69 73 20 6f 6e 65 20 67 72 65 61 74 65  ex is one greate
44d8a 72 20 74 68 61 6e 20 74 68 65 20 76 61 6c 75 65  r than the value
44d8b 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 3a   returned.** by:
44d8c 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  .**.**   SELECT 
44d8d 6d 61 78 28 69 64 78 29 20 46 52 4f 4d 20 25 5f  max(idx) FROM %_
44d8e 73 65 67 64 69 72 20 57 48 45 52 45 20 6c 65 76  segdir WHERE lev
44d8f 65 6c 20 3d 20 3a 69 4c 65 76 65 6c 0a 2a 2a 0a  el = :iLevel.**.
44d90 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74  ** However, if t
44d91 68 65 72 65 20 61 72 65 20 61 6c 72 65 61 64 79  here are already
44d92 20 46 54 53 33 5f 4d 45 52 47 45 5f 43 4f 55 4e   FTS3_MERGE_COUN
44d93 54 20 69 6e 64 65 78 65 73 20 61 74 20 74 68 65  T indexes at the
44d94 20 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 6c 65   requested.** le
44d95 76 65 6c 2c 20 74 68 65 79 20 61 72 65 20 6d 65  vel, they are me
44d96 72 67 65 64 20 69 6e 74 6f 20 61 20 73 69 6e 67  rged into a sing
44d97 6c 65 20 6c 65 76 65 6c 20 28 69 4c 65 76 65 6c  le level (iLevel
44d98 2b 31 29 20 73 65 67 6d 65 6e 74 20 61 6e 64 20  +1) segment and 
44d99 74 68 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  the .** allocate
44d9a 64 20 69 6e 64 65 78 20 69 73 20 30 2e 0a 2a 2a  d index is 0..**
44d9b 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
44d9c 6c 2c 20 2a 70 69 49 64 78 20 69 73 20 73 65 74  l, *piIdx is set
44d9d 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65   to the allocate
44d9e 64 20 69 6e 64 65 78 20 73 6c 6f 74 20 61 6e 64  d index slot and
44d9f 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 72 65   SQLITE_OK.** re
44da0 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  turned. Otherwis
44da1 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  e, an SQLite err
44da2 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
44da3 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
44da4 6e 74 20 66 74 73 33 41 6c 6c 6f 63 61 74 65 53  nt fts3AllocateS
44da5 65 67 64 69 72 49 64 78 28 0a 20 20 46 74 73 33  egdirIdx(.  Fts3
44da6 54 61 62 6c 65 20 2a 70 2c 20 0a 20 20 69 6e 74  Table *p, .  int
44da7 20 69 4c 61 6e 67 69 64 2c 20 20 20 20 20 20 20   iLangid,       
44da8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
44da9 4c 61 6e 67 75 61 67 65 20 69 64 20 2a 2f 0a 20  Language id */. 
44daa 20 69 6e 74 20 69 49 6e 64 65 78 2c 20 20 20 20   int iIndex,    
44dab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44dac 20 2f 2a 20 49 6e 64 65 78 20 66 6f 72 20 70 2d   /* Index for p-
44dad 3e 61 49 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  >aIndex */.  int
44dae 20 69 4c 65 76 65 6c 2c 20 0a 20 20 69 6e 74 20   iLevel, .  int 
44daf 2a 70 69 49 64 78 0a 29 7b 0a 20 20 69 6e 74 20  *piIdx.){.  int 
44db0 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
44db1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
44db2 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20  eturn Code */.  
44db3 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 4e  sqlite3_stmt *pN
44db4 65 78 74 49 64 78 3b 20 20 20 20 20 20 20 20 20  extIdx;         
44db5 2f 2a 20 51 75 65 72 79 20 66 6f 72 20 6e 65 78  /* Query for nex
44db6 74 20 69 64 78 20 61 74 20 6c 65 76 65 6c 20 69  t idx at level i
44db7 4c 65 76 65 6c 20 2a 2f 0a 20 20 69 6e 74 20 69  Level */.  int i
44db8 4e 65 78 74 20 3d 20 30 3b 20 20 20 20 20 20 20  Next = 0;       
44db9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
44dba 73 75 6c 74 20 6f 66 20 71 75 65 72 79 20 70 4e  sult of query pN
44dbb 65 78 74 49 64 78 20 2a 2f 0a 0a 20 20 61 73 73  extIdx */..  ass
44dbc 65 72 74 28 20 69 4c 61 6e 67 69 64 3e 3d 30 20  ert( iLangid>=0 
44dbd 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
44dbe 6e 49 6e 64 65 78 3e 3d 31 20 29 3b 0a 0a 20 20  nIndex>=1 );..  
44dbf 2f 2a 20 53 65 74 20 76 61 72 69 61 62 6c 65 20  /* Set variable 
44dc0 69 4e 65 78 74 20 74 6f 20 74 68 65 20 6e 65 78  iNext to the nex
44dc1 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65 67 64  t available segd
44dc2 69 72 20 69 6e 64 65 78 20 61 74 20 6c 65 76 65  ir index at leve
44dc3 6c 20 69 4c 65 76 65 6c 2e 20 2a 2f 0a 20 20 72  l iLevel. */.  r
44dc4 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28  c = fts3SqlStmt(
44dc5 70 2c 20 53 51 4c 5f 4e 45 58 54 5f 53 45 47 4d  p, SQL_NEXT_SEGM
44dc6 45 4e 54 5f 49 4e 44 45 58 2c 20 26 70 4e 65 78  ENT_INDEX, &pNex
44dc7 74 49 64 78 2c 20 30 29 3b 0a 20 20 69 66 28 20  tIdx, 0);.  if( 
44dc8 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
44dc9 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  .    sqlite3_bin
44dca 64 5f 69 6e 74 36 34 28 0a 20 20 20 20 20 20 20  d_int64(.       
44dcb 20 70 4e 65 78 74 49 64 78 2c 20 31 2c 20 67 65   pNextIdx, 1, ge
44dcc 74 41 62 73 6f 6c 75 74 65 4c 65 76 65 6c 28 70  tAbsoluteLevel(p
44dcd 2c 20 69 4c 61 6e 67 69 64 2c 20 69 49 6e 64 65  , iLangid, iInde
44dce 78 2c 20 69 4c 65 76 65 6c 29 0a 20 20 20 20 29  x, iLevel).    )
44dcf 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  ;.    if( SQLITE
44dd0 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74  _ROW==sqlite3_st
44dd1 65 70 28 70 4e 65 78 74 49 64 78 29 20 29 7b 0a  ep(pNextIdx) ){.
44dd2 20 20 20 20 20 20 69 4e 65 78 74 20 3d 20 73 71        iNext = sq
44dd3 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
44dd4 28 70 4e 65 78 74 49 64 78 2c 20 30 29 3b 0a 20  (pNextIdx, 0);. 
44dd5 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71     }.    rc = sq
44dd6 6c 69 74 65 33 5f 72 65 73 65 74 28 70 4e 65 78  lite3_reset(pNex
44dd7 74 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  tIdx);.  }..  if
44dd8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
44dd9 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 69 4e 65  ){.    /* If iNe
44dda 78 74 20 69 73 20 46 54 53 33 5f 4d 45 52 47 45  xt is FTS3_MERGE
44ddb 5f 43 4f 55 4e 54 2c 20 69 6e 64 69 63 61 74 69  _COUNT, indicati
44ddc 6e 67 20 74 68 61 74 20 6c 65 76 65 6c 20 69 4c  ng that level iL
44ddd 65 76 65 6c 20 69 73 20 61 6c 72 65 61 64 79 0a  evel is already.
44dde 20 20 20 20 2a 2a 20 66 75 6c 6c 2c 20 6d 65 72      ** full, mer
44ddf 67 65 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20  ge all segments 
44de0 69 6e 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c 20  in level iLevel 
44de1 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 69 4c  into a single iL
44de2 65 76 65 6c 2b 31 0a 20 20 20 20 2a 2a 20 73 65  evel+1.    ** se
44de3 67 6d 65 6e 74 20 61 6e 64 20 61 6c 6c 6f 63 61  gment and alloca
44de4 74 65 20 28 6e 65 77 6c 79 20 66 72 65 65 64 29  te (newly freed)
44de5 20 69 6e 64 65 78 20 30 20 61 74 20 6c 65 76 65   index 0 at leve
44de6 6c 20 69 4c 65 76 65 6c 2e 20 4f 74 68 65 72 77  l iLevel. Otherw
44de7 69 73 65 2c 0a 20 20 20 20 2a 2a 20 69 66 20 69  ise,.    ** if i
44de8 4e 65 78 74 20 69 73 20 6c 65 73 73 20 74 68 61  Next is less tha
44de9 6e 20 46 54 53 33 5f 4d 45 52 47 45 5f 43 4f 55  n FTS3_MERGE_COU
44dea 4e 54 2c 20 61 6c 6c 6f 63 61 74 65 20 69 6e 64  NT, allocate ind
44deb 65 78 20 69 4e 65 78 74 2e 0a 20 20 20 20 2a 2f  ex iNext..    */
44dec 0a 20 20 20 20 69 66 28 20 69 4e 65 78 74 3e 3d  .    if( iNext>=
44ded 46 54 53 33 5f 4d 45 52 47 45 5f 43 4f 55 4e 54  FTS3_MERGE_COUNT
44dee 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33 4c 6f   ){.      fts3Lo
44def 67 4d 65 72 67 65 28 31 36 2c 20 67 65 74 41 62  gMerge(16, getAb
44df0 73 6f 6c 75 74 65 4c 65 76 65 6c 28 70 2c 20 69  soluteLevel(p, i
44df1 4c 61 6e 67 69 64 2c 20 69 49 6e 64 65 78 2c 20  Langid, iIndex, 
44df2 69 4c 65 76 65 6c 29 29 3b 0a 20 20 20 20 20 20  iLevel));.      
44df3 72 63 20 3d 20 66 74 73 33 53 65 67 6d 65 6e 74  rc = fts3Segment
44df4 4d 65 72 67 65 28 70 2c 20 69 4c 61 6e 67 69 64  Merge(p, iLangid
44df5 2c 20 69 49 6e 64 65 78 2c 20 69 4c 65 76 65 6c  , iIndex, iLevel
44df6 29 3b 0a 20 20 20 20 20 20 2a 70 69 49 64 78 20  );.      *piIdx 
44df7 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 0;.    }else{.
44df8 20 20 20 20 20 20 2a 70 69 49 64 78 20 3d 20 69        *piIdx = i
44df9 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Next;.    }.  }.
44dfa 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
44dfb 0a 2f 2a 0a 2a 2a 20 54 68 65 20 25 5f 73 65 67  ./*.** The %_seg
44dfc 6d 65 6e 74 73 20 74 61 62 6c 65 20 69 73 20 64  ments table is d
44dfd 65 63 6c 61 72 65 64 20 61 73 20 66 6f 6c 6c 6f  eclared as follo
44dfe 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 43 52 45 41  ws:.**.**   CREA
44dff 54 45 20 54 41 42 4c 45 20 25 5f 73 65 67 6d 65  TE TABLE %_segme
44e00 6e 74 73 28 62 6c 6f 63 6b 69 64 20 49 4e 54 45  nts(blockid INTE
44e01 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  GER PRIMARY KEY,
44e02 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 0a 2a 2a 0a   block BLOB).**.
44e03 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
44e04 20 72 65 61 64 73 20 64 61 74 61 20 66 72 6f 6d   reads data from
44e05 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66   a single row of
44e06 20 74 68 65 20 25 5f 73 65 67 6d 65 6e 74 73 20   the %_segments 
44e07 74 61 62 6c 65 2e 20 54 68 65 0a 2a 2a 20 73 70  table. The.** sp
44e08 65 63 69 66 69 63 20 72 6f 77 20 69 73 20 69 64  ecific row is id
44e09 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20  entified by the 
44e0a 69 42 6c 6f 63 6b 69 64 20 70 61 72 61 6d 65 74  iBlockid paramet
44e0b 65 72 2e 20 49 66 20 70 61 42 6c 6f 62 20 69 73  er. If paBlob is
44e0c 20 6e 6f 74 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68   not.** NULL, th
44e0d 65 6e 20 61 20 62 75 66 66 65 72 20 69 73 20 61  en a buffer is a
44e0e 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 73  llocated using s
44e0f 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
44e10 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 0a 2a 2a  and populated.**
44e11 20 77 69 74 68 20 74 68 65 20 63 6f 6e 74 65 6e   with the conten
44e12 74 73 20 6f 66 20 74 68 65 20 62 6c 6f 62 20 73  ts of the blob s
44e13 74 6f 72 65 64 20 69 6e 20 74 68 65 20 22 62 6c  tored in the "bl
44e14 6f 63 6b 22 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  ock" column of t
44e15 68 65 20 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65  he .** identifie
44e16 64 20 74 61 62 6c 65 20 72 6f 77 20 69 73 2e 20  d table row is. 
44e17 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 70  Whether or not p
44e18 61 42 6c 6f 62 20 69 73 20 4e 55 4c 4c 2c 20 2a  aBlob is NULL, *
44e19 70 6e 42 6c 6f 62 20 69 73 20 73 65 74 0a 2a 2a  pnBlob is set.**
44e1a 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20   to the size of 
44e1b 74 68 65 20 62 6c 6f 62 20 69 6e 20 62 79 74 65  the blob in byte
44e1c 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  s before returni
44e1d 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ng..**.** If an 
44e1e 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6f 72  error occurs, or
44e1f 20 74 68 65 20 74 61 62 6c 65 20 64 6f 65 73 20   the table does 
44e20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  not contain the 
44e21 73 70 65 63 69 66 69 65 64 20 72 6f 77 2c 0a 2a  specified row,.*
44e22 2a 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  * an SQLite erro
44e23 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
44e24 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53  ed. Otherwise, S
44e25 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
44e26 72 6e 65 64 2e 20 49 66 0a 2a 2a 20 70 61 42 6c  rned. If.** paBl
44e27 6f 62 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20  ob is non-NULL, 
44e28 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 72  then it is the r
44e29 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66  esponsibility of
44e2a 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 0a 2a   the caller to.*
44e2b 2a 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65  * eventually fre
44e2c 65 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 62  e the returned b
44e2d 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  uffer..**.** Thi
44e2e 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6c  s function may l
44e2f 65 61 76 65 20 61 6e 20 6f 70 65 6e 20 73 71 6c  eave an open sql
44e30 69 74 65 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c  ite3_blob* handl
44e31 65 20 69 6e 20 74 68 65 0a 2a 2a 20 46 74 73 33  e in the.** Fts3
44e32 54 61 62 6c 65 2e 70 53 65 67 6d 65 6e 74 73 20  Table.pSegments 
44e33 76 61 72 69 61 62 6c 65 2e 20 54 68 69 73 20 68  variable. This h
44e34 61 6e 64 6c 65 20 69 73 20 72 65 75 73 65 64 20  andle is reused 
44e35 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 63 61  by subsequent ca
44e36 6c 6c 73 0a 2a 2a 20 74 6f 20 74 68 69 73 20 66  lls.** to this f
44e37 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 68 61 6e  unction. The han
44e38 64 6c 65 20 6d 61 79 20 62 65 20 63 6c 6f 73 65  dle may be close
44e39 64 20 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 65  d by calling the
44e3a 0a 2a 2a 20 73 71 6c 69 74 65 33 46 74 73 33 53  .** sqlite3Fts3S
44e3b 65 67 6d 65 6e 74 73 43 6c 6f 73 65 28 29 20 66  egmentsClose() f
44e3c 75 6e 63 74 69 6f 6e 2e 20 52 65 75 73 69 6e 67  unction. Reusing
44e3d 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 69   a blob handle i
44e3e 73 20 61 20 68 61 6e 64 79 0a 2a 2a 20 70 65 72  s a handy.** per
44e3f 66 6f 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65  formance improve
44e40 6d 65 6e 74 2c 20 62 75 74 20 74 68 65 20 62 6c  ment, but the bl
44e41 6f 62 20 68 61 6e 64 6c 65 20 73 68 6f 75 6c 64  ob handle should
44e42 20 61 6c 77 61 79 73 20 62 65 20 63 6c 6f 73 65   always be close
44e43 64 0a 2a 2a 20 62 65 66 6f 72 65 20 63 6f 6e 74  d.** before cont
44e44 72 6f 6c 20 69 73 20 72 65 74 75 72 6e 65 64 20  rol is returned 
44e45 74 6f 20 74 68 65 20 75 73 65 72 20 28 74 6f 20  to the user (to 
44e46 70 72 65 76 65 6e 74 20 61 20 6c 6f 63 6b 20 62  prevent a lock b
44e47 65 69 6e 67 20 68 65 6c 64 0a 2a 2a 20 6f 6e 20  eing held.** on 
44e48 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
44e49 65 20 66 6f 72 20 6c 6f 6e 67 65 72 20 74 68 61  e for longer tha
44e4a 6e 20 6e 65 63 65 73 73 61 72 79 29 2e 20 54 68  n necessary). Th
44e4b 75 73 2c 20 61 6e 79 20 76 69 72 74 75 61 6c 20  us, any virtual 
44e4c 74 61 62 6c 65 0a 2a 2a 20 6d 65 74 68 6f 64 20  table.** method 
44e4d 28 78 46 69 6c 74 65 72 20 65 74 63 2e 29 20 74  (xFilter etc.) t
44e4e 68 61 74 20 6d 61 79 20 64 69 72 65 63 74 6c 79  hat may directly
44e4f 20 6f 72 20 69 6e 64 69 72 65 63 74 6c 79 20 63   or indirectly c
44e50 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  all this functio
44e51 6e 0a 2a 2a 20 6d 75 73 74 20 63 61 6c 6c 20 73  n.** must call s
44e52 71 6c 69 74 65 33 46 74 73 33 53 65 67 6d 65 6e  qlite3Fts3Segmen
44e53 74 73 43 6c 6f 73 65 28 29 20 62 65 66 6f 72 65  tsClose() before
44e54 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53   returning..*/.S
44e55 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
44e56 74 20 73 71 6c 69 74 65 33 46 74 73 33 52 65 61  t sqlite3Fts3Rea
44e57 64 42 6c 6f 63 6b 28 0a 20 20 46 74 73 33 54 61  dBlock(.  Fts3Ta
44e58 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ble *p,         
44e59 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53            /* FTS
44e5a 33 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  3 table handle *
44e5b 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
44e5c 34 20 69 42 6c 6f 63 6b 69 64 2c 20 20 20 20 20  4 iBlockid,     
44e5d 20 20 20 20 2f 2a 20 41 63 63 65 73 73 20 74 68      /* Access th
44e5e 65 20 72 6f 77 20 77 69 74 68 20 62 6c 6f 63 6b  e row with block
44e5f 69 64 3d 24 69 42 6c 6f 63 6b 69 64 20 2a 2f 0a  id=$iBlockid */.
44e60 20 20 63 68 61 72 20 2a 2a 70 61 42 6c 6f 62 2c    char **paBlob,
44e61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44e62 20 20 2f 2a 20 4f 55 54 3a 20 42 6c 6f 62 20 64    /* OUT: Blob d
44e63 61 74 61 20 69 6e 20 6d 61 6c 6c 6f 63 27 64 20  ata in malloc'd 
44e64 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20  buffer */.  int 
44e65 2a 70 6e 42 6c 6f 62 2c 20 20 20 20 20 20 20 20  *pnBlob,        
44e66 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
44e67 55 54 3a 20 53 69 7a 65 20 6f 66 20 62 6c 6f 62  UT: Size of blob
44e68 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a   data */.  int *
44e69 70 6e 4c 6f 61 64 20 20 20 20 20 20 20 20 20 20  pnLoad          
44e6a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
44e6b 54 3a 20 42 79 74 65 73 20 61 63 74 75 61 6c 6c  T: Bytes actuall
44e6c 79 20 6c 6f 61 64 65 64 20 2a 2f 0a 29 7b 0a 20  y loaded */.){. 
44e6d 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
44e6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44e6f 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
44e70 2a 2f 0a 0a 20 20 2f 2a 20 70 6e 42 6c 6f 62 20  */..  /* pnBlob 
44e71 6d 75 73 74 20 62 65 20 6e 6f 6e 2d 4e 55 4c 4c  must be non-NULL
44e72 2e 20 70 61 42 6c 6f 62 20 6d 61 79 20 62 65 20  . paBlob may be 
44e73 4e 55 4c 4c 20 6f 72 20 6e 6f 6e 2d 4e 55 4c 4c  NULL or non-NULL
44e74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  . */.  assert( p
44e75 6e 42 6c 6f 62 20 29 3b 0a 0a 20 20 69 66 28 20  nBlob );..  if( 
44e76 70 2d 3e 70 53 65 67 6d 65 6e 74 73 20 29 7b 0a  p->pSegments ){.
44e77 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
44e78 5f 62 6c 6f 62 5f 72 65 6f 70 65 6e 28 70 2d 3e  _blob_reopen(p->
44e79 70 53 65 67 6d 65 6e 74 73 2c 20 69 42 6c 6f 63  pSegments, iBloc
44e7a 6b 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  kid);.  }else{. 
44e7b 20 20 20 69 66 28 20 30 3d 3d 70 2d 3e 7a 53 65     if( 0==p->zSe
44e7c 67 6d 65 6e 74 73 54 62 6c 20 29 7b 0a 20 20 20  gmentsTbl ){.   
44e7d 20 20 20 70 2d 3e 7a 53 65 67 6d 65 6e 74 73 54     p->zSegmentsT
44e7e 62 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  bl = sqlite3_mpr
44e7f 69 6e 74 66 28 22 25 73 5f 73 65 67 6d 65 6e 74  intf("%s_segment
44e80 73 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  s", p->zName);. 
44e81 20 20 20 20 20 69 66 28 20 30 3d 3d 70 2d 3e 7a       if( 0==p->z
44e82 53 65 67 6d 65 6e 74 73 54 62 6c 20 29 20 72 65  SegmentsTbl ) re
44e83 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
44e84 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  M;.    }.    rc 
44e85 3d 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f  = sqlite3_blob_o
44e86 70 65 6e 28 0a 20 20 20 20 20 20 20 70 2d 3e 64  pen(.       p->d
44e87 62 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 53  b, p->zDb, p->zS
44e88 65 67 6d 65 6e 74 73 54 62 6c 2c 20 22 62 6c 6f  egmentsTbl, "blo
44e89 63 6b 22 2c 20 69 42 6c 6f 63 6b 69 64 2c 20 30  ck", iBlockid, 0
44e8a 2c 20 26 70 2d 3e 70 53 65 67 6d 65 6e 74 73 0a  , &p->pSegments.
44e8b 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66      );.  }..  if
44e8c 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
44e8d 29 7b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65  ){.    int nByte
44e8e 20 3d 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f   = sqlite3_blob_
44e8f 62 79 74 65 73 28 70 2d 3e 70 53 65 67 6d 65 6e  bytes(p->pSegmen
44e90 74 73 29 3b 0a 20 20 20 20 2a 70 6e 42 6c 6f 62  ts);.    *pnBlob
44e91 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 69 66   = nByte;.    if
44e92 28 20 70 61 42 6c 6f 62 20 29 7b 0a 20 20 20 20  ( paBlob ){.    
44e93 20 20 63 68 61 72 20 2a 61 42 79 74 65 20 3d 20    char *aByte = 
44e94 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
44e95 42 79 74 65 20 2b 20 46 54 53 33 5f 4e 4f 44 45  Byte + FTS3_NODE
44e96 5f 50 41 44 44 49 4e 47 29 3b 0a 20 20 20 20 20  _PADDING);.     
44e97 20 69 66 28 20 21 61 42 79 74 65 20 29 7b 0a 20   if( !aByte ){. 
44e98 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
44e99 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
44e9a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69  }else{.        i
44e9b 66 28 20 70 6e 4c 6f 61 64 20 26 26 20 6e 42 79  f( pnLoad && nBy
44e9c 74 65 3e 28 46 54 53 33 5f 4e 4f 44 45 5f 43 48  te>(FTS3_NODE_CH
44e9d 55 4e 4b 5f 54 48 52 45 53 48 4f 4c 44 29 20 29  UNK_THRESHOLD) )
44e9e 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 42 79 74  {.          nByt
44e9f 65 20 3d 20 46 54 53 33 5f 4e 4f 44 45 5f 43 48  e = FTS3_NODE_CH
44ea0 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 20 20 20  UNKSIZE;.       
44ea1 20 20 20 2a 70 6e 4c 6f 61 64 20 3d 20 6e 42 79     *pnLoad = nBy
44ea2 74 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  te;.        }.  
44ea3 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
44ea4 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 70 2d 3e  e3_blob_read(p->
44ea5 70 53 65 67 6d 65 6e 74 73 2c 20 61 42 79 74 65  pSegments, aByte
44ea6 2c 20 6e 42 79 74 65 2c 20 30 29 3b 0a 20 20 20  , nByte, 0);.   
44ea7 20 20 20 20 20 6d 65 6d 73 65 74 28 26 61 42 79       memset(&aBy
44ea8 74 65 5b 6e 42 79 74 65 5d 2c 20 30 2c 20 46 54  te[nByte], 0, FT
44ea9 53 33 5f 4e 4f 44 45 5f 50 41 44 44 49 4e 47 29  S3_NODE_PADDING)
44eaa 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
44eab 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
44eac 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
44ead 5f 66 72 65 65 28 61 42 79 74 65 29 3b 0a 20 20  _free(aByte);.  
44eae 20 20 20 20 20 20 20 20 61 42 79 74 65 20 3d 20          aByte = 
44eaf 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
44eb0 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 61 42 6c     }.      *paBl
44eb1 6f 62 20 3d 20 61 42 79 74 65 3b 0a 20 20 20 20  ob = aByte;.    
44eb2 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
44eb3 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  rc;.}../*.** Clo
44eb4 73 65 20 74 68 65 20 62 6c 6f 62 20 68 61 6e 64  se the blob hand
44eb5 6c 65 20 61 74 20 70 2d 3e 70 53 65 67 6d 65 6e  le at p->pSegmen
44eb6 74 73 2c 20 69 66 20 69 74 20 69 73 20 6f 70 65  ts, if it is ope
44eb7 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20  n. See comments 
44eb8 61 62 6f 76 65 0a 2a 2a 20 74 68 65 20 73 71 6c  above.** the sql
44eb9 69 74 65 33 46 74 73 33 52 65 61 64 42 6c 6f 63  ite3Fts3ReadBloc
44eba 6b 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  k() function for
44ebb 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c   details..*/.SQL
44ebc 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
44ebd 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 6d   sqlite3Fts3Segm
44ebe 65 6e 74 73 43 6c 6f 73 65 28 46 74 73 33 54 61  entsClose(Fts3Ta
44ebf 62 6c 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74  ble *p){.  sqlit
44ec0 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 70 2d  e3_blob_close(p-
44ec1 3e 70 53 65 67 6d 65 6e 74 73 29 3b 0a 20 20 70  >pSegments);.  p
44ec2 2d 3e 70 53 65 67 6d 65 6e 74 73 20 3d 20 30 3b  ->pSegments = 0;
44ec3 0a 7d 0a 20 20 20 20 0a 73 74 61 74 69 63 20 69  .}.    .static i
44ec4 6e 74 20 66 74 73 33 53 65 67 52 65 61 64 65 72  nt fts3SegReader
44ec5 49 6e 63 72 52 65 61 64 28 46 74 73 33 53 65 67  IncrRead(Fts3Seg
44ec6 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
44ec7 7b 0a 20 20 69 6e 74 20 6e 52 65 61 64 3b 20 20  {.  int nRead;  
44ec8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44ec9 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
44eca 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a   bytes to read *
44ecb 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
44ecc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44ecd 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
44ece 64 65 20 2a 2f 0a 0a 20 20 6e 52 65 61 64 20 3d  de */..  nRead =
44ecf 20 4d 49 4e 28 70 52 65 61 64 65 72 2d 3e 6e 4e   MIN(pReader->nN
44ed0 6f 64 65 20 2d 20 70 52 65 61 64 65 72 2d 3e 6e  ode - pReader->n
44ed1 50 6f 70 75 6c 61 74 65 2c 20 46 54 53 33 5f 4e  Populate, FTS3_N
44ed2 4f 44 45 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a  ODE_CHUNKSIZE);.
44ed3 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
44ed4 6c 6f 62 5f 72 65 61 64 28 0a 20 20 20 20 20 20  lob_read(.      
44ed5 70 52 65 61 64 65 72 2d 3e 70 42 6c 6f 62 2c 20  pReader->pBlob, 
44ed6 0a 20 20 20 20 20 20 26 70 52 65 61 64 65 72 2d  .      &pReader-
44ed7 3e 61 4e 6f 64 65 5b 70 52 65 61 64 65 72 2d 3e  >aNode[pReader->
44ed8 6e 50 6f 70 75 6c 61 74 65 5d 2c 0a 20 20 20 20  nPopulate],.    
44ed9 20 20 6e 52 65 61 64 2c 0a 20 20 20 20 20 20 70    nRead,.      p
44eda 52 65 61 64 65 72 2d 3e 6e 50 6f 70 75 6c 61 74  Reader->nPopulat
44edb 65 0a 20 20 29 3b 0a 0a 20 20 69 66 28 20 72 63  e.  );..  if( rc
44edc 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
44edd 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 50 6f 70     pReader->nPop
44ede 75 6c 61 74 65 20 2b 3d 20 6e 52 65 61 64 3b 0a  ulate += nRead;.
44edf 20 20 20 20 6d 65 6d 73 65 74 28 26 70 52 65 61      memset(&pRea
44ee0 64 65 72 2d 3e 61 4e 6f 64 65 5b 70 52 65 61 64  der->aNode[pRead
44ee1 65 72 2d 3e 6e 50 6f 70 75 6c 61 74 65 5d 2c 20  er->nPopulate], 
44ee2 30 2c 20 46 54 53 33 5f 4e 4f 44 45 5f 50 41 44  0, FTS3_NODE_PAD
44ee3 44 49 4e 47 29 3b 0a 20 20 20 20 69 66 28 20 70  DING);.    if( p
44ee4 52 65 61 64 65 72 2d 3e 6e 50 6f 70 75 6c 61 74  Reader->nPopulat
44ee5 65 3d 3d 70 52 65 61 64 65 72 2d 3e 6e 4e 6f 64  e==pReader->nNod
44ee6 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  e ){.      sqlit
44ee7 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 70 52  e3_blob_close(pR
44ee8 65 61 64 65 72 2d 3e 70 42 6c 6f 62 29 3b 0a 20  eader->pBlob);. 
44ee9 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70 42       pReader->pB
44eea 6c 6f 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  lob = 0;.      p
44eeb 52 65 61 64 65 72 2d 3e 6e 50 6f 70 75 6c 61 74  Reader->nPopulat
44eec 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  e = 0;.    }.  }
44eed 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
44eee 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
44eef 53 65 67 52 65 61 64 65 72 52 65 71 75 69 72 65  SegReaderRequire
44ef0 28 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a  (Fts3SegReader *
44ef1 70 52 65 61 64 65 72 2c 20 63 68 61 72 20 2a 70  pReader, char *p
44ef2 46 72 6f 6d 2c 20 69 6e 74 20 6e 42 79 74 65 29  From, int nByte)
44ef3 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
44ef4 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74  ITE_OK;.  assert
44ef5 28 20 21 70 52 65 61 64 65 72 2d 3e 70 42 6c 6f  ( !pReader->pBlo
44ef6 62 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 46  b .       || (pF
44ef7 72 6f 6d 3e 3d 70 52 65 61 64 65 72 2d 3e 61 4e  rom>=pReader->aN
44ef8 6f 64 65 20 26 26 20 70 46 72 6f 6d 3c 26 70 52  ode && pFrom<&pR
44ef9 65 61 64 65 72 2d 3e 61 4e 6f 64 65 5b 70 52 65  eader->aNode[pRe
44efa 61 64 65 72 2d 3e 6e 4e 6f 64 65 5d 29 0a 20 20  ader->nNode]).  
44efb 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 52 65 61  );.  while( pRea
44efc 64 65 72 2d 3e 70 42 6c 6f 62 20 26 26 20 72 63  der->pBlob && rc
44efd 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 0a 20 20 20  ==SQLITE_OK .   
44efe 20 20 26 26 20 20 28 70 46 72 6f 6d 20 2d 20 70    &&  (pFrom - p
44eff 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 20 2b 20  Reader->aNode + 
44f00 6e 42 79 74 65 29 3e 70 52 65 61 64 65 72 2d 3e  nByte)>pReader->
44f01 6e 50 6f 70 75 6c 61 74 65 0a 20 20 29 7b 0a 20  nPopulate.  ){. 
44f02 20 20 20 72 63 20 3d 20 66 74 73 33 53 65 67 52     rc = fts3SegR
44f03 65 61 64 65 72 49 6e 63 72 52 65 61 64 28 70 52  eaderIncrRead(pR
44f04 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65  eader);.  }.  re
44f05 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
44f06 2a 20 53 65 74 20 61 6e 20 46 74 73 33 53 65 67  * Set an Fts3Seg
44f07 52 65 61 64 65 72 20 63 75 72 73 6f 72 20 74 6f  Reader cursor to
44f08 20 70 6f 69 6e 74 20 61 74 20 45 4f 46 2e 0a 2a   point at EOF..*
44f09 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  /.static void ft
44f0a 73 33 53 65 67 52 65 61 64 65 72 53 65 74 45 6f  s3SegReaderSetEo
44f0b 66 28 46 74 73 33 53 65 67 52 65 61 64 65 72 20  f(Fts3SegReader 
44f0c 2a 70 53 65 67 29 7b 0a 20 20 69 66 28 20 21 66  *pSeg){.  if( !f
44f0d 74 73 33 53 65 67 52 65 61 64 65 72 49 73 52 6f  ts3SegReaderIsRo
44f0e 6f 74 4f 6e 6c 79 28 70 53 65 67 29 20 29 7b 0a  otOnly(pSeg) ){.
44f0f 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
44f10 28 70 53 65 67 2d 3e 61 4e 6f 64 65 29 3b 0a 20  (pSeg->aNode);. 
44f11 20 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f     sqlite3_blob_
44f12 63 6c 6f 73 65 28 70 53 65 67 2d 3e 70 42 6c 6f  close(pSeg->pBlo
44f13 62 29 3b 0a 20 20 20 20 70 53 65 67 2d 3e 70 42  b);.    pSeg->pB
44f14 6c 6f 62 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70  lob = 0;.  }.  p
44f15 53 65 67 2d 3e 61 4e 6f 64 65 20 3d 20 30 3b 0a  Seg->aNode = 0;.
44f16 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68  }../*.** Move th
44f17 65 20 69 74 65 72 61 74 6f 72 20 70 61 73 73 65  e iterator passe
44f18 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  d as the first a
44f19 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 6e  rgument to the n
44f1a 65 78 74 20 74 65 72 6d 20 69 6e 20 74 68 65 0a  ext term in the.
44f1b 2a 2a 20 73 65 67 6d 65 6e 74 2e 20 49 66 20 73  ** segment. If s
44f1c 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54  uccessful, SQLIT
44f1d 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
44f1e 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  . If there is no
44f1f 20 6e 65 78 74 20 74 65 72 6d 2c 0a 2a 2a 20 53   next term,.** S
44f20 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 4f 74 68 65  QLITE_DONE. Othe
44f21 72 77 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65  rwise, an SQLite
44f22 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a   error code..*/.
44f23 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53  static int fts3S
44f24 65 67 52 65 61 64 65 72 4e 65 78 74 28 0a 20 20  egReaderNext(.  
44f25 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 0a 20  Fts3Table *p, . 
44f26 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a   Fts3SegReader *
44f27 70 52 65 61 64 65 72 2c 0a 20 20 69 6e 74 20 62  pReader,.  int b
44f28 49 6e 63 72 0a 29 7b 0a 20 20 69 6e 74 20 72 63  Incr.){.  int rc
44f29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
44f2a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
44f2b 75 72 6e 20 63 6f 64 65 20 6f 66 20 76 61 72 69  urn code of vari
44f2c 6f 75 73 20 73 75 62 2d 72 6f 75 74 69 6e 65 73  ous sub-routines
44f2d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 4e 65 78   */.  char *pNex
44f2e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
44f2f 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
44f30 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  variable */.  in
44f31 74 20 6e 50 72 65 66 69 78 3b 20 20 20 20 20 20  t nPrefix;      
44f32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
44f33 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
44f34 20 69 6e 20 74 65 72 6d 20 70 72 65 66 69 78 20   in term prefix 
44f35 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 66 66 69 78  */.  int nSuffix
44f36 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
44f37 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
44f38 66 20 62 79 74 65 73 20 69 6e 20 74 65 72 6d 20  f bytes in term 
44f39 73 75 66 66 69 78 20 2a 2f 0a 0a 20 20 69 66 28  suffix */..  if(
44f3a 20 21 70 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c   !pReader->aDocl
44f3b 69 73 74 20 29 7b 0a 20 20 20 20 70 4e 65 78 74  ist ){.    pNext
44f3c 20 3d 20 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64   = pReader->aNod
44f3d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  e;.  }else{.    
44f3e 70 4e 65 78 74 20 3d 20 26 70 52 65 61 64 65 72  pNext = &pReader
44f3f 2d 3e 61 44 6f 63 6c 69 73 74 5b 70 52 65 61 64  ->aDoclist[pRead
44f40 65 72 2d 3e 6e 44 6f 63 6c 69 73 74 5d 3b 0a 20  er->nDoclist];. 
44f41 20 7d 0a 0a 20 20 69 66 28 20 21 70 4e 65 78 74   }..  if( !pNext
44f42 20 7c 7c 20 70 4e 65 78 74 3e 3d 26 70 52 65 61   || pNext>=&pRea
44f43 64 65 72 2d 3e 61 4e 6f 64 65 5b 70 52 65 61 64  der->aNode[pRead
44f44 65 72 2d 3e 6e 4e 6f 64 65 5d 20 29 7b 0a 0a 20  er->nNode] ){.. 
44f45 20 20 20 69 66 28 20 66 74 73 33 53 65 67 52 65     if( fts3SegRe
44f46 61 64 65 72 49 73 50 65 6e 64 69 6e 67 28 70 52  aderIsPending(pR
44f47 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 20 20  eader) ){.      
44f48 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 45  Fts3HashElem *pE
44f49 6c 65 6d 20 3d 20 2a 28 70 52 65 61 64 65 72 2d  lem = *(pReader-
44f4a 3e 70 70 4e 65 78 74 45 6c 65 6d 29 3b 0a 20 20  >ppNextElem);.  
44f4b 20 20 20 20 69 66 28 20 70 45 6c 65 6d 3d 3d 30      if( pElem==0
44f4c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 65 61   ){.        pRea
44f4d 64 65 72 2d 3e 61 4e 6f 64 65 20 3d 20 30 3b 0a  der->aNode = 0;.
44f4e 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
44f4f 20 20 20 20 20 50 65 6e 64 69 6e 67 4c 69 73 74       PendingList
44f50 20 2a 70 4c 69 73 74 20 3d 20 28 50 65 6e 64 69   *pList = (Pendi
44f51 6e 67 4c 69 73 74 20 2a 29 66 74 73 33 48 61 73  ngList *)fts3Has
44f52 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20  hData(pElem);.  
44f53 20 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e 7a        pReader->z
44f54 54 65 72 6d 20 3d 20 28 63 68 61 72 20 2a 29 66  Term = (char *)f
44f55 74 73 33 48 61 73 68 4b 65 79 28 70 45 6c 65 6d  ts3HashKey(pElem
44f56 29 3b 0a 20 20 20 20 20 20 20 20 70 52 65 61 64  );.        pRead
44f57 65 72 2d 3e 6e 54 65 72 6d 20 3d 20 66 74 73 33  er->nTerm = fts3
44f58 48 61 73 68 4b 65 79 73 69 7a 65 28 70 45 6c 65  HashKeysize(pEle
44f59 6d 29 3b 0a 20 20 20 20 20 20 20 20 70 52 65 61  m);.        pRea
44f5a 64 65 72 2d 3e 6e 4e 6f 64 65 20 3d 20 70 52 65  der->nNode = pRe
44f5b 61 64 65 72 2d 3e 6e 44 6f 63 6c 69 73 74 20 3d  ader->nDoclist =
44f5c 20 70 4c 69 73 74 2d 3e 6e 44 61 74 61 20 2b 20   pList->nData + 
44f5d 31 3b 0a 20 20 20 20 20 20 20 20 70 52 65 61 64  1;.        pRead
44f5e 65 72 2d 3e 61 4e 6f 64 65 20 3d 20 70 52 65 61  er->aNode = pRea
44f5f 64 65 72 2d 3e 61 44 6f 63 6c 69 73 74 20 3d 20  der->aDoclist = 
44f60 70 4c 69 73 74 2d 3e 61 44 61 74 61 3b 0a 20 20  pList->aData;.  
44f61 20 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70        pReader->p
44f62 70 4e 65 78 74 45 6c 65 6d 2b 2b 3b 0a 20 20 20  pNextElem++;.   
44f63 20 20 20 20 20 61 73 73 65 72 74 28 20 70 52 65       assert( pRe
44f64 61 64 65 72 2d 3e 61 4e 6f 64 65 20 29 3b 0a 20  ader->aNode );. 
44f65 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
44f66 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
44f67 20 20 20 7d 0a 0a 20 20 20 20 66 74 73 33 53 65     }..    fts3Se
44f68 67 52 65 61 64 65 72 53 65 74 45 6f 66 28 70 52  gReaderSetEof(pR
44f69 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 2f 2a 20  eader);..    /* 
44f6a 49 66 20 69 43 75 72 72 65 6e 74 42 6c 6f 63 6b  If iCurrentBlock
44f6b 3e 3d 69 4c 65 61 66 45 6e 64 42 6c 6f 63 6b 2c  >=iLeafEndBlock,
44f6c 20 74 68 69 73 20 69 73 20 61 6e 20 45 4f 46 20   this is an EOF 
44f6d 63 6f 6e 64 69 74 69 6f 6e 2e 20 41 6c 6c 20 6c  condition. All l
44f6e 65 61 66 20 0a 20 20 20 20 2a 2a 20 62 6c 6f 63  eaf .    ** bloc
44f6f 6b 73 20 68 61 76 65 20 61 6c 72 65 61 64 79 20  ks have already 
44f70 62 65 65 6e 20 74 72 61 76 65 72 73 65 64 2e 20  been traversed. 
44f71 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
44f72 70 52 65 61 64 65 72 2d 3e 69 43 75 72 72 65 6e  pReader->iCurren
44f73 74 42 6c 6f 63 6b 3c 3d 70 52 65 61 64 65 72 2d  tBlock<=pReader-
44f74 3e 69 4c 65 61 66 45 6e 64 42 6c 6f 63 6b 20 29  >iLeafEndBlock )
44f75 3b 0a 20 20 20 20 69 66 28 20 70 52 65 61 64 65  ;.    if( pReade
44f76 72 2d 3e 69 43 75 72 72 65 6e 74 42 6c 6f 63 6b  r->iCurrentBlock
44f77 3e 3d 70 52 65 61 64 65 72 2d 3e 69 4c 65 61 66  >=pReader->iLeaf
44f78 45 6e 64 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20  EndBlock ){.    
44f79 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
44f7a 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72  OK;.    }..    r
44f7b 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 52  c = sqlite3Fts3R
44f7c 65 61 64 42 6c 6f 63 6b 28 0a 20 20 20 20 20 20  eadBlock(.      
44f7d 20 20 70 2c 20 2b 2b 70 52 65 61 64 65 72 2d 3e    p, ++pReader->
44f7e 69 43 75 72 72 65 6e 74 42 6c 6f 63 6b 2c 20 26  iCurrentBlock, &
44f7f 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 2c 20  pReader->aNode, 
44f80 26 70 52 65 61 64 65 72 2d 3e 6e 4e 6f 64 65 2c  &pReader->nNode,
44f81 20 0a 20 20 20 20 20 20 20 20 28 62 49 6e 63 72   .        (bIncr
44f82 20 3f 20 26 70 52 65 61 64 65 72 2d 3e 6e 50 6f   ? &pReader->nPo
44f83 70 75 6c 61 74 65 20 3a 20 30 29 0a 20 20 20 20  pulate : 0).    
44f84 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
44f85 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
44f86 6e 20 72 63 3b 0a 20 20 20 20 61 73 73 65 72 74  n rc;.    assert
44f87 28 20 70 52 65 61 64 65 72 2d 3e 70 42 6c 6f 62  ( pReader->pBlob
44f88 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 62  ==0 );.    if( b
44f89 49 6e 63 72 20 26 26 20 70 52 65 61 64 65 72 2d  Incr && pReader-
44f8a 3e 6e 50 6f 70 75 6c 61 74 65 3c 70 52 65 61 64  >nPopulate<pRead
44f8b 65 72 2d 3e 6e 4e 6f 64 65 20 29 7b 0a 20 20 20  er->nNode ){.   
44f8c 20 20 20 70 52 65 61 64 65 72 2d 3e 70 42 6c 6f     pReader->pBlo
44f8d 62 20 3d 20 70 2d 3e 70 53 65 67 6d 65 6e 74 73  b = p->pSegments
44f8e 3b 0a 20 20 20 20 20 20 70 2d 3e 70 53 65 67 6d  ;.      p->pSegm
44f8f 65 6e 74 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  ents = 0;.    }.
44f90 20 20 20 20 70 4e 65 78 74 20 3d 20 70 52 65 61      pNext = pRea
44f91 64 65 72 2d 3e 61 4e 6f 64 65 3b 0a 20 20 7d 0a  der->aNode;.  }.
44f92 0a 20 20 61 73 73 65 72 74 28 20 21 66 74 73 33  .  assert( !fts3
44f93 53 65 67 52 65 61 64 65 72 49 73 50 65 6e 64 69  SegReaderIsPendi
44f94 6e 67 28 70 52 65 61 64 65 72 29 20 29 3b 0a 0a  ng(pReader) );..
44f95 20 20 72 63 20 3d 20 66 74 73 33 53 65 67 52 65    rc = fts3SegRe
44f96 61 64 65 72 52 65 71 75 69 72 65 28 70 52 65 61  aderRequire(pRea
44f97 64 65 72 2c 20 70 4e 65 78 74 2c 20 46 54 53 33  der, pNext, FTS3
44f98 5f 56 41 52 49 4e 54 5f 4d 41 58 2a 32 29 3b 0a  _VARINT_MAX*2);.
44f99 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
44f9a 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
44f9b 0a 20 20 0a 20 20 2f 2a 20 42 65 63 61 75 73 65  .  .  /* Because
44f9c 20 6f 66 20 74 68 65 20 46 54 53 33 5f 4e 4f 44   of the FTS3_NOD
44f9d 45 5f 50 41 44 44 49 4e 47 20 62 79 74 65 73 20  E_PADDING bytes 
44f9e 6f 66 20 70 61 64 64 69 6e 67 2c 20 74 68 65 20  of padding, the 
44f9f 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 0a 20 20  following is .  
44fa0 2a 2a 20 73 61 66 65 20 28 6e 6f 20 72 69 73 6b  ** safe (no risk
44fa1 20 6f 66 20 6f 76 65 72 72 65 61 64 29 20 65 76   of overread) ev
44fa2 65 6e 20 69 66 20 74 68 65 20 6e 6f 64 65 20 64  en if the node d
44fa3 61 74 61 20 69 73 20 63 6f 72 72 75 70 74 65 64  ata is corrupted
44fa4 2e 20 2a 2f 0a 20 20 70 4e 65 78 74 20 2b 3d 20  . */.  pNext += 
44fa5 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61  sqlite3Fts3GetVa
44fa6 72 69 6e 74 33 32 28 70 4e 65 78 74 2c 20 26 6e  rint32(pNext, &n
44fa7 50 72 65 66 69 78 29 3b 0a 20 20 70 4e 65 78 74  Prefix);.  pNext
44fa8 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47   += sqlite3Fts3G
44fa9 65 74 56 61 72 69 6e 74 33 32 28 70 4e 65 78 74  etVarint32(pNext
44faa 2c 20 26 6e 53 75 66 66 69 78 29 3b 0a 20 20 69  , &nSuffix);.  i
44fab 66 28 20 6e 50 72 65 66 69 78 3c 30 20 7c 7c 20  f( nPrefix<0 || 
44fac 6e 53 75 66 66 69 78 3c 3d 30 20 0a 20 20 20 7c  nSuffix<=0 .   |
44fad 7c 20 26 70 4e 65 78 74 5b 6e 53 75 66 66 69 78  | &pNext[nSuffix
44fae 5d 3e 26 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64  ]>&pReader->aNod
44faf 65 5b 70 52 65 61 64 65 72 2d 3e 6e 4e 6f 64 65  e[pReader->nNode
44fb0 5d 20 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75  ] .  ){.    retu
44fb1 72 6e 20 46 54 53 5f 43 4f 52 52 55 50 54 5f 56  rn FTS_CORRUPT_V
44fb2 54 41 42 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  TAB;.  }..  if( 
44fb3 6e 50 72 65 66 69 78 2b 6e 53 75 66 66 69 78 3e  nPrefix+nSuffix>
44fb4 70 52 65 61 64 65 72 2d 3e 6e 54 65 72 6d 41 6c  pReader->nTermAl
44fb5 6c 6f 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  loc ){.    int n
44fb6 4e 65 77 20 3d 20 28 6e 50 72 65 66 69 78 2b 6e  New = (nPrefix+n
44fb7 53 75 66 66 69 78 29 2a 32 3b 0a 20 20 20 20 63  Suffix)*2;.    c
44fb8 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73 71 6c 69  har *zNew = sqli
44fb9 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 52 65 61  te3_realloc(pRea
44fba 64 65 72 2d 3e 7a 54 65 72 6d 2c 20 6e 4e 65 77  der->zTerm, nNew
44fbb 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 4e 65 77  );.    if( !zNew
44fbc 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
44fbd 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
44fbe 20 20 20 7d 0a 20 20 20 20 70 52 65 61 64 65 72     }.    pReader
44fbf 2d 3e 7a 54 65 72 6d 20 3d 20 7a 4e 65 77 3b 0a  ->zTerm = zNew;.
44fc0 20 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 54 65      pReader->nTe
44fc1 72 6d 41 6c 6c 6f 63 20 3d 20 6e 4e 65 77 3b 0a  rmAlloc = nNew;.
44fc2 20 20 7d 0a 0a 20 20 72 63 20 3d 20 66 74 73 33    }..  rc = fts3
44fc3 53 65 67 52 65 61 64 65 72 52 65 71 75 69 72 65  SegReaderRequire
44fc4 28 70 52 65 61 64 65 72 2c 20 70 4e 65 78 74 2c  (pReader, pNext,
44fc5 20 6e 53 75 66 66 69 78 2b 46 54 53 33 5f 56 41   nSuffix+FTS3_VA
44fc6 52 49 4e 54 5f 4d 41 58 29 3b 0a 20 20 69 66 28  RINT_MAX);.  if(
44fc7 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
44fc8 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 6d   return rc;..  m
44fc9 65 6d 63 70 79 28 26 70 52 65 61 64 65 72 2d 3e  emcpy(&pReader->
44fca 7a 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 2c 20  zTerm[nPrefix], 
44fcb 70 4e 65 78 74 2c 20 6e 53 75 66 66 69 78 29 3b  pNext, nSuffix);
44fcc 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 54 65 72  .  pReader->nTer
44fcd 6d 20 3d 20 6e 50 72 65 66 69 78 2b 6e 53 75 66  m = nPrefix+nSuf
44fce 66 69 78 3b 0a 20 20 70 4e 65 78 74 20 2b 3d 20  fix;.  pNext += 
44fcf 6e 53 75 66 66 69 78 3b 0a 20 20 70 4e 65 78 74  nSuffix;.  pNext
44fd0 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47   += sqlite3Fts3G
44fd1 65 74 56 61 72 69 6e 74 33 32 28 70 4e 65 78 74  etVarint32(pNext
44fd2 2c 20 26 70 52 65 61 64 65 72 2d 3e 6e 44 6f 63  , &pReader->nDoc
44fd3 6c 69 73 74 29 3b 0a 20 20 70 52 65 61 64 65 72  list);.  pReader
44fd4 2d 3e 61 44 6f 63 6c 69 73 74 20 3d 20 70 4e 65  ->aDoclist = pNe
44fd5 78 74 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70  xt;.  pReader->p
44fd6 4f 66 66 73 65 74 4c 69 73 74 20 3d 20 30 3b 0a  OffsetList = 0;.
44fd7 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74  .  /* Check that
44fd8 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64 6f 65   the doclist doe
44fd9 73 20 6e 6f 74 20 61 70 70 65 61 72 20 74 6f 20  s not appear to 
44fda 65 78 74 65 6e 64 20 70 61 73 74 20 74 68 65 20  extend past the 
44fdb 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  end of the.  ** 
44fdc 62 2d 74 72 65 65 20 6e 6f 64 65 2e 20 41 6e 64  b-tree node. And
44fdd 20 74 68 61 74 20 74 68 65 20 66 69 6e 61 6c 20   that the final 
44fde 62 79 74 65 20 6f 66 20 74 68 65 20 64 6f 63 6c  byte of the docl
44fdf 69 73 74 20 69 73 20 30 78 30 30 2e 20 49 66 20  ist is 0x00. If 
44fe0 65 69 74 68 65 72 20 0a 20 20 2a 2a 20 6f 66 20  either .  ** of 
44fe1 74 68 65 73 65 20 73 74 61 74 65 6d 65 6e 74 73  these statements
44fe2 20 69 73 20 75 6e 74 72 75 65 2c 20 74 68 65 6e   is untrue, then
44fe3 20 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74   the data struct
44fe4 75 72 65 20 69 73 20 63 6f 72 72 75 70 74 2e 0a  ure is corrupt..
44fe5 20 20 2a 2f 0a 20 20 69 66 28 20 26 70 52 65 61    */.  if( &pRea
44fe6 64 65 72 2d 3e 61 44 6f 63 6c 69 73 74 5b 70 52  der->aDoclist[pR
44fe7 65 61 64 65 72 2d 3e 6e 44 6f 63 6c 69 73 74 5d  eader->nDoclist]
44fe8 3e 26 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65  >&pReader->aNode
44fe9 5b 70 52 65 61 64 65 72 2d 3e 6e 4e 6f 64 65 5d  [pReader->nNode]
44fea 20 0a 20 20 20 7c 7c 20 28 70 52 65 61 64 65 72   .   || (pReader
44feb 2d 3e 6e 50 6f 70 75 6c 61 74 65 3d 3d 30 20 26  ->nPopulate==0 &
44fec 26 20 70 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c  & pReader->aDocl
44fed 69 73 74 5b 70 52 65 61 64 65 72 2d 3e 6e 44 6f  ist[pReader->nDo
44fee 63 6c 69 73 74 2d 31 5d 29 0a 20 20 29 7b 0a 20  clist-1]).  ){. 
44fef 20 20 20 72 65 74 75 72 6e 20 46 54 53 5f 43 4f     return FTS_CO
44ff0 52 52 55 50 54 5f 56 54 41 42 3b 0a 20 20 7d 0a  RRUPT_VTAB;.  }.
44ff1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
44ff2 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  OK;.}../*.** Set
44ff3 20 74 68 65 20 53 65 67 52 65 61 64 65 72 20 74   the SegReader t
44ff4 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66  o point to the f
44ff5 69 72 73 74 20 64 6f 63 69 64 20 69 6e 20 74 68  irst docid in th
44ff6 65 20 64 6f 63 6c 69 73 74 20 61 73 73 6f 63 69  e doclist associ
44ff7 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65  ated.** with the
44ff8 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 0a 2a   current term..*
44ff9 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
44ffa 33 53 65 67 52 65 61 64 65 72 46 69 72 73 74 44  3SegReaderFirstD
44ffb 6f 63 69 64 28 46 74 73 33 54 61 62 6c 65 20 2a  ocid(Fts3Table *
44ffc 70 54 61 62 2c 20 46 74 73 33 53 65 67 52 65 61  pTab, Fts3SegRea
44ffd 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
44ffe 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
44fff 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70  _OK;.  assert( p
45000 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c 69 73 74  Reader->aDoclist
45001 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70   );.  assert( !p
45002 52 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c  Reader->pOffsetL
45003 69 73 74 20 29 3b 0a 20 20 69 66 28 20 70 54 61  ist );.  if( pTa
45004 62 2d 3e 62 44 65 73 63 49 64 78 20 26 26 20 66  b->bDescIdx && f
45005 74 73 33 53 65 67 52 65 61 64 65 72 49 73 50 65  ts3SegReaderIsPe
45006 6e 64 69 6e 67 28 70 52 65 61 64 65 72 29 20 29  nding(pReader) )
45007 7b 0a 20 20 20 20 75 38 20 62 45 6f 66 20 3d 20  {.    u8 bEof = 
45008 30 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  0;.    pReader->
45009 69 44 6f 63 69 64 20 3d 20 30 3b 0a 20 20 20 20  iDocid = 0;.    
4500a 70 52 65 61 64 65 72 2d 3e 6e 4f 66 66 73 65 74  pReader->nOffset
4500b 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 73 71  List = 0;.    sq
4500c 6c 69 74 65 33 46 74 73 33 44 6f 63 6c 69 73 74  lite3Fts3Doclist
4500d 50 72 65 76 28 30 2c 0a 20 20 20 20 20 20 20 20  Prev(0,.        
4500e 70 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c 69 73  pReader->aDoclis
4500f 74 2c 20 70 52 65 61 64 65 72 2d 3e 6e 44 6f 63  t, pReader->nDoc
45010 6c 69 73 74 2c 20 26 70 52 65 61 64 65 72 2d 3e  list, &pReader->
45011 70 4f 66 66 73 65 74 4c 69 73 74 2c 20 0a 20 20  pOffsetList, .  
45012 20 20 20 20 20 20 26 70 52 65 61 64 65 72 2d 3e        &pReader->
45013 69 44 6f 63 69 64 2c 20 26 70 52 65 61 64 65 72  iDocid, &pReader
45014 2d 3e 6e 4f 66 66 73 65 74 4c 69 73 74 2c 20 26  ->nOffsetList, &
45015 62 45 6f 66 0a 20 20 20 20 29 3b 0a 20 20 7d 65  bEof.    );.  }e
45016 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 66 74  lse{.    rc = ft
45017 73 33 53 65 67 52 65 61 64 65 72 52 65 71 75 69  s3SegReaderRequi
45018 72 65 28 70 52 65 61 64 65 72 2c 20 70 52 65 61  re(pReader, pRea
45019 64 65 72 2d 3e 61 44 6f 63 6c 69 73 74 2c 20 46  der->aDoclist, F
4501a 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58 29 3b  TS3_VARINT_MAX);
4501b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
4501c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
4501d 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 46  int n = sqlite3F
4501e 74 73 33 47 65 74 56 61 72 69 6e 74 28 70 52 65  ts3GetVarint(pRe
4501f 61 64 65 72 2d 3e 61 44 6f 63 6c 69 73 74 2c 20  ader->aDoclist, 
45020 26 70 52 65 61 64 65 72 2d 3e 69 44 6f 63 69 64  &pReader->iDocid
45021 29 3b 0a 20 20 20 20 20 20 70 52 65 61 64 65 72  );.      pReader
45022 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 20 3d 20  ->pOffsetList = 
45023 26 70 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c 69  &pReader->aDocli
45024 73 74 5b 6e 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d  st[n];.    }.  }
45025 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
45026 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74  ./*.** Advance t
45027 68 65 20 53 65 67 52 65 61 64 65 72 20 74 6f 20  he SegReader to 
45028 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 78  point to the nex
45029 74 20 64 6f 63 69 64 20 69 6e 20 74 68 65 20 64  t docid in the d
4502a 6f 63 6c 69 73 74 0a 2a 2a 20 61 73 73 6f 63 69  oclist.** associ
4502b 61 74 65 64 20 77 69 74 68 20 74 68 65 20 63 75  ated with the cu
4502c 72 72 65 6e 74 20 74 65 72 6d 2e 0a 2a 2a 20 0a  rrent term..** .
4502d 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 73 20  ** If arguments 
4502e 70 70 4f 66 66 73 65 74 4c 69 73 74 20 61 6e 64  ppOffsetList and
4502f 20 70 6e 4f 66 66 73 65 74 4c 69 73 74 20 61 72   pnOffsetList ar
45030 65 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  e not NULL, then
45031 20 0a 2a 2a 20 2a 70 70 4f 66 66 73 65 74 4c 69   .** *ppOffsetLi
45032 73 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69  st is set to poi
45033 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  nt to the first 
45034 63 6f 6c 75 6d 6e 2d 6f 66 66 73 65 74 20 6c 69  column-offset li
45035 73 74 0a 2a 2a 20 69 6e 20 74 68 65 20 64 6f 63  st.** in the doc
45036 6c 69 73 74 20 65 6e 74 72 79 20 28 69 2e 65 2e  list entry (i.e.
45037 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 70 61 73   immediately pas
45038 74 20 74 68 65 20 64 6f 63 69 64 20 76 61 72 69  t the docid vari
45039 6e 74 29 2e 0a 2a 2a 20 2a 70 6e 4f 66 66 73 65  nt)..** *pnOffse
4503a 74 4c 69 73 74 20 69 73 20 73 65 74 20 74 6f 20  tList is set to 
4503b 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  the length of th
4503c 65 20 73 65 74 20 6f 66 20 63 6f 6c 75 6d 6e 2d  e set of column-
4503d 6f 66 66 73 65 74 0a 2a 2a 20 6c 69 73 74 73 2c  offset.** lists,
4503e 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20 74   not including t
4503f 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f  he nul-terminato
45040 72 20 62 79 74 65 2e 20 46 6f 72 20 65 78 61 6d  r byte. For exam
45041 70 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ple:.*/.static i
45042 6e 74 20 66 74 73 33 53 65 67 52 65 61 64 65 72  nt fts3SegReader
45043 4e 65 78 74 44 6f 63 69 64 28 0a 20 20 46 74 73  NextDocid(.  Fts
45044 33 54 61 62 6c 65 20 2a 70 54 61 62 2c 0a 20 20  3Table *pTab,.  
45045 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 70  Fts3SegReader *p
45046 52 65 61 64 65 72 2c 20 20 20 20 20 20 20 20 20  Reader,         
45047 2f 2a 20 52 65 61 64 65 72 20 74 6f 20 61 64 76  /* Reader to adv
45048 61 6e 63 65 20 74 6f 20 6e 65 78 74 20 64 6f 63  ance to next doc
45049 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70  id */.  char **p
4504a 70 4f 66 66 73 65 74 4c 69 73 74 2c 20 20 20 20  pOffsetList,    
4504b 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
4504c 50 6f 69 6e 74 65 72 20 74 6f 20 63 75 72 72 65  Pointer to curre
4504d 6e 74 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74  nt position-list
4504e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4f 66 66   */.  int *pnOff
4504f 73 65 74 4c 69 73 74 20 20 20 20 20 20 20 20 20  setList         
45050 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4c 65        /* OUT: Le
45051 6e 67 74 68 20 6f 66 20 2a 70 70 4f 66 66 73 65  ngth of *ppOffse
45052 74 4c 69 73 74 20 69 6e 20 62 79 74 65 73 20 2a  tList in bytes *
45053 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  /.){.  int rc = 
45054 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 68 61  SQLITE_OK;.  cha
45055 72 20 2a 70 20 3d 20 70 52 65 61 64 65 72 2d 3e  r *p = pReader->
45056 70 4f 66 66 73 65 74 4c 69 73 74 3b 0a 20 20 63  pOffsetList;.  c
45057 68 61 72 20 63 20 3d 20 30 3b 0a 0a 20 20 61 73  har c = 0;..  as
45058 73 65 72 74 28 20 70 20 29 3b 0a 0a 20 20 69 66  sert( p );..  if
45059 28 20 70 54 61 62 2d 3e 62 44 65 73 63 49 64 78  ( pTab->bDescIdx
4505a 20 26 26 20 66 74 73 33 53 65 67 52 65 61 64 65   && fts3SegReade
4505b 72 49 73 50 65 6e 64 69 6e 67 28 70 52 65 61 64  rIsPending(pRead
4505c 65 72 29 20 29 7b 0a 20 20 20 20 2f 2a 20 41 20  er) ){.    /* A 
4505d 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 73 65  pending-terms se
4505e 67 2d 72 65 61 64 65 72 20 66 6f 72 20 61 6e 20  g-reader for an 
4505f 46 54 53 34 20 74 61 62 6c 65 20 74 68 61 74 20  FTS4 table that 
45060 75 73 65 73 20 6f 72 64 65 72 3d 64 65 73 63 2e  uses order=desc.
45061 0a 20 20 20 20 2a 2a 20 50 65 6e 64 69 6e 67 2d  .    ** Pending-
45062 74 65 72 6d 73 20 64 6f 63 6c 69 73 74 73 20 61  terms doclists a
45063 72 65 20 61 6c 77 61 79 73 20 62 75 69 6c 74 20  re always built 
45064 75 70 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20  up in ascending 
45065 6f 72 64 65 72 2c 20 73 6f 0a 20 20 20 20 2a 2a  order, so.    **
45066 20 77 65 20 68 61 76 65 20 74 6f 20 69 74 65 72   we have to iter
45067 61 74 65 20 74 68 72 6f 75 67 68 20 74 68 65 6d  ate through them
45068 20 62 61 63 6b 77 61 72 64 73 20 68 65 72 65 2e   backwards here.
45069 20 2a 2f 0a 20 20 20 20 75 38 20 62 45 6f 66 20   */.    u8 bEof 
4506a 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 70 4f  = 0;.    if( ppO
4506b 66 66 73 65 74 4c 69 73 74 20 29 7b 0a 20 20 20  ffsetList ){.   
4506c 20 20 20 2a 70 70 4f 66 66 73 65 74 4c 69 73 74     *ppOffsetList
4506d 20 3d 20 70 52 65 61 64 65 72 2d 3e 70 4f 66 66   = pReader->pOff
4506e 73 65 74 4c 69 73 74 3b 0a 20 20 20 20 20 20 2a  setList;.      *
4506f 70 6e 4f 66 66 73 65 74 4c 69 73 74 20 3d 20 70  pnOffsetList = p
45070 52 65 61 64 65 72 2d 3e 6e 4f 66 66 73 65 74 4c  Reader->nOffsetL
45071 69 73 74 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20  ist - 1;.    }. 
45072 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 44 6f     sqlite3Fts3Do
45073 63 6c 69 73 74 50 72 65 76 28 30 2c 0a 20 20 20  clistPrev(0,.   
45074 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e 61 44       pReader->aD
45075 6f 63 6c 69 73 74 2c 20 70 52 65 61 64 65 72 2d  oclist, pReader-
45076 3e 6e 44 6f 63 6c 69 73 74 2c 20 26 70 2c 20 26  >nDoclist, &p, &
45077 70 52 65 61 64 65 72 2d 3e 69 44 6f 63 69 64 2c  pReader->iDocid,
45078 0a 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65  .        &pReade
45079 72 2d 3e 6e 4f 66 66 73 65 74 4c 69 73 74 2c 20  r->nOffsetList, 
4507a 26 62 45 6f 66 0a 20 20 20 20 29 3b 0a 20 20 20  &bEof.    );.   
4507b 20 69 66 28 20 62 45 6f 66 20 29 7b 0a 20 20 20   if( bEof ){.   
4507c 20 20 20 70 52 65 61 64 65 72 2d 3e 70 4f 66 66     pReader->pOff
4507d 73 65 74 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20  setList = 0;.   
4507e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 52   }else{.      pR
4507f 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c 69  eader->pOffsetLi
45080 73 74 20 3d 20 70 3b 0a 20 20 20 20 7d 0a 20 20  st = p;.    }.  
45081 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20  }else{.    char 
45082 2a 70 45 6e 64 20 3d 20 26 70 52 65 61 64 65 72  *pEnd = &pReader
45083 2d 3e 61 44 6f 63 6c 69 73 74 5b 70 52 65 61 64  ->aDoclist[pRead
45084 65 72 2d 3e 6e 44 6f 63 6c 69 73 74 5d 3b 0a 0a  er->nDoclist];..
45085 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 70      /* Pointer p
45086 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
45087 73 20 61 74 20 74 68 65 20 66 69 72 73 74 20 62  s at the first b
45088 79 74 65 20 6f 66 20 61 6e 20 6f 66 66 73 65 74  yte of an offset
45089 20 6c 69 73 74 2e 20 54 68 65 0a 20 20 20 20 2a   list. The.    *
4508a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  * following bloc
4508b 6b 20 61 64 76 61 6e 63 65 73 20 69 74 20 74 6f  k advances it to
4508c 20 70 6f 69 6e 74 20 6f 6e 65 20 62 79 74 65 20   point one byte 
4508d 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 0a  past the end of.
4508e 20 20 20 20 2a 2a 20 74 68 65 20 73 61 6d 65 20      ** the same 
4508f 6f 66 66 73 65 74 20 6c 69 73 74 2e 20 2a 2f 0a  offset list. */.
45090 20 20 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a      while( 1 ){.
45091 20 20 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20    .      /* The 
45092 66 6f 6c 6c 6f 77 69 6e 67 20 6c 69 6e 65 20 6f  following line o
45093 66 20 63 6f 64 65 20 28 61 6e 64 20 74 68 65 20  f code (and the 
45094 22 70 2b 2b 22 20 62 65 6c 6f 77 20 74 68 65 20  "p++" below the 
45095 77 68 69 6c 65 28 29 20 6c 6f 6f 70 29 20 69 73  while() loop) is
45096 0a 20 20 20 20 20 20 2a 2a 20 6e 6f 72 6d 61 6c  .      ** normal
45097 6c 79 20 61 6c 6c 20 74 68 61 74 20 69 73 20 72  ly all that is r
45098 65 71 75 69 72 65 64 20 74 6f 20 6d 6f 76 65 20  equired to move 
45099 70 6f 69 6e 74 65 72 20 70 20 74 6f 20 74 68 65  pointer p to the
4509a 20 64 65 73 69 72 65 64 20 0a 20 20 20 20 20 20   desired .      
4509b 2a 2a 20 70 6f 73 69 74 69 6f 6e 2e 20 54 68 65  ** position. The
4509c 20 65 78 63 65 70 74 69 6f 6e 20 69 73 20 69 66   exception is if
4509d 20 74 68 69 73 20 6e 6f 64 65 20 69 73 20 62 65   this node is be
4509e 69 6e 67 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20  ing loaded from 
4509f 64 69 73 6b 0a 20 20 20 20 20 20 2a 2a 20 69 6e  disk.      ** in
450a0 63 72 65 6d 65 6e 74 61 6c 6c 79 20 61 6e 64 20  crementally and 
450a1 70 6f 69 6e 74 65 72 20 22 70 22 20 6e 6f 77 20  pointer "p" now 
450a2 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69  points to the fi
450a3 72 73 74 20 62 79 74 65 20 70 61 73 73 65 64 0a  rst byte passed.
450a4 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 6f 70        ** the pop
450a5 75 6c 61 74 65 64 20 70 61 72 74 20 6f 66 20 70  ulated part of p
450a6 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 5b 5d 2e  Reader->aNode[].
450a7 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
450a8 77 68 69 6c 65 28 20 2a 70 20 7c 20 63 20 29 20  while( *p | c ) 
450a9 63 20 3d 20 2a 70 2b 2b 20 26 20 30 78 38 30 3b  c = *p++ & 0x80;
450aa 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a  .      assert( *
450ab 70 3d 3d 30 20 29 3b 0a 20 20 0a 20 20 20 20 20  p==0 );.  .     
450ac 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 70 42   if( pReader->pB
450ad 6c 6f 62 3d 3d 30 20 7c 7c 20 70 3c 26 70 52 65  lob==0 || p<&pRe
450ae 61 64 65 72 2d 3e 61 4e 6f 64 65 5b 70 52 65 61  ader->aNode[pRea
450af 64 65 72 2d 3e 6e 50 6f 70 75 6c 61 74 65 5d 20  der->nPopulate] 
450b0 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 72  ) break;.      r
450b1 63 20 3d 20 66 74 73 33 53 65 67 52 65 61 64 65  c = fts3SegReade
450b2 72 49 6e 63 72 52 65 61 64 28 70 52 65 61 64 65  rIncrRead(pReade
450b3 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  r);.      if( rc
450b4 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
450b5 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
450b6 20 20 20 70 2b 2b 3b 0a 20 20 0a 20 20 20 20 2f     p++;.  .    /
450b7 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c 20 70  * If required, p
450b8 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 75 74 70  opulate the outp
450b9 75 74 20 76 61 72 69 61 62 6c 65 73 20 77 69 74  ut variables wit
450ba 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  h a pointer to a
450bb 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 69  nd the.    ** si
450bc 7a 65 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f  ze of the previo
450bd 75 73 20 6f 66 66 73 65 74 2d 6c 69 73 74 2e 0a  us offset-list..
450be 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
450bf 70 4f 66 66 73 65 74 4c 69 73 74 20 29 7b 0a 20  pOffsetList ){. 
450c0 20 20 20 20 20 2a 70 70 4f 66 66 73 65 74 4c 69       *ppOffsetLi
450c1 73 74 20 3d 20 70 52 65 61 64 65 72 2d 3e 70 4f  st = pReader->pO
450c2 66 66 73 65 74 4c 69 73 74 3b 0a 20 20 20 20 20  ffsetList;.     
450c3 20 2a 70 6e 4f 66 66 73 65 74 4c 69 73 74 20 3d   *pnOffsetList =
450c4 20 28 69 6e 74 29 28 70 20 2d 20 70 52 65 61 64   (int)(p - pRead
450c5 65 72 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 20  er->pOffsetList 
450c6 2d 20 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  - 1);.    }..   
450c7 20 77 68 69 6c 65 28 20 70 3c 70 45 6e 64 20 26   while( p<pEnd &
450c8 26 20 2a 70 3d 3d 30 20 29 20 70 2b 2b 3b 0a 20  & *p==0 ) p++;. 
450c9 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72   .    /* If ther
450ca 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 65 6e  e are no more en
450cb 74 72 69 65 73 20 69 6e 20 74 68 65 20 64 6f 63  tries in the doc
450cc 6c 69 73 74 2c 20 73 65 74 20 70 4f 66 66 73 65  list, set pOffse
450cd 74 4c 69 73 74 20 74 6f 0a 20 20 20 20 2a 2a 20  tList to.    ** 
450ce 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65 2c  NULL. Otherwise,
450cf 20 73 65 74 20 46 74 73 33 53 65 67 52 65 61 64   set Fts3SegRead
450d0 65 72 2e 69 44 6f 63 69 64 20 74 6f 20 74 68 65  er.iDocid to the
450d1 20 6e 65 78 74 20 64 6f 63 69 64 20 61 6e 64 0a   next docid and.
450d2 20 20 20 20 2a 2a 20 46 74 73 33 53 65 67 52 65      ** Fts3SegRe
450d3 61 64 65 72 2e 70 4f 66 66 73 65 74 4c 69 73 74  ader.pOffsetList
450d4 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
450d5 20 6e 65 78 74 20 6f 66 66 73 65 74 20 6c 69 73   next offset lis
450d6 74 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20  t before.    ** 
450d7 72 65 74 75 72 6e 69 6e 67 2e 0a 20 20 20 20 2a  returning..    *
450d8 2f 0a 20 20 20 20 69 66 28 20 70 3e 3d 70 45 6e  /.    if( p>=pEn
450d9 64 20 29 7b 0a 20 20 20 20 20 20 70 52 65 61 64  d ){.      pRead
450da 65 72 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 20  er->pOffsetList 
450db 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 0;.    }else{.
450dc 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 53        rc = fts3S
450dd 65 67 52 65 61 64 65 72 52 65 71 75 69 72 65 28  egReaderRequire(
450de 70 52 65 61 64 65 72 2c 20 70 2c 20 46 54 53 33  pReader, p, FTS3
450df 5f 56 41 52 49 4e 54 5f 4d 41 58 29 3b 0a 20 20  _VARINT_MAX);.  
450e0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
450e1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
450e2 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
450e3 44 65 6c 74 61 3b 0a 20 20 20 20 20 20 20 20 70  Delta;.        p
450e4 52 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c  Reader->pOffsetL
450e5 69 73 74 20 3d 20 70 20 2b 20 73 71 6c 69 74 65  ist = p + sqlite
450e6 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 28 70  3Fts3GetVarint(p
450e7 2c 20 26 69 44 65 6c 74 61 29 3b 0a 20 20 20 20  , &iDelta);.    
450e8 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 62 44      if( pTab->bD
450e9 65 73 63 49 64 78 20 29 7b 0a 20 20 20 20 20 20  escIdx ){.      
450ea 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69 44 6f      pReader->iDo
450eb 63 69 64 20 2d 3d 20 69 44 65 6c 74 61 3b 0a 20  cid -= iDelta;. 
450ec 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
450ed 20 20 20 20 20 20 20 20 70 52 65 61 64 65 72 2d          pReader-
450ee 3e 69 44 6f 63 69 64 20 2b 3d 20 69 44 65 6c 74  >iDocid += iDelt
450ef 61 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  a;.        }.   
450f0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
450f1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
450f2 4f 4b 3b 0a 7d 0a 0a 0a 53 51 4c 49 54 45 5f 50  OK;.}...SQLITE_P
450f3 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
450f4 65 33 46 74 73 33 4d 73 72 4f 76 66 6c 28 0a 20  e3Fts3MsrOvfl(. 
450f5 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73   Fts3Cursor *pCs
450f6 72 2c 20 0a 20 20 46 74 73 33 4d 75 6c 74 69 53  r, .  Fts3MultiS
450f7 65 67 52 65 61 64 65 72 20 2a 70 4d 73 72 2c 0a  egReader *pMsr,.
450f8 20 20 69 6e 74 20 2a 70 6e 4f 76 66 6c 0a 29 7b    int *pnOvfl.){
450f9 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 20  .  Fts3Table *p 
450fa 3d 20 28 46 74 73 33 54 61 62 6c 65 2a 29 70 43  = (Fts3Table*)pC
450fb 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b 0a  sr->base.pVtab;.
450fc 20 20 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 30 3b    int nOvfl = 0;
450fd 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
450fe 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
450ff 0a 20 20 69 6e 74 20 70 67 73 7a 20 3d 20 70 2d  .  int pgsz = p-
45100 3e 6e 50 67 73 7a 3b 0a 0a 20 20 61 73 73 65 72  >nPgsz;..  asser
45101 74 28 20 70 2d 3e 62 46 74 73 34 20 29 3b 0a 20  t( p->bFts4 );. 
45102 20 61 73 73 65 72 74 28 20 70 67 73 7a 3e 30 20   assert( pgsz>0 
45103 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  );..  for(ii=0; 
45104 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
45105 20 69 69 3c 70 4d 73 72 2d 3e 6e 53 65 67 6d 65   ii<pMsr->nSegme
45106 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 46  nt; ii++){.    F
45107 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 70 52  ts3SegReader *pR
45108 65 61 64 65 72 20 3d 20 70 4d 73 72 2d 3e 61 70  eader = pMsr->ap
45109 53 65 67 6d 65 6e 74 5b 69 69 5d 3b 0a 20 20 20  Segment[ii];.   
4510a 20 69 66 28 20 21 66 74 73 33 53 65 67 52 65 61   if( !fts3SegRea
4510b 64 65 72 49 73 50 65 6e 64 69 6e 67 28 70 52 65  derIsPending(pRe
4510c 61 64 65 72 29 20 0a 20 20 20 20 20 26 26 20 21  ader) .     && !
4510d 66 74 73 33 53 65 67 52 65 61 64 65 72 49 73 52  fts3SegReaderIsR
4510e 6f 6f 74 4f 6e 6c 79 28 70 52 65 61 64 65 72 29  ootOnly(pReader)
4510f 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 73   .    ){.      s
45110 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6a 6a 3b  qlite3_int64 jj;
45111 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 70 52  .      for(jj=pR
45112 65 61 64 65 72 2d 3e 69 53 74 61 72 74 42 6c 6f  eader->iStartBlo
45113 63 6b 3b 20 6a 6a 3c 3d 70 52 65 61 64 65 72 2d  ck; jj<=pReader-
45114 3e 69 4c 65 61 66 45 6e 64 42 6c 6f 63 6b 3b 20  >iLeafEndBlock; 
45115 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  jj++){.        i
45116 6e 74 20 6e 42 6c 6f 62 3b 0a 20 20 20 20 20 20  nt nBlob;.      
45117 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74    rc = sqlite3Ft
45118 73 33 52 65 61 64 42 6c 6f 63 6b 28 70 2c 20 6a  s3ReadBlock(p, j
45119 6a 2c 20 30 2c 20 26 6e 42 6c 6f 62 2c 20 30 29  j, 0, &nBlob, 0)
4511a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
4511b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72  !=SQLITE_OK ) br
4511c 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 28  eak;.        if(
4511d 20 28 6e 42 6c 6f 62 2b 33 35 29 3e 70 67 73 7a   (nBlob+35)>pgsz
4511e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 4f   ){.          nO
4511f 76 66 6c 20 2b 3d 20 28 6e 42 6c 6f 62 20 2b 20  vfl += (nBlob + 
45120 33 34 29 2f 70 67 73 7a 3b 0a 20 20 20 20 20 20  34)/pgsz;.      
45121 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
45122 7d 0a 20 20 7d 0a 20 20 2a 70 6e 4f 76 66 6c 20  }.  }.  *pnOvfl 
45123 3d 20 6e 4f 76 66 6c 3b 0a 20 20 72 65 74 75 72  = nOvfl;.  retur
45124 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  n rc;.}../*.** F
45125 72 65 65 20 61 6c 6c 20 61 6c 6c 6f 63 61 74 69  ree all allocati
45126 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ons associated w
45127 69 74 68 20 74 68 65 20 69 74 65 72 61 74 6f 72  ith the iterator
45128 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 0a   passed as the .
45129 2a 2a 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  ** second argume
4512a 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
4512b 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
4512c 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72 46  e3Fts3SegReaderF
4512d 72 65 65 28 46 74 73 33 53 65 67 52 65 61 64 65  ree(Fts3SegReade
4512e 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69  r *pReader){.  i
4512f 66 28 20 70 52 65 61 64 65 72 20 26 26 20 21 66  f( pReader && !f
45130 74 73 33 53 65 67 52 65 61 64 65 72 49 73 50 65  ts3SegReaderIsPe
45131 6e 64 69 6e 67 28 70 52 65 61 64 65 72 29 20 29  nding(pReader) )
45132 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
45133 65 65 28 70 52 65 61 64 65 72 2d 3e 7a 54 65 72  ee(pReader->zTer
45134 6d 29 3b 0a 20 20 20 20 69 66 28 20 21 66 74 73  m);.    if( !fts
45135 33 53 65 67 52 65 61 64 65 72 49 73 52 6f 6f 74  3SegReaderIsRoot
45136 4f 6e 6c 79 28 70 52 65 61 64 65 72 29 20 29 7b  Only(pReader) ){
45137 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
45138 72 65 65 28 70 52 65 61 64 65 72 2d 3e 61 4e 6f  ree(pReader->aNo
45139 64 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  de);.      sqlit
4513a 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 70 52  e3_blob_close(pR
4513b 65 61 64 65 72 2d 3e 70 42 6c 6f 62 29 3b 0a 20  eader->pBlob);. 
4513c 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
4513d 65 33 5f 66 72 65 65 28 70 52 65 61 64 65 72 29  e3_free(pReader)
4513e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  ;.}../*.** Alloc
4513f 61 74 65 20 61 20 6e 65 77 20 53 65 67 52 65 61  ate a new SegRea
45140 64 65 72 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53  der object..*/.S
45141 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
45142 74 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67  t sqlite3Fts3Seg
45143 52 65 61 64 65 72 4e 65 77 28 0a 20 20 69 6e 74  ReaderNew(.  int
45144 20 69 41 67 65 2c 20 20 20 20 20 20 20 20 20 20   iAge,          
45145 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
45146 53 65 67 6d 65 6e 74 20 22 61 67 65 22 2e 20 2a  Segment "age". *
45147 2f 0a 20 20 69 6e 74 20 62 4c 6f 6f 6b 75 70 2c  /.  int bLookup,
45148 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45149 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
4514a 61 20 6c 6f 6f 6b 75 70 20 6f 6e 6c 79 20 2a 2f  a lookup only */
4514b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
4514c 20 69 53 74 61 72 74 4c 65 61 66 2c 20 20 20 20   iStartLeaf,    
4514d 20 20 20 2f 2a 20 46 69 72 73 74 20 6c 65 61 66     /* First leaf
4514e 20 74 6f 20 74 72 61 76 65 72 73 65 20 2a 2f 0a   to traverse */.
4514f 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
45150 69 45 6e 64 4c 65 61 66 2c 20 20 20 20 20 20 20  iEndLeaf,       
45151 20 20 2f 2a 20 46 69 6e 61 6c 20 6c 65 61 66 20    /* Final leaf 
45152 74 6f 20 74 72 61 76 65 72 73 65 20 2a 2f 0a 20  to traverse */. 
45153 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
45154 45 6e 64 42 6c 6f 63 6b 2c 20 20 20 20 20 20 20  EndBlock,       
45155 20 2f 2a 20 46 69 6e 61 6c 20 62 6c 6f 63 6b 20   /* Final block 
45156 6f 66 20 73 65 67 6d 65 6e 74 20 2a 2f 0a 20 20  of segment */.  
45157 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 6f 6f  const char *zRoo
45158 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
45159 2f 2a 20 42 75 66 66 65 72 20 63 6f 6e 74 61 69  /* Buffer contai
4515a 6e 69 6e 67 20 72 6f 6f 74 20 6e 6f 64 65 20 2a  ning root node *
4515b 2f 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 20  /.  int nRoot,  
4515c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4515d 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62      /* Size of b
4515e 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  uffer containing
4515f 20 72 6f 6f 74 20 6e 6f 64 65 20 2a 2f 0a 20 20   root node */.  
45160 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a  Fts3SegReader **
45161 70 70 52 65 61 64 65 72 20 20 20 20 20 20 20 20  ppReader        
45162 2f 2a 20 4f 55 54 3a 20 41 6c 6c 6f 63 61 74 65  /* OUT: Allocate
45163 64 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20  d Fts3SegReader 
45164 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 53 65 67 52  */.){.  Fts3SegR
45165 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 3b 20  eader *pReader; 
45166 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 6c 79          /* Newly
45167 20 61 6c 6c 6f 63 61 74 65 64 20 53 65 67 52 65   allocated SegRe
45168 61 64 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  ader object */. 
45169 20 69 6e 74 20 6e 45 78 74 72 61 20 3d 20 30 3b   int nExtra = 0;
4516a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4516b 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c   /* Bytes to all
4516c 6f 63 61 74 65 20 73 65 67 6d 65 6e 74 20 72 6f  ocate segment ro
4516d 6f 74 20 6e 6f 64 65 20 2a 2f 0a 0a 20 20 61 73  ot node */..  as
4516e 73 65 72 74 28 20 69 53 74 61 72 74 4c 65 61 66  sert( iStartLeaf
4516f 3c 3d 69 45 6e 64 4c 65 61 66 20 29 3b 0a 20 20  <=iEndLeaf );.  
45170 69 66 28 20 69 53 74 61 72 74 4c 65 61 66 3d 3d  if( iStartLeaf==
45171 30 20 29 7b 0a 20 20 20 20 6e 45 78 74 72 61 20  0 ){.    nExtra 
45172 3d 20 6e 52 6f 6f 74 20 2b 20 46 54 53 33 5f 4e  = nRoot + FTS3_N
45173 4f 44 45 5f 50 41 44 44 49 4e 47 3b 0a 20 20 7d  ODE_PADDING;.  }
45174 0a 0a 20 20 70 52 65 61 64 65 72 20 3d 20 28 46  ..  pReader = (F
45175 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 29 73  ts3SegReader *)s
45176 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
45177 7a 65 6f 66 28 46 74 73 33 53 65 67 52 65 61 64  zeof(Fts3SegRead
45178 65 72 29 20 2b 20 6e 45 78 74 72 61 29 3b 0a 20  er) + nExtra);. 
45179 20 69 66 28 20 21 70 52 65 61 64 65 72 20 29 7b   if( !pReader ){
4517a 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
4517b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
4517c 6d 65 6d 73 65 74 28 70 52 65 61 64 65 72 2c 20  memset(pReader, 
4517d 30 2c 20 73 69 7a 65 6f 66 28 46 74 73 33 53 65  0, sizeof(Fts3Se
4517e 67 52 65 61 64 65 72 29 29 3b 0a 20 20 70 52 65  gReader));.  pRe
4517f 61 64 65 72 2d 3e 69 49 64 78 20 3d 20 69 41 67  ader->iIdx = iAg
45180 65 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 62 4c  e;.  pReader->bL
45181 6f 6f 6b 75 70 20 3d 20 62 4c 6f 6f 6b 75 70 21  ookup = bLookup!
45182 3d 30 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69  =0;.  pReader->i
45183 53 74 61 72 74 42 6c 6f 63 6b 20 3d 20 69 53 74  StartBlock = iSt
45184 61 72 74 4c 65 61 66 3b 0a 20 20 70 52 65 61 64  artLeaf;.  pRead
45185 65 72 2d 3e 69 4c 65 61 66 45 6e 64 42 6c 6f 63  er->iLeafEndBloc
45186 6b 20 3d 20 69 45 6e 64 4c 65 61 66 3b 0a 20 20  k = iEndLeaf;.  
45187 70 52 65 61 64 65 72 2d 3e 69 45 6e 64 42 6c 6f  pReader->iEndBlo
45188 63 6b 20 3d 20 69 45 6e 64 42 6c 6f 63 6b 3b 0a  ck = iEndBlock;.
45189 0a 20 20 69 66 28 20 6e 45 78 74 72 61 20 29 7b  .  if( nExtra ){
4518a 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 6e 74 69  .    /* The enti
4518b 72 65 20 73 65 67 6d 65 6e 74 20 69 73 20 73 74  re segment is st
4518c 6f 72 65 64 20 69 6e 20 74 68 65 20 72 6f 6f 74  ored in the root
4518d 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 70 52   node. */.    pR
4518e 65 61 64 65 72 2d 3e 61 4e 6f 64 65 20 3d 20 28  eader->aNode = (
4518f 63 68 61 72 20 2a 29 26 70 52 65 61 64 65 72 5b  char *)&pReader[
45190 31 5d 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d  1];.    pReader-
45191 3e 72 6f 6f 74 4f 6e 6c 79 20 3d 20 31 3b 0a 20  >rootOnly = 1;. 
45192 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 4e 6f 64     pReader->nNod
45193 65 20 3d 20 6e 52 6f 6f 74 3b 0a 20 20 20 20 6d  e = nRoot;.    m
45194 65 6d 63 70 79 28 70 52 65 61 64 65 72 2d 3e 61  emcpy(pReader->a
45195 4e 6f 64 65 2c 20 7a 52 6f 6f 74 2c 20 6e 52 6f  Node, zRoot, nRo
45196 6f 74 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  ot);.    memset(
45197 26 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 5b  &pReader->aNode[
45198 6e 52 6f 6f 74 5d 2c 20 30 2c 20 46 54 53 33 5f  nRoot], 0, FTS3_
45199 4e 4f 44 45 5f 50 41 44 44 49 4e 47 29 3b 0a 20  NODE_PADDING);. 
4519a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 52 65 61   }else{.    pRea
4519b 64 65 72 2d 3e 69 43 75 72 72 65 6e 74 42 6c 6f  der->iCurrentBlo
4519c 63 6b 20 3d 20 69 53 74 61 72 74 4c 65 61 66 2d  ck = iStartLeaf-
4519d 31 3b 0a 20 20 7d 0a 20 20 2a 70 70 52 65 61 64  1;.  }.  *ppRead
4519e 65 72 20 3d 20 70 52 65 61 64 65 72 3b 0a 20 20  er = pReader;.  
4519f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
451a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
451a1 69 73 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20  is a comparison 
451a2 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 61 73  function used as
451a3 20 61 20 71 73 6f 72 74 28 29 20 63 61 6c 6c 62   a qsort() callb
451a4 61 63 6b 20 77 68 65 6e 20 73 6f 72 74 69 6e 67  ack when sorting
451a5 0a 2a 2a 20 61 6e 20 61 72 72 61 79 20 6f 66 20  .** an array of 
451a6 70 65 6e 64 69 6e 67 20 74 65 72 6d 73 20 62 79  pending terms by
451a7 20 74 65 72 6d 2e 20 54 68 69 73 20 6f 63 63 75   term. This occu
451a8 72 73 20 61 73 20 70 61 72 74 20 6f 66 20 66 6c  rs as part of fl
451a9 75 73 68 69 6e 67 0a 2a 2a 20 74 68 65 20 63 6f  ushing.** the co
451aa 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 65  ntents of the pe
451ab 6e 64 69 6e 67 2d 74 65 72 6d 73 20 68 61 73 68  nding-terms hash
451ac 20 74 61 62 6c 65 20 74 6f 20 74 68 65 20 64 61   table to the da
451ad 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  tabase..*/.stati
451ae 63 20 69 6e 74 20 66 74 73 33 43 6f 6d 70 61 72  c int fts3Compar
451af 65 45 6c 65 6d 42 79 54 65 72 6d 28 63 6f 6e 73  eElemByTerm(cons
451b0 74 20 76 6f 69 64 20 2a 6c 68 73 2c 20 63 6f 6e  t void *lhs, con
451b1 73 74 20 76 6f 69 64 20 2a 72 68 73 29 7b 0a 20  st void *rhs){. 
451b2 20 63 68 61 72 20 2a 7a 31 20 3d 20 66 74 73 33   char *z1 = fts3
451b3 48 61 73 68 4b 65 79 28 2a 28 46 74 73 33 48 61  HashKey(*(Fts3Ha
451b4 73 68 45 6c 65 6d 20 2a 2a 29 6c 68 73 29 3b 0a  shElem **)lhs);.
451b5 20 20 63 68 61 72 20 2a 7a 32 20 3d 20 66 74 73    char *z2 = fts
451b6 33 48 61 73 68 4b 65 79 28 2a 28 46 74 73 33 48  3HashKey(*(Fts3H
451b7 61 73 68 45 6c 65 6d 20 2a 2a 29 72 68 73 29 3b  ashElem **)rhs);
451b8 0a 20 20 69 6e 74 20 6e 31 20 3d 20 66 74 73 33  .  int n1 = fts3
451b9 48 61 73 68 4b 65 79 73 69 7a 65 28 2a 28 46 74  HashKeysize(*(Ft
451ba 73 33 48 61 73 68 45 6c 65 6d 20 2a 2a 29 6c 68  s3HashElem **)lh
451bb 73 29 3b 0a 20 20 69 6e 74 20 6e 32 20 3d 20 66  s);.  int n2 = f
451bc 74 73 33 48 61 73 68 4b 65 79 73 69 7a 65 28 2a  ts3HashKeysize(*
451bd 28 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 2a  (Fts3HashElem **
451be 29 72 68 73 29 3b 0a 0a 20 20 69 6e 74 20 6e 20  )rhs);..  int n 
451bf 3d 20 28 6e 31 3c 6e 32 20 3f 20 6e 31 20 3a 20  = (n1<n2 ? n1 : 
451c0 6e 32 29 3b 0a 20 20 69 6e 74 20 63 20 3d 20 6d  n2);.  int c = m
451c1 65 6d 63 6d 70 28 7a 31 2c 20 7a 32 2c 20 6e 29  emcmp(z1, z2, n)
451c2 3b 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a  ;.  if( c==0 ){.
451c3 20 20 20 20 63 20 3d 20 6e 31 20 2d 20 6e 32 3b      c = n1 - n2;
451c4 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 3b  .  }.  return c;
451c5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
451c6 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
451c7 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 20 46  to allocate an F
451c8 74 73 33 53 65 67 52 65 61 64 65 72 20 74 68 61  ts3SegReader tha
451c9 74 20 69 74 65 72 61 74 65 73 20 74 68 72 6f 75  t iterates throu
451ca 67 68 0a 2a 2a 20 61 20 73 75 62 73 65 74 20 6f  gh.** a subset o
451cb 66 20 74 68 65 20 74 65 72 6d 73 20 73 74 6f 72  f the terms stor
451cc 65 64 20 69 6e 20 74 68 65 20 46 74 73 33 54 61  ed in the Fts3Ta
451cd 62 6c 65 2e 70 65 6e 64 69 6e 67 54 65 72 6d 73  ble.pendingTerms
451ce 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 49 66   array..**.** If
451cf 20 74 68 65 20 69 73 50 72 65 66 69 78 49 74 65   the isPrefixIte
451d0 72 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 7a  r parameter is z
451d1 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 72 65  ero, then the re
451d2 74 75 72 6e 65 64 20 53 65 67 52 65 61 64 65 72  turned SegReader
451d3 20 69 74 65 72 61 74 65 73 0a 2a 2a 20 74 68 72   iterates.** thr
451d4 6f 75 67 68 20 65 61 63 68 20 74 65 72 6d 20 69  ough each term i
451d5 6e 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 74 65  n the pending-te
451d6 72 6d 73 20 74 61 62 6c 65 2e 20 4f 72 2c 20 69  rms table. Or, i
451d7 66 20 69 73 50 72 65 66 69 78 49 74 65 72 20 69  f isPrefixIter i
451d8 73 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 69  s.** non-zero, i
451d9 74 20 69 74 65 72 61 74 65 73 20 74 68 72 6f 75  t iterates throu
451da 67 68 20 65 61 63 68 20 74 65 72 6d 20 61 6e 64  gh each term and
451db 20 69 74 73 20 70 72 65 66 69 78 65 73 2e 20 46   its prefixes. F
451dc 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 0a 2a  or example, if.*
451dd 2a 20 74 68 65 20 70 65 6e 64 69 6e 67 20 74 65  * the pending te
451de 72 6d 73 20 68 61 73 68 20 74 61 62 6c 65 20 63  rms hash table c
451df 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 65 72 6d  ontains the term
451e0 73 20 22 73 71 6c 69 74 65 22 2c 20 22 6d 79 73  s "sqlite", "mys
451e1 71 6c 22 20 61 6e 64 0a 2a 2a 20 22 66 69 72 65  ql" and.** "fire
451e2 62 69 72 64 22 2c 20 74 68 65 6e 20 74 68 65 20  bird", then the 
451e3 69 74 65 72 61 74 6f 72 20 76 69 73 69 74 73 20  iterator visits 
451e4 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 27 74  the following 't
451e5 65 72 6d 73 27 20 28 69 6e 20 74 68 65 20 6f 72  erms' (in the or
451e6 64 65 72 0a 2a 2a 20 73 68 6f 77 6e 29 3a 0a 2a  der.** shown):.*
451e7 2a 0a 2a 2a 20 20 20 66 20 66 69 20 66 69 72 20  *.**   f fi fir 
451e8 66 69 72 65 20 66 69 72 65 62 20 66 69 72 65 62  fire fireb fireb
451e9 69 20 66 69 72 65 62 69 72 20 66 69 72 65 62 69  i firebir firebi
451ea 72 64 0a 2a 2a 20 20 20 6d 20 6d 79 20 6d 79 73  rd.**   m my mys
451eb 20 6d 79 73 71 20 6d 79 73 71 6c 0a 2a 2a 20 20   mysq mysql.**  
451ec 20 73 20 73 71 20 73 71 6c 20 73 71 6c 69 20 73   s sq sql sqli s
451ed 71 6c 69 74 20 73 71 6c 69 74 65 0a 2a 2a 0a 2a  qlit sqlite.**.*
451ee 2a 20 57 68 65 72 65 61 73 20 69 66 20 69 73 50  * Whereas if isP
451ef 72 65 66 69 78 49 74 65 72 20 69 73 20 7a 65 72  refixIter is zer
451f0 6f 2c 20 74 68 65 20 74 65 72 6d 73 20 76 69 73  o, the terms vis
451f1 69 74 65 64 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20  ited are:.**.** 
451f2 20 20 66 69 72 65 62 69 72 64 20 6d 79 73 71 6c    firebird mysql
451f3 20 73 71 6c 69 74 65 0a 2a 2f 0a 53 51 4c 49 54   sqlite.*/.SQLIT
451f4 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
451f5 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64  lite3Fts3SegRead
451f6 65 72 50 65 6e 64 69 6e 67 28 0a 20 20 46 74 73  erPending(.  Fts
451f7 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20  3Table *p,      
451f8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
451f9 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61  Virtual table ha
451fa 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 49  ndle */.  int iI
451fb 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 20 20  ndex,           
451fc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
451fd 65 78 20 66 6f 72 20 70 2d 3e 61 49 6e 64 65 78  ex for p->aIndex
451fe 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
451ff 20 2a 7a 54 65 72 6d 2c 20 20 20 20 20 20 20 20   *zTerm,        
45200 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f        /* Term to
45201 20 73 65 61 72 63 68 20 66 6f 72 20 2a 2f 0a 20   search for */. 
45202 20 69 6e 74 20 6e 54 65 72 6d 2c 20 20 20 20 20   int nTerm,     
45203 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45204 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66   /* Size of buff
45205 65 72 20 7a 54 65 72 6d 20 2a 2f 0a 20 20 69 6e  er zTerm */.  in
45206 74 20 62 50 72 65 66 69 78 2c 20 20 20 20 20 20  t bPrefix,      
45207 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
45208 20 54 72 75 65 20 66 6f 72 20 61 20 70 72 65 66   True for a pref
45209 69 78 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20  ix iterator */. 
4520a 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a   Fts3SegReader *
4520b 2a 70 70 52 65 61 64 65 72 20 20 20 20 20 20 20  *ppReader       
4520c 20 2f 2a 20 4f 55 54 3a 20 53 65 67 52 65 61 64   /* OUT: SegRead
4520d 65 72 20 66 6f 72 20 70 65 6e 64 69 6e 67 2d 74  er for pending-t
4520e 65 72 6d 73 20 2a 2f 0a 29 7b 0a 20 20 46 74 73  erms */.){.  Fts
4520f 33 53 65 67 52 65 61 64 65 72 20 2a 70 52 65 61  3SegReader *pRea
45210 64 65 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  der = 0;     /* 
45211 46 74 73 33 53 65 67 52 65 61 64 65 72 20 6f 62  Fts3SegReader ob
45212 6a 65 63 74 20 74 6f 20 72 65 74 75 72 6e 20 2a  ject to return *
45213 2f 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d  /.  Fts3HashElem
45214 20 2a 70 45 3b 20 20 20 20 20 20 20 20 20 20 20   *pE;           
45215 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
45216 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 46 74  variable */.  Ft
45217 73 33 48 61 73 68 45 6c 65 6d 20 2a 2a 61 45 6c  s3HashElem **aEl
45218 65 6d 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  em = 0;       /*
45219 20 41 72 72 61 79 20 6f 66 20 74 65 72 6d 20 68   Array of term h
4521a 61 73 68 20 65 6e 74 72 69 65 73 20 74 6f 20 73  ash entries to s
4521b 63 61 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 6c  can */.  int nEl
4521c 65 6d 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  em = 0;         
4521d 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
4521e 20 6f 66 20 61 72 72 61 79 20 61 74 20 61 45 6c   of array at aEl
4521f 65 6d 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d  em */.  int rc =
45220 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
45221 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
45222 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 46 74 73 33  n Code */.  Fts3
45223 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 0a 20 20  Hash *pHash;..  
45224 70 48 61 73 68 20 3d 20 26 70 2d 3e 61 49 6e 64  pHash = &p->aInd
45225 65 78 5b 69 49 6e 64 65 78 5d 2e 68 50 65 6e 64  ex[iIndex].hPend
45226 69 6e 67 3b 0a 20 20 69 66 28 20 62 50 72 65 66  ing;.  if( bPref
45227 69 78 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41  ix ){.    int nA
45228 6c 6c 6f 63 20 3d 20 30 3b 20 20 20 20 20 20 20  lloc = 0;       
45229 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
4522a 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 61 72 72  of allocated arr
4522b 61 79 20 61 74 20 61 45 6c 65 6d 20 2a 2f 0a 0a  ay at aElem */..
4522c 20 20 20 20 66 6f 72 28 70 45 3d 66 74 73 33 48      for(pE=fts3H
4522d 61 73 68 46 69 72 73 74 28 70 48 61 73 68 29 3b  ashFirst(pHash);
4522e 20 70 45 3b 20 70 45 3d 66 74 73 33 48 61 73 68   pE; pE=fts3Hash
4522f 4e 65 78 74 28 70 45 29 29 7b 0a 20 20 20 20 20  Next(pE)){.     
45230 20 63 68 61 72 20 2a 7a 4b 65 79 20 3d 20 28 63   char *zKey = (c
45231 68 61 72 20 2a 29 66 74 73 33 48 61 73 68 4b 65  har *)fts3HashKe
45232 79 28 70 45 29 3b 0a 20 20 20 20 20 20 69 6e 74  y(pE);.      int
45233 20 6e 4b 65 79 20 3d 20 66 74 73 33 48 61 73 68   nKey = fts3Hash
45234 4b 65 79 73 69 7a 65 28 70 45 29 3b 0a 20 20 20  Keysize(pE);.   
45235 20 20 20 69 66 28 20 6e 54 65 72 6d 3d 3d 30 20     if( nTerm==0 
45236 7c 7c 20 28 6e 4b 65 79 3e 3d 6e 54 65 72 6d 20  || (nKey>=nTerm 
45237 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 7a 4b 65  && 0==memcmp(zKe
45238 79 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29  y, zTerm, nTerm)
45239 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ) ){.        if(
4523a 20 6e 45 6c 65 6d 3d 3d 6e 41 6c 6c 6f 63 20 29   nElem==nAlloc )
4523b 7b 0a 20 20 20 20 20 20 20 20 20 20 46 74 73 33  {.          Fts3
4523c 48 61 73 68 45 6c 65 6d 20 2a 2a 61 45 6c 65 6d  HashElem **aElem
4523d 32 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 41 6c  2;.          nAl
4523e 6c 6f 63 20 2b 3d 20 31 36 3b 0a 20 20 20 20 20  loc += 16;.     
4523f 20 20 20 20 20 61 45 6c 65 6d 32 20 3d 20 28 46       aElem2 = (F
45240 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 2a 29 73  ts3HashElem **)s
45241 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 0a  qlite3_realloc(.
45242 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 45                aE
45243 6c 65 6d 2c 20 6e 41 6c 6c 6f 63 2a 73 69 7a 65  lem, nAlloc*size
45244 6f 66 28 46 74 73 33 48 61 73 68 45 6c 65 6d 20  of(Fts3HashElem 
45245 2a 29 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a  *).          );.
45246 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 61            if( !a
45247 45 6c 65 6d 32 20 29 7b 0a 20 20 20 20 20 20 20  Elem2 ){.       
45248 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
45249 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
4524a 20 20 20 20 6e 45 6c 65 6d 20 3d 20 30 3b 0a 20      nElem = 0;. 
4524b 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
4524c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
4524d 20 20 20 20 20 20 20 20 61 45 6c 65 6d 20 3d 20          aElem = 
4524e 61 45 6c 65 6d 32 3b 0a 20 20 20 20 20 20 20 20  aElem2;.        
4524f 7d 0a 0a 20 20 20 20 20 20 20 20 61 45 6c 65 6d  }..        aElem
45250 5b 6e 45 6c 65 6d 2b 2b 5d 20 3d 20 70 45 3b 0a  [nElem++] = pE;.
45251 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
45252 20 20 20 2f 2a 20 49 66 20 6d 6f 72 65 20 74 68     /* If more th
45253 61 6e 20 6f 6e 65 20 74 65 72 6d 20 6d 61 74 63  an one term matc
45254 68 65 73 20 74 68 65 20 70 72 65 66 69 78 2c 20  hes the prefix, 
45255 73 6f 72 74 20 74 68 65 20 46 74 73 33 48 61 73  sort the Fts3Has
45256 68 45 6c 65 6d 0a 20 20 20 20 2a 2a 20 6f 62 6a  hElem.    ** obj
45257 65 63 74 73 20 69 6e 20 74 65 72 6d 20 6f 72 64  ects in term ord
45258 65 72 20 75 73 69 6e 67 20 71 73 6f 72 74 28 29  er using qsort()
45259 2e 20 54 68 69 73 20 75 73 65 73 20 74 68 65 20  . This uses the 
4525a 73 61 6d 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a  same comparison.
4525b 20 20 20 20 2a 2a 20 63 61 6c 6c 62 61 63 6b 20      ** callback 
4525c 61 73 20 69 73 20 75 73 65 64 20 77 68 65 6e 20  as is used when 
4525d 66 6c 75 73 68 69 6e 67 20 74 65 72 6d 73 20 74  flushing terms t
4525e 6f 20 64 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20  o disk..    */. 
4525f 20 20 20 69 66 28 20 6e 45 6c 65 6d 3e 31 20 29     if( nElem>1 )
45260 7b 0a 20 20 20 20 20 20 71 73 6f 72 74 28 61 45  {.      qsort(aE
45261 6c 65 6d 2c 20 6e 45 6c 65 6d 2c 20 73 69 7a 65  lem, nElem, size
45262 6f 66 28 46 74 73 33 48 61 73 68 45 6c 65 6d 20  of(Fts3HashElem 
45263 2a 29 2c 20 66 74 73 33 43 6f 6d 70 61 72 65 45  *), fts3CompareE
45264 6c 65 6d 42 79 54 65 72 6d 29 3b 0a 20 20 20 20  lemByTerm);.    
45265 7d 0a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  }..  }else{.    
45266 2f 2a 20 54 68 65 20 71 75 65 72 79 20 69 73 20  /* The query is 
45267 61 20 73 69 6d 70 6c 65 20 74 65 72 6d 20 6c 6f  a simple term lo
45268 6f 6b 75 70 20 74 68 61 74 20 6d 61 74 63 68 65  okup that matche
45269 73 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 74 65  s at most one te
4526a 72 6d 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65  rm in.    ** the
4526b 20 69 6e 64 65 78 2e 20 41 6c 6c 20 74 68 61 74   index. All that
4526c 20 69 73 20 72 65 71 75 69 72 65 64 20 69 73 20   is required is 
4526d 61 20 73 74 72 61 69 67 68 74 20 68 61 73 68 2d  a straight hash-
4526e 6c 6f 6f 6b 75 70 2e 20 0a 20 20 20 20 2a 2a 0a  lookup. .    **.
4526f 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65 20 74      ** Because t
45270 68 65 20 73 74 61 63 6b 20 61 64 64 72 65 73 73  he stack address
45271 20 6f 66 20 70 45 20 6d 61 79 20 62 65 20 61 63   of pE may be ac
45272 63 65 73 73 65 64 20 76 69 61 20 74 68 65 20 61  cessed via the a
45273 45 6c 65 6d 20 70 6f 69 6e 74 65 72 0a 20 20 20  Elem pointer.   
45274 20 2a 2a 20 62 65 6c 6f 77 2c 20 74 68 65 20 22   ** below, the "
45275 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 45  Fts3HashElem *pE
45276 22 20 6d 75 73 74 20 62 65 20 64 65 63 6c 61 72  " must be declar
45277 65 64 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  ed so that it is
45278 20 76 61 6c 69 64 0a 20 20 20 20 2a 2a 20 77 69   valid.    ** wi
45279 74 68 69 6e 20 74 68 69 73 20 65 6e 74 69 72 65  thin this entire
4527a 20 66 75 6e 63 74 69 6f 6e 2c 20 6e 6f 74 20 6a   function, not j
4527b 75 73 74 20 74 68 69 73 20 22 65 6c 73 65 7b 2e  ust this "else{.
4527c 2e 2e 7d 22 20 62 6c 6f 63 6b 2e 0a 20 20 20 20  ..}" block..    
4527d 2a 2f 0a 20 20 20 20 70 45 20 3d 20 66 74 73 33  */.    pE = fts3
4527e 48 61 73 68 46 69 6e 64 45 6c 65 6d 28 70 48 61  HashFindElem(pHa
4527f 73 68 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d  sh, zTerm, nTerm
45280 29 3b 0a 20 20 20 20 69 66 28 20 70 45 20 29 7b  );.    if( pE ){
45281 0a 20 20 20 20 20 20 61 45 6c 65 6d 20 3d 20 26  .      aElem = &
45282 70 45 3b 0a 20 20 20 20 20 20 6e 45 6c 65 6d 20  pE;.      nElem 
45283 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  = 1;.    }.  }..
45284 20 20 69 66 28 20 6e 45 6c 65 6d 3e 30 20 29 7b    if( nElem>0 ){
45285 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d  .    int nByte =
45286 20 73 69 7a 65 6f 66 28 46 74 73 33 53 65 67 52   sizeof(Fts3SegR
45287 65 61 64 65 72 29 20 2b 20 28 6e 45 6c 65 6d 2b  eader) + (nElem+
45288 31 29 2a 73 69 7a 65 6f 66 28 46 74 73 33 48 61  1)*sizeof(Fts3Ha
45289 73 68 45 6c 65 6d 20 2a 29 3b 0a 20 20 20 20 70  shElem *);.    p
4528a 52 65 61 64 65 72 20 3d 20 28 46 74 73 33 53 65  Reader = (Fts3Se
4528b 67 52 65 61 64 65 72 20 2a 29 73 71 6c 69 74 65  gReader *)sqlite
4528c 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b  3_malloc(nByte);
4528d 0a 20 20 20 20 69 66 28 20 21 70 52 65 61 64 65  .    if( !pReade
4528e 72 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  r ){.      rc = 
4528f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
45290 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d    }else{.      m
45291 65 6d 73 65 74 28 70 52 65 61 64 65 72 2c 20 30  emset(pReader, 0
45292 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20  , nByte);.      
45293 70 52 65 61 64 65 72 2d 3e 69 49 64 78 20 3d 20  pReader->iIdx = 
45294 30 78 37 46 46 46 46 46 46 46 3b 0a 20 20 20 20  0x7FFFFFFF;.    
45295 20 20 70 52 65 61 64 65 72 2d 3e 70 70 4e 65 78    pReader->ppNex
45296 74 45 6c 65 6d 20 3d 20 28 46 74 73 33 48 61 73  tElem = (Fts3Has
45297 68 45 6c 65 6d 20 2a 2a 29 26 70 52 65 61 64 65  hElem **)&pReade
45298 72 5b 31 5d 3b 0a 20 20 20 20 20 20 6d 65 6d 63  r[1];.      memc
45299 70 79 28 70 52 65 61 64 65 72 2d 3e 70 70 4e 65  py(pReader->ppNe
4529a 78 74 45 6c 65 6d 2c 20 61 45 6c 65 6d 2c 20 6e  xtElem, aElem, n
4529b 45 6c 65 6d 2a 73 69 7a 65 6f 66 28 46 74 73 33  Elem*sizeof(Fts3
4529c 48 61 73 68 45 6c 65 6d 20 2a 29 29 3b 0a 20 20  HashElem *));.  
4529d 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 62    }.  }..  if( b
4529e 50 72 65 66 69 78 20 29 7b 0a 20 20 20 20 73 71  Prefix ){.    sq
4529f 6c 69 74 65 33 5f 66 72 65 65 28 61 45 6c 65 6d  lite3_free(aElem
452a0 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 52 65 61 64  );.  }.  *ppRead
452a1 65 72 20 3d 20 70 52 65 61 64 65 72 3b 0a 20 20  er = pReader;.  
452a2 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
452a3 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20  .** Compare the 
452a4 65 6e 74 72 69 65 73 20 70 6f 69 6e 74 65 64 20  entries pointed 
452a5 74 6f 20 62 79 20 74 77 6f 20 46 74 73 33 53 65  to by two Fts3Se
452a6 67 52 65 61 64 65 72 20 73 74 72 75 63 74 75 72  gReader structur
452a7 65 73 2e 20 0a 2a 2a 20 43 6f 6d 70 61 72 69 73  es. .** Comparis
452a8 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  on is as follows
452a9 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 45 4f 46  :.**.**   1) EOF
452aa 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
452ab 20 6e 6f 74 20 45 4f 46 2e 0a 2a 2a 0a 2a 2a 20   not EOF..**.** 
452ac 20 20 32 29 20 54 68 65 20 63 75 72 72 65 6e 74    2) The current
452ad 20 74 65 72 6d 73 20 28 69 66 20 61 6e 79 29 20   terms (if any) 
452ae 61 72 65 20 63 6f 6d 70 61 72 65 64 20 75 73 69  are compared usi
452af 6e 67 20 6d 65 6d 63 6d 70 28 29 2e 20 49 66 20  ng memcmp(). If 
452b0 6f 6e 65 0a 2a 2a 20 20 20 20 20 20 74 65 72 6d  one.**      term
452b1 20 69 73 20 61 20 70 72 65 66 69 78 20 6f 66 20   is a prefix of 
452b2 61 6e 6f 74 68 65 72 2c 20 74 68 65 20 6c 6f 6e  another, the lon
452b3 67 65 72 20 74 65 72 6d 20 69 73 20 63 6f 6e 73  ger term is cons
452b4 69 64 65 72 65 64 20 74 68 65 0a 2a 2a 20 20 20  idered the.**   
452b5 20 20 20 6c 61 72 67 65 72 2e 0a 2a 2a 0a 2a 2a     larger..**.**
452b6 20 20 20 33 29 20 42 79 20 73 65 67 6d 65 6e 74     3) By segment
452b7 20 61 67 65 2e 20 41 6e 20 6f 6c 64 65 72 20 73   age. An older s
452b8 65 67 6d 65 6e 74 20 69 73 20 63 6f 6e 73 69 64  egment is consid
452b9 65 72 65 64 20 6c 61 72 67 65 72 2e 0a 2a 2f 0a  ered larger..*/.
452ba 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53  static int fts3S
452bb 65 67 52 65 61 64 65 72 43 6d 70 28 46 74 73 33  egReaderCmp(Fts3
452bc 53 65 67 52 65 61 64 65 72 20 2a 70 4c 68 73 2c  SegReader *pLhs,
452bd 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a   Fts3SegReader *
452be 70 52 68 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b  pRhs){.  int rc;
452bf 0a 20 20 69 66 28 20 70 4c 68 73 2d 3e 61 4e 6f  .  if( pLhs->aNo
452c0 64 65 20 26 26 20 70 52 68 73 2d 3e 61 4e 6f 64  de && pRhs->aNod
452c1 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32  e ){.    int rc2
452c2 20 3d 20 70 4c 68 73 2d 3e 6e 54 65 72 6d 20 2d   = pLhs->nTerm -
452c3 20 70 52 68 73 2d 3e 6e 54 65 72 6d 3b 0a 20 20   pRhs->nTerm;.  
452c4 20 20 69 66 28 20 72 63 32 3c 30 20 29 7b 0a 20    if( rc2<0 ){. 
452c5 20 20 20 20 20 72 63 20 3d 20 6d 65 6d 63 6d 70       rc = memcmp
452c6 28 70 4c 68 73 2d 3e 7a 54 65 72 6d 2c 20 70 52  (pLhs->zTerm, pR
452c7 68 73 2d 3e 7a 54 65 72 6d 2c 20 70 4c 68 73 2d  hs->zTerm, pLhs-
452c8 3e 6e 54 65 72 6d 29 3b 0a 20 20 20 20 7d 65 6c  >nTerm);.    }el
452c9 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d  se{.      rc = m
452ca 65 6d 63 6d 70 28 70 4c 68 73 2d 3e 7a 54 65 72  emcmp(pLhs->zTer
452cb 6d 2c 20 70 52 68 73 2d 3e 7a 54 65 72 6d 2c 20  m, pRhs->zTerm, 
452cc 70 52 68 73 2d 3e 6e 54 65 72 6d 29 3b 0a 20 20  pRhs->nTerm);.  
452cd 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
452ce 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
452cf 72 63 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  rc2;.    }.  }el
452d0 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 28 70 4c  se{.    rc = (pL
452d1 68 73 2d 3e 61 4e 6f 64 65 3d 3d 30 29 20 2d 20  hs->aNode==0) - 
452d2 28 70 52 68 73 2d 3e 61 4e 6f 64 65 3d 3d 30 29  (pRhs->aNode==0)
452d3 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  ;.  }.  if( rc==
452d4 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 52  0 ){.    rc = pR
452d5 68 73 2d 3e 69 49 64 78 20 2d 20 70 4c 68 73 2d  hs->iIdx - pLhs-
452d6 3e 69 49 64 78 3b 0a 20 20 7d 0a 20 20 61 73 73  >iIdx;.  }.  ass
452d7 65 72 74 28 20 72 63 21 3d 30 20 29 3b 0a 20 20  ert( rc!=0 );.  
452d8 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
452d9 0a 2a 2a 20 41 20 64 69 66 66 65 72 65 6e 74 20  .** A different 
452da 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74  comparison funct
452db 69 6f 6e 20 66 6f 72 20 53 65 67 52 65 61 64 65  ion for SegReade
452dc 72 20 73 74 72 75 63 74 75 72 65 73 2e 20 49 6e  r structures. In
452dd 20 74 68 69 73 0a 2a 2a 20 76 65 72 73 69 6f 6e   this.** version
452de 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 20  , it is assumed 
452df 74 68 61 74 20 65 61 63 68 20 53 65 67 52 65 61  that each SegRea
452e0 64 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e  der points to an
452e1 20 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 61 20 64   entry in.** a d
452e2 6f 63 6c 69 73 74 20 66 6f 72 20 69 64 65 6e 74  oclist for ident
452e3 69 63 61 6c 20 74 65 72 6d 73 2e 20 43 6f 6d 70  ical terms. Comp
452e4 61 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 20 61  arison is made a
452e5 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
452e6 20 20 20 31 29 20 45 4f 46 20 28 65 6e 64 20 6f     1) EOF (end o
452e7 66 20 64 6f 63 6c 69 73 74 20 69 6e 20 74 68 69  f doclist in thi
452e8 73 20 63 61 73 65 29 20 69 73 20 67 72 65 61 74  s case) is great
452e9 65 72 20 74 68 61 6e 20 6e 6f 74 20 45 4f 46 2e  er than not EOF.
452ea 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 42 79 20 63  .**.**   2) By c
452eb 75 72 72 65 6e 74 20 64 6f 63 69 64 2e 0a 2a 2a  urrent docid..**
452ec 0a 2a 2a 20 20 20 33 29 20 42 79 20 73 65 67 6d  .**   3) By segm
452ed 65 6e 74 20 61 67 65 2e 20 41 6e 20 6f 6c 64 65  ent age. An olde
452ee 72 20 73 65 67 6d 65 6e 74 20 69 73 20 63 6f 6e  r segment is con
452ef 73 69 64 65 72 65 64 20 6c 61 72 67 65 72 2e 0a  sidered larger..
452f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
452f1 73 33 53 65 67 52 65 61 64 65 72 44 6f 63 6c 69  s3SegReaderDocli
452f2 73 74 43 6d 70 28 46 74 73 33 53 65 67 52 65 61  stCmp(Fts3SegRea
452f3 64 65 72 20 2a 70 4c 68 73 2c 20 46 74 73 33 53  der *pLhs, Fts3S
452f4 65 67 52 65 61 64 65 72 20 2a 70 52 68 73 29 7b  egReader *pRhs){
452f5 0a 20 20 69 6e 74 20 72 63 20 3d 20 28 70 4c 68  .  int rc = (pLh
452f6 73 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 3d 3d  s->pOffsetList==
452f7 30 29 2d 28 70 52 68 73 2d 3e 70 4f 66 66 73 65  0)-(pRhs->pOffse
452f8 74 4c 69 73 74 3d 3d 30 29 3b 0a 20 20 69 66 28  tList==0);.  if(
452f9 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66   rc==0 ){.    if
452fa 28 20 70 4c 68 73 2d 3e 69 44 6f 63 69 64 3d 3d  ( pLhs->iDocid==
452fb 70 52 68 73 2d 3e 69 44 6f 63 69 64 20 29 7b 0a  pRhs->iDocid ){.
452fc 20 20 20 20 20 20 72 63 20 3d 20 70 52 68 73 2d        rc = pRhs-
452fd 3e 69 49 64 78 20 2d 20 70 4c 68 73 2d 3e 69 49  >iIdx - pLhs->iI
452fe 64 78 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  dx;.    }else{. 
452ff 20 20 20 20 20 72 63 20 3d 20 28 70 4c 68 73 2d       rc = (pLhs-
45300 3e 69 44 6f 63 69 64 20 3e 20 70 52 68 73 2d 3e  >iDocid > pRhs->
45301 69 44 6f 63 69 64 29 20 3f 20 31 20 3a 20 2d 31  iDocid) ? 1 : -1
45302 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73  ;.    }.  }.  as
45303 73 65 72 74 28 20 70 4c 68 73 2d 3e 61 4e 6f 64  sert( pLhs->aNod
45304 65 20 26 26 20 70 52 68 73 2d 3e 61 4e 6f 64 65  e && pRhs->aNode
45305 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   );.  return rc;
45306 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  .}.static int ft
45307 73 33 53 65 67 52 65 61 64 65 72 44 6f 63 6c 69  s3SegReaderDocli
45308 73 74 43 6d 70 52 65 76 28 46 74 73 33 53 65 67  stCmpRev(Fts3Seg
45309 52 65 61 64 65 72 20 2a 70 4c 68 73 2c 20 46 74  Reader *pLhs, Ft
4530a 73 33 53 65 67 52 65 61 64 65 72 20 2a 70 52 68  s3SegReader *pRh
4530b 73 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 28  s){.  int rc = (
4530c 70 4c 68 73 2d 3e 70 4f 66 66 73 65 74 4c 69 73  pLhs->pOffsetLis
4530d 74 3d 3d 30 29 2d 28 70 52 68 73 2d 3e 70 4f 66  t==0)-(pRhs->pOf
4530e 66 73 65 74 4c 69 73 74 3d 3d 30 29 3b 0a 20 20  fsetList==0);.  
4530f 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20  if( rc==0 ){.   
45310 20 69 66 28 20 70 4c 68 73 2d 3e 69 44 6f 63 69   if( pLhs->iDoci
45311 64 3d 3d 70 52 68 73 2d 3e 69 44 6f 63 69 64 20  d==pRhs->iDocid 
45312 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 52  ){.      rc = pR
45313 68 73 2d 3e 69 49 64 78 20 2d 20 70 4c 68 73 2d  hs->iIdx - pLhs-
45314 3e 69 49 64 78 3b 0a 20 20 20 20 7d 65 6c 73 65  >iIdx;.    }else
45315 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 28 70 4c  {.      rc = (pL
45316 68 73 2d 3e 69 44 6f 63 69 64 20 3c 20 70 52 68  hs->iDocid < pRh
45317 73 2d 3e 69 44 6f 63 69 64 29 20 3f 20 31 20 3a  s->iDocid) ? 1 :
45318 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   -1;.    }.  }. 
45319 20 61 73 73 65 72 74 28 20 70 4c 68 73 2d 3e 61   assert( pLhs->a
4531a 4e 6f 64 65 20 26 26 20 70 52 68 73 2d 3e 61 4e  Node && pRhs->aN
4531b 6f 64 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ode );.  return 
4531c 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  rc;.}../*.** Com
4531d 70 61 72 65 20 74 68 65 20 74 65 72 6d 20 74 68  pare the term th
4531e 61 74 20 74 68 65 20 46 74 73 33 53 65 67 52 65  at the Fts3SegRe
4531f 61 64 65 72 20 6f 62 6a 65 63 74 20 70 61 73 73  ader object pass
45320 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
45321 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 70 6f 69 6e  argument.** poin
45322 74 73 20 74 6f 20 77 69 74 68 20 74 68 65 20 74  ts to with the t
45323 65 72 6d 20 73 70 65 63 69 66 69 65 64 20 62 79  erm specified by
45324 20 61 72 67 75 6d 65 6e 74 73 20 7a 54 65 72 6d   arguments zTerm
45325 20 61 6e 64 20 6e 54 65 72 6d 2e 20 0a 2a 2a 0a   and nTerm. .**.
45326 2a 2a 20 49 66 20 74 68 65 20 70 53 65 67 20 69  ** If the pSeg i
45327 74 65 72 61 74 6f 72 20 69 73 20 61 6c 72 65 61  terator is alrea
45328 64 79 20 61 74 20 45 4f 46 2c 20 72 65 74 75 72  dy at EOF, retur
45329 6e 20 30 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  n 0. Otherwise, 
4532a 72 65 74 75 72 6e 0a 2a 2a 20 2d 76 65 20 69 66  return.** -ve if
4532b 20 74 68 65 20 70 53 65 67 20 74 65 72 6d 20 69   the pSeg term i
4532c 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 54 65 72  s less than zTer
4532d 6d 2f 6e 54 65 72 6d 2c 20 30 20 69 66 20 74 68  m/nTerm, 0 if th
4532e 65 20 74 77 6f 20 74 65 72 6d 73 20 61 72 65 0a  e two terms are.
4532f 2a 2a 20 65 71 75 61 6c 2c 20 6f 72 20 2b 76 65  ** equal, or +ve
45330 20 69 66 20 74 68 65 20 70 53 65 67 20 74 65 72   if the pSeg ter
45331 6d 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  m is greater tha
45332 6e 20 7a 54 65 72 6d 2f 6e 54 65 72 6d 2e 0a 2a  n zTerm/nTerm..*
45333 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
45334 33 53 65 67 52 65 61 64 65 72 54 65 72 6d 43 6d  3SegReaderTermCm
45335 70 28 0a 20 20 46 74 73 33 53 65 67 52 65 61 64  p(.  Fts3SegRead
45336 65 72 20 2a 70 53 65 67 2c 20 20 20 20 20 20 20  er *pSeg,       
45337 20 20 20 20 20 2f 2a 20 53 65 67 6d 65 6e 74 20       /* Segment 
45338 72 65 61 64 65 72 20 6f 62 6a 65 63 74 20 2a 2f  reader object */
45339 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
4533a 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20  Term,           
4533b 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20 63 6f     /* Term to co
4533c 6d 70 61 72 65 20 74 6f 20 2a 2f 0a 20 20 69 6e  mpare to */.  in
4533d 74 20 6e 54 65 72 6d 20 20 20 20 20 20 20 20 20  t nTerm         
4533e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4533f 20 53 69 7a 65 20 6f 66 20 74 65 72 6d 20 7a 54   Size of term zT
45340 65 72 6d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  erm in bytes */.
45341 29 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 30  ){.  int res = 0
45342 3b 0a 20 20 69 66 28 20 70 53 65 67 2d 3e 61 4e  ;.  if( pSeg->aN
45343 6f 64 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ode ){.    if( p
45344 53 65 67 2d 3e 6e 54 65 72 6d 3e 6e 54 65 72 6d  Seg->nTerm>nTerm
45345 20 29 7b 0a 20 20 20 20 20 20 72 65 73 20 3d 20   ){.      res = 
45346 6d 65 6d 63 6d 70 28 70 53 65 67 2d 3e 7a 54 65  memcmp(pSeg->zTe
45347 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d  rm, zTerm, nTerm
45348 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
45349 20 20 20 20 72 65 73 20 3d 20 6d 65 6d 63 6d 70      res = memcmp
4534a 28 70 53 65 67 2d 3e 7a 54 65 72 6d 2c 20 7a 54  (pSeg->zTerm, zT
4534b 65 72 6d 2c 20 70 53 65 67 2d 3e 6e 54 65 72 6d  erm, pSeg->nTerm
4534c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
4534d 20 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20   res==0 ){.     
4534e 20 72 65 73 20 3d 20 70 53 65 67 2d 3e 6e 54 65   res = pSeg->nTe
4534f 72 6d 2d 6e 54 65 72 6d 3b 0a 20 20 20 20 7d 0a  rm-nTerm;.    }.
45350 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73    }.  return res
45351 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d  ;.}../*.** Argum
45352 65 6e 74 20 61 70 53 65 67 6d 65 6e 74 20 69 73  ent apSegment is
45353 20 61 6e 20 61 72 72 61 79 20 6f 66 20 6e 53 65   an array of nSe
45354 67 6d 65 6e 74 20 65 6c 65 6d 65 6e 74 73 2e 20  gment elements. 
45355 49 74 20 69 73 20 6b 6e 6f 77 6e 20 74 68 61 74  It is known that
45356 0a 2a 2a 20 74 68 65 20 66 69 6e 61 6c 20 28 6e  .** the final (n
45357 53 65 67 6d 65 6e 74 2d 6e 53 75 73 70 65 63 74  Segment-nSuspect
45358 29 20 6d 65 6d 62 65 72 73 20 61 72 65 20 61 6c  ) members are al
45359 72 65 61 64 79 20 69 6e 20 73 6f 72 74 65 64 20  ready in sorted 
4535a 6f 72 64 65 72 0a 2a 2a 20 28 61 63 63 6f 72 64  order.** (accord
4535b 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 6d 70 61  ing to the compa
4535c 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70  rison function p
4535d 72 6f 76 69 64 65 64 29 2e 20 54 68 69 73 20 66  rovided). This f
4535e 75 6e 63 74 69 6f 6e 20 73 68 75 66 66 6c 65 73  unction shuffles
4535f 0a 2a 2a 20 74 68 65 20 61 72 72 61 79 20 61 72  .** the array ar
45360 6f 75 6e 64 20 75 6e 74 69 6c 20 61 6c 6c 20 65  ound until all e
45361 6e 74 72 69 65 73 20 61 72 65 20 69 6e 20 73 6f  ntries are in so
45362 72 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73  rted order..*/.s
45363 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 53  tatic void fts3S
45364 65 67 52 65 61 64 65 72 53 6f 72 74 28 0a 20 20  egReaderSort(.  
45365 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a  Fts3SegReader **
45366 61 70 53 65 67 6d 65 6e 74 2c 20 20 20 20 20 20  apSegment,      
45367 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
45368 2a 20 41 72 72 61 79 20 74 6f 20 73 6f 72 74 20  * Array to sort 
45369 65 6e 74 72 69 65 73 20 6f 66 20 2a 2f 0a 20 20  entries of */.  
4536a 69 6e 74 20 6e 53 65 67 6d 65 6e 74 2c 20 20 20  int nSegment,   
4536b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4536c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4536d 2a 20 53 69 7a 65 20 6f 66 20 61 70 53 65 67 6d  * Size of apSegm
4536e 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 69  ent array */.  i
4536f 6e 74 20 6e 53 75 73 70 65 63 74 2c 20 20 20 20  nt nSuspect,    
45370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45371 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
45372 20 55 6e 73 6f 72 74 65 64 20 65 6e 74 72 79 20   Unsorted entry 
45373 63 6f 75 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 28  count */.  int (
45374 2a 78 43 6d 70 29 28 46 74 73 33 53 65 67 52 65  *xCmp)(Fts3SegRe
45375 61 64 65 72 20 2a 2c 20 46 74 73 33 53 65 67 52  ader *, Fts3SegR
45376 65 61 64 65 72 20 2a 29 20 20 2f 2a 20 43 6f 6d  eader *)  /* Com
45377 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e  parison function
45378 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20   */.){.  int i; 
45379 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4537a 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
4537b 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
4537c 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53 75 73  ..  assert( nSus
4537d 70 65 63 74 3c 3d 6e 53 65 67 6d 65 6e 74 20 29  pect<=nSegment )
4537e 3b 0a 0a 20 20 69 66 28 20 6e 53 75 73 70 65 63  ;..  if( nSuspec
4537f 74 3d 3d 6e 53 65 67 6d 65 6e 74 20 29 20 6e 53  t==nSegment ) nS
45380 75 73 70 65 63 74 2d 2d 3b 0a 20 20 66 6f 72 28  uspect--;.  for(
45381 69 3d 6e 53 75 73 70 65 63 74 2d 31 3b 20 69 3e  i=nSuspect-1; i>
45382 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e  =0; i--){.    in
45383 74 20 6a 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69  t j;.    for(j=i
45384 3b 20 6a 3c 28 6e 53 65 67 6d 65 6e 74 2d 31 29  ; j<(nSegment-1)
45385 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 46 74  ; j++){.      Ft
45386 73 33 53 65 67 52 65 61 64 65 72 20 2a 70 54 6d  s3SegReader *pTm
45387 70 3b 0a 20 20 20 20 20 20 69 66 28 20 78 43 6d  p;.      if( xCm
45388 70 28 61 70 53 65 67 6d 65 6e 74 5b 6a 5d 2c 20  p(apSegment[j], 
45389 61 70 53 65 67 6d 65 6e 74 5b 6a 2b 31 5d 29 3c  apSegment[j+1])<
4538a 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  0 ) break;.     
4538b 20 70 54 6d 70 20 3d 20 61 70 53 65 67 6d 65 6e   pTmp = apSegmen
4538c 74 5b 6a 2b 31 5d 3b 0a 20 20 20 20 20 20 61 70  t[j+1];.      ap
4538d 53 65 67 6d 65 6e 74 5b 6a 2b 31 5d 20 3d 20 61  Segment[j+1] = a
4538e 70 53 65 67 6d 65 6e 74 5b 6a 5d 3b 0a 20 20 20  pSegment[j];.   
4538f 20 20 20 61 70 53 65 67 6d 65 6e 74 5b 6a 5d 20     apSegment[j] 
45390 3d 20 70 54 6d 70 3b 0a 20 20 20 20 7d 0a 20 20  = pTmp;.    }.  
45391 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  }..#ifndef NDEBU
45392 47 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  G.  /* Check tha
45393 74 20 74 68 65 20 6c 69 73 74 20 72 65 61 6c 6c  t the list reall
45394 79 20 69 73 20 73 6f 72 74 65 64 20 6e 6f 77 2e  y is sorted now.
45395 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   */.  for(i=0; i
45396 3c 28 6e 53 75 73 70 65 63 74 2d 31 29 3b 20 69  <(nSuspect-1); i
45397 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ++){.    assert(
45398 20 78 43 6d 70 28 61 70 53 65 67 6d 65 6e 74 5b   xCmp(apSegment[
45399 69 5d 2c 20 61 70 53 65 67 6d 65 6e 74 5b 69 2b  i], apSegment[i+
4539a 31 5d 29 3c 30 20 29 3b 0a 20 20 7d 0a 23 65 6e  1])<0 );.  }.#en
4539b 64 69 66 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 49 6e  dif.}../* .** In
4539c 73 65 72 74 20 61 20 72 65 63 6f 72 64 20 69 6e  sert a record in
4539d 74 6f 20 74 68 65 20 25 5f 73 65 67 6d 65 6e 74  to the %_segment
4539e 73 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  s table..*/.stat
4539f 69 63 20 69 6e 74 20 66 74 73 33 57 72 69 74 65  ic int fts3Write
453a0 53 65 67 6d 65 6e 74 28 0a 20 20 46 74 73 33 54  Segment(.  Fts3T
453a1 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20  able *p,        
453a2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69             /* Vi
453a3 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64  rtual table hand
453a4 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  le */.  sqlite3_
453a5 69 6e 74 36 34 20 69 42 6c 6f 63 6b 2c 20 20 20  int64 iBlock,   
453a6 20 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b          /* Block
453a7 20 69 64 20 66 6f 72 20 6e 65 77 20 62 6c 6f 63   id for new bloc
453a8 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 2c 20  k */.  char *z, 
453a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
453aa 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
453ab 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e 74  r to buffer cont
453ac 61 69 6e 69 6e 67 20 62 6c 6f 63 6b 20 64 61 74  aining block dat
453ad 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 20 20 20 20  a */.  int n    
453ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
453af 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
453b0 66 20 62 75 66 66 65 72 20 7a 20 69 6e 20 62 79  f buffer z in by
453b1 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  tes */.){.  sqli
453b2 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
453b3 0a 20 20 69 6e 74 20 72 63 20 3d 20 66 74 73 33  .  int rc = fts3
453b4 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 49  SqlStmt(p, SQL_I
453b5 4e 53 45 52 54 5f 53 45 47 4d 45 4e 54 53 2c 20  NSERT_SEGMENTS, 
453b6 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66  &pStmt, 0);.  if
453b7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
453b8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62  ){.    sqlite3_b
453b9 69 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c  ind_int64(pStmt,
453ba 20 31 2c 20 69 42 6c 6f 63 6b 29 3b 0a 20 20 20   1, iBlock);.   
453bb 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
453bc 6f 62 28 70 53 74 6d 74 2c 20 32 2c 20 7a 2c 20  ob(pStmt, 2, z, 
453bd 6e 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  n, SQLITE_STATIC
453be 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  );.    sqlite3_s
453bf 74 65 70 28 70 53 74 6d 74 29 3b 0a 20 20 20 20  tep(pStmt);.    
453c0 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
453c1 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 7d 0a 20  et(pStmt);.  }. 
453c2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
453c3 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 6c 61  *.** Find the la
453c4 72 67 65 73 74 20 72 65 6c 61 74 69 76 65 20 6c  rgest relative l
453c5 65 76 65 6c 20 6e 75 6d 62 65 72 20 69 6e 20 74  evel number in t
453c6 68 65 20 74 61 62 6c 65 2e 20 49 66 20 73 75 63  he table. If suc
453c7 63 65 73 73 66 75 6c 2c 20 73 65 74 0a 2a 2a 20  cessful, set.** 
453c8 2a 70 6e 4d 61 78 20 74 6f 20 74 68 69 73 20 76  *pnMax to this v
453c9 61 6c 75 65 20 61 6e 64 20 72 65 74 75 72 6e 20  alue and return 
453ca 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72  SQLITE_OK. Other
453cb 77 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72 6f  wise, if an erro
453cc 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 73 65 74  r occurs,.** set
453cd 20 2a 70 6e 4d 61 78 20 74 6f 20 7a 65 72 6f 20   *pnMax to zero 
453ce 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 53 51  and return an SQ
453cf 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e  Lite error code.
453d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
453d1 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
453d2 73 33 4d 61 78 4c 65 76 65 6c 28 46 74 73 33 54  s3MaxLevel(Fts3T
453d3 61 62 6c 65 20 2a 70 2c 20 69 6e 74 20 2a 70 6e  able *p, int *pn
453d4 4d 61 78 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Max){.  int rc;.
453d5 20 20 69 6e 74 20 6d 78 4c 65 76 65 6c 20 3d 20    int mxLevel = 
453d6 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  0;.  sqlite3_stm
453d7 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 0a 0a 20  t *pStmt = 0;.. 
453d8 20 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d   rc = fts3SqlStm
453d9 74 28 70 2c 20 53 51 4c 5f 53 45 4c 45 43 54 5f  t(p, SQL_SELECT_
453da 4d 58 4c 45 56 45 4c 2c 20 26 70 53 74 6d 74 2c  MXLEVEL, &pStmt,
453db 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53   0);.  if( rc==S
453dc 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
453dd 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d  if( SQLITE_ROW==
453de 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
453df 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 6d 78 4c  mt) ){.      mxL
453e0 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63  evel = sqlite3_c
453e1 6f 6c 75 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2c  olumn_int(pStmt,
453e2 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72   0);.    }.    r
453e3 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
453e4 74 28 70 53 74 6d 74 29 3b 0a 20 20 7d 0a 20 20  t(pStmt);.  }.  
453e5 2a 70 6e 4d 61 78 20 3d 20 6d 78 4c 65 76 65 6c  *pnMax = mxLevel
453e6 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
453e7 0a 0a 2f 2a 20 0a 2a 2a 20 49 6e 73 65 72 74 20  ../* .** Insert 
453e8 61 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68  a record into th
453e9 65 20 25 5f 73 65 67 64 69 72 20 74 61 62 6c 65  e %_segdir table
453ea 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
453eb 66 74 73 33 57 72 69 74 65 53 65 67 64 69 72 28  fts3WriteSegdir(
453ec 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c  .  Fts3Table *p,
453ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
453ee 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61     /* Virtual ta
453ef 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ble handle */.  
453f0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4c  sqlite3_int64 iL
453f1 65 76 65 6c 2c 20 20 20 20 20 20 20 20 20 20 20  evel,           
453f2 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 6c 65  /* Value for "le
453f3 76 65 6c 22 20 66 69 65 6c 64 20 28 61 62 73 6f  vel" field (abso
453f4 6c 75 74 65 20 6c 65 76 65 6c 29 20 2a 2f 0a 20  lute level) */. 
453f5 20 69 6e 74 20 69 49 64 78 2c 20 20 20 20 20 20   int iIdx,      
453f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
453f7 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 69   /* Value for "i
453f8 64 78 22 20 66 69 65 6c 64 20 2a 2f 0a 20 20 73  dx" field */.  s
453f9 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 53 74  qlite3_int64 iSt
453fa 61 72 74 42 6c 6f 63 6b 2c 20 20 20 20 20 20 2f  artBlock,      /
453fb 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 73 74 61  * Value for "sta
453fc 72 74 5f 62 6c 6f 63 6b 22 20 66 69 65 6c 64 20  rt_block" field 
453fd 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
453fe 36 34 20 69 4c 65 61 66 45 6e 64 42 6c 6f 63 6b  64 iLeafEndBlock
453ff 2c 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f  ,    /* Value fo
45400 72 20 22 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c  r "leaves_end_bl
45401 6f 63 6b 22 20 66 69 65 6c 64 20 2a 2f 0a 20 20  ock" field */.  
45402 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 45  sqlite3_int64 iE
45403 6e 64 42 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20  ndBlock,        
45404 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 65 6e  /* Value for "en
45405 64 5f 62 6c 6f 63 6b 22 20 66 69 65 6c 64 20 2a  d_block" field *
45406 2f 0a 20 20 63 68 61 72 20 2a 7a 52 6f 6f 74 2c  /.  char *zRoot,
45407 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45408 20 20 20 20 2f 2a 20 42 6c 6f 62 20 76 61 6c 75      /* Blob valu
45409 65 20 66 6f 72 20 22 72 6f 6f 74 22 20 66 69 65  e for "root" fie
4540a 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f 6f  ld */.  int nRoo
4540b 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
4540c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
4540d 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 62 75  r of bytes in bu
4540e 66 66 65 72 20 7a 52 6f 6f 74 20 2a 2f 0a 29 7b  ffer zRoot */.){
4540f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
45410 2a 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 72 63  *pStmt;.  int rc
45411 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70   = fts3SqlStmt(p
45412 2c 20 53 51 4c 5f 49 4e 53 45 52 54 5f 53 45 47  , SQL_INSERT_SEG
45413 44 49 52 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b  DIR, &pStmt, 0);
45414 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
45415 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
45416 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
45417 53 74 6d 74 2c 20 31 2c 20 69 4c 65 76 65 6c 29  Stmt, 1, iLevel)
45418 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69  ;.    sqlite3_bi
45419 6e 64 5f 69 6e 74 28 70 53 74 6d 74 2c 20 32 2c  nd_int(pStmt, 2,
4541a 20 69 49 64 78 29 3b 0a 20 20 20 20 73 71 6c 69   iIdx);.    sqli
4541b 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
4541c 53 74 6d 74 2c 20 33 2c 20 69 53 74 61 72 74 42  Stmt, 3, iStartB
4541d 6c 6f 63 6b 29 3b 0a 20 20 20 20 73 71 6c 69 74  lock);.    sqlit
4541e 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53  e3_bind_int64(pS
4541f 74 6d 74 2c 20 34 2c 20 69 4c 65 61 66 45 6e 64  tmt, 4, iLeafEnd
45420 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 73 71 6c 69  Block);.    sqli
45421 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
45422 53 74 6d 74 2c 20 35 2c 20 69 45 6e 64 42 6c 6f  Stmt, 5, iEndBlo
45423 63 6b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ck);.    sqlite3
45424 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74  _bind_blob(pStmt
45425 2c 20 36 2c 20 7a 52 6f 6f 74 2c 20 6e 52 6f 6f  , 6, zRoot, nRoo
45426 74 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  t, SQLITE_STATIC
45427 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  );.    sqlite3_s
45428 74 65 70 28 70 53 74 6d 74 29 3b 0a 20 20 20 20  tep(pStmt);.    
45429 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
4542a 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 7d 0a 20  et(pStmt);.  }. 
4542b 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
4542c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
4542d 73 69 7a 65 20 6f 66 20 74 68 65 20 63 6f 6d 6d  size of the comm
4542e 6f 6e 20 70 72 65 66 69 78 20 28 69 66 20 61 6e  on prefix (if an
4542f 79 29 20 73 68 61 72 65 64 20 62 79 20 7a 50 72  y) shared by zPr
45430 65 76 20 61 6e 64 0a 2a 2a 20 7a 4e 65 78 74 2c  ev and.** zNext,
45431 20 69 6e 20 62 79 74 65 73 2e 20 46 6f 72 20 65   in bytes. For e
45432 78 61 6d 70 6c 65 2c 20 0a 2a 2a 0a 2a 2a 20 20  xample, .**.**  
45433 20 66 74 73 33 50 72 65 66 69 78 43 6f 6d 70 72   fts3PrefixCompr
45434 65 73 73 28 22 61 62 63 22 2c 20 33 2c 20 22 61  ess("abc", 3, "a
45435 62 63 64 65 66 22 2c 20 36 29 20 20 20 2f 2f 20  bcdef", 6)   // 
45436 72 65 74 75 72 6e 73 20 33 0a 2a 2a 20 20 20 66  returns 3.**   f
45437 74 73 33 50 72 65 66 69 78 43 6f 6d 70 72 65 73  ts3PrefixCompres
45438 73 28 22 61 62 58 22 2c 20 33 2c 20 22 61 62 63  s("abX", 3, "abc
45439 64 65 66 22 2c 20 36 29 20 20 20 2f 2f 20 72 65  def", 6)   // re
4543a 74 75 72 6e 73 20 32 0a 2a 2a 20 20 20 66 74 73  turns 2.**   fts
4543b 33 50 72 65 66 69 78 43 6f 6d 70 72 65 73 73 28  3PrefixCompress(
4543c 22 61 62 58 22 2c 20 33 2c 20 22 58 62 63 64 65  "abX", 3, "Xbcde
4543d 66 22 2c 20 36 29 20 20 20 2f 2f 20 72 65 74 75  f", 6)   // retu
4543e 72 6e 73 20 30 0a 2a 2f 0a 73 74 61 74 69 63 20  rns 0.*/.static 
4543f 69 6e 74 20 66 74 73 33 50 72 65 66 69 78 43 6f  int fts3PrefixCo
45440 6d 70 72 65 73 73 28 0a 20 20 63 6f 6e 73 74 20  mpress(.  const 
45441 63 68 61 72 20 2a 7a 50 72 65 76 2c 20 20 20 20  char *zPrev,    
45442 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66            /* Buf
45443 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70  fer containing p
45444 72 65 76 69 6f 75 73 20 74 65 72 6d 20 2a 2f 0a  revious term */.
45445 20 20 69 6e 74 20 6e 50 72 65 76 2c 20 20 20 20    int nPrev,    
45446 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45447 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66    /* Size of buf
45448 66 65 72 20 7a 50 72 65 76 20 69 6e 20 62 79 74  fer zPrev in byt
45449 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  es */.  const ch
4544a 61 72 20 2a 7a 4e 65 78 74 2c 20 20 20 20 20 20  ar *zNext,      
4544b 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65          /* Buffe
4544c 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e 65 78  r containing nex
4544d 74 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20  t term */.  int 
4544e 6e 4e 65 78 74 20 20 20 20 20 20 20 20 20 20 20  nNext           
4544f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
45450 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20 7a 4e  ize of buffer zN
45451 65 78 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  ext in bytes */.
45452 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 55 4e  ){.  int n;.  UN
45453 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e  USED_PARAMETER(n
45454 4e 65 78 74 29 3b 0a 20 20 66 6f 72 28 6e 3d 30  Next);.  for(n=0
45455 3b 20 6e 3c 6e 50 72 65 76 20 26 26 20 7a 50 72  ; n<nPrev && zPr
45456 65 76 5b 6e 5d 3d 3d 7a 4e 65 78 74 5b 6e 5d 3b  ev[n]==zNext[n];
45457 20 6e 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20   n++);.  return 
45458 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20  n;.}../*.** Add 
45459 74 65 72 6d 20 7a 54 65 72 6d 20 74 6f 20 74 68  term zTerm to th
4545a 65 20 53 65 67 6d 65 6e 74 4e 6f 64 65 2e 20 49  e SegmentNode. I
4545b 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  t is guaranteed 
4545c 74 68 61 74 20 7a 54 65 72 6d 20 69 73 20 6c 61  that zTerm is la
4545d 72 67 65 72 0a 2a 2a 20 28 61 63 63 6f 72 64 69  rger.** (accordi
4545e 6e 67 20 74 6f 20 6d 65 6d 63 6d 70 29 20 74 68  ng to memcmp) th
4545f 61 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  an the previous 
45460 74 65 72 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  term..*/.static 
45461 69 6e 74 20 66 74 73 33 4e 6f 64 65 41 64 64 54  int fts3NodeAddT
45462 65 72 6d 28 0a 20 20 46 74 73 33 54 61 62 6c 65  erm(.  Fts3Table
45463 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
45464 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61         /* Virtua
45465 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  l table handle *
45466 2f 0a 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20  /.  SegmentNode 
45467 2a 2a 70 70 54 72 65 65 2c 20 20 20 20 20 20 20  **ppTree,       
45468 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53      /* IN/OUT: S
45469 65 67 6d 65 6e 74 4e 6f 64 65 20 68 61 6e 64 6c  egmentNode handl
4546a 65 20 2a 2f 20 0a 20 20 69 6e 74 20 69 73 43 6f  e */ .  int isCo
4546b 70 79 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20  pyTerm,         
4546c 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
4546d 69 66 20 7a 54 65 72 6d 2f 6e 54 65 72 6d 20 69  if zTerm/nTerm i
4546e 73 20 74 72 61 6e 73 69 65 6e 74 20 2a 2f 0a 20  s transient */. 
4546f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65   const char *zTe
45470 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rm,             
45471 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62   /* Pointer to b
45472 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  uffer containing
45473 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e   term */.  int n
45474 54 65 72 6d 20 20 20 20 20 20 20 20 20 20 20 20  Term            
45475 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
45476 7a 65 20 6f 66 20 74 65 72 6d 20 69 6e 20 62 79  ze of term in by
45477 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 53 65 67 6d  tes */.){.  Segm
45478 65 6e 74 4e 6f 64 65 20 2a 70 54 72 65 65 20 3d  entNode *pTree =
45479 20 2a 70 70 54 72 65 65 3b 0a 20 20 69 6e 74 20   *ppTree;.  int 
4547a 72 63 3b 0a 20 20 53 65 67 6d 65 6e 74 4e 6f 64  rc;.  SegmentNod
4547b 65 20 2a 70 4e 65 77 3b 0a 0a 20 20 2f 2a 20 46  e *pNew;..  /* F
4547c 69 72 73 74 20 74 72 79 20 74 6f 20 61 70 70 65  irst try to appe
4547d 6e 64 20 74 68 65 20 74 65 72 6d 20 74 6f 20 74  nd the term to t
4547e 68 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 2e  he current node.
4547f 20 52 65 74 75 72 6e 20 65 61 72 6c 79 20 69 66   Return early if
45480 20 0a 20 20 2a 2a 20 74 68 69 73 20 69 73 20 70   .  ** this is p
45481 6f 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  ossible..  */.  
45482 69 66 28 20 70 54 72 65 65 20 29 7b 0a 20 20 20  if( pTree ){.   
45483 20 69 6e 74 20 6e 44 61 74 61 20 3d 20 70 54 72   int nData = pTr
45484 65 65 2d 3e 6e 44 61 74 61 3b 20 20 20 20 20 2f  ee->nData;     /
45485 2a 20 43 75 72 72 65 6e 74 20 73 69 7a 65 20 6f  * Current size o
45486 66 20 6e 6f 64 65 20 69 6e 20 62 79 74 65 73 20  f node in bytes 
45487 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 52 65 71 20  */.    int nReq 
45488 3d 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20  = nData;        
45489 20 20 20 20 20 2f 2a 20 52 65 71 75 69 72 65 64       /* Required
4548a 20 73 70 61 63 65 20 61 66 74 65 72 20 61 64 64   space after add
4548b 69 6e 67 20 7a 54 65 72 6d 20 2a 2f 0a 20 20 20  ing zTerm */.   
4548c 20 69 6e 74 20 6e 50 72 65 66 69 78 3b 20 20 20   int nPrefix;   
4548d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4548e 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
4548f 73 20 6f 66 20 70 72 65 66 69 78 20 63 6f 6d 70  s of prefix comp
45490 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69  ression */.    i
45491 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20 20 20  nt nSuffix;     
45492 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
45493 53 75 66 66 69 78 20 6c 65 6e 67 74 68 20 2a 2f  Suffix length */
45494 0a 0a 20 20 20 20 6e 50 72 65 66 69 78 20 3d 20  ..    nPrefix = 
45495 66 74 73 33 50 72 65 66 69 78 43 6f 6d 70 72 65  fts3PrefixCompre
45496 73 73 28 70 54 72 65 65 2d 3e 7a 54 65 72 6d 2c  ss(pTree->zTerm,
45497 20 70 54 72 65 65 2d 3e 6e 54 65 72 6d 2c 20 7a   pTree->nTerm, z
45498 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20  Term, nTerm);.  
45499 20 20 6e 53 75 66 66 69 78 20 3d 20 6e 54 65 72    nSuffix = nTer
4549a 6d 2d 6e 50 72 65 66 69 78 3b 0a 0a 20 20 20 20  m-nPrefix;..    
4549b 6e 52 65 71 20 2b 3d 20 73 71 6c 69 74 65 33 46  nReq += sqlite3F
4549c 74 73 33 56 61 72 69 6e 74 4c 65 6e 28 6e 50 72  ts3VarintLen(nPr
4549d 65 66 69 78 29 2b 73 71 6c 69 74 65 33 46 74 73  efix)+sqlite3Fts
4549e 33 56 61 72 69 6e 74 4c 65 6e 28 6e 53 75 66 66  3VarintLen(nSuff
4549f 69 78 29 2b 6e 53 75 66 66 69 78 3b 0a 20 20 20  ix)+nSuffix;.   
454a0 20 69 66 28 20 6e 52 65 71 3c 3d 70 2d 3e 6e 4e   if( nReq<=p->nN
454a1 6f 64 65 53 69 7a 65 20 7c 7c 20 21 70 54 72 65  odeSize || !pTre
454a2 65 2d 3e 7a 54 65 72 6d 20 29 7b 0a 0a 20 20 20  e->zTerm ){..   
454a3 20 20 20 69 66 28 20 6e 52 65 71 3e 70 2d 3e 6e     if( nReq>p->n
454a4 4e 6f 64 65 53 69 7a 65 20 29 7b 0a 20 20 20 20  NodeSize ){.    
454a5 20 20 20 20 2f 2a 20 41 6e 20 75 6e 75 73 75 61      /* An unusua
454a6 6c 20 63 61 73 65 3a 20 74 68 69 73 20 69 73 20  l case: this is 
454a7 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 20 74  the first term t
454a8 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68  o be added to th
454a9 65 20 6e 6f 64 65 0a 20 20 20 20 20 20 20 20 2a  e node.        *
454aa 2a 20 61 6e 64 20 74 68 65 20 73 74 61 74 69 63  * and the static
454ab 20 6e 6f 64 65 20 62 75 66 66 65 72 20 28 70 2d   node buffer (p-
454ac 3e 6e 4e 6f 64 65 53 69 7a 65 20 62 79 74 65 73  >nNodeSize bytes
454ad 29 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 0a 20  ) is not large. 
454ae 20 20 20 20 20 20 20 2a 2a 20 65 6e 6f 75 67 68         ** enough
454af 2e 20 55 73 65 20 61 20 73 65 70 61 72 61 74 65  . Use a separate
454b0 6c 79 20 6d 61 6c 6c 6f 63 65 64 20 62 75 66 66  ly malloced buff
454b1 65 72 20 69 6e 73 74 65 61 64 20 54 68 69 73 20  er instead This 
454b2 77 61 73 74 65 73 0a 20 20 20 20 20 20 20 20 2a  wastes.        *
454b3 2a 20 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65 20 62  * p->nNodeSize b
454b4 79 74 65 73 2c 20 62 75 74 20 73 69 6e 63 65 20  ytes, but since 
454b5 74 68 69 73 20 73 63 65 6e 61 72 69 6f 20 6f 6e  this scenario on
454b6 6c 79 20 63 6f 6d 65 73 20 61 62 6f 75 74 20 77  ly comes about w
454b7 68 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  hen.        ** t
454b8 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
454b9 61 69 6e 20 74 77 6f 20 74 65 72 6d 73 20 74 68  ain two terms th
454ba 61 74 20 73 68 61 72 65 20 61 20 70 72 65 66 69  at share a prefi
454bb 78 20 6f 66 20 61 6c 6d 6f 73 74 20 32 4b 42 2c  x of almost 2KB,
454bc 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69   .        ** thi
454bd 73 20 69 73 20 6e 6f 74 20 65 78 70 65 63 74 65  s is not expecte
454be 64 20 74 6f 20 62 65 20 61 20 73 65 72 69 6f 75  d to be a seriou
454bf 73 20 70 72 6f 62 6c 65 6d 2e 20 0a 20 20 20 20  s problem. .    
454c0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61      */.        a
454c1 73 73 65 72 74 28 20 70 54 72 65 65 2d 3e 61 44  ssert( pTree->aD
454c2 61 74 61 3d 3d 28 63 68 61 72 20 2a 29 26 70 54  ata==(char *)&pT
454c3 72 65 65 5b 31 5d 20 29 3b 0a 20 20 20 20 20 20  ree[1] );.      
454c4 20 20 70 54 72 65 65 2d 3e 61 44 61 74 61 20 3d    pTree->aData =
454c5 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
454c6 5f 6d 61 6c 6c 6f 63 28 6e 52 65 71 29 3b 0a 20  _malloc(nReq);. 
454c7 20 20 20 20 20 20 20 69 66 28 20 21 70 54 72 65         if( !pTre
454c8 65 2d 3e 61 44 61 74 61 20 29 7b 0a 20 20 20 20  e->aData ){.    
454c9 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
454ca 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
454cb 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
454cc 20 20 20 20 69 66 28 20 70 54 72 65 65 2d 3e 7a      if( pTree->z
454cd 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  Term ){.        
454ce 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 70  /* There is no p
454cf 72 65 66 69 78 2d 6c 65 6e 67 74 68 20 66 69 65  refix-length fie
454d0 6c 64 20 66 6f 72 20 66 69 72 73 74 20 74 65 72  ld for first ter
454d1 6d 20 69 6e 20 61 20 6e 6f 64 65 20 2a 2f 0a 20  m in a node */. 
454d2 20 20 20 20 20 20 20 6e 44 61 74 61 20 2b 3d 20         nData += 
454d3 73 71 6c 69 74 65 33 46 74 73 33 50 75 74 56 61  sqlite3Fts3PutVa
454d4 72 69 6e 74 28 26 70 54 72 65 65 2d 3e 61 44 61  rint(&pTree->aDa
454d5 74 61 5b 6e 44 61 74 61 5d 2c 20 6e 50 72 65 66  ta[nData], nPref
454d6 69 78 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  ix);.      }..  
454d7 20 20 20 20 6e 44 61 74 61 20 2b 3d 20 73 71 6c      nData += sql
454d8 69 74 65 33 46 74 73 33 50 75 74 56 61 72 69 6e  ite3Fts3PutVarin
454d9 74 28 26 70 54 72 65 65 2d 3e 61 44 61 74 61 5b  t(&pTree->aData[
454da 6e 44 61 74 61 5d 2c 20 6e 53 75 66 66 69 78 29  nData], nSuffix)
454db 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  ;.      memcpy(&
454dc 70 54 72 65 65 2d 3e 61 44 61 74 61 5b 6e 44 61  pTree->aData[nDa
454dd 74 61 5d 2c 20 26 7a 54 65 72 6d 5b 6e 50 72 65  ta], &zTerm[nPre
454de 66 69 78 5d 2c 20 6e 53 75 66 66 69 78 29 3b 0a  fix], nSuffix);.
454df 20 20 20 20 20 20 70 54 72 65 65 2d 3e 6e 44 61        pTree->nDa
454e0 74 61 20 3d 20 6e 44 61 74 61 20 2b 20 6e 53 75  ta = nData + nSu
454e1 66 66 69 78 3b 0a 20 20 20 20 20 20 70 54 72 65  ffix;.      pTre
454e2 65 2d 3e 6e 45 6e 74 72 79 2b 2b 3b 0a 0a 20 20  e->nEntry++;..  
454e3 20 20 20 20 69 66 28 20 69 73 43 6f 70 79 54 65      if( isCopyTe
454e4 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  rm ){.        if
454e5 28 20 70 54 72 65 65 2d 3e 6e 4d 61 6c 6c 6f 63  ( pTree->nMalloc
454e6 3c 6e 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20  <nTerm ){.      
454e7 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d      char *zNew =
454e8 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
454e9 28 70 54 72 65 65 2d 3e 7a 4d 61 6c 6c 6f 63 2c  (pTree->zMalloc,
454ea 20 6e 54 65 72 6d 2a 32 29 3b 0a 20 20 20 20 20   nTerm*2);.     
454eb 20 20 20 20 20 69 66 28 20 21 7a 4e 65 77 20 29       if( !zNew )
454ec 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65  {.            re
454ed 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
454ee 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  M;.          }. 
454ef 20 20 20 20 20 20 20 20 20 70 54 72 65 65 2d 3e           pTree->
454f0 6e 4d 61 6c 6c 6f 63 20 3d 20 6e 54 65 72 6d 2a  nMalloc = nTerm*
454f1 32 3b 0a 20 20 20 20 20 20 20 20 20 20 70 54 72  2;.          pTr
454f2 65 65 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 7a 4e  ee->zMalloc = zN
454f3 65 77 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ew;.        }.  
454f4 20 20 20 20 20 20 70 54 72 65 65 2d 3e 7a 54 65        pTree->zTe
454f5 72 6d 20 3d 20 70 54 72 65 65 2d 3e 7a 4d 61 6c  rm = pTree->zMal
454f6 6c 6f 63 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  loc;.        mem
454f7 63 70 79 28 70 54 72 65 65 2d 3e 7a 54 65 72 6d  cpy(pTree->zTerm
454f8 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b  , zTerm, nTerm);
454f9 0a 20 20 20 20 20 20 20 20 70 54 72 65 65 2d 3e  .        pTree->
454fa 6e 54 65 72 6d 20 3d 20 6e 54 65 72 6d 3b 0a 20  nTerm = nTerm;. 
454fb 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
454fc 20 20 20 20 70 54 72 65 65 2d 3e 7a 54 65 72 6d      pTree->zTerm
454fd 20 3d 20 28 63 68 61 72 20 2a 29 7a 54 65 72 6d   = (char *)zTerm
454fe 3b 0a 20 20 20 20 20 20 20 20 70 54 72 65 65 2d  ;.        pTree-
454ff 3e 6e 54 65 72 6d 20 3d 20 6e 54 65 72 6d 3b 0a  >nTerm = nTerm;.
45500 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
45501 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
45502 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
45503 49 66 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77 73  If control flows
45504 20 74 6f 20 68 65 72 65 2c 20 69 74 20 77 61 73   to here, it was
45505 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
45506 20 61 70 70 65 6e 64 20 7a 54 65 72 6d 20 74 6f   append zTerm to
45507 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e   the.  ** curren
45508 74 20 6e 6f 64 65 2e 20 43 72 65 61 74 65 20 61  t node. Create a
45509 20 6e 65 77 20 6e 6f 64 65 20 28 61 20 72 69 67   new node (a rig
4550a 68 74 2d 73 69 62 6c 69 6e 67 20 6f 66 20 74 68  ht-sibling of th
4550b 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 29 2e  e current node).
4550c 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73  .  ** If this is
4550d 20 74 68 65 20 66 69 72 73 74 20 6e 6f 64 65 20   the first node 
4550e 69 6e 20 74 68 65 20 74 72 65 65 2c 20 74 68 65  in the tree, the
4550f 20 74 65 72 6d 20 69 73 20 61 64 64 65 64 20 74   term is added t
45510 6f 20 69 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  o it..  **.  ** 
45511 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 74  Otherwise, the t
45512 65 72 6d 20 69 73 20 6e 6f 74 20 61 64 64 65 64  erm is not added
45513 20 74 6f 20 74 68 65 20 6e 65 77 20 6e 6f 64 65   to the new node
45514 2c 20 69 74 20 69 73 20 6c 65 66 74 20 65 6d 70  , it is left emp
45515 74 79 20 66 6f 72 0a 20 20 2a 2a 20 6e 6f 77 2e  ty for.  ** now.
45516 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20 74 65   Instead, the te
45517 72 6d 20 69 73 20 69 6e 73 65 72 74 65 64 20 69  rm is inserted i
45518 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 6f  nto the parent o
45519 66 20 70 54 72 65 65 2e 20 49 66 20 70 54 72 65  f pTree. If pTre
4551a 65 20 0a 20 20 2a 2a 20 68 61 73 20 6e 6f 20 70  e .  ** has no p
4551b 61 72 65 6e 74 2c 20 6f 6e 65 20 69 73 20 63 72  arent, one is cr
4551c 65 61 74 65 64 20 68 65 72 65 2e 0a 20 20 2a 2f  eated here..  */
4551d 0a 20 20 70 4e 65 77 20 3d 20 28 53 65 67 6d 65  .  pNew = (Segme
4551e 6e 74 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65 33  ntNode *)sqlite3
4551f 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 53  _malloc(sizeof(S
45520 65 67 6d 65 6e 74 4e 6f 64 65 29 20 2b 20 70 2d  egmentNode) + p-
45521 3e 6e 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 69  >nNodeSize);.  i
45522 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20  f( !pNew ){.    
45523 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
45524 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65  MEM;.  }.  memse
45525 74 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f  t(pNew, 0, sizeo
45526 66 28 53 65 67 6d 65 6e 74 4e 6f 64 65 29 29 3b  f(SegmentNode));
45527 0a 20 20 70 4e 65 77 2d 3e 6e 44 61 74 61 20 3d  .  pNew->nData =
45528 20 31 20 2b 20 46 54 53 33 5f 56 41 52 49 4e 54   1 + FTS3_VARINT
45529 5f 4d 41 58 3b 0a 20 20 70 4e 65 77 2d 3e 61 44  _MAX;.  pNew->aD
4552a 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 26 70  ata = (char *)&p
4552b 4e 65 77 5b 31 5d 3b 0a 0a 20 20 69 66 28 20 70  New[1];..  if( p
4552c 54 72 65 65 20 29 7b 0a 20 20 20 20 53 65 67 6d  Tree ){.    Segm
4552d 65 6e 74 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74  entNode *pParent
4552e 20 3d 20 70 54 72 65 65 2d 3e 70 50 61 72 65 6e   = pTree->pParen
4552f 74 3b 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33  t;.    rc = fts3
45530 4e 6f 64 65 41 64 64 54 65 72 6d 28 70 2c 20 26  NodeAddTerm(p, &
45531 70 50 61 72 65 6e 74 2c 20 69 73 43 6f 70 79 54  pParent, isCopyT
45532 65 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72  erm, zTerm, nTer
45533 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 54 72 65  m);.    if( pTre
45534 65 2d 3e 70 50 61 72 65 6e 74 3d 3d 30 20 29 7b  e->pParent==0 ){
45535 0a 20 20 20 20 20 20 70 54 72 65 65 2d 3e 70 50  .      pTree->pP
45536 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e 74 3b  arent = pParent;
45537 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 72 65 65  .    }.    pTree
45538 2d 3e 70 52 69 67 68 74 20 3d 20 70 4e 65 77 3b  ->pRight = pNew;
45539 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74  .    pNew->pLeft
4553a 6d 6f 73 74 20 3d 20 70 54 72 65 65 2d 3e 70 4c  most = pTree->pL
4553b 65 66 74 6d 6f 73 74 3b 0a 20 20 20 20 70 4e 65  eftmost;.    pNe
4553c 77 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61  w->pParent = pPa
4553d 72 65 6e 74 3b 0a 20 20 20 20 70 4e 65 77 2d 3e  rent;.    pNew->
4553e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 54 72 65 65 2d  zMalloc = pTree-
4553f 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 70 4e  >zMalloc;.    pN
45540 65 77 2d 3e 6e 4d 61 6c 6c 6f 63 20 3d 20 70 54  ew->nMalloc = pT
45541 72 65 65 2d 3e 6e 4d 61 6c 6c 6f 63 3b 0a 20 20  ree->nMalloc;.  
45542 20 20 70 54 72 65 65 2d 3e 7a 4d 61 6c 6c 6f 63    pTree->zMalloc
45543 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
45544 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 6d 6f     pNew->pLeftmo
45545 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 72  st = pNew;.    r
45546 63 20 3d 20 66 74 73 33 4e 6f 64 65 41 64 64 54  c = fts3NodeAddT
45547 65 72 6d 28 70 2c 20 26 70 4e 65 77 2c 20 69 73  erm(p, &pNew, is
45548 43 6f 70 79 54 65 72 6d 2c 20 7a 54 65 72 6d 2c  CopyTerm, zTerm,
45549 20 6e 54 65 72 6d 29 3b 20 0a 20 20 7d 0a 0a 20   nTerm); .  }.. 
4554a 20 2a 70 70 54 72 65 65 20 3d 20 70 4e 65 77 3b   *ppTree = pNew;
4554b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
4554c 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75  ./*.** Helper fu
4554d 6e 63 74 69 6f 6e 20 66 6f 72 20 66 74 73 33 4e  nction for fts3N
4554e 6f 64 65 57 72 69 74 65 28 29 2e 0a 2a 2f 0a 73  odeWrite()..*/.s
4554f 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 54 72  tatic int fts3Tr
45550 65 65 46 69 6e 69 73 68 4e 6f 64 65 28 0a 20 20  eeFinishNode(.  
45551 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 54 72  SegmentNode *pTr
45552 65 65 2c 20 0a 20 20 69 6e 74 20 69 48 65 69 67  ee, .  int iHeig
45553 68 74 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 69  ht, .  sqlite3_i
45554 6e 74 36 34 20 69 4c 65 66 74 43 68 69 6c 64 0a  nt64 iLeftChild.
45555 29 7b 0a 20 20 69 6e 74 20 6e 53 74 61 72 74 3b  ){.  int nStart;
45556 0a 20 20 61 73 73 65 72 74 28 20 69 48 65 69 67  .  assert( iHeig
45557 68 74 3e 3d 31 20 26 26 20 69 48 65 69 67 68 74  ht>=1 && iHeight
45558 3c 31 32 38 20 29 3b 0a 20 20 6e 53 74 61 72 74  <128 );.  nStart
45559 20 3d 20 46 54 53 33 5f 56 41 52 49 4e 54 5f 4d   = FTS3_VARINT_M
4555a 41 58 20 2d 20 73 71 6c 69 74 65 33 46 74 73 33  AX - sqlite3Fts3
4555b 56 61 72 69 6e 74 4c 65 6e 28 69 4c 65 66 74 43  VarintLen(iLeftC
4555c 68 69 6c 64 29 3b 0a 20 20 70 54 72 65 65 2d 3e  hild);.  pTree->
4555d 61 44 61 74 61 5b 6e 53 74 61 72 74 5d 20 3d 20  aData[nStart] = 
4555e 28 63 68 61 72 29 69 48 65 69 67 68 74 3b 0a 20  (char)iHeight;. 
4555f 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74 56   sqlite3Fts3PutV
45560 61 72 69 6e 74 28 26 70 54 72 65 65 2d 3e 61 44  arint(&pTree->aD
45561 61 74 61 5b 6e 53 74 61 72 74 2b 31 5d 2c 20 69  ata[nStart+1], i
45562 4c 65 66 74 43 68 69 6c 64 29 3b 0a 20 20 72 65  LeftChild);.  re
45563 74 75 72 6e 20 6e 53 74 61 72 74 3b 0a 7d 0a 0a  turn nStart;.}..
45564 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  /*.** Write the 
45565 62 75 66 66 65 72 20 66 6f 72 20 74 68 65 20 73  buffer for the s
45566 65 67 6d 65 6e 74 20 6e 6f 64 65 20 70 54 72 65  egment node pTre
45567 65 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73  e and all of its
45568 20 70 65 65 72 73 20 74 6f 20 74 68 65 0a 2a 2a   peers to the.**
45569 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 6e 20   database. Then 
4556a 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69  call this functi
4556b 6f 6e 20 72 65 63 75 72 73 69 76 65 6c 79 20 74  on recursively t
4556c 6f 20 77 72 69 74 65 20 74 68 65 20 70 61 72 65  o write the pare
4556d 6e 74 20 6f 66 20 0a 2a 2a 20 70 54 72 65 65 20  nt of .** pTree 
4556e 61 6e 64 20 69 74 73 20 70 65 65 72 73 20 74 6f  and its peers to
4556f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 0a   the database. .
45570 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66  **.** Except, if
45571 20 70 54 72 65 65 20 69 73 20 61 20 72 6f 6f 74   pTree is a root
45572 20 6e 6f 64 65 2c 20 64 6f 20 6e 6f 74 20 77 72   node, do not wr
45573 69 74 65 20 69 74 20 74 6f 20 74 68 65 20 64 61  ite it to the da
45574 74 61 62 61 73 65 2e 20 49 6e 73 74 65 61 64 2c  tabase. Instead,
45575 0a 2a 2a 20 73 65 74 20 6f 75 74 70 75 74 20 76  .** set output v
45576 61 72 69 61 62 6c 65 73 20 2a 70 61 52 6f 6f 74  ariables *paRoot
45577 20 61 6e 64 20 2a 70 6e 52 6f 6f 74 20 74 6f 20   and *pnRoot to 
45578 63 6f 6e 74 61 69 6e 20 74 68 65 20 72 6f 6f 74  contain the root
45579 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   node..**.** If 
4557a 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49  successful, SQLI
4557b 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
4557c 64 20 61 6e 64 20 6f 75 74 70 75 74 20 76 61 72  d and output var
4557d 69 61 62 6c 65 20 2a 70 69 4c 61 73 74 20 69 73  iable *piLast is
4557e 0a 2a 2a 20 73 65 74 20 74 6f 20 74 68 65 20 6c  .** set to the l
4557f 61 72 67 65 73 74 20 62 6c 6f 63 6b 69 64 20 77  argest blockid w
45580 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61  ritten to the da
45581 74 61 62 61 73 65 20 28 6f 72 20 7a 65 72 6f 20  tabase (or zero 
45582 69 66 20 6e 6f 0a 2a 2a 20 62 6c 6f 63 6b 73 20  if no.** blocks 
45583 77 65 72 65 20 77 72 69 74 74 65 6e 20 74 6f 20  were written to 
45584 74 68 65 20 64 62 29 2e 20 4f 74 68 65 72 77 69  the db). Otherwi
45585 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65 72  se, an SQLite er
45586 72 6f 72 20 63 6f 64 65 20 69 73 20 0a 2a 2a 20  ror code is .** 
45587 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61  returned..*/.sta
45588 74 69 63 20 69 6e 74 20 66 74 73 33 4e 6f 64 65  tic int fts3Node
45589 57 72 69 74 65 28 0a 20 20 46 74 73 33 54 61 62  Write(.  Fts3Tab
4558a 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  le *p,          
4558b 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
4558c 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
4558d 20 2a 2f 0a 20 20 53 65 67 6d 65 6e 74 4e 6f 64   */.  SegmentNod
4558e 65 20 2a 70 54 72 65 65 2c 20 20 20 20 20 20 20  e *pTree,       
4558f 20 20 20 20 20 20 2f 2a 20 53 65 67 6d 65 6e 74        /* Segment
45590 4e 6f 64 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  Node handle */. 
45591 20 69 6e 74 20 69 48 65 69 67 68 74 2c 20 20 20   int iHeight,   
45592 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45593 20 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 74 68   /* Height of th
45594 69 73 20 6e 6f 64 65 20 69 6e 20 74 72 65 65 20  is node in tree 
45595 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
45596 36 34 20 69 4c 65 61 66 2c 20 20 20 20 20 20 20  64 iLeaf,       
45597 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b 20 69 64       /* Block id
45598 20 6f 66 20 66 69 72 73 74 20 6c 65 61 66 20 6e   of first leaf n
45599 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ode */.  sqlite3
4559a 5f 69 6e 74 36 34 20 69 46 72 65 65 2c 20 20 20  _int64 iFree,   
4559b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63           /* Bloc
4559c 6b 20 69 64 20 6f 66 20 6e 65 78 74 20 66 72 65  k id of next fre
4559d 65 20 73 6c 6f 74 20 69 6e 20 25 5f 73 65 67 6d  e slot in %_segm
4559e 65 6e 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ents */.  sqlite
4559f 33 5f 69 6e 74 36 34 20 2a 70 69 4c 61 73 74 2c  3_int64 *piLast,
455a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
455a1 3a 20 42 6c 6f 63 6b 20 69 64 20 6f 66 20 6c 61  : Block id of la
455a2 73 74 20 65 6e 74 72 79 20 77 72 69 74 74 65 6e  st entry written
455a3 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 61 52   */.  char **paR
455a4 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  oot,            
455a5 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 44 61        /* OUT: Da
455a6 74 61 20 66 6f 72 20 72 6f 6f 74 20 6e 6f 64 65  ta for root node
455a7 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f 6f   */.  int *pnRoo
455a8 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
455a9 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 69        /* OUT: Si
455aa 7a 65 20 6f 66 20 72 6f 6f 74 20 6e 6f 64 65 20  ze of root node 
455ab 69 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20  in bytes */.){. 
455ac 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
455ad 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 21 70 54 72  _OK;..  if( !pTr
455ae 65 65 2d 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20  ee->pParent ){. 
455af 20 20 20 2f 2a 20 52 6f 6f 74 20 6e 6f 64 65 20     /* Root node 
455b0 6f 66 20 74 68 65 20 74 72 65 65 2e 20 2a 2f 0a  of the tree. */.
455b1 20 20 20 20 69 6e 74 20 6e 53 74 61 72 74 20 3d      int nStart =
455b2 20 66 74 73 33 54 72 65 65 46 69 6e 69 73 68 4e   fts3TreeFinishN
455b3 6f 64 65 28 70 54 72 65 65 2c 20 69 48 65 69 67  ode(pTree, iHeig
455b4 68 74 2c 20 69 4c 65 61 66 29 3b 0a 20 20 20 20  ht, iLeaf);.    
455b5 2a 70 69 4c 61 73 74 20 3d 20 69 46 72 65 65 2d  *piLast = iFree-
455b6 31 3b 0a 20 20 20 20 2a 70 6e 52 6f 6f 74 20 3d  1;.    *pnRoot =
455b7 20 70 54 72 65 65 2d 3e 6e 44 61 74 61 20 2d 20   pTree->nData - 
455b8 6e 53 74 61 72 74 3b 0a 20 20 20 20 2a 70 61 52  nStart;.    *paR
455b9 6f 6f 74 20 3d 20 26 70 54 72 65 65 2d 3e 61 44  oot = &pTree->aD
455ba 61 74 61 5b 6e 53 74 61 72 74 5d 3b 0a 20 20 7d  ata[nStart];.  }
455bb 65 6c 73 65 7b 0a 20 20 20 20 53 65 67 6d 65 6e  else{.    Segmen
455bc 74 4e 6f 64 65 20 2a 70 49 74 65 72 3b 0a 20 20  tNode *pIter;.  
455bd 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
455be 69 4e 65 78 74 46 72 65 65 20 3d 20 69 46 72 65  iNextFree = iFre
455bf 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69  e;.    sqlite3_i
455c0 6e 74 36 34 20 69 4e 65 78 74 4c 65 61 66 20 3d  nt64 iNextLeaf =
455c1 20 69 4c 65 61 66 3b 0a 20 20 20 20 66 6f 72 28   iLeaf;.    for(
455c2 70 49 74 65 72 3d 70 54 72 65 65 2d 3e 70 4c 65  pIter=pTree->pLe
455c3 66 74 6d 6f 73 74 3b 20 70 49 74 65 72 20 26 26  ftmost; pIter &&
455c4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20   rc==SQLITE_OK; 
455c5 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 52 69  pIter=pIter->pRi
455c6 67 68 74 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  ght){.      int 
455c7 6e 53 74 61 72 74 20 3d 20 66 74 73 33 54 72 65  nStart = fts3Tre
455c8 65 46 69 6e 69 73 68 4e 6f 64 65 28 70 49 74 65  eFinishNode(pIte
455c9 72 2c 20 69 48 65 69 67 68 74 2c 20 69 4e 65 78  r, iHeight, iNex
455ca 74 4c 65 61 66 29 3b 0a 20 20 20 20 20 20 69 6e  tLeaf);.      in
455cb 74 20 6e 57 72 69 74 65 20 3d 20 70 49 74 65 72  t nWrite = pIter
455cc 2d 3e 6e 44 61 74 61 20 2d 20 6e 53 74 61 72 74  ->nData - nStart
455cd 3b 0a 20 20 0a 20 20 20 20 20 20 72 63 20 3d 20  ;.  .      rc = 
455ce 66 74 73 33 57 72 69 74 65 53 65 67 6d 65 6e 74  fts3WriteSegment
455cf 28 70 2c 20 69 4e 65 78 74 46 72 65 65 2c 20 26  (p, iNextFree, &
455d0 70 49 74 65 72 2d 3e 61 44 61 74 61 5b 6e 53 74  pIter->aData[nSt
455d1 61 72 74 5d 2c 20 6e 57 72 69 74 65 29 3b 0a 20  art], nWrite);. 
455d2 20 20 20 20 20 69 4e 65 78 74 46 72 65 65 2b 2b       iNextFree++
455d3 3b 0a 20 20 20 20 20 20 69 4e 65 78 74 4c 65 61  ;.      iNextLea
455d4 66 20 2b 3d 20 28 70 49 74 65 72 2d 3e 6e 45 6e  f += (pIter->nEn
455d5 74 72 79 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 20  try+1);.    }.  
455d6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
455d7 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73  _OK ){.      ass
455d8 65 72 74 28 20 69 4e 65 78 74 4c 65 61 66 3d 3d  ert( iNextLeaf==
455d9 69 46 72 65 65 20 29 3b 0a 20 20 20 20 20 20 72  iFree );.      r
455da 63 20 3d 20 66 74 73 33 4e 6f 64 65 57 72 69 74  c = fts3NodeWrit
455db 65 28 0a 20 20 20 20 20 20 20 20 20 20 70 2c 20  e(.          p, 
455dc 70 54 72 65 65 2d 3e 70 50 61 72 65 6e 74 2c 20  pTree->pParent, 
455dd 69 48 65 69 67 68 74 2b 31 2c 20 69 46 72 65 65  iHeight+1, iFree
455de 2c 20 69 4e 65 78 74 46 72 65 65 2c 20 70 69 4c  , iNextFree, piL
455df 61 73 74 2c 20 70 61 52 6f 6f 74 2c 20 70 6e 52  ast, paRoot, pnR
455e0 6f 6f 74 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  oot.      );.   
455e1 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
455e2 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72   rc;.}../*.** Fr
455e3 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c  ee all memory al
455e4 6c 6f 63 61 74 69 6f 6e 73 20 61 73 73 6f 63 69  locations associ
455e5 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74 72  ated with the tr
455e6 65 65 20 70 54 72 65 65 2e 0a 2a 2f 0a 73 74 61  ee pTree..*/.sta
455e7 74 69 63 20 76 6f 69 64 20 66 74 73 33 4e 6f 64  tic void fts3Nod
455e8 65 46 72 65 65 28 53 65 67 6d 65 6e 74 4e 6f 64  eFree(SegmentNod
455e9 65 20 2a 70 54 72 65 65 29 7b 0a 20 20 69 66 28  e *pTree){.  if(
455ea 20 70 54 72 65 65 20 29 7b 0a 20 20 20 20 53 65   pTree ){.    Se
455eb 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 20 3d 20 70  gmentNode *p = p
455ec 54 72 65 65 2d 3e 70 4c 65 66 74 6d 6f 73 74 3b  Tree->pLeftmost;
455ed 0a 20 20 20 20 66 74 73 33 4e 6f 64 65 46 72 65  .    fts3NodeFre
455ee 65 28 70 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20  e(p->pParent);. 
455ef 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20     while( p ){. 
455f0 20 20 20 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65       SegmentNode
455f1 20 2a 70 52 69 67 68 74 20 3d 20 70 2d 3e 70 52   *pRight = p->pR
455f2 69 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28 20  ight;.      if( 
455f3 70 2d 3e 61 44 61 74 61 21 3d 28 63 68 61 72 20  p->aData!=(char 
455f4 2a 29 26 70 5b 31 5d 20 29 7b 0a 20 20 20 20 20  *)&p[1] ){.     
455f5 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
455f6 70 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 20  p->aData);.     
455f7 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
455f8 20 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 2d   pRight==0 || p-
455f9 3e 7a 4d 61 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20  >zMalloc==0 );. 
455fa 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
455fb 65 28 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20  e(p->zMalloc);. 
455fc 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
455fd 65 28 70 29 3b 0a 20 20 20 20 20 20 70 20 3d 20  e(p);.      p = 
455fe 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 0a 20 20  pRight;.    }.  
455ff 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  }.}../*.** Add a
45600 20 74 65 72 6d 20 74 6f 20 74 68 65 20 73 65 67   term to the seg
45601 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 6e 73 74  ment being const
45602 72 75 63 74 65 64 20 62 79 20 74 68 65 20 53 65  ructed by the Se
45603 67 6d 65 6e 74 57 72 69 74 65 72 20 6f 62 6a 65  gmentWriter obje
45604 63 74 0a 2a 2a 20 2a 70 70 57 72 69 74 65 72 2e  ct.** *ppWriter.
45605 20 57 68 65 6e 20 61 64 64 69 6e 67 20 74 68 65   When adding the
45606 20 66 69 72 73 74 20 74 65 72 6d 20 74 6f 20 61   first term to a
45607 20 73 65 67 6d 65 6e 74 2c 20 2a 70 70 57 72 69   segment, *ppWri
45608 74 65 72 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65  ter should.** be
45609 20 70 61 73 73 65 64 20 4e 55 4c 4c 2e 20 54 68   passed NULL. Th
4560a 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c  is function will
4560b 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   allocate a new 
4560c 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20 6f 62  SegmentWriter ob
4560d 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 72 65 74 75  ject.** and retu
4560e 72 6e 20 69 74 20 76 69 61 20 74 68 65 20 69 6e  rn it via the in
4560f 70 75 74 2f 6f 75 74 70 75 74 20 76 61 72 69 61  put/output varia
45610 62 6c 65 20 2a 70 70 57 72 69 74 65 72 20 69 6e  ble *ppWriter in
45611 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a   this case..**.*
45612 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  * If successful,
45613 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
45614 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  turned. Otherwis
45615 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  e, an SQLite err
45616 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74  or code..*/.stat
45617 69 63 20 69 6e 74 20 66 74 73 33 53 65 67 57 72  ic int fts3SegWr
45618 69 74 65 72 41 64 64 28 0a 20 20 46 74 73 33 54  iterAdd(.  Fts3T
45619 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20  able *p,        
4561a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69             /* Vi
4561b 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64  rtual table hand
4561c 6c 65 20 2a 2f 0a 20 20 53 65 67 6d 65 6e 74 57  le */.  SegmentW
4561d 72 69 74 65 72 20 2a 2a 70 70 57 72 69 74 65 72  riter **ppWriter
4561e 2c 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55  ,       /* IN/OU
4561f 54 3a 20 53 65 67 6d 65 6e 74 57 72 69 74 65 72  T: SegmentWriter
45620 20 68 61 6e 64 6c 65 20 2a 2f 20 0a 20 20 69 6e   handle */ .  in
45621 74 20 69 73 43 6f 70 79 54 65 72 6d 2c 20 20 20  t isCopyTerm,   
45622 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
45623 20 54 72 75 65 20 69 66 20 62 75 66 66 65 72 20   True if buffer 
45624 7a 54 65 72 6d 20 6d 75 73 74 20 62 65 20 63 6f  zTerm must be co
45625 70 69 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  pied */.  const 
45626 63 68 61 72 20 2a 7a 54 65 72 6d 2c 20 20 20 20  char *zTerm,    
45627 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
45628 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72 20 63  nter to buffer c
45629 6f 6e 74 61 69 6e 69 6e 67 20 74 65 72 6d 20 2a  ontaining term *
4562a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c 20 20  /.  int nTerm,  
4562b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4562c 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
4562d 65 72 6d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  erm in bytes */.
4562e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 44    const char *aD
4562f 6f 63 6c 69 73 74 2c 20 20 20 20 20 20 20 20 20  oclist,         
45630 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
45631 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e  buffer containin
45632 67 20 64 6f 63 6c 69 73 74 20 2a 2f 0a 20 20 69  g doclist */.  i
45633 6e 74 20 6e 44 6f 63 6c 69 73 74 20 20 20 20 20  nt nDoclist     
45634 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
45635 2a 20 53 69 7a 65 20 6f 66 20 64 6f 63 6c 69 73  * Size of doclis
45636 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b  t in bytes */.){
45637 0a 20 20 69 6e 74 20 6e 50 72 65 66 69 78 3b 20  .  int nPrefix; 
45638 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45639 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 65     /* Size of te
4563a 72 6d 20 70 72 65 66 69 78 20 69 6e 20 62 79 74  rm prefix in byt
4563b 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 66  es */.  int nSuf
4563c 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  fix;            
4563d 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
4563e 6f 66 20 74 65 72 6d 20 73 75 66 66 69 78 20 69  of term suffix i
4563f 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74  n bytes */.  int
45640 20 6e 52 65 71 3b 20 20 20 20 20 20 20 20 20 20   nReq;          
45641 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
45642 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
45643 72 65 71 75 69 72 65 64 20 6f 6e 20 6c 65 61 66  required on leaf
45644 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   page */.  int n
45645 44 61 74 61 3b 0a 20 20 53 65 67 6d 65 6e 74 57  Data;.  SegmentW
45646 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 20 3d  riter *pWriter =
45647 20 2a 70 70 57 72 69 74 65 72 3b 0a 0a 20 20 69   *ppWriter;..  i
45648 66 28 20 21 70 57 72 69 74 65 72 20 29 7b 0a 20  f( !pWriter ){. 
45649 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 73     int rc;.    s
4564a 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
4564b 6d 74 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f  mt;..    /* Allo
4564c 63 61 74 65 20 74 68 65 20 53 65 67 6d 65 6e 74  cate the Segment
4564d 57 72 69 74 65 72 20 73 74 72 75 63 74 75 72 65  Writer structure
4564e 20 2a 2f 0a 20 20 20 20 70 57 72 69 74 65 72 20   */.    pWriter 
4564f 3d 20 28 53 65 67 6d 65 6e 74 57 72 69 74 65 72  = (SegmentWriter
45650 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
45651 63 28 73 69 7a 65 6f 66 28 53 65 67 6d 65 6e 74  c(sizeof(Segment
45652 57 72 69 74 65 72 29 29 3b 0a 20 20 20 20 69 66  Writer));.    if
45653 28 20 21 70 57 72 69 74 65 72 20 29 20 72 65 74  ( !pWriter ) ret
45654 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
45655 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 57 72  ;.    memset(pWr
45656 69 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  iter, 0, sizeof(
45657 53 65 67 6d 65 6e 74 57 72 69 74 65 72 29 29 3b  SegmentWriter));
45658 0a 20 20 20 20 2a 70 70 57 72 69 74 65 72 20 3d  .    *ppWriter =
45659 20 70 57 72 69 74 65 72 3b 0a 0a 20 20 20 20 2f   pWriter;..    /
4565a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 62 75 66  * Allocate a buf
4565b 66 65 72 20 69 6e 20 77 68 69 63 68 20 74 6f 20  fer in which to 
4565c 61 63 63 75 6d 75 6c 61 74 65 20 64 61 74 61 20  accumulate data 
4565d 2a 2f 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  */.    pWriter->
4565e 61 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29  aData = (char *)
4565f 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 70  sqlite3_malloc(p
45660 2d 3e 6e 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20  ->nNodeSize);.  
45661 20 20 69 66 28 20 21 70 57 72 69 74 65 72 2d 3e    if( !pWriter->
45662 61 44 61 74 61 20 29 20 72 65 74 75 72 6e 20 53  aData ) return S
45663 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
45664 20 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a 65 20   pWriter->nSize 
45665 3d 20 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65 3b 0a  = p->nNodeSize;.
45666 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65  .    /* Find the
45667 20 6e 65 78 74 20 66 72 65 65 20 62 6c 6f 63 6b   next free block
45668 69 64 20 69 6e 20 74 68 65 20 25 5f 73 65 67 6d  id in the %_segm
45669 65 6e 74 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  ents table */.  
4566a 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74    rc = fts3SqlSt
4566b 6d 74 28 70 2c 20 53 51 4c 5f 4e 45 58 54 5f 53  mt(p, SQL_NEXT_S
4566c 45 47 4d 45 4e 54 53 5f 49 44 2c 20 26 70 53 74  EGMENTS_ID, &pSt
4566d 6d 74 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  mt, 0);.    if( 
4566e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
4566f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 69  return rc;.    i
45670 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73  f( SQLITE_ROW==s
45671 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d  qlite3_step(pStm
45672 74 29 20 29 7b 0a 20 20 20 20 20 20 70 57 72 69  t) ){.      pWri
45673 74 65 72 2d 3e 69 46 72 65 65 20 3d 20 73 71 6c  ter->iFree = sql
45674 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
45675 34 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20  4(pStmt, 0);.   
45676 20 20 20 70 57 72 69 74 65 72 2d 3e 69 46 69 72     pWriter->iFir
45677 73 74 20 3d 20 70 57 72 69 74 65 72 2d 3e 69 46  st = pWriter->iF
45678 72 65 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  ree;.    }.    r
45679 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
4567a 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 69 66  t(pStmt);.    if
4567b 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
4567c 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
4567d 0a 20 20 6e 44 61 74 61 20 3d 20 70 57 72 69 74  .  nData = pWrit
4567e 65 72 2d 3e 6e 44 61 74 61 3b 0a 0a 20 20 6e 50  er->nData;..  nP
4567f 72 65 66 69 78 20 3d 20 66 74 73 33 50 72 65 66  refix = fts3Pref
45680 69 78 43 6f 6d 70 72 65 73 73 28 70 57 72 69 74  ixCompress(pWrit
45681 65 72 2d 3e 7a 54 65 72 6d 2c 20 70 57 72 69 74  er->zTerm, pWrit
45682 65 72 2d 3e 6e 54 65 72 6d 2c 20 7a 54 65 72 6d  er->nTerm, zTerm
45683 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 6e 53 75 66  , nTerm);.  nSuf
45684 66 69 78 20 3d 20 6e 54 65 72 6d 2d 6e 50 72 65  fix = nTerm-nPre
45685 66 69 78 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72  fix;..  /* Figur
45686 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 62  e out how many b
45687 79 74 65 73 20 61 72 65 20 72 65 71 75 69 72 65  ytes are require
45688 64 20 62 79 20 74 68 69 73 20 6e 65 77 20 65 6e  d by this new en
45689 74 72 79 20 2a 2f 0a 20 20 6e 52 65 71 20 3d 20  try */.  nReq = 
4568a 73 71 6c 69 74 65 33 46 74 73 33 56 61 72 69 6e  sqlite3Fts3Varin
4568b 74 4c 65 6e 28 6e 50 72 65 66 69 78 29 20 2b 20  tLen(nPrefix) + 
4568c 20 20 20 2f 2a 20 76 61 72 69 6e 74 20 63 6f 6e     /* varint con
4568d 74 61 69 6e 69 6e 67 20 70 72 65 66 69 78 20 73  taining prefix s
4568e 69 7a 65 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  ize */.    sqlit
4568f 65 33 46 74 73 33 56 61 72 69 6e 74 4c 65 6e 28  e3Fts3VarintLen(
45690 6e 53 75 66 66 69 78 29 20 2b 20 20 20 20 20 20  nSuffix) +      
45691 20 20 20 2f 2a 20 76 61 72 69 6e 74 20 63 6f 6e     /* varint con
45692 74 61 69 6e 69 6e 67 20 73 75 66 66 69 78 20 73  taining suffix s
45693 69 7a 65 20 2a 2f 0a 20 20 20 20 6e 53 75 66 66  ize */.    nSuff
45694 69 78 20 2b 20 20 20 20 20 20 20 20 20 20 20 20  ix +            
45695 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45696 20 20 20 2f 2a 20 54 65 72 6d 20 73 75 66 66 69     /* Term suffi
45697 78 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  x */.    sqlite3
45698 46 74 73 33 56 61 72 69 6e 74 4c 65 6e 28 6e 44  Fts3VarintLen(nD
45699 6f 63 6c 69 73 74 29 20 2b 20 20 20 20 20 20 20  oclist) +       
4569a 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 6f 63 6c   /* Size of docl
4569b 69 73 74 20 2a 2f 0a 20 20 20 20 6e 44 6f 63 6c  ist */.    nDocl
4569c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ist;            
4569d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4569e 20 20 20 2f 2a 20 44 6f 63 6c 69 73 74 20 64 61     /* Doclist da
4569f 74 61 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 44 61  ta */..  if( nDa
456a0 74 61 3e 30 20 26 26 20 6e 44 61 74 61 2b 6e 52  ta>0 && nData+nR
456a1 65 71 3e 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65 20  eq>p->nNodeSize 
456a2 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 0a  ){.    int rc;..
456a3 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 72 65      /* The curre
456a4 6e 74 20 6c 65 61 66 20 6e 6f 64 65 20 69 73 20  nt leaf node is 
456a5 66 75 6c 6c 2e 20 57 72 69 74 65 20 69 74 20 6f  full. Write it o
456a6 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ut to the databa
456a7 73 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  se. */.    rc = 
456a8 66 74 73 33 57 72 69 74 65 53 65 67 6d 65 6e 74  fts3WriteSegment
456a9 28 70 2c 20 70 57 72 69 74 65 72 2d 3e 69 46 72  (p, pWriter->iFr
456aa 65 65 2b 2b 2c 20 70 57 72 69 74 65 72 2d 3e 61  ee++, pWriter->a
456ab 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20 20  Data, nData);.  
456ac 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
456ad 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
456ae 0a 20 20 20 20 70 2d 3e 6e 4c 65 61 66 41 64 64  .    p->nLeafAdd
456af 2b 2b 3b 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20  ++;..    /* Add 
456b0 74 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d  the current term
456b1 20 74 6f 20 74 68 65 20 69 6e 74 65 72 69 6f 72   to the interior
456b2 20 6e 6f 64 65 20 74 72 65 65 2e 20 54 68 65 20   node tree. The 
456b3 74 65 72 6d 20 61 64 64 65 64 20 74 6f 0a 20 20  term added to.  
456b4 20 20 2a 2a 20 74 68 65 20 69 6e 74 65 72 69 6f    ** the interio
456b5 72 20 74 72 65 65 20 6d 75 73 74 3a 0a 20 20 20  r tree must:.   
456b6 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 61 29 20   **.    **   a) 
456b7 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  be greater than 
456b8 74 68 65 20 6c 61 72 67 65 73 74 20 74 65 72 6d  the largest term
456b9 20 6f 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64   on the leaf nod
456ba 65 20 6a 75 73 74 20 77 72 69 74 74 65 6e 0a 20  e just written. 
456bb 20 20 20 2a 2a 20 20 20 20 20 20 74 6f 20 74 68     **      to th
456bc 65 20 64 61 74 61 62 61 73 65 20 28 73 74 69 6c  e database (stil
456bd 6c 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 70  l available in p
456be 57 72 69 74 65 72 2d 3e 7a 54 65 72 6d 29 2c 20  Writer->zTerm), 
456bf 61 6e 64 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  and.    **.    *
456c0 2a 20 20 20 62 29 20 62 65 20 6c 65 73 73 20 74  *   b) be less t
456c1 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
456c2 74 68 65 20 74 65 72 6d 20 61 62 6f 75 74 20 74  the term about t
456c3 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68  o be added to th
456c4 65 20 6e 65 77 0a 20 20 20 20 2a 2a 20 20 20 20  e new.    **    
456c5 20 20 6c 65 61 66 20 6e 6f 64 65 20 28 7a 54 65    leaf node (zTe
456c6 72 6d 2f 6e 54 65 72 6d 29 2e 0a 20 20 20 20 2a  rm/nTerm)..    *
456c7 2a 0a 20 20 20 20 2a 2a 20 49 6e 20 6f 74 68 65  *.    ** In othe
456c8 72 20 77 6f 72 64 73 2c 20 69 74 20 6d 75 73 74  r words, it must
456c9 20 62 65 20 74 68 65 20 70 72 65 66 69 78 20 6f   be the prefix o
456ca 66 20 7a 54 65 72 6d 20 31 20 62 79 74 65 20 6c  f zTerm 1 byte l
456cb 6f 6e 67 65 72 20 74 68 61 6e 0a 20 20 20 20 2a  onger than.    *
456cc 2a 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65  * the common pre
456cd 66 69 78 20 28 69 66 20 61 6e 79 29 20 6f 66 20  fix (if any) of 
456ce 7a 54 65 72 6d 20 61 6e 64 20 70 57 72 69 74 65  zTerm and pWrite
456cf 72 2d 3e 7a 54 65 72 6d 2e 0a 20 20 20 20 2a 2f  r->zTerm..    */
456d0 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 50 72  .    assert( nPr
456d1 65 66 69 78 3c 6e 54 65 72 6d 20 29 3b 0a 20 20  efix<nTerm );.  
456d2 20 20 72 63 20 3d 20 66 74 73 33 4e 6f 64 65 41    rc = fts3NodeA
456d3 64 64 54 65 72 6d 28 70 2c 20 26 70 57 72 69 74  ddTerm(p, &pWrit
456d4 65 72 2d 3e 70 54 72 65 65 2c 20 69 73 43 6f 70  er->pTree, isCop
456d5 79 54 65 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 50  yTerm, zTerm, nP
456d6 72 65 66 69 78 2b 31 29 3b 0a 20 20 20 20 69 66  refix+1);.    if
456d7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
456d8 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
456d9 20 20 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 20    nData = 0;.   
456da 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d 20   pWriter->nTerm 
456db 3d 20 30 3b 0a 0a 20 20 20 20 6e 50 72 65 66 69  = 0;..    nPrefi
456dc 78 20 3d 20 30 3b 0a 20 20 20 20 6e 53 75 66 66  x = 0;.    nSuff
456dd 69 78 20 3d 20 6e 54 65 72 6d 3b 0a 20 20 20 20  ix = nTerm;.    
456de 6e 52 65 71 20 3d 20 31 20 2b 20 20 20 20 20 20  nReq = 1 +      
456df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
456e0 20 20 20 20 20 20 20 20 2f 2a 20 76 61 72 69 6e          /* varin
456e1 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 72 65  t containing pre
456e2 66 69 78 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20  fix size */.    
456e3 20 20 73 71 6c 69 74 65 33 46 74 73 33 56 61 72    sqlite3Fts3Var
456e4 69 6e 74 4c 65 6e 28 6e 54 65 72 6d 29 20 2b 20  intLen(nTerm) + 
456e5 20 20 20 20 20 20 20 20 2f 2a 20 76 61 72 69 6e          /* varin
456e6 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 73 75 66  t containing suf
456e7 66 69 78 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20  fix size */.    
456e8 20 20 6e 54 65 72 6d 20 2b 20 20 20 20 20 20 20    nTerm +       
456e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
456ea 20 20 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20          /* Term 
456eb 73 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20  suffix */.      
456ec 73 71 6c 69 74 65 33 46 74 73 33 56 61 72 69 6e  sqlite3Fts3Varin
456ed 74 4c 65 6e 28 6e 44 6f 63 6c 69 73 74 29 20 2b  tLen(nDoclist) +
456ee 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
456ef 20 64 6f 63 6c 69 73 74 20 2a 2f 0a 20 20 20 20   doclist */.    
456f0 20 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20    nDoclist;     
456f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
456f2 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69          /* Docli
456f3 73 74 20 64 61 74 61 20 2a 2f 0a 20 20 7d 0a 0a  st data */.  }..
456f4 20 20 2f 2a 20 49 66 20 74 68 65 20 62 75 66 66    /* If the buff
456f5 65 72 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c  er currently all
456f6 6f 63 61 74 65 64 20 69 73 20 74 6f 6f 20 73 6d  ocated is too sm
456f7 61 6c 6c 20 66 6f 72 20 74 68 69 73 20 65 6e 74  all for this ent
456f8 72 79 2c 20 72 65 61 6c 6c 6f 63 0a 20 20 2a 2a  ry, realloc.  **
456f9 20 74 68 65 20 62 75 66 66 65 72 20 74 6f 20 6d   the buffer to m
456fa 61 6b 65 20 69 74 20 6c 61 72 67 65 20 65 6e 6f  ake it large eno
456fb 75 67 68 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ugh..  */.  if( 
456fc 6e 52 65 71 3e 70 57 72 69 74 65 72 2d 3e 6e 53  nReq>pWriter->nS
456fd 69 7a 65 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ize ){.    char 
456fe 2a 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f  *aNew = sqlite3_
456ff 72 65 61 6c 6c 6f 63 28 70 57 72 69 74 65 72 2d  realloc(pWriter-
45700 3e 61 44 61 74 61 2c 20 6e 52 65 71 29 3b 0a 20  >aData, nReq);. 
45701 20 20 20 69 66 28 20 21 61 4e 65 77 20 29 20 72     if( !aNew ) r
45702 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
45703 45 4d 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d  EM;.    pWriter-
45704 3e 61 44 61 74 61 20 3d 20 61 4e 65 77 3b 0a 20  >aData = aNew;. 
45705 20 20 20 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a     pWriter->nSiz
45706 65 20 3d 20 6e 52 65 71 3b 0a 20 20 7d 0a 20 20  e = nReq;.  }.  
45707 61 73 73 65 72 74 28 20 6e 44 61 74 61 2b 6e 52  assert( nData+nR
45708 65 71 3c 3d 70 57 72 69 74 65 72 2d 3e 6e 53 69  eq<=pWriter->nSi
45709 7a 65 20 29 3b 0a 0a 20 20 2f 2a 20 41 70 70 65  ze );..  /* Appe
4570a 6e 64 20 74 68 65 20 70 72 65 66 69 78 2d 63 6f  nd the prefix-co
4570b 6d 70 72 65 73 73 65 64 20 74 65 72 6d 20 61 6e  mpressed term an
4570c 64 20 64 6f 63 6c 69 73 74 20 74 6f 20 74 68 65  d doclist to the
4570d 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 6e 44   buffer. */.  nD
4570e 61 74 61 20 2b 3d 20 73 71 6c 69 74 65 33 46 74  ata += sqlite3Ft
4570f 73 33 50 75 74 56 61 72 69 6e 74 28 26 70 57 72  s3PutVarint(&pWr
45710 69 74 65 72 2d 3e 61 44 61 74 61 5b 6e 44 61 74  iter->aData[nDat
45711 61 5d 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20 20  a], nPrefix);.  
45712 6e 44 61 74 61 20 2b 3d 20 73 71 6c 69 74 65 33  nData += sqlite3
45713 46 74 73 33 50 75 74 56 61 72 69 6e 74 28 26 70  Fts3PutVarint(&p
45714 57 72 69 74 65 72 2d 3e 61 44 61 74 61 5b 6e 44  Writer->aData[nD
45715 61 74 61 5d 2c 20 6e 53 75 66 66 69 78 29 3b 0a  ata], nSuffix);.
45716 20 20 6d 65 6d 63 70 79 28 26 70 57 72 69 74 65    memcpy(&pWrite
45717 72 2d 3e 61 44 61 74 61 5b 6e 44 61 74 61 5d 2c  r->aData[nData],
45718 20 26 7a 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d   &zTerm[nPrefix]
45719 2c 20 6e 53 75 66 66 69 78 29 3b 0a 20 20 6e 44  , nSuffix);.  nD
4571a 61 74 61 20 2b 3d 20 6e 53 75 66 66 69 78 3b 0a  ata += nSuffix;.
4571b 20 20 6e 44 61 74 61 20 2b 3d 20 73 71 6c 69 74    nData += sqlit
4571c 65 33 46 74 73 33 50 75 74 56 61 72 69 6e 74 28  e3Fts3PutVarint(
4571d 26 70 57 72 69 74 65 72 2d 3e 61 44 61 74 61 5b  &pWriter->aData[
4571e 6e 44 61 74 61 5d 2c 20 6e 44 6f 63 6c 69 73 74  nData], nDoclist
4571f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 70 57 72  );.  memcpy(&pWr
45720 69 74 65 72 2d 3e 61 44 61 74 61 5b 6e 44 61 74  iter->aData[nDat
45721 61 5d 2c 20 61 44 6f 63 6c 69 73 74 2c 20 6e 44  a], aDoclist, nD
45722 6f 63 6c 69 73 74 29 3b 0a 20 20 70 57 72 69 74  oclist);.  pWrit
45723 65 72 2d 3e 6e 44 61 74 61 20 3d 20 6e 44 61 74  er->nData = nDat
45724 61 20 2b 20 6e 44 6f 63 6c 69 73 74 3b 0a 0a 20  a + nDoclist;.. 
45725 20 2f 2a 20 53 61 76 65 20 74 68 65 20 63 75 72   /* Save the cur
45726 72 65 6e 74 20 74 65 72 6d 20 73 6f 20 74 68 61  rent term so tha
45727 74 20 69 74 20 63 61 6e 20 62 65 20 75 73 65 64  t it can be used
45728 20 74 6f 20 70 72 65 66 69 78 2d 63 6f 6d 70 72   to prefix-compr
45729 65 73 73 20 74 68 65 20 6e 65 78 74 2e 0a 20 20  ess the next..  
4572a 2a 2a 20 49 66 20 74 68 65 20 69 73 43 6f 70 79  ** If the isCopy
4572b 54 65 72 6d 20 70 61 72 61 6d 65 74 65 72 20 69  Term parameter i
4572c 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65  s true, then the
4572d 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20   buffer pointed 
4572e 74 6f 20 62 79 0a 20 20 2a 2a 20 7a 54 65 72 6d  to by.  ** zTerm
4572f 20 69 73 20 74 72 61 6e 73 69 65 6e 74 2c 20 73   is transient, s
45730 6f 20 74 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  o take a copy of
45731 20 74 68 65 20 74 65 72 6d 20 64 61 74 61 2e 20   the term data. 
45732 4f 74 68 65 72 77 69 73 65 2c 20 6a 75 73 74 0a  Otherwise, just.
45733 20 20 2a 2a 20 73 74 6f 72 65 20 61 20 63 6f 70    ** store a cop
45734 79 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72  y of the pointer
45735 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 43  ..  */.  if( isC
45736 6f 70 79 54 65 72 6d 20 29 7b 0a 20 20 20 20 69  opyTerm ){.    i
45737 66 28 20 6e 54 65 72 6d 3e 70 57 72 69 74 65 72  f( nTerm>pWriter
45738 2d 3e 6e 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20  ->nMalloc ){.   
45739 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d 20     char *zNew = 
4573a 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
4573b 70 57 72 69 74 65 72 2d 3e 7a 4d 61 6c 6c 6f 63  pWriter->zMalloc
4573c 2c 20 6e 54 65 72 6d 2a 32 29 3b 0a 20 20 20 20  , nTerm*2);.    
4573d 20 20 69 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20    if( !zNew ){. 
4573e 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
4573f 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
45740 20 20 7d 0a 20 20 20 20 20 20 70 57 72 69 74 65    }.      pWrite
45741 72 2d 3e 6e 4d 61 6c 6c 6f 63 20 3d 20 6e 54 65  r->nMalloc = nTe
45742 72 6d 2a 32 3b 0a 20 20 20 20 20 20 70 57 72 69  rm*2;.      pWri
45743 74 65 72 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 7a  ter->zMalloc = z
45744 4e 65 77 3b 0a 20 20 20 20 20 20 70 57 72 69 74  New;.      pWrit
45745 65 72 2d 3e 7a 54 65 72 6d 20 3d 20 7a 4e 65 77  er->zTerm = zNew
45746 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
45747 72 74 28 20 70 57 72 69 74 65 72 2d 3e 7a 54 65  rt( pWriter->zTe
45748 72 6d 3d 3d 70 57 72 69 74 65 72 2d 3e 7a 4d 61  rm==pWriter->zMa
45749 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 6d 65 6d 63  lloc );.    memc
4574a 70 79 28 70 57 72 69 74 65 72 2d 3e 7a 54 65 72  py(pWriter->zTer
4574b 6d 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29  m, zTerm, nTerm)
4574c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
4574d 57 72 69 74 65 72 2d 3e 7a 54 65 72 6d 20 3d 20  Writer->zTerm = 
4574e 28 63 68 61 72 20 2a 29 7a 54 65 72 6d 3b 0a 20  (char *)zTerm;. 
4574f 20 7d 0a 20 20 70 57 72 69 74 65 72 2d 3e 6e 54   }.  pWriter->nT
45750 65 72 6d 20 3d 20 6e 54 65 72 6d 3b 0a 0a 20 20  erm = nTerm;..  
45751 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
45752 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6c 75 73 68  ;.}../*.** Flush
45753 20 61 6c 6c 20 64 61 74 61 20 61 73 73 6f 63 69   all data associ
45754 61 74 65 64 20 77 69 74 68 20 74 68 65 20 53 65  ated with the Se
45755 67 6d 65 6e 74 57 72 69 74 65 72 20 6f 62 6a 65  gmentWriter obje
45756 63 74 20 70 57 72 69 74 65 72 20 74 6f 20 74 68  ct pWriter to th
45757 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54  e.** database. T
45758 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  his function mus
45759 74 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65  t be called afte
4575a 72 20 61 6c 6c 20 74 65 72 6d 73 20 68 61 76 65  r all terms have
4575b 20 62 65 65 6e 20 61 64 64 65 64 0a 2a 2a 20 74   been added.** t
4575c 6f 20 74 68 65 20 73 65 67 6d 65 6e 74 20 75 73  o the segment us
4575d 69 6e 67 20 66 74 73 33 53 65 67 57 72 69 74 65  ing fts3SegWrite
4575e 72 41 64 64 28 29 2e 20 49 66 20 73 75 63 63 65  rAdd(). If succe
4575f 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b  ssful, SQLITE_OK
45760 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e   is.** returned.
45761 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 53   Otherwise, an S
45762 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
45763 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
45764 66 74 73 33 53 65 67 57 72 69 74 65 72 46 6c 75  fts3SegWriterFlu
45765 73 68 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20  sh(.  Fts3Table 
45766 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
45767 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c        /* Virtual
45768 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f   table handle */
45769 0a 20 20 53 65 67 6d 65 6e 74 57 72 69 74 65 72  .  SegmentWriter
4576a 20 2a 70 57 72 69 74 65 72 2c 20 20 20 20 20 20   *pWriter,      
4576b 20 20 20 2f 2a 20 53 65 67 6d 65 6e 74 57 72 69     /* SegmentWri
4576c 74 65 72 20 74 6f 20 66 6c 75 73 68 20 74 6f 20  ter to flush to 
4576d 74 68 65 20 64 62 20 2a 2f 0a 20 20 73 71 6c 69  the db */.  sqli
4576e 74 65 33 5f 69 6e 74 36 34 20 69 4c 65 76 65 6c  te3_int64 iLevel
4576f 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56  ,           /* V
45770 61 6c 75 65 20 66 6f 72 20 27 6c 65 76 65 6c 27  alue for 'level'
45771 20 63 6f 6c 75 6d 6e 20 6f 66 20 25 5f 73 65 67   column of %_seg
45772 64 69 72 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64  dir */.  int iId
45773 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x               
45774 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
45775 65 20 66 6f 72 20 27 69 64 78 27 20 63 6f 6c 75  e for 'idx' colu
45776 6d 6e 20 6f 66 20 25 5f 73 65 67 64 69 72 20 2a  mn of %_segdir *
45777 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20  /.){.  int rc;  
45778 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45779 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
4577a 20 63 6f 64 65 20 2a 2f 0a 20 20 69 66 28 20 70   code */.  if( p
4577b 57 72 69 74 65 72 2d 3e 70 54 72 65 65 20 29 7b  Writer->pTree ){
4577c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  .    sqlite3_int
4577d 36 34 20 69 4c 61 73 74 20 3d 20 30 3b 20 20 20  64 iLast = 0;   
4577e 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 62 6c     /* Largest bl
4577f 6f 63 6b 20 69 64 20 77 72 69 74 74 65 6e 20 74  ock id written t
45780 6f 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  o database */.  
45781 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
45782 69 4c 61 73 74 4c 65 61 66 3b 20 20 20 20 20 20  iLastLeaf;      
45783 2f 2a 20 4c 61 72 67 65 73 74 20 6c 65 61 66 20  /* Largest leaf 
45784 62 6c 6f 63 6b 20 69 64 20 77 72 69 74 74 65 6e  block id written
45785 20 74 6f 20 64 62 20 2a 2f 0a 20 20 20 20 63 68   to db */.    ch
45786 61 72 20 2a 7a 52 6f 6f 74 20 3d 20 4e 55 4c 4c  ar *zRoot = NULL
45787 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ;           /* P
45788 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72  ointer to buffer
45789 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74   containing root
4578a 20 6e 6f 64 65 20 2a 2f 0a 20 20 20 20 69 6e 74   node */.    int
4578b 20 6e 52 6f 6f 74 20 3d 20 30 3b 20 20 20 20 20   nRoot = 0;     
4578c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
4578d 7a 65 20 6f 66 20 62 75 66 66 65 72 20 7a 52 6f  ze of buffer zRo
4578e 6f 74 20 2a 2f 0a 0a 20 20 20 20 69 4c 61 73 74  ot */..    iLast
4578f 4c 65 61 66 20 3d 20 70 57 72 69 74 65 72 2d 3e  Leaf = pWriter->
45790 69 46 72 65 65 3b 0a 20 20 20 20 72 63 20 3d 20  iFree;.    rc = 
45791 66 74 73 33 57 72 69 74 65 53 65 67 6d 65 6e 74  fts3WriteSegment
45792 28 70 2c 20 70 57 72 69 74 65 72 2d 3e 69 46 72  (p, pWriter->iFr
45793 65 65 2b 2b 2c 20 70 57 72 69 74 65 72 2d 3e 61  ee++, pWriter->a
45794 44 61 74 61 2c 20 70 57 72 69 74 65 72 2d 3e 6e  Data, pWriter->n
45795 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 72  Data);.    if( r
45796 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
45797 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 4e        rc = fts3N
45798 6f 64 65 57 72 69 74 65 28 70 2c 20 70 57 72 69  odeWrite(p, pWri
45799 74 65 72 2d 3e 70 54 72 65 65 2c 20 31 2c 0a 20  ter->pTree, 1,. 
4579a 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72           pWriter
4579b 2d 3e 69 46 69 72 73 74 2c 20 70 57 72 69 74 65  ->iFirst, pWrite
4579c 72 2d 3e 69 46 72 65 65 2c 20 26 69 4c 61 73 74  r->iFree, &iLast
4579d 2c 20 26 7a 52 6f 6f 74 2c 20 26 6e 52 6f 6f 74  , &zRoot, &nRoot
4579e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
4579f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
457a0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73  {.      rc = fts
457a1 33 57 72 69 74 65 53 65 67 64 69 72 28 0a 20 20  3WriteSegdir(.  
457a2 20 20 20 20 20 20 20 20 70 2c 20 69 4c 65 76 65          p, iLeve
457a3 6c 2c 20 69 49 64 78 2c 20 70 57 72 69 74 65 72  l, iIdx, pWriter
457a4 2d 3e 69 46 69 72 73 74 2c 20 69 4c 61 73 74 4c  ->iFirst, iLastL
457a5 65 61 66 2c 20 69 4c 61 73 74 2c 20 7a 52 6f 6f  eaf, iLast, zRoo
457a6 74 2c 20 6e 52 6f 6f 74 29 3b 0a 20 20 20 20 7d  t, nRoot);.    }
457a7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
457a8 20 54 68 65 20 65 6e 74 69 72 65 20 74 72 65 65   The entire tree
457a9 20 66 69 74 73 20 6f 6e 20 74 68 65 20 72 6f 6f   fits on the roo
457aa 74 20 6e 6f 64 65 2e 20 57 72 69 74 65 20 69 74  t node. Write it
457ab 20 74 6f 20 74 68 65 20 73 65 67 64 69 72 20 74   to the segdir t
457ac 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20  able. */.    rc 
457ad 3d 20 66 74 73 33 57 72 69 74 65 53 65 67 64 69  = fts3WriteSegdi
457ae 72 28 0a 20 20 20 20 20 20 20 20 70 2c 20 69 4c  r(.        p, iL
457af 65 76 65 6c 2c 20 69 49 64 78 2c 20 30 2c 20 30  evel, iIdx, 0, 0
457b0 2c 20 30 2c 20 70 57 72 69 74 65 72 2d 3e 61 44  , 0, pWriter->aD
457b1 61 74 61 2c 20 70 57 72 69 74 65 72 2d 3e 6e 44  ata, pWriter->nD
457b2 61 74 61 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e  ata);.  }.  p->n
457b3 4c 65 61 66 41 64 64 2b 2b 3b 0a 20 20 72 65 74  LeafAdd++;.  ret
457b4 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
457b5 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 6d 65 6d   Release all mem
457b6 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 20  ory held by the 
457b7 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20 6f 62  SegmentWriter ob
457b8 6a 65 63 74 20 70 61 73 73 65 64 20 61 73 20 74  ject passed as t
457b9 68 65 20 0a 2a 2a 20 66 69 72 73 74 20 61 72 67  he .** first arg
457ba 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ument..*/.static
457bb 20 76 6f 69 64 20 66 74 73 33 53 65 67 57 72 69   void fts3SegWri
457bc 74 65 72 46 72 65 65 28 53 65 67 6d 65 6e 74 57  terFree(SegmentW
457bd 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b  riter *pWriter){
457be 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 20 29  .  if( pWriter )
457bf 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
457c0 65 65 28 70 57 72 69 74 65 72 2d 3e 61 44 61 74  ee(pWriter->aDat
457c1 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  a);.    sqlite3_
457c2 66 72 65 65 28 70 57 72 69 74 65 72 2d 3e 7a 4d  free(pWriter->zM
457c3 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 66 74 73 33  alloc);.    fts3
457c4 4e 6f 64 65 46 72 65 65 28 70 57 72 69 74 65 72  NodeFree(pWriter
457c5 2d 3e 70 54 72 65 65 29 3b 0a 20 20 20 20 73 71  ->pTree);.    sq
457c6 6c 69 74 65 33 5f 66 72 65 65 28 70 57 72 69 74  lite3_free(pWrit
457c7 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  er);.  }.}../*.*
457c8 2a 20 54 68 65 20 66 69 72 73 74 20 76 61 6c 75  * The first valu
457c9 65 20 69 6e 20 74 68 65 20 61 70 56 61 6c 5b 5d  e in the apVal[]
457ca 20 61 72 72 61 79 20 69 73 20 61 73 73 75 6d 65   array is assume
457cb 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 6e 20  d to contain an 
457cc 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73  integer..** This
457cd 20 66 75 6e 63 74 69 6f 6e 20 74 65 73 74 73 20   function tests 
457ce 69 66 20 74 68 65 72 65 20 65 78 69 73 74 20 61  if there exist a
457cf 6e 79 20 64 6f 63 75 6d 65 6e 74 73 20 77 69 74  ny documents wit
457d0 68 20 64 6f 63 69 64 20 76 61 6c 75 65 73 20 74  h docid values t
457d1 68 61 74 0a 2a 2a 20 61 72 65 20 64 69 66 66 65  hat.** are diffe
457d2 72 65 6e 74 20 66 72 6f 6d 20 74 68 61 74 20 69  rent from that i
457d3 6e 74 65 67 65 72 2e 20 69 2e 65 2e 20 69 66 20  nteger. i.e. if 
457d4 64 65 6c 65 74 69 6e 67 20 74 68 65 20 64 6f 63  deleting the doc
457d5 75 6d 65 6e 74 20 77 69 74 68 20 64 6f 63 69 64  ument with docid
457d6 0a 2a 2a 20 70 52 6f 77 69 64 20 77 6f 75 6c 64  .** pRowid would
457d7 20 6d 65 61 6e 20 74 68 65 20 46 54 53 33 20 74   mean the FTS3 t
457d8 61 62 6c 65 20 77 65 72 65 20 65 6d 70 74 79 2e  able were empty.
457d9 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  .**.** If succes
457da 73 66 75 6c 2c 20 2a 70 69 73 45 6d 70 74 79 20  sful, *pisEmpty 
457db 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69  is set to true i
457dc 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65  f the table is e
457dd 6d 70 74 79 20 65 78 63 65 70 74 20 66 6f 72 0a  mpty except for.
457de 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 70 52 6f 77  ** document pRow
457df 69 64 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68  id, or false oth
457e0 65 72 77 69 73 65 2c 20 61 6e 64 20 53 51 4c 49  erwise, and SQLI
457e1 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
457e2 64 2e 20 49 66 20 61 6e 0a 2a 2a 20 65 72 72 6f  d. If an.** erro
457e3 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 53 51 4c  r occurs, an SQL
457e4 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  ite error code i
457e5 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73  s returned..*/.s
457e6 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 49 73  tatic int fts3Is
457e7 45 6d 70 74 79 28 46 74 73 33 54 61 62 6c 65 20  Empty(Fts3Table 
457e8 2a 70 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  *p, sqlite3_valu
457e9 65 20 2a 70 52 6f 77 69 64 2c 20 69 6e 74 20 2a  e *pRowid, int *
457ea 70 69 73 45 6d 70 74 79 29 7b 0a 20 20 73 71 6c  pisEmpty){.  sql
457eb 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
457ec 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66  ;.  int rc;.  if
457ed 28 20 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c  ( p->zContentTbl
457ee 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 75 73   ){.    /* If us
457ef 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 3d  ing the content=
457f0 78 78 78 20 6f 70 74 69 6f 6e 2c 20 61 73 73 75  xxx option, assu
457f1 6d 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  me the table is 
457f2 6e 65 76 65 72 20 65 6d 70 74 79 20 2a 2f 0a 20  never empty */. 
457f3 20 20 20 2a 70 69 73 45 6d 70 74 79 20 3d 20 30     *pisEmpty = 0
457f4 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
457f5 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
457f6 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c 53     rc = fts3SqlS
457f7 74 6d 74 28 70 2c 20 53 51 4c 5f 49 53 5f 45 4d  tmt(p, SQL_IS_EM
457f8 50 54 59 2c 20 26 70 53 74 6d 74 2c 20 26 70 52  PTY, &pStmt, &pR
457f9 6f 77 69 64 29 3b 0a 20 20 20 20 69 66 28 20 72  owid);.    if( r
457fa 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
457fb 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45        if( SQLITE
457fc 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74  _ROW==sqlite3_st
457fd 65 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20  ep(pStmt) ){.   
457fe 20 20 20 20 20 2a 70 69 73 45 6d 70 74 79 20 3d       *pisEmpty =
457ff 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
45800 69 6e 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20  int(pStmt, 0);. 
45801 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20       }.      rc 
45802 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
45803 70 53 74 6d 74 29 3b 0a 20 20 20 20 7d 0a 20 20  pStmt);.    }.  
45804 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
45805 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 6e 4d  ../*.** Set *pnM
45806 61 78 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73  ax to the larges
45807 74 20 73 65 67 6d 65 6e 74 20 6c 65 76 65 6c 20  t segment level 
45808 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
45809 66 6f 72 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a  for the index.**
4580a 20 69 49 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53   iIndex..**.** S
4580b 65 67 6d 65 6e 74 20 6c 65 76 65 6c 73 20 61 72  egment levels ar
4580c 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
4580d 27 6c 65 76 65 6c 27 20 63 6f 6c 75 6d 6e 20 6f  'level' column o
4580e 66 20 74 68 65 20 25 5f 73 65 67 64 69 72 20 74  f the %_segdir t
4580f 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  able..**.** Retu
45810 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  rn SQLITE_OK if 
45811 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 61  successful, or a
45812 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
45813 6f 64 65 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73  ode if not..*/.s
45814 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65  tatic int fts3Se
45815 67 6d 65 6e 74 4d 61 78 4c 65 76 65 6c 28 0a 20  gmentMaxLevel(. 
45816 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 0a   Fts3Table *p, .
45817 20 20 69 6e 74 20 69 4c 61 6e 67 69 64 2c 0a 20    int iLangid,. 
45818 20 69 6e 74 20 69 49 6e 64 65 78 2c 20 0a 20 20   int iIndex, .  
45819 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
4581a 6e 4d 61 78 0a 29 7b 0a 20 20 73 71 6c 69 74 65  nMax.){.  sqlite
4581b 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20  3_stmt *pStmt;. 
4581c 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72   int rc;.  asser
4581d 74 28 20 69 49 6e 64 65 78 3e 3d 30 20 26 26 20  t( iIndex>=0 && 
4581e 69 49 6e 64 65 78 3c 70 2d 3e 6e 49 6e 64 65 78  iIndex<p->nIndex
4581f 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 70 53   );..  /* Set pS
45820 74 6d 74 20 74 6f 20 74 68 65 20 63 6f 6d 70 69  tmt to the compi
45821 6c 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 3a 0a  led version of:.
45822 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 53 45 4c 45    **.  **   SELE
45823 43 54 20 6d 61 78 28 6c 65 76 65 6c 29 20 46 52  CT max(level) FR
45824 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72  OM %Q.'%q_segdir
45825 27 20 57 48 45 52 45 20 6c 65 76 65 6c 20 42 45  ' WHERE level BE
45826 54 57 45 45 4e 20 3f 20 41 4e 44 20 3f 0a 20 20  TWEEN ? AND ?.  
45827 2a 2a 0a 20 20 2a 2a 20 28 31 30 32 34 20 69 73  **.  ** (1024 is
45828 20 61 63 74 75 61 6c 6c 79 20 74 68 65 20 76 61   actually the va
45829 6c 75 65 20 6f 66 20 6d 61 63 72 6f 20 46 54 53  lue of macro FTS
4582a 33 5f 53 45 47 44 49 52 5f 50 52 45 46 49 58 4c  3_SEGDIR_PREFIXL
4582b 45 56 45 4c 5f 53 54 52 29 2e 0a 20 20 2a 2f 0a  EVEL_STR)..  */.
4582c 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74    rc = fts3SqlSt
4582d 6d 74 28 70 2c 20 53 51 4c 5f 53 45 4c 45 43 54  mt(p, SQL_SELECT
4582e 5f 53 45 47 44 49 52 5f 4d 41 58 5f 4c 45 56 45  _SEGDIR_MAX_LEVE
4582f 4c 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  L, &pStmt, 0);. 
45830 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
45831 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
45832 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
45833 6e 74 36 34 28 70 53 74 6d 74 2c 20 31 2c 20 67  nt64(pStmt, 1, g
45834 65 74 41 62 73 6f 6c 75 74 65 4c 65 76 65 6c 28  etAbsoluteLevel(
45835 70 2c 20 69 4c 61 6e 67 69 64 2c 20 69 49 6e 64  p, iLangid, iInd
45836 65 78 2c 20 30 29 29 3b 0a 20 20 73 71 6c 69 74  ex, 0));.  sqlit
45837 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53  e3_bind_int64(pS
45838 74 6d 74 2c 20 32 2c 20 0a 20 20 20 20 20 20 67  tmt, 2, .      g
45839 65 74 41 62 73 6f 6c 75 74 65 4c 65 76 65 6c 28  etAbsoluteLevel(
4583a 70 2c 20 69 4c 61 6e 67 69 64 2c 20 69 49 6e 64  p, iLangid, iInd
4583b 65 78 2c 20 46 54 53 33 5f 53 45 47 44 49 52 5f  ex, FTS3_SEGDIR_
4583c 4d 41 58 4c 45 56 45 4c 2d 31 29 0a 20 20 29 3b  MAXLEVEL-1).  );
4583d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f  .  if( SQLITE_RO
4583e 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28  W==sqlite3_step(
4583f 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 2a 70  pStmt) ){.    *p
45840 6e 4d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f 63  nMax = sqlite3_c
45841 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 53 74 6d  olumn_int64(pStm
45842 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74  t, 0);.  }.  ret
45843 75 72 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65  urn sqlite3_rese
45844 74 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a  t(pStmt);.}../*.
45845 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 65 6e  ** Delete all en
45846 74 72 69 65 73 20 69 6e 20 74 68 65 20 25 5f 73  tries in the %_s
45847 65 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 61 73  egments table as
45848 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
45849 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 6f 70 65  e segment.** ope
4584a 6e 65 64 20 77 69 74 68 20 73 65 67 2d 72 65 61  ned with seg-rea
4584b 64 65 72 20 70 53 65 67 2e 20 54 68 69 73 20 66  der pSeg. This f
4584c 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  unction does not
4584d 20 61 66 66 65 63 74 20 74 68 65 20 63 6f 6e 74   affect the cont
4584e 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 25  ents.** of the %
4584f 5f 73 65 67 64 69 72 20 74 61 62 6c 65 2e 0a 2a  _segdir table..*
45850 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
45851 33 44 65 6c 65 74 65 53 65 67 6d 65 6e 74 28 0a  3DeleteSegment(.
45852 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20    Fts3Table *p, 
45853 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45854 20 20 2f 2a 20 46 54 53 20 74 61 62 6c 65 20 68    /* FTS table h
45855 61 6e 64 6c 65 20 2a 2f 0a 20 20 46 74 73 33 53  andle */.  Fts3S
45856 65 67 52 65 61 64 65 72 20 2a 70 53 65 67 20 20  egReader *pSeg  
45857 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
45858 67 6d 65 6e 74 20 74 6f 20 64 65 6c 65 74 65 20  gment to delete 
45859 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
4585a 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
4585b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
4585c 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 66 28 20  n code */.  if( 
4585d 70 53 65 67 2d 3e 69 53 74 61 72 74 42 6c 6f 63  pSeg->iStartBloc
4585e 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  k ){.    sqlite3
4585f 5f 73 74 6d 74 20 2a 70 44 65 6c 65 74 65 3b 20  _stmt *pDelete; 
45860 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74         /* SQL st
45861 61 74 65 6d 65 6e 74 20 74 6f 20 64 65 6c 65 74  atement to delet
45862 65 20 72 6f 77 73 20 2a 2f 0a 20 20 20 20 72 63  e rows */.    rc
45863 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70   = fts3SqlStmt(p
45864 2c 20 53 51 4c 5f 44 45 4c 45 54 45 5f 53 45 47  , SQL_DELETE_SEG
45865 4d 45 4e 54 53 5f 52 41 4e 47 45 2c 20 26 70 44  MENTS_RANGE, &pD
45866 65 6c 65 74 65 2c 20 30 29 3b 0a 20 20 20 20 69  elete, 0);.    i
45867 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
45868 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
45869 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 44 65  3_bind_int64(pDe
4586a 6c 65 74 65 2c 20 31 2c 20 70 53 65 67 2d 3e 69  lete, 1, pSeg->i
4586b 53 74 61 72 74 42 6c 6f 63 6b 29 3b 0a 20 20 20  StartBlock);.   
4586c 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
4586d 69 6e 74 36 34 28 70 44 65 6c 65 74 65 2c 20 32  int64(pDelete, 2
4586e 2c 20 70 53 65 67 2d 3e 69 45 6e 64 42 6c 6f 63  , pSeg->iEndBloc
4586f 6b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  k);.      sqlite
45870 33 5f 73 74 65 70 28 70 44 65 6c 65 74 65 29 3b  3_step(pDelete);
45871 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
45872 74 65 33 5f 72 65 73 65 74 28 70 44 65 6c 65 74  te3_reset(pDelet
45873 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
45874 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
45875 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
45876 6e 20 69 73 20 75 73 65 64 20 61 66 74 65 72 20  n is used after 
45877 6d 65 72 67 69 6e 67 20 6d 75 6c 74 69 70 6c 65  merging multiple
45878 20 73 65 67 6d 65 6e 74 73 20 69 6e 74 6f 20 61   segments into a
45879 20 73 69 6e 67 6c 65 20 6c 61 72 67 65 0a 2a 2a   single large.**
4587a 20 73 65 67 6d 65 6e 74 20 74 6f 20 64 65 6c 65   segment to dele
4587b 74 65 20 74 68 65 20 6f 6c 64 2c 20 6e 6f 77 20  te the old, now 
4587c 72 65 64 75 6e 64 61 6e 74 2c 20 73 65 67 6d 65  redundant, segme
4587d 6e 74 20 62 2d 74 72 65 65 73 2e 20 53 70 65 63  nt b-trees. Spec
4587e 69 66 69 63 61 6c 6c 79 2c 0a 2a 2a 20 69 74 3a  ifically,.** it:
4587f 0a 2a 2a 20 0a 2a 2a 20 20 20 31 29 20 44 65 6c  .** .**   1) Del
45880 65 74 65 73 20 61 6c 6c 20 25 5f 73 65 67 6d 65  etes all %_segme
45881 6e 74 73 20 65 6e 74 72 69 65 73 20 66 6f 72 20  nts entries for 
45882 74 68 65 20 73 65 67 6d 65 6e 74 73 20 61 73 73  the segments ass
45883 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a  ociated with .**
45884 20 20 20 20 20 20 65 61 63 68 20 6f 66 20 74 68        each of th
45885 65 20 53 65 67 52 65 61 64 65 72 20 6f 62 6a 65  e SegReader obje
45886 63 74 73 20 69 6e 20 74 68 65 20 61 72 72 61 79  cts in the array
45887 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 74   passed as the t
45888 68 69 72 64 20 0a 2a 2a 20 20 20 20 20 20 61 72  hird .**      ar
45889 67 75 6d 65 6e 74 2c 20 61 6e 64 0a 2a 2a 0a 2a  gument, and.**.*
4588a 2a 20 20 20 32 29 20 64 65 6c 65 74 65 73 20 61  *   2) deletes a
4588b 6c 6c 20 25 5f 73 65 67 64 69 72 20 65 6e 74 72  ll %_segdir entr
4588c 69 65 73 20 77 69 74 68 20 6c 65 76 65 6c 20 69  ies with level i
4588d 4c 65 76 65 6c 2c 20 6f 72 20 61 6c 6c 20 25 5f  Level, or all %_
4588e 73 65 67 64 69 72 0a 2a 2a 20 20 20 20 20 20 65  segdir.**      e
4588f 6e 74 72 69 65 73 20 72 65 67 61 72 64 6c 65 73  ntries regardles
45890 73 20 6f 66 20 6c 65 76 65 6c 20 69 66 20 28 69  s of level if (i
45891 4c 65 76 65 6c 3c 30 29 2e 0a 2a 2a 0a 2a 2a 20  Level<0)..**.** 
45892 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
45893 75 72 6e 65 64 20 69 66 20 73 75 63 63 65 73 73  urned if success
45894 66 75 6c 2c 20 6f 74 68 65 72 77 69 73 65 20 61  ful, otherwise a
45895 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
45896 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ode..*/.static i
45897 6e 74 20 66 74 73 33 44 65 6c 65 74 65 53 65 67  nt fts3DeleteSeg
45898 64 69 72 28 0a 20 20 46 74 73 33 54 61 62 6c 65  dir(.  Fts3Table
45899 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
4589a 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61         /* Virtua
4589b 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  l table handle *
4589c 2f 0a 20 20 69 6e 74 20 69 4c 61 6e 67 69 64 2c  /.  int iLangid,
4589d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4589e 20 20 20 20 2f 2a 20 4c 61 6e 67 75 61 67 65 20      /* Language 
4589f 69 64 20 2a 2f 0a 20 20 69 6e 74 20 69 49 6e 64  id */.  int iInd
458a0 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ex,             
458a1 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
458a2 20 66 6f 72 20 70 2d 3e 61 49 6e 64 65 78 20 2a   for p->aIndex *
458a3 2f 0a 20 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20  /.  int iLevel, 
458a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
458a5 20 20 20 20 2f 2a 20 4c 65 76 65 6c 20 6f 66 20      /* Level of 
458a6 25 5f 73 65 67 64 69 72 20 65 6e 74 72 69 65 73  %_segdir entries
458a7 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20   to delete */.  
458a8 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a  Fts3SegReader **
458a9 61 70 53 65 67 6d 65 6e 74 2c 20 20 20 20 20 20  apSegment,      
458aa 2f 2a 20 41 72 72 61 79 20 6f 66 20 53 65 67 52  /* Array of SegR
458ab 65 61 64 65 72 20 6f 62 6a 65 63 74 73 20 2a 2f  eader objects */
458ac 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 20 20  .  int nReader  
458ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
458ae 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72     /* Size of ar
458af 72 61 79 20 61 70 53 65 67 6d 65 6e 74 20 2a 2f  ray apSegment */
458b0 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
458b1 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
458b2 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
458b3 43 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  Code */.  int i;
458b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
458b5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
458b6 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
458b7 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
458b8 20 2a 70 44 65 6c 65 74 65 20 3d 20 30 3b 20 20   *pDelete = 0;  
458b9 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65      /* SQL state
458ba 6d 65 6e 74 20 74 6f 20 64 65 6c 65 74 65 20 72  ment to delete r
458bb 6f 77 73 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 3d  ows */..  for(i=
458bc 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
458bd 20 26 26 20 69 3c 6e 52 65 61 64 65 72 3b 20 69   && i<nReader; i
458be 2b 2b 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 74  ++){.    rc = ft
458bf 73 33 44 65 6c 65 74 65 53 65 67 6d 65 6e 74 28  s3DeleteSegment(
458c0 70 2c 20 61 70 53 65 67 6d 65 6e 74 5b 69 5d 29  p, apSegment[i])
458c1 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d  ;.  }.  if( rc!=
458c2 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
458c3 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
458c4 0a 20 20 61 73 73 65 72 74 28 20 69 4c 65 76 65  .  assert( iLeve
458c5 6c 3e 3d 30 20 7c 7c 20 69 4c 65 76 65 6c 3d 3d  l>=0 || iLevel==
458c6 46 54 53 33 5f 53 45 47 43 55 52 53 4f 52 5f 41  FTS3_SEGCURSOR_A
458c7 4c 4c 20 29 3b 0a 20 20 69 66 28 20 69 4c 65 76  LL );.  if( iLev
458c8 65 6c 3d 3d 46 54 53 33 5f 53 45 47 43 55 52 53  el==FTS3_SEGCURS
458c9 4f 52 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 63  OR_ALL ){.    rc
458ca 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70   = fts3SqlStmt(p
458cb 2c 20 53 51 4c 5f 44 45 4c 45 54 45 5f 53 45 47  , SQL_DELETE_SEG
458cc 44 49 52 5f 52 41 4e 47 45 2c 20 26 70 44 65 6c  DIR_RANGE, &pDel
458cd 65 74 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  ete, 0);.    if(
458ce 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
458cf 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
458d0 62 69 6e 64 5f 69 6e 74 36 34 28 70 44 65 6c 65  bind_int64(pDele
458d1 74 65 2c 20 31 2c 20 67 65 74 41 62 73 6f 6c 75  te, 1, getAbsolu
458d2 74 65 4c 65 76 65 6c 28 70 2c 20 69 4c 61 6e 67  teLevel(p, iLang
458d3 69 64 2c 20 69 49 6e 64 65 78 2c 20 30 29 29 3b  id, iIndex, 0));
458d4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62  .      sqlite3_b
458d5 69 6e 64 5f 69 6e 74 36 34 28 70 44 65 6c 65 74  ind_int64(pDelet
458d6 65 2c 20 32 2c 20 0a 20 20 20 20 20 20 20 20 20  e, 2, .         
458d7 20 67 65 74 41 62 73 6f 6c 75 74 65 4c 65 76 65   getAbsoluteLeve
458d8 6c 28 70 2c 20 69 4c 61 6e 67 69 64 2c 20 69 49  l(p, iLangid, iI
458d9 6e 64 65 78 2c 20 46 54 53 33 5f 53 45 47 44 49  ndex, FTS3_SEGDI
458da 52 5f 4d 41 58 4c 45 56 45 4c 2d 31 29 0a 20 20  R_MAXLEVEL-1).  
458db 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d      );.    }.  }
458dc 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 66  else{.    rc = f
458dd 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51  ts3SqlStmt(p, SQ
458de 4c 5f 44 45 4c 45 54 45 5f 53 45 47 44 49 52 5f  L_DELETE_SEGDIR_
458df 4c 45 56 45 4c 2c 20 26 70 44 65 6c 65 74 65 2c  LEVEL, &pDelete,
458e0 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d   0);.    if( rc=
458e1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
458e2 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
458e3 5f 69 6e 74 36 34 28 0a 20 20 20 20 20 20 20 20  _int64(.        
458e4 20 20 70 44 65 6c 65 74 65 2c 20 31 2c 20 67 65    pDelete, 1, ge
458e5 74 41 62 73 6f 6c 75 74 65 4c 65 76 65 6c 28 70  tAbsoluteLevel(p
458e6 2c 20 69 4c 61 6e 67 69 64 2c 20 69 49 6e 64 65  , iLangid, iInde
458e7 78 2c 20 69 4c 65 76 65 6c 29 0a 20 20 20 20 20  x, iLevel).     
458e8 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   );.    }.  }.. 
458e9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
458ea 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
458eb 33 5f 73 74 65 70 28 70 44 65 6c 65 74 65 29 3b  3_step(pDelete);
458ec 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
458ed 33 5f 72 65 73 65 74 28 70 44 65 6c 65 74 65 29  3_reset(pDelete)
458ee 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
458ef 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65  rc;.}../*.** Whe
458f0 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
458f1 69 73 20 63 61 6c 6c 65 64 2c 20 62 75 66 66 65  is called, buffe
458f2 72 20 2a 70 70 4c 69 73 74 20 28 73 69 7a 65 20  r *ppList (size 
458f3 2a 70 6e 4c 69 73 74 20 62 79 74 65 73 29 20 63  *pnList bytes) c
458f4 6f 6e 74 61 69 6e 73 20 0a 2a 2a 20 61 20 70 6f  ontains .** a po
458f5 73 69 74 69 6f 6e 20 6c 69 73 74 20 74 68 61 74  sition list that
458f6 20 6d 61 79 20 28 6f 72 20 6d 61 79 20 6e 6f 74   may (or may not
458f7 29 20 66 65 61 74 75 72 65 20 6d 75 6c 74 69 70  ) feature multip
458f8 6c 65 20 63 6f 6c 75 6d 6e 73 2e 20 54 68 69 73  le columns. This
458f9 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 64 6a  .** function adj
458fa 75 73 74 73 20 74 68 65 20 70 6f 69 6e 74 65 72  usts the pointer
458fb 20 2a 70 70 4c 69 73 74 20 61 6e 64 20 74 68 65   *ppList and the
458fc 20 6c 65 6e 67 74 68 20 2a 70 6e 4c 69 73 74 20   length *pnList 
458fd 73 6f 20 74 68 61 74 20 74 68 65 79 0a 2a 2a 20  so that they.** 
458fe 69 64 65 6e 74 69 66 79 20 74 68 65 20 73 75 62  identify the sub
458ff 73 65 74 20 6f 66 20 74 68 65 20 70 6f 73 69 74  set of the posit
45900 69 6f 6e 20 6c 69 73 74 20 74 68 61 74 20 63 6f  ion list that co
45901 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 63 6f 6c  rresponds to col
45902 75 6d 6e 20 69 43 6f 6c 2e 0a 2a 2a 0a 2a 2a 20  umn iCol..**.** 
45903 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  If there are no 
45904 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 69  entries in the i
45905 6e 70 75 74 20 70 6f 73 69 74 69 6f 6e 20 6c 69  nput position li
45906 73 74 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 69 43  st for column iC
45907 6f 6c 2c 20 74 68 65 6e 0a 2a 2a 20 2a 70 6e 4c  ol, then.** *pnL
45908 69 73 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65  ist is set to ze
45909 72 6f 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  ro before return
4590a 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ing..*/.static v
4590b 6f 69 64 20 66 74 73 33 43 6f 6c 75 6d 6e 46 69  oid fts3ColumnFi
4590c 6c 74 65 72 28 0a 20 20 69 6e 74 20 69 43 6f 6c  lter(.  int iCol
4590d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
4590e 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
4590f 6e 20 74 6f 20 66 69 6c 74 65 72 20 6f 6e 20 2a  n to filter on *
45910 2f 0a 20 20 63 68 61 72 20 2a 2a 70 70 4c 69 73  /.  char **ppLis
45911 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
45912 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 50      /* IN/OUT: P
45913 6f 69 6e 74 65 72 20 74 6f 20 70 6f 73 69 74 69  ointer to positi
45914 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  on list */.  int
45915 20 2a 70 6e 4c 69 73 74 20 20 20 20 20 20 20 20   *pnList        
45916 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
45917 49 4e 2f 4f 55 54 3a 20 53 69 7a 65 20 6f 66 20  IN/OUT: Size of 
45918 62 75 66 66 65 72 20 2a 70 70 4c 69 73 74 20 69  buffer *ppList i
45919 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20  n bytes */.){.  
4591a 63 68 61 72 20 2a 70 4c 69 73 74 20 3d 20 2a 70  char *pList = *p
4591b 70 4c 69 73 74 3b 0a 20 20 69 6e 74 20 6e 4c 69  pList;.  int nLi
4591c 73 74 20 3d 20 2a 70 6e 4c 69 73 74 3b 0a 20 20  st = *pnList;.  
4591d 63 68 61 72 20 2a 70 45 6e 64 20 3d 20 26 70 4c  char *pEnd = &pL
4591e 69 73 74 5b 6e 4c 69 73 74 5d 3b 0a 20 20 69 6e  ist[nList];.  in
4591f 74 20 69 43 75 72 72 65 6e 74 20 3d 20 30 3b 0a  t iCurrent = 0;.
45920 20 20 63 68 61 72 20 2a 70 20 3d 20 70 4c 69 73    char *p = pLis
45921 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43  t;..  assert( iC
45922 6f 6c 3e 3d 30 20 29 3b 0a 20 20 77 68 69 6c 65  ol>=0 );.  while
45923 28 20 31 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ( 1 ){.    char 
45924 63 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65  c = 0;.    while
45925 28 20 70 3c 70 45 6e 64 20 26 26 20 28 63 20 7c  ( p<pEnd && (c |
45926 20 2a 70 29 26 30 78 46 45 20 29 20 63 20 3d 20   *p)&0xFE ) c = 
45927 2a 70 2b 2b 20 26 20 30 78 38 30 3b 0a 20 20 0a  *p++ & 0x80;.  .
45928 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 69 43      if( iCol==iC
45929 75 72 72 65 6e 74 20 29 7b 0a 20 20 20 20 20 20  urrent ){.      
4592a 6e 4c 69 73 74 20 3d 20 28 69 6e 74 29 28 70 20  nList = (int)(p 
4592b 2d 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20  - pList);.      
4592c 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
4592d 20 20 6e 4c 69 73 74 20 2d 3d 20 28 69 6e 74 29    nList -= (int)
4592e 28 70 20 2d 20 70 4c 69 73 74 29 3b 0a 20 20 20  (p - pList);.   
4592f 20 70 4c 69 73 74 20 3d 20 70 3b 0a 20 20 20 20   pList = p;.    
45930 69 66 28 20 6e 4c 69 73 74 3d 3d 30 20 29 7b 0a  if( nList==0 ){.
45931 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
45932 20 7d 0a 20 20 20 20 70 20 3d 20 26 70 4c 69 73   }.    p = &pLis
45933 74 5b 31 5d 3b 0a 20 20 20 20 70 20 2b 3d 20 73  t[1];.    p += s
45934 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61 72  qlite3Fts3GetVar
45935 69 6e 74 33 32 28 70 2c 20 26 69 43 75 72 72 65  int32(p, &iCurre
45936 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 4c  nt);.  }..  *ppL
45937 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 2a  ist = pList;.  *
45938 70 6e 4c 69 73 74 20 3d 20 6e 4c 69 73 74 3b 0a  pnList = nList;.
45939 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 63 68 65 20 64  }../*.** Cache d
4593a 61 74 61 20 69 6e 20 74 68 65 20 46 74 73 33 4d  ata in the Fts3M
4593b 75 6c 74 69 53 65 67 52 65 61 64 65 72 2e 61 42  ultiSegReader.aB
4593c 75 66 66 65 72 5b 5d 20 62 75 66 66 65 72 20 28  uffer[] buffer (
4593d 6f 76 65 72 77 72 69 74 69 6e 67 20 61 6e 79 0a  overwriting any.
4593e 2a 2a 20 65 78 69 73 74 69 6e 67 20 64 61 74 61  ** existing data
4593f 29 2e 20 47 72 6f 77 20 74 68 65 20 62 75 66 66  ). Grow the buff
45940 65 72 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a  er if required..
45941 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73  **.** If success
45942 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ful, return SQLI
45943 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65  TE_OK. Otherwise
45944 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 65 72 72 6f  , if an OOM erro
45945 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
45946 0a 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 72 65  .** trying to re
45947 73 69 7a 65 20 74 68 65 20 62 75 66 66 65 72 2c  size the buffer,
45948 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
45949 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  OMEM..*/.static 
4594a 69 6e 74 20 66 74 73 33 4d 73 72 42 75 66 66 65  int fts3MsrBuffe
4594b 72 44 61 74 61 28 0a 20 20 46 74 73 33 4d 75 6c  rData(.  Fts3Mul
4594c 74 69 53 65 67 52 65 61 64 65 72 20 2a 70 4d 73  tiSegReader *pMs
4594d 72 2c 20 20 20 20 20 20 20 2f 2a 20 4d 75 6c 74  r,       /* Mult
4594e 69 2d 73 65 67 6d 65 6e 74 2d 72 65 61 64 65 72  i-segment-reader
4594f 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 68 61   handle */.  cha
45950 72 20 2a 70 4c 69 73 74 2c 0a 20 20 69 6e 74 20  r *pList,.  int 
45951 6e 4c 69 73 74 0a 29 7b 0a 20 20 69 66 28 20 6e  nList.){.  if( n
45952 4c 69 73 74 3e 70 4d 73 72 2d 3e 6e 42 75 66 66  List>pMsr->nBuff
45953 65 72 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  er ){.    char *
45954 70 4e 65 77 3b 0a 20 20 20 20 70 4d 73 72 2d 3e  pNew;.    pMsr->
45955 6e 42 75 66 66 65 72 20 3d 20 6e 4c 69 73 74 2a  nBuffer = nList*
45956 32 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 28 63  2;.    pNew = (c
45957 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 72 65  har *)sqlite3_re
45958 61 6c 6c 6f 63 28 70 4d 73 72 2d 3e 61 42 75 66  alloc(pMsr->aBuf
45959 66 65 72 2c 20 70 4d 73 72 2d 3e 6e 42 75 66 66  fer, pMsr->nBuff
4595a 65 72 29 3b 0a 20 20 20 20 69 66 28 20 21 70 4e  er);.    if( !pN
4595b 65 77 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  ew ) return SQLI
4595c 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 70 4d  TE_NOMEM;.    pM
4595d 73 72 2d 3e 61 42 75 66 66 65 72 20 3d 20 70 4e  sr->aBuffer = pN
4595e 65 77 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 63 70  ew;.  }..  memcp
4595f 79 28 70 4d 73 72 2d 3e 61 42 75 66 66 65 72 2c  y(pMsr->aBuffer,
45960 20 70 4c 69 73 74 2c 20 6e 4c 69 73 74 29 3b 0a   pList, nList);.
45961 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
45962 4f 4b 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  OK;.}..SQLITE_PR
45963 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
45964 33 46 74 73 33 4d 73 72 49 6e 63 72 4e 65 78 74  3Fts3MsrIncrNext
45965 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  (.  Fts3Table *p
45966 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
45967 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74      /* Virtual t
45968 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  able handle */. 
45969 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61   Fts3MultiSegRea
4596a 64 65 72 20 2a 70 4d 73 72 2c 20 20 20 20 20 20  der *pMsr,      
4596b 20 2f 2a 20 4d 75 6c 74 69 2d 73 65 67 6d 65 6e   /* Multi-segmen
4596c 74 2d 72 65 61 64 65 72 20 68 61 6e 64 6c 65 20  t-reader handle 
4596d 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
4596e 36 34 20 2a 70 69 44 6f 63 69 64 2c 20 20 20 20  64 *piDocid,    
4596f 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 44 6f 63       /* OUT: Doc
45970 69 64 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63 68  id value */.  ch
45971 61 72 20 2a 2a 70 61 50 6f 73 6c 69 73 74 2c 20  ar **paPoslist, 
45972 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
45973 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f   OUT: Pointer to
45974 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 2a   position list *
45975 2f 0a 20 20 69 6e 74 20 2a 70 6e 50 6f 73 6c 69  /.  int *pnPosli
45976 73 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  st              
45977 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 69 7a 65      /* OUT: Size
45978 20 6f 66 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73   of position lis
45979 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b  t in bytes */.){
4597a 0a 20 20 69 6e 74 20 6e 4d 65 72 67 65 20 3d 20  .  int nMerge = 
4597b 70 4d 73 72 2d 3e 6e 41 64 76 61 6e 63 65 3b 0a  pMsr->nAdvance;.
4597c 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20    Fts3SegReader 
4597d 2a 2a 61 70 53 65 67 6d 65 6e 74 20 3d 20 70 4d  **apSegment = pM
4597e 73 72 2d 3e 61 70 53 65 67 6d 65 6e 74 3b 0a 20  sr->apSegment;. 
4597f 20 69 6e 74 20 28 2a 78 43 6d 70 29 28 46 74 73   int (*xCmp)(Fts
45980 33 53 65 67 52 65 61 64 65 72 20 2a 2c 20 46 74  3SegReader *, Ft
45981 73 33 53 65 67 52 65 61 64 65 72 20 2a 29 20 3d  s3SegReader *) =
45982 20 28 0a 20 20 20 20 70 2d 3e 62 44 65 73 63 49   (.    p->bDescI
45983 64 78 20 3f 20 66 74 73 33 53 65 67 52 65 61 64  dx ? fts3SegRead
45984 65 72 44 6f 63 6c 69 73 74 43 6d 70 52 65 76 20  erDoclistCmpRev 
45985 3a 20 66 74 73 33 53 65 67 52 65 61 64 65 72 44  : fts3SegReaderD
45986 6f 63 6c 69 73 74 43 6d 70 0a 20 20 29 3b 0a 0a  oclistCmp.  );..
45987 20 20 69 66 28 20 6e 4d 65 72 67 65 3d 3d 30 20    if( nMerge==0 
45988 29 7b 0a 20 20 20 20 2a 70 61 50 6f 73 6c 69 73  ){.    *paPoslis
45989 74 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72  t = 0;.    retur
4598a 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
4598b 0a 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a  ..  while( 1 ){.
4598c 20 20 20 20 46 74 73 33 53 65 67 52 65 61 64 65      Fts3SegReade
4598d 72 20 2a 70 53 65 67 3b 0a 20 20 20 20 70 53 65  r *pSeg;.    pSe
4598e 67 20 3d 20 70 4d 73 72 2d 3e 61 70 53 65 67 6d  g = pMsr->apSegm
4598f 65 6e 74 5b 30 5d 3b 0a 0a 20 20 20 20 69 66 28  ent[0];..    if(
45990 20 70 53 65 67 2d 3e 70 4f 66 66 73 65 74 4c 69   pSeg->pOffsetLi
45991 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a  st==0 ){.      *
45992 70 61 50 6f 73 6c 69 73 74 20 3d 20 30 3b 0a 20  paPoslist = 0;. 
45993 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
45994 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
45995 20 72 63 3b 0a 20 20 20 20 20 20 63 68 61 72 20   rc;.      char 
45996 2a 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 69 6e  *pList;.      in
45997 74 20 6e 4c 69 73 74 3b 0a 20 20 20 20 20 20 69  t nList;.      i
45998 6e 74 20 6a 3b 0a 20 20 20 20 20 20 73 71 6c 69  nt j;.      sqli
45999 74 65 33 5f 69 6e 74 36 34 20 69 44 6f 63 69 64  te3_int64 iDocid
4599a 20 3d 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d 2d   = apSegment[0]-
4599b 3e 69 44 6f 63 69 64 3b 0a 0a 20 20 20 20 20 20  >iDocid;..      
4599c 72 63 20 3d 20 66 74 73 33 53 65 67 52 65 61 64  rc = fts3SegRead
4599d 65 72 4e 65 78 74 44 6f 63 69 64 28 70 2c 20 61  erNextDocid(p, a
4599e 70 53 65 67 6d 65 6e 74 5b 30 5d 2c 20 26 70 4c  pSegment[0], &pL
4599f 69 73 74 2c 20 26 6e 4c 69 73 74 29 3b 0a 20 20  ist, &nList);.  
459a0 20 20 20 20 6a 20 3d 20 31 3b 0a 20 20 20 20 20      j = 1;.     
459a1 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49   while( rc==SQLI
459a2 54 45 5f 4f 4b 20 0a 20 20 20 20 20 20 20 20 26  TE_OK .        &
459a3 26 20 6a 3c 6e 4d 65 72 67 65 0a 20 20 20 20 20  & j<nMerge.     
459a4 20 20 20 26 26 20 61 70 53 65 67 6d 65 6e 74 5b     && apSegment[
459a5 6a 5d 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 0a  j]->pOffsetList.
459a6 20 20 20 20 20 20 20 20 26 26 20 61 70 53 65 67          && apSeg
459a7 6d 65 6e 74 5b 6a 5d 2d 3e 69 44 6f 63 69 64 3d  ment[j]->iDocid=
459a8 3d 69 44 6f 63 69 64 0a 20 20 20 20 20 20 29 7b  =iDocid.      ){
459a9 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 74  .        rc = ft
459aa 73 33 53 65 67 52 65 61 64 65 72 4e 65 78 74 44  s3SegReaderNextD
459ab 6f 63 69 64 28 70 2c 20 61 70 53 65 67 6d 65 6e  ocid(p, apSegmen
459ac 74 5b 6a 5d 2c 20 30 2c 20 30 29 3b 0a 20 20 20  t[j], 0, 0);.   
459ad 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20       j++;.      
459ae 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  }.      if( rc!=
459af 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
459b0 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 66 74 73  rn rc;.      fts
459b1 33 53 65 67 52 65 61 64 65 72 53 6f 72 74 28 70  3SegReaderSort(p
459b2 4d 73 72 2d 3e 61 70 53 65 67 6d 65 6e 74 2c 20  Msr->apSegment, 
459b3 6e 4d 65 72 67 65 2c 20 6a 2c 20 78 43 6d 70 29  nMerge, j, xCmp)
459b4 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 4d 73  ;..      if( pMs
459b5 72 2d 3e 69 43 6f 6c 46 69 6c 74 65 72 3e 3d 30  r->iColFilter>=0
459b6 20 29 7b 0a 20 20 20 20 20 20 20 20 66 74 73 33   ){.        fts3
459b7 43 6f 6c 75 6d 6e 46 69 6c 74 65 72 28 70 4d 73  ColumnFilter(pMs
459b8 72 2d 3e 69 43 6f 6c 46 69 6c 74 65 72 2c 20 26  r->iColFilter, &
459b9 70 4c 69 73 74 2c 20 26 6e 4c 69 73 74 29 3b 0a  pList, &nList);.
459ba 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69        }..      i
459bb 66 28 20 6e 4c 69 73 74 3e 30 20 29 7b 0a 20 20  f( nList>0 ){.  
459bc 20 20 20 20 20 20 69 66 28 20 66 74 73 33 53 65        if( fts3Se
459bd 67 52 65 61 64 65 72 49 73 50 65 6e 64 69 6e 67  gReaderIsPending
459be 28 61 70 53 65 67 6d 65 6e 74 5b 30 5d 29 20 29  (apSegment[0]) )
459bf 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
459c0 20 66 74 73 33 4d 73 72 42 75 66 66 65 72 44 61   fts3MsrBufferDa
459c1 74 61 28 70 4d 73 72 2c 20 70 4c 69 73 74 2c 20  ta(pMsr, pList, 
459c2 6e 4c 69 73 74 2b 31 29 3b 0a 20 20 20 20 20 20  nList+1);.      
459c3 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
459c4 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
459c5 63 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 61  c;.          *pa
459c6 50 6f 73 6c 69 73 74 20 3d 20 70 4d 73 72 2d 3e  Poslist = pMsr->
459c7 61 42 75 66 66 65 72 3b 0a 20 20 20 20 20 20 20  aBuffer;.       
459c8 20 20 20 61 73 73 65 72 74 28 20 28 70 4d 73 72     assert( (pMsr
459c9 2d 3e 61 42 75 66 66 65 72 5b 6e 4c 69 73 74 5d  ->aBuffer[nList]
459ca 20 26 20 30 78 46 45 29 3d 3d 30 78 30 30 20 29   & 0xFE)==0x00 )
459cb 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
459cc 0a 20 20 20 20 20 20 20 20 20 20 2a 70 61 50 6f  .          *paPo
459cd 73 6c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20  slist = pList;. 
459ce 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
459cf 20 2a 70 69 44 6f 63 69 64 20 3d 20 69 44 6f 63   *piDocid = iDoc
459d0 69 64 3b 0a 20 20 20 20 20 20 20 20 2a 70 6e 50  id;.        *pnP
459d1 6f 73 6c 69 73 74 20 3d 20 6e 4c 69 73 74 3b 0a  oslist = nList;.
459d2 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
459d3 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
459d4 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
459d5 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  E_OK;.}..static 
459d6 69 6e 74 20 66 74 73 33 53 65 67 52 65 61 64 65  int fts3SegReade
459d7 72 53 74 61 72 74 28 0a 20 20 46 74 73 33 54 61  rStart(.  Fts3Ta
459d8 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ble *p,         
459d9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72            /* Vir
459da 74 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c  tual table handl
459db 65 20 2a 2f 0a 20 20 46 74 73 33 4d 75 6c 74 69  e */.  Fts3Multi
459dc 53 65 67 52 65 61 64 65 72 20 2a 70 43 73 72 2c  SegReader *pCsr,
459dd 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
459de 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e   object */.  con
459df 73 74 20 63 68 61 72 20 2a 7a 54 65 72 6d 2c 20  st char *zTerm, 
459e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
459e1 54 65 72 6d 20 73 65 61 72 63 68 65 64 20 66 6f  Term searched fo
459e2 72 20 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20  r (or NULL) */. 
459e3 20 69 6e 74 20 6e 54 65 72 6d 20 20 20 20 20 20   int nTerm      
459e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
459e5 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 54   /* Length of zT
459e6 65 72 6d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  erm in bytes */.
459e7 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
459e8 74 20 6e 53 65 67 20 3d 20 70 43 73 72 2d 3e 6e  t nSeg = pCsr->n
459e9 53 65 67 6d 65 6e 74 3b 0a 0a 20 20 2f 2a 20 49  Segment;..  /* I
459ea 66 20 74 68 65 20 46 74 73 33 53 65 67 46 69 6c  f the Fts3SegFil
459eb 74 65 72 20 64 65 66 69 6e 65 73 20 61 20 73 70  ter defines a sp
459ec 65 63 69 66 69 63 20 74 65 72 6d 20 28 6f 72 20  ecific term (or 
459ed 74 65 72 6d 20 70 72 65 66 69 78 29 20 74 6f 20  term prefix) to 
459ee 73 65 61 72 63 68 20 0a 20 20 2a 2a 20 66 6f 72  search .  ** for
459ef 2c 20 74 68 65 6e 20 61 64 76 61 6e 63 65 20 65  , then advance e
459f0 61 63 68 20 73 65 67 6d 65 6e 74 20 69 74 65 72  ach segment iter
459f1 61 74 6f 72 20 75 6e 74 69 6c 20 69 74 20 70 6f  ator until it po
459f2 69 6e 74 73 20 74 6f 20 61 20 74 65 72 6d 20 6f  ints to a term o
459f3 66 0a 20 20 2a 2a 20 65 71 75 61 6c 20 6f 72 20  f.  ** equal or 
459f4 67 72 65 61 74 65 72 20 76 61 6c 75 65 20 74 68  greater value th
459f5 61 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64  an the specified
459f6 20 74 65 72 6d 2e 20 54 68 69 73 20 70 72 65 76   term. This prev
459f7 65 6e 74 73 20 6d 61 6e 79 0a 20 20 2a 2a 20 75  ents many.  ** u
459f8 6e 6e 65 63 65 73 73 61 72 79 20 6d 65 72 67 65  nnecessary merge
459f9 2f 73 6f 72 74 20 6f 70 65 72 61 74 69 6f 6e 73  /sort operations
459fa 20 66 6f 72 20 74 68 65 20 63 61 73 65 20 77 68   for the case wh
459fb 65 72 65 20 73 69 6e 67 6c 65 20 73 65 67 6d 65  ere single segme
459fc 6e 74 0a 20 20 2a 2a 20 62 2d 74 72 65 65 20 6c  nt.  ** b-tree l
459fd 65 61 66 20 6e 6f 64 65 73 20 63 6f 6e 74 61 69  eaf nodes contai
459fe 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20  n more than one 
459ff 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  term..  */.  for
45a00 28 69 3d 30 3b 20 70 43 73 72 2d 3e 62 52 65 73  (i=0; pCsr->bRes
45a01 74 61 72 74 3d 3d 30 20 26 26 20 69 3c 70 43 73  tart==0 && i<pCs
45a02 72 2d 3e 6e 53 65 67 6d 65 6e 74 3b 20 69 2b 2b  r->nSegment; i++
45a03 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 20 3d  ){.    int res =
45a04 20 30 3b 0a 20 20 20 20 46 74 73 33 53 65 67 52   0;.    Fts3SegR
45a05 65 61 64 65 72 20 2a 70 53 65 67 20 3d 20 70 43  eader *pSeg = pC
45a06 73 72 2d 3e 61 70 53 65 67 6d 65 6e 74 5b 69 5d  sr->apSegment[i]
45a07 3b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20  ;.    do {.     
45a08 20 69 6e 74 20 72 63 20 3d 20 66 74 73 33 53 65   int rc = fts3Se
45a09 67 52 65 61 64 65 72 4e 65 78 74 28 70 2c 20 70  gReaderNext(p, p
45a0a 53 65 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  Seg, 0);.      i
45a0b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
45a0c 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
45a0d 20 20 7d 77 68 69 6c 65 28 20 7a 54 65 72 6d 20    }while( zTerm 
45a0e 26 26 20 28 72 65 73 20 3d 20 66 74 73 33 53 65  && (res = fts3Se
45a0f 67 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 70  gReaderTermCmp(p
45a10 53 65 67 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72  Seg, zTerm, nTer
45a11 6d 29 29 3c 30 20 29 3b 0a 0a 20 20 20 20 69 66  m))<0 );..    if
45a12 28 20 70 53 65 67 2d 3e 62 4c 6f 6f 6b 75 70 20  ( pSeg->bLookup 
45a13 26 26 20 72 65 73 21 3d 30 20 29 7b 0a 20 20 20  && res!=0 ){.   
45a14 20 20 20 66 74 73 33 53 65 67 52 65 61 64 65 72     fts3SegReader
45a15 53 65 74 45 6f 66 28 70 53 65 67 29 3b 0a 20 20  SetEof(pSeg);.  
45a16 20 20 7d 0a 20 20 7d 0a 20 20 66 74 73 33 53 65    }.  }.  fts3Se
45a17 67 52 65 61 64 65 72 53 6f 72 74 28 70 43 73 72  gReaderSort(pCsr
45a18 2d 3e 61 70 53 65 67 6d 65 6e 74 2c 20 6e 53 65  ->apSegment, nSe
45a19 67 2c 20 6e 53 65 67 2c 20 66 74 73 33 53 65 67  g, nSeg, fts3Seg
45a1a 52 65 61 64 65 72 43 6d 70 29 3b 0a 0a 20 20 72  ReaderCmp);..  r
45a1b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
45a1c 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
45a1d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
45a1e 73 33 53 65 67 52 65 61 64 65 72 53 74 61 72 74  s3SegReaderStart
45a1f 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  (.  Fts3Table *p
45a20 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
45a21 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74      /* Virtual t
45a22 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  able handle */. 
45a23 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61   Fts3MultiSegRea
45a24 64 65 72 20 2a 70 43 73 72 2c 20 20 20 20 20 20  der *pCsr,      
45a25 20 2f 2a 20 43 75 72 73 6f 72 20 6f 62 6a 65 63   /* Cursor objec
45a26 74 20 2a 2f 0a 20 20 46 74 73 33 53 65 67 46 69  t */.  Fts3SegFi
45a27 6c 74 65 72 20 2a 70 46 69 6c 74 65 72 20 20 20  lter *pFilter   
45a28 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69         /* Restri
45a29 63 74 69 6f 6e 73 20 6f 6e 20 72 61 6e 67 65 20  ctions on range 
45a2a 6f 66 20 69 74 65 72 61 74 69 6f 6e 20 2a 2f 0a  of iteration */.
45a2b 29 7b 0a 20 20 70 43 73 72 2d 3e 70 46 69 6c 74  ){.  pCsr->pFilt
45a2c 65 72 20 3d 20 70 46 69 6c 74 65 72 3b 0a 20 20  er = pFilter;.  
45a2d 72 65 74 75 72 6e 20 66 74 73 33 53 65 67 52 65  return fts3SegRe
45a2e 61 64 65 72 53 74 61 72 74 28 70 2c 20 70 43 73  aderStart(p, pCs
45a2f 72 2c 20 70 46 69 6c 74 65 72 2d 3e 7a 54 65 72  r, pFilter->zTer
45a30 6d 2c 20 70 46 69 6c 74 65 72 2d 3e 6e 54 65 72  m, pFilter->nTer
45a31 6d 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  m);.}..SQLITE_PR
45a32 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
45a33 33 46 74 73 33 4d 73 72 49 6e 63 72 53 74 61 72  3Fts3MsrIncrStar
45a34 74 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  t(.  Fts3Table *
45a35 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
45a36 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20       /* Virtual 
45a37 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  table handle */.
45a38 20 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65    Fts3MultiSegRe
45a39 61 64 65 72 20 2a 70 43 73 72 2c 20 20 20 20 20  ader *pCsr,     
45a3a 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 62 6a 65    /* Cursor obje
45a3b 63 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c  ct */.  int iCol
45a3c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
45a3d 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
45a3e 6e 20 74 6f 20 6d 61 74 63 68 20 6f 6e 2e 20 2a  n to match on. *
45a3f 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
45a40 7a 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20  zTerm,          
45a41 20 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20 69      /* Term to i
45a42 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61  terate through a
45a43 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 2a 2f 0a   doclist for */.
45a44 20 20 69 6e 74 20 6e 54 65 72 6d 20 20 20 20 20    int nTerm     
45a45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45a46 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
45a47 79 74 65 73 20 69 6e 20 7a 54 65 72 6d 20 2a 2f  ytes in zTerm */
45a48 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  .){.  int i;.  i
45a49 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 53 65  nt rc;.  int nSe
45a4a 67 6d 65 6e 74 20 3d 20 70 43 73 72 2d 3e 6e 53  gment = pCsr->nS
45a4b 65 67 6d 65 6e 74 3b 0a 20 20 69 6e 74 20 28 2a  egment;.  int (*
45a4c 78 43 6d 70 29 28 46 74 73 33 53 65 67 52 65 61  xCmp)(Fts3SegRea
45a4d 64 65 72 20 2a 2c 20 46 74 73 33 53 65 67 52 65  der *, Fts3SegRe
45a4e 61 64 65 72 20 2a 29 20 3d 20 28 0a 20 20 20 20  ader *) = (.    
45a4f 70 2d 3e 62 44 65 73 63 49 64 78 20 3f 20 66 74  p->bDescIdx ? ft
45a50 73 33 53 65 67 52 65 61 64 65 72 44 6f 63 6c 69  s3SegReaderDocli
45a51 73 74 43 6d 70 52 65 76 20 3a 20 66 74 73 33 53  stCmpRev : fts3S
45a52 65 67 52 65 61 64 65 72 44 6f 63 6c 69 73 74 43  egReaderDoclistC
45a53 6d 70 0a 20 20 29 3b 0a 0a 20 20 61 73 73 65 72  mp.  );..  asser
45a54 74 28 20 70 43 73 72 2d 3e 70 46 69 6c 74 65 72  t( pCsr->pFilter
45a55 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
45a56 20 7a 54 65 72 6d 20 26 26 20 6e 54 65 72 6d 3e   zTerm && nTerm>
45a57 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 64 76 61 6e  0 );..  /* Advan
45a58 63 65 20 65 61 63 68 20 73 65 67 6d 65 6e 74 20  ce each segment 
45a59 69 74 65 72 61 74 6f 72 20 75 6e 74 69 6c 20 69  iterator until i
45a5a 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  t points to the 
45a5b 74 65 72 6d 20 7a 54 65 72 6d 2f 6e 54 65 72 6d  term zTerm/nTerm
45a5c 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 66 74 73 33  . */.  rc = fts3
45a5d 53 65 67 52 65 61 64 65 72 53 74 61 72 74 28 70  SegReaderStart(p
45a5e 2c 20 70 43 73 72 2c 20 7a 54 65 72 6d 2c 20 6e  , pCsr, zTerm, n
45a5f 54 65 72 6d 29 3b 0a 20 20 69 66 28 20 72 63 21  Term);.  if( rc!
45a60 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
45a61 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 65  urn rc;..  /* De
45a62 74 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79  termine how many
45a63 20 6f 66 20 74 68 65 20 73 65 67 6d 65 6e 74 73   of the segments
45a64 20 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e 74 20   actually point 
45a65 74 6f 20 7a 54 65 72 6d 2f 6e 54 65 72 6d 2e 20  to zTerm/nTerm. 
45a66 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
45a67 6e 53 65 67 6d 65 6e 74 3b 20 69 2b 2b 29 7b 0a  nSegment; i++){.
45a68 20 20 20 20 46 74 73 33 53 65 67 52 65 61 64 65      Fts3SegReade
45a69 72 20 2a 70 53 65 67 20 3d 20 70 43 73 72 2d 3e  r *pSeg = pCsr->
45a6a 61 70 53 65 67 6d 65 6e 74 5b 69 5d 3b 0a 20 20  apSegment[i];.  
45a6b 20 20 69 66 28 20 21 70 53 65 67 2d 3e 61 4e 6f    if( !pSeg->aNo
45a6c 64 65 20 7c 7c 20 66 74 73 33 53 65 67 52 65 61  de || fts3SegRea
45a6d 64 65 72 54 65 72 6d 43 6d 70 28 70 53 65 67 2c  derTermCmp(pSeg,
45a6e 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 20 29   zTerm, nTerm) )
45a6f 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  {.      break;. 
45a70 20 20 20 7d 0a 20 20 7d 0a 20 20 70 43 73 72 2d     }.  }.  pCsr-
45a71 3e 6e 41 64 76 61 6e 63 65 20 3d 20 69 3b 0a 0a  >nAdvance = i;..
45a72 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 65 61 63    /* Advance eac
45a73 68 20 6f 66 20 74 68 65 20 73 65 67 6d 65 6e 74  h of the segment
45a74 73 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  s to point to th
45a75 65 20 66 69 72 73 74 20 64 6f 63 69 64 2e 20 2a  e first docid. *
45a76 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  /.  for(i=0; i<p
45a77 43 73 72 2d 3e 6e 41 64 76 61 6e 63 65 3b 20 69  Csr->nAdvance; i
45a78 2b 2b 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 74  ++){.    rc = ft
45a79 73 33 53 65 67 52 65 61 64 65 72 46 69 72 73 74  s3SegReaderFirst
45a7a 44 6f 63 69 64 28 70 2c 20 70 43 73 72 2d 3e 61  Docid(p, pCsr->a
45a7b 70 53 65 67 6d 65 6e 74 5b 69 5d 29 3b 0a 20 20  pSegment[i]);.  
45a7c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
45a7d 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
45a7e 0a 20 20 7d 0a 20 20 66 74 73 33 53 65 67 52 65  .  }.  fts3SegRe
45a7f 61 64 65 72 53 6f 72 74 28 70 43 73 72 2d 3e 61  aderSort(pCsr->a
45a80 70 53 65 67 6d 65 6e 74 2c 20 69 2c 20 69 2c 20  pSegment, i, i, 
45a81 78 43 6d 70 29 3b 0a 0a 20 20 61 73 73 65 72 74  xCmp);..  assert
45a82 28 20 69 43 6f 6c 3c 30 20 7c 7c 20 69 43 6f 6c  ( iCol<0 || iCol
45a83 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 20  <p->nColumn );. 
45a84 20 70 43 73 72 2d 3e 69 43 6f 6c 46 69 6c 74 65   pCsr->iColFilte
45a85 72 20 3d 20 69 43 6f 6c 3b 0a 0a 20 20 72 65 74  r = iCol;..  ret
45a86 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
45a87 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
45a88 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
45a89 6f 6e 20 61 20 4d 75 6c 74 69 53 65 67 52 65 61  on a MultiSegRea
45a8a 64 65 72 20 74 68 61 74 20 68 61 73 20 62 65 65  der that has bee
45a8b 6e 20 73 74 61 72 74 65 64 20 75 73 69 6e 67 0a  n started using.
45a8c 2a 2a 20 73 71 6c 69 74 65 33 46 74 73 33 4d 73  ** sqlite3Fts3Ms
45a8d 72 49 6e 63 72 53 74 61 72 74 28 29 2e 20 4f 6e  rIncrStart(). On
45a8e 65 20 6f 72 20 6d 6f 72 65 20 63 61 6c 6c 73 20  e or more calls 
45a8f 74 6f 20 4d 73 72 49 6e 63 72 4e 65 78 74 28 29  to MsrIncrNext()
45a90 20 6d 61 79 20 61 6c 73 6f 0a 2a 2a 20 68 61 76   may also.** hav
45a91 65 20 62 65 65 6e 20 6d 61 64 65 2e 20 43 61 6c  e been made. Cal
45a92 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69  ling this functi
45a93 6f 6e 20 70 75 74 73 20 74 68 65 20 4d 75 6c 74  on puts the Mult
45a94 69 53 65 67 52 65 61 64 65 72 20 69 6e 20 73 75  iSegReader in su
45a95 63 68 0a 2a 2a 20 61 20 73 74 61 74 65 20 74 68  ch.** a state th
45a96 61 74 20 69 66 20 74 68 65 20 6e 65 78 74 20 74  at if the next t
45a97 77 6f 20 63 61 6c 6c 73 20 61 72 65 3a 0a 2a 2a  wo calls are:.**
45a98 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46 74 73  .**   sqlite3Fts
45a99 33 53 65 67 52 65 61 64 65 72 53 74 61 72 74 28  3SegReaderStart(
45a9a 29 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46 74  ).**   sqlite3Ft
45a9b 73 33 53 65 67 52 65 61 64 65 72 53 74 65 70 28  s3SegReaderStep(
45a9c 29 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ).**.** then the
45a9d 20 65 6e 74 69 72 65 20 64 6f 63 6c 69 73 74 20   entire doclist 
45a9e 66 6f 72 20 74 68 65 20 74 65 72 6d 20 69 73 20  for the term is 
45a9f 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 0a 2a 2a  available in .**
45aa0 20 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72 2e   MultiSegReader.
45aa1 61 44 6f 63 6c 69 73 74 2f 6e 44 6f 63 6c 69 73  aDoclist/nDoclis
45aa2 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
45aa3 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
45aa4 46 74 73 33 4d 73 72 49 6e 63 72 52 65 73 74 61  Fts3MsrIncrResta
45aa5 72 74 28 46 74 73 33 4d 75 6c 74 69 53 65 67 52  rt(Fts3MultiSegR
45aa6 65 61 64 65 72 20 2a 70 43 73 72 29 7b 0a 20 20  eader *pCsr){.  
45aa7 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
45aa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45aa9 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72 61  /* Used to itera
45aaa 74 65 20 74 68 72 6f 75 67 68 20 73 65 67 6d 65  te through segme
45aab 6e 74 2d 72 65 61 64 65 72 73 20 2a 2f 0a 0a 20  nt-readers */.. 
45aac 20 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e 7a   assert( pCsr->z
45aad 54 65 72 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Term==0 );.  ass
45aae 65 72 74 28 20 70 43 73 72 2d 3e 6e 54 65 72 6d  ert( pCsr->nTerm
45aaf 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
45ab0 20 70 43 73 72 2d 3e 61 44 6f 63 6c 69 73 74 3d   pCsr->aDoclist=
45ab1 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
45ab2 70 43 73 72 2d 3e 6e 44 6f 63 6c 69 73 74 3d 3d  pCsr->nDoclist==
45ab3 30 20 29 3b 0a 0a 20 20 70 43 73 72 2d 3e 6e 41  0 );..  pCsr->nA
45ab4 64 76 61 6e 63 65 20 3d 20 30 3b 0a 20 20 70 43  dvance = 0;.  pC
45ab5 73 72 2d 3e 62 52 65 73 74 61 72 74 20 3d 20 31  sr->bRestart = 1
45ab6 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
45ab7 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 3b 20 69  Csr->nSegment; i
45ab8 2b 2b 29 7b 0a 20 20 20 20 70 43 73 72 2d 3e 61  ++){.    pCsr->a
45ab9 70 53 65 67 6d 65 6e 74 5b 69 5d 2d 3e 70 4f 66  pSegment[i]->pOf
45aba 66 73 65 74 4c 69 73 74 20 3d 20 30 3b 0a 20 20  fsetList = 0;.  
45abb 20 20 70 43 73 72 2d 3e 61 70 53 65 67 6d 65 6e    pCsr->apSegmen
45abc 74 5b 69 5d 2d 3e 6e 4f 66 66 73 65 74 4c 69 73  t[i]->nOffsetLis
45abd 74 20 3d 20 30 3b 0a 20 20 20 20 70 43 73 72 2d  t = 0;.    pCsr-
45abe 3e 61 70 53 65 67 6d 65 6e 74 5b 69 5d 2d 3e 69  >apSegment[i]->i
45abf 44 6f 63 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a  Docid = 0;.  }..
45ac0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
45ac1 4f 4b 3b 0a 7d 0a 0a 0a 53 51 4c 49 54 45 5f 50  OK;.}...SQLITE_P
45ac2 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
45ac3 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72 53  e3Fts3SegReaderS
45ac4 74 65 70 28 0a 20 20 46 74 73 33 54 61 62 6c 65  tep(.  Fts3Table
45ac5 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
45ac6 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61         /* Virtua
45ac7 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  l table handle *
45ac8 2f 0a 20 20 46 74 73 33 4d 75 6c 74 69 53 65 67  /.  Fts3MultiSeg
45ac9 52 65 61 64 65 72 20 2a 70 43 73 72 20 20 20 20  Reader *pCsr    
45aca 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 62      /* Cursor ob
45acb 6a 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ject */.){.  int
45acc 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
45acd 0a 0a 20 20 69 6e 74 20 69 73 49 67 6e 6f 72 65  ..  int isIgnore
45ace 45 6d 70 74 79 20 3d 20 20 28 70 43 73 72 2d 3e  Empty =  (pCsr->
45acf 70 46 69 6c 74 65 72 2d 3e 66 6c 61 67 73 20 26  pFilter->flags &
45ad0 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 49 47   FTS3_SEGMENT_IG
45ad1 4e 4f 52 45 5f 45 4d 50 54 59 29 3b 0a 20 20 69  NORE_EMPTY);.  i
45ad2 6e 74 20 69 73 52 65 71 75 69 72 65 50 6f 73 20  nt isRequirePos 
45ad3 3d 20 20 20 28 70 43 73 72 2d 3e 70 46 69 6c 74  =   (pCsr->pFilt
45ad4 65 72 2d 3e 66 6c 61 67 73 20 26 20 46 54 53 33  er->flags & FTS3
45ad5 5f 53 45 47 4d 45 4e 54 5f 52 45 51 55 49 52 45  _SEGMENT_REQUIRE
45ad6 5f 50 4f 53 29 3b 0a 20 20 69 6e 74 20 69 73 43  _POS);.  int isC
45ad7 6f 6c 46 69 6c 74 65 72 20 3d 20 20 20 20 28 70  olFilter =    (p
45ad8 43 73 72 2d 3e 70 46 69 6c 74 65 72 2d 3e 66 6c  Csr->pFilter->fl
45ad9 61 67 73 20 26 20 46 54 53 33 5f 53 45 47 4d 45  ags & FTS3_SEGME
45ada 4e 54 5f 43 4f 4c 55 4d 4e 5f 46 49 4c 54 45 52  NT_COLUMN_FILTER
45adb 29 3b 0a 20 20 69 6e 74 20 69 73 50 72 65 66 69  );.  int isPrefi
45adc 78 20 3d 20 20 20 20 20 20 20 28 70 43 73 72 2d  x =       (pCsr-
45add 3e 70 46 69 6c 74 65 72 2d 3e 66 6c 61 67 73 20  >pFilter->flags 
45ade 26 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 50  & FTS3_SEGMENT_P
45adf 52 45 46 49 58 29 3b 0a 20 20 69 6e 74 20 69 73  REFIX);.  int is
45ae0 53 63 61 6e 20 3d 20 20 20 20 20 20 20 20 20 28  Scan =         (
45ae1 70 43 73 72 2d 3e 70 46 69 6c 74 65 72 2d 3e 66  pCsr->pFilter->f
45ae2 6c 61 67 73 20 26 20 46 54 53 33 5f 53 45 47 4d  lags & FTS3_SEGM
45ae3 45 4e 54 5f 53 43 41 4e 29 3b 0a 20 20 69 6e 74  ENT_SCAN);.  int
45ae4 20 69 73 46 69 72 73 74 20 3d 20 20 20 20 20 20   isFirst =      
45ae5 20 20 28 70 43 73 72 2d 3e 70 46 69 6c 74 65 72    (pCsr->pFilter
45ae6 2d 3e 66 6c 61 67 73 20 26 20 46 54 53 33 5f 53  ->flags & FTS3_S
45ae7 45 47 4d 45 4e 54 5f 46 49 52 53 54 29 3b 0a 0a  EGMENT_FIRST);..
45ae8 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20    Fts3SegReader 
45ae9 2a 2a 61 70 53 65 67 6d 65 6e 74 20 3d 20 70 43  **apSegment = pC
45aea 73 72 2d 3e 61 70 53 65 67 6d 65 6e 74 3b 0a 20  sr->apSegment;. 
45aeb 20 69 6e 74 20 6e 53 65 67 6d 65 6e 74 20 3d 20   int nSegment = 
45aec 70 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 3b 0a  pCsr->nSegment;.
45aed 20 20 46 74 73 33 53 65 67 46 69 6c 74 65 72 20    Fts3SegFilter 
45aee 2a 70 46 69 6c 74 65 72 20 3d 20 70 43 73 72 2d  *pFilter = pCsr-
45aef 3e 70 46 69 6c 74 65 72 3b 0a 20 20 69 6e 74 20  >pFilter;.  int 
45af0 28 2a 78 43 6d 70 29 28 46 74 73 33 53 65 67 52  (*xCmp)(Fts3SegR
45af1 65 61 64 65 72 20 2a 2c 20 46 74 73 33 53 65 67  eader *, Fts3Seg
45af2 52 65 61 64 65 72 20 2a 29 20 3d 20 28 0a 20 20  Reader *) = (.  
45af3 20 20 70 2d 3e 62 44 65 73 63 49 64 78 20 3f 20    p->bDescIdx ? 
45af4 66 74 73 33 53 65 67 52 65 61 64 65 72 44 6f 63  fts3SegReaderDoc
45af5 6c 69 73 74 43 6d 70 52 65 76 20 3a 20 66 74 73  listCmpRev : fts
45af6 33 53 65 67 52 65 61 64 65 72 44 6f 63 6c 69 73  3SegReaderDoclis
45af7 74 43 6d 70 0a 20 20 29 3b 0a 0a 20 20 69 66 28  tCmp.  );..  if(
45af8 20 70 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 3d   pCsr->nSegment=
45af9 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
45afa 54 45 5f 4f 4b 3b 0a 0a 20 20 64 6f 20 7b 0a 20  TE_OK;..  do {. 
45afb 20 20 20 69 6e 74 20 6e 4d 65 72 67 65 3b 0a 20     int nMerge;. 
45afc 20 20 20 69 6e 74 20 69 3b 0a 20 20 0a 20 20 20     int i;.  .   
45afd 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 68 65 20   /* Advance the 
45afe 66 69 72 73 74 20 70 43 73 72 2d 3e 6e 41 64 76  first pCsr->nAdv
45aff 61 6e 63 65 20 65 6e 74 72 69 65 73 20 69 6e 20  ance entries in 
45b00 74 68 65 20 61 70 53 65 67 6d 65 6e 74 5b 5d 20  the apSegment[] 
45b01 61 72 72 61 79 0a 20 20 20 20 2a 2a 20 66 6f 72  array.    ** for
45b02 77 61 72 64 2e 20 54 68 65 6e 20 73 6f 72 74 20  ward. Then sort 
45b03 74 68 65 20 6c 69 73 74 20 69 6e 20 6f 72 64 65  the list in orde
45b04 72 20 6f 66 20 63 75 72 72 65 6e 74 20 74 65 72  r of current ter
45b05 6d 20 61 67 61 69 6e 2e 20 20 0a 20 20 20 20 2a  m again.  .    *
45b06 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
45b07 3c 70 43 73 72 2d 3e 6e 41 64 76 61 6e 63 65 3b  <pCsr->nAdvance;
45b08 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 46 74 73   i++){.      Fts
45b09 33 53 65 67 52 65 61 64 65 72 20 2a 70 53 65 67  3SegReader *pSeg
45b0a 20 3d 20 61 70 53 65 67 6d 65 6e 74 5b 69 5d 3b   = apSegment[i];
45b0b 0a 20 20 20 20 20 20 69 66 28 20 70 53 65 67 2d  .      if( pSeg-
45b0c 3e 62 4c 6f 6f 6b 75 70 20 29 7b 0a 20 20 20 20  >bLookup ){.    
45b0d 20 20 20 20 66 74 73 33 53 65 67 52 65 61 64 65      fts3SegReade
45b0e 72 53 65 74 45 6f 66 28 70 53 65 67 29 3b 0a 20  rSetEof(pSeg);. 
45b0f 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
45b10 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 65 67      rc = fts3Seg
45b11 52 65 61 64 65 72 4e 65 78 74 28 70 2c 20 70 53  ReaderNext(p, pS
45b12 65 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  eg, 0);.      }.
45b13 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
45b14 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
45b15 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66   rc;.    }.    f
45b16 74 73 33 53 65 67 52 65 61 64 65 72 53 6f 72 74  ts3SegReaderSort
45b17 28 61 70 53 65 67 6d 65 6e 74 2c 20 6e 53 65 67  (apSegment, nSeg
45b18 6d 65 6e 74 2c 20 70 43 73 72 2d 3e 6e 41 64 76  ment, pCsr->nAdv
45b19 61 6e 63 65 2c 20 66 74 73 33 53 65 67 52 65 61  ance, fts3SegRea
45b1a 64 65 72 43 6d 70 29 3b 0a 20 20 20 20 70 43 73  derCmp);.    pCs
45b1b 72 2d 3e 6e 41 64 76 61 6e 63 65 20 3d 20 30 3b  r->nAdvance = 0;
45b1c 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6c 6c 20  ..    /* If all 
45b1d 74 68 65 20 73 65 67 2d 72 65 61 64 65 72 73 20  the seg-readers 
45b1e 61 72 65 20 61 74 20 45 4f 46 2c 20 77 65 27 72  are at EOF, we'r
45b1f 65 20 66 69 6e 69 73 68 65 64 2e 20 72 65 74 75  e finished. retu
45b20 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 2a 2f  rn SQLITE_OK. */
45b21 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d  .    assert( rc=
45b22 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
45b23 20 20 69 66 28 20 61 70 53 65 67 6d 65 6e 74 5b    if( apSegment[
45b24 30 5d 2d 3e 61 4e 6f 64 65 3d 3d 30 20 29 20 62  0]->aNode==0 ) b
45b25 72 65 61 6b 3b 0a 0a 20 20 20 20 70 43 73 72 2d  reak;..    pCsr-
45b26 3e 6e 54 65 72 6d 20 3d 20 61 70 53 65 67 6d 65  >nTerm = apSegme
45b27 6e 74 5b 30 5d 2d 3e 6e 54 65 72 6d 3b 0a 20 20  nt[0]->nTerm;.  
45b28 20 20 70 43 73 72 2d 3e 7a 54 65 72 6d 20 3d 20    pCsr->zTerm = 
45b29 61 70 53 65 67 6d 65 6e 74 5b 30 5d 2d 3e 7a 54  apSegment[0]->zT
45b2a 65 72 6d 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  erm;..    /* If 
45b2b 74 68 69 73 20 69 73 20 61 20 70 72 65 66 69 78  this is a prefix
45b2c 2d 73 65 61 72 63 68 2c 20 61 6e 64 20 69 66 20  -search, and if 
45b2d 74 68 65 20 74 65 72 6d 20 74 68 61 74 20 61 70  the term that ap
45b2e 53 65 67 6d 65 6e 74 5b 30 5d 20 70 6f 69 6e 74  Segment[0] point
45b2f 73 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f 65 73  s.    ** to does
45b30 20 6e 6f 74 20 73 68 61 72 65 20 61 20 73 75 66   not share a suf
45b31 66 69 78 20 77 69 74 68 20 70 46 69 6c 74 65 72  fix with pFilter
45b32 2d 3e 7a 54 65 72 6d 2f 6e 54 65 72 6d 2c 20 74  ->zTerm/nTerm, t
45b33 68 65 6e 20 61 6c 6c 20 0a 20 20 20 20 2a 2a 20  hen all .    ** 
45b34 72 65 71 75 69 72 65 64 20 63 61 6c 6c 62 61 63  required callbac
45b35 6b 73 20 68 61 76 65 20 62 65 65 6e 20 6d 61 64  ks have been mad
45b36 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  e. In this case 
45b37 65 78 69 74 20 65 61 72 6c 79 2e 0a 20 20 20 20  exit early..    
45b38 2a 2a 0a 20 20 20 20 2a 2a 20 53 69 6d 69 6c 61  **.    ** Simila
45b39 72 6c 79 2c 20 69 66 20 74 68 69 73 20 69 73 20  rly, if this is 
45b3a 61 20 73 65 61 72 63 68 20 66 6f 72 20 61 6e 20  a search for an 
45b3b 65 78 61 63 74 20 6d 61 74 63 68 2c 20 61 6e 64  exact match, and
45b3c 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 0a   the first term.
45b3d 20 20 20 20 2a 2a 20 6f 66 20 73 65 67 6d 65 6e      ** of segmen
45b3e 74 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d 20 69  t apSegment[0] i
45b3f 73 20 6e 6f 74 20 61 20 6d 61 74 63 68 2c 20 65  s not a match, e
45b40 78 69 74 20 65 61 72 6c 79 2e 0a 20 20 20 20 2a  xit early..    *
45b41 2f 0a 20 20 20 20 69 66 28 20 70 46 69 6c 74 65  /.    if( pFilte
45b42 72 2d 3e 7a 54 65 72 6d 20 26 26 20 21 69 73 53  r->zTerm && !isS
45b43 63 61 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28  can ){.      if(
45b44 20 70 43 73 72 2d 3e 6e 54 65 72 6d 3c 70 46 69   pCsr->nTerm<pFi
45b45 6c 74 65 72 2d 3e 6e 54 65 72 6d 20 0a 20 20 20  lter->nTerm .   
45b46 20 20 20 20 7c 7c 20 28 21 69 73 50 72 65 66 69      || (!isPrefi
45b47 78 20 26 26 20 70 43 73 72 2d 3e 6e 54 65 72 6d  x && pCsr->nTerm
45b48 3e 70 46 69 6c 74 65 72 2d 3e 6e 54 65 72 6d 29  >pFilter->nTerm)
45b49 0a 20 20 20 20 20 20 20 7c 7c 20 6d 65 6d 63 6d  .       || memcm
45b4a 70 28 70 43 73 72 2d 3e 7a 54 65 72 6d 2c 20 70  p(pCsr->zTerm, p
45b4b 46 69 6c 74 65 72 2d 3e 7a 54 65 72 6d 2c 20 70  Filter->zTerm, p
45b4c 46 69 6c 74 65 72 2d 3e 6e 54 65 72 6d 29 20 0a  Filter->nTerm) .
45b4d 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
45b4e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
45b4f 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4d 65 72 67      }..    nMerg
45b50 65 20 3d 20 31 3b 0a 20 20 20 20 77 68 69 6c 65  e = 1;.    while
45b51 28 20 6e 4d 65 72 67 65 3c 6e 53 65 67 6d 65 6e  ( nMerge<nSegmen
45b52 74 20 0a 20 20 20 20 20 20 20 20 26 26 20 61 70  t .        && ap
45b53 53 65 67 6d 65 6e 74 5b 6e 4d 65 72 67 65 5d 2d  Segment[nMerge]-
45b54 3e 61 4e 6f 64 65 0a 20 20 20 20 20 20 20 20 26  >aNode.        &
45b55 26 20 61 70 53 65 67 6d 65 6e 74 5b 6e 4d 65 72  & apSegment[nMer
45b56 67 65 5d 2d 3e 6e 54 65 72 6d 3d 3d 70 43 73 72  ge]->nTerm==pCsr
45b57 2d 3e 6e 54 65 72 6d 20 0a 20 20 20 20 20 20 20  ->nTerm .       
45b58 20 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 70 43   && 0==memcmp(pC
45b59 73 72 2d 3e 7a 54 65 72 6d 2c 20 61 70 53 65 67  sr->zTerm, apSeg
45b5a 6d 65 6e 74 5b 6e 4d 65 72 67 65 5d 2d 3e 7a 54  ment[nMerge]->zT
45b5b 65 72 6d 2c 20 70 43 73 72 2d 3e 6e 54 65 72 6d  erm, pCsr->nTerm
45b5c 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6e  ).    ){.      n
45b5d 4d 65 72 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 0a  Merge++;.    }..
45b5e 20 20 20 20 61 73 73 65 72 74 28 20 69 73 49 67      assert( isIg
45b5f 6e 6f 72 65 45 6d 70 74 79 20 7c 7c 20 28 69 73  noreEmpty || (is
45b60 52 65 71 75 69 72 65 50 6f 73 20 26 26 20 21 69  RequirePos && !i
45b61 73 43 6f 6c 46 69 6c 74 65 72 29 20 29 3b 0a 20  sColFilter) );. 
45b62 20 20 20 69 66 28 20 6e 4d 65 72 67 65 3d 3d 31     if( nMerge==1
45b63 20 0a 20 20 20 20 20 26 26 20 21 69 73 49 67 6e   .     && !isIgn
45b64 6f 72 65 45 6d 70 74 79 20 0a 20 20 20 20 20 26  oreEmpty .     &
45b65 26 20 21 69 73 46 69 72 73 74 20 0a 20 20 20 20  & !isFirst .    
45b66 20 26 26 20 28 70 2d 3e 62 44 65 73 63 49 64 78   && (p->bDescIdx
45b67 3d 3d 30 20 7c 7c 20 66 74 73 33 53 65 67 52 65  ==0 || fts3SegRe
45b68 61 64 65 72 49 73 50 65 6e 64 69 6e 67 28 61 70  aderIsPending(ap
45b69 53 65 67 6d 65 6e 74 5b 30 5d 29 3d 3d 30 29 0a  Segment[0])==0).
45b6a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 70 43 73      ){.      pCs
45b6b 72 2d 3e 6e 44 6f 63 6c 69 73 74 20 3d 20 61 70  r->nDoclist = ap
45b6c 53 65 67 6d 65 6e 74 5b 30 5d 2d 3e 6e 44 6f 63  Segment[0]->nDoc
45b6d 6c 69 73 74 3b 0a 20 20 20 20 20 20 69 66 28 20  list;.      if( 
45b6e 66 74 73 33 53 65 67 52 65 61 64 65 72 49 73 50  fts3SegReaderIsP
45b6f 65 6e 64 69 6e 67 28 61 70 53 65 67 6d 65 6e 74  ending(apSegment
45b70 5b 30 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20  [0]) ){.        
45b71 72 63 20 3d 20 66 74 73 33 4d 73 72 42 75 66 66  rc = fts3MsrBuff
45b72 65 72 44 61 74 61 28 70 43 73 72 2c 20 61 70 53  erData(pCsr, apS
45b73 65 67 6d 65 6e 74 5b 30 5d 2d 3e 61 44 6f 63 6c  egment[0]->aDocl
45b74 69 73 74 2c 20 70 43 73 72 2d 3e 6e 44 6f 63 6c  ist, pCsr->nDocl
45b75 69 73 74 29 3b 0a 20 20 20 20 20 20 20 20 70 43  ist);.        pC
45b76 73 72 2d 3e 61 44 6f 63 6c 69 73 74 20 3d 20 70  sr->aDoclist = p
45b77 43 73 72 2d 3e 61 42 75 66 66 65 72 3b 0a 20 20  Csr->aBuffer;.  
45b78 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
45b79 20 20 20 70 43 73 72 2d 3e 61 44 6f 63 6c 69 73     pCsr->aDoclis
45b7a 74 20 3d 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d  t = apSegment[0]
45b7b 2d 3e 61 44 6f 63 6c 69 73 74 3b 0a 20 20 20 20  ->aDoclist;.    
45b7c 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63    }.      if( rc
45b7d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63  ==SQLITE_OK ) rc
45b7e 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20   = SQLITE_ROW;. 
45b7f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
45b80 69 6e 74 20 6e 44 6f 63 6c 69 73 74 20 3d 20 30  int nDoclist = 0
45b81 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ;           /* S
45b82 69 7a 65 20 6f 66 20 64 6f 63 6c 69 73 74 20 2a  ize of doclist *
45b83 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  /.      sqlite3_
45b84 69 6e 74 36 34 20 69 50 72 65 76 20 3d 20 30 3b  int64 iPrev = 0;
45b85 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20      /* Previous 
45b86 64 6f 63 69 64 20 73 74 6f 72 65 64 20 69 6e 20  docid stored in 
45b87 64 6f 63 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20  doclist */..    
45b88 20 20 2f 2a 20 54 68 65 20 63 75 72 72 65 6e 74    /* The current
45b89 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66 69 72   term of the fir
45b8a 73 74 20 6e 4d 65 72 67 65 20 65 6e 74 72 69 65  st nMerge entrie
45b8b 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 0a 20  s in the array. 
45b8c 20 20 20 20 20 2a 2a 20 6f 66 20 46 74 73 33 53       ** of Fts3S
45b8d 65 67 52 65 61 64 65 72 20 6f 62 6a 65 63 74 73  egReader objects
45b8e 20 69 73 20 74 68 65 20 73 61 6d 65 2e 20 54 68   is the same. Th
45b8f 65 20 64 6f 63 6c 69 73 74 73 20 6d 75 73 74 20  e doclists must 
45b90 62 65 20 6d 65 72 67 65 64 0a 20 20 20 20 20 20  be merged.      
45b91 2a 2a 20 61 6e 64 20 61 20 73 69 6e 67 6c 65 20  ** and a single 
45b92 74 65 72 6d 20 72 65 74 75 72 6e 65 64 20 77 69  term returned wi
45b93 74 68 20 74 68 65 20 6d 65 72 67 65 64 20 64 6f  th the merged do
45b94 63 6c 69 73 74 2e 0a 20 20 20 20 20 20 2a 2f 0a  clist..      */.
45b95 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
45b96 3c 6e 4d 65 72 67 65 3b 20 69 2b 2b 29 7b 0a 20  <nMerge; i++){. 
45b97 20 20 20 20 20 20 20 66 74 73 33 53 65 67 52 65         fts3SegRe
45b98 61 64 65 72 46 69 72 73 74 44 6f 63 69 64 28 70  aderFirstDocid(p
45b99 2c 20 61 70 53 65 67 6d 65 6e 74 5b 69 5d 29 3b  , apSegment[i]);
45b9a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
45b9b 74 73 33 53 65 67 52 65 61 64 65 72 53 6f 72 74  ts3SegReaderSort
45b9c 28 61 70 53 65 67 6d 65 6e 74 2c 20 6e 4d 65 72  (apSegment, nMer
45b9d 67 65 2c 20 6e 4d 65 72 67 65 2c 20 78 43 6d 70  ge, nMerge, xCmp
45b9e 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  );.      while( 
45b9f 61 70 53 65 67 6d 65 6e 74 5b 30 5d 2d 3e 70 4f  apSegment[0]->pO
45ba0 66 66 73 65 74 4c 69 73 74 20 29 7b 0a 20 20 20  ffsetList ){.   
45ba1 20 20 20 20 20 69 6e 74 20 6a 3b 20 20 20 20 20       int j;     
45ba2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
45ba3 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 67 6d  * Number of segm
45ba4 65 6e 74 73 20 74 68 61 74 20 73 68 61 72 65 20  ents that share 
45ba5 61 20 64 6f 63 69 64 20 2a 2f 0a 20 20 20 20 20  a docid */.     
45ba6 20 20 20 63 68 61 72 20 2a 70 4c 69 73 74 3b 0a     char *pList;.
45ba7 20 20 20 20 20 20 20 20 69 6e 74 20 6e 4c 69 73          int nLis
45ba8 74 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  t;.        int n
45ba9 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 73 71  Byte;.        sq
45baa 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 44 6f 63  lite3_int64 iDoc
45bab 69 64 20 3d 20 61 70 53 65 67 6d 65 6e 74 5b 30  id = apSegment[0
45bac 5d 2d 3e 69 44 6f 63 69 64 3b 0a 20 20 20 20 20  ]->iDocid;.     
45bad 20 20 20 66 74 73 33 53 65 67 52 65 61 64 65 72     fts3SegReader
45bae 4e 65 78 74 44 6f 63 69 64 28 70 2c 20 61 70 53  NextDocid(p, apS
45baf 65 67 6d 65 6e 74 5b 30 5d 2c 20 26 70 4c 69 73  egment[0], &pLis
45bb0 74 2c 20 26 6e 4c 69 73 74 29 3b 0a 20 20 20 20  t, &nList);.    
45bb1 20 20 20 20 6a 20 3d 20 31 3b 0a 20 20 20 20 20      j = 1;.     
45bb2 20 20 20 77 68 69 6c 65 28 20 6a 3c 6e 4d 65 72     while( j<nMer
45bb3 67 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 26  ge.            &
45bb4 26 20 61 70 53 65 67 6d 65 6e 74 5b 6a 5d 2d 3e  & apSegment[j]->
45bb5 70 4f 66 66 73 65 74 4c 69 73 74 0a 20 20 20 20  pOffsetList.    
45bb6 20 20 20 20 20 20 20 20 26 26 20 61 70 53 65 67          && apSeg
45bb7 6d 65 6e 74 5b 6a 5d 2d 3e 69 44 6f 63 69 64 3d  ment[j]->iDocid=
45bb8 3d 69 44 6f 63 69 64 0a 20 20 20 20 20 20 20 20  =iDocid.        
45bb9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  ){.          fts
45bba 33 53 65 67 52 65 61 64 65 72 4e 65 78 74 44 6f  3SegReaderNextDo
45bbb 63 69 64 28 70 2c 20 61 70 53 65 67 6d 65 6e 74  cid(p, apSegment
45bbc 5b 6a 5d 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  [j], 0, 0);.    
45bbd 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20        j++;.     
45bbe 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 66     }..        if
45bbf 28 20 69 73 43 6f 6c 46 69 6c 74 65 72 20 29 7b  ( isColFilter ){
45bc0 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33 43  .          fts3C
45bc1 6f 6c 75 6d 6e 46 69 6c 74 65 72 28 70 46 69 6c  olumnFilter(pFil
45bc2 74 65 72 2d 3e 69 43 6f 6c 2c 20 26 70 4c 69 73  ter->iCol, &pLis
45bc3 74 2c 20 26 6e 4c 69 73 74 29 3b 0a 20 20 20 20  t, &nList);.    
45bc4 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69      }..        i
45bc5 66 28 20 21 69 73 49 67 6e 6f 72 65 45 6d 70 74  f( !isIgnoreEmpt
45bc6 79 20 7c 7c 20 6e 4c 69 73 74 3e 30 20 29 7b 0a  y || nList>0 ){.
45bc7 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61  .          /* Ca
45bc8 6c 63 75 6c 61 74 65 20 74 68 65 20 27 64 6f 63  lculate the 'doc
45bc9 69 64 27 20 64 65 6c 74 61 20 76 61 6c 75 65 20  id' delta value 
45bca 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 74 68  to write into th
45bcb 65 20 6d 65 72 67 65 64 20 0a 20 20 20 20 20 20  e merged .      
45bcc 20 20 20 20 2a 2a 20 64 6f 63 6c 69 73 74 2e 20      ** doclist. 
45bcd 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  */.          sql
45bce 69 74 65 33 5f 69 6e 74 36 34 20 69 44 65 6c 74  ite3_int64 iDelt
45bcf 61 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  a;.          if(
45bd0 20 70 2d 3e 62 44 65 73 63 49 64 78 20 26 26 20   p->bDescIdx && 
45bd1 6e 44 6f 63 6c 69 73 74 3e 30 20 29 7b 0a 20 20  nDoclist>0 ){.  
45bd2 20 20 20 20 20 20 20 20 20 20 69 44 65 6c 74 61            iDelta
45bd3 20 3d 20 69 50 72 65 76 20 2d 20 69 44 6f 63 69   = iPrev - iDoci
45bd4 64 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  d;.          }el
45bd5 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
45bd6 69 44 65 6c 74 61 20 3d 20 69 44 6f 63 69 64 20  iDelta = iDocid 
45bd7 2d 20 69 50 72 65 76 3b 0a 20 20 20 20 20 20 20  - iPrev;.       
45bd8 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 61     }.          a
45bd9 73 73 65 72 74 28 20 69 44 65 6c 74 61 3e 30 20  ssert( iDelta>0 
45bda 7c 7c 20 28 6e 44 6f 63 6c 69 73 74 3d 3d 30 20  || (nDoclist==0 
45bdb 26 26 20 69 44 65 6c 74 61 3d 3d 69 44 6f 63 69  && iDelta==iDoci
45bdc 64 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  d) );.          
45bdd 61 73 73 65 72 74 28 20 6e 44 6f 63 6c 69 73 74  assert( nDoclist
45bde 3e 30 20 7c 7c 20 69 44 65 6c 74 61 3d 3d 69 44  >0 || iDelta==iD
45bdf 6f 63 69 64 20 29 3b 0a 0a 20 20 20 20 20 20 20  ocid );..       
45be0 20 20 20 6e 42 79 74 65 20 3d 20 73 71 6c 69 74     nByte = sqlit
45be1 65 33 46 74 73 33 56 61 72 69 6e 74 4c 65 6e 28  e3Fts3VarintLen(
45be2 69 44 65 6c 74 61 29 20 2b 20 28 69 73 52 65 71  iDelta) + (isReq
45be3 75 69 72 65 50 6f 73 3f 6e 4c 69 73 74 2b 31 3a  uirePos?nList+1:
45be4 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  0);.          if
45be5 28 20 6e 44 6f 63 6c 69 73 74 2b 6e 42 79 74 65  ( nDoclist+nByte
45be6 3e 70 43 73 72 2d 3e 6e 42 75 66 66 65 72 20 29  >pCsr->nBuffer )
45be7 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  {.            ch
45be8 61 72 20 2a 61 4e 65 77 3b 0a 20 20 20 20 20 20  ar *aNew;.      
45be9 20 20 20 20 20 20 70 43 73 72 2d 3e 6e 42 75 66        pCsr->nBuf
45bea 66 65 72 20 3d 20 28 6e 44 6f 63 6c 69 73 74 2b  fer = (nDoclist+
45beb 6e 42 79 74 65 29 2a 32 3b 0a 20 20 20 20 20 20  nByte)*2;.      
45bec 20 20 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c        aNew = sql
45bed 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 43 73  ite3_realloc(pCs
45bee 72 2d 3e 61 42 75 66 66 65 72 2c 20 70 43 73 72  r->aBuffer, pCsr
45bef 2d 3e 6e 42 75 66 66 65 72 29 3b 0a 20 20 20 20  ->nBuffer);.    
45bf0 20 20 20 20 20 20 20 20 69 66 28 20 21 61 4e 65          if( !aNe
45bf1 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  w ){.           
45bf2 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
45bf3 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
45bf4 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
45bf5 20 20 70 43 73 72 2d 3e 61 42 75 66 66 65 72 20    pCsr->aBuffer 
45bf6 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 20 20  = aNew;.        
45bf7 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 69    }..          i
45bf8 66 28 20 69 73 46 69 72 73 74 20 29 7b 0a 20 20  f( isFirst ){.  
45bf9 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
45bfa 61 20 3d 20 26 70 43 73 72 2d 3e 61 42 75 66 66  a = &pCsr->aBuff
45bfb 65 72 5b 6e 44 6f 63 6c 69 73 74 5d 3b 0a 20 20  er[nDoclist];.  
45bfc 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 57            int nW
45bfd 72 69 74 65 3b 0a 20 20 20 20 20 20 20 20 20 20  rite;.          
45bfe 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 57   .            nW
45bff 72 69 74 65 20 3d 20 73 71 6c 69 74 65 33 46 74  rite = sqlite3Ft
45c00 73 33 46 69 72 73 74 46 69 6c 74 65 72 28 69 44  s3FirstFilter(iD
45c01 65 6c 74 61 2c 20 70 4c 69 73 74 2c 20 6e 4c 69  elta, pList, nLi
45c02 73 74 2c 20 61 29 3b 0a 20 20 20 20 20 20 20 20  st, a);.        
45c03 20 20 20 20 69 66 28 20 6e 57 72 69 74 65 20 29      if( nWrite )
45c04 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
45c05 69 50 72 65 76 20 3d 20 69 44 6f 63 69 64 3b 0a  iPrev = iDocid;.
45c06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 44                nD
45c07 6f 63 6c 69 73 74 20 2b 3d 20 6e 57 72 69 74 65  oclist += nWrite
45c08 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
45c09 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
45c0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 6f  .            nDo
45c0b 63 6c 69 73 74 20 2b 3d 20 73 71 6c 69 74 65 33  clist += sqlite3
45c0c 46 74 73 33 50 75 74 56 61 72 69 6e 74 28 26 70  Fts3PutVarint(&p
45c0d 43 73 72 2d 3e 61 42 75 66 66 65 72 5b 6e 44 6f  Csr->aBuffer[nDo
45c0e 63 6c 69 73 74 5d 2c 20 69 44 65 6c 74 61 29 3b  clist], iDelta);
45c0f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 50 72  .            iPr
45c10 65 76 20 3d 20 69 44 6f 63 69 64 3b 0a 20 20 20  ev = iDocid;.   
45c11 20 20 20 20 20 20 20 20 20 69 66 28 20 69 73 52           if( isR
45c12 65 71 75 69 72 65 50 6f 73 20 29 7b 0a 20 20 20  equirePos ){.   
45c13 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70             memcp
45c14 79 28 26 70 43 73 72 2d 3e 61 42 75 66 66 65 72  y(&pCsr->aBuffer
45c15 5b 6e 44 6f 63 6c 69 73 74 5d 2c 20 70 4c 69 73  [nDoclist], pLis
45c16 74 2c 20 6e 4c 69 73 74 29 3b 0a 20 20 20 20 20  t, nList);.     
45c17 20 20 20 20 20 20 20 20 20 6e 44 6f 63 6c 69 73           nDoclis
45c18 74 20 2b 3d 20 6e 4c 69 73 74 3b 0a 20 20 20 20  t += nList;.    
45c19 20 20 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e            pCsr->
45c1a 61 42 75 66 66 65 72 5b 6e 44 6f 63 6c 69 73 74  aBuffer[nDoclist
45c1b 2b 2b 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  ++] = '\0';.    
45c1c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
45c1d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
45c1e 0a 20 20 20 20 20 20 20 20 66 74 73 33 53 65 67  .        fts3Seg
45c1f 52 65 61 64 65 72 53 6f 72 74 28 61 70 53 65 67  ReaderSort(apSeg
45c20 6d 65 6e 74 2c 20 6e 4d 65 72 67 65 2c 20 6a 2c  ment, nMerge, j,
45c21 20 78 43 6d 70 29 3b 0a 20 20 20 20 20 20 7d 0a   xCmp);.      }.
45c22 20 20 20 20 20 20 69 66 28 20 6e 44 6f 63 6c 69        if( nDocli
45c23 73 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  st>0 ){.        
45c24 70 43 73 72 2d 3e 61 44 6f 63 6c 69 73 74 20 3d  pCsr->aDoclist =
45c25 20 70 43 73 72 2d 3e 61 42 75 66 66 65 72 3b 0a   pCsr->aBuffer;.
45c26 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e 6e 44          pCsr->nD
45c27 6f 63 6c 69 73 74 20 3d 20 6e 44 6f 63 6c 69 73  oclist = nDoclis
45c28 74 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  t;.        rc = 
45c29 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 20 20  SQLITE_ROW;.    
45c2a 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 43    }.    }.    pC
45c2b 73 72 2d 3e 6e 41 64 76 61 6e 63 65 20 3d 20 6e  sr->nAdvance = n
45c2c 4d 65 72 67 65 3b 0a 20 20 7d 77 68 69 6c 65 28  Merge;.  }while(
45c2d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
45c2e 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  ;..  return rc;.
45c2f 7d 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  }...SQLITE_PRIVA
45c30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
45c31 74 73 33 53 65 67 52 65 61 64 65 72 46 69 6e 69  ts3SegReaderFini
45c32 73 68 28 0a 20 20 46 74 73 33 4d 75 6c 74 69 53  sh(.  Fts3MultiS
45c33 65 67 52 65 61 64 65 72 20 2a 70 43 73 72 20 20  egReader *pCsr  
45c34 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f       /* Cursor o
45c35 62 6a 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 69 66  bject */.){.  if
45c36 28 20 70 43 73 72 20 29 7b 0a 20 20 20 20 69 6e  ( pCsr ){.    in
45c37 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
45c38 3b 20 69 3c 70 43 73 72 2d 3e 6e 53 65 67 6d 65  ; i<pCsr->nSegme
45c39 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  nt; i++){.      
45c3a 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65  sqlite3Fts3SegRe
45c3b 61 64 65 72 46 72 65 65 28 70 43 73 72 2d 3e 61  aderFree(pCsr->a
45c3c 70 53 65 67 6d 65 6e 74 5b 69 5d 29 3b 0a 20 20  pSegment[i]);.  
45c3d 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
45c3e 66 72 65 65 28 70 43 73 72 2d 3e 61 70 53 65 67  free(pCsr->apSeg
45c3f 6d 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  ment);.    sqlit
45c40 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 61 42  e3_free(pCsr->aB
45c41 75 66 66 65 72 29 3b 0a 0a 20 20 20 20 70 43 73  uffer);..    pCs
45c42 72 2d 3e 6e 53 65 67 6d 65 6e 74 20 3d 20 30 3b  r->nSegment = 0;
45c43 0a 20 20 20 20 70 43 73 72 2d 3e 61 70 53 65 67  .    pCsr->apSeg
45c44 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 70 43  ment = 0;.    pC
45c45 73 72 2d 3e 61 42 75 66 66 65 72 20 3d 20 30 3b  sr->aBuffer = 0;
45c46 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65  .  }.}../*.** Me
45c47 72 67 65 20 61 6c 6c 20 6c 65 76 65 6c 20 69 4c  rge all level iL
45c48 65 76 65 6c 20 73 65 67 6d 65 6e 74 73 20 69 6e  evel segments in
45c49 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e   the database in
45c4a 74 6f 20 61 20 73 69 6e 67 6c 65 20 0a 2a 2a 20  to a single .** 
45c4b 69 4c 65 76 65 6c 2b 31 20 73 65 67 6d 65 6e 74  iLevel+1 segment
45c4c 2e 20 4f 72 2c 20 69 66 20 69 4c 65 76 65 6c 3c  . Or, if iLevel<
45c4d 30 2c 20 6d 65 72 67 65 20 61 6c 6c 20 73 65 67  0, merge all seg
45c4e 6d 65 6e 74 73 20 69 6e 74 6f 20 61 0a 2a 2a 20  ments into a.** 
45c4f 73 69 6e 67 6c 65 20 73 65 67 6d 65 6e 74 20 77  single segment w
45c50 69 74 68 20 61 20 6c 65 76 65 6c 20 65 71 75 61  ith a level equa
45c51 6c 20 74 6f 20 74 68 65 20 6e 75 6d 65 72 69 63  l to the numeric
45c52 61 6c 6c 79 20 6c 61 72 67 65 73 74 20 6c 65 76  ally largest lev
45c53 65 6c 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79  el .** currently
45c54 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20   present in the 
45c55 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  database..**.** 
45c56 49 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  If this function
45c57 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20   is called with 
45c58 69 4c 65 76 65 6c 3c 30 2c 20 62 75 74 20 74 68  iLevel<0, but th
45c59 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 0a  ere is only one.
45c5a 2a 2a 20 73 65 67 6d 65 6e 74 20 69 6e 20 74 68  ** segment in th
45c5b 65 20 64 61 74 61 62 61 73 65 2c 20 53 51 4c 49  e database, SQLI
45c5c 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72  TE_DONE is retur
45c5d 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e  ned immediately.
45c5e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20   .** Otherwise, 
45c5f 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53  if successful, S
45c60 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
45c61 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f  rned. If an erro
45c62 72 20 6f 63 63 75 72 73 2c 20 0a 2a 2a 20 61 6e  r occurs, .** an
45c63 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f   SQLite error co
45c64 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
45c65 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
45c66 73 33 53 65 67 6d 65 6e 74 4d 65 72 67 65 28 0a  s3SegmentMerge(.
45c67 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20    Fts3Table *p, 
45c68 0a 20 20 69 6e 74 20 69 4c 61 6e 67 69 64 2c 20  .  int iLangid, 
45c69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45c6a 20 20 20 2f 2a 20 4c 61 6e 67 75 61 67 65 20 69     /* Language i
45c6b 64 20 74 6f 20 6d 65 72 67 65 20 2a 2f 0a 20 20  d to merge */.  
45c6c 69 6e 74 20 69 49 6e 64 65 78 2c 20 20 20 20 20  int iIndex,     
45c6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45c6e 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 2d 3e 61  /* Index in p->a
45c6f 49 6e 64 65 78 5b 5d 20 74 6f 20 6d 65 72 67 65  Index[] to merge
45c70 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 76 65 6c   */.  int iLevel
45c71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45c72 20 20 20 20 20 20 2f 2a 20 4c 65 76 65 6c 20 74        /* Level t
45c73 6f 20 6d 65 72 67 65 20 2a 2f 0a 29 7b 0a 20 20  o merge */.){.  
45c74 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
45c75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45c76 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
45c77 2f 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30  /.  int iIdx = 0
45c78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
45c79 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
45c7a 6e 65 77 20 73 65 67 6d 65 6e 74 20 2a 2f 0a 20  new segment */. 
45c7b 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
45c7c 4e 65 77 4c 65 76 65 6c 20 3d 20 30 3b 20 20 20  NewLevel = 0;   
45c7d 20 2f 2a 20 4c 65 76 65 6c 2f 69 6e 64 65 78 20   /* Level/index 
45c7e 74 6f 20 63 72 65 61 74 65 20 6e 65 77 20 73 65  to create new se
45c7f 67 6d 65 6e 74 20 61 74 20 2a 2f 0a 20 20 53 65  gment at */.  Se
45c80 67 6d 65 6e 74 57 72 69 74 65 72 20 2a 70 57 72  gmentWriter *pWr
45c81 69 74 65 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a  iter = 0;     /*
45c82 20 55 73 65 64 20 74 6f 20 77 72 69 74 65 20 74   Used to write t
45c83 68 65 20 6e 65 77 2c 20 6d 65 72 67 65 64 2c 20  he new, merged, 
45c84 73 65 67 6d 65 6e 74 20 2a 2f 0a 20 20 46 74 73  segment */.  Fts
45c85 33 53 65 67 46 69 6c 74 65 72 20 66 69 6c 74 65  3SegFilter filte
45c86 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
45c87 53 65 67 6d 65 6e 74 20 74 65 72 6d 20 66 69 6c  Segment term fil
45c88 74 65 72 20 63 6f 6e 64 69 74 69 6f 6e 20 2a 2f  ter condition */
45c89 0a 20 20 46 74 73 33 4d 75 6c 74 69 53 65 67 52  .  Fts3MultiSegR
45c8a 65 61 64 65 72 20 63 73 72 3b 20 20 20 20 20 20  eader csr;      
45c8b 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20     /* Cursor to 
45c8c 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20  iterate through 
45c8d 6c 65 76 65 6c 28 73 29 20 2a 2f 0a 20 20 69 6e  level(s) */.  in
45c8e 74 20 62 49 67 6e 6f 72 65 45 6d 70 74 79 20 3d  t bIgnoreEmpty =
45c8f 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   0;           /*
45c90 20 54 72 75 65 20 74 6f 20 69 67 6e 6f 72 65 20   True to ignore 
45c91 65 6d 70 74 79 20 73 65 67 6d 65 6e 74 73 20 2a  empty segments *
45c92 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 4c 65  /..  assert( iLe
45c93 76 65 6c 3d 3d 46 54 53 33 5f 53 45 47 43 55 52  vel==FTS3_SEGCUR
45c94 53 4f 52 5f 41 4c 4c 0a 20 20 20 20 20 20 20 7c  SOR_ALL.       |
45c95 7c 20 69 4c 65 76 65 6c 3d 3d 46 54 53 33 5f 53  | iLevel==FTS3_S
45c96 45 47 43 55 52 53 4f 52 5f 50 45 4e 44 49 4e 47  EGCURSOR_PENDING
45c97 0a 20 20 20 20 20 20 20 7c 7c 20 69 4c 65 76 65  .       || iLeve
45c98 6c 3e 3d 30 0a 20 20 29 3b 0a 20 20 61 73 73 65  l>=0.  );.  asse
45c99 72 74 28 20 69 4c 65 76 65 6c 3c 46 54 53 33 5f  rt( iLevel<FTS3_
45c9a 53 45 47 44 49 52 5f 4d 41 58 4c 45 56 45 4c 20  SEGDIR_MAXLEVEL 
45c9b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 49 6e  );.  assert( iIn
45c9c 64 65 78 3e 3d 30 20 26 26 20 69 49 6e 64 65 78  dex>=0 && iIndex
45c9d 3c 70 2d 3e 6e 49 6e 64 65 78 20 29 3b 0a 0a 20  <p->nIndex );.. 
45c9e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
45c9f 33 53 65 67 52 65 61 64 65 72 43 75 72 73 6f 72  3SegReaderCursor
45ca0 28 70 2c 20 69 4c 61 6e 67 69 64 2c 20 69 49 6e  (p, iLangid, iIn
45ca1 64 65 78 2c 20 69 4c 65 76 65 6c 2c 20 30 2c 20  dex, iLevel, 0, 
45ca2 30 2c 20 31 2c 20 30 2c 20 26 63 73 72 29 3b 0a  0, 1, 0, &csr);.
45ca3 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
45ca4 5f 4f 4b 20 7c 7c 20 63 73 72 2e 6e 53 65 67 6d  _OK || csr.nSegm
45ca5 65 6e 74 3d 3d 30 20 29 20 67 6f 74 6f 20 66 69  ent==0 ) goto fi
45ca6 6e 69 73 68 65 64 3b 0a 0a 20 20 69 66 28 20 69  nished;..  if( i
45ca7 4c 65 76 65 6c 3d 3d 46 54 53 33 5f 53 45 47 43  Level==FTS3_SEGC
45ca8 55 52 53 4f 52 5f 41 4c 4c 20 29 7b 0a 20 20 20  URSOR_ALL ){.   
45ca9 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73   /* This call is
45caa 20 74 6f 20 6d 65 72 67 65 20 61 6c 6c 20 73 65   to merge all se
45cab 67 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 64 61  gments in the da
45cac 74 61 62 61 73 65 20 74 6f 20 61 20 73 69 6e 67  tabase to a sing
45cad 6c 65 0a 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e  le.    ** segmen
45cae 74 2e 20 54 68 65 20 6c 65 76 65 6c 20 6f 66 20  t. The level of 
45caf 74 68 65 20 6e 65 77 20 73 65 67 6d 65 6e 74 20  the new segment 
45cb0 69 73 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  is equal to the 
45cb1 6e 75 6d 65 72 69 63 61 6c 6c 79 0a 20 20 20 20  numerically.    
45cb2 2a 2a 20 67 72 65 61 74 65 73 74 20 73 65 67 6d  ** greatest segm
45cb3 65 6e 74 20 6c 65 76 65 6c 20 63 75 72 72 65 6e  ent level curren
45cb4 74 6c 79 20 70 72 65 73 65 6e 74 20 69 6e 20 74  tly present in t
45cb5 68 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 20  he database for 
45cb6 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 6e 64 65  this.    ** inde
45cb7 78 2e 20 54 68 65 20 69 64 78 20 6f 66 20 74 68  x. The idx of th
45cb8 65 20 6e 65 77 20 73 65 67 6d 65 6e 74 20 69 73  e new segment is
45cb9 20 61 6c 77 61 79 73 20 30 2e 20 20 2a 2f 0a 20   always 0.  */. 
45cba 20 20 20 69 66 28 20 63 73 72 2e 6e 53 65 67 6d     if( csr.nSegm
45cbb 65 6e 74 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  ent==1 ){.      
45cbc 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45  rc = SQLITE_DONE
45cbd 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 69 6e  ;.      goto fin
45cbe 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20  ished;.    }.   
45cbf 20 72 63 20 3d 20 66 74 73 33 53 65 67 6d 65 6e   rc = fts3Segmen
45cc0 74 4d 61 78 4c 65 76 65 6c 28 70 2c 20 69 4c 61  tMaxLevel(p, iLa
45cc1 6e 67 69 64 2c 20 69 49 6e 64 65 78 2c 20 26 69  ngid, iIndex, &i
45cc2 4e 65 77 4c 65 76 65 6c 29 3b 0a 20 20 20 20 62  NewLevel);.    b
45cc3 49 67 6e 6f 72 65 45 6d 70 74 79 20 3d 20 31 3b  IgnoreEmpty = 1;
45cc4 0a 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 4c  ..  }else if( iL
45cc5 65 76 65 6c 3d 3d 46 54 53 33 5f 53 45 47 43 55  evel==FTS3_SEGCU
45cc6 52 53 4f 52 5f 50 45 4e 44 49 4e 47 20 29 7b 0a  RSOR_PENDING ){.
45cc7 20 20 20 20 69 4e 65 77 4c 65 76 65 6c 20 3d 20      iNewLevel = 
45cc8 67 65 74 41 62 73 6f 6c 75 74 65 4c 65 76 65 6c  getAbsoluteLevel
45cc9 28 70 2c 20 69 4c 61 6e 67 69 64 2c 20 69 49 6e  (p, iLangid, iIn
45cca 64 65 78 2c 20 30 29 3b 0a 20 20 20 20 72 63 20  dex, 0);.    rc 
45ccb 3d 20 66 74 73 33 41 6c 6c 6f 63 61 74 65 53 65  = fts3AllocateSe
45ccc 67 64 69 72 49 64 78 28 70 2c 20 69 4c 61 6e 67  gdirIdx(p, iLang
45ccd 69 64 2c 20 69 49 6e 64 65 78 2c 20 30 2c 20 26  id, iIndex, 0, &
45cce 69 49 64 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  iIdx);.  }else{.
45ccf 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c      /* This call
45cd0 20 69 73 20 74 6f 20 6d 65 72 67 65 20 61 6c 6c   is to merge all
45cd1 20 73 65 67 6d 65 6e 74 73 20 61 74 20 6c 65 76   segments at lev
45cd2 65 6c 20 69 4c 65 76 65 6c 2e 20 66 69 6e 64 20  el iLevel. find 
45cd3 74 68 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20  the next.    ** 
45cd4 61 76 61 69 6c 61 62 6c 65 20 73 65 67 6d 65 6e  available segmen
45cd5 74 20 69 6e 64 65 78 20 61 74 20 6c 65 76 65 6c  t index at level
45cd6 20 69 4c 65 76 65 6c 2b 31 2e 20 54 68 65 20 63   iLevel+1. The c
45cd7 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 66 74  all to.    ** ft
45cd8 73 33 41 6c 6c 6f 63 61 74 65 53 65 67 64 69 72  s3AllocateSegdir
45cd9 49 64 78 28 29 20 77 69 6c 6c 20 6d 65 72 67 65  Idx() will merge
45cda 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 61 74   the segments at
45cdb 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c 2b 31 20   level iLevel+1 
45cdc 74 6f 20 0a 20 20 20 20 2a 2a 20 61 20 73 69 6e  to .    ** a sin
45cdd 67 6c 65 20 69 4c 65 76 65 6c 2b 32 20 73 65 67  gle iLevel+2 seg
45cde 6d 65 6e 74 20 69 66 20 6e 65 63 65 73 73 61 72  ment if necessar
45cdf 79 2e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  y.  */.    rc = 
45ce0 66 74 73 33 41 6c 6c 6f 63 61 74 65 53 65 67 64  fts3AllocateSegd
45ce1 69 72 49 64 78 28 70 2c 20 69 4c 61 6e 67 69 64  irIdx(p, iLangid
45ce2 2c 20 69 49 6e 64 65 78 2c 20 69 4c 65 76 65 6c  , iIndex, iLevel
45ce3 2b 31 2c 20 26 69 49 64 78 29 3b 0a 20 20 20 20  +1, &iIdx);.    
45ce4 69 4e 65 77 4c 65 76 65 6c 20 3d 20 67 65 74 41  iNewLevel = getA
45ce5 62 73 6f 6c 75 74 65 4c 65 76 65 6c 28 70 2c 20  bsoluteLevel(p, 
45ce6 69 4c 61 6e 67 69 64 2c 20 69 49 6e 64 65 78 2c  iLangid, iIndex,
45ce7 20 69 4c 65 76 65 6c 2b 31 29 3b 0a 20 20 7d 0a   iLevel+1);.  }.
45ce8 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
45ce9 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 69 6e 69 73  _OK ) goto finis
45cea 68 65 64 3b 0a 20 20 61 73 73 65 72 74 28 20 63  hed;.  assert( c
45ceb 73 72 2e 6e 53 65 67 6d 65 6e 74 3e 30 20 29 3b  sr.nSegment>0 );
45cec 0a 20 20 61 73 73 65 72 74 28 20 69 4e 65 77 4c  .  assert( iNewL
45ced 65 76 65 6c 3e 3d 67 65 74 41 62 73 6f 6c 75 74  evel>=getAbsolut
45cee 65 4c 65 76 65 6c 28 70 2c 20 69 4c 61 6e 67 69  eLevel(p, iLangi
45cef 64 2c 20 69 49 6e 64 65 78 2c 20 30 29 20 29 3b  d, iIndex, 0) );
45cf0 0a 20 20 61 73 73 65 72 74 28 20 69 4e 65 77 4c  .  assert( iNewL
45cf1 65 76 65 6c 3c 67 65 74 41 62 73 6f 6c 75 74 65  evel<getAbsolute
45cf2 4c 65 76 65 6c 28 70 2c 20 69 4c 61 6e 67 69 64  Level(p, iLangid
45cf3 2c 20 69 49 6e 64 65 78 2c 46 54 53 33 5f 53 45  , iIndex,FTS3_SE
45cf4 47 44 49 52 5f 4d 41 58 4c 45 56 45 4c 29 20 29  GDIR_MAXLEVEL) )
45cf5 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 26 66 69 6c  ;..  memset(&fil
45cf6 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46  ter, 0, sizeof(F
45cf7 74 73 33 53 65 67 46 69 6c 74 65 72 29 29 3b 0a  ts3SegFilter));.
45cf8 20 20 66 69 6c 74 65 72 2e 66 6c 61 67 73 20 3d    filter.flags =
45cf9 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 52 45   FTS3_SEGMENT_RE
45cfa 51 55 49 52 45 5f 50 4f 53 3b 0a 20 20 66 69 6c  QUIRE_POS;.  fil
45cfb 74 65 72 2e 66 6c 61 67 73 20 7c 3d 20 28 62 49  ter.flags |= (bI
45cfc 67 6e 6f 72 65 45 6d 70 74 79 20 3f 20 46 54 53  gnoreEmpty ? FTS
45cfd 33 5f 53 45 47 4d 45 4e 54 5f 49 47 4e 4f 52 45  3_SEGMENT_IGNORE
45cfe 5f 45 4d 50 54 59 20 3a 20 30 29 3b 0a 0a 20 20  _EMPTY : 0);..  
45cff 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  rc = sqlite3Fts3
45d00 53 65 67 52 65 61 64 65 72 53 74 61 72 74 28 70  SegReaderStart(p
45d01 2c 20 26 63 73 72 2c 20 26 66 69 6c 74 65 72 29  , &csr, &filter)
45d02 3b 0a 20 20 77 68 69 6c 65 28 20 53 51 4c 49 54  ;.  while( SQLIT
45d03 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20  E_OK==rc ){.    
45d04 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  rc = sqlite3Fts3
45d05 53 65 67 52 65 61 64 65 72 53 74 65 70 28 70 2c  SegReaderStep(p,
45d06 20 26 63 73 72 29 3b 0a 20 20 20 20 69 66 28 20   &csr);.    if( 
45d07 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc!=SQLITE_ROW )
45d08 20 62 72 65 61 6b 3b 0a 20 20 20 20 72 63 20 3d   break;.    rc =
45d09 20 66 74 73 33 53 65 67 57 72 69 74 65 72 41 64   fts3SegWriterAd
45d0a 64 28 70 2c 20 26 70 57 72 69 74 65 72 2c 20 31  d(p, &pWriter, 1
45d0b 2c 20 0a 20 20 20 20 20 20 20 20 63 73 72 2e 7a  , .        csr.z
45d0c 54 65 72 6d 2c 20 63 73 72 2e 6e 54 65 72 6d 2c  Term, csr.nTerm,
45d0d 20 63 73 72 2e 61 44 6f 63 6c 69 73 74 2c 20 63   csr.aDoclist, c
45d0e 73 72 2e 6e 44 6f 63 6c 69 73 74 29 3b 0a 20 20  sr.nDoclist);.  
45d0f 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  }.  if( rc!=SQLI
45d10 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 69 6e  TE_OK ) goto fin
45d11 69 73 68 65 64 3b 0a 20 20 61 73 73 65 72 74 28  ished;.  assert(
45d12 20 70 57 72 69 74 65 72 20 29 3b 0a 0a 20 20 69   pWriter );..  i
45d13 66 28 20 69 4c 65 76 65 6c 21 3d 46 54 53 33 5f  f( iLevel!=FTS3_
45d14 53 45 47 43 55 52 53 4f 52 5f 50 45 4e 44 49 4e  SEGCURSOR_PENDIN
45d15 47 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 74  G ){.    rc = ft
45d16 73 33 44 65 6c 65 74 65 53 65 67 64 69 72 28 0a  s3DeleteSegdir(.
45d17 20 20 20 20 20 20 20 20 70 2c 20 69 4c 61 6e 67          p, iLang
45d18 69 64 2c 20 69 49 6e 64 65 78 2c 20 69 4c 65 76  id, iIndex, iLev
45d19 65 6c 2c 20 63 73 72 2e 61 70 53 65 67 6d 65 6e  el, csr.apSegmen
45d1a 74 2c 20 63 73 72 2e 6e 53 65 67 6d 65 6e 74 0a  t, csr.nSegment.
45d1b 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 72      );.    if( r
45d1c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
45d1d 6f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20  oto finished;.  
45d1e 7d 0a 20 20 72 63 20 3d 20 66 74 73 33 53 65 67  }.  rc = fts3Seg
45d1f 57 72 69 74 65 72 46 6c 75 73 68 28 70 2c 20 70  WriterFlush(p, p
45d20 57 72 69 74 65 72 2c 20 69 4e 65 77 4c 65 76 65  Writer, iNewLeve
45d21 6c 2c 20 69 49 64 78 29 3b 0a 0a 20 66 69 6e 69  l, iIdx);.. fini
45d22 73 68 65 64 3a 0a 20 20 66 74 73 33 53 65 67 57  shed:.  fts3SegW
45d23 72 69 74 65 72 46 72 65 65 28 70 57 72 69 74 65  riterFree(pWrite
45d24 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 46 74 73  r);.  sqlite3Fts
45d25 33 53 65 67 52 65 61 64 65 72 46 69 6e 69 73 68  3SegReaderFinish
45d26 28 26 63 73 72 29 3b 0a 20 20 72 65 74 75 72 6e  (&csr);.  return
45d27 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 20 0a 2a 2a 20   rc;.}.../* .** 
45d28 46 6c 75 73 68 20 74 68 65 20 63 6f 6e 74 65 6e  Flush the conten
45d29 74 73 20 6f 66 20 70 65 6e 64 69 6e 67 54 65 72  ts of pendingTer
45d2a 6d 73 20 74 6f 20 6c 65 76 65 6c 20 30 20 73 65  ms to level 0 se
45d2b 67 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54  gments..*/.SQLIT
45d2c 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
45d2d 6c 69 74 65 33 46 74 73 33 50 65 6e 64 69 6e 67  lite3Fts3Pending
45d2e 54 65 72 6d 73 46 6c 75 73 68 28 46 74 73 33 54  TermsFlush(Fts3T
45d2f 61 62 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  able *p){.  int 
45d30 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
45d31 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20    int i;.       
45d32 20 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d   .  for(i=0; rc=
45d33 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c  =SQLITE_OK && i<
45d34 70 2d 3e 6e 49 6e 64 65 78 3b 20 69 2b 2b 29 7b  p->nIndex; i++){
45d35 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 65  .    rc = fts3Se
45d36 67 6d 65 6e 74 4d 65 72 67 65 28 70 2c 20 70 2d  gmentMerge(p, p-
45d37 3e 69 50 72 65 76 4c 61 6e 67 69 64 2c 20 69 2c  >iPrevLangid, i,
45d38 20 46 54 53 33 5f 53 45 47 43 55 52 53 4f 52 5f   FTS3_SEGCURSOR_
45d39 50 45 4e 44 49 4e 47 29 3b 0a 20 20 20 20 69 66  PENDING);.    if
45d3a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
45d3b 45 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  E ) rc = SQLITE_
45d3c 4f 4b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  OK;.  }.  sqlite
45d3d 33 46 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d  3Fts3PendingTerm
45d3e 73 43 6c 65 61 72 28 70 29 3b 0a 0a 20 20 2f 2a  sClear(p);..  /*
45d3f 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 61   Determine the a
45d40 75 74 6f 2d 69 6e 63 72 2d 6d 65 72 67 65 20 73  uto-incr-merge s
45d41 65 74 74 69 6e 67 20 69 66 20 75 6e 6b 6e 6f 77  etting if unknow
45d42 6e 2e 20 20 49 66 20 65 6e 61 62 6c 65 64 2c 0a  n.  If enabled,.
45d43 20 20 2a 2a 20 65 73 74 69 6d 61 74 65 20 74 68    ** estimate th
45d44 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66  e number of leaf
45d45 20 62 6c 6f 63 6b 73 20 6f 66 20 63 6f 6e 74 65   blocks of conte
45d46 6e 74 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e  nt to be written
45d47 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  .  */.  if( rc==
45d48 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e  SQLITE_OK && p->
45d49 62 48 61 73 53 74 61 74 0a 20 20 20 26 26 20 70  bHasStat.   && p
45d4a 2d 3e 62 41 75 74 6f 69 6e 63 72 6d 65 72 67 65  ->bAutoincrmerge
45d4b 3d 3d 30 78 66 66 20 26 26 20 70 2d 3e 6e 4c 65  ==0xff && p->nLe
45d4c 61 66 41 64 64 3e 30 0a 20 20 29 7b 0a 20 20 20  afAdd>0.  ){.   
45d4d 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
45d4e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 72 63  Stmt = 0;.    rc
45d4f 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70   = fts3SqlStmt(p
45d50 2c 20 53 51 4c 5f 53 45 4c 45 43 54 5f 53 54 41  , SQL_SELECT_STA
45d51 54 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  T, &pStmt, 0);. 
45d52 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
45d53 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71  E_OK ){.      sq
45d54 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 70  lite3_bind_int(p
45d55 53 74 6d 74 2c 20 31 2c 20 46 54 53 5f 53 54 41  Stmt, 1, FTS_STA
45d56 54 5f 41 55 54 4f 49 4e 43 52 4d 45 52 47 45 29  T_AUTOINCRMERGE)
45d57 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
45d58 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29  ite3_step(pStmt)
45d59 3b 0a 20 20 20 20 20 20 70 2d 3e 62 41 75 74 6f  ;.      p->bAuto
45d5a 69 6e 63 72 6d 65 72 67 65 20 3d 20 28 72 63 3d  incrmerge = (rc=
45d5b 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 73  =SQLITE_ROW && s
45d5c 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
45d5d 74 28 70 53 74 6d 74 2c 20 30 29 29 3b 0a 20 20  t(pStmt, 0));.  
45d5e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
45d5f 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20  _reset(pStmt);. 
45d60 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
45d61 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  n rc;.}../*.** E
45d62 6e 63 6f 64 65 20 4e 20 69 6e 74 65 67 65 72 73  ncode N integers
45d63 20 61 73 20 76 61 72 69 6e 74 73 20 69 6e 74 6f   as varints into
45d64 20 61 20 62 6c 6f 62 2e 0a 2a 2f 0a 73 74 61 74   a blob..*/.stat
45d65 69 63 20 76 6f 69 64 20 66 74 73 33 45 6e 63 6f  ic void fts3Enco
45d66 64 65 49 6e 74 41 72 72 61 79 28 0a 20 20 69 6e  deIntArray(.  in
45d67 74 20 4e 2c 20 20 20 20 20 20 20 20 20 20 20 20  t N,            
45d68 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f   /* The number o
45d69 66 20 69 6e 74 65 67 65 72 73 20 74 6f 20 65 6e  f integers to en
45d6a 63 6f 64 65 20 2a 2f 0a 20 20 75 33 32 20 2a 61  code */.  u32 *a
45d6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
45d6c 54 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75  The integer valu
45d6d 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42  es */.  char *zB
45d6e 75 66 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 72  uf,        /* Wr
45d6f 69 74 65 20 74 68 65 20 42 4c 4f 42 20 68 65 72  ite the BLOB her
45d70 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 42 75  e */.  int *pNBu
45d71 66 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69  f         /* Wri
45d72 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  te number of byt
45d73 65 73 20 69 66 20 7a 42 75 66 5b 5d 20 75 73 65  es if zBuf[] use
45d74 64 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  d here */.){.  i
45d75 6e 74 20 69 2c 20 6a 3b 0a 20 20 66 6f 72 28 69  nt i, j;.  for(i
45d76 3d 6a 3d 30 3b 20 69 3c 4e 3b 20 69 2b 2b 29 7b  =j=0; i<N; i++){
45d77 0a 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74 65  .    j += sqlite
45d78 33 46 74 73 33 50 75 74 56 61 72 69 6e 74 28 26  3Fts3PutVarint(&
45d79 7a 42 75 66 5b 6a 5d 2c 20 28 73 71 6c 69 74 65  zBuf[j], (sqlite
45d7a 33 5f 69 6e 74 36 34 29 61 5b 69 5d 29 3b 0a 20  3_int64)a[i]);. 
45d7b 20 7d 0a 20 20 2a 70 4e 42 75 66 20 3d 20 6a 3b   }.  *pNBuf = j;
45d7c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64 65  .}../*.** Decode
45d7d 20 61 20 62 6c 6f 62 20 6f 66 20 76 61 72 69 6e   a blob of varin
45d7e 74 73 20 69 6e 74 6f 20 4e 20 69 6e 74 65 67 65  ts into N intege
45d7f 72 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  rs.*/.static voi
45d80 64 20 66 74 73 33 44 65 63 6f 64 65 49 6e 74 41  d fts3DecodeIntA
45d81 72 72 61 79 28 0a 20 20 69 6e 74 20 4e 2c 20 20  rray(.  int N,  
45d82 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
45d83 65 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 74 65  e number of inte
45d84 67 65 72 73 20 74 6f 20 64 65 63 6f 64 65 20 2a  gers to decode *
45d85 2f 0a 20 20 75 33 32 20 2a 61 2c 20 20 20 20 20  /.  u32 *a,     
45d86 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
45d87 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75  the integer valu
45d88 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  es */.  const ch
45d89 61 72 20 2a 7a 42 75 66 2c 20 20 2f 2a 20 54 68  ar *zBuf,  /* Th
45d8a 65 20 42 4c 4f 42 20 63 6f 6e 74 61 69 6e 69 6e  e BLOB containin
45d8b 67 20 74 68 65 20 76 61 72 69 6e 74 73 20 2a 2f  g the varints */
45d8c 0a 20 20 69 6e 74 20 6e 42 75 66 20 20 20 20 20  .  int nBuf     
45d8d 20 20 20 20 20 20 2f 2a 20 73 69 7a 65 20 6f 66        /* size of
45d8e 20 74 68 65 20 42 4c 4f 42 20 2a 2f 0a 29 7b 0a   the BLOB */.){.
45d8f 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 55 4e    int i, j;.  UN
45d90 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e  USED_PARAMETER(n
45d91 42 75 66 29 3b 0a 20 20 66 6f 72 28 69 3d 6a 3d  Buf);.  for(i=j=
45d92 30 3b 20 69 3c 4e 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<N; i++){.  
45d93 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
45d94 78 3b 0a 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69  x;.    j += sqli
45d95 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74  te3Fts3GetVarint
45d96 28 26 7a 42 75 66 5b 6a 5d 2c 20 26 78 29 3b 0a  (&zBuf[j], &x);.
45d97 20 20 20 20 61 73 73 65 72 74 28 6a 3c 3d 6e 42      assert(j<=nB
45d98 75 66 29 3b 0a 20 20 20 20 61 5b 69 5d 20 3d 20  uf);.    a[i] = 
45d99 28 75 33 32 29 28 78 20 26 20 30 78 66 66 66 66  (u32)(x & 0xffff
45d9a 66 66 66 66 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ffff);.  }.}../*
45d9b 0a 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 20 73  .** Insert the s
45d9c 69 7a 65 73 20 28 69 6e 20 74 6f 6b 65 6e 73 29  izes (in tokens)
45d9d 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e   for each column
45d9e 20 6f 66 20 74 68 65 20 64 6f 63 75 6d 65 6e 74   of the document
45d9f 0a 2a 2a 20 77 69 74 68 20 64 6f 63 69 64 20 65  .** with docid e
45da0 71 75 61 6c 20 74 6f 20 70 2d 3e 69 50 72 65 76  qual to p->iPrev
45da1 44 6f 63 69 64 2e 20 20 54 68 65 20 73 69 7a 65  Docid.  The size
45da2 73 20 61 72 65 20 65 6e 63 6f 64 65 64 20 61 73  s are encoded as
45da3 0a 2a 2a 20 61 20 62 6c 6f 62 20 6f 66 20 76 61  .** a blob of va
45da4 72 69 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  rints..*/.static
45da5 20 76 6f 69 64 20 66 74 73 33 49 6e 73 65 72 74   void fts3Insert
45da6 44 6f 63 73 69 7a 65 28 0a 20 20 69 6e 74 20 2a  Docsize(.  int *
45da7 70 52 43 2c 20 20 20 20 20 20 20 20 20 20 20 20  pRC,            
45da8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
45da9 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 20 20 46  sult code */.  F
45daa 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20  ts3Table *p,    
45dab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
45dac 2a 20 54 61 62 6c 65 20 69 6e 74 6f 20 77 68 69  * Table into whi
45dad 63 68 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a  ch to insert */.
45dae 20 20 75 33 32 20 2a 61 53 7a 20 20 20 20 20 20    u32 *aSz      
45daf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45db0 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66 20 65 61    /* Sizes of ea
45db1 63 68 20 63 6f 6c 75 6d 6e 2c 20 69 6e 20 74 6f  ch column, in to
45db2 6b 65 6e 73 20 2a 2f 0a 29 7b 0a 20 20 63 68 61  kens */.){.  cha
45db3 72 20 2a 70 42 6c 6f 62 3b 20 20 20 20 20 20 20  r *pBlob;       
45db4 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 4c 4f        /* The BLO
45db5 42 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68  B encoding of th
45db6 65 20 64 6f 63 75 6d 65 6e 74 20 73 69 7a 65 20  e document size 
45db7 2a 2f 0a 20 20 69 6e 74 20 6e 42 6c 6f 62 3b 20  */.  int nBlob; 
45db8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
45db9 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
45dba 20 69 6e 20 74 68 65 20 42 4c 4f 42 20 2a 2f 0a   in the BLOB */.
45dbb 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
45dbc 70 53 74 6d 74 3b 20 20 20 20 20 2f 2a 20 53 74  pStmt;     /* St
45dbd 61 74 65 6d 65 6e 74 20 75 73 65 64 20 74 6f 20  atement used to 
45dbe 69 6e 73 65 72 74 20 74 68 65 20 65 6e 63 6f 64  insert the encod
45dbf 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ing */.  int rc;
45dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45dc1 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65    /* Result code
45dc2 20 66 72 6f 6d 20 73 75 62 66 75 6e 63 74 69 6f   from subfunctio
45dc3 6e 73 20 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52  ns */..  if( *pR
45dc4 43 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 42  C ) return;.  pB
45dc5 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  lob = sqlite3_ma
45dc6 6c 6c 6f 63 28 20 31 30 2a 70 2d 3e 6e 43 6f 6c  lloc( 10*p->nCol
45dc7 75 6d 6e 20 29 3b 0a 20 20 69 66 28 20 70 42 6c  umn );.  if( pBl
45dc8 6f 62 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52  ob==0 ){.    *pR
45dc9 43 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  C = SQLITE_NOMEM
45dca 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
45dcb 7d 0a 20 20 66 74 73 33 45 6e 63 6f 64 65 49 6e  }.  fts3EncodeIn
45dcc 74 41 72 72 61 79 28 70 2d 3e 6e 43 6f 6c 75 6d  tArray(p->nColum
45dcd 6e 2c 20 61 53 7a 2c 20 70 42 6c 6f 62 2c 20 26  n, aSz, pBlob, &
45dce 6e 42 6c 6f 62 29 3b 0a 20 20 72 63 20 3d 20 66  nBlob);.  rc = f
45dcf 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51  ts3SqlStmt(p, SQ
45dd0 4c 5f 52 45 50 4c 41 43 45 5f 44 4f 43 53 49 5a  L_REPLACE_DOCSIZ
45dd1 45 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  E, &pStmt, 0);. 
45dd2 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 73   if( rc ){.    s
45dd3 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 6c 6f  qlite3_free(pBlo
45dd4 62 29 3b 0a 20 20 20 20 2a 70 52 43 20 3d 20 72  b);.    *pRC = r
45dd5 63 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  c;.    return;. 
45dd6 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e   }.  sqlite3_bin
45dd7 64 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c 20 31  d_int64(pStmt, 1
45dd8 2c 20 70 2d 3e 69 50 72 65 76 44 6f 63 69 64 29  , p->iPrevDocid)
45dd9 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ;.  sqlite3_bind
45dda 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 32 2c 20  _blob(pStmt, 2, 
45ddb 70 42 6c 6f 62 2c 20 6e 42 6c 6f 62 2c 20 73 71  pBlob, nBlob, sq
45ddc 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 73  lite3_free);.  s
45ddd 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d  qlite3_step(pStm
45dde 74 29 3b 0a 20 20 2a 70 52 43 20 3d 20 73 71 6c  t);.  *pRC = sql
45ddf 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74  ite3_reset(pStmt
45de0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f  );.}../*.** Reco
45de1 72 64 20 30 20 6f 66 20 74 68 65 20 25 5f 73 74  rd 0 of the %_st
45de2 61 74 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  at table contain
45de3 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 69 73 74  s a blob consist
45de4 69 6e 67 20 6f 66 20 4e 20 76 61 72 69 6e 74 73  ing of N varints
45de5 2c 0a 2a 2a 20 77 68 65 72 65 20 4e 20 69 73 20  ,.** where N is 
45de6 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 73  the number of us
45de7 65 72 20 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d  er defined colum
45de8 6e 73 20 69 6e 20 74 68 65 20 66 74 73 33 20 74  ns in the fts3 t
45de9 61 62 6c 65 20 70 6c 75 73 0a 2a 2a 20 74 77 6f  able plus.** two
45dea 2e 20 49 66 20 6e 43 6f 6c 20 69 73 20 74 68 65  . If nCol is the
45deb 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 20   number of user 
45dec 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e 73 2c  defined columns,
45ded 20 74 68 65 6e 20 76 61 6c 75 65 73 20 6f 66 20   then values of 
45dee 74 68 65 20 0a 2a 2a 20 76 61 72 69 6e 74 73 20  the .** varints 
45def 61 72 65 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f  are set as follo
45df0 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 56 61 72 69  ws:.**.**   Vari
45df1 6e 74 20 30 3a 20 20 20 20 20 20 20 54 6f 74 61  nt 0:       Tota
45df2 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  l number of rows
45df3 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a   in the table..*
45df4 2a 0a 2a 2a 20 20 20 56 61 72 69 6e 74 20 31 2e  *.**   Varint 1.
45df5 2e 6e 43 6f 6c 3a 20 46 6f 72 20 65 61 63 68 20  .nCol: For each 
45df6 63 6f 6c 75 6d 6e 2c 20 74 68 65 20 74 6f 74 61  column, the tota
45df7 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  l number of toke
45df8 6e 73 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20  ns stored in.** 
45df9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45dfa 20 20 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72    the column for
45dfb 20 61 6c 6c 20 72 6f 77 73 20 6f 66 20 74 68 65   all rows of the
45dfc 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20   table..**.**   
45dfd 56 61 72 69 6e 74 20 31 2b 6e 43 6f 6c 3a 20 20  Varint 1+nCol:  
45dfe 54 68 65 20 74 6f 74 61 6c 20 73 69 7a 65 2c 20  The total size, 
45dff 69 6e 20 62 79 74 65 73 2c 20 6f 66 20 61 6c 6c  in bytes, of all
45e00 20 74 65 78 74 20 76 61 6c 75 65 73 20 69 6e 20   text values in 
45e01 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  all.**          
45e02 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 73           columns
45e03 20 6f 66 20 61 6c 6c 20 72 6f 77 73 20 6f 66 20   of all rows of 
45e04 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2f  the table..**.*/
45e05 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73  .static void fts
45e06 33 55 70 64 61 74 65 44 6f 63 54 6f 74 61 6c 73  3UpdateDocTotals
45e07 28 0a 20 20 69 6e 74 20 2a 70 52 43 2c 20 20 20  (.  int *pRC,   
45e08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45e09 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c      /* The resul
45e0a 74 20 63 6f 64 65 20 2a 2f 0a 20 20 46 74 73 33  t code */.  Fts3
45e0b 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20  Table *p,       
45e0c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
45e0d 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61 74  able being updat
45e0e 65 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 53 7a  ed */.  u32 *aSz
45e0f 49 6e 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  Ins,            
45e10 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
45e11 69 6e 63 72 65 61 73 65 73 20 2a 2f 0a 20 20 75  increases */.  u
45e12 33 32 20 2a 61 53 7a 44 65 6c 2c 20 20 20 20 20  32 *aSzDel,     
45e13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
45e14 2a 20 53 69 7a 65 20 64 65 63 72 65 61 73 65 73  * Size decreases
45e15 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 6e 67 20   */.  int nChng 
45e16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45e17 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 20        /* Change 
45e18 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  in the number of
45e19 20 64 6f 63 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b   documents */.){
45e1a 0a 20 20 63 68 61 72 20 2a 70 42 6c 6f 62 3b 20  .  char *pBlob; 
45e1b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
45e1c 74 6f 72 61 67 65 20 66 6f 72 20 42 4c 4f 42 20  torage for BLOB 
45e1d 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 25 5f 73  written into %_s
45e1e 74 61 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 6c  tat */.  int nBl
45e1f 6f 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ob;             
45e20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 42 4c 4f    /* Size of BLO
45e21 42 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 25  B written into %
45e22 5f 73 74 61 74 20 2a 2f 0a 20 20 75 33 32 20 2a  _stat */.  u32 *
45e23 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a;              
45e24 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
45e25 69 6e 74 65 67 65 72 73 20 74 68 61 74 20 62 65  integers that be
45e26 63 6f 6d 65 73 20 74 68 65 20 42 4c 4f 42 20 2a  comes the BLOB *
45e27 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
45e28 20 2a 70 53 74 6d 74 3b 20 20 20 20 20 2f 2a 20   *pStmt;     /* 
45e29 53 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 72 65  Statement for re
45e2a 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e  ading and writin
45e2b 67 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  g */.  int i;   
45e2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45e2d 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
45e2e 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
45e2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
45e30 20 52 65 73 75 6c 74 20 63 6f 64 65 20 66 72 6f   Result code fro
45e31 6d 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a  m subfunctions *
45e32 2f 0a 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e  /..  const int n
45e33 53 74 61 74 20 3d 20 70 2d 3e 6e 43 6f 6c 75 6d  Stat = p->nColum
45e34 6e 2b 32 3b 0a 0a 20 20 69 66 28 20 2a 70 52 43  n+2;..  if( *pRC
45e35 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 20 3d   ) return;.  a =
45e36 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
45e37 20 28 73 69 7a 65 6f 66 28 75 33 32 29 2b 31 30   (sizeof(u32)+10
45e38 29 2a 6e 53 74 61 74 20 29 3b 0a 20 20 69 66 28  )*nStat );.  if(
45e39 20 61 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52   a==0 ){.    *pR
45e3a 43 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  C = SQLITE_NOMEM
45e3b 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
45e3c 7d 0a 20 20 70 42 6c 6f 62 20 3d 20 28 63 68 61  }.  pBlob = (cha
45e3d 72 2a 29 26 61 5b 6e 53 74 61 74 5d 3b 0a 20 20  r*)&a[nStat];.  
45e3e 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
45e3f 28 70 2c 20 53 51 4c 5f 53 45 4c 45 43 54 5f 53  (p, SQL_SELECT_S
45e40 54 41 54 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b  TAT, &pStmt, 0);
45e41 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
45e42 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 29   sqlite3_free(a)
45e43 3b 0a 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b  ;.    *pRC = rc;
45e44 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
45e45 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
45e46 69 6e 74 28 70 53 74 6d 74 2c 20 31 2c 20 46 54  int(pStmt, 1, FT
45e47 53 5f 53 54 41 54 5f 44 4f 43 54 4f 54 41 4c 29  S_STAT_DOCTOTAL)
45e48 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ;.  if( sqlite3_
45e49 73 74 65 70 28 70 53 74 6d 74 29 3d 3d 53 51 4c  step(pStmt)==SQL
45e4a 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 66  ITE_ROW ){.    f
45e4b 74 73 33 44 65 63 6f 64 65 49 6e 74 41 72 72 61  ts3DecodeIntArra
45e4c 79 28 6e 53 74 61 74 2c 20 61 2c 0a 20 20 20 20  y(nStat, a,.    
45e4d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c       sqlite3_col
45e4e 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20  umn_blob(pStmt, 
45e4f 30 29 2c 0a 20 20 20 20 20 20 20 20 20 73 71 6c  0),.         sql
45e50 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
45e51 73 28 70 53 74 6d 74 2c 20 30 29 29 3b 0a 20 20  s(pStmt, 0));.  
45e52 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 73 65  }else{.    memse
45e53 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75  t(a, 0, sizeof(u
45e54 33 32 29 2a 28 6e 53 74 61 74 29 20 29 3b 0a 20  32)*(nStat) );. 
45e55 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   }.  rc = sqlite
45e56 33 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a  3_reset(pStmt);.
45e57 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
45e58 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
45e59 65 33 5f 66 72 65 65 28 61 29 3b 0a 20 20 20 20  e3_free(a);.    
45e5a 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72  *pRC = rc;.    r
45e5b 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28  eturn;.  }.  if(
45e5c 20 6e 43 68 6e 67 3c 30 20 26 26 20 61 5b 30 5d   nChng<0 && a[0]
45e5d 3c 28 75 33 32 29 28 2d 6e 43 68 6e 67 29 20 29  <(u32)(-nChng) )
45e5e 7b 0a 20 20 20 20 61 5b 30 5d 20 3d 20 30 3b 0a  {.    a[0] = 0;.
45e5f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 5b 30    }else{.    a[0
45e60 5d 20 2b 3d 20 6e 43 68 6e 67 3b 0a 20 20 7d 0a  ] += nChng;.  }.
45e61 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
45e62 6e 43 6f 6c 75 6d 6e 2b 31 3b 20 69 2b 2b 29 7b  nColumn+1; i++){
45e63 0a 20 20 20 20 75 33 32 20 78 20 3d 20 61 5b 69  .    u32 x = a[i
45e64 2b 31 5d 3b 0a 20 20 20 20 69 66 28 20 78 2b 61  +1];.    if( x+a
45e65 53 7a 49 6e 73 5b 69 5d 20 3c 20 61 53 7a 44 65  SzIns[i] < aSzDe
45e66 6c 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 78 20  l[i] ){.      x 
45e67 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 0;.    }else{.
45e68 20 20 20 20 20 20 78 20 3d 20 78 20 2b 20 61 53        x = x + aS
45e69 7a 49 6e 73 5b 69 5d 20 2d 20 61 53 7a 44 65 6c  zIns[i] - aSzDel
45e6a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  [i];.    }.    a
45e6b 5b 69 2b 31 5d 20 3d 20 78 3b 0a 20 20 7d 0a 20  [i+1] = x;.  }. 
45e6c 20 66 74 73 33 45 6e 63 6f 64 65 49 6e 74 41 72   fts3EncodeIntAr
45e6d 72 61 79 28 6e 53 74 61 74 2c 20 61 2c 20 70 42  ray(nStat, a, pB
45e6e 6c 6f 62 2c 20 26 6e 42 6c 6f 62 29 3b 0a 20 20  lob, &nBlob);.  
45e6f 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
45e70 28 70 2c 20 53 51 4c 5f 52 45 50 4c 41 43 45 5f  (p, SQL_REPLACE_
45e71 53 54 41 54 2c 20 26 70 53 74 6d 74 2c 20 30 29  STAT, &pStmt, 0)
45e72 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
45e73 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61    sqlite3_free(a
45e74 29 3b 0a 20 20 20 20 2a 70 52 43 20 3d 20 72 63  );.    *pRC = rc
45e75 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
45e76 7d 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  }.  sqlite3_bind
45e77 5f 69 6e 74 28 70 53 74 6d 74 2c 20 31 2c 20 46  _int(pStmt, 1, F
45e78 54 53 5f 53 54 41 54 5f 44 4f 43 54 4f 54 41 4c  TS_STAT_DOCTOTAL
45e79 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  );.  sqlite3_bin
45e7a 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 32 2c  d_blob(pStmt, 2,
45e7b 20 70 42 6c 6f 62 2c 20 6e 42 6c 6f 62 2c 20 53   pBlob, nBlob, S
45e7c 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
45e7d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53   sqlite3_step(pS
45e7e 74 6d 74 29 3b 0a 20 20 2a 70 52 43 20 3d 20 73  tmt);.  *pRC = s
45e7f 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74  qlite3_reset(pSt
45e80 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  mt);.  sqlite3_f
45e81 72 65 65 28 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ree(a);.}../*.**
45e82 20 4d 65 72 67 65 20 74 68 65 20 65 6e 74 69 72   Merge the entir
45e83 65 20 64 61 74 61 62 61 73 65 20 73 6f 20 74 68  e database so th
45e84 61 74 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20  at there is one 
45e85 73 65 67 6d 65 6e 74 20 66 6f 72 20 65 61 63 68  segment for each
45e86 20 0a 2a 2a 20 69 49 6e 64 65 78 2f 69 4c 61 6e   .** iIndex/iLan
45e87 67 69 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 2e  gid combination.
45e88 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
45e89 74 73 33 44 6f 4f 70 74 69 6d 69 7a 65 28 46 74  ts3DoOptimize(Ft
45e8a 73 33 54 61 62 6c 65 20 2a 70 2c 20 69 6e 74 20  s3Table *p, int 
45e8b 62 52 65 74 75 72 6e 44 6f 6e 65 29 7b 0a 20 20  bReturnDone){.  
45e8c 69 6e 74 20 62 53 65 65 6e 44 6f 6e 65 20 3d 20  int bSeenDone = 
45e8d 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  0;.  int rc;.  s
45e8e 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 41 6c  qlite3_stmt *pAl
45e8f 6c 4c 61 6e 67 69 64 20 3d 20 30 3b 0a 0a 20 20  lLangid = 0;..  
45e90 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
45e91 28 70 2c 20 53 51 4c 5f 53 45 4c 45 43 54 5f 41  (p, SQL_SELECT_A
45e92 4c 4c 5f 4c 41 4e 47 49 44 2c 20 26 70 41 6c 6c  LL_LANGID, &pAll
45e93 4c 61 6e 67 69 64 2c 20 30 29 3b 0a 20 20 69 66  Langid, 0);.  if
45e94 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
45e95 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a  ){.    int rc2;.
45e96 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
45e97 5f 69 6e 74 28 70 41 6c 6c 4c 61 6e 67 69 64 2c  _int(pAllLangid,
45e98 20 31 2c 20 70 2d 3e 6e 49 6e 64 65 78 29 3b 0a   1, p->nIndex);.
45e99 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74      while( sqlit
45e9a 65 33 5f 73 74 65 70 28 70 41 6c 6c 4c 61 6e 67  e3_step(pAllLang
45e9b 69 64 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20  id)==SQLITE_ROW 
45e9c 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  ){.      int i;.
45e9d 20 20 20 20 20 20 69 6e 74 20 69 4c 61 6e 67 69        int iLangi
45e9e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
45e9f 6d 6e 5f 69 6e 74 28 70 41 6c 6c 4c 61 6e 67 69  mn_int(pAllLangi
45ea0 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 66 6f 72  d, 0);.      for
45ea1 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  (i=0; rc==SQLITE
45ea2 5f 4f 4b 20 26 26 20 69 3c 70 2d 3e 6e 49 6e 64  _OK && i<p->nInd
45ea3 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ex; i++){.      
45ea4 20 20 72 63 20 3d 20 66 74 73 33 53 65 67 6d 65    rc = fts3Segme
45ea5 6e 74 4d 65 72 67 65 28 70 2c 20 69 4c 61 6e 67  ntMerge(p, iLang
45ea6 69 64 2c 20 69 2c 20 46 54 53 33 5f 53 45 47 43  id, i, FTS3_SEGC
45ea7 55 52 53 4f 52 5f 41 4c 4c 29 3b 0a 20 20 20 20  URSOR_ALL);.    
45ea8 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
45ea9 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20  TE_DONE ){.     
45eaa 20 20 20 20 20 62 53 65 65 6e 44 6f 6e 65 20 3d       bSeenDone =
45eab 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63   1;.          rc
45eac 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
45ead 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
45eae 20 20 20 20 7d 0a 20 20 20 20 72 63 32 20 3d 20      }.    rc2 = 
45eaf 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 41  sqlite3_reset(pA
45eb0 6c 6c 4c 61 6e 67 69 64 29 3b 0a 20 20 20 20 69  llLangid);.    i
45eb1 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
45eb2 20 29 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 7d   ) rc = rc2;.  }
45eb3 0a 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 53  ..  sqlite3Fts3S
45eb4 65 67 6d 65 6e 74 73 43 6c 6f 73 65 28 70 29 3b  egmentsClose(p);
45eb5 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 50 65  .  sqlite3Fts3Pe
45eb6 6e 64 69 6e 67 54 65 72 6d 73 43 6c 65 61 72 28  ndingTermsClear(
45eb7 70 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 28 72  p);..  return (r
45eb8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
45eb9 62 52 65 74 75 72 6e 44 6f 6e 65 20 26 26 20 62  bReturnDone && b
45eba 53 65 65 6e 44 6f 6e 65 29 20 3f 20 53 51 4c 49  SeenDone) ? SQLI
45ebb 54 45 5f 44 4f 4e 45 20 3a 20 72 63 3b 0a 7d 0a  TE_DONE : rc;.}.
45ebc 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
45ebd 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77  tion is called w
45ebe 68 65 6e 20 74 68 65 20 75 73 65 72 20 65 78 65  hen the user exe
45ebf 63 75 74 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77  cutes the follow
45ec0 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a  ing statement:.*
45ec1 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52 54 20  *.**     INSERT 
45ec2 49 4e 54 4f 20 3c 74 62 6c 3e 28 3c 74 62 6c 3e  INTO <tbl>(<tbl>
45ec3 29 20 56 41 4c 55 45 53 28 27 72 65 62 75 69 6c  ) VALUES('rebuil
45ec4 64 27 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65  d');.**.** The e
45ec5 6e 74 69 72 65 20 46 54 53 20 69 6e 64 65 78 20  ntire FTS index 
45ec6 69 73 20 64 69 73 63 61 72 64 65 64 20 61 6e 64  is discarded and
45ec7 20 72 65 62 75 69 6c 74 2e 20 49 66 20 74 68 65   rebuilt. If the
45ec8 20 74 61 62 6c 65 20 69 73 20 6f 6e 65 20 0a 2a   table is one .*
45ec9 2a 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  * created using 
45eca 74 68 65 20 63 6f 6e 74 65 6e 74 3d 78 78 78 20  the content=xxx 
45ecb 6f 70 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65  option, then the
45ecc 20 6e 65 77 20 69 6e 64 65 78 20 69 73 20 62 61   new index is ba
45ecd 73 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 75  sed on.** the cu
45ece 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 6f  rrent contents o
45ecf 66 20 74 68 65 20 78 78 78 20 74 61 62 6c 65 2e  f the xxx table.
45ed0 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 69   Otherwise, it i
45ed1 73 20 72 65 62 75 69 6c 74 20 62 61 73 65 64 0a  s rebuilt based.
45ed2 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e  ** on the conten
45ed3 74 73 20 6f 66 20 74 68 65 20 25 5f 63 6f 6e 74  ts of the %_cont
45ed4 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ent table..*/.st
45ed5 61 74 69 63 20 69 6e 74 20 66 74 73 33 44 6f 52  atic int fts3DoR
45ed6 65 62 75 69 6c 64 28 46 74 73 33 54 61 62 6c 65  ebuild(Fts3Table
45ed7 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20   *p){.  int rc; 
45ed8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45ed9 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
45eda 6e 20 43 6f 64 65 20 2a 2f 0a 0a 20 20 72 63 20  n Code */..  rc 
45edb 3d 20 66 74 73 33 44 65 6c 65 74 65 41 6c 6c 28  = fts3DeleteAll(
45edc 70 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d  p, 0);.  if( rc=
45edd 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
45ede 20 20 75 33 32 20 2a 61 53 7a 20 3d 20 30 3b 0a    u32 *aSz = 0;.
45edf 20 20 20 20 75 33 32 20 2a 61 53 7a 49 6e 73 20      u32 *aSzIns 
45ee0 3d 20 30 3b 0a 20 20 20 20 75 33 32 20 2a 61 53  = 0;.    u32 *aS
45ee1 7a 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 73 71  zDel = 0;.    sq
45ee2 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
45ee3 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 6e  t = 0;.    int n
45ee4 45 6e 74 72 79 20 3d 20 30 3b 0a 0a 20 20 20 20  Entry = 0;..    
45ee5 2f 2a 20 43 6f 6d 70 6f 73 65 20 61 6e 64 20 70  /* Compose and p
45ee6 72 65 70 61 72 65 20 61 6e 20 53 51 4c 20 73 74  repare an SQL st
45ee7 61 74 65 6d 65 6e 74 20 74 6f 20 6c 6f 6f 70 20  atement to loop 
45ee8 74 68 72 6f 75 67 68 20 74 68 65 20 63 6f 6e 74  through the cont
45ee9 65 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  ent table */.   
45eea 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71   char *zSql = sq
45eeb 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 53  lite3_mprintf("S
45eec 45 4c 45 43 54 20 25 73 22 20 2c 20 70 2d 3e 7a  ELECT %s" , p->z
45eed 52 65 61 64 45 78 70 72 6c 69 73 74 29 3b 0a 20  ReadExprlist);. 
45eee 20 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a     if( !zSql ){.
45eef 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
45ef0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c  E_NOMEM;.    }el
45ef1 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  se{.      rc = s
45ef2 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
45ef3 32 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 2d  2(p->db, zSql, -
45ef4 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  1, &pStmt, 0);. 
45ef5 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
45ef6 65 28 7a 53 71 6c 29 3b 0a 20 20 20 20 7d 0a 0a  e(zSql);.    }..
45ef7 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
45ef8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
45ef9 6e 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f  nt nByte = sizeo
45efa 66 28 75 33 32 29 20 2a 20 28 70 2d 3e 6e 43 6f  f(u32) * (p->nCo
45efb 6c 75 6d 6e 2b 31 29 2a 33 3b 0a 20 20 20 20 20  lumn+1)*3;.     
45efc 20 61 53 7a 20 3d 20 28 75 33 32 20 2a 29 73 71   aSz = (u32 *)sq
45efd 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79  lite3_malloc(nBy
45efe 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61  te);.      if( a
45eff 53 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  Sz==0 ){.       
45f00 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
45f01 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  EM;.      }else{
45f02 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
45f03 61 53 7a 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a  aSz, 0, nByte);.
45f04 20 20 20 20 20 20 20 20 61 53 7a 49 6e 73 20 3d          aSzIns =
45f05 20 26 61 53 7a 5b 70 2d 3e 6e 43 6f 6c 75 6d 6e   &aSz[p->nColumn
45f06 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 61 53 7a  +1];.        aSz
45f07 44 65 6c 20 3d 20 26 61 53 7a 49 6e 73 5b 70 2d  Del = &aSzIns[p-
45f08 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 3b 0a 20 20 20  >nColumn+1];.   
45f09 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
45f0a 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
45f0b 45 5f 4f 4b 20 26 26 20 53 51 4c 49 54 45 5f 52  E_OK && SQLITE_R
45f0c 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70  OW==sqlite3_step
45f0d 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 20  (pStmt) ){.     
45f0e 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20 20   int iCol;.     
45f0f 20 69 6e 74 20 69 4c 61 6e 67 69 64 20 3d 20 6c   int iLangid = l
45f10 61 6e 67 69 64 46 72 6f 6d 53 65 6c 65 63 74 28  angidFromSelect(
45f11 70 2c 20 70 53 74 6d 74 29 3b 0a 20 20 20 20 20  p, pStmt);.     
45f12 20 72 63 20 3d 20 66 74 73 33 50 65 6e 64 69 6e   rc = fts3Pendin
45f13 67 54 65 72 6d 73 44 6f 63 69 64 28 70 2c 20 69  gTermsDocid(p, i
45f14 4c 61 6e 67 69 64 2c 20 73 71 6c 69 74 65 33 5f  Langid, sqlite3_
45f15 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 53 74  column_int64(pSt
45f16 6d 74 2c 20 30 29 29 3b 0a 20 20 20 20 20 20 6d  mt, 0));.      m
45f17 65 6d 73 65 74 28 61 53 7a 2c 20 30 2c 20 73 69  emset(aSz, 0, si
45f18 7a 65 6f 66 28 61 53 7a 5b 30 5d 29 20 2a 20 28  zeof(aSz[0]) * (
45f19 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 29 3b 0a  p->nColumn+1));.
45f1a 20 20 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30        for(iCol=0
45f1b 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
45f1c 26 26 20 69 43 6f 6c 3c 70 2d 3e 6e 43 6f 6c 75  && iCol<p->nColu
45f1d 6d 6e 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20  mn; iCol++){.   
45f1e 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
45f1f 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  *z = (const char
45f20 20 2a 29 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   *) sqlite3_colu
45f21 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 69  mn_text(pStmt, i
45f22 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 20 20 20 20  Col+1);.        
45f23 72 63 20 3d 20 66 74 73 33 50 65 6e 64 69 6e 67  rc = fts3Pending
45f24 54 65 72 6d 73 41 64 64 28 70 2c 20 69 4c 61 6e  TermsAdd(p, iLan
45f25 67 69 64 2c 20 7a 2c 20 69 43 6f 6c 2c 20 26 61  gid, z, iCol, &a
45f26 53 7a 5b 69 43 6f 6c 5d 29 3b 0a 20 20 20 20 20  Sz[iCol]);.     
45f27 20 20 20 61 53 7a 5b 70 2d 3e 6e 43 6f 6c 75 6d     aSz[p->nColum
45f28 6e 5d 20 2b 3d 20 73 71 6c 69 74 65 33 5f 63 6f  n] += sqlite3_co
45f29 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53 74 6d 74  lumn_bytes(pStmt
45f2a 2c 20 69 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 20  , iCol+1);.     
45f2b 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e   }.      if( p->
45f2c 62 48 61 73 44 6f 63 73 69 7a 65 20 29 7b 0a 20  bHasDocsize ){. 
45f2d 20 20 20 20 20 20 20 66 74 73 33 49 6e 73 65 72         fts3Inser
45f2e 74 44 6f 63 73 69 7a 65 28 26 72 63 2c 20 70 2c  tDocsize(&rc, p,
45f2f 20 61 53 7a 29 3b 0a 20 20 20 20 20 20 7d 0a 20   aSz);.      }. 
45f30 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
45f31 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
45f32 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
45f33 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20  ze(pStmt);.     
45f34 20 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20     pStmt = 0;.  
45f35 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
45f36 20 20 20 6e 45 6e 74 72 79 2b 2b 3b 0a 20 20 20     nEntry++;.   
45f37 20 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b       for(iCol=0;
45f38 20 69 43 6f 6c 3c 3d 70 2d 3e 6e 43 6f 6c 75 6d   iCol<=p->nColum
45f39 6e 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  n; iCol++){.    
45f3a 20 20 20 20 20 20 61 53 7a 49 6e 73 5b 69 43 6f        aSzIns[iCo
45f3b 6c 5d 20 2b 3d 20 61 53 7a 5b 69 43 6f 6c 5d 3b  l] += aSz[iCol];
45f3c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
45f3d 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
45f3e 20 70 2d 3e 62 46 74 73 34 20 29 7b 0a 20 20 20   p->bFts4 ){.   
45f3f 20 20 20 66 74 73 33 55 70 64 61 74 65 44 6f 63     fts3UpdateDoc
45f40 54 6f 74 61 6c 73 28 26 72 63 2c 20 70 2c 20 61  Totals(&rc, p, a
45f41 53 7a 49 6e 73 2c 20 61 53 7a 44 65 6c 2c 20 6e  SzIns, aSzDel, n
45f42 45 6e 74 72 79 29 3b 0a 20 20 20 20 7d 0a 20 20  Entry);.    }.  
45f43 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61    sqlite3_free(a
45f44 53 7a 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 53  Sz);..    if( pS
45f45 74 6d 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  tmt ){.      int
45f46 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 5f 66   rc2 = sqlite3_f
45f47 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a  inalize(pStmt);.
45f48 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
45f49 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
45f4a 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20     rc = rc2;.   
45f4b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
45f4c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
45f4d 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
45f4e 74 69 6f 6e 20 6f 70 65 6e 73 20 61 20 63 75 72  tion opens a cur
45f4f 73 6f 72 20 75 73 65 64 20 74 6f 20 72 65 61 64  sor used to read
45f50 20 74 68 65 20 69 6e 70 75 74 20 64 61 74 61 20   the input data 
45f51 66 6f 72 20 61 6e 20 0a 2a 2a 20 69 6e 63 72 65  for an .** incre
45f52 6d 65 6e 74 61 6c 20 6d 65 72 67 65 20 6f 70 65  mental merge ope
45f53 72 61 74 69 6f 6e 2e 20 53 70 65 63 69 66 69 63  ration. Specific
45f54 61 6c 6c 79 2c 20 69 74 20 6f 70 65 6e 73 20 61  ally, it opens a
45f55 20 63 75 72 73 6f 72 20 74 6f 20 73 63 61 6e 0a   cursor to scan.
45f56 2a 2a 20 74 68 65 20 6f 6c 64 65 73 74 20 6e 53  ** the oldest nS
45f57 65 67 20 73 65 67 6d 65 6e 74 73 20 28 69 64 78  eg segments (idx
45f58 3d 30 20 74 68 72 6f 75 67 68 20 69 64 78 3d 28  =0 through idx=(
45f59 6e 53 65 67 2d 31 29 29 20 69 6e 20 61 62 73 6f  nSeg-1)) in abso
45f5a 6c 75 74 65 20 0a 2a 2a 20 6c 65 76 65 6c 20 69  lute .** level i
45f5b 41 62 73 4c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61  AbsLevel..*/.sta
45f5c 74 69 63 20 69 6e 74 20 66 74 73 33 49 6e 63 72  tic int fts3Incr
45f5d 6d 65 72 67 65 43 73 72 28 0a 20 20 46 74 73 33  mergeCsr(.  Fts3
45f5e 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20  Table *p,       
45f5f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
45f60 54 53 33 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  TS3 table handle
45f61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
45f62 74 36 34 20 69 41 62 73 4c 65 76 65 6c 2c 20 20  t64 iAbsLevel,  
45f63 20 20 20 20 20 20 2f 2a 20 41 62 73 6f 6c 75 74        /* Absolut
45f64 65 20 6c 65 76 65 6c 20 74 6f 20 6f 70 65 6e 20  e level to open 
45f65 2a 2f 0a 20 20 69 6e 74 20 6e 53 65 67 2c 20 20  */.  int nSeg,  
45f66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45f67 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
45f68 66 20 73 65 67 6d 65 6e 74 73 20 74 6f 20 6d 65  f segments to me
45f69 72 67 65 20 2a 2f 0a 20 20 46 74 73 33 4d 75 6c  rge */.  Fts3Mul
45f6a 74 69 53 65 67 52 65 61 64 65 72 20 2a 70 43 73  tiSegReader *pCs
45f6b 72 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73  r        /* Curs
45f6c 6f 72 20 6f 62 6a 65 63 74 20 74 6f 20 70 6f 70  or object to pop
45f6d 75 6c 61 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ulate */.){.  in
45f6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
45f6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
45f70 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a   Return Code */.
45f71 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
45f72 70 53 74 6d 74 20 3d 20 30 3b 20 20 20 20 20 20  pStmt = 0;      
45f73 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 75    /* Statement u
45f74 73 65 64 20 74 6f 20 72 65 61 64 20 25 5f 73 65  sed to read %_se
45f75 67 64 69 72 20 65 6e 74 72 79 20 2a 2f 20 20 0a  gdir entry */  .
45f76 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20    int nByte;    
45f77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45f78 20 20 2f 2a 20 42 79 74 65 73 20 61 6c 6c 6f 63    /* Bytes alloc
45f79 61 74 65 64 20 61 74 20 70 43 73 72 2d 3e 61 70  ated at pCsr->ap
45f7a 53 65 67 6d 65 6e 74 5b 5d 20 2a 2f 0a 0a 20 20  Segment[] */..  
45f7b 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63  /* Allocate spac
45f7c 65 20 66 6f 72 20 74 68 65 20 46 74 73 33 4d 75  e for the Fts3Mu
45f7d 6c 74 69 53 65 67 52 65 61 64 65 72 2e 61 43 73  ltiSegReader.aCs
45f7e 72 5b 5d 20 61 72 72 61 79 20 2a 2f 0a 20 20 6d  r[] array */.  m
45f7f 65 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20 73  emset(pCsr, 0, s
45f80 69 7a 65 6f 66 28 2a 70 43 73 72 29 29 3b 0a 20  izeof(*pCsr));. 
45f81 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28   nByte = sizeof(
45f82 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 29  Fts3SegReader *)
45f83 20 2a 20 6e 53 65 67 3b 0a 20 20 70 43 73 72 2d   * nSeg;.  pCsr-
45f84 3e 61 70 53 65 67 6d 65 6e 74 20 3d 20 28 46 74  >apSegment = (Ft
45f85 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a 29 73  s3SegReader **)s
45f86 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42  qlite3_malloc(nB
45f87 79 74 65 29 3b 0a 0a 20 20 69 66 28 20 70 43 73  yte);..  if( pCs
45f88 72 2d 3e 61 70 53 65 67 6d 65 6e 74 3d 3d 30 20  r->apSegment==0 
45f89 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
45f8a 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73  TE_NOMEM;.  }els
45f8b 65 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 43  e{.    memset(pC
45f8c 73 72 2d 3e 61 70 53 65 67 6d 65 6e 74 2c 20 30  sr->apSegment, 0
45f8d 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 72 63  , nByte);.    rc
45f8e 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70   = fts3SqlStmt(p
45f8f 2c 20 53 51 4c 5f 53 45 4c 45 43 54 5f 4c 45 56  , SQL_SELECT_LEV
45f90 45 4c 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a  EL, &pStmt, 0);.
45f91 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
45f92 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
45f93 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 72 63  nt i;.    int rc
45f94 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62  2;.    sqlite3_b
45f95 69 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c  ind_int64(pStmt,
45f96 20 31 2c 20 69 41 62 73 4c 65 76 65 6c 29 3b 0a   1, iAbsLevel);.
45f97 20 20 20 20 61 73 73 65 72 74 28 20 70 43 73 72      assert( pCsr
45f98 2d 3e 6e 53 65 67 6d 65 6e 74 3d 3d 30 20 29 3b  ->nSegment==0 );
45f99 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63  .    for(i=0; rc
45f9a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73  ==SQLITE_OK && s
45f9b 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d  qlite3_step(pStm
45f9c 74 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26  t)==SQLITE_ROW &
45f9d 26 20 69 3c 6e 53 65 67 3b 20 69 2b 2b 29 7b 0a  & i<nSeg; i++){.
45f9e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
45f9f 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72 4e  e3Fts3SegReaderN
45fa0 65 77 28 69 2c 20 30 2c 0a 20 20 20 20 20 20 20  ew(i, 0,.       
45fa1 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d     sqlite3_colum
45fa2 6e 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c 20 31  n_int64(pStmt, 1
45fa3 29 2c 20 20 20 20 20 20 20 20 2f 2a 20 73 65 67  ),        /* seg
45fa4 64 69 72 2e 73 74 61 72 74 5f 62 6c 6f 63 6b 20  dir.start_block 
45fa5 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  */.          sql
45fa6 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
45fa7 34 28 70 53 74 6d 74 2c 20 32 29 2c 20 20 20 20  4(pStmt, 2),    
45fa8 20 20 20 20 2f 2a 20 73 65 67 64 69 72 2e 6c 65      /* segdir.le
45fa9 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 2a  aves_end_block *
45faa 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  /.          sqli
45fab 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
45fac 28 70 53 74 6d 74 2c 20 33 29 2c 20 20 20 20 20  (pStmt, 3),     
45fad 20 20 20 2f 2a 20 73 65 67 64 69 72 2e 65 6e 64     /* segdir.end
45fae 5f 62 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20  _block */.      
45faf 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
45fb0 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 34  mn_blob(pStmt, 4
45fb1 29 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 73 65  ),         /* se
45fb2 67 64 69 72 2e 72 6f 6f 74 20 2a 2f 0a 20 20 20  gdir.root */.   
45fb3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63         sqlite3_c
45fb4 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53 74 6d  olumn_bytes(pStm
45fb5 74 2c 20 34 29 2c 20 20 20 20 20 20 20 20 2f 2a  t, 4),        /*
45fb6 20 73 65 67 64 69 72 2e 72 6f 6f 74 20 2a 2f 0a   segdir.root */.
45fb7 20 20 20 20 20 20 20 20 20 20 26 70 43 73 72 2d            &pCsr-
45fb8 3e 61 70 53 65 67 6d 65 6e 74 5b 69 5d 0a 20 20  >apSegment[i].  
45fb9 20 20 20 20 29 3b 0a 20 20 20 20 20 20 70 43 73      );.      pCs
45fba 72 2d 3e 6e 53 65 67 6d 65 6e 74 2b 2b 3b 0a 20  r->nSegment++;. 
45fbb 20 20 20 7d 0a 20 20 20 20 72 63 32 20 3d 20 73     }.    rc2 = s
45fbc 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74  qlite3_reset(pSt
45fbd 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  mt);.    if( rc=
45fbe 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20  =SQLITE_OK ) rc 
45fbf 3d 20 72 63 32 3b 0a 20 20 7d 0a 0a 20 20 72 65  = rc2;.  }..  re
45fc0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 74 79 70 65  turn rc;.}..type
45fc1 64 65 66 20 73 74 72 75 63 74 20 49 6e 63 72 6d  def struct Incrm
45fc2 65 72 67 65 57 72 69 74 65 72 20 49 6e 63 72 6d  ergeWriter Incrm
45fc3 65 72 67 65 57 72 69 74 65 72 3b 0a 74 79 70 65  ergeWriter;.type
45fc4 64 65 66 20 73 74 72 75 63 74 20 4e 6f 64 65 57  def struct NodeW
45fc5 72 69 74 65 72 20 4e 6f 64 65 57 72 69 74 65 72  riter NodeWriter
45fc6 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
45fc7 20 42 6c 6f 62 20 42 6c 6f 62 3b 0a 74 79 70 65   Blob Blob;.type
45fc8 64 65 66 20 73 74 72 75 63 74 20 4e 6f 64 65 52  def struct NodeR
45fc9 65 61 64 65 72 20 4e 6f 64 65 52 65 61 64 65 72  eader NodeReader
45fca 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  ;../*.** An inst
45fcb 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
45fcc 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
45fcd 69 73 20 75 73 65 64 20 61 73 20 61 20 64 79 6e  is used as a dyn
45fce 61 6d 69 63 20 62 75 66 66 65 72 0a 2a 2a 20 74  amic buffer.** t
45fcf 6f 20 62 75 69 6c 64 20 75 70 20 6e 6f 64 65 73  o build up nodes
45fd0 20 6f 72 20 6f 74 68 65 72 20 62 6c 6f 62 73 20   or other blobs 
45fd1 6f 66 20 64 61 74 61 20 69 6e 2e 0a 2a 2a 0a 2a  of data in..**.*
45fd2 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 62  * The function b
45fd3 6c 6f 62 47 72 6f 77 42 75 66 66 65 72 28 29 20  lobGrowBuffer() 
45fd4 69 73 20 75 73 65 64 20 74 6f 20 65 78 74 65 6e  is used to exten
45fd5 64 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  d the allocation
45fd6 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 6c 6f 62  ..*/.struct Blob
45fd7 20 7b 0a 20 20 63 68 61 72 20 2a 61 3b 20 20 20   {.  char *a;   
45fd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45fd9 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
45fda 74 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f  to allocation */
45fdb 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20  .  int n;       
45fdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45fdd 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
45fde 76 61 6c 69 64 20 62 79 74 65 73 20 6f 66 20 64  valid bytes of d
45fdf 61 74 61 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20 20  ata in a[] */.  
45fe0 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20  int nAlloc;     
45fe1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45fe2 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a  /* Allocated siz
45fe3 65 20 6f 66 20 61 5b 5d 20 28 6e 41 6c 6c 6f 63  e of a[] (nAlloc
45fe4 3e 3d 6e 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  >=n) */.};../*.*
45fe5 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65  * This structure
45fe6 20 69 73 20 75 73 65 64 20 74 6f 20 62 75 69 6c   is used to buil
45fe7 64 20 75 70 20 62 75 66 66 65 72 73 20 63 6f 6e  d up buffers con
45fe8 74 61 69 6e 69 6e 67 20 73 65 67 6d 65 6e 74 20  taining segment 
45fe9 62 2d 74 72 65 65 20 0a 2a 2a 20 6e 6f 64 65 73  b-tree .** nodes
45fea 20 28 62 6c 6f 63 6b 73 29 2e 0a 2a 2f 0a 73 74   (blocks)..*/.st
45feb 72 75 63 74 20 4e 6f 64 65 57 72 69 74 65 72 20  ruct NodeWriter 
45fec 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  {.  sqlite3_int6
45fed 34 20 69 42 6c 6f 63 6b 3b 20 20 20 20 20 20 20  4 iBlock;       
45fee 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 62      /* Current b
45fef 6c 6f 63 6b 20 69 64 20 2a 2f 0a 20 20 42 6c 6f  lock id */.  Blo
45ff0 62 20 6b 65 79 3b 20 20 20 20 20 20 20 20 20 20  b key;          
45ff1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
45ff2 4c 61 73 74 20 6b 65 79 20 77 72 69 74 74 65 6e  Last key written
45ff3 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20   to the current 
45ff4 62 6c 6f 63 6b 20 2a 2f 0a 20 20 42 6c 6f 62 20  block */.  Blob 
45ff5 62 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20  block;          
45ff6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
45ff7 72 72 65 6e 74 20 62 6c 6f 63 6b 20 69 6d 61 67  rrent block imag
45ff8 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  e */.};../*.** A
45ff9 6e 20 6f 62 6a 65 63 74 20 6f 66 20 74 68 69 73  n object of this
45ffa 20 74 79 70 65 20 63 6f 6e 74 61 69 6e 73 20 74   type contains t
45ffb 68 65 20 73 74 61 74 65 20 72 65 71 75 69 72 65  he state require
45ffc 64 20 74 6f 20 63 72 65 61 74 65 20 6f 72 20 61  d to create or a
45ffd 70 70 65 6e 64 0a 2a 2a 20 74 6f 20 61 6e 20 61  ppend.** to an a
45ffe 70 70 65 6e 64 61 62 6c 65 20 62 2d 74 72 65 65  ppendable b-tree
45fff 20 73 65 67 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72   segment..*/.str
46000 75 63 74 20 49 6e 63 72 6d 65 72 67 65 57 72 69  uct IncrmergeWri
46001 74 65 72 20 7b 0a 20 20 69 6e 74 20 6e 4c 65 61  ter {.  int nLea
46002 66 45 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  fEst;           
46003 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
46004 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 6c   allocated for l
46005 65 61 66 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20  eaf blocks */.  
46006 69 6e 74 20 6e 57 6f 72 6b 3b 20 20 20 20 20 20  int nWork;      
46007 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46008 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61  /* Number of lea
46009 66 20 70 61 67 65 73 20 66 6c 75 73 68 65 64 20  f pages flushed 
4600a 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
4600b 36 34 20 69 41 62 73 4c 65 76 65 6c 3b 20 20 20  64 iAbsLevel;   
4600c 20 20 20 20 20 2f 2a 20 41 62 73 6f 6c 75 74 65       /* Absolute
4600d 20 6c 65 76 65 6c 20 6f 66 20 69 6e 70 75 74 20   level of input 
4600e 73 65 67 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e  segments */.  in
4600f 74 20 69 49 64 78 3b 20 20 20 20 20 20 20 20 20  t iIdx;         
46010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
46011 20 49 6e 64 65 78 20 6f 66 20 2a 6f 75 74 70 75   Index of *outpu
46012 74 2a 20 73 65 67 6d 65 6e 74 20 69 6e 20 69 41  t* segment in iA
46013 62 73 4c 65 76 65 6c 2b 31 20 2a 2f 0a 20 20 73  bsLevel+1 */.  s
46014 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 53 74  qlite3_int64 iSt
46015 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  art;           /
46016 2a 20 42 6c 6f 63 6b 20 6e 75 6d 62 65 72 20 6f  * Block number o
46017 66 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65  f first allocate
46018 64 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 73 71 6c  d block */.  sql
46019 69 74 65 33 5f 69 6e 74 36 34 20 69 45 6e 64 3b  ite3_int64 iEnd;
4601a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4601b 42 6c 6f 63 6b 20 6e 75 6d 62 65 72 20 6f 66 20  Block number of 
4601c 6c 61 73 74 20 61 6c 6c 6f 63 61 74 65 64 20 62  last allocated b
4601d 6c 6f 63 6b 20 2a 2f 0a 20 20 4e 6f 64 65 57 72  lock */.  NodeWr
4601e 69 74 65 72 20 61 4e 6f 64 65 57 72 69 74 65 72  iter aNodeWriter
4601f 5b 46 54 53 5f 4d 41 58 5f 41 50 50 45 4e 44 41  [FTS_MAX_APPENDA
46020 42 4c 45 5f 48 45 49 47 48 54 5d 3b 0a 7d 3b 0a  BLE_HEIGHT];.};.
46021 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74  ./*.** An object
46022 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
46023 67 20 74 79 70 65 20 69 73 20 75 73 65 64 20 74  g type is used t
46024 6f 20 72 65 61 64 20 64 61 74 61 20 66 72 6f 6d  o read data from
46025 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 46 54 53   a single.** FTS
46026 20 73 65 67 6d 65 6e 74 20 6e 6f 64 65 2e 20 53   segment node. S
46027 65 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ee the following
46028 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a   functions:.**.*
46029 2a 20 20 20 20 20 6e 6f 64 65 52 65 61 64 65 72  *     nodeReader
4602a 49 6e 69 74 28 29 0a 2a 2a 20 20 20 20 20 6e 6f  Init().**     no
4602b 64 65 52 65 61 64 65 72 4e 65 78 74 28 29 0a 2a  deReaderNext().*
4602c 2a 20 20 20 20 20 6e 6f 64 65 52 65 61 64 65 72  *     nodeReader
4602d 52 65 6c 65 61 73 65 28 29 0a 2a 2f 0a 73 74 72  Release().*/.str
4602e 75 63 74 20 4e 6f 64 65 52 65 61 64 65 72 20 7b  uct NodeReader {
4602f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61  .  const char *a
46030 4e 6f 64 65 3b 0a 20 20 69 6e 74 20 6e 4e 6f 64  Node;.  int nNod
46031 65 3b 0a 20 20 69 6e 74 20 69 4f 66 66 3b 20 20  e;.  int iOff;  
46032 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46033 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
46034 6f 66 66 73 65 74 20 77 69 74 68 69 6e 20 61 4e  offset within aN
46035 6f 64 65 5b 5d 20 2a 2f 0a 0a 20 20 2f 2a 20 4f  ode[] */..  /* O
46036 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 73 2e  utput variables.
46037 20 43 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   Containing the 
46038 63 75 72 72 65 6e 74 20 6e 6f 64 65 20 65 6e 74  current node ent
46039 72 79 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ry. */.  sqlite3
4603a 5f 69 6e 74 36 34 20 69 43 68 69 6c 64 3b 20 20  _int64 iChild;  
4603b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
4603c 74 65 72 20 74 6f 20 63 68 69 6c 64 20 6e 6f 64  ter to child nod
4603d 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 74 65 72 6d  e */.  Blob term
4603e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4603f 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
46040 74 20 74 65 72 6d 20 2a 2f 0a 20 20 63 6f 6e 73  t term */.  cons
46041 74 20 63 68 61 72 20 2a 61 44 6f 63 6c 69 73 74  t char *aDoclist
46042 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  ;           /* P
46043 6f 69 6e 74 65 72 20 74 6f 20 64 6f 63 6c 69 73  ointer to doclis
46044 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 6f 63 6c  t */.  int nDocl
46045 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ist;            
46046 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
46047 66 20 64 6f 63 6c 69 73 74 20 69 6e 20 62 79 74  f doclist in byt
46048 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  es */.};../*.** 
46049 49 66 20 2a 70 52 63 20 69 73 20 6e 6f 74 20 53  If *pRc is not S
4604a 51 4c 49 54 45 5f 4f 4b 20 77 68 65 6e 20 74 68  QLITE_OK when th
4604b 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
4604c 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e  alled, it is a n
4604d 6f 2d 6f 70 2e 0a 2a 2a 20 4f 74 68 65 72 77 69  o-op..** Otherwi
4604e 73 65 2c 20 69 66 20 74 68 65 20 61 6c 6c 6f 63  se, if the alloc
4604f 61 74 69 6f 6e 20 61 74 20 70 42 6c 6f 62 2d 3e  ation at pBlob->
46050 61 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  a is not already
46051 20 61 74 20 6c 65 61 73 74 20 6e 4d 69 6e 0a 2a   at least nMin.*
46052 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c  * bytes in size,
46053 20 65 78 74 65 6e 64 20 28 72 65 61 6c 6c 6f 63   extend (realloc
46054 29 20 69 74 20 74 6f 20 62 65 20 73 6f 2e 0a 2a  ) it to be so..*
46055 2a 0a 2a 2a 20 49 66 20 61 6e 20 4f 4f 4d 20 65  *.** If an OOM e
46056 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 73 65 74  rror occurs, set
46057 20 2a 70 52 63 20 74 6f 20 53 51 4c 49 54 45 5f   *pRc to SQLITE_
46058 4e 4f 4d 45 4d 20 61 6e 64 20 6c 65 61 76 65 20  NOMEM and leave 
46059 70 42 6c 6f 62 2d 3e 61 0a 2a 2a 20 75 6e 6d 6f  pBlob->a.** unmo
4605a 64 69 66 69 65 64 2e 20 4f 74 68 65 72 77 69 73  dified. Otherwis
4605b 65 2c 20 69 66 20 74 68 65 20 61 6c 6c 6f 63 61  e, if the alloca
4605c 74 69 6f 6e 20 73 75 63 63 65 65 64 73 2c 20 75  tion succeeds, u
4605d 70 64 61 74 65 20 70 42 6c 6f 62 2d 3e 6e 41 6c  pdate pBlob->nAl
4605e 6c 6f 63 0a 2a 2a 20 74 6f 20 72 65 66 6c 65 63  loc.** to reflec
4605f 74 20 74 68 65 20 6e 65 77 20 73 69 7a 65 20 6f  t the new size o
46060 66 20 74 68 65 20 70 42 6c 6f 62 2d 3e 61 5b 5d  f the pBlob->a[]
46061 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74   buffer..*/.stat
46062 69 63 20 76 6f 69 64 20 62 6c 6f 62 47 72 6f 77  ic void blobGrow
46063 42 75 66 66 65 72 28 42 6c 6f 62 20 2a 70 42 6c  Buffer(Blob *pBl
46064 6f 62 2c 20 69 6e 74 20 6e 4d 69 6e 2c 20 69 6e  ob, int nMin, in
46065 74 20 2a 70 52 63 29 7b 0a 20 20 69 66 28 20 2a  t *pRc){.  if( *
46066 70 52 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26  pRc==SQLITE_OK &
46067 26 20 6e 4d 69 6e 3e 70 42 6c 6f 62 2d 3e 6e 41  & nMin>pBlob->nA
46068 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 69 6e 74 20  lloc ){.    int 
46069 6e 41 6c 6c 6f 63 20 3d 20 6e 4d 69 6e 3b 0a 20  nAlloc = nMin;. 
4606a 20 20 20 63 68 61 72 20 2a 61 20 3d 20 28 63 68     char *a = (ch
4606b 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 72 65 61  ar *)sqlite3_rea
4606c 6c 6c 6f 63 28 70 42 6c 6f 62 2d 3e 61 2c 20 6e  lloc(pBlob->a, n
4606d 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28 20  Alloc);.    if( 
4606e 61 20 29 7b 0a 20 20 20 20 20 20 70 42 6c 6f 62  a ){.      pBlob
4606f 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 41 6c 6c 6f  ->nAlloc = nAllo
46070 63 3b 0a 20 20 20 20 20 20 70 42 6c 6f 62 2d 3e  c;.      pBlob->
46071 61 20 3d 20 61 3b 0a 20 20 20 20 7d 65 6c 73 65  a = a;.    }else
46072 7b 0a 20 20 20 20 20 20 2a 70 52 63 20 3d 20 53  {.      *pRc = S
46073 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
46074 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
46075 41 74 74 65 6d 70 74 20 74 6f 20 61 64 76 61 6e  Attempt to advan
46076 63 65 20 74 68 65 20 6e 6f 64 65 2d 72 65 61 64  ce the node-read
46077 65 72 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64  er object passed
46078 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
46079 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65  gument to.** the
4607a 20 6e 65 78 74 20 65 6e 74 72 79 20 6f 6e 20 74   next entry on t
4607b 68 65 20 6e 6f 64 65 2e 20 0a 2a 2a 0a 2a 2a 20  he node. .**.** 
4607c 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  Return an error 
4607d 63 6f 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72  code if an error
4607e 20 6f 63 63 75 72 73 20 28 53 51 4c 49 54 45 5f   occurs (SQLITE_
4607f 4e 4f 4d 45 4d 20 69 73 20 70 6f 73 73 69 62 6c  NOMEM is possibl
46080 65 29 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73  e). .** Otherwis
46081 65 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  e return SQLITE_
46082 4f 4b 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  OK. If there is 
46083 6e 6f 20 6e 65 78 74 20 65 6e 74 72 79 20 6f 6e  no next entry on
46084 20 74 68 65 20 6e 6f 64 65 0a 2a 2a 20 28 65 2e   the node.** (e.
46085 67 2e 20 62 65 63 61 75 73 65 20 74 68 65 20 63  g. because the c
46086 75 72 72 65 6e 74 20 65 6e 74 72 79 20 69 73 20  urrent entry is 
46087 74 68 65 20 6c 61 73 74 29 20 73 65 74 20 4e 6f  the last) set No
46088 64 65 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 20  deReader->aNode 
46089 74 6f 0a 2a 2a 20 4e 55 4c 4c 20 74 6f 20 69 6e  to.** NULL to in
4608a 64 69 63 61 74 65 20 45 4f 46 2e 20 4f 74 68 65  dicate EOF. Othe
4608b 72 77 69 73 65 2c 20 70 6f 70 75 6c 61 74 65 20  rwise, populate 
4608c 74 68 65 20 4e 6f 64 65 52 65 61 64 65 72 20 73  the NodeReader s
4608d 74 72 75 63 74 75 72 65 20 6f 75 74 70 75 74 20  tructure output 
4608e 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 66 6f  .** variables fo
4608f 72 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 2e  r the new entry.
46090 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
46091 6f 64 65 52 65 61 64 65 72 4e 65 78 74 28 4e 6f  odeReaderNext(No
46092 64 65 52 65 61 64 65 72 20 2a 70 29 7b 0a 20 20  deReader *p){.  
46093 69 6e 74 20 62 46 69 72 73 74 20 3d 20 28 70 2d  int bFirst = (p-
46094 3e 74 65 72 6d 2e 6e 3d 3d 30 29 3b 20 20 20 20  >term.n==0);    
46095 2f 2a 20 54 72 75 65 20 66 6f 72 20 66 69 72 73  /* True for firs
46096 74 20 74 65 72 6d 20 6f 6e 20 74 68 65 20 6e 6f  t term on the no
46097 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 65  de */.  int nPre
46098 66 69 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20  fix = 0;        
46099 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
4609a 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 70 72   to copy from pr
4609b 65 76 69 6f 75 73 20 74 65 72 6d 20 2a 2f 0a 20  evious term */. 
4609c 20 69 6e 74 20 6e 53 75 66 66 69 78 20 3d 20 30   int nSuffix = 0
4609d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4609e 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 70 70   /* Bytes to app
4609f 65 6e 64 20 74 6f 20 74 68 65 20 70 72 65 66 69  end to the prefi
460a0 78 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20  x */.  int rc = 
460a1 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
460a2 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
460a3 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 61 73 73 65   code */..  asse
460a4 72 74 28 20 70 2d 3e 61 4e 6f 64 65 20 29 3b 0a  rt( p->aNode );.
460a5 20 20 69 66 28 20 70 2d 3e 69 43 68 69 6c 64 20    if( p->iChild 
460a6 26 26 20 62 46 69 72 73 74 3d 3d 30 20 29 20 70  && bFirst==0 ) p
460a7 2d 3e 69 43 68 69 6c 64 2b 2b 3b 0a 20 20 69 66  ->iChild++;.  if
460a8 28 20 70 2d 3e 69 4f 66 66 3e 3d 70 2d 3e 6e 4e  ( p->iOff>=p->nN
460a9 6f 64 65 20 29 7b 0a 20 20 20 20 2f 2a 20 45 4f  ode ){.    /* EO
460aa 46 20 2a 2f 0a 20 20 20 20 70 2d 3e 61 4e 6f 64  F */.    p->aNod
460ab 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  e = 0;.  }else{.
460ac 20 20 20 20 69 66 28 20 62 46 69 72 73 74 3d 3d      if( bFirst==
460ad 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4f  0 ){.      p->iO
460ae 66 66 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73  ff += sqlite3Fts
460af 33 47 65 74 56 61 72 69 6e 74 33 32 28 26 70 2d  3GetVarint32(&p-
460b0 3e 61 4e 6f 64 65 5b 70 2d 3e 69 4f 66 66 5d 2c  >aNode[p->iOff],
460b1 20 26 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20   &nPrefix);.    
460b2 7d 0a 20 20 20 20 70 2d 3e 69 4f 66 66 20 2b 3d  }.    p->iOff +=
460b3 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56   sqlite3Fts3GetV
460b4 61 72 69 6e 74 33 32 28 26 70 2d 3e 61 4e 6f 64  arint32(&p->aNod
460b5 65 5b 70 2d 3e 69 4f 66 66 5d 2c 20 26 6e 53 75  e[p->iOff], &nSu
460b6 66 66 69 78 29 3b 0a 0a 20 20 20 20 62 6c 6f 62  ffix);..    blob
460b7 47 72 6f 77 42 75 66 66 65 72 28 26 70 2d 3e 74  GrowBuffer(&p->t
460b8 65 72 6d 2c 20 6e 50 72 65 66 69 78 2b 6e 53 75  erm, nPrefix+nSu
460b9 66 66 69 78 2c 20 26 72 63 29 3b 0a 20 20 20 20  ffix, &rc);.    
460ba 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
460bb 4b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  K ){.      memcp
460bc 79 28 26 70 2d 3e 74 65 72 6d 2e 61 5b 6e 50 72  y(&p->term.a[nPr
460bd 65 66 69 78 5d 2c 20 26 70 2d 3e 61 4e 6f 64 65  efix], &p->aNode
460be 5b 70 2d 3e 69 4f 66 66 5d 2c 20 6e 53 75 66 66  [p->iOff], nSuff
460bf 69 78 29 3b 0a 20 20 20 20 20 20 70 2d 3e 74 65  ix);.      p->te
460c0 72 6d 2e 6e 20 3d 20 6e 50 72 65 66 69 78 2b 6e  rm.n = nPrefix+n
460c1 53 75 66 66 69 78 3b 0a 20 20 20 20 20 20 70 2d  Suffix;.      p-
460c2 3e 69 4f 66 66 20 2b 3d 20 6e 53 75 66 66 69 78  >iOff += nSuffix
460c3 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69  ;.      if( p->i
460c4 43 68 69 6c 64 3d 3d 30 20 29 7b 0a 20 20 20 20  Child==0 ){.    
460c5 20 20 20 20 70 2d 3e 69 4f 66 66 20 2b 3d 20 73      p->iOff += s
460c6 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61 72  qlite3Fts3GetVar
460c7 69 6e 74 33 32 28 26 70 2d 3e 61 4e 6f 64 65 5b  int32(&p->aNode[
460c8 70 2d 3e 69 4f 66 66 5d 2c 20 26 70 2d 3e 6e 44  p->iOff], &p->nD
460c9 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 20 20 20  oclist);.       
460ca 20 70 2d 3e 61 44 6f 63 6c 69 73 74 20 3d 20 26   p->aDoclist = &
460cb 70 2d 3e 61 4e 6f 64 65 5b 70 2d 3e 69 4f 66 66  p->aNode[p->iOff
460cc 5d 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4f  ];.        p->iO
460cd 66 66 20 2b 3d 20 70 2d 3e 6e 44 6f 63 6c 69 73  ff += p->nDoclis
460ce 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
460cf 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
460d0 70 2d 3e 69 4f 66 66 3c 3d 70 2d 3e 6e 4e 6f 64  p->iOff<=p->nNod
460d1 65 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72  e );..  return r
460d2 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65  c;.}../*.** Rele
460d3 61 73 65 20 61 6c 6c 20 64 79 6e 61 6d 69 63 20  ase all dynamic 
460d4 72 65 73 6f 75 72 63 65 73 20 68 65 6c 64 20 62  resources held b
460d5 79 20 6e 6f 64 65 2d 72 65 61 64 65 72 20 6f 62  y node-reader ob
460d6 6a 65 63 74 20 2a 70 2e 0a 2a 2f 0a 73 74 61 74  ject *p..*/.stat
460d7 69 63 20 76 6f 69 64 20 6e 6f 64 65 52 65 61 64  ic void nodeRead
460d8 65 72 52 65 6c 65 61 73 65 28 4e 6f 64 65 52 65  erRelease(NodeRe
460d9 61 64 65 72 20 2a 70 29 7b 0a 20 20 73 71 6c 69  ader *p){.  sqli
460da 74 65 33 5f 66 72 65 65 28 70 2d 3e 74 65 72 6d  te3_free(p->term
460db 2e 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  .a);.}../*.** In
460dc 69 74 69 61 6c 69 7a 65 20 61 20 6e 6f 64 65 2d  itialize a node-
460dd 72 65 61 64 65 72 20 6f 62 6a 65 63 74 20 74 6f  reader object to
460de 20 72 65 61 64 20 74 68 65 20 6e 6f 64 65 20 69   read the node i
460df 6e 20 62 75 66 66 65 72 20 61 4e 6f 64 65 2f 6e  n buffer aNode/n
460e0 4e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  Node..**.** If s
460e1 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54  uccessful, SQLIT
460e2 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
460e3 20 61 6e 64 20 74 68 65 20 4e 6f 64 65 52 65 61   and the NodeRea
460e4 64 65 72 20 6f 62 6a 65 63 74 20 73 65 74 20 74  der object set t
460e5 6f 20 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74  o .** point to t
460e6 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 6f  he first entry o
460e7 6e 20 74 68 65 20 6e 6f 64 65 20 28 69 66 20 61  n the node (if a
460e8 6e 79 29 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ny). Otherwise, 
460e9 61 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 65 72 72  an SQLite.** err
460ea 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
460eb 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
460ec 6e 74 20 6e 6f 64 65 52 65 61 64 65 72 49 6e 69  nt nodeReaderIni
460ed 74 28 4e 6f 64 65 52 65 61 64 65 72 20 2a 70 2c  t(NodeReader *p,
460ee 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 4e 6f   const char *aNo
460ef 64 65 2c 20 69 6e 74 20 6e 4e 6f 64 65 29 7b 0a  de, int nNode){.
460f0 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73    memset(p, 0, s
460f1 69 7a 65 6f 66 28 4e 6f 64 65 52 65 61 64 65 72  izeof(NodeReader
460f2 29 29 3b 0a 20 20 70 2d 3e 61 4e 6f 64 65 20 3d  ));.  p->aNode =
460f3 20 61 4e 6f 64 65 3b 0a 20 20 70 2d 3e 6e 4e 6f   aNode;.  p->nNo
460f4 64 65 20 3d 20 6e 4e 6f 64 65 3b 0a 0a 20 20 2f  de = nNode;..  /
460f5 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 66 20  * Figure out if 
460f6 74 68 69 73 20 69 73 20 61 20 6c 65 61 66 20 6f  this is a leaf o
460f7 72 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f  r an internal no
460f8 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  de. */.  if( p->
460f9 61 4e 6f 64 65 5b 30 5d 20 29 7b 0a 20 20 20 20  aNode[0] ){.    
460fa 2f 2a 20 41 6e 20 69 6e 74 65 72 6e 61 6c 20 6e  /* An internal n
460fb 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 70 2d 3e 69  ode. */.    p->i
460fc 4f 66 66 20 3d 20 31 20 2b 20 73 71 6c 69 74 65  Off = 1 + sqlite
460fd 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 28 26  3Fts3GetVarint(&
460fe 70 2d 3e 61 4e 6f 64 65 5b 31 5d 2c 20 26 70 2d  p->aNode[1], &p-
460ff 3e 69 43 68 69 6c 64 29 3b 0a 20 20 7d 65 6c 73  >iChild);.  }els
46100 65 7b 0a 20 20 20 20 70 2d 3e 69 4f 66 66 20 3d  e{.    p->iOff =
46101 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72   1;.  }..  retur
46102 6e 20 6e 6f 64 65 52 65 61 64 65 72 4e 65 78 74  n nodeReaderNext
46103 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  (p);.}../*.** Th
46104 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
46105 61 6c 6c 65 64 20 77 68 69 6c 65 20 77 72 69 74  alled while writ
46106 69 6e 67 20 61 6e 20 46 54 53 20 73 65 67 6d 65  ing an FTS segme
46107 6e 74 20 65 61 63 68 20 74 69 6d 65 20 61 20 6c  nt each time a l
46108 65 61 66 20 6f 0a 2a 2a 20 6e 6f 64 65 20 69 73  eaf o.** node is
46109 20 66 69 6e 69 73 68 65 64 20 61 6e 64 20 77 72   finished and wr
4610a 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 20 54  itten to disk. T
4610b 68 65 20 6b 65 79 20 28 7a 54 65 72 6d 2f 6e 54  he key (zTerm/nT
4610c 65 72 6d 29 20 69 73 20 67 75 61 72 61 6e 74 65  erm) is guarante
4610d 65 64 0a 2a 2a 20 74 6f 20 62 65 20 67 72 65 61  ed.** to be grea
4610e 74 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61 72  ter than the lar
4610f 67 65 73 74 20 6b 65 79 20 6f 6e 20 74 68 65 20  gest key on the 
46110 6e 6f 64 65 20 6a 75 73 74 20 77 72 69 74 74 65  node just writte
46111 6e 2c 20 62 75 74 20 73 6d 61 6c 6c 65 72 0a 2a  n, but smaller.*
46112 2a 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  * than or equal 
46113 74 6f 20 74 68 65 20 66 69 72 73 74 20 6b 65 79  to the first key
46114 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 77 72   that will be wr
46115 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6e 65 78  itten to the nex
46116 74 20 6c 65 61 66 0a 2a 2a 20 6e 6f 64 65 2e 0a  t leaf.** node..
46117 2a 2a 0a 2a 2a 20 54 68 65 20 62 6c 6f 63 6b 20  **.** The block 
46118 69 64 20 6f 66 20 74 68 65 20 6c 65 61 66 20 6e  id of the leaf n
46119 6f 64 65 20 6a 75 73 74 20 77 72 69 74 74 65 6e  ode just written
4611a 20 74 6f 20 64 69 73 6b 20 6d 61 79 20 62 65 20   to disk may be 
4611b 66 6f 75 6e 64 20 69 6e 0a 2a 2a 20 28 70 57 72  found in.** (pWr
4611c 69 74 65 72 2d 3e 61 4e 6f 64 65 57 72 69 74 65  iter->aNodeWrite
4611d 72 5b 30 5d 2e 69 42 6c 6f 63 6b 29 20 77 68 65  r[0].iBlock) whe
4611e 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
4611f 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74  is called..*/.st
46120 61 74 69 63 20 69 6e 74 20 66 74 73 33 49 6e 63  atic int fts3Inc
46121 72 6d 65 72 67 65 50 75 73 68 28 0a 20 20 46 74  rmergePush(.  Ft
46122 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20  s3Table *p,     
46123 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
46124 20 46 74 73 33 20 74 61 62 6c 65 20 68 61 6e 64   Fts3 table hand
46125 6c 65 20 2a 2f 0a 20 20 49 6e 63 72 6d 65 72 67  le */.  Incrmerg
46126 65 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  eWriter *pWriter
46127 2c 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65  ,       /* Write
46128 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f  r object */.  co
46129 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 72 6d 2c  nst char *zTerm,
4612a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4612b 20 54 65 72 6d 20 74 6f 20 77 72 69 74 65 20 74   Term to write t
4612c 6f 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20  o internal node 
4612d 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 20 20  */.  int nTerm  
4612e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4612f 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 61 74       /* Bytes at
46130 20 7a 54 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 73   zTerm */.){.  s
46131 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50 74  qlite3_int64 iPt
46132 72 20 3d 20 70 57 72 69 74 65 72 2d 3e 61 4e 6f  r = pWriter->aNo
46133 64 65 57 72 69 74 65 72 5b 30 5d 2e 69 42 6c 6f  deWriter[0].iBlo
46134 63 6b 3b 0a 20 20 69 6e 74 20 69 4c 61 79 65 72  ck;.  int iLayer
46135 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65  ;..  assert( nTe
46136 72 6d 3e 30 20 29 3b 0a 20 20 66 6f 72 28 69 4c  rm>0 );.  for(iL
46137 61 79 65 72 3d 31 3b 20 41 4c 57 41 59 53 28 69  ayer=1; ALWAYS(i
46138 4c 61 79 65 72 3c 46 54 53 5f 4d 41 58 5f 41 50  Layer<FTS_MAX_AP
46139 50 45 4e 44 41 42 4c 45 5f 48 45 49 47 48 54 29  PENDABLE_HEIGHT)
4613a 3b 20 69 4c 61 79 65 72 2b 2b 29 7b 0a 20 20 20  ; iLayer++){.   
4613b 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
4613c 4e 65 78 74 50 74 72 20 3d 20 30 3b 0a 20 20 20  NextPtr = 0;.   
4613d 20 4e 6f 64 65 57 72 69 74 65 72 20 2a 70 4e 6f   NodeWriter *pNo
4613e 64 65 20 3d 20 26 70 57 72 69 74 65 72 2d 3e 61  de = &pWriter->a
4613f 4e 6f 64 65 57 72 69 74 65 72 5b 69 4c 61 79 65  NodeWriter[iLaye
46140 72 5d 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d  r];.    int rc =
46141 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
46142 69 6e 74 20 6e 50 72 65 66 69 78 3b 0a 20 20 20  int nPrefix;.   
46143 20 69 6e 74 20 6e 53 75 66 66 69 78 3b 0a 20 20   int nSuffix;.  
46144 20 20 69 6e 74 20 6e 53 70 61 63 65 3b 0a 0a 20    int nSpace;.. 
46145 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
46146 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65 20   how much space 
46147 74 68 65 20 6b 65 79 20 77 69 6c 6c 20 63 6f 6e  the key will con
46148 73 75 6d 65 20 69 66 20 69 74 20 69 73 20 77 72  sume if it is wr
46149 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a 2a 20  itten to.    ** 
4614a 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65  the current node
4614b 20 6f 66 20 6c 61 79 65 72 20 69 4c 61 79 65 72   of layer iLayer
4614c 2e 20 44 75 65 20 74 6f 20 74 68 65 20 70 72 65  . Due to the pre
4614d 66 69 78 20 63 6f 6d 70 72 65 73 73 69 6f 6e 2c  fix compression,
4614e 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 70 61   .    ** the spa
4614f 63 65 20 72 65 71 75 69 72 65 64 20 63 68 61 6e  ce required chan
46150 67 65 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  ges depending on
46151 20 77 68 69 63 68 20 6e 6f 64 65 20 74 68 65 20   which node the 
46152 6b 65 79 20 69 73 20 74 6f 0a 20 20 20 20 2a 2a  key is to.    **
46153 20 62 65 20 61 64 64 65 64 20 74 6f 2e 20 20 2a   be added to.  *
46154 2f 0a 20 20 20 20 6e 50 72 65 66 69 78 20 3d 20  /.    nPrefix = 
46155 66 74 73 33 50 72 65 66 69 78 43 6f 6d 70 72 65  fts3PrefixCompre
46156 73 73 28 70 4e 6f 64 65 2d 3e 6b 65 79 2e 61 2c  ss(pNode->key.a,
46157 20 70 4e 6f 64 65 2d 3e 6b 65 79 2e 6e 2c 20 7a   pNode->key.n, z
46158 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20  Term, nTerm);.  
46159 20 20 6e 53 75 66 66 69 78 20 3d 20 6e 54 65 72    nSuffix = nTer
4615a 6d 20 2d 20 6e 50 72 65 66 69 78 3b 0a 20 20 20  m - nPrefix;.   
4615b 20 6e 53 70 61 63 65 20 20 3d 20 73 71 6c 69 74   nSpace  = sqlit
4615c 65 33 46 74 73 33 56 61 72 69 6e 74 4c 65 6e 28  e3Fts3VarintLen(
4615d 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20 6e 53  nPrefix);.    nS
4615e 70 61 63 65 20 2b 3d 20 73 71 6c 69 74 65 33 46  pace += sqlite3F
4615f 74 73 33 56 61 72 69 6e 74 4c 65 6e 28 6e 53 75  ts3VarintLen(nSu
46160 66 66 69 78 29 20 2b 20 6e 53 75 66 66 69 78 3b  ffix) + nSuffix;
46161 0a 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d  ..    if( pNode-
46162 3e 6b 65 79 2e 6e 3d 3d 30 20 7c 7c 20 28 70 4e  >key.n==0 || (pN
46163 6f 64 65 2d 3e 62 6c 6f 63 6b 2e 6e 20 2b 20 6e  ode->block.n + n
46164 53 70 61 63 65 29 3c 3d 70 2d 3e 6e 4e 6f 64 65  Space)<=p->nNode
46165 53 69 7a 65 20 29 7b 20 0a 20 20 20 20 20 20 2f  Size ){ .      /
46166 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74  * If the current
46167 20 6e 6f 64 65 20 6f 66 20 6c 61 79 65 72 20 69   node of layer i
46168 4c 61 79 65 72 20 63 6f 6e 74 61 69 6e 73 20 7a  Layer contains z
46169 65 72 6f 20 6b 65 79 73 2c 20 6f 72 20 69 66 20  ero keys, or if 
4616a 61 64 64 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20  adding.      ** 
4616b 74 68 65 20 6b 65 79 20 74 6f 20 69 74 20 77 69  the key to it wi
4616c 6c 6c 20 6e 6f 74 20 63 61 75 73 65 20 69 74 20  ll not cause it 
4616d 74 6f 20 67 72 6f 77 20 74 6f 20 6c 61 72 67 65  to grow to large
4616e 72 20 74 68 61 6e 20 6e 4e 6f 64 65 53 69 7a 65  r than nNodeSize
4616f 20 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73   .      ** bytes
46170 20 69 6e 20 73 69 7a 65 2c 20 77 72 69 74 65 20   in size, write 
46171 74 68 65 20 6b 65 79 20 68 65 72 65 2e 20 20 2a  the key here.  *
46172 2f 0a 0a 20 20 20 20 20 20 42 6c 6f 62 20 2a 70  /..      Blob *p
46173 42 6c 6b 20 3d 20 26 70 4e 6f 64 65 2d 3e 62 6c  Blk = &pNode->bl
46174 6f 63 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ock;.      if( p
46175 42 6c 6b 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20  Blk->n==0 ){.   
46176 20 20 20 20 20 62 6c 6f 62 47 72 6f 77 42 75 66       blobGrowBuf
46177 66 65 72 28 70 42 6c 6b 2c 20 70 2d 3e 6e 4e 6f  fer(pBlk, p->nNo
46178 64 65 53 69 7a 65 2c 20 26 72 63 29 3b 0a 20 20  deSize, &rc);.  
46179 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
4617a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
4617b 20 20 20 20 20 70 42 6c 6b 2d 3e 61 5b 30 5d 20       pBlk->a[0] 
4617c 3d 20 28 63 68 61 72 29 69 4c 61 79 65 72 3b 0a  = (char)iLayer;.
4617d 20 20 20 20 20 20 20 20 20 20 70 42 6c 6b 2d 3e            pBlk->
4617e 6e 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 46  n = 1 + sqlite3F
4617f 74 73 33 50 75 74 56 61 72 69 6e 74 28 26 70 42  ts3PutVarint(&pB
46180 6c 6b 2d 3e 61 5b 31 5d 2c 20 69 50 74 72 29 3b  lk->a[1], iPtr);
46181 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
46182 20 7d 0a 20 20 20 20 20 20 62 6c 6f 62 47 72 6f   }.      blobGro
46183 77 42 75 66 66 65 72 28 70 42 6c 6b 2c 20 70 42  wBuffer(pBlk, pB
46184 6c 6b 2d 3e 6e 20 2b 20 6e 53 70 61 63 65 2c 20  lk->n + nSpace, 
46185 26 72 63 29 3b 0a 20 20 20 20 20 20 62 6c 6f 62  &rc);.      blob
46186 47 72 6f 77 42 75 66 66 65 72 28 26 70 4e 6f 64  GrowBuffer(&pNod
46187 65 2d 3e 6b 65 79 2c 20 6e 54 65 72 6d 2c 20 26  e->key, nTerm, &
46188 72 63 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20  rc);..      if( 
46189 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
4618a 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 6f  .        if( pNo
4618b 64 65 2d 3e 6b 65 79 2e 6e 20 29 7b 0a 20 20 20  de->key.n ){.   
4618c 20 20 20 20 20 20 20 70 42 6c 6b 2d 3e 6e 20 2b         pBlk->n +
4618d 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74  = sqlite3Fts3Put
4618e 56 61 72 69 6e 74 28 26 70 42 6c 6b 2d 3e 61 5b  Varint(&pBlk->a[
4618f 70 42 6c 6b 2d 3e 6e 5d 2c 20 6e 50 72 65 66 69  pBlk->n], nPrefi
46190 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  x);.        }.  
46191 20 20 20 20 20 20 70 42 6c 6b 2d 3e 6e 20 2b 3d        pBlk->n +=
46192 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74 56   sqlite3Fts3PutV
46193 61 72 69 6e 74 28 26 70 42 6c 6b 2d 3e 61 5b 70  arint(&pBlk->a[p
46194 42 6c 6b 2d 3e 6e 5d 2c 20 6e 53 75 66 66 69 78  Blk->n], nSuffix
46195 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  );.        memcp
46196 79 28 26 70 42 6c 6b 2d 3e 61 5b 70 42 6c 6b 2d  y(&pBlk->a[pBlk-
46197 3e 6e 5d 2c 20 26 7a 54 65 72 6d 5b 6e 50 72 65  >n], &zTerm[nPre
46198 66 69 78 5d 2c 20 6e 53 75 66 66 69 78 29 3b 0a  fix], nSuffix);.
46199 20 20 20 20 20 20 20 20 70 42 6c 6b 2d 3e 6e 20          pBlk->n 
4619a 2b 3d 20 6e 53 75 66 66 69 78 3b 0a 0a 20 20 20  += nSuffix;..   
4619b 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 6f 64       memcpy(pNod
4619c 65 2d 3e 6b 65 79 2e 61 2c 20 7a 54 65 72 6d 2c  e->key.a, zTerm,
4619d 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 20 20 20   nTerm);.       
4619e 20 70 4e 6f 64 65 2d 3e 6b 65 79 2e 6e 20 3d 20   pNode->key.n = 
4619f 6e 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a 20  nTerm;.      }. 
461a0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
461a1 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6c  /* Otherwise, fl
461a2 75 73 68 20 74 68 65 20 63 75 72 72 65 6e 74 20  ush the current 
461a3 6e 6f 64 65 20 6f 66 20 6c 61 79 65 72 20 69 4c  node of layer iL
461a4 61 79 65 72 20 74 6f 20 64 69 73 6b 2e 0a 20 20  ayer to disk..  
461a5 20 20 20 20 2a 2a 20 54 68 65 6e 20 61 6c 6c 6f      ** Then allo
461a6 63 61 74 65 20 61 20 6e 65 77 2c 20 65 6d 70 74  cate a new, empt
461a7 79 20 73 69 62 6c 69 6e 67 20 6e 6f 64 65 2e 20  y sibling node. 
461a8 54 68 65 20 6b 65 79 20 77 69 6c 6c 20 62 65 20  The key will be 
461a9 77 72 69 74 74 65 6e 0a 20 20 20 20 20 20 2a 2a  written.      **
461aa 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74   into the parent
461ab 20 6f 66 20 74 68 69 73 20 6e 6f 64 65 2e 20 2a   of this node. *
461ac 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73  /.      rc = fts
461ad 33 57 72 69 74 65 53 65 67 6d 65 6e 74 28 70 2c  3WriteSegment(p,
461ae 20 70 4e 6f 64 65 2d 3e 69 42 6c 6f 63 6b 2c 20   pNode->iBlock, 
461af 70 4e 6f 64 65 2d 3e 62 6c 6f 63 6b 2e 61 2c 20  pNode->block.a, 
461b0 70 4e 6f 64 65 2d 3e 62 6c 6f 63 6b 2e 6e 29 3b  pNode->block.n);
461b1 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
461b2 70 4e 6f 64 65 2d 3e 62 6c 6f 63 6b 2e 6e 41 6c  pNode->block.nAl
461b3 6c 6f 63 3e 3d 70 2d 3e 6e 4e 6f 64 65 53 69 7a  loc>=p->nNodeSiz
461b4 65 20 29 3b 0a 20 20 20 20 20 20 70 4e 6f 64 65  e );.      pNode
461b5 2d 3e 62 6c 6f 63 6b 2e 61 5b 30 5d 20 3d 20 28  ->block.a[0] = (
461b6 63 68 61 72 29 69 4c 61 79 65 72 3b 0a 20 20 20  char)iLayer;.   
461b7 20 20 20 70 4e 6f 64 65 2d 3e 62 6c 6f 63 6b 2e     pNode->block.
461b8 6e 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 46  n = 1 + sqlite3F
461b9 74 73 33 50 75 74 56 61 72 69 6e 74 28 26 70 4e  ts3PutVarint(&pN
461ba 6f 64 65 2d 3e 62 6c 6f 63 6b 2e 61 5b 31 5d 2c  ode->block.a[1],
461bb 20 69 50 74 72 2b 31 29 3b 0a 0a 20 20 20 20 20   iPtr+1);..     
461bc 20 69 4e 65 78 74 50 74 72 20 3d 20 70 4e 6f 64   iNextPtr = pNod
461bd 65 2d 3e 69 42 6c 6f 63 6b 3b 0a 20 20 20 20 20  e->iBlock;.     
461be 20 70 4e 6f 64 65 2d 3e 69 42 6c 6f 63 6b 2b 2b   pNode->iBlock++
461bf 3b 0a 20 20 20 20 20 20 70 4e 6f 64 65 2d 3e 6b  ;.      pNode->k
461c0 65 79 2e 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  ey.n = 0;.    }.
461c1 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
461c2 49 54 45 5f 4f 4b 20 7c 7c 20 69 4e 65 78 74 50  ITE_OK || iNextP
461c3 74 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 72  tr==0 ) return r
461c4 63 3b 0a 20 20 20 20 69 50 74 72 20 3d 20 69 4e  c;.    iPtr = iN
461c5 65 78 74 50 74 72 3b 0a 20 20 7d 0a 0a 20 20 61  extPtr;.  }..  a
461c6 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 72 65  ssert( 0 );.  re
461c7 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
461c8 20 41 70 70 65 6e 64 20 61 20 74 65 72 6d 20 61   Append a term a
461c9 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20  nd (optionally) 
461ca 64 6f 63 6c 69 73 74 20 74 6f 20 74 68 65 20 46  doclist to the F
461cb 54 53 20 73 65 67 6d 65 6e 74 20 6e 6f 64 65 20  TS segment node 
461cc 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 6f  currently.** sto
461cd 72 65 64 20 69 6e 20 62 6c 6f 62 20 2a 70 4e 6f  red in blob *pNo
461ce 64 65 2e 20 54 68 65 20 6e 6f 64 65 20 6e 65 65  de. The node nee
461cf 64 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e  d not contain an
461d0 79 20 74 65 72 6d 73 2c 20 62 75 74 20 74 68 65  y terms, but the
461d1 0a 2a 2a 20 68 65 61 64 65 72 20 6d 75 73 74 20  .** header must 
461d2 62 65 20 77 72 69 74 74 65 6e 20 62 65 66 6f 72  be written befor
461d3 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  e this function 
461d4 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a  is called..**.**
461d5 20 41 20 6e 6f 64 65 20 68 65 61 64 65 72 20 69   A node header i
461d6 73 20 61 20 73 69 6e 67 6c 65 20 30 78 30 30 20  s a single 0x00 
461d7 62 79 74 65 20 66 6f 72 20 61 20 6c 65 61 66 20  byte for a leaf 
461d8 6e 6f 64 65 2c 20 6f 72 20 61 20 68 65 69 67 68  node, or a heigh
461d9 74 20 76 61 72 69 6e 74 0a 2a 2a 20 66 6f 6c 6c  t varint.** foll
461da 6f 77 65 64 20 62 79 20 74 68 65 20 6c 65 66 74  owed by the left
461db 2d 68 61 6e 64 2d 63 68 69 6c 64 20 76 61 72 69  -hand-child vari
461dc 6e 74 20 66 6f 72 20 61 6e 20 69 6e 74 65 72 6e  nt for an intern
461dd 61 6c 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54  al node..**.** T
461de 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20 61 70  he term to be ap
461df 70 65 6e 64 65 64 20 69 73 20 70 61 73 73 65 64  pended is passed
461e0 20 76 69 61 20 61 72 67 75 6d 65 6e 74 73 20 7a   via arguments z
461e1 54 65 72 6d 2f 6e 54 65 72 6d 2e 20 46 6f 72 20  Term/nTerm. For 
461e2 61 20 0a 2a 2a 20 6c 65 61 66 20 6e 6f 64 65 2c  a .** leaf node,
461e3 20 74 68 65 20 64 6f 63 6c 69 73 74 20 69 73 20   the doclist is 
461e4 70 61 73 73 65 64 20 61 73 20 61 44 6f 63 6c 69  passed as aDocli
461e5 73 74 2f 6e 44 6f 63 6c 69 73 74 2e 20 46 6f 72  st/nDoclist. For
461e6 20 61 6e 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20   an internal.** 
461e7 6e 6f 64 65 2c 20 62 6f 74 68 20 61 44 6f 63 6c  node, both aDocl
461e8 69 73 74 20 61 6e 64 20 6e 44 6f 63 6c 69 73 74  ist and nDoclist
461e9 20 6d 75 73 74 20 62 65 20 70 61 73 73 65 64 20   must be passed 
461ea 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  0..**.** If the 
461eb 73 69 7a 65 20 6f 66 20 74 68 65 20 76 61 6c 75  size of the valu
461ec 65 20 69 6e 20 62 6c 6f 62 20 70 50 72 65 76 20  e in blob pPrev 
461ed 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  is zero, then th
461ee 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 0a  is is the first.
461ef 2a 2a 20 74 65 72 6d 20 77 72 69 74 74 65 6e 20  ** term written 
461f0 74 6f 20 74 68 65 20 6e 6f 64 65 2e 20 4f 74 68  to the node. Oth
461f1 65 72 77 69 73 65 2c 20 70 50 72 65 76 20 63 6f  erwise, pPrev co
461f2 6e 74 61 69 6e 73 20 61 20 63 6f 70 79 20 6f 66  ntains a copy of
461f3 20 74 68 65 20 0a 2a 2a 20 70 72 65 76 69 6f 75   the .** previou
461f4 73 20 74 65 72 6d 2e 20 42 65 66 6f 72 65 20 74  s term. Before t
461f5 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
461f6 75 72 6e 73 2c 20 69 74 20 69 73 20 75 70 64 61  urns, it is upda
461f7 74 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61  ted to contain a
461f8 0a 2a 2a 20 63 6f 70 79 20 6f 66 20 7a 54 65 72  .** copy of zTer
461f9 6d 2f 6e 54 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 49  m/nTerm..**.** I
461fa 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
461fb 74 20 74 68 65 20 62 75 66 66 65 72 20 61 73 73  t the buffer ass
461fc 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 4e 6f  ociated with pNo
461fd 64 65 20 69 73 20 61 6c 72 65 61 64 79 20 6c 61  de is already la
461fe 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f  rge.** enough to
461ff 20 61 63 63 6f 6d 6d 6f 64 61 74 65 20 74 68 65   accommodate the
46200 20 6e 65 77 20 65 6e 74 72 79 2e 20 54 68 65 20   new entry. The 
46201 62 75 66 66 65 72 20 61 73 73 6f 63 69 61 74 65  buffer associate
46202 64 20 77 69 74 68 20 70 50 72 65 76 0a 2a 2a 20  d with pPrev.** 
46203 69 73 20 65 78 74 65 6e 64 65 64 20 62 79 20 74  is extended by t
46204 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20  his function if 
46205 72 65 71 75 72 69 72 65 64 2e 0a 2a 2a 0a 2a 2a  requrired..**.**
46206 20 49 66 20 61 6e 20 65 72 72 6f 72 20 28 69 2e   If an error (i.
46207 65 2e 20 4f 4f 4d 20 63 6f 6e 64 69 74 69 6f 6e  e. OOM condition
46208 29 20 6f 63 63 75 72 73 2c 20 61 6e 20 53 51 4c  ) occurs, an SQL
46209 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  ite error code i
4620a 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 4f  s.** returned. O
4620b 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45  therwise, SQLITE
4620c 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  _OK..*/.static i
4620d 6e 74 20 66 74 73 33 41 70 70 65 6e 64 54 6f 4e  nt fts3AppendToN
4620e 6f 64 65 28 0a 20 20 42 6c 6f 62 20 2a 70 4e 6f  ode(.  Blob *pNo
4620f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  de,             
46210 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
46211 74 20 6e 6f 64 65 20 69 6d 61 67 65 20 74 6f 20  t node image to 
46212 61 70 70 65 6e 64 20 74 6f 20 2a 2f 0a 20 20 42  append to */.  B
46213 6c 6f 62 20 2a 70 50 72 65 76 2c 20 20 20 20 20  lob *pPrev,     
46214 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
46215 2a 20 42 75 66 66 65 72 20 63 6f 6e 74 61 69 6e  * Buffer contain
46216 69 6e 67 20 70 72 65 76 69 6f 75 73 20 74 65 72  ing previous ter
46217 6d 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 63  m written */.  c
46218 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 72 6d  onst char *zTerm
46219 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
4621a 2a 20 4e 65 77 20 74 65 72 6d 20 74 6f 20 77 72  * New term to wr
4621b 69 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65  ite */.  int nTe
4621c 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  rm,             
4621d 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
4621e 20 6f 66 20 7a 54 65 72 6d 20 69 6e 20 62 79 74   of zTerm in byt
4621f 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  es */.  const ch
46220 61 72 20 2a 61 44 6f 63 6c 69 73 74 2c 20 20 20  ar *aDoclist,   
46221 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69          /* Docli
46222 73 74 20 28 6f 72 20 4e 55 4c 4c 29 20 74 6f 20  st (or NULL) to 
46223 77 72 69 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  write */.  int n
46224 44 6f 63 6c 69 73 74 20 20 20 20 20 20 20 20 20  Doclist         
46225 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
46226 7a 65 20 6f 66 20 61 44 6f 63 6c 69 73 74 20 69  ze of aDoclist i
46227 6e 20 62 79 74 65 73 20 2a 2f 20 0a 29 7b 0a 20  n bytes */ .){. 
46228 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
46229 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
4622a 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
4622b 2a 2f 0a 20 20 69 6e 74 20 62 46 69 72 73 74 20  */.  int bFirst 
4622c 3d 20 28 70 50 72 65 76 2d 3e 6e 3d 3d 30 29 3b  = (pPrev->n==0);
4622d 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
4622e 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73  this is the firs
4622f 74 20 74 65 72 6d 20 77 72 69 74 74 65 6e 20 2a  t term written *
46230 2f 0a 20 20 69 6e 74 20 6e 50 72 65 66 69 78 3b  /.  int nPrefix;
46231 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46232 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
46233 65 72 6d 20 70 72 65 66 69 78 20 69 6e 20 62 79  erm prefix in by
46234 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75  tes */.  int nSu
46235 66 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20  ffix;           
46236 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
46237 20 6f 66 20 74 65 72 6d 20 73 75 66 66 69 78 20   of term suffix 
46238 69 6e 20 62 79 74 65 73 20 2a 2f 0a 0a 20 20 2f  in bytes */..  /
46239 2a 20 4e 6f 64 65 20 6d 75 73 74 20 68 61 76 65  * Node must have
4623a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73 74   already been st
4623b 61 72 74 65 64 2e 20 54 68 65 72 65 20 6d 75 73  arted. There mus
4623c 74 20 62 65 20 61 20 64 6f 63 6c 69 73 74 20 66  t be a doclist f
4623d 6f 72 20 61 0a 20 20 2a 2a 20 6c 65 61 66 20 6e  or a.  ** leaf n
4623e 6f 64 65 2c 20 61 6e 64 20 74 68 65 72 65 20 6d  ode, and there m
4623f 75 73 74 20 6e 6f 74 20 62 65 20 61 20 64 6f 63  ust not be a doc
46240 6c 69 73 74 20 66 6f 72 20 61 6e 20 69 6e 74 65  list for an inte
46241 72 6e 61 6c 20 6e 6f 64 65 2e 20 20 2a 2f 0a 20  rnal node.  */. 
46242 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65 2d 3e   assert( pNode->
46243 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  n>0 );.  assert(
46244 20 28 70 4e 6f 64 65 2d 3e 61 5b 30 5d 3d 3d 27   (pNode->a[0]=='
46245 5c 30 27 29 3d 3d 28 61 44 6f 63 6c 69 73 74 21  \0')==(aDoclist!
46246 3d 30 29 20 29 3b 0a 0a 20 20 62 6c 6f 62 47 72  =0) );..  blobGr
46247 6f 77 42 75 66 66 65 72 28 70 50 72 65 76 2c 20  owBuffer(pPrev, 
46248 6e 54 65 72 6d 2c 20 26 72 63 29 3b 0a 20 20 69  nTerm, &rc);.  i
46249 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
4624a 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
4624b 20 6e 50 72 65 66 69 78 20 3d 20 66 74 73 33 50   nPrefix = fts3P
4624c 72 65 66 69 78 43 6f 6d 70 72 65 73 73 28 70 50  refixCompress(pP
4624d 72 65 76 2d 3e 61 2c 20 70 50 72 65 76 2d 3e 6e  rev->a, pPrev->n
4624e 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b  , zTerm, nTerm);
4624f 0a 20 20 6e 53 75 66 66 69 78 20 3d 20 6e 54 65  .  nSuffix = nTe
46250 72 6d 20 2d 20 6e 50 72 65 66 69 78 3b 0a 20 20  rm - nPrefix;.  
46251 6d 65 6d 63 70 79 28 70 50 72 65 76 2d 3e 61 2c  memcpy(pPrev->a,
46252 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a   zTerm, nTerm);.
46253 20 20 70 50 72 65 76 2d 3e 6e 20 3d 20 6e 54 65    pPrev->n = nTe
46254 72 6d 3b 0a 0a 20 20 69 66 28 20 62 46 69 72 73  rm;..  if( bFirs
46255 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 6f 64  t==0 ){.    pNod
46256 65 2d 3e 6e 20 2b 3d 20 73 71 6c 69 74 65 33 46  e->n += sqlite3F
46257 74 73 33 50 75 74 56 61 72 69 6e 74 28 26 70 4e  ts3PutVarint(&pN
46258 6f 64 65 2d 3e 61 5b 70 4e 6f 64 65 2d 3e 6e 5d  ode->a[pNode->n]
46259 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20 20 7d 0a  , nPrefix);.  }.
4625a 20 20 70 4e 6f 64 65 2d 3e 6e 20 2b 3d 20 73 71    pNode->n += sq
4625b 6c 69 74 65 33 46 74 73 33 50 75 74 56 61 72 69  lite3Fts3PutVari
4625c 6e 74 28 26 70 4e 6f 64 65 2d 3e 61 5b 70 4e 6f  nt(&pNode->a[pNo
4625d 64 65 2d 3e 6e 5d 2c 20 6e 53 75 66 66 69 78 29  de->n], nSuffix)
4625e 3b 0a 20 20 6d 65 6d 63 70 79 28 26 70 4e 6f 64  ;.  memcpy(&pNod
4625f 65 2d 3e 61 5b 70 4e 6f 64 65 2d 3e 6e 5d 2c 20  e->a[pNode->n], 
46260 26 7a 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 2c  &zTerm[nPrefix],
46261 20 6e 53 75 66 66 69 78 29 3b 0a 20 20 70 4e 6f   nSuffix);.  pNo
46262 64 65 2d 3e 6e 20 2b 3d 20 6e 53 75 66 66 69 78  de->n += nSuffix
46263 3b 0a 0a 20 20 69 66 28 20 61 44 6f 63 6c 69 73  ;..  if( aDoclis
46264 74 20 29 7b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  t ){.    pNode->
46265 6e 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33  n += sqlite3Fts3
46266 50 75 74 56 61 72 69 6e 74 28 26 70 4e 6f 64 65  PutVarint(&pNode
46267 2d 3e 61 5b 70 4e 6f 64 65 2d 3e 6e 5d 2c 20 6e  ->a[pNode->n], n
46268 44 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 6d 65  Doclist);.    me
46269 6d 63 70 79 28 26 70 4e 6f 64 65 2d 3e 61 5b 70  mcpy(&pNode->a[p
4626a 4e 6f 64 65 2d 3e 6e 5d 2c 20 61 44 6f 63 6c 69  Node->n], aDocli
4626b 73 74 2c 20 6e 44 6f 63 6c 69 73 74 29 3b 0a 20  st, nDoclist);. 
4626c 20 20 20 70 4e 6f 64 65 2d 3e 6e 20 2b 3d 20 6e     pNode->n += n
4626d 44 6f 63 6c 69 73 74 3b 0a 20 20 7d 0a 0a 20 20  Doclist;.  }..  
4626e 61 73 73 65 72 74 28 20 70 4e 6f 64 65 2d 3e 6e  assert( pNode->n
4626f 3c 3d 70 4e 6f 64 65 2d 3e 6e 41 6c 6c 6f 63 20  <=pNode->nAlloc 
46270 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  );..  return SQL
46271 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
46272 20 41 70 70 65 6e 64 20 74 68 65 20 63 75 72 72   Append the curr
46273 65 6e 74 20 74 65 72 6d 20 61 6e 64 20 64 6f 63  ent term and doc
46274 6c 69 73 74 20 70 6f 69 6e 74 65 64 20 74 6f 20  list pointed to 
46275 62 79 20 63 75 72 73 6f 72 20 70 43 73 72 20 74  by cursor pCsr t
46276 6f 20 74 68 65 0a 2a 2a 20 61 70 70 65 6e 64 61  o the.** appenda
46277 62 6c 65 20 62 2d 74 72 65 65 20 73 65 67 6d 65  ble b-tree segme
46278 6e 74 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72  nt opened for wr
46279 69 74 69 6e 67 20 62 79 20 70 57 72 69 74 65 72  iting by pWriter
4627a 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
4627b 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63  QLITE_OK if succ
4627c 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51  essful, or an SQ
4627d 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  Lite error code 
4627e 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74  otherwise..*/.st
4627f 61 74 69 63 20 69 6e 74 20 66 74 73 33 49 6e 63  atic int fts3Inc
46280 72 6d 65 72 67 65 41 70 70 65 6e 64 28 0a 20 20  rmergeAppend(.  
46281 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20  Fts3Table *p,   
46282 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46283 2f 2a 20 46 74 73 33 20 74 61 62 6c 65 20 68 61  /* Fts3 table ha
46284 6e 64 6c 65 20 2a 2f 0a 20 20 49 6e 63 72 6d 65  ndle */.  Incrme
46285 72 67 65 57 72 69 74 65 72 20 2a 70 57 72 69 74  rgeWriter *pWrit
46286 65 72 2c 20 20 20 20 20 20 20 2f 2a 20 57 72 69  er,       /* Wri
46287 74 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  ter object */.  
46288 46 74 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64  Fts3MultiSegRead
46289 65 72 20 2a 70 43 73 72 20 20 20 20 20 20 20 20  er *pCsr        
4628a 2f 2a 20 43 75 72 73 6f 72 20 63 6f 6e 74 61 69  /* Cursor contai
4628b 6e 69 6e 67 20 74 65 72 6d 20 61 6e 64 20 64 6f  ning term and do
4628c 63 6c 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f  clist */.){.  co
4628d 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 72 6d 20  nst char *zTerm 
4628e 3d 20 70 43 73 72 2d 3e 7a 54 65 72 6d 3b 0a 20  = pCsr->zTerm;. 
4628f 20 69 6e 74 20 6e 54 65 72 6d 20 3d 20 70 43 73   int nTerm = pCs
46290 72 2d 3e 6e 54 65 72 6d 3b 0a 20 20 63 6f 6e 73  r->nTerm;.  cons
46291 74 20 63 68 61 72 20 2a 61 44 6f 63 6c 69 73 74  t char *aDoclist
46292 20 3d 20 70 43 73 72 2d 3e 61 44 6f 63 6c 69 73   = pCsr->aDoclis
46293 74 3b 0a 20 20 69 6e 74 20 6e 44 6f 63 6c 69 73  t;.  int nDoclis
46294 74 20 3d 20 70 43 73 72 2d 3e 6e 44 6f 63 6c 69  t = pCsr->nDocli
46295 73 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  st;.  int rc = S
46296 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
46297 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
46298 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 70 61  de */.  int nSpa
46299 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ce;             
4629a 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73        /* Total s
4629b 70 61 63 65 20 69 6e 20 62 79 74 65 73 20 72 65  pace in bytes re
4629c 71 75 69 72 65 64 20 6f 6e 20 6c 65 61 66 20 2a  quired on leaf *
4629d 2f 0a 20 20 69 6e 74 20 6e 50 72 65 66 69 78 3b  /.  int nPrefix;
4629e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4629f 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 72 65    /* Size of pre
462a0 66 69 78 20 73 68 61 72 65 64 20 77 69 74 68 20  fix shared with 
462a1 70 72 65 76 69 6f 75 73 20 74 65 72 6d 20 2a 2f  previous term */
462a2 0a 20 20 69 6e 74 20 6e 53 75 66 66 69 78 3b 20  .  int nSuffix; 
462a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
462a4 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 75 66 66   /* Size of suff
462a5 69 78 20 28 6e 54 65 72 6d 20 2d 20 6e 50 72 65  ix (nTerm - nPre
462a6 66 69 78 29 20 2a 2f 0a 20 20 4e 6f 64 65 57 72  fix) */.  NodeWr
462a7 69 74 65 72 20 2a 70 4c 65 61 66 3b 20 20 20 20  iter *pLeaf;    
462a8 20 20 20 20 20 20 20 20 2f 2a 20 4f 62 6a 65 63          /* Objec
462a9 74 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 20  t used to write 
462aa 6c 65 61 66 20 6e 6f 64 65 73 20 2a 2f 0a 0a 20  leaf nodes */.. 
462ab 20 70 4c 65 61 66 20 3d 20 26 70 57 72 69 74 65   pLeaf = &pWrite
462ac 72 2d 3e 61 4e 6f 64 65 57 72 69 74 65 72 5b 30  r->aNodeWriter[0
462ad 5d 3b 0a 20 20 6e 50 72 65 66 69 78 20 3d 20 66  ];.  nPrefix = f
462ae 74 73 33 50 72 65 66 69 78 43 6f 6d 70 72 65 73  ts3PrefixCompres
462af 73 28 70 4c 65 61 66 2d 3e 6b 65 79 2e 61 2c 20  s(pLeaf->key.a, 
462b0 70 4c 65 61 66 2d 3e 6b 65 79 2e 6e 2c 20 7a 54  pLeaf->key.n, zT
462b1 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 6e  erm, nTerm);.  n
462b2 53 75 66 66 69 78 20 3d 20 6e 54 65 72 6d 20 2d  Suffix = nTerm -
462b3 20 6e 50 72 65 66 69 78 3b 0a 0a 20 20 6e 53 70   nPrefix;..  nSp
462b4 61 63 65 20 20 3d 20 73 71 6c 69 74 65 33 46 74  ace  = sqlite3Ft
462b5 73 33 56 61 72 69 6e 74 4c 65 6e 28 6e 50 72 65  s3VarintLen(nPre
462b6 66 69 78 29 3b 0a 20 20 6e 53 70 61 63 65 20 2b  fix);.  nSpace +
462b7 3d 20 73 71 6c 69 74 65 33 46 74 73 33 56 61 72  = sqlite3Fts3Var
462b8 69 6e 74 4c 65 6e 28 6e 53 75 66 66 69 78 29 20  intLen(nSuffix) 
462b9 2b 20 6e 53 75 66 66 69 78 3b 0a 20 20 6e 53 70  + nSuffix;.  nSp
462ba 61 63 65 20 2b 3d 20 73 71 6c 69 74 65 33 46 74  ace += sqlite3Ft
462bb 73 33 56 61 72 69 6e 74 4c 65 6e 28 6e 44 6f 63  s3VarintLen(nDoc
462bc 6c 69 73 74 29 20 2b 20 6e 44 6f 63 6c 69 73 74  list) + nDoclist
462bd 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63  ;..  /* If the c
462be 75 72 72 65 6e 74 20 62 6c 6f 63 6b 20 69 73 20  urrent block is 
462bf 6e 6f 74 20 65 6d 70 74 79 2c 20 61 6e 64 20 69  not empty, and i
462c0 66 20 61 64 64 69 6e 67 20 74 68 69 73 20 74 65  f adding this te
462c1 72 6d 2f 64 6f 63 6c 69 73 74 0a 20 20 2a 2a 20  rm/doclist.  ** 
462c2 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 62  to the current b
462c3 6c 6f 63 6b 20 77 6f 75 6c 64 20 6d 61 6b 65 20  lock would make 
462c4 69 74 20 6c 61 72 67 65 72 20 74 68 61 6e 20 46  it larger than F
462c5 74 73 33 54 61 62 6c 65 2e 6e 4e 6f 64 65 53 69  ts3Table.nNodeSi
462c6 7a 65 0a 20 20 2a 2a 20 62 79 74 65 73 2c 20 77  ze.  ** bytes, w
462c7 72 69 74 65 20 74 68 69 73 20 62 6c 6f 63 6b 20  rite this block 
462c8 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62  out to the datab
462c9 61 73 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 4c  ase. */.  if( pL
462ca 65 61 66 2d 3e 62 6c 6f 63 6b 2e 6e 3e 30 20 26  eaf->block.n>0 &
462cb 26 20 28 70 4c 65 61 66 2d 3e 62 6c 6f 63 6b 2e  & (pLeaf->block.
462cc 6e 20 2b 20 6e 53 70 61 63 65 29 3e 70 2d 3e 6e  n + nSpace)>p->n
462cd 4e 6f 64 65 53 69 7a 65 20 29 7b 0a 20 20 20 20  NodeSize ){.    
462ce 72 63 20 3d 20 66 74 73 33 57 72 69 74 65 53 65  rc = fts3WriteSe
462cf 67 6d 65 6e 74 28 70 2c 20 70 4c 65 61 66 2d 3e  gment(p, pLeaf->
462d0 69 42 6c 6f 63 6b 2c 20 70 4c 65 61 66 2d 3e 62  iBlock, pLeaf->b
462d1 6c 6f 63 6b 2e 61 2c 20 70 4c 65 61 66 2d 3e 62  lock.a, pLeaf->b
462d2 6c 6f 63 6b 2e 6e 29 3b 0a 20 20 20 20 70 57 72  lock.n);.    pWr
462d3 69 74 65 72 2d 3e 6e 57 6f 72 6b 2b 2b 3b 0a 0a  iter->nWork++;..
462d4 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 63      /* Add the c
462d5 75 72 72 65 6e 74 20 74 65 72 6d 20 74 6f 20 74  urrent term to t
462d6 68 65 20 70 61 72 65 6e 74 20 6e 6f 64 65 2e 20  he parent node. 
462d7 54 68 65 20 74 65 72 6d 20 61 64 64 65 64 20 74  The term added t
462d8 6f 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 70 61  o the .    ** pa
462d9 72 65 6e 74 20 6d 75 73 74 3a 0a 20 20 20 20 2a  rent must:.    *
462da 2a 0a 20 20 20 20 2a 2a 20 20 20 61 29 20 62 65  *.    **   a) be
462db 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
462dc 65 20 6c 61 72 67 65 73 74 20 74 65 72 6d 20 6f  e largest term o
462dd 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20  n the leaf node 
462de 6a 75 73 74 20 77 72 69 74 74 65 6e 0a 20 20 20  just written.   
462df 20 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20   **      to the 
462e0 64 61 74 61 62 61 73 65 20 28 73 74 69 6c 6c 20  database (still 
462e1 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 70 4c 65  available in pLe
462e2 61 66 2d 3e 6b 65 79 29 2c 20 61 6e 64 0a 20 20  af->key), and.  
462e3 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 62 29    **.    **   b)
462e4 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 6f 72   be less than or
462e5 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74 65   equal to the te
462e6 72 6d 20 61 62 6f 75 74 20 74 6f 20 62 65 20 61  rm about to be a
462e7 64 64 65 64 20 74 6f 20 74 68 65 20 6e 65 77 0a  dded to the new.
462e8 20 20 20 20 2a 2a 20 20 20 20 20 20 6c 65 61 66      **      leaf
462e9 20 6e 6f 64 65 20 28 7a 54 65 72 6d 2f 6e 54 65   node (zTerm/nTe
462ea 72 6d 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  rm)..    **.    
462eb 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  ** In other word
462ec 73 2c 20 69 74 20 6d 75 73 74 20 62 65 20 74 68  s, it must be th
462ed 65 20 70 72 65 66 69 78 20 6f 66 20 7a 54 65 72  e prefix of zTer
462ee 6d 20 31 20 62 79 74 65 20 6c 6f 6e 67 65 72 20  m 1 byte longer 
462ef 74 68 61 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20  than.    ** the 
462f0 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 20 28 69  common prefix (i
462f1 66 20 61 6e 79 29 20 6f 66 20 7a 54 65 72 6d 20  f any) of zTerm 
462f2 61 6e 64 20 70 57 72 69 74 65 72 2d 3e 7a 54 65  and pWriter->zTe
462f3 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  rm..    */.    i
462f4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
462f5 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66   ){.      rc = f
462f6 74 73 33 49 6e 63 72 6d 65 72 67 65 50 75 73 68  ts3IncrmergePush
462f7 28 70 2c 20 70 57 72 69 74 65 72 2c 20 7a 54 65  (p, pWriter, zTe
462f8 72 6d 2c 20 6e 50 72 65 66 69 78 2b 31 29 3b 0a  rm, nPrefix+1);.
462f9 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64      }..    /* Ad
462fa 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78  vance to the nex
462fb 74 20 6f 75 74 70 75 74 20 62 6c 6f 63 6b 20 2a  t output block *
462fc 2f 0a 20 20 20 20 70 4c 65 61 66 2d 3e 69 42 6c  /.    pLeaf->iBl
462fd 6f 63 6b 2b 2b 3b 0a 20 20 20 20 70 4c 65 61 66  ock++;.    pLeaf
462fe 2d 3e 6b 65 79 2e 6e 20 3d 20 30 3b 0a 20 20 20  ->key.n = 0;.   
462ff 20 70 4c 65 61 66 2d 3e 62 6c 6f 63 6b 2e 6e 20   pLeaf->block.n 
46300 3d 20 30 3b 0a 0a 20 20 20 20 6e 53 75 66 66 69  = 0;..    nSuffi
46301 78 20 3d 20 6e 54 65 72 6d 3b 0a 20 20 20 20 6e  x = nTerm;.    n
46302 53 70 61 63 65 20 20 3d 20 31 3b 0a 20 20 20 20  Space  = 1;.    
46303 6e 53 70 61 63 65 20 2b 3d 20 73 71 6c 69 74 65  nSpace += sqlite
46304 33 46 74 73 33 56 61 72 69 6e 74 4c 65 6e 28 6e  3Fts3VarintLen(n
46305 53 75 66 66 69 78 29 20 2b 20 6e 53 75 66 66 69  Suffix) + nSuffi
46306 78 3b 0a 20 20 20 20 6e 53 70 61 63 65 20 2b 3d  x;.    nSpace +=
46307 20 73 71 6c 69 74 65 33 46 74 73 33 56 61 72 69   sqlite3Fts3Vari
46308 6e 74 4c 65 6e 28 6e 44 6f 63 6c 69 73 74 29 20  ntLen(nDoclist) 
46309 2b 20 6e 44 6f 63 6c 69 73 74 3b 0a 20 20 7d 0a  + nDoclist;.  }.
4630a 0a 20 20 62 6c 6f 62 47 72 6f 77 42 75 66 66 65  .  blobGrowBuffe
4630b 72 28 26 70 4c 65 61 66 2d 3e 62 6c 6f 63 6b 2c  r(&pLeaf->block,
4630c 20 70 4c 65 61 66 2d 3e 62 6c 6f 63 6b 2e 6e 20   pLeaf->block.n 
4630d 2b 20 6e 53 70 61 63 65 2c 20 26 72 63 29 3b 0a  + nSpace, &rc);.
4630e 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
4630f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
46310 70 4c 65 61 66 2d 3e 62 6c 6f 63 6b 2e 6e 3d 3d  pLeaf->block.n==
46311 30 20 29 7b 0a 20 20 20 20 20 20 70 4c 65 61 66  0 ){.      pLeaf
46312 2d 3e 62 6c 6f 63 6b 2e 6e 20 3d 20 31 3b 0a 20  ->block.n = 1;. 
46313 20 20 20 20 20 70 4c 65 61 66 2d 3e 62 6c 6f 63       pLeaf->bloc
46314 6b 2e 61 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 20  k.a[0] = '\0';. 
46315 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 74     }.    rc = ft
46316 73 33 41 70 70 65 6e 64 54 6f 4e 6f 64 65 28 0a  s3AppendToNode(.
46317 20 20 20 20 20 20 20 20 26 70 4c 65 61 66 2d 3e          &pLeaf->
46318 62 6c 6f 63 6b 2c 20 26 70 4c 65 61 66 2d 3e 6b  block, &pLeaf->k
46319 65 79 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d  ey, zTerm, nTerm
4631a 2c 20 61 44 6f 63 6c 69 73 74 2c 20 6e 44 6f 63  , aDoclist, nDoc
4631b 6c 69 73 74 0a 20 20 20 20 29 3b 0a 20 20 7d 0a  list.    );.  }.
4631c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
4631d 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
4631e 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74  tion is called t
4631f 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 64 79  o release all dy
46320 6e 61 6d 69 63 20 72 65 73 6f 75 72 63 65 73 20  namic resources 
46321 68 65 6c 64 20 62 79 20 74 68 65 0a 2a 2a 20 6d  held by the.** m
46322 65 72 67 65 2d 77 72 69 74 65 72 20 6f 62 6a 65  erge-writer obje
46323 63 74 20 70 57 72 69 74 65 72 2c 20 61 6e 64 20  ct pWriter, and 
46324 69 66 20 6e 6f 20 65 72 72 6f 72 20 68 61 73 20  if no error has 
46325 6f 63 63 75 72 72 65 64 2c 20 74 6f 20 66 6c 75  occurred, to flu
46326 73 68 0a 2a 2a 20 61 6c 6c 20 6f 75 74 73 74 61  sh.** all outsta
46327 6e 64 69 6e 67 20 6e 6f 64 65 20 62 75 66 66 65  nding node buffe
46328 72 73 20 68 65 6c 64 20 62 79 20 70 57 72 69 74  rs held by pWrit
46329 65 72 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a  er to disk..**.*
4632a 2a 20 49 66 20 2a 70 52 63 20 69 73 20 6e 6f 74  * If *pRc is not
4632b 20 53 51 4c 49 54 45 5f 4f 4b 20 77 68 65 6e 20   SQLITE_OK when 
4632c 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
4632d 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 6e 6f   called, then no
4632e 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d   attempt.** is m
4632f 61 64 65 20 74 6f 20 77 72 69 74 65 20 61 6e 79  ade to write any
46330 20 64 61 74 61 20 74 6f 20 64 69 73 6b 2e 20 49   data to disk. I
46331 6e 73 74 65 61 64 2c 20 74 68 69 73 20 66 75 6e  nstead, this fun
46332 63 74 69 6f 6e 20 73 65 72 76 65 73 20 6f 6e 6c  ction serves onl
46333 79 0a 2a 2a 20 74 6f 20 72 65 6c 65 61 73 65 20  y.** to release 
46334 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 73 6f  outstanding reso
46335 75 72 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68  urces..**.** Oth
46336 65 72 77 69 73 65 2c 20 69 66 20 2a 70 52 63 20  erwise, if *pRc 
46337 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 53 51 4c  is initially SQL
46338 49 54 45 5f 4f 4b 20 61 6e 64 20 61 6e 20 65 72  ITE_OK and an er
46339 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65  ror occurs while
4633a 0a 2a 2a 20 66 6c 75 73 68 69 6e 67 20 62 75 66  .** flushing buf
4633b 66 65 72 73 20 74 6f 20 64 69 73 6b 2c 20 2a 70  fers to disk, *p
4633c 52 63 20 69 73 20 73 65 74 20 74 6f 20 61 6e 20  Rc is set to an 
4633d 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
4633e 65 20 62 65 66 6f 72 65 0a 2a 2a 20 72 65 74 75  e before.** retu
4633f 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  rning..*/.static
46340 20 76 6f 69 64 20 66 74 73 33 49 6e 63 72 6d 65   void fts3Incrme
46341 72 67 65 52 65 6c 65 61 73 65 28 0a 20 20 46 74  rgeRelease(.  Ft
46342 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20  s3Table *p,     
46343 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
46344 20 46 54 53 33 20 74 61 62 6c 65 20 68 61 6e 64   FTS3 table hand
46345 6c 65 20 2a 2f 0a 20 20 49 6e 63 72 6d 65 72 67  le */.  Incrmerg
46346 65 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  eWriter *pWriter
46347 2c 20 20 20 20 20 20 20 2f 2a 20 4d 65 72 67 65  ,       /* Merge
46348 2d 77 72 69 74 65 72 20 6f 62 6a 65 63 74 20 2a  -writer object *
46349 2f 0a 20 20 69 6e 74 20 2a 70 52 63 20 20 20 20  /.  int *pRc    
4634a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4634b 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 45      /* IN/OUT: E
4634c 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a 29 7b 0a  rror code */.){.
4634d 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
4634e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4634f 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65    /* Used to ite
46350 72 61 74 65 20 74 68 72 6f 75 67 68 20 6e 6f 6e  rate through non
46351 2d 72 6f 6f 74 20 6c 61 79 65 72 73 20 2a 2f 0a  -root layers */.
46352 20 20 69 6e 74 20 69 52 6f 6f 74 3b 20 20 20 20    int iRoot;    
46353 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46354 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 6f    /* Index of ro
46355 6f 74 20 69 6e 20 70 57 72 69 74 65 72 2d 3e 61  ot in pWriter->a
46356 4e 6f 64 65 57 72 69 74 65 72 20 2a 2f 0a 20 20  NodeWriter */.  
46357 4e 6f 64 65 57 72 69 74 65 72 20 2a 70 52 6f 6f  NodeWriter *pRoo
46358 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
46359 2f 2a 20 4e 6f 64 65 57 72 69 74 65 72 20 66 6f  /* NodeWriter fo
4635a 72 20 72 6f 6f 74 20 6e 6f 64 65 20 2a 2f 0a 20  r root node */. 
4635b 20 69 6e 74 20 72 63 20 3d 20 2a 70 52 63 3b 20   int rc = *pRc; 
4635c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4635d 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 2a   /* Error code *
4635e 2f 0a 0a 20 20 2f 2a 20 53 65 74 20 69 52 6f 6f  /..  /* Set iRoo
4635f 74 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 69  t to the index i
46360 6e 20 70 57 72 69 74 65 72 2d 3e 61 4e 6f 64 65  n pWriter->aNode
46361 57 72 69 74 65 72 5b 5d 20 6f 66 20 74 68 65 20  Writer[] of the 
46362 6f 75 74 70 75 74 20 73 65 67 6d 65 6e 74 20 0a  output segment .
46363 20 20 2a 2a 20 72 6f 6f 74 20 6e 6f 64 65 2e 20    ** root node. 
46364 49 66 20 74 68 65 20 73 65 67 6d 65 6e 74 20 66  If the segment f
46365 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20  its entirely on 
46366 61 20 73 69 6e 67 6c 65 20 6c 65 61 66 20 6e 6f  a single leaf no
46367 64 65 2c 20 69 52 6f 6f 74 0a 20 20 2a 2a 20 77  de, iRoot.  ** w
46368 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 30 2e  ill be set to 0.
46369 20 49 66 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64   If the root nod
4636a 65 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 20  e is the parent 
4636b 6f 66 20 74 68 65 20 6c 65 61 76 65 73 2c 20 69  of the leaves, i
4636c 52 6f 6f 74 0a 20 20 2a 2a 20 77 69 6c 6c 20 62  Root.  ** will b
4636d 65 20 31 2e 20 41 6e 64 20 73 6f 20 6f 6e 2e 20  e 1. And so on. 
4636e 20 2a 2f 0a 20 20 66 6f 72 28 69 52 6f 6f 74 3d   */.  for(iRoot=
4636f 46 54 53 5f 4d 41 58 5f 41 50 50 45 4e 44 41 42  FTS_MAX_APPENDAB
46370 4c 45 5f 48 45 49 47 48 54 2d 31 3b 20 69 52 6f  LE_HEIGHT-1; iRo
46371 6f 74 3e 3d 30 3b 20 69 52 6f 6f 74 2d 2d 29 7b  ot>=0; iRoot--){
46372 0a 20 20 20 20 4e 6f 64 65 57 72 69 74 65 72 20  .    NodeWriter 
46373 2a 70 4e 6f 64 65 20 3d 20 26 70 57 72 69 74 65  *pNode = &pWrite
46374 72 2d 3e 61 4e 6f 64 65 57 72 69 74 65 72 5b 69  r->aNodeWriter[i
46375 52 6f 6f 74 5d 3b 0a 20 20 20 20 69 66 28 20 70  Root];.    if( p
46376 4e 6f 64 65 2d 3e 62 6c 6f 63 6b 2e 6e 3e 30 20  Node->block.n>0 
46377 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 61 73 73  ) break;.    ass
46378 65 72 74 28 20 2a 70 52 63 20 7c 7c 20 70 4e 6f  ert( *pRc || pNo
46379 64 65 2d 3e 62 6c 6f 63 6b 2e 6e 41 6c 6c 6f 63  de->block.nAlloc
4637a 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==0 );.    asser
4637b 74 28 20 2a 70 52 63 20 7c 7c 20 70 4e 6f 64 65  t( *pRc || pNode
4637c 2d 3e 6b 65 79 2e 6e 41 6c 6c 6f 63 3d 3d 30 20  ->key.nAlloc==0 
4637d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
4637e 72 65 65 28 70 4e 6f 64 65 2d 3e 62 6c 6f 63 6b  ree(pNode->block
4637f 2e 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  .a);.    sqlite3
46380 5f 66 72 65 65 28 70 4e 6f 64 65 2d 3e 6b 65 79  _free(pNode->key
46381 2e 61 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45  .a);.  }..  /* E
46382 6d 70 74 79 20 6f 75 74 70 75 74 20 73 65 67 6d  mpty output segm
46383 65 6e 74 2e 20 54 68 69 73 20 69 73 20 61 20 6e  ent. This is a n
46384 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20 69  o-op. */.  if( i
46385 52 6f 6f 74 3c 30 20 29 20 72 65 74 75 72 6e 3b  Root<0 ) return;
46386 0a 0a 20 20 2f 2a 20 54 68 65 20 65 6e 74 69 72  ..  /* The entir
46387 65 20 6f 75 74 70 75 74 20 73 65 67 6d 65 6e 74  e output segment
46388 20 66 69 74 73 20 6f 6e 20 61 20 73 69 6e 67 6c   fits on a singl
46389 65 20 6e 6f 64 65 2e 20 4e 6f 72 6d 61 6c 6c 79  e node. Normally
4638a 2c 20 74 68 69 73 20 6d 65 61 6e 73 0a 20 20 2a  , this means.  *
4638b 2a 20 74 68 65 20 6e 6f 64 65 20 77 6f 75 6c 64  * the node would
4638c 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 61 20   be stored as a 
4638d 62 6c 6f 62 20 69 6e 20 74 68 65 20 22 72 6f 6f  blob in the "roo
4638e 74 22 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  t" column of the
4638f 20 25 5f 73 65 67 64 69 72 0a 20 20 2a 2a 20 74   %_segdir.  ** t
46390 61 62 6c 65 2e 20 48 6f 77 65 76 65 72 2c 20 74  able. However, t
46391 68 69 73 20 69 73 20 6e 6f 74 20 70 65 72 6d 69  his is not permi
46392 74 74 65 64 20 69 6e 20 74 68 69 73 20 63 61 73  tted in this cas
46393 65 2e 20 54 68 65 20 70 72 6f 62 6c 65 6d 20 69  e. The problem i
46394 73 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 70 61  s that .  ** spa
46395 63 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  ce has already b
46396 65 65 6e 20 72 65 73 65 72 76 65 64 20 69 6e 20  een reserved in 
46397 74 68 65 20 25 5f 73 65 67 6d 65 6e 74 73 20 74  the %_segments t
46398 61 62 6c 65 2c 20 61 6e 64 20 73 6f 20 74 68 65  able, and so the
46399 20 0a 20 20 2a 2a 20 73 74 61 72 74 5f 62 6c 6f   .  ** start_blo
4639a 63 6b 20 61 6e 64 20 65 6e 64 5f 62 6c 6f 63 6b  ck and end_block
4639b 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 25   fields of the %
4639c 5f 73 65 67 64 69 72 20 74 61 62 6c 65 20 6d 75  _segdir table mu
4639d 73 74 20 62 65 20 70 6f 70 75 6c 61 74 65 64 2e  st be populated.
4639e 20 0a 20 20 2a 2a 20 41 6e 64 2c 20 62 79 20 64   .  ** And, by d
4639f 65 73 69 67 6e 20 6f 72 20 62 79 20 61 63 63 69  esign or by acci
463a0 64 65 6e 74 2c 20 72 65 6c 65 61 73 65 64 20 76  dent, released v
463a1 65 72 73 69 6f 6e 73 20 6f 66 20 46 54 53 20 63  ersions of FTS c
463a2 61 6e 6e 6f 74 20 68 61 6e 64 6c 65 20 0a 20 20  annot handle .  
463a3 2a 2a 20 73 65 67 6d 65 6e 74 73 20 74 68 61 74  ** segments that
463a4 20 66 69 74 20 65 6e 74 69 72 65 6c 79 20 6f 6e   fit entirely on
463a5 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 77   the root node w
463a6 69 74 68 20 73 74 61 72 74 5f 62 6c 6f 63 6b 21  ith start_block!
463a7 3d 30 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e  =0..  **.  ** In
463a8 73 74 65 61 64 2c 20 63 72 65 61 74 65 20 61 20  stead, create a 
463a9 73 79 6e 74 68 65 74 69 63 20 72 6f 6f 74 20 6e  synthetic root n
463aa 6f 64 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ode that contain
463ab 73 20 6e 6f 74 68 69 6e 67 20 62 75 74 20 61 20  s nothing but a 
463ac 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f  .  ** pointer to
463ad 20 74 68 65 20 73 69 6e 67 6c 65 20 63 6f 6e 74   the single cont
463ae 65 6e 74 20 6e 6f 64 65 2e 20 53 6f 20 74 68 61  ent node. So tha
463af 74 20 74 68 65 20 73 65 67 6d 65 6e 74 20 63 6f  t the segment co
463b0 6e 73 69 73 74 73 20 6f 66 20 61 0a 20 20 2a 2a  nsists of a.  **
463b1 20 73 69 6e 67 6c 65 20 6c 65 61 66 20 61 6e 64   single leaf and
463b2 20 61 20 73 69 6e 67 6c 65 20 69 6e 74 65 72 69   a single interi
463b3 6f 72 20 28 72 6f 6f 74 29 20 6e 6f 64 65 2e 0a  or (root) node..
463b4 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 64 6f 3a 20    **.  ** Todo: 
463b5 42 65 74 74 65 72 20 6d 69 67 68 74 20 62 65 20  Better might be 
463b6 74 6f 20 64 65 66 65 72 20 61 6c 6c 6f 63 61 74  to defer allocat
463b7 69 6e 67 20 73 70 61 63 65 20 69 6e 20 74 68 65  ing space in the
463b8 20 25 5f 73 65 67 6d 65 6e 74 73 20 0a 20 20 2a   %_segments .  *
463b9 2a 20 74 61 62 6c 65 20 75 6e 74 69 6c 20 77 65  * table until we
463ba 20 61 72 65 20 73 75 72 65 20 69 74 20 69 73 20   are sure it is 
463bb 6e 65 65 64 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  needed..  */.  i
463bc 66 28 20 69 52 6f 6f 74 3d 3d 30 20 29 7b 0a 20  f( iRoot==0 ){. 
463bd 20 20 20 42 6c 6f 62 20 2a 70 42 6c 6f 63 6b 20     Blob *pBlock 
463be 3d 20 26 70 57 72 69 74 65 72 2d 3e 61 4e 6f 64  = &pWriter->aNod
463bf 65 57 72 69 74 65 72 5b 31 5d 2e 62 6c 6f 63 6b  eWriter[1].block
463c0 3b 0a 20 20 20 20 62 6c 6f 62 47 72 6f 77 42 75  ;.    blobGrowBu
463c1 66 66 65 72 28 70 42 6c 6f 63 6b 2c 20 31 20 2b  ffer(pBlock, 1 +
463c2 20 46 54 53 33 5f 56 41 52 49 4e 54 5f 4d 41 58   FTS3_VARINT_MAX
463c3 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20  , &rc);.    if( 
463c4 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
463c5 0a 20 20 20 20 20 20 70 42 6c 6f 63 6b 2d 3e 61  .      pBlock->a
463c6 5b 30 5d 20 3d 20 30 78 30 31 3b 0a 20 20 20 20  [0] = 0x01;.    
463c7 20 20 70 42 6c 6f 63 6b 2d 3e 6e 20 3d 20 31 20    pBlock->n = 1 
463c8 2b 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74  + sqlite3Fts3Put
463c9 56 61 72 69 6e 74 28 0a 20 20 20 20 20 20 20 20  Varint(.        
463ca 20 20 26 70 42 6c 6f 63 6b 2d 3e 61 5b 31 5d 2c    &pBlock->a[1],
463cb 20 70 57 72 69 74 65 72 2d 3e 61 4e 6f 64 65 57   pWriter->aNodeW
463cc 72 69 74 65 72 5b 30 5d 2e 69 42 6c 6f 63 6b 0a  riter[0].iBlock.
463cd 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20        );.    }. 
463ce 20 20 20 69 52 6f 6f 74 20 3d 20 31 3b 0a 20 20     iRoot = 1;.  
463cf 7d 0a 20 20 70 52 6f 6f 74 20 3d 20 26 70 57 72  }.  pRoot = &pWr
463d0 69 74 65 72 2d 3e 61 4e 6f 64 65 57 72 69 74 65  iter->aNodeWrite
463d1 72 5b 69 52 6f 6f 74 5d 3b 0a 0a 20 20 2f 2a 20  r[iRoot];..  /* 
463d2 46 6c 75 73 68 20 61 6c 6c 20 63 75 72 72 65 6e  Flush all curren
463d3 74 6c 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  tly outstanding 
463d4 6e 6f 64 65 73 20 74 6f 20 64 69 73 6b 2e 20 2a  nodes to disk. *
463d5 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 69  /.  for(i=0; i<i
463d6 52 6f 6f 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Root; i++){.    
463d7 4e 6f 64 65 57 72 69 74 65 72 20 2a 70 4e 6f 64  NodeWriter *pNod
463d8 65 20 3d 20 26 70 57 72 69 74 65 72 2d 3e 61 4e  e = &pWriter->aN
463d9 6f 64 65 57 72 69 74 65 72 5b 69 5d 3b 0a 20 20  odeWriter[i];.  
463da 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 62 6c 6f    if( pNode->blo
463db 63 6b 2e 6e 3e 30 20 26 26 20 72 63 3d 3d 53 51  ck.n>0 && rc==SQ
463dc 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
463dd 20 72 63 20 3d 20 66 74 73 33 57 72 69 74 65 53   rc = fts3WriteS
463de 65 67 6d 65 6e 74 28 70 2c 20 70 4e 6f 64 65 2d  egment(p, pNode-
463df 3e 69 42 6c 6f 63 6b 2c 20 70 4e 6f 64 65 2d 3e  >iBlock, pNode->
463e0 62 6c 6f 63 6b 2e 61 2c 20 70 4e 6f 64 65 2d 3e  block.a, pNode->
463e1 62 6c 6f 63 6b 2e 6e 29 3b 0a 20 20 20 20 7d 0a  block.n);.    }.
463e2 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
463e3 28 70 4e 6f 64 65 2d 3e 62 6c 6f 63 6b 2e 61 29  (pNode->block.a)
463e4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
463e5 65 65 28 70 4e 6f 64 65 2d 3e 6b 65 79 2e 61 29  ee(pNode->key.a)
463e6 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 74  ;.  }..  /* Writ
463e7 65 20 74 68 65 20 25 5f 73 65 67 64 69 72 20 72  e the %_segdir r
463e8 65 63 6f 72 64 2e 20 2a 2f 0a 20 20 69 66 28 20  ecord. */.  if( 
463e9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
463ea 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 57 72  .    rc = fts3Wr
463eb 69 74 65 53 65 67 64 69 72 28 70 2c 20 0a 20 20  iteSegdir(p, .  
463ec 20 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69        pWriter->i
463ed 41 62 73 4c 65 76 65 6c 2b 31 2c 20 20 20 20 20  AbsLevel+1,     
463ee 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6c 65 76            /* lev
463ef 65 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 57  el */.        pW
463f0 72 69 74 65 72 2d 3e 69 49 64 78 2c 20 20 20 20  riter->iIdx,    
463f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
463f2 20 20 2f 2a 20 69 64 78 20 2a 2f 0a 20 20 20 20    /* idx */.    
463f3 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 53 74      pWriter->iSt
463f4 61 72 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  art,            
463f5 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61 72 74          /* start
463f6 5f 62 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20  _block */.      
463f7 20 20 70 57 72 69 74 65 72 2d 3e 61 4e 6f 64 65    pWriter->aNode
463f8 57 72 69 74 65 72 5b 30 5d 2e 69 42 6c 6f 63 6b  Writer[0].iBlock
463f9 2c 20 20 20 20 20 2f 2a 20 6c 65 61 76 65 73 5f  ,     /* leaves_
463fa 65 6e 64 5f 62 6c 6f 63 6b 20 2a 2f 0a 20 20 20  end_block */.   
463fb 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 45       pWriter->iE
463fc 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nd,             
463fd 20 20 20 20 20 20 20 20 20 2f 2a 20 65 6e 64 5f           /* end_
463fe 62 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20  block */.       
463ff 20 70 52 6f 6f 74 2d 3e 62 6c 6f 63 6b 2e 61 2c   pRoot->block.a,
46400 20 70 52 6f 6f 74 2d 3e 62 6c 6f 63 6b 2e 6e 20   pRoot->block.n 
46401 20 20 20 20 20 2f 2a 20 72 6f 6f 74 20 2a 2f 0a       /* root */.
46402 20 20 20 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c      );.  }.  sql
46403 69 74 65 33 5f 66 72 65 65 28 70 52 6f 6f 74 2d  ite3_free(pRoot-
46404 3e 62 6c 6f 63 6b 2e 61 29 3b 0a 20 20 73 71 6c  >block.a);.  sql
46405 69 74 65 33 5f 66 72 65 65 28 70 52 6f 6f 74 2d  ite3_free(pRoot-
46406 3e 6b 65 79 2e 61 29 3b 0a 0a 20 20 2a 70 52 63  >key.a);..  *pRc
46407 20 3d 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   = rc;.}../*.** 
46408 43 6f 6d 70 61 72 65 20 74 68 65 20 74 65 72 6d  Compare the term
46409 20 69 6e 20 62 75 66 66 65 72 20 7a 4c 68 73 20   in buffer zLhs 
4640a 28 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6e  (size in bytes n
4640b 4c 68 73 29 20 77 69 74 68 20 74 68 61 74 20 69  Lhs) with that i
4640c 6e 0a 2a 2a 20 7a 52 68 73 20 28 73 69 7a 65 20  n.** zRhs (size 
4640d 69 6e 20 62 79 74 65 73 20 6e 52 68 73 29 20 75  in bytes nRhs) u
4640e 73 69 6e 67 20 6d 65 6d 63 6d 70 2e 20 49 66 20  sing memcmp. If 
4640f 6f 6e 65 20 74 65 72 6d 20 69 73 20 61 20 70 72  one term is a pr
46410 65 66 69 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6f  efix of.** the o
46411 74 68 65 72 2c 20 69 74 20 69 73 20 63 6f 6e 73  ther, it is cons
46412 69 64 65 72 65 64 20 74 6f 20 62 65 20 73 6d 61  idered to be sma
46413 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 6f 74  ller than the ot
46414 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  her..**.** Retur
46415 6e 20 2d 76 65 20 69 66 20 7a 4c 68 73 20 69 73  n -ve if zLhs is
46416 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 7a 52   smaller than zR
46417 68 73 2c 20 30 20 69 66 20 69 74 20 69 73 20 65  hs, 0 if it is e
46418 71 75 61 6c 2c 20 6f 72 20 2b 76 65 0a 2a 2a 20  qual, or +ve.** 
46419 69 66 20 69 74 20 69 73 20 67 72 65 61 74 65 72  if it is greater
4641a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
4641b 66 74 73 33 54 65 72 6d 43 6d 70 28 0a 20 20 63  fts3TermCmp(.  c
4641c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 68 73 2c  onst char *zLhs,
4641d 20 69 6e 74 20 6e 4c 68 73 2c 20 20 20 20 20 2f   int nLhs,     /
4641e 2a 20 4c 48 53 20 6f 66 20 63 6f 6d 70 61 72 69  * LHS of compari
4641f 73 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  son */.  const c
46420 68 61 72 20 2a 7a 52 68 73 2c 20 69 6e 74 20 6e  har *zRhs, int n
46421 52 68 73 20 20 20 20 20 20 2f 2a 20 52 48 53 20  Rhs      /* RHS 
46422 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f  of comparison */
46423 0a 29 7b 0a 20 20 69 6e 74 20 6e 43 6d 70 20 3d  .){.  int nCmp =
46424 20 4d 49 4e 28 6e 4c 68 73 2c 20 6e 52 68 73 29   MIN(nLhs, nRhs)
46425 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20  ;.  int res;..  
46426 72 65 73 20 3d 20 6d 65 6d 63 6d 70 28 7a 4c 68  res = memcmp(zLh
46427 73 2c 20 7a 52 68 73 2c 20 6e 43 6d 70 29 3b 0a  s, zRhs, nCmp);.
46428 20 20 69 66 28 20 72 65 73 3d 3d 30 20 29 20 72    if( res==0 ) r
46429 65 73 20 3d 20 6e 4c 68 73 20 2d 20 6e 52 68 73  es = nLhs - nRhs
4642a 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b  ;..  return res;
4642b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79  .}.../*.** Query
4642c 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 65   to see if the e
4642d 6e 74 72 79 20 69 6e 20 74 68 65 20 25 5f 73 65  ntry in the %_se
4642e 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 77 69 74  gments table wit
4642f 68 20 62 6c 6f 63 6b 69 64 20 69 45 6e 64 20 69  h blockid iEnd i
46430 73 20 0a 2a 2a 20 4e 55 4c 4c 2e 20 49 66 20 6e  s .** NULL. If n
46431 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61  o error occurs a
46432 6e 64 20 74 68 65 20 65 6e 74 72 79 20 69 73 20  nd the entry is 
46433 4e 55 4c 4c 2c 20 73 65 74 20 2a 70 62 52 65 73  NULL, set *pbRes
46434 20 31 20 62 65 66 6f 72 65 0a 2a 2a 20 72 65 74   1 before.** ret
46435 75 72 6e 69 6e 67 2e 20 4f 74 68 65 72 77 69 73  urning. Otherwis
46436 65 2c 20 73 65 74 20 2a 70 62 52 65 73 20 74 6f  e, set *pbRes to
46437 20 30 2e 20 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69   0. .**.** Or, i
46438 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
46439 73 20 77 68 69 6c 65 20 71 75 65 72 79 69 6e 67  s while querying
4643a 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 72   the database, r
4643b 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65 20  eturn an SQLite 
4643c 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 2e 20  .** error code. 
4643d 54 68 65 20 66 69 6e 61 6c 20 76 61 6c 75 65 20  The final value 
4643e 6f 66 20 2a 70 62 52 65 73 20 69 73 20 75 6e 64  of *pbRes is und
4643f 65 66 69 6e 65 64 20 69 6e 20 74 68 69 73 20 63  efined in this c
46440 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ase..**.** This 
46441 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 20  is used to test 
46442 69 66 20 61 20 73 65 67 6d 65 6e 74 20 69 73 20  if a segment is 
46443 61 6e 20 22 61 70 70 65 6e 64 61 62 6c 65 22 20  an "appendable" 
46444 73 65 67 6d 65 6e 74 2e 20 49 66 20 69 74 0a 2a  segment. If it.*
46445 2a 20 69 73 2c 20 74 68 65 6e 20 61 20 4e 55 4c  * is, then a NUL
46446 4c 20 65 6e 74 72 79 20 68 61 73 20 62 65 65 6e  L entry has been
46447 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74   inserted into t
46448 68 65 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61  he %_segments ta
46449 62 6c 65 0a 2a 2a 20 77 69 74 68 20 62 6c 6f 63  ble.** with bloc
4644a 6b 69 64 20 25 5f 73 65 67 64 69 72 2e 65 6e 64  kid %_segdir.end
4644b 5f 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69  _block..*/.stati
4644c 63 20 69 6e 74 20 66 74 73 33 49 73 41 70 70 65  c int fts3IsAppe
4644d 6e 64 61 62 6c 65 28 46 74 73 33 54 61 62 6c 65  ndable(Fts3Table
4644e 20 2a 70 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74   *p, sqlite3_int
4644f 36 34 20 69 45 6e 64 2c 20 69 6e 74 20 2a 70 62  64 iEnd, int *pb
46450 52 65 73 29 7b 0a 20 20 69 6e 74 20 62 52 65 73  Res){.  int bRes
46451 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
46452 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
46453 74 20 74 6f 20 73 65 74 20 2a 70 62 52 65 73 20  t to set *pbRes 
46454 74 6f 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  to */.  sqlite3_
46455 73 74 6d 74 20 2a 70 43 68 65 63 6b 20 3d 20 30  stmt *pCheck = 0
46456 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65  ;       /* State
46457 6d 65 6e 74 20 74 6f 20 71 75 65 72 79 20 64 61  ment to query da
46458 74 61 62 61 73 65 20 77 69 74 68 20 2a 2f 0a 20  tabase with */. 
46459 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
4645a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4645b 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
4645c 2a 2f 0a 0a 20 20 72 63 20 3d 20 66 74 73 33 53  */..  rc = fts3S
4645d 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 53 45  qlStmt(p, SQL_SE
4645e 47 4d 45 4e 54 5f 49 53 5f 41 50 50 45 4e 44 41  GMENT_IS_APPENDA
4645f 42 4c 45 2c 20 26 70 43 68 65 63 6b 2c 20 30 29  BLE, &pCheck, 0)
46460 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
46461 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
46462 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
46463 70 43 68 65 63 6b 2c 20 31 2c 20 69 45 6e 64 29  pCheck, 1, iEnd)
46464 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  ;.    if( SQLITE
46465 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74  _ROW==sqlite3_st
46466 65 70 28 70 43 68 65 63 6b 29 20 29 20 62 52 65  ep(pCheck) ) bRe
46467 73 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20  s = 1;.    rc = 
46468 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 43  sqlite3_reset(pC
46469 68 65 63 6b 29 3b 0a 20 20 7d 0a 20 20 0a 20 20  heck);.  }.  .  
4646a 2a 70 62 52 65 73 20 3d 20 62 52 65 73 3b 0a 20  *pbRes = bRes;. 
4646b 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
4646c 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
4646d 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  on is called whe
4646e 6e 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61  n initializing a
4646f 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 6d 65  n incremental-me
46470 72 67 65 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a  rge operation..*
46471 2a 20 49 74 20 63 68 65 63 6b 73 20 69 66 20 74  * It checks if t
46472 68 65 20 65 78 69 73 74 69 6e 67 20 73 65 67 6d  he existing segm
46473 65 6e 74 20 77 69 74 68 20 69 6e 64 65 78 20 76  ent with index v
46474 61 6c 75 65 20 69 49 64 78 20 61 74 20 61 62 73  alue iIdx at abs
46475 6f 6c 75 74 65 20 6c 65 76 65 6c 20 0a 2a 2a 20  olute level .** 
46476 28 69 41 62 73 4c 65 76 65 6c 2b 31 29 20 63 61  (iAbsLevel+1) ca
46477 6e 20 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f  n be appended to
46478 20 62 79 20 74 68 65 20 69 6e 63 72 65 6d 65 6e   by the incremen
46479 74 61 6c 20 6d 65 72 67 65 2e 20 49 66 20 69 74  tal merge. If it
4647a 20 63 61 6e 2c 20 74 68 65 0a 2a 2a 20 6d 65 72   can, the.** mer
4647b 67 65 2d 77 72 69 74 65 72 20 6f 62 6a 65 63 74  ge-writer object
4647c 20 2a 70 57 72 69 74 65 72 20 69 73 20 69 6e 69   *pWriter is ini
4647d 74 69 61 6c 69 7a 65 64 20 74 6f 20 77 72 69 74  tialized to writ
4647e 65 20 74 6f 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 41  e to it..**.** A
4647f 6e 20 65 78 69 73 74 69 6e 67 20 73 65 67 6d 65  n existing segme
46480 6e 74 20 63 61 6e 20 62 65 20 61 70 70 65 6e 64  nt can be append
46481 65 64 20 74 6f 20 62 79 20 61 6e 20 69 6e 63 72  ed to by an incr
46482 65 6d 65 6e 74 61 6c 20 6d 65 72 67 65 20 69 66  emental merge if
46483 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 74 20 77  :.**.**   * It w
46484 61 73 20 69 6e 69 74 69 61 6c 6c 79 20 63 72 65  as initially cre
46485 61 74 65 64 20 61 73 20 61 6e 20 61 70 70 65 6e  ated as an appen
46486 64 61 62 6c 65 20 73 65 67 6d 65 6e 74 20 28 77  dable segment (w
46487 69 74 68 20 61 6c 6c 20 72 65 71 75 69 72 65 64  ith all required
46488 0a 2a 2a 20 20 20 20 20 73 70 61 63 65 20 70 72  .**     space pr
46489 65 2d 61 6c 6c 6f 63 61 74 65 64 29 2c 20 61 6e  e-allocated), an
4648a 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20  d.**.**   * The 
4648b 66 69 72 73 74 20 6b 65 79 20 72 65 61 64 20 66  first key read f
4648c 72 6f 6d 20 74 68 65 20 69 6e 70 75 74 20 28 61  rom the input (a
4648d 72 67 75 6d 65 6e 74 73 20 7a 4b 65 79 20 61 6e  rguments zKey an
4648e 64 20 6e 4b 65 79 29 20 69 73 20 0a 2a 2a 20 20  d nKey) is .**  
4648f 20 20 20 67 72 65 61 74 65 72 20 74 68 61 6e 20     greater than 
46490 74 68 65 20 6c 61 72 67 65 73 74 20 6b 65 79 20  the largest key 
46491 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64  currently stored
46492 20 69 6e 20 74 68 65 20 70 6f 74 65 6e 74 69 61   in the potentia
46493 6c 0a 2a 2a 20 20 20 20 20 6f 75 74 70 75 74 20  l.**     output 
46494 73 65 67 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74  segment..*/.stat
46495 69 63 20 69 6e 74 20 66 74 73 33 49 6e 63 72 6d  ic int fts3Incrm
46496 65 72 67 65 4c 6f 61 64 28 0a 20 20 46 74 73 33  ergeLoad(.  Fts3
46497 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20  Table *p,       
46498 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
46499 74 73 33 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ts3 table handle
4649a 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
4649b 74 36 34 20 69 41 62 73 4c 65 76 65 6c 2c 20 20  t64 iAbsLevel,  
4649c 20 20 20 20 20 20 2f 2a 20 41 62 73 6f 6c 75 74        /* Absolut
4649d 65 20 6c 65 76 65 6c 20 6f 66 20 69 6e 70 75 74  e level of input
4649e 20 73 65 67 6d 65 6e 74 73 20 2a 2f 0a 20 20 69   segments */.  i
4649f 6e 74 20 69 49 64 78 2c 20 20 20 20 20 20 20 20  nt iIdx,        
464a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
464a1 2a 20 49 6e 64 65 78 20 6f 66 20 63 61 6e 64 69  * Index of candi
464a2 64 61 74 65 20 6f 75 74 70 75 74 20 73 65 67 6d  date output segm
464a3 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ent */.  const c
464a4 68 61 72 20 2a 7a 4b 65 79 2c 20 20 20 20 20 20  har *zKey,      
464a5 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
464a6 74 20 6b 65 79 20 74 6f 20 77 72 69 74 65 20 2a  t key to write *
464a7 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20  /.  int nKey,   
464a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
464a9 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
464aa 20 62 79 74 65 73 20 69 6e 20 6e 4b 65 79 20 2a   bytes in nKey *
464ab 2f 0a 20 20 49 6e 63 72 6d 65 72 67 65 57 72 69  /.  IncrmergeWri
464ac 74 65 72 20 2a 70 57 72 69 74 65 72 20 20 20 20  ter *pWriter    
464ad 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20      /* Populate 
464ae 74 68 69 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 29  this object */.)
464af 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
464b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
464b1 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
464b2 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  de */.  sqlite3_
464b3 73 74 6d 74 20 2a 70 53 65 6c 65 63 74 20 3d 20  stmt *pSelect = 
464b4 30 3b 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43  0;      /* SELEC
464b5 54 20 74 6f 20 72 65 61 64 20 25 5f 73 65 67 64  T to read %_segd
464b6 69 72 20 65 6e 74 72 79 20 2a 2f 0a 0a 20 20 72  ir entry */..  r
464b7 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28  c = fts3SqlStmt(
464b8 70 2c 20 53 51 4c 5f 53 45 4c 45 43 54 5f 53 45  p, SQL_SELECT_SE
464b9 47 44 49 52 2c 20 26 70 53 65 6c 65 63 74 2c 20  GDIR, &pSelect, 
464ba 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  0);.  if( rc==SQ
464bb 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
464bc 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 53 74  qlite3_int64 iSt
464bd 61 72 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  art = 0;     /* 
464be 56 61 6c 75 65 20 6f 66 20 25 5f 73 65 67 64 69  Value of %_segdi
464bf 72 2e 73 74 61 72 74 5f 62 6c 6f 63 6b 20 2a 2f  r.start_block */
464c0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  .    sqlite3_int
464c1 36 34 20 69 4c 65 61 66 45 6e 64 20 3d 20 30 3b  64 iLeafEnd = 0;
464c2 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 25     /* Value of %
464c3 5f 73 65 67 64 69 72 2e 6c 65 61 76 65 73 5f 65  _segdir.leaves_e
464c4 6e 64 5f 62 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  nd_block */.    
464c5 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 45  sqlite3_int64 iE
464c6 6e 64 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  nd = 0;       /*
464c7 20 56 61 6c 75 65 20 6f 66 20 25 5f 73 65 67 64   Value of %_segd
464c8 69 72 2e 65 6e 64 5f 62 6c 6f 63 6b 20 2a 2f 0a  ir.end_block */.
464c9 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
464ca 61 52 6f 6f 74 20 3d 20 30 3b 20 20 20 20 20 20  aRoot = 0;      
464cb 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
464cc 25 5f 73 65 67 64 69 72 2e 72 6f 6f 74 20 62 75  %_segdir.root bu
464cd 66 66 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ffer */.    int 
464ce 6e 52 6f 6f 74 20 3d 20 30 3b 20 20 20 20 20 20  nRoot = 0;      
464cf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
464d0 65 20 6f 66 20 61 52 6f 6f 74 5b 5d 20 69 6e 20  e of aRoot[] in 
464d1 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74  bytes */.    int
464d2 20 72 63 32 3b 20 20 20 20 20 20 20 20 20 20 20   rc2;           
464d3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
464d4 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73  turn code from s
464d5 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 2a  qlite3_reset() *
464d6 2f 0a 20 20 20 20 69 6e 74 20 62 41 70 70 65 6e  /.    int bAppen
464d7 64 61 62 6c 65 20 3d 20 30 3b 20 20 20 20 20 20  dable = 0;      
464d8 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 74 72      /* Set to tr
464d9 75 65 20 69 66 20 73 65 67 6d 65 6e 74 20 69 73  ue if segment is
464da 20 61 70 70 65 6e 64 61 62 6c 65 20 2a 2f 0a 0a   appendable */..
464db 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20      /* Read the 
464dc 25 5f 73 65 67 64 69 72 20 65 6e 74 72 79 20 66  %_segdir entry f
464dd 6f 72 20 69 6e 64 65 78 20 69 49 64 78 20 61 62  or index iIdx ab
464de 73 6f 6c 75 74 65 20 6c 65 76 65 6c 20 28 69 41  solute level (iA
464df 62 73 4c 65 76 65 6c 2b 31 29 20 2a 2f 0a 20 20  bsLevel+1) */.  
464e0 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
464e1 6e 74 36 34 28 70 53 65 6c 65 63 74 2c 20 31 2c  nt64(pSelect, 1,
464e2 20 69 41 62 73 4c 65 76 65 6c 2b 31 29 3b 0a 20   iAbsLevel+1);. 
464e3 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
464e4 69 6e 74 28 70 53 65 6c 65 63 74 2c 20 32 2c 20  int(pSelect, 2, 
464e5 69 49 64 78 29 3b 0a 20 20 20 20 69 66 28 20 73  iIdx);.    if( s
464e6 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 65 6c  qlite3_step(pSel
464e7 65 63 74 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ect)==SQLITE_ROW
464e8 20 29 7b 0a 20 20 20 20 20 20 69 53 74 61 72 74   ){.      iStart
464e9 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
464ea 6e 5f 69 6e 74 36 34 28 70 53 65 6c 65 63 74 2c  n_int64(pSelect,
464eb 20 31 29 3b 0a 20 20 20 20 20 20 69 4c 65 61 66   1);.      iLeaf
464ec 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  End = sqlite3_co
464ed 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 53 65 6c 65  lumn_int64(pSele
464ee 63 74 2c 20 32 29 3b 0a 20 20 20 20 20 20 69 45  ct, 2);.      iE
464ef 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  nd = sqlite3_col
464f0 75 6d 6e 5f 69 6e 74 36 34 28 70 53 65 6c 65 63  umn_int64(pSelec
464f1 74 2c 20 33 29 3b 0a 20 20 20 20 20 20 6e 52 6f  t, 3);.      nRo
464f2 6f 74 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ot = sqlite3_col
464f3 75 6d 6e 5f 62 79 74 65 73 28 70 53 65 6c 65 63  umn_bytes(pSelec
464f4 74 2c 20 34 29 3b 0a 20 20 20 20 20 20 61 52 6f  t, 4);.      aRo
464f5 6f 74 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ot = sqlite3_col
464f6 75 6d 6e 5f 62 6c 6f 62 28 70 53 65 6c 65 63 74  umn_blob(pSelect
464f7 2c 20 34 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , 4);.    }else{
464f8 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 71  .      return sq
464f9 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 65 6c  lite3_reset(pSel
464fa 65 63 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ect);.    }..   
464fb 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 74 68   /* Check for th
464fc 65 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 6d 61  e zero-length ma
464fd 72 6b 65 72 20 69 6e 20 74 68 65 20 25 5f 73 65  rker in the %_se
464fe 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 2a 2f 0a  gments table */.
464ff 20 20 20 20 72 63 20 3d 20 66 74 73 33 49 73 41      rc = fts3IsA
46500 70 70 65 6e 64 61 62 6c 65 28 70 2c 20 69 45 6e  ppendable(p, iEn
46501 64 2c 20 26 62 41 70 70 65 6e 64 61 62 6c 65 29  d, &bAppendable)
46502 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  ;..    /* Check 
46503 74 68 61 74 20 7a 4b 65 79 2f 6e 4b 65 79 20 69  that zKey/nKey i
46504 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  s larger than th
46505 65 20 6c 61 72 67 65 73 74 20 6b 65 79 20 74 68  e largest key th
46506 65 20 63 61 6e 64 69 64 61 74 65 20 2a 2f 0a 20  e candidate */. 
46507 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
46508 45 5f 4f 4b 20 26 26 20 62 41 70 70 65 6e 64 61  E_OK && bAppenda
46509 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 63 68 61  ble ){.      cha
4650a 72 20 2a 61 4c 65 61 66 20 3d 20 30 3b 0a 20 20  r *aLeaf = 0;.  
4650b 20 20 20 20 69 6e 74 20 6e 4c 65 61 66 20 3d 20      int nLeaf = 
4650c 30 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 73  0;..      rc = s
4650d 71 6c 69 74 65 33 46 74 73 33 52 65 61 64 42 6c  qlite3Fts3ReadBl
4650e 6f 63 6b 28 70 2c 20 69 4c 65 61 66 45 6e 64 2c  ock(p, iLeafEnd,
4650f 20 26 61 4c 65 61 66 2c 20 26 6e 4c 65 61 66 2c   &aLeaf, &nLeaf,
46510 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
46511 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
46512 20 20 20 20 20 20 20 20 4e 6f 64 65 52 65 61 64          NodeRead
46513 65 72 20 72 65 61 64 65 72 3b 0a 20 20 20 20 20  er reader;.     
46514 20 20 20 66 6f 72 28 72 63 20 3d 20 6e 6f 64 65     for(rc = node
46515 52 65 61 64 65 72 49 6e 69 74 28 26 72 65 61 64  ReaderInit(&read
46516 65 72 2c 20 61 4c 65 61 66 2c 20 6e 4c 65 61 66  er, aLeaf, nLeaf
46517 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  );.            r
46518 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
46519 72 65 61 64 65 72 2e 61 4e 6f 64 65 3b 0a 20 20  reader.aNode;.  
4651a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e            rc = n
4651b 6f 64 65 52 65 61 64 65 72 4e 65 78 74 28 26 72  odeReaderNext(&r
4651c 65 61 64 65 72 29 0a 20 20 20 20 20 20 20 20 29  eader).        )
4651d 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
4651e 72 74 28 20 72 65 61 64 65 72 2e 61 4e 6f 64 65  rt( reader.aNode
4651f 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   );.        }.  
46520 20 20 20 20 20 20 69 66 28 20 66 74 73 33 54 65        if( fts3Te
46521 72 6d 43 6d 70 28 7a 4b 65 79 2c 20 6e 4b 65 79  rmCmp(zKey, nKey
46522 2c 20 72 65 61 64 65 72 2e 74 65 72 6d 2e 61 2c  , reader.term.a,
46523 20 72 65 61 64 65 72 2e 74 65 72 6d 2e 6e 29 3c   reader.term.n)<
46524 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
46525 62 41 70 70 65 6e 64 61 62 6c 65 20 3d 20 30 3b  bAppendable = 0;
46526 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
46527 20 20 20 6e 6f 64 65 52 65 61 64 65 72 52 65 6c     nodeReaderRel
46528 65 61 73 65 28 26 72 65 61 64 65 72 29 3b 0a 20  ease(&reader);. 
46529 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
4652a 69 74 65 33 5f 66 72 65 65 28 61 4c 65 61 66 29  ite3_free(aLeaf)
4652b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
4652c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
4652d 26 20 62 41 70 70 65 6e 64 61 62 6c 65 20 29 7b  & bAppendable ){
4652e 0a 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20  .      /* It is 
4652f 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 70 70 65  possible to appe
46530 6e 64 20 74 6f 20 74 68 69 73 20 73 65 67 6d 65  nd to this segme
46531 6e 74 2e 20 53 65 74 20 75 70 20 74 68 65 20 49  nt. Set up the I
46532 6e 63 72 6d 65 72 67 65 57 72 69 74 65 72 0a 20  ncrmergeWriter. 
46533 20 20 20 20 20 2a 2a 20 6f 62 6a 65 63 74 20 74       ** object t
46534 6f 20 64 6f 20 73 6f 2e 20 20 2a 2f 0a 20 20 20  o do so.  */.   
46535 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
46536 69 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 28 69  int nHeight = (i
46537 6e 74 29 61 52 6f 6f 74 5b 30 5d 3b 0a 20 20 20  nt)aRoot[0];.   
46538 20 20 20 4e 6f 64 65 57 72 69 74 65 72 20 2a 70     NodeWriter *p
46539 4e 6f 64 65 3b 0a 0a 20 20 20 20 20 20 70 57 72  Node;..      pWr
4653a 69 74 65 72 2d 3e 6e 4c 65 61 66 45 73 74 20 3d  iter->nLeafEst =
4653b 20 28 69 6e 74 29 28 28 69 45 6e 64 20 2d 20 69   (int)((iEnd - i
4653c 53 74 61 72 74 29 20 2b 20 31 29 2f 46 54 53 5f  Start) + 1)/FTS_
4653d 4d 41 58 5f 41 50 50 45 4e 44 41 42 4c 45 5f 48  MAX_APPENDABLE_H
4653e 45 49 47 48 54 3b 0a 20 20 20 20 20 20 70 57 72  EIGHT;.      pWr
4653f 69 74 65 72 2d 3e 69 53 74 61 72 74 20 3d 20 69  iter->iStart = i
46540 53 74 61 72 74 3b 0a 20 20 20 20 20 20 70 57 72  Start;.      pWr
46541 69 74 65 72 2d 3e 69 45 6e 64 20 3d 20 69 45 6e  iter->iEnd = iEn
46542 64 3b 0a 20 20 20 20 20 20 70 57 72 69 74 65 72  d;.      pWriter
46543 2d 3e 69 41 62 73 4c 65 76 65 6c 20 3d 20 69 41  ->iAbsLevel = iA
46544 62 73 4c 65 76 65 6c 3b 0a 20 20 20 20 20 20 70  bsLevel;.      p
46545 57 72 69 74 65 72 2d 3e 69 49 64 78 20 3d 20 69  Writer->iIdx = i
46546 49 64 78 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28  Idx;..      for(
46547 69 3d 6e 48 65 69 67 68 74 2b 31 3b 20 69 3c 46  i=nHeight+1; i<F
46548 54 53 5f 4d 41 58 5f 41 50 50 45 4e 44 41 42 4c  TS_MAX_APPENDABL
46549 45 5f 48 45 49 47 48 54 3b 20 69 2b 2b 29 7b 0a  E_HEIGHT; i++){.
4654a 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d          pWriter-
4654b 3e 61 4e 6f 64 65 57 72 69 74 65 72 5b 69 5d 2e  >aNodeWriter[i].
4654c 69 42 6c 6f 63 6b 20 3d 20 70 57 72 69 74 65 72  iBlock = pWriter
4654d 2d 3e 69 53 74 61 72 74 20 2b 20 69 2a 70 57 72  ->iStart + i*pWr
4654e 69 74 65 72 2d 3e 6e 4c 65 61 66 45 73 74 3b 0a  iter->nLeafEst;.
4654f 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 70        }..      p
46550 4e 6f 64 65 20 3d 20 26 70 57 72 69 74 65 72 2d  Node = &pWriter-
46551 3e 61 4e 6f 64 65 57 72 69 74 65 72 5b 6e 48 65  >aNodeWriter[nHe
46552 69 67 68 74 5d 3b 0a 20 20 20 20 20 20 70 4e 6f  ight];.      pNo
46553 64 65 2d 3e 69 42 6c 6f 63 6b 20 3d 20 70 57 72  de->iBlock = pWr
46554 69 74 65 72 2d 3e 69 53 74 61 72 74 20 2b 20 70  iter->iStart + p
46555 57 72 69 74 65 72 2d 3e 6e 4c 65 61 66 45 73 74  Writer->nLeafEst
46556 2a 6e 48 65 69 67 68 74 3b 0a 20 20 20 20 20 20  *nHeight;.      
46557 62 6c 6f 62 47 72 6f 77 42 75 66 66 65 72 28 26  blobGrowBuffer(&
46558 70 4e 6f 64 65 2d 3e 62 6c 6f 63 6b 2c 20 4d 41  pNode->block, MA
46559 58 28 6e 52 6f 6f 74 2c 20 70 2d 3e 6e 4e 6f 64  X(nRoot, p->nNod
4655a 65 53 69 7a 65 29 2c 20 26 72 63 29 3b 0a 20 20  eSize), &rc);.  
4655b 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
4655c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
4655d 20 6d 65 6d 63 70 79 28 70 4e 6f 64 65 2d 3e 62   memcpy(pNode->b
4655e 6c 6f 63 6b 2e 61 2c 20 61 52 6f 6f 74 2c 20 6e  lock.a, aRoot, n
4655f 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 70  Root);.        p
46560 4e 6f 64 65 2d 3e 62 6c 6f 63 6b 2e 6e 20 3d 20  Node->block.n = 
46561 6e 52 6f 6f 74 3b 0a 20 20 20 20 20 20 7d 0a 0a  nRoot;.      }..
46562 20 20 20 20 20 20 66 6f 72 28 69 3d 6e 48 65 69        for(i=nHei
46563 67 68 74 3b 20 69 3e 3d 30 20 26 26 20 72 63 3d  ght; i>=0 && rc=
46564 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 2d 2d 29  =SQLITE_OK; i--)
46565 7b 0a 20 20 20 20 20 20 20 20 4e 6f 64 65 52 65  {.        NodeRe
46566 61 64 65 72 20 72 65 61 64 65 72 3b 0a 20 20 20  ader reader;.   
46567 20 20 20 20 20 70 4e 6f 64 65 20 3d 20 26 70 57       pNode = &pW
46568 72 69 74 65 72 2d 3e 61 4e 6f 64 65 57 72 69 74  riter->aNodeWrit
46569 65 72 5b 69 5d 3b 0a 0a 20 20 20 20 20 20 20 20  er[i];..        
4656a 72 63 20 3d 20 6e 6f 64 65 52 65 61 64 65 72 49  rc = nodeReaderI
4656b 6e 69 74 28 26 72 65 61 64 65 72 2c 20 70 4e 6f  nit(&reader, pNo
4656c 64 65 2d 3e 62 6c 6f 63 6b 2e 61 2c 20 70 4e 6f  de->block.a, pNo
4656d 64 65 2d 3e 62 6c 6f 63 6b 2e 6e 29 3b 0a 20 20  de->block.n);.  
4656e 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61        while( rea
4656f 64 65 72 2e 61 4e 6f 64 65 20 26 26 20 72 63 3d  der.aNode && rc=
46570 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20  =SQLITE_OK ) rc 
46571 3d 20 6e 6f 64 65 52 65 61 64 65 72 4e 65 78 74  = nodeReaderNext
46572 28 26 72 65 61 64 65 72 29 3b 0a 20 20 20 20 20  (&reader);.     
46573 20 20 20 62 6c 6f 62 47 72 6f 77 42 75 66 66 65     blobGrowBuffe
46574 72 28 26 70 4e 6f 64 65 2d 3e 6b 65 79 2c 20 72  r(&pNode->key, r
46575 65 61 64 65 72 2e 74 65 72 6d 2e 6e 2c 20 26 72  eader.term.n, &r
46576 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  c);.        if( 
46577 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
46578 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
46579 79 28 70 4e 6f 64 65 2d 3e 6b 65 79 2e 61 2c 20  y(pNode->key.a, 
4657a 72 65 61 64 65 72 2e 74 65 72 6d 2e 61 2c 20 72  reader.term.a, r
4657b 65 61 64 65 72 2e 74 65 72 6d 2e 6e 29 3b 0a 20  eader.term.n);. 
4657c 20 20 20 20 20 20 20 20 20 70 4e 6f 64 65 2d 3e           pNode->
4657d 6b 65 79 2e 6e 20 3d 20 72 65 61 64 65 72 2e 74  key.n = reader.t
4657e 65 72 6d 2e 6e 3b 0a 20 20 20 20 20 20 20 20 20  erm.n;.         
4657f 20 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 20   if( i>0 ){.    
46580 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 61 42          char *aB
46581 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20  lock = 0;.      
46582 20 20 20 20 20 20 69 6e 74 20 6e 42 6c 6f 63 6b        int nBlock
46583 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
46584 20 20 70 4e 6f 64 65 20 3d 20 26 70 57 72 69 74    pNode = &pWrit
46585 65 72 2d 3e 61 4e 6f 64 65 57 72 69 74 65 72 5b  er->aNodeWriter[
46586 69 2d 31 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  i-1];.          
46587 20 20 70 4e 6f 64 65 2d 3e 69 42 6c 6f 63 6b 20    pNode->iBlock 
46588 3d 20 72 65 61 64 65 72 2e 69 43 68 69 6c 64 3b  = reader.iChild;
46589 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
4658a 3d 20 73 71 6c 69 74 65 33 46 74 73 33 52 65 61  = sqlite3Fts3Rea
4658b 64 42 6c 6f 63 6b 28 70 2c 20 72 65 61 64 65 72  dBlock(p, reader
4658c 2e 69 43 68 69 6c 64 2c 20 26 61 42 6c 6f 63 6b  .iChild, &aBlock
4658d 2c 20 26 6e 42 6c 6f 63 6b 2c 20 30 29 3b 0a 20  , &nBlock, 0);. 
4658e 20 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62 47             blobG
4658f 72 6f 77 42 75 66 66 65 72 28 26 70 4e 6f 64 65  rowBuffer(&pNode
46590 2d 3e 62 6c 6f 63 6b 2c 20 4d 41 58 28 6e 42 6c  ->block, MAX(nBl
46591 6f 63 6b 2c 20 70 2d 3e 6e 4e 6f 64 65 53 69 7a  ock, p->nNodeSiz
46592 65 29 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20  e), &rc);.      
46593 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
46594 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
46595 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
46596 70 4e 6f 64 65 2d 3e 62 6c 6f 63 6b 2e 61 2c 20  pNode->block.a, 
46597 61 42 6c 6f 63 6b 2c 20 6e 42 6c 6f 63 6b 29 3b  aBlock, nBlock);
46598 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
46599 4e 6f 64 65 2d 3e 62 6c 6f 63 6b 2e 6e 20 3d 20  Node->block.n = 
4659a 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20  nBlock;.        
4659b 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
4659c 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61    sqlite3_free(a
4659d 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 20 20  Block);.        
4659e 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
4659f 20 20 20 20 20 20 6e 6f 64 65 52 65 61 64 65 72        nodeReader
465a0 52 65 6c 65 61 73 65 28 26 72 65 61 64 65 72 29  Release(&reader)
465a1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
465a2 0a 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74  .    rc2 = sqlit
465a3 65 33 5f 72 65 73 65 74 28 70 53 65 6c 65 63 74  e3_reset(pSelect
465a4 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
465a5 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20  QLITE_OK ) rc = 
465a6 72 63 32 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  rc2;.  }..  retu
465a7 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
465a8 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6c 61  Determine the la
465a9 72 67 65 73 74 20 73 65 67 6d 65 6e 74 20 69 6e  rgest segment in
465aa 64 65 78 20 76 61 6c 75 65 20 74 68 61 74 20 65  dex value that e
465ab 78 69 73 74 73 20 77 69 74 68 69 6e 20 61 62 73  xists within abs
465ac 6f 6c 75 74 65 0a 2a 2a 20 6c 65 76 65 6c 20 69  olute.** level i
465ad 41 62 73 4c 65 76 65 6c 2b 31 2e 20 49 66 20 6e  AbsLevel+1. If n
465ae 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  o error occurs, 
465af 73 65 74 20 2a 70 69 49 64 78 20 74 6f 20 74 68  set *piIdx to th
465b0 69 73 20 76 61 6c 75 65 20 70 6c 75 73 0a 2a 2a  is value plus.**
465b1 20 6f 6e 65 20 62 65 66 6f 72 65 20 72 65 74 75   one before retu
465b2 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 2e  rning SQLITE_OK.
465b3 20 4f 72 2c 20 69 66 20 74 68 65 72 65 20 61 72   Or, if there ar
465b4 65 20 6e 6f 20 73 65 67 6d 65 6e 74 73 20 61 74  e no segments at
465b5 20 61 6c 6c 20 0a 2a 2a 20 77 69 74 68 69 6e 20   all .** within 
465b6 6c 65 76 65 6c 20 69 41 62 73 4c 65 76 65 6c 2c  level iAbsLevel,
465b7 20 73 65 74 20 2a 70 69 49 64 78 20 74 6f 20 7a   set *piIdx to z
465b8 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ero..**.** If an
465b9 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72   error occurs, r
465ba 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65 20  eturn an SQLite 
465bb 65 72 72 6f 72 20 63 6f 64 65 2e 20 54 68 65 20  error code. The 
465bc 66 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 0a 2a  final value of.*
465bd 2a 20 2a 70 69 49 64 78 20 69 73 20 75 6e 64 65  * *piIdx is unde
465be 66 69 6e 65 64 20 69 6e 20 74 68 69 73 20 63 61  fined in this ca
465bf 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  se..*/.static in
465c0 74 20 66 74 73 33 49 6e 63 72 6d 65 72 67 65 4f  t fts3IncrmergeO
465c1 75 74 70 75 74 49 64 78 28 20 0a 20 20 46 74 73  utputIdx( .  Fts
465c2 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20  3Table *p,      
465c3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
465c4 46 54 53 20 54 61 62 6c 65 20 68 61 6e 64 6c 65  FTS Table handle
465c5 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
465c6 74 36 34 20 69 41 62 73 4c 65 76 65 6c 2c 20 20  t64 iAbsLevel,  
465c7 20 20 20 20 20 20 2f 2a 20 41 62 73 6f 6c 75 74        /* Absolut
465c8 65 20 69 6e 64 65 78 20 6f 66 20 69 6e 70 75 74  e index of input
465c9 20 73 65 67 6d 65 6e 74 73 20 2a 2f 0a 20 20 69   segments */.  i
465ca 6e 74 20 2a 70 69 49 64 78 20 20 20 20 20 20 20  nt *piIdx       
465cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
465cc 2a 20 4f 55 54 3a 20 4e 65 78 74 20 66 72 65 65  * OUT: Next free
465cd 20 69 6e 64 65 78 20 61 74 20 69 41 62 73 4c 65   index at iAbsLe
465ce 76 65 6c 2b 31 20 2a 2f 0a 29 7b 0a 20 20 69 6e  vel+1 */.){.  in
465cf 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
465d0 73 74 6d 74 20 2a 70 4f 75 74 70 75 74 49 64 78  stmt *pOutputIdx
465d1 20 3d 20 30 3b 20 20 20 2f 2a 20 53 51 4c 20 75   = 0;   /* SQL u
465d2 73 65 64 20 74 6f 20 66 69 6e 64 20 6f 75 74 70  sed to find outp
465d3 75 74 20 69 6e 64 65 78 20 2a 2f 0a 0a 20 20 72  ut index */..  r
465d4 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28  c = fts3SqlStmt(
465d5 70 2c 20 53 51 4c 5f 4e 45 58 54 5f 53 45 47 4d  p, SQL_NEXT_SEGM
465d6 45 4e 54 5f 49 4e 44 45 58 2c 20 26 70 4f 75 74  ENT_INDEX, &pOut
465d7 70 75 74 49 64 78 2c 20 30 29 3b 0a 20 20 69 66  putIdx, 0);.  if
465d8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
465d9 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62  ){.    sqlite3_b
465da 69 6e 64 5f 69 6e 74 36 34 28 70 4f 75 74 70 75  ind_int64(pOutpu
465db 74 49 64 78 2c 20 31 2c 20 69 41 62 73 4c 65 76  tIdx, 1, iAbsLev
465dc 65 6c 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  el+1);.    sqlit
465dd 65 33 5f 73 74 65 70 28 70 4f 75 74 70 75 74 49  e3_step(pOutputI
465de 64 78 29 3b 0a 20 20 20 20 2a 70 69 49 64 78 20  dx);.    *piIdx 
465df 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
465e0 5f 69 6e 74 28 70 4f 75 74 70 75 74 49 64 78 2c  _int(pOutputIdx,
465e1 20 30 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71   0);.    rc = sq
465e2 6c 69 74 65 33 5f 72 65 73 65 74 28 70 4f 75 74  lite3_reset(pOut
465e3 70 75 74 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 20  putIdx);.  }..  
465e4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
465e5 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e   .** Allocate an
465e6 20 61 70 70 65 6e 64 61 62 6c 65 20 6f 75 74 70   appendable outp
465e7 75 74 20 73 65 67 6d 65 6e 74 20 6f 6e 20 61 62  ut segment on ab
465e8 73 6f 6c 75 74 65 20 6c 65 76 65 6c 20 69 41 62  solute level iAb
465e9 73 4c 65 76 65 6c 2b 31 0a 2a 2a 20 77 69 74 68  sLevel+1.** with
465ea 20 69 64 78 20 76 61 6c 75 65 20 69 49 64 78 2e   idx value iIdx.
465eb 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 25 5f  .**.** In the %_
465ec 73 65 67 64 69 72 20 74 61 62 6c 65 2c 20 61 20  segdir table, a 
465ed 73 65 67 6d 65 6e 74 20 69 73 20 64 65 66 69 6e  segment is defin
465ee 65 64 20 62 79 20 74 68 65 20 76 61 6c 75 65 73  ed by the values
465ef 20 69 6e 20 74 68 72 65 65 0a 2a 2a 20 63 6f 6c   in three.** col
465f0 75 6d 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  umns:.**.**     
465f1 73 74 61 72 74 5f 62 6c 6f 63 6b 0a 2a 2a 20 20  start_block.**  
465f2 20 20 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c     leaves_end_bl
465f3 6f 63 6b 0a 2a 2a 20 20 20 20 20 65 6e 64 5f 62  ock.**     end_b
465f4 6c 6f 63 6b 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  lock.**.** When 
465f5 61 6e 20 61 70 70 65 6e 64 61 62 6c 65 20 73 65  an appendable se
465f6 67 6d 65 6e 74 20 69 73 20 61 6c 6c 6f 63 61 74  gment is allocat
465f7 65 64 2c 20 69 74 20 69 73 20 65 73 74 69 6d 61  ed, it is estima
465f8 74 65 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ted that the.** 
465f9 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
465fa 66 20 6c 65 61 66 20 62 6c 6f 63 6b 73 20 74 68  f leaf blocks th
465fb 61 74 20 6d 61 79 20 62 65 20 72 65 71 75 69 72  at may be requir
465fc 65 64 20 69 73 20 74 68 65 20 73 75 6d 20 6f 66  ed is the sum of
465fd 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
465fe 66 20 6c 65 61 66 20 62 6c 6f 63 6b 73 20 63 6f  f leaf blocks co
465ff 6e 73 75 6d 65 64 20 62 79 20 74 68 65 20 69 6e  nsumed by the in
46600 70 75 74 20 73 65 67 6d 65 6e 74 73 2c 20 70 6c  put segments, pl
46601 75 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a  us the number.**
46602 20 6f 66 20 69 6e 70 75 74 20 73 65 67 6d 65 6e   of input segmen
46603 74 73 2c 20 6d 75 6c 74 69 70 6c 69 65 64 20 62  ts, multiplied b
46604 79 20 74 77 6f 2e 20 54 68 69 73 20 76 61 6c 75  y two. This valu
46605 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 73  e is stored in s
46606 74 61 63 6b 20 0a 2a 2a 20 76 61 72 69 61 62 6c  tack .** variabl
46607 65 20 6e 4c 65 61 66 45 73 74 2e 0a 2a 2a 0a 2a  e nLeafEst..**.*
46608 2a 20 41 20 74 6f 74 61 6c 20 6f 66 20 31 36 2a  * A total of 16*
46609 6e 4c 65 61 66 45 73 74 20 62 6c 6f 63 6b 73 20  nLeafEst blocks 
4660a 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 77 68  are allocated wh
4660b 65 6e 20 61 6e 20 61 70 70 65 6e 64 61 62 6c 65  en an appendable
4660c 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 69 73 20 63   segment.** is c
4660d 72 65 61 74 65 64 20 28 28 31 20 2b 20 65 6e 64  reated ((1 + end
4660e 5f 62 6c 6f 63 6b 20 2d 20 73 74 61 72 74 5f 62  _block - start_b
4660f 6c 6f 63 6b 29 3d 3d 31 36 2a 6e 4c 65 61 66 45  lock)==16*nLeafE
46610 73 74 29 2e 20 54 68 65 20 63 6f 6e 74 69 67 75  st). The contigu
46611 6f 75 73 0a 2a 2a 20 61 72 72 61 79 20 6f 66 20  ous.** array of 
46612 6c 65 61 66 20 6e 6f 64 65 73 20 73 74 61 72 74  leaf nodes start
46613 73 20 61 74 20 74 68 65 20 66 69 72 73 74 20 62  s at the first b
46614 6c 6f 63 6b 20 61 6c 6c 6f 63 61 74 65 64 2e 20  lock allocated. 
46615 54 68 65 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20  The array.** of 
46616 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 74  interior nodes t
46617 68 61 74 20 61 72 65 20 70 61 72 65 6e 74 73 20  hat are parents 
46618 6f 66 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65  of the leaf node
46619 73 20 73 74 61 72 74 20 61 74 20 62 6c 6f 63 6b  s start at block
4661a 0a 2a 2a 20 28 73 74 61 72 74 5f 62 6c 6f 63 6b  .** (start_block
4661b 20 2b 20 28 31 20 2b 20 65 6e 64 5f 62 6c 6f 63   + (1 + end_bloc
4661c 6b 20 2d 20 73 74 61 72 74 5f 62 6c 6f 63 6b 29  k - start_block)
4661d 20 2f 20 31 36 29 2e 20 41 6e 64 20 73 6f 20 6f   / 16). And so o
4661e 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  n..**.** In the 
4661f 61 63 74 75 61 6c 20 63 6f 64 65 20 62 65 6c 6f  actual code belo
46620 77 2c 20 74 68 65 20 76 61 6c 75 65 20 22 31 36  w, the value "16
46621 22 20 69 73 20 72 65 70 6c 61 63 65 64 20 77 69  " is replaced wi
46622 74 68 20 74 68 65 20 0a 2a 2a 20 70 72 65 2d 70  th the .** pre-p
46623 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 46  rocessor macro F
46624 54 53 5f 4d 41 58 5f 41 50 50 45 4e 44 41 42 4c  TS_MAX_APPENDABL
46625 45 5f 48 45 49 47 48 54 2e 0a 2a 2f 0a 73 74 61  E_HEIGHT..*/.sta
46626 74 69 63 20 69 6e 74 20 66 74 73 33 49 6e 63 72  tic int fts3Incr
46627 6d 65 72 67 65 57 72 69 74 65 72 28 20 0a 20 20  mergeWriter( .  
46628 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20  Fts3Table *p,   
46629 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4662a 2f 2a 20 46 74 73 33 20 74 61 62 6c 65 20 68 61  /* Fts3 table ha
4662b 6e 64 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ndle */.  sqlite
4662c 33 5f 69 6e 74 36 34 20 69 41 62 73 4c 65 76 65  3_int64 iAbsLeve
4662d 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 41 62 73  l,        /* Abs
4662e 6f 6c 75 74 65 20 6c 65 76 65 6c 20 6f 66 20 69  olute level of i
4662f 6e 70 75 74 20 73 65 67 6d 65 6e 74 73 20 2a 2f  nput segments */
46630 0a 20 20 69 6e 74 20 69 49 64 78 2c 20 20 20 20  .  int iIdx,    
46631 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46632 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6e     /* Index of n
46633 65 77 20 6f 75 74 70 75 74 20 73 65 67 6d 65 6e  ew output segmen
46634 74 20 2a 2f 0a 20 20 46 74 73 33 4d 75 6c 74 69  t */.  Fts3Multi
46635 53 65 67 52 65 61 64 65 72 20 2a 70 43 73 72 2c  SegReader *pCsr,
46636 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
46637 20 74 68 61 74 20 64 61 74 61 20 77 69 6c 6c 20   that data will 
46638 62 65 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a  be read from */.
46639 20 20 49 6e 63 72 6d 65 72 67 65 57 72 69 74 65    IncrmergeWrite
4663a 72 20 2a 70 57 72 69 74 65 72 20 20 20 20 20 20  r *pWriter      
4663b 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68    /* Populate th
4663c 69 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 29 7b 0a  is object */.){.
4663d 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
4663e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4663f 20 20 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65    /* Return Code
46640 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20   */.  int i;    
46641 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46642 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
46643 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
46644 69 6e 74 20 6e 4c 65 61 66 45 73 74 20 3d 20 30  int nLeafEst = 0
46645 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
46646 2f 2a 20 42 6c 6f 63 6b 73 20 61 6c 6c 6f 63 61  /* Blocks alloca
46647 74 65 64 20 66 6f 72 20 6c 65 61 66 20 6e 6f 64  ted for leaf nod
46648 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  es */.  sqlite3_
46649 73 74 6d 74 20 2a 70 4c 65 61 66 45 73 74 20 3d  stmt *pLeafEst =
4664a 20 30 3b 20 20 20 20 20 2f 2a 20 53 51 4c 20 75   0;     /* SQL u
4664b 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
4664c 20 6e 4c 65 61 66 45 73 74 20 2a 2f 0a 20 20 73   nLeafEst */.  s
4664d 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 69  qlite3_stmt *pFi
4664e 72 73 74 42 6c 6f 63 6b 20 3d 20 30 3b 20 20 2f  rstBlock = 0;  /
4664f 2a 20 53 51 4c 20 75 73 65 64 20 74 6f 20 64 65  * SQL used to de
46650 74 65 72 6d 69 6e 65 20 66 69 72 73 74 20 62 6c  termine first bl
46651 6f 63 6b 20 2a 2f 0a 0a 20 20 2f 2a 20 43 61 6c  ock */..  /* Cal
46652 63 75 6c 61 74 65 20 6e 4c 65 61 66 45 73 74 2e  culate nLeafEst.
46653 20 2a 2f 0a 20 20 72 63 20 3d 20 66 74 73 33 53   */.  rc = fts3S
46654 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 4d 41  qlStmt(p, SQL_MA
46655 58 5f 4c 45 41 46 5f 4e 4f 44 45 5f 45 53 54 49  X_LEAF_NODE_ESTI
46656 4d 41 54 45 2c 20 26 70 4c 65 61 66 45 73 74 2c  MATE, &pLeafEst,
46657 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53   0);.  if( rc==S
46658 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
46659 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
4665a 36 34 28 70 4c 65 61 66 45 73 74 2c 20 31 2c 20  64(pLeafEst, 1, 
4665b 69 41 62 73 4c 65 76 65 6c 29 3b 0a 20 20 20 20  iAbsLevel);.    
4665c 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
4665d 36 34 28 70 4c 65 61 66 45 73 74 2c 20 32 2c 20  64(pLeafEst, 2, 
4665e 70 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 29 3b  pCsr->nSegment);
4665f 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
46660 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65  ROW==sqlite3_ste
46661 70 28 70 4c 65 61 66 45 73 74 29 20 29 7b 0a 20  p(pLeafEst) ){. 
46662 20 20 20 20 20 6e 4c 65 61 66 45 73 74 20 3d 20       nLeafEst = 
46663 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
46664 6e 74 28 70 4c 65 61 66 45 73 74 2c 20 30 29 3b  nt(pLeafEst, 0);
46665 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
46666 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 4c  sqlite3_reset(pL
46667 65 61 66 45 73 74 29 3b 0a 20 20 7d 0a 20 20 69  eafEst);.  }.  i
46668 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
46669 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
4666a 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68   /* Calculate th
4666b 65 20 66 69 72 73 74 20 62 6c 6f 63 6b 20 74 6f  e first block to
4666c 20 75 73 65 20 69 6e 20 74 68 65 20 6f 75 74 70   use in the outp
4666d 75 74 20 73 65 67 6d 65 6e 74 20 2a 2f 0a 20 20  ut segment */.  
4666e 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
4666f 28 70 2c 20 53 51 4c 5f 4e 45 58 54 5f 53 45 47  (p, SQL_NEXT_SEG
46670 4d 45 4e 54 53 5f 49 44 2c 20 26 70 46 69 72 73  MENTS_ID, &pFirs
46671 74 42 6c 6f 63 6b 2c 20 30 29 3b 0a 20 20 69 66  tBlock, 0);.  if
46672 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
46673 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  ){.    if( SQLIT
46674 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
46675 74 65 70 28 70 46 69 72 73 74 42 6c 6f 63 6b 29  tep(pFirstBlock)
46676 20 29 7b 0a 20 20 20 20 20 20 70 57 72 69 74 65   ){.      pWrite
46677 72 2d 3e 69 53 74 61 72 74 20 3d 20 73 71 6c 69  r->iStart = sqli
46678 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
46679 28 70 46 69 72 73 74 42 6c 6f 63 6b 2c 20 30 29  (pFirstBlock, 0)
4667a 3b 0a 20 20 20 20 20 20 70 57 72 69 74 65 72 2d  ;.      pWriter-
4667b 3e 69 45 6e 64 20 3d 20 70 57 72 69 74 65 72 2d  >iEnd = pWriter-
4667c 3e 69 53 74 61 72 74 20 2d 20 31 3b 0a 20 20 20  >iStart - 1;.   
4667d 20 20 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64     pWriter->iEnd
4667e 20 2b 3d 20 6e 4c 65 61 66 45 73 74 20 2a 20 46   += nLeafEst * F
4667f 54 53 5f 4d 41 58 5f 41 50 50 45 4e 44 41 42 4c  TS_MAX_APPENDABL
46680 45 5f 48 45 49 47 48 54 3b 0a 20 20 20 20 7d 0a  E_HEIGHT;.    }.
46681 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
46682 5f 72 65 73 65 74 28 70 46 69 72 73 74 42 6c 6f  _reset(pFirstBlo
46683 63 6b 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  ck);.  }.  if( r
46684 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
46685 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
46686 49 6e 73 65 72 74 20 74 68 65 20 6d 61 72 6b 65  Insert the marke
46687 72 20 69 6e 20 74 68 65 20 25 5f 73 65 67 6d 65  r in the %_segme
46688 6e 74 73 20 74 61 62 6c 65 20 74 6f 20 6d 61 6b  nts table to mak
46689 65 20 73 75 72 65 20 6e 6f 62 6f 64 79 20 74 72  e sure nobody tr
4668a 69 65 73 0a 20 20 2a 2a 20 74 6f 20 73 74 65 61  ies.  ** to stea
4668b 6c 20 74 68 65 20 73 70 61 63 65 20 6a 75 73 74  l the space just
4668c 20 61 6c 6c 6f 63 61 74 65 64 2e 20 54 68 69 73   allocated. This
4668d 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 74 6f   is also used to
4668e 20 69 64 65 6e 74 69 66 79 20 0a 20 20 2a 2a 20   identify .  ** 
4668f 61 70 70 65 6e 64 61 62 6c 65 20 73 65 67 6d 65  appendable segme
46690 6e 74 73 2e 20 20 2a 2f 0a 20 20 72 63 20 3d 20  nts.  */.  rc = 
46691 66 74 73 33 57 72 69 74 65 53 65 67 6d 65 6e 74  fts3WriteSegment
46692 28 70 2c 20 70 57 72 69 74 65 72 2d 3e 69 45 6e  (p, pWriter->iEn
46693 64 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20  d, 0, 0);.  if( 
46694 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
46695 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 70 57  return rc;..  pW
46696 72 69 74 65 72 2d 3e 69 41 62 73 4c 65 76 65 6c  riter->iAbsLevel
46697 20 3d 20 69 41 62 73 4c 65 76 65 6c 3b 0a 20 20   = iAbsLevel;.  
46698 70 57 72 69 74 65 72 2d 3e 6e 4c 65 61 66 45 73  pWriter->nLeafEs
46699 74 20 3d 20 6e 4c 65 61 66 45 73 74 3b 0a 20 20  t = nLeafEst;.  
4669a 70 57 72 69 74 65 72 2d 3e 69 49 64 78 20 3d 20  pWriter->iIdx = 
4669b 69 49 64 78 3b 0a 0a 20 20 2f 2a 20 53 65 74 20  iIdx;..  /* Set 
4669c 75 70 20 74 68 65 20 61 72 72 61 79 20 6f 66 20  up the array of 
4669d 4e 6f 64 65 57 72 69 74 65 72 20 6f 62 6a 65 63  NodeWriter objec
4669e 74 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b  ts */.  for(i=0;
4669f 20 69 3c 46 54 53 5f 4d 41 58 5f 41 50 50 45 4e   i<FTS_MAX_APPEN
466a0 44 41 42 4c 45 5f 48 45 49 47 48 54 3b 20 69 2b  DABLE_HEIGHT; i+
466a1 2b 29 7b 0a 20 20 20 20 70 57 72 69 74 65 72 2d  +){.    pWriter-
466a2 3e 61 4e 6f 64 65 57 72 69 74 65 72 5b 69 5d 2e  >aNodeWriter[i].
466a3 69 42 6c 6f 63 6b 20 3d 20 70 57 72 69 74 65 72  iBlock = pWriter
466a4 2d 3e 69 53 74 61 72 74 20 2b 20 69 2a 70 57 72  ->iStart + i*pWr
466a5 69 74 65 72 2d 3e 6e 4c 65 61 66 45 73 74 3b 0a  iter->nLeafEst;.
466a6 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
466a7 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
466a8 20 52 65 6d 6f 76 65 20 61 6e 20 65 6e 74 72 79   Remove an entry
466a9 20 66 72 6f 6d 20 74 68 65 20 25 5f 73 65 67 64   from the %_segd
466aa 69 72 20 74 61 62 6c 65 2e 20 54 68 69 73 20 69  ir table. This i
466ab 6e 76 6f 6c 76 65 73 20 72 75 6e 6e 69 6e 67 20  nvolves running 
466ac 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  the .** followin
466ad 67 20 74 77 6f 20 73 74 61 74 65 6d 65 6e 74 73  g two statements
466ae 3a 0a 2a 2a 0a 2a 2a 20 20 20 44 45 4c 45 54 45  :.**.**   DELETE
466af 20 46 52 4f 4d 20 25 5f 73 65 67 64 69 72 20 57   FROM %_segdir W
466b0 48 45 52 45 20 6c 65 76 65 6c 20 3d 20 3a 69 41  HERE level = :iA
466b1 62 73 4c 65 76 65 6c 20 41 4e 44 20 69 64 78 20  bsLevel AND idx 
466b2 3d 20 3a 69 49 64 78 0a 2a 2a 20 20 20 55 50 44  = :iIdx.**   UPD
466b3 41 54 45 20 25 5f 73 65 67 64 69 72 20 53 45 54  ATE %_segdir SET
466b4 20 69 64 78 20 3d 20 69 64 78 20 2d 20 31 20 57   idx = idx - 1 W
466b5 48 45 52 45 20 6c 65 76 65 6c 20 3d 20 3a 69 41  HERE level = :iA
466b6 62 73 4c 65 76 65 6c 20 41 4e 44 20 69 64 78 20  bsLevel AND idx 
466b7 3e 20 3a 69 49 64 78 0a 2a 2a 0a 2a 2a 20 54 68  > :iIdx.**.** Th
466b8 65 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  e DELETE stateme
466b9 6e 74 20 72 65 6d 6f 76 65 73 20 74 68 65 20 73  nt removes the s
466ba 70 65 63 69 66 69 63 20 25 5f 73 65 67 64 69 72  pecific %_segdir
466bb 20 6c 65 76 65 6c 2e 20 54 68 65 20 55 50 44 41   level. The UPDA
466bc 54 45 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  TE .** statement
466bd 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 74 68   ensures that th
466be 65 20 72 65 6d 61 69 6e 69 6e 67 20 73 65 67 6d  e remaining segm
466bf 65 6e 74 73 20 68 61 76 65 20 63 6f 6e 74 69 67  ents have contig
466c0 75 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64  uously allocated
466c1 0a 2a 2a 20 69 64 78 20 76 61 6c 75 65 73 2e 0a  .** idx values..
466c2 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
466c3 73 33 52 65 6d 6f 76 65 53 65 67 64 69 72 45 6e  s3RemoveSegdirEn
466c4 74 72 79 28 0a 20 20 46 74 73 33 54 61 62 6c 65  try(.  Fts3Table
466c5 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
466c6 20 20 20 20 20 20 20 2f 2a 20 46 54 53 33 20 74         /* FTS3 t
466c7 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  able handle */. 
466c8 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
466c9 41 62 73 4c 65 76 65 6c 2c 20 20 20 20 20 20 20  AbsLevel,       
466ca 20 2f 2a 20 41 62 73 6f 6c 75 74 65 20 6c 65 76   /* Absolute lev
466cb 65 6c 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f  el to delete fro
466cc 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 20  m */.  int iIdx 
466cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
466ce 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
466cf 6f 66 20 25 5f 73 65 67 64 69 72 20 65 6e 74 72  of %_segdir entr
466d0 79 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 29  y to delete */.)
466d1 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
466d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
466d3 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
466d4 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  de */.  sqlite3_
466d5 73 74 6d 74 20 2a 70 44 65 6c 65 74 65 20 3d 20  stmt *pDelete = 
466d6 30 3b 20 20 20 20 20 20 2f 2a 20 44 45 4c 45 54  0;      /* DELET
466d7 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 0a  E statement */..
466d8 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74    rc = fts3SqlSt
466d9 6d 74 28 70 2c 20 53 51 4c 5f 44 45 4c 45 54 45  mt(p, SQL_DELETE
466da 5f 53 45 47 44 49 52 5f 45 4e 54 52 59 2c 20 26  _SEGDIR_ENTRY, &
466db 70 44 65 6c 65 74 65 2c 20 30 29 3b 0a 20 20 69  pDelete, 0);.  i
466dc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
466dd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
466de 62 69 6e 64 5f 69 6e 74 36 34 28 70 44 65 6c 65  bind_int64(pDele
466df 74 65 2c 20 31 2c 20 69 41 62 73 4c 65 76 65 6c  te, 1, iAbsLevel
466e0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62  );.    sqlite3_b
466e1 69 6e 64 5f 69 6e 74 28 70 44 65 6c 65 74 65 2c  ind_int(pDelete,
466e2 20 32 2c 20 69 49 64 78 29 3b 0a 20 20 20 20 73   2, iIdx);.    s
466e3 71 6c 69 74 65 33 5f 73 74 65 70 28 70 44 65 6c  qlite3_step(pDel
466e4 65 74 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  ete);.    rc = s
466e5 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 44 65  qlite3_reset(pDe
466e6 6c 65 74 65 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  lete);.  }..  re
466e7 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
466e8 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 65  * One or more se
466e9 67 6d 65 6e 74 73 20 68 61 76 65 20 6a 75 73 74  gments have just
466ea 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 66 72   been removed fr
466eb 6f 6d 20 61 62 73 6f 6c 75 74 65 20 6c 65 76 65  om absolute leve
466ec 6c 20 69 41 62 73 4c 65 76 65 6c 2e 0a 2a 2a 20  l iAbsLevel..** 
466ed 55 70 64 61 74 65 20 74 68 65 20 27 69 64 78 27  Update the 'idx'
466ee 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65 20 72   values of the r
466ef 65 6d 61 69 6e 69 6e 67 20 73 65 67 6d 65 6e 74  emaining segment
466f0 73 20 69 6e 20 74 68 65 20 6c 65 76 65 6c 20 73  s in the level s
466f1 6f 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 69 64  o that.** the id
466f2 78 20 76 61 6c 75 65 73 20 61 72 65 20 61 20 63  x values are a c
466f3 6f 6e 74 69 67 75 6f 75 73 20 73 65 71 75 65 6e  ontiguous sequen
466f4 63 65 20 73 74 61 72 74 69 6e 67 20 66 72 6f 6d  ce starting from
466f5 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e   0..*/.static in
466f6 74 20 66 74 73 33 52 65 70 61 63 6b 53 65 67 64  t fts3RepackSegd
466f7 69 72 4c 65 76 65 6c 28 0a 20 20 46 74 73 33 54  irLevel(.  Fts3T
466f8 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20  able *p,        
466f9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54             /* FT
466fa 53 33 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20  S3 table handle 
466fb 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
466fc 36 34 20 69 41 62 73 4c 65 76 65 6c 20 20 20 20  64 iAbsLevel    
466fd 20 20 20 20 20 2f 2a 20 41 62 73 6f 6c 75 74 65       /* Absolute
466fe 20 6c 65 76 65 6c 20 74 6f 20 72 65 70 61 63 6b   level to repack
466ff 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
46700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46701 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
46702 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74  rn code */.  int
46703 20 2a 61 49 64 78 20 3d 20 30 3b 20 20 20 20 20   *aIdx = 0;     
46704 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
46705 41 72 72 61 79 20 6f 66 20 72 65 6d 61 69 6e 69  Array of remaini
46706 6e 67 20 69 64 78 20 76 61 6c 75 65 73 20 2a 2f  ng idx values */
46707 0a 20 20 69 6e 74 20 6e 49 64 78 20 3d 20 30 3b  .  int nIdx = 0;
46708 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46709 20 20 20 2f 2a 20 56 61 6c 69 64 20 65 6e 74 72     /* Valid entr
4670a 69 65 73 20 69 6e 20 61 49 64 78 5b 5d 20 2a 2f  ies in aIdx[] */
4670b 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20  .  int nAlloc = 
4670c 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
4670d 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20     /* Allocated 
4670e 73 69 7a 65 20 6f 66 20 61 49 64 78 5b 5d 20 2a  size of aIdx[] *
4670f 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
46710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46711 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
46712 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 73 71  variable */.  sq
46713 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 65 6c  lite3_stmt *pSel
46714 65 63 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  ect = 0;      /*
46715 20 53 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e   Select statemen
46716 74 20 74 6f 20 72 65 61 64 20 69 64 78 20 76 61  t to read idx va
46717 6c 75 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  lues */.  sqlite
46718 33 5f 73 74 6d 74 20 2a 70 55 70 64 61 74 65 20  3_stmt *pUpdate 
46719 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 55 70 64  = 0;      /* Upd
4671a 61 74 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  ate statement to
4671b 20 6d 6f 64 69 66 79 20 69 64 78 20 76 61 6c 75   modify idx valu
4671c 65 73 20 2a 2f 0a 0a 20 20 72 63 20 3d 20 66 74  es */..  rc = ft
4671d 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c  s3SqlStmt(p, SQL
4671e 5f 53 45 4c 45 43 54 5f 49 4e 44 45 58 45 53 2c  _SELECT_INDEXES,
4671f 20 26 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20   &pSelect, 0);. 
46720 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
46721 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  OK ){.    int rc
46722 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62  2;.    sqlite3_b
46723 69 6e 64 5f 69 6e 74 36 34 28 70 53 65 6c 65 63  ind_int64(pSelec
46724 74 2c 20 31 2c 20 69 41 62 73 4c 65 76 65 6c 29  t, 1, iAbsLevel)
46725 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 53 51 4c  ;.    while( SQL
46726 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33  ITE_ROW==sqlite3
46727 5f 73 74 65 70 28 70 53 65 6c 65 63 74 29 20 29  _step(pSelect) )
46728 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 49 64 78  {.      if( nIdx
46729 3e 3d 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  >=nAlloc ){.    
4672a 20 20 20 20 69 6e 74 20 2a 61 4e 65 77 3b 0a 20      int *aNew;. 
4672b 20 20 20 20 20 20 20 6e 41 6c 6c 6f 63 20 2b 3d         nAlloc +=
4672c 20 31 36 3b 0a 20 20 20 20 20 20 20 20 61 4e 65   16;.        aNe
4672d 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  w = sqlite3_real
4672e 6c 6f 63 28 61 49 64 78 2c 20 6e 41 6c 6c 6f 63  loc(aIdx, nAlloc
4672f 2a 73 69 7a 65 6f 66 28 69 6e 74 29 29 3b 0a 20  *sizeof(int));. 
46730 20 20 20 20 20 20 20 69 66 28 20 21 61 4e 65 77         if( !aNew
46731 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
46732 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
46733 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
46734 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
46735 20 20 20 20 61 49 64 78 20 3d 20 61 4e 65 77 3b      aIdx = aNew;
46736 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
46737 49 64 78 5b 6e 49 64 78 2b 2b 5d 20 3d 20 73 71  Idx[nIdx++] = sq
46738 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
46739 28 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20  (pSelect, 0);.  
4673a 20 20 7d 0a 20 20 20 20 72 63 32 20 3d 20 73 71    }.    rc2 = sq
4673b 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 65 6c  lite3_reset(pSel
4673c 65 63 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ect);.    if( rc
4673d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63  ==SQLITE_OK ) rc
4673e 20 3d 20 72 63 32 3b 0a 20 20 7d 0a 0a 20 20 69   = rc2;.  }..  i
4673f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
46740 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 74 73   ){.    rc = fts
46741 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f  3SqlStmt(p, SQL_
46742 53 48 49 46 54 5f 53 45 47 44 49 52 5f 45 4e 54  SHIFT_SEGDIR_ENT
46743 52 59 2c 20 26 70 55 70 64 61 74 65 2c 20 30 29  RY, &pUpdate, 0)
46744 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  ;.  }.  if( rc==
46745 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
46746 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
46747 74 36 34 28 70 55 70 64 61 74 65 2c 20 32 2c 20  t64(pUpdate, 2, 
46748 69 41 62 73 4c 65 76 65 6c 29 3b 0a 20 20 7d 0a  iAbsLevel);.  }.
46749 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 62 49  .  assert( p->bI
4674a 67 6e 6f 72 65 53 61 76 65 70 6f 69 6e 74 3d 3d  gnoreSavepoint==
4674b 30 20 29 3b 0a 20 20 70 2d 3e 62 49 67 6e 6f 72  0 );.  p->bIgnor
4674c 65 53 61 76 65 70 6f 69 6e 74 20 3d 20 31 3b 0a  eSavepoint = 1;.
4674d 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
4674e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 6e 49  QLITE_OK && i<nI
4674f 64 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  dx; i++){.    if
46750 28 20 61 49 64 78 5b 69 5d 21 3d 69 20 29 7b 0a  ( aIdx[i]!=i ){.
46751 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69        sqlite3_bi
46752 6e 64 5f 69 6e 74 28 70 55 70 64 61 74 65 2c 20  nd_int(pUpdate, 
46753 33 2c 20 61 49 64 78 5b 69 5d 29 3b 0a 20 20 20  3, aIdx[i]);.   
46754 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
46755 69 6e 74 28 70 55 70 64 61 74 65 2c 20 31 2c 20  int(pUpdate, 1, 
46756 69 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  i);.      sqlite
46757 33 5f 73 74 65 70 28 70 55 70 64 61 74 65 29 3b  3_step(pUpdate);
46758 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
46759 74 65 33 5f 72 65 73 65 74 28 70 55 70 64 61 74  te3_reset(pUpdat
4675a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
4675b 70 2d 3e 62 49 67 6e 6f 72 65 53 61 76 65 70 6f  p->bIgnoreSavepo
4675c 69 6e 74 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69  int = 0;..  sqli
4675d 74 65 33 5f 66 72 65 65 28 61 49 64 78 29 3b 0a  te3_free(aIdx);.
4675e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
4675f 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
46760 53 74 61 72 74 4e 6f 64 65 28 42 6c 6f 62 20 2a  StartNode(Blob *
46761 70 4e 6f 64 65 2c 20 69 6e 74 20 69 48 65 69 67  pNode, int iHeig
46762 68 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ht, sqlite3_int6
46763 34 20 69 43 68 69 6c 64 29 7b 0a 20 20 70 4e 6f  4 iChild){.  pNo
46764 64 65 2d 3e 61 5b 30 5d 20 3d 20 28 63 68 61 72  de->a[0] = (char
46765 29 69 48 65 69 67 68 74 3b 0a 20 20 69 66 28 20  )iHeight;.  if( 
46766 69 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 61 73  iChild ){.    as
46767 73 65 72 74 28 20 70 4e 6f 64 65 2d 3e 6e 41 6c  sert( pNode->nAl
46768 6c 6f 63 3e 3d 31 2b 73 71 6c 69 74 65 33 46 74  loc>=1+sqlite3Ft
46769 73 33 56 61 72 69 6e 74 4c 65 6e 28 69 43 68 69  s3VarintLen(iChi
4676a 6c 64 29 20 29 3b 0a 20 20 20 20 70 4e 6f 64 65  ld) );.    pNode
4676b 2d 3e 6e 20 3d 20 31 20 2b 20 73 71 6c 69 74 65  ->n = 1 + sqlite
4676c 33 46 74 73 33 50 75 74 56 61 72 69 6e 74 28 26  3Fts3PutVarint(&
4676d 70 4e 6f 64 65 2d 3e 61 5b 31 5d 2c 20 69 43 68  pNode->a[1], iCh
4676e 69 6c 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ild);.  }else{. 
4676f 20 20 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65     assert( pNode
46770 2d 3e 6e 41 6c 6c 6f 63 3e 3d 31 20 29 3b 0a 20  ->nAlloc>=1 );. 
46771 20 20 20 70 4e 6f 64 65 2d 3e 6e 20 3d 20 31 3b     pNode->n = 1;
46772 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
46773 65 20 66 69 72 73 74 20 74 77 6f 20 61 72 67 75  e first two argu
46774 6d 65 6e 74 73 20 61 72 65 20 61 20 70 6f 69 6e  ments are a poin
46775 74 65 72 20 74 6f 20 61 6e 64 20 74 68 65 20 73  ter to and the s
46776 69 7a 65 20 6f 66 20 61 20 73 65 67 6d 65 6e 74  ize of a segment
46777 20 62 2d 74 72 65 65 0a 2a 2a 20 6e 6f 64 65 2e   b-tree.** node.
46778 20 54 68 65 20 6e 6f 64 65 20 6d 61 79 20 62 65   The node may be
46779 20 61 20 6c 65 61 66 20 6f 72 20 61 6e 20 69 6e   a leaf or an in
4677a 74 65 72 6e 61 6c 20 6e 6f 64 65 2e 0a 2a 2a 0a  ternal node..**.
4677b 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
4677c 20 63 72 65 61 74 65 73 20 61 20 6e 65 77 20 6e   creates a new n
4677d 6f 64 65 20 69 6d 61 67 65 20 69 6e 20 62 6c 6f  ode image in blo
4677e 62 20 6f 62 6a 65 63 74 20 2a 70 4e 65 77 20 62  b object *pNew b
4677f 79 20 63 6f 70 79 69 6e 67 0a 2a 2a 20 61 6c 6c  y copying.** all
46780 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 65 20   terms that are 
46781 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
46782 65 71 75 61 6c 20 74 6f 20 7a 54 65 72 6d 2f 6e  equal to zTerm/n
46783 54 65 72 6d 20 28 66 6f 72 20 6c 65 61 66 20 6e  Term (for leaf n
46784 6f 64 65 73 29 0a 2a 2a 20 6f 72 20 67 72 65 61  odes).** or grea
46785 74 65 72 20 74 68 61 6e 20 7a 54 65 72 6d 2f 6e  ter than zTerm/n
46786 54 65 72 6d 20 28 66 6f 72 20 69 6e 74 65 72 6e  Term (for intern
46787 61 6c 20 6e 6f 64 65 73 29 20 66 72 6f 6d 20 61  al nodes) from a
46788 4e 6f 64 65 2f 6e 4e 6f 64 65 2e 0a 2a 2f 0a 73  Node/nNode..*/.s
46789 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 54 72  tatic int fts3Tr
4678a 75 6e 63 61 74 65 4e 6f 64 65 28 0a 20 20 63 6f  uncateNode(.  co
4678b 6e 73 74 20 63 68 61 72 20 2a 61 4e 6f 64 65 2c  nst char *aNode,
4678c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4678d 20 43 75 72 72 65 6e 74 20 6e 6f 64 65 20 69 6d   Current node im
4678e 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 6f  age */.  int nNo
4678f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  de,             
46790 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
46791 20 6f 66 20 61 4e 6f 64 65 20 69 6e 20 62 79 74   of aNode in byt
46792 65 73 20 2a 2f 0a 20 20 42 6c 6f 62 20 2a 70 4e  es */.  Blob *pN
46793 65 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ew,             
46794 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
46795 57 72 69 74 65 20 6e 65 77 20 6e 6f 64 65 20 69  Write new node i
46796 6d 61 67 65 20 68 65 72 65 20 2a 2f 0a 20 20 63  mage here */.  c
46797 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 72 6d  onst char *zTerm
46798 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
46799 2a 20 4f 6d 69 74 20 61 6c 6c 20 74 65 72 6d 73  * Omit all terms
4679a 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68   smaller than th
4679b 69 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72  is */.  int nTer
4679c 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m,              
4679d 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
4679e 6f 66 20 7a 54 65 72 6d 20 69 6e 20 62 79 74 65  of zTerm in byte
4679f 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  s */.  sqlite3_i
467a0 6e 74 36 34 20 2a 70 69 42 6c 6f 63 6b 20 20 20  nt64 *piBlock   
467a1 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 42         /* OUT: B
467a2 6c 6f 63 6b 20 6e 75 6d 62 65 72 20 69 6e 20 6e  lock number in n
467a3 65 78 74 20 6c 61 79 65 72 20 64 6f 77 6e 20 2a  ext layer down *
467a4 2f 0a 29 7b 0a 20 20 4e 6f 64 65 52 65 61 64 65  /.){.  NodeReade
467a5 72 20 72 65 61 64 65 72 3b 20 20 20 20 20 20 20  r reader;       
467a6 20 20 20 20 20 20 20 2f 2a 20 52 65 61 64 65 72         /* Reader
467a7 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 42 6c 6f   object */.  Blo
467a8 62 20 70 72 65 76 20 3d 20 7b 30 2c 20 30 2c 20  b prev = {0, 0, 
467a9 30 7d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  0};          /* 
467aa 50 72 65 76 69 6f 75 73 20 74 65 72 6d 20 77 72  Previous term wr
467ab 69 74 74 65 6e 20 74 6f 20 6e 65 77 20 6e 6f 64  itten to new nod
467ac 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20  e */.  int rc = 
467ad 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
467ae 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
467af 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 62   code */.  int b
467b0 4c 65 61 66 20 3d 20 61 4e 6f 64 65 5b 30 5d 3d  Leaf = aNode[0]=
467b1 3d 27 5c 30 27 3b 20 20 20 20 20 2f 2a 20 54 72  ='\0';     /* Tr
467b2 75 65 20 66 6f 72 20 61 20 6c 65 61 66 20 6e 6f  ue for a leaf no
467b3 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  de */..  /* Allo
467b4 63 61 74 65 20 72 65 71 75 69 72 65 64 20 6f 75  cate required ou
467b5 74 70 75 74 20 73 70 61 63 65 20 2a 2f 0a 20 20  tput space */.  
467b6 62 6c 6f 62 47 72 6f 77 42 75 66 66 65 72 28 70  blobGrowBuffer(p
467b7 4e 65 77 2c 20 6e 4e 6f 64 65 2c 20 26 72 63 29  New, nNode, &rc)
467b8 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
467b9 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
467ba 63 3b 0a 20 20 70 4e 65 77 2d 3e 6e 20 3d 20 30  c;.  pNew->n = 0
467bb 3b 0a 0a 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65  ;..  /* Populate
467bc 20 6e 65 77 20 6e 6f 64 65 20 62 75 66 66 65 72   new node buffer
467bd 20 2a 2f 0a 20 20 66 6f 72 28 72 63 20 3d 20 6e   */.  for(rc = n
467be 6f 64 65 52 65 61 64 65 72 49 6e 69 74 28 26 72  odeReaderInit(&r
467bf 65 61 64 65 72 2c 20 61 4e 6f 64 65 2c 20 6e 4e  eader, aNode, nN
467c0 6f 64 65 29 3b 20 0a 20 20 20 20 20 20 72 63 3d  ode); .      rc=
467c1 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65  =SQLITE_OK && re
467c2 61 64 65 72 2e 61 4e 6f 64 65 3b 20 0a 20 20 20  ader.aNode; .   
467c3 20 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 61 64     rc = nodeRead
467c4 65 72 4e 65 78 74 28 26 72 65 61 64 65 72 29 0a  erNext(&reader).
467c5 20 20 29 7b 0a 20 20 20 20 69 66 28 20 70 4e 65    ){.    if( pNe
467c6 77 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  w->n==0 ){.     
467c7 20 69 6e 74 20 72 65 73 20 3d 20 66 74 73 33 54   int res = fts3T
467c8 65 72 6d 43 6d 70 28 72 65 61 64 65 72 2e 74 65  ermCmp(reader.te
467c9 72 6d 2e 61 2c 20 72 65 61 64 65 72 2e 74 65 72  rm.a, reader.ter
467ca 6d 2e 6e 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72  m.n, zTerm, nTer
467cb 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65  m);.      if( re
467cc 73 3c 30 20 7c 7c 20 28 62 4c 65 61 66 3d 3d 30  s<0 || (bLeaf==0
467cd 20 26 26 20 72 65 73 3d 3d 30 29 20 29 20 63 6f   && res==0) ) co
467ce 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 66 74  ntinue;.      ft
467cf 73 33 53 74 61 72 74 4e 6f 64 65 28 70 4e 65 77  s3StartNode(pNew
467d0 2c 20 28 69 6e 74 29 61 4e 6f 64 65 5b 30 5d 2c  , (int)aNode[0],
467d1 20 72 65 61 64 65 72 2e 69 43 68 69 6c 64 29 3b   reader.iChild);
467d2 0a 20 20 20 20 20 20 2a 70 69 42 6c 6f 63 6b 20  .      *piBlock 
467d3 3d 20 72 65 61 64 65 72 2e 69 43 68 69 6c 64 3b  = reader.iChild;
467d4 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
467d5 66 74 73 33 41 70 70 65 6e 64 54 6f 4e 6f 64 65  fts3AppendToNode
467d6 28 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2c 20  (.        pNew, 
467d7 26 70 72 65 76 2c 20 72 65 61 64 65 72 2e 74 65  &prev, reader.te
467d8 72 6d 2e 61 2c 20 72 65 61 64 65 72 2e 74 65 72  rm.a, reader.ter
467d9 6d 2e 6e 2c 0a 20 20 20 20 20 20 20 20 72 65 61  m.n,.        rea
467da 64 65 72 2e 61 44 6f 63 6c 69 73 74 2c 20 72 65  der.aDoclist, re
467db 61 64 65 72 2e 6e 44 6f 63 6c 69 73 74 0a 20 20  ader.nDoclist.  
467dc 20 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21    );.    if( rc!
467dd 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65  =SQLITE_OK ) bre
467de 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e  ak;.  }.  if( pN
467df 65 77 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  ew->n==0 ){.    
467e0 66 74 73 33 53 74 61 72 74 4e 6f 64 65 28 70 4e  fts3StartNode(pN
467e1 65 77 2c 20 28 69 6e 74 29 61 4e 6f 64 65 5b 30  ew, (int)aNode[0
467e2 5d 2c 20 72 65 61 64 65 72 2e 69 43 68 69 6c 64  ], reader.iChild
467e3 29 3b 0a 20 20 20 20 2a 70 69 42 6c 6f 63 6b 20  );.    *piBlock 
467e4 3d 20 72 65 61 64 65 72 2e 69 43 68 69 6c 64 3b  = reader.iChild;
467e5 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
467e6 4e 65 77 2d 3e 6e 3c 3d 70 4e 65 77 2d 3e 6e 41  New->n<=pNew->nA
467e7 6c 6c 6f 63 20 29 3b 0a 0a 20 20 6e 6f 64 65 52  lloc );..  nodeR
467e8 65 61 64 65 72 52 65 6c 65 61 73 65 28 26 72 65  eaderRelease(&re
467e9 61 64 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  ader);.  sqlite3
467ea 5f 66 72 65 65 28 70 72 65 76 2e 61 29 3b 0a 20  _free(prev.a);. 
467eb 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
467ec 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20  *.** Remove all 
467ed 74 65 72 6d 73 20 73 6d 61 6c 6c 65 72 20 74 68  terms smaller th
467ee 61 6e 20 7a 54 65 72 6d 2f 6e 54 65 72 6d 20 66  an zTerm/nTerm f
467ef 72 6f 6d 20 73 65 67 6d 65 6e 74 20 69 49 64 78  rom segment iIdx
467f0 20 69 6e 20 61 62 73 6f 6c 75 74 65 20 0a 2a 2a   in absolute .**
467f1 20 6c 65 76 65 6c 20 69 41 62 73 4c 65 76 65 6c   level iAbsLevel
467f2 2e 20 54 68 69 73 20 6d 61 79 20 69 6e 76 6f 6c  . This may invol
467f3 76 65 20 64 65 6c 65 74 69 6e 67 20 65 6e 74 72  ve deleting entr
467f4 69 65 73 20 66 72 6f 6d 20 74 68 65 20 25 5f 73  ies from the %_s
467f5 65 67 6d 65 6e 74 73 0a 2a 2a 20 74 61 62 6c 65  egments.** table
467f6 2c 20 61 6e 64 20 6d 6f 64 69 66 79 69 6e 67 20  , and modifying 
467f7 65 78 69 73 74 69 6e 67 20 65 6e 74 72 69 65 73  existing entries
467f8 20 69 6e 20 62 6f 74 68 20 74 68 65 20 25 5f 73   in both the %_s
467f9 65 67 6d 65 6e 74 73 20 61 6e 64 20 25 5f 73 65  egments and %_se
467fa 67 64 69 72 0a 2a 2a 20 74 61 62 6c 65 73 2e 0a  gdir.** tables..
467fb 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  **.** SQLITE_OK 
467fc 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
467fd 68 65 20 73 65 67 6d 65 6e 74 20 69 73 20 75 70  he segment is up
467fe 64 61 74 65 64 20 73 75 63 63 65 73 73 66 75 6c  dated successful
467ff 6c 79 2e 20 4f 72 20 61 6e 0a 2a 2a 20 53 51 4c  ly. Or an.** SQL
46800 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f  ite error code o
46801 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61  therwise..*/.sta
46802 74 69 63 20 69 6e 74 20 66 74 73 33 54 72 75 6e  tic int fts3Trun
46803 63 61 74 65 53 65 67 6d 65 6e 74 28 0a 20 20 46  cateSegment(.  F
46804 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20  ts3Table *p,    
46805 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
46806 2a 20 46 54 53 33 20 74 61 62 6c 65 20 68 61 6e  * FTS3 table han
46807 64 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  dle */.  sqlite3
46808 5f 69 6e 74 36 34 20 69 41 62 73 4c 65 76 65 6c  _int64 iAbsLevel
46809 2c 20 20 20 20 20 20 20 20 2f 2a 20 41 62 73 6f  ,        /* Abso
4680a 6c 75 74 65 20 6c 65 76 65 6c 20 6f 66 20 73 65  lute level of se
4680b 67 6d 65 6e 74 20 74 6f 20 6d 6f 64 69 66 79 20  gment to modify 
4680c 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 2c 20 20  */.  int iIdx,  
4680d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4680e 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 77 69       /* Index wi
4680f 74 68 69 6e 20 6c 65 76 65 6c 20 6f 66 20 73 65  thin level of se
46810 67 6d 65 6e 74 20 74 6f 20 6d 6f 64 69 66 79 20  gment to modify 
46811 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
46812 2a 7a 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20  *zTerm,         
46813 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74       /* Remove t
46814 65 72 6d 73 20 73 6d 61 6c 6c 65 72 20 74 68 61  erms smaller tha
46815 6e 20 74 68 69 73 20 2a 2f 0a 20 20 69 6e 74 20  n this */.  int 
46816 6e 54 65 72 6d 20 20 20 20 20 20 20 20 20 20 20  nTerm           
46817 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
46818 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
46819 20 62 75 66 66 65 72 20 7a 54 65 72 6d 20 2a 2f   buffer zTerm */
4681a 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
4681b 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
4681c 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
4681d 63 6f 64 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 72  code */.  Blob r
4681e 6f 6f 74 20 3d 20 7b 30 2c 30 2c 30 7d 3b 20 20  oot = {0,0,0};  
4681f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77            /* New
46820 20 72 6f 6f 74 20 70 61 67 65 20 69 6d 61 67 65   root page image
46821 20 2a 2f 0a 20 20 42 6c 6f 62 20 62 6c 6f 63 6b   */.  Blob block
46822 20 3d 20 7b 30 2c 30 2c 30 7d 3b 20 20 20 20 20   = {0,0,0};     
46823 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20        /* Buffer 
46824 75 73 65 64 20 66 6f 72 20 61 6e 79 20 6f 74 68  used for any oth
46825 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 73 71  er block */.  sq
46826 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 42 6c 6f  lite3_int64 iBlo
46827 63 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  ck = 0;       /*
46828 20 42 6c 6f 63 6b 20 69 64 20 2a 2f 0a 20 20 73   Block id */.  s
46829 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4e 65  qlite3_int64 iNe
4682a 77 53 74 61 72 74 20 3d 20 30 3b 20 20 20 20 2f  wStart = 0;    /
4682b 2a 20 4e 65 77 20 76 61 6c 75 65 20 66 6f 72 20  * New value for 
4682c 69 53 74 61 72 74 42 6c 6f 63 6b 20 2a 2f 0a 20  iStartBlock */. 
4682d 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
4682e 4f 6c 64 53 74 61 72 74 20 3d 20 30 3b 20 20 20  OldStart = 0;   
4682f 20 2f 2a 20 4f 6c 64 20 76 61 6c 75 65 20 66 6f   /* Old value fo
46830 72 20 69 53 74 61 72 74 42 6c 6f 63 6b 20 2a 2f  r iStartBlock */
46831 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
46832 2a 70 46 65 74 63 68 20 3d 20 30 3b 20 20 20 20  *pFetch = 0;    
46833 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20     /* Statement 
46834 75 73 65 64 20 74 6f 20 66 65 74 63 68 20 73 65  used to fetch se
46835 67 64 69 72 20 2a 2f 0a 0a 20 20 72 63 20 3d 20  gdir */..  rc = 
46836 66 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53  fts3SqlStmt(p, S
46837 51 4c 5f 53 45 4c 45 43 54 5f 53 45 47 44 49 52  QL_SELECT_SEGDIR
46838 2c 20 26 70 46 65 74 63 68 2c 20 30 29 3b 0a 20  , &pFetch, 0);. 
46839 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
4683a 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  OK ){.    int rc
4683b 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2;              
4683c 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74          /* sqlit
4683d 65 33 5f 72 65 73 65 74 28 29 20 72 65 74 75 72  e3_reset() retur
4683e 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 20 20 73 71  n code */.    sq
4683f 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
46840 28 70 46 65 74 63 68 2c 20 31 2c 20 69 41 62 73  (pFetch, 1, iAbs
46841 4c 65 76 65 6c 29 3b 0a 20 20 20 20 73 71 6c 69  Level);.    sqli
46842 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 70 46 65  te3_bind_int(pFe
46843 74 63 68 2c 20 32 2c 20 69 49 64 78 29 3b 0a 20  tch, 2, iIdx);. 
46844 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f     if( SQLITE_RO
46845 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28  W==sqlite3_step(
46846 70 46 65 74 63 68 29 20 29 7b 0a 20 20 20 20 20  pFetch) ){.     
46847 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 52 6f   const char *aRo
46848 6f 74 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ot = sqlite3_col
46849 75 6d 6e 5f 62 6c 6f 62 28 70 46 65 74 63 68 2c  umn_blob(pFetch,
4684a 20 34 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e   4);.      int n
4684b 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65 33 5f 63  Root = sqlite3_c
4684c 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 46 65 74  olumn_bytes(pFet
4684d 63 68 2c 20 34 29 3b 0a 20 20 20 20 20 20 69 4f  ch, 4);.      iO
4684e 6c 64 53 74 61 72 74 20 3d 20 73 71 6c 69 74 65  ldStart = sqlite
4684f 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70  3_column_int64(p
46850 46 65 74 63 68 2c 20 31 29 3b 0a 20 20 20 20 20  Fetch, 1);.     
46851 20 72 63 20 3d 20 66 74 73 33 54 72 75 6e 63 61   rc = fts3Trunca
46852 74 65 4e 6f 64 65 28 61 52 6f 6f 74 2c 20 6e 52  teNode(aRoot, nR
46853 6f 6f 74 2c 20 26 72 6f 6f 74 2c 20 7a 54 65 72  oot, &root, zTer
46854 6d 2c 20 6e 54 65 72 6d 2c 20 26 69 42 6c 6f 63  m, nTerm, &iBloc
46855 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  k);.    }.    rc
46856 32 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  2 = sqlite3_rese
46857 74 28 70 46 65 74 63 68 29 3b 0a 20 20 20 20 69  t(pFetch);.    i
46858 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
46859 20 29 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 7d   ) rc = rc2;.  }
4685a 0a 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53  ..  while( rc==S
4685b 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 42 6c 6f  QLITE_OK && iBlo
4685c 63 6b 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  ck ){.    char *
4685d 61 42 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20  aBlock = 0;.    
4685e 69 6e 74 20 6e 42 6c 6f 63 6b 20 3d 20 30 3b 0a  int nBlock = 0;.
4685f 20 20 20 20 69 4e 65 77 53 74 61 72 74 20 3d 20      iNewStart = 
46860 69 42 6c 6f 63 6b 3b 0a 0a 20 20 20 20 72 63 20  iBlock;..    rc 
46861 3d 20 73 71 6c 69 74 65 33 46 74 73 33 52 65 61  = sqlite3Fts3Rea
46862 64 42 6c 6f 63 6b 28 70 2c 20 69 42 6c 6f 63 6b  dBlock(p, iBlock
46863 2c 20 26 61 42 6c 6f 63 6b 2c 20 26 6e 42 6c 6f  , &aBlock, &nBlo
46864 63 6b 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ck, 0);.    if( 
46865 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
46866 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33  .      rc = fts3
46867 54 72 75 6e 63 61 74 65 4e 6f 64 65 28 61 42 6c  TruncateNode(aBl
46868 6f 63 6b 2c 20 6e 42 6c 6f 63 6b 2c 20 26 62 6c  ock, nBlock, &bl
46869 6f 63 6b 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72  ock, zTerm, nTer
4686a 6d 2c 20 26 69 42 6c 6f 63 6b 29 3b 0a 20 20 20  m, &iBlock);.   
4686b 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
4686c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
4686d 20 20 72 63 20 3d 20 66 74 73 33 57 72 69 74 65    rc = fts3Write
4686e 53 65 67 6d 65 6e 74 28 70 2c 20 69 4e 65 77 53  Segment(p, iNewS
4686f 74 61 72 74 2c 20 62 6c 6f 63 6b 2e 61 2c 20 62  tart, block.a, b
46870 6c 6f 63 6b 2e 6e 29 3b 0a 20 20 20 20 7d 0a 20  lock.n);.    }. 
46871 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
46872 61 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 20 20  aBlock);.  }..  
46873 2f 2a 20 56 61 72 69 61 62 6c 65 20 69 4e 65 77  /* Variable iNew
46874 53 74 61 72 74 20 6e 6f 77 20 63 6f 6e 74 61 69  Start now contai
46875 6e 73 20 74 68 65 20 66 69 72 73 74 20 76 61 6c  ns the first val
46876 69 64 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f  id leaf node. */
46877 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
46878 45 5f 4f 4b 20 26 26 20 69 4e 65 77 53 74 61 72  E_OK && iNewStar
46879 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
4687a 5f 73 74 6d 74 20 2a 70 44 65 6c 20 3d 20 30 3b  _stmt *pDel = 0;
4687b 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 71  .    rc = fts3Sq
4687c 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 44 45 4c  lStmt(p, SQL_DEL
4687d 45 54 45 5f 53 45 47 4d 45 4e 54 53 5f 52 41 4e  ETE_SEGMENTS_RAN
4687e 47 45 2c 20 26 70 44 65 6c 2c 20 30 29 3b 0a 20  GE, &pDel, 0);. 
4687f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
46880 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71  E_OK ){.      sq
46881 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
46882 28 70 44 65 6c 2c 20 31 2c 20 69 4f 6c 64 53 74  (pDel, 1, iOldSt
46883 61 72 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  art);.      sqli
46884 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
46885 44 65 6c 2c 20 32 2c 20 69 4e 65 77 53 74 61 72  Del, 2, iNewStar
46886 74 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  t-1);.      sqli
46887 74 65 33 5f 73 74 65 70 28 70 44 65 6c 29 3b 0a  te3_step(pDel);.
46888 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
46889 65 33 5f 72 65 73 65 74 28 70 44 65 6c 29 3b 0a  e3_reset(pDel);.
4688a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
4688b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
4688c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74  {.    sqlite3_st
4688d 6d 74 20 2a 70 43 68 6f 6d 70 20 3d 20 30 3b 0a  mt *pChomp = 0;.
4688e 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c      rc = fts3Sql
4688f 53 74 6d 74 28 70 2c 20 53 51 4c 5f 43 48 4f 4d  Stmt(p, SQL_CHOM
46890 50 5f 53 45 47 44 49 52 2c 20 26 70 43 68 6f 6d  P_SEGDIR, &pChom
46891 70 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72  p, 0);.    if( r
46892 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
46893 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69        sqlite3_bi
46894 6e 64 5f 69 6e 74 36 34 28 70 43 68 6f 6d 70 2c  nd_int64(pChomp,
46895 20 31 2c 20 69 4e 65 77 53 74 61 72 74 29 3b 0a   1, iNewStart);.
46896 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69        sqlite3_bi
46897 6e 64 5f 62 6c 6f 62 28 70 43 68 6f 6d 70 2c 20  nd_blob(pChomp, 
46898 32 2c 20 72 6f 6f 74 2e 61 2c 20 72 6f 6f 74 2e  2, root.a, root.
46899 6e 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  n, SQLITE_STATIC
4689a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
4689b 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 43 68 6f  _bind_int64(pCho
4689c 6d 70 2c 20 33 2c 20 69 41 62 73 4c 65 76 65 6c  mp, 3, iAbsLevel
4689d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
4689e 5f 62 69 6e 64 5f 69 6e 74 28 70 43 68 6f 6d 70  _bind_int(pChomp
4689f 2c 20 34 2c 20 69 49 64 78 29 3b 0a 20 20 20 20  , 4, iIdx);.    
468a0 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70    sqlite3_step(p
468a1 43 68 6f 6d 70 29 3b 0a 20 20 20 20 20 20 72 63  Chomp);.      rc
468a2 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
468a3 28 70 43 68 6f 6d 70 29 3b 0a 20 20 20 20 7d 0a  (pChomp);.    }.
468a4 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66    }..  sqlite3_f
468a5 72 65 65 28 72 6f 6f 74 2e 61 29 3b 0a 20 20 73  ree(root.a);.  s
468a6 71 6c 69 74 65 33 5f 66 72 65 65 28 62 6c 6f 63  qlite3_free(bloc
468a7 6b 2e 61 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  k.a);.  return r
468a8 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
468a9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
468aa 6c 65 64 20 61 66 74 65 72 20 61 6e 20 69 6e 63  led after an inc
468ab 72 6d 65 6e 74 61 6c 2d 6d 65 72 67 65 20 6f 70  rmental-merge op
468ac 65 72 61 74 69 6f 6e 20 68 61 73 20 72 75 6e 20  eration has run 
468ad 74 6f 0a 2a 2a 20 6d 65 72 67 65 20 28 6f 72 20  to.** merge (or 
468ae 70 61 72 74 69 61 6c 6c 79 20 6d 65 72 67 65 29  partially merge)
468af 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 65 67   two or more seg
468b0 6d 65 6e 74 73 20 66 72 6f 6d 20 61 62 73 6f 6c  ments from absol
468b1 75 74 65 20 6c 65 76 65 6c 0a 2a 2a 20 69 41 62  ute level.** iAb
468b2 73 4c 65 76 65 6c 2e 0a 2a 2a 0a 2a 2a 20 45 61  sLevel..**.** Ea
468b3 63 68 20 69 6e 70 75 74 20 73 65 67 6d 65 6e 74  ch input segment
468b4 20 69 73 20 65 69 74 68 65 72 20 72 65 6d 6f 76   is either remov
468b5 65 64 20 66 72 6f 6d 20 74 68 65 20 64 62 20 63  ed from the db c
468b6 6f 6d 70 6c 65 74 65 6c 79 20 28 69 66 20 61 6c  ompletely (if al
468b7 6c 20 6f 66 0a 2a 2a 20 69 74 73 20 64 61 74 61  l of.** its data
468b8 20 77 61 73 20 63 6f 70 69 65 64 20 74 6f 20 74   was copied to t
468b9 68 65 20 6f 75 74 70 75 74 20 73 65 67 6d 65 6e  he output segmen
468ba 74 20 62 79 20 74 68 65 20 69 6e 63 72 6d 65 72  t by the incrmer
468bb 67 65 20 6f 70 65 72 61 74 69 6f 6e 29 0a 2a 2a  ge operation).**
468bc 20 6f 72 20 6d 6f 64 69 66 69 65 64 20 69 6e 20   or modified in 
468bd 70 6c 61 63 65 20 73 6f 20 74 68 61 74 20 69 74  place so that it
468be 20 6e 6f 20 6c 6f 6e 67 65 72 20 63 6f 6e 74 61   no longer conta
468bf 69 6e 73 20 74 68 6f 73 65 20 65 6e 74 72 69 65  ins those entrie
468c0 73 20 74 68 61 74 0a 2a 2a 20 68 61 76 65 20 62  s that.** have b
468c1 65 65 6e 20 64 75 70 6c 69 63 61 74 65 64 20 69  een duplicated i
468c2 6e 20 74 68 65 20 6f 75 74 70 75 74 20 73 65 67  n the output seg
468c3 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ment..*/.static 
468c4 69 6e 74 20 66 74 73 33 49 6e 63 72 6d 65 72 67  int fts3Incrmerg
468c5 65 43 68 6f 6d 70 28 0a 20 20 46 74 73 33 54 61  eChomp(.  Fts3Ta
468c6 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ble *p,         
468c7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53            /* FTS
468c8 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f   table handle */
468c9 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
468ca 20 69 41 62 73 4c 65 76 65 6c 2c 20 20 20 20 20   iAbsLevel,     
468cb 20 20 20 2f 2a 20 41 62 73 6f 6c 75 74 65 20 6c     /* Absolute l
468cc 65 76 65 6c 20 63 6f 6e 74 61 69 6e 69 6e 67 20  evel containing 
468cd 73 65 67 6d 65 6e 74 73 20 2a 2f 0a 20 20 46 74  segments */.  Ft
468ce 73 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72  s3MultiSegReader
468cf 20 2a 70 43 73 72 2c 20 20 20 20 20 20 20 2f 2a   *pCsr,       /*
468d0 20 43 68 6f 6d 70 20 61 6c 6c 20 73 65 67 6d 65   Chomp all segme
468d1 6e 74 73 20 6f 70 65 6e 65 64 20 62 79 20 74 68  nts opened by th
468d2 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 69  is cursor */.  i
468d3 6e 74 20 2a 70 6e 52 65 6d 20 20 20 20 20 20 20  nt *pnRem       
468d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
468d5 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 67 6d  * Number of segm
468d6 65 6e 74 73 20 6e 6f 74 20 64 65 6c 65 74 65 64  ents not deleted
468d7 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
468d8 20 20 69 6e 74 20 6e 52 65 6d 20 3d 20 30 3b 0a    int nRem = 0;.
468d9 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
468da 45 5f 4f 4b 3b 0a 0a 20 20 66 6f 72 28 69 3d 70  E_OK;..  for(i=p
468db 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 2d 31 3b  Csr->nSegment-1;
468dc 20 69 3e 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c   i>=0 && rc==SQL
468dd 49 54 45 5f 4f 4b 3b 20 69 2d 2d 29 7b 0a 20 20  ITE_OK; i--){.  
468de 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20    Fts3SegReader 
468df 2a 70 53 65 67 20 3d 20 30 3b 0a 20 20 20 20 69  *pSeg = 0;.    i
468e0 6e 74 20 6a 3b 0a 0a 20 20 20 20 2f 2a 20 46 69  nt j;..    /* Fi
468e1 6e 64 20 74 68 65 20 46 74 73 33 53 65 67 52 65  nd the Fts3SegRe
468e2 61 64 65 72 20 6f 62 6a 65 63 74 20 77 69 74 68  ader object with
468e3 20 46 74 73 33 53 65 67 52 65 61 64 65 72 2e 69   Fts3SegReader.i
468e4 49 64 78 3d 3d 69 2e 20 49 74 20 69 73 20 68 69  Idx==i. It is hi
468e5 64 69 6e 67 0a 20 20 20 20 2a 2a 20 73 6f 6d 65  ding.    ** some
468e6 77 68 65 72 65 20 69 6e 20 74 68 65 20 70 43 73  where in the pCs
468e7 72 2d 3e 61 70 53 65 67 6d 65 6e 74 5b 5d 20 61  r->apSegment[] a
468e8 72 72 61 79 2e 20 20 2a 2f 0a 20 20 20 20 66 6f  rray.  */.    fo
468e9 72 28 6a 3d 30 3b 20 41 4c 57 41 59 53 28 6a 3c  r(j=0; ALWAYS(j<
468ea 70 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 29 3b  pCsr->nSegment);
468eb 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 70 53 65   j++){.      pSe
468ec 67 20 3d 20 70 43 73 72 2d 3e 61 70 53 65 67 6d  g = pCsr->apSegm
468ed 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 20 20 69 66  ent[j];.      if
468ee 28 20 70 53 65 67 2d 3e 69 49 64 78 3d 3d 69 20  ( pSeg->iIdx==i 
468ef 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20  ) break;.    }. 
468f0 20 20 20 61 73 73 65 72 74 28 20 6a 3c 70 43 73     assert( j<pCs
468f1 72 2d 3e 6e 53 65 67 6d 65 6e 74 20 26 26 20 70  r->nSegment && p
468f2 53 65 67 2d 3e 69 49 64 78 3d 3d 69 20 29 3b 0a  Seg->iIdx==i );.
468f3 0a 20 20 20 20 69 66 28 20 70 53 65 67 2d 3e 61  .    if( pSeg->a
468f4 4e 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Node==0 ){.     
468f5 20 2f 2a 20 53 65 67 2d 72 65 61 64 65 72 20 69   /* Seg-reader i
468f6 73 20 61 74 20 45 4f 46 2e 20 52 65 6d 6f 76 65  s at EOF. Remove
468f7 20 74 68 65 20 65 6e 74 69 72 65 20 69 6e 70 75   the entire inpu
468f8 74 20 73 65 67 6d 65 6e 74 2e 20 2a 2f 0a 20 20  t segment. */.  
468f9 20 20 20 20 72 63 20 3d 20 66 74 73 33 44 65 6c      rc = fts3Del
468fa 65 74 65 53 65 67 6d 65 6e 74 28 70 2c 20 70 53  eteSegment(p, pS
468fb 65 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  eg);.      if( r
468fc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
468fd 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 74 73          rc = fts
468fe 33 52 65 6d 6f 76 65 53 65 67 64 69 72 45 6e 74  3RemoveSegdirEnt
468ff 72 79 28 70 2c 20 69 41 62 73 4c 65 76 65 6c 2c  ry(p, iAbsLevel,
46900 20 70 53 65 67 2d 3e 69 49 64 78 29 3b 0a 20 20   pSeg->iIdx);.  
46901 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 6e 52      }.      *pnR
46902 65 6d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  em = 0;.    }els
46903 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  e{.      /* The 
46904 69 6e 63 72 65 6d 65 6e 74 61 6c 20 6d 65 72 67  incremental merg
46905 65 20 64 69 64 20 6e 6f 74 20 63 6f 70 79 20 61  e did not copy a
46906 6c 6c 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d  ll the data from
46907 20 74 68 69 73 20 0a 20 20 20 20 20 20 2a 2a 20   this .      ** 
46908 73 65 67 6d 65 6e 74 20 74 6f 20 74 68 65 20 75  segment to the u
46909 70 70 65 72 20 6c 65 76 65 6c 2e 20 54 68 65 20  pper level. The 
4690a 73 65 67 6d 65 6e 74 20 69 73 20 6d 6f 64 69 66  segment is modif
4690b 69 65 64 20 69 6e 20 70 6c 61 63 65 0a 20 20 20  ied in place.   
4690c 20 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 69 74     ** so that it
4690d 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 6b 65 79   contains no key
4690e 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 7a  s smaller than z
4690f 54 65 72 6d 2f 6e 54 65 72 6d 2e 20 2a 2f 20 0a  Term/nTerm. */ .
46910 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
46911 20 2a 7a 54 65 72 6d 20 3d 20 70 53 65 67 2d 3e   *zTerm = pSeg->
46912 7a 54 65 72 6d 3b 0a 20 20 20 20 20 20 69 6e 74  zTerm;.      int
46913 20 6e 54 65 72 6d 20 3d 20 70 53 65 67 2d 3e 6e   nTerm = pSeg->n
46914 54 65 72 6d 3b 0a 20 20 20 20 20 20 72 63 20 3d  Term;.      rc =
46915 20 66 74 73 33 54 72 75 6e 63 61 74 65 53 65 67   fts3TruncateSeg
46916 6d 65 6e 74 28 70 2c 20 69 41 62 73 4c 65 76 65  ment(p, iAbsLeve
46917 6c 2c 20 70 53 65 67 2d 3e 69 49 64 78 2c 20 7a  l, pSeg->iIdx, z
46918 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20  Term, nTerm);.  
46919 20 20 20 20 6e 52 65 6d 2b 2b 3b 0a 20 20 20 20      nRem++;.    
4691a 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d  }.  }..  if( rc=
4691b 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 52  =SQLITE_OK && nR
4691c 65 6d 21 3d 70 43 73 72 2d 3e 6e 53 65 67 6d 65  em!=pCsr->nSegme
4691d 6e 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66  nt ){.    rc = f
4691e 74 73 33 52 65 70 61 63 6b 53 65 67 64 69 72 4c  ts3RepackSegdirL
4691f 65 76 65 6c 28 70 2c 20 69 41 62 73 4c 65 76 65  evel(p, iAbsLeve
46920 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2a 70 6e 52 65  l);.  }..  *pnRe
46921 6d 20 3d 20 6e 52 65 6d 3b 0a 20 20 72 65 74 75  m = nRem;.  retu
46922 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
46923 53 74 6f 72 65 20 61 6e 20 69 6e 63 72 2d 6d 65  Store an incr-me
46924 72 67 65 20 68 69 6e 74 20 69 6e 20 74 68 65 20  rge hint in the 
46925 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61  database..*/.sta
46926 74 69 63 20 69 6e 74 20 66 74 73 33 49 6e 63 72  tic int fts3Incr
46927 6d 65 72 67 65 48 69 6e 74 53 74 6f 72 65 28 46  mergeHintStore(F
46928 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 42 6c 6f  ts3Table *p, Blo
46929 62 20 2a 70 48 69 6e 74 29 7b 0a 20 20 73 71 6c  b *pHint){.  sql
4692a 69 74 65 33 5f 73 74 6d 74 20 2a 70 52 65 70 6c  ite3_stmt *pRepl
4692b 61 63 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  ace = 0;.  int r
4692c 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
4692d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
4692e 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20  turn code */..  
4692f 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
46930 28 70 2c 20 53 51 4c 5f 52 45 50 4c 41 43 45 5f  (p, SQL_REPLACE_
46931 53 54 41 54 2c 20 26 70 52 65 70 6c 61 63 65 2c  STAT, &pReplace,
46932 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53   0);.  if( rc==S
46933 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
46934 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
46935 28 70 52 65 70 6c 61 63 65 2c 20 31 2c 20 46 54  (pReplace, 1, FT
46936 53 5f 53 54 41 54 5f 49 4e 43 52 4d 45 52 47 45  S_STAT_INCRMERGE
46937 48 49 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74  HINT);.    sqlit
46938 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 52 65  e3_bind_blob(pRe
46939 70 6c 61 63 65 2c 20 32 2c 20 70 48 69 6e 74 2d  place, 2, pHint-
4693a 3e 61 2c 20 70 48 69 6e 74 2d 3e 6e 2c 20 53 51  >a, pHint->n, SQ
4693b 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
4693c 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70    sqlite3_step(p
4693d 52 65 70 6c 61 63 65 29 3b 0a 20 20 20 20 72 63  Replace);.    rc
4693e 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
4693f 28 70 52 65 70 6c 61 63 65 29 3b 0a 20 20 7d 0a  (pReplace);.  }.
46940 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
46941 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 61 6e 20 69  ./*.** Load an i
46942 6e 63 72 2d 6d 65 72 67 65 20 68 69 6e 74 20 66  ncr-merge hint f
46943 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
46944 2e 20 54 68 65 20 69 6e 63 72 2d 6d 65 72 67 65  . The incr-merge
46945 20 68 69 6e 74 2c 20 69 66 20 6f 6e 65 20 0a 2a   hint, if one .*
46946 2a 20 65 78 69 73 74 73 2c 20 69 73 20 73 74 6f  * exists, is sto
46947 72 65 64 20 69 6e 20 74 68 65 20 72 6f 77 69 64  red in the rowid
46948 3d 3d 31 20 72 6f 77 20 6f 66 20 74 68 65 20 25  ==1 row of the %
46949 5f 73 74 61 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a  _stat table..**.
4694a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
4694b 2c 20 70 6f 70 75 6c 61 74 65 20 62 6c 6f 62 20  , populate blob 
4694c 2a 70 48 69 6e 74 20 77 69 74 68 20 74 68 65 20  *pHint with the 
4694d 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20  value read from 
4694e 74 68 65 20 25 5f 73 74 61 74 0a 2a 2a 20 74 61  the %_stat.** ta
4694f 62 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 53  ble and return S
46950 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77  QLITE_OK. Otherw
46951 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72 6f 72  ise, if an error
46952 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20   occurs, return 
46953 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 65 72 72  an.** SQLite err
46954 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74  or code..*/.stat
46955 69 63 20 69 6e 74 20 66 74 73 33 49 6e 63 72 6d  ic int fts3Incrm
46956 65 72 67 65 48 69 6e 74 4c 6f 61 64 28 46 74 73  ergeHintLoad(Fts
46957 33 54 61 62 6c 65 20 2a 70 2c 20 42 6c 6f 62 20  3Table *p, Blob 
46958 2a 70 48 69 6e 74 29 7b 0a 20 20 73 71 6c 69 74  *pHint){.  sqlit
46959 65 33 5f 73 74 6d 74 20 2a 70 53 65 6c 65 63 74  e3_stmt *pSelect
4695a 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a   = 0;.  int rc;.
4695b 0a 20 20 70 48 69 6e 74 2d 3e 6e 20 3d 20 30 3b  .  pHint->n = 0;
4695c 0a 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c 53  .  rc = fts3SqlS
4695d 74 6d 74 28 70 2c 20 53 51 4c 5f 53 45 4c 45 43  tmt(p, SQL_SELEC
4695e 54 5f 53 54 41 54 2c 20 26 70 53 65 6c 65 63 74  T_STAT, &pSelect
4695f 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , 0);.  if( rc==
46960 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
46961 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 73 71   int rc2;.    sq
46962 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 70  lite3_bind_int(p
46963 53 65 6c 65 63 74 2c 20 31 2c 20 46 54 53 5f 53  Select, 1, FTS_S
46964 54 41 54 5f 49 4e 43 52 4d 45 52 47 45 48 49 4e  TAT_INCRMERGEHIN
46965 54 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49  T);.    if( SQLI
46966 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f  TE_ROW==sqlite3_
46967 73 74 65 70 28 70 53 65 6c 65 63 74 29 20 29 7b  step(pSelect) ){
46968 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
46969 72 20 2a 61 48 69 6e 74 20 3d 20 73 71 6c 69 74  r *aHint = sqlit
4696a 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70  e3_column_blob(p
4696b 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 20 20  Select, 0);.    
4696c 20 20 69 6e 74 20 6e 48 69 6e 74 20 3d 20 73 71    int nHint = sq
4696d 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
4696e 65 73 28 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a  es(pSelect, 0);.
4696f 20 20 20 20 20 20 69 66 28 20 61 48 69 6e 74 20        if( aHint 
46970 29 7b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 47  ){.        blobG
46971 72 6f 77 42 75 66 66 65 72 28 70 48 69 6e 74 2c  rowBuffer(pHint,
46972 20 6e 48 69 6e 74 2c 20 26 72 63 29 3b 0a 20 20   nHint, &rc);.  
46973 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
46974 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
46975 20 20 20 20 20 6d 65 6d 63 70 79 28 70 48 69 6e       memcpy(pHin
46976 74 2d 3e 61 2c 20 61 48 69 6e 74 2c 20 6e 48 69  t->a, aHint, nHi
46977 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  nt);.          p
46978 48 69 6e 74 2d 3e 6e 20 3d 20 6e 48 69 6e 74 3b  Hint->n = nHint;
46979 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
4697a 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 32   }.    }.    rc2
4697b 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
4697c 28 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 69  (pSelect);.    i
4697d 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
4697e 20 29 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 7d   ) rc = rc2;.  }
4697f 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
46980 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 2a 70 52 63 20  ../*.** If *pRc 
46981 69 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4f 4b  is not SQLITE_OK
46982 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
46983 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69  ion is called, i
46984 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  t is a no-op..**
46985 20 4f 74 68 65 72 77 69 73 65 2c 20 61 70 70 65   Otherwise, appe
46986 6e 64 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 74  nd an entry to t
46987 68 65 20 68 69 6e 74 20 73 74 6f 72 65 64 20 69  he hint stored i
46988 6e 20 62 6c 6f 62 20 2a 70 48 69 6e 74 2e 20 45  n blob *pHint. E
46989 61 63 68 20 65 6e 74 72 79 0a 2a 2a 20 63 6f 6e  ach entry.** con
4698a 73 69 73 74 73 20 6f 66 20 74 77 6f 20 76 61 72  sists of two var
4698b 69 6e 74 73 2c 20 74 68 65 20 61 62 73 6f 6c 75  ints, the absolu
4698c 74 65 20 6c 65 76 65 6c 20 6e 75 6d 62 65 72 20  te level number 
4698d 6f 66 20 74 68 65 20 69 6e 70 75 74 20 73 65 67  of the input seg
4698e 6d 65 6e 74 73 20 0a 2a 2a 20 61 6e 64 20 74 68  ments .** and th
4698f 65 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 70 75  e number of inpu
46990 74 20 73 65 67 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  t segments..**.*
46991 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  * If successful,
46992 20 6c 65 61 76 65 20 2a 70 52 63 20 73 65 74 20   leave *pRc set 
46993 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64  to SQLITE_OK and
46994 20 72 65 74 75 72 6e 2e 20 49 66 20 61 6e 20 65   return. If an e
46995 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20  rror occurs,.** 
46996 73 65 74 20 2a 70 52 63 20 74 6f 20 61 6e 20 53  set *pRc to an S
46997 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
46998 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
46999 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  g..*/.static voi
4699a 64 20 66 74 73 33 49 6e 63 72 6d 65 72 67 65 48  d fts3IncrmergeH
4699b 69 6e 74 50 75 73 68 28 0a 20 20 42 6c 6f 62 20  intPush(.  Blob 
4699c 2a 70 48 69 6e 74 2c 20 20 20 20 20 20 20 20 20  *pHint,         
4699d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 69             /* Hi
4699e 6e 74 20 62 6c 6f 62 20 74 6f 20 61 70 70 65 6e  nt blob to appen
4699f 64 20 74 6f 20 2a 2f 0a 20 20 69 36 34 20 69 41  d to */.  i64 iA
469a0 62 73 4c 65 76 65 6c 2c 20 20 20 20 20 20 20 20  bsLevel,        
469a1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
469a2 73 74 20 76 61 72 69 6e 74 20 74 6f 20 73 74 6f  st varint to sto
469a3 72 65 20 69 6e 20 68 69 6e 74 20 2a 2f 0a 20 20  re in hint */.  
469a4 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20 20 20  int nInput,     
469a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
469a6 2f 2a 20 53 65 63 6f 6e 64 20 76 61 72 69 6e 74  /* Second varint
469a7 20 74 6f 20 73 74 6f 72 65 20 69 6e 20 68 69 6e   to store in hin
469a8 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 63 20  t */.  int *pRc 
469a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
469aa 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54         /* IN/OUT
469ab 3a 20 45 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a  : Error code */.
469ac 29 7b 0a 20 20 62 6c 6f 62 47 72 6f 77 42 75 66  ){.  blobGrowBuf
469ad 66 65 72 28 70 48 69 6e 74 2c 20 70 48 69 6e 74  fer(pHint, pHint
469ae 2d 3e 6e 20 2b 20 32 2a 46 54 53 33 5f 56 41 52  ->n + 2*FTS3_VAR
469af 49 4e 54 5f 4d 41 58 2c 20 70 52 63 29 3b 0a 20  INT_MAX, pRc);. 
469b0 20 69 66 28 20 2a 70 52 63 3d 3d 53 51 4c 49 54   if( *pRc==SQLIT
469b1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 48 69 6e  E_OK ){.    pHin
469b2 74 2d 3e 6e 20 2b 3d 20 73 71 6c 69 74 65 33 46  t->n += sqlite3F
469b3 74 73 33 50 75 74 56 61 72 69 6e 74 28 26 70 48  ts3PutVarint(&pH
469b4 69 6e 74 2d 3e 61 5b 70 48 69 6e 74 2d 3e 6e 5d  int->a[pHint->n]
469b5 2c 20 69 41 62 73 4c 65 76 65 6c 29 3b 0a 20 20  , iAbsLevel);.  
469b6 20 20 70 48 69 6e 74 2d 3e 6e 20 2b 3d 20 73 71    pHint->n += sq
469b7 6c 69 74 65 33 46 74 73 33 50 75 74 56 61 72 69  lite3Fts3PutVari
469b8 6e 74 28 26 70 48 69 6e 74 2d 3e 61 5b 70 48 69  nt(&pHint->a[pHi
469b9 6e 74 2d 3e 6e 5d 2c 20 28 69 36 34 29 6e 49 6e  nt->n], (i64)nIn
469ba 70 75 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  put);.  }.}../*.
469bb 2a 2a 20 52 65 61 64 20 74 68 65 20 6c 61 73 74  ** Read the last
469bc 20 65 6e 74 72 79 20 28 6d 6f 73 74 20 72 65 63   entry (most rec
469bd 65 6e 74 6c 79 20 70 75 73 68 65 64 29 20 66 72  ently pushed) fr
469be 6f 6d 20 74 68 65 20 68 69 6e 74 20 62 6c 6f 62  om the hint blob
469bf 20 2a 70 48 69 6e 74 0a 2a 2a 20 61 6e 64 20 74   *pHint.** and t
469c0 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 65  hen remove the e
469c1 6e 74 72 79 2e 20 57 72 69 74 65 20 74 68 65 20  ntry. Write the 
469c2 74 77 6f 20 76 61 6c 75 65 73 20 72 65 61 64 20  two values read 
469c3 74 6f 20 2a 70 69 41 62 73 4c 65 76 65 6c 20 61  to *piAbsLevel a
469c4 6e 64 20 0a 2a 2a 20 2a 70 6e 49 6e 70 75 74 20  nd .** *pnInput 
469c5 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
469c6 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 72  ..**.** If no er
469c7 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75  ror occurs, retu
469c8 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66  rn SQLITE_OK. If
469c9 20 74 68 65 20 68 69 6e 74 20 62 6c 6f 62 20 69   the hint blob i
469ca 6e 20 2a 70 48 69 6e 74 20 64 6f 65 73 0a 2a 2a  n *pHint does.**
469cb 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 74 20   not contain at 
469cc 6c 65 61 73 74 20 74 77 6f 20 76 61 6c 69 64 20  least two valid 
469cd 76 61 72 69 6e 74 73 2c 20 72 65 74 75 72 6e 20  varints, return 
469ce 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 56  SQLITE_CORRUPT_V
469cf 54 41 42 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  TAB..*/.static i
469d0 6e 74 20 66 74 73 33 49 6e 63 72 6d 65 72 67 65  nt fts3Incrmerge
469d1 48 69 6e 74 50 6f 70 28 42 6c 6f 62 20 2a 70 48  HintPop(Blob *pH
469d2 69 6e 74 2c 20 69 36 34 20 2a 70 69 41 62 73 4c  int, i64 *piAbsL
469d3 65 76 65 6c 2c 20 69 6e 74 20 2a 70 6e 49 6e 70  evel, int *pnInp
469d4 75 74 29 7b 0a 20 20 63 6f 6e 73 74 20 69 6e 74  ut){.  const int
469d5 20 6e 48 69 6e 74 20 3d 20 70 48 69 6e 74 2d 3e   nHint = pHint->
469d6 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 69  n;.  int i;..  i
469d7 20 3d 20 70 48 69 6e 74 2d 3e 6e 2d 32 3b 0a 20   = pHint->n-2;. 
469d8 20 77 68 69 6c 65 28 20 69 3e 30 20 26 26 20 28   while( i>0 && (
469d9 70 48 69 6e 74 2d 3e 61 5b 69 2d 31 5d 20 26 20  pHint->a[i-1] & 
469da 30 78 38 30 29 20 29 20 69 2d 2d 3b 0a 20 20 77  0x80) ) i--;.  w
469db 68 69 6c 65 28 20 69 3e 30 20 26 26 20 28 70 48  hile( i>0 && (pH
469dc 69 6e 74 2d 3e 61 5b 69 2d 31 5d 20 26 20 30 78  int->a[i-1] & 0x
469dd 38 30 29 20 29 20 69 2d 2d 3b 0a 0a 20 20 70 48  80) ) i--;..  pH
469de 69 6e 74 2d 3e 6e 20 3d 20 69 3b 0a 20 20 69 20  int->n = i;.  i 
469df 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65  += sqlite3Fts3Ge
469e0 74 56 61 72 69 6e 74 28 26 70 48 69 6e 74 2d 3e  tVarint(&pHint->
469e1 61 5b 69 5d 2c 20 70 69 41 62 73 4c 65 76 65 6c  a[i], piAbsLevel
469e2 29 3b 0a 20 20 69 20 2b 3d 20 73 71 6c 69 74 65  );.  i += sqlite
469e3 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32  3Fts3GetVarint32
469e4 28 26 70 48 69 6e 74 2d 3e 61 5b 69 5d 2c 20 70  (&pHint->a[i], p
469e5 6e 49 6e 70 75 74 29 3b 0a 20 20 69 66 28 20 69  nInput);.  if( i
469e6 21 3d 6e 48 69 6e 74 20 29 20 72 65 74 75 72 6e  !=nHint ) return
469e7 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
469e8 56 54 41 42 3b 0a 0a 20 20 72 65 74 75 72 6e 20  VTAB;..  return 
469e9 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
469ea 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 61 6e 20  *.** Attempt an 
469eb 69 6e 63 72 65 6d 65 6e 74 61 6c 20 6d 65 72 67  incremental merg
469ec 65 20 74 68 61 74 20 77 72 69 74 65 73 20 6e 4d  e that writes nM
469ed 65 72 67 65 20 6c 65 61 66 20 62 6c 6f 63 6b 73  erge leaf blocks
469ee 2e 0a 2a 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e  ..**.** Incremen
469ef 74 61 6c 20 6d 65 72 67 65 73 20 68 61 70 70 65  tal merges happe
469f0 6e 20 6e 4d 69 6e 20 73 65 67 6d 65 6e 74 73 20  n nMin segments 
469f1 61 74 20 61 20 74 69 6d 65 2e 20 54 68 65 20 74  at a time. The t
469f2 77 6f 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20 74  wo.** segments t
469f3 6f 20 62 65 20 6d 65 72 67 65 64 20 61 72 65 20  o be merged are 
469f4 74 68 65 20 6e 4d 69 6e 20 6f 6c 64 65 73 74 20  the nMin oldest 
469f5 73 65 67 6d 65 6e 74 73 20 28 74 68 65 20 6f 6e  segments (the on
469f6 65 73 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 73  es with.** the s
469f7 6d 61 6c 6c 65 73 74 20 69 6e 64 65 78 65 73 29  mallest indexes)
469f8 20 69 6e 20 74 68 65 20 68 69 67 68 65 73 74 20   in the highest 
469f9 6c 65 76 65 6c 20 74 68 61 74 20 63 6f 6e 74 61  level that conta
469fa 69 6e 73 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20  ins at least.** 
469fb 6e 4d 69 6e 20 73 65 67 6d 65 6e 74 73 2e 20 4d  nMin segments. M
469fc 75 6c 74 69 70 6c 65 20 6d 65 72 67 65 73 20 6d  ultiple merges m
469fd 69 67 68 74 20 6f 63 63 75 72 20 69 6e 20 61 6e  ight occur in an
469fe 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74   attempt to writ
469ff 65 20 74 68 65 20 0a 2a 2a 20 71 75 6f 74 61 20  e the .** quota 
46a00 6f 66 20 6e 4d 65 72 67 65 20 6c 65 61 66 20 62  of nMerge leaf b
46a01 6c 6f 63 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  locks..*/.SQLITE
46a02 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
46a03 69 74 65 33 46 74 73 33 49 6e 63 72 6d 65 72 67  ite3Fts3Incrmerg
46a04 65 28 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20  e(Fts3Table *p, 
46a05 69 6e 74 20 6e 4d 65 72 67 65 2c 20 69 6e 74 20  int nMerge, int 
46a06 6e 4d 69 6e 29 7b 0a 20 20 69 6e 74 20 72 63 3b  nMin){.  int rc;
46a07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46a08 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
46a09 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74  rn code */.  int
46a0a 20 6e 52 65 6d 20 3d 20 6e 4d 65 72 67 65 3b 20   nRem = nMerge; 
46a0b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
46a0c 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 70  Number of leaf p
46a0d 61 67 65 73 20 79 65 74 20 74 6f 20 20 62 65 20  ages yet to  be 
46a0e 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 46 74 73  written */.  Fts
46a0f 33 4d 75 6c 74 69 53 65 67 52 65 61 64 65 72 20  3MultiSegReader 
46a10 2a 70 43 73 72 3b 20 20 20 20 20 20 20 2f 2a 20  *pCsr;       /* 
46a11 43 75 72 73 6f 72 20 75 73 65 64 20 74 6f 20 72  Cursor used to r
46a12 65 61 64 20 69 6e 70 75 74 20 64 61 74 61 20 2a  ead input data *
46a13 2f 0a 20 20 46 74 73 33 53 65 67 46 69 6c 74 65  /.  Fts3SegFilte
46a14 72 20 2a 70 46 69 6c 74 65 72 3b 20 20 20 20 20  r *pFilter;     
46a15 20 20 20 20 2f 2a 20 46 69 6c 74 65 72 20 75 73      /* Filter us
46a16 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 70  ed with cursor p
46a17 43 73 72 20 2a 2f 0a 20 20 49 6e 63 72 6d 65 72  Csr */.  Incrmer
46a18 67 65 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  geWriter *pWrite
46a19 72 3b 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74  r;       /* Writ
46a1a 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69  er object */.  i
46a1b 6e 74 20 6e 53 65 67 20 3d 20 30 3b 20 20 20 20  nt nSeg = 0;    
46a1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
46a1d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e 70 75  * Number of inpu
46a1e 74 20 73 65 67 6d 65 6e 74 73 20 2a 2f 0a 20 20  t segments */.  
46a1f 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 41  sqlite3_int64 iA
46a20 62 73 4c 65 76 65 6c 20 3d 20 30 3b 20 20 20 20  bsLevel = 0;    
46a21 2f 2a 20 41 62 73 6f 6c 75 74 65 20 6c 65 76 65  /* Absolute leve
46a22 6c 20 6e 75 6d 62 65 72 20 74 6f 20 77 6f 72 6b  l number to work
46a23 20 6f 6e 20 2a 2f 0a 20 20 42 6c 6f 62 20 68 69   on */.  Blob hi
46a24 6e 74 20 3d 20 7b 30 2c 20 30 2c 20 30 7d 3b 20  nt = {0, 0, 0}; 
46a25 20 20 20 20 20 20 20 20 20 2f 2a 20 48 69 6e 74           /* Hint
46a26 20 72 65 61 64 20 66 72 6f 6d 20 25 5f 73 74 61   read from %_sta
46a27 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  t table */.  int
46a28 20 62 44 69 72 74 79 48 69 6e 74 20 3d 20 30 3b   bDirtyHint = 0;
46a29 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
46a2a 54 72 75 65 20 69 66 20 62 6c 6f 62 20 27 68 69  True if blob 'hi
46a2b 6e 74 27 20 68 61 73 20 62 65 65 6e 20 6d 6f 64  nt' has been mod
46a2c 69 66 69 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 41  ified */..  /* A
46a2d 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f  llocate space fo
46a2e 72 20 74 68 65 20 63 75 72 73 6f 72 2c 20 66 69  r the cursor, fi
46a2f 6c 74 65 72 20 61 6e 64 20 77 72 69 74 65 72 20  lter and writer 
46a30 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 63 6f 6e  objects */.  con
46a31 73 74 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20  st int nAlloc = 
46a32 73 69 7a 65 6f 66 28 2a 70 43 73 72 29 20 2b 20  sizeof(*pCsr) + 
46a33 73 69 7a 65 6f 66 28 2a 70 46 69 6c 74 65 72 29  sizeof(*pFilter)
46a34 20 2b 20 73 69 7a 65 6f 66 28 2a 70 57 72 69 74   + sizeof(*pWrit
46a35 65 72 29 3b 0a 20 20 70 57 72 69 74 65 72 20 3d  er);.  pWriter =
46a36 20 28 49 6e 63 72 6d 65 72 67 65 57 72 69 74 65   (IncrmergeWrite
46a37 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  r *)sqlite3_mall
46a38 6f 63 28 6e 41 6c 6c 6f 63 29 3b 0a 20 20 69 66  oc(nAlloc);.  if
46a39 28 20 21 70 57 72 69 74 65 72 20 29 20 72 65 74  ( !pWriter ) ret
46a3a 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
46a3b 3b 0a 20 20 70 46 69 6c 74 65 72 20 3d 20 28 46  ;.  pFilter = (F
46a3c 74 73 33 53 65 67 46 69 6c 74 65 72 20 2a 29 26  ts3SegFilter *)&
46a3d 70 57 72 69 74 65 72 5b 31 5d 3b 0a 20 20 70 43  pWriter[1];.  pC
46a3e 73 72 20 3d 20 28 46 74 73 33 4d 75 6c 74 69 53  sr = (Fts3MultiS
46a3f 65 67 52 65 61 64 65 72 20 2a 29 26 70 46 69 6c  egReader *)&pFil
46a40 74 65 72 5b 31 5d 3b 0a 0a 20 20 72 63 20 3d 20  ter[1];..  rc = 
46a41 66 74 73 33 49 6e 63 72 6d 65 72 67 65 48 69 6e  fts3IncrmergeHin
46a42 74 4c 6f 61 64 28 70 2c 20 26 68 69 6e 74 29 3b  tLoad(p, &hint);
46a43 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
46a44 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 52 65 6d 3e  LITE_OK && nRem>
46a45 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69  0 ){.    const i
46a46 36 34 20 6e 4d 6f 64 20 3d 20 46 54 53 33 5f 53  64 nMod = FTS3_S
46a47 45 47 44 49 52 5f 4d 41 58 4c 45 56 45 4c 20 2a  EGDIR_MAXLEVEL *
46a48 20 70 2d 3e 6e 49 6e 64 65 78 3b 0a 20 20 20 20   p->nIndex;.    
46a49 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46  sqlite3_stmt *pF
46a4a 69 6e 64 4c 65 76 65 6c 20 3d 20 30 3b 20 2f 2a  indLevel = 0; /*
46a4b 20 53 51 4c 20 75 73 65 64 20 74 6f 20 64 65 74   SQL used to det
46a4c 65 72 6d 69 6e 65 20 69 41 62 73 4c 65 76 65 6c  ermine iAbsLevel
46a4d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62 55 73 65   */.    int bUse
46a4e 48 69 6e 74 20 3d 20 30 3b 20 20 20 20 20 20 20  Hint = 0;       
46a4f 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
46a50 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 61   attempting to a
46a51 70 70 65 6e 64 20 2a 2f 0a 0a 20 20 20 20 2f 2a  ppend */..    /*
46a52 20 53 65 61 72 63 68 20 74 68 65 20 25 5f 73 65   Search the %_se
46a53 67 64 69 72 20 74 61 62 6c 65 20 66 6f 72 20 74  gdir table for t
46a54 68 65 20 61 62 73 6f 6c 75 74 65 20 6c 65 76 65  he absolute leve
46a55 6c 20 77 69 74 68 20 74 68 65 20 73 6d 61 6c 6c  l with the small
46a56 65 73 74 0a 20 20 20 20 2a 2a 20 72 65 6c 61 74  est.    ** relat
46a57 69 76 65 20 6c 65 76 65 6c 20 6e 75 6d 62 65 72  ive level number
46a58 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61   that contains a
46a59 74 20 6c 65 61 73 74 20 6e 4d 69 6e 20 73 65 67  t least nMin seg
46a5a 6d 65 6e 74 73 2c 20 69 66 20 61 6e 79 2e 0a 20  ments, if any.. 
46a5b 20 20 20 2a 2a 20 49 66 20 6f 6e 65 20 69 73 20     ** If one is 
46a5c 66 6f 75 6e 64 2c 20 73 65 74 20 69 41 62 73 4c  found, set iAbsL
46a5d 65 76 65 6c 20 74 6f 20 74 68 65 20 61 62 73 6f  evel to the abso
46a5e 6c 75 74 65 20 6c 65 76 65 6c 20 6e 75 6d 62 65  lute level numbe
46a5f 72 20 61 6e 64 0a 20 20 20 20 2a 2a 20 6e 53 65  r and.    ** nSe
46a60 67 20 74 6f 20 6e 4d 69 6e 2e 20 49 66 20 6e 6f  g to nMin. If no
46a61 20 6c 65 76 65 6c 20 77 69 74 68 20 61 74 20 6c   level with at l
46a62 65 61 73 74 20 6e 4d 69 6e 20 73 65 67 6d 65 6e  east nMin segmen
46a63 74 73 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c  ts can be found,
46a64 20 0a 20 20 20 20 2a 2a 20 73 65 74 20 6e 53 65   .    ** set nSe
46a65 67 20 74 6f 20 2d 31 2e 0a 20 20 20 20 2a 2f 0a  g to -1..    */.
46a66 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c      rc = fts3Sql
46a67 53 74 6d 74 28 70 2c 20 53 51 4c 5f 46 49 4e 44  Stmt(p, SQL_FIND
46a68 5f 4d 45 52 47 45 5f 4c 45 56 45 4c 2c 20 26 70  _MERGE_LEVEL, &p
46a69 46 69 6e 64 4c 65 76 65 6c 2c 20 30 29 3b 0a 20  FindLevel, 0);. 
46a6a 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
46a6b 69 6e 74 28 70 46 69 6e 64 4c 65 76 65 6c 2c 20  int(pFindLevel, 
46a6c 31 2c 20 6e 4d 69 6e 29 3b 0a 20 20 20 20 69 66  1, nMin);.    if
46a6d 28 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70  ( sqlite3_step(p
46a6e 46 69 6e 64 4c 65 76 65 6c 29 3d 3d 53 51 4c 49  FindLevel)==SQLI
46a6f 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20  TE_ROW ){.      
46a70 69 41 62 73 4c 65 76 65 6c 20 3d 20 73 71 6c 69  iAbsLevel = sqli
46a71 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
46a72 28 70 46 69 6e 64 4c 65 76 65 6c 2c 20 30 29 3b  (pFindLevel, 0);
46a73 0a 20 20 20 20 20 20 6e 53 65 67 20 3d 20 6e 4d  .      nSeg = nM
46a74 69 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  in;.    }else{. 
46a75 20 20 20 20 20 6e 53 65 67 20 3d 20 2d 31 3b 0a       nSeg = -1;.
46a76 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
46a77 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 46 69  qlite3_reset(pFi
46a78 6e 64 4c 65 76 65 6c 29 3b 0a 0a 20 20 20 20 2f  ndLevel);..    /
46a79 2a 20 49 66 20 74 68 65 20 68 69 6e 74 20 72 65  * If the hint re
46a7a 61 64 20 66 72 6f 6d 20 74 68 65 20 25 5f 73 74  ad from the %_st
46a7b 61 74 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  at table is not 
46a7c 65 6d 70 74 79 2c 20 63 68 65 63 6b 20 69 66 20  empty, check if 
46a7d 74 68 65 0a 20 20 20 20 2a 2a 20 6c 61 73 74 20  the.    ** last 
46a7e 65 6e 74 72 79 20 69 6e 20 69 74 20 73 70 65 63  entry in it spec
46a7f 69 66 69 65 73 20 61 20 72 65 6c 61 74 69 76 65  ifies a relative
46a80 20 6c 65 76 65 6c 20 73 6d 61 6c 6c 65 72 20 74   level smaller t
46a81 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a 20 20 20  han or equal.   
46a82 20 2a 2a 20 74 6f 20 74 68 65 20 6c 65 76 65 6c   ** to the level
46a83 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74   identified by t
46a84 68 65 20 62 6c 6f 63 6b 20 61 62 6f 76 65 20 28  he block above (
46a85 69 66 20 61 6e 79 29 2e 20 49 66 20 73 6f 2c 20  if any). If so, 
46a86 74 68 69 73 20 0a 20 20 20 20 2a 2a 20 69 74 65  this .    ** ite
46a87 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f  ration of the lo
46a88 6f 70 20 77 69 6c 6c 20 77 6f 72 6b 20 6f 6e 20  op will work on 
46a89 6d 65 72 67 69 6e 67 20 61 74 20 74 68 65 20 68  merging at the h
46a8a 69 6e 74 65 64 20 6c 65 76 65 6c 2e 0a 20 20 20  inted level..   
46a8b 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
46a8c 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 68 69 6e  SQLITE_OK && hin
46a8d 74 2e 6e 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  t.n ){.      int
46a8e 20 6e 48 69 6e 74 20 3d 20 68 69 6e 74 2e 6e 3b   nHint = hint.n;
46a8f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69  .      sqlite3_i
46a90 6e 74 36 34 20 69 48 69 6e 74 41 62 73 4c 65 76  nt64 iHintAbsLev
46a91 65 6c 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  el = 0;      /* 
46a92 48 69 6e 74 20 6c 65 76 65 6c 20 2a 2f 0a 20 20  Hint level */.  
46a93 20 20 20 20 69 6e 74 20 6e 48 69 6e 74 53 65 67      int nHintSeg
46a94 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
46a95 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 69 6e            /* Hin
46a96 74 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 67 6d  t number of segm
46a97 65 6e 74 73 20 2a 2f 0a 0a 20 20 20 20 20 20 72  ents */..      r
46a98 63 20 3d 20 66 74 73 33 49 6e 63 72 6d 65 72 67  c = fts3Incrmerg
46a99 65 48 69 6e 74 50 6f 70 28 26 68 69 6e 74 2c 20  eHintPop(&hint, 
46a9a 26 69 48 69 6e 74 41 62 73 4c 65 76 65 6c 2c 20  &iHintAbsLevel, 
46a9b 26 6e 48 69 6e 74 53 65 67 29 3b 0a 20 20 20 20  &nHintSeg);.    
46a9c 20 20 69 66 28 20 6e 53 65 67 3c 30 20 7c 7c 20    if( nSeg<0 || 
46a9d 28 69 41 62 73 4c 65 76 65 6c 20 25 20 6e 4d 6f  (iAbsLevel % nMo
46a9e 64 29 20 3e 3d 20 28 69 48 69 6e 74 41 62 73 4c  d) >= (iHintAbsL
46a9f 65 76 65 6c 20 25 20 6e 4d 6f 64 29 20 29 7b 0a  evel % nMod) ){.
46aa0 20 20 20 20 20 20 20 20 69 41 62 73 4c 65 76 65          iAbsLeve
46aa1 6c 20 3d 20 69 48 69 6e 74 41 62 73 4c 65 76 65  l = iHintAbsLeve
46aa2 6c 3b 0a 20 20 20 20 20 20 20 20 6e 53 65 67 20  l;.        nSeg 
46aa3 3d 20 6e 48 69 6e 74 53 65 67 3b 0a 20 20 20 20  = nHintSeg;.    
46aa4 20 20 20 20 62 55 73 65 48 69 6e 74 20 3d 20 31      bUseHint = 1
46aa5 3b 0a 20 20 20 20 20 20 20 20 62 44 69 72 74 79  ;.        bDirty
46aa6 48 69 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  Hint = 1;.      
46aa7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
46aa8 2a 20 54 68 69 73 20 75 6e 64 6f 65 73 20 74 68  * This undoes th
46aa9 65 20 65 66 66 65 63 74 20 6f 66 20 74 68 65 20  e effect of the 
46aaa 48 69 6e 74 50 6f 70 28 29 20 61 62 6f 76 65 20  HintPop() above 
46aab 2d 20 73 6f 20 74 68 61 74 20 6e 6f 20 65 6e 74  - so that no ent
46aac 72 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73  ry.        ** is
46aad 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
46aae 65 20 68 69 6e 74 20 62 6c 6f 62 2e 20 20 2a 2f  e hint blob.  */
46aaf 0a 20 20 20 20 20 20 20 20 68 69 6e 74 2e 6e 20  .        hint.n 
46ab0 3d 20 6e 48 69 6e 74 3b 0a 20 20 20 20 20 20 7d  = nHint;.      }
46ab1 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
46ab2 66 20 6e 53 65 67 20 69 73 20 6c 65 73 73 20 74  f nSeg is less t
46ab3 68 61 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74  hat zero, then t
46ab4 68 65 72 65 20 69 73 20 6e 6f 20 6c 65 76 65 6c  here is no level
46ab5 20 77 69 74 68 20 61 74 20 6c 65 61 73 74 0a 20   with at least. 
46ab6 20 20 20 2a 2a 20 6e 4d 69 6e 20 73 65 67 6d 65     ** nMin segme
46ab7 6e 74 73 20 61 6e 64 20 6e 6f 20 68 69 6e 74 20  nts and no hint 
46ab8 69 6e 20 74 68 65 20 25 5f 73 74 61 74 20 74 61  in the %_stat ta
46ab9 62 6c 65 2e 20 4e 6f 20 77 6f 72 6b 20 74 6f 20  ble. No work to 
46aba 64 6f 2e 0a 20 20 20 20 2a 2a 20 45 78 69 74 20  do..    ** Exit 
46abb 65 61 72 6c 79 20 69 6e 20 74 68 69 73 20 63 61  early in this ca
46abc 73 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  se.  */.    if( 
46abd 6e 53 65 67 3c 30 20 29 20 62 72 65 61 6b 3b 0a  nSeg<0 ) break;.
46abe 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 61 20 63  .    /* Open a c
46abf 75 72 73 6f 72 20 74 6f 20 69 74 65 72 61 74 65  ursor to iterate
46ac0 20 74 68 72 6f 75 67 68 20 74 68 65 20 63 6f 6e   through the con
46ac1 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6f 6c 64  tents of the old
46ac2 65 73 74 20 6e 53 65 67 20 0a 20 20 20 20 2a 2a  est nSeg .    **
46ac3 20 69 6e 64 65 78 65 73 20 6f 66 20 61 62 73 6f   indexes of abso
46ac4 6c 75 74 65 20 6c 65 76 65 6c 20 69 41 62 73 4c  lute level iAbsL
46ac5 65 76 65 6c 2e 20 49 66 20 74 68 69 73 20 63 75  evel. If this cu
46ac6 72 73 6f 72 20 69 73 20 6f 70 65 6e 65 64 20 75  rsor is opened u
46ac7 73 69 6e 67 20 0a 20 20 20 20 2a 2a 20 74 68 65  sing .    ** the
46ac8 20 27 68 69 6e 74 27 20 70 61 72 61 6d 65 74 65   'hint' paramete
46ac9 72 73 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62  rs, it is possib
46aca 6c 65 20 74 68 61 74 20 74 68 65 72 65 20 61 72  le that there ar
46acb 65 20 6c 65 73 73 20 74 68 61 6e 20 6e 53 65 67  e less than nSeg
46acc 0a 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 73  .    ** segments
46acd 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 6c 65   available in le
46ace 76 65 6c 20 69 41 62 73 4c 65 76 65 6c 2e 20 49  vel iAbsLevel. I
46acf 6e 20 74 68 69 73 20 63 61 73 65 2c 20 6e 6f 20  n this case, no 
46ad0 77 6f 72 6b 20 69 73 0a 20 20 20 20 2a 2a 20 64  work is.    ** d
46ad1 6f 6e 65 20 6f 6e 20 69 41 62 73 4c 65 76 65 6c  one on iAbsLevel
46ad2 20 2d 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20   - fall through 
46ad3 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74 65 72  to the next iter
46ad4 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f  ation of the loo
46ad5 70 20 0a 20 20 20 20 2a 2a 20 74 6f 20 73 74 61  p .    ** to sta
46ad6 72 74 20 77 6f 72 6b 20 6f 6e 20 73 6f 6d 65 20  rt work on some 
46ad7 6f 74 68 65 72 20 6c 65 76 65 6c 2e 20 20 2a 2f  other level.  */
46ad8 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 57 72 69  .    memset(pWri
46ad9 74 65 72 2c 20 30 2c 20 6e 41 6c 6c 6f 63 29 3b  ter, 0, nAlloc);
46ada 0a 20 20 20 20 70 46 69 6c 74 65 72 2d 3e 66 6c  .    pFilter->fl
46adb 61 67 73 20 3d 20 46 54 53 33 5f 53 45 47 4d 45  ags = FTS3_SEGME
46adc 4e 54 5f 52 45 51 55 49 52 45 5f 50 4f 53 3b 0a  NT_REQUIRE_POS;.
46add 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
46ade 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
46adf 63 20 3d 20 66 74 73 33 49 6e 63 72 6d 65 72 67  c = fts3Incrmerg
46ae0 65 43 73 72 28 70 2c 20 69 41 62 73 4c 65 76 65  eCsr(p, iAbsLeve
46ae1 6c 2c 20 6e 53 65 67 2c 20 70 43 73 72 29 3b 0a  l, nSeg, pCsr);.
46ae2 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 53 51      }.    if( SQ
46ae3 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 70  LITE_OK==rc && p
46ae4 43 73 72 2d 3e 6e 53 65 67 6d 65 6e 74 3d 3d 6e  Csr->nSegment==n
46ae5 53 65 67 0a 20 20 20 20 20 26 26 20 53 51 4c 49  Seg.     && SQLI
46ae6 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
46ae7 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65  ite3Fts3SegReade
46ae8 72 53 74 61 72 74 28 70 2c 20 70 43 73 72 2c 20  rStart(p, pCsr, 
46ae9 70 46 69 6c 74 65 72 29 29 0a 20 20 20 20 20 26  pFilter)).     &
46aea 26 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 28 72  & SQLITE_ROW==(r
46aeb 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 53  c = sqlite3Fts3S
46aec 65 67 52 65 61 64 65 72 53 74 65 70 28 70 2c 20  egReaderStep(p, 
46aed 70 43 73 72 29 29 0a 20 20 20 20 29 7b 0a 20 20  pCsr)).    ){.  
46aee 20 20 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30      int iIdx = 0
46aef 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
46af0 2f 2a 20 4c 61 72 67 65 73 74 20 69 64 78 20 69  /* Largest idx i
46af1 6e 20 6c 65 76 65 6c 20 28 69 41 62 73 4c 65 76  n level (iAbsLev
46af2 65 6c 2b 31 29 20 2a 2f 0a 20 20 20 20 20 20 72  el+1) */.      r
46af3 63 20 3d 20 66 74 73 33 49 6e 63 72 6d 65 72 67  c = fts3Incrmerg
46af4 65 4f 75 74 70 75 74 49 64 78 28 70 2c 20 69 41  eOutputIdx(p, iA
46af5 62 73 4c 65 76 65 6c 2c 20 26 69 49 64 78 29 3b  bsLevel, &iIdx);
46af6 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
46af7 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
46af8 20 20 20 20 69 66 28 20 62 55 73 65 48 69 6e 74      if( bUseHint
46af9 20 26 26 20 69 49 64 78 3e 30 20 29 7b 0a 20 20   && iIdx>0 ){.  
46afa 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
46afb 61 72 20 2a 7a 4b 65 79 20 3d 20 70 43 73 72 2d  ar *zKey = pCsr-
46afc 3e 7a 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20  >zTerm;.        
46afd 20 20 69 6e 74 20 6e 4b 65 79 20 3d 20 70 43 73    int nKey = pCs
46afe 72 2d 3e 6e 54 65 72 6d 3b 0a 20 20 20 20 20 20  r->nTerm;.      
46aff 20 20 20 20 72 63 20 3d 20 66 74 73 33 49 6e 63      rc = fts3Inc
46b00 72 6d 65 72 67 65 4c 6f 61 64 28 70 2c 20 69 41  rmergeLoad(p, iA
46b01 62 73 4c 65 76 65 6c 2c 20 69 49 64 78 2d 31 2c  bsLevel, iIdx-1,
46b02 20 7a 4b 65 79 2c 20 6e 4b 65 79 2c 20 70 57 72   zKey, nKey, pWr
46b03 69 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d  iter);.        }
46b04 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
46b05 72 63 20 3d 20 66 74 73 33 49 6e 63 72 6d 65 72  rc = fts3Incrmer
46b06 67 65 57 72 69 74 65 72 28 70 2c 20 69 41 62 73  geWriter(p, iAbs
46b07 4c 65 76 65 6c 2c 20 69 49 64 78 2c 20 70 43 73  Level, iIdx, pCs
46b08 72 2c 20 70 57 72 69 74 65 72 29 3b 0a 20 20 20  r, pWriter);.   
46b09 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
46b0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
46b0b 4c 49 54 45 5f 4f 4b 20 26 26 20 70 57 72 69 74  LITE_OK && pWrit
46b0c 65 72 2d 3e 6e 4c 65 61 66 45 73 74 20 29 7b 0a  er->nLeafEst ){.
46b0d 20 20 20 20 20 20 20 20 66 74 73 33 4c 6f 67 4d          fts3LogM
46b0e 65 72 67 65 28 6e 53 65 67 2c 20 69 41 62 73 4c  erge(nSeg, iAbsL
46b0f 65 76 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 64  evel);.        d
46b10 6f 20 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63  o {.          rc
46b11 20 3d 20 66 74 73 33 49 6e 63 72 6d 65 72 67 65   = fts3Incrmerge
46b12 41 70 70 65 6e 64 28 70 2c 20 70 57 72 69 74 65  Append(p, pWrite
46b13 72 2c 20 70 43 73 72 29 3b 0a 20 20 20 20 20 20  r, pCsr);.      
46b14 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
46b15 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 73 71 6c  TE_OK ) rc = sql
46b16 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65  ite3Fts3SegReade
46b17 72 53 74 65 70 28 70 2c 20 70 43 73 72 29 3b 0a  rStep(p, pCsr);.
46b18 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 57            if( pW
46b19 72 69 74 65 72 2d 3e 6e 57 6f 72 6b 3e 3d 6e 52  riter->nWork>=nR
46b1a 65 6d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  em && rc==SQLITE
46b1b 5f 52 4f 57 20 29 20 72 63 20 3d 20 53 51 4c 49  _ROW ) rc = SQLI
46b1c 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d  TE_OK;.        }
46b1d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
46b1e 45 5f 52 4f 57 20 29 3b 0a 0a 20 20 20 20 20 20  E_ROW );..      
46b1f 20 20 2f 2a 20 55 70 64 61 74 65 20 6f 72 20 64    /* Update or d
46b20 65 6c 65 74 65 20 74 68 65 20 69 6e 70 75 74 20  elete the input 
46b21 73 65 67 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  segments */.    
46b22 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
46b23 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
46b24 20 20 20 6e 52 65 6d 20 2d 3d 20 28 31 20 2b 20     nRem -= (1 + 
46b25 70 57 72 69 74 65 72 2d 3e 6e 57 6f 72 6b 29 3b  pWriter->nWork);
46b26 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
46b27 66 74 73 33 49 6e 63 72 6d 65 72 67 65 43 68 6f  fts3IncrmergeCho
46b28 6d 70 28 70 2c 20 69 41 62 73 4c 65 76 65 6c 2c  mp(p, iAbsLevel,
46b29 20 70 43 73 72 2c 20 26 6e 53 65 67 29 3b 0a 20   pCsr, &nSeg);. 
46b2a 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 53 65           if( nSe
46b2b 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  g!=0 ){.        
46b2c 20 20 20 20 62 44 69 72 74 79 48 69 6e 74 20 3d      bDirtyHint =
46b2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
46b2e 66 74 73 33 49 6e 63 72 6d 65 72 67 65 48 69 6e  fts3IncrmergeHin
46b2f 74 50 75 73 68 28 26 68 69 6e 74 2c 20 69 41 62  tPush(&hint, iAb
46b30 73 4c 65 76 65 6c 2c 20 6e 53 65 67 2c 20 26 72  sLevel, nSeg, &r
46b31 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  c);.          }.
46b32 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
46b33 7d 0a 0a 20 20 20 20 20 20 66 74 73 33 49 6e 63  }..      fts3Inc
46b34 72 6d 65 72 67 65 52 65 6c 65 61 73 65 28 70 2c  rmergeRelease(p,
46b35 20 70 57 72 69 74 65 72 2c 20 26 72 63 29 3b 0a   pWriter, &rc);.
46b36 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
46b37 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72 46  e3Fts3SegReaderF
46b38 69 6e 69 73 68 28 70 43 73 72 29 3b 0a 20 20 7d  inish(pCsr);.  }
46b39 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65  ..  /* Write the
46b3a 20 68 69 6e 74 20 76 61 6c 75 65 73 20 69 6e 74   hint values int
46b3b 6f 20 74 68 65 20 25 5f 73 74 61 74 20 74 61 62  o the %_stat tab
46b3c 6c 65 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20  le for the next 
46b3d 69 6e 63 72 2d 6d 65 72 67 65 72 20 2a 2f 0a 20  incr-merger */. 
46b3e 20 69 66 28 20 62 44 69 72 74 79 48 69 6e 74 20   if( bDirtyHint 
46b3f 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
46b40 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 74 73   ){.    rc = fts
46b41 33 49 6e 63 72 6d 65 72 67 65 48 69 6e 74 53 74  3IncrmergeHintSt
46b42 6f 72 65 28 70 2c 20 26 68 69 6e 74 29 3b 0a 20  ore(p, &hint);. 
46b43 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72   }..  sqlite3_fr
46b44 65 65 28 70 57 72 69 74 65 72 29 3b 0a 20 20 73  ee(pWriter);.  s
46b45 71 6c 69 74 65 33 5f 66 72 65 65 28 68 69 6e 74  qlite3_free(hint
46b46 2e 61 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  .a);.  return rc
46b47 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
46b48 72 74 20 74 68 65 20 74 65 78 74 20 62 65 67 69  rt the text begi
46b49 6e 6e 69 6e 67 20 61 74 20 2a 70 7a 20 69 6e 74  nning at *pz int
46b4a 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64  o an integer and
46b4b 20 72 65 74 75 72 6e 0a 2a 2a 20 69 74 73 20 76   return.** its v
46b4c 61 6c 75 65 2e 20 20 41 64 76 61 6e 63 65 20 2a  alue.  Advance *
46b4d 70 7a 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  pz to point to t
46b4e 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74  he first charact
46b4f 65 72 20 70 61 73 74 0a 2a 2a 20 74 68 65 20 69  er past.** the i
46b50 6e 74 65 67 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  nteger..*/.stati
46b51 63 20 69 6e 74 20 66 74 73 33 47 65 74 69 6e 74  c int fts3Getint
46b52 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a  (const char **pz
46b53 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
46b54 2a 7a 20 3d 20 2a 70 7a 3b 0a 20 20 69 6e 74 20  *z = *pz;.  int 
46b55 69 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  i = 0;.  while( 
46b56 28 2a 7a 29 3e 3d 27 30 27 20 26 26 20 28 2a 7a  (*z)>='0' && (*z
46b57 29 3c 3d 27 39 27 20 29 20 69 20 3d 20 31 30 2a  )<='9' ) i = 10*
46b58 69 20 2b 20 2a 28 7a 2b 2b 29 20 2d 20 27 30 27  i + *(z++) - '0'
46b59 3b 0a 20 20 2a 70 7a 20 3d 20 7a 3b 0a 20 20 72  ;.  *pz = z;.  r
46b5a 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn i;.}../*.*
46b5b 2a 20 50 72 6f 63 65 73 73 20 73 74 61 74 65 6d  * Process statem
46b5c 65 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ents of the form
46b5d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 49 4e 53 45 52  :.**.**    INSER
46b5e 54 20 49 4e 54 4f 20 74 61 62 6c 65 28 74 61 62  T INTO table(tab
46b5f 6c 65 29 20 56 41 4c 55 45 53 28 27 6d 65 72 67  le) VALUES('merg
46b60 65 3d 41 2c 42 27 29 3b 0a 2a 2a 0a 2a 2a 20 41  e=A,B');.**.** A
46b61 20 61 6e 64 20 42 20 61 72 65 20 69 6e 74 65 67   and B are integ
46b62 65 72 73 20 74 68 61 74 20 64 65 63 6f 64 65 20  ers that decode 
46b63 74 6f 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72  to be the number
46b64 20 6f 66 20 6c 65 61 66 20 70 61 67 65 73 0a 2a   of leaf pages.*
46b65 2a 20 77 72 69 74 74 65 6e 20 66 6f 72 20 74 68  * written for th
46b66 65 20 6d 65 72 67 65 2c 20 61 6e 64 20 74 68 65  e merge, and the
46b67 20 6d 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20   minimum number 
46b68 6f 66 20 73 65 67 6d 65 6e 74 73 20 6f 6e 20 61  of segments on a
46b69 20 6c 65 76 65 6c 0a 2a 2a 20 62 65 66 6f 72 65   level.** before
46b6a 20 69 74 20 77 69 6c 6c 20 62 65 20 73 65 6c 65   it will be sele
46b6b 63 74 65 64 20 66 6f 72 20 61 20 6d 65 72 67 65  cted for a merge
46b6c 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a  , respectively..
46b6d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
46b6e 73 33 44 6f 49 6e 63 72 6d 65 72 67 65 28 0a 20  s3DoIncrmerge(. 
46b6f 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20 20   Fts3Table *p,  
46b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46b71 20 2f 2a 20 46 54 53 33 20 74 61 62 6c 65 20 68   /* FTS3 table h
46b72 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
46b73 20 63 68 61 72 20 2a 7a 50 61 72 61 6d 20 20 20   char *zParam   
46b74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
46b75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72  l-terminated str
46b76 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 22  ing containing "
46b77 41 2c 42 22 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  A,B" */.){.  int
46b78 20 72 63 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 20   rc;.  int nMin 
46b79 3d 20 28 46 54 53 33 5f 4d 45 52 47 45 5f 43 4f  = (FTS3_MERGE_CO
46b7a 55 4e 54 20 2f 20 32 29 3b 0a 20 20 69 6e 74 20  UNT / 2);.  int 
46b7b 6e 4d 65 72 67 65 20 3d 20 30 3b 0a 20 20 63 6f  nMerge = 0;.  co
46b7c 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 7a 50  nst char *z = zP
46b7d 61 72 61 6d 3b 0a 0a 20 20 2f 2a 20 52 65 61 64  aram;..  /* Read
46b7e 20 74 68 65 20 66 69 72 73 74 20 69 6e 74 65 67   the first integ
46b7f 65 72 20 76 61 6c 75 65 20 2a 2f 0a 20 20 6e 4d  er value */.  nM
46b80 65 72 67 65 20 3d 20 66 74 73 33 47 65 74 69 6e  erge = fts3Getin
46b81 74 28 26 7a 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  t(&z);..  /* If 
46b82 74 68 65 20 66 69 72 73 74 20 69 6e 74 65 67 65  the first intege
46b83 72 20 76 61 6c 75 65 20 69 73 20 66 6f 6c 6c 6f  r value is follo
46b84 77 65 64 20 62 79 20 61 20 27 2c 27 2c 20 20 72  wed by a ',',  r
46b85 65 61 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 20  ead the second. 
46b86 20 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75   ** integer valu
46b87 65 2e 20 2a 2f 0a 20 20 69 66 28 20 7a 5b 30 5d  e. */.  if( z[0]
46b88 3d 3d 27 2c 27 20 26 26 20 7a 5b 31 5d 21 3d 27  ==',' && z[1]!='
46b89 5c 30 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a  \0' ){.    z++;.
46b8a 20 20 20 20 6e 4d 69 6e 20 3d 20 66 74 73 33 47      nMin = fts3G
46b8b 65 74 69 6e 74 28 26 7a 29 3b 0a 20 20 7d 0a 0a  etint(&z);.  }..
46b8c 20 20 69 66 28 20 7a 5b 30 5d 21 3d 27 5c 30 27    if( z[0]!='\0'
46b8d 20 7c 7c 20 6e 4d 69 6e 3c 32 20 29 7b 0a 20 20   || nMin<2 ){.  
46b8e 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
46b8f 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ROR;.  }else{.  
46b90 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
46b91 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 62 48  ;.    if( !p->bH
46b92 61 73 53 74 61 74 20 29 7b 0a 20 20 20 20 20 20  asStat ){.      
46b93 61 73 73 65 72 74 28 20 70 2d 3e 62 46 74 73 34  assert( p->bFts4
46b94 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
46b95 69 74 65 33 46 74 73 33 43 72 65 61 74 65 53 74  ite3Fts3CreateSt
46b96 61 74 54 61 62 6c 65 28 26 72 63 2c 20 70 29 3b  atTable(&rc, p);
46b97 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
46b98 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
46b99 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
46b9a 65 33 46 74 73 33 49 6e 63 72 6d 65 72 67 65 28  e3Fts3Incrmerge(
46b9b 70 2c 20 6e 4d 65 72 67 65 2c 20 6e 4d 69 6e 29  p, nMerge, nMin)
46b9c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
46b9d 74 65 33 46 74 73 33 53 65 67 6d 65 6e 74 73 43  te3Fts3SegmentsC
46b9e 6c 6f 73 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72  lose(p);.  }.  r
46b9f 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
46ba0 2a 2a 20 50 72 6f 63 65 73 73 20 73 74 61 74 65  ** Process state
46ba1 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72  ments of the for
46ba2 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 49 4e 53 45  m:.**.**    INSE
46ba3 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 28 74 61  RT INTO table(ta
46ba4 62 6c 65 29 20 56 41 4c 55 45 53 28 27 61 75 74  ble) VALUES('aut
46ba5 6f 6d 65 72 67 65 3d 58 27 29 3b 0a 2a 2a 0a 2a  omerge=X');.**.*
46ba6 2a 20 77 68 65 72 65 20 58 20 69 73 20 61 6e 20  * where X is an 
46ba7 69 6e 74 65 67 65 72 2e 20 20 58 3d 3d 30 20 6d  integer.  X==0 m
46ba8 65 61 6e 73 20 74 6f 20 74 75 72 6e 20 61 75 74  eans to turn aut
46ba9 6f 6d 65 72 67 65 20 6f 66 66 2e 20 20 58 21 3d  omerge off.  X!=
46baa 30 20 6d 65 61 6e 73 0a 2a 2a 20 74 75 72 6e 20  0 means.** turn 
46bab 69 74 20 6f 6e 2e 20 20 54 68 65 20 73 65 74 74  it on.  The sett
46bac 69 6e 67 20 69 73 20 70 65 72 73 69 73 74 65 6e  ing is persisten
46bad 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
46bae 20 66 74 73 33 44 6f 41 75 74 6f 69 6e 63 72 6d   fts3DoAutoincrm
46baf 65 72 67 65 28 0a 20 20 46 74 73 33 54 61 62 6c  erge(.  Fts3Tabl
46bb0 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  e *p,           
46bb1 20 20 20 20 20 20 20 20 2f 2a 20 46 54 53 33 20          /* FTS3 
46bb2 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  table handle */.
46bb3 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
46bb4 61 72 61 6d 20 20 20 20 20 20 20 20 20 20 20 20  aram            
46bb5 20 20 2f 2a 20 4e 75 6c 2d 74 65 72 6d 69 6e 61    /* Nul-termina
46bb6 74 65 64 20 73 74 72 69 6e 67 20 63 6f 6e 74 61  ted string conta
46bb7 69 6e 69 6e 67 20 62 6f 6f 6c 65 61 6e 20 2a 2f  ining boolean */
46bb8 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
46bb9 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69  QLITE_OK;.  sqli
46bba 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 20  te3_stmt *pStmt 
46bbb 3d 20 30 3b 0a 20 20 70 2d 3e 62 41 75 74 6f 69  = 0;.  p->bAutoi
46bbc 6e 63 72 6d 65 72 67 65 20 3d 20 66 74 73 33 47  ncrmerge = fts3G
46bbd 65 74 69 6e 74 28 26 7a 50 61 72 61 6d 29 21 3d  etint(&zParam)!=
46bbe 30 3b 0a 20 20 69 66 28 20 21 70 2d 3e 62 48 61  0;.  if( !p->bHa
46bbf 73 53 74 61 74 20 29 7b 0a 20 20 20 20 61 73 73  sStat ){.    ass
46bc0 65 72 74 28 20 70 2d 3e 62 46 74 73 34 3d 3d 30  ert( p->bFts4==0
46bc1 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46   );.    sqlite3F
46bc2 74 73 33 43 72 65 61 74 65 53 74 61 74 54 61 62  ts3CreateStatTab
46bc3 6c 65 28 26 72 63 2c 20 70 29 3b 0a 20 20 20 20  le(&rc, p);.    
46bc4 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
46bc5 72 63 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 66  rc;.  }.  rc = f
46bc6 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51  ts3SqlStmt(p, SQ
46bc7 4c 5f 52 45 50 4c 41 43 45 5f 53 54 41 54 2c 20  L_REPLACE_STAT, 
46bc8 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66  &pStmt, 0);.  if
46bc9 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
46bca 3b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  ;;.  sqlite3_bin
46bcb 64 5f 69 6e 74 28 70 53 74 6d 74 2c 20 31 2c 20  d_int(pStmt, 1, 
46bcc 46 54 53 5f 53 54 41 54 5f 41 55 54 4f 49 4e 43  FTS_STAT_AUTOINC
46bcd 52 4d 45 52 47 45 29 3b 0a 20 20 73 71 6c 69 74  RMERGE);.  sqlit
46bce 65 33 5f 62 69 6e 64 5f 69 6e 74 28 70 53 74 6d  e3_bind_int(pStm
46bcf 74 2c 20 32 2c 20 70 2d 3e 62 41 75 74 6f 69 6e  t, 2, p->bAutoin
46bd0 63 72 6d 65 72 67 65 29 3b 0a 20 20 73 71 6c 69  crmerge);.  sqli
46bd1 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3b  te3_step(pStmt);
46bd2 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
46bd3 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20  reset(pStmt);.  
46bd4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
46bd5 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 36 34 2d  .** Return a 64-
46bd6 62 69 74 20 63 68 65 63 6b 73 75 6d 20 66 6f 72  bit checksum for
46bd7 20 74 68 65 20 46 54 53 20 69 6e 64 65 78 20 65   the FTS index e
46bd8 6e 74 72 79 20 73 70 65 63 69 66 69 65 64 20 62  ntry specified b
46bd9 79 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e  y the.** argumen
46bda 74 73 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  ts to this funct
46bdb 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75  ion..*/.static u
46bdc 36 34 20 66 74 73 33 43 68 65 63 6b 73 75 6d 45  64 fts3ChecksumE
46bdd 6e 74 72 79 28 0a 20 20 63 6f 6e 73 74 20 63 68  ntry(.  const ch
46bde 61 72 20 2a 7a 54 65 72 6d 2c 20 20 20 20 20 20  ar *zTerm,      
46bdf 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
46be0 65 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e  er to buffer con
46be1 74 61 69 6e 69 6e 67 20 74 65 72 6d 20 2a 2f 0a  taining term */.
46be2 20 20 69 6e 74 20 6e 54 65 72 6d 2c 20 20 20 20    int nTerm,    
46be3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46be4 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 54 65    /* Size of zTe
46be5 72 6d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  rm in bytes */. 
46be6 20 69 6e 74 20 69 4c 61 6e 67 69 64 2c 20 20 20   int iLangid,   
46be7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46be8 20 2f 2a 20 4c 61 6e 67 75 61 67 65 20 69 64 20   /* Language id 
46be9 66 6f 72 20 63 75 72 72 65 6e 74 20 72 6f 77 20  for current row 
46bea 2a 2f 0a 20 20 69 6e 74 20 69 49 6e 64 65 78 2c  */.  int iIndex,
46beb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46bec 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 28 30       /* Index (0
46bed 2e 2e 46 74 73 33 54 61 62 6c 65 2e 6e 49 6e 64  ..Fts3Table.nInd
46bee 65 78 2d 31 29 20 2a 2f 0a 20 20 69 36 34 20 69  ex-1) */.  i64 i
46bef 44 6f 63 69 64 2c 20 20 20 20 20 20 20 20 20 20  Docid,          
46bf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f             /* Do
46bf1 63 69 64 20 66 6f 72 20 63 75 72 72 65 6e 74 20  cid for current 
46bf2 72 6f 77 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 43  row. */.  int iC
46bf3 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ol,             
46bf4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
46bf5 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  umn number */.  
46bf6 69 6e 74 20 69 50 6f 73 20 20 20 20 20 20 20 20  int iPos        
46bf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46bf8 2f 2a 20 50 6f 73 69 74 69 6f 6e 20 2a 2f 0a 29  /* Position */.)
46bf9 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 75 36 34  {.  int i;.  u64
46bfa 20 72 65 74 20 3d 20 28 75 36 34 29 69 44 6f 63   ret = (u64)iDoc
46bfb 69 64 3b 0a 0a 20 20 72 65 74 20 2b 3d 20 28 72  id;..  ret += (r
46bfc 65 74 3c 3c 33 29 20 2b 20 69 4c 61 6e 67 69 64  et<<3) + iLangid
46bfd 3b 0a 20 20 72 65 74 20 2b 3d 20 28 72 65 74 3c  ;.  ret += (ret<
46bfe 3c 33 29 20 2b 20 69 49 6e 64 65 78 3b 0a 20 20  <3) + iIndex;.  
46bff 72 65 74 20 2b 3d 20 28 72 65 74 3c 3c 33 29 20  ret += (ret<<3) 
46c00 2b 20 69 43 6f 6c 3b 0a 20 20 72 65 74 20 2b 3d  + iCol;.  ret +=
46c01 20 28 72 65 74 3c 3c 33 29 20 2b 20 69 50 6f 73   (ret<<3) + iPos
46c02 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
46c03 54 65 72 6d 3b 20 69 2b 2b 29 20 72 65 74 20 2b  Term; i++) ret +
46c04 3d 20 28 72 65 74 3c 3c 33 29 20 2b 20 7a 54 65  = (ret<<3) + zTe
46c05 72 6d 5b 69 5d 3b 0a 0a 20 20 72 65 74 75 72 6e  rm[i];..  return
46c06 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   ret;.}../*.** R
46c07 65 74 75 72 6e 20 61 20 63 68 65 63 6b 73 75 6d  eturn a checksum
46c08 20 6f 66 20 61 6c 6c 20 65 6e 74 72 69 65 73 20   of all entries 
46c09 69 6e 20 74 68 65 20 46 54 53 20 69 6e 64 65 78  in the FTS index
46c0a 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64   that correspond
46c0b 20 74 6f 0a 2a 2a 20 6c 61 6e 67 75 61 67 65 20   to.** language 
46c0c 69 64 20 69 4c 61 6e 67 69 64 2e 20 54 68 65 20  id iLangid. The 
46c0d 63 68 65 63 6b 73 75 6d 20 69 73 20 63 61 6c 63  checksum is calc
46c0e 75 6c 61 74 65 64 20 62 79 20 58 4f 52 69 6e 67  ulated by XORing
46c0f 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 0a 2a   the checksums.*
46c10 2a 20 6f 66 20 65 61 63 68 20 69 6e 64 69 76 69  * of each indivi
46c11 64 75 61 6c 20 65 6e 74 72 79 20 28 73 65 65 20  dual entry (see 
46c12 66 74 73 33 43 68 65 63 6b 73 75 6d 45 6e 74 72  fts3ChecksumEntr
46c13 79 28 29 29 20 74 6f 67 65 74 68 65 72 2e 0a 2a  y()) together..*
46c14 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66  *.** If successf
46c15 75 6c 2c 20 74 68 65 20 63 68 65 63 6b 73 75 6d  ul, the checksum
46c16 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e   value is return
46c17 65 64 20 61 6e 64 20 2a 70 52 63 20 73 65 74 20  ed and *pRc set 
46c18 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a  to SQLITE_OK..**
46c19 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61   Otherwise, if a
46c1a 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
46c1b 2a 70 52 63 20 69 73 20 73 65 74 20 74 6f 20 61  *pRc is set to a
46c1c 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
46c1d 6f 64 65 2e 20 54 68 65 0a 2a 2a 20 72 65 74 75  ode. The.** retu
46c1e 72 6e 20 76 61 6c 75 65 20 69 73 20 75 6e 64 65  rn value is unde
46c1f 66 69 6e 65 64 20 69 6e 20 74 68 69 73 20 63 61  fined in this ca
46c20 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 36  se..*/.static u6
46c21 34 20 66 74 73 33 43 68 65 63 6b 73 75 6d 49 6e  4 fts3ChecksumIn
46c22 64 65 78 28 0a 20 20 46 74 73 33 54 61 62 6c 65  dex(.  Fts3Table
46c23 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
46c24 20 20 20 20 20 20 20 2f 2a 20 46 54 53 33 20 74         /* FTS3 t
46c25 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  able handle */. 
46c26 20 69 6e 74 20 69 4c 61 6e 67 69 64 2c 20 20 20   int iLangid,   
46c27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46c28 20 2f 2a 20 4c 61 6e 67 75 61 67 65 20 69 64 20   /* Language id 
46c29 74 6f 20 72 65 74 75 72 6e 20 63 6b 73 75 6d 20  to return cksum 
46c2a 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69 49 6e  for */.  int iIn
46c2b 64 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  dex,            
46c2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
46c2d 78 20 74 6f 20 63 6b 73 75 6d 20 28 30 2e 2e 70  x to cksum (0..p
46c2e 2d 3e 6e 49 6e 64 65 78 2d 31 29 20 2a 2f 0a 20  ->nIndex-1) */. 
46c2f 20 69 6e 74 20 2a 70 52 63 20 20 20 20 20 20 20   int *pRc       
46c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46c31 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20   /* OUT: Return 
46c32 63 6f 64 65 20 2a 2f 0a 29 7b 0a 20 20 46 74 73  code */.){.  Fts
46c33 33 53 65 67 46 69 6c 74 65 72 20 66 69 6c 74 65  3SegFilter filte
46c34 72 3b 0a 20 20 46 74 73 33 4d 75 6c 74 69 53 65  r;.  Fts3MultiSe
46c35 67 52 65 61 64 65 72 20 63 73 72 3b 0a 20 20 69  gReader csr;.  i
46c36 6e 74 20 72 63 3b 0a 20 20 75 36 34 20 63 6b 73  nt rc;.  u64 cks
46c37 75 6d 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  um = 0;..  asser
46c38 74 28 20 2a 70 52 63 3d 3d 53 51 4c 49 54 45 5f  t( *pRc==SQLITE_
46c39 4f 4b 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28  OK );..  memset(
46c3a 26 66 69 6c 74 65 72 2c 20 30 2c 20 73 69 7a 65  &filter, 0, size
46c3b 6f 66 28 66 69 6c 74 65 72 29 29 3b 0a 20 20 6d  of(filter));.  m
46c3c 65 6d 73 65 74 28 26 63 73 72 2c 20 30 2c 20 73  emset(&csr, 0, s
46c3d 69 7a 65 6f 66 28 63 73 72 29 29 3b 0a 20 20 66  izeof(csr));.  f
46c3e 69 6c 74 65 72 2e 66 6c 61 67 73 20 3d 20 20 46  ilter.flags =  F
46c3f 54 53 33 5f 53 45 47 4d 45 4e 54 5f 52 45 51 55  TS3_SEGMENT_REQU
46c40 49 52 45 5f 50 4f 53 7c 46 54 53 33 5f 53 45 47  IRE_POS|FTS3_SEG
46c41 4d 45 4e 54 5f 49 47 4e 4f 52 45 5f 45 4d 50 54  MENT_IGNORE_EMPT
46c42 59 3b 0a 20 20 66 69 6c 74 65 72 2e 66 6c 61 67  Y;.  filter.flag
46c43 73 20 7c 3d 20 46 54 53 33 5f 53 45 47 4d 45 4e  s |= FTS3_SEGMEN
46c44 54 5f 53 43 41 4e 3b 0a 0a 20 20 72 63 20 3d 20  T_SCAN;..  rc = 
46c45 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65  sqlite3Fts3SegRe
46c46 61 64 65 72 43 75 72 73 6f 72 28 0a 20 20 20 20  aderCursor(.    
46c47 20 20 70 2c 20 69 4c 61 6e 67 69 64 2c 20 69 49    p, iLangid, iI
46c48 6e 64 65 78 2c 20 46 54 53 33 5f 53 45 47 43 55  ndex, FTS3_SEGCU
46c49 52 53 4f 52 5f 41 4c 4c 2c 20 30 2c 20 30 2c 20  RSOR_ALL, 0, 0, 
46c4a 30 2c 20 31 2c 26 63 73 72 0a 20 20 29 3b 0a 20  0, 1,&csr.  );. 
46c4b 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
46c4c 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  OK ){.    rc = s
46c4d 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61  qlite3Fts3SegRea
46c4e 64 65 72 53 74 61 72 74 28 70 2c 20 26 63 73 72  derStart(p, &csr
46c4f 2c 20 26 66 69 6c 74 65 72 29 3b 0a 20 20 7d 0a  , &filter);.  }.
46c50 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
46c51 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 77 68 69 6c  E_OK ){.    whil
46c52 65 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 28  e( SQLITE_ROW==(
46c53 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  rc = sqlite3Fts3
46c54 53 65 67 52 65 61 64 65 72 53 74 65 70 28 70 2c  SegReaderStep(p,
46c55 20 26 63 73 72 29 29 20 29 7b 0a 20 20 20 20 20   &csr)) ){.     
46c56 20 63 68 61 72 20 2a 70 43 73 72 20 3d 20 63 73   char *pCsr = cs
46c57 72 2e 61 44 6f 63 6c 69 73 74 3b 0a 20 20 20 20  r.aDoclist;.    
46c58 20 20 63 68 61 72 20 2a 70 45 6e 64 20 3d 20 26    char *pEnd = &
46c59 70 43 73 72 5b 63 73 72 2e 6e 44 6f 63 6c 69 73  pCsr[csr.nDoclis
46c5a 74 5d 3b 0a 0a 20 20 20 20 20 20 69 36 34 20 69  t];..      i64 i
46c5b 44 6f 63 69 64 20 3d 20 30 3b 0a 20 20 20 20 20  Docid = 0;.     
46c5c 20 69 36 34 20 69 43 6f 6c 20 3d 20 30 3b 0a 20   i64 iCol = 0;. 
46c5d 20 20 20 20 20 69 36 34 20 69 50 6f 73 20 3d 20       i64 iPos = 
46c5e 30 3b 0a 0a 20 20 20 20 20 20 70 43 73 72 20 2b  0;..      pCsr +
46c5f 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  = sqlite3Fts3Get
46c60 56 61 72 69 6e 74 28 70 43 73 72 2c 20 26 69 44  Varint(pCsr, &iD
46c61 6f 63 69 64 29 3b 0a 20 20 20 20 20 20 77 68 69  ocid);.      whi
46c62 6c 65 28 20 70 43 73 72 3c 70 45 6e 64 20 29 7b  le( pCsr<pEnd ){
46c63 0a 20 20 20 20 20 20 20 20 69 36 34 20 69 56 61  .        i64 iVa
46c64 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  l = 0;.        p
46c65 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 46 74  Csr += sqlite3Ft
46c66 73 33 47 65 74 56 61 72 69 6e 74 28 70 43 73 72  s3GetVarint(pCsr
46c67 2c 20 26 69 56 61 6c 29 3b 0a 20 20 20 20 20 20  , &iVal);.      
46c68 20 20 69 66 28 20 70 43 73 72 3c 70 45 6e 64 20    if( pCsr<pEnd 
46c69 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
46c6a 20 69 56 61 6c 3d 3d 30 20 7c 7c 20 69 56 61 6c   iVal==0 || iVal
46c6b 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==1 ){.         
46c6c 20 20 20 69 43 6f 6c 20 3d 20 30 3b 0a 20 20 20     iCol = 0;.   
46c6d 20 20 20 20 20 20 20 20 20 69 50 6f 73 20 3d 20           iPos = 
46c6e 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  0;.            i
46c6f 66 28 20 69 56 61 6c 20 29 7b 0a 20 20 20 20 20  f( iVal ){.     
46c70 20 20 20 20 20 20 20 20 20 70 43 73 72 20 2b 3d           pCsr +=
46c71 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56   sqlite3Fts3GetV
46c72 61 72 69 6e 74 28 70 43 73 72 2c 20 26 69 43 6f  arint(pCsr, &iCo
46c73 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  l);.            
46c74 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
46c75 20 20 20 20 20 70 43 73 72 20 2b 3d 20 73 71 6c       pCsr += sql
46c76 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e  ite3Fts3GetVarin
46c77 74 28 70 43 73 72 2c 20 26 69 56 61 6c 29 3b 0a  t(pCsr, &iVal);.
46c78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 44                iD
46c79 6f 63 69 64 20 2b 3d 20 69 56 61 6c 3b 0a 20 20  ocid += iVal;.  
46c7a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
46c7b 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
46c7c 20 20 20 20 20 20 20 20 20 69 50 6f 73 20 2b 3d           iPos +=
46c7d 20 28 69 56 61 6c 20 2d 20 32 29 3b 0a 20 20 20   (iVal - 2);.   
46c7e 20 20 20 20 20 20 20 20 20 63 6b 73 75 6d 20 3d           cksum =
46c7f 20 63 6b 73 75 6d 20 5e 20 66 74 73 33 43 68 65   cksum ^ fts3Che
46c80 63 6b 73 75 6d 45 6e 74 72 79 28 0a 20 20 20 20  cksumEntry(.    
46c81 20 20 20 20 20 20 20 20 20 20 20 20 63 73 72 2e              csr.
46c82 7a 54 65 72 6d 2c 20 63 73 72 2e 6e 54 65 72 6d  zTerm, csr.nTerm
46c83 2c 20 69 4c 61 6e 67 69 64 2c 20 69 49 6e 64 65  , iLangid, iInde
46c84 78 2c 20 69 44 6f 63 69 64 2c 0a 20 20 20 20 20  x, iDocid,.     
46c85 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29             (int)
46c86 69 43 6f 6c 2c 20 28 69 6e 74 29 69 50 6f 73 0a  iCol, (int)iPos.
46c87 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20              );. 
46c88 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
46c89 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
46c8a 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
46c8b 46 74 73 33 53 65 67 52 65 61 64 65 72 46 69 6e  Fts3SegReaderFin
46c8c 69 73 68 28 26 63 73 72 29 3b 0a 0a 20 20 2a 70  ish(&csr);..  *p
46c8d 52 63 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 72  Rc = rc;.  retur
46c8e 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a  n cksum;.}../*.*
46c8f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 63  * Check if the c
46c90 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 46  ontents of the F
46c91 54 53 20 69 6e 64 65 78 20 6d 61 74 63 68 20 74  TS index match t
46c92 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65  he current conte
46c93 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f  nts of the.** co
46c94 6e 74 65 6e 74 20 74 61 62 6c 65 2e 20 49 66 20  ntent table. If 
46c95 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  no error occurs 
46c96 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  and the contents
46c97 20 64 6f 20 6d 61 74 63 68 2c 20 73 65 74 20 2a   do match, set *
46c98 70 62 4f 6b 0a 2a 2a 20 74 6f 20 74 72 75 65 20  pbOk.** to true 
46c99 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  and return SQLIT
46c9a 45 5f 4f 4b 2e 20 4f 72 20 69 66 20 74 68 65 20  E_OK. Or if the 
46c9b 63 6f 6e 74 65 6e 74 73 20 64 6f 20 6e 6f 74 20  contents do not 
46c9c 6d 61 74 63 68 2c 20 73 65 74 20 2a 70 62 4f 6b  match, set *pbOk
46c9d 0a 2a 2a 20 74 6f 20 66 61 6c 73 65 20 62 65 66  .** to false bef
46c9e 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
46c9f 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
46ca0 20 6f 63 63 75 72 73 20 28 65 2e 67 2e 20 61 6e   occurs (e.g. an
46ca1 20 4f 4f 4d 20 6f 72 20 49 4f 20 65 72 72 6f 72   OOM or IO error
46ca2 29 2c 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c  ), return an SQL
46ca3 69 74 65 20 65 72 72 6f 72 20 0a 2a 2a 20 63 6f  ite error .** co
46ca4 64 65 2e 20 54 68 65 20 66 69 6e 61 6c 20 76 61  de. The final va
46ca5 6c 75 65 20 6f 66 20 2a 70 62 4f 6b 20 69 73 20  lue of *pbOk is 
46ca6 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 74 68 69  undefined in thi
46ca7 73 20 63 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  s case..*/.stati
46ca8 63 20 69 6e 74 20 66 74 73 33 49 6e 74 65 67 72  c int fts3Integr
46ca9 69 74 79 43 68 65 63 6b 28 46 74 73 33 54 61 62  ityCheck(Fts3Tab
46caa 6c 65 20 2a 70 2c 20 69 6e 74 20 2a 70 62 4f 6b  le *p, int *pbOk
46cab 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
46cac 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
46cad 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
46cae 6f 64 65 20 2a 2f 0a 20 20 75 36 34 20 63 6b 73  ode */.  u64 cks
46caf 75 6d 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20  um1 = 0;        
46cb0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63           /* Chec
46cb1 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 20 46 54  ksum based on FT
46cb2 53 20 69 6e 64 65 78 20 63 6f 6e 74 65 6e 74 73  S index contents
46cb3 20 2a 2f 0a 20 20 75 36 34 20 63 6b 73 75 6d 32   */.  u64 cksum2
46cb4 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
46cb5 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75        /* Checksu
46cb6 6d 20 62 61 73 65 64 20 6f 6e 20 25 5f 63 6f 6e  m based on %_con
46cb7 74 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 2a 2f  tent contents */
46cb8 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
46cb9 2a 70 41 6c 6c 4c 61 6e 67 69 64 20 3d 20 30 3b  *pAllLangid = 0;
46cba 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20     /* Statement 
46cbb 74 6f 20 72 65 74 75 72 6e 20 61 6c 6c 20 6c 61  to return all la
46cbc 6e 67 75 61 67 65 2d 69 64 73 20 2a 2f 0a 0a 20  nguage-ids */.. 
46cbd 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63   /* This block c
46cbe 61 6c 63 75 6c 61 74 65 73 20 74 68 65 20 63 68  alculates the ch
46cbf 65 63 6b 73 75 6d 20 61 63 63 6f 72 64 69 6e 67  ecksum according
46cc0 20 74 6f 20 74 68 65 20 46 54 53 20 69 6e 64 65   to the FTS inde
46cc1 78 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 66 74 73  x. */.  rc = fts
46cc2 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f  3SqlStmt(p, SQL_
46cc3 53 45 4c 45 43 54 5f 41 4c 4c 5f 4c 41 4e 47 49  SELECT_ALL_LANGI
46cc4 44 2c 20 26 70 41 6c 6c 4c 61 6e 67 69 64 2c 20  D, &pAllLangid, 
46cc5 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  0);.  if( rc==SQ
46cc6 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
46cc7 6e 74 20 72 63 32 3b 0a 20 20 20 20 73 71 6c 69  nt rc2;.    sqli
46cc8 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 70 41 6c  te3_bind_int(pAl
46cc9 6c 4c 61 6e 67 69 64 2c 20 31 2c 20 70 2d 3e 6e  lLangid, 1, p->n
46cca 49 6e 64 65 78 29 3b 0a 20 20 20 20 77 68 69 6c  Index);.    whil
46ccb 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc==SQLITE_OK
46ccc 20 26 26 20 73 71 6c 69 74 65 33 5f 73 74 65 70   && sqlite3_step
46ccd 28 70 41 6c 6c 4c 61 6e 67 69 64 29 3d 3d 53 51  (pAllLangid)==SQ
46cce 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
46ccf 20 20 69 6e 74 20 69 4c 61 6e 67 69 64 20 3d 20    int iLangid = 
46cd0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
46cd1 6e 74 28 70 41 6c 6c 4c 61 6e 67 69 64 2c 20 30  nt(pAllLangid, 0
46cd2 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  );.      int i;.
46cd3 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
46cd4 3c 70 2d 3e 6e 49 6e 64 65 78 3b 20 69 2b 2b 29  <p->nIndex; i++)
46cd5 7b 0a 20 20 20 20 20 20 20 20 63 6b 73 75 6d 31  {.        cksum1
46cd6 20 3d 20 63 6b 73 75 6d 31 20 5e 20 66 74 73 33   = cksum1 ^ fts3
46cd7 43 68 65 63 6b 73 75 6d 49 6e 64 65 78 28 70 2c  ChecksumIndex(p,
46cd8 20 69 4c 61 6e 67 69 64 2c 20 69 2c 20 26 72 63   iLangid, i, &rc
46cd9 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
46cda 0a 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74  .    rc2 = sqlit
46cdb 65 33 5f 72 65 73 65 74 28 70 41 6c 6c 4c 61 6e  e3_reset(pAllLan
46cdc 67 69 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63  gid);.    if( rc
46cdd 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63  ==SQLITE_OK ) rc
46cde 20 3d 20 72 63 32 3b 0a 20 20 7d 0a 0a 20 20 2f   = rc2;.  }..  /
46cdf 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 61 6c  * This block cal
46ce0 63 75 6c 61 74 65 73 20 74 68 65 20 63 68 65 63  culates the chec
46ce1 6b 73 75 6d 20 61 63 63 6f 72 64 69 6e 67 20 74  ksum according t
46ce2 6f 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20  o the %_content 
46ce3 74 61 62 6c 65 20 2a 2f 0a 20 20 72 63 20 3d 20  table */.  rc = 
46ce4 66 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53  fts3SqlStmt(p, S
46ce5 51 4c 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 4c 41  QL_SELECT_ALL_LA
46ce6 4e 47 49 44 2c 20 26 70 41 6c 6c 4c 61 6e 67 69  NGID, &pAllLangi
46ce7 64 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d  d, 0);.  if( rc=
46ce8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
46ce9 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
46cea 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
46ceb 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 2d 3e 70   *pModule = p->p
46cec 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75  Tokenizer->pModu
46ced 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  le;.    sqlite3_
46cee 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b  stmt *pStmt = 0;
46cef 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b  .    char *zSql;
46cf0 0a 20 20 20 0a 20 20 20 20 7a 53 71 6c 20 3d 20  .   .    zSql = 
46cf1 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
46cf2 22 53 45 4c 45 43 54 20 25 73 22 20 2c 20 70 2d  "SELECT %s" , p-
46cf3 3e 7a 52 65 61 64 45 78 70 72 6c 69 73 74 29 3b  >zReadExprlist);
46cf4 0a 20 20 20 20 69 66 28 20 21 7a 53 71 6c 20 29  .    if( !zSql )
46cf5 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
46cf6 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
46cf7 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
46cf8 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
46cf9 5f 76 32 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c  _v2(p->db, zSql,
46cfa 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b   -1, &pStmt, 0);
46cfb 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
46cfc 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 20 20 7d  ree(zSql);.    }
46cfd 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 72 63 3d  ..    while( rc=
46cfe 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 53 51  =SQLITE_OK && SQ
46cff 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65  LITE_ROW==sqlite
46d00 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b  3_step(pStmt) ){
46d01 0a 20 20 20 20 20 20 69 36 34 20 69 44 6f 63 69  .      i64 iDoci
46d02 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
46d03 6d 6e 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c 20  mn_int64(pStmt, 
46d04 30 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 4c  0);.      int iL
46d05 61 6e 67 20 3d 20 6c 61 6e 67 69 64 46 72 6f 6d  ang = langidFrom
46d06 53 65 6c 65 63 74 28 70 2c 20 70 53 74 6d 74 29  Select(p, pStmt)
46d07 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  ;.      int iCol
46d08 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 69 43 6f  ;..      for(iCo
46d09 6c 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  l=0; rc==SQLITE_
46d0a 4f 4b 20 26 26 20 69 43 6f 6c 3c 70 2d 3e 6e 43  OK && iCol<p->nC
46d0b 6f 6c 75 6d 6e 3b 20 69 43 6f 6c 2b 2b 29 7b 0a  olumn; iCol++){.
46d0c 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
46d0d 61 72 20 2a 7a 54 65 78 74 20 3d 20 28 63 6f 6e  ar *zText = (con
46d0e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
46d0f 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53  3_column_text(pS
46d10 74 6d 74 2c 20 69 43 6f 6c 2b 31 29 3b 0a 20 20  tmt, iCol+1);.  
46d11 20 20 20 20 20 20 69 6e 74 20 6e 54 65 78 74 20        int nText 
46d12 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
46d13 5f 62 79 74 65 73 28 70 53 74 6d 74 2c 20 69 43  _bytes(pStmt, iC
46d14 6f 6c 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 73  ol+1);.        s
46d15 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
46d16 5f 63 75 72 73 6f 72 20 2a 70 54 20 3d 20 30 3b  _cursor *pT = 0;
46d17 0a 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ..        rc = s
46d18 71 6c 69 74 65 33 46 74 73 33 4f 70 65 6e 54 6f  qlite3Fts3OpenTo
46d19 6b 65 6e 69 7a 65 72 28 70 2d 3e 70 54 6f 6b 65  kenizer(p->pToke
46d1a 6e 69 7a 65 72 2c 20 69 4c 61 6e 67 2c 20 7a 54  nizer, iLang, zT
46d1b 65 78 74 2c 20 6e 54 65 78 74 2c 20 26 70 54 29  ext, nText, &pT)
46d1c 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
46d1d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
46d1e 7b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72  {.          char
46d1f 20 63 6f 6e 73 74 20 2a 7a 54 6f 6b 65 6e 3b 20   const *zToken; 
46d20 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20        /* Buffer 
46d21 63 6f 6e 74 61 69 6e 69 6e 67 20 74 6f 6b 65 6e  containing token
46d22 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e   */.          in
46d23 74 20 6e 54 6f 6b 65 6e 20 3d 20 30 3b 20 20 20  t nToken = 0;   
46d24 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
46d25 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 6f  r of bytes in to
46d26 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ken */.         
46d27 20 69 6e 74 20 69 44 75 6d 31 20 3d 20 30 2c 20   int iDum1 = 0, 
46d28 69 44 75 6d 32 20 3d 20 30 3b 20 2f 2a 20 44 75  iDum2 = 0; /* Du
46d29 6d 6d 79 20 76 61 72 69 61 62 6c 65 73 20 2a 2f  mmy variables */
46d2a 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
46d2b 50 6f 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Pos = 0;        
46d2c 20 20 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e       /* Position
46d2d 20 6f 66 20 74 6f 6b 65 6e 20 69 6e 20 7a 54 65   of token in zTe
46d2e 78 74 20 2a 2f 0a 0a 20 20 20 20 20 20 20 20 20  xt */..         
46d2f 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78   rc = pModule->x
46d30 4e 65 78 74 28 70 54 2c 20 26 7a 54 6f 6b 65 6e  Next(pT, &zToken
46d31 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 44 75 6d  , &nToken, &iDum
46d32 31 2c 20 26 69 44 75 6d 32 2c 20 26 69 50 6f 73  1, &iDum2, &iPos
46d33 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
46d34 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
46d35 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  {.            in
46d36 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20  t i;.           
46d37 20 63 6b 73 75 6d 32 20 3d 20 63 6b 73 75 6d 32   cksum2 = cksum2
46d38 20 5e 20 66 74 73 33 43 68 65 63 6b 73 75 6d 45   ^ fts3ChecksumE
46d39 6e 74 72 79 28 0a 20 20 20 20 20 20 20 20 20 20  ntry(.          
46d3a 20 20 20 20 20 20 7a 54 6f 6b 65 6e 2c 20 6e 54        zToken, nT
46d3b 6f 6b 65 6e 2c 20 69 4c 61 6e 67 2c 20 30 2c 20  oken, iLang, 0, 
46d3c 69 44 6f 63 69 64 2c 20 69 43 6f 6c 2c 20 69 50  iDocid, iCol, iP
46d3d 6f 73 0a 20 20 20 20 20 20 20 20 20 20 20 20 29  os.            )
46d3e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 6f  ;.            fo
46d3f 72 28 69 3d 31 3b 20 69 3c 70 2d 3e 6e 49 6e 64  r(i=1; i<p->nInd
46d40 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ex; i++){.      
46d41 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 61          if( p->a
46d42 49 6e 64 65 78 5b 69 5d 2e 6e 50 72 65 66 69 78  Index[i].nPrefix
46d43 3c 3d 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20  <=nToken ){.    
46d44 20 20 20 20 20 20 20 20 20 20 20 20 63 6b 73 75              cksu
46d45 6d 32 20 3d 20 63 6b 73 75 6d 32 20 5e 20 66 74  m2 = cksum2 ^ ft
46d46 73 33 43 68 65 63 6b 73 75 6d 45 6e 74 72 79 28  s3ChecksumEntry(
46d47 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
46d48 20 20 20 7a 54 6f 6b 65 6e 2c 20 70 2d 3e 61 49     zToken, p->aI
46d49 6e 64 65 78 5b 69 5d 2e 6e 50 72 65 66 69 78 2c  ndex[i].nPrefix,
46d4a 20 69 4c 61 6e 67 2c 20 69 2c 20 69 44 6f 63 69   iLang, i, iDoci
46d4b 64 2c 20 69 43 6f 6c 2c 20 69 50 6f 73 0a 20 20  d, iCol, iPos.  
46d4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29 3b                );
46d4d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
46d4e 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
46d4f 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
46d50 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
46d51 20 70 54 20 29 20 70 4d 6f 64 75 6c 65 2d 3e 78   pT ) pModule->x
46d52 43 6c 6f 73 65 28 70 54 29 3b 0a 20 20 20 20 20  Close(pT);.     
46d53 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
46d54 45 5f 44 4f 4e 45 20 29 20 72 63 20 3d 20 53 51  E_DONE ) rc = SQ
46d55 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d  LITE_OK;.      }
46d56 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
46d57 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74  te3_finalize(pSt
46d58 6d 74 29 3b 0a 20 20 7d 0a 0a 20 20 2a 70 62 4f  mt);.  }..  *pbO
46d59 6b 20 3d 20 28 63 6b 73 75 6d 31 3d 3d 63 6b 73  k = (cksum1==cks
46d5a 75 6d 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  um2);.  return r
46d5b 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20  c;.}../*.** Run 
46d5c 74 68 65 20 69 6e 74 65 67 72 69 74 79 2d 63 68  the integrity-ch
46d5d 65 63 6b 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72  eck. If no error
46d5e 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68 65 20   occurs and the 
46d5f 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73  current contents
46d60 20 6f 66 0a 2a 2a 20 74 68 65 20 46 54 53 20 69   of.** the FTS i
46d61 6e 64 65 78 20 61 72 65 20 63 6f 72 72 65 63 74  ndex are correct
46d62 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
46d63 4f 4b 2e 20 4f 72 2c 20 69 66 20 74 68 65 20 63  OK. Or, if the c
46d64 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a  ontents of the.*
46d65 2a 20 46 54 53 20 69 6e 64 65 78 20 61 72 65 20  * FTS index are 
46d66 69 6e 63 6f 72 72 65 63 74 2c 20 72 65 74 75 72  incorrect, retur
46d67 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
46d68 5f 56 54 41 42 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c  _VTAB..**.** Or,
46d69 20 69 66 20 61 6e 20 65 72 72 6f 72 20 28 65 2e   if an error (e.
46d6a 67 2e 20 61 6e 20 4f 4f 4d 20 6f 72 20 49 4f 20  g. an OOM or IO 
46d6b 65 72 72 6f 72 29 20 6f 63 63 75 72 73 2c 20 72  error) occurs, r
46d6c 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65 20  eturn an SQLite 
46d6d 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a  .** error code..
46d6e 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 67 72  **.** The integr
46d6f 69 74 79 2d 63 68 65 63 6b 20 77 6f 72 6b 73 20  ity-check works 
46d70 61 73 20 66 6f 6c 6c 6f 77 73 2e 20 46 6f 72 20  as follows. For 
46d71 65 61 63 68 20 74 6f 6b 65 6e 20 61 6e 64 20 69  each token and i
46d72 6e 64 65 78 65 64 20 74 6f 6b 65 6e 0a 2a 2a 20  ndexed token.** 
46d73 70 72 65 66 69 78 20 69 6e 20 74 68 65 20 64 6f  prefix in the do
46d74 63 75 6d 65 6e 74 20 73 65 74 2c 20 61 20 36 34  cument set, a 64
46d75 2d 62 69 74 20 63 68 65 63 6b 73 75 6d 20 69 73  -bit checksum is
46d76 20 63 61 6c 63 75 6c 61 74 65 64 20 28 62 79 20   calculated (by 
46d77 63 6f 64 65 0a 2a 2a 20 69 6e 20 66 74 73 33 43  code.** in fts3C
46d78 68 65 63 6b 73 75 6d 45 6e 74 72 79 28 29 29 20  hecksumEntry()) 
46d79 62 61 73 65 64 20 6f 6e 20 74 68 65 20 66 6f 6c  based on the fol
46d7a 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
46d7b 20 20 2b 20 54 68 65 20 69 6e 64 65 78 20 6e 75    + The index nu
46d7c 6d 62 65 72 20 28 30 20 66 6f 72 20 74 68 65 20  mber (0 for the 
46d7d 6d 61 69 6e 20 69 6e 64 65 78 2c 20 31 20 66 6f  main index, 1 fo
46d7e 72 20 74 68 65 20 66 69 72 73 74 20 70 72 65 66  r the first pref
46d7f 69 78 0a 2a 2a 20 20 20 20 20 20 20 69 6e 64 65  ix.**       inde
46d80 78 20 65 74 63 2e 29 2c 0a 2a 2a 20 20 20 20 20  x etc.),.**     
46d81 2b 20 54 68 65 20 74 6f 6b 65 6e 20 28 6f 72 20  + The token (or 
46d82 74 6f 6b 65 6e 20 70 72 65 66 69 78 29 20 74 65  token prefix) te
46d83 78 74 20 69 74 73 65 6c 66 2c 20 0a 2a 2a 20 20  xt itself, .**  
46d84 20 20 20 2b 20 54 68 65 20 6c 61 6e 67 75 61 67     + The languag
46d85 65 2d 69 64 20 6f 66 20 74 68 65 20 72 6f 77 20  e-id of the row 
46d86 69 74 20 61 70 70 65 61 72 73 20 69 6e 2c 0a 2a  it appears in,.*
46d87 2a 20 20 20 20 20 2b 20 54 68 65 20 64 6f 63 69  *     + The doci
46d88 64 20 6f 66 20 74 68 65 20 72 6f 77 20 69 74 20  d of the row it 
46d89 61 70 70 65 61 72 73 20 69 6e 2c 0a 2a 2a 20 20  appears in,.**  
46d8a 20 20 20 2b 20 54 68 65 20 63 6f 6c 75 6d 6e 20     + The column 
46d8b 69 74 20 61 70 70 65 61 72 73 20 69 6e 2c 20 61  it appears in, a
46d8c 6e 64 0a 2a 2a 20 20 20 20 20 2b 20 54 68 65 20  nd.**     + The 
46d8d 74 6f 6b 65 6e 73 20 70 6f 73 69 74 69 6f 6e 20  tokens position 
46d8e 77 69 74 68 69 6e 20 74 68 61 74 20 63 6f 6c 75  within that colu
46d8f 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 68  mn..**.** The ch
46d90 65 63 6b 73 75 6d 73 20 66 6f 72 20 61 6c 6c 20  ecksums for all 
46d91 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 69  entries in the i
46d92 6e 64 65 78 20 61 72 65 20 58 4f 52 65 64 20 74  ndex are XORed t
46d93 6f 67 65 74 68 65 72 20 74 6f 20 63 72 65 61 74  ogether to creat
46d94 65 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 63 68  e.** a single ch
46d95 65 63 6b 73 75 6d 20 66 6f 72 20 74 68 65 20 65  ecksum for the e
46d96 6e 74 69 72 65 20 69 6e 64 65 78 2e 0a 2a 2a 0a  ntire index..**.
46d97 2a 2a 20 54 68 65 20 69 6e 74 65 67 72 69 74 79  ** The integrity
46d98 2d 63 68 65 63 6b 20 63 6f 64 65 20 63 61 6c 63  -check code calc
46d99 75 6c 61 74 65 73 20 74 68 65 20 73 61 6d 65 20  ulates the same 
46d9a 63 68 65 63 6b 73 75 6d 20 69 6e 20 74 77 6f 20  checksum in two 
46d9b 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ways:.**.**     
46d9c 31 2e 20 42 79 20 73 63 61 6e 6e 69 6e 67 20 74  1. By scanning t
46d9d 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
46d9e 68 65 20 46 54 53 20 69 6e 64 65 78 2c 20 61 6e  he FTS index, an
46d9f 64 20 0a 2a 2a 20 20 20 20 20 32 2e 20 42 79 20  d .**     2. By 
46da0 73 63 61 6e 6e 69 6e 67 20 61 6e 64 20 74 6f 6b  scanning and tok
46da1 65 6e 69 7a 69 6e 67 20 74 68 65 20 63 6f 6e 74  enizing the cont
46da2 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ent table..**.**
46da3 20 49 66 20 74 68 65 20 74 77 6f 20 63 68 65 63   If the two chec
46da4 6b 73 75 6d 73 20 61 72 65 20 69 64 65 6e 74 69  ksums are identi
46da5 63 61 6c 2c 20 74 68 65 20 69 6e 74 65 67 72 69  cal, the integri
46da6 74 79 2d 63 68 65 63 6b 20 69 73 20 64 65 65 6d  ty-check is deem
46da7 65 64 20 74 6f 20 68 61 76 65 0a 2a 2a 20 70 61  ed to have.** pa
46da8 73 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ssed..*/.static 
46da9 69 6e 74 20 66 74 73 33 44 6f 49 6e 74 65 67 72  int fts3DoIntegr
46daa 69 74 79 43 68 65 63 6b 28 0a 20 20 46 74 73 33  ityCheck(.  Fts3
46dab 54 61 62 6c 65 20 2a 70 20 20 20 20 20 20 20 20  Table *p        
46dac 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
46dad 54 53 33 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  TS3 table handle
46dae 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
46daf 0a 20 20 69 6e 74 20 62 4f 6b 20 3d 20 30 3b 0a  .  int bOk = 0;.
46db0 20 20 72 63 20 3d 20 66 74 73 33 49 6e 74 65 67    rc = fts3Integ
46db1 72 69 74 79 43 68 65 63 6b 28 70 2c 20 26 62 4f  rityCheck(p, &bO
46db2 6b 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  k);.  if( rc==SQ
46db3 4c 49 54 45 5f 4f 4b 20 26 26 20 62 4f 6b 3d 3d  LITE_OK && bOk==
46db4 30 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  0 ) rc = SQLITE_
46db5 43 4f 52 52 55 50 54 5f 56 54 41 42 3b 0a 20 20  CORRUPT_VTAB;.  
46db6 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
46db7 0a 2a 2a 20 48 61 6e 64 6c 65 20 61 20 27 73 70  .** Handle a 'sp
46db8 65 63 69 61 6c 27 20 49 4e 53 45 52 54 20 6f 66  ecial' INSERT of
46db9 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a   the form:.**.**
46dba 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20     "INSERT INTO 
46dbb 74 62 6c 28 74 62 6c 29 20 56 41 4c 55 45 53 28  tbl(tbl) VALUES(
46dbc 3c 65 78 70 72 3e 29 22 0a 2a 2a 0a 2a 2a 20 41  <expr>)".**.** A
46dbd 72 67 75 6d 65 6e 74 20 70 56 61 6c 20 63 6f 6e  rgument pVal con
46dbe 74 61 69 6e 73 20 74 68 65 20 72 65 73 75 6c 74  tains the result
46dbf 20 6f 66 20 3c 65 78 70 72 3e 2e 20 43 75 72 72   of <expr>. Curr
46dc0 65 6e 74 6c 79 20 74 68 65 20 6f 6e 6c 79 20 0a  ently the only .
46dc1 2a 2a 20 6d 65 61 6e 69 6e 67 66 75 6c 20 76 61  ** meaningful va
46dc2 6c 75 65 20 74 6f 20 69 6e 73 65 72 74 20 69 73  lue to insert is
46dc3 20 74 68 65 20 74 65 78 74 20 27 6f 70 74 69 6d   the text 'optim
46dc4 69 7a 65 27 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ize'..*/.static 
46dc5 69 6e 74 20 66 74 73 33 53 70 65 63 69 61 6c 49  int fts3SpecialI
46dc6 6e 73 65 72 74 28 46 74 73 33 54 61 62 6c 65 20  nsert(Fts3Table 
46dc7 2a 70 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  *p, sqlite3_valu
46dc8 65 20 2a 70 56 61 6c 29 7b 0a 20 20 69 6e 74 20  e *pVal){.  int 
46dc9 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
46dca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
46dcb 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20  eturn Code */.  
46dcc 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 61 6c  const char *zVal
46dcd 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
46dce 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
46dcf 65 78 74 28 70 56 61 6c 29 3b 0a 20 20 69 6e 74  ext(pVal);.  int
46dd0 20 6e 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f   nVal = sqlite3_
46dd1 76 61 6c 75 65 5f 62 79 74 65 73 28 70 56 61 6c  value_bytes(pVal
46dd2 29 3b 0a 0a 20 20 69 66 28 20 21 7a 56 61 6c 20  );..  if( !zVal 
46dd3 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
46dd4 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
46dd5 6c 73 65 20 69 66 28 20 6e 56 61 6c 3d 3d 38 20  lse if( nVal==8 
46dd6 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 5f 73 74  && 0==sqlite3_st
46dd7 72 6e 69 63 6d 70 28 7a 56 61 6c 2c 20 22 6f 70  rnicmp(zVal, "op
46dd8 74 69 6d 69 7a 65 22 2c 20 38 29 20 29 7b 0a 20  timize", 8) ){. 
46dd9 20 20 20 72 63 20 3d 20 66 74 73 33 44 6f 4f 70     rc = fts3DoOp
46dda 74 69 6d 69 7a 65 28 70 2c 20 30 29 3b 0a 20 20  timize(p, 0);.  
46ddb 7d 65 6c 73 65 20 69 66 28 20 6e 56 61 6c 3d 3d  }else if( nVal==
46ddc 37 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 5f  7 && 0==sqlite3_
46ddd 73 74 72 6e 69 63 6d 70 28 7a 56 61 6c 2c 20 22  strnicmp(zVal, "
46dde 72 65 62 75 69 6c 64 22 2c 20 37 29 20 29 7b 0a  rebuild", 7) ){.
46ddf 20 20 20 20 72 63 20 3d 20 66 74 73 33 44 6f 52      rc = fts3DoR
46de0 65 62 75 69 6c 64 28 70 29 3b 0a 20 20 7d 65 6c  ebuild(p);.  }el
46de1 73 65 20 69 66 28 20 6e 56 61 6c 3d 3d 31 35 20  se if( nVal==15 
46de2 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 5f 73 74  && 0==sqlite3_st
46de3 72 6e 69 63 6d 70 28 7a 56 61 6c 2c 20 22 69 6e  rnicmp(zVal, "in
46de4 74 65 67 72 69 74 79 2d 63 68 65 63 6b 22 2c 20  tegrity-check", 
46de5 31 35 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  15) ){.    rc = 
46de6 66 74 73 33 44 6f 49 6e 74 65 67 72 69 74 79 43  fts3DoIntegrityC
46de7 68 65 63 6b 28 70 29 3b 0a 20 20 7d 65 6c 73 65  heck(p);.  }else
46de8 20 69 66 28 20 6e 56 61 6c 3e 36 20 26 26 20 30   if( nVal>6 && 0
46de9 3d 3d 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63  ==sqlite3_strnic
46dea 6d 70 28 7a 56 61 6c 2c 20 22 6d 65 72 67 65 3d  mp(zVal, "merge=
46deb 22 2c 20 36 29 20 29 7b 0a 20 20 20 20 72 63 20  ", 6) ){.    rc 
46dec 3d 20 66 74 73 33 44 6f 49 6e 63 72 6d 65 72 67  = fts3DoIncrmerg
46ded 65 28 70 2c 20 26 7a 56 61 6c 5b 36 5d 29 3b 0a  e(p, &zVal[6]);.
46dee 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 56 61 6c    }else if( nVal
46def 3e 31 30 20 26 26 20 30 3d 3d 73 71 6c 69 74 65  >10 && 0==sqlite
46df0 33 5f 73 74 72 6e 69 63 6d 70 28 7a 56 61 6c 2c  3_strnicmp(zVal,
46df1 20 22 61 75 74 6f 6d 65 72 67 65 3d 22 2c 20 31   "automerge=", 1
46df2 30 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66  0) ){.    rc = f
46df3 74 73 33 44 6f 41 75 74 6f 69 6e 63 72 6d 65 72  ts3DoAutoincrmer
46df4 67 65 28 70 2c 20 26 7a 56 61 6c 5b 31 30 5d 29  ge(p, &zVal[10])
46df5 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
46df6 54 45 53 54 0a 20 20 7d 65 6c 73 65 20 69 66 28  TEST.  }else if(
46df7 20 6e 56 61 6c 3e 39 20 26 26 20 30 3d 3d 73 71   nVal>9 && 0==sq
46df8 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 7a  lite3_strnicmp(z
46df9 56 61 6c 2c 20 22 6e 6f 64 65 73 69 7a 65 3d 22  Val, "nodesize="
46dfa 2c 20 39 29 20 29 7b 0a 20 20 20 20 70 2d 3e 6e  , 9) ){.    p->n
46dfb 4e 6f 64 65 53 69 7a 65 20 3d 20 61 74 6f 69 28  NodeSize = atoi(
46dfc 26 7a 56 61 6c 5b 39 5d 29 3b 0a 20 20 20 20 72  &zVal[9]);.    r
46dfd 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
46dfe 20 7d 65 6c 73 65 20 69 66 28 20 6e 56 61 6c 3e   }else if( nVal>
46dff 31 31 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33  11 && 0==sqlite3
46e00 5f 73 74 72 6e 69 63 6d 70 28 7a 56 61 6c 2c 20  _strnicmp(zVal, 
46e01 22 6d 61 78 70 65 6e 64 69 6e 67 3d 22 2c 20 39  "maxpending=", 9
46e02 29 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 4d 61 78  ) ){.    p->nMax
46e03 50 65 6e 64 69 6e 67 44 61 74 61 20 3d 20 61 74  PendingData = at
46e04 6f 69 28 26 7a 56 61 6c 5b 31 31 5d 29 3b 0a 20  oi(&zVal[11]);. 
46e05 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
46e06 4b 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73  K;.#endif.  }els
46e07 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  e{.    rc = SQLI
46e08 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20  TE_ERROR;.  }.. 
46e09 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
46e0a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49  ifndef SQLITE_DI
46e0b 53 41 42 4c 45 5f 46 54 53 34 5f 44 45 46 45 52  SABLE_FTS4_DEFER
46e0c 52 45 44 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  RED./*.** Delete
46e0d 20 61 6c 6c 20 63 61 63 68 65 64 20 64 65 66 65   all cached defe
46e0e 72 72 65 64 20 64 6f 63 6c 69 73 74 73 2e 20 44  rred doclists. D
46e0f 65 66 65 72 72 65 64 20 64 6f 63 6c 69 73 74 73  eferred doclists
46e10 20 61 72 65 20 63 61 63 68 65 64 0a 2a 2a 20 28   are cached.** (
46e11 61 6c 6c 6f 63 61 74 65 64 29 20 62 79 20 74 68  allocated) by th
46e12 65 20 73 71 6c 69 74 65 33 46 74 73 33 43 61 63  e sqlite3Fts3Cac
46e13 68 65 44 65 66 65 72 72 65 64 44 6f 63 6c 69 73  heDeferredDoclis
46e14 74 73 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  ts() function..*
46e15 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
46e16 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
46e17 33 46 72 65 65 44 65 66 65 72 72 65 64 44 6f 63  3FreeDeferredDoc
46e18 6c 69 73 74 73 28 46 74 73 33 43 75 72 73 6f 72  lists(Fts3Cursor
46e19 20 2a 70 43 73 72 29 7b 0a 20 20 46 74 73 33 44   *pCsr){.  Fts3D
46e1a 65 66 65 72 72 65 64 54 6f 6b 65 6e 20 2a 70 44  eferredToken *pD
46e1b 65 66 3b 0a 20 20 66 6f 72 28 70 44 65 66 3d 70  ef;.  for(pDef=p
46e1c 43 73 72 2d 3e 70 44 65 66 65 72 72 65 64 3b 20  Csr->pDeferred; 
46e1d 70 44 65 66 3b 20 70 44 65 66 3d 70 44 65 66 2d  pDef; pDef=pDef-
46e1e 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 66 74 73  >pNext){.    fts
46e1f 33 50 65 6e 64 69 6e 67 4c 69 73 74 44 65 6c 65  3PendingListDele
46e20 74 65 28 70 44 65 66 2d 3e 70 4c 69 73 74 29 3b  te(pDef->pList);
46e21 0a 20 20 20 20 70 44 65 66 2d 3e 70 4c 69 73 74  .    pDef->pList
46e22 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   = 0;.  }.}../*.
46e23 2a 2a 20 46 72 65 65 20 61 6c 6c 20 65 6e 74 72  ** Free all entr
46e24 69 65 73 20 69 6e 20 74 68 65 20 70 43 73 72 2d  ies in the pCsr-
46e25 3e 70 44 65 66 66 65 72 65 64 20 6c 69 73 74 2e  >pDeffered list.
46e26 20 45 6e 74 72 69 65 73 20 61 72 65 20 61 64 64   Entries are add
46e27 65 64 20 74 6f 20 0a 2a 2a 20 74 68 69 73 20 6c  ed to .** this l
46e28 69 73 74 20 75 73 69 6e 67 20 73 71 6c 69 74 65  ist using sqlite
46e29 33 46 74 73 33 44 65 66 65 72 54 6f 6b 65 6e 28  3Fts3DeferToken(
46e2a 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
46e2b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
46e2c 33 46 74 73 33 46 72 65 65 44 65 66 65 72 72 65  3Fts3FreeDeferre
46e2d 64 54 6f 6b 65 6e 73 28 46 74 73 33 43 75 72 73  dTokens(Fts3Curs
46e2e 6f 72 20 2a 70 43 73 72 29 7b 0a 20 20 46 74 73  or *pCsr){.  Fts
46e2f 33 44 65 66 65 72 72 65 64 54 6f 6b 65 6e 20 2a  3DeferredToken *
46e30 70 44 65 66 3b 0a 20 20 46 74 73 33 44 65 66 65  pDef;.  Fts3Defe
46e31 72 72 65 64 54 6f 6b 65 6e 20 2a 70 4e 65 78 74  rredToken *pNext
46e32 3b 0a 20 20 66 6f 72 28 70 44 65 66 3d 70 43 73  ;.  for(pDef=pCs
46e33 72 2d 3e 70 44 65 66 65 72 72 65 64 3b 20 70 44  r->pDeferred; pD
46e34 65 66 3b 20 70 44 65 66 3d 70 4e 65 78 74 29 7b  ef; pDef=pNext){
46e35 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 44 65  .    pNext = pDe
46e36 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 66 74  f->pNext;.    ft
46e37 73 33 50 65 6e 64 69 6e 67 4c 69 73 74 44 65 6c  s3PendingListDel
46e38 65 74 65 28 70 44 65 66 2d 3e 70 4c 69 73 74 29  ete(pDef->pList)
46e39 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
46e3a 65 65 28 70 44 65 66 29 3b 0a 20 20 7d 0a 20 20  ee(pDef);.  }.  
46e3b 70 43 73 72 2d 3e 70 44 65 66 65 72 72 65 64 20  pCsr->pDeferred 
46e3c 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  = 0;.}../*.** Ge
46e3d 6e 65 72 61 74 65 20 64 65 66 65 72 72 65 64 2d  nerate deferred-
46e3e 64 6f 63 6c 69 73 74 73 20 66 6f 72 20 61 6c 6c  doclists for all
46e3f 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 70   tokens in the p
46e40 43 73 72 2d 3e 70 44 65 66 65 72 72 65 64 20 6c  Csr->pDeferred l
46e41 69 73 74 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20  ist.** based on 
46e42 74 68 65 20 72 6f 77 20 74 68 61 74 20 70 43 73  the row that pCs
46e43 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  r currently poin
46e44 74 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 64  ts to..**.** A d
46e45 65 66 65 72 72 65 64 2d 64 6f 63 6c 69 73 74 20  eferred-doclist 
46e46 69 73 20 6c 69 6b 65 20 61 6e 79 20 6f 74 68 65  is like any othe
46e47 72 20 64 6f 63 6c 69 73 74 20 77 69 74 68 20 70  r doclist with p
46e48 6f 73 69 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74  osition informat
46e49 69 6f 6e 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 2c  ion.** included,
46e4a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20   except that it 
46e4b 6f 6e 6c 79 20 63 6f 6e 74 61 69 6e 73 20 65 6e  only contains en
46e4c 74 72 69 65 73 20 66 6f 72 20 61 20 73 69 6e 67  tries for a sing
46e4d 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 0a 2a 2a  le row of the.**
46e4e 20 74 61 62 6c 65 2c 20 6e 6f 74 20 66 6f 72 20   table, not for 
46e4f 61 6c 6c 20 72 6f 77 73 2e 0a 2a 2f 0a 53 51 4c  all rows..*/.SQL
46e50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
46e51 73 71 6c 69 74 65 33 46 74 73 33 43 61 63 68 65  sqlite3Fts3Cache
46e52 44 65 66 65 72 72 65 64 44 6f 63 6c 69 73 74 73  DeferredDoclists
46e53 28 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73  (Fts3Cursor *pCs
46e54 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  r){.  int rc = S
46e55 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
46e56 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
46e57 63 6f 64 65 20 2a 2f 0a 20 20 69 66 28 20 70 43  code */.  if( pC
46e58 73 72 2d 3e 70 44 65 66 65 72 72 65 64 20 29 7b  sr->pDeferred ){
46e59 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20  .    int i;     
46e5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46e5b 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74     /* Used to it
46e5c 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 74 61  erate through ta
46e5d 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20  ble columns */. 
46e5e 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
46e5f 20 69 44 6f 63 69 64 3b 20 20 20 20 20 20 20 20   iDocid;        
46e60 20 2f 2a 20 44 6f 63 69 64 20 6f 66 20 74 68 65   /* Docid of the
46e61 20 72 6f 77 20 70 43 73 72 20 70 6f 69 6e 74 73   row pCsr points
46e62 20 74 6f 20 2a 2f 0a 20 20 20 20 46 74 73 33 44   to */.    Fts3D
46e63 65 66 65 72 72 65 64 54 6f 6b 65 6e 20 2a 70 44  eferredToken *pD
46e64 65 66 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 64  ef;      /* Used
46e65 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f   to iterate thro
46e66 75 67 68 20 64 65 66 65 72 72 65 64 20 74 6f 6b  ugh deferred tok
46e67 65 6e 73 20 2a 2f 0a 20 20 0a 20 20 20 20 46 74  ens */.  .    Ft
46e68 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28 46 74  s3Table *p = (Ft
46e69 73 33 54 61 62 6c 65 20 2a 29 70 43 73 72 2d 3e  s3Table *)pCsr->
46e6a 62 61 73 65 2e 70 56 74 61 62 3b 0a 20 20 20 20  base.pVtab;.    
46e6b 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
46e6c 72 20 2a 70 54 20 3d 20 70 2d 3e 70 54 6f 6b 65  r *pT = p->pToke
46e6d 6e 69 7a 65 72 3b 0a 20 20 20 20 73 71 6c 69 74  nizer;.    sqlit
46e6e 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
46e6f 75 6c 65 20 63 6f 6e 73 74 20 2a 70 4d 6f 64 75  ule const *pModu
46e70 6c 65 20 3d 20 70 54 2d 3e 70 4d 6f 64 75 6c 65  le = pT->pModule
46e71 3b 0a 20 20 20 0a 20 20 20 20 61 73 73 65 72 74  ;.   .    assert
46e72 28 20 70 43 73 72 2d 3e 69 73 52 65 71 75 69 72  ( pCsr->isRequir
46e73 65 53 65 65 6b 3d 3d 30 20 29 3b 0a 20 20 20 20  eSeek==0 );.    
46e74 69 44 6f 63 69 64 20 3d 20 73 71 6c 69 74 65 33  iDocid = sqlite3
46e75 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 43  _column_int64(pC
46e76 73 72 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0a 20  sr->pStmt, 0);. 
46e77 20 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69   .    for(i=0; i
46e78 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 26 26 20 72  <p->nColumn && r
46e79 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 2b  c==SQLITE_OK; i+
46e7a 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  +){.      const 
46e7b 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 28 63  char *zText = (c
46e7c 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
46e7d 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
46e7e 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20 69 2b 31  pCsr->pStmt, i+1
46e7f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
46e80 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
46e81 72 20 2a 70 54 43 20 3d 20 30 3b 0a 20 20 0a 20  r *pTC = 0;.  . 
46e82 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
46e83 33 46 74 73 33 4f 70 65 6e 54 6f 6b 65 6e 69 7a  3Fts3OpenTokeniz
46e84 65 72 28 70 54 2c 20 70 43 73 72 2d 3e 69 4c 61  er(pT, pCsr->iLa
46e85 6e 67 69 64 2c 20 7a 54 65 78 74 2c 20 2d 31 2c  ngid, zText, -1,
46e86 20 26 70 54 43 29 3b 0a 20 20 20 20 20 20 77 68   &pTC);.      wh
46e87 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
46e88 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  OK ){.        ch
46e89 61 72 20 63 6f 6e 73 74 20 2a 7a 54 6f 6b 65 6e  ar const *zToken
46e8a 3b 20 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65  ;       /* Buffe
46e8b 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 6f 6b  r containing tok
46e8c 65 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e  en */.        in
46e8d 74 20 6e 54 6f 6b 65 6e 20 3d 20 30 3b 20 20 20  t nToken = 0;   
46e8e 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
46e8f 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 6f  r of bytes in to
46e90 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ken */.        i
46e91 6e 74 20 69 44 75 6d 31 20 3d 20 30 2c 20 69 44  nt iDum1 = 0, iD
46e92 75 6d 32 20 3d 20 30 3b 20 2f 2a 20 44 75 6d 6d  um2 = 0; /* Dumm
46e93 79 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20  y variables */. 
46e94 20 20 20 20 20 20 20 69 6e 74 20 69 50 6f 73 20         int iPos 
46e95 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
46e96 20 2f 2a 20 50 6f 73 69 74 69 6f 6e 20 6f 66 20   /* Position of 
46e97 74 6f 6b 65 6e 20 69 6e 20 7a 54 65 78 74 20 2a  token in zText *
46e98 2f 0a 20 20 0a 20 20 20 20 20 20 20 20 72 63 20  /.  .        rc 
46e99 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74  = pModule->xNext
46e9a 28 70 54 43 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26  (pTC, &zToken, &
46e9b 6e 54 6f 6b 65 6e 2c 20 26 69 44 75 6d 31 2c 20  nToken, &iDum1, 
46e9c 26 69 44 75 6d 32 2c 20 26 69 50 6f 73 29 3b 0a  &iDum2, &iPos);.
46e9d 20 20 20 20 20 20 20 20 66 6f 72 28 70 44 65 66          for(pDef
46e9e 3d 70 43 73 72 2d 3e 70 44 65 66 65 72 72 65 64  =pCsr->pDeferred
46e9f 3b 20 70 44 65 66 20 26 26 20 72 63 3d 3d 53 51  ; pDef && rc==SQ
46ea0 4c 49 54 45 5f 4f 4b 3b 20 70 44 65 66 3d 70 44  LITE_OK; pDef=pD
46ea1 65 66 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  ef->pNext){.    
46ea2 20 20 20 20 20 20 46 74 73 33 50 68 72 61 73 65        Fts3Phrase
46ea3 54 6f 6b 65 6e 20 2a 70 50 54 20 3d 20 70 44 65  Token *pPT = pDe
46ea4 66 2d 3e 70 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  f->pToken;.     
46ea5 20 20 20 20 20 69 66 28 20 28 70 44 65 66 2d 3e       if( (pDef->
46ea6 69 43 6f 6c 3e 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e  iCol>=p->nColumn
46ea7 20 7c 7c 20 70 44 65 66 2d 3e 69 43 6f 6c 3d 3d   || pDef->iCol==
46ea8 69 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26  i).           &&
46ea9 20 28 70 50 54 2d 3e 62 46 69 72 73 74 3d 3d 30   (pPT->bFirst==0
46eaa 20 7c 7c 20 69 50 6f 73 3d 3d 30 29 0a 20 20 20   || iPos==0).   
46eab 20 20 20 20 20 20 20 20 26 26 20 28 70 50 54 2d          && (pPT-
46eac 3e 6e 3d 3d 6e 54 6f 6b 65 6e 20 7c 7c 20 28 70  >n==nToken || (p
46ead 50 54 2d 3e 69 73 50 72 65 66 69 78 20 26 26 20  PT->isPrefix && 
46eae 70 50 54 2d 3e 6e 3c 6e 54 6f 6b 65 6e 29 29 0a  pPT->n<nToken)).
46eaf 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 30             && (0
46eb0 3d 3d 6d 65 6d 63 6d 70 28 7a 54 6f 6b 65 6e 2c  ==memcmp(zToken,
46eb1 20 70 50 54 2d 3e 7a 2c 20 70 50 54 2d 3e 6e 29   pPT->z, pPT->n)
46eb2 29 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20  ).          ){. 
46eb3 20 20 20 20 20 20 20 20 20 20 20 66 74 73 33 50             fts3P
46eb4 65 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e 64  endingListAppend
46eb5 28 26 70 44 65 66 2d 3e 70 4c 69 73 74 2c 20 69  (&pDef->pList, i
46eb6 44 6f 63 69 64 2c 20 69 2c 20 69 50 6f 73 2c 20  Docid, i, iPos, 
46eb7 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20  &rc);.          
46eb8 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
46eb9 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 54    }.      if( pT
46eba 43 20 29 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c  C ) pModule->xCl
46ebb 6f 73 65 28 70 54 43 29 3b 0a 20 20 20 20 20 20  ose(pTC);.      
46ebc 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
46ebd 4f 4e 45 20 29 20 72 63 20 3d 20 53 51 4c 49 54  ONE ) rc = SQLIT
46ebe 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  E_OK;.    }.  . 
46ebf 20 20 20 66 6f 72 28 70 44 65 66 3d 70 43 73 72     for(pDef=pCsr
46ec0 2d 3e 70 44 65 66 65 72 72 65 64 3b 20 70 44 65  ->pDeferred; pDe
46ec1 66 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f  f && rc==SQLITE_
46ec2 4f 4b 3b 20 70 44 65 66 3d 70 44 65 66 2d 3e 70  OK; pDef=pDef->p
46ec3 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28  Next){.      if(
46ec4 20 70 44 65 66 2d 3e 70 4c 69 73 74 20 29 7b 0a   pDef->pList ){.
46ec5 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 74 73          rc = fts
46ec6 33 50 65 6e 64 69 6e 67 4c 69 73 74 41 70 70 65  3PendingListAppe
46ec7 6e 64 56 61 72 69 6e 74 28 26 70 44 65 66 2d 3e  ndVarint(&pDef->
46ec8 70 4c 69 73 74 2c 20 30 29 3b 0a 20 20 20 20 20  pList, 0);.     
46ec9 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
46eca 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 53 51  return rc;.}..SQ
46ecb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
46ecc 20 73 71 6c 69 74 65 33 46 74 73 33 44 65 66 65   sqlite3Fts3Defe
46ecd 72 72 65 64 54 6f 6b 65 6e 4c 69 73 74 28 0a 20  rredTokenList(. 
46ece 20 46 74 73 33 44 65 66 65 72 72 65 64 54 6f 6b   Fts3DeferredTok
46ecf 65 6e 20 2a 70 2c 20 0a 20 20 63 68 61 72 20 2a  en *p, .  char *
46ed0 2a 70 70 44 61 74 61 2c 20 0a 20 20 69 6e 74 20  *ppData, .  int 
46ed1 2a 70 6e 44 61 74 61 0a 29 7b 0a 20 20 63 68 61  *pnData.){.  cha
46ed2 72 20 2a 70 52 65 74 3b 0a 20 20 69 6e 74 20 6e  r *pRet;.  int n
46ed3 53 6b 69 70 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Skip;.  sqlite3_
46ed4 69 6e 74 36 34 20 64 75 6d 6d 79 3b 0a 0a 20 20  int64 dummy;..  
46ed5 2a 70 70 44 61 74 61 20 3d 20 30 3b 0a 20 20 2a  *ppData = 0;.  *
46ed6 70 6e 44 61 74 61 20 3d 20 30 3b 0a 0a 20 20 69  pnData = 0;..  i
46ed7 66 28 20 70 2d 3e 70 4c 69 73 74 3d 3d 30 20 29  f( p->pList==0 )
46ed8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
46ed9 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 70  ITE_OK;.  }..  p
46eda 52 65 74 20 3d 20 28 63 68 61 72 20 2a 29 73 71  Ret = (char *)sq
46edb 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 70 2d 3e  lite3_malloc(p->
46edc 70 4c 69 73 74 2d 3e 6e 44 61 74 61 29 3b 0a 20  pList->nData);. 
46edd 20 69 66 28 20 21 70 52 65 74 20 29 20 72 65 74   if( !pRet ) ret
46ede 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
46edf 3b 0a 0a 20 20 6e 53 6b 69 70 20 3d 20 73 71 6c  ;..  nSkip = sql
46ee0 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e  ite3Fts3GetVarin
46ee1 74 28 70 2d 3e 70 4c 69 73 74 2d 3e 61 44 61 74  t(p->pList->aDat
46ee2 61 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 20 2a 70  a, &dummy);.  *p
46ee3 6e 44 61 74 61 20 3d 20 70 2d 3e 70 4c 69 73 74  nData = p->pList
46ee4 2d 3e 6e 44 61 74 61 20 2d 20 6e 53 6b 69 70 3b  ->nData - nSkip;
46ee5 0a 20 20 2a 70 70 44 61 74 61 20 3d 20 70 52 65  .  *ppData = pRe
46ee6 74 3b 0a 20 20 0a 20 20 6d 65 6d 63 70 79 28 70  t;.  .  memcpy(p
46ee7 52 65 74 2c 20 26 70 2d 3e 70 4c 69 73 74 2d 3e  Ret, &p->pList->
46ee8 61 44 61 74 61 5b 6e 53 6b 69 70 5d 2c 20 2a 70  aData[nSkip], *p
46ee9 6e 44 61 74 61 29 3b 0a 20 20 72 65 74 75 72 6e  nData);.  return
46eea 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
46eeb 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 65 6e 74 72  *.** Add an entr
46eec 79 20 66 6f 72 20 74 6f 6b 65 6e 20 70 54 6f 6b  y for token pTok
46eed 65 6e 20 74 6f 20 74 68 65 20 70 43 73 72 2d 3e  en to the pCsr->
46eee 70 44 65 66 65 72 72 65 64 20 6c 69 73 74 2e 0a  pDeferred list..
46eef 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
46ef0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
46ef1 33 44 65 66 65 72 54 6f 6b 65 6e 28 0a 20 20 46  3DeferToken(.  F
46ef2 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72 2c  ts3Cursor *pCsr,
46ef3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
46ef4 2a 20 46 74 73 33 20 74 61 62 6c 65 20 63 75 72  * Fts3 table cur
46ef5 73 6f 72 20 2a 2f 0a 20 20 46 74 73 33 50 68 72  sor */.  Fts3Phr
46ef6 61 73 65 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  aseToken *pToken
46ef7 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65  ,        /* Toke
46ef8 6e 20 74 6f 20 64 65 66 65 72 20 2a 2f 0a 20 20  n to defer */.  
46ef9 69 6e 74 20 69 43 6f 6c 20 20 20 20 20 20 20 20  int iCol        
46efa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46efb 2f 2a 20 43 6f 6c 75 6d 6e 20 74 68 61 74 20 74  /* Column that t
46efc 6f 6b 65 6e 20 6d 75 73 74 20 61 70 70 65 61 72  oken must appear
46efd 20 69 6e 20 28 6f 72 20 2d 31 29 20 2a 2f 0a 29   in (or -1) */.)
46efe 7b 0a 20 20 46 74 73 33 44 65 66 65 72 72 65 64  {.  Fts3Deferred
46eff 54 6f 6b 65 6e 20 2a 70 44 65 66 65 72 72 65 64  Token *pDeferred
46f00 3b 0a 20 20 70 44 65 66 65 72 72 65 64 20 3d 20  ;.  pDeferred = 
46f01 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
46f02 69 7a 65 6f 66 28 2a 70 44 65 66 65 72 72 65 64  izeof(*pDeferred
46f03 29 29 3b 0a 20 20 69 66 28 20 21 70 44 65 66 65  ));.  if( !pDefe
46f04 72 72 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75  rred ){.    retu
46f05 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
46f06 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 44  .  }.  memset(pD
46f07 65 66 65 72 72 65 64 2c 20 30 2c 20 73 69 7a 65  eferred, 0, size
46f08 6f 66 28 2a 70 44 65 66 65 72 72 65 64 29 29 3b  of(*pDeferred));
46f09 0a 20 20 70 44 65 66 65 72 72 65 64 2d 3e 70 54  .  pDeferred->pT
46f0a 6f 6b 65 6e 20 3d 20 70 54 6f 6b 65 6e 3b 0a 20  oken = pToken;. 
46f0b 20 70 44 65 66 65 72 72 65 64 2d 3e 70 4e 65 78   pDeferred->pNex
46f0c 74 20 3d 20 70 43 73 72 2d 3e 70 44 65 66 65 72  t = pCsr->pDefer
46f0d 72 65 64 3b 20 0a 20 20 70 44 65 66 65 72 72 65  red; .  pDeferre
46f0e 64 2d 3e 69 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a  d->iCol = iCol;.
46f0f 20 20 70 43 73 72 2d 3e 70 44 65 66 65 72 72 65    pCsr->pDeferre
46f10 64 20 3d 20 70 44 65 66 65 72 72 65 64 3b 0a 0a  d = pDeferred;..
46f11 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e    assert( pToken
46f12 2d 3e 70 44 65 66 65 72 72 65 64 3d 3d 30 20 29  ->pDeferred==0 )
46f13 3b 0a 20 20 70 54 6f 6b 65 6e 2d 3e 70 44 65 66  ;.  pToken->pDef
46f14 65 72 72 65 64 20 3d 20 70 44 65 66 65 72 72 65  erred = pDeferre
46f15 64 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  d;..  return SQL
46f16 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
46f17 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 76  ../*.** SQLite v
46f18 61 6c 75 65 20 70 52 6f 77 69 64 20 63 6f 6e 74  alue pRowid cont
46f19 61 69 6e 73 20 74 68 65 20 72 6f 77 69 64 20 6f  ains the rowid o
46f1a 66 20 61 20 72 6f 77 20 74 68 61 74 20 6d 61 79  f a row that may
46f1b 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a   or may not be.*
46f1c 2a 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65  * present in the
46f1d 20 46 54 53 33 20 74 61 62 6c 65 2e 20 49 66 20   FTS3 table. If 
46f1e 69 74 20 69 73 2c 20 64 65 6c 65 74 65 20 69 74  it is, delete it
46f1f 20 61 6e 64 20 61 64 6a 75 73 74 20 74 68 65 20   and adjust the 
46f20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 73  contents.** of s
46f21 75 62 73 69 64 75 61 72 79 20 64 61 74 61 20 73  ubsiduary data s
46f22 74 72 75 63 74 75 72 65 73 20 61 63 63 6f 72 64  tructures accord
46f23 69 6e 67 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  ingly..*/.static
46f24 20 69 6e 74 20 66 74 73 33 44 65 6c 65 74 65 42   int fts3DeleteB
46f25 79 52 6f 77 69 64 28 0a 20 20 46 74 73 33 54 61  yRowid(.  Fts3Ta
46f26 62 6c 65 20 2a 70 2c 20 0a 20 20 73 71 6c 69 74  ble *p, .  sqlit
46f27 65 33 5f 76 61 6c 75 65 20 2a 70 52 6f 77 69 64  e3_value *pRowid
46f28 2c 20 0a 20 20 69 6e 74 20 2a 70 6e 43 68 6e 67  , .  int *pnChng
46f29 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
46f2a 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
46f2b 44 65 63 72 65 6d 65 6e 74 20 69 66 20 72 6f 77  Decrement if row
46f2c 20 69 73 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20   is deleted */. 
46f2d 20 75 33 32 20 2a 61 53 7a 44 65 6c 0a 29 7b 0a   u32 *aSzDel.){.
46f2e 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
46f2f 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20  E_OK;           
46f30 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
46f31 20 2a 2f 0a 20 20 69 6e 74 20 62 46 6f 75 6e 64   */.  int bFound
46f32 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
46f33 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
46f34 20 2a 70 52 6f 77 69 64 20 72 65 61 6c 6c 79 20   *pRowid really 
46f35 69 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  is in the table 
46f36 2a 2f 0a 0a 20 20 66 74 73 33 44 65 6c 65 74 65  */..  fts3Delete
46f37 54 65 72 6d 73 28 26 72 63 2c 20 70 2c 20 70 52  Terms(&rc, p, pR
46f38 6f 77 69 64 2c 20 61 53 7a 44 65 6c 2c 20 26 62  owid, aSzDel, &b
46f39 46 6f 75 6e 64 29 3b 0a 20 20 69 66 28 20 62 46  Found);.  if( bF
46f3a 6f 75 6e 64 20 26 26 20 72 63 3d 3d 53 51 4c 49  ound && rc==SQLI
46f3b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74  TE_OK ){.    int
46f3c 20 69 73 45 6d 70 74 79 20 3d 20 30 3b 20 20 20   isEmpty = 0;   
46f3d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
46f3e 6c 65 74 69 6e 67 20 2a 70 52 6f 77 69 64 20 6c  leting *pRowid l
46f3f 65 61 76 65 73 20 74 68 65 20 74 61 62 6c 65 20  eaves the table 
46f40 65 6d 70 74 79 20 2a 2f 0a 20 20 20 20 72 63 20  empty */.    rc 
46f41 3d 20 66 74 73 33 49 73 45 6d 70 74 79 28 70 2c  = fts3IsEmpty(p,
46f42 20 70 52 6f 77 69 64 2c 20 26 69 73 45 6d 70 74   pRowid, &isEmpt
46f43 79 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  y);.    if( rc==
46f44 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
46f45 20 20 20 69 66 28 20 69 73 45 6d 70 74 79 20 29     if( isEmpty )
46f46 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c  {.        /* Del
46f47 65 74 69 6e 67 20 74 68 69 73 20 72 6f 77 20 6d  eting this row m
46f48 65 61 6e 73 20 74 68 65 20 77 68 6f 6c 65 20 74  eans the whole t
46f49 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 20 49  able is empty. I
46f4a 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20 20 20  n this case.    
46f4b 20 20 20 20 2a 2a 20 64 65 6c 65 74 65 20 74 68      ** delete th
46f4c 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6c  e contents of al
46f4d 6c 20 74 68 72 65 65 20 74 61 62 6c 65 73 20 61  l three tables a
46f4e 6e 64 20 74 68 72 6f 77 20 61 77 61 79 20 61 6e  nd throw away an
46f4f 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74  y.        ** dat
46f50 61 20 69 6e 20 74 68 65 20 70 65 6e 64 69 6e 67  a in the pending
46f51 54 65 72 6d 73 20 68 61 73 68 20 74 61 62 6c 65  Terms hash table
46f52 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63  .  */.        rc
46f53 20 3d 20 66 74 73 33 44 65 6c 65 74 65 41 6c 6c   = fts3DeleteAll
46f54 28 70 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  (p, 1);.        
46f55 2a 70 6e 43 68 6e 67 20 3d 20 30 3b 0a 20 20 20  *pnChng = 0;.   
46f56 20 20 20 20 20 6d 65 6d 73 65 74 28 61 53 7a 44       memset(aSzD
46f57 65 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 33  el, 0, sizeof(u3
46f58 32 29 20 2a 20 28 70 2d 3e 6e 43 6f 6c 75 6d 6e  2) * (p->nColumn
46f59 2b 31 29 20 2a 20 32 29 3b 0a 20 20 20 20 20 20  +1) * 2);.      
46f5a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a  }else{.        *
46f5b 70 6e 43 68 6e 67 20 3d 20 2a 70 6e 43 68 6e 67  pnChng = *pnChng
46f5c 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66   - 1;.        if
46f5d 28 20 70 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c  ( p->zContentTbl
46f5e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
46f5f 20 66 74 73 33 53 71 6c 45 78 65 63 28 26 72 63   fts3SqlExec(&rc
46f60 2c 20 70 2c 20 53 51 4c 5f 44 45 4c 45 54 45 5f  , p, SQL_DELETE_
46f61 43 4f 4e 54 45 4e 54 2c 20 26 70 52 6f 77 69 64  CONTENT, &pRowid
46f62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
46f63 20 20 20 20 20 69 66 28 20 70 2d 3e 62 48 61 73       if( p->bHas
46f64 44 6f 63 73 69 7a 65 20 29 7b 0a 20 20 20 20 20  Docsize ){.     
46f65 20 20 20 20 20 66 74 73 33 53 71 6c 45 78 65 63       fts3SqlExec
46f66 28 26 72 63 2c 20 70 2c 20 53 51 4c 5f 44 45 4c  (&rc, p, SQL_DEL
46f67 45 54 45 5f 44 4f 43 53 49 5a 45 2c 20 26 70 52  ETE_DOCSIZE, &pR
46f68 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20 7d  owid);.        }
46f69 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
46f6a 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
46f6b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
46f6c 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 74 68 65  unction does the
46f6d 20 77 6f 72 6b 20 66 6f 72 20 74 68 65 20 78 55   work for the xU
46f6e 70 64 61 74 65 20 6d 65 74 68 6f 64 20 6f 66 20  pdate method of 
46f6f 46 54 53 33 20 76 69 72 74 75 61 6c 0a 2a 2a 20  FTS3 virtual.** 
46f70 74 61 62 6c 65 73 2e 20 54 68 65 20 73 63 68 65  tables. The sche
46f71 6d 61 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  ma of the virtua
46f72 6c 20 74 61 62 6c 65 20 62 65 69 6e 67 3a 0a 2a  l table being:.*
46f73 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20  *.**     CREATE 
46f74 54 41 42 4c 45 20 3c 74 61 62 6c 65 20 6e 61 6d  TABLE <table nam
46f75 65 3e 28 20 0a 2a 2a 20 20 20 20 20 20 20 3c 75  e>( .**       <u
46f76 73 65 72 20 63 6f 6c 75 6d 6e 73 3e 2c 0a 2a 2a  ser columns>,.**
46f77 20 20 20 20 20 20 20 3c 74 61 62 6c 65 20 6e 61         <table na
46f78 6d 65 3e 20 48 49 44 44 45 4e 2c 20 0a 2a 2a 20  me> HIDDEN, .** 
46f79 20 20 20 20 20 20 64 6f 63 69 64 20 48 49 44 44        docid HIDD
46f7a 45 4e 2c 20 0a 2a 2a 20 20 20 20 20 20 20 3c 6c  EN, .**       <l
46f7b 61 6e 67 69 64 3e 20 48 49 44 44 45 4e 0a 2a 2a  angid> HIDDEN.**
46f7c 20 20 20 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 0a 2a       );.**.** .*
46f7d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
46f7e 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
46f7f 55 70 64 61 74 65 4d 65 74 68 6f 64 28 0a 20 20  UpdateMethod(.  
46f80 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
46f81 74 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  tab,            
46f82 2f 2a 20 46 54 53 33 20 76 74 61 62 20 6f 62 6a  /* FTS3 vtab obj
46f83 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72  ect */.  int nAr
46f84 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g,              
46f85 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
46f86 20 6f 66 20 61 72 67 75 6d 65 6e 74 20 61 72 72   of argument arr
46f87 61 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ay */.  sqlite3_
46f88 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 2c 20 20  value **apVal,  
46f89 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79          /* Array
46f8a 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f   of arguments */
46f8b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
46f8c 2a 70 52 6f 77 69 64 20 20 20 20 20 20 20 20 20  *pRowid         
46f8d 20 20 20 2f 2a 20 4f 55 54 3a 20 54 68 65 20 61     /* OUT: The a
46f8e 66 66 65 63 74 65 64 20 28 6f 72 20 65 66 66 65  ffected (or effe
46f8f 63 74 65 64 29 20 72 6f 77 69 64 20 2a 2f 0a 29  cted) rowid */.)
46f90 7b 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  {.  Fts3Table *p
46f91 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29   = (Fts3Table *)
46f92 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 20  pVtab;.  int rc 
46f93 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
46f94 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
46f95 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69 6e 74  rn Code */.  int
46f96 20 69 73 52 65 6d 6f 76 65 20 3d 20 30 3b 20 20   isRemove = 0;  
46f97 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
46f98 54 72 75 65 20 66 6f 72 20 61 6e 20 55 50 44 41  True for an UPDA
46f99 54 45 20 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a  TE or DELETE */.
46f9a 20 20 75 33 32 20 2a 61 53 7a 49 6e 73 20 3d 20    u32 *aSzIns = 
46f9b 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
46f9c 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66 20 69 6e    /* Sizes of in
46f9d 73 65 72 74 65 64 20 64 6f 63 75 6d 65 6e 74 73  serted documents
46f9e 20 2a 2f 0a 20 20 75 33 32 20 2a 61 53 7a 44 65   */.  u32 *aSzDe
46f9f 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  l = 0;          
46fa0 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f        /* Sizes o
46fa1 66 20 64 65 6c 65 74 65 64 20 64 6f 63 75 6d 65  f deleted docume
46fa2 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68  nts */.  int nCh
46fa3 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ng = 0;         
46fa4 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 74 20           /* Net 
46fa5 63 68 61 6e 67 65 20 69 6e 20 6e 75 6d 62 65 72  change in number
46fa6 20 6f 66 20 64 6f 63 75 6d 65 6e 74 73 20 2a 2f   of documents */
46fa7 0a 20 20 69 6e 74 20 62 49 6e 73 65 72 74 44 6f  .  int bInsertDo
46fa8 6e 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  ne = 0;..  asser
46fa9 74 28 20 70 2d 3e 70 53 65 67 6d 65 6e 74 73 3d  t( p->pSegments=
46faa 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
46fab 0a 20 20 20 20 20 20 6e 41 72 67 3d 3d 31 20 20  .      nArg==1  
46fac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46fad 20 20 20 2f 2a 20 44 45 4c 45 54 45 20 6f 70 65     /* DELETE ope
46fae 72 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 20 7c 7c  rations */.   ||
46faf 20 6e 41 72 67 3d 3d 28 32 20 2b 20 70 2d 3e 6e   nArg==(2 + p->n
46fb0 43 6f 6c 75 6d 6e 20 2b 20 33 29 20 20 2f 2a 20  Column + 3)  /* 
46fb1 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54 45  INSERT or UPDATE
46fb2 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f 0a 20   operations */. 
46fb3 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20   );..  /* Check 
46fb4 66 6f 72 20 61 20 22 73 70 65 63 69 61 6c 22 20  for a "special" 
46fb5 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f 6e  INSERT operation
46fb6 2e 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f 72  . One of the for
46fb7 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 49  m:.  **.  **   I
46fb8 4e 53 45 52 54 20 49 4e 54 4f 20 78 79 7a 28 78  NSERT INTO xyz(x
46fb9 79 7a 29 20 56 41 4c 55 45 53 28 27 63 6f 6d 6d  yz) VALUES('comm
46fba 61 6e 64 27 29 3b 0a 20 20 2a 2f 0a 20 20 69 66  and');.  */.  if
46fbb 28 20 6e 41 72 67 3e 31 20 0a 20 20 20 26 26 20  ( nArg>1 .   && 
46fbc 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
46fbd 70 65 28 61 70 56 61 6c 5b 30 5d 29 3d 3d 53 51  pe(apVal[0])==SQ
46fbe 4c 49 54 45 5f 4e 55 4c 4c 20 0a 20 20 20 26 26  LITE_NULL .   &&
46fbf 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
46fc0 79 70 65 28 61 70 56 61 6c 5b 70 2d 3e 6e 43 6f  ype(apVal[p->nCo
46fc1 6c 75 6d 6e 2b 32 5d 29 21 3d 53 51 4c 49 54 45  lumn+2])!=SQLITE
46fc2 5f 4e 55 4c 4c 20 0a 20 20 29 7b 0a 20 20 20 20  _NULL .  ){.    
46fc3 72 63 20 3d 20 66 74 73 33 53 70 65 63 69 61 6c  rc = fts3Special
46fc4 49 6e 73 65 72 74 28 70 2c 20 61 70 56 61 6c 5b  Insert(p, apVal[
46fc5 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 5d 29 3b 0a  p->nColumn+2]);.
46fc6 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
46fc7 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  out;.  }..  if( 
46fc8 6e 41 72 67 3e 31 20 26 26 20 73 71 6c 69 74 65  nArg>1 && sqlite
46fc9 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 70 56 61  3_value_int(apVa
46fca 6c 5b 32 20 2b 20 70 2d 3e 6e 43 6f 6c 75 6d 6e  l[2 + p->nColumn
46fcb 20 2b 20 32 5d 29 3c 30 20 29 7b 0a 20 20 20 20   + 2])<0 ){.    
46fcc 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53  rc = SQLITE_CONS
46fcd 54 52 41 49 4e 54 3b 0a 20 20 20 20 67 6f 74 6f  TRAINT;.    goto
46fce 20 75 70 64 61 74 65 5f 6f 75 74 3b 0a 20 20 7d   update_out;.  }
46fcf 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
46fd0 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  space to hold th
46fd1 65 20 63 68 61 6e 67 65 20 69 6e 20 64 6f 63 75  e change in docu
46fd2 6d 65 6e 74 20 73 69 7a 65 73 20 2a 2f 0a 20 20  ment sizes */.  
46fd3 61 53 7a 44 65 6c 20 3d 20 73 71 6c 69 74 65 33  aSzDel = sqlite3
46fd4 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28  _malloc( sizeof(
46fd5 61 53 7a 44 65 6c 5b 30 5d 29 2a 28 70 2d 3e 6e  aSzDel[0])*(p->n
46fd6 43 6f 6c 75 6d 6e 2b 31 29 2a 32 20 29 3b 0a 20  Column+1)*2 );. 
46fd7 20 69 66 28 20 61 53 7a 44 65 6c 3d 3d 30 20 29   if( aSzDel==0 )
46fd8 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
46fd9 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74  E_NOMEM;.    got
46fda 6f 20 75 70 64 61 74 65 5f 6f 75 74 3b 0a 20 20  o update_out;.  
46fdb 7d 0a 20 20 61 53 7a 49 6e 73 20 3d 20 26 61 53  }.  aSzIns = &aS
46fdc 7a 44 65 6c 5b 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b  zDel[p->nColumn+
46fdd 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 53 7a  1];.  memset(aSz
46fde 44 65 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61  Del, 0, sizeof(a
46fdf 53 7a 44 65 6c 5b 30 5d 29 2a 28 70 2d 3e 6e 43  SzDel[0])*(p->nC
46fe0 6f 6c 75 6d 6e 2b 31 29 2a 32 29 3b 0a 0a 20 20  olumn+1)*2);..  
46fe1 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  /* If this is an
46fe2 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f   INSERT operatio
46fe3 6e 2c 20 6f 72 20 61 6e 20 55 50 44 41 54 45 20  n, or an UPDATE 
46fe4 74 68 61 74 20 6d 6f 64 69 66 69 65 73 20 74 68  that modifies th
46fe5 65 20 72 6f 77 69 64 0a 20 20 2a 2a 20 76 61 6c  e rowid.  ** val
46fe6 75 65 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70  ue, then this op
46fe7 65 72 61 74 69 6f 6e 20 72 65 71 75 69 72 65 73  eration requires
46fe8 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 61 6e 64   constraint hand
46fe9 6c 69 6e 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ling..  **.  ** 
46fea 49 66 20 74 68 65 20 6f 6e 2d 63 6f 6e 66 6c 69  If the on-confli
46feb 63 74 20 6d 6f 64 65 20 69 73 20 52 45 50 4c 41  ct mode is REPLA
46fec 43 45 2c 20 74 68 69 73 20 6d 65 61 6e 73 20 74  CE, this means t
46fed 68 61 74 20 74 68 65 20 65 78 69 73 74 69 6e 67  hat the existing
46fee 20 72 6f 77 0a 20 20 2a 2a 20 73 68 6f 75 6c 64   row.  ** should
46fef 20 62 65 20 64 65 6c 65 74 65 64 20 66 72 6f 6d   be deleted from
46ff0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 65   the database be
46ff1 66 6f 72 65 20 69 6e 73 65 72 74 69 6e 67 20 74  fore inserting t
46ff2 68 65 20 6e 65 77 20 72 6f 77 2e 20 4f 72 2c 0a  he new row. Or,.
46ff3 20 20 2a 2a 20 69 66 20 74 68 65 20 6f 6e 2d 63    ** if the on-c
46ff4 6f 6e 66 6c 69 63 74 20 6d 6f 64 65 20 69 73 20  onflict mode is 
46ff5 6f 74 68 65 72 20 74 68 61 6e 20 52 45 50 4c 41  other than REPLA
46ff6 43 45 2c 20 74 68 65 6e 20 74 68 69 73 20 6d 65  CE, then this me
46ff7 74 68 6f 64 20 6d 75 73 74 0a 20 20 2a 2a 20 64  thod must.  ** d
46ff8 65 74 65 63 74 20 74 68 65 20 63 6f 6e 66 6c 69  etect the confli
46ff9 63 74 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51  ct and return SQ
46ffa 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20  LITE_CONSTRAINT 
46ffb 62 65 66 6f 72 65 20 62 65 67 69 6e 6e 69 6e 67  before beginning
46ffc 20 74 6f 0a 20 20 2a 2a 20 6d 6f 64 69 66 79 20   to.  ** modify 
46ffd 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
46ffe 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 41  e..  */.  if( nA
46fff 72 67 3e 31 20 26 26 20 70 2d 3e 7a 43 6f 6e 74  rg>1 && p->zCont
47000 65 6e 74 54 62 6c 3d 3d 30 20 29 7b 0a 20 20 20  entTbl==0 ){.   
47001 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 76 61 6c   /* Find the val
47002 75 65 20 6f 62 6a 65 63 74 20 74 68 61 74 20 68  ue object that h
47003 6f 6c 64 73 20 74 68 65 20 6e 65 77 20 72 6f 77  olds the new row
47004 69 64 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20  id value. */.   
47005 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
47006 70 4e 65 77 52 6f 77 69 64 20 3d 20 61 70 56 61  pNewRowid = apVa
47007 6c 5b 33 2b 70 2d 3e 6e 43 6f 6c 75 6d 6e 5d 3b  l[3+p->nColumn];
47008 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
47009 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 4e 65 77  _value_type(pNew
4700a 52 6f 77 69 64 29 3d 3d 53 51 4c 49 54 45 5f 4e  Rowid)==SQLITE_N
4700b 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 70 4e 65  ULL ){.      pNe
4700c 77 52 6f 77 69 64 20 3d 20 61 70 56 61 6c 5b 31  wRowid = apVal[1
4700d 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  ];.    }..    if
4700e 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
4700f 74 79 70 65 28 70 4e 65 77 52 6f 77 69 64 29 21  type(pNewRowid)!
47010 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 26 26 20  =SQLITE_NULL && 
47011 28 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ( .        sqlit
47012 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 70  e3_value_type(ap
47013 56 61 6c 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f  Val[0])==SQLITE_
47014 4e 55 4c 4c 0a 20 20 20 20 20 7c 7c 20 73 71 6c  NULL.     || sql
47015 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
47016 28 61 70 56 61 6c 5b 30 5d 29 21 3d 73 71 6c 69  (apVal[0])!=sqli
47017 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28  te3_value_int64(
47018 70 4e 65 77 52 6f 77 69 64 29 0a 20 20 20 20 29  pNewRowid).    )
47019 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ){.      /* The 
4701a 6e 65 77 20 72 6f 77 69 64 20 69 73 20 6e 6f 74  new rowid is not
4701b 20 4e 55 4c 4c 20 28 69 6e 20 74 68 69 73 20 63   NULL (in this c
4701c 61 73 65 20 74 68 65 20 72 6f 77 69 64 20 77 69  ase the rowid wi
4701d 6c 6c 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 61  ll be.      ** a
4701e 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61 73 73  utomatically ass
4701f 69 67 6e 65 64 20 61 6e 64 20 74 68 65 72 65 20  igned and there 
47020 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 6f 66 20  is no chance of 
47021 61 20 63 6f 6e 66 6c 69 63 74 29 2c 20 61 6e 64  a conflict), and
47022 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73   .      ** the s
47023 74 61 74 65 6d 65 6e 74 20 69 73 20 65 69 74 68  tatement is eith
47024 65 72 20 61 6e 20 49 4e 53 45 52 54 20 6f 72 20  er an INSERT or 
47025 61 6e 20 55 50 44 41 54 45 20 74 68 61 74 20 6d  an UPDATE that m
47026 6f 64 69 66 69 65 73 20 74 68 65 0a 20 20 20 20  odifies the.    
47027 20 20 2a 2a 20 72 6f 77 69 64 20 63 6f 6c 75 6d    ** rowid colum
47028 6e 2e 20 53 6f 20 69 66 20 74 68 65 20 63 6f 6e  n. So if the con
47029 66 6c 69 63 74 20 6d 6f 64 65 20 69 73 20 52 45  flict mode is RE
4702a 50 4c 41 43 45 2c 20 74 68 65 6e 20 64 65 6c 65  PLACE, then dele
4702b 74 65 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20  te any.      ** 
4702c 65 78 69 73 74 69 6e 67 20 72 6f 77 20 77 69 74  existing row wit
4702d 68 20 72 6f 77 69 64 3d 70 4e 65 77 52 6f 77 69  h rowid=pNewRowi
4702e 64 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  d. .      **.   
4702f 20 20 20 2a 2a 20 4f 72 2c 20 69 66 20 74 68 65     ** Or, if the
47030 20 63 6f 6e 66 6c 69 63 74 20 6d 6f 64 65 20 69   conflict mode i
47031 73 20 6e 6f 74 20 52 45 50 4c 41 43 45 2c 20 69  s not REPLACE, i
47032 6e 73 65 72 74 20 74 68 65 20 6e 65 77 20 72 65  nsert the new re
47033 63 6f 72 64 20 69 6e 74 6f 20 0a 20 20 20 20 20  cord into .     
47034 20 2a 2a 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e   ** the %_conten
47035 74 20 74 61 62 6c 65 2e 20 49 66 20 77 65 20 68  t table. If we h
47036 69 74 20 74 68 65 20 64 75 70 6c 69 63 61 74 65  it the duplicate
47037 20 72 6f 77 69 64 20 63 6f 6e 73 74 72 61 69 6e   rowid constrain
47038 74 20 28 6f 72 20 61 6e 79 0a 20 20 20 20 20 20  t (or any.      
47039 2a 2a 20 6f 74 68 65 72 20 65 72 72 6f 72 29 20  ** other error) 
4703a 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f 2c 20  while doing so, 
4703b 72 65 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65  return immediate
4703c 6c 79 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ly..      **.   
4703d 20 20 20 2a 2a 20 54 68 69 73 20 62 72 61 6e 63     ** This branc
4703e 68 20 6d 61 79 20 61 6c 73 6f 20 72 75 6e 20 69  h may also run i
4703f 66 20 70 4e 65 77 52 6f 77 69 64 20 63 6f 6e 74  f pNewRowid cont
47040 61 69 6e 73 20 61 20 76 61 6c 75 65 20 74 68 61  ains a value tha
47041 74 20 63 61 6e 6e 6f 74 0a 20 20 20 20 20 20 2a  t cannot.      *
47042 2a 20 62 65 20 6c 6f 73 73 6c 65 73 73 6c 79 20  * be losslessly 
47043 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 6e 20  converted to an 
47044 69 6e 74 65 67 65 72 2e 20 49 6e 20 74 68 69 73  integer. In this
47045 20 63 61 73 65 2c 20 74 68 65 20 65 76 65 6e 74   case, the event
47046 75 61 6c 20 0a 20 20 20 20 20 20 2a 2a 20 63 61  ual .      ** ca
47047 6c 6c 20 74 6f 20 66 74 73 33 49 6e 73 65 72 74  ll to fts3Insert
47048 44 61 74 61 28 29 20 28 65 69 74 68 65 72 20 6a  Data() (either j
47049 75 73 74 20 62 65 6c 6f 77 20 6f 72 20 66 75 72  ust below or fur
4704a 74 68 65 72 20 6f 6e 20 69 6e 20 74 68 69 73 0a  ther on in this.
4704b 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f        ** functio
4704c 6e 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53  n) will return S
4704d 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 2e 20  QLITE_MISMATCH. 
4704e 49 66 20 66 74 73 33 44 65 6c 65 74 65 42 79 52  If fts3DeleteByR
4704f 6f 77 69 64 20 69 73 20 0a 20 20 20 20 20 20 2a  owid is .      *
47050 2a 20 69 6e 76 6f 6b 65 64 2c 20 69 74 20 77 69  * invoked, it wi
47051 6c 6c 20 64 65 6c 65 74 65 20 7a 65 72 6f 20 72  ll delete zero r
47052 6f 77 73 20 28 73 69 6e 63 65 20 6e 6f 20 72 6f  ows (since no ro
47053 77 20 77 69 6c 6c 20 68 61 76 65 0a 20 20 20 20  w will have.    
47054 20 20 2a 2a 20 64 6f 63 69 64 3d 24 70 4e 65 77    ** docid=$pNew
47055 52 6f 77 69 64 20 69 66 20 24 70 4e 65 77 52 6f  Rowid if $pNewRo
47056 77 69 64 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e  wid is not an in
47057 74 65 67 65 72 20 76 61 6c 75 65 29 2e 0a 20 20  teger value)..  
47058 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
47059 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 6f 6e   sqlite3_vtab_on
4705a 5f 63 6f 6e 66 6c 69 63 74 28 70 2d 3e 64 62 29  _conflict(p->db)
4705b 3d 3d 53 51 4c 49 54 45 5f 52 45 50 4c 41 43 45  ==SQLITE_REPLACE
4705c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
4705d 20 66 74 73 33 44 65 6c 65 74 65 42 79 52 6f 77   fts3DeleteByRow
4705e 69 64 28 70 2c 20 70 4e 65 77 52 6f 77 69 64 2c  id(p, pNewRowid,
4705f 20 26 6e 43 68 6e 67 2c 20 61 53 7a 44 65 6c 29   &nChng, aSzDel)
47060 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
47061 20 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33         rc = fts3
47062 49 6e 73 65 72 74 44 61 74 61 28 70 2c 20 61 70  InsertData(p, ap
47063 56 61 6c 2c 20 70 52 6f 77 69 64 29 3b 0a 20 20  Val, pRowid);.  
47064 20 20 20 20 20 20 62 49 6e 73 65 72 74 44 6f 6e        bInsertDon
47065 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  e = 1;.      }. 
47066 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72     }.  }.  if( r
47067 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
47068 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
47069 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  out;.  }..  /* I
4706a 66 20 74 68 69 73 20 69 73 20 61 20 44 45 4c 45  f this is a DELE
4706b 54 45 20 6f 72 20 55 50 44 41 54 45 20 6f 70 65  TE or UPDATE ope
4706c 72 61 74 69 6f 6e 2c 20 72 65 6d 6f 76 65 20 74  ration, remove t
4706d 68 65 20 6f 6c 64 20 72 65 63 6f 72 64 2e 20 2a  he old record. *
4706e 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  /.  if( sqlite3_
4706f 76 61 6c 75 65 5f 74 79 70 65 28 61 70 56 61 6c  value_type(apVal
47070 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c  [0])!=SQLITE_NUL
47071 4c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  L ){.    assert(
47072 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
47073 79 70 65 28 61 70 56 61 6c 5b 30 5d 29 3d 3d 53  ype(apVal[0])==S
47074 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 29 3b  QLITE_INTEGER );
47075 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 44 65  .    rc = fts3De
47076 6c 65 74 65 42 79 52 6f 77 69 64 28 70 2c 20 61  leteByRowid(p, a
47077 70 56 61 6c 5b 30 5d 2c 20 26 6e 43 68 6e 67 2c  pVal[0], &nChng,
47078 20 61 53 7a 44 65 6c 29 3b 0a 20 20 20 20 69 73   aSzDel);.    is
47079 52 65 6d 6f 76 65 20 3d 20 31 3b 0a 20 20 7d 0a  Remove = 1;.  }.
4707a 20 20 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20    .  /* If this 
4707b 69 73 20 61 6e 20 49 4e 53 45 52 54 20 6f 72 20  is an INSERT or 
4707c 55 50 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e  UPDATE operation
4707d 2c 20 69 6e 73 65 72 74 20 74 68 65 20 6e 65 77  , insert the new
4707e 20 72 65 63 6f 72 64 2e 20 2a 2f 0a 20 20 69 66   record. */.  if
4707f 28 20 6e 41 72 67 3e 31 20 26 26 20 72 63 3d 3d  ( nArg>1 && rc==
47080 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
47081 20 69 6e 74 20 69 4c 61 6e 67 69 64 20 3d 20 73   int iLangid = s
47082 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
47083 28 61 70 56 61 6c 5b 32 20 2b 20 70 2d 3e 6e 43  (apVal[2 + p->nC
47084 6f 6c 75 6d 6e 20 2b 20 32 5d 29 3b 0a 20 20 20  olumn + 2]);.   
47085 20 69 66 28 20 62 49 6e 73 65 72 74 44 6f 6e 65   if( bInsertDone
47086 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ==0 ){.      rc 
47087 3d 20 66 74 73 33 49 6e 73 65 72 74 44 61 74 61  = fts3InsertData
47088 28 70 2c 20 61 70 56 61 6c 2c 20 70 52 6f 77 69  (p, apVal, pRowi
47089 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  d);.      if( rc
4708a 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  ==SQLITE_CONSTRA
4708b 49 4e 54 20 26 26 20 70 2d 3e 7a 43 6f 6e 74 65  INT && p->zConte
4708c 6e 74 54 62 6c 3d 3d 30 20 29 7b 0a 20 20 20 20  ntTbl==0 ){.    
4708d 20 20 20 20 72 63 20 3d 20 46 54 53 5f 43 4f 52      rc = FTS_COR
4708e 52 55 50 54 5f 56 54 41 42 3b 0a 20 20 20 20 20  RUPT_VTAB;.     
4708f 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
47090 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
47091 26 20 28 21 69 73 52 65 6d 6f 76 65 20 7c 7c 20  & (!isRemove || 
47092 2a 70 52 6f 77 69 64 21 3d 70 2d 3e 69 50 72 65  *pRowid!=p->iPre
47093 76 44 6f 63 69 64 20 29 20 29 7b 0a 20 20 20 20  vDocid ) ){.    
47094 20 20 72 63 20 3d 20 66 74 73 33 50 65 6e 64 69    rc = fts3Pendi
47095 6e 67 54 65 72 6d 73 44 6f 63 69 64 28 70 2c 20  ngTermsDocid(p, 
47096 69 4c 61 6e 67 69 64 2c 20 2a 70 52 6f 77 69 64  iLangid, *pRowid
47097 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
47098 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
47099 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
4709a 70 2d 3e 69 50 72 65 76 44 6f 63 69 64 3d 3d 2a  p->iPrevDocid==*
4709b 70 52 6f 77 69 64 20 29 3b 0a 20 20 20 20 20 20  pRowid );.      
4709c 72 63 20 3d 20 66 74 73 33 49 6e 73 65 72 74 54  rc = fts3InsertT
4709d 65 72 6d 73 28 70 2c 20 69 4c 61 6e 67 69 64 2c  erms(p, iLangid,
4709e 20 61 70 56 61 6c 2c 20 61 53 7a 49 6e 73 29 3b   apVal, aSzIns);
4709f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
470a0 2d 3e 62 48 61 73 44 6f 63 73 69 7a 65 20 29 7b  ->bHasDocsize ){
470a1 0a 20 20 20 20 20 20 66 74 73 33 49 6e 73 65 72  .      fts3Inser
470a2 74 44 6f 63 73 69 7a 65 28 26 72 63 2c 20 70 2c  tDocsize(&rc, p,
470a3 20 61 53 7a 49 6e 73 29 3b 0a 20 20 20 20 7d 0a   aSzIns);.    }.
470a4 20 20 20 20 6e 43 68 6e 67 2b 2b 3b 0a 20 20 7d      nChng++;.  }
470a5 0a 0a 20 20 69 66 28 20 70 2d 3e 62 46 74 73 34  ..  if( p->bFts4
470a6 20 29 7b 0a 20 20 20 20 66 74 73 33 55 70 64 61   ){.    fts3Upda
470a7 74 65 44 6f 63 54 6f 74 61 6c 73 28 26 72 63 2c  teDocTotals(&rc,
470a8 20 70 2c 20 61 53 7a 49 6e 73 2c 20 61 53 7a 44   p, aSzIns, aSzD
470a9 65 6c 2c 20 6e 43 68 6e 67 29 3b 0a 20 20 7d 0a  el, nChng);.  }.
470aa 0a 20 75 70 64 61 74 65 5f 6f 75 74 3a 0a 20 20  . update_out:.  
470ab 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 53 7a  sqlite3_free(aSz
470ac 44 65 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 46  Del);.  sqlite3F
470ad 74 73 33 53 65 67 6d 65 6e 74 73 43 6c 6f 73 65  ts3SegmentsClose
470ae 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
470af 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 46 6c 75 73  ;.}../* .** Flus
470b0 68 20 61 6e 79 20 64 61 74 61 20 69 6e 20 74 68  h any data in th
470b1 65 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20  e pending-terms 
470b2 68 61 73 68 20 74 61 62 6c 65 20 74 6f 20 64 69  hash table to di
470b3 73 6b 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  sk. If successfu
470b4 6c 2c 0a 2a 2a 20 6d 65 72 67 65 20 61 6c 6c 20  l,.** merge all 
470b5 73 65 67 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  segments in the 
470b6 64 61 74 61 62 61 73 65 20 28 69 6e 63 6c 75 64  database (includ
470b7 69 6e 67 20 74 68 65 20 6e 65 77 20 73 65 67 6d  ing the new segm
470b8 65 6e 74 2c 20 69 66 20 0a 2a 2a 20 74 68 65 72  ent, if .** ther
470b9 65 20 77 61 73 20 61 6e 79 20 64 61 74 61 20 74  e was any data t
470ba 6f 20 66 6c 75 73 68 29 20 69 6e 74 6f 20 61 20  o flush) into a 
470bb 73 69 6e 67 6c 65 20 73 65 67 6d 65 6e 74 2e 20  single segment. 
470bc 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
470bd 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
470be 73 33 4f 70 74 69 6d 69 7a 65 28 46 74 73 33 54  s3Optimize(Fts3T
470bf 61 62 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  able *p){.  int 
470c0 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  rc;.  rc = sqlit
470c1 65 33 5f 65 78 65 63 28 70 2d 3e 64 62 2c 20 22  e3_exec(p->db, "
470c2 53 41 56 45 50 4f 49 4e 54 20 66 74 73 33 22 2c  SAVEPOINT fts3",
470c3 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28   0, 0, 0);.  if(
470c4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
470c5 7b 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 44  {.    rc = fts3D
470c6 6f 4f 70 74 69 6d 69 7a 65 28 70 2c 20 31 29 3b  oOptimize(p, 1);
470c7 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
470c8 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51  ITE_OK || rc==SQ
470c9 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
470ca 20 20 20 69 6e 74 20 72 63 32 20 3d 20 73 71 6c     int rc2 = sql
470cb 69 74 65 33 5f 65 78 65 63 28 70 2d 3e 64 62 2c  ite3_exec(p->db,
470cc 20 22 52 45 4c 45 41 53 45 20 66 74 73 33 22 2c   "RELEASE fts3",
470cd 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20   0, 0, 0);.     
470ce 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45   if( rc2!=SQLITE
470cf 5f 4f 4b 20 29 20 72 63 20 3d 20 72 63 32 3b 0a  _OK ) rc = rc2;.
470d0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
470d1 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 70 2d   sqlite3_exec(p-
470d2 3e 64 62 2c 20 22 52 4f 4c 4c 42 41 43 4b 20 54  >db, "ROLLBACK T
470d3 4f 20 66 74 73 33 22 2c 20 30 2c 20 30 2c 20 30  O fts3", 0, 0, 0
470d4 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
470d5 5f 65 78 65 63 28 70 2d 3e 64 62 2c 20 22 52 45  _exec(p->db, "RE
470d6 4c 45 41 53 45 20 66 74 73 33 22 2c 20 30 2c 20  LEASE fts3", 0, 
470d7 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  0, 0);.    }.  }
470d8 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 53 65  .  sqlite3Fts3Se
470d9 67 6d 65 6e 74 73 43 6c 6f 73 65 28 70 29 3b 0a  gmentsClose(p);.
470da 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
470db 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
470dc 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66  ******* End of f
470dd 74 73 33 5f 77 72 69 74 65 2e 63 20 2a 2a 2a 2a  ts3_write.c ****
470de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
470df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
470e0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
470e1 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
470e2 6c 65 20 66 74 73 33 5f 73 6e 69 70 70 65 74 2e  le fts3_snippet.
470e3 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
470e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
470e5 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
470e6 30 39 20 4f 63 74 20 32 33 0a 2a 2a 0a 2a 2a 20  09 Oct 23.**.** 
470e7 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
470e8 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
470e9 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
470ea 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
470eb 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
470ec 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
470ed 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
470ee 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
470ef 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
470f0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
470f1 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
470f2 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
470f3 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
470f4 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
470f5 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
470f6 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
470f7 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
470f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
470f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
470fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
470fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
470fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2f  *************.*/
470fd 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
470fe 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
470ff 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
47100 41 42 4c 45 5f 46 54 53 33 29 0a 0a 2f 2a 20 23  ABLE_FTS3)../* #
47101 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e  include <string.
47102 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64  h> */./* #includ
47103 65 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a 2f 0a  e <assert.h> */.
47104 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74 65 72  ./*.** Character
47105 73 20 74 68 61 74 20 6d 61 79 20 61 70 70 65 61  s that may appea
47106 72 20 69 6e 20 74 68 65 20 73 65 63 6f 6e 64 20  r in the second 
47107 61 72 67 75 6d 65 6e 74 20 74 6f 20 6d 61 74 63  argument to matc
47108 68 69 6e 66 6f 28 29 2e 0a 2a 2f 0a 23 64 65 66  hinfo()..*/.#def
47109 69 6e 65 20 46 54 53 33 5f 4d 41 54 43 48 49 4e  ine FTS3_MATCHIN
4710a 46 4f 5f 4e 50 48 52 41 53 45 20 20 20 27 70 27  FO_NPHRASE   'p'
4710b 20 20 20 20 20 20 20 20 2f 2a 20 31 20 76 61 6c          /* 1 val
4710c 75 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54  ue */.#define FT
4710d 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4e 43 4f  S3_MATCHINFO_NCO
4710e 4c 20 20 20 20 20 20 27 63 27 20 20 20 20 20 20  L      'c'      
4710f 20 20 2f 2a 20 31 20 76 61 6c 75 65 20 2a 2f 0a    /* 1 value */.
47110 23 64 65 66 69 6e 65 20 46 54 53 33 5f 4d 41 54  #define FTS3_MAT
47111 43 48 49 4e 46 4f 5f 4e 44 4f 43 20 20 20 20 20  CHINFO_NDOC     
47112 20 27 6e 27 20 20 20 20 20 20 20 20 2f 2a 20 31   'n'        /* 1
47113 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e   value */.#defin
47114 65 20 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f  e FTS3_MATCHINFO
47115 5f 41 56 47 4c 45 4e 47 54 48 20 27 61 27 20 20  _AVGLENGTH 'a'  
47116 20 20 20 20 20 20 2f 2a 20 6e 43 6f 6c 20 76 61        /* nCol va
47117 6c 75 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  lues */.#define 
47118 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4c  FTS3_MATCHINFO_L
47119 45 4e 47 54 48 20 20 20 20 27 6c 27 20 20 20 20  ENGTH    'l'    
4711a 20 20 20 20 2f 2a 20 6e 43 6f 6c 20 76 61 6c 75      /* nCol valu
4711b 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54  es */.#define FT
4711c 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4c 43 53  S3_MATCHINFO_LCS
4711d 20 20 20 20 20 20 20 27 73 27 20 20 20 20 20 20         's'      
4711e 20 20 2f 2a 20 6e 43 6f 6c 20 76 61 6c 75 65 73    /* nCol values
4711f 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 33   */.#define FTS3
47120 5f 4d 41 54 43 48 49 4e 46 4f 5f 48 49 54 53 20  _MATCHINFO_HITS 
47121 20 20 20 20 20 27 78 27 20 20 20 20 20 20 20 20       'x'        
47122 2f 2a 20 33 2a 6e 43 6f 6c 2a 6e 50 68 72 61 73  /* 3*nCol*nPhras
47123 65 20 76 61 6c 75 65 73 20 2a 2f 0a 0a 2f 2a 0a  e values */../*.
47124 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 76  ** The default v
47125 61 6c 75 65 20 66 6f 72 20 74 68 65 20 73 65 63  alue for the sec
47126 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
47127 6d 61 74 63 68 69 6e 66 6f 28 29 2e 20 0a 2a 2f  matchinfo(). .*/
47128 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 4d 41  .#define FTS3_MA
47129 54 43 48 49 4e 46 4f 5f 44 45 46 41 55 4c 54 20  TCHINFO_DEFAULT 
4712a 20 20 22 70 63 78 22 0a 0a 0a 2f 2a 0a 2a 2a 20    "pcx".../*.** 
4712b 55 73 65 64 20 61 73 20 61 6e 20 66 74 73 33 45  Used as an fts3E
4712c 78 70 72 49 74 65 72 61 74 65 28 29 20 63 6f 6e  xprIterate() con
4712d 74 65 78 74 20 77 68 65 6e 20 6c 6f 61 64 69 6e  text when loadin
4712e 67 20 70 68 72 61 73 65 20 64 6f 63 6c 69 73 74  g phrase doclist
4712f 73 20 74 6f 0a 2a 2a 20 46 74 73 33 45 78 70 72  s to.** Fts3Expr
47130 2e 61 44 6f 63 6c 69 73 74 5b 5d 2f 6e 44 6f 63  .aDoclist[]/nDoc
47131 6c 69 73 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  list..*/.typedef
47132 20 73 74 72 75 63 74 20 4c 6f 61 64 44 6f 63 6c   struct LoadDocl
47133 69 73 74 43 74 78 20 4c 6f 61 64 44 6f 63 6c 69  istCtx LoadDocli
47134 73 74 43 74 78 3b 0a 73 74 72 75 63 74 20 4c 6f  stCtx;.struct Lo
47135 61 64 44 6f 63 6c 69 73 74 43 74 78 20 7b 0a 20  adDoclistCtx {. 
47136 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73   Fts3Cursor *pCs
47137 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
47138 20 2f 2a 20 46 54 53 33 20 43 75 72 73 6f 72 20   /* FTS3 Cursor 
47139 2a 2f 0a 20 20 69 6e 74 20 6e 50 68 72 61 73 65  */.  int nPhrase
4713a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4713b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
4713c 66 20 70 68 72 61 73 65 73 20 73 65 65 6e 20 73  f phrases seen s
4713d 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  o far */.  int n
4713e 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20  Token;          
4713f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
47140 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 73  mber of tokens s
47141 65 65 6e 20 73 6f 20 66 61 72 20 2a 2f 0a 7d 3b  een so far */.};
47142 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
47143 6f 77 69 6e 67 20 74 79 70 65 73 20 61 72 65 20  owing types are 
47144 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  used as part of 
47145 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
47146 6f 6e 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 74  on of the .** ft
47147 73 33 42 65 73 74 53 6e 69 70 70 65 74 28 29 20  s3BestSnippet() 
47148 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 74 79 70 65  routine..*/.type
47149 64 65 66 20 73 74 72 75 63 74 20 53 6e 69 70 70  def struct Snipp
4714a 65 74 49 74 65 72 20 53 6e 69 70 70 65 74 49 74  etIter SnippetIt
4714b 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  er;.typedef stru
4714c 63 74 20 53 6e 69 70 70 65 74 50 68 72 61 73 65  ct SnippetPhrase
4714d 20 53 6e 69 70 70 65 74 50 68 72 61 73 65 3b 0a   SnippetPhrase;.
4714e 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
4714f 6e 69 70 70 65 74 46 72 61 67 6d 65 6e 74 20 53  nippetFragment S
47150 6e 69 70 70 65 74 46 72 61 67 6d 65 6e 74 3b 0a  nippetFragment;.
47151 0a 73 74 72 75 63 74 20 53 6e 69 70 70 65 74 49  .struct SnippetI
47152 74 65 72 20 7b 0a 20 20 46 74 73 33 43 75 72 73  ter {.  Fts3Curs
47153 6f 72 20 2a 70 43 73 72 3b 20 20 20 20 20 20 20  or *pCsr;       
47154 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
47155 72 20 73 6e 69 70 70 65 74 20 69 73 20 62 65 69  r snippet is bei
47156 6e 67 20 67 65 6e 65 72 61 74 65 64 20 66 72 6f  ng generated fro
47157 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 3b  m */.  int iCol;
47158 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47159 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 63         /* Extrac
4715a 74 20 73 6e 69 70 70 65 74 20 66 72 6f 6d 20 74  t snippet from t
4715b 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  his column */.  
4715c 69 6e 74 20 6e 53 6e 69 70 70 65 74 3b 20 20 20  int nSnippet;   
4715d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4715e 2f 2a 20 52 65 71 75 65 73 74 65 64 20 73 6e 69  /* Requested sni
4715f 70 70 65 74 20 6c 65 6e 67 74 68 20 28 69 6e 20  ppet length (in 
47160 74 6f 6b 65 6e 73 29 20 2a 2f 0a 20 20 69 6e 74  tokens) */.  int
47161 20 6e 50 68 72 61 73 65 3b 20 20 20 20 20 20 20   nPhrase;       
47162 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47163 4e 75 6d 62 65 72 20 6f 66 20 70 68 72 61 73 65  Number of phrase
47164 73 20 69 6e 20 71 75 65 72 79 20 2a 2f 0a 20 20  s in query */.  
47165 53 6e 69 70 70 65 74 50 68 72 61 73 65 20 2a 61  SnippetPhrase *a
47166 50 68 72 61 73 65 3b 20 20 20 20 20 20 20 20 20  Phrase;         
47167 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 69 7a 65  /* Array of size
47168 20 6e 50 68 72 61 73 65 20 2a 2f 0a 20 20 69 6e   nPhrase */.  in
47169 74 20 69 43 75 72 72 65 6e 74 3b 20 20 20 20 20  t iCurrent;     
4716a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4716b 20 46 69 72 73 74 20 74 6f 6b 65 6e 20 6f 66 20   First token of 
4716c 63 75 72 72 65 6e 74 20 73 6e 69 70 70 65 74 20  current snippet 
4716d 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 53 6e  */.};..struct Sn
4716e 69 70 70 65 74 50 68 72 61 73 65 20 7b 0a 20 20  ippetPhrase {.  
4716f 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20 20  int nToken;     
47170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47171 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b  /* Number of tok
47172 65 6e 73 20 69 6e 20 70 68 72 61 73 65 20 2a 2f  ens in phrase */
47173 0a 20 20 63 68 61 72 20 2a 70 4c 69 73 74 3b 20  .  char *pList; 
47174 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47175 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
47176 20 73 74 61 72 74 20 6f 66 20 70 68 72 61 73 65   start of phrase
47177 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 2a   position list *
47178 2f 0a 20 20 69 6e 74 20 69 48 65 61 64 3b 20 20  /.  int iHead;  
47179 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4717a 20 20 20 20 2f 2a 20 4e 65 78 74 20 76 61 6c 75      /* Next valu
4717b 65 20 69 6e 20 70 6f 73 69 74 69 6f 6e 20 6c 69  e in position li
4717c 73 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 48  st */.  char *pH
4717d 65 61 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ead;            
4717e 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 69 74          /* Posit
4717f 69 6f 6e 20 6c 69 73 74 20 64 61 74 61 20 66 6f  ion list data fo
47180 6c 6c 6f 77 69 6e 67 20 69 48 65 61 64 20 2a 2f  llowing iHead */
47181 0a 20 20 69 6e 74 20 69 54 61 69 6c 3b 20 20 20  .  int iTail;   
47182 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47183 20 20 20 2f 2a 20 4e 65 78 74 20 76 61 6c 75 65     /* Next value
47184 20 69 6e 20 74 72 61 69 6c 69 6e 67 20 70 6f 73   in trailing pos
47185 69 74 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20  ition list */.  
47186 63 68 61 72 20 2a 70 54 61 69 6c 3b 20 20 20 20  char *pTail;    
47187 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47188 2f 2a 20 50 6f 73 69 74 69 6f 6e 20 6c 69 73 74  /* Position list
47189 20 64 61 74 61 20 66 6f 6c 6c 6f 77 69 6e 67 20   data following 
4718a 69 54 61 69 6c 20 2a 2f 0a 7d 3b 0a 0a 73 74 72  iTail */.};..str
4718b 75 63 74 20 53 6e 69 70 70 65 74 46 72 61 67 6d  uct SnippetFragm
4718c 65 6e 74 20 7b 0a 20 20 69 6e 74 20 69 43 6f 6c  ent {.  int iCol
4718d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4718e 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
4718f 6e 20 73 6e 69 70 70 65 74 20 69 73 20 65 78 74  n snippet is ext
47190 72 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20  racted from */. 
47191 20 69 6e 74 20 69 50 6f 73 3b 20 20 20 20 20 20   int iPos;      
47192 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47193 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 69 72   /* Index of fir
47194 73 74 20 74 6f 6b 65 6e 20 69 6e 20 73 6e 69 70  st token in snip
47195 70 65 74 20 2a 2f 0a 20 20 75 36 34 20 63 6f 76  pet */.  u64 cov
47196 65 72 65 64 3b 20 20 20 20 20 20 20 20 20 20 20  ered;           
47197 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
47198 20 6f 66 20 71 75 65 72 79 20 70 68 72 61 73 65   of query phrase
47199 73 20 63 6f 76 65 72 65 64 20 2a 2f 0a 20 20 75  s covered */.  u
4719a 36 34 20 68 6c 6d 61 73 6b 3b 20 20 20 20 20 20  64 hlmask;      
4719b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4719c 2a 20 4d 61 73 6b 20 6f 66 20 73 6e 69 70 70 65  * Mask of snippe
4719d 74 20 74 65 72 6d 73 20 74 6f 20 68 69 67 68 6c  t terms to highl
4719e 69 67 68 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ight */.};../*.*
4719f 2a 20 54 68 69 73 20 74 79 70 65 20 69 73 20 75  * This type is u
471a0 73 65 64 20 61 73 20 61 6e 20 66 74 73 33 45 78  sed as an fts3Ex
471a1 70 72 49 74 65 72 61 74 65 28 29 20 63 6f 6e 74  prIterate() cont
471a2 65 78 74 20 6f 62 6a 65 63 74 20 77 68 69 6c 65  ext object while
471a3 20 0a 2a 2a 20 61 63 63 75 6d 75 6c 61 74 69 6e   .** accumulatin
471a4 67 20 74 68 65 20 64 61 74 61 20 72 65 74 75 72  g the data retur
471a5 6e 65 64 20 62 79 20 74 68 65 20 6d 61 74 63 68  ned by the match
471a6 69 6e 66 6f 28 29 20 66 75 6e 63 74 69 6f 6e 2e  info() function.
471a7 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
471a8 63 74 20 4d 61 74 63 68 49 6e 66 6f 20 4d 61 74  ct MatchInfo Mat
471a9 63 68 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20 4d  chInfo;.struct M
471aa 61 74 63 68 49 6e 66 6f 20 7b 0a 20 20 46 74 73  atchInfo {.  Fts
471ab 33 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  3Cursor *pCursor
471ac 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
471ad 46 54 53 33 20 43 75 72 73 6f 72 20 2a 2f 0a 20  FTS3 Cursor */. 
471ae 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20   int nCol;      
471af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
471b0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
471b1 6c 75 6d 6e 73 20 69 6e 20 74 61 62 6c 65 20 2a  lumns in table *
471b2 2f 0a 20 20 69 6e 74 20 6e 50 68 72 61 73 65 3b  /.  int nPhrase;
471b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
471b4 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
471b5 20 6d 61 74 63 68 61 62 6c 65 20 70 68 72 61 73   matchable phras
471b6 65 73 20 69 6e 20 71 75 65 72 79 20 2a 2f 0a 20  es in query */. 
471b7 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e   sqlite3_int64 n
471b8 44 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Doc;            
471b9 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 6f   /* Number of do
471ba 63 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 2a  cs in database *
471bb 2f 0a 20 20 75 33 32 20 2a 61 4d 61 74 63 68 69  /.  u32 *aMatchi
471bc 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  nfo;            
471bd 20 20 20 20 2f 2a 20 50 72 65 2d 61 6c 6c 6f 63      /* Pre-alloc
471be 61 74 65 64 20 62 75 66 66 65 72 20 2a 2f 0a 7d  ated buffer */.}
471bf 3b 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;..../*.** The s
471c0 6e 69 70 70 65 74 28 29 20 61 6e 64 20 6f 66 66  nippet() and off
471c1 73 65 74 73 28 29 20 66 75 6e 63 74 69 6f 6e 73  sets() functions
471c2 20 62 6f 74 68 20 72 65 74 75 72 6e 20 74 65 78   both return tex
471c3 74 20 76 61 6c 75 65 73 2e 20 41 6e 20 69 6e 73  t values. An ins
471c4 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20  tance.** of the 
471c5 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
471c6 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 61  ure is used to a
471c7 63 63 75 6d 75 6c 61 74 65 20 74 68 6f 73 65 20  ccumulate those 
471c8 76 61 6c 75 65 73 20 77 68 69 6c 65 20 74 68 65  values while the
471c9 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  .** functions ar
471ca 65 20 72 75 6e 6e 69 6e 67 2e 20 53 65 65 20 66  e running. See f
471cb 74 73 33 53 74 72 69 6e 67 41 70 70 65 6e 64 28  ts3StringAppend(
471cc 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a  ) for details..*
471cd 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
471ce 20 53 74 72 42 75 66 66 65 72 20 53 74 72 42 75   StrBuffer StrBu
471cf 66 66 65 72 3b 0a 73 74 72 75 63 74 20 53 74 72  ffer;.struct Str
471d0 42 75 66 66 65 72 20 7b 0a 20 20 63 68 61 72 20  Buffer {.  char 
471d1 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *z;             
471d2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
471d3 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72 20  inter to buffer 
471d4 63 6f 6e 74 61 69 6e 69 6e 67 20 73 74 72 69 6e  containing strin
471d5 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20  g */.  int n;   
471d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
471d7 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
471d8 20 6f 66 20 7a 20 69 6e 20 62 79 74 65 73 20 28   of z in bytes (
471d9 65 78 63 6c 2e 20 6e 75 6c 2d 74 65 72 6d 29 20  excl. nul-term) 
471da 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b  */.  int nAlloc;
471db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
471dc 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65       /* Allocate
471dd 64 20 73 69 7a 65 20 6f 66 20 62 75 66 66 65 72  d size of buffer
471de 20 7a 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 7d   z in bytes */.}
471df 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  ;.../*.** This f
471e0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
471e1 74 6f 20 68 65 6c 70 20 69 74 65 72 61 74 65 20  to help iterate 
471e2 74 68 72 6f 75 67 68 20 61 20 70 6f 73 69 74 69  through a positi
471e3 6f 6e 2d 6c 69 73 74 2e 20 41 20 70 6f 73 69 74  on-list. A posit
471e4 69 6f 6e 0a 2a 2a 20 6c 69 73 74 20 69 73 20 61  ion.** list is a
471e5 20 6c 69 73 74 20 6f 66 20 75 6e 69 71 75 65 20   list of unique 
471e6 69 6e 74 65 67 65 72 73 2c 20 73 6f 72 74 65 64  integers, sorted
471e7 20 66 72 6f 6d 20 73 6d 61 6c 6c 65 73 74 20 74   from smallest t
471e8 6f 20 6c 61 72 67 65 73 74 2e 20 45 61 63 68 0a  o largest. Each.
471e9 2a 2a 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  ** element of th
471ea 65 20 6c 69 73 74 20 69 73 20 72 65 70 72 65 73  e list is repres
471eb 65 6e 74 65 64 20 62 79 20 61 6e 20 46 54 53 33  ented by an FTS3
471ec 20 76 61 72 69 6e 74 20 74 68 61 74 20 74 61 6b   varint that tak
471ed 65 73 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  es the value.** 
471ee 6f 66 20 74 68 65 20 64 69 66 66 65 72 65 6e 63  of the differenc
471ef 65 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 75  e between the cu
471f0 72 72 65 6e 74 20 65 6c 65 6d 65 6e 74 20 61 6e  rrent element an
471f1 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6f  d the previous o
471f2 6e 65 20 70 6c 75 73 0a 2a 2a 20 74 77 6f 2e 20  ne plus.** two. 
471f3 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20  For example, to 
471f4 73 74 6f 72 65 20 74 68 65 20 70 6f 73 69 74 69  store the positi
471f5 6f 6e 2d 6c 69 73 74 3a 0a 2a 2a 0a 2a 2a 20 20  on-list:.**.**  
471f6 20 20 20 34 20 39 20 31 31 33 0a 2a 2a 0a 2a 2a     4 9 113.**.**
471f7 20 74 68 65 20 74 68 72 65 65 20 76 61 72 69 6e   the three varin
471f8 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 36 20  ts:.**.**     6 
471f9 37 20 31 30 36 0a 2a 2a 0a 2a 2a 20 61 72 65 20  7 106.**.** are 
471fa 65 6e 63 6f 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 57  encoded..**.** W
471fb 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
471fc 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 2a 70 70  n is called, *pp
471fd 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73   points to the s
471fe 74 61 72 74 20 6f 66 20 61 6e 20 65 6c 65 6d 65  tart of an eleme
471ff 6e 74 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 69 73  nt of.** the lis
47200 74 2e 20 2a 70 69 50 6f 73 20 63 6f 6e 74 61 69  t. *piPos contai
47201 6e 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ns the value of 
47202 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74  the previous ent
47203 72 79 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a  ry in the list..
47204 2a 2a 20 41 66 74 65 72 20 69 74 20 72 65 74 75  ** After it retu
47205 72 6e 73 2c 20 2a 70 69 50 6f 73 20 63 6f 6e 74  rns, *piPos cont
47206 61 69 6e 73 20 74 68 65 20 76 61 6c 75 65 20 6f  ains the value o
47207 66 20 74 68 65 20 6e 65 78 74 20 65 6c 65 6d 65  f the next eleme
47208 6e 74 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 73  nt of the.** lis
47209 74 20 61 6e 64 20 2a 70 70 20 69 73 20 61 64 76  t and *pp is adv
4720a 61 6e 63 65 64 20 74 6f 20 74 68 65 20 66 6f 6c  anced to the fol
4720b 6c 6f 77 69 6e 67 20 76 61 72 69 6e 74 2e 0a 2a  lowing varint..*
4720c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  /.static void ft
4720d 73 33 47 65 74 44 65 6c 74 61 50 6f 73 69 74 69  s3GetDeltaPositi
4720e 6f 6e 28 63 68 61 72 20 2a 2a 70 70 2c 20 69 6e  on(char **pp, in
4720f 74 20 2a 70 69 50 6f 73 29 7b 0a 20 20 69 6e 74  t *piPos){.  int
47210 20 69 56 61 6c 3b 0a 20 20 2a 70 70 20 2b 3d 20   iVal;.  *pp += 
47211 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61  sqlite3Fts3GetVa
47212 72 69 6e 74 33 32 28 2a 70 70 2c 20 26 69 56 61  rint32(*pp, &iVa
47213 6c 29 3b 0a 20 20 2a 70 69 50 6f 73 20 2b 3d 20  l);.  *piPos += 
47214 28 69 56 61 6c 2d 32 29 3b 0a 7d 0a 0a 2f 2a 0a  (iVal-2);.}../*.
47215 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69  ** Helper functi
47216 6f 6e 20 66 6f 72 20 66 74 73 33 45 78 70 72 49  on for fts3ExprI
47217 74 65 72 61 74 65 28 29 20 28 73 65 65 20 62 65  terate() (see be
47218 6c 6f 77 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  low)..*/.static 
47219 69 6e 74 20 66 74 73 33 45 78 70 72 49 74 65 72  int fts3ExprIter
4721a 61 74 65 32 28 0a 20 20 46 74 73 33 45 78 70 72  ate2(.  Fts3Expr
4721b 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20   *pExpr,        
4721c 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65          /* Expre
4721d 73 73 69 6f 6e 20 74 6f 20 69 74 65 72 61 74 65  ssion to iterate
4721e 20 70 68 72 61 73 65 73 20 6f 66 20 2a 2f 0a 20   phrases of */. 
4721f 20 69 6e 74 20 2a 70 69 50 68 72 61 73 65 2c 20   int *piPhrase, 
47220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47221 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70   /* Pointer to p
47222 68 72 61 73 65 20 63 6f 75 6e 74 65 72 20 2a 2f  hrase counter */
47223 0a 20 20 69 6e 74 20 28 2a 78 29 28 46 74 73 33  .  int (*x)(Fts3
47224 45 78 70 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29  Expr*,int,void*)
47225 2c 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66  ,  /* Callback f
47226 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b  unction to invok
47227 65 20 66 6f 72 20 70 68 72 61 73 65 73 20 2a 2f  e for phrases */
47228 0a 20 20 76 6f 69 64 20 2a 70 43 74 78 20 20 20  .  void *pCtx   
47229 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4722a 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 61 72 67     /* Second arg
4722b 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f  ument to pass to
4722c 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 29 7b 0a   callback */.){.
4722d 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
4722e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4722f 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
47230 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65 20   */.  int eType 
47231 3d 20 70 45 78 70 72 2d 3e 65 54 79 70 65 3b 20  = pExpr->eType; 
47232 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66        /* Type of
47233 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65   expression node
47234 20 70 45 78 70 72 20 2a 2f 0a 0a 20 20 69 66 28   pExpr */..  if(
47235 20 65 54 79 70 65 21 3d 46 54 53 51 55 45 52 59   eType!=FTSQUERY
47236 5f 50 48 52 41 53 45 20 29 7b 0a 20 20 20 20 61  _PHRASE ){.    a
47237 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c  ssert( pExpr->pL
47238 65 66 74 20 26 26 20 70 45 78 70 72 2d 3e 70 52  eft && pExpr->pR
47239 69 67 68 74 20 29 3b 0a 20 20 20 20 72 63 20 3d  ight );.    rc =
4723a 20 66 74 73 33 45 78 70 72 49 74 65 72 61 74 65   fts3ExprIterate
4723b 32 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  2(pExpr->pLeft, 
4723c 70 69 50 68 72 61 73 65 2c 20 78 2c 20 70 43 74  piPhrase, x, pCt
4723d 78 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  x);.    if( rc==
4723e 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 54 79  SQLITE_OK && eTy
4723f 70 65 21 3d 46 54 53 51 55 45 52 59 5f 4e 4f 54  pe!=FTSQUERY_NOT
47240 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66   ){.      rc = f
47241 74 73 33 45 78 70 72 49 74 65 72 61 74 65 32 28  ts3ExprIterate2(
47242 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 70  pExpr->pRight, p
47243 69 50 68 72 61 73 65 2c 20 78 2c 20 70 43 74 78  iPhrase, x, pCtx
47244 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
47245 7b 0a 20 20 20 20 72 63 20 3d 20 78 28 70 45 78  {.    rc = x(pEx
47246 70 72 2c 20 2a 70 69 50 68 72 61 73 65 2c 20 70  pr, *piPhrase, p
47247 43 74 78 29 3b 0a 20 20 20 20 28 2a 70 69 50 68  Ctx);.    (*piPh
47248 72 61 73 65 29 2b 2b 3b 0a 20 20 7d 0a 20 20 72  rase)++;.  }.  r
47249 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
4724a 2a 2a 20 49 74 65 72 61 74 65 20 74 68 72 6f 75  ** Iterate throu
4724b 67 68 20 61 6c 6c 20 70 68 72 61 73 65 20 6e 6f  gh all phrase no
4724c 64 65 73 20 69 6e 20 61 6e 20 46 54 53 33 20 71  des in an FTS3 q
4724d 75 65 72 79 2c 20 65 78 63 65 70 74 20 74 68 6f  uery, except tho
4724e 73 65 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 70  se that.** are p
4724f 61 72 74 20 6f 66 20 61 20 73 75 62 2d 74 72 65  art of a sub-tre
47250 65 20 74 68 61 74 20 69 73 20 74 68 65 20 72 69  e that is the ri
47251 67 68 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f 66  ght-hand-side of
47252 20 61 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 2e   a NOT operator.
47253 0a 2a 2a 20 46 6f 72 20 65 61 63 68 20 70 68 72  .** For each phr
47254 61 73 65 20 6e 6f 64 65 20 66 6f 75 6e 64 2c 20  ase node found, 
47255 74 68 65 20 73 75 70 70 6c 69 65 64 20 63 61 6c  the supplied cal
47256 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69  lback function i
47257 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a  s invoked..**.**
47258 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   If the callback
47259 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
4725a 73 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 72  s anything other
4725b 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 2c   than SQLITE_OK,
4725c 20 0a 2a 2a 20 74 68 65 20 69 74 65 72 61 74 69   .** the iterati
4725d 6f 6e 20 69 73 20 61 62 61 6e 64 6f 6e 65 64 20  on is abandoned 
4725e 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f  and the error co
4725f 64 65 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65  de returned imme
47260 64 69 61 74 65 6c 79 2e 0a 2a 2a 20 4f 74 68 65  diately..** Othe
47261 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b  rwise, SQLITE_OK
47262 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 66 74   is returned aft
47263 65 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 68 61  er a callback ha
47264 73 20 62 65 65 6e 20 6d 61 64 65 20 66 6f 72 0a  s been made for.
47265 2a 2a 20 61 6c 6c 20 65 6c 69 67 69 62 6c 65 20  ** all eligible 
47266 70 68 72 61 73 65 20 6e 6f 64 65 73 2e 0a 2a 2f  phrase nodes..*/
47267 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
47268 45 78 70 72 49 74 65 72 61 74 65 28 0a 20 20 46  ExprIterate(.  F
47269 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 2c 20  ts3Expr *pExpr, 
4726a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4726b 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  * Expression to 
4726c 69 74 65 72 61 74 65 20 70 68 72 61 73 65 73 20  iterate phrases 
4726d 6f 66 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 29  of */.  int (*x)
4726e 28 46 74 73 33 45 78 70 72 2a 2c 69 6e 74 2c 76  (Fts3Expr*,int,v
4726f 6f 69 64 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c 62  oid*),  /* Callb
47270 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  ack function to 
47271 69 6e 76 6f 6b 65 20 66 6f 72 20 70 68 72 61 73  invoke for phras
47272 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43  es */.  void *pC
47273 74 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20  tx              
47274 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e          /* Secon
47275 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 70 61  d argument to pa
47276 73 73 20 74 6f 20 63 61 6c 6c 62 61 63 6b 20 2a  ss to callback *
47277 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 50 68 72 61  /.){.  int iPhra
47278 73 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  se = 0;         
47279 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62         /* Variab
4727a 6c 65 20 75 73 65 64 20 61 73 20 74 68 65 20 70  le used as the p
4727b 68 72 61 73 65 20 63 6f 75 6e 74 65 72 20 2a 2f  hrase counter */
4727c 0a 20 20 72 65 74 75 72 6e 20 66 74 73 33 45 78  .  return fts3Ex
4727d 70 72 49 74 65 72 61 74 65 32 28 70 45 78 70 72  prIterate2(pExpr
4727e 2c 20 26 69 50 68 72 61 73 65 2c 20 78 2c 20 70  , &iPhrase, x, p
4727f 43 74 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  Ctx);.}../*.** T
47280 68 69 73 20 69 73 20 61 6e 20 66 74 73 33 45 78  his is an fts3Ex
47281 70 72 49 74 65 72 61 74 65 28 29 20 63 61 6c 6c  prIterate() call
47282 62 61 63 6b 20 75 73 65 64 20 77 68 69 6c 65 20  back used while 
47283 6c 6f 61 64 69 6e 67 20 74 68 65 20 64 6f 63 6c  loading the docl
47284 69 73 74 73 0a 2a 2a 20 66 6f 72 20 65 61 63 68  ists.** for each
47285 20 70 68 72 61 73 65 20 69 6e 74 6f 20 46 74 73   phrase into Fts
47286 33 45 78 70 72 2e 61 44 6f 63 6c 69 73 74 5b 5d  3Expr.aDoclist[]
47287 2f 6e 44 6f 63 6c 69 73 74 2e 20 53 65 65 20 61  /nDoclist. See a
47288 6c 73 6f 0a 2a 2a 20 66 74 73 33 45 78 70 72 4c  lso.** fts3ExprL
47289 6f 61 64 44 6f 63 6c 69 73 74 73 28 29 2e 0a 2a  oadDoclists()..*
4728a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
4728b 33 45 78 70 72 4c 6f 61 64 44 6f 63 6c 69 73 74  3ExprLoadDoclist
4728c 73 43 62 28 46 74 73 33 45 78 70 72 20 2a 70 45  sCb(Fts3Expr *pE
4728d 78 70 72 2c 20 69 6e 74 20 69 50 68 72 61 73 65  xpr, int iPhrase
4728e 2c 20 76 6f 69 64 20 2a 63 74 78 29 7b 0a 20 20  , void *ctx){.  
4728f 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
47290 4f 4b 3b 0a 20 20 46 74 73 33 50 68 72 61 73 65  OK;.  Fts3Phrase
47291 20 2a 70 50 68 72 61 73 65 20 3d 20 70 45 78 70   *pPhrase = pExp
47292 72 2d 3e 70 50 68 72 61 73 65 3b 0a 20 20 4c 6f  r->pPhrase;.  Lo
47293 61 64 44 6f 63 6c 69 73 74 43 74 78 20 2a 70 20  adDoclistCtx *p 
47294 3d 20 28 4c 6f 61 64 44 6f 63 6c 69 73 74 43 74  = (LoadDoclistCt
47295 78 20 2a 29 63 74 78 3b 0a 0a 20 20 55 4e 55 53  x *)ctx;..  UNUS
47296 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 50 68  ED_PARAMETER(iPh
47297 72 61 73 65 29 3b 0a 0a 20 20 70 2d 3e 6e 50 68  rase);..  p->nPh
47298 72 61 73 65 2b 2b 3b 0a 20 20 70 2d 3e 6e 54 6f  rase++;.  p->nTo
47299 6b 65 6e 20 2b 3d 20 70 50 68 72 61 73 65 2d 3e  ken += pPhrase->
4729a 6e 54 6f 6b 65 6e 3b 0a 0a 20 20 72 65 74 75 72  nToken;..  retur
4729b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  n rc;.}../*.** L
4729c 6f 61 64 20 74 68 65 20 64 6f 63 6c 69 73 74 73  oad the doclists
4729d 20 66 6f 72 20 65 61 63 68 20 70 68 72 61 73 65   for each phrase
4729e 20 69 6e 20 74 68 65 20 71 75 65 72 79 20 61 73   in the query as
4729f 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 46 54  sociated with FT
472a0 53 33 20 63 75 72 73 6f 72 0a 2a 2a 20 70 43 73  S3 cursor.** pCs
472a1 72 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 50  r. .**.** If pnP
472a2 68 72 61 73 65 20 69 73 20 6e 6f 74 20 4e 55 4c  hrase is not NUL
472a3 4c 2c 20 74 68 65 6e 20 2a 70 6e 50 68 72 61 73  L, then *pnPhras
472a4 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  e is set to the 
472a5 6e 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 61  number of matcha
472a6 62 6c 65 20 0a 2a 2a 20 70 68 72 61 73 65 73 20  ble .** phrases 
472a7 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  in the expressio
472a8 6e 20 28 61 6c 6c 20 70 68 72 61 73 65 73 20 65  n (all phrases e
472a9 78 63 65 70 74 20 74 68 6f 73 65 20 64 69 72 65  xcept those dire
472aa 63 74 6c 79 20 6f 72 20 0a 2a 2a 20 69 6e 64 69  ctly or .** indi
472ab 72 65 63 74 6c 79 20 64 65 73 63 65 6e 64 65 64  rectly descended
472ac 20 66 72 6f 6d 20 74 68 65 20 72 69 67 68 74 2d   from the right-
472ad 68 61 6e 64 2d 73 69 64 65 20 6f 66 20 61 20 4e  hand-side of a N
472ae 4f 54 20 6f 70 65 72 61 74 6f 72 29 2e 20 49 66  OT operator). If
472af 20 0a 2a 2a 20 70 6e 54 6f 6b 65 6e 20 69 73 20   .** pnToken is 
472b0 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69  not NULL, then i
472b1 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  t is set to the 
472b2 6e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73  number of tokens
472b3 20 69 6e 20 61 6c 6c 0a 2a 2a 20 6d 61 74 63 68   in all.** match
472b4 61 62 6c 65 20 70 68 72 61 73 65 73 20 6f 66 20  able phrases of 
472b5 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a  the expression..
472b6 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
472b7 73 33 45 78 70 72 4c 6f 61 64 44 6f 63 6c 69 73  s3ExprLoadDoclis
472b8 74 73 28 0a 20 20 46 74 73 33 43 75 72 73 6f 72  ts(.  Fts3Cursor
472b9 20 2a 70 43 73 72 2c 20 20 20 20 20 20 20 20 20   *pCsr,         
472ba 20 20 20 20 20 20 2f 2a 20 46 74 73 33 20 63 75        /* Fts3 cu
472bb 72 73 6f 72 20 66 6f 72 20 63 75 72 72 65 6e 74  rsor for current
472bc 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20   query */.  int 
472bd 2a 70 6e 50 68 72 61 73 65 2c 20 20 20 20 20 20  *pnPhrase,      
472be 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
472bf 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 70 68  UT: Number of ph
472c0 72 61 73 65 73 20 69 6e 20 71 75 65 72 79 20 2a  rases in query *
472c1 2f 0a 20 20 69 6e 74 20 2a 70 6e 54 6f 6b 65 6e  /.  int *pnToken
472c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472c3 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62      /* OUT: Numb
472c4 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20  er of tokens in 
472c5 71 75 65 72 79 20 2a 2f 0a 29 7b 0a 20 20 69 6e  query */.){.  in
472c6 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
472c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
472c8 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a   Return Code */.
472c9 20 20 4c 6f 61 64 44 6f 63 6c 69 73 74 43 74 78    LoadDoclistCtx
472ca 20 73 43 74 78 20 3d 20 7b 30 2c 30 2c 30 7d 3b   sCtx = {0,0,0};
472cb 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72    /* Context for
472cc 20 66 74 73 33 45 78 70 72 49 74 65 72 61 74 65   fts3ExprIterate
472cd 28 29 20 2a 2f 0a 20 20 73 43 74 78 2e 70 43 73  () */.  sCtx.pCs
472ce 72 20 3d 20 70 43 73 72 3b 0a 20 20 72 63 20 3d  r = pCsr;.  rc =
472cf 20 66 74 73 33 45 78 70 72 49 74 65 72 61 74 65   fts3ExprIterate
472d0 28 70 43 73 72 2d 3e 70 45 78 70 72 2c 20 66 74  (pCsr->pExpr, ft
472d1 73 33 45 78 70 72 4c 6f 61 64 44 6f 63 6c 69 73  s3ExprLoadDoclis
472d2 74 73 43 62 2c 20 28 76 6f 69 64 20 2a 29 26 73  tsCb, (void *)&s
472d3 43 74 78 29 3b 0a 20 20 69 66 28 20 70 6e 50 68  Ctx);.  if( pnPh
472d4 72 61 73 65 20 29 20 2a 70 6e 50 68 72 61 73 65  rase ) *pnPhrase
472d5 20 3d 20 73 43 74 78 2e 6e 50 68 72 61 73 65 3b   = sCtx.nPhrase;
472d6 0a 20 20 69 66 28 20 70 6e 54 6f 6b 65 6e 20 29  .  if( pnToken )
472d7 20 2a 70 6e 54 6f 6b 65 6e 20 3d 20 73 43 74 78   *pnToken = sCtx
472d8 2e 6e 54 6f 6b 65 6e 3b 0a 20 20 72 65 74 75 72  .nToken;.  retur
472d9 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  n rc;.}..static 
472da 69 6e 74 20 66 74 73 33 45 78 70 72 50 68 72 61  int fts3ExprPhra
472db 73 65 43 6f 75 6e 74 43 62 28 46 74 73 33 45 78  seCountCb(Fts3Ex
472dc 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 69  pr *pExpr, int i
472dd 50 68 72 61 73 65 2c 20 76 6f 69 64 20 2a 63 74  Phrase, void *ct
472de 78 29 7b 0a 20 20 28 2a 28 69 6e 74 20 2a 29 63  x){.  (*(int *)c
472df 74 78 29 2b 2b 3b 0a 20 20 55 4e 55 53 45 44 5f  tx)++;.  UNUSED_
472e0 50 41 52 41 4d 45 54 45 52 28 70 45 78 70 72 29  PARAMETER(pExpr)
472e1 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
472e2 45 54 45 52 28 69 50 68 72 61 73 65 29 3b 0a 20  ETER(iPhrase);. 
472e3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
472e4 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  K;.}.static int 
472e5 66 74 73 33 45 78 70 72 50 68 72 61 73 65 43 6f  fts3ExprPhraseCo
472e6 75 6e 74 28 46 74 73 33 45 78 70 72 20 2a 70 45  unt(Fts3Expr *pE
472e7 78 70 72 29 7b 0a 20 20 69 6e 74 20 6e 50 68 72  xpr){.  int nPhr
472e8 61 73 65 20 3d 20 30 3b 0a 20 20 28 76 6f 69 64  ase = 0;.  (void
472e9 29 66 74 73 33 45 78 70 72 49 74 65 72 61 74 65  )fts3ExprIterate
472ea 28 70 45 78 70 72 2c 20 66 74 73 33 45 78 70 72  (pExpr, fts3Expr
472eb 50 68 72 61 73 65 43 6f 75 6e 74 43 62 2c 20 28  PhraseCountCb, (
472ec 76 6f 69 64 20 2a 29 26 6e 50 68 72 61 73 65 29  void *)&nPhrase)
472ed 3b 0a 20 20 72 65 74 75 72 6e 20 6e 50 68 72 61  ;.  return nPhra
472ee 73 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 76  se;.}../*.** Adv
472ef 61 6e 63 65 20 74 68 65 20 70 6f 73 69 74 69 6f  ance the positio
472f0 6e 20 6c 69 73 74 20 69 74 65 72 61 74 6f 72 20  n list iterator 
472f1 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
472f2 20 66 69 72 73 74 20 74 77 6f 20 0a 2a 2a 20 61   first two .** a
472f3 72 67 75 6d 65 6e 74 73 20 73 6f 20 74 68 61 74  rguments so that
472f4 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68   it points to th
472f5 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20  e first element 
472f6 77 69 74 68 20 61 20 76 61 6c 75 65 20 67 72 65  with a value gre
472f7 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 6f 72 20  ater.** than or 
472f8 65 71 75 61 6c 20 74 6f 20 70 61 72 61 6d 65 74  equal to paramet
472f9 65 72 20 69 4e 65 78 74 2e 0a 2a 2f 0a 73 74 61  er iNext..*/.sta
472fa 74 69 63 20 76 6f 69 64 20 66 74 73 33 53 6e 69  tic void fts3Sni
472fb 70 70 65 74 41 64 76 61 6e 63 65 28 63 68 61 72  ppetAdvance(char
472fc 20 2a 2a 70 70 49 74 65 72 2c 20 69 6e 74 20 2a   **ppIter, int *
472fd 70 69 49 74 65 72 2c 20 69 6e 74 20 69 4e 65 78  piIter, int iNex
472fe 74 29 7b 0a 20 20 63 68 61 72 20 2a 70 49 74 65  t){.  char *pIte
472ff 72 20 3d 20 2a 70 70 49 74 65 72 3b 0a 20 20 69  r = *ppIter;.  i
47300 66 28 20 70 49 74 65 72 20 29 7b 0a 20 20 20 20  f( pIter ){.    
47301 69 6e 74 20 69 49 74 65 72 20 3d 20 2a 70 69 49  int iIter = *piI
47302 74 65 72 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28  ter;..    while(
47303 20 69 49 74 65 72 3c 69 4e 65 78 74 20 29 7b 0a   iIter<iNext ){.
47304 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 2a 70        if( 0==(*p
47305 49 74 65 72 20 26 20 30 78 46 45 29 20 29 7b 0a  Iter & 0xFE) ){.
47306 20 20 20 20 20 20 20 20 69 49 74 65 72 20 3d 20          iIter = 
47307 2d 31 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65  -1;.        pIte
47308 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62  r = 0;.        b
47309 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
4730a 20 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61      fts3GetDelta
4730b 50 6f 73 69 74 69 6f 6e 28 26 70 49 74 65 72 2c  Position(&pIter,
4730c 20 26 69 49 74 65 72 29 3b 0a 20 20 20 20 7d 0a   &iIter);.    }.
4730d 0a 20 20 20 20 2a 70 69 49 74 65 72 20 3d 20 69  .    *piIter = i
4730e 49 74 65 72 3b 0a 20 20 20 20 2a 70 70 49 74 65  Iter;.    *ppIte
4730f 72 20 3d 20 70 49 74 65 72 3b 0a 20 20 7d 0a 7d  r = pIter;.  }.}
47310 0a 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20  ../*.** Advance 
47311 74 68 65 20 73 6e 69 70 70 65 74 20 69 74 65 72  the snippet iter
47312 61 74 6f 72 20 74 6f 20 74 68 65 20 6e 65 78 74  ator to the next
47313 20 63 61 6e 64 69 64 61 74 65 20 73 6e 69 70 70   candidate snipp
47314 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  et..*/.static in
47315 74 20 66 74 73 33 53 6e 69 70 70 65 74 4e 65 78  t fts3SnippetNex
47316 74 43 61 6e 64 69 64 61 74 65 28 53 6e 69 70 70  tCandidate(Snipp
47317 65 74 49 74 65 72 20 2a 70 49 74 65 72 29 7b 0a  etIter *pIter){.
47318 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
47319 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4731a 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
4731b 72 20 2a 2f 0a 0a 20 20 69 66 28 20 70 49 74 65  r */..  if( pIte
4731c 72 2d 3e 69 43 75 72 72 65 6e 74 3c 30 20 29 7b  r->iCurrent<0 ){
4731d 0a 20 20 20 20 2f 2a 20 54 68 65 20 53 6e 69 70  .    /* The Snip
4731e 70 65 74 49 74 65 72 20 6f 62 6a 65 63 74 20 68  petIter object h
4731f 61 73 20 6a 75 73 74 20 62 65 65 6e 20 69 6e 69  as just been ini
47320 74 69 61 6c 69 7a 65 64 2e 20 54 68 65 20 66 69  tialized. The fi
47321 72 73 74 20 73 6e 69 70 70 65 74 0a 20 20 20 20  rst snippet.    
47322 2a 2a 20 63 61 6e 64 69 64 61 74 65 20 61 6c 77  ** candidate alw
47323 61 79 73 20 73 74 61 72 74 73 20 61 74 20 6f 66  ays starts at of
47324 66 73 65 74 20 30 20 28 65 76 65 6e 20 69 66 20  fset 0 (even if 
47325 74 68 69 73 20 63 61 6e 64 69 64 61 74 65 20 68  this candidate h
47326 61 73 20 61 0a 20 20 20 20 2a 2a 20 73 63 6f 72  as a.    ** scor
47327 65 20 6f 66 20 30 2e 30 29 2e 0a 20 20 20 20 2a  e of 0.0)..    *
47328 2f 0a 20 20 20 20 70 49 74 65 72 2d 3e 69 43 75  /.    pIter->iCu
47329 72 72 65 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20  rrent = 0;..    
4732a 2f 2a 20 41 64 76 61 6e 63 65 20 74 68 65 20 27  /* Advance the '
4732b 68 65 61 64 27 20 69 74 65 72 61 74 6f 72 20 6f  head' iterator o
4732c 66 20 65 61 63 68 20 70 68 72 61 73 65 20 74 6f  f each phrase to
4732d 20 74 68 65 20 66 69 72 73 74 20 6f 66 66 73 65   the first offse
4732e 74 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 69 73  t that.    ** is
4732f 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72   greater than or
47330 20 65 71 75 61 6c 20 74 6f 20 28 69 4e 65 78 74   equal to (iNext
47331 2b 6e 53 6e 69 70 70 65 74 29 2e 0a 20 20 20 20  +nSnippet)..    
47332 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  */.    for(i=0; 
47333 69 3c 70 49 74 65 72 2d 3e 6e 50 68 72 61 73 65  i<pIter->nPhrase
47334 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 53 6e  ; i++){.      Sn
47335 69 70 70 65 74 50 68 72 61 73 65 20 2a 70 50 68  ippetPhrase *pPh
47336 72 61 73 65 20 3d 20 26 70 49 74 65 72 2d 3e 61  rase = &pIter->a
47337 50 68 72 61 73 65 5b 69 5d 3b 0a 20 20 20 20 20  Phrase[i];.     
47338 20 66 74 73 33 53 6e 69 70 70 65 74 41 64 76 61   fts3SnippetAdva
47339 6e 63 65 28 26 70 50 68 72 61 73 65 2d 3e 70 48  nce(&pPhrase->pH
4733a 65 61 64 2c 20 26 70 50 68 72 61 73 65 2d 3e 69  ead, &pPhrase->i
4733b 48 65 61 64 2c 20 70 49 74 65 72 2d 3e 6e 53 6e  Head, pIter->nSn
4733c 69 70 70 65 74 29 3b 0a 20 20 20 20 7d 0a 20 20  ippet);.    }.  
4733d 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69  }else{.    int i
4733e 53 74 61 72 74 3b 0a 20 20 20 20 69 6e 74 20 69  Start;.    int i
4733f 45 6e 64 20 3d 20 30 78 37 46 46 46 46 46 46 46  End = 0x7FFFFFFF
47340 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ;..    for(i=0; 
47341 69 3c 70 49 74 65 72 2d 3e 6e 50 68 72 61 73 65  i<pIter->nPhrase
47342 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 53 6e  ; i++){.      Sn
47343 69 70 70 65 74 50 68 72 61 73 65 20 2a 70 50 68  ippetPhrase *pPh
47344 72 61 73 65 20 3d 20 26 70 49 74 65 72 2d 3e 61  rase = &pIter->a
47345 50 68 72 61 73 65 5b 69 5d 3b 0a 20 20 20 20 20  Phrase[i];.     
47346 20 69 66 28 20 70 50 68 72 61 73 65 2d 3e 70 48   if( pPhrase->pH
47347 65 61 64 20 26 26 20 70 50 68 72 61 73 65 2d 3e  ead && pPhrase->
47348 69 48 65 61 64 3c 69 45 6e 64 20 29 7b 0a 20 20  iHead<iEnd ){.  
47349 20 20 20 20 20 20 69 45 6e 64 20 3d 20 70 50 68        iEnd = pPh
4734a 72 61 73 65 2d 3e 69 48 65 61 64 3b 0a 20 20 20  rase->iHead;.   
4734b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
4734c 66 28 20 69 45 6e 64 3d 3d 30 78 37 46 46 46 46  f( iEnd==0x7FFFF
4734d 46 46 46 20 29 7b 0a 20 20 20 20 20 20 72 65 74  FFF ){.      ret
4734e 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20  urn 1;.    }..  
4734f 20 20 70 49 74 65 72 2d 3e 69 43 75 72 72 65 6e    pIter->iCurren
47350 74 20 3d 20 69 53 74 61 72 74 20 3d 20 69 45 6e  t = iStart = iEn
47351 64 20 2d 20 70 49 74 65 72 2d 3e 6e 53 6e 69 70  d - pIter->nSnip
47352 70 65 74 20 2b 20 31 3b 0a 20 20 20 20 66 6f 72  pet + 1;.    for
47353 28 69 3d 30 3b 20 69 3c 70 49 74 65 72 2d 3e 6e  (i=0; i<pIter->n
47354 50 68 72 61 73 65 3b 20 69 2b 2b 29 7b 0a 20 20  Phrase; i++){.  
47355 20 20 20 20 53 6e 69 70 70 65 74 50 68 72 61 73      SnippetPhras
47356 65 20 2a 70 50 68 72 61 73 65 20 3d 20 26 70 49  e *pPhrase = &pI
47357 74 65 72 2d 3e 61 50 68 72 61 73 65 5b 69 5d 3b  ter->aPhrase[i];
47358 0a 20 20 20 20 20 20 66 74 73 33 53 6e 69 70 70  .      fts3Snipp
47359 65 74 41 64 76 61 6e 63 65 28 26 70 50 68 72 61  etAdvance(&pPhra
4735a 73 65 2d 3e 70 48 65 61 64 2c 20 26 70 50 68 72  se->pHead, &pPhr
4735b 61 73 65 2d 3e 69 48 65 61 64 2c 20 69 45 6e 64  ase->iHead, iEnd
4735c 2b 31 29 3b 0a 20 20 20 20 20 20 66 74 73 33 53  +1);.      fts3S
4735d 6e 69 70 70 65 74 41 64 76 61 6e 63 65 28 26 70  nippetAdvance(&p
4735e 50 68 72 61 73 65 2d 3e 70 54 61 69 6c 2c 20 26  Phrase->pTail, &
4735f 70 50 68 72 61 73 65 2d 3e 69 54 61 69 6c 2c 20  pPhrase->iTail, 
47360 69 53 74 61 72 74 29 3b 0a 20 20 20 20 7d 0a 20  iStart);.    }. 
47361 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a   }..  return 0;.
47362 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 76  }../*.** Retriev
47363 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  e information ab
47364 6f 75 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  out the current 
47365 63 61 6e 64 69 64 61 74 65 20 73 6e 69 70 70 65  candidate snippe
47366 74 20 6f 66 20 73 6e 69 70 70 65 74 20 0a 2a 2a  t of snippet .**
47367 20 69 74 65 72 61 74 6f 72 20 70 49 74 65 72 2e   iterator pIter.
47368 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
47369 66 74 73 33 53 6e 69 70 70 65 74 44 65 74 61 69  fts3SnippetDetai
4736a 6c 73 28 0a 20 20 53 6e 69 70 70 65 74 49 74 65  ls(.  SnippetIte
4736b 72 20 2a 70 49 74 65 72 2c 20 20 20 20 20 20 20  r *pIter,       
4736c 20 20 20 20 20 20 2f 2a 20 53 6e 69 70 70 65 74        /* Snippet
4736d 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75   iterator */.  u
4736e 36 34 20 6d 43 6f 76 65 72 65 64 2c 20 20 20 20  64 mCovered,    
4736f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
47370 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 70 68 72  * Bitmask of phr
47371 61 73 65 73 20 61 6c 72 65 61 64 79 20 63 6f 76  ases already cov
47372 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ered */.  int *p
47373 69 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20 20  iToken,         
47374 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
47375 3a 20 46 69 72 73 74 20 74 6f 6b 65 6e 20 6f 66  : First token of
47376 20 70 72 6f 70 6f 73 65 64 20 73 6e 69 70 70 65   proposed snippe
47377 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 53 63  t */.  int *piSc
47378 6f 72 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ore,            
47379 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 22         /* OUT: "
4737a 53 63 6f 72 65 22 20 66 6f 72 20 74 68 69 73 20  Score" for this 
4737b 73 6e 69 70 70 65 74 20 2a 2f 0a 20 20 75 36 34  snippet */.  u64
4737c 20 2a 70 6d 43 6f 76 65 72 2c 20 20 20 20 20 20   *pmCover,      
4737d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4737e 4f 55 54 3a 20 42 69 74 6d 61 73 6b 20 6f 66 20  OUT: Bitmask of 
4737f 70 68 72 61 73 65 73 20 63 6f 76 65 72 65 64 20  phrases covered 
47380 2a 2f 0a 20 20 75 36 34 20 2a 70 6d 48 69 67 68  */.  u64 *pmHigh
47381 6c 69 67 68 74 20 20 20 20 20 20 20 20 20 20 20  light           
47382 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 42 69 74       /* OUT: Bit
47383 6d 61 73 6b 20 6f 66 20 74 65 72 6d 73 20 74 6f  mask of terms to
47384 20 68 69 67 68 6c 69 67 68 74 20 2a 2f 0a 29 7b   highlight */.){
47385 0a 20 20 69 6e 74 20 69 53 74 61 72 74 20 3d 20  .  int iStart = 
47386 70 49 74 65 72 2d 3e 69 43 75 72 72 65 6e 74 3b  pIter->iCurrent;
47387 20 20 20 2f 2a 20 46 69 72 73 74 20 74 6f 6b 65     /* First toke
47388 6e 20 6f 66 20 73 6e 69 70 70 65 74 20 2a 2f 0a  n of snippet */.
47389 20 20 69 6e 74 20 69 53 63 6f 72 65 20 3d 20 30    int iScore = 0
4738a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4738b 20 20 2f 2a 20 53 63 6f 72 65 20 6f 66 20 74 68    /* Score of th
4738c 69 73 20 73 6e 69 70 70 65 74 20 2a 2f 0a 20 20  is snippet */.  
4738d 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
4738e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4738f 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
47390 2a 2f 0a 20 20 75 36 34 20 6d 43 6f 76 65 72 20  */.  u64 mCover 
47391 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
47392 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
47393 70 68 72 61 73 65 73 20 63 6f 76 65 72 65 64 20  phrases covered 
47394 62 79 20 74 68 69 73 20 73 6e 69 70 70 65 74 20  by this snippet 
47395 2a 2f 0a 20 20 75 36 34 20 6d 48 69 67 68 6c 69  */.  u64 mHighli
47396 67 68 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ght = 0;        
47397 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
47398 74 6f 6b 65 6e 73 20 74 6f 20 68 69 67 68 6c 69  tokens to highli
47399 67 68 74 20 69 6e 20 73 6e 69 70 70 65 74 20 2a  ght in snippet *
4739a 2f 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  /..  for(i=0; i<
4739b 70 49 74 65 72 2d 3e 6e 50 68 72 61 73 65 3b 20  pIter->nPhrase; 
4739c 69 2b 2b 29 7b 0a 20 20 20 20 53 6e 69 70 70 65  i++){.    Snippe
4739d 74 50 68 72 61 73 65 20 2a 70 50 68 72 61 73 65  tPhrase *pPhrase
4739e 20 3d 20 26 70 49 74 65 72 2d 3e 61 50 68 72 61   = &pIter->aPhra
4739f 73 65 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70  se[i];.    if( p
473a0 50 68 72 61 73 65 2d 3e 70 54 61 69 6c 20 29 7b  Phrase->pTail ){
473a1 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 43 73  .      char *pCs
473a2 72 20 3d 20 70 50 68 72 61 73 65 2d 3e 70 54 61  r = pPhrase->pTa
473a3 69 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43  il;.      int iC
473a4 73 72 20 3d 20 70 50 68 72 61 73 65 2d 3e 69 54  sr = pPhrase->iT
473a5 61 69 6c 3b 0a 0a 20 20 20 20 20 20 77 68 69 6c  ail;..      whil
473a6 65 28 20 69 43 73 72 3c 28 69 53 74 61 72 74 2b  e( iCsr<(iStart+
473a7 70 49 74 65 72 2d 3e 6e 53 6e 69 70 70 65 74 29  pIter->nSnippet)
473a8 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
473a9 6a 3b 0a 20 20 20 20 20 20 20 20 75 36 34 20 6d  j;.        u64 m
473aa 50 68 72 61 73 65 20 3d 20 28 75 36 34 29 31 20  Phrase = (u64)1 
473ab 3c 3c 20 69 3b 0a 20 20 20 20 20 20 20 20 75 36  << i;.        u6
473ac 34 20 6d 50 6f 73 20 3d 20 28 75 36 34 29 31 20  4 mPos = (u64)1 
473ad 3c 3c 20 28 69 43 73 72 20 2d 20 69 53 74 61 72  << (iCsr - iStar
473ae 74 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  t);.        asse
473af 72 74 28 20 69 43 73 72 3e 3d 69 53 74 61 72 74  rt( iCsr>=iStart
473b0 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
473b1 28 6d 43 6f 76 65 72 7c 6d 43 6f 76 65 72 65 64  (mCover|mCovered
473b2 29 26 6d 50 68 72 61 73 65 20 29 7b 0a 20 20 20  )&mPhrase ){.   
473b3 20 20 20 20 20 20 20 69 53 63 6f 72 65 2b 2b 3b         iScore++;
473b4 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
473b5 20 20 20 20 20 20 20 20 20 20 69 53 63 6f 72 65            iScore
473b6 20 2b 3d 20 31 30 30 30 3b 0a 20 20 20 20 20 20   += 1000;.      
473b7 20 20 7d 0a 20 20 20 20 20 20 20 20 6d 43 6f 76    }.        mCov
473b8 65 72 20 7c 3d 20 6d 50 68 72 61 73 65 3b 0a 0a  er |= mPhrase;..
473b9 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
473ba 20 6a 3c 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b   j<pPhrase->nTok
473bb 65 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  en; j++){.      
473bc 20 20 20 20 6d 48 69 67 68 6c 69 67 68 74 20 7c      mHighlight |
473bd 3d 20 28 6d 50 6f 73 3e 3e 6a 29 3b 0a 20 20 20  = (mPos>>j);.   
473be 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
473bf 69 66 28 20 30 3d 3d 28 2a 70 43 73 72 20 26 20  if( 0==(*pCsr & 
473c0 30 78 30 46 45 29 20 29 20 62 72 65 61 6b 3b 0a  0x0FE) ) break;.
473c1 20 20 20 20 20 20 20 20 66 74 73 33 47 65 74 44          fts3GetD
473c2 65 6c 74 61 50 6f 73 69 74 69 6f 6e 28 26 70 43  eltaPosition(&pC
473c3 73 72 2c 20 26 69 43 73 72 29 3b 0a 20 20 20 20  sr, &iCsr);.    
473c4 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
473c5 20 2f 2a 20 53 65 74 20 74 68 65 20 6f 75 74 70   /* Set the outp
473c6 75 74 20 76 61 72 69 61 62 6c 65 73 20 62 65 66  ut variables bef
473c7 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 20 2a  ore returning. *
473c8 2f 0a 20 20 2a 70 69 54 6f 6b 65 6e 20 3d 20 69  /.  *piToken = i
473c9 53 74 61 72 74 3b 0a 20 20 2a 70 69 53 63 6f 72  Start;.  *piScor
473ca 65 20 3d 20 69 53 63 6f 72 65 3b 0a 20 20 2a 70  e = iScore;.  *p
473cb 6d 43 6f 76 65 72 20 3d 20 6d 43 6f 76 65 72 3b  mCover = mCover;
473cc 0a 20 20 2a 70 6d 48 69 67 68 6c 69 67 68 74 20  .  *pmHighlight 
473cd 3d 20 6d 48 69 67 68 6c 69 67 68 74 3b 0a 7d 0a  = mHighlight;.}.
473ce 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
473cf 74 69 6f 6e 20 69 73 20 61 6e 20 66 74 73 33 45  tion is an fts3E
473d0 78 70 72 49 74 65 72 61 74 65 28 29 20 63 61 6c  xprIterate() cal
473d1 6c 62 61 63 6b 20 75 73 65 64 20 62 79 20 66 74  lback used by ft
473d2 73 33 42 65 73 74 53 6e 69 70 70 65 74 28 29 2e  s3BestSnippet().
473d3 0a 2a 2a 20 45 61 63 68 20 69 6e 76 6f 63 61 74  .** Each invocat
473d4 69 6f 6e 20 70 6f 70 75 6c 61 74 65 73 20 61 6e  ion populates an
473d5 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
473d6 53 6e 69 70 70 65 74 49 74 65 72 2e 61 50 68 72  SnippetIter.aPhr
473d7 61 73 65 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a  ase[] array..*/.
473d8 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53  static int fts3S
473d9 6e 69 70 70 65 74 46 69 6e 64 50 6f 73 69 74 69  nippetFindPositi
473da 6f 6e 73 28 46 74 73 33 45 78 70 72 20 2a 70 45  ons(Fts3Expr *pE
473db 78 70 72 2c 20 69 6e 74 20 69 50 68 72 61 73 65  xpr, int iPhrase
473dc 2c 20 76 6f 69 64 20 2a 63 74 78 29 7b 0a 20 20  , void *ctx){.  
473dd 53 6e 69 70 70 65 74 49 74 65 72 20 2a 70 20 3d  SnippetIter *p =
473de 20 28 53 6e 69 70 70 65 74 49 74 65 72 20 2a 29   (SnippetIter *)
473df 63 74 78 3b 0a 20 20 53 6e 69 70 70 65 74 50 68  ctx;.  SnippetPh
473e0 72 61 73 65 20 2a 70 50 68 72 61 73 65 20 3d 20  rase *pPhrase = 
473e1 26 70 2d 3e 61 50 68 72 61 73 65 5b 69 50 68 72  &p->aPhrase[iPhr
473e2 61 73 65 5d 3b 0a 20 20 63 68 61 72 20 2a 70 43  ase];.  char *pC
473e3 73 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  sr;.  int rc;.. 
473e4 20 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e   pPhrase->nToken
473e5 20 3d 20 70 45 78 70 72 2d 3e 70 50 68 72 61 73   = pExpr->pPhras
473e6 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 72 63 20  e->nToken;.  rc 
473e7 3d 20 73 71 6c 69 74 65 33 46 74 73 33 45 76 61  = sqlite3Fts3Eva
473e8 6c 50 68 72 61 73 65 50 6f 73 6c 69 73 74 28 70  lPhrasePoslist(p
473e9 2d 3e 70 43 73 72 2c 20 70 45 78 70 72 2c 20 70  ->pCsr, pExpr, p
473ea 2d 3e 69 43 6f 6c 2c 20 26 70 43 73 72 29 3b 0a  ->iCol, &pCsr);.
473eb 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
473ec 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 43 73 72 3d  LITE_OK || pCsr=
473ed 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 43 73 72  =0 );.  if( pCsr
473ee 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 46 69 72   ){.    int iFir
473ef 73 74 20 3d 20 30 3b 0a 20 20 20 20 70 50 68 72  st = 0;.    pPhr
473f0 61 73 65 2d 3e 70 4c 69 73 74 20 3d 20 70 43 73  ase->pList = pCs
473f1 72 3b 0a 20 20 20 20 66 74 73 33 47 65 74 44 65  r;.    fts3GetDe
473f2 6c 74 61 50 6f 73 69 74 69 6f 6e 28 26 70 43 73  ltaPosition(&pCs
473f3 72 2c 20 26 69 46 69 72 73 74 29 3b 0a 20 20 20  r, &iFirst);.   
473f4 20 61 73 73 65 72 74 28 20 69 46 69 72 73 74 3e   assert( iFirst>
473f5 3d 30 20 29 3b 0a 20 20 20 20 70 50 68 72 61 73  =0 );.    pPhras
473f6 65 2d 3e 70 48 65 61 64 20 3d 20 70 43 73 72 3b  e->pHead = pCsr;
473f7 0a 20 20 20 20 70 50 68 72 61 73 65 2d 3e 70 54  .    pPhrase->pT
473f8 61 69 6c 20 3d 20 70 43 73 72 3b 0a 20 20 20 20  ail = pCsr;.    
473f9 70 50 68 72 61 73 65 2d 3e 69 48 65 61 64 20 3d  pPhrase->iHead =
473fa 20 69 46 69 72 73 74 3b 0a 20 20 20 20 70 50 68   iFirst;.    pPh
473fb 72 61 73 65 2d 3e 69 54 61 69 6c 20 3d 20 69 46  rase->iTail = iF
473fc 69 72 73 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  irst;.  }else{. 
473fd 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
473fe 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 0a 20 20  QLITE_OK || (.  
473ff 20 20 20 20 20 70 50 68 72 61 73 65 2d 3e 70 4c       pPhrase->pL
47400 69 73 74 3d 3d 30 20 26 26 20 70 50 68 72 61 73  ist==0 && pPhras
47401 65 2d 3e 70 48 65 61 64 3d 3d 30 20 26 26 20 70  e->pHead==0 && p
47402 50 68 72 61 73 65 2d 3e 70 54 61 69 6c 3d 3d 30  Phrase->pTail==0
47403 20 0a 20 20 20 20 29 29 3b 0a 20 20 7d 0a 0a 20   .    ));.  }.. 
47404 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
47405 2a 0a 2a 2a 20 53 65 6c 65 63 74 20 74 68 65 20  *.** Select the 
47406 66 72 61 67 6d 65 6e 74 20 6f 66 20 74 65 78 74  fragment of text
47407 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20 6e   consisting of n
47408 46 72 61 67 6d 65 6e 74 20 63 6f 6e 74 69 67 75  Fragment contigu
47409 6f 75 73 20 74 6f 6b 65 6e 73 20 0a 2a 2a 20 66  ous tokens .** f
4740a 72 6f 6d 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20  rom column iCol 
4740b 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 20 74  that represent t
4740c 68 65 20 22 62 65 73 74 22 20 73 6e 69 70 70 65  he "best" snippe
4740d 74 2e 20 54 68 65 20 62 65 73 74 20 73 6e 69 70  t. The best snip
4740e 70 65 74 0a 2a 2a 20 69 73 20 74 68 65 20 73 6e  pet.** is the sn
4740f 69 70 70 65 74 20 77 69 74 68 20 74 68 65 20 68  ippet with the h
47410 69 67 68 65 73 74 20 73 63 6f 72 65 2c 20 77 68  ighest score, wh
47411 65 72 65 20 73 63 6f 72 65 73 20 61 72 65 20 63  ere scores are c
47412 61 6c 63 75 6c 61 74 65 64 0a 2a 2a 20 62 79 20  alculated.** by 
47413 61 64 64 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  adding:.**.**   
47414 28 61 29 20 2b 31 20 70 6f 69 6e 74 20 66 6f 72  (a) +1 point for
47415 20 65 61 63 68 20 6f 63 63 75 72 65 6e 63 65 20   each occurence 
47416 6f 66 20 61 20 6d 61 74 63 68 61 62 6c 65 20 70  of a matchable p
47417 68 72 61 73 65 20 69 6e 20 74 68 65 20 73 6e 69  hrase in the sni
47418 70 70 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 62  ppet..**.**   (b
47419 29 20 2b 31 30 30 30 20 70 6f 69 6e 74 73 20 66  ) +1000 points f
4741a 6f 72 20 74 68 65 20 66 69 72 73 74 20 6f 63 63  or the first occ
4741b 75 72 65 6e 63 65 20 6f 66 20 65 61 63 68 20 6d  urence of each m
4741c 61 74 63 68 61 62 6c 65 20 70 68 72 61 73 65 20  atchable phrase 
4741d 69 6e 20 0a 2a 2a 20 20 20 20 20 20 20 74 68 65  in .**       the
4741e 20 73 6e 69 70 70 65 74 20 66 6f 72 20 77 68 69   snippet for whi
4741f 63 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  ch the correspon
47420 64 69 6e 67 20 6d 43 6f 76 65 72 65 64 20 62 69  ding mCovered bi
47421 74 20 69 73 20 6e 6f 74 20 73 65 74 2e 0a 2a 2a  t is not set..**
47422 0a 2a 2a 20 54 68 65 20 73 65 6c 65 63 74 65 64  .** The selected
47423 20 73 6e 69 70 70 65 74 20 70 61 72 61 6d 65 74   snippet paramet
47424 65 72 73 20 61 72 65 20 73 74 6f 72 65 64 20 69  ers are stored i
47425 6e 20 73 74 72 75 63 74 75 72 65 20 2a 70 46 72  n structure *pFr
47426 61 67 6d 65 6e 74 20 62 65 66 6f 72 65 0a 2a 2a  agment before.**
47427 20 72 65 74 75 72 6e 69 6e 67 2e 20 54 68 65 20   returning. The 
47428 73 63 6f 72 65 20 6f 66 20 74 68 65 20 73 65 6c  score of the sel
47429 65 63 74 65 64 20 73 6e 69 70 70 65 74 20 69 73  ected snippet is
4742a 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 69 53 63   stored in *piSc
4742b 6f 72 65 0a 2a 2a 20 62 65 66 6f 72 65 20 72 65  ore.** before re
4742c 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  turning..*/.stat
4742d 69 63 20 69 6e 74 20 66 74 73 33 42 65 73 74 53  ic int fts3BestS
4742e 6e 69 70 70 65 74 28 0a 20 20 69 6e 74 20 6e 53  nippet(.  int nS
4742f 6e 69 70 70 65 74 2c 20 20 20 20 20 20 20 20 20  nippet,         
47430 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73            /* Des
47431 69 72 65 64 20 73 6e 69 70 70 65 74 20 6c 65 6e  ired snippet len
47432 67 74 68 20 2a 2f 0a 20 20 46 74 73 33 43 75 72  gth */.  Fts3Cur
47433 73 6f 72 20 2a 70 43 73 72 2c 20 20 20 20 20 20  sor *pCsr,      
47434 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
47435 6f 72 20 74 6f 20 63 72 65 61 74 65 20 73 6e 69  or to create sni
47436 70 70 65 74 20 66 6f 72 20 2a 2f 0a 20 20 69 6e  ppet for */.  in
47437 74 20 69 43 6f 6c 2c 20 20 20 20 20 20 20 20 20  t iCol,         
47438 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
47439 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e   Index of column
4743a 20 74 6f 20 63 72 65 61 74 65 20 73 6e 69 70 70   to create snipp
4743b 65 74 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 36 34  et from */.  u64
4743c 20 6d 43 6f 76 65 72 65 64 2c 20 20 20 20 20 20   mCovered,      
4743d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4743e 4d 61 73 6b 20 6f 66 20 70 68 72 61 73 65 73 20  Mask of phrases 
4743f 61 6c 72 65 61 64 79 20 63 6f 76 65 72 65 64 20  already covered 
47440 2a 2f 0a 20 20 75 36 34 20 2a 70 6d 53 65 65 6e  */.  u64 *pmSeen
47441 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
47442 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
47443 4d 61 73 6b 20 6f 66 20 70 68 72 61 73 65 73 20  Mask of phrases 
47444 73 65 65 6e 20 2a 2f 0a 20 20 53 6e 69 70 70 65  seen */.  Snippe
47445 74 46 72 61 67 6d 65 6e 74 20 2a 70 46 72 61 67  tFragment *pFrag
47446 6d 65 6e 74 2c 20 20 20 20 20 2f 2a 20 4f 55 54  ment,     /* OUT
47447 3a 20 42 65 73 74 20 73 6e 69 70 70 65 74 20 66  : Best snippet f
47448 6f 75 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ound */.  int *p
47449 69 53 63 6f 72 65 20 20 20 20 20 20 20 20 20 20  iScore          
4744a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
4744b 3a 20 53 63 6f 72 65 20 6f 66 20 73 6e 69 70 70  : Score of snipp
4744c 65 74 20 70 46 72 61 67 6d 65 6e 74 20 2a 2f 0a  et pFragment */.
4744d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
4744e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4744f 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 43       /* Return C
47450 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 69  ode */.  int nLi
47451 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
47452 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
47453 65 72 20 6f 66 20 70 68 72 61 73 65 73 20 69 6e  er of phrases in
47454 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
47455 20 53 6e 69 70 70 65 74 49 74 65 72 20 73 49 74   SnippetIter sIt
47456 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  er;             
47457 20 2f 2a 20 49 74 65 72 61 74 65 73 20 74 68 72   /* Iterates thr
47458 6f 75 67 68 20 73 6e 69 70 70 65 74 20 63 61 6e  ough snippet can
47459 64 69 64 61 74 65 73 20 2a 2f 0a 20 20 69 6e 74  didates */.  int
4745a 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20   nByte;         
4745b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4745c 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
4745d 6f 66 20 73 70 61 63 65 20 74 6f 20 61 6c 6c 6f  of space to allo
4745e 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 69 42  cate */.  int iB
4745f 65 73 74 53 63 6f 72 65 20 3d 20 2d 31 3b 20 20  estScore = -1;  
47460 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 73            /* Bes
47461 74 20 73 6e 69 70 70 65 74 20 73 63 6f 72 65 20  t snippet score 
47462 66 6f 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f 0a  found so far */.
47463 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
47464 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47465 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
47466 72 20 2a 2f 0a 0a 20 20 6d 65 6d 73 65 74 28 26  r */..  memset(&
47467 73 49 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66  sIter, 0, sizeof
47468 28 73 49 74 65 72 29 29 3b 0a 0a 20 20 2f 2a 20  (sIter));..  /* 
47469 49 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20  Iterate through 
4746a 74 68 65 20 70 68 72 61 73 65 73 20 69 6e 20 74  the phrases in t
4746b 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  he expression to
4746c 20 63 6f 75 6e 74 20 74 68 65 6d 2e 20 54 68 65   count them. The
4746d 20 73 61 6d 65 0a 20 20 2a 2a 20 63 61 6c 6c 62   same.  ** callb
4746e 61 63 6b 20 6d 61 6b 65 73 20 73 75 72 65 20 74  ack makes sure t
4746f 68 65 20 64 6f 63 6c 69 73 74 73 20 61 72 65 20  he doclists are 
47470 6c 6f 61 64 65 64 20 66 6f 72 20 65 61 63 68 20  loaded for each 
47471 70 68 72 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 72  phrase..  */.  r
47472 63 20 3d 20 66 74 73 33 45 78 70 72 4c 6f 61 64  c = fts3ExprLoad
47473 44 6f 63 6c 69 73 74 73 28 70 43 73 72 2c 20 26  Doclists(pCsr, &
47474 6e 4c 69 73 74 2c 20 30 29 3b 0a 20 20 69 66 28  nList, 0);.  if(
47475 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
47476 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
47477 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 74  .  }..  /* Now t
47478 68 61 74 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20  hat it is known 
47479 68 6f 77 20 6d 61 6e 79 20 70 68 72 61 73 65 73  how many phrases
4747a 20 74 68 65 72 65 20 61 72 65 2c 20 61 6c 6c 6f   there are, allo
4747b 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 0a 20 20  cate and zero.  
4747c 2a 2a 20 74 68 65 20 72 65 71 75 69 72 65 64 20  ** the required 
4747d 73 70 61 63 65 20 75 73 69 6e 67 20 6d 61 6c 6c  space using mall
4747e 6f 63 28 29 2e 0a 20 20 2a 2f 0a 20 20 6e 42 79  oc()..  */.  nBy
4747f 74 65 20 3d 20 73 69 7a 65 6f 66 28 53 6e 69 70  te = sizeof(Snip
47480 70 65 74 50 68 72 61 73 65 29 20 2a 20 6e 4c 69  petPhrase) * nLi
47481 73 74 3b 0a 20 20 73 49 74 65 72 2e 61 50 68 72  st;.  sIter.aPhr
47482 61 73 65 20 3d 20 28 53 6e 69 70 70 65 74 50 68  ase = (SnippetPh
47483 72 61 73 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d  rase *)sqlite3_m
47484 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20  alloc(nByte);.  
47485 69 66 28 20 21 73 49 74 65 72 2e 61 50 68 72 61  if( !sIter.aPhra
47486 73 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  se ){.    return
47487 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
47488 20 7d 0a 20 20 6d 65 6d 73 65 74 28 73 49 74 65   }.  memset(sIte
47489 72 2e 61 50 68 72 61 73 65 2c 20 30 2c 20 6e 42  r.aPhrase, 0, nB
4748a 79 74 65 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74  yte);..  /* Init
4748b 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74 65  ialize the conte
4748c 6e 74 73 20 6f 66 20 74 68 65 20 53 6e 69 70 70  nts of the Snipp
4748d 65 74 49 74 65 72 20 6f 62 6a 65 63 74 2e 20 54  etIter object. T
4748e 68 65 6e 20 69 74 65 72 61 74 65 20 74 68 72 6f  hen iterate thro
4748f 75 67 68 0a 20 20 2a 2a 20 74 68 65 20 73 65 74  ugh.  ** the set
47490 20 6f 66 20 70 68 72 61 73 65 73 20 69 6e 20 74   of phrases in t
47491 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  he expression to
47492 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 61 50   populate the aP
47493 68 72 61 73 65 5b 5d 20 61 72 72 61 79 2e 0a 20  hrase[] array.. 
47494 20 2a 2f 0a 20 20 73 49 74 65 72 2e 70 43 73 72   */.  sIter.pCsr
47495 20 3d 20 70 43 73 72 3b 0a 20 20 73 49 74 65 72   = pCsr;.  sIter
47496 2e 69 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20  .iCol = iCol;.  
47497 73 49 74 65 72 2e 6e 53 6e 69 70 70 65 74 20 3d  sIter.nSnippet =
47498 20 6e 53 6e 69 70 70 65 74 3b 0a 20 20 73 49 74   nSnippet;.  sIt
47499 65 72 2e 6e 50 68 72 61 73 65 20 3d 20 6e 4c 69  er.nPhrase = nLi
4749a 73 74 3b 0a 20 20 73 49 74 65 72 2e 69 43 75 72  st;.  sIter.iCur
4749b 72 65 6e 74 20 3d 20 2d 31 3b 0a 20 20 28 76 6f  rent = -1;.  (vo
4749c 69 64 29 66 74 73 33 45 78 70 72 49 74 65 72 61  id)fts3ExprItera
4749d 74 65 28 70 43 73 72 2d 3e 70 45 78 70 72 2c 20  te(pCsr->pExpr, 
4749e 66 74 73 33 53 6e 69 70 70 65 74 46 69 6e 64 50  fts3SnippetFindP
4749f 6f 73 69 74 69 6f 6e 73 2c 20 28 76 6f 69 64 20  ositions, (void 
474a0 2a 29 26 73 49 74 65 72 29 3b 0a 0a 20 20 2f 2a  *)&sIter);..  /*
474a1 20 53 65 74 20 74 68 65 20 2a 70 6d 53 65 65 6e   Set the *pmSeen
474a2 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65   output variable
474a3 2e 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20  . */.  for(i=0; 
474a4 69 3c 6e 4c 69 73 74 3b 20 69 2b 2b 29 7b 0a 20  i<nList; i++){. 
474a5 20 20 20 69 66 28 20 73 49 74 65 72 2e 61 50 68     if( sIter.aPh
474a6 72 61 73 65 5b 69 5d 2e 70 48 65 61 64 20 29 7b  rase[i].pHead ){
474a7 0a 20 20 20 20 20 20 2a 70 6d 53 65 65 6e 20 7c  .      *pmSeen |
474a8 3d 20 28 75 36 34 29 31 20 3c 3c 20 69 3b 0a 20  = (u64)1 << i;. 
474a9 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c     }.  }..  /* L
474aa 6f 6f 70 20 74 68 72 6f 75 67 68 20 61 6c 6c 20  oop through all 
474ab 63 61 6e 64 69 64 61 74 65 20 73 6e 69 70 70 65  candidate snippe
474ac 74 73 2e 20 53 74 6f 72 65 20 74 68 65 20 62 65  ts. Store the be
474ad 73 74 20 73 6e 69 70 70 65 74 20 69 6e 20 0a 20  st snippet in . 
474ae 20 2a 2a 20 2a 70 46 72 61 67 6d 65 6e 74 2e 20   ** *pFragment. 
474af 53 74 6f 72 65 20 69 74 73 20 61 73 73 6f 63 69  Store its associ
474b0 61 74 65 64 20 27 73 63 6f 72 65 27 20 69 6e 20  ated 'score' in 
474b1 69 42 65 73 74 53 63 6f 72 65 2e 0a 20 20 2a 2f  iBestScore..  */
474b2 0a 20 20 70 46 72 61 67 6d 65 6e 74 2d 3e 69 43  .  pFragment->iC
474b3 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 77 68 69  ol = iCol;.  whi
474b4 6c 65 28 20 21 66 74 73 33 53 6e 69 70 70 65 74  le( !fts3Snippet
474b5 4e 65 78 74 43 61 6e 64 69 64 61 74 65 28 26 73  NextCandidate(&s
474b6 49 74 65 72 29 20 29 7b 0a 20 20 20 20 69 6e 74  Iter) ){.    int
474b7 20 69 50 6f 73 3b 0a 20 20 20 20 69 6e 74 20 69   iPos;.    int i
474b8 53 63 6f 72 65 3b 0a 20 20 20 20 75 36 34 20 6d  Score;.    u64 m
474b9 43 6f 76 65 72 3b 0a 20 20 20 20 75 36 34 20 6d  Cover;.    u64 m
474ba 48 69 67 68 6c 69 67 68 74 3b 0a 20 20 20 20 66  Highlight;.    f
474bb 74 73 33 53 6e 69 70 70 65 74 44 65 74 61 69 6c  ts3SnippetDetail
474bc 73 28 26 73 49 74 65 72 2c 20 6d 43 6f 76 65 72  s(&sIter, mCover
474bd 65 64 2c 20 26 69 50 6f 73 2c 20 26 69 53 63 6f  ed, &iPos, &iSco
474be 72 65 2c 20 26 6d 43 6f 76 65 72 2c 20 26 6d 48  re, &mCover, &mH
474bf 69 67 68 6c 69 67 68 74 29 3b 0a 20 20 20 20 61  ighlight);.    a
474c0 73 73 65 72 74 28 20 69 53 63 6f 72 65 3e 3d 30  ssert( iScore>=0
474c1 20 29 3b 0a 20 20 20 20 69 66 28 20 69 53 63 6f   );.    if( iSco
474c2 72 65 3e 69 42 65 73 74 53 63 6f 72 65 20 29 7b  re>iBestScore ){
474c3 0a 20 20 20 20 20 20 70 46 72 61 67 6d 65 6e 74  .      pFragment
474c4 2d 3e 69 50 6f 73 20 3d 20 69 50 6f 73 3b 0a 20  ->iPos = iPos;. 
474c5 20 20 20 20 20 70 46 72 61 67 6d 65 6e 74 2d 3e       pFragment->
474c6 68 6c 6d 61 73 6b 20 3d 20 6d 48 69 67 68 6c 69  hlmask = mHighli
474c7 67 68 74 3b 0a 20 20 20 20 20 20 70 46 72 61 67  ght;.      pFrag
474c8 6d 65 6e 74 2d 3e 63 6f 76 65 72 65 64 20 3d 20  ment->covered = 
474c9 6d 43 6f 76 65 72 3b 0a 20 20 20 20 20 20 69 42  mCover;.      iB
474ca 65 73 74 53 63 6f 72 65 20 3d 20 69 53 63 6f 72  estScore = iScor
474cb 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  e;.    }.  }..  
474cc 73 71 6c 69 74 65 33 5f 66 72 65 65 28 73 49 74  sqlite3_free(sIt
474cd 65 72 2e 61 50 68 72 61 73 65 29 3b 0a 20 20 2a  er.aPhrase);.  *
474ce 70 69 53 63 6f 72 65 20 3d 20 69 42 65 73 74 53  piScore = iBestS
474cf 63 6f 72 65 3b 0a 20 20 72 65 74 75 72 6e 20 53  core;.  return S
474d0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a  QLITE_OK;.}.../*
474d1 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 73 74 72  .** Append a str
474d2 69 6e 67 20 74 6f 20 74 68 65 20 73 74 72 69 6e  ing to the strin
474d3 67 2d 62 75 66 66 65 72 20 70 61 73 73 65 64 20  g-buffer passed 
474d4 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
474d5 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ument..**.** If 
474d6 6e 41 70 70 65 6e 64 20 69 73 20 6e 65 67 61 74  nAppend is negat
474d7 69 76 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 65  ive, then the le
474d8 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 72 69  ngth of the stri
474d9 6e 67 20 7a 41 70 70 65 6e 64 20 69 73 0a 2a 2a  ng zAppend is.**
474da 20 64 65 74 65 72 6d 69 6e 65 64 20 75 73 69 6e   determined usin
474db 67 20 73 74 72 6c 65 6e 28 29 2e 0a 2a 2f 0a 73  g strlen()..*/.s
474dc 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 74  tatic int fts3St
474dd 72 69 6e 67 41 70 70 65 6e 64 28 0a 20 20 53 74  ringAppend(.  St
474de 72 42 75 66 66 65 72 20 2a 70 53 74 72 2c 20 20  rBuffer *pStr,  
474df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
474e0 20 42 75 66 66 65 72 20 74 6f 20 61 70 70 65 6e   Buffer to appen
474e1 64 20 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  d to */.  const 
474e2 63 68 61 72 20 2a 7a 41 70 70 65 6e 64 2c 20 20  char *zAppend,  
474e3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
474e4 6e 74 65 72 20 74 6f 20 64 61 74 61 20 74 6f 20  nter to data to 
474e5 61 70 70 65 6e 64 20 74 6f 20 62 75 66 66 65 72  append to buffer
474e6 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 70 70 65 6e   */.  int nAppen
474e7 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
474e8 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
474e9 20 7a 41 70 70 65 6e 64 20 69 6e 20 62 79 74 65   zAppend in byte
474ea 73 20 28 6f 72 20 2d 31 29 20 2a 2f 0a 29 7b 0a  s (or -1) */.){.
474eb 20 20 69 66 28 20 6e 41 70 70 65 6e 64 3c 30 20    if( nAppend<0 
474ec 29 7b 0a 20 20 20 20 6e 41 70 70 65 6e 64 20 3d  ){.    nAppend =
474ed 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 41 70   (int)strlen(zAp
474ee 70 65 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  pend);.  }..  /*
474ef 20 49 66 20 74 68 65 72 65 20 69 73 20 69 6e 73   If there is ins
474f0 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20  ufficient space 
474f1 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 53 74 72  allocated at Str
474f2 42 75 66 66 65 72 2e 7a 2c 20 75 73 65 20 72 65  Buffer.z, use re
474f3 61 6c 6c 6f 63 28 29 0a 20 20 2a 2a 20 74 6f 20  alloc().  ** to 
474f4 67 72 6f 77 20 74 68 65 20 62 75 66 66 65 72 20  grow the buffer 
474f5 75 6e 74 69 6c 20 73 6f 20 74 68 61 74 20 69 74  until so that it
474f6 20 69 73 20 62 69 67 20 65 6e 6f 75 67 68 20 74   is big enough t
474f7 6f 20 61 63 63 6f 6d 61 64 61 74 65 20 74 68 65  o accomadate the
474f8 0a 20 20 2a 2a 20 61 70 70 65 6e 64 65 64 20 64  .  ** appended d
474f9 61 74 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ata..  */.  if( 
474fa 70 53 74 72 2d 3e 6e 2b 6e 41 70 70 65 6e 64 2b  pStr->n+nAppend+
474fb 31 3e 3d 70 53 74 72 2d 3e 6e 41 6c 6c 6f 63 20  1>=pStr->nAlloc 
474fc 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f  ){.    int nAllo
474fd 63 20 3d 20 70 53 74 72 2d 3e 6e 41 6c 6c 6f 63  c = pStr->nAlloc
474fe 2b 6e 41 70 70 65 6e 64 2b 31 30 30 3b 0a 20 20  +nAppend+100;.  
474ff 20 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73    char *zNew = s
47500 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70  qlite3_realloc(p
47501 53 74 72 2d 3e 7a 2c 20 6e 41 6c 6c 6f 63 29 3b  Str->z, nAlloc);
47502 0a 20 20 20 20 69 66 28 20 21 7a 4e 65 77 20 29  .    if( !zNew )
47503 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
47504 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
47505 20 7d 0a 20 20 20 20 70 53 74 72 2d 3e 7a 20 3d   }.    pStr->z =
47506 20 7a 4e 65 77 3b 0a 20 20 20 20 70 53 74 72 2d   zNew;.    pStr-
47507 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 41 6c 6c 6f 63  >nAlloc = nAlloc
47508 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 70 70 65  ;.  }..  /* Appe
47509 6e 64 20 74 68 65 20 64 61 74 61 20 74 6f 20 74  nd the data to t
4750a 68 65 20 73 74 72 69 6e 67 20 62 75 66 66 65 72  he string buffer
4750b 2e 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 26 70  . */.  memcpy(&p
4750c 53 74 72 2d 3e 7a 5b 70 53 74 72 2d 3e 6e 5d 2c  Str->z[pStr->n],
4750d 20 7a 41 70 70 65 6e 64 2c 20 6e 41 70 70 65 6e   zAppend, nAppen
4750e 64 29 3b 0a 20 20 70 53 74 72 2d 3e 6e 20 2b 3d  d);.  pStr->n +=
4750f 20 6e 41 70 70 65 6e 64 3b 0a 20 20 70 53 74 72   nAppend;.  pStr
47510 2d 3e 7a 5b 70 53 74 72 2d 3e 6e 5d 20 3d 20 27  ->z[pStr->n] = '
47511 5c 30 27 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53  \0';..  return S
47512 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
47513 2a 2a 20 54 68 65 20 66 74 73 33 42 65 73 74 53  ** The fts3BestS
47514 6e 69 70 70 65 74 28 29 20 66 75 6e 63 74 69 6f  nippet() functio
47515 6e 20 6f 66 74 65 6e 20 73 65 6c 65 63 74 73 20  n often selects 
47516 73 6e 69 70 70 65 74 73 20 74 68 61 74 20 65 6e  snippets that en
47517 64 20 77 69 74 68 20 61 0a 2a 2a 20 71 75 65 72  d with a.** quer
47518 79 20 74 65 72 6d 2e 20 54 68 61 74 20 69 73 2c  y term. That is,
47519 20 74 68 65 20 66 69 6e 61 6c 20 74 65 72 6d 20   the final term 
4751a 6f 66 20 74 68 65 20 73 6e 69 70 70 65 74 20 69  of the snippet i
4751b 73 20 61 6c 77 61 79 73 20 61 20 74 65 72 6d 0a  s always a term.
4751c 2a 2a 20 74 68 61 74 20 72 65 71 75 69 72 65 73  ** that requires
4751d 20 68 69 67 68 6c 69 67 68 74 69 6e 67 2e 20 46   highlighting. F
4751e 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 27  or example, if '
4751f 58 27 20 69 73 20 61 20 68 69 67 68 6c 69 67 68  X' is a highligh
47520 74 65 64 20 74 65 72 6d 0a 2a 2a 20 61 6e 64 20  ted term.** and 
47521 27 2e 27 20 69 73 20 61 20 6e 6f 6e 2d 68 69 67  '.' is a non-hig
47522 68 6c 69 67 68 74 65 64 20 74 65 72 6d 2c 20 42  hlighted term, B
47523 65 73 74 53 6e 69 70 70 65 74 28 29 20 6d 61 79  estSnippet() may
47524 20 73 65 6c 65 63 74 3a 0a 2a 2a 0a 2a 2a 20 20   select:.**.**  
47525 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 58 2e 2e 2e 2e     ........X....
47526 2e 58 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  .X.**.** This fu
47527 6e 63 74 69 6f 6e 20 22 73 68 69 66 74 73 22 20  nction "shifts" 
47528 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
47529 20 74 68 65 20 73 6e 69 70 70 65 74 20 66 6f 72   the snippet for
4752a 77 61 72 64 20 69 6e 20 74 68 65 20 0a 2a 2a 20  ward in the .** 
4752b 64 6f 63 75 6d 65 6e 74 20 73 6f 20 74 68 61 74  document so that
4752c 20 74 68 65 72 65 20 61 72 65 20 61 70 70 72 6f   there are appro
4752d 78 69 6d 61 74 65 6c 79 20 74 68 65 20 73 61 6d  ximately the sam
4752e 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 2a 2a 20  e number of .** 
4752f 6e 6f 6e 2d 68 69 67 68 6c 69 67 68 74 65 64 20  non-highlighted 
47530 74 65 72 6d 73 20 74 6f 20 74 68 65 20 72 69 67  terms to the rig
47531 68 74 20 6f 66 20 74 68 65 20 66 69 6e 61 6c 20  ht of the final 
47532 68 69 67 68 6c 69 67 68 74 65 64 20 74 65 72 6d  highlighted term
47533 20 61 73 20 74 68 65 72 65 0a 2a 2a 20 61 72 65   as there.** are
47534 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20   to the left of 
47535 74 68 65 20 66 69 72 73 74 20 68 69 67 68 6c 69  the first highli
47536 67 68 74 65 64 20 74 65 72 6d 2e 20 46 6f 72 20  ghted term. For 
47537 65 78 61 6d 70 6c 65 2c 20 74 6f 20 74 68 69 73  example, to this
47538 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2e 2e 2e 2e  :.**.**     ....
47539 58 2e 2e 2e 2e 2e 58 2e 2e 2e 2e 0a 2a 2a 0a 2a  X.....X.....**.*
4753a 2a 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20 61  * This is done a
4753b 73 20 70 61 72 74 20 6f 66 20 65 78 74 72 61 63  s part of extrac
4753c 74 69 6e 67 20 74 68 65 20 73 6e 69 70 70 65 74  ting the snippet
4753d 20 74 65 78 74 2c 20 6e 6f 74 20 77 68 65 6e 20   text, not when 
4753e 73 65 6c 65 63 74 69 6e 67 0a 2a 2a 20 74 68 65  selecting.** the
4753f 20 73 6e 69 70 70 65 74 2e 20 53 6e 69 70 70 65   snippet. Snippe
47540 74 20 73 65 6c 65 63 74 69 6f 6e 20 69 73 20 64  t selection is d
47541 6f 6e 65 20 62 61 73 65 64 20 6f 6e 20 64 6f 63  one based on doc
47542 6c 69 73 74 73 20 6f 6e 6c 79 2c 20 73 6f 20 74  lists only, so t
47543 68 65 72 65 0a 2a 2a 20 69 73 20 6e 6f 20 77 61  here.** is no wa
47544 79 20 66 6f 72 20 66 74 73 33 42 65 73 74 53 6e  y for fts3BestSn
47545 69 70 70 65 74 28 29 20 74 6f 20 6b 6e 6f 77 20  ippet() to know 
47546 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
47547 68 65 20 64 6f 63 75 6d 65 6e 74 20 0a 2a 2a 20  he document .** 
47548 61 63 74 75 61 6c 6c 79 20 63 6f 6e 74 61 69 6e  actually contain
47549 73 20 74 65 72 6d 73 20 74 68 61 74 20 66 6f 6c  s terms that fol
4754a 6c 6f 77 20 74 68 65 20 66 69 6e 61 6c 20 68 69  low the final hi
4754b 67 68 6c 69 67 68 74 65 64 20 74 65 72 6d 2e 20  ghlighted term. 
4754c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
4754d 74 73 33 53 6e 69 70 70 65 74 53 68 69 66 74 28  ts3SnippetShift(
4754e 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 54  .  Fts3Table *pT
4754f 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ab,             
47550 20 20 20 2f 2a 20 46 54 53 33 20 74 61 62 6c 65     /* FTS3 table
47551 20 73 6e 69 70 70 65 74 20 63 6f 6d 65 73 20 66   snippet comes f
47552 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 61  rom */.  int iLa
47553 6e 67 69 64 2c 20 20 20 20 20 20 20 20 20 20 20  ngid,           
47554 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 6e 67           /* Lang
47555 75 61 67 65 20 69 64 20 74 6f 20 75 73 65 20 69  uage id to use i
47556 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 2a 2f 0a  n tokenizing */.
47557 20 20 69 6e 74 20 6e 53 6e 69 70 70 65 74 2c 20    int nSnippet, 
47558 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47559 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
4755a 6f 6b 65 6e 73 20 64 65 73 69 72 65 64 20 66 6f  okens desired fo
4755b 72 20 73 6e 69 70 70 65 74 20 2a 2f 0a 20 20 63  r snippet */.  c
4755c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 2c  onst char *zDoc,
4755d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4755e 2a 20 44 6f 63 75 6d 65 6e 74 20 74 65 78 74 20  * Document text 
4755f 74 6f 20 65 78 74 72 61 63 74 20 73 6e 69 70 70  to extract snipp
47560 65 74 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74  et from */.  int
47561 20 6e 44 6f 63 2c 20 20 20 20 20 20 20 20 20 20   nDoc,          
47562 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47563 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20 7a  Size of buffer z
47564 44 6f 63 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  Doc in bytes */.
47565 20 20 69 6e 74 20 2a 70 69 50 6f 73 2c 20 20 20    int *piPos,   
47566 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47567 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 46 69 72    /* IN/OUT: Fir
47568 73 74 20 74 6f 6b 65 6e 20 6f 66 20 73 6e 69 70  st token of snip
47569 70 65 74 20 2a 2f 0a 20 20 75 36 34 20 2a 70 48  pet */.  u64 *pH
4756a 6c 6d 61 73 6b 20 20 20 20 20 20 20 20 20 20 20  lmask           
4756b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
4756c 55 54 3a 20 4d 61 73 6b 20 6f 66 20 74 6f 6b 65  UT: Mask of toke
4756d 6e 73 20 74 6f 20 68 69 67 68 6c 69 67 68 74 20  ns to highlight 
4756e 2a 2f 0a 29 7b 0a 20 20 75 36 34 20 68 6c 6d 61  */.){.  u64 hlma
4756f 73 6b 20 3d 20 2a 70 48 6c 6d 61 73 6b 3b 20 20  sk = *pHlmask;  
47570 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c          /* Local
47571 20 63 6f 70 79 20 6f 66 20 69 6e 69 74 69 61 6c   copy of initial
47572 20 68 69 67 68 6c 69 67 68 74 2d 6d 61 73 6b 20   highlight-mask 
47573 2a 2f 0a 0a 20 20 69 66 28 20 68 6c 6d 61 73 6b  */..  if( hlmask
47574 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 4c 65 66   ){.    int nLef
47575 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
47576 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 73 20        /* Tokens 
47577 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 66  to the left of f
47578 69 72 73 74 20 68 69 67 68 6c 69 67 68 74 20 2a  irst highlight *
47579 2f 0a 20 20 20 20 69 6e 74 20 6e 52 69 67 68 74  /.    int nRight
4757a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4757b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 73 20 74 6f      /* Tokens to
4757c 20 74 68 65 20 72 69 67 68 74 20 6f 66 20 6c 61   the right of la
4757d 73 74 20 68 69 67 68 6c 69 67 68 74 20 2a 2f 0a  st highlight */.
4757e 20 20 20 20 69 6e 74 20 6e 44 65 73 69 72 65 64      int nDesired
4757f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
47580 20 20 2f 2a 20 49 64 65 61 6c 20 6e 75 6d 62 65    /* Ideal numbe
47581 72 20 6f 66 20 74 6f 6b 65 6e 73 20 74 6f 20 73  r of tokens to s
47582 68 69 66 74 20 66 6f 72 77 61 72 64 20 2a 2f 0a  hift forward */.
47583 0a 20 20 20 20 66 6f 72 28 6e 4c 65 66 74 3d 30  .    for(nLeft=0
47584 3b 20 21 28 68 6c 6d 61 73 6b 20 26 20 28 28 75  ; !(hlmask & ((u
47585 36 34 29 31 20 3c 3c 20 6e 4c 65 66 74 29 29 3b  64)1 << nLeft));
47586 20 6e 4c 65 66 74 2b 2b 29 3b 0a 20 20 20 20 66   nLeft++);.    f
47587 6f 72 28 6e 52 69 67 68 74 3d 30 3b 20 21 28 68  or(nRight=0; !(h
47588 6c 6d 61 73 6b 20 26 20 28 28 75 36 34 29 31 20  lmask & ((u64)1 
47589 3c 3c 20 28 6e 53 6e 69 70 70 65 74 2d 31 2d 6e  << (nSnippet-1-n
4758a 52 69 67 68 74 29 29 29 3b 20 6e 52 69 67 68 74  Right))); nRight
4758b 2b 2b 29 3b 0a 20 20 20 20 6e 44 65 73 69 72 65  ++);.    nDesire
4758c 64 20 3d 20 28 6e 4c 65 66 74 2d 6e 52 69 67 68  d = (nLeft-nRigh
4758d 74 29 2f 32 3b 0a 0a 20 20 20 20 2f 2a 20 49 64  t)/2;..    /* Id
4758e 65 61 6c 6c 79 2c 20 74 68 65 20 73 74 61 72 74  eally, the start
4758f 20 6f 66 20 74 68 65 20 73 6e 69 70 70 65 74 20   of the snippet 
47590 73 68 6f 75 6c 64 20 62 65 20 70 75 73 68 65 64  should be pushed
47591 20 66 6f 72 77 61 72 64 20 69 6e 20 74 68 65 0a   forward in the.
47592 20 20 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74 20      ** document 
47593 6e 44 65 73 69 72 65 64 20 74 6f 6b 65 6e 73 2e  nDesired tokens.
47594 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 68 65 63   This block chec
47595 6b 73 20 69 66 20 74 68 65 72 65 20 61 72 65 20  ks if there are 
47596 61 63 74 75 61 6c 6c 79 0a 20 20 20 20 2a 2a 20  actually.    ** 
47597 6e 44 65 73 69 72 65 64 20 74 6f 6b 65 6e 73 20  nDesired tokens 
47598 74 6f 20 74 68 65 20 72 69 67 68 74 20 6f 66 20  to the right of 
47599 74 68 65 20 73 6e 69 70 70 65 74 2e 20 49 66 20  the snippet. If 
4759a 73 6f 2c 20 2a 70 69 50 6f 73 20 61 6e 64 0a 20  so, *piPos and. 
4759b 20 20 20 2a 2a 20 2a 70 48 6c 4d 61 73 6b 20 61     ** *pHlMask a
4759c 72 65 20 75 70 64 61 74 65 64 20 74 6f 20 73 68  re updated to sh
4759d 69 66 74 20 74 68 65 20 73 6e 69 70 70 65 74 20  ift the snippet 
4759e 6e 44 65 73 69 72 65 64 20 74 6f 6b 65 6e 73 20  nDesired tokens 
4759f 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 69  to the.    ** ri
475a0 67 68 74 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ght. Otherwise, 
475a1 74 68 65 20 73 6e 69 70 70 65 74 20 69 73 20 73  the snippet is s
475a2 68 69 66 74 65 64 20 62 79 20 74 68 65 20 6e 75  hifted by the nu
475a3 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 0a 20  mber of tokens. 
475a4 20 20 20 2a 2a 20 61 76 61 69 6c 61 62 6c 65 2e     ** available.
475a5 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
475a6 6e 44 65 73 69 72 65 64 3e 30 20 29 7b 0a 20 20  nDesired>0 ){.  
475a7 20 20 20 20 69 6e 74 20 6e 53 68 69 66 74 3b 20      int nShift; 
475a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475a9 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b  /* Number of tok
475aa 65 6e 73 20 74 6f 20 73 68 69 66 74 20 73 6e 69  ens to shift sni
475ab 70 70 65 74 20 62 79 20 2a 2f 0a 20 20 20 20 20  ppet by */.     
475ac 20 69 6e 74 20 69 43 75 72 72 65 6e 74 20 3d 20   int iCurrent = 
475ad 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  0;           /* 
475ae 54 6f 6b 65 6e 20 63 6f 75 6e 74 65 72 20 2a 2f  Token counter */
475af 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b 20 20  .      int rc;  
475b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475b1 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64     /* Return Cod
475b2 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  e */.      sqlit
475b3 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
475b4 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20 20 20 20  ule *pMod;.     
475b5 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
475b6 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 3b 0a 20  er_cursor *pC;. 
475b7 20 20 20 20 20 70 4d 6f 64 20 3d 20 28 73 71 6c       pMod = (sql
475b8 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
475b9 6f 64 75 6c 65 20 2a 29 70 54 61 62 2d 3e 70 54  odule *)pTab->pT
475ba 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c  okenizer->pModul
475bb 65 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65  e;..      /* Ope
475bc 6e 20 61 20 63 75 72 73 6f 72 20 6f 6e 20 7a 44  n a cursor on zD
475bd 6f 63 2f 6e 44 6f 63 2e 20 43 68 65 63 6b 20 69  oc/nDoc. Check i
475be 66 20 74 68 65 72 65 20 61 72 65 20 28 6e 53 6e  f there are (nSn
475bf 69 70 70 65 74 2b 6e 44 65 73 69 72 65 64 29 0a  ippet+nDesired).
475c0 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f 72 65        ** or more
475c1 20 74 6f 6b 65 6e 73 20 69 6e 20 7a 44 6f 63 2f   tokens in zDoc/
475c2 6e 44 6f 63 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  nDoc..      */. 
475c3 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
475c4 33 46 74 73 33 4f 70 65 6e 54 6f 6b 65 6e 69 7a  3Fts3OpenTokeniz
475c5 65 72 28 70 54 61 62 2d 3e 70 54 6f 6b 65 6e 69  er(pTab->pTokeni
475c6 7a 65 72 2c 20 69 4c 61 6e 67 69 64 2c 20 7a 44  zer, iLangid, zD
475c7 6f 63 2c 20 6e 44 6f 63 2c 20 26 70 43 29 3b 0a  oc, nDoc, &pC);.
475c8 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
475c9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
475ca 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
475cb 20 20 20 20 7d 0a 20 20 20 20 20 20 77 68 69 6c      }.      whil
475cc 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc==SQLITE_OK
475cd 20 26 26 20 69 43 75 72 72 65 6e 74 3c 28 6e 53   && iCurrent<(nS
475ce 6e 69 70 70 65 74 2b 6e 44 65 73 69 72 65 64 29  nippet+nDesired)
475cf 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73   ){.        cons
475d0 74 20 63 68 61 72 20 2a 5a 44 55 4d 4d 59 3b 20  t char *ZDUMMY; 
475d1 69 6e 74 20 44 55 4d 4d 59 31 20 3d 20 30 2c 20  int DUMMY1 = 0, 
475d2 44 55 4d 4d 59 32 20 3d 20 30 2c 20 44 55 4d 4d  DUMMY2 = 0, DUMM
475d3 59 33 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  Y3 = 0;.        
475d4 72 63 20 3d 20 70 4d 6f 64 2d 3e 78 4e 65 78 74  rc = pMod->xNext
475d5 28 70 43 2c 20 26 5a 44 55 4d 4d 59 2c 20 26 44  (pC, &ZDUMMY, &D
475d6 55 4d 4d 59 31 2c 20 26 44 55 4d 4d 59 32 2c 20  UMMY1, &DUMMY2, 
475d7 26 44 55 4d 4d 59 33 2c 20 26 69 43 75 72 72 65  &DUMMY3, &iCurre
475d8 6e 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nt);.      }.   
475d9 20 20 20 70 4d 6f 64 2d 3e 78 43 6c 6f 73 65 28     pMod->xClose(
475da 70 43 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  pC);.      if( r
475db 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c!=SQLITE_OK && 
475dc 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
475dd 29 7b 20 72 65 74 75 72 6e 20 72 63 3b 20 7d 0a  ){ return rc; }.
475de 0a 20 20 20 20 20 20 6e 53 68 69 66 74 20 3d 20  .      nShift = 
475df 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45  (rc==SQLITE_DONE
475e0 29 2b 69 43 75 72 72 65 6e 74 2d 6e 53 6e 69 70  )+iCurrent-nSnip
475e1 70 65 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72  pet;.      asser
475e2 74 28 20 6e 53 68 69 66 74 3c 3d 6e 44 65 73 69  t( nShift<=nDesi
475e3 72 65 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28  red );.      if(
475e4 20 6e 53 68 69 66 74 3e 30 20 29 7b 0a 20 20 20   nShift>0 ){.   
475e5 20 20 20 20 20 2a 70 69 50 6f 73 20 2b 3d 20 6e       *piPos += n
475e6 53 68 69 66 74 3b 0a 20 20 20 20 20 20 20 20 2a  Shift;.        *
475e7 70 48 6c 6d 61 73 6b 20 3d 20 68 6c 6d 61 73 6b  pHlmask = hlmask
475e8 20 3e 3e 20 6e 53 68 69 66 74 3b 0a 20 20 20 20   >> nShift;.    
475e9 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
475ea 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
475eb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61  ;.}../*.** Extra
475ec 63 74 20 74 68 65 20 73 6e 69 70 70 65 74 20 74  ct the snippet t
475ed 65 78 74 20 66 6f 72 20 66 72 61 67 6d 65 6e 74  ext for fragment
475ee 20 70 46 72 61 67 6d 65 6e 74 20 66 72 6f 6d 20   pFragment from 
475ef 63 75 72 73 6f 72 20 70 43 73 72 20 61 6e 64 0a  cursor pCsr and.
475f0 2a 2a 20 61 70 70 65 6e 64 20 69 74 20 74 6f 20  ** append it to 
475f1 73 74 72 69 6e 67 20 62 75 66 66 65 72 20 70 4f  string buffer pO
475f2 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ut..*/.static in
475f3 74 20 66 74 73 33 53 6e 69 70 70 65 74 54 65 78  t fts3SnippetTex
475f4 74 28 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20  t(.  Fts3Cursor 
475f5 2a 70 43 73 72 2c 20 20 20 20 20 20 20 20 20 20  *pCsr,          
475f6 20 20 20 20 20 2f 2a 20 46 54 53 33 20 43 75 72       /* FTS3 Cur
475f7 73 6f 72 20 2a 2f 0a 20 20 53 6e 69 70 70 65 74  sor */.  Snippet
475f8 46 72 61 67 6d 65 6e 74 20 2a 70 46 72 61 67 6d  Fragment *pFragm
475f9 65 6e 74 2c 20 20 20 20 20 2f 2a 20 53 6e 69 70  ent,     /* Snip
475fa 70 65 74 20 74 6f 20 65 78 74 72 61 63 74 20 2a  pet to extract *
475fb 2f 0a 20 20 69 6e 74 20 69 46 72 61 67 6d 65 6e  /.  int iFragmen
475fc 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
475fd 20 20 20 20 2f 2a 20 46 72 61 67 6d 65 6e 74 20      /* Fragment 
475fe 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20  number */.  int 
475ff 69 73 4c 61 73 74 2c 20 20 20 20 20 20 20 20 20  isLast,         
47600 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
47601 72 75 65 20 66 6f 72 20 66 69 6e 61 6c 20 66 72  rue for final fr
47602 61 67 6d 65 6e 74 20 69 6e 20 73 6e 69 70 70 65  agment in snippe
47603 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6e 69 70  t */.  int nSnip
47604 70 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  pet,            
47605 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
47606 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 65 78   of tokens in ex
47607 74 72 61 63 74 65 64 20 73 6e 69 70 70 65 74 20  tracted snippet 
47608 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
47609 2a 7a 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20  *zOpen,         
4760a 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 69       /* String i
4760b 6e 73 65 72 74 65 64 20 62 65 66 6f 72 65 20 68  nserted before h
4760c 69 67 68 6c 69 67 68 74 65 64 20 74 65 72 6d 20  ighlighted term 
4760d 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
4760e 2a 7a 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  *zClose,        
4760f 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 69       /* String i
47610 6e 73 65 72 74 65 64 20 61 66 74 65 72 20 68 69  nserted after hi
47611 67 68 6c 69 67 68 74 65 64 20 74 65 72 6d 20 2a  ghlighted term *
47612 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
47613 7a 45 6c 6c 69 70 73 69 73 2c 20 20 20 20 20 20  zEllipsis,      
47614 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 69 6e      /* String in
47615 73 65 72 74 65 64 20 62 65 74 77 65 65 6e 20 73  serted between s
47616 6e 69 70 70 65 74 73 20 2a 2f 0a 20 20 53 74 72  nippets */.  Str
47617 42 75 66 66 65 72 20 2a 70 4f 75 74 20 20 20 20  Buffer *pOut    
47618 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47619 57 72 69 74 65 20 6f 75 74 70 75 74 20 68 65 72  Write output her
4761a 65 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 54 61  e */.){.  Fts3Ta
4761b 62 6c 65 20 2a 70 54 61 62 20 3d 20 28 46 74 73  ble *pTab = (Fts
4761c 33 54 61 62 6c 65 20 2a 29 70 43 73 72 2d 3e 62  3Table *)pCsr->b
4761d 61 73 65 2e 70 56 74 61 62 3b 0a 20 20 69 6e 74  ase.pVtab;.  int
4761e 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
4761f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47620 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
47621 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f   const char *zDo
47622 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
47623 20 2f 2a 20 44 6f 63 75 6d 65 6e 74 20 74 65 78   /* Document tex
47624 74 20 74 6f 20 65 78 74 72 61 63 74 20 73 6e 69  t to extract sni
47625 70 70 65 74 20 66 72 6f 6d 20 2a 2f 0a 20 20 69  ppet from */.  i
47626 6e 74 20 6e 44 6f 63 3b 20 20 20 20 20 20 20 20  nt nDoc;        
47627 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
47628 2a 20 53 69 7a 65 20 6f 66 20 7a 44 6f 63 20 69  * Size of zDoc i
47629 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74  n bytes */.  int
4762a 20 69 43 75 72 72 65 6e 74 20 3d 20 30 3b 20 20   iCurrent = 0;  
4762b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4762c 43 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 6e 75  Current token nu
4762d 6d 62 65 72 20 6f 66 20 64 6f 63 75 6d 65 6e 74  mber of document
4762e 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d   */.  int iEnd =
4762f 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
47630 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66        /* Byte of
47631 66 73 65 74 20 6f 66 20 65 6e 64 20 6f 66 20 63  fset of end of c
47632 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a  urrent token */.
47633 20 20 69 6e 74 20 69 73 53 68 69 66 74 44 6f 6e    int isShiftDon
47634 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
47635 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20    /* True after 
47636 73 6e 69 70 70 65 74 20 69 73 20 73 68 69 66 74  snippet is shift
47637 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 50 6f 73  ed */.  int iPos
47638 20 3d 20 70 46 72 61 67 6d 65 6e 74 2d 3e 69 50   = pFragment->iP
47639 6f 73 3b 20 20 20 20 20 2f 2a 20 46 69 72 73 74  os;     /* First
4763a 20 74 6f 6b 65 6e 20 6f 66 20 73 6e 69 70 70 65   token of snippe
4763b 74 20 2a 2f 0a 20 20 75 36 34 20 68 6c 6d 61 73  t */.  u64 hlmas
4763c 6b 20 3d 20 70 46 72 61 67 6d 65 6e 74 2d 3e 68  k = pFragment->h
4763d 6c 6d 61 73 6b 3b 20 2f 2a 20 48 69 67 68 6c 69  lmask; /* Highli
4763e 67 68 74 2d 6d 61 73 6b 20 66 6f 72 20 73 6e 69  ght-mask for sni
4763f 70 70 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43  ppet */.  int iC
47640 6f 6c 20 3d 20 70 46 72 61 67 6d 65 6e 74 2d 3e  ol = pFragment->
47641 69 43 6f 6c 2b 31 3b 20 20 20 2f 2a 20 51 75 65  iCol+1;   /* Que
47642 72 79 20 63 6f 6c 75 6d 6e 20 74 6f 20 65 78 74  ry column to ext
47643 72 61 63 74 20 74 65 78 74 20 66 72 6f 6d 20 2a  ract text from *
47644 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  /.  sqlite3_toke
47645 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 4d  nizer_module *pM
47646 6f 64 3b 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72  od; /* Tokenizer
47647 20 6d 6f 64 75 6c 65 20 6d 65 74 68 6f 64 73 20   module methods 
47648 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69  object */.  sqli
47649 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
4764a 72 73 6f 72 20 2a 70 43 3b 20 20 20 2f 2a 20 54  rsor *pC;   /* T
4764b 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20  okenizer cursor 
4764c 6f 70 65 6e 20 6f 6e 20 7a 44 6f 63 2f 6e 44 6f  open on zDoc/nDo
4764d 63 20 2a 2f 0a 20 20 0a 20 20 7a 44 6f 63 20 3d  c */.  .  zDoc =
4764e 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
4764f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
47650 78 74 28 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20  xt(pCsr->pStmt, 
47651 69 43 6f 6c 29 3b 0a 20 20 69 66 28 20 7a 44 6f  iCol);.  if( zDo
47652 63 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  c==0 ){.    if( 
47653 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
47654 79 70 65 28 70 43 73 72 2d 3e 70 53 74 6d 74 2c  ype(pCsr->pStmt,
47655 20 69 43 6f 6c 29 21 3d 53 51 4c 49 54 45 5f 4e   iCol)!=SQLITE_N
47656 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ULL ){.      ret
47657 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
47658 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
47659 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
4765a 7d 0a 20 20 6e 44 6f 63 20 3d 20 73 71 6c 69 74  }.  nDoc = sqlit
4765b 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
4765c 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20 69 43 6f  pCsr->pStmt, iCo
4765d 6c 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 61  l);..  /* Open a
4765e 20 74 6f 6b 65 6e 20 63 75 72 73 6f 72 20 6f 6e   token cursor on
4765f 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 2e 20 2a   the document. *
47660 2f 0a 20 20 70 4d 6f 64 20 3d 20 28 73 71 6c 69  /.  pMod = (sqli
47661 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
47662 64 75 6c 65 20 2a 29 70 54 61 62 2d 3e 70 54 6f  dule *)pTab->pTo
47663 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65  kenizer->pModule
47664 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
47665 46 74 73 33 4f 70 65 6e 54 6f 6b 65 6e 69 7a 65  Fts3OpenTokenize
47666 72 28 70 54 61 62 2d 3e 70 54 6f 6b 65 6e 69 7a  r(pTab->pTokeniz
47667 65 72 2c 20 70 43 73 72 2d 3e 69 4c 61 6e 67 69  er, pCsr->iLangi
47668 64 2c 20 7a 44 6f 63 2c 6e 44 6f 63 2c 26 70 43  d, zDoc,nDoc,&pC
47669 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
4766a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
4766b 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
4766c 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
4766d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 63 6f 6e 73  E_OK ){.    cons
4766e 74 20 63 68 61 72 20 2a 5a 44 55 4d 4d 59 3b 20  t char *ZDUMMY; 
4766f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 75 6d            /* Dum
47670 6d 79 20 61 72 67 75 6d 65 6e 74 20 75 73 65 64  my argument used
47671 20 77 69 74 68 20 74 6f 6b 65 6e 69 7a 65 72 20   with tokenizer 
47672 2a 2f 0a 20 20 20 20 69 6e 74 20 44 55 4d 4d 59  */.    int DUMMY
47673 31 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20  1 = -1;         
47674 20 20 20 20 20 2f 2a 20 44 75 6d 6d 79 20 61 72       /* Dummy ar
47675 67 75 6d 65 6e 74 20 75 73 65 64 20 77 69 74 68  gument used with
47676 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20   tokenizer */.  
47677 20 20 69 6e 74 20 69 42 65 67 69 6e 20 3d 20 30    int iBegin = 0
47678 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
47679 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 7a 44 6f  /* Offset in zDo
4767a 63 20 6f 66 20 73 74 61 72 74 20 6f 66 20 74 6f  c of start of to
4767b 6b 65 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ken */.    int i
4767c 46 69 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Fin = 0;        
4767d 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
4767e 65 74 20 69 6e 20 7a 44 6f 63 20 6f 66 20 65 6e  et in zDoc of en
4767f 64 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  d of token */.  
47680 20 20 69 6e 74 20 69 73 48 69 67 68 6c 69 67 68    int isHighligh
47681 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
47682 2f 2a 20 54 72 75 65 20 66 6f 72 20 68 69 67 68  /* True for high
47683 6c 69 67 68 74 65 64 20 74 65 72 6d 73 20 2a 2f  lighted terms */
47684 0a 0a 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c  ..    /* Variabl
47685 65 20 44 55 4d 4d 59 31 20 69 73 20 69 6e 69 74  e DUMMY1 is init
47686 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 6e 65 67  ialized to a neg
47687 61 74 69 76 65 20 76 61 6c 75 65 20 61 62 6f 76  ative value abov
47688 65 2e 20 45 6c 73 65 77 68 65 72 65 0a 20 20 20  e. Elsewhere.   
47689 20 2a 2a 20 69 6e 20 74 68 65 20 46 54 53 20 63   ** in the FTS c
4768a 6f 64 65 20 74 68 65 20 76 61 72 69 61 62 6c 65  ode the variable
4768b 20 74 68 61 74 20 74 68 65 20 74 68 69 72 64 20   that the third 
4768c 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 4e 65 78  argument to xNex
4768d 74 20 70 6f 69 6e 74 73 20 74 6f 0a 20 20 20 20  t points to.    
4768e 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  ** is initialize
4768f 64 20 74 6f 20 7a 65 72 6f 20 62 65 66 6f 72 65  d to zero before
47690 20 74 68 65 20 66 69 72 73 74 20 28 2a 62 75 74   the first (*but
47691 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
47692 0a 20 20 20 20 2a 2a 20 73 75 62 73 65 71 75 65  .    ** subseque
47693 6e 74 2a 29 20 63 61 6c 6c 20 74 6f 20 78 4e 65  nt*) call to xNe
47694 78 74 28 29 2e 20 54 68 69 73 20 69 73 20 64 6f  xt(). This is do
47695 6e 65 20 66 6f 72 20 61 20 70 61 72 74 69 63 75  ne for a particu
47696 6c 61 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a  lar application.
47697 20 20 20 20 2a 2a 20 74 68 61 74 20 6e 65 65 64      ** that need
47698 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65  s to know whethe
47699 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 74 6f 6b  r or not the tok
4769a 65 6e 69 7a 65 72 20 69 73 20 62 65 69 6e 67 20  enizer is being 
4769b 75 73 65 64 20 66 6f 72 0a 20 20 20 20 2a 2a 20  used for.    ** 
4769c 73 6e 69 70 70 65 74 20 67 65 6e 65 72 61 74 69  snippet generati
4769d 6f 6e 20 6f 72 20 66 6f 72 20 73 6f 6d 65 20 6f  on or for some o
4769e 74 68 65 72 20 70 75 72 70 6f 73 65 2e 0a 20 20  ther purpose..  
4769f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 78 74 72    **.    ** Extr
476a0 65 6d 65 20 63 61 72 65 20 69 73 20 72 65 71 75  eme care is requ
476a1 69 72 65 64 20 77 68 65 6e 20 77 72 69 74 69 6e  ired when writin
476a2 67 20 63 6f 64 65 20 74 6f 20 64 65 70 65 6e 64  g code to depend
476a3 20 6f 6e 20 74 68 69 73 0a 20 20 20 20 2a 2a 20   on this.    ** 
476a4 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20  initialization. 
476a5 49 74 20 69 73 20 6e 6f 74 20 61 20 64 6f 63 75  It is not a docu
476a6 6d 65 6e 74 65 64 20 70 61 72 74 20 6f 66 20 74  mented part of t
476a7 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 74  he tokenizer int
476a8 65 72 66 61 63 65 2e 0a 20 20 20 20 2a 2a 20 49  erface..    ** I
476a9 66 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 69 73  f a tokenizer is
476aa 20 75 73 65 64 20 64 69 72 65 63 74 6c 79 20 62   used directly b
476ab 79 20 61 6e 79 20 63 6f 64 65 20 6f 75 74 73 69  y any code outsi
476ac 64 65 20 6f 66 20 46 54 53 2c 20 74 68 69 73 0a  de of FTS, this.
476ad 20 20 20 20 2a 2a 20 63 6f 6e 76 65 6e 74 69 6f      ** conventio
476ae 6e 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 72  n might not be r
476af 65 73 70 65 63 74 65 64 2e 20 20 2a 2f 0a 20 20  espected.  */.  
476b0 20 20 72 63 20 3d 20 70 4d 6f 64 2d 3e 78 4e 65    rc = pMod->xNe
476b1 78 74 28 70 43 2c 20 26 5a 44 55 4d 4d 59 2c 20  xt(pC, &ZDUMMY, 
476b2 26 44 55 4d 4d 59 31 2c 20 26 69 42 65 67 69 6e  &DUMMY1, &iBegin
476b3 2c 20 26 69 46 69 6e 2c 20 26 69 43 75 72 72 65  , &iFin, &iCurre
476b4 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  nt);.    if( rc!
476b5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
476b6 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
476b7 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20  TE_DONE ){.     
476b8 20 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61     /* Special ca
476b9 73 65 20 2d 20 74 68 65 20 6c 61 73 74 20 74 6f  se - the last to
476ba 6b 65 6e 20 6f 66 20 74 68 65 20 73 6e 69 70 70  ken of the snipp
476bb 65 74 20 69 73 20 61 6c 73 6f 20 74 68 65 20 6c  et is also the l
476bc 61 73 74 20 74 6f 6b 65 6e 0a 20 20 20 20 20 20  ast token.      
476bd 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 6f 6c 75    ** of the colu
476be 6d 6e 2e 20 41 70 70 65 6e 64 20 61 6e 79 20 70  mn. Append any p
476bf 75 6e 63 74 75 61 74 69 6f 6e 20 74 68 61 74 20  unctuation that 
476c0 6f 63 63 75 72 72 65 64 20 62 65 74 77 65 65 6e  occurred between
476c1 20 74 68 65 20 65 6e 64 0a 20 20 20 20 20 20 20   the end.       
476c2 20 2a 2a 20 6f 66 20 74 68 65 20 70 72 65 76 69   ** of the previ
476c3 6f 75 73 20 74 6f 6b 65 6e 20 61 6e 64 20 74 68  ous token and th
476c4 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 6f 63  e end of the doc
476c5 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 6f 75 74  ument to the out
476c6 70 75 74 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a  put. .        **
476c7 20 54 68 65 6e 20 62 72 65 61 6b 20 6f 75 74 20   Then break out 
476c8 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 2a 2f 0a  of the loop. */.
476c9 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 74 73          rc = fts
476ca 33 53 74 72 69 6e 67 41 70 70 65 6e 64 28 70 4f  3StringAppend(pO
476cb 75 74 2c 20 26 7a 44 6f 63 5b 69 45 6e 64 5d 2c  ut, &zDoc[iEnd],
476cc 20 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20   -1);.      }.  
476cd 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
476ce 0a 20 20 20 20 69 66 28 20 69 43 75 72 72 65 6e  .    if( iCurren
476cf 74 3c 69 50 6f 73 20 29 7b 20 63 6f 6e 74 69 6e  t<iPos ){ contin
476d0 75 65 3b 20 7d 0a 0a 20 20 20 20 69 66 28 20 21  ue; }..    if( !
476d1 69 73 53 68 69 66 74 44 6f 6e 65 20 29 7b 0a 20  isShiftDone ){. 
476d2 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 6e 44 6f       int n = nDo
476d3 63 20 2d 20 69 42 65 67 69 6e 3b 0a 20 20 20 20  c - iBegin;.    
476d4 20 20 72 63 20 3d 20 66 74 73 33 53 6e 69 70 70    rc = fts3Snipp
476d5 65 74 53 68 69 66 74 28 0a 20 20 20 20 20 20 20  etShift(.       
476d6 20 20 20 70 54 61 62 2c 20 70 43 73 72 2d 3e 69     pTab, pCsr->i
476d7 4c 61 6e 67 69 64 2c 20 6e 53 6e 69 70 70 65 74  Langid, nSnippet
476d8 2c 20 26 7a 44 6f 63 5b 69 42 65 67 69 6e 5d 2c  , &zDoc[iBegin],
476d9 20 6e 2c 20 26 69 50 6f 73 2c 20 26 68 6c 6d 61   n, &iPos, &hlma
476da 73 6b 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  sk.      );.    
476db 20 20 69 73 53 68 69 66 74 44 6f 6e 65 20 3d 20    isShiftDone = 
476dc 31 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4e 6f 77  1;..      /* Now
476dd 20 74 68 61 74 20 74 68 65 20 73 68 69 66 74 20   that the shift 
476de 68 61 73 20 62 65 65 6e 20 64 6f 6e 65 2c 20 63  has been done, c
476df 68 65 63 6b 20 69 66 20 74 68 65 20 69 6e 69 74  heck if the init
476e0 69 61 6c 20 22 2e 2e 2e 22 20 61 72 65 0a 20 20  ial "..." are.  
476e1 20 20 20 20 2a 2a 20 72 65 71 75 69 72 65 64 2e      ** required.
476e2 20 54 68 65 79 20 61 72 65 20 72 65 71 75 69 72   They are requir
476e3 65 64 20 69 66 20 28 61 29 20 74 68 69 73 20 69  ed if (a) this i
476e4 73 20 6e 6f 74 20 74 68 65 20 66 69 72 73 74 20  s not the first 
476e5 66 72 61 67 6d 65 6e 74 2c 0a 20 20 20 20 20 20  fragment,.      
476e6 2a 2a 20 6f 72 20 28 62 29 20 74 68 69 73 20 66  ** or (b) this f
476e7 72 61 67 6d 65 6e 74 20 64 6f 65 73 20 6e 6f 74  ragment does not
476e8 20 62 65 67 69 6e 20 61 74 20 70 6f 73 69 74 69   begin at positi
476e9 6f 6e 20 30 20 6f 66 20 69 74 73 20 63 6f 6c 75  on 0 of its colu
476ea 6d 6e 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20  mn. .      */.  
476eb 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
476ec 54 45 5f 4f 4b 20 26 26 20 28 69 50 6f 73 3e 30  TE_OK && (iPos>0
476ed 20 7c 7c 20 69 46 72 61 67 6d 65 6e 74 3e 30 29   || iFragment>0)
476ee 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
476ef 20 66 74 73 33 53 74 72 69 6e 67 41 70 70 65 6e   fts3StringAppen
476f0 64 28 70 4f 75 74 2c 20 7a 45 6c 6c 69 70 73 69  d(pOut, zEllipsi
476f1 73 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a  s, -1);.      }.
476f2 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
476f3 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 43 75 72 72  LITE_OK || iCurr
476f4 65 6e 74 3c 69 50 6f 73 20 29 20 63 6f 6e 74 69  ent<iPos ) conti
476f5 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  nue;.    }..    
476f6 69 66 28 20 69 43 75 72 72 65 6e 74 3e 3d 28 69  if( iCurrent>=(i
476f7 50 6f 73 2b 6e 53 6e 69 70 70 65 74 29 20 29 7b  Pos+nSnippet) ){
476f8 0a 20 20 20 20 20 20 69 66 28 20 69 73 4c 61 73  .      if( isLas
476f9 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  t ){.        rc 
476fa 3d 20 66 74 73 33 53 74 72 69 6e 67 41 70 70 65  = fts3StringAppe
476fb 6e 64 28 70 4f 75 74 2c 20 7a 45 6c 6c 69 70 73  nd(pOut, zEllips
476fc 69 73 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 7d  is, -1);.      }
476fd 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
476fe 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20    }..    /* Set 
476ff 69 73 48 69 67 68 6c 69 67 68 74 20 74 6f 20 74  isHighlight to t
47700 72 75 65 20 69 66 20 74 68 69 73 20 74 65 72 6d  rue if this term
47701 20 73 68 6f 75 6c 64 20 62 65 20 68 69 67 68 6c   should be highl
47702 69 67 68 74 65 64 2e 20 2a 2f 0a 20 20 20 20 69  ighted. */.    i
47703 73 48 69 67 68 6c 69 67 68 74 20 3d 20 28 68 6c  sHighlight = (hl
47704 6d 61 73 6b 20 26 20 28 28 75 36 34 29 31 20 3c  mask & ((u64)1 <
47705 3c 20 28 69 43 75 72 72 65 6e 74 2d 69 50 6f 73  < (iCurrent-iPos
47706 29 29 29 21 3d 30 3b 0a 0a 20 20 20 20 69 66 28  )))!=0;..    if(
47707 20 69 43 75 72 72 65 6e 74 3e 69 50 6f 73 20 29   iCurrent>iPos )
47708 20 72 63 20 3d 20 66 74 73 33 53 74 72 69 6e 67   rc = fts3String
47709 41 70 70 65 6e 64 28 70 4f 75 74 2c 20 26 7a 44  Append(pOut, &zD
4770a 6f 63 5b 69 45 6e 64 5d 2c 20 69 42 65 67 69 6e  oc[iEnd], iBegin
4770b 2d 69 45 6e 64 29 3b 0a 20 20 20 20 69 66 28 20  -iEnd);.    if( 
4770c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
4770d 20 69 73 48 69 67 68 6c 69 67 68 74 20 29 20 72   isHighlight ) r
4770e 63 20 3d 20 66 74 73 33 53 74 72 69 6e 67 41 70  c = fts3StringAp
4770f 70 65 6e 64 28 70 4f 75 74 2c 20 7a 4f 70 65 6e  pend(pOut, zOpen
47710 2c 20 2d 31 29 3b 0a 20 20 20 20 69 66 28 20 72  , -1);.    if( r
47711 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c==SQLITE_OK ) r
47712 63 20 3d 20 66 74 73 33 53 74 72 69 6e 67 41 70  c = fts3StringAp
47713 70 65 6e 64 28 70 4f 75 74 2c 20 26 7a 44 6f 63  pend(pOut, &zDoc
47714 5b 69 42 65 67 69 6e 5d 2c 20 69 46 69 6e 2d 69  [iBegin], iFin-i
47715 42 65 67 69 6e 29 3b 0a 20 20 20 20 69 66 28 20  Begin);.    if( 
47716 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
47717 20 69 73 48 69 67 68 6c 69 67 68 74 20 29 20 72   isHighlight ) r
47718 63 20 3d 20 66 74 73 33 53 74 72 69 6e 67 41 70  c = fts3StringAp
47719 70 65 6e 64 28 70 4f 75 74 2c 20 7a 43 6c 6f 73  pend(pOut, zClos
4771a 65 2c 20 2d 31 29 3b 0a 0a 20 20 20 20 69 45 6e  e, -1);..    iEn
4771b 64 20 3d 20 69 46 69 6e 3b 0a 20 20 7d 0a 0a 20  d = iFin;.  }.. 
4771c 20 70 4d 6f 64 2d 3e 78 43 6c 6f 73 65 28 70 43   pMod->xClose(pC
4771d 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
4771e 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  }.../*.** This f
4771f 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
47720 74 6f 20 63 6f 75 6e 74 20 74 68 65 20 65 6e 74  to count the ent
47721 72 69 65 73 20 69 6e 20 61 20 63 6f 6c 75 6d 6e  ries in a column
47722 2d 6c 69 73 74 20 28 61 20 0a 2a 2a 20 64 65 6c  -list (a .** del
47723 74 61 2d 65 6e 63 6f 64 65 64 20 6c 69 73 74 20  ta-encoded list 
47724 6f 66 20 74 65 72 6d 20 6f 66 66 73 65 74 73 20  of term offsets 
47725 77 69 74 68 69 6e 20 61 20 73 69 6e 67 6c 65 20  within a single 
47726 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 69 6e 67  column of a sing
47727 6c 65 20 0a 2a 2a 20 72 6f 77 29 2e 20 57 68 65  le .** row). Whe
47728 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
47729 69 73 20 63 61 6c 6c 65 64 2c 20 2a 70 70 43 6f  is called, *ppCo
4772a 6c 6c 69 73 74 20 73 68 6f 75 6c 64 20 70 6f 69  llist should poi
4772b 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 62 65 67  nt to the.** beg
4772c 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69  inning of the fi
4772d 72 73 74 20 76 61 72 69 6e 74 20 69 6e 20 74 68  rst varint in th
4772e 65 20 63 6f 6c 75 6d 6e 2d 6c 69 73 74 20 28 74  e column-list (t
4772f 68 65 20 76 61 72 69 6e 74 20 74 68 61 74 0a 2a  he varint that.*
47730 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70  * contains the p
47731 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 66  osition of the f
47732 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20 74 65  irst matching te
47733 72 6d 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e  rm in the column
47734 20 64 61 74 61 29 2e 0a 2a 2a 20 42 65 66 6f 72   data)..** Befor
47735 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 70  e returning, *pp
47736 43 6f 6c 6c 69 73 74 20 69 73 20 73 65 74 20 74  Collist is set t
47737 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66  o point to the f
47738 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72 0a  irst byte after.
47739 2a 2a 20 74 68 65 20 6c 61 73 74 20 76 61 72 69  ** the last vari
4773a 6e 74 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e  nt in the column
4773b 2d 6c 69 73 74 20 28 65 69 74 68 65 72 20 74 68  -list (either th
4773c 65 20 30 78 30 30 20 73 69 67 6e 69 66 79 69 6e  e 0x00 signifyin
4773d 67 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20  g the end.** of 
4773e 74 68 65 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73  the position-lis
4773f 74 2c 20 6f 72 20 74 68 65 20 30 78 30 31 20 74  t, or the 0x01 t
47740 68 61 74 20 70 72 65 63 65 64 65 73 20 74 68 65  hat precedes the
47741 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 6f   column number o
47742 66 0a 2a 2a 20 74 68 65 20 6e 65 78 74 20 63 6f  f.** the next co
47743 6c 75 6d 6e 20 69 6e 20 74 68 65 20 70 6f 73 69  lumn in the posi
47744 74 69 6f 6e 2d 6c 69 73 74 29 2e 0a 2a 2a 0a 2a  tion-list)..**.*
47745 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * The number of 
47746 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  elements in the 
47747 63 6f 6c 75 6d 6e 2d 6c 69 73 74 20 69 73 20 72  column-list is r
47748 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
47749 69 63 20 69 6e 74 20 66 74 73 33 43 6f 6c 75 6d  ic int fts3Colum
4774a 6e 6c 69 73 74 43 6f 75 6e 74 28 63 68 61 72 20  nlistCount(char 
4774b 2a 2a 70 70 43 6f 6c 6c 69 73 74 29 7b 0a 20 20  **ppCollist){.  
4774c 63 68 61 72 20 2a 70 45 6e 64 20 3d 20 2a 70 70  char *pEnd = *pp
4774d 43 6f 6c 6c 69 73 74 3b 0a 20 20 63 68 61 72 20  Collist;.  char 
4774e 63 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 45 6e  c = 0;.  int nEn
4774f 74 72 79 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 41  try = 0;..  /* A
47750 20 63 6f 6c 75 6d 6e 2d 6c 69 73 74 20 69 73 20   column-list is 
47751 74 65 72 6d 69 6e 61 74 65 64 20 62 79 20 65 69  terminated by ei
47752 74 68 65 72 20 61 20 30 78 30 31 20 6f 72 20 30  ther a 0x01 or 0
47753 78 30 30 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28  x00. */.  while(
47754 20 30 78 46 45 20 26 20 28 2a 70 45 6e 64 20 7c   0xFE & (*pEnd |
47755 20 63 29 20 29 7b 0a 20 20 20 20 63 20 3d 20 2a   c) ){.    c = *
47756 70 45 6e 64 2b 2b 20 26 20 30 78 38 30 3b 0a 20  pEnd++ & 0x80;. 
47757 20 20 20 69 66 28 20 21 63 20 29 20 6e 45 6e 74     if( !c ) nEnt
47758 72 79 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70  ry++;.  }..  *pp
47759 43 6f 6c 6c 69 73 74 20 3d 20 70 45 6e 64 3b 0a  Collist = pEnd;.
4775a 20 20 72 65 74 75 72 6e 20 6e 45 6e 74 72 79 3b    return nEntry;
4775b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 66 74 73 33 45 78  .}../*.** fts3Ex
4775c 70 72 49 74 65 72 61 74 65 28 29 20 63 61 6c 6c  prIterate() call
4775d 62 61 63 6b 20 75 73 65 64 20 74 6f 20 63 6f 6c  back used to col
4775e 6c 65 63 74 20 74 68 65 20 22 67 6c 6f 62 61 6c  lect the "global
4775f 22 20 6d 61 74 63 68 69 6e 66 6f 20 73 74 61 74  " matchinfo stat
47760 73 0a 2a 2a 20 66 6f 72 20 61 20 73 69 6e 67 6c  s.** for a singl
47761 65 20 71 75 65 72 79 2e 20 0a 2a 2a 0a 2a 2a 20  e query. .**.** 
47762 66 74 73 33 45 78 70 72 49 74 65 72 61 74 65 28  fts3ExprIterate(
47763 29 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 6c 6f  ) callback to lo
47764 61 64 20 74 68 65 20 27 67 6c 6f 62 61 6c 27 20  ad the 'global' 
47765 65 6c 65 6d 65 6e 74 73 20 6f 66 20 61 0a 2a 2a  elements of a.**
47766 20 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f   FTS3_MATCHINFO_
47767 48 49 54 53 20 6d 61 74 63 68 69 6e 66 6f 20 61  HITS matchinfo a
47768 72 72 61 79 2e 20 54 68 65 20 67 6c 6f 62 61 6c  rray. The global
47769 20 73 74 61 74 73 20 61 72 65 20 74 68 6f 73 65   stats are those
4776a 20 65 6c 65 6d 65 6e 74 73 20 0a 2a 2a 20 6f 66   elements .** of
4776b 20 74 68 65 20 6d 61 74 63 68 69 6e 66 6f 20 61   the matchinfo a
4776c 72 72 61 79 20 74 68 61 74 20 61 72 65 20 63 6f  rray that are co
4776d 6e 73 74 61 6e 74 20 66 6f 72 20 61 6c 6c 20 72  nstant for all r
4776e 6f 77 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  ows returned by 
4776f 74 68 65 20 0a 2a 2a 20 63 75 72 72 65 6e 74 20  the .** current 
47770 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 41 72 67  query..**.** Arg
47771 75 6d 65 6e 74 20 70 43 74 78 20 69 73 20 61 63  ument pCtx is ac
47772 74 75 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72  tually a pointer
47773 20 74 6f 20 61 20 73 74 72 75 63 74 20 6f 66 20   to a struct of 
47774 74 79 70 65 20 4d 61 74 63 68 49 6e 66 6f 2e 20  type MatchInfo. 
47775 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  This.** function
47776 20 70 6f 70 75 6c 61 74 65 73 20 4d 61 74 63 68   populates Match
47777 69 6e 66 6f 2e 61 4d 61 74 63 68 69 6e 66 6f 5b  info.aMatchinfo[
47778 5d 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  ] as follows:.**
47779 0a 2a 2a 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30  .**   for(iCol=0
4777a 3b 20 69 43 6f 6c 3c 6e 43 6f 6c 3b 20 69 43 6f  ; iCol<nCol; iCo
4777b 6c 2b 2b 29 7b 0a 2a 2a 20 20 20 20 20 61 4d 61  l++){.**     aMa
4777c 74 63 68 69 6e 66 6f 5b 33 2a 69 50 68 72 61 73  tchinfo[3*iPhras
4777d 65 2a 6e 43 6f 6c 20 2b 20 33 2a 69 43 6f 6c 20  e*nCol + 3*iCol 
4777e 2b 20 31 5d 20 3d 20 58 3b 0a 2a 2a 20 20 20 20  + 1] = X;.**    
4777f 20 61 4d 61 74 63 68 69 6e 66 6f 5b 33 2a 69 50   aMatchinfo[3*iP
47780 68 72 61 73 65 2a 6e 43 6f 6c 20 2b 20 33 2a 69  hrase*nCol + 3*i
47781 43 6f 6c 20 2b 20 32 5d 20 3d 20 59 3b 0a 2a 2a  Col + 2] = Y;.**
47782 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65     }.**.** where
47783 20 58 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72   X is the number
47784 20 6f 66 20 6d 61 74 63 68 65 73 20 66 6f 72 20   of matches for 
47785 70 68 72 61 73 65 20 69 50 68 72 61 73 65 20 69  phrase iPhrase i
47786 73 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20 6f 66  s column iCol of
47787 20 61 6c 6c 0a 2a 2a 20 72 6f 77 73 20 6f 66 20   all.** rows of 
47788 74 68 65 20 74 61 62 6c 65 2e 20 59 20 69 73 20  the table. Y is 
47789 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
4778a 77 73 20 66 6f 72 20 77 68 69 63 68 20 63 6f 6c  ws for which col
4778b 75 6d 6e 20 69 43 6f 6c 20 63 6f 6e 74 61 69 6e  umn iCol contain
4778c 73 0a 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e  s.** at least on
4778d 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 70 68  e instance of ph
4778e 72 61 73 65 20 69 50 68 72 61 73 65 2e 0a 2a 2a  rase iPhrase..**
4778f 0a 2a 2a 20 49 66 20 74 68 65 20 70 68 72 61 73  .** If the phras
47790 65 20 70 45 78 70 72 20 63 6f 6e 73 69 73 74 73  e pExpr consists
47791 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 64 65 66   entirely of def
47792 65 72 72 65 64 20 74 6f 6b 65 6e 73 2c 20 74 68  erred tokens, th
47793 65 6e 20 61 6c 6c 20 58 20 61 6e 64 0a 2a 2a 20  en all X and.** 
47794 59 20 76 61 6c 75 65 73 20 61 72 65 20 73 65 74  Y values are set
47795 20 74 6f 20 6e 44 6f 63 2c 20 77 68 65 72 65 20   to nDoc, where 
47796 6e 44 6f 63 20 69 73 20 74 68 65 20 6e 75 6d 62  nDoc is the numb
47797 65 72 20 6f 66 20 64 6f 63 75 6d 65 6e 74 73 20  er of documents 
47798 69 6e 20 74 68 65 20 0a 2a 2a 20 66 69 6c 65 20  in the .** file 
47799 73 79 73 74 65 6d 2e 20 54 68 69 73 20 69 73 20  system. This is 
4779a 64 6f 6e 65 20 62 65 63 61 75 73 65 20 74 68 65  done because the
4779b 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78   full-text index
4779c 20 64 6f 63 6c 69 73 74 20 69 73 20 72 65 71 75   doclist is requ
4779d 69 72 65 64 0a 2a 2a 20 74 6f 20 63 61 6c 63 75  ired.** to calcu
4779e 6c 61 74 65 20 74 68 65 73 65 20 76 61 6c 75 65  late these value
4779f 73 20 70 72 6f 70 65 72 6c 79 2c 20 61 6e 64 20  s properly, and 
477a0 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e  the full-text in
477a1 64 65 78 20 64 6f 63 6c 69 73 74 20 69 73 0a 2a  dex doclist is.*
477a2 2a 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20  * not available 
477a3 66 6f 72 20 64 65 66 65 72 72 65 64 20 74 6f 6b  for deferred tok
477a4 65 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ens..*/.static i
477a5 6e 74 20 66 74 73 33 45 78 70 72 47 6c 6f 62 61  nt fts3ExprGloba
477a6 6c 48 69 74 73 43 62 28 0a 20 20 46 74 73 33 45  lHitsCb(.  Fts3E
477a7 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20  xpr *pExpr,     
477a8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 68             /* Ph
477a9 72 61 73 65 20 65 78 70 72 65 73 73 69 6f 6e 20  rase expression 
477aa 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 50  node */.  int iP
477ab 68 72 61 73 65 2c 20 20 20 20 20 20 20 20 20 20  hrase,          
477ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 68 72            /* Phr
477ad 61 73 65 20 6e 75 6d 62 65 72 20 28 6e 75 6d 62  ase number (numb
477ae 65 72 65 64 20 66 72 6f 6d 20 7a 65 72 6f 29 20  ered from zero) 
477af 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 74 78 20  */.  void *pCtx 
477b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477b1 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
477b2 74 6f 20 4d 61 74 63 68 49 6e 66 6f 20 73 74 72  to MatchInfo str
477b3 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 4d  ucture */.){.  M
477b4 61 74 63 68 49 6e 66 6f 20 2a 70 20 3d 20 28 4d  atchInfo *p = (M
477b5 61 74 63 68 49 6e 66 6f 20 2a 29 70 43 74 78 3b  atchInfo *)pCtx;
477b6 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
477b7 33 46 74 73 33 45 76 61 6c 50 68 72 61 73 65 53  3Fts3EvalPhraseS
477b8 74 61 74 73 28 0a 20 20 20 20 20 20 70 2d 3e 70  tats(.      p->p
477b9 43 75 72 73 6f 72 2c 20 70 45 78 70 72 2c 20 26  Cursor, pExpr, &
477ba 70 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 5b 33 2a  p->aMatchinfo[3*
477bb 69 50 68 72 61 73 65 2a 70 2d 3e 6e 43 6f 6c 5d  iPhrase*p->nCol]
477bc 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 66  .  );.}../*.** f
477bd 74 73 33 45 78 70 72 49 74 65 72 61 74 65 28 29  ts3ExprIterate()
477be 20 63 61 6c 6c 62 61 63 6b 20 75 73 65 64 20 74   callback used t
477bf 6f 20 63 6f 6c 6c 65 63 74 20 74 68 65 20 22 6c  o collect the "l
477c0 6f 63 61 6c 22 20 70 61 72 74 20 6f 66 20 74 68  ocal" part of th
477c1 65 0a 2a 2a 20 46 54 53 33 5f 4d 41 54 43 48 49  e.** FTS3_MATCHI
477c2 4e 46 4f 5f 48 49 54 53 20 61 72 72 61 79 2e 20  NFO_HITS array. 
477c3 54 68 65 20 6c 6f 63 61 6c 20 73 74 61 74 73 20  The local stats 
477c4 61 72 65 20 74 68 6f 73 65 20 65 6c 65 6d 65 6e  are those elemen
477c5 74 73 20 6f 66 20 74 68 65 20 0a 2a 2a 20 61 72  ts of the .** ar
477c6 72 61 79 20 74 68 61 74 20 61 72 65 20 64 69 66  ray that are dif
477c7 66 65 72 65 6e 74 20 66 6f 72 20 65 61 63 68 20  ferent for each 
477c8 72 6f 77 20 72 65 74 75 72 6e 65 64 20 62 79 20  row returned by 
477c9 74 68 65 20 71 75 65 72 79 2e 0a 2a 2f 0a 73 74  the query..*/.st
477ca 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 78 70  atic int fts3Exp
477cb 72 4c 6f 63 61 6c 48 69 74 73 43 62 28 0a 20 20  rLocalHitsCb(.  
477cc 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 2c  Fts3Expr *pExpr,
477cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477ce 2f 2a 20 50 68 72 61 73 65 20 65 78 70 72 65 73  /* Phrase expres
477cf 73 69 6f 6e 20 6e 6f 64 65 20 2a 2f 0a 20 20 69  sion node */.  i
477d0 6e 74 20 69 50 68 72 61 73 65 2c 20 20 20 20 20  nt iPhrase,     
477d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
477d2 2a 20 50 68 72 61 73 65 20 6e 75 6d 62 65 72 20  * Phrase number 
477d3 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 74 78 20  */.  void *pCtx 
477d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477d5 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
477d6 74 6f 20 4d 61 74 63 68 49 6e 66 6f 20 73 74 72  to MatchInfo str
477d7 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  ucture */.){.  i
477d8 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
477d9 4b 3b 0a 20 20 4d 61 74 63 68 49 6e 66 6f 20 2a  K;.  MatchInfo *
477da 70 20 3d 20 28 4d 61 74 63 68 49 6e 66 6f 20 2a  p = (MatchInfo *
477db 29 70 43 74 78 3b 0a 20 20 69 6e 74 20 69 53 74  )pCtx;.  int iSt
477dc 61 72 74 20 3d 20 69 50 68 72 61 73 65 20 2a 20  art = iPhrase * 
477dd 70 2d 3e 6e 43 6f 6c 20 2a 20 33 3b 0a 20 20 69  p->nCol * 3;.  i
477de 6e 74 20 69 3b 0a 0a 20 20 66 6f 72 28 69 3d 30  nt i;..  for(i=0
477df 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 20 26 26 20 72  ; i<p->nCol && r
477e0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 2b  c==SQLITE_OK; i+
477e1 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 70 43  +){.    char *pC
477e2 73 72 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  sr;.    rc = sql
477e3 69 74 65 33 46 74 73 33 45 76 61 6c 50 68 72 61  ite3Fts3EvalPhra
477e4 73 65 50 6f 73 6c 69 73 74 28 70 2d 3e 70 43 75  sePoslist(p->pCu
477e5 72 73 6f 72 2c 20 70 45 78 70 72 2c 20 69 2c 20  rsor, pExpr, i, 
477e6 26 70 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20  &pCsr);.    if( 
477e7 70 43 73 72 20 29 7b 0a 20 20 20 20 20 20 70 2d  pCsr ){.      p-
477e8 3e 61 4d 61 74 63 68 69 6e 66 6f 5b 69 53 74 61  >aMatchinfo[iSta
477e9 72 74 2b 69 2a 33 5d 20 3d 20 66 74 73 33 43 6f  rt+i*3] = fts3Co
477ea 6c 75 6d 6e 6c 69 73 74 43 6f 75 6e 74 28 26 70  lumnlistCount(&p
477eb 43 73 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Csr);.    }else{
477ec 0a 20 20 20 20 20 20 70 2d 3e 61 4d 61 74 63 68  .      p->aMatch
477ed 69 6e 66 6f 5b 69 53 74 61 72 74 2b 69 2a 33 5d  info[iStart+i*3]
477ee 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
477ef 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
477f0 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
477f1 4d 61 74 63 68 69 6e 66 6f 43 68 65 63 6b 28 0a  MatchinfoCheck(.
477f2 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61    Fts3Table *pTa
477f3 62 2c 20 0a 20 20 63 68 61 72 20 63 41 72 67 2c  b, .  char cArg,
477f4 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a  .  char **pzErr.
477f5 29 7b 0a 20 20 69 66 28 20 28 63 41 72 67 3d 3d  ){.  if( (cArg==
477f6 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4e  FTS3_MATCHINFO_N
477f7 50 48 52 41 53 45 29 0a 20 20 20 7c 7c 20 28 63  PHRASE).   || (c
477f8 41 72 67 3d 3d 46 54 53 33 5f 4d 41 54 43 48 49  Arg==FTS3_MATCHI
477f9 4e 46 4f 5f 4e 43 4f 4c 29 0a 20 20 20 7c 7c 20  NFO_NCOL).   || 
477fa 28 63 41 72 67 3d 3d 46 54 53 33 5f 4d 41 54 43  (cArg==FTS3_MATC
477fb 48 49 4e 46 4f 5f 4e 44 4f 43 20 26 26 20 70 54  HINFO_NDOC && pT
477fc 61 62 2d 3e 62 46 74 73 34 29 0a 20 20 20 7c 7c  ab->bFts4).   ||
477fd 20 28 63 41 72 67 3d 3d 46 54 53 33 5f 4d 41 54   (cArg==FTS3_MAT
477fe 43 48 49 4e 46 4f 5f 41 56 47 4c 45 4e 47 54 48  CHINFO_AVGLENGTH
477ff 20 26 26 20 70 54 61 62 2d 3e 62 46 74 73 34 29   && pTab->bFts4)
47800 0a 20 20 20 7c 7c 20 28 63 41 72 67 3d 3d 46 54  .   || (cArg==FT
47801 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4c 45 4e  S3_MATCHINFO_LEN
47802 47 54 48 20 26 26 20 70 54 61 62 2d 3e 62 48 61  GTH && pTab->bHa
47803 73 44 6f 63 73 69 7a 65 29 0a 20 20 20 7c 7c 20  sDocsize).   || 
47804 28 63 41 72 67 3d 3d 46 54 53 33 5f 4d 41 54 43  (cArg==FTS3_MATC
47805 48 49 4e 46 4f 5f 4c 43 53 29 0a 20 20 20 7c 7c  HINFO_LCS).   ||
47806 20 28 63 41 72 67 3d 3d 46 54 53 33 5f 4d 41 54   (cArg==FTS3_MAT
47807 43 48 49 4e 46 4f 5f 48 49 54 53 29 0a 20 20 29  CHINFO_HITS).  )
47808 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
47809 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 2a 70  ITE_OK;.  }.  *p
4780a 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zErr = sqlite3_m
4780b 70 72 69 6e 74 66 28 22 75 6e 72 65 63 6f 67 6e  printf("unrecogn
4780c 69 7a 65 64 20 6d 61 74 63 68 69 6e 66 6f 20 72  ized matchinfo r
4780d 65 71 75 65 73 74 3a 20 25 63 22 2c 20 63 41 72  equest: %c", cAr
4780e 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  g);.  return SQL
4780f 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 73 74  ITE_ERROR;.}..st
47810 61 74 69 63 20 69 6e 74 20 66 74 73 33 4d 61 74  atic int fts3Mat
47811 63 68 69 6e 66 6f 53 69 7a 65 28 4d 61 74 63 68  chinfoSize(Match
47812 49 6e 66 6f 20 2a 70 49 6e 66 6f 2c 20 63 68 61  Info *pInfo, cha
47813 72 20 63 41 72 67 29 7b 0a 20 20 69 6e 74 20 6e  r cArg){.  int n
47814 56 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Val;            
47815 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
47816 6d 62 65 72 20 6f 66 20 69 6e 74 65 67 65 72 73  mber of integers
47817 20 6f 75 74 70 75 74 20 62 79 20 63 41 72 67 20   output by cArg 
47818 2a 2f 0a 0a 20 20 73 77 69 74 63 68 28 20 63 41  */..  switch( cA
47819 72 67 20 29 7b 0a 20 20 20 20 63 61 73 65 20 46  rg ){.    case F
4781a 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4e 44  TS3_MATCHINFO_ND
4781b 4f 43 3a 0a 20 20 20 20 63 61 73 65 20 46 54 53  OC:.    case FTS
4781c 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4e 50 48 52  3_MATCHINFO_NPHR
4781d 41 53 45 3a 20 0a 20 20 20 20 63 61 73 65 20 46  ASE: .    case F
4781e 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4e 43  TS3_MATCHINFO_NC
4781f 4f 4c 3a 20 0a 20 20 20 20 20 20 6e 56 61 6c 20  OL: .      nVal 
47820 3d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  = 1;.      break
47821 3b 0a 0a 20 20 20 20 63 61 73 65 20 46 54 53 33  ;..    case FTS3
47822 5f 4d 41 54 43 48 49 4e 46 4f 5f 41 56 47 4c 45  _MATCHINFO_AVGLE
47823 4e 47 54 48 3a 0a 20 20 20 20 63 61 73 65 20 46  NGTH:.    case F
47824 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4c 45  TS3_MATCHINFO_LE
47825 4e 47 54 48 3a 0a 20 20 20 20 63 61 73 65 20 46  NGTH:.    case F
47826 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4c 43  TS3_MATCHINFO_LC
47827 53 3a 0a 20 20 20 20 20 20 6e 56 61 6c 20 3d 20  S:.      nVal = 
47828 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 3b 0a 20 20 20  pInfo->nCol;.   
47829 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64     break;..    d
4782a 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73  efault:.      as
4782b 73 65 72 74 28 20 63 41 72 67 3d 3d 46 54 53 33  sert( cArg==FTS3
4782c 5f 4d 41 54 43 48 49 4e 46 4f 5f 48 49 54 53 20  _MATCHINFO_HITS 
4782d 29 3b 0a 20 20 20 20 20 20 6e 56 61 6c 20 3d 20  );.      nVal = 
4782e 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 20 2a 20 70 49  pInfo->nCol * pI
4782f 6e 66 6f 2d 3e 6e 50 68 72 61 73 65 20 2a 20 33  nfo->nPhrase * 3
47830 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
47831 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 6e 56 61   }..  return nVa
47832 6c 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  l;.}..static int
47833 20 66 74 73 33 4d 61 74 63 68 69 6e 66 6f 53 65   fts3MatchinfoSe
47834 6c 65 63 74 44 6f 63 74 6f 74 61 6c 28 0a 20 20  lectDoctotal(.  
47835 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61 62 2c  Fts3Table *pTab,
47836 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
47837 2a 2a 70 70 53 74 6d 74 2c 0a 20 20 73 71 6c 69  **ppStmt,.  sqli
47838 74 65 33 5f 69 6e 74 36 34 20 2a 70 6e 44 6f 63  te3_int64 *pnDoc
47839 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
4783a 2a 70 61 4c 65 6e 0a 29 7b 0a 20 20 73 71 6c 69  *paLen.){.  sqli
4783b 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
4783c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61  .  const char *a
4783d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ;.  sqlite3_int6
4783e 34 20 6e 44 6f 63 3b 0a 0a 20 20 69 66 28 20 21  4 nDoc;..  if( !
4783f 2a 70 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 69  *ppStmt ){.    i
47840 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46  nt rc = sqlite3F
47841 74 73 33 53 65 6c 65 63 74 44 6f 63 74 6f 74 61  ts3SelectDoctota
47842 6c 28 70 54 61 62 2c 20 70 70 53 74 6d 74 29 3b  l(pTab, ppStmt);
47843 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
47844 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
47845 72 63 3b 0a 20 20 7d 0a 20 20 70 53 74 6d 74 20  rc;.  }.  pStmt 
47846 3d 20 2a 70 70 53 74 6d 74 3b 0a 20 20 61 73 73  = *ppStmt;.  ass
47847 65 72 74 28 20 73 71 6c 69 74 65 33 5f 64 61 74  ert( sqlite3_dat
47848 61 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3d 3d  a_count(pStmt)==
47849 31 20 29 3b 0a 0a 20 20 61 20 3d 20 73 71 6c 69  1 );..  a = sqli
4784a 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
4784b 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 61 20 2b  pStmt, 0);.  a +
4784c 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  = sqlite3Fts3Get
4784d 56 61 72 69 6e 74 28 61 2c 20 26 6e 44 6f 63 29  Varint(a, &nDoc)
4784e 3b 0a 20 20 69 66 28 20 6e 44 6f 63 3d 3d 30 20  ;.  if( nDoc==0 
4784f 29 20 72 65 74 75 72 6e 20 46 54 53 5f 43 4f 52  ) return FTS_COR
47850 52 55 50 54 5f 56 54 41 42 3b 0a 20 20 2a 70 6e  RUPT_VTAB;.  *pn
47851 44 6f 63 20 3d 20 28 75 33 32 29 6e 44 6f 63 3b  Doc = (u32)nDoc;
47852 0a 0a 20 20 69 66 28 20 70 61 4c 65 6e 20 29 20  ..  if( paLen ) 
47853 2a 70 61 4c 65 6e 20 3d 20 61 3b 0a 20 20 72 65  *paLen = a;.  re
47854 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
47855 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  }../*.** An inst
47856 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
47857 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
47858 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  is used to store
47859 20 73 74 61 74 65 20 77 68 69 6c 65 20 0a 2a 2a   state while .**
4785a 20 69 74 65 72 61 74 69 6e 67 20 74 68 72 6f 75   iterating throu
4785b 67 68 20 61 20 6d 75 6c 74 69 2d 63 6f 6c 75 6d  gh a multi-colum
4785c 6e 20 70 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 20  n position-list 
4785d 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
4785e 20 74 68 65 0a 2a 2a 20 68 69 74 73 20 66 6f 72   the.** hits for
4785f 20 61 20 73 69 6e 67 6c 65 20 70 68 72 61 73 65   a single phrase
47860 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 72 6f 77   on a single row
47861 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 61 6c   in order to cal
47862 63 75 6c 61 74 65 20 74 68 65 0a 2a 2a 20 76 61  culate the.** va
47863 6c 75 65 73 20 66 6f 72 20 61 20 6d 61 74 63 68  lues for a match
47864 69 6e 66 6f 28 29 20 46 54 53 33 5f 4d 41 54 43  info() FTS3_MATC
47865 48 49 4e 46 4f 5f 4c 43 53 20 72 65 71 75 65 73  HINFO_LCS reques
47866 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  t..*/.typedef st
47867 72 75 63 74 20 4c 63 73 49 74 65 72 61 74 6f 72  ruct LcsIterator
47868 20 4c 63 73 49 74 65 72 61 74 6f 72 3b 0a 73 74   LcsIterator;.st
47869 72 75 63 74 20 4c 63 73 49 74 65 72 61 74 6f 72  ruct LcsIterator
4786a 20 7b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70   {.  Fts3Expr *p
4786b 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20  Expr;           
4786c 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
4786d 74 6f 20 70 68 72 61 73 65 20 65 78 70 72 65 73  to phrase expres
4786e 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 50  sion */.  int iP
4786f 6f 73 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20  osOffset;       
47870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b            /* Tok
47871 65 6e 73 20 63 6f 75 6e 74 20 75 70 20 74 6f 20  ens count up to 
47872 65 6e 64 20 6f 66 20 74 68 69 73 20 70 68 72 61  end of this phra
47873 73 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 52  se */.  char *pR
47874 65 61 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ead;            
47875 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
47876 72 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 74  r used to iterat
47877 65 20 74 68 72 6f 75 67 68 20 61 44 6f 63 6c 69  e through aDocli
47878 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 50 6f 73  st */.  int iPos
47879 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4787a 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
4787b 6e 74 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 7d  nt position */.}
4787c 3b 0a 0a 2f 2a 20 0a 2a 2a 20 49 66 20 4c 63 73  ;../* .** If Lcs
4787d 49 74 65 72 61 74 6f 72 2e 69 43 6f 6c 20 69 73  Iterator.iCol is
4787e 20 73 65 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c   set to the foll
4787f 6f 77 69 6e 67 20 76 61 6c 75 65 2c 20 74 68 65  owing value, the
47880 20 69 74 65 72 61 74 6f 72 20 68 61 73 0a 2a 2a   iterator has.**
47881 20 66 69 6e 69 73 68 65 64 20 69 74 65 72 61 74   finished iterat
47882 69 6e 67 20 74 68 72 6f 75 67 68 20 61 6c 6c 20  ing through all 
47883 6f 66 66 73 65 74 73 20 66 6f 72 20 61 6c 6c 20  offsets for all 
47884 63 6f 6c 75 6d 6e 73 2e 0a 2a 2f 0a 23 64 65 66  columns..*/.#def
47885 69 6e 65 20 4c 43 53 5f 49 54 45 52 41 54 4f 52  ine LCS_ITERATOR
47886 5f 46 49 4e 49 53 48 45 44 20 30 78 37 46 46 46  _FINISHED 0x7FFF
47887 46 46 46 46 3b 0a 0a 73 74 61 74 69 63 20 69 6e  FFFF;..static in
47888 74 20 66 74 73 33 4d 61 74 63 68 69 6e 66 6f 4c  t fts3MatchinfoL
47889 63 73 43 62 28 0a 20 20 46 74 73 33 45 78 70 72  csCb(.  Fts3Expr
4788a 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20   *pExpr,        
4788b 20 20 20 20 20 20 20 20 2f 2a 20 50 68 72 61 73          /* Phras
4788c 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64  e expression nod
4788d 65 20 2a 2f 0a 20 20 69 6e 74 20 69 50 68 72 61  e */.  int iPhra
4788e 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
4788f 20 20 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65         /* Phrase
47890 20 6e 75 6d 62 65 72 20 28 6e 75 6d 62 65 72 65   number (numbere
47891 64 20 66 72 6f 6d 20 7a 65 72 6f 29 20 2a 2f 0a  d from zero) */.
47892 20 20 76 6f 69 64 20 2a 70 43 74 78 20 20 20 20    void *pCtx    
47893 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47894 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
47895 4d 61 74 63 68 49 6e 66 6f 20 73 74 72 75 63 74  MatchInfo struct
47896 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 4c 63 73 49  ure */.){.  LcsI
47897 74 65 72 61 74 6f 72 20 2a 61 49 74 65 72 20 3d  terator *aIter =
47898 20 28 4c 63 73 49 74 65 72 61 74 6f 72 20 2a 29   (LcsIterator *)
47899 70 43 74 78 3b 0a 20 20 61 49 74 65 72 5b 69 50  pCtx;.  aIter[iP
4789a 68 72 61 73 65 5d 2e 70 45 78 70 72 20 3d 20 70  hrase].pExpr = p
4789b 45 78 70 72 3b 0a 20 20 72 65 74 75 72 6e 20 53  Expr;.  return S
4789c 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
4789d 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 65 20 69  ** Advance the i
4789e 74 65 72 61 74 6f 72 20 70 61 73 73 65 64 20 61  terator passed a
4789f 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f  s an argument to
478a0 20 74 68 65 20 6e 65 78 74 20 70 6f 73 69 74 69   the next positi
478a1 6f 6e 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 31 20  on. Return.** 1 
478a2 69 66 20 74 68 65 20 69 74 65 72 61 74 6f 72 20  if the iterator 
478a3 69 73 20 61 74 20 45 4f 46 20 6f 72 20 69 66 20  is at EOF or if 
478a4 69 74 20 6e 6f 77 20 70 6f 69 6e 74 73 20 74 6f  it now points to
478a5 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
478a6 65 0a 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 6c 69  e.** position li
478a7 73 74 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20  st for the next 
478a8 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69  column..*/.stati
478a9 63 20 69 6e 74 20 66 74 73 33 4c 63 73 49 74 65  c int fts3LcsIte
478aa 72 61 74 6f 72 41 64 76 61 6e 63 65 28 4c 63 73  ratorAdvance(Lcs
478ab 49 74 65 72 61 74 6f 72 20 2a 70 49 74 65 72 29  Iterator *pIter)
478ac 7b 0a 20 20 63 68 61 72 20 2a 70 52 65 61 64 20  {.  char *pRead 
478ad 3d 20 70 49 74 65 72 2d 3e 70 52 65 61 64 3b 0a  = pIter->pRead;.
478ae 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
478af 69 52 65 61 64 3b 0a 20 20 69 6e 74 20 72 63 20  iRead;.  int rc 
478b0 3d 20 30 3b 0a 0a 20 20 70 52 65 61 64 20 2b 3d  = 0;..  pRead +=
478b1 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56   sqlite3Fts3GetV
478b2 61 72 69 6e 74 28 70 52 65 61 64 2c 20 26 69 52  arint(pRead, &iR
478b3 65 61 64 29 3b 0a 20 20 69 66 28 20 69 52 65 61  ead);.  if( iRea
478b4 64 3d 3d 30 20 7c 7c 20 69 52 65 61 64 3d 3d 31  d==0 || iRead==1
478b5 20 29 7b 0a 20 20 20 20 70 52 65 61 64 20 3d 20   ){.    pRead = 
478b6 30 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20  0;.    rc = 1;. 
478b7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 74 65   }else{.    pIte
478b8 72 2d 3e 69 50 6f 73 20 2b 3d 20 28 69 6e 74 29  r->iPos += (int)
478b9 28 69 52 65 61 64 2d 32 29 3b 0a 20 20 7d 0a 0a  (iRead-2);.  }..
478ba 20 20 70 49 74 65 72 2d 3e 70 52 65 61 64 20 3d    pIter->pRead =
478bb 20 70 52 65 61 64 3b 0a 20 20 72 65 74 75 72 6e   pRead;.  return
478bc 20 72 63 3b 0a 7d 0a 20 20 0a 2f 2a 0a 2a 2a 20   rc;.}.  ./*.** 
478bd 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d  This function im
478be 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 46 54 53  plements the FTS
478bf 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4c 43 53 20  3_MATCHINFO_LCS 
478c0 6d 61 74 63 68 69 6e 66 6f 28 29 20 66 6c 61 67  matchinfo() flag
478c1 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  . .**.** If the 
478c2 63 61 6c 6c 20 69 73 20 73 75 63 63 65 73 73 66  call is successf
478c3 75 6c 2c 20 74 68 65 20 6c 6f 6e 67 65 73 74 2d  ul, the longest-
478c4 63 6f 6d 6d 6f 6e 2d 73 75 62 73 74 72 69 6e 67  common-substring
478c5 20 6c 65 6e 67 74 68 73 20 66 6f 72 20 65 61 63   lengths for eac
478c6 68 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 61 72 65 20  h.** column are 
478c7 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
478c8 20 66 69 72 73 74 20 6e 43 6f 6c 20 65 6c 65 6d   first nCol elem
478c9 65 6e 74 73 20 6f 66 20 74 68 65 20 70 49 6e 66  ents of the pInf
478ca 6f 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 5b 5d 20  o->aMatchinfo[] 
478cb 0a 2a 2a 20 61 72 72 61 79 20 62 65 66 6f 72 65  .** array before
478cc 20 72 65 74 75 72 6e 69 6e 67 2e 20 53 51 4c 49   returning. SQLI
478cd 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
478ce 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  d in this case..
478cf 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
478d0 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   if an error occ
478d1 75 72 73 2c 20 61 6e 20 53 51 4c 69 74 65 20 65  urs, an SQLite e
478d2 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
478d3 75 72 6e 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a  urned and the.**
478d4 20 64 61 74 61 20 77 72 69 74 74 65 6e 20 74 6f   data written to
478d5 20 74 68 65 20 66 69 72 73 74 20 6e 43 6f 6c 20   the first nCol 
478d6 65 6c 65 6d 65 6e 74 73 20 6f 66 20 70 49 6e 66  elements of pInf
478d7 6f 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 5b 5d 20  o->aMatchinfo[] 
478d8 69 73 20 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64  is .** undefined
478d9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
478da 66 74 73 33 4d 61 74 63 68 69 6e 66 6f 4c 63 73  fts3MatchinfoLcs
478db 28 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73  (Fts3Cursor *pCs
478dc 72 2c 20 4d 61 74 63 68 49 6e 66 6f 20 2a 70 49  r, MatchInfo *pI
478dd 6e 66 6f 29 7b 0a 20 20 4c 63 73 49 74 65 72 61  nfo){.  LcsItera
478de 74 6f 72 20 2a 61 49 74 65 72 3b 0a 20 20 69 6e  tor *aIter;.  in
478df 74 20 69 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b  t i;.  int iCol;
478e0 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20  .  int nToken = 
478e1 30 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  0;..  /* Allocat
478e2 65 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 74  e and populate t
478e3 68 65 20 61 72 72 61 79 20 6f 66 20 4c 63 73 49  he array of LcsI
478e4 74 65 72 61 74 6f 72 20 6f 62 6a 65 63 74 73 2e  terator objects.
478e5 20 54 68 65 20 61 72 72 61 79 0a 20 20 2a 2a 20   The array.  ** 
478e6 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 65 6c 65  contains one ele
478e7 6d 65 6e 74 20 66 6f 72 20 65 61 63 68 20 6d 61  ment for each ma
478e8 74 63 68 61 62 6c 65 20 70 68 72 61 73 65 20 69  tchable phrase i
478e9 6e 20 74 68 65 20 71 75 65 72 79 2e 0a 20 20 2a  n the query..  *
478ea 2a 2f 0a 20 20 61 49 74 65 72 20 3d 20 73 71 6c  */.  aIter = sql
478eb 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
478ec 6f 66 28 4c 63 73 49 74 65 72 61 74 6f 72 29 20  of(LcsIterator) 
478ed 2a 20 70 43 73 72 2d 3e 6e 50 68 72 61 73 65 29  * pCsr->nPhrase)
478ee 3b 0a 20 20 69 66 28 20 21 61 49 74 65 72 20 29  ;.  if( !aIter )
478ef 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
478f0 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65 74 28 61  OMEM;.  memset(a
478f1 49 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Iter, 0, sizeof(
478f2 4c 63 73 49 74 65 72 61 74 6f 72 29 20 2a 20 70  LcsIterator) * p
478f3 43 73 72 2d 3e 6e 50 68 72 61 73 65 29 3b 0a 20  Csr->nPhrase);. 
478f4 20 28 76 6f 69 64 29 66 74 73 33 45 78 70 72 49   (void)fts3ExprI
478f5 74 65 72 61 74 65 28 70 43 73 72 2d 3e 70 45 78  terate(pCsr->pEx
478f6 70 72 2c 20 66 74 73 33 4d 61 74 63 68 69 6e 66  pr, fts3Matchinf
478f7 6f 4c 63 73 43 62 2c 20 28 76 6f 69 64 2a 29 61  oLcsCb, (void*)a
478f8 49 74 65 72 29 3b 0a 0a 20 20 66 6f 72 28 69 3d  Iter);..  for(i=
478f9 30 3b 20 69 3c 70 49 6e 66 6f 2d 3e 6e 50 68 72  0; i<pInfo->nPhr
478fa 61 73 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4c  ase; i++){.    L
478fb 63 73 49 74 65 72 61 74 6f 72 20 2a 70 49 74 65  csIterator *pIte
478fc 72 20 3d 20 26 61 49 74 65 72 5b 69 5d 3b 0a 20  r = &aIter[i];. 
478fd 20 20 20 6e 54 6f 6b 65 6e 20 2d 3d 20 70 49 74     nToken -= pIt
478fe 65 72 2d 3e 70 45 78 70 72 2d 3e 70 50 68 72 61  er->pExpr->pPhra
478ff 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20  se->nToken;.    
47900 70 49 74 65 72 2d 3e 69 50 6f 73 4f 66 66 73 65  pIter->iPosOffse
47901 74 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20 7d 0a  t = nToken;.  }.
47902 0a 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69  .  for(iCol=0; i
47903 43 6f 6c 3c 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 3b  Col<pInfo->nCol;
47904 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 69 6e   iCol++){.    in
47905 74 20 6e 4c 63 73 20 3d 20 30 3b 20 20 20 20 20  t nLcs = 0;     
47906 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
47907 43 53 20 76 61 6c 75 65 20 66 6f 72 20 74 68 69  CS value for thi
47908 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20  s column */.    
47909 69 6e 74 20 6e 4c 69 76 65 20 3d 20 30 3b 20 20  int nLive = 0;  
4790a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4790b 20 4e 75 6d 62 65 72 20 6f 66 20 69 74 65 72 61   Number of itera
4790c 74 6f 72 73 20 69 6e 20 61 49 74 65 72 20 6e 6f  tors in aIter no
4790d 74 20 61 74 20 45 4f 46 20 2a 2f 0a 0a 20 20 20  t at EOF */..   
4790e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 66   for(i=0; i<pInf
4790f 6f 2d 3e 6e 50 68 72 61 73 65 3b 20 69 2b 2b 29  o->nPhrase; i++)
47910 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a  {.      int rc;.
47911 20 20 20 20 20 20 4c 63 73 49 74 65 72 61 74 6f        LcsIterato
47912 72 20 2a 70 49 74 20 3d 20 26 61 49 74 65 72 5b  r *pIt = &aIter[
47913 69 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  i];.      rc = s
47914 71 6c 69 74 65 33 46 74 73 33 45 76 61 6c 50 68  qlite3Fts3EvalPh
47915 72 61 73 65 50 6f 73 6c 69 73 74 28 70 43 73 72  rasePoslist(pCsr
47916 2c 20 70 49 74 2d 3e 70 45 78 70 72 2c 20 69 43  , pIt->pExpr, iC
47917 6f 6c 2c 20 26 70 49 74 2d 3e 70 52 65 61 64 29  ol, &pIt->pRead)
47918 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
47919 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
4791a 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 69 66 28  rn rc;.      if(
4791b 20 70 49 74 2d 3e 70 52 65 61 64 20 29 7b 0a 20   pIt->pRead ){. 
4791c 20 20 20 20 20 20 20 70 49 74 2d 3e 69 50 6f 73         pIt->iPos
4791d 20 3d 20 70 49 74 2d 3e 69 50 6f 73 4f 66 66 73   = pIt->iPosOffs
4791e 65 74 3b 0a 20 20 20 20 20 20 20 20 66 74 73 33  et;.        fts3
4791f 4c 63 73 49 74 65 72 61 74 6f 72 41 64 76 61 6e  LcsIteratorAdvan
47920 63 65 28 26 61 49 74 65 72 5b 69 5d 29 3b 0a 20  ce(&aIter[i]);. 
47921 20 20 20 20 20 20 20 6e 4c 69 76 65 2b 2b 3b 0a         nLive++;.
47922 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
47923 20 20 20 77 68 69 6c 65 28 20 6e 4c 69 76 65 3e     while( nLive>
47924 30 20 29 7b 0a 20 20 20 20 20 20 4c 63 73 49 74  0 ){.      LcsIt
47925 65 72 61 74 6f 72 20 2a 70 41 64 76 20 3d 20 30  erator *pAdv = 0
47926 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 74  ;      /* The it
47927 65 72 61 74 6f 72 20 74 6f 20 61 64 76 61 6e 63  erator to advanc
47928 65 20 62 79 20 6f 6e 65 20 70 6f 73 69 74 69 6f  e by one positio
47929 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  n */.      int n
4792a 54 68 69 73 4c 63 73 20 3d 20 30 3b 20 20 20 20  ThisLcs = 0;    
4792b 20 20 20 20 20 20 20 2f 2a 20 4c 43 53 20 66 6f         /* LCS fo
4792c 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 69 74  r the current it
4792d 65 72 61 74 6f 72 20 70 6f 73 69 74 69 6f 6e 73  erator positions
4792e 20 2a 2f 0a 0a 20 20 20 20 20 20 66 6f 72 28 69   */..      for(i
4792f 3d 30 3b 20 69 3c 70 49 6e 66 6f 2d 3e 6e 50 68  =0; i<pInfo->nPh
47930 72 61 73 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  rase; i++){.    
47931 20 20 20 20 4c 63 73 49 74 65 72 61 74 6f 72 20      LcsIterator 
47932 2a 70 49 74 65 72 20 3d 20 26 61 49 74 65 72 5b  *pIter = &aIter[
47933 69 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  i];.        if( 
47934 70 49 74 65 72 2d 3e 70 52 65 61 64 3d 3d 30 20  pIter->pRead==0 
47935 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
47936 54 68 69 73 20 69 74 65 72 61 74 6f 72 20 69 73  This iterator is
47937 20 61 6c 72 65 61 64 79 20 61 74 20 45 4f 46 20   already at EOF 
47938 66 6f 72 20 74 68 69 73 20 63 6f 6c 75 6d 6e 2e  for this column.
47939 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6e 54   */.          nT
4793a 68 69 73 4c 63 73 20 3d 20 30 3b 0a 20 20 20 20  hisLcs = 0;.    
4793b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
4793c 20 20 20 20 20 69 66 28 20 70 41 64 76 3d 3d 30       if( pAdv==0
4793d 20 7c 7c 20 70 49 74 65 72 2d 3e 69 50 6f 73 3c   || pIter->iPos<
4793e 70 41 64 76 2d 3e 69 50 6f 73 20 29 7b 0a 20 20  pAdv->iPos ){.  
4793f 20 20 20 20 20 20 20 20 20 20 70 41 64 76 20 3d            pAdv =
47940 20 70 49 74 65 72 3b 0a 20 20 20 20 20 20 20 20   pIter;.        
47941 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66    }.          if
47942 28 20 6e 54 68 69 73 4c 63 73 3d 3d 30 20 7c 7c  ( nThisLcs==0 ||
47943 20 70 49 74 65 72 2d 3e 69 50 6f 73 3d 3d 70 49   pIter->iPos==pI
47944 74 65 72 5b 2d 31 5d 2e 69 50 6f 73 20 29 7b 0a  ter[-1].iPos ){.
47945 20 20 20 20 20 20 20 20 20 20 20 20 6e 54 68 69              nThi
47946 73 4c 63 73 2b 2b 3b 0a 20 20 20 20 20 20 20 20  sLcs++;.        
47947 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
47948 20 20 20 20 20 6e 54 68 69 73 4c 63 73 20 3d 20       nThisLcs = 
47949 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  1;.          }. 
4794a 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 54 68           if( nTh
4794b 69 73 4c 63 73 3e 6e 4c 63 73 20 29 20 6e 4c 63  isLcs>nLcs ) nLc
4794c 73 20 3d 20 6e 54 68 69 73 4c 63 73 3b 0a 20 20  s = nThisLcs;.  
4794d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
4794e 20 20 20 20 20 20 69 66 28 20 66 74 73 33 4c 63        if( fts3Lc
4794f 73 49 74 65 72 61 74 6f 72 41 64 76 61 6e 63 65  sIteratorAdvance
47950 28 70 41 64 76 29 20 29 20 6e 4c 69 76 65 2d 2d  (pAdv) ) nLive--
47951 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 49 6e  ;.    }..    pIn
47952 66 6f 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 5b 69  fo->aMatchinfo[i
47953 43 6f 6c 5d 20 3d 20 6e 4c 63 73 3b 0a 20 20 7d  Col] = nLcs;.  }
47954 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
47955 28 61 49 74 65 72 29 3b 0a 20 20 72 65 74 75 72  (aIter);.  retur
47956 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
47957 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74  /*.** Populate t
47958 68 65 20 62 75 66 66 65 72 20 70 49 6e 66 6f 2d  he buffer pInfo-
47959 3e 61 4d 61 74 63 68 69 6e 66 6f 5b 5d 20 77 69  >aMatchinfo[] wi
4795a 74 68 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69  th an array of i
4795b 6e 74 65 67 65 72 73 20 74 6f 0a 2a 2a 20 62 65  ntegers to.** be
4795c 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
4795d 20 6d 61 74 63 68 69 6e 66 6f 28 29 20 66 75 6e   matchinfo() fun
4795e 63 74 69 6f 6e 2e 20 41 72 67 75 6d 65 6e 74 20  ction. Argument 
4795f 7a 41 72 67 20 63 6f 6e 74 61 69 6e 73 20 74 68  zArg contains th
47960 65 20 0a 2a 2a 20 66 6f 72 6d 61 74 20 73 74 72  e .** format str
47961 69 6e 67 20 70 61 73 73 65 64 20 61 73 20 74 68  ing passed as th
47962 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
47963 74 20 74 6f 20 6d 61 74 63 68 69 6e 66 6f 20 28  t to matchinfo (
47964 6f 72 20 74 68 65 0a 2a 2a 20 64 65 66 61 75 6c  or the.** defaul
47965 74 20 76 61 6c 75 65 20 22 70 63 78 22 20 69 66  t value "pcx" if
47966 20 6e 6f 20 73 65 63 6f 6e 64 20 61 72 67 75 6d   no second argum
47967 65 6e 74 20 77 61 73 20 73 70 65 63 69 66 69 65  ent was specifie
47968 64 29 2e 20 54 68 65 20 66 6f 72 6d 61 74 0a 2a  d). The format.*
47969 2a 20 73 74 72 69 6e 67 20 68 61 73 20 61 6c 72  * string has alr
4796a 65 61 64 79 20 62 65 65 6e 20 76 61 6c 69 64 61  eady been valida
4796b 74 65 64 20 61 6e 64 20 74 68 65 20 70 49 6e 66  ted and the pInf
4796c 6f 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 5b 5d 20  o->aMatchinfo[] 
4796d 61 72 72 61 79 0a 2a 2a 20 69 73 20 67 75 61 72  array.** is guar
4796e 61 6e 74 65 65 64 20 74 6f 20 62 65 20 6c 61 72  anteed to be lar
4796f 67 65 20 65 6e 6f 75 67 68 20 66 6f 72 20 74 68  ge enough for th
47970 65 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20  e output..**.** 
47971 49 66 20 62 47 6c 6f 62 61 6c 20 69 73 20 74 72  If bGlobal is tr
47972 75 65 2c 20 74 68 65 6e 20 70 6f 70 75 6c 61 74  ue, then populat
47973 65 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66 20  e all fields of 
47974 74 68 65 20 6d 61 74 63 68 69 6e 66 6f 28 29 20  the matchinfo() 
47975 6f 75 74 70 75 74 2e 0a 2a 2a 20 49 66 20 69 74  output..** If it
47976 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20   is false, then 
47977 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 6f 73  assume that thos
47978 65 20 66 69 65 6c 64 73 20 74 68 61 74 20 64 6f  e fields that do
47979 20 6e 6f 74 20 63 68 61 6e 67 65 20 62 65 74 77   not change betw
4797a 65 65 6e 0a 2a 2a 20 72 6f 77 73 20 28 69 2e 65  een.** rows (i.e
4797b 2e 20 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f  . FTS3_MATCHINFO
4797c 5f 4e 50 48 52 41 53 45 2c 20 4e 43 4f 4c 2c 20  _NPHRASE, NCOL, 
4797d 4e 44 4f 43 2c 20 41 56 47 4c 45 4e 47 54 48 20  NDOC, AVGLENGTH 
4797e 61 6e 64 20 70 61 72 74 20 6f 66 20 48 49 54 53  and part of HITS
4797f 29 0a 2a 2a 20 68 61 76 65 20 61 6c 72 65 61 64  ).** have alread
47980 79 20 62 65 65 6e 20 70 6f 70 75 6c 61 74 65 64  y been populated
47981 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
47982 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63  QLITE_OK if succ
47983 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51  essful, or an SQ
47984 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  Lite error code 
47985 69 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a 2a 20  if an error .** 
47986 6f 63 63 75 72 73 2e 20 49 66 20 61 20 76 61 6c  occurs. If a val
47987 75 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51  ue other than SQ
47988 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
47989 6e 65 64 2c 20 74 68 65 20 73 74 61 74 65 20 74  ned, the state t
4798a 68 65 0a 2a 2a 20 70 49 6e 66 6f 2d 3e 61 4d 61  he.** pInfo->aMa
4798b 74 63 68 69 6e 66 6f 5b 5d 20 62 75 66 66 65 72  tchinfo[] buffer
4798c 20 69 73 20 6c 65 66 74 20 69 6e 20 69 73 20 75   is left in is u
4798d 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61  ndefined..*/.sta
4798e 74 69 63 20 69 6e 74 20 66 74 73 33 4d 61 74 63  tic int fts3Matc
4798f 68 69 6e 66 6f 56 61 6c 75 65 73 28 0a 20 20 46  hinfoValues(.  F
47990 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72 2c  ts3Cursor *pCsr,
47991 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
47992 2a 20 46 54 53 33 20 63 75 72 73 6f 72 20 6f 62  * FTS3 cursor ob
47993 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 62 47  ject */.  int bG
47994 6c 6f 62 61 6c 2c 20 20 20 20 20 20 20 20 20 20  lobal,          
47995 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
47996 65 20 74 6f 20 67 72 61 62 20 74 68 65 20 67 6c  e to grab the gl
47997 6f 62 61 6c 20 73 74 61 74 73 20 2a 2f 0a 20 20  obal stats */.  
47998 4d 61 74 63 68 49 6e 66 6f 20 2a 70 49 6e 66 6f  MatchInfo *pInfo
47999 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
4799a 2f 2a 20 4d 61 74 63 68 69 6e 66 6f 20 63 6f 6e  /* Matchinfo con
4799b 74 65 78 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  text object */. 
4799c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72   const char *zAr
4799d 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g               
4799e 20 2f 2a 20 4d 61 74 63 68 69 6e 66 6f 20 66 6f   /* Matchinfo fo
4799f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 29  rmat string */.)
479a0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
479a1 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b  ITE_OK;.  int i;
479a2 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 54  .  Fts3Table *pT
479a3 61 62 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20  ab = (Fts3Table 
479a4 2a 29 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74  *)pCsr->base.pVt
479a5 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  ab;.  sqlite3_st
479a6 6d 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 30 3b  mt *pSelect = 0;
479a7 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d  ..  for(i=0; rc=
479a8 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 41  =SQLITE_OK && zA
479a9 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 0a 20 20  rg[i]; i++){..  
479aa 20 20 73 77 69 74 63 68 28 20 7a 41 72 67 5b 69    switch( zArg[i
479ab 5d 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  ] ){.      case 
479ac 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4e  FTS3_MATCHINFO_N
479ad 50 48 52 41 53 45 3a 0a 20 20 20 20 20 20 20 20  PHRASE:.        
479ae 69 66 28 20 62 47 6c 6f 62 61 6c 20 29 20 70 49  if( bGlobal ) pI
479af 6e 66 6f 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 5b  nfo->aMatchinfo[
479b0 30 5d 20 3d 20 70 49 6e 66 6f 2d 3e 6e 50 68 72  0] = pInfo->nPhr
479b1 61 73 65 3b 0a 20 20 20 20 20 20 20 20 62 72 65  ase;.        bre
479b2 61 6b 3b 0a 0a 20 20 20 20 20 20 63 61 73 65 20  ak;..      case 
479b3 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4e  FTS3_MATCHINFO_N
479b4 43 4f 4c 3a 0a 20 20 20 20 20 20 20 20 69 66 28  COL:.        if(
479b5 20 62 47 6c 6f 62 61 6c 20 29 20 70 49 6e 66 6f   bGlobal ) pInfo
479b6 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 5b 30 5d 20  ->aMatchinfo[0] 
479b7 3d 20 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 3b 0a 20  = pInfo->nCol;. 
479b8 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
479b9 20 20 20 20 20 20 0a 20 20 20 20 20 20 63 61 73        .      cas
479ba 65 20 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f  e FTS3_MATCHINFO
479bb 5f 4e 44 4f 43 3a 0a 20 20 20 20 20 20 20 20 69  _NDOC:.        i
479bc 66 28 20 62 47 6c 6f 62 61 6c 20 29 7b 0a 20 20  f( bGlobal ){.  
479bd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
479be 69 6e 74 36 34 20 6e 44 6f 63 20 3d 20 30 3b 0a  int64 nDoc = 0;.
479bf 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 66            rc = f
479c0 74 73 33 4d 61 74 63 68 69 6e 66 6f 53 65 6c 65  ts3MatchinfoSele
479c1 63 74 44 6f 63 74 6f 74 61 6c 28 70 54 61 62 2c  ctDoctotal(pTab,
479c2 20 26 70 53 65 6c 65 63 74 2c 20 26 6e 44 6f 63   &pSelect, &nDoc
479c3 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
479c4 70 49 6e 66 6f 2d 3e 61 4d 61 74 63 68 69 6e 66  pInfo->aMatchinf
479c5 6f 5b 30 5d 20 3d 20 28 75 33 32 29 6e 44 6f 63  o[0] = (u32)nDoc
479c6 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
479c7 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20      break;..    
479c8 20 20 63 61 73 65 20 46 54 53 33 5f 4d 41 54 43    case FTS3_MATC
479c9 48 49 4e 46 4f 5f 41 56 47 4c 45 4e 47 54 48 3a  HINFO_AVGLENGTH:
479ca 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 47   .        if( bG
479cb 6c 6f 62 61 6c 20 29 7b 0a 20 20 20 20 20 20 20  lobal ){.       
479cc 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
479cd 20 6e 44 6f 63 3b 20 20 20 20 20 2f 2a 20 4e 75   nDoc;     /* Nu
479ce 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20  mber of rows in 
479cf 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20  table */.       
479d0 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61     const char *a
479d1 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67  ;          /* Ag
479d2 67 72 65 67 61 74 65 20 63 6f 6c 75 6d 6e 20 6c  gregate column l
479d3 65 6e 67 74 68 20 61 72 72 61 79 20 2a 2f 0a 0a  ength array */..
479d4 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 66            rc = f
479d5 74 73 33 4d 61 74 63 68 69 6e 66 6f 53 65 6c 65  ts3MatchinfoSele
479d6 63 74 44 6f 63 74 6f 74 61 6c 28 70 54 61 62 2c  ctDoctotal(pTab,
479d7 20 26 70 53 65 6c 65 63 74 2c 20 26 6e 44 6f 63   &pSelect, &nDoc
479d8 2c 20 26 61 29 3b 0a 20 20 20 20 20 20 20 20 20  , &a);.         
479d9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
479da 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
479db 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20    int iCol;.    
479dc 20 20 20 20 20 20 20 20 66 6f 72 28 69 43 6f 6c          for(iCol
479dd 3d 30 3b 20 69 43 6f 6c 3c 70 49 6e 66 6f 2d 3e  =0; iCol<pInfo->
479de 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20  nCol; iCol++){. 
479df 20 20 20 20 20 20 20 20 20 20 20 20 20 75 33 32               u32
479e0 20 69 56 61 6c 3b 0a 20 20 20 20 20 20 20 20 20   iVal;.         
479e1 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
479e2 36 34 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  64 nToken;.     
479e3 20 20 20 20 20 20 20 20 20 61 20 2b 3d 20 73 71           a += sq
479e4 6c 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69  lite3Fts3GetVari
479e5 6e 74 28 61 2c 20 26 6e 54 6f 6b 65 6e 29 3b 0a  nt(a, &nToken);.
479e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 56                iV
479e7 61 6c 20 3d 20 28 75 33 32 29 28 28 28 75 33 32  al = (u32)(((u32
479e8 29 28 6e 54 6f 6b 65 6e 26 30 78 66 66 66 66 66  )(nToken&0xfffff
479e9 66 66 66 29 2b 6e 44 6f 63 2f 32 29 2f 6e 44 6f  fff)+nDoc/2)/nDo
479ea 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  c);.            
479eb 20 20 70 49 6e 66 6f 2d 3e 61 4d 61 74 63 68 69    pInfo->aMatchi
479ec 6e 66 6f 5b 69 43 6f 6c 5d 20 3d 20 69 56 61 6c  nfo[iCol] = iVal
479ed 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
479ee 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
479ef 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72      }.        br
479f0 65 61 6b 3b 0a 0a 20 20 20 20 20 20 63 61 73 65  eak;..      case
479f1 20 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f   FTS3_MATCHINFO_
479f2 4c 45 4e 47 54 48 3a 20 7b 0a 20 20 20 20 20 20  LENGTH: {.      
479f3 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
479f4 70 53 65 6c 65 63 74 44 6f 63 73 69 7a 65 20 3d  pSelectDocsize =
479f5 20 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   0;.        rc =
479f6 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 6c 65   sqlite3Fts3Sele
479f7 63 74 44 6f 63 73 69 7a 65 28 70 54 61 62 2c 20  ctDocsize(pTab, 
479f8 70 43 73 72 2d 3e 69 50 72 65 76 49 64 2c 20 26  pCsr->iPrevId, &
479f9 70 53 65 6c 65 63 74 44 6f 63 73 69 7a 65 29 3b  pSelectDocsize);
479fa 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
479fb 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
479fc 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c          int iCol
479fd 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73  ;.          cons
479fe 74 20 63 68 61 72 20 2a 61 20 3d 20 73 71 6c 69  t char *a = sqli
479ff 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
47a00 70 53 65 6c 65 63 74 44 6f 63 73 69 7a 65 2c 20  pSelectDocsize, 
47a01 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f  0);.          fo
47a02 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70  r(iCol=0; iCol<p
47a03 49 6e 66 6f 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c  Info->nCol; iCol
47a04 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
47a05 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e   sqlite3_int64 n
47a06 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 20  Token;.         
47a07 20 20 20 61 20 2b 3d 20 73 71 6c 69 74 65 33 46     a += sqlite3F
47a08 74 73 33 47 65 74 56 61 72 69 6e 74 28 61 2c 20  ts3GetVarint(a, 
47a09 26 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20  &nToken);.      
47a0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 4d 61        pInfo->aMa
47a0b 74 63 68 69 6e 66 6f 5b 69 43 6f 6c 5d 20 3d 20  tchinfo[iCol] = 
47a0c 28 75 33 32 29 6e 54 6f 6b 65 6e 3b 0a 20 20 20  (u32)nToken;.   
47a0d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
47a0e 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
47a0f 65 33 5f 72 65 73 65 74 28 70 53 65 6c 65 63 74  e3_reset(pSelect
47a10 44 6f 63 73 69 7a 65 29 3b 0a 20 20 20 20 20 20  Docsize);.      
47a11 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
47a12 0a 0a 20 20 20 20 20 20 63 61 73 65 20 46 54 53  ..      case FTS
47a13 33 5f 4d 41 54 43 48 49 4e 46 4f 5f 4c 43 53 3a  3_MATCHINFO_LCS:
47a14 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 74  .        rc = ft
47a15 73 33 45 78 70 72 4c 6f 61 64 44 6f 63 6c 69 73  s3ExprLoadDoclis
47a16 74 73 28 70 43 73 72 2c 20 30 2c 20 30 29 3b 0a  ts(pCsr, 0, 0);.
47a17 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
47a18 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
47a19 20 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33         rc = fts3
47a1a 4d 61 74 63 68 69 6e 66 6f 4c 63 73 28 70 43 73  MatchinfoLcs(pCs
47a1b 72 2c 20 70 49 6e 66 6f 29 3b 0a 20 20 20 20 20  r, pInfo);.     
47a1c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65     }.        bre
47a1d 61 6b 3b 0a 0a 20 20 20 20 20 20 64 65 66 61 75  ak;..      defau
47a1e 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 46 74  lt: {.        Ft
47a1f 73 33 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 20  s3Expr *pExpr;. 
47a20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a         assert( z
47a21 41 72 67 5b 69 5d 3d 3d 46 54 53 33 5f 4d 41 54  Arg[i]==FTS3_MAT
47a22 43 48 49 4e 46 4f 5f 48 49 54 53 20 29 3b 0a 20  CHINFO_HITS );. 
47a23 20 20 20 20 20 20 20 70 45 78 70 72 20 3d 20 70         pExpr = p
47a24 43 73 72 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20  Csr->pExpr;.    
47a25 20 20 20 20 72 63 20 3d 20 66 74 73 33 45 78 70      rc = fts3Exp
47a26 72 4c 6f 61 64 44 6f 63 6c 69 73 74 73 28 70 43  rLoadDoclists(pC
47a27 73 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  sr, 0, 0);.     
47a28 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
47a29 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20 20  E_OK ) break;.  
47a2a 20 20 20 20 20 20 69 66 28 20 62 47 6c 6f 62 61        if( bGloba
47a2b 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  l ){.          i
47a2c 66 28 20 70 43 73 72 2d 3e 70 44 65 66 65 72 72  f( pCsr->pDeferr
47a2d 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ed ){.          
47a2e 20 20 72 63 20 3d 20 66 74 73 33 4d 61 74 63 68    rc = fts3Match
47a2f 69 6e 66 6f 53 65 6c 65 63 74 44 6f 63 74 6f 74  infoSelectDoctot
47a30 61 6c 28 70 54 61 62 2c 20 26 70 53 65 6c 65 63  al(pTab, &pSelec
47a31 74 2c 20 26 70 49 6e 66 6f 2d 3e 6e 44 6f 63 2c  t, &pInfo->nDoc,
47a32 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
47a33 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
47a34 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  OK ) break;.    
47a35 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
47a36 20 20 72 63 20 3d 20 66 74 73 33 45 78 70 72 49    rc = fts3ExprI
47a37 74 65 72 61 74 65 28 70 45 78 70 72 2c 20 66 74  terate(pExpr, ft
47a38 73 33 45 78 70 72 47 6c 6f 62 61 6c 48 69 74 73  s3ExprGlobalHits
47a39 43 62 2c 28 76 6f 69 64 2a 29 70 49 6e 66 6f 29  Cb,(void*)pInfo)
47a3a 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
47a3b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
47a3c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
47a3d 0a 20 20 20 20 20 20 20 20 28 76 6f 69 64 29 66  .        (void)f
47a3e 74 73 33 45 78 70 72 49 74 65 72 61 74 65 28 70  ts3ExprIterate(p
47a3f 45 78 70 72 2c 20 66 74 73 33 45 78 70 72 4c 6f  Expr, fts3ExprLo
47a40 63 61 6c 48 69 74 73 43 62 2c 28 76 6f 69 64 2a  calHitsCb,(void*
47a41 29 70 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20  )pInfo);.       
47a42 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
47a43 20 20 20 20 7d 0a 0a 20 20 20 20 70 49 6e 66 6f      }..    pInfo
47a44 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 20 2b 3d 20  ->aMatchinfo += 
47a45 66 74 73 33 4d 61 74 63 68 69 6e 66 6f 53 69 7a  fts3MatchinfoSiz
47a46 65 28 70 49 6e 66 6f 2c 20 7a 41 72 67 5b 69 5d  e(pInfo, zArg[i]
47a47 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  );.  }..  sqlite
47a48 33 5f 72 65 73 65 74 28 70 53 65 6c 65 63 74 29  3_reset(pSelect)
47a49 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
47a4a 0a 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74  .../*.** Populat
47a4b 65 20 70 43 73 72 2d 3e 61 4d 61 74 63 68 69 6e  e pCsr->aMatchin
47a4c 66 6f 5b 5d 20 77 69 74 68 20 64 61 74 61 20 66  fo[] with data f
47a4d 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  or the current r
47a4e 6f 77 2e 20 54 68 65 20 0a 2a 2a 20 27 6d 61 74  ow. The .** 'mat
47a4f 63 68 69 6e 66 6f 27 20 64 61 74 61 20 69 73 20  chinfo' data is 
47a50 61 6e 20 61 72 72 61 79 20 6f 66 20 33 32 2d 62  an array of 32-b
47a51 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65  it unsigned inte
47a52 67 65 72 73 20 28 43 20 74 79 70 65 20 75 33 32  gers (C type u32
47a53 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
47a54 20 66 74 73 33 47 65 74 4d 61 74 63 68 69 6e 66   fts3GetMatchinf
47a55 6f 28 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20  o(.  Fts3Cursor 
47a56 2a 70 43 73 72 2c 20 20 20 20 20 20 20 20 20 20  *pCsr,          
47a57 20 20 20 20 20 2f 2a 20 46 54 53 33 20 43 75 72       /* FTS3 Cur
47a58 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  sor object */.  
47a59 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67  const char *zArg
47a5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a5b 2f 2a 20 53 65 63 6f 6e 64 20 61 72 67 75 6d 65  /* Second argume
47a5c 6e 74 20 74 6f 20 6d 61 74 63 68 69 6e 66 6f 28  nt to matchinfo(
47a5d 29 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  ) function */.){
47a5e 0a 20 20 4d 61 74 63 68 49 6e 66 6f 20 73 49 6e  .  MatchInfo sIn
47a5f 66 6f 3b 0a 20 20 46 74 73 33 54 61 62 6c 65 20  fo;.  Fts3Table 
47a60 2a 70 54 61 62 20 3d 20 28 46 74 73 33 54 61 62  *pTab = (Fts3Tab
47a61 6c 65 20 2a 29 70 43 73 72 2d 3e 62 61 73 65 2e  le *)pCsr->base.
47a62 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 20  pVtab;.  int rc 
47a63 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
47a64 6e 74 20 62 47 6c 6f 62 61 6c 20 3d 20 30 3b 20  nt bGlobal = 0; 
47a65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
47a66 2a 20 43 6f 6c 6c 65 63 74 20 27 67 6c 6f 62 61  * Collect 'globa
47a67 6c 27 20 73 74 61 74 73 20 61 73 20 77 65 6c 6c  l' stats as well
47a68 20 61 73 20 6c 6f 63 61 6c 20 2a 2f 0a 0a 20 20   as local */..  
47a69 6d 65 6d 73 65 74 28 26 73 49 6e 66 6f 2c 20 30  memset(&sInfo, 0
47a6a 2c 20 73 69 7a 65 6f 66 28 4d 61 74 63 68 49 6e  , sizeof(MatchIn
47a6b 66 6f 29 29 3b 0a 20 20 73 49 6e 66 6f 2e 70 43  fo));.  sInfo.pC
47a6c 75 72 73 6f 72 20 3d 20 70 43 73 72 3b 0a 20 20  ursor = pCsr;.  
47a6d 73 49 6e 66 6f 2e 6e 43 6f 6c 20 3d 20 70 54 61  sInfo.nCol = pTa
47a6e 62 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 0a 20 20 2f  b->nColumn;..  /
47a6f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 63 61  * If there is ca
47a70 63 68 65 64 20 6d 61 74 63 68 69 6e 66 6f 28 29  ched matchinfo()
47a71 20 64 61 74 61 2c 20 62 75 74 20 74 68 65 20 66   data, but the f
47a72 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 66 6f 72  ormat string for
47a73 20 74 68 65 20 0a 20 20 2a 2a 20 63 61 63 68 65   the .  ** cache
47a74 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
47a75 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e  the format strin
47a76 67 20 66 6f 72 20 74 68 69 73 20 72 65 71 75 65  g for this reque
47a77 73 74 2c 20 64 69 73 63 61 72 64 20 0a 20 20 2a  st, discard .  *
47a78 2a 20 74 68 65 20 63 61 63 68 65 64 20 64 61 74  * the cached dat
47a79 61 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 73 72  a. */.  if( pCsr
47a7a 2d 3e 7a 4d 61 74 63 68 69 6e 66 6f 20 26 26 20  ->zMatchinfo && 
47a7b 73 74 72 63 6d 70 28 70 43 73 72 2d 3e 7a 4d 61  strcmp(pCsr->zMa
47a7c 74 63 68 69 6e 66 6f 2c 20 7a 41 72 67 29 20 29  tchinfo, zArg) )
47a7d 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  {.    assert( pC
47a7e 73 72 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 20 29  sr->aMatchinfo )
47a7f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
47a80 65 65 28 70 43 73 72 2d 3e 61 4d 61 74 63 68 69  ee(pCsr->aMatchi
47a81 6e 66 6f 29 3b 0a 20 20 20 20 70 43 73 72 2d 3e  nfo);.    pCsr->
47a82 7a 4d 61 74 63 68 69 6e 66 6f 20 3d 20 30 3b 0a  zMatchinfo = 0;.
47a83 20 20 20 20 70 43 73 72 2d 3e 61 4d 61 74 63 68      pCsr->aMatch
47a84 69 6e 66 6f 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  info = 0;.  }.. 
47a85 20 2f 2a 20 49 66 20 46 74 73 33 43 75 72 73 6f   /* If Fts3Curso
47a86 72 2e 61 4d 61 74 63 68 69 6e 66 6f 5b 5d 20 69  r.aMatchinfo[] i
47a87 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69  s NULL, then thi
47a88 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 74  s is the first t
47a89 69 6d 65 20 74 68 65 0a 20 20 2a 2a 20 6d 61 74  ime the.  ** mat
47a8a 63 68 69 6e 66 6f 20 66 75 6e 63 74 69 6f 6e 20  chinfo function 
47a8b 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  has been called 
47a8c 66 6f 72 20 74 68 69 73 20 71 75 65 72 79 2e 20  for this query. 
47a8d 49 6e 20 74 68 69 73 20 63 61 73 65 20 0a 20 20  In this case .  
47a8e 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20  ** allocate the 
47a8f 61 72 72 61 79 20 75 73 65 64 20 74 6f 20 61 63  array used to ac
47a90 63 75 6d 75 6c 61 74 65 20 74 68 65 20 6d 61 74  cumulate the mat
47a91 63 68 69 6e 66 6f 20 64 61 74 61 20 61 6e 64 0a  chinfo data and.
47a92 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20    ** initialize 
47a93 74 68 6f 73 65 20 65 6c 65 6d 65 6e 74 73 20 74  those elements t
47a94 68 61 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74  hat are constant
47a95 20 66 6f 72 20 65 76 65 72 79 20 72 6f 77 2e 0a   for every row..
47a96 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 73 72 2d    */.  if( pCsr-
47a97 3e 61 4d 61 74 63 68 69 6e 66 6f 3d 3d 30 20 29  >aMatchinfo==0 )
47a98 7b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 74 63 68  {.    int nMatch
47a99 69 6e 66 6f 20 3d 20 30 3b 20 20 20 20 20 20 20  info = 0;       
47a9a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
47a9b 20 75 33 32 20 65 6c 65 6d 65 6e 74 73 20 69 6e   u32 elements in
47a9c 20 6d 61 74 63 68 2d 69 6e 66 6f 20 2a 2f 0a 20   match-info */. 
47a9d 20 20 20 69 6e 74 20 6e 41 72 67 3b 20 20 20 20     int nArg;    
47a9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a9f 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 7a 41 72   /* Bytes in zAr
47aa0 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 20  g */.    int i; 
47aa1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47aa2 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74         /* Used t
47aa3 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
47aa4 68 20 7a 41 72 67 20 2a 2f 0a 0a 20 20 20 20 2f  h zArg */..    /
47aa5 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20  * Determine the 
47aa6 6e 75 6d 62 65 72 20 6f 66 20 70 68 72 61 73 65  number of phrase
47aa7 73 20 69 6e 20 74 68 65 20 71 75 65 72 79 20 2a  s in the query *
47aa8 2f 0a 20 20 20 20 70 43 73 72 2d 3e 6e 50 68 72  /.    pCsr->nPhr
47aa9 61 73 65 20 3d 20 66 74 73 33 45 78 70 72 50 68  ase = fts3ExprPh
47aaa 72 61 73 65 43 6f 75 6e 74 28 70 43 73 72 2d 3e  raseCount(pCsr->
47aab 70 45 78 70 72 29 3b 0a 20 20 20 20 73 49 6e 66  pExpr);.    sInf
47aac 6f 2e 6e 50 68 72 61 73 65 20 3d 20 70 43 73 72  o.nPhrase = pCsr
47aad 2d 3e 6e 50 68 72 61 73 65 3b 0a 0a 20 20 20 20  ->nPhrase;..    
47aae 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65  /* Determine the
47aaf 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 74 65 67   number of integ
47ab0 65 72 73 20 69 6e 20 74 68 65 20 62 75 66 66 65  ers in the buffe
47ab1 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  r returned by th
47ab2 69 73 20 63 61 6c 6c 2e 20 2a 2f 0a 20 20 20 20  is call. */.    
47ab3 66 6f 72 28 69 3d 30 3b 20 7a 41 72 67 5b 69 5d  for(i=0; zArg[i]
47ab4 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 4d  ; i++){.      nM
47ab5 61 74 63 68 69 6e 66 6f 20 2b 3d 20 66 74 73 33  atchinfo += fts3
47ab6 4d 61 74 63 68 69 6e 66 6f 53 69 7a 65 28 26 73  MatchinfoSize(&s
47ab7 49 6e 66 6f 2c 20 7a 41 72 67 5b 69 5d 29 3b 0a  Info, zArg[i]);.
47ab8 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c      }..    /* Al
47ab9 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72  locate space for
47aba 20 46 74 73 33 43 75 72 73 6f 72 2e 61 4d 61 74   Fts3Cursor.aMat
47abb 63 68 69 6e 66 6f 5b 5d 20 61 6e 64 20 46 74 73  chinfo[] and Fts
47abc 33 43 75 72 73 6f 72 2e 7a 4d 61 74 63 68 69 6e  3Cursor.zMatchin
47abd 66 6f 2e 20 2a 2f 0a 20 20 20 20 6e 41 72 67 20  fo. */.    nArg 
47abe 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 41  = (int)strlen(zA
47abf 72 67 29 3b 0a 20 20 20 20 70 43 73 72 2d 3e 61  rg);.    pCsr->a
47ac0 4d 61 74 63 68 69 6e 66 6f 20 3d 20 28 75 33 32  Matchinfo = (u32
47ac1 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
47ac2 63 28 73 69 7a 65 6f 66 28 75 33 32 29 2a 6e 4d  c(sizeof(u32)*nM
47ac3 61 74 63 68 69 6e 66 6f 20 2b 20 6e 41 72 67 20  atchinfo + nArg 
47ac4 2b 20 31 29 3b 0a 20 20 20 20 69 66 28 20 21 70  + 1);.    if( !p
47ac5 43 73 72 2d 3e 61 4d 61 74 63 68 69 6e 66 6f 20  Csr->aMatchinfo 
47ac6 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
47ac7 4e 4f 4d 45 4d 3b 0a 0a 20 20 20 20 70 43 73 72  NOMEM;..    pCsr
47ac8 2d 3e 7a 4d 61 74 63 68 69 6e 66 6f 20 3d 20 28  ->zMatchinfo = (
47ac9 63 68 61 72 20 2a 29 26 70 43 73 72 2d 3e 61 4d  char *)&pCsr->aM
47aca 61 74 63 68 69 6e 66 6f 5b 6e 4d 61 74 63 68 69  atchinfo[nMatchi
47acb 6e 66 6f 5d 3b 0a 20 20 20 20 70 43 73 72 2d 3e  nfo];.    pCsr->
47acc 6e 4d 61 74 63 68 69 6e 66 6f 20 3d 20 6e 4d 61  nMatchinfo = nMa
47acd 74 63 68 69 6e 66 6f 3b 0a 20 20 20 20 6d 65 6d  tchinfo;.    mem
47ace 63 70 79 28 70 43 73 72 2d 3e 7a 4d 61 74 63 68  cpy(pCsr->zMatch
47acf 69 6e 66 6f 2c 20 7a 41 72 67 2c 20 6e 41 72 67  info, zArg, nArg
47ad0 2b 31 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  +1);.    memset(
47ad1 70 43 73 72 2d 3e 61 4d 61 74 63 68 69 6e 66 6f  pCsr->aMatchinfo
47ad2 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 33 32 29  , 0, sizeof(u32)
47ad3 2a 6e 4d 61 74 63 68 69 6e 66 6f 29 3b 0a 20 20  *nMatchinfo);.  
47ad4 20 20 70 43 73 72 2d 3e 69 73 4d 61 74 63 68 69    pCsr->isMatchi
47ad5 6e 66 6f 4e 65 65 64 65 64 20 3d 20 31 3b 0a 20  nfoNeeded = 1;. 
47ad6 20 20 20 62 47 6c 6f 62 61 6c 20 3d 20 31 3b 0a     bGlobal = 1;.
47ad7 20 20 7d 0a 0a 20 20 73 49 6e 66 6f 2e 61 4d 61    }..  sInfo.aMa
47ad8 74 63 68 69 6e 66 6f 20 3d 20 70 43 73 72 2d 3e  tchinfo = pCsr->
47ad9 61 4d 61 74 63 68 69 6e 66 6f 3b 0a 20 20 73 49  aMatchinfo;.  sI
47ada 6e 66 6f 2e 6e 50 68 72 61 73 65 20 3d 20 70 43  nfo.nPhrase = pC
47adb 73 72 2d 3e 6e 50 68 72 61 73 65 3b 0a 20 20 69  sr->nPhrase;.  i
47adc 66 28 20 70 43 73 72 2d 3e 69 73 4d 61 74 63 68  f( pCsr->isMatch
47add 69 6e 66 6f 4e 65 65 64 65 64 20 29 7b 0a 20 20  infoNeeded ){.  
47ade 20 20 72 63 20 3d 20 66 74 73 33 4d 61 74 63 68    rc = fts3Match
47adf 69 6e 66 6f 56 61 6c 75 65 73 28 70 43 73 72 2c  infoValues(pCsr,
47ae0 20 62 47 6c 6f 62 61 6c 2c 20 26 73 49 6e 66 6f   bGlobal, &sInfo
47ae1 2c 20 7a 41 72 67 29 3b 0a 20 20 20 20 70 43 73  , zArg);.    pCs
47ae2 72 2d 3e 69 73 4d 61 74 63 68 69 6e 66 6f 4e 65  r->isMatchinfoNe
47ae3 65 64 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  eded = 0;.  }.. 
47ae4 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
47ae5 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
47ae6 69 6f 6e 20 6f 66 20 73 6e 69 70 70 65 74 28 29  ion of snippet()
47ae7 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51   function..*/.SQ
47ae8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
47ae9 64 20 73 71 6c 69 74 65 33 46 74 73 33 53 6e 69  d sqlite3Fts3Sni
47aea 70 70 65 74 28 0a 20 20 73 71 6c 69 74 65 33 5f  ppet(.  sqlite3_
47aeb 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20  context *pCtx,  
47aec 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74          /* SQLit
47aed 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20  e function call 
47aee 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 46 74 73  context */.  Fts
47aef 33 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 20  3Cursor *pCsr,  
47af0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47af1 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f  Cursor object */
47af2 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
47af3 53 74 61 72 74 2c 20 20 20 20 20 20 20 20 20 20  Start,          
47af4 20 20 20 2f 2a 20 53 6e 69 70 70 65 74 20 73 74     /* Snippet st
47af5 61 72 74 20 74 65 78 74 20 2d 20 22 3c 62 3e 22  art text - "<b>"
47af6 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
47af7 20 2a 7a 45 6e 64 2c 20 20 20 20 20 20 20 20 20   *zEnd,         
47af8 20 20 20 20 20 20 2f 2a 20 53 6e 69 70 70 65 74        /* Snippet
47af9 20 65 6e 64 20 74 65 78 74 20 2d 20 22 3c 2f 62   end text - "</b
47afa 3e 22 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  >" */.  const ch
47afb 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73 2c 20 20  ar *zEllipsis,  
47afc 20 20 20 20 20 20 20 20 2f 2a 20 53 6e 69 70 70          /* Snipp
47afd 65 74 20 65 6c 6c 69 70 73 69 73 20 74 65 78 74  et ellipsis text
47afe 20 2d 20 22 3c 62 3e 2e 2e 2e 3c 2f 62 3e 22 20   - "<b>...</b>" 
47aff 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20  */.  int iCol,  
47b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b01 20 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20       /* Extract 
47b02 73 6e 69 70 70 65 74 20 66 72 6f 6d 20 74 68 69  snippet from thi
47b03 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e  s column */.  in
47b04 74 20 6e 54 6f 6b 65 6e 20 20 20 20 20 20 20 20  t nToken        
47b05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
47b06 20 41 70 70 72 6f 78 69 6d 61 74 65 20 6e 75 6d   Approximate num
47b07 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e  ber of tokens in
47b08 20 73 6e 69 70 70 65 74 20 2a 2f 0a 29 7b 0a 20   snippet */.){. 
47b09 20 46 74 73 33 54 61 62 6c 65 20 2a 70 54 61 62   Fts3Table *pTab
47b0a 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29   = (Fts3Table *)
47b0b 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62  pCsr->base.pVtab
47b0c 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
47b0d 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b  ITE_OK;.  int i;
47b0e 0a 20 20 53 74 72 42 75 66 66 65 72 20 72 65 73  .  StrBuffer res
47b0f 20 3d 20 7b 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20   = {0, 0, 0};.. 
47b10 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64   /* The returned
47b11 20 74 65 78 74 20 69 6e 63 6c 75 64 65 73 20 75   text includes u
47b12 70 20 74 6f 20 66 6f 75 72 20 66 72 61 67 6d 65  p to four fragme
47b13 6e 74 73 20 6f 66 20 74 65 78 74 20 65 78 74 72  nts of text extr
47b14 61 63 74 65 64 20 66 72 6f 6d 0a 20 20 2a 2a 20  acted from.  ** 
47b15 74 68 65 20 64 61 74 61 20 69 6e 20 74 68 65 20  the data in the 
47b16 63 75 72 72 65 6e 74 20 72 6f 77 2e 20 54 68 65  current row. The
47b17 20 66 69 72 73 74 20 69 74 65 72 61 74 69 6f 6e   first iteration
47b18 20 6f 66 20 74 68 65 20 66 6f 72 28 2e 2e 2e 29   of the for(...)
47b19 20 6c 6f 6f 70 0a 20 20 2a 2a 20 62 65 6c 6f 77   loop.  ** below
47b1a 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 63   attempts to loc
47b1b 61 74 65 20 61 20 73 69 6e 67 6c 65 20 66 72 61  ate a single fra
47b1c 67 6d 65 6e 74 20 6f 66 20 74 65 78 74 20 6e 54  gment of text nT
47b1d 6f 6b 65 6e 20 74 6f 6b 65 6e 73 20 69 6e 20 0a  oken tokens in .
47b1e 20 20 2a 2a 20 73 69 7a 65 20 74 68 61 74 20 63    ** size that c
47b1f 6f 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73 74  ontains at least
47b20 20 6f 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f 66   one instance of
47b21 20 61 6c 6c 20 70 68 72 61 73 65 73 20 69 6e 20   all phrases in 
47b22 74 68 65 20 71 75 65 72 79 0a 20 20 2a 2a 20 65  the query.  ** e
47b23 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 61  xpression that a
47b24 70 70 65 61 72 20 69 6e 20 74 68 65 20 63 75 72  ppear in the cur
47b25 72 65 6e 74 20 72 6f 77 2e 20 49 66 20 73 75 63  rent row. If suc
47b26 68 20 61 20 66 72 61 67 6d 65 6e 74 20 6f 66 20  h a fragment of 
47b27 74 65 78 74 0a 20 20 2a 2a 20 63 61 6e 6e 6f 74  text.  ** cannot
47b28 20 62 65 20 66 6f 75 6e 64 2c 20 74 68 65 20 73   be found, the s
47b29 65 63 6f 6e 64 20 69 74 65 72 61 74 69 6f 6e 20  econd iteration 
47b2a 6f 66 20 74 68 65 20 6c 6f 6f 70 20 61 74 74 65  of the loop atte
47b2b 6d 70 74 73 20 74 6f 20 6c 6f 63 61 74 65 0a 20  mpts to locate. 
47b2c 20 2a 2a 20 61 20 70 61 69 72 20 6f 66 20 66 72   ** a pair of fr
47b2d 61 67 6d 65 6e 74 73 2c 20 61 6e 64 20 73 6f 20  agments, and so 
47b2e 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e  on..  */.  int n
47b2f 53 6e 69 70 70 65 74 20 3d 20 30 3b 20 20 20 20  Snippet = 0;    
47b30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
47b31 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74  mber of fragment
47b32 73 20 69 6e 20 74 68 69 73 20 73 6e 69 70 70 65  s in this snippe
47b33 74 20 2a 2f 0a 20 20 53 6e 69 70 70 65 74 46 72  t */.  SnippetFr
47b34 61 67 6d 65 6e 74 20 61 53 6e 69 70 70 65 74 5b  agment aSnippet[
47b35 34 5d 3b 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75  4];    /* Maximu
47b36 6d 20 6f 66 20 34 20 66 72 61 67 6d 65 6e 74 73  m of 4 fragments
47b37 20 70 65 72 20 73 6e 69 70 70 65 74 20 2a 2f 0a   per snippet */.
47b38 20 20 69 6e 74 20 6e 46 54 6f 6b 65 6e 20 3d 20    int nFToken = 
47b39 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  -1;             
47b3a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
47b3b 6f 6b 65 6e 73 20 69 6e 20 65 61 63 68 20 66 72  okens in each fr
47b3c 61 67 6d 65 6e 74 20 2a 2f 0a 0a 20 20 69 66 28  agment */..  if(
47b3d 20 21 70 43 73 72 2d 3e 70 45 78 70 72 20 29 7b   !pCsr->pExpr ){
47b3e 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
47b3f 75 6c 74 5f 74 65 78 74 28 70 43 74 78 2c 20 22  ult_text(pCtx, "
47b40 22 2c 20 30 2c 20 53 51 4c 49 54 45 5f 53 54 41  ", 0, SQLITE_STA
47b41 54 49 43 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  TIC);.    return
47b42 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 6e 53 6e  ;.  }..  for(nSn
47b43 69 70 70 65 74 3d 31 3b 20 31 3b 20 6e 53 6e 69  ippet=1; 1; nSni
47b44 70 70 65 74 2b 2b 29 7b 0a 0a 20 20 20 20 69 6e  ppet++){..    in
47b45 74 20 69 53 6e 69 70 3b 20 20 20 20 20 20 20 20  t iSnip;        
47b46 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
47b47 6f 6f 70 20 63 6f 75 6e 74 65 72 20 30 2e 2e 6e  oop counter 0..n
47b48 53 6e 69 70 70 65 74 2d 31 20 2a 2f 0a 20 20 20  Snippet-1 */.   
47b49 20 75 36 34 20 6d 43 6f 76 65 72 65 64 20 3d 20   u64 mCovered = 
47b4a 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0;             /
47b4b 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 70 68 72  * Bitmask of phr
47b4c 61 73 65 73 20 63 6f 76 65 72 65 64 20 62 79 20  ases covered by 
47b4d 73 6e 69 70 70 65 74 20 2a 2f 0a 20 20 20 20 75  snippet */.    u
47b4e 36 34 20 6d 53 65 65 6e 20 3d 20 30 3b 20 20 20  64 mSeen = 0;   
47b4f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47b50 42 69 74 6d 61 73 6b 20 6f 66 20 70 68 72 61 73  Bitmask of phras
47b51 65 73 20 73 65 65 6e 20 62 79 20 42 65 73 74 53  es seen by BestS
47b52 6e 69 70 70 65 74 28 29 20 2a 2f 0a 0a 20 20 20  nippet() */..   
47b53 20 69 66 28 20 6e 54 6f 6b 65 6e 3e 3d 30 20 29   if( nToken>=0 )
47b54 7b 0a 20 20 20 20 20 20 6e 46 54 6f 6b 65 6e 20  {.      nFToken 
47b55 3d 20 28 6e 54 6f 6b 65 6e 2b 6e 53 6e 69 70 70  = (nToken+nSnipp
47b56 65 74 2d 31 29 20 2f 20 6e 53 6e 69 70 70 65 74  et-1) / nSnippet
47b57 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
47b58 20 20 20 6e 46 54 6f 6b 65 6e 20 3d 20 2d 31 20     nFToken = -1 
47b59 2a 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 7d 0a  * nToken;.    }.
47b5a 0a 20 20 20 20 66 6f 72 28 69 53 6e 69 70 3d 30  .    for(iSnip=0
47b5b 3b 20 69 53 6e 69 70 3c 6e 53 6e 69 70 70 65 74  ; iSnip<nSnippet
47b5c 3b 20 69 53 6e 69 70 2b 2b 29 7b 0a 20 20 20 20  ; iSnip++){.    
47b5d 20 20 69 6e 74 20 69 42 65 73 74 53 63 6f 72 65    int iBestScore
47b5e 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 2f 2a   = -1;        /*
47b5f 20 42 65 73 74 20 73 63 6f 72 65 20 6f 66 20 63   Best score of c
47b60 6f 6c 75 6d 6e 73 20 63 68 65 63 6b 65 64 20 73  olumns checked s
47b61 6f 20 66 61 72 20 2a 2f 0a 20 20 20 20 20 20 69  o far */.      i
47b62 6e 74 20 69 52 65 61 64 3b 20 20 20 20 20 20 20  nt iRead;       
47b63 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
47b64 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  ed to iterate th
47b65 72 6f 75 67 68 20 63 6f 6c 75 6d 6e 73 20 2a 2f  rough columns */
47b66 0a 20 20 20 20 20 20 53 6e 69 70 70 65 74 46 72  .      SnippetFr
47b67 61 67 6d 65 6e 74 20 2a 70 46 72 61 67 6d 65 6e  agment *pFragmen
47b68 74 20 3d 20 26 61 53 6e 69 70 70 65 74 5b 69 53  t = &aSnippet[iS
47b69 6e 69 70 5d 3b 0a 0a 20 20 20 20 20 20 6d 65 6d  nip];..      mem
47b6a 73 65 74 28 70 46 72 61 67 6d 65 6e 74 2c 20 30  set(pFragment, 0
47b6b 2c 20 73 69 7a 65 6f 66 28 2a 70 46 72 61 67 6d  , sizeof(*pFragm
47b6c 65 6e 74 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  ent));..      /*
47b6d 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 61 6c   Loop through al
47b6e 6c 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65  l columns of the
47b6f 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e   table being con
47b70 73 69 64 65 72 65 64 20 66 6f 72 20 73 6e 69 70  sidered for snip
47b71 70 65 74 73 2e 0a 20 20 20 20 20 20 2a 2a 20 49  pets..      ** I
47b72 66 20 74 68 65 20 69 43 6f 6c 20 61 72 67 75 6d  f the iCol argum
47b73 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
47b74 74 69 6f 6e 20 77 61 73 20 6e 65 67 61 74 69 76  tion was negativ
47b75 65 2c 20 74 68 69 73 20 6d 65 61 6e 73 20 61 6c  e, this means al
47b76 6c 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d  l.      ** colum
47b77 6e 73 20 6f 66 20 74 68 65 20 46 54 53 33 20 74  ns of the FTS3 t
47b78 61 62 6c 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  able. Otherwise,
47b79 20 6f 6e 6c 79 20 63 6f 6c 75 6d 6e 20 69 43 6f   only column iCo
47b7a 6c 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 2e  l is considered.
47b7b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
47b7c 66 6f 72 28 69 52 65 61 64 3d 30 3b 20 69 52 65  for(iRead=0; iRe
47b7d 61 64 3c 70 54 61 62 2d 3e 6e 43 6f 6c 75 6d 6e  ad<pTab->nColumn
47b7e 3b 20 69 52 65 61 64 2b 2b 29 7b 0a 20 20 20 20  ; iRead++){.    
47b7f 20 20 20 20 53 6e 69 70 70 65 74 46 72 61 67 6d      SnippetFragm
47b80 65 6e 74 20 73 46 20 3d 20 7b 30 2c 20 30 2c 20  ent sF = {0, 0, 
47b81 30 2c 20 30 7d 3b 0a 20 20 20 20 20 20 20 20 69  0, 0};.        i
47b82 6e 74 20 69 53 3b 0a 20 20 20 20 20 20 20 20 69  nt iS;.        i
47b83 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 52  f( iCol>=0 && iR
47b84 65 61 64 21 3d 69 43 6f 6c 20 29 20 63 6f 6e 74  ead!=iCol ) cont
47b85 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20 20 20 2f  inue;..        /
47b86 2a 20 46 69 6e 64 20 74 68 65 20 62 65 73 74 20  * Find the best 
47b87 73 6e 69 70 70 65 74 20 6f 66 20 6e 46 54 6f 6b  snippet of nFTok
47b88 65 6e 20 74 6f 6b 65 6e 73 20 69 6e 20 63 6f 6c  en tokens in col
47b89 75 6d 6e 20 69 52 65 61 64 2e 20 2a 2f 0a 20 20  umn iRead. */.  
47b8a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 42        rc = fts3B
47b8b 65 73 74 53 6e 69 70 70 65 74 28 6e 46 54 6f 6b  estSnippet(nFTok
47b8c 65 6e 2c 20 70 43 73 72 2c 20 69 52 65 61 64 2c  en, pCsr, iRead,
47b8d 20 6d 43 6f 76 65 72 65 64 2c 20 26 6d 53 65 65   mCovered, &mSee
47b8e 6e 2c 20 26 73 46 2c 20 26 69 53 29 3b 0a 20 20  n, &sF, &iS);.  
47b8f 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
47b90 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
47b91 20 20 20 20 20 67 6f 74 6f 20 73 6e 69 70 70 65       goto snippe
47b92 74 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d  t_out;.        }
47b93 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 53 3e  .        if( iS>
47b94 69 42 65 73 74 53 63 6f 72 65 20 29 7b 0a 20 20  iBestScore ){.  
47b95 20 20 20 20 20 20 20 20 2a 70 46 72 61 67 6d 65          *pFragme
47b96 6e 74 20 3d 20 73 46 3b 0a 20 20 20 20 20 20 20  nt = sF;.       
47b97 20 20 20 69 42 65 73 74 53 63 6f 72 65 20 3d 20     iBestScore = 
47b98 69 53 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  iS;.        }.  
47b99 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 6d 43 6f      }..      mCo
47b9a 76 65 72 65 64 20 7c 3d 20 70 46 72 61 67 6d 65  vered |= pFragme
47b9b 6e 74 2d 3e 63 6f 76 65 72 65 64 3b 0a 20 20 20  nt->covered;.   
47b9c 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6c   }..    /* If al
47b9d 6c 20 71 75 65 72 79 20 70 68 72 61 73 65 73 20  l query phrases 
47b9e 73 65 65 6e 20 62 79 20 66 74 73 33 42 65 73 74  seen by fts3Best
47b9f 53 6e 69 70 70 65 74 28 29 20 61 72 65 20 70 72  Snippet() are pr
47ba0 65 73 65 6e 74 20 69 6e 20 61 74 20 6c 65 61 73  esent in at leas
47ba1 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 6f 66 20  t.    ** one of 
47ba2 74 68 65 20 6e 53 6e 69 70 70 65 74 20 73 6e 69  the nSnippet sni
47ba3 70 70 65 74 20 66 72 61 67 6d 65 6e 74 73 2c 20  ppet fragments, 
47ba4 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65  break out of the
47ba5 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20   loop..    */.  
47ba6 20 20 61 73 73 65 72 74 28 20 28 6d 43 6f 76 65    assert( (mCove
47ba7 72 65 64 26 6d 53 65 65 6e 29 3d 3d 6d 43 6f 76  red&mSeen)==mCov
47ba8 65 72 65 64 20 29 3b 0a 20 20 20 20 69 66 28 20  ered );.    if( 
47ba9 6d 53 65 65 6e 3d 3d 6d 43 6f 76 65 72 65 64 20  mSeen==mCovered 
47baa 7c 7c 20 6e 53 6e 69 70 70 65 74 3d 3d 53 69 7a  || nSnippet==Siz
47bab 65 6f 66 41 72 72 61 79 28 61 53 6e 69 70 70 65  eofArray(aSnippe
47bac 74 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  t) ) break;.  }.
47bad 0a 20 20 61 73 73 65 72 74 28 20 6e 46 54 6f 6b  .  assert( nFTok
47bae 65 6e 3e 30 20 29 3b 0a 0a 20 20 66 6f 72 28 69  en>0 );..  for(i
47baf 3d 30 3b 20 69 3c 6e 53 6e 69 70 70 65 74 20 26  =0; i<nSnippet &
47bb0 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b  & rc==SQLITE_OK;
47bb1 20 69 2b 2b 29 7b 0a 20 20 20 20 72 63 20 3d 20   i++){.    rc = 
47bb2 66 74 73 33 53 6e 69 70 70 65 74 54 65 78 74 28  fts3SnippetText(
47bb3 70 43 73 72 2c 20 26 61 53 6e 69 70 70 65 74 5b  pCsr, &aSnippet[
47bb4 69 5d 2c 20 0a 20 20 20 20 20 20 20 20 69 2c 20  i], .        i, 
47bb5 28 69 3d 3d 6e 53 6e 69 70 70 65 74 2d 31 29 2c  (i==nSnippet-1),
47bb6 20 6e 46 54 6f 6b 65 6e 2c 20 7a 53 74 61 72 74   nFToken, zStart
47bb7 2c 20 7a 45 6e 64 2c 20 7a 45 6c 6c 69 70 73 69  , zEnd, zEllipsi
47bb8 73 2c 20 26 72 65 73 0a 20 20 20 20 29 3b 0a 20  s, &res.    );. 
47bb9 20 7d 0a 0a 20 73 6e 69 70 70 65 74 5f 6f 75 74   }.. snippet_out
47bba 3a 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 53  :.  sqlite3Fts3S
47bbb 65 67 6d 65 6e 74 73 43 6c 6f 73 65 28 70 54 61  egmentsClose(pTa
47bbc 62 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  b);.  if( rc!=SQ
47bbd 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
47bbe 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
47bbf 72 6f 72 5f 63 6f 64 65 28 70 43 74 78 2c 20 72  ror_code(pCtx, r
47bc0 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  c);.    sqlite3_
47bc1 66 72 65 65 28 72 65 73 2e 7a 29 3b 0a 20 20 7d  free(res.z);.  }
47bc2 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
47bc3 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43  3_result_text(pC
47bc4 74 78 2c 20 72 65 73 2e 7a 2c 20 2d 31 2c 20 73  tx, res.z, -1, s
47bc5 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
47bc6 7d 0a 7d 0a 0a 0a 74 79 70 65 64 65 66 20 73 74  }.}...typedef st
47bc7 72 75 63 74 20 54 65 72 6d 4f 66 66 73 65 74 20  ruct TermOffset 
47bc8 54 65 72 6d 4f 66 66 73 65 74 3b 0a 74 79 70 65  TermOffset;.type
47bc9 64 65 66 20 73 74 72 75 63 74 20 54 65 72 6d 4f  def struct TermO
47bca 66 66 73 65 74 43 74 78 20 54 65 72 6d 4f 66 66  ffsetCtx TermOff
47bcb 73 65 74 43 74 78 3b 0a 0a 73 74 72 75 63 74 20  setCtx;..struct 
47bcc 54 65 72 6d 4f 66 66 73 65 74 20 7b 0a 20 20 63  TermOffset {.  c
47bcd 68 61 72 20 2a 70 4c 69 73 74 3b 20 20 20 20 20  har *pList;     
47bce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
47bcf 2a 20 50 6f 73 69 74 69 6f 6e 2d 6c 69 73 74 20  * Position-list 
47bd0 2a 2f 0a 20 20 69 6e 74 20 69 50 6f 73 3b 20 20  */.  int iPos;  
47bd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47bd2 20 20 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e       /* Position
47bd3 20 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20   just read from 
47bd4 70 4c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69  pList */.  int i
47bd5 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Off;            
47bd6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66             /* Of
47bd7 66 73 65 74 20 6f 66 20 74 68 69 73 20 74 65 72  fset of this ter
47bd8 6d 20 66 72 6f 6d 20 72 65 61 64 20 70 6f 73 69  m from read posi
47bd9 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a 73 74 72  tions */.};..str
47bda 75 63 74 20 54 65 72 6d 4f 66 66 73 65 74 43 74  uct TermOffsetCt
47bdb 78 20 7b 0a 20 20 46 74 73 33 43 75 72 73 6f 72  x {.  Fts3Cursor
47bdc 20 2a 70 43 73 72 3b 0a 20 20 69 6e 74 20 69 43   *pCsr;.  int iC
47bdd 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
47bde 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
47bdf 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 74 6f 20  umn of table to 
47be0 70 6f 70 75 6c 61 74 65 20 61 54 65 72 6d 20 66  populate aTerm f
47be1 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69 54 65 72  or */.  int iTer
47be2 6d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  m;.  sqlite3_int
47be3 36 34 20 69 44 6f 63 69 64 3b 0a 20 20 54 65 72  64 iDocid;.  Ter
47be4 6d 4f 66 66 73 65 74 20 2a 61 54 65 72 6d 3b 0a  mOffset *aTerm;.
47be5 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  };../*.** This f
47be6 75 6e 63 74 69 6f 6e 20 69 73 20 61 6e 20 66 74  unction is an ft
47be7 73 33 45 78 70 72 49 74 65 72 61 74 65 28 29 20  s3ExprIterate() 
47be8 63 61 6c 6c 62 61 63 6b 20 75 73 65 64 20 62 79  callback used by
47be9 20 73 71 6c 69 74 65 33 46 74 73 33 4f 66 66 73   sqlite3Fts3Offs
47bea 65 74 73 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ets()..*/.static
47beb 20 69 6e 74 20 66 74 73 33 45 78 70 72 54 65 72   int fts3ExprTer
47bec 6d 4f 66 66 73 65 74 49 6e 69 74 28 46 74 73 33  mOffsetInit(Fts3
47bed 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74  Expr *pExpr, int
47bee 20 69 50 68 72 61 73 65 2c 20 76 6f 69 64 20 2a   iPhrase, void *
47bef 63 74 78 29 7b 0a 20 20 54 65 72 6d 4f 66 66 73  ctx){.  TermOffs
47bf0 65 74 43 74 78 20 2a 70 20 3d 20 28 54 65 72 6d  etCtx *p = (Term
47bf1 4f 66 66 73 65 74 43 74 78 20 2a 29 63 74 78 3b  OffsetCtx *)ctx;
47bf2 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20  .  int nTerm;   
47bf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47bf4 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
47bf5 74 6f 6b 65 6e 73 20 69 6e 20 70 68 72 61 73 65  tokens in phrase
47bf6 20 2a 2f 0a 20 20 69 6e 74 20 69 54 65 72 6d 3b   */.  int iTerm;
47bf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47bf8 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f        /* For loo
47bf9 70 69 6e 67 20 74 68 72 6f 75 67 68 20 6e 54 65  ping through nTe
47bfa 72 6d 20 70 68 72 61 73 65 20 74 65 72 6d 73 20  rm phrase terms 
47bfb 2a 2f 0a 20 20 63 68 61 72 20 2a 70 4c 69 73 74  */.  char *pList
47bfc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
47bfd 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
47bfe 74 6f 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74  to position list
47bff 20 66 6f 72 20 70 68 72 61 73 65 20 2a 2f 0a 20   for phrase */. 
47c00 20 69 6e 74 20 69 50 6f 73 20 3d 20 30 3b 20 20   int iPos = 0;  
47c01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c02 20 2f 2a 20 46 69 72 73 74 20 70 6f 73 69 74 69   /* First positi
47c03 6f 6e 20 69 6e 20 70 6f 73 69 74 69 6f 6e 2d 6c  on in position-l
47c04 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ist */.  int rc;
47c05 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
47c06 45 54 45 52 28 69 50 68 72 61 73 65 29 3b 0a 20  ETER(iPhrase);. 
47c07 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
47c08 33 45 76 61 6c 50 68 72 61 73 65 50 6f 73 6c 69  3EvalPhrasePosli
47c09 73 74 28 70 2d 3e 70 43 73 72 2c 20 70 45 78 70  st(p->pCsr, pExp
47c0a 72 2c 20 70 2d 3e 69 43 6f 6c 2c 20 26 70 4c 69  r, p->iCol, &pLi
47c0b 73 74 29 3b 0a 20 20 6e 54 65 72 6d 20 3d 20 70  st);.  nTerm = p
47c0c 45 78 70 72 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  Expr->pPhrase->n
47c0d 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 20 70 4c 69  Token;.  if( pLi
47c0e 73 74 20 29 7b 0a 20 20 20 20 66 74 73 33 47 65  st ){.    fts3Ge
47c0f 74 44 65 6c 74 61 50 6f 73 69 74 69 6f 6e 28 26  tDeltaPosition(&
47c10 70 4c 69 73 74 2c 20 26 69 50 6f 73 29 3b 0a 20  pList, &iPos);. 
47c11 20 20 20 61 73 73 65 72 74 28 20 69 50 6f 73 3e     assert( iPos>
47c12 3d 30 20 29 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72  =0 );.  }..  for
47c13 28 69 54 65 72 6d 3d 30 3b 20 69 54 65 72 6d 3c  (iTerm=0; iTerm<
47c14 6e 54 65 72 6d 3b 20 69 54 65 72 6d 2b 2b 29 7b  nTerm; iTerm++){
47c15 0a 20 20 20 20 54 65 72 6d 4f 66 66 73 65 74 20  .    TermOffset 
47c16 2a 70 54 20 3d 20 26 70 2d 3e 61 54 65 72 6d 5b  *pT = &p->aTerm[
47c17 70 2d 3e 69 54 65 72 6d 2b 2b 5d 3b 0a 20 20 20  p->iTerm++];.   
47c18 20 70 54 2d 3e 69 4f 66 66 20 3d 20 6e 54 65 72   pT->iOff = nTer
47c19 6d 2d 69 54 65 72 6d 2d 31 3b 0a 20 20 20 20 70  m-iTerm-1;.    p
47c1a 54 2d 3e 70 4c 69 73 74 20 3d 20 70 4c 69 73 74  T->pList = pList
47c1b 3b 0a 20 20 20 20 70 54 2d 3e 69 50 6f 73 20 3d  ;.    pT->iPos =
47c1c 20 69 50 6f 73 3b 0a 20 20 7d 0a 0a 20 20 72 65   iPos;.  }..  re
47c1d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
47c1e 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
47c1f 20 6f 66 20 6f 66 66 73 65 74 73 28 29 20 66 75   of offsets() fu
47c20 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  nction..*/.SQLIT
47c21 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
47c22 71 6c 69 74 65 33 46 74 73 33 4f 66 66 73 65 74  qlite3Fts3Offset
47c23 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  s(.  sqlite3_con
47c24 74 65 78 74 20 2a 70 43 74 78 2c 20 20 20 20 20  text *pCtx,     
47c25 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 66       /* SQLite f
47c26 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e  unction call con
47c27 74 65 78 74 20 2a 2f 0a 20 20 46 74 73 33 43 75  text */.  Fts3Cu
47c28 72 73 6f 72 20 2a 70 43 73 72 20 20 20 20 20 20  rsor *pCsr      
47c29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
47c2a 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 29 7b  sor object */.){
47c2b 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 54  .  Fts3Table *pT
47c2c 61 62 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20  ab = (Fts3Table 
47c2d 2a 29 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74  *)pCsr->base.pVt
47c2e 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  ab;.  sqlite3_to
47c2f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63  kenizer_module c
47c30 6f 6e 73 74 20 2a 70 4d 6f 64 20 3d 20 70 54 61  onst *pMod = pTa
47c31 62 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70  b->pTokenizer->p
47c32 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 63  Module;.  int rc
47c33 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
47c34 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
47c35 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69 6e  urn Code */.  in
47c36 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20  t nToken;       
47c37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
47c38 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e   Number of token
47c39 73 20 69 6e 20 71 75 65 72 79 20 2a 2f 0a 20 20  s in query */.  
47c3a 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20  int iCol;       
47c3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c3c 2f 2a 20 43 6f 6c 75 6d 6e 20 63 75 72 72 65 6e  /* Column curren
47c3d 74 6c 79 20 62 65 69 6e 67 20 70 72 6f 63 65 73  tly being proces
47c3e 73 65 64 20 2a 2f 0a 20 20 53 74 72 42 75 66 66  sed */.  StrBuff
47c3f 65 72 20 72 65 73 20 3d 20 7b 30 2c 20 30 2c 20  er res = {0, 0, 
47c40 30 7d 3b 20 20 20 20 20 20 2f 2a 20 52 65 73 75  0};      /* Resu
47c41 6c 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 54  lt string */.  T
47c42 65 72 6d 4f 66 66 73 65 74 43 74 78 20 73 43 74  ermOffsetCtx sCt
47c43 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  x;             /
47c44 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 66 74  * Context for ft
47c45 73 33 45 78 70 72 54 65 72 6d 4f 66 66 73 65 74  s3ExprTermOffset
47c46 49 6e 69 74 28 29 20 2a 2f 0a 0a 20 20 69 66 28  Init() */..  if(
47c47 20 21 70 43 73 72 2d 3e 70 45 78 70 72 20 29 7b   !pCsr->pExpr ){
47c48 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
47c49 75 6c 74 5f 74 65 78 74 28 70 43 74 78 2c 20 22  ult_text(pCtx, "
47c4a 22 2c 20 30 2c 20 53 51 4c 49 54 45 5f 53 54 41  ", 0, SQLITE_STA
47c4b 54 49 43 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  TIC);.    return
47c4c 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 73 65 74 28  ;.  }..  memset(
47c4d 26 73 43 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66  &sCtx, 0, sizeof
47c4e 28 73 43 74 78 29 29 3b 0a 20 20 61 73 73 65 72  (sCtx));.  asser
47c4f 74 28 20 70 43 73 72 2d 3e 69 73 52 65 71 75 69  t( pCsr->isRequi
47c50 72 65 53 65 65 6b 3d 3d 30 20 29 3b 0a 0a 20 20  reSeek==0 );..  
47c51 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d  /* Count the num
47c52 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ber of terms in 
47c53 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 72  the query */.  r
47c54 63 20 3d 20 66 74 73 33 45 78 70 72 4c 6f 61 64  c = fts3ExprLoad
47c55 44 6f 63 6c 69 73 74 73 28 70 43 73 72 2c 20 30  Doclists(pCsr, 0
47c56 2c 20 26 6e 54 6f 6b 65 6e 29 3b 0a 20 20 69 66  , &nToken);.  if
47c57 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
47c58 29 20 67 6f 74 6f 20 6f 66 66 73 65 74 73 5f 6f  ) goto offsets_o
47c59 75 74 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61  ut;..  /* Alloca
47c5a 74 65 20 74 68 65 20 61 72 72 61 79 20 6f 66 20  te the array of 
47c5b 54 65 72 6d 4f 66 66 73 65 74 20 69 74 65 72 61  TermOffset itera
47c5c 74 6f 72 73 2e 20 2a 2f 0a 20 20 73 43 74 78 2e  tors. */.  sCtx.
47c5d 61 54 65 72 6d 20 3d 20 28 54 65 72 6d 4f 66 66  aTerm = (TermOff
47c5e 73 65 74 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61  set *)sqlite3_ma
47c5f 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 54 65 72 6d  lloc(sizeof(Term
47c60 4f 66 66 73 65 74 29 2a 6e 54 6f 6b 65 6e 29 3b  Offset)*nToken);
47c61 0a 20 20 69 66 28 20 30 3d 3d 73 43 74 78 2e 61  .  if( 0==sCtx.a
47c62 54 65 72 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d  Term ){.    rc =
47c63 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
47c64 20 20 20 67 6f 74 6f 20 6f 66 66 73 65 74 73 5f     goto offsets_
47c65 6f 75 74 3b 0a 20 20 7d 0a 20 20 73 43 74 78 2e  out;.  }.  sCtx.
47c66 69 44 6f 63 69 64 20 3d 20 70 43 73 72 2d 3e 69  iDocid = pCsr->i
47c67 50 72 65 76 49 64 3b 0a 20 20 73 43 74 78 2e 70  PrevId;.  sCtx.p
47c68 43 73 72 20 3d 20 70 43 73 72 3b 0a 0a 20 20 2f  Csr = pCsr;..  /
47c69 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  * Loop through t
47c6a 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73  he table columns
47c6b 2c 20 61 70 70 65 6e 64 69 6e 67 20 6f 66 66 73  , appending offs
47c6c 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74  et information t
47c6d 6f 20 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2d 62  o .  ** string-b
47c6e 75 66 66 65 72 20 72 65 73 20 66 6f 72 20 65 61  uffer res for ea
47c6f 63 68 20 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2f 0a  ch column..  */.
47c70 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43    for(iCol=0; iC
47c71 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 75 6d 6e  ol<pTab->nColumn
47c72 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 73  ; iCol++){.    s
47c73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
47c74 5f 63 75 72 73 6f 72 20 2a 70 43 3b 20 2f 2a 20  _cursor *pC; /* 
47c75 54 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72  Tokenizer cursor
47c76 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   */.    const ch
47c77 61 72 20 2a 5a 44 55 4d 4d 59 3b 20 20 20 20 20  ar *ZDUMMY;     
47c78 20 20 20 20 20 20 2f 2a 20 44 75 6d 6d 79 20 61        /* Dummy a
47c79 72 67 75 6d 65 6e 74 20 75 73 65 64 20 77 69 74  rgument used wit
47c7a 68 20 78 4e 65 78 74 28 29 20 2a 2f 0a 20 20 20  h xNext() */.   
47c7b 20 69 6e 74 20 4e 44 55 4d 4d 59 20 3d 20 30 3b   int NDUMMY = 0;
47c7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
47c7d 2a 20 44 75 6d 6d 79 20 61 72 67 75 6d 65 6e 74  * Dummy argument
47c7e 20 75 73 65 64 20 77 69 74 68 20 78 4e 65 78 74   used with xNext
47c7f 28 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53  () */.    int iS
47c80 74 61 72 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e  tart = 0;.    in
47c81 74 20 69 45 6e 64 20 3d 20 30 3b 0a 20 20 20 20  t iEnd = 0;.    
47c82 69 6e 74 20 69 43 75 72 72 65 6e 74 20 3d 20 30  int iCurrent = 0
47c83 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
47c84 20 2a 7a 44 6f 63 3b 0a 20 20 20 20 69 6e 74 20   *zDoc;.    int 
47c85 6e 44 6f 63 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e  nDoc;..    /* In
47c86 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e  itialize the con
47c87 74 65 6e 74 73 20 6f 66 20 73 43 74 78 2e 61 54  tents of sCtx.aT
47c88 65 72 6d 5b 5d 20 66 6f 72 20 63 6f 6c 75 6d 6e  erm[] for column
47c89 20 69 43 6f 6c 2e 20 54 68 65 72 65 20 69 73 20   iCol. There is 
47c8a 0a 20 20 20 20 2a 2a 20 6e 6f 20 77 61 79 20 74  .    ** no way t
47c8b 68 61 74 20 74 68 69 73 20 6f 70 65 72 61 74 69  hat this operati
47c8c 6f 6e 20 63 61 6e 20 66 61 69 6c 2c 20 73 6f 20  on can fail, so 
47c8d 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  the return code 
47c8e 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 66 74 73 33  from.    ** fts3
47c8f 45 78 70 72 49 74 65 72 61 74 65 28 29 20 63 61  ExprIterate() ca
47c90 6e 20 62 65 20 64 69 73 63 61 72 64 65 64 2e 0a  n be discarded..
47c91 20 20 20 20 2a 2f 0a 20 20 20 20 73 43 74 78 2e      */.    sCtx.
47c92 69 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 20  iCol = iCol;.   
47c93 20 73 43 74 78 2e 69 54 65 72 6d 20 3d 20 30 3b   sCtx.iTerm = 0;
47c94 0a 20 20 20 20 28 76 6f 69 64 29 66 74 73 33 45  .    (void)fts3E
47c95 78 70 72 49 74 65 72 61 74 65 28 70 43 73 72 2d  xprIterate(pCsr-
47c96 3e 70 45 78 70 72 2c 20 66 74 73 33 45 78 70 72  >pExpr, fts3Expr
47c97 54 65 72 6d 4f 66 66 73 65 74 49 6e 69 74 2c 20  TermOffsetInit, 
47c98 28 76 6f 69 64 20 2a 29 26 73 43 74 78 29 3b 0a  (void *)&sCtx);.
47c99 0a 20 20 20 20 2f 2a 20 52 65 74 72 65 69 76 65  .    /* Retreive
47c9a 20 74 68 65 20 74 65 78 74 20 73 74 6f 72 65 64   the text stored
47c9b 20 69 6e 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 2e   in column iCol.
47c9c 20 49 66 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20   If an SQL NULL 
47c9d 69 73 20 73 74 6f 72 65 64 20 0a 20 20 20 20 2a  is stored .    *
47c9e 2a 20 69 6e 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c  * in column iCol
47c9f 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65  , jump immediate
47ca0 6c 79 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69  ly to the next i
47ca1 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  teration of the 
47ca2 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2a 20 49 66 20  loop..    ** If 
47ca3 61 6e 20 4f 4f 4d 20 6f 63 63 75 72 73 20 77 68  an OOM occurs wh
47ca4 69 6c 65 20 72 65 74 72 69 65 76 69 6e 67 20 74  ile retrieving t
47ca5 68 65 20 64 61 74 61 20 28 74 68 69 73 20 63 61  he data (this ca
47ca6 6e 20 68 61 70 70 65 6e 20 69 66 20 53 51 4c 69  n happen if SQLi
47ca7 74 65 0a 20 20 20 20 2a 2a 20 6e 65 65 64 73 20  te.    ** needs 
47ca8 74 6f 20 74 72 61 6e 73 66 6f 72 6d 20 74 68 65  to transform the
47ca9 20 64 61 74 61 20 66 72 6f 6d 20 75 74 66 2d 31   data from utf-1
47caa 36 20 74 6f 20 75 74 66 2d 38 29 2c 20 72 65 74  6 to utf-8), ret
47cab 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
47cac 20 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20   .    ** to the 
47cad 63 61 6c 6c 65 72 2e 20 0a 20 20 20 20 2a 2f 0a  caller. .    */.
47cae 20 20 20 20 7a 44 6f 63 20 3d 20 28 63 6f 6e 73      zDoc = (cons
47caf 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
47cb0 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 43 73  _column_text(pCs
47cb1 72 2d 3e 70 53 74 6d 74 2c 20 69 43 6f 6c 2b 31  r->pStmt, iCol+1
47cb2 29 3b 0a 20 20 20 20 6e 44 6f 63 20 3d 20 73 71  );.    nDoc = sq
47cb3 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
47cb4 65 73 28 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20  es(pCsr->pStmt, 
47cb5 69 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 69 66 28  iCol+1);.    if(
47cb6 20 7a 44 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20   zDoc==0 ){.    
47cb7 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 6f    if( sqlite3_co
47cb8 6c 75 6d 6e 5f 74 79 70 65 28 70 43 73 72 2d 3e  lumn_type(pCsr->
47cb9 70 53 74 6d 74 2c 20 69 43 6f 6c 2b 31 29 3d 3d  pStmt, iCol+1)==
47cba 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20  SQLITE_NULL ){. 
47cbb 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
47cbc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
47cbd 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
47cbe 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 66 66  ;.      goto off
47cbf 73 65 74 73 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  sets_out;.    }.
47cc0 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  .    /* Initiali
47cc1 7a 65 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 69  ze a tokenizer i
47cc2 74 65 72 61 74 6f 72 20 74 6f 20 69 74 65 72 61  terator to itera
47cc3 74 65 20 74 68 72 6f 75 67 68 20 63 6f 6c 75 6d  te through colum
47cc4 6e 20 69 43 6f 6c 2e 20 2a 2f 0a 20 20 20 20 72  n iCol. */.    r
47cc5 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 4f  c = sqlite3Fts3O
47cc6 70 65 6e 54 6f 6b 65 6e 69 7a 65 72 28 70 54 61  penTokenizer(pTa
47cc7 62 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 70  b->pTokenizer, p
47cc8 43 73 72 2d 3e 69 4c 61 6e 67 69 64 2c 0a 20 20  Csr->iLangid,.  
47cc9 20 20 20 20 20 20 7a 44 6f 63 2c 20 6e 44 6f 63        zDoc, nDoc
47cca 2c 20 26 70 43 0a 20 20 20 20 29 3b 0a 20 20 20  , &pC.    );.   
47ccb 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
47ccc 4f 4b 20 29 20 67 6f 74 6f 20 6f 66 66 73 65 74  OK ) goto offset
47ccd 73 5f 6f 75 74 3b 0a 0a 20 20 20 20 72 63 20 3d  s_out;..    rc =
47cce 20 70 4d 6f 64 2d 3e 78 4e 65 78 74 28 70 43 2c   pMod->xNext(pC,
47ccf 20 26 5a 44 55 4d 4d 59 2c 20 26 4e 44 55 4d 4d   &ZDUMMY, &NDUMM
47cd0 59 2c 20 26 69 53 74 61 72 74 2c 20 26 69 45 6e  Y, &iStart, &iEn
47cd1 64 2c 20 26 69 43 75 72 72 65 6e 74 29 3b 0a 20  d, &iCurrent);. 
47cd2 20 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51     while( rc==SQ
47cd3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
47cd4 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
47cd5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47cd6 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72  Used to loop thr
47cd7 6f 75 67 68 20 74 65 72 6d 73 20 2a 2f 0a 20 20  ough terms */.  
47cd8 20 20 20 20 69 6e 74 20 69 4d 69 6e 50 6f 73 20      int iMinPos 
47cd9 3d 20 30 78 37 46 46 46 46 46 46 46 3b 20 20 20  = 0x7FFFFFFF;   
47cda 2f 2a 20 50 6f 73 69 74 69 6f 6e 20 6f 66 20 6e  /* Position of n
47cdb 65 78 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20  ext token */.   
47cdc 20 20 20 54 65 72 6d 4f 66 66 73 65 74 20 2a 70     TermOffset *p
47cdd 54 65 72 6d 20 3d 20 30 3b 20 20 20 20 20 20 2f  Term = 0;      /
47cde 2a 20 54 65 72 6d 4f 66 66 73 65 74 20 61 73 73  * TermOffset ass
47cdf 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 78  ociated with nex
47ce0 74 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 20 20 20 20  t token */..    
47ce1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 6f    for(i=0; i<nTo
47ce2 6b 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ken; i++){.     
47ce3 20 20 20 54 65 72 6d 4f 66 66 73 65 74 20 2a 70     TermOffset *p
47ce4 54 20 3d 20 26 73 43 74 78 2e 61 54 65 72 6d 5b  T = &sCtx.aTerm[
47ce5 69 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  i];.        if( 
47ce6 70 54 2d 3e 70 4c 69 73 74 20 26 26 20 28 70 54  pT->pList && (pT
47ce7 2d 3e 69 50 6f 73 2d 70 54 2d 3e 69 4f 66 66 29  ->iPos-pT->iOff)
47ce8 3c 69 4d 69 6e 50 6f 73 20 29 7b 0a 20 20 20 20  <iMinPos ){.    
47ce9 20 20 20 20 20 20 69 4d 69 6e 50 6f 73 20 3d 20        iMinPos = 
47cea 70 54 2d 3e 69 50 6f 73 2d 70 54 2d 3e 69 4f 66  pT->iPos-pT->iOf
47ceb 66 3b 0a 20 20 20 20 20 20 20 20 20 20 70 54 65  f;.          pTe
47cec 72 6d 20 3d 20 70 54 3b 0a 20 20 20 20 20 20 20  rm = pT;.       
47ced 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20   }.      }..    
47cee 20 20 69 66 28 20 21 70 54 65 72 6d 20 29 7b 0a    if( !pTerm ){.
47cef 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 6f          /* All o
47cf0 66 66 73 65 74 73 20 66 6f 72 20 74 68 69 73 20  ffsets for this 
47cf1 63 6f 6c 75 6d 6e 20 68 61 76 65 20 62 65 65 6e  column have been
47cf2 20 67 61 74 68 65 72 65 64 2e 20 2a 2f 0a 20 20   gathered. */.  
47cf3 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
47cf4 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 65  E_DONE;.      }e
47cf5 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
47cf6 65 72 74 28 20 69 43 75 72 72 65 6e 74 3c 3d 69  ert( iCurrent<=i
47cf7 4d 69 6e 50 6f 73 20 29 3b 0a 20 20 20 20 20 20  MinPos );.      
47cf8 20 20 69 66 28 20 30 3d 3d 28 30 78 46 45 26 2a    if( 0==(0xFE&*
47cf9 70 54 65 72 6d 2d 3e 70 4c 69 73 74 29 20 29 7b  pTerm->pList) ){
47cfa 0a 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d  .          pTerm
47cfb 2d 3e 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20  ->pList = 0;.   
47cfc 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
47cfd 20 20 20 20 20 20 66 74 73 33 47 65 74 44 65 6c        fts3GetDel
47cfe 74 61 50 6f 73 69 74 69 6f 6e 28 26 70 54 65 72  taPosition(&pTer
47cff 6d 2d 3e 70 4c 69 73 74 2c 20 26 70 54 65 72 6d  m->pList, &pTerm
47d00 2d 3e 69 50 6f 73 29 3b 0a 20 20 20 20 20 20 20  ->iPos);.       
47d01 20 7d 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65   }.        while
47d02 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
47d03 26 26 20 69 43 75 72 72 65 6e 74 3c 69 4d 69 6e  && iCurrent<iMin
47d04 50 6f 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Pos ){.         
47d05 20 72 63 20 3d 20 70 4d 6f 64 2d 3e 78 4e 65 78   rc = pMod->xNex
47d06 74 28 70 43 2c 20 26 5a 44 55 4d 4d 59 2c 20 26  t(pC, &ZDUMMY, &
47d07 4e 44 55 4d 4d 59 2c 20 26 69 53 74 61 72 74 2c  NDUMMY, &iStart,
47d08 20 26 69 45 6e 64 2c 20 26 69 43 75 72 72 65 6e   &iEnd, &iCurren
47d09 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  t);.        }.  
47d0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
47d0b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
47d0c 20 20 20 20 20 63 68 61 72 20 61 42 75 66 66 65       char aBuffe
47d0d 72 5b 36 34 5d 3b 0a 20 20 20 20 20 20 20 20 20  r[64];.         
47d0e 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
47d0f 66 28 73 69 7a 65 6f 66 28 61 42 75 66 66 65 72  f(sizeof(aBuffer
47d10 29 2c 20 61 42 75 66 66 65 72 2c 20 0a 20 20 20  ), aBuffer, .   
47d11 20 20 20 20 20 20 20 20 20 20 20 22 25 64 20 25             "%d %
47d12 64 20 25 64 20 25 64 20 22 2c 20 69 43 6f 6c 2c  d %d %d ", iCol,
47d13 20 70 54 65 72 6d 2d 73 43 74 78 2e 61 54 65 72   pTerm-sCtx.aTer
47d14 6d 2c 20 69 53 74 61 72 74 2c 20 69 45 6e 64 2d  m, iStart, iEnd-
47d15 69 53 74 61 72 74 0a 20 20 20 20 20 20 20 20 20  iStart.         
47d16 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63   );.          rc
47d17 20 3d 20 66 74 73 33 53 74 72 69 6e 67 41 70 70   = fts3StringApp
47d18 65 6e 64 28 26 72 65 73 2c 20 61 42 75 66 66 65  end(&res, aBuffe
47d19 72 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  r, -1);.        
47d1a 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51  }else if( rc==SQ
47d1b 4c 49 54 45 5f 44 4f 4e 45 20 26 26 20 70 54 61  LITE_DONE && pTa
47d1c 62 2d 3e 7a 43 6f 6e 74 65 6e 74 54 62 6c 3d 3d  b->zContentTbl==
47d1d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  0 ){.          r
47d1e 63 20 3d 20 46 54 53 5f 43 4f 52 52 55 50 54 5f  c = FTS_CORRUPT_
47d1f 56 54 41 42 3b 0a 20 20 20 20 20 20 20 20 7d 0a  VTAB;.        }.
47d20 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
47d21 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
47d22 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 72  _DONE ){.      r
47d23 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
47d24 20 20 20 7d 0a 0a 20 20 20 20 70 4d 6f 64 2d 3e     }..    pMod->
47d25 78 43 6c 6f 73 65 28 70 43 29 3b 0a 20 20 20 20  xClose(pC);.    
47d26 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
47d27 4b 20 29 20 67 6f 74 6f 20 6f 66 66 73 65 74 73  K ) goto offsets
47d28 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 6f 66 66 73  _out;.  }.. offs
47d29 65 74 73 5f 6f 75 74 3a 0a 20 20 73 71 6c 69 74  ets_out:.  sqlit
47d2a 65 33 5f 66 72 65 65 28 73 43 74 78 2e 61 54 65  e3_free(sCtx.aTe
47d2b 72 6d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  rm);.  assert( r
47d2c 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c!=SQLITE_DONE )
47d2d 3b 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 53  ;.  sqlite3Fts3S
47d2e 65 67 6d 65 6e 74 73 43 6c 6f 73 65 28 70 54 61  egmentsClose(pTa
47d2f 62 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  b);.  if( rc!=SQ
47d30 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
47d31 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
47d32 72 6f 72 5f 63 6f 64 65 28 70 43 74 78 2c 20 20  ror_code(pCtx,  
47d33 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rc);.    sqlite3
47d34 5f 66 72 65 65 28 72 65 73 2e 7a 29 3b 0a 20 20  _free(res.z);.  
47d35 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
47d36 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70  e3_result_text(p
47d37 43 74 78 2c 20 72 65 73 2e 7a 2c 20 72 65 73 2e  Ctx, res.z, res.
47d38 6e 2d 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65  n-1, sqlite3_fre
47d39 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
47d3a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
47d3b 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 61 74  mentation of mat
47d3c 63 68 69 6e 66 6f 28 29 20 66 75 6e 63 74 69 6f  chinfo() functio
47d3d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
47d3e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
47d3f 33 46 74 73 33 4d 61 74 63 68 69 6e 66 6f 28 0a  3Fts3Matchinfo(.
47d40 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
47d41 74 20 2a 70 43 6f 6e 74 65 78 74 2c 20 20 20 20  t *pContext,    
47d42 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 63 61    /* Function ca
47d43 6c 6c 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ll context */.  
47d44 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73 72  Fts3Cursor *pCsr
47d45 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
47d46 2f 2a 20 46 54 53 33 20 74 61 62 6c 65 20 63 75  /* FTS3 table cu
47d47 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rsor */.  const 
47d48 63 68 61 72 20 2a 7a 41 72 67 20 20 20 20 20 20  char *zArg      
47d49 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63            /* Sec
47d4a 6f 6e 64 20 61 72 67 20 74 6f 20 6d 61 74 63 68  ond arg to match
47d4b 69 6e 66 6f 28 29 20 66 75 6e 63 74 69 6f 6e 20  info() function 
47d4c 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 54 61 62 6c  */.){.  Fts3Tabl
47d4d 65 20 2a 70 54 61 62 20 3d 20 28 46 74 73 33 54  e *pTab = (Fts3T
47d4e 61 62 6c 65 20 2a 29 70 43 73 72 2d 3e 62 61 73  able *)pCsr->bas
47d4f 65 2e 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72  e.pVtab;.  int r
47d50 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f  c;.  int i;.  co
47d51 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
47d52 74 3b 0a 0a 20 20 69 66 28 20 7a 41 72 67 20 29  t;..  if( zArg )
47d53 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a  {.    for(i=0; z
47d54 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20  Arg[i]; i++){.  
47d55 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d      char *zErr =
47d56 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 66 74   0;.      if( ft
47d57 73 33 4d 61 74 63 68 69 6e 66 6f 43 68 65 63 6b  s3MatchinfoCheck
47d58 28 70 54 61 62 2c 20 7a 41 72 67 5b 69 5d 2c 20  (pTab, zArg[i], 
47d59 26 7a 45 72 72 29 20 29 7b 0a 20 20 20 20 20 20  &zErr) ){.      
47d5a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
47d5b 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  _error(pContext,
47d5c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 20 20   zErr, -1);.    
47d5d 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
47d5e 28 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 20 20  (zErr);.        
47d5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
47d60 20 20 20 20 7d 0a 20 20 20 20 7a 46 6f 72 6d 61      }.    zForma
47d61 74 20 3d 20 7a 41 72 67 3b 0a 20 20 7d 65 6c 73  t = zArg;.  }els
47d62 65 7b 0a 20 20 20 20 7a 46 6f 72 6d 61 74 20 3d  e{.    zFormat =
47d63 20 46 54 53 33 5f 4d 41 54 43 48 49 4e 46 4f 5f   FTS3_MATCHINFO_
47d64 44 45 46 41 55 4c 54 3b 0a 20 20 7d 0a 0a 20 20  DEFAULT;.  }..  
47d65 69 66 28 20 21 70 43 73 72 2d 3e 70 45 78 70 72  if( !pCsr->pExpr
47d66 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
47d67 72 65 73 75 6c 74 5f 62 6c 6f 62 28 70 43 6f 6e  result_blob(pCon
47d68 74 65 78 74 2c 20 22 22 2c 20 30 2c 20 53 51 4c  text, "", 0, SQL
47d69 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
47d6a 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
47d6b 2f 2a 20 52 65 74 72 69 65 76 65 20 6d 61 74 63  /* Retrieve matc
47d6c 68 69 6e 66 6f 28 29 20 64 61 74 61 2e 20 2a 2f  hinfo() data. */
47d6d 0a 20 20 72 63 20 3d 20 66 74 73 33 47 65 74 4d  .  rc = fts3GetM
47d6e 61 74 63 68 69 6e 66 6f 28 70 43 73 72 2c 20 7a  atchinfo(pCsr, z
47d6f 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 6c 69 74  Format);.  sqlit
47d70 65 33 46 74 73 33 53 65 67 6d 65 6e 74 73 43 6c  e3Fts3SegmentsCl
47d71 6f 73 65 28 70 54 61 62 29 3b 0a 0a 20 20 69 66  ose(pTab);..  if
47d72 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
47d73 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
47d74 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
47d75 28 70 43 6f 6e 74 65 78 74 2c 20 72 63 29 3b 0a  (pContext, rc);.
47d76 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
47d77 20 6e 20 3d 20 70 43 73 72 2d 3e 6e 4d 61 74 63   n = pCsr->nMatc
47d78 68 69 6e 66 6f 20 2a 20 73 69 7a 65 6f 66 28 75  hinfo * sizeof(u
47d79 33 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  32);.    sqlite3
47d7a 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 70 43 6f  _result_blob(pCo
47d7b 6e 74 65 78 74 2c 20 70 43 73 72 2d 3e 61 4d 61  ntext, pCsr->aMa
47d7c 74 63 68 69 6e 66 6f 2c 20 6e 2c 20 53 51 4c 49  tchinfo, n, SQLI
47d7d 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
47d7e 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a   }.}..#endif../*
47d7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
47d80 64 20 6f 66 20 66 74 73 33 5f 73 6e 69 70 70 65  d of fts3_snippe
47d81 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
47d82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47d83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
47d84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
47d85 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 75 6e  gin file fts3_un
47d86 69 63 6f 64 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  icode.c ********
47d87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47d88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
47d89 0a 2a 2a 20 32 30 31 32 20 4d 61 79 20 32 34 0a  .** 2012 May 24.
47d8a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
47d8b 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
47d8c 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
47d8d 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
47d8e 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
47d8f 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
47d90 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
47d91 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
47d92 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
47d93 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
47d94 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
47d95 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
47d96 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
47d97 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
47d98 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
47d99 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
47d9a 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
47d9b 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
47d9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47d9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47d9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47d9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47da0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  ***.**.** Implem
47da1 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
47da2 22 75 6e 69 63 6f 64 65 22 20 66 75 6c 6c 2d 74  "unicode" full-t
47da3 65 78 74 2d 73 65 61 72 63 68 20 74 6f 6b 65 6e  ext-search token
47da4 69 7a 65 72 2e 0a 2a 2f 0a 0a 23 69 66 64 65 66  izer..*/..#ifdef
47da5 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46   SQLITE_ENABLE_F
47da6 54 53 34 5f 55 4e 49 43 4f 44 45 36 31 0a 0a 23  TS4_UNICODE61..#
47da7 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
47da8 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
47da9 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
47daa 45 5f 46 54 53 33 29 0a 0a 2f 2a 20 23 69 6e 63  E_FTS3)../* #inc
47dab 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 20  lude <assert.h> 
47dac 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c  */./* #include <
47dad 73 74 64 6c 69 62 2e 68 3e 20 2a 2f 0a 2f 2a 20  stdlib.h> */./* 
47dae 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e  #include <stdio.
47daf 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64  h> */./* #includ
47db0 65 20 3c 73 74 72 69 6e 67 2e 68 3e 20 2a 2f 0a  e <string.h> */.
47db1 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
47db2 6f 77 69 6e 67 20 74 77 6f 20 6d 61 63 72 6f 73  owing two macros
47db3 20 2d 20 52 45 41 44 5f 55 54 46 38 20 61 6e 64   - READ_UTF8 and
47db4 20 57 52 49 54 45 5f 55 54 46 38 20 2d 20 68 61   WRITE_UTF8 - ha
47db5 76 65 20 62 65 65 6e 20 63 6f 70 69 65 64 0a 2a  ve been copied.*
47db6 2a 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74  * from the sqlit
47db7 65 33 20 73 6f 75 72 63 65 20 66 69 6c 65 20 75  e3 source file u
47db8 74 66 2e 63 2e 20 49 66 20 74 68 69 73 20 66 69  tf.c. If this fi
47db9 6c 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 61  le is compiled a
47dba 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65  s part.** of the
47dbb 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2c 20 74   amalgamation, t
47dbc 68 65 79 20 61 72 65 20 6e 6f 74 20 72 65 71 75  hey are not requ
47dbd 69 72 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  ired..*/.#ifndef
47dbe 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41   SQLITE_AMALGAMA
47dbf 54 49 4f 4e 0a 0a 73 74 61 74 69 63 20 63 6f 6e  TION..static con
47dc0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
47dc1 20 73 71 6c 69 74 65 33 55 74 66 38 54 72 61 6e   sqlite3Utf8Tran
47dc2 73 31 5b 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c  s1[] = {.  0x00,
47dc3 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30   0x01, 0x02, 0x0
47dc4 33 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30  3, 0x04, 0x05, 0
47dc5 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30  x06, 0x07,.  0x0
47dc6 38 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30  8, 0x09, 0x0a, 0
47dc7 78 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c  x0b, 0x0c, 0x0d,
47dc8 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30   0x0e, 0x0f,.  0
47dc9 78 31 30 2c 20 30 78 31 31 2c 20 30 78 31 32 2c  x10, 0x11, 0x12,
47dca 20 30 78 31 33 2c 20 30 78 31 34 2c 20 30 78 31   0x13, 0x14, 0x1
47dcb 35 2c 20 30 78 31 36 2c 20 30 78 31 37 2c 0a 20  5, 0x16, 0x17,. 
47dcc 20 30 78 31 38 2c 20 30 78 31 39 2c 20 30 78 31   0x18, 0x19, 0x1
47dcd 61 2c 20 30 78 31 62 2c 20 30 78 31 63 2c 20 30  a, 0x1b, 0x1c, 0
47dce 78 31 64 2c 20 30 78 31 65 2c 20 30 78 31 66 2c  x1d, 0x1e, 0x1f,
47dcf 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30  .  0x00, 0x01, 0
47dd0 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c  x02, 0x03, 0x04,
47dd1 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30   0x05, 0x06, 0x0
47dd2 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c  7,.  0x08, 0x09,
47dd3 20 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30   0x0a, 0x0b, 0x0
47dd4 63 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30  c, 0x0d, 0x0e, 0
47dd5 78 30 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 30  x0f,.  0x00, 0x0
47dd6 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30  1, 0x02, 0x03, 0
47dd7 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c  x04, 0x05, 0x06,
47dd8 20 30 78 30 37 2c 0a 20 20 30 78 30 30 2c 20 30   0x07,.  0x00, 0
47dd9 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c  x01, 0x02, 0x03,
47dda 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30   0x00, 0x01, 0x0
47ddb 30 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a 23 64 65  0, 0x00,.};..#de
47ddc 66 69 6e 65 20 52 45 41 44 5f 55 54 46 38 28 7a  fine READ_UTF8(z
47ddd 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 20 20 20  In, zTerm, c)   
47dde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ddf 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20          \.  c = 
47de0 2a 28 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20  *(zIn++);       
47de1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47de2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47de3 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d       \.  if( c>=
47de4 30 78 63 30 20 29 7b 20 20 20 20 20 20 20 20 20  0xc0 ){         
47de5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47de6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47de7 20 20 5c 0a 20 20 20 20 63 20 3d 20 73 71 6c 69    \.    c = sqli
47de8 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b 63 2d  te3Utf8Trans1[c-
47de9 30 78 63 30 5d 3b 20 20 20 20 20 20 20 20 20 20  0xc0];          
47dea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
47deb 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 21  .    while( zIn!
47dec 3d 7a 54 65 72 6d 20 26 26 20 28 2a 7a 49 6e 20  =zTerm && (*zIn 
47ded 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b  & 0xc0)==0x80 ){
47dee 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
47def 20 20 20 20 63 20 3d 20 28 63 3c 3c 36 29 20 2b      c = (c<<6) +
47df0 20 28 30 78 33 66 20 26 20 2a 28 7a 49 6e 2b 2b   (0x3f & *(zIn++
47df1 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ));             
47df2 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 7d           \.    }
47df3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47df4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47df5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47df6 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66 28 20        \.    if( 
47df7 63 3c 30 78 38 30 20 20 20 20 20 20 20 20 20 20  c<0x80          
47df8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47df9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47dfa 20 20 20 5c 0a 20 20 20 20 20 20 20 20 7c 7c 20     \.        || 
47dfb 28 63 26 30 78 46 46 46 46 46 38 30 30 29 3d 3d  (c&0xFFFFF800)==
47dfc 30 78 44 38 30 30 20 20 20 20 20 20 20 20 20 20  0xD800          
47dfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47dfe 5c 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26  \.        || (c&
47dff 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46  0xFFFFFFFE)==0xF
47e00 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78 46 46  FFE ){  c = 0xFF
47e01 46 44 3b 20 7d 20 20 20 20 20 20 20 20 5c 0a 20  FD; }        \. 
47e02 20 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54   }..#define WRIT
47e03 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 20  E_UTF8(zOut, c) 
47e04 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
47e05 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69             \.  i
47e06 66 28 20 63 3c 30 78 30 30 30 38 30 20 29 7b 20  f( c<0x00080 ){ 
47e07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e09 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
47e0a 2b 20 3d 20 28 75 38 29 28 63 26 30 78 46 46 29  + = (u8)(c&0xFF)
47e0b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
47e0c 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
47e0d 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20   }              
47e0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e10 20 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 69        \.  else i
47e11 66 28 20 63 3c 30 78 30 30 38 30 30 20 29 7b 20  f( c<0x00800 ){ 
47e12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
47e14 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30  .    *zOut++ = 0
47e15 78 43 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 36  xC0 + (u8)((c>>6
47e16 29 26 30 78 31 46 29 3b 20 20 20 20 20 20 20 20  )&0x1F);        
47e17 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
47e18 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28  Out++ = 0x80 + (
47e19 75 38 29 28 63 20 26 20 30 78 33 46 29 3b 20 20  u8)(c & 0x3F);  
47e1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e1b 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20   \.  }          
47e1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e1e 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 65 6c            \.  el
47e1f 73 65 20 69 66 28 20 63 3c 30 78 31 30 30 30 30  se if( c<0x10000
47e20 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20   ){             
47e21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e22 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
47e23 20 3d 20 30 78 45 30 20 2b 20 28 75 38 29 28 28   = 0xE0 + (u8)((
47e24 63 3e 3e 31 32 29 26 30 78 30 46 29 3b 20 20 20  c>>12)&0x0F);   
47e25 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
47e26 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30    *zOut++ = 0x80
47e27 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 20 26   + (u8)((c>>6) &
47e28 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20   0x3F);         
47e29 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
47e2a 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29  ++ = 0x80 + (u8)
47e2b 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20 20  (c & 0x3F);     
47e2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
47e2d 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20    }else{        
47e2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e30 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
47e31 75 74 2b 2b 20 3d 20 30 78 46 30 20 2b 20 28 75  ut++ = 0xF0 + (u
47e32 38 29 28 28 63 3e 3e 31 38 29 20 26 20 30 78 30  8)((c>>18) & 0x0
47e33 37 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  7);             
47e34 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
47e35 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e 3e  0x80 + (u8)((c>>
47e36 31 32 29 20 26 20 30 78 33 46 29 3b 20 20 20 20  12) & 0x3F);    
47e37 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
47e38 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20  zOut++ = 0x80 + 
47e39 28 75 38 29 28 28 63 3e 3e 36 29 20 26 20 30 78  (u8)((c>>6) & 0x
47e3a 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3F);            
47e3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
47e3c 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 20  = 0x80 + (u8)(c 
47e3d 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20  & 0x3F);        
47e3e 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
47e3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e42 20 20 20 20 5c 0a 7d 0a 0a 23 65 6e 64 69 66 20      \.}..#endif 
47e43 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /* ifndef SQLITE
47e44 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f  _AMALGAMATION */
47e45 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
47e46 20 75 6e 69 63 6f 64 65 5f 74 6f 6b 65 6e 69 7a   unicode_tokeniz
47e47 65 72 20 75 6e 69 63 6f 64 65 5f 74 6f 6b 65 6e  er unicode_token
47e48 69 7a 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74  izer;.typedef st
47e49 72 75 63 74 20 75 6e 69 63 6f 64 65 5f 63 75 72  ruct unicode_cur
47e4a 73 6f 72 20 75 6e 69 63 6f 64 65 5f 63 75 72 73  sor unicode_curs
47e4b 6f 72 3b 0a 0a 73 74 72 75 63 74 20 75 6e 69 63  or;..struct unic
47e4c 6f 64 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a  ode_tokenizer {.
47e4d 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
47e4e 7a 65 72 20 62 61 73 65 3b 0a 20 20 69 6e 74 20  zer base;.  int 
47e4f 62 52 65 6d 6f 76 65 44 69 61 63 72 69 74 69 63  bRemoveDiacritic
47e50 3b 0a 20 20 69 6e 74 20 6e 45 78 63 65 70 74 69  ;.  int nExcepti
47e51 6f 6e 3b 0a 20 20 69 6e 74 20 2a 61 69 45 78 63  on;.  int *aiExc
47e52 65 70 74 69 6f 6e 3b 0a 7d 3b 0a 0a 73 74 72 75  eption;.};..stru
47e53 63 74 20 75 6e 69 63 6f 64 65 5f 63 75 72 73 6f  ct unicode_curso
47e54 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  r {.  sqlite3_to
47e55 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 62  kenizer_cursor b
47e56 61 73 65 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  ase;.  const uns
47e57 69 67 6e 65 64 20 63 68 61 72 20 2a 61 49 6e 70  igned char *aInp
47e58 75 74 3b 20 20 20 20 2f 2a 20 49 6e 70 75 74 20  ut;    /* Input 
47e59 74 65 78 74 20 62 65 69 6e 67 20 74 6f 6b 65 6e  text being token
47e5a 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 49  ized */.  int nI
47e5b 6e 70 75 74 3b 20 20 20 20 20 20 20 20 20 20 20  nput;           
47e5c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
47e5d 65 20 6f 66 20 61 49 6e 70 75 74 5b 5d 20 69 6e  e of aInput[] in
47e5e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20   bytes */.  int 
47e5f 69 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20  iOff;           
47e60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
47e61 75 72 72 65 6e 74 20 6f 66 66 73 65 74 20 77 69  urrent offset wi
47e62 74 68 69 6e 20 61 49 6e 70 75 74 5b 5d 20 2a 2f  thin aInput[] */
47e63 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 20 20  .  int iToken;  
47e64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e65 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6e     /* Index of n
47e66 65 78 74 20 74 6f 6b 65 6e 20 74 6f 20 62 65 20  ext token to be 
47e67 72 65 74 75 72 6e 65 64 20 2a 2f 0a 20 20 63 68  returned */.  ch
47e68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 20  ar *zToken;     
47e69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
47e6a 20 73 74 6f 72 61 67 65 20 66 6f 72 20 63 75 72   storage for cur
47e6b 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  rent token */.  
47e6c 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20  int nAlloc;     
47e6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e6e 2f 2a 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74  /* space allocat
47e6f 65 64 20 61 74 20 7a 54 6f 6b 65 6e 20 2a 2f 0a  ed at zToken */.
47e70 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72  };.../*.** Destr
47e71 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 61  oy a tokenizer a
47e72 6c 6c 6f 63 61 74 65 64 20 62 79 20 75 6e 69 63  llocated by unic
47e73 6f 64 65 43 72 65 61 74 65 28 29 2e 0a 2a 2f 0a  odeCreate()..*/.
47e74 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 63 6f  static int unico
47e75 64 65 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65  deDestroy(sqlite
47e76 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
47e77 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 69 66 28 20  kenizer){.  if( 
47e78 70 54 6f 6b 65 6e 69 7a 65 72 20 29 7b 0a 20 20  pTokenizer ){.  
47e79 20 20 75 6e 69 63 6f 64 65 5f 74 6f 6b 65 6e 69    unicode_tokeni
47e7a 7a 65 72 20 2a 70 20 3d 20 28 75 6e 69 63 6f 64  zer *p = (unicod
47e7b 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 54  e_tokenizer *)pT
47e7c 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 20 20 73 71  okenizer;.    sq
47e7d 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 69  lite3_free(p->ai
47e7e 45 78 63 65 70 74 69 6f 6e 29 3b 0a 20 20 20 20  Exception);.    
47e7f 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
47e80 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
47e81 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
47e82 2a 20 41 73 20 70 61 72 74 20 6f 66 20 61 20 74  * As part of a t
47e83 6f 6b 65 6e 63 68 61 72 73 3d 20 6f 72 20 73 65  okenchars= or se
47e84 70 61 72 61 74 6f 72 73 3d 20 6f 70 74 69 6f 6e  parators= option
47e85 2c 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52  , the CREATE VIR
47e86 54 55 41 4c 20 54 41 42 4c 45 0a 2a 2a 20 73 74  TUAL TABLE.** st
47e87 61 74 65 6d 65 6e 74 20 68 61 73 20 73 70 65 63  atement has spec
47e88 69 66 69 65 64 20 74 68 61 74 20 74 68 65 20 74  ified that the t
47e89 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 74 68 69  okenizer for thi
47e8a 73 20 74 61 62 6c 65 20 73 68 61 6c 6c 20 63 6f  s table shall co
47e8b 6e 73 69 64 65 72 0a 2a 2a 20 61 6c 6c 20 63 68  nsider.** all ch
47e8c 61 72 61 63 74 65 72 73 20 69 6e 20 73 74 72 69  aracters in stri
47e8d 6e 67 20 7a 49 6e 2f 6e 49 6e 20 74 6f 20 62 65  ng zIn/nIn to be
47e8e 20 73 65 70 61 72 61 74 6f 72 73 20 28 69 66 20   separators (if 
47e8f 62 41 6c 6e 75 6d 3d 3d 30 29 20 6f 72 0a 2a 2a  bAlnum==0) or.**
47e90 20 74 6f 6b 65 6e 20 63 68 61 72 61 63 74 65 72   token character
47e91 73 20 28 69 66 20 62 41 6c 6e 75 6d 3d 3d 31 29  s (if bAlnum==1)
47e92 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68  ..**.** For each
47e93 20 63 6f 64 65 70 6f 69 6e 74 20 69 6e 20 74 68   codepoint in th
47e94 65 20 7a 49 6e 2f 6e 49 6e 20 73 74 72 69 6e 67  e zIn/nIn string
47e95 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
47e96 63 68 65 63 6b 73 20 69 66 20 74 68 65 0a 2a 2a  checks if the.**
47e97 20 73 71 6c 69 74 65 33 46 74 73 55 6e 69 63 6f   sqlite3FtsUnico
47e98 64 65 49 73 61 6c 6e 75 6d 28 29 20 66 75 6e 63  deIsalnum() func
47e99 74 69 6f 6e 20 61 6c 72 65 61 64 79 20 72 65 74  tion already ret
47e9a 75 72 6e 73 20 74 68 65 20 64 65 73 69 72 65 64  urns the desired
47e9b 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 73   result..** If s
47e9c 6f 2c 20 6e 6f 20 61 63 74 69 6f 6e 20 69 73 20  o, no action is 
47e9d 74 61 6b 65 6e 2e 20 4f 74 68 65 72 77 69 73 65  taken. Otherwise
47e9e 2c 20 74 68 65 20 63 6f 64 65 70 6f 69 6e 74 20  , the codepoint 
47e9f 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  is added to the 
47ea0 0a 2a 2a 20 75 6e 69 63 6f 64 65 5f 74 6f 6b 65  .** unicode_toke
47ea1 6e 69 7a 65 72 2e 61 69 45 78 63 65 70 74 69 6f  nizer.aiExceptio
47ea2 6e 5b 5d 20 61 72 72 61 79 2e 20 46 6f 72 20 74  n[] array. For t
47ea3 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74  he purposes of t
47ea4 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2c 0a 2a 2a 20  okenization,.** 
47ea5 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
47ea6 20 6f 66 20 73 71 6c 69 74 65 33 46 74 73 55 6e   of sqlite3FtsUn
47ea7 69 63 6f 64 65 49 73 61 6c 6e 75 6d 28 29 20 69  icodeIsalnum() i
47ea8 73 20 69 6e 76 65 72 74 65 64 20 66 6f 72 20 61  s inverted for a
47ea9 6c 6c 0a 2a 2a 20 63 6f 64 65 70 6f 69 6e 74 73  ll.** codepoints
47eaa 20 69 6e 20 74 68 65 20 61 69 45 78 63 65 70 74   in the aiExcept
47eab 69 6f 6e 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a  ion[] array..**.
47eac 2a 2a 20 49 66 20 61 20 73 74 61 6e 64 61 6c 6f  ** If a standalo
47ead 6e 65 20 64 69 61 63 72 69 74 69 63 20 6d 61 72  ne diacritic mar
47eae 6b 20 28 6f 6e 65 20 74 68 61 74 20 73 71 6c 69  k (one that sqli
47eaf 74 65 33 46 74 73 55 6e 69 63 6f 64 65 49 73 64  te3FtsUnicodeIsd
47eb0 69 61 63 72 69 74 69 63 28 29 0a 2a 2a 20 69 64  iacritic().** id
47eb1 65 6e 74 69 66 69 65 73 20 61 73 20 61 20 64 69  entifies as a di
47eb2 61 63 72 69 74 69 63 29 20 6f 63 63 75 72 73 20  acritic) occurs 
47eb3 69 6e 20 74 68 65 20 7a 49 6e 2f 6e 49 6e 20 73  in the zIn/nIn s
47eb4 74 72 69 6e 67 20 69 74 20 69 73 20 69 67 6e 6f  tring it is igno
47eb5 72 65 64 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f  red..** It is no
47eb6 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 68  t possible to ch
47eb7 61 6e 67 65 20 74 68 65 20 62 65 68 61 76 69 6f  ange the behavio
47eb8 75 72 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 69  ur of the tokeni
47eb9 7a 65 72 20 77 69 74 68 20 72 65 73 70 65 63 74  zer with respect
47eba 0a 2a 2a 20 74 6f 20 74 68 65 73 65 20 63 6f 64  .** to these cod
47ebb 65 70 6f 69 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74  epoints..*/.stat
47ebc 69 63 20 69 6e 74 20 75 6e 69 63 6f 64 65 41 64  ic int unicodeAd
47ebd 64 45 78 63 65 70 74 69 6f 6e 73 28 0a 20 20 75  dExceptions(.  u
47ebe 6e 69 63 6f 64 65 5f 74 6f 6b 65 6e 69 7a 65 72  nicode_tokenizer
47ebf 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 2f   *p,           /
47ec0 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 74 6f 20 61  * Tokenizer to a
47ec1 64 64 20 65 78 63 65 70 74 69 6f 6e 73 20 74 6f  dd exceptions to
47ec2 20 2a 2f 0a 20 20 69 6e 74 20 62 41 6c 6e 75 6d   */.  int bAlnum
47ec3 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
47ec4 20 20 20 20 20 20 2f 2a 20 52 65 70 6c 61 63 65        /* Replace
47ec5 20 49 73 61 6c 6e 75 6d 28 29 20 72 65 74 75 72   Isalnum() retur
47ec6 6e 20 76 61 6c 75 65 20 77 69 74 68 20 74 68 69  n value with thi
47ec7 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
47ec8 72 20 2a 7a 49 6e 2c 20 20 20 20 20 20 20 20 20  r *zIn,         
47ec9 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
47eca 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 74 6f  of characters to
47ecb 20 6d 61 6b 65 20 65 78 63 65 70 74 69 6f 6e 73   make exceptions
47ecc 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 20 20 20   */.  int nIn   
47ecd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ece 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
47ecf 6f 66 20 7a 20 69 6e 20 62 79 74 65 73 20 2a 2f  of z in bytes */
47ed0 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  .){.  const unsi
47ed1 67 6e 65 64 20 63 68 61 72 20 2a 7a 20 3d 20 28  gned char *z = (
47ed2 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
47ed3 68 61 72 20 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e  har *)zIn;.  con
47ed4 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
47ed5 20 2a 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 49 6e   *zTerm = &z[nIn
47ed6 5d 3b 0a 20 20 69 6e 74 20 69 43 6f 64 65 3b 0a  ];.  int iCode;.
47ed7 20 20 69 6e 74 20 6e 45 6e 74 72 79 20 3d 20 30    int nEntry = 0
47ed8 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 62 41 6c  ;..  assert( bAl
47ed9 6e 75 6d 3d 3d 30 20 7c 7c 20 62 41 6c 6e 75 6d  num==0 || bAlnum
47eda 3d 3d 31 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28  ==1 );..  while(
47edb 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20   z<zTerm ){.    
47edc 52 45 41 44 5f 55 54 46 38 28 7a 2c 20 7a 54 65  READ_UTF8(z, zTe
47edd 72 6d 2c 20 69 43 6f 64 65 29 3b 0a 20 20 20 20  rm, iCode);.    
47ede 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 65 33  assert( (sqlite3
47edf 46 74 73 55 6e 69 63 6f 64 65 49 73 61 6c 6e 75  FtsUnicodeIsalnu
47ee0 6d 28 69 43 6f 64 65 29 20 26 20 30 78 46 46 46  m(iCode) & 0xFFF
47ee1 46 46 46 46 45 29 3d 3d 30 20 29 3b 0a 20 20 20  FFFFE)==0 );.   
47ee2 20 69 66 28 20 73 71 6c 69 74 65 33 46 74 73 55   if( sqlite3FtsU
47ee3 6e 69 63 6f 64 65 49 73 61 6c 6e 75 6d 28 69 43  nicodeIsalnum(iC
47ee4 6f 64 65 29 21 3d 62 41 6c 6e 75 6d 20 0a 20 20  ode)!=bAlnum .  
47ee5 20 20 20 26 26 20 73 71 6c 69 74 65 33 46 74 73     && sqlite3Fts
47ee6 55 6e 69 63 6f 64 65 49 73 64 69 61 63 72 69 74  UnicodeIsdiacrit
47ee7 69 63 28 69 43 6f 64 65 29 3d 3d 30 20 0a 20 20  ic(iCode)==0 .  
47ee8 20 20 29 7b 0a 20 20 20 20 20 20 6e 45 6e 74 72    ){.      nEntr
47ee9 79 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  y++;.    }.  }..
47eea 20 20 69 66 28 20 6e 45 6e 74 72 79 20 29 7b 0a    if( nEntry ){.
47eeb 20 20 20 20 69 6e 74 20 2a 61 4e 65 77 3b 20 20      int *aNew;  
47eec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47eed 20 20 2f 2a 20 4e 65 77 20 61 69 45 78 63 65 70    /* New aiExcep
47eee 74 69 6f 6e 5b 5d 20 61 72 72 61 79 20 2a 2f 0a  tion[] array */.
47eef 20 20 20 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20      int nNew;   
47ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ef1 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76    /* Number of v
47ef2 61 6c 69 64 20 65 6e 74 72 69 65 73 20 69 6e 20  alid entries in 
47ef3 61 72 72 61 79 20 61 4e 65 77 5b 5d 20 2a 2f 0a  array aNew[] */.
47ef4 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69  .    aNew = sqli
47ef5 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 2d 3e 61  te3_realloc(p->a
47ef6 69 45 78 63 65 70 74 69 6f 6e 2c 20 28 70 2d 3e  iException, (p->
47ef7 6e 45 78 63 65 70 74 69 6f 6e 2b 6e 45 6e 74 72  nException+nEntr
47ef8 79 29 2a 73 69 7a 65 6f 66 28 69 6e 74 29 29 3b  y)*sizeof(int));
47ef9 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30  .    if( aNew==0
47efa 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
47efb 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 6e 4e 65 77  _NOMEM;.    nNew
47efc 20 3d 20 70 2d 3e 6e 45 78 63 65 70 74 69 6f 6e   = p->nException
47efd 3b 0a 0a 20 20 20 20 7a 20 3d 20 28 63 6f 6e 73  ;..    z = (cons
47efe 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
47eff 2a 29 7a 49 6e 3b 0a 20 20 20 20 77 68 69 6c 65  *)zIn;.    while
47f00 28 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  ( z<zTerm ){.   
47f01 20 20 20 52 45 41 44 5f 55 54 46 38 28 7a 2c 20     READ_UTF8(z, 
47f02 7a 54 65 72 6d 2c 20 69 43 6f 64 65 29 3b 0a 20  zTerm, iCode);. 
47f03 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
47f04 46 74 73 55 6e 69 63 6f 64 65 49 73 61 6c 6e 75  FtsUnicodeIsalnu
47f05 6d 28 69 43 6f 64 65 29 21 3d 62 41 6c 6e 75 6d  m(iCode)!=bAlnum
47f06 20 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c 69   .       && sqli
47f07 74 65 33 46 74 73 55 6e 69 63 6f 64 65 49 73 64  te3FtsUnicodeIsd
47f08 69 61 63 72 69 74 69 63 28 69 43 6f 64 65 29 3d  iacritic(iCode)=
47f09 3d 30 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  =0.      ){.    
47f0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20      int i, j;.  
47f0b 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
47f0c 3c 6e 4e 65 77 20 26 26 20 61 4e 65 77 5b 69 5d  <nNew && aNew[i]
47f0d 3c 69 43 6f 64 65 3b 20 69 2b 2b 29 3b 0a 20 20  <iCode; i++);.  
47f0e 20 20 20 20 20 20 66 6f 72 28 6a 3d 6e 4e 65 77        for(j=nNew
47f0f 3b 20 6a 3e 69 3b 20 6a 2d 2d 29 20 61 4e 65 77  ; j>i; j--) aNew
47f10 5b 6a 5d 20 3d 20 61 4e 65 77 5b 6a 2d 31 5d 3b  [j] = aNew[j-1];
47f11 0a 20 20 20 20 20 20 20 20 61 4e 65 77 5b 69 5d  .        aNew[i]
47f12 20 3d 20 69 43 6f 64 65 3b 0a 20 20 20 20 20 20   = iCode;.      
47f13 20 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 20 20    nNew++;.      
47f14 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61  }.    }.    p->a
47f15 69 45 78 63 65 70 74 69 6f 6e 20 3d 20 61 4e 65  iException = aNe
47f16 77 3b 0a 20 20 20 20 70 2d 3e 6e 45 78 63 65 70  w;.    p->nExcep
47f17 74 69 6f 6e 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d  tion = nNew;.  }
47f18 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
47f19 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  E_OK;.}../*.** R
47f1a 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68  eturn true if th
47f1b 65 20 70 2d 3e 61 69 45 78 63 65 70 74 69 6f 6e  e p->aiException
47f1c 5b 5d 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e  [] array contain
47f1d 73 20 74 68 65 20 76 61 6c 75 65 20 69 43 6f 64  s the value iCod
47f1e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
47f1f 20 75 6e 69 63 6f 64 65 49 73 45 78 63 65 70 74   unicodeIsExcept
47f20 69 6f 6e 28 75 6e 69 63 6f 64 65 5f 74 6f 6b 65  ion(unicode_toke
47f21 6e 69 7a 65 72 20 2a 70 2c 20 69 6e 74 20 69 43  nizer *p, int iC
47f22 6f 64 65 29 7b 0a 20 20 69 66 28 20 70 2d 3e 6e  ode){.  if( p->n
47f23 45 78 63 65 70 74 69 6f 6e 3e 30 20 29 7b 0a 20  Exception>0 ){. 
47f24 20 20 20 69 6e 74 20 2a 61 20 3d 20 70 2d 3e 61     int *a = p->a
47f25 69 45 78 63 65 70 74 69 6f 6e 3b 0a 20 20 20 20  iException;.    
47f26 69 6e 74 20 69 4c 6f 20 3d 20 30 3b 0a 20 20 20  int iLo = 0;.   
47f27 20 69 6e 74 20 69 48 69 20 3d 20 70 2d 3e 6e 45   int iHi = p->nE
47f28 78 63 65 70 74 69 6f 6e 2d 31 3b 0a 0a 20 20 20  xception-1;..   
47f29 20 77 68 69 6c 65 28 20 69 48 69 3e 3d 69 4c 6f   while( iHi>=iLo
47f2a 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 54   ){.      int iT
47f2b 65 73 74 20 3d 20 28 69 48 69 20 2b 20 69 4c 6f  est = (iHi + iLo
47f2c 29 20 2f 20 32 3b 0a 20 20 20 20 20 20 69 66 28  ) / 2;.      if(
47f2d 20 69 43 6f 64 65 3d 3d 61 5b 69 54 65 73 74 5d   iCode==a[iTest]
47f2e 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
47f2f 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rn 1;.      }els
47f30 65 20 69 66 28 20 69 43 6f 64 65 3e 61 5b 69 54  e if( iCode>a[iT
47f31 65 73 74 5d 20 29 7b 0a 20 20 20 20 20 20 20 20  est] ){.        
47f32 69 4c 6f 20 3d 20 69 54 65 73 74 2b 31 3b 0a 20  iLo = iTest+1;. 
47f33 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
47f34 20 20 20 20 69 48 69 20 3d 20 69 54 65 73 74 2d      iHi = iTest-
47f35 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
47f36 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 30  .  }..  return 0
47f37 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
47f38 6e 20 74 72 75 65 20 69 66 2c 20 66 6f 72 20 74  n true if, for t
47f39 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74  he purposes of t
47f3a 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2c 20 63 6f 64  okenization, cod
47f3b 65 70 6f 69 6e 74 20 69 43 6f 64 65 20 69 73 0a  epoint iCode is.
47f3c 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20  ** considered a 
47f3d 74 6f 6b 65 6e 20 63 68 61 72 61 63 74 65 72 20  token character 
47f3e 28 6e 6f 74 20 61 20 73 65 70 61 72 61 74 6f 72  (not a separator
47f3f 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
47f40 20 75 6e 69 63 6f 64 65 49 73 41 6c 6e 75 6d 28   unicodeIsAlnum(
47f41 75 6e 69 63 6f 64 65 5f 74 6f 6b 65 6e 69 7a 65  unicode_tokenize
47f42 72 20 2a 70 2c 20 69 6e 74 20 69 43 6f 64 65 29  r *p, int iCode)
47f43 7b 0a 20 20 61 73 73 65 72 74 28 20 28 73 71 6c  {.  assert( (sql
47f44 69 74 65 33 46 74 73 55 6e 69 63 6f 64 65 49 73  ite3FtsUnicodeIs
47f45 61 6c 6e 75 6d 28 69 43 6f 64 65 29 20 26 20 30  alnum(iCode) & 0
47f46 78 46 46 46 46 46 46 46 45 29 3d 3d 30 20 29 3b  xFFFFFFFE)==0 );
47f47 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
47f48 33 46 74 73 55 6e 69 63 6f 64 65 49 73 61 6c 6e  3FtsUnicodeIsaln
47f49 75 6d 28 69 43 6f 64 65 29 20 5e 20 75 6e 69 63  um(iCode) ^ unic
47f4a 6f 64 65 49 73 45 78 63 65 70 74 69 6f 6e 28 70  odeIsException(p
47f4b 2c 20 69 43 6f 64 65 29 3b 0a 7d 0a 0a 2f 2a 0a  , iCode);.}../*.
47f4c 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
47f4d 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e  tokenizer instan
47f4e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ce..*/.static in
47f4f 74 20 75 6e 69 63 6f 64 65 43 72 65 61 74 65 28  t unicodeCreate(
47f50 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20  .  int nArg,    
47f51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f52 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72     /* Size of ar
47f53 72 61 79 20 61 72 67 76 5b 5d 20 2a 2f 0a 20 20  ray argv[] */.  
47f54 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e  const char * con
47f55 73 74 20 2a 61 7a 41 72 67 2c 20 20 20 20 20 20  st *azArg,      
47f56 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 63 72 65  /* Tokenizer cre
47f57 61 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20  ation arguments 
47f58 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
47f59 65 6e 69 7a 65 72 20 2a 2a 70 70 20 20 20 20 20  enizer **pp     
47f5a 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 65 77       /* OUT: New
47f5b 20 74 6f 6b 65 6e 69 7a 65 72 20 68 61 6e 64 6c   tokenizer handl
47f5c 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 63 6f 64  e */.){.  unicod
47f5d 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 4e 65  e_tokenizer *pNe
47f5e 77 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77  w;        /* New
47f5f 20 74 6f 6b 65 6e 69 7a 65 72 20 6f 62 6a 65 63   tokenizer objec
47f60 74 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 0a 20 20  t */.  int i;.  
47f61 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
47f62 4f 4b 3b 0a 0a 20 20 70 4e 65 77 20 3d 20 28 75  OK;..  pNew = (u
47f63 6e 69 63 6f 64 65 5f 74 6f 6b 65 6e 69 7a 65 72  nicode_tokenizer
47f64 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   *) sqlite3_mall
47f65 6f 63 28 73 69 7a 65 6f 66 28 75 6e 69 63 6f 64  oc(sizeof(unicod
47f66 65 5f 74 6f 6b 65 6e 69 7a 65 72 29 29 3b 0a 20  e_tokenizer));. 
47f67 20 69 66 28 20 70 4e 65 77 3d 3d 4e 55 4c 4c 20   if( pNew==NULL 
47f68 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
47f69 4e 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65 74 28  NOMEM;.  memset(
47f6a 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pNew, 0, sizeof(
47f6b 75 6e 69 63 6f 64 65 5f 74 6f 6b 65 6e 69 7a 65  unicode_tokenize
47f6c 72 29 29 3b 0a 20 20 70 4e 65 77 2d 3e 62 52 65  r));.  pNew->bRe
47f6d 6d 6f 76 65 44 69 61 63 72 69 74 69 63 20 3d 20  moveDiacritic = 
47f6e 31 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72  1;..  for(i=0; r
47f6f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
47f70 69 3c 6e 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20  i<nArg; i++){.  
47f71 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
47f72 3d 20 61 7a 41 72 67 5b 69 5d 3b 0a 20 20 20 20  = azArg[i];.    
47f73 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a  int n = strlen(z
47f74 29 3b 0a 0a 20 20 20 20 69 66 28 20 6e 3d 3d 31  );..    if( n==1
47f75 39 20 26 26 20 6d 65 6d 63 6d 70 28 22 72 65 6d  9 && memcmp("rem
47f76 6f 76 65 5f 64 69 61 63 72 69 74 69 63 73 3d 31  ove_diacritics=1
47f77 22 2c 20 7a 2c 20 31 39 29 3d 3d 30 20 29 7b 0a  ", z, 19)==0 ){.
47f78 20 20 20 20 20 20 70 4e 65 77 2d 3e 62 52 65 6d        pNew->bRem
47f79 6f 76 65 44 69 61 63 72 69 74 69 63 20 3d 20 31  oveDiacritic = 1
47f7a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65  ;.    }.    else
47f7b 20 69 66 28 20 6e 3d 3d 31 39 20 26 26 20 6d 65   if( n==19 && me
47f7c 6d 63 6d 70 28 22 72 65 6d 6f 76 65 5f 64 69 61  mcmp("remove_dia
47f7d 63 72 69 74 69 63 73 3d 30 22 2c 20 7a 2c 20 31  critics=0", z, 1
47f7e 39 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  9)==0 ){.      p
47f7f 4e 65 77 2d 3e 62 52 65 6d 6f 76 65 44 69 61 63  New->bRemoveDiac
47f80 72 69 74 69 63 20 3d 20 30 3b 0a 20 20 20 20 7d  ritic = 0;.    }
47f81 0a 20 20 20 20 65 6c 73 65 20 69 66 28 20 6e 3e  .    else if( n>
47f82 3d 31 31 20 26 26 20 6d 65 6d 63 6d 70 28 22 74  =11 && memcmp("t
47f83 6f 6b 65 6e 63 68 61 72 73 3d 22 2c 20 7a 2c 20  okenchars=", z, 
47f84 31 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  11)==0 ){.      
47f85 72 63 20 3d 20 75 6e 69 63 6f 64 65 41 64 64 45  rc = unicodeAddE
47f86 78 63 65 70 74 69 6f 6e 73 28 70 4e 65 77 2c 20  xceptions(pNew, 
47f87 31 2c 20 26 7a 5b 31 31 5d 2c 20 6e 2d 31 31 29  1, &z[11], n-11)
47f88 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65  ;.    }.    else
47f89 20 69 66 28 20 6e 3e 3d 31 31 20 26 26 20 6d 65   if( n>=11 && me
47f8a 6d 63 6d 70 28 22 73 65 70 61 72 61 74 6f 72 73  mcmp("separators
47f8b 3d 22 2c 20 7a 2c 20 31 31 29 3d 3d 30 20 29 7b  =", z, 11)==0 ){
47f8c 0a 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69 63  .      rc = unic
47f8d 6f 64 65 41 64 64 45 78 63 65 70 74 69 6f 6e 73  odeAddExceptions
47f8e 28 70 4e 65 77 2c 20 30 2c 20 26 7a 5b 31 31 5d  (pNew, 0, &z[11]
47f8f 2c 20 6e 2d 31 31 29 3b 0a 20 20 20 20 7d 0a 20  , n-11);.    }. 
47f90 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f     else{.      /
47f91 2a 20 55 6e 72 65 63 6f 67 6e 69 7a 65 64 20 61  * Unrecognized a
47f92 72 67 75 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20  rgument */.     
47f93 20 72 63 20 20 3d 20 53 51 4c 49 54 45 5f 45 52   rc  = SQLITE_ER
47f94 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ROR;.    }.  }..
47f95 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
47f96 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 69 63 6f  _OK ){.    unico
47f97 64 65 44 65 73 74 72 6f 79 28 28 73 71 6c 69 74  deDestroy((sqlit
47f98 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 70  e3_tokenizer *)p
47f99 4e 65 77 29 3b 0a 20 20 20 20 70 4e 65 77 20 3d  New);.    pNew =
47f9a 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20   0;.  }.  *pp = 
47f9b 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
47f9c 65 72 20 2a 29 70 4e 65 77 3b 0a 20 20 72 65 74  er *)pNew;.  ret
47f9d 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
47f9e 20 50 72 65 70 61 72 65 20 74 6f 20 62 65 67 69   Prepare to begi
47f9f 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 20 70  n tokenizing a p
47fa0 61 72 74 69 63 75 6c 61 72 20 73 74 72 69 6e 67  articular string
47fa1 2e 20 20 54 68 65 20 69 6e 70 75 74 0a 2a 2a 20  .  The input.** 
47fa2 73 74 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b  string to be tok
47fa3 65 6e 69 7a 65 64 20 69 73 20 70 49 6e 70 75 74  enized is pInput
47fa4 5b 30 2e 2e 6e 42 79 74 65 73 2d 31 5d 2e 20 20  [0..nBytes-1].  
47fa5 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75 73 65 64  A cursor.** used
47fa6 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c   to incrementall
47fa7 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69 73 20  y tokenize this 
47fa8 73 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e  string is return
47fa9 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70 43 75 72  ed in .** *ppCur
47faa 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  sor..*/.static i
47fab 6e 74 20 75 6e 69 63 6f 64 65 4f 70 65 6e 28 0a  nt unicodeOpen(.
47fac 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
47fad 7a 65 72 20 2a 70 2c 20 20 20 20 20 20 20 20 20  zer *p,         
47fae 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a    /* The tokeniz
47faf 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  er */.  const ch
47fb0 61 72 20 2a 61 49 6e 70 75 74 2c 20 20 20 20 20  ar *aInput,     
47fb1 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74          /* Input
47fb2 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 69 6e 74   string */.  int
47fb3 20 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20   nInput,        
47fb4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
47fb5 53 69 7a 65 20 6f 66 20 73 74 72 69 6e 67 20 61  Size of string a
47fb6 49 6e 70 75 74 20 69 6e 20 62 79 74 65 73 20 2a  Input in bytes *
47fb7 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  /.  sqlite3_toke
47fb8 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 2a 70  nizer_cursor **p
47fb9 70 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 65 77 20  p   /* OUT: New 
47fba 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f  cursor object */
47fbb 0a 29 7b 0a 20 20 75 6e 69 63 6f 64 65 5f 63 75  .){.  unicode_cu
47fbc 72 73 6f 72 20 2a 70 43 73 72 3b 0a 0a 20 20 70  rsor *pCsr;..  p
47fbd 43 73 72 20 3d 20 28 75 6e 69 63 6f 64 65 5f 63  Csr = (unicode_c
47fbe 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65 33 5f  ursor *)sqlite3_
47fbf 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 75 6e  malloc(sizeof(un
47fc0 69 63 6f 64 65 5f 63 75 72 73 6f 72 29 29 3b 0a  icode_cursor));.
47fc1 20 20 69 66 28 20 70 43 73 72 3d 3d 30 20 29 7b    if( pCsr==0 ){
47fc2 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
47fc3 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
47fc4 6d 65 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20  memset(pCsr, 0, 
47fc5 73 69 7a 65 6f 66 28 75 6e 69 63 6f 64 65 5f 63  sizeof(unicode_c
47fc6 75 72 73 6f 72 29 29 3b 0a 0a 20 20 70 43 73 72  ursor));..  pCsr
47fc7 2d 3e 61 49 6e 70 75 74 20 3d 20 28 63 6f 6e 73  ->aInput = (cons
47fc8 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
47fc9 2a 29 61 49 6e 70 75 74 3b 0a 20 20 69 66 28 20  *)aInput;.  if( 
47fca 61 49 6e 70 75 74 3d 3d 30 20 29 7b 0a 20 20 20  aInput==0 ){.   
47fcb 20 70 43 73 72 2d 3e 6e 49 6e 70 75 74 20 3d 20   pCsr->nInput = 
47fcc 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e  0;.  }else if( n
47fcd 49 6e 70 75 74 3c 30 20 29 7b 0a 20 20 20 20 70  Input<0 ){.    p
47fce 43 73 72 2d 3e 6e 49 6e 70 75 74 20 3d 20 28 69  Csr->nInput = (i
47fcf 6e 74 29 73 74 72 6c 65 6e 28 61 49 6e 70 75 74  nt)strlen(aInput
47fd0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
47fd1 70 43 73 72 2d 3e 6e 49 6e 70 75 74 20 3d 20 6e  pCsr->nInput = n
47fd2 49 6e 70 75 74 3b 0a 20 20 7d 0a 0a 20 20 2a 70  Input;.  }..  *p
47fd3 70 20 3d 20 26 70 43 73 72 2d 3e 62 61 73 65 3b  p = &pCsr->base;
47fd4 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
47fd5 54 45 52 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  TER(p);.  return
47fd6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
47fd7 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74 6f 6b  *.** Close a tok
47fd8 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
47fd9 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e   previously open
47fda 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a  ed by a call to.
47fdb 2a 2a 20 73 69 6d 70 6c 65 4f 70 65 6e 28 29 20  ** simpleOpen() 
47fdc 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  above..*/.static
47fdd 20 69 6e 74 20 75 6e 69 63 6f 64 65 43 6c 6f 73   int unicodeClos
47fde 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
47fdf 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
47fe0 73 6f 72 29 7b 0a 20 20 75 6e 69 63 6f 64 65 5f  sor){.  unicode_
47fe1 63 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28  cursor *pCsr = (
47fe2 75 6e 69 63 6f 64 65 5f 63 75 72 73 6f 72 20 2a  unicode_cursor *
47fe3 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c  ) pCursor;.  sql
47fe4 69 74 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e  ite3_free(pCsr->
47fe5 7a 54 6f 6b 65 6e 29 3b 0a 20 20 73 71 6c 69 74  zToken);.  sqlit
47fe6 65 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0a 20  e3_free(pCsr);. 
47fe7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
47fe8 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72  K;.}../*.** Extr
47fe9 61 63 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b  act the next tok
47fea 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b 65 6e 69  en from a tokeni
47feb 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 2e 20 20  zation cursor.  
47fec 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 0a  The cursor must.
47fed 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65  ** have been ope
47fee 6e 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 63  ned by a prior c
47fef 61 6c 6c 20 74 6f 20 73 69 6d 70 6c 65 4f 70 65  all to simpleOpe
47ff0 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  n()..*/.static i
47ff1 6e 74 20 75 6e 69 63 6f 64 65 4e 65 78 74 28 0a  nt unicodeNext(.
47ff2 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
47ff3 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 2c 20  zer_cursor *pC, 
47ff4 20 20 2f 2a 20 43 75 72 73 6f 72 20 72 65 74 75    /* Cursor retu
47ff5 72 6e 65 64 20 62 79 20 73 69 6d 70 6c 65 4f 70  rned by simpleOp
47ff6 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  en */.  const ch
47ff7 61 72 20 2a 2a 70 61 54 6f 6b 65 6e 2c 20 20 20  ar **paToken,   
47ff8 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
47ff9 54 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20  Token text */.  
47ffa 69 6e 74 20 2a 70 6e 54 6f 6b 65 6e 2c 20 20 20  int *pnToken,   
47ffb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ffc 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f  /* OUT: Number o
47ffd 66 20 62 79 74 65 73 20 61 74 20 2a 70 61 54 6f  f bytes at *paTo
47ffe 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ken */.  int *pi
47fff 53 74 61 72 74 2c 20 20 20 20 20 20 20 20 20 20  Start,          
48000 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
48001 20 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74   Starting offset
48002 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69   of token */.  i
48003 6e 74 20 2a 70 69 45 6e 64 2c 20 20 20 20 20 20  nt *piEnd,      
48004 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
48005 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66  * OUT: Ending of
48006 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  fset of token */
48007 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73 20 20 20  .  int *piPos   
48008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48009 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 73 69 74     /* OUT: Posit
4800a 69 6f 6e 20 69 6e 74 65 67 65 72 20 6f 66 20 74  ion integer of t
4800b 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69  oken */.){.  uni
4800c 63 6f 64 65 5f 63 75 72 73 6f 72 20 2a 70 43 73  code_cursor *pCs
4800d 72 20 3d 20 28 75 6e 69 63 6f 64 65 5f 63 75 72  r = (unicode_cur
4800e 73 6f 72 20 2a 29 70 43 3b 0a 20 20 75 6e 69 63  sor *)pC;.  unic
4800f 6f 64 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  ode_tokenizer *p
48010 20 3d 20 28 28 75 6e 69 63 6f 64 65 5f 74 6f 6b   = ((unicode_tok
48011 65 6e 69 7a 65 72 20 2a 29 70 43 73 72 2d 3e 62  enizer *)pCsr->b
48012 61 73 65 2e 70 54 6f 6b 65 6e 69 7a 65 72 29 3b  ase.pTokenizer);
48013 0a 20 20 69 6e 74 20 69 43 6f 64 65 3b 0a 20 20  .  int iCode;.  
48014 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 63 6f  char *zOut;.  co
48015 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
48016 72 20 2a 7a 20 3d 20 26 70 43 73 72 2d 3e 61 49  r *z = &pCsr->aI
48017 6e 70 75 74 5b 70 43 73 72 2d 3e 69 4f 66 66 5d  nput[pCsr->iOff]
48018 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ;.  const unsign
48019 65 64 20 63 68 61 72 20 2a 7a 53 74 61 72 74 20  ed char *zStart 
4801a 3d 20 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  = z;.  const uns
4801b 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 6e 64  igned char *zEnd
4801c 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ;.  const unsign
4801d 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d 20 3d  ed char *zTerm =
4801e 20 26 70 43 73 72 2d 3e 61 49 6e 70 75 74 5b 70   &pCsr->aInput[p
4801f 43 73 72 2d 3e 6e 49 6e 70 75 74 5d 3b 0a 0a 20  Csr->nInput];.. 
48020 20 2f 2a 20 53 63 61 6e 20 70 61 73 74 20 61 6e   /* Scan past an
48021 79 20 64 65 6c 69 6d 69 74 65 72 20 63 68 61 72  y delimiter char
48022 61 63 74 65 72 73 20 62 65 66 6f 72 65 20 74 68  acters before th
48023 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6e  e start of the n
48024 65 78 74 20 74 6f 6b 65 6e 2e 0a 20 20 2a 2a 20  ext token..  ** 
48025 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  Return SQLITE_DO
48026 4e 45 20 65 61 72 6c 79 20 69 66 20 74 68 69 73  NE early if this
48027 20 74 61 6b 65 73 20 75 73 20 61 6c 6c 20 74 68   takes us all th
48028 65 20 77 61 79 20 74 6f 20 74 68 65 20 65 6e 64  e way to the end
48029 20 6f 66 20 0a 20 20 2a 2a 20 74 68 65 20 69 6e   of .  ** the in
4802a 70 75 74 2e 20 20 2a 2f 0a 20 20 77 68 69 6c 65  put.  */.  while
4802b 28 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  ( z<zTerm ){.   
4802c 20 52 45 41 44 5f 55 54 46 38 28 7a 2c 20 7a 54   READ_UTF8(z, zT
4802d 65 72 6d 2c 20 69 43 6f 64 65 29 3b 0a 20 20 20  erm, iCode);.   
4802e 20 69 66 28 20 75 6e 69 63 6f 64 65 49 73 41 6c   if( unicodeIsAl
4802f 6e 75 6d 28 70 2c 20 69 43 6f 64 65 29 20 29 20  num(p, iCode) ) 
48030 62 72 65 61 6b 3b 0a 20 20 20 20 7a 53 74 61 72  break;.    zStar
48031 74 20 3d 20 7a 3b 0a 20 20 7d 0a 20 20 69 66 28  t = z;.  }.  if(
48032 20 7a 53 74 61 72 74 3e 3d 7a 54 65 72 6d 20 29   zStart>=zTerm )
48033 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
48034 4f 4e 45 3b 0a 0a 20 20 7a 4f 75 74 20 3d 20 70  ONE;..  zOut = p
48035 43 73 72 2d 3e 7a 54 6f 6b 65 6e 3b 0a 20 20 64  Csr->zToken;.  d
48036 6f 20 7b 0a 20 20 20 20 69 6e 74 20 69 4f 75 74  o {.    int iOut
48037 3b 0a 0a 20 20 20 20 2f 2a 20 47 72 6f 77 20 74  ;..    /* Grow t
48038 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72  he output buffer
48039 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f   if required. */
4803a 0a 20 20 20 20 69 66 28 20 28 7a 4f 75 74 2d 70  .    if( (zOut-p
4803b 43 73 72 2d 3e 7a 54 6f 6b 65 6e 29 3e 3d 28 70  Csr->zToken)>=(p
4803c 43 73 72 2d 3e 6e 41 6c 6c 6f 63 2d 34 29 20 29  Csr->nAlloc-4) )
4803d 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e  {.      char *zN
4803e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ew = sqlite3_rea
4803f 6c 6c 6f 63 28 70 43 73 72 2d 3e 7a 54 6f 6b 65  lloc(pCsr->zToke
48040 6e 2c 20 70 43 73 72 2d 3e 6e 41 6c 6c 6f 63 2b  n, pCsr->nAlloc+
48041 36 34 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  64);.      if( !
48042 7a 4e 65 77 20 29 20 72 65 74 75 72 6e 20 53 51  zNew ) return SQ
48043 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
48044 20 20 7a 4f 75 74 20 3d 20 26 7a 4e 65 77 5b 7a    zOut = &zNew[z
48045 4f 75 74 20 2d 20 70 43 73 72 2d 3e 7a 54 6f 6b  Out - pCsr->zTok
48046 65 6e 5d 3b 0a 20 20 20 20 20 20 70 43 73 72 2d  en];.      pCsr-
48047 3e 7a 54 6f 6b 65 6e 20 3d 20 7a 4e 65 77 3b 0a  >zToken = zNew;.
48048 20 20 20 20 20 20 70 43 73 72 2d 3e 6e 41 6c 6c        pCsr->nAll
48049 6f 63 20 2b 3d 20 36 34 3b 0a 20 20 20 20 7d 0a  oc += 64;.    }.
4804a 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68  .    /* Write th
4804b 65 20 66 6f 6c 64 65 64 20 63 61 73 65 20 6f 66  e folded case of
4804c 20 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 63   the last charac
4804d 74 65 72 20 72 65 61 64 20 74 6f 20 74 68 65 20  ter read to the 
4804e 6f 75 74 70 75 74 20 2a 2f 0a 20 20 20 20 7a 45  output */.    zE
4804f 6e 64 20 3d 20 7a 3b 0a 20 20 20 20 69 4f 75 74  nd = z;.    iOut
48050 20 3d 20 73 71 6c 69 74 65 33 46 74 73 55 6e 69   = sqlite3FtsUni
48051 63 6f 64 65 46 6f 6c 64 28 69 43 6f 64 65 2c 20  codeFold(iCode, 
48052 70 2d 3e 62 52 65 6d 6f 76 65 44 69 61 63 72 69  p->bRemoveDiacri
48053 74 69 63 29 3b 0a 20 20 20 20 69 66 28 20 69 4f  tic);.    if( iO
48054 75 74 20 29 7b 0a 20 20 20 20 20 20 57 52 49 54  ut ){.      WRIT
48055 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 69 4f 75  E_UTF8(zOut, iOu
48056 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  t);.    }..    /
48057 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20  * If the cursor 
48058 69 73 20 6e 6f 74 20 61 74 20 45 4f 46 2c 20 72  is not at EOF, r
48059 65 61 64 20 74 68 65 20 6e 65 78 74 20 63 68 61  ead the next cha
4805a 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 69 66  racter */.    if
4805b 28 20 7a 3e 3d 7a 54 65 72 6d 20 29 20 62 72 65  ( z>=zTerm ) bre
4805c 61 6b 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46  ak;.    READ_UTF
4805d 38 28 7a 2c 20 7a 54 65 72 6d 2c 20 69 43 6f 64  8(z, zTerm, iCod
4805e 65 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 75 6e  e);.  }while( un
4805f 69 63 6f 64 65 49 73 41 6c 6e 75 6d 28 70 2c 20  icodeIsAlnum(p, 
48060 69 43 6f 64 65 29 20 0a 20 20 20 20 20 20 20 7c  iCode) .       |
48061 7c 20 73 71 6c 69 74 65 33 46 74 73 55 6e 69 63  | sqlite3FtsUnic
48062 6f 64 65 49 73 64 69 61 63 72 69 74 69 63 28 69  odeIsdiacritic(i
48063 43 6f 64 65 29 0a 20 20 29 3b 0a 0a 20 20 2f 2a  Code).  );..  /*
48064 20 53 65 74 20 74 68 65 20 6f 75 74 70 75 74 20   Set the output 
48065 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 72 65  variables and re
48066 74 75 72 6e 2e 20 2a 2f 0a 20 20 70 43 73 72 2d  turn. */.  pCsr-
48067 3e 69 4f 66 66 20 3d 20 28 7a 20 2d 20 70 43 73  >iOff = (z - pCs
48068 72 2d 3e 61 49 6e 70 75 74 29 3b 0a 20 20 2a 70  r->aInput);.  *p
48069 61 54 6f 6b 65 6e 20 3d 20 70 43 73 72 2d 3e 7a  aToken = pCsr->z
4806a 54 6f 6b 65 6e 3b 0a 20 20 2a 70 6e 54 6f 6b 65  Token;.  *pnToke
4806b 6e 20 3d 20 7a 4f 75 74 20 2d 20 70 43 73 72 2d  n = zOut - pCsr-
4806c 3e 7a 54 6f 6b 65 6e 3b 0a 20 20 2a 70 69 53 74  >zToken;.  *piSt
4806d 61 72 74 20 3d 20 28 7a 53 74 61 72 74 20 2d 20  art = (zStart - 
4806e 70 43 73 72 2d 3e 61 49 6e 70 75 74 29 3b 0a 20  pCsr->aInput);. 
4806f 20 2a 70 69 45 6e 64 20 3d 20 28 7a 45 6e 64 20   *piEnd = (zEnd 
48070 2d 20 70 43 73 72 2d 3e 61 49 6e 70 75 74 29 3b  - pCsr->aInput);
48071 0a 20 20 2a 70 69 50 6f 73 20 3d 20 70 43 73 72  .  *piPos = pCsr
48072 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 72 65  ->iToken++;.  re
48073 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
48074 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 70  }../*.** Set *pp
48075 4d 6f 64 75 6c 65 20 74 6f 20 61 20 70 6f 69 6e  Module to a poin
48076 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74  ter to the sqlit
48077 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
48078 75 6c 65 20 0a 2a 2a 20 73 74 72 75 63 74 75 72  ule .** structur
48079 65 20 66 6f 72 20 74 68 65 20 75 6e 69 63 6f 64  e for the unicod
4807a 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a  e tokenizer..*/.
4807b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
4807c 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 55  oid sqlite3Fts3U
4807d 6e 69 63 6f 64 65 54 6f 6b 65 6e 69 7a 65 72 28  nicodeTokenizer(
4807e 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
4807f 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 20 2a  r_module const *
48080 2a 70 70 4d 6f 64 75 6c 65 29 7b 0a 20 20 73 74  *ppModule){.  st
48081 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
48082 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
48083 75 6c 65 20 6d 6f 64 75 6c 65 20 3d 20 7b 0a 20  ule module = {. 
48084 20 20 20 30 2c 0a 20 20 20 20 75 6e 69 63 6f 64     0,.    unicod
48085 65 43 72 65 61 74 65 2c 0a 20 20 20 20 75 6e 69  eCreate,.    uni
48086 63 6f 64 65 44 65 73 74 72 6f 79 2c 0a 20 20 20  codeDestroy,.   
48087 20 75 6e 69 63 6f 64 65 4f 70 65 6e 2c 0a 20 20   unicodeOpen,.  
48088 20 20 75 6e 69 63 6f 64 65 43 6c 6f 73 65 2c 0a    unicodeClose,.
48089 20 20 20 20 75 6e 69 63 6f 64 65 4e 65 78 74 2c      unicodeNext,
4808a 0a 20 20 20 20 30 2c 0a 20 20 7d 3b 0a 20 20 2a  .    0,.  };.  *
4808b 70 70 4d 6f 64 75 6c 65 20 3d 20 26 6d 6f 64 75  ppModule = &modu
4808c 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  le;.}..#endif /*
4808d 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
4808e 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
4808f 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
48090 46 54 53 33 29 20 2a 2f 0a 23 65 6e 64 69 66 20  FTS3) */.#endif 
48091 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /* ifndef SQLITE
48092 5f 45 4e 41 42 4c 45 5f 46 54 53 34 5f 55 4e 49  _ENABLE_FTS4_UNI
48093 43 4f 44 45 36 31 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  CODE61 */../****
48094 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
48095 66 20 66 74 73 33 5f 75 6e 69 63 6f 64 65 2e 63  f fts3_unicode.c
48096 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
48097 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48098 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
48099 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
4809a 20 66 69 6c 65 20 66 74 73 33 5f 75 6e 69 63 6f   file fts3_unico
4809b 64 65 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  de2.c **********
4809c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4809d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
4809e 20 32 30 31 32 20 4d 61 79 20 32 35 0a 2a 2a 0a   2012 May 25.**.
4809f 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
480a0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
480a1 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
480a2 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
480a3 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
480a4 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
480a5 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
480a6 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
480a7 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
480a8 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
480a9 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
480aa 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
480ab 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
480ac 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
480ad 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
480ae 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
480af 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
480b0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
480b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
480b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
480b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
480b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
480b5 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 4f 20 4e 4f  .*/../*.** DO NO
480b6 54 20 45 44 49 54 20 54 48 49 53 20 4d 41 43 48  T EDIT THIS MACH
480b7 49 4e 45 20 47 45 4e 45 52 41 54 45 44 20 46 49  INE GENERATED FI
480b8 4c 45 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65 66 69  LE..*/..#if defi
480b9 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
480ba 45 5f 46 54 53 34 5f 55 4e 49 43 4f 44 45 36 31  E_FTS4_UNICODE61
480bb 29 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ).#if defined(SQ
480bc 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
480bd 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
480be 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 34 29  ITE_ENABLE_FTS4)
480bf 0a 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 61  ../* #include <a
480c0 73 73 65 72 74 2e 68 3e 20 2a 2f 0a 0a 2f 2a 0a  ssert.h> */../*.
480c1 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
480c2 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 63  f the argument c
480c3 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 61 20  orresponds to a 
480c4 75 6e 69 63 6f 64 65 20 63 6f 64 65 70 6f 69 6e  unicode codepoin
480c5 74 0a 2a 2a 20 63 6c 61 73 73 69 66 69 65 64 20  t.** classified 
480c6 61 73 20 65 69 74 68 65 72 20 61 20 6c 65 74 74  as either a lett
480c7 65 72 20 6f 72 20 61 20 6e 75 6d 62 65 72 2e 20  er or a number. 
480c8 4f 74 68 65 72 77 69 73 65 20 66 61 6c 73 65 2e  Otherwise false.
480c9 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c  .**.** The resul
480ca 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64  ts are undefined
480cb 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 70 61   if the value pa
480cc 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e  ssed to this fun
480cd 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 6c 65 73 73  ction.** is less
480ce 20 74 68 61 6e 20 7a 65 72 6f 2e 0a 2a 2f 0a 53   than zero..*/.S
480cf 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
480d0 74 20 73 71 6c 69 74 65 33 46 74 73 55 6e 69 63  t sqlite3FtsUnic
480d1 6f 64 65 49 73 61 6c 6e 75 6d 28 69 6e 74 20 63  odeIsalnum(int c
480d2 29 7b 0a 20 20 2f 2a 20 45 61 63 68 20 75 6e 73  ){.  /* Each uns
480d3 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 69 6e  igned integer in
480d4 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   the following a
480d5 72 72 61 79 20 63 6f 72 72 65 73 70 6f 6e 64 73  rray corresponds
480d6 20 74 6f 20 61 20 63 6f 6e 74 69 67 75 6f 75 73   to a contiguous
480d7 0a 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 75  .  ** range of u
480d8 6e 69 63 6f 64 65 20 63 6f 64 65 70 6f 69 6e 74  nicode codepoint
480d9 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65  s that are not e
480da 69 74 68 65 72 20 6c 65 74 74 65 72 73 20 6f 72  ither letters or
480db 20 6e 75 6d 62 65 72 73 20 28 69 2e 65 2e 0a 20   numbers (i.e.. 
480dc 20 2a 2a 20 63 6f 64 65 70 6f 69 6e 74 73 20 66   ** codepoints f
480dd 6f 72 20 77 68 69 63 68 20 74 68 69 73 20 66 75  or which this fu
480de 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65  nction should re
480df 74 75 72 6e 20 30 29 2e 0a 20 20 2a 2a 0a 20 20  turn 0)..  **.  
480e0 2a 2a 20 54 68 65 20 6d 6f 73 74 20 73 69 67 6e  ** The most sign
480e1 69 66 69 63 61 6e 74 20 32 32 20 62 69 74 73 20  ificant 22 bits 
480e2 69 6e 20 65 61 63 68 20 33 32 2d 62 69 74 20 76  in each 32-bit v
480e3 61 6c 75 65 20 63 6f 6e 74 61 69 6e 20 74 68 65  alue contain the
480e4 20 66 69 72 73 74 20 0a 20 20 2a 2a 20 63 6f 64   first .  ** cod
480e5 65 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 72 61  epoint in the ra
480e6 6e 67 65 2e 20 54 68 65 20 6c 65 61 73 74 20 73  nge. The least s
480e7 69 67 6e 69 66 69 63 61 6e 74 20 31 30 20 62 69  ignificant 10 bi
480e8 74 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 73  ts are used to s
480e9 74 6f 72 65 0a 20 20 2a 2a 20 74 68 65 20 73 69  tore.  ** the si
480ea 7a 65 20 6f 66 20 74 68 65 20 72 61 6e 67 65 20  ze of the range 
480eb 28 61 6c 77 61 79 73 20 61 74 20 6c 65 61 73 74  (always at least
480ec 20 31 29 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f   1). In other wo
480ed 72 64 73 2c 20 74 68 65 20 76 61 6c 75 65 20 0a  rds, the value .
480ee 20 20 2a 2a 20 28 28 43 3c 3c 32 32 29 20 2b 20    ** ((C<<22) + 
480ef 4e 29 20 72 65 70 72 65 73 65 6e 74 73 20 61 20  N) represents a 
480f0 72 61 6e 67 65 20 6f 66 20 4e 20 63 6f 64 65 70  range of N codep
480f1 6f 69 6e 74 73 20 73 74 61 72 74 69 6e 67 20 77  oints starting w
480f2 69 74 68 20 63 6f 64 65 70 6f 69 6e 74 20 0a 20  ith codepoint . 
480f3 20 2a 2a 20 43 2e 20 49 74 20 69 73 20 6e 6f 74   ** C. It is not
480f4 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 70   possible to rep
480f5 72 65 73 65 6e 74 20 61 20 72 61 6e 67 65 20 6c  resent a range l
480f6 61 72 67 65 72 20 74 68 61 6e 20 31 30 32 33 20  arger than 1023 
480f7 63 6f 64 65 70 6f 69 6e 74 73 20 0a 20 20 2a 2a  codepoints .  **
480f8 20 75 73 69 6e 67 20 74 68 69 73 20 66 6f 72 6d   using this form
480f9 61 74 2e 0a 20 20 2a 2f 0a 20 20 63 6f 6e 73 74  at..  */.  const
480fa 20 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64   static unsigned
480fb 20 69 6e 74 20 61 45 6e 74 72 79 5b 5d 20 3d 20   int aEntry[] = 
480fc 7b 0a 20 20 20 20 30 78 30 30 30 30 30 30 33 30  {.    0x00000030
480fd 2c 20 30 78 30 30 30 30 45 38 30 37 2c 20 30 78  , 0x0000E807, 0x
480fe 30 30 30 31 36 43 30 36 2c 20 30 78 30 30 30 31  00016C06, 0x0001
480ff 45 43 32 46 2c 20 30 78 30 30 30 32 41 43 30 37  EC2F, 0x0002AC07
48100 2c 0a 20 20 20 20 30 78 30 30 30 32 44 30 30 31  ,.    0x0002D001
48101 2c 20 30 78 30 30 30 32 44 38 30 33 2c 20 30 78  , 0x0002D803, 0x
48102 30 30 30 32 45 43 30 31 2c 20 30 78 30 30 30 32  0002EC01, 0x0002
48103 46 43 30 31 2c 20 30 78 30 30 30 33 35 43 30 31  FC01, 0x00035C01
48104 2c 0a 20 20 20 20 30 78 30 30 30 33 44 43 30 31  ,.    0x0003DC01
48105 2c 20 30 78 30 30 30 42 30 38 30 34 2c 20 30 78  , 0x000B0804, 0x
48106 30 30 30 42 34 38 30 45 2c 20 30 78 30 30 30 42  000B480E, 0x000B
48107 39 34 30 37 2c 20 30 78 30 30 30 42 42 34 30 31  9407, 0x000BB401
48108 2c 0a 20 20 20 20 30 78 30 30 30 42 42 43 38 31  ,.    0x000BBC81
48109 2c 20 30 78 30 30 30 44 44 34 30 31 2c 20 30 78  , 0x000DD401, 0x
4810a 30 30 30 44 46 38 30 31 2c 20 30 78 30 30 30 45  000DF801, 0x000E
4810b 31 30 30 32 2c 20 30 78 30 30 30 45 31 43 30 31  1002, 0x000E1C01
4810c 2c 0a 20 20 20 20 30 78 30 30 30 46 44 38 30 31  ,.    0x000FD801
4810d 2c 20 30 78 30 30 31 32 30 38 30 38 2c 20 30 78  , 0x00120808, 0x
4810e 30 30 31 35 36 38 30 36 2c 20 30 78 30 30 31 36  00156806, 0x0016
4810f 32 34 30 32 2c 20 30 78 30 30 31 36 33 43 30 31  2402, 0x00163C01
48110 2c 0a 20 20 20 20 30 78 30 30 31 36 34 34 33 37  ,.    0x00164437
48111 2c 20 30 78 30 30 31 37 43 43 30 32 2c 20 30 78  , 0x0017CC02, 0x
48112 30 30 31 38 30 30 30 35 2c 20 30 78 30 30 31 38  00180005, 0x0018
48113 31 38 31 36 2c 20 30 78 30 30 31 38 37 38 30 32  1816, 0x00187802
48114 2c 0a 20 20 20 20 30 78 30 30 31 39 32 43 31 35  ,.    0x00192C15
48115 2c 20 30 78 30 30 31 39 41 38 30 34 2c 20 30 78  , 0x0019A804, 0x
48116 30 30 31 39 43 30 30 31 2c 20 30 78 30 30 31 42  0019C001, 0x001B
48117 35 30 30 31 2c 20 30 78 30 30 31 42 35 38 30 46  5001, 0x001B580F
48118 2c 0a 20 20 20 20 30 78 30 30 31 42 39 43 30 37  ,.    0x001B9C07
48119 2c 20 30 78 30 30 31 42 46 34 30 32 2c 20 30 78  , 0x001BF402, 0x
4811a 30 30 31 43 30 30 30 45 2c 20 30 78 30 30 31 43  001C000E, 0x001C
4811b 33 43 30 31 2c 20 30 78 30 30 31 43 34 34 30 31  3C01, 0x001C4401
4811c 2c 0a 20 20 20 20 30 78 30 30 31 43 43 30 31 42  ,.    0x001CC01B
4811d 2c 20 30 78 30 30 31 45 39 38 30 42 2c 20 30 78  , 0x001E980B, 0x
4811e 30 30 31 46 41 43 30 39 2c 20 30 78 30 30 31 46  001FAC09, 0x001F
4811f 44 38 30 34 2c 20 30 78 30 30 32 30 35 38 30 34  D804, 0x00205804
48120 2c 0a 20 20 20 20 30 78 30 30 32 30 36 43 30 39  ,.    0x00206C09
48121 2c 20 30 78 30 30 32 30 39 34 30 33 2c 20 30 78  , 0x00209403, 0x
48122 30 30 32 30 41 34 30 35 2c 20 30 78 30 30 32 30  0020A405, 0x0020
48123 43 30 30 46 2c 20 30 78 30 30 32 31 36 34 30 33  C00F, 0x00216403
48124 2c 0a 20 20 20 20 30 78 30 30 32 31 37 38 30 31  ,.    0x00217801
48125 2c 20 30 78 30 30 32 33 39 30 31 42 2c 20 30 78  , 0x0023901B, 0x
48126 30 30 32 34 30 30 30 34 2c 20 30 78 30 30 32 34  00240004, 0x0024
48127 45 38 30 33 2c 20 30 78 30 30 32 34 46 38 31 32  E803, 0x0024F812
48128 2c 0a 20 20 20 20 30 78 30 30 32 35 34 34 30 37  ,.    0x00254407
48129 2c 20 30 78 30 30 32 35 38 38 30 34 2c 20 30 78  , 0x00258804, 0x
4812a 30 30 32 35 43 30 30 31 2c 20 30 78 30 30 32 36  0025C001, 0x0026
4812b 30 34 30 33 2c 20 30 78 30 30 32 36 46 30 30 31  0403, 0x0026F001
4812c 2c 0a 20 20 20 20 30 78 30 30 32 36 46 38 30 37  ,.    0x0026F807
4812d 2c 20 30 78 30 30 32 37 31 43 30 32 2c 20 30 78  , 0x00271C02, 0x
4812e 30 30 32 37 32 43 30 33 2c 20 30 78 30 30 32 37  00272C03, 0x0027
4812f 35 43 30 31 2c 20 30 78 30 30 32 37 38 38 30 32  5C01, 0x00278802
48130 2c 0a 20 20 20 20 30 78 30 30 32 37 43 38 30 32  ,.    0x0027C802
48131 2c 20 30 78 30 30 32 37 45 38 30 32 2c 20 30 78  , 0x0027E802, 0x
48132 30 30 32 38 30 34 30 33 2c 20 30 78 30 30 32 38  00280403, 0x0028
48133 46 30 30 31 2c 20 30 78 30 30 32 38 46 38 30 35  F001, 0x0028F805
48134 2c 0a 20 20 20 20 30 78 30 30 32 39 31 43 30 32  ,.    0x00291C02
48135 2c 20 30 78 30 30 32 39 32 43 30 33 2c 20 30 78  , 0x00292C03, 0x
48136 30 30 32 39 34 34 30 31 2c 20 30 78 30 30 32 39  00294401, 0x0029
48137 43 30 30 32 2c 20 30 78 30 30 32 39 44 34 30 31  C002, 0x0029D401
48138 2c 0a 20 20 20 20 30 78 30 30 32 41 30 34 30 33  ,.    0x002A0403
48139 2c 20 30 78 30 30 32 41 46 30 30 31 2c 20 30 78  , 0x002AF001, 0x
4813a 30 30 32 41 46 38 30 38 2c 20 30 78 30 30 32 42  002AF808, 0x002B
4813b 31 43 30 33 2c 20 30 78 30 30 32 42 32 43 30 33  1C03, 0x002B2C03
4813c 2c 0a 20 20 20 20 30 78 30 30 32 42 38 38 30 32  ,.    0x002B8802
4813d 2c 20 30 78 30 30 32 42 43 30 30 32 2c 20 30 78  , 0x002BC002, 0x
4813e 30 30 32 43 30 34 30 33 2c 20 30 78 30 30 32 43  002C0403, 0x002C
4813f 46 30 30 31 2c 20 30 78 30 30 32 43 46 38 30 37  F001, 0x002CF807
48140 2c 0a 20 20 20 20 30 78 30 30 32 44 31 43 30 32  ,.    0x002D1C02
48141 2c 20 30 78 30 30 32 44 32 43 30 33 2c 20 30 78  , 0x002D2C03, 0x
48142 30 30 32 44 35 38 30 32 2c 20 30 78 30 30 32 44  002D5802, 0x002D
48143 38 38 30 32 2c 20 30 78 30 30 32 44 43 30 30 31  8802, 0x002DC001
48144 2c 0a 20 20 20 20 30 78 30 30 32 45 30 38 30 31  ,.    0x002E0801
48145 2c 20 30 78 30 30 32 45 46 38 30 35 2c 20 30 78  , 0x002EF805, 0x
48146 30 30 32 46 31 38 30 33 2c 20 30 78 30 30 32 46  002F1803, 0x002F
48147 32 38 30 34 2c 20 30 78 30 30 32 46 35 43 30 31  2804, 0x002F5C01
48148 2c 0a 20 20 20 20 30 78 30 30 32 46 43 43 30 38  ,.    0x002FCC08
48149 2c 20 30 78 30 30 33 30 30 34 30 33 2c 20 30 78  , 0x00300403, 0x
4814a 30 30 33 30 46 38 30 37 2c 20 30 78 30 30 33 31  0030F807, 0x0031
4814b 31 38 30 33 2c 20 30 78 30 30 33 31 32 38 30 34  1803, 0x00312804
4814c 2c 0a 20 20 20 20 30 78 30 30 33 31 35 34 30 32  ,.    0x00315402
4814d 2c 20 30 78 30 30 33 31 38 38 30 32 2c 20 30 78  , 0x00318802, 0x
4814e 30 30 33 31 46 43 30 31 2c 20 30 78 30 30 33 32  0031FC01, 0x0032
4814f 30 38 30 32 2c 20 30 78 30 30 33 32 46 30 30 31  0802, 0x0032F001
48150 2c 0a 20 20 20 20 30 78 30 30 33 32 46 38 30 37  ,.    0x0032F807
48151 2c 20 30 78 30 30 33 33 31 38 30 33 2c 20 30 78  , 0x00331803, 0x
48152 30 30 33 33 32 38 30 34 2c 20 30 78 30 30 33 33  00332804, 0x0033
48153 35 34 30 32 2c 20 30 78 30 30 33 33 38 38 30 32  5402, 0x00338802
48154 2c 0a 20 20 20 20 30 78 30 30 33 34 30 38 30 32  ,.    0x00340802
48155 2c 20 30 78 30 30 33 34 46 38 30 37 2c 20 30 78  , 0x0034F807, 0x
48156 30 30 33 35 31 38 30 33 2c 20 30 78 30 30 33 35  00351803, 0x0035
48157 32 38 30 34 2c 20 30 78 30 30 33 35 35 43 30 31  2804, 0x00355C01
48158 2c 0a 20 20 20 20 30 78 30 30 33 35 38 38 30 32  ,.    0x00358802
48159 2c 20 30 78 30 30 33 35 45 34 30 31 2c 20 30 78  , 0x0035E401, 0x
4815a 30 30 33 36 30 38 30 32 2c 20 30 78 30 30 33 37  00360802, 0x0037
4815b 32 38 30 31 2c 20 30 78 30 30 33 37 33 43 30 36  2801, 0x00373C06
4815c 2c 0a 20 20 20 20 30 78 30 30 33 37 35 38 30 31  ,.    0x00375801
4815d 2c 20 30 78 30 30 33 37 36 30 30 38 2c 20 30 78  , 0x00376008, 0x
4815e 30 30 33 37 43 38 30 33 2c 20 30 78 30 30 33 38  0037C803, 0x0038
4815f 43 34 30 31 2c 20 30 78 30 30 33 38 44 30 30 37  C401, 0x0038D007
48160 2c 0a 20 20 20 20 30 78 30 30 33 38 46 43 30 31  ,.    0x0038FC01
48161 2c 20 30 78 30 30 33 39 31 43 30 39 2c 20 30 78  , 0x00391C09, 0x
48162 30 30 33 39 36 38 30 32 2c 20 30 78 30 30 33 41  00396802, 0x003A
48163 43 34 30 31 2c 20 30 78 30 30 33 41 44 30 30 36  C401, 0x003AD006
48164 2c 0a 20 20 20 20 30 78 30 30 33 41 45 43 30 32  ,.    0x003AEC02
48165 2c 20 30 78 30 30 33 42 32 30 30 36 2c 20 30 78  , 0x003B2006, 0x
48166 30 30 33 43 30 34 31 46 2c 20 30 78 30 30 33 43  003C041F, 0x003C
48167 44 30 30 43 2c 20 30 78 30 30 33 44 43 34 31 37  D00C, 0x003DC417
48168 2c 0a 20 20 20 20 30 78 30 30 33 45 33 34 30 42  ,.    0x003E340B
48169 2c 20 30 78 30 30 33 45 36 34 32 34 2c 20 30 78  , 0x003E6424, 0x
4816a 30 30 33 45 46 38 30 46 2c 20 30 78 30 30 33 46  003EF80F, 0x003F
4816b 33 38 30 44 2c 20 30 78 30 30 34 30 41 43 31 34  380D, 0x0040AC14
4816c 2c 0a 20 20 20 20 30 78 30 30 34 31 32 38 30 36  ,.    0x00412806
4816d 2c 20 30 78 30 30 34 31 35 38 30 34 2c 20 30 78  , 0x00415804, 0x
4816e 30 30 34 31 37 38 30 33 2c 20 30 78 30 30 34 31  00417803, 0x0041
4816f 38 38 30 33 2c 20 30 78 30 30 34 31 39 43 30 37  8803, 0x00419C07
48170 2c 0a 20 20 20 20 30 78 30 30 34 31 43 34 30 34  ,.    0x0041C404
48171 2c 20 30 78 30 30 34 32 30 38 30 43 2c 20 30 78  , 0x0042080C, 0x
48172 30 30 34 32 33 43 30 31 2c 20 30 78 30 30 34 32  00423C01, 0x0042
48173 36 38 30 36 2c 20 30 78 30 30 34 33 45 43 30 31  6806, 0x0043EC01
48174 2c 0a 20 20 20 20 30 78 30 30 34 44 37 34 30 43  ,.    0x004D740C
48175 2c 20 30 78 30 30 34 45 34 30 30 41 2c 20 30 78  , 0x004E400A, 0x
48176 30 30 35 30 30 30 30 31 2c 20 30 78 30 30 35 39  00500001, 0x0059
48177 42 34 30 32 2c 20 30 78 30 30 35 41 30 30 30 31  B402, 0x005A0001
48178 2c 0a 20 20 20 20 30 78 30 30 35 41 36 43 30 32  ,.    0x005A6C02
48179 2c 20 30 78 30 30 35 42 41 43 30 33 2c 20 30 78  , 0x005BAC03, 0x
4817a 30 30 35 43 34 38 30 33 2c 20 30 78 30 30 35 43  005C4803, 0x005C
4817b 43 38 30 35 2c 20 30 78 30 30 35 44 34 38 30 32  C805, 0x005D4802
4817c 2c 0a 20 20 20 20 30 78 30 30 35 44 43 38 30 32  ,.    0x005DC802
4817d 2c 20 30 78 30 30 35 45 44 30 32 33 2c 20 30 78  , 0x005ED023, 0x
4817e 30 30 35 46 36 30 30 34 2c 20 30 78 30 30 35 46  005F6004, 0x005F
4817f 37 34 30 31 2c 20 30 78 30 30 36 30 30 30 30 46  7401, 0x0060000F
48180 2c 0a 20 20 20 20 30 78 30 30 36 32 41 34 30 31  ,.    0x0062A401
48181 2c 20 30 78 30 30 36 34 38 30 30 43 2c 20 30 78  , 0x0064800C, 0x
48182 30 30 36 34 43 30 30 43 2c 20 30 78 30 30 36 35  0064C00C, 0x0065
48183 30 30 30 31 2c 20 30 78 30 30 36 35 31 30 30 32  0001, 0x00651002
48184 2c 0a 20 20 20 20 30 78 30 30 36 36 43 30 31 31  ,.    0x0066C011
48185 2c 20 30 78 30 30 36 37 32 30 30 32 2c 20 30 78  , 0x00672002, 0x
48186 30 30 36 37 37 38 32 32 2c 20 30 78 30 30 36 38  00677822, 0x0068
48187 35 43 30 35 2c 20 30 78 30 30 36 38 37 38 30 32  5C05, 0x00687802
48188 2c 0a 20 20 20 20 30 78 30 30 36 39 35 34 30 41  ,.    0x0069540A
48189 2c 20 30 78 30 30 36 39 38 30 31 44 2c 20 30 78  , 0x0069801D, 0x
4818a 30 30 36 39 46 43 30 31 2c 20 30 78 30 30 36 41  0069FC01, 0x006A
4818b 38 30 30 37 2c 20 30 78 30 30 36 41 41 30 30 36  8007, 0x006AA006
4818c 2c 0a 20 20 20 20 30 78 30 30 36 43 30 30 30 35  ,.    0x006C0005
4818d 2c 20 30 78 30 30 36 43 44 30 31 31 2c 20 30 78  , 0x006CD011, 0x
4818e 30 30 36 44 36 38 32 33 2c 20 30 78 30 30 36 45  006D6823, 0x006E
4818f 30 30 30 33 2c 20 30 78 30 30 36 45 38 34 30 44  0003, 0x006E840D
48190 2c 0a 20 20 20 20 30 78 30 30 36 46 39 38 30 45  ,.    0x006F980E
48191 2c 20 30 78 30 30 36 46 46 30 30 34 2c 20 30 78  , 0x006FF004, 0x
48192 30 30 37 30 39 30 31 34 2c 20 30 78 30 30 37 30  00709014, 0x0070
48193 45 43 30 35 2c 20 30 78 30 30 37 31 46 38 30 32  EC05, 0x0071F802
48194 2c 0a 20 20 20 20 30 78 30 30 37 33 30 30 30 38  ,.    0x00730008
48195 2c 20 30 78 30 30 37 33 34 30 31 39 2c 20 30 78  , 0x00734019, 0x
48196 30 30 37 33 42 34 30 31 2c 20 30 78 30 30 37 33  0073B401, 0x0073
48197 43 38 30 33 2c 20 30 78 30 30 37 37 30 30 32 37  C803, 0x00770027
48198 2c 0a 20 20 20 20 30 78 30 30 37 37 46 30 30 34  ,.    0x0077F004
48199 2c 20 30 78 30 30 37 45 46 34 30 31 2c 20 30 78  , 0x007EF401, 0x
4819a 30 30 37 45 46 43 30 33 2c 20 30 78 30 30 37 46  007EFC03, 0x007F
4819b 33 34 30 33 2c 20 30 78 30 30 37 46 37 34 30 33  3403, 0x007F7403
4819c 2c 0a 20 20 20 20 30 78 30 30 37 46 42 34 30 33  ,.    0x007FB403
4819d 2c 20 30 78 30 30 37 46 46 34 30 32 2c 20 30 78  , 0x007FF402, 0x
4819e 30 30 38 30 30 30 36 35 2c 20 30 78 30 30 38 31  00800065, 0x0081
4819f 41 38 30 36 2c 20 30 78 30 30 38 31 45 38 30 35  A806, 0x0081E805
481a0 2c 0a 20 20 20 20 30 78 30 30 38 32 32 38 30 35  ,.    0x00822805
481a1 2c 20 30 78 30 30 38 32 38 30 31 41 2c 20 30 78  , 0x0082801A, 0x
481a2 30 30 38 33 34 30 32 31 2c 20 30 78 30 30 38 34  00834021, 0x0084
481a3 30 30 30 32 2c 20 30 78 30 30 38 34 30 43 30 34  0002, 0x00840C04
481a4 2c 0a 20 20 20 20 30 78 30 30 38 34 32 30 30 32  ,.    0x00842002
481a5 2c 20 30 78 30 30 38 34 35 30 30 31 2c 20 30 78  , 0x00845001, 0x
481a6 30 30 38 34 35 38 30 33 2c 20 30 78 30 30 38 34  00845803, 0x0084
481a7 37 38 30 36 2c 20 30 78 30 30 38 34 39 34 30 31  7806, 0x00849401
481a8 2c 0a 20 20 20 20 30 78 30 30 38 34 39 43 30 31  ,.    0x00849C01
481a9 2c 20 30 78 30 30 38 34 41 34 30 31 2c 20 30 78  , 0x0084A401, 0x
481aa 30 30 38 34 42 38 30 31 2c 20 30 78 30 30 38 34  0084B801, 0x0084
481ab 45 38 30 32 2c 20 30 78 30 30 38 35 30 30 30 35  E802, 0x00850005
481ac 2c 0a 20 20 20 20 30 78 30 30 38 35 32 38 30 34  ,.    0x00852804
481ad 2c 20 30 78 30 30 38 35 33 43 30 31 2c 20 30 78  , 0x00853C01, 0x
481ae 30 30 38 36 34 32 36 34 2c 20 30 78 30 30 39 30  00864264, 0x0090
481af 30 30 32 37 2c 20 30 78 30 30 39 31 30 30 30 42  0027, 0x0091000B
481b0 2c 0a 20 20 20 20 30 78 30 30 39 32 37 30 34 45  ,.    0x0092704E
481b1 2c 20 30 78 30 30 39 34 30 32 30 30 2c 20 30 78  , 0x00940200, 0x
481b2 30 30 39 43 30 34 37 35 2c 20 30 78 30 30 39 45  009C0475, 0x009E
481b3 35 33 42 39 2c 20 30 78 30 30 41 44 34 30 30 41  53B9, 0x00AD400A
481b4 2c 0a 20 20 20 20 30 78 30 30 42 33 39 34 30 36  ,.    0x00B39406
481b5 2c 20 30 78 30 30 42 33 42 43 30 33 2c 20 30 78  , 0x00B3BC03, 0x
481b6 30 30 42 33 45 34 30 34 2c 20 30 78 30 30 42 33  00B3E404, 0x00B3
481b7 46 38 30 32 2c 20 30 78 30 30 42 35 43 30 30 31  F802, 0x00B5C001
481b8 2c 0a 20 20 20 20 30 78 30 30 42 35 46 43 30 31  ,.    0x00B5FC01
481b9 2c 20 30 78 30 30 42 37 38 30 34 46 2c 20 30 78  , 0x00B7804F, 0x
481ba 30 30 42 38 43 30 30 43 2c 20 30 78 30 30 42 41  00B8C00C, 0x00BA
481bb 30 30 31 41 2c 20 30 78 30 30 42 41 36 43 35 39  001A, 0x00BA6C59
481bc 2c 0a 20 20 20 20 30 78 30 30 42 43 30 30 44 36  ,.    0x00BC00D6
481bd 2c 20 30 78 30 30 42 46 43 30 30 43 2c 20 30 78  , 0x00BFC00C, 0x
481be 30 30 43 30 30 30 30 35 2c 20 30 78 30 30 43 30  00C00005, 0x00C0
481bf 32 30 31 39 2c 20 30 78 30 30 43 30 41 38 30 37  2019, 0x00C0A807
481c0 2c 0a 20 20 20 20 30 78 30 30 43 30 44 38 30 32  ,.    0x00C0D802
481c1 2c 20 30 78 30 30 43 30 46 34 30 33 2c 20 30 78  , 0x00C0F403, 0x
481c2 30 30 43 32 36 34 30 34 2c 20 30 78 30 30 43 32  00C26404, 0x00C2
481c3 38 30 30 31 2c 20 30 78 30 30 43 33 45 43 30 31  8001, 0x00C3EC01
481c4 2c 0a 20 20 20 20 30 78 30 30 43 36 34 30 30 32  ,.    0x00C64002
481c5 2c 20 30 78 30 30 43 36 35 38 30 41 2c 20 30 78  , 0x00C6580A, 0x
481c6 30 30 43 37 30 30 32 34 2c 20 30 78 30 30 43 38  00C70024, 0x00C8
481c7 30 30 31 46 2c 20 30 78 30 30 43 38 41 38 31 45  001F, 0x00C8A81E
481c8 2c 0a 20 20 20 20 30 78 30 30 43 39 34 30 30 31  ,.    0x00C94001
481c9 2c 20 30 78 30 30 43 39 38 30 32 30 2c 20 30 78  , 0x00C98020, 0x
481ca 30 30 43 41 32 38 32 37 2c 20 30 78 30 30 43 42  00CA2827, 0x00CB
481cb 30 30 33 46 2c 20 30 78 30 30 43 43 30 31 30 30  003F, 0x00CC0100
481cc 2c 0a 20 20 20 20 30 78 30 31 33 37 30 30 34 30  ,.    0x01370040
481cd 2c 20 30 78 30 32 39 32 34 30 33 37 2c 20 30 78  , 0x02924037, 0x
481ce 30 32 39 33 46 38 30 32 2c 20 30 78 30 32 39 38  0293F802, 0x0298
481cf 33 34 30 33 2c 20 30 78 30 32 39 39 42 43 31 30  3403, 0x0299BC10
481d0 2c 0a 20 20 20 20 30 78 30 32 39 41 37 43 30 31  ,.    0x029A7C01
481d1 2c 20 30 78 30 32 39 42 43 30 30 38 2c 20 30 78  , 0x029BC008, 0x
481d2 30 32 39 43 30 30 31 37 2c 20 30 78 30 32 39 43  029C0017, 0x029C
481d3 38 30 30 32 2c 20 30 78 30 32 39 45 32 34 30 32  8002, 0x029E2402
481d4 2c 0a 20 20 20 20 30 78 30 32 41 30 30 38 30 31  ,.    0x02A00801
481d5 2c 20 30 78 30 32 41 30 31 38 30 31 2c 20 30 78  , 0x02A01801, 0x
481d6 30 32 41 30 32 43 30 31 2c 20 30 78 30 32 41 30  02A02C01, 0x02A0
481d7 38 43 30 39 2c 20 30 78 30 32 41 30 44 38 30 34  8C09, 0x02A0D804
481d8 2c 0a 20 20 20 20 30 78 30 32 41 31 44 30 30 34  ,.    0x02A1D004
481d9 2c 20 30 78 30 32 41 32 30 30 30 32 2c 20 30 78  , 0x02A20002, 0x
481da 30 32 41 32 44 30 31 31 2c 20 30 78 30 32 41 33  02A2D011, 0x02A3
481db 33 38 30 32 2c 20 30 78 30 32 41 33 38 30 31 32  3802, 0x02A38012
481dc 2c 0a 20 20 20 20 30 78 30 32 41 33 45 30 30 33  ,.    0x02A3E003
481dd 2c 20 30 78 30 32 41 34 39 38 30 41 2c 20 30 78  , 0x02A4980A, 0x
481de 30 32 41 35 31 43 30 44 2c 20 30 78 30 32 41 35  02A51C0D, 0x02A5
481df 37 43 30 31 2c 20 30 78 30 32 41 36 30 30 30 34  7C01, 0x02A60004
481e0 2c 0a 20 20 20 20 30 78 30 32 41 36 43 43 31 42  ,.    0x02A6CC1B
481e1 2c 20 30 78 30 32 41 37 37 38 30 32 2c 20 30 78  , 0x02A77802, 0x
481e2 30 32 41 38 41 34 30 45 2c 20 30 78 30 32 41 39  02A8A40E, 0x02A9
481e3 30 43 30 31 2c 20 30 78 30 32 41 39 33 30 30 32  0C01, 0x02A93002
481e4 2c 0a 20 20 20 20 30 78 30 32 41 39 37 30 30 34  ,.    0x02A97004
481e5 2c 20 30 78 30 32 41 39 44 43 30 33 2c 20 30 78  , 0x02A9DC03, 0x
481e6 30 32 41 39 45 43 30 31 2c 20 30 78 30 32 41 41  02A9EC01, 0x02AA
481e7 43 30 30 31 2c 20 30 78 30 32 41 41 43 38 30 33  C001, 0x02AAC803
481e8 2c 0a 20 20 20 20 30 78 30 32 41 41 44 43 30 32  ,.    0x02AADC02
481e9 2c 20 30 78 30 32 41 41 46 38 30 32 2c 20 30 78  , 0x02AAF802, 0x
481ea 30 32 41 42 30 34 30 31 2c 20 30 78 30 32 41 42  02AB0401, 0x02AB
481eb 37 38 30 32 2c 20 30 78 30 32 41 42 41 43 30 37  7802, 0x02ABAC07
481ec 2c 0a 20 20 20 20 30 78 30 32 41 42 44 34 30 32  ,.    0x02ABD402
481ed 2c 20 30 78 30 32 41 46 38 43 30 42 2c 20 30 78  , 0x02AF8C0B, 0x
481ee 30 33 36 30 30 30 30 31 2c 20 30 78 30 33 36 44  03600001, 0x036D
481ef 46 43 30 32 2c 20 30 78 30 33 36 46 46 43 30 32  FC02, 0x036FFC02
481f0 2c 0a 20 20 20 20 30 78 30 33 37 46 46 43 30 32  ,.    0x037FFC02
481f1 2c 20 30 78 30 33 45 33 46 43 30 31 2c 20 30 78  , 0x03E3FC01, 0x
481f2 30 33 45 43 37 38 30 31 2c 20 30 78 30 33 45 43  03EC7801, 0x03EC
481f3 41 34 30 31 2c 20 30 78 30 33 45 45 43 38 31 30  A401, 0x03EEC810
481f4 2c 0a 20 20 20 20 30 78 30 33 46 34 46 38 30 32  ,.    0x03F4F802
481f5 2c 20 30 78 30 33 46 37 46 30 30 32 2c 20 30 78  , 0x03F7F002, 0x
481f6 30 33 46 38 30 30 31 41 2c 20 30 78 30 33 46 38  03F8001A, 0x03F8
481f7 38 30 30 37 2c 20 30 78 30 33 46 38 43 30 32 33  8007, 0x03F8C023
481f8 2c 0a 20 20 20 20 30 78 30 33 46 39 35 30 31 33  ,.    0x03F95013
481f9 2c 20 30 78 30 33 46 39 41 30 30 34 2c 20 30 78  , 0x03F9A004, 0x
481fa 30 33 46 42 46 43 30 31 2c 20 30 78 30 33 46 43  03FBFC01, 0x03FC
481fb 30 34 30 46 2c 20 30 78 30 33 46 43 36 38 30 37  040F, 0x03FC6807
481fc 2c 0a 20 20 20 20 30 78 30 33 46 43 45 43 30 36  ,.    0x03FCEC06
481fd 2c 20 30 78 30 33 46 44 36 43 30 42 2c 20 30 78  , 0x03FD6C0B, 0x
481fe 30 33 46 46 38 30 30 37 2c 20 30 78 30 33 46 46  03FF8007, 0x03FF
481ff 41 30 30 37 2c 20 30 78 30 33 46 46 45 34 30 35  A007, 0x03FFE405
48200 2c 0a 20 20 20 20 30 78 30 34 30 34 30 30 30 33  ,.    0x04040003
48201 2c 20 30 78 30 34 30 34 44 43 30 39 2c 20 30 78  , 0x0404DC09, 0x
48202 30 34 30 35 45 34 31 31 2c 20 30 78 30 34 30 36  0405E411, 0x0406
48203 34 30 30 43 2c 20 30 78 30 34 30 37 34 30 32 45  400C, 0x0407402E
48204 2c 0a 20 20 20 20 30 78 30 34 30 45 37 43 30 31  ,.    0x040E7C01
48205 2c 20 30 78 30 34 30 46 34 30 30 31 2c 20 30 78  , 0x040F4001, 0x
48206 30 34 32 31 35 43 30 31 2c 20 30 78 30 34 32 34  04215C01, 0x0424
48207 37 43 30 31 2c 20 30 78 30 34 32 34 46 43 30 31  7C01, 0x0424FC01
48208 2c 0a 20 20 20 20 30 78 30 34 32 38 30 34 30 33  ,.    0x04280403
48209 2c 20 30 78 30 34 32 38 31 34 30 32 2c 20 30 78  , 0x04281402, 0x
4820a 30 34 32 38 33 30 30 34 2c 20 30 78 30 34 32 38  04283004, 0x0428
4820b 45 30 30 33 2c 20 30 78 30 34 32 38 46 43 30 31  E003, 0x0428FC01
4820c 2c 0a 20 20 20 20 30 78 30 34 32 39 34 30 30 39  ,.    0x04294009
4820d 2c 20 30 78 30 34 32 39 46 43 30 31 2c 20 30 78  , 0x0429FC01, 0x
4820e 30 34 32 43 45 34 30 37 2c 20 30 78 30 34 34 30  042CE407, 0x0440
4820f 30 30 30 33 2c 20 30 78 30 34 34 30 45 30 31 36  0003, 0x0440E016
48210 2c 0a 20 20 20 20 30 78 30 34 34 32 30 30 30 33  ,.    0x04420003
48211 2c 20 30 78 30 34 34 32 43 30 31 32 2c 20 30 78  , 0x0442C012, 0x
48212 30 34 34 34 30 30 30 33 2c 20 30 78 30 34 34 34  04440003, 0x0444
48213 39 43 30 45 2c 20 30 78 30 34 34 35 30 30 30 34  9C0E, 0x04450004
48214 2c 0a 20 20 20 20 30 78 30 34 34 36 30 30 30 33  ,.    0x04460003
48215 2c 20 30 78 30 34 34 36 43 43 30 45 2c 20 30 78  , 0x0446CC0E, 0x
48216 30 34 34 37 31 34 30 34 2c 20 30 78 30 34 35 41  04471404, 0x045A
48217 41 43 30 44 2c 20 30 78 30 34 39 31 43 30 30 34  AC0D, 0x0491C004
48218 2c 0a 20 20 20 20 30 78 30 35 42 44 34 34 32 45  ,.    0x05BD442E
48219 2c 20 30 78 30 35 42 45 33 43 30 34 2c 20 30 78  , 0x05BE3C04, 0x
4821a 30 37 34 30 30 30 46 36 2c 20 30 78 30 37 34 34  074000F6, 0x0744
4821b 30 30 32 37 2c 20 30 78 30 37 34 34 41 34 42 35  0027, 0x0744A4B5
4821c 2c 0a 20 20 20 20 30 78 30 37 34 38 30 30 34 36  ,.    0x07480046
4821d 2c 20 30 78 30 37 34 43 30 30 35 37 2c 20 30 78  , 0x074C0057, 0x
4821e 30 37 35 42 30 34 30 31 2c 20 30 78 30 37 35 42  075B0401, 0x075B
4821f 36 43 30 31 2c 20 30 78 30 37 35 42 45 43 30 31  6C01, 0x075BEC01
48220 2c 0a 20 20 20 20 30 78 30 37 35 43 35 34 30 31  ,.    0x075C5401
48221 2c 20 30 78 30 37 35 43 44 34 30 31 2c 20 30 78  , 0x075CD401, 0x
48222 30 37 35 44 33 43 30 31 2c 20 30 78 30 37 35 44  075D3C01, 0x075D
48223 42 43 30 31 2c 20 30 78 30 37 35 45 32 34 30 31  BC01, 0x075E2401
48224 2c 0a 20 20 20 20 30 78 30 37 35 45 41 34 30 31  ,.    0x075EA401
48225 2c 20 30 78 30 37 35 46 30 43 30 31 2c 20 30 78  , 0x075F0C01, 0x
48226 30 37 42 42 43 30 30 32 2c 20 30 78 30 37 43 30  07BBC002, 0x07C0
48227 30 30 32 43 2c 20 30 78 30 37 43 30 43 30 36 34  002C, 0x07C0C064
48228 2c 0a 20 20 20 20 30 78 30 37 43 32 38 30 30 46  ,.    0x07C2800F
48229 2c 20 30 78 30 37 43 32 43 34 30 45 2c 20 30 78  , 0x07C2C40E, 0x
4822a 30 37 43 33 30 34 30 46 2c 20 30 78 30 37 43 33  07C3040F, 0x07C3
4822b 34 34 30 46 2c 20 30 78 30 37 43 34 34 30 31 46  440F, 0x07C4401F
4822c 2c 0a 20 20 20 20 30 78 30 37 43 34 43 30 33 43  ,.    0x07C4C03C
4822d 2c 20 30 78 30 37 43 35 43 30 32 42 2c 20 30 78  , 0x07C5C02B, 0x
4822e 30 37 43 37 39 38 31 44 2c 20 30 78 30 37 43 38  07C7981D, 0x07C8
4822f 34 30 32 42 2c 20 30 78 30 37 43 39 30 30 30 39  402B, 0x07C90009
48230 2c 0a 20 20 20 20 30 78 30 37 43 39 34 30 30 32  ,.    0x07C94002
48231 2c 20 30 78 30 37 43 43 30 30 32 31 2c 20 30 78  , 0x07CC0021, 0x
48232 30 37 43 43 43 30 30 36 2c 20 30 78 30 37 43 43  07CCC006, 0x07CC
48233 44 43 34 36 2c 20 30 78 30 37 43 45 30 30 31 34  DC46, 0x07CE0014
48234 2c 0a 20 20 20 20 30 78 30 37 43 45 38 30 32 35  ,.    0x07CE8025
48235 2c 20 30 78 30 37 43 46 31 38 30 35 2c 20 30 78  , 0x07CF1805, 0x
48236 30 37 43 46 38 30 31 31 2c 20 30 78 30 37 44 30  07CF8011, 0x07D0
48237 30 30 33 46 2c 20 30 78 30 37 44 31 30 30 30 31  003F, 0x07D10001
48238 2c 0a 20 20 20 20 30 78 30 37 44 31 30 38 42 36  ,.    0x07D108B6
48239 2c 20 30 78 30 37 44 33 45 34 30 34 2c 20 30 78  , 0x07D3E404, 0x
4823a 30 37 44 34 30 30 33 45 2c 20 30 78 30 37 44 35  07D4003E, 0x07D5
4823b 30 30 30 34 2c 20 30 78 30 37 44 35 34 30 31 38  0004, 0x07D54018
4823c 2c 0a 20 20 20 20 30 78 30 37 44 37 45 43 34 36  ,.    0x07D7EC46
4823d 2c 20 30 78 30 37 44 39 31 34 30 42 2c 20 30 78  , 0x07D9140B, 0x
4823e 30 37 44 41 30 30 34 36 2c 20 30 78 30 37 44 43  07DA0046, 0x07DC
4823f 30 30 37 34 2c 20 30 78 33 38 30 30 30 34 30 31  0074, 0x38000401
48240 2c 0a 20 20 20 20 30 78 33 38 30 30 38 30 36 30  ,.    0x38008060
48241 2c 20 30 78 33 38 30 34 30 30 46 30 2c 20 30 78  , 0x380400F0, 0x
48242 33 43 30 30 30 30 30 31 2c 20 30 78 33 46 46 46  3C000001, 0x3FFF
48243 46 34 30 31 2c 20 30 78 34 30 30 30 30 30 30 31  F401, 0x40000001
48244 2c 0a 20 20 20 20 30 78 34 33 46 46 46 34 30 31  ,.    0x43FFF401
48245 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20  ,.  };.  static 
48246 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 69  const unsigned i
48247 6e 74 20 61 41 73 63 69 69 5b 34 5d 20 3d 20 7b  nt aAscii[4] = {
48248 0a 20 20 20 20 30 78 46 46 46 46 46 46 46 46 2c  .    0xFFFFFFFF,
48249 20 30 78 46 43 30 30 46 46 46 46 2c 20 30 78 46   0xFC00FFFF, 0xF
4824a 38 30 30 30 30 30 31 2c 20 30 78 46 38 30 30 30  8000001, 0xF8000
4824b 30 30 31 2c 0a 20 20 7d 3b 0a 0a 20 20 69 66 28  001,.  };..  if(
4824c 20 63 3c 31 32 38 20 29 7b 0a 20 20 20 20 72 65   c<128 ){.    re
4824d 74 75 72 6e 20 28 20 28 61 41 73 63 69 69 5b 63  turn ( (aAscii[c
4824e 20 3e 3e 20 35 5d 20 26 20 28 31 20 3c 3c 20 28   >> 5] & (1 << (
4824f 63 20 26 20 30 78 30 30 31 46 29 29 29 3d 3d 30  c & 0x001F)))==0
48250 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   );.  }else if( 
48251 63 3c 28 31 3c 3c 32 32 29 20 29 7b 0a 20 20 20  c<(1<<22) ){.   
48252 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6b 65   unsigned int ke
48253 79 20 3d 20 28 28 28 75 6e 73 69 67 6e 65 64 20  y = (((unsigned 
48254 69 6e 74 29 63 29 3c 3c 31 30 29 20 7c 20 30 78  int)c)<<10) | 0x
48255 30 30 30 30 30 33 46 46 3b 0a 20 20 20 20 69 6e  000003FF;.    in
48256 74 20 69 52 65 73 3b 0a 20 20 20 20 69 6e 74 20  t iRes;.    int 
48257 69 48 69 20 3d 20 73 69 7a 65 6f 66 28 61 45 6e  iHi = sizeof(aEn
48258 74 72 79 29 2f 73 69 7a 65 6f 66 28 61 45 6e 74  try)/sizeof(aEnt
48259 72 79 5b 30 5d 29 20 2d 20 31 3b 0a 20 20 20 20  ry[0]) - 1;.    
4825a 69 6e 74 20 69 4c 6f 20 3d 20 30 3b 0a 20 20 20  int iLo = 0;.   
4825b 20 77 68 69 6c 65 28 20 69 48 69 3e 3d 69 4c 6f   while( iHi>=iLo
4825c 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 54   ){.      int iT
4825d 65 73 74 20 3d 20 28 69 48 69 20 2b 20 69 4c 6f  est = (iHi + iLo
4825e 29 20 2f 20 32 3b 0a 20 20 20 20 20 20 69 66 28  ) / 2;.      if(
4825f 20 6b 65 79 20 3e 3d 20 61 45 6e 74 72 79 5b 69   key >= aEntry[i
48260 54 65 73 74 5d 20 29 7b 0a 20 20 20 20 20 20 20  Test] ){.       
48261 20 69 52 65 73 20 3d 20 69 54 65 73 74 3b 0a 20   iRes = iTest;. 
48262 20 20 20 20 20 20 20 69 4c 6f 20 3d 20 69 54 65         iLo = iTe
48263 73 74 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73  st+1;.      }els
48264 65 7b 0a 20 20 20 20 20 20 20 20 69 48 69 20 3d  e{.        iHi =
48265 20 69 54 65 73 74 2d 31 3b 0a 20 20 20 20 20 20   iTest-1;.      
48266 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  }.    }.    asse
48267 72 74 28 20 61 45 6e 74 72 79 5b 30 5d 3c 6b 65  rt( aEntry[0]<ke
48268 79 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  y );.    assert(
48269 20 6b 65 79 3e 3d 61 45 6e 74 72 79 5b 69 52 65   key>=aEntry[iRe
4826a 73 5d 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  s] );.    return
4826b 20 28 28 28 75 6e 73 69 67 6e 65 64 20 69 6e 74   (((unsigned int
4826c 29 63 29 20 3e 3d 20 28 28 61 45 6e 74 72 79 5b  )c) >= ((aEntry[
4826d 69 52 65 73 5d 3e 3e 31 30 29 20 2b 20 28 61 45  iRes]>>10) + (aE
4826e 6e 74 72 79 5b 69 52 65 73 5d 26 30 78 33 46 46  ntry[iRes]&0x3FF
4826f 29 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  )));.  }.  retur
48270 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49  n 1;.}.../*.** I
48271 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69  f the argument i
48272 73 20 61 20 63 6f 64 65 70 6f 69 6e 74 20 63 6f  s a codepoint co
48273 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 61  rresponding to a
48274 20 6c 6f 77 65 72 63 61 73 65 20 6c 65 74 74 65   lowercase lette
48275 72 0a 2a 2a 20 69 6e 20 74 68 65 20 41 53 43 49  r.** in the ASCI
48276 49 20 72 61 6e 67 65 20 77 69 74 68 20 61 20 64  I range with a d
48277 69 61 63 72 69 74 69 63 20 61 64 64 65 64 2c 20  iacritic added, 
48278 72 65 74 75 72 6e 20 74 68 65 20 63 6f 64 65 70  return the codep
48279 6f 69 6e 74 0a 2a 2a 20 6f 66 20 74 68 65 20 41  oint.** of the A
4827a 53 43 49 49 20 6c 65 74 74 65 72 20 6f 6e 6c 79  SCII letter only
4827b 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  . For example, i
4827c 66 20 70 61 73 73 65 64 20 32 33 35 20 2d 20 22  f passed 235 - "
4827d 4c 41 54 49 4e 0a 2a 2a 20 53 4d 41 4c 4c 20 4c  LATIN.** SMALL L
4827e 45 54 54 45 52 20 45 20 57 49 54 48 20 44 49 41  ETTER E WITH DIA
4827f 45 52 45 53 49 53 22 20 2d 20 72 65 74 75 72 6e  ERESIS" - return
48280 20 36 35 20 28 22 4c 41 54 49 4e 20 53 4d 41 4c   65 ("LATIN SMAL
48281 4c 20 4c 45 54 54 45 52 0a 2a 2a 20 45 22 29 2e  L LETTER.** E").
48282 20 54 68 65 20 72 65 73 75 6c 73 20 6f 66 20 70   The resuls of p
48283 61 73 73 69 6e 67 20 61 20 63 6f 64 65 70 6f 69  assing a codepoi
48284 6e 74 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f  nt that correspo
48285 6e 64 73 20 74 6f 20 61 6e 0a 2a 2a 20 75 70 70  nds to an.** upp
48286 65 72 63 61 73 65 20 6c 65 74 74 65 72 20 61 72  ercase letter ar
48287 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  e undefined..*/.
48288 73 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f 76  static int remov
48289 65 5f 64 69 61 63 72 69 74 69 63 28 69 6e 74 20  e_diacritic(int 
4828a 63 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 73  c){.  unsigned s
4828b 68 6f 72 74 20 61 44 69 61 5b 5d 20 3d 20 7b 0a  hort aDia[] = {.
4828c 20 20 20 20 20 20 20 20 30 2c 20 20 31 37 39 37          0,  1797
4828d 2c 20 20 31 38 34 38 2c 20 20 31 38 35 39 2c 20  ,  1848,  1859, 
4828e 20 31 38 39 31 2c 20 20 31 39 32 38 2c 20 20 31   1891,  1928,  1
4828f 39 34 30 2c 20 20 31 39 39 35 2c 20 0a 20 20 20  940,  1995, .   
48290 20 20 32 30 32 34 2c 20 20 32 30 34 30 2c 20 20    2024,  2040,  
48291 32 30 36 30 2c 20 20 32 31 31 30 2c 20 20 32 31  2060,  2110,  21
48292 36 38 2c 20 20 32 32 30 36 2c 20 20 32 32 36 34  68,  2206,  2264
48293 2c 20 20 32 32 38 36 2c 20 0a 20 20 20 20 20 32  ,  2286, .     2
48294 33 34 34 2c 20 20 32 33 38 33 2c 20 20 32 34 37  344,  2383,  247
48295 32 2c 20 20 32 34 38 38 2c 20 20 32 35 31 36 2c  2,  2488,  2516,
48296 20 20 32 35 39 36 2c 20 20 32 36 36 38 2c 20 20    2596,  2668,  
48297 32 37 33 32 2c 20 0a 20 20 20 20 20 32 37 38 32  2732, .     2782
48298 2c 20 20 32 38 34 32 2c 20 20 32 38 39 34 2c 20  ,  2842,  2894, 
48299 20 32 39 35 34 2c 20 20 32 39 38 34 2c 20 20 33   2954,  2984,  3
4829a 30 30 30 2c 20 20 33 30 32 38 2c 20 20 33 33 33  000,  3028,  333
4829b 36 2c 20 0a 20 20 20 20 20 33 34 35 36 2c 20 20  6, .     3456,  
4829c 33 36 39 36 2c 20 20 33 37 31 32 2c 20 20 33 37  3696,  3712,  37
4829d 32 38 2c 20 20 33 37 34 34 2c 20 20 33 38 39 36  28,  3744,  3896
4829e 2c 20 20 33 39 31 32 2c 20 20 33 39 32 38 2c 20  ,  3912,  3928, 
4829f 0a 20 20 20 20 20 33 39 36 38 2c 20 20 34 30 30  .     3968,  400
482a0 38 2c 20 20 34 30 34 30 2c 20 20 34 31 30 36 2c  8,  4040,  4106,
482a1 20 20 34 31 33 38 2c 20 20 34 31 37 30 2c 20 20    4138,  4170,  
482a2 34 32 30 32 2c 20 20 34 32 33 34 2c 20 0a 20 20  4202,  4234, .  
482a3 20 20 20 34 32 36 36 2c 20 20 34 32 39 36 2c 20     4266,  4296, 
482a4 20 34 33 31 32 2c 20 20 34 33 34 34 2c 20 20 34   4312,  4344,  4
482a5 34 30 38 2c 20 20 34 34 32 34 2c 20 20 34 34 37  408,  4424,  447
482a6 32 2c 20 20 34 35 30 34 2c 20 0a 20 20 20 20 20  2,  4504, .     
482a7 36 31 34 38 2c 20 20 36 31 39 38 2c 20 20 36 32  6148,  6198,  62
482a8 36 34 2c 20 20 36 32 38 30 2c 20 20 36 33 36 30  64,  6280,  6360
482a9 2c 20 20 36 34 32 39 2c 20 20 36 35 30 35 2c 20  ,  6429,  6505, 
482aa 20 36 35 32 39 2c 20 0a 20 20 20 20 36 31 34 34   6529, .    6144
482ab 38 2c 20 36 31 34 36 38 2c 20 36 31 35 33 34 2c  8, 61468, 61534,
482ac 20 36 31 35 39 32 2c 20 36 31 36 34 32 2c 20 36   61592, 61642, 6
482ad 31 36 38 38 2c 20 36 31 37 30 34 2c 20 36 31 37  1688, 61704, 617
482ae 32 36 2c 20 0a 20 20 20 20 36 31 37 38 34 2c 20  26, .    61784, 
482af 36 31 38 30 30 2c 20 36 31 38 33 36 2c 20 36 31  61800, 61836, 61
482b0 38 38 30 2c 20 36 31 39 31 34 2c 20 36 31 39 34  880, 61914, 6194
482b1 38 2c 20 36 31 39 39 38 2c 20 36 32 31 32 32 2c  8, 61998, 62122,
482b2 20 0a 20 20 20 20 36 32 31 35 34 2c 20 36 32 32   .    62154, 622
482b3 30 30 2c 20 36 32 32 31 38 2c 20 36 32 33 30 32  00, 62218, 62302
482b4 2c 20 36 32 33 36 34 2c 20 36 32 34 34 32 2c 20  , 62364, 62442, 
482b5 36 32 34 37 38 2c 20 36 32 35 33 36 2c 20 0a 20  62478, 62536, . 
482b6 20 20 20 36 32 35 35 34 2c 20 36 32 35 38 34 2c     62554, 62584,
482b7 20 36 32 36 30 34 2c 20 36 32 36 34 30 2c 20 36   62604, 62640, 6
482b8 32 36 34 38 2c 20 36 32 36 35 36 2c 20 36 32 36  2648, 62656, 626
482b9 36 34 2c 20 36 32 37 33 30 2c 20 0a 20 20 20 20  64, 62730, .    
482ba 36 32 39 32 34 2c 20 36 33 30 35 30 2c 20 36 33  62924, 63050, 63
482bb 30 38 32 2c 20 36 33 32 37 34 2c 20 36 33 33 39  082, 63274, 6339
482bc 30 2c 20 0a 20 20 7d 3b 0a 20 20 63 68 61 72 20  0, .  };.  char 
482bd 61 43 68 61 72 5b 5d 20 3d 20 7b 0a 20 20 20 20  aChar[] = {.    
482be 27 5c 30 27 2c 20 27 61 27 2c 20 20 27 63 27 2c  '\0', 'a',  'c',
482bf 20 20 27 65 27 2c 20 20 27 69 27 2c 20 20 27 6e    'e',  'i',  'n
482c0 27 2c 20 20 27 6f 27 2c 20 20 27 75 27 2c 20 20  ',  'o',  'u',  
482c1 27 79 27 2c 20 20 27 79 27 2c 20 20 27 61 27 2c  'y',  'y',  'a',
482c2 20 20 27 63 27 2c 20 20 0a 20 20 20 20 27 64 27    'c',  .    'd'
482c3 2c 20 20 27 65 27 2c 20 20 27 65 27 2c 20 20 27  ,  'e',  'e',  '
482c4 67 27 2c 20 20 27 68 27 2c 20 20 27 69 27 2c 20  g',  'h',  'i', 
482c5 20 27 6a 27 2c 20 20 27 6b 27 2c 20 20 27 6c 27   'j',  'k',  'l'
482c6 2c 20 20 27 6e 27 2c 20 20 27 6f 27 2c 20 20 27  ,  'n',  'o',  '
482c7 72 27 2c 20 20 0a 20 20 20 20 27 73 27 2c 20 20  r',  .    's',  
482c8 27 74 27 2c 20 20 27 75 27 2c 20 20 27 75 27 2c  't',  'u',  'u',
482c9 20 20 27 77 27 2c 20 20 27 79 27 2c 20 20 27 7a    'w',  'y',  'z
482ca 27 2c 20 20 27 6f 27 2c 20 20 27 75 27 2c 20 20  ',  'o',  'u',  
482cb 27 61 27 2c 20 20 27 69 27 2c 20 20 27 6f 27 2c  'a',  'i',  'o',
482cc 20 20 0a 20 20 20 20 27 75 27 2c 20 20 27 67 27    .    'u',  'g'
482cd 2c 20 20 27 6b 27 2c 20 20 27 6f 27 2c 20 20 27  ,  'k',  'o',  '
482ce 6a 27 2c 20 20 27 67 27 2c 20 20 27 6e 27 2c 20  j',  'g',  'n', 
482cf 20 27 61 27 2c 20 20 27 65 27 2c 20 20 27 69 27   'a',  'e',  'i'
482d0 2c 20 20 27 6f 27 2c 20 20 27 72 27 2c 20 20 0a  ,  'o',  'r',  .
482d1 20 20 20 20 27 75 27 2c 20 20 27 73 27 2c 20 20      'u',  's',  
482d2 27 74 27 2c 20 20 27 68 27 2c 20 20 27 61 27 2c  't',  'h',  'a',
482d3 20 20 27 65 27 2c 20 20 27 6f 27 2c 20 20 27 79    'e',  'o',  'y
482d4 27 2c 20 20 27 5c 30 27 2c 20 27 5c 30 27 2c 20  ',  '\0', '\0', 
482d5 27 5c 30 27 2c 20 27 5c 30 27 2c 20 0a 20 20 20  '\0', '\0', .   
482d6 20 27 5c 30 27 2c 20 27 5c 30 27 2c 20 27 5c 30   '\0', '\0', '\0
482d7 27 2c 20 27 5c 30 27 2c 20 27 61 27 2c 20 20 27  ', '\0', 'a',  '
482d8 62 27 2c 20 20 27 64 27 2c 20 20 27 64 27 2c 20  b',  'd',  'd', 
482d9 20 27 65 27 2c 20 20 27 66 27 2c 20 20 27 67 27   'e',  'f',  'g'
482da 2c 20 20 27 68 27 2c 20 20 0a 20 20 20 20 27 68  ,  'h',  .    'h
482db 27 2c 20 20 27 69 27 2c 20 20 27 6b 27 2c 20 20  ',  'i',  'k',  
482dc 27 6c 27 2c 20 20 27 6c 27 2c 20 20 27 6d 27 2c  'l',  'l',  'm',
482dd 20 20 27 6e 27 2c 20 20 27 70 27 2c 20 20 27 72    'n',  'p',  'r
482de 27 2c 20 20 27 72 27 2c 20 20 27 73 27 2c 20 20  ',  'r',  's',  
482df 27 74 27 2c 20 20 0a 20 20 20 20 27 75 27 2c 20  't',  .    'u', 
482e0 20 27 76 27 2c 20 20 27 77 27 2c 20 20 27 77 27   'v',  'w',  'w'
482e1 2c 20 20 27 78 27 2c 20 20 27 79 27 2c 20 20 27  ,  'x',  'y',  '
482e2 7a 27 2c 20 20 27 68 27 2c 20 20 27 74 27 2c 20  z',  'h',  't', 
482e3 20 27 77 27 2c 20 20 27 79 27 2c 20 20 27 61 27   'w',  'y',  'a'
482e4 2c 20 20 0a 20 20 20 20 27 65 27 2c 20 20 27 69  ,  .    'e',  'i
482e5 27 2c 20 20 27 6f 27 2c 20 20 27 75 27 2c 20 20  ',  'o',  'u',  
482e6 27 79 27 2c 20 20 0a 20 20 7d 3b 0a 0a 20 20 75  'y',  .  };..  u
482e7 6e 73 69 67 6e 65 64 20 69 6e 74 20 6b 65 79 20  nsigned int key 
482e8 3d 20 28 28 28 75 6e 73 69 67 6e 65 64 20 69 6e  = (((unsigned in
482e9 74 29 63 29 3c 3c 33 29 20 7c 20 30 78 30 30 30  t)c)<<3) | 0x000
482ea 30 30 30 30 37 3b 0a 20 20 69 6e 74 20 69 52 65  00007;.  int iRe
482eb 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 48 69  s = 0;.  int iHi
482ec 20 3d 20 73 69 7a 65 6f 66 28 61 44 69 61 29 2f   = sizeof(aDia)/
482ed 73 69 7a 65 6f 66 28 61 44 69 61 5b 30 5d 29 20  sizeof(aDia[0]) 
482ee 2d 20 31 3b 0a 20 20 69 6e 74 20 69 4c 6f 20 3d  - 1;.  int iLo =
482ef 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 48 69   0;.  while( iHi
482f0 3e 3d 69 4c 6f 20 29 7b 0a 20 20 20 20 69 6e 74  >=iLo ){.    int
482f1 20 69 54 65 73 74 20 3d 20 28 69 48 69 20 2b 20   iTest = (iHi + 
482f2 69 4c 6f 29 20 2f 20 32 3b 0a 20 20 20 20 69 66  iLo) / 2;.    if
482f3 28 20 6b 65 79 20 3e 3d 20 61 44 69 61 5b 69 54  ( key >= aDia[iT
482f4 65 73 74 5d 20 29 7b 0a 20 20 20 20 20 20 69 52  est] ){.      iR
482f5 65 73 20 3d 20 69 54 65 73 74 3b 0a 20 20 20 20  es = iTest;.    
482f6 20 20 69 4c 6f 20 3d 20 69 54 65 73 74 2b 31 3b    iLo = iTest+1;
482f7 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
482f8 20 20 69 48 69 20 3d 20 69 54 65 73 74 2d 31 3b    iHi = iTest-1;
482f9 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73  .    }.  }.  ass
482fa 65 72 74 28 20 6b 65 79 3e 3d 61 44 69 61 5b 69  ert( key>=aDia[i
482fb 52 65 73 5d 20 29 3b 0a 20 20 72 65 74 75 72 6e  Res] );.  return
482fc 20 28 28 63 20 3e 20 28 61 44 69 61 5b 69 52 65   ((c > (aDia[iRe
482fd 73 5d 3e 3e 33 29 20 2b 20 28 61 44 69 61 5b 69  s]>>3) + (aDia[i
482fe 52 65 73 5d 26 30 78 30 37 29 29 20 3f 20 63 20  Res]&0x07)) ? c 
482ff 3a 20 28 69 6e 74 29 61 43 68 61 72 5b 69 52 65  : (int)aChar[iRe
48300 73 5d 29 3b 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20  s]);.};.../*.** 
48301 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
48302 68 65 20 61 72 67 75 6d 65 6e 74 20 69 6e 74 65  he argument inte
48303 72 70 72 65 74 65 64 20 61 73 20 61 20 75 6e 69  rpreted as a uni
48304 63 6f 64 65 20 63 6f 64 65 70 6f 69 6e 74 0a 2a  code codepoint.*
48305 2a 20 69 73 20 61 20 64 69 61 63 72 69 74 69 63  * is a diacritic
48306 61 6c 20 6d 6f 64 69 66 69 65 72 20 63 68 61 72  al modifier char
48307 61 63 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  acter..*/.SQLITE
48308 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
48309 69 74 65 33 46 74 73 55 6e 69 63 6f 64 65 49 73  ite3FtsUnicodeIs
4830a 64 69 61 63 72 69 74 69 63 28 69 6e 74 20 63 29  diacritic(int c)
4830b 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  {.  unsigned int
4830c 20 6d 61 73 6b 30 20 3d 20 30 78 30 38 30 32 39   mask0 = 0x08029
4830d 46 44 46 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  FDF;.  unsigned 
4830e 69 6e 74 20 6d 61 73 6b 31 20 3d 20 30 78 30 30  int mask1 = 0x00
4830f 30 33 36 31 46 38 3b 0a 20 20 69 66 28 20 63 3c  0361F8;.  if( c<
48310 37 36 38 20 7c 7c 20 63 3e 38 31 37 20 29 20 72  768 || c>817 ) r
48311 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72  eturn 0;.  retur
48312 6e 20 28 63 20 3c 20 37 36 38 2b 33 32 29 20 3f  n (c < 768+32) ?
48313 0a 20 20 20 20 20 20 28 6d 61 73 6b 30 20 26 20  .      (mask0 & 
48314 28 31 20 3c 3c 20 28 63 2d 37 36 38 29 29 29 20  (1 << (c-768))) 
48315 3a 0a 20 20 20 20 20 20 28 6d 61 73 6b 31 20 26  :.      (mask1 &
48316 20 28 31 20 3c 3c 20 28 63 2d 37 36 38 2d 33 32   (1 << (c-768-32
48317 29 29 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49  )));.}.../*.** I
48318 6e 74 65 72 70 72 65 74 20 74 68 65 20 61 72 67  nterpret the arg
48319 75 6d 65 6e 74 20 61 73 20 61 20 75 6e 69 63 6f  ument as a unico
4831a 64 65 20 63 6f 64 65 70 6f 69 6e 74 2e 20 49 66  de codepoint. If
4831b 20 74 68 65 20 63 6f 64 65 70 6f 69 6e 74 0a 2a   the codepoint.*
4831c 2a 20 69 73 20 61 6e 20 75 70 70 65 72 20 63 61  * is an upper ca
4831d 73 65 20 63 68 61 72 61 63 74 65 72 20 74 68 61  se character tha
4831e 74 20 68 61 73 20 61 20 6c 6f 77 65 72 20 63 61  t has a lower ca
4831f 73 65 20 65 71 75 69 76 61 6c 65 6e 74 2c 0a 2a  se equivalent,.*
48320 2a 20 72 65 74 75 72 6e 20 74 68 65 20 63 6f 64  * return the cod
48321 65 70 6f 69 6e 74 20 63 6f 72 72 65 73 70 6f 6e  epoint correspon
48322 64 69 6e 67 20 74 6f 20 74 68 65 20 6c 6f 77 65  ding to the lowe
48323 72 20 63 61 73 65 20 76 65 72 73 69 6f 6e 2e 0a  r case version..
48324 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65  ** Otherwise, re
48325 74 75 72 6e 20 61 20 63 6f 70 79 20 6f 66 20 74  turn a copy of t
48326 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  he argument..**.
48327 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 61  ** The results a
48328 72 65 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20  re undefined if 
48329 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64  the value passed
4832a 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
4832b 6e 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74 68 61  n.** is less tha
4832c 6e 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54  n zero..*/.SQLIT
4832d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4832e 6c 69 74 65 33 46 74 73 55 6e 69 63 6f 64 65 46  lite3FtsUnicodeF
4832f 6f 6c 64 28 69 6e 74 20 63 2c 20 69 6e 74 20 62  old(int c, int b
48330 52 65 6d 6f 76 65 44 69 61 63 72 69 74 69 63 29  RemoveDiacritic)
48331 7b 0a 20 20 2f 2a 20 45 61 63 68 20 65 6e 74 72  {.  /* Each entr
48332 79 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  y in the followi
48333 6e 67 20 61 72 72 61 79 20 64 65 66 69 6e 65 73  ng array defines
48334 20 61 20 72 75 6c 65 20 66 6f 72 20 66 6f 6c 64   a rule for fold
48335 69 6e 67 20 61 20 72 61 6e 67 65 0a 20 20 2a 2a  ing a range.  **
48336 20 6f 66 20 63 6f 64 65 70 6f 69 6e 74 73 20 74   of codepoints t
48337 6f 20 6c 6f 77 65 72 20 63 61 73 65 2e 20 54 68  o lower case. Th
48338 65 20 72 75 6c 65 20 61 70 70 6c 69 65 73 20 74  e rule applies t
48339 6f 20 61 20 72 61 6e 67 65 20 6f 66 20 6e 52 61  o a range of nRa
4833a 6e 67 65 0a 20 20 2a 2a 20 63 6f 64 65 70 6f 69  nge.  ** codepoi
4833b 6e 74 73 20 73 74 61 72 74 69 6e 67 20 61 74 20  nts starting at 
4833c 63 6f 64 65 70 6f 69 6e 74 20 69 43 6f 64 65 2e  codepoint iCode.
4833d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68  .  **.  ** If th
4833e 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66 69 63  e least signific
4833f 61 6e 74 20 62 69 74 20 69 6e 20 66 6c 61 67 73  ant bit in flags
48340 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65 6e 20   is clear, then 
48341 74 68 65 20 72 75 6c 65 20 61 70 70 6c 69 65 73  the rule applies
48342 0a 20 20 2a 2a 20 74 6f 20 61 6c 6c 20 6e 52 61  .  ** to all nRa
48343 6e 67 65 20 63 6f 64 65 70 6f 69 6e 74 73 20 28  nge codepoints (
48344 69 2e 65 2e 20 61 6c 6c 20 6e 52 61 6e 67 65 20  i.e. all nRange 
48345 63 6f 64 65 70 6f 69 6e 74 73 20 61 72 65 20 75  codepoints are u
48346 70 70 65 72 20 63 61 73 65 20 61 6e 64 0a 20 20  pper case and.  
48347 2a 2a 20 6e 65 65 64 20 74 6f 20 62 65 20 66 6f  ** need to be fo
48348 6c 64 65 64 29 2e 20 4f 72 2c 20 69 66 20 69 74  lded). Or, if it
48349 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68   is set, then th
4834a 65 20 72 75 6c 65 20 6f 6e 6c 79 20 61 70 70 6c  e rule only appl
4834b 69 65 73 20 74 6f 0a 20 20 2a 2a 20 65 76 65 72  ies to.  ** ever
4834c 79 20 73 65 63 6f 6e 64 20 63 6f 64 65 70 6f 69  y second codepoi
4834d 6e 74 20 69 6e 20 74 68 65 20 72 61 6e 67 65 2c  nt in the range,
4834e 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 63   starting with c
4834f 6f 64 65 70 6f 69 6e 74 20 43 2e 0a 20 20 2a 2a  odepoint C..  **
48350 0a 20 20 2a 2a 20 54 68 65 20 37 20 6d 6f 73 74  .  ** The 7 most
48351 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74   significant bit
48352 73 20 69 6e 20 66 6c 61 67 73 20 61 72 65 20 61  s in flags are a
48353 6e 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65  n index into the
48354 20 61 69 4f 66 66 5b 5d 0a 20 20 2a 2a 20 61 72   aiOff[].  ** ar
48355 72 61 79 2e 20 49 66 20 61 20 73 70 65 63 69 66  ray. If a specif
48356 69 63 20 63 6f 64 65 70 6f 69 6e 74 20 43 20 64  ic codepoint C d
48357 6f 65 73 20 72 65 71 75 69 72 65 20 66 6f 6c 64  oes require fold
48358 69 6e 67 2c 20 74 68 65 6e 20 69 74 73 20 6c 6f  ing, then its lo
48359 77 65 72 0a 20 20 2a 2a 20 63 61 73 65 20 65 71  wer.  ** case eq
4835a 75 69 76 61 6c 65 6e 74 20 69 73 20 28 28 43 20  uivalent is ((C 
4835b 2b 20 61 69 4f 66 66 5b 66 6c 61 67 73 3e 3e 31  + aiOff[flags>>1
4835c 5d 29 20 26 20 30 78 46 46 46 46 29 2e 0a 20 20  ]) & 0xFFFF)..  
4835d 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6e 74  **.  ** The cont
4835e 65 6e 74 73 20 6f 66 20 74 68 69 73 20 61 72 72  ents of this arr
4835f 61 79 20 61 72 65 20 67 65 6e 65 72 61 74 65 64  ay are generated
48360 20 62 79 20 70 61 72 73 69 6e 67 20 74 68 65 20   by parsing the 
48361 43 61 73 65 46 6f 6c 64 69 6e 67 2e 74 78 74 0a  CaseFolding.txt.
48362 20 20 2a 2a 20 66 69 6c 65 20 64 69 73 74 72 69    ** file distri
48363 62 75 74 65 64 20 61 73 20 70 61 72 74 20 6f 66  buted as part of
48364 20 74 68 65 20 22 55 6e 69 63 6f 64 65 20 43 68   the "Unicode Ch
48365 61 72 61 63 74 65 72 20 44 61 74 61 62 61 73 65  aracter Database
48366 22 2e 20 53 65 65 0a 20 20 2a 2a 20 68 74 74 70  ". See.  ** http
48367 3a 2f 2f 77 77 77 2e 75 6e 69 63 6f 64 65 2e 6f  ://www.unicode.o
48368 72 67 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a  rg for details..
48369 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f    */.  static co
4836a 6e 73 74 20 73 74 72 75 63 74 20 54 61 62 6c 65  nst struct Table
4836b 45 6e 74 72 79 20 7b 0a 20 20 20 20 75 6e 73 69  Entry {.    unsi
4836c 67 6e 65 64 20 73 68 6f 72 74 20 69 43 6f 64 65  gned short iCode
4836d 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  ;.    unsigned c
4836e 68 61 72 20 66 6c 61 67 73 3b 0a 20 20 20 20 75  har flags;.    u
4836f 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6e 52 61  nsigned char nRa
48370 6e 67 65 3b 0a 20 20 7d 20 61 45 6e 74 72 79 5b  nge;.  } aEntry[
48371 5d 20 3d 20 7b 0a 20 20 20 20 7b 36 35 2c 20 31  ] = {.    {65, 1
48372 34 2c 20 32 36 7d 2c 20 20 20 20 20 20 20 20 20  4, 26},         
48373 20 7b 31 38 31 2c 20 36 34 2c 20 31 7d 2c 20 20   {181, 64, 1},  
48374 20 20 20 20 20 20 20 20 7b 31 39 32 2c 20 31 34          {192, 14
48375 2c 20 32 33 7d 2c 0a 20 20 20 20 7b 32 31 36 2c  , 23},.    {216,
48376 20 31 34 2c 20 37 7d 2c 20 20 20 20 20 20 20 20   14, 7},        
48377 20 20 7b 32 35 36 2c 20 31 2c 20 34 38 7d 2c 20    {256, 1, 48}, 
48378 20 20 20 20 20 20 20 20 20 7b 33 30 36 2c 20 31           {306, 1
48379 2c 20 36 7d 2c 0a 20 20 20 20 7b 33 31 33 2c 20  , 6},.    {313, 
4837a 31 2c 20 31 36 7d 2c 20 20 20 20 20 20 20 20 20  1, 16},         
4837b 20 7b 33 33 30 2c 20 31 2c 20 34 36 7d 2c 20 20   {330, 1, 46},  
4837c 20 20 20 20 20 20 20 20 7b 33 37 36 2c 20 31 31          {376, 11
4837d 36 2c 20 31 7d 2c 0a 20 20 20 20 7b 33 37 37 2c  6, 1},.    {377,
4837e 20 31 2c 20 36 7d 2c 20 20 20 20 20 20 20 20 20   1, 6},         
4837f 20 20 7b 33 38 33 2c 20 31 30 34 2c 20 31 7d 2c    {383, 104, 1},
48380 20 20 20 20 20 20 20 20 20 7b 33 38 35 2c 20 35           {385, 5
48381 30 2c 20 31 7d 2c 0a 20 20 20 20 7b 33 38 36 2c  0, 1},.    {386,
48382 20 31 2c 20 34 7d 2c 20 20 20 20 20 20 20 20 20   1, 4},         
48383 20 20 7b 33 39 30 2c 20 34 34 2c 20 31 7d 2c 20    {390, 44, 1}, 
48384 20 20 20 20 20 20 20 20 20 7b 33 39 31 2c 20 30           {391, 0
48385 2c 20 31 7d 2c 0a 20 20 20 20 7b 33 39 33 2c 20  , 1},.    {393, 
48386 34 32 2c 20 32 7d 2c 20 20 20 20 20 20 20 20 20  42, 2},         
48387 20 7b 33 39 35 2c 20 30 2c 20 31 7d 2c 20 20 20   {395, 0, 1},   
48388 20 20 20 20 20 20 20 20 7b 33 39 38 2c 20 33 32          {398, 32
48389 2c 20 31 7d 2c 0a 20 20 20 20 7b 33 39 39 2c 20  , 1},.    {399, 
4838a 33 38 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20  38, 1},         
4838b 20 7b 34 30 30 2c 20 34 30 2c 20 31 7d 2c 20 20   {400, 40, 1},  
4838c 20 20 20 20 20 20 20 20 7b 34 30 31 2c 20 30 2c          {401, 0,
4838d 20 31 7d 2c 0a 20 20 20 20 7b 34 30 33 2c 20 34   1},.    {403, 4
4838e 32 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20  2, 1},          
4838f 7b 34 30 34 2c 20 34 36 2c 20 31 7d 2c 20 20 20  {404, 46, 1},   
48390 20 20 20 20 20 20 20 7b 34 30 36 2c 20 35 32 2c         {406, 52,
48391 20 31 7d 2c 0a 20 20 20 20 7b 34 30 37 2c 20 34   1},.    {407, 4
48392 38 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20  8, 1},          
48393 7b 34 30 38 2c 20 30 2c 20 31 7d 2c 20 20 20 20  {408, 0, 1},    
48394 20 20 20 20 20 20 20 7b 34 31 32 2c 20 35 32 2c         {412, 52,
48395 20 31 7d 2c 0a 20 20 20 20 7b 34 31 33 2c 20 35   1},.    {413, 5
48396 34 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20  4, 1},          
48397 7b 34 31 35 2c 20 35 36 2c 20 31 7d 2c 20 20 20  {415, 56, 1},   
48398 20 20 20 20 20 20 20 7b 34 31 36 2c 20 31 2c 20         {416, 1, 
48399 36 7d 2c 0a 20 20 20 20 7b 34 32 32 2c 20 36 30  6},.    {422, 60
4839a 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20 7b  , 1},          {
4839b 34 32 33 2c 20 30 2c 20 31 7d 2c 20 20 20 20 20  423, 0, 1},     
4839c 20 20 20 20 20 20 7b 34 32 35 2c 20 36 30 2c 20        {425, 60, 
4839d 31 7d 2c 0a 20 20 20 20 7b 34 32 38 2c 20 30 2c  1},.    {428, 0,
4839e 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20 20 7b   1},           {
4839f 34 33 30 2c 20 36 30 2c 20 31 7d 2c 20 20 20 20  430, 60, 1},    
483a0 20 20 20 20 20 20 7b 34 33 31 2c 20 30 2c 20 31        {431, 0, 1
483a1 7d 2c 0a 20 20 20 20 7b 34 33 33 2c 20 35 38 2c  },.    {433, 58,
483a2 20 32 7d 2c 20 20 20 20 20 20 20 20 20 20 7b 34   2},          {4
483a3 33 35 2c 20 31 2c 20 34 7d 2c 20 20 20 20 20 20  35, 1, 4},      
483a4 20 20 20 20 20 7b 34 33 39 2c 20 36 32 2c 20 31       {439, 62, 1
483a5 7d 2c 0a 20 20 20 20 7b 34 34 30 2c 20 30 2c 20  },.    {440, 0, 
483a6 31 7d 2c 20 20 20 20 20 20 20 20 20 20 20 7b 34  1},           {4
483a7 34 34 2c 20 30 2c 20 31 7d 2c 20 20 20 20 20 20  44, 0, 1},      
483a8 20 20 20 20 20 7b 34 35 32 2c 20 32 2c 20 31 7d       {452, 2, 1}
483a9 2c 0a 20 20 20 20 7b 34 35 33 2c 20 30 2c 20 31  ,.    {453, 0, 1
483aa 7d 2c 20 20 20 20 20 20 20 20 20 20 20 7b 34 35  },           {45
483ab 35 2c 20 32 2c 20 31 7d 2c 20 20 20 20 20 20 20  5, 2, 1},       
483ac 20 20 20 20 7b 34 35 36 2c 20 30 2c 20 31 7d 2c      {456, 0, 1},
483ad 0a 20 20 20 20 7b 34 35 38 2c 20 32 2c 20 31 7d  .    {458, 2, 1}
483ae 2c 20 20 20 20 20 20 20 20 20 20 20 7b 34 35 39  ,           {459
483af 2c 20 31 2c 20 31 38 7d 2c 20 20 20 20 20 20 20  , 1, 18},       
483b0 20 20 20 7b 34 37 38 2c 20 31 2c 20 31 38 7d 2c     {478, 1, 18},
483b1 0a 20 20 20 20 7b 34 39 37 2c 20 32 2c 20 31 7d  .    {497, 2, 1}
483b2 2c 20 20 20 20 20 20 20 20 20 20 20 7b 34 39 38  ,           {498
483b3 2c 20 31 2c 20 34 7d 2c 20 20 20 20 20 20 20 20  , 1, 4},        
483b4 20 20 20 7b 35 30 32 2c 20 31 32 32 2c 20 31 7d     {502, 122, 1}
483b5 2c 0a 20 20 20 20 7b 35 30 33 2c 20 31 33 34 2c  ,.    {503, 134,
483b6 20 31 7d 2c 20 20 20 20 20 20 20 20 20 7b 35 30   1},         {50
483b7 34 2c 20 31 2c 20 34 30 7d 2c 20 20 20 20 20 20  4, 1, 40},      
483b8 20 20 20 20 7b 35 34 34 2c 20 31 31 30 2c 20 31      {544, 110, 1
483b9 7d 2c 0a 20 20 20 20 7b 35 34 36 2c 20 31 2c 20  },.    {546, 1, 
483ba 31 38 7d 2c 20 20 20 20 20 20 20 20 20 20 7b 35  18},          {5
483bb 37 30 2c 20 37 30 2c 20 31 7d 2c 20 20 20 20 20  70, 70, 1},     
483bc 20 20 20 20 20 7b 35 37 31 2c 20 30 2c 20 31 7d       {571, 0, 1}
483bd 2c 0a 20 20 20 20 7b 35 37 33 2c 20 31 30 38 2c  ,.    {573, 108,
483be 20 31 7d 2c 20 20 20 20 20 20 20 20 20 7b 35 37   1},         {57
483bf 34 2c 20 36 38 2c 20 31 7d 2c 20 20 20 20 20 20  4, 68, 1},      
483c0 20 20 20 20 7b 35 37 37 2c 20 30 2c 20 31 7d 2c      {577, 0, 1},
483c1 0a 20 20 20 20 7b 35 37 39 2c 20 31 30 36 2c 20  .    {579, 106, 
483c2 31 7d 2c 20 20 20 20 20 20 20 20 20 7b 35 38 30  1},         {580
483c3 2c 20 32 38 2c 20 31 7d 2c 20 20 20 20 20 20 20  , 28, 1},       
483c4 20 20 20 7b 35 38 31 2c 20 33 30 2c 20 31 7d 2c     {581, 30, 1},
483c5 0a 20 20 20 20 7b 35 38 32 2c 20 31 2c 20 31 30  .    {582, 1, 10
483c6 7d 2c 20 20 20 20 20 20 20 20 20 20 7b 38 33 37  },          {837
483c7 2c 20 33 36 2c 20 31 7d 2c 20 20 20 20 20 20 20  , 36, 1},       
483c8 20 20 20 7b 38 38 30 2c 20 31 2c 20 34 7d 2c 0a     {880, 1, 4},.
483c9 20 20 20 20 7b 38 38 36 2c 20 30 2c 20 31 7d 2c      {886, 0, 1},
483ca 20 20 20 20 20 20 20 20 20 20 20 7b 39 30 32 2c             {902,
483cb 20 31 38 2c 20 31 7d 2c 20 20 20 20 20 20 20 20   18, 1},        
483cc 20 20 7b 39 30 34 2c 20 31 36 2c 20 33 7d 2c 0a    {904, 16, 3},.
483cd 20 20 20 20 7b 39 30 38 2c 20 32 36 2c 20 31 7d      {908, 26, 1}
483ce 2c 20 20 20 20 20 20 20 20 20 20 7b 39 31 30 2c  ,          {910,
483cf 20 32 34 2c 20 32 7d 2c 20 20 20 20 20 20 20 20   24, 2},        
483d0 20 20 7b 39 31 33 2c 20 31 34 2c 20 31 37 7d 2c    {913, 14, 17},
483d1 0a 20 20 20 20 7b 39 33 31 2c 20 31 34 2c 20 39  .    {931, 14, 9
483d2 7d 2c 20 20 20 20 20 20 20 20 20 20 7b 39 36 32  },          {962
483d3 2c 20 30 2c 20 31 7d 2c 20 20 20 20 20 20 20 20  , 0, 1},        
483d4 20 20 20 7b 39 37 35 2c 20 34 2c 20 31 7d 2c 0a     {975, 4, 1},.
483d5 20 20 20 20 7b 39 37 36 2c 20 31 34 30 2c 20 31      {976, 140, 1
483d6 7d 2c 20 20 20 20 20 20 20 20 20 7b 39 37 37 2c  },         {977,
483d7 20 31 34 32 2c 20 31 7d 2c 20 20 20 20 20 20 20   142, 1},       
483d8 20 20 7b 39 38 31 2c 20 31 34 36 2c 20 31 7d 2c    {981, 146, 1},
483d9 0a 20 20 20 20 7b 39 38 32 2c 20 31 34 34 2c 20  .    {982, 144, 
483da 31 7d 2c 20 20 20 20 20 20 20 20 20 7b 39 38 34  1},         {984
483db 2c 20 31 2c 20 32 34 7d 2c 20 20 20 20 20 20 20  , 1, 24},       
483dc 20 20 20 7b 31 30 30 38 2c 20 31 33 36 2c 20 31     {1008, 136, 1
483dd 7d 2c 0a 20 20 20 20 7b 31 30 30 39 2c 20 31 33  },.    {1009, 13
483de 38 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 7b 31  8, 1},        {1
483df 30 31 32 2c 20 31 33 30 2c 20 31 7d 2c 20 20 20  012, 130, 1},   
483e0 20 20 20 20 20 7b 31 30 31 33 2c 20 31 32 38 2c       {1013, 128,
483e1 20 31 7d 2c 0a 20 20 20 20 7b 31 30 31 35 2c 20   1},.    {1015, 
483e2 30 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20  0, 1},          
483e3 7b 31 30 31 37 2c 20 31 35 32 2c 20 31 7d 2c 20  {1017, 152, 1}, 
483e4 20 20 20 20 20 20 20 7b 31 30 31 38 2c 20 30 2c         {1018, 0,
483e5 20 31 7d 2c 0a 20 20 20 20 7b 31 30 32 31 2c 20   1},.    {1021, 
483e6 31 31 30 2c 20 33 7d 2c 20 20 20 20 20 20 20 20  110, 3},        
483e7 7b 31 30 32 34 2c 20 33 34 2c 20 31 36 7d 2c 20  {1024, 34, 16}, 
483e8 20 20 20 20 20 20 20 7b 31 30 34 30 2c 20 31 34         {1040, 14
483e9 2c 20 33 32 7d 2c 0a 20 20 20 20 7b 31 31 32 30  , 32},.    {1120
483ea 2c 20 31 2c 20 33 34 7d 2c 20 20 20 20 20 20 20  , 1, 34},       
483eb 20 20 7b 31 31 36 32 2c 20 31 2c 20 35 34 7d 2c    {1162, 1, 54},
483ec 20 20 20 20 20 20 20 20 20 7b 31 32 31 36 2c 20           {1216, 
483ed 36 2c 20 31 7d 2c 0a 20 20 20 20 7b 31 32 31 37  6, 1},.    {1217
483ee 2c 20 31 2c 20 31 34 7d 2c 20 20 20 20 20 20 20  , 1, 14},       
483ef 20 20 7b 31 32 33 32 2c 20 31 2c 20 38 38 7d 2c    {1232, 1, 88},
483f0 20 20 20 20 20 20 20 20 20 7b 31 33 32 39 2c 20           {1329, 
483f1 32 32 2c 20 33 38 7d 2c 0a 20 20 20 20 7b 34 32  22, 38},.    {42
483f2 35 36 2c 20 36 36 2c 20 33 38 7d 2c 20 20 20 20  56, 66, 38},    
483f3 20 20 20 20 7b 34 32 39 35 2c 20 36 36 2c 20 31      {4295, 66, 1
483f4 7d 2c 20 20 20 20 20 20 20 20 20 7b 34 33 30 31  },         {4301
483f5 2c 20 36 36 2c 20 31 7d 2c 0a 20 20 20 20 7b 37  , 66, 1},.    {7
483f6 36 38 30 2c 20 31 2c 20 31 35 30 7d 2c 20 20 20  680, 1, 150},   
483f7 20 20 20 20 20 7b 37 38 33 35 2c 20 31 33 32 2c       {7835, 132,
483f8 20 31 7d 2c 20 20 20 20 20 20 20 20 7b 37 38 33   1},        {783
483f9 38 2c 20 39 36 2c 20 31 7d 2c 0a 20 20 20 20 7b  8, 96, 1},.    {
483fa 37 38 34 30 2c 20 31 2c 20 39 36 7d 2c 20 20 20  7840, 1, 96},   
483fb 20 20 20 20 20 20 7b 37 39 34 34 2c 20 31 35 30        {7944, 150
483fc 2c 20 38 7d 2c 20 20 20 20 20 20 20 20 7b 37 39  , 8},        {79
483fd 36 30 2c 20 31 35 30 2c 20 36 7d 2c 0a 20 20 20  60, 150, 6},.   
483fe 20 7b 37 39 37 36 2c 20 31 35 30 2c 20 38 7d 2c   {7976, 150, 8},
483ff 20 20 20 20 20 20 20 20 7b 37 39 39 32 2c 20 31          {7992, 1
48400 35 30 2c 20 38 7d 2c 20 20 20 20 20 20 20 20 7b  50, 8},        {
48401 38 30 30 38 2c 20 31 35 30 2c 20 36 7d 2c 0a 20  8008, 150, 6},. 
48402 20 20 20 7b 38 30 32 35 2c 20 31 35 31 2c 20 38     {8025, 151, 8
48403 7d 2c 20 20 20 20 20 20 20 20 7b 38 30 34 30 2c  },        {8040,
48404 20 31 35 30 2c 20 38 7d 2c 20 20 20 20 20 20 20   150, 8},       
48405 20 7b 38 30 37 32 2c 20 31 35 30 2c 20 38 7d 2c   {8072, 150, 8},
48406 0a 20 20 20 20 7b 38 30 38 38 2c 20 31 35 30 2c  .    {8088, 150,
48407 20 38 7d 2c 20 20 20 20 20 20 20 20 7b 38 31 30   8},        {810
48408 34 2c 20 31 35 30 2c 20 38 7d 2c 20 20 20 20 20  4, 150, 8},     
48409 20 20 20 7b 38 31 32 30 2c 20 31 35 30 2c 20 32     {8120, 150, 2
4840a 7d 2c 0a 20 20 20 20 7b 38 31 32 32 2c 20 31 32  },.    {8122, 12
4840b 36 2c 20 32 7d 2c 20 20 20 20 20 20 20 20 7b 38  6, 2},        {8
4840c 31 32 34 2c 20 31 34 38 2c 20 31 7d 2c 20 20 20  124, 148, 1},   
4840d 20 20 20 20 20 7b 38 31 32 36 2c 20 31 30 30 2c       {8126, 100,
4840e 20 31 7d 2c 0a 20 20 20 20 7b 38 31 33 36 2c 20   1},.    {8136, 
4840f 31 32 34 2c 20 34 7d 2c 20 20 20 20 20 20 20 20  124, 4},        
48410 7b 38 31 34 30 2c 20 31 34 38 2c 20 31 7d 2c 20  {8140, 148, 1}, 
48411 20 20 20 20 20 20 20 7b 38 31 35 32 2c 20 31 35         {8152, 15
48412 30 2c 20 32 7d 2c 0a 20 20 20 20 7b 38 31 35 34  0, 2},.    {8154
48413 2c 20 31 32 30 2c 20 32 7d 2c 20 20 20 20 20 20  , 120, 2},      
48414 20 20 7b 38 31 36 38 2c 20 31 35 30 2c 20 32 7d    {8168, 150, 2}
48415 2c 20 20 20 20 20 20 20 20 7b 38 31 37 30 2c 20  ,        {8170, 
48416 31 31 38 2c 20 32 7d 2c 0a 20 20 20 20 7b 38 31  118, 2},.    {81
48417 37 32 2c 20 31 35 32 2c 20 31 7d 2c 20 20 20 20  72, 152, 1},    
48418 20 20 20 20 7b 38 31 38 34 2c 20 31 31 32 2c 20      {8184, 112, 
48419 32 7d 2c 20 20 20 20 20 20 20 20 7b 38 31 38 36  2},        {8186
4841a 2c 20 31 31 34 2c 20 32 7d 2c 0a 20 20 20 20 7b  , 114, 2},.    {
4841b 38 31 38 38 2c 20 31 34 38 2c 20 31 7d 2c 20 20  8188, 148, 1},  
4841c 20 20 20 20 20 20 7b 38 34 38 36 2c 20 39 38 2c        {8486, 98,
4841d 20 31 7d 2c 20 20 20 20 20 20 20 20 20 7b 38 34   1},         {84
4841e 39 30 2c 20 39 32 2c 20 31 7d 2c 0a 20 20 20 20  90, 92, 1},.    
4841f 7b 38 34 39 31 2c 20 39 34 2c 20 31 7d 2c 20 20  {8491, 94, 1},  
48420 20 20 20 20 20 20 20 7b 38 34 39 38 2c 20 31 32         {8498, 12
48421 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20 7b 38  , 1},         {8
48422 35 34 34 2c 20 38 2c 20 31 36 7d 2c 0a 20 20 20  544, 8, 16},.   
48423 20 7b 38 35 37 39 2c 20 30 2c 20 31 7d 2c 20 20   {8579, 0, 1},  
48424 20 20 20 20 20 20 20 20 7b 39 33 39 38 2c 20 31          {9398, 1
48425 30 2c 20 32 36 7d 2c 20 20 20 20 20 20 20 20 7b  0, 26},        {
48426 31 31 32 36 34 2c 20 32 32 2c 20 34 37 7d 2c 0a  11264, 22, 47},.
48427 20 20 20 20 7b 31 31 33 36 30 2c 20 30 2c 20 31      {11360, 0, 1
48428 7d 2c 20 20 20 20 20 20 20 20 20 7b 31 31 33 36  },         {1136
48429 32 2c 20 38 38 2c 20 31 7d 2c 20 20 20 20 20 20  2, 88, 1},      
4842a 20 20 7b 31 31 33 36 33 2c 20 31 30 32 2c 20 31    {11363, 102, 1
4842b 7d 2c 0a 20 20 20 20 7b 31 31 33 36 34 2c 20 39  },.    {11364, 9
4842c 30 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 7b 31  0, 1},        {1
4842d 31 33 36 37 2c 20 31 2c 20 36 7d 2c 20 20 20 20  1367, 1, 6},    
4842e 20 20 20 20 20 7b 31 31 33 37 33 2c 20 38 34 2c       {11373, 84,
4842f 20 31 7d 2c 0a 20 20 20 20 7b 31 31 33 37 34 2c   1},.    {11374,
48430 20 38 36 2c 20 31 7d 2c 20 20 20 20 20 20 20 20   86, 1},        
48431 7b 31 31 33 37 35 2c 20 38 30 2c 20 31 7d 2c 20  {11375, 80, 1}, 
48432 20 20 20 20 20 20 20 7b 31 31 33 37 36 2c 20 38         {11376, 8
48433 32 2c 20 31 7d 2c 0a 20 20 20 20 7b 31 31 33 37  2, 1},.    {1137
48434 38 2c 20 30 2c 20 31 7d 2c 20 20 20 20 20 20 20  8, 0, 1},       
48435 20 20 7b 31 31 33 38 31 2c 20 30 2c 20 31 7d 2c    {11381, 0, 1},
48436 20 20 20 20 20 20 20 20 20 7b 31 31 33 39 30 2c           {11390,
48437 20 37 38 2c 20 32 7d 2c 0a 20 20 20 20 7b 31 31   78, 2},.    {11
48438 33 39 32 2c 20 31 2c 20 31 30 30 7d 2c 20 20 20  392, 1, 100},   
48439 20 20 20 20 7b 31 31 34 39 39 2c 20 31 2c 20 34      {11499, 1, 4
4843a 7d 2c 20 20 20 20 20 20 20 20 20 7b 31 31 35 30  },         {1150
4843b 36 2c 20 30 2c 20 31 7d 2c 0a 20 20 20 20 7b 34  6, 0, 1},.    {4
4843c 32 35 36 30 2c 20 31 2c 20 34 36 7d 2c 20 20 20  2560, 1, 46},   
4843d 20 20 20 20 20 7b 34 32 36 32 34 2c 20 31 2c 20       {42624, 1, 
4843e 32 34 7d 2c 20 20 20 20 20 20 20 20 7b 34 32 37  24},        {427
4843f 38 36 2c 20 31 2c 20 31 34 7d 2c 0a 20 20 20 20  86, 1, 14},.    
48440 7b 34 32 38 30 32 2c 20 31 2c 20 36 32 7d 2c 20  {42802, 1, 62}, 
48441 20 20 20 20 20 20 20 7b 34 32 38 37 33 2c 20 31         {42873, 1
48442 2c 20 34 7d 2c 20 20 20 20 20 20 20 20 20 7b 34  , 4},         {4
48443 32 38 37 37 2c 20 37 36 2c 20 31 7d 2c 0a 20 20  2877, 76, 1},.  
48444 20 20 7b 34 32 38 37 38 2c 20 31 2c 20 31 30 7d    {42878, 1, 10}
48445 2c 20 20 20 20 20 20 20 20 7b 34 32 38 39 31 2c  ,        {42891,
48446 20 30 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20   0, 1},         
48447 7b 34 32 38 39 33 2c 20 37 34 2c 20 31 7d 2c 0a  {42893, 74, 1},.
48448 20 20 20 20 7b 34 32 38 39 36 2c 20 31 2c 20 34      {42896, 1, 4
48449 7d 2c 20 20 20 20 20 20 20 20 20 7b 34 32 39 31  },         {4291
4844a 32 2c 20 31 2c 20 31 30 7d 2c 20 20 20 20 20 20  2, 1, 10},      
4844b 20 20 7b 34 32 39 32 32 2c 20 37 32 2c 20 31 7d    {42922, 72, 1}
4844c 2c 0a 20 20 20 20 7b 36 35 33 31 33 2c 20 31 34  ,.    {65313, 14
4844d 2c 20 32 36 7d 2c 20 20 20 20 20 20 20 0a 20 20  , 26},       .  
4844e 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  };.  static cons
4844f 74 20 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74  t unsigned short
48450 20 61 69 4f 66 66 5b 5d 20 3d 20 7b 0a 20 20 20   aiOff[] = {.   
48451 31 2c 20 20 20 20 20 32 2c 20 20 20 20 20 38 2c  1,     2,     8,
48452 20 20 20 20 20 31 35 2c 20 20 20 20 31 36 2c 20       15,    16, 
48453 20 20 20 32 36 2c 20 20 20 20 32 38 2c 20 20 20     26,    28,   
48454 20 33 32 2c 20 20 20 20 0a 20 20 20 33 37 2c 20   32,    .   37, 
48455 20 20 20 33 38 2c 20 20 20 20 34 30 2c 20 20 20     38,    40,   
48456 20 34 38 2c 20 20 20 20 36 33 2c 20 20 20 20 36   48,    63,    6
48457 34 2c 20 20 20 20 36 39 2c 20 20 20 20 37 31 2c  4,    69,    71,
48458 20 20 20 20 0a 20 20 20 37 39 2c 20 20 20 20 38      .   79,    8
48459 30 2c 20 20 20 20 31 31 36 2c 20 20 20 32 30 32  0,    116,   202
4845a 2c 20 20 20 32 30 33 2c 20 20 20 32 30 35 2c 20  ,   203,   205, 
4845b 20 20 32 30 36 2c 20 20 20 32 30 37 2c 20 20 20    206,   207,   
4845c 0a 20 20 20 32 30 39 2c 20 20 20 32 31 30 2c 20  .   209,   210, 
4845d 20 20 32 31 31 2c 20 20 20 32 31 33 2c 20 20 20    211,   213,   
4845e 32 31 34 2c 20 20 20 32 31 37 2c 20 20 20 32 31  214,   217,   21
4845f 38 2c 20 20 20 32 31 39 2c 20 20 20 0a 20 20 20  8,   219,   .   
48460 37 37 35 2c 20 20 20 37 32 36 34 2c 20 20 31 30  775,   7264,  10
48461 37 39 32 2c 20 31 30 37 39 35 2c 20 32 33 32 32  792, 10795, 2322
48462 38 2c 20 32 33 32 35 36 2c 20 33 30 32 30 34 2c  8, 23256, 30204,
48463 20 35 34 37 32 31 2c 20 0a 20 20 20 35 34 37 35   54721, .   5475
48464 33 2c 20 35 34 37 35 34 2c 20 35 34 37 35 36 2c  3, 54754, 54756,
48465 20 35 34 37 38 37 2c 20 35 34 37 39 33 2c 20 35   54787, 54793, 5
48466 34 38 30 39 2c 20 35 37 31 35 33 2c 20 35 37 32  4809, 57153, 572
48467 37 34 2c 20 0a 20 20 20 35 37 39 32 31 2c 20 35  74, .   57921, 5
48468 38 30 31 39 2c 20 35 38 33 36 33 2c 20 36 31 37  8019, 58363, 617
48469 32 32 2c 20 36 35 32 36 38 2c 20 36 35 33 34 31  22, 65268, 65341
4846a 2c 20 36 35 33 37 33 2c 20 36 35 34 30 36 2c 20  , 65373, 65406, 
4846b 0a 20 20 20 36 35 34 30 38 2c 20 36 35 34 31 30  .   65408, 65410
4846c 2c 20 36 35 34 31 35 2c 20 36 35 34 32 34 2c 20  , 65415, 65424, 
4846d 36 35 34 33 36 2c 20 36 35 34 33 39 2c 20 36 35  65436, 65439, 65
4846e 34 35 30 2c 20 36 35 34 36 32 2c 20 0a 20 20 20  450, 65462, .   
4846f 36 35 34 37 32 2c 20 36 35 34 37 36 2c 20 36 35  65472, 65476, 65
48470 34 37 38 2c 20 36 35 34 38 30 2c 20 36 35 34 38  478, 65480, 6548
48471 32 2c 20 36 35 34 38 38 2c 20 36 35 35 30 36 2c  2, 65488, 65506,
48472 20 36 35 35 31 31 2c 20 0a 20 20 20 36 35 35 31   65511, .   6551
48473 34 2c 20 36 35 35 32 31 2c 20 36 35 35 32 37 2c  4, 65521, 65527,
48474 20 36 35 35 32 38 2c 20 36 35 35 32 39 2c 20 0a   65528, 65529, .
48475 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 72 65 74 20    };..  int ret 
48476 3d 20 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = c;..  assert( 
48477 63 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  c>=0 );.  assert
48478 28 20 73 69 7a 65 6f 66 28 75 6e 73 69 67 6e 65  ( sizeof(unsigne
48479 64 20 73 68 6f 72 74 29 3d 3d 32 20 26 26 20 73  d short)==2 && s
4847a 69 7a 65 6f 66 28 75 6e 73 69 67 6e 65 64 20 63  izeof(unsigned c
4847b 68 61 72 29 3d 3d 31 20 29 3b 0a 0a 20 20 69 66  har)==1 );..  if
4847c 28 20 63 3c 31 32 38 20 29 7b 0a 20 20 20 20 69  ( c<128 ){.    i
4847d 66 28 20 63 3e 3d 27 41 27 20 26 26 20 63 3c 3d  f( c>='A' && c<=
4847e 27 5a 27 20 29 20 72 65 74 20 3d 20 63 20 2b 20  'Z' ) ret = c + 
4847f 28 27 61 27 20 2d 20 27 41 27 29 3b 0a 20 20 7d  ('a' - 'A');.  }
48480 65 6c 73 65 20 69 66 28 20 63 3c 36 35 35 33 36  else if( c<65536
48481 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 48 69 20   ){.    int iHi 
48482 3d 20 73 69 7a 65 6f 66 28 61 45 6e 74 72 79 29  = sizeof(aEntry)
48483 2f 73 69 7a 65 6f 66 28 61 45 6e 74 72 79 5b 30  /sizeof(aEntry[0
48484 5d 29 20 2d 20 31 3b 0a 20 20 20 20 69 6e 74 20  ]) - 1;.    int 
48485 69 4c 6f 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  iLo = 0;.    int
48486 20 69 52 65 73 20 3d 20 2d 31 3b 0a 0a 20 20 20   iRes = -1;..   
48487 20 77 68 69 6c 65 28 20 69 48 69 3e 3d 69 4c 6f   while( iHi>=iLo
48488 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 54   ){.      int iT
48489 65 73 74 20 3d 20 28 69 48 69 20 2b 20 69 4c 6f  est = (iHi + iLo
4848a 29 20 2f 20 32 3b 0a 20 20 20 20 20 20 69 6e 74  ) / 2;.      int
4848b 20 63 6d 70 20 3d 20 28 63 20 2d 20 61 45 6e 74   cmp = (c - aEnt
4848c 72 79 5b 69 54 65 73 74 5d 2e 69 43 6f 64 65 29  ry[iTest].iCode)
4848d 3b 0a 20 20 20 20 20 20 69 66 28 20 63 6d 70 3e  ;.      if( cmp>
4848e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 52  =0 ){.        iR
4848f 65 73 20 3d 20 69 54 65 73 74 3b 0a 20 20 20 20  es = iTest;.    
48490 20 20 20 20 69 4c 6f 20 3d 20 69 54 65 73 74 2b      iLo = iTest+
48491 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  1;.      }else{.
48492 20 20 20 20 20 20 20 20 69 48 69 20 3d 20 69 54          iHi = iT
48493 65 73 74 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20  est-1;.      }. 
48494 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
48495 20 69 52 65 73 3c 30 20 7c 7c 20 63 3e 3d 61 45   iRes<0 || c>=aE
48496 6e 74 72 79 5b 69 52 65 73 5d 2e 69 43 6f 64 65  ntry[iRes].iCode
48497 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 52 65   );..    if( iRe
48498 73 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f  s>=0 ){.      co
48499 6e 73 74 20 73 74 72 75 63 74 20 54 61 62 6c 65  nst struct Table
4849a 45 6e 74 72 79 20 2a 70 20 3d 20 26 61 45 6e 74  Entry *p = &aEnt
4849b 72 79 5b 69 52 65 73 5d 3b 0a 20 20 20 20 20 20  ry[iRes];.      
4849c 69 66 28 20 63 3c 28 70 2d 3e 69 43 6f 64 65 20  if( c<(p->iCode 
4849d 2b 20 70 2d 3e 6e 52 61 6e 67 65 29 20 26 26 20  + p->nRange) && 
4849e 30 3d 3d 28 30 78 30 31 20 26 20 70 2d 3e 66 6c  0==(0x01 & p->fl
4849f 61 67 73 20 26 20 28 70 2d 3e 69 43 6f 64 65 20  ags & (p->iCode 
484a0 5e 20 63 29 29 20 29 7b 0a 20 20 20 20 20 20 20  ^ c)) ){.       
484a1 20 72 65 74 20 3d 20 28 63 20 2b 20 28 61 69 4f   ret = (c + (aiO
484a2 66 66 5b 70 2d 3e 66 6c 61 67 73 3e 3e 31 5d 29  ff[p->flags>>1])
484a3 29 20 26 20 30 78 30 30 30 30 46 46 46 46 3b 0a  ) & 0x0000FFFF;.
484a4 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
484a5 72 65 74 3e 30 20 29 3b 0a 20 20 20 20 20 20 7d  ret>0 );.      }
484a6 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
484a7 62 52 65 6d 6f 76 65 44 69 61 63 72 69 74 69 63  bRemoveDiacritic
484a8 20 29 20 72 65 74 20 3d 20 72 65 6d 6f 76 65 5f   ) ret = remove_
484a9 64 69 61 63 72 69 74 69 63 28 72 65 74 29 3b 0a  diacritic(ret);.
484aa 20 20 7d 0a 20 20 0a 20 20 65 6c 73 65 20 69 66    }.  .  else if
484ab 28 20 63 3e 3d 36 36 35 36 30 20 26 26 20 63 3c  ( c>=66560 && c<
484ac 36 36 36 30 30 20 29 7b 0a 20 20 20 20 72 65 74  66600 ){.    ret
484ad 20 3d 20 63 20 2b 20 34 30 3b 0a 20 20 7d 0a 0a   = c + 40;.  }..
484ae 20 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a    return ret;.}.
484af 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65  #endif /* define
484b0 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
484b1 46 54 53 33 29 20 7c 7c 20 64 65 66 69 6e 65 64  FTS3) || defined
484b2 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
484b3 54 53 34 29 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  TS4) */.#endif /
484b4 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
484b5 45 5f 45 4e 41 42 4c 45 5f 46 54 53 34 5f 55 4e  E_ENABLE_FTS4_UN
484b6 49 43 4f 44 45 36 31 29 20 2a 2f 0a 0a 2f 2a 2a  ICODE61) */../**
484b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
484b8 20 6f 66 20 66 74 73 33 5f 75 6e 69 63 6f 64 65   of fts3_unicode
484b9 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  2.c ************
484ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
484bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
484bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
484bd 69 6e 20 66 69 6c 65 20 72 74 72 65 65 2e 63 20  in file rtree.c 
484be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
484bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
484c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
484c1 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
484c2 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
484c3 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
484c4 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
484c5 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
484c6 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
484c7 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
484c8 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
484c9 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
484ca 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
484cb 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
484cc 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
484cd 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
484ce 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
484cf 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
484d0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
484d1 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
484d2 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
484d3 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
484d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
484d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
484d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
484d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
484d8 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
484d9 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66   contains code f
484da 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  or implementatio
484db 6e 73 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65  ns of the r-tree
484dc 20 61 6e 64 20 72 2a 2d 74 72 65 65 0a 2a 2a 20   and r*-tree.** 
484dd 61 6c 67 6f 72 69 74 68 6d 73 20 70 61 63 6b 61  algorithms packa
484de 67 65 64 20 61 73 20 61 6e 20 53 51 4c 69 74 65  ged as an SQLite
484df 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
484e0 6f 64 75 6c 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  odule..*/../*.**
484e1 20 44 61 74 61 62 61 73 65 20 46 6f 72 6d 61 74   Database Format
484e2 20 6f 66 20 52 2d 54 72 65 65 20 54 61 62 6c 65   of R-Tree Table
484e3 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  s.** -----------
484e4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
484e5 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20  -----.**.** The 
484e6 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 66  data structure f
484e7 6f 72 20 61 20 73 69 6e 67 6c 65 20 76 69 72 74  or a single virt
484e8 75 61 6c 20 72 2d 74 72 65 65 20 74 61 62 6c 65  ual r-tree table
484e9 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68   is stored in th
484ea 72 65 65 20 0a 2a 2a 20 6e 61 74 69 76 65 20 53  ree .** native S
484eb 51 4c 69 74 65 20 74 61 62 6c 65 73 20 64 65 63  QLite tables dec
484ec 6c 61 72 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73  lared as follows
484ed 2e 20 49 6e 20 65 61 63 68 20 63 61 73 65 2c 20  . In each case, 
484ee 74 68 65 20 27 25 27 20 63 68 61 72 61 63 74 65  the '%' characte
484ef 72 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62 6c  r.** in the tabl
484f0 65 20 6e 61 6d 65 20 69 73 20 72 65 70 6c 61 63  e name is replac
484f1 65 64 20 77 69 74 68 20 74 68 65 20 75 73 65 72  ed with the user
484f2 2d 73 75 70 70 6c 69 65 64 20 6e 61 6d 65 20 6f  -supplied name o
484f3 66 20 74 68 65 20 72 2d 74 72 65 65 0a 2a 2a 20  f the r-tree.** 
484f4 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 43  table..**.**   C
484f5 52 45 41 54 45 20 54 41 42 4c 45 20 25 5f 6e 6f  REATE TABLE %_no
484f6 64 65 28 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45  de(nodeno INTEGE
484f7 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 64  R PRIMARY KEY, d
484f8 61 74 61 20 42 4c 4f 42 29 0a 2a 2a 20 20 20 43  ata BLOB).**   C
484f9 52 45 41 54 45 20 54 41 42 4c 45 20 25 5f 70 61  REATE TABLE %_pa
484fa 72 65 6e 74 28 6e 6f 64 65 6e 6f 20 49 4e 54 45  rent(nodeno INTE
484fb 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  GER PRIMARY KEY,
484fc 20 70 61 72 65 6e 74 6e 6f 64 65 20 49 4e 54 45   parentnode INTE
484fd 47 45 52 29 0a 2a 2a 20 20 20 43 52 45 41 54 45  GER).**   CREATE
484fe 20 54 41 42 4c 45 20 25 5f 72 6f 77 69 64 28 72   TABLE %_rowid(r
484ff 6f 77 69 64 20 49 4e 54 45 47 45 52 20 50 52 49  owid INTEGER PRI
48500 4d 41 52 59 20 4b 45 59 2c 20 6e 6f 64 65 6e 6f  MARY KEY, nodeno
48501 20 49 4e 54 45 47 45 52 29 0a 2a 2a 0a 2a 2a 20   INTEGER).**.** 
48502 54 68 65 20 64 61 74 61 20 66 6f 72 20 65 61 63  The data for eac
48503 68 20 6e 6f 64 65 20 6f 66 20 74 68 65 20 72 2d  h node of the r-
48504 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 69  tree structure i
48505 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
48506 25 5f 6e 6f 64 65 0a 2a 2a 20 74 61 62 6c 65 2e  %_node.** table.
48507 20 46 6f 72 20 65 61 63 68 20 6e 6f 64 65 20 74   For each node t
48508 68 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 72  hat is not the r
48509 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 74 68 65 20  oot node of the 
4850a 72 2d 74 72 65 65 2c 20 74 68 65 72 65 20 69 73  r-tree, there is
4850b 0a 2a 2a 20 61 6e 20 65 6e 74 72 79 20 69 6e 20  .** an entry in 
4850c 74 68 65 20 25 5f 70 61 72 65 6e 74 20 74 61 62  the %_parent tab
4850d 6c 65 20 61 73 73 6f 63 69 61 74 69 6e 67 20 74  le associating t
4850e 68 65 20 6e 6f 64 65 20 77 69 74 68 20 69 74 73  he node with its
4850f 20 70 61 72 65 6e 74 2e 0a 2a 2a 20 41 6e 64 20   parent..** And 
48510 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66 20  for each row of 
48511 64 61 74 61 20 69 6e 20 74 68 65 20 74 61 62 6c  data in the tabl
48512 65 2c 20 74 68 65 72 65 20 69 73 20 61 6e 20 65  e, there is an e
48513 6e 74 72 79 20 69 6e 20 74 68 65 20 25 5f 72 6f  ntry in the %_ro
48514 77 69 64 0a 2a 2a 20 74 61 62 6c 65 20 74 68 61  wid.** table tha
48515 74 20 6d 61 70 73 20 66 72 6f 6d 20 74 68 65 20  t maps from the 
48516 65 6e 74 72 69 65 73 20 72 6f 77 69 64 20 74 6f  entries rowid to
48517 20 74 68 65 20 69 64 20 6f 66 20 74 68 65 20 6e   the id of the n
48518 6f 64 65 20 74 68 61 74 20 69 74 0a 2a 2a 20 69  ode that it.** i
48519 73 20 73 74 6f 72 65 64 20 6f 6e 2e 0a 2a 2a 0a  s stored on..**.
4851a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 6e 6f 64 65  ** The root node
4851b 20 6f 66 20 61 6e 20 72 2d 74 72 65 65 20 61 6c   of an r-tree al
4851c 77 61 79 73 20 65 78 69 73 74 73 2c 20 65 76 65  ways exists, eve
4851d 6e 20 69 66 20 74 68 65 20 72 2d 74 72 65 65 20  n if the r-tree 
4851e 74 61 62 6c 65 20 69 73 0a 2a 2a 20 65 6d 70 74  table is.** empt
4851f 79 2e 20 54 68 65 20 6e 6f 64 65 6e 6f 20 6f 66  y. The nodeno of
48520 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 69   the root node i
48521 73 20 61 6c 77 61 79 73 20 31 2e 20 41 6c 6c 20  s always 1. All 
48522 6f 74 68 65 72 20 6e 6f 64 65 73 20 69 6e 20 74  other nodes in t
48523 68 65 0a 2a 2a 20 74 61 62 6c 65 20 6d 75 73 74  he.** table must
48524 20 62 65 20 74 68 65 20 73 61 6d 65 20 73 69 7a   be the same siz
48525 65 20 61 73 20 74 68 65 20 72 6f 6f 74 20 6e 6f  e as the root no
48526 64 65 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 20  de. The content 
48527 6f 66 20 65 61 63 68 20 6e 6f 64 65 0a 2a 2a 20  of each node.** 
48528 69 73 20 66 6f 72 6d 61 74 74 65 64 20 61 73 20  is formatted as 
48529 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
4852a 20 31 2e 20 49 66 20 74 68 65 20 6e 6f 64 65 20   1. If the node 
4852b 69 73 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65  is the root node
4852c 20 28 6e 6f 64 65 20 31 29 2c 20 74 68 65 6e 20   (node 1), then 
4852d 74 68 65 20 66 69 72 73 74 20 32 20 62 79 74 65  the first 2 byte
4852e 73 0a 2a 2a 20 20 20 20 20 20 6f 66 20 74 68 65  s.**      of the
4852f 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 20 74 68   node contain th
48530 65 20 74 72 65 65 20 64 65 70 74 68 20 61 73 20  e tree depth as 
48531 61 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74  a big-endian int
48532 65 67 65 72 2e 0a 2a 2a 20 20 20 20 20 20 46 6f  eger..**      Fo
48533 72 20 6e 6f 6e 2d 72 6f 6f 74 20 6e 6f 64 65 73  r non-root nodes
48534 2c 20 74 68 65 20 66 69 72 73 74 20 32 20 62 79  , the first 2 by
48535 74 65 73 20 61 72 65 20 6c 65 66 74 20 75 6e 75  tes are left unu
48536 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20  sed..**.**   2. 
48537 54 68 65 20 6e 65 78 74 20 32 20 62 79 74 65 73  The next 2 bytes
48538 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d   contain the num
48539 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 63  ber of entries c
4853a 75 72 72 65 6e 74 6c 79 20 0a 2a 2a 20 20 20 20  urrently .**    
4853b 20 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20    stored in the 
4853c 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e  node..**.**   3.
4853d 20 54 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f   The remainder o
4853e 66 20 74 68 65 20 6e 6f 64 65 20 63 6f 6e 74 61  f the node conta
4853f 69 6e 73 20 74 68 65 20 6e 6f 64 65 20 65 6e 74  ins the node ent
48540 72 69 65 73 2e 20 45 61 63 68 20 65 6e 74 72 79  ries. Each entry
48541 0a 2a 2a 20 20 20 20 20 20 63 6f 6e 73 69 73 74  .**      consist
48542 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 38 2d  s of a single 8-
48543 62 79 74 65 20 69 6e 74 65 67 65 72 20 66 6f 6c  byte integer fol
48544 6c 6f 77 65 64 20 62 79 20 61 6e 20 65 76 65 6e  lowed by an even
48545 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20   number.**      
48546 6f 66 20 34 2d 62 79 74 65 20 63 6f 6f 72 64 69  of 4-byte coordi
48547 6e 61 74 65 73 2e 20 46 6f 72 20 6c 65 61 66 20  nates. For leaf 
48548 6e 6f 64 65 73 20 74 68 65 20 69 6e 74 65 67 65  nodes the intege
48549 72 20 69 73 20 74 68 65 20 72 6f 77 69 64 0a 2a  r is the rowid.*
4854a 2a 20 20 20 20 20 20 6f 66 20 61 20 72 65 63 6f  *      of a reco
4854b 72 64 2e 20 46 6f 72 20 69 6e 74 65 72 6e 61 6c  rd. For internal
4854c 20 6e 6f 64 65 73 20 69 74 20 69 73 20 74 68 65   nodes it is the
4854d 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20 6f 66 20   node number of 
4854e 61 0a 2a 2a 20 20 20 20 20 20 63 68 69 6c 64 20  a.**      child 
4854f 70 61 67 65 2e 0a 2a 2f 0a 0a 23 69 66 20 21 64  page..*/..#if !d
48550 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
48551 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
48552 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52  QLITE_ENABLE_RTR
48553 45 45 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  EE)../*.** This 
48554 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e  file contains an
48555 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
48556 6f 66 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64  of a couple of d
48557 69 66 66 65 72 65 6e 74 20 76 61 72 69 61 6e 74  ifferent variant
48558 73 0a 2a 2a 20 6f 66 20 74 68 65 20 72 2d 74 72  s.** of the r-tr
48559 65 65 20 61 6c 67 6f 72 69 74 68 6d 2e 20 53 65  ee algorithm. Se
4855a 65 20 74 68 65 20 52 45 41 44 4d 45 20 66 69 6c  e the README fil
4855b 65 20 66 6f 72 20 66 75 72 74 68 65 72 20 64 65  e for further de
4855c 74 61 69 6c 73 2e 20 54 68 65 20 0a 2a 2a 20 73  tails. The .** s
4855d 61 6d 65 20 64 61 74 61 2d 73 74 72 75 63 74 75  ame data-structu
4855e 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 61  re is used for a
4855f 6c 6c 2c 20 62 75 74 20 74 68 65 20 61 6c 67 6f  ll, but the algo
48560 72 69 74 68 6d 73 20 66 6f 72 20 69 6e 73 65 72  rithms for inser
48561 74 20 61 6e 64 0a 2a 2a 20 64 65 6c 65 74 65 20  t and.** delete 
48562 6f 70 65 72 61 74 69 6f 6e 73 20 76 61 72 79 2e  operations vary.
48563 20 54 68 65 20 76 61 72 69 61 6e 74 73 20 75 73   The variants us
48564 65 64 20 61 72 65 20 73 65 6c 65 63 74 65 64 20  ed are selected 
48565 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20  at compile time 
48566 0a 2a 2a 20 62 79 20 64 65 66 69 6e 69 6e 67 20  .** by defining 
48567 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79  the following sy
48568 6d 62 6f 6c 73 3a 0a 2a 2f 0a 0a 2f 2a 20 45 69  mbols:.*/../* Ei
48569 74 68 65 72 2c 20 62 6f 74 68 20 6f 72 20 6e 6f  ther, both or no
4856a 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
4856b 69 6e 67 20 6d 61 79 20 62 65 20 73 65 74 20 74  ing may be set t
4856c 6f 20 61 63 74 69 76 61 74 65 20 0a 2a 2a 20 72  o activate .** r
4856d 2a 74 72 65 65 20 76 61 72 69 61 6e 74 20 61 6c  *tree variant al
4856e 67 6f 72 69 74 68 6d 73 2e 0a 2a 2f 0a 23 64 65  gorithms..*/.#de
4856f 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52 53 54  fine VARIANT_RST
48570 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42  ARTREE_CHOOSESUB
48571 54 52 45 45 20 30 0a 23 64 65 66 69 6e 65 20 56  TREE 0.#define V
48572 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45  ARIANT_RSTARTREE
48573 5f 52 45 49 4e 53 45 52 54 20 20 20 20 20 20 31  _REINSERT      1
48574 0a 0a 2f 2a 20 0a 2a 2a 20 45 78 61 63 74 6c 79  ../* .** Exactly
48575 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
48576 6f 77 69 6e 67 20 6d 75 73 74 20 62 65 20 73 65  owing must be se
48577 74 20 74 6f 20 31 2e 0a 2a 2f 0a 23 64 65 66 69  t to 1..*/.#defi
48578 6e 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d  ne VARIANT_GUTTM
48579 41 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c  AN_QUADRATIC_SPL
4857a 49 54 20 30 0a 23 64 65 66 69 6e 65 20 56 41 52  IT 0.#define VAR
4857b 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e  IANT_GUTTMAN_LIN
4857c 45 41 52 5f 53 50 4c 49 54 20 20 20 20 30 0a 23  EAR_SPLIT    0.#
4857d 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52  define VARIANT_R
4857e 53 54 41 52 54 52 45 45 5f 53 50 4c 49 54 20 20  STARTREE_SPLIT  
4857f 20 20 20 20 20 20 20 31 0a 0a 23 64 65 66 69 6e         1..#defin
48580 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  e VARIANT_GUTTMA
48581 4e 5f 53 50 4c 49 54 20 5c 0a 20 20 20 20 20 20  N_SPLIT \.      
48582 20 20 28 56 41 52 49 41 4e 54 5f 47 55 54 54 4d    (VARIANT_GUTTM
48583 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 7c  AN_LINEAR_SPLIT|
48584 7c 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e  |VARIANT_GUTTMAN
48585 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49 54  _QUADRATIC_SPLIT
48586 29 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  )..#if VARIANT_G
48587 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
48588 5f 53 50 4c 49 54 0a 20 20 23 64 65 66 69 6e 65  _SPLIT.  #define
48589 20 50 69 63 6b 4e 65 78 74 20 51 75 61 64 72 61   PickNext Quadra
4858a 74 69 63 50 69 63 6b 4e 65 78 74 0a 20 20 23 64  ticPickNext.  #d
4858b 65 66 69 6e 65 20 50 69 63 6b 53 65 65 64 73 20  efine PickSeeds 
4858c 51 75 61 64 72 61 74 69 63 50 69 63 6b 53 65 65  QuadraticPickSee
4858d 64 73 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73  ds.  #define Ass
4858e 69 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f  ignCells splitNo
4858f 64 65 47 75 74 74 6d 61 6e 0a 23 65 6e 64 69 66  deGuttman.#endif
48590 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55 54  .#if VARIANT_GUT
48591 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49  TMAN_LINEAR_SPLI
48592 54 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b  T.  #define Pick
48593 4e 65 78 74 20 4c 69 6e 65 61 72 50 69 63 6b 4e  Next LinearPickN
48594 65 78 74 0a 20 20 23 64 65 66 69 6e 65 20 50 69  ext.  #define Pi
48595 63 6b 53 65 65 64 73 20 4c 69 6e 65 61 72 50 69  ckSeeds LinearPi
48596 63 6b 53 65 65 64 73 0a 20 20 23 64 65 66 69 6e  ckSeeds.  #defin
48597 65 20 41 73 73 69 67 6e 43 65 6c 6c 73 20 73 70  e AssignCells sp
48598 6c 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e 0a 23  litNodeGuttman.#
48599 65 6e 64 69 66 0a 23 69 66 20 56 41 52 49 41 4e  endif.#if VARIAN
4859a 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c 49  T_RSTARTREE_SPLI
4859b 54 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73 69  T.  #define Assi
4859c 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f 64  gnCells splitNod
4859d 65 53 74 61 72 74 72 65 65 0a 23 65 6e 64 69 66  eStartree.#endif
4859e 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e  ..#if !defined(N
4859f 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e  DEBUG) && !defin
485a0 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
485a1 20 0a 23 20 64 65 66 69 6e 65 20 4e 44 45 42 55   .# define NDEBU
485a2 47 20 31 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  G 1.#endif..#ifn
485a3 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a  def SQLITE_CORE.
485a4 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49    SQLITE_EXTENSI
485a5 4f 4e 5f 49 4e 49 54 31 0a 23 65 6c 73 65 0a 23  ON_INIT1.#else.#
485a6 65 6e 64 69 66 0a 0a 2f 2a 20 23 69 6e 63 6c 75  endif../* #inclu
485a7 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 20 2a 2f  de <string.h> */
485a8 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 61 73  ./* #include <as
485a9 73 65 72 74 2e 68 3e 20 2a 2f 0a 0a 23 69 66 6e  sert.h> */..#ifn
485aa 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47  def SQLITE_AMALG
485ab 41 4d 41 54 49 4f 4e 0a 23 69 6e 63 6c 75 64 65  AMATION.#include
485ac 20 22 73 71 6c 69 74 65 33 72 74 72 65 65 2e 68   "sqlite3rtree.h
485ad 22 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65  ".typedef sqlite
485ae 33 5f 69 6e 74 36 34 20 69 36 34 3b 0a 74 79 70  3_int64 i64;.typ
485af 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68  edef unsigned ch
485b0 61 72 20 75 38 3b 0a 74 79 70 65 64 65 66 20 75  ar u8;.typedef u
485b1 6e 73 69 67 6e 65 64 20 69 6e 74 20 75 33 32 3b  nsigned int u32;
485b2 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 20 54 68 65  .#endif../*  The
485b3 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
485b4 20 69 73 20 75 73 65 64 20 74 6f 20 73 75 70 70   is used to supp
485b5 72 65 73 73 20 63 6f 6d 70 69 6c 65 72 20 77 61  ress compiler wa
485b6 72 6e 69 6e 67 73 2e 0a 2a 2f 0a 23 69 66 6e 64  rnings..*/.#ifnd
485b7 65 66 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  ef UNUSED_PARAME
485b8 54 45 52 0a 23 20 64 65 66 69 6e 65 20 55 4e 55  TER.# define UNU
485b9 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78 29  SED_PARAMETER(x)
485ba 20 28 76 6f 69 64 29 28 78 29 0a 23 65 6e 64 69   (void)(x).#endi
485bb 66 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  f..typedef struc
485bc 74 20 52 74 72 65 65 20 52 74 72 65 65 3b 0a 74  t Rtree Rtree;.t
485bd 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52 74  ypedef struct Rt
485be 72 65 65 43 75 72 73 6f 72 20 52 74 72 65 65 43  reeCursor RtreeC
485bf 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73  ursor;.typedef s
485c0 74 72 75 63 74 20 52 74 72 65 65 4e 6f 64 65 20  truct RtreeNode 
485c1 52 74 72 65 65 4e 6f 64 65 3b 0a 74 79 70 65 64  RtreeNode;.typed
485c2 65 66 20 73 74 72 75 63 74 20 52 74 72 65 65 43  ef struct RtreeC
485c3 65 6c 6c 20 52 74 72 65 65 43 65 6c 6c 3b 0a 74  ell RtreeCell;.t
485c4 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52 74  ypedef struct Rt
485c5 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 52 74  reeConstraint Rt
485c6 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 74  reeConstraint;.t
485c7 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52 74  ypedef struct Rt
485c8 72 65 65 4d 61 74 63 68 41 72 67 20 52 74 72 65  reeMatchArg Rtre
485c9 65 4d 61 74 63 68 41 72 67 3b 0a 74 79 70 65 64  eMatchArg;.typed
485ca 65 66 20 73 74 72 75 63 74 20 52 74 72 65 65 47  ef struct RtreeG
485cb 65 6f 6d 43 61 6c 6c 62 61 63 6b 20 52 74 72 65  eomCallback Rtre
485cc 65 47 65 6f 6d 43 61 6c 6c 62 61 63 6b 3b 0a 74  eGeomCallback;.t
485cd 79 70 65 64 65 66 20 75 6e 69 6f 6e 20 52 74 72  ypedef union Rtr
485ce 65 65 43 6f 6f 72 64 20 52 74 72 65 65 43 6f 6f  eeCoord RtreeCoo
485cf 72 64 3b 0a 0a 2f 2a 20 54 68 65 20 72 74 72 65  rd;../* The rtre
485d0 65 20 6d 61 79 20 68 61 76 65 20 62 65 74 77 65  e may have betwe
485d1 65 6e 20 31 20 61 6e 64 20 52 54 52 45 45 5f 4d  en 1 and RTREE_M
485d2 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 20 64 69  AX_DIMENSIONS di
485d3 6d 65 6e 73 69 6f 6e 73 2e 20 2a 2f 0a 23 64 65  mensions. */.#de
485d4 66 69 6e 65 20 52 54 52 45 45 5f 4d 41 58 5f 44  fine RTREE_MAX_D
485d5 49 4d 45 4e 53 49 4f 4e 53 20 35 0a 0a 2f 2a 20  IMENSIONS 5../* 
485d6 53 69 7a 65 20 6f 66 20 68 61 73 68 20 74 61 62  Size of hash tab
485d7 6c 65 20 52 74 72 65 65 2e 61 48 61 73 68 2e 20  le Rtree.aHash. 
485d8 54 68 69 73 20 68 61 73 68 20 74 61 62 6c 65 20  This hash table 
485d9 69 73 20 6e 6f 74 20 65 78 70 65 63 74 65 64 20  is not expected 
485da 74 6f 0a 2a 2a 20 65 76 65 72 20 63 6f 6e 74 61  to.** ever conta
485db 69 6e 20 76 65 72 79 20 6d 61 6e 79 20 65 6e 74  in very many ent
485dc 72 69 65 73 2c 20 73 6f 20 61 20 66 69 78 65 64  ries, so a fixed
485dd 20 6e 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65   number of bucke
485de 74 73 20 69 73 20 0a 2a 2a 20 75 73 65 64 2e 0a  ts is .** used..
485df 2a 2f 0a 23 64 65 66 69 6e 65 20 48 41 53 48 53  */.#define HASHS
485e0 49 5a 45 20 31 32 38 0a 0a 2f 2a 20 0a 2a 2a 20  IZE 128../* .** 
485e1 41 6e 20 72 74 72 65 65 20 76 69 72 74 75 61 6c  An rtree virtual
485e2 2d 74 61 62 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a  -table object..*
485e3 2f 0a 73 74 72 75 63 74 20 52 74 72 65 65 20 7b  /.struct Rtree {
485e4 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
485e5 62 61 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 20  base;.  sqlite3 
485e6 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  *db;            
485e7 20 20 20 20 2f 2a 20 48 6f 73 74 20 64 61 74 61      /* Host data
485e8 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
485e9 2a 2f 0a 20 20 69 6e 74 20 69 4e 6f 64 65 53 69  */.  int iNodeSi
485ea 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
485eb 20 2f 2a 20 53 69 7a 65 20 69 6e 20 62 79 74 65   /* Size in byte
485ec 73 20 6f 66 20 65 61 63 68 20 6e 6f 64 65 20 69  s of each node i
485ed 6e 20 74 68 65 20 6e 6f 64 65 20 74 61 62 6c 65  n the node table
485ee 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 69 6d 3b 20   */.  int nDim; 
485ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485f0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64    /* Number of d
485f1 69 6d 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 20 69  imensions */.  i
485f2 6e 74 20 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  nt nBytesPerCell
485f3 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79  ;          /* By
485f4 74 65 73 20 63 6f 6e 73 75 6d 65 64 20 70 65 72  tes consumed per
485f5 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69   cell */.  int i
485f6 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20  Depth;          
485f7 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
485f8 74 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 72  t depth of the r
485f9 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20  -tree structure 
485fa 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62 3b 20  */.  char *zDb; 
485fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485fc 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61   /* Name of data
485fd 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
485fe 72 2d 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a  r-tree table */.
485ff 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
48600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
48601 20 4e 61 6d 65 20 6f 66 20 72 2d 74 72 65 65 20   Name of r-tree 
48602 74 61 62 6c 65 20 2a 2f 20 0a 20 20 52 74 72 65  table */ .  Rtre
48603 65 4e 6f 64 65 20 2a 61 48 61 73 68 5b 48 41 53  eNode *aHash[HAS
48604 48 53 49 5a 45 5d 3b 20 2f 2a 20 48 61 73 68 20  HSIZE]; /* Hash 
48605 74 61 62 6c 65 20 6f 66 20 69 6e 2d 6d 65 6d 6f  table of in-memo
48606 72 79 20 6e 6f 64 65 73 2e 20 2a 2f 20 0a 20 20  ry nodes. */ .  
48607 69 6e 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20  int nBusy;      
48608 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
48609 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66  urrent number of
4860a 20 75 73 65 72 73 20 6f 66 20 74 68 69 73 20 73   users of this s
4860b 74 72 75 63 74 75 72 65 20 2a 2f 0a 0a 20 20 2f  tructure */..  /
4860c 2a 20 4c 69 73 74 20 6f 66 20 6e 6f 64 65 73 20  * List of nodes 
4860d 72 65 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61  removed during a
4860e 20 43 6f 6e 64 65 6e 73 65 54 72 65 65 20 6f 70   CondenseTree op
4860f 65 72 61 74 69 6f 6e 2e 20 4c 69 73 74 20 69 73  eration. List is
48610 0a 20 20 2a 2a 20 6c 69 6e 6b 65 64 20 74 6f 67  .  ** linked tog
48611 65 74 68 65 72 20 76 69 61 20 74 68 65 20 70 6f  ether via the po
48612 69 6e 74 65 72 20 6e 6f 72 6d 61 6c 6c 79 20 75  inter normally u
48613 73 65 64 20 66 6f 72 20 68 61 73 68 20 63 68 61  sed for hash cha
48614 69 6e 73 20 2d 0a 20 20 2a 2a 20 52 74 72 65 65  ins -.  ** Rtree
48615 4e 6f 64 65 2e 70 4e 65 78 74 2e 20 52 74 72 65  Node.pNext. Rtre
48616 65 4e 6f 64 65 2e 69 4e 6f 64 65 20 73 74 6f 72  eNode.iNode stor
48617 65 73 20 74 68 65 20 64 65 70 74 68 20 6f 66 20  es the depth of 
48618 74 68 65 20 73 75 62 2d 74 72 65 65 20 0a 20 20  the sub-tree .  
48619 2a 2a 20 68 65 61 64 65 64 20 62 79 20 74 68 65  ** headed by the
4861a 20 6e 6f 64 65 20 28 6c 65 61 66 20 6e 6f 64 65   node (leaf node
4861b 73 20 68 61 76 65 20 52 74 72 65 65 4e 6f 64 65  s have RtreeNode
4861c 2e 69 4e 6f 64 65 3d 3d 30 29 2e 0a 20 20 2a 2f  .iNode==0)..  */
4861d 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 44  .  RtreeNode *pD
4861e 65 6c 65 74 65 64 3b 0a 20 20 69 6e 74 20 69 52  eleted;.  int iR
4861f 65 69 6e 73 65 72 74 48 65 69 67 68 74 3b 20 20  einsertHeight;  
48620 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20        /* Height 
48621 6f 66 20 73 75 62 2d 74 72 65 65 73 20 52 65 69  of sub-trees Rei
48622 6e 73 65 72 74 28 29 20 68 61 73 20 72 75 6e 20  nsert() has run 
48623 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 53 74 61 74  on */..  /* Stat
48624 65 6d 65 6e 74 73 20 74 6f 20 72 65 61 64 2f 77  ements to read/w
48625 72 69 74 65 2f 64 65 6c 65 74 65 20 61 20 72 65  rite/delete a re
48626 63 6f 72 64 20 66 72 6f 6d 20 78 78 78 5f 6e 6f  cord from xxx_no
48627 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  de */.  sqlite3_
48628 73 74 6d 74 20 2a 70 52 65 61 64 4e 6f 64 65 3b  stmt *pReadNode;
48629 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
4862a 2a 70 57 72 69 74 65 4e 6f 64 65 3b 0a 20 20 73  *pWriteNode;.  s
4862b 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44 65  qlite3_stmt *pDe
4862c 6c 65 74 65 4e 6f 64 65 3b 0a 0a 20 20 2f 2a 20  leteNode;..  /* 
4862d 53 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 72 65  Statements to re
4862e 61 64 2f 77 72 69 74 65 2f 64 65 6c 65 74 65 20  ad/write/delete 
4862f 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 78 78  a record from xx
48630 78 5f 72 6f 77 69 64 20 2a 2f 0a 20 20 73 71 6c  x_rowid */.  sql
48631 69 74 65 33 5f 73 74 6d 74 20 2a 70 52 65 61 64  ite3_stmt *pRead
48632 52 6f 77 69 64 3b 0a 20 20 73 71 6c 69 74 65 33  Rowid;.  sqlite3
48633 5f 73 74 6d 74 20 2a 70 57 72 69 74 65 52 6f 77  _stmt *pWriteRow
48634 69 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  id;.  sqlite3_st
48635 6d 74 20 2a 70 44 65 6c 65 74 65 52 6f 77 69 64  mt *pDeleteRowid
48636 3b 0a 0a 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e  ;..  /* Statemen
48637 74 73 20 74 6f 20 72 65 61 64 2f 77 72 69 74 65  ts to read/write
48638 2f 64 65 6c 65 74 65 20 61 20 72 65 63 6f 72 64  /delete a record
48639 20 66 72 6f 6d 20 78 78 78 5f 70 61 72 65 6e 74   from xxx_parent
4863a 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
4863b 6d 74 20 2a 70 52 65 61 64 50 61 72 65 6e 74 3b  mt *pReadParent;
4863c 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
4863d 2a 70 57 72 69 74 65 50 61 72 65 6e 74 3b 0a 20  *pWriteParent;. 
4863e 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
4863f 44 65 6c 65 74 65 50 61 72 65 6e 74 3b 0a 0a 20  DeleteParent;.. 
48640 20 69 6e 74 20 65 43 6f 6f 72 64 54 79 70 65 3b   int eCoordType;
48641 0a 7d 3b 0a 0a 2f 2a 20 50 6f 73 73 69 62 6c 65  .};../* Possible
48642 20 76 61 6c 75 65 73 20 66 6f 72 20 65 43 6f 6f   values for eCoo
48643 72 64 54 79 70 65 3a 20 2a 2f 0a 23 64 65 66 69  rdType: */.#defi
48644 6e 65 20 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52  ne RTREE_COORD_R
48645 45 41 4c 33 32 20 30 0a 23 64 65 66 69 6e 65 20  EAL32 0.#define 
48646 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33  RTREE_COORD_INT3
48647 32 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53  2  1../*.** If S
48648 51 4c 49 54 45 5f 52 54 52 45 45 5f 49 4e 54 5f  QLITE_RTREE_INT_
48649 4f 4e 4c 59 20 69 73 20 64 65 66 69 6e 65 64 2c  ONLY is defined,
4864a 20 74 68 65 6e 20 74 68 69 73 20 76 69 72 74 75   then this virtu
4864b 61 6c 20 74 61 62 6c 65 20 77 69 6c 6c 0a 2a 2a  al table will.**
4864c 20 6f 6e 6c 79 20 64 65 61 6c 20 77 69 74 68 20   only deal with 
4864d 69 6e 74 65 67 65 72 20 63 6f 6f 72 64 69 6e 61  integer coordina
4864e 74 65 73 2e 20 20 4e 6f 20 66 6c 6f 61 74 69 6e  tes.  No floatin
4864f 67 20 70 6f 69 6e 74 20 6f 70 65 72 61 74 69 6f  g point operatio
48650 6e 73 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 64 6f  ns.** will be do
48651 6e 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ne..*/.#ifdef SQ
48652 4c 49 54 45 5f 52 54 52 45 45 5f 49 4e 54 5f 4f  LITE_RTREE_INT_O
48653 4e 4c 59 0a 20 20 74 79 70 65 64 65 66 20 73 71  NLY.  typedef sq
48654 6c 69 74 65 33 5f 69 6e 74 36 34 20 52 74 72 65  lite3_int64 Rtre
48655 65 44 56 61 6c 75 65 3b 20 20 20 20 20 20 20 2f  eDValue;       /
48656 2a 20 48 69 67 68 20 61 63 63 75 72 61 63 79 20  * High accuracy 
48657 63 6f 6f 72 64 69 6e 61 74 65 20 2a 2f 0a 20 20  coordinate */.  
48658 74 79 70 65 64 65 66 20 69 6e 74 20 52 74 72 65  typedef int Rtre
48659 65 56 61 6c 75 65 3b 20 20 20 20 20 20 20 20 20  eValue;         
4865a 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 20           /* Low 
4865b 61 63 63 75 72 61 63 79 20 63 6f 6f 72 64 69 6e  accuracy coordin
4865c 61 74 65 20 2a 2f 0a 23 65 6c 73 65 0a 20 20 74  ate */.#else.  t
4865d 79 70 65 64 65 66 20 64 6f 75 62 6c 65 20 52 74  ypedef double Rt
4865e 72 65 65 44 56 61 6c 75 65 3b 20 20 20 20 20 20  reeDValue;      
4865f 20 20 20 20 20 20 20 20 2f 2a 20 48 69 67 68 20          /* High 
48660 61 63 63 75 72 61 63 79 20 63 6f 6f 72 64 69 6e  accuracy coordin
48661 61 74 65 20 2a 2f 0a 20 20 74 79 70 65 64 65 66  ate */.  typedef
48662 20 66 6c 6f 61 74 20 52 74 72 65 65 56 61 6c 75   float RtreeValu
48663 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
48664 20 20 2f 2a 20 4c 6f 77 20 61 63 63 75 72 61 63    /* Low accurac
48665 79 20 63 6f 6f 72 64 69 6e 61 74 65 20 2a 2f 0a  y coordinate */.
48666 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
48667 65 20 6d 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72  e minimum number
48668 20 6f 66 20 63 65 6c 6c 73 20 61 6c 6c 6f 77 65   of cells allowe
48669 64 20 66 6f 72 20 61 20 6e 6f 64 65 20 69 73 20  d for a node is 
4866a 61 20 74 68 69 72 64 20 6f 66 20 74 68 65 20 0a  a third of the .
4866b 2a 2a 20 6d 61 78 69 6d 75 6d 2e 20 49 6e 20 47  ** maximum. In G
4866c 75 74 6d 61 6e 27 73 20 6e 6f 74 61 74 69 6f 6e  utman's notation
4866d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 6d 20 3d 20  :.**.**     m = 
4866e 4d 2f 33 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  M/3.**.** If an 
4866f 52 2a 2d 74 72 65 65 20 22 52 65 69 6e 73 65 72  R*-tree "Reinser
48670 74 22 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20  t" operation is 
48671 72 65 71 75 69 72 65 64 2c 20 74 68 65 20 73 61  required, the sa
48672 6d 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  me number of.** 
48673 63 65 6c 6c 73 20 61 72 65 20 72 65 6d 6f 76 65  cells are remove
48674 64 20 66 72 6f 6d 20 74 68 65 20 6f 76 65 72 66  d from the overf
48675 75 6c 6c 20 6e 6f 64 65 20 61 6e 64 20 72 65 69  ull node and rei
48676 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65  nserted into the
48677 20 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e   tree..*/.#defin
48678 65 20 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53  e RTREE_MINCELLS
48679 28 70 29 20 28 28 28 28 70 29 2d 3e 69 4e 6f 64  (p) ((((p)->iNod
4867a 65 53 69 7a 65 2d 34 29 2f 28 70 29 2d 3e 6e 42  eSize-4)/(p)->nB
4867b 79 74 65 73 50 65 72 43 65 6c 6c 29 2f 33 29 0a  ytesPerCell)/3).
4867c 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 52 45  #define RTREE_RE
4867d 49 4e 53 45 52 54 28 70 29 20 52 54 52 45 45 5f  INSERT(p) RTREE_
4867e 4d 49 4e 43 45 4c 4c 53 28 70 29 0a 23 64 65 66  MINCELLS(p).#def
4867f 69 6e 65 20 52 54 52 45 45 5f 4d 41 58 43 45 4c  ine RTREE_MAXCEL
48680 4c 53 20 35 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  LS 51../*.** The
48681 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62   smallest possib
48682 6c 65 20 6e 6f 64 65 2d 73 69 7a 65 20 69 73 20  le node-size is 
48683 28 35 31 32 2d 36 34 29 3d 3d 34 34 38 20 62 79  (512-64)==448 by
48684 74 65 73 2e 20 41 6e 64 20 74 68 65 20 6c 61 72  tes. And the lar
48685 67 65 73 74 0a 2a 2a 20 73 75 70 70 6f 72 74 65  gest.** supporte
48686 64 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34  d cell size is 4
48687 38 20 62 79 74 65 73 20 28 38 20 62 79 74 65 20  8 bytes (8 byte 
48688 72 6f 77 69 64 20 2b 20 74 65 6e 20 34 20 62 79  rowid + ten 4 by
48689 74 65 20 63 6f 6f 72 64 69 6e 61 74 65 73 29 2e  te coordinates).
4868a 0a 2a 2a 20 54 68 65 72 65 66 6f 72 65 20 61 6c  .** Therefore al
4868b 6c 20 6e 6f 6e 2d 72 6f 6f 74 20 6e 6f 64 65 73  l non-root nodes
4868c 20 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 61 74   must contain at
4868d 20 6c 65 61 73 74 20 33 20 65 6e 74 72 69 65 73   least 3 entries
4868e 2e 20 53 69 6e 63 65 20 0a 2a 2a 20 32 5e 34 30  . Since .** 2^40
4868f 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
48690 20 32 5e 36 34 2c 20 61 6e 20 72 2d 74 72 65 65   2^64, an r-tree
48691 20 73 74 72 75 63 74 75 72 65 20 61 6c 77 61 79   structure alway
48692 73 20 68 61 73 20 61 20 64 65 70 74 68 20 6f 66  s has a depth of
48693 0a 2a 2a 20 34 30 20 6f 72 20 6c 65 73 73 2e 0a  .** 40 or less..
48694 2a 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45  */.#define RTREE
48695 5f 4d 41 58 5f 44 45 50 54 48 20 34 30 0a 0a 2f  _MAX_DEPTH 40../
48696 2a 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65 20 63  * .** An rtree c
48697 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e 0a 2a 2f  ursor object..*/
48698 0a 73 74 72 75 63 74 20 52 74 72 65 65 43 75 72  .struct RtreeCur
48699 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  sor {.  sqlite3_
4869a 76 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65  vtab_cursor base
4869b 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ;.  RtreeNode *p
4869c 4e 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  Node;           
4869d 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 63 75        /* Node cu
4869e 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74 6c  rsor is currentl
4869f 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 2a 2f  y pointing at */
486a0 0a 20 20 69 6e 74 20 69 43 65 6c 6c 3b 20 20 20  .  int iCell;   
486a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
486a2 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
486a3 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e   current cell in
486a4 20 70 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20   pNode */.  int 
486a5 69 53 74 72 61 74 65 67 79 3b 20 20 20 20 20 20  iStrategy;      
486a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
486a7 20 43 6f 70 79 20 6f 66 20 69 64 78 4e 75 6d 20   Copy of idxNum 
486a8 73 65 61 72 63 68 20 70 61 72 61 6d 65 74 65 72  search parameter
486a9 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6e 73 74   */.  int nConst
486aa 72 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  raint;          
486ab 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
486ac 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
486ad 61 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20  aConstraint */. 
486ae 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74   RtreeConstraint
486af 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20   *aConstraint;  
486b0 20 20 20 2f 2a 20 53 65 61 72 63 68 20 63 6f 6e     /* Search con
486b1 73 74 72 61 69 6e 74 73 2e 20 2a 2f 0a 7d 3b 0a  straints. */.};.
486b2 0a 75 6e 69 6f 6e 20 52 74 72 65 65 43 6f 6f 72  .union RtreeCoor
486b3 64 20 7b 0a 20 20 52 74 72 65 65 56 61 6c 75 65  d {.  RtreeValue
486b4 20 66 3b 0a 20 20 69 6e 74 20 69 3b 0a 7d 3b 0a   f;.  int i;.};.
486b5 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  ./*.** The argum
486b6 65 6e 74 20 69 73 20 61 6e 20 52 74 72 65 65 43  ent is an RtreeC
486b7 6f 6f 72 64 2e 20 52 65 74 75 72 6e 20 74 68 65  oord. Return the
486b8 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 77 69   value stored wi
486b9 74 68 69 6e 20 74 68 65 20 52 74 72 65 65 43 6f  thin the RtreeCo
486ba 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 74 65 64  ord.** formatted
486bb 20 61 73 20 61 20 52 74 72 65 65 44 56 61 6c 75   as a RtreeDValu
486bc 65 20 28 64 6f 75 62 6c 65 20 6f 72 20 69 6e 74  e (double or int
486bd 36 34 29 2e 20 54 68 69 73 20 6d 61 63 72 6f 20  64). This macro 
486be 61 73 73 75 6d 65 73 20 74 68 61 74 20 6c 6f 63  assumes that loc
486bf 61 6c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 70  al.** variable p
486c0 52 74 72 65 65 20 70 6f 69 6e 74 73 20 74 6f 20  Rtree points to 
486c1 74 68 65 20 52 74 72 65 65 20 73 74 72 75 63 74  the Rtree struct
486c2 75 72 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ure associated w
486c3 69 74 68 20 74 68 65 0a 2a 2a 20 52 74 72 65 65  ith the.** Rtree
486c4 43 6f 6f 72 64 2e 0a 2a 2f 0a 23 69 66 64 65 66  Coord..*/.#ifdef
486c5 20 53 51 4c 49 54 45 5f 52 54 52 45 45 5f 49 4e   SQLITE_RTREE_IN
486c6 54 5f 4f 4e 4c 59 0a 23 20 64 65 66 69 6e 65 20  T_ONLY.# define 
486c7 44 43 4f 4f 52 44 28 63 6f 6f 72 64 29 20 28 28  DCOORD(coord) ((
486c8 52 74 72 65 65 44 56 61 6c 75 65 29 63 6f 6f 72  RtreeDValue)coor
486c9 64 2e 69 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  d.i).#else.# def
486ca 69 6e 65 20 44 43 4f 4f 52 44 28 63 6f 6f 72 64  ine DCOORD(coord
486cb 29 20 28 20 20 20 20 20 20 20 20 20 20 20 20 20  ) (             
486cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
486cd 20 20 20 20 28 70 52 74 72 65 65 2d 3e 65 43 6f      (pRtree->eCo
486ce 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43  ordType==RTREE_C
486cf 4f 4f 52 44 5f 52 45 41 4c 33 32 29 20 3f 20 20  OORD_REAL32) ?  
486d0 20 20 20 20 5c 0a 20 20 20 20 20 20 28 28 64 6f      \.      ((do
486d1 75 62 6c 65 29 63 6f 6f 72 64 2e 66 29 20 3a 20  uble)coord.f) : 
486d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
486d3 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
486d4 20 20 28 28 64 6f 75 62 6c 65 29 63 6f 6f 72 64    ((double)coord
486d5 2e 69 29 20 20 20 20 20 20 20 20 20 20 20 20 20  .i)             
486d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
486d7 5c 0a 20 20 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  \.  ).#endif../*
486d8 0a 2a 2a 20 41 20 73 65 61 72 63 68 20 63 6f 6e  .** A search con
486d9 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a 73 74 72 75  straint..*/.stru
486da 63 74 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69  ct RtreeConstrai
486db 6e 74 20 7b 0a 20 20 69 6e 74 20 69 43 6f 6f 72  nt {.  int iCoor
486dc 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
486dd 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
486de 6f 66 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 63  of constrained c
486df 6f 6f 72 64 69 6e 61 74 65 20 2a 2f 0a 20 20 69  oordinate */.  i
486e0 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20  nt op;          
486e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
486e2 2a 20 43 6f 6e 73 74 72 61 69 6e 69 6e 67 20 6f  * Constraining o
486e3 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 52 74  peration */.  Rt
486e4 72 65 65 44 56 61 6c 75 65 20 72 56 61 6c 75 65  reeDValue rValue
486e5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
486e6 20 43 6f 6e 73 74 72 61 69 6e 74 20 76 61 6c 75   Constraint valu
486e7 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 47  e. */.  int (*xG
486e8 65 6f 6d 29 28 73 71 6c 69 74 65 33 5f 72 74 72  eom)(sqlite3_rtr
486e9 65 65 5f 67 65 6f 6d 65 74 72 79 2a 2c 20 69 6e  ee_geometry*, in
486ea 74 2c 20 52 74 72 65 65 44 56 61 6c 75 65 2a 2c  t, RtreeDValue*,
486eb 20 69 6e 74 2a 29 3b 0a 20 20 73 71 6c 69 74 65   int*);.  sqlite
486ec 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 79  3_rtree_geometry
486ed 20 2a 70 47 65 6f 6d 3b 20 20 2f 2a 20 43 6f 6e   *pGeom;  /* Con
486ee 73 74 72 61 69 6e 74 20 63 61 6c 6c 62 61 63 6b  straint callback
486ef 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 61 20   argument for a 
486f0 4d 41 54 43 48 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20  MATCH */.};../* 
486f1 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20  Possible values 
486f2 66 6f 72 20 52 74 72 65 65 43 6f 6e 73 74 72 61  for RtreeConstra
486f3 69 6e 74 2e 6f 70 20 2a 2f 0a 23 64 65 66 69 6e  int.op */.#defin
486f4 65 20 52 54 52 45 45 5f 45 51 20 20 20 20 30 78  e RTREE_EQ    0x
486f5 34 31 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45  41.#define RTREE
486f6 5f 4c 45 20 20 20 20 30 78 34 32 0a 23 64 65 66  _LE    0x42.#def
486f7 69 6e 65 20 52 54 52 45 45 5f 4c 54 20 20 20 20  ine RTREE_LT    
486f8 30 78 34 33 0a 23 64 65 66 69 6e 65 20 52 54 52  0x43.#define RTR
486f9 45 45 5f 47 45 20 20 20 20 30 78 34 34 0a 23 64  EE_GE    0x44.#d
486fa 65 66 69 6e 65 20 52 54 52 45 45 5f 47 54 20 20  efine RTREE_GT  
486fb 20 20 30 78 34 35 0a 23 64 65 66 69 6e 65 20 52    0x45.#define R
486fc 54 52 45 45 5f 4d 41 54 43 48 20 30 78 34 36 0a  TREE_MATCH 0x46.
486fd 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65  ./* .** An rtree
486fe 20 73 74 72 75 63 74 75 72 65 20 6e 6f 64 65 2e   structure node.
486ff 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72 65 65  .*/.struct Rtree
48700 4e 6f 64 65 20 7b 0a 20 20 52 74 72 65 65 4e 6f  Node {.  RtreeNo
48701 64 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20 20  de *pParent;    
48702 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
48703 72 65 6e 74 20 6e 6f 64 65 20 2a 2f 0a 20 20 69  rent node */.  i
48704 36 34 20 69 4e 6f 64 65 3b 0a 20 20 69 6e 74 20  64 iNode;.  int 
48705 6e 52 65 66 3b 0a 20 20 69 6e 74 20 69 73 44 69  nRef;.  int isDi
48706 72 74 79 3b 0a 20 20 75 38 20 2a 7a 44 61 74 61  rty;.  u8 *zData
48707 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ;.  RtreeNode *p
48708 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20  Next;           
48709 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 6e 6f        /* Next no
4870a 64 65 20 69 6e 20 74 68 69 73 20 68 61 73 68 20  de in this hash 
4870b 63 68 61 69 6e 20 2a 2f 0a 7d 3b 0a 23 64 65 66  chain */.};.#def
4870c 69 6e 65 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29  ine NCELL(pNode)
4870d 20 72 65 61 64 49 6e 74 31 36 28 26 28 70 4e 6f   readInt16(&(pNo
4870e 64 65 29 2d 3e 7a 44 61 74 61 5b 32 5d 29 0a 0a  de)->zData[2])..
4870f 2f 2a 20 0a 2a 2a 20 53 74 72 75 63 74 75 72 65  /* .** Structure
48710 20 74 6f 20 73 74 6f 72 65 20 61 20 64 65 73 65   to store a dese
48711 72 69 61 6c 69 7a 65 64 20 72 74 72 65 65 20 72  rialized rtree r
48712 65 63 6f 72 64 2e 0a 2a 2f 0a 73 74 72 75 63 74  ecord..*/.struct
48713 20 52 74 72 65 65 43 65 6c 6c 20 7b 0a 20 20 69   RtreeCell {.  i
48714 36 34 20 69 52 6f 77 69 64 3b 0a 20 20 52 74 72  64 iRowid;.  Rtr
48715 65 65 43 6f 6f 72 64 20 61 43 6f 6f 72 64 5b 52  eeCoord aCoord[R
48716 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49  TREE_MAX_DIMENSI
48717 4f 4e 53 2a 32 5d 3b 0a 7d 3b 0a 0a 0a 2f 2a 0a  ONS*2];.};.../*.
48718 2a 2a 20 56 61 6c 75 65 20 66 6f 72 20 74 68 65  ** Value for the
48719 20 66 69 72 73 74 20 66 69 65 6c 64 20 6f 66 20   first field of 
4871a 65 76 65 72 79 20 52 74 72 65 65 4d 61 74 63 68  every RtreeMatch
4871b 41 72 67 20 6f 62 6a 65 63 74 2e 20 54 68 65 20  Arg object. The 
4871c 4d 41 54 43 48 0a 2a 2a 20 6f 70 65 72 61 74 6f  MATCH.** operato
4871d 72 20 74 65 73 74 73 20 74 68 61 74 20 74 68 65  r tests that the
4871e 20 66 69 72 73 74 20 66 69 65 6c 64 20 6f 66 20   first field of 
4871f 61 20 62 6c 6f 62 20 6f 70 65 72 61 6e 64 20 6d  a blob operand m
48720 61 74 63 68 65 73 20 74 68 69 73 0a 2a 2a 20 76  atches this.** v
48721 61 6c 75 65 20 74 6f 20 61 76 6f 69 64 20 6f 70  alue to avoid op
48722 65 72 61 74 69 6e 67 20 6f 6e 20 69 6e 76 61 6c  erating on inval
48723 69 64 20 62 6c 6f 62 73 20 28 77 68 69 63 68 20  id blobs (which 
48724 63 6f 75 6c 64 20 63 61 75 73 65 20 61 20 73 65  could cause a se
48725 67 66 61 75 6c 74 29 2e 0a 2a 2f 0a 23 64 65 66  gfault)..*/.#def
48726 69 6e 65 20 52 54 52 45 45 5f 47 45 4f 4d 45 54  ine RTREE_GEOMET
48727 52 59 5f 4d 41 47 49 43 20 30 78 38 39 31 32 34  RY_MAGIC 0x89124
48728 35 41 42 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  5AB../*.** An in
48729 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
4872a 74 72 75 63 74 75 72 65 20 6d 75 73 74 20 62 65  tructure must be
4872b 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 20 62   supplied as a b
4872c 6c 6f 62 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a  lob argument to.
4872d 2a 2a 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  ** the right-han
4872e 64 2d 73 69 64 65 20 6f 66 20 61 6e 20 53 51 4c  d-side of an SQL
4872f 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 20   MATCH operator 
48730 75 73 65 64 20 74 6f 20 63 6f 6e 73 74 72 61 69  used to constrai
48731 6e 20 61 6e 0a 2a 2a 20 72 2d 74 72 65 65 20 71  n an.** r-tree q
48732 75 65 72 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  uery..*/.struct 
48733 52 74 72 65 65 4d 61 74 63 68 41 72 67 20 7b 0a  RtreeMatchArg {.
48734 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20    u32 magic;    
48735 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48736 20 20 2f 2a 20 41 6c 77 61 79 73 20 52 54 52 45    /* Always RTRE
48737 45 5f 47 45 4f 4d 45 54 52 59 5f 4d 41 47 49 43  E_GEOMETRY_MAGIC
48738 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 47 65 6f   */.  int (*xGeo
48739 6d 29 28 73 71 6c 69 74 65 33 5f 72 74 72 65 65  m)(sqlite3_rtree
4873a 5f 67 65 6f 6d 65 74 72 79 20 2a 2c 20 69 6e 74  _geometry *, int
4873b 2c 20 52 74 72 65 65 44 56 61 6c 75 65 2a 2c 20  , RtreeDValue*, 
4873c 69 6e 74 20 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  int *);.  void *
4873d 70 43 6f 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20  pContext;.  int 
4873e 6e 50 61 72 61 6d 3b 0a 20 20 52 74 72 65 65 44  nParam;.  RtreeD
4873f 56 61 6c 75 65 20 61 50 61 72 61 6d 5b 31 5d 3b  Value aParam[1];
48740 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  .};../*.** When 
48741 61 20 67 65 6f 6d 65 74 72 79 20 63 61 6c 6c 62  a geometry callb
48742 61 63 6b 20 69 73 20 63 72 65 61 74 65 64 20 28  ack is created (
48743 73 65 65 20 73 71 6c 69 74 65 33 5f 72 74 72 65  see sqlite3_rtre
48744 65 5f 67 65 6f 6d 65 74 72 79 5f 63 61 6c 6c 62  e_geometry_callb
48745 61 63 6b 29 2c 0a 2a 2a 20 61 20 73 69 6e 67 6c  ack),.** a singl
48746 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  e instance of th
48747 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
48748 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74  cture is allocat
48749 65 64 2e 20 49 74 20 69 73 20 75 73 65 64 0a 2a  ed. It is used.*
4874a 2a 20 61 73 20 74 68 65 20 63 6f 6e 74 65 78 74  * as the context
4874b 20 66 6f 72 20 74 68 65 20 75 73 65 72 2d 66 75   for the user-fu
4874c 6e 63 74 69 6f 6e 20 63 72 65 61 74 65 64 20 62  nction created b
4874d 79 20 62 79 20 73 5f 72 5f 67 5f 63 28 29 2e 20  y by s_r_g_c(). 
4874e 54 68 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 69 73  The object.** is
4874f 20 65 76 65 6e 74 75 61 6c 6c 79 20 64 65 6c 65   eventually dele
48750 74 65 64 20 62 79 20 74 68 65 20 64 65 73 74 72  ted by the destr
48751 75 63 74 6f 72 20 6d 65 63 68 61 6e 69 73 6d 20  uctor mechanism 
48752 70 72 6f 76 69 64 65 64 20 62 79 0a 2a 2a 20 73  provided by.** s
48753 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
48754 6e 63 74 69 6f 6e 5f 76 32 28 29 20 28 77 68 69  nction_v2() (whi
48755 63 68 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  ch is called by 
48756 73 5f 72 5f 67 5f 63 28 29 20 74 6f 20 63 72 65  s_r_g_c() to cre
48757 61 74 65 0a 2a 2a 20 74 68 65 20 67 65 6f 6d 65  ate.** the geome
48758 74 72 79 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  try callback fun
48759 63 74 69 6f 6e 29 2e 0a 2a 2f 0a 73 74 72 75 63  ction)..*/.struc
4875a 74 20 52 74 72 65 65 47 65 6f 6d 43 61 6c 6c 62  t RtreeGeomCallb
4875b 61 63 6b 20 7b 0a 20 20 69 6e 74 20 28 2a 78 47  ack {.  int (*xG
4875c 65 6f 6d 29 28 73 71 6c 69 74 65 33 5f 72 74 72  eom)(sqlite3_rtr
4875d 65 65 5f 67 65 6f 6d 65 74 72 79 2a 2c 20 69 6e  ee_geometry*, in
4875e 74 2c 20 52 74 72 65 65 44 56 61 6c 75 65 2a 2c  t, RtreeDValue*,
4875f 20 69 6e 74 2a 29 3b 0a 20 20 76 6f 69 64 20 2a   int*);.  void *
48760 70 43 6f 6e 74 65 78 74 3b 0a 7d 3b 0a 0a 23 69  pContext;.};..#i
48761 66 6e 64 65 66 20 4d 41 58 0a 23 20 64 65 66 69  fndef MAX.# defi
48762 6e 65 20 4d 41 58 28 78 2c 79 29 20 28 28 78 29  ne MAX(x,y) ((x)
48763 20 3c 20 28 79 29 20 3f 20 28 79 29 20 3a 20 28   < (y) ? (y) : (
48764 78 29 29 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  x)).#endif.#ifnd
48765 65 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20  ef MIN.# define 
48766 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 20 3e 20  MIN(x,y) ((x) > 
48767 28 79 29 20 3f 20 28 79 29 20 3a 20 28 78 29 29  (y) ? (y) : (x))
48768 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46  .#endif../*.** F
48769 75 6e 63 74 69 6f 6e 73 20 74 6f 20 64 65 73 65  unctions to dese
4876a 72 69 61 6c 69 7a 65 20 61 20 31 36 20 62 69 74  rialize a 16 bit
4876b 20 69 6e 74 65 67 65 72 2c 20 33 32 20 62 69 74   integer, 32 bit
4876c 20 72 65 61 6c 20 6e 75 6d 62 65 72 20 61 6e 64   real number and
4876d 0a 2a 2a 20 36 34 20 62 69 74 20 69 6e 74 65 67  .** 64 bit integ
4876e 65 72 2e 20 54 68 65 20 64 65 73 65 72 69 61 6c  er. The deserial
4876f 69 7a 65 64 20 76 61 6c 75 65 20 69 73 20 72 65  ized value is re
48770 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  turned..*/.stati
48771 63 20 69 6e 74 20 72 65 61 64 49 6e 74 31 36 28  c int readInt16(
48772 75 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  u8 *p){.  return
48773 20 28 70 5b 30 5d 3c 3c 38 29 20 2b 20 70 5b 31   (p[0]<<8) + p[1
48774 5d 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  ];.}.static void
48775 20 72 65 61 64 43 6f 6f 72 64 28 75 38 20 2a 70   readCoord(u8 *p
48776 2c 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43  , RtreeCoord *pC
48777 6f 6f 72 64 29 7b 0a 20 20 75 33 32 20 69 20 3d  oord){.  u32 i =
48778 20 28 0a 20 20 20 20 28 28 28 75 33 32 29 70 5b   (.    (((u32)p[
48779 30 5d 29 20 3c 3c 20 32 34 29 20 2b 20 0a 20 20  0]) << 24) + .  
4877a 20 20 28 28 28 75 33 32 29 70 5b 31 5d 29 20 3c    (((u32)p[1]) <
4877b 3c 20 31 36 29 20 2b 20 0a 20 20 20 20 28 28 28  < 16) + .    (((
4877c 75 33 32 29 70 5b 32 5d 29 20 3c 3c 20 20 38 29  u32)p[2]) <<  8)
4877d 20 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70   + .    (((u32)p
4877e 5b 33 5d 29 20 3c 3c 20 20 30 29 0a 20 20 29 3b  [3]) <<  0).  );
4877f 0a 20 20 2a 28 75 33 32 20 2a 29 70 43 6f 6f 72  .  *(u32 *)pCoor
48780 64 20 3d 20 69 3b 0a 7d 0a 73 74 61 74 69 63 20  d = i;.}.static 
48781 69 36 34 20 72 65 61 64 49 6e 74 36 34 28 75 38  i64 readInt64(u8
48782 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28   *p){.  return (
48783 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 30 5d  .    (((i64)p[0]
48784 29 20 3c 3c 20 35 36 29 20 2b 20 0a 20 20 20 20  ) << 56) + .    
48785 28 28 28 69 36 34 29 70 5b 31 5d 29 20 3c 3c 20  (((i64)p[1]) << 
48786 34 38 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36  48) + .    (((i6
48787 34 29 70 5b 32 5d 29 20 3c 3c 20 34 30 29 20 2b  4)p[2]) << 40) +
48788 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 33   .    (((i64)p[3
48789 5d 29 20 3c 3c 20 33 32 29 20 2b 20 0a 20 20 20  ]) << 32) + .   
4878a 20 28 28 28 69 36 34 29 70 5b 34 5d 29 20 3c 3c   (((i64)p[4]) <<
4878b 20 32 34 29 20 2b 20 0a 20 20 20 20 28 28 28 69   24) + .    (((i
4878c 36 34 29 70 5b 35 5d 29 20 3c 3c 20 31 36 29 20  64)p[5]) << 16) 
4878d 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b  + .    (((i64)p[
4878e 36 5d 29 20 3c 3c 20 20 38 29 20 2b 20 0a 20 20  6]) <<  8) + .  
4878f 20 20 28 28 28 69 36 34 29 70 5b 37 5d 29 20 3c    (((i64)p[7]) <
48790 3c 20 20 30 29 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a  <  0).  );.}../*
48791 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 74 6f  .** Functions to
48792 20 73 65 72 69 61 6c 69 7a 65 20 61 20 31 36 20   serialize a 16 
48793 62 69 74 20 69 6e 74 65 67 65 72 2c 20 33 32 20  bit integer, 32 
48794 62 69 74 20 72 65 61 6c 20 6e 75 6d 62 65 72 20  bit real number 
48795 61 6e 64 0a 2a 2a 20 36 34 20 62 69 74 20 69 6e  and.** 64 bit in
48796 74 65 67 65 72 2e 20 54 68 65 20 76 61 6c 75 65  teger. The value
48797 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
48798 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
48799 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 74   written.** to t
4879a 68 65 20 61 72 67 75 6d 65 6e 74 20 62 75 66 66  he argument buff
4879b 65 72 20 28 61 6c 77 61 79 73 20 32 2c 20 34 20  er (always 2, 4 
4879c 61 6e 64 20 38 20 72 65 73 70 65 63 74 69 76 65  and 8 respective
4879d 6c 79 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ly)..*/.static i
4879e 6e 74 20 77 72 69 74 65 49 6e 74 31 36 28 75 38  nt writeInt16(u8
4879f 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 70   *p, int i){.  p
487a0 5b 30 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78  [0] = (i>> 8)&0x
487a1 46 46 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 69 3e  FF;.  p[1] = (i>
487a2 3e 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74  > 0)&0xFF;.  ret
487a3 75 72 6e 20 32 3b 0a 7d 0a 73 74 61 74 69 63 20  urn 2;.}.static 
487a4 69 6e 74 20 77 72 69 74 65 43 6f 6f 72 64 28 75  int writeCoord(u
487a5 38 20 2a 70 2c 20 52 74 72 65 65 43 6f 6f 72 64  8 *p, RtreeCoord
487a6 20 2a 70 43 6f 6f 72 64 29 7b 0a 20 20 75 33 32   *pCoord){.  u32
487a7 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69   i;.  assert( si
487a8 7a 65 6f 66 28 52 74 72 65 65 43 6f 6f 72 64 29  zeof(RtreeCoord)
487a9 3d 3d 34 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==4 );.  assert(
487aa 20 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34 20   sizeof(u32)==4 
487ab 29 3b 0a 20 20 69 20 3d 20 2a 28 75 33 32 20 2a  );.  i = *(u32 *
487ac 29 70 43 6f 6f 72 64 3b 0a 20 20 70 5b 30 5d 20  )pCoord;.  p[0] 
487ad 3d 20 28 69 3e 3e 32 34 29 26 30 78 46 46 3b 0a  = (i>>24)&0xFF;.
487ae 20 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 31 36 29    p[1] = (i>>16)
487af 26 30 78 46 46 3b 0a 20 20 70 5b 32 5d 20 3d 20  &0xFF;.  p[2] = 
487b0 28 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20 20  (i>> 8)&0xFF;.  
487b1 70 5b 33 5d 20 3d 20 28 69 3e 3e 20 30 29 26 30  p[3] = (i>> 0)&0
487b2 78 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 34 3b  xFF;.  return 4;
487b3 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72  .}.static int wr
487b4 69 74 65 49 6e 74 36 34 28 75 38 20 2a 70 2c 20  iteInt64(u8 *p, 
487b5 69 36 34 20 69 29 7b 0a 20 20 70 5b 30 5d 20 3d  i64 i){.  p[0] =
487b6 20 28 69 3e 3e 35 36 29 26 30 78 46 46 3b 0a 20   (i>>56)&0xFF;. 
487b7 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 34 38 29 26   p[1] = (i>>48)&
487b8 30 78 46 46 3b 0a 20 20 70 5b 32 5d 20 3d 20 28  0xFF;.  p[2] = (
487b9 69 3e 3e 34 30 29 26 30 78 46 46 3b 0a 20 20 70  i>>40)&0xFF;.  p
487ba 5b 33 5d 20 3d 20 28 69 3e 3e 33 32 29 26 30 78  [3] = (i>>32)&0x
487bb 46 46 3b 0a 20 20 70 5b 34 5d 20 3d 20 28 69 3e  FF;.  p[4] = (i>
487bc 3e 32 34 29 26 30 78 46 46 3b 0a 20 20 70 5b 35  >24)&0xFF;.  p[5
487bd 5d 20 3d 20 28 69 3e 3e 31 36 29 26 30 78 46 46  ] = (i>>16)&0xFF
487be 3b 0a 20 20 70 5b 36 5d 20 3d 20 28 69 3e 3e 20  ;.  p[6] = (i>> 
487bf 38 29 26 30 78 46 46 3b 0a 20 20 70 5b 37 5d 20  8)&0xFF;.  p[7] 
487c0 3d 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b 0a  = (i>> 0)&0xFF;.
487c1 20 20 72 65 74 75 72 6e 20 38 3b 0a 7d 0a 0a 2f    return 8;.}../
487c2 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74  *.** Increment t
487c3 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
487c4 6e 74 20 6f 66 20 6e 6f 64 65 20 70 2e 0a 2a 2f  nt of node p..*/
487c5 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64  .static void nod
487c6 65 52 65 66 65 72 65 6e 63 65 28 52 74 72 65 65  eReference(Rtree
487c7 4e 6f 64 65 20 2a 70 29 7b 0a 20 20 69 66 28 20  Node *p){.  if( 
487c8 70 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 52 65 66  p ){.    p->nRef
487c9 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ++;.  }.}../*.**
487ca 20 43 6c 65 61 72 20 74 68 65 20 63 6f 6e 74 65   Clear the conte
487cb 6e 74 20 6f 66 20 6e 6f 64 65 20 70 20 28 73 65  nt of node p (se
487cc 74 20 61 6c 6c 20 62 79 74 65 73 20 74 6f 20 30  t all bytes to 0
487cd 78 30 30 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  x00)..*/.static 
487ce 76 6f 69 64 20 6e 6f 64 65 5a 65 72 6f 28 52 74  void nodeZero(Rt
487cf 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
487d0 65 65 4e 6f 64 65 20 2a 70 29 7b 0a 20 20 6d 65  eeNode *p){.  me
487d1 6d 73 65 74 28 26 70 2d 3e 7a 44 61 74 61 5b 32  mset(&p->zData[2
487d2 5d 2c 20 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e  ], 0, pRtree->iN
487d3 6f 64 65 53 69 7a 65 2d 32 29 3b 0a 20 20 70 2d  odeSize-2);.  p-
487d4 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 7d 0a  >isDirty = 1;.}.
487d5 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 6e  ./*.** Given a n
487d6 6f 64 65 20 6e 75 6d 62 65 72 20 69 4e 6f 64 65  ode number iNode
487d7 2c 20 72 65 74 75 72 6e 20 74 68 65 20 63 6f 72  , return the cor
487d8 72 65 73 70 6f 6e 64 69 6e 67 20 6b 65 79 20 74  responding key t
487d9 6f 20 75 73 65 0a 2a 2a 20 69 6e 20 74 68 65 20  o use.** in the 
487da 52 74 72 65 65 2e 61 48 61 73 68 20 74 61 62 6c  Rtree.aHash tabl
487db 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
487dc 20 6e 6f 64 65 48 61 73 68 28 69 36 34 20 69 4e   nodeHash(i64 iN
487dd 6f 64 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  ode){.  return (
487de 0a 20 20 20 20 28 69 4e 6f 64 65 3e 3e 35 36 29  .    (iNode>>56)
487df 20 5e 20 28 69 4e 6f 64 65 3e 3e 34 38 29 20 5e   ^ (iNode>>48) ^
487e0 20 28 69 4e 6f 64 65 3e 3e 34 30 29 20 5e 20 28   (iNode>>40) ^ (
487e1 69 4e 6f 64 65 3e 3e 33 32 29 20 5e 20 0a 20 20  iNode>>32) ^ .  
487e2 20 20 28 69 4e 6f 64 65 3e 3e 32 34 29 20 5e 20    (iNode>>24) ^ 
487e3 28 69 4e 6f 64 65 3e 3e 31 36 29 20 5e 20 28 69  (iNode>>16) ^ (i
487e4 4e 6f 64 65 3e 3e 20 38 29 20 5e 20 28 69 4e 6f  Node>> 8) ^ (iNo
487e5 64 65 3e 3e 20 30 29 0a 20 20 29 20 25 20 48 41  de>> 0).  ) % HA
487e6 53 48 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  SHSIZE;.}../*.**
487e7 20 53 65 61 72 63 68 20 74 68 65 20 6e 6f 64 65   Search the node
487e8 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20   hash table for 
487e9 6e 6f 64 65 20 69 4e 6f 64 65 2e 20 49 66 20 66  node iNode. If f
487ea 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 61 20 70  ound, return a p
487eb 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 69 74 2e  ointer.** to it.
487ec 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75   Otherwise, retu
487ed 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rn 0..*/.static 
487ee 52 74 72 65 65 4e 6f 64 65 20 2a 6e 6f 64 65 48  RtreeNode *nodeH
487ef 61 73 68 4c 6f 6f 6b 75 70 28 52 74 72 65 65 20  ashLookup(Rtree 
487f0 2a 70 52 74 72 65 65 2c 20 69 36 34 20 69 4e 6f  *pRtree, i64 iNo
487f1 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65  de){.  RtreeNode
487f2 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 52 74   *p;.  for(p=pRt
487f3 72 65 65 2d 3e 61 48 61 73 68 5b 6e 6f 64 65 48  ree->aHash[nodeH
487f4 61 73 68 28 69 4e 6f 64 65 29 5d 3b 20 70 20 26  ash(iNode)]; p &
487f5 26 20 70 2d 3e 69 4e 6f 64 65 21 3d 69 4e 6f 64  & p->iNode!=iNod
487f6 65 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 3b 0a  e; p=p->pNext);.
487f7 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
487f8 2a 0a 2a 2a 20 41 64 64 20 6e 6f 64 65 20 70 4e  *.** Add node pN
487f9 6f 64 65 20 74 6f 20 74 68 65 20 6e 6f 64 65 20  ode to the node 
487fa 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  hash table..*/.s
487fb 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 48  tatic void nodeH
487fc 61 73 68 49 6e 73 65 72 74 28 52 74 72 65 65 20  ashInsert(Rtree 
487fd 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f  *pRtree, RtreeNo
487fe 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e  de *pNode){.  in
487ff 74 20 69 48 61 73 68 3b 0a 20 20 61 73 73 65 72  t iHash;.  asser
48800 74 28 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 3d  t( pNode->pNext=
48801 3d 30 20 29 3b 0a 20 20 69 48 61 73 68 20 3d 20  =0 );.  iHash = 
48802 6e 6f 64 65 48 61 73 68 28 70 4e 6f 64 65 2d 3e  nodeHash(pNode->
48803 69 4e 6f 64 65 29 3b 0a 20 20 70 4e 6f 64 65 2d  iNode);.  pNode-
48804 3e 70 4e 65 78 74 20 3d 20 70 52 74 72 65 65 2d  >pNext = pRtree-
48805 3e 61 48 61 73 68 5b 69 48 61 73 68 5d 3b 0a 20  >aHash[iHash];. 
48806 20 70 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 69   pRtree->aHash[i
48807 48 61 73 68 5d 20 3d 20 70 4e 6f 64 65 3b 0a 7d  Hash] = pNode;.}
48808 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 6e  ../*.** Remove n
48809 6f 64 65 20 70 4e 6f 64 65 20 66 72 6f 6d 20 74  ode pNode from t
4880a 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74 61 62  he node hash tab
4880b 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le..*/.static vo
4880c 69 64 20 6e 6f 64 65 48 61 73 68 44 65 6c 65 74  id nodeHashDelet
4880d 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
4880e 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
4880f 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  e){.  RtreeNode 
48810 2a 2a 70 70 3b 0a 20 20 69 66 28 20 70 4e 6f 64  **pp;.  if( pNod
48811 65 2d 3e 69 4e 6f 64 65 21 3d 30 20 29 7b 0a 20  e->iNode!=0 ){. 
48812 20 20 20 70 70 20 3d 20 26 70 52 74 72 65 65 2d     pp = &pRtree-
48813 3e 61 48 61 73 68 5b 6e 6f 64 65 48 61 73 68 28  >aHash[nodeHash(
48814 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 5d 3b 0a  pNode->iNode)];.
48815 20 20 20 20 66 6f 72 28 20 3b 20 28 2a 70 70 29      for( ; (*pp)
48816 21 3d 70 4e 6f 64 65 3b 20 70 70 20 3d 20 26 28  !=pNode; pp = &(
48817 2a 70 70 29 2d 3e 70 4e 65 78 74 29 7b 20 61 73  *pp)->pNext){ as
48818 73 65 72 74 28 2a 70 70 29 3b 20 7d 0a 20 20 20  sert(*pp); }.   
48819 20 2a 70 70 20 3d 20 70 4e 6f 64 65 2d 3e 70 4e   *pp = pNode->pN
4881a 65 78 74 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  ext;.    pNode->
4881b 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 7d 0a 7d  pNext = 0;.  }.}
4881c 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
4881d 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 65 77 20   and return new 
4881e 72 2d 74 72 65 65 20 6e 6f 64 65 2e 20 49 6e 69  r-tree node. Ini
4881f 74 69 61 6c 6c 79 2c 20 28 52 74 72 65 65 4e 6f  tially, (RtreeNo
48820 64 65 2e 69 4e 6f 64 65 3d 3d 30 29 2c 0a 2a 2a  de.iNode==0),.**
48821 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
48822 20 6e 6f 64 65 20 68 61 73 20 6e 6f 74 20 79 65   node has not ye
48823 74 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64 20  t been assigned 
48824 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20 49  a node number. I
48825 74 20 69 73 0a 2a 2a 20 61 73 73 69 67 6e 65 64  t is.** assigned
48826 20 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20 77   a node number w
48827 68 65 6e 20 6e 6f 64 65 57 72 69 74 65 28 29 20  hen nodeWrite() 
48828 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 77 72 69  is called to wri
48829 74 65 20 74 68 65 0a 2a 2a 20 6e 6f 64 65 20 63  te the.** node c
4882a 6f 6e 74 65 6e 74 73 20 6f 75 74 20 74 6f 20 74  ontents out to t
4882b 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a  he database..*/.
4882c 73 74 61 74 69 63 20 52 74 72 65 65 4e 6f 64 65  static RtreeNode
4882d 20 2a 6e 6f 64 65 4e 65 77 28 52 74 72 65 65 20   *nodeNew(Rtree 
4882e 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f  *pRtree, RtreeNo
4882f 64 65 20 2a 70 50 61 72 65 6e 74 29 7b 0a 20 20  de *pParent){.  
48830 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
48831 3b 0a 20 20 70 4e 6f 64 65 20 3d 20 28 52 74 72  ;.  pNode = (Rtr
48832 65 65 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65 33  eeNode *)sqlite3
48833 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52  _malloc(sizeof(R
48834 74 72 65 65 4e 6f 64 65 29 20 2b 20 70 52 74 72  treeNode) + pRtr
48835 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0a  ee->iNodeSize);.
48836 20 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20    if( pNode ){. 
48837 20 20 20 6d 65 6d 73 65 74 28 70 4e 6f 64 65 2c     memset(pNode,
48838 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65   0, sizeof(Rtree
48839 4e 6f 64 65 29 20 2b 20 70 52 74 72 65 65 2d 3e  Node) + pRtree->
4883a 69 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 20 20  iNodeSize);.    
4883b 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 20 3d 20 28  pNode->zData = (
4883c 75 38 20 2a 29 26 70 4e 6f 64 65 5b 31 5d 3b 0a  u8 *)&pNode[1];.
4883d 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 20      pNode->nRef 
4883e 3d 20 31 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  = 1;.    pNode->
4883f 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e  pParent = pParen
48840 74 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73  t;.    pNode->is
48841 44 69 72 74 79 20 3d 20 31 3b 0a 20 20 20 20 6e  Dirty = 1;.    n
48842 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 50 61  odeReference(pPa
48843 72 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74  rent);.  }.  ret
48844 75 72 6e 20 70 4e 6f 64 65 3b 0a 7d 0a 0a 2f 2a  urn pNode;.}../*
48845 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 72 65 66  .** Obtain a ref
48846 65 72 65 6e 63 65 20 74 6f 20 61 6e 20 72 2d 74  erence to an r-t
48847 72 65 65 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61  ree node..*/.sta
48848 74 69 63 20 69 6e 74 0a 6e 6f 64 65 41 63 71 75  tic int.nodeAcqu
48849 69 72 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ire(.  Rtree *pR
4884a 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20  tree,           
4884b 20 20 2f 2a 20 52 2d 74 72 65 65 20 73 74 72 75    /* R-tree stru
4884c 63 74 75 72 65 20 2a 2f 0a 20 20 69 36 34 20 69  cture */.  i64 i
4884d 4e 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20  Node,           
4884e 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 6e 75        /* Node nu
4884f 6d 62 65 72 20 74 6f 20 6c 6f 61 64 20 2a 2f 0a  mber to load */.
48850 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61    RtreeNode *pPa
48851 72 65 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20  rent,        /* 
48852 45 69 74 68 65 72 20 74 68 65 20 70 61 72 65 6e  Either the paren
48853 74 20 6e 6f 64 65 20 6f 72 20 4e 55 4c 4c 20 2a  t node or NULL *
48854 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a  /.  RtreeNode **
48855 70 70 4e 6f 64 65 20 20 20 20 20 20 20 20 20 2f  ppNode         /
48856 2a 20 4f 55 54 3a 20 41 63 71 75 69 72 65 64 20  * OUT: Acquired 
48857 6e 6f 64 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  node */.){.  int
48858 20 72 63 3b 0a 20 20 69 6e 74 20 72 63 32 20 3d   rc;.  int rc2 =
48859 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 52 74   SQLITE_OK;.  Rt
4885a 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a  reeNode *pNode;.
4885b 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74  .  /* Check if t
4885c 68 65 20 72 65 71 75 65 73 74 65 64 20 6e 6f 64  he requested nod
4885d 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  e is already in 
4885e 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 20  the hash table. 
4885f 49 66 20 73 6f 2c 0a 20 20 2a 2a 20 69 6e 63 72  If so,.  ** incr
48860 65 61 73 65 20 69 74 73 20 72 65 66 65 72 65 6e  ease its referen
48861 63 65 20 63 6f 75 6e 74 20 61 6e 64 20 72 65 74  ce count and ret
48862 75 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69  urn it..  */.  i
48863 66 28 20 28 70 4e 6f 64 65 20 3d 20 6e 6f 64 65  f( (pNode = node
48864 48 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65  HashLookup(pRtre
48865 65 2c 20 69 4e 6f 64 65 29 29 20 29 7b 0a 20 20  e, iNode)) ){.  
48866 20 20 61 73 73 65 72 74 28 20 21 70 50 61 72 65    assert( !pPare
48867 6e 74 20 7c 7c 20 21 70 4e 6f 64 65 2d 3e 70 50  nt || !pNode->pP
48868 61 72 65 6e 74 20 7c 7c 20 70 4e 6f 64 65 2d 3e  arent || pNode->
48869 70 50 61 72 65 6e 74 3d 3d 70 50 61 72 65 6e 74  pParent==pParent
4886a 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72   );.    if( pPar
4886b 65 6e 74 20 26 26 20 21 70 4e 6f 64 65 2d 3e 70  ent && !pNode->p
4886c 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 20 20  Parent ){.      
4886d 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 50  nodeReference(pP
4886e 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 70 4e  arent);.      pN
4886f 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70  ode->pParent = p
48870 50 61 72 65 6e 74 3b 0a 20 20 20 20 7d 0a 20 20  Parent;.    }.  
48871 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b    pNode->nRef++;
48872 0a 20 20 20 20 2a 70 70 4e 6f 64 65 20 3d 20 70  .    *ppNode = p
48873 4e 6f 64 65 3b 0a 20 20 20 20 72 65 74 75 72 6e  Node;.    return
48874 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
48875 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
48876 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52  int64(pRtree->pR
48877 65 61 64 4e 6f 64 65 2c 20 31 2c 20 69 4e 6f 64  eadNode, 1, iNod
48878 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  e);.  rc = sqlit
48879 65 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e  e3_step(pRtree->
4887a 70 52 65 61 64 4e 6f 64 65 29 3b 0a 20 20 69 66  pReadNode);.  if
4887b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc==SQLITE_ROW
4887c 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75 38   ){.    const u8
4887d 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65   *zBlob = sqlite
4887e 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 52  3_column_blob(pR
4887f 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 2c  tree->pReadNode,
48880 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 52 74   0);.    if( pRt
48881 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 3d 3d  ree->iNodeSize==
48882 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
48883 79 74 65 73 28 70 52 74 72 65 65 2d 3e 70 52 65  ytes(pRtree->pRe
48884 61 64 4e 6f 64 65 2c 20 30 29 20 29 7b 0a 20 20  adNode, 0) ){.  
48885 20 20 20 20 70 4e 6f 64 65 20 3d 20 28 52 74 72      pNode = (Rtr
48886 65 65 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65 33  eeNode *)sqlite3
48887 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52  _malloc(sizeof(R
48888 74 72 65 65 4e 6f 64 65 29 2b 70 52 74 72 65 65  treeNode)+pRtree
48889 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20  ->iNodeSize);.  
4888a 20 20 20 20 69 66 28 20 21 70 4e 6f 64 65 20 29      if( !pNode )
4888b 7b 0a 20 20 20 20 20 20 20 20 72 63 32 20 3d 20  {.        rc2 = 
4888c 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
4888d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
4888e 20 20 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e     pNode->pParen
4888f 74 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 20  t = pParent;.   
48890 20 20 20 20 20 70 4e 6f 64 65 2d 3e 7a 44 61 74       pNode->zDat
48891 61 20 3d 20 28 75 38 20 2a 29 26 70 4e 6f 64 65  a = (u8 *)&pNode
48892 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f  [1];.        pNo
48893 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20  de->nRef = 1;.  
48894 20 20 20 20 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f        pNode->iNo
48895 64 65 20 3d 20 69 4e 6f 64 65 3b 0a 20 20 20 20  de = iNode;.    
48896 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72      pNode->isDir
48897 74 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ty = 0;.        
48898 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 30  pNode->pNext = 0
48899 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  ;.        memcpy
4889a 28 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c 20 7a  (pNode->zData, z
4889b 42 6c 6f 62 2c 20 70 52 74 72 65 65 2d 3e 69 4e  Blob, pRtree->iN
4889c 6f 64 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  odeSize);.      
4889d 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28    nodeReference(
4889e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20  pParent);.      
4889f 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 63  }.    }.  }.  rc
488a0 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
488a1 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f  (pRtree->pReadNo
488a2 64 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  de);.  if( rc==S
488a3 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20  QLITE_OK ) rc = 
488a4 72 63 32 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  rc2;..  /* If th
488a5 65 20 72 6f 6f 74 20 6e 6f 64 65 20 77 61 73 20  e root node was 
488a6 6a 75 73 74 20 6c 6f 61 64 65 64 2c 20 73 65 74  just loaded, set
488a7 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 20   pRtree->iDepth 
488a8 74 6f 20 74 68 65 20 68 65 69 67 68 74 0a 20 20  to the height.  
488a9 2a 2a 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65  ** of the r-tree
488aa 20 73 74 72 75 63 74 75 72 65 2e 20 41 20 68 65   structure. A he
488ab 69 67 68 74 20 6f 66 20 7a 65 72 6f 20 6d 65 61  ight of zero mea
488ac 6e 73 20 61 6c 6c 20 64 61 74 61 20 69 73 20 73  ns all data is s
488ad 74 6f 72 65 64 20 6f 6e 0a 20 20 2a 2a 20 74 68  tored on.  ** th
488ae 65 20 72 6f 6f 74 20 6e 6f 64 65 2e 20 41 20 68  e root node. A h
488af 65 69 67 68 74 20 6f 66 20 6f 6e 65 20 6d 65 61  eight of one mea
488b0 6e 73 20 74 68 65 20 63 68 69 6c 64 72 65 6e 20  ns the children 
488b1 6f 66 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65  of the root node
488b2 0a 20 20 2a 2a 20 61 72 65 20 74 68 65 20 6c 65  .  ** are the le
488b3 61 76 65 73 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e  aves, and so on.
488b4 20 49 66 20 74 68 65 20 64 65 70 74 68 20 61 73   If the depth as
488b5 20 73 70 65 63 69 66 69 65 64 20 6f 6e 20 74 68   specified on th
488b6 65 20 72 6f 6f 74 20 6e 6f 64 65 0a 20 20 2a 2a  e root node.  **
488b7 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
488b8 20 52 54 52 45 45 5f 4d 41 58 5f 44 45 50 54 48   RTREE_MAX_DEPTH
488b9 2c 20 74 68 65 20 72 2d 74 72 65 65 20 73 74 72  , the r-tree str
488ba 75 63 74 75 72 65 20 6d 75 73 74 20 62 65 20 63  ucture must be c
488bb 6f 72 72 75 70 74 2e 0a 20 20 2a 2f 0a 20 20 69  orrupt..  */.  i
488bc 66 28 20 70 4e 6f 64 65 20 26 26 20 69 4e 6f 64  f( pNode && iNod
488bd 65 3d 3d 31 20 29 7b 0a 20 20 20 20 70 52 74 72  e==1 ){.    pRtr
488be 65 65 2d 3e 69 44 65 70 74 68 20 3d 20 72 65 61  ee->iDepth = rea
488bf 64 49 6e 74 31 36 28 70 4e 6f 64 65 2d 3e 7a 44  dInt16(pNode->zD
488c0 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 70 52  ata);.    if( pR
488c1 74 72 65 65 2d 3e 69 44 65 70 74 68 3e 52 54 52  tree->iDepth>RTR
488c2 45 45 5f 4d 41 58 5f 44 45 50 54 48 20 29 7b 0a  EE_MAX_DEPTH ){.
488c3 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
488c4 45 5f 43 4f 52 52 55 50 54 5f 56 54 41 42 3b 0a  E_CORRUPT_VTAB;.
488c5 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
488c6 49 66 20 6e 6f 20 65 72 72 6f 72 20 68 61 73 20  If no error has 
488c7 6f 63 63 75 72 72 65 64 20 73 6f 20 66 61 72 2c  occurred so far,
488c8 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 22 6e   check if the "n
488c9 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
488ca 22 0a 20 20 2a 2a 20 66 69 65 6c 64 20 6f 6e 20  ".  ** field on 
488cb 74 68 65 20 6e 6f 64 65 20 69 73 20 74 6f 6f 20  the node is too 
488cc 6c 61 72 67 65 2e 20 49 66 20 73 6f 2c 20 73 65  large. If so, se
488cd 74 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64  t the return cod
488ce 65 20 74 6f 20 0a 20 20 2a 2a 20 53 51 4c 49 54  e to .  ** SQLIT
488cf 45 5f 43 4f 52 52 55 50 54 5f 56 54 41 42 2e 0a  E_CORRUPT_VTAB..
488d0 20 20 2a 2f 0a 20 20 69 66 28 20 70 4e 6f 64 65    */.  if( pNode
488d1 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   && rc==SQLITE_O
488d2 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 4e 43 45  K ){.    if( NCE
488d3 4c 4c 28 70 4e 6f 64 65 29 3e 28 28 70 52 74 72  LL(pNode)>((pRtr
488d4 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d 34 29  ee->iNodeSize-4)
488d5 2f 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50  /pRtree->nBytesP
488d6 65 72 43 65 6c 6c 29 20 29 7b 0a 20 20 20 20 20  erCell) ){.     
488d7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
488d8 52 55 50 54 5f 56 54 41 42 3b 0a 20 20 20 20 7d  RUPT_VTAB;.    }
488d9 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
488da 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
488db 20 69 66 28 20 70 4e 6f 64 65 21 3d 30 20 29 7b   if( pNode!=0 ){
488dc 0a 20 20 20 20 20 20 6e 6f 64 65 48 61 73 68 49  .      nodeHashI
488dd 6e 73 65 72 74 28 70 52 74 72 65 65 2c 20 70 4e  nsert(pRtree, pN
488de 6f 64 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ode);.    }else{
488df 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
488e0 54 45 5f 43 4f 52 52 55 50 54 5f 56 54 41 42 3b  TE_CORRUPT_VTAB;
488e1 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4e 6f  .    }.    *ppNo
488e2 64 65 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 7d 65  de = pNode;.  }e
488e3 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
488e4 5f 66 72 65 65 28 70 4e 6f 64 65 29 3b 0a 20 20  _free(pNode);.  
488e5 20 20 2a 70 70 4e 6f 64 65 20 3d 20 30 3b 0a 20    *ppNode = 0;. 
488e6 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
488e7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 76 65 72 77 72  .}../*.** Overwr
488e8 69 74 65 20 63 65 6c 6c 20 69 43 65 6c 6c 20 6f  ite cell iCell o
488e9 66 20 6e 6f 64 65 20 70 4e 6f 64 65 20 77 69 74  f node pNode wit
488ea 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  h the contents o
488eb 66 20 70 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74  f pCell..*/.stat
488ec 69 63 20 76 6f 69 64 20 6e 6f 64 65 4f 76 65 72  ic void nodeOver
488ed 77 72 69 74 65 43 65 6c 6c 28 0a 20 20 52 74 72  writeCell(.  Rtr
488ee 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52  ee *pRtree, .  R
488ef 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
488f0 20 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a    .  RtreeCell *
488f1 70 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 69 43  pCell, .  int iC
488f2 65 6c 6c 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b  ell.){.  int ii;
488f3 0a 20 20 75 38 20 2a 70 20 3d 20 26 70 4e 6f 64  .  u8 *p = &pNod
488f4 65 2d 3e 7a 44 61 74 61 5b 34 20 2b 20 70 52 74  e->zData[4 + pRt
488f5 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65  ree->nBytesPerCe
488f6 6c 6c 2a 69 43 65 6c 6c 5d 3b 0a 20 20 70 20 2b  ll*iCell];.  p +
488f7 3d 20 77 72 69 74 65 49 6e 74 36 34 28 70 2c 20  = writeInt64(p, 
488f8 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 29 3b 0a  pCell->iRowid);.
488f9 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28    for(ii=0; ii<(
488fa 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b  pRtree->nDim*2);
488fb 20 69 69 2b 2b 29 7b 0a 20 20 20 20 70 20 2b 3d   ii++){.    p +=
488fc 20 77 72 69 74 65 43 6f 6f 72 64 28 70 2c 20 26   writeCoord(p, &
488fd 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b 69 69  pCell->aCoord[ii
488fe 5d 29 3b 0a 20 20 7d 0a 20 20 70 4e 6f 64 65 2d  ]);.  }.  pNode-
488ff 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 7d 0a  >isDirty = 1;.}.
48900 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 63 65  ./*.** Remove ce
48901 6c 6c 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68  ll the cell with
48902 20 69 6e 64 65 78 20 69 43 65 6c 6c 20 66 72 6f   index iCell fro
48903 6d 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 0a 2a 2f  m node pNode..*/
48904 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64  .static void nod
48905 65 44 65 6c 65 74 65 43 65 6c 6c 28 52 74 72 65  eDeleteCell(Rtre
48906 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
48907 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 69 6e 74  Node *pNode, int
48908 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70   iCell){.  u8 *p
48909 44 73 74 20 3d 20 26 70 4e 6f 64 65 2d 3e 7a 44  Dst = &pNode->zD
4890a 61 74 61 5b 34 20 2b 20 70 52 74 72 65 65 2d 3e  ata[4 + pRtree->
4890b 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43  nBytesPerCell*iC
4890c 65 6c 6c 5d 3b 0a 20 20 75 38 20 2a 70 53 72 63  ell];.  u8 *pSrc
4890d 20 3d 20 26 70 44 73 74 5b 70 52 74 72 65 65 2d   = &pDst[pRtree-
4890e 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 5d 3b  >nBytesPerCell];
4890f 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28  .  int nByte = (
48910 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 20 2d 20 69  NCELL(pNode) - i
48911 43 65 6c 6c 20 2d 20 31 29 20 2a 20 70 52 74 72  Cell - 1) * pRtr
48912 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c  ee->nBytesPerCel
48913 6c 3b 0a 20 20 6d 65 6d 6d 6f 76 65 28 70 44 73  l;.  memmove(pDs
48914 74 2c 20 70 53 72 63 2c 20 6e 42 79 74 65 29 3b  t, pSrc, nByte);
48915 0a 20 20 77 72 69 74 65 49 6e 74 31 36 28 26 70  .  writeInt16(&p
48916 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20  Node->zData[2], 
48917 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 2d 31 29 3b  NCELL(pNode)-1);
48918 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74  .  pNode->isDirt
48919 79 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  y = 1;.}../*.** 
4891a 49 6e 73 65 72 74 20 74 68 65 20 63 6f 6e 74 65  Insert the conte
4891b 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70 43 65 6c  nts of cell pCel
4891c 6c 20 69 6e 74 6f 20 6e 6f 64 65 20 70 4e 6f 64  l into node pNod
4891d 65 2e 20 49 66 20 74 68 65 20 69 6e 73 65 72 74  e. If the insert
4891e 0a 2a 2a 20 69 73 20 73 75 63 63 65 73 73 66 75  .** is successfu
4891f 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  l, return SQLITE
48920 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  _OK..**.** If th
48921 65 72 65 20 69 73 20 6e 6f 74 20 65 6e 6f 75 67  ere is not enoug
48922 68 20 66 72 65 65 20 73 70 61 63 65 20 69 6e 20  h free space in 
48923 70 4e 6f 64 65 2c 20 72 65 74 75 72 6e 20 53 51  pNode, return SQ
48924 4c 49 54 45 5f 46 55 4c 4c 2e 0a 2a 2f 0a 73 74  LITE_FULL..*/.st
48925 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65 49 6e 73  atic int.nodeIns
48926 65 72 74 43 65 6c 6c 28 0a 20 20 52 74 72 65 65  ertCell(.  Rtree
48927 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72   *pRtree, .  Rtr
48928 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a  eeNode *pNode, .
48929 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65    RtreeCell *pCe
4892a 6c 6c 20 0a 29 7b 0a 20 20 69 6e 74 20 6e 43 65  ll .){.  int nCe
4892b 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
4892c 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
4892d 74 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c  t number of cell
4892e 73 20 69 6e 20 70 4e 6f 64 65 20 2a 2f 0a 20 20  s in pNode */.  
4892f 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 3b 20 20 20  int nMaxCell;   
48930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
48931 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   Maximum number 
48932 6f 66 20 63 65 6c 6c 73 20 66 6f 72 20 70 4e 6f  of cells for pNo
48933 64 65 20 2a 2f 0a 0a 20 20 6e 4d 61 78 43 65 6c  de */..  nMaxCel
48934 6c 20 3d 20 28 70 52 74 72 65 65 2d 3e 69 4e 6f  l = (pRtree->iNo
48935 64 65 53 69 7a 65 2d 34 29 2f 70 52 74 72 65 65  deSize-4)/pRtree
48936 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b  ->nBytesPerCell;
48937 0a 20 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c  .  nCell = NCELL
48938 28 70 4e 6f 64 65 29 3b 0a 0a 20 20 61 73 73 65  (pNode);..  asse
48939 72 74 28 20 6e 43 65 6c 6c 3c 3d 6e 4d 61 78 43  rt( nCell<=nMaxC
4893a 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20 6e 43 65  ell );.  if( nCe
4893b 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 20 29 7b 0a 20  ll<nMaxCell ){. 
4893c 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74 65     nodeOverwrite
4893d 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f  Cell(pRtree, pNo
4893e 64 65 2c 20 70 43 65 6c 6c 2c 20 6e 43 65 6c 6c  de, pCell, nCell
4893f 29 3b 0a 20 20 20 20 77 72 69 74 65 49 6e 74 31  );.    writeInt1
48940 36 28 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b  6(&pNode->zData[
48941 32 5d 2c 20 6e 43 65 6c 6c 2b 31 29 3b 0a 20 20  2], nCell+1);.  
48942 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79    pNode->isDirty
48943 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 65 74   = 1;.  }..  ret
48944 75 72 6e 20 28 6e 43 65 6c 6c 3d 3d 6e 4d 61 78  urn (nCell==nMax
48945 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Cell);.}../*.** 
48946 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20 64  If the node is d
48947 69 72 74 79 2c 20 77 72 69 74 65 20 69 74 20 6f  irty, write it o
48948 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ut to the databa
48949 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  se..*/.static in
4894a 74 0a 6e 6f 64 65 57 72 69 74 65 28 52 74 72 65  t.nodeWrite(Rtre
4894b 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
4894c 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20  Node *pNode){.  
4894d 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
4894e 4f 4b 3b 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d  OK;.  if( pNode-
4894f 3e 69 73 44 69 72 74 79 20 29 7b 0a 20 20 20 20  >isDirty ){.    
48950 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 20  sqlite3_stmt *p 
48951 3d 20 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  = pRtree->pWrite
48952 4e 6f 64 65 3b 0a 20 20 20 20 69 66 28 20 70 4e  Node;.    if( pN
48953 6f 64 65 2d 3e 69 4e 6f 64 65 20 29 7b 0a 20 20  ode->iNode ){.  
48954 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
48955 5f 69 6e 74 36 34 28 70 2c 20 31 2c 20 70 4e 6f  _int64(p, 1, pNo
48956 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  de->iNode);.    
48957 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
48958 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70  ite3_bind_null(p
48959 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 1);.    }.    
4895a 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
4895b 62 28 70 2c 20 32 2c 20 70 4e 6f 64 65 2d 3e 7a  b(p, 2, pNode->z
4895c 44 61 74 61 2c 20 70 52 74 72 65 65 2d 3e 69 4e  Data, pRtree->iN
4895d 6f 64 65 53 69 7a 65 2c 20 53 51 4c 49 54 45 5f  odeSize, SQLITE_
4895e 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c  STATIC);.    sql
4895f 69 74 65 33 5f 73 74 65 70 28 70 29 3b 0a 20 20  ite3_step(p);.  
48960 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79    pNode->isDirty
48961 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 73   = 0;.    rc = s
48962 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 29 3b  qlite3_reset(p);
48963 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e  .    if( pNode->
48964 69 4e 6f 64 65 3d 3d 30 20 26 26 20 72 63 3d 3d  iNode==0 && rc==
48965 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
48966 20 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20     pNode->iNode 
48967 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  = sqlite3_last_i
48968 6e 73 65 72 74 5f 72 6f 77 69 64 28 70 52 74 72  nsert_rowid(pRtr
48969 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 6e  ee->db);.      n
4896a 6f 64 65 48 61 73 68 49 6e 73 65 72 74 28 70 52  odeHashInsert(pR
4896b 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20  tree, pNode);.  
4896c 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
4896d 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
4896e 6c 65 61 73 65 20 61 20 72 65 66 65 72 65 6e 63  lease a referenc
4896f 65 20 74 6f 20 61 20 6e 6f 64 65 2e 20 49 66 20  e to a node. If 
48970 74 68 65 20 6e 6f 64 65 20 69 73 20 64 69 72 74  the node is dirt
48971 79 20 61 6e 64 20 74 68 65 20 72 65 66 65 72 65  y and the refere
48972 6e 63 65 0a 2a 2a 20 63 6f 75 6e 74 20 64 72 6f  nce.** count dro
48973 70 73 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 20  ps to zero, the 
48974 6e 6f 64 65 20 64 61 74 61 20 69 73 20 77 72 69  node data is wri
48975 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61  tten to the data
48976 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  base..*/.static 
48977 69 6e 74 0a 6e 6f 64 65 52 65 6c 65 61 73 65 28  int.nodeRelease(
48978 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
48979 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29  treeNode *pNode)
4897a 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
4897b 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 4e  ITE_OK;.  if( pN
4897c 6f 64 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ode ){.    asser
4897d 74 28 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 3e 30  t( pNode->nRef>0
4897e 20 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 6e   );.    pNode->n
4897f 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70  Ref--;.    if( p
48980 4e 6f 64 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b  Node->nRef==0 ){
48981 0a 20 20 20 20 20 20 69 66 28 20 70 4e 6f 64 65  .      if( pNode
48982 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20  ->iNode==1 ){.  
48983 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e 69 44        pRtree->iD
48984 65 70 74 68 20 3d 20 2d 31 3b 0a 20 20 20 20 20  epth = -1;.     
48985 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4e 6f   }.      if( pNo
48986 64 65 2d 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20  de->pParent ){. 
48987 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64 65         rc = node
48988 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
48989 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 29 3b  pNode->pParent);
4898a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
4898b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
4898c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
4898d 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74 72 65   nodeWrite(pRtre
4898e 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20  e, pNode);.     
4898f 20 7d 0a 20 20 20 20 20 20 6e 6f 64 65 48 61 73   }.      nodeHas
48990 68 44 65 6c 65 74 65 28 70 52 74 72 65 65 2c 20  hDelete(pRtree, 
48991 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 73 71  pNode);.      sq
48992 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 6f 64 65  lite3_free(pNode
48993 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
48994 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
48995 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 36 34  ** Return the 64
48996 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c  -bit integer val
48997 75 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ue associated wi
48998 74 68 20 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66  th cell iCell of
48999 0a 2a 2a 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20  .** node pNode. 
4899a 49 66 20 70 4e 6f 64 65 20 69 73 20 61 20 6c 65  If pNode is a le
4899b 61 66 20 6e 6f 64 65 2c 20 74 68 69 73 20 69 73  af node, this is
4899c 20 61 20 72 6f 77 69 64 2e 20 49 66 20 69 74 20   a rowid. If it 
4899d 69 73 0a 2a 2a 20 61 6e 20 69 6e 74 65 72 6e 61  is.** an interna
4899e 6c 20 6e 6f 64 65 2c 20 74 68 65 6e 20 74 68 65  l node, then the
4899f 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20   64-bit integer 
489a0 69 73 20 61 20 63 68 69 6c 64 20 70 61 67 65 20  is a child page 
489a1 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
489a2 63 20 69 36 34 20 6e 6f 64 65 47 65 74 52 6f 77  c i64 nodeGetRow
489a3 69 64 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  id(.  Rtree *pRt
489a4 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64  ree, .  RtreeNod
489a5 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74  e *pNode, .  int
489a6 20 69 43 65 6c 6c 0a 29 7b 0a 20 20 61 73 73 65   iCell.){.  asse
489a7 72 74 28 20 69 43 65 6c 6c 3c 4e 43 45 4c 4c 28  rt( iCell<NCELL(
489a8 70 4e 6f 64 65 29 20 29 3b 0a 20 20 72 65 74 75  pNode) );.  retu
489a9 72 6e 20 72 65 61 64 49 6e 74 36 34 28 26 70 4e  rn readInt64(&pN
489aa 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b 20 70  ode->zData[4 + p
489ab 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72  Rtree->nBytesPer
489ac 43 65 6c 6c 2a 69 43 65 6c 6c 5d 29 3b 0a 7d 0a  Cell*iCell]);.}.
489ad 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 63 6f  ./*.** Return co
489ae 6f 72 64 69 6e 61 74 65 20 69 43 6f 6f 72 64 20  ordinate iCoord 
489af 66 72 6f 6d 20 63 65 6c 6c 20 69 43 65 6c 6c 20  from cell iCell 
489b0 69 6e 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 0a 2a  in node pNode..*
489b1 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f  /.static void no
489b2 64 65 47 65 74 43 6f 6f 72 64 28 0a 20 20 52 74  deGetCoord(.  Rt
489b3 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20  ree *pRtree, .  
489b4 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
489b5 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 0a  , .  int iCell,.
489b6 20 20 69 6e 74 20 69 43 6f 6f 72 64 2c 0a 20 20    int iCoord,.  
489b7 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43 6f 6f  RtreeCoord *pCoo
489b8 72 64 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  rd           /* 
489b9 53 70 61 63 65 20 74 6f 20 77 72 69 74 65 20 72  Space to write r
489ba 65 73 75 6c 74 20 74 6f 20 2a 2f 0a 29 7b 0a 20  esult to */.){. 
489bb 20 72 65 61 64 43 6f 6f 72 64 28 26 70 4e 6f 64   readCoord(&pNod
489bc 65 2d 3e 7a 44 61 74 61 5b 31 32 20 2b 20 70 52  e->zData[12 + pR
489bd 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43  tree->nBytesPerC
489be 65 6c 6c 2a 69 43 65 6c 6c 20 2b 20 34 2a 69 43  ell*iCell + 4*iC
489bf 6f 6f 72 64 5d 2c 20 70 43 6f 6f 72 64 29 3b 0a  oord], pCoord);.
489c0 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72 69 61  }../*.** Deseria
489c1 6c 69 7a 65 20 63 65 6c 6c 20 69 43 65 6c 6c 20  lize cell iCell 
489c2 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 50  of node pNode. P
489c3 6f 70 75 6c 61 74 65 20 74 68 65 20 73 74 72 75  opulate the stru
489c4 63 74 75 72 65 20 70 6f 69 6e 74 65 64 0a 2a 2a  cture pointed.**
489c5 20 74 6f 20 62 79 20 70 43 65 6c 6c 20 77 69 74   to by pCell wit
489c6 68 20 74 68 65 20 72 65 73 75 6c 74 73 2e 0a 2a  h the results..*
489c7 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f  /.static void no
489c8 64 65 47 65 74 43 65 6c 6c 28 0a 20 20 52 74 72  deGetCell(.  Rtr
489c9 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52  ee *pRtree, .  R
489ca 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
489cb 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 0a 20   .  int iCell,. 
489cc 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
489cd 6c 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  l.){.  int ii;. 
489ce 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 20 3d   pCell->iRowid =
489cf 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52   nodeGetRowid(pR
489d0 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65  tree, pNode, iCe
489d1 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b  ll);.  for(ii=0;
489d2 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d   ii<pRtree->nDim
489d3 2a 32 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 6e  *2; ii++){.    n
489d4 6f 64 65 47 65 74 43 6f 6f 72 64 28 70 52 74 72  odeGetCoord(pRtr
489d5 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65 6c 6c  ee, pNode, iCell
489d6 2c 20 69 69 2c 20 26 70 43 65 6c 6c 2d 3e 61 43  , ii, &pCell->aC
489d7 6f 6f 72 64 5b 69 69 5d 29 3b 0a 20 20 7d 0a 7d  oord[ii]);.  }.}
489d8 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  .../* Forward de
489d9 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20 74 68  claration for th
489da 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  e function that 
489db 64 6f 65 73 20 74 68 65 20 77 6f 72 6b 20 6f 66  does the work of
489dc 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20  .** the virtual 
489dd 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43 72  table module xCr
489de 65 61 74 65 28 29 20 61 6e 64 20 78 43 6f 6e 6e  eate() and xConn
489df 65 63 74 28 29 20 6d 65 74 68 6f 64 73 2e 0a 2a  ect() methods..*
489e0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
489e1 65 65 49 6e 69 74 28 0a 20 20 73 71 6c 69 74 65  eeInit(.  sqlite
489e2 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69 6e 74  3 *, void *, int
489e3 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f  , const char *co
489e4 6e 73 74 2a 2c 20 73 71 6c 69 74 65 33 5f 76 74  nst*, sqlite3_vt
489e5 61 62 20 2a 2a 2c 20 63 68 61 72 20 2a 2a 2c 20  ab **, char **, 
489e6 69 6e 74 0a 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 52  int.);../* .** R
489e7 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
489e8 6c 65 20 6d 6f 64 75 6c 65 20 78 43 72 65 61 74  le module xCreat
489e9 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61  e method..*/.sta
489ea 74 69 63 20 69 6e 74 20 72 74 72 65 65 43 72 65  tic int rtreeCre
489eb 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ate(.  sqlite3 *
489ec 64 62 2c 0a 20 20 76 6f 69 64 20 2a 70 41 75 78  db,.  void *pAux
489ed 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f  ,.  int argc, co
489ee 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a  nst char *const*
489ef 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argv,.  sqlite3_
489f0 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c 0a 20  vtab **ppVtab,. 
489f1 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b   char **pzErr.){
489f2 0a 20 20 72 65 74 75 72 6e 20 72 74 72 65 65 49  .  return rtreeI
489f3 6e 69 74 28 64 62 2c 20 70 41 75 78 2c 20 61 72  nit(db, pAux, ar
489f4 67 63 2c 20 61 72 67 76 2c 20 70 70 56 74 61 62  gc, argv, ppVtab
489f5 2c 20 70 7a 45 72 72 2c 20 31 29 3b 0a 7d 0a 0a  , pzErr, 1);.}..
489f6 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72  /* .** Rtree vir
489f7 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
489f8 65 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74 68 6f  e xConnect metho
489f9 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
489fa 20 72 74 72 65 65 43 6f 6e 6e 65 63 74 28 0a 20   rtreeConnect(. 
489fb 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20   sqlite3 *db,.  
489fc 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 69 6e  void *pAux,.  in
489fd 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68  t argc, const ch
489fe 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a  ar *const*argv,.
489ff 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
48a00 2a 70 70 56 74 61 62 2c 0a 20 20 63 68 61 72 20  *ppVtab,.  char 
48a01 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20 72 65 74  **pzErr.){.  ret
48a02 75 72 6e 20 72 74 72 65 65 49 6e 69 74 28 64 62  urn rtreeInit(db
48a03 2c 20 70 41 75 78 2c 20 61 72 67 63 2c 20 61 72  , pAux, argc, ar
48a04 67 76 2c 20 70 70 56 74 61 62 2c 20 70 7a 45 72  gv, ppVtab, pzEr
48a05 72 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  r, 0);.}../*.** 
48a06 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 2d  Increment the r-
48a07 74 72 65 65 20 72 65 66 65 72 65 6e 63 65 20 63  tree reference c
48a08 6f 75 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ount..*/.static 
48a09 76 6f 69 64 20 72 74 72 65 65 52 65 66 65 72 65  void rtreeRefere
48a0a 6e 63 65 28 52 74 72 65 65 20 2a 70 52 74 72 65  nce(Rtree *pRtre
48a0b 65 29 7b 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42  e){.  pRtree->nB
48a0c 75 73 79 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  usy++;.}../*.** 
48a0d 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 2d  Decrement the r-
48a0e 74 72 65 65 20 72 65 66 65 72 65 6e 63 65 20 63  tree reference c
48a0f 6f 75 6e 74 2e 20 57 68 65 6e 20 74 68 65 20 72  ount. When the r
48a10 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 72  eference count r
48a11 65 61 63 68 65 73 0a 2a 2a 20 7a 65 72 6f 20 74  eaches.** zero t
48a12 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  he structure is 
48a13 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 73 74 61 74  deleted..*/.stat
48a14 69 63 20 76 6f 69 64 20 72 74 72 65 65 52 65 6c  ic void rtreeRel
48a15 65 61 73 65 28 52 74 72 65 65 20 2a 70 52 74 72  ease(Rtree *pRtr
48a16 65 65 29 7b 0a 20 20 70 52 74 72 65 65 2d 3e 6e  ee){.  pRtree->n
48a17 42 75 73 79 2d 2d 3b 0a 20 20 69 66 28 20 70 52  Busy--;.  if( pR
48a18 74 72 65 65 2d 3e 6e 42 75 73 79 3d 3d 30 20 29  tree->nBusy==0 )
48a19 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  {.    sqlite3_fi
48a1a 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
48a1b 52 65 61 64 4e 6f 64 65 29 3b 0a 20 20 20 20 73  ReadNode);.    s
48a1c 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
48a1d 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f  pRtree->pWriteNo
48a1e 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  de);.    sqlite3
48a1f 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65  _finalize(pRtree
48a20 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29 3b 0a  ->pDeleteNode);.
48a21 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
48a22 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 52 65  lize(pRtree->pRe
48a23 61 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71  adRowid);.    sq
48a24 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70  lite3_finalize(p
48a25 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77  Rtree->pWriteRow
48a26 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  id);.    sqlite3
48a27 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65  _finalize(pRtree
48a28 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64 29 3b  ->pDeleteRowid);
48a29 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e  .    sqlite3_fin
48a2a 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 52  alize(pRtree->pR
48a2b 65 61 64 50 61 72 65 6e 74 29 3b 0a 20 20 20 20  eadParent);.    
48a2c 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
48a2d 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50  (pRtree->pWriteP
48a2e 61 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69  arent);.    sqli
48a2f 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74  te3_finalize(pRt
48a30 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72 65  ree->pDeletePare
48a31 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nt);.    sqlite3
48a32 5f 66 72 65 65 28 70 52 74 72 65 65 29 3b 0a 20  _free(pRtree);. 
48a33 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72   }.}../* .** Rtr
48a34 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ee virtual table
48a35 20 6d 6f 64 75 6c 65 20 78 44 69 73 63 6f 6e 6e   module xDisconn
48a36 65 63 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  ect method..*/.s
48a37 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 44  tatic int rtreeD
48a38 69 73 63 6f 6e 6e 65 63 74 28 73 71 6c 69 74 65  isconnect(sqlite
48a39 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a  3_vtab *pVtab){.
48a3a 20 20 72 74 72 65 65 52 65 6c 65 61 73 65 28 28    rtreeRelease((
48a3b 52 74 72 65 65 20 2a 29 70 56 74 61 62 29 3b 0a  Rtree *)pVtab);.
48a3c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
48a3d 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  OK;.}../* .** Rt
48a3e 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
48a3f 65 20 6d 6f 64 75 6c 65 20 78 44 65 73 74 72 6f  e module xDestro
48a40 79 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61  y method..*/.sta
48a41 74 69 63 20 69 6e 74 20 72 74 72 65 65 44 65 73  tic int rtreeDes
48a42 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 76 74 61  troy(sqlite3_vta
48a43 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 52 74 72  b *pVtab){.  Rtr
48a44 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74  ee *pRtree = (Rt
48a45 72 65 65 20 2a 29 70 56 74 61 62 3b 0a 20 20 69  ree *)pVtab;.  i
48a46 6e 74 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a  nt rc;.  char *z
48a47 43 72 65 61 74 65 20 3d 20 73 71 6c 69 74 65 33  Create = sqlite3
48a48 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 22 44  _mprintf(.    "D
48a49 52 4f 50 20 54 41 42 4c 45 20 27 25 71 27 2e 27  ROP TABLE '%q'.'
48a4a 25 71 5f 6e 6f 64 65 27 3b 22 0a 20 20 20 20 22  %q_node';".    "
48a4b 44 52 4f 50 20 54 41 42 4c 45 20 27 25 71 27 2e  DROP TABLE '%q'.
48a4c 27 25 71 5f 72 6f 77 69 64 27 3b 22 0a 20 20 20  '%q_rowid';".   
48a4d 20 22 44 52 4f 50 20 54 41 42 4c 45 20 27 25 71   "DROP TABLE '%q
48a4e 27 2e 27 25 71 5f 70 61 72 65 6e 74 27 3b 22 2c  '.'%q_parent';",
48a4f 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 7a 44 62  .    pRtree->zDb
48a50 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c  , pRtree->zName,
48a51 20 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 7a 44   .    pRtree->zD
48a52 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65  b, pRtree->zName
48a53 2c 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 7a 44  ,.    pRtree->zD
48a54 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65  b, pRtree->zName
48a55 0a 20 20 29 3b 0a 20 20 69 66 28 20 21 7a 43 72  .  );.  if( !zCr
48a56 65 61 74 65 20 29 7b 0a 20 20 20 20 72 63 20 3d  eate ){.    rc =
48a57 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
48a58 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
48a59 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 70 52   sqlite3_exec(pR
48a5a 74 72 65 65 2d 3e 64 62 2c 20 7a 43 72 65 61 74  tree->db, zCreat
48a5b 65 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20  e, 0, 0, 0);.   
48a5c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43   sqlite3_free(zC
48a5d 72 65 61 74 65 29 3b 0a 20 20 7d 0a 20 20 69 66  reate);.  }.  if
48a5e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
48a5f 29 7b 0a 20 20 20 20 72 74 72 65 65 52 65 6c 65  ){.    rtreeRele
48a60 61 73 65 28 70 52 74 72 65 65 29 3b 0a 20 20 7d  ase(pRtree);.  }
48a61 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
48a62 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
48a63 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
48a64 75 6c 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64  ule xOpen method
48a65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
48a66 72 74 72 65 65 4f 70 65 6e 28 73 71 6c 69 74 65  rtreeOpen(sqlite
48a67 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73  3_vtab *pVTab, s
48a68 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
48a69 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29 7b 0a  or **ppCursor){.
48a6a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
48a6b 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 52 74 72 65 65  E_NOMEM;.  Rtree
48a6c 43 75 72 73 6f 72 20 2a 70 43 73 72 3b 0a 0a 20  Cursor *pCsr;.. 
48a6d 20 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75   pCsr = (RtreeCu
48a6e 72 73 6f 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d  rsor *)sqlite3_m
48a6f 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72  alloc(sizeof(Rtr
48a70 65 65 43 75 72 73 6f 72 29 29 3b 0a 20 20 69 66  eeCursor));.  if
48a71 28 20 70 43 73 72 20 29 7b 0a 20 20 20 20 6d 65  ( pCsr ){.    me
48a72 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20 73 69  mset(pCsr, 0, si
48a73 7a 65 6f 66 28 52 74 72 65 65 43 75 72 73 6f 72  zeof(RtreeCursor
48a74 29 29 3b 0a 20 20 20 20 70 43 73 72 2d 3e 62 61  ));.    pCsr->ba
48a75 73 65 2e 70 56 74 61 62 20 3d 20 70 56 54 61 62  se.pVtab = pVTab
48a76 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
48a77 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 2a 70 70 43  E_OK;.  }.  *ppC
48a78 75 72 73 6f 72 20 3d 20 28 73 71 6c 69 74 65 33  ursor = (sqlite3
48a79 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 29 70  _vtab_cursor *)p
48a7a 43 73 72 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72  Csr;..  return r
48a7b 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  c;.}.../*.** Fre
48a7c 65 20 74 68 65 20 52 74 72 65 65 43 75 72 73 6f  e the RtreeCurso
48a7d 72 2e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20  r.aConstraint[] 
48a7e 61 72 72 61 79 20 61 6e 64 20 69 74 73 20 63 6f  array and its co
48a7f 6e 74 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69  ntents..*/.stati
48a80 63 20 76 6f 69 64 20 66 72 65 65 43 75 72 73 6f  c void freeCurso
48a81 72 43 6f 6e 73 74 72 61 69 6e 74 73 28 52 74 72  rConstraints(Rtr
48a82 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 29 7b  eeCursor *pCsr){
48a83 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 61 43 6f  .  if( pCsr->aCo
48a84 6e 73 74 72 61 69 6e 74 20 29 7b 0a 20 20 20 20  nstraint ){.    
48a85 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
48a86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
48a87 20 55 73 65 64 20 74 6f 20 69 74 65 72 61 74 65   Used to iterate
48a88 20 74 68 72 6f 75 67 68 20 63 6f 6e 73 74 72 61   through constra
48a89 69 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 20  int array */.   
48a8a 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 73 72   for(i=0; i<pCsr
48a8b 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69  ->nConstraint; i
48a8c 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
48a8d 65 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72  e3_rtree_geometr
48a8e 79 20 2a 70 47 65 6f 6d 20 3d 20 70 43 73 72 2d  y *pGeom = pCsr-
48a8f 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e  >aConstraint[i].
48a90 70 47 65 6f 6d 3b 0a 20 20 20 20 20 20 69 66 28  pGeom;.      if(
48a91 20 70 47 65 6f 6d 20 29 7b 0a 20 20 20 20 20 20   pGeom ){.      
48a92 20 20 69 66 28 20 70 47 65 6f 6d 2d 3e 78 44 65    if( pGeom->xDe
48a93 6c 55 73 65 72 20 29 20 70 47 65 6f 6d 2d 3e 78  lUser ) pGeom->x
48a94 44 65 6c 55 73 65 72 28 70 47 65 6f 6d 2d 3e 70  DelUser(pGeom->p
48a95 55 73 65 72 29 3b 0a 20 20 20 20 20 20 20 20 73  User);.        s
48a96 71 6c 69 74 65 33 5f 66 72 65 65 28 70 47 65 6f  qlite3_free(pGeo
48a97 6d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  m);.      }.    
48a98 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
48a99 65 65 28 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72  ee(pCsr->aConstr
48a9a 61 69 6e 74 29 3b 0a 20 20 20 20 70 43 73 72 2d  aint);.    pCsr-
48a9b 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 30  >aConstraint = 0
48a9c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  ;.  }.}../* .** 
48a9d 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61  Rtree virtual ta
48a9e 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43 6c 6f 73  ble module xClos
48a9f 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61  e method..*/.sta
48aa0 74 69 63 20 69 6e 74 20 72 74 72 65 65 43 6c 6f  tic int rtreeClo
48aa1 73 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  se(sqlite3_vtab_
48aa2 63 75 72 73 6f 72 20 2a 63 75 72 29 7b 0a 20 20  cursor *cur){.  
48aa3 52 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20  Rtree *pRtree = 
48aa4 28 52 74 72 65 65 20 2a 29 28 63 75 72 2d 3e 70  (Rtree *)(cur->p
48aa5 56 74 61 62 29 3b 0a 20 20 69 6e 74 20 72 63 3b  Vtab);.  int rc;
48aa6 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a  .  RtreeCursor *
48aa7 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75 72  pCsr = (RtreeCur
48aa8 73 6f 72 20 2a 29 63 75 72 3b 0a 20 20 66 72 65  sor *)cur;.  fre
48aa9 65 43 75 72 73 6f 72 43 6f 6e 73 74 72 61 69 6e  eCursorConstrain
48aaa 74 73 28 70 43 73 72 29 3b 0a 20 20 72 63 20 3d  ts(pCsr);.  rc =
48aab 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
48aac 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65  ree, pCsr->pNode
48aad 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
48aae 65 28 70 43 73 72 29 3b 0a 20 20 72 65 74 75 72  e(pCsr);.  retur
48aaf 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
48ab0 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
48ab1 6c 65 20 6d 6f 64 75 6c 65 20 78 45 6f 66 20 6d  le module xEof m
48ab2 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  ethod..**.** Ret
48ab3 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20  urn non-zero if 
48ab4 74 68 65 20 63 75 72 73 6f 72 20 64 6f 65 73 20  the cursor does 
48ab5 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f  not currently po
48ab6 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 0a  int to a valid .
48ab7 2a 2a 20 72 65 63 6f 72 64 20 28 69 2e 65 20 69  ** record (i.e i
48ab8 66 20 74 68 65 20 73 63 61 6e 20 68 61 73 20 66  f the scan has f
48ab9 69 6e 69 73 68 65 64 29 2c 20 6f 72 20 7a 65 72  inished), or zer
48aba 6f 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a  o otherwise..*/.
48abb 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
48abc 45 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62  Eof(sqlite3_vtab
48abd 5f 63 75 72 73 6f 72 20 2a 63 75 72 29 7b 0a 20  _cursor *cur){. 
48abe 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43   RtreeCursor *pC
48abf 73 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f  sr = (RtreeCurso
48ac0 72 20 2a 29 63 75 72 3b 0a 20 20 72 65 74 75 72  r *)cur;.  retur
48ac1 6e 20 28 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d  n (pCsr->pNode==
48ac2 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  0);.}../*.** The
48ac3 20 72 2d 74 72 65 65 20 63 6f 6e 73 74 72 61 69   r-tree constrai
48ac4 6e 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65  nt passed as the
48ac5 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
48ac6 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
48ac7 6e 20 69 73 0a 2a 2a 20 67 75 61 72 61 6e 74 65  n is.** guarante
48ac8 65 64 20 74 6f 20 62 65 20 61 20 4d 41 54 43 48  ed to be a MATCH
48ac9 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a   constraint..*/.
48aca 73 74 61 74 69 63 20 69 6e 74 20 74 65 73 74 52  static int testR
48acb 74 72 65 65 47 65 6f 6d 28 0a 20 20 52 74 72 65  treeGeom(.  Rtre
48acc 65 20 2a 70 52 74 72 65 65 2c 20 20 20 20 20 20  e *pRtree,      
48acd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
48ace 2d 54 72 65 65 20 6f 62 6a 65 63 74 20 2a 2f 0a  -Tree object */.
48acf 20 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e    RtreeConstrain
48ad0 74 20 2a 70 43 6f 6e 73 74 72 61 69 6e 74 2c 20  t *pConstraint, 
48ad1 20 20 2f 2a 20 4d 41 54 43 48 20 63 6f 6e 73 74    /* MATCH const
48ad2 72 61 69 6e 74 20 74 6f 20 74 65 73 74 20 2a 2f  raint to test */
48ad3 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43  .  RtreeCell *pC
48ad4 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  ell,            
48ad5 20 20 20 2f 2a 20 43 65 6c 6c 20 74 6f 20 74 65     /* Cell to te
48ad6 73 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 62 52  st */.  int *pbR
48ad7 65 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  es              
48ad8 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
48ad9 54 65 73 74 20 72 65 73 75 6c 74 20 2a 2f 0a 29  Test result */.)
48ada 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 52 74 72  {.  int i;.  Rtr
48adb 65 65 44 56 61 6c 75 65 20 61 43 6f 6f 72 64 5b  eeDValue aCoord[
48adc 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53  RTREE_MAX_DIMENS
48add 49 4f 4e 53 2a 32 5d 3b 0a 20 20 69 6e 74 20 6e  IONS*2];.  int n
48ade 43 6f 6f 72 64 20 3d 20 70 52 74 72 65 65 2d 3e  Coord = pRtree->
48adf 6e 44 69 6d 2a 32 3b 0a 0a 20 20 61 73 73 65 72  nDim*2;..  asser
48ae0 74 28 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e  t( pConstraint->
48ae1 6f 70 3d 3d 52 54 52 45 45 5f 4d 41 54 43 48 20  op==RTREE_MATCH 
48ae2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 6f  );.  assert( pCo
48ae3 6e 73 74 72 61 69 6e 74 2d 3e 70 47 65 6f 6d 20  nstraint->pGeom 
48ae4 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  );..  for(i=0; i
48ae5 3c 6e 43 6f 6f 72 64 3b 20 69 2b 2b 29 7b 0a 20  <nCoord; i++){. 
48ae6 20 20 20 61 43 6f 6f 72 64 5b 69 5d 20 3d 20 44     aCoord[i] = D
48ae7 43 4f 4f 52 44 28 70 43 65 6c 6c 2d 3e 61 43 6f  COORD(pCell->aCo
48ae8 6f 72 64 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72  ord[i]);.  }.  r
48ae9 65 74 75 72 6e 20 70 43 6f 6e 73 74 72 61 69 6e  eturn pConstrain
48aea 74 2d 3e 78 47 65 6f 6d 28 70 43 6f 6e 73 74 72  t->xGeom(pConstr
48aeb 61 69 6e 74 2d 3e 70 47 65 6f 6d 2c 20 6e 43 6f  aint->pGeom, nCo
48aec 6f 72 64 2c 20 61 43 6f 6f 72 64 2c 20 70 62 52  ord, aCoord, pbR
48aed 65 73 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 43  es);.}../* .** C
48aee 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20 63 75  ursor pCursor cu
48aef 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74  rrently points t
48af0 6f 20 61 20 63 65 6c 6c 20 69 6e 20 61 20 6e 6f  o a cell in a no
48af1 6e 2d 6c 65 61 66 20 70 61 67 65 2e 0a 2a 2a 20  n-leaf page..** 
48af2 53 65 74 20 2a 70 62 45 6f 66 20 74 6f 20 74 72  Set *pbEof to tr
48af3 75 65 20 69 66 20 74 68 65 20 73 75 62 2d 74 72  ue if the sub-tr
48af4 65 65 20 68 65 61 64 65 64 20 62 79 20 74 68 65  ee headed by the
48af5 20 63 65 6c 6c 20 69 73 20 66 69 6c 74 65 72 65   cell is filtere
48af6 64 0a 2a 2a 20 28 65 78 63 6c 75 64 65 64 29 20  d.** (excluded) 
48af7 62 79 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  by the constrain
48af8 74 73 20 69 6e 20 74 68 65 20 70 43 75 72 73 6f  ts in the pCurso
48af9 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d  r->aConstraint[]
48afa 20 0a 2a 2a 20 61 72 72 61 79 2c 20 6f 72 20 66   .** array, or f
48afb 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e 0a  alse otherwise..
48afc 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  **.** Return SQL
48afd 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 73  ITE_OK if succes
48afe 73 66 75 6c 20 6f 72 20 61 6e 20 53 51 4c 69 74  sful or an SQLit
48aff 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20  e error code if 
48b00 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75  an error.** occu
48b01 72 73 20 77 69 74 68 69 6e 20 61 20 67 65 6f 6d  rs within a geom
48b02 65 74 72 79 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a  etry callback..*
48b03 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65 73  /.static int tes
48b04 74 52 74 72 65 65 43 65 6c 6c 28 52 74 72 65 65  tRtreeCell(Rtree
48b05 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 43   *pRtree, RtreeC
48b06 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20  ursor *pCursor, 
48b07 69 6e 74 20 2a 70 62 45 6f 66 29 7b 0a 20 20 52  int *pbEof){.  R
48b08 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
48b09 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 62   int ii;.  int b
48b0a 52 65 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  Res = 0;.  int r
48b0b 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
48b0c 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
48b0d 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e 70  tree, pCursor->p
48b0e 4e 6f 64 65 2c 20 70 43 75 72 73 6f 72 2d 3e 69  Node, pCursor->i
48b0f 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  Cell, &cell);.  
48b10 66 6f 72 28 69 69 3d 30 3b 20 62 52 65 73 3d 3d  for(ii=0; bRes==
48b11 30 20 26 26 20 69 69 3c 70 43 75 72 73 6f 72 2d  0 && ii<pCursor-
48b12 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69  >nConstraint; ii
48b13 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 43 6f  ++){.    RtreeCo
48b14 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d 20 26 70  nstraint *p = &p
48b15 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61  Cursor->aConstra
48b16 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 52 74 72  int[ii];.    Rtr
48b17 65 65 44 56 61 6c 75 65 20 63 65 6c 6c 5f 6d 69  eeDValue cell_mi
48b18 6e 20 3d 20 44 43 4f 4f 52 44 28 63 65 6c 6c 2e  n = DCOORD(cell.
48b19 61 43 6f 6f 72 64 5b 28 70 2d 3e 69 43 6f 6f 72  aCoord[(p->iCoor
48b1a 64 3e 3e 31 29 2a 32 5d 29 3b 0a 20 20 20 20 52  d>>1)*2]);.    R
48b1b 74 72 65 65 44 56 61 6c 75 65 20 63 65 6c 6c 5f  treeDValue cell_
48b1c 6d 61 78 20 3d 20 44 43 4f 4f 52 44 28 63 65 6c  max = DCOORD(cel
48b1d 6c 2e 61 43 6f 6f 72 64 5b 28 70 2d 3e 69 43 6f  l.aCoord[(p->iCo
48b1e 6f 72 64 3e 3e 31 29 2a 32 2b 31 5d 29 3b 0a 0a  ord>>1)*2+1]);..
48b1f 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e 6f 70      assert(p->op
48b20 3d 3d 52 54 52 45 45 5f 4c 45 20 7c 7c 20 70 2d  ==RTREE_LE || p-
48b21 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 54 20 7c 7c  >op==RTREE_LT ||
48b22 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 45   p->op==RTREE_GE
48b23 20 0a 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e   .        || p->
48b24 6f 70 3d 3d 52 54 52 45 45 5f 47 54 20 7c 7c 20  op==RTREE_GT || 
48b25 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 45 51 20  p->op==RTREE_EQ 
48b26 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  || p->op==RTREE_
48b27 4d 41 54 43 48 0a 20 20 20 20 29 3b 0a 0a 20 20  MATCH.    );..  
48b28 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20    switch( p->op 
48b29 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54  ){.      case RT
48b2a 52 45 45 5f 4c 45 3a 20 63 61 73 65 20 52 54 52  REE_LE: case RTR
48b2b 45 45 5f 4c 54 3a 20 0a 20 20 20 20 20 20 20 20  EE_LT: .        
48b2c 62 52 65 73 20 3d 20 70 2d 3e 72 56 61 6c 75 65  bRes = p->rValue
48b2d 3c 63 65 6c 6c 5f 6d 69 6e 3b 20 0a 20 20 20 20  <cell_min; .    
48b2e 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20      break;..    
48b2f 20 20 63 61 73 65 20 52 54 52 45 45 5f 47 45 3a    case RTREE_GE:
48b30 20 63 61 73 65 20 52 54 52 45 45 5f 47 54 3a 20   case RTREE_GT: 
48b31 0a 20 20 20 20 20 20 20 20 62 52 65 73 20 3d 20  .        bRes = 
48b32 70 2d 3e 72 56 61 6c 75 65 3e 63 65 6c 6c 5f 6d  p->rValue>cell_m
48b33 61 78 3b 20 0a 20 20 20 20 20 20 20 20 62 72 65  ax; .        bre
48b34 61 6b 3b 0a 0a 20 20 20 20 20 20 63 61 73 65 20  ak;..      case 
48b35 52 54 52 45 45 5f 45 51 3a 0a 20 20 20 20 20 20  RTREE_EQ:.      
48b36 20 20 62 52 65 73 20 3d 20 28 70 2d 3e 72 56 61    bRes = (p->rVa
48b37 6c 75 65 3e 63 65 6c 6c 5f 6d 61 78 20 7c 7c 20  lue>cell_max || 
48b38 70 2d 3e 72 56 61 6c 75 65 3c 63 65 6c 6c 5f 6d  p->rValue<cell_m
48b39 69 6e 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  in);.        bre
48b3a 61 6b 3b 0a 0a 20 20 20 20 20 20 64 65 66 61 75  ak;..      defau
48b3b 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73  lt: {.        as
48b3c 73 65 72 74 28 20 70 2d 3e 6f 70 3d 3d 52 54 52  sert( p->op==RTR
48b3d 45 45 5f 4d 41 54 43 48 20 29 3b 0a 20 20 20 20  EE_MATCH );.    
48b3e 20 20 20 20 72 63 20 3d 20 74 65 73 74 52 74 72      rc = testRtr
48b3f 65 65 47 65 6f 6d 28 70 52 74 72 65 65 2c 20 70  eeGeom(pRtree, p
48b40 2c 20 26 63 65 6c 6c 2c 20 26 62 52 65 73 29 3b  , &cell, &bRes);
48b41 0a 20 20 20 20 20 20 20 20 62 52 65 73 20 3d 20  .        bRes = 
48b42 21 62 52 65 73 3b 0a 20 20 20 20 20 20 20 20 62  !bRes;.        b
48b43 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
48b44 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 62 45 6f    }.  }..  *pbEo
48b45 66 20 3d 20 62 52 65 73 3b 0a 20 20 72 65 74 75  f = bRes;.  retu
48b46 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  rn rc;.}../* .**
48b47 20 54 65 73 74 20 69 66 20 74 68 65 20 63 65 6c   Test if the cel
48b48 6c 20 74 68 61 74 20 63 75 72 73 6f 72 20 70 43  l that cursor pC
48b49 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20  ursor currently 
48b4a 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 77 6f 75  points to.** wou
48b4b 6c 64 20 62 65 20 66 69 6c 74 65 72 65 64 20 28  ld be filtered (
48b4c 65 78 63 6c 75 64 65 64 29 20 62 79 20 74 68 65  excluded) by the
48b4d 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e 20   constraints in 
48b4e 74 68 65 20 0a 2a 2a 20 70 43 75 72 73 6f 72 2d  the .** pCursor-
48b4f 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61  >aConstraint[] a
48b50 72 72 61 79 2e 20 49 66 20 73 6f 2c 20 73 65 74  rray. If so, set
48b51 20 2a 70 62 45 6f 66 20 74 6f 20 74 72 75 65 20   *pbEof to true 
48b52 62 65 66 6f 72 65 0a 2a 2a 20 72 65 74 75 72 6e  before.** return
48b53 69 6e 67 2e 20 49 66 20 74 68 65 20 63 65 6c 6c  ing. If the cell
48b54 20 69 73 20 6e 6f 74 20 66 69 6c 74 65 72 65 64   is not filtered
48b55 20 28 65 78 63 6c 75 64 65 64 29 20 62 79 20 74   (excluded) by t
48b56 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a  he constraints,.
48b57 2a 2a 20 73 65 74 20 70 62 45 6f 66 20 74 6f 20  ** set pbEof to 
48b58 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  zero..**.** Retu
48b59 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  rn SQLITE_OK if 
48b5a 73 75 63 63 65 73 73 66 75 6c 20 6f 72 20 61 6e  successful or an
48b5b 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f   SQLite error co
48b5c 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72 0a 2a  de if an error.*
48b5d 2a 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20  * occurs within 
48b5e 61 20 67 65 6f 6d 65 74 72 79 20 63 61 6c 6c 62  a geometry callb
48b5f 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ack..**.** This 
48b60 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73  function assumes
48b61 20 74 68 61 74 20 74 68 65 20 63 65 6c 6c 20 69   that the cell i
48b62 73 20 70 61 72 74 20 6f 66 20 61 20 6c 65 61 66  s part of a leaf
48b63 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   node..*/.static
48b64 20 69 6e 74 20 74 65 73 74 52 74 72 65 65 45 6e   int testRtreeEn
48b65 74 72 79 28 52 74 72 65 65 20 2a 70 52 74 72 65  try(Rtree *pRtre
48b66 65 2c 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a  e, RtreeCursor *
48b67 70 43 75 72 73 6f 72 2c 20 69 6e 74 20 2a 70 62  pCursor, int *pb
48b68 45 6f 66 29 7b 0a 20 20 52 74 72 65 65 43 65 6c  Eof){.  RtreeCel
48b69 6c 20 63 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 69  l cell;.  int ii
48b6a 3b 0a 20 20 2a 70 62 45 6f 66 20 3d 20 30 3b 0a  ;.  *pbEof = 0;.
48b6b 0a 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70  .  nodeGetCell(p
48b6c 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e  Rtree, pCursor->
48b6d 70 4e 6f 64 65 2c 20 70 43 75 72 73 6f 72 2d 3e  pNode, pCursor->
48b6e 69 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20  iCell, &cell);. 
48b6f 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 43   for(ii=0; ii<pC
48b70 75 72 73 6f 72 2d 3e 6e 43 6f 6e 73 74 72 61 69  ursor->nConstrai
48b71 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52  nt; ii++){.    R
48b72 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a  treeConstraint *
48b73 70 20 3d 20 26 70 43 75 72 73 6f 72 2d 3e 61 43  p = &pCursor->aC
48b74 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20  onstraint[ii];. 
48b75 20 20 20 52 74 72 65 65 44 56 61 6c 75 65 20 63     RtreeDValue c
48b76 6f 6f 72 64 20 3d 20 44 43 4f 4f 52 44 28 63 65  oord = DCOORD(ce
48b77 6c 6c 2e 61 43 6f 6f 72 64 5b 70 2d 3e 69 43 6f  ll.aCoord[p->iCo
48b78 6f 72 64 5d 29 3b 0a 20 20 20 20 69 6e 74 20 72  ord]);.    int r
48b79 65 73 3b 0a 20 20 20 20 61 73 73 65 72 74 28 70  es;.    assert(p
48b7a 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 45 20 7c  ->op==RTREE_LE |
48b7b 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c  | p->op==RTREE_L
48b7c 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45  T || p->op==RTRE
48b7d 45 5f 47 45 20 0a 20 20 20 20 20 20 20 20 7c 7c  E_GE .        ||
48b7e 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 54   p->op==RTREE_GT
48b7f 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45   || p->op==RTREE
48b80 5f 45 51 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54  _EQ || p->op==RT
48b81 52 45 45 5f 4d 41 54 43 48 0a 20 20 20 20 29 3b  REE_MATCH.    );
48b82 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 2d 3e  .    switch( p->
48b83 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  op ){.      case
48b84 20 52 54 52 45 45 5f 4c 45 3a 20 72 65 73 20 3d   RTREE_LE: res =
48b85 20 28 63 6f 6f 72 64 3c 3d 70 2d 3e 72 56 61 6c   (coord<=p->rVal
48b86 75 65 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20  ue); break;.    
48b87 20 20 63 61 73 65 20 52 54 52 45 45 5f 4c 54 3a    case RTREE_LT:
48b88 20 72 65 73 20 3d 20 28 63 6f 6f 72 64 3c 70 2d   res = (coord<p-
48b89 3e 72 56 61 6c 75 65 29 3b 20 20 62 72 65 61 6b  >rValue);  break
48b8a 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52  ;.      case RTR
48b8b 45 45 5f 47 45 3a 20 72 65 73 20 3d 20 28 63 6f  EE_GE: res = (co
48b8c 6f 72 64 3e 3d 70 2d 3e 72 56 61 6c 75 65 29 3b  ord>=p->rValue);
48b8d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
48b8e 73 65 20 52 54 52 45 45 5f 47 54 3a 20 72 65 73  se RTREE_GT: res
48b8f 20 3d 20 28 63 6f 6f 72 64 3e 70 2d 3e 72 56 61   = (coord>p->rVa
48b90 6c 75 65 29 3b 20 20 62 72 65 61 6b 3b 0a 20 20  lue);  break;.  
48b91 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 45      case RTREE_E
48b92 51 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64 3d  Q: res = (coord=
48b93 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20 62 72 65  =p->rValue); bre
48b94 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c  ak;.      defaul
48b95 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  t: {.        int
48b96 20 72 63 3b 0a 20 20 20 20 20 20 20 20 61 73 73   rc;.        ass
48b97 65 72 74 28 20 70 2d 3e 6f 70 3d 3d 52 54 52 45  ert( p->op==RTRE
48b98 45 5f 4d 41 54 43 48 20 29 3b 0a 20 20 20 20 20  E_MATCH );.     
48b99 20 20 20 72 63 20 3d 20 74 65 73 74 52 74 72 65     rc = testRtre
48b9a 65 47 65 6f 6d 28 70 52 74 72 65 65 2c 20 70 2c  eGeom(pRtree, p,
48b9b 20 26 63 65 6c 6c 2c 20 26 72 65 73 29 3b 0a 20   &cell, &res);. 
48b9c 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
48b9d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
48b9e 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
48b9f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
48ba0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
48ba1 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  }.    }..    if(
48ba2 20 21 72 65 73 20 29 7b 0a 20 20 20 20 20 20 2a   !res ){.      *
48ba3 70 62 45 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20  pbEof = 1;.     
48ba4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
48ba5 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  K;.    }.  }..  
48ba6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
48ba7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 75 72 73 6f  ;.}../*.** Curso
48ba8 72 20 70 43 75 72 73 6f 72 20 63 75 72 72 65 6e  r pCursor curren
48ba9 74 6c 79 20 70 6f 69 6e 74 73 20 61 74 20 61 20  tly points at a 
48baa 6e 6f 64 65 20 74 68 61 74 20 68 65 61 64 73 20  node that heads 
48bab 61 20 73 75 62 2d 74 72 65 65 20 6f 66 0a 2a 2a  a sub-tree of.**
48bac 20 68 65 69 67 68 74 20 69 48 65 69 67 68 74 20   height iHeight 
48bad 28 69 66 20 69 48 65 69 67 68 74 3d 3d 30 2c 20  (if iHeight==0, 
48bae 74 68 65 6e 20 74 68 65 20 6e 6f 64 65 20 69 73  then the node is
48baf 20 61 20 6c 65 61 66 29 2e 20 44 65 73 63 65 6e   a leaf). Descen
48bb0 64 0a 2a 2a 20 74 6f 20 70 6f 69 6e 74 20 74 6f  d.** to point to
48bb1 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63   the left-most c
48bb2 65 6c 6c 20 6f 66 20 74 68 65 20 73 75 62 2d 74  ell of the sub-t
48bb3 72 65 65 20 74 68 61 74 20 6d 61 74 63 68 65 73  ree that matches
48bb4 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 66 69 67 75   the .** configu
48bb5 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  red constraints.
48bb6 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
48bb7 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 0a 20 20  escendToCell(.  
48bb8 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a  Rtree *pRtree, .
48bb9 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70    RtreeCursor *p
48bba 43 75 72 73 6f 72 2c 20 0a 20 20 69 6e 74 20 69  Cursor, .  int i
48bbb 48 65 69 67 68 74 2c 0a 20 20 69 6e 74 20 2a 70  Height,.  int *p
48bbc 45 6f 66 20 20 20 20 20 20 20 20 20 20 20 20 20  Eof             
48bbd 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74 20      /* OUT: Set 
48bbe 74 6f 20 74 72 75 65 20 69 66 20 63 61 6e 6e 6f  to true if canno
48bbf 74 20 64 65 73 63 65 6e 64 20 2a 2f 0a 29 7b 0a  t descend */.){.
48bc0 20 20 69 6e 74 20 69 73 45 6f 66 3b 0a 20 20 69    int isEof;.  i
48bc1 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 69 3b  nt rc;.  int ii;
48bc2 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43  .  RtreeNode *pC
48bc3 68 69 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f  hild;.  sqlite3_
48bc4 69 6e 74 36 34 20 69 52 6f 77 69 64 3b 0a 0a 20  int64 iRowid;.. 
48bc5 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 53 61 76   RtreeNode *pSav
48bc6 65 64 4e 6f 64 65 20 3d 20 70 43 75 72 73 6f 72  edNode = pCursor
48bc7 2d 3e 70 4e 6f 64 65 3b 0a 20 20 69 6e 74 20 69  ->pNode;.  int i
48bc8 53 61 76 65 64 43 65 6c 6c 20 3d 20 70 43 75 72  SavedCell = pCur
48bc9 73 6f 72 2d 3e 69 43 65 6c 6c 3b 0a 0a 20 20 61  sor->iCell;..  a
48bca 73 73 65 72 74 28 20 69 48 65 69 67 68 74 3e 3d  ssert( iHeight>=
48bcb 30 20 29 3b 0a 0a 20 20 69 66 28 20 69 48 65 69  0 );..  if( iHei
48bcc 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  ght==0 ){.    rc
48bcd 20 3d 20 74 65 73 74 52 74 72 65 65 45 6e 74 72   = testRtreeEntr
48bce 79 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f  y(pRtree, pCurso
48bcf 72 2c 20 26 69 73 45 6f 66 29 3b 0a 20 20 7d 65  r, &isEof);.  }e
48bd0 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 74 65  lse{.    rc = te
48bd1 73 74 52 74 72 65 65 43 65 6c 6c 28 70 52 74 72  stRtreeCell(pRtr
48bd2 65 65 2c 20 70 43 75 72 73 6f 72 2c 20 26 69 73  ee, pCursor, &is
48bd3 45 6f 66 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Eof);.  }.  if( 
48bd4 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
48bd5 20 69 73 45 6f 66 20 7c 7c 20 69 48 65 69 67 68   isEof || iHeigh
48bd6 74 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f  t==0 ){.    goto
48bd7 20 64 65 73 63 65 6e 64 5f 74 6f 5f 63 65 6c 6c   descend_to_cell
48bd8 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 69 52 6f  _out;.  }..  iRo
48bd9 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77  wid = nodeGetRow
48bda 69 64 28 70 52 74 72 65 65 2c 20 70 43 75 72 73  id(pRtree, pCurs
48bdb 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72 73  or->pNode, pCurs
48bdc 6f 72 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 72 63  or->iCell);.  rc
48bdd 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70   = nodeAcquire(p
48bde 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70  Rtree, iRowid, p
48bdf 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20 26  Cursor->pNode, &
48be0 70 43 68 69 6c 64 29 3b 0a 20 20 69 66 28 20 72  pChild);.  if( r
48be1 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
48be2 20 20 20 20 67 6f 74 6f 20 64 65 73 63 65 6e 64      goto descend
48be3 5f 74 6f 5f 63 65 6c 6c 5f 6f 75 74 3b 0a 20 20  _to_cell_out;.  
48be4 7d 0a 0a 20 20 6e 6f 64 65 52 65 6c 65 61 73 65  }..  nodeRelease
48be5 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72  (pRtree, pCursor
48be6 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20 70 43 75 72  ->pNode);.  pCur
48be7 73 6f 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 43 68  sor->pNode = pCh
48be8 69 6c 64 3b 0a 20 20 69 73 45 6f 66 20 3d 20 31  ild;.  isEof = 1
48be9 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 73  ;.  for(ii=0; is
48bea 45 6f 66 20 26 26 20 69 69 3c 4e 43 45 4c 4c 28  Eof && ii<NCELL(
48beb 70 43 68 69 6c 64 29 3b 20 69 69 2b 2b 29 7b 0a  pChild); ii++){.
48bec 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 69 43 65      pCursor->iCe
48bed 6c 6c 20 3d 20 69 69 3b 0a 20 20 20 20 72 63 20  ll = ii;.    rc 
48bee 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28  = descendToCell(
48bef 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2c  pRtree, pCursor,
48bf0 20 69 48 65 69 67 68 74 2d 31 2c 20 26 69 73 45   iHeight-1, &isE
48bf1 6f 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  of);.    if( rc!
48bf2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
48bf3 20 20 20 20 67 6f 74 6f 20 64 65 73 63 65 6e 64      goto descend
48bf4 5f 74 6f 5f 63 65 6c 6c 5f 6f 75 74 3b 0a 20 20  _to_cell_out;.  
48bf5 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69    }.  }..  if( i
48bf6 73 45 6f 66 20 29 7b 0a 20 20 20 20 61 73 73 65  sEof ){.    asse
48bf7 72 74 28 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f  rt( pCursor->pNo
48bf8 64 65 3d 3d 70 43 68 69 6c 64 20 29 3b 0a 20 20  de==pChild );.  
48bf9 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28    nodeReference(
48bfa 70 53 61 76 65 64 4e 6f 64 65 29 3b 0a 20 20 20  pSavedNode);.   
48bfb 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
48bfc 72 65 65 2c 20 70 43 68 69 6c 64 29 3b 0a 20 20  ree, pChild);.  
48bfd 20 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65    pCursor->pNode
48bfe 20 3d 20 70 53 61 76 65 64 4e 6f 64 65 3b 0a 20   = pSavedNode;. 
48bff 20 20 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c     pCursor->iCel
48c00 6c 20 3d 20 69 53 61 76 65 64 43 65 6c 6c 3b 0a  l = iSavedCell;.
48c01 20 20 7d 0a 0a 64 65 73 63 65 6e 64 5f 74 6f 5f    }..descend_to_
48c02 63 65 6c 6c 5f 6f 75 74 3a 0a 20 20 2a 70 45 6f  cell_out:.  *pEo
48c03 66 20 3d 20 69 73 45 6f 66 3b 0a 20 20 72 65 74  f = isEof;.  ret
48c04 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
48c05 20 4f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c   One of the cell
48c06 73 20 69 6e 20 6e 6f 64 65 20 70 4e 6f 64 65 20  s in node pNode 
48c07 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  is guaranteed to
48c08 20 68 61 76 65 20 61 20 36 34 2d 62 69 74 20 0a   have a 64-bit .
48c09 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  ** integer value
48c0a 20 65 71 75 61 6c 20 74 6f 20 69 52 6f 77 69 64   equal to iRowid
48c0b 2e 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64  . Return the ind
48c0c 65 78 20 6f 66 20 74 68 69 73 20 63 65 6c 6c 2e  ex of this cell.
48c0d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
48c0e 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28 0a 20  odeRowidIndex(. 
48c0f 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20   Rtree *pRtree, 
48c10 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  .  RtreeNode *pN
48c11 6f 64 65 2c 20 0a 20 20 69 36 34 20 69 52 6f 77  ode, .  i64 iRow
48c12 69 64 2c 0a 20 20 69 6e 74 20 2a 70 69 49 6e 64  id,.  int *piInd
48c13 65 78 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  ex.){.  int ii;.
48c14 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43    int nCell = NC
48c15 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 66 6f  ELL(pNode);.  fo
48c16 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c  r(ii=0; ii<nCell
48c17 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  ; ii++){.    if(
48c18 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52   nodeGetRowid(pR
48c19 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 29  tree, pNode, ii)
48c1a 3d 3d 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  ==iRowid ){.    
48c1b 20 20 2a 70 69 49 6e 64 65 78 20 3d 20 69 69 3b    *piIndex = ii;
48c1c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
48c1d 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
48c1e 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
48c1f 54 45 5f 43 4f 52 52 55 50 54 5f 56 54 41 42 3b  TE_CORRUPT_VTAB;
48c20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
48c21 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
48c22 65 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 69 6e  e cell containin
48c23 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 6e  g a pointer to n
48c24 6f 64 65 20 70 4e 6f 64 65 0a 2a 2a 20 69 6e 20  ode pNode.** in 
48c25 69 74 73 20 70 61 72 65 6e 74 2e 20 49 66 20 70  its parent. If p
48c26 4e 6f 64 65 20 69 73 20 74 68 65 20 72 6f 6f 74  Node is the root
48c27 20 6e 6f 64 65 2c 20 72 65 74 75 72 6e 20 2d 31   node, return -1
48c28 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
48c29 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28  nodeParentIndex(
48c2a 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
48c2b 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
48c2c 20 69 6e 74 20 2a 70 69 49 6e 64 65 78 29 7b 0a   int *piIndex){.
48c2d 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61    RtreeNode *pPa
48c2e 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70 50  rent = pNode->pP
48c2f 61 72 65 6e 74 3b 0a 20 20 69 66 28 20 70 50 61  arent;.  if( pPa
48c30 72 65 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75  rent ){.    retu
48c31 72 6e 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65  rn nodeRowidInde
48c32 78 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e  x(pRtree, pParen
48c33 74 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 2c  t, pNode->iNode,
48c34 20 70 69 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20   piIndex);.  }. 
48c35 20 2a 70 69 49 6e 64 65 78 20 3d 20 2d 31 3b 0a   *piIndex = -1;.
48c36 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
48c37 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  OK;.}../* .** Rt
48c38 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
48c39 65 20 6d 6f 64 75 6c 65 20 78 4e 65 78 74 20 6d  e module xNext m
48c3a 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
48c3b 20 69 6e 74 20 72 74 72 65 65 4e 65 78 74 28 73   int rtreeNext(s
48c3c 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
48c3d 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 29  or *pVtabCursor)
48c3e 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  {.  Rtree *pRtre
48c3f 65 20 3d 20 28 52 74 72 65 65 20 2a 29 28 70 56  e = (Rtree *)(pV
48c40 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
48c41 29 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72  );.  RtreeCursor
48c42 20 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65 43   *pCsr = (RtreeC
48c43 75 72 73 6f 72 20 2a 29 70 56 74 61 62 43 75 72  ursor *)pVtabCur
48c44 73 6f 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  sor;.  int rc = 
48c45 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a  SQLITE_OK;..  /*
48c46 20 52 74 72 65 65 43 75 72 73 6f 72 2e 70 4e 6f   RtreeCursor.pNo
48c47 64 65 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 4e  de must not be N
48c48 55 4c 4c 2e 20 49 66 20 69 73 20 69 73 20 4e 55  ULL. If is is NU
48c49 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 20 63 75  LL, then this cu
48c4a 72 73 6f 72 20 69 73 0a 20 20 2a 2a 20 61 6c 72  rsor is.  ** alr
48c4b 65 61 64 79 20 61 74 20 45 4f 46 2e 20 49 74 20  eady at EOF. It 
48c4c 69 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 72  is against the r
48c4d 75 6c 65 73 20 74 6f 20 63 61 6c 6c 20 74 68 65  ules to call the
48c4e 20 78 4e 65 78 74 28 29 20 6d 65 74 68 6f 64 20   xNext() method 
48c4f 6f 66 0a 20 20 2a 2a 20 61 20 63 75 72 73 6f 72  of.  ** a cursor
48c50 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64   that has alread
48c51 79 20 72 65 61 63 68 65 64 20 45 4f 46 2e 0a 20  y reached EOF.. 
48c52 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 43   */.  assert( pC
48c53 73 72 2d 3e 70 4e 6f 64 65 20 29 3b 0a 0a 20 20  sr->pNode );..  
48c54 69 66 28 20 70 43 73 72 2d 3e 69 53 74 72 61 74  if( pCsr->iStrat
48c55 65 67 79 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a  egy==1 ){.    /*
48c56 20 54 68 69 73 20 22 73 63 61 6e 22 20 69 73 20   This "scan" is 
48c57 61 20 64 69 72 65 63 74 20 6c 6f 6f 6b 75 70 20  a direct lookup 
48c58 62 79 20 72 6f 77 69 64 2e 20 54 68 65 72 65 20  by rowid. There 
48c59 69 73 20 6e 6f 20 6e 65 78 74 20 65 6e 74 72 79  is no next entry
48c5a 2e 20 2a 2f 0a 20 20 20 20 6e 6f 64 65 52 65 6c  . */.    nodeRel
48c5b 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43 73  ease(pRtree, pCs
48c5c 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70  r->pNode);.    p
48c5d 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a  Csr->pNode = 0;.
48c5e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
48c5f 4d 6f 76 65 20 74 6f 20 74 68 65 20 6e 65 78 74  Move to the next
48c60 20 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63   entry that matc
48c61 68 65 73 20 74 68 65 20 63 6f 6e 66 69 67 75 72  hes the configur
48c62 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20  ed constraints. 
48c63 2a 2f 0a 20 20 20 20 69 6e 74 20 69 48 65 69 67  */.    int iHeig
48c64 68 74 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c  ht = 0;.    whil
48c65 65 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 29  e( pCsr->pNode )
48c66 7b 0a 20 20 20 20 20 20 52 74 72 65 65 4e 6f 64  {.      RtreeNod
48c67 65 20 2a 70 4e 6f 64 65 20 3d 20 70 43 73 72 2d  e *pNode = pCsr-
48c68 3e 70 4e 6f 64 65 3b 0a 20 20 20 20 20 20 69 6e  >pNode;.      in
48c69 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28  t nCell = NCELL(
48c6a 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 66 6f  pNode);.      fo
48c6b 72 28 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 3b  r(pCsr->iCell++;
48c6c 20 70 43 73 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65   pCsr->iCell<nCe
48c6d 6c 6c 3b 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2b  ll; pCsr->iCell+
48c6e 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  +){.        int 
48c6f 69 73 45 6f 66 3b 0a 20 20 20 20 20 20 20 20 72  isEof;.        r
48c70 63 20 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c  c = descendToCel
48c71 6c 28 70 52 74 72 65 65 2c 20 70 43 73 72 2c 20  l(pRtree, pCsr, 
48c72 69 48 65 69 67 68 74 2c 20 26 69 73 45 6f 66 29  iHeight, &isEof)
48c73 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
48c74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21  !=SQLITE_OK || !
48c75 69 73 45 6f 66 20 29 7b 0a 20 20 20 20 20 20 20  isEof ){.       
48c76 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
48c77 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
48c78 20 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64        pCsr->pNod
48c79 65 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65  e = pNode->pPare
48c7a 6e 74 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e  nt;.      rc = n
48c7b 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 70  odeParentIndex(p
48c7c 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 26 70  Rtree, pNode, &p
48c7d 43 73 72 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 20  Csr->iCell);.   
48c7e 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
48c7f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
48c80 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
48c81 20 7d 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66   }.      nodeRef
48c82 65 72 65 6e 63 65 28 70 43 73 72 2d 3e 70 4e 6f  erence(pCsr->pNo
48c83 64 65 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52  de);.      nodeR
48c84 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
48c85 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 69 48 65  Node);.      iHe
48c86 69 67 68 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  ight++;.    }.  
48c87 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
48c88 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20  }../* .** Rtree 
48c89 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
48c8a 64 75 6c 65 20 78 52 6f 77 69 64 20 6d 65 74 68  dule xRowid meth
48c8b 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
48c8c 74 20 72 74 72 65 65 52 6f 77 69 64 28 73 71 6c  t rtreeRowid(sql
48c8d 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
48c8e 20 2a 70 56 74 61 62 43 75 72 73 6f 72 2c 20 73   *pVtabCursor, s
48c8f 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52 6f  qlite_int64 *pRo
48c90 77 69 64 29 7b 0a 20 20 52 74 72 65 65 20 2a 70  wid){.  Rtree *p
48c91 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
48c92 29 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56  )pVtabCursor->pV
48c93 74 61 62 3b 0a 20 20 52 74 72 65 65 43 75 72 73  tab;.  RtreeCurs
48c94 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72 65  or *pCsr = (Rtre
48c95 65 43 75 72 73 6f 72 20 2a 29 70 56 74 61 62 43  eCursor *)pVtabC
48c96 75 72 73 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74  ursor;..  assert
48c97 28 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20  (pCsr->pNode);. 
48c98 20 2a 70 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47   *pRowid = nodeG
48c99 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20  etRowid(pRtree, 
48c9a 70 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 73  pCsr->pNode, pCs
48c9b 72 2d 3e 69 43 65 6c 6c 29 3b 0a 0a 20 20 72 65  r->iCell);..  re
48c9c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
48c9d 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20  }../* .** Rtree 
48c9e 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
48c9f 64 75 6c 65 20 78 43 6f 6c 75 6d 6e 20 6d 65 74  dule xColumn met
48ca0 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
48ca1 6e 74 20 72 74 72 65 65 43 6f 6c 75 6d 6e 28 73  nt rtreeColumn(s
48ca2 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
48ca3 6f 72 20 2a 63 75 72 2c 20 73 71 6c 69 74 65 33  or *cur, sqlite3
48ca4 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c 20 69  _context *ctx, i
48ca5 6e 74 20 69 29 7b 0a 20 20 52 74 72 65 65 20 2a  nt i){.  Rtree *
48ca6 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20  pRtree = (Rtree 
48ca7 2a 29 63 75 72 2d 3e 70 56 74 61 62 3b 0a 20 20  *)cur->pVtab;.  
48ca8 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73  RtreeCursor *pCs
48ca9 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72  r = (RtreeCursor
48caa 20 2a 29 63 75 72 3b 0a 0a 20 20 69 66 28 20 69   *)cur;..  if( i
48cab 3d 3d 30 20 29 7b 0a 20 20 20 20 69 36 34 20 69  ==0 ){.    i64 i
48cac 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52  Rowid = nodeGetR
48cad 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70 43 73  owid(pRtree, pCs
48cae 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72 2d 3e  r->pNode, pCsr->
48caf 69 43 65 6c 6c 29 3b 0a 20 20 20 20 73 71 6c 69  iCell);.    sqli
48cb0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34  te3_result_int64
48cb1 28 63 74 78 2c 20 69 52 6f 77 69 64 29 3b 0a 20  (ctx, iRowid);. 
48cb2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 52 74 72 65   }else{.    Rtre
48cb3 65 43 6f 6f 72 64 20 63 3b 0a 20 20 20 20 6e 6f  eCoord c;.    no
48cb4 64 65 47 65 74 43 6f 6f 72 64 28 70 52 74 72 65  deGetCoord(pRtre
48cb5 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c 20  e, pCsr->pNode, 
48cb6 70 43 73 72 2d 3e 69 43 65 6c 6c 2c 20 69 2d 31  pCsr->iCell, i-1
48cb7 2c 20 26 63 29 3b 0a 23 69 66 6e 64 65 66 20 53  , &c);.#ifndef S
48cb8 51 4c 49 54 45 5f 52 54 52 45 45 5f 49 4e 54 5f  QLITE_RTREE_INT_
48cb9 4f 4e 4c 59 0a 20 20 20 20 69 66 28 20 70 52 74  ONLY.    if( pRt
48cba 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d  ree->eCoordType=
48cbb 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41  =RTREE_COORD_REA
48cbc 4c 33 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  L32 ){.      sql
48cbd 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
48cbe 6c 65 28 63 74 78 2c 20 63 2e 66 29 3b 0a 20 20  le(ctx, c.f);.  
48cbf 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
48cc0 20 20 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72     {.      asser
48cc1 74 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72  t( pRtree->eCoor
48cc2 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f  dType==RTREE_COO
48cc3 52 44 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20  RD_INT32 );.    
48cc4 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
48cc5 5f 69 6e 74 28 63 74 78 2c 20 63 2e 69 29 3b 0a  _int(ctx, c.i);.
48cc6 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
48cc7 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
48cc8 0a 0a 2f 2a 20 0a 2a 2a 20 55 73 65 20 6e 6f 64  ../* .** Use nod
48cc9 65 41 63 71 75 69 72 65 28 29 20 74 6f 20 6f 62  eAcquire() to ob
48cca 74 61 69 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f  tain the leaf no
48ccb 64 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  de containing th
48ccc 65 20 72 65 63 6f 72 64 20 77 69 74 68 20 0a 2a  e record with .*
48ccd 2a 20 72 6f 77 69 64 20 69 52 6f 77 69 64 2e 20  * rowid iRowid. 
48cce 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73  If successful, s
48ccf 65 74 20 2a 70 70 4c 65 61 66 20 74 6f 20 70 6f  et *ppLeaf to po
48cd0 69 6e 74 20 74 6f 20 74 68 65 20 6e 6f 64 65 20  int to the node 
48cd1 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51  and.** return SQ
48cd2 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 65 72  LITE_OK. If ther
48cd3 65 20 69 73 20 6e 6f 20 73 75 63 68 20 72 65 63  e is no such rec
48cd4 6f 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65  ord in the table
48cd5 2c 20 73 65 74 0a 2a 2a 20 2a 70 70 4c 65 61 66  , set.** *ppLeaf
48cd6 20 74 6f 20 30 20 61 6e 64 20 72 65 74 75 72 6e   to 0 and return
48cd7 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61   SQLITE_OK. If a
48cd8 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
48cd9 73 65 74 20 2a 70 70 4c 65 61 66 0a 2a 2a 20 74  set *ppLeaf.** t
48cda 6f 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72  o zero and retur
48cdb 6e 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  n an SQLite erro
48cdc 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69  r code..*/.stati
48cdd 63 20 69 6e 74 20 66 69 6e 64 4c 65 61 66 4e 6f  c int findLeafNo
48cde 64 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  de(Rtree *pRtree
48cdf 2c 20 69 36 34 20 69 52 6f 77 69 64 2c 20 52 74  , i64 iRowid, Rt
48ce0 72 65 65 4e 6f 64 65 20 2a 2a 70 70 4c 65 61 66  reeNode **ppLeaf
48ce1 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 2a  ){.  int rc;.  *
48ce2 70 70 4c 65 61 66 20 3d 20 30 3b 0a 20 20 73 71  ppLeaf = 0;.  sq
48ce3 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
48ce4 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f  (pRtree->pReadRo
48ce5 77 69 64 2c 20 31 2c 20 69 52 6f 77 69 64 29 3b  wid, 1, iRowid);
48ce6 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 73  .  if( sqlite3_s
48ce7 74 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61  tep(pRtree->pRea
48ce8 64 52 6f 77 69 64 29 3d 3d 53 51 4c 49 54 45 5f  dRowid)==SQLITE_
48ce9 52 4f 57 20 29 7b 0a 20 20 20 20 69 36 34 20 69  ROW ){.    i64 i
48cea 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 5f 63  Node = sqlite3_c
48ceb 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 52 74 72  olumn_int64(pRtr
48cec 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20  ee->pReadRowid, 
48ced 30 29 3b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64  0);.    rc = nod
48cee 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c  eAcquire(pRtree,
48cef 20 69 4e 6f 64 65 2c 20 30 2c 20 70 70 4c 65 61   iNode, 0, ppLea
48cf0 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  f);.    sqlite3_
48cf1 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52  reset(pRtree->pR
48cf2 65 61 64 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c  eadRowid);.  }el
48cf3 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  se{.    rc = sql
48cf4 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
48cf5 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a  e->pReadRowid);.
48cf6 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
48cf7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
48cf8 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
48cf9 64 20 74 6f 20 63 6f 6e 66 69 67 75 72 65 20 74  d to configure t
48cfa 68 65 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69  he RtreeConstrai
48cfb 6e 74 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64  nt object passed
48cfc 0a 2a 2a 20 61 73 20 74 68 65 20 73 65 63 6f 6e  .** as the secon
48cfd 64 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 61  d argument for a
48cfe 20 4d 41 54 43 48 20 63 6f 6e 73 74 72 61 69 6e   MATCH constrain
48cff 74 2e 20 54 68 65 20 76 61 6c 75 65 20 70 61 73  t. The value pas
48d00 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 66 69  sed as the.** fi
48d01 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
48d02 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
48d03 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20   the right-hand 
48d04 6f 70 65 72 61 6e 64 20 74 6f 20 74 68 65 20 4d  operand to the M
48d05 41 54 43 48 0a 2a 2a 20 6f 70 65 72 61 74 6f 72  ATCH.** operator
48d06 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
48d07 64 65 73 65 72 69 61 6c 69 7a 65 47 65 6f 6d 65  deserializeGeome
48d08 74 72 79 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  try(sqlite3_valu
48d09 65 20 2a 70 56 61 6c 75 65 2c 20 52 74 72 65 65  e *pValue, Rtree
48d0a 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70 43 6f 6e  Constraint *pCon
48d0b 73 29 7b 0a 20 20 52 74 72 65 65 4d 61 74 63 68  s){.  RtreeMatch
48d0c 41 72 67 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65  Arg *p;.  sqlite
48d0d 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 79  3_rtree_geometry
48d0e 20 2a 70 47 65 6f 6d 3b 0a 20 20 69 6e 74 20 6e   *pGeom;.  int n
48d0f 42 6c 6f 62 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  Blob;..  /* Chec
48d10 6b 20 74 68 61 74 20 76 61 6c 75 65 20 69 73 20  k that value is 
48d11 61 63 74 75 61 6c 6c 79 20 61 20 62 6c 6f 62 2e  actually a blob.
48d12 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
48d13 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 56 61  3_value_type(pVa
48d14 6c 75 65 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f  lue)!=SQLITE_BLO
48d15 42 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  B ) return SQLIT
48d16 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 43  E_ERROR;..  /* C
48d17 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 62 6c  heck that the bl
48d18 6f 62 20 69 73 20 72 6f 75 67 68 6c 79 20 74 68  ob is roughly th
48d19 65 20 72 69 67 68 74 20 73 69 7a 65 2e 20 2a 2f  e right size. */
48d1a 0a 20 20 6e 42 6c 6f 62 20 3d 20 73 71 6c 69 74  .  nBlob = sqlit
48d1b 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 70  e3_value_bytes(p
48d1c 56 61 6c 75 65 29 3b 0a 20 20 69 66 28 20 6e 42  Value);.  if( nB
48d1d 6c 6f 62 3c 28 69 6e 74 29 73 69 7a 65 6f 66 28  lob<(int)sizeof(
48d1e 52 74 72 65 65 4d 61 74 63 68 41 72 67 29 20 0a  RtreeMatchArg) .
48d1f 20 20 20 7c 7c 20 28 28 6e 42 6c 6f 62 2d 73 69     || ((nBlob-si
48d20 7a 65 6f 66 28 52 74 72 65 65 4d 61 74 63 68 41  zeof(RtreeMatchA
48d21 72 67 29 29 25 73 69 7a 65 6f 66 28 52 74 72 65  rg))%sizeof(Rtre
48d22 65 44 56 61 6c 75 65 29 29 21 3d 30 0a 20 20 29  eDValue))!=0.  )
48d23 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
48d24 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a  ITE_ERROR;.  }..
48d25 20 20 70 47 65 6f 6d 20 3d 20 28 73 71 6c 69 74    pGeom = (sqlit
48d26 65 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72  e3_rtree_geometr
48d27 79 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  y *)sqlite3_mall
48d28 6f 63 28 0a 20 20 20 20 20 20 73 69 7a 65 6f 66  oc(.      sizeof
48d29 28 73 71 6c 69 74 65 33 5f 72 74 72 65 65 5f 67  (sqlite3_rtree_g
48d2a 65 6f 6d 65 74 72 79 29 20 2b 20 6e 42 6c 6f 62  eometry) + nBlob
48d2b 0a 20 20 29 3b 0a 20 20 69 66 28 20 21 70 47 65  .  );.  if( !pGe
48d2c 6f 6d 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  om ) return SQLI
48d2d 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73  TE_NOMEM;.  mems
48d2e 65 74 28 70 47 65 6f 6d 2c 20 30 2c 20 73 69 7a  et(pGeom, 0, siz
48d2f 65 6f 66 28 73 71 6c 69 74 65 33 5f 72 74 72 65  eof(sqlite3_rtre
48d30 65 5f 67 65 6f 6d 65 74 72 79 29 29 3b 0a 20 20  e_geometry));.  
48d31 70 20 3d 20 28 52 74 72 65 65 4d 61 74 63 68 41  p = (RtreeMatchA
48d32 72 67 20 2a 29 26 70 47 65 6f 6d 5b 31 5d 3b 0a  rg *)&pGeom[1];.
48d33 0a 20 20 6d 65 6d 63 70 79 28 70 2c 20 73 71 6c  .  memcpy(p, sql
48d34 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
48d35 70 56 61 6c 75 65 29 2c 20 6e 42 6c 6f 62 29 3b  pValue), nBlob);
48d36 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21  .  if( p->magic!
48d37 3d 52 54 52 45 45 5f 47 45 4f 4d 45 54 52 59 5f  =RTREE_GEOMETRY_
48d38 4d 41 47 49 43 20 0a 20 20 20 7c 7c 20 6e 42 6c  MAGIC .   || nBl
48d39 6f 62 21 3d 28 69 6e 74 29 28 73 69 7a 65 6f 66  ob!=(int)(sizeof
48d3a 28 52 74 72 65 65 4d 61 74 63 68 41 72 67 29 20  (RtreeMatchArg) 
48d3b 2b 20 28 70 2d 3e 6e 50 61 72 61 6d 2d 31 29 2a  + (p->nParam-1)*
48d3c 73 69 7a 65 6f 66 28 52 74 72 65 65 44 56 61 6c  sizeof(RtreeDVal
48d3d 75 65 29 29 0a 20 20 29 7b 0a 20 20 20 20 73 71  ue)).  ){.    sq
48d3e 6c 69 74 65 33 5f 66 72 65 65 28 70 47 65 6f 6d  lite3_free(pGeom
48d3f 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
48d40 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
48d41 0a 20 20 70 47 65 6f 6d 2d 3e 70 43 6f 6e 74 65  .  pGeom->pConte
48d42 78 74 20 3d 20 70 2d 3e 70 43 6f 6e 74 65 78 74  xt = p->pContext
48d43 3b 0a 20 20 70 47 65 6f 6d 2d 3e 6e 50 61 72 61  ;.  pGeom->nPara
48d44 6d 20 3d 20 70 2d 3e 6e 50 61 72 61 6d 3b 0a 20  m = p->nParam;. 
48d45 20 70 47 65 6f 6d 2d 3e 61 50 61 72 61 6d 20 3d   pGeom->aParam =
48d46 20 70 2d 3e 61 50 61 72 61 6d 3b 0a 0a 20 20 70   p->aParam;..  p
48d47 43 6f 6e 73 2d 3e 78 47 65 6f 6d 20 3d 20 70 2d  Cons->xGeom = p-
48d48 3e 78 47 65 6f 6d 3b 0a 20 20 70 43 6f 6e 73 2d  >xGeom;.  pCons-
48d49 3e 70 47 65 6f 6d 20 3d 20 70 47 65 6f 6d 3b 0a  >pGeom = pGeom;.
48d4a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
48d4b 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  OK;.}../* .** Rt
48d4c 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
48d4d 65 20 6d 6f 64 75 6c 65 20 78 46 69 6c 74 65 72  e module xFilter
48d4e 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
48d4f 69 63 20 69 6e 74 20 72 74 72 65 65 46 69 6c 74  ic int rtreeFilt
48d50 65 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  er(.  sqlite3_vt
48d51 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62  ab_cursor *pVtab
48d52 43 75 72 73 6f 72 2c 20 0a 20 20 69 6e 74 20 69  Cursor, .  int i
48d53 64 78 4e 75 6d 2c 20 63 6f 6e 73 74 20 63 68 61  dxNum, const cha
48d54 72 20 2a 69 64 78 53 74 72 2c 0a 20 20 69 6e 74  r *idxStr,.  int
48d55 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
48d56 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
48d57 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d   Rtree *pRtree =
48d58 20 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 43   (Rtree *)pVtabC
48d59 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20  ursor->pVtab;.  
48d5a 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73  RtreeCursor *pCs
48d5b 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72  r = (RtreeCursor
48d5c 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0a   *)pVtabCursor;.
48d5d 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52  .  RtreeNode *pR
48d5e 6f 6f 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  oot = 0;.  int i
48d5f 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  i;.  int rc = SQ
48d60 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 72 74 72 65  LITE_OK;..  rtre
48d61 65 52 65 66 65 72 65 6e 63 65 28 70 52 74 72 65  eReference(pRtre
48d62 65 29 3b 0a 0a 20 20 66 72 65 65 43 75 72 73 6f  e);..  freeCurso
48d63 72 43 6f 6e 73 74 72 61 69 6e 74 73 28 70 43 73  rConstraints(pCs
48d64 72 29 3b 0a 20 20 70 43 73 72 2d 3e 69 53 74 72  r);.  pCsr->iStr
48d65 61 74 65 67 79 20 3d 20 69 64 78 4e 75 6d 3b 0a  ategy = idxNum;.
48d66 0a 20 20 69 66 28 20 69 64 78 4e 75 6d 3d 3d 31  .  if( idxNum==1
48d67 20 29 7b 0a 20 20 20 20 2f 2a 20 53 70 65 63 69   ){.    /* Speci
48d68 61 6c 20 63 61 73 65 20 2d 20 6c 6f 6f 6b 75 70  al case - lookup
48d69 20 62 79 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20   by rowid. */.  
48d6a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65    RtreeNode *pLe
48d6b 61 66 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  af;        /* Le
48d6c 61 66 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20  af on which the 
48d6d 72 65 71 75 69 72 65 64 20 63 65 6c 6c 20 72 65  required cell re
48d6e 73 69 64 65 73 20 2a 2f 0a 20 20 20 20 69 36 34  sides */.    i64
48d6f 20 69 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65   iRowid = sqlite
48d70 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72  3_value_int64(ar
48d71 67 76 5b 30 5d 29 3b 0a 20 20 20 20 72 63 20 3d  gv[0]);.    rc =
48d72 20 66 69 6e 64 4c 65 61 66 4e 6f 64 65 28 70 52   findLeafNode(pR
48d73 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 26 70  tree, iRowid, &p
48d74 4c 65 61 66 29 3b 0a 20 20 20 20 70 43 73 72 2d  Leaf);.    pCsr-
48d75 3e 70 4e 6f 64 65 20 3d 20 70 4c 65 61 66 3b 20  >pNode = pLeaf; 
48d76 0a 20 20 20 20 69 66 28 20 70 4c 65 61 66 20 29  .    if( pLeaf )
48d77 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
48d78 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
48d79 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64 65  .      rc = node
48d7a 52 6f 77 69 64 49 6e 64 65 78 28 70 52 74 72 65  RowidIndex(pRtre
48d7b 65 2c 20 70 4c 65 61 66 2c 20 69 52 6f 77 69 64  e, pLeaf, iRowid
48d7c 2c 20 26 70 43 73 72 2d 3e 69 43 65 6c 6c 29 3b  , &pCsr->iCell);
48d7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
48d7e 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 63 61      /* Normal ca
48d7f 73 65 20 2d 20 72 2d 74 72 65 65 20 73 63 61 6e  se - r-tree scan
48d80 2e 20 53 65 74 20 75 70 20 74 68 65 20 52 74 72  . Set up the Rtr
48d81 65 65 43 75 72 73 6f 72 2e 61 43 6f 6e 73 74 72  eeCursor.aConstr
48d82 61 69 6e 74 20 61 72 72 61 79 20 0a 20 20 20 20  aint array .    
48d83 2a 2a 20 77 69 74 68 20 74 68 65 20 63 6f 6e 66  ** with the conf
48d84 69 67 75 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  igured constrain
48d85 74 73 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ts. .    */.    
48d86 69 66 28 20 61 72 67 63 3e 30 20 29 7b 0a 20 20  if( argc>0 ){.  
48d87 20 20 20 20 70 43 73 72 2d 3e 61 43 6f 6e 73 74      pCsr->aConst
48d88 72 61 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f  raint = sqlite3_
48d89 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74  malloc(sizeof(Rt
48d8a 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 29 2a 61  reeConstraint)*a
48d8b 72 67 63 29 3b 0a 20 20 20 20 20 20 70 43 73 72  rgc);.      pCsr
48d8c 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20  ->nConstraint = 
48d8d 61 72 67 63 3b 0a 20 20 20 20 20 20 69 66 28 20  argc;.      if( 
48d8e 21 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69  !pCsr->aConstrai
48d8f 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  nt ){.        rc
48d90 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
48d91 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
48d92 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 43 73        memset(pCs
48d93 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 2c 20  r->aConstraint, 
48d94 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43  0, sizeof(RtreeC
48d95 6f 6e 73 74 72 61 69 6e 74 29 2a 61 72 67 63 29  onstraint)*argc)
48d96 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
48d97 28 20 28 69 64 78 53 74 72 3d 3d 30 20 26 26 20  ( (idxStr==0 && 
48d98 61 72 67 63 3d 3d 30 29 0a 20 20 20 20 20 20 20  argc==0).       
48d99 20 20 20 20 20 20 20 20 20 7c 7c 20 28 69 64 78           || (idx
48d9a 53 74 72 20 26 26 20 28 69 6e 74 29 73 74 72 6c  Str && (int)strl
48d9b 65 6e 28 69 64 78 53 74 72 29 3d 3d 61 72 67 63  en(idxStr)==argc
48d9c 2a 32 29 20 29 3b 0a 20 20 20 20 20 20 20 20 66  *2) );.        f
48d9d 6f 72 28 69 69 3d 30 3b 20 69 69 3c 61 72 67 63  or(ii=0; ii<argc
48d9e 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ; ii++){.       
48d9f 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69     RtreeConstrai
48da0 6e 74 20 2a 70 20 3d 20 26 70 43 73 72 2d 3e 61  nt *p = &pCsr->a
48da1 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a  Constraint[ii];.
48da2 20 20 20 20 20 20 20 20 20 20 70 2d 3e 6f 70 20            p->op 
48da3 3d 20 69 64 78 53 74 72 5b 69 69 2a 32 5d 3b 0a  = idxStr[ii*2];.
48da4 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 43 6f            p->iCo
48da5 6f 72 64 20 3d 20 69 64 78 53 74 72 5b 69 69 2a  ord = idxStr[ii*
48da6 32 2b 31 5d 2d 27 61 27 3b 0a 20 20 20 20 20 20  2+1]-'a';.      
48da7 20 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 52      if( p->op==R
48da8 54 52 45 45 5f 4d 41 54 43 48 20 29 7b 0a 20 20  TREE_MATCH ){.  
48da9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 4d            /* A M
48daa 41 54 43 48 20 6f 70 65 72 61 74 6f 72 2e 20 54  ATCH operator. T
48dab 68 65 20 72 69 67 68 74 2d 68 61 6e 64 2d 73 69  he right-hand-si
48dac 64 65 20 6d 75 73 74 20 62 65 20 61 20 62 6c 6f  de must be a blo
48dad 62 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 20  b that.         
48dae 20 20 20 2a 2a 20 63 61 6e 20 62 65 20 63 61 73     ** can be cas
48daf 74 20 69 6e 74 6f 20 61 6e 20 52 74 72 65 65 4d  t into an RtreeM
48db0 61 74 63 68 41 72 67 20 6f 62 6a 65 63 74 2e 20  atchArg object. 
48db1 4f 6e 65 20 63 72 65 61 74 65 64 20 75 73 69 6e  One created usin
48db2 67 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  g.            **
48db3 20 61 6e 20 73 71 6c 69 74 65 33 5f 72 74 72 65   an sqlite3_rtre
48db4 65 5f 67 65 6f 6d 65 74 72 79 5f 63 61 6c 6c 62  e_geometry_callb
48db5 61 63 6b 28 29 20 53 51 4c 20 75 73 65 72 20 66  ack() SQL user f
48db6 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20  unction..       
48db7 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
48db8 20 20 20 20 72 63 20 3d 20 64 65 73 65 72 69 61      rc = deseria
48db9 6c 69 7a 65 47 65 6f 6d 65 74 72 79 28 61 72 67  lizeGeometry(arg
48dba 76 5b 69 69 5d 2c 20 70 29 3b 0a 20 20 20 20 20  v[ii], p);.     
48dbb 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
48dbc 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
48dbd 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
48dbe 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
48dbf 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
48dc0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 52 54  #ifdef SQLITE_RT
48dc1 52 45 45 5f 49 4e 54 5f 4f 4e 4c 59 0a 20 20 20  REE_INT_ONLY.   
48dc2 20 20 20 20 20 20 20 20 20 70 2d 3e 72 56 61 6c           p->rVal
48dc3 75 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ue = sqlite3_val
48dc4 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 69 69  ue_int64(argv[ii
48dc5 5d 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20  ]);.#else.      
48dc6 20 20 20 20 20 20 70 2d 3e 72 56 61 6c 75 65 20        p->rValue 
48dc7 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
48dc8 64 6f 75 62 6c 65 28 61 72 67 76 5b 69 69 5d 29  double(argv[ii])
48dc9 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  ;.#endif.       
48dca 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
48dcb 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a       }.    }.  .
48dcc 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
48dcd 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70  TE_OK ){.      p
48dce 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a  Csr->pNode = 0;.
48dcf 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41        rc = nodeA
48dd0 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 31  cquire(pRtree, 1
48dd1 2c 20 30 2c 20 26 70 52 6f 6f 74 29 3b 0a 20 20  , 0, &pRoot);.  
48dd2 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
48dd3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
48dd4 20 20 20 69 6e 74 20 69 73 45 6f 66 20 3d 20 31     int isEof = 1
48dd5 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43 65 6c  ;.      int nCel
48dd6 6c 20 3d 20 4e 43 45 4c 4c 28 70 52 6f 6f 74 29  l = NCELL(pRoot)
48dd7 3b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 70 4e  ;.      pCsr->pN
48dd8 6f 64 65 20 3d 20 70 52 6f 6f 74 3b 0a 20 20 20  ode = pRoot;.   
48dd9 20 20 20 66 6f 72 28 70 43 73 72 2d 3e 69 43 65     for(pCsr->iCe
48dda 6c 6c 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  ll=0; rc==SQLITE
48ddb 5f 4f 4b 20 26 26 20 70 43 73 72 2d 3e 69 43 65  _OK && pCsr->iCe
48ddc 6c 6c 3c 6e 43 65 6c 6c 3b 20 70 43 73 72 2d 3e  ll<nCell; pCsr->
48ddd 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20  iCell++){.      
48dde 20 20 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e    assert( pCsr->
48ddf 70 4e 6f 64 65 3d 3d 70 52 6f 6f 74 20 29 3b 0a  pNode==pRoot );.
48de0 20 20 20 20 20 20 20 20 72 63 20 3d 20 64 65 73          rc = des
48de1 63 65 6e 64 54 6f 43 65 6c 6c 28 70 52 74 72 65  cendToCell(pRtre
48de2 65 2c 20 70 43 73 72 2c 20 70 52 74 72 65 65 2d  e, pCsr, pRtree-
48de3 3e 69 44 65 70 74 68 2c 20 26 69 73 45 6f 66 29  >iDepth, &isEof)
48de4 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 69  ;.        if( !i
48de5 73 45 6f 66 20 29 7b 0a 20 20 20 20 20 20 20 20  sEof ){.        
48de6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
48de7 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
48de8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
48de9 4f 4b 20 26 26 20 69 73 45 6f 66 20 29 7b 0a 20  OK && isEof ){. 
48dea 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
48deb 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 70 52 6f 6f  Csr->pNode==pRoo
48dec 74 20 29 3b 0a 20 20 20 20 20 20 20 20 6e 6f 64  t );.        nod
48ded 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
48dee 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20   pRoot);.       
48def 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 30   pCsr->pNode = 0
48df0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
48df1 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
48df2 54 45 5f 4f 4b 20 7c 7c 20 21 70 43 73 72 2d 3e  TE_OK || !pCsr->
48df3 70 4e 6f 64 65 20 7c 7c 20 70 43 73 72 2d 3e 69  pNode || pCsr->i
48df4 43 65 6c 6c 3c 4e 43 45 4c 4c 28 70 43 73 72 2d  Cell<NCELL(pCsr-
48df5 3e 70 4e 6f 64 65 29 20 29 3b 0a 20 20 20 20 7d  >pNode) );.    }
48df6 0a 20 20 7d 0a 0a 20 20 72 74 72 65 65 52 65 6c  .  }..  rtreeRel
48df7 65 61 73 65 28 70 52 74 72 65 65 29 3b 0a 20 20  ease(pRtree);.  
48df8 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
48df9 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
48dfa 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
48dfb 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f 64  BestIndex method
48dfc 2e 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65  . There are thre
48dfd 65 0a 2a 2a 20 74 61 62 6c 65 20 73 63 61 6e 20  e.** table scan 
48dfe 73 74 72 61 74 65 67 69 65 73 20 74 6f 20 63 68  strategies to ch
48dff 6f 6f 73 65 20 66 72 6f 6d 20 28 69 6e 20 6f 72  oose from (in or
48e00 64 65 72 20 66 72 6f 6d 20 6d 6f 73 74 20 74 6f  der from most to
48e01 20 0a 2a 2a 20 6c 65 61 73 74 20 64 65 73 69 72   .** least desir
48e02 61 62 6c 65 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 69  able):.**.**   i
48e03 64 78 4e 75 6d 20 20 20 20 20 69 64 78 53 74 72  dxNum     idxStr
48e04 20 20 20 20 20 20 20 20 53 74 72 61 74 65 67 79          Strategy
48e05 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**   ----------
48e06 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
48e07 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
48e08 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 31 20  ------.**     1 
48e09 20 20 20 20 20 20 20 55 6e 75 73 65 64 20 20 20         Unused   
48e0a 20 20 20 20 20 44 69 72 65 63 74 20 6c 6f 6f 6b       Direct look
48e0b 75 70 20 62 79 20 72 6f 77 69 64 2e 0a 2a 2a 20  up by rowid..** 
48e0c 20 20 20 20 32 20 20 20 20 20 20 20 20 53 65 65      2        See
48e0d 20 62 65 6c 6f 77 20 20 20 20 20 52 2d 74 72 65   below     R-tre
48e0e 65 20 71 75 65 72 79 20 6f 72 20 66 75 6c 6c 2d  e query or full-
48e0f 74 61 62 6c 65 20 73 63 61 6e 2e 0a 2a 2a 20 20  table scan..**  
48e10 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
48e11 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
48e12 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
48e13 2d 0a 2a 2a 0a 2a 2a 20 49 66 20 73 74 72 61 74  -.**.** If strat
48e14 65 67 79 20 31 20 69 73 20 75 73 65 64 2c 20 74  egy 1 is used, t
48e15 68 65 6e 20 69 64 78 53 74 72 20 69 73 20 6e 6f  hen idxStr is no
48e16 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 20 49 66  t meaningful. If
48e17 20 73 74 72 61 74 65 67 79 0a 2a 2a 20 32 20 69   strategy.** 2 i
48e18 73 20 75 73 65 64 2c 20 69 64 78 53 74 72 20 69  s used, idxStr i
48e19 73 20 66 6f 72 6d 61 74 74 65 64 20 74 6f 20 63  s formatted to c
48e1a 6f 6e 74 61 69 6e 20 32 20 62 79 74 65 73 20 66  ontain 2 bytes f
48e1b 6f 72 20 65 61 63 68 20 0a 2a 2a 20 63 6f 6e 73  or each .** cons
48e1c 74 72 61 69 6e 74 20 75 73 65 64 2e 20 54 68 65  traint used. The
48e1d 20 66 69 72 73 74 20 74 77 6f 20 62 79 74 65 73   first two bytes
48e1e 20 6f 66 20 69 64 78 53 74 72 20 63 6f 72 72 65   of idxStr corre
48e1f 73 70 6f 6e 64 20 74 6f 20 0a 2a 2a 20 74 68 65  spond to .** the
48e20 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 73   constraint in s
48e21 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
48e22 6f 2e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  o.aConstraintUsa
48e23 67 65 5b 5d 20 77 69 74 68 0a 2a 2a 20 28 61 72  ge[] with.** (ar
48e24 67 76 49 6e 64 65 78 3d 3d 31 29 20 65 74 63 2e  gvIndex==1) etc.
48e25 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
48e26 20 6f 66 20 65 61 63 68 20 70 61 69 72 20 6f 66   of each pair of
48e27 20 62 79 74 65 73 20 69 6e 20 69 64 78 53 74 72   bytes in idxStr
48e28 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
48e29 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 6f 70  constraint.** op
48e2a 65 72 61 74 6f 72 20 61 73 20 66 6f 6c 6c 6f 77  erator as follow
48e2b 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 70 65 72 61  s:.**.**   Opera
48e2c 74 6f 72 20 20 20 20 42 79 74 65 20 56 61 6c 75  tor    Byte Valu
48e2d 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  e.**   ---------
48e2e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
48e2f 20 20 20 20 20 20 3d 20 20 20 20 20 20 20 20 30        =        0
48e30 78 34 31 20 28 27 41 27 29 0a 2a 2a 20 20 20 20  x41 ('A').**    
48e31 20 3c 3d 20 20 20 20 20 20 20 20 30 78 34 32 20   <=        0x42 
48e32 28 27 42 27 29 0a 2a 2a 20 20 20 20 20 20 3c 20  ('B').**      < 
48e33 20 20 20 20 20 20 20 30 78 34 33 20 28 27 43 27         0x43 ('C'
48e34 29 0a 2a 2a 20 20 20 20 20 3e 3d 20 20 20 20 20  ).**     >=     
48e35 20 20 20 30 78 34 34 20 28 27 44 27 29 0a 2a 2a     0x44 ('D').**
48e36 20 20 20 20 20 20 3e 20 20 20 20 20 20 20 20 30        >        0
48e37 78 34 35 20 28 27 45 27 29 0a 2a 2a 20 20 20 4d  x45 ('E').**   M
48e38 41 54 43 48 20 20 20 20 20 20 20 30 78 34 36 20  ATCH       0x46 
48e39 28 27 46 27 29 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d  ('F').**   -----
48e3a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
48e3b 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  -.**.** The seco
48e3c 6e 64 20 6f 66 20 65 61 63 68 20 70 61 69 72 20  nd of each pair 
48e3d 6f 66 20 62 79 74 65 73 20 69 64 65 6e 74 69 66  of bytes identif
48e3e 69 65 73 20 74 68 65 20 63 6f 6f 72 64 69 6e 61  ies the coordina
48e3f 74 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f 20  te column.** to 
48e40 77 68 69 63 68 20 74 68 65 20 63 6f 6e 73 74 72  which the constr
48e41 61 69 6e 74 20 61 70 70 6c 69 65 73 2e 20 54 68  aint applies. Th
48e42 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6f 72 64  e leftmost coord
48e43 69 6e 61 74 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  inate column.** 
48e44 69 73 20 27 61 27 2c 20 74 68 65 20 73 65 63 6f  is 'a', the seco
48e45 6e 64 20 66 72 6f 6d 20 74 68 65 20 6c 65 66 74  nd from the left
48e46 20 27 62 27 20 65 74 63 2e 0a 2a 2f 0a 73 74 61   'b' etc..*/.sta
48e47 74 69 63 20 69 6e 74 20 72 74 72 65 65 42 65 73  tic int rtreeBes
48e48 74 49 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 76  tIndex(sqlite3_v
48e49 74 61 62 20 2a 74 61 62 2c 20 73 71 6c 69 74 65  tab *tab, sqlite
48e4a 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49  3_index_info *pI
48e4b 64 78 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 72  dxInfo){.  int r
48e4c 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
48e4d 20 69 6e 74 20 69 69 3b 0a 0a 20 20 69 6e 74 20   int ii;..  int 
48e4e 69 49 64 78 20 3d 20 30 3b 0a 20 20 63 68 61 72  iIdx = 0;.  char
48e4f 20 7a 49 64 78 53 74 72 5b 52 54 52 45 45 5f 4d   zIdxStr[RTREE_M
48e50 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 38 2b  AX_DIMENSIONS*8+
48e51 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 49 64  1];.  memset(zId
48e52 78 53 74 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  xStr, 0, sizeof(
48e53 7a 49 64 78 53 74 72 29 29 3b 0a 20 20 55 4e 55  zIdxStr));.  UNU
48e54 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 74 61  SED_PARAMETER(ta
48e55 62 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  b);..  assert( p
48e56 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 3d  IdxInfo->idxStr=
48e57 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30  =0 );.  for(ii=0
48e58 3b 20 69 69 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e  ; ii<pIdxInfo->n
48e59 43 6f 6e 73 74 72 61 69 6e 74 20 26 26 20 69 49  Constraint && iI
48e5a 64 78 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28  dx<(int)(sizeof(
48e5b 7a 49 64 78 53 74 72 29 2d 31 29 3b 20 69 69 2b  zIdxStr)-1); ii+
48e5c 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 73  +){.    struct s
48e5d 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
48e5e 73 74 72 61 69 6e 74 20 2a 70 20 3d 20 26 70 49  straint *p = &pI
48e5f 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61  dxInfo->aConstra
48e60 69 6e 74 5b 69 69 5d 3b 0a 0a 20 20 20 20 69 66  int[ii];..    if
48e61 28 20 70 2d 3e 75 73 61 62 6c 65 20 26 26 20 70  ( p->usable && p
48e62 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20  ->iColumn==0 && 
48e63 70 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e  p->op==SQLITE_IN
48e64 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45  DEX_CONSTRAINT_E
48e65 51 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57 65  Q ){.      /* We
48e66 20 68 61 76 65 20 61 6e 20 65 71 75 61 6c 69 74   have an equalit
48e67 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20  y constraint on 
48e68 74 68 65 20 72 6f 77 69 64 2e 20 55 73 65 20 73  the rowid. Use s
48e69 74 72 61 74 65 67 79 20 31 2e 20 2a 2f 0a 20 20  trategy 1. */.  
48e6a 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20      int jj;.    
48e6b 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 69    for(jj=0; jj<i
48e6c 69 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  i; jj++){.      
48e6d 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e    pIdxInfo->aCon
48e6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 6a 6a 5d  straintUsage[jj]
48e6f 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 30 3b 0a  .argvIndex = 0;.
48e70 20 20 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f          pIdxInfo
48e71 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
48e72 67 65 5b 6a 6a 5d 2e 6f 6d 69 74 20 3d 20 30 3b  ge[jj].omit = 0;
48e73 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
48e74 49 64 78 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20  IdxInfo->idxNum 
48e75 3d 20 31 3b 0a 20 20 20 20 20 20 70 49 64 78 49  = 1;.      pIdxI
48e76 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
48e77 55 73 61 67 65 5b 69 69 5d 2e 61 72 67 76 49 6e  Usage[ii].argvIn
48e78 64 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 70  dex = 1;.      p
48e79 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
48e7a 61 69 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e 6f 6d  aintUsage[jj].om
48e7b 69 74 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 2f  it = 1;..      /
48e7c 2a 20 54 68 69 73 20 73 74 72 61 74 65 67 79 20  * This strategy 
48e7d 69 6e 76 6f 6c 76 65 73 20 61 20 74 77 6f 20 72  involves a two r
48e7e 6f 77 69 64 20 6c 6f 6f 6b 75 70 73 20 6f 6e 20  owid lookups on 
48e7f 61 6e 20 42 2d 54 72 65 65 20 73 74 72 75 63 74  an B-Tree struct
48e80 75 72 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 6e  ures.      ** an
48e81 64 20 74 68 65 6e 20 61 20 6c 69 6e 65 61 72 20  d then a linear 
48e82 73 65 61 72 63 68 20 6f 66 20 61 6e 20 52 2d 54  search of an R-T
48e83 72 65 65 20 6e 6f 64 65 2e 20 54 68 69 73 20 73  ree node. This s
48e84 68 6f 75 6c 64 20 62 65 20 0a 20 20 20 20 20 20  hould be .      
48e85 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 20 61 6c  ** considered al
48e86 6d 6f 73 74 20 61 73 20 71 75 69 63 6b 20 61 73  most as quick as
48e87 20 61 20 64 69 72 65 63 74 20 72 6f 77 69 64 20   a direct rowid 
48e88 6c 6f 6f 6b 75 70 20 28 66 6f 72 20 77 68 69 63  lookup (for whic
48e89 68 20 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69  h .      ** sqli
48e8a 74 65 20 75 73 65 73 20 61 6e 20 69 6e 74 65 72  te uses an inter
48e8b 6e 61 6c 20 63 6f 73 74 20 6f 66 20 30 2e 30 29  nal cost of 0.0)
48e8c 2e 0a 20 20 20 20 20 20 2a 2f 20 0a 20 20 20 20  ..      */ .    
48e8d 20 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69    pIdxInfo->esti
48e8e 6d 61 74 65 64 43 6f 73 74 20 3d 20 31 30 2e 30  matedCost = 10.0
48e8f 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
48e90 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
48e91 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73 61 62  .    if( p->usab
48e92 6c 65 20 26 26 20 28 70 2d 3e 69 43 6f 6c 75 6d  le && (p->iColum
48e93 6e 3e 30 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 53 51  n>0 || p->op==SQ
48e94 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
48e95 52 41 49 4e 54 5f 4d 41 54 43 48 29 20 29 7b 0a  RAINT_MATCH) ){.
48e96 20 20 20 20 20 20 75 38 20 6f 70 3b 0a 20 20 20        u8 op;.   
48e97 20 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70     switch( p->op
48e98 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65   ){.        case
48e99 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
48e9a 4e 53 54 52 41 49 4e 54 5f 45 51 3a 20 6f 70 20  NSTRAINT_EQ: op 
48e9b 3d 20 52 54 52 45 45 5f 45 51 3b 20 62 72 65 61  = RTREE_EQ; brea
48e9c 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
48e9d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
48e9e 53 54 52 41 49 4e 54 5f 47 54 3a 20 6f 70 20 3d  STRAINT_GT: op =
48e9f 20 52 54 52 45 45 5f 47 54 3b 20 62 72 65 61 6b   RTREE_GT; break
48ea0 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53  ;.        case S
48ea1 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
48ea2 54 52 41 49 4e 54 5f 4c 45 3a 20 6f 70 20 3d 20  TRAINT_LE: op = 
48ea3 52 54 52 45 45 5f 4c 45 3b 20 62 72 65 61 6b 3b  RTREE_LE; break;
48ea4 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51  .        case SQ
48ea5 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
48ea6 52 41 49 4e 54 5f 4c 54 3a 20 6f 70 20 3d 20 52  RAINT_LT: op = R
48ea7 54 52 45 45 5f 4c 54 3b 20 62 72 65 61 6b 3b 0a  TREE_LT; break;.
48ea8 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51 4c          case SQL
48ea9 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
48eaa 41 49 4e 54 5f 47 45 3a 20 6f 70 20 3d 20 52 54  AINT_GE: op = RT
48eab 52 45 45 5f 47 45 3b 20 62 72 65 61 6b 3b 0a 20  REE_GE; break;. 
48eac 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a         default:.
48ead 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
48eae 28 20 70 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f  ( p->op==SQLITE_
48eaf 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
48eb0 5f 4d 41 54 43 48 20 29 3b 0a 20 20 20 20 20 20  _MATCH );.      
48eb1 20 20 20 20 6f 70 20 3d 20 52 54 52 45 45 5f 4d      op = RTREE_M
48eb2 41 54 43 48 3b 20 0a 20 20 20 20 20 20 20 20 20  ATCH; .         
48eb3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
48eb4 20 20 20 20 20 20 7a 49 64 78 53 74 72 5b 69 49        zIdxStr[iI
48eb5 64 78 2b 2b 5d 20 3d 20 6f 70 3b 0a 20 20 20 20  dx++] = op;.    
48eb6 20 20 7a 49 64 78 53 74 72 5b 69 49 64 78 2b 2b    zIdxStr[iIdx++
48eb7 5d 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 2d  ] = p->iColumn -
48eb8 20 31 20 2b 20 27 61 27 3b 0a 20 20 20 20 20 20   1 + 'a';.      
48eb9 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  pIdxInfo->aConst
48eba 72 61 69 6e 74 55 73 61 67 65 5b 69 69 5d 2e 61  raintUsage[ii].a
48ebb 72 67 76 49 6e 64 65 78 20 3d 20 28 69 49 64 78  rgvIndex = (iIdx
48ebc 2f 32 29 3b 0a 20 20 20 20 20 20 70 49 64 78 49  /2);.      pIdxI
48ebd 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
48ebe 55 73 61 67 65 5b 69 69 5d 2e 6f 6d 69 74 20 3d  Usage[ii].omit =
48ebf 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   1;.    }.  }.. 
48ec0 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 4e 75   pIdxInfo->idxNu
48ec1 6d 20 3d 20 32 3b 0a 20 20 70 49 64 78 49 6e 66  m = 2;.  pIdxInf
48ec2 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78  o->needToFreeIdx
48ec3 53 74 72 20 3d 20 31 3b 0a 20 20 69 66 28 20 69  Str = 1;.  if( i
48ec4 49 64 78 3e 30 20 26 26 20 30 3d 3d 28 70 49 64  Idx>0 && 0==(pId
48ec5 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 20 3d 20  xInfo->idxStr = 
48ec6 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
48ec7 22 25 73 22 2c 20 7a 49 64 78 53 74 72 29 29 20  "%s", zIdxStr)) 
48ec8 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
48ec9 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
48eca 20 20 61 73 73 65 72 74 28 20 69 49 64 78 3e 3d    assert( iIdx>=
48ecb 30 20 29 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d  0 );.  pIdxInfo-
48ecc 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d  >estimatedCost =
48ecd 20 28 32 30 30 30 30 30 30 2e 30 20 2f 20 28 64   (2000000.0 / (d
48ece 6f 75 62 6c 65 29 28 69 49 64 78 20 2b 20 31 29  ouble)(iIdx + 1)
48ecf 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
48ed0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
48ed1 74 68 65 20 4e 2d 64 69 6d 65 6e 73 69 6f 6e 61  the N-dimensiona
48ed2 6c 20 76 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  l volumn of the 
48ed3 63 65 6c 6c 20 73 74 6f 72 65 64 20 69 6e 20 2a  cell stored in *
48ed4 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 74 72  p..*/.static Rtr
48ed5 65 65 44 56 61 6c 75 65 20 63 65 6c 6c 41 72 65  eeDValue cellAre
48ed6 61 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  a(Rtree *pRtree,
48ed7 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 29 7b 0a   RtreeCell *p){.
48ed8 20 20 52 74 72 65 65 44 56 61 6c 75 65 20 61 72    RtreeDValue ar
48ed9 65 61 20 3d 20 28 52 74 72 65 65 44 56 61 6c 75  ea = (RtreeDValu
48eda 65 29 31 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  e)1;.  int ii;. 
48edb 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70   for(ii=0; ii<(p
48edc 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20  Rtree->nDim*2); 
48edd 69 69 2b 3d 32 29 7b 0a 20 20 20 20 61 72 65 61  ii+=2){.    area
48ede 20 3d 20 28 61 72 65 61 20 2a 20 28 44 43 4f 4f   = (area * (DCOO
48edf 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b  RD(p->aCoord[ii+
48ee0 31 5d 29 20 2d 20 44 43 4f 4f 52 44 28 70 2d 3e  1]) - DCOORD(p->
48ee1 61 43 6f 6f 72 64 5b 69 69 5d 29 29 29 3b 0a 20  aCoord[ii])));. 
48ee2 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 72 65 61   }.  return area
48ee3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
48ee4 6e 20 74 68 65 20 6d 61 72 67 69 6e 20 6c 65 6e  n the margin len
48ee5 67 74 68 20 6f 66 20 63 65 6c 6c 20 70 2e 20 54  gth of cell p. T
48ee6 68 65 20 6d 61 72 67 69 6e 20 6c 65 6e 67 74 68  he margin length
48ee7 20 69 73 20 74 68 65 20 73 75 6d 0a 2a 2a 20 6f   is the sum.** o
48ee8 66 20 74 68 65 20 6f 62 6a 65 63 74 73 20 73 69  f the objects si
48ee9 7a 65 20 69 6e 20 65 61 63 68 20 64 69 6d 65 6e  ze in each dimen
48eea 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  sion..*/.static 
48eeb 52 74 72 65 65 44 56 61 6c 75 65 20 63 65 6c 6c  RtreeDValue cell
48eec 4d 61 72 67 69 6e 28 52 74 72 65 65 20 2a 70 52  Margin(Rtree *pR
48eed 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20  tree, RtreeCell 
48eee 2a 70 29 7b 0a 20 20 52 74 72 65 65 44 56 61 6c  *p){.  RtreeDVal
48eef 75 65 20 6d 61 72 67 69 6e 20 3d 20 28 52 74 72  ue margin = (Rtr
48ef0 65 65 44 56 61 6c 75 65 29 30 3b 0a 20 20 69 6e  eeDValue)0;.  in
48ef1 74 20 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30  t ii;.  for(ii=0
48ef2 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44  ; ii<(pRtree->nD
48ef3 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20  im*2); ii+=2){. 
48ef4 20 20 20 6d 61 72 67 69 6e 20 2b 3d 20 28 44 43     margin += (DC
48ef5 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 69  OORD(p->aCoord[i
48ef6 69 2b 31 5d 29 20 2d 20 44 43 4f 4f 52 44 28 70  i+1]) - DCOORD(p
48ef7 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29 29 3b 0a  ->aCoord[ii]));.
48ef8 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61 72    }.  return mar
48ef9 67 69 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74  gin;.}../*.** St
48efa 6f 72 65 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66  ore the union of
48efb 20 63 65 6c 6c 73 20 70 31 20 61 6e 64 20 70 32   cells p1 and p2
48efc 20 69 6e 20 70 31 2e 0a 2a 2f 0a 73 74 61 74 69   in p1..*/.stati
48efd 63 20 76 6f 69 64 20 63 65 6c 6c 55 6e 69 6f 6e  c void cellUnion
48efe 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
48eff 52 74 72 65 65 43 65 6c 6c 20 2a 70 31 2c 20 52  RtreeCell *p1, R
48f00 74 72 65 65 43 65 6c 6c 20 2a 70 32 29 7b 0a 20  treeCell *p2){. 
48f01 20 69 6e 74 20 69 69 3b 0a 20 20 69 66 28 20 70   int ii;.  if( p
48f02 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70  Rtree->eCoordTyp
48f03 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52  e==RTREE_COORD_R
48f04 45 41 4c 33 32 20 29 7b 0a 20 20 20 20 66 6f 72  EAL32 ){.    for
48f05 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65  (ii=0; ii<(pRtre
48f06 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d  e->nDim*2); ii+=
48f07 32 29 7b 0a 20 20 20 20 20 20 70 31 2d 3e 61 43  2){.      p1->aC
48f08 6f 6f 72 64 5b 69 69 5d 2e 66 20 3d 20 4d 49 4e  oord[ii].f = MIN
48f09 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e  (p1->aCoord[ii].
48f0a 66 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69  f, p2->aCoord[ii
48f0b 5d 2e 66 29 3b 0a 20 20 20 20 20 20 70 31 2d 3e  ].f);.      p1->
48f0c 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 20 3d  aCoord[ii+1].f =
48f0d 20 4d 41 58 28 70 31 2d 3e 61 43 6f 6f 72 64 5b   MAX(p1->aCoord[
48f0e 69 69 2b 31 5d 2e 66 2c 20 70 32 2d 3e 61 43 6f  ii+1].f, p2->aCo
48f0f 6f 72 64 5b 69 69 2b 31 5d 2e 66 29 3b 0a 20 20  ord[ii+1].f);.  
48f10 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
48f11 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70   for(ii=0; ii<(p
48f12 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20  Rtree->nDim*2); 
48f13 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 70 31  ii+=2){.      p1
48f14 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 20 3d  ->aCoord[ii].i =
48f15 20 4d 49 4e 28 70 31 2d 3e 61 43 6f 6f 72 64 5b   MIN(p1->aCoord[
48f16 69 69 5d 2e 69 2c 20 70 32 2d 3e 61 43 6f 6f 72  ii].i, p2->aCoor
48f17 64 5b 69 69 5d 2e 69 29 3b 0a 20 20 20 20 20 20  d[ii].i);.      
48f18 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  p1->aCoord[ii+1]
48f19 2e 69 20 3d 20 4d 41 58 28 70 31 2d 3e 61 43 6f  .i = MAX(p1->aCo
48f1a 6f 72 64 5b 69 69 2b 31 5d 2e 69 2c 20 70 32 2d  ord[ii+1].i, p2-
48f1b 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 29  >aCoord[ii+1].i)
48f1c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
48f1d 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
48f1e 20 69 66 20 74 68 65 20 61 72 65 61 20 63 6f 76   if the area cov
48f1f 65 72 65 64 20 62 79 20 70 32 20 69 73 20 61 20  ered by p2 is a 
48f20 73 75 62 73 65 74 20 6f 66 20 74 68 65 20 61 72  subset of the ar
48f21 65 61 20 63 6f 76 65 72 65 64 0a 2a 2a 20 62 79  ea covered.** by
48f22 20 70 31 2e 20 46 61 6c 73 65 20 6f 74 68 65 72   p1. False other
48f23 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  wise..*/.static 
48f24 69 6e 74 20 63 65 6c 6c 43 6f 6e 74 61 69 6e 73  int cellContains
48f25 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
48f26 52 74 72 65 65 43 65 6c 6c 20 2a 70 31 2c 20 52  RtreeCell *p1, R
48f27 74 72 65 65 43 65 6c 6c 20 2a 70 32 29 7b 0a 20  treeCell *p2){. 
48f28 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 69   int ii;.  int i
48f29 73 49 6e 74 20 3d 20 28 70 52 74 72 65 65 2d 3e  sInt = (pRtree->
48f2a 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45  eCoordType==RTRE
48f2b 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 29 3b 0a  E_COORD_INT32);.
48f2c 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28    for(ii=0; ii<(
48f2d 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b  pRtree->nDim*2);
48f2e 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 52 74 72   ii+=2){.    Rtr
48f2f 65 65 43 6f 6f 72 64 20 2a 61 31 20 3d 20 26 70  eeCoord *a1 = &p
48f30 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 3b 0a 20  1->aCoord[ii];. 
48f31 20 20 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 61     RtreeCoord *a
48f32 32 20 3d 20 26 70 32 2d 3e 61 43 6f 6f 72 64 5b  2 = &p2->aCoord[
48f33 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 28 21 69  ii];.    if( (!i
48f34 73 49 6e 74 20 26 26 20 28 61 32 5b 30 5d 2e 66  sInt && (a2[0].f
48f35 3c 61 31 5b 30 5d 2e 66 20 7c 7c 20 61 32 5b 31  <a1[0].f || a2[1
48f36 5d 2e 66 3e 61 31 5b 31 5d 2e 66 29 29 20 0a 20  ].f>a1[1].f)) . 
48f37 20 20 20 20 7c 7c 20 28 20 69 73 49 6e 74 20 26      || ( isInt &
48f38 26 20 28 61 32 5b 30 5d 2e 69 3c 61 31 5b 30 5d  & (a2[0].i<a1[0]
48f39 2e 69 20 7c 7c 20 61 32 5b 31 5d 2e 69 3e 61 31  .i || a2[1].i>a1
48f3a 5b 31 5d 2e 69 29 29 20 0a 20 20 20 20 29 7b 0a  [1].i)) .    ){.
48f3b 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
48f3c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
48f3d 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn 1;.}../*.** R
48f3e 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74  eturn the amount
48f3f 20 63 65 6c 6c 20 70 20 77 6f 75 6c 64 20 67 72   cell p would gr
48f40 6f 77 20 62 79 20 69 66 20 69 74 20 77 65 72 65  ow by if it were
48f41 20 75 6e 69 6f 6e 65 64 20 77 69 74 68 20 70 43   unioned with pC
48f42 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52  ell..*/.static R
48f43 74 72 65 65 44 56 61 6c 75 65 20 63 65 6c 6c 47  treeDValue cellG
48f44 72 6f 77 74 68 28 52 74 72 65 65 20 2a 70 52 74  rowth(Rtree *pRt
48f45 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a  ree, RtreeCell *
48f46 70 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43  p, RtreeCell *pC
48f47 65 6c 6c 29 7b 0a 20 20 52 74 72 65 65 44 56 61  ell){.  RtreeDVa
48f48 6c 75 65 20 61 72 65 61 3b 0a 20 20 52 74 72 65  lue area;.  Rtre
48f49 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 6d 65  eCell cell;.  me
48f4a 6d 63 70 79 28 26 63 65 6c 6c 2c 20 70 2c 20 73  mcpy(&cell, p, s
48f4b 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
48f4c 29 3b 0a 20 20 61 72 65 61 20 3d 20 63 65 6c 6c  );.  area = cell
48f4d 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 63 65  Area(pRtree, &ce
48f4e 6c 6c 29 3b 0a 20 20 63 65 6c 6c 55 6e 69 6f 6e  ll);.  cellUnion
48f4f 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20  (pRtree, &cell, 
48f50 70 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e  pCell);.  return
48f51 20 28 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65   (cellArea(pRtre
48f52 65 2c 20 26 63 65 6c 6c 29 2d 61 72 65 61 29 3b  e, &cell)-area);
48f53 0a 7d 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  .}..#if VARIANT_
48f54 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45  RSTARTREE_CHOOSE
48f55 53 55 42 54 52 45 45 20 7c 7c 20 56 41 52 49 41  SUBTREE || VARIA
48f56 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c  NT_RSTARTREE_SPL
48f57 49 54 0a 73 74 61 74 69 63 20 52 74 72 65 65 44  IT.static RtreeD
48f58 56 61 6c 75 65 20 63 65 6c 6c 4f 76 65 72 6c 61  Value cellOverla
48f59 70 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  p(.  Rtree *pRtr
48f5a 65 65 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c  ee, .  RtreeCell
48f5b 20 2a 70 2c 20 0a 20 20 52 74 72 65 65 43 65 6c   *p, .  RtreeCel
48f5c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74  l *aCell, .  int
48f5d 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 69   nCell, .  int i
48f5e 45 78 63 6c 75 64 65 0a 29 7b 0a 20 20 69 6e 74  Exclude.){.  int
48f5f 20 69 69 3b 0a 20 20 52 74 72 65 65 44 56 61 6c   ii;.  RtreeDVal
48f60 75 65 20 6f 76 65 72 6c 61 70 20 3d 20 30 2e 30  ue overlap = 0.0
48f61 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
48f62 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 23  <nCell; ii++){.#
48f63 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52  if VARIANT_RSTAR
48f64 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42 54 52  TREE_CHOOSESUBTR
48f65 45 45 0a 20 20 20 20 69 66 28 20 69 69 21 3d 69  EE.    if( ii!=i
48f66 45 78 63 6c 75 64 65 20 29 0a 23 65 6c 73 65 0a  Exclude ).#else.
48f67 20 20 20 20 61 73 73 65 72 74 28 20 69 45 78 63      assert( iExc
48f68 6c 75 64 65 3d 3d 2d 31 20 29 3b 0a 20 20 20 20  lude==-1 );.    
48f69 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
48f6a 28 69 45 78 63 6c 75 64 65 29 3b 0a 23 65 6e 64  (iExclude);.#end
48f6b 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69  if.    {.      i
48f6c 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 52 74 72  nt jj;.      Rtr
48f6d 65 65 44 56 61 6c 75 65 20 6f 20 3d 20 28 52 74  eeDValue o = (Rt
48f6e 72 65 65 44 56 61 6c 75 65 29 31 3b 0a 20 20 20  reeDValue)1;.   
48f6f 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c     for(jj=0; jj<
48f70 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29  (pRtree->nDim*2)
48f71 3b 20 6a 6a 2b 3d 32 29 7b 0a 20 20 20 20 20 20  ; jj+=2){.      
48f72 20 20 52 74 72 65 65 44 56 61 6c 75 65 20 78 31    RtreeDValue x1
48f73 2c 20 78 32 3b 0a 0a 20 20 20 20 20 20 20 20 78  , x2;..        x
48f74 31 20 3d 20 4d 41 58 28 44 43 4f 4f 52 44 28 70  1 = MAX(DCOORD(p
48f75 2d 3e 61 43 6f 6f 72 64 5b 6a 6a 5d 29 2c 20 44  ->aCoord[jj]), D
48f76 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e  COORD(aCell[ii].
48f77 61 43 6f 6f 72 64 5b 6a 6a 5d 29 29 3b 0a 20 20  aCoord[jj]));.  
48f78 20 20 20 20 20 20 78 32 20 3d 20 4d 49 4e 28 44        x2 = MIN(D
48f79 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b  COORD(p->aCoord[
48f7a 6a 6a 2b 31 5d 29 2c 20 44 43 4f 4f 52 44 28 61  jj+1]), DCOORD(a
48f7b 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b  Cell[ii].aCoord[
48f7c 6a 6a 2b 31 5d 29 29 3b 0a 0a 20 20 20 20 20 20  jj+1]));..      
48f7d 20 20 69 66 28 20 78 32 3c 78 31 20 29 7b 0a 20    if( x2<x1 ){. 
48f7e 20 20 20 20 20 20 20 20 20 6f 20 3d 20 30 2e 30           o = 0.0
48f7f 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
48f80 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  k;.        }else
48f81 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 20 3d 20  {.          o = 
48f82 6f 20 2a 20 28 78 32 2d 78 31 29 3b 0a 20 20 20  o * (x2-x1);.   
48f83 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
48f84 20 20 20 20 20 6f 76 65 72 6c 61 70 20 2b 3d 20       overlap += 
48f85 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  o;.    }.  }.  r
48f86 65 74 75 72 6e 20 6f 76 65 72 6c 61 70 3b 0a 7d  eturn overlap;.}
48f87 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 56 41 52  .#endif..#if VAR
48f88 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 43  IANT_RSTARTREE_C
48f89 48 4f 4f 53 45 53 55 42 54 52 45 45 0a 73 74 61  HOOSESUBTREE.sta
48f8a 74 69 63 20 52 74 72 65 65 44 56 61 6c 75 65 20  tic RtreeDValue 
48f8b 63 65 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61 72  cellOverlapEnlar
48f8c 67 65 6d 65 6e 74 28 0a 20 20 52 74 72 65 65 20  gement(.  Rtree 
48f8d 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65  *pRtree, .  Rtre
48f8e 65 43 65 6c 6c 20 2a 70 2c 20 0a 20 20 52 74 72  eCell *p, .  Rtr
48f8f 65 65 43 65 6c 6c 20 2a 70 49 6e 73 65 72 74 2c  eeCell *pInsert,
48f90 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61   .  RtreeCell *a
48f91 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65  Cell, .  int nCe
48f92 6c 6c 2c 20 0a 20 20 69 6e 74 20 69 45 78 63 6c  ll, .  int iExcl
48f93 75 64 65 0a 29 7b 0a 20 20 52 74 72 65 65 44 56  ude.){.  RtreeDV
48f94 61 6c 75 65 20 62 65 66 6f 72 65 2c 20 61 66 74  alue before, aft
48f95 65 72 3b 0a 20 20 62 65 66 6f 72 65 20 3d 20 63  er;.  before = c
48f96 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74 72 65  ellOverlap(pRtre
48f97 65 2c 20 70 2c 20 61 43 65 6c 6c 2c 20 6e 43 65  e, p, aCell, nCe
48f98 6c 6c 2c 20 69 45 78 63 6c 75 64 65 29 3b 0a 20  ll, iExclude);. 
48f99 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
48f9a 65 2c 20 70 2c 20 70 49 6e 73 65 72 74 29 3b 0a  e, p, pInsert);.
48f9b 20 20 61 66 74 65 72 20 3d 20 63 65 6c 6c 4f 76    after = cellOv
48f9c 65 72 6c 61 70 28 70 52 74 72 65 65 2c 20 70 2c  erlap(pRtree, p,
48f9d 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 69   aCell, nCell, i
48f9e 45 78 63 6c 75 64 65 29 3b 0a 20 20 72 65 74 75  Exclude);.  retu
48f9f 72 6e 20 28 61 66 74 65 72 2d 62 65 66 6f 72 65  rn (after-before
48fa0 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  );.}.#endif.../*
48fa1 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
48fa2 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  n implements the
48fa3 20 43 68 6f 6f 73 65 4c 65 61 66 20 61 6c 67 6f   ChooseLeaf algo
48fa4 72 69 74 68 6d 20 66 72 6f 6d 20 47 75 74 6d 61  rithm from Gutma
48fa5 6e 5b 38 34 5d 2e 0a 2a 2a 20 43 68 6f 6f 73 65  n[84]..** Choose
48fa6 53 75 62 54 72 65 65 20 69 6e 20 72 2a 74 72 65  SubTree in r*tre
48fa7 65 20 74 65 72 6d 69 6e 6f 6c 6f 67 79 2e 0a 2a  e terminology..*
48fa8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 43 68 6f  /.static int Cho
48fa9 6f 73 65 4c 65 61 66 28 0a 20 20 52 74 72 65 65  oseLeaf(.  Rtree
48faa 20 2a 70 52 74 72 65 65 2c 20 20 20 20 20 20 20   *pRtree,       
48fab 20 20 20 20 20 20 20 20 2f 2a 20 52 74 72 65 65          /* Rtree
48fac 20 74 61 62 6c 65 20 2a 2f 0a 20 20 52 74 72 65   table */.  Rtre
48fad 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20 20 20  eCell *pCell,   
48fae 20 20 20 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c           /* Cell
48faf 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20   to insert into 
48fb0 72 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69  rtree */.  int i
48fb1 48 65 69 67 68 74 2c 20 20 20 20 20 20 20 20 20  Height,         
48fb2 20 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68          /* Heigh
48fb3 74 20 6f 66 20 73 75 62 2d 74 72 65 65 20 72 6f  t of sub-tree ro
48fb4 6f 74 65 64 20 61 74 20 70 43 65 6c 6c 20 2a 2f  oted at pCell */
48fb5 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70  .  RtreeNode **p
48fb6 70 4c 65 61 66 20 20 20 20 20 20 20 20 20 20 20  pLeaf           
48fb7 2f 2a 20 4f 55 54 3a 20 53 65 6c 65 63 74 65 64  /* OUT: Selected
48fb8 20 6c 65 61 66 20 70 61 67 65 20 2a 2f 0a 29 7b   leaf page */.){
48fb9 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
48fba 20 69 69 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65   ii;.  RtreeNode
48fbb 20 2a 70 4e 6f 64 65 3b 0a 20 20 72 63 20 3d 20   *pNode;.  rc = 
48fbc 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72  nodeAcquire(pRtr
48fbd 65 65 2c 20 31 2c 20 30 2c 20 26 70 4e 6f 64 65  ee, 1, 0, &pNode
48fbe 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  );..  for(ii=0; 
48fbf 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
48fc0 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 69 44 65   ii<(pRtree->iDe
48fc1 70 74 68 2d 69 48 65 69 67 68 74 29 3b 20 69 69  pth-iHeight); ii
48fc2 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 65  ++){.    int iCe
48fc3 6c 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ll;.    sqlite3_
48fc4 69 6e 74 36 34 20 69 42 65 73 74 20 3d 20 30 3b  int64 iBest = 0;
48fc5 0a 0a 20 20 20 20 52 74 72 65 65 44 56 61 6c 75  ..    RtreeDValu
48fc6 65 20 66 4d 69 6e 47 72 6f 77 74 68 20 3d 20 30  e fMinGrowth = 0
48fc7 2e 30 3b 0a 20 20 20 20 52 74 72 65 65 44 56 61  .0;.    RtreeDVa
48fc8 6c 75 65 20 66 4d 69 6e 41 72 65 61 20 3d 20 30  lue fMinArea = 0
48fc9 2e 30 3b 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  .0;.#if VARIANT_
48fca 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45  RSTARTREE_CHOOSE
48fcb 53 55 42 54 52 45 45 0a 20 20 20 20 52 74 72 65  SUBTREE.    Rtre
48fcc 65 44 56 61 6c 75 65 20 66 4d 69 6e 4f 76 65 72  eDValue fMinOver
48fcd 6c 61 70 20 3d 20 30 2e 30 3b 0a 20 20 20 20 52  lap = 0.0;.    R
48fce 74 72 65 65 44 56 61 6c 75 65 20 6f 76 65 72 6c  treeDValue overl
48fcf 61 70 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20  ap;.#endif..    
48fd0 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c  int nCell = NCEL
48fd1 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 52 74  L(pNode);.    Rt
48fd2 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20  reeCell cell;.  
48fd3 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68    RtreeNode *pCh
48fd4 69 6c 64 3b 0a 0a 20 20 20 20 52 74 72 65 65 43  ild;..    RtreeC
48fd5 65 6c 6c 20 2a 61 43 65 6c 6c 20 3d 20 30 3b 0a  ell *aCell = 0;.
48fd6 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54  .#if VARIANT_RST
48fd7 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42  ARTREE_CHOOSESUB
48fd8 54 52 45 45 0a 20 20 20 20 69 66 28 20 69 69 3d  TREE.    if( ii=
48fd9 3d 28 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68  =(pRtree->iDepth
48fda 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  -1) ){.      int
48fdb 20 6a 6a 3b 0a 20 20 20 20 20 20 61 43 65 6c 6c   jj;.      aCell
48fdc 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
48fdd 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65  c(sizeof(RtreeCe
48fde 6c 6c 29 2a 6e 43 65 6c 6c 29 3b 0a 20 20 20 20  ll)*nCell);.    
48fdf 20 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b 0a    if( !aCell ){.
48fe0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
48fe1 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
48fe2 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70     nodeRelease(p
48fe3 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
48fe4 20 20 20 20 20 20 20 70 4e 6f 64 65 20 3d 20 30         pNode = 0
48fe5 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e  ;.        contin
48fe6 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ue;.      }.    
48fe7 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 6e    for(jj=0; jj<n
48fe8 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20  Cell; jj++){.   
48fe9 20 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c       nodeGetCell
48fea 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
48feb 6a 6a 2c 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b  jj, &aCell[jj]);
48fec 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
48fed 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 53 65  endif..    /* Se
48fee 6c 65 63 74 20 74 68 65 20 63 68 69 6c 64 20 6e  lect the child n
48fef 6f 64 65 20 77 68 69 63 68 20 77 69 6c 6c 20 62  ode which will b
48ff0 65 20 65 6e 6c 61 72 67 65 64 20 74 68 65 20 6c  e enlarged the l
48ff1 65 61 73 74 20 69 66 20 70 43 65 6c 6c 0a 20 20  east if pCell.  
48ff2 20 20 2a 2a 20 69 73 20 69 6e 73 65 72 74 65 64    ** is inserted
48ff3 20 69 6e 74 6f 20 69 74 2e 20 52 65 73 6f 6c 76   into it. Resolv
48ff4 65 20 74 69 65 73 20 62 79 20 63 68 6f 6f 73 69  e ties by choosi
48ff5 6e 67 20 74 68 65 20 65 6e 74 72 79 20 77 69 74  ng the entry wit
48ff6 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 6d 61  h.    ** the sma
48ff7 6c 6c 65 73 74 20 61 72 65 61 2e 0a 20 20 20 20  llest area..    
48ff8 2a 2f 0a 20 20 20 20 66 6f 72 28 69 43 65 6c 6c  */.    for(iCell
48ff9 3d 30 3b 20 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b  =0; iCell<nCell;
48ffa 20 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20   iCell++){.     
48ffb 20 69 6e 74 20 62 42 65 73 74 20 3d 20 30 3b 0a   int bBest = 0;.
48ffc 20 20 20 20 20 20 52 74 72 65 65 44 56 61 6c 75        RtreeDValu
48ffd 65 20 67 72 6f 77 74 68 3b 0a 20 20 20 20 20 20  e growth;.      
48ffe 52 74 72 65 65 44 56 61 6c 75 65 20 61 72 65 61  RtreeDValue area
48fff 3b 0a 20 20 20 20 20 20 6e 6f 64 65 47 65 74 43  ;.      nodeGetC
49000 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ell(pRtree, pNod
49001 65 2c 20 69 43 65 6c 6c 2c 20 26 63 65 6c 6c 29  e, iCell, &cell)
49002 3b 0a 20 20 20 20 20 20 67 72 6f 77 74 68 20 3d  ;.      growth =
49003 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74 72   cellGrowth(pRtr
49004 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c  ee, &cell, pCell
49005 29 3b 0a 20 20 20 20 20 20 61 72 65 61 20 3d 20  );.      area = 
49006 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c  cellArea(pRtree,
49007 20 26 63 65 6c 6c 29 3b 0a 0a 23 69 66 20 56 41   &cell);..#if VA
49008 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f  RIANT_RSTARTREE_
49009 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0a 20 20  CHOOSESUBTREE.  
4900a 20 20 20 20 69 66 28 20 69 69 3d 3d 28 70 52 74      if( ii==(pRt
4900b 72 65 65 2d 3e 69 44 65 70 74 68 2d 31 29 20 29  ree->iDepth-1) )
4900c 7b 0a 20 20 20 20 20 20 20 20 6f 76 65 72 6c 61  {.        overla
4900d 70 20 3d 20 63 65 6c 6c 4f 76 65 72 6c 61 70 45  p = cellOverlapE
4900e 6e 6c 61 72 67 65 6d 65 6e 74 28 70 52 74 72 65  nlargement(pRtre
4900f 65 2c 26 63 65 6c 6c 2c 70 43 65 6c 6c 2c 61 43  e,&cell,pCell,aC
49010 65 6c 6c 2c 6e 43 65 6c 6c 2c 69 43 65 6c 6c 29  ell,nCell,iCell)
49011 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
49012 20 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20 3d         overlap =
49013 20 30 2e 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   0.0;.      }.  
49014 20 20 20 20 69 66 28 20 28 69 43 65 6c 6c 3d 3d      if( (iCell==
49015 30 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f  0) .       || (o
49016 76 65 72 6c 61 70 3c 66 4d 69 6e 4f 76 65 72 6c  verlap<fMinOverl
49017 61 70 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 28  ap) .       || (
49018 6f 76 65 72 6c 61 70 3d 3d 66 4d 69 6e 4f 76 65  overlap==fMinOve
49019 72 6c 61 70 20 26 26 20 67 72 6f 77 74 68 3c 66  rlap && growth<f
4901a 4d 69 6e 47 72 6f 77 74 68 29 0a 20 20 20 20 20  MinGrowth).     
4901b 20 20 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66    || (overlap==f
4901c 4d 69 6e 4f 76 65 72 6c 61 70 20 26 26 20 67 72  MinOverlap && gr
4901d 6f 77 74 68 3d 3d 66 4d 69 6e 47 72 6f 77 74 68  owth==fMinGrowth
4901e 20 26 26 20 61 72 65 61 3c 66 4d 69 6e 41 72 65   && area<fMinAre
4901f 61 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  a).      ){.    
49020 20 20 20 20 62 42 65 73 74 20 3d 20 31 3b 0a 20      bBest = 1;. 
49021 20 20 20 20 20 20 20 66 4d 69 6e 4f 76 65 72 6c         fMinOverl
49022 61 70 20 3d 20 6f 76 65 72 6c 61 70 3b 0a 20 20  ap = overlap;.  
49023 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20      }.#else.    
49024 20 20 69 66 28 20 69 43 65 6c 6c 3d 3d 30 7c 7c    if( iCell==0||
49025 67 72 6f 77 74 68 3c 66 4d 69 6e 47 72 6f 77 74  growth<fMinGrowt
49026 68 7c 7c 28 67 72 6f 77 74 68 3d 3d 66 4d 69 6e  h||(growth==fMin
49027 47 72 6f 77 74 68 20 26 26 20 61 72 65 61 3c 66  Growth && area<f
49028 4d 69 6e 41 72 65 61 29 20 29 7b 0a 20 20 20 20  MinArea) ){.    
49029 20 20 20 20 62 42 65 73 74 20 3d 20 31 3b 0a 20      bBest = 1;. 
4902a 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
4902b 20 20 20 20 69 66 28 20 62 42 65 73 74 20 29 7b      if( bBest ){
4902c 0a 20 20 20 20 20 20 20 20 66 4d 69 6e 47 72 6f  .        fMinGro
4902d 77 74 68 20 3d 20 67 72 6f 77 74 68 3b 0a 20 20  wth = growth;.  
4902e 20 20 20 20 20 20 66 4d 69 6e 41 72 65 61 20 3d        fMinArea =
4902f 20 61 72 65 61 3b 0a 20 20 20 20 20 20 20 20 69   area;.        i
49030 42 65 73 74 20 3d 20 63 65 6c 6c 2e 69 52 6f 77  Best = cell.iRow
49031 69 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  id;.      }.    
49032 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  }..    sqlite3_f
49033 72 65 65 28 61 43 65 6c 6c 29 3b 0a 20 20 20 20  ree(aCell);.    
49034 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65  rc = nodeAcquire
49035 28 70 52 74 72 65 65 2c 20 69 42 65 73 74 2c 20  (pRtree, iBest, 
49036 70 4e 6f 64 65 2c 20 26 70 43 68 69 6c 64 29 3b  pNode, &pChild);
49037 0a 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65  .    nodeRelease
49038 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
49039 0a 20 20 20 20 70 4e 6f 64 65 20 3d 20 70 43 68  .    pNode = pCh
4903a 69 6c 64 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 4c  ild;.  }..  *ppL
4903b 65 61 66 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 72  eaf = pNode;.  r
4903c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
4903d 2a 2a 20 41 20 63 65 6c 6c 20 77 69 74 68 20 74  ** A cell with t
4903e 68 65 20 73 61 6d 65 20 63 6f 6e 74 65 6e 74 20  he same content 
4903f 61 73 20 70 43 65 6c 6c 20 68 61 73 20 6a 75 73  as pCell has jus
49040 74 20 62 65 65 6e 20 69 6e 73 65 72 74 65 64 20  t been inserted 
49041 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 6e 6f 64 65  into.** the node
49042 20 70 4e 6f 64 65 2e 20 54 68 69 73 20 66 75 6e   pNode. This fun
49043 63 74 69 6f 6e 20 75 70 64 61 74 65 73 20 74 68  ction updates th
49044 65 20 62 6f 75 6e 64 69 6e 67 20 62 6f 78 20 63  e bounding box c
49045 65 6c 6c 73 20 69 6e 0a 2a 2a 20 61 6c 6c 20 61  ells in.** all a
49046 6e 63 65 73 74 6f 72 20 65 6c 65 6d 65 6e 74 73  ncestor elements
49047 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
49048 41 64 6a 75 73 74 54 72 65 65 28 0a 20 20 52 74  AdjustTree(.  Rt
49049 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20 20 20  ree *pRtree,    
4904a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4904b 2f 2a 20 52 74 72 65 65 20 74 61 62 6c 65 20 2a  /* Rtree table *
4904c 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  /.  RtreeNode *p
4904d 4e 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20  Node,           
4904e 20 20 20 20 20 20 2f 2a 20 41 64 6a 75 73 74 20        /* Adjust 
4904f 61 6e 63 65 73 74 72 79 20 6f 66 20 74 68 69 73  ancestry of this
49050 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 52 74 72 65   node. */.  Rtre
49051 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 20 20 20  eCell *pCell    
49052 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
49053 20 54 68 69 73 20 63 65 6c 6c 20 77 61 73 20 6a   This cell was j
49054 75 73 74 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a  ust inserted */.
49055 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  ){.  RtreeNode *
49056 70 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 77 68 69  p = pNode;.  whi
49057 6c 65 28 20 70 2d 3e 70 50 61 72 65 6e 74 20 29  le( p->pParent )
49058 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  {.    RtreeNode 
49059 2a 70 50 61 72 65 6e 74 20 3d 20 70 2d 3e 70 50  *pParent = p->pP
4905a 61 72 65 6e 74 3b 0a 20 20 20 20 52 74 72 65 65  arent;.    Rtree
4905b 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20 69  Cell cell;.    i
4905c 6e 74 20 69 43 65 6c 6c 3b 0a 0a 20 20 20 20 69  nt iCell;..    i
4905d 66 28 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64  f( nodeParentInd
4905e 65 78 28 70 52 74 72 65 65 2c 20 70 2c 20 26 69  ex(pRtree, p, &i
4905f 43 65 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 72  Cell) ){.      r
49060 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
49061 52 55 50 54 5f 56 54 41 42 3b 0a 20 20 20 20 7d  RUPT_VTAB;.    }
49062 0a 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c  ..    nodeGetCel
49063 6c 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e  l(pRtree, pParen
49064 74 2c 20 69 43 65 6c 6c 2c 20 26 63 65 6c 6c 29  t, iCell, &cell)
49065 3b 0a 20 20 20 20 69 66 28 20 21 63 65 6c 6c 43  ;.    if( !cellC
49066 6f 6e 74 61 69 6e 73 28 70 52 74 72 65 65 2c 20  ontains(pRtree, 
49067 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 20 29 7b  &cell, pCell) ){
49068 0a 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e  .      cellUnion
49069 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20  (pRtree, &cell, 
4906a 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 6e 6f  pCell);.      no
4906b 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c 28  deOverwriteCell(
4906c 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c  pRtree, pParent,
4906d 20 26 63 65 6c 6c 2c 20 69 43 65 6c 6c 29 3b 0a   &cell, iCell);.
4906e 20 20 20 20 7d 0a 20 0a 20 20 20 20 70 20 3d 20      }. .    p = 
4906f 70 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 20 20 72  pParent;.  }.  r
49070 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
49071 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
49072 6d 61 70 70 69 6e 67 20 28 69 52 6f 77 69 64 2d  mapping (iRowid-
49073 3e 69 4e 6f 64 65 29 20 74 6f 20 74 68 65 20 3c  >iNode) to the <
49074 72 74 72 65 65 3e 5f 72 6f 77 69 64 20 74 61 62  rtree>_rowid tab
49075 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
49076 74 20 72 6f 77 69 64 57 72 69 74 65 28 52 74 72  t rowidWrite(Rtr
49077 65 65 20 2a 70 52 74 72 65 65 2c 20 73 71 6c 69  ee *pRtree, sqli
49078 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64  te3_int64 iRowid
49079 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
4907a 69 4e 6f 64 65 29 7b 0a 20 20 73 71 6c 69 74 65  iNode){.  sqlite
4907b 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74  3_bind_int64(pRt
4907c 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64  ree->pWriteRowid
4907d 2c 20 31 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20  , 1, iRowid);.  
4907e 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
4907f 36 34 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74  64(pRtree->pWrit
49080 65 52 6f 77 69 64 2c 20 32 2c 20 69 4e 6f 64 65  eRowid, 2, iNode
49081 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65  );.  sqlite3_ste
49082 70 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  p(pRtree->pWrite
49083 52 6f 77 69 64 29 3b 0a 20 20 72 65 74 75 72 6e  Rowid);.  return
49084 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
49085 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77  Rtree->pWriteRow
49086 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72  id);.}../*.** Wr
49087 69 74 65 20 6d 61 70 70 69 6e 67 20 28 69 4e 6f  ite mapping (iNo
49088 64 65 2d 3e 69 50 61 72 29 20 74 6f 20 74 68 65  de->iPar) to the
49089 20 3c 72 74 72 65 65 3e 5f 70 61 72 65 6e 74 20   <rtree>_parent 
4908a 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  table..*/.static
4908b 20 69 6e 74 20 70 61 72 65 6e 74 57 72 69 74 65   int parentWrite
4908c 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
4908d 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4e  sqlite3_int64 iN
4908e 6f 64 65 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  ode, sqlite3_int
4908f 36 34 20 69 50 61 72 29 7b 0a 20 20 73 71 6c 69  64 iPar){.  sqli
49090 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
49091 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72  Rtree->pWritePar
49092 65 6e 74 2c 20 31 2c 20 69 4e 6f 64 65 29 3b 0a  ent, 1, iNode);.
49093 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
49094 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 57 72  nt64(pRtree->pWr
49095 69 74 65 50 61 72 65 6e 74 2c 20 32 2c 20 69 50  iteParent, 2, iP
49096 61 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  ar);.  sqlite3_s
49097 74 65 70 28 70 52 74 72 65 65 2d 3e 70 57 72 69  tep(pRtree->pWri
49098 74 65 50 61 72 65 6e 74 29 3b 0a 20 20 72 65 74  teParent);.  ret
49099 75 72 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65  urn sqlite3_rese
4909a 74 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  t(pRtree->pWrite
4909b 50 61 72 65 6e 74 29 3b 0a 7d 0a 0a 73 74 61 74  Parent);.}..stat
4909c 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e 73 65  ic int rtreeInse
4909d 72 74 43 65 6c 6c 28 52 74 72 65 65 20 2a 2c 20  rtCell(Rtree *, 
4909e 52 74 72 65 65 4e 6f 64 65 20 2a 2c 20 52 74 72  RtreeNode *, Rtr
4909f 65 65 43 65 6c 6c 20 2a 2c 20 69 6e 74 29 3b 0a  eeCell *, int);.
490a0 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55 54  .#if VARIANT_GUT
490a1 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49  TMAN_LINEAR_SPLI
490a2 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  T./*.** Implemen
490a3 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69  tation of the li
490a4 6e 65 61 72 20 76 61 72 69 61 6e 74 20 6f 66 20  near variant of 
490a5 74 68 65 20 50 69 63 6b 4e 65 78 74 28 29 20 66  the PickNext() f
490a6 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20  unction from.** 
490a7 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a  Guttman[84]..*/.
490a8 73 74 61 74 69 63 20 52 74 72 65 65 43 65 6c 6c  static RtreeCell
490a9 20 2a 4c 69 6e 65 61 72 50 69 63 6b 4e 65 78 74   *LinearPickNext
490aa 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  (.  Rtree *pRtre
490ab 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  e,.  RtreeCell *
490ac 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43  aCell, .  int nC
490ad 65 6c 6c 2c 20 0a 20 20 52 74 72 65 65 43 65 6c  ell, .  RtreeCel
490ae 6c 20 2a 70 4c 65 66 74 42 6f 78 2c 20 0a 20 20  l *pLeftBox, .  
490af 52 74 72 65 65 43 65 6c 6c 20 2a 70 52 69 67 68  RtreeCell *pRigh
490b0 74 42 6f 78 2c 0a 20 20 69 6e 74 20 2a 61 69 55  tBox,.  int *aiU
490b1 73 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b  sed.){.  int ii;
490b2 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 61 69 55  .  for(ii=0; aiU
490b3 73 65 64 5b 69 69 5d 3b 20 69 69 2b 2b 29 3b 0a  sed[ii]; ii++);.
490b4 20 20 61 69 55 73 65 64 5b 69 69 5d 20 3d 20 31    aiUsed[ii] = 1
490b5 3b 0a 20 20 72 65 74 75 72 6e 20 26 61 43 65 6c  ;.  return &aCel
490b6 6c 5b 69 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  l[ii];.}../*.** 
490b7 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
490b8 66 20 74 68 65 20 6c 69 6e 65 61 72 20 76 61 72  f the linear var
490b9 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b  iant of the Pick
490ba 53 65 65 64 73 28 29 20 66 75 6e 63 74 69 6f 6e  Seeds() function
490bb 20 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e   from.** Guttman
490bc 5b 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  [84]..*/.static 
490bd 76 6f 69 64 20 4c 69 6e 65 61 72 50 69 63 6b 53  void LinearPickS
490be 65 65 64 73 28 0a 20 20 52 74 72 65 65 20 2a 70  eeds(.  Rtree *p
490bf 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65  Rtree,.  RtreeCe
490c0 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e  ll *aCell, .  in
490c1 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20  t nCell, .  int 
490c2 2a 70 69 4c 65 66 74 53 65 65 64 2c 20 0a 20 20  *piLeftSeed, .  
490c3 69 6e 74 20 2a 70 69 52 69 67 68 74 53 65 65 64  int *piRightSeed
490c4 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  .){.  int i;.  i
490c5 6e 74 20 69 4c 65 66 74 53 65 65 64 20 3d 20 30  nt iLeftSeed = 0
490c6 3b 0a 20 20 69 6e 74 20 69 52 69 67 68 74 53 65  ;.  int iRightSe
490c7 65 64 20 3d 20 31 3b 0a 20 20 52 74 72 65 65 44  ed = 1;.  RtreeD
490c8 56 61 6c 75 65 20 6d 61 78 4e 6f 72 6d 61 6c 49  Value maxNormalI
490c9 6e 6e 65 72 57 69 64 74 68 20 3d 20 28 52 74 72  nnerWidth = (Rtr
490ca 65 65 44 56 61 6c 75 65 29 30 3b 0a 0a 20 20 2f  eeDValue)0;..  /
490cb 2a 20 50 69 63 6b 20 74 77 6f 20 22 73 65 65 64  * Pick two "seed
490cc 22 20 63 65 6c 6c 73 20 66 72 6f 6d 20 74 68 65  " cells from the
490cd 20 61 72 72 61 79 20 6f 66 20 63 65 6c 6c 73 2e   array of cells.
490ce 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 75   The algorithm u
490cf 73 65 64 0a 20 20 2a 2a 20 68 65 72 65 20 69 73  sed.  ** here is
490d0 20 74 68 65 20 4c 69 6e 65 61 72 50 69 63 6b 53   the LinearPickS
490d1 65 65 64 73 20 61 6c 67 6f 72 69 74 68 6d 20 66  eeds algorithm f
490d2 72 6f 6d 20 47 75 74 6d 61 6e 5b 31 39 38 34 5d  rom Gutman[1984]
490d3 2e 20 54 68 65 20 0a 20 20 2a 2a 20 69 6e 64 69  . The .  ** indi
490d4 63 65 73 20 6f 66 20 74 68 65 20 74 77 6f 20 73  ces of the two s
490d5 65 65 64 20 63 65 6c 6c 73 20 69 6e 20 74 68 65  eed cells in the
490d6 20 61 72 72 61 79 20 61 72 65 20 73 74 6f 72 65   array are store
490d7 64 20 69 6e 20 6c 6f 63 61 6c 0a 20 20 2a 2a 20  d in local.  ** 
490d8 76 61 72 69 61 62 6c 65 73 20 69 4c 65 66 74 53  variables iLeftS
490d9 65 65 6b 20 61 6e 64 20 69 52 69 67 68 74 53 65  eek and iRightSe
490da 65 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  ed..  */.  for(i
490db 3d 30 3b 20 69 3c 70 52 74 72 65 65 2d 3e 6e 44  =0; i<pRtree->nD
490dc 69 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 52 74  im; i++){.    Rt
490dd 72 65 65 44 56 61 6c 75 65 20 78 31 20 3d 20 44  reeDValue x1 = D
490de 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 30 5d 2e 61  COORD(aCell[0].a
490df 43 6f 6f 72 64 5b 69 2a 32 5d 29 3b 0a 20 20 20  Coord[i*2]);.   
490e0 20 52 74 72 65 65 44 56 61 6c 75 65 20 78 32 20   RtreeDValue x2 
490e1 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 30  = DCOORD(aCell[0
490e2 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d 29  ].aCoord[i*2+1])
490e3 3b 0a 20 20 20 20 52 74 72 65 65 44 56 61 6c 75  ;.    RtreeDValu
490e4 65 20 78 33 20 3d 20 78 31 3b 0a 20 20 20 20 52  e x3 = x1;.    R
490e5 74 72 65 65 44 56 61 6c 75 65 20 78 34 20 3d 20  treeDValue x4 = 
490e6 78 32 3b 0a 20 20 20 20 69 6e 74 20 6a 6a 3b 0a  x2;.    int jj;.
490e7 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 4c 65  .    int iCellLe
490e8 66 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  ft = 0;.    int 
490e9 69 43 65 6c 6c 52 69 67 68 74 20 3d 20 30 3b 0a  iCellRight = 0;.
490ea 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20 6a  .    for(jj=1; j
490eb 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a  j<nCell; jj++){.
490ec 20 20 20 20 20 20 52 74 72 65 65 44 56 61 6c 75        RtreeDValu
490ed 65 20 6c 65 66 74 20 3d 20 44 43 4f 4f 52 44 28  e left = DCOORD(
490ee 61 43 65 6c 6c 5b 6a 6a 5d 2e 61 43 6f 6f 72 64  aCell[jj].aCoord
490ef 5b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 52 74  [i*2]);.      Rt
490f0 72 65 65 44 56 61 6c 75 65 20 72 69 67 68 74 20  reeDValue right 
490f1 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 6a  = DCOORD(aCell[j
490f2 6a 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d  j].aCoord[i*2+1]
490f3 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 6c 65  );..      if( le
490f4 66 74 3c 78 31 20 29 20 78 31 20 3d 20 6c 65 66  ft<x1 ) x1 = lef
490f5 74 3b 0a 20 20 20 20 20 20 69 66 28 20 72 69 67  t;.      if( rig
490f6 68 74 3e 78 34 20 29 20 78 34 20 3d 20 72 69 67  ht>x4 ) x4 = rig
490f7 68 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65  ht;.      if( le
490f8 66 74 3e 78 33 20 29 7b 0a 20 20 20 20 20 20 20  ft>x3 ){.       
490f9 20 78 33 20 3d 20 6c 65 66 74 3b 0a 20 20 20 20   x3 = left;.    
490fa 20 20 20 20 69 43 65 6c 6c 52 69 67 68 74 20 3d      iCellRight =
490fb 20 6a 6a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   jj;.      }.   
490fc 20 20 20 69 66 28 20 72 69 67 68 74 3c 78 32 20     if( right<x2 
490fd 29 7b 0a 20 20 20 20 20 20 20 20 78 32 20 3d 20  ){.        x2 = 
490fe 72 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 69  right;.        i
490ff 43 65 6c 6c 4c 65 66 74 20 3d 20 6a 6a 3b 0a 20  CellLeft = jj;. 
49100 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
49101 20 20 69 66 28 20 78 34 21 3d 78 31 20 29 7b 0a    if( x4!=x1 ){.
49102 20 20 20 20 20 20 52 74 72 65 65 44 56 61 6c 75        RtreeDValu
49103 65 20 6e 6f 72 6d 61 6c 77 69 64 74 68 20 3d 20  e normalwidth = 
49104 28 78 33 20 2d 20 78 32 29 20 2f 20 28 78 34 20  (x3 - x2) / (x4 
49105 2d 20 78 31 29 3b 0a 20 20 20 20 20 20 69 66 28  - x1);.      if(
49106 20 6e 6f 72 6d 61 6c 77 69 64 74 68 3e 6d 61 78   normalwidth>max
49107 4e 6f 72 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68  NormalInnerWidth
49108 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c 65 66   ){.        iLef
49109 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 4c 65 66  tSeed = iCellLef
4910a 74 3b 0a 20 20 20 20 20 20 20 20 69 52 69 67 68  t;.        iRigh
4910b 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 52 69 67  tSeed = iCellRig
4910c 68 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ht;.      }.    
4910d 7d 0a 20 20 7d 0a 0a 20 20 2a 70 69 4c 65 66 74  }.  }..  *piLeft
4910e 53 65 65 64 20 3d 20 69 4c 65 66 74 53 65 65 64  Seed = iLeftSeed
4910f 3b 0a 20 20 2a 70 69 52 69 67 68 74 53 65 65 64  ;.  *piRightSeed
49110 20 3d 20 69 52 69 67 68 74 53 65 65 64 3b 0a 7d   = iRightSeed;.}
49111 0a 23 65 6e 64 69 66 20 2f 2a 20 56 41 52 49 41  .#endif /* VARIA
49112 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41  NT_GUTTMAN_LINEA
49113 52 5f 53 50 4c 49 54 20 2a 2f 0a 0a 23 69 66 20  R_SPLIT */..#if 
49114 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
49115 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49 54 0a  QUADRATIC_SPLIT.
49116 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
49117 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 61 64  tion of the quad
49118 72 61 74 69 63 20 76 61 72 69 61 6e 74 20 6f 66  ratic variant of
49119 20 74 68 65 20 50 69 63 6b 4e 65 78 74 28 29 20   the PickNext() 
4911a 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a  function from.**
4911b 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f   Guttman[84]..*/
4911c 0a 73 74 61 74 69 63 20 52 74 72 65 65 43 65 6c  .static RtreeCel
4911d 6c 20 2a 51 75 61 64 72 61 74 69 63 50 69 63 6b  l *QuadraticPick
4911e 4e 65 78 74 28 0a 20 20 52 74 72 65 65 20 2a 70  Next(.  Rtree *p
4911f 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65  Rtree,.  RtreeCe
49120 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e  ll *aCell, .  in
49121 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 52 74 72 65  t nCell, .  Rtre
49122 65 43 65 6c 6c 20 2a 70 4c 65 66 74 42 6f 78 2c  eCell *pLeftBox,
49123 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70   .  RtreeCell *p
49124 52 69 67 68 74 42 6f 78 2c 0a 20 20 69 6e 74 20  RightBox,.  int 
49125 2a 61 69 55 73 65 64 0a 29 7b 0a 20 20 23 64 65  *aiUsed.){.  #de
49126 66 69 6e 65 20 46 41 42 53 28 61 29 20 28 28 61  fine FABS(a) ((a
49127 29 3c 30 2e 30 3f 2d 31 2e 30 2a 28 61 29 3a 28  )<0.0?-1.0*(a):(
49128 61 29 29 0a 0a 20 20 69 6e 74 20 69 53 65 6c 65  a))..  int iSele
49129 63 74 20 3d 20 2d 31 3b 0a 20 20 52 74 72 65 65  ct = -1;.  Rtree
4912a 44 56 61 6c 75 65 20 66 44 69 66 66 3b 0a 20 20  DValue fDiff;.  
4912b 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69 69  int ii;.  for(ii
4912c 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69  =0; ii<nCell; ii
4912d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 69 55  ++){.    if( aiU
4912e 73 65 64 5b 69 69 5d 3d 3d 30 20 29 7b 0a 20 20  sed[ii]==0 ){.  
4912f 20 20 20 20 52 74 72 65 65 44 56 61 6c 75 65 20      RtreeDValue 
49130 6c 65 66 74 20 3d 20 63 65 6c 6c 47 72 6f 77 74  left = cellGrowt
49131 68 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 42  h(pRtree, pLeftB
49132 6f 78 2c 20 26 61 43 65 6c 6c 5b 69 69 5d 29 3b  ox, &aCell[ii]);
49133 0a 20 20 20 20 20 20 52 74 72 65 65 44 56 61 6c  .      RtreeDVal
49134 75 65 20 72 69 67 68 74 20 3d 20 63 65 6c 6c 47  ue right = cellG
49135 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70 4c  rowth(pRtree, pL
49136 65 66 74 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69  eftBox, &aCell[i
49137 69 5d 29 3b 0a 20 20 20 20 20 20 52 74 72 65 65  i]);.      Rtree
49138 44 56 61 6c 75 65 20 64 69 66 66 20 3d 20 46 41  DValue diff = FA
49139 42 53 28 72 69 67 68 74 2d 6c 65 66 74 29 3b 0a  BS(right-left);.
4913a 20 20 20 20 20 20 69 66 28 20 69 53 65 6c 65 63        if( iSelec
4913b 74 3c 30 20 7c 7c 20 64 69 66 66 3e 66 44 69 66  t<0 || diff>fDif
4913c 66 20 29 7b 0a 20 20 20 20 20 20 20 20 66 44 69  f ){.        fDi
4913d 66 66 20 3d 20 64 69 66 66 3b 0a 20 20 20 20 20  ff = diff;.     
4913e 20 20 20 69 53 65 6c 65 63 74 20 3d 20 69 69 3b     iSelect = ii;
4913f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
49140 20 7d 0a 20 20 61 69 55 73 65 64 5b 69 53 65 6c   }.  aiUsed[iSel
49141 65 63 74 5d 20 3d 20 31 3b 0a 20 20 72 65 74 75  ect] = 1;.  retu
49142 72 6e 20 26 61 43 65 6c 6c 5b 69 53 65 6c 65 63  rn &aCell[iSelec
49143 74 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  t];.}../*.** Imp
49144 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
49145 68 65 20 71 75 61 64 72 61 74 69 63 20 76 61 72  he quadratic var
49146 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b  iant of the Pick
49147 53 65 65 64 73 28 29 20 66 75 6e 63 74 69 6f 6e  Seeds() function
49148 20 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e   from.** Guttman
49149 5b 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  [84]..*/.static 
4914a 76 6f 69 64 20 51 75 61 64 72 61 74 69 63 50 69  void QuadraticPi
4914b 63 6b 53 65 65 64 73 28 0a 20 20 52 74 72 65 65  ckSeeds(.  Rtree
4914c 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65   *pRtree,.  Rtre
4914d 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20  eCell *aCell, . 
4914e 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69   int nCell, .  i
4914f 6e 74 20 2a 70 69 4c 65 66 74 53 65 65 64 2c 20  nt *piLeftSeed, 
49150 0a 20 20 69 6e 74 20 2a 70 69 52 69 67 68 74 53  .  int *piRightS
49151 65 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b  eed.){.  int ii;
49152 0a 20 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 69 6e  .  int jj;..  in
49153 74 20 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b  t iLeftSeed = 0;
49154 0a 20 20 69 6e 74 20 69 52 69 67 68 74 53 65 65  .  int iRightSee
49155 64 20 3d 20 31 3b 0a 20 20 52 74 72 65 65 44 56  d = 1;.  RtreeDV
49156 61 6c 75 65 20 66 57 61 73 74 65 20 3d 20 30 2e  alue fWaste = 0.
49157 30 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  0;..  for(ii=0; 
49158 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b  ii<nCell; ii++){
49159 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 69 69 2b 31  .    for(jj=ii+1
4915a 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b  ; jj<nCell; jj++
4915b 29 7b 0a 20 20 20 20 20 20 52 74 72 65 65 44 56  ){.      RtreeDV
4915c 61 6c 75 65 20 72 69 67 68 74 20 3d 20 63 65 6c  alue right = cel
4915d 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 61  lArea(pRtree, &a
4915e 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20  Cell[jj]);.     
4915f 20 52 74 72 65 65 44 56 61 6c 75 65 20 67 72 6f   RtreeDValue gro
49160 77 74 68 20 3d 20 63 65 6c 6c 47 72 6f 77 74 68  wth = cellGrowth
49161 28 70 52 74 72 65 65 2c 20 26 61 43 65 6c 6c 5b  (pRtree, &aCell[
49162 69 69 5d 2c 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29  ii], &aCell[jj])
49163 3b 0a 20 20 20 20 20 20 52 74 72 65 65 44 56 61  ;.      RtreeDVa
49164 6c 75 65 20 77 61 73 74 65 20 3d 20 67 72 6f 77  lue waste = grow
49165 74 68 20 2d 20 72 69 67 68 74 3b 0a 0a 20 20 20  th - right;..   
49166 20 20 20 69 66 28 20 77 61 73 74 65 3e 66 57 61     if( waste>fWa
49167 73 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ste ){.        i
49168 4c 65 66 74 53 65 65 64 20 3d 20 69 69 3b 0a 20  LeftSeed = ii;. 
49169 20 20 20 20 20 20 20 69 52 69 67 68 74 53 65 65         iRightSee
4916a 64 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20 20 20  d = jj;.        
4916b 66 57 61 73 74 65 20 3d 20 77 61 73 74 65 3b 0a  fWaste = waste;.
4916c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
4916d 7d 0a 0a 20 20 2a 70 69 4c 65 66 74 53 65 65 64  }..  *piLeftSeed
4916e 20 3d 20 69 4c 65 66 74 53 65 65 64 3b 0a 20 20   = iLeftSeed;.  
4916f 2a 70 69 52 69 67 68 74 53 65 65 64 20 3d 20 69  *piRightSeed = i
49170 52 69 67 68 74 53 65 65 64 3b 0a 7d 0a 23 65 6e  RightSeed;.}.#en
49171 64 69 66 20 2f 2a 20 56 41 52 49 41 4e 54 5f 47  dif /* VARIANT_G
49172 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
49173 5f 53 50 4c 49 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _SPLIT */../*.**
49174 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64 78 2c   Arguments aIdx,
49175 20 61 44 69 73 74 61 6e 63 65 20 61 6e 64 20 61   aDistance and a
49176 53 70 61 72 65 20 61 6c 6c 20 70 6f 69 6e 74 20  Spare all point 
49177 74 6f 20 61 72 72 61 79 73 20 6f 66 20 73 69 7a  to arrays of siz
49178 65 0a 2a 2a 20 6e 49 64 78 2e 20 54 68 65 20 61  e.** nIdx. The a
49179 49 64 78 20 61 72 72 61 79 20 63 6f 6e 74 61 69  Idx array contai
4917a 6e 73 20 74 68 65 20 73 65 74 20 6f 66 20 69 6e  ns the set of in
4917b 74 65 67 65 72 73 20 66 72 6f 6d 20 30 20 74 6f  tegers from 0 to
4917c 20 0a 2a 2a 20 28 6e 49 64 78 2d 31 29 20 69 6e   .** (nIdx-1) in
4917d 20 6e 6f 20 70 61 72 74 69 63 75 6c 61 72 20 6f   no particular o
4917e 72 64 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74  rder. This funct
4917f 69 6f 6e 20 73 6f 72 74 73 20 74 68 65 20 76 61  ion sorts the va
49180 6c 75 65 73 0a 2a 2a 20 69 6e 20 61 49 64 78 20  lues.** in aIdx 
49181 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
49182 20 69 6e 64 65 78 65 64 20 76 61 6c 75 65 73 20   indexed values 
49183 69 6e 20 61 44 69 73 74 61 6e 63 65 2e 20 46 6f  in aDistance. Fo
49184 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 73  r.** example, as
49185 73 75 6d 69 6e 67 20 74 68 65 20 69 6e 70 75 74  suming the input
49186 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 49 64 78 20  s:.**.**   aIdx 
49187 20 20 20 20 20 3d 20 7b 20 30 2c 20 20 20 31 2c       = { 0,   1,
49188 20 20 20 32 2c 20 20 20 33 20 7d 0a 2a 2a 20 20     2,   3 }.**  
49189 20 61 44 69 73 74 61 6e 63 65 20 3d 20 7b 20 35   aDistance = { 5
4918a 2e 30 2c 20 32 2e 30 2c 20 37 2e 30 2c 20 36 2e  .0, 2.0, 7.0, 6.
4918b 30 20 7d 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20 66  0 }.**.** this f
4918c 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65  unction sets the
4918d 20 61 49 64 78 20 61 72 72 61 79 20 74 6f 20 63   aIdx array to c
4918e 6f 6e 74 61 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20  ontain:.**.**   
4918f 61 49 64 78 20 20 20 20 20 20 3d 20 7b 20 30 2c  aIdx      = { 0,
49190 20 20 20 31 2c 20 20 20 32 2c 20 20 20 33 20 7d     1,   2,   3 }
49191 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 53 70 61 72  .**.** The aSpar
49192 65 20 61 72 72 61 79 20 69 73 20 75 73 65 64 20  e array is used 
49193 61 73 20 74 65 6d 70 6f 72 61 72 79 20 77 6f 72  as temporary wor
49194 6b 69 6e 67 20 73 70 61 63 65 20 62 79 20 74 68  king space by th
49195 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 61 6c 67  e.** sorting alg
49196 6f 72 69 74 68 6d 2e 0a 2a 2f 0a 73 74 61 74 69  orithm..*/.stati
49197 63 20 76 6f 69 64 20 53 6f 72 74 42 79 44 69 73  c void SortByDis
49198 74 61 6e 63 65 28 0a 20 20 69 6e 74 20 2a 61 49  tance(.  int *aI
49199 64 78 2c 20 0a 20 20 69 6e 74 20 6e 49 64 78 2c  dx, .  int nIdx,
4919a 20 0a 20 20 52 74 72 65 65 44 56 61 6c 75 65 20   .  RtreeDValue 
4919b 2a 61 44 69 73 74 61 6e 63 65 2c 20 0a 20 20 69  *aDistance, .  i
4919c 6e 74 20 2a 61 53 70 61 72 65 0a 29 7b 0a 20 20  nt *aSpare.){.  
4919d 69 66 28 20 6e 49 64 78 3e 31 20 29 7b 0a 20 20  if( nIdx>1 ){.  
4919e 20 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 30 3b    int iLeft = 0;
4919f 0a 20 20 20 20 69 6e 74 20 69 52 69 67 68 74 20  .    int iRight 
491a0 3d 20 30 3b 0a 0a 20 20 20 20 69 6e 74 20 6e 4c  = 0;..    int nL
491a1 65 66 74 20 3d 20 6e 49 64 78 2f 32 3b 0a 20 20  eft = nIdx/2;.  
491a2 20 20 69 6e 74 20 6e 52 69 67 68 74 20 3d 20 6e    int nRight = n
491a3 49 64 78 2d 6e 4c 65 66 74 3b 0a 20 20 20 20 69  Idx-nLeft;.    i
491a4 6e 74 20 2a 61 4c 65 66 74 20 3d 20 61 49 64 78  nt *aLeft = aIdx
491a5 3b 0a 20 20 20 20 69 6e 74 20 2a 61 52 69 67 68  ;.    int *aRigh
491a6 74 20 3d 20 26 61 49 64 78 5b 6e 4c 65 66 74 5d  t = &aIdx[nLeft]
491a7 3b 0a 0a 20 20 20 20 53 6f 72 74 42 79 44 69 73  ;..    SortByDis
491a8 74 61 6e 63 65 28 61 4c 65 66 74 2c 20 6e 4c 65  tance(aLeft, nLe
491a9 66 74 2c 20 61 44 69 73 74 61 6e 63 65 2c 20 61  ft, aDistance, a
491aa 53 70 61 72 65 29 3b 0a 20 20 20 20 53 6f 72 74  Spare);.    Sort
491ab 42 79 44 69 73 74 61 6e 63 65 28 61 52 69 67 68  ByDistance(aRigh
491ac 74 2c 20 6e 52 69 67 68 74 2c 20 61 44 69 73 74  t, nRight, aDist
491ad 61 6e 63 65 2c 20 61 53 70 61 72 65 29 3b 0a 0a  ance, aSpare);..
491ae 20 20 20 20 6d 65 6d 63 70 79 28 61 53 70 61 72      memcpy(aSpar
491af 65 2c 20 61 4c 65 66 74 2c 20 73 69 7a 65 6f 66  e, aLeft, sizeof
491b0 28 69 6e 74 29 2a 6e 4c 65 66 74 29 3b 0a 20 20  (int)*nLeft);.  
491b1 20 20 61 4c 65 66 74 20 3d 20 61 53 70 61 72 65    aLeft = aSpare
491b2 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c  ;..    while( iL
491b3 65 66 74 3c 6e 4c 65 66 74 20 7c 7c 20 69 52 69  eft<nLeft || iRi
491b4 67 68 74 3c 6e 52 69 67 68 74 20 29 7b 0a 20 20  ght<nRight ){.  
491b5 20 20 20 20 69 66 28 20 69 4c 65 66 74 3d 3d 6e      if( iLeft==n
491b6 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Left ){.        
491b7 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68  aIdx[iLeft+iRigh
491b8 74 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69 67  t] = aRight[iRig
491b9 68 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 52 69  ht];.        iRi
491ba 67 68 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c  ght++;.      }el
491bb 73 65 20 69 66 28 20 69 52 69 67 68 74 3d 3d 6e  se if( iRight==n
491bc 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 20  Right ){.       
491bd 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67   aIdx[iLeft+iRig
491be 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66  ht] = aLeft[iLef
491bf 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 4c 65 66  t];.        iLef
491c0 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  t++;.      }else
491c1 7b 0a 20 20 20 20 20 20 20 20 52 74 72 65 65 44  {.        RtreeD
491c2 56 61 6c 75 65 20 66 4c 65 66 74 20 3d 20 61 44  Value fLeft = aD
491c3 69 73 74 61 6e 63 65 5b 61 4c 65 66 74 5b 69 4c  istance[aLeft[iL
491c4 65 66 74 5d 5d 3b 0a 20 20 20 20 20 20 20 20 52  eft]];.        R
491c5 74 72 65 65 44 56 61 6c 75 65 20 66 52 69 67 68  treeDValue fRigh
491c6 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 52  t = aDistance[aR
491c7 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 3b 0a 20  ight[iRight]];. 
491c8 20 20 20 20 20 20 20 69 66 28 20 66 4c 65 66 74         if( fLeft
491c9 3c 66 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  <fRight ){.     
491ca 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b       aIdx[iLeft+
491cb 69 52 69 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b  iRight] = aLeft[
491cc 69 4c 65 66 74 5d 3b 0a 20 20 20 20 20 20 20 20  iLeft];.        
491cd 20 20 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20 20    iLeft++;.     
491ce 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
491cf 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69      aIdx[iLeft+i
491d0 52 69 67 68 74 5d 20 3d 20 61 52 69 67 68 74 5b  Right] = aRight[
491d1 69 52 69 67 68 74 5d 3b 0a 20 20 20 20 20 20 20  iRight];.       
491d2 20 20 20 69 52 69 67 68 74 2b 2b 3b 0a 20 20 20     iRight++;.   
491d3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
491d4 20 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20     }..#if 0.    
491d5 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
491d6 65 20 73 6f 72 74 20 77 6f 72 6b 65 64 20 2a 2f  e sort worked */
491d7 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74  .    {.      int
491d8 20 6a 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a   jj;.      for(j
491d9 6a 3d 31 3b 20 6a 6a 3c 6e 49 64 78 3b 20 6a 6a  j=1; jj<nIdx; jj
491da 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 52 74 72  ++){.        Rtr
491db 65 65 44 56 61 6c 75 65 20 6c 65 66 74 20 3d 20  eeDValue left = 
491dc 61 44 69 73 74 61 6e 63 65 5b 61 49 64 78 5b 6a  aDistance[aIdx[j
491dd 6a 2d 31 5d 5d 3b 0a 20 20 20 20 20 20 20 20 52  j-1]];.        R
491de 74 72 65 65 44 56 61 6c 75 65 20 72 69 67 68 74  treeDValue right
491df 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 49 64   = aDistance[aId
491e0 78 5b 6a 6a 5d 5d 3b 0a 20 20 20 20 20 20 20 20  x[jj]];.        
491e1 61 73 73 65 72 74 28 20 6c 65 66 74 3c 3d 72 69  assert( left<=ri
491e2 67 68 74 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ght );.      }. 
491e3 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
491e4 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e  }../*.** Argumen
491e5 74 73 20 61 49 64 78 2c 20 61 43 65 6c 6c 20 61  ts aIdx, aCell a
491e6 6e 64 20 61 53 70 61 72 65 20 61 6c 6c 20 70 6f  nd aSpare all po
491e7 69 6e 74 20 74 6f 20 61 72 72 61 79 73 20 6f 66  int to arrays of
491e8 20 73 69 7a 65 0a 2a 2a 20 6e 49 64 78 2e 20 54   size.** nIdx. T
491e9 68 65 20 61 49 64 78 20 61 72 72 61 79 20 63 6f  he aIdx array co
491ea 6e 74 61 69 6e 73 20 74 68 65 20 73 65 74 20 6f  ntains the set o
491eb 66 20 69 6e 74 65 67 65 72 73 20 66 72 6f 6d 20  f integers from 
491ec 30 20 74 6f 20 0a 2a 2a 20 28 6e 49 64 78 2d 31  0 to .** (nIdx-1
491ed 29 20 69 6e 20 6e 6f 20 70 61 72 74 69 63 75 6c  ) in no particul
491ee 61 72 20 6f 72 64 65 72 2e 20 54 68 69 73 20 66  ar order. This f
491ef 75 6e 63 74 69 6f 6e 20 73 6f 72 74 73 20 74 68  unction sorts th
491f0 65 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e 20 61  e values.** in a
491f1 49 64 78 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  Idx according to
491f2 20 64 69 6d 65 6e 73 69 6f 6e 20 69 44 69 6d 20   dimension iDim 
491f3 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20  of the cells in 
491f4 61 43 65 6c 6c 2e 20 54 68 65 0a 2a 2a 20 6d 69  aCell. The.** mi
491f5 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 64  nimum value of d
491f6 69 6d 65 6e 73 69 6f 6e 20 69 44 69 6d 20 69 73  imension iDim is
491f7 20 63 6f 6e 73 69 64 65 72 65 64 20 66 69 72 73   considered firs
491f8 74 2c 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75  t, the.** maximu
491f9 6d 20 75 73 65 64 20 74 6f 20 62 72 65 61 6b 20  m used to break 
491fa 74 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ties..**.** The 
491fb 61 53 70 61 72 65 20 61 72 72 61 79 20 69 73 20  aSpare array is 
491fc 75 73 65 64 20 61 73 20 74 65 6d 70 6f 72 61 72  used as temporar
491fd 79 20 77 6f 72 6b 69 6e 67 20 73 70 61 63 65 20  y working space 
491fe 62 79 20 74 68 65 0a 2a 2a 20 73 6f 72 74 69 6e  by the.** sortin
491ff 67 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a 2f 0a  g algorithm..*/.
49200 73 74 61 74 69 63 20 76 6f 69 64 20 53 6f 72 74  static void Sort
49201 42 79 44 69 6d 65 6e 73 69 6f 6e 28 0a 20 20 52  ByDimension(.  R
49202 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20  tree *pRtree,.  
49203 69 6e 74 20 2a 61 49 64 78 2c 20 0a 20 20 69 6e  int *aIdx, .  in
49204 74 20 6e 49 64 78 2c 20 0a 20 20 69 6e 74 20 69  t nIdx, .  int i
49205 44 69 6d 2c 20 0a 20 20 52 74 72 65 65 43 65 6c  Dim, .  RtreeCel
49206 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74  l *aCell, .  int
49207 20 2a 61 53 70 61 72 65 0a 29 7b 0a 20 20 69 66   *aSpare.){.  if
49208 28 20 6e 49 64 78 3e 31 20 29 7b 0a 0a 20 20 20  ( nIdx>1 ){..   
49209 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 30 3b 0a   int iLeft = 0;.
4920a 20 20 20 20 69 6e 74 20 69 52 69 67 68 74 20 3d      int iRight =
4920b 20 30 3b 0a 0a 20 20 20 20 69 6e 74 20 6e 4c 65   0;..    int nLe
4920c 66 74 20 3d 20 6e 49 64 78 2f 32 3b 0a 20 20 20  ft = nIdx/2;.   
4920d 20 69 6e 74 20 6e 52 69 67 68 74 20 3d 20 6e 49   int nRight = nI
4920e 64 78 2d 6e 4c 65 66 74 3b 0a 20 20 20 20 69 6e  dx-nLeft;.    in
4920f 74 20 2a 61 4c 65 66 74 20 3d 20 61 49 64 78 3b  t *aLeft = aIdx;
49210 0a 20 20 20 20 69 6e 74 20 2a 61 52 69 67 68 74  .    int *aRight
49211 20 3d 20 26 61 49 64 78 5b 6e 4c 65 66 74 5d 3b   = &aIdx[nLeft];
49212 0a 0a 20 20 20 20 53 6f 72 74 42 79 44 69 6d 65  ..    SortByDime
49213 6e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20 61 4c  nsion(pRtree, aL
49214 65 66 74 2c 20 6e 4c 65 66 74 2c 20 69 44 69 6d  eft, nLeft, iDim
49215 2c 20 61 43 65 6c 6c 2c 20 61 53 70 61 72 65 29  , aCell, aSpare)
49216 3b 0a 20 20 20 20 53 6f 72 74 42 79 44 69 6d 65  ;.    SortByDime
49217 6e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20 61 52  nsion(pRtree, aR
49218 69 67 68 74 2c 20 6e 52 69 67 68 74 2c 20 69 44  ight, nRight, iD
49219 69 6d 2c 20 61 43 65 6c 6c 2c 20 61 53 70 61 72  im, aCell, aSpar
4921a 65 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28  e);..    memcpy(
4921b 61 53 70 61 72 65 2c 20 61 4c 65 66 74 2c 20 73  aSpare, aLeft, s
4921c 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 4c 65 66 74  izeof(int)*nLeft
4921d 29 3b 0a 20 20 20 20 61 4c 65 66 74 20 3d 20 61  );.    aLeft = a
4921e 53 70 61 72 65 3b 0a 20 20 20 20 77 68 69 6c 65  Spare;.    while
4921f 28 20 69 4c 65 66 74 3c 6e 4c 65 66 74 20 7c 7c  ( iLeft<nLeft ||
49220 20 69 52 69 67 68 74 3c 6e 52 69 67 68 74 20 29   iRight<nRight )
49221 7b 0a 20 20 20 20 20 20 52 74 72 65 65 44 56 61  {.      RtreeDVa
49222 6c 75 65 20 78 6c 65 66 74 31 20 3d 20 44 43 4f  lue xleft1 = DCO
49223 4f 52 44 28 61 43 65 6c 6c 5b 61 4c 65 66 74 5b  ORD(aCell[aLeft[
49224 69 4c 65 66 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69  iLeft]].aCoord[i
49225 44 69 6d 2a 32 5d 29 3b 0a 20 20 20 20 20 20 52  Dim*2]);.      R
49226 74 72 65 65 44 56 61 6c 75 65 20 78 6c 65 66 74  treeDValue xleft
49227 32 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c  2 = DCOORD(aCell
49228 5b 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d 2e 61  [aLeft[iLeft]].a
49229 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29  Coord[iDim*2+1])
4922a 3b 0a 20 20 20 20 20 20 52 74 72 65 65 44 56 61  ;.      RtreeDVa
4922b 6c 75 65 20 78 72 69 67 68 74 31 20 3d 20 44 43  lue xright1 = DC
4922c 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 52 69 67 68  OORD(aCell[aRigh
4922d 74 5b 69 52 69 67 68 74 5d 5d 2e 61 43 6f 6f 72  t[iRight]].aCoor
4922e 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20 20  d[iDim*2]);.    
4922f 20 20 52 74 72 65 65 44 56 61 6c 75 65 20 78 72    RtreeDValue xr
49230 69 67 68 74 32 20 3d 20 44 43 4f 4f 52 44 28 61  ight2 = DCOORD(a
49231 43 65 6c 6c 5b 61 52 69 67 68 74 5b 69 52 69 67  Cell[aRight[iRig
49232 68 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  ht]].aCoord[iDim
49233 2a 32 2b 31 5d 29 3b 0a 20 20 20 20 20 20 69 66  *2+1]);.      if
49234 28 20 28 69 4c 65 66 74 21 3d 6e 4c 65 66 74 29  ( (iLeft!=nLeft)
49235 20 26 26 20 28 28 69 52 69 67 68 74 3d 3d 6e 52   && ((iRight==nR
49236 69 67 68 74 29 0a 20 20 20 20 20 20 20 7c 7c 20  ight).       || 
49237 28 78 6c 65 66 74 31 3c 78 72 69 67 68 74 31 29  (xleft1<xright1)
49238 0a 20 20 20 20 20 20 20 7c 7c 20 28 78 6c 65 66  .       || (xlef
49239 74 31 3d 3d 78 72 69 67 68 74 31 20 26 26 20 78  t1==xright1 && x
4923a 6c 65 66 74 32 3c 78 72 69 67 68 74 32 29 0a 20  left2<xright2). 
4923b 20 20 20 20 20 29 29 7b 0a 20 20 20 20 20 20 20       )){.       
4923c 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67   aIdx[iLeft+iRig
4923d 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66  ht] = aLeft[iLef
4923e 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 4c 65 66  t];.        iLef
4923f 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  t++;.      }else
49240 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78 5b 69  {.        aIdx[i
49241 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61  Left+iRight] = a
49242 52 69 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20  Right[iRight];. 
49243 20 20 20 20 20 20 20 69 52 69 67 68 74 2b 2b 3b         iRight++;
49244 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
49245 23 69 66 20 30 0a 20 20 20 20 2f 2a 20 43 68 65  #if 0.    /* Che
49246 63 6b 20 74 68 61 74 20 74 68 65 20 73 6f 72 74  ck that the sort
49247 20 77 6f 72 6b 65 64 20 2a 2f 0a 20 20 20 20 7b   worked */.    {
49248 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20  .      int jj;. 
49249 20 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20 6a       for(jj=1; j
4924a 6a 3c 6e 49 64 78 3b 20 6a 6a 2b 2b 29 7b 0a 20  j<nIdx; jj++){. 
4924b 20 20 20 20 20 20 20 52 74 72 65 65 44 56 61 6c         RtreeDVal
4924c 75 65 20 78 6c 65 66 74 31 20 3d 20 61 43 65 6c  ue xleft1 = aCel
4924d 6c 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61 43  l[aIdx[jj-1]].aC
4924e 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 3b 0a 20 20  oord[iDim*2];.  
4924f 20 20 20 20 20 20 52 74 72 65 65 44 56 61 6c 75        RtreeDValu
49250 65 20 78 6c 65 66 74 32 20 3d 20 61 43 65 6c 6c  e xleft2 = aCell
49251 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61 43 6f  [aIdx[jj-1]].aCo
49252 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 3b 0a 20  ord[iDim*2+1];. 
49253 20 20 20 20 20 20 20 52 74 72 65 65 44 56 61 6c         RtreeDVal
49254 75 65 20 78 72 69 67 68 74 31 20 3d 20 61 43 65  ue xright1 = aCe
49255 6c 6c 5b 61 49 64 78 5b 6a 6a 5d 5d 2e 61 43 6f  ll[aIdx[jj]].aCo
49256 6f 72 64 5b 69 44 69 6d 2a 32 5d 3b 0a 20 20 20  ord[iDim*2];.   
49257 20 20 20 20 20 52 74 72 65 65 44 56 61 6c 75 65       RtreeDValue
49258 20 78 72 69 67 68 74 32 20 3d 20 61 43 65 6c 6c   xright2 = aCell
49259 5b 61 49 64 78 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72  [aIdx[jj]].aCoor
4925a 64 5b 69 44 69 6d 2a 32 2b 31 5d 3b 0a 20 20 20  d[iDim*2+1];.   
4925b 20 20 20 20 20 61 73 73 65 72 74 28 20 78 6c 65       assert( xle
4925c 66 74 31 3c 3d 78 72 69 67 68 74 31 20 26 26 20  ft1<=xright1 && 
4925d 28 78 6c 65 66 74 31 3c 78 72 69 67 68 74 31 20  (xleft1<xright1 
4925e 7c 7c 20 78 6c 65 66 74 32 3c 3d 78 72 69 67 68  || xleft2<=xrigh
4925f 74 32 29 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20  t2) );.      }. 
49260 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
49261 7d 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52  }..#if VARIANT_R
49262 53 54 41 52 54 52 45 45 5f 53 50 4c 49 54 0a 2f  STARTREE_SPLIT./
49263 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
49264 69 6f 6e 20 6f 66 20 74 68 65 20 52 2a 2d 74 72  ion of the R*-tr
49265 65 65 20 76 61 72 69 61 6e 74 20 6f 66 20 53 70  ee variant of Sp
49266 6c 69 74 4e 6f 64 65 20 66 72 6f 6d 20 42 65 63  litNode from Bec
49267 6b 6d 61 6e 5b 31 39 39 30 5d 2e 0a 2a 2f 0a 73  kman[1990]..*/.s
49268 74 61 74 69 63 20 69 6e 74 20 73 70 6c 69 74 4e  tatic int splitN
49269 6f 64 65 53 74 61 72 74 72 65 65 28 0a 20 20 52  odeStartree(.  R
4926a 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20  tree *pRtree,.  
4926b 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c  RtreeCell *aCell
4926c 2c 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 0a 20  ,.  int nCell,. 
4926d 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66   RtreeNode *pLef
4926e 74 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  t,.  RtreeNode *
4926f 70 52 69 67 68 74 2c 0a 20 20 52 74 72 65 65 43  pRight,.  RtreeC
49270 65 6c 6c 20 2a 70 42 62 6f 78 4c 65 66 74 2c 0a  ell *pBboxLeft,.
49271 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62    RtreeCell *pBb
49272 6f 78 52 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74  oxRight.){.  int
49273 20 2a 2a 61 61 53 6f 72 74 65 64 3b 0a 20 20 69   **aaSorted;.  i
49274 6e 74 20 2a 61 53 70 61 72 65 3b 0a 20 20 69 6e  nt *aSpare;.  in
49275 74 20 69 69 3b 0a 0a 20 20 69 6e 74 20 69 42 65  t ii;..  int iBe
49276 73 74 44 69 6d 20 3d 20 30 3b 0a 20 20 69 6e 74  stDim = 0;.  int
49277 20 69 42 65 73 74 53 70 6c 69 74 20 3d 20 30 3b   iBestSplit = 0;
49278 0a 20 20 52 74 72 65 65 44 56 61 6c 75 65 20 66  .  RtreeDValue f
49279 42 65 73 74 4d 61 72 67 69 6e 20 3d 20 30 2e 30  BestMargin = 0.0
4927a 3b 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d  ;..  int nByte =
4927b 20 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2b 31   (pRtree->nDim+1
4927c 29 2a 28 73 69 7a 65 6f 66 28 69 6e 74 2a 29 2b  )*(sizeof(int*)+
4927d 6e 43 65 6c 6c 2a 73 69 7a 65 6f 66 28 69 6e 74  nCell*sizeof(int
4927e 29 29 3b 0a 0a 20 20 61 61 53 6f 72 74 65 64 20  ));..  aaSorted 
4927f 3d 20 28 69 6e 74 20 2a 2a 29 73 71 6c 69 74 65  = (int **)sqlite
49280 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b  3_malloc(nByte);
49281 0a 20 20 69 66 28 20 21 61 61 53 6f 72 74 65 64  .  if( !aaSorted
49282 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
49283 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
49284 0a 0a 20 20 61 53 70 61 72 65 20 3d 20 26 28 28  ..  aSpare = &((
49285 69 6e 74 20 2a 29 26 61 61 53 6f 72 74 65 64 5b  int *)&aaSorted[
49286 70 52 74 72 65 65 2d 3e 6e 44 69 6d 5d 29 5b 70  pRtree->nDim])[p
49287 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 6e 43 65 6c  Rtree->nDim*nCel
49288 6c 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 61 53  l];.  memset(aaS
49289 6f 72 74 65 64 2c 20 30 2c 20 6e 42 79 74 65 29  orted, 0, nByte)
4928a 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
4928b 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69  <pRtree->nDim; i
4928c 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a 6a  i++){.    int jj
4928d 3b 0a 20 20 20 20 61 61 53 6f 72 74 65 64 5b 69  ;.    aaSorted[i
4928e 69 5d 20 3d 20 26 28 28 69 6e 74 20 2a 29 26 61  i] = &((int *)&a
4928f 61 53 6f 72 74 65 64 5b 70 52 74 72 65 65 2d 3e  aSorted[pRtree->
49290 6e 44 69 6d 5d 29 5b 69 69 2a 6e 43 65 6c 6c 5d  nDim])[ii*nCell]
49291 3b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20  ;.    for(jj=0; 
49292 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b  jj<nCell; jj++){
49293 0a 20 20 20 20 20 20 61 61 53 6f 72 74 65 64 5b  .      aaSorted[
49294 69 69 5d 5b 6a 6a 5d 20 3d 20 6a 6a 3b 0a 20 20  ii][jj] = jj;.  
49295 20 20 7d 0a 20 20 20 20 53 6f 72 74 42 79 44 69    }.    SortByDi
49296 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20  mension(pRtree, 
49297 61 61 53 6f 72 74 65 64 5b 69 69 5d 2c 20 6e 43  aaSorted[ii], nC
49298 65 6c 6c 2c 20 69 69 2c 20 61 43 65 6c 6c 2c 20  ell, ii, aCell, 
49299 61 53 70 61 72 65 29 3b 0a 20 20 7d 0a 0a 20 20  aSpare);.  }..  
4929a 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 52 74  for(ii=0; ii<pRt
4929b 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 69 2b 2b 29  ree->nDim; ii++)
4929c 7b 0a 20 20 20 20 52 74 72 65 65 44 56 61 6c 75  {.    RtreeDValu
4929d 65 20 6d 61 72 67 69 6e 20 3d 20 30 2e 30 3b 0a  e margin = 0.0;.
4929e 20 20 20 20 52 74 72 65 65 44 56 61 6c 75 65 20      RtreeDValue 
4929f 66 42 65 73 74 4f 76 65 72 6c 61 70 20 3d 20 30  fBestOverlap = 0
492a0 2e 30 3b 0a 20 20 20 20 52 74 72 65 65 44 56 61  .0;.    RtreeDVa
492a1 6c 75 65 20 66 42 65 73 74 41 72 65 61 20 3d 20  lue fBestArea = 
492a2 30 2e 30 3b 0a 20 20 20 20 69 6e 74 20 69 42 65  0.0;.    int iBe
492a3 73 74 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20  stLeft = 0;.    
492a4 69 6e 74 20 6e 4c 65 66 74 3b 0a 0a 20 20 20 20  int nLeft;..    
492a5 66 6f 72 28 0a 20 20 20 20 20 20 6e 4c 65 66 74  for(.      nLeft
492a6 3d 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28  =RTREE_MINCELLS(
492a7 70 52 74 72 65 65 29 3b 20 0a 20 20 20 20 20 20  pRtree); .      
492a8 6e 4c 65 66 74 3c 3d 28 6e 43 65 6c 6c 2d 52 54  nLeft<=(nCell-RT
492a9 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74  REE_MINCELLS(pRt
492aa 72 65 65 29 29 3b 20 0a 20 20 20 20 20 20 6e 4c  ree)); .      nL
492ab 65 66 74 2b 2b 0a 20 20 20 20 29 7b 0a 20 20 20  eft++.    ){.   
492ac 20 20 20 52 74 72 65 65 43 65 6c 6c 20 6c 65 66     RtreeCell lef
492ad 74 3b 0a 20 20 20 20 20 20 52 74 72 65 65 43 65  t;.      RtreeCe
492ae 6c 6c 20 72 69 67 68 74 3b 0a 20 20 20 20 20 20  ll right;.      
492af 69 6e 74 20 6b 6b 3b 0a 20 20 20 20 20 20 52 74  int kk;.      Rt
492b0 72 65 65 44 56 61 6c 75 65 20 6f 76 65 72 6c 61  reeDValue overla
492b1 70 3b 0a 20 20 20 20 20 20 52 74 72 65 65 44 56  p;.      RtreeDV
492b2 61 6c 75 65 20 61 72 65 61 3b 0a 0a 20 20 20 20  alue area;..    
492b3 20 20 6d 65 6d 63 70 79 28 26 6c 65 66 74 2c 20    memcpy(&left, 
492b4 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b  &aCell[aaSorted[
492b5 69 69 5d 5b 30 5d 5d 2c 20 73 69 7a 65 6f 66 28  ii][0]], sizeof(
492b6 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 20  RtreeCell));.   
492b7 20 20 20 6d 65 6d 63 70 79 28 26 72 69 67 68 74     memcpy(&right
492b8 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65  , &aCell[aaSorte
492b9 64 5b 69 69 5d 5b 6e 43 65 6c 6c 2d 31 5d 5d 2c  d[ii][nCell-1]],
492ba 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c   sizeof(RtreeCel
492bb 6c 29 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6b  l));.      for(k
492bc 6b 3d 31 3b 20 6b 6b 3c 28 6e 43 65 6c 6c 2d 31  k=1; kk<(nCell-1
492bd 29 3b 20 6b 6b 2b 2b 29 7b 0a 20 20 20 20 20 20  ); kk++){.      
492be 20 20 69 66 28 20 6b 6b 3c 6e 4c 65 66 74 20 29    if( kk<nLeft )
492bf 7b 0a 20 20 20 20 20 20 20 20 20 20 63 65 6c 6c  {.          cell
492c0 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26 6c  Union(pRtree, &l
492c1 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f  eft, &aCell[aaSo
492c2 72 74 65 64 5b 69 69 5d 5b 6b 6b 5d 5d 29 3b 0a  rted[ii][kk]]);.
492c3 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
492c4 20 20 20 20 20 20 20 20 20 63 65 6c 6c 55 6e 69           cellUni
492c5 6f 6e 28 70 52 74 72 65 65 2c 20 26 72 69 67 68  on(pRtree, &righ
492c6 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74  t, &aCell[aaSort
492c7 65 64 5b 69 69 5d 5b 6b 6b 5d 5d 29 3b 0a 20 20  ed[ii][kk]]);.  
492c8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
492c9 20 20 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d 20        margin += 
492ca 63 65 6c 6c 4d 61 72 67 69 6e 28 70 52 74 72 65  cellMargin(pRtre
492cb 65 2c 20 26 6c 65 66 74 29 3b 0a 20 20 20 20 20  e, &left);.     
492cc 20 6d 61 72 67 69 6e 20 2b 3d 20 63 65 6c 6c 4d   margin += cellM
492cd 61 72 67 69 6e 28 70 52 74 72 65 65 2c 20 26 72  argin(pRtree, &r
492ce 69 67 68 74 29 3b 0a 20 20 20 20 20 20 6f 76 65  ight);.      ove
492cf 72 6c 61 70 20 3d 20 63 65 6c 6c 4f 76 65 72 6c  rlap = cellOverl
492d0 61 70 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74  ap(pRtree, &left
492d1 2c 20 26 72 69 67 68 74 2c 20 31 2c 20 2d 31 29  , &right, 1, -1)
492d2 3b 0a 20 20 20 20 20 20 61 72 65 61 20 3d 20 63  ;.      area = c
492d3 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20  ellArea(pRtree, 
492d4 26 6c 65 66 74 29 20 2b 20 63 65 6c 6c 41 72 65  &left) + cellAre
492d5 61 28 70 52 74 72 65 65 2c 20 26 72 69 67 68 74  a(pRtree, &right
492d6 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 6e 4c  );.      if( (nL
492d7 65 66 74 3d 3d 52 54 52 45 45 5f 4d 49 4e 43 45  eft==RTREE_MINCE
492d8 4c 4c 53 28 70 52 74 72 65 65 29 29 0a 20 20 20  LLS(pRtree)).   
492d9 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c 61 70 3c      || (overlap<
492da 66 42 65 73 74 4f 76 65 72 6c 61 70 29 0a 20 20  fBestOverlap).  
492db 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c 61 70       || (overlap
492dc 3d 3d 66 42 65 73 74 4f 76 65 72 6c 61 70 20 26  ==fBestOverlap &
492dd 26 20 61 72 65 61 3c 66 42 65 73 74 41 72 65 61  & area<fBestArea
492de 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20  ).      ){.     
492df 20 20 20 69 42 65 73 74 4c 65 66 74 20 3d 20 6e     iBestLeft = n
492e0 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 66 42  Left;.        fB
492e1 65 73 74 4f 76 65 72 6c 61 70 20 3d 20 6f 76 65  estOverlap = ove
492e2 72 6c 61 70 3b 0a 20 20 20 20 20 20 20 20 66 42  rlap;.        fB
492e3 65 73 74 41 72 65 61 20 3d 20 61 72 65 61 3b 0a  estArea = area;.
492e4 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
492e5 20 20 20 69 66 28 20 69 69 3d 3d 30 20 7c 7c 20     if( ii==0 || 
492e6 6d 61 72 67 69 6e 3c 66 42 65 73 74 4d 61 72 67  margin<fBestMarg
492e7 69 6e 20 29 7b 0a 20 20 20 20 20 20 69 42 65 73  in ){.      iBes
492e8 74 44 69 6d 20 3d 20 69 69 3b 0a 20 20 20 20 20  tDim = ii;.     
492e9 20 66 42 65 73 74 4d 61 72 67 69 6e 20 3d 20 6d   fBestMargin = m
492ea 61 72 67 69 6e 3b 0a 20 20 20 20 20 20 69 42 65  argin;.      iBe
492eb 73 74 53 70 6c 69 74 20 3d 20 69 42 65 73 74 4c  stSplit = iBestL
492ec 65 66 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  eft;.    }.  }..
492ed 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78 4c 65    memcpy(pBboxLe
492ee 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72  ft, &aCell[aaSor
492ef 74 65 64 5b 69 42 65 73 74 44 69 6d 5d 5b 30 5d  ted[iBestDim][0]
492f0 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43  ], sizeof(RtreeC
492f1 65 6c 6c 29 29 3b 0a 20 20 6d 65 6d 63 70 79 28  ell));.  memcpy(
492f2 70 42 62 6f 78 52 69 67 68 74 2c 20 26 61 43 65  pBboxRight, &aCe
492f3 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 42 65 73  ll[aaSorted[iBes
492f4 74 44 69 6d 5d 5b 69 42 65 73 74 53 70 6c 69 74  tDim][iBestSplit
492f5 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  ]], sizeof(Rtree
492f6 43 65 6c 6c 29 29 3b 0a 20 20 66 6f 72 28 69 69  Cell));.  for(ii
492f7 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69  =0; ii<nCell; ii
492f8 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f  ++){.    RtreeNo
492f9 64 65 20 2a 70 54 61 72 67 65 74 20 3d 20 28 69  de *pTarget = (i
492fa 69 3c 69 42 65 73 74 53 70 6c 69 74 29 3f 70 4c  i<iBestSplit)?pL
492fb 65 66 74 3a 70 52 69 67 68 74 3b 0a 20 20 20 20  eft:pRight;.    
492fc 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78  RtreeCell *pBbox
492fd 20 3d 20 28 69 69 3c 69 42 65 73 74 53 70 6c 69   = (ii<iBestSpli
492fe 74 29 3f 70 42 62 6f 78 4c 65 66 74 3a 70 42 62  t)?pBboxLeft:pBb
492ff 6f 78 52 69 67 68 74 3b 0a 20 20 20 20 52 74 72  oxRight;.    Rtr
49300 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 3d 20  eeCell *pCell = 
49301 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b  &aCell[aaSorted[
49302 69 42 65 73 74 44 69 6d 5d 5b 69 69 5d 5d 3b 0a  iBestDim][ii]];.
49303 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65      nodeInsertCe
49304 6c 6c 28 70 52 74 72 65 65 2c 20 70 54 61 72 67  ll(pRtree, pTarg
49305 65 74 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20  et, pCell);.    
49306 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65  cellUnion(pRtree
49307 2c 20 70 42 62 6f 78 2c 20 70 43 65 6c 6c 29 3b  , pBbox, pCell);
49308 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
49309 66 72 65 65 28 61 61 53 6f 72 74 65 64 29 3b 0a  free(aaSorted);.
4930a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
4930b 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  OK;.}.#endif..#i
4930c 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  f VARIANT_GUTTMA
4930d 4e 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d  N_SPLIT./*.** Im
4930e 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
4930f 74 68 65 20 72 65 67 75 6c 61 72 20 52 2d 74 72  the regular R-tr
49310 65 65 20 53 70 6c 69 74 4e 6f 64 65 20 66 72 6f  ee SplitNode fro
49311 6d 20 47 75 74 74 6d 61 6e 5b 31 39 38 34 5d 2e  m Guttman[1984].
49312 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
49313 70 6c 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e 28  plitNodeGuttman(
49314 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
49315 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61  ,.  RtreeCell *a
49316 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 6e 43 65 6c  Cell,.  int nCel
49317 6c 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  l,.  RtreeNode *
49318 70 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 4e 6f  pLeft,.  RtreeNo
49319 64 65 20 2a 70 52 69 67 68 74 2c 0a 20 20 52 74  de *pRight,.  Rt
4931a 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78 4c 65  reeCell *pBboxLe
4931b 66 74 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ft,.  RtreeCell 
4931c 2a 70 42 62 6f 78 52 69 67 68 74 0a 29 7b 0a 20  *pBboxRight.){. 
4931d 20 69 6e 74 20 69 4c 65 66 74 53 65 65 64 20 3d   int iLeftSeed =
4931e 20 30 3b 0a 20 20 69 6e 74 20 69 52 69 67 68 74   0;.  int iRight
4931f 53 65 65 64 20 3d 20 31 3b 0a 20 20 69 6e 74 20  Seed = 1;.  int 
49320 2a 61 69 55 73 65 64 3b 0a 20 20 69 6e 74 20 69  *aiUsed;.  int i
49321 3b 0a 0a 20 20 61 69 55 73 65 64 20 3d 20 73 71  ;..  aiUsed = sq
49322 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
49323 65 6f 66 28 69 6e 74 29 2a 6e 43 65 6c 6c 29 3b  eof(int)*nCell);
49324 0a 20 20 69 66 28 20 21 61 69 55 73 65 64 20 29  .  if( !aiUsed )
49325 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
49326 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
49327 20 6d 65 6d 73 65 74 28 61 69 55 73 65 64 2c 20   memset(aiUsed, 
49328 30 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e  0, sizeof(int)*n
49329 43 65 6c 6c 29 3b 0a 0a 20 20 50 69 63 6b 53 65  Cell);..  PickSe
4932a 65 64 73 28 70 52 74 72 65 65 2c 20 61 43 65 6c  eds(pRtree, aCel
4932b 6c 2c 20 6e 43 65 6c 6c 2c 20 26 69 4c 65 66 74  l, nCell, &iLeft
4932c 53 65 65 64 2c 20 26 69 52 69 67 68 74 53 65 65  Seed, &iRightSee
4932d 64 29 3b 0a 0a 20 20 6d 65 6d 63 70 79 28 70 42  d);..  memcpy(pB
4932e 62 6f 78 4c 65 66 74 2c 20 26 61 43 65 6c 6c 5b  boxLeft, &aCell[
4932f 69 4c 65 66 74 53 65 65 64 5d 2c 20 73 69 7a 65  iLeftSeed], size
49330 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a  of(RtreeCell));.
49331 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78 52 69    memcpy(pBboxRi
49332 67 68 74 2c 20 26 61 43 65 6c 6c 5b 69 52 69 67  ght, &aCell[iRig
49333 68 74 53 65 65 64 5d 2c 20 73 69 7a 65 6f 66 28  htSeed], sizeof(
49334 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6e  RtreeCell));.  n
49335 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  odeInsertCell(pR
49336 74 72 65 65 2c 20 70 4c 65 66 74 2c 20 26 61 43  tree, pLeft, &aC
49337 65 6c 6c 5b 69 4c 65 66 74 53 65 65 64 5d 29 3b  ell[iLeftSeed]);
49338 0a 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c  .  nodeInsertCel
49339 6c 28 70 52 74 72 65 65 2c 20 70 52 69 67 68 74  l(pRtree, pRight
4933a 2c 20 26 61 43 65 6c 6c 5b 69 52 69 67 68 74 53  , &aCell[iRightS
4933b 65 65 64 5d 29 3b 0a 20 20 61 69 55 73 65 64 5b  eed]);.  aiUsed[
4933c 69 4c 65 66 74 53 65 65 64 5d 20 3d 20 31 3b 0a  iLeftSeed] = 1;.
4933d 20 20 61 69 55 73 65 64 5b 69 52 69 67 68 74 53    aiUsed[iRightS
4933e 65 65 64 5d 20 3d 20 31 3b 0a 0a 20 20 66 6f 72  eed] = 1;..  for
4933f 28 69 3d 6e 43 65 6c 6c 2d 32 3b 20 69 3e 30 3b  (i=nCell-2; i>0;
49340 20 69 2d 2d 29 7b 0a 20 20 20 20 52 74 72 65 65   i--){.    Rtree
49341 43 65 6c 6c 20 2a 70 4e 65 78 74 3b 0a 20 20 20  Cell *pNext;.   
49342 20 70 4e 65 78 74 20 3d 20 50 69 63 6b 4e 65 78   pNext = PickNex
49343 74 28 70 52 74 72 65 65 2c 20 61 43 65 6c 6c 2c  t(pRtree, aCell,
49344 20 6e 43 65 6c 6c 2c 20 70 42 62 6f 78 4c 65 66   nCell, pBboxLef
49345 74 2c 20 70 42 62 6f 78 52 69 67 68 74 2c 20 61  t, pBboxRight, a
49346 69 55 73 65 64 29 3b 0a 20 20 20 20 52 74 72 65  iUsed);.    Rtre
49347 65 44 56 61 6c 75 65 20 64 69 66 66 20 3d 20 20  eDValue diff =  
49348 0a 20 20 20 20 20 20 63 65 6c 6c 47 72 6f 77 74  .      cellGrowt
49349 68 28 70 52 74 72 65 65 2c 20 70 42 62 6f 78 4c  h(pRtree, pBboxL
4934a 65 66 74 2c 20 70 4e 65 78 74 29 20 2d 20 0a 20  eft, pNext) - . 
4934b 20 20 20 20 20 63 65 6c 6c 47 72 6f 77 74 68 28       cellGrowth(
4934c 70 52 74 72 65 65 2c 20 70 42 62 6f 78 52 69 67  pRtree, pBboxRig
4934d 68 74 2c 20 70 4e 65 78 74 29 0a 20 20 20 20 3b  ht, pNext).    ;
4934e 0a 20 20 20 20 69 66 28 20 28 52 54 52 45 45 5f  .    if( (RTREE_
4934f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29  MINCELLS(pRtree)
49350 2d 4e 43 45 4c 4c 28 70 52 69 67 68 74 29 3d 3d  -NCELL(pRight)==
49351 69 29 0a 20 20 20 20 20 7c 7c 20 28 64 69 66 66  i).     || (diff
49352 3e 30 2e 30 20 26 26 20 28 52 54 52 45 45 5f 4d  >0.0 && (RTREE_M
49353 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29 2d  INCELLS(pRtree)-
49354 4e 43 45 4c 4c 28 70 4c 65 66 74 29 21 3d 69 29  NCELL(pLeft)!=i)
49355 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6e  ).    ){.      n
49356 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  odeInsertCell(pR
49357 74 72 65 65 2c 20 70 52 69 67 68 74 2c 20 70 4e  tree, pRight, pN
49358 65 78 74 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c  ext);.      cell
49359 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42  Union(pRtree, pB
4935a 62 6f 78 52 69 67 68 74 2c 20 70 4e 65 78 74 29  boxRight, pNext)
4935b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
4935c 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c     nodeInsertCel
4935d 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2c  l(pRtree, pLeft,
4935e 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 63   pNext);.      c
4935f 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c  ellUnion(pRtree,
49360 20 70 42 62 6f 78 4c 65 66 74 2c 20 70 4e 65 78   pBboxLeft, pNex
49361 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
49362 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 69   sqlite3_free(ai
49363 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  Used);.  return 
49364 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
49365 64 69 66 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  dif..static int 
49366 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28 0a 20  updateMapping(. 
49367 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20   Rtree *pRtree, 
49368 0a 20 20 69 36 34 20 69 52 6f 77 69 64 2c 20 0a  .  i64 iRowid, .
49369 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
4936a 64 65 2c 20 0a 20 20 69 6e 74 20 69 48 65 69 67  de, .  int iHeig
4936b 68 74 0a 29 7b 0a 20 20 69 6e 74 20 28 2a 78 53  ht.){.  int (*xS
4936c 65 74 4d 61 70 70 69 6e 67 29 28 52 74 72 65 65  etMapping)(Rtree
4936d 20 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36   *, sqlite3_int6
4936e 34 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  4, sqlite3_int64
4936f 29 3b 0a 20 20 78 53 65 74 4d 61 70 70 69 6e 67  );.  xSetMapping
49370 20 3d 20 28 28 69 48 65 69 67 68 74 3d 3d 30 29   = ((iHeight==0)
49371 3f 72 6f 77 69 64 57 72 69 74 65 3a 70 61 72 65  ?rowidWrite:pare
49372 6e 74 57 72 69 74 65 29 3b 0a 20 20 69 66 28 20  ntWrite);.  if( 
49373 69 48 65 69 67 68 74 3e 30 20 29 7b 0a 20 20 20  iHeight>0 ){.   
49374 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69   RtreeNode *pChi
49375 6c 64 20 3d 20 6e 6f 64 65 48 61 73 68 4c 6f 6f  ld = nodeHashLoo
49376 6b 75 70 28 70 52 74 72 65 65 2c 20 69 52 6f 77  kup(pRtree, iRow
49377 69 64 29 3b 0a 20 20 20 20 69 66 28 20 70 43 68  id);.    if( pCh
49378 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64  ild ){.      nod
49379 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
4937a 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74   pChild->pParent
4937b 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66  );.      nodeRef
4937c 65 72 65 6e 63 65 28 70 4e 6f 64 65 29 3b 0a 20  erence(pNode);. 
4937d 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70 50 61       pChild->pPa
4937e 72 65 6e 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20  rent = pNode;.  
4937f 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
49380 20 78 53 65 74 4d 61 70 70 69 6e 67 28 70 52 74   xSetMapping(pRt
49381 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70 4e 6f  ree, iRowid, pNo
49382 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 7d 0a 0a 73  de->iNode);.}..s
49383 74 61 74 69 63 20 69 6e 74 20 53 70 6c 69 74 4e  tatic int SplitN
49384 6f 64 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ode(.  Rtree *pR
49385 74 72 65 65 2c 0a 20 20 52 74 72 65 65 4e 6f 64  tree,.  RtreeNod
49386 65 20 2a 70 4e 6f 64 65 2c 0a 20 20 52 74 72 65  e *pNode,.  Rtre
49387 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 0a 20 20  eCell *pCell,.  
49388 69 6e 74 20 69 48 65 69 67 68 74 0a 29 7b 0a 20  int iHeight.){. 
49389 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 65   int i;.  int ne
4938a 77 43 65 6c 6c 49 73 52 69 67 68 74 20 3d 20 30  wCellIsRight = 0
4938b 3b 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ;..  int rc = SQ
4938c 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e  LITE_OK;.  int n
4938d 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f  Cell = NCELL(pNo
4938e 64 65 29 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c  de);.  RtreeCell
4938f 20 2a 61 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 2a   *aCell;.  int *
49390 61 69 55 73 65 64 3b 0a 0a 20 20 52 74 72 65 65  aiUsed;..  Rtree
49391 4e 6f 64 65 20 2a 70 4c 65 66 74 20 3d 20 30 3b  Node *pLeft = 0;
49392 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52  .  RtreeNode *pR
49393 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20 52 74 72  ight = 0;..  Rtr
49394 65 65 43 65 6c 6c 20 6c 65 66 74 62 62 6f 78 3b  eeCell leftbbox;
49395 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 72 69 67  .  RtreeCell rig
49396 68 74 62 62 6f 78 3b 0a 0a 20 20 2f 2a 20 41 6c  htbbox;..  /* Al
49397 6c 6f 63 61 74 65 20 61 6e 20 61 72 72 61 79 20  locate an array 
49398 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 69 74 20  and populate it 
49399 77 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20 70  with a copy of p
4939a 43 65 6c 6c 20 61 6e 64 20 0a 20 20 2a 2a 20 61  Cell and .  ** a
4939b 6c 6c 20 63 65 6c 6c 73 20 66 72 6f 6d 20 6e 6f  ll cells from no
4939c 64 65 20 70 4c 65 66 74 2e 20 54 68 65 6e 20 7a  de pLeft. Then z
4939d 65 72 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  ero the original
4939e 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 43   node..  */.  aC
4939f 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  ell = sqlite3_ma
493a0 6c 6c 6f 63 28 28 73 69 7a 65 6f 66 28 52 74 72  lloc((sizeof(Rtr
493a1 65 65 43 65 6c 6c 29 2b 73 69 7a 65 6f 66 28 69  eeCell)+sizeof(i
493a2 6e 74 29 29 2a 28 6e 43 65 6c 6c 2b 31 29 29 3b  nt))*(nCell+1));
493a3 0a 20 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b  .  if( !aCell ){
493a4 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
493a5 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f  _NOMEM;.    goto
493a6 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a   splitnode_out;.
493a7 20 20 7d 0a 20 20 61 69 55 73 65 64 20 3d 20 28    }.  aiUsed = (
493a8 69 6e 74 20 2a 29 26 61 43 65 6c 6c 5b 6e 43 65  int *)&aCell[nCe
493a9 6c 6c 2b 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 28  ll+1];.  memset(
493aa 61 69 55 73 65 64 2c 20 30 2c 20 73 69 7a 65 6f  aiUsed, 0, sizeo
493ab 66 28 69 6e 74 29 2a 28 6e 43 65 6c 6c 2b 31 29  f(int)*(nCell+1)
493ac 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
493ad 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  nCell; i++){.   
493ae 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74   nodeGetCell(pRt
493af 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 2c 20 26  ree, pNode, i, &
493b0 61 43 65 6c 6c 5b 69 5d 29 3b 0a 20 20 7d 0a 20  aCell[i]);.  }. 
493b1 20 6e 6f 64 65 5a 65 72 6f 28 70 52 74 72 65 65   nodeZero(pRtree
493b2 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 6d 65 6d 63  , pNode);.  memc
493b3 70 79 28 26 61 43 65 6c 6c 5b 6e 43 65 6c 6c 5d  py(&aCell[nCell]
493b4 2c 20 70 43 65 6c 6c 2c 20 73 69 7a 65 6f 66 28  , pCell, sizeof(
493b5 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6e  RtreeCell));.  n
493b6 43 65 6c 6c 2b 2b 3b 0a 0a 20 20 69 66 28 20 70  Cell++;..  if( p
493b7 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29  Node->iNode==1 )
493b8 7b 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 6e  {.    pRight = n
493b9 6f 64 65 4e 65 77 28 70 52 74 72 65 65 2c 20 70  odeNew(pRtree, p
493ba 4e 6f 64 65 29 3b 0a 20 20 20 20 70 4c 65 66 74  Node);.    pLeft
493bb 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52 74 72 65   = nodeNew(pRtre
493bc 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70  e, pNode);.    p
493bd 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2b 2b 3b  Rtree->iDepth++;
493be 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69  .    pNode->isDi
493bf 72 74 79 20 3d 20 31 3b 0a 20 20 20 20 77 72 69  rty = 1;.    wri
493c0 74 65 49 6e 74 31 36 28 70 4e 6f 64 65 2d 3e 7a  teInt16(pNode->z
493c1 44 61 74 61 2c 20 70 52 74 72 65 65 2d 3e 69 44  Data, pRtree->iD
493c2 65 70 74 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  epth);.  }else{.
493c3 20 20 20 20 70 4c 65 66 74 20 3d 20 70 4e 6f 64      pLeft = pNod
493c4 65 3b 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20  e;.    pRight = 
493c5 6e 6f 64 65 4e 65 77 28 70 52 74 72 65 65 2c 20  nodeNew(pRtree, 
493c6 70 4c 65 66 74 2d 3e 70 50 61 72 65 6e 74 29 3b  pLeft->pParent);
493c7 0a 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e  .    nodeReferen
493c8 63 65 28 70 4c 65 66 74 29 3b 0a 20 20 7d 0a 0a  ce(pLeft);.  }..
493c9 20 20 69 66 28 20 21 70 4c 65 66 74 20 7c 7c 20    if( !pLeft || 
493ca 21 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 72  !pRight ){.    r
493cb 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
493cc 3b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74  ;.    goto split
493cd 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  node_out;.  }.. 
493ce 20 6d 65 6d 73 65 74 28 70 4c 65 66 74 2d 3e 7a   memset(pLeft->z
493cf 44 61 74 61 2c 20 30 2c 20 70 52 74 72 65 65 2d  Data, 0, pRtree-
493d0 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 6d  >iNodeSize);.  m
493d1 65 6d 73 65 74 28 70 52 69 67 68 74 2d 3e 7a 44  emset(pRight->zD
493d2 61 74 61 2c 20 30 2c 20 70 52 74 72 65 65 2d 3e  ata, 0, pRtree->
493d3 69 4e 6f 64 65 53 69 7a 65 29 3b 0a 0a 20 20 72  iNodeSize);..  r
493d4 63 20 3d 20 41 73 73 69 67 6e 43 65 6c 6c 73 28  c = AssignCells(
493d5 70 52 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e  pRtree, aCell, n
493d6 43 65 6c 6c 2c 20 70 4c 65 66 74 2c 20 70 52 69  Cell, pLeft, pRi
493d7 67 68 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20  ght, &leftbbox, 
493d8 26 72 69 67 68 74 62 62 6f 78 29 3b 0a 20 20 69  &rightbbox);.  i
493d9 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
493da 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c   ){.    goto spl
493db 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a  itnode_out;.  }.
493dc 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 62 6f 74  .  /* Ensure bot
493dd 68 20 63 68 69 6c 64 20 6e 6f 64 65 73 20 68 61  h child nodes ha
493de 76 65 20 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20  ve node numbers 
493df 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 65 6d  assigned to them
493e0 20 62 79 20 63 61 6c 6c 69 6e 67 0a 20 20 2a 2a   by calling.  **
493e1 20 6e 6f 64 65 57 72 69 74 65 28 29 2e 20 4e 6f   nodeWrite(). No
493e2 64 65 20 70 52 69 67 68 74 20 61 6c 77 61 79 73  de pRight always
493e3 20 6e 65 65 64 73 20 61 20 6e 6f 64 65 20 6e 75   needs a node nu
493e4 6d 62 65 72 2c 20 61 73 20 69 74 20 77 61 73 20  mber, as it was 
493e5 63 72 65 61 74 65 64 0a 20 20 2a 2a 20 62 79 20  created.  ** by 
493e6 6e 6f 64 65 4e 65 77 28 29 20 61 62 6f 76 65 2e  nodeNew() above.
493e7 20 42 75 74 20 6e 6f 64 65 20 70 4c 65 66 74 20   But node pLeft 
493e8 73 6f 6d 65 74 69 6d 65 73 20 61 6c 72 65 61 64  sometimes alread
493e9 79 20 68 61 73 20 61 20 6e 6f 64 65 20 6e 75 6d  y has a node num
493ea 62 65 72 2e 0a 20 20 2a 2a 20 49 6e 20 74 68 69  ber..  ** In thi
493eb 73 20 63 61 73 65 20 61 76 6f 69 64 20 74 68 65  s case avoid the
493ec 20 61 6c 6c 20 74 6f 20 6e 6f 64 65 57 72 69 74   all to nodeWrit
493ed 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  e()..  */.  if( 
493ee 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
493ef 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74 72 65   nodeWrite(pRtre
493f0 65 2c 20 70 52 69 67 68 74 29 29 0a 20 20 20 7c  e, pRight)).   |
493f1 7c 20 28 30 3d 3d 70 4c 65 66 74 2d 3e 69 4e 6f  | (0==pLeft->iNo
493f2 64 65 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21  de && SQLITE_OK!
493f3 3d 28 72 63 20 3d 20 6e 6f 64 65 57 72 69 74 65  =(rc = nodeWrite
493f4 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 29 29  (pRtree, pLeft))
493f5 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ).  ){.    goto 
493f6 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20  splitnode_out;. 
493f7 20 7d 0a 0a 20 20 72 69 67 68 74 62 62 6f 78 2e   }..  rightbbox.
493f8 69 52 6f 77 69 64 20 3d 20 70 52 69 67 68 74 2d  iRowid = pRight-
493f9 3e 69 4e 6f 64 65 3b 0a 20 20 6c 65 66 74 62 62  >iNode;.  leftbb
493fa 6f 78 2e 69 52 6f 77 69 64 20 3d 20 70 4c 65 66  ox.iRowid = pLef
493fb 74 2d 3e 69 4e 6f 64 65 3b 0a 0a 20 20 69 66 28  t->iNode;..  if(
493fc 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31   pNode->iNode==1
493fd 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 74 72   ){.    rc = rtr
493fe 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  eeInsertCell(pRt
493ff 72 65 65 2c 20 70 4c 65 66 74 2d 3e 70 50 61 72  ree, pLeft->pPar
49400 65 6e 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20  ent, &leftbbox, 
49401 69 48 65 69 67 68 74 2b 31 29 3b 0a 20 20 20 20  iHeight+1);.    
49402 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
49403 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  K ){.      goto 
49404 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20  splitnode_out;. 
49405 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
49406 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61    RtreeNode *pPa
49407 72 65 6e 74 20 3d 20 70 4c 65 66 74 2d 3e 70 50  rent = pLeft->pP
49408 61 72 65 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69  arent;.    int i
49409 43 65 6c 6c 3b 0a 20 20 20 20 72 63 20 3d 20 6e  Cell;.    rc = n
4940a 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 70  odeParentIndex(p
4940b 52 74 72 65 65 2c 20 70 4c 65 66 74 2c 20 26 69  Rtree, pLeft, &i
4940c 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72  Cell);.    if( r
4940d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
4940e 20 20 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72        nodeOverwr
4940f 69 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  iteCell(pRtree, 
49410 70 50 61 72 65 6e 74 2c 20 26 6c 65 66 74 62 62  pParent, &leftbb
49411 6f 78 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 20 20  ox, iCell);.    
49412 20 20 72 63 20 3d 20 41 64 6a 75 73 74 54 72 65    rc = AdjustTre
49413 65 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e  e(pRtree, pParen
49414 74 2c 20 26 6c 65 66 74 62 62 6f 78 29 3b 0a 20  t, &leftbbox);. 
49415 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
49416 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
49417 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f      goto splitno
49418 64 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  de_out;.    }.  
49419 7d 0a 20 20 69 66 28 20 28 72 63 20 3d 20 72 74  }.  if( (rc = rt
4941a 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  reeInsertCell(pR
4941b 74 72 65 65 2c 20 70 52 69 67 68 74 2d 3e 70 50  tree, pRight->pP
4941c 61 72 65 6e 74 2c 20 26 72 69 67 68 74 62 62 6f  arent, &rightbbo
4941d 78 2c 20 69 48 65 69 67 68 74 2b 31 29 29 20 29  x, iHeight+1)) )
4941e 7b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74  {.    goto split
4941f 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  node_out;.  }.. 
49420 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 45 4c   for(i=0; i<NCEL
49421 4c 28 70 52 69 67 68 74 29 3b 20 69 2b 2b 29 7b  L(pRight); i++){
49422 0a 20 20 20 20 69 36 34 20 69 52 6f 77 69 64 20  .    i64 iRowid 
49423 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70  = nodeGetRowid(p
49424 52 74 72 65 65 2c 20 70 52 69 67 68 74 2c 20 69  Rtree, pRight, i
49425 29 3b 0a 20 20 20 20 72 63 20 3d 20 75 70 64 61  );.    rc = upda
49426 74 65 4d 61 70 70 69 6e 67 28 70 52 74 72 65 65  teMapping(pRtree
49427 2c 20 69 52 6f 77 69 64 2c 20 70 52 69 67 68 74  , iRowid, pRight
49428 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20  , iHeight);.    
49429 69 66 28 20 69 52 6f 77 69 64 3d 3d 70 43 65 6c  if( iRowid==pCel
4942a 6c 2d 3e 69 52 6f 77 69 64 20 29 7b 0a 20 20 20  l->iRowid ){.   
4942b 20 20 20 6e 65 77 43 65 6c 6c 49 73 52 69 67 68     newCellIsRigh
4942c 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  t = 1;.    }.   
4942d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
4942e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  OK ){.      goto
4942f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a   splitnode_out;.
49430 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
49431 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20  pNode->iNode==1 
49432 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
49433 69 3c 4e 43 45 4c 4c 28 70 4c 65 66 74 29 3b 20  i<NCELL(pLeft); 
49434 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 36 34 20  i++){.      i64 
49435 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74  iRowid = nodeGet
49436 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70 4c  Rowid(pRtree, pL
49437 65 66 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 72  eft, i);.      r
49438 63 20 3d 20 75 70 64 61 74 65 4d 61 70 70 69 6e  c = updateMappin
49439 67 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64  g(pRtree, iRowid
4943a 2c 20 70 4c 65 66 74 2c 20 69 48 65 69 67 68 74  , pLeft, iHeight
4943b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
4943c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
4943d 20 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74        goto split
4943e 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20  node_out;.      
4943f 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  }.    }.  }else 
49440 69 66 28 20 6e 65 77 43 65 6c 6c 49 73 52 69 67  if( newCellIsRig
49441 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20  ht==0 ){.    rc 
49442 3d 20 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28  = updateMapping(
49443 70 52 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69  pRtree, pCell->i
49444 52 6f 77 69 64 2c 20 70 4c 65 66 74 2c 20 69 48  Rowid, pLeft, iH
49445 65 69 67 68 74 29 3b 0a 20 20 7d 0a 0a 20 20 69  eight);.  }..  i
49446 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
49447 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64   ){.    rc = nod
49448 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
49449 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20 70 52   pRight);.    pR
4944a 69 67 68 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  ight = 0;.  }.  
4944b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
4944c 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6e 6f  K ){.    rc = no
4944d 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
4944e 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 70 4c  , pLeft);.    pL
4944f 65 66 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 73 70  eft = 0;.  }..sp
49450 6c 69 74 6e 6f 64 65 5f 6f 75 74 3a 0a 20 20 6e  litnode_out:.  n
49451 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
49452 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 6e 6f  e, pRight);.  no
49453 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
49454 2c 20 70 4c 65 66 74 29 3b 0a 20 20 73 71 6c 69  , pLeft);.  sqli
49455 74 65 33 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b  te3_free(aCell);
49456 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
49457 0a 2f 2a 0a 2a 2a 20 49 66 20 6e 6f 64 65 20 70  ./*.** If node p
49458 4c 65 61 66 20 69 73 20 6e 6f 74 20 74 68 65 20  Leaf is not the 
49459 72 6f 6f 74 20 6f 66 20 74 68 65 20 72 2d 74 72  root of the r-tr
4945a 65 65 20 61 6e 64 20 69 74 73 20 70 50 61 72 65  ee and its pPare
4945b 6e 74 20 70 6f 69 6e 74 65 72 20 69 73 20 0a 2a  nt pointer is .*
4945c 2a 20 73 74 69 6c 6c 20 4e 55 4c 4c 2c 20 6c 6f  * still NULL, lo
4945d 61 64 20 61 6c 6c 20 61 6e 63 65 73 74 6f 72 20  ad all ancestor 
4945e 6e 6f 64 65 73 20 6f 66 20 70 4c 65 61 66 20 69  nodes of pLeaf i
4945f 6e 74 6f 20 6d 65 6d 6f 72 79 20 61 6e 64 20 70  nto memory and p
49460 6f 70 75 6c 61 74 65 0a 2a 2a 20 74 68 65 20 70  opulate.** the p
49461 4c 65 61 66 2d 3e 70 50 61 72 65 6e 74 20 63 68  Leaf->pParent ch
49462 61 69 6e 20 61 6c 6c 20 74 68 65 20 77 61 79 20  ain all the way 
49463 75 70 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e  up to the root n
49464 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ode..**.** This 
49465 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72 65 71  operation is req
49466 75 69 72 65 64 20 77 68 65 6e 20 61 20 72 6f 77  uired when a row
49467 20 69 73 20 64 65 6c 65 74 65 64 20 28 6f 72 20   is deleted (or 
49468 75 70 64 61 74 65 64 20 2d 20 61 6e 20 75 70 64  updated - an upd
49469 61 74 65 0a 2a 2a 20 69 73 20 69 6d 70 6c 65 6d  ate.** is implem
4946a 65 6e 74 65 64 20 61 73 20 61 20 64 65 6c 65 74  ented as a delet
4946b 65 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e  e followed by an
4946c 20 69 6e 73 65 72 74 29 2e 20 53 51 4c 69 74 65   insert). SQLite
4946d 20 70 72 6f 76 69 64 65 73 20 74 68 65 0a 2a 2a   provides the.**
4946e 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72 6f   rowid of the ro
4946f 77 20 74 6f 20 64 65 6c 65 74 65 2c 20 77 68 69  w to delete, whi
49470 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  ch can be used t
49471 6f 20 66 69 6e 64 20 74 68 65 20 6c 65 61 66 20  o find the leaf 
49472 6f 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20  on which.** the 
49473 65 6e 74 72 79 20 72 65 73 69 64 65 73 20 28 61  entry resides (a
49474 72 67 75 6d 65 6e 74 20 70 4c 65 61 66 29 2e 20  rgument pLeaf). 
49475 4f 6e 63 65 20 74 68 65 20 6c 65 61 66 20 69 73  Once the leaf is
49476 20 6c 6f 63 61 74 65 64 2c 20 74 68 69 73 20 0a   located, this .
49477 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  ** function is c
49478 61 6c 6c 65 64 20 74 6f 20 64 65 74 65 72 6d 69  alled to determi
49479 6e 65 20 69 74 73 20 61 6e 63 65 73 74 72 79 2e  ne its ancestry.
4947a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
4947b 69 78 4c 65 61 66 50 61 72 65 6e 74 28 52 74 72  ixLeafParent(Rtr
4947c 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
4947d 65 4e 6f 64 65 20 2a 70 4c 65 61 66 29 7b 0a 20  eNode *pLeaf){. 
4947e 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
4947f 5f 4f 4b 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65  _OK;.  RtreeNode
49480 20 2a 70 43 68 69 6c 64 20 3d 20 70 4c 65 61 66   *pChild = pLeaf
49481 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53  ;.  while( rc==S
49482 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 68 69  QLITE_OK && pChi
49483 6c 64 2d 3e 69 4e 6f 64 65 21 3d 31 20 26 26 20  ld->iNode!=1 && 
49484 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74 3d  pChild->pParent=
49485 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  =0 ){.    int rc
49486 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  2 = SQLITE_OK;  
49487 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74          /* sqlit
49488 65 33 5f 72 65 73 65 74 28 29 20 72 65 74 75 72  e3_reset() retur
49489 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 20 20 73 71  n code */.    sq
4948a 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
4948b 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50 61  (pRtree->pReadPa
4948c 72 65 6e 74 2c 20 31 2c 20 70 43 68 69 6c 64 2d  rent, 1, pChild-
4948d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 72 63 20  >iNode);.    rc 
4948e 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70  = sqlite3_step(p
4948f 52 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65  Rtree->pReadPare
49490 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  nt);.    if( rc=
49491 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
49492 20 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a       RtreeNode *
49493 70 54 65 73 74 3b 20 20 20 20 20 20 20 20 20 20  pTest;          
49494 20 2f 2a 20 55 73 65 64 20 74 6f 20 74 65 73 74   /* Used to test
49495 20 66 6f 72 20 72 65 66 65 72 65 6e 63 65 20 6c   for reference l
49496 6f 6f 70 73 20 2a 2f 0a 20 20 20 20 20 20 69 36  oops */.      i6
49497 34 20 69 4e 6f 64 65 3b 20 20 20 20 20 20 20 20  4 iNode;        
49498 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 64            /* Nod
49499 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 72 65  e number of pare
4949a 6e 74 20 6e 6f 64 65 20 2a 2f 0a 0a 20 20 20 20  nt node */..    
4949b 20 20 2f 2a 20 42 65 66 6f 72 65 20 73 65 74 74    /* Before sett
4949c 69 6e 67 20 70 43 68 69 6c 64 2d 3e 70 50 61 72  ing pChild->pPar
4949d 65 6e 74 2c 20 74 65 73 74 20 74 68 61 74 20 77  ent, test that w
4949e 65 20 61 72 65 20 6e 6f 74 20 63 72 65 61 74 69  e are not creati
4949f 6e 67 20 61 0a 20 20 20 20 20 20 2a 2a 20 6c 6f  ng a.      ** lo
494a0 6f 70 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73  op of references
494a1 20 28 61 73 20 77 65 20 77 6f 75 6c 64 20 69 66   (as we would if
494a2 2c 20 73 61 79 2c 20 70 43 68 69 6c 64 3d 3d 70  , say, pChild==p
494a3 50 61 72 65 6e 74 29 2e 20 57 65 20 64 6f 6e 27  Parent). We don'
494a4 74 0a 20 20 20 20 20 20 2a 2a 20 77 61 6e 74 20  t.      ** want 
494a5 74 6f 20 64 6f 20 74 68 69 73 20 61 73 20 69 74  to do this as it
494a6 20 6c 65 61 64 73 20 74 6f 20 61 20 6d 65 6d 6f   leads to a memo
494a7 72 79 20 6c 65 61 6b 20 77 68 65 6e 20 74 72 79  ry leak when try
494a8 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 0a 20 20  ing to delete.  
494a9 20 20 20 20 2a 2a 20 74 68 65 20 72 65 66 65 72      ** the refer
494aa 65 6e 63 65 64 20 63 6f 75 6e 74 65 64 20 6e 6f  enced counted no
494ab 64 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a 20  de structures.. 
494ac 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 4e       */.      iN
494ad 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ode = sqlite3_co
494ae 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 52 74 72 65  lumn_int64(pRtre
494af 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 2c 20  e->pReadParent, 
494b0 30 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 54  0);.      for(pT
494b1 65 73 74 3d 70 4c 65 61 66 3b 20 70 54 65 73 74  est=pLeaf; pTest
494b2 20 26 26 20 70 54 65 73 74 2d 3e 69 4e 6f 64 65   && pTest->iNode
494b3 21 3d 69 4e 6f 64 65 3b 20 70 54 65 73 74 3d 70  !=iNode; pTest=p
494b4 54 65 73 74 2d 3e 70 50 61 72 65 6e 74 29 3b 0a  Test->pParent);.
494b5 20 20 20 20 20 20 69 66 28 20 21 70 54 65 73 74        if( !pTest
494b6 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 32 20   ){.        rc2 
494b7 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52  = nodeAcquire(pR
494b8 74 72 65 65 2c 20 69 4e 6f 64 65 2c 20 30 2c 20  tree, iNode, 0, 
494b9 26 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74  &pChild->pParent
494ba 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
494bb 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
494bc 33 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e  3_reset(pRtree->
494bd 70 52 65 61 64 50 61 72 65 6e 74 29 3b 0a 20 20  pReadParent);.  
494be 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
494bf 5f 4f 4b 20 29 20 72 63 20 3d 20 72 63 32 3b 0a  _OK ) rc = rc2;.
494c0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
494c1 54 45 5f 4f 4b 20 26 26 20 21 70 43 68 69 6c 64  TE_OK && !pChild
494c2 2d 3e 70 50 61 72 65 6e 74 20 29 20 72 63 20 3d  ->pParent ) rc =
494c3 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
494c4 56 54 41 42 3b 0a 20 20 20 20 70 43 68 69 6c 64  VTAB;.    pChild
494c5 20 3d 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65   = pChild->pPare
494c6 6e 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  nt;.  }.  return
494c7 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69   rc;.}..static i
494c8 6e 74 20 64 65 6c 65 74 65 43 65 6c 6c 28 52 74  nt deleteCell(Rt
494c9 72 65 65 20 2a 2c 20 52 74 72 65 65 4e 6f 64 65  ree *, RtreeNode
494ca 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a   *, int, int);..
494cb 73 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f 76  static int remov
494cc 65 4e 6f 64 65 28 52 74 72 65 65 20 2a 70 52 74  eNode(Rtree *pRt
494cd 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a  ree, RtreeNode *
494ce 70 4e 6f 64 65 2c 20 69 6e 74 20 69 48 65 69 67  pNode, int iHeig
494cf 68 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ht){.  int rc;. 
494d0 20 69 6e 74 20 72 63 32 3b 0a 20 20 52 74 72 65   int rc2;.  Rtre
494d1 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d  eNode *pParent =
494d2 20 30 3b 0a 20 20 69 6e 74 20 69 43 65 6c 6c 3b   0;.  int iCell;
494d3 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 6f 64  ..  assert( pNod
494d4 65 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 0a 20  e->nRef==1 );.. 
494d5 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 65   /* Remove the e
494d6 6e 74 72 79 20 69 6e 20 74 68 65 20 70 61 72 65  ntry in the pare
494d7 6e 74 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 72 63  nt cell. */.  rc
494d8 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64   = nodeParentInd
494d9 65 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ex(pRtree, pNode
494da 2c 20 26 69 43 65 6c 6c 29 3b 0a 20 20 69 66 28  , &iCell);.  if(
494db 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
494dc 7b 0a 20 20 20 20 70 50 61 72 65 6e 74 20 3d 20  {.    pParent = 
494dd 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a  pNode->pParent;.
494de 20 20 20 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65      pNode->pPare
494df 6e 74 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  nt = 0;.    rc =
494e0 20 64 65 6c 65 74 65 43 65 6c 6c 28 70 52 74 72   deleteCell(pRtr
494e1 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 69 43 65  ee, pParent, iCe
494e2 6c 6c 2c 20 69 48 65 69 67 68 74 2b 31 29 3b 0a  ll, iHeight+1);.
494e3 20 20 7d 0a 20 20 72 63 32 20 3d 20 6e 6f 64 65    }.  rc2 = node
494e4 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
494e5 70 50 61 72 65 6e 74 29 3b 0a 20 20 69 66 28 20  pParent);.  if( 
494e6 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
494e7 0a 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20  .    rc = rc2;. 
494e8 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c   }.  if( rc!=SQL
494e9 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
494ea 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
494eb 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 78 78  /* Remove the xx
494ec 78 5f 6e 6f 64 65 20 65 6e 74 72 79 2e 20 2a 2f  x_node entry. */
494ed 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
494ee 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 44  int64(pRtree->pD
494ef 65 6c 65 74 65 4e 6f 64 65 2c 20 31 2c 20 70 4e  eleteNode, 1, pN
494f0 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 73  ode->iNode);.  s
494f1 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
494f2 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29  ee->pDeleteNode)
494f3 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ;.  if( SQLITE_O
494f4 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K!=(rc = sqlite3
494f5 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70  _reset(pRtree->p
494f6 44 65 6c 65 74 65 4e 6f 64 65 29 29 20 29 7b 0a  DeleteNode)) ){.
494f7 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
494f8 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20   }..  /* Remove 
494f9 74 68 65 20 78 78 78 5f 70 61 72 65 6e 74 20 65  the xxx_parent e
494fa 6e 74 72 79 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  ntry. */.  sqlit
494fb 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52  e3_bind_int64(pR
494fc 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72  tree->pDeletePar
494fd 65 6e 74 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69  ent, 1, pNode->i
494fe 4e 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Node);.  sqlite3
494ff 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 44  _step(pRtree->pD
49500 65 6c 65 74 65 50 61 72 65 6e 74 29 3b 0a 20 20  eleteParent);.  
49501 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
49502 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
49503 65 74 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  et(pRtree->pDele
49504 74 65 50 61 72 65 6e 74 29 29 20 29 7b 0a 20 20  teParent)) ){.  
49505 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
49506 0a 20 20 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20  .  .  /* Remove 
49507 74 68 65 20 6e 6f 64 65 20 66 72 6f 6d 20 74 68  the node from th
49508 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73 68  e in-memory hash
49509 20 74 61 62 6c 65 20 61 6e 64 20 6c 69 6e 6b 20   table and link 
4950a 69 74 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65  it into.  ** the
4950b 20 52 74 72 65 65 2e 70 44 65 6c 65 74 65 64 20   Rtree.pDeleted 
4950c 6c 69 73 74 2e 20 49 74 73 20 63 6f 6e 74 65 6e  list. Its conten
4950d 74 73 20 77 69 6c 6c 20 62 65 20 72 65 2d 69 6e  ts will be re-in
4950e 73 65 72 74 65 64 20 6c 61 74 65 72 20 6f 6e 2e  serted later on.
4950f 0a 20 20 2a 2f 0a 20 20 6e 6f 64 65 48 61 73 68  .  */.  nodeHash
49510 44 65 6c 65 74 65 28 70 52 74 72 65 65 2c 20 70  Delete(pRtree, p
49511 4e 6f 64 65 29 3b 0a 20 20 70 4e 6f 64 65 2d 3e  Node);.  pNode->
49512 69 4e 6f 64 65 20 3d 20 69 48 65 69 67 68 74 3b  iNode = iHeight;
49513 0a 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20  .  pNode->pNext 
49514 3d 20 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  = pRtree->pDelet
49515 65 64 3b 0a 20 20 70 4e 6f 64 65 2d 3e 6e 52 65  ed;.  pNode->nRe
49516 66 2b 2b 3b 0a 20 20 70 52 74 72 65 65 2d 3e 70  f++;.  pRtree->p
49517 44 65 6c 65 74 65 64 20 3d 20 70 4e 6f 64 65 3b  Deleted = pNode;
49518 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
49519 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  E_OK;.}..static 
4951a 69 6e 74 20 66 69 78 42 6f 75 6e 64 69 6e 67 42  int fixBoundingB
4951b 6f 78 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ox(Rtree *pRtree
4951c 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
4951d 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65  de){.  RtreeNode
4951e 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64   *pParent = pNod
4951f 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 69 6e  e->pParent;.  in
49520 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
49521 3b 20 0a 20 20 69 66 28 20 70 50 61 72 65 6e 74  ; .  if( pParent
49522 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 20   ){.    int ii; 
49523 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d  .    int nCell =
49524 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20   NCELL(pNode);. 
49525 20 20 20 52 74 72 65 65 43 65 6c 6c 20 62 6f 78     RtreeCell box
49526 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
49527 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
49528 42 6f 75 6e 64 69 6e 67 20 62 6f 78 20 66 6f 72  Bounding box for
49529 20 70 4e 6f 64 65 20 2a 2f 0a 20 20 20 20 6e 6f   pNode */.    no
4952a 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65  deGetCell(pRtree
4952b 2c 20 70 4e 6f 64 65 2c 20 30 2c 20 26 62 6f 78  , pNode, 0, &box
4952c 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 31 3b  );.    for(ii=1;
4952d 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29   ii<nCell; ii++)
4952e 7b 0a 20 20 20 20 20 20 52 74 72 65 65 43 65 6c  {.      RtreeCel
4952f 6c 20 63 65 6c 6c 3b 0a 20 20 20 20 20 20 6e 6f  l cell;.      no
49530 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65  deGetCell(pRtree
49531 2c 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26 63 65  , pNode, ii, &ce
49532 6c 6c 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c 55  ll);.      cellU
49533 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26 62 6f  nion(pRtree, &bo
49534 78 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 7d  x, &cell);.    }
49535 0a 20 20 20 20 62 6f 78 2e 69 52 6f 77 69 64 20  .    box.iRowid 
49536 3d 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3b 0a  = pNode->iNode;.
49537 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 50 61 72      rc = nodePar
49538 65 6e 74 49 6e 64 65 78 28 70 52 74 72 65 65 2c  entIndex(pRtree,
49539 20 70 4e 6f 64 65 2c 20 26 69 69 29 3b 0a 20 20   pNode, &ii);.  
4953a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
4953b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64  _OK ){.      nod
4953c 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c 28 70  eOverwriteCell(p
4953d 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20  Rtree, pParent, 
4953e 26 62 6f 78 2c 20 69 69 29 3b 0a 20 20 20 20 20  &box, ii);.     
4953f 20 72 63 20 3d 20 66 69 78 42 6f 75 6e 64 69 6e   rc = fixBoundin
49540 67 42 6f 78 28 70 52 74 72 65 65 2c 20 70 50 61  gBox(pRtree, pPa
49541 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  rent);.    }.  }
49542 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
49543 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68  ./*.** Delete th
49544 65 20 63 65 6c 6c 20 61 74 20 69 6e 64 65 78 20  e cell at index 
49545 69 43 65 6c 6c 20 6f 66 20 6e 6f 64 65 20 70 4e  iCell of node pN
49546 6f 64 65 2e 20 41 66 74 65 72 20 72 65 6d 6f 76  ode. After remov
49547 69 6e 67 20 74 68 65 0a 2a 2a 20 63 65 6c 6c 2c  ing the.** cell,
49548 20 61 64 6a 75 73 74 20 74 68 65 20 72 2d 74 72   adjust the r-tr
49549 65 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72  ee data structur
4954a 65 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a  e if required..*
4954b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 6c  /.static int del
4954c 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 70  eteCell(Rtree *p
4954d 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
4954e 20 2a 70 4e 6f 64 65 2c 20 69 6e 74 20 69 43 65   *pNode, int iCe
4954f 6c 6c 2c 20 69 6e 74 20 69 48 65 69 67 68 74 29  ll, int iHeight)
49550 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  {.  RtreeNode *p
49551 50 61 72 65 6e 74 3b 0a 20 20 69 6e 74 20 72 63  Parent;.  int rc
49552 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  ;..  if( SQLITE_
49553 4f 4b 21 3d 28 72 63 20 3d 20 66 69 78 4c 65 61  OK!=(rc = fixLea
49554 66 50 61 72 65 6e 74 28 70 52 74 72 65 65 2c 20  fParent(pRtree, 
49555 70 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20 72  pNode)) ){.    r
49556 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
49557 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 63   /* Remove the c
49558 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 6e 6f 64  ell from the nod
49559 65 2e 20 54 68 69 73 20 63 61 6c 6c 20 6a 75 73  e. This call jus
4955a 74 20 6d 6f 76 65 73 20 62 79 74 65 73 20 61 72  t moves bytes ar
4955b 6f 75 6e 64 0a 20 20 2a 2a 20 74 68 65 20 69 6e  ound.  ** the in
4955c 2d 6d 65 6d 6f 72 79 20 6e 6f 64 65 20 69 6d 61  -memory node ima
4955d 67 65 2c 20 73 6f 20 69 74 20 63 61 6e 6e 6f 74  ge, so it cannot
4955e 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 6e 6f   fail..  */.  no
4955f 64 65 44 65 6c 65 74 65 43 65 6c 6c 28 70 52 74  deDeleteCell(pRt
49560 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65 6c  ree, pNode, iCel
49561 6c 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  l);..  /* If the
49562 20 6e 6f 64 65 20 69 73 20 6e 6f 74 20 74 68 65   node is not the
49563 20 74 72 65 65 20 72 6f 6f 74 20 61 6e 64 20 6e   tree root and n
49564 6f 77 20 68 61 73 20 6c 65 73 73 20 74 68 61 6e  ow has less than
49565 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20 2a   the minimum.  *
49566 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c  * number of cell
49567 73 2c 20 72 65 6d 6f 76 65 20 69 74 20 66 72 6f  s, remove it fro
49568 6d 20 74 68 65 20 74 72 65 65 2e 20 4f 74 68 65  m the tree. Othe
49569 72 77 69 73 65 2c 20 75 70 64 61 74 65 20 74 68  rwise, update th
4956a 65 0a 20 20 2a 2a 20 63 65 6c 6c 20 69 6e 20 74  e.  ** cell in t
4956b 68 65 20 70 61 72 65 6e 74 20 6e 6f 64 65 20 73  he parent node s
4956c 6f 20 74 68 61 74 20 69 74 20 74 69 67 68 74 6c  o that it tightl
4956d 79 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 75  y contains the u
4956e 70 64 61 74 65 64 0a 20 20 2a 2a 20 6e 6f 64 65  pdated.  ** node
4956f 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 72 65 6e 74  ..  */.  pParent
49570 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e   = pNode->pParen
49571 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  t;.  assert( pPa
49572 72 65 6e 74 20 7c 7c 20 70 4e 6f 64 65 2d 3e 69  rent || pNode->i
49573 4e 6f 64 65 3d 3d 31 20 29 3b 0a 20 20 69 66 28  Node==1 );.  if(
49574 20 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20   pParent ){.    
49575 69 66 28 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29  if( NCELL(pNode)
49576 3c 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28  <RTREE_MINCELLS(
49577 70 52 74 72 65 65 29 20 29 7b 0a 20 20 20 20 20  pRtree) ){.     
49578 20 72 63 20 3d 20 72 65 6d 6f 76 65 4e 6f 64 65   rc = removeNode
49579 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
4957a 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 65  iHeight);.    }e
4957b 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
4957c 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28 70  fixBoundingBox(p
4957d 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
4957e 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
4957f 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63  rn rc;.}..static
49580 20 69 6e 74 20 52 65 69 6e 73 65 72 74 28 0a 20   int Reinsert(. 
49581 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20   Rtree *pRtree, 
49582 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  .  RtreeNode *pN
49583 6f 64 65 2c 20 0a 20 20 52 74 72 65 65 43 65 6c  ode, .  RtreeCel
49584 6c 20 2a 70 43 65 6c 6c 2c 20 0a 20 20 69 6e 74  l *pCell, .  int
49585 20 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69 6e   iHeight.){.  in
49586 74 20 2a 61 4f 72 64 65 72 3b 0a 20 20 69 6e 74  t *aOrder;.  int
49587 20 2a 61 53 70 61 72 65 3b 0a 20 20 52 74 72 65   *aSpare;.  Rtre
49588 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 3b 0a 20 20  eCell *aCell;.  
49589 52 74 72 65 65 44 56 61 6c 75 65 20 2a 61 44 69  RtreeDValue *aDi
4958a 73 74 61 6e 63 65 3b 0a 20 20 69 6e 74 20 6e 43  stance;.  int nC
4958b 65 6c 6c 3b 0a 20 20 52 74 72 65 65 44 56 61 6c  ell;.  RtreeDVal
4958c 75 65 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b  ue aCenterCoord[
4958d 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53  RTREE_MAX_DIMENS
4958e 49 4f 4e 53 5d 3b 0a 20 20 69 6e 74 20 69 44 69  IONS];.  int iDi
4958f 6d 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69  m;.  int ii;.  i
49590 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
49591 4b 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 6d  K;.  int n;..  m
49592 65 6d 73 65 74 28 61 43 65 6e 74 65 72 43 6f 6f  emset(aCenterCoo
49593 72 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 52 74  rd, 0, sizeof(Rt
49594 72 65 65 44 56 61 6c 75 65 29 2a 52 54 52 45 45  reeDValue)*RTREE
49595 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 29  _MAX_DIMENSIONS)
49596 3b 0a 0a 20 20 6e 43 65 6c 6c 20 3d 20 4e 43 45  ;..  nCell = NCE
49597 4c 4c 28 70 4e 6f 64 65 29 2b 31 3b 0a 20 20 6e  LL(pNode)+1;.  n
49598 20 3d 20 28 6e 43 65 6c 6c 2b 31 29 26 28 7e 31   = (nCell+1)&(~1
49599 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  );..  /* Allocat
4959a 65 20 74 68 65 20 62 75 66 66 65 72 73 20 75 73  e the buffers us
4959b 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 61  ed by this opera
4959c 74 69 6f 6e 2e 20 54 68 65 20 61 6c 6c 6f 63 61  tion. The alloca
4959d 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 65 6c  tion is.  ** rel
4959e 69 6e 71 75 69 73 68 65 64 20 62 65 66 6f 72 65  inquished before
4959f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
495a0 65 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 61  eturns..  */.  a
495a1 43 65 6c 6c 20 3d 20 28 52 74 72 65 65 43 65 6c  Cell = (RtreeCel
495a2 6c 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  l *)sqlite3_mall
495a3 6f 63 28 6e 20 2a 20 28 0a 20 20 20 20 73 69 7a  oc(n * (.    siz
495a4 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 20 20  eof(RtreeCell)  
495a5 20 20 20 2b 20 20 20 20 20 20 20 20 20 2f 2a 20     +         /* 
495a6 61 43 65 6c 6c 20 61 72 72 61 79 20 2a 2f 0a 20  aCell array */. 
495a7 20 20 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 20     sizeof(int)  
495a8 20 20 20 20 20 20 20 20 20 2b 20 20 20 20 20 20           +      
495a9 20 20 20 2f 2a 20 61 4f 72 64 65 72 20 61 72 72     /* aOrder arr
495aa 61 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66  ay */.    sizeof
495ab 28 69 6e 74 29 20 20 20 20 20 20 20 20 20 20 20  (int)           
495ac 2b 20 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70  +         /* aSp
495ad 61 72 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 20  are array */.   
495ae 20 73 69 7a 65 6f 66 28 52 74 72 65 65 44 56 61   sizeof(RtreeDVa
495af 6c 75 65 29 20 20 20 20 20 20 20 20 20 20 20 20  lue)            
495b0 20 2f 2a 20 61 44 69 73 74 61 6e 63 65 20 61 72   /* aDistance ar
495b1 72 61 79 20 2a 2f 0a 20 20 29 29 3b 0a 20 20 69  ray */.  ));.  i
495b2 66 28 20 21 61 43 65 6c 6c 20 29 7b 0a 20 20 20  f( !aCell ){.   
495b3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
495b4 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 4f 72 64  OMEM;.  }.  aOrd
495b5 65 72 20 20 20 20 3d 20 28 69 6e 74 20 2a 29 26  er    = (int *)&
495b6 61 43 65 6c 6c 5b 6e 5d 3b 0a 20 20 61 53 70 61  aCell[n];.  aSpa
495b7 72 65 20 20 20 20 3d 20 28 69 6e 74 20 2a 29 26  re    = (int *)&
495b8 61 4f 72 64 65 72 5b 6e 5d 3b 0a 20 20 61 44 69  aOrder[n];.  aDi
495b9 73 74 61 6e 63 65 20 3d 20 28 52 74 72 65 65 44  stance = (RtreeD
495ba 56 61 6c 75 65 20 2a 29 26 61 53 70 61 72 65 5b  Value *)&aSpare[
495bb 6e 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b  n];..  for(ii=0;
495bc 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29   ii<nCell; ii++)
495bd 7b 0a 20 20 20 20 69 66 28 20 69 69 3d 3d 28 6e  {.    if( ii==(n
495be 43 65 6c 6c 2d 31 29 20 29 7b 0a 20 20 20 20 20  Cell-1) ){.     
495bf 20 6d 65 6d 63 70 79 28 26 61 43 65 6c 6c 5b 69   memcpy(&aCell[i
495c0 69 5d 2c 20 70 43 65 6c 6c 2c 20 73 69 7a 65 6f  i], pCell, sizeo
495c1 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20  f(RtreeCell));. 
495c2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
495c3 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72  nodeGetCell(pRtr
495c4 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26  ee, pNode, ii, &
495c5 61 43 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20  aCell[ii]);.    
495c6 7d 0a 20 20 20 20 61 4f 72 64 65 72 5b 69 69 5d  }.    aOrder[ii]
495c7 20 3d 20 69 69 3b 0a 20 20 20 20 66 6f 72 28 69   = ii;.    for(i
495c8 44 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52 74 72  Dim=0; iDim<pRtr
495c9 65 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b  ee->nDim; iDim++
495ca 29 7b 0a 20 20 20 20 20 20 61 43 65 6e 74 65 72  ){.      aCenter
495cb 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 44  Coord[iDim] += D
495cc 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e  COORD(aCell[ii].
495cd 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b  aCoord[iDim*2]);
495ce 0a 20 20 20 20 20 20 61 43 65 6e 74 65 72 43 6f  .      aCenterCo
495cf 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 44 43 4f  ord[iDim] += DCO
495d0 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43  ORD(aCell[ii].aC
495d1 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b  oord[iDim*2+1]);
495d2 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72  .    }.  }.  for
495d3 28 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52  (iDim=0; iDim<pR
495d4 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d  tree->nDim; iDim
495d5 2b 2b 29 7b 0a 20 20 20 20 61 43 65 6e 74 65 72  ++){.    aCenter
495d6 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 3d 20 28 61  Coord[iDim] = (a
495d7 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d  CenterCoord[iDim
495d8 5d 2f 28 6e 43 65 6c 6c 2a 28 52 74 72 65 65 44  ]/(nCell*(RtreeD
495d9 56 61 6c 75 65 29 32 29 29 3b 0a 20 20 7d 0a 0a  Value)2));.  }..
495da 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e    for(ii=0; ii<n
495db 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  Cell; ii++){.   
495dc 20 61 44 69 73 74 61 6e 63 65 5b 69 69 5d 20 3d   aDistance[ii] =
495dd 20 30 2e 30 3b 0a 20 20 20 20 66 6f 72 28 69 44   0.0;.    for(iD
495de 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52 74 72 65  im=0; iDim<pRtre
495df 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b 29  e->nDim; iDim++)
495e0 7b 0a 20 20 20 20 20 20 52 74 72 65 65 44 56 61  {.      RtreeDVa
495e1 6c 75 65 20 63 6f 6f 72 64 20 3d 20 28 44 43 4f  lue coord = (DCO
495e2 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43  ORD(aCell[ii].aC
495e3 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 20  oord[iDim*2+1]) 
495e4 2d 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  - .             
495e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
495e6 20 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69    DCOORD(aCell[i
495e7 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  i].aCoord[iDim*2
495e8 5d 29 29 3b 0a 20 20 20 20 20 20 61 44 69 73 74  ]));.      aDist
495e9 61 6e 63 65 5b 69 69 5d 20 2b 3d 20 28 63 6f 6f  ance[ii] += (coo
495ea 72 64 2d 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b  rd-aCenterCoord[
495eb 69 44 69 6d 5d 29 2a 28 63 6f 6f 72 64 2d 61 43  iDim])*(coord-aC
495ec 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d  enterCoord[iDim]
495ed 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
495ee 53 6f 72 74 42 79 44 69 73 74 61 6e 63 65 28 61  SortByDistance(a
495ef 4f 72 64 65 72 2c 20 6e 43 65 6c 6c 2c 20 61 44  Order, nCell, aD
495f0 69 73 74 61 6e 63 65 2c 20 61 53 70 61 72 65 29  istance, aSpare)
495f1 3b 0a 20 20 6e 6f 64 65 5a 65 72 6f 28 70 52 74  ;.  nodeZero(pRt
495f2 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 0a 20 20  ree, pNode);..  
495f3 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51  for(ii=0; rc==SQ
495f4 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 28 6e  LITE_OK && ii<(n
495f5 43 65 6c 6c 2d 28 52 54 52 45 45 5f 4d 49 4e 43  Cell-(RTREE_MINC
495f6 45 4c 4c 53 28 70 52 74 72 65 65 29 2b 31 29 29  ELLS(pRtree)+1))
495f7 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72  ; ii++){.    Rtr
495f8 65 65 43 65 6c 6c 20 2a 70 20 3d 20 26 61 43 65  eeCell *p = &aCe
495f9 6c 6c 5b 61 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a  ll[aOrder[ii]];.
495fa 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65      nodeInsertCe
495fb 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
495fc 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70 2d  , p);.    if( p-
495fd 3e 69 52 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e  >iRowid==pCell->
495fe 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  iRowid ){.      
495ff 69 66 28 20 69 48 65 69 67 68 74 3d 3d 30 20 29  if( iHeight==0 )
49600 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72  {.        rc = r
49601 6f 77 69 64 57 72 69 74 65 28 70 52 74 72 65 65  owidWrite(pRtree
49602 2c 20 70 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f  , p->iRowid, pNo
49603 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  de->iNode);.    
49604 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
49605 20 72 63 20 3d 20 70 61 72 65 6e 74 57 72 69 74   rc = parentWrit
49606 65 28 70 52 74 72 65 65 2c 20 70 2d 3e 69 52 6f  e(pRtree, p->iRo
49607 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64  wid, pNode->iNod
49608 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
49609 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  }.  }.  if( rc==
4960a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
4960b 20 72 63 20 3d 20 66 69 78 42 6f 75 6e 64 69 6e   rc = fixBoundin
4960c 67 42 6f 78 28 70 52 74 72 65 65 2c 20 70 4e 6f  gBox(pRtree, pNo
4960d 64 65 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 3b  de);.  }.  for(;
4960e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
4960f 26 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b  & ii<nCell; ii++
49610 29 7b 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61  ){.    /* Find a
49611 20 6e 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74   node to store t
49612 68 69 73 20 63 65 6c 6c 20 69 6e 2e 20 70 4e 6f  his cell in. pNo
49613 64 65 2d 3e 69 4e 6f 64 65 20 63 75 72 72 65 6e  de->iNode curren
49614 74 6c 79 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20  tly contains.   
49615 20 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20 6f   ** the height o
49616 66 20 74 68 65 20 73 75 62 2d 74 72 65 65 20 68  f the sub-tree h
49617 65 61 64 65 64 20 62 79 20 74 68 65 20 63 65 6c  eaded by the cel
49618 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 52 74  l..    */.    Rt
49619 72 65 65 4e 6f 64 65 20 2a 70 49 6e 73 65 72 74  reeNode *pInsert
4961a 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  ;.    RtreeCell 
4961b 2a 70 20 3d 20 26 61 43 65 6c 6c 5b 61 4f 72 64  *p = &aCell[aOrd
4961c 65 72 5b 69 69 5d 5d 3b 0a 20 20 20 20 72 63 20  er[ii]];.    rc 
4961d 3d 20 43 68 6f 6f 73 65 4c 65 61 66 28 70 52 74  = ChooseLeaf(pRt
4961e 72 65 65 2c 20 70 2c 20 69 48 65 69 67 68 74 2c  ree, p, iHeight,
4961f 20 26 70 49 6e 73 65 72 74 29 3b 0a 20 20 20 20   &pInsert);.    
49620 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
49621 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  K ){.      int r
49622 63 32 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72  c2;.      rc = r
49623 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70  treeInsertCell(p
49624 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 2c 20  Rtree, pInsert, 
49625 70 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20  p, iHeight);.   
49626 20 20 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c     rc2 = nodeRel
49627 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 49 6e  ease(pRtree, pIn
49628 73 65 72 74 29 3b 0a 20 20 20 20 20 20 69 66 28  sert);.      if(
49629 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
4962a 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72  {.        rc = r
4962b 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c2;.      }.    
4962c 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  }.  }..  sqlite3
4962d 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b 0a 20 20  _free(aCell);.  
4962e 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
4962f 0a 2a 2a 20 49 6e 73 65 72 74 20 63 65 6c 6c 20  .** Insert cell 
49630 70 43 65 6c 6c 20 69 6e 74 6f 20 6e 6f 64 65 20  pCell into node 
49631 70 4e 6f 64 65 2e 20 4e 6f 64 65 20 70 4e 6f 64  pNode. Node pNod
49632 65 20 69 73 20 74 68 65 20 68 65 61 64 20 6f 66  e is the head of
49633 20 61 20 0a 2a 2a 20 73 75 62 74 72 65 65 20 69   a .** subtree i
49634 48 65 69 67 68 74 20 68 69 67 68 20 28 6c 65 61  Height high (lea
49635 66 20 6e 6f 64 65 73 20 68 61 76 65 20 69 48 65  f nodes have iHe
49636 69 67 68 74 3d 3d 30 29 2e 0a 2a 2f 0a 73 74 61  ight==0)..*/.sta
49637 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e 73  tic int rtreeIns
49638 65 72 74 43 65 6c 6c 28 0a 20 20 52 74 72 65 65  ertCell(.  Rtree
49639 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65   *pRtree,.  Rtre
4963a 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 0a 20 20  eNode *pNode,.  
4963b 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c  RtreeCell *pCell
4963c 2c 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74 0a  ,.  int iHeight.
4963d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
4963e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69  LITE_OK;.  if( i
4963f 48 65 69 67 68 74 3e 30 20 29 7b 0a 20 20 20 20  Height>0 ){.    
49640 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69 6c  RtreeNode *pChil
49641 64 20 3d 20 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b  d = nodeHashLook
49642 75 70 28 70 52 74 72 65 65 2c 20 70 43 65 6c 6c  up(pRtree, pCell
49643 2d 3e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 69  ->iRowid);.    i
49644 66 28 20 70 43 68 69 6c 64 20 29 7b 0a 20 20 20  f( pChild ){.   
49645 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70     nodeRelease(p
49646 52 74 72 65 65 2c 20 70 43 68 69 6c 64 2d 3e 70  Rtree, pChild->p
49647 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 6e  Parent);.      n
49648 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 4e 6f  odeReference(pNo
49649 64 65 29 3b 0a 20 20 20 20 20 20 70 43 68 69 6c  de);.      pChil
4964a 64 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f  d->pParent = pNo
4964b 64 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  de;.    }.  }.  
4964c 69 66 28 20 6e 6f 64 65 49 6e 73 65 72 74 43 65  if( nodeInsertCe
4964d 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
4964e 2c 20 70 43 65 6c 6c 29 20 29 7b 0a 23 69 66 20  , pCell) ){.#if 
4964f 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
49650 45 5f 52 45 49 4e 53 45 52 54 0a 20 20 20 20 69  E_REINSERT.    i
49651 66 28 20 69 48 65 69 67 68 74 3c 3d 70 52 74 72  f( iHeight<=pRtr
49652 65 65 2d 3e 69 52 65 69 6e 73 65 72 74 48 65 69  ee->iReinsertHei
49653 67 68 74 20 7c 7c 20 70 4e 6f 64 65 2d 3e 69 4e  ght || pNode->iN
49654 6f 64 65 3d 3d 31 29 7b 0a 20 20 20 20 20 20 72  ode==1){.      r
49655 63 20 3d 20 53 70 6c 69 74 4e 6f 64 65 28 70 52  c = SplitNode(pR
49656 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65  tree, pNode, pCe
49657 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20  ll, iHeight);.  
49658 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
49659 52 74 72 65 65 2d 3e 69 52 65 69 6e 73 65 72 74  Rtree->iReinsert
4965a 48 65 69 67 68 74 20 3d 20 69 48 65 69 67 68 74  Height = iHeight
4965b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 52 65 69  ;.      rc = Rei
4965c 6e 73 65 72 74 28 70 52 74 72 65 65 2c 20 70 4e  nsert(pRtree, pN
4965d 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48 65 69  ode, pCell, iHei
4965e 67 68 74 29 3b 0a 20 20 20 20 7d 0a 23 65 6c 73  ght);.    }.#els
4965f 65 0a 20 20 20 20 72 63 20 3d 20 53 70 6c 69 74  e.    rc = Split
49660 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  Node(pRtree, pNo
49661 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48 65 69 67  de, pCell, iHeig
49662 68 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65  ht);.#endif.  }e
49663 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 41 64  lse{.    rc = Ad
49664 6a 75 73 74 54 72 65 65 28 70 52 74 72 65 65 2c  justTree(pRtree,
49665 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 29 3b 0a   pNode, pCell);.
49666 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
49667 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
49668 66 28 20 69 48 65 69 67 68 74 3d 3d 30 20 29 7b  f( iHeight==0 ){
49669 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 6f  .        rc = ro
4966a 77 69 64 57 72 69 74 65 28 70 52 74 72 65 65 2c  widWrite(pRtree,
4966b 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20   pCell->iRowid, 
4966c 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20  pNode->iNode);. 
4966d 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
4966e 20 20 20 20 72 63 20 3d 20 70 61 72 65 6e 74 57      rc = parentW
4966f 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 43 65  rite(pRtree, pCe
49670 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64  ll->iRowid, pNod
49671 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20  e->iNode);.     
49672 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
49673 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61  eturn rc;.}..sta
49674 74 69 63 20 69 6e 74 20 72 65 69 6e 73 65 72 74  tic int reinsert
49675 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 52 74 72 65  NodeContent(Rtre
49676 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
49677 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20  Node *pNode){.  
49678 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 72 63  int ii;.  int rc
49679 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
4967a 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c  int nCell = NCEL
4967b 4c 28 70 4e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72  L(pNode);..  for
4967c 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  (ii=0; rc==SQLIT
4967d 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 43 65 6c 6c  E_OK && ii<nCell
4967e 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72  ; ii++){.    Rtr
4967f 65 65 4e 6f 64 65 20 2a 70 49 6e 73 65 72 74 3b  eeNode *pInsert;
49680 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63  .    RtreeCell c
49681 65 6c 6c 3b 0a 20 20 20 20 6e 6f 64 65 47 65 74  ell;.    nodeGet
49682 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f  Cell(pRtree, pNo
49683 64 65 2c 20 69 69 2c 20 26 63 65 6c 6c 29 3b 0a  de, ii, &cell);.
49684 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 20 6e  .    /* Find a n
49685 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68 69  ode to store thi
49686 73 20 63 65 6c 6c 20 69 6e 2e 20 70 4e 6f 64 65  s cell in. pNode
49687 2d 3e 69 4e 6f 64 65 20 63 75 72 72 65 6e 74 6c  ->iNode currentl
49688 79 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 2a  y contains.    *
49689 2a 20 74 68 65 20 68 65 69 67 68 74 20 6f 66 20  * the height of 
4968a 74 68 65 20 73 75 62 2d 74 72 65 65 20 68 65 61  the sub-tree hea
4968b 64 65 64 20 62 79 20 74 68 65 20 63 65 6c 6c 2e  ded by the cell.
4968c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
4968d 20 43 68 6f 6f 73 65 4c 65 61 66 28 70 52 74 72   ChooseLeaf(pRtr
4968e 65 65 2c 20 26 63 65 6c 6c 2c 20 28 69 6e 74 29  ee, &cell, (int)
4968f 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 2c 20 26 70  pNode->iNode, &p
49690 49 6e 73 65 72 74 29 3b 0a 20 20 20 20 69 66 28  Insert);.    if(
49691 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
49692 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b  {.      int rc2;
49693 0a 20 20 20 20 20 20 72 63 20 3d 20 72 74 72 65  .      rc = rtre
49694 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72  eInsertCell(pRtr
49695 65 65 2c 20 70 49 6e 73 65 72 74 2c 20 26 63 65  ee, pInsert, &ce
49696 6c 6c 2c 20 28 69 6e 74 29 70 4e 6f 64 65 2d 3e  ll, (int)pNode->
49697 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 72 63  iNode);.      rc
49698 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  2 = nodeRelease(
49699 70 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 29  pRtree, pInsert)
4969a 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
4969b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
4969c 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20       rc = rc2;. 
4969d 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
4969e 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
4969f 0a 2f 2a 0a 2a 2a 20 53 65 6c 65 63 74 20 61 20  ./*.** Select a 
496a0 63 75 72 72 65 6e 74 6c 79 20 75 6e 75 73 65 64  currently unused
496a1 20 72 6f 77 69 64 20 66 6f 72 20 61 20 6e 65 77   rowid for a new
496a2 20 72 2d 74 72 65 65 20 72 65 63 6f 72 64 2e 0a   r-tree record..
496a3 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65  */.static int ne
496a4 77 52 6f 77 69 64 28 52 74 72 65 65 20 2a 70 52  wRowid(Rtree *pR
496a5 74 72 65 65 2c 20 69 36 34 20 2a 70 69 52 6f 77  tree, i64 *piRow
496a6 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  id){.  int rc;. 
496a7 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75   sqlite3_bind_nu
496a8 6c 6c 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74  ll(pRtree->pWrit
496a9 65 52 6f 77 69 64 2c 20 31 29 3b 0a 20 20 73 71  eRowid, 1);.  sq
496aa 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28  lite3_bind_null(
496ab 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f  pRtree->pWriteRo
496ac 77 69 64 2c 20 32 29 3b 0a 20 20 73 71 6c 69 74  wid, 2);.  sqlit
496ad 65 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e  e3_step(pRtree->
496ae 70 57 72 69 74 65 52 6f 77 69 64 29 3b 0a 20 20  pWriteRowid);.  
496af 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
496b0 65 74 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74  et(pRtree->pWrit
496b1 65 52 6f 77 69 64 29 3b 0a 20 20 2a 70 69 52 6f  eRowid);.  *piRo
496b2 77 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61  wid = sqlite3_la
496b3 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
496b4 70 52 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 72  pRtree->db);.  r
496b5 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
496b6 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 65 6e  ** Remove the en
496b7 74 72 79 20 77 69 74 68 20 72 6f 77 69 64 3d 69  try with rowid=i
496b8 44 65 6c 65 74 65 20 66 72 6f 6d 20 74 68 65 20  Delete from the 
496b9 72 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65  r-tree structure
496ba 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
496bb 72 74 72 65 65 44 65 6c 65 74 65 52 6f 77 69 64  rtreeDeleteRowid
496bc 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
496bd 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 44  sqlite3_int64 iD
496be 65 6c 65 74 65 29 7b 0a 20 20 69 6e 74 20 72 63  elete){.  int rc
496bf 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
496c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
496c1 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 52 74  urn code */.  Rt
496c2 72 65 65 4e 6f 64 65 20 2a 70 4c 65 61 66 20 3d  reeNode *pLeaf =
496c3 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   0;           /*
496c4 20 4c 65 61 66 20 6e 6f 64 65 20 63 6f 6e 74 61   Leaf node conta
496c5 69 6e 69 6e 67 20 72 65 63 6f 72 64 20 69 44 65  ining record iDe
496c6 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43  lete */.  int iC
496c7 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  ell;            
496c8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
496c9 65 78 20 6f 66 20 69 44 65 6c 65 74 65 20 63 65  ex of iDelete ce
496ca 6c 6c 20 69 6e 20 70 4c 65 61 66 20 2a 2f 0a 20  ll in pLeaf */. 
496cb 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 6f 6f   RtreeNode *pRoo
496cc 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
496cd 20 2f 2a 20 52 6f 6f 74 20 6e 6f 64 65 20 6f 66   /* Root node of
496ce 20 72 74 72 65 65 20 73 74 72 75 63 74 75 72 65   rtree structure
496cf 20 2a 2f 0a 0a 0a 20 20 2f 2a 20 4f 62 74 61 69   */...  /* Obtai
496d0 6e 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  n a reference to
496d1 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 74   the root node t
496d2 6f 20 69 6e 69 74 69 61 6c 69 73 65 20 52 74 72  o initialise Rtr
496d3 65 65 2e 69 44 65 70 74 68 20 2a 2f 0a 20 20 72  ee.iDepth */.  r
496d4 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28  c = nodeAcquire(
496d5 70 52 74 72 65 65 2c 20 31 2c 20 30 2c 20 26 70  pRtree, 1, 0, &p
496d6 52 6f 6f 74 29 3b 0a 0a 20 20 2f 2a 20 4f 62 74  Root);..  /* Obt
496d7 61 69 6e 20 61 20 72 65 66 65 72 65 6e 63 65 20  ain a reference 
496d8 74 6f 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65  to the leaf node
496d9 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
496da 68 65 20 65 6e 74 72 79 20 0a 20 20 2a 2a 20 61  he entry .  ** a
496db 62 6f 75 74 20 74 6f 20 62 65 20 64 65 6c 65 74  bout to be delet
496dc 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ed. .  */.  if( 
496dd 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
496de 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 65  .    rc = findLe
496df 61 66 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 69  afNode(pRtree, i
496e0 44 65 6c 65 74 65 2c 20 26 70 4c 65 61 66 29 3b  Delete, &pLeaf);
496e1 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c 65 74  .  }..  /* Delet
496e2 65 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 71 75  e the cell in qu
496e3 65 73 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20  estion from the 
496e4 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20  leaf node. */.  
496e5 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
496e6 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32  K ){.    int rc2
496e7 3b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 52  ;.    rc = nodeR
496e8 6f 77 69 64 49 6e 64 65 78 28 70 52 74 72 65 65  owidIndex(pRtree
496e9 2c 20 70 4c 65 61 66 2c 20 69 44 65 6c 65 74 65  , pLeaf, iDelete
496ea 2c 20 26 69 43 65 6c 6c 29 3b 0a 20 20 20 20 69  , &iCell);.    i
496eb 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
496ec 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 64   ){.      rc = d
496ed 65 6c 65 74 65 43 65 6c 6c 28 70 52 74 72 65 65  eleteCell(pRtree
496ee 2c 20 70 4c 65 61 66 2c 20 69 43 65 6c 6c 2c 20  , pLeaf, iCell, 
496ef 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  0);.    }.    rc
496f0 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  2 = nodeRelease(
496f1 70 52 74 72 65 65 2c 20 70 4c 65 61 66 29 3b 0a  pRtree, pLeaf);.
496f2 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
496f3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
496f4 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 20  c = rc2;.    }. 
496f5 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20   }..  /* Delete 
496f6 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
496f7 67 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 3c  g entry in the <
496f8 72 74 72 65 65 3e 5f 72 6f 77 69 64 20 74 61 62  rtree>_rowid tab
496f9 6c 65 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d  le. */.  if( rc=
496fa 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
496fb 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
496fc 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 44 65  nt64(pRtree->pDe
496fd 6c 65 74 65 52 6f 77 69 64 2c 20 31 2c 20 69 44  leteRowid, 1, iD
496fe 65 6c 65 74 65 29 3b 0a 20 20 20 20 73 71 6c 69  elete);.    sqli
496ff 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d  te3_step(pRtree-
49700 3e 70 44 65 6c 65 74 65 52 6f 77 69 64 29 3b 0a  >pDeleteRowid);.
49701 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
49702 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70  _reset(pRtree->p
49703 44 65 6c 65 74 65 52 6f 77 69 64 29 3b 0a 20 20  DeleteRowid);.  
49704 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  }..  /* Check if
49705 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6e   the root node n
49706 6f 77 20 68 61 73 20 65 78 61 63 74 6c 79 20 6f  ow has exactly o
49707 6e 65 20 63 68 69 6c 64 2e 20 49 66 20 73 6f 2c  ne child. If so,
49708 20 72 65 6d 6f 76 65 0a 20 20 2a 2a 20 69 74 2c   remove.  ** it,
49709 20 73 63 68 65 64 75 6c 65 20 74 68 65 20 63 6f   schedule the co
4970a 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 68  ntents of the ch
4970b 69 6c 64 20 66 6f 72 20 72 65 69 6e 73 65 72 74  ild for reinsert
4970c 69 6f 6e 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65  ion and .  ** re
4970d 64 75 63 65 20 74 68 65 20 74 72 65 65 20 68 65  duce the tree he
4970e 69 67 68 74 20 62 79 20 6f 6e 65 2e 0a 20 20 2a  ight by one..  *
4970f 2a 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 65  *.  ** This is e
49710 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 63 6f 70  quivalent to cop
49711 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74  ying the content
49712 73 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 69  s of the child i
49713 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 72 6f 6f  nto.  ** the roo
49714 74 20 6e 6f 64 65 20 28 74 68 65 20 6f 70 65 72  t node (the oper
49715 61 74 69 6f 6e 20 74 68 61 74 20 47 75 74 6d 61  ation that Gutma
49716 6e 27 73 20 70 61 70 65 72 20 73 61 79 73 20 74  n's paper says t
49717 6f 20 70 65 72 66 6f 72 6d 20 0a 20 20 2a 2a 20  o perform .  ** 
49718 69 6e 20 74 68 69 73 20 73 63 65 6e 61 72 69 6f  in this scenario
49719 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  )..  */.  if( rc
4971a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
4971b 52 74 72 65 65 2d 3e 69 44 65 70 74 68 3e 30 20  Rtree->iDepth>0 
4971c 26 26 20 4e 43 45 4c 4c 28 70 52 6f 6f 74 29 3d  && NCELL(pRoot)=
4971d 3d 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  =1 ){.    int rc
4971e 32 3b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  2;.    RtreeNode
4971f 20 2a 70 43 68 69 6c 64 3b 0a 20 20 20 20 69 36   *pChild;.    i6
49720 34 20 69 43 68 69 6c 64 20 3d 20 6e 6f 64 65 47  4 iChild = nodeG
49721 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20  etRowid(pRtree, 
49722 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 72  pRoot, 0);.    r
49723 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28  c = nodeAcquire(
49724 70 52 74 72 65 65 2c 20 69 43 68 69 6c 64 2c 20  pRtree, iChild, 
49725 70 52 6f 6f 74 2c 20 26 70 43 68 69 6c 64 29 3b  pRoot, &pChild);
49726 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
49727 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
49728 72 63 20 3d 20 72 65 6d 6f 76 65 4e 6f 64 65 28  rc = removeNode(
49729 70 52 74 72 65 65 2c 20 70 43 68 69 6c 64 2c 20  pRtree, pChild, 
4972a 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 31  pRtree->iDepth-1
4972b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 32  );.    }.    rc2
4972c 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
4972d 52 74 72 65 65 2c 20 70 43 68 69 6c 64 29 3b 0a  Rtree, pChild);.
4972e 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
4972f 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 72 63 32  TE_OK ) rc = rc2
49730 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
49731 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
49732 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d   pRtree->iDepth-
49733 2d 3b 0a 20 20 20 20 20 20 77 72 69 74 65 49 6e  -;.      writeIn
49734 74 31 36 28 70 52 6f 6f 74 2d 3e 7a 44 61 74 61  t16(pRoot->zData
49735 2c 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68  , pRtree->iDepth
49736 29 3b 0a 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e  );.      pRoot->
49737 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20 20  isDirty = 1;.   
49738 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 2d   }.  }..  /* Re-
49739 69 6e 73 65 72 74 20 74 68 65 20 63 6f 6e 74 65  insert the conte
4973a 6e 74 73 20 6f 66 20 61 6e 79 20 75 6e 64 65 72  nts of any under
4973b 66 75 6c 6c 20 6e 6f 64 65 73 20 72 65 6d 6f 76  full nodes remov
4973c 65 64 20 66 72 6f 6d 20 74 68 65 20 74 72 65 65  ed from the tree
4973d 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 65 61 66  . */.  for(pLeaf
4973e 3d 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65  =pRtree->pDelete
4973f 64 3b 20 70 4c 65 61 66 3b 20 70 4c 65 61 66 3d  d; pLeaf; pLeaf=
49740 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64  pRtree->pDeleted
49741 29 7b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  ){.    if( rc==S
49742 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
49743 20 20 72 63 20 3d 20 72 65 69 6e 73 65 72 74 4e    rc = reinsertN
49744 6f 64 65 43 6f 6e 74 65 6e 74 28 70 52 74 72 65  odeContent(pRtre
49745 65 2c 20 70 4c 65 61 66 29 3b 0a 20 20 20 20 7d  e, pLeaf);.    }
49746 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 70 44 65  .    pRtree->pDe
49747 6c 65 74 65 64 20 3d 20 70 4c 65 61 66 2d 3e 70  leted = pLeaf->p
49748 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  Next;.    sqlite
49749 33 5f 66 72 65 65 28 70 4c 65 61 66 29 3b 0a 20  3_free(pLeaf);. 
4974a 20 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 65   }..  /* Release
4974b 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 74   the reference t
4974c 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 2e  o the root node.
4974d 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
4974e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
4974f 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
49750 70 52 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a  pRtree, pRoot);.
49751 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 6f 64    }else{.    nod
49752 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
49753 20 70 52 6f 6f 74 29 3b 0a 20 20 7d 0a 0a 20 20   pRoot);.  }..  
49754 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
49755 0a 2a 2a 20 52 6f 75 6e 64 69 6e 67 20 63 6f 6e  .** Rounding con
49756 73 74 61 6e 74 73 20 66 6f 72 20 66 6c 6f 61 74  stants for float
49757 2d 3e 64 6f 75 62 6c 65 20 63 6f 6e 76 65 72 73  ->double convers
49758 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ion..*/.#define 
49759 52 4e 44 54 4f 57 41 52 44 53 20 20 28 31 2e 30  RNDTOWARDS  (1.0
4975a 20 2d 20 31 2e 30 2f 38 33 38 38 36 30 38 2e 30   - 1.0/8388608.0
4975b 29 20 20 2f 2a 20 52 6f 75 6e 64 20 74 6f 77 61  )  /* Round towa
4975c 72 64 73 20 7a 65 72 6f 20 2a 2f 0a 23 64 65 66  rds zero */.#def
4975d 69 6e 65 20 52 4e 44 41 57 41 59 20 20 20 20 20  ine RNDAWAY     
4975e 28 31 2e 30 20 2b 20 31 2e 30 2f 38 33 38 38 36  (1.0 + 1.0/83886
4975f 30 38 2e 30 29 20 20 2f 2a 20 52 6f 75 6e 64 20  08.0)  /* Round 
49760 61 77 61 79 20 66 72 6f 6d 20 7a 65 72 6f 20 2a  away from zero *
49761 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
49762 53 51 4c 49 54 45 5f 52 54 52 45 45 5f 49 4e 54  SQLITE_RTREE_INT
49763 5f 4f 4e 4c 59 29 0a 2f 2a 0a 2a 2a 20 43 6f 6e  _ONLY)./*.** Con
49764 76 65 72 74 20 61 6e 20 73 71 6c 69 74 65 33 5f  vert an sqlite3_
49765 76 61 6c 75 65 20 69 6e 74 6f 20 61 6e 20 52 74  value into an Rt
49766 72 65 65 56 61 6c 75 65 20 28 70 72 65 73 75 6d  reeValue (presum
49767 61 62 6c 79 20 61 20 66 6c 6f 61 74 29 0a 2a 2a  ably a float).**
49768 20 77 68 69 6c 65 20 74 61 6b 69 6e 67 20 63 61   while taking ca
49769 72 65 20 74 6f 20 72 6f 75 6e 64 20 74 6f 77 61  re to round towa
4976a 72 64 20 6e 65 67 61 74 69 76 65 20 6f 72 20 70  rd negative or p
4976b 6f 73 69 74 69 76 65 2c 20 72 65 73 70 65 63 74  ositive, respect
4976c 69 76 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  ively..*/.static
4976d 20 52 74 72 65 65 56 61 6c 75 65 20 72 74 72 65   RtreeValue rtre
4976e 65 56 61 6c 75 65 44 6f 77 6e 28 73 71 6c 69 74  eValueDown(sqlit
4976f 65 33 5f 76 61 6c 75 65 20 2a 76 29 7b 0a 20 20  e3_value *v){.  
49770 64 6f 75 62 6c 65 20 64 20 3d 20 73 71 6c 69 74  double d = sqlit
49771 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
49772 76 29 3b 0a 20 20 66 6c 6f 61 74 20 66 20 3d 20  v);.  float f = 
49773 28 66 6c 6f 61 74 29 64 3b 0a 20 20 69 66 28 20  (float)d;.  if( 
49774 66 3e 64 20 29 7b 0a 20 20 20 20 66 20 3d 20 28  f>d ){.    f = (
49775 66 6c 6f 61 74 29 28 64 2a 28 64 3c 30 20 3f 20  float)(d*(d<0 ? 
49776 52 4e 44 41 57 41 59 20 3a 20 52 4e 44 54 4f 57  RNDAWAY : RNDTOW
49777 41 52 44 53 29 29 3b 0a 20 20 7d 0a 20 20 72 65  ARDS));.  }.  re
49778 74 75 72 6e 20 66 3b 0a 7d 0a 73 74 61 74 69 63  turn f;.}.static
49779 20 52 74 72 65 65 56 61 6c 75 65 20 72 74 72 65   RtreeValue rtre
4977a 65 56 61 6c 75 65 55 70 28 73 71 6c 69 74 65 33  eValueUp(sqlite3
4977b 5f 76 61 6c 75 65 20 2a 76 29 7b 0a 20 20 64 6f  _value *v){.  do
4977c 75 62 6c 65 20 64 20 3d 20 73 71 6c 69 74 65 33  uble d = sqlite3
4977d 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 76 29  _value_double(v)
4977e 3b 0a 20 20 66 6c 6f 61 74 20 66 20 3d 20 28 66  ;.  float f = (f
4977f 6c 6f 61 74 29 64 3b 0a 20 20 69 66 28 20 66 3c  loat)d;.  if( f<
49780 64 20 29 7b 0a 20 20 20 20 66 20 3d 20 28 66 6c  d ){.    f = (fl
49781 6f 61 74 29 28 64 2a 28 64 3c 30 20 3f 20 52 4e  oat)(d*(d<0 ? RN
49782 44 54 4f 57 41 52 44 53 20 3a 20 52 4e 44 41 57  DTOWARDS : RNDAW
49783 41 59 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  AY));.  }.  retu
49784 72 6e 20 66 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  rn f;.}.#endif /
49785 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
49786 45 5f 52 54 52 45 45 5f 49 4e 54 5f 4f 4e 4c 59  E_RTREE_INT_ONLY
49787 29 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ) */.../*.** The
49788 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 20   xUpdate method 
49789 66 6f 72 20 72 74 72 65 65 20 6d 6f 64 75 6c 65  for rtree module
4978a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e   virtual tables.
4978b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
4978c 74 72 65 65 55 70 64 61 74 65 28 0a 20 20 73 71  treeUpdate(.  sq
4978d 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
4978e 62 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c  b, .  int nData,
4978f 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   .  sqlite3_valu
49790 65 20 2a 2a 61 7a 44 61 74 61 2c 20 0a 20 20 73  e **azData, .  s
49791 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52 6f  qlite_int64 *pRo
49792 77 69 64 0a 29 7b 0a 20 20 52 74 72 65 65 20 2a  wid.){.  Rtree *
49793 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20  pRtree = (Rtree 
49794 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72  *)pVtab;.  int r
49795 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
49796 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b   RtreeCell cell;
49797 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49798 20 2f 2a 20 4e 65 77 20 63 65 6c 6c 20 74 6f 20   /* New cell to 
49799 69 6e 73 65 72 74 20 69 66 20 6e 44 61 74 61 3e  insert if nData>
4979a 31 20 2a 2f 0a 20 20 69 6e 74 20 62 48 61 76 65  1 */.  int bHave
4979b 52 6f 77 69 64 20 3d 20 30 3b 20 20 20 20 20 20  Rowid = 0;      
4979c 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f         /* Set to
4979d 20 31 20 61 66 74 65 72 20 6e 65 77 20 72 6f 77   1 after new row
4979e 69 64 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  id is determined
4979f 20 2a 2f 0a 0a 20 20 72 74 72 65 65 52 65 66 65   */..  rtreeRefe
497a0 72 65 6e 63 65 28 70 52 74 72 65 65 29 3b 0a 20  rence(pRtree);. 
497a1 20 61 73 73 65 72 74 28 6e 44 61 74 61 3e 3d 31   assert(nData>=1
497a2 29 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 61  );..  /* Constra
497a3 69 6e 74 20 68 61 6e 64 6c 69 6e 67 2e 20 41 20  int handling. A 
497a4 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20  write operation 
497a5 6f 6e 20 61 6e 20 72 2d 74 72 65 65 20 74 61 62  on an r-tree tab
497a6 6c 65 20 6d 61 79 20 72 65 74 75 72 6e 0a 20 20  le may return.  
497a7 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  ** SQLITE_CONSTR
497a8 41 49 4e 54 20 66 6f 72 20 74 77 6f 20 72 65 61  AINT for two rea
497a9 73 6f 6e 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  sons:.  **.  ** 
497aa 20 20 31 2e 20 41 20 64 75 70 6c 69 63 61 74 65    1. A duplicate
497ab 20 72 6f 77 69 64 20 76 61 6c 75 65 2c 20 6f 72   rowid value, or
497ac 0a 20 20 2a 2a 20 20 20 32 2e 20 54 68 65 20 73  .  **   2. The s
497ad 75 70 70 6c 69 65 64 20 64 61 74 61 20 76 69 6f  upplied data vio
497ae 6c 61 74 65 73 20 74 68 65 20 22 78 32 3e 3d 78  lates the "x2>=x
497af 31 22 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 20  1" constraint.. 
497b0 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 74 68 65 20   **.  ** In the 
497b1 66 69 72 73 74 20 63 61 73 65 2c 20 69 66 20 74  first case, if t
497b2 68 65 20 63 6f 6e 66 6c 69 63 74 2d 68 61 6e 64  he conflict-hand
497b3 6c 69 6e 67 20 6d 6f 64 65 20 69 73 20 52 45 50  ling mode is REP
497b4 4c 41 43 45 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  LACE, then.  ** 
497b5 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20  the conflicting 
497b6 72 6f 77 20 63 61 6e 20 62 65 20 72 65 6d 6f 76  row can be remov
497b7 65 64 20 62 65 66 6f 72 65 20 70 72 6f 63 65 65  ed before procee
497b8 64 69 6e 67 2e 20 49 6e 20 74 68 65 20 73 65 63  ding. In the sec
497b9 6f 6e 64 0a 20 20 2a 2a 20 63 61 73 65 2c 20 53  ond.  ** case, S
497ba 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
497bb 20 6d 75 73 74 20 62 65 20 72 65 74 75 72 6e 65   must be returne
497bc 64 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20  d regardless of 
497bd 74 68 65 0a 20 20 2a 2a 20 63 6f 6e 66 6c 69 63  the.  ** conflic
497be 74 2d 68 61 6e 64 6c 69 6e 67 20 6d 6f 64 65 20  t-handling mode 
497bf 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
497c0 20 75 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66   user..  */.  if
497c1 28 20 6e 44 61 74 61 3e 31 20 29 7b 0a 20 20 20  ( nData>1 ){.   
497c2 20 69 6e 74 20 69 69 3b 0a 0a 20 20 20 20 2f 2a   int ii;..    /*
497c3 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 63 65   Populate the ce
497c4 6c 6c 2e 61 43 6f 6f 72 64 5b 5d 20 61 72 72 61  ll.aCoord[] arra
497c5 79 2e 20 54 68 65 20 66 69 72 73 74 20 63 6f 6f  y. The first coo
497c6 72 64 69 6e 61 74 65 20 69 73 20 61 7a 44 61 74  rdinate is azDat
497c7 61 5b 33 5d 2e 20 2a 2f 0a 20 20 20 20 61 73 73  a[3]. */.    ass
497c8 65 72 74 28 20 6e 44 61 74 61 3d 3d 28 70 52 74  ert( nData==(pRt
497c9 72 65 65 2d 3e 6e 44 69 6d 2a 32 20 2b 20 33 29  ree->nDim*2 + 3)
497ca 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   );.#ifndef SQLI
497cb 54 45 5f 52 54 52 45 45 5f 49 4e 54 5f 4f 4e 4c  TE_RTREE_INT_ONL
497cc 59 0a 20 20 20 20 69 66 28 20 70 52 74 72 65 65  Y.    if( pRtree
497cd 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54  ->eCoordType==RT
497ce 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32  REE_COORD_REAL32
497cf 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 69   ){.      for(ii
497d0 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e  =0; ii<(pRtree->
497d1 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b  nDim*2); ii+=2){
497d2 0a 20 20 20 20 20 20 20 20 63 65 6c 6c 2e 61 43  .        cell.aC
497d3 6f 6f 72 64 5b 69 69 5d 2e 66 20 3d 20 72 74 72  oord[ii].f = rtr
497d4 65 65 56 61 6c 75 65 44 6f 77 6e 28 61 7a 44 61  eeValueDown(azDa
497d5 74 61 5b 69 69 2b 33 5d 29 3b 0a 20 20 20 20 20  ta[ii+3]);.     
497d6 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69     cell.aCoord[i
497d7 69 2b 31 5d 2e 66 20 3d 20 72 74 72 65 65 56 61  i+1].f = rtreeVa
497d8 6c 75 65 55 70 28 61 7a 44 61 74 61 5b 69 69 2b  lueUp(azData[ii+
497d9 34 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  4]);.        if(
497da 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d   cell.aCoord[ii]
497db 2e 66 3e 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69  .f>cell.aCoord[i
497dc 69 2b 31 5d 2e 66 20 29 7b 0a 20 20 20 20 20 20  i+1].f ){.      
497dd 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
497de 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20  CONSTRAINT;.    
497df 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6e 73 74        goto const
497e0 72 61 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 7d  raint;.        }
497e1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
497e2 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a  se.#endif.    {.
497e3 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20        for(ii=0; 
497e4 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ii<(pRtree->nDim
497e5 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20  *2); ii+=2){.   
497e6 20 20 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64       cell.aCoord
497e7 5b 69 69 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33  [ii].i = sqlite3
497e8 5f 76 61 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74  _value_int(azDat
497e9 61 5b 69 69 2b 33 5d 29 3b 0a 20 20 20 20 20 20  a[ii+3]);.      
497ea 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69    cell.aCoord[ii
497eb 2b 31 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33 5f  +1].i = sqlite3_
497ec 76 61 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74 61  value_int(azData
497ed 5b 69 69 2b 34 5d 29 3b 0a 20 20 20 20 20 20 20  [ii+4]);.       
497ee 20 69 66 28 20 63 65 6c 6c 2e 61 43 6f 6f 72 64   if( cell.aCoord
497ef 5b 69 69 5d 2e 69 3e 63 65 6c 6c 2e 61 43 6f 6f  [ii].i>cell.aCoo
497f0 72 64 5b 69 69 2b 31 5d 2e 69 20 29 7b 0a 20 20  rd[ii+1].i ){.  
497f1 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
497f2 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a  ITE_CONSTRAINT;.
497f3 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63            goto c
497f4 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20  onstraint;.     
497f5 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
497f6 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 20   }..    /* If a 
497f7 72 6f 77 69 64 20 76 61 6c 75 65 20 77 61 73 20  rowid value was 
497f8 73 75 70 70 6c 69 65 64 2c 20 63 68 65 63 6b 20  supplied, check 
497f9 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79  if it is already
497fa 20 70 72 65 73 65 6e 74 20 69 6e 20 0a 20 20 20   present in .   
497fb 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20 49   ** the table. I
497fc 66 20 73 6f 2c 20 74 68 65 20 63 6f 6e 73 74 72  f so, the constr
497fd 61 69 6e 74 20 68 61 73 20 66 61 69 6c 65 64 2e  aint has failed.
497fe 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69   */.    if( sqli
497ff 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
49800 7a 44 61 74 61 5b 32 5d 29 21 3d 53 51 4c 49 54  zData[2])!=SQLIT
49801 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  E_NULL ){.      
49802 63 65 6c 6c 2e 69 52 6f 77 69 64 20 3d 20 73 71  cell.iRowid = sq
49803 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
49804 34 28 61 7a 44 61 74 61 5b 32 5d 29 3b 0a 20 20  4(azData[2]);.  
49805 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
49806 76 61 6c 75 65 5f 74 79 70 65 28 61 7a 44 61 74  value_type(azDat
49807 61 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55  a[0])==SQLITE_NU
49808 4c 4c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c  LL.       || sql
49809 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
4980a 28 61 7a 44 61 74 61 5b 30 5d 29 21 3d 63 65 6c  (azData[0])!=cel
4980b 6c 2e 69 52 6f 77 69 64 0a 20 20 20 20 20 20 29  l.iRowid.      )
4980c 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 74  {.        int st
4980d 65 70 72 63 3b 0a 20 20 20 20 20 20 20 20 73 71  eprc;.        sq
4980e 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
4980f 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f  (pRtree->pReadRo
49810 77 69 64 2c 20 31 2c 20 63 65 6c 6c 2e 69 52 6f  wid, 1, cell.iRo
49811 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20 73 74  wid);.        st
49812 65 70 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73  eprc = sqlite3_s
49813 74 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61  tep(pRtree->pRea
49814 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 20  dRowid);.       
49815 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65   rc = sqlite3_re
49816 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65 61  set(pRtree->pRea
49817 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 20  dRowid);.       
49818 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d   if( SQLITE_ROW=
49819 3d 73 74 65 70 72 63 20 29 7b 0a 20 20 20 20 20  =steprc ){.     
4981a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
4981b 5f 76 74 61 62 5f 6f 6e 5f 63 6f 6e 66 6c 69 63  _vtab_on_conflic
4981c 74 28 70 52 74 72 65 65 2d 3e 64 62 29 3d 3d 53  t(pRtree->db)==S
4981d 51 4c 49 54 45 5f 52 45 50 4c 41 43 45 20 29 7b  QLITE_REPLACE ){
4981e 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
4981f 3d 20 72 74 72 65 65 44 65 6c 65 74 65 52 6f 77  = rtreeDeleteRow
49820 69 64 28 70 52 74 72 65 65 2c 20 63 65 6c 6c 2e  id(pRtree, cell.
49821 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 20  iRowid);.       
49822 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
49823 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
49824 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20  E_CONSTRAINT;.  
49825 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63            goto c
49826 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20  onstraint;.     
49827 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
49828 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
49829 48 61 76 65 52 6f 77 69 64 20 3d 20 31 3b 0a 20  HaveRowid = 1;. 
4982a 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
4982b 66 20 61 7a 44 61 74 61 5b 30 5d 20 69 73 20 6e  f azData[0] is n
4982c 6f 74 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76  ot an SQL NULL v
4982d 61 6c 75 65 2c 20 69 74 20 69 73 20 74 68 65 20  alue, it is the 
4982e 72 6f 77 69 64 20 6f 66 20 61 0a 20 20 2a 2a 20  rowid of a.  ** 
4982f 72 65 63 6f 72 64 20 74 6f 20 64 65 6c 65 74 65  record to delete
49830 20 66 72 6f 6d 20 74 68 65 20 72 2d 74 72 65 65   from the r-tree
49831 20 74 61 62 6c 65 2e 20 54 68 65 20 66 6f 6c 6c   table. The foll
49832 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 64 6f 65 73  owing block does
49833 0a 20 20 2a 2a 20 6a 75 73 74 20 74 68 61 74 2e  .  ** just that.
49834 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
49835 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
49836 7a 44 61 74 61 5b 30 5d 29 21 3d 53 51 4c 49 54  zData[0])!=SQLIT
49837 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 72 63  E_NULL ){.    rc
49838 20 3d 20 72 74 72 65 65 44 65 6c 65 74 65 52 6f   = rtreeDeleteRo
49839 77 69 64 28 70 52 74 72 65 65 2c 20 73 71 6c 69  wid(pRtree, sqli
4983a 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28  te3_value_int64(
4983b 61 7a 44 61 74 61 5b 30 5d 29 29 3b 0a 20 20 7d  azData[0]));.  }
4983c 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61 7a  ..  /* If the az
4983d 44 61 74 61 5b 5d 20 61 72 72 61 79 20 63 6f 6e  Data[] array con
4983e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e 20  tains more than 
4983f 6f 6e 65 20 65 6c 65 6d 65 6e 74 2c 20 65 6c 65  one element, ele
49840 6d 65 6e 74 73 0a 20 20 2a 2a 20 28 61 7a 44 61  ments.  ** (azDa
49841 74 61 5b 32 5d 2e 2e 61 7a 44 61 74 61 5b 61 72  ta[2]..azData[ar
49842 67 63 2d 31 5d 29 20 63 6f 6e 74 61 69 6e 20 61  gc-1]) contain a
49843 20 6e 65 77 20 72 65 63 6f 72 64 20 74 6f 20 69   new record to i
49844 6e 73 65 72 74 20 69 6e 74 6f 0a 20 20 2a 2a 20  nsert into.  ** 
49845 74 68 65 20 72 2d 74 72 65 65 20 73 74 72 75 63  the r-tree struc
49846 74 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ture..  */.  if(
49847 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
49848 26 20 6e 44 61 74 61 3e 31 20 29 7b 0a 20 20 20  & nData>1 ){.   
49849 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 65 20 6e   /* Insert the n
4984a 65 77 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74  ew record into t
4984b 68 65 20 72 2d 74 72 65 65 20 2a 2f 0a 20 20 20  he r-tree */.   
4984c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 61   RtreeNode *pLea
4984d 66 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 46  f = 0;..    /* F
4984e 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 72 6f  igure out the ro
4984f 77 69 64 20 6f 66 20 74 68 65 20 6e 65 77 20 72  wid of the new r
49850 6f 77 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 62  ow. */.    if( b
49851 48 61 76 65 52 6f 77 69 64 3d 3d 30 20 29 7b 0a  HaveRowid==0 ){.
49852 20 20 20 20 20 20 72 63 20 3d 20 6e 65 77 52 6f        rc = newRo
49853 77 69 64 28 70 52 74 72 65 65 2c 20 26 63 65 6c  wid(pRtree, &cel
49854 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d  l.iRowid);.    }
49855 0a 20 20 20 20 2a 70 52 6f 77 69 64 20 3d 20 63  .    *pRowid = c
49856 65 6c 6c 2e 69 52 6f 77 69 64 3b 0a 0a 20 20 20  ell.iRowid;..   
49857 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
49858 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
49859 20 43 68 6f 6f 73 65 4c 65 61 66 28 70 52 74 72   ChooseLeaf(pRtr
4985a 65 65 2c 20 26 63 65 6c 6c 2c 20 30 2c 20 26 70  ee, &cell, 0, &p
4985b 4c 65 61 66 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Leaf);.    }.   
4985c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
4985d 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OK ){.      int 
4985e 72 63 32 3b 0a 20 20 20 20 20 20 70 52 74 72 65  rc2;.      pRtre
4985f 65 2d 3e 69 52 65 69 6e 73 65 72 74 48 65 69 67  e->iReinsertHeig
49860 68 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72  ht = -1;.      r
49861 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43  c = rtreeInsertC
49862 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 61  ell(pRtree, pLea
49863 66 2c 20 26 63 65 6c 6c 2c 20 30 29 3b 0a 20 20  f, &cell, 0);.  
49864 20 20 20 20 72 63 32 20 3d 20 6e 6f 64 65 52 65      rc2 = nodeRe
49865 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4c  lease(pRtree, pL
49866 65 61 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20  eaf);.      if( 
49867 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
49868 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63  .        rc = rc
49869 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  2;.      }.    }
4986a 0a 20 20 7d 0a 0a 63 6f 6e 73 74 72 61 69 6e 74  .  }..constraint
4986b 3a 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73 65  :.  rtreeRelease
4986c 28 70 52 74 72 65 65 29 3b 0a 20 20 72 65 74 75  (pRtree);.  retu
4986d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
4986e 54 68 65 20 78 52 65 6e 61 6d 65 20 6d 65 74 68  The xRename meth
4986f 6f 64 20 66 6f 72 20 72 74 72 65 65 20 6d 6f 64  od for rtree mod
49870 75 6c 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ule virtual tabl
49871 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
49872 74 20 72 74 72 65 65 52 65 6e 61 6d 65 28 73 71  t rtreeRename(sq
49873 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
49874 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
49875 4e 65 77 4e 61 6d 65 29 7b 0a 20 20 52 74 72 65  NewName){.  Rtre
49876 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72  e *pRtree = (Rtr
49877 65 65 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e  ee *)pVtab;.  in
49878 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  t rc = SQLITE_NO
49879 4d 45 4d 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71  MEM;.  char *zSq
4987a 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  l = sqlite3_mpri
4987b 6e 74 66 28 0a 20 20 20 20 22 41 4c 54 45 52 20  ntf(.    "ALTER 
4987c 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 6e 6f 64  TABLE %Q.'%q_nod
4987d 65 27 20 20 20 52 45 4e 41 4d 45 20 54 4f 20 5c  e'   RENAME TO \
4987e 22 25 77 5f 6e 6f 64 65 5c 22 3b 22 0a 20 20 20  "%w_node\";".   
4987f 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51   "ALTER TABLE %Q
49880 2e 27 25 71 5f 70 61 72 65 6e 74 27 20 52 45 4e  .'%q_parent' REN
49881 41 4d 45 20 54 4f 20 5c 22 25 77 5f 70 61 72 65  AME TO \"%w_pare
49882 6e 74 5c 22 3b 22 0a 20 20 20 20 22 41 4c 54 45  nt\";".    "ALTE
49883 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 72  R TABLE %Q.'%q_r
49884 6f 77 69 64 27 20 20 52 45 4e 41 4d 45 20 54 4f  owid'  RENAME TO
49885 20 5c 22 25 77 5f 72 6f 77 69 64 5c 22 3b 22 0a   \"%w_rowid\";".
49886 20 20 20 20 2c 20 70 52 74 72 65 65 2d 3e 7a 44      , pRtree->zD
49887 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65  b, pRtree->zName
49888 2c 20 7a 4e 65 77 4e 61 6d 65 20 0a 20 20 20 20  , zNewName .    
49889 2c 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70  , pRtree->zDb, p
4988a 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e  Rtree->zName, zN
4988b 65 77 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70 52  ewName .    , pR
4988c 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65  tree->zDb, pRtre
4988d 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61  e->zName, zNewNa
4988e 6d 65 0a 20 20 29 3b 0a 20 20 69 66 28 20 7a 53  me.  );.  if( zS
4988f 71 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  ql ){.    rc = s
49890 71 6c 69 74 65 33 5f 65 78 65 63 28 70 52 74 72  qlite3_exec(pRtr
49891 65 65 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c  ee->db, zSql, 0,
49892 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
49893 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a  te3_free(zSql);.
49894 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
49895 0a 7d 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74  .}..static sqlit
49896 65 33 5f 6d 6f 64 75 6c 65 20 72 74 72 65 65 4d  e3_module rtreeM
49897 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 20 20  odule = {.  0,  
49898 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49899 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
4989a 69 6f 6e 20 2a 2f 0a 20 20 72 74 72 65 65 43 72  ion */.  rtreeCr
4989b 65 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  eate,           
4989c 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20       /* xCreate 
4989d 2d 20 63 72 65 61 74 65 20 61 20 74 61 62 6c 65  - create a table
4989e 20 2a 2f 0a 20 20 72 74 72 65 65 43 6f 6e 6e 65   */.  rtreeConne
4989f 63 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ct,             
498a0 20 20 2f 2a 20 78 43 6f 6e 6e 65 63 74 20 2d 20    /* xConnect - 
498a1 63 6f 6e 6e 65 63 74 20 74 6f 20 61 6e 20 65 78  connect to an ex
498a2 69 73 74 69 6e 67 20 74 61 62 6c 65 20 2a 2f 0a  isting table */.
498a3 20 20 72 74 72 65 65 42 65 73 74 49 6e 64 65 78    rtreeBestIndex
498a4 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
498a5 20 78 42 65 73 74 49 6e 64 65 78 20 2d 20 44 65   xBestIndex - De
498a6 74 65 72 6d 69 6e 65 20 73 65 61 72 63 68 20 73  termine search s
498a7 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 72 74 72  trategy */.  rtr
498a8 65 65 44 69 73 63 6f 6e 6e 65 63 74 2c 20 20 20  eeDisconnect,   
498a9 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 69 73           /* xDis
498aa 63 6f 6e 6e 65 63 74 20 2d 20 44 69 73 63 6f 6e  connect - Discon
498ab 6e 65 63 74 20 66 72 6f 6d 20 61 20 74 61 62 6c  nect from a tabl
498ac 65 20 2a 2f 0a 20 20 72 74 72 65 65 44 65 73 74  e */.  rtreeDest
498ad 72 6f 79 2c 20 20 20 20 20 20 20 20 20 20 20 20  roy,            
498ae 20 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 2d     /* xDestroy -
498af 20 44 72 6f 70 20 61 20 74 61 62 6c 65 20 2a 2f   Drop a table */
498b0 0a 20 20 72 74 72 65 65 4f 70 65 6e 2c 20 20 20  .  rtreeOpen,   
498b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
498b2 2a 20 78 4f 70 65 6e 20 2d 20 6f 70 65 6e 20 61  * xOpen - open a
498b3 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74 72   cursor */.  rtr
498b4 65 65 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  eeClose,        
498b5 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f           /* xClo
498b6 73 65 20 2d 20 63 6c 6f 73 65 20 61 20 63 75 72  se - close a cur
498b7 73 6f 72 20 2a 2f 0a 20 20 72 74 72 65 65 46 69  sor */.  rtreeFi
498b8 6c 74 65 72 2c 20 20 20 20 20 20 20 20 20 20 20  lter,           
498b9 20 20 20 20 20 2f 2a 20 78 46 69 6c 74 65 72 20       /* xFilter 
498ba 2d 20 63 6f 6e 66 69 67 75 72 65 20 73 63 61 6e  - configure scan
498bb 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
498bc 20 20 72 74 72 65 65 4e 65 78 74 2c 20 20 20 20    rtreeNext,    
498bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
498be 20 78 4e 65 78 74 20 2d 20 61 64 76 61 6e 63 65   xNext - advance
498bf 20 61 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 72   a cursor */.  r
498c0 74 72 65 65 45 6f 66 2c 20 20 20 20 20 20 20 20  treeEof,        
498c1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 45             /* xE
498c2 6f 66 20 2a 2f 0a 20 20 72 74 72 65 65 43 6f 6c  of */.  rtreeCol
498c3 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  umn,            
498c4 20 20 20 20 2f 2a 20 78 43 6f 6c 75 6d 6e 20 2d      /* xColumn -
498c5 20 72 65 61 64 20 64 61 74 61 20 2a 2f 0a 20 20   read data */.  
498c6 72 74 72 65 65 52 6f 77 69 64 2c 20 20 20 20 20  rtreeRowid,     
498c7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
498c8 52 6f 77 69 64 20 2d 20 72 65 61 64 20 64 61 74  Rowid - read dat
498c9 61 20 2a 2f 0a 20 20 72 74 72 65 65 55 70 64 61  a */.  rtreeUpda
498ca 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
498cb 20 20 20 2f 2a 20 78 55 70 64 61 74 65 20 2d 20     /* xUpdate - 
498cc 77 72 69 74 65 20 64 61 74 61 20 2a 2f 0a 20 20  write data */.  
498cd 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
498ce 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
498cf 42 65 67 69 6e 20 2d 20 62 65 67 69 6e 20 74 72  Begin - begin tr
498d0 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 30  ansaction */.  0
498d1 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
498d2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
498d3 79 6e 63 20 2d 20 73 79 6e 63 20 74 72 61 6e 73  ync - sync trans
498d4 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20  action */.  0,  
498d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
498d6 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6f 6d 6d          /* xComm
498d7 69 74 20 2d 20 63 6f 6d 6d 69 74 20 74 72 61 6e  it - commit tran
498d8 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20  saction */.  0, 
498d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
498da 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 6f 6c           /* xRol
498db 6c 62 61 63 6b 20 2d 20 72 6f 6c 6c 62 61 63 6b  lback - rollback
498dc 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a   transaction */.
498dd 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
498de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
498df 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 2d   xFindFunction -
498e0 20 66 75 6e 63 74 69 6f 6e 20 6f 76 65 72 6c 6f   function overlo
498e1 61 64 69 6e 67 20 2a 2f 0a 20 20 72 74 72 65 65  ading */.  rtree
498e2 52 65 6e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  Rename,         
498e3 20 20 20 20 20 20 20 2f 2a 20 78 52 65 6e 61 6d         /* xRenam
498e4 65 20 2d 20 72 65 6e 61 6d 65 20 74 68 65 20 74  e - rename the t
498e5 61 62 6c 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20  able */.  0,    
498e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
498e7 20 20 20 20 20 20 2f 2a 20 78 53 61 76 65 70 6f        /* xSavepo
498e8 69 6e 74 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20  int */.  0,     
498e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
498ea 20 20 20 20 20 2f 2a 20 78 52 65 6c 65 61 73 65       /* xRelease
498eb 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20 20   */.  0         
498ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
498ed 20 20 2f 2a 20 78 52 6f 6c 6c 62 61 63 6b 54 6f    /* xRollbackTo
498ee 20 2a 2f 0a 7d 3b 0a 0a 73 74 61 74 69 63 20 69   */.};..static i
498ef 6e 74 20 72 74 72 65 65 53 71 6c 49 6e 69 74 28  nt rtreeSqlInit(
498f0 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
498f1 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  , .  sqlite3 *db
498f2 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
498f3 2a 7a 44 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63  *zDb, .  const c
498f4 68 61 72 20 2a 7a 50 72 65 66 69 78 2c 20 0a 20  har *zPrefix, . 
498f5 20 69 6e 74 20 69 73 43 72 65 61 74 65 0a 29 7b   int isCreate.){
498f6 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
498f7 54 45 5f 4f 4b 3b 0a 0a 20 20 23 64 65 66 69 6e  TE_OK;..  #defin
498f8 65 20 4e 5f 53 54 41 54 45 4d 45 4e 54 20 39 0a  e N_STATEMENT 9.
498f9 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
498fa 68 61 72 20 2a 61 7a 53 71 6c 5b 4e 5f 53 54 41  har *azSql[N_STA
498fb 54 45 4d 45 4e 54 5d 20 3d 20 7b 0a 20 20 20 20  TEMENT] = {.    
498fc 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 74  /* Read and writ
498fd 65 20 74 68 65 20 78 78 78 5f 6e 6f 64 65 20 74  e the xxx_node t
498fe 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22 53 45 4c  able */.    "SEL
498ff 45 43 54 20 64 61 74 61 20 46 52 4f 4d 20 27 25  ECT data FROM '%
49900 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57 48 45  q'.'%q_node' WHE
49901 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c  RE nodeno = :1",
49902 0a 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20  .    "INSERT OR 
49903 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 27 25 71  REPLACE INTO '%q
49904 27 2e 27 25 71 5f 6e 6f 64 65 27 20 56 41 4c 55  '.'%q_node' VALU
49905 45 53 28 3a 31 2c 20 3a 32 29 22 2c 0a 20 20 20  ES(:1, :2)",.   
49906 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 27 25   "DELETE FROM '%
49907 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57 48 45  q'.'%q_node' WHE
49908 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c  RE nodeno = :1",
49909 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 61 6e  ..    /* Read an
4990a 64 20 77 72 69 74 65 20 74 68 65 20 78 78 78 5f  d write the xxx_
4990b 72 6f 77 69 64 20 74 61 62 6c 65 20 2a 2f 0a 20  rowid table */. 
4990c 20 20 20 22 53 45 4c 45 43 54 20 6e 6f 64 65 6e     "SELECT noden
4990d 6f 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f  o FROM '%q'.'%q_
4990e 72 6f 77 69 64 27 20 57 48 45 52 45 20 72 6f 77  rowid' WHERE row
4990f 69 64 20 3d 20 3a 31 22 2c 0a 20 20 20 20 22 49  id = :1",.    "I
49910 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45  NSERT OR REPLACE
49911 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f 72   INTO '%q'.'%q_r
49912 6f 77 69 64 27 20 56 41 4c 55 45 53 28 3a 31 2c  owid' VALUES(:1,
49913 20 3a 32 29 22 2c 0a 20 20 20 20 22 44 45 4c 45   :2)",.    "DELE
49914 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71  TE FROM '%q'.'%q
49915 5f 72 6f 77 69 64 27 20 57 48 45 52 45 20 72 6f  _rowid' WHERE ro
49916 77 69 64 20 3d 20 3a 31 22 2c 0a 0a 20 20 20 20  wid = :1",..    
49917 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 74  /* Read and writ
49918 65 20 74 68 65 20 78 78 78 5f 70 61 72 65 6e 74  e the xxx_parent
49919 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22 53   table */.    "S
4991a 45 4c 45 43 54 20 70 61 72 65 6e 74 6e 6f 64 65  ELECT parentnode
4991b 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 70   FROM '%q'.'%q_p
4991c 61 72 65 6e 74 27 20 57 48 45 52 45 20 6e 6f 64  arent' WHERE nod
4991d 65 6e 6f 20 3d 20 3a 31 22 2c 0a 20 20 20 20 22  eno = :1",.    "
4991e 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43  INSERT OR REPLAC
4991f 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f  E INTO '%q'.'%q_
49920 70 61 72 65 6e 74 27 20 56 41 4c 55 45 53 28 3a  parent' VALUES(:
49921 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22 44 45  1, :2)",.    "DE
49922 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27  LETE FROM '%q'.'
49923 25 71 5f 70 61 72 65 6e 74 27 20 57 48 45 52 45  %q_parent' WHERE
49924 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 0a 20 20   nodeno = :1".  
49925 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  };.  sqlite3_stm
49926 74 20 2a 2a 61 70 70 53 74 6d 74 5b 4e 5f 53 54  t **appStmt[N_ST
49927 41 54 45 4d 45 4e 54 5d 3b 0a 20 20 69 6e 74 20  ATEMENT];.  int 
49928 69 3b 0a 0a 20 20 70 52 74 72 65 65 2d 3e 64 62  i;..  pRtree->db
49929 20 3d 20 64 62 3b 0a 0a 20 20 69 66 28 20 69 73   = db;..  if( is
4992a 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20 63 68  Create ){.    ch
4992b 61 72 20 2a 7a 43 72 65 61 74 65 20 3d 20 73 71  ar *zCreate = sq
4992c 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 22  lite3_mprintf(."
4992d 43 52 45 41 54 45 20 54 41 42 4c 45 20 5c 22 25  CREATE TABLE \"%
4992e 77 5c 22 2e 5c 22 25 77 5f 6e 6f 64 65 5c 22 28  w\".\"%w_node\"(
4992f 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52 20 50  nodeno INTEGER P
49930 52 49 4d 41 52 59 20 4b 45 59 2c 20 64 61 74 61  RIMARY KEY, data
49931 20 42 4c 4f 42 29 3b 22 0a 22 43 52 45 41 54 45   BLOB);"."CREATE
49932 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e 5c 22   TABLE \"%w\".\"
49933 25 77 5f 72 6f 77 69 64 5c 22 28 72 6f 77 69 64  %w_rowid\"(rowid
49934 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
49935 20 4b 45 59 2c 20 6e 6f 64 65 6e 6f 20 49 4e 54   KEY, nodeno INT
49936 45 47 45 52 29 3b 22 0a 22 43 52 45 41 54 45 20  EGER);"."CREATE 
49937 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25  TABLE \"%w\".\"%
49938 77 5f 70 61 72 65 6e 74 5c 22 28 6e 6f 64 65 6e  w_parent\"(noden
49939 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  o INTEGER PRIMAR
4993a 59 20 4b 45 59 2c 20 70 61 72 65 6e 74 6e 6f 64  Y KEY, parentnod
4993b 65 20 49 4e 54 45 47 45 52 29 3b 22 0a 22 49 4e  e INTEGER);"."IN
4993c 53 45 52 54 20 49 4e 54 4f 20 27 25 71 27 2e 27  SERT INTO '%q'.'
4993d 25 71 5f 6e 6f 64 65 27 20 56 41 4c 55 45 53 28  %q_node' VALUES(
4993e 31 2c 20 7a 65 72 6f 62 6c 6f 62 28 25 64 29 29  1, zeroblob(%d))
4993f 22 2c 0a 20 20 20 20 20 20 7a 44 62 2c 20 7a 50  ",.      zDb, zP
49940 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65  refix, zDb, zPre
49941 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69  fix, zDb, zPrefi
49942 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c  x, zDb, zPrefix,
49943 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69   pRtree->iNodeSi
49944 7a 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66  ze.    );.    if
49945 28 20 21 7a 43 72 65 61 74 65 20 29 7b 0a 20 20  ( !zCreate ){.  
49946 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
49947 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
49948 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
49949 65 78 65 63 28 64 62 2c 20 7a 43 72 65 61 74 65  exec(db, zCreate
4994a 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
4994b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 72  sqlite3_free(zCr
4994c 65 61 74 65 29 3b 0a 20 20 20 20 69 66 28 20 72  eate);.    if( r
4994d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
4994e 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
4994f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 70  .    }.  }..  ap
49950 70 53 74 6d 74 5b 30 5d 20 3d 20 26 70 52 74 72  pStmt[0] = &pRtr
49951 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 3b 0a 20  ee->pReadNode;. 
49952 20 61 70 70 53 74 6d 74 5b 31 5d 20 3d 20 26 70   appStmt[1] = &p
49953 52 74 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64  Rtree->pWriteNod
49954 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b 32 5d 20  e;.  appStmt[2] 
49955 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  = &pRtree->pDele
49956 74 65 4e 6f 64 65 3b 0a 20 20 61 70 70 53 74 6d  teNode;.  appStm
49957 74 5b 33 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[3] = &pRtree->
49958 70 52 65 61 64 52 6f 77 69 64 3b 0a 20 20 61 70  pReadRowid;.  ap
49959 70 53 74 6d 74 5b 34 5d 20 3d 20 26 70 52 74 72  pStmt[4] = &pRtr
4995a 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 3b  ee->pWriteRowid;
4995b 0a 20 20 61 70 70 53 74 6d 74 5b 35 5d 20 3d 20  .  appStmt[5] = 
4995c 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65  &pRtree->pDelete
4995d 52 6f 77 69 64 3b 0a 20 20 61 70 70 53 74 6d 74  Rowid;.  appStmt
4995e 5b 36 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70  [6] = &pRtree->p
4995f 52 65 61 64 50 61 72 65 6e 74 3b 0a 20 20 61 70  ReadParent;.  ap
49960 70 53 74 6d 74 5b 37 5d 20 3d 20 26 70 52 74 72  pStmt[7] = &pRtr
49961 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74  ee->pWriteParent
49962 3b 0a 20 20 61 70 70 53 74 6d 74 5b 38 5d 20 3d  ;.  appStmt[8] =
49963 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74   &pRtree->pDelet
49964 65 50 61 72 65 6e 74 3b 0a 0a 20 20 66 6f 72 28  eParent;..  for(
49965 69 3d 30 3b 20 69 3c 4e 5f 53 54 41 54 45 4d 45  i=0; i<N_STATEME
49966 4e 54 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  NT && rc==SQLITE
49967 5f 4f 4b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63  _OK; i++){.    c
49968 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69  har *zSql = sqli
49969 74 65 33 5f 6d 70 72 69 6e 74 66 28 61 7a 53 71  te3_mprintf(azSq
4996a 6c 5b 69 5d 2c 20 7a 44 62 2c 20 7a 50 72 65 66  l[i], zDb, zPref
4996b 69 78 29 3b 0a 20 20 20 20 69 66 28 20 7a 53 71  ix);.    if( zSq
4996c 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  l ){.      rc = 
4996d 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
4996e 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  v2(db, zSql, -1,
4996f 20 61 70 70 53 74 6d 74 5b 69 5d 2c 20 30 29 3b   appStmt[i], 0);
49970 20 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20   .    }else{.   
49971 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
49972 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
49973 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71  sqlite3_free(zSq
49974 6c 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  l);.  }..  retur
49975 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
49976 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
49977 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  nt to this funct
49978 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ion contains the
49979 20 74 65 78 74 20 6f 66 20 61 6e 20 53 51 4c 20   text of an SQL 
4997a 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 61  statement.** tha
4997b 74 20 72 65 74 75 72 6e 73 20 61 20 73 69 6e 67  t returns a sing
4997c 6c 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  le integer value
4997d 2e 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20  . The statement 
4997e 69 73 20 63 6f 6d 70 69 6c 65 64 20 61 6e 64 20  is compiled and 
4997f 65 78 65 63 75 74 65 64 0a 2a 2a 20 75 73 69 6e  executed.** usin
49980 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  g database conne
49981 63 74 69 6f 6e 20 64 62 2e 20 49 66 20 73 75 63  ction db. If suc
49982 63 65 73 73 66 75 6c 2c 20 74 68 65 20 69 6e 74  cessful, the int
49983 65 67 65 72 20 76 61 6c 75 65 20 72 65 74 75 72  eger value retur
49984 6e 65 64 0a 2a 2a 20 69 73 20 77 72 69 74 74 65  ned.** is writte
49985 6e 20 74 6f 20 2a 70 69 56 61 6c 20 61 6e 64 20  n to *piVal and 
49986 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e  SQLITE_OK return
49987 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61  ed. Otherwise, a
49988 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 0a 2a  n SQLite error.*
49989 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  * code is return
4998a 65 64 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65  ed and the value
4998b 20 6f 66 20 2a 70 69 56 61 6c 20 61 66 74 65 72   of *piVal after
4998c 20 72 65 74 75 72 6e 69 6e 67 20 69 73 20 6e 6f   returning is no
4998d 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74  t defined..*/.st
4998e 61 74 69 63 20 69 6e 74 20 67 65 74 49 6e 74 46  atic int getIntF
4998f 72 6f 6d 53 74 6d 74 28 73 71 6c 69 74 65 33 20  romStmt(sqlite3 
49990 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
49991 2a 7a 53 71 6c 2c 20 69 6e 74 20 2a 70 69 56 61  *zSql, int *piVa
49992 6c 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  l){.  int rc = S
49993 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 69  QLITE_NOMEM;.  i
49994 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 73  f( zSql ){.    s
49995 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
49996 6d 74 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  mt = 0;.    rc =
49997 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
49998 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31  _v2(db, zSql, -1
49999 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20  , &pStmt, 0);.  
4999a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
4999b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28  _OK ){.      if(
4999c 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c   SQLITE_ROW==sql
4999d 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29  ite3_step(pStmt)
4999e 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 69 56   ){.        *piV
4999f 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  al = sqlite3_col
499a0 75 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2c 20 30  umn_int(pStmt, 0
499a1 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
499a2 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69   rc = sqlite3_fi
499a3 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20  nalize(pStmt);. 
499a4 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
499a5 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
499a6 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
499a7 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68  called from with
499a8 69 6e 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 28  in the xConnect(
499a9 29 20 6f 72 20 78 43 72 65 61 74 65 28 29 20 6d  ) or xCreate() m
499aa 65 74 68 6f 64 20 74 6f 0a 2a 2a 20 64 65 74 65  ethod to.** dete
499ab 72 6d 69 6e 65 20 74 68 65 20 6e 6f 64 65 2d 73  rmine the node-s
499ac 69 7a 65 20 75 73 65 64 20 62 79 20 74 68 65 20  ize used by the 
499ad 72 74 72 65 65 20 74 61 62 6c 65 20 62 65 69 6e  rtree table bein
499ae 67 20 63 72 65 61 74 65 64 20 6f 72 20 63 6f 6e  g created or con
499af 6e 65 63 74 65 64 0a 2a 2a 20 74 6f 2e 20 49 66  nected.** to. If
499b0 20 73 75 63 63 65 73 73 66 75 6c 2c 20 70 52 74   successful, pRt
499b1 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 20 69  ree->iNodeSize i
499b2 73 20 70 6f 70 75 6c 61 74 65 64 20 61 6e 64 20  s populated and 
499b3 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e  SQLITE_OK return
499b4 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  ed..** Otherwise
499b5 2c 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  , an SQLite erro
499b6 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
499b7 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69  ed..**.** If thi
499b8 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65  s function is be
499b9 69 6e 67 20 63 61 6c 6c 65 64 20 61 73 20 70 61  ing called as pa
499ba 72 74 20 6f 66 20 61 6e 20 78 43 6f 6e 6e 65 63  rt of an xConnec
499bb 74 28 29 2c 20 74 68 65 6e 20 74 68 65 20 72 74  t(), then the rt
499bc 72 65 65 0a 2a 2a 20 74 61 62 6c 65 20 61 6c 72  ree.** table alr
499bd 65 61 64 79 20 65 78 69 73 74 73 2e 20 49 6e 20  eady exists. In 
499be 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6e 6f  this case the no
499bf 64 65 2d 73 69 7a 65 20 69 73 20 64 65 74 65 72  de-size is deter
499c0 6d 69 6e 65 64 20 62 79 20 69 6e 73 70 65 63 74  mined by inspect
499c1 69 6e 67 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20  ing.** the root 
499c2 6e 6f 64 65 20 6f 66 20 74 68 65 20 74 72 65 65  node of the tree
499c3 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ..**.** Otherwis
499c4 65 2c 20 66 6f 72 20 61 6e 20 78 43 72 65 61 74  e, for an xCreat
499c5 65 28 29 2c 20 75 73 65 20 36 34 20 62 79 74 65  e(), use 64 byte
499c6 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20  s less than the 
499c7 64 61 74 61 62 61 73 65 20 70 61 67 65 2d 73 69  database page-si
499c8 7a 65 2e 20 0a 2a 2a 20 54 68 69 73 20 65 6e 73  ze. .** This ens
499c9 75 72 65 73 20 74 68 61 74 20 65 61 63 68 20 6e  ures that each n
499ca 6f 64 65 20 69 73 20 73 74 6f 72 65 64 20 6f 6e  ode is stored on
499cb 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61   a single databa
499cc 73 65 20 70 61 67 65 2e 20 49 66 20 74 68 65 20  se page. If the 
499cd 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67  .** database pag
499ce 65 2d 73 69 7a 65 20 69 73 20 73 6f 20 6c 61 72  e-size is so lar
499cf 67 65 20 74 68 61 74 20 6d 6f 72 65 20 74 68 61  ge that more tha
499d0 6e 20 52 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53  n RTREE_MAXCELLS
499d1 20 65 6e 74 72 69 65 73 20 0a 2a 2a 20 77 6f 75   entries .** wou
499d2 6c 64 20 66 69 74 20 69 6e 20 61 20 73 69 6e 67  ld fit in a sing
499d3 6c 65 20 6e 6f 64 65 2c 20 75 73 65 20 61 20 73  le node, use a s
499d4 6d 61 6c 6c 65 72 20 6e 6f 64 65 2d 73 69 7a 65  maller node-size
499d5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
499d6 67 65 74 4e 6f 64 65 53 69 7a 65 28 0a 20 20 73  getNodeSize(.  s
499d7 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
499d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
499d9 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
499da 65 20 2a 2f 0a 20 20 52 74 72 65 65 20 2a 70 52  e */.  Rtree *pR
499db 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20  tree,           
499dc 20 20 20 20 20 20 20 2f 2a 20 52 74 72 65 65 20         /* Rtree 
499dd 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  handle */.  int 
499de 69 73 43 72 65 61 74 65 20 20 20 20 20 20 20 20  isCreate        
499df 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
499e0 72 75 65 20 66 6f 72 20 78 43 72 65 61 74 65 2c  rue for xCreate,
499e1 20 66 61 6c 73 65 20 66 6f 72 20 78 43 6f 6e 6e   false for xConn
499e2 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ect */.){.  int 
499e3 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c  rc;.  char *zSql
499e4 3b 0a 20 20 69 66 28 20 69 73 43 72 65 61 74 65  ;.  if( isCreate
499e5 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 50 61 67   ){.    int iPag
499e6 65 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7a  eSize = 0;.    z
499e7 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Sql = sqlite3_mp
499e8 72 69 6e 74 66 28 22 50 52 41 47 4d 41 20 25 51  rintf("PRAGMA %Q
499e9 2e 70 61 67 65 5f 73 69 7a 65 22 2c 20 70 52 74  .page_size", pRt
499ea 72 65 65 2d 3e 7a 44 62 29 3b 0a 20 20 20 20 72  ree->zDb);.    r
499eb 63 20 3d 20 67 65 74 49 6e 74 46 72 6f 6d 53 74  c = getIntFromSt
499ec 6d 74 28 64 62 2c 20 7a 53 71 6c 2c 20 26 69 50  mt(db, zSql, &iP
499ed 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 69 66  ageSize);.    if
499ee 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
499ef 29 7b 0a 20 20 20 20 20 20 70 52 74 72 65 65 2d  ){.      pRtree-
499f0 3e 69 4e 6f 64 65 53 69 7a 65 20 3d 20 69 50 61  >iNodeSize = iPa
499f1 67 65 53 69 7a 65 2d 36 34 3b 0a 20 20 20 20 20  geSize-64;.     
499f2 20 69 66 28 20 28 34 2b 70 52 74 72 65 65 2d 3e   if( (4+pRtree->
499f3 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 52 54  nBytesPerCell*RT
499f4 52 45 45 5f 4d 41 58 43 45 4c 4c 53 29 3c 70 52  REE_MAXCELLS)<pR
499f5 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 20  tree->iNodeSize 
499f6 29 7b 0a 20 20 20 20 20 20 20 20 70 52 74 72 65  ){.        pRtre
499f7 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 20 3d 20 34  e->iNodeSize = 4
499f8 2b 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50  +pRtree->nBytesP
499f9 65 72 43 65 6c 6c 2a 52 54 52 45 45 5f 4d 41 58  erCell*RTREE_MAX
499fa 43 45 4c 4c 53 3b 0a 20 20 20 20 20 20 7d 0a 20  CELLS;.      }. 
499fb 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
499fc 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33    zSql = sqlite3
499fd 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20  _mprintf(.      
499fe 20 20 22 53 45 4c 45 43 54 20 6c 65 6e 67 74 68    "SELECT length
499ff 28 64 61 74 61 29 20 46 52 4f 4d 20 27 25 71 27  (data) FROM '%q'
49a00 2e 27 25 71 5f 6e 6f 64 65 27 20 57 48 45 52 45  .'%q_node' WHERE
49a01 20 6e 6f 64 65 6e 6f 20 3d 20 31 22 2c 0a 20 20   nodeno = 1",.  
49a02 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e 7a 44        pRtree->zD
49a03 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65  b, pRtree->zName
49a04 0a 20 20 20 20 29 3b 0a 20 20 20 20 72 63 20 3d  .    );.    rc =
49a05 20 67 65 74 49 6e 74 46 72 6f 6d 53 74 6d 74 28   getIntFromStmt(
49a06 64 62 2c 20 7a 53 71 6c 2c 20 26 70 52 74 72 65  db, zSql, &pRtre
49a07 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0a 20  e->iNodeSize);. 
49a08 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72   }..  sqlite3_fr
49a09 65 65 28 7a 53 71 6c 29 3b 0a 20 20 72 65 74 75  ee(zSql);.  retu
49a0a 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  rn rc;.}../* .**
49a0b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
49a0c 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
49a0d 74 69 6f 6e 20 6f 66 20 62 6f 74 68 20 74 68 65  tion of both the
49a0e 20 78 43 6f 6e 6e 65 63 74 20 61 6e 64 20 78 43   xConnect and xC
49a0f 72 65 61 74 65 0a 2a 2a 20 6d 65 74 68 6f 64 73  reate.** methods
49a10 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20 76   of the r-tree v
49a11 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a  irtual table..**
49a12 0a 2a 2a 20 20 20 61 72 67 76 5b 30 5d 20 20 20  .**   argv[0]   
49a13 2d 3e 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 0a 2a  -> module name.*
49a14 2a 20 20 20 61 72 67 76 5b 31 5d 20 20 20 2d 3e  *   argv[1]   ->
49a15 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a   database name.*
49a16 2a 20 20 20 61 72 67 76 5b 32 5d 20 20 20 2d 3e  *   argv[2]   ->
49a17 20 74 61 62 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20   table name.**  
49a18 20 61 72 67 76 5b 2e 2e 2e 5d 20 2d 3e 20 63 6f   argv[...] -> co
49a19 6c 75 6d 6e 20 6e 61 6d 65 73 2e 2e 2e 0a 2a 2f  lumn names....*/
49a1a 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
49a1b 65 49 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33  eInit(.  sqlite3
49a1c 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
49a1d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
49a1e 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
49a1f 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ion */.  void *p
49a20 41 75 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  Aux,            
49a21 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
49a22 4f 6e 65 20 6f 66 20 74 68 65 20 52 54 52 45 45  One of the RTREE
49a23 5f 43 4f 4f 52 44 5f 2a 20 63 6f 6e 73 74 61 6e  _COORD_* constan
49a24 74 73 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63  ts */.  int argc
49a25 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f  , const char *co
49a26 6e 73 74 2a 61 72 67 76 2c 20 20 20 2f 2a 20 50  nst*argv,   /* P
49a27 61 72 61 6d 65 74 65 72 73 20 74 6f 20 43 52 45  arameters to CRE
49a28 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
49a29 65 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ent */.  sqlite3
49a2a 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c 20  _vtab **ppVtab, 
49a2b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
49a2c 4f 55 54 3a 20 4e 65 77 20 76 69 72 74 75 61 6c  OUT: New virtual
49a2d 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72   table */.  char
49a2e 20 2a 2a 70 7a 45 72 72 2c 20 20 20 20 20 20 20   **pzErr,       
49a2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49a30 2f 2a 20 4f 55 54 3a 20 45 72 72 6f 72 20 6d 65  /* OUT: Error me
49a31 73 73 61 67 65 2c 20 69 66 20 61 6e 79 20 2a 2f  ssage, if any */
49a32 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74 65 20  .  int isCreate 
49a33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49a34 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66         /* True f
49a35 6f 72 20 78 43 72 65 61 74 65 2c 20 66 61 6c 73  or xCreate, fals
49a36 65 20 66 6f 72 20 78 43 6f 6e 6e 65 63 74 20 2a  e for xConnect *
49a37 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  /.){.  int rc = 
49a38 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 52 74 72  SQLITE_OK;.  Rtr
49a39 65 65 20 2a 70 52 74 72 65 65 3b 0a 20 20 69 6e  ee *pRtree;.  in
49a3a 74 20 6e 44 62 3b 20 20 20 20 20 20 20 20 20 20  t nDb;          
49a3b 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
49a3c 20 73 74 72 69 6e 67 20 61 72 67 76 5b 31 5d 20   string argv[1] 
49a3d 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20  */.  int nName; 
49a3e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
49a3f 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 61  ngth of string a
49a40 72 67 76 5b 32 5d 20 2a 2f 0a 20 20 69 6e 74 20  rgv[2] */.  int 
49a41 65 43 6f 6f 72 64 54 79 70 65 20 3d 20 28 70 41  eCoordType = (pA
49a42 75 78 20 3f 20 52 54 52 45 45 5f 43 4f 4f 52 44  ux ? RTREE_COORD
49a43 5f 49 4e 54 33 32 20 3a 20 52 54 52 45 45 5f 43  _INT32 : RTREE_C
49a44 4f 4f 52 44 5f 52 45 41 4c 33 32 29 3b 0a 0a 20  OORD_REAL32);.. 
49a45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 45 72   const char *aEr
49a46 72 4d 73 67 5b 5d 20 3d 20 7b 0a 20 20 20 20 30  rMsg[] = {.    0
49a47 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
49a48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49a49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49a4a 20 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20       /* 0 */.   
49a4b 20 22 57 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f   "Wrong number o
49a4c 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e  f columns for an
49a4d 20 72 74 72 65 65 20 74 61 62 6c 65 22 2c 20 20   rtree table",  
49a4e 20 20 20 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20         /* 1 */. 
49a4f 20 20 20 22 54 6f 6f 20 66 65 77 20 63 6f 6c 75     "Too few colu
49a50 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65 65  mns for an rtree
49a51 20 74 61 62 6c 65 22 2c 20 20 20 20 20 20 20 20   table",        
49a52 20 20 20 20 20 20 20 20 20 2f 2a 20 32 20 2a 2f           /* 2 */
49a53 0a 20 20 20 20 22 54 6f 6f 20 6d 61 6e 79 20 63  .    "Too many c
49a54 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74  olumns for an rt
49a55 72 65 65 20 74 61 62 6c 65 22 20 20 20 20 20 20  ree table"      
49a56 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 33 20             /* 3 
49a57 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69  */.  };..  int i
49a58 45 72 72 20 3d 20 28 61 72 67 63 3c 36 29 20 3f  Err = (argc<6) ?
49a59 20 32 20 3a 20 61 72 67 63 3e 28 52 54 52 45 45   2 : argc>(RTREE
49a5a 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a  _MAX_DIMENSIONS*
49a5b 32 2b 34 29 20 3f 20 33 20 3a 20 61 72 67 63 25  2+4) ? 3 : argc%
49a5c 32 3b 0a 20 20 69 66 28 20 61 45 72 72 4d 73 67  2;.  if( aErrMsg
49a5d 5b 69 45 72 72 5d 20 29 7b 0a 20 20 20 20 2a 70  [iErr] ){.    *p
49a5e 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zErr = sqlite3_m
49a5f 70 72 69 6e 74 66 28 22 25 73 22 2c 20 61 45 72  printf("%s", aEr
49a60 72 4d 73 67 5b 69 45 72 72 5d 29 3b 0a 20 20 20  rMsg[iErr]);.   
49a61 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
49a62 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  RROR;.  }..  sql
49a63 69 74 65 33 5f 76 74 61 62 5f 63 6f 6e 66 69 67  ite3_vtab_config
49a64 28 64 62 2c 20 53 51 4c 49 54 45 5f 56 54 41 42  (db, SQLITE_VTAB
49a65 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 53 55 50 50  _CONSTRAINT_SUPP
49a66 4f 52 54 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 41  ORT, 1);..  /* A
49a67 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 71 6c 69  llocate the sqli
49a68 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75  te3_vtab structu
49a69 72 65 20 2a 2f 0a 20 20 6e 44 62 20 3d 20 28 69  re */.  nDb = (i
49a6a 6e 74 29 73 74 72 6c 65 6e 28 61 72 67 76 5b 31  nt)strlen(argv[1
49a6b 5d 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 28 69  ]);.  nName = (i
49a6c 6e 74 29 73 74 72 6c 65 6e 28 61 72 67 76 5b 32  nt)strlen(argv[2
49a6d 5d 29 3b 0a 20 20 70 52 74 72 65 65 20 3d 20 28  ]);.  pRtree = (
49a6e 52 74 72 65 65 20 2a 29 73 71 6c 69 74 65 33 5f  Rtree *)sqlite3_
49a6f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74  malloc(sizeof(Rt
49a70 72 65 65 29 2b 6e 44 62 2b 6e 4e 61 6d 65 2b 32  ree)+nDb+nName+2
49a71 29 3b 0a 20 20 69 66 28 20 21 70 52 74 72 65 65  );.  if( !pRtree
49a72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
49a73 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
49a74 0a 20 20 6d 65 6d 73 65 74 28 70 52 74 72 65 65  .  memset(pRtree
49a75 2c 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  , 0, sizeof(Rtre
49a76 65 29 2b 6e 44 62 2b 6e 4e 61 6d 65 2b 32 29 3b  e)+nDb+nName+2);
49a77 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79  .  pRtree->nBusy
49a78 20 3d 20 31 3b 0a 20 20 70 52 74 72 65 65 2d 3e   = 1;.  pRtree->
49a79 62 61 73 65 2e 70 4d 6f 64 75 6c 65 20 3d 20 26  base.pModule = &
49a7a 72 74 72 65 65 4d 6f 64 75 6c 65 3b 0a 20 20 70  rtreeModule;.  p
49a7b 52 74 72 65 65 2d 3e 7a 44 62 20 3d 20 28 63 68  Rtree->zDb = (ch
49a7c 61 72 20 2a 29 26 70 52 74 72 65 65 5b 31 5d 3b  ar *)&pRtree[1];
49a7d 0a 20 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65  .  pRtree->zName
49a7e 20 3d 20 26 70 52 74 72 65 65 2d 3e 7a 44 62 5b   = &pRtree->zDb[
49a7f 6e 44 62 2b 31 5d 3b 0a 20 20 70 52 74 72 65 65  nDb+1];.  pRtree
49a80 2d 3e 6e 44 69 6d 20 3d 20 28 61 72 67 63 2d 34  ->nDim = (argc-4
49a81 29 2f 32 3b 0a 20 20 70 52 74 72 65 65 2d 3e 6e  )/2;.  pRtree->n
49a82 42 79 74 65 73 50 65 72 43 65 6c 6c 20 3d 20 38  BytesPerCell = 8
49a83 20 2b 20 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a   + pRtree->nDim*
49a84 34 2a 32 3b 0a 20 20 70 52 74 72 65 65 2d 3e 65  4*2;.  pRtree->e
49a85 43 6f 6f 72 64 54 79 70 65 20 3d 20 65 43 6f 6f  CoordType = eCoo
49a86 72 64 54 79 70 65 3b 0a 20 20 6d 65 6d 63 70 79  rdType;.  memcpy
49a87 28 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 61 72  (pRtree->zDb, ar
49a88 67 76 5b 31 5d 2c 20 6e 44 62 29 3b 0a 20 20 6d  gv[1], nDb);.  m
49a89 65 6d 63 70 79 28 70 52 74 72 65 65 2d 3e 7a 4e  emcpy(pRtree->zN
49a8a 61 6d 65 2c 20 61 72 67 76 5b 32 5d 2c 20 6e 4e  ame, argv[2], nN
49a8b 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75  ame);..  /* Figu
49a8c 72 65 20 6f 75 74 20 74 68 65 20 6e 6f 64 65 20  re out the node 
49a8d 73 69 7a 65 20 74 6f 20 75 73 65 2e 20 2a 2f 0a  size to use. */.
49a8e 20 20 72 63 20 3d 20 67 65 74 4e 6f 64 65 53 69    rc = getNodeSi
49a8f 7a 65 28 64 62 2c 20 70 52 74 72 65 65 2c 20 69  ze(db, pRtree, i
49a90 73 43 72 65 61 74 65 29 3b 0a 0a 20 20 2f 2a 20  sCreate);..  /* 
49a91 43 72 65 61 74 65 2f 43 6f 6e 6e 65 63 74 20 74  Create/Connect t
49a92 6f 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  o the underlying
49a93 20 72 65 6c 61 74 69 6f 6e 61 6c 20 64 61 74 61   relational data
49a94 62 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 0a  base schema. If.
49a95 20 20 2a 2a 20 74 68 61 74 20 69 73 20 73 75 63    ** that is suc
49a96 63 65 73 73 66 75 6c 2c 20 63 61 6c 6c 20 73 71  cessful, call sq
49a97 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
49a98 61 62 28 29 20 74 6f 20 63 6f 6e 66 69 67 75 72  ab() to configur
49a99 65 0a 20 20 2a 2a 20 74 68 65 20 72 2d 74 72 65  e.  ** the r-tre
49a9a 65 20 74 61 62 6c 65 20 73 63 68 65 6d 61 2e 0a  e table schema..
49a9b 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53    */.  if( rc==S
49a9c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
49a9d 69 66 28 20 28 72 63 20 3d 20 72 74 72 65 65 53  if( (rc = rtreeS
49a9e 71 6c 49 6e 69 74 28 70 52 74 72 65 65 2c 20 64  qlInit(pRtree, d
49a9f 62 2c 20 61 72 67 76 5b 31 5d 2c 20 61 72 67 76  b, argv[1], argv
49aa0 5b 32 5d 2c 20 69 73 43 72 65 61 74 65 29 29 20  [2], isCreate)) 
49aa1 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20  ){.      *pzErr 
49aa2 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
49aa3 66 28 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f  f("%s", sqlite3_
49aa4 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 20  errmsg(db));.   
49aa5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68   }else{.      ch
49aa6 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74  ar *zSql = sqlit
49aa7 65 33 5f 6d 70 72 69 6e 74 66 28 22 43 52 45 41  e3_mprintf("CREA
49aa8 54 45 20 54 41 42 4c 45 20 78 28 25 73 22 2c 20  TE TABLE x(%s", 
49aa9 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 20 20  argv[3]);.      
49aaa 63 68 61 72 20 2a 7a 54 6d 70 3b 0a 20 20 20 20  char *zTmp;.    
49aab 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20    int ii;.      
49aac 66 6f 72 28 69 69 3d 34 3b 20 7a 53 71 6c 20 26  for(ii=4; zSql &
49aad 26 20 69 69 3c 61 72 67 63 3b 20 69 69 2b 2b 29  & ii<argc; ii++)
49aae 7b 0a 20 20 20 20 20 20 20 20 7a 54 6d 70 20 3d  {.        zTmp =
49aaf 20 7a 53 71 6c 3b 0a 20 20 20 20 20 20 20 20 7a   zSql;.        z
49ab0 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Sql = sqlite3_mp
49ab1 72 69 6e 74 66 28 22 25 73 2c 20 25 73 22 2c 20  rintf("%s, %s", 
49ab2 7a 54 6d 70 2c 20 61 72 67 76 5b 69 69 5d 29 3b  zTmp, argv[ii]);
49ab3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
49ab4 5f 66 72 65 65 28 7a 54 6d 70 29 3b 0a 20 20 20  _free(zTmp);.   
49ab5 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 7a     }.      if( z
49ab6 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  Sql ){.        z
49ab7 54 6d 70 20 3d 20 7a 53 71 6c 3b 0a 20 20 20 20  Tmp = zSql;.    
49ab8 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74      zSql = sqlit
49ab9 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 29 3b  e3_mprintf("%s);
49aba 22 2c 20 7a 54 6d 70 29 3b 0a 20 20 20 20 20 20  ", zTmp);.      
49abb 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
49abc 54 6d 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Tmp);.      }.  
49abd 20 20 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b      if( !zSql ){
49abe 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
49abf 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
49ac0 20 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49    }else if( SQLI
49ac1 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
49ac2 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
49ac3 62 28 64 62 2c 20 7a 53 71 6c 29 29 20 29 7b 0a  b(db, zSql)) ){.
49ac4 20 20 20 20 20 20 20 20 2a 70 7a 45 72 72 20 3d          *pzErr =
49ac5 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
49ac6 28 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 65  ("%s", sqlite3_e
49ac7 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 20 20  rrmsg(db));.    
49ac8 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
49ac9 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20  3_free(zSql);.  
49aca 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72    }.  }..  if( r
49acb 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
49acc 20 20 20 20 2a 70 70 56 74 61 62 20 3d 20 28 73      *ppVtab = (s
49acd 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 70 52  qlite3_vtab *)pR
49ace 74 72 65 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  tree;.  }else{. 
49acf 20 20 20 72 74 72 65 65 52 65 6c 65 61 73 65 28     rtreeRelease(
49ad0 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72  pRtree);.  }.  r
49ad1 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
49ad2 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
49ad3 6f 6e 20 6f 66 20 61 20 73 63 61 6c 61 72 20 66  on of a scalar f
49ad4 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 65 63  unction that dec
49ad5 6f 64 65 73 20 72 2d 74 72 65 65 20 6e 6f 64 65  odes r-tree node
49ad6 73 20 74 6f 0a 2a 2a 20 68 75 6d 61 6e 20 72 65  s to.** human re
49ad7 61 64 61 62 6c 65 20 73 74 72 69 6e 67 73 2e 20  adable strings. 
49ad8 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  This can be used
49ad9 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 61   for debugging a
49ada 6e 64 20 61 6e 61 6c 79 73 69 73 2e 0a 2a 2a 0a  nd analysis..**.
49adb 2a 2a 20 54 68 65 20 73 63 61 6c 61 72 20 66 75  ** The scalar fu
49adc 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 74 77 6f  nction takes two
49add 20 61 72 67 75 6d 65 6e 74 73 2c 20 61 20 62 6c   arguments, a bl
49ade 6f 62 20 6f 66 20 64 61 74 61 20 63 6f 6e 74 61  ob of data conta
49adf 69 6e 69 6e 67 0a 2a 2a 20 61 6e 20 72 2d 74 72  ining.** an r-tr
49ae0 65 65 20 6e 6f 64 65 2c 20 61 6e 64 20 74 68 65  ee node, and the
49ae1 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 6d 65 6e   number of dimen
49ae2 73 69 6f 6e 73 20 74 68 65 20 72 2d 74 72 65 65  sions the r-tree
49ae3 20 69 6e 64 65 78 65 73 2e 0a 2a 2a 20 46 6f 72   indexes..** For
49ae4 20 61 20 74 77 6f 2d 64 69 6d 65 6e 73 69 6f 6e   a two-dimension
49ae5 61 6c 20 72 2d 74 72 65 65 20 73 74 72 75 63 74  al r-tree struct
49ae6 75 72 65 20 63 61 6c 6c 65 64 20 22 72 74 22 2c  ure called "rt",
49ae7 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 0a   to deserialize.
49ae8 2a 2a 20 61 6c 6c 20 6e 6f 64 65 73 2c 20 61 20  ** all nodes, a 
49ae9 73 74 61 74 65 6d 65 6e 74 20 6c 69 6b 65 3a 0a  statement like:.
49aea 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 72  **.**   SELECT r
49aeb 74 72 65 65 6e 6f 64 65 28 32 2c 20 64 61 74 61  treenode(2, data
49aec 29 20 46 52 4f 4d 20 72 74 5f 6e 6f 64 65 3b 0a  ) FROM rt_node;.
49aed 2a 2a 0a 2a 2a 20 54 68 65 20 68 75 6d 61 6e 20  **.** The human 
49aee 72 65 61 64 61 62 6c 65 20 73 74 72 69 6e 67 20  readable string 
49aef 74 61 6b 65 73 20 74 68 65 20 66 6f 72 6d 20 6f  takes the form o
49af0 66 20 61 20 54 63 6c 20 6c 69 73 74 20 77 69 74  f a Tcl list wit
49af1 68 20 6f 6e 65 0a 2a 2a 20 65 6e 74 72 79 20 66  h one.** entry f
49af2 6f 72 20 65 61 63 68 20 63 65 6c 6c 20 69 6e 20  or each cell in 
49af3 74 68 65 20 72 2d 74 72 65 65 20 6e 6f 64 65 2e  the r-tree node.
49af4 20 45 61 63 68 20 65 6e 74 72 79 20 69 73 20 69   Each entry is i
49af5 74 73 65 6c 66 20 61 0a 2a 2a 20 6c 69 73 74 2c  tself a.** list,
49af6 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
49af7 38 2d 62 79 74 65 20 72 6f 77 69 64 2f 70 61 67  8-byte rowid/pag
49af8 65 6e 6f 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  eno followed by 
49af9 74 68 65 20 0a 2a 2a 20 3c 6e 75 6d 2d 64 69 6d  the .** <num-dim
49afa 65 6e 73 69 6f 6e 3e 2a 32 20 63 6f 6f 72 64 69  ension>*2 coordi
49afb 6e 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  nates..*/.static
49afc 20 76 6f 69 64 20 72 74 72 65 65 6e 6f 64 65 28   void rtreenode(
49afd 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
49afe 2a 63 74 78 2c 20 69 6e 74 20 6e 41 72 67 2c 20  *ctx, int nArg, 
49aff 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
49b00 61 70 41 72 67 29 7b 0a 20 20 63 68 61 72 20 2a  apArg){.  char *
49b01 7a 54 65 78 74 20 3d 20 30 3b 0a 20 20 52 74 72  zText = 0;.  Rtr
49b02 65 65 4e 6f 64 65 20 6e 6f 64 65 3b 0a 20 20 52  eeNode node;.  R
49b03 74 72 65 65 20 74 72 65 65 3b 0a 20 20 69 6e 74  tree tree;.  int
49b04 20 69 69 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50   ii;..  UNUSED_P
49b05 41 52 41 4d 45 54 45 52 28 6e 41 72 67 29 3b 0a  ARAMETER(nArg);.
49b06 20 20 6d 65 6d 73 65 74 28 26 6e 6f 64 65 2c 20    memset(&node, 
49b07 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 4e  0, sizeof(RtreeN
49b08 6f 64 65 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28  ode));.  memset(
49b09 26 74 72 65 65 2c 20 30 2c 20 73 69 7a 65 6f 66  &tree, 0, sizeof
49b0a 28 52 74 72 65 65 29 29 3b 0a 20 20 74 72 65 65  (Rtree));.  tree
49b0b 2e 6e 44 69 6d 20 3d 20 73 71 6c 69 74 65 33 5f  .nDim = sqlite3_
49b0c 76 61 6c 75 65 5f 69 6e 74 28 61 70 41 72 67 5b  value_int(apArg[
49b0d 30 5d 29 3b 0a 20 20 74 72 65 65 2e 6e 42 79 74  0]);.  tree.nByt
49b0e 65 73 50 65 72 43 65 6c 6c 20 3d 20 38 20 2b 20  esPerCell = 8 + 
49b0f 38 20 2a 20 74 72 65 65 2e 6e 44 69 6d 3b 0a 20  8 * tree.nDim;. 
49b10 20 6e 6f 64 65 2e 7a 44 61 74 61 20 3d 20 28 75   node.zData = (u
49b11 38 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  8 *)sqlite3_valu
49b12 65 5f 62 6c 6f 62 28 61 70 41 72 67 5b 31 5d 29  e_blob(apArg[1])
49b13 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  ;..  for(ii=0; i
49b14 69 3c 4e 43 45 4c 4c 28 26 6e 6f 64 65 29 3b 20  i<NCELL(&node); 
49b15 69 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20  ii++){.    char 
49b16 7a 43 65 6c 6c 5b 35 31 32 5d 3b 0a 20 20 20 20  zCell[512];.    
49b17 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20  int nCell = 0;. 
49b18 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c     RtreeCell cel
49b19 6c 3b 0a 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 0a  l;.    int jj;..
49b1a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
49b1b 26 74 72 65 65 2c 20 26 6e 6f 64 65 2c 20 69 69  &tree, &node, ii
49b1c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 73 71  , &cell);.    sq
49b1d 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 35  lite3_snprintf(5
49b1e 31 32 2d 6e 43 65 6c 6c 2c 26 7a 43 65 6c 6c 5b  12-nCell,&zCell[
49b1f 6e 43 65 6c 6c 5d 2c 22 25 6c 6c 64 22 2c 20 63  nCell],"%lld", c
49b20 65 6c 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20 20  ell.iRowid);.   
49b21 20 6e 43 65 6c 6c 20 3d 20 28 69 6e 74 29 73 74   nCell = (int)st
49b22 72 6c 65 6e 28 7a 43 65 6c 6c 29 3b 0a 20 20 20  rlen(zCell);.   
49b23 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 74 72   for(jj=0; jj<tr
49b24 65 65 2e 6e 44 69 6d 2a 32 3b 20 6a 6a 2b 2b 29  ee.nDim*2; jj++)
49b25 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
49b26 5f 52 54 52 45 45 5f 49 4e 54 5f 4f 4e 4c 59 0a  _RTREE_INT_ONLY.
49b27 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
49b28 70 72 69 6e 74 66 28 35 31 32 2d 6e 43 65 6c 6c  printf(512-nCell
49b29 2c 26 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20  ,&zCell[nCell], 
49b2a 22 20 25 66 22 2c 0a 20 20 20 20 20 20 20 20 20  " %f",.         
49b2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 64                (d
49b2c 6f 75 62 6c 65 29 63 65 6c 6c 2e 61 43 6f 6f 72  ouble)cell.aCoor
49b2d 64 5b 6a 6a 5d 2e 66 29 3b 0a 23 65 6c 73 65 0a  d[jj].f);.#else.
49b2e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
49b2f 70 72 69 6e 74 66 28 35 31 32 2d 6e 43 65 6c 6c  printf(512-nCell
49b30 2c 26 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20  ,&zCell[nCell], 
49b31 22 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20  " %d",.         
49b32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 65                ce
49b33 6c 6c 2e 61 43 6f 6f 72 64 5b 6a 6a 5d 2e 69 29  ll.aCoord[jj].i)
49b34 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 6e  ;.#endif.      n
49b35 43 65 6c 6c 20 3d 20 28 69 6e 74 29 73 74 72 6c  Cell = (int)strl
49b36 65 6e 28 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 7d  en(zCell);.    }
49b37 0a 0a 20 20 20 20 69 66 28 20 7a 54 65 78 74 20  ..    if( zText 
49b38 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
49b39 54 65 78 74 4e 65 77 20 3d 20 73 71 6c 69 74 65  TextNew = sqlite
49b3a 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 20 7b 25  3_mprintf("%s {%
49b3b 73 7d 22 2c 20 7a 54 65 78 74 2c 20 7a 43 65 6c  s}", zText, zCel
49b3c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  l);.      sqlite
49b3d 33 5f 66 72 65 65 28 7a 54 65 78 74 29 3b 0a 20  3_free(zText);. 
49b3e 20 20 20 20 20 7a 54 65 78 74 20 3d 20 7a 54 65       zText = zTe
49b3f 78 74 4e 65 77 3b 0a 20 20 20 20 7d 65 6c 73 65  xtNew;.    }else
49b40 7b 0a 20 20 20 20 20 20 7a 54 65 78 74 20 3d 20  {.      zText = 
49b41 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
49b42 22 7b 25 73 7d 22 2c 20 7a 43 65 6c 6c 29 3b 0a  "{%s}", zCell);.
49b43 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 73      }.  }.  .  s
49b44 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
49b45 78 74 28 63 74 78 2c 20 7a 54 65 78 74 2c 20 2d  xt(ctx, zText, -
49b46 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29  1, sqlite3_free)
49b47 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
49b48 20 72 74 72 65 65 64 65 70 74 68 28 73 71 6c 69   rtreedepth(sqli
49b49 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78  te3_context *ctx
49b4a 2c 20 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69  , int nArg, sqli
49b4b 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72  te3_value **apAr
49b4c 67 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  g){.  UNUSED_PAR
49b4d 41 4d 45 54 45 52 28 6e 41 72 67 29 3b 0a 20 20  AMETER(nArg);.  
49b4e 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
49b4f 65 5f 74 79 70 65 28 61 70 41 72 67 5b 30 5d 29  e_type(apArg[0])
49b50 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 0a 20  !=SQLITE_BLOB . 
49b51 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 76 61 6c    || sqlite3_val
49b52 75 65 5f 62 79 74 65 73 28 61 70 41 72 67 5b 30  ue_bytes(apArg[0
49b53 5d 29 3c 32 0a 20 20 29 7b 0a 20 20 20 20 73 71  ])<2.  ){.    sq
49b54 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
49b55 6f 72 28 63 74 78 2c 20 22 49 6e 76 61 6c 69 64  or(ctx, "Invalid
49b56 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 74 72   argument to rtr
49b57 65 65 64 65 70 74 68 28 29 22 2c 20 2d 31 29 3b  eedepth()", -1);
49b58 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75   .  }else{.    u
49b59 38 20 2a 7a 42 6c 6f 62 20 3d 20 28 75 38 20 2a  8 *zBlob = (u8 *
49b5a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  )sqlite3_value_b
49b5b 6c 6f 62 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20  lob(apArg[0]);. 
49b5c 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
49b5d 74 5f 69 6e 74 28 63 74 78 2c 20 72 65 61 64 49  t_int(ctx, readI
49b5e 6e 74 31 36 28 7a 42 6c 6f 62 29 29 3b 0a 20 20  nt16(zBlob));.  
49b5f 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  }.}../*.** Regis
49b60 74 65 72 20 74 68 65 20 72 2d 74 72 65 65 20 6d  ter the r-tree m
49b61 6f 64 75 6c 65 20 77 69 74 68 20 64 61 74 61 62  odule with datab
49b62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 20 54  ase handle db. T
49b63 68 69 73 20 63 72 65 61 74 65 73 20 74 68 65 0a  his creates the.
49b64 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
49b65 20 6d 6f 64 75 6c 65 20 22 72 74 72 65 65 22 20   module "rtree" 
49b66 61 6e 64 20 74 68 65 20 64 65 62 75 67 67 69 6e  and the debuggin
49b67 67 2f 61 6e 61 6c 79 73 69 73 20 73 63 61 6c 61  g/analysis scala
49b68 72 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 22  r .** function "
49b69 72 74 72 65 65 6e 6f 64 65 22 2e 0a 2a 2f 0a 53  rtreenode"..*/.S
49b6a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
49b6b 74 20 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e  t sqlite3RtreeIn
49b6c 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  it(sqlite3 *db){
49b6d 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 75 74 66  .  const int utf
49b6e 38 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  8 = SQLITE_UTF8;
49b6f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63  .  int rc;..  rc
49b70 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74   = sqlite3_creat
49b71 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22  e_function(db, "
49b72 72 74 72 65 65 6e 6f 64 65 22 2c 20 32 2c 20 75  rtreenode", 2, u
49b73 74 66 38 2c 20 30 2c 20 72 74 72 65 65 6e 6f 64  tf8, 0, rtreenod
49b74 65 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20  e, 0, 0);.  if( 
49b75 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
49b76 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
49b77 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
49b78 6e 28 64 62 2c 20 22 72 74 72 65 65 64 65 70 74  n(db, "rtreedept
49b79 68 22 2c 20 31 2c 20 75 74 66 38 2c 20 30 2c 72  h", 1, utf8, 0,r
49b7a 74 72 65 65 64 65 70 74 68 2c 20 30 2c 20 30 29  treedepth, 0, 0)
49b7b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  ;.  }.  if( rc==
49b7c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66  SQLITE_OK ){.#if
49b7d 64 65 66 20 53 51 4c 49 54 45 5f 52 54 52 45 45  def SQLITE_RTREE
49b7e 5f 49 4e 54 5f 4f 4e 4c 59 0a 20 20 20 20 76 6f  _INT_ONLY.    vo
49b7f 69 64 20 2a 63 20 3d 20 28 76 6f 69 64 20 2a 29  id *c = (void *)
49b80 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33  RTREE_COORD_INT3
49b81 32 3b 0a 23 65 6c 73 65 0a 20 20 20 20 76 6f 69  2;.#else.    voi
49b82 64 20 2a 63 20 3d 20 28 76 6f 69 64 20 2a 29 52  d *c = (void *)R
49b83 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33  TREE_COORD_REAL3
49b84 32 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 63  2;.#endif.    rc
49b85 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74   = sqlite3_creat
49b86 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 64 62 2c 20  e_module_v2(db, 
49b87 22 72 74 72 65 65 22 2c 20 26 72 74 72 65 65 4d  "rtree", &rtreeM
49b88 6f 64 75 6c 65 2c 20 63 2c 20 30 29 3b 0a 20 20  odule, c, 0);.  
49b89 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
49b8a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 76 6f 69  TE_OK ){.    voi
49b8b 64 20 2a 63 20 3d 20 28 76 6f 69 64 20 2a 29 52  d *c = (void *)R
49b8c 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32  TREE_COORD_INT32
49b8d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
49b8e 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
49b8f 5f 76 32 28 64 62 2c 20 22 72 74 72 65 65 5f 69  _v2(db, "rtree_i
49b90 33 32 22 2c 20 26 72 74 72 65 65 4d 6f 64 75 6c  32", &rtreeModul
49b91 65 2c 20 63 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20  e, c, 0);.  }.. 
49b92 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
49b93 2a 0a 2a 2a 20 41 20 76 65 72 73 69 6f 6e 20 6f  *.** A version o
49b94 66 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  f sqlite3_free()
49b95 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
49b96 64 20 61 73 20 61 20 63 61 6c 6c 62 61 63 6b 2e  d as a callback.
49b97 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a 2a   This is used.**
49b98 20 69 6e 20 74 77 6f 20 70 6c 61 63 65 73 20 2d   in two places -
49b99 20 61 73 20 74 68 65 20 64 65 73 74 72 75 63 74   as the destruct
49b9a 6f 72 20 66 6f 72 20 74 68 65 20 62 6c 6f 62 20  or for the blob 
49b9b 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
49b9c 79 20 74 68 65 0a 2a 2a 20 69 6e 76 6f 63 61 74  y the.** invocat
49b9d 69 6f 6e 20 6f 66 20 61 20 67 65 6f 6d 65 74 72  ion of a geometr
49b9e 79 20 66 75 6e 63 74 69 6f 6e 2c 20 61 6e 64 20  y function, and 
49b9f 61 73 20 74 68 65 20 64 65 73 74 72 75 63 74 6f  as the destructo
49ba0 72 20 66 6f 72 20 74 68 65 20 67 65 6f 6d 65 74  r for the geomet
49ba1 72 79 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20  ry.** functions 
49ba2 74 68 65 6d 73 65 6c 76 65 73 2e 0a 2a 2f 0a 73  themselves..*/.s
49ba3 74 61 74 69 63 20 76 6f 69 64 20 64 6f 53 71 6c  tatic void doSql
49ba4 69 74 65 33 46 72 65 65 28 76 6f 69 64 20 2a 70  ite3Free(void *p
49ba5 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ){.  sqlite3_fre
49ba6 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  e(p);.}../*.** E
49ba7 61 63 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ach call to sqli
49ba8 74 65 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74  te3_rtree_geomet
49ba9 72 79 5f 63 61 6c 6c 62 61 63 6b 28 29 20 63 72  ry_callback() cr
49baa 65 61 74 65 73 20 61 6e 20 6f 72 64 69 6e 61 72  eates an ordinar
49bab 79 20 53 51 4c 69 74 65 0a 2a 2a 20 73 63 61 6c  y SQLite.** scal
49bac 61 72 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e  ar user function
49bad 2e 20 54 68 69 73 20 43 20 66 75 6e 63 74 69 6f  . This C functio
49bae 6e 20 69 73 20 74 68 65 20 63 61 6c 6c 62 61 63  n is the callbac
49baf 6b 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 73  k used for all s
49bb0 75 63 68 0a 2a 2a 20 72 65 67 69 73 74 65 72 65  uch.** registere
49bb1 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e  d SQL functions.
49bb2 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 61 6c 61  .**.** The scala
49bb3 72 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73  r user functions
49bb4 20 72 65 74 75 72 6e 20 61 20 62 6c 6f 62 20 74   return a blob t
49bb5 68 61 74 20 69 73 20 69 6e 74 65 72 70 72 65 74  hat is interpret
49bb6 65 64 20 62 79 20 72 2d 74 72 65 65 0a 2a 2a 20  ed by r-tree.** 
49bb7 74 61 62 6c 65 20 4d 41 54 43 48 20 6f 70 65 72  table MATCH oper
49bb8 61 74 6f 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ators..*/.static
49bb9 20 76 6f 69 64 20 67 65 6f 6d 43 61 6c 6c 62 61   void geomCallba
49bba 63 6b 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ck(sqlite3_conte
49bbb 78 74 20 2a 63 74 78 2c 20 69 6e 74 20 6e 41 72  xt *ctx, int nAr
49bbc 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  g, sqlite3_value
49bbd 20 2a 2a 61 41 72 67 29 7b 0a 20 20 52 74 72 65   **aArg){.  Rtre
49bbe 65 47 65 6f 6d 43 61 6c 6c 62 61 63 6b 20 2a 70  eGeomCallback *p
49bbf 47 65 6f 6d 43 74 78 20 3d 20 28 52 74 72 65 65  GeomCtx = (Rtree
49bc0 47 65 6f 6d 43 61 6c 6c 62 61 63 6b 20 2a 29 73  GeomCallback *)s
49bc1 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
49bc2 28 63 74 78 29 3b 0a 20 20 52 74 72 65 65 4d 61  (ctx);.  RtreeMa
49bc3 74 63 68 41 72 67 20 2a 70 42 6c 6f 62 3b 0a 20  tchArg *pBlob;. 
49bc4 20 69 6e 74 20 6e 42 6c 6f 62 3b 0a 0a 20 20 6e   int nBlob;..  n
49bc5 42 6c 6f 62 20 3d 20 73 69 7a 65 6f 66 28 52 74  Blob = sizeof(Rt
49bc6 72 65 65 4d 61 74 63 68 41 72 67 29 20 2b 20 28  reeMatchArg) + (
49bc7 6e 41 72 67 2d 31 29 2a 73 69 7a 65 6f 66 28 52  nArg-1)*sizeof(R
49bc8 74 72 65 65 44 56 61 6c 75 65 29 3b 0a 20 20 70  treeDValue);.  p
49bc9 42 6c 6f 62 20 3d 20 28 52 74 72 65 65 4d 61 74  Blob = (RtreeMat
49bca 63 68 41 72 67 20 2a 29 73 71 6c 69 74 65 33 5f  chArg *)sqlite3_
49bcb 6d 61 6c 6c 6f 63 28 6e 42 6c 6f 62 29 3b 0a 20  malloc(nBlob);. 
49bcc 20 69 66 28 20 21 70 42 6c 6f 62 20 29 7b 0a 20   if( !pBlob ){. 
49bcd 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
49bce 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 74  t_error_nomem(ct
49bcf 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  x);.  }else{.   
49bd0 20 69 6e 74 20 69 3b 0a 20 20 20 20 70 42 6c 6f   int i;.    pBlo
49bd1 62 2d 3e 6d 61 67 69 63 20 3d 20 52 54 52 45 45  b->magic = RTREE
49bd2 5f 47 45 4f 4d 45 54 52 59 5f 4d 41 47 49 43 3b  _GEOMETRY_MAGIC;
49bd3 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 78 47 65 6f  .    pBlob->xGeo
49bd4 6d 20 3d 20 70 47 65 6f 6d 43 74 78 2d 3e 78 47  m = pGeomCtx->xG
49bd5 65 6f 6d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e  eom;.    pBlob->
49bd6 70 43 6f 6e 74 65 78 74 20 3d 20 70 47 65 6f 6d  pContext = pGeom
49bd7 43 74 78 2d 3e 70 43 6f 6e 74 65 78 74 3b 0a 20  Ctx->pContext;. 
49bd8 20 20 20 70 42 6c 6f 62 2d 3e 6e 50 61 72 61 6d     pBlob->nParam
49bd9 20 3d 20 6e 41 72 67 3b 0a 20 20 20 20 66 6f 72   = nArg;.    for
49bda 28 69 3d 30 3b 20 69 3c 6e 41 72 67 3b 20 69 2b  (i=0; i<nArg; i+
49bdb 2b 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  +){.#ifdef SQLIT
49bdc 45 5f 52 54 52 45 45 5f 49 4e 54 5f 4f 4e 4c 59  E_RTREE_INT_ONLY
49bdd 0a 20 20 20 20 20 20 70 42 6c 6f 62 2d 3e 61 50  .      pBlob->aP
49bde 61 72 61 6d 5b 69 5d 20 3d 20 73 71 6c 69 74 65  aram[i] = sqlite
49bdf 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 41  3_value_int64(aA
49be0 72 67 5b 69 5d 29 3b 0a 23 65 6c 73 65 0a 20 20  rg[i]);.#else.  
49be1 20 20 20 20 70 42 6c 6f 62 2d 3e 61 50 61 72 61      pBlob->aPara
49be2 6d 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 5f 76  m[i] = sqlite3_v
49be3 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 41 72 67  alue_double(aArg
49be4 5b 69 5d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  [i]);.#endif.   
49be5 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72   }.    sqlite3_r
49be6 65 73 75 6c 74 5f 62 6c 6f 62 28 63 74 78 2c 20  esult_blob(ctx, 
49be7 70 42 6c 6f 62 2c 20 6e 42 6c 6f 62 2c 20 64 6f  pBlob, nBlob, do
49be8 53 71 6c 69 74 65 33 46 72 65 65 29 3b 0a 20 20  Sqlite3Free);.  
49be9 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  }.}../*.** Regis
49bea 74 65 72 20 61 20 6e 65 77 20 67 65 6f 6d 65 74  ter a new geomet
49beb 72 79 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  ry function for 
49bec 75 73 65 20 77 69 74 68 20 74 68 65 20 72 2d 74  use with the r-t
49bed 72 65 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74  ree MATCH operat
49bee 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  or..*/.SQLITE_AP
49bef 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 74  I int sqlite3_rt
49bf0 72 65 65 5f 67 65 6f 6d 65 74 72 79 5f 63 61 6c  ree_geometry_cal
49bf1 6c 62 61 63 6b 28 0a 20 20 73 71 6c 69 74 65 33  lback(.  sqlite3
49bf2 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68   *db,.  const ch
49bf3 61 72 20 2a 7a 47 65 6f 6d 2c 0a 20 20 69 6e 74  ar *zGeom,.  int
49bf4 20 28 2a 78 47 65 6f 6d 29 28 73 71 6c 69 74 65   (*xGeom)(sqlite
49bf5 33 5f 72 74 72 65 65 5f 67 65 6f 6d 65 74 72 79  3_rtree_geometry
49bf6 20 2a 2c 20 69 6e 74 2c 20 52 74 72 65 65 44 56   *, int, RtreeDV
49bf7 61 6c 75 65 20 2a 2c 20 69 6e 74 20 2a 29 2c 0a  alue *, int *),.
49bf8 20 20 76 6f 69 64 20 2a 70 43 6f 6e 74 65 78 74    void *pContext
49bf9 0a 29 7b 0a 20 20 52 74 72 65 65 47 65 6f 6d 43  .){.  RtreeGeomC
49bfa 61 6c 6c 62 61 63 6b 20 2a 70 47 65 6f 6d 43 74  allback *pGeomCt
49bfb 78 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  x;      /* Conte
49bfc 78 74 20 6f 62 6a 65 63 74 20 66 6f 72 20 6e 65  xt object for ne
49bfd 77 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20  w user-function 
49bfe 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  */..  /* Allocat
49bff 65 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 74  e and populate t
49c00 68 65 20 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63  he context objec
49c01 74 2e 20 2a 2f 0a 20 20 70 47 65 6f 6d 43 74 78  t. */.  pGeomCtx
49c02 20 3d 20 28 52 74 72 65 65 47 65 6f 6d 43 61 6c   = (RtreeGeomCal
49c03 6c 62 61 63 6b 20 2a 29 73 71 6c 69 74 65 33 5f  lback *)sqlite3_
49c04 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74  malloc(sizeof(Rt
49c05 72 65 65 47 65 6f 6d 43 61 6c 6c 62 61 63 6b 29  reeGeomCallback)
49c06 29 3b 0a 20 20 69 66 28 20 21 70 47 65 6f 6d 43  );.  if( !pGeomC
49c07 74 78 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  tx ) return SQLI
49c08 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 47 65 6f  TE_NOMEM;.  pGeo
49c09 6d 43 74 78 2d 3e 78 47 65 6f 6d 20 3d 20 78 47  mCtx->xGeom = xG
49c0a 65 6f 6d 3b 0a 20 20 70 47 65 6f 6d 43 74 78 2d  eom;.  pGeomCtx-
49c0b 3e 70 43 6f 6e 74 65 78 74 20 3d 20 70 43 6f 6e  >pContext = pCon
49c0c 74 65 78 74 3b 0a 0a 20 20 2f 2a 20 43 72 65 61  text;..  /* Crea
49c0d 74 65 20 74 68 65 20 6e 65 77 20 75 73 65 72 2d  te the new user-
49c0e 66 75 6e 63 74 69 6f 6e 2e 20 52 65 67 69 73 74  function. Regist
49c0f 65 72 20 61 20 64 65 73 74 72 75 63 74 6f 72 20  er a destructor 
49c10 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65 6c 65  function to dele
49c11 74 65 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6e 74  te.  ** the cont
49c12 65 78 74 20 6f 62 6a 65 63 74 20 77 68 65 6e 20  ext object when 
49c13 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  it is no longer 
49c14 72 65 71 75 69 72 65 64 2e 20 20 2a 2f 0a 20 20  required.  */.  
49c15 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 63  return sqlite3_c
49c16 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 5f 76  reate_function_v
49c17 32 28 64 62 2c 20 7a 47 65 6f 6d 2c 20 2d 31 2c  2(db, zGeom, -1,
49c18 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 0a 20 20   SQLITE_ANY, .  
49c19 20 20 20 20 28 76 6f 69 64 20 2a 29 70 47 65 6f      (void *)pGeo
49c1a 6d 43 74 78 2c 20 67 65 6f 6d 43 61 6c 6c 62 61  mCtx, geomCallba
49c1b 63 6b 2c 20 30 2c 20 30 2c 20 64 6f 53 71 6c 69  ck, 0, 0, doSqli
49c1c 74 65 33 46 72 65 65 0a 20 20 29 3b 0a 7d 0a 0a  te3Free.  );.}..
49c1d 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f 52 45  #if !SQLITE_CORE
49c1e 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
49c1f 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f  sqlite3_extensio
49c20 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65  n_init(.  sqlite
49c21 33 20 2a 64 62 2c 0a 20 20 63 68 61 72 20 2a 2a  3 *db,.  char **
49c22 70 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f 6e 73  pzErrMsg,.  cons
49c23 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f  t sqlite3_api_ro
49c24 75 74 69 6e 65 73 20 2a 70 41 70 69 0a 29 7b 0a  utines *pApi.){.
49c25 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49    SQLITE_EXTENSI
49c26 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29 0a 20  ON_INIT2(pApi). 
49c27 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52   return sqlite3R
49c28 74 72 65 65 49 6e 69 74 28 64 62 29 3b 0a 7d 0a  treeInit(db);.}.
49c29 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a  #endif..#endif..
49c2a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
49c2b 45 6e 64 20 6f 66 20 72 74 72 65 65 2e 63 20 2a  End of rtree.c *
49c2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49c2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49c2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
49c2f 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
49c30 42 65 67 69 6e 20 66 69 6c 65 20 69 63 75 2e 63  Begin file icu.c
49c31 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
49c32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49c33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
49c34 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20 36  /*.** 2007 May 6
49c35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
49c36 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
49c37 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
49c38 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
49c39 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
49c3a 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
49c3b 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
49c3c 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
49c3d 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
49c3e 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
49c3f 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
49c40 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
49c41 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
49c42 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
49c43 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
49c44 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
49c45 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
49c46 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
49c47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49c48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49c49 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49c4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
49c4b 2a 2a 20 24 49 64 3a 20 69 63 75 2e 63 2c 76 20  ** $Id: icu.c,v 
49c4c 31 2e 37 20 32 30 30 37 2f 31 32 2f 31 33 20 32  1.7 2007/12/13 2
49c4d 31 3a 35 34 3a 31 31 20 64 72 68 20 45 78 70 20  1:54:11 drh Exp 
49c4e 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  $.**.** This fil
49c4f 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20  e implements an 
49c50 69 6e 74 65 67 72 61 74 69 6f 6e 20 62 65 74 77  integration betw
49c51 65 65 6e 20 74 68 65 20 49 43 55 20 6c 69 62 72  een the ICU libr
49c52 61 72 79 20 0a 2a 2a 20 28 22 49 6e 74 65 72 6e  ary .** ("Intern
49c53 61 74 69 6f 6e 61 6c 20 43 6f 6d 70 6f 6e 65 6e  ational Componen
49c54 74 73 20 66 6f 72 20 55 6e 69 63 6f 64 65 22 2c  ts for Unicode",
49c55 20 61 6e 20 6f 70 65 6e 2d 73 6f 75 72 63 65 20   an open-source 
49c56 6c 69 62 72 61 72 79 20 0a 2a 2a 20 66 6f 72 20  library .** for 
49c57 68 61 6e 64 6c 69 6e 67 20 75 6e 69 63 6f 64 65  handling unicode
49c58 20 64 61 74 61 29 20 61 6e 64 20 53 51 4c 69 74   data) and SQLit
49c59 65 2e 20 54 68 65 20 69 6e 74 65 67 72 61 74 69  e. The integrati
49c5a 6f 6e 20 75 73 65 73 20 0a 2a 2a 20 49 43 55 20  on uses .** ICU 
49c5b 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20 66  to provide the f
49c5c 6f 6c 6c 6f 77 69 6e 67 20 74 6f 20 53 51 4c 69  ollowing to SQLi
49c5d 74 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e  te:.**.**   * An
49c5e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
49c5f 6f 66 20 74 68 65 20 53 51 4c 20 72 65 67 65 78  of the SQL regex
49c60 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 61 6e  p() function (an
49c61 64 20 68 65 6e 63 65 20 52 45 47 45 58 50 0a 2a  d hence REGEXP.*
49c62 2a 20 20 20 20 20 6f 70 65 72 61 74 6f 72 29 20  *     operator) 
49c63 75 73 69 6e 67 20 74 68 65 20 49 43 55 20 75 72  using the ICU ur
49c64 65 67 65 78 5f 58 58 28 29 20 41 50 49 73 2e 0a  egex_XX() APIs..
49c65 2a 2a 0a 2a 2a 20 20 20 2a 20 49 6d 70 6c 65 6d  **.**   * Implem
49c66 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  entations of the
49c67 20 53 51 4c 20 73 63 61 6c 61 72 20 75 70 70 65   SQL scalar uppe
49c68 72 28 29 20 61 6e 64 20 6c 6f 77 65 72 28 29 20  r() and lower() 
49c69 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 20 20  functions.**    
49c6a 20 66 6f 72 20 63 61 73 65 20 6d 61 70 70 69 6e   for case mappin
49c6b 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 6e 74  g..**.**   * Int
49c6c 65 67 72 61 74 69 6f 6e 20 6f 66 20 49 43 55 20  egration of ICU 
49c6d 61 6e 64 20 53 51 4c 69 74 65 20 63 6f 6c 6c 61  and SQLite colla
49c6e 74 69 6f 6e 20 73 65 71 65 6e 63 65 73 2e 0a 2a  tion seqences..*
49c6f 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6d 70 6c  *.**   * An impl
49c70 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
49c71 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20  e LIKE operator 
49c72 74 68 61 74 20 75 73 65 73 20 49 43 55 20 74 6f  that uses ICU to
49c73 20 0a 2a 2a 20 20 20 20 20 70 72 6f 76 69 64 65   .**     provide
49c74 20 63 61 73 65 2d 69 6e 64 65 70 65 6e 64 65 6e   case-independen
49c75 74 20 6d 61 74 63 68 69 6e 67 2e 0a 2a 2f 0a 0a  t matching..*/..
49c76 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
49c77 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
49c78 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
49c79 4c 45 5f 49 43 55 29 0a 0a 2f 2a 20 49 6e 63 6c  LE_ICU)../* Incl
49c7a 75 64 65 20 49 43 55 20 68 65 61 64 65 72 73 20  ude ICU headers 
49c7b 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69  */.#include <uni
49c7c 63 6f 64 65 2f 75 74 79 70 65 73 2e 68 3e 0a 23  code/utypes.h>.#
49c7d 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65  include <unicode
49c7e 2f 75 72 65 67 65 78 2e 68 3e 0a 23 69 6e 63 6c  /uregex.h>.#incl
49c7f 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 73 74  ude <unicode/ust
49c80 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  ring.h>.#include
49c81 20 3c 75 6e 69 63 6f 64 65 2f 75 63 6f 6c 2e 68   <unicode/ucol.h
49c82 3e 0a 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c  >../* #include <
49c83 61 73 73 65 72 74 2e 68 3e 20 2a 2f 0a 0a 23 69  assert.h> */..#i
49c84 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52  fndef SQLITE_COR
49c85 45 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e  E.  SQLITE_EXTEN
49c86 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6c 73 65  SION_INIT1.#else
49c87 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d  .#endif../*.** M
49c88 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 28 69  aximum length (i
49c89 6e 20 62 79 74 65 73 29 20 6f 66 20 74 68 65 20  n bytes) of the 
49c8a 70 61 74 74 65 72 6e 20 69 6e 20 61 20 4c 49 4b  pattern in a LIK
49c8b 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70 65  E or GLOB.** ope
49c8c 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65  rator..*/.#ifnde
49c8d 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b  f SQLITE_MAX_LIK
49c8e 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
49c8f 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
49c90 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52  _MAX_LIKE_PATTER
49c91 4e 5f 4c 45 4e 47 54 48 20 35 30 30 30 30 0a 23  N_LENGTH 50000.#
49c92 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 56 65 72  endif../*.** Ver
49c93 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f  sion of sqlite3_
49c94 66 72 65 65 28 29 20 74 68 61 74 20 69 73 20 61  free() that is a
49c95 6c 77 61 79 73 20 61 20 66 75 6e 63 74 69 6f 6e  lways a function
49c96 2c 20 6e 65 76 65 72 20 61 20 6d 61 63 72 6f 2e  , never a macro.
49c97 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
49c98 78 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a  xFree(void *p){.
49c99 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
49c9a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  );.}../*.** Comp
49c9b 61 72 65 20 74 77 6f 20 55 54 46 2d 38 20 73 74  are two UTF-8 st
49c9c 72 69 6e 67 73 20 66 6f 72 20 65 71 75 61 6c 69  rings for equali
49c9d 74 79 20 77 68 65 72 65 20 74 68 65 20 66 69 72  ty where the fir
49c9e 73 74 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20  st string is.** 
49c9f 61 20 22 4c 49 4b 45 22 20 65 78 70 72 65 73 73  a "LIKE" express
49ca0 69 6f 6e 2e 20 52 65 74 75 72 6e 20 74 72 75 65  ion. Return true
49ca1 20 28 31 29 20 69 66 20 74 68 65 79 20 61 72 65   (1) if they are
49ca2 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20 0a 2a   the same and .*
49ca3 2a 20 66 61 6c 73 65 20 28 30 29 20 69 66 20 74  * false (0) if t
49ca4 68 65 79 20 61 72 65 20 64 69 66 66 65 72 65 6e  hey are differen
49ca5 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
49ca6 20 69 63 75 4c 69 6b 65 43 6f 6d 70 61 72 65 28   icuLikeCompare(
49ca7 0a 20 20 63 6f 6e 73 74 20 75 69 6e 74 38 5f 74  .  const uint8_t
49ca8 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20 20 2f 2a   *zPattern,   /*
49ca9 20 4c 49 4b 45 20 70 61 74 74 65 72 6e 20 2a 2f   LIKE pattern */
49caa 0a 20 20 63 6f 6e 73 74 20 75 69 6e 74 38 5f 74  .  const uint8_t
49cab 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20 20 2f 2a   *zString,    /*
49cac 20 54 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e   The UTF-8 strin
49cad 67 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67 61  g to compare aga
49cae 69 6e 73 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  inst */.  const 
49caf 55 43 68 61 72 33 32 20 75 45 73 63 20 20 20 20  UChar32 uEsc    
49cb0 20 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63 61       /* The esca
49cb1 70 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a  pe character */.
49cb2 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
49cb3 74 20 69 6e 74 20 4d 41 54 43 48 5f 4f 4e 45 20  t int MATCH_ONE 
49cb4 3d 20 28 55 43 68 61 72 33 32 29 27 5f 27 3b 0a  = (UChar32)'_';.
49cb5 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69    static const i
49cb6 6e 74 20 4d 41 54 43 48 5f 41 4c 4c 20 3d 20 28  nt MATCH_ALL = (
49cb7 55 43 68 61 72 33 32 29 27 25 27 3b 0a 0a 20 20  UChar32)'%';..  
49cb8 69 6e 74 20 69 50 61 74 74 65 72 6e 20 3d 20 30  int iPattern = 0
49cb9 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65  ;       /* Curre
49cba 6e 74 20 62 79 74 65 20 69 6e 64 65 78 20 69 6e  nt byte index in
49cbb 20 7a 50 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69   zPattern */.  i
49cbc 6e 74 20 69 53 74 72 69 6e 67 20 3d 20 30 3b 20  nt iString = 0; 
49cbd 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
49cbe 74 20 62 79 74 65 20 69 6e 64 65 78 20 69 6e 20  t byte index in 
49cbf 7a 53 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 69 6e  zString */..  in
49cc0 74 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30  t prevEscape = 0
49cc1 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ;     /* True if
49cc2 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68   the previous ch
49cc3 61 72 61 63 74 65 72 20 77 61 73 20 75 45 73 63  aracter was uEsc
49cc4 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20 7a 50   */..  while( zP
49cc5 61 74 74 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d  attern[iPattern]
49cc6 21 3d 30 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 52  !=0 ){..    /* R
49cc7 65 61 64 20 28 61 6e 64 20 63 6f 6e 73 75 6d 65  ead (and consume
49cc8 29 20 74 68 65 20 6e 65 78 74 20 63 68 61 72 61  ) the next chara
49cc9 63 74 65 72 20 66 72 6f 6d 20 74 68 65 20 69 6e  cter from the in
49cca 70 75 74 20 70 61 74 74 65 72 6e 2e 20 2a 2f 0a  put pattern. */.
49ccb 20 20 20 20 55 43 68 61 72 33 32 20 75 50 61 74      UChar32 uPat
49ccc 74 65 72 6e 3b 0a 20 20 20 20 55 38 5f 4e 45 58  tern;.    U8_NEX
49ccd 54 5f 55 4e 53 41 46 45 28 7a 50 61 74 74 65 72  T_UNSAFE(zPatter
49cce 6e 2c 20 69 50 61 74 74 65 72 6e 2c 20 75 50 61  n, iPattern, uPa
49ccf 74 74 65 72 6e 29 3b 0a 20 20 20 20 61 73 73 65  ttern);.    asse
49cd0 72 74 28 75 50 61 74 74 65 72 6e 21 3d 30 29 3b  rt(uPattern!=0);
49cd1 0a 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61  ..    /* There a
49cd2 72 65 20 6e 6f 77 20 34 20 70 6f 73 73 69 62 69  re now 4 possibi
49cd3 6c 69 74 69 65 73 3a 0a 20 20 20 20 2a 2a 0a 20  lities:.    **. 
49cd4 20 20 20 2a 2a 20 20 20 20 20 31 2e 20 75 50 61     **     1. uPa
49cd5 74 74 65 72 6e 20 69 73 20 61 6e 20 75 6e 65 73  ttern is an unes
49cd6 63 61 70 65 64 20 6d 61 74 63 68 2d 61 6c 6c 20  caped match-all 
49cd7 63 68 61 72 61 63 74 65 72 20 22 25 22 2c 0a 20  character "%",. 
49cd8 20 20 20 2a 2a 20 20 20 20 20 32 2e 20 75 50 61     **     2. uPa
49cd9 74 74 65 72 6e 20 69 73 20 61 6e 20 75 6e 65 73  ttern is an unes
49cda 63 61 70 65 64 20 6d 61 74 63 68 2d 6f 6e 65 20  caped match-one 
49cdb 63 68 61 72 61 63 74 65 72 20 22 5f 22 2c 0a 20  character "_",. 
49cdc 20 20 20 2a 2a 20 20 20 20 20 33 2e 20 75 50 61     **     3. uPa
49cdd 74 74 65 72 6e 20 69 73 20 61 6e 20 75 6e 65 73  ttern is an unes
49cde 63 61 70 65 64 20 65 73 63 61 70 65 20 63 68 61  caped escape cha
49cdf 72 61 63 74 65 72 2c 20 6f 72 0a 20 20 20 20 2a  racter, or.    *
49ce0 2a 20 20 20 20 20 34 2e 20 75 50 61 74 74 65 72  *     4. uPatter
49ce1 6e 20 69 73 20 74 6f 20 62 65 20 68 61 6e 64 6c  n is to be handl
49ce2 65 64 20 61 73 20 61 6e 20 6f 72 64 69 6e 61 72  ed as an ordinar
49ce3 79 20 63 68 61 72 61 63 74 65 72 0a 20 20 20 20  y character.    
49ce4 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 72 65 76  */.    if( !prev
49ce5 45 73 63 61 70 65 20 26 26 20 75 50 61 74 74 65  Escape && uPatte
49ce6 72 6e 3d 3d 4d 41 54 43 48 5f 41 4c 4c 20 29 7b  rn==MATCH_ALL ){
49ce7 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 31  .      /* Case 1
49ce8 2e 20 2a 2f 0a 20 20 20 20 20 20 75 69 6e 74 38  . */.      uint8
49ce9 5f 74 20 63 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  _t c;..      /* 
49cea 53 6b 69 70 20 61 6e 79 20 4d 41 54 43 48 5f 41  Skip any MATCH_A
49ceb 4c 4c 20 6f 72 20 4d 41 54 43 48 5f 4f 4e 45 20  LL or MATCH_ONE 
49cec 63 68 61 72 61 63 74 65 72 73 20 74 68 61 74 20  characters that 
49ced 66 6f 6c 6c 6f 77 20 61 0a 20 20 20 20 20 20 2a  follow a.      *
49cee 2a 20 4d 41 54 43 48 5f 41 4c 4c 2e 20 46 6f 72  * MATCH_ALL. For
49cef 20 65 61 63 68 20 4d 41 54 43 48 5f 4f 4e 45 2c   each MATCH_ONE,
49cf0 20 73 6b 69 70 20 6f 6e 65 20 63 68 61 72 61 63   skip one charac
49cf1 74 65 72 20 69 6e 20 74 68 65 20 0a 20 20 20 20  ter in the .    
49cf2 20 20 2a 2a 20 74 65 73 74 20 73 74 72 69 6e 67    ** test string
49cf3 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
49cf4 20 77 68 69 6c 65 28 20 28 63 3d 7a 50 61 74 74   while( (c=zPatt
49cf5 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 29 20 3d  ern[iPattern]) =
49cf6 3d 20 4d 41 54 43 48 5f 41 4c 4c 20 7c 7c 20 63  = MATCH_ALL || c
49cf7 20 3d 3d 20 4d 41 54 43 48 5f 4f 4e 45 20 29 7b   == MATCH_ONE ){
49cf8 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d  .        if( c==
49cf9 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a 20 20 20  MATCH_ONE ){.   
49cfa 20 20 20 20 20 20 20 69 66 28 20 7a 53 74 72 69         if( zStri
49cfb 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d 30 20 29  ng[iString]==0 )
49cfc 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
49cfd 20 20 20 20 20 55 38 5f 46 57 44 5f 31 5f 55 4e       U8_FWD_1_UN
49cfe 53 41 46 45 28 7a 53 74 72 69 6e 67 2c 20 69 53  SAFE(zString, iS
49cff 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20  tring);.        
49d00 7d 0a 20 20 20 20 20 20 20 20 69 50 61 74 74 65  }.        iPatte
49d01 72 6e 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  rn++;.      }.. 
49d02 20 20 20 20 20 69 66 28 20 7a 50 61 74 74 65 72       if( zPatter
49d03 6e 5b 69 50 61 74 74 65 72 6e 5d 3d 3d 30 20 29  n[iPattern]==0 )
49d04 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 20 20   return 1;..    
49d05 20 20 77 68 69 6c 65 28 20 7a 53 74 72 69 6e 67    while( zString
49d06 5b 69 53 74 72 69 6e 67 5d 20 29 7b 0a 20 20 20  [iString] ){.   
49d07 20 20 20 20 20 69 66 28 20 69 63 75 4c 69 6b 65       if( icuLike
49d08 43 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65 72  Compare(&zPatter
49d09 6e 5b 69 50 61 74 74 65 72 6e 5d 2c 20 26 7a 53  n[iPattern], &zS
49d0a 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 2c 20  tring[iString], 
49d0b 75 45 73 63 29 20 29 7b 0a 20 20 20 20 20 20 20  uEsc) ){.       
49d0c 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
49d0d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 55       }.        U
49d0e 38 5f 46 57 44 5f 31 5f 55 4e 53 41 46 45 28 7a  8_FWD_1_UNSAFE(z
49d0f 53 74 72 69 6e 67 2c 20 69 53 74 72 69 6e 67 29  String, iString)
49d10 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
49d11 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 20 20 7d  return 0;..    }
49d12 65 6c 73 65 20 69 66 28 20 21 70 72 65 76 45 73  else if( !prevEs
49d13 63 61 70 65 20 26 26 20 75 50 61 74 74 65 72 6e  cape && uPattern
49d14 3d 3d 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a 20  ==MATCH_ONE ){. 
49d15 20 20 20 20 20 2f 2a 20 43 61 73 65 20 32 2e 20       /* Case 2. 
49d16 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 53 74  */.      if( zSt
49d17 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d 30  ring[iString]==0
49d18 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
49d19 20 20 20 55 38 5f 46 57 44 5f 31 5f 55 4e 53 41     U8_FWD_1_UNSA
49d1a 46 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72  FE(zString, iStr
49d1b 69 6e 67 29 3b 0a 0a 20 20 20 20 7d 65 6c 73 65  ing);..    }else
49d1c 20 69 66 28 20 21 70 72 65 76 45 73 63 61 70 65   if( !prevEscape
49d1d 20 26 26 20 75 50 61 74 74 65 72 6e 3d 3d 75 45   && uPattern==uE
49d1e 73 63 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61  sc){.      /* Ca
49d1f 73 65 20 33 2e 20 2a 2f 0a 20 20 20 20 20 20 70  se 3. */.      p
49d20 72 65 76 45 73 63 61 70 65 20 3d 20 31 3b 0a 0a  revEscape = 1;..
49d21 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
49d22 20 2f 2a 20 43 61 73 65 20 34 2e 20 2a 2f 0a 20   /* Case 4. */. 
49d23 20 20 20 20 20 55 43 68 61 72 33 32 20 75 53 74       UChar32 uSt
49d24 72 69 6e 67 3b 0a 20 20 20 20 20 20 55 38 5f 4e  ring;.      U8_N
49d25 45 58 54 5f 55 4e 53 41 46 45 28 7a 53 74 72 69  EXT_UNSAFE(zStri
49d26 6e 67 2c 20 69 53 74 72 69 6e 67 2c 20 75 53 74  ng, iString, uSt
49d27 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 75 53 74  ring);.      uSt
49d28 72 69 6e 67 20 3d 20 75 5f 66 6f 6c 64 43 61 73  ring = u_foldCas
49d29 65 28 75 53 74 72 69 6e 67 2c 20 55 5f 46 4f 4c  e(uString, U_FOL
49d2a 44 5f 43 41 53 45 5f 44 45 46 41 55 4c 54 29 3b  D_CASE_DEFAULT);
49d2b 0a 20 20 20 20 20 20 75 50 61 74 74 65 72 6e 20  .      uPattern 
49d2c 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28 75 50 61  = u_foldCase(uPa
49d2d 74 74 65 72 6e 2c 20 55 5f 46 4f 4c 44 5f 43 41  ttern, U_FOLD_CA
49d2e 53 45 5f 44 45 46 41 55 4c 54 29 3b 0a 20 20 20  SE_DEFAULT);.   
49d2f 20 20 20 69 66 28 20 75 53 74 72 69 6e 67 21 3d     if( uString!=
49d30 75 50 61 74 74 65 72 6e 20 29 7b 0a 20 20 20 20  uPattern ){.    
49d31 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
49d32 20 20 20 20 7d 0a 20 20 20 20 20 20 70 72 65 76      }.      prev
49d33 45 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 20 20  Escape = 0;.    
49d34 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
49d35 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d  zString[iString]
49d36 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  ==0;.}../*.** Im
49d37 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
49d38 74 68 65 20 6c 69 6b 65 28 29 20 53 51 4c 20 66  the like() SQL f
49d39 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66  unction.  This f
49d3a 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  unction implemen
49d3b 74 73 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d  ts.** the build-
49d3c 69 6e 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72  in LIKE operator
49d3d 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67  .  The first arg
49d3e 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e  ument to the fun
49d3f 63 74 69 6f 6e 20 69 73 20 74 68 65 0a 2a 2a 20  ction is the.** 
49d40 70 61 74 74 65 72 6e 20 61 6e 64 20 74 68 65 20  pattern and the 
49d41 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
49d42 69 73 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20  is the string.  
49d43 53 6f 2c 20 74 68 65 20 53 51 4c 20 73 74 61 74  So, the SQL stat
49d44 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ements:.**.**   
49d45 20 20 20 20 41 20 4c 49 4b 45 20 42 0a 2a 2a 0a      A LIKE B.**.
49d46 2a 2a 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65  ** is implemente
49d47 64 20 61 73 20 6c 69 6b 65 28 42 2c 20 41 29 2e  d as like(B, A).
49d48 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   If there is an 
49d49 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65 72  escape character
49d4a 20 45 2c 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20   E, .**.**      
49d4b 20 41 20 4c 49 4b 45 20 42 20 45 53 43 41 50 45   A LIKE B ESCAPE
49d4c 20 45 0a 2a 2a 0a 2a 2a 20 69 73 20 6d 61 70 70   E.**.** is mapp
49d4d 65 64 20 74 6f 20 6c 69 6b 65 28 42 2c 20 41 2c  ed to like(B, A,
49d4e 20 45 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76   E)..*/.static v
49d4f 6f 69 64 20 69 63 75 4c 69 6b 65 46 75 6e 63 28  oid icuLikeFunc(
49d50 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
49d51 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20  xt *context, .  
49d52 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 71 6c  int argc, .  sql
49d53 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
49d54 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  v.){.  const uns
49d55 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41 20 3d  igned char *zA =
49d56 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
49d57 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
49d58 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
49d59 68 61 72 20 2a 7a 42 20 3d 20 73 71 6c 69 74 65  har *zB = sqlite
49d5a 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
49d5b 76 5b 31 5d 29 3b 0a 20 20 55 43 68 61 72 33 32  v[1]);.  UChar32
49d5c 20 75 45 73 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a   uEsc = 0;..  /*
49d5d 20 4c 69 6d 69 74 20 74 68 65 20 6c 65 6e 67 74   Limit the lengt
49d5e 68 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f 72  h of the LIKE or
49d5f 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f   GLOB pattern to
49d60 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a   avoid problems.
49d61 20 20 2a 2a 20 6f 66 20 64 65 65 70 20 72 65 63    ** of deep rec
49d62 75 72 73 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62  ursion and N*N b
49d63 65 68 61 76 69 6f 72 20 69 6e 20 70 61 74 74 65  ehavior in patte
49d64 72 6e 43 6f 6d 70 61 72 65 28 29 2e 0a 20 20 2a  rnCompare()..  *
49d65 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  /.  if( sqlite3_
49d66 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
49d67 5b 30 5d 29 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  [0])>SQLITE_MAX_
49d68 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
49d69 47 54 48 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  GTH ){.    sqlit
49d6a 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
49d6b 63 6f 6e 74 65 78 74 2c 20 22 4c 49 4b 45 20 6f  context, "LIKE o
49d6c 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74  r GLOB pattern t
49d6d 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c 20 2d 31 29  oo complex", -1)
49d6e 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
49d6f 7d 0a 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d  }...  if( argc==
49d70 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  3 ){.    /* The 
49d71 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65 72  escape character
49d72 20 73 74 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e   string must con
49d73 73 69 73 74 20 6f 66 20 61 20 73 69 6e 67 6c 65  sist of a single
49d74 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
49d75 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69  ..    ** Otherwi
49d76 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72  se, return an er
49d77 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ror..    */.    
49d78 69 6e 74 20 6e 45 3d 20 73 71 6c 69 74 65 33 5f  int nE= sqlite3_
49d79 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
49d7a 5b 32 5d 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20  [2]);.    const 
49d7b 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
49d7c 45 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  E = sqlite3_valu
49d7d 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b  e_text(argv[2]);
49d7e 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a  .    int i = 0;.
49d7f 20 20 20 20 69 66 28 20 7a 45 3d 3d 30 20 29 20      if( zE==0 ) 
49d80 72 65 74 75 72 6e 3b 0a 20 20 20 20 55 38 5f 4e  return;.    U8_N
49d81 45 58 54 28 7a 45 2c 20 69 2c 20 6e 45 2c 20 75  EXT(zE, i, nE, u
49d82 45 73 63 29 3b 0a 20 20 20 20 69 66 28 20 69 21  Esc);.    if( i!
49d83 3d 6e 45 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =nE){.      sqli
49d84 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
49d85 28 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20  (context, .     
49d86 20 20 20 20 20 22 45 53 43 41 50 45 20 65 78 70       "ESCAPE exp
49d87 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20  ression must be 
49d88 61 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63 74  a single charact
49d89 65 72 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  er", -1);.      
49d8a 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
49d8b 7d 0a 0a 20 20 69 66 28 20 7a 41 20 26 26 20 7a  }..  if( zA && z
49d8c 42 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  B ){.    sqlite3
49d8d 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74  _result_int(cont
49d8e 65 78 74 2c 20 69 63 75 4c 69 6b 65 43 6f 6d 70  ext, icuLikeComp
49d8f 61 72 65 28 7a 41 2c 20 7a 42 2c 20 75 45 73 63  are(zA, zB, uEsc
49d90 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ));.  }.}../*.**
49d91 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
49d92 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e  s called when an
49d93 20 49 43 55 20 66 75 6e 63 74 69 6f 6e 20 63 61   ICU function ca
49d94 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
49d95 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  .** the implemen
49d96 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c  tation of an SQL
49d97 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
49d98 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f   returns an erro
49d99 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 61  r..**.** The sca
49d9a 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e  lar function con
49d9b 74 65 78 74 20 70 61 73 73 65 64 20 61 73 20 74  text passed as t
49d9c 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
49d9d 74 20 69 73 20 0a 2a 2a 20 6c 6f 61 64 65 64 20  t is .** loaded 
49d9e 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65  with an error me
49d9f 73 73 61 67 65 20 62 61 73 65 64 20 6f 6e 20 74  ssage based on t
49da0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f  he following two
49da1 20 61 72 67 73 2e 0a 2a 2f 0a 73 74 61 74 69 63   args..*/.static
49da2 20 76 6f 69 64 20 69 63 75 46 75 6e 63 74 69 6f   void icuFunctio
49da3 6e 45 72 72 6f 72 28 0a 20 20 73 71 6c 69 74 65  nError(.  sqlite
49da4 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
49da5 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65         /* SQLite
49da6 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
49da7 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f   context */.  co
49da8 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
49da9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
49daa 6d 65 20 6f 66 20 49 43 55 20 66 75 6e 63 74 69  me of ICU functi
49dab 6f 6e 20 74 68 61 74 20 66 61 69 6c 65 64 20 2a  on that failed *
49dac 2f 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20 65  /.  UErrorCode e
49dad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49dae 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 72   /* Error code r
49daf 65 74 75 72 6e 65 64 20 62 79 20 49 43 55 20 66  eturned by ICU f
49db0 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20  unction */.){.  
49db1 63 68 61 72 20 7a 42 75 66 5b 31 32 38 5d 3b 0a  char zBuf[128];.
49db2 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
49db3 74 66 28 31 32 38 2c 20 7a 42 75 66 2c 20 22 49  tf(128, zBuf, "I
49db4 43 55 20 65 72 72 6f 72 3a 20 25 73 28 29 3a 20  CU error: %s(): 
49db5 25 73 22 2c 20 7a 4e 61 6d 65 2c 20 75 5f 65 72  %s", zName, u_er
49db6 72 6f 72 4e 61 6d 65 28 65 29 29 3b 0a 20 20 7a  rorName(e));.  z
49db7 42 75 66 5b 31 32 37 5d 20 3d 20 27 5c 30 27 3b  Buf[127] = '\0';
49db8 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
49db9 74 5f 65 72 72 6f 72 28 70 43 74 78 2c 20 7a 42  t_error(pCtx, zB
49dba 75 66 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  uf, -1);.}../*.*
49dbb 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65  * Function to de
49dbc 6c 65 74 65 20 63 6f 6d 70 69 6c 65 64 20 72 65  lete compiled re
49dbd 67 65 78 70 20 6f 62 6a 65 63 74 73 2e 20 52 65  gexp objects. Re
49dbe 67 69 73 74 65 72 65 64 20 61 73 0a 2a 2a 20 61  gistered as.** a
49dbf 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63   destructor func
49dc0 74 69 6f 6e 20 77 69 74 68 20 73 71 6c 69 74 65  tion with sqlite
49dc1 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 2e  3_set_auxdata().
49dc2 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
49dc3 69 63 75 52 65 67 65 78 70 44 65 6c 65 74 65 28  icuRegexpDelete(
49dc4 76 6f 69 64 20 2a 70 29 7b 0a 20 20 55 52 65 67  void *p){.  UReg
49dc5 75 6c 61 72 45 78 70 72 65 73 73 69 6f 6e 20 2a  ularExpression *
49dc6 70 45 78 70 72 20 3d 20 28 55 52 65 67 75 6c 61  pExpr = (URegula
49dc7 72 45 78 70 72 65 73 73 69 6f 6e 20 2a 29 70 3b  rExpression *)p;
49dc8 0a 20 20 75 72 65 67 65 78 5f 63 6c 6f 73 65 28  .  uregex_close(
49dc9 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pExpr);.}../*.**
49dca 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
49dcb 6f 66 20 53 51 4c 69 74 65 20 52 45 47 45 58 50  of SQLite REGEXP
49dcc 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 69 73 20   operator. This 
49dcd 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  scalar function 
49dce 74 61 6b 65 73 0a 2a 2a 20 74 77 6f 20 61 72 67  takes.** two arg
49dcf 75 6d 65 6e 74 73 2e 20 54 68 65 20 66 69 72 73  uments. The firs
49dd0 74 20 69 73 20 61 20 72 65 67 75 6c 61 72 20 65  t is a regular e
49dd1 78 70 72 65 73 73 69 6f 6e 20 70 61 74 74 65 72  xpression patter
49dd2 6e 20 74 6f 20 63 6f 6d 70 69 6c 65 0a 2a 2a 20  n to compile.** 
49dd3 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 61 20  the second is a 
49dd4 73 74 72 69 6e 67 20 74 6f 20 6d 61 74 63 68 20  string to match 
49dd5 61 67 61 69 6e 73 74 20 74 68 61 74 20 70 61 74  against that pat
49dd6 74 65 72 6e 2e 20 49 66 20 65 69 74 68 65 72 20  tern. If either 
49dd7 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20  .** argument is 
49dd8 61 6e 20 53 51 4c 20 4e 55 4c 4c 2c 20 74 68 65  an SQL NULL, the
49dd9 6e 20 4e 55 4c 4c 20 49 73 20 72 65 74 75 72 6e  n NULL Is return
49dda 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74  ed. Otherwise, t
49ddb 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20  he result.** is 
49ddc 31 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20  1 if the string 
49ddd 6d 61 74 63 68 65 73 20 74 68 65 20 70 61 74 74  matches the patt
49dde 65 72 6e 2c 20 6f 72 20 30 20 6f 74 68 65 72 77  ern, or 0 otherw
49ddf 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  ise..**.** SQLit
49de0 65 20 6d 61 70 73 20 74 68 65 20 72 65 67 65 78  e maps the regex
49de1 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  p() function to 
49de2 74 68 65 20 72 65 67 65 78 70 28 29 20 6f 70 65  the regexp() ope
49de3 72 61 74 6f 72 20 73 75 63 68 0a 2a 2a 20 74 68  rator such.** th
49de4 61 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  at the following
49de5 20 74 77 6f 20 61 72 65 20 65 71 75 69 76 61 6c   two are equival
49de6 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 7a  ent:.**.**     z
49de7 53 74 72 69 6e 67 20 52 45 47 45 58 50 20 7a 50  String REGEXP zP
49de8 61 74 74 65 72 6e 0a 2a 2a 20 20 20 20 20 72 65  attern.**     re
49de9 67 65 78 70 28 7a 50 61 74 74 65 72 6e 2c 20 7a  gexp(zPattern, z
49dea 53 74 72 69 6e 67 29 0a 2a 2a 0a 2a 2a 20 55 73  String).**.** Us
49deb 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  es the following
49dec 20 49 43 55 20 72 65 67 65 78 70 20 41 50 49 73   ICU regexp APIs
49ded 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 75 72 65 67  :.**.**     ureg
49dee 65 78 5f 6f 70 65 6e 28 29 0a 2a 2a 20 20 20 20  ex_open().**    
49def 20 75 72 65 67 65 78 5f 6d 61 74 63 68 65 73 28   uregex_matches(
49df0 29 0a 2a 2a 20 20 20 20 20 75 72 65 67 65 78 5f  ).**     uregex_
49df1 63 6c 6f 73 65 28 29 0a 2a 2f 0a 73 74 61 74 69  close().*/.stati
49df2 63 20 76 6f 69 64 20 69 63 75 52 65 67 65 78 70  c void icuRegexp
49df3 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e  Func(sqlite3_con
49df4 74 65 78 74 20 2a 70 2c 20 69 6e 74 20 6e 41 72  text *p, int nAr
49df5 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  g, sqlite3_value
49df6 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20 55 45 72   **apArg){.  UEr
49df7 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d  rorCode status =
49df8 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20   U_ZERO_ERROR;. 
49df9 20 55 52 65 67 75 6c 61 72 45 78 70 72 65 73 73   URegularExpress
49dfa 69 6f 6e 20 2a 70 45 78 70 72 3b 0a 20 20 55 42  ion *pExpr;.  UB
49dfb 6f 6f 6c 20 72 65 73 3b 0a 20 20 63 6f 6e 73 74  ool res;.  const
49dfc 20 55 43 68 61 72 20 2a 7a 53 74 72 69 6e 67 20   UChar *zString 
49dfd 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
49dfe 74 65 78 74 31 36 28 61 70 41 72 67 5b 31 5d 29  text16(apArg[1])
49dff 3b 0a 0a 20 20 28 76 6f 69 64 29 6e 41 72 67 3b  ;..  (void)nArg;
49e00 20 20 2f 2a 20 55 6e 75 73 65 64 20 70 61 72 61    /* Unused para
49e01 6d 65 74 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 49  meter */..  /* I
49e02 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 20  f the left hand 
49e03 73 69 64 65 20 6f 66 20 74 68 65 20 72 65 67 65  side of the rege
49e04 78 70 20 6f 70 65 72 61 74 6f 72 20 69 73 20 4e  xp operator is N
49e05 55 4c 4c 2c 20 0a 20 20 2a 2a 20 74 68 65 6e 20  ULL, .  ** then 
49e06 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c  the result is al
49e07 73 6f 20 4e 55 4c 4c 2e 20 0a 20 20 2a 2f 0a 20  so NULL. .  */. 
49e08 20 69 66 28 20 21 7a 53 74 72 69 6e 67 20 29 7b   if( !zString ){
49e09 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
49e0a 0a 0a 20 20 70 45 78 70 72 20 3d 20 73 71 6c 69  ..  pExpr = sqli
49e0b 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
49e0c 70 2c 20 30 29 3b 0a 20 20 69 66 28 20 21 70 45  p, 0);.  if( !pE
49e0d 78 70 72 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  xpr ){.    const
49e0e 20 55 43 68 61 72 20 2a 7a 50 61 74 74 65 72 6e   UChar *zPattern
49e0f 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
49e10 5f 74 65 78 74 31 36 28 61 70 41 72 67 5b 30 5d  _text16(apArg[0]
49e11 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 50 61 74  );.    if( !zPat
49e12 74 65 72 6e 20 29 7b 0a 20 20 20 20 20 20 72 65  tern ){.      re
49e13 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
49e14 70 45 78 70 72 20 3d 20 75 72 65 67 65 78 5f 6f  pExpr = uregex_o
49e15 70 65 6e 28 7a 50 61 74 74 65 72 6e 2c 20 2d 31  pen(zPattern, -1
49e16 2c 20 30 2c 20 30 2c 20 26 73 74 61 74 75 73 29  , 0, 0, &status)
49e17 3b 0a 0a 20 20 20 20 69 66 28 20 55 5f 53 55 43  ;..    if( U_SUC
49e18 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a  CESS(status) ){.
49e19 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65        sqlite3_se
49e1a 74 5f 61 75 78 64 61 74 61 28 70 2c 20 30 2c 20  t_auxdata(p, 0, 
49e1b 70 45 78 70 72 2c 20 69 63 75 52 65 67 65 78 70  pExpr, icuRegexp
49e1c 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 7d 65 6c  Delete);.    }el
49e1d 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
49e1e 28 21 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  (!pExpr);.      
49e1f 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72  icuFunctionError
49e20 28 70 2c 20 22 75 72 65 67 65 78 5f 6f 70 65 6e  (p, "uregex_open
49e21 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20  ", status);.    
49e22 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
49e23 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 66 69 67    }..  /* Config
49e24 75 72 65 20 74 68 65 20 74 65 78 74 20 74 68 61  ure the text tha
49e25 74 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78  t the regular ex
49e26 70 72 65 73 73 69 6f 6e 20 6f 70 65 72 61 74 65  pression operate
49e27 73 20 6f 6e 2e 20 2a 2f 0a 20 20 75 72 65 67 65  s on. */.  urege
49e28 78 5f 73 65 74 54 65 78 74 28 70 45 78 70 72 2c  x_setText(pExpr,
49e29 20 7a 53 74 72 69 6e 67 2c 20 2d 31 2c 20 26 73   zString, -1, &s
49e2a 74 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55  tatus);.  if( !U
49e2b 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29  _SUCCESS(status)
49e2c 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74   ){.    icuFunct
49e2d 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72 65  ionError(p, "ure
49e2e 67 65 78 5f 73 65 74 54 65 78 74 22 2c 20 73 74  gex_setText", st
49e2f 61 74 75 73 29 3b 0a 20 20 20 20 72 65 74 75 72  atus);.    retur
49e30 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 74  n;.  }..  /* Att
49e31 65 6d 70 74 20 74 68 65 20 6d 61 74 63 68 20 2a  empt the match *
49e32 2f 0a 20 20 72 65 73 20 3d 20 75 72 65 67 65 78  /.  res = uregex
49e33 5f 6d 61 74 63 68 65 73 28 70 45 78 70 72 2c 20  _matches(pExpr, 
49e34 30 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 69  0, &status);.  i
49e35 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73 74  f( !U_SUCCESS(st
49e36 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69 63 75  atus) ){.    icu
49e37 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70 2c  FunctionError(p,
49e38 20 22 75 72 65 67 65 78 5f 6d 61 74 63 68 65 73   "uregex_matches
49e39 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20  ", status);.    
49e3a 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  return;.  }..  /
49e3b 2a 20 53 65 74 20 74 68 65 20 74 65 78 74 20 74  * Set the text t
49e3c 68 61 74 20 74 68 65 20 72 65 67 75 6c 61 72 20  hat the regular 
49e3d 65 78 70 72 65 73 73 69 6f 6e 20 6f 70 65 72 61  expression opera
49e3e 74 65 73 20 6f 6e 20 74 6f 20 61 20 4e 55 4c 4c  tes on to a NULL
49e3f 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 54  .  ** pointer. T
49e40 68 69 73 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c  his is not reall
49e41 79 20 6e 65 63 65 73 73 61 72 79 2c 20 62 75 74  y necessary, but
49e42 20 69 74 20 69 73 20 74 69 64 69 65 72 20 74 68   it is tidier th
49e43 61 6e 20 0a 20 20 2a 2a 20 6c 65 61 76 69 6e 67  an .  ** leaving
49e44 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70   the regular exp
49e45 72 65 73 73 69 6f 6e 20 6f 62 6a 65 63 74 20 63  ression object c
49e46 6f 6e 66 69 67 75 72 65 64 20 77 69 74 68 20 61  onfigured with a
49e47 6e 20 69 6e 76 61 6c 69 64 0a 20 20 2a 2a 20 70  n invalid.  ** p
49e48 6f 69 6e 74 65 72 20 61 66 74 65 72 20 74 68 69  ointer after thi
49e49 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
49e4a 6e 73 2e 0a 20 20 2a 2f 0a 20 20 75 72 65 67 65  ns..  */.  urege
49e4b 78 5f 73 65 74 54 65 78 74 28 70 45 78 70 72 2c  x_setText(pExpr,
49e4c 20 30 2c 20 30 2c 20 26 73 74 61 74 75 73 29 3b   0, 0, &status);
49e4d 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 31 20  ..  /* Return 1 
49e4e 6f 72 20 30 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  or 0. */.  sqlit
49e4f 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 70 2c  e3_result_int(p,
49e50 20 72 65 73 20 3f 20 31 20 3a 20 30 29 3b 0a 7d   res ? 1 : 0);.}
49e51 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
49e52 74 61 74 69 6f 6e 73 20 6f 66 20 73 63 61 6c 61  tations of scala
49e53 72 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20  r functions for 
49e54 63 61 73 65 20 6d 61 70 70 69 6e 67 20 2d 20 75  case mapping - u
49e55 70 70 65 72 28 29 20 61 6e 64 20 0a 2a 2a 20 6c  pper() and .** l
49e56 6f 77 65 72 28 29 2e 20 46 75 6e 63 74 69 6f 6e  ower(). Function
49e57 20 75 70 70 65 72 28 29 20 63 6f 6e 76 65 72 74   upper() convert
49e58 73 20 69 74 73 20 69 6e 70 75 74 20 74 6f 20 75  s its input to u
49e59 70 70 65 72 2d 63 61 73 65 20 28 41 42 43 29 2e  pper-case (ABC).
49e5a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 6c 6f 77  .** Function low
49e5b 65 72 28 29 20 63 6f 6e 76 65 72 74 73 20 74 6f  er() converts to
49e5c 20 6c 6f 77 65 72 2d 63 61 73 65 20 28 61 62 63   lower-case (abc
49e5d 29 2e 0a 2a 2a 0a 2a 2a 20 49 43 55 20 70 72 6f  )..**.** ICU pro
49e5e 76 69 64 65 73 20 74 77 6f 20 74 79 70 65 73 20  vides two types 
49e5f 6f 66 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2c  of case mapping,
49e60 20 22 67 65 6e 65 72 61 6c 22 20 63 61 73 65 20   "general" case 
49e61 6d 61 70 70 69 6e 67 20 61 6e 64 0a 2a 2a 20 22  mapping and.** "
49e62 6c 61 6e 67 75 61 67 65 20 73 70 65 63 69 66 69  language specifi
49e63 63 22 2e 20 52 65 66 65 72 20 74 6f 20 49 43 55  c". Refer to ICU
49e64 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66   documentation f
49e65 6f 72 20 74 68 65 20 64 69 66 66 65 72 65 6e 63  or the differenc
49e66 65 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 74 68  es.** between th
49e67 65 20 74 77 6f 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20  e two..**.** To 
49e68 75 74 69 6c 69 73 65 20 22 67 65 6e 65 72 61 6c  utilise "general
49e69 22 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2c 20  " case mapping, 
49e6a 74 68 65 20 75 70 70 65 72 28 29 20 6f 72 20 6c  the upper() or l
49e6b 6f 77 65 72 28 29 20 73 63 61 6c 61 72 20 0a 2a  ower() scalar .*
49e6c 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  * functions are 
49e6d 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 6f 6e 65  invoked with one
49e6e 20 61 72 67 75 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a   argument:.**.**
49e6f 20 20 20 20 20 75 70 70 65 72 28 27 41 42 43 27       upper('ABC'
49e70 29 20 2d 3e 20 27 61 62 63 27 0a 2a 2a 20 20 20  ) -> 'abc'.**   
49e71 20 20 6c 6f 77 65 72 28 27 61 62 63 27 29 20 2d    lower('abc') -
49e72 3e 20 27 41 42 43 27 0a 2a 2a 0a 2a 2a 20 54 6f  > 'ABC'.**.** To
49e73 20 61 63 63 65 73 73 20 49 43 55 20 22 6c 61 6e   access ICU "lan
49e74 67 75 61 67 65 20 73 70 65 63 69 66 69 63 22 20  guage specific" 
49e75 63 61 73 65 20 6d 61 70 70 69 6e 67 2c 20 75 70  case mapping, up
49e76 70 65 72 28 29 20 6f 72 20 6c 6f 77 65 72 28 29  per() or lower()
49e77 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 69 6e  .** should be in
49e78 76 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20 61  voked with two a
49e79 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 20 73 65  rguments. The se
49e7a 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
49e7b 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20   the name.** of 
49e7c 74 68 65 20 6c 6f 63 61 6c 65 20 74 6f 20 75 73  the locale to us
49e7d 65 2e 20 50 61 73 73 69 6e 67 20 61 6e 20 65 6d  e. Passing an em
49e7e 70 74 79 20 73 74 72 69 6e 67 20 28 22 22 29 20  pty string ("") 
49e7f 6f 72 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75  or SQL NULL valu
49e80 65 0a 2a 2a 20 61 73 20 74 68 65 20 73 65 63 6f  e.** as the seco
49e81 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  nd argument is t
49e82 68 65 20 73 61 6d 65 20 61 73 20 69 6e 76 6f 6b  he same as invok
49e83 69 6e 67 20 74 68 65 20 31 20 61 72 67 75 6d 65  ing the 1 argume
49e84 6e 74 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66  nt version.** of
49e85 20 75 70 70 65 72 28 29 20 6f 72 20 6c 6f 77 65   upper() or lowe
49e86 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 6c  r()..**.**     l
49e87 6f 77 65 72 28 27 49 27 2c 20 27 65 6e 5f 75 73  ower('I', 'en_us
49e88 27 29 20 2d 3e 20 27 69 27 0a 2a 2a 20 20 20 20  ') -> 'i'.**    
49e89 20 6c 6f 77 65 72 28 27 49 27 2c 20 27 74 72 5f   lower('I', 'tr_
49e8a 74 72 27 29 20 2d 3e 20 27 c4 b1 27 20 28 73 6d  tr') -> '..' (sm
49e8b 61 6c 6c 20 64 6f 74 6c 65 73 73 20 69 29 0a 2a  all dotless i).*
49e8c 2a 0a 2a 2a 20 68 74 74 70 3a 2f 2f 77 77 77 2e  *.** http://www.
49e8d 69 63 75 2d 70 72 6f 6a 65 63 74 2e 6f 72 67 2f  icu-project.org/
49e8e 75 73 65 72 67 75 69 64 65 2f 70 6f 73 69 78 2e  userguide/posix.
49e8f 68 74 6d 6c 23 63 61 73 65 5f 6d 61 70 70 69 6e  html#case_mappin
49e90 67 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  gs.*/.static voi
49e91 64 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 28  d icuCaseFunc16(
49e92 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
49e93 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20 73 71  *p, int nArg, sq
49e94 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70  lite3_value **ap
49e95 41 72 67 29 7b 0a 20 20 63 6f 6e 73 74 20 55 43  Arg){.  const UC
49e96 68 61 72 20 2a 7a 49 6e 70 75 74 3b 0a 20 20 55  har *zInput;.  U
49e97 43 68 61 72 20 2a 7a 4f 75 74 70 75 74 3b 0a 20  Char *zOutput;. 
49e98 20 69 6e 74 20 6e 49 6e 70 75 74 3b 0a 20 20 69   int nInput;.  i
49e99 6e 74 20 6e 4f 75 74 70 75 74 3b 0a 0a 20 20 55  nt nOutput;..  U
49e9a 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73  ErrorCode status
49e9b 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b   = U_ZERO_ERROR;
49e9c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
49e9d 4c 6f 63 61 6c 65 20 3d 20 30 3b 0a 0a 20 20 61  Locale = 0;..  a
49e9e 73 73 65 72 74 28 6e 41 72 67 3d 3d 31 20 7c 7c  ssert(nArg==1 ||
49e9f 20 6e 41 72 67 3d 3d 32 29 3b 0a 20 20 69 66 28   nArg==2);.  if(
49ea0 20 6e 41 72 67 3d 3d 32 20 29 7b 0a 20 20 20 20   nArg==2 ){.    
49ea1 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 6f 6e 73 74  zLocale = (const
49ea2 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
49ea3 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41 72 67  value_text(apArg
49ea4 5b 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 7a 49 6e  [1]);.  }..  zIn
49ea5 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  put = sqlite3_va
49ea6 6c 75 65 5f 74 65 78 74 31 36 28 61 70 41 72 67  lue_text16(apArg
49ea7 5b 30 5d 29 3b 0a 20 20 69 66 28 20 21 7a 49 6e  [0]);.  if( !zIn
49ea8 70 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  put ){.    retur
49ea9 6e 3b 0a 20 20 7d 0a 20 20 6e 49 6e 70 75 74 20  n;.  }.  nInput 
49eaa 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
49eab 62 79 74 65 73 31 36 28 61 70 41 72 67 5b 30 5d  bytes16(apArg[0]
49eac 29 3b 0a 0a 20 20 6e 4f 75 74 70 75 74 20 3d 20  );..  nOutput = 
49ead 6e 49 6e 70 75 74 20 2a 20 32 20 2b 20 32 3b 0a  nInput * 2 + 2;.
49eae 20 20 7a 4f 75 74 70 75 74 20 3d 20 73 71 6c 69    zOutput = sqli
49eaf 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4f 75 74 70  te3_malloc(nOutp
49eb0 75 74 29 3b 0a 20 20 69 66 28 20 21 7a 4f 75 74  ut);.  if( !zOut
49eb1 70 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  put ){.    retur
49eb2 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71  n;.  }..  if( sq
49eb3 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
49eb4 70 29 20 29 7b 0a 20 20 20 20 75 5f 73 74 72 54  p) ){.    u_strT
49eb5 6f 55 70 70 65 72 28 7a 4f 75 74 70 75 74 2c 20  oUpper(zOutput, 
49eb6 6e 4f 75 74 70 75 74 2f 32 2c 20 7a 49 6e 70 75  nOutput/2, zInpu
49eb7 74 2c 20 6e 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f  t, nInput/2, zLo
49eb8 63 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b 0a  cale, &status);.
49eb9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 5f 73    }else{.    u_s
49eba 74 72 54 6f 4c 6f 77 65 72 28 7a 4f 75 74 70 75  trToLower(zOutpu
49ebb 74 2c 20 6e 4f 75 74 70 75 74 2f 32 2c 20 7a 49  t, nOutput/2, zI
49ebc 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2f 32 2c 20  nput, nInput/2, 
49ebd 7a 4c 6f 63 61 6c 65 2c 20 26 73 74 61 74 75 73  zLocale, &status
49ebe 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 55  );.  }..  if( !U
49ebf 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29  _SUCCESS(status)
49ec0 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74   ){.    icuFunct
49ec1 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 5f 73  ionError(p, "u_s
49ec2 74 72 54 6f 4c 6f 77 65 72 28 29 2f 75 5f 73 74  trToLower()/u_st
49ec3 72 54 6f 55 70 70 65 72 22 2c 20 73 74 61 74 75  rToUpper", statu
49ec4 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  s);.    return;.
49ec5 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72    }..  sqlite3_r
49ec6 65 73 75 6c 74 5f 74 65 78 74 31 36 28 70 2c 20  esult_text16(p, 
49ec7 7a 4f 75 74 70 75 74 2c 20 2d 31 2c 20 78 46 72  zOutput, -1, xFr
49ec8 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  ee);.}../*.** Co
49ec9 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
49eca 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63   destructor func
49ecb 74 69 6f 6e 2e 20 54 68 65 20 70 43 74 78 20 61  tion. The pCtx a
49ecc 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74  rgument points t
49ecd 6f 0a 2a 2a 20 61 20 55 43 6f 6c 6c 61 74 6f 72  o.** a UCollator
49ece 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69   structure previ
49ecf 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ously allocated 
49ed0 75 73 69 6e 67 20 75 63 6f 6c 5f 6f 70 65 6e 28  using ucol_open(
49ed1 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
49ed2 64 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 44 65  d icuCollationDe
49ed3 6c 28 76 6f 69 64 20 2a 70 43 74 78 29 7b 0a 20  l(void *pCtx){. 
49ed4 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d 20   UCollator *p = 
49ed5 28 55 43 6f 6c 6c 61 74 6f 72 20 2a 29 70 43 74  (UCollator *)pCt
49ed6 78 3b 0a 20 20 75 63 6f 6c 5f 63 6c 6f 73 65 28  x;.  ucol_close(
49ed7 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c  p);.}../*.** Col
49ed8 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
49ed9 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74  comparison funct
49eda 69 6f 6e 2e 20 54 68 65 20 70 43 74 78 20 61 72  ion. The pCtx ar
49edb 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f  gument points to
49edc 0a 2a 2a 20 61 20 55 43 6f 6c 6c 61 74 6f 72 20  .** a UCollator 
49edd 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69 6f  structure previo
49ede 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 75  usly allocated u
49edf 73 69 6e 67 20 75 63 6f 6c 5f 6f 70 65 6e 28 29  sing ucol_open()
49ee0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
49ee1 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 43 6f 6c 6c  icuCollationColl
49ee2 28 0a 20 20 76 6f 69 64 20 2a 70 43 74 78 2c 0a  (.  void *pCtx,.
49ee3 20 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63    int nLeft,.  c
49ee4 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4c 65 66 74  onst void *zLeft
49ee5 2c 0a 20 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a  ,.  int nRight,.
49ee6 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 52    const void *zR
49ee7 69 67 68 74 0a 29 7b 0a 20 20 55 43 6f 6c 6c 61  ight.){.  UColla
49ee8 74 69 6f 6e 52 65 73 75 6c 74 20 72 65 73 3b 0a  tionResult res;.
49ee9 20 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d    UCollator *p =
49eea 20 28 55 43 6f 6c 6c 61 74 6f 72 20 2a 29 70 43   (UCollator *)pC
49eeb 74 78 3b 0a 20 20 72 65 73 20 3d 20 75 63 6f 6c  tx;.  res = ucol
49eec 5f 73 74 72 63 6f 6c 6c 28 70 2c 20 28 55 43 68  _strcoll(p, (UCh
49eed 61 72 20 2a 29 7a 4c 65 66 74 2c 20 6e 4c 65 66  ar *)zLeft, nLef
49eee 74 2f 32 2c 20 28 55 43 68 61 72 20 2a 29 7a 52  t/2, (UChar *)zR
49eef 69 67 68 74 2c 20 6e 52 69 67 68 74 2f 32 29 3b  ight, nRight/2);
49ef0 0a 20 20 73 77 69 74 63 68 28 20 72 65 73 20 29  .  switch( res )
49ef1 7b 0a 20 20 20 20 63 61 73 65 20 55 43 4f 4c 5f  {.    case UCOL_
49ef2 4c 45 53 53 3a 20 20 20 20 72 65 74 75 72 6e 20  LESS:    return 
49ef3 2d 31 3b 0a 20 20 20 20 63 61 73 65 20 55 43 4f  -1;.    case UCO
49ef4 4c 5f 47 52 45 41 54 45 52 3a 20 72 65 74 75 72  L_GREATER: retur
49ef5 6e 20 2b 31 3b 0a 20 20 20 20 63 61 73 65 20 55  n +1;.    case U
49ef6 43 4f 4c 5f 45 51 55 41 4c 3a 20 20 20 72 65 74  COL_EQUAL:   ret
49ef7 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 0;.  }.  ass
49ef8 65 72 74 28 21 22 55 6e 65 78 70 65 63 74 65 64  ert(!"Unexpected
49ef9 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72   return value fr
49efa 6f 6d 20 75 63 6f 6c 5f 73 74 72 63 6f 6c 6c 28  om ucol_strcoll(
49efb 29 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  )");.  return 0;
49efc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
49efd 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
49efe 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  scalar function 
49eff 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69  icu_load_collati
49f00 6f 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  on()..**.** This
49f01 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
49f02 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20   is used to add 
49f03 49 43 55 20 63 6f 6c 6c 61 74 69 6f 6e 20 62 61  ICU collation ba
49f04 73 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 2a  sed collation .*
49f05 2a 20 74 79 70 65 73 20 74 6f 20 61 6e 20 53 51  * types to an SQ
49f06 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 63 6f  Lite database co
49f07 6e 6e 65 63 74 69 6f 6e 2e 20 49 74 20 69 73 20  nnection. It is 
49f08 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 63  intended to be c
49f09 61 6c 6c 65 64 0a 2a 2a 20 61 73 20 66 6f 6c 6c  alled.** as foll
49f0a 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  ows:.**.**     S
49f0b 45 4c 45 43 54 20 69 63 75 5f 6c 6f 61 64 5f 63  ELECT icu_load_c
49f0c 6f 6c 6c 61 74 69 6f 6e 28 3c 6c 6f 63 61 6c 65  ollation(<locale
49f0d 3e 2c 20 3c 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61  >, <collation-na
49f0e 6d 65 3e 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 72  me>);.**.** Wher
49f0f 65 20 3c 6c 6f 63 61 6c 65 3e 20 69 73 20 61 20  e <locale> is a 
49f10 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e  string containin
49f11 67 20 61 6e 20 49 43 55 20 6c 6f 63 61 6c 65 20  g an ICU locale 
49f12 69 64 65 6e 74 69 66 69 65 72 20 28 69 2e 65 2e  identifier (i.e.
49f13 0a 2a 2a 20 22 65 6e 5f 41 55 22 2c 20 22 74 72  .** "en_AU", "tr
49f14 5f 54 52 22 20 65 74 63 2e 29 20 61 6e 64 20 3c  _TR" etc.) and <
49f15 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61 6d 65 3e 20  collation-name> 
49f16 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
49f17 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20  he.** collation 
49f18 73 65 71 75 65 6e 63 65 20 74 6f 20 63 72 65 61  sequence to crea
49f19 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  te..*/.static vo
49f1a 69 64 20 69 63 75 4c 6f 61 64 43 6f 6c 6c 61 74  id icuLoadCollat
49f1b 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  ion(.  sqlite3_c
49f1c 6f 6e 74 65 78 74 20 2a 70 2c 20 0a 20 20 69 6e  ontext *p, .  in
49f1d 74 20 6e 41 72 67 2c 20 0a 20 20 73 71 6c 69 74  t nArg, .  sqlit
49f1e 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67  e3_value **apArg
49f1f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
49f20 62 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 73  b = (sqlite3 *)s
49f21 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
49f22 28 70 29 3b 0a 20 20 55 45 72 72 6f 72 43 6f 64  (p);.  UErrorCod
49f23 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52  e status = U_ZER
49f24 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 6f 6e 73 74  O_ERROR;.  const
49f25 20 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 3b 20   char *zLocale; 
49f26 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 65 20 69       /* Locale i
49f27 64 65 6e 74 69 66 69 65 72 20 2d 20 28 65 67 2e  dentifier - (eg.
49f28 20 22 6a 70 5f 4a 50 22 29 20 2a 2f 0a 20 20 63   "jp_JP") */.  c
49f29 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
49f2a 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20  ;        /* SQL 
49f2b 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  Collation sequen
49f2c 63 65 20 6e 61 6d 65 20 28 65 67 2e 20 22 6a 61  ce name (eg. "ja
49f2d 70 61 6e 65 73 65 22 29 20 2a 2f 0a 20 20 55 43  panese") */.  UC
49f2e 6f 6c 6c 61 74 6f 72 20 2a 70 55 43 6f 6c 6c 61  ollator *pUColla
49f2f 74 6f 72 3b 20 20 20 20 2f 2a 20 49 43 55 20 6c  tor;    /* ICU l
49f30 69 62 72 61 72 79 20 63 6f 6c 6c 61 74 69 6f 6e  ibrary collation
49f31 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74   object */.  int
49f32 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
49f33 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
49f34 20 63 6f 64 65 20 66 72 6f 6d 20 73 71 6c 69 74   code from sqlit
49f35 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
49f36 69 6f 6e 5f 78 28 29 20 2a 2f 0a 0a 20 20 61 73  ion_x() */..  as
49f37 73 65 72 74 28 6e 41 72 67 3d 3d 32 29 3b 0a 20  sert(nArg==2);. 
49f38 20 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 6f 6e 73   zLocale = (cons
49f39 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
49f3a 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41 72  _value_text(apAr
49f3b 67 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d  g[0]);.  zName =
49f3c 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
49f3d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
49f3e 74 28 61 70 41 72 67 5b 31 5d 29 3b 0a 0a 20 20  t(apArg[1]);..  
49f3f 69 66 28 20 21 7a 4c 6f 63 61 6c 65 20 7c 7c 20  if( !zLocale || 
49f40 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 72 65  !zName ){.    re
49f41 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 55 43  turn;.  }..  pUC
49f42 6f 6c 6c 61 74 6f 72 20 3d 20 75 63 6f 6c 5f 6f  ollator = ucol_o
49f43 70 65 6e 28 7a 4c 6f 63 61 6c 65 2c 20 26 73 74  pen(zLocale, &st
49f44 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55 5f  atus);.  if( !U_
49f45 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29 20  SUCCESS(status) 
49f46 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74 69  ){.    icuFuncti
49f47 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 63 6f 6c  onError(p, "ucol
49f48 5f 6f 70 65 6e 22 2c 20 73 74 61 74 75 73 29 3b  _open", status);
49f49 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
49f4a 0a 20 20 61 73 73 65 72 74 28 70 29 3b 0a 0a 20  .  assert(p);.. 
49f4b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72   rc = sqlite3_cr
49f4c 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
49f4d 32 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c  2(db, zName, SQL
49f4e 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f 69 64  ITE_UTF16, (void
49f4f 20 2a 29 70 55 43 6f 6c 6c 61 74 6f 72 2c 20 0a   *)pUCollator, .
49f50 20 20 20 20 20 20 69 63 75 43 6f 6c 6c 61 74 69        icuCollati
49f51 6f 6e 43 6f 6c 6c 2c 20 69 63 75 43 6f 6c 6c 61  onColl, icuColla
49f52 74 69 6f 6e 44 65 6c 0a 20 20 29 3b 0a 20 20 69  tionDel.  );.  i
49f53 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
49f54 20 29 7b 0a 20 20 20 20 75 63 6f 6c 5f 63 6c 6f   ){.    ucol_clo
49f55 73 65 28 70 55 43 6f 6c 6c 61 74 6f 72 29 3b 0a  se(pUCollator);.
49f56 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
49f57 6c 74 5f 65 72 72 6f 72 28 70 2c 20 22 45 72 72  lt_error(p, "Err
49f58 6f 72 20 72 65 67 69 73 74 65 72 69 6e 67 20 63  or registering c
49f59 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  ollation functio
49f5a 6e 22 2c 20 2d 31 29 3b 0a 20 20 7d 0a 7d 0a 0a  n", -1);.  }.}..
49f5b 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74  /*.** Register t
49f5c 68 65 20 49 43 55 20 65 78 74 65 6e 73 69 6f 6e  he ICU extension
49f5d 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 74 68 20   functions with 
49f5e 64 61 74 61 62 61 73 65 20 64 62 2e 0a 2a 2f 0a  database db..*/.
49f5f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
49f60 6e 74 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69  nt sqlite3IcuIni
49f61 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  t(sqlite3 *db){.
49f62 20 20 73 74 72 75 63 74 20 49 63 75 53 63 61 6c    struct IcuScal
49f63 61 72 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ar {.    const c
49f64 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
49f65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49f66 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 6e     /* Function n
49f67 61 6d 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ame */.    int n
49f68 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
49f69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49f6a 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
49f6b 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20  f arguments */. 
49f6c 20 20 20 69 6e 74 20 65 6e 63 3b 20 20 20 20 20     int enc;     
49f6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49f6e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
49f6f 4f 70 74 69 6d 61 6c 20 74 65 78 74 20 65 6e 63  Optimal text enc
49f70 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20 76 6f 69  oding */.    voi
49f71 64 20 2a 70 43 6f 6e 74 65 78 74 3b 20 20 20 20  d *pContext;    
49f72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49f73 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65         /* sqlite
49f74 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 63 6f  3_user_data() co
49f75 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 76 6f 69  ntext */.    voi
49f76 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74  d (*xFunc)(sqlit
49f77 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
49f78 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
49f79 3b 0a 20 20 7d 20 73 63 61 6c 61 72 73 5b 5d 20  ;.  } scalars[] 
49f7a 3d 20 7b 0a 20 20 20 20 7b 22 72 65 67 65 78 70  = {.    {"regexp
49f7b 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 2, SQLITE_ANY
49f7c 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 69 63  ,          0, ic
49f7d 75 52 65 67 65 78 70 46 75 6e 63 7d 2c 0a 0a 20  uRegexpFunc},.. 
49f7e 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 31 2c     {"lower",  1,
49f7f 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 20   SQLITE_UTF16,  
49f80 20 20 20 20 20 20 30 2c 20 69 63 75 43 61 73 65        0, icuCase
49f81 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 6c  Func16},.    {"l
49f82 6f 77 65 72 22 2c 20 20 32 2c 20 53 51 4c 49 54  ower",  2, SQLIT
49f83 45 5f 55 54 46 31 36 2c 20 20 20 20 20 20 20 20  E_UTF16,        
49f84 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  0, icuCaseFunc16
49f85 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22 2c  },.    {"upper",
49f86 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 31    1, SQLITE_UTF1
49f87 36 2c 20 28 76 6f 69 64 2a 29 31 2c 20 69 63 75  6, (void*)1, icu
49f88 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20  CaseFunc16},.   
49f89 20 7b 22 75 70 70 65 72 22 2c 20 20 32 2c 20 53   {"upper",  2, S
49f8a 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f  QLITE_UTF16, (vo
49f8b 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75  id*)1, icuCaseFu
49f8c 6e 63 31 36 7d 2c 0a 0a 20 20 20 20 7b 22 6c 6f  nc16},..    {"lo
49f8d 77 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54 45  wer",  1, SQLITE
49f8e 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20 30  _UTF8,         0
49f8f 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d  , icuCaseFunc16}
49f90 2c 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20  ,.    {"lower", 
49f91 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
49f92 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 43           0, icuC
49f93 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20  aseFunc16},.    
49f94 7b 22 75 70 70 65 72 22 2c 20 20 31 2c 20 53 51  {"upper",  1, SQ
49f95 4c 49 54 45 5f 55 54 46 38 2c 20 20 28 76 6f 69  LITE_UTF8,  (voi
49f96 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75 6e  d*)1, icuCaseFun
49f97 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65  c16},.    {"uppe
49f98 72 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55  r",  2, SQLITE_U
49f99 54 46 38 2c 20 20 28 76 6f 69 64 2a 29 31 2c 20  TF8,  (void*)1, 
49f9a 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a  icuCaseFunc16},.
49f9b 0a 20 20 20 20 7b 22 6c 69 6b 65 22 2c 20 20 20  .    {"like",   
49f9c 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  2, SQLITE_UTF8, 
49f9d 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 4c 69          0, icuLi
49f9e 6b 65 46 75 6e 63 7d 2c 0a 20 20 20 20 7b 22 6c  keFunc},.    {"l
49f9f 69 6b 65 22 2c 20 20 20 33 2c 20 53 51 4c 49 54  ike",   3, SQLIT
49fa0 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20  E_UTF8,         
49fa1 30 2c 20 69 63 75 4c 69 6b 65 46 75 6e 63 7d 2c  0, icuLikeFunc},
49fa2 0a 0a 20 20 20 20 7b 22 69 63 75 5f 6c 6f 61 64  ..    {"icu_load
49fa3 5f 63 6f 6c 6c 61 74 69 6f 6e 22 2c 20 20 32 2c  _collation",  2,
49fa4 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 28 76   SQLITE_UTF8, (v
49fa5 6f 69 64 2a 29 64 62 2c 20 69 63 75 4c 6f 61 64  oid*)db, icuLoad
49fa6 43 6f 6c 6c 61 74 69 6f 6e 7d 2c 0a 20 20 7d 3b  Collation},.  };
49fa7 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
49fa8 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b  ITE_OK;.  int i;
49fa9 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d  ..  for(i=0; rc=
49faa 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c  =SQLITE_OK && i<
49fab 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 73 63 61  (int)(sizeof(sca
49fac 6c 61 72 73 29 2f 73 69 7a 65 6f 66 28 73 63 61  lars)/sizeof(sca
49fad 6c 61 72 73 5b 30 5d 29 29 3b 20 69 2b 2b 29 7b  lars[0])); i++){
49fae 0a 20 20 20 20 73 74 72 75 63 74 20 49 63 75 53  .    struct IcuS
49faf 63 61 6c 61 72 20 2a 70 20 3d 20 26 73 63 61 6c  calar *p = &scal
49fb0 61 72 73 5b 69 5d 3b 0a 20 20 20 20 72 63 20 3d  ars[i];.    rc =
49fb1 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
49fb2 66 75 6e 63 74 69 6f 6e 28 0a 20 20 20 20 20 20  function(.      
49fb3 20 20 64 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20    db, p->zName, 
49fb4 70 2d 3e 6e 41 72 67 2c 20 70 2d 3e 65 6e 63 2c  p->nArg, p->enc,
49fb5 20 70 2d 3e 70 43 6f 6e 74 65 78 74 2c 20 70 2d   p->pContext, p-
49fb6 3e 78 46 75 6e 63 2c 20 30 2c 20 30 0a 20 20 20  >xFunc, 0, 0.   
49fb7 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72   );.  }..  retur
49fb8 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 53 51  n rc;.}..#if !SQ
49fb9 4c 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49 54 45  LITE_CORE.SQLITE
49fba 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
49fbb 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 28  _extension_init(
49fbc 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
49fbd 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d  .  char **pzErrM
49fbe 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  sg,.  const sqli
49fbf 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
49fc0 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53 51 4c 49   *pApi.){.  SQLI
49fc1 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49  TE_EXTENSION_INI
49fc2 54 32 28 70 41 70 69 29 0a 20 20 72 65 74 75 72  T2(pApi).  retur
49fc3 6e 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69 74  n sqlite3IcuInit
49fc4 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  (db);.}.#endif..
49fc5 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
49fc6 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 69  ******* End of i
49fc7 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cu.c ***********
49fc8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49fc9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49fca 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
49fcb 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
49fcc 6c 65 20 66 74 73 33 5f 69 63 75 2e 63 20 2a 2a  le fts3_icu.c **
49fcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49fce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49fcf 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
49fd0 30 37 20 4a 75 6e 65 20 32 32 0a 2a 2a 0a 2a 2a  07 June 22.**.**
49fd1 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
49fd2 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
49fd3 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
49fd4 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
49fd5 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
49fd6 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
49fd7 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
49fd8 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
49fd9 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
49fda 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
49fdb 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
49fdc 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
49fdd 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
49fde 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
49fdf 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
49fe0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
49fe1 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
49fe2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49fe3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49fe4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49fe5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
49fe6 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
49fe7 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
49fe8 73 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f  s a tokenizer fo
49fe9 72 20 66 74 73 33 20 62 61 73 65 64 20 6f 6e 20  r fts3 based on 
49fea 74 68 65 20 49 43 55 20 6c 69 62 72 61 72 79 2e  the ICU library.
49feb 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
49fec 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
49fed 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
49fee 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 23 69 66  ENABLE_FTS3).#if
49fef 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
49ff0 45 5f 49 43 55 0a 0a 2f 2a 20 23 69 6e 63 6c 75  E_ICU../* #inclu
49ff1 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 20 2a 2f  de <assert.h> */
49ff2 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 3c 73 74  ./* #include <st
49ff3 72 69 6e 67 2e 68 3e 20 2a 2f 0a 0a 23 69 6e 63  ring.h> */..#inc
49ff4 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 62  lude <unicode/ub
49ff5 72 6b 2e 68 3e 0a 2f 2a 20 23 69 6e 63 6c 75 64  rk.h>./* #includ
49ff6 65 20 3c 75 6e 69 63 6f 64 65 2f 75 63 6f 6c 2e  e <unicode/ucol.
49ff7 68 3e 20 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64  h> */./* #includ
49ff8 65 20 3c 75 6e 69 63 6f 64 65 2f 75 73 74 72 69  e <unicode/ustri
49ff9 6e 67 2e 68 3e 20 2a 2f 0a 23 69 6e 63 6c 75 64  ng.h> */.#includ
49ffa 65 20 3c 75 6e 69 63 6f 64 65 2f 75 74 66 31 36  e <unicode/utf16
49ffb 2e 68 3e 0a 0a 74 79 70 65 64 65 66 20 73 74 72  .h>..typedef str
49ffc 75 63 74 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72  uct IcuTokenizer
49ffd 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 3b 0a 74   IcuTokenizer;.t
49ffe 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 63  ypedef struct Ic
49fff 75 43 75 72 73 6f 72 20 49 63 75 43 75 72 73 6f  uCursor IcuCurso
4a000 72 3b 0a 0a 73 74 72 75 63 74 20 49 63 75 54 6f  r;..struct IcuTo
4a001 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 73 71 6c 69  kenizer {.  sqli
4a002 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61  te3_tokenizer ba
4a003 73 65 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63  se;.  char *zLoc
4a004 61 6c 65 3b 0a 7d 3b 0a 0a 73 74 72 75 63 74 20  ale;.};..struct 
4a005 49 63 75 43 75 72 73 6f 72 20 7b 0a 20 20 73 71  IcuCursor {.  sq
4a006 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
4a007 63 75 72 73 6f 72 20 62 61 73 65 3b 0a 0a 20 20  cursor base;..  
4a008 55 42 72 65 61 6b 49 74 65 72 61 74 6f 72 20 2a  UBreakIterator *
4a009 70 49 74 65 72 3b 20 20 20 20 20 20 2f 2a 20 49  pIter;      /* I
4a00a 43 55 20 62 72 65 61 6b 2d 69 74 65 72 61 74 6f  CU break-iterato
4a00b 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e  r object */.  in
4a00c 74 20 6e 43 68 61 72 3b 20 20 20 20 20 20 20 20  t nChar;        
4a00d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
4a00e 62 65 72 20 6f 66 20 55 43 68 61 72 20 65 6c 65  ber of UChar ele
4a00f 6d 65 6e 74 73 20 69 6e 20 70 49 6e 70 75 74 20  ments in pInput 
4a010 2a 2f 0a 20 20 55 43 68 61 72 20 2a 61 43 68 61  */.  UChar *aCha
4a011 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
4a012 20 2f 2a 20 43 6f 70 79 20 6f 66 20 69 6e 70 75   /* Copy of inpu
4a013 74 20 75 73 69 6e 67 20 75 74 66 2d 31 36 20 65  t using utf-16 e
4a014 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  ncoding */.  int
4a015 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20   *aOffset;      
4a016 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
4a017 65 74 73 20 6f 66 20 65 61 63 68 20 63 68 61 72  ets of each char
4a018 61 63 74 65 72 20 69 6e 20 75 74 66 2d 38 20 69  acter in utf-8 i
4a019 6e 70 75 74 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e  nput */..  int n
4a01a 42 75 66 66 65 72 3b 0a 20 20 63 68 61 72 20 2a  Buffer;.  char *
4a01b 7a 42 75 66 66 65 72 3b 0a 0a 20 20 69 6e 74 20  zBuffer;..  int 
4a01c 69 54 6f 6b 65 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a  iToken;.};../*.*
4a01d 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 74  * Create a new t
4a01e 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e 63  okenizer instanc
4a01f 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
4a020 20 69 63 75 43 72 65 61 74 65 28 0a 20 20 69 6e   icuCreate(.  in
4a021 74 20 61 72 67 63 2c 20 20 20 20 20 20 20 20 20  t argc,         
4a022 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a023 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
4a024 65 6e 74 72 69 65 73 20 69 6e 20 61 72 67 76 5b  entries in argv[
4a025 5d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  ] */.  const cha
4a026 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c  r * const *argv,
4a027 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
4a028 6f 6b 65 6e 69 7a 65 72 20 63 72 65 61 74 69 6f  okenizer creatio
4a029 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20  n arguments */. 
4a02a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
4a02b 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72  er **ppTokenizer
4a02c 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 43 72        /* OUT: Cr
4a02d 65 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20  eated tokenizer 
4a02e 2a 2f 0a 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e  */.){.  IcuToken
4a02f 69 7a 65 72 20 2a 70 3b 0a 20 20 69 6e 74 20 6e  izer *p;.  int n
4a030 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 61 72 67   = 0;..  if( arg
4a031 63 3e 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73  c>0 ){.    n = s
4a032 74 72 6c 65 6e 28 61 72 67 76 5b 30 5d 29 2b 31  trlen(argv[0])+1
4a033 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 28 49 63 75  ;.  }.  p = (Icu
4a034 54 6f 6b 65 6e 69 7a 65 72 20 2a 29 73 71 6c 69  Tokenizer *)sqli
4a035 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
4a036 66 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 29 2b  f(IcuTokenizer)+
4a037 6e 29 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a  n);.  if( !p ){.
4a038 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
4a039 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d  E_NOMEM;.  }.  m
4a03a 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65  emset(p, 0, size
4a03b 6f 66 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 29  of(IcuTokenizer)
4a03c 29 3b 0a 0a 20 20 69 66 28 20 6e 20 29 7b 0a 20  );..  if( n ){. 
4a03d 20 20 20 70 2d 3e 7a 4c 6f 63 61 6c 65 20 3d 20     p->zLocale = 
4a03e 28 63 68 61 72 20 2a 29 26 70 5b 31 5d 3b 0a 20  (char *)&p[1];. 
4a03f 20 20 20 6d 65 6d 63 70 79 28 70 2d 3e 7a 4c 6f     memcpy(p->zLo
4a040 63 61 6c 65 2c 20 61 72 67 76 5b 30 5d 2c 20 6e  cale, argv[0], n
4a041 29 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 54 6f 6b  );.  }..  *ppTok
4a042 65 6e 69 7a 65 72 20 3d 20 28 73 71 6c 69 74 65  enizer = (sqlite
4a043 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 3b  3_tokenizer *)p;
4a044 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
4a045 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  E_OK;.}../*.** D
4a046 65 73 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a  estroy a tokeniz
4a047 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  er.*/.static int
4a048 20 69 63 75 44 65 73 74 72 6f 79 28 73 71 6c 69   icuDestroy(sqli
4a049 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
4a04a 54 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 49 63  Tokenizer){.  Ic
4a04b 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 20 3d 20  uTokenizer *p = 
4a04c 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 29  (IcuTokenizer *)
4a04d 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71  pTokenizer;.  sq
4a04e 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20  lite3_free(p);. 
4a04f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
4a050 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70  K;.}../*.** Prep
4a051 61 72 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b  are to begin tok
4a052 65 6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63  enizing a partic
4a053 75 6c 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68  ular string.  Th
4a054 65 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e  e input.** strin
4a055 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65  g to be tokenize
4a056 64 20 69 73 20 70 49 6e 70 75 74 5b 30 2e 2e 6e  d is pInput[0..n
4a057 42 79 74 65 73 2d 31 5d 2e 20 20 41 20 63 75 72  Bytes-1].  A cur
4a058 73 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69  sor.** used to i
4a059 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b  ncrementally tok
4a05a 65 6e 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e  enize this strin
4a05b 67 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  g is returned in
4a05c 20 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a   .** *ppCursor..
4a05d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63  */.static int ic
4a05e 75 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  uOpen(.  sqlite3
4a05f 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
4a060 65 6e 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20  enizer,         
4a061 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72  /* The tokenizer
4a062 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
4a063 20 2a 7a 49 6e 70 75 74 2c 20 20 20 20 20 20 20   *zInput,       
4a064 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4a065 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a  Input string */.
4a066 20 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20    int nInput,   
4a067 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a068 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
4a069 74 68 20 6f 66 20 7a 49 6e 70 75 74 20 69 6e 20  th of zInput in 
4a06a 62 79 74 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74  bytes */.  sqlit
4a06b 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
4a06c 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20  sor **ppCursor  
4a06d 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69    /* OUT: Tokeni
4a06e 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f  zation cursor */
4a06f 0a 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a  .){.  IcuTokeniz
4a070 65 72 20 2a 70 20 3d 20 28 49 63 75 54 6f 6b 65  er *p = (IcuToke
4a071 6e 69 7a 65 72 20 2a 29 70 54 6f 6b 65 6e 69 7a  nizer *)pTokeniz
4a072 65 72 3b 0a 20 20 49 63 75 43 75 72 73 6f 72 20  er;.  IcuCursor 
4a073 2a 70 43 73 72 3b 0a 0a 20 20 63 6f 6e 73 74 20  *pCsr;..  const 
4a074 69 6e 74 33 32 5f 74 20 6f 70 74 20 3d 20 55 5f  int32_t opt = U_
4a075 46 4f 4c 44 5f 43 41 53 45 5f 44 45 46 41 55 4c  FOLD_CASE_DEFAUL
4a076 54 3b 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20  T;.  UErrorCode 
4a077 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f  status = U_ZERO_
4a078 45 52 52 4f 52 3b 0a 20 20 69 6e 74 20 6e 43 68  ERROR;.  int nCh
4a079 61 72 3b 0a 0a 20 20 55 43 68 61 72 33 32 20 63  ar;..  UChar32 c
4a07a 3b 0a 20 20 69 6e 74 20 69 49 6e 70 75 74 20 3d  ;.  int iInput =
4a07b 20 30 3b 0a 20 20 69 6e 74 20 69 4f 75 74 20 3d   0;.  int iOut =
4a07c 20 30 3b 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72   0;..  *ppCursor
4a07d 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 7a 49 6e   = 0;..  if( zIn
4a07e 70 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 49  put==0 ){.    nI
4a07f 6e 70 75 74 20 3d 20 30 3b 0a 20 20 20 20 7a 49  nput = 0;.    zI
4a080 6e 70 75 74 20 3d 20 22 22 3b 0a 20 20 7d 65 6c  nput = "";.  }el
4a081 73 65 20 69 66 28 20 6e 49 6e 70 75 74 3c 30 20  se if( nInput<0 
4a082 29 7b 0a 20 20 20 20 6e 49 6e 70 75 74 20 3d 20  ){.    nInput = 
4a083 73 74 72 6c 65 6e 28 7a 49 6e 70 75 74 29 3b 0a  strlen(zInput);.
4a084 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d 20 6e 49    }.  nChar = nI
4a085 6e 70 75 74 2b 31 3b 0a 20 20 70 43 73 72 20 3d  nput+1;.  pCsr =
4a086 20 28 49 63 75 43 75 72 73 6f 72 20 2a 29 73 71   (IcuCursor *)sq
4a087 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 0a 20 20  lite3_malloc(.  
4a088 20 20 20 20 73 69 7a 65 6f 66 28 49 63 75 43 75      sizeof(IcuCu
4a089 72 73 6f 72 29 20 2b 20 20 20 20 20 20 20 20 20  rsor) +         
4a08a 20 20 20 20 20 20 20 2f 2a 20 49 63 75 43 75 72         /* IcuCur
4a08b 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 28 28 6e  sor */.      ((n
4a08c 43 68 61 72 2b 33 29 26 7e 33 29 20 2a 20 73 69  Char+3)&~3) * si
4a08d 7a 65 6f 66 28 55 43 68 61 72 29 20 2b 20 20 20  zeof(UChar) +   
4a08e 2f 2a 20 49 63 75 43 75 72 73 6f 72 2e 61 43 68  /* IcuCursor.aCh
4a08f 61 72 5b 5d 20 2a 2f 0a 20 20 20 20 20 20 28 6e  ar[] */.      (n
4a090 43 68 61 72 2b 31 29 20 2a 20 73 69 7a 65 6f 66  Char+1) * sizeof
4a091 28 69 6e 74 29 20 20 20 20 20 20 20 20 20 20 20  (int)           
4a092 20 2f 2a 20 49 63 75 43 75 72 73 6f 72 2e 61 4f   /* IcuCursor.aO
4a093 66 66 73 65 74 5b 5d 20 2a 2f 0a 20 20 29 3b 0a  ffset[] */.  );.
4a094 20 20 69 66 28 20 21 70 43 73 72 20 29 7b 0a 20    if( !pCsr ){. 
4a095 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
4a096 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65  _NOMEM;.  }.  me
4a097 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20 73 69  mset(pCsr, 0, si
4a098 7a 65 6f 66 28 49 63 75 43 75 72 73 6f 72 29 29  zeof(IcuCursor))
4a099 3b 0a 20 20 70 43 73 72 2d 3e 61 43 68 61 72 20  ;.  pCsr->aChar 
4a09a 3d 20 28 55 43 68 61 72 20 2a 29 26 70 43 73 72  = (UChar *)&pCsr
4a09b 5b 31 5d 3b 0a 20 20 70 43 73 72 2d 3e 61 4f 66  [1];.  pCsr->aOf
4a09c 66 73 65 74 20 3d 20 28 69 6e 74 20 2a 29 26 70  fset = (int *)&p
4a09d 43 73 72 2d 3e 61 43 68 61 72 5b 28 6e 43 68 61  Csr->aChar[(nCha
4a09e 72 2b 33 29 26 7e 33 5d 3b 0a 0a 20 20 70 43 73  r+3)&~3];..  pCs
4a09f 72 2d 3e 61 4f 66 66 73 65 74 5b 69 4f 75 74 5d  r->aOffset[iOut]
4a0a0 20 3d 20 69 49 6e 70 75 74 3b 0a 20 20 55 38 5f   = iInput;.  U8_
4a0a1 4e 45 58 54 28 7a 49 6e 70 75 74 2c 20 69 49 6e  NEXT(zInput, iIn
4a0a2 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20 63 29 3b  put, nInput, c);
4a0a3 20 0a 20 20 77 68 69 6c 65 28 20 63 3e 30 20 29   .  while( c>0 )
4a0a4 7b 0a 20 20 20 20 69 6e 74 20 69 73 45 72 72 6f  {.    int isErro
4a0a5 72 20 3d 20 30 3b 0a 20 20 20 20 63 20 3d 20 75  r = 0;.    c = u
4a0a6 5f 66 6f 6c 64 43 61 73 65 28 63 2c 20 6f 70 74  _foldCase(c, opt
4a0a7 29 3b 0a 20 20 20 20 55 31 36 5f 41 50 50 45 4e  );.    U16_APPEN
4a0a8 44 28 70 43 73 72 2d 3e 61 43 68 61 72 2c 20 69  D(pCsr->aChar, i
4a0a9 4f 75 74 2c 20 6e 43 68 61 72 2c 20 63 2c 20 69  Out, nChar, c, i
4a0aa 73 45 72 72 6f 72 29 3b 0a 20 20 20 20 69 66 28  sError);.    if(
4a0ab 20 69 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 20   isError ){.    
4a0ac 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
4a0ad 43 73 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75  Csr);.      retu
4a0ae 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
4a0af 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 73 72 2d  .    }.    pCsr-
4a0b0 3e 61 4f 66 66 73 65 74 5b 69 4f 75 74 5d 20 3d  >aOffset[iOut] =
4a0b1 20 69 49 6e 70 75 74 3b 0a 0a 20 20 20 20 69 66   iInput;..    if
4a0b2 28 20 69 49 6e 70 75 74 3c 6e 49 6e 70 75 74 20  ( iInput<nInput 
4a0b3 29 7b 0a 20 20 20 20 20 20 55 38 5f 4e 45 58 54  ){.      U8_NEXT
4a0b4 28 7a 49 6e 70 75 74 2c 20 69 49 6e 70 75 74 2c  (zInput, iInput,
4a0b5 20 6e 49 6e 70 75 74 2c 20 63 29 3b 0a 20 20 20   nInput, c);.   
4a0b6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 20   }else{.      c 
4a0b7 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  = 0;.    }.  }..
4a0b8 20 20 70 43 73 72 2d 3e 70 49 74 65 72 20 3d 20    pCsr->pIter = 
4a0b9 75 62 72 6b 5f 6f 70 65 6e 28 55 42 52 4b 5f 57  ubrk_open(UBRK_W
4a0ba 4f 52 44 2c 20 70 2d 3e 7a 4c 6f 63 61 6c 65 2c  ORD, p->zLocale,
4a0bb 20 70 43 73 72 2d 3e 61 43 68 61 72 2c 20 69 4f   pCsr->aChar, iO
4a0bc 75 74 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20  ut, &status);.  
4a0bd 69 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73  if( !U_SUCCESS(s
4a0be 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20 73 71  tatus) ){.    sq
4a0bf 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72 29  lite3_free(pCsr)
4a0c0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
4a0c1 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
4a0c2 20 70 43 73 72 2d 3e 6e 43 68 61 72 20 3d 20 69   pCsr->nChar = i
4a0c3 4f 75 74 3b 0a 0a 20 20 75 62 72 6b 5f 66 69 72  Out;..  ubrk_fir
4a0c4 73 74 28 70 43 73 72 2d 3e 70 49 74 65 72 29 3b  st(pCsr->pIter);
4a0c5 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 28  .  *ppCursor = (
4a0c6 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
4a0c7 72 5f 63 75 72 73 6f 72 20 2a 29 70 43 73 72 3b  r_cursor *)pCsr;
4a0c8 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
4a0c9 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
4a0ca 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69  ose a tokenizati
4a0cb 6f 6e 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f  on cursor previo
4a0cc 75 73 6c 79 20 6f 70 65 6e 65 64 20 62 79 20 61  usly opened by a
4a0cd 20 63 61 6c 6c 20 74 6f 20 69 63 75 4f 70 65 6e   call to icuOpen
4a0ce 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ()..*/.static in
4a0cf 74 20 69 63 75 43 6c 6f 73 65 28 73 71 6c 69 74  t icuClose(sqlit
4a0d0 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
4a0d1 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20  sor *pCursor){. 
4a0d2 20 49 63 75 43 75 72 73 6f 72 20 2a 70 43 73 72   IcuCursor *pCsr
4a0d3 20 3d 20 28 49 63 75 43 75 72 73 6f 72 20 2a 29   = (IcuCursor *)
4a0d4 70 43 75 72 73 6f 72 3b 0a 20 20 75 62 72 6b 5f  pCursor;.  ubrk_
4a0d5 63 6c 6f 73 65 28 70 43 73 72 2d 3e 70 49 74 65  close(pCsr->pIte
4a0d6 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  r);.  sqlite3_fr
4a0d7 65 65 28 70 43 73 72 2d 3e 7a 42 75 66 66 65 72  ee(pCsr->zBuffer
4a0d8 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
4a0d9 65 28 70 43 73 72 29 3b 0a 20 20 72 65 74 75 72  e(pCsr);.  retur
4a0da 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
4a0db 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68  /*.** Extract th
4a0dc 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f  e next token fro
4a0dd 6d 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e  m a tokenization
4a0de 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74   cursor..*/.stat
4a0df 69 63 20 69 6e 74 20 69 63 75 4e 65 78 74 28 0a  ic int icuNext(.
4a0e0 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
4a0e1 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
4a0e2 73 6f 72 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20  sor,  /* Cursor 
4a0e3 72 65 74 75 72 6e 65 64 20 62 79 20 73 69 6d 70  returned by simp
4a0e4 6c 65 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73  leOpen */.  cons
4a0e5 74 20 63 68 61 72 20 2a 2a 70 70 54 6f 6b 65 6e  t char **ppToken
4a0e6 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
4a0e7 2f 2a 20 4f 55 54 3a 20 2a 70 70 54 6f 6b 65 6e  /* OUT: *ppToken
4a0e8 20 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 65   is the token te
4a0e9 78 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42  xt */.  int *pnB
4a0ea 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20  ytes,           
4a0eb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
4a0ec 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79  UT: Number of by
4a0ed 74 65 73 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a  tes in token */.
4a0ee 20 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66    int *piStartOf
4a0ef 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20  fset,           
4a0f0 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74        /* OUT: St
4a0f1 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20 6f 66  arting offset of
4a0f2 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
4a0f3 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20  *piEndOffset,   
4a0f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a0f5 2f 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f  /* OUT: Ending o
4a0f6 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a  ffset of token *
4a0f7 2f 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73 69 74  /.  int *piPosit
4a0f8 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ion             
4a0f9 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
4a0fa 50 6f 73 69 74 69 6f 6e 20 69 6e 74 65 67 65 72  Position integer
4a0fb 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a   of token */.){.
4a0fc 20 20 49 63 75 43 75 72 73 6f 72 20 2a 70 43 73    IcuCursor *pCs
4a0fd 72 20 3d 20 28 49 63 75 43 75 72 73 6f 72 20 2a  r = (IcuCursor *
4a0fe 29 70 43 75 72 73 6f 72 3b 0a 0a 20 20 69 6e 74  )pCursor;..  int
4a0ff 20 69 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 69   iStart = 0;.  i
4a100 6e 74 20 69 45 6e 64 20 3d 20 30 3b 0a 20 20 69  nt iEnd = 0;.  i
4a101 6e 74 20 6e 42 79 74 65 20 3d 20 30 3b 0a 0a 20  nt nByte = 0;.. 
4a102 20 77 68 69 6c 65 28 20 69 53 74 61 72 74 3d 3d   while( iStart==
4a103 69 45 6e 64 20 29 7b 0a 20 20 20 20 55 43 68 61  iEnd ){.    UCha
4a104 72 33 32 20 63 3b 0a 0a 20 20 20 20 69 53 74 61  r32 c;..    iSta
4a105 72 74 20 3d 20 75 62 72 6b 5f 63 75 72 72 65 6e  rt = ubrk_curren
4a106 74 28 70 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a  t(pCsr->pIter);.
4a107 20 20 20 20 69 45 6e 64 20 3d 20 75 62 72 6b 5f      iEnd = ubrk_
4a108 6e 65 78 74 28 70 43 73 72 2d 3e 70 49 74 65 72  next(pCsr->pIter
4a109 29 3b 0a 20 20 20 20 69 66 28 20 69 45 6e 64 3d  );.    if( iEnd=
4a10a 3d 55 42 52 4b 5f 44 4f 4e 45 20 29 7b 0a 20 20  =UBRK_DONE ){.  
4a10b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
4a10c 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20  E_DONE;.    }.. 
4a10d 20 20 20 77 68 69 6c 65 28 20 69 53 74 61 72 74     while( iStart
4a10e 3c 69 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 69  <iEnd ){.      i
4a10f 6e 74 20 69 57 68 69 74 65 20 3d 20 69 53 74 61  nt iWhite = iSta
4a110 72 74 3b 0a 20 20 20 20 20 20 55 31 36 5f 4e 45  rt;.      U16_NE
4a111 58 54 28 70 43 73 72 2d 3e 61 43 68 61 72 2c 20  XT(pCsr->aChar, 
4a112 69 57 68 69 74 65 2c 20 70 43 73 72 2d 3e 6e 43  iWhite, pCsr->nC
4a113 68 61 72 2c 20 63 29 3b 0a 20 20 20 20 20 20 69  har, c);.      i
4a114 66 28 20 75 5f 69 73 73 70 61 63 65 28 63 29 20  f( u_isspace(c) 
4a115 29 7b 0a 20 20 20 20 20 20 20 20 69 53 74 61 72  ){.        iStar
4a116 74 20 3d 20 69 57 68 69 74 65 3b 0a 20 20 20 20  t = iWhite;.    
4a117 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
4a118 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
4a119 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
4a11a 28 69 53 74 61 72 74 3c 3d 69 45 6e 64 29 3b 0a  (iStart<=iEnd);.
4a11b 20 20 7d 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20    }..  do {.    
4a11c 55 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75  UErrorCode statu
4a11d 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52  s = U_ZERO_ERROR
4a11e 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 20  ;.    if( nByte 
4a11f 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
4a120 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65  New = sqlite3_re
4a121 61 6c 6c 6f 63 28 70 43 73 72 2d 3e 7a 42 75 66  alloc(pCsr->zBuf
4a122 66 65 72 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20  fer, nByte);.   
4a123 20 20 20 69 66 28 20 21 7a 4e 65 77 20 29 7b 0a     if( !zNew ){.
4a124 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
4a125 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
4a126 20 20 20 7d 0a 20 20 20 20 20 20 70 43 73 72 2d     }.      pCsr-
4a127 3e 7a 42 75 66 66 65 72 20 3d 20 7a 4e 65 77 3b  >zBuffer = zNew;
4a128 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 6e 42 75  .      pCsr->nBu
4a129 66 66 65 72 20 3d 20 6e 42 79 74 65 3b 0a 20 20  ffer = nByte;.  
4a12a 20 20 7d 0a 0a 20 20 20 20 75 5f 73 74 72 54 6f    }..    u_strTo
4a12b 55 54 46 38 28 0a 20 20 20 20 20 20 20 20 70 43  UTF8(.        pC
4a12c 73 72 2d 3e 7a 42 75 66 66 65 72 2c 20 70 43 73  sr->zBuffer, pCs
4a12d 72 2d 3e 6e 42 75 66 66 65 72 2c 20 26 6e 42 79  r->nBuffer, &nBy
4a12e 74 65 2c 20 20 20 20 2f 2a 20 4f 75 74 70 75 74  te,    /* Output
4a12f 20 76 61 72 73 20 2a 2f 0a 20 20 20 20 20 20 20   vars */.       
4a130 20 26 70 43 73 72 2d 3e 61 43 68 61 72 5b 69 53   &pCsr->aChar[iS
4a131 74 61 72 74 5d 2c 20 69 45 6e 64 2d 69 53 74 61  tart], iEnd-iSta
4a132 72 74 2c 20 20 20 20 20 20 20 2f 2a 20 49 6e 70  rt,       /* Inp
4a133 75 74 20 76 61 72 73 20 2a 2f 0a 20 20 20 20 20  ut vars */.     
4a134 20 20 20 26 73 74 61 74 75 73 20 20 20 20 20 20     &status      
4a135 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a136 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
4a137 75 74 70 75 74 20 73 75 63 63 65 73 73 2f 66 61  utput success/fa
4a138 69 6c 75 72 65 20 2a 2f 0a 20 20 20 20 29 3b 0a  ilure */.    );.
4a139 20 20 7d 20 77 68 69 6c 65 28 20 6e 42 79 74 65    } while( nByte
4a13a 3e 70 43 73 72 2d 3e 6e 42 75 66 66 65 72 20 29  >pCsr->nBuffer )
4a13b 3b 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e 20 3d 20  ;..  *ppToken = 
4a13c 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 3b 0a 20  pCsr->zBuffer;. 
4a13d 20 2a 70 6e 42 79 74 65 73 20 3d 20 6e 42 79 74   *pnBytes = nByt
4a13e 65 3b 0a 20 20 2a 70 69 53 74 61 72 74 4f 66 66  e;.  *piStartOff
4a13f 73 65 74 20 3d 20 70 43 73 72 2d 3e 61 4f 66 66  set = pCsr->aOff
4a140 73 65 74 5b 69 53 74 61 72 74 5d 3b 0a 20 20 2a  set[iStart];.  *
4a141 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 70 43  piEndOffset = pC
4a142 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69 45 6e 64  sr->aOffset[iEnd
4a143 5d 3b 0a 20 20 2a 70 69 50 6f 73 69 74 69 6f 6e  ];.  *piPosition
4a144 20 3d 20 70 43 73 72 2d 3e 69 54 6f 6b 65 6e 2b   = pCsr->iToken+
4a145 2b 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  +;..  return SQL
4a146 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
4a147 20 54 68 65 20 73 65 74 20 6f 66 20 72 6f 75 74   The set of rout
4a148 69 6e 65 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ines that implem
4a149 65 6e 74 20 74 68 65 20 73 69 6d 70 6c 65 20 74  ent the simple t
4a14a 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74  okenizer.*/.stat
4a14b 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
4a14c 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
4a14d 65 20 69 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f  e icuTokenizerMo
4a14e 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 20 20 20  dule = {.  0,   
4a14f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a150 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
4a151 69 6f 6e 20 2a 2f 0a 20 20 69 63 75 43 72 65 61  ion */.  icuCrea
4a152 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
4a153 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65        /* xCreate
4a154 20 20 2a 2f 0a 20 20 69 63 75 44 65 73 74 72 6f    */.  icuDestro
4a155 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y,              
4a156 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 20      /* xCreate  
4a157 2a 2f 0a 20 20 69 63 75 4f 70 65 6e 2c 20 20 20  */.  icuOpen,   
4a158 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a159 20 20 2f 2a 20 78 4f 70 65 6e 20 20 20 20 2a 2f    /* xOpen    */
4a15a 0a 20 20 69 63 75 43 6c 6f 73 65 2c 20 20 20 20  .  icuClose,    
4a15b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a15c 2f 2a 20 78 43 6c 6f 73 65 20 20 20 2a 2f 0a 20  /* xClose   */. 
4a15d 20 69 63 75 4e 65 78 74 2c 20 20 20 20 20 20 20   icuNext,       
4a15e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4a15f 20 78 4e 65 78 74 20 20 20 20 2a 2f 0a 7d 3b 0a   xNext    */.};.
4a160 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 70 4d 6f  ./*.** Set *ppMo
4a161 64 75 6c 65 20 74 6f 20 70 6f 69 6e 74 20 61 74  dule to point at
4a162 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
4a163 69 6f 6e 20 6f 66 20 74 68 65 20 49 43 55 20 74  ion of the ICU t
4a164 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 53 51 4c  okenizer..*/.SQL
4a165 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4a166 20 73 71 6c 69 74 65 33 46 74 73 33 49 63 75 54   sqlite3Fts3IcuT
4a167 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 0a  okenizerModule(.
4a168 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
4a169 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
4a16a 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20  **ppModule.){.  
4a16b 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26 69 63 75  *ppModule = &icu
4a16c 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b  TokenizerModule;
4a16d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65  .}..#endif /* de
4a16e 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
4a16f 42 4c 45 5f 49 43 55 29 20 2a 2f 0a 23 65 6e 64  BLE_ICU) */.#end
4a170 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
4a171 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
4a172 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
4a173 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f  ABLE_FTS3) */../
4a174 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
4a175 6e 64 20 6f 66 20 66 74 73 33 5f 69 63 75 2e 63  nd of fts3_icu.c
4a176 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
4a177 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a178 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a     *************/.